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, int32_t indexLen)
159 Header h = xcalloc(1, sizeof(*h));
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);
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 * @return no. bytes of data in region, -1 on error
385 static int regionSwab(indexEntry entry, int il, int dl,
387 unsigned char * dataStart,
388 const unsigned char * dataEnd,
391 if ((entry != NULL && regionid >= 0) || (entry == NULL && regionid != 0))
394 for (; il > 0; il--, pe++) {
395 struct indexEntry_s ie;
397 ie.info.tag = ntohl(pe->tag);
398 ie.info.type = ntohl(pe->type);
399 ie.info.count = ntohl(pe->count);
400 ie.info.offset = ntohl(pe->offset);
402 if (hdrchkType(ie.info.type))
404 if (hdrchkData(ie.info.count))
406 if (hdrchkData(ie.info.offset))
408 if (hdrchkAlign(ie.info.type, ie.info.offset))
411 ie.data = dataStart + ie.info.offset;
412 if (dataEnd && (unsigned char *)ie.data >= dataEnd)
415 ie.length = dataLength(ie.info.type, ie.data, ie.info.count, 1, dataEnd);
416 if (ie.length < 0 || hdrchkData(ie.length))
422 ie.info.offset = regionid;
423 *entry = ie; /* structure assignment */
428 dl += alignDiff(ie.info.type, dl);
430 /* Perform endian conversions */
431 switch (ntohl(pe->type)) {
433 { uint64_t * it = ie.data;
434 for (; ie.info.count > 0; ie.info.count--, it += 1) {
435 if (dataEnd && ((unsigned char *)it) >= dataEnd)
441 { int32_t * it = ie.data;
442 for (; ie.info.count > 0; ie.info.count--, it += 1) {
443 if (dataEnd && ((unsigned char *)it) >= dataEnd)
449 { int16_t * it = ie.data;
450 for (; ie.info.count > 0; ie.info.count--, it += 1) {
451 if (dataEnd && ((unsigned char *)it) >= dataEnd)
467 * @retval *lengthPtr no. bytes in unloaded header blob
468 * @return unloaded header blob (NULL on error)
470 static void * doHeaderUnload(Header h, size_t * lengthPtr)
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);
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);
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 doHeaderUnload(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 headerLoad(void * uh)
771 int32_t * ei = (int32_t *) uh;
772 int32_t il = ntohl(ei[0]); /* index length */
773 int32_t dl = ntohl(ei[1]); /* data length */
774 size_t pvlen = sizeof(il) + sizeof(dl) +
775 (il * sizeof(struct entryInfo_s)) + dl;
778 unsigned char * dataStart;
779 unsigned char * dataEnd;
783 /* Sanity checks on header intro. */
784 if (hdrchkTags(il) || hdrchkData(dl))
787 pe = (entryInfo) &ei[2];
788 dataStart = (unsigned char *) (pe + il);
789 dataEnd = dataStart + dl;
791 h = headerCreate(uh, il);
794 if (!(htonl(pe->tag) < RPMTAG_HEADERI18NTABLE)) {
795 h->flags |= HEADERFLAG_LEGACY;
796 entry->info.type = REGION_TAG_TYPE;
797 entry->info.tag = RPMTAG_HEADERIMAGE;
798 entry->info.count = REGION_TAG_COUNT;
799 entry->info.offset = ((unsigned char *)pe - dataStart); /* negative offset */
802 entry->length = pvlen - sizeof(il) - sizeof(dl);
803 rdlen = regionSwab(entry+1, il, 0, pe, dataStart, dataEnd, entry->info.offset);
806 entry->rdlen = rdlen;
812 h->flags &= ~HEADERFLAG_LEGACY;
814 entry->info.type = htonl(pe->type);
815 entry->info.count = htonl(pe->count);
817 if (hdrchkType(entry->info.type))
819 if (hdrchkTags(entry->info.count))
822 { int off = ntohl(pe->offset);
825 size_t nb = REGION_TAG_COUNT;
827 if (hdrchkRange(dl, (off + nb)))
829 /* XXX Hmm, why the copy? */
830 memcpy(&stei, dataStart + off, nb);
831 rdl = -ntohl(stei[2]); /* negative offset */
832 ril = rdl/sizeof(*pe);
833 if (hdrchkTags(ril) || hdrchkData(rdl))
835 entry->info.tag = htonl(pe->tag);
838 rdl = (ril * sizeof(struct entryInfo_s));
839 entry->info.tag = RPMTAG_HEADERIMAGE;
842 entry->info.offset = -rdl; /* negative offset */
845 entry->length = pvlen - sizeof(il) - sizeof(dl);
846 rdlen = regionSwab(entry+1, ril-1, 0, pe+1, dataStart, dataEnd, entry->info.offset);
849 entry->rdlen = rdlen;
851 if (ril < h->indexUsed) {
852 indexEntry newEntry = entry + ril;
853 int ne = (h->indexUsed - ril);
854 int rid = entry->info.offset+1;
856 /* Load dribble entries from region. */
857 rdlen = regionSwab(newEntry, ne, rdlen, pe+ril,
858 dataStart, dataEnd, rid);
862 { indexEntry firstEntry = newEntry;
863 int save = h->indexUsed;
866 /* Dribble entries replace duplicate region entries. */
868 for (j = 0; j < ne; j++, newEntry++) {
869 (void) headerDel(h, newEntry->info.tag);
870 if (newEntry->info.tag == RPMTAG_BASENAMES)
871 (void) headerDel(h, RPMTAG_OLDFILENAMES);
874 /* If any duplicate entries were replaced, move new entries down. */
875 if (h->indexUsed < (save - ne)) {
876 memmove(h->index + h->indexUsed, firstEntry,
877 (ne * sizeof(*entry)));
883 rdlen += REGION_TAG_COUNT;
889 h->flags &= ~HEADERFLAG_SORTED;
891 h->flags |= HEADERFLAG_ALLOCATED;
903 Header headerReload(Header h, rpmTagVal tag)
906 void * uh = doHeaderUnload(h, NULL);
916 if (ENTRY_IS_REGION(nh->index)) {
917 if (tag == RPMTAG_HEADERSIGNATURES || tag == RPMTAG_HEADERIMMUTABLE)
918 nh->index[0].info.tag = tag;
923 Header headerCopyLoad(const void * uh)
925 int32_t * ei = (int32_t *) uh;
926 int32_t il = ntohl(ei[0]); /* index length */
927 int32_t dl = ntohl(ei[1]); /* data length */
928 size_t pvlen = sizeof(il) + sizeof(dl) +
929 (il * sizeof(struct entryInfo_s)) + dl;
932 /* Sanity checks on header intro. */
933 if (!(hdrchkTags(il) || hdrchkData(dl)) && pvlen < headerMaxbytes) {
934 void * nuh = memcpy(xmalloc(pvlen), uh, pvlen);
935 if ((h = headerLoad(nuh)) == NULL)
941 Header headerRead(FD_t fd, int magicp)
950 if (magicp == HEADER_MAGIC_YES) {
953 if (Fread(block, 1, 4*sizeof(*block), fd) != 4*sizeof(*block))
958 if (memcmp(&magic, rpm_header_magic, sizeof(magic)))
961 il = ntohl(block[2]);
962 dl = ntohl(block[3]);
964 if (Fread(block, 1, 2*sizeof(*block), fd) != 2*sizeof(*block))
967 il = ntohl(block[0]);
968 dl = ntohl(block[1]);
971 len = sizeof(il) + sizeof(dl) + (il * sizeof(struct entryInfo_s)) + dl;
973 /* Sanity checks on header intro. */
974 if (hdrchkTags(il) || hdrchkData(dl) || len > headerMaxbytes)
980 len -= sizeof(il) + sizeof(dl);
982 if (Fread((char *)&ei[2], 1, len, fd) != len)
988 if (h == NULL && ei != NULL) {
994 int headerWrite(FD_t fd, Header h, int magicp)
1000 uh = doHeaderUnload(h, &length);
1004 case HEADER_MAGIC_YES:
1005 nb = Fwrite(rpm_header_magic, sizeof(uint8_t), sizeof(rpm_header_magic), fd);
1006 if (nb != sizeof(rpm_header_magic))
1009 case HEADER_MAGIC_NO:
1013 nb = Fwrite(uh, sizeof(char), length, fd);
1017 return (nb == length ? 0 : 1);
1020 int headerIsEntry(Header h, rpmTagVal tag)
1022 /* FIX: h modified by sort. */
1023 return (findEntry(h, tag, RPM_NULL_TYPE) ? 1 : 0);
1028 * Retrieve data from header entry.
1029 * Relevant flags (others are ignored), if neither is set allocation
1030 * behavior depends on data type(!)
1031 * HEADERGET_MINMEM: return pointers to header memory
1032 * HEADERGET_ALLOC: always return malloced memory, overrides MINMEM
1034 * @todo Permit retrieval of regions other than HEADER_IMUTABLE.
1035 * @param entry header entry
1036 * @param td tag data container
1037 * @param minMem string pointers refer to header memory?
1038 * @param flags flags to control memory allocation
1039 * @return 1 on success, otherwise error.
1041 static int copyTdEntry(const indexEntry entry, rpmtd td, headerGetFlags flags)
1043 rpm_count_t count = entry->info.count;
1044 int rc = 1; /* XXX 1 on success. */
1045 /* ALLOC overrides MINMEM */
1046 int allocMem = flags & HEADERGET_ALLOC;
1047 int minMem = allocMem ? 0 : flags & HEADERGET_MINMEM;
1048 int argvArray = (flags & HEADERGET_ARGV) ? 1 : 0;
1051 td->flags = RPMTD_IMMUTABLE;
1052 switch (entry->info.type) {
1055 * XXX This only works for
1056 * XXX "sealed" HEADER_IMMUTABLE/HEADER_SIGNATURES/HEADER_IMAGE.
1057 * XXX This will *not* work for unsealed legacy HEADER_IMAGE (i.e.
1058 * XXX a legacy header freshly read, but not yet unloaded to the rpmdb).
1060 if (ENTRY_IS_REGION(entry)) {
1061 int32_t * ei = ((int32_t *)entry->data) - 2;
1062 entryInfo pe = (entryInfo) (ei + 2);
1063 unsigned char * dataStart = (unsigned char *) (pe + ntohl(ei[0]));
1064 int32_t rdl = -entry->info.offset; /* negative offset */
1065 int32_t ril = rdl/sizeof(*pe);
1068 count = 2 * sizeof(*ei) + (ril * sizeof(*pe)) + rdl;
1069 if (entry->info.tag == RPMTAG_HEADERIMAGE) {
1073 count += REGION_TAG_COUNT;
1074 rdl += REGION_TAG_COUNT;
1077 td->data = xmalloc(count);
1078 ei = (int32_t *) td->data;
1082 pe = (entryInfo) memcpy(ei + 2, pe, (ril * sizeof(*pe)));
1084 dataStart = (unsigned char *) memcpy(pe + ril, dataStart, rdl);
1086 rc = regionSwab(NULL, ril, 0, pe, dataStart, dataStart + rdl, 0);
1087 /* don't return data on failure */
1089 td->data = _free(td->data);
1091 /* XXX 1 on success. */
1092 rc = (rc < 0) ? 0 : 1;
1094 count = entry->length;
1096 ? memcpy(xmalloc(count), entry->data, count)
1100 case RPM_STRING_TYPE:
1101 /* simple string, but fallthrough if its actually an array */
1102 if (count == 1 && !argvArray) {
1103 td->data = allocMem ? xstrdup(entry->data) : entry->data;
1106 case RPM_STRING_ARRAY_TYPE:
1107 case RPM_I18NSTRING_TYPE:
1108 { const char ** ptrEntry;
1109 int tableSize = (count + argvArray) * sizeof(char *);
1114 td->data = xmalloc(tableSize);
1115 ptrEntry = (const char **) td->data;
1118 t = xmalloc(tableSize + entry->length);
1119 td->data = (void *)t;
1120 ptrEntry = (const char **) td->data;
1122 memcpy(t, entry->data, entry->length);
1124 for (i = 0; i < count; i++) {
1131 td->flags |= RPMTD_ARGV;
1136 case RPM_INT16_TYPE:
1137 case RPM_INT32_TYPE:
1138 case RPM_INT64_TYPE:
1140 td->data = xmalloc(entry->length);
1141 memcpy(td->data, entry->data, entry->length);
1143 td->data = entry->data;
1147 /* WTH? Don't mess with unknown data types... */
1152 td->type = entry->info.type;
1155 if (td->data && entry->data != td->data) {
1156 td->flags |= RPMTD_ALLOCED;
1163 * Does locale match entry in header i18n table?
1166 * The range [l,le) contains the next locale to match:
1167 * ll[_CC][.EEEEE][@dddd]
1169 * ll ISO language code (in lowercase).
1170 * CC (optional) ISO coutnry code (in uppercase).
1171 * EEEEE (optional) encoding (not really standardized).
1172 * dddd (optional) dialect.
1175 * @param td header i18n table data, NUL terminated
1176 * @param l start of locale to match
1177 * @param le end of locale to match
1178 * @return 1 on good match, 2 on weak match, 0 on no match
1180 static int headerMatchLocale(const char *td, const char *l, const char *le)
1184 /* First try a complete match. */
1185 if (strlen(td) == (le-l) && rstreqn(td, l, (le - l)))
1188 /* Next, try stripping optional dialect and matching. */
1189 for (fe = l; fe < le && *fe != '@'; fe++)
1191 if (fe < le && rstreqn(td, l, (fe - l)))
1194 /* Next, try stripping optional codeset and matching. */
1195 for (fe = l; fe < le && *fe != '.'; fe++)
1197 if (fe < le && rstreqn(td, l, (fe - l)))
1200 /* Finally, try stripping optional country code and matching. */
1201 for (fe = l; fe < le && *fe != '_'; fe++)
1203 if (fe < le && rstreqn(td, l, (fe - l)))
1210 * Return i18n string from header that matches locale.
1212 * @param entry i18n string data
1213 * @retval td tag data container
1214 * @param flags flags to control allocation
1217 static int copyI18NEntry(Header h, indexEntry entry, rpmtd td,
1218 headerGetFlags flags)
1220 const char *lang, *l, *le;
1223 td->type = RPM_STRING_TYPE;
1225 /* if no match, just return the first string */
1226 td->data = entry->data;
1228 /* XXX Drepper sez' this is the order. */
1229 if ((lang = getenv("LANGUAGE")) == NULL &&
1230 (lang = getenv("LC_ALL")) == NULL &&
1231 (lang = getenv("LC_MESSAGES")) == NULL &&
1232 (lang = getenv("LANG")) == NULL)
1235 if ((table = findEntry(h, RPMTAG_HEADERI18NTABLE, RPM_STRING_ARRAY_TYPE)) == NULL)
1238 for (l = lang; *l != '\0'; l = le) {
1240 char *ed, *ed_weak = NULL;
1243 while (*l && *l == ':') /* skip leading colons */
1247 for (le = l; *le && *le != ':'; le++) /* find end of this locale */
1250 /* For each entry in the header ... */
1251 for (langNum = 0, t = table->data, ed = entry->data;
1252 langNum < entry->info.count;
1253 langNum++, t += strlen(t) + 1, ed += strlen(ed) + 1) {
1255 int match = headerMatchLocale(t, l, le);
1259 } else if (match == 2) {
1270 if (flags & HEADERGET_ALLOC) {
1271 td->data = xstrdup(td->data);
1272 td->flags |= RPMTD_ALLOCED;
1279 * Retrieve tag data from header.
1281 * @retval td tag data container
1282 * @param flags flags to control retrieval
1283 * @return 1 on success, 0 on not found
1285 static int intGetTdEntry(Header h, rpmtd td, headerGetFlags flags)
1290 /* First find the tag */
1291 /* FIX: h modified by sort. */
1292 entry = findEntry(h, td->tag, RPM_NULL_TYPE);
1293 if (entry == NULL) {
1294 /* Td is zeroed above, just return... */
1298 if (flags & HEADERGET_RAW) {
1299 rc = copyTdEntry(entry, td, flags);
1301 switch (entry->info.type) {
1302 case RPM_I18NSTRING_TYPE:
1303 rc = copyI18NEntry(h, entry, td, flags);
1306 rc = copyTdEntry(entry, td, flags);
1311 /* XXX 1 on success */
1312 return ((rc == 1) ? 1 : 0);
1315 int headerGet(Header h, rpmTagVal tag, rpmtd td, headerGetFlags flags)
1318 headerTagTagFunction tagfunc = intGetTdEntry;
1320 if (td == NULL) return 0;
1325 if (flags & HEADERGET_EXT) {
1326 headerTagTagFunction extfunc = rpmHeaderTagFunc(tag);
1327 if (extfunc) tagfunc = extfunc;
1329 rc = tagfunc(h, td, flags);
1331 assert(tag == td->tag);
1337 static void copyData(rpm_tagtype_t type, rpm_data_t dstPtr,
1338 rpm_constdata_t srcPtr, rpm_count_t cnt, int dataLength)
1341 case RPM_STRING_ARRAY_TYPE:
1342 case RPM_I18NSTRING_TYPE:
1343 { const char ** av = (const char **) srcPtr;
1346 while (cnt-- > 0 && dataLength > 0) {
1348 if ((s = *av++) == NULL)
1352 } while (s[-1] && --dataLength > 0);
1357 memmove(dstPtr, srcPtr, dataLength);
1363 * Return (malloc'ed) copy of entry data.
1364 * @param type entry data type
1365 * @param p entry data
1366 * @param c entry item count
1367 * @retval lengthPtr no. bytes in returned data
1368 * @return (malloc'ed) copy of entry data, NULL on error
1371 grabData(rpm_tagtype_t type, rpm_constdata_t p, rpm_count_t c, int * lengthPtr)
1373 rpm_data_t data = NULL;
1376 length = dataLength(type, p, c, 0, NULL);
1378 data = xmalloc(length);
1379 copyData(type, data, p, c, length);
1383 *lengthPtr = length;
1387 static int intAddEntry(Header h, rpmtd td)
1393 /* Count must always be >= 1 for headerAddEntry. */
1397 if (hdrchkType(td->type))
1399 if (hdrchkData(td->count))
1403 data = grabData(td->type, td->data, td->count, &length);
1404 if (data == NULL || length <= 0)
1407 /* Allocate more index space if necessary */
1408 if (h->indexUsed == h->indexAlloced) {
1409 h->indexAlloced += INDEX_MALLOC_SIZE;
1410 h->index = xrealloc(h->index, h->indexAlloced * sizeof(*h->index));
1413 /* Fill in the index */
1414 entry = h->index + h->indexUsed;
1415 entry->info.tag = td->tag;
1416 entry->info.type = td->type;
1417 entry->info.count = td->count;
1418 entry->info.offset = 0;
1420 entry->length = length;
1422 if (h->indexUsed > 0 && td->tag < h->index[h->indexUsed-1].info.tag)
1423 h->flags &= ~HEADERFLAG_SORTED;
1429 static int intAppendEntry(Header h, rpmtd td)
1434 if (td->type == RPM_STRING_TYPE || td->type == RPM_I18NSTRING_TYPE) {
1435 /* we can't do this */
1439 /* Find the tag entry in the header. */
1440 entry = findEntry(h, td->tag, td->type);
1444 length = dataLength(td->type, td->data, td->count, 0, NULL);
1448 if (ENTRY_IN_REGION(entry)) {
1449 char * t = xmalloc(entry->length + length);
1450 memcpy(t, entry->data, entry->length);
1452 entry->info.offset = 0;
1454 entry->data = xrealloc(entry->data, entry->length + length);
1456 copyData(td->type, ((char *) entry->data) + entry->length,
1457 td->data, td->count, length);
1459 entry->length += length;
1461 entry->info.count += td->count;
1466 int headerPut(Header h, rpmtd td, headerPutFlags flags)
1471 if (flags & HEADERPUT_APPEND) {
1472 rc = findEntry(h, td->tag, td->type) ?
1473 intAppendEntry(h, td) :
1476 rc = intAddEntry(h, td);
1481 int headerAddI18NString(Header h, rpmTagVal tag, const char * string,
1484 indexEntry table, entry;
1485 const char ** strArray;
1488 rpm_count_t i, langNum;
1491 table = findEntry(h, RPMTAG_HEADERI18NTABLE, RPM_STRING_ARRAY_TYPE);
1492 entry = findEntry(h, tag, RPM_I18NSTRING_TYPE);
1494 if (!table && entry)
1495 return 0; /* this shouldn't ever happen!! */
1497 if (!table && !entry) {
1498 const char * charArray[2];
1499 rpm_count_t count = 0;
1501 if (!lang || (lang[0] == 'C' && lang[1] == '\0')) {
1502 charArray[count++] = "C";
1504 charArray[count++] = "C";
1505 charArray[count++] = lang;
1509 td.tag = RPMTAG_HEADERI18NTABLE;
1510 td.type = RPM_STRING_ARRAY_TYPE;
1511 td.data = (void *) charArray;
1513 if (!headerPut(h, &td, HEADERPUT_DEFAULT))
1515 table = findEntry(h, RPMTAG_HEADERI18NTABLE, RPM_STRING_ARRAY_TYPE);
1520 if (!lang) lang = "C";
1522 { const char * l = table->data;
1523 for (langNum = 0; langNum < table->info.count; langNum++) {
1524 if (rstreq(l, lang)) break;
1529 if (langNum >= table->info.count) {
1530 length = strlen(lang) + 1;
1531 if (ENTRY_IN_REGION(table)) {
1532 char * t = xmalloc(table->length + length);
1533 memcpy(t, table->data, table->length);
1535 table->info.offset = 0;
1537 table->data = xrealloc(table->data, table->length + length);
1538 memmove(((char *)table->data) + table->length, lang, length);
1539 table->length += length;
1540 table->info.count++;
1546 strArray = xmalloc(sizeof(*strArray) * (langNum + 1));
1547 for (i = 0; i < langNum; i++)
1549 strArray[langNum] = string;
1553 td.type = RPM_I18NSTRING_TYPE;
1555 td.count = langNum + 1;
1556 rc = headerPut(h, &td, HEADERPUT_DEFAULT);
1559 } else if (langNum >= entry->info.count) {
1560 ghosts = langNum - entry->info.count;
1562 length = strlen(string) + 1 + ghosts;
1563 if (ENTRY_IN_REGION(entry)) {
1564 char * t = xmalloc(entry->length + length);
1565 memcpy(t, entry->data, entry->length);
1567 entry->info.offset = 0;
1569 entry->data = xrealloc(entry->data, entry->length + length);
1571 memset(((char *)entry->data) + entry->length, '\0', ghosts);
1572 memmove(((char *)entry->data) + entry->length + ghosts, string, strlen(string)+1);
1574 entry->length += length;
1575 entry->info.count = langNum + 1;
1577 char *b, *be, *e, *ee, *t;
1580 /* Set beginning/end pointers to previous data */
1581 b = be = e = ee = entry->data;
1582 for (i = 0; i < table->info.count; i++) {
1585 ee += strlen(ee) + 1;
1590 /* Get storage for new buffer */
1592 sn = strlen(string) + 1;
1594 length = bn + sn + en;
1595 t = buf = xmalloc(length);
1597 /* Copy values into new storage */
1600 memcpy(t, string, sn);
1605 /* Replace i18N string array */
1606 entry->length -= strlen(be) + 1;
1607 entry->length += sn;
1609 if (ENTRY_IN_REGION(entry)) {
1610 entry->info.offset = 0;
1612 entry->data = _free(entry->data);
1619 int headerMod(Header h, rpmtd td)
1626 /* First find the tag */
1627 entry = findEntry(h, td->tag, td->type);
1632 data = grabData(td->type, td->data, td->count, &length);
1633 if (data == NULL || length <= 0)
1636 /* make sure entry points to the first occurence of this tag */
1637 while (entry > h->index && (entry - 1)->info.tag == td->tag)
1640 /* free after we've grabbed the new data in case the two are intertwined;
1641 that's a bad idea but at least we won't break */
1642 oldData = entry->data;
1644 entry->info.count = td->count;
1645 entry->info.type = td->type;
1647 entry->length = length;
1649 if (ENTRY_IN_REGION(entry)) {
1650 entry->info.offset = 0;
1658 * Header tag iterator data structure.
1660 struct headerIterator_s {
1661 Header h; /*!< Header being iterated. */
1662 int next_index; /*!< Next tag index. */
1665 HeaderIterator headerFreeIterator(HeaderIterator hi)
1668 hi->h = headerFree(hi->h);
1674 HeaderIterator headerInitIterator(Header h)
1676 HeaderIterator hi = xmalloc(sizeof(*hi));
1680 hi->h = headerLink(h);
1685 static indexEntry nextIndex(HeaderIterator hi)
1689 indexEntry entry = NULL;
1691 for (slot = hi->next_index; slot < h->indexUsed; slot++) {
1692 entry = h->index + slot;
1693 if (!ENTRY_IS_REGION(entry))
1696 hi->next_index = slot;
1697 if (entry == NULL || slot >= h->indexUsed)
1704 rpmTagVal headerNextTag(HeaderIterator hi)
1706 indexEntry entry = nextIndex(hi);
1707 return entry ? entry->info.tag : RPMTAG_NOT_FOUND;
1710 int headerNext(HeaderIterator hi, rpmtd td)
1712 indexEntry entry = nextIndex(hi);
1717 td->tag = entry->info.tag;
1718 rc = copyTdEntry(entry, td, HEADERGET_DEFAULT);
1720 return ((rc == 1) ? 1 : 0);
1723 unsigned int headerGetInstance(Header h)
1725 return h ? h->instance : 0;
1728 void headerSetInstance(Header h, unsigned int instance)
1730 h->instance = instance;