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"
23 const unsigned char rpm_header_magic[8] = {
24 0x8e, 0xad, 0xe8, 0x01, 0x00, 0x00, 0x00, 0x00
28 * Alignment needed for header data types.
30 static const int typeAlign[16] = {
31 1, /*!< RPM_NULL_TYPE */
32 1, /*!< RPM_CHAR_TYPE */
33 1, /*!< RPM_INT8_TYPE */
34 2, /*!< RPM_INT16_TYPE */
35 4, /*!< RPM_INT32_TYPE */
36 8, /*!< RPM_INT64_TYPE */
37 1, /*!< RPM_STRING_TYPE */
38 1, /*!< RPM_BIN_TYPE */
39 1, /*!< RPM_STRING_ARRAY_TYPE */
40 1, /*!< RPM_I18NSTRING_TYPE */
50 * Size of header data types.
52 static const int typeSizes[16] = {
53 0, /*!< RPM_NULL_TYPE */
54 1, /*!< RPM_CHAR_TYPE */
55 1, /*!< RPM_INT8_TYPE */
56 2, /*!< RPM_INT16_TYPE */
57 4, /*!< RPM_INT32_TYPE */
58 8, /*!< RPM_INT64_TYPE */
59 -1, /*!< RPM_STRING_TYPE */
60 1, /*!< RPM_BIN_TYPE */
61 -1, /*!< RPM_STRING_ARRAY_TYPE */
62 -1, /*!< RPM_I18NSTRING_TYPE */
71 typedef enum headerFlags_e {
72 HEADERFLAG_SORTED = (1 << 0), /*!< Are header entries sorted? */
73 HEADERFLAG_ALLOCATED = (1 << 1), /*!< Is 1st header region allocated? */
74 HEADERFLAG_LEGACY = (1 << 2), /*!< Header came from legacy source? */
75 HEADERFLAG_DEBUG = (1 << 3), /*!< Debug this header? */
79 * The Header data structure.
81 struct headerToken_s {
82 void * blob; /*!< Header region blob. */
83 indexEntry index; /*!< Array of tags. */
84 int indexUsed; /*!< Current size of tag array. */
85 int indexAlloced; /*!< Allocated size of tag array. */
86 unsigned int instance; /*!< Rpmdb instance (offset) */
88 int nrefs; /*!< Reference count. */
92 * Maximum no. of bytes permitted in a header.
94 static const size_t headerMaxbytes = (32*1024*1024);
96 #define INDEX_MALLOC_SIZE 8
98 #define ENTRY_IS_REGION(_e) \
99 (((_e)->info.tag >= HEADER_IMAGE) && ((_e)->info.tag < HEADER_REGIONS))
100 #define ENTRY_IN_REGION(_e) ((_e)->info.offset < 0)
103 * HEADER_EXT_TAG format function prototype.
104 * This is allowed to fail, which indicates the tag doesn't exist.
107 * @retval td tag data container
108 * @param flags modifier flags
109 * @return 0 on success
111 typedef int (*headerTagTagFunction) (Header h, rpmtd td, headerGetFlags hgflags);
113 extern void *rpmHeaderTagFunc(rpmTag tag);
115 /* Convert a 64bit value to network byte order. */
116 static uint64_t htonll( uint64_t n ) {
117 uint32_t *i = (uint32_t*)&n;
124 Header headerLink(Header h)
126 if (h == NULL) return NULL;
130 fprintf(stderr, "--> h %p ++ %d at %s:%u\n", h, h->nrefs, __FILE__, __LINE__);
135 Header headerUnlink(Header h)
137 if (h == NULL) return NULL;
139 fprintf(stderr, "--> h %p -- %d at %s:%u\n", h, h->nrefs, __FILE__, __LINE__);
144 Header headerFree(Header h)
146 (void) headerUnlink(h);
148 if (h == NULL || h->nrefs > 0)
149 return NULL; /* XXX return previous header? */
152 indexEntry entry = h->index;
154 for (i = 0; i < h->indexUsed; i++, entry++) {
155 if ((h->flags & HEADERFLAG_ALLOCATED) && ENTRY_IS_REGION(entry)) {
156 if (entry->length > 0) {
157 int32_t * ei = entry->data;
158 if ((ei - 2) == h->blob) h->blob = _free(h->blob);
161 } else if (!ENTRY_IN_REGION(entry)) {
162 entry->data = _free(entry->data);
166 h->index = _free(h->index);
173 Header headerNew(void)
175 Header h = xcalloc(1, sizeof(*h));
178 h->indexAlloced = INDEX_MALLOC_SIZE;
181 h->flags |= HEADERFLAG_SORTED;
183 h->index = (h->indexAlloced
184 ? xcalloc(h->indexAlloced, sizeof(*h->index))
188 return headerLink(h);
191 int headerVerifyInfo(int il, int dl, const void * pev, void * iv, int negate)
193 entryInfo pe = (entryInfo) pev;
197 for (i = 0; i < il; i++) {
198 info->tag = ntohl(pe[i].tag);
199 info->type = ntohl(pe[i].type);
200 info->offset = ntohl(pe[i].offset);
202 info->offset = -info->offset;
203 info->count = ntohl(pe[i].count);
205 if (hdrchkType(info->type))
207 if (hdrchkAlign(info->type, info->offset))
209 if (!negate && hdrchkRange(dl, info->offset))
211 if (hdrchkData(info->count))
220 static int indexCmp(const void * avp, const void * bvp)
222 indexEntry ap = (indexEntry) avp, bp = (indexEntry) bvp;
223 return (ap->info.tag - bp->info.tag);
226 void headerSort(Header h)
228 if (!(h->flags & HEADERFLAG_SORTED)) {
229 qsort(h->index, h->indexUsed, sizeof(*h->index), indexCmp);
230 h->flags |= HEADERFLAG_SORTED;
236 static int offsetCmp(const void * avp, const void * bvp)
238 indexEntry ap = (indexEntry) avp, bp = (indexEntry) bvp;
239 int rc = (ap->info.offset - bp->info.offset);
242 /* Within a region, entries sort by address. Added drips sort by tag. */
243 if (ap->info.offset < 0)
244 rc = (((char *)ap->data) - ((char *)bp->data));
246 rc = (ap->info.tag - bp->info.tag);
252 * Restore tags in header to original ordering.
255 void headerUnsort(Header h)
257 qsort(h->index, h->indexUsed, sizeof(*h->index), offsetCmp);
260 unsigned headerSizeof(Header h, enum hMagic magicp)
263 unsigned int size = 0;
272 case HEADER_MAGIC_YES:
273 size += sizeof(rpm_header_magic);
275 case HEADER_MAGIC_NO:
279 size += 2 * sizeof(int32_t); /* count of index entries */
281 for (i = 0, entry = h->index; i < h->indexUsed; i++, entry++) {
284 /* Regions go in as is ... */
285 if (ENTRY_IS_REGION(entry)) {
286 size += entry->length;
287 /* XXX Legacy regions do not include the region tag and data. */
288 if (i == 0 && (h->flags & HEADERFLAG_LEGACY))
289 size += sizeof(struct entryInfo_s) + entry->info.count;
293 /* ... and region elements are skipped. */
294 if (entry->info.offset < 0)
298 type = entry->info.type;
299 if (typeSizes[type] > 1) {
300 unsigned diff = typeSizes[type] - (size % typeSizes[type]);
301 if (diff != typeSizes[type]) {
306 size += sizeof(struct entryInfo_s) + entry->length;
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(rpmTagType type, rpm_constdata_t p, rpm_count_t count,
322 int onDisk, rpm_constdata_t pend)
324 const unsigned char * s = p;
325 const unsigned char * se = pend;
329 case RPM_STRING_TYPE:
337 length++; /* count nul terminator too. */
340 case RPM_STRING_ARRAY_TYPE:
341 case RPM_I18NSTRING_TYPE:
342 /* These are like RPM_STRING_TYPE, except they're *always* an array */
343 /* Compute sum of length of all strings, including nul terminators */
347 length++; /* count nul terminator too */
355 const char ** av = (const char **)p;
357 /* add one for null termination */
358 length += strlen(*av++) + 1;
364 if (typeSizes[type] == -1)
366 length = typeSizes[(type & 0xf)] * count;
367 if (length < 0 || (se && (s + length) > se))
376 * Swap int32_t and int16_t arrays within header region.
378 * If a header region tag is in the set to be swabbed, as the data for a
379 * a header region is located after all other tag data.
381 * @param entry header entry
382 * @param il no. of entries
383 * @param dl start no. bytes of data
384 * @param pe header physical entry pointer (swapped)
385 * @param dataStart header data start
386 * @param dataEnd header data end
387 * @param regionid region offset
388 * @return no. bytes of data in region, -1 on error
390 static int regionSwab(indexEntry entry, int il, int dl,
392 unsigned char * dataStart,
393 const unsigned char * dataEnd,
396 for (; il > 0; il--, pe++) {
397 struct indexEntry_s ie;
400 ie.info.tag = ntohl(pe->tag);
401 ie.info.type = ntohl(pe->type);
402 ie.info.count = ntohl(pe->count);
403 ie.info.offset = ntohl(pe->offset);
405 if (hdrchkType(ie.info.type))
407 if (hdrchkData(ie.info.count))
409 if (hdrchkData(ie.info.offset))
411 if (hdrchkAlign(ie.info.type, ie.info.offset))
414 ie.data = dataStart + ie.info.offset;
415 if (dataEnd && (unsigned char *)ie.data >= dataEnd)
418 ie.length = dataLength(ie.info.type, ie.data, ie.info.count, 1, dataEnd);
419 if (ie.length < 0 || hdrchkData(ie.length))
425 ie.info.offset = regionid;
426 *entry = ie; /* structure assignment */
432 if (typeSizes[type] > 1) {
433 unsigned diff = typeSizes[type] - (dl % typeSizes[type]);
434 if (diff != typeSizes[type]) {
439 /* Perform endian conversions */
440 switch (ntohl(pe->type)) {
442 { uint64_t * it = ie.data;
443 for (; ie.info.count > 0; ie.info.count--, it += 1) {
444 if (dataEnd && ((unsigned char *)it) >= dataEnd)
450 { int32_t * it = ie.data;
451 for (; ie.info.count > 0; ie.info.count--, it += 1) {
452 if (dataEnd && ((unsigned char *)it) >= dataEnd)
458 { int16_t * it = ie.data;
459 for (; ie.info.count > 0; ie.info.count--, it += 1) {
460 if (dataEnd && ((unsigned char *)it) >= dataEnd)
476 * @retval *lengthPtr no. bytes in unloaded header blob
477 * @return unloaded header blob (NULL on error)
479 static void * doHeaderUnload(Header h,
492 int drlen, ndribbles;
494 if (h == NULL) return NULL;
496 /* Sort entries by (offset,tag). */
499 /* Compute (il,dl) for all tags, including those deleted in region. */
500 drlen = ndribbles = 0;
501 for (i = 0, entry = h->index; i < h->indexUsed; i++, entry++) {
502 if (ENTRY_IS_REGION(entry)) {
503 int32_t rdl = -entry->info.offset; /* negative offset */
504 int32_t ril = rdl/sizeof(*pe);
505 int rid = entry->info.offset;
508 dl += entry->rdlen + entry->info.count;
509 /* XXX Legacy regions do not include the region tag and data. */
510 if (i == 0 && (h->flags & HEADERFLAG_LEGACY))
513 /* Skip rest of entries in region, but account for dribbles. */
514 for (; i < h->indexUsed && entry->info.offset <= rid+1; i++, entry++) {
515 if (entry->info.offset <= rid)
519 type = entry->info.type;
520 if (typeSizes[type] > 1) {
521 unsigned diff = typeSizes[type] - (dl % typeSizes[type]);
522 if (diff != typeSizes[type]) {
530 drlen += entry->length;
538 /* Ignore deleted drips. */
539 if (entry->data == NULL || entry->length <= 0)
543 type = entry->info.type;
544 if (typeSizes[type] > 1) {
545 unsigned diff = typeSizes[type] - (dl % typeSizes[type]);
546 if (diff != typeSizes[type]) {
555 /* Sanity checks on header intro. */
556 if (hdrchkTags(il) || hdrchkData(dl))
559 len = sizeof(il) + sizeof(dl) + (il * sizeof(*pe)) + dl;
565 pe = (entryInfo) &ei[2];
566 dataStart = te = (char *) (pe + il);
568 for (i = 0, entry = h->index; i < h->indexUsed; i++, entry++) {
574 if (entry->data == NULL || entry->length <= 0)
577 t = (unsigned char*)te;
578 pe->tag = htonl(entry->info.tag);
579 pe->type = htonl(entry->info.type);
580 pe->count = htonl(entry->info.count);
582 if (ENTRY_IS_REGION(entry)) {
583 int32_t rdl = -entry->info.offset; /* negative offset */
584 int32_t ril = rdl/sizeof(*pe) + ndribbles;
585 int rid = entry->info.offset;
587 src = (char *)entry->data;
588 rdlen = entry->rdlen;
590 /* XXX Legacy regions do not include the region tag and data. */
591 if (i == 0 && (h->flags & HEADERFLAG_LEGACY)) {
594 memcpy(pe+1, src, rdl);
595 memcpy(te, src + rdl, rdlen);
598 pe->offset = htonl(te - dataStart);
601 stei[2] = htonl(-rdl-entry->info.count);
603 memcpy(te, stei, entry->info.count);
604 te += entry->info.count;
606 rdlen += entry->info.count;
608 count = regionSwab(NULL, ril, 0, pe, t, NULL, 0);
614 memcpy(pe+1, src + sizeof(*pe), ((ril-1) * sizeof(*pe)));
615 memcpy(te, src + (ril * sizeof(*pe)), rdlen+entry->info.count+drlen);
618 entryInfo se = (entryInfo)src;
619 int off = ntohl(se->offset);
620 pe->offset = (off) ? htonl(te - dataStart) : htonl(off);
622 te += entry->info.count + drlen;
624 count = regionSwab(NULL, ril, 0, pe, t, NULL, 0);
625 if (count != (rdlen + entry->info.count + drlen))
629 /* Skip rest of entries in region. */
630 while (i < h->indexUsed && entry->info.offset <= rid+1) {
640 /* Ignore deleted drips. */
641 if (entry->data == NULL || entry->length <= 0)
645 type = entry->info.type;
646 if (typeSizes[type] > 1) {
648 diff = typeSizes[type] - ((te - dataStart) % typeSizes[type]);
649 if (diff != typeSizes[type]) {
655 pe->offset = htonl(te - dataStart);
657 /* copy data w/ endian conversions */
658 switch (entry->info.type) {
660 count = entry->info.count;
663 *((uint64_t *)te) = htonll(*((uint64_t *)src));
664 te += sizeof(uint64_t);
665 src += sizeof(uint64_t);
670 count = entry->info.count;
673 *((int32_t *)te) = htonl(*((int32_t *)src));
674 te += sizeof(int32_t);
675 src += sizeof(int32_t);
680 count = entry->info.count;
683 *((int16_t *)te) = htons(*((int16_t *)src));
684 te += sizeof(int16_t);
685 src += sizeof(int16_t);
690 memcpy(te, entry->data, entry->length);
697 /* Insure that there are no memcpy underruns/overruns. */
698 if (((char *)pe) != dataStart)
700 if ((((char *)ei)+len) != te)
706 h->flags &= ~HEADERFLAG_SORTED;
716 void * headerUnload(Header h)
719 void * uh = doHeaderUnload(h, &length);
724 * Find matching (tag,type) entry in header.
726 * @param tag entry tag
727 * @param type entry type
728 * @return header entry
731 indexEntry findEntry(Header h, rpmTag tag, rpmTagType type)
734 struct indexEntry_s key;
736 if (h == NULL) return NULL;
737 if (!(h->flags & HEADERFLAG_SORTED)) headerSort(h);
741 entry = bsearch(&key, h->index, h->indexUsed, sizeof(*h->index), indexCmp);
745 if (type == RPM_NULL_TYPE)
749 while (entry->info.tag == tag && entry->info.type != type &&
750 entry > h->index) entry--;
752 if (entry->info.tag == tag && entry->info.type == type)
758 int headerDel(Header h, rpmTag tag)
760 indexEntry last = h->index + h->indexUsed;
761 indexEntry entry, first;
764 entry = findEntry(h, tag, RPM_NULL_TYPE);
765 if (!entry) return 1;
767 /* Make sure entry points to the first occurence of this tag. */
768 while (entry > h->index && (entry - 1)->info.tag == tag)
771 /* Free data for tags being removed. */
772 for (first = entry; first < last; first++) {
774 if (first->info.tag != tag)
779 if (ENTRY_IN_REGION(first))
784 ne = (first - entry);
789 memmove(entry, first, (ne * sizeof(*entry)));
795 Header headerLoad(void * uh)
797 int32_t * ei = (int32_t *) uh;
798 int32_t il = ntohl(ei[0]); /* index length */
799 int32_t dl = ntohl(ei[1]); /* data length */
800 size_t pvlen = sizeof(il) + sizeof(dl) +
801 (il * sizeof(struct entryInfo_s)) + dl;
805 unsigned char * dataStart;
806 unsigned char * dataEnd;
810 /* Sanity checks on header intro. */
811 if (hdrchkTags(il) || hdrchkData(dl))
815 pe = (entryInfo) &ei[2];
816 dataStart = (unsigned char *) (pe + il);
817 dataEnd = dataStart + dl;
819 h = xcalloc(1, sizeof(*h));
821 h->indexAlloced = il + 1;
824 h->index = xcalloc(h->indexAlloced, sizeof(*h->index));
825 h->flags |= HEADERFLAG_SORTED;
830 if (!(htonl(pe->tag) < HEADER_I18NTABLE)) {
831 h->flags |= HEADERFLAG_LEGACY;
832 entry->info.type = REGION_TAG_TYPE;
833 entry->info.tag = HEADER_IMAGE;
834 entry->info.count = REGION_TAG_COUNT;
835 entry->info.offset = ((unsigned char *)pe - dataStart); /* negative offset */
838 entry->length = pvlen - sizeof(il) - sizeof(dl);
839 rdlen = regionSwab(entry+1, il, 0, pe, dataStart, dataEnd, entry->info.offset);
842 entry->rdlen = rdlen;
848 h->flags &= ~HEADERFLAG_LEGACY;
850 entry->info.type = htonl(pe->type);
851 entry->info.count = htonl(pe->count);
853 if (hdrchkType(entry->info.type))
855 if (hdrchkTags(entry->info.count))
858 { int off = ntohl(pe->offset);
863 size_t nb = REGION_TAG_COUNT;
865 /* XXX Hmm, why the copy? */
866 memcpy(&stei, dataStart + off, nb);
867 rdl = -ntohl(stei[2]); /* negative offset */
868 ril = rdl/sizeof(*pe);
869 if (hdrchkTags(ril) || hdrchkData(rdl))
871 entry->info.tag = htonl(pe->tag);
874 rdl = (ril * sizeof(struct entryInfo_s));
875 entry->info.tag = HEADER_IMAGE;
878 entry->info.offset = -rdl; /* negative offset */
881 entry->length = pvlen - sizeof(il) - sizeof(dl);
882 rdlen = regionSwab(entry+1, ril-1, 0, pe+1, dataStart, dataEnd, entry->info.offset);
885 entry->rdlen = rdlen;
887 if (ril < h->indexUsed) {
888 indexEntry newEntry = entry + ril;
889 int ne = (h->indexUsed - ril);
890 int rid = entry->info.offset+1;
893 /* Load dribble entries from region. */
894 rc = regionSwab(newEntry, ne, 0, pe+ril, dataStart, dataEnd, rid);
899 { indexEntry firstEntry = newEntry;
900 int save = h->indexUsed;
903 /* Dribble entries replace duplicate region entries. */
905 for (j = 0; j < ne; j++, newEntry++) {
906 (void) headerDel(h, newEntry->info.tag);
907 if (newEntry->info.tag == RPMTAG_BASENAMES)
908 (void) headerDel(h, RPMTAG_OLDFILENAMES);
911 /* If any duplicate entries were replaced, move new entries down. */
912 if (h->indexUsed < (save - ne)) {
913 memmove(h->index + h->indexUsed, firstEntry,
914 (ne * sizeof(*entry)));
921 h->flags &= ~HEADERFLAG_SORTED;
923 h->flags |= HEADERFLAG_ALLOCATED;
929 h->index = _free(h->index);
935 Header headerReload(Header h, rpmTag tag)
939 void * uh = doHeaderUnload(h, &length);
949 if (ENTRY_IS_REGION(nh->index)) {
950 if (tag == HEADER_SIGNATURES || tag == HEADER_IMMUTABLE)
951 nh->index[0].info.tag = tag;
956 Header headerCopyLoad(const void * uh)
958 int32_t * ei = (int32_t *) uh;
959 int32_t il = ntohl(ei[0]); /* index length */
960 int32_t dl = ntohl(ei[1]); /* data length */
961 size_t pvlen = sizeof(il) + sizeof(dl) +
962 (il * sizeof(struct entryInfo_s)) + dl;
966 /* Sanity checks on header intro. */
967 if (!(hdrchkTags(il) || hdrchkData(dl)) && pvlen < headerMaxbytes) {
968 nuh = memcpy(xmalloc(pvlen), uh, pvlen);
969 if ((h = headerLoad(nuh)) == NULL)
976 * Read (and load) header from file handle.
977 * @param fd file handle
978 * @param magicp read (and verify) 8 bytes of (magic, 0)?
979 * @return header (or NULL on error)
981 Header headerRead(FD_t fd, enum hMagic magicp)
993 memset(block, 0, sizeof(block));
995 if (magicp == HEADER_MAGIC_YES)
999 if (timedRead(fd, (char *)block, i*sizeof(*block)) != (i * sizeof(*block)))
1004 if (magicp == HEADER_MAGIC_YES) {
1006 if (memcmp(&magic, rpm_header_magic, sizeof(magic)))
1008 reserved = block[i++];
1011 il = ntohl(block[i]); i++;
1012 dl = ntohl(block[i]); i++;
1014 len = sizeof(il) + sizeof(dl) + (il * sizeof(struct entryInfo_s)) + dl;
1016 /* Sanity checks on header intro. */
1017 if (hdrchkTags(il) || hdrchkData(dl) || len > headerMaxbytes)
1023 len -= sizeof(il) + sizeof(dl);
1026 if (timedRead(fd, (char *)&ei[2], len) != len)
1032 if (h == NULL && ei != NULL) {
1038 int headerWrite(FD_t fd, Header h, enum hMagic magicp)
1044 uh = doHeaderUnload(h, &length);
1048 case HEADER_MAGIC_YES:
1049 nb = Fwrite(rpm_header_magic, sizeof(uint8_t), sizeof(rpm_header_magic), fd);
1050 if (nb != sizeof(rpm_header_magic))
1053 case HEADER_MAGIC_NO:
1057 nb = Fwrite(uh, sizeof(char), length, fd);
1061 return (nb == length ? 0 : 1);
1064 int headerIsEntry(Header h, rpmTag tag)
1066 /* FIX: h modified by sort. */
1067 return (findEntry(h, tag, RPM_NULL_TYPE) ? 1 : 0);
1072 * Retrieve data from header entry.
1073 * Relevant flags (others are ignored), if neither is set allocation
1074 * behavior depends on data type(!)
1075 * HEADERGET_MINMEM: return pointers to header memory
1076 * HEADERGET_ALLOC: always return malloced memory, overrides MINMEM
1078 * @todo Permit retrieval of regions other than HEADER_IMUTABLE.
1079 * @param entry header entry
1080 * @param td tag data container
1081 * @param minMem string pointers refer to header memory?
1082 * @param flags flags to control memory allocation
1083 * @return 1 on success, otherwise error.
1085 static int copyTdEntry(const indexEntry entry, rpmtd td, headerGetFlags flags)
1087 rpm_count_t count = entry->info.count;
1088 int rc = 1; /* XXX 1 on success. */
1089 /* ALLOC overrides MINMEM */
1090 int allocMem = flags & HEADERGET_ALLOC;
1091 int minMem = allocMem ? 0 : flags & HEADERGET_MINMEM;
1092 int argvArray = (flags & HEADERGET_ARGV) ? 1 : 0;
1095 td->flags = RPMTD_IMMUTABLE;
1096 switch (entry->info.type) {
1099 * XXX This only works for
1100 * XXX "sealed" HEADER_IMMUTABLE/HEADER_SIGNATURES/HEADER_IMAGE.
1101 * XXX This will *not* work for unsealed legacy HEADER_IMAGE (i.e.
1102 * XXX a legacy header freshly read, but not yet unloaded to the rpmdb).
1104 if (ENTRY_IS_REGION(entry)) {
1105 int32_t * ei = ((int32_t *)entry->data) - 2;
1106 entryInfo pe = (entryInfo) (ei + 2);
1107 unsigned char * dataStart = (unsigned char *) (pe + ntohl(ei[0]));
1108 int32_t rdl = -entry->info.offset; /* negative offset */
1109 int32_t ril = rdl/sizeof(*pe);
1112 count = 2 * sizeof(*ei) + (ril * sizeof(*pe)) + rdl;
1113 if (entry->info.tag == HEADER_IMAGE) {
1117 count += REGION_TAG_COUNT;
1118 rdl += REGION_TAG_COUNT;
1121 td->data = xmalloc(count);
1122 ei = (int32_t *) td->data;
1126 pe = (entryInfo) memcpy(ei + 2, pe, (ril * sizeof(*pe)));
1128 dataStart = (unsigned char *) memcpy(pe + ril, dataStart, rdl);
1130 rc = regionSwab(NULL, ril, 0, pe, dataStart, dataStart + rdl, 0);
1131 /* don't return data on failure */
1133 td->data = _free(td->data);
1135 /* XXX 1 on success. */
1136 rc = (rc < 0) ? 0 : 1;
1138 count = entry->length;
1140 ? memcpy(xmalloc(count), entry->data, count)
1144 case RPM_STRING_TYPE:
1145 /* simple string, but fallthrough if its actually an array */
1146 if (count == 1 && !argvArray) {
1147 td->data = allocMem ? xstrdup(entry->data) : entry->data;
1150 case RPM_STRING_ARRAY_TYPE:
1151 case RPM_I18NSTRING_TYPE:
1152 { const char ** ptrEntry;
1153 int tableSize = (count + argvArray) * sizeof(char *);
1158 td->data = xmalloc(tableSize);
1159 ptrEntry = (const char **) td->data;
1162 t = xmalloc(tableSize + entry->length);
1163 td->data = (void *)t;
1164 ptrEntry = (const char **) td->data;
1166 memcpy(t, entry->data, entry->length);
1168 for (i = 0; i < count; i++) {
1175 td->flags |= RPMTD_ARGV;
1180 case RPM_INT16_TYPE:
1181 case RPM_INT32_TYPE:
1182 case RPM_INT64_TYPE:
1184 td->data = xmalloc(entry->length);
1185 memcpy(td->data, entry->data, entry->length);
1187 td->data = entry->data;
1191 /* WTH? Don't mess with unknown data types... */
1196 td->type = entry->info.type;
1199 if (td->data && entry->data != td->data) {
1200 td->flags |= RPMTD_ALLOCED;
1207 * Does locale match entry in header i18n table?
1210 * The range [l,le) contains the next locale to match:
1211 * ll[_CC][.EEEEE][@dddd]
1213 * ll ISO language code (in lowercase).
1214 * CC (optional) ISO coutnry code (in uppercase).
1215 * EEEEE (optional) encoding (not really standardized).
1216 * dddd (optional) dialect.
1219 * @param td header i18n table data, NUL terminated
1220 * @param l start of locale to match
1221 * @param le end of locale to match
1222 * @return 1 on good match, 2 on weak match, 0 on no match
1224 static int headerMatchLocale(const char *td, const char *l, const char *le)
1228 /* First try a complete match. */
1229 if (strlen(td) == (le-l) && rstreqn(td, l, (le - l)))
1232 /* Next, try stripping optional dialect and matching. */
1233 for (fe = l; fe < le && *fe != '@'; fe++)
1235 if (fe < le && rstreqn(td, l, (fe - l)))
1238 /* Next, try stripping optional codeset and matching. */
1239 for (fe = l; fe < le && *fe != '.'; fe++)
1241 if (fe < le && rstreqn(td, l, (fe - l)))
1244 /* Finally, try stripping optional country code and matching. */
1245 for (fe = l; fe < le && *fe != '_'; fe++)
1247 if (fe < le && rstreqn(td, l, (fe - l)))
1254 * Return i18n string from header that matches locale.
1256 * @param entry i18n string data
1257 * @retval td tag data container
1258 * @param flags flags to control allocation
1261 static int copyI18NEntry(Header h, indexEntry entry, rpmtd td,
1262 headerGetFlags flags)
1264 const char *lang, *l, *le;
1267 td->type = RPM_STRING_TYPE;
1269 /* if no match, just return the first string */
1270 td->data = entry->data;
1272 /* XXX Drepper sez' this is the order. */
1273 if ((lang = getenv("LANGUAGE")) == NULL &&
1274 (lang = getenv("LC_ALL")) == NULL &&
1275 (lang = getenv("LC_MESSAGES")) == NULL &&
1276 (lang = getenv("LANG")) == NULL)
1279 if ((table = findEntry(h, HEADER_I18NTABLE, RPM_STRING_ARRAY_TYPE)) == NULL)
1282 for (l = lang; *l != '\0'; l = le) {
1284 char *ed, *ed_weak = NULL;
1287 while (*l && *l == ':') /* skip leading colons */
1291 for (le = l; *le && *le != ':'; le++) /* find end of this locale */
1294 /* For each entry in the header ... */
1295 for (langNum = 0, t = table->data, ed = entry->data;
1296 langNum < entry->info.count;
1297 langNum++, t += strlen(t) + 1, ed += strlen(ed) + 1) {
1299 int match = headerMatchLocale(t, l, le);
1303 } else if (match == 2) {
1314 if (flags & HEADERGET_ALLOC) {
1315 td->data = xstrdup(td->data);
1316 td->flags |= RPMTD_ALLOCED;
1323 * Retrieve tag data from header.
1325 * @retval td tag data container
1326 * @param flags flags to control retrieval
1327 * @return 1 on success, 0 on not found
1329 static int intGetTdEntry(Header h, rpmtd td, headerGetFlags flags)
1334 /* First find the tag */
1335 /* FIX: h modified by sort. */
1336 entry = findEntry(h, td->tag, RPM_NULL_TYPE);
1337 if (entry == NULL) {
1338 /* Td is zeroed above, just return... */
1342 if (flags & HEADERGET_RAW) {
1343 rc = copyTdEntry(entry, td, flags);
1345 switch (entry->info.type) {
1346 case RPM_I18NSTRING_TYPE:
1347 rc = copyI18NEntry(h, entry, td, flags);
1350 rc = copyTdEntry(entry, td, flags);
1355 /* XXX 1 on success */
1356 return ((rc == 1) ? 1 : 0);
1359 int headerGet(Header h, rpmTag tag, rpmtd td, headerGetFlags flags)
1362 headerTagTagFunction tagfunc = intGetTdEntry;
1364 if (td == NULL) return 0;
1369 if (flags & HEADERGET_EXT) {
1370 headerTagTagFunction extfunc = rpmHeaderTagFunc(tag);
1371 if (extfunc) tagfunc = extfunc;
1373 rc = tagfunc(h, td, flags);
1375 assert(tag == td->tag);
1381 static void copyData(rpmTagType type, rpm_data_t dstPtr,
1382 rpm_constdata_t srcPtr, rpm_count_t cnt, int dataLength)
1385 case RPM_STRING_ARRAY_TYPE:
1386 case RPM_I18NSTRING_TYPE:
1387 { const char ** av = (const char **) srcPtr;
1390 while (cnt-- > 0 && dataLength > 0) {
1392 if ((s = *av++) == NULL)
1396 } while (s[-1] && --dataLength > 0);
1401 memmove(dstPtr, srcPtr, dataLength);
1407 * Return (malloc'ed) copy of entry data.
1408 * @param type entry data type
1409 * @param p entry data
1410 * @param c entry item count
1411 * @retval lengthPtr no. bytes in returned data
1412 * @return (malloc'ed) copy of entry data, NULL on error
1415 grabData(rpmTagType type, rpm_constdata_t p, rpm_count_t c, int * lengthPtr)
1417 rpm_data_t data = NULL;
1420 length = dataLength(type, p, c, 0, NULL);
1422 data = xmalloc(length);
1423 copyData(type, data, p, c, length);
1427 *lengthPtr = length;
1431 static int intAddEntry(Header h, rpmtd td)
1437 /* Count must always be >= 1 for headerAddEntry. */
1441 if (hdrchkType(td->type))
1443 if (hdrchkData(td->count))
1447 data = grabData(td->type, td->data, td->count, &length);
1448 if (data == NULL || length <= 0)
1451 /* Allocate more index space if necessary */
1452 if (h->indexUsed == h->indexAlloced) {
1453 h->indexAlloced += INDEX_MALLOC_SIZE;
1454 h->index = xrealloc(h->index, h->indexAlloced * sizeof(*h->index));
1457 /* Fill in the index */
1458 entry = h->index + h->indexUsed;
1459 entry->info.tag = td->tag;
1460 entry->info.type = td->type;
1461 entry->info.count = td->count;
1462 entry->info.offset = 0;
1464 entry->length = length;
1466 if (h->indexUsed > 0 && td->tag < h->index[h->indexUsed-1].info.tag)
1467 h->flags &= ~HEADERFLAG_SORTED;
1473 static int intAppendEntry(Header h, rpmtd td)
1478 if (td->type == RPM_STRING_TYPE || td->type == RPM_I18NSTRING_TYPE) {
1479 /* we can't do this */
1483 /* Find the tag entry in the header. */
1484 entry = findEntry(h, td->tag, td->type);
1488 length = dataLength(td->type, td->data, td->count, 0, NULL);
1492 if (ENTRY_IN_REGION(entry)) {
1493 char * t = xmalloc(entry->length + length);
1494 memcpy(t, entry->data, entry->length);
1496 entry->info.offset = 0;
1498 entry->data = xrealloc(entry->data, entry->length + length);
1500 copyData(td->type, ((char *) entry->data) + entry->length,
1501 td->data, td->count, length);
1503 entry->length += length;
1505 entry->info.count += td->count;
1510 int headerPut(Header h, rpmtd td, headerPutFlags flags)
1515 if (flags & HEADERPUT_APPEND) {
1516 rc = findEntry(h, td->tag, td->type) ?
1517 intAppendEntry(h, td) :
1520 rc = intAddEntry(h, td);
1525 int headerAddI18NString(Header h, rpmTag tag, const char * string,
1528 indexEntry table, entry;
1529 const char ** strArray;
1532 rpm_count_t i, langNum;
1535 table = findEntry(h, HEADER_I18NTABLE, RPM_STRING_ARRAY_TYPE);
1536 entry = findEntry(h, tag, RPM_I18NSTRING_TYPE);
1538 if (!table && entry)
1539 return 0; /* this shouldn't ever happen!! */
1541 if (!table && !entry) {
1542 const char * charArray[2];
1543 rpm_count_t count = 0;
1545 if (!lang || (lang[0] == 'C' && lang[1] == '\0')) {
1546 charArray[count++] = "C";
1548 charArray[count++] = "C";
1549 charArray[count++] = lang;
1553 td.tag = HEADER_I18NTABLE;
1554 td.type = RPM_STRING_ARRAY_TYPE;
1555 td.data = (void *) charArray;
1557 if (!headerPut(h, &td, HEADERPUT_DEFAULT))
1559 table = findEntry(h, HEADER_I18NTABLE, RPM_STRING_ARRAY_TYPE);
1564 if (!lang) lang = "C";
1566 { const char * l = table->data;
1567 for (langNum = 0; langNum < table->info.count; langNum++) {
1568 if (rstreq(l, lang)) break;
1573 if (langNum >= table->info.count) {
1574 length = strlen(lang) + 1;
1575 if (ENTRY_IN_REGION(table)) {
1576 char * t = xmalloc(table->length + length);
1577 memcpy(t, table->data, table->length);
1579 table->info.offset = 0;
1581 table->data = xrealloc(table->data, table->length + length);
1582 memmove(((char *)table->data) + table->length, lang, length);
1583 table->length += length;
1584 table->info.count++;
1590 strArray = xmalloc(sizeof(*strArray) * (langNum + 1));
1591 for (i = 0; i < langNum; i++)
1593 strArray[langNum] = string;
1597 td.type = RPM_I18NSTRING_TYPE;
1599 td.count = langNum + 1;
1600 rc = headerPut(h, &td, HEADERPUT_DEFAULT);
1603 } else if (langNum >= entry->info.count) {
1604 ghosts = langNum - entry->info.count;
1606 length = strlen(string) + 1 + ghosts;
1607 if (ENTRY_IN_REGION(entry)) {
1608 char * t = xmalloc(entry->length + length);
1609 memcpy(t, entry->data, entry->length);
1611 entry->info.offset = 0;
1613 entry->data = xrealloc(entry->data, entry->length + length);
1615 memset(((char *)entry->data) + entry->length, '\0', ghosts);
1616 memmove(((char *)entry->data) + entry->length + ghosts, string, strlen(string)+1);
1618 entry->length += length;
1619 entry->info.count = langNum + 1;
1621 char *b, *be, *e, *ee, *t;
1624 /* Set beginning/end pointers to previous data */
1625 b = be = e = ee = entry->data;
1626 for (i = 0; i < table->info.count; i++) {
1629 ee += strlen(ee) + 1;
1634 /* Get storage for new buffer */
1636 sn = strlen(string) + 1;
1638 length = bn + sn + en;
1639 t = buf = xmalloc(length);
1641 /* Copy values into new storage */
1644 memcpy(t, string, sn);
1649 /* Replace i18N string array */
1650 entry->length -= strlen(be) + 1;
1651 entry->length += sn;
1653 if (ENTRY_IN_REGION(entry)) {
1654 entry->info.offset = 0;
1656 entry->data = _free(entry->data);
1663 int headerMod(Header h, rpmtd td)
1670 /* First find the tag */
1671 entry = findEntry(h, td->tag, td->type);
1676 data = grabData(td->type, td->data, td->count, &length);
1677 if (data == NULL || length <= 0)
1680 /* make sure entry points to the first occurence of this tag */
1681 while (entry > h->index && (entry - 1)->info.tag == td->tag)
1684 /* free after we've grabbed the new data in case the two are intertwined;
1685 that's a bad idea but at least we won't break */
1686 oldData = entry->data;
1688 entry->info.count = td->count;
1689 entry->info.type = td->type;
1691 entry->length = length;
1693 if (ENTRY_IN_REGION(entry)) {
1694 entry->info.offset = 0;
1696 oldData = _free(oldData);
1702 * Header tag iterator data structure.
1704 struct headerIterator_s {
1705 Header h; /*!< Header being iterated. */
1706 int next_index; /*!< Next tag index. */
1709 HeaderIterator headerFreeIterator(HeaderIterator hi)
1712 hi->h = headerFree(hi->h);
1718 HeaderIterator headerInitIterator(Header h)
1720 HeaderIterator hi = xmalloc(sizeof(*hi));
1724 hi->h = headerLink(h);
1729 static indexEntry nextIndex(HeaderIterator hi)
1733 indexEntry entry = NULL;
1735 for (slot = hi->next_index; slot < h->indexUsed; slot++) {
1736 entry = h->index + slot;
1737 if (!ENTRY_IS_REGION(entry))
1740 hi->next_index = slot;
1741 if (entry == NULL || slot >= h->indexUsed)
1748 rpmTag headerNextTag(HeaderIterator hi)
1750 indexEntry entry = nextIndex(hi);
1751 return entry ? entry->info.tag : RPMTAG_NOT_FOUND;
1754 int headerNext(HeaderIterator hi, rpmtd td)
1756 indexEntry entry = nextIndex(hi);
1761 td->tag = entry->info.tag;
1762 rc = copyTdEntry(entry, td, HEADERGET_DEFAULT);
1764 return ((rc == 1) ? 1 : 0);
1767 unsigned int headerGetInstance(Header h)
1769 return h ? h->instance : 0;
1772 void headerSetInstance(Header h, unsigned int instance)
1774 h->instance = instance;