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"
16 #include "lib/misc.h" /* tag function proto */
22 const unsigned char rpm_header_magic[8] = {
23 0x8e, 0xad, 0xe8, 0x01, 0x00, 0x00, 0x00, 0x00
27 * Alignment needed for header data types.
29 static const int typeAlign[16] = {
30 1, /*!< RPM_NULL_TYPE */
31 1, /*!< RPM_CHAR_TYPE */
32 1, /*!< RPM_INT8_TYPE */
33 2, /*!< RPM_INT16_TYPE */
34 4, /*!< RPM_INT32_TYPE */
35 8, /*!< RPM_INT64_TYPE */
36 1, /*!< RPM_STRING_TYPE */
37 1, /*!< RPM_BIN_TYPE */
38 1, /*!< RPM_STRING_ARRAY_TYPE */
39 1, /*!< RPM_I18NSTRING_TYPE */
49 * Size of header data types.
51 static const int typeSizes[16] = {
52 0, /*!< RPM_NULL_TYPE */
53 1, /*!< RPM_CHAR_TYPE */
54 1, /*!< RPM_INT8_TYPE */
55 2, /*!< RPM_INT16_TYPE */
56 4, /*!< RPM_INT32_TYPE */
57 8, /*!< RPM_INT64_TYPE */
58 -1, /*!< RPM_STRING_TYPE */
59 1, /*!< RPM_BIN_TYPE */
60 -1, /*!< RPM_STRING_ARRAY_TYPE */
61 -1, /*!< RPM_I18NSTRING_TYPE */
71 HEADERFLAG_SORTED = (1 << 0), /*!< Are header entries sorted? */
72 HEADERFLAG_ALLOCATED = (1 << 1), /*!< Is 1st header region allocated? */
73 HEADERFLAG_LEGACY = (1 << 2), /*!< Header came from legacy source? */
74 HEADERFLAG_DEBUG = (1 << 3), /*!< Debug this header? */
77 typedef rpmFlags headerFlags;
80 * The Header data structure.
82 struct headerToken_s {
83 void * blob; /*!< Header region blob. */
84 indexEntry index; /*!< Array of tags. */
85 int indexUsed; /*!< Current size of tag array. */
86 int indexAlloced; /*!< Allocated size of tag array. */
87 unsigned int instance; /*!< Rpmdb instance (offset) */
89 int nrefs; /*!< Reference count. */
93 * Maximum no. of bytes permitted in a header.
95 static const size_t headerMaxbytes = (32*1024*1024);
97 #define INDEX_MALLOC_SIZE 8
99 #define ENTRY_IS_REGION(_e) \
100 (((_e)->info.tag >= RPMTAG_HEADERIMAGE) && ((_e)->info.tag < RPMTAG_HEADERREGIONS))
101 #define ENTRY_IN_REGION(_e) ((_e)->info.offset < 0)
103 /* Convert a 64bit value to network byte order. */
105 static uint64_t htonll(uint64_t n)
107 uint32_t *i = (uint32_t*)&n;
114 Header headerLink(Header h)
121 static Header headerUnlink(Header h)
128 Header headerFree(Header h)
130 (void) headerUnlink(h);
132 if (h == NULL || h->nrefs > 0)
136 indexEntry entry = h->index;
138 for (i = 0; i < h->indexUsed; i++, entry++) {
139 if ((h->flags & HEADERFLAG_ALLOCATED) && ENTRY_IS_REGION(entry)) {
140 if (entry->length > 0) {
141 int32_t * ei = entry->data;
142 if ((ei - 2) == h->blob) h->blob = _free(h->blob);
145 } else if (!ENTRY_IN_REGION(entry)) {
146 entry->data = _free(entry->data);
150 h->index = _free(h->index);
157 static Header headerCreate(void *blob, unsigned int pvlen, int32_t indexLen)
159 Header h = xcalloc(1, sizeof(*h));
161 h->blob = (pvlen > 0) ? memcpy(xmalloc(pvlen), blob, pvlen) : blob;
162 h->indexAlloced = indexLen + 1;
163 h->indexUsed = indexLen;
165 h->indexAlloced = INDEX_MALLOC_SIZE;
169 h->flags |= HEADERFLAG_SORTED;
171 h->index = (h->indexAlloced
172 ? xcalloc(h->indexAlloced, sizeof(*h->index))
176 return headerLink(h);
179 Header headerNew(void)
181 return headerCreate(NULL, 0, 0);
184 int headerVerifyInfo(int il, int dl, const void * pev, void * iv, int negate)
186 entryInfo pe = (entryInfo) pev;
190 for (i = 0; i < il; i++) {
191 info->tag = ntohl(pe[i].tag);
192 info->type = ntohl(pe[i].type);
193 info->offset = ntohl(pe[i].offset);
195 info->offset = -info->offset;
196 info->count = ntohl(pe[i].count);
198 if (hdrchkType(info->type))
200 if (hdrchkAlign(info->type, info->offset))
202 if (!negate && hdrchkRange(dl, info->offset))
204 if (hdrchkData(info->count))
211 static int indexCmp(const void * avp, const void * bvp)
213 indexEntry ap = (indexEntry) avp, bp = (indexEntry) bvp;
214 return (ap->info.tag - bp->info.tag);
217 void headerSort(Header h)
219 if (!(h->flags & HEADERFLAG_SORTED)) {
220 qsort(h->index, h->indexUsed, sizeof(*h->index), indexCmp);
221 h->flags |= HEADERFLAG_SORTED;
225 static int offsetCmp(const void * avp, const void * bvp)
227 indexEntry ap = (indexEntry) avp, bp = (indexEntry) bvp;
228 int rc = (ap->info.offset - bp->info.offset);
231 /* Within a region, entries sort by address. Added drips sort by tag. */
232 if (ap->info.offset < 0)
233 rc = (((char *)ap->data) - ((char *)bp->data));
235 rc = (ap->info.tag - bp->info.tag);
240 void headerUnsort(Header h)
242 if (h->flags & HEADERFLAG_SORTED) {
243 qsort(h->index, h->indexUsed, sizeof(*h->index), offsetCmp);
244 h->flags &= ~HEADERFLAG_SORTED;
248 static inline unsigned int alignDiff(rpm_tagtype_t type, unsigned int alignsize)
250 int typesize = typeSizes[type];
253 unsigned int diff = typesize - (alignsize % typesize);
254 if (diff != typesize)
260 unsigned headerSizeof(Header h, int magicp)
263 unsigned int size = 0;
272 case HEADER_MAGIC_YES:
273 size += sizeof(rpm_header_magic);
275 case HEADER_MAGIC_NO:
279 size += 2 * sizeof(int32_t); /* count of index entries */
281 for (i = 0, entry = h->index; i < h->indexUsed; i++, entry++) {
282 /* Regions go in as is ... */
283 if (ENTRY_IS_REGION(entry)) {
284 size += entry->length;
285 /* XXX Legacy regions do not include the region tag and data. */
286 if (i == 0 && (h->flags & HEADERFLAG_LEGACY))
287 size += sizeof(struct entryInfo_s) + entry->info.count;
291 /* ... and region elements are skipped. */
292 if (entry->info.offset < 0)
296 size += alignDiff(entry->info.type, size);
298 size += sizeof(struct entryInfo_s) + entry->length;
304 /* Bounded header string (array) size calculation, return -1 on error */
305 static inline int strtaglen(const char *str, rpm_count_t c, const char *end)
307 const char *start = str;
310 while ((s = memchr(start, '\0', end-start))) {
311 if (--c == 0 || s > end)
315 return (c > 0) ? -1 : (s - str + 1);
319 * Return length of entry data.
320 * @param type entry data type
321 * @param p entry data
322 * @param count entry item count
323 * @param onDisk data is concatenated strings (with NUL's))?
324 * @param pend pointer to end of data (or NULL)
325 * @return no. bytes in data, -1 on failure
327 static int dataLength(rpm_tagtype_t type, rpm_constdata_t p, rpm_count_t count,
328 int onDisk, rpm_constdata_t pend)
331 /* Not all callers supply data end, avoid falling over edge of the world */
332 const char * se = pend ? pend : s + HEADER_DATA_MAX;
336 case RPM_STRING_TYPE:
339 length = strtaglen(s, 1, se);
342 case RPM_STRING_ARRAY_TYPE:
343 case RPM_I18NSTRING_TYPE:
344 /* These are like RPM_STRING_TYPE, except they're *always* an array */
345 /* Compute sum of length of all strings, including nul terminators */
348 length = strtaglen(s, count, se);
350 const char ** av = (const char **)p;
352 /* add one for null termination */
353 length += strlen(*av++) + 1;
359 if (typeSizes[type] == -1)
361 length = typeSizes[(type & 0xf)] * count;
362 if (length < 0 || (se && (s + length) > se))
371 * Swap int32_t and int16_t arrays within header region.
373 * If a header region tag is in the set to be swabbed, as the data for a
374 * a header region is located after all other tag data.
376 * @param entry header entry
377 * @param il no. of entries
378 * @param dl start no. bytes of data
379 * @param pe header physical entry pointer (swapped)
380 * @param dataStart header data start
381 * @param dataEnd header data end
382 * @param regionid region offset
383 * @param fast use offsets for data sizes if possible
384 * @return no. bytes of data in region, -1 on error
386 static int regionSwab(indexEntry entry, int il, int dl,
388 unsigned char * dataStart,
389 const unsigned char * dataEnd,
390 int regionid, int fast)
392 if ((entry != NULL && regionid >= 0) || (entry == NULL && regionid != 0))
395 for (; il > 0; il--, pe++) {
396 struct indexEntry_s ie;
398 ie.info.tag = ntohl(pe->tag);
399 ie.info.type = ntohl(pe->type);
400 ie.info.count = ntohl(pe->count);
401 ie.info.offset = ntohl(pe->offset);
403 if (hdrchkType(ie.info.type))
405 if (hdrchkData(ie.info.count))
407 if (hdrchkData(ie.info.offset))
409 if (hdrchkAlign(ie.info.type, ie.info.offset))
412 ie.data = dataStart + ie.info.offset;
413 if (dataEnd && (unsigned char *)ie.data >= dataEnd)
416 if (fast && il > 1) {
417 ie.length = ntohl(pe[1].offset) - ie.info.offset;
419 ie.length = dataLength(ie.info.type, ie.data, ie.info.count,
422 if (ie.length < 0 || hdrchkData(ie.length))
428 ie.info.offset = regionid;
429 *entry = ie; /* structure assignment */
434 dl += alignDiff(ie.info.type, dl);
436 /* Perform endian conversions */
437 switch (ntohl(pe->type)) {
439 { uint64_t * it = ie.data;
440 for (; ie.info.count > 0; ie.info.count--, it += 1) {
441 if (dataEnd && ((unsigned char *)it) >= dataEnd)
447 { int32_t * it = ie.data;
448 for (; ie.info.count > 0; ie.info.count--, it += 1) {
449 if (dataEnd && ((unsigned char *)it) >= dataEnd)
455 { int16_t * it = ie.data;
456 for (; ie.info.count > 0; ie.info.count--, it += 1) {
457 if (dataEnd && ((unsigned char *)it) >= dataEnd)
470 void * headerExport(Header h, unsigned int *bsize)
481 int drlen, ndribbles;
483 if (h == NULL) return NULL;
485 /* Sort entries by (offset,tag). */
488 /* Compute (il,dl) for all tags, including those deleted in region. */
489 drlen = ndribbles = 0;
490 for (i = 0, entry = h->index; i < h->indexUsed; i++, entry++) {
491 if (ENTRY_IS_REGION(entry)) {
492 int32_t rdl = -entry->info.offset; /* negative offset */
493 int32_t ril = rdl/sizeof(*pe);
494 int rid = entry->info.offset;
497 dl += entry->rdlen + entry->info.count;
498 /* XXX Legacy regions do not include the region tag and data. */
499 if (i == 0 && (h->flags & HEADERFLAG_LEGACY))
502 /* Skip rest of entries in region, but account for dribbles. */
503 for (; i < h->indexUsed && entry->info.offset <= rid+1; i++, entry++) {
504 if (entry->info.offset <= rid)
508 diff = alignDiff(entry->info.type, dl);
516 drlen += entry->length;
524 /* Ignore deleted drips. */
525 if (entry->data == NULL || entry->length <= 0)
529 dl += alignDiff(entry->info.type, dl);
535 /* Sanity checks on header intro. */
536 if (hdrchkTags(il) || hdrchkData(dl))
539 len = sizeof(il) + sizeof(dl) + (il * sizeof(*pe)) + dl;
545 pe = (entryInfo) &ei[2];
546 dataStart = te = (char *) (pe + il);
548 for (i = 0, entry = h->index; i < h->indexUsed; i++, entry++) {
555 if (entry->data == NULL || entry->length <= 0)
558 t = (unsigned char*)te;
559 pe->tag = htonl(entry->info.tag);
560 pe->type = htonl(entry->info.type);
561 pe->count = htonl(entry->info.count);
563 if (ENTRY_IS_REGION(entry)) {
564 int32_t rdl = -entry->info.offset; /* negative offset */
565 int32_t ril = rdl/sizeof(*pe) + ndribbles;
566 int rid = entry->info.offset;
568 src = (char *)entry->data;
569 rdlen = entry->rdlen;
571 /* XXX Legacy regions do not include the region tag and data. */
572 if (i == 0 && (h->flags & HEADERFLAG_LEGACY)) {
575 memcpy(pe+1, src, rdl);
576 memcpy(te, src + rdl, rdlen);
579 pe->offset = htonl(te - dataStart);
582 stei[2] = htonl(-rdl-entry->info.count);
584 memcpy(te, stei, entry->info.count);
585 te += entry->info.count;
587 rdlen += entry->info.count;
589 count = regionSwab(NULL, ril, 0, pe, t, NULL, 0, 0);
595 memcpy(pe+1, src + sizeof(*pe), ((ril-1) * sizeof(*pe)));
596 memcpy(te, src + (ril * sizeof(*pe)), rdlen+entry->info.count+drlen);
599 entryInfo se = (entryInfo)src;
600 int off = ntohl(se->offset);
601 pe->offset = (off) ? htonl(te - dataStart) : htonl(off);
603 te += entry->info.count + drlen;
605 count = regionSwab(NULL, ril, 0, pe, t, NULL, 0, 0);
606 if (count != (rdlen + entry->info.count + drlen))
610 /* Skip rest of entries in region. */
611 while (i < h->indexUsed && entry->info.offset <= rid+1) {
621 /* Ignore deleted drips. */
622 if (entry->data == NULL || entry->length <= 0)
626 diff = alignDiff(entry->info.type, (te - dataStart));
632 pe->offset = htonl(te - dataStart);
634 /* copy data w/ endian conversions */
635 switch (entry->info.type) {
637 count = entry->info.count;
640 *((uint64_t *)te) = htonll(*((uint64_t *)src));
641 te += sizeof(uint64_t);
642 src += sizeof(uint64_t);
647 count = entry->info.count;
650 *((int32_t *)te) = htonl(*((int32_t *)src));
651 te += sizeof(int32_t);
652 src += sizeof(int32_t);
657 count = entry->info.count;
660 *((int16_t *)te) = htons(*((int16_t *)src));
661 te += sizeof(int16_t);
662 src += sizeof(int16_t);
667 memcpy(te, entry->data, entry->length);
674 /* Insure that there are no memcpy underruns/overruns. */
675 if (((char *)pe) != dataStart)
677 if ((((char *)ei)+len) != te)
692 void * headerUnload(Header h)
694 return headerExport(h, NULL);
698 * Find matching (tag,type) entry in header.
700 * @param tag entry tag
701 * @param type entry type
702 * @return header entry
705 indexEntry findEntry(Header h, rpmTagVal tag, rpm_tagtype_t type)
708 struct indexEntry_s key;
710 if (h == NULL) return NULL;
711 if (!(h->flags & HEADERFLAG_SORTED)) headerSort(h);
715 entry = bsearch(&key, h->index, h->indexUsed, sizeof(*h->index), indexCmp);
719 if (type == RPM_NULL_TYPE)
723 while (entry->info.tag == tag && entry->info.type != type &&
724 entry > h->index) entry--;
726 if (entry->info.tag == tag && entry->info.type == type)
732 int headerDel(Header h, rpmTagVal tag)
734 indexEntry last = h->index + h->indexUsed;
735 indexEntry entry, first;
738 entry = findEntry(h, tag, RPM_NULL_TYPE);
739 if (!entry) return 1;
741 /* Make sure entry points to the first occurence of this tag. */
742 while (entry > h->index && (entry - 1)->info.tag == tag)
745 /* Free data for tags being removed. */
746 for (first = entry; first < last; first++) {
748 if (first->info.tag != tag)
753 if (ENTRY_IN_REGION(first))
758 ne = (first - entry);
763 memmove(entry, first, (ne * sizeof(*entry)));
769 Header headerImport(void * blob, unsigned int bsize, headerImportFlags flags)
771 const int32_t * ei = (int32_t *) blob;
772 int32_t il = ntohl(ei[0]); /* index length */
773 int32_t dl = ntohl(ei[1]); /* data length */
774 unsigned int pvlen = sizeof(il) + sizeof(dl) +
775 (il * sizeof(struct entryInfo_s)) + dl;;
778 unsigned char * dataStart;
779 unsigned char * dataEnd;
782 int fast = (flags & HEADERIMPORT_FAST);
784 /* Sanity checks on header intro. */
785 if (bsize && bsize != pvlen)
787 if (hdrchkTags(il) || hdrchkData(dl) || pvlen >= headerMaxbytes)
790 h = headerCreate(blob, (flags & HEADERIMPORT_COPY) ? pvlen : 0, il);
792 ei = h->blob; /* In case we had to copy */
793 pe = (entryInfo) &ei[2];
794 dataStart = (unsigned char *) (pe + il);
795 dataEnd = dataStart + dl;
798 if (!(htonl(pe->tag) < RPMTAG_HEADERI18NTABLE)) {
799 h->flags |= HEADERFLAG_LEGACY;
800 entry->info.type = REGION_TAG_TYPE;
801 entry->info.tag = RPMTAG_HEADERIMAGE;
802 entry->info.count = REGION_TAG_COUNT;
803 entry->info.offset = ((unsigned char *)pe - dataStart); /* negative offset */
806 entry->length = pvlen - sizeof(il) - sizeof(dl);
807 rdlen = regionSwab(entry+1, il, 0, pe,
808 dataStart, dataEnd, entry->info.offset, fast);
811 entry->rdlen = rdlen;
817 h->flags &= ~HEADERFLAG_LEGACY;
819 entry->info.type = htonl(pe->type);
820 entry->info.count = htonl(pe->count);
822 if (hdrchkType(entry->info.type))
824 if (hdrchkTags(entry->info.count))
827 { int off = ntohl(pe->offset);
830 size_t nb = REGION_TAG_COUNT;
832 if (hdrchkRange(dl, (off + nb)))
834 /* XXX Hmm, why the copy? */
835 memcpy(&stei, dataStart + off, nb);
836 rdl = -ntohl(stei[2]); /* negative offset */
837 ril = rdl/sizeof(*pe);
838 if (hdrchkTags(ril) || hdrchkData(rdl))
840 entry->info.tag = htonl(pe->tag);
843 rdl = (ril * sizeof(struct entryInfo_s));
844 entry->info.tag = RPMTAG_HEADERIMAGE;
847 entry->info.offset = -rdl; /* negative offset */
850 entry->length = pvlen - sizeof(il) - sizeof(dl);
851 rdlen = regionSwab(entry+1, ril-1, 0, pe+1,
852 dataStart, dataEnd, entry->info.offset, fast);
855 entry->rdlen = rdlen;
857 if (ril < h->indexUsed) {
858 indexEntry newEntry = entry + ril;
859 int ne = (h->indexUsed - ril);
860 int rid = entry->info.offset+1;
862 /* Load dribble entries from region. */
863 rdlen = regionSwab(newEntry, ne, rdlen, pe+ril,
864 dataStart, dataEnd, rid, fast);
868 { indexEntry firstEntry = newEntry;
869 int save = h->indexUsed;
872 /* Dribble entries replace duplicate region entries. */
874 for (j = 0; j < ne; j++, newEntry++) {
875 (void) headerDel(h, newEntry->info.tag);
876 if (newEntry->info.tag == RPMTAG_BASENAMES)
877 (void) headerDel(h, RPMTAG_OLDFILENAMES);
880 /* If any duplicate entries were replaced, move new entries down. */
881 if (h->indexUsed < (save - ne)) {
882 memmove(h->index + h->indexUsed, firstEntry,
883 (ne * sizeof(*entry)));
889 rdlen += REGION_TAG_COUNT;
895 h->flags &= ~HEADERFLAG_SORTED;
897 h->flags |= HEADERFLAG_ALLOCATED;
903 if (flags & HEADERIMPORT_COPY)
911 Header headerReload(Header h, rpmTagVal tag)
915 void * uh = headerExport(h, &uc);
920 nh = headerImport(uh, uc, 0);
925 if (ENTRY_IS_REGION(nh->index)) {
926 if (tag == RPMTAG_HEADERSIGNATURES || tag == RPMTAG_HEADERIMMUTABLE)
927 nh->index[0].info.tag = tag;
932 Header headerLoad(void * uh)
934 return headerImport(uh, 0, 0);
937 Header headerCopyLoad(const void * uh)
939 /* Discards const but that's ok as we'll take a copy */
940 return headerImport((void *)uh, 0, HEADERIMPORT_COPY);
943 Header headerRead(FD_t fd, int magicp)
950 unsigned int len, blen;
952 if (magicp == HEADER_MAGIC_YES) {
955 if (Fread(block, 1, 4*sizeof(*block), fd) != 4*sizeof(*block))
960 if (memcmp(&magic, rpm_header_magic, sizeof(magic)))
963 il = ntohl(block[2]);
964 dl = ntohl(block[3]);
966 if (Fread(block, 1, 2*sizeof(*block), fd) != 2*sizeof(*block))
969 il = ntohl(block[0]);
970 dl = ntohl(block[1]);
973 blen = (il * sizeof(struct entryInfo_s)) + dl;
974 len = sizeof(il) + sizeof(dl) + blen;
976 /* Sanity checks on header intro. */
977 if (hdrchkTags(il) || hdrchkData(dl) || len > headerMaxbytes)
984 if (Fread((char *)&ei[2], 1, blen, fd) != blen)
987 h = headerImport(ei, len, 0);
990 if (h == NULL && ei != NULL) {
996 int headerWrite(FD_t fd, Header h, int magicp)
1002 uh = headerExport(h, &length);
1006 case HEADER_MAGIC_YES:
1007 nb = Fwrite(rpm_header_magic, sizeof(uint8_t), sizeof(rpm_header_magic), fd);
1008 if (nb != sizeof(rpm_header_magic))
1011 case HEADER_MAGIC_NO:
1015 nb = Fwrite(uh, sizeof(char), length, fd);
1019 return (nb == length ? 0 : 1);
1022 int headerIsEntry(Header h, rpmTagVal tag)
1024 /* FIX: h modified by sort. */
1025 return (findEntry(h, tag, RPM_NULL_TYPE) ? 1 : 0);
1030 * Retrieve data from header entry.
1031 * Relevant flags (others are ignored), if neither is set allocation
1032 * behavior depends on data type(!)
1033 * HEADERGET_MINMEM: return pointers to header memory
1034 * HEADERGET_ALLOC: always return malloced memory, overrides MINMEM
1036 * @todo Permit retrieval of regions other than HEADER_IMUTABLE.
1037 * @param entry header entry
1038 * @param td tag data container
1039 * @param minMem string pointers refer to header memory?
1040 * @param flags flags to control memory allocation
1041 * @return 1 on success, otherwise error.
1043 static int copyTdEntry(const indexEntry entry, rpmtd td, headerGetFlags flags)
1045 rpm_count_t count = entry->info.count;
1046 int rc = 1; /* XXX 1 on success. */
1047 /* ALLOC overrides MINMEM */
1048 int allocMem = flags & HEADERGET_ALLOC;
1049 int minMem = allocMem ? 0 : flags & HEADERGET_MINMEM;
1050 int argvArray = (flags & HEADERGET_ARGV) ? 1 : 0;
1053 td->flags = RPMTD_IMMUTABLE;
1054 switch (entry->info.type) {
1057 * XXX This only works for
1058 * XXX "sealed" HEADER_IMMUTABLE/HEADER_SIGNATURES/HEADER_IMAGE.
1059 * XXX This will *not* work for unsealed legacy HEADER_IMAGE (i.e.
1060 * XXX a legacy header freshly read, but not yet unloaded to the rpmdb).
1062 if (ENTRY_IS_REGION(entry)) {
1063 int32_t * ei = ((int32_t *)entry->data) - 2;
1064 entryInfo pe = (entryInfo) (ei + 2);
1065 unsigned char * dataStart = (unsigned char *) (pe + ntohl(ei[0]));
1066 int32_t rdl = -entry->info.offset; /* negative offset */
1067 int32_t ril = rdl/sizeof(*pe);
1070 count = 2 * sizeof(*ei) + (ril * sizeof(*pe)) + rdl;
1071 if (entry->info.tag == RPMTAG_HEADERIMAGE) {
1075 count += REGION_TAG_COUNT;
1076 rdl += REGION_TAG_COUNT;
1079 td->data = xmalloc(count);
1080 ei = (int32_t *) td->data;
1084 pe = (entryInfo) memcpy(ei + 2, pe, (ril * sizeof(*pe)));
1086 dataStart = (unsigned char *) memcpy(pe + ril, dataStart, rdl);
1088 rc = regionSwab(NULL, ril, 0, pe, dataStart, dataStart + rdl, 0, 0);
1089 /* don't return data on failure */
1091 td->data = _free(td->data);
1093 /* XXX 1 on success. */
1094 rc = (rc < 0) ? 0 : 1;
1096 count = entry->length;
1098 ? memcpy(xmalloc(count), entry->data, count)
1102 case RPM_STRING_TYPE:
1103 /* simple string, but fallthrough if its actually an array */
1104 if (count == 1 && !argvArray) {
1105 td->data = allocMem ? xstrdup(entry->data) : entry->data;
1108 case RPM_STRING_ARRAY_TYPE:
1109 case RPM_I18NSTRING_TYPE:
1110 { const char ** ptrEntry;
1111 int tableSize = (count + argvArray) * sizeof(char *);
1116 td->data = xmalloc(tableSize);
1117 ptrEntry = (const char **) td->data;
1120 t = xmalloc(tableSize + entry->length);
1121 td->data = (void *)t;
1122 ptrEntry = (const char **) td->data;
1124 memcpy(t, entry->data, entry->length);
1126 for (i = 0; i < count; i++) {
1133 td->flags |= RPMTD_ARGV;
1138 case RPM_INT16_TYPE:
1139 case RPM_INT32_TYPE:
1140 case RPM_INT64_TYPE:
1142 td->data = xmalloc(entry->length);
1143 memcpy(td->data, entry->data, entry->length);
1145 td->data = entry->data;
1149 /* WTH? Don't mess with unknown data types... */
1154 td->type = entry->info.type;
1157 if (td->data && entry->data != td->data) {
1158 td->flags |= RPMTD_ALLOCED;
1165 * Does locale match entry in header i18n table?
1168 * The range [l,le) contains the next locale to match:
1169 * ll[_CC][.EEEEE][@dddd]
1171 * ll ISO language code (in lowercase).
1172 * CC (optional) ISO coutnry code (in uppercase).
1173 * EEEEE (optional) encoding (not really standardized).
1174 * dddd (optional) dialect.
1177 * @param td header i18n table data, NUL terminated
1178 * @param l start of locale to match
1179 * @param le end of locale to match
1180 * @return 1 on good match, 2 on weak match, 0 on no match
1182 static int headerMatchLocale(const char *td, const char *l, const char *le)
1186 /* First try a complete match. */
1187 if (strlen(td) == (le-l) && rstreqn(td, l, (le - l)))
1190 /* Next, try stripping optional dialect and matching. */
1191 for (fe = l; fe < le && *fe != '@'; fe++)
1193 if (fe < le && rstreqn(td, l, (fe - l)))
1196 /* Next, try stripping optional codeset and matching. */
1197 for (fe = l; fe < le && *fe != '.'; fe++)
1199 if (fe < le && rstreqn(td, l, (fe - l)))
1202 /* Finally, try stripping optional country code and matching. */
1203 for (fe = l; fe < le && *fe != '_'; fe++)
1205 if (fe < le && rstreqn(td, l, (fe - l)))
1212 * Return i18n string from header that matches locale.
1214 * @param entry i18n string data
1215 * @retval td tag data container
1216 * @param flags flags to control allocation
1219 static int copyI18NEntry(Header h, indexEntry entry, rpmtd td,
1220 headerGetFlags flags)
1222 const char *lang, *l, *le;
1225 td->type = RPM_STRING_TYPE;
1227 /* if no match, just return the first string */
1228 td->data = entry->data;
1230 /* XXX Drepper sez' this is the order. */
1231 if ((lang = getenv("LANGUAGE")) == NULL &&
1232 (lang = getenv("LC_ALL")) == NULL &&
1233 (lang = getenv("LC_MESSAGES")) == NULL &&
1234 (lang = getenv("LANG")) == NULL)
1237 if ((table = findEntry(h, RPMTAG_HEADERI18NTABLE, RPM_STRING_ARRAY_TYPE)) == NULL)
1240 for (l = lang; *l != '\0'; l = le) {
1242 char *ed, *ed_weak = NULL;
1245 while (*l && *l == ':') /* skip leading colons */
1249 for (le = l; *le && *le != ':'; le++) /* find end of this locale */
1252 /* For each entry in the header ... */
1253 for (langNum = 0, t = table->data, ed = entry->data;
1254 langNum < entry->info.count;
1255 langNum++, t += strlen(t) + 1, ed += strlen(ed) + 1) {
1257 int match = headerMatchLocale(t, l, le);
1261 } else if (match == 2) {
1272 if (flags & HEADERGET_ALLOC) {
1273 td->data = xstrdup(td->data);
1274 td->flags |= RPMTD_ALLOCED;
1281 * Retrieve tag data from header.
1283 * @retval td tag data container
1284 * @param flags flags to control retrieval
1285 * @return 1 on success, 0 on not found
1287 static int intGetTdEntry(Header h, rpmtd td, headerGetFlags flags)
1292 /* First find the tag */
1293 /* FIX: h modified by sort. */
1294 entry = findEntry(h, td->tag, RPM_NULL_TYPE);
1295 if (entry == NULL) {
1296 /* Td is zeroed above, just return... */
1300 if (flags & HEADERGET_RAW) {
1301 rc = copyTdEntry(entry, td, flags);
1303 switch (entry->info.type) {
1304 case RPM_I18NSTRING_TYPE:
1305 rc = copyI18NEntry(h, entry, td, flags);
1308 rc = copyTdEntry(entry, td, flags);
1314 td->flags |= RPMTD_INVALID;
1316 /* XXX 1 on success */
1317 return ((rc == 1) ? 1 : 0);
1320 int headerGet(Header h, rpmTagVal tag, rpmtd td, headerGetFlags flags)
1323 headerTagTagFunction tagfunc = intGetTdEntry;
1325 if (td == NULL) return 0;
1330 if (flags & HEADERGET_EXT) {
1331 headerTagTagFunction extfunc = rpmHeaderTagFunc(tag);
1332 if (extfunc) tagfunc = extfunc;
1334 rc = tagfunc(h, td, flags);
1336 assert(tag == td->tag);
1342 static void copyData(rpm_tagtype_t type, rpm_data_t dstPtr,
1343 rpm_constdata_t srcPtr, rpm_count_t cnt, int dataLength)
1346 case RPM_STRING_ARRAY_TYPE:
1347 case RPM_I18NSTRING_TYPE:
1348 { const char ** av = (const char **) srcPtr;
1351 while (cnt-- > 0 && dataLength > 0) {
1353 if ((s = *av++) == NULL)
1357 } while (s[-1] && --dataLength > 0);
1362 memmove(dstPtr, srcPtr, dataLength);
1368 * Return (malloc'ed) copy of entry data.
1369 * @param type entry data type
1370 * @param p entry data
1371 * @param c entry item count
1372 * @retval lengthPtr no. bytes in returned data
1373 * @return (malloc'ed) copy of entry data, NULL on error
1376 grabData(rpm_tagtype_t type, rpm_constdata_t p, rpm_count_t c, int * lengthPtr)
1378 rpm_data_t data = NULL;
1381 length = dataLength(type, p, c, 0, NULL);
1383 data = xmalloc(length);
1384 copyData(type, data, p, c, length);
1388 *lengthPtr = length;
1392 static int intAddEntry(Header h, rpmtd td)
1398 /* Count must always be >= 1 for headerAddEntry. */
1402 if (hdrchkType(td->type))
1404 if (hdrchkData(td->count))
1408 data = grabData(td->type, td->data, td->count, &length);
1409 if (data == NULL || length <= 0)
1412 /* Allocate more index space if necessary */
1413 if (h->indexUsed == h->indexAlloced) {
1414 h->indexAlloced += INDEX_MALLOC_SIZE;
1415 h->index = xrealloc(h->index, h->indexAlloced * sizeof(*h->index));
1418 /* Fill in the index */
1419 entry = h->index + h->indexUsed;
1420 entry->info.tag = td->tag;
1421 entry->info.type = td->type;
1422 entry->info.count = td->count;
1423 entry->info.offset = 0;
1425 entry->length = length;
1427 if (h->indexUsed > 0 && td->tag < h->index[h->indexUsed-1].info.tag)
1428 h->flags &= ~HEADERFLAG_SORTED;
1434 static int intAppendEntry(Header h, rpmtd td)
1439 if (td->type == RPM_STRING_TYPE || td->type == RPM_I18NSTRING_TYPE) {
1440 /* we can't do this */
1444 /* Find the tag entry in the header. */
1445 entry = findEntry(h, td->tag, td->type);
1449 length = dataLength(td->type, td->data, td->count, 0, NULL);
1453 if (ENTRY_IN_REGION(entry)) {
1454 char * t = xmalloc(entry->length + length);
1455 memcpy(t, entry->data, entry->length);
1457 entry->info.offset = 0;
1459 entry->data = xrealloc(entry->data, entry->length + length);
1461 copyData(td->type, ((char *) entry->data) + entry->length,
1462 td->data, td->count, length);
1464 entry->length += length;
1466 entry->info.count += td->count;
1471 int headerPut(Header h, rpmtd td, headerPutFlags flags)
1476 if (flags & HEADERPUT_APPEND) {
1477 rc = findEntry(h, td->tag, td->type) ?
1478 intAppendEntry(h, td) :
1481 rc = intAddEntry(h, td);
1486 int headerAddI18NString(Header h, rpmTagVal tag, const char * string,
1489 indexEntry table, entry;
1490 const char ** strArray;
1493 rpm_count_t i, langNum;
1496 table = findEntry(h, RPMTAG_HEADERI18NTABLE, RPM_STRING_ARRAY_TYPE);
1497 entry = findEntry(h, tag, RPM_I18NSTRING_TYPE);
1499 if (!table && entry)
1500 return 0; /* this shouldn't ever happen!! */
1502 if (!table && !entry) {
1503 const char * charArray[2];
1504 rpm_count_t count = 0;
1506 if (!lang || (lang[0] == 'C' && lang[1] == '\0')) {
1507 charArray[count++] = "C";
1509 charArray[count++] = "C";
1510 charArray[count++] = lang;
1514 td.tag = RPMTAG_HEADERI18NTABLE;
1515 td.type = RPM_STRING_ARRAY_TYPE;
1516 td.data = (void *) charArray;
1518 if (!headerPut(h, &td, HEADERPUT_DEFAULT))
1520 table = findEntry(h, RPMTAG_HEADERI18NTABLE, RPM_STRING_ARRAY_TYPE);
1525 if (!lang) lang = "C";
1527 { const char * l = table->data;
1528 for (langNum = 0; langNum < table->info.count; langNum++) {
1529 if (rstreq(l, lang)) break;
1534 if (langNum >= table->info.count) {
1535 length = strlen(lang) + 1;
1536 if (ENTRY_IN_REGION(table)) {
1537 char * t = xmalloc(table->length + length);
1538 memcpy(t, table->data, table->length);
1540 table->info.offset = 0;
1542 table->data = xrealloc(table->data, table->length + length);
1543 memmove(((char *)table->data) + table->length, lang, length);
1544 table->length += length;
1545 table->info.count++;
1551 strArray = xmalloc(sizeof(*strArray) * (langNum + 1));
1552 for (i = 0; i < langNum; i++)
1554 strArray[langNum] = string;
1558 td.type = RPM_I18NSTRING_TYPE;
1560 td.count = langNum + 1;
1561 rc = headerPut(h, &td, HEADERPUT_DEFAULT);
1564 } else if (langNum >= entry->info.count) {
1565 ghosts = langNum - entry->info.count;
1567 length = strlen(string) + 1 + ghosts;
1568 if (ENTRY_IN_REGION(entry)) {
1569 char * t = xmalloc(entry->length + length);
1570 memcpy(t, entry->data, entry->length);
1572 entry->info.offset = 0;
1574 entry->data = xrealloc(entry->data, entry->length + length);
1576 memset(((char *)entry->data) + entry->length, '\0', ghosts);
1577 memmove(((char *)entry->data) + entry->length + ghosts, string, strlen(string)+1);
1579 entry->length += length;
1580 entry->info.count = langNum + 1;
1582 char *b, *be, *e, *ee, *t;
1585 /* Set beginning/end pointers to previous data */
1586 b = be = e = ee = entry->data;
1587 for (i = 0; i < table->info.count; i++) {
1590 ee += strlen(ee) + 1;
1595 /* Get storage for new buffer */
1597 sn = strlen(string) + 1;
1599 length = bn + sn + en;
1600 t = buf = xmalloc(length);
1602 /* Copy values into new storage */
1605 memcpy(t, string, sn);
1610 /* Replace i18N string array */
1611 entry->length -= strlen(be) + 1;
1612 entry->length += sn;
1614 if (ENTRY_IN_REGION(entry)) {
1615 entry->info.offset = 0;
1617 entry->data = _free(entry->data);
1624 int headerMod(Header h, rpmtd td)
1631 /* First find the tag */
1632 entry = findEntry(h, td->tag, td->type);
1637 data = grabData(td->type, td->data, td->count, &length);
1638 if (data == NULL || length <= 0)
1641 /* make sure entry points to the first occurence of this tag */
1642 while (entry > h->index && (entry - 1)->info.tag == td->tag)
1645 /* free after we've grabbed the new data in case the two are intertwined;
1646 that's a bad idea but at least we won't break */
1647 oldData = entry->data;
1649 entry->info.count = td->count;
1650 entry->info.type = td->type;
1652 entry->length = length;
1654 if (ENTRY_IN_REGION(entry)) {
1655 entry->info.offset = 0;
1663 * Header tag iterator data structure.
1665 struct headerIterator_s {
1666 Header h; /*!< Header being iterated. */
1667 int next_index; /*!< Next tag index. */
1670 HeaderIterator headerFreeIterator(HeaderIterator hi)
1673 hi->h = headerFree(hi->h);
1679 HeaderIterator headerInitIterator(Header h)
1681 HeaderIterator hi = xmalloc(sizeof(*hi));
1685 hi->h = headerLink(h);
1690 static indexEntry nextIndex(HeaderIterator hi)
1694 indexEntry entry = NULL;
1696 for (slot = hi->next_index; slot < h->indexUsed; slot++) {
1697 entry = h->index + slot;
1698 if (!ENTRY_IS_REGION(entry))
1701 hi->next_index = slot;
1702 if (entry == NULL || slot >= h->indexUsed)
1709 rpmTagVal headerNextTag(HeaderIterator hi)
1711 indexEntry entry = nextIndex(hi);
1712 return entry ? entry->info.tag : RPMTAG_NOT_FOUND;
1715 int headerNext(HeaderIterator hi, rpmtd td)
1717 indexEntry entry = nextIndex(hi);
1722 td->tag = entry->info.tag;
1723 rc = copyTdEntry(entry, td, HEADERGET_DEFAULT);
1725 return ((rc == 1) ? 1 : 0);
1728 unsigned int headerGetInstance(Header h)
1730 return h ? h->instance : 0;
1733 void headerSetInstance(Header h, unsigned int instance)
1735 h->instance = instance;