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;
941 /* Sanity checks on header intro. */
942 if (!(hdrchkTags(il) || hdrchkData(dl)) && pvlen < headerMaxbytes) {
943 nuh = memcpy(xmalloc(pvlen), uh, pvlen);
944 if ((h = headerLoad(nuh)) == NULL)
950 Header headerRead(FD_t fd, int magicp)
959 if (magicp == HEADER_MAGIC_YES) {
962 if (Fread(block, 1, 4*sizeof(*block), fd) != 4*sizeof(*block))
967 if (memcmp(&magic, rpm_header_magic, sizeof(magic)))
970 il = ntohl(block[2]);
971 dl = ntohl(block[3]);
973 if (Fread(block, 1, 2*sizeof(*block), fd) != 2*sizeof(*block))
976 il = ntohl(block[0]);
977 dl = ntohl(block[1]);
980 len = sizeof(il) + sizeof(dl) + (il * sizeof(struct entryInfo_s)) + dl;
982 /* Sanity checks on header intro. */
983 if (hdrchkTags(il) || hdrchkData(dl) || len > headerMaxbytes)
989 len -= sizeof(il) + sizeof(dl);
991 if (Fread((char *)&ei[2], 1, len, fd) != len)
997 if (h == NULL && ei != NULL) {
1003 int headerWrite(FD_t fd, Header h, int magicp)
1009 uh = doHeaderUnload(h, &length);
1013 case HEADER_MAGIC_YES:
1014 nb = Fwrite(rpm_header_magic, sizeof(uint8_t), sizeof(rpm_header_magic), fd);
1015 if (nb != sizeof(rpm_header_magic))
1018 case HEADER_MAGIC_NO:
1022 nb = Fwrite(uh, sizeof(char), length, fd);
1026 return (nb == length ? 0 : 1);
1029 int headerIsEntry(Header h, rpmTagVal tag)
1031 /* FIX: h modified by sort. */
1032 return (findEntry(h, tag, RPM_NULL_TYPE) ? 1 : 0);
1037 * Retrieve data from header entry.
1038 * Relevant flags (others are ignored), if neither is set allocation
1039 * behavior depends on data type(!)
1040 * HEADERGET_MINMEM: return pointers to header memory
1041 * HEADERGET_ALLOC: always return malloced memory, overrides MINMEM
1043 * @todo Permit retrieval of regions other than HEADER_IMUTABLE.
1044 * @param entry header entry
1045 * @param td tag data container
1046 * @param minMem string pointers refer to header memory?
1047 * @param flags flags to control memory allocation
1048 * @return 1 on success, otherwise error.
1050 static int copyTdEntry(const indexEntry entry, rpmtd td, headerGetFlags flags)
1052 rpm_count_t count = entry->info.count;
1053 int rc = 1; /* XXX 1 on success. */
1054 /* ALLOC overrides MINMEM */
1055 int allocMem = flags & HEADERGET_ALLOC;
1056 int minMem = allocMem ? 0 : flags & HEADERGET_MINMEM;
1057 int argvArray = (flags & HEADERGET_ARGV) ? 1 : 0;
1060 td->flags = RPMTD_IMMUTABLE;
1061 switch (entry->info.type) {
1064 * XXX This only works for
1065 * XXX "sealed" HEADER_IMMUTABLE/HEADER_SIGNATURES/HEADER_IMAGE.
1066 * XXX This will *not* work for unsealed legacy HEADER_IMAGE (i.e.
1067 * XXX a legacy header freshly read, but not yet unloaded to the rpmdb).
1069 if (ENTRY_IS_REGION(entry)) {
1070 int32_t * ei = ((int32_t *)entry->data) - 2;
1071 entryInfo pe = (entryInfo) (ei + 2);
1072 unsigned char * dataStart = (unsigned char *) (pe + ntohl(ei[0]));
1073 int32_t rdl = -entry->info.offset; /* negative offset */
1074 int32_t ril = rdl/sizeof(*pe);
1077 count = 2 * sizeof(*ei) + (ril * sizeof(*pe)) + rdl;
1078 if (entry->info.tag == RPMTAG_HEADERIMAGE) {
1082 count += REGION_TAG_COUNT;
1083 rdl += REGION_TAG_COUNT;
1086 td->data = xmalloc(count);
1087 ei = (int32_t *) td->data;
1091 pe = (entryInfo) memcpy(ei + 2, pe, (ril * sizeof(*pe)));
1093 dataStart = (unsigned char *) memcpy(pe + ril, dataStart, rdl);
1095 rc = regionSwab(NULL, ril, 0, pe, dataStart, dataStart + rdl, 0);
1096 /* don't return data on failure */
1098 td->data = _free(td->data);
1100 /* XXX 1 on success. */
1101 rc = (rc < 0) ? 0 : 1;
1103 count = entry->length;
1105 ? memcpy(xmalloc(count), entry->data, count)
1109 case RPM_STRING_TYPE:
1110 /* simple string, but fallthrough if its actually an array */
1111 if (count == 1 && !argvArray) {
1112 td->data = allocMem ? xstrdup(entry->data) : entry->data;
1115 case RPM_STRING_ARRAY_TYPE:
1116 case RPM_I18NSTRING_TYPE:
1117 { const char ** ptrEntry;
1118 int tableSize = (count + argvArray) * sizeof(char *);
1123 td->data = xmalloc(tableSize);
1124 ptrEntry = (const char **) td->data;
1127 t = xmalloc(tableSize + entry->length);
1128 td->data = (void *)t;
1129 ptrEntry = (const char **) td->data;
1131 memcpy(t, entry->data, entry->length);
1133 for (i = 0; i < count; i++) {
1140 td->flags |= RPMTD_ARGV;
1145 case RPM_INT16_TYPE:
1146 case RPM_INT32_TYPE:
1147 case RPM_INT64_TYPE:
1149 td->data = xmalloc(entry->length);
1150 memcpy(td->data, entry->data, entry->length);
1152 td->data = entry->data;
1156 /* WTH? Don't mess with unknown data types... */
1161 td->type = entry->info.type;
1164 if (td->data && entry->data != td->data) {
1165 td->flags |= RPMTD_ALLOCED;
1172 * Does locale match entry in header i18n table?
1175 * The range [l,le) contains the next locale to match:
1176 * ll[_CC][.EEEEE][@dddd]
1178 * ll ISO language code (in lowercase).
1179 * CC (optional) ISO coutnry code (in uppercase).
1180 * EEEEE (optional) encoding (not really standardized).
1181 * dddd (optional) dialect.
1184 * @param td header i18n table data, NUL terminated
1185 * @param l start of locale to match
1186 * @param le end of locale to match
1187 * @return 1 on good match, 2 on weak match, 0 on no match
1189 static int headerMatchLocale(const char *td, const char *l, const char *le)
1193 /* First try a complete match. */
1194 if (strlen(td) == (le-l) && rstreqn(td, l, (le - l)))
1197 /* Next, try stripping optional dialect and matching. */
1198 for (fe = l; fe < le && *fe != '@'; fe++)
1200 if (fe < le && rstreqn(td, l, (fe - l)))
1203 /* Next, try stripping optional codeset and matching. */
1204 for (fe = l; fe < le && *fe != '.'; fe++)
1206 if (fe < le && rstreqn(td, l, (fe - l)))
1209 /* Finally, try stripping optional country code and matching. */
1210 for (fe = l; fe < le && *fe != '_'; fe++)
1212 if (fe < le && rstreqn(td, l, (fe - l)))
1219 * Return i18n string from header that matches locale.
1221 * @param entry i18n string data
1222 * @retval td tag data container
1223 * @param flags flags to control allocation
1226 static int copyI18NEntry(Header h, indexEntry entry, rpmtd td,
1227 headerGetFlags flags)
1229 const char *lang, *l, *le;
1232 td->type = RPM_STRING_TYPE;
1234 /* if no match, just return the first string */
1235 td->data = entry->data;
1237 /* XXX Drepper sez' this is the order. */
1238 if ((lang = getenv("LANGUAGE")) == NULL &&
1239 (lang = getenv("LC_ALL")) == NULL &&
1240 (lang = getenv("LC_MESSAGES")) == NULL &&
1241 (lang = getenv("LANG")) == NULL)
1244 if ((table = findEntry(h, RPMTAG_HEADERI18NTABLE, RPM_STRING_ARRAY_TYPE)) == NULL)
1247 for (l = lang; *l != '\0'; l = le) {
1249 char *ed, *ed_weak = NULL;
1252 while (*l && *l == ':') /* skip leading colons */
1256 for (le = l; *le && *le != ':'; le++) /* find end of this locale */
1259 /* For each entry in the header ... */
1260 for (langNum = 0, t = table->data, ed = entry->data;
1261 langNum < entry->info.count;
1262 langNum++, t += strlen(t) + 1, ed += strlen(ed) + 1) {
1264 int match = headerMatchLocale(t, l, le);
1268 } else if (match == 2) {
1279 if (flags & HEADERGET_ALLOC) {
1280 td->data = xstrdup(td->data);
1281 td->flags |= RPMTD_ALLOCED;
1288 * Retrieve tag data from header.
1290 * @retval td tag data container
1291 * @param flags flags to control retrieval
1292 * @return 1 on success, 0 on not found
1294 static int intGetTdEntry(Header h, rpmtd td, headerGetFlags flags)
1299 /* First find the tag */
1300 /* FIX: h modified by sort. */
1301 entry = findEntry(h, td->tag, RPM_NULL_TYPE);
1302 if (entry == NULL) {
1303 /* Td is zeroed above, just return... */
1307 if (flags & HEADERGET_RAW) {
1308 rc = copyTdEntry(entry, td, flags);
1310 switch (entry->info.type) {
1311 case RPM_I18NSTRING_TYPE:
1312 rc = copyI18NEntry(h, entry, td, flags);
1315 rc = copyTdEntry(entry, td, flags);
1320 /* XXX 1 on success */
1321 return ((rc == 1) ? 1 : 0);
1324 int headerGet(Header h, rpmTagVal tag, rpmtd td, headerGetFlags flags)
1327 headerTagTagFunction tagfunc = intGetTdEntry;
1329 if (td == NULL) return 0;
1334 if (flags & HEADERGET_EXT) {
1335 headerTagTagFunction extfunc = rpmHeaderTagFunc(tag);
1336 if (extfunc) tagfunc = extfunc;
1338 rc = tagfunc(h, td, flags);
1340 assert(tag == td->tag);
1346 static void copyData(rpm_tagtype_t type, rpm_data_t dstPtr,
1347 rpm_constdata_t srcPtr, rpm_count_t cnt, int dataLength)
1350 case RPM_STRING_ARRAY_TYPE:
1351 case RPM_I18NSTRING_TYPE:
1352 { const char ** av = (const char **) srcPtr;
1355 while (cnt-- > 0 && dataLength > 0) {
1357 if ((s = *av++) == NULL)
1361 } while (s[-1] && --dataLength > 0);
1366 memmove(dstPtr, srcPtr, dataLength);
1372 * Return (malloc'ed) copy of entry data.
1373 * @param type entry data type
1374 * @param p entry data
1375 * @param c entry item count
1376 * @retval lengthPtr no. bytes in returned data
1377 * @return (malloc'ed) copy of entry data, NULL on error
1380 grabData(rpm_tagtype_t type, rpm_constdata_t p, rpm_count_t c, int * lengthPtr)
1382 rpm_data_t data = NULL;
1385 length = dataLength(type, p, c, 0, NULL);
1387 data = xmalloc(length);
1388 copyData(type, data, p, c, length);
1392 *lengthPtr = length;
1396 static int intAddEntry(Header h, rpmtd td)
1402 /* Count must always be >= 1 for headerAddEntry. */
1406 if (hdrchkType(td->type))
1408 if (hdrchkData(td->count))
1412 data = grabData(td->type, td->data, td->count, &length);
1413 if (data == NULL || length <= 0)
1416 /* Allocate more index space if necessary */
1417 if (h->indexUsed == h->indexAlloced) {
1418 h->indexAlloced += INDEX_MALLOC_SIZE;
1419 h->index = xrealloc(h->index, h->indexAlloced * sizeof(*h->index));
1422 /* Fill in the index */
1423 entry = h->index + h->indexUsed;
1424 entry->info.tag = td->tag;
1425 entry->info.type = td->type;
1426 entry->info.count = td->count;
1427 entry->info.offset = 0;
1429 entry->length = length;
1431 if (h->indexUsed > 0 && td->tag < h->index[h->indexUsed-1].info.tag)
1432 h->flags &= ~HEADERFLAG_SORTED;
1438 static int intAppendEntry(Header h, rpmtd td)
1443 if (td->type == RPM_STRING_TYPE || td->type == RPM_I18NSTRING_TYPE) {
1444 /* we can't do this */
1448 /* Find the tag entry in the header. */
1449 entry = findEntry(h, td->tag, td->type);
1453 length = dataLength(td->type, td->data, td->count, 0, NULL);
1457 if (ENTRY_IN_REGION(entry)) {
1458 char * t = xmalloc(entry->length + length);
1459 memcpy(t, entry->data, entry->length);
1461 entry->info.offset = 0;
1463 entry->data = xrealloc(entry->data, entry->length + length);
1465 copyData(td->type, ((char *) entry->data) + entry->length,
1466 td->data, td->count, length);
1468 entry->length += length;
1470 entry->info.count += td->count;
1475 int headerPut(Header h, rpmtd td, headerPutFlags flags)
1480 if (flags & HEADERPUT_APPEND) {
1481 rc = findEntry(h, td->tag, td->type) ?
1482 intAppendEntry(h, td) :
1485 rc = intAddEntry(h, td);
1490 int headerAddI18NString(Header h, rpmTagVal tag, const char * string,
1493 indexEntry table, entry;
1494 const char ** strArray;
1497 rpm_count_t i, langNum;
1500 table = findEntry(h, RPMTAG_HEADERI18NTABLE, RPM_STRING_ARRAY_TYPE);
1501 entry = findEntry(h, tag, RPM_I18NSTRING_TYPE);
1503 if (!table && entry)
1504 return 0; /* this shouldn't ever happen!! */
1506 if (!table && !entry) {
1507 const char * charArray[2];
1508 rpm_count_t count = 0;
1510 if (!lang || (lang[0] == 'C' && lang[1] == '\0')) {
1511 charArray[count++] = "C";
1513 charArray[count++] = "C";
1514 charArray[count++] = lang;
1518 td.tag = RPMTAG_HEADERI18NTABLE;
1519 td.type = RPM_STRING_ARRAY_TYPE;
1520 td.data = (void *) charArray;
1522 if (!headerPut(h, &td, HEADERPUT_DEFAULT))
1524 table = findEntry(h, RPMTAG_HEADERI18NTABLE, RPM_STRING_ARRAY_TYPE);
1529 if (!lang) lang = "C";
1531 { const char * l = table->data;
1532 for (langNum = 0; langNum < table->info.count; langNum++) {
1533 if (rstreq(l, lang)) break;
1538 if (langNum >= table->info.count) {
1539 length = strlen(lang) + 1;
1540 if (ENTRY_IN_REGION(table)) {
1541 char * t = xmalloc(table->length + length);
1542 memcpy(t, table->data, table->length);
1544 table->info.offset = 0;
1546 table->data = xrealloc(table->data, table->length + length);
1547 memmove(((char *)table->data) + table->length, lang, length);
1548 table->length += length;
1549 table->info.count++;
1555 strArray = xmalloc(sizeof(*strArray) * (langNum + 1));
1556 for (i = 0; i < langNum; i++)
1558 strArray[langNum] = string;
1562 td.type = RPM_I18NSTRING_TYPE;
1564 td.count = langNum + 1;
1565 rc = headerPut(h, &td, HEADERPUT_DEFAULT);
1568 } else if (langNum >= entry->info.count) {
1569 ghosts = langNum - entry->info.count;
1571 length = strlen(string) + 1 + ghosts;
1572 if (ENTRY_IN_REGION(entry)) {
1573 char * t = xmalloc(entry->length + length);
1574 memcpy(t, entry->data, entry->length);
1576 entry->info.offset = 0;
1578 entry->data = xrealloc(entry->data, entry->length + length);
1580 memset(((char *)entry->data) + entry->length, '\0', ghosts);
1581 memmove(((char *)entry->data) + entry->length + ghosts, string, strlen(string)+1);
1583 entry->length += length;
1584 entry->info.count = langNum + 1;
1586 char *b, *be, *e, *ee, *t;
1589 /* Set beginning/end pointers to previous data */
1590 b = be = e = ee = entry->data;
1591 for (i = 0; i < table->info.count; i++) {
1594 ee += strlen(ee) + 1;
1599 /* Get storage for new buffer */
1601 sn = strlen(string) + 1;
1603 length = bn + sn + en;
1604 t = buf = xmalloc(length);
1606 /* Copy values into new storage */
1609 memcpy(t, string, sn);
1614 /* Replace i18N string array */
1615 entry->length -= strlen(be) + 1;
1616 entry->length += sn;
1618 if (ENTRY_IN_REGION(entry)) {
1619 entry->info.offset = 0;
1621 entry->data = _free(entry->data);
1628 int headerMod(Header h, rpmtd td)
1635 /* First find the tag */
1636 entry = findEntry(h, td->tag, td->type);
1641 data = grabData(td->type, td->data, td->count, &length);
1642 if (data == NULL || length <= 0)
1645 /* make sure entry points to the first occurence of this tag */
1646 while (entry > h->index && (entry - 1)->info.tag == td->tag)
1649 /* free after we've grabbed the new data in case the two are intertwined;
1650 that's a bad idea but at least we won't break */
1651 oldData = entry->data;
1653 entry->info.count = td->count;
1654 entry->info.type = td->type;
1656 entry->length = length;
1658 if (ENTRY_IN_REGION(entry)) {
1659 entry->info.offset = 0;
1661 oldData = _free(oldData);
1667 * Header tag iterator data structure.
1669 struct headerIterator_s {
1670 Header h; /*!< Header being iterated. */
1671 int next_index; /*!< Next tag index. */
1674 HeaderIterator headerFreeIterator(HeaderIterator hi)
1677 hi->h = headerFree(hi->h);
1683 HeaderIterator headerInitIterator(Header h)
1685 HeaderIterator hi = xmalloc(sizeof(*hi));
1689 hi->h = headerLink(h);
1694 static indexEntry nextIndex(HeaderIterator hi)
1698 indexEntry entry = NULL;
1700 for (slot = hi->next_index; slot < h->indexUsed; slot++) {
1701 entry = h->index + slot;
1702 if (!ENTRY_IS_REGION(entry))
1705 hi->next_index = slot;
1706 if (entry == NULL || slot >= h->indexUsed)
1713 rpmTagVal headerNextTag(HeaderIterator hi)
1715 indexEntry entry = nextIndex(hi);
1716 return entry ? entry->info.tag : RPMTAG_NOT_FOUND;
1719 int headerNext(HeaderIterator hi, rpmtd td)
1721 indexEntry entry = nextIndex(hi);
1726 td->tag = entry->info.tag;
1727 rc = copyTdEntry(entry, td, HEADERGET_DEFAULT);
1729 return ((rc == 1) ? 1 : 0);
1732 unsigned int headerGetInstance(Header h)
1734 return h ? h->instance : 0;
1737 void headerSetInstance(Header h, unsigned int instance)
1739 h->instance = instance;