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 static unsigned char const header_magic[8] = {
24 0x8e, 0xad, 0xe8, 0x01, 0x00, 0x00, 0x00, 0x00
28 * Alignment needed for header data types.
30 static const int typeAlign[16] = {
31 1, /*!< RPM_NULL_TYPE */
32 1, /*!< RPM_CHAR_TYPE */
33 1, /*!< RPM_INT8_TYPE */
34 2, /*!< RPM_INT16_TYPE */
35 4, /*!< RPM_INT32_TYPE */
36 8, /*!< RPM_INT64_TYPE */
37 1, /*!< RPM_STRING_TYPE */
38 1, /*!< RPM_BIN_TYPE */
39 1, /*!< RPM_STRING_ARRAY_TYPE */
40 1, /*!< RPM_I18NSTRING_TYPE */
50 * Size of header data types.
52 static const int typeSizes[16] = {
53 0, /*!< RPM_NULL_TYPE */
54 1, /*!< RPM_CHAR_TYPE */
55 1, /*!< RPM_INT8_TYPE */
56 2, /*!< RPM_INT16_TYPE */
57 4, /*!< RPM_INT32_TYPE */
58 8, /*!< RPM_INT64_TYPE */
59 -1, /*!< RPM_STRING_TYPE */
60 1, /*!< RPM_BIN_TYPE */
61 -1, /*!< RPM_STRING_ARRAY_TYPE */
62 -1, /*!< RPM_I18NSTRING_TYPE */
71 /* dumb macro to avoid 50 copies of this code while converting... */
83 * Maximum no. of bytes permitted in a header.
85 static const size_t headerMaxbytes = (32*1024*1024);
88 * HEADER_EXT_TAG format function prototype.
89 * This is allowed to fail, which indicates the tag doesn't exist.
92 * @retval td tag data container
93 * @return 0 on success
95 typedef int (*headerTagTagFunction) (Header h, rpmtd td);
97 extern void *rpmHeaderTagFunc(rpmTag tag);
99 Header headerLink(Header h)
101 if (h == NULL) return NULL;
105 fprintf(stderr, "--> h %p ++ %d at %s:%u\n", h, h->nrefs, __FILE__, __LINE__);
110 Header headerUnlink(Header h)
112 if (h == NULL) return NULL;
114 fprintf(stderr, "--> h %p -- %d at %s:%u\n", h, h->nrefs, __FILE__, __LINE__);
119 Header headerFree(Header h)
121 (void) headerUnlink(h);
123 if (h == NULL || h->nrefs > 0)
124 return NULL; /* XXX return previous header? */
127 indexEntry entry = h->index;
129 for (i = 0; i < h->indexUsed; i++, entry++) {
130 if ((h->flags & HEADERFLAG_ALLOCATED) && ENTRY_IS_REGION(entry)) {
131 if (entry->length > 0) {
132 int32_t * ei = entry->data;
133 if ((ei - 2) == h->blob) h->blob = _free(h->blob);
136 } else if (!ENTRY_IN_REGION(entry)) {
137 entry->data = _free(entry->data);
141 h->index = _free(h->index);
148 Header headerNew(void)
150 Header h = xcalloc(1, sizeof(*h));
153 h->indexAlloced = INDEX_MALLOC_SIZE;
155 h->flags |= HEADERFLAG_SORTED;
157 h->index = (h->indexAlloced
158 ? xcalloc(h->indexAlloced, sizeof(*h->index))
162 return headerLink(h);
165 int headerVerifyInfo(int il, int dl, const void * pev, void * iv, int negate)
167 entryInfo pe = (entryInfo) pev;
171 for (i = 0; i < il; i++) {
172 info->tag = ntohl(pe[i].tag);
173 info->type = ntohl(pe[i].type);
174 info->offset = ntohl(pe[i].offset);
176 info->offset = -info->offset;
177 info->count = ntohl(pe[i].count);
179 if (hdrchkType(info->type))
181 if (hdrchkAlign(info->type, info->offset))
183 if (!negate && hdrchkRange(dl, info->offset))
185 if (hdrchkData(info->count))
194 static int indexCmp(const void * avp, const void * bvp)
196 indexEntry ap = (indexEntry) avp, bp = (indexEntry) bvp;
197 return (ap->info.tag - bp->info.tag);
200 void headerSort(Header h)
202 if (!(h->flags & HEADERFLAG_SORTED)) {
203 qsort(h->index, h->indexUsed, sizeof(*h->index), indexCmp);
204 h->flags |= HEADERFLAG_SORTED;
210 static int offsetCmp(const void * avp, const void * bvp)
212 indexEntry ap = (indexEntry) avp, bp = (indexEntry) bvp;
213 int rc = (ap->info.offset - bp->info.offset);
216 /* Within a region, entries sort by address. Added drips sort by tag. */
217 if (ap->info.offset < 0)
218 rc = (((char *)ap->data) - ((char *)bp->data));
220 rc = (ap->info.tag - bp->info.tag);
226 * Restore tags in header to original ordering.
229 void headerUnsort(Header h)
231 qsort(h->index, h->indexUsed, sizeof(*h->index), offsetCmp);
234 unsigned headerSizeof(Header h, enum hMagic magicp)
237 unsigned int size = 0;
238 unsigned int pad = 0;
247 case HEADER_MAGIC_YES:
248 size += sizeof(header_magic);
250 case HEADER_MAGIC_NO:
254 size += 2 * sizeof(int32_t); /* count of index entries */
256 for (i = 0, entry = h->index; i < h->indexUsed; i++, entry++) {
260 /* Regions go in as is ... */
261 if (ENTRY_IS_REGION(entry)) {
262 size += entry->length;
263 /* XXX Legacy regions do not include the region tag and data. */
264 if (i == 0 && (h->flags & HEADERFLAG_LEGACY))
265 size += sizeof(struct entryInfo_s) + entry->info.count;
269 /* ... and region elements are skipped. */
270 if (entry->info.offset < 0)
274 type = entry->info.type;
275 if (typeSizes[type] > 1) {
276 diff = typeSizes[type] - (size % typeSizes[type]);
277 if (diff != typeSizes[type]) {
283 size += sizeof(struct entryInfo_s) + entry->length;
290 * Return length of entry data.
291 * @param type entry data type
292 * @param p entry data
293 * @param count entry item count
294 * @param onDisk data is concatenated strings (with NUL's))?
295 * @param pend pointer to end of data (or NULL)
296 * @return no. bytes in data, -1 on failure
298 static int dataLength(rpmTagType type, rpm_constdata_t p, rpm_count_t count,
299 int onDisk, rpm_constdata_t pend)
301 const unsigned char * s = p;
302 const unsigned char * se = pend;
306 case RPM_STRING_TYPE:
314 length++; /* count nul terminator too. */
317 case RPM_STRING_ARRAY_TYPE:
318 case RPM_I18NSTRING_TYPE:
319 /* These are like RPM_STRING_TYPE, except they're *always* an array */
320 /* Compute sum of length of all strings, including nul terminators */
324 length++; /* count nul terminator too */
332 const char ** av = (const char **)p;
334 /* add one for null termination */
335 length += strlen(*av++) + 1;
341 if (typeSizes[type] == -1)
343 length = typeSizes[(type & 0xf)] * count;
344 if (length < 0 || (se && (s + length) > se))
353 * Swap int32_t and int16_t arrays within header region.
355 * This code is way more twisty than I would like.
357 * A bug with RPM_I18NSTRING_TYPE in rpm-2.5.x (fixed in August 1998)
358 * causes the offset and length of elements in a header region to disagree
359 * regarding the total length of the region data.
361 * The "fix" is to compute the size using both offset and length and
362 * return the larger of the two numbers as the size of the region.
363 * Kinda like computing left and right Riemann sums of the data elements
364 * to determine the size of a data structure, go figger :-).
366 * There's one other twist if a header region tag is in the set to be swabbed,
367 * as the data for a header region is located after all other tag data.
369 * @param entry header entry
370 * @param il no. of entries
371 * @param dl start no. bytes of data
372 * @param pe header physical entry pointer (swapped)
373 * @param dataStart header data start
374 * @param dataEnd header data end
375 * @param regionid region offset
376 * @return no. bytes of data in region, -1 on error
378 static int regionSwab(indexEntry entry, int il, int dl,
380 unsigned char * dataStart,
381 const unsigned char * dataEnd,
384 unsigned char * tprev = NULL;
385 unsigned char * t = NULL;
388 struct indexEntry_s ieprev;
390 memset(&ieprev, 0, sizeof(ieprev));
391 for (; il > 0; il--, pe++) {
392 struct indexEntry_s ie;
395 ie.info.tag = ntohl(pe->tag);
396 ie.info.type = ntohl(pe->type);
397 ie.info.count = ntohl(pe->count);
398 ie.info.offset = ntohl(pe->offset);
400 if (hdrchkType(ie.info.type))
402 if (hdrchkData(ie.info.count))
404 if (hdrchkData(ie.info.offset))
406 if (hdrchkAlign(ie.info.type, ie.info.offset))
409 ie.data = t = dataStart + ie.info.offset;
410 if (dataEnd && t >= dataEnd)
413 ie.length = dataLength(ie.info.type, ie.data, ie.info.count, 1, dataEnd);
414 if (ie.length < 0 || hdrchkData(ie.length))
420 ie.info.offset = regionid;
421 *entry = ie; /* structure assignment */
427 if (typeSizes[type] > 1) {
429 diff = typeSizes[type] - (dl % typeSizes[type]);
430 if (diff != typeSizes[type]) {
432 if (ieprev.info.type == RPM_I18NSTRING_TYPE)
433 ieprev.length += diff;
436 tdel = (tprev ? (t - tprev) : 0);
437 if (ieprev.info.type == RPM_I18NSTRING_TYPE)
438 tdel = ieprev.length;
440 if (ie.info.tag >= HEADER_I18NTABLE) {
444 /* XXX HEADER_IMAGE tags don't include region sub-tag. */
445 if (ie.info.tag == HEADER_IMAGE)
446 tprev -= REGION_TAG_COUNT;
449 /* Perform endian conversions */
450 switch (ntohl(pe->type)) {
452 { uint64_t * it = (uint64_t *)t;
453 for (; ie.info.count > 0; ie.info.count--, it += 1) {
454 if (dataEnd && ((unsigned char *)it) >= dataEnd)
458 t = (unsigned char *) it;
461 { int32_t * it = (int32_t *)t;
462 for (; ie.info.count > 0; ie.info.count--, it += 1) {
463 if (dataEnd && ((unsigned char *)it) >= dataEnd)
467 t = (unsigned char *) it;
470 { int16_t * it = (int16_t *) t;
471 for (; ie.info.count > 0; ie.info.count--, it += 1) {
472 if (dataEnd && ((unsigned char *)it) >= dataEnd)
476 t = (unsigned char *) it;
485 ieprev = ie; /* structure assignment */
488 tdel = (tprev ? (t - tprev) : 0);
492 * There are two hacks here:
493 * 1) tl is 16b (i.e. REGION_TAG_COUNT) short while doing headerReload().
494 * 2) the 8/98 rpm bug with inserting i18n tags needs to use tl, not dl.
496 if (tl+REGION_TAG_COUNT == dl)
497 tl += REGION_TAG_COUNT;
505 * @retval *lengthPtr no. bytes in unloaded header blob
506 * @return unloaded header blob (NULL on error)
508 static void * doHeaderUnload(Header h,
522 int drlen, ndribbles;
526 /* Sort entries by (offset,tag). */
529 /* Compute (il,dl) for all tags, including those deleted in region. */
531 drlen = ndribbles = driplen = ndrips = 0;
532 for (i = 0, entry = h->index; i < h->indexUsed; i++, entry++) {
533 if (ENTRY_IS_REGION(entry)) {
534 int32_t rdl = -entry->info.offset; /* negative offset */
535 int32_t ril = rdl/sizeof(*pe);
536 int rid = entry->info.offset;
539 dl += entry->rdlen + entry->info.count;
540 /* XXX Legacy regions do not include the region tag and data. */
541 if (i == 0 && (h->flags & HEADERFLAG_LEGACY))
544 /* Skip rest of entries in region, but account for dribbles. */
545 for (; i < h->indexUsed && entry->info.offset <= rid+1; i++, entry++) {
546 if (entry->info.offset <= rid)
550 type = entry->info.type;
551 if (typeSizes[type] > 1) {
553 diff = typeSizes[type] - (dl % typeSizes[type]);
554 if (diff != typeSizes[type]) {
563 drlen += entry->length;
571 /* Ignore deleted drips. */
572 if (entry->data == NULL || entry->length <= 0)
576 type = entry->info.type;
577 if (typeSizes[type] > 1) {
579 diff = typeSizes[type] - (dl % typeSizes[type]);
580 if (diff != typeSizes[type]) {
590 driplen += entry->length;
594 /* Sanity checks on header intro. */
595 if (hdrchkTags(il) || hdrchkData(dl))
598 len = sizeof(il) + sizeof(dl) + (il * sizeof(*pe)) + dl;
604 pe = (entryInfo) &ei[2];
605 dataStart = te = (char *) (pe + il);
608 for (i = 0, entry = h->index; i < h->indexUsed; i++, entry++) {
614 if (entry->data == NULL || entry->length <= 0)
617 t = (unsigned char*)te;
618 pe->tag = htonl(entry->info.tag);
619 pe->type = htonl(entry->info.type);
620 pe->count = htonl(entry->info.count);
622 if (ENTRY_IS_REGION(entry)) {
623 int32_t rdl = -entry->info.offset; /* negative offset */
624 int32_t ril = rdl/sizeof(*pe) + ndribbles;
625 int rid = entry->info.offset;
627 src = (char *)entry->data;
628 rdlen = entry->rdlen;
630 /* XXX Legacy regions do not include the region tag and data. */
631 if (i == 0 && (h->flags & HEADERFLAG_LEGACY)) {
635 memcpy(pe+1, src, rdl);
636 memcpy(te, src + rdl, rdlen);
639 pe->offset = htonl(te - dataStart);
642 stei[2] = htonl(-rdl-entry->info.count);
644 memcpy(te, stei, entry->info.count);
645 te += entry->info.count;
647 rdlen += entry->info.count;
649 count = regionSwab(NULL, ril, 0, pe, t, NULL, 0);
655 memcpy(pe+1, src + sizeof(*pe), ((ril-1) * sizeof(*pe)));
656 memcpy(te, src + (ril * sizeof(*pe)), rdlen+entry->info.count+drlen);
659 entryInfo se = (entryInfo)src;
660 int off = ntohl(se->offset);
661 pe->offset = (off) ? htonl(te - dataStart) : htonl(off);
663 te += entry->info.count + drlen;
665 count = regionSwab(NULL, ril, 0, pe, t, NULL, 0);
666 if (count != (rdlen + entry->info.count + drlen))
670 /* Skip rest of entries in region. */
671 while (i < h->indexUsed && entry->info.offset <= rid+1) {
681 /* Ignore deleted drips. */
682 if (entry->data == NULL || entry->length <= 0)
686 type = entry->info.type;
687 if (typeSizes[type] > 1) {
689 diff = typeSizes[type] - ((te - dataStart) % typeSizes[type]);
690 if (diff != typeSizes[type]) {
697 pe->offset = htonl(te - dataStart);
699 /* copy data w/ endian conversions */
700 switch (entry->info.type) {
702 count = entry->info.count;
705 *((uint64_t *)te) = htonll(*((uint64_t *)src));
706 te += sizeof(uint64_t);
707 src += sizeof(uint64_t);
712 count = entry->info.count;
715 *((int32_t *)te) = htonl(*((int32_t *)src));
716 te += sizeof(int32_t);
717 src += sizeof(int32_t);
722 count = entry->info.count;
725 *((int16_t *)te) = htons(*((int16_t *)src));
726 te += sizeof(int16_t);
727 src += sizeof(int16_t);
732 memcpy(te, entry->data, entry->length);
739 /* Insure that there are no memcpy underruns/overruns. */
740 if (((char *)pe) != dataStart)
742 if ((((char *)ei)+len) != te)
748 h->flags &= ~HEADERFLAG_SORTED;
758 void * headerUnload(Header h)
761 void * uh = doHeaderUnload(h, &length);
766 * Find matching (tag,type) entry in header.
768 * @param tag entry tag
769 * @param type entry type
770 * @return header entry
773 indexEntry findEntry(Header h, rpmTag tag, rpmTagType type)
775 indexEntry entry, entry2, last;
776 struct indexEntry_s key;
778 if (h == NULL) return NULL;
779 if (!(h->flags & HEADERFLAG_SORTED)) headerSort(h);
784 bsearch(&key, h->index, h->indexUsed, sizeof(*h->index), indexCmp);
788 if (type == RPM_NULL_TYPE)
792 while (entry->info.tag == tag && entry->info.type != type &&
793 entry > h->index) entry--;
795 if (entry->info.tag == tag && entry->info.type == type)
798 last = h->index + h->indexUsed;
799 /* FIX: entry2 = entry. Code looks bogus as well. */
800 while (entry2->info.tag == tag && entry2->info.type != type &&
801 entry2 < last) entry2++;
803 if (entry->info.tag == tag && entry->info.type == type)
809 int headerRemoveEntry(Header h, rpmTag tag)
811 indexEntry last = h->index + h->indexUsed;
812 indexEntry entry, first;
815 entry = findEntry(h, tag, RPM_NULL_TYPE);
816 if (!entry) return 1;
818 /* Make sure entry points to the first occurence of this tag. */
819 while (entry > h->index && (entry - 1)->info.tag == tag)
822 /* Free data for tags being removed. */
823 for (first = entry; first < last; first++) {
825 if (first->info.tag != tag)
830 if (ENTRY_IN_REGION(first))
835 ne = (first - entry);
840 memmove(entry, first, (ne * sizeof(*entry)));
846 Header headerLoad(void * uh)
848 int32_t * ei = (int32_t *) uh;
849 int32_t il = ntohl(ei[0]); /* index length */
850 int32_t dl = ntohl(ei[1]); /* data length */
851 size_t pvlen = sizeof(il) + sizeof(dl) +
852 (il * sizeof(struct entryInfo_s)) + dl;
856 unsigned char * dataStart;
857 unsigned char * dataEnd;
862 /* Sanity checks on header intro. */
863 if (hdrchkTags(il) || hdrchkData(dl))
867 pe = (entryInfo) &ei[2];
868 dataStart = (unsigned char *) (pe + il);
869 dataEnd = dataStart + dl;
871 h = xcalloc(1, sizeof(*h));
873 h->indexAlloced = il + 1;
875 h->index = xcalloc(h->indexAlloced, sizeof(*h->index));
876 h->flags |= HEADERFLAG_SORTED;
882 if (!(htonl(pe->tag) < HEADER_I18NTABLE)) {
883 h->flags |= HEADERFLAG_LEGACY;
884 entry->info.type = REGION_TAG_TYPE;
885 entry->info.tag = HEADER_IMAGE;
886 entry->info.count = REGION_TAG_COUNT;
887 entry->info.offset = ((unsigned char *)pe - dataStart); /* negative offset */
890 entry->length = pvlen - sizeof(il) - sizeof(dl);
891 rdlen = regionSwab(entry+1, il, 0, pe, dataStart, dataEnd, entry->info.offset);
892 #if 0 /* XXX don't check, the 8/98 i18n bug fails here. */
896 entry->rdlen = rdlen;
903 h->flags &= ~HEADERFLAG_LEGACY;
905 entry->info.type = htonl(pe->type);
906 entry->info.count = htonl(pe->count);
908 if (hdrchkType(entry->info.type))
910 if (hdrchkTags(entry->info.count))
913 { int off = ntohl(pe->offset);
918 size_t nb = REGION_TAG_COUNT;
920 /* XXX Hmm, why the copy? */
921 memcpy(&stei, dataStart + off, nb);
922 rdl = -ntohl(stei[2]); /* negative offset */
923 ril = rdl/sizeof(*pe);
924 if (hdrchkTags(ril) || hdrchkData(rdl))
926 entry->info.tag = htonl(pe->tag);
929 rdl = (ril * sizeof(struct entryInfo_s));
930 entry->info.tag = HEADER_IMAGE;
933 entry->info.offset = -rdl; /* negative offset */
936 entry->length = pvlen - sizeof(il) - sizeof(dl);
937 rdlen = regionSwab(entry+1, ril-1, 0, pe+1, dataStart, dataEnd, entry->info.offset);
940 entry->rdlen = rdlen;
942 if (ril < h->indexUsed) {
943 indexEntry newEntry = entry + ril;
944 int ne = (h->indexUsed - ril);
945 int rid = entry->info.offset+1;
948 /* Load dribble entries from region. */
949 rc = regionSwab(newEntry, ne, 0, pe+ril, dataStart, dataEnd, rid);
954 { indexEntry firstEntry = newEntry;
955 int save = h->indexUsed;
958 /* Dribble entries replace duplicate region entries. */
960 for (j = 0; j < ne; j++, newEntry++) {
961 (void) headerRemoveEntry(h, newEntry->info.tag);
962 if (newEntry->info.tag == HEADER_BASENAMES)
963 (void) headerRemoveEntry(h, HEADER_OLDFILENAMES);
966 /* If any duplicate entries were replaced, move new entries down. */
967 if (h->indexUsed < (save - ne)) {
968 memmove(h->index + h->indexUsed, firstEntry,
969 (ne * sizeof(*entry)));
976 h->flags &= ~HEADERFLAG_SORTED;
983 h->index = _free(h->index);
989 Header headerReload(Header h, rpmTag tag)
993 void * uh = doHeaderUnload(h, &length);
1003 if (nh->flags & HEADERFLAG_ALLOCATED)
1005 nh->flags |= HEADERFLAG_ALLOCATED;
1006 if (ENTRY_IS_REGION(nh->index)) {
1007 if (tag == HEADER_SIGNATURES || tag == HEADER_IMMUTABLE)
1008 nh->index[0].info.tag = tag;
1013 Header headerCopyLoad(const void * uh)
1015 int32_t * ei = (int32_t *) uh;
1016 int32_t il = ntohl(ei[0]); /* index length */
1017 int32_t dl = ntohl(ei[1]); /* data length */
1018 size_t pvlen = sizeof(il) + sizeof(dl) +
1019 (il * sizeof(struct entryInfo_s)) + dl;
1023 /* Sanity checks on header intro. */
1024 if (!(hdrchkTags(il) || hdrchkData(dl)) && pvlen < headerMaxbytes) {
1025 nuh = memcpy(xmalloc(pvlen), uh, pvlen);
1026 if ((h = headerLoad(nuh)) != NULL)
1027 h->flags |= HEADERFLAG_ALLOCATED;
1035 * Read (and load) header from file handle.
1036 * @param fd file handle
1037 * @param magicp read (and verify) 8 bytes of (magic, 0)?
1038 * @return header (or NULL on error)
1040 Header headerRead(FD_t fd, enum hMagic magicp)
1044 int32_t * ei = NULL;
1052 memset(block, 0, sizeof(block));
1054 if (magicp == HEADER_MAGIC_YES)
1058 if (timedRead(fd, (char *)block, i*sizeof(*block)) != (i * sizeof(*block)))
1063 if (magicp == HEADER_MAGIC_YES) {
1065 if (memcmp(&magic, header_magic, sizeof(magic)))
1067 reserved = block[i++];
1070 il = ntohl(block[i]); i++;
1071 dl = ntohl(block[i]); i++;
1073 len = sizeof(il) + sizeof(dl) + (il * sizeof(struct entryInfo_s)) + dl;
1075 /* Sanity checks on header intro. */
1076 if (hdrchkTags(il) || hdrchkData(dl) || len > headerMaxbytes)
1082 len -= sizeof(il) + sizeof(dl);
1085 if (timedRead(fd, (char *)&ei[2], len) != len)
1092 if (h->flags & HEADERFLAG_ALLOCATED)
1094 h->flags |= HEADERFLAG_ALLOCATED;
1097 /* FIX: timedRead macro obscures annotation */
1101 int headerWrite(FD_t fd, Header h, enum hMagic magicp)
1109 uh = doHeaderUnload(h, &length);
1113 case HEADER_MAGIC_YES:
1114 nb = Fwrite(header_magic, sizeof(char), sizeof(header_magic), fd);
1115 if (nb != sizeof(header_magic))
1118 case HEADER_MAGIC_NO:
1122 nb = Fwrite(uh, sizeof(char), length, fd);
1126 return (nb == length ? 0 : 1);
1129 int headerIsEntry(Header h, rpmTag tag)
1131 /* FIX: h modified by sort. */
1132 return (findEntry(h, tag, RPM_NULL_TYPE) ? 1 : 0);
1137 * Retrieve data from header entry.
1138 * Relevant flags (others are ignored), if neither is set allocation
1139 * behavior depends on data type(!)
1140 * HEADERGET_MINMEM: return pointers to header memory
1141 * HEADERGET_ALLOC: always return malloced memory, overrides MINMEM
1143 * @todo Permit retrieval of regions other than HEADER_IMUTABLE.
1144 * @param entry header entry
1145 * @param td tag data container
1146 * @param minMem string pointers refer to header memory?
1147 * @param flags flags to control memory allocation
1148 * @return 1 on success, otherwise error.
1150 static int copyTdEntry(const indexEntry entry, rpmtd td, headerGetFlags flags)
1152 rpm_count_t count = entry->info.count;
1153 int rc = 1; /* XXX 1 on success. */
1154 /* ALLOC overrides MINMEM */
1155 int allocMem = flags & HEADERGET_ALLOC;
1156 int minMem = allocMem ? 0 : flags & HEADERGET_MINMEM;
1159 switch (entry->info.type) {
1162 * XXX This only works for
1163 * XXX "sealed" HEADER_IMMUTABLE/HEADER_SIGNATURES/HEADER_IMAGE.
1164 * XXX This will *not* work for unsealed legacy HEADER_IMAGE (i.e.
1165 * XXX a legacy header freshly read, but not yet unloaded to the rpmdb).
1167 if (ENTRY_IS_REGION(entry)) {
1168 int32_t * ei = ((int32_t *)entry->data) - 2;
1169 entryInfo pe = (entryInfo) (ei + 2);
1170 unsigned char * dataStart = (unsigned char *) (pe + ntohl(ei[0]));
1171 int32_t rdl = -entry->info.offset; /* negative offset */
1172 int32_t ril = rdl/sizeof(*pe);
1175 count = 2 * sizeof(*ei) + (ril * sizeof(*pe)) + rdl;
1176 if (entry->info.tag == HEADER_IMAGE) {
1180 count += REGION_TAG_COUNT;
1181 rdl += REGION_TAG_COUNT;
1184 td->data = xmalloc(count);
1185 ei = (int32_t *) td->data;
1189 pe = (entryInfo) memcpy(ei + 2, pe, (ril * sizeof(*pe)));
1191 dataStart = (unsigned char *) memcpy(pe + ril, dataStart, rdl);
1193 rc = regionSwab(NULL, ril, 0, pe, dataStart, dataStart + rdl, 0);
1194 /* XXX 1 on success. */
1195 rc = (rc < 0) ? 0 : 1;
1197 count = entry->length;
1199 ? memcpy(xmalloc(count), entry->data, count)
1203 case RPM_STRING_TYPE:
1205 td->data = allocMem ? xstrdup(entry->data) : entry->data;
1208 case RPM_STRING_ARRAY_TYPE:
1209 case RPM_I18NSTRING_TYPE:
1210 { const char ** ptrEntry;
1211 int tableSize = count * sizeof(char *);
1216 td->data = xmalloc(tableSize);
1217 ptrEntry = (const char **) td->data;
1220 t = xmalloc(tableSize + entry->length);
1221 td->data = (void *)t;
1222 ptrEntry = (const char **) td->data;
1224 memcpy(t, entry->data, entry->length);
1226 for (i = 0; i < count; i++) {
1234 case RPM_INT16_TYPE:
1235 case RPM_INT32_TYPE:
1236 case RPM_INT64_TYPE:
1238 size_t dlen = entry->length * typeSizes[entry->info.type];
1239 td->data = xmalloc(dlen);
1240 memcpy(td->data, entry->data, dlen);
1242 td->data = entry->data;
1246 /* WTH? Don't mess with unknown data types... */
1251 td->type = entry->info.type;
1254 td->flags = RPMTD_IMMUTABLE;
1255 if (td->data && entry->data != td->data) {
1256 td->flags |= RPMTD_ALLOCED;
1263 * Retrieve data from header entry.
1264 * Just a compat wrapper around copyTdEntry() until everything's converted
1266 * @param entry header entry
1267 * @retval type address of type (or NULL)
1268 * @retval p address of data (or NULL)
1269 * @retval c address of count (or NULL)
1270 * @param minMem string pointers refer to header memory?
1271 * @return 1 on success, otherwise error.
1273 static int copyEntry(const indexEntry entry,
1283 rc = copyTdEntry(entry, &td, minMem ? HEADERGET_MINMEM : HEADERGET_DEFAULT);
1289 * Does locale match entry in header i18n table?
1292 * The range [l,le) contains the next locale to match:
1293 * ll[_CC][.EEEEE][@dddd]
1295 * ll ISO language code (in lowercase).
1296 * CC (optional) ISO coutnry code (in uppercase).
1297 * EEEEE (optional) encoding (not really standardized).
1298 * dddd (optional) dialect.
1301 * @param td header i18n table data, NUL terminated
1302 * @param l start of locale to match
1303 * @param le end of locale to match
1304 * @return 1 on good match, 2 on weak match, 0 on no match
1306 static int headerMatchLocale(const char *td, const char *l, const char *le)
1310 /* First try a complete match. */
1311 if (strlen(td) == (le-l) && !strncmp(td, l, (le - l)))
1314 /* Next, try stripping optional dialect and matching. */
1315 for (fe = l; fe < le && *fe != '@'; fe++)
1317 if (fe < le && !strncmp(td, l, (fe - l)))
1320 /* Next, try stripping optional codeset and matching. */
1321 for (fe = l; fe < le && *fe != '.'; fe++)
1323 if (fe < le && !strncmp(td, l, (fe - l)))
1326 /* Finally, try stripping optional country code and matching. */
1327 for (fe = l; fe < le && *fe != '_'; fe++)
1329 if (fe < le && !strncmp(td, l, (fe - l)))
1336 * Return i18n string from header that matches locale.
1338 * @param entry i18n string data
1339 * @retval td tag data container
1340 * @param flags flags to control allocation
1343 static int copyI18NEntry(Header h, indexEntry entry, rpmtd td,
1344 headerGetFlags flags)
1346 const char *lang, *l, *le;
1349 td->type = RPM_STRING_TYPE;
1351 /* if no match, just return the first string */
1352 td->data = entry->data;
1354 /* XXX Drepper sez' this is the order. */
1355 if ((lang = getenv("LANGUAGE")) == NULL &&
1356 (lang = getenv("LC_ALL")) == NULL &&
1357 (lang = getenv("LC_MESSAGES")) == NULL &&
1358 (lang = getenv("LANG")) == NULL)
1361 if ((table = findEntry(h, HEADER_I18NTABLE, RPM_STRING_ARRAY_TYPE)) == NULL)
1364 for (l = lang; *l != '\0'; l = le) {
1366 char *ed, *ed_weak = NULL;
1369 while (*l && *l == ':') /* skip leading colons */
1373 for (le = l; *le && *le != ':'; le++) /* find end of this locale */
1376 /* For each entry in the header ... */
1377 for (langNum = 0, t = table->data, ed = entry->data;
1378 langNum < entry->info.count;
1379 langNum++, t += strlen(t) + 1, ed += strlen(ed) + 1) {
1381 int match = headerMatchLocale(t, l, le);
1385 } else if (match == 2) {
1396 if (flags & HEADERGET_ALLOC) {
1397 td->data = xstrdup(td->data);
1404 * Retrieve tag data from header.
1406 * @param tag tag to retrieve
1407 * @retval td tag data container
1408 * @param flags flags to control retrieval
1409 * @return 1 on success, 0 on not found
1411 static int intGetTdEntry(Header h, rpmTag tag, rpmtd td, headerGetFlags flags)
1417 /* ensure clean state */
1420 /* First find the tag */
1421 /* FIX: h modified by sort. */
1422 entry = findEntry(h, tag, RPM_NULL_TYPE);
1423 if (entry == NULL) {
1424 /* Td is zeroed above, just return... */
1428 if (flags & HEADERGET_RAW) {
1429 rc = copyTdEntry(entry, td, flags);
1431 switch (entry->info.type) {
1432 case RPM_I18NSTRING_TYPE:
1433 rc = copyI18NEntry(h, entry, td, flags);
1436 rc = copyTdEntry(entry, td, flags);
1441 /* XXX 1 on success */
1442 return ((rc == 1) ? 1 : 0);
1445 void * headerFreeTag(Header h, rpm_data_t data, rpmTagType type)
1448 if (type == RPM_FORCEFREE_TYPE ||
1449 type == RPM_STRING_ARRAY_TYPE ||
1450 type == RPM_I18NSTRING_TYPE ||
1451 type == RPM_BIN_TYPE)
1458 * XXX temporary kludgery until tag extensions have been converted to
1459 * take rpmtd as argument
1461 static int intGetTagExt(Header h, rpmTag tag, rpmtd td, headerTagTagFunction tagfunc)
1465 rc = tagfunc(h, td);
1470 int headerGet(Header h, rpmTag tag, rpmtd td, headerGetFlags flags)
1473 headerTagTagFunction tagfunc = NULL;
1477 if (flags & HEADERGET_EXT) {
1478 tagfunc = rpmHeaderTagFunc(tag);
1482 rc = intGetTagExt(h, tag, td, tagfunc);
1484 rc = intGetTdEntry(h, tag, td, flags);
1487 assert(tag == td->tag);
1491 static int headerGetWrap(Header h, rpmTag tag,
1495 headerGetFlags flags)
1500 rc = headerGet(h, tag, &td, flags);
1505 int headerGetEntry(Header h, rpmTag tag,
1510 return headerGetWrap(h, tag, type, p, c, HEADERGET_DEFAULT);
1513 int headerGetEntryMinMemory(Header h, rpmTag tag,
1518 return headerGetWrap(h, tag, type, (rpm_data_t) p, c, HEADERGET_MINMEM);
1521 int headerGetRawEntry(Header h, rpmTag tag, rpmTagType * type, rpm_data_t * p,
1527 if (p == NULL) return headerIsEntry(h, tag);
1529 /* First find the tag */
1530 /* FIX: h modified by sort. */
1531 entry = findEntry(h, tag, RPM_NULL_TYPE);
1538 rc = copyEntry(entry, type, p, c, 0);
1540 /* XXX 1 on success */
1541 return ((rc == 1) ? 1 : 0);
1546 static void copyData(rpmTagType type, rpm_data_t dstPtr,
1547 rpm_constdata_t srcPtr, rpm_count_t cnt, int dataLength)
1550 case RPM_STRING_ARRAY_TYPE:
1551 case RPM_I18NSTRING_TYPE:
1552 { const char ** av = (const char **) srcPtr;
1555 while (cnt-- > 0 && dataLength > 0) {
1557 if ((s = *av++) == NULL)
1561 } while (s[-1] && --dataLength > 0);
1566 memmove(dstPtr, srcPtr, dataLength);
1572 * Return (malloc'ed) copy of entry data.
1573 * @param type entry data type
1574 * @param p entry data
1575 * @param c entry item count
1576 * @retval lengthPtr no. bytes in returned data
1577 * @return (malloc'ed) copy of entry data, NULL on error
1580 grabData(rpmTagType type, rpm_constdata_t p, rpm_count_t c, int * lengthPtr)
1582 rpm_data_t data = NULL;
1585 length = dataLength(type, p, c, 0, NULL);
1587 data = xmalloc(length);
1588 copyData(type, data, p, c, length);
1592 *lengthPtr = length;
1596 int headerAddEntry(Header h, rpmTag tag, rpmTagType type,
1597 rpm_constdata_t p, rpm_count_t c)
1603 /* Count must always be >= 1 for headerAddEntry. */
1607 if (hdrchkType(type))
1613 data = grabData(type, p, c, &length);
1614 if (data == NULL || length <= 0)
1617 /* Allocate more index space if necessary */
1618 if (h->indexUsed == h->indexAlloced) {
1619 h->indexAlloced += INDEX_MALLOC_SIZE;
1620 h->index = xrealloc(h->index, h->indexAlloced * sizeof(*h->index));
1623 /* Fill in the index */
1624 entry = h->index + h->indexUsed;
1625 entry->info.tag = tag;
1626 entry->info.type = type;
1627 entry->info.count = c;
1628 entry->info.offset = 0;
1630 entry->length = length;
1632 if (h->indexUsed > 0 && tag < h->index[h->indexUsed-1].info.tag)
1633 h->flags &= ~HEADERFLAG_SORTED;
1639 int headerAppendEntry(Header h, rpmTag tag, rpmTagType type,
1640 rpm_constdata_t p, rpm_count_t c)
1645 if (type == RPM_STRING_TYPE || type == RPM_I18NSTRING_TYPE) {
1646 /* we can't do this */
1650 /* Find the tag entry in the header. */
1651 entry = findEntry(h, tag, type);
1655 length = dataLength(type, p, c, 0, NULL);
1659 if (ENTRY_IN_REGION(entry)) {
1660 char * t = xmalloc(entry->length + length);
1661 memcpy(t, entry->data, entry->length);
1663 entry->info.offset = 0;
1665 entry->data = xrealloc(entry->data, entry->length + length);
1667 copyData(type, ((char *) entry->data) + entry->length, p, c, length);
1669 entry->length += length;
1671 entry->info.count += c;
1676 int headerAddOrAppendEntry(Header h, rpmTag tag, rpmTagType type,
1677 rpm_constdata_t p, rpm_count_t c)
1679 return (findEntry(h, tag, type)
1680 ? headerAppendEntry(h, tag, type, p, c)
1681 : headerAddEntry(h, tag, type, p, c));
1684 int headerPut(Header h, rpmtd td, headerPutFlags flags)
1689 if (flags & HEADERPUT_APPEND) {
1690 rc = headerAddOrAppendEntry(h, td->tag, td->type, td->data, td->count);
1692 rc = headerAddEntry(h, td->tag, td->type, td->data, td->count);
1697 int headerAddI18NString(Header h, rpmTag tag, const char * string,
1700 indexEntry table, entry;
1701 const char ** strArray;
1704 rpm_count_t i, langNum;
1707 table = findEntry(h, HEADER_I18NTABLE, RPM_STRING_ARRAY_TYPE);
1708 entry = findEntry(h, tag, RPM_I18NSTRING_TYPE);
1710 if (!table && entry)
1711 return 0; /* this shouldn't ever happen!! */
1713 if (!table && !entry) {
1714 const char * charArray[2];
1715 rpm_count_t count = 0;
1716 if (!lang || (lang[0] == 'C' && lang[1] == '\0')) {
1717 charArray[count++] = "C";
1719 charArray[count++] = "C";
1720 charArray[count++] = lang;
1722 if (!headerAddEntry(h, HEADER_I18NTABLE, RPM_STRING_ARRAY_TYPE,
1725 table = findEntry(h, HEADER_I18NTABLE, RPM_STRING_ARRAY_TYPE);
1730 if (!lang) lang = "C";
1732 { const char * l = table->data;
1733 for (langNum = 0; langNum < table->info.count; langNum++) {
1734 if (!strcmp(l, lang)) break;
1739 if (langNum >= table->info.count) {
1740 length = strlen(lang) + 1;
1741 if (ENTRY_IN_REGION(table)) {
1742 char * t = xmalloc(table->length + length);
1743 memcpy(t, table->data, table->length);
1745 table->info.offset = 0;
1747 table->data = xrealloc(table->data, table->length + length);
1748 memmove(((char *)table->data) + table->length, lang, length);
1749 table->length += length;
1750 table->info.count++;
1755 strArray = xmalloc(sizeof(*strArray) * (langNum + 1));
1756 for (i = 0; i < langNum; i++)
1758 strArray[langNum] = string;
1759 rc = headerAddEntry(h, tag, RPM_I18NSTRING_TYPE, strArray, langNum + 1);
1762 } else if (langNum >= entry->info.count) {
1763 ghosts = langNum - entry->info.count;
1765 length = strlen(string) + 1 + ghosts;
1766 if (ENTRY_IN_REGION(entry)) {
1767 char * t = xmalloc(entry->length + length);
1768 memcpy(t, entry->data, entry->length);
1770 entry->info.offset = 0;
1772 entry->data = xrealloc(entry->data, entry->length + length);
1774 memset(((char *)entry->data) + entry->length, '\0', ghosts);
1775 memmove(((char *)entry->data) + entry->length + ghosts, string, strlen(string)+1);
1777 entry->length += length;
1778 entry->info.count = langNum + 1;
1780 char *b, *be, *e, *ee, *t;
1783 /* Set beginning/end pointers to previous data */
1784 b = be = e = ee = entry->data;
1785 for (i = 0; i < table->info.count; i++) {
1788 ee += strlen(ee) + 1;
1793 /* Get storage for new buffer */
1795 sn = strlen(string) + 1;
1797 length = bn + sn + en;
1798 t = buf = xmalloc(length);
1800 /* Copy values into new storage */
1803 memcpy(t, string, sn);
1808 /* Replace i18N string array */
1809 entry->length -= strlen(be) + 1;
1810 entry->length += sn;
1812 if (ENTRY_IN_REGION(entry)) {
1813 entry->info.offset = 0;
1815 entry->data = _free(entry->data);
1822 int headerModifyEntry(Header h, rpmTag tag, rpmTagType type,
1823 rpm_constdata_t p, rpm_count_t c)
1830 /* First find the tag */
1831 entry = findEntry(h, tag, type);
1836 data = grabData(type, p, c, &length);
1837 if (data == NULL || length <= 0)
1840 /* make sure entry points to the first occurence of this tag */
1841 while (entry > h->index && (entry - 1)->info.tag == tag)
1844 /* free after we've grabbed the new data in case the two are intertwined;
1845 that's a bad idea but at least we won't break */
1846 oldData = entry->data;
1848 entry->info.count = c;
1849 entry->info.type = type;
1851 entry->length = length;
1853 if (ENTRY_IN_REGION(entry)) {
1854 entry->info.offset = 0;
1856 oldData = _free(oldData);
1862 * Header tag iterator data structure.
1864 struct headerIterator_s {
1865 Header h; /*!< Header being iterated. */
1866 int next_index; /*!< Next tag index. */
1869 HeaderIterator headerFreeIterator(HeaderIterator hi)
1872 hi->h = headerFree(hi->h);
1878 HeaderIterator headerInitIterator(Header h)
1880 HeaderIterator hi = xmalloc(sizeof(*hi));
1884 hi->h = headerLink(h);
1889 int headerNext(HeaderIterator hi, rpmtd td)
1892 int slot = hi->next_index;
1893 indexEntry entry = NULL;
1899 for (slot = hi->next_index; slot < h->indexUsed; slot++) {
1900 entry = h->index + slot;
1901 if (!ENTRY_IS_REGION(entry))
1904 hi->next_index = slot;
1905 if (entry == NULL || slot >= h->indexUsed)
1911 td->tag = entry->info.tag;
1913 rc = copyTdEntry(entry, td, HEADERGET_MINMEM);
1915 /* XXX 1 on success */
1916 return ((rc == 1) ? 1 : 0);
1919 int headerNextIterator(HeaderIterator hi,
1928 rc = headerNext(hi, &td);
1936 * Duplicate a header.
1938 * @return new header instance
1940 Header headerCopy(Header h)
1942 Header nh = headerNew();
1946 hi = headerInitIterator(h);
1947 while (headerNext(hi, &td)) {
1948 if (rpmtdCount(&td) > 0) {
1949 (void) headerPut(nh, &td, HEADERPUT_DEFAULT);
1953 hi = headerFreeIterator(hi);
1955 return headerReload(nh, HEADER_IMAGE);
1958 void headerCopyTags(Header headerFrom, Header headerTo,
1959 const rpmTag * tagstocopy)
1964 if (headerFrom == headerTo)
1967 for (p = tagstocopy; *p != 0; p++) {
1968 if (headerIsEntry(headerTo, *p))
1970 if (!headerGet(headerFrom, *p, &td, HEADERGET_MINMEM))
1972 (void) headerPut(headerTo, &td, HEADERPUT_DEFAULT);
1977 void * headerFreeData(rpm_data_t data, rpmTagType type)
1980 if (type == RPM_FORCEFREE_TYPE ||
1981 type == RPM_STRING_ARRAY_TYPE ||
1982 type == RPM_I18NSTRING_TYPE ||
1983 type == RPM_BIN_TYPE)
1984 free(data); /* XXX _free() */