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"
16 #include "lib/misc.h" /* tag function proto */
22 const unsigned char rpm_header_magic[8] = {
23 0x8e, 0xad, 0xe8, 0x01, 0x00, 0x00, 0x00, 0x00
27 * Alignment needed for header data types.
29 static const int typeAlign[16] = {
30 1, /*!< RPM_NULL_TYPE */
31 1, /*!< RPM_CHAR_TYPE */
32 1, /*!< RPM_INT8_TYPE */
33 2, /*!< RPM_INT16_TYPE */
34 4, /*!< RPM_INT32_TYPE */
35 8, /*!< RPM_INT64_TYPE */
36 1, /*!< RPM_STRING_TYPE */
37 1, /*!< RPM_BIN_TYPE */
38 1, /*!< RPM_STRING_ARRAY_TYPE */
39 1, /*!< RPM_I18NSTRING_TYPE */
49 * Size of header data types.
51 static const int typeSizes[16] = {
52 0, /*!< RPM_NULL_TYPE */
53 1, /*!< RPM_CHAR_TYPE */
54 1, /*!< RPM_INT8_TYPE */
55 2, /*!< RPM_INT16_TYPE */
56 4, /*!< RPM_INT32_TYPE */
57 8, /*!< RPM_INT64_TYPE */
58 -1, /*!< RPM_STRING_TYPE */
59 1, /*!< RPM_BIN_TYPE */
60 -1, /*!< RPM_STRING_ARRAY_TYPE */
61 -1, /*!< RPM_I18NSTRING_TYPE */
71 HEADERFLAG_SORTED = (1 << 0), /*!< Are header entries sorted? */
72 HEADERFLAG_ALLOCATED = (1 << 1), /*!< Is 1st header region allocated? */
73 HEADERFLAG_LEGACY = (1 << 2), /*!< Header came from legacy source? */
74 HEADERFLAG_DEBUG = (1 << 3), /*!< Debug this header? */
77 typedef rpmFlags headerFlags;
80 * The Header data structure.
82 struct headerToken_s {
83 void * blob; /*!< Header region blob. */
84 indexEntry index; /*!< Array of tags. */
85 int indexUsed; /*!< Current size of tag array. */
86 int indexAlloced; /*!< Allocated size of tag array. */
87 unsigned int instance; /*!< Rpmdb instance (offset) */
89 int nrefs; /*!< Reference count. */
93 * Maximum no. of bytes permitted in a header.
95 static const size_t headerMaxbytes = (32*1024*1024);
97 #define INDEX_MALLOC_SIZE 8
99 #define ENTRY_IS_REGION(_e) \
100 (((_e)->info.tag >= RPMTAG_HEADERIMAGE) && ((_e)->info.tag < RPMTAG_HEADERREGIONS))
101 #define ENTRY_IN_REGION(_e) ((_e)->info.offset < 0)
103 /* Convert a 64bit value to network byte order. */
105 static uint64_t htonll(uint64_t n)
107 uint32_t *i = (uint32_t*)&n;
114 Header headerLink(Header h)
121 static Header headerUnlink(Header h)
128 Header headerFree(Header h)
130 (void) headerUnlink(h);
132 if (h == NULL || h->nrefs > 0)
136 indexEntry entry = h->index;
138 for (i = 0; i < h->indexUsed; i++, entry++) {
139 if ((h->flags & HEADERFLAG_ALLOCATED) && ENTRY_IS_REGION(entry)) {
140 if (entry->length > 0) {
141 int32_t * ei = entry->data;
142 if ((ei - 2) == h->blob) h->blob = _free(h->blob);
145 } else if (!ENTRY_IN_REGION(entry)) {
146 entry->data = _free(entry->data);
150 h->index = _free(h->index);
157 static Header headerCreate(void *blob, int32_t indexLen)
159 Header h = xcalloc(1, sizeof(*h));
162 h->indexAlloced = indexLen + 1;
163 h->indexUsed = indexLen;
165 h->indexAlloced = INDEX_MALLOC_SIZE;
169 h->flags |= HEADERFLAG_SORTED;
171 h->index = (h->indexAlloced
172 ? xcalloc(h->indexAlloced, sizeof(*h->index))
176 return headerLink(h);
179 Header headerNew(void)
181 return headerCreate(NULL, 0);
184 int headerVerifyInfo(int il, int dl, const void * pev, void * iv, int negate)
186 entryInfo pe = (entryInfo) pev;
190 for (i = 0; i < il; i++) {
191 info->tag = ntohl(pe[i].tag);
192 info->type = ntohl(pe[i].type);
193 info->offset = ntohl(pe[i].offset);
195 info->offset = -info->offset;
196 info->count = ntohl(pe[i].count);
198 if (hdrchkType(info->type))
200 if (hdrchkAlign(info->type, info->offset))
202 if (!negate && hdrchkRange(dl, info->offset))
204 if (hdrchkData(info->count))
211 static int indexCmp(const void * avp, const void * bvp)
213 indexEntry ap = (indexEntry) avp, bp = (indexEntry) bvp;
214 return (ap->info.tag - bp->info.tag);
217 void headerSort(Header h)
219 if (!(h->flags & HEADERFLAG_SORTED)) {
220 qsort(h->index, h->indexUsed, sizeof(*h->index), indexCmp);
221 h->flags |= HEADERFLAG_SORTED;
225 static int offsetCmp(const void * avp, const void * bvp)
227 indexEntry ap = (indexEntry) avp, bp = (indexEntry) bvp;
228 int rc = (ap->info.offset - bp->info.offset);
231 /* Within a region, entries sort by address. Added drips sort by tag. */
232 if (ap->info.offset < 0)
233 rc = (((char *)ap->data) - ((char *)bp->data));
235 rc = (ap->info.tag - bp->info.tag);
240 void headerUnsort(Header h)
242 if (h->flags & HEADERFLAG_SORTED) {
243 qsort(h->index, h->indexUsed, sizeof(*h->index), offsetCmp);
244 h->flags &= ~HEADERFLAG_SORTED;
248 unsigned headerSizeof(Header h, int magicp)
251 unsigned int size = 0;
260 case HEADER_MAGIC_YES:
261 size += sizeof(rpm_header_magic);
263 case HEADER_MAGIC_NO:
267 size += 2 * sizeof(int32_t); /* count of index entries */
269 for (i = 0, entry = h->index; i < h->indexUsed; i++, entry++) {
272 /* Regions go in as is ... */
273 if (ENTRY_IS_REGION(entry)) {
274 size += entry->length;
275 /* XXX Legacy regions do not include the region tag and data. */
276 if (i == 0 && (h->flags & HEADERFLAG_LEGACY))
277 size += sizeof(struct entryInfo_s) + entry->info.count;
281 /* ... and region elements are skipped. */
282 if (entry->info.offset < 0)
286 type = entry->info.type;
287 if (typeSizes[type] > 1) {
288 unsigned diff = typeSizes[type] - (size % typeSizes[type]);
289 if (diff != typeSizes[type]) {
294 size += sizeof(struct entryInfo_s) + entry->length;
300 /* Bounded header string (array) size calculation, return -1 on error */
301 static inline int strtaglen(const char *str, rpm_count_t c, const char *end)
305 for (s = str; s < end; s++) {
306 if ((*s == '\0') && (--c == 0))
309 return (c > 0) ? -1 : (s - str + 1);
313 * Return length of entry data.
314 * @param type entry data type
315 * @param p entry data
316 * @param count entry item count
317 * @param onDisk data is concatenated strings (with NUL's))?
318 * @param pend pointer to end of data (or NULL)
319 * @return no. bytes in data, -1 on failure
321 static int dataLength(rpm_tagtype_t type, rpm_constdata_t p, rpm_count_t count,
322 int onDisk, rpm_constdata_t pend)
325 /* Not all callers supply data end, avoid falling over edge of the world */
326 const char * se = pend ? pend : s + HEADER_DATA_MAX;
330 case RPM_STRING_TYPE:
333 length = strtaglen(s, 1, se);
336 case RPM_STRING_ARRAY_TYPE:
337 case RPM_I18NSTRING_TYPE:
338 /* These are like RPM_STRING_TYPE, except they're *always* an array */
339 /* Compute sum of length of all strings, including nul terminators */
342 length = strtaglen(s, count, se);
344 const char ** av = (const char **)p;
346 /* add one for null termination */
347 length += strlen(*av++) + 1;
353 if (typeSizes[type] == -1)
355 length = typeSizes[(type & 0xf)] * count;
356 if (length < 0 || (se && (s + length) > se))
365 * Swap int32_t and int16_t arrays within header region.
367 * If a header region tag is in the set to be swabbed, as the data for a
368 * a header region is located after all other tag data.
370 * @param entry header entry
371 * @param il no. of entries
372 * @param dl start no. bytes of data
373 * @param pe header physical entry pointer (swapped)
374 * @param dataStart header data start
375 * @param dataEnd header data end
376 * @param regionid region offset
377 * @return no. bytes of data in region, -1 on error
379 static int regionSwab(indexEntry entry, int il, int dl,
381 unsigned char * dataStart,
382 const unsigned char * dataEnd,
385 if ((entry != NULL && regionid >= 0) || (entry == NULL && regionid != 0))
388 for (; il > 0; il--, pe++) {
389 struct indexEntry_s ie;
392 ie.info.tag = ntohl(pe->tag);
393 ie.info.type = ntohl(pe->type);
394 ie.info.count = ntohl(pe->count);
395 ie.info.offset = ntohl(pe->offset);
397 if (hdrchkType(ie.info.type))
399 if (hdrchkData(ie.info.count))
401 if (hdrchkData(ie.info.offset))
403 if (hdrchkAlign(ie.info.type, ie.info.offset))
406 ie.data = dataStart + ie.info.offset;
407 if (dataEnd && (unsigned char *)ie.data >= dataEnd)
410 ie.length = dataLength(ie.info.type, ie.data, ie.info.count, 1, dataEnd);
411 if (ie.length < 0 || hdrchkData(ie.length))
417 ie.info.offset = regionid;
418 *entry = ie; /* structure assignment */
424 if (typeSizes[type] > 1) {
425 unsigned diff = typeSizes[type] - (dl % typeSizes[type]);
426 if (diff != typeSizes[type]) {
431 /* Perform endian conversions */
432 switch (ntohl(pe->type)) {
434 { uint64_t * it = ie.data;
435 for (; ie.info.count > 0; ie.info.count--, it += 1) {
436 if (dataEnd && ((unsigned char *)it) >= dataEnd)
442 { int32_t * it = ie.data;
443 for (; ie.info.count > 0; ie.info.count--, it += 1) {
444 if (dataEnd && ((unsigned char *)it) >= dataEnd)
450 { int16_t * it = ie.data;
451 for (; ie.info.count > 0; ie.info.count--, it += 1) {
452 if (dataEnd && ((unsigned char *)it) >= dataEnd)
468 * @retval *lengthPtr no. bytes in unloaded header blob
469 * @return unloaded header blob (NULL on error)
471 static void * doHeaderUnload(Header h, size_t * lengthPtr)
483 int drlen, ndribbles;
485 if (h == NULL) return NULL;
487 /* Sort entries by (offset,tag). */
490 /* Compute (il,dl) for all tags, including those deleted in region. */
491 drlen = ndribbles = 0;
492 for (i = 0, entry = h->index; i < h->indexUsed; i++, entry++) {
493 if (ENTRY_IS_REGION(entry)) {
494 int32_t rdl = -entry->info.offset; /* negative offset */
495 int32_t ril = rdl/sizeof(*pe);
496 int rid = entry->info.offset;
499 dl += entry->rdlen + entry->info.count;
500 /* XXX Legacy regions do not include the region tag and data. */
501 if (i == 0 && (h->flags & HEADERFLAG_LEGACY))
504 /* Skip rest of entries in region, but account for dribbles. */
505 for (; i < h->indexUsed && entry->info.offset <= rid+1; i++, entry++) {
506 if (entry->info.offset <= rid)
510 type = entry->info.type;
511 if (typeSizes[type] > 1) {
512 unsigned diff = typeSizes[type] - (dl % typeSizes[type]);
513 if (diff != typeSizes[type]) {
521 drlen += entry->length;
529 /* Ignore deleted drips. */
530 if (entry->data == NULL || entry->length <= 0)
534 type = entry->info.type;
535 if (typeSizes[type] > 1) {
536 unsigned diff = typeSizes[type] - (dl % typeSizes[type]);
537 if (diff != typeSizes[type]) {
546 /* Sanity checks on header intro. */
547 if (hdrchkTags(il) || hdrchkData(dl))
550 len = sizeof(il) + sizeof(dl) + (il * sizeof(*pe)) + dl;
556 pe = (entryInfo) &ei[2];
557 dataStart = te = (char *) (pe + il);
559 for (i = 0, entry = h->index; i < h->indexUsed; i++, entry++) {
565 if (entry->data == NULL || entry->length <= 0)
568 t = (unsigned char*)te;
569 pe->tag = htonl(entry->info.tag);
570 pe->type = htonl(entry->info.type);
571 pe->count = htonl(entry->info.count);
573 if (ENTRY_IS_REGION(entry)) {
574 int32_t rdl = -entry->info.offset; /* negative offset */
575 int32_t ril = rdl/sizeof(*pe) + ndribbles;
576 int rid = entry->info.offset;
578 src = (char *)entry->data;
579 rdlen = entry->rdlen;
581 /* XXX Legacy regions do not include the region tag and data. */
582 if (i == 0 && (h->flags & HEADERFLAG_LEGACY)) {
585 memcpy(pe+1, src, rdl);
586 memcpy(te, src + rdl, rdlen);
589 pe->offset = htonl(te - dataStart);
592 stei[2] = htonl(-rdl-entry->info.count);
594 memcpy(te, stei, entry->info.count);
595 te += entry->info.count;
597 rdlen += entry->info.count;
599 count = regionSwab(NULL, ril, 0, pe, t, NULL, 0);
605 memcpy(pe+1, src + sizeof(*pe), ((ril-1) * sizeof(*pe)));
606 memcpy(te, src + (ril * sizeof(*pe)), rdlen+entry->info.count+drlen);
609 entryInfo se = (entryInfo)src;
610 int off = ntohl(se->offset);
611 pe->offset = (off) ? htonl(te - dataStart) : htonl(off);
613 te += entry->info.count + drlen;
615 count = regionSwab(NULL, ril, 0, pe, t, NULL, 0);
616 if (count != (rdlen + entry->info.count + drlen))
620 /* Skip rest of entries in region. */
621 while (i < h->indexUsed && entry->info.offset <= rid+1) {
631 /* Ignore deleted drips. */
632 if (entry->data == NULL || entry->length <= 0)
636 type = entry->info.type;
637 if (typeSizes[type] > 1) {
639 diff = typeSizes[type] - ((te - dataStart) % typeSizes[type]);
640 if (diff != typeSizes[type]) {
646 pe->offset = htonl(te - dataStart);
648 /* copy data w/ endian conversions */
649 switch (entry->info.type) {
651 count = entry->info.count;
654 *((uint64_t *)te) = htonll(*((uint64_t *)src));
655 te += sizeof(uint64_t);
656 src += sizeof(uint64_t);
661 count = entry->info.count;
664 *((int32_t *)te) = htonl(*((int32_t *)src));
665 te += sizeof(int32_t);
666 src += sizeof(int32_t);
671 count = entry->info.count;
674 *((int16_t *)te) = htons(*((int16_t *)src));
675 te += sizeof(int16_t);
676 src += sizeof(int16_t);
681 memcpy(te, entry->data, entry->length);
688 /* Insure that there are no memcpy underruns/overruns. */
689 if (((char *)pe) != dataStart)
691 if ((((char *)ei)+len) != te)
706 void * headerUnload(Header h)
708 return doHeaderUnload(h, NULL);
712 * Find matching (tag,type) entry in header.
714 * @param tag entry tag
715 * @param type entry type
716 * @return header entry
719 indexEntry findEntry(Header h, rpmTagVal tag, rpm_tagtype_t type)
722 struct indexEntry_s key;
724 if (h == NULL) return NULL;
725 if (!(h->flags & HEADERFLAG_SORTED)) headerSort(h);
729 entry = bsearch(&key, h->index, h->indexUsed, sizeof(*h->index), indexCmp);
733 if (type == RPM_NULL_TYPE)
737 while (entry->info.tag == tag && entry->info.type != type &&
738 entry > h->index) entry--;
740 if (entry->info.tag == tag && entry->info.type == type)
746 int headerDel(Header h, rpmTagVal tag)
748 indexEntry last = h->index + h->indexUsed;
749 indexEntry entry, first;
752 entry = findEntry(h, tag, RPM_NULL_TYPE);
753 if (!entry) return 1;
755 /* Make sure entry points to the first occurence of this tag. */
756 while (entry > h->index && (entry - 1)->info.tag == tag)
759 /* Free data for tags being removed. */
760 for (first = entry; first < last; first++) {
762 if (first->info.tag != tag)
767 if (ENTRY_IN_REGION(first))
772 ne = (first - entry);
777 memmove(entry, first, (ne * sizeof(*entry)));
783 Header headerLoad(void * uh)
785 int32_t * ei = (int32_t *) uh;
786 int32_t il = ntohl(ei[0]); /* index length */
787 int32_t dl = ntohl(ei[1]); /* data length */
788 size_t pvlen = sizeof(il) + sizeof(dl) +
789 (il * sizeof(struct entryInfo_s)) + dl;
792 unsigned char * dataStart;
793 unsigned char * dataEnd;
797 /* Sanity checks on header intro. */
798 if (hdrchkTags(il) || hdrchkData(dl))
801 pe = (entryInfo) &ei[2];
802 dataStart = (unsigned char *) (pe + il);
803 dataEnd = dataStart + dl;
805 h = headerCreate(uh, il);
808 if (!(htonl(pe->tag) < RPMTAG_HEADERI18NTABLE)) {
809 h->flags |= HEADERFLAG_LEGACY;
810 entry->info.type = REGION_TAG_TYPE;
811 entry->info.tag = RPMTAG_HEADERIMAGE;
812 entry->info.count = REGION_TAG_COUNT;
813 entry->info.offset = ((unsigned char *)pe - dataStart); /* negative offset */
816 entry->length = pvlen - sizeof(il) - sizeof(dl);
817 rdlen = regionSwab(entry+1, il, 0, pe, dataStart, dataEnd, entry->info.offset);
820 entry->rdlen = rdlen;
826 h->flags &= ~HEADERFLAG_LEGACY;
828 entry->info.type = htonl(pe->type);
829 entry->info.count = htonl(pe->count);
831 if (hdrchkType(entry->info.type))
833 if (hdrchkTags(entry->info.count))
836 { int off = ntohl(pe->offset);
839 size_t nb = REGION_TAG_COUNT;
841 if (hdrchkRange(dl, (off + nb)))
843 /* XXX Hmm, why the copy? */
844 memcpy(&stei, dataStart + off, nb);
845 rdl = -ntohl(stei[2]); /* negative offset */
846 ril = rdl/sizeof(*pe);
847 if (hdrchkTags(ril) || hdrchkData(rdl))
849 entry->info.tag = htonl(pe->tag);
852 rdl = (ril * sizeof(struct entryInfo_s));
853 entry->info.tag = RPMTAG_HEADERIMAGE;
856 entry->info.offset = -rdl; /* negative offset */
859 entry->length = pvlen - sizeof(il) - sizeof(dl);
860 rdlen = regionSwab(entry+1, ril-1, 0, pe+1, dataStart, dataEnd, entry->info.offset);
863 entry->rdlen = rdlen;
865 if (ril < h->indexUsed) {
866 indexEntry newEntry = entry + ril;
867 int ne = (h->indexUsed - ril);
868 int rid = entry->info.offset+1;
871 /* Load dribble entries from region. */
872 rc = regionSwab(newEntry, ne, 0, pe+ril, dataStart, dataEnd, rid);
877 { indexEntry firstEntry = newEntry;
878 int save = h->indexUsed;
881 /* Dribble entries replace duplicate region entries. */
883 for (j = 0; j < ne; j++, newEntry++) {
884 (void) headerDel(h, newEntry->info.tag);
885 if (newEntry->info.tag == RPMTAG_BASENAMES)
886 (void) headerDel(h, RPMTAG_OLDFILENAMES);
889 /* If any duplicate entries were replaced, move new entries down. */
890 if (h->indexUsed < (save - ne)) {
891 memmove(h->index + h->indexUsed, firstEntry,
892 (ne * sizeof(*entry)));
898 rdlen += REGION_TAG_COUNT;
899 /* XXX should be equality test, but dribbles are sometimes a bit off? */
900 if (rdlen > dl || (rdlen < dl && ril == h->indexUsed))
904 h->flags &= ~HEADERFLAG_SORTED;
906 h->flags |= HEADERFLAG_ALLOCATED;
918 Header headerReload(Header h, rpmTagVal tag)
921 void * uh = doHeaderUnload(h, NULL);
931 if (ENTRY_IS_REGION(nh->index)) {
932 if (tag == RPMTAG_HEADERSIGNATURES || tag == RPMTAG_HEADERIMMUTABLE)
933 nh->index[0].info.tag = tag;
938 Header headerCopyLoad(const void * uh)
940 int32_t * ei = (int32_t *) uh;
941 int32_t il = ntohl(ei[0]); /* index length */
942 int32_t dl = ntohl(ei[1]); /* data length */
943 size_t pvlen = sizeof(il) + sizeof(dl) +
944 (il * sizeof(struct entryInfo_s)) + dl;
947 /* Sanity checks on header intro. */
948 if (!(hdrchkTags(il) || hdrchkData(dl)) && pvlen < headerMaxbytes) {
949 void * nuh = memcpy(xmalloc(pvlen), uh, pvlen);
950 if ((h = headerLoad(nuh)) == NULL)
956 Header headerRead(FD_t fd, int magicp)
965 if (magicp == HEADER_MAGIC_YES) {
968 if (Fread(block, 1, 4*sizeof(*block), fd) != 4*sizeof(*block))
973 if (memcmp(&magic, rpm_header_magic, sizeof(magic)))
976 il = ntohl(block[2]);
977 dl = ntohl(block[3]);
979 if (Fread(block, 1, 2*sizeof(*block), fd) != 2*sizeof(*block))
982 il = ntohl(block[0]);
983 dl = ntohl(block[1]);
986 len = sizeof(il) + sizeof(dl) + (il * sizeof(struct entryInfo_s)) + dl;
988 /* Sanity checks on header intro. */
989 if (hdrchkTags(il) || hdrchkData(dl) || len > headerMaxbytes)
995 len -= sizeof(il) + sizeof(dl);
997 if (Fread((char *)&ei[2], 1, len, fd) != len)
1003 if (h == NULL && ei != NULL) {
1009 int headerWrite(FD_t fd, Header h, int magicp)
1015 uh = doHeaderUnload(h, &length);
1019 case HEADER_MAGIC_YES:
1020 nb = Fwrite(rpm_header_magic, sizeof(uint8_t), sizeof(rpm_header_magic), fd);
1021 if (nb != sizeof(rpm_header_magic))
1024 case HEADER_MAGIC_NO:
1028 nb = Fwrite(uh, sizeof(char), length, fd);
1032 return (nb == length ? 0 : 1);
1035 int headerIsEntry(Header h, rpmTagVal tag)
1037 /* FIX: h modified by sort. */
1038 return (findEntry(h, tag, RPM_NULL_TYPE) ? 1 : 0);
1043 * Retrieve data from header entry.
1044 * Relevant flags (others are ignored), if neither is set allocation
1045 * behavior depends on data type(!)
1046 * HEADERGET_MINMEM: return pointers to header memory
1047 * HEADERGET_ALLOC: always return malloced memory, overrides MINMEM
1049 * @todo Permit retrieval of regions other than HEADER_IMUTABLE.
1050 * @param entry header entry
1051 * @param td tag data container
1052 * @param minMem string pointers refer to header memory?
1053 * @param flags flags to control memory allocation
1054 * @return 1 on success, otherwise error.
1056 static int copyTdEntry(const indexEntry entry, rpmtd td, headerGetFlags flags)
1058 rpm_count_t count = entry->info.count;
1059 int rc = 1; /* XXX 1 on success. */
1060 /* ALLOC overrides MINMEM */
1061 int allocMem = flags & HEADERGET_ALLOC;
1062 int minMem = allocMem ? 0 : flags & HEADERGET_MINMEM;
1063 int argvArray = (flags & HEADERGET_ARGV) ? 1 : 0;
1066 td->flags = RPMTD_IMMUTABLE;
1067 switch (entry->info.type) {
1070 * XXX This only works for
1071 * XXX "sealed" HEADER_IMMUTABLE/HEADER_SIGNATURES/HEADER_IMAGE.
1072 * XXX This will *not* work for unsealed legacy HEADER_IMAGE (i.e.
1073 * XXX a legacy header freshly read, but not yet unloaded to the rpmdb).
1075 if (ENTRY_IS_REGION(entry)) {
1076 int32_t * ei = ((int32_t *)entry->data) - 2;
1077 entryInfo pe = (entryInfo) (ei + 2);
1078 unsigned char * dataStart = (unsigned char *) (pe + ntohl(ei[0]));
1079 int32_t rdl = -entry->info.offset; /* negative offset */
1080 int32_t ril = rdl/sizeof(*pe);
1083 count = 2 * sizeof(*ei) + (ril * sizeof(*pe)) + rdl;
1084 if (entry->info.tag == RPMTAG_HEADERIMAGE) {
1088 count += REGION_TAG_COUNT;
1089 rdl += REGION_TAG_COUNT;
1092 td->data = xmalloc(count);
1093 ei = (int32_t *) td->data;
1097 pe = (entryInfo) memcpy(ei + 2, pe, (ril * sizeof(*pe)));
1099 dataStart = (unsigned char *) memcpy(pe + ril, dataStart, rdl);
1101 rc = regionSwab(NULL, ril, 0, pe, dataStart, dataStart + rdl, 0);
1102 /* don't return data on failure */
1104 td->data = _free(td->data);
1106 /* XXX 1 on success. */
1107 rc = (rc < 0) ? 0 : 1;
1109 count = entry->length;
1111 ? memcpy(xmalloc(count), entry->data, count)
1115 case RPM_STRING_TYPE:
1116 /* simple string, but fallthrough if its actually an array */
1117 if (count == 1 && !argvArray) {
1118 td->data = allocMem ? xstrdup(entry->data) : entry->data;
1121 case RPM_STRING_ARRAY_TYPE:
1122 case RPM_I18NSTRING_TYPE:
1123 { const char ** ptrEntry;
1124 int tableSize = (count + argvArray) * sizeof(char *);
1129 td->data = xmalloc(tableSize);
1130 ptrEntry = (const char **) td->data;
1133 t = xmalloc(tableSize + entry->length);
1134 td->data = (void *)t;
1135 ptrEntry = (const char **) td->data;
1137 memcpy(t, entry->data, entry->length);
1139 for (i = 0; i < count; i++) {
1146 td->flags |= RPMTD_ARGV;
1151 case RPM_INT16_TYPE:
1152 case RPM_INT32_TYPE:
1153 case RPM_INT64_TYPE:
1155 td->data = xmalloc(entry->length);
1156 memcpy(td->data, entry->data, entry->length);
1158 td->data = entry->data;
1162 /* WTH? Don't mess with unknown data types... */
1167 td->type = entry->info.type;
1170 if (td->data && entry->data != td->data) {
1171 td->flags |= RPMTD_ALLOCED;
1178 * Does locale match entry in header i18n table?
1181 * The range [l,le) contains the next locale to match:
1182 * ll[_CC][.EEEEE][@dddd]
1184 * ll ISO language code (in lowercase).
1185 * CC (optional) ISO coutnry code (in uppercase).
1186 * EEEEE (optional) encoding (not really standardized).
1187 * dddd (optional) dialect.
1190 * @param td header i18n table data, NUL terminated
1191 * @param l start of locale to match
1192 * @param le end of locale to match
1193 * @return 1 on good match, 2 on weak match, 0 on no match
1195 static int headerMatchLocale(const char *td, const char *l, const char *le)
1199 /* First try a complete match. */
1200 if (strlen(td) == (le-l) && rstreqn(td, l, (le - l)))
1203 /* Next, try stripping optional dialect and matching. */
1204 for (fe = l; fe < le && *fe != '@'; fe++)
1206 if (fe < le && rstreqn(td, l, (fe - l)))
1209 /* Next, try stripping optional codeset and matching. */
1210 for (fe = l; fe < le && *fe != '.'; fe++)
1212 if (fe < le && rstreqn(td, l, (fe - l)))
1215 /* Finally, try stripping optional country code and matching. */
1216 for (fe = l; fe < le && *fe != '_'; fe++)
1218 if (fe < le && rstreqn(td, l, (fe - l)))
1225 * Return i18n string from header that matches locale.
1227 * @param entry i18n string data
1228 * @retval td tag data container
1229 * @param flags flags to control allocation
1232 static int copyI18NEntry(Header h, indexEntry entry, rpmtd td,
1233 headerGetFlags flags)
1235 const char *lang, *l, *le;
1238 td->type = RPM_STRING_TYPE;
1240 /* if no match, just return the first string */
1241 td->data = entry->data;
1243 /* XXX Drepper sez' this is the order. */
1244 if ((lang = getenv("LANGUAGE")) == NULL &&
1245 (lang = getenv("LC_ALL")) == NULL &&
1246 (lang = getenv("LC_MESSAGES")) == NULL &&
1247 (lang = getenv("LANG")) == NULL)
1250 if ((table = findEntry(h, RPMTAG_HEADERI18NTABLE, RPM_STRING_ARRAY_TYPE)) == NULL)
1253 for (l = lang; *l != '\0'; l = le) {
1255 char *ed, *ed_weak = NULL;
1258 while (*l && *l == ':') /* skip leading colons */
1262 for (le = l; *le && *le != ':'; le++) /* find end of this locale */
1265 /* For each entry in the header ... */
1266 for (langNum = 0, t = table->data, ed = entry->data;
1267 langNum < entry->info.count;
1268 langNum++, t += strlen(t) + 1, ed += strlen(ed) + 1) {
1270 int match = headerMatchLocale(t, l, le);
1274 } else if (match == 2) {
1285 if (flags & HEADERGET_ALLOC) {
1286 td->data = xstrdup(td->data);
1287 td->flags |= RPMTD_ALLOCED;
1294 * Retrieve tag data from header.
1296 * @retval td tag data container
1297 * @param flags flags to control retrieval
1298 * @return 1 on success, 0 on not found
1300 static int intGetTdEntry(Header h, rpmtd td, headerGetFlags flags)
1305 /* First find the tag */
1306 /* FIX: h modified by sort. */
1307 entry = findEntry(h, td->tag, RPM_NULL_TYPE);
1308 if (entry == NULL) {
1309 /* Td is zeroed above, just return... */
1313 if (flags & HEADERGET_RAW) {
1314 rc = copyTdEntry(entry, td, flags);
1316 switch (entry->info.type) {
1317 case RPM_I18NSTRING_TYPE:
1318 rc = copyI18NEntry(h, entry, td, flags);
1321 rc = copyTdEntry(entry, td, flags);
1326 /* XXX 1 on success */
1327 return ((rc == 1) ? 1 : 0);
1330 int headerGet(Header h, rpmTagVal tag, rpmtd td, headerGetFlags flags)
1333 headerTagTagFunction tagfunc = intGetTdEntry;
1335 if (td == NULL) return 0;
1340 if (flags & HEADERGET_EXT) {
1341 headerTagTagFunction extfunc = rpmHeaderTagFunc(tag);
1342 if (extfunc) tagfunc = extfunc;
1344 rc = tagfunc(h, td, flags);
1346 assert(tag == td->tag);
1352 static void copyData(rpm_tagtype_t type, rpm_data_t dstPtr,
1353 rpm_constdata_t srcPtr, rpm_count_t cnt, int dataLength)
1356 case RPM_STRING_ARRAY_TYPE:
1357 case RPM_I18NSTRING_TYPE:
1358 { const char ** av = (const char **) srcPtr;
1361 while (cnt-- > 0 && dataLength > 0) {
1363 if ((s = *av++) == NULL)
1367 } while (s[-1] && --dataLength > 0);
1372 memmove(dstPtr, srcPtr, dataLength);
1378 * Return (malloc'ed) copy of entry data.
1379 * @param type entry data type
1380 * @param p entry data
1381 * @param c entry item count
1382 * @retval lengthPtr no. bytes in returned data
1383 * @return (malloc'ed) copy of entry data, NULL on error
1386 grabData(rpm_tagtype_t type, rpm_constdata_t p, rpm_count_t c, int * lengthPtr)
1388 rpm_data_t data = NULL;
1391 length = dataLength(type, p, c, 0, NULL);
1393 data = xmalloc(length);
1394 copyData(type, data, p, c, length);
1398 *lengthPtr = length;
1402 static int intAddEntry(Header h, rpmtd td)
1408 /* Count must always be >= 1 for headerAddEntry. */
1412 if (hdrchkType(td->type))
1414 if (hdrchkData(td->count))
1418 data = grabData(td->type, td->data, td->count, &length);
1419 if (data == NULL || length <= 0)
1422 /* Allocate more index space if necessary */
1423 if (h->indexUsed == h->indexAlloced) {
1424 h->indexAlloced += INDEX_MALLOC_SIZE;
1425 h->index = xrealloc(h->index, h->indexAlloced * sizeof(*h->index));
1428 /* Fill in the index */
1429 entry = h->index + h->indexUsed;
1430 entry->info.tag = td->tag;
1431 entry->info.type = td->type;
1432 entry->info.count = td->count;
1433 entry->info.offset = 0;
1435 entry->length = length;
1437 if (h->indexUsed > 0 && td->tag < h->index[h->indexUsed-1].info.tag)
1438 h->flags &= ~HEADERFLAG_SORTED;
1444 static int intAppendEntry(Header h, rpmtd td)
1449 if (td->type == RPM_STRING_TYPE || td->type == RPM_I18NSTRING_TYPE) {
1450 /* we can't do this */
1454 /* Find the tag entry in the header. */
1455 entry = findEntry(h, td->tag, td->type);
1459 length = dataLength(td->type, td->data, td->count, 0, NULL);
1463 if (ENTRY_IN_REGION(entry)) {
1464 char * t = xmalloc(entry->length + length);
1465 memcpy(t, entry->data, entry->length);
1467 entry->info.offset = 0;
1469 entry->data = xrealloc(entry->data, entry->length + length);
1471 copyData(td->type, ((char *) entry->data) + entry->length,
1472 td->data, td->count, length);
1474 entry->length += length;
1476 entry->info.count += td->count;
1481 int headerPut(Header h, rpmtd td, headerPutFlags flags)
1486 if (flags & HEADERPUT_APPEND) {
1487 rc = findEntry(h, td->tag, td->type) ?
1488 intAppendEntry(h, td) :
1491 rc = intAddEntry(h, td);
1496 int headerAddI18NString(Header h, rpmTagVal tag, const char * string,
1499 indexEntry table, entry;
1500 const char ** strArray;
1503 rpm_count_t i, langNum;
1506 table = findEntry(h, RPMTAG_HEADERI18NTABLE, RPM_STRING_ARRAY_TYPE);
1507 entry = findEntry(h, tag, RPM_I18NSTRING_TYPE);
1509 if (!table && entry)
1510 return 0; /* this shouldn't ever happen!! */
1512 if (!table && !entry) {
1513 const char * charArray[2];
1514 rpm_count_t count = 0;
1516 if (!lang || (lang[0] == 'C' && lang[1] == '\0')) {
1517 charArray[count++] = "C";
1519 charArray[count++] = "C";
1520 charArray[count++] = lang;
1524 td.tag = RPMTAG_HEADERI18NTABLE;
1525 td.type = RPM_STRING_ARRAY_TYPE;
1526 td.data = (void *) charArray;
1528 if (!headerPut(h, &td, HEADERPUT_DEFAULT))
1530 table = findEntry(h, RPMTAG_HEADERI18NTABLE, RPM_STRING_ARRAY_TYPE);
1535 if (!lang) lang = "C";
1537 { const char * l = table->data;
1538 for (langNum = 0; langNum < table->info.count; langNum++) {
1539 if (rstreq(l, lang)) break;
1544 if (langNum >= table->info.count) {
1545 length = strlen(lang) + 1;
1546 if (ENTRY_IN_REGION(table)) {
1547 char * t = xmalloc(table->length + length);
1548 memcpy(t, table->data, table->length);
1550 table->info.offset = 0;
1552 table->data = xrealloc(table->data, table->length + length);
1553 memmove(((char *)table->data) + table->length, lang, length);
1554 table->length += length;
1555 table->info.count++;
1561 strArray = xmalloc(sizeof(*strArray) * (langNum + 1));
1562 for (i = 0; i < langNum; i++)
1564 strArray[langNum] = string;
1568 td.type = RPM_I18NSTRING_TYPE;
1570 td.count = langNum + 1;
1571 rc = headerPut(h, &td, HEADERPUT_DEFAULT);
1574 } else if (langNum >= entry->info.count) {
1575 ghosts = langNum - entry->info.count;
1577 length = strlen(string) + 1 + ghosts;
1578 if (ENTRY_IN_REGION(entry)) {
1579 char * t = xmalloc(entry->length + length);
1580 memcpy(t, entry->data, entry->length);
1582 entry->info.offset = 0;
1584 entry->data = xrealloc(entry->data, entry->length + length);
1586 memset(((char *)entry->data) + entry->length, '\0', ghosts);
1587 memmove(((char *)entry->data) + entry->length + ghosts, string, strlen(string)+1);
1589 entry->length += length;
1590 entry->info.count = langNum + 1;
1592 char *b, *be, *e, *ee, *t;
1595 /* Set beginning/end pointers to previous data */
1596 b = be = e = ee = entry->data;
1597 for (i = 0; i < table->info.count; i++) {
1600 ee += strlen(ee) + 1;
1605 /* Get storage for new buffer */
1607 sn = strlen(string) + 1;
1609 length = bn + sn + en;
1610 t = buf = xmalloc(length);
1612 /* Copy values into new storage */
1615 memcpy(t, string, sn);
1620 /* Replace i18N string array */
1621 entry->length -= strlen(be) + 1;
1622 entry->length += sn;
1624 if (ENTRY_IN_REGION(entry)) {
1625 entry->info.offset = 0;
1627 entry->data = _free(entry->data);
1634 int headerMod(Header h, rpmtd td)
1641 /* First find the tag */
1642 entry = findEntry(h, td->tag, td->type);
1647 data = grabData(td->type, td->data, td->count, &length);
1648 if (data == NULL || length <= 0)
1651 /* make sure entry points to the first occurence of this tag */
1652 while (entry > h->index && (entry - 1)->info.tag == td->tag)
1655 /* free after we've grabbed the new data in case the two are intertwined;
1656 that's a bad idea but at least we won't break */
1657 oldData = entry->data;
1659 entry->info.count = td->count;
1660 entry->info.type = td->type;
1662 entry->length = length;
1664 if (ENTRY_IN_REGION(entry)) {
1665 entry->info.offset = 0;
1673 * Header tag iterator data structure.
1675 struct headerIterator_s {
1676 Header h; /*!< Header being iterated. */
1677 int next_index; /*!< Next tag index. */
1680 HeaderIterator headerFreeIterator(HeaderIterator hi)
1683 hi->h = headerFree(hi->h);
1689 HeaderIterator headerInitIterator(Header h)
1691 HeaderIterator hi = xmalloc(sizeof(*hi));
1695 hi->h = headerLink(h);
1700 static indexEntry nextIndex(HeaderIterator hi)
1704 indexEntry entry = NULL;
1706 for (slot = hi->next_index; slot < h->indexUsed; slot++) {
1707 entry = h->index + slot;
1708 if (!ENTRY_IS_REGION(entry))
1711 hi->next_index = slot;
1712 if (entry == NULL || slot >= h->indexUsed)
1719 rpmTagVal headerNextTag(HeaderIterator hi)
1721 indexEntry entry = nextIndex(hi);
1722 return entry ? entry->info.tag : RPMTAG_NOT_FOUND;
1725 int headerNext(HeaderIterator hi, rpmtd td)
1727 indexEntry entry = nextIndex(hi);
1732 td->tag = entry->info.tag;
1733 rc = copyTdEntry(entry, td, HEADERGET_DEFAULT);
1735 return ((rc == 1) ? 1 : 0);
1738 unsigned int headerGetInstance(Header h)
1740 return h ? h->instance : 0;
1743 void headerSetInstance(Header h, unsigned int instance)
1745 h->instance = instance;