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 unsigned headerSizeof(Header h, int magicp)
251 unsigned int size = 0;
260 case HEADER_MAGIC_YES:
261 size += sizeof(rpm_header_magic);
263 case HEADER_MAGIC_NO:
267 size += 2 * sizeof(int32_t); /* count of index entries */
269 for (i = 0, entry = h->index; i < h->indexUsed; i++, entry++) {
272 /* Regions go in as is ... */
273 if (ENTRY_IS_REGION(entry)) {
274 size += entry->length;
275 /* XXX Legacy regions do not include the region tag and data. */
276 if (i == 0 && (h->flags & HEADERFLAG_LEGACY))
277 size += sizeof(struct entryInfo_s) + entry->info.count;
281 /* ... and region elements are skipped. */
282 if (entry->info.offset < 0)
286 type = entry->info.type;
287 if (typeSizes[type] > 1) {
288 unsigned diff = typeSizes[type] - (size % typeSizes[type]);
289 if (diff != typeSizes[type]) {
294 size += sizeof(struct entryInfo_s) + entry->length;
301 * Return length of entry data.
302 * @param type entry data type
303 * @param p entry data
304 * @param count entry item count
305 * @param onDisk data is concatenated strings (with NUL's))?
306 * @param pend pointer to end of data (or NULL)
307 * @return no. bytes in data, -1 on failure
309 static int dataLength(rpm_tagtype_t type, rpm_constdata_t p, rpm_count_t count,
310 int onDisk, rpm_constdata_t pend)
312 const unsigned char * s = p;
313 const unsigned char * se = pend;
317 case RPM_STRING_TYPE:
325 length++; /* count nul terminator too. */
328 case RPM_STRING_ARRAY_TYPE:
329 case RPM_I18NSTRING_TYPE:
330 /* These are like RPM_STRING_TYPE, except they're *always* an array */
331 /* Compute sum of length of all strings, including nul terminators */
335 length++; /* count nul terminator too */
343 const char ** av = (const char **)p;
345 /* add one for null termination */
346 length += strlen(*av++) + 1;
352 if (typeSizes[type] == -1)
354 length = typeSizes[(type & 0xf)] * count;
355 if (length < 0 || (se && (s + length) > se))
364 * Swap int32_t and int16_t arrays within header region.
366 * If a header region tag is in the set to be swabbed, as the data for a
367 * a header region is located after all other tag data.
369 * @param entry header entry
370 * @param il no. of entries
371 * @param dl start no. bytes of data
372 * @param pe header physical entry pointer (swapped)
373 * @param dataStart header data start
374 * @param dataEnd header data end
375 * @param regionid region offset
376 * @return no. bytes of data in region, -1 on error
378 static int regionSwab(indexEntry entry, int il, int dl,
380 unsigned char * dataStart,
381 const unsigned char * dataEnd,
384 for (; il > 0; il--, pe++) {
385 struct indexEntry_s ie;
388 ie.info.tag = ntohl(pe->tag);
389 ie.info.type = ntohl(pe->type);
390 ie.info.count = ntohl(pe->count);
391 ie.info.offset = ntohl(pe->offset);
393 if (hdrchkType(ie.info.type))
395 if (hdrchkData(ie.info.count))
397 if (hdrchkData(ie.info.offset))
399 if (hdrchkAlign(ie.info.type, ie.info.offset))
402 ie.data = dataStart + ie.info.offset;
403 if (dataEnd && (unsigned char *)ie.data >= dataEnd)
406 ie.length = dataLength(ie.info.type, ie.data, ie.info.count, 1, dataEnd);
407 if (ie.length < 0 || hdrchkData(ie.length))
413 ie.info.offset = regionid;
414 *entry = ie; /* structure assignment */
420 if (typeSizes[type] > 1) {
421 unsigned diff = typeSizes[type] - (dl % typeSizes[type]);
422 if (diff != typeSizes[type]) {
427 /* Perform endian conversions */
428 switch (ntohl(pe->type)) {
430 { uint64_t * it = ie.data;
431 for (; ie.info.count > 0; ie.info.count--, it += 1) {
432 if (dataEnd && ((unsigned char *)it) >= dataEnd)
438 { int32_t * it = ie.data;
439 for (; ie.info.count > 0; ie.info.count--, it += 1) {
440 if (dataEnd && ((unsigned char *)it) >= dataEnd)
446 { int16_t * it = ie.data;
447 for (; ie.info.count > 0; ie.info.count--, it += 1) {
448 if (dataEnd && ((unsigned char *)it) >= dataEnd)
464 * @retval *lengthPtr no. bytes in unloaded header blob
465 * @return unloaded header blob (NULL on error)
467 static void * doHeaderUnload(Header h, size_t * lengthPtr)
479 int drlen, ndribbles;
481 if (h == NULL) return NULL;
483 /* Sort entries by (offset,tag). */
486 /* Compute (il,dl) for all tags, including those deleted in region. */
487 drlen = ndribbles = 0;
488 for (i = 0, entry = h->index; i < h->indexUsed; i++, entry++) {
489 if (ENTRY_IS_REGION(entry)) {
490 int32_t rdl = -entry->info.offset; /* negative offset */
491 int32_t ril = rdl/sizeof(*pe);
492 int rid = entry->info.offset;
495 dl += entry->rdlen + entry->info.count;
496 /* XXX Legacy regions do not include the region tag and data. */
497 if (i == 0 && (h->flags & HEADERFLAG_LEGACY))
500 /* Skip rest of entries in region, but account for dribbles. */
501 for (; i < h->indexUsed && entry->info.offset <= rid+1; i++, entry++) {
502 if (entry->info.offset <= rid)
506 type = entry->info.type;
507 if (typeSizes[type] > 1) {
508 unsigned diff = typeSizes[type] - (dl % typeSizes[type]);
509 if (diff != typeSizes[type]) {
517 drlen += entry->length;
525 /* Ignore deleted drips. */
526 if (entry->data == NULL || entry->length <= 0)
530 type = entry->info.type;
531 if (typeSizes[type] > 1) {
532 unsigned diff = typeSizes[type] - (dl % typeSizes[type]);
533 if (diff != typeSizes[type]) {
542 /* Sanity checks on header intro. */
543 if (hdrchkTags(il) || hdrchkData(dl))
546 len = sizeof(il) + sizeof(dl) + (il * sizeof(*pe)) + dl;
552 pe = (entryInfo) &ei[2];
553 dataStart = te = (char *) (pe + il);
555 for (i = 0, entry = h->index; i < h->indexUsed; i++, entry++) {
561 if (entry->data == NULL || entry->length <= 0)
564 t = (unsigned char*)te;
565 pe->tag = htonl(entry->info.tag);
566 pe->type = htonl(entry->info.type);
567 pe->count = htonl(entry->info.count);
569 if (ENTRY_IS_REGION(entry)) {
570 int32_t rdl = -entry->info.offset; /* negative offset */
571 int32_t ril = rdl/sizeof(*pe) + ndribbles;
572 int rid = entry->info.offset;
574 src = (char *)entry->data;
575 rdlen = entry->rdlen;
577 /* XXX Legacy regions do not include the region tag and data. */
578 if (i == 0 && (h->flags & HEADERFLAG_LEGACY)) {
581 memcpy(pe+1, src, rdl);
582 memcpy(te, src + rdl, rdlen);
585 pe->offset = htonl(te - dataStart);
588 stei[2] = htonl(-rdl-entry->info.count);
590 memcpy(te, stei, entry->info.count);
591 te += entry->info.count;
593 rdlen += entry->info.count;
595 count = regionSwab(NULL, ril, 0, pe, t, NULL, 0);
601 memcpy(pe+1, src + sizeof(*pe), ((ril-1) * sizeof(*pe)));
602 memcpy(te, src + (ril * sizeof(*pe)), rdlen+entry->info.count+drlen);
605 entryInfo se = (entryInfo)src;
606 int off = ntohl(se->offset);
607 pe->offset = (off) ? htonl(te - dataStart) : htonl(off);
609 te += entry->info.count + drlen;
611 count = regionSwab(NULL, ril, 0, pe, t, NULL, 0);
612 if (count != (rdlen + entry->info.count + drlen))
616 /* Skip rest of entries in region. */
617 while (i < h->indexUsed && entry->info.offset <= rid+1) {
627 /* Ignore deleted drips. */
628 if (entry->data == NULL || entry->length <= 0)
632 type = entry->info.type;
633 if (typeSizes[type] > 1) {
635 diff = typeSizes[type] - ((te - dataStart) % typeSizes[type]);
636 if (diff != typeSizes[type]) {
642 pe->offset = htonl(te - dataStart);
644 /* copy data w/ endian conversions */
645 switch (entry->info.type) {
647 count = entry->info.count;
650 *((uint64_t *)te) = htonll(*((uint64_t *)src));
651 te += sizeof(uint64_t);
652 src += sizeof(uint64_t);
657 count = entry->info.count;
660 *((int32_t *)te) = htonl(*((int32_t *)src));
661 te += sizeof(int32_t);
662 src += sizeof(int32_t);
667 count = entry->info.count;
670 *((int16_t *)te) = htons(*((int16_t *)src));
671 te += sizeof(int16_t);
672 src += sizeof(int16_t);
677 memcpy(te, entry->data, entry->length);
684 /* Insure that there are no memcpy underruns/overruns. */
685 if (((char *)pe) != dataStart)
687 if ((((char *)ei)+len) != te)
702 void * headerUnload(Header h)
704 return doHeaderUnload(h, NULL);
708 * Find matching (tag,type) entry in header.
710 * @param tag entry tag
711 * @param type entry type
712 * @return header entry
715 indexEntry findEntry(Header h, rpmTagVal tag, rpm_tagtype_t type)
718 struct indexEntry_s key;
720 if (h == NULL) return NULL;
721 if (!(h->flags & HEADERFLAG_SORTED)) headerSort(h);
725 entry = bsearch(&key, h->index, h->indexUsed, sizeof(*h->index), indexCmp);
729 if (type == RPM_NULL_TYPE)
733 while (entry->info.tag == tag && entry->info.type != type &&
734 entry > h->index) entry--;
736 if (entry->info.tag == tag && entry->info.type == type)
742 int headerDel(Header h, rpmTagVal tag)
744 indexEntry last = h->index + h->indexUsed;
745 indexEntry entry, first;
748 entry = findEntry(h, tag, RPM_NULL_TYPE);
749 if (!entry) return 1;
751 /* Make sure entry points to the first occurence of this tag. */
752 while (entry > h->index && (entry - 1)->info.tag == tag)
755 /* Free data for tags being removed. */
756 for (first = entry; first < last; first++) {
758 if (first->info.tag != tag)
763 if (ENTRY_IN_REGION(first))
768 ne = (first - entry);
773 memmove(entry, first, (ne * sizeof(*entry)));
779 Header headerLoad(void * uh)
781 int32_t * ei = (int32_t *) uh;
782 int32_t il = ntohl(ei[0]); /* index length */
783 int32_t dl = ntohl(ei[1]); /* data length */
784 size_t pvlen = sizeof(il) + sizeof(dl) +
785 (il * sizeof(struct entryInfo_s)) + dl;
789 unsigned char * dataStart;
790 unsigned char * dataEnd;
794 /* Sanity checks on header intro. */
795 if (hdrchkTags(il) || hdrchkData(dl))
799 pe = (entryInfo) &ei[2];
800 dataStart = (unsigned char *) (pe + il);
801 dataEnd = dataStart + dl;
803 h = headerCreate(uh, il);
806 if (!(htonl(pe->tag) < RPMTAG_HEADERI18NTABLE)) {
807 h->flags |= HEADERFLAG_LEGACY;
808 entry->info.type = REGION_TAG_TYPE;
809 entry->info.tag = RPMTAG_HEADERIMAGE;
810 entry->info.count = REGION_TAG_COUNT;
811 entry->info.offset = ((unsigned char *)pe - dataStart); /* negative offset */
814 entry->length = pvlen - sizeof(il) - sizeof(dl);
815 rdlen = regionSwab(entry+1, il, 0, pe, dataStart, dataEnd, entry->info.offset);
818 entry->rdlen = rdlen;
824 h->flags &= ~HEADERFLAG_LEGACY;
826 entry->info.type = htonl(pe->type);
827 entry->info.count = htonl(pe->count);
829 if (hdrchkType(entry->info.type))
831 if (hdrchkTags(entry->info.count))
834 { int off = ntohl(pe->offset);
839 size_t nb = REGION_TAG_COUNT;
841 /* XXX Hmm, why the copy? */
842 memcpy(&stei, dataStart + off, nb);
843 rdl = -ntohl(stei[2]); /* negative offset */
844 ril = rdl/sizeof(*pe);
845 if (hdrchkTags(ril) || hdrchkData(rdl))
847 entry->info.tag = htonl(pe->tag);
850 rdl = (ril * sizeof(struct entryInfo_s));
851 entry->info.tag = RPMTAG_HEADERIMAGE;
854 entry->info.offset = -rdl; /* negative offset */
857 entry->length = pvlen - sizeof(il) - sizeof(dl);
858 rdlen = regionSwab(entry+1, ril-1, 0, pe+1, dataStart, dataEnd, entry->info.offset);
861 entry->rdlen = rdlen;
863 if (ril < h->indexUsed) {
864 indexEntry newEntry = entry + ril;
865 int ne = (h->indexUsed - ril);
866 int rid = entry->info.offset+1;
869 /* Load dribble entries from region. */
870 rc = regionSwab(newEntry, ne, 0, pe+ril, dataStart, dataEnd, rid);
875 { indexEntry firstEntry = newEntry;
876 int save = h->indexUsed;
879 /* Dribble entries replace duplicate region entries. */
881 for (j = 0; j < ne; j++, newEntry++) {
882 (void) headerDel(h, newEntry->info.tag);
883 if (newEntry->info.tag == RPMTAG_BASENAMES)
884 (void) headerDel(h, RPMTAG_OLDFILENAMES);
887 /* If any duplicate entries were replaced, move new entries down. */
888 if (h->indexUsed < (save - ne)) {
889 memmove(h->index + h->indexUsed, firstEntry,
890 (ne * sizeof(*entry)));
897 h->flags &= ~HEADERFLAG_SORTED;
899 h->flags |= HEADERFLAG_ALLOCATED;
905 h->index = _free(h->index);
911 Header headerReload(Header h, rpmTagVal tag)
914 void * uh = doHeaderUnload(h, NULL);
924 if (ENTRY_IS_REGION(nh->index)) {
925 if (tag == RPMTAG_HEADERSIGNATURES || tag == RPMTAG_HEADERIMMUTABLE)
926 nh->index[0].info.tag = tag;
931 Header headerCopyLoad(const void * uh)
933 int32_t * ei = (int32_t *) uh;
934 int32_t il = ntohl(ei[0]); /* index length */
935 int32_t dl = ntohl(ei[1]); /* data length */
936 size_t pvlen = sizeof(il) + sizeof(dl) +
937 (il * sizeof(struct entryInfo_s)) + dl;
940 /* Sanity checks on header intro. */
941 if (!(hdrchkTags(il) || hdrchkData(dl)) && pvlen < headerMaxbytes) {
942 void * nuh = memcpy(xmalloc(pvlen), uh, pvlen);
943 if ((h = headerLoad(nuh)) == NULL)
949 Header headerRead(FD_t fd, int magicp)
958 if (magicp == HEADER_MAGIC_YES) {
961 if (Fread(block, 1, 4*sizeof(*block), fd) != 4*sizeof(*block))
966 if (memcmp(&magic, rpm_header_magic, sizeof(magic)))
969 il = ntohl(block[2]);
970 dl = ntohl(block[3]);
972 if (Fread(block, 1, 2*sizeof(*block), fd) != 2*sizeof(*block))
975 il = ntohl(block[0]);
976 dl = ntohl(block[1]);
979 len = sizeof(il) + sizeof(dl) + (il * sizeof(struct entryInfo_s)) + dl;
981 /* Sanity checks on header intro. */
982 if (hdrchkTags(il) || hdrchkData(dl) || len > headerMaxbytes)
988 len -= sizeof(il) + sizeof(dl);
990 if (Fread((char *)&ei[2], 1, len, fd) != len)
996 if (h == NULL && ei != NULL) {
1002 int headerWrite(FD_t fd, Header h, int magicp)
1008 uh = doHeaderUnload(h, &length);
1012 case HEADER_MAGIC_YES:
1013 nb = Fwrite(rpm_header_magic, sizeof(uint8_t), sizeof(rpm_header_magic), fd);
1014 if (nb != sizeof(rpm_header_magic))
1017 case HEADER_MAGIC_NO:
1021 nb = Fwrite(uh, sizeof(char), length, fd);
1025 return (nb == length ? 0 : 1);
1028 int headerIsEntry(Header h, rpmTagVal tag)
1030 /* FIX: h modified by sort. */
1031 return (findEntry(h, tag, RPM_NULL_TYPE) ? 1 : 0);
1036 * Retrieve data from header entry.
1037 * Relevant flags (others are ignored), if neither is set allocation
1038 * behavior depends on data type(!)
1039 * HEADERGET_MINMEM: return pointers to header memory
1040 * HEADERGET_ALLOC: always return malloced memory, overrides MINMEM
1042 * @todo Permit retrieval of regions other than HEADER_IMUTABLE.
1043 * @param entry header entry
1044 * @param td tag data container
1045 * @param minMem string pointers refer to header memory?
1046 * @param flags flags to control memory allocation
1047 * @return 1 on success, otherwise error.
1049 static int copyTdEntry(const indexEntry entry, rpmtd td, headerGetFlags flags)
1051 rpm_count_t count = entry->info.count;
1052 int rc = 1; /* XXX 1 on success. */
1053 /* ALLOC overrides MINMEM */
1054 int allocMem = flags & HEADERGET_ALLOC;
1055 int minMem = allocMem ? 0 : flags & HEADERGET_MINMEM;
1056 int argvArray = (flags & HEADERGET_ARGV) ? 1 : 0;
1059 td->flags = RPMTD_IMMUTABLE;
1060 switch (entry->info.type) {
1063 * XXX This only works for
1064 * XXX "sealed" HEADER_IMMUTABLE/HEADER_SIGNATURES/HEADER_IMAGE.
1065 * XXX This will *not* work for unsealed legacy HEADER_IMAGE (i.e.
1066 * XXX a legacy header freshly read, but not yet unloaded to the rpmdb).
1068 if (ENTRY_IS_REGION(entry)) {
1069 int32_t * ei = ((int32_t *)entry->data) - 2;
1070 entryInfo pe = (entryInfo) (ei + 2);
1071 unsigned char * dataStart = (unsigned char *) (pe + ntohl(ei[0]));
1072 int32_t rdl = -entry->info.offset; /* negative offset */
1073 int32_t ril = rdl/sizeof(*pe);
1076 count = 2 * sizeof(*ei) + (ril * sizeof(*pe)) + rdl;
1077 if (entry->info.tag == RPMTAG_HEADERIMAGE) {
1081 count += REGION_TAG_COUNT;
1082 rdl += REGION_TAG_COUNT;
1085 td->data = xmalloc(count);
1086 ei = (int32_t *) td->data;
1090 pe = (entryInfo) memcpy(ei + 2, pe, (ril * sizeof(*pe)));
1092 dataStart = (unsigned char *) memcpy(pe + ril, dataStart, rdl);
1094 rc = regionSwab(NULL, ril, 0, pe, dataStart, dataStart + rdl, 0);
1095 /* don't return data on failure */
1097 td->data = _free(td->data);
1099 /* XXX 1 on success. */
1100 rc = (rc < 0) ? 0 : 1;
1102 count = entry->length;
1104 ? memcpy(xmalloc(count), entry->data, count)
1108 case RPM_STRING_TYPE:
1109 /* simple string, but fallthrough if its actually an array */
1110 if (count == 1 && !argvArray) {
1111 td->data = allocMem ? xstrdup(entry->data) : entry->data;
1114 case RPM_STRING_ARRAY_TYPE:
1115 case RPM_I18NSTRING_TYPE:
1116 { const char ** ptrEntry;
1117 int tableSize = (count + argvArray) * sizeof(char *);
1122 td->data = xmalloc(tableSize);
1123 ptrEntry = (const char **) td->data;
1126 t = xmalloc(tableSize + entry->length);
1127 td->data = (void *)t;
1128 ptrEntry = (const char **) td->data;
1130 memcpy(t, entry->data, entry->length);
1132 for (i = 0; i < count; i++) {
1139 td->flags |= RPMTD_ARGV;
1144 case RPM_INT16_TYPE:
1145 case RPM_INT32_TYPE:
1146 case RPM_INT64_TYPE:
1148 td->data = xmalloc(entry->length);
1149 memcpy(td->data, entry->data, entry->length);
1151 td->data = entry->data;
1155 /* WTH? Don't mess with unknown data types... */
1160 td->type = entry->info.type;
1163 if (td->data && entry->data != td->data) {
1164 td->flags |= RPMTD_ALLOCED;
1171 * Does locale match entry in header i18n table?
1174 * The range [l,le) contains the next locale to match:
1175 * ll[_CC][.EEEEE][@dddd]
1177 * ll ISO language code (in lowercase).
1178 * CC (optional) ISO coutnry code (in uppercase).
1179 * EEEEE (optional) encoding (not really standardized).
1180 * dddd (optional) dialect.
1183 * @param td header i18n table data, NUL terminated
1184 * @param l start of locale to match
1185 * @param le end of locale to match
1186 * @return 1 on good match, 2 on weak match, 0 on no match
1188 static int headerMatchLocale(const char *td, const char *l, const char *le)
1192 /* First try a complete match. */
1193 if (strlen(td) == (le-l) && rstreqn(td, l, (le - l)))
1196 /* Next, try stripping optional dialect and matching. */
1197 for (fe = l; fe < le && *fe != '@'; fe++)
1199 if (fe < le && rstreqn(td, l, (fe - l)))
1202 /* Next, try stripping optional codeset and matching. */
1203 for (fe = l; fe < le && *fe != '.'; fe++)
1205 if (fe < le && rstreqn(td, l, (fe - l)))
1208 /* Finally, try stripping optional country code and matching. */
1209 for (fe = l; fe < le && *fe != '_'; fe++)
1211 if (fe < le && rstreqn(td, l, (fe - l)))
1218 * Return i18n string from header that matches locale.
1220 * @param entry i18n string data
1221 * @retval td tag data container
1222 * @param flags flags to control allocation
1225 static int copyI18NEntry(Header h, indexEntry entry, rpmtd td,
1226 headerGetFlags flags)
1228 const char *lang, *l, *le;
1231 td->type = RPM_STRING_TYPE;
1233 /* if no match, just return the first string */
1234 td->data = entry->data;
1236 /* XXX Drepper sez' this is the order. */
1237 if ((lang = getenv("LANGUAGE")) == NULL &&
1238 (lang = getenv("LC_ALL")) == NULL &&
1239 (lang = getenv("LC_MESSAGES")) == NULL &&
1240 (lang = getenv("LANG")) == NULL)
1243 if ((table = findEntry(h, RPMTAG_HEADERI18NTABLE, RPM_STRING_ARRAY_TYPE)) == NULL)
1246 for (l = lang; *l != '\0'; l = le) {
1248 char *ed, *ed_weak = NULL;
1251 while (*l && *l == ':') /* skip leading colons */
1255 for (le = l; *le && *le != ':'; le++) /* find end of this locale */
1258 /* For each entry in the header ... */
1259 for (langNum = 0, t = table->data, ed = entry->data;
1260 langNum < entry->info.count;
1261 langNum++, t += strlen(t) + 1, ed += strlen(ed) + 1) {
1263 int match = headerMatchLocale(t, l, le);
1267 } else if (match == 2) {
1278 if (flags & HEADERGET_ALLOC) {
1279 td->data = xstrdup(td->data);
1280 td->flags |= RPMTD_ALLOCED;
1287 * Retrieve tag data from header.
1289 * @retval td tag data container
1290 * @param flags flags to control retrieval
1291 * @return 1 on success, 0 on not found
1293 static int intGetTdEntry(Header h, rpmtd td, headerGetFlags flags)
1298 /* First find the tag */
1299 /* FIX: h modified by sort. */
1300 entry = findEntry(h, td->tag, RPM_NULL_TYPE);
1301 if (entry == NULL) {
1302 /* Td is zeroed above, just return... */
1306 if (flags & HEADERGET_RAW) {
1307 rc = copyTdEntry(entry, td, flags);
1309 switch (entry->info.type) {
1310 case RPM_I18NSTRING_TYPE:
1311 rc = copyI18NEntry(h, entry, td, flags);
1314 rc = copyTdEntry(entry, td, flags);
1319 /* XXX 1 on success */
1320 return ((rc == 1) ? 1 : 0);
1323 int headerGet(Header h, rpmTagVal tag, rpmtd td, headerGetFlags flags)
1326 headerTagTagFunction tagfunc = intGetTdEntry;
1328 if (td == NULL) return 0;
1333 if (flags & HEADERGET_EXT) {
1334 headerTagTagFunction extfunc = rpmHeaderTagFunc(tag);
1335 if (extfunc) tagfunc = extfunc;
1337 rc = tagfunc(h, td, flags);
1339 assert(tag == td->tag);
1345 static void copyData(rpm_tagtype_t type, rpm_data_t dstPtr,
1346 rpm_constdata_t srcPtr, rpm_count_t cnt, int dataLength)
1349 case RPM_STRING_ARRAY_TYPE:
1350 case RPM_I18NSTRING_TYPE:
1351 { const char ** av = (const char **) srcPtr;
1354 while (cnt-- > 0 && dataLength > 0) {
1356 if ((s = *av++) == NULL)
1360 } while (s[-1] && --dataLength > 0);
1365 memmove(dstPtr, srcPtr, dataLength);
1371 * Return (malloc'ed) copy of entry data.
1372 * @param type entry data type
1373 * @param p entry data
1374 * @param c entry item count
1375 * @retval lengthPtr no. bytes in returned data
1376 * @return (malloc'ed) copy of entry data, NULL on error
1379 grabData(rpm_tagtype_t type, rpm_constdata_t p, rpm_count_t c, int * lengthPtr)
1381 rpm_data_t data = NULL;
1384 length = dataLength(type, p, c, 0, NULL);
1386 data = xmalloc(length);
1387 copyData(type, data, p, c, length);
1391 *lengthPtr = length;
1395 static int intAddEntry(Header h, rpmtd td)
1401 /* Count must always be >= 1 for headerAddEntry. */
1405 if (hdrchkType(td->type))
1407 if (hdrchkData(td->count))
1411 data = grabData(td->type, td->data, td->count, &length);
1412 if (data == NULL || length <= 0)
1415 /* Allocate more index space if necessary */
1416 if (h->indexUsed == h->indexAlloced) {
1417 h->indexAlloced += INDEX_MALLOC_SIZE;
1418 h->index = xrealloc(h->index, h->indexAlloced * sizeof(*h->index));
1421 /* Fill in the index */
1422 entry = h->index + h->indexUsed;
1423 entry->info.tag = td->tag;
1424 entry->info.type = td->type;
1425 entry->info.count = td->count;
1426 entry->info.offset = 0;
1428 entry->length = length;
1430 if (h->indexUsed > 0 && td->tag < h->index[h->indexUsed-1].info.tag)
1431 h->flags &= ~HEADERFLAG_SORTED;
1437 static int intAppendEntry(Header h, rpmtd td)
1442 if (td->type == RPM_STRING_TYPE || td->type == RPM_I18NSTRING_TYPE) {
1443 /* we can't do this */
1447 /* Find the tag entry in the header. */
1448 entry = findEntry(h, td->tag, td->type);
1452 length = dataLength(td->type, td->data, td->count, 0, NULL);
1456 if (ENTRY_IN_REGION(entry)) {
1457 char * t = xmalloc(entry->length + length);
1458 memcpy(t, entry->data, entry->length);
1460 entry->info.offset = 0;
1462 entry->data = xrealloc(entry->data, entry->length + length);
1464 copyData(td->type, ((char *) entry->data) + entry->length,
1465 td->data, td->count, length);
1467 entry->length += length;
1469 entry->info.count += td->count;
1474 int headerPut(Header h, rpmtd td, headerPutFlags flags)
1479 if (flags & HEADERPUT_APPEND) {
1480 rc = findEntry(h, td->tag, td->type) ?
1481 intAppendEntry(h, td) :
1484 rc = intAddEntry(h, td);
1489 int headerAddI18NString(Header h, rpmTagVal tag, const char * string,
1492 indexEntry table, entry;
1493 const char ** strArray;
1496 rpm_count_t i, langNum;
1499 table = findEntry(h, RPMTAG_HEADERI18NTABLE, RPM_STRING_ARRAY_TYPE);
1500 entry = findEntry(h, tag, RPM_I18NSTRING_TYPE);
1502 if (!table && entry)
1503 return 0; /* this shouldn't ever happen!! */
1505 if (!table && !entry) {
1506 const char * charArray[2];
1507 rpm_count_t count = 0;
1509 if (!lang || (lang[0] == 'C' && lang[1] == '\0')) {
1510 charArray[count++] = "C";
1512 charArray[count++] = "C";
1513 charArray[count++] = lang;
1517 td.tag = RPMTAG_HEADERI18NTABLE;
1518 td.type = RPM_STRING_ARRAY_TYPE;
1519 td.data = (void *) charArray;
1521 if (!headerPut(h, &td, HEADERPUT_DEFAULT))
1523 table = findEntry(h, RPMTAG_HEADERI18NTABLE, RPM_STRING_ARRAY_TYPE);
1528 if (!lang) lang = "C";
1530 { const char * l = table->data;
1531 for (langNum = 0; langNum < table->info.count; langNum++) {
1532 if (rstreq(l, lang)) break;
1537 if (langNum >= table->info.count) {
1538 length = strlen(lang) + 1;
1539 if (ENTRY_IN_REGION(table)) {
1540 char * t = xmalloc(table->length + length);
1541 memcpy(t, table->data, table->length);
1543 table->info.offset = 0;
1545 table->data = xrealloc(table->data, table->length + length);
1546 memmove(((char *)table->data) + table->length, lang, length);
1547 table->length += length;
1548 table->info.count++;
1554 strArray = xmalloc(sizeof(*strArray) * (langNum + 1));
1555 for (i = 0; i < langNum; i++)
1557 strArray[langNum] = string;
1561 td.type = RPM_I18NSTRING_TYPE;
1563 td.count = langNum + 1;
1564 rc = headerPut(h, &td, HEADERPUT_DEFAULT);
1567 } else if (langNum >= entry->info.count) {
1568 ghosts = langNum - entry->info.count;
1570 length = strlen(string) + 1 + ghosts;
1571 if (ENTRY_IN_REGION(entry)) {
1572 char * t = xmalloc(entry->length + length);
1573 memcpy(t, entry->data, entry->length);
1575 entry->info.offset = 0;
1577 entry->data = xrealloc(entry->data, entry->length + length);
1579 memset(((char *)entry->data) + entry->length, '\0', ghosts);
1580 memmove(((char *)entry->data) + entry->length + ghosts, string, strlen(string)+1);
1582 entry->length += length;
1583 entry->info.count = langNum + 1;
1585 char *b, *be, *e, *ee, *t;
1588 /* Set beginning/end pointers to previous data */
1589 b = be = e = ee = entry->data;
1590 for (i = 0; i < table->info.count; i++) {
1593 ee += strlen(ee) + 1;
1598 /* Get storage for new buffer */
1600 sn = strlen(string) + 1;
1602 length = bn + sn + en;
1603 t = buf = xmalloc(length);
1605 /* Copy values into new storage */
1608 memcpy(t, string, sn);
1613 /* Replace i18N string array */
1614 entry->length -= strlen(be) + 1;
1615 entry->length += sn;
1617 if (ENTRY_IN_REGION(entry)) {
1618 entry->info.offset = 0;
1620 entry->data = _free(entry->data);
1627 int headerMod(Header h, rpmtd td)
1634 /* First find the tag */
1635 entry = findEntry(h, td->tag, td->type);
1640 data = grabData(td->type, td->data, td->count, &length);
1641 if (data == NULL || length <= 0)
1644 /* make sure entry points to the first occurence of this tag */
1645 while (entry > h->index && (entry - 1)->info.tag == td->tag)
1648 /* free after we've grabbed the new data in case the two are intertwined;
1649 that's a bad idea but at least we won't break */
1650 oldData = entry->data;
1652 entry->info.count = td->count;
1653 entry->info.type = td->type;
1655 entry->length = length;
1657 if (ENTRY_IN_REGION(entry)) {
1658 entry->info.offset = 0;
1666 * Header tag iterator data structure.
1668 struct headerIterator_s {
1669 Header h; /*!< Header being iterated. */
1670 int next_index; /*!< Next tag index. */
1673 HeaderIterator headerFreeIterator(HeaderIterator hi)
1676 hi->h = headerFree(hi->h);
1682 HeaderIterator headerInitIterator(Header h)
1684 HeaderIterator hi = xmalloc(sizeof(*hi));
1688 hi->h = headerLink(h);
1693 static indexEntry nextIndex(HeaderIterator hi)
1697 indexEntry entry = NULL;
1699 for (slot = hi->next_index; slot < h->indexUsed; slot++) {
1700 entry = h->index + slot;
1701 if (!ENTRY_IS_REGION(entry))
1704 hi->next_index = slot;
1705 if (entry == NULL || slot >= h->indexUsed)
1712 rpmTagVal headerNextTag(HeaderIterator hi)
1714 indexEntry entry = nextIndex(hi);
1715 return entry ? entry->info.tag : RPMTAG_NOT_FOUND;
1718 int headerNext(HeaderIterator hi, rpmtd td)
1720 indexEntry entry = nextIndex(hi);
1725 td->tag = entry->info.tag;
1726 rc = copyTdEntry(entry, td, HEADERGET_DEFAULT);
1728 return ((rc == 1) ? 1 : 0);
1731 unsigned int headerGetInstance(Header h)
1733 return h ? h->instance : 0;
1736 void headerSetInstance(Header h, unsigned int instance)
1738 h->instance = instance;