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 */
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 * @return 0 on success
84 typedef int (*headerTagTagFunction) (Header h, rpmtd td);
86 extern void *rpmHeaderTagFunc(rpmTag tag);
88 Header headerLink(Header h)
90 if (h == NULL) return NULL;
94 fprintf(stderr, "--> h %p ++ %d at %s:%u\n", h, h->nrefs, __FILE__, __LINE__);
99 Header headerUnlink(Header h)
101 if (h == NULL) return NULL;
103 fprintf(stderr, "--> h %p -- %d at %s:%u\n", h, h->nrefs, __FILE__, __LINE__);
108 Header headerFree(Header h)
110 (void) headerUnlink(h);
112 if (h == NULL || h->nrefs > 0)
113 return NULL; /* XXX return previous header? */
116 indexEntry entry = h->index;
118 for (i = 0; i < h->indexUsed; i++, entry++) {
119 if ((h->flags & HEADERFLAG_ALLOCATED) && ENTRY_IS_REGION(entry)) {
120 if (entry->length > 0) {
121 int32_t * ei = entry->data;
122 if ((ei - 2) == h->blob) h->blob = _free(h->blob);
125 } else if (!ENTRY_IN_REGION(entry)) {
126 entry->data = _free(entry->data);
130 h->index = _free(h->index);
137 Header headerNew(void)
139 Header h = xcalloc(1, sizeof(*h));
142 h->indexAlloced = INDEX_MALLOC_SIZE;
144 h->flags |= HEADERFLAG_SORTED;
146 h->index = (h->indexAlloced
147 ? xcalloc(h->indexAlloced, sizeof(*h->index))
151 return headerLink(h);
154 int headerVerifyInfo(int il, int dl, const void * pev, void * iv, int negate)
156 entryInfo pe = (entryInfo) pev;
160 for (i = 0; i < il; i++) {
161 info->tag = ntohl(pe[i].tag);
162 info->type = ntohl(pe[i].type);
163 info->offset = ntohl(pe[i].offset);
165 info->offset = -info->offset;
166 info->count = ntohl(pe[i].count);
168 if (hdrchkType(info->type))
170 if (hdrchkAlign(info->type, info->offset))
172 if (!negate && hdrchkRange(dl, info->offset))
174 if (hdrchkData(info->count))
183 static int indexCmp(const void * avp, const void * bvp)
185 indexEntry ap = (indexEntry) avp, bp = (indexEntry) bvp;
186 return (ap->info.tag - bp->info.tag);
189 void headerSort(Header h)
191 if (!(h->flags & HEADERFLAG_SORTED)) {
192 qsort(h->index, h->indexUsed, sizeof(*h->index), indexCmp);
193 h->flags |= HEADERFLAG_SORTED;
199 static int offsetCmp(const void * avp, const void * bvp)
201 indexEntry ap = (indexEntry) avp, bp = (indexEntry) bvp;
202 int rc = (ap->info.offset - bp->info.offset);
205 /* Within a region, entries sort by address. Added drips sort by tag. */
206 if (ap->info.offset < 0)
207 rc = (((char *)ap->data) - ((char *)bp->data));
209 rc = (ap->info.tag - bp->info.tag);
215 * Restore tags in header to original ordering.
218 void headerUnsort(Header h)
220 qsort(h->index, h->indexUsed, sizeof(*h->index), offsetCmp);
223 unsigned headerSizeof(Header h, enum hMagic magicp)
226 unsigned int size = 0;
227 unsigned int pad = 0;
236 case HEADER_MAGIC_YES:
237 size += sizeof(header_magic);
239 case HEADER_MAGIC_NO:
243 size += 2 * sizeof(int32_t); /* count of index entries */
245 for (i = 0, entry = h->index; i < h->indexUsed; i++, entry++) {
249 /* Regions go in as is ... */
250 if (ENTRY_IS_REGION(entry)) {
251 size += entry->length;
252 /* XXX Legacy regions do not include the region tag and data. */
253 if (i == 0 && (h->flags & HEADERFLAG_LEGACY))
254 size += sizeof(struct entryInfo_s) + entry->info.count;
258 /* ... and region elements are skipped. */
259 if (entry->info.offset < 0)
263 type = entry->info.type;
264 if (typeSizes[type] > 1) {
265 diff = typeSizes[type] - (size % typeSizes[type]);
266 if (diff != typeSizes[type]) {
272 size += sizeof(struct entryInfo_s) + entry->length;
279 * Return length of entry data.
280 * @param type entry data type
281 * @param p entry data
282 * @param count entry item count
283 * @param onDisk data is concatenated strings (with NUL's))?
284 * @param pend pointer to end of data (or NULL)
285 * @return no. bytes in data, -1 on failure
287 static int dataLength(rpmTagType type, rpm_constdata_t p, rpm_count_t count,
288 int onDisk, rpm_constdata_t pend)
290 const unsigned char * s = p;
291 const unsigned char * se = pend;
295 case RPM_STRING_TYPE:
303 length++; /* count nul terminator too. */
306 case RPM_STRING_ARRAY_TYPE:
307 case RPM_I18NSTRING_TYPE:
308 /* These are like RPM_STRING_TYPE, except they're *always* an array */
309 /* Compute sum of length of all strings, including nul terminators */
313 length++; /* count nul terminator too */
321 const char ** av = (const char **)p;
323 /* add one for null termination */
324 length += strlen(*av++) + 1;
330 if (typeSizes[type] == -1)
332 length = typeSizes[(type & 0xf)] * count;
333 if (length < 0 || (se && (s + length) > se))
342 * Swap int32_t and int16_t arrays within header region.
344 * This code is way more twisty than I would like.
346 * A bug with RPM_I18NSTRING_TYPE in rpm-2.5.x (fixed in August 1998)
347 * causes the offset and length of elements in a header region to disagree
348 * regarding the total length of the region data.
350 * The "fix" is to compute the size using both offset and length and
351 * return the larger of the two numbers as the size of the region.
352 * Kinda like computing left and right Riemann sums of the data elements
353 * to determine the size of a data structure, go figger :-).
355 * There's one other twist if a header region tag is in the set to be swabbed,
356 * as the data for a header region is located after all other tag data.
358 * @param entry header entry
359 * @param il no. of entries
360 * @param dl start no. bytes of data
361 * @param pe header physical entry pointer (swapped)
362 * @param dataStart header data start
363 * @param dataEnd header data end
364 * @param regionid region offset
365 * @return no. bytes of data in region, -1 on error
367 static int regionSwab(indexEntry entry, int il, int dl,
369 unsigned char * dataStart,
370 const unsigned char * dataEnd,
373 unsigned char * tprev = NULL;
374 unsigned char * t = NULL;
377 struct indexEntry_s ieprev;
379 memset(&ieprev, 0, sizeof(ieprev));
380 for (; il > 0; il--, pe++) {
381 struct indexEntry_s ie;
384 ie.info.tag = ntohl(pe->tag);
385 ie.info.type = ntohl(pe->type);
386 ie.info.count = ntohl(pe->count);
387 ie.info.offset = ntohl(pe->offset);
389 if (hdrchkType(ie.info.type))
391 if (hdrchkData(ie.info.count))
393 if (hdrchkData(ie.info.offset))
395 if (hdrchkAlign(ie.info.type, ie.info.offset))
398 ie.data = t = dataStart + ie.info.offset;
399 if (dataEnd && t >= dataEnd)
402 ie.length = dataLength(ie.info.type, ie.data, ie.info.count, 1, dataEnd);
403 if (ie.length < 0 || hdrchkData(ie.length))
409 ie.info.offset = regionid;
410 *entry = ie; /* structure assignment */
416 if (typeSizes[type] > 1) {
418 diff = typeSizes[type] - (dl % typeSizes[type]);
419 if (diff != typeSizes[type]) {
421 if (ieprev.info.type == RPM_I18NSTRING_TYPE)
422 ieprev.length += diff;
425 tdel = (tprev ? (t - tprev) : 0);
426 if (ieprev.info.type == RPM_I18NSTRING_TYPE)
427 tdel = ieprev.length;
429 if (ie.info.tag >= HEADER_I18NTABLE) {
433 /* XXX HEADER_IMAGE tags don't include region sub-tag. */
434 if (ie.info.tag == HEADER_IMAGE)
435 tprev -= REGION_TAG_COUNT;
438 /* Perform endian conversions */
439 switch (ntohl(pe->type)) {
441 { uint64_t * it = (uint64_t *)t;
442 for (; ie.info.count > 0; ie.info.count--, it += 1) {
443 if (dataEnd && ((unsigned char *)it) >= dataEnd)
447 t = (unsigned char *) it;
450 { int32_t * it = (int32_t *)t;
451 for (; ie.info.count > 0; ie.info.count--, it += 1) {
452 if (dataEnd && ((unsigned char *)it) >= dataEnd)
456 t = (unsigned char *) it;
459 { int16_t * it = (int16_t *) t;
460 for (; ie.info.count > 0; ie.info.count--, it += 1) {
461 if (dataEnd && ((unsigned char *)it) >= dataEnd)
465 t = (unsigned char *) it;
474 ieprev = ie; /* structure assignment */
477 tdel = (tprev ? (t - tprev) : 0);
481 * There are two hacks here:
482 * 1) tl is 16b (i.e. REGION_TAG_COUNT) short while doing headerReload().
483 * 2) the 8/98 rpm bug with inserting i18n tags needs to use tl, not dl.
485 if (tl+REGION_TAG_COUNT == dl)
486 tl += REGION_TAG_COUNT;
494 * @retval *lengthPtr no. bytes in unloaded header blob
495 * @return unloaded header blob (NULL on error)
497 static void * doHeaderUnload(Header h,
511 int drlen, ndribbles;
515 /* Sort entries by (offset,tag). */
518 /* Compute (il,dl) for all tags, including those deleted in region. */
520 drlen = ndribbles = driplen = ndrips = 0;
521 for (i = 0, entry = h->index; i < h->indexUsed; i++, entry++) {
522 if (ENTRY_IS_REGION(entry)) {
523 int32_t rdl = -entry->info.offset; /* negative offset */
524 int32_t ril = rdl/sizeof(*pe);
525 int rid = entry->info.offset;
528 dl += entry->rdlen + entry->info.count;
529 /* XXX Legacy regions do not include the region tag and data. */
530 if (i == 0 && (h->flags & HEADERFLAG_LEGACY))
533 /* Skip rest of entries in region, but account for dribbles. */
534 for (; i < h->indexUsed && entry->info.offset <= rid+1; i++, entry++) {
535 if (entry->info.offset <= rid)
539 type = entry->info.type;
540 if (typeSizes[type] > 1) {
542 diff = typeSizes[type] - (dl % typeSizes[type]);
543 if (diff != typeSizes[type]) {
552 drlen += entry->length;
560 /* Ignore deleted drips. */
561 if (entry->data == NULL || entry->length <= 0)
565 type = entry->info.type;
566 if (typeSizes[type] > 1) {
568 diff = typeSizes[type] - (dl % typeSizes[type]);
569 if (diff != typeSizes[type]) {
579 driplen += entry->length;
583 /* Sanity checks on header intro. */
584 if (hdrchkTags(il) || hdrchkData(dl))
587 len = sizeof(il) + sizeof(dl) + (il * sizeof(*pe)) + dl;
593 pe = (entryInfo) &ei[2];
594 dataStart = te = (char *) (pe + il);
597 for (i = 0, entry = h->index; i < h->indexUsed; i++, entry++) {
603 if (entry->data == NULL || entry->length <= 0)
606 t = (unsigned char*)te;
607 pe->tag = htonl(entry->info.tag);
608 pe->type = htonl(entry->info.type);
609 pe->count = htonl(entry->info.count);
611 if (ENTRY_IS_REGION(entry)) {
612 int32_t rdl = -entry->info.offset; /* negative offset */
613 int32_t ril = rdl/sizeof(*pe) + ndribbles;
614 int rid = entry->info.offset;
616 src = (char *)entry->data;
617 rdlen = entry->rdlen;
619 /* XXX Legacy regions do not include the region tag and data. */
620 if (i == 0 && (h->flags & HEADERFLAG_LEGACY)) {
624 memcpy(pe+1, src, rdl);
625 memcpy(te, src + rdl, rdlen);
628 pe->offset = htonl(te - dataStart);
631 stei[2] = htonl(-rdl-entry->info.count);
633 memcpy(te, stei, entry->info.count);
634 te += entry->info.count;
636 rdlen += entry->info.count;
638 count = regionSwab(NULL, ril, 0, pe, t, NULL, 0);
644 memcpy(pe+1, src + sizeof(*pe), ((ril-1) * sizeof(*pe)));
645 memcpy(te, src + (ril * sizeof(*pe)), rdlen+entry->info.count+drlen);
648 entryInfo se = (entryInfo)src;
649 int off = ntohl(se->offset);
650 pe->offset = (off) ? htonl(te - dataStart) : htonl(off);
652 te += entry->info.count + drlen;
654 count = regionSwab(NULL, ril, 0, pe, t, NULL, 0);
655 if (count != (rdlen + entry->info.count + drlen))
659 /* Skip rest of entries in region. */
660 while (i < h->indexUsed && entry->info.offset <= rid+1) {
670 /* Ignore deleted drips. */
671 if (entry->data == NULL || entry->length <= 0)
675 type = entry->info.type;
676 if (typeSizes[type] > 1) {
678 diff = typeSizes[type] - ((te - dataStart) % typeSizes[type]);
679 if (diff != typeSizes[type]) {
686 pe->offset = htonl(te - dataStart);
688 /* copy data w/ endian conversions */
689 switch (entry->info.type) {
691 count = entry->info.count;
694 *((uint64_t *)te) = htonll(*((uint64_t *)src));
695 te += sizeof(uint64_t);
696 src += sizeof(uint64_t);
701 count = entry->info.count;
704 *((int32_t *)te) = htonl(*((int32_t *)src));
705 te += sizeof(int32_t);
706 src += sizeof(int32_t);
711 count = entry->info.count;
714 *((int16_t *)te) = htons(*((int16_t *)src));
715 te += sizeof(int16_t);
716 src += sizeof(int16_t);
721 memcpy(te, entry->data, entry->length);
728 /* Insure that there are no memcpy underruns/overruns. */
729 if (((char *)pe) != dataStart)
731 if ((((char *)ei)+len) != te)
737 h->flags &= ~HEADERFLAG_SORTED;
747 void * headerUnload(Header h)
750 void * uh = doHeaderUnload(h, &length);
755 * Find matching (tag,type) entry in header.
757 * @param tag entry tag
758 * @param type entry type
759 * @return header entry
762 indexEntry findEntry(Header h, rpmTag tag, rpmTagType type)
764 indexEntry entry, entry2, last;
765 struct indexEntry_s key;
767 if (h == NULL) return NULL;
768 if (!(h->flags & HEADERFLAG_SORTED)) headerSort(h);
773 bsearch(&key, h->index, h->indexUsed, sizeof(*h->index), indexCmp);
777 if (type == RPM_NULL_TYPE)
781 while (entry->info.tag == tag && entry->info.type != type &&
782 entry > h->index) entry--;
784 if (entry->info.tag == tag && entry->info.type == type)
787 last = h->index + h->indexUsed;
788 /* FIX: entry2 = entry. Code looks bogus as well. */
789 while (entry2->info.tag == tag && entry2->info.type != type &&
790 entry2 < last) entry2++;
792 if (entry->info.tag == tag && entry->info.type == type)
798 int headerDel(Header h, rpmTag tag)
800 indexEntry last = h->index + h->indexUsed;
801 indexEntry entry, first;
804 entry = findEntry(h, tag, RPM_NULL_TYPE);
805 if (!entry) return 1;
807 /* Make sure entry points to the first occurence of this tag. */
808 while (entry > h->index && (entry - 1)->info.tag == tag)
811 /* Free data for tags being removed. */
812 for (first = entry; first < last; first++) {
814 if (first->info.tag != tag)
819 if (ENTRY_IN_REGION(first))
824 ne = (first - entry);
829 memmove(entry, first, (ne * sizeof(*entry)));
835 Header headerLoad(void * uh)
837 int32_t * ei = (int32_t *) uh;
838 int32_t il = ntohl(ei[0]); /* index length */
839 int32_t dl = ntohl(ei[1]); /* data length */
840 size_t pvlen = sizeof(il) + sizeof(dl) +
841 (il * sizeof(struct entryInfo_s)) + dl;
845 unsigned char * dataStart;
846 unsigned char * dataEnd;
851 /* Sanity checks on header intro. */
852 if (hdrchkTags(il) || hdrchkData(dl))
856 pe = (entryInfo) &ei[2];
857 dataStart = (unsigned char *) (pe + il);
858 dataEnd = dataStart + dl;
860 h = xcalloc(1, sizeof(*h));
862 h->indexAlloced = il + 1;
864 h->index = xcalloc(h->indexAlloced, sizeof(*h->index));
865 h->flags |= HEADERFLAG_SORTED;
871 if (!(htonl(pe->tag) < HEADER_I18NTABLE)) {
872 h->flags |= HEADERFLAG_LEGACY;
873 entry->info.type = REGION_TAG_TYPE;
874 entry->info.tag = HEADER_IMAGE;
875 entry->info.count = REGION_TAG_COUNT;
876 entry->info.offset = ((unsigned char *)pe - dataStart); /* negative offset */
879 entry->length = pvlen - sizeof(il) - sizeof(dl);
880 rdlen = regionSwab(entry+1, il, 0, pe, dataStart, dataEnd, entry->info.offset);
881 #if 0 /* XXX don't check, the 8/98 i18n bug fails here. */
885 entry->rdlen = rdlen;
892 h->flags &= ~HEADERFLAG_LEGACY;
894 entry->info.type = htonl(pe->type);
895 entry->info.count = htonl(pe->count);
897 if (hdrchkType(entry->info.type))
899 if (hdrchkTags(entry->info.count))
902 { int off = ntohl(pe->offset);
907 size_t nb = REGION_TAG_COUNT;
909 /* XXX Hmm, why the copy? */
910 memcpy(&stei, dataStart + off, nb);
911 rdl = -ntohl(stei[2]); /* negative offset */
912 ril = rdl/sizeof(*pe);
913 if (hdrchkTags(ril) || hdrchkData(rdl))
915 entry->info.tag = htonl(pe->tag);
918 rdl = (ril * sizeof(struct entryInfo_s));
919 entry->info.tag = HEADER_IMAGE;
922 entry->info.offset = -rdl; /* negative offset */
925 entry->length = pvlen - sizeof(il) - sizeof(dl);
926 rdlen = regionSwab(entry+1, ril-1, 0, pe+1, dataStart, dataEnd, entry->info.offset);
929 entry->rdlen = rdlen;
931 if (ril < h->indexUsed) {
932 indexEntry newEntry = entry + ril;
933 int ne = (h->indexUsed - ril);
934 int rid = entry->info.offset+1;
937 /* Load dribble entries from region. */
938 rc = regionSwab(newEntry, ne, 0, pe+ril, dataStart, dataEnd, rid);
943 { indexEntry firstEntry = newEntry;
944 int save = h->indexUsed;
947 /* Dribble entries replace duplicate region entries. */
949 for (j = 0; j < ne; j++, newEntry++) {
950 (void) headerDel(h, newEntry->info.tag);
951 if (newEntry->info.tag == HEADER_BASENAMES)
952 (void) headerDel(h, HEADER_OLDFILENAMES);
955 /* If any duplicate entries were replaced, move new entries down. */
956 if (h->indexUsed < (save - ne)) {
957 memmove(h->index + h->indexUsed, firstEntry,
958 (ne * sizeof(*entry)));
965 h->flags &= ~HEADERFLAG_SORTED;
972 h->index = _free(h->index);
978 Header headerReload(Header h, rpmTag tag)
982 void * uh = doHeaderUnload(h, &length);
992 if (nh->flags & HEADERFLAG_ALLOCATED)
994 nh->flags |= HEADERFLAG_ALLOCATED;
995 if (ENTRY_IS_REGION(nh->index)) {
996 if (tag == HEADER_SIGNATURES || tag == HEADER_IMMUTABLE)
997 nh->index[0].info.tag = tag;
1002 Header headerCopyLoad(const void * uh)
1004 int32_t * ei = (int32_t *) uh;
1005 int32_t il = ntohl(ei[0]); /* index length */
1006 int32_t dl = ntohl(ei[1]); /* data length */
1007 size_t pvlen = sizeof(il) + sizeof(dl) +
1008 (il * sizeof(struct entryInfo_s)) + dl;
1012 /* Sanity checks on header intro. */
1013 if (!(hdrchkTags(il) || hdrchkData(dl)) && pvlen < headerMaxbytes) {
1014 nuh = memcpy(xmalloc(pvlen), uh, pvlen);
1015 if ((h = headerLoad(nuh)) != NULL)
1016 h->flags |= HEADERFLAG_ALLOCATED;
1024 * Read (and load) header from file handle.
1025 * @param fd file handle
1026 * @param magicp read (and verify) 8 bytes of (magic, 0)?
1027 * @return header (or NULL on error)
1029 Header headerRead(FD_t fd, enum hMagic magicp)
1033 int32_t * ei = NULL;
1041 memset(block, 0, sizeof(block));
1043 if (magicp == HEADER_MAGIC_YES)
1047 if (timedRead(fd, (char *)block, i*sizeof(*block)) != (i * sizeof(*block)))
1052 if (magicp == HEADER_MAGIC_YES) {
1054 if (memcmp(&magic, header_magic, sizeof(magic)))
1056 reserved = block[i++];
1059 il = ntohl(block[i]); i++;
1060 dl = ntohl(block[i]); i++;
1062 len = sizeof(il) + sizeof(dl) + (il * sizeof(struct entryInfo_s)) + dl;
1064 /* Sanity checks on header intro. */
1065 if (hdrchkTags(il) || hdrchkData(dl) || len > headerMaxbytes)
1071 len -= sizeof(il) + sizeof(dl);
1074 if (timedRead(fd, (char *)&ei[2], len) != len)
1081 if (h->flags & HEADERFLAG_ALLOCATED)
1083 h->flags |= HEADERFLAG_ALLOCATED;
1086 /* FIX: timedRead macro obscures annotation */
1090 int headerWrite(FD_t fd, Header h, enum hMagic magicp)
1098 uh = doHeaderUnload(h, &length);
1102 case HEADER_MAGIC_YES:
1103 nb = Fwrite(header_magic, sizeof(char), sizeof(header_magic), fd);
1104 if (nb != sizeof(header_magic))
1107 case HEADER_MAGIC_NO:
1111 nb = Fwrite(uh, sizeof(char), length, fd);
1115 return (nb == length ? 0 : 1);
1118 int headerIsEntry(Header h, rpmTag tag)
1120 /* FIX: h modified by sort. */
1121 return (findEntry(h, tag, RPM_NULL_TYPE) ? 1 : 0);
1126 * Retrieve data from header entry.
1127 * Relevant flags (others are ignored), if neither is set allocation
1128 * behavior depends on data type(!)
1129 * HEADERGET_MINMEM: return pointers to header memory
1130 * HEADERGET_ALLOC: always return malloced memory, overrides MINMEM
1132 * @todo Permit retrieval of regions other than HEADER_IMUTABLE.
1133 * @param entry header entry
1134 * @param td tag data container
1135 * @param minMem string pointers refer to header memory?
1136 * @param flags flags to control memory allocation
1137 * @return 1 on success, otherwise error.
1139 static int copyTdEntry(const indexEntry entry, rpmtd td, headerGetFlags flags)
1141 rpm_count_t count = entry->info.count;
1142 int rc = 1; /* XXX 1 on success. */
1143 /* ALLOC overrides MINMEM */
1144 int allocMem = flags & HEADERGET_ALLOC;
1145 int minMem = allocMem ? 0 : flags & HEADERGET_MINMEM;
1148 switch (entry->info.type) {
1151 * XXX This only works for
1152 * XXX "sealed" HEADER_IMMUTABLE/HEADER_SIGNATURES/HEADER_IMAGE.
1153 * XXX This will *not* work for unsealed legacy HEADER_IMAGE (i.e.
1154 * XXX a legacy header freshly read, but not yet unloaded to the rpmdb).
1156 if (ENTRY_IS_REGION(entry)) {
1157 int32_t * ei = ((int32_t *)entry->data) - 2;
1158 entryInfo pe = (entryInfo) (ei + 2);
1159 unsigned char * dataStart = (unsigned char *) (pe + ntohl(ei[0]));
1160 int32_t rdl = -entry->info.offset; /* negative offset */
1161 int32_t ril = rdl/sizeof(*pe);
1164 count = 2 * sizeof(*ei) + (ril * sizeof(*pe)) + rdl;
1165 if (entry->info.tag == HEADER_IMAGE) {
1169 count += REGION_TAG_COUNT;
1170 rdl += REGION_TAG_COUNT;
1173 td->data = xmalloc(count);
1174 ei = (int32_t *) td->data;
1178 pe = (entryInfo) memcpy(ei + 2, pe, (ril * sizeof(*pe)));
1180 dataStart = (unsigned char *) memcpy(pe + ril, dataStart, rdl);
1182 rc = regionSwab(NULL, ril, 0, pe, dataStart, dataStart + rdl, 0);
1183 /* XXX 1 on success. */
1184 rc = (rc < 0) ? 0 : 1;
1186 count = entry->length;
1188 ? memcpy(xmalloc(count), entry->data, count)
1192 case RPM_STRING_TYPE:
1194 td->data = allocMem ? xstrdup(entry->data) : entry->data;
1197 case RPM_STRING_ARRAY_TYPE:
1198 case RPM_I18NSTRING_TYPE:
1199 { const char ** ptrEntry;
1200 int tableSize = count * sizeof(char *);
1205 td->data = xmalloc(tableSize);
1206 ptrEntry = (const char **) td->data;
1209 t = xmalloc(tableSize + entry->length);
1210 td->data = (void *)t;
1211 ptrEntry = (const char **) td->data;
1213 memcpy(t, entry->data, entry->length);
1215 for (i = 0; i < count; i++) {
1223 case RPM_INT16_TYPE:
1224 case RPM_INT32_TYPE:
1225 case RPM_INT64_TYPE:
1227 td->data = xmalloc(entry->length);
1228 memcpy(td->data, entry->data, entry->length);
1230 td->data = entry->data;
1234 /* WTH? Don't mess with unknown data types... */
1239 td->type = entry->info.type;
1242 td->flags = RPMTD_IMMUTABLE;
1243 if (td->data && entry->data != td->data) {
1244 td->flags |= RPMTD_ALLOCED;
1251 * Does locale match entry in header i18n table?
1254 * The range [l,le) contains the next locale to match:
1255 * ll[_CC][.EEEEE][@dddd]
1257 * ll ISO language code (in lowercase).
1258 * CC (optional) ISO coutnry code (in uppercase).
1259 * EEEEE (optional) encoding (not really standardized).
1260 * dddd (optional) dialect.
1263 * @param td header i18n table data, NUL terminated
1264 * @param l start of locale to match
1265 * @param le end of locale to match
1266 * @return 1 on good match, 2 on weak match, 0 on no match
1268 static int headerMatchLocale(const char *td, const char *l, const char *le)
1272 /* First try a complete match. */
1273 if (strlen(td) == (le-l) && !strncmp(td, l, (le - l)))
1276 /* Next, try stripping optional dialect and matching. */
1277 for (fe = l; fe < le && *fe != '@'; fe++)
1279 if (fe < le && !strncmp(td, l, (fe - l)))
1282 /* Next, try stripping optional codeset and matching. */
1283 for (fe = l; fe < le && *fe != '.'; fe++)
1285 if (fe < le && !strncmp(td, l, (fe - l)))
1288 /* Finally, try stripping optional country code and matching. */
1289 for (fe = l; fe < le && *fe != '_'; fe++)
1291 if (fe < le && !strncmp(td, l, (fe - l)))
1298 * Return i18n string from header that matches locale.
1300 * @param entry i18n string data
1301 * @retval td tag data container
1302 * @param flags flags to control allocation
1305 static int copyI18NEntry(Header h, indexEntry entry, rpmtd td,
1306 headerGetFlags flags)
1308 const char *lang, *l, *le;
1311 td->type = RPM_STRING_TYPE;
1313 /* if no match, just return the first string */
1314 td->data = entry->data;
1316 /* XXX Drepper sez' this is the order. */
1317 if ((lang = getenv("LANGUAGE")) == NULL &&
1318 (lang = getenv("LC_ALL")) == NULL &&
1319 (lang = getenv("LC_MESSAGES")) == NULL &&
1320 (lang = getenv("LANG")) == NULL)
1323 if ((table = findEntry(h, HEADER_I18NTABLE, RPM_STRING_ARRAY_TYPE)) == NULL)
1326 for (l = lang; *l != '\0'; l = le) {
1328 char *ed, *ed_weak = NULL;
1331 while (*l && *l == ':') /* skip leading colons */
1335 for (le = l; *le && *le != ':'; le++) /* find end of this locale */
1338 /* For each entry in the header ... */
1339 for (langNum = 0, t = table->data, ed = entry->data;
1340 langNum < entry->info.count;
1341 langNum++, t += strlen(t) + 1, ed += strlen(ed) + 1) {
1343 int match = headerMatchLocale(t, l, le);
1347 } else if (match == 2) {
1358 if (flags & HEADERGET_ALLOC) {
1359 td->data = xstrdup(td->data);
1366 * Retrieve tag data from header.
1368 * @param tag tag to retrieve
1369 * @retval td tag data container
1370 * @param flags flags to control retrieval
1371 * @return 1 on success, 0 on not found
1373 static int intGetTdEntry(Header h, rpmTag tag, rpmtd td, headerGetFlags flags)
1379 /* ensure clean state */
1382 /* First find the tag */
1383 /* FIX: h modified by sort. */
1384 entry = findEntry(h, tag, RPM_NULL_TYPE);
1385 if (entry == NULL) {
1386 /* Td is zeroed above, just return... */
1390 if (flags & HEADERGET_RAW) {
1391 rc = copyTdEntry(entry, td, flags);
1393 switch (entry->info.type) {
1394 case RPM_I18NSTRING_TYPE:
1395 rc = copyI18NEntry(h, entry, td, flags);
1398 rc = copyTdEntry(entry, td, flags);
1403 /* XXX 1 on success */
1404 return ((rc == 1) ? 1 : 0);
1408 * XXX temporary kludgery until tag extensions have been converted to
1409 * take rpmtd as argument
1411 static int intGetTagExt(Header h, rpmTag tag, rpmtd td, headerTagTagFunction tagfunc)
1415 rc = tagfunc(h, td);
1420 int headerGet(Header h, rpmTag tag, rpmtd td, headerGetFlags flags)
1423 headerTagTagFunction tagfunc = NULL;
1427 if (flags & HEADERGET_EXT) {
1428 tagfunc = rpmHeaderTagFunc(tag);
1432 rc = intGetTagExt(h, tag, td, tagfunc);
1434 rc = intGetTdEntry(h, tag, td, flags);
1437 assert(tag == td->tag);
1443 static void copyData(rpmTagType type, rpm_data_t dstPtr,
1444 rpm_constdata_t srcPtr, rpm_count_t cnt, int dataLength)
1447 case RPM_STRING_ARRAY_TYPE:
1448 case RPM_I18NSTRING_TYPE:
1449 { const char ** av = (const char **) srcPtr;
1452 while (cnt-- > 0 && dataLength > 0) {
1454 if ((s = *av++) == NULL)
1458 } while (s[-1] && --dataLength > 0);
1463 memmove(dstPtr, srcPtr, dataLength);
1469 * Return (malloc'ed) copy of entry data.
1470 * @param type entry data type
1471 * @param p entry data
1472 * @param c entry item count
1473 * @retval lengthPtr no. bytes in returned data
1474 * @return (malloc'ed) copy of entry data, NULL on error
1477 grabData(rpmTagType type, rpm_constdata_t p, rpm_count_t c, int * lengthPtr)
1479 rpm_data_t data = NULL;
1482 length = dataLength(type, p, c, 0, NULL);
1484 data = xmalloc(length);
1485 copyData(type, data, p, c, length);
1489 *lengthPtr = length;
1493 static int intAddEntry(Header h, rpmtd td)
1499 /* Count must always be >= 1 for headerAddEntry. */
1503 if (hdrchkType(td->type))
1505 if (hdrchkData(td->count))
1509 data = grabData(td->type, td->data, td->count, &length);
1510 if (data == NULL || length <= 0)
1513 /* Allocate more index space if necessary */
1514 if (h->indexUsed == h->indexAlloced) {
1515 h->indexAlloced += INDEX_MALLOC_SIZE;
1516 h->index = xrealloc(h->index, h->indexAlloced * sizeof(*h->index));
1519 /* Fill in the index */
1520 entry = h->index + h->indexUsed;
1521 entry->info.tag = td->tag;
1522 entry->info.type = td->type;
1523 entry->info.count = td->count;
1524 entry->info.offset = 0;
1526 entry->length = length;
1528 if (h->indexUsed > 0 && td->tag < h->index[h->indexUsed-1].info.tag)
1529 h->flags &= ~HEADERFLAG_SORTED;
1535 static int intAppendEntry(Header h, rpmtd td)
1540 if (td->type == RPM_STRING_TYPE || td->type == RPM_I18NSTRING_TYPE) {
1541 /* we can't do this */
1545 /* Find the tag entry in the header. */
1546 entry = findEntry(h, td->tag, td->type);
1550 length = dataLength(td->type, td->data, td->count, 0, NULL);
1554 if (ENTRY_IN_REGION(entry)) {
1555 char * t = xmalloc(entry->length + length);
1556 memcpy(t, entry->data, entry->length);
1558 entry->info.offset = 0;
1560 entry->data = xrealloc(entry->data, entry->length + length);
1562 copyData(td->type, ((char *) entry->data) + entry->length,
1563 td->data, td->count, length);
1565 entry->length += length;
1567 entry->info.count += td->count;
1572 int headerPut(Header h, rpmtd td, headerPutFlags flags)
1577 if (flags & HEADERPUT_APPEND) {
1578 rc = findEntry(h, td->tag, td->type) ?
1579 intAppendEntry(h, td) :
1582 rc = intAddEntry(h, td);
1587 int headerAddI18NString(Header h, rpmTag tag, const char * string,
1590 indexEntry table, entry;
1591 const char ** strArray;
1594 rpm_count_t i, langNum;
1597 table = findEntry(h, HEADER_I18NTABLE, RPM_STRING_ARRAY_TYPE);
1598 entry = findEntry(h, tag, RPM_I18NSTRING_TYPE);
1600 if (!table && entry)
1601 return 0; /* this shouldn't ever happen!! */
1603 if (!table && !entry) {
1604 const char * charArray[2];
1605 rpm_count_t count = 0;
1606 if (!lang || (lang[0] == 'C' && lang[1] == '\0')) {
1607 charArray[count++] = "C";
1609 charArray[count++] = "C";
1610 charArray[count++] = lang;
1612 if (!headerAddEntry(h, HEADER_I18NTABLE, RPM_STRING_ARRAY_TYPE,
1615 table = findEntry(h, HEADER_I18NTABLE, RPM_STRING_ARRAY_TYPE);
1620 if (!lang) lang = "C";
1622 { const char * l = table->data;
1623 for (langNum = 0; langNum < table->info.count; langNum++) {
1624 if (!strcmp(l, lang)) break;
1629 if (langNum >= table->info.count) {
1630 length = strlen(lang) + 1;
1631 if (ENTRY_IN_REGION(table)) {
1632 char * t = xmalloc(table->length + length);
1633 memcpy(t, table->data, table->length);
1635 table->info.offset = 0;
1637 table->data = xrealloc(table->data, table->length + length);
1638 memmove(((char *)table->data) + table->length, lang, length);
1639 table->length += length;
1640 table->info.count++;
1645 strArray = xmalloc(sizeof(*strArray) * (langNum + 1));
1646 for (i = 0; i < langNum; i++)
1648 strArray[langNum] = string;
1649 rc = headerAddEntry(h, tag, RPM_I18NSTRING_TYPE, strArray, langNum + 1);
1652 } else if (langNum >= entry->info.count) {
1653 ghosts = langNum - entry->info.count;
1655 length = strlen(string) + 1 + ghosts;
1656 if (ENTRY_IN_REGION(entry)) {
1657 char * t = xmalloc(entry->length + length);
1658 memcpy(t, entry->data, entry->length);
1660 entry->info.offset = 0;
1662 entry->data = xrealloc(entry->data, entry->length + length);
1664 memset(((char *)entry->data) + entry->length, '\0', ghosts);
1665 memmove(((char *)entry->data) + entry->length + ghosts, string, strlen(string)+1);
1667 entry->length += length;
1668 entry->info.count = langNum + 1;
1670 char *b, *be, *e, *ee, *t;
1673 /* Set beginning/end pointers to previous data */
1674 b = be = e = ee = entry->data;
1675 for (i = 0; i < table->info.count; i++) {
1678 ee += strlen(ee) + 1;
1683 /* Get storage for new buffer */
1685 sn = strlen(string) + 1;
1687 length = bn + sn + en;
1688 t = buf = xmalloc(length);
1690 /* Copy values into new storage */
1693 memcpy(t, string, sn);
1698 /* Replace i18N string array */
1699 entry->length -= strlen(be) + 1;
1700 entry->length += sn;
1702 if (ENTRY_IN_REGION(entry)) {
1703 entry->info.offset = 0;
1705 entry->data = _free(entry->data);
1712 int headerMod(Header h, rpmtd td)
1719 /* First find the tag */
1720 entry = findEntry(h, td->tag, td->type);
1725 data = grabData(td->type, td->data, td->count, &length);
1726 if (data == NULL || length <= 0)
1729 /* make sure entry points to the first occurence of this tag */
1730 while (entry > h->index && (entry - 1)->info.tag == td->tag)
1733 /* free after we've grabbed the new data in case the two are intertwined;
1734 that's a bad idea but at least we won't break */
1735 oldData = entry->data;
1737 entry->info.count = td->count;
1738 entry->info.type = td->type;
1740 entry->length = length;
1742 if (ENTRY_IN_REGION(entry)) {
1743 entry->info.offset = 0;
1745 oldData = _free(oldData);
1751 * Header tag iterator data structure.
1753 struct headerIterator_s {
1754 Header h; /*!< Header being iterated. */
1755 int next_index; /*!< Next tag index. */
1758 HeaderIterator headerFreeIterator(HeaderIterator hi)
1761 hi->h = headerFree(hi->h);
1767 HeaderIterator headerInitIterator(Header h)
1769 HeaderIterator hi = xmalloc(sizeof(*hi));
1773 hi->h = headerLink(h);
1778 int headerNext(HeaderIterator hi, rpmtd td)
1781 int slot = hi->next_index;
1782 indexEntry entry = NULL;
1788 for (slot = hi->next_index; slot < h->indexUsed; slot++) {
1789 entry = h->index + slot;
1790 if (!ENTRY_IS_REGION(entry))
1793 hi->next_index = slot;
1794 if (entry == NULL || slot >= h->indexUsed)
1800 td->tag = entry->info.tag;
1802 rc = copyTdEntry(entry, td, HEADERGET_DEFAULT);
1804 /* XXX 1 on success */
1805 return ((rc == 1) ? 1 : 0);
1809 * Duplicate a header.
1811 * @return new header instance
1813 Header headerCopy(Header h)
1815 Header nh = headerNew();
1819 hi = headerInitIterator(h);
1820 while (headerNext(hi, &td)) {
1821 if (rpmtdCount(&td) > 0) {
1822 (void) headerPut(nh, &td, HEADERPUT_DEFAULT);
1826 hi = headerFreeIterator(hi);
1828 return headerReload(nh, HEADER_IMAGE);
1831 void headerCopyTags(Header headerFrom, Header headerTo,
1832 const rpmTag * tagstocopy)
1837 if (headerFrom == headerTo)
1840 for (p = tagstocopy; *p != 0; p++) {
1841 if (headerIsEntry(headerTo, *p))
1843 if (!headerGet(headerFrom, *p, &td, HEADERGET_MINMEM))
1845 (void) headerPut(headerTo, &td, HEADERPUT_DEFAULT);