# lib/header.c File Reference

`#include "system.h"`
`#include <header_internal.h>`
`#include "debug.h"`

Go to the source code of this file.

## Data Structures

struct | headerIteratorS
| Header tag iterator data structure. More...

## Defines

#define | __HEADER_PROTOTYPES__
#define | PARSER_BEGIN 0
#define | PARSER_IN_ARRAY 1
#define | PARSER_IN_EXPR 2
#define | hdrchkTags(_ntags) ((_ntags) & 0xffff0000)
| Sanity check on no.
#define | hdrchkData(_nbytes) ((_nbytes) & 0xff000000)
| Sanity check on data size and/or offset.

## Functions

const char *const | tagName (int tag)
static void * | _free (const void *p)
| Wrapper to free(3), hides const compilation noise, permit NULL, return NULL.
static Header | headerLink (Header h)
| Reference a header instance.
static Header | headerUnlink (Header h)
| Dereference a header instance.
static Header | headerFree (Header h)
| Dereference a header instance.
static Header | headerNew (void)
| Create new (empty) header instance.
static int | indexCmp (const void *avp, const void *bvp)
static void | headerSort (Header h)
| Sort tags in header.
static int | offsetCmp (const void *avp, const void *bvp)
static void | headerUnsort (Header h)
| Restore tags in header to original ordering.
static unsigned int | headerSizeof (Header h, enum hMagic magicp)
| Return size of on-disk header representation in bytes.
static int | dataLength (int_32 type, hPTR_t p, int_32 count, int onDisk)
| Return length of entry data.
static int | regionSwab (indexEntry entry, int il, int dl, entryInfo pe, char *dataStart, int regionid)
| Swap int_32 and int_16 arrays within header region.
static void * | doHeaderUnload (Header h, int *lengthPtr)
static void * | headerUnload (Header h)
| Convert header to on-disk representation.
static indexEntry | findEntry (Header h, int_32 tag, int_32 type)
| Find matching (tag,type) entry in header.
static int | headerRemoveEntry (Header h, int_32 tag)
| Delete tag in header.
static Header | headerLoad (void *uh)
| Convert header to in-memory representation.
static Header | headerReload (Header h, int tag)
| Convert header to on-disk representation, and then reload.
static Header | headerCopyLoad (const void *uh)
| Make a copy and convert header to in-memory representation.
static Header | headerRead (FD_t fd, enum hMagic magicp)
| Read (and load) header from file handle.
static int | headerWrite (FD_t fd, Header h, enum hMagic magicp)
| Write (with unload) header to file handle.
static int | headerIsEntry (Header h, int_32 tag)
| Check if tag is in header.
static int | copyEntry (const indexEntry entry, hTYP_t type, hPTR_t *p, hCNT_t c, int minMem)
| Retrieve data from header entry.
static int | headerMatchLocale (const char *td, const char *l, const char *le)
| Does locale match entry in header i18n table?
static char * | headerFindI18NString (Header h, indexEntry entry)
| Return i18n string from header that matches locale.
static int | intGetEntry (Header h, int_32 tag, hTAG_t type, hPTR_t *p, hCNT_t c, int minMem)
| Retrieve tag data from header.
static void * | headerFreeTag (Header h, const void *data, rpmTagType type)
| Free data allocated when retrieved from header.
static int | headerGetEntry (Header h, int_32 tag, hTYP_t type, void **p, hCNT_t c)
| Retrieve tag value.
static int | headerGetEntryMinMemory (Header h, int_32 tag, hTYP_t type, hPTR_t *p, hCNT_t c)
| Retrieve tag value using header internal array.
int | headerGetRawEntry (Header h, int_32 tag, int_32 *type, hPTR_t *p, int_32 *c)
static void | copyData (int_32 type, void *dstPtr, const void *srcPtr, int_32 c, int dataLength)
static void * | grabData (int_32 type, hPTR_t p, int_32 c, int *lengthPtr)
| Return (malloc'ed) copy of entry data.
static int | headerAddEntry (Header h, int_32 tag, int_32 type, const void *p, int_32 c)
| Add tag to header.
static int | headerAppendEntry (Header h, int_32 tag, int_32 type, const void *p, int_32 c)
| Append element to tag array in header.
static int | headerAddOrAppendEntry (Header h, int_32 tag, int_32 type, const void *p, int_32 c)
| Add or append element to tag array in header.
static int | headerAddI18NString (Header h, int_32 tag, const char *string, const char *lang)
| Add locale specific tag to header.
static int | headerModifyEntry (Header h, int_32 tag, int_32 type, const void *p, int_32 c)
| Modify tag in header.
static char | escapedChar (const char ch)
static sprintfToken | freeFormat (sprintfToken format, int num)
| Destroy headerSprintf format array.
static void | findTag (char *name, const headerTagTableEntry tags, const headerSprintfExtension extensions, headerTagTableEntry *tagMatch, headerSprintfExtension *extMatch)
static int | parseExpression (sprintfToken token, char *str, const headerTagTableEntry tags, const headerSprintfExtension extensions,char **endPtr,errmsg_t *errmsg)
static int | parseFormat (char *str, const headerTagTableEntry tags, const headerSprintfExtension extensions, sprintfToken *formatPtr, int *numTokensPtr, char **endPtr, int state, errmsg_t *errmsg)
static int | getExtension (Header h, headerTagTagFunction fn, hTYP_t typeptr, hPTR_t *data, hCNT_t countptr, extensionCache ext)
static char * | formatValue (sprintfTag tag, Header h, const headerSprintfExtension extensions, extensionCache extCache, int element)
static const char * | singleSprintf (Header h, sprintfToken token, const headerSprintfExtension extensions, extensionCache extCache, int element)
static extensionCache | allocateExtensionCache (const headerSprintfExtension extensions)
static extensionCache | freeExtensionCache (const headerSprintfExtension extensions, extensionCache cache)
static char * | headerSprintf (Header h, const char *fmt, const struct headerTagTableEntry_s *tbltags, const struct headerSprintfExtension_s *extensions, errmsg_t *errmsg)
| Return formatted output string from header tags.
static char * | octalFormat (int_32 type, hPTR_t data, char *formatPrefix, int padding, int element)
static char * | hexFormat (int_32 type, hPTR_t data, char *formatPrefix, int padding, int element)
static char * | realDateFormat (int_32 type, hPTR_t data, char *formatPrefix, int padding, int element, const char *strftimeFormat)
static char * | dateFormat (int_32 type, hPTR_t data, char *formatPrefix, int padding, int element)
static char * | dayFormat (int_32 type, hPTR_t data, char *formatPrefix, int padding, int element)
static char * | shescapeFormat (int_32 type, hPTR_t data, char *formatPrefix, int padding, int element)
static void | headerCopyTags (Header headerFrom, Header headerTo, hTAG_t tagstocopy)
| Duplicate tag values from one header into another.
static HeaderIterator | headerFreeIterator (HeaderIterator hi)
| Destroy header tag iterator.
static HeaderIterator | headerInitIterator (Header h)
| Create header tag iterator.
static int | headerNextIterator (HeaderIterator hi, hTAG_t tag, hTYP_t type, hPTR_t *p, hCNT_t c)
| Return next tag from header.
static Header | headerCopy (Header h)
| Duplicate a header.

## Variables

static unsigned char | header_magic [8]
static size_t | headerMaxbytes = (32*1024*1024)
| Maximum no.
static int | typeSizes []
| Alignment needs (and sizeof scalars types) for internal rpm data types.
HV_t | hdrVec
| Header methods for rpm headers.
const struct headerSprintfExtension_s | headerDefaultFormats []
| Supported default header tag output formats.
static struct HV_s | hdrVec1
HV_t | hdrVec = &hdrVec1
| Header methods for rpm headers.

## Detailed Description

Definition in file header.c.

## Define Documentation

#define __HEADER_PROTOTYPES__

| Definition at line 13 of file header.c.

#define hdrchkData | ( | _nbytes |  | ) | ((_nbytes) & 0xff000000)

| Sanity check on data size and/or offset. This check imposes a limit of 16Mb, more than enough. Definition at line 58 of file header.c.

#define hdrchkTags | ( | _ntags |  | ) | ((_ntags) & 0xffff0000)

| Sanity check on no. of tags. This check imposes a limit of 65K tags, more than enough. Definition at line 52 of file header.c.

#define PARSER_BEGIN 0

| Definition at line 31 of file header.c.

#define PARSER_IN_ARRAY 1

| Definition at line 32 of file header.c. Referenced by parseFormat().

#define PARSER_IN_EXPR 2

| Definition at line 33 of file header.c. Referenced by parseExpression(), and parseFormat().

## Function Documentation

static void* _free | ( | const void * | p | ) | `[inline, static]`

Wrapper to free(3), hides const compilation noise, permit NULL, return NULL.

Parameters:

  | p | memory to free

Returns:
  NULL always

Definition at line 86 of file header.c.

Referenced by addFileToTagAux(), addOrAppendListEntry(), addSource(), alAllFileSatisfiesDepend(), alFree(), alFreeIndex(), alSatisfiesDepend(), armorFormat(), base64Format(), buildForTarget(), checkPackageDeps(), checkSpec(), cleanOSE(), closeSpec(), cpio_doio(), cpioHeaderRead(), db1close(), db1open(), db3close(), db3open(), db3stat(), db_init(), dbiFreeIndexSet(), dbiTagsInit(), dbiUpdateRecord(), defaultMachine(), dnlFreeIterator(), domd5(), doPatch(), doReadRC(), doRmSource(), doScript(), doSetupMacro(), expandFilelist(), expandRegular(), fillOutMainPackage(), formatValue(), freeAttrRec(), freeExtensionCache(), freeFi(), freeFileList(), freeFilesystems(), freeFl(), freeFSM(), freeHardLink(), freeNames(), freePackage(), freeRpmVar(), freeSl(), freeSources(), freeSpec(), freeSplitString(), freeSt(), freeStringBuf(), freeTriggerFiles(), fsmCommitLinks(), fsmMakeLinks(), fsmMapPath(), fssizesTag(), genCpioListAndHeader(), getFilesystemList(), handlePreambleTag(), htFree(), i18nTag(), IDTXfree(), ignoreDep(), machCompatCacheAdd(), machFindEquivs(), main(), makeGPGSignature(), makePGPSignature(), makeTempFile(), mapFreeIterator(), newRpmdb(), packageBinaries(), packageSources(), parseDescription(), parseExpressionBoolean(), parseExpressionString(), parseFiles(), parseForRegexLang(), parseForRegexMultiLib(), parseScript(), parseSpec(), permsFormat(), pgpFreeDig(), pgpHexSet(), pgpReadPkts(), popMacro(), poptFreeContext(), poptGetNextOpt(), poptResetContext(), poptSetExecPath(), poptSetOtherOptionHelp(), printFileInfo(), processBinaryFile(), processPackageFiles(), processSourceFiles(), psmStage(), pushMacro(), readIcon(), readLine(), rhnUnload(), rpmCheckSig(), rpmdbClose(), rpmdbFindByFile(), rpmdbFreeIterator(), rpmdbNextIterator(), rpmdbRebuild(), rpmdbSetIteratorRE(), rpmdepFreeConflicts(), rpmDetectPGPVersion(), rpmErase(), rpmExpandNumeric(), rpmFreeMacros(), rpmFreeRpmrc(), rpmGenPath(), rpmGetPassPhrase(), rpmInstall(), rpmInstallSource(), rpmInstallSourcePackage(), rpmlogClose(), rpmLookupSignatureType(), rpmProblemPrint(), rpmProblemSetFree(), rpmQueryVerify(), rpmRangesOverlap(), rpmReadConfigFiles(), rpmReadPackageManifest(), rpmReSign(), rpmRollback(), rpmRunTransactions(), rpmSetMachine(), rpmSetVarArch(), rpmShowProgress(), rpmtransFree(), rpmVerifyDigest(), rpmVerifyScript(), saveHardLink(), setPathDefault(), showQueryPackage(), singleOptionDefaultValue(), singleOptionHelp(), singleSprintf(), skipFiles(), triggercondsTag(), tsFreeIterator(), urlSplit(), valueFree(), verifyGPGSignature(), verifyPGPSignature(), vrpmlog(), writeLinkedFile(), writeRPM(), XfdFree(), XurlFree(), and zapRelation().

static extensionCache allocateExtensionCache | ( | const headerSprintfExtension | extensions | ) | `[static]`

| Definition at line 2809 of file header.c. References HEADER_EXT_LAST, HEADER_EXT_MORE, headerSprintfExtension_s::more, headerSprintfExtension_s::type, headerSprintfExtension_s::u, and xcalloc().

static void copyData | ( | int_32 | type,
|  | void * | dstPtr,
|  | const void * | srcPtr,
|  | int_32 | c,
|  | int | dataLength
| ) | `[static]`

| Definition at line 1632 of file header.c. References RPM_I18NSTRING_TYPE, and RPM_STRING_ARRAY_TYPE. Referenced by grabData().

static int dataLength | ( | int_32 | type,
|  | hPTR_t | p,
|  | int_32 | count,
|  | int | onDisk
| ) | `[static]`

Return length of entry data.

Todo:
  Remove sanity check exit's.

Parameters:

  | type | entry data type
  | p | entry data
  | count | entry item count
  | onDisk | data is concatenated strings (with NUL's))?

Returns:
  no. bytes in data

Definition at line 319 of file header.c.

References _, EXIT_FAILURE, RPM_I18NSTRING_TYPE, RPM_STRING_ARRAY_TYPE, RPM_STRING_TYPE, and typeSizes.

Referenced by grabData(), and regionSwab().

static char* dateFormat | ( | int_32 | type,
|  | hPTR_t | data,
|  | char * | formatPrefix,
|  | int | padding,
|  | int | element
| ) | `[static]`

| Definition at line 3005 of file header.c. References realDateFormat().

static char* dayFormat | ( | int_32 | type,
|  | hPTR_t | data,
|  | char * | formatPrefix,
|  | int | padding,
|  | int | element
| ) | `[static]`

| Definition at line 3014 of file header.c. References realDateFormat().

static char escapedChar | ( | const char | ch | ) | `[static]`

| Definition at line 2003 of file header.c. Referenced by parseFormat().

static indexEntry findEntry | ( | Header | h,
|  | int_32 | tag,
|  | int_32 | type
| ) | `[static]`

Find matching (tag,type) entry in header.

Parameters:

  | h | header
  | tag | entry tag
  | type | entry type

Returns:
  header entry

Definition at line 771 of file header.c.

References headerToken::flags, HEADERFLAG_SORTED, headerSort(), indexCmp(), indexEntry::info, RPM_NULL_TYPE, entryInfo::tag, and entryInfo::type.

Referenced by addMacro(), delMacro(), expandMacro(), headerFindI18NString(), headerGetRawEntry(), htGetEntry(), htHasEntry(), and intGetEntry().

static void findTag | ( | char * | name,
|  | const headerTagTableEntry | tags,
|  | const headerSprintfExtension | extensions,
|  | headerTagTableEntry * | tagMatch,
|  | headerSprintfExtension * | extMatch
| ) | `[static]`

| Definition at line 2058 of file header.c. References alloca(), HEADER_EXT_LAST, HEADER_EXT_MORE, HEADER_EXT_TAG, headerSprintfExtension_s::more, headerTagTableEntry_s::name, headerSprintfExtension_s::name, stpcpy(), headerSprintfExtension_s::type, headerSprintfExtension_s::u, and xstrcasecmp(). Referenced by parseExpression(), and parseFormat().

static char* formatValue | ( | sprintfTag | tag,
|  | Header | h,
|  | const headerSprintfExtension | extensions,
|  | extensionCache | extCache,
|  | int | element
| ) | `[static]`

Definition at line 2515 of file header.c.

References _, _free(), headerSprintfExtension_s::formatFunction, getExtension(), HEADER_EXT_FORMAT, HEADER_EXT_LAST, HEADER_EXT_MORE, headerFreeData(), headerGetEntry(), headerSprintfExtension_s::more, headerSprintfExtension_s::name, RPM_BIN_TYPE, RPM_CHAR_TYPE, RPM_INT16_TYPE, RPM_INT32_TYPE, RPM_INT8_TYPE, RPM_STRING_ARRAY_TYPE, RPM_STRING_TYPE, stpcpy(), entryInfo::tag, headerSprintfExtension_s::type, headerSprintfExtension_s::u, xmalloc(), and xstrdup().

Referenced by singleSprintf().

static extensionCache freeExtensionCache | ( | const headerSprintfExtension | extensions,
|  | extensionCache | cache
| ) | `[static]`

Returns:
  NULL always

Definition at line 2832 of file header.c.

References _free(), extensionCache::data, HEADER_EXT_LAST, HEADER_EXT_MORE, headerSprintfExtension_s::more, headerSprintfExtension_s::type, and headerSprintfExtension_s::u.

static sprintfToken freeFormat | ( | sprintfToken | format,
|  | int | num
| ) | `[static]`

Destroy headerSprintf format array.

Parameters:

  | format | sprintf format array
  | num | number of elements

Returns:
  NULL always

Definition at line 2024 of file header.c.

References sprintfToken::array, sprintfToken::cond, and sprintfToken::u.

Referenced by parseExpression(), and parseFormat().

static int getExtension | ( | Header | h,
|  | headerTagTagFunction | fn,
|  | hTYP_t | typeptr,
|  | hPTR_t * | data,
|  | hCNT_t | countptr,
|  | extensionCache | ext
| ) | `[static]`

Returns:
  0 on success, 1 on failure

Definition at line 2493 of file header.c.

References extensionCache::avail, extensionCache::count, extensionCache::data, extensionCache::freeit, and extensionCache::type.

Referenced by formatValue(), and singleSprintf().

static void* grabData | ( | int_32 | type,
|  | hPTR_t | p,
|  | int_32 | c,
|  | int * | lengthPtr
| ) | `[static]`

Return (malloc'ed) copy of entry data.

Parameters:

  | type | entry data type
  | p | entry data
  | c | entry item count

Return values:

  | lengthPtr | no. bytes in returned data

Returns:
  (malloc'ed) copy of entry data

Definition at line 1671 of file header.c.

References copyData(), dataLength(), and xmalloc().

static char* headerFindI18NString | ( | Header | h,
|  | indexEntry | entry
| ) | `[static]`

Return i18n string from header that matches locale.

Parameters:

  | h | header
  | entry | i18n string data

Returns:
  matching i18n string (or 1st string if no match)

Definition at line 1445 of file header.c.

References indexEntry::data, findEntry(), getenv(), HEADER_I18NTABLE, headerMatchLocale(), lang, and RPM_STRING_ARRAY_TYPE.

Referenced by intGetEntry().

int headerGetRawEntry | ( | Header | h,
|  | int_32 | tag,
|  | int_32 * | type,
|  | hPTR_t * | p,
|  | int_32 * | c
| ) |

| Definition at line 1606 of file header.c. References copyEntry(), findEntry(), headerIsEntry(), and RPM_NULL_TYPE. Referenced by headerGetLangs().

static int headerMatchLocale | ( | const char * | td,
|  | const char * | l,
|  | const char * | le
| ) | `[static]`

Does locale match entry in header i18n table?

     * The range [l,le) contains the next locale to match:
     *    ll[_CC][.EEEEE][@dddd]
     * where
     *    ll	ISO language code (in lowercase).
     *    CC	(optional) ISO coutnry code (in uppercase).
     *    EEEEE	(optional) encoding (not really standardized).
     *    dddd	(optional) dialect.
     *

Parameters:

  | td | header i18n table data, NUL terminated
  | l | start of locale to match
  | le | end of locale to match

Returns:
  1 on match, 0 on no match

Definition at line 1372 of file header.c.

References alloca().

Referenced by headerFindI18NString().

static char* hexFormat | ( | int_32 | type,
|  | hPTR_t | data,
|  | char * | formatPrefix,
|  | int | padding,
|  | int | element
| ) | `[static]`

| Definition at line 2951 of file header.c. References _, RPM_INT32_TYPE, xmalloc(), and xstrdup().

static int indexCmp | ( | const void * | avp,
|  | const void * | bvp
| ) | `[static]`

| Definition at line 188 of file header.c. References indexEntry::info, and entryInfo::tag. Referenced by findEntry().

static int intGetEntry | ( | Header | h,
|  | int_32 | tag,
|  | hTAG_t | type,
|  | hPTR_t * | p,
|  | hCNT_t | c,
|  | int | minMem
| ) | `[static]`

Retrieve tag data from header.

Parameters:

  | h | header
  | tag | tag to retrieve

Return values:

  | type | address of type (or NULL)
  | p | address of data (or NULL)
  | c | address of count (or NULL)

Parameters:

  | minMem | string pointers reference header memory?

Returns:
  1 on success, 0 on not found

Definition at line 1499 of file header.c.

References copyEntry(), findEntry(), headerFindI18NString(), indexEntry::info, RPM_I18NSTRING_TYPE, RPM_NULL_TYPE, RPM_STRING_TYPE, and entryInfo::type.

static char* octalFormat | ( | int_32 | type,
|  | hPTR_t | data,
|  | char * | formatPrefix,
|  | int | padding,
|  | int | element
| ) | `[static]`

| Definition at line 2930 of file header.c. References _, RPM_INT32_TYPE, xmalloc(), and xstrdup().

static int offsetCmp | ( | const void * | avp,
|  | const void * | bvp
| ) | `[static]`

| Definition at line 212 of file header.c. References indexEntry::data, indexEntry::info, entryInfo::offset, and entryInfo::tag.

static int parseExpression | ( | sprintfToken | token,
|  | char * | str,
|  | const headerTagTableEntry | tags,
|  | const headerSprintfExtension | extensions,
|  | char ** | endPtr,
|  | errmsg_t * | errmsg
| ) | `[static]`

| Definition at line 2367 of file header.c. References _, sprintfToken::cond, findTag(), freeFormat(), parseFormat(), PARSER_IN_EXPR, entryInfo::tag, headerSprintfExtension_s::tagFunction, sprintfToken::type, headerSprintfExtension_s::u, sprintfToken::u, and xstrdup(). Referenced by parseFormat().

static int parseFormat | ( | char * | str,
|  | const headerTagTableEntry | tags,
|  | const headerSprintfExtension | extensions,
|  | sprintfToken * | formatPtr,
|  | int * | numTokensPtr,
|  | char ** | endPtr,
|  | int | state,
|  | errmsg_t * | errmsg
| ) | `[static]`

| Definition at line 2117 of file header.c. References _, escapedChar(), findTag(), freeFormat(), parseExpression(), PARSER_IN_ARRAY, PARSER_IN_EXPR, entryInfo::tag, headerSprintfExtension_s::tagFunction, headerSprintfExtension_s::u, xcalloc(), and xisdigit(). Referenced by parseExpression().

static char* realDateFormat | ( | int_32 | type,
|  | hPTR_t | data,
|  | char * | formatPrefix,
|  | int | padding,
|  | int | element,
|  | const char * | strftimeFormat
| ) | `[static]`

| Definition at line 2972 of file header.c. References _, RPM_INT32_TYPE, xmalloc(), and xstrdup(). Referenced by dateFormat(), and dayFormat().

static char* shescapeFormat | ( | int_32 | type,
|  | hPTR_t | data,
|  | char * | formatPrefix,
|  | int | padding,
|  | int | element
| ) | `[static]`

| Definition at line 3024 of file header.c. References alloca(), RPM_INT32_TYPE, and xmalloc().

static const char* singleSprintf | ( | Header | h,
|  | sprintfToken | token,
|  | const headerSprintfExtension | extensions,
|  | extensionCache | extCache,
|  | int | element
| ) | `[static]`

| Definition at line 2691 of file header.c. References _free(), sprintfToken::array, sprintfToken::cond, formatValue(), getExtension(), headerIsEntry(), sprintfTag::justOne, sprintfToken::string, sprintfToken::tag, sprintfToken::type, sprintfToken::u, xmalloc(), and xrealloc().

const char* const tagName | ( | int | tag | ) |

Definition at line 10 of file tagName.c.

Referenced by checkForDuplicates(), checkForRequired(), db1basename(), db3close(), db3open(), dbiCclose(), dbiClose(), dbiCopen(), dbiCount(), dbiDel(), dbiGet(), dbiOpen(), dbiPut(), dbiSearch(), dbiSync(), dbiVerify(), handlePreambleTag(), i18nTag(), printNewSpecfile(), rpmdbInitIterator(), rpmdbMoveDatabase(), rpmdbRemove(), rpmdbRemoveDatabase(), rpmHeaderGetEntry(), and stashSt().

## Variable Documentation

struct HV_s hdrVec1`[static]`

| Definition at line 3214 of file header.c.