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 * If a header region tag is in the set to be swabbed, as the data for a
346 * a header region is located after all other tag data.
348 * @param entry header entry
349 * @param il no. of entries
350 * @param dl start no. bytes of data
351 * @param pe header physical entry pointer (swapped)
352 * @param dataStart header data start
353 * @param dataEnd header data end
354 * @param regionid region offset
355 * @return no. bytes of data in region, -1 on error
357 static int regionSwab(indexEntry entry, int il, int dl,
359 unsigned char * dataStart,
360 const unsigned char * dataEnd,
363 struct indexEntry_s ieprev;
365 memset(&ieprev, 0, sizeof(ieprev));
366 for (; il > 0; il--, pe++) {
367 struct indexEntry_s ie;
369 unsigned char * t = NULL;
371 ie.info.tag = ntohl(pe->tag);
372 ie.info.type = ntohl(pe->type);
373 ie.info.count = ntohl(pe->count);
374 ie.info.offset = ntohl(pe->offset);
376 if (hdrchkType(ie.info.type))
378 if (hdrchkData(ie.info.count))
380 if (hdrchkData(ie.info.offset))
382 if (hdrchkAlign(ie.info.type, ie.info.offset))
385 ie.data = t = dataStart + ie.info.offset;
386 if (dataEnd && t >= dataEnd)
389 ie.length = dataLength(ie.info.type, ie.data, ie.info.count, 1, dataEnd);
390 if (ie.length < 0 || hdrchkData(ie.length))
396 ie.info.offset = regionid;
397 *entry = ie; /* structure assignment */
403 if (typeSizes[type] > 1) {
405 diff = typeSizes[type] - (dl % typeSizes[type]);
406 if (diff != typeSizes[type]) {
408 if (ieprev.info.type == RPM_I18NSTRING_TYPE)
409 ieprev.length += diff;
413 /* Perform endian conversions */
414 switch (ntohl(pe->type)) {
416 { uint64_t * it = (uint64_t *)t;
417 for (; ie.info.count > 0; ie.info.count--, it += 1) {
418 if (dataEnd && ((unsigned char *)it) >= dataEnd)
422 t = (unsigned char *) it;
425 { int32_t * it = (int32_t *)t;
426 for (; ie.info.count > 0; ie.info.count--, it += 1) {
427 if (dataEnd && ((unsigned char *)it) >= dataEnd)
431 t = (unsigned char *) it;
434 { int16_t * it = (int16_t *) t;
435 for (; ie.info.count > 0; ie.info.count--, it += 1) {
436 if (dataEnd && ((unsigned char *)it) >= dataEnd)
440 t = (unsigned char *) it;
445 ieprev = ie; /* structure assignment */
454 * @retval *lengthPtr no. bytes in unloaded header blob
455 * @return unloaded header blob (NULL on error)
457 static void * doHeaderUnload(Header h,
471 int drlen, ndribbles;
475 /* Sort entries by (offset,tag). */
478 /* Compute (il,dl) for all tags, including those deleted in region. */
480 drlen = ndribbles = driplen = ndrips = 0;
481 for (i = 0, entry = h->index; i < h->indexUsed; i++, entry++) {
482 if (ENTRY_IS_REGION(entry)) {
483 int32_t rdl = -entry->info.offset; /* negative offset */
484 int32_t ril = rdl/sizeof(*pe);
485 int rid = entry->info.offset;
488 dl += entry->rdlen + entry->info.count;
489 /* XXX Legacy regions do not include the region tag and data. */
490 if (i == 0 && (h->flags & HEADERFLAG_LEGACY))
493 /* Skip rest of entries in region, but account for dribbles. */
494 for (; i < h->indexUsed && entry->info.offset <= rid+1; i++, entry++) {
495 if (entry->info.offset <= rid)
499 type = entry->info.type;
500 if (typeSizes[type] > 1) {
502 diff = typeSizes[type] - (dl % typeSizes[type]);
503 if (diff != typeSizes[type]) {
512 drlen += entry->length;
520 /* Ignore deleted drips. */
521 if (entry->data == NULL || entry->length <= 0)
525 type = entry->info.type;
526 if (typeSizes[type] > 1) {
528 diff = typeSizes[type] - (dl % typeSizes[type]);
529 if (diff != typeSizes[type]) {
539 driplen += entry->length;
543 /* Sanity checks on header intro. */
544 if (hdrchkTags(il) || hdrchkData(dl))
547 len = sizeof(il) + sizeof(dl) + (il * sizeof(*pe)) + dl;
553 pe = (entryInfo) &ei[2];
554 dataStart = te = (char *) (pe + il);
557 for (i = 0, entry = h->index; i < h->indexUsed; i++, entry++) {
563 if (entry->data == NULL || entry->length <= 0)
566 t = (unsigned char*)te;
567 pe->tag = htonl(entry->info.tag);
568 pe->type = htonl(entry->info.type);
569 pe->count = htonl(entry->info.count);
571 if (ENTRY_IS_REGION(entry)) {
572 int32_t rdl = -entry->info.offset; /* negative offset */
573 int32_t ril = rdl/sizeof(*pe) + ndribbles;
574 int rid = entry->info.offset;
576 src = (char *)entry->data;
577 rdlen = entry->rdlen;
579 /* XXX Legacy regions do not include the region tag and data. */
580 if (i == 0 && (h->flags & HEADERFLAG_LEGACY)) {
584 memcpy(pe+1, src, rdl);
585 memcpy(te, src + rdl, rdlen);
588 pe->offset = htonl(te - dataStart);
591 stei[2] = htonl(-rdl-entry->info.count);
593 memcpy(te, stei, entry->info.count);
594 te += entry->info.count;
596 rdlen += entry->info.count;
598 count = regionSwab(NULL, ril, 0, pe, t, NULL, 0);
604 memcpy(pe+1, src + sizeof(*pe), ((ril-1) * sizeof(*pe)));
605 memcpy(te, src + (ril * sizeof(*pe)), rdlen+entry->info.count+drlen);
608 entryInfo se = (entryInfo)src;
609 int off = ntohl(se->offset);
610 pe->offset = (off) ? htonl(te - dataStart) : htonl(off);
612 te += entry->info.count + drlen;
614 count = regionSwab(NULL, ril, 0, pe, t, NULL, 0);
615 if (count != (rdlen + entry->info.count + drlen))
619 /* Skip rest of entries in region. */
620 while (i < h->indexUsed && entry->info.offset <= rid+1) {
630 /* Ignore deleted drips. */
631 if (entry->data == NULL || entry->length <= 0)
635 type = entry->info.type;
636 if (typeSizes[type] > 1) {
638 diff = typeSizes[type] - ((te - dataStart) % typeSizes[type]);
639 if (diff != typeSizes[type]) {
646 pe->offset = htonl(te - dataStart);
648 /* copy data w/ endian conversions */
649 switch (entry->info.type) {
651 count = entry->info.count;
654 *((uint64_t *)te) = htonll(*((uint64_t *)src));
655 te += sizeof(uint64_t);
656 src += sizeof(uint64_t);
661 count = entry->info.count;
664 *((int32_t *)te) = htonl(*((int32_t *)src));
665 te += sizeof(int32_t);
666 src += sizeof(int32_t);
671 count = entry->info.count;
674 *((int16_t *)te) = htons(*((int16_t *)src));
675 te += sizeof(int16_t);
676 src += sizeof(int16_t);
681 memcpy(te, entry->data, entry->length);
688 /* Insure that there are no memcpy underruns/overruns. */
689 if (((char *)pe) != dataStart)
691 if ((((char *)ei)+len) != te)
697 h->flags &= ~HEADERFLAG_SORTED;
707 void * headerUnload(Header h)
710 void * uh = doHeaderUnload(h, &length);
715 * Find matching (tag,type) entry in header.
717 * @param tag entry tag
718 * @param type entry type
719 * @return header entry
722 indexEntry findEntry(Header h, rpmTag tag, rpmTagType type)
724 indexEntry entry, entry2, last;
725 struct indexEntry_s key;
727 if (h == NULL) return NULL;
728 if (!(h->flags & HEADERFLAG_SORTED)) headerSort(h);
733 bsearch(&key, h->index, h->indexUsed, sizeof(*h->index), indexCmp);
737 if (type == RPM_NULL_TYPE)
741 while (entry->info.tag == tag && entry->info.type != type &&
742 entry > h->index) entry--;
744 if (entry->info.tag == tag && entry->info.type == type)
747 last = h->index + h->indexUsed;
748 /* FIX: entry2 = entry. Code looks bogus as well. */
749 while (entry2->info.tag == tag && entry2->info.type != type &&
750 entry2 < last) entry2++;
752 if (entry->info.tag == tag && entry->info.type == type)
758 int headerDel(Header h, rpmTag tag)
760 indexEntry last = h->index + h->indexUsed;
761 indexEntry entry, first;
764 entry = findEntry(h, tag, RPM_NULL_TYPE);
765 if (!entry) return 1;
767 /* Make sure entry points to the first occurence of this tag. */
768 while (entry > h->index && (entry - 1)->info.tag == tag)
771 /* Free data for tags being removed. */
772 for (first = entry; first < last; first++) {
774 if (first->info.tag != tag)
779 if (ENTRY_IN_REGION(first))
784 ne = (first - entry);
789 memmove(entry, first, (ne * sizeof(*entry)));
795 Header headerLoad(void * uh)
797 int32_t * ei = (int32_t *) uh;
798 int32_t il = ntohl(ei[0]); /* index length */
799 int32_t dl = ntohl(ei[1]); /* data length */
800 size_t pvlen = sizeof(il) + sizeof(dl) +
801 (il * sizeof(struct entryInfo_s)) + dl;
805 unsigned char * dataStart;
806 unsigned char * dataEnd;
811 /* Sanity checks on header intro. */
812 if (hdrchkTags(il) || hdrchkData(dl))
816 pe = (entryInfo) &ei[2];
817 dataStart = (unsigned char *) (pe + il);
818 dataEnd = dataStart + dl;
820 h = xcalloc(1, sizeof(*h));
822 h->indexAlloced = il + 1;
825 h->index = xcalloc(h->indexAlloced, sizeof(*h->index));
826 h->flags |= HEADERFLAG_SORTED;
832 if (!(htonl(pe->tag) < HEADER_I18NTABLE)) {
833 h->flags |= HEADERFLAG_LEGACY;
834 entry->info.type = REGION_TAG_TYPE;
835 entry->info.tag = HEADER_IMAGE;
836 entry->info.count = REGION_TAG_COUNT;
837 entry->info.offset = ((unsigned char *)pe - dataStart); /* negative offset */
840 entry->length = pvlen - sizeof(il) - sizeof(dl);
841 rdlen = regionSwab(entry+1, il, 0, pe, dataStart, dataEnd, entry->info.offset);
844 entry->rdlen = rdlen;
851 h->flags &= ~HEADERFLAG_LEGACY;
853 entry->info.type = htonl(pe->type);
854 entry->info.count = htonl(pe->count);
856 if (hdrchkType(entry->info.type))
858 if (hdrchkTags(entry->info.count))
861 { int off = ntohl(pe->offset);
866 size_t nb = REGION_TAG_COUNT;
868 /* XXX Hmm, why the copy? */
869 memcpy(&stei, dataStart + off, nb);
870 rdl = -ntohl(stei[2]); /* negative offset */
871 ril = rdl/sizeof(*pe);
872 if (hdrchkTags(ril) || hdrchkData(rdl))
874 entry->info.tag = htonl(pe->tag);
877 rdl = (ril * sizeof(struct entryInfo_s));
878 entry->info.tag = HEADER_IMAGE;
881 entry->info.offset = -rdl; /* negative offset */
884 entry->length = pvlen - sizeof(il) - sizeof(dl);
885 rdlen = regionSwab(entry+1, ril-1, 0, pe+1, dataStart, dataEnd, entry->info.offset);
888 entry->rdlen = rdlen;
890 if (ril < h->indexUsed) {
891 indexEntry newEntry = entry + ril;
892 int ne = (h->indexUsed - ril);
893 int rid = entry->info.offset+1;
896 /* Load dribble entries from region. */
897 rc = regionSwab(newEntry, ne, 0, pe+ril, dataStart, dataEnd, rid);
902 { indexEntry firstEntry = newEntry;
903 int save = h->indexUsed;
906 /* Dribble entries replace duplicate region entries. */
908 for (j = 0; j < ne; j++, newEntry++) {
909 (void) headerDel(h, newEntry->info.tag);
910 if (newEntry->info.tag == RPMTAG_BASENAMES)
911 (void) headerDel(h, RPMTAG_OLDFILENAMES);
914 /* If any duplicate entries were replaced, move new entries down. */
915 if (h->indexUsed < (save - ne)) {
916 memmove(h->index + h->indexUsed, firstEntry,
917 (ne * sizeof(*entry)));
924 h->flags &= ~HEADERFLAG_SORTED;
926 h->flags |= HEADERFLAG_ALLOCATED;
932 h->index = _free(h->index);
938 Header headerReload(Header h, rpmTag tag)
942 void * uh = doHeaderUnload(h, &length);
952 if (ENTRY_IS_REGION(nh->index)) {
953 if (tag == HEADER_SIGNATURES || tag == HEADER_IMMUTABLE)
954 nh->index[0].info.tag = tag;
959 Header headerCopyLoad(const void * uh)
961 int32_t * ei = (int32_t *) uh;
962 int32_t il = ntohl(ei[0]); /* index length */
963 int32_t dl = ntohl(ei[1]); /* data length */
964 size_t pvlen = sizeof(il) + sizeof(dl) +
965 (il * sizeof(struct entryInfo_s)) + dl;
969 /* Sanity checks on header intro. */
970 if (!(hdrchkTags(il) || hdrchkData(dl)) && pvlen < headerMaxbytes) {
971 nuh = memcpy(xmalloc(pvlen), uh, pvlen);
972 if ((h = headerLoad(nuh)) == NULL)
979 * Read (and load) header from file handle.
980 * @param fd file handle
981 * @param magicp read (and verify) 8 bytes of (magic, 0)?
982 * @return header (or NULL on error)
984 Header headerRead(FD_t fd, enum hMagic magicp)
996 memset(block, 0, sizeof(block));
998 if (magicp == HEADER_MAGIC_YES)
1002 if (timedRead(fd, (char *)block, i*sizeof(*block)) != (i * sizeof(*block)))
1007 if (magicp == HEADER_MAGIC_YES) {
1009 if (memcmp(&magic, rpm_header_magic, sizeof(magic)))
1011 reserved = block[i++];
1014 il = ntohl(block[i]); i++;
1015 dl = ntohl(block[i]); i++;
1017 len = sizeof(il) + sizeof(dl) + (il * sizeof(struct entryInfo_s)) + dl;
1019 /* Sanity checks on header intro. */
1020 if (hdrchkTags(il) || hdrchkData(dl) || len > headerMaxbytes)
1026 len -= sizeof(il) + sizeof(dl);
1029 if (timedRead(fd, (char *)&ei[2], len) != len)
1035 if (h == NULL && ei != NULL) {
1041 int headerWrite(FD_t fd, Header h, enum hMagic magicp)
1049 uh = doHeaderUnload(h, &length);
1053 case HEADER_MAGIC_YES:
1054 nb = Fwrite(rpm_header_magic, sizeof(uint8_t), sizeof(rpm_header_magic), fd);
1055 if (nb != sizeof(rpm_header_magic))
1058 case HEADER_MAGIC_NO:
1062 nb = Fwrite(uh, sizeof(char), length, fd);
1066 return (nb == length ? 0 : 1);
1069 int headerIsEntry(Header h, rpmTag tag)
1071 /* FIX: h modified by sort. */
1072 return (findEntry(h, tag, RPM_NULL_TYPE) ? 1 : 0);
1077 * Retrieve data from header entry.
1078 * Relevant flags (others are ignored), if neither is set allocation
1079 * behavior depends on data type(!)
1080 * HEADERGET_MINMEM: return pointers to header memory
1081 * HEADERGET_ALLOC: always return malloced memory, overrides MINMEM
1083 * @todo Permit retrieval of regions other than HEADER_IMUTABLE.
1084 * @param entry header entry
1085 * @param td tag data container
1086 * @param minMem string pointers refer to header memory?
1087 * @param flags flags to control memory allocation
1088 * @return 1 on success, otherwise error.
1090 static int copyTdEntry(const indexEntry entry, rpmtd td, headerGetFlags flags)
1092 rpm_count_t count = entry->info.count;
1093 int rc = 1; /* XXX 1 on success. */
1094 /* ALLOC overrides MINMEM */
1095 int allocMem = flags & HEADERGET_ALLOC;
1096 int minMem = allocMem ? 0 : flags & HEADERGET_MINMEM;
1097 int argvArray = (flags & HEADERGET_ARGV) ? 1 : 0;
1100 td->flags = RPMTD_IMMUTABLE;
1101 switch (entry->info.type) {
1104 * XXX This only works for
1105 * XXX "sealed" HEADER_IMMUTABLE/HEADER_SIGNATURES/HEADER_IMAGE.
1106 * XXX This will *not* work for unsealed legacy HEADER_IMAGE (i.e.
1107 * XXX a legacy header freshly read, but not yet unloaded to the rpmdb).
1109 if (ENTRY_IS_REGION(entry)) {
1110 int32_t * ei = ((int32_t *)entry->data) - 2;
1111 entryInfo pe = (entryInfo) (ei + 2);
1112 unsigned char * dataStart = (unsigned char *) (pe + ntohl(ei[0]));
1113 int32_t rdl = -entry->info.offset; /* negative offset */
1114 int32_t ril = rdl/sizeof(*pe);
1117 count = 2 * sizeof(*ei) + (ril * sizeof(*pe)) + rdl;
1118 if (entry->info.tag == HEADER_IMAGE) {
1122 count += REGION_TAG_COUNT;
1123 rdl += REGION_TAG_COUNT;
1126 td->data = xmalloc(count);
1127 ei = (int32_t *) td->data;
1131 pe = (entryInfo) memcpy(ei + 2, pe, (ril * sizeof(*pe)));
1133 dataStart = (unsigned char *) memcpy(pe + ril, dataStart, rdl);
1135 rc = regionSwab(NULL, ril, 0, pe, dataStart, dataStart + rdl, 0);
1136 /* don't return data on failure */
1138 td->data = _free(td->data);
1140 /* XXX 1 on success. */
1141 rc = (rc < 0) ? 0 : 1;
1143 count = entry->length;
1145 ? memcpy(xmalloc(count), entry->data, count)
1149 case RPM_STRING_TYPE:
1151 td->data = allocMem ? xstrdup(entry->data) : entry->data;
1154 case RPM_STRING_ARRAY_TYPE:
1155 case RPM_I18NSTRING_TYPE:
1156 { const char ** ptrEntry;
1157 int tableSize = (count + argvArray) * sizeof(char *);
1162 td->data = xmalloc(tableSize);
1163 ptrEntry = (const char **) td->data;
1166 t = xmalloc(tableSize + entry->length);
1167 td->data = (void *)t;
1168 ptrEntry = (const char **) td->data;
1170 memcpy(t, entry->data, entry->length);
1172 for (i = 0; i < count; i++) {
1179 td->flags |= RPMTD_ARGV;
1184 case RPM_INT16_TYPE:
1185 case RPM_INT32_TYPE:
1186 case RPM_INT64_TYPE:
1188 td->data = xmalloc(entry->length);
1189 memcpy(td->data, entry->data, entry->length);
1191 td->data = entry->data;
1195 /* WTH? Don't mess with unknown data types... */
1200 td->type = entry->info.type;
1203 if (td->data && entry->data != td->data) {
1204 td->flags |= RPMTD_ALLOCED;
1211 * Does locale match entry in header i18n table?
1214 * The range [l,le) contains the next locale to match:
1215 * ll[_CC][.EEEEE][@dddd]
1217 * ll ISO language code (in lowercase).
1218 * CC (optional) ISO coutnry code (in uppercase).
1219 * EEEEE (optional) encoding (not really standardized).
1220 * dddd (optional) dialect.
1223 * @param td header i18n table data, NUL terminated
1224 * @param l start of locale to match
1225 * @param le end of locale to match
1226 * @return 1 on good match, 2 on weak match, 0 on no match
1228 static int headerMatchLocale(const char *td, const char *l, const char *le)
1232 /* First try a complete match. */
1233 if (strlen(td) == (le-l) && !strncmp(td, l, (le - l)))
1236 /* Next, try stripping optional dialect and matching. */
1237 for (fe = l; fe < le && *fe != '@'; fe++)
1239 if (fe < le && !strncmp(td, l, (fe - l)))
1242 /* Next, try stripping optional codeset and matching. */
1243 for (fe = l; fe < le && *fe != '.'; fe++)
1245 if (fe < le && !strncmp(td, l, (fe - l)))
1248 /* Finally, try stripping optional country code and matching. */
1249 for (fe = l; fe < le && *fe != '_'; fe++)
1251 if (fe < le && !strncmp(td, l, (fe - l)))
1258 * Return i18n string from header that matches locale.
1260 * @param entry i18n string data
1261 * @retval td tag data container
1262 * @param flags flags to control allocation
1265 static int copyI18NEntry(Header h, indexEntry entry, rpmtd td,
1266 headerGetFlags flags)
1268 const char *lang, *l, *le;
1271 td->type = RPM_STRING_TYPE;
1273 /* if no match, just return the first string */
1274 td->data = entry->data;
1276 /* XXX Drepper sez' this is the order. */
1277 if ((lang = getenv("LANGUAGE")) == NULL &&
1278 (lang = getenv("LC_ALL")) == NULL &&
1279 (lang = getenv("LC_MESSAGES")) == NULL &&
1280 (lang = getenv("LANG")) == NULL)
1283 if ((table = findEntry(h, HEADER_I18NTABLE, RPM_STRING_ARRAY_TYPE)) == NULL)
1286 for (l = lang; *l != '\0'; l = le) {
1288 char *ed, *ed_weak = NULL;
1291 while (*l && *l == ':') /* skip leading colons */
1295 for (le = l; *le && *le != ':'; le++) /* find end of this locale */
1298 /* For each entry in the header ... */
1299 for (langNum = 0, t = table->data, ed = entry->data;
1300 langNum < entry->info.count;
1301 langNum++, t += strlen(t) + 1, ed += strlen(ed) + 1) {
1303 int match = headerMatchLocale(t, l, le);
1307 } else if (match == 2) {
1318 if (flags & HEADERGET_ALLOC) {
1319 td->data = xstrdup(td->data);
1320 td->flags |= RPMTD_ALLOCED;
1327 * Retrieve tag data from header.
1329 * @param tag tag to retrieve
1330 * @retval td tag data container
1331 * @param flags flags to control retrieval
1332 * @return 1 on success, 0 on not found
1334 static int intGetTdEntry(Header h, rpmTag tag, rpmtd td, headerGetFlags flags)
1340 /* ensure clean state */
1343 /* First find the tag */
1344 /* FIX: h modified by sort. */
1345 entry = findEntry(h, tag, RPM_NULL_TYPE);
1346 if (entry == NULL) {
1347 /* Td is zeroed above, just return... */
1351 if (flags & HEADERGET_RAW) {
1352 rc = copyTdEntry(entry, td, flags);
1354 switch (entry->info.type) {
1355 case RPM_I18NSTRING_TYPE:
1356 rc = copyI18NEntry(h, entry, td, flags);
1359 rc = copyTdEntry(entry, td, flags);
1364 /* XXX 1 on success */
1365 return ((rc == 1) ? 1 : 0);
1369 * XXX temporary kludgery until tag extensions have been converted to
1370 * take rpmtd as argument
1372 static int intGetTagExt(Header h, rpmTag tag, rpmtd td, headerTagTagFunction tagfunc)
1376 rc = tagfunc(h, td);
1381 int headerGet(Header h, rpmTag tag, rpmtd td, headerGetFlags flags)
1384 headerTagTagFunction tagfunc = NULL;
1388 if (flags & HEADERGET_EXT) {
1389 tagfunc = rpmHeaderTagFunc(tag);
1393 rc = intGetTagExt(h, tag, td, tagfunc);
1395 rc = intGetTdEntry(h, tag, td, flags);
1398 assert(tag == td->tag);
1404 static void copyData(rpmTagType type, rpm_data_t dstPtr,
1405 rpm_constdata_t srcPtr, rpm_count_t cnt, int dataLength)
1408 case RPM_STRING_ARRAY_TYPE:
1409 case RPM_I18NSTRING_TYPE:
1410 { const char ** av = (const char **) srcPtr;
1413 while (cnt-- > 0 && dataLength > 0) {
1415 if ((s = *av++) == NULL)
1419 } while (s[-1] && --dataLength > 0);
1424 memmove(dstPtr, srcPtr, dataLength);
1430 * Return (malloc'ed) copy of entry data.
1431 * @param type entry data type
1432 * @param p entry data
1433 * @param c entry item count
1434 * @retval lengthPtr no. bytes in returned data
1435 * @return (malloc'ed) copy of entry data, NULL on error
1438 grabData(rpmTagType type, rpm_constdata_t p, rpm_count_t c, int * lengthPtr)
1440 rpm_data_t data = NULL;
1443 length = dataLength(type, p, c, 0, NULL);
1445 data = xmalloc(length);
1446 copyData(type, data, p, c, length);
1450 *lengthPtr = length;
1454 static int intAddEntry(Header h, rpmtd td)
1460 /* Count must always be >= 1 for headerAddEntry. */
1464 if (hdrchkType(td->type))
1466 if (hdrchkData(td->count))
1470 data = grabData(td->type, td->data, td->count, &length);
1471 if (data == NULL || length <= 0)
1474 /* Allocate more index space if necessary */
1475 if (h->indexUsed == h->indexAlloced) {
1476 h->indexAlloced += INDEX_MALLOC_SIZE;
1477 h->index = xrealloc(h->index, h->indexAlloced * sizeof(*h->index));
1480 /* Fill in the index */
1481 entry = h->index + h->indexUsed;
1482 entry->info.tag = td->tag;
1483 entry->info.type = td->type;
1484 entry->info.count = td->count;
1485 entry->info.offset = 0;
1487 entry->length = length;
1489 if (h->indexUsed > 0 && td->tag < h->index[h->indexUsed-1].info.tag)
1490 h->flags &= ~HEADERFLAG_SORTED;
1496 static int intAppendEntry(Header h, rpmtd td)
1501 if (td->type == RPM_STRING_TYPE || td->type == RPM_I18NSTRING_TYPE) {
1502 /* we can't do this */
1506 /* Find the tag entry in the header. */
1507 entry = findEntry(h, td->tag, td->type);
1511 length = dataLength(td->type, td->data, td->count, 0, NULL);
1515 if (ENTRY_IN_REGION(entry)) {
1516 char * t = xmalloc(entry->length + length);
1517 memcpy(t, entry->data, entry->length);
1519 entry->info.offset = 0;
1521 entry->data = xrealloc(entry->data, entry->length + length);
1523 copyData(td->type, ((char *) entry->data) + entry->length,
1524 td->data, td->count, length);
1526 entry->length += length;
1528 entry->info.count += td->count;
1533 int headerPut(Header h, rpmtd td, headerPutFlags flags)
1538 if (flags & HEADERPUT_APPEND) {
1539 rc = findEntry(h, td->tag, td->type) ?
1540 intAppendEntry(h, td) :
1543 rc = intAddEntry(h, td);
1549 * Sanity check data types against tag table before putting. Assume
1550 * append on all array-types.
1552 static int headerPutType(Header h, rpmTag tag, rpmTagType reqtype,
1553 rpm_constdata_t data, rpm_count_t size)
1556 rpmTagType type = rpmTagGetType(tag);
1557 headerPutFlags flags = HEADERPUT_APPEND;
1560 /* Basic sanity checks: type must match and there must be data to put */
1561 if ((type & RPM_MASK_TYPE) != reqtype
1562 || size < 1 || data == NULL || h == NULL) {
1567 * Non-array types can't be appended to. Binary types use size
1568 * for data length, for other non-array types size must be 1.
1570 if ((type & RPM_MASK_RETURN_TYPE) != RPM_ARRAY_RETURN_TYPE) {
1571 flags = HEADERPUT_DEFAULT;
1572 if ((type & RPM_MASK_TYPE) != RPM_BIN_TYPE && size != 1) {
1580 td.type = type & RPM_MASK_TYPE;
1581 td.data = (void *) data;
1584 valid = headerPut(h, &td, flags);
1590 int headerPutString(Header h, rpmTag tag, const char *val)
1592 rpmTagType type = rpmTagGetType(tag) & RPM_MASK_TYPE;
1593 const void *sptr = NULL;
1595 /* string arrays expect char **, arrange that */
1596 if (type == RPM_STRING_ARRAY_TYPE || type == RPM_I18NSTRING_TYPE) {
1598 } else if (type == RPM_STRING_TYPE) {
1604 return headerPutType(h, tag, type, sptr, 1);
1607 int headerPutStringArray(Header h, rpmTag tag, const char **array, rpm_count_t size)
1609 return headerPutType(h, tag, RPM_STRING_ARRAY_TYPE, array, size);
1612 int headerPutChar(Header h, rpmTag tag, char *val, rpm_count_t size)
1614 return headerPutType(h, tag, RPM_CHAR_TYPE, val, size);
1617 int headerPutUint8(Header h, rpmTag tag, uint8_t *val, rpm_count_t size)
1619 return headerPutType(h, tag, RPM_INT8_TYPE, val, size);
1622 int headerPutUint16(Header h, rpmTag tag, uint16_t *val, rpm_count_t size)
1624 return headerPutType(h, tag, RPM_INT16_TYPE, val, size);
1627 int headerPutUint32(Header h, rpmTag tag, uint32_t *val, rpm_count_t size)
1629 return headerPutType(h, tag, RPM_INT32_TYPE, val, size);
1632 int headerPutUint64(Header h, rpmTag tag, uint64_t *val, rpm_count_t size)
1634 return headerPutType(h, tag, RPM_INT64_TYPE, val, size);
1637 int headerPutBin(Header h, rpmTag tag, uint8_t *val, rpm_count_t size)
1639 return headerPutType(h, tag, RPM_BIN_TYPE, val, size);
1642 int headerAddI18NString(Header h, rpmTag tag, const char * string,
1645 indexEntry table, entry;
1646 const char ** strArray;
1649 rpm_count_t i, langNum;
1652 table = findEntry(h, HEADER_I18NTABLE, RPM_STRING_ARRAY_TYPE);
1653 entry = findEntry(h, tag, RPM_I18NSTRING_TYPE);
1655 if (!table && entry)
1656 return 0; /* this shouldn't ever happen!! */
1658 if (!table && !entry) {
1659 const char * charArray[2];
1660 rpm_count_t count = 0;
1662 if (!lang || (lang[0] == 'C' && lang[1] == '\0')) {
1663 charArray[count++] = "C";
1665 charArray[count++] = "C";
1666 charArray[count++] = lang;
1670 td.tag = HEADER_I18NTABLE;
1671 td.type = RPM_STRING_ARRAY_TYPE;
1672 td.data = (void *) charArray;
1674 if (!headerPut(h, &td, HEADERPUT_DEFAULT))
1676 table = findEntry(h, HEADER_I18NTABLE, RPM_STRING_ARRAY_TYPE);
1681 if (!lang) lang = "C";
1683 { const char * l = table->data;
1684 for (langNum = 0; langNum < table->info.count; langNum++) {
1685 if (!strcmp(l, lang)) break;
1690 if (langNum >= table->info.count) {
1691 length = strlen(lang) + 1;
1692 if (ENTRY_IN_REGION(table)) {
1693 char * t = xmalloc(table->length + length);
1694 memcpy(t, table->data, table->length);
1696 table->info.offset = 0;
1698 table->data = xrealloc(table->data, table->length + length);
1699 memmove(((char *)table->data) + table->length, lang, length);
1700 table->length += length;
1701 table->info.count++;
1707 strArray = xmalloc(sizeof(*strArray) * (langNum + 1));
1708 for (i = 0; i < langNum; i++)
1710 strArray[langNum] = string;
1714 td.type = RPM_I18NSTRING_TYPE;
1716 td.count = langNum + 1;
1717 rc = headerPut(h, &td, HEADERPUT_DEFAULT);
1720 } else if (langNum >= entry->info.count) {
1721 ghosts = langNum - entry->info.count;
1723 length = strlen(string) + 1 + ghosts;
1724 if (ENTRY_IN_REGION(entry)) {
1725 char * t = xmalloc(entry->length + length);
1726 memcpy(t, entry->data, entry->length);
1728 entry->info.offset = 0;
1730 entry->data = xrealloc(entry->data, entry->length + length);
1732 memset(((char *)entry->data) + entry->length, '\0', ghosts);
1733 memmove(((char *)entry->data) + entry->length + ghosts, string, strlen(string)+1);
1735 entry->length += length;
1736 entry->info.count = langNum + 1;
1738 char *b, *be, *e, *ee, *t;
1741 /* Set beginning/end pointers to previous data */
1742 b = be = e = ee = entry->data;
1743 for (i = 0; i < table->info.count; i++) {
1746 ee += strlen(ee) + 1;
1751 /* Get storage for new buffer */
1753 sn = strlen(string) + 1;
1755 length = bn + sn + en;
1756 t = buf = xmalloc(length);
1758 /* Copy values into new storage */
1761 memcpy(t, string, sn);
1766 /* Replace i18N string array */
1767 entry->length -= strlen(be) + 1;
1768 entry->length += sn;
1770 if (ENTRY_IN_REGION(entry)) {
1771 entry->info.offset = 0;
1773 entry->data = _free(entry->data);
1780 int headerMod(Header h, rpmtd td)
1787 /* First find the tag */
1788 entry = findEntry(h, td->tag, td->type);
1793 data = grabData(td->type, td->data, td->count, &length);
1794 if (data == NULL || length <= 0)
1797 /* make sure entry points to the first occurence of this tag */
1798 while (entry > h->index && (entry - 1)->info.tag == td->tag)
1801 /* free after we've grabbed the new data in case the two are intertwined;
1802 that's a bad idea but at least we won't break */
1803 oldData = entry->data;
1805 entry->info.count = td->count;
1806 entry->info.type = td->type;
1808 entry->length = length;
1810 if (ENTRY_IN_REGION(entry)) {
1811 entry->info.offset = 0;
1813 oldData = _free(oldData);
1819 * Header tag iterator data structure.
1821 struct headerIterator_s {
1822 Header h; /*!< Header being iterated. */
1823 int next_index; /*!< Next tag index. */
1826 HeaderIterator headerFreeIterator(HeaderIterator hi)
1829 hi->h = headerFree(hi->h);
1835 HeaderIterator headerInitIterator(Header h)
1837 HeaderIterator hi = xmalloc(sizeof(*hi));
1841 hi->h = headerLink(h);
1846 int headerNext(HeaderIterator hi, rpmtd td)
1849 int slot = hi->next_index;
1850 indexEntry entry = NULL;
1856 for (slot = hi->next_index; slot < h->indexUsed; slot++) {
1857 entry = h->index + slot;
1858 if (!ENTRY_IS_REGION(entry))
1861 hi->next_index = slot;
1862 if (entry == NULL || slot >= h->indexUsed)
1868 td->tag = entry->info.tag;
1870 rc = copyTdEntry(entry, td, HEADERGET_DEFAULT);
1872 /* XXX 1 on success */
1873 return ((rc == 1) ? 1 : 0);
1877 * Duplicate a header.
1879 * @return new header instance
1881 Header headerCopy(Header h)
1883 Header nh = headerNew();
1887 hi = headerInitIterator(h);
1888 while (headerNext(hi, &td)) {
1889 if (rpmtdCount(&td) > 0) {
1890 (void) headerPut(nh, &td, HEADERPUT_DEFAULT);
1894 hi = headerFreeIterator(hi);
1896 return headerReload(nh, HEADER_IMAGE);
1899 void headerCopyTags(Header headerFrom, Header headerTo,
1900 const rpmTag * tagstocopy)
1905 if (headerFrom == headerTo)
1908 for (p = tagstocopy; *p != 0; p++) {
1909 if (headerIsEntry(headerTo, *p))
1911 if (!headerGet(headerFrom, *p, &td, HEADERGET_MINMEM))
1913 (void) headerPut(headerTo, &td, HEADERPUT_DEFAULT);
1918 unsigned int headerGetInstance(Header h)
1920 return h ? h->instance : 0;
1923 void headerSetInstance(Header h, unsigned int instance)
1925 h->instance = instance;