5 /* RPM - Copyright (C) 1995-2002 Red Hat Software */
7 /* Data written to file descriptors is in network byte order. */
8 /* Data read from file descriptors is expected to be in */
9 /* network byte order and is converted on the fly to host order. */
13 #include <rpm/rpmtypes.h>
14 #include <rpm/rpmstring.h>
15 #include "lib/header_internal.h"
21 const unsigned char rpm_header_magic[8] = {
22 0x8e, 0xad, 0xe8, 0x01, 0x00, 0x00, 0x00, 0x00
26 * Alignment needed for header data types.
28 static const int typeAlign[16] = {
29 1, /*!< RPM_NULL_TYPE */
30 1, /*!< RPM_CHAR_TYPE */
31 1, /*!< RPM_INT8_TYPE */
32 2, /*!< RPM_INT16_TYPE */
33 4, /*!< RPM_INT32_TYPE */
34 8, /*!< RPM_INT64_TYPE */
35 1, /*!< RPM_STRING_TYPE */
36 1, /*!< RPM_BIN_TYPE */
37 1, /*!< RPM_STRING_ARRAY_TYPE */
38 1, /*!< RPM_I18NSTRING_TYPE */
48 * Size of header data types.
50 static const int typeSizes[16] = {
51 0, /*!< RPM_NULL_TYPE */
52 1, /*!< RPM_CHAR_TYPE */
53 1, /*!< RPM_INT8_TYPE */
54 2, /*!< RPM_INT16_TYPE */
55 4, /*!< RPM_INT32_TYPE */
56 8, /*!< RPM_INT64_TYPE */
57 -1, /*!< RPM_STRING_TYPE */
58 1, /*!< RPM_BIN_TYPE */
59 -1, /*!< RPM_STRING_ARRAY_TYPE */
60 -1, /*!< RPM_I18NSTRING_TYPE */
70 HEADERFLAG_SORTED = (1 << 0), /*!< Are header entries sorted? */
71 HEADERFLAG_ALLOCATED = (1 << 1), /*!< Is 1st header region allocated? */
72 HEADERFLAG_LEGACY = (1 << 2), /*!< Header came from legacy source? */
73 HEADERFLAG_DEBUG = (1 << 3), /*!< Debug this header? */
76 typedef rpmFlags headerFlags;
79 * The Header data structure.
81 struct headerToken_s {
82 void * blob; /*!< Header region blob. */
83 indexEntry index; /*!< Array of tags. */
84 int indexUsed; /*!< Current size of tag array. */
85 int indexAlloced; /*!< Allocated size of tag array. */
86 unsigned int instance; /*!< Rpmdb instance (offset) */
88 int nrefs; /*!< Reference count. */
92 * Maximum no. of bytes permitted in a header.
94 static const size_t headerMaxbytes = (32*1024*1024);
96 #define INDEX_MALLOC_SIZE 8
98 #define ENTRY_IS_REGION(_e) \
99 (((_e)->info.tag >= HEADER_IMAGE) && ((_e)->info.tag < HEADER_REGIONS))
100 #define ENTRY_IN_REGION(_e) ((_e)->info.offset < 0)
103 * HEADER_EXT_TAG format function prototype.
104 * This is allowed to fail, which indicates the tag doesn't exist.
107 * @retval td tag data container
108 * @param flags modifier flags
109 * @return 0 on success
111 typedef int (*headerTagTagFunction) (Header h, rpmtd td, headerGetFlags hgflags);
113 extern void *rpmHeaderTagFunc(rpmTag tag);
115 /* Convert a 64bit value to network byte order. */
116 static uint64_t htonll( uint64_t n ) {
117 uint32_t *i = (uint32_t*)&n;
124 Header headerLink(Header h)
131 static Header headerUnlink(Header h)
138 Header headerFree(Header h)
140 (void) headerUnlink(h);
142 if (h == NULL || h->nrefs > 0)
143 return NULL; /* XXX return previous header? */
146 indexEntry entry = h->index;
148 for (i = 0; i < h->indexUsed; i++, entry++) {
149 if ((h->flags & HEADERFLAG_ALLOCATED) && ENTRY_IS_REGION(entry)) {
150 if (entry->length > 0) {
151 int32_t * ei = entry->data;
152 if ((ei - 2) == h->blob) h->blob = _free(h->blob);
155 } else if (!ENTRY_IN_REGION(entry)) {
156 entry->data = _free(entry->data);
160 h->index = _free(h->index);
167 static Header headerCreate(void *blob, int32_t indexLen)
169 Header h = xcalloc(1, sizeof(*h));
172 h->indexAlloced = indexLen + 1;
173 h->indexUsed = indexLen;
175 h->indexAlloced = INDEX_MALLOC_SIZE;
179 h->flags |= HEADERFLAG_SORTED;
181 h->index = (h->indexAlloced
182 ? xcalloc(h->indexAlloced, sizeof(*h->index))
186 return headerLink(h);
189 Header headerNew(void)
191 return headerCreate(NULL, 0);
194 int headerVerifyInfo(int il, int dl, const void * pev, void * iv, int negate)
196 entryInfo pe = (entryInfo) pev;
200 for (i = 0; i < il; i++) {
201 info->tag = ntohl(pe[i].tag);
202 info->type = ntohl(pe[i].type);
203 info->offset = ntohl(pe[i].offset);
205 info->offset = -info->offset;
206 info->count = ntohl(pe[i].count);
208 if (hdrchkType(info->type))
210 if (hdrchkAlign(info->type, info->offset))
212 if (!negate && hdrchkRange(dl, info->offset))
214 if (hdrchkData(info->count))
223 static int indexCmp(const void * avp, const void * bvp)
225 indexEntry ap = (indexEntry) avp, bp = (indexEntry) bvp;
226 return (ap->info.tag - bp->info.tag);
229 void headerSort(Header h)
231 if (!(h->flags & HEADERFLAG_SORTED)) {
232 qsort(h->index, h->indexUsed, sizeof(*h->index), indexCmp);
233 h->flags |= HEADERFLAG_SORTED;
239 static int offsetCmp(const void * avp, const void * bvp)
241 indexEntry ap = (indexEntry) avp, bp = (indexEntry) bvp;
242 int rc = (ap->info.offset - bp->info.offset);
245 /* Within a region, entries sort by address. Added drips sort by tag. */
246 if (ap->info.offset < 0)
247 rc = (((char *)ap->data) - ((char *)bp->data));
249 rc = (ap->info.tag - bp->info.tag);
255 * Restore tags in header to original ordering.
258 void headerUnsort(Header h)
260 qsort(h->index, h->indexUsed, sizeof(*h->index), offsetCmp);
263 unsigned headerSizeof(Header h, enum hMagic magicp)
266 unsigned int size = 0;
275 case HEADER_MAGIC_YES:
276 size += sizeof(rpm_header_magic);
278 case HEADER_MAGIC_NO:
282 size += 2 * sizeof(int32_t); /* count of index entries */
284 for (i = 0, entry = h->index; i < h->indexUsed; i++, entry++) {
287 /* Regions go in as is ... */
288 if (ENTRY_IS_REGION(entry)) {
289 size += entry->length;
290 /* XXX Legacy regions do not include the region tag and data. */
291 if (i == 0 && (h->flags & HEADERFLAG_LEGACY))
292 size += sizeof(struct entryInfo_s) + entry->info.count;
296 /* ... and region elements are skipped. */
297 if (entry->info.offset < 0)
301 type = entry->info.type;
302 if (typeSizes[type] > 1) {
303 unsigned diff = typeSizes[type] - (size % typeSizes[type]);
304 if (diff != typeSizes[type]) {
309 size += sizeof(struct entryInfo_s) + entry->length;
316 * Return length of entry data.
317 * @param type entry data type
318 * @param p entry data
319 * @param count entry item count
320 * @param onDisk data is concatenated strings (with NUL's))?
321 * @param pend pointer to end of data (or NULL)
322 * @return no. bytes in data, -1 on failure
324 static int dataLength(rpmTagType type, rpm_constdata_t p, rpm_count_t count,
325 int onDisk, rpm_constdata_t pend)
327 const unsigned char * s = p;
328 const unsigned char * se = pend;
332 case RPM_STRING_TYPE:
340 length++; /* count nul terminator too. */
343 case RPM_STRING_ARRAY_TYPE:
344 case RPM_I18NSTRING_TYPE:
345 /* These are like RPM_STRING_TYPE, except they're *always* an array */
346 /* Compute sum of length of all strings, including nul terminators */
350 length++; /* count nul terminator too */
358 const char ** av = (const char **)p;
360 /* add one for null termination */
361 length += strlen(*av++) + 1;
367 if (typeSizes[type] == -1)
369 length = typeSizes[(type & 0xf)] * count;
370 if (length < 0 || (se && (s + length) > se))
379 * Swap int32_t and int16_t arrays within header region.
381 * If a header region tag is in the set to be swabbed, as the data for a
382 * a header region is located after all other tag data.
384 * @param entry header entry
385 * @param il no. of entries
386 * @param dl start no. bytes of data
387 * @param pe header physical entry pointer (swapped)
388 * @param dataStart header data start
389 * @param dataEnd header data end
390 * @param regionid region offset
391 * @return no. bytes of data in region, -1 on error
393 static int regionSwab(indexEntry entry, int il, int dl,
395 unsigned char * dataStart,
396 const unsigned char * dataEnd,
399 for (; il > 0; il--, pe++) {
400 struct indexEntry_s ie;
403 ie.info.tag = ntohl(pe->tag);
404 ie.info.type = ntohl(pe->type);
405 ie.info.count = ntohl(pe->count);
406 ie.info.offset = ntohl(pe->offset);
408 if (hdrchkType(ie.info.type))
410 if (hdrchkData(ie.info.count))
412 if (hdrchkData(ie.info.offset))
414 if (hdrchkAlign(ie.info.type, ie.info.offset))
417 ie.data = dataStart + ie.info.offset;
418 if (dataEnd && (unsigned char *)ie.data >= dataEnd)
421 ie.length = dataLength(ie.info.type, ie.data, ie.info.count, 1, dataEnd);
422 if (ie.length < 0 || hdrchkData(ie.length))
428 ie.info.offset = regionid;
429 *entry = ie; /* structure assignment */
435 if (typeSizes[type] > 1) {
436 unsigned diff = typeSizes[type] - (dl % typeSizes[type]);
437 if (diff != typeSizes[type]) {
442 /* Perform endian conversions */
443 switch (ntohl(pe->type)) {
445 { uint64_t * it = ie.data;
446 for (; ie.info.count > 0; ie.info.count--, it += 1) {
447 if (dataEnd && ((unsigned char *)it) >= dataEnd)
453 { int32_t * it = ie.data;
454 for (; ie.info.count > 0; ie.info.count--, it += 1) {
455 if (dataEnd && ((unsigned char *)it) >= dataEnd)
461 { int16_t * it = ie.data;
462 for (; ie.info.count > 0; ie.info.count--, it += 1) {
463 if (dataEnd && ((unsigned char *)it) >= dataEnd)
479 * @retval *lengthPtr no. bytes in unloaded header blob
480 * @return unloaded header blob (NULL on error)
482 static void * doHeaderUnload(Header h,
495 int drlen, ndribbles;
497 if (h == NULL) return NULL;
499 /* Sort entries by (offset,tag). */
502 /* Compute (il,dl) for all tags, including those deleted in region. */
503 drlen = ndribbles = 0;
504 for (i = 0, entry = h->index; i < h->indexUsed; i++, entry++) {
505 if (ENTRY_IS_REGION(entry)) {
506 int32_t rdl = -entry->info.offset; /* negative offset */
507 int32_t ril = rdl/sizeof(*pe);
508 int rid = entry->info.offset;
511 dl += entry->rdlen + entry->info.count;
512 /* XXX Legacy regions do not include the region tag and data. */
513 if (i == 0 && (h->flags & HEADERFLAG_LEGACY))
516 /* Skip rest of entries in region, but account for dribbles. */
517 for (; i < h->indexUsed && entry->info.offset <= rid+1; i++, entry++) {
518 if (entry->info.offset <= rid)
522 type = entry->info.type;
523 if (typeSizes[type] > 1) {
524 unsigned diff = typeSizes[type] - (dl % typeSizes[type]);
525 if (diff != typeSizes[type]) {
533 drlen += entry->length;
541 /* Ignore deleted drips. */
542 if (entry->data == NULL || entry->length <= 0)
546 type = entry->info.type;
547 if (typeSizes[type] > 1) {
548 unsigned diff = typeSizes[type] - (dl % typeSizes[type]);
549 if (diff != typeSizes[type]) {
558 /* Sanity checks on header intro. */
559 if (hdrchkTags(il) || hdrchkData(dl))
562 len = sizeof(il) + sizeof(dl) + (il * sizeof(*pe)) + dl;
568 pe = (entryInfo) &ei[2];
569 dataStart = te = (char *) (pe + il);
571 for (i = 0, entry = h->index; i < h->indexUsed; i++, entry++) {
577 if (entry->data == NULL || entry->length <= 0)
580 t = (unsigned char*)te;
581 pe->tag = htonl(entry->info.tag);
582 pe->type = htonl(entry->info.type);
583 pe->count = htonl(entry->info.count);
585 if (ENTRY_IS_REGION(entry)) {
586 int32_t rdl = -entry->info.offset; /* negative offset */
587 int32_t ril = rdl/sizeof(*pe) + ndribbles;
588 int rid = entry->info.offset;
590 src = (char *)entry->data;
591 rdlen = entry->rdlen;
593 /* XXX Legacy regions do not include the region tag and data. */
594 if (i == 0 && (h->flags & HEADERFLAG_LEGACY)) {
597 memcpy(pe+1, src, rdl);
598 memcpy(te, src + rdl, rdlen);
601 pe->offset = htonl(te - dataStart);
604 stei[2] = htonl(-rdl-entry->info.count);
606 memcpy(te, stei, entry->info.count);
607 te += entry->info.count;
609 rdlen += entry->info.count;
611 count = regionSwab(NULL, ril, 0, pe, t, NULL, 0);
617 memcpy(pe+1, src + sizeof(*pe), ((ril-1) * sizeof(*pe)));
618 memcpy(te, src + (ril * sizeof(*pe)), rdlen+entry->info.count+drlen);
621 entryInfo se = (entryInfo)src;
622 int off = ntohl(se->offset);
623 pe->offset = (off) ? htonl(te - dataStart) : htonl(off);
625 te += entry->info.count + drlen;
627 count = regionSwab(NULL, ril, 0, pe, t, NULL, 0);
628 if (count != (rdlen + entry->info.count + drlen))
632 /* Skip rest of entries in region. */
633 while (i < h->indexUsed && entry->info.offset <= rid+1) {
643 /* Ignore deleted drips. */
644 if (entry->data == NULL || entry->length <= 0)
648 type = entry->info.type;
649 if (typeSizes[type] > 1) {
651 diff = typeSizes[type] - ((te - dataStart) % typeSizes[type]);
652 if (diff != typeSizes[type]) {
658 pe->offset = htonl(te - dataStart);
660 /* copy data w/ endian conversions */
661 switch (entry->info.type) {
663 count = entry->info.count;
666 *((uint64_t *)te) = htonll(*((uint64_t *)src));
667 te += sizeof(uint64_t);
668 src += sizeof(uint64_t);
673 count = entry->info.count;
676 *((int32_t *)te) = htonl(*((int32_t *)src));
677 te += sizeof(int32_t);
678 src += sizeof(int32_t);
683 count = entry->info.count;
686 *((int16_t *)te) = htons(*((int16_t *)src));
687 te += sizeof(int16_t);
688 src += sizeof(int16_t);
693 memcpy(te, entry->data, entry->length);
700 /* Insure that there are no memcpy underruns/overruns. */
701 if (((char *)pe) != dataStart)
703 if ((((char *)ei)+len) != te)
709 h->flags &= ~HEADERFLAG_SORTED;
719 void * headerUnload(Header h)
722 void * uh = doHeaderUnload(h, &length);
727 * Find matching (tag,type) entry in header.
729 * @param tag entry tag
730 * @param type entry type
731 * @return header entry
734 indexEntry findEntry(Header h, rpmTag tag, rpmTagType type)
737 struct indexEntry_s key;
739 if (h == NULL) return NULL;
740 if (!(h->flags & HEADERFLAG_SORTED)) headerSort(h);
744 entry = bsearch(&key, h->index, h->indexUsed, sizeof(*h->index), indexCmp);
748 if (type == RPM_NULL_TYPE)
752 while (entry->info.tag == tag && entry->info.type != type &&
753 entry > h->index) entry--;
755 if (entry->info.tag == tag && entry->info.type == type)
761 int headerDel(Header h, rpmTag tag)
763 indexEntry last = h->index + h->indexUsed;
764 indexEntry entry, first;
767 entry = findEntry(h, tag, RPM_NULL_TYPE);
768 if (!entry) return 1;
770 /* Make sure entry points to the first occurence of this tag. */
771 while (entry > h->index && (entry - 1)->info.tag == tag)
774 /* Free data for tags being removed. */
775 for (first = entry; first < last; first++) {
777 if (first->info.tag != tag)
782 if (ENTRY_IN_REGION(first))
787 ne = (first - entry);
792 memmove(entry, first, (ne * sizeof(*entry)));
798 Header headerLoad(void * uh)
800 int32_t * ei = (int32_t *) uh;
801 int32_t il = ntohl(ei[0]); /* index length */
802 int32_t dl = ntohl(ei[1]); /* data length */
803 size_t pvlen = sizeof(il) + sizeof(dl) +
804 (il * sizeof(struct entryInfo_s)) + dl;
808 unsigned char * dataStart;
809 unsigned char * dataEnd;
813 /* Sanity checks on header intro. */
814 if (hdrchkTags(il) || hdrchkData(dl))
818 pe = (entryInfo) &ei[2];
819 dataStart = (unsigned char *) (pe + il);
820 dataEnd = dataStart + dl;
822 h = headerCreate(uh, il);
825 if (!(htonl(pe->tag) < HEADER_I18NTABLE)) {
826 h->flags |= HEADERFLAG_LEGACY;
827 entry->info.type = REGION_TAG_TYPE;
828 entry->info.tag = HEADER_IMAGE;
829 entry->info.count = REGION_TAG_COUNT;
830 entry->info.offset = ((unsigned char *)pe - dataStart); /* negative offset */
833 entry->length = pvlen - sizeof(il) - sizeof(dl);
834 rdlen = regionSwab(entry+1, il, 0, pe, dataStart, dataEnd, entry->info.offset);
837 entry->rdlen = rdlen;
843 h->flags &= ~HEADERFLAG_LEGACY;
845 entry->info.type = htonl(pe->type);
846 entry->info.count = htonl(pe->count);
848 if (hdrchkType(entry->info.type))
850 if (hdrchkTags(entry->info.count))
853 { int off = ntohl(pe->offset);
858 size_t nb = REGION_TAG_COUNT;
860 /* XXX Hmm, why the copy? */
861 memcpy(&stei, dataStart + off, nb);
862 rdl = -ntohl(stei[2]); /* negative offset */
863 ril = rdl/sizeof(*pe);
864 if (hdrchkTags(ril) || hdrchkData(rdl))
866 entry->info.tag = htonl(pe->tag);
869 rdl = (ril * sizeof(struct entryInfo_s));
870 entry->info.tag = HEADER_IMAGE;
873 entry->info.offset = -rdl; /* negative offset */
876 entry->length = pvlen - sizeof(il) - sizeof(dl);
877 rdlen = regionSwab(entry+1, ril-1, 0, pe+1, dataStart, dataEnd, entry->info.offset);
880 entry->rdlen = rdlen;
882 if (ril < h->indexUsed) {
883 indexEntry newEntry = entry + ril;
884 int ne = (h->indexUsed - ril);
885 int rid = entry->info.offset+1;
888 /* Load dribble entries from region. */
889 rc = regionSwab(newEntry, ne, 0, pe+ril, dataStart, dataEnd, rid);
894 { indexEntry firstEntry = newEntry;
895 int save = h->indexUsed;
898 /* Dribble entries replace duplicate region entries. */
900 for (j = 0; j < ne; j++, newEntry++) {
901 (void) headerDel(h, newEntry->info.tag);
902 if (newEntry->info.tag == RPMTAG_BASENAMES)
903 (void) headerDel(h, RPMTAG_OLDFILENAMES);
906 /* If any duplicate entries were replaced, move new entries down. */
907 if (h->indexUsed < (save - ne)) {
908 memmove(h->index + h->indexUsed, firstEntry,
909 (ne * sizeof(*entry)));
916 h->flags &= ~HEADERFLAG_SORTED;
918 h->flags |= HEADERFLAG_ALLOCATED;
924 h->index = _free(h->index);
930 Header headerReload(Header h, rpmTag tag)
934 void * uh = doHeaderUnload(h, &length);
944 if (ENTRY_IS_REGION(nh->index)) {
945 if (tag == HEADER_SIGNATURES || tag == HEADER_IMMUTABLE)
946 nh->index[0].info.tag = tag;
951 Header headerCopyLoad(const void * uh)
953 int32_t * ei = (int32_t *) uh;
954 int32_t il = ntohl(ei[0]); /* index length */
955 int32_t dl = ntohl(ei[1]); /* data length */
956 size_t pvlen = sizeof(il) + sizeof(dl) +
957 (il * sizeof(struct entryInfo_s)) + dl;
961 /* Sanity checks on header intro. */
962 if (!(hdrchkTags(il) || hdrchkData(dl)) && pvlen < headerMaxbytes) {
963 nuh = memcpy(xmalloc(pvlen), uh, pvlen);
964 if ((h = headerLoad(nuh)) == NULL)
971 * Read (and load) header from file handle.
972 * @param fd file handle
973 * @param magicp read (and verify) 8 bytes of (magic, 0)?
974 * @return header (or NULL on error)
976 Header headerRead(FD_t fd, enum hMagic magicp)
988 memset(block, 0, sizeof(block));
990 if (magicp == HEADER_MAGIC_YES)
994 if (timedRead(fd, (char *)block, i*sizeof(*block)) != (i * sizeof(*block)))
999 if (magicp == HEADER_MAGIC_YES) {
1001 if (memcmp(&magic, rpm_header_magic, sizeof(magic)))
1003 reserved = block[i++];
1006 il = ntohl(block[i]); i++;
1007 dl = ntohl(block[i]); i++;
1009 len = sizeof(il) + sizeof(dl) + (il * sizeof(struct entryInfo_s)) + dl;
1011 /* Sanity checks on header intro. */
1012 if (hdrchkTags(il) || hdrchkData(dl) || len > headerMaxbytes)
1018 len -= sizeof(il) + sizeof(dl);
1021 if (timedRead(fd, (char *)&ei[2], len) != len)
1027 if (h == NULL && ei != NULL) {
1033 int headerWrite(FD_t fd, Header h, enum hMagic magicp)
1039 uh = doHeaderUnload(h, &length);
1043 case HEADER_MAGIC_YES:
1044 nb = Fwrite(rpm_header_magic, sizeof(uint8_t), sizeof(rpm_header_magic), fd);
1045 if (nb != sizeof(rpm_header_magic))
1048 case HEADER_MAGIC_NO:
1052 nb = Fwrite(uh, sizeof(char), length, fd);
1056 return (nb == length ? 0 : 1);
1059 int headerIsEntry(Header h, rpmTag tag)
1061 /* FIX: h modified by sort. */
1062 return (findEntry(h, tag, RPM_NULL_TYPE) ? 1 : 0);
1067 * Retrieve data from header entry.
1068 * Relevant flags (others are ignored), if neither is set allocation
1069 * behavior depends on data type(!)
1070 * HEADERGET_MINMEM: return pointers to header memory
1071 * HEADERGET_ALLOC: always return malloced memory, overrides MINMEM
1073 * @todo Permit retrieval of regions other than HEADER_IMUTABLE.
1074 * @param entry header entry
1075 * @param td tag data container
1076 * @param minMem string pointers refer to header memory?
1077 * @param flags flags to control memory allocation
1078 * @return 1 on success, otherwise error.
1080 static int copyTdEntry(const indexEntry entry, rpmtd td, headerGetFlags flags)
1082 rpm_count_t count = entry->info.count;
1083 int rc = 1; /* XXX 1 on success. */
1084 /* ALLOC overrides MINMEM */
1085 int allocMem = flags & HEADERGET_ALLOC;
1086 int minMem = allocMem ? 0 : flags & HEADERGET_MINMEM;
1087 int argvArray = (flags & HEADERGET_ARGV) ? 1 : 0;
1090 td->flags = RPMTD_IMMUTABLE;
1091 switch (entry->info.type) {
1094 * XXX This only works for
1095 * XXX "sealed" HEADER_IMMUTABLE/HEADER_SIGNATURES/HEADER_IMAGE.
1096 * XXX This will *not* work for unsealed legacy HEADER_IMAGE (i.e.
1097 * XXX a legacy header freshly read, but not yet unloaded to the rpmdb).
1099 if (ENTRY_IS_REGION(entry)) {
1100 int32_t * ei = ((int32_t *)entry->data) - 2;
1101 entryInfo pe = (entryInfo) (ei + 2);
1102 unsigned char * dataStart = (unsigned char *) (pe + ntohl(ei[0]));
1103 int32_t rdl = -entry->info.offset; /* negative offset */
1104 int32_t ril = rdl/sizeof(*pe);
1107 count = 2 * sizeof(*ei) + (ril * sizeof(*pe)) + rdl;
1108 if (entry->info.tag == HEADER_IMAGE) {
1112 count += REGION_TAG_COUNT;
1113 rdl += REGION_TAG_COUNT;
1116 td->data = xmalloc(count);
1117 ei = (int32_t *) td->data;
1121 pe = (entryInfo) memcpy(ei + 2, pe, (ril * sizeof(*pe)));
1123 dataStart = (unsigned char *) memcpy(pe + ril, dataStart, rdl);
1125 rc = regionSwab(NULL, ril, 0, pe, dataStart, dataStart + rdl, 0);
1126 /* don't return data on failure */
1128 td->data = _free(td->data);
1130 /* XXX 1 on success. */
1131 rc = (rc < 0) ? 0 : 1;
1133 count = entry->length;
1135 ? memcpy(xmalloc(count), entry->data, count)
1139 case RPM_STRING_TYPE:
1140 /* simple string, but fallthrough if its actually an array */
1141 if (count == 1 && !argvArray) {
1142 td->data = allocMem ? xstrdup(entry->data) : entry->data;
1145 case RPM_STRING_ARRAY_TYPE:
1146 case RPM_I18NSTRING_TYPE:
1147 { const char ** ptrEntry;
1148 int tableSize = (count + argvArray) * sizeof(char *);
1153 td->data = xmalloc(tableSize);
1154 ptrEntry = (const char **) td->data;
1157 t = xmalloc(tableSize + entry->length);
1158 td->data = (void *)t;
1159 ptrEntry = (const char **) td->data;
1161 memcpy(t, entry->data, entry->length);
1163 for (i = 0; i < count; i++) {
1170 td->flags |= RPMTD_ARGV;
1175 case RPM_INT16_TYPE:
1176 case RPM_INT32_TYPE:
1177 case RPM_INT64_TYPE:
1179 td->data = xmalloc(entry->length);
1180 memcpy(td->data, entry->data, entry->length);
1182 td->data = entry->data;
1186 /* WTH? Don't mess with unknown data types... */
1191 td->type = entry->info.type;
1194 if (td->data && entry->data != td->data) {
1195 td->flags |= RPMTD_ALLOCED;
1202 * Does locale match entry in header i18n table?
1205 * The range [l,le) contains the next locale to match:
1206 * ll[_CC][.EEEEE][@dddd]
1208 * ll ISO language code (in lowercase).
1209 * CC (optional) ISO coutnry code (in uppercase).
1210 * EEEEE (optional) encoding (not really standardized).
1211 * dddd (optional) dialect.
1214 * @param td header i18n table data, NUL terminated
1215 * @param l start of locale to match
1216 * @param le end of locale to match
1217 * @return 1 on good match, 2 on weak match, 0 on no match
1219 static int headerMatchLocale(const char *td, const char *l, const char *le)
1223 /* First try a complete match. */
1224 if (strlen(td) == (le-l) && rstreqn(td, l, (le - l)))
1227 /* Next, try stripping optional dialect and matching. */
1228 for (fe = l; fe < le && *fe != '@'; fe++)
1230 if (fe < le && rstreqn(td, l, (fe - l)))
1233 /* Next, try stripping optional codeset and matching. */
1234 for (fe = l; fe < le && *fe != '.'; fe++)
1236 if (fe < le && rstreqn(td, l, (fe - l)))
1239 /* Finally, try stripping optional country code and matching. */
1240 for (fe = l; fe < le && *fe != '_'; fe++)
1242 if (fe < le && rstreqn(td, l, (fe - l)))
1249 * Return i18n string from header that matches locale.
1251 * @param entry i18n string data
1252 * @retval td tag data container
1253 * @param flags flags to control allocation
1256 static int copyI18NEntry(Header h, indexEntry entry, rpmtd td,
1257 headerGetFlags flags)
1259 const char *lang, *l, *le;
1262 td->type = RPM_STRING_TYPE;
1264 /* if no match, just return the first string */
1265 td->data = entry->data;
1267 /* XXX Drepper sez' this is the order. */
1268 if ((lang = getenv("LANGUAGE")) == NULL &&
1269 (lang = getenv("LC_ALL")) == NULL &&
1270 (lang = getenv("LC_MESSAGES")) == NULL &&
1271 (lang = getenv("LANG")) == NULL)
1274 if ((table = findEntry(h, HEADER_I18NTABLE, RPM_STRING_ARRAY_TYPE)) == NULL)
1277 for (l = lang; *l != '\0'; l = le) {
1279 char *ed, *ed_weak = NULL;
1282 while (*l && *l == ':') /* skip leading colons */
1286 for (le = l; *le && *le != ':'; le++) /* find end of this locale */
1289 /* For each entry in the header ... */
1290 for (langNum = 0, t = table->data, ed = entry->data;
1291 langNum < entry->info.count;
1292 langNum++, t += strlen(t) + 1, ed += strlen(ed) + 1) {
1294 int match = headerMatchLocale(t, l, le);
1298 } else if (match == 2) {
1309 if (flags & HEADERGET_ALLOC) {
1310 td->data = xstrdup(td->data);
1311 td->flags |= RPMTD_ALLOCED;
1318 * Retrieve tag data from header.
1320 * @retval td tag data container
1321 * @param flags flags to control retrieval
1322 * @return 1 on success, 0 on not found
1324 static int intGetTdEntry(Header h, rpmtd td, headerGetFlags flags)
1329 /* First find the tag */
1330 /* FIX: h modified by sort. */
1331 entry = findEntry(h, td->tag, RPM_NULL_TYPE);
1332 if (entry == NULL) {
1333 /* Td is zeroed above, just return... */
1337 if (flags & HEADERGET_RAW) {
1338 rc = copyTdEntry(entry, td, flags);
1340 switch (entry->info.type) {
1341 case RPM_I18NSTRING_TYPE:
1342 rc = copyI18NEntry(h, entry, td, flags);
1345 rc = copyTdEntry(entry, td, flags);
1350 /* XXX 1 on success */
1351 return ((rc == 1) ? 1 : 0);
1354 int headerGet(Header h, rpmTag tag, rpmtd td, headerGetFlags flags)
1357 headerTagTagFunction tagfunc = intGetTdEntry;
1359 if (td == NULL) return 0;
1364 if (flags & HEADERGET_EXT) {
1365 headerTagTagFunction extfunc = rpmHeaderTagFunc(tag);
1366 if (extfunc) tagfunc = extfunc;
1368 rc = tagfunc(h, td, flags);
1370 assert(tag == td->tag);
1376 static void copyData(rpmTagType type, rpm_data_t dstPtr,
1377 rpm_constdata_t srcPtr, rpm_count_t cnt, int dataLength)
1380 case RPM_STRING_ARRAY_TYPE:
1381 case RPM_I18NSTRING_TYPE:
1382 { const char ** av = (const char **) srcPtr;
1385 while (cnt-- > 0 && dataLength > 0) {
1387 if ((s = *av++) == NULL)
1391 } while (s[-1] && --dataLength > 0);
1396 memmove(dstPtr, srcPtr, dataLength);
1402 * Return (malloc'ed) copy of entry data.
1403 * @param type entry data type
1404 * @param p entry data
1405 * @param c entry item count
1406 * @retval lengthPtr no. bytes in returned data
1407 * @return (malloc'ed) copy of entry data, NULL on error
1410 grabData(rpmTagType type, rpm_constdata_t p, rpm_count_t c, int * lengthPtr)
1412 rpm_data_t data = NULL;
1415 length = dataLength(type, p, c, 0, NULL);
1417 data = xmalloc(length);
1418 copyData(type, data, p, c, length);
1422 *lengthPtr = length;
1426 static int intAddEntry(Header h, rpmtd td)
1432 /* Count must always be >= 1 for headerAddEntry. */
1436 if (hdrchkType(td->type))
1438 if (hdrchkData(td->count))
1442 data = grabData(td->type, td->data, td->count, &length);
1443 if (data == NULL || length <= 0)
1446 /* Allocate more index space if necessary */
1447 if (h->indexUsed == h->indexAlloced) {
1448 h->indexAlloced += INDEX_MALLOC_SIZE;
1449 h->index = xrealloc(h->index, h->indexAlloced * sizeof(*h->index));
1452 /* Fill in the index */
1453 entry = h->index + h->indexUsed;
1454 entry->info.tag = td->tag;
1455 entry->info.type = td->type;
1456 entry->info.count = td->count;
1457 entry->info.offset = 0;
1459 entry->length = length;
1461 if (h->indexUsed > 0 && td->tag < h->index[h->indexUsed-1].info.tag)
1462 h->flags &= ~HEADERFLAG_SORTED;
1468 static int intAppendEntry(Header h, rpmtd td)
1473 if (td->type == RPM_STRING_TYPE || td->type == RPM_I18NSTRING_TYPE) {
1474 /* we can't do this */
1478 /* Find the tag entry in the header. */
1479 entry = findEntry(h, td->tag, td->type);
1483 length = dataLength(td->type, td->data, td->count, 0, NULL);
1487 if (ENTRY_IN_REGION(entry)) {
1488 char * t = xmalloc(entry->length + length);
1489 memcpy(t, entry->data, entry->length);
1491 entry->info.offset = 0;
1493 entry->data = xrealloc(entry->data, entry->length + length);
1495 copyData(td->type, ((char *) entry->data) + entry->length,
1496 td->data, td->count, length);
1498 entry->length += length;
1500 entry->info.count += td->count;
1505 int headerPut(Header h, rpmtd td, headerPutFlags flags)
1510 if (flags & HEADERPUT_APPEND) {
1511 rc = findEntry(h, td->tag, td->type) ?
1512 intAppendEntry(h, td) :
1515 rc = intAddEntry(h, td);
1520 int headerAddI18NString(Header h, rpmTag tag, const char * string,
1523 indexEntry table, entry;
1524 const char ** strArray;
1527 rpm_count_t i, langNum;
1530 table = findEntry(h, HEADER_I18NTABLE, RPM_STRING_ARRAY_TYPE);
1531 entry = findEntry(h, tag, RPM_I18NSTRING_TYPE);
1533 if (!table && entry)
1534 return 0; /* this shouldn't ever happen!! */
1536 if (!table && !entry) {
1537 const char * charArray[2];
1538 rpm_count_t count = 0;
1540 if (!lang || (lang[0] == 'C' && lang[1] == '\0')) {
1541 charArray[count++] = "C";
1543 charArray[count++] = "C";
1544 charArray[count++] = lang;
1548 td.tag = HEADER_I18NTABLE;
1549 td.type = RPM_STRING_ARRAY_TYPE;
1550 td.data = (void *) charArray;
1552 if (!headerPut(h, &td, HEADERPUT_DEFAULT))
1554 table = findEntry(h, HEADER_I18NTABLE, RPM_STRING_ARRAY_TYPE);
1559 if (!lang) lang = "C";
1561 { const char * l = table->data;
1562 for (langNum = 0; langNum < table->info.count; langNum++) {
1563 if (rstreq(l, lang)) break;
1568 if (langNum >= table->info.count) {
1569 length = strlen(lang) + 1;
1570 if (ENTRY_IN_REGION(table)) {
1571 char * t = xmalloc(table->length + length);
1572 memcpy(t, table->data, table->length);
1574 table->info.offset = 0;
1576 table->data = xrealloc(table->data, table->length + length);
1577 memmove(((char *)table->data) + table->length, lang, length);
1578 table->length += length;
1579 table->info.count++;
1585 strArray = xmalloc(sizeof(*strArray) * (langNum + 1));
1586 for (i = 0; i < langNum; i++)
1588 strArray[langNum] = string;
1592 td.type = RPM_I18NSTRING_TYPE;
1594 td.count = langNum + 1;
1595 rc = headerPut(h, &td, HEADERPUT_DEFAULT);
1598 } else if (langNum >= entry->info.count) {
1599 ghosts = langNum - entry->info.count;
1601 length = strlen(string) + 1 + ghosts;
1602 if (ENTRY_IN_REGION(entry)) {
1603 char * t = xmalloc(entry->length + length);
1604 memcpy(t, entry->data, entry->length);
1606 entry->info.offset = 0;
1608 entry->data = xrealloc(entry->data, entry->length + length);
1610 memset(((char *)entry->data) + entry->length, '\0', ghosts);
1611 memmove(((char *)entry->data) + entry->length + ghosts, string, strlen(string)+1);
1613 entry->length += length;
1614 entry->info.count = langNum + 1;
1616 char *b, *be, *e, *ee, *t;
1619 /* Set beginning/end pointers to previous data */
1620 b = be = e = ee = entry->data;
1621 for (i = 0; i < table->info.count; i++) {
1624 ee += strlen(ee) + 1;
1629 /* Get storage for new buffer */
1631 sn = strlen(string) + 1;
1633 length = bn + sn + en;
1634 t = buf = xmalloc(length);
1636 /* Copy values into new storage */
1639 memcpy(t, string, sn);
1644 /* Replace i18N string array */
1645 entry->length -= strlen(be) + 1;
1646 entry->length += sn;
1648 if (ENTRY_IN_REGION(entry)) {
1649 entry->info.offset = 0;
1651 entry->data = _free(entry->data);
1658 int headerMod(Header h, rpmtd td)
1665 /* First find the tag */
1666 entry = findEntry(h, td->tag, td->type);
1671 data = grabData(td->type, td->data, td->count, &length);
1672 if (data == NULL || length <= 0)
1675 /* make sure entry points to the first occurence of this tag */
1676 while (entry > h->index && (entry - 1)->info.tag == td->tag)
1679 /* free after we've grabbed the new data in case the two are intertwined;
1680 that's a bad idea but at least we won't break */
1681 oldData = entry->data;
1683 entry->info.count = td->count;
1684 entry->info.type = td->type;
1686 entry->length = length;
1688 if (ENTRY_IN_REGION(entry)) {
1689 entry->info.offset = 0;
1691 oldData = _free(oldData);
1697 * Header tag iterator data structure.
1699 struct headerIterator_s {
1700 Header h; /*!< Header being iterated. */
1701 int next_index; /*!< Next tag index. */
1704 HeaderIterator headerFreeIterator(HeaderIterator hi)
1707 hi->h = headerFree(hi->h);
1713 HeaderIterator headerInitIterator(Header h)
1715 HeaderIterator hi = xmalloc(sizeof(*hi));
1719 hi->h = headerLink(h);
1724 static indexEntry nextIndex(HeaderIterator hi)
1728 indexEntry entry = NULL;
1730 for (slot = hi->next_index; slot < h->indexUsed; slot++) {
1731 entry = h->index + slot;
1732 if (!ENTRY_IS_REGION(entry))
1735 hi->next_index = slot;
1736 if (entry == NULL || slot >= h->indexUsed)
1743 rpmTag headerNextTag(HeaderIterator hi)
1745 indexEntry entry = nextIndex(hi);
1746 return entry ? entry->info.tag : RPMTAG_NOT_FOUND;
1749 int headerNext(HeaderIterator hi, rpmtd td)
1751 indexEntry entry = nextIndex(hi);
1756 td->tag = entry->info.tag;
1757 rc = copyTdEntry(entry, td, HEADERGET_DEFAULT);
1759 return ((rc == 1) ? 1 : 0);
1762 unsigned int headerGetInstance(Header h)
1764 return h ? h->instance : 0;
1767 void headerSetInstance(Header h, unsigned int instance)
1769 h->instance = instance;