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:
1146 td->data = allocMem ? xstrdup(entry->data) : entry->data;
1149 case RPM_STRING_ARRAY_TYPE:
1150 case RPM_I18NSTRING_TYPE:
1151 { const char ** ptrEntry;
1152 int tableSize = (count + argvArray) * sizeof(char *);
1157 td->data = xmalloc(tableSize);
1158 ptrEntry = (const char **) td->data;
1161 t = xmalloc(tableSize + entry->length);
1162 td->data = (void *)t;
1163 ptrEntry = (const char **) td->data;
1165 memcpy(t, entry->data, entry->length);
1167 for (i = 0; i < count; i++) {
1174 td->flags |= RPMTD_ARGV;
1179 case RPM_INT16_TYPE:
1180 case RPM_INT32_TYPE:
1181 case RPM_INT64_TYPE:
1183 td->data = xmalloc(entry->length);
1184 memcpy(td->data, entry->data, entry->length);
1186 td->data = entry->data;
1190 /* WTH? Don't mess with unknown data types... */
1195 td->type = entry->info.type;
1198 if (td->data && entry->data != td->data) {
1199 td->flags |= RPMTD_ALLOCED;
1206 * Does locale match entry in header i18n table?
1209 * The range [l,le) contains the next locale to match:
1210 * ll[_CC][.EEEEE][@dddd]
1212 * ll ISO language code (in lowercase).
1213 * CC (optional) ISO coutnry code (in uppercase).
1214 * EEEEE (optional) encoding (not really standardized).
1215 * dddd (optional) dialect.
1218 * @param td header i18n table data, NUL terminated
1219 * @param l start of locale to match
1220 * @param le end of locale to match
1221 * @return 1 on good match, 2 on weak match, 0 on no match
1223 static int headerMatchLocale(const char *td, const char *l, const char *le)
1227 /* First try a complete match. */
1228 if (strlen(td) == (le-l) && rstreqn(td, l, (le - l)))
1231 /* Next, try stripping optional dialect and matching. */
1232 for (fe = l; fe < le && *fe != '@'; fe++)
1234 if (fe < le && rstreqn(td, l, (fe - l)))
1237 /* Next, try stripping optional codeset and matching. */
1238 for (fe = l; fe < le && *fe != '.'; fe++)
1240 if (fe < le && rstreqn(td, l, (fe - l)))
1243 /* Finally, try stripping optional country code and matching. */
1244 for (fe = l; fe < le && *fe != '_'; fe++)
1246 if (fe < le && rstreqn(td, l, (fe - l)))
1253 * Return i18n string from header that matches locale.
1255 * @param entry i18n string data
1256 * @retval td tag data container
1257 * @param flags flags to control allocation
1260 static int copyI18NEntry(Header h, indexEntry entry, rpmtd td,
1261 headerGetFlags flags)
1263 const char *lang, *l, *le;
1266 td->type = RPM_STRING_TYPE;
1268 /* if no match, just return the first string */
1269 td->data = entry->data;
1271 /* XXX Drepper sez' this is the order. */
1272 if ((lang = getenv("LANGUAGE")) == NULL &&
1273 (lang = getenv("LC_ALL")) == NULL &&
1274 (lang = getenv("LC_MESSAGES")) == NULL &&
1275 (lang = getenv("LANG")) == NULL)
1278 if ((table = findEntry(h, HEADER_I18NTABLE, RPM_STRING_ARRAY_TYPE)) == NULL)
1281 for (l = lang; *l != '\0'; l = le) {
1283 char *ed, *ed_weak = NULL;
1286 while (*l && *l == ':') /* skip leading colons */
1290 for (le = l; *le && *le != ':'; le++) /* find end of this locale */
1293 /* For each entry in the header ... */
1294 for (langNum = 0, t = table->data, ed = entry->data;
1295 langNum < entry->info.count;
1296 langNum++, t += strlen(t) + 1, ed += strlen(ed) + 1) {
1298 int match = headerMatchLocale(t, l, le);
1302 } else if (match == 2) {
1313 if (flags & HEADERGET_ALLOC) {
1314 td->data = xstrdup(td->data);
1315 td->flags |= RPMTD_ALLOCED;
1322 * Retrieve tag data from header.
1324 * @retval td tag data container
1325 * @param flags flags to control retrieval
1326 * @return 1 on success, 0 on not found
1328 static int intGetTdEntry(Header h, rpmtd td, headerGetFlags flags)
1333 /* First find the tag */
1334 /* FIX: h modified by sort. */
1335 entry = findEntry(h, td->tag, RPM_NULL_TYPE);
1336 if (entry == NULL) {
1337 /* Td is zeroed above, just return... */
1341 if (flags & HEADERGET_RAW) {
1342 rc = copyTdEntry(entry, td, flags);
1344 switch (entry->info.type) {
1345 case RPM_I18NSTRING_TYPE:
1346 rc = copyI18NEntry(h, entry, td, flags);
1349 rc = copyTdEntry(entry, td, flags);
1354 /* XXX 1 on success */
1355 return ((rc == 1) ? 1 : 0);
1358 int headerGet(Header h, rpmTag tag, rpmtd td, headerGetFlags flags)
1361 headerTagTagFunction tagfunc = intGetTdEntry;
1363 if (td == NULL) return 0;
1368 if (flags & HEADERGET_EXT) {
1369 headerTagTagFunction extfunc = rpmHeaderTagFunc(tag);
1370 if (extfunc) tagfunc = extfunc;
1372 rc = tagfunc(h, td, flags);
1374 assert(tag == td->tag);
1380 static void copyData(rpmTagType type, rpm_data_t dstPtr,
1381 rpm_constdata_t srcPtr, rpm_count_t cnt, int dataLength)
1384 case RPM_STRING_ARRAY_TYPE:
1385 case RPM_I18NSTRING_TYPE:
1386 { const char ** av = (const char **) srcPtr;
1389 while (cnt-- > 0 && dataLength > 0) {
1391 if ((s = *av++) == NULL)
1395 } while (s[-1] && --dataLength > 0);
1400 memmove(dstPtr, srcPtr, dataLength);
1406 * Return (malloc'ed) copy of entry data.
1407 * @param type entry data type
1408 * @param p entry data
1409 * @param c entry item count
1410 * @retval lengthPtr no. bytes in returned data
1411 * @return (malloc'ed) copy of entry data, NULL on error
1414 grabData(rpmTagType type, rpm_constdata_t p, rpm_count_t c, int * lengthPtr)
1416 rpm_data_t data = NULL;
1419 length = dataLength(type, p, c, 0, NULL);
1421 data = xmalloc(length);
1422 copyData(type, data, p, c, length);
1426 *lengthPtr = length;
1430 static int intAddEntry(Header h, rpmtd td)
1436 /* Count must always be >= 1 for headerAddEntry. */
1440 if (hdrchkType(td->type))
1442 if (hdrchkData(td->count))
1446 data = grabData(td->type, td->data, td->count, &length);
1447 if (data == NULL || length <= 0)
1450 /* Allocate more index space if necessary */
1451 if (h->indexUsed == h->indexAlloced) {
1452 h->indexAlloced += INDEX_MALLOC_SIZE;
1453 h->index = xrealloc(h->index, h->indexAlloced * sizeof(*h->index));
1456 /* Fill in the index */
1457 entry = h->index + h->indexUsed;
1458 entry->info.tag = td->tag;
1459 entry->info.type = td->type;
1460 entry->info.count = td->count;
1461 entry->info.offset = 0;
1463 entry->length = length;
1465 if (h->indexUsed > 0 && td->tag < h->index[h->indexUsed-1].info.tag)
1466 h->flags &= ~HEADERFLAG_SORTED;
1472 static int intAppendEntry(Header h, rpmtd td)
1477 if (td->type == RPM_STRING_TYPE || td->type == RPM_I18NSTRING_TYPE) {
1478 /* we can't do this */
1482 /* Find the tag entry in the header. */
1483 entry = findEntry(h, td->tag, td->type);
1487 length = dataLength(td->type, td->data, td->count, 0, NULL);
1491 if (ENTRY_IN_REGION(entry)) {
1492 char * t = xmalloc(entry->length + length);
1493 memcpy(t, entry->data, entry->length);
1495 entry->info.offset = 0;
1497 entry->data = xrealloc(entry->data, entry->length + length);
1499 copyData(td->type, ((char *) entry->data) + entry->length,
1500 td->data, td->count, length);
1502 entry->length += length;
1504 entry->info.count += td->count;
1509 int headerPut(Header h, rpmtd td, headerPutFlags flags)
1514 if (flags & HEADERPUT_APPEND) {
1515 rc = findEntry(h, td->tag, td->type) ?
1516 intAppendEntry(h, td) :
1519 rc = intAddEntry(h, td);
1524 int headerAddI18NString(Header h, rpmTag tag, const char * string,
1527 indexEntry table, entry;
1528 const char ** strArray;
1531 rpm_count_t i, langNum;
1534 table = findEntry(h, HEADER_I18NTABLE, RPM_STRING_ARRAY_TYPE);
1535 entry = findEntry(h, tag, RPM_I18NSTRING_TYPE);
1537 if (!table && entry)
1538 return 0; /* this shouldn't ever happen!! */
1540 if (!table && !entry) {
1541 const char * charArray[2];
1542 rpm_count_t count = 0;
1544 if (!lang || (lang[0] == 'C' && lang[1] == '\0')) {
1545 charArray[count++] = "C";
1547 charArray[count++] = "C";
1548 charArray[count++] = lang;
1552 td.tag = HEADER_I18NTABLE;
1553 td.type = RPM_STRING_ARRAY_TYPE;
1554 td.data = (void *) charArray;
1556 if (!headerPut(h, &td, HEADERPUT_DEFAULT))
1558 table = findEntry(h, HEADER_I18NTABLE, RPM_STRING_ARRAY_TYPE);
1563 if (!lang) lang = "C";
1565 { const char * l = table->data;
1566 for (langNum = 0; langNum < table->info.count; langNum++) {
1567 if (rstreq(l, lang)) break;
1572 if (langNum >= table->info.count) {
1573 length = strlen(lang) + 1;
1574 if (ENTRY_IN_REGION(table)) {
1575 char * t = xmalloc(table->length + length);
1576 memcpy(t, table->data, table->length);
1578 table->info.offset = 0;
1580 table->data = xrealloc(table->data, table->length + length);
1581 memmove(((char *)table->data) + table->length, lang, length);
1582 table->length += length;
1583 table->info.count++;
1589 strArray = xmalloc(sizeof(*strArray) * (langNum + 1));
1590 for (i = 0; i < langNum; i++)
1592 strArray[langNum] = string;
1596 td.type = RPM_I18NSTRING_TYPE;
1598 td.count = langNum + 1;
1599 rc = headerPut(h, &td, HEADERPUT_DEFAULT);
1602 } else if (langNum >= entry->info.count) {
1603 ghosts = langNum - entry->info.count;
1605 length = strlen(string) + 1 + ghosts;
1606 if (ENTRY_IN_REGION(entry)) {
1607 char * t = xmalloc(entry->length + length);
1608 memcpy(t, entry->data, entry->length);
1610 entry->info.offset = 0;
1612 entry->data = xrealloc(entry->data, entry->length + length);
1614 memset(((char *)entry->data) + entry->length, '\0', ghosts);
1615 memmove(((char *)entry->data) + entry->length + ghosts, string, strlen(string)+1);
1617 entry->length += length;
1618 entry->info.count = langNum + 1;
1620 char *b, *be, *e, *ee, *t;
1623 /* Set beginning/end pointers to previous data */
1624 b = be = e = ee = entry->data;
1625 for (i = 0; i < table->info.count; i++) {
1628 ee += strlen(ee) + 1;
1633 /* Get storage for new buffer */
1635 sn = strlen(string) + 1;
1637 length = bn + sn + en;
1638 t = buf = xmalloc(length);
1640 /* Copy values into new storage */
1643 memcpy(t, string, sn);
1648 /* Replace i18N string array */
1649 entry->length -= strlen(be) + 1;
1650 entry->length += sn;
1652 if (ENTRY_IN_REGION(entry)) {
1653 entry->info.offset = 0;
1655 entry->data = _free(entry->data);
1662 int headerMod(Header h, rpmtd td)
1669 /* First find the tag */
1670 entry = findEntry(h, td->tag, td->type);
1675 data = grabData(td->type, td->data, td->count, &length);
1676 if (data == NULL || length <= 0)
1679 /* make sure entry points to the first occurence of this tag */
1680 while (entry > h->index && (entry - 1)->info.tag == td->tag)
1683 /* free after we've grabbed the new data in case the two are intertwined;
1684 that's a bad idea but at least we won't break */
1685 oldData = entry->data;
1687 entry->info.count = td->count;
1688 entry->info.type = td->type;
1690 entry->length = length;
1692 if (ENTRY_IN_REGION(entry)) {
1693 entry->info.offset = 0;
1695 oldData = _free(oldData);
1701 * Header tag iterator data structure.
1703 struct headerIterator_s {
1704 Header h; /*!< Header being iterated. */
1705 int next_index; /*!< Next tag index. */
1708 HeaderIterator headerFreeIterator(HeaderIterator hi)
1711 hi->h = headerFree(hi->h);
1717 HeaderIterator headerInitIterator(Header h)
1719 HeaderIterator hi = xmalloc(sizeof(*hi));
1723 hi->h = headerLink(h);
1728 static indexEntry nextIndex(HeaderIterator hi)
1732 indexEntry entry = NULL;
1734 for (slot = hi->next_index; slot < h->indexUsed; slot++) {
1735 entry = h->index + slot;
1736 if (!ENTRY_IS_REGION(entry))
1739 hi->next_index = slot;
1740 if (entry == NULL || slot >= h->indexUsed)
1747 rpmTag headerNextTag(HeaderIterator hi)
1749 indexEntry entry = nextIndex(hi);
1750 return entry ? entry->info.tag : RPMTAG_NOT_FOUND;
1753 int headerNext(HeaderIterator hi, rpmtd td)
1755 indexEntry entry = nextIndex(hi);
1760 td->tag = entry->info.tag;
1761 rc = copyTdEntry(entry, td, HEADERGET_DEFAULT);
1763 return ((rc == 1) ? 1 : 0);
1766 unsigned int headerGetInstance(Header h)
1768 return h ? h->instance : 0;
1771 void headerSetInstance(Header h, unsigned int instance)
1773 h->instance = instance;