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;
305 * Header string (array) size calculation, bounded if end is non-NULL.
306 * Return length (including \0 termination) on success, -1 on error.
308 static inline int strtaglen(const char *str, rpm_count_t c, const char *end)
310 const char *start = str;
314 while ((s = memchr(start, '\0', end-start))) {
315 if (--c == 0 || s > end)
320 while ((s = strchr(start, '\0'))) {
326 return (c > 0) ? -1 : (s - str + 1);
330 * Return length of entry data.
331 * @param type entry data type
332 * @param p entry data
333 * @param count entry item count
334 * @param onDisk data is concatenated strings (with NUL's))?
335 * @param pend pointer to end of data (or NULL)
336 * @return no. bytes in data, -1 on failure
338 static int dataLength(rpm_tagtype_t type, rpm_constdata_t p, rpm_count_t count,
339 int onDisk, rpm_constdata_t pend)
342 const char * se = pend;
346 case RPM_STRING_TYPE:
349 length = strtaglen(s, 1, se);
352 case RPM_STRING_ARRAY_TYPE:
353 case RPM_I18NSTRING_TYPE:
354 /* These are like RPM_STRING_TYPE, except they're *always* an array */
355 /* Compute sum of length of all strings, including nul terminators */
358 length = strtaglen(s, count, se);
360 const char ** av = (const char **)p;
362 /* add one for null termination */
363 length += strlen(*av++) + 1;
369 if (typeSizes[type] == -1)
371 length = typeSizes[(type & 0xf)] * count;
372 if (length < 0 || (se && (s + length) > se))
381 * Swap int32_t and int16_t arrays within header region.
383 * If a header region tag is in the set to be swabbed, as the data for a
384 * a header region is located after all other tag data.
386 * @param entry header entry
387 * @param il no. of entries
388 * @param dl start no. bytes of data
389 * @param pe header physical entry pointer (swapped)
390 * @param dataStart header data start
391 * @param dataEnd header data end
392 * @param regionid region offset
393 * @param fast use offsets for data sizes if possible
394 * @return no. bytes of data in region, -1 on error
396 static int regionSwab(indexEntry entry, int il, int dl,
398 unsigned char * dataStart,
399 const unsigned char * dataEnd,
400 int regionid, int fast)
402 if ((entry != NULL && regionid >= 0) || (entry == NULL && regionid != 0))
405 for (; il > 0; il--, pe++) {
406 struct indexEntry_s ie;
408 ie.info.tag = ntohl(pe->tag);
409 ie.info.type = ntohl(pe->type);
410 ie.info.count = ntohl(pe->count);
411 ie.info.offset = ntohl(pe->offset);
413 if (hdrchkType(ie.info.type))
415 if (hdrchkData(ie.info.count))
417 if (hdrchkData(ie.info.offset))
419 if (hdrchkAlign(ie.info.type, ie.info.offset))
422 ie.data = dataStart + ie.info.offset;
423 if (dataEnd && (unsigned char *)ie.data >= dataEnd)
426 if (fast && il > 1) {
427 ie.length = ntohl(pe[1].offset) - ie.info.offset;
429 ie.length = dataLength(ie.info.type, ie.data, ie.info.count,
432 if (ie.length < 0 || hdrchkData(ie.length))
438 ie.info.offset = regionid;
439 *entry = ie; /* structure assignment */
444 dl += alignDiff(ie.info.type, dl);
446 /* Perform endian conversions */
447 switch (ntohl(pe->type)) {
449 { uint64_t * it = ie.data;
450 for (; ie.info.count > 0; ie.info.count--, it += 1) {
451 if (dataEnd && ((unsigned char *)it) >= dataEnd)
457 { int32_t * it = ie.data;
458 for (; ie.info.count > 0; ie.info.count--, it += 1) {
459 if (dataEnd && ((unsigned char *)it) >= dataEnd)
465 { int16_t * it = ie.data;
466 for (; ie.info.count > 0; ie.info.count--, it += 1) {
467 if (dataEnd && ((unsigned char *)it) >= dataEnd)
480 void * headerExport(Header h, unsigned int *bsize)
491 int drlen, ndribbles;
493 if (h == NULL) return NULL;
495 /* Sort entries by (offset,tag). */
498 /* Compute (il,dl) for all tags, including those deleted in region. */
499 drlen = ndribbles = 0;
500 for (i = 0, entry = h->index; i < h->indexUsed; i++, entry++) {
501 if (ENTRY_IS_REGION(entry)) {
502 int32_t rdl = -entry->info.offset; /* negative offset */
503 int32_t ril = rdl/sizeof(*pe);
504 int rid = entry->info.offset;
507 dl += entry->rdlen + entry->info.count;
508 /* XXX Legacy regions do not include the region tag and data. */
509 if (i == 0 && (h->flags & HEADERFLAG_LEGACY))
512 /* Skip rest of entries in region, but account for dribbles. */
513 for (; i < h->indexUsed && entry->info.offset <= rid+1; i++, entry++) {
514 if (entry->info.offset <= rid)
518 diff = alignDiff(entry->info.type, dl);
526 drlen += entry->length;
534 /* Ignore deleted drips. */
535 if (entry->data == NULL || entry->length <= 0)
539 dl += alignDiff(entry->info.type, dl);
545 /* Sanity checks on header intro. */
546 if (hdrchkTags(il) || hdrchkData(dl))
549 len = sizeof(il) + sizeof(dl) + (il * sizeof(*pe)) + dl;
555 pe = (entryInfo) &ei[2];
556 dataStart = te = (char *) (pe + il);
558 for (i = 0, entry = h->index; i < h->indexUsed; i++, entry++) {
565 if (entry->data == NULL || entry->length <= 0)
568 t = (unsigned char*)te;
569 pe->tag = htonl(entry->info.tag);
570 pe->type = htonl(entry->info.type);
571 pe->count = htonl(entry->info.count);
573 if (ENTRY_IS_REGION(entry)) {
574 int32_t rdl = -entry->info.offset; /* negative offset */
575 int32_t ril = rdl/sizeof(*pe) + ndribbles;
576 int rid = entry->info.offset;
578 src = (char *)entry->data;
579 rdlen = entry->rdlen;
581 /* XXX Legacy regions do not include the region tag and data. */
582 if (i == 0 && (h->flags & HEADERFLAG_LEGACY)) {
585 memcpy(pe+1, src, rdl);
586 memcpy(te, src + rdl, rdlen);
589 pe->offset = htonl(te - dataStart);
592 stei[2] = htonl(-rdl-entry->info.count);
594 memcpy(te, stei, entry->info.count);
595 te += entry->info.count;
597 rdlen += entry->info.count;
599 count = regionSwab(NULL, ril, 0, pe, t, NULL, 0, 0);
605 memcpy(pe+1, src + sizeof(*pe), ((ril-1) * sizeof(*pe)));
606 memcpy(te, src + (ril * sizeof(*pe)), rdlen+entry->info.count+drlen);
609 entryInfo se = (entryInfo)src;
610 int off = ntohl(se->offset);
611 pe->offset = (off) ? htonl(te - dataStart) : htonl(off);
613 te += entry->info.count + drlen;
615 count = regionSwab(NULL, ril, 0, pe, t, NULL, 0, 0);
616 if (count != (rdlen + entry->info.count + drlen))
620 /* Skip rest of entries in region. */
621 while (i < h->indexUsed && entry->info.offset <= rid+1) {
631 /* Ignore deleted drips. */
632 if (entry->data == NULL || entry->length <= 0)
636 diff = alignDiff(entry->info.type, (te - dataStart));
642 pe->offset = htonl(te - dataStart);
644 /* copy data w/ endian conversions */
645 switch (entry->info.type) {
647 count = entry->info.count;
650 *((uint64_t *)te) = htonll(*((uint64_t *)src));
651 te += sizeof(uint64_t);
652 src += sizeof(uint64_t);
657 count = entry->info.count;
660 *((int32_t *)te) = htonl(*((int32_t *)src));
661 te += sizeof(int32_t);
662 src += sizeof(int32_t);
667 count = entry->info.count;
670 *((int16_t *)te) = htons(*((int16_t *)src));
671 te += sizeof(int16_t);
672 src += sizeof(int16_t);
677 memcpy(te, entry->data, entry->length);
684 /* Insure that there are no memcpy underruns/overruns. */
685 if (((char *)pe) != dataStart)
687 if ((((char *)ei)+len) != te)
702 void * headerUnload(Header h)
704 return headerExport(h, NULL);
708 * Find matching (tag,type) entry in header.
710 * @param tag entry tag
711 * @param type entry type
712 * @return header entry
715 indexEntry findEntry(Header h, rpmTagVal tag, rpm_tagtype_t type)
718 struct indexEntry_s key;
720 if (h == NULL) return NULL;
721 if (!(h->flags & HEADERFLAG_SORTED)) headerSort(h);
725 entry = bsearch(&key, h->index, h->indexUsed, sizeof(*h->index), indexCmp);
729 if (type == RPM_NULL_TYPE)
733 while (entry->info.tag == tag && entry->info.type != type &&
734 entry > h->index) entry--;
736 if (entry->info.tag == tag && entry->info.type == type)
742 int headerDel(Header h, rpmTagVal tag)
744 indexEntry last = h->index + h->indexUsed;
745 indexEntry entry, first;
748 entry = findEntry(h, tag, RPM_NULL_TYPE);
749 if (!entry) return 1;
751 /* Make sure entry points to the first occurence of this tag. */
752 while (entry > h->index && (entry - 1)->info.tag == tag)
755 /* Free data for tags being removed. */
756 for (first = entry; first < last; first++) {
758 if (first->info.tag != tag)
763 if (ENTRY_IN_REGION(first))
768 ne = (first - entry);
773 memmove(entry, first, (ne * sizeof(*entry)));
779 Header headerImport(void * blob, unsigned int bsize, headerImportFlags flags)
781 const int32_t * ei = (int32_t *) blob;
782 int32_t il = ntohl(ei[0]); /* index length */
783 int32_t dl = ntohl(ei[1]); /* data length */
784 unsigned int pvlen = sizeof(il) + sizeof(dl) +
785 (il * sizeof(struct entryInfo_s)) + dl;;
788 unsigned char * dataStart;
789 unsigned char * dataEnd;
792 int fast = (flags & HEADERIMPORT_FAST);
794 /* Sanity checks on header intro. */
795 if (bsize && bsize != pvlen)
797 if (hdrchkTags(il) || hdrchkData(dl) || pvlen >= headerMaxbytes)
800 h = headerCreate(blob, (flags & HEADERIMPORT_COPY) ? pvlen : 0, il);
802 ei = h->blob; /* In case we had to copy */
803 pe = (entryInfo) &ei[2];
804 dataStart = (unsigned char *) (pe + il);
805 dataEnd = dataStart + dl;
808 if (!(htonl(pe->tag) < RPMTAG_HEADERI18NTABLE)) {
809 h->flags |= HEADERFLAG_LEGACY;
810 entry->info.type = REGION_TAG_TYPE;
811 entry->info.tag = RPMTAG_HEADERIMAGE;
812 entry->info.count = REGION_TAG_COUNT;
813 entry->info.offset = ((unsigned char *)pe - dataStart); /* negative offset */
816 entry->length = pvlen - sizeof(il) - sizeof(dl);
817 rdlen = regionSwab(entry+1, il, 0, pe,
818 dataStart, dataEnd, entry->info.offset, fast);
821 entry->rdlen = rdlen;
827 h->flags &= ~HEADERFLAG_LEGACY;
829 entry->info.type = htonl(pe->type);
830 entry->info.count = htonl(pe->count);
832 if (hdrchkType(entry->info.type))
834 if (hdrchkTags(entry->info.count))
837 { int off = ntohl(pe->offset);
840 size_t nb = REGION_TAG_COUNT;
842 if (hdrchkRange(dl, (off + nb)))
844 /* XXX Hmm, why the copy? */
845 memcpy(&stei, dataStart + off, nb);
846 rdl = -ntohl(stei[2]); /* negative offset */
847 ril = rdl/sizeof(*pe);
848 if (hdrchkTags(ril) || hdrchkData(rdl))
850 entry->info.tag = htonl(pe->tag);
853 rdl = (ril * sizeof(struct entryInfo_s));
854 entry->info.tag = RPMTAG_HEADERIMAGE;
857 entry->info.offset = -rdl; /* negative offset */
860 entry->length = pvlen - sizeof(il) - sizeof(dl);
861 rdlen = regionSwab(entry+1, ril-1, 0, pe+1,
862 dataStart, dataEnd, entry->info.offset, fast);
865 entry->rdlen = rdlen;
867 if (ril < h->indexUsed) {
868 indexEntry newEntry = entry + ril;
869 int ne = (h->indexUsed - ril);
870 int rid = entry->info.offset+1;
872 /* Load dribble entries from region. */
873 rdlen = regionSwab(newEntry, ne, rdlen, pe+ril,
874 dataStart, dataEnd, rid, fast);
878 { indexEntry firstEntry = newEntry;
879 int save = h->indexUsed;
882 /* Dribble entries replace duplicate region entries. */
884 for (j = 0; j < ne; j++, newEntry++) {
885 (void) headerDel(h, newEntry->info.tag);
886 if (newEntry->info.tag == RPMTAG_BASENAMES)
887 (void) headerDel(h, RPMTAG_OLDFILENAMES);
890 /* If any duplicate entries were replaced, move new entries down. */
891 if (h->indexUsed < (save - ne)) {
892 memmove(h->index + h->indexUsed, firstEntry,
893 (ne * sizeof(*entry)));
899 rdlen += REGION_TAG_COUNT;
905 h->flags &= ~HEADERFLAG_SORTED;
907 h->flags |= HEADERFLAG_ALLOCATED;
913 if (flags & HEADERIMPORT_COPY)
921 Header headerReload(Header h, rpmTagVal tag)
925 void * uh = headerExport(h, &uc);
930 nh = headerImport(uh, uc, 0);
935 if (ENTRY_IS_REGION(nh->index)) {
936 if (tag == RPMTAG_HEADERSIGNATURES || tag == RPMTAG_HEADERIMMUTABLE)
937 nh->index[0].info.tag = tag;
942 Header headerLoad(void * uh)
944 return headerImport(uh, 0, 0);
947 Header headerCopyLoad(const void * uh)
949 /* Discards const but that's ok as we'll take a copy */
950 return headerImport((void *)uh, 0, HEADERIMPORT_COPY);
953 Header headerRead(FD_t fd, int magicp)
960 unsigned int len, blen;
962 if (magicp == HEADER_MAGIC_YES) {
965 if (Fread(block, 1, 4*sizeof(*block), fd) != 4*sizeof(*block))
970 if (memcmp(&magic, rpm_header_magic, sizeof(magic)))
973 il = ntohl(block[2]);
974 dl = ntohl(block[3]);
976 if (Fread(block, 1, 2*sizeof(*block), fd) != 2*sizeof(*block))
979 il = ntohl(block[0]);
980 dl = ntohl(block[1]);
983 blen = (il * sizeof(struct entryInfo_s)) + dl;
984 len = sizeof(il) + sizeof(dl) + blen;
986 /* Sanity checks on header intro. */
987 if (hdrchkTags(il) || hdrchkData(dl) || len > headerMaxbytes)
994 if (Fread((char *)&ei[2], 1, blen, fd) != blen)
997 h = headerImport(ei, len, 0);
1000 if (h == NULL && ei != NULL) {
1006 int headerWrite(FD_t fd, Header h, int magicp)
1009 unsigned int length;
1012 uh = headerExport(h, &length);
1016 case HEADER_MAGIC_YES:
1017 nb = Fwrite(rpm_header_magic, sizeof(uint8_t), sizeof(rpm_header_magic), fd);
1018 if (nb != sizeof(rpm_header_magic))
1021 case HEADER_MAGIC_NO:
1025 nb = Fwrite(uh, sizeof(char), length, fd);
1029 return (nb == length ? 0 : 1);
1032 int headerIsEntry(Header h, rpmTagVal tag)
1034 /* FIX: h modified by sort. */
1035 return (findEntry(h, tag, RPM_NULL_TYPE) ? 1 : 0);
1040 * Retrieve data from header entry.
1041 * Relevant flags (others are ignored), if neither is set allocation
1042 * behavior depends on data type(!)
1043 * HEADERGET_MINMEM: return pointers to header memory
1044 * HEADERGET_ALLOC: always return malloced memory, overrides MINMEM
1046 * @todo Permit retrieval of regions other than HEADER_IMUTABLE.
1047 * @param entry header entry
1048 * @param td tag data container
1049 * @param minMem string pointers refer to header memory?
1050 * @param flags flags to control memory allocation
1051 * @return 1 on success, otherwise error.
1053 static int copyTdEntry(const indexEntry entry, rpmtd td, headerGetFlags flags)
1055 rpm_count_t count = entry->info.count;
1056 int rc = 1; /* XXX 1 on success. */
1057 /* ALLOC overrides MINMEM */
1058 int allocMem = flags & HEADERGET_ALLOC;
1059 int minMem = allocMem ? 0 : flags & HEADERGET_MINMEM;
1060 int argvArray = (flags & HEADERGET_ARGV) ? 1 : 0;
1063 td->flags = RPMTD_IMMUTABLE;
1064 switch (entry->info.type) {
1067 * XXX This only works for
1068 * XXX "sealed" HEADER_IMMUTABLE/HEADER_SIGNATURES/HEADER_IMAGE.
1069 * XXX This will *not* work for unsealed legacy HEADER_IMAGE (i.e.
1070 * XXX a legacy header freshly read, but not yet unloaded to the rpmdb).
1072 if (ENTRY_IS_REGION(entry)) {
1073 int32_t * ei = ((int32_t *)entry->data) - 2;
1074 entryInfo pe = (entryInfo) (ei + 2);
1075 unsigned char * dataStart = (unsigned char *) (pe + ntohl(ei[0]));
1076 int32_t rdl = -entry->info.offset; /* negative offset */
1077 int32_t ril = rdl/sizeof(*pe);
1080 count = 2 * sizeof(*ei) + (ril * sizeof(*pe)) + rdl;
1081 if (entry->info.tag == RPMTAG_HEADERIMAGE) {
1085 count += REGION_TAG_COUNT;
1086 rdl += REGION_TAG_COUNT;
1089 td->data = xmalloc(count);
1090 ei = (int32_t *) td->data;
1094 pe = (entryInfo) memcpy(ei + 2, pe, (ril * sizeof(*pe)));
1096 dataStart = (unsigned char *) memcpy(pe + ril, dataStart, rdl);
1098 rc = regionSwab(NULL, ril, 0, pe, dataStart, dataStart + rdl, 0, 0);
1099 /* don't return data on failure */
1101 td->data = _free(td->data);
1103 /* XXX 1 on success. */
1104 rc = (rc < 0) ? 0 : 1;
1106 count = entry->length;
1108 ? memcpy(xmalloc(count), entry->data, count)
1112 case RPM_STRING_TYPE:
1113 /* simple string, but fallthrough if its actually an array */
1114 if (count == 1 && !argvArray) {
1115 td->data = allocMem ? xstrdup(entry->data) : entry->data;
1118 case RPM_STRING_ARRAY_TYPE:
1119 case RPM_I18NSTRING_TYPE:
1120 { const char ** ptrEntry;
1121 int tableSize = (count + argvArray) * sizeof(char *);
1126 td->data = xmalloc(tableSize);
1127 ptrEntry = (const char **) td->data;
1130 t = xmalloc(tableSize + entry->length);
1131 td->data = (void *)t;
1132 ptrEntry = (const char **) td->data;
1134 memcpy(t, entry->data, entry->length);
1136 for (i = 0; i < count; i++) {
1143 td->flags |= RPMTD_ARGV;
1148 case RPM_INT16_TYPE:
1149 case RPM_INT32_TYPE:
1150 case RPM_INT64_TYPE:
1152 td->data = xmalloc(entry->length);
1153 memcpy(td->data, entry->data, entry->length);
1155 td->data = entry->data;
1159 /* WTH? Don't mess with unknown data types... */
1164 td->type = entry->info.type;
1167 if (td->data && entry->data != td->data) {
1168 td->flags |= RPMTD_ALLOCED;
1175 * Does locale match entry in header i18n table?
1178 * The range [l,le) contains the next locale to match:
1179 * ll[_CC][.EEEEE][@dddd]
1181 * ll ISO language code (in lowercase).
1182 * CC (optional) ISO coutnry code (in uppercase).
1183 * EEEEE (optional) encoding (not really standardized).
1184 * dddd (optional) dialect.
1187 * @param td header i18n table data, NUL terminated
1188 * @param l start of locale to match
1189 * @param le end of locale to match
1190 * @return 1 on good match, 2 on weak match, 0 on no match
1192 static int headerMatchLocale(const char *td, const char *l, const char *le)
1196 /* First try a complete match. */
1197 if (strlen(td) == (le-l) && rstreqn(td, l, (le - l)))
1200 /* Next, try stripping optional dialect and matching. */
1201 for (fe = l; fe < le && *fe != '@'; fe++)
1203 if (fe < le && rstreqn(td, l, (fe - l)))
1206 /* Next, try stripping optional codeset and matching. */
1207 for (fe = l; fe < le && *fe != '.'; fe++)
1209 if (fe < le && rstreqn(td, l, (fe - l)))
1212 /* Finally, try stripping optional country code and matching. */
1213 for (fe = l; fe < le && *fe != '_'; fe++)
1215 if (fe < le && rstreqn(td, l, (fe - l)))
1222 * Return i18n string from header that matches locale.
1224 * @param entry i18n string data
1225 * @retval td tag data container
1226 * @param flags flags to control allocation
1229 static int copyI18NEntry(Header h, indexEntry entry, rpmtd td,
1230 headerGetFlags flags)
1232 const char *lang, *l, *le;
1235 td->type = RPM_STRING_TYPE;
1237 /* if no match, just return the first string */
1238 td->data = entry->data;
1240 /* XXX Drepper sez' this is the order. */
1241 if ((lang = getenv("LANGUAGE")) == NULL &&
1242 (lang = getenv("LC_ALL")) == NULL &&
1243 (lang = getenv("LC_MESSAGES")) == NULL &&
1244 (lang = getenv("LANG")) == NULL)
1247 if ((table = findEntry(h, RPMTAG_HEADERI18NTABLE, RPM_STRING_ARRAY_TYPE)) == NULL)
1250 for (l = lang; *l != '\0'; l = le) {
1252 char *ed, *ed_weak = NULL;
1255 while (*l && *l == ':') /* skip leading colons */
1259 for (le = l; *le && *le != ':'; le++) /* find end of this locale */
1262 /* For each entry in the header ... */
1263 for (langNum = 0, t = table->data, ed = entry->data;
1264 langNum < entry->info.count;
1265 langNum++, t += strlen(t) + 1, ed += strlen(ed) + 1) {
1267 int match = headerMatchLocale(t, l, le);
1271 } else if (match == 2) {
1282 if (flags & HEADERGET_ALLOC) {
1283 td->data = xstrdup(td->data);
1284 td->flags |= RPMTD_ALLOCED;
1291 * Retrieve tag data from header.
1293 * @retval td tag data container
1294 * @param flags flags to control retrieval
1295 * @return 1 on success, 0 on not found
1297 static int intGetTdEntry(Header h, rpmtd td, headerGetFlags flags)
1302 /* First find the tag */
1303 /* FIX: h modified by sort. */
1304 entry = findEntry(h, td->tag, RPM_NULL_TYPE);
1305 if (entry == NULL) {
1306 /* Td is zeroed above, just return... */
1310 if (flags & HEADERGET_RAW) {
1311 rc = copyTdEntry(entry, td, flags);
1313 switch (entry->info.type) {
1314 case RPM_I18NSTRING_TYPE:
1315 rc = copyI18NEntry(h, entry, td, flags);
1318 rc = copyTdEntry(entry, td, flags);
1324 td->flags |= RPMTD_INVALID;
1326 /* XXX 1 on success */
1327 return ((rc == 1) ? 1 : 0);
1330 int headerGet(Header h, rpmTagVal tag, rpmtd td, headerGetFlags flags)
1333 headerTagTagFunction tagfunc = intGetTdEntry;
1335 if (td == NULL) return 0;
1340 if (flags & HEADERGET_EXT) {
1341 headerTagTagFunction extfunc = rpmHeaderTagFunc(tag);
1342 if (extfunc) tagfunc = extfunc;
1344 rc = tagfunc(h, td, flags);
1346 assert(tag == td->tag);
1352 static void copyData(rpm_tagtype_t type, rpm_data_t dstPtr,
1353 rpm_constdata_t srcPtr, rpm_count_t cnt, int dataLength)
1356 case RPM_STRING_ARRAY_TYPE:
1357 case RPM_I18NSTRING_TYPE:
1358 { const char ** av = (const char **) srcPtr;
1361 while (cnt-- > 0 && dataLength > 0) {
1363 if ((s = *av++) == NULL)
1367 } while (s[-1] && --dataLength > 0);
1372 memmove(dstPtr, srcPtr, dataLength);
1378 * Return (malloc'ed) copy of entry data.
1379 * @param type entry data type
1380 * @param p entry data
1381 * @param c entry item count
1382 * @retval lengthPtr no. bytes in returned data
1383 * @return (malloc'ed) copy of entry data, NULL on error
1386 grabData(rpm_tagtype_t type, rpm_constdata_t p, rpm_count_t c, int * lengthPtr)
1388 rpm_data_t data = NULL;
1391 length = dataLength(type, p, c, 0, NULL);
1393 data = xmalloc(length);
1394 copyData(type, data, p, c, length);
1398 *lengthPtr = length;
1402 static int intAddEntry(Header h, rpmtd td)
1408 /* Count must always be >= 1 for headerAddEntry. */
1412 if (hdrchkType(td->type))
1414 if (hdrchkData(td->count))
1418 data = grabData(td->type, td->data, td->count, &length);
1419 if (data == NULL || length <= 0)
1422 /* Allocate more index space if necessary */
1423 if (h->indexUsed == h->indexAlloced) {
1424 h->indexAlloced += INDEX_MALLOC_SIZE;
1425 h->index = xrealloc(h->index, h->indexAlloced * sizeof(*h->index));
1428 /* Fill in the index */
1429 entry = h->index + h->indexUsed;
1430 entry->info.tag = td->tag;
1431 entry->info.type = td->type;
1432 entry->info.count = td->count;
1433 entry->info.offset = 0;
1435 entry->length = length;
1437 if (h->indexUsed > 0 && td->tag < h->index[h->indexUsed-1].info.tag)
1438 h->flags &= ~HEADERFLAG_SORTED;
1444 static int intAppendEntry(Header h, rpmtd td)
1449 if (td->type == RPM_STRING_TYPE || td->type == RPM_I18NSTRING_TYPE) {
1450 /* we can't do this */
1454 /* Find the tag entry in the header. */
1455 entry = findEntry(h, td->tag, td->type);
1459 length = dataLength(td->type, td->data, td->count, 0, NULL);
1463 if (ENTRY_IN_REGION(entry)) {
1464 char * t = xmalloc(entry->length + length);
1465 memcpy(t, entry->data, entry->length);
1467 entry->info.offset = 0;
1469 entry->data = xrealloc(entry->data, entry->length + length);
1471 copyData(td->type, ((char *) entry->data) + entry->length,
1472 td->data, td->count, length);
1474 entry->length += length;
1476 entry->info.count += td->count;
1481 int headerPut(Header h, rpmtd td, headerPutFlags flags)
1486 if (flags & HEADERPUT_APPEND) {
1487 rc = findEntry(h, td->tag, td->type) ?
1488 intAppendEntry(h, td) :
1491 rc = intAddEntry(h, td);
1496 int headerAddI18NString(Header h, rpmTagVal tag, const char * string,
1499 indexEntry table, entry;
1500 const char ** strArray;
1503 rpm_count_t i, langNum;
1506 table = findEntry(h, RPMTAG_HEADERI18NTABLE, RPM_STRING_ARRAY_TYPE);
1507 entry = findEntry(h, tag, RPM_I18NSTRING_TYPE);
1509 if (!table && entry)
1510 return 0; /* this shouldn't ever happen!! */
1512 if (!table && !entry) {
1513 const char * charArray[2];
1514 rpm_count_t count = 0;
1516 if (!lang || (lang[0] == 'C' && lang[1] == '\0')) {
1517 charArray[count++] = "C";
1519 charArray[count++] = "C";
1520 charArray[count++] = lang;
1524 td.tag = RPMTAG_HEADERI18NTABLE;
1525 td.type = RPM_STRING_ARRAY_TYPE;
1526 td.data = (void *) charArray;
1528 if (!headerPut(h, &td, HEADERPUT_DEFAULT))
1530 table = findEntry(h, RPMTAG_HEADERI18NTABLE, RPM_STRING_ARRAY_TYPE);
1535 if (!lang) lang = "C";
1537 { const char * l = table->data;
1538 for (langNum = 0; langNum < table->info.count; langNum++) {
1539 if (rstreq(l, lang)) break;
1544 if (langNum >= table->info.count) {
1545 length = strlen(lang) + 1;
1546 if (ENTRY_IN_REGION(table)) {
1547 char * t = xmalloc(table->length + length);
1548 memcpy(t, table->data, table->length);
1550 table->info.offset = 0;
1552 table->data = xrealloc(table->data, table->length + length);
1553 memmove(((char *)table->data) + table->length, lang, length);
1554 table->length += length;
1555 table->info.count++;
1561 strArray = xmalloc(sizeof(*strArray) * (langNum + 1));
1562 for (i = 0; i < langNum; i++)
1564 strArray[langNum] = string;
1568 td.type = RPM_I18NSTRING_TYPE;
1570 td.count = langNum + 1;
1571 rc = headerPut(h, &td, HEADERPUT_DEFAULT);
1574 } else if (langNum >= entry->info.count) {
1575 ghosts = langNum - entry->info.count;
1577 length = strlen(string) + 1 + ghosts;
1578 if (ENTRY_IN_REGION(entry)) {
1579 char * t = xmalloc(entry->length + length);
1580 memcpy(t, entry->data, entry->length);
1582 entry->info.offset = 0;
1584 entry->data = xrealloc(entry->data, entry->length + length);
1586 memset(((char *)entry->data) + entry->length, '\0', ghosts);
1587 memmove(((char *)entry->data) + entry->length + ghosts, string, strlen(string)+1);
1589 entry->length += length;
1590 entry->info.count = langNum + 1;
1592 char *b, *be, *e, *ee, *t;
1595 /* Set beginning/end pointers to previous data */
1596 b = be = e = ee = entry->data;
1597 for (i = 0; i < table->info.count; i++) {
1600 ee += strlen(ee) + 1;
1605 /* Get storage for new buffer */
1607 sn = strlen(string) + 1;
1609 length = bn + sn + en;
1610 t = buf = xmalloc(length);
1612 /* Copy values into new storage */
1615 memcpy(t, string, sn);
1620 /* Replace i18N string array */
1621 entry->length -= strlen(be) + 1;
1622 entry->length += sn;
1624 if (ENTRY_IN_REGION(entry)) {
1625 entry->info.offset = 0;
1627 entry->data = _free(entry->data);
1634 int headerMod(Header h, rpmtd td)
1641 /* First find the tag */
1642 entry = findEntry(h, td->tag, td->type);
1647 data = grabData(td->type, td->data, td->count, &length);
1648 if (data == NULL || length <= 0)
1651 /* make sure entry points to the first occurence of this tag */
1652 while (entry > h->index && (entry - 1)->info.tag == td->tag)
1655 /* free after we've grabbed the new data in case the two are intertwined;
1656 that's a bad idea but at least we won't break */
1657 oldData = entry->data;
1659 entry->info.count = td->count;
1660 entry->info.type = td->type;
1662 entry->length = length;
1664 if (ENTRY_IN_REGION(entry)) {
1665 entry->info.offset = 0;
1673 * Header tag iterator data structure.
1675 struct headerIterator_s {
1676 Header h; /*!< Header being iterated. */
1677 int next_index; /*!< Next tag index. */
1680 HeaderIterator headerFreeIterator(HeaderIterator hi)
1683 hi->h = headerFree(hi->h);
1689 HeaderIterator headerInitIterator(Header h)
1691 HeaderIterator hi = xmalloc(sizeof(*hi));
1695 hi->h = headerLink(h);
1700 static indexEntry nextIndex(HeaderIterator hi)
1704 indexEntry entry = NULL;
1706 for (slot = hi->next_index; slot < h->indexUsed; slot++) {
1707 entry = h->index + slot;
1708 if (!ENTRY_IS_REGION(entry))
1711 hi->next_index = slot;
1712 if (entry == NULL || slot >= h->indexUsed)
1719 rpmTagVal headerNextTag(HeaderIterator hi)
1721 indexEntry entry = nextIndex(hi);
1722 return entry ? entry->info.tag : RPMTAG_NOT_FOUND;
1725 int headerNext(HeaderIterator hi, rpmtd td)
1727 indexEntry entry = nextIndex(hi);
1732 td->tag = entry->info.tag;
1733 rc = copyTdEntry(entry, td, HEADERGET_DEFAULT);
1735 return ((rc == 1) ? 1 : 0);
1738 unsigned int headerGetInstance(Header h)
1740 return h ? h->instance : 0;
1743 void headerSetInstance(Header h, unsigned int instance)
1745 h->instance = instance;