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;
870 /* Load dribble entries from region. */
871 rdlen = regionSwab(newEntry, ne, rdlen, pe+ril,
872 dataStart, dataEnd, rid);
876 { indexEntry firstEntry = newEntry;
877 int save = h->indexUsed;
880 /* Dribble entries replace duplicate region entries. */
882 for (j = 0; j < ne; j++, newEntry++) {
883 (void) headerDel(h, newEntry->info.tag);
884 if (newEntry->info.tag == RPMTAG_BASENAMES)
885 (void) headerDel(h, RPMTAG_OLDFILENAMES);
888 /* If any duplicate entries were replaced, move new entries down. */
889 if (h->indexUsed < (save - ne)) {
890 memmove(h->index + h->indexUsed, firstEntry,
891 (ne * sizeof(*entry)));
897 rdlen += REGION_TAG_COUNT;
903 h->flags &= ~HEADERFLAG_SORTED;
905 h->flags |= HEADERFLAG_ALLOCATED;
917 Header headerReload(Header h, rpmTagVal tag)
920 void * uh = doHeaderUnload(h, NULL);
930 if (ENTRY_IS_REGION(nh->index)) {
931 if (tag == RPMTAG_HEADERSIGNATURES || tag == RPMTAG_HEADERIMMUTABLE)
932 nh->index[0].info.tag = tag;
937 Header headerCopyLoad(const void * uh)
939 int32_t * ei = (int32_t *) uh;
940 int32_t il = ntohl(ei[0]); /* index length */
941 int32_t dl = ntohl(ei[1]); /* data length */
942 size_t pvlen = sizeof(il) + sizeof(dl) +
943 (il * sizeof(struct entryInfo_s)) + dl;
946 /* Sanity checks on header intro. */
947 if (!(hdrchkTags(il) || hdrchkData(dl)) && pvlen < headerMaxbytes) {
948 void * nuh = memcpy(xmalloc(pvlen), uh, pvlen);
949 if ((h = headerLoad(nuh)) == NULL)
955 Header headerRead(FD_t fd, int magicp)
964 if (magicp == HEADER_MAGIC_YES) {
967 if (Fread(block, 1, 4*sizeof(*block), fd) != 4*sizeof(*block))
972 if (memcmp(&magic, rpm_header_magic, sizeof(magic)))
975 il = ntohl(block[2]);
976 dl = ntohl(block[3]);
978 if (Fread(block, 1, 2*sizeof(*block), fd) != 2*sizeof(*block))
981 il = ntohl(block[0]);
982 dl = ntohl(block[1]);
985 len = sizeof(il) + sizeof(dl) + (il * sizeof(struct entryInfo_s)) + dl;
987 /* Sanity checks on header intro. */
988 if (hdrchkTags(il) || hdrchkData(dl) || len > headerMaxbytes)
994 len -= sizeof(il) + sizeof(dl);
996 if (Fread((char *)&ei[2], 1, len, fd) != len)
1002 if (h == NULL && ei != NULL) {
1008 int headerWrite(FD_t fd, Header h, int magicp)
1014 uh = doHeaderUnload(h, &length);
1018 case HEADER_MAGIC_YES:
1019 nb = Fwrite(rpm_header_magic, sizeof(uint8_t), sizeof(rpm_header_magic), fd);
1020 if (nb != sizeof(rpm_header_magic))
1023 case HEADER_MAGIC_NO:
1027 nb = Fwrite(uh, sizeof(char), length, fd);
1031 return (nb == length ? 0 : 1);
1034 int headerIsEntry(Header h, rpmTagVal tag)
1036 /* FIX: h modified by sort. */
1037 return (findEntry(h, tag, RPM_NULL_TYPE) ? 1 : 0);
1042 * Retrieve data from header entry.
1043 * Relevant flags (others are ignored), if neither is set allocation
1044 * behavior depends on data type(!)
1045 * HEADERGET_MINMEM: return pointers to header memory
1046 * HEADERGET_ALLOC: always return malloced memory, overrides MINMEM
1048 * @todo Permit retrieval of regions other than HEADER_IMUTABLE.
1049 * @param entry header entry
1050 * @param td tag data container
1051 * @param minMem string pointers refer to header memory?
1052 * @param flags flags to control memory allocation
1053 * @return 1 on success, otherwise error.
1055 static int copyTdEntry(const indexEntry entry, rpmtd td, headerGetFlags flags)
1057 rpm_count_t count = entry->info.count;
1058 int rc = 1; /* XXX 1 on success. */
1059 /* ALLOC overrides MINMEM */
1060 int allocMem = flags & HEADERGET_ALLOC;
1061 int minMem = allocMem ? 0 : flags & HEADERGET_MINMEM;
1062 int argvArray = (flags & HEADERGET_ARGV) ? 1 : 0;
1065 td->flags = RPMTD_IMMUTABLE;
1066 switch (entry->info.type) {
1069 * XXX This only works for
1070 * XXX "sealed" HEADER_IMMUTABLE/HEADER_SIGNATURES/HEADER_IMAGE.
1071 * XXX This will *not* work for unsealed legacy HEADER_IMAGE (i.e.
1072 * XXX a legacy header freshly read, but not yet unloaded to the rpmdb).
1074 if (ENTRY_IS_REGION(entry)) {
1075 int32_t * ei = ((int32_t *)entry->data) - 2;
1076 entryInfo pe = (entryInfo) (ei + 2);
1077 unsigned char * dataStart = (unsigned char *) (pe + ntohl(ei[0]));
1078 int32_t rdl = -entry->info.offset; /* negative offset */
1079 int32_t ril = rdl/sizeof(*pe);
1082 count = 2 * sizeof(*ei) + (ril * sizeof(*pe)) + rdl;
1083 if (entry->info.tag == RPMTAG_HEADERIMAGE) {
1087 count += REGION_TAG_COUNT;
1088 rdl += REGION_TAG_COUNT;
1091 td->data = xmalloc(count);
1092 ei = (int32_t *) td->data;
1096 pe = (entryInfo) memcpy(ei + 2, pe, (ril * sizeof(*pe)));
1098 dataStart = (unsigned char *) memcpy(pe + ril, dataStart, rdl);
1100 rc = regionSwab(NULL, ril, 0, pe, dataStart, dataStart + rdl, 0);
1101 /* don't return data on failure */
1103 td->data = _free(td->data);
1105 /* XXX 1 on success. */
1106 rc = (rc < 0) ? 0 : 1;
1108 count = entry->length;
1110 ? memcpy(xmalloc(count), entry->data, count)
1114 case RPM_STRING_TYPE:
1115 /* simple string, but fallthrough if its actually an array */
1116 if (count == 1 && !argvArray) {
1117 td->data = allocMem ? xstrdup(entry->data) : entry->data;
1120 case RPM_STRING_ARRAY_TYPE:
1121 case RPM_I18NSTRING_TYPE:
1122 { const char ** ptrEntry;
1123 int tableSize = (count + argvArray) * sizeof(char *);
1128 td->data = xmalloc(tableSize);
1129 ptrEntry = (const char **) td->data;
1132 t = xmalloc(tableSize + entry->length);
1133 td->data = (void *)t;
1134 ptrEntry = (const char **) td->data;
1136 memcpy(t, entry->data, entry->length);
1138 for (i = 0; i < count; i++) {
1145 td->flags |= RPMTD_ARGV;
1150 case RPM_INT16_TYPE:
1151 case RPM_INT32_TYPE:
1152 case RPM_INT64_TYPE:
1154 td->data = xmalloc(entry->length);
1155 memcpy(td->data, entry->data, entry->length);
1157 td->data = entry->data;
1161 /* WTH? Don't mess with unknown data types... */
1166 td->type = entry->info.type;
1169 if (td->data && entry->data != td->data) {
1170 td->flags |= RPMTD_ALLOCED;
1177 * Does locale match entry in header i18n table?
1180 * The range [l,le) contains the next locale to match:
1181 * ll[_CC][.EEEEE][@dddd]
1183 * ll ISO language code (in lowercase).
1184 * CC (optional) ISO coutnry code (in uppercase).
1185 * EEEEE (optional) encoding (not really standardized).
1186 * dddd (optional) dialect.
1189 * @param td header i18n table data, NUL terminated
1190 * @param l start of locale to match
1191 * @param le end of locale to match
1192 * @return 1 on good match, 2 on weak match, 0 on no match
1194 static int headerMatchLocale(const char *td, const char *l, const char *le)
1198 /* First try a complete match. */
1199 if (strlen(td) == (le-l) && rstreqn(td, l, (le - l)))
1202 /* Next, try stripping optional dialect and matching. */
1203 for (fe = l; fe < le && *fe != '@'; fe++)
1205 if (fe < le && rstreqn(td, l, (fe - l)))
1208 /* Next, try stripping optional codeset and matching. */
1209 for (fe = l; fe < le && *fe != '.'; fe++)
1211 if (fe < le && rstreqn(td, l, (fe - l)))
1214 /* Finally, try stripping optional country code and matching. */
1215 for (fe = l; fe < le && *fe != '_'; fe++)
1217 if (fe < le && rstreqn(td, l, (fe - l)))
1224 * Return i18n string from header that matches locale.
1226 * @param entry i18n string data
1227 * @retval td tag data container
1228 * @param flags flags to control allocation
1231 static int copyI18NEntry(Header h, indexEntry entry, rpmtd td,
1232 headerGetFlags flags)
1234 const char *lang, *l, *le;
1237 td->type = RPM_STRING_TYPE;
1239 /* if no match, just return the first string */
1240 td->data = entry->data;
1242 /* XXX Drepper sez' this is the order. */
1243 if ((lang = getenv("LANGUAGE")) == NULL &&
1244 (lang = getenv("LC_ALL")) == NULL &&
1245 (lang = getenv("LC_MESSAGES")) == NULL &&
1246 (lang = getenv("LANG")) == NULL)
1249 if ((table = findEntry(h, RPMTAG_HEADERI18NTABLE, RPM_STRING_ARRAY_TYPE)) == NULL)
1252 for (l = lang; *l != '\0'; l = le) {
1254 char *ed, *ed_weak = NULL;
1257 while (*l && *l == ':') /* skip leading colons */
1261 for (le = l; *le && *le != ':'; le++) /* find end of this locale */
1264 /* For each entry in the header ... */
1265 for (langNum = 0, t = table->data, ed = entry->data;
1266 langNum < entry->info.count;
1267 langNum++, t += strlen(t) + 1, ed += strlen(ed) + 1) {
1269 int match = headerMatchLocale(t, l, le);
1273 } else if (match == 2) {
1284 if (flags & HEADERGET_ALLOC) {
1285 td->data = xstrdup(td->data);
1286 td->flags |= RPMTD_ALLOCED;
1293 * Retrieve tag data from header.
1295 * @retval td tag data container
1296 * @param flags flags to control retrieval
1297 * @return 1 on success, 0 on not found
1299 static int intGetTdEntry(Header h, rpmtd td, headerGetFlags flags)
1304 /* First find the tag */
1305 /* FIX: h modified by sort. */
1306 entry = findEntry(h, td->tag, RPM_NULL_TYPE);
1307 if (entry == NULL) {
1308 /* Td is zeroed above, just return... */
1312 if (flags & HEADERGET_RAW) {
1313 rc = copyTdEntry(entry, td, flags);
1315 switch (entry->info.type) {
1316 case RPM_I18NSTRING_TYPE:
1317 rc = copyI18NEntry(h, entry, td, flags);
1320 rc = copyTdEntry(entry, td, flags);
1325 /* XXX 1 on success */
1326 return ((rc == 1) ? 1 : 0);
1329 int headerGet(Header h, rpmTagVal tag, rpmtd td, headerGetFlags flags)
1332 headerTagTagFunction tagfunc = intGetTdEntry;
1334 if (td == NULL) return 0;
1339 if (flags & HEADERGET_EXT) {
1340 headerTagTagFunction extfunc = rpmHeaderTagFunc(tag);
1341 if (extfunc) tagfunc = extfunc;
1343 rc = tagfunc(h, td, flags);
1345 assert(tag == td->tag);
1351 static void copyData(rpm_tagtype_t type, rpm_data_t dstPtr,
1352 rpm_constdata_t srcPtr, rpm_count_t cnt, int dataLength)
1355 case RPM_STRING_ARRAY_TYPE:
1356 case RPM_I18NSTRING_TYPE:
1357 { const char ** av = (const char **) srcPtr;
1360 while (cnt-- > 0 && dataLength > 0) {
1362 if ((s = *av++) == NULL)
1366 } while (s[-1] && --dataLength > 0);
1371 memmove(dstPtr, srcPtr, dataLength);
1377 * Return (malloc'ed) copy of entry data.
1378 * @param type entry data type
1379 * @param p entry data
1380 * @param c entry item count
1381 * @retval lengthPtr no. bytes in returned data
1382 * @return (malloc'ed) copy of entry data, NULL on error
1385 grabData(rpm_tagtype_t type, rpm_constdata_t p, rpm_count_t c, int * lengthPtr)
1387 rpm_data_t data = NULL;
1390 length = dataLength(type, p, c, 0, NULL);
1392 data = xmalloc(length);
1393 copyData(type, data, p, c, length);
1397 *lengthPtr = length;
1401 static int intAddEntry(Header h, rpmtd td)
1407 /* Count must always be >= 1 for headerAddEntry. */
1411 if (hdrchkType(td->type))
1413 if (hdrchkData(td->count))
1417 data = grabData(td->type, td->data, td->count, &length);
1418 if (data == NULL || length <= 0)
1421 /* Allocate more index space if necessary */
1422 if (h->indexUsed == h->indexAlloced) {
1423 h->indexAlloced += INDEX_MALLOC_SIZE;
1424 h->index = xrealloc(h->index, h->indexAlloced * sizeof(*h->index));
1427 /* Fill in the index */
1428 entry = h->index + h->indexUsed;
1429 entry->info.tag = td->tag;
1430 entry->info.type = td->type;
1431 entry->info.count = td->count;
1432 entry->info.offset = 0;
1434 entry->length = length;
1436 if (h->indexUsed > 0 && td->tag < h->index[h->indexUsed-1].info.tag)
1437 h->flags &= ~HEADERFLAG_SORTED;
1443 static int intAppendEntry(Header h, rpmtd td)
1448 if (td->type == RPM_STRING_TYPE || td->type == RPM_I18NSTRING_TYPE) {
1449 /* we can't do this */
1453 /* Find the tag entry in the header. */
1454 entry = findEntry(h, td->tag, td->type);
1458 length = dataLength(td->type, td->data, td->count, 0, NULL);
1462 if (ENTRY_IN_REGION(entry)) {
1463 char * t = xmalloc(entry->length + length);
1464 memcpy(t, entry->data, entry->length);
1466 entry->info.offset = 0;
1468 entry->data = xrealloc(entry->data, entry->length + length);
1470 copyData(td->type, ((char *) entry->data) + entry->length,
1471 td->data, td->count, length);
1473 entry->length += length;
1475 entry->info.count += td->count;
1480 int headerPut(Header h, rpmtd td, headerPutFlags flags)
1485 if (flags & HEADERPUT_APPEND) {
1486 rc = findEntry(h, td->tag, td->type) ?
1487 intAppendEntry(h, td) :
1490 rc = intAddEntry(h, td);
1495 int headerAddI18NString(Header h, rpmTagVal tag, const char * string,
1498 indexEntry table, entry;
1499 const char ** strArray;
1502 rpm_count_t i, langNum;
1505 table = findEntry(h, RPMTAG_HEADERI18NTABLE, RPM_STRING_ARRAY_TYPE);
1506 entry = findEntry(h, tag, RPM_I18NSTRING_TYPE);
1508 if (!table && entry)
1509 return 0; /* this shouldn't ever happen!! */
1511 if (!table && !entry) {
1512 const char * charArray[2];
1513 rpm_count_t count = 0;
1515 if (!lang || (lang[0] == 'C' && lang[1] == '\0')) {
1516 charArray[count++] = "C";
1518 charArray[count++] = "C";
1519 charArray[count++] = lang;
1523 td.tag = RPMTAG_HEADERI18NTABLE;
1524 td.type = RPM_STRING_ARRAY_TYPE;
1525 td.data = (void *) charArray;
1527 if (!headerPut(h, &td, HEADERPUT_DEFAULT))
1529 table = findEntry(h, RPMTAG_HEADERI18NTABLE, RPM_STRING_ARRAY_TYPE);
1534 if (!lang) lang = "C";
1536 { const char * l = table->data;
1537 for (langNum = 0; langNum < table->info.count; langNum++) {
1538 if (rstreq(l, lang)) break;
1543 if (langNum >= table->info.count) {
1544 length = strlen(lang) + 1;
1545 if (ENTRY_IN_REGION(table)) {
1546 char * t = xmalloc(table->length + length);
1547 memcpy(t, table->data, table->length);
1549 table->info.offset = 0;
1551 table->data = xrealloc(table->data, table->length + length);
1552 memmove(((char *)table->data) + table->length, lang, length);
1553 table->length += length;
1554 table->info.count++;
1560 strArray = xmalloc(sizeof(*strArray) * (langNum + 1));
1561 for (i = 0; i < langNum; i++)
1563 strArray[langNum] = string;
1567 td.type = RPM_I18NSTRING_TYPE;
1569 td.count = langNum + 1;
1570 rc = headerPut(h, &td, HEADERPUT_DEFAULT);
1573 } else if (langNum >= entry->info.count) {
1574 ghosts = langNum - entry->info.count;
1576 length = strlen(string) + 1 + ghosts;
1577 if (ENTRY_IN_REGION(entry)) {
1578 char * t = xmalloc(entry->length + length);
1579 memcpy(t, entry->data, entry->length);
1581 entry->info.offset = 0;
1583 entry->data = xrealloc(entry->data, entry->length + length);
1585 memset(((char *)entry->data) + entry->length, '\0', ghosts);
1586 memmove(((char *)entry->data) + entry->length + ghosts, string, strlen(string)+1);
1588 entry->length += length;
1589 entry->info.count = langNum + 1;
1591 char *b, *be, *e, *ee, *t;
1594 /* Set beginning/end pointers to previous data */
1595 b = be = e = ee = entry->data;
1596 for (i = 0; i < table->info.count; i++) {
1599 ee += strlen(ee) + 1;
1604 /* Get storage for new buffer */
1606 sn = strlen(string) + 1;
1608 length = bn + sn + en;
1609 t = buf = xmalloc(length);
1611 /* Copy values into new storage */
1614 memcpy(t, string, sn);
1619 /* Replace i18N string array */
1620 entry->length -= strlen(be) + 1;
1621 entry->length += sn;
1623 if (ENTRY_IN_REGION(entry)) {
1624 entry->info.offset = 0;
1626 entry->data = _free(entry->data);
1633 int headerMod(Header h, rpmtd td)
1640 /* First find the tag */
1641 entry = findEntry(h, td->tag, td->type);
1646 data = grabData(td->type, td->data, td->count, &length);
1647 if (data == NULL || length <= 0)
1650 /* make sure entry points to the first occurence of this tag */
1651 while (entry > h->index && (entry - 1)->info.tag == td->tag)
1654 /* free after we've grabbed the new data in case the two are intertwined;
1655 that's a bad idea but at least we won't break */
1656 oldData = entry->data;
1658 entry->info.count = td->count;
1659 entry->info.type = td->type;
1661 entry->length = length;
1663 if (ENTRY_IN_REGION(entry)) {
1664 entry->info.offset = 0;
1672 * Header tag iterator data structure.
1674 struct headerIterator_s {
1675 Header h; /*!< Header being iterated. */
1676 int next_index; /*!< Next tag index. */
1679 HeaderIterator headerFreeIterator(HeaderIterator hi)
1682 hi->h = headerFree(hi->h);
1688 HeaderIterator headerInitIterator(Header h)
1690 HeaderIterator hi = xmalloc(sizeof(*hi));
1694 hi->h = headerLink(h);
1699 static indexEntry nextIndex(HeaderIterator hi)
1703 indexEntry entry = NULL;
1705 for (slot = hi->next_index; slot < h->indexUsed; slot++) {
1706 entry = h->index + slot;
1707 if (!ENTRY_IS_REGION(entry))
1710 hi->next_index = slot;
1711 if (entry == NULL || slot >= h->indexUsed)
1718 rpmTagVal headerNextTag(HeaderIterator hi)
1720 indexEntry entry = nextIndex(hi);
1721 return entry ? entry->info.tag : RPMTAG_NOT_FOUND;
1724 int headerNext(HeaderIterator hi, rpmtd td)
1726 indexEntry entry = nextIndex(hi);
1731 td->tag = entry->info.tag;
1732 rc = copyTdEntry(entry, td, HEADERGET_DEFAULT);
1734 return ((rc == 1) ? 1 : 0);
1737 unsigned int headerGetInstance(Header h)
1739 return h ? h->instance : 0;
1742 void headerSetInstance(Header h, unsigned int instance)
1744 h->instance = instance;