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 * @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;
145 h->flags |= HEADERFLAG_SORTED;
147 h->index = (h->indexAlloced
148 ? xcalloc(h->indexAlloced, sizeof(*h->index))
152 return headerLink(h);
155 int headerVerifyInfo(int il, int dl, const void * pev, void * iv, int negate)
157 entryInfo pe = (entryInfo) pev;
161 for (i = 0; i < il; i++) {
162 info->tag = ntohl(pe[i].tag);
163 info->type = ntohl(pe[i].type);
164 info->offset = ntohl(pe[i].offset);
166 info->offset = -info->offset;
167 info->count = ntohl(pe[i].count);
169 if (hdrchkType(info->type))
171 if (hdrchkAlign(info->type, info->offset))
173 if (!negate && hdrchkRange(dl, info->offset))
175 if (hdrchkData(info->count))
184 static int indexCmp(const void * avp, const void * bvp)
186 indexEntry ap = (indexEntry) avp, bp = (indexEntry) bvp;
187 return (ap->info.tag - bp->info.tag);
190 void headerSort(Header h)
192 if (!(h->flags & HEADERFLAG_SORTED)) {
193 qsort(h->index, h->indexUsed, sizeof(*h->index), indexCmp);
194 h->flags |= HEADERFLAG_SORTED;
200 static int offsetCmp(const void * avp, const void * bvp)
202 indexEntry ap = (indexEntry) avp, bp = (indexEntry) bvp;
203 int rc = (ap->info.offset - bp->info.offset);
206 /* Within a region, entries sort by address. Added drips sort by tag. */
207 if (ap->info.offset < 0)
208 rc = (((char *)ap->data) - ((char *)bp->data));
210 rc = (ap->info.tag - bp->info.tag);
216 * Restore tags in header to original ordering.
219 void headerUnsort(Header h)
221 qsort(h->index, h->indexUsed, sizeof(*h->index), offsetCmp);
224 unsigned headerSizeof(Header h, enum hMagic magicp)
227 unsigned int size = 0;
228 unsigned int pad = 0;
237 case HEADER_MAGIC_YES:
238 size += sizeof(rpm_header_magic);
240 case HEADER_MAGIC_NO:
244 size += 2 * sizeof(int32_t); /* count of index entries */
246 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 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 * This code is way more twisty than I would like.
347 * A bug with RPM_I18NSTRING_TYPE in rpm-2.5.x (fixed in August 1998)
348 * causes the offset and length of elements in a header region to disagree
349 * regarding the total length of the region data.
351 * The "fix" is to compute the size using both offset and length and
352 * return the larger of the two numbers as the size of the region.
353 * Kinda like computing left and right Riemann sums of the data elements
354 * to determine the size of a data structure, go figger :-).
356 * There's one other twist if a header region tag is in the set to be swabbed,
357 * as the data for a header region is located after all other tag data.
359 * @param entry header entry
360 * @param il no. of entries
361 * @param dl start no. bytes of data
362 * @param pe header physical entry pointer (swapped)
363 * @param dataStart header data start
364 * @param dataEnd header data end
365 * @param regionid region offset
366 * @return no. bytes of data in region, -1 on error
368 static int regionSwab(indexEntry entry, int il, int dl,
370 unsigned char * dataStart,
371 const unsigned char * dataEnd,
374 unsigned char * tprev = NULL;
375 unsigned char * t = NULL;
378 struct indexEntry_s ieprev;
380 memset(&ieprev, 0, sizeof(ieprev));
381 for (; il > 0; il--, pe++) {
382 struct indexEntry_s ie;
385 ie.info.tag = ntohl(pe->tag);
386 ie.info.type = ntohl(pe->type);
387 ie.info.count = ntohl(pe->count);
388 ie.info.offset = ntohl(pe->offset);
390 if (hdrchkType(ie.info.type))
392 if (hdrchkData(ie.info.count))
394 if (hdrchkData(ie.info.offset))
396 if (hdrchkAlign(ie.info.type, ie.info.offset))
399 ie.data = t = dataStart + ie.info.offset;
400 if (dataEnd && t >= dataEnd)
403 ie.length = dataLength(ie.info.type, ie.data, ie.info.count, 1, dataEnd);
404 if (ie.length < 0 || hdrchkData(ie.length))
410 ie.info.offset = regionid;
411 *entry = ie; /* structure assignment */
417 if (typeSizes[type] > 1) {
419 diff = typeSizes[type] - (dl % typeSizes[type]);
420 if (diff != typeSizes[type]) {
422 if (ieprev.info.type == RPM_I18NSTRING_TYPE)
423 ieprev.length += diff;
426 tdel = (tprev ? (t - tprev) : 0);
427 if (ieprev.info.type == RPM_I18NSTRING_TYPE)
428 tdel = ieprev.length;
430 if (ie.info.tag >= HEADER_I18NTABLE) {
434 /* XXX HEADER_IMAGE tags don't include region sub-tag. */
435 if (ie.info.tag == HEADER_IMAGE)
436 tprev -= REGION_TAG_COUNT;
439 /* Perform endian conversions */
440 switch (ntohl(pe->type)) {
442 { uint64_t * it = (uint64_t *)t;
443 for (; ie.info.count > 0; ie.info.count--, it += 1) {
444 if (dataEnd && ((unsigned char *)it) >= dataEnd)
448 t = (unsigned char *) it;
451 { int32_t * it = (int32_t *)t;
452 for (; ie.info.count > 0; ie.info.count--, it += 1) {
453 if (dataEnd && ((unsigned char *)it) >= dataEnd)
457 t = (unsigned char *) it;
460 { int16_t * it = (int16_t *) t;
461 for (; ie.info.count > 0; ie.info.count--, it += 1) {
462 if (dataEnd && ((unsigned char *)it) >= dataEnd)
466 t = (unsigned char *) it;
475 ieprev = ie; /* structure assignment */
478 tdel = (tprev ? (t - tprev) : 0);
482 * There are two hacks here:
483 * 1) tl is 16b (i.e. REGION_TAG_COUNT) short while doing headerReload().
484 * 2) the 8/98 rpm bug with inserting i18n tags needs to use tl, not dl.
486 if (tl+REGION_TAG_COUNT == dl)
487 tl += REGION_TAG_COUNT;
495 * @retval *lengthPtr no. bytes in unloaded header blob
496 * @return unloaded header blob (NULL on error)
498 static void * doHeaderUnload(Header h,
512 int drlen, ndribbles;
516 /* Sort entries by (offset,tag). */
519 /* Compute (il,dl) for all tags, including those deleted in region. */
521 drlen = ndribbles = driplen = ndrips = 0;
522 for (i = 0, entry = h->index; i < h->indexUsed; i++, entry++) {
523 if (ENTRY_IS_REGION(entry)) {
524 int32_t rdl = -entry->info.offset; /* negative offset */
525 int32_t ril = rdl/sizeof(*pe);
526 int rid = entry->info.offset;
529 dl += entry->rdlen + entry->info.count;
530 /* XXX Legacy regions do not include the region tag and data. */
531 if (i == 0 && (h->flags & HEADERFLAG_LEGACY))
534 /* Skip rest of entries in region, but account for dribbles. */
535 for (; i < h->indexUsed && entry->info.offset <= rid+1; i++, entry++) {
536 if (entry->info.offset <= rid)
540 type = entry->info.type;
541 if (typeSizes[type] > 1) {
543 diff = typeSizes[type] - (dl % typeSizes[type]);
544 if (diff != typeSizes[type]) {
553 drlen += entry->length;
561 /* Ignore deleted drips. */
562 if (entry->data == NULL || entry->length <= 0)
566 type = entry->info.type;
567 if (typeSizes[type] > 1) {
569 diff = typeSizes[type] - (dl % typeSizes[type]);
570 if (diff != typeSizes[type]) {
580 driplen += entry->length;
584 /* Sanity checks on header intro. */
585 if (hdrchkTags(il) || hdrchkData(dl))
588 len = sizeof(il) + sizeof(dl) + (il * sizeof(*pe)) + dl;
594 pe = (entryInfo) &ei[2];
595 dataStart = te = (char *) (pe + il);
598 for (i = 0, entry = h->index; i < h->indexUsed; i++, entry++) {
604 if (entry->data == NULL || entry->length <= 0)
607 t = (unsigned char*)te;
608 pe->tag = htonl(entry->info.tag);
609 pe->type = htonl(entry->info.type);
610 pe->count = htonl(entry->info.count);
612 if (ENTRY_IS_REGION(entry)) {
613 int32_t rdl = -entry->info.offset; /* negative offset */
614 int32_t ril = rdl/sizeof(*pe) + ndribbles;
615 int rid = entry->info.offset;
617 src = (char *)entry->data;
618 rdlen = entry->rdlen;
620 /* XXX Legacy regions do not include the region tag and data. */
621 if (i == 0 && (h->flags & HEADERFLAG_LEGACY)) {
625 memcpy(pe+1, src, rdl);
626 memcpy(te, src + rdl, rdlen);
629 pe->offset = htonl(te - dataStart);
632 stei[2] = htonl(-rdl-entry->info.count);
634 memcpy(te, stei, entry->info.count);
635 te += entry->info.count;
637 rdlen += entry->info.count;
639 count = regionSwab(NULL, ril, 0, pe, t, NULL, 0);
645 memcpy(pe+1, src + sizeof(*pe), ((ril-1) * sizeof(*pe)));
646 memcpy(te, src + (ril * sizeof(*pe)), rdlen+entry->info.count+drlen);
649 entryInfo se = (entryInfo)src;
650 int off = ntohl(se->offset);
651 pe->offset = (off) ? htonl(te - dataStart) : htonl(off);
653 te += entry->info.count + drlen;
655 count = regionSwab(NULL, ril, 0, pe, t, NULL, 0);
656 if (count != (rdlen + entry->info.count + drlen))
660 /* Skip rest of entries in region. */
661 while (i < h->indexUsed && entry->info.offset <= rid+1) {
671 /* Ignore deleted drips. */
672 if (entry->data == NULL || entry->length <= 0)
676 type = entry->info.type;
677 if (typeSizes[type] > 1) {
679 diff = typeSizes[type] - ((te - dataStart) % typeSizes[type]);
680 if (diff != typeSizes[type]) {
687 pe->offset = htonl(te - dataStart);
689 /* copy data w/ endian conversions */
690 switch (entry->info.type) {
692 count = entry->info.count;
695 *((uint64_t *)te) = htonll(*((uint64_t *)src));
696 te += sizeof(uint64_t);
697 src += sizeof(uint64_t);
702 count = entry->info.count;
705 *((int32_t *)te) = htonl(*((int32_t *)src));
706 te += sizeof(int32_t);
707 src += sizeof(int32_t);
712 count = entry->info.count;
715 *((int16_t *)te) = htons(*((int16_t *)src));
716 te += sizeof(int16_t);
717 src += sizeof(int16_t);
722 memcpy(te, entry->data, entry->length);
729 /* Insure that there are no memcpy underruns/overruns. */
730 if (((char *)pe) != dataStart)
732 if ((((char *)ei)+len) != te)
738 h->flags &= ~HEADERFLAG_SORTED;
748 void * headerUnload(Header h)
751 void * uh = doHeaderUnload(h, &length);
756 * Find matching (tag,type) entry in header.
758 * @param tag entry tag
759 * @param type entry type
760 * @return header entry
763 indexEntry findEntry(Header h, rpmTag tag, rpmTagType type)
765 indexEntry entry, entry2, last;
766 struct indexEntry_s key;
768 if (h == NULL) return NULL;
769 if (!(h->flags & HEADERFLAG_SORTED)) headerSort(h);
774 bsearch(&key, h->index, h->indexUsed, sizeof(*h->index), indexCmp);
778 if (type == RPM_NULL_TYPE)
782 while (entry->info.tag == tag && entry->info.type != type &&
783 entry > h->index) entry--;
785 if (entry->info.tag == tag && entry->info.type == type)
788 last = h->index + h->indexUsed;
789 /* FIX: entry2 = entry. Code looks bogus as well. */
790 while (entry2->info.tag == tag && entry2->info.type != type &&
791 entry2 < last) entry2++;
793 if (entry->info.tag == tag && entry->info.type == type)
799 int headerDel(Header h, rpmTag tag)
801 indexEntry last = h->index + h->indexUsed;
802 indexEntry entry, first;
805 entry = findEntry(h, tag, RPM_NULL_TYPE);
806 if (!entry) return 1;
808 /* Make sure entry points to the first occurence of this tag. */
809 while (entry > h->index && (entry - 1)->info.tag == tag)
812 /* Free data for tags being removed. */
813 for (first = entry; first < last; first++) {
815 if (first->info.tag != tag)
820 if (ENTRY_IN_REGION(first))
825 ne = (first - entry);
830 memmove(entry, first, (ne * sizeof(*entry)));
836 Header headerLoad(void * uh)
838 int32_t * ei = (int32_t *) uh;
839 int32_t il = ntohl(ei[0]); /* index length */
840 int32_t dl = ntohl(ei[1]); /* data length */
841 size_t pvlen = sizeof(il) + sizeof(dl) +
842 (il * sizeof(struct entryInfo_s)) + dl;
846 unsigned char * dataStart;
847 unsigned char * dataEnd;
852 /* Sanity checks on header intro. */
853 if (hdrchkTags(il) || hdrchkData(dl))
857 pe = (entryInfo) &ei[2];
858 dataStart = (unsigned char *) (pe + il);
859 dataEnd = dataStart + dl;
861 h = xcalloc(1, sizeof(*h));
863 h->indexAlloced = il + 1;
866 h->index = xcalloc(h->indexAlloced, sizeof(*h->index));
867 h->flags |= HEADERFLAG_SORTED;
873 if (!(htonl(pe->tag) < HEADER_I18NTABLE)) {
874 h->flags |= HEADERFLAG_LEGACY;
875 entry->info.type = REGION_TAG_TYPE;
876 entry->info.tag = HEADER_IMAGE;
877 entry->info.count = REGION_TAG_COUNT;
878 entry->info.offset = ((unsigned char *)pe - dataStart); /* negative offset */
881 entry->length = pvlen - sizeof(il) - sizeof(dl);
882 rdlen = regionSwab(entry+1, il, 0, pe, dataStart, dataEnd, entry->info.offset);
883 #if 0 /* XXX don't check, the 8/98 i18n bug fails here. */
887 entry->rdlen = rdlen;
894 h->flags &= ~HEADERFLAG_LEGACY;
896 entry->info.type = htonl(pe->type);
897 entry->info.count = htonl(pe->count);
899 if (hdrchkType(entry->info.type))
901 if (hdrchkTags(entry->info.count))
904 { int off = ntohl(pe->offset);
909 size_t nb = REGION_TAG_COUNT;
911 /* XXX Hmm, why the copy? */
912 memcpy(&stei, dataStart + off, nb);
913 rdl = -ntohl(stei[2]); /* negative offset */
914 ril = rdl/sizeof(*pe);
915 if (hdrchkTags(ril) || hdrchkData(rdl))
917 entry->info.tag = htonl(pe->tag);
920 rdl = (ril * sizeof(struct entryInfo_s));
921 entry->info.tag = HEADER_IMAGE;
924 entry->info.offset = -rdl; /* negative offset */
927 entry->length = pvlen - sizeof(il) - sizeof(dl);
928 rdlen = regionSwab(entry+1, ril-1, 0, pe+1, dataStart, dataEnd, entry->info.offset);
931 entry->rdlen = rdlen;
933 if (ril < h->indexUsed) {
934 indexEntry newEntry = entry + ril;
935 int ne = (h->indexUsed - ril);
936 int rid = entry->info.offset+1;
939 /* Load dribble entries from region. */
940 rc = regionSwab(newEntry, ne, 0, pe+ril, dataStart, dataEnd, rid);
945 { indexEntry firstEntry = newEntry;
946 int save = h->indexUsed;
949 /* Dribble entries replace duplicate region entries. */
951 for (j = 0; j < ne; j++, newEntry++) {
952 (void) headerDel(h, newEntry->info.tag);
953 if (newEntry->info.tag == RPMTAG_BASENAMES)
954 (void) headerDel(h, RPMTAG_OLDFILENAMES);
957 /* If any duplicate entries were replaced, move new entries down. */
958 if (h->indexUsed < (save - ne)) {
959 memmove(h->index + h->indexUsed, firstEntry,
960 (ne * sizeof(*entry)));
967 h->flags &= ~HEADERFLAG_SORTED;
969 h->flags |= HEADERFLAG_ALLOCATED;
975 h->index = _free(h->index);
981 Header headerReload(Header h, rpmTag tag)
985 void * uh = doHeaderUnload(h, &length);
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)
1022 * Read (and load) header from file handle.
1023 * @param fd file handle
1024 * @param magicp read (and verify) 8 bytes of (magic, 0)?
1025 * @return header (or NULL on error)
1027 Header headerRead(FD_t fd, enum hMagic magicp)
1031 int32_t * ei = NULL;
1039 memset(block, 0, sizeof(block));
1041 if (magicp == HEADER_MAGIC_YES)
1045 if (timedRead(fd, (char *)block, i*sizeof(*block)) != (i * sizeof(*block)))
1050 if (magicp == HEADER_MAGIC_YES) {
1052 if (memcmp(&magic, rpm_header_magic, sizeof(magic)))
1054 reserved = block[i++];
1057 il = ntohl(block[i]); i++;
1058 dl = ntohl(block[i]); i++;
1060 len = sizeof(il) + sizeof(dl) + (il * sizeof(struct entryInfo_s)) + dl;
1062 /* Sanity checks on header intro. */
1063 if (hdrchkTags(il) || hdrchkData(dl) || len > headerMaxbytes)
1069 len -= sizeof(il) + sizeof(dl);
1072 if (timedRead(fd, (char *)&ei[2], len) != len)
1078 if (h == NULL && ei != NULL) {
1084 int headerWrite(FD_t fd, Header h, enum hMagic magicp)
1092 uh = doHeaderUnload(h, &length);
1096 case HEADER_MAGIC_YES:
1097 nb = Fwrite(rpm_header_magic, sizeof(uint8_t), sizeof(rpm_header_magic), fd);
1098 if (nb != sizeof(rpm_header_magic))
1101 case HEADER_MAGIC_NO:
1105 nb = Fwrite(uh, sizeof(char), length, fd);
1109 return (nb == length ? 0 : 1);
1112 int headerIsEntry(Header h, rpmTag tag)
1114 /* FIX: h modified by sort. */
1115 return (findEntry(h, tag, RPM_NULL_TYPE) ? 1 : 0);
1120 * Retrieve data from header entry.
1121 * Relevant flags (others are ignored), if neither is set allocation
1122 * behavior depends on data type(!)
1123 * HEADERGET_MINMEM: return pointers to header memory
1124 * HEADERGET_ALLOC: always return malloced memory, overrides MINMEM
1126 * @todo Permit retrieval of regions other than HEADER_IMUTABLE.
1127 * @param entry header entry
1128 * @param td tag data container
1129 * @param minMem string pointers refer to header memory?
1130 * @param flags flags to control memory allocation
1131 * @return 1 on success, otherwise error.
1133 static int copyTdEntry(const indexEntry entry, rpmtd td, headerGetFlags flags)
1135 rpm_count_t count = entry->info.count;
1136 int rc = 1; /* XXX 1 on success. */
1137 /* ALLOC overrides MINMEM */
1138 int allocMem = flags & HEADERGET_ALLOC;
1139 int minMem = allocMem ? 0 : flags & HEADERGET_MINMEM;
1140 int argvArray = (flags & HEADERGET_ARGV) ? 1 : 0;
1143 td->flags = RPMTD_IMMUTABLE;
1144 switch (entry->info.type) {
1147 * XXX This only works for
1148 * XXX "sealed" HEADER_IMMUTABLE/HEADER_SIGNATURES/HEADER_IMAGE.
1149 * XXX This will *not* work for unsealed legacy HEADER_IMAGE (i.e.
1150 * XXX a legacy header freshly read, but not yet unloaded to the rpmdb).
1152 if (ENTRY_IS_REGION(entry)) {
1153 int32_t * ei = ((int32_t *)entry->data) - 2;
1154 entryInfo pe = (entryInfo) (ei + 2);
1155 unsigned char * dataStart = (unsigned char *) (pe + ntohl(ei[0]));
1156 int32_t rdl = -entry->info.offset; /* negative offset */
1157 int32_t ril = rdl/sizeof(*pe);
1160 count = 2 * sizeof(*ei) + (ril * sizeof(*pe)) + rdl;
1161 if (entry->info.tag == HEADER_IMAGE) {
1165 count += REGION_TAG_COUNT;
1166 rdl += REGION_TAG_COUNT;
1169 td->data = xmalloc(count);
1170 ei = (int32_t *) td->data;
1174 pe = (entryInfo) memcpy(ei + 2, pe, (ril * sizeof(*pe)));
1176 dataStart = (unsigned char *) memcpy(pe + ril, dataStart, rdl);
1178 rc = regionSwab(NULL, ril, 0, pe, dataStart, dataStart + rdl, 0);
1179 /* don't return data on failure */
1181 td->data = _free(td->data);
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 + argvArray) * 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++) {
1222 td->flags |= RPMTD_ARGV;
1227 case RPM_INT16_TYPE:
1228 case RPM_INT32_TYPE:
1229 case RPM_INT64_TYPE:
1231 td->data = xmalloc(entry->length);
1232 memcpy(td->data, entry->data, entry->length);
1234 td->data = entry->data;
1238 /* WTH? Don't mess with unknown data types... */
1243 td->type = entry->info.type;
1246 if (td->data && entry->data != td->data) {
1247 td->flags |= RPMTD_ALLOCED;
1254 * Does locale match entry in header i18n table?
1257 * The range [l,le) contains the next locale to match:
1258 * ll[_CC][.EEEEE][@dddd]
1260 * ll ISO language code (in lowercase).
1261 * CC (optional) ISO coutnry code (in uppercase).
1262 * EEEEE (optional) encoding (not really standardized).
1263 * dddd (optional) dialect.
1266 * @param td header i18n table data, NUL terminated
1267 * @param l start of locale to match
1268 * @param le end of locale to match
1269 * @return 1 on good match, 2 on weak match, 0 on no match
1271 static int headerMatchLocale(const char *td, const char *l, const char *le)
1275 /* First try a complete match. */
1276 if (strlen(td) == (le-l) && !strncmp(td, l, (le - l)))
1279 /* Next, try stripping optional dialect and matching. */
1280 for (fe = l; fe < le && *fe != '@'; fe++)
1282 if (fe < le && !strncmp(td, l, (fe - l)))
1285 /* Next, try stripping optional codeset and matching. */
1286 for (fe = l; fe < le && *fe != '.'; fe++)
1288 if (fe < le && !strncmp(td, l, (fe - l)))
1291 /* Finally, try stripping optional country code and matching. */
1292 for (fe = l; fe < le && *fe != '_'; fe++)
1294 if (fe < le && !strncmp(td, l, (fe - l)))
1301 * Return i18n string from header that matches locale.
1303 * @param entry i18n string data
1304 * @retval td tag data container
1305 * @param flags flags to control allocation
1308 static int copyI18NEntry(Header h, indexEntry entry, rpmtd td,
1309 headerGetFlags flags)
1311 const char *lang, *l, *le;
1314 td->type = RPM_STRING_TYPE;
1316 /* if no match, just return the first string */
1317 td->data = entry->data;
1319 /* XXX Drepper sez' this is the order. */
1320 if ((lang = getenv("LANGUAGE")) == NULL &&
1321 (lang = getenv("LC_ALL")) == NULL &&
1322 (lang = getenv("LC_MESSAGES")) == NULL &&
1323 (lang = getenv("LANG")) == NULL)
1326 if ((table = findEntry(h, HEADER_I18NTABLE, RPM_STRING_ARRAY_TYPE)) == NULL)
1329 for (l = lang; *l != '\0'; l = le) {
1331 char *ed, *ed_weak = NULL;
1334 while (*l && *l == ':') /* skip leading colons */
1338 for (le = l; *le && *le != ':'; le++) /* find end of this locale */
1341 /* For each entry in the header ... */
1342 for (langNum = 0, t = table->data, ed = entry->data;
1343 langNum < entry->info.count;
1344 langNum++, t += strlen(t) + 1, ed += strlen(ed) + 1) {
1346 int match = headerMatchLocale(t, l, le);
1350 } else if (match == 2) {
1361 if (flags & HEADERGET_ALLOC) {
1362 td->data = xstrdup(td->data);
1363 td->flags |= RPMTD_ALLOCED;
1370 * Retrieve tag data from header.
1372 * @param tag tag to retrieve
1373 * @retval td tag data container
1374 * @param flags flags to control retrieval
1375 * @return 1 on success, 0 on not found
1377 static int intGetTdEntry(Header h, rpmTag tag, rpmtd td, headerGetFlags flags)
1383 /* ensure clean state */
1386 /* First find the tag */
1387 /* FIX: h modified by sort. */
1388 entry = findEntry(h, tag, RPM_NULL_TYPE);
1389 if (entry == NULL) {
1390 /* Td is zeroed above, just return... */
1394 if (flags & HEADERGET_RAW) {
1395 rc = copyTdEntry(entry, td, flags);
1397 switch (entry->info.type) {
1398 case RPM_I18NSTRING_TYPE:
1399 rc = copyI18NEntry(h, entry, td, flags);
1402 rc = copyTdEntry(entry, td, flags);
1407 /* XXX 1 on success */
1408 return ((rc == 1) ? 1 : 0);
1412 * XXX temporary kludgery until tag extensions have been converted to
1413 * take rpmtd as argument
1415 static int intGetTagExt(Header h, rpmTag tag, rpmtd td, headerTagTagFunction tagfunc)
1419 rc = tagfunc(h, td);
1424 int headerGet(Header h, rpmTag tag, rpmtd td, headerGetFlags flags)
1427 headerTagTagFunction tagfunc = NULL;
1431 if (flags & HEADERGET_EXT) {
1432 tagfunc = rpmHeaderTagFunc(tag);
1436 rc = intGetTagExt(h, tag, td, tagfunc);
1438 rc = intGetTdEntry(h, tag, td, flags);
1441 assert(tag == td->tag);
1447 static void copyData(rpmTagType type, rpm_data_t dstPtr,
1448 rpm_constdata_t srcPtr, rpm_count_t cnt, int dataLength)
1451 case RPM_STRING_ARRAY_TYPE:
1452 case RPM_I18NSTRING_TYPE:
1453 { const char ** av = (const char **) srcPtr;
1456 while (cnt-- > 0 && dataLength > 0) {
1458 if ((s = *av++) == NULL)
1462 } while (s[-1] && --dataLength > 0);
1467 memmove(dstPtr, srcPtr, dataLength);
1473 * Return (malloc'ed) copy of entry data.
1474 * @param type entry data type
1475 * @param p entry data
1476 * @param c entry item count
1477 * @retval lengthPtr no. bytes in returned data
1478 * @return (malloc'ed) copy of entry data, NULL on error
1481 grabData(rpmTagType type, rpm_constdata_t p, rpm_count_t c, int * lengthPtr)
1483 rpm_data_t data = NULL;
1486 length = dataLength(type, p, c, 0, NULL);
1488 data = xmalloc(length);
1489 copyData(type, data, p, c, length);
1493 *lengthPtr = length;
1497 static int intAddEntry(Header h, rpmtd td)
1503 /* Count must always be >= 1 for headerAddEntry. */
1507 if (hdrchkType(td->type))
1509 if (hdrchkData(td->count))
1513 data = grabData(td->type, td->data, td->count, &length);
1514 if (data == NULL || length <= 0)
1517 /* Allocate more index space if necessary */
1518 if (h->indexUsed == h->indexAlloced) {
1519 h->indexAlloced += INDEX_MALLOC_SIZE;
1520 h->index = xrealloc(h->index, h->indexAlloced * sizeof(*h->index));
1523 /* Fill in the index */
1524 entry = h->index + h->indexUsed;
1525 entry->info.tag = td->tag;
1526 entry->info.type = td->type;
1527 entry->info.count = td->count;
1528 entry->info.offset = 0;
1530 entry->length = length;
1532 if (h->indexUsed > 0 && td->tag < h->index[h->indexUsed-1].info.tag)
1533 h->flags &= ~HEADERFLAG_SORTED;
1539 static int intAppendEntry(Header h, rpmtd td)
1544 if (td->type == RPM_STRING_TYPE || td->type == RPM_I18NSTRING_TYPE) {
1545 /* we can't do this */
1549 /* Find the tag entry in the header. */
1550 entry = findEntry(h, td->tag, td->type);
1554 length = dataLength(td->type, td->data, td->count, 0, NULL);
1558 if (ENTRY_IN_REGION(entry)) {
1559 char * t = xmalloc(entry->length + length);
1560 memcpy(t, entry->data, entry->length);
1562 entry->info.offset = 0;
1564 entry->data = xrealloc(entry->data, entry->length + length);
1566 copyData(td->type, ((char *) entry->data) + entry->length,
1567 td->data, td->count, length);
1569 entry->length += length;
1571 entry->info.count += td->count;
1576 int headerPut(Header h, rpmtd td, headerPutFlags flags)
1581 if (flags & HEADERPUT_APPEND) {
1582 rc = findEntry(h, td->tag, td->type) ?
1583 intAppendEntry(h, td) :
1586 rc = intAddEntry(h, td);
1592 * Sanity check data types against tag table before putting. Assume
1593 * append on all array-types.
1595 static int headerPutType(Header h, rpmTag tag, rpmTagType reqtype,
1596 rpm_constdata_t data, rpm_count_t size)
1599 rpmTagType type = rpmTagGetType(tag);
1600 headerPutFlags flags = HEADERPUT_APPEND;
1603 /* Basic sanity checks: type must match and there must be data to put */
1604 if ((type & RPM_MASK_TYPE) != reqtype
1605 || size < 1 || data == NULL || h == NULL) {
1610 * Non-array types can't be appended to. Binary types use size
1611 * for data length, for other non-array types size must be 1.
1613 if ((type & RPM_MASK_RETURN_TYPE) != RPM_ARRAY_RETURN_TYPE) {
1614 flags = HEADERPUT_DEFAULT;
1615 if ((type & RPM_MASK_TYPE) != RPM_BIN_TYPE && size != 1) {
1623 td.type = type & RPM_MASK_TYPE;
1624 td.data = (void *) data;
1627 valid = headerPut(h, &td, flags);
1633 int headerPutString(Header h, rpmTag tag, const char *val)
1635 rpmTagType type = rpmTagGetType(tag) & RPM_MASK_TYPE;
1636 const void *sptr = NULL;
1638 /* string arrays expect char **, arrange that */
1639 if (type == RPM_STRING_ARRAY_TYPE || type == RPM_I18NSTRING_TYPE) {
1641 } else if (type == RPM_STRING_TYPE) {
1647 return headerPutType(h, tag, type, sptr, 1);
1650 int headerPutStringArray(Header h, rpmTag tag, const char **array, rpm_count_t size)
1652 return headerPutType(h, tag, RPM_STRING_ARRAY_TYPE, array, size);
1655 int headerPutChar(Header h, rpmTag tag, char *val, rpm_count_t size)
1657 return headerPutType(h, tag, RPM_CHAR_TYPE, val, size);
1660 int headerPutUint8(Header h, rpmTag tag, uint8_t *val, rpm_count_t size)
1662 return headerPutType(h, tag, RPM_INT8_TYPE, val, size);
1665 int headerPutUint16(Header h, rpmTag tag, uint16_t *val, rpm_count_t size)
1667 return headerPutType(h, tag, RPM_INT16_TYPE, val, size);
1670 int headerPutUint32(Header h, rpmTag tag, uint32_t *val, rpm_count_t size)
1672 return headerPutType(h, tag, RPM_INT32_TYPE, val, size);
1675 int headerPutUint64(Header h, rpmTag tag, uint64_t *val, rpm_count_t size)
1677 return headerPutType(h, tag, RPM_INT64_TYPE, val, size);
1680 int headerPutBin(Header h, rpmTag tag, uint8_t *val, rpm_count_t size)
1682 return headerPutType(h, tag, RPM_BIN_TYPE, val, size);
1685 int headerAddI18NString(Header h, rpmTag tag, const char * string,
1688 indexEntry table, entry;
1689 const char ** strArray;
1692 rpm_count_t i, langNum;
1695 table = findEntry(h, HEADER_I18NTABLE, RPM_STRING_ARRAY_TYPE);
1696 entry = findEntry(h, tag, RPM_I18NSTRING_TYPE);
1698 if (!table && entry)
1699 return 0; /* this shouldn't ever happen!! */
1701 if (!table && !entry) {
1702 const char * charArray[2];
1703 rpm_count_t count = 0;
1705 if (!lang || (lang[0] == 'C' && lang[1] == '\0')) {
1706 charArray[count++] = "C";
1708 charArray[count++] = "C";
1709 charArray[count++] = lang;
1713 td.tag = HEADER_I18NTABLE;
1714 td.type = RPM_STRING_ARRAY_TYPE;
1715 td.data = (void *) charArray;
1717 if (!headerPut(h, &td, HEADERPUT_DEFAULT))
1719 table = findEntry(h, HEADER_I18NTABLE, RPM_STRING_ARRAY_TYPE);
1724 if (!lang) lang = "C";
1726 { const char * l = table->data;
1727 for (langNum = 0; langNum < table->info.count; langNum++) {
1728 if (!strcmp(l, lang)) break;
1733 if (langNum >= table->info.count) {
1734 length = strlen(lang) + 1;
1735 if (ENTRY_IN_REGION(table)) {
1736 char * t = xmalloc(table->length + length);
1737 memcpy(t, table->data, table->length);
1739 table->info.offset = 0;
1741 table->data = xrealloc(table->data, table->length + length);
1742 memmove(((char *)table->data) + table->length, lang, length);
1743 table->length += length;
1744 table->info.count++;
1750 strArray = xmalloc(sizeof(*strArray) * (langNum + 1));
1751 for (i = 0; i < langNum; i++)
1753 strArray[langNum] = string;
1757 td.type = RPM_I18NSTRING_TYPE;
1759 td.count = langNum + 1;
1760 rc = headerPut(h, &td, HEADERPUT_DEFAULT);
1763 } else if (langNum >= entry->info.count) {
1764 ghosts = langNum - entry->info.count;
1766 length = strlen(string) + 1 + ghosts;
1767 if (ENTRY_IN_REGION(entry)) {
1768 char * t = xmalloc(entry->length + length);
1769 memcpy(t, entry->data, entry->length);
1771 entry->info.offset = 0;
1773 entry->data = xrealloc(entry->data, entry->length + length);
1775 memset(((char *)entry->data) + entry->length, '\0', ghosts);
1776 memmove(((char *)entry->data) + entry->length + ghosts, string, strlen(string)+1);
1778 entry->length += length;
1779 entry->info.count = langNum + 1;
1781 char *b, *be, *e, *ee, *t;
1784 /* Set beginning/end pointers to previous data */
1785 b = be = e = ee = entry->data;
1786 for (i = 0; i < table->info.count; i++) {
1789 ee += strlen(ee) + 1;
1794 /* Get storage for new buffer */
1796 sn = strlen(string) + 1;
1798 length = bn + sn + en;
1799 t = buf = xmalloc(length);
1801 /* Copy values into new storage */
1804 memcpy(t, string, sn);
1809 /* Replace i18N string array */
1810 entry->length -= strlen(be) + 1;
1811 entry->length += sn;
1813 if (ENTRY_IN_REGION(entry)) {
1814 entry->info.offset = 0;
1816 entry->data = _free(entry->data);
1823 int headerMod(Header h, rpmtd td)
1830 /* First find the tag */
1831 entry = findEntry(h, td->tag, td->type);
1836 data = grabData(td->type, td->data, td->count, &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 == td->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 = td->count;
1849 entry->info.type = td->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_DEFAULT);
1915 /* XXX 1 on success */
1916 return ((rc == 1) ? 1 : 0);
1920 * Duplicate a header.
1922 * @return new header instance
1924 Header headerCopy(Header h)
1926 Header nh = headerNew();
1930 hi = headerInitIterator(h);
1931 while (headerNext(hi, &td)) {
1932 if (rpmtdCount(&td) > 0) {
1933 (void) headerPut(nh, &td, HEADERPUT_DEFAULT);
1937 hi = headerFreeIterator(hi);
1939 return headerReload(nh, HEADER_IMAGE);
1942 void headerCopyTags(Header headerFrom, Header headerTo,
1943 const rpmTag * tagstocopy)
1948 if (headerFrom == headerTo)
1951 for (p = tagstocopy; *p != 0; p++) {
1952 if (headerIsEntry(headerTo, *p))
1954 if (!headerGet(headerFrom, *p, &td, HEADERGET_MINMEM))
1956 (void) headerPut(headerTo, &td, HEADERPUT_DEFAULT);
1961 unsigned int headerGetInstance(Header h)
1963 return h ? h->instance : 0;
1966 void headerSetInstance(Header h, unsigned int instance)
1968 h->instance = instance;