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 * @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 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;
783 /* Sanity checks on header intro. */
784 if (bsize && bsize != pvlen)
786 if (hdrchkTags(il) || hdrchkData(dl) || pvlen >= headerMaxbytes)
789 h = headerCreate(blob, (flags & HEADERIMPORT_COPY) ? pvlen : 0, il);
791 ei = h->blob; /* In case we had to copy */
792 pe = (entryInfo) &ei[2];
793 dataStart = (unsigned char *) (pe + il);
794 dataEnd = dataStart + dl;
797 if (!(htonl(pe->tag) < RPMTAG_HEADERI18NTABLE)) {
798 h->flags |= HEADERFLAG_LEGACY;
799 entry->info.type = REGION_TAG_TYPE;
800 entry->info.tag = RPMTAG_HEADERIMAGE;
801 entry->info.count = REGION_TAG_COUNT;
802 entry->info.offset = ((unsigned char *)pe - dataStart); /* negative offset */
805 entry->length = pvlen - sizeof(il) - sizeof(dl);
806 rdlen = regionSwab(entry+1, il, 0, pe, dataStart, dataEnd, entry->info.offset);
809 entry->rdlen = rdlen;
815 h->flags &= ~HEADERFLAG_LEGACY;
817 entry->info.type = htonl(pe->type);
818 entry->info.count = htonl(pe->count);
820 if (hdrchkType(entry->info.type))
822 if (hdrchkTags(entry->info.count))
825 { int off = ntohl(pe->offset);
828 size_t nb = REGION_TAG_COUNT;
830 if (hdrchkRange(dl, (off + nb)))
832 /* XXX Hmm, why the copy? */
833 memcpy(&stei, dataStart + off, nb);
834 rdl = -ntohl(stei[2]); /* negative offset */
835 ril = rdl/sizeof(*pe);
836 if (hdrchkTags(ril) || hdrchkData(rdl))
838 entry->info.tag = htonl(pe->tag);
841 rdl = (ril * sizeof(struct entryInfo_s));
842 entry->info.tag = RPMTAG_HEADERIMAGE;
845 entry->info.offset = -rdl; /* negative offset */
848 entry->length = pvlen - sizeof(il) - sizeof(dl);
849 rdlen = regionSwab(entry+1, ril-1, 0, pe+1, dataStart, dataEnd, entry->info.offset);
852 entry->rdlen = rdlen;
854 if (ril < h->indexUsed) {
855 indexEntry newEntry = entry + ril;
856 int ne = (h->indexUsed - ril);
857 int rid = entry->info.offset+1;
859 /* Load dribble entries from region. */
860 rdlen = regionSwab(newEntry, ne, rdlen, pe+ril,
861 dataStart, dataEnd, rid);
865 { indexEntry firstEntry = newEntry;
866 int save = h->indexUsed;
869 /* Dribble entries replace duplicate region entries. */
871 for (j = 0; j < ne; j++, newEntry++) {
872 (void) headerDel(h, newEntry->info.tag);
873 if (newEntry->info.tag == RPMTAG_BASENAMES)
874 (void) headerDel(h, RPMTAG_OLDFILENAMES);
877 /* If any duplicate entries were replaced, move new entries down. */
878 if (h->indexUsed < (save - ne)) {
879 memmove(h->index + h->indexUsed, firstEntry,
880 (ne * sizeof(*entry)));
886 rdlen += REGION_TAG_COUNT;
892 h->flags &= ~HEADERFLAG_SORTED;
894 h->flags |= HEADERFLAG_ALLOCATED;
900 if (flags & HEADERIMPORT_COPY)
908 Header headerReload(Header h, rpmTagVal tag)
911 void * uh = doHeaderUnload(h, NULL);
921 if (ENTRY_IS_REGION(nh->index)) {
922 if (tag == RPMTAG_HEADERSIGNATURES || tag == RPMTAG_HEADERIMMUTABLE)
923 nh->index[0].info.tag = tag;
928 Header headerLoad(void * uh)
930 return headerImport(uh, 0, 0);
933 Header headerCopyLoad(const void * uh)
935 /* Discards const but that's ok as we'll take a copy */
936 return headerImport((void *)uh, 0, HEADERIMPORT_COPY);
939 Header headerRead(FD_t fd, int magicp)
948 if (magicp == HEADER_MAGIC_YES) {
951 if (Fread(block, 1, 4*sizeof(*block), fd) != 4*sizeof(*block))
956 if (memcmp(&magic, rpm_header_magic, sizeof(magic)))
959 il = ntohl(block[2]);
960 dl = ntohl(block[3]);
962 if (Fread(block, 1, 2*sizeof(*block), fd) != 2*sizeof(*block))
965 il = ntohl(block[0]);
966 dl = ntohl(block[1]);
969 len = sizeof(il) + sizeof(dl) + (il * sizeof(struct entryInfo_s)) + dl;
971 /* Sanity checks on header intro. */
972 if (hdrchkTags(il) || hdrchkData(dl) || len > headerMaxbytes)
978 len -= sizeof(il) + sizeof(dl);
980 if (Fread((char *)&ei[2], 1, len, fd) != len)
986 if (h == NULL && ei != NULL) {
992 int headerWrite(FD_t fd, Header h, int magicp)
998 uh = doHeaderUnload(h, &length);
1002 case HEADER_MAGIC_YES:
1003 nb = Fwrite(rpm_header_magic, sizeof(uint8_t), sizeof(rpm_header_magic), fd);
1004 if (nb != sizeof(rpm_header_magic))
1007 case HEADER_MAGIC_NO:
1011 nb = Fwrite(uh, sizeof(char), length, fd);
1015 return (nb == length ? 0 : 1);
1018 int headerIsEntry(Header h, rpmTagVal tag)
1020 /* FIX: h modified by sort. */
1021 return (findEntry(h, tag, RPM_NULL_TYPE) ? 1 : 0);
1026 * Retrieve data from header entry.
1027 * Relevant flags (others are ignored), if neither is set allocation
1028 * behavior depends on data type(!)
1029 * HEADERGET_MINMEM: return pointers to header memory
1030 * HEADERGET_ALLOC: always return malloced memory, overrides MINMEM
1032 * @todo Permit retrieval of regions other than HEADER_IMUTABLE.
1033 * @param entry header entry
1034 * @param td tag data container
1035 * @param minMem string pointers refer to header memory?
1036 * @param flags flags to control memory allocation
1037 * @return 1 on success, otherwise error.
1039 static int copyTdEntry(const indexEntry entry, rpmtd td, headerGetFlags flags)
1041 rpm_count_t count = entry->info.count;
1042 int rc = 1; /* XXX 1 on success. */
1043 /* ALLOC overrides MINMEM */
1044 int allocMem = flags & HEADERGET_ALLOC;
1045 int minMem = allocMem ? 0 : flags & HEADERGET_MINMEM;
1046 int argvArray = (flags & HEADERGET_ARGV) ? 1 : 0;
1049 td->flags = RPMTD_IMMUTABLE;
1050 switch (entry->info.type) {
1053 * XXX This only works for
1054 * XXX "sealed" HEADER_IMMUTABLE/HEADER_SIGNATURES/HEADER_IMAGE.
1055 * XXX This will *not* work for unsealed legacy HEADER_IMAGE (i.e.
1056 * XXX a legacy header freshly read, but not yet unloaded to the rpmdb).
1058 if (ENTRY_IS_REGION(entry)) {
1059 int32_t * ei = ((int32_t *)entry->data) - 2;
1060 entryInfo pe = (entryInfo) (ei + 2);
1061 unsigned char * dataStart = (unsigned char *) (pe + ntohl(ei[0]));
1062 int32_t rdl = -entry->info.offset; /* negative offset */
1063 int32_t ril = rdl/sizeof(*pe);
1066 count = 2 * sizeof(*ei) + (ril * sizeof(*pe)) + rdl;
1067 if (entry->info.tag == RPMTAG_HEADERIMAGE) {
1071 count += REGION_TAG_COUNT;
1072 rdl += REGION_TAG_COUNT;
1075 td->data = xmalloc(count);
1076 ei = (int32_t *) td->data;
1080 pe = (entryInfo) memcpy(ei + 2, pe, (ril * sizeof(*pe)));
1082 dataStart = (unsigned char *) memcpy(pe + ril, dataStart, rdl);
1084 rc = regionSwab(NULL, ril, 0, pe, dataStart, dataStart + rdl, 0);
1085 /* don't return data on failure */
1087 td->data = _free(td->data);
1089 /* XXX 1 on success. */
1090 rc = (rc < 0) ? 0 : 1;
1092 count = entry->length;
1094 ? memcpy(xmalloc(count), entry->data, count)
1098 case RPM_STRING_TYPE:
1099 /* simple string, but fallthrough if its actually an array */
1100 if (count == 1 && !argvArray) {
1101 td->data = allocMem ? xstrdup(entry->data) : entry->data;
1104 case RPM_STRING_ARRAY_TYPE:
1105 case RPM_I18NSTRING_TYPE:
1106 { const char ** ptrEntry;
1107 int tableSize = (count + argvArray) * sizeof(char *);
1112 td->data = xmalloc(tableSize);
1113 ptrEntry = (const char **) td->data;
1116 t = xmalloc(tableSize + entry->length);
1117 td->data = (void *)t;
1118 ptrEntry = (const char **) td->data;
1120 memcpy(t, entry->data, entry->length);
1122 for (i = 0; i < count; i++) {
1129 td->flags |= RPMTD_ARGV;
1134 case RPM_INT16_TYPE:
1135 case RPM_INT32_TYPE:
1136 case RPM_INT64_TYPE:
1138 td->data = xmalloc(entry->length);
1139 memcpy(td->data, entry->data, entry->length);
1141 td->data = entry->data;
1145 /* WTH? Don't mess with unknown data types... */
1150 td->type = entry->info.type;
1153 if (td->data && entry->data != td->data) {
1154 td->flags |= RPMTD_ALLOCED;
1161 * Does locale match entry in header i18n table?
1164 * The range [l,le) contains the next locale to match:
1165 * ll[_CC][.EEEEE][@dddd]
1167 * ll ISO language code (in lowercase).
1168 * CC (optional) ISO coutnry code (in uppercase).
1169 * EEEEE (optional) encoding (not really standardized).
1170 * dddd (optional) dialect.
1173 * @param td header i18n table data, NUL terminated
1174 * @param l start of locale to match
1175 * @param le end of locale to match
1176 * @return 1 on good match, 2 on weak match, 0 on no match
1178 static int headerMatchLocale(const char *td, const char *l, const char *le)
1182 /* First try a complete match. */
1183 if (strlen(td) == (le-l) && rstreqn(td, l, (le - l)))
1186 /* Next, try stripping optional dialect and matching. */
1187 for (fe = l; fe < le && *fe != '@'; fe++)
1189 if (fe < le && rstreqn(td, l, (fe - l)))
1192 /* Next, try stripping optional codeset and matching. */
1193 for (fe = l; fe < le && *fe != '.'; fe++)
1195 if (fe < le && rstreqn(td, l, (fe - l)))
1198 /* Finally, try stripping optional country code and matching. */
1199 for (fe = l; fe < le && *fe != '_'; fe++)
1201 if (fe < le && rstreqn(td, l, (fe - l)))
1208 * Return i18n string from header that matches locale.
1210 * @param entry i18n string data
1211 * @retval td tag data container
1212 * @param flags flags to control allocation
1215 static int copyI18NEntry(Header h, indexEntry entry, rpmtd td,
1216 headerGetFlags flags)
1218 const char *lang, *l, *le;
1221 td->type = RPM_STRING_TYPE;
1223 /* if no match, just return the first string */
1224 td->data = entry->data;
1226 /* XXX Drepper sez' this is the order. */
1227 if ((lang = getenv("LANGUAGE")) == NULL &&
1228 (lang = getenv("LC_ALL")) == NULL &&
1229 (lang = getenv("LC_MESSAGES")) == NULL &&
1230 (lang = getenv("LANG")) == NULL)
1233 if ((table = findEntry(h, RPMTAG_HEADERI18NTABLE, RPM_STRING_ARRAY_TYPE)) == NULL)
1236 for (l = lang; *l != '\0'; l = le) {
1238 char *ed, *ed_weak = NULL;
1241 while (*l && *l == ':') /* skip leading colons */
1245 for (le = l; *le && *le != ':'; le++) /* find end of this locale */
1248 /* For each entry in the header ... */
1249 for (langNum = 0, t = table->data, ed = entry->data;
1250 langNum < entry->info.count;
1251 langNum++, t += strlen(t) + 1, ed += strlen(ed) + 1) {
1253 int match = headerMatchLocale(t, l, le);
1257 } else if (match == 2) {
1268 if (flags & HEADERGET_ALLOC) {
1269 td->data = xstrdup(td->data);
1270 td->flags |= RPMTD_ALLOCED;
1277 * Retrieve tag data from header.
1279 * @retval td tag data container
1280 * @param flags flags to control retrieval
1281 * @return 1 on success, 0 on not found
1283 static int intGetTdEntry(Header h, rpmtd td, headerGetFlags flags)
1288 /* First find the tag */
1289 /* FIX: h modified by sort. */
1290 entry = findEntry(h, td->tag, RPM_NULL_TYPE);
1291 if (entry == NULL) {
1292 /* Td is zeroed above, just return... */
1296 if (flags & HEADERGET_RAW) {
1297 rc = copyTdEntry(entry, td, flags);
1299 switch (entry->info.type) {
1300 case RPM_I18NSTRING_TYPE:
1301 rc = copyI18NEntry(h, entry, td, flags);
1304 rc = copyTdEntry(entry, td, flags);
1309 /* XXX 1 on success */
1310 return ((rc == 1) ? 1 : 0);
1313 int headerGet(Header h, rpmTagVal tag, rpmtd td, headerGetFlags flags)
1316 headerTagTagFunction tagfunc = intGetTdEntry;
1318 if (td == NULL) return 0;
1323 if (flags & HEADERGET_EXT) {
1324 headerTagTagFunction extfunc = rpmHeaderTagFunc(tag);
1325 if (extfunc) tagfunc = extfunc;
1327 rc = tagfunc(h, td, flags);
1329 assert(tag == td->tag);
1335 static void copyData(rpm_tagtype_t type, rpm_data_t dstPtr,
1336 rpm_constdata_t srcPtr, rpm_count_t cnt, int dataLength)
1339 case RPM_STRING_ARRAY_TYPE:
1340 case RPM_I18NSTRING_TYPE:
1341 { const char ** av = (const char **) srcPtr;
1344 while (cnt-- > 0 && dataLength > 0) {
1346 if ((s = *av++) == NULL)
1350 } while (s[-1] && --dataLength > 0);
1355 memmove(dstPtr, srcPtr, dataLength);
1361 * Return (malloc'ed) copy of entry data.
1362 * @param type entry data type
1363 * @param p entry data
1364 * @param c entry item count
1365 * @retval lengthPtr no. bytes in returned data
1366 * @return (malloc'ed) copy of entry data, NULL on error
1369 grabData(rpm_tagtype_t type, rpm_constdata_t p, rpm_count_t c, int * lengthPtr)
1371 rpm_data_t data = NULL;
1374 length = dataLength(type, p, c, 0, NULL);
1376 data = xmalloc(length);
1377 copyData(type, data, p, c, length);
1381 *lengthPtr = length;
1385 static int intAddEntry(Header h, rpmtd td)
1391 /* Count must always be >= 1 for headerAddEntry. */
1395 if (hdrchkType(td->type))
1397 if (hdrchkData(td->count))
1401 data = grabData(td->type, td->data, td->count, &length);
1402 if (data == NULL || length <= 0)
1405 /* Allocate more index space if necessary */
1406 if (h->indexUsed == h->indexAlloced) {
1407 h->indexAlloced += INDEX_MALLOC_SIZE;
1408 h->index = xrealloc(h->index, h->indexAlloced * sizeof(*h->index));
1411 /* Fill in the index */
1412 entry = h->index + h->indexUsed;
1413 entry->info.tag = td->tag;
1414 entry->info.type = td->type;
1415 entry->info.count = td->count;
1416 entry->info.offset = 0;
1418 entry->length = length;
1420 if (h->indexUsed > 0 && td->tag < h->index[h->indexUsed-1].info.tag)
1421 h->flags &= ~HEADERFLAG_SORTED;
1427 static int intAppendEntry(Header h, rpmtd td)
1432 if (td->type == RPM_STRING_TYPE || td->type == RPM_I18NSTRING_TYPE) {
1433 /* we can't do this */
1437 /* Find the tag entry in the header. */
1438 entry = findEntry(h, td->tag, td->type);
1442 length = dataLength(td->type, td->data, td->count, 0, NULL);
1446 if (ENTRY_IN_REGION(entry)) {
1447 char * t = xmalloc(entry->length + length);
1448 memcpy(t, entry->data, entry->length);
1450 entry->info.offset = 0;
1452 entry->data = xrealloc(entry->data, entry->length + length);
1454 copyData(td->type, ((char *) entry->data) + entry->length,
1455 td->data, td->count, length);
1457 entry->length += length;
1459 entry->info.count += td->count;
1464 int headerPut(Header h, rpmtd td, headerPutFlags flags)
1469 if (flags & HEADERPUT_APPEND) {
1470 rc = findEntry(h, td->tag, td->type) ?
1471 intAppendEntry(h, td) :
1474 rc = intAddEntry(h, td);
1479 int headerAddI18NString(Header h, rpmTagVal tag, const char * string,
1482 indexEntry table, entry;
1483 const char ** strArray;
1486 rpm_count_t i, langNum;
1489 table = findEntry(h, RPMTAG_HEADERI18NTABLE, RPM_STRING_ARRAY_TYPE);
1490 entry = findEntry(h, tag, RPM_I18NSTRING_TYPE);
1492 if (!table && entry)
1493 return 0; /* this shouldn't ever happen!! */
1495 if (!table && !entry) {
1496 const char * charArray[2];
1497 rpm_count_t count = 0;
1499 if (!lang || (lang[0] == 'C' && lang[1] == '\0')) {
1500 charArray[count++] = "C";
1502 charArray[count++] = "C";
1503 charArray[count++] = lang;
1507 td.tag = RPMTAG_HEADERI18NTABLE;
1508 td.type = RPM_STRING_ARRAY_TYPE;
1509 td.data = (void *) charArray;
1511 if (!headerPut(h, &td, HEADERPUT_DEFAULT))
1513 table = findEntry(h, RPMTAG_HEADERI18NTABLE, RPM_STRING_ARRAY_TYPE);
1518 if (!lang) lang = "C";
1520 { const char * l = table->data;
1521 for (langNum = 0; langNum < table->info.count; langNum++) {
1522 if (rstreq(l, lang)) break;
1527 if (langNum >= table->info.count) {
1528 length = strlen(lang) + 1;
1529 if (ENTRY_IN_REGION(table)) {
1530 char * t = xmalloc(table->length + length);
1531 memcpy(t, table->data, table->length);
1533 table->info.offset = 0;
1535 table->data = xrealloc(table->data, table->length + length);
1536 memmove(((char *)table->data) + table->length, lang, length);
1537 table->length += length;
1538 table->info.count++;
1544 strArray = xmalloc(sizeof(*strArray) * (langNum + 1));
1545 for (i = 0; i < langNum; i++)
1547 strArray[langNum] = string;
1551 td.type = RPM_I18NSTRING_TYPE;
1553 td.count = langNum + 1;
1554 rc = headerPut(h, &td, HEADERPUT_DEFAULT);
1557 } else if (langNum >= entry->info.count) {
1558 ghosts = langNum - entry->info.count;
1560 length = strlen(string) + 1 + ghosts;
1561 if (ENTRY_IN_REGION(entry)) {
1562 char * t = xmalloc(entry->length + length);
1563 memcpy(t, entry->data, entry->length);
1565 entry->info.offset = 0;
1567 entry->data = xrealloc(entry->data, entry->length + length);
1569 memset(((char *)entry->data) + entry->length, '\0', ghosts);
1570 memmove(((char *)entry->data) + entry->length + ghosts, string, strlen(string)+1);
1572 entry->length += length;
1573 entry->info.count = langNum + 1;
1575 char *b, *be, *e, *ee, *t;
1578 /* Set beginning/end pointers to previous data */
1579 b = be = e = ee = entry->data;
1580 for (i = 0; i < table->info.count; i++) {
1583 ee += strlen(ee) + 1;
1588 /* Get storage for new buffer */
1590 sn = strlen(string) + 1;
1592 length = bn + sn + en;
1593 t = buf = xmalloc(length);
1595 /* Copy values into new storage */
1598 memcpy(t, string, sn);
1603 /* Replace i18N string array */
1604 entry->length -= strlen(be) + 1;
1605 entry->length += sn;
1607 if (ENTRY_IN_REGION(entry)) {
1608 entry->info.offset = 0;
1610 entry->data = _free(entry->data);
1617 int headerMod(Header h, rpmtd td)
1624 /* First find the tag */
1625 entry = findEntry(h, td->tag, td->type);
1630 data = grabData(td->type, td->data, td->count, &length);
1631 if (data == NULL || length <= 0)
1634 /* make sure entry points to the first occurence of this tag */
1635 while (entry > h->index && (entry - 1)->info.tag == td->tag)
1638 /* free after we've grabbed the new data in case the two are intertwined;
1639 that's a bad idea but at least we won't break */
1640 oldData = entry->data;
1642 entry->info.count = td->count;
1643 entry->info.type = td->type;
1645 entry->length = length;
1647 if (ENTRY_IN_REGION(entry)) {
1648 entry->info.offset = 0;
1656 * Header tag iterator data structure.
1658 struct headerIterator_s {
1659 Header h; /*!< Header being iterated. */
1660 int next_index; /*!< Next tag index. */
1663 HeaderIterator headerFreeIterator(HeaderIterator hi)
1666 hi->h = headerFree(hi->h);
1672 HeaderIterator headerInitIterator(Header h)
1674 HeaderIterator hi = xmalloc(sizeof(*hi));
1678 hi->h = headerLink(h);
1683 static indexEntry nextIndex(HeaderIterator hi)
1687 indexEntry entry = NULL;
1689 for (slot = hi->next_index; slot < h->indexUsed; slot++) {
1690 entry = h->index + slot;
1691 if (!ENTRY_IS_REGION(entry))
1694 hi->next_index = slot;
1695 if (entry == NULL || slot >= h->indexUsed)
1702 rpmTagVal headerNextTag(HeaderIterator hi)
1704 indexEntry entry = nextIndex(hi);
1705 return entry ? entry->info.tag : RPMTAG_NOT_FOUND;
1708 int headerNext(HeaderIterator hi, rpmtd td)
1710 indexEntry entry = nextIndex(hi);
1715 td->tag = entry->info.tag;
1716 rc = copyTdEntry(entry, td, HEADERGET_DEFAULT);
1718 return ((rc == 1) ? 1 : 0);
1721 unsigned int headerGetInstance(Header h)
1723 return h ? h->instance : 0;
1726 void headerSetInstance(Header h, unsigned int instance)
1728 h->instance = instance;