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 */
69 typedef enum headerFlags_e {
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? */
77 * The Header data structure.
79 struct headerToken_s {
80 void * blob; /*!< Header region blob. */
81 indexEntry index; /*!< Array of tags. */
82 int indexUsed; /*!< Current size of tag array. */
83 int indexAlloced; /*!< Allocated size of tag array. */
84 unsigned int instance; /*!< Rpmdb instance (offset) */
86 int nrefs; /*!< Reference count. */
90 * Maximum no. of bytes permitted in a header.
92 static const size_t headerMaxbytes = (32*1024*1024);
94 #define INDEX_MALLOC_SIZE 8
96 #define ENTRY_IS_REGION(_e) \
97 (((_e)->info.tag >= HEADER_IMAGE) && ((_e)->info.tag < HEADER_REGIONS))
98 #define ENTRY_IN_REGION(_e) ((_e)->info.offset < 0)
101 * HEADER_EXT_TAG format function prototype.
102 * This is allowed to fail, which indicates the tag doesn't exist.
105 * @retval td tag data container
106 * @param flags modifier flags
107 * @return 0 on success
109 typedef int (*headerTagTagFunction) (Header h, rpmtd td, headerGetFlags hgflags);
111 extern void *rpmHeaderTagFunc(rpmTag tag);
113 /* Convert a 64bit value to network byte order. */
114 static uint64_t htonll( uint64_t n ) {
115 uint32_t *i = (uint32_t*)&n;
122 Header headerLink(Header h)
129 static Header headerUnlink(Header h)
136 Header headerFree(Header h)
138 (void) headerUnlink(h);
140 if (h == NULL || h->nrefs > 0)
141 return NULL; /* XXX return previous header? */
144 indexEntry entry = h->index;
146 for (i = 0; i < h->indexUsed; i++, entry++) {
147 if ((h->flags & HEADERFLAG_ALLOCATED) && ENTRY_IS_REGION(entry)) {
148 if (entry->length > 0) {
149 int32_t * ei = entry->data;
150 if ((ei - 2) == h->blob) h->blob = _free(h->blob);
153 } else if (!ENTRY_IN_REGION(entry)) {
154 entry->data = _free(entry->data);
158 h->index = _free(h->index);
165 static Header headerCreate(void *blob, int32_t indexLen)
167 Header h = xcalloc(1, sizeof(*h));
170 h->indexAlloced = indexLen + 1;
171 h->indexUsed = indexLen;
173 h->indexAlloced = INDEX_MALLOC_SIZE;
177 h->flags |= HEADERFLAG_SORTED;
179 h->index = (h->indexAlloced
180 ? xcalloc(h->indexAlloced, sizeof(*h->index))
184 return headerLink(h);
187 Header headerNew(void)
189 return headerCreate(NULL, 0);
192 int headerVerifyInfo(int il, int dl, const void * pev, void * iv, int negate)
194 entryInfo pe = (entryInfo) pev;
198 for (i = 0; i < il; i++) {
199 info->tag = ntohl(pe[i].tag);
200 info->type = ntohl(pe[i].type);
201 info->offset = ntohl(pe[i].offset);
203 info->offset = -info->offset;
204 info->count = ntohl(pe[i].count);
206 if (hdrchkType(info->type))
208 if (hdrchkAlign(info->type, info->offset))
210 if (!negate && hdrchkRange(dl, info->offset))
212 if (hdrchkData(info->count))
221 static int indexCmp(const void * avp, const void * bvp)
223 indexEntry ap = (indexEntry) avp, bp = (indexEntry) bvp;
224 return (ap->info.tag - bp->info.tag);
227 void headerSort(Header h)
229 if (!(h->flags & HEADERFLAG_SORTED)) {
230 qsort(h->index, h->indexUsed, sizeof(*h->index), indexCmp);
231 h->flags |= HEADERFLAG_SORTED;
237 static int offsetCmp(const void * avp, const void * bvp)
239 indexEntry ap = (indexEntry) avp, bp = (indexEntry) bvp;
240 int rc = (ap->info.offset - bp->info.offset);
243 /* Within a region, entries sort by address. Added drips sort by tag. */
244 if (ap->info.offset < 0)
245 rc = (((char *)ap->data) - ((char *)bp->data));
247 rc = (ap->info.tag - bp->info.tag);
253 * Restore tags in header to original ordering.
256 void headerUnsort(Header h)
258 qsort(h->index, h->indexUsed, sizeof(*h->index), offsetCmp);
261 unsigned headerSizeof(Header h, enum hMagic magicp)
264 unsigned int size = 0;
273 case HEADER_MAGIC_YES:
274 size += sizeof(rpm_header_magic);
276 case HEADER_MAGIC_NO:
280 size += 2 * sizeof(int32_t); /* count of index entries */
282 for (i = 0, entry = h->index; i < h->indexUsed; i++, entry++) {
285 /* Regions go in as is ... */
286 if (ENTRY_IS_REGION(entry)) {
287 size += entry->length;
288 /* XXX Legacy regions do not include the region tag and data. */
289 if (i == 0 && (h->flags & HEADERFLAG_LEGACY))
290 size += sizeof(struct entryInfo_s) + entry->info.count;
294 /* ... and region elements are skipped. */
295 if (entry->info.offset < 0)
299 type = entry->info.type;
300 if (typeSizes[type] > 1) {
301 unsigned diff = typeSizes[type] - (size % typeSizes[type]);
302 if (diff != typeSizes[type]) {
307 size += sizeof(struct entryInfo_s) + entry->length;
314 * Return length of entry data.
315 * @param type entry data type
316 * @param p entry data
317 * @param count entry item count
318 * @param onDisk data is concatenated strings (with NUL's))?
319 * @param pend pointer to end of data (or NULL)
320 * @return no. bytes in data, -1 on failure
322 static int dataLength(rpmTagType type, rpm_constdata_t p, rpm_count_t count,
323 int onDisk, rpm_constdata_t pend)
325 const unsigned char * s = p;
326 const unsigned char * se = pend;
330 case RPM_STRING_TYPE:
338 length++; /* count nul terminator too. */
341 case RPM_STRING_ARRAY_TYPE:
342 case RPM_I18NSTRING_TYPE:
343 /* These are like RPM_STRING_TYPE, except they're *always* an array */
344 /* Compute sum of length of all strings, including nul terminators */
348 length++; /* count nul terminator too */
356 const char ** av = (const char **)p;
358 /* add one for null termination */
359 length += strlen(*av++) + 1;
365 if (typeSizes[type] == -1)
367 length = typeSizes[(type & 0xf)] * count;
368 if (length < 0 || (se && (s + length) > se))
377 * Swap int32_t and int16_t arrays within header region.
379 * If a header region tag is in the set to be swabbed, as the data for a
380 * a header region is located after all other tag data.
382 * @param entry header entry
383 * @param il no. of entries
384 * @param dl start no. bytes of data
385 * @param pe header physical entry pointer (swapped)
386 * @param dataStart header data start
387 * @param dataEnd header data end
388 * @param regionid region offset
389 * @return no. bytes of data in region, -1 on error
391 static int regionSwab(indexEntry entry, int il, int dl,
393 unsigned char * dataStart,
394 const unsigned char * dataEnd,
397 for (; il > 0; il--, pe++) {
398 struct indexEntry_s ie;
401 ie.info.tag = ntohl(pe->tag);
402 ie.info.type = ntohl(pe->type);
403 ie.info.count = ntohl(pe->count);
404 ie.info.offset = ntohl(pe->offset);
406 if (hdrchkType(ie.info.type))
408 if (hdrchkData(ie.info.count))
410 if (hdrchkData(ie.info.offset))
412 if (hdrchkAlign(ie.info.type, ie.info.offset))
415 ie.data = dataStart + ie.info.offset;
416 if (dataEnd && (unsigned char *)ie.data >= dataEnd)
419 ie.length = dataLength(ie.info.type, ie.data, ie.info.count, 1, dataEnd);
420 if (ie.length < 0 || hdrchkData(ie.length))
426 ie.info.offset = regionid;
427 *entry = ie; /* structure assignment */
433 if (typeSizes[type] > 1) {
434 unsigned diff = typeSizes[type] - (dl % typeSizes[type]);
435 if (diff != typeSizes[type]) {
440 /* Perform endian conversions */
441 switch (ntohl(pe->type)) {
443 { uint64_t * it = ie.data;
444 for (; ie.info.count > 0; ie.info.count--, it += 1) {
445 if (dataEnd && ((unsigned char *)it) >= dataEnd)
451 { int32_t * it = ie.data;
452 for (; ie.info.count > 0; ie.info.count--, it += 1) {
453 if (dataEnd && ((unsigned char *)it) >= dataEnd)
459 { int16_t * it = ie.data;
460 for (; ie.info.count > 0; ie.info.count--, it += 1) {
461 if (dataEnd && ((unsigned char *)it) >= dataEnd)
477 * @retval *lengthPtr no. bytes in unloaded header blob
478 * @return unloaded header blob (NULL on error)
480 static void * doHeaderUnload(Header h,
493 int drlen, ndribbles;
495 if (h == NULL) return NULL;
497 /* Sort entries by (offset,tag). */
500 /* Compute (il,dl) for all tags, including those deleted in region. */
501 drlen = ndribbles = 0;
502 for (i = 0, entry = h->index; i < h->indexUsed; i++, entry++) {
503 if (ENTRY_IS_REGION(entry)) {
504 int32_t rdl = -entry->info.offset; /* negative offset */
505 int32_t ril = rdl/sizeof(*pe);
506 int rid = entry->info.offset;
509 dl += entry->rdlen + entry->info.count;
510 /* XXX Legacy regions do not include the region tag and data. */
511 if (i == 0 && (h->flags & HEADERFLAG_LEGACY))
514 /* Skip rest of entries in region, but account for dribbles. */
515 for (; i < h->indexUsed && entry->info.offset <= rid+1; i++, entry++) {
516 if (entry->info.offset <= rid)
520 type = entry->info.type;
521 if (typeSizes[type] > 1) {
522 unsigned diff = typeSizes[type] - (dl % typeSizes[type]);
523 if (diff != typeSizes[type]) {
531 drlen += entry->length;
539 /* Ignore deleted drips. */
540 if (entry->data == NULL || entry->length <= 0)
544 type = entry->info.type;
545 if (typeSizes[type] > 1) {
546 unsigned diff = typeSizes[type] - (dl % typeSizes[type]);
547 if (diff != typeSizes[type]) {
556 /* Sanity checks on header intro. */
557 if (hdrchkTags(il) || hdrchkData(dl))
560 len = sizeof(il) + sizeof(dl) + (il * sizeof(*pe)) + dl;
566 pe = (entryInfo) &ei[2];
567 dataStart = te = (char *) (pe + il);
569 for (i = 0, entry = h->index; i < h->indexUsed; i++, entry++) {
575 if (entry->data == NULL || entry->length <= 0)
578 t = (unsigned char*)te;
579 pe->tag = htonl(entry->info.tag);
580 pe->type = htonl(entry->info.type);
581 pe->count = htonl(entry->info.count);
583 if (ENTRY_IS_REGION(entry)) {
584 int32_t rdl = -entry->info.offset; /* negative offset */
585 int32_t ril = rdl/sizeof(*pe) + ndribbles;
586 int rid = entry->info.offset;
588 src = (char *)entry->data;
589 rdlen = entry->rdlen;
591 /* XXX Legacy regions do not include the region tag and data. */
592 if (i == 0 && (h->flags & HEADERFLAG_LEGACY)) {
595 memcpy(pe+1, src, rdl);
596 memcpy(te, src + rdl, rdlen);
599 pe->offset = htonl(te - dataStart);
602 stei[2] = htonl(-rdl-entry->info.count);
604 memcpy(te, stei, entry->info.count);
605 te += entry->info.count;
607 rdlen += entry->info.count;
609 count = regionSwab(NULL, ril, 0, pe, t, NULL, 0);
615 memcpy(pe+1, src + sizeof(*pe), ((ril-1) * sizeof(*pe)));
616 memcpy(te, src + (ril * sizeof(*pe)), rdlen+entry->info.count+drlen);
619 entryInfo se = (entryInfo)src;
620 int off = ntohl(se->offset);
621 pe->offset = (off) ? htonl(te - dataStart) : htonl(off);
623 te += entry->info.count + drlen;
625 count = regionSwab(NULL, ril, 0, pe, t, NULL, 0);
626 if (count != (rdlen + entry->info.count + drlen))
630 /* Skip rest of entries in region. */
631 while (i < h->indexUsed && entry->info.offset <= rid+1) {
641 /* Ignore deleted drips. */
642 if (entry->data == NULL || entry->length <= 0)
646 type = entry->info.type;
647 if (typeSizes[type] > 1) {
649 diff = typeSizes[type] - ((te - dataStart) % typeSizes[type]);
650 if (diff != typeSizes[type]) {
656 pe->offset = htonl(te - dataStart);
658 /* copy data w/ endian conversions */
659 switch (entry->info.type) {
661 count = entry->info.count;
664 *((uint64_t *)te) = htonll(*((uint64_t *)src));
665 te += sizeof(uint64_t);
666 src += sizeof(uint64_t);
671 count = entry->info.count;
674 *((int32_t *)te) = htonl(*((int32_t *)src));
675 te += sizeof(int32_t);
676 src += sizeof(int32_t);
681 count = entry->info.count;
684 *((int16_t *)te) = htons(*((int16_t *)src));
685 te += sizeof(int16_t);
686 src += sizeof(int16_t);
691 memcpy(te, entry->data, entry->length);
698 /* Insure that there are no memcpy underruns/overruns. */
699 if (((char *)pe) != dataStart)
701 if ((((char *)ei)+len) != te)
707 h->flags &= ~HEADERFLAG_SORTED;
717 void * headerUnload(Header h)
720 void * uh = doHeaderUnload(h, &length);
725 * Find matching (tag,type) entry in header.
727 * @param tag entry tag
728 * @param type entry type
729 * @return header entry
732 indexEntry findEntry(Header h, rpmTag tag, rpmTagType type)
735 struct indexEntry_s key;
737 if (h == NULL) return NULL;
738 if (!(h->flags & HEADERFLAG_SORTED)) headerSort(h);
742 entry = bsearch(&key, h->index, h->indexUsed, sizeof(*h->index), indexCmp);
746 if (type == RPM_NULL_TYPE)
750 while (entry->info.tag == tag && entry->info.type != type &&
751 entry > h->index) entry--;
753 if (entry->info.tag == tag && entry->info.type == type)
759 int headerDel(Header h, rpmTag tag)
761 indexEntry last = h->index + h->indexUsed;
762 indexEntry entry, first;
765 entry = findEntry(h, tag, RPM_NULL_TYPE);
766 if (!entry) return 1;
768 /* Make sure entry points to the first occurence of this tag. */
769 while (entry > h->index && (entry - 1)->info.tag == tag)
772 /* Free data for tags being removed. */
773 for (first = entry; first < last; first++) {
775 if (first->info.tag != tag)
780 if (ENTRY_IN_REGION(first))
785 ne = (first - entry);
790 memmove(entry, first, (ne * sizeof(*entry)));
796 Header headerLoad(void * uh)
798 int32_t * ei = (int32_t *) uh;
799 int32_t il = ntohl(ei[0]); /* index length */
800 int32_t dl = ntohl(ei[1]); /* data length */
801 size_t pvlen = sizeof(il) + sizeof(dl) +
802 (il * sizeof(struct entryInfo_s)) + dl;
806 unsigned char * dataStart;
807 unsigned char * dataEnd;
811 /* Sanity checks on header intro. */
812 if (hdrchkTags(il) || hdrchkData(dl))
816 pe = (entryInfo) &ei[2];
817 dataStart = (unsigned char *) (pe + il);
818 dataEnd = dataStart + dl;
820 h = headerCreate(uh, il);
823 if (!(htonl(pe->tag) < HEADER_I18NTABLE)) {
824 h->flags |= HEADERFLAG_LEGACY;
825 entry->info.type = REGION_TAG_TYPE;
826 entry->info.tag = HEADER_IMAGE;
827 entry->info.count = REGION_TAG_COUNT;
828 entry->info.offset = ((unsigned char *)pe - dataStart); /* negative offset */
831 entry->length = pvlen - sizeof(il) - sizeof(dl);
832 rdlen = regionSwab(entry+1, il, 0, pe, dataStart, dataEnd, entry->info.offset);
835 entry->rdlen = rdlen;
841 h->flags &= ~HEADERFLAG_LEGACY;
843 entry->info.type = htonl(pe->type);
844 entry->info.count = htonl(pe->count);
846 if (hdrchkType(entry->info.type))
848 if (hdrchkTags(entry->info.count))
851 { int off = ntohl(pe->offset);
856 size_t nb = REGION_TAG_COUNT;
858 /* XXX Hmm, why the copy? */
859 memcpy(&stei, dataStart + off, nb);
860 rdl = -ntohl(stei[2]); /* negative offset */
861 ril = rdl/sizeof(*pe);
862 if (hdrchkTags(ril) || hdrchkData(rdl))
864 entry->info.tag = htonl(pe->tag);
867 rdl = (ril * sizeof(struct entryInfo_s));
868 entry->info.tag = HEADER_IMAGE;
871 entry->info.offset = -rdl; /* negative offset */
874 entry->length = pvlen - sizeof(il) - sizeof(dl);
875 rdlen = regionSwab(entry+1, ril-1, 0, pe+1, dataStart, dataEnd, entry->info.offset);
878 entry->rdlen = rdlen;
880 if (ril < h->indexUsed) {
881 indexEntry newEntry = entry + ril;
882 int ne = (h->indexUsed - ril);
883 int rid = entry->info.offset+1;
886 /* Load dribble entries from region. */
887 rc = regionSwab(newEntry, ne, 0, pe+ril, dataStart, dataEnd, rid);
892 { indexEntry firstEntry = newEntry;
893 int save = h->indexUsed;
896 /* Dribble entries replace duplicate region entries. */
898 for (j = 0; j < ne; j++, newEntry++) {
899 (void) headerDel(h, newEntry->info.tag);
900 if (newEntry->info.tag == RPMTAG_BASENAMES)
901 (void) headerDel(h, RPMTAG_OLDFILENAMES);
904 /* If any duplicate entries were replaced, move new entries down. */
905 if (h->indexUsed < (save - ne)) {
906 memmove(h->index + h->indexUsed, firstEntry,
907 (ne * sizeof(*entry)));
914 h->flags &= ~HEADERFLAG_SORTED;
916 h->flags |= HEADERFLAG_ALLOCATED;
922 h->index = _free(h->index);
928 Header headerReload(Header h, rpmTag tag)
932 void * uh = doHeaderUnload(h, &length);
942 if (ENTRY_IS_REGION(nh->index)) {
943 if (tag == HEADER_SIGNATURES || tag == HEADER_IMMUTABLE)
944 nh->index[0].info.tag = tag;
949 Header headerCopyLoad(const void * uh)
951 int32_t * ei = (int32_t *) uh;
952 int32_t il = ntohl(ei[0]); /* index length */
953 int32_t dl = ntohl(ei[1]); /* data length */
954 size_t pvlen = sizeof(il) + sizeof(dl) +
955 (il * sizeof(struct entryInfo_s)) + dl;
959 /* Sanity checks on header intro. */
960 if (!(hdrchkTags(il) || hdrchkData(dl)) && pvlen < headerMaxbytes) {
961 nuh = memcpy(xmalloc(pvlen), uh, pvlen);
962 if ((h = headerLoad(nuh)) == NULL)
969 * Read (and load) header from file handle.
970 * @param fd file handle
971 * @param magicp read (and verify) 8 bytes of (magic, 0)?
972 * @return header (or NULL on error)
974 Header headerRead(FD_t fd, enum hMagic magicp)
986 memset(block, 0, sizeof(block));
988 if (magicp == HEADER_MAGIC_YES)
992 if (timedRead(fd, (char *)block, i*sizeof(*block)) != (i * sizeof(*block)))
997 if (magicp == HEADER_MAGIC_YES) {
999 if (memcmp(&magic, rpm_header_magic, sizeof(magic)))
1001 reserved = block[i++];
1004 il = ntohl(block[i]); i++;
1005 dl = ntohl(block[i]); i++;
1007 len = sizeof(il) + sizeof(dl) + (il * sizeof(struct entryInfo_s)) + dl;
1009 /* Sanity checks on header intro. */
1010 if (hdrchkTags(il) || hdrchkData(dl) || len > headerMaxbytes)
1016 len -= sizeof(il) + sizeof(dl);
1019 if (timedRead(fd, (char *)&ei[2], len) != len)
1025 if (h == NULL && ei != NULL) {
1031 int headerWrite(FD_t fd, Header h, enum hMagic magicp)
1037 uh = doHeaderUnload(h, &length);
1041 case HEADER_MAGIC_YES:
1042 nb = Fwrite(rpm_header_magic, sizeof(uint8_t), sizeof(rpm_header_magic), fd);
1043 if (nb != sizeof(rpm_header_magic))
1046 case HEADER_MAGIC_NO:
1050 nb = Fwrite(uh, sizeof(char), length, fd);
1054 return (nb == length ? 0 : 1);
1057 int headerIsEntry(Header h, rpmTag tag)
1059 /* FIX: h modified by sort. */
1060 return (findEntry(h, tag, RPM_NULL_TYPE) ? 1 : 0);
1065 * Retrieve data from header entry.
1066 * Relevant flags (others are ignored), if neither is set allocation
1067 * behavior depends on data type(!)
1068 * HEADERGET_MINMEM: return pointers to header memory
1069 * HEADERGET_ALLOC: always return malloced memory, overrides MINMEM
1071 * @todo Permit retrieval of regions other than HEADER_IMUTABLE.
1072 * @param entry header entry
1073 * @param td tag data container
1074 * @param minMem string pointers refer to header memory?
1075 * @param flags flags to control memory allocation
1076 * @return 1 on success, otherwise error.
1078 static int copyTdEntry(const indexEntry entry, rpmtd td, headerGetFlags flags)
1080 rpm_count_t count = entry->info.count;
1081 int rc = 1; /* XXX 1 on success. */
1082 /* ALLOC overrides MINMEM */
1083 int allocMem = flags & HEADERGET_ALLOC;
1084 int minMem = allocMem ? 0 : flags & HEADERGET_MINMEM;
1085 int argvArray = (flags & HEADERGET_ARGV) ? 1 : 0;
1088 td->flags = RPMTD_IMMUTABLE;
1089 switch (entry->info.type) {
1092 * XXX This only works for
1093 * XXX "sealed" HEADER_IMMUTABLE/HEADER_SIGNATURES/HEADER_IMAGE.
1094 * XXX This will *not* work for unsealed legacy HEADER_IMAGE (i.e.
1095 * XXX a legacy header freshly read, but not yet unloaded to the rpmdb).
1097 if (ENTRY_IS_REGION(entry)) {
1098 int32_t * ei = ((int32_t *)entry->data) - 2;
1099 entryInfo pe = (entryInfo) (ei + 2);
1100 unsigned char * dataStart = (unsigned char *) (pe + ntohl(ei[0]));
1101 int32_t rdl = -entry->info.offset; /* negative offset */
1102 int32_t ril = rdl/sizeof(*pe);
1105 count = 2 * sizeof(*ei) + (ril * sizeof(*pe)) + rdl;
1106 if (entry->info.tag == HEADER_IMAGE) {
1110 count += REGION_TAG_COUNT;
1111 rdl += REGION_TAG_COUNT;
1114 td->data = xmalloc(count);
1115 ei = (int32_t *) td->data;
1119 pe = (entryInfo) memcpy(ei + 2, pe, (ril * sizeof(*pe)));
1121 dataStart = (unsigned char *) memcpy(pe + ril, dataStart, rdl);
1123 rc = regionSwab(NULL, ril, 0, pe, dataStart, dataStart + rdl, 0);
1124 /* don't return data on failure */
1126 td->data = _free(td->data);
1128 /* XXX 1 on success. */
1129 rc = (rc < 0) ? 0 : 1;
1131 count = entry->length;
1133 ? memcpy(xmalloc(count), entry->data, count)
1137 case RPM_STRING_TYPE:
1138 /* simple string, but fallthrough if its actually an array */
1139 if (count == 1 && !argvArray) {
1140 td->data = allocMem ? xstrdup(entry->data) : entry->data;
1143 case RPM_STRING_ARRAY_TYPE:
1144 case RPM_I18NSTRING_TYPE:
1145 { const char ** ptrEntry;
1146 int tableSize = (count + argvArray) * sizeof(char *);
1151 td->data = xmalloc(tableSize);
1152 ptrEntry = (const char **) td->data;
1155 t = xmalloc(tableSize + entry->length);
1156 td->data = (void *)t;
1157 ptrEntry = (const char **) td->data;
1159 memcpy(t, entry->data, entry->length);
1161 for (i = 0; i < count; i++) {
1168 td->flags |= RPMTD_ARGV;
1173 case RPM_INT16_TYPE:
1174 case RPM_INT32_TYPE:
1175 case RPM_INT64_TYPE:
1177 td->data = xmalloc(entry->length);
1178 memcpy(td->data, entry->data, entry->length);
1180 td->data = entry->data;
1184 /* WTH? Don't mess with unknown data types... */
1189 td->type = entry->info.type;
1192 if (td->data && entry->data != td->data) {
1193 td->flags |= RPMTD_ALLOCED;
1200 * Does locale match entry in header i18n table?
1203 * The range [l,le) contains the next locale to match:
1204 * ll[_CC][.EEEEE][@dddd]
1206 * ll ISO language code (in lowercase).
1207 * CC (optional) ISO coutnry code (in uppercase).
1208 * EEEEE (optional) encoding (not really standardized).
1209 * dddd (optional) dialect.
1212 * @param td header i18n table data, NUL terminated
1213 * @param l start of locale to match
1214 * @param le end of locale to match
1215 * @return 1 on good match, 2 on weak match, 0 on no match
1217 static int headerMatchLocale(const char *td, const char *l, const char *le)
1221 /* First try a complete match. */
1222 if (strlen(td) == (le-l) && rstreqn(td, l, (le - l)))
1225 /* Next, try stripping optional dialect and matching. */
1226 for (fe = l; fe < le && *fe != '@'; fe++)
1228 if (fe < le && rstreqn(td, l, (fe - l)))
1231 /* Next, try stripping optional codeset and matching. */
1232 for (fe = l; fe < le && *fe != '.'; fe++)
1234 if (fe < le && rstreqn(td, l, (fe - l)))
1237 /* Finally, try stripping optional country code and matching. */
1238 for (fe = l; fe < le && *fe != '_'; fe++)
1240 if (fe < le && rstreqn(td, l, (fe - l)))
1247 * Return i18n string from header that matches locale.
1249 * @param entry i18n string data
1250 * @retval td tag data container
1251 * @param flags flags to control allocation
1254 static int copyI18NEntry(Header h, indexEntry entry, rpmtd td,
1255 headerGetFlags flags)
1257 const char *lang, *l, *le;
1260 td->type = RPM_STRING_TYPE;
1262 /* if no match, just return the first string */
1263 td->data = entry->data;
1265 /* XXX Drepper sez' this is the order. */
1266 if ((lang = getenv("LANGUAGE")) == NULL &&
1267 (lang = getenv("LC_ALL")) == NULL &&
1268 (lang = getenv("LC_MESSAGES")) == NULL &&
1269 (lang = getenv("LANG")) == NULL)
1272 if ((table = findEntry(h, HEADER_I18NTABLE, RPM_STRING_ARRAY_TYPE)) == NULL)
1275 for (l = lang; *l != '\0'; l = le) {
1277 char *ed, *ed_weak = NULL;
1280 while (*l && *l == ':') /* skip leading colons */
1284 for (le = l; *le && *le != ':'; le++) /* find end of this locale */
1287 /* For each entry in the header ... */
1288 for (langNum = 0, t = table->data, ed = entry->data;
1289 langNum < entry->info.count;
1290 langNum++, t += strlen(t) + 1, ed += strlen(ed) + 1) {
1292 int match = headerMatchLocale(t, l, le);
1296 } else if (match == 2) {
1307 if (flags & HEADERGET_ALLOC) {
1308 td->data = xstrdup(td->data);
1309 td->flags |= RPMTD_ALLOCED;
1316 * Retrieve tag data from header.
1318 * @retval td tag data container
1319 * @param flags flags to control retrieval
1320 * @return 1 on success, 0 on not found
1322 static int intGetTdEntry(Header h, rpmtd td, headerGetFlags flags)
1327 /* First find the tag */
1328 /* FIX: h modified by sort. */
1329 entry = findEntry(h, td->tag, RPM_NULL_TYPE);
1330 if (entry == NULL) {
1331 /* Td is zeroed above, just return... */
1335 if (flags & HEADERGET_RAW) {
1336 rc = copyTdEntry(entry, td, flags);
1338 switch (entry->info.type) {
1339 case RPM_I18NSTRING_TYPE:
1340 rc = copyI18NEntry(h, entry, td, flags);
1343 rc = copyTdEntry(entry, td, flags);
1348 /* XXX 1 on success */
1349 return ((rc == 1) ? 1 : 0);
1352 int headerGet(Header h, rpmTag tag, rpmtd td, headerGetFlags flags)
1355 headerTagTagFunction tagfunc = intGetTdEntry;
1357 if (td == NULL) return 0;
1362 if (flags & HEADERGET_EXT) {
1363 headerTagTagFunction extfunc = rpmHeaderTagFunc(tag);
1364 if (extfunc) tagfunc = extfunc;
1366 rc = tagfunc(h, td, flags);
1368 assert(tag == td->tag);
1374 static void copyData(rpmTagType type, rpm_data_t dstPtr,
1375 rpm_constdata_t srcPtr, rpm_count_t cnt, int dataLength)
1378 case RPM_STRING_ARRAY_TYPE:
1379 case RPM_I18NSTRING_TYPE:
1380 { const char ** av = (const char **) srcPtr;
1383 while (cnt-- > 0 && dataLength > 0) {
1385 if ((s = *av++) == NULL)
1389 } while (s[-1] && --dataLength > 0);
1394 memmove(dstPtr, srcPtr, dataLength);
1400 * Return (malloc'ed) copy of entry data.
1401 * @param type entry data type
1402 * @param p entry data
1403 * @param c entry item count
1404 * @retval lengthPtr no. bytes in returned data
1405 * @return (malloc'ed) copy of entry data, NULL on error
1408 grabData(rpmTagType type, rpm_constdata_t p, rpm_count_t c, int * lengthPtr)
1410 rpm_data_t data = NULL;
1413 length = dataLength(type, p, c, 0, NULL);
1415 data = xmalloc(length);
1416 copyData(type, data, p, c, length);
1420 *lengthPtr = length;
1424 static int intAddEntry(Header h, rpmtd td)
1430 /* Count must always be >= 1 for headerAddEntry. */
1434 if (hdrchkType(td->type))
1436 if (hdrchkData(td->count))
1440 data = grabData(td->type, td->data, td->count, &length);
1441 if (data == NULL || length <= 0)
1444 /* Allocate more index space if necessary */
1445 if (h->indexUsed == h->indexAlloced) {
1446 h->indexAlloced += INDEX_MALLOC_SIZE;
1447 h->index = xrealloc(h->index, h->indexAlloced * sizeof(*h->index));
1450 /* Fill in the index */
1451 entry = h->index + h->indexUsed;
1452 entry->info.tag = td->tag;
1453 entry->info.type = td->type;
1454 entry->info.count = td->count;
1455 entry->info.offset = 0;
1457 entry->length = length;
1459 if (h->indexUsed > 0 && td->tag < h->index[h->indexUsed-1].info.tag)
1460 h->flags &= ~HEADERFLAG_SORTED;
1466 static int intAppendEntry(Header h, rpmtd td)
1471 if (td->type == RPM_STRING_TYPE || td->type == RPM_I18NSTRING_TYPE) {
1472 /* we can't do this */
1476 /* Find the tag entry in the header. */
1477 entry = findEntry(h, td->tag, td->type);
1481 length = dataLength(td->type, td->data, td->count, 0, NULL);
1485 if (ENTRY_IN_REGION(entry)) {
1486 char * t = xmalloc(entry->length + length);
1487 memcpy(t, entry->data, entry->length);
1489 entry->info.offset = 0;
1491 entry->data = xrealloc(entry->data, entry->length + length);
1493 copyData(td->type, ((char *) entry->data) + entry->length,
1494 td->data, td->count, length);
1496 entry->length += length;
1498 entry->info.count += td->count;
1503 int headerPut(Header h, rpmtd td, headerPutFlags flags)
1508 if (flags & HEADERPUT_APPEND) {
1509 rc = findEntry(h, td->tag, td->type) ?
1510 intAppendEntry(h, td) :
1513 rc = intAddEntry(h, td);
1518 int headerAddI18NString(Header h, rpmTag tag, const char * string,
1521 indexEntry table, entry;
1522 const char ** strArray;
1525 rpm_count_t i, langNum;
1528 table = findEntry(h, HEADER_I18NTABLE, RPM_STRING_ARRAY_TYPE);
1529 entry = findEntry(h, tag, RPM_I18NSTRING_TYPE);
1531 if (!table && entry)
1532 return 0; /* this shouldn't ever happen!! */
1534 if (!table && !entry) {
1535 const char * charArray[2];
1536 rpm_count_t count = 0;
1538 if (!lang || (lang[0] == 'C' && lang[1] == '\0')) {
1539 charArray[count++] = "C";
1541 charArray[count++] = "C";
1542 charArray[count++] = lang;
1546 td.tag = HEADER_I18NTABLE;
1547 td.type = RPM_STRING_ARRAY_TYPE;
1548 td.data = (void *) charArray;
1550 if (!headerPut(h, &td, HEADERPUT_DEFAULT))
1552 table = findEntry(h, HEADER_I18NTABLE, RPM_STRING_ARRAY_TYPE);
1557 if (!lang) lang = "C";
1559 { const char * l = table->data;
1560 for (langNum = 0; langNum < table->info.count; langNum++) {
1561 if (rstreq(l, lang)) break;
1566 if (langNum >= table->info.count) {
1567 length = strlen(lang) + 1;
1568 if (ENTRY_IN_REGION(table)) {
1569 char * t = xmalloc(table->length + length);
1570 memcpy(t, table->data, table->length);
1572 table->info.offset = 0;
1574 table->data = xrealloc(table->data, table->length + length);
1575 memmove(((char *)table->data) + table->length, lang, length);
1576 table->length += length;
1577 table->info.count++;
1583 strArray = xmalloc(sizeof(*strArray) * (langNum + 1));
1584 for (i = 0; i < langNum; i++)
1586 strArray[langNum] = string;
1590 td.type = RPM_I18NSTRING_TYPE;
1592 td.count = langNum + 1;
1593 rc = headerPut(h, &td, HEADERPUT_DEFAULT);
1596 } else if (langNum >= entry->info.count) {
1597 ghosts = langNum - entry->info.count;
1599 length = strlen(string) + 1 + ghosts;
1600 if (ENTRY_IN_REGION(entry)) {
1601 char * t = xmalloc(entry->length + length);
1602 memcpy(t, entry->data, entry->length);
1604 entry->info.offset = 0;
1606 entry->data = xrealloc(entry->data, entry->length + length);
1608 memset(((char *)entry->data) + entry->length, '\0', ghosts);
1609 memmove(((char *)entry->data) + entry->length + ghosts, string, strlen(string)+1);
1611 entry->length += length;
1612 entry->info.count = langNum + 1;
1614 char *b, *be, *e, *ee, *t;
1617 /* Set beginning/end pointers to previous data */
1618 b = be = e = ee = entry->data;
1619 for (i = 0; i < table->info.count; i++) {
1622 ee += strlen(ee) + 1;
1627 /* Get storage for new buffer */
1629 sn = strlen(string) + 1;
1631 length = bn + sn + en;
1632 t = buf = xmalloc(length);
1634 /* Copy values into new storage */
1637 memcpy(t, string, sn);
1642 /* Replace i18N string array */
1643 entry->length -= strlen(be) + 1;
1644 entry->length += sn;
1646 if (ENTRY_IN_REGION(entry)) {
1647 entry->info.offset = 0;
1649 entry->data = _free(entry->data);
1656 int headerMod(Header h, rpmtd td)
1663 /* First find the tag */
1664 entry = findEntry(h, td->tag, td->type);
1669 data = grabData(td->type, td->data, td->count, &length);
1670 if (data == NULL || length <= 0)
1673 /* make sure entry points to the first occurence of this tag */
1674 while (entry > h->index && (entry - 1)->info.tag == td->tag)
1677 /* free after we've grabbed the new data in case the two are intertwined;
1678 that's a bad idea but at least we won't break */
1679 oldData = entry->data;
1681 entry->info.count = td->count;
1682 entry->info.type = td->type;
1684 entry->length = length;
1686 if (ENTRY_IN_REGION(entry)) {
1687 entry->info.offset = 0;
1689 oldData = _free(oldData);
1695 * Header tag iterator data structure.
1697 struct headerIterator_s {
1698 Header h; /*!< Header being iterated. */
1699 int next_index; /*!< Next tag index. */
1702 HeaderIterator headerFreeIterator(HeaderIterator hi)
1705 hi->h = headerFree(hi->h);
1711 HeaderIterator headerInitIterator(Header h)
1713 HeaderIterator hi = xmalloc(sizeof(*hi));
1717 hi->h = headerLink(h);
1722 static indexEntry nextIndex(HeaderIterator hi)
1726 indexEntry entry = NULL;
1728 for (slot = hi->next_index; slot < h->indexUsed; slot++) {
1729 entry = h->index + slot;
1730 if (!ENTRY_IS_REGION(entry))
1733 hi->next_index = slot;
1734 if (entry == NULL || slot >= h->indexUsed)
1741 rpmTag headerNextTag(HeaderIterator hi)
1743 indexEntry entry = nextIndex(hi);
1744 return entry ? entry->info.tag : RPMTAG_NOT_FOUND;
1747 int headerNext(HeaderIterator hi, rpmtd td)
1749 indexEntry entry = nextIndex(hi);
1754 td->tag = entry->info.tag;
1755 rc = copyTdEntry(entry, td, HEADERGET_DEFAULT);
1757 return ((rc == 1) ? 1 : 0);
1760 unsigned int headerGetInstance(Header h)
1762 return h ? h->instance : 0;
1765 void headerSetInstance(Header h, unsigned int instance)
1767 h->instance = instance;