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) {
405 diff = typeSizes[type] - (dl % typeSizes[type]);
406 if (diff != typeSizes[type]) {
408 if (ieprev.info.type == RPM_I18NSTRING_TYPE)
409 ieprev.length += diff;
413 /* Perform endian conversions */
414 switch (ntohl(pe->type)) {
416 { uint64_t * it = (uint64_t *)t;
417 for (; ie.info.count > 0; ie.info.count--, it += 1) {
418 if (dataEnd && ((unsigned char *)it) >= dataEnd)
422 t = (unsigned char *) it;
425 { int32_t * it = (int32_t *)t;
426 for (; ie.info.count > 0; ie.info.count--, it += 1) {
427 if (dataEnd && ((unsigned char *)it) >= dataEnd)
431 t = (unsigned char *) it;
434 { int16_t * it = (int16_t *) t;
435 for (; ie.info.count > 0; ie.info.count--, it += 1) {
436 if (dataEnd && ((unsigned char *)it) >= dataEnd)
440 t = (unsigned char *) it;
445 ieprev = ie; /* structure assignment */
454 * @retval *lengthPtr no. bytes in unloaded header blob
455 * @return unloaded header blob (NULL on error)
457 static void * doHeaderUnload(Header h,
471 int drlen, ndribbles;
474 /* Sort entries by (offset,tag). */
477 /* Compute (il,dl) for all tags, including those deleted in region. */
479 drlen = ndribbles = driplen = ndrips = 0;
480 for (i = 0, entry = h->index; i < h->indexUsed; i++, entry++) {
481 if (ENTRY_IS_REGION(entry)) {
482 int32_t rdl = -entry->info.offset; /* negative offset */
483 int32_t ril = rdl/sizeof(*pe);
484 int rid = entry->info.offset;
487 dl += entry->rdlen + entry->info.count;
488 /* XXX Legacy regions do not include the region tag and data. */
489 if (i == 0 && (h->flags & HEADERFLAG_LEGACY))
492 /* Skip rest of entries in region, but account for dribbles. */
493 for (; i < h->indexUsed && entry->info.offset <= rid+1; i++, entry++) {
494 if (entry->info.offset <= rid)
498 type = entry->info.type;
499 if (typeSizes[type] > 1) {
501 diff = typeSizes[type] - (dl % typeSizes[type]);
502 if (diff != typeSizes[type]) {
511 drlen += entry->length;
519 /* Ignore deleted drips. */
520 if (entry->data == NULL || entry->length <= 0)
524 type = entry->info.type;
525 if (typeSizes[type] > 1) {
527 diff = typeSizes[type] - (dl % typeSizes[type]);
528 if (diff != typeSizes[type]) {
537 driplen += entry->length;
541 /* Sanity checks on header intro. */
542 if (hdrchkTags(il) || hdrchkData(dl))
545 len = sizeof(il) + sizeof(dl) + (il * sizeof(*pe)) + dl;
551 pe = (entryInfo) &ei[2];
552 dataStart = te = (char *) (pe + il);
555 for (i = 0, entry = h->index; i < h->indexUsed; i++, entry++) {
561 if (entry->data == NULL || entry->length <= 0)
564 t = (unsigned char*)te;
565 pe->tag = htonl(entry->info.tag);
566 pe->type = htonl(entry->info.type);
567 pe->count = htonl(entry->info.count);
569 if (ENTRY_IS_REGION(entry)) {
570 int32_t rdl = -entry->info.offset; /* negative offset */
571 int32_t ril = rdl/sizeof(*pe) + ndribbles;
572 int rid = entry->info.offset;
574 src = (char *)entry->data;
575 rdlen = entry->rdlen;
577 /* XXX Legacy regions do not include the region tag and data. */
578 if (i == 0 && (h->flags & HEADERFLAG_LEGACY)) {
581 memcpy(pe+1, src, rdl);
582 memcpy(te, src + rdl, rdlen);
585 pe->offset = htonl(te - dataStart);
588 stei[2] = htonl(-rdl-entry->info.count);
590 memcpy(te, stei, entry->info.count);
591 te += entry->info.count;
593 rdlen += entry->info.count;
595 count = regionSwab(NULL, ril, 0, pe, t, NULL, 0);
601 memcpy(pe+1, src + sizeof(*pe), ((ril-1) * sizeof(*pe)));
602 memcpy(te, src + (ril * sizeof(*pe)), rdlen+entry->info.count+drlen);
605 entryInfo se = (entryInfo)src;
606 int off = ntohl(se->offset);
607 pe->offset = (off) ? htonl(te - dataStart) : htonl(off);
609 te += entry->info.count + drlen;
611 count = regionSwab(NULL, ril, 0, pe, t, NULL, 0);
612 if (count != (rdlen + entry->info.count + drlen))
616 /* Skip rest of entries in region. */
617 while (i < h->indexUsed && entry->info.offset <= rid+1) {
627 /* Ignore deleted drips. */
628 if (entry->data == NULL || entry->length <= 0)
632 type = entry->info.type;
633 if (typeSizes[type] > 1) {
635 diff = typeSizes[type] - ((te - dataStart) % typeSizes[type]);
636 if (diff != typeSizes[type]) {
643 pe->offset = htonl(te - dataStart);
645 /* copy data w/ endian conversions */
646 switch (entry->info.type) {
648 count = entry->info.count;
651 *((uint64_t *)te) = htonll(*((uint64_t *)src));
652 te += sizeof(uint64_t);
653 src += sizeof(uint64_t);
658 count = entry->info.count;
661 *((int32_t *)te) = htonl(*((int32_t *)src));
662 te += sizeof(int32_t);
663 src += sizeof(int32_t);
668 count = entry->info.count;
671 *((int16_t *)te) = htons(*((int16_t *)src));
672 te += sizeof(int16_t);
673 src += sizeof(int16_t);
678 memcpy(te, entry->data, entry->length);
685 /* Insure that there are no memcpy underruns/overruns. */
686 if (((char *)pe) != dataStart)
688 if ((((char *)ei)+len) != te)
694 h->flags &= ~HEADERFLAG_SORTED;
704 void * headerUnload(Header h)
707 void * uh = doHeaderUnload(h, &length);
712 * Find matching (tag,type) entry in header.
714 * @param tag entry tag
715 * @param type entry type
716 * @return header entry
719 indexEntry findEntry(Header h, rpmTag tag, rpmTagType type)
721 indexEntry entry, entry2, last;
722 struct indexEntry_s key;
724 if (h == NULL) return NULL;
725 if (!(h->flags & HEADERFLAG_SORTED)) headerSort(h);
730 bsearch(&key, h->index, h->indexUsed, sizeof(*h->index), indexCmp);
734 if (type == RPM_NULL_TYPE)
738 while (entry->info.tag == tag && entry->info.type != type &&
739 entry > h->index) entry--;
741 if (entry->info.tag == tag && entry->info.type == type)
744 last = h->index + h->indexUsed;
745 /* FIX: entry2 = entry. Code looks bogus as well. */
746 while (entry2->info.tag == tag && entry2->info.type != type &&
747 entry2 < last) entry2++;
749 if (entry->info.tag == tag && entry->info.type == type)
755 int headerDel(Header h, rpmTag tag)
757 indexEntry last = h->index + h->indexUsed;
758 indexEntry entry, first;
761 entry = findEntry(h, tag, RPM_NULL_TYPE);
762 if (!entry) return 1;
764 /* Make sure entry points to the first occurence of this tag. */
765 while (entry > h->index && (entry - 1)->info.tag == tag)
768 /* Free data for tags being removed. */
769 for (first = entry; first < last; first++) {
771 if (first->info.tag != tag)
776 if (ENTRY_IN_REGION(first))
781 ne = (first - entry);
786 memmove(entry, first, (ne * sizeof(*entry)));
792 Header headerLoad(void * uh)
794 int32_t * ei = (int32_t *) uh;
795 int32_t il = ntohl(ei[0]); /* index length */
796 int32_t dl = ntohl(ei[1]); /* data length */
797 size_t pvlen = sizeof(il) + sizeof(dl) +
798 (il * sizeof(struct entryInfo_s)) + dl;
802 unsigned char * dataStart;
803 unsigned char * dataEnd;
808 /* Sanity checks on header intro. */
809 if (hdrchkTags(il) || hdrchkData(dl))
813 pe = (entryInfo) &ei[2];
814 dataStart = (unsigned char *) (pe + il);
815 dataEnd = dataStart + dl;
817 h = xcalloc(1, sizeof(*h));
819 h->indexAlloced = il + 1;
822 h->index = xcalloc(h->indexAlloced, sizeof(*h->index));
823 h->flags |= HEADERFLAG_SORTED;
829 if (!(htonl(pe->tag) < HEADER_I18NTABLE)) {
830 h->flags |= HEADERFLAG_LEGACY;
831 entry->info.type = REGION_TAG_TYPE;
832 entry->info.tag = HEADER_IMAGE;
833 entry->info.count = REGION_TAG_COUNT;
834 entry->info.offset = ((unsigned char *)pe - dataStart); /* negative offset */
837 entry->length = pvlen - sizeof(il) - sizeof(dl);
838 rdlen = regionSwab(entry+1, il, 0, pe, dataStart, dataEnd, entry->info.offset);
841 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)
1046 uh = doHeaderUnload(h, &length);
1050 case HEADER_MAGIC_YES:
1051 nb = Fwrite(rpm_header_magic, sizeof(uint8_t), sizeof(rpm_header_magic), fd);
1052 if (nb != sizeof(rpm_header_magic))
1055 case HEADER_MAGIC_NO:
1059 nb = Fwrite(uh, sizeof(char), length, fd);
1063 return (nb == length ? 0 : 1);
1066 int headerIsEntry(Header h, rpmTag tag)
1068 /* FIX: h modified by sort. */
1069 return (findEntry(h, tag, RPM_NULL_TYPE) ? 1 : 0);
1074 * Retrieve data from header entry.
1075 * Relevant flags (others are ignored), if neither is set allocation
1076 * behavior depends on data type(!)
1077 * HEADERGET_MINMEM: return pointers to header memory
1078 * HEADERGET_ALLOC: always return malloced memory, overrides MINMEM
1080 * @todo Permit retrieval of regions other than HEADER_IMUTABLE.
1081 * @param entry header entry
1082 * @param td tag data container
1083 * @param minMem string pointers refer to header memory?
1084 * @param flags flags to control memory allocation
1085 * @return 1 on success, otherwise error.
1087 static int copyTdEntry(const indexEntry entry, rpmtd td, headerGetFlags flags)
1089 rpm_count_t count = entry->info.count;
1090 int rc = 1; /* XXX 1 on success. */
1091 /* ALLOC overrides MINMEM */
1092 int allocMem = flags & HEADERGET_ALLOC;
1093 int minMem = allocMem ? 0 : flags & HEADERGET_MINMEM;
1094 int argvArray = (flags & HEADERGET_ARGV) ? 1 : 0;
1097 td->flags = RPMTD_IMMUTABLE;
1098 switch (entry->info.type) {
1101 * XXX This only works for
1102 * XXX "sealed" HEADER_IMMUTABLE/HEADER_SIGNATURES/HEADER_IMAGE.
1103 * XXX This will *not* work for unsealed legacy HEADER_IMAGE (i.e.
1104 * XXX a legacy header freshly read, but not yet unloaded to the rpmdb).
1106 if (ENTRY_IS_REGION(entry)) {
1107 int32_t * ei = ((int32_t *)entry->data) - 2;
1108 entryInfo pe = (entryInfo) (ei + 2);
1109 unsigned char * dataStart = (unsigned char *) (pe + ntohl(ei[0]));
1110 int32_t rdl = -entry->info.offset; /* negative offset */
1111 int32_t ril = rdl/sizeof(*pe);
1114 count = 2 * sizeof(*ei) + (ril * sizeof(*pe)) + rdl;
1115 if (entry->info.tag == HEADER_IMAGE) {
1119 count += REGION_TAG_COUNT;
1120 rdl += REGION_TAG_COUNT;
1123 td->data = xmalloc(count);
1124 ei = (int32_t *) td->data;
1128 pe = (entryInfo) memcpy(ei + 2, pe, (ril * sizeof(*pe)));
1130 dataStart = (unsigned char *) memcpy(pe + ril, dataStart, rdl);
1132 rc = regionSwab(NULL, ril, 0, pe, dataStart, dataStart + rdl, 0);
1133 /* don't return data on failure */
1135 td->data = _free(td->data);
1137 /* XXX 1 on success. */
1138 rc = (rc < 0) ? 0 : 1;
1140 count = entry->length;
1142 ? memcpy(xmalloc(count), entry->data, count)
1146 case RPM_STRING_TYPE:
1148 td->data = allocMem ? xstrdup(entry->data) : entry->data;
1151 case RPM_STRING_ARRAY_TYPE:
1152 case RPM_I18NSTRING_TYPE:
1153 { const char ** ptrEntry;
1154 int tableSize = (count + argvArray) * sizeof(char *);
1159 td->data = xmalloc(tableSize);
1160 ptrEntry = (const char **) td->data;
1163 t = xmalloc(tableSize + entry->length);
1164 td->data = (void *)t;
1165 ptrEntry = (const char **) td->data;
1167 memcpy(t, entry->data, entry->length);
1169 for (i = 0; i < count; i++) {
1176 td->flags |= RPMTD_ARGV;
1181 case RPM_INT16_TYPE:
1182 case RPM_INT32_TYPE:
1183 case RPM_INT64_TYPE:
1185 td->data = xmalloc(entry->length);
1186 memcpy(td->data, entry->data, entry->length);
1188 td->data = entry->data;
1192 /* WTH? Don't mess with unknown data types... */
1197 td->type = entry->info.type;
1200 if (td->data && entry->data != td->data) {
1201 td->flags |= RPMTD_ALLOCED;
1208 * Does locale match entry in header i18n table?
1211 * The range [l,le) contains the next locale to match:
1212 * ll[_CC][.EEEEE][@dddd]
1214 * ll ISO language code (in lowercase).
1215 * CC (optional) ISO coutnry code (in uppercase).
1216 * EEEEE (optional) encoding (not really standardized).
1217 * dddd (optional) dialect.
1220 * @param td header i18n table data, NUL terminated
1221 * @param l start of locale to match
1222 * @param le end of locale to match
1223 * @return 1 on good match, 2 on weak match, 0 on no match
1225 static int headerMatchLocale(const char *td, const char *l, const char *le)
1229 /* First try a complete match. */
1230 if (strlen(td) == (le-l) && rstreqn(td, l, (le - l)))
1233 /* Next, try stripping optional dialect and matching. */
1234 for (fe = l; fe < le && *fe != '@'; fe++)
1236 if (fe < le && rstreqn(td, l, (fe - l)))
1239 /* Next, try stripping optional codeset and matching. */
1240 for (fe = l; fe < le && *fe != '.'; fe++)
1242 if (fe < le && rstreqn(td, l, (fe - l)))
1245 /* Finally, try stripping optional country code and matching. */
1246 for (fe = l; fe < le && *fe != '_'; fe++)
1248 if (fe < le && rstreqn(td, l, (fe - l)))
1255 * Return i18n string from header that matches locale.
1257 * @param entry i18n string data
1258 * @retval td tag data container
1259 * @param flags flags to control allocation
1262 static int copyI18NEntry(Header h, indexEntry entry, rpmtd td,
1263 headerGetFlags flags)
1265 const char *lang, *l, *le;
1268 td->type = RPM_STRING_TYPE;
1270 /* if no match, just return the first string */
1271 td->data = entry->data;
1273 /* XXX Drepper sez' this is the order. */
1274 if ((lang = getenv("LANGUAGE")) == NULL &&
1275 (lang = getenv("LC_ALL")) == NULL &&
1276 (lang = getenv("LC_MESSAGES")) == NULL &&
1277 (lang = getenv("LANG")) == NULL)
1280 if ((table = findEntry(h, HEADER_I18NTABLE, RPM_STRING_ARRAY_TYPE)) == NULL)
1283 for (l = lang; *l != '\0'; l = le) {
1285 char *ed, *ed_weak = NULL;
1288 while (*l && *l == ':') /* skip leading colons */
1292 for (le = l; *le && *le != ':'; le++) /* find end of this locale */
1295 /* For each entry in the header ... */
1296 for (langNum = 0, t = table->data, ed = entry->data;
1297 langNum < entry->info.count;
1298 langNum++, t += strlen(t) + 1, ed += strlen(ed) + 1) {
1300 int match = headerMatchLocale(t, l, le);
1304 } else if (match == 2) {
1315 if (flags & HEADERGET_ALLOC) {
1316 td->data = xstrdup(td->data);
1317 td->flags |= RPMTD_ALLOCED;
1324 * Retrieve tag data from header.
1326 * @retval td tag data container
1327 * @param flags flags to control retrieval
1328 * @return 1 on success, 0 on not found
1330 static int intGetTdEntry(Header h, rpmtd td, headerGetFlags flags)
1335 /* First find the tag */
1336 /* FIX: h modified by sort. */
1337 entry = findEntry(h, td->tag, RPM_NULL_TYPE);
1338 if (entry == NULL) {
1339 /* Td is zeroed above, just return... */
1343 if (flags & HEADERGET_RAW) {
1344 rc = copyTdEntry(entry, td, flags);
1346 switch (entry->info.type) {
1347 case RPM_I18NSTRING_TYPE:
1348 rc = copyI18NEntry(h, entry, td, flags);
1351 rc = copyTdEntry(entry, td, flags);
1356 /* XXX 1 on success */
1357 return ((rc == 1) ? 1 : 0);
1360 int headerGet(Header h, rpmTag tag, rpmtd td, headerGetFlags flags)
1363 headerTagTagFunction tagfunc = intGetTdEntry;
1365 if (td == NULL) return 0;
1370 if (flags & HEADERGET_EXT) {
1371 headerTagTagFunction extfunc = rpmHeaderTagFunc(tag);
1372 if (extfunc) tagfunc = extfunc;
1374 rc = tagfunc(h, td, flags);
1376 assert(tag == td->tag);
1382 static void copyData(rpmTagType type, rpm_data_t dstPtr,
1383 rpm_constdata_t srcPtr, rpm_count_t cnt, int dataLength)
1386 case RPM_STRING_ARRAY_TYPE:
1387 case RPM_I18NSTRING_TYPE:
1388 { const char ** av = (const char **) srcPtr;
1391 while (cnt-- > 0 && dataLength > 0) {
1393 if ((s = *av++) == NULL)
1397 } while (s[-1] && --dataLength > 0);
1402 memmove(dstPtr, srcPtr, dataLength);
1408 * Return (malloc'ed) copy of entry data.
1409 * @param type entry data type
1410 * @param p entry data
1411 * @param c entry item count
1412 * @retval lengthPtr no. bytes in returned data
1413 * @return (malloc'ed) copy of entry data, NULL on error
1416 grabData(rpmTagType type, rpm_constdata_t p, rpm_count_t c, int * lengthPtr)
1418 rpm_data_t data = NULL;
1421 length = dataLength(type, p, c, 0, NULL);
1423 data = xmalloc(length);
1424 copyData(type, data, p, c, length);
1428 *lengthPtr = length;
1432 static int intAddEntry(Header h, rpmtd td)
1438 /* Count must always be >= 1 for headerAddEntry. */
1442 if (hdrchkType(td->type))
1444 if (hdrchkData(td->count))
1448 data = grabData(td->type, td->data, td->count, &length);
1449 if (data == NULL || length <= 0)
1452 /* Allocate more index space if necessary */
1453 if (h->indexUsed == h->indexAlloced) {
1454 h->indexAlloced += INDEX_MALLOC_SIZE;
1455 h->index = xrealloc(h->index, h->indexAlloced * sizeof(*h->index));
1458 /* Fill in the index */
1459 entry = h->index + h->indexUsed;
1460 entry->info.tag = td->tag;
1461 entry->info.type = td->type;
1462 entry->info.count = td->count;
1463 entry->info.offset = 0;
1465 entry->length = length;
1467 if (h->indexUsed > 0 && td->tag < h->index[h->indexUsed-1].info.tag)
1468 h->flags &= ~HEADERFLAG_SORTED;
1474 static int intAppendEntry(Header h, rpmtd td)
1479 if (td->type == RPM_STRING_TYPE || td->type == RPM_I18NSTRING_TYPE) {
1480 /* we can't do this */
1484 /* Find the tag entry in the header. */
1485 entry = findEntry(h, td->tag, td->type);
1489 length = dataLength(td->type, td->data, td->count, 0, NULL);
1493 if (ENTRY_IN_REGION(entry)) {
1494 char * t = xmalloc(entry->length + length);
1495 memcpy(t, entry->data, entry->length);
1497 entry->info.offset = 0;
1499 entry->data = xrealloc(entry->data, entry->length + length);
1501 copyData(td->type, ((char *) entry->data) + entry->length,
1502 td->data, td->count, length);
1504 entry->length += length;
1506 entry->info.count += td->count;
1511 int headerPut(Header h, rpmtd td, headerPutFlags flags)
1516 if (flags & HEADERPUT_APPEND) {
1517 rc = findEntry(h, td->tag, td->type) ?
1518 intAppendEntry(h, td) :
1521 rc = intAddEntry(h, td);
1527 * Sanity check data types against tag table before putting. Assume
1528 * append on all array-types.
1530 static int headerPutType(Header h, rpmTag tag, rpmTagType reqtype,
1531 rpm_constdata_t data, rpm_count_t size)
1534 rpmTagType type = rpmTagGetType(tag);
1535 headerPutFlags flags = HEADERPUT_APPEND;
1538 /* Basic sanity checks: type must match and there must be data to put */
1539 if ((type & RPM_MASK_TYPE) != reqtype
1540 || size < 1 || data == NULL || h == NULL) {
1545 * Non-array types can't be appended to. Binary types use size
1546 * for data length, for other non-array types size must be 1.
1548 if ((type & RPM_MASK_RETURN_TYPE) != RPM_ARRAY_RETURN_TYPE) {
1549 flags = HEADERPUT_DEFAULT;
1550 if ((type & RPM_MASK_TYPE) != RPM_BIN_TYPE && size != 1) {
1558 td.type = type & RPM_MASK_TYPE;
1559 td.data = (void *) data;
1562 valid = headerPut(h, &td, flags);
1568 int headerPutString(Header h, rpmTag tag, const char *val)
1570 rpmTagType type = rpmTagGetType(tag) & RPM_MASK_TYPE;
1571 const void *sptr = NULL;
1573 /* string arrays expect char **, arrange that */
1574 if (type == RPM_STRING_ARRAY_TYPE || type == RPM_I18NSTRING_TYPE) {
1576 } else if (type == RPM_STRING_TYPE) {
1582 return headerPutType(h, tag, type, sptr, 1);
1585 int headerPutStringArray(Header h, rpmTag tag, const char **array, rpm_count_t size)
1587 return headerPutType(h, tag, RPM_STRING_ARRAY_TYPE, array, size);
1590 int headerPutChar(Header h, rpmTag tag, char *val, rpm_count_t size)
1592 return headerPutType(h, tag, RPM_CHAR_TYPE, val, size);
1595 int headerPutUint8(Header h, rpmTag tag, uint8_t *val, rpm_count_t size)
1597 return headerPutType(h, tag, RPM_INT8_TYPE, val, size);
1600 int headerPutUint16(Header h, rpmTag tag, uint16_t *val, rpm_count_t size)
1602 return headerPutType(h, tag, RPM_INT16_TYPE, val, size);
1605 int headerPutUint32(Header h, rpmTag tag, uint32_t *val, rpm_count_t size)
1607 return headerPutType(h, tag, RPM_INT32_TYPE, val, size);
1610 int headerPutUint64(Header h, rpmTag tag, uint64_t *val, rpm_count_t size)
1612 return headerPutType(h, tag, RPM_INT64_TYPE, val, size);
1615 int headerPutBin(Header h, rpmTag tag, uint8_t *val, rpm_count_t size)
1617 return headerPutType(h, tag, RPM_BIN_TYPE, val, size);
1620 int headerAddI18NString(Header h, rpmTag tag, const char * string,
1623 indexEntry table, entry;
1624 const char ** strArray;
1627 rpm_count_t i, langNum;
1630 table = findEntry(h, HEADER_I18NTABLE, RPM_STRING_ARRAY_TYPE);
1631 entry = findEntry(h, tag, RPM_I18NSTRING_TYPE);
1633 if (!table && entry)
1634 return 0; /* this shouldn't ever happen!! */
1636 if (!table && !entry) {
1637 const char * charArray[2];
1638 rpm_count_t count = 0;
1640 if (!lang || (lang[0] == 'C' && lang[1] == '\0')) {
1641 charArray[count++] = "C";
1643 charArray[count++] = "C";
1644 charArray[count++] = lang;
1648 td.tag = HEADER_I18NTABLE;
1649 td.type = RPM_STRING_ARRAY_TYPE;
1650 td.data = (void *) charArray;
1652 if (!headerPut(h, &td, HEADERPUT_DEFAULT))
1654 table = findEntry(h, HEADER_I18NTABLE, RPM_STRING_ARRAY_TYPE);
1659 if (!lang) lang = "C";
1661 { const char * l = table->data;
1662 for (langNum = 0; langNum < table->info.count; langNum++) {
1663 if (rstreq(l, lang)) break;
1668 if (langNum >= table->info.count) {
1669 length = strlen(lang) + 1;
1670 if (ENTRY_IN_REGION(table)) {
1671 char * t = xmalloc(table->length + length);
1672 memcpy(t, table->data, table->length);
1674 table->info.offset = 0;
1676 table->data = xrealloc(table->data, table->length + length);
1677 memmove(((char *)table->data) + table->length, lang, length);
1678 table->length += length;
1679 table->info.count++;
1685 strArray = xmalloc(sizeof(*strArray) * (langNum + 1));
1686 for (i = 0; i < langNum; i++)
1688 strArray[langNum] = string;
1692 td.type = RPM_I18NSTRING_TYPE;
1694 td.count = langNum + 1;
1695 rc = headerPut(h, &td, HEADERPUT_DEFAULT);
1698 } else if (langNum >= entry->info.count) {
1699 ghosts = langNum - entry->info.count;
1701 length = strlen(string) + 1 + ghosts;
1702 if (ENTRY_IN_REGION(entry)) {
1703 char * t = xmalloc(entry->length + length);
1704 memcpy(t, entry->data, entry->length);
1706 entry->info.offset = 0;
1708 entry->data = xrealloc(entry->data, entry->length + length);
1710 memset(((char *)entry->data) + entry->length, '\0', ghosts);
1711 memmove(((char *)entry->data) + entry->length + ghosts, string, strlen(string)+1);
1713 entry->length += length;
1714 entry->info.count = langNum + 1;
1716 char *b, *be, *e, *ee, *t;
1719 /* Set beginning/end pointers to previous data */
1720 b = be = e = ee = entry->data;
1721 for (i = 0; i < table->info.count; i++) {
1724 ee += strlen(ee) + 1;
1729 /* Get storage for new buffer */
1731 sn = strlen(string) + 1;
1733 length = bn + sn + en;
1734 t = buf = xmalloc(length);
1736 /* Copy values into new storage */
1739 memcpy(t, string, sn);
1744 /* Replace i18N string array */
1745 entry->length -= strlen(be) + 1;
1746 entry->length += sn;
1748 if (ENTRY_IN_REGION(entry)) {
1749 entry->info.offset = 0;
1751 entry->data = _free(entry->data);
1758 int headerMod(Header h, rpmtd td)
1765 /* First find the tag */
1766 entry = findEntry(h, td->tag, td->type);
1771 data = grabData(td->type, td->data, td->count, &length);
1772 if (data == NULL || length <= 0)
1775 /* make sure entry points to the first occurence of this tag */
1776 while (entry > h->index && (entry - 1)->info.tag == td->tag)
1779 /* free after we've grabbed the new data in case the two are intertwined;
1780 that's a bad idea but at least we won't break */
1781 oldData = entry->data;
1783 entry->info.count = td->count;
1784 entry->info.type = td->type;
1786 entry->length = length;
1788 if (ENTRY_IN_REGION(entry)) {
1789 entry->info.offset = 0;
1791 oldData = _free(oldData);
1797 * Header tag iterator data structure.
1799 struct headerIterator_s {
1800 Header h; /*!< Header being iterated. */
1801 int next_index; /*!< Next tag index. */
1804 HeaderIterator headerFreeIterator(HeaderIterator hi)
1807 hi->h = headerFree(hi->h);
1813 HeaderIterator headerInitIterator(Header h)
1815 HeaderIterator hi = xmalloc(sizeof(*hi));
1819 hi->h = headerLink(h);
1824 int headerNext(HeaderIterator hi, rpmtd td)
1828 indexEntry entry = NULL;
1834 for (slot = hi->next_index; slot < h->indexUsed; slot++) {
1835 entry = h->index + slot;
1836 if (!ENTRY_IS_REGION(entry))
1839 hi->next_index = slot;
1840 if (entry == NULL || slot >= h->indexUsed)
1845 td->tag = entry->info.tag;
1847 rc = copyTdEntry(entry, td, HEADERGET_DEFAULT);
1849 /* XXX 1 on success */
1850 return ((rc == 1) ? 1 : 0);
1854 * Duplicate a header.
1856 * @return new header instance
1858 Header headerCopy(Header h)
1860 Header nh = headerNew();
1864 hi = headerInitIterator(h);
1865 while (headerNext(hi, &td)) {
1866 if (rpmtdCount(&td) > 0) {
1867 (void) headerPut(nh, &td, HEADERPUT_DEFAULT);
1871 hi = headerFreeIterator(hi);
1873 return headerReload(nh, HEADER_IMAGE);
1876 void headerCopyTags(Header headerFrom, Header headerTo,
1877 const rpmTag * tagstocopy)
1882 if (headerFrom == headerTo)
1885 for (p = tagstocopy; *p != 0; p++) {
1886 if (headerIsEntry(headerTo, *p))
1888 if (!headerGet(headerFrom, *p, &td, HEADERGET_MINMEM))
1890 (void) headerPut(headerTo, &td, HEADERPUT_DEFAULT);
1895 unsigned int headerGetInstance(Header h)
1897 return h ? h->instance : 0;
1900 void headerSetInstance(Header h, unsigned int instance)
1902 h->instance = instance;
1905 char * headerGetAsString(Header h, rpmTag tag)
1910 if (headerGet(h, tag, &td, HEADERGET_EXT)) {
1911 if (rpmtdCount(&td) == 1) {
1912 res = rpmtdFormat(&td, RPMTD_FORMAT_STRING, NULL);
1919 const char * headerGetString(Header h, rpmTag tag)
1921 const char *res = NULL;
1924 if (headerGet(h, tag, &td, HEADERGET_MINMEM)) {
1925 if (rpmtdCount(&td) == 1) {
1926 res = rpmtdGetString(&td);
1933 uint64_t headerGetNumber(Header h, rpmTag tag)
1938 if (headerGet(h, tag, &td, HEADERGET_EXT)) {
1939 if (rpmtdCount(&td) == 1) {
1940 res = rpmtdGetNumber(&td);