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)
464 void * headerExport(Header h, unsigned int *bsize)
475 int drlen, ndribbles;
477 if (h == NULL) return NULL;
479 /* Sort entries by (offset,tag). */
482 /* Compute (il,dl) for all tags, including those deleted in region. */
483 drlen = ndribbles = 0;
484 for (i = 0, entry = h->index; i < h->indexUsed; i++, entry++) {
485 if (ENTRY_IS_REGION(entry)) {
486 int32_t rdl = -entry->info.offset; /* negative offset */
487 int32_t ril = rdl/sizeof(*pe);
488 int rid = entry->info.offset;
491 dl += entry->rdlen + entry->info.count;
492 /* XXX Legacy regions do not include the region tag and data. */
493 if (i == 0 && (h->flags & HEADERFLAG_LEGACY))
496 /* Skip rest of entries in region, but account for dribbles. */
497 for (; i < h->indexUsed && entry->info.offset <= rid+1; i++, entry++) {
498 if (entry->info.offset <= rid)
502 diff = alignDiff(entry->info.type, dl);
510 drlen += entry->length;
518 /* Ignore deleted drips. */
519 if (entry->data == NULL || entry->length <= 0)
523 dl += alignDiff(entry->info.type, dl);
529 /* Sanity checks on header intro. */
530 if (hdrchkTags(il) || hdrchkData(dl))
533 len = sizeof(il) + sizeof(dl) + (il * sizeof(*pe)) + dl;
539 pe = (entryInfo) &ei[2];
540 dataStart = te = (char *) (pe + il);
542 for (i = 0, entry = h->index; i < h->indexUsed; i++, entry++) {
549 if (entry->data == NULL || entry->length <= 0)
552 t = (unsigned char*)te;
553 pe->tag = htonl(entry->info.tag);
554 pe->type = htonl(entry->info.type);
555 pe->count = htonl(entry->info.count);
557 if (ENTRY_IS_REGION(entry)) {
558 int32_t rdl = -entry->info.offset; /* negative offset */
559 int32_t ril = rdl/sizeof(*pe) + ndribbles;
560 int rid = entry->info.offset;
562 src = (char *)entry->data;
563 rdlen = entry->rdlen;
565 /* XXX Legacy regions do not include the region tag and data. */
566 if (i == 0 && (h->flags & HEADERFLAG_LEGACY)) {
569 memcpy(pe+1, src, rdl);
570 memcpy(te, src + rdl, rdlen);
573 pe->offset = htonl(te - dataStart);
576 stei[2] = htonl(-rdl-entry->info.count);
578 memcpy(te, stei, entry->info.count);
579 te += entry->info.count;
581 rdlen += entry->info.count;
583 count = regionSwab(NULL, ril, 0, pe, t, NULL, 0);
589 memcpy(pe+1, src + sizeof(*pe), ((ril-1) * sizeof(*pe)));
590 memcpy(te, src + (ril * sizeof(*pe)), rdlen+entry->info.count+drlen);
593 entryInfo se = (entryInfo)src;
594 int off = ntohl(se->offset);
595 pe->offset = (off) ? htonl(te - dataStart) : htonl(off);
597 te += entry->info.count + drlen;
599 count = regionSwab(NULL, ril, 0, pe, t, NULL, 0);
600 if (count != (rdlen + entry->info.count + drlen))
604 /* Skip rest of entries in region. */
605 while (i < h->indexUsed && entry->info.offset <= rid+1) {
615 /* Ignore deleted drips. */
616 if (entry->data == NULL || entry->length <= 0)
620 diff = alignDiff(entry->info.type, (te - dataStart));
626 pe->offset = htonl(te - dataStart);
628 /* copy data w/ endian conversions */
629 switch (entry->info.type) {
631 count = entry->info.count;
634 *((uint64_t *)te) = htonll(*((uint64_t *)src));
635 te += sizeof(uint64_t);
636 src += sizeof(uint64_t);
641 count = entry->info.count;
644 *((int32_t *)te) = htonl(*((int32_t *)src));
645 te += sizeof(int32_t);
646 src += sizeof(int32_t);
651 count = entry->info.count;
654 *((int16_t *)te) = htons(*((int16_t *)src));
655 te += sizeof(int16_t);
656 src += sizeof(int16_t);
661 memcpy(te, entry->data, entry->length);
668 /* Insure that there are no memcpy underruns/overruns. */
669 if (((char *)pe) != dataStart)
671 if ((((char *)ei)+len) != te)
686 void * headerUnload(Header h)
688 return headerExport(h, NULL);
692 * Find matching (tag,type) entry in header.
694 * @param tag entry tag
695 * @param type entry type
696 * @return header entry
699 indexEntry findEntry(Header h, rpmTagVal tag, rpm_tagtype_t type)
702 struct indexEntry_s key;
704 if (h == NULL) return NULL;
705 if (!(h->flags & HEADERFLAG_SORTED)) headerSort(h);
709 entry = bsearch(&key, h->index, h->indexUsed, sizeof(*h->index), indexCmp);
713 if (type == RPM_NULL_TYPE)
717 while (entry->info.tag == tag && entry->info.type != type &&
718 entry > h->index) entry--;
720 if (entry->info.tag == tag && entry->info.type == type)
726 int headerDel(Header h, rpmTagVal tag)
728 indexEntry last = h->index + h->indexUsed;
729 indexEntry entry, first;
732 entry = findEntry(h, tag, RPM_NULL_TYPE);
733 if (!entry) return 1;
735 /* Make sure entry points to the first occurence of this tag. */
736 while (entry > h->index && (entry - 1)->info.tag == tag)
739 /* Free data for tags being removed. */
740 for (first = entry; first < last; first++) {
742 if (first->info.tag != tag)
747 if (ENTRY_IN_REGION(first))
752 ne = (first - entry);
757 memmove(entry, first, (ne * sizeof(*entry)));
763 Header headerImport(void * blob, unsigned int bsize, headerImportFlags flags)
765 const int32_t * ei = (int32_t *) blob;
766 int32_t il = ntohl(ei[0]); /* index length */
767 int32_t dl = ntohl(ei[1]); /* data length */
768 unsigned int pvlen = sizeof(il) + sizeof(dl) +
769 (il * sizeof(struct entryInfo_s)) + dl;;
772 unsigned char * dataStart;
773 unsigned char * dataEnd;
777 /* Sanity checks on header intro. */
778 if (bsize && bsize != pvlen)
780 if (hdrchkTags(il) || hdrchkData(dl) || pvlen >= headerMaxbytes)
783 h = headerCreate(blob, (flags & HEADERIMPORT_COPY) ? pvlen : 0, il);
785 ei = h->blob; /* In case we had to copy */
786 pe = (entryInfo) &ei[2];
787 dataStart = (unsigned char *) (pe + il);
788 dataEnd = dataStart + dl;
791 if (!(htonl(pe->tag) < RPMTAG_HEADERI18NTABLE)) {
792 h->flags |= HEADERFLAG_LEGACY;
793 entry->info.type = REGION_TAG_TYPE;
794 entry->info.tag = RPMTAG_HEADERIMAGE;
795 entry->info.count = REGION_TAG_COUNT;
796 entry->info.offset = ((unsigned char *)pe - dataStart); /* negative offset */
799 entry->length = pvlen - sizeof(il) - sizeof(dl);
800 rdlen = regionSwab(entry+1, il, 0, pe, dataStart, dataEnd, entry->info.offset);
803 entry->rdlen = rdlen;
809 h->flags &= ~HEADERFLAG_LEGACY;
811 entry->info.type = htonl(pe->type);
812 entry->info.count = htonl(pe->count);
814 if (hdrchkType(entry->info.type))
816 if (hdrchkTags(entry->info.count))
819 { int off = ntohl(pe->offset);
822 size_t nb = REGION_TAG_COUNT;
824 if (hdrchkRange(dl, (off + nb)))
826 /* XXX Hmm, why the copy? */
827 memcpy(&stei, dataStart + off, nb);
828 rdl = -ntohl(stei[2]); /* negative offset */
829 ril = rdl/sizeof(*pe);
830 if (hdrchkTags(ril) || hdrchkData(rdl))
832 entry->info.tag = htonl(pe->tag);
835 rdl = (ril * sizeof(struct entryInfo_s));
836 entry->info.tag = RPMTAG_HEADERIMAGE;
839 entry->info.offset = -rdl; /* negative offset */
842 entry->length = pvlen - sizeof(il) - sizeof(dl);
843 rdlen = regionSwab(entry+1, ril-1, 0, pe+1, dataStart, dataEnd, entry->info.offset);
846 entry->rdlen = rdlen;
848 if (ril < h->indexUsed) {
849 indexEntry newEntry = entry + ril;
850 int ne = (h->indexUsed - ril);
851 int rid = entry->info.offset+1;
853 /* Load dribble entries from region. */
854 rdlen = regionSwab(newEntry, ne, rdlen, pe+ril,
855 dataStart, dataEnd, rid);
859 { indexEntry firstEntry = newEntry;
860 int save = h->indexUsed;
863 /* Dribble entries replace duplicate region entries. */
865 for (j = 0; j < ne; j++, newEntry++) {
866 (void) headerDel(h, newEntry->info.tag);
867 if (newEntry->info.tag == RPMTAG_BASENAMES)
868 (void) headerDel(h, RPMTAG_OLDFILENAMES);
871 /* If any duplicate entries were replaced, move new entries down. */
872 if (h->indexUsed < (save - ne)) {
873 memmove(h->index + h->indexUsed, firstEntry,
874 (ne * sizeof(*entry)));
880 rdlen += REGION_TAG_COUNT;
886 h->flags &= ~HEADERFLAG_SORTED;
888 h->flags |= HEADERFLAG_ALLOCATED;
894 if (flags & HEADERIMPORT_COPY)
902 Header headerReload(Header h, rpmTagVal tag)
906 void * uh = headerExport(h, &uc);
911 nh = headerImport(uh, uc, 0);
916 if (ENTRY_IS_REGION(nh->index)) {
917 if (tag == RPMTAG_HEADERSIGNATURES || tag == RPMTAG_HEADERIMMUTABLE)
918 nh->index[0].info.tag = tag;
923 Header headerLoad(void * uh)
925 return headerImport(uh, 0, 0);
928 Header headerCopyLoad(const void * uh)
930 /* Discards const but that's ok as we'll take a copy */
931 return headerImport((void *)uh, 0, HEADERIMPORT_COPY);
934 Header headerRead(FD_t fd, int magicp)
941 unsigned int len, blen;
943 if (magicp == HEADER_MAGIC_YES) {
946 if (Fread(block, 1, 4*sizeof(*block), fd) != 4*sizeof(*block))
951 if (memcmp(&magic, rpm_header_magic, sizeof(magic)))
954 il = ntohl(block[2]);
955 dl = ntohl(block[3]);
957 if (Fread(block, 1, 2*sizeof(*block), fd) != 2*sizeof(*block))
960 il = ntohl(block[0]);
961 dl = ntohl(block[1]);
964 blen = (il * sizeof(struct entryInfo_s)) + dl;
965 len = sizeof(il) + sizeof(dl) + blen;
967 /* Sanity checks on header intro. */
968 if (hdrchkTags(il) || hdrchkData(dl) || len > headerMaxbytes)
975 if (Fread((char *)&ei[2], 1, blen, fd) != blen)
978 h = headerImport(ei, len, 0);
981 if (h == NULL && ei != NULL) {
987 int headerWrite(FD_t fd, Header h, int magicp)
993 uh = headerExport(h, &length);
997 case HEADER_MAGIC_YES:
998 nb = Fwrite(rpm_header_magic, sizeof(uint8_t), sizeof(rpm_header_magic), fd);
999 if (nb != sizeof(rpm_header_magic))
1002 case HEADER_MAGIC_NO:
1006 nb = Fwrite(uh, sizeof(char), length, fd);
1010 return (nb == length ? 0 : 1);
1013 int headerIsEntry(Header h, rpmTagVal tag)
1015 /* FIX: h modified by sort. */
1016 return (findEntry(h, tag, RPM_NULL_TYPE) ? 1 : 0);
1021 * Retrieve data from header entry.
1022 * Relevant flags (others are ignored), if neither is set allocation
1023 * behavior depends on data type(!)
1024 * HEADERGET_MINMEM: return pointers to header memory
1025 * HEADERGET_ALLOC: always return malloced memory, overrides MINMEM
1027 * @todo Permit retrieval of regions other than HEADER_IMUTABLE.
1028 * @param entry header entry
1029 * @param td tag data container
1030 * @param minMem string pointers refer to header memory?
1031 * @param flags flags to control memory allocation
1032 * @return 1 on success, otherwise error.
1034 static int copyTdEntry(const indexEntry entry, rpmtd td, headerGetFlags flags)
1036 rpm_count_t count = entry->info.count;
1037 int rc = 1; /* XXX 1 on success. */
1038 /* ALLOC overrides MINMEM */
1039 int allocMem = flags & HEADERGET_ALLOC;
1040 int minMem = allocMem ? 0 : flags & HEADERGET_MINMEM;
1041 int argvArray = (flags & HEADERGET_ARGV) ? 1 : 0;
1044 td->flags = RPMTD_IMMUTABLE;
1045 switch (entry->info.type) {
1048 * XXX This only works for
1049 * XXX "sealed" HEADER_IMMUTABLE/HEADER_SIGNATURES/HEADER_IMAGE.
1050 * XXX This will *not* work for unsealed legacy HEADER_IMAGE (i.e.
1051 * XXX a legacy header freshly read, but not yet unloaded to the rpmdb).
1053 if (ENTRY_IS_REGION(entry)) {
1054 int32_t * ei = ((int32_t *)entry->data) - 2;
1055 entryInfo pe = (entryInfo) (ei + 2);
1056 unsigned char * dataStart = (unsigned char *) (pe + ntohl(ei[0]));
1057 int32_t rdl = -entry->info.offset; /* negative offset */
1058 int32_t ril = rdl/sizeof(*pe);
1061 count = 2 * sizeof(*ei) + (ril * sizeof(*pe)) + rdl;
1062 if (entry->info.tag == RPMTAG_HEADERIMAGE) {
1066 count += REGION_TAG_COUNT;
1067 rdl += REGION_TAG_COUNT;
1070 td->data = xmalloc(count);
1071 ei = (int32_t *) td->data;
1075 pe = (entryInfo) memcpy(ei + 2, pe, (ril * sizeof(*pe)));
1077 dataStart = (unsigned char *) memcpy(pe + ril, dataStart, rdl);
1079 rc = regionSwab(NULL, ril, 0, pe, dataStart, dataStart + rdl, 0);
1080 /* don't return data on failure */
1082 td->data = _free(td->data);
1084 /* XXX 1 on success. */
1085 rc = (rc < 0) ? 0 : 1;
1087 count = entry->length;
1089 ? memcpy(xmalloc(count), entry->data, count)
1093 case RPM_STRING_TYPE:
1094 /* simple string, but fallthrough if its actually an array */
1095 if (count == 1 && !argvArray) {
1096 td->data = allocMem ? xstrdup(entry->data) : entry->data;
1099 case RPM_STRING_ARRAY_TYPE:
1100 case RPM_I18NSTRING_TYPE:
1101 { const char ** ptrEntry;
1102 int tableSize = (count + argvArray) * sizeof(char *);
1107 td->data = xmalloc(tableSize);
1108 ptrEntry = (const char **) td->data;
1111 t = xmalloc(tableSize + entry->length);
1112 td->data = (void *)t;
1113 ptrEntry = (const char **) td->data;
1115 memcpy(t, entry->data, entry->length);
1117 for (i = 0; i < count; i++) {
1124 td->flags |= RPMTD_ARGV;
1129 case RPM_INT16_TYPE:
1130 case RPM_INT32_TYPE:
1131 case RPM_INT64_TYPE:
1133 td->data = xmalloc(entry->length);
1134 memcpy(td->data, entry->data, entry->length);
1136 td->data = entry->data;
1140 /* WTH? Don't mess with unknown data types... */
1145 td->type = entry->info.type;
1148 if (td->data && entry->data != td->data) {
1149 td->flags |= RPMTD_ALLOCED;
1156 * Does locale match entry in header i18n table?
1159 * The range [l,le) contains the next locale to match:
1160 * ll[_CC][.EEEEE][@dddd]
1162 * ll ISO language code (in lowercase).
1163 * CC (optional) ISO coutnry code (in uppercase).
1164 * EEEEE (optional) encoding (not really standardized).
1165 * dddd (optional) dialect.
1168 * @param td header i18n table data, NUL terminated
1169 * @param l start of locale to match
1170 * @param le end of locale to match
1171 * @return 1 on good match, 2 on weak match, 0 on no match
1173 static int headerMatchLocale(const char *td, const char *l, const char *le)
1177 /* First try a complete match. */
1178 if (strlen(td) == (le-l) && rstreqn(td, l, (le - l)))
1181 /* Next, try stripping optional dialect and matching. */
1182 for (fe = l; fe < le && *fe != '@'; fe++)
1184 if (fe < le && rstreqn(td, l, (fe - l)))
1187 /* Next, try stripping optional codeset and matching. */
1188 for (fe = l; fe < le && *fe != '.'; fe++)
1190 if (fe < le && rstreqn(td, l, (fe - l)))
1193 /* Finally, try stripping optional country code and matching. */
1194 for (fe = l; fe < le && *fe != '_'; fe++)
1196 if (fe < le && rstreqn(td, l, (fe - l)))
1203 * Return i18n string from header that matches locale.
1205 * @param entry i18n string data
1206 * @retval td tag data container
1207 * @param flags flags to control allocation
1210 static int copyI18NEntry(Header h, indexEntry entry, rpmtd td,
1211 headerGetFlags flags)
1213 const char *lang, *l, *le;
1216 td->type = RPM_STRING_TYPE;
1218 /* if no match, just return the first string */
1219 td->data = entry->data;
1221 /* XXX Drepper sez' this is the order. */
1222 if ((lang = getenv("LANGUAGE")) == NULL &&
1223 (lang = getenv("LC_ALL")) == NULL &&
1224 (lang = getenv("LC_MESSAGES")) == NULL &&
1225 (lang = getenv("LANG")) == NULL)
1228 if ((table = findEntry(h, RPMTAG_HEADERI18NTABLE, RPM_STRING_ARRAY_TYPE)) == NULL)
1231 for (l = lang; *l != '\0'; l = le) {
1233 char *ed, *ed_weak = NULL;
1236 while (*l && *l == ':') /* skip leading colons */
1240 for (le = l; *le && *le != ':'; le++) /* find end of this locale */
1243 /* For each entry in the header ... */
1244 for (langNum = 0, t = table->data, ed = entry->data;
1245 langNum < entry->info.count;
1246 langNum++, t += strlen(t) + 1, ed += strlen(ed) + 1) {
1248 int match = headerMatchLocale(t, l, le);
1252 } else if (match == 2) {
1263 if (flags & HEADERGET_ALLOC) {
1264 td->data = xstrdup(td->data);
1265 td->flags |= RPMTD_ALLOCED;
1272 * Retrieve tag data from header.
1274 * @retval td tag data container
1275 * @param flags flags to control retrieval
1276 * @return 1 on success, 0 on not found
1278 static int intGetTdEntry(Header h, rpmtd td, headerGetFlags flags)
1283 /* First find the tag */
1284 /* FIX: h modified by sort. */
1285 entry = findEntry(h, td->tag, RPM_NULL_TYPE);
1286 if (entry == NULL) {
1287 /* Td is zeroed above, just return... */
1291 if (flags & HEADERGET_RAW) {
1292 rc = copyTdEntry(entry, td, flags);
1294 switch (entry->info.type) {
1295 case RPM_I18NSTRING_TYPE:
1296 rc = copyI18NEntry(h, entry, td, flags);
1299 rc = copyTdEntry(entry, td, flags);
1304 /* XXX 1 on success */
1305 return ((rc == 1) ? 1 : 0);
1308 int headerGet(Header h, rpmTagVal tag, rpmtd td, headerGetFlags flags)
1311 headerTagTagFunction tagfunc = intGetTdEntry;
1313 if (td == NULL) return 0;
1318 if (flags & HEADERGET_EXT) {
1319 headerTagTagFunction extfunc = rpmHeaderTagFunc(tag);
1320 if (extfunc) tagfunc = extfunc;
1322 rc = tagfunc(h, td, flags);
1324 assert(tag == td->tag);
1330 static void copyData(rpm_tagtype_t type, rpm_data_t dstPtr,
1331 rpm_constdata_t srcPtr, rpm_count_t cnt, int dataLength)
1334 case RPM_STRING_ARRAY_TYPE:
1335 case RPM_I18NSTRING_TYPE:
1336 { const char ** av = (const char **) srcPtr;
1339 while (cnt-- > 0 && dataLength > 0) {
1341 if ((s = *av++) == NULL)
1345 } while (s[-1] && --dataLength > 0);
1350 memmove(dstPtr, srcPtr, dataLength);
1356 * Return (malloc'ed) copy of entry data.
1357 * @param type entry data type
1358 * @param p entry data
1359 * @param c entry item count
1360 * @retval lengthPtr no. bytes in returned data
1361 * @return (malloc'ed) copy of entry data, NULL on error
1364 grabData(rpm_tagtype_t type, rpm_constdata_t p, rpm_count_t c, int * lengthPtr)
1366 rpm_data_t data = NULL;
1369 length = dataLength(type, p, c, 0, NULL);
1371 data = xmalloc(length);
1372 copyData(type, data, p, c, length);
1376 *lengthPtr = length;
1380 static int intAddEntry(Header h, rpmtd td)
1386 /* Count must always be >= 1 for headerAddEntry. */
1390 if (hdrchkType(td->type))
1392 if (hdrchkData(td->count))
1396 data = grabData(td->type, td->data, td->count, &length);
1397 if (data == NULL || length <= 0)
1400 /* Allocate more index space if necessary */
1401 if (h->indexUsed == h->indexAlloced) {
1402 h->indexAlloced += INDEX_MALLOC_SIZE;
1403 h->index = xrealloc(h->index, h->indexAlloced * sizeof(*h->index));
1406 /* Fill in the index */
1407 entry = h->index + h->indexUsed;
1408 entry->info.tag = td->tag;
1409 entry->info.type = td->type;
1410 entry->info.count = td->count;
1411 entry->info.offset = 0;
1413 entry->length = length;
1415 if (h->indexUsed > 0 && td->tag < h->index[h->indexUsed-1].info.tag)
1416 h->flags &= ~HEADERFLAG_SORTED;
1422 static int intAppendEntry(Header h, rpmtd td)
1427 if (td->type == RPM_STRING_TYPE || td->type == RPM_I18NSTRING_TYPE) {
1428 /* we can't do this */
1432 /* Find the tag entry in the header. */
1433 entry = findEntry(h, td->tag, td->type);
1437 length = dataLength(td->type, td->data, td->count, 0, NULL);
1441 if (ENTRY_IN_REGION(entry)) {
1442 char * t = xmalloc(entry->length + length);
1443 memcpy(t, entry->data, entry->length);
1445 entry->info.offset = 0;
1447 entry->data = xrealloc(entry->data, entry->length + length);
1449 copyData(td->type, ((char *) entry->data) + entry->length,
1450 td->data, td->count, length);
1452 entry->length += length;
1454 entry->info.count += td->count;
1459 int headerPut(Header h, rpmtd td, headerPutFlags flags)
1464 if (flags & HEADERPUT_APPEND) {
1465 rc = findEntry(h, td->tag, td->type) ?
1466 intAppendEntry(h, td) :
1469 rc = intAddEntry(h, td);
1474 int headerAddI18NString(Header h, rpmTagVal tag, const char * string,
1477 indexEntry table, entry;
1478 const char ** strArray;
1481 rpm_count_t i, langNum;
1484 table = findEntry(h, RPMTAG_HEADERI18NTABLE, RPM_STRING_ARRAY_TYPE);
1485 entry = findEntry(h, tag, RPM_I18NSTRING_TYPE);
1487 if (!table && entry)
1488 return 0; /* this shouldn't ever happen!! */
1490 if (!table && !entry) {
1491 const char * charArray[2];
1492 rpm_count_t count = 0;
1494 if (!lang || (lang[0] == 'C' && lang[1] == '\0')) {
1495 charArray[count++] = "C";
1497 charArray[count++] = "C";
1498 charArray[count++] = lang;
1502 td.tag = RPMTAG_HEADERI18NTABLE;
1503 td.type = RPM_STRING_ARRAY_TYPE;
1504 td.data = (void *) charArray;
1506 if (!headerPut(h, &td, HEADERPUT_DEFAULT))
1508 table = findEntry(h, RPMTAG_HEADERI18NTABLE, RPM_STRING_ARRAY_TYPE);
1513 if (!lang) lang = "C";
1515 { const char * l = table->data;
1516 for (langNum = 0; langNum < table->info.count; langNum++) {
1517 if (rstreq(l, lang)) break;
1522 if (langNum >= table->info.count) {
1523 length = strlen(lang) + 1;
1524 if (ENTRY_IN_REGION(table)) {
1525 char * t = xmalloc(table->length + length);
1526 memcpy(t, table->data, table->length);
1528 table->info.offset = 0;
1530 table->data = xrealloc(table->data, table->length + length);
1531 memmove(((char *)table->data) + table->length, lang, length);
1532 table->length += length;
1533 table->info.count++;
1539 strArray = xmalloc(sizeof(*strArray) * (langNum + 1));
1540 for (i = 0; i < langNum; i++)
1542 strArray[langNum] = string;
1546 td.type = RPM_I18NSTRING_TYPE;
1548 td.count = langNum + 1;
1549 rc = headerPut(h, &td, HEADERPUT_DEFAULT);
1552 } else if (langNum >= entry->info.count) {
1553 ghosts = langNum - entry->info.count;
1555 length = strlen(string) + 1 + ghosts;
1556 if (ENTRY_IN_REGION(entry)) {
1557 char * t = xmalloc(entry->length + length);
1558 memcpy(t, entry->data, entry->length);
1560 entry->info.offset = 0;
1562 entry->data = xrealloc(entry->data, entry->length + length);
1564 memset(((char *)entry->data) + entry->length, '\0', ghosts);
1565 memmove(((char *)entry->data) + entry->length + ghosts, string, strlen(string)+1);
1567 entry->length += length;
1568 entry->info.count = langNum + 1;
1570 char *b, *be, *e, *ee, *t;
1573 /* Set beginning/end pointers to previous data */
1574 b = be = e = ee = entry->data;
1575 for (i = 0; i < table->info.count; i++) {
1578 ee += strlen(ee) + 1;
1583 /* Get storage for new buffer */
1585 sn = strlen(string) + 1;
1587 length = bn + sn + en;
1588 t = buf = xmalloc(length);
1590 /* Copy values into new storage */
1593 memcpy(t, string, sn);
1598 /* Replace i18N string array */
1599 entry->length -= strlen(be) + 1;
1600 entry->length += sn;
1602 if (ENTRY_IN_REGION(entry)) {
1603 entry->info.offset = 0;
1605 entry->data = _free(entry->data);
1612 int headerMod(Header h, rpmtd td)
1619 /* First find the tag */
1620 entry = findEntry(h, td->tag, td->type);
1625 data = grabData(td->type, td->data, td->count, &length);
1626 if (data == NULL || length <= 0)
1629 /* make sure entry points to the first occurence of this tag */
1630 while (entry > h->index && (entry - 1)->info.tag == td->tag)
1633 /* free after we've grabbed the new data in case the two are intertwined;
1634 that's a bad idea but at least we won't break */
1635 oldData = entry->data;
1637 entry->info.count = td->count;
1638 entry->info.type = td->type;
1640 entry->length = length;
1642 if (ENTRY_IN_REGION(entry)) {
1643 entry->info.offset = 0;
1651 * Header tag iterator data structure.
1653 struct headerIterator_s {
1654 Header h; /*!< Header being iterated. */
1655 int next_index; /*!< Next tag index. */
1658 HeaderIterator headerFreeIterator(HeaderIterator hi)
1661 hi->h = headerFree(hi->h);
1667 HeaderIterator headerInitIterator(Header h)
1669 HeaderIterator hi = xmalloc(sizeof(*hi));
1673 hi->h = headerLink(h);
1678 static indexEntry nextIndex(HeaderIterator hi)
1682 indexEntry entry = NULL;
1684 for (slot = hi->next_index; slot < h->indexUsed; slot++) {
1685 entry = h->index + slot;
1686 if (!ENTRY_IS_REGION(entry))
1689 hi->next_index = slot;
1690 if (entry == NULL || slot >= h->indexUsed)
1697 rpmTagVal headerNextTag(HeaderIterator hi)
1699 indexEntry entry = nextIndex(hi);
1700 return entry ? entry->info.tag : RPMTAG_NOT_FOUND;
1703 int headerNext(HeaderIterator hi, rpmtd td)
1705 indexEntry entry = nextIndex(hi);
1710 td->tag = entry->info.tag;
1711 rc = copyTdEntry(entry, td, HEADERGET_DEFAULT);
1713 return ((rc == 1) ? 1 : 0);
1716 unsigned int headerGetInstance(Header h)
1718 return h ? h->instance : 0;
1721 void headerSetInstance(Header h, unsigned int instance)
1723 h->instance = instance;