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 */
72 * Maximum no. of bytes permitted in a header.
74 static const size_t headerMaxbytes = (32*1024*1024);
77 * HEADER_EXT_TAG format function prototype.
78 * This is allowed to fail, which indicates the tag doesn't exist.
81 * @retval td tag data container
82 * @param flags modifier flags
83 * @return 0 on success
85 typedef int (*headerTagTagFunction) (Header h, rpmtd td, headerGetFlags hgflags);
87 extern void *rpmHeaderTagFunc(rpmTag tag);
89 Header headerLink(Header h)
91 if (h == NULL) return NULL;
95 fprintf(stderr, "--> h %p ++ %d at %s:%u\n", h, h->nrefs, __FILE__, __LINE__);
100 Header headerUnlink(Header h)
102 if (h == NULL) return NULL;
104 fprintf(stderr, "--> h %p -- %d at %s:%u\n", h, h->nrefs, __FILE__, __LINE__);
109 Header headerFree(Header h)
111 (void) headerUnlink(h);
113 if (h == NULL || h->nrefs > 0)
114 return NULL; /* XXX return previous header? */
117 indexEntry entry = h->index;
119 for (i = 0; i < h->indexUsed; i++, entry++) {
120 if ((h->flags & HEADERFLAG_ALLOCATED) && ENTRY_IS_REGION(entry)) {
121 if (entry->length > 0) {
122 int32_t * ei = entry->data;
123 if ((ei - 2) == h->blob) h->blob = _free(h->blob);
126 } else if (!ENTRY_IN_REGION(entry)) {
127 entry->data = _free(entry->data);
131 h->index = _free(h->index);
138 Header headerNew(void)
140 Header h = xcalloc(1, sizeof(*h));
143 h->indexAlloced = INDEX_MALLOC_SIZE;
146 h->flags |= HEADERFLAG_SORTED;
148 h->index = (h->indexAlloced
149 ? xcalloc(h->indexAlloced, sizeof(*h->index))
153 return headerLink(h);
156 int headerVerifyInfo(int il, int dl, const void * pev, void * iv, int negate)
158 entryInfo pe = (entryInfo) pev;
162 for (i = 0; i < il; i++) {
163 info->tag = ntohl(pe[i].tag);
164 info->type = ntohl(pe[i].type);
165 info->offset = ntohl(pe[i].offset);
167 info->offset = -info->offset;
168 info->count = ntohl(pe[i].count);
170 if (hdrchkType(info->type))
172 if (hdrchkAlign(info->type, info->offset))
174 if (!negate && hdrchkRange(dl, info->offset))
176 if (hdrchkData(info->count))
185 static int indexCmp(const void * avp, const void * bvp)
187 indexEntry ap = (indexEntry) avp, bp = (indexEntry) bvp;
188 return (ap->info.tag - bp->info.tag);
191 void headerSort(Header h)
193 if (!(h->flags & HEADERFLAG_SORTED)) {
194 qsort(h->index, h->indexUsed, sizeof(*h->index), indexCmp);
195 h->flags |= HEADERFLAG_SORTED;
201 static int offsetCmp(const void * avp, const void * bvp)
203 indexEntry ap = (indexEntry) avp, bp = (indexEntry) bvp;
204 int rc = (ap->info.offset - bp->info.offset);
207 /* Within a region, entries sort by address. Added drips sort by tag. */
208 if (ap->info.offset < 0)
209 rc = (((char *)ap->data) - ((char *)bp->data));
211 rc = (ap->info.tag - bp->info.tag);
217 * Restore tags in header to original ordering.
220 void headerUnsort(Header h)
222 qsort(h->index, h->indexUsed, sizeof(*h->index), offsetCmp);
225 unsigned headerSizeof(Header h, enum hMagic magicp)
228 unsigned int size = 0;
229 unsigned int pad = 0;
238 case HEADER_MAGIC_YES:
239 size += sizeof(rpm_header_magic);
241 case HEADER_MAGIC_NO:
245 size += 2 * sizeof(int32_t); /* count of index entries */
247 for (i = 0, entry = h->index; i < h->indexUsed; i++, entry++) {
250 /* Regions go in as is ... */
251 if (ENTRY_IS_REGION(entry)) {
252 size += entry->length;
253 /* XXX Legacy regions do not include the region tag and data. */
254 if (i == 0 && (h->flags & HEADERFLAG_LEGACY))
255 size += sizeof(struct entryInfo_s) + entry->info.count;
259 /* ... and region elements are skipped. */
260 if (entry->info.offset < 0)
264 type = entry->info.type;
265 if (typeSizes[type] > 1) {
266 unsigned diff = typeSizes[type] - (size % typeSizes[type]);
267 if (diff != typeSizes[type]) {
273 size += sizeof(struct entryInfo_s) + entry->length;
280 * Return length of entry data.
281 * @param type entry data type
282 * @param p entry data
283 * @param count entry item count
284 * @param onDisk data is concatenated strings (with NUL's))?
285 * @param pend pointer to end of data (or NULL)
286 * @return no. bytes in data, -1 on failure
288 static int dataLength(rpmTagType type, rpm_constdata_t p, rpm_count_t count,
289 int onDisk, rpm_constdata_t pend)
291 const unsigned char * s = p;
292 const unsigned char * se = pend;
296 case RPM_STRING_TYPE:
304 length++; /* count nul terminator too. */
307 case RPM_STRING_ARRAY_TYPE:
308 case RPM_I18NSTRING_TYPE:
309 /* These are like RPM_STRING_TYPE, except they're *always* an array */
310 /* Compute sum of length of all strings, including nul terminators */
314 length++; /* count nul terminator too */
322 const char ** av = (const char **)p;
324 /* add one for null termination */
325 length += strlen(*av++) + 1;
331 if (typeSizes[type] == -1)
333 length = typeSizes[(type & 0xf)] * count;
334 if (length < 0 || (se && (s + length) > se))
343 * Swap int32_t and int16_t arrays within header region.
345 * If a header region tag is in the set to be swabbed, as the data for a
346 * a header region is located after all other tag data.
348 * @param entry header entry
349 * @param il no. of entries
350 * @param dl start no. bytes of data
351 * @param pe header physical entry pointer (swapped)
352 * @param dataStart header data start
353 * @param dataEnd header data end
354 * @param regionid region offset
355 * @return no. bytes of data in region, -1 on error
357 static int regionSwab(indexEntry entry, int il, int dl,
359 unsigned char * dataStart,
360 const unsigned char * dataEnd,
363 struct indexEntry_s ieprev;
365 memset(&ieprev, 0, sizeof(ieprev));
366 for (; il > 0; il--, pe++) {
367 struct indexEntry_s ie;
369 unsigned char * t = NULL;
371 ie.info.tag = ntohl(pe->tag);
372 ie.info.type = ntohl(pe->type);
373 ie.info.count = ntohl(pe->count);
374 ie.info.offset = ntohl(pe->offset);
376 if (hdrchkType(ie.info.type))
378 if (hdrchkData(ie.info.count))
380 if (hdrchkData(ie.info.offset))
382 if (hdrchkAlign(ie.info.type, ie.info.offset))
385 ie.data = t = dataStart + ie.info.offset;
386 if (dataEnd && t >= dataEnd)
389 ie.length = dataLength(ie.info.type, ie.data, ie.info.count, 1, dataEnd);
390 if (ie.length < 0 || hdrchkData(ie.length))
396 ie.info.offset = regionid;
397 *entry = ie; /* structure assignment */
403 if (typeSizes[type] > 1) {
404 unsigned diff = typeSizes[type] - (dl % typeSizes[type]);
405 if (diff != typeSizes[type]) {
407 if (ieprev.info.type == RPM_I18NSTRING_TYPE)
408 ieprev.length += diff;
412 /* Perform endian conversions */
413 switch (ntohl(pe->type)) {
415 { uint64_t * it = (uint64_t *)t;
416 for (; ie.info.count > 0; ie.info.count--, it += 1) {
417 if (dataEnd && ((unsigned char *)it) >= dataEnd)
421 t = (unsigned char *) it;
424 { int32_t * it = (int32_t *)t;
425 for (; ie.info.count > 0; ie.info.count--, it += 1) {
426 if (dataEnd && ((unsigned char *)it) >= dataEnd)
430 t = (unsigned char *) it;
433 { int16_t * it = (int16_t *) t;
434 for (; ie.info.count > 0; ie.info.count--, it += 1) {
435 if (dataEnd && ((unsigned char *)it) >= dataEnd)
439 t = (unsigned char *) it;
444 ieprev = ie; /* structure assignment */
453 * @retval *lengthPtr no. bytes in unloaded header blob
454 * @return unloaded header blob (NULL on error)
456 static void * doHeaderUnload(Header h,
470 int drlen, ndribbles;
473 /* Sort entries by (offset,tag). */
476 /* Compute (il,dl) for all tags, including those deleted in region. */
478 drlen = ndribbles = driplen = ndrips = 0;
479 for (i = 0, entry = h->index; i < h->indexUsed; i++, entry++) {
480 if (ENTRY_IS_REGION(entry)) {
481 int32_t rdl = -entry->info.offset; /* negative offset */
482 int32_t ril = rdl/sizeof(*pe);
483 int rid = entry->info.offset;
486 dl += entry->rdlen + entry->info.count;
487 /* XXX Legacy regions do not include the region tag and data. */
488 if (i == 0 && (h->flags & HEADERFLAG_LEGACY))
491 /* Skip rest of entries in region, but account for dribbles. */
492 for (; i < h->indexUsed && entry->info.offset <= rid+1; i++, entry++) {
493 if (entry->info.offset <= rid)
497 type = entry->info.type;
498 if (typeSizes[type] > 1) {
499 unsigned diff = typeSizes[type] - (dl % typeSizes[type]);
500 if (diff != typeSizes[type]) {
509 drlen += entry->length;
517 /* Ignore deleted drips. */
518 if (entry->data == NULL || entry->length <= 0)
522 type = entry->info.type;
523 if (typeSizes[type] > 1) {
524 unsigned diff = typeSizes[type] - (dl % typeSizes[type]);
525 if (diff != typeSizes[type]) {
534 driplen += entry->length;
538 /* Sanity checks on header intro. */
539 if (hdrchkTags(il) || hdrchkData(dl))
542 len = sizeof(il) + sizeof(dl) + (il * sizeof(*pe)) + dl;
548 pe = (entryInfo) &ei[2];
549 dataStart = te = (char *) (pe + il);
552 for (i = 0, entry = h->index; i < h->indexUsed; i++, entry++) {
558 if (entry->data == NULL || entry->length <= 0)
561 t = (unsigned char*)te;
562 pe->tag = htonl(entry->info.tag);
563 pe->type = htonl(entry->info.type);
564 pe->count = htonl(entry->info.count);
566 if (ENTRY_IS_REGION(entry)) {
567 int32_t rdl = -entry->info.offset; /* negative offset */
568 int32_t ril = rdl/sizeof(*pe) + ndribbles;
569 int rid = entry->info.offset;
571 src = (char *)entry->data;
572 rdlen = entry->rdlen;
574 /* XXX Legacy regions do not include the region tag and data. */
575 if (i == 0 && (h->flags & HEADERFLAG_LEGACY)) {
578 memcpy(pe+1, src, rdl);
579 memcpy(te, src + rdl, rdlen);
582 pe->offset = htonl(te - dataStart);
585 stei[2] = htonl(-rdl-entry->info.count);
587 memcpy(te, stei, entry->info.count);
588 te += entry->info.count;
590 rdlen += entry->info.count;
592 count = regionSwab(NULL, ril, 0, pe, t, NULL, 0);
598 memcpy(pe+1, src + sizeof(*pe), ((ril-1) * sizeof(*pe)));
599 memcpy(te, src + (ril * sizeof(*pe)), rdlen+entry->info.count+drlen);
602 entryInfo se = (entryInfo)src;
603 int off = ntohl(se->offset);
604 pe->offset = (off) ? htonl(te - dataStart) : htonl(off);
606 te += entry->info.count + drlen;
608 count = regionSwab(NULL, ril, 0, pe, t, NULL, 0);
609 if (count != (rdlen + entry->info.count + drlen))
613 /* Skip rest of entries in region. */
614 while (i < h->indexUsed && entry->info.offset <= rid+1) {
624 /* Ignore deleted drips. */
625 if (entry->data == NULL || entry->length <= 0)
629 type = entry->info.type;
630 if (typeSizes[type] > 1) {
632 diff = typeSizes[type] - ((te - dataStart) % typeSizes[type]);
633 if (diff != typeSizes[type]) {
640 pe->offset = htonl(te - dataStart);
642 /* copy data w/ endian conversions */
643 switch (entry->info.type) {
645 count = entry->info.count;
648 *((uint64_t *)te) = htonll(*((uint64_t *)src));
649 te += sizeof(uint64_t);
650 src += sizeof(uint64_t);
655 count = entry->info.count;
658 *((int32_t *)te) = htonl(*((int32_t *)src));
659 te += sizeof(int32_t);
660 src += sizeof(int32_t);
665 count = entry->info.count;
668 *((int16_t *)te) = htons(*((int16_t *)src));
669 te += sizeof(int16_t);
670 src += sizeof(int16_t);
675 memcpy(te, entry->data, entry->length);
682 /* Insure that there are no memcpy underruns/overruns. */
683 if (((char *)pe) != dataStart)
685 if ((((char *)ei)+len) != te)
691 h->flags &= ~HEADERFLAG_SORTED;
701 void * headerUnload(Header h)
704 void * uh = doHeaderUnload(h, &length);
709 * Find matching (tag,type) entry in header.
711 * @param tag entry tag
712 * @param type entry type
713 * @return header entry
716 indexEntry findEntry(Header h, rpmTag tag, rpmTagType type)
718 indexEntry entry, entry2, last;
719 struct indexEntry_s key;
721 if (h == NULL) return NULL;
722 if (!(h->flags & HEADERFLAG_SORTED)) headerSort(h);
727 bsearch(&key, h->index, h->indexUsed, sizeof(*h->index), indexCmp);
731 if (type == RPM_NULL_TYPE)
735 while (entry->info.tag == tag && entry->info.type != type &&
736 entry > h->index) entry--;
738 if (entry->info.tag == tag && entry->info.type == type)
741 last = h->index + h->indexUsed;
742 /* FIX: entry2 = entry. Code looks bogus as well. */
743 while (entry2->info.tag == tag && entry2->info.type != type &&
744 entry2 < last) entry2++;
746 if (entry->info.tag == tag && entry->info.type == type)
752 int headerDel(Header h, rpmTag tag)
754 indexEntry last = h->index + h->indexUsed;
755 indexEntry entry, first;
758 entry = findEntry(h, tag, RPM_NULL_TYPE);
759 if (!entry) return 1;
761 /* Make sure entry points to the first occurence of this tag. */
762 while (entry > h->index && (entry - 1)->info.tag == tag)
765 /* Free data for tags being removed. */
766 for (first = entry; first < last; first++) {
768 if (first->info.tag != tag)
773 if (ENTRY_IN_REGION(first))
778 ne = (first - entry);
783 memmove(entry, first, (ne * sizeof(*entry)));
789 Header headerLoad(void * uh)
791 int32_t * ei = (int32_t *) uh;
792 int32_t il = ntohl(ei[0]); /* index length */
793 int32_t dl = ntohl(ei[1]); /* data length */
794 size_t pvlen = sizeof(il) + sizeof(dl) +
795 (il * sizeof(struct entryInfo_s)) + dl;
799 unsigned char * dataStart;
800 unsigned char * dataEnd;
805 /* Sanity checks on header intro. */
806 if (hdrchkTags(il) || hdrchkData(dl))
810 pe = (entryInfo) &ei[2];
811 dataStart = (unsigned char *) (pe + il);
812 dataEnd = dataStart + dl;
814 h = xcalloc(1, sizeof(*h));
816 h->indexAlloced = il + 1;
819 h->index = xcalloc(h->indexAlloced, sizeof(*h->index));
820 h->flags |= HEADERFLAG_SORTED;
826 if (!(htonl(pe->tag) < HEADER_I18NTABLE)) {
827 h->flags |= HEADERFLAG_LEGACY;
828 entry->info.type = REGION_TAG_TYPE;
829 entry->info.tag = HEADER_IMAGE;
830 entry->info.count = REGION_TAG_COUNT;
831 entry->info.offset = ((unsigned char *)pe - dataStart); /* negative offset */
834 entry->length = pvlen - sizeof(il) - sizeof(dl);
835 rdlen = regionSwab(entry+1, il, 0, pe, dataStart, dataEnd, entry->info.offset);
838 entry->rdlen = rdlen;
845 h->flags &= ~HEADERFLAG_LEGACY;
847 entry->info.type = htonl(pe->type);
848 entry->info.count = htonl(pe->count);
850 if (hdrchkType(entry->info.type))
852 if (hdrchkTags(entry->info.count))
855 { int off = ntohl(pe->offset);
860 size_t nb = REGION_TAG_COUNT;
862 /* XXX Hmm, why the copy? */
863 memcpy(&stei, dataStart + off, nb);
864 rdl = -ntohl(stei[2]); /* negative offset */
865 ril = rdl/sizeof(*pe);
866 if (hdrchkTags(ril) || hdrchkData(rdl))
868 entry->info.tag = htonl(pe->tag);
871 rdl = (ril * sizeof(struct entryInfo_s));
872 entry->info.tag = HEADER_IMAGE;
875 entry->info.offset = -rdl; /* negative offset */
878 entry->length = pvlen - sizeof(il) - sizeof(dl);
879 rdlen = regionSwab(entry+1, ril-1, 0, pe+1, dataStart, dataEnd, entry->info.offset);
882 entry->rdlen = rdlen;
884 if (ril < h->indexUsed) {
885 indexEntry newEntry = entry + ril;
886 int ne = (h->indexUsed - ril);
887 int rid = entry->info.offset+1;
890 /* Load dribble entries from region. */
891 rc = regionSwab(newEntry, ne, 0, pe+ril, dataStart, dataEnd, rid);
896 { indexEntry firstEntry = newEntry;
897 int save = h->indexUsed;
900 /* Dribble entries replace duplicate region entries. */
902 for (j = 0; j < ne; j++, newEntry++) {
903 (void) headerDel(h, newEntry->info.tag);
904 if (newEntry->info.tag == RPMTAG_BASENAMES)
905 (void) headerDel(h, RPMTAG_OLDFILENAMES);
908 /* If any duplicate entries were replaced, move new entries down. */
909 if (h->indexUsed < (save - ne)) {
910 memmove(h->index + h->indexUsed, firstEntry,
911 (ne * sizeof(*entry)));
918 h->flags &= ~HEADERFLAG_SORTED;
920 h->flags |= HEADERFLAG_ALLOCATED;
926 h->index = _free(h->index);
932 Header headerReload(Header h, rpmTag tag)
936 void * uh = doHeaderUnload(h, &length);
946 if (ENTRY_IS_REGION(nh->index)) {
947 if (tag == HEADER_SIGNATURES || tag == HEADER_IMMUTABLE)
948 nh->index[0].info.tag = tag;
953 Header headerCopyLoad(const void * uh)
955 int32_t * ei = (int32_t *) uh;
956 int32_t il = ntohl(ei[0]); /* index length */
957 int32_t dl = ntohl(ei[1]); /* data length */
958 size_t pvlen = sizeof(il) + sizeof(dl) +
959 (il * sizeof(struct entryInfo_s)) + dl;
963 /* Sanity checks on header intro. */
964 if (!(hdrchkTags(il) || hdrchkData(dl)) && pvlen < headerMaxbytes) {
965 nuh = memcpy(xmalloc(pvlen), uh, pvlen);
966 if ((h = headerLoad(nuh)) == NULL)
973 * Read (and load) header from file handle.
974 * @param fd file handle
975 * @param magicp read (and verify) 8 bytes of (magic, 0)?
976 * @return header (or NULL on error)
978 Header headerRead(FD_t fd, enum hMagic magicp)
990 memset(block, 0, sizeof(block));
992 if (magicp == HEADER_MAGIC_YES)
996 if (timedRead(fd, (char *)block, i*sizeof(*block)) != (i * sizeof(*block)))
1001 if (magicp == HEADER_MAGIC_YES) {
1003 if (memcmp(&magic, rpm_header_magic, sizeof(magic)))
1005 reserved = block[i++];
1008 il = ntohl(block[i]); i++;
1009 dl = ntohl(block[i]); i++;
1011 len = sizeof(il) + sizeof(dl) + (il * sizeof(struct entryInfo_s)) + dl;
1013 /* Sanity checks on header intro. */
1014 if (hdrchkTags(il) || hdrchkData(dl) || len > headerMaxbytes)
1020 len -= sizeof(il) + sizeof(dl);
1023 if (timedRead(fd, (char *)&ei[2], len) != len)
1029 if (h == NULL && ei != NULL) {
1035 int headerWrite(FD_t fd, Header h, enum hMagic magicp)
1043 uh = doHeaderUnload(h, &length);
1047 case HEADER_MAGIC_YES:
1048 nb = Fwrite(rpm_header_magic, sizeof(uint8_t), sizeof(rpm_header_magic), fd);
1049 if (nb != sizeof(rpm_header_magic))
1052 case HEADER_MAGIC_NO:
1056 nb = Fwrite(uh, sizeof(char), length, fd);
1060 return (nb == length ? 0 : 1);
1063 int headerIsEntry(Header h, rpmTag tag)
1065 /* FIX: h modified by sort. */
1066 return (findEntry(h, tag, RPM_NULL_TYPE) ? 1 : 0);
1071 * Retrieve data from header entry.
1072 * Relevant flags (others are ignored), if neither is set allocation
1073 * behavior depends on data type(!)
1074 * HEADERGET_MINMEM: return pointers to header memory
1075 * HEADERGET_ALLOC: always return malloced memory, overrides MINMEM
1077 * @todo Permit retrieval of regions other than HEADER_IMUTABLE.
1078 * @param entry header entry
1079 * @param td tag data container
1080 * @param minMem string pointers refer to header memory?
1081 * @param flags flags to control memory allocation
1082 * @return 1 on success, otherwise error.
1084 static int copyTdEntry(const indexEntry entry, rpmtd td, headerGetFlags flags)
1086 rpm_count_t count = entry->info.count;
1087 int rc = 1; /* XXX 1 on success. */
1088 /* ALLOC overrides MINMEM */
1089 int allocMem = flags & HEADERGET_ALLOC;
1090 int minMem = allocMem ? 0 : flags & HEADERGET_MINMEM;
1091 int argvArray = (flags & HEADERGET_ARGV) ? 1 : 0;
1094 td->flags = RPMTD_IMMUTABLE;
1095 switch (entry->info.type) {
1098 * XXX This only works for
1099 * XXX "sealed" HEADER_IMMUTABLE/HEADER_SIGNATURES/HEADER_IMAGE.
1100 * XXX This will *not* work for unsealed legacy HEADER_IMAGE (i.e.
1101 * XXX a legacy header freshly read, but not yet unloaded to the rpmdb).
1103 if (ENTRY_IS_REGION(entry)) {
1104 int32_t * ei = ((int32_t *)entry->data) - 2;
1105 entryInfo pe = (entryInfo) (ei + 2);
1106 unsigned char * dataStart = (unsigned char *) (pe + ntohl(ei[0]));
1107 int32_t rdl = -entry->info.offset; /* negative offset */
1108 int32_t ril = rdl/sizeof(*pe);
1111 count = 2 * sizeof(*ei) + (ril * sizeof(*pe)) + rdl;
1112 if (entry->info.tag == HEADER_IMAGE) {
1116 count += REGION_TAG_COUNT;
1117 rdl += REGION_TAG_COUNT;
1120 td->data = xmalloc(count);
1121 ei = (int32_t *) td->data;
1125 pe = (entryInfo) memcpy(ei + 2, pe, (ril * sizeof(*pe)));
1127 dataStart = (unsigned char *) memcpy(pe + ril, dataStart, rdl);
1129 rc = regionSwab(NULL, ril, 0, pe, dataStart, dataStart + rdl, 0);
1130 /* don't return data on failure */
1132 td->data = _free(td->data);
1134 /* XXX 1 on success. */
1135 rc = (rc < 0) ? 0 : 1;
1137 count = entry->length;
1139 ? memcpy(xmalloc(count), entry->data, count)
1143 case RPM_STRING_TYPE:
1145 td->data = allocMem ? xstrdup(entry->data) : entry->data;
1148 case RPM_STRING_ARRAY_TYPE:
1149 case RPM_I18NSTRING_TYPE:
1150 { const char ** ptrEntry;
1151 int tableSize = (count + argvArray) * sizeof(char *);
1156 td->data = xmalloc(tableSize);
1157 ptrEntry = (const char **) td->data;
1160 t = xmalloc(tableSize + entry->length);
1161 td->data = (void *)t;
1162 ptrEntry = (const char **) td->data;
1164 memcpy(t, entry->data, entry->length);
1166 for (i = 0; i < count; i++) {
1173 td->flags |= RPMTD_ARGV;
1178 case RPM_INT16_TYPE:
1179 case RPM_INT32_TYPE:
1180 case RPM_INT64_TYPE:
1182 td->data = xmalloc(entry->length);
1183 memcpy(td->data, entry->data, entry->length);
1185 td->data = entry->data;
1189 /* WTH? Don't mess with unknown data types... */
1194 td->type = entry->info.type;
1197 if (td->data && entry->data != td->data) {
1198 td->flags |= RPMTD_ALLOCED;
1205 * Does locale match entry in header i18n table?
1208 * The range [l,le) contains the next locale to match:
1209 * ll[_CC][.EEEEE][@dddd]
1211 * ll ISO language code (in lowercase).
1212 * CC (optional) ISO coutnry code (in uppercase).
1213 * EEEEE (optional) encoding (not really standardized).
1214 * dddd (optional) dialect.
1217 * @param td header i18n table data, NUL terminated
1218 * @param l start of locale to match
1219 * @param le end of locale to match
1220 * @return 1 on good match, 2 on weak match, 0 on no match
1222 static int headerMatchLocale(const char *td, const char *l, const char *le)
1226 /* First try a complete match. */
1227 if (strlen(td) == (le-l) && rstreqn(td, l, (le - l)))
1230 /* Next, try stripping optional dialect and matching. */
1231 for (fe = l; fe < le && *fe != '@'; fe++)
1233 if (fe < le && rstreqn(td, l, (fe - l)))
1236 /* Next, try stripping optional codeset and matching. */
1237 for (fe = l; fe < le && *fe != '.'; fe++)
1239 if (fe < le && rstreqn(td, l, (fe - l)))
1242 /* Finally, try stripping optional country code and matching. */
1243 for (fe = l; fe < le && *fe != '_'; fe++)
1245 if (fe < le && rstreqn(td, l, (fe - l)))
1252 * Return i18n string from header that matches locale.
1254 * @param entry i18n string data
1255 * @retval td tag data container
1256 * @param flags flags to control allocation
1259 static int copyI18NEntry(Header h, indexEntry entry, rpmtd td,
1260 headerGetFlags flags)
1262 const char *lang, *l, *le;
1265 td->type = RPM_STRING_TYPE;
1267 /* if no match, just return the first string */
1268 td->data = entry->data;
1270 /* XXX Drepper sez' this is the order. */
1271 if ((lang = getenv("LANGUAGE")) == NULL &&
1272 (lang = getenv("LC_ALL")) == NULL &&
1273 (lang = getenv("LC_MESSAGES")) == NULL &&
1274 (lang = getenv("LANG")) == NULL)
1277 if ((table = findEntry(h, HEADER_I18NTABLE, RPM_STRING_ARRAY_TYPE)) == NULL)
1280 for (l = lang; *l != '\0'; l = le) {
1282 char *ed, *ed_weak = NULL;
1285 while (*l && *l == ':') /* skip leading colons */
1289 for (le = l; *le && *le != ':'; le++) /* find end of this locale */
1292 /* For each entry in the header ... */
1293 for (langNum = 0, t = table->data, ed = entry->data;
1294 langNum < entry->info.count;
1295 langNum++, t += strlen(t) + 1, ed += strlen(ed) + 1) {
1297 int match = headerMatchLocale(t, l, le);
1301 } else if (match == 2) {
1312 if (flags & HEADERGET_ALLOC) {
1313 td->data = xstrdup(td->data);
1314 td->flags |= RPMTD_ALLOCED;
1321 * Retrieve tag data from header.
1323 * @retval td tag data container
1324 * @param flags flags to control retrieval
1325 * @return 1 on success, 0 on not found
1327 static int intGetTdEntry(Header h, rpmtd td, headerGetFlags flags)
1332 /* First find the tag */
1333 /* FIX: h modified by sort. */
1334 entry = findEntry(h, td->tag, RPM_NULL_TYPE);
1335 if (entry == NULL) {
1336 /* Td is zeroed above, just return... */
1340 if (flags & HEADERGET_RAW) {
1341 rc = copyTdEntry(entry, td, flags);
1343 switch (entry->info.type) {
1344 case RPM_I18NSTRING_TYPE:
1345 rc = copyI18NEntry(h, entry, td, flags);
1348 rc = copyTdEntry(entry, td, flags);
1353 /* XXX 1 on success */
1354 return ((rc == 1) ? 1 : 0);
1357 int headerGet(Header h, rpmTag tag, rpmtd td, headerGetFlags flags)
1360 headerTagTagFunction tagfunc = intGetTdEntry;
1362 if (td == NULL) return 0;
1367 if (flags & HEADERGET_EXT) {
1368 headerTagTagFunction extfunc = rpmHeaderTagFunc(tag);
1369 if (extfunc) tagfunc = extfunc;
1371 rc = tagfunc(h, td, flags);
1373 assert(tag == td->tag);
1379 static void copyData(rpmTagType type, rpm_data_t dstPtr,
1380 rpm_constdata_t srcPtr, rpm_count_t cnt, int dataLength)
1383 case RPM_STRING_ARRAY_TYPE:
1384 case RPM_I18NSTRING_TYPE:
1385 { const char ** av = (const char **) srcPtr;
1388 while (cnt-- > 0 && dataLength > 0) {
1390 if ((s = *av++) == NULL)
1394 } while (s[-1] && --dataLength > 0);
1399 memmove(dstPtr, srcPtr, dataLength);
1405 * Return (malloc'ed) copy of entry data.
1406 * @param type entry data type
1407 * @param p entry data
1408 * @param c entry item count
1409 * @retval lengthPtr no. bytes in returned data
1410 * @return (malloc'ed) copy of entry data, NULL on error
1413 grabData(rpmTagType type, rpm_constdata_t p, rpm_count_t c, int * lengthPtr)
1415 rpm_data_t data = NULL;
1418 length = dataLength(type, p, c, 0, NULL);
1420 data = xmalloc(length);
1421 copyData(type, data, p, c, length);
1425 *lengthPtr = length;
1429 static int intAddEntry(Header h, rpmtd td)
1435 /* Count must always be >= 1 for headerAddEntry. */
1439 if (hdrchkType(td->type))
1441 if (hdrchkData(td->count))
1445 data = grabData(td->type, td->data, td->count, &length);
1446 if (data == NULL || length <= 0)
1449 /* Allocate more index space if necessary */
1450 if (h->indexUsed == h->indexAlloced) {
1451 h->indexAlloced += INDEX_MALLOC_SIZE;
1452 h->index = xrealloc(h->index, h->indexAlloced * sizeof(*h->index));
1455 /* Fill in the index */
1456 entry = h->index + h->indexUsed;
1457 entry->info.tag = td->tag;
1458 entry->info.type = td->type;
1459 entry->info.count = td->count;
1460 entry->info.offset = 0;
1462 entry->length = length;
1464 if (h->indexUsed > 0 && td->tag < h->index[h->indexUsed-1].info.tag)
1465 h->flags &= ~HEADERFLAG_SORTED;
1471 static int intAppendEntry(Header h, rpmtd td)
1476 if (td->type == RPM_STRING_TYPE || td->type == RPM_I18NSTRING_TYPE) {
1477 /* we can't do this */
1481 /* Find the tag entry in the header. */
1482 entry = findEntry(h, td->tag, td->type);
1486 length = dataLength(td->type, td->data, td->count, 0, NULL);
1490 if (ENTRY_IN_REGION(entry)) {
1491 char * t = xmalloc(entry->length + length);
1492 memcpy(t, entry->data, entry->length);
1494 entry->info.offset = 0;
1496 entry->data = xrealloc(entry->data, entry->length + length);
1498 copyData(td->type, ((char *) entry->data) + entry->length,
1499 td->data, td->count, length);
1501 entry->length += length;
1503 entry->info.count += td->count;
1508 int headerPut(Header h, rpmtd td, headerPutFlags flags)
1513 if (flags & HEADERPUT_APPEND) {
1514 rc = findEntry(h, td->tag, td->type) ?
1515 intAppendEntry(h, td) :
1518 rc = intAddEntry(h, td);
1524 * Sanity check data types against tag table before putting. Assume
1525 * append on all array-types.
1527 static int headerPutType(Header h, rpmTag tag, rpmTagType reqtype,
1528 rpm_constdata_t data, rpm_count_t size)
1531 rpmTagType type = rpmTagGetType(tag);
1532 headerPutFlags flags = HEADERPUT_APPEND;
1535 /* Basic sanity checks: type must match and there must be data to put */
1536 if ((type & RPM_MASK_TYPE) != reqtype
1537 || size < 1 || data == NULL || h == NULL) {
1542 * Non-array types can't be appended to. Binary types use size
1543 * for data length, for other non-array types size must be 1.
1545 if ((type & RPM_MASK_RETURN_TYPE) != RPM_ARRAY_RETURN_TYPE) {
1546 flags = HEADERPUT_DEFAULT;
1547 if ((type & RPM_MASK_TYPE) != RPM_BIN_TYPE && size != 1) {
1555 td.type = type & RPM_MASK_TYPE;
1556 td.data = (void *) data;
1559 valid = headerPut(h, &td, flags);
1565 int headerPutString(Header h, rpmTag tag, const char *val)
1567 rpmTagType type = rpmTagGetType(tag) & RPM_MASK_TYPE;
1568 const void *sptr = NULL;
1570 /* string arrays expect char **, arrange that */
1571 if (type == RPM_STRING_ARRAY_TYPE || type == RPM_I18NSTRING_TYPE) {
1573 } else if (type == RPM_STRING_TYPE) {
1579 return headerPutType(h, tag, type, sptr, 1);
1582 int headerPutStringArray(Header h, rpmTag tag, const char **array, rpm_count_t size)
1584 return headerPutType(h, tag, RPM_STRING_ARRAY_TYPE, array, size);
1587 int headerPutChar(Header h, rpmTag tag, char *val, rpm_count_t size)
1589 return headerPutType(h, tag, RPM_CHAR_TYPE, val, size);
1592 int headerPutUint8(Header h, rpmTag tag, uint8_t *val, rpm_count_t size)
1594 return headerPutType(h, tag, RPM_INT8_TYPE, val, size);
1597 int headerPutUint16(Header h, rpmTag tag, uint16_t *val, rpm_count_t size)
1599 return headerPutType(h, tag, RPM_INT16_TYPE, val, size);
1602 int headerPutUint32(Header h, rpmTag tag, uint32_t *val, rpm_count_t size)
1604 return headerPutType(h, tag, RPM_INT32_TYPE, val, size);
1607 int headerPutUint64(Header h, rpmTag tag, uint64_t *val, rpm_count_t size)
1609 return headerPutType(h, tag, RPM_INT64_TYPE, val, size);
1612 int headerPutBin(Header h, rpmTag tag, uint8_t *val, rpm_count_t size)
1614 return headerPutType(h, tag, RPM_BIN_TYPE, val, size);
1617 int headerAddI18NString(Header h, rpmTag tag, const char * string,
1620 indexEntry table, entry;
1621 const char ** strArray;
1624 rpm_count_t i, langNum;
1627 table = findEntry(h, HEADER_I18NTABLE, RPM_STRING_ARRAY_TYPE);
1628 entry = findEntry(h, tag, RPM_I18NSTRING_TYPE);
1630 if (!table && entry)
1631 return 0; /* this shouldn't ever happen!! */
1633 if (!table && !entry) {
1634 const char * charArray[2];
1635 rpm_count_t count = 0;
1637 if (!lang || (lang[0] == 'C' && lang[1] == '\0')) {
1638 charArray[count++] = "C";
1640 charArray[count++] = "C";
1641 charArray[count++] = lang;
1645 td.tag = HEADER_I18NTABLE;
1646 td.type = RPM_STRING_ARRAY_TYPE;
1647 td.data = (void *) charArray;
1649 if (!headerPut(h, &td, HEADERPUT_DEFAULT))
1651 table = findEntry(h, HEADER_I18NTABLE, RPM_STRING_ARRAY_TYPE);
1656 if (!lang) lang = "C";
1658 { const char * l = table->data;
1659 for (langNum = 0; langNum < table->info.count; langNum++) {
1660 if (rstreq(l, lang)) break;
1665 if (langNum >= table->info.count) {
1666 length = strlen(lang) + 1;
1667 if (ENTRY_IN_REGION(table)) {
1668 char * t = xmalloc(table->length + length);
1669 memcpy(t, table->data, table->length);
1671 table->info.offset = 0;
1673 table->data = xrealloc(table->data, table->length + length);
1674 memmove(((char *)table->data) + table->length, lang, length);
1675 table->length += length;
1676 table->info.count++;
1682 strArray = xmalloc(sizeof(*strArray) * (langNum + 1));
1683 for (i = 0; i < langNum; i++)
1685 strArray[langNum] = string;
1689 td.type = RPM_I18NSTRING_TYPE;
1691 td.count = langNum + 1;
1692 rc = headerPut(h, &td, HEADERPUT_DEFAULT);
1695 } else if (langNum >= entry->info.count) {
1696 ghosts = langNum - entry->info.count;
1698 length = strlen(string) + 1 + ghosts;
1699 if (ENTRY_IN_REGION(entry)) {
1700 char * t = xmalloc(entry->length + length);
1701 memcpy(t, entry->data, entry->length);
1703 entry->info.offset = 0;
1705 entry->data = xrealloc(entry->data, entry->length + length);
1707 memset(((char *)entry->data) + entry->length, '\0', ghosts);
1708 memmove(((char *)entry->data) + entry->length + ghosts, string, strlen(string)+1);
1710 entry->length += length;
1711 entry->info.count = langNum + 1;
1713 char *b, *be, *e, *ee, *t;
1716 /* Set beginning/end pointers to previous data */
1717 b = be = e = ee = entry->data;
1718 for (i = 0; i < table->info.count; i++) {
1721 ee += strlen(ee) + 1;
1726 /* Get storage for new buffer */
1728 sn = strlen(string) + 1;
1730 length = bn + sn + en;
1731 t = buf = xmalloc(length);
1733 /* Copy values into new storage */
1736 memcpy(t, string, sn);
1741 /* Replace i18N string array */
1742 entry->length -= strlen(be) + 1;
1743 entry->length += sn;
1745 if (ENTRY_IN_REGION(entry)) {
1746 entry->info.offset = 0;
1748 entry->data = _free(entry->data);
1755 int headerMod(Header h, rpmtd td)
1762 /* First find the tag */
1763 entry = findEntry(h, td->tag, td->type);
1768 data = grabData(td->type, td->data, td->count, &length);
1769 if (data == NULL || length <= 0)
1772 /* make sure entry points to the first occurence of this tag */
1773 while (entry > h->index && (entry - 1)->info.tag == td->tag)
1776 /* free after we've grabbed the new data in case the two are intertwined;
1777 that's a bad idea but at least we won't break */
1778 oldData = entry->data;
1780 entry->info.count = td->count;
1781 entry->info.type = td->type;
1783 entry->length = length;
1785 if (ENTRY_IN_REGION(entry)) {
1786 entry->info.offset = 0;
1788 oldData = _free(oldData);
1794 * Header tag iterator data structure.
1796 struct headerIterator_s {
1797 Header h; /*!< Header being iterated. */
1798 int next_index; /*!< Next tag index. */
1801 HeaderIterator headerFreeIterator(HeaderIterator hi)
1804 hi->h = headerFree(hi->h);
1810 HeaderIterator headerInitIterator(Header h)
1812 HeaderIterator hi = xmalloc(sizeof(*hi));
1816 hi->h = headerLink(h);
1821 int headerNext(HeaderIterator hi, rpmtd td)
1825 indexEntry entry = NULL;
1831 for (slot = hi->next_index; slot < h->indexUsed; slot++) {
1832 entry = h->index + slot;
1833 if (!ENTRY_IS_REGION(entry))
1836 hi->next_index = slot;
1837 if (entry == NULL || slot >= h->indexUsed)
1842 td->tag = entry->info.tag;
1844 rc = copyTdEntry(entry, td, HEADERGET_DEFAULT);
1846 /* XXX 1 on success */
1847 return ((rc == 1) ? 1 : 0);
1851 * Duplicate a header.
1853 * @return new header instance
1855 Header headerCopy(Header h)
1857 Header nh = headerNew();
1861 hi = headerInitIterator(h);
1862 while (headerNext(hi, &td)) {
1863 if (rpmtdCount(&td) > 0) {
1864 (void) headerPut(nh, &td, HEADERPUT_DEFAULT);
1868 hi = headerFreeIterator(hi);
1870 return headerReload(nh, HEADER_IMAGE);
1873 void headerCopyTags(Header headerFrom, Header headerTo,
1874 const rpmTag * tagstocopy)
1879 if (headerFrom == headerTo)
1882 for (p = tagstocopy; *p != 0; p++) {
1883 if (headerIsEntry(headerTo, *p))
1885 if (!headerGet(headerFrom, *p, &td, HEADERGET_MINMEM))
1887 (void) headerPut(headerTo, &td, HEADERPUT_DEFAULT);
1892 unsigned int headerGetInstance(Header h)
1894 return h ? h->instance : 0;
1897 void headerSetInstance(Header h, unsigned int instance)
1899 h->instance = instance;
1902 char * headerGetAsString(Header h, rpmTag tag)
1907 if (headerGet(h, tag, &td, HEADERGET_EXT)) {
1908 if (rpmtdCount(&td) == 1) {
1909 res = rpmtdFormat(&td, RPMTD_FORMAT_STRING, NULL);
1916 const char * headerGetString(Header h, rpmTag tag)
1918 const char *res = NULL;
1921 if (headerGet(h, tag, &td, HEADERGET_MINMEM)) {
1922 if (rpmtdCount(&td) == 1) {
1923 res = rpmtdGetString(&td);
1930 uint64_t headerGetNumber(Header h, rpmTag tag)
1935 if (headerGet(h, tag, &td, HEADERGET_EXT)) {
1936 if (rpmtdCount(&td) == 1) {
1937 res = rpmtdGetNumber(&td);