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++) {
251 /* Regions go in as is ... */
252 if (ENTRY_IS_REGION(entry)) {
253 size += entry->length;
254 /* XXX Legacy regions do not include the region tag and data. */
255 if (i == 0 && (h->flags & HEADERFLAG_LEGACY))
256 size += sizeof(struct entryInfo_s) + entry->info.count;
260 /* ... and region elements are skipped. */
261 if (entry->info.offset < 0)
265 type = entry->info.type;
266 if (typeSizes[type] > 1) {
267 diff = typeSizes[type] - (size % typeSizes[type]);
268 if (diff != typeSizes[type]) {
274 size += sizeof(struct entryInfo_s) + entry->length;
281 * Return length of entry data.
282 * @param type entry data type
283 * @param p entry data
284 * @param count entry item count
285 * @param onDisk data is concatenated strings (with NUL's))?
286 * @param pend pointer to end of data (or NULL)
287 * @return no. bytes in data, -1 on failure
289 static int dataLength(rpmTagType type, rpm_constdata_t p, rpm_count_t count,
290 int onDisk, rpm_constdata_t pend)
292 const unsigned char * s = p;
293 const unsigned char * se = pend;
297 case RPM_STRING_TYPE:
305 length++; /* count nul terminator too. */
308 case RPM_STRING_ARRAY_TYPE:
309 case RPM_I18NSTRING_TYPE:
310 /* These are like RPM_STRING_TYPE, except they're *always* an array */
311 /* Compute sum of length of all strings, including nul terminators */
315 length++; /* count nul terminator too */
323 const char ** av = (const char **)p;
325 /* add one for null termination */
326 length += strlen(*av++) + 1;
332 if (typeSizes[type] == -1)
334 length = typeSizes[(type & 0xf)] * count;
335 if (length < 0 || (se && (s + length) > se))
344 * Swap int32_t and int16_t arrays within header region.
346 * If a header region tag is in the set to be swabbed, as the data for a
347 * a header region is located after all other tag data.
349 * @param entry header entry
350 * @param il no. of entries
351 * @param dl start no. bytes of data
352 * @param pe header physical entry pointer (swapped)
353 * @param dataStart header data start
354 * @param dataEnd header data end
355 * @param regionid region offset
356 * @return no. bytes of data in region, -1 on error
358 static int regionSwab(indexEntry entry, int il, int dl,
360 unsigned char * dataStart,
361 const unsigned char * dataEnd,
364 struct indexEntry_s ieprev;
366 memset(&ieprev, 0, sizeof(ieprev));
367 for (; il > 0; il--, pe++) {
368 struct indexEntry_s ie;
370 unsigned char * t = NULL;
372 ie.info.tag = ntohl(pe->tag);
373 ie.info.type = ntohl(pe->type);
374 ie.info.count = ntohl(pe->count);
375 ie.info.offset = ntohl(pe->offset);
377 if (hdrchkType(ie.info.type))
379 if (hdrchkData(ie.info.count))
381 if (hdrchkData(ie.info.offset))
383 if (hdrchkAlign(ie.info.type, ie.info.offset))
386 ie.data = t = dataStart + ie.info.offset;
387 if (dataEnd && t >= dataEnd)
390 ie.length = dataLength(ie.info.type, ie.data, ie.info.count, 1, dataEnd);
391 if (ie.length < 0 || hdrchkData(ie.length))
397 ie.info.offset = regionid;
398 *entry = ie; /* structure assignment */
404 if (typeSizes[type] > 1) {
406 diff = typeSizes[type] - (dl % typeSizes[type]);
407 if (diff != typeSizes[type]) {
409 if (ieprev.info.type == RPM_I18NSTRING_TYPE)
410 ieprev.length += diff;
414 /* Perform endian conversions */
415 switch (ntohl(pe->type)) {
417 { uint64_t * it = (uint64_t *)t;
418 for (; ie.info.count > 0; ie.info.count--, it += 1) {
419 if (dataEnd && ((unsigned char *)it) >= dataEnd)
423 t = (unsigned char *) it;
426 { int32_t * it = (int32_t *)t;
427 for (; ie.info.count > 0; ie.info.count--, it += 1) {
428 if (dataEnd && ((unsigned char *)it) >= dataEnd)
432 t = (unsigned char *) it;
435 { int16_t * it = (int16_t *) t;
436 for (; ie.info.count > 0; ie.info.count--, it += 1) {
437 if (dataEnd && ((unsigned char *)it) >= dataEnd)
441 t = (unsigned char *) it;
446 ieprev = ie; /* structure assignment */
455 * @retval *lengthPtr no. bytes in unloaded header blob
456 * @return unloaded header blob (NULL on error)
458 static void * doHeaderUnload(Header h,
472 int drlen, ndribbles;
476 /* Sort entries by (offset,tag). */
479 /* Compute (il,dl) for all tags, including those deleted in region. */
481 drlen = ndribbles = driplen = ndrips = 0;
482 for (i = 0, entry = h->index; i < h->indexUsed; i++, entry++) {
483 if (ENTRY_IS_REGION(entry)) {
484 int32_t rdl = -entry->info.offset; /* negative offset */
485 int32_t ril = rdl/sizeof(*pe);
486 int rid = entry->info.offset;
489 dl += entry->rdlen + entry->info.count;
490 /* XXX Legacy regions do not include the region tag and data. */
491 if (i == 0 && (h->flags & HEADERFLAG_LEGACY))
494 /* Skip rest of entries in region, but account for dribbles. */
495 for (; i < h->indexUsed && entry->info.offset <= rid+1; i++, entry++) {
496 if (entry->info.offset <= rid)
500 type = entry->info.type;
501 if (typeSizes[type] > 1) {
503 diff = typeSizes[type] - (dl % typeSizes[type]);
504 if (diff != typeSizes[type]) {
513 drlen += entry->length;
521 /* Ignore deleted drips. */
522 if (entry->data == NULL || entry->length <= 0)
526 type = entry->info.type;
527 if (typeSizes[type] > 1) {
529 diff = typeSizes[type] - (dl % typeSizes[type]);
530 if (diff != typeSizes[type]) {
540 driplen += entry->length;
544 /* Sanity checks on header intro. */
545 if (hdrchkTags(il) || hdrchkData(dl))
548 len = sizeof(il) + sizeof(dl) + (il * sizeof(*pe)) + dl;
554 pe = (entryInfo) &ei[2];
555 dataStart = te = (char *) (pe + il);
558 for (i = 0, entry = h->index; i < h->indexUsed; i++, entry++) {
564 if (entry->data == NULL || entry->length <= 0)
567 t = (unsigned char*)te;
568 pe->tag = htonl(entry->info.tag);
569 pe->type = htonl(entry->info.type);
570 pe->count = htonl(entry->info.count);
572 if (ENTRY_IS_REGION(entry)) {
573 int32_t rdl = -entry->info.offset; /* negative offset */
574 int32_t ril = rdl/sizeof(*pe) + ndribbles;
575 int rid = entry->info.offset;
577 src = (char *)entry->data;
578 rdlen = entry->rdlen;
580 /* XXX Legacy regions do not include the region tag and data. */
581 if (i == 0 && (h->flags & HEADERFLAG_LEGACY)) {
585 memcpy(pe+1, src, rdl);
586 memcpy(te, src + rdl, rdlen);
589 pe->offset = htonl(te - dataStart);
592 stei[2] = htonl(-rdl-entry->info.count);
594 memcpy(te, stei, entry->info.count);
595 te += entry->info.count;
597 rdlen += entry->info.count;
599 count = regionSwab(NULL, ril, 0, pe, t, NULL, 0);
605 memcpy(pe+1, src + sizeof(*pe), ((ril-1) * sizeof(*pe)));
606 memcpy(te, src + (ril * sizeof(*pe)), rdlen+entry->info.count+drlen);
609 entryInfo se = (entryInfo)src;
610 int off = ntohl(se->offset);
611 pe->offset = (off) ? htonl(te - dataStart) : htonl(off);
613 te += entry->info.count + drlen;
615 count = regionSwab(NULL, ril, 0, pe, t, NULL, 0);
616 if (count != (rdlen + entry->info.count + drlen))
620 /* Skip rest of entries in region. */
621 while (i < h->indexUsed && entry->info.offset <= rid+1) {
631 /* Ignore deleted drips. */
632 if (entry->data == NULL || entry->length <= 0)
636 type = entry->info.type;
637 if (typeSizes[type] > 1) {
639 diff = typeSizes[type] - ((te - dataStart) % typeSizes[type]);
640 if (diff != typeSizes[type]) {
647 pe->offset = htonl(te - dataStart);
649 /* copy data w/ endian conversions */
650 switch (entry->info.type) {
652 count = entry->info.count;
655 *((uint64_t *)te) = htonll(*((uint64_t *)src));
656 te += sizeof(uint64_t);
657 src += sizeof(uint64_t);
662 count = entry->info.count;
665 *((int32_t *)te) = htonl(*((int32_t *)src));
666 te += sizeof(int32_t);
667 src += sizeof(int32_t);
672 count = entry->info.count;
675 *((int16_t *)te) = htons(*((int16_t *)src));
676 te += sizeof(int16_t);
677 src += sizeof(int16_t);
682 memcpy(te, entry->data, entry->length);
689 /* Insure that there are no memcpy underruns/overruns. */
690 if (((char *)pe) != dataStart)
692 if ((((char *)ei)+len) != te)
698 h->flags &= ~HEADERFLAG_SORTED;
708 void * headerUnload(Header h)
711 void * uh = doHeaderUnload(h, &length);
716 * Find matching (tag,type) entry in header.
718 * @param tag entry tag
719 * @param type entry type
720 * @return header entry
723 indexEntry findEntry(Header h, rpmTag tag, rpmTagType type)
725 indexEntry entry, entry2, last;
726 struct indexEntry_s key;
728 if (h == NULL) return NULL;
729 if (!(h->flags & HEADERFLAG_SORTED)) headerSort(h);
734 bsearch(&key, h->index, h->indexUsed, sizeof(*h->index), indexCmp);
738 if (type == RPM_NULL_TYPE)
742 while (entry->info.tag == tag && entry->info.type != type &&
743 entry > h->index) entry--;
745 if (entry->info.tag == tag && entry->info.type == type)
748 last = h->index + h->indexUsed;
749 /* FIX: entry2 = entry. Code looks bogus as well. */
750 while (entry2->info.tag == tag && entry2->info.type != type &&
751 entry2 < last) entry2++;
753 if (entry->info.tag == tag && entry->info.type == type)
759 int headerDel(Header h, rpmTag tag)
761 indexEntry last = h->index + h->indexUsed;
762 indexEntry entry, first;
765 entry = findEntry(h, tag, RPM_NULL_TYPE);
766 if (!entry) return 1;
768 /* Make sure entry points to the first occurence of this tag. */
769 while (entry > h->index && (entry - 1)->info.tag == tag)
772 /* Free data for tags being removed. */
773 for (first = entry; first < last; first++) {
775 if (first->info.tag != tag)
780 if (ENTRY_IN_REGION(first))
785 ne = (first - entry);
790 memmove(entry, first, (ne * sizeof(*entry)));
796 Header headerLoad(void * uh)
798 int32_t * ei = (int32_t *) uh;
799 int32_t il = ntohl(ei[0]); /* index length */
800 int32_t dl = ntohl(ei[1]); /* data length */
801 size_t pvlen = sizeof(il) + sizeof(dl) +
802 (il * sizeof(struct entryInfo_s)) + dl;
806 unsigned char * dataStart;
807 unsigned char * dataEnd;
812 /* Sanity checks on header intro. */
813 if (hdrchkTags(il) || hdrchkData(dl))
817 pe = (entryInfo) &ei[2];
818 dataStart = (unsigned char *) (pe + il);
819 dataEnd = dataStart + dl;
821 h = xcalloc(1, sizeof(*h));
823 h->indexAlloced = il + 1;
826 h->index = xcalloc(h->indexAlloced, sizeof(*h->index));
827 h->flags |= HEADERFLAG_SORTED;
833 if (!(htonl(pe->tag) < HEADER_I18NTABLE)) {
834 h->flags |= HEADERFLAG_LEGACY;
835 entry->info.type = REGION_TAG_TYPE;
836 entry->info.tag = HEADER_IMAGE;
837 entry->info.count = REGION_TAG_COUNT;
838 entry->info.offset = ((unsigned char *)pe - dataStart); /* negative offset */
841 entry->length = pvlen - sizeof(il) - sizeof(dl);
842 rdlen = regionSwab(entry+1, il, 0, pe, dataStart, dataEnd, entry->info.offset);
845 entry->rdlen = rdlen;
852 h->flags &= ~HEADERFLAG_LEGACY;
854 entry->info.type = htonl(pe->type);
855 entry->info.count = htonl(pe->count);
857 if (hdrchkType(entry->info.type))
859 if (hdrchkTags(entry->info.count))
862 { int off = ntohl(pe->offset);
867 size_t nb = REGION_TAG_COUNT;
869 /* XXX Hmm, why the copy? */
870 memcpy(&stei, dataStart + off, nb);
871 rdl = -ntohl(stei[2]); /* negative offset */
872 ril = rdl/sizeof(*pe);
873 if (hdrchkTags(ril) || hdrchkData(rdl))
875 entry->info.tag = htonl(pe->tag);
878 rdl = (ril * sizeof(struct entryInfo_s));
879 entry->info.tag = HEADER_IMAGE;
882 entry->info.offset = -rdl; /* negative offset */
885 entry->length = pvlen - sizeof(il) - sizeof(dl);
886 rdlen = regionSwab(entry+1, ril-1, 0, pe+1, dataStart, dataEnd, entry->info.offset);
889 entry->rdlen = rdlen;
891 if (ril < h->indexUsed) {
892 indexEntry newEntry = entry + ril;
893 int ne = (h->indexUsed - ril);
894 int rid = entry->info.offset+1;
897 /* Load dribble entries from region. */
898 rc = regionSwab(newEntry, ne, 0, pe+ril, dataStart, dataEnd, rid);
903 { indexEntry firstEntry = newEntry;
904 int save = h->indexUsed;
907 /* Dribble entries replace duplicate region entries. */
909 for (j = 0; j < ne; j++, newEntry++) {
910 (void) headerDel(h, newEntry->info.tag);
911 if (newEntry->info.tag == RPMTAG_BASENAMES)
912 (void) headerDel(h, RPMTAG_OLDFILENAMES);
915 /* If any duplicate entries were replaced, move new entries down. */
916 if (h->indexUsed < (save - ne)) {
917 memmove(h->index + h->indexUsed, firstEntry,
918 (ne * sizeof(*entry)));
925 h->flags &= ~HEADERFLAG_SORTED;
927 h->flags |= HEADERFLAG_ALLOCATED;
933 h->index = _free(h->index);
939 Header headerReload(Header h, rpmTag tag)
943 void * uh = doHeaderUnload(h, &length);
953 if (ENTRY_IS_REGION(nh->index)) {
954 if (tag == HEADER_SIGNATURES || tag == HEADER_IMMUTABLE)
955 nh->index[0].info.tag = tag;
960 Header headerCopyLoad(const void * uh)
962 int32_t * ei = (int32_t *) uh;
963 int32_t il = ntohl(ei[0]); /* index length */
964 int32_t dl = ntohl(ei[1]); /* data length */
965 size_t pvlen = sizeof(il) + sizeof(dl) +
966 (il * sizeof(struct entryInfo_s)) + dl;
970 /* Sanity checks on header intro. */
971 if (!(hdrchkTags(il) || hdrchkData(dl)) && pvlen < headerMaxbytes) {
972 nuh = memcpy(xmalloc(pvlen), uh, pvlen);
973 if ((h = headerLoad(nuh)) == NULL)
980 * Read (and load) header from file handle.
981 * @param fd file handle
982 * @param magicp read (and verify) 8 bytes of (magic, 0)?
983 * @return header (or NULL on error)
985 Header headerRead(FD_t fd, enum hMagic magicp)
997 memset(block, 0, sizeof(block));
999 if (magicp == HEADER_MAGIC_YES)
1003 if (timedRead(fd, (char *)block, i*sizeof(*block)) != (i * sizeof(*block)))
1008 if (magicp == HEADER_MAGIC_YES) {
1010 if (memcmp(&magic, rpm_header_magic, sizeof(magic)))
1012 reserved = block[i++];
1015 il = ntohl(block[i]); i++;
1016 dl = ntohl(block[i]); i++;
1018 len = sizeof(il) + sizeof(dl) + (il * sizeof(struct entryInfo_s)) + dl;
1020 /* Sanity checks on header intro. */
1021 if (hdrchkTags(il) || hdrchkData(dl) || len > headerMaxbytes)
1027 len -= sizeof(il) + sizeof(dl);
1030 if (timedRead(fd, (char *)&ei[2], len) != len)
1036 if (h == NULL && ei != NULL) {
1042 int headerWrite(FD_t fd, Header h, enum hMagic magicp)
1050 uh = doHeaderUnload(h, &length);
1054 case HEADER_MAGIC_YES:
1055 nb = Fwrite(rpm_header_magic, sizeof(uint8_t), sizeof(rpm_header_magic), fd);
1056 if (nb != sizeof(rpm_header_magic))
1059 case HEADER_MAGIC_NO:
1063 nb = Fwrite(uh, sizeof(char), length, fd);
1067 return (nb == length ? 0 : 1);
1070 int headerIsEntry(Header h, rpmTag tag)
1072 /* FIX: h modified by sort. */
1073 return (findEntry(h, tag, RPM_NULL_TYPE) ? 1 : 0);
1078 * Retrieve data from header entry.
1079 * Relevant flags (others are ignored), if neither is set allocation
1080 * behavior depends on data type(!)
1081 * HEADERGET_MINMEM: return pointers to header memory
1082 * HEADERGET_ALLOC: always return malloced memory, overrides MINMEM
1084 * @todo Permit retrieval of regions other than HEADER_IMUTABLE.
1085 * @param entry header entry
1086 * @param td tag data container
1087 * @param minMem string pointers refer to header memory?
1088 * @param flags flags to control memory allocation
1089 * @return 1 on success, otherwise error.
1091 static int copyTdEntry(const indexEntry entry, rpmtd td, headerGetFlags flags)
1093 rpm_count_t count = entry->info.count;
1094 int rc = 1; /* XXX 1 on success. */
1095 /* ALLOC overrides MINMEM */
1096 int allocMem = flags & HEADERGET_ALLOC;
1097 int minMem = allocMem ? 0 : flags & HEADERGET_MINMEM;
1098 int argvArray = (flags & HEADERGET_ARGV) ? 1 : 0;
1101 td->flags = RPMTD_IMMUTABLE;
1102 switch (entry->info.type) {
1105 * XXX This only works for
1106 * XXX "sealed" HEADER_IMMUTABLE/HEADER_SIGNATURES/HEADER_IMAGE.
1107 * XXX This will *not* work for unsealed legacy HEADER_IMAGE (i.e.
1108 * XXX a legacy header freshly read, but not yet unloaded to the rpmdb).
1110 if (ENTRY_IS_REGION(entry)) {
1111 int32_t * ei = ((int32_t *)entry->data) - 2;
1112 entryInfo pe = (entryInfo) (ei + 2);
1113 unsigned char * dataStart = (unsigned char *) (pe + ntohl(ei[0]));
1114 int32_t rdl = -entry->info.offset; /* negative offset */
1115 int32_t ril = rdl/sizeof(*pe);
1118 count = 2 * sizeof(*ei) + (ril * sizeof(*pe)) + rdl;
1119 if (entry->info.tag == HEADER_IMAGE) {
1123 count += REGION_TAG_COUNT;
1124 rdl += REGION_TAG_COUNT;
1127 td->data = xmalloc(count);
1128 ei = (int32_t *) td->data;
1132 pe = (entryInfo) memcpy(ei + 2, pe, (ril * sizeof(*pe)));
1134 dataStart = (unsigned char *) memcpy(pe + ril, dataStart, rdl);
1136 rc = regionSwab(NULL, ril, 0, pe, dataStart, dataStart + rdl, 0);
1137 /* don't return data on failure */
1139 td->data = _free(td->data);
1141 /* XXX 1 on success. */
1142 rc = (rc < 0) ? 0 : 1;
1144 count = entry->length;
1146 ? memcpy(xmalloc(count), entry->data, count)
1150 case RPM_STRING_TYPE:
1152 td->data = allocMem ? xstrdup(entry->data) : entry->data;
1155 case RPM_STRING_ARRAY_TYPE:
1156 case RPM_I18NSTRING_TYPE:
1157 { const char ** ptrEntry;
1158 int tableSize = (count + argvArray) * sizeof(char *);
1163 td->data = xmalloc(tableSize);
1164 ptrEntry = (const char **) td->data;
1167 t = xmalloc(tableSize + entry->length);
1168 td->data = (void *)t;
1169 ptrEntry = (const char **) td->data;
1171 memcpy(t, entry->data, entry->length);
1173 for (i = 0; i < count; i++) {
1180 td->flags |= RPMTD_ARGV;
1185 case RPM_INT16_TYPE:
1186 case RPM_INT32_TYPE:
1187 case RPM_INT64_TYPE:
1189 td->data = xmalloc(entry->length);
1190 memcpy(td->data, entry->data, entry->length);
1192 td->data = entry->data;
1196 /* WTH? Don't mess with unknown data types... */
1201 td->type = entry->info.type;
1204 if (td->data && entry->data != td->data) {
1205 td->flags |= RPMTD_ALLOCED;
1212 * Does locale match entry in header i18n table?
1215 * The range [l,le) contains the next locale to match:
1216 * ll[_CC][.EEEEE][@dddd]
1218 * ll ISO language code (in lowercase).
1219 * CC (optional) ISO coutnry code (in uppercase).
1220 * EEEEE (optional) encoding (not really standardized).
1221 * dddd (optional) dialect.
1224 * @param td header i18n table data, NUL terminated
1225 * @param l start of locale to match
1226 * @param le end of locale to match
1227 * @return 1 on good match, 2 on weak match, 0 on no match
1229 static int headerMatchLocale(const char *td, const char *l, const char *le)
1233 /* First try a complete match. */
1234 if (strlen(td) == (le-l) && rstreqn(td, l, (le - l)))
1237 /* Next, try stripping optional dialect and matching. */
1238 for (fe = l; fe < le && *fe != '@'; fe++)
1240 if (fe < le && rstreqn(td, l, (fe - l)))
1243 /* Next, try stripping optional codeset and matching. */
1244 for (fe = l; fe < le && *fe != '.'; fe++)
1246 if (fe < le && rstreqn(td, l, (fe - l)))
1249 /* Finally, try stripping optional country code and matching. */
1250 for (fe = l; fe < le && *fe != '_'; fe++)
1252 if (fe < le && rstreqn(td, l, (fe - l)))
1259 * Return i18n string from header that matches locale.
1261 * @param entry i18n string data
1262 * @retval td tag data container
1263 * @param flags flags to control allocation
1266 static int copyI18NEntry(Header h, indexEntry entry, rpmtd td,
1267 headerGetFlags flags)
1269 const char *lang, *l, *le;
1272 td->type = RPM_STRING_TYPE;
1274 /* if no match, just return the first string */
1275 td->data = entry->data;
1277 /* XXX Drepper sez' this is the order. */
1278 if ((lang = getenv("LANGUAGE")) == NULL &&
1279 (lang = getenv("LC_ALL")) == NULL &&
1280 (lang = getenv("LC_MESSAGES")) == NULL &&
1281 (lang = getenv("LANG")) == NULL)
1284 if ((table = findEntry(h, HEADER_I18NTABLE, RPM_STRING_ARRAY_TYPE)) == NULL)
1287 for (l = lang; *l != '\0'; l = le) {
1289 char *ed, *ed_weak = NULL;
1292 while (*l && *l == ':') /* skip leading colons */
1296 for (le = l; *le && *le != ':'; le++) /* find end of this locale */
1299 /* For each entry in the header ... */
1300 for (langNum = 0, t = table->data, ed = entry->data;
1301 langNum < entry->info.count;
1302 langNum++, t += strlen(t) + 1, ed += strlen(ed) + 1) {
1304 int match = headerMatchLocale(t, l, le);
1308 } else if (match == 2) {
1319 if (flags & HEADERGET_ALLOC) {
1320 td->data = xstrdup(td->data);
1321 td->flags |= RPMTD_ALLOCED;
1328 * Retrieve tag data from header.
1330 * @retval td tag data container
1331 * @param flags flags to control retrieval
1332 * @return 1 on success, 0 on not found
1334 static int intGetTdEntry(Header h, rpmtd td, headerGetFlags flags)
1339 /* First find the tag */
1340 /* FIX: h modified by sort. */
1341 entry = findEntry(h, td->tag, RPM_NULL_TYPE);
1342 if (entry == NULL) {
1343 /* Td is zeroed above, just return... */
1347 if (flags & HEADERGET_RAW) {
1348 rc = copyTdEntry(entry, td, flags);
1350 switch (entry->info.type) {
1351 case RPM_I18NSTRING_TYPE:
1352 rc = copyI18NEntry(h, entry, td, flags);
1355 rc = copyTdEntry(entry, td, flags);
1360 /* XXX 1 on success */
1361 return ((rc == 1) ? 1 : 0);
1364 int headerGet(Header h, rpmTag tag, rpmtd td, headerGetFlags flags)
1367 headerTagTagFunction tagfunc = intGetTdEntry;
1369 if (td == NULL) return 0;
1374 if (flags & HEADERGET_EXT) {
1375 headerTagTagFunction extfunc = rpmHeaderTagFunc(tag);
1376 if (extfunc) tagfunc = extfunc;
1378 rc = tagfunc(h, td, flags);
1380 assert(tag == td->tag);
1386 static void copyData(rpmTagType type, rpm_data_t dstPtr,
1387 rpm_constdata_t srcPtr, rpm_count_t cnt, int dataLength)
1390 case RPM_STRING_ARRAY_TYPE:
1391 case RPM_I18NSTRING_TYPE:
1392 { const char ** av = (const char **) srcPtr;
1395 while (cnt-- > 0 && dataLength > 0) {
1397 if ((s = *av++) == NULL)
1401 } while (s[-1] && --dataLength > 0);
1406 memmove(dstPtr, srcPtr, dataLength);
1412 * Return (malloc'ed) copy of entry data.
1413 * @param type entry data type
1414 * @param p entry data
1415 * @param c entry item count
1416 * @retval lengthPtr no. bytes in returned data
1417 * @return (malloc'ed) copy of entry data, NULL on error
1420 grabData(rpmTagType type, rpm_constdata_t p, rpm_count_t c, int * lengthPtr)
1422 rpm_data_t data = NULL;
1425 length = dataLength(type, p, c, 0, NULL);
1427 data = xmalloc(length);
1428 copyData(type, data, p, c, length);
1432 *lengthPtr = length;
1436 static int intAddEntry(Header h, rpmtd td)
1442 /* Count must always be >= 1 for headerAddEntry. */
1446 if (hdrchkType(td->type))
1448 if (hdrchkData(td->count))
1452 data = grabData(td->type, td->data, td->count, &length);
1453 if (data == NULL || length <= 0)
1456 /* Allocate more index space if necessary */
1457 if (h->indexUsed == h->indexAlloced) {
1458 h->indexAlloced += INDEX_MALLOC_SIZE;
1459 h->index = xrealloc(h->index, h->indexAlloced * sizeof(*h->index));
1462 /* Fill in the index */
1463 entry = h->index + h->indexUsed;
1464 entry->info.tag = td->tag;
1465 entry->info.type = td->type;
1466 entry->info.count = td->count;
1467 entry->info.offset = 0;
1469 entry->length = length;
1471 if (h->indexUsed > 0 && td->tag < h->index[h->indexUsed-1].info.tag)
1472 h->flags &= ~HEADERFLAG_SORTED;
1478 static int intAppendEntry(Header h, rpmtd td)
1483 if (td->type == RPM_STRING_TYPE || td->type == RPM_I18NSTRING_TYPE) {
1484 /* we can't do this */
1488 /* Find the tag entry in the header. */
1489 entry = findEntry(h, td->tag, td->type);
1493 length = dataLength(td->type, td->data, td->count, 0, NULL);
1497 if (ENTRY_IN_REGION(entry)) {
1498 char * t = xmalloc(entry->length + length);
1499 memcpy(t, entry->data, entry->length);
1501 entry->info.offset = 0;
1503 entry->data = xrealloc(entry->data, entry->length + length);
1505 copyData(td->type, ((char *) entry->data) + entry->length,
1506 td->data, td->count, length);
1508 entry->length += length;
1510 entry->info.count += td->count;
1515 int headerPut(Header h, rpmtd td, headerPutFlags flags)
1520 if (flags & HEADERPUT_APPEND) {
1521 rc = findEntry(h, td->tag, td->type) ?
1522 intAppendEntry(h, td) :
1525 rc = intAddEntry(h, td);
1531 * Sanity check data types against tag table before putting. Assume
1532 * append on all array-types.
1534 static int headerPutType(Header h, rpmTag tag, rpmTagType reqtype,
1535 rpm_constdata_t data, rpm_count_t size)
1538 rpmTagType type = rpmTagGetType(tag);
1539 headerPutFlags flags = HEADERPUT_APPEND;
1542 /* Basic sanity checks: type must match and there must be data to put */
1543 if ((type & RPM_MASK_TYPE) != reqtype
1544 || size < 1 || data == NULL || h == NULL) {
1549 * Non-array types can't be appended to. Binary types use size
1550 * for data length, for other non-array types size must be 1.
1552 if ((type & RPM_MASK_RETURN_TYPE) != RPM_ARRAY_RETURN_TYPE) {
1553 flags = HEADERPUT_DEFAULT;
1554 if ((type & RPM_MASK_TYPE) != RPM_BIN_TYPE && size != 1) {
1562 td.type = type & RPM_MASK_TYPE;
1563 td.data = (void *) data;
1566 valid = headerPut(h, &td, flags);
1572 int headerPutString(Header h, rpmTag tag, const char *val)
1574 rpmTagType type = rpmTagGetType(tag) & RPM_MASK_TYPE;
1575 const void *sptr = NULL;
1577 /* string arrays expect char **, arrange that */
1578 if (type == RPM_STRING_ARRAY_TYPE || type == RPM_I18NSTRING_TYPE) {
1580 } else if (type == RPM_STRING_TYPE) {
1586 return headerPutType(h, tag, type, sptr, 1);
1589 int headerPutStringArray(Header h, rpmTag tag, const char **array, rpm_count_t size)
1591 return headerPutType(h, tag, RPM_STRING_ARRAY_TYPE, array, size);
1594 int headerPutChar(Header h, rpmTag tag, char *val, rpm_count_t size)
1596 return headerPutType(h, tag, RPM_CHAR_TYPE, val, size);
1599 int headerPutUint8(Header h, rpmTag tag, uint8_t *val, rpm_count_t size)
1601 return headerPutType(h, tag, RPM_INT8_TYPE, val, size);
1604 int headerPutUint16(Header h, rpmTag tag, uint16_t *val, rpm_count_t size)
1606 return headerPutType(h, tag, RPM_INT16_TYPE, val, size);
1609 int headerPutUint32(Header h, rpmTag tag, uint32_t *val, rpm_count_t size)
1611 return headerPutType(h, tag, RPM_INT32_TYPE, val, size);
1614 int headerPutUint64(Header h, rpmTag tag, uint64_t *val, rpm_count_t size)
1616 return headerPutType(h, tag, RPM_INT64_TYPE, val, size);
1619 int headerPutBin(Header h, rpmTag tag, uint8_t *val, rpm_count_t size)
1621 return headerPutType(h, tag, RPM_BIN_TYPE, val, size);
1624 int headerAddI18NString(Header h, rpmTag tag, const char * string,
1627 indexEntry table, entry;
1628 const char ** strArray;
1631 rpm_count_t i, langNum;
1634 table = findEntry(h, HEADER_I18NTABLE, RPM_STRING_ARRAY_TYPE);
1635 entry = findEntry(h, tag, RPM_I18NSTRING_TYPE);
1637 if (!table && entry)
1638 return 0; /* this shouldn't ever happen!! */
1640 if (!table && !entry) {
1641 const char * charArray[2];
1642 rpm_count_t count = 0;
1644 if (!lang || (lang[0] == 'C' && lang[1] == '\0')) {
1645 charArray[count++] = "C";
1647 charArray[count++] = "C";
1648 charArray[count++] = lang;
1652 td.tag = HEADER_I18NTABLE;
1653 td.type = RPM_STRING_ARRAY_TYPE;
1654 td.data = (void *) charArray;
1656 if (!headerPut(h, &td, HEADERPUT_DEFAULT))
1658 table = findEntry(h, HEADER_I18NTABLE, RPM_STRING_ARRAY_TYPE);
1663 if (!lang) lang = "C";
1665 { const char * l = table->data;
1666 for (langNum = 0; langNum < table->info.count; langNum++) {
1667 if (rstreq(l, lang)) break;
1672 if (langNum >= table->info.count) {
1673 length = strlen(lang) + 1;
1674 if (ENTRY_IN_REGION(table)) {
1675 char * t = xmalloc(table->length + length);
1676 memcpy(t, table->data, table->length);
1678 table->info.offset = 0;
1680 table->data = xrealloc(table->data, table->length + length);
1681 memmove(((char *)table->data) + table->length, lang, length);
1682 table->length += length;
1683 table->info.count++;
1689 strArray = xmalloc(sizeof(*strArray) * (langNum + 1));
1690 for (i = 0; i < langNum; i++)
1692 strArray[langNum] = string;
1696 td.type = RPM_I18NSTRING_TYPE;
1698 td.count = langNum + 1;
1699 rc = headerPut(h, &td, HEADERPUT_DEFAULT);
1702 } else if (langNum >= entry->info.count) {
1703 ghosts = langNum - entry->info.count;
1705 length = strlen(string) + 1 + ghosts;
1706 if (ENTRY_IN_REGION(entry)) {
1707 char * t = xmalloc(entry->length + length);
1708 memcpy(t, entry->data, entry->length);
1710 entry->info.offset = 0;
1712 entry->data = xrealloc(entry->data, entry->length + length);
1714 memset(((char *)entry->data) + entry->length, '\0', ghosts);
1715 memmove(((char *)entry->data) + entry->length + ghosts, string, strlen(string)+1);
1717 entry->length += length;
1718 entry->info.count = langNum + 1;
1720 char *b, *be, *e, *ee, *t;
1723 /* Set beginning/end pointers to previous data */
1724 b = be = e = ee = entry->data;
1725 for (i = 0; i < table->info.count; i++) {
1728 ee += strlen(ee) + 1;
1733 /* Get storage for new buffer */
1735 sn = strlen(string) + 1;
1737 length = bn + sn + en;
1738 t = buf = xmalloc(length);
1740 /* Copy values into new storage */
1743 memcpy(t, string, sn);
1748 /* Replace i18N string array */
1749 entry->length -= strlen(be) + 1;
1750 entry->length += sn;
1752 if (ENTRY_IN_REGION(entry)) {
1753 entry->info.offset = 0;
1755 entry->data = _free(entry->data);
1762 int headerMod(Header h, rpmtd td)
1769 /* First find the tag */
1770 entry = findEntry(h, td->tag, td->type);
1775 data = grabData(td->type, td->data, td->count, &length);
1776 if (data == NULL || length <= 0)
1779 /* make sure entry points to the first occurence of this tag */
1780 while (entry > h->index && (entry - 1)->info.tag == td->tag)
1783 /* free after we've grabbed the new data in case the two are intertwined;
1784 that's a bad idea but at least we won't break */
1785 oldData = entry->data;
1787 entry->info.count = td->count;
1788 entry->info.type = td->type;
1790 entry->length = length;
1792 if (ENTRY_IN_REGION(entry)) {
1793 entry->info.offset = 0;
1795 oldData = _free(oldData);
1801 * Header tag iterator data structure.
1803 struct headerIterator_s {
1804 Header h; /*!< Header being iterated. */
1805 int next_index; /*!< Next tag index. */
1808 HeaderIterator headerFreeIterator(HeaderIterator hi)
1811 hi->h = headerFree(hi->h);
1817 HeaderIterator headerInitIterator(Header h)
1819 HeaderIterator hi = xmalloc(sizeof(*hi));
1823 hi->h = headerLink(h);
1828 int headerNext(HeaderIterator hi, rpmtd td)
1831 int slot = hi->next_index;
1832 indexEntry entry = NULL;
1838 for (slot = hi->next_index; slot < h->indexUsed; slot++) {
1839 entry = h->index + slot;
1840 if (!ENTRY_IS_REGION(entry))
1843 hi->next_index = slot;
1844 if (entry == NULL || slot >= h->indexUsed)
1849 td->tag = entry->info.tag;
1851 rc = copyTdEntry(entry, td, HEADERGET_DEFAULT);
1853 /* XXX 1 on success */
1854 return ((rc == 1) ? 1 : 0);
1858 * Duplicate a header.
1860 * @return new header instance
1862 Header headerCopy(Header h)
1864 Header nh = headerNew();
1868 hi = headerInitIterator(h);
1869 while (headerNext(hi, &td)) {
1870 if (rpmtdCount(&td) > 0) {
1871 (void) headerPut(nh, &td, HEADERPUT_DEFAULT);
1875 hi = headerFreeIterator(hi);
1877 return headerReload(nh, HEADER_IMAGE);
1880 void headerCopyTags(Header headerFrom, Header headerTo,
1881 const rpmTag * tagstocopy)
1886 if (headerFrom == headerTo)
1889 for (p = tagstocopy; *p != 0; p++) {
1890 if (headerIsEntry(headerTo, *p))
1892 if (!headerGet(headerFrom, *p, &td, HEADERGET_MINMEM))
1894 (void) headerPut(headerTo, &td, HEADERPUT_DEFAULT);
1899 unsigned int headerGetInstance(Header h)
1901 return h ? h->instance : 0;
1904 void headerSetInstance(Header h, unsigned int instance)
1906 h->instance = instance;
1909 char * headerGetAsString(Header h, rpmTag tag)
1914 if (headerGet(h, tag, &td, HEADERGET_EXT)) {
1915 if (rpmtdCount(&td) == 1) {
1916 res = rpmtdFormat(&td, RPMTD_FORMAT_STRING, NULL);
1923 const char * headerGetString(Header h, rpmTag tag)
1925 const char *res = NULL;
1928 if (headerGet(h, tag, &td, HEADERGET_MINMEM)) {
1929 if (rpmtdCount(&td) == 1) {
1930 res = rpmtdGetString(&td);
1937 uint64_t headerGetNumber(Header h, rpmTag tag)
1942 if (headerGet(h, tag, &td, HEADERGET_EXT)) {
1943 if (rpmtdCount(&td) == 1) {
1944 res = rpmtdGetNumber(&td);