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 if ((entry != NULL && regionid >= 0) || (entry == NULL && regionid != 0))
387 for (; il > 0; il--, pe++) {
388 struct indexEntry_s ie;
391 ie.info.tag = ntohl(pe->tag);
392 ie.info.type = ntohl(pe->type);
393 ie.info.count = ntohl(pe->count);
394 ie.info.offset = ntohl(pe->offset);
396 if (hdrchkType(ie.info.type))
398 if (hdrchkData(ie.info.count))
400 if (hdrchkData(ie.info.offset))
402 if (hdrchkAlign(ie.info.type, ie.info.offset))
405 ie.data = dataStart + ie.info.offset;
406 if (dataEnd && (unsigned char *)ie.data >= dataEnd)
409 ie.length = dataLength(ie.info.type, ie.data, ie.info.count, 1, dataEnd);
410 if (ie.length < 0 || hdrchkData(ie.length))
416 ie.info.offset = regionid;
417 *entry = ie; /* structure assignment */
423 if (typeSizes[type] > 1) {
424 unsigned diff = typeSizes[type] - (dl % typeSizes[type]);
425 if (diff != typeSizes[type]) {
430 /* Perform endian conversions */
431 switch (ntohl(pe->type)) {
433 { uint64_t * it = ie.data;
434 for (; ie.info.count > 0; ie.info.count--, it += 1) {
435 if (dataEnd && ((unsigned char *)it) >= dataEnd)
441 { int32_t * it = ie.data;
442 for (; ie.info.count > 0; ie.info.count--, it += 1) {
443 if (dataEnd && ((unsigned char *)it) >= dataEnd)
449 { int16_t * it = ie.data;
450 for (; ie.info.count > 0; ie.info.count--, it += 1) {
451 if (dataEnd && ((unsigned char *)it) >= dataEnd)
467 * @retval *lengthPtr no. bytes in unloaded header blob
468 * @return unloaded header blob (NULL on error)
470 static void * doHeaderUnload(Header h, size_t * lengthPtr)
482 int drlen, ndribbles;
484 if (h == NULL) return NULL;
486 /* Sort entries by (offset,tag). */
489 /* Compute (il,dl) for all tags, including those deleted in region. */
490 drlen = ndribbles = 0;
491 for (i = 0, entry = h->index; i < h->indexUsed; i++, entry++) {
492 if (ENTRY_IS_REGION(entry)) {
493 int32_t rdl = -entry->info.offset; /* negative offset */
494 int32_t ril = rdl/sizeof(*pe);
495 int rid = entry->info.offset;
498 dl += entry->rdlen + entry->info.count;
499 /* XXX Legacy regions do not include the region tag and data. */
500 if (i == 0 && (h->flags & HEADERFLAG_LEGACY))
503 /* Skip rest of entries in region, but account for dribbles. */
504 for (; i < h->indexUsed && entry->info.offset <= rid+1; i++, entry++) {
505 if (entry->info.offset <= rid)
509 type = entry->info.type;
510 if (typeSizes[type] > 1) {
511 unsigned diff = typeSizes[type] - (dl % typeSizes[type]);
512 if (diff != typeSizes[type]) {
520 drlen += entry->length;
528 /* Ignore deleted drips. */
529 if (entry->data == NULL || entry->length <= 0)
533 type = entry->info.type;
534 if (typeSizes[type] > 1) {
535 unsigned diff = typeSizes[type] - (dl % typeSizes[type]);
536 if (diff != typeSizes[type]) {
545 /* Sanity checks on header intro. */
546 if (hdrchkTags(il) || hdrchkData(dl))
549 len = sizeof(il) + sizeof(dl) + (il * sizeof(*pe)) + dl;
555 pe = (entryInfo) &ei[2];
556 dataStart = te = (char *) (pe + il);
558 for (i = 0, entry = h->index; i < h->indexUsed; i++, entry++) {
564 if (entry->data == NULL || entry->length <= 0)
567 t = (unsigned char*)te;
568 pe->tag = htonl(entry->info.tag);
569 pe->type = htonl(entry->info.type);
570 pe->count = htonl(entry->info.count);
572 if (ENTRY_IS_REGION(entry)) {
573 int32_t rdl = -entry->info.offset; /* negative offset */
574 int32_t ril = rdl/sizeof(*pe) + ndribbles;
575 int rid = entry->info.offset;
577 src = (char *)entry->data;
578 rdlen = entry->rdlen;
580 /* XXX Legacy regions do not include the region tag and data. */
581 if (i == 0 && (h->flags & HEADERFLAG_LEGACY)) {
584 memcpy(pe+1, src, rdl);
585 memcpy(te, src + rdl, rdlen);
588 pe->offset = htonl(te - dataStart);
591 stei[2] = htonl(-rdl-entry->info.count);
593 memcpy(te, stei, entry->info.count);
594 te += entry->info.count;
596 rdlen += entry->info.count;
598 count = regionSwab(NULL, ril, 0, pe, t, NULL, 0);
604 memcpy(pe+1, src + sizeof(*pe), ((ril-1) * sizeof(*pe)));
605 memcpy(te, src + (ril * sizeof(*pe)), rdlen+entry->info.count+drlen);
608 entryInfo se = (entryInfo)src;
609 int off = ntohl(se->offset);
610 pe->offset = (off) ? htonl(te - dataStart) : htonl(off);
612 te += entry->info.count + drlen;
614 count = regionSwab(NULL, ril, 0, pe, t, NULL, 0);
615 if (count != (rdlen + entry->info.count + drlen))
619 /* Skip rest of entries in region. */
620 while (i < h->indexUsed && entry->info.offset <= rid+1) {
630 /* Ignore deleted drips. */
631 if (entry->data == NULL || entry->length <= 0)
635 type = entry->info.type;
636 if (typeSizes[type] > 1) {
638 diff = typeSizes[type] - ((te - dataStart) % typeSizes[type]);
639 if (diff != typeSizes[type]) {
645 pe->offset = htonl(te - dataStart);
647 /* copy data w/ endian conversions */
648 switch (entry->info.type) {
650 count = entry->info.count;
653 *((uint64_t *)te) = htonll(*((uint64_t *)src));
654 te += sizeof(uint64_t);
655 src += sizeof(uint64_t);
660 count = entry->info.count;
663 *((int32_t *)te) = htonl(*((int32_t *)src));
664 te += sizeof(int32_t);
665 src += sizeof(int32_t);
670 count = entry->info.count;
673 *((int16_t *)te) = htons(*((int16_t *)src));
674 te += sizeof(int16_t);
675 src += sizeof(int16_t);
680 memcpy(te, entry->data, entry->length);
687 /* Insure that there are no memcpy underruns/overruns. */
688 if (((char *)pe) != dataStart)
690 if ((((char *)ei)+len) != te)
705 void * headerUnload(Header h)
707 return doHeaderUnload(h, NULL);
711 * Find matching (tag,type) entry in header.
713 * @param tag entry tag
714 * @param type entry type
715 * @return header entry
718 indexEntry findEntry(Header h, rpmTagVal tag, rpm_tagtype_t type)
721 struct indexEntry_s key;
723 if (h == NULL) return NULL;
724 if (!(h->flags & HEADERFLAG_SORTED)) headerSort(h);
728 entry = bsearch(&key, h->index, h->indexUsed, sizeof(*h->index), indexCmp);
732 if (type == RPM_NULL_TYPE)
736 while (entry->info.tag == tag && entry->info.type != type &&
737 entry > h->index) entry--;
739 if (entry->info.tag == tag && entry->info.type == type)
745 int headerDel(Header h, rpmTagVal tag)
747 indexEntry last = h->index + h->indexUsed;
748 indexEntry entry, first;
751 entry = findEntry(h, tag, RPM_NULL_TYPE);
752 if (!entry) return 1;
754 /* Make sure entry points to the first occurence of this tag. */
755 while (entry > h->index && (entry - 1)->info.tag == tag)
758 /* Free data for tags being removed. */
759 for (first = entry; first < last; first++) {
761 if (first->info.tag != tag)
766 if (ENTRY_IN_REGION(first))
771 ne = (first - entry);
776 memmove(entry, first, (ne * sizeof(*entry)));
782 Header headerLoad(void * uh)
784 int32_t * ei = (int32_t *) uh;
785 int32_t il = ntohl(ei[0]); /* index length */
786 int32_t dl = ntohl(ei[1]); /* data length */
787 size_t pvlen = sizeof(il) + sizeof(dl) +
788 (il * sizeof(struct entryInfo_s)) + dl;
792 unsigned char * dataStart;
793 unsigned char * dataEnd;
797 /* Sanity checks on header intro. */
798 if (hdrchkTags(il) || hdrchkData(dl))
802 pe = (entryInfo) &ei[2];
803 dataStart = (unsigned char *) (pe + il);
804 dataEnd = dataStart + dl;
806 h = headerCreate(uh, il);
809 if (!(htonl(pe->tag) < RPMTAG_HEADERI18NTABLE)) {
810 h->flags |= HEADERFLAG_LEGACY;
811 entry->info.type = REGION_TAG_TYPE;
812 entry->info.tag = RPMTAG_HEADERIMAGE;
813 entry->info.count = REGION_TAG_COUNT;
814 entry->info.offset = ((unsigned char *)pe - dataStart); /* negative offset */
817 entry->length = pvlen - sizeof(il) - sizeof(dl);
818 rdlen = regionSwab(entry+1, il, 0, pe, dataStart, dataEnd, entry->info.offset);
821 entry->rdlen = rdlen;
827 h->flags &= ~HEADERFLAG_LEGACY;
829 entry->info.type = htonl(pe->type);
830 entry->info.count = htonl(pe->count);
832 if (hdrchkType(entry->info.type))
834 if (hdrchkTags(entry->info.count))
837 { int off = ntohl(pe->offset);
842 size_t nb = REGION_TAG_COUNT;
844 /* XXX Hmm, why the copy? */
845 memcpy(&stei, dataStart + off, nb);
846 rdl = -ntohl(stei[2]); /* negative offset */
847 ril = rdl/sizeof(*pe);
848 if (hdrchkTags(ril) || hdrchkData(rdl))
850 entry->info.tag = htonl(pe->tag);
853 rdl = (ril * sizeof(struct entryInfo_s));
854 entry->info.tag = RPMTAG_HEADERIMAGE;
857 entry->info.offset = -rdl; /* negative offset */
860 entry->length = pvlen - sizeof(il) - sizeof(dl);
861 rdlen = regionSwab(entry+1, ril-1, 0, pe+1, dataStart, dataEnd, entry->info.offset);
864 entry->rdlen = rdlen;
866 if (ril < h->indexUsed) {
867 indexEntry newEntry = entry + ril;
868 int ne = (h->indexUsed - ril);
869 int rid = entry->info.offset+1;
872 /* Load dribble entries from region. */
873 rc = regionSwab(newEntry, ne, 0, pe+ril, dataStart, dataEnd, rid);
878 { indexEntry firstEntry = newEntry;
879 int save = h->indexUsed;
882 /* Dribble entries replace duplicate region entries. */
884 for (j = 0; j < ne; j++, newEntry++) {
885 (void) headerDel(h, newEntry->info.tag);
886 if (newEntry->info.tag == RPMTAG_BASENAMES)
887 (void) headerDel(h, RPMTAG_OLDFILENAMES);
890 /* If any duplicate entries were replaced, move new entries down. */
891 if (h->indexUsed < (save - ne)) {
892 memmove(h->index + h->indexUsed, firstEntry,
893 (ne * sizeof(*entry)));
900 h->flags &= ~HEADERFLAG_SORTED;
902 h->flags |= HEADERFLAG_ALLOCATED;
914 Header headerReload(Header h, rpmTagVal tag)
917 void * uh = doHeaderUnload(h, NULL);
927 if (ENTRY_IS_REGION(nh->index)) {
928 if (tag == RPMTAG_HEADERSIGNATURES || tag == RPMTAG_HEADERIMMUTABLE)
929 nh->index[0].info.tag = tag;
934 Header headerCopyLoad(const void * uh)
936 int32_t * ei = (int32_t *) uh;
937 int32_t il = ntohl(ei[0]); /* index length */
938 int32_t dl = ntohl(ei[1]); /* data length */
939 size_t pvlen = sizeof(il) + sizeof(dl) +
940 (il * sizeof(struct entryInfo_s)) + dl;
943 /* Sanity checks on header intro. */
944 if (!(hdrchkTags(il) || hdrchkData(dl)) && pvlen < headerMaxbytes) {
945 void * nuh = memcpy(xmalloc(pvlen), uh, pvlen);
946 if ((h = headerLoad(nuh)) == NULL)
952 Header headerRead(FD_t fd, int magicp)
961 if (magicp == HEADER_MAGIC_YES) {
964 if (Fread(block, 1, 4*sizeof(*block), fd) != 4*sizeof(*block))
969 if (memcmp(&magic, rpm_header_magic, sizeof(magic)))
972 il = ntohl(block[2]);
973 dl = ntohl(block[3]);
975 if (Fread(block, 1, 2*sizeof(*block), fd) != 2*sizeof(*block))
978 il = ntohl(block[0]);
979 dl = ntohl(block[1]);
982 len = sizeof(il) + sizeof(dl) + (il * sizeof(struct entryInfo_s)) + dl;
984 /* Sanity checks on header intro. */
985 if (hdrchkTags(il) || hdrchkData(dl) || len > headerMaxbytes)
991 len -= sizeof(il) + sizeof(dl);
993 if (Fread((char *)&ei[2], 1, len, fd) != len)
999 if (h == NULL && ei != NULL) {
1005 int headerWrite(FD_t fd, Header h, int magicp)
1011 uh = doHeaderUnload(h, &length);
1015 case HEADER_MAGIC_YES:
1016 nb = Fwrite(rpm_header_magic, sizeof(uint8_t), sizeof(rpm_header_magic), fd);
1017 if (nb != sizeof(rpm_header_magic))
1020 case HEADER_MAGIC_NO:
1024 nb = Fwrite(uh, sizeof(char), length, fd);
1028 return (nb == length ? 0 : 1);
1031 int headerIsEntry(Header h, rpmTagVal tag)
1033 /* FIX: h modified by sort. */
1034 return (findEntry(h, tag, RPM_NULL_TYPE) ? 1 : 0);
1039 * Retrieve data from header entry.
1040 * Relevant flags (others are ignored), if neither is set allocation
1041 * behavior depends on data type(!)
1042 * HEADERGET_MINMEM: return pointers to header memory
1043 * HEADERGET_ALLOC: always return malloced memory, overrides MINMEM
1045 * @todo Permit retrieval of regions other than HEADER_IMUTABLE.
1046 * @param entry header entry
1047 * @param td tag data container
1048 * @param minMem string pointers refer to header memory?
1049 * @param flags flags to control memory allocation
1050 * @return 1 on success, otherwise error.
1052 static int copyTdEntry(const indexEntry entry, rpmtd td, headerGetFlags flags)
1054 rpm_count_t count = entry->info.count;
1055 int rc = 1; /* XXX 1 on success. */
1056 /* ALLOC overrides MINMEM */
1057 int allocMem = flags & HEADERGET_ALLOC;
1058 int minMem = allocMem ? 0 : flags & HEADERGET_MINMEM;
1059 int argvArray = (flags & HEADERGET_ARGV) ? 1 : 0;
1062 td->flags = RPMTD_IMMUTABLE;
1063 switch (entry->info.type) {
1066 * XXX This only works for
1067 * XXX "sealed" HEADER_IMMUTABLE/HEADER_SIGNATURES/HEADER_IMAGE.
1068 * XXX This will *not* work for unsealed legacy HEADER_IMAGE (i.e.
1069 * XXX a legacy header freshly read, but not yet unloaded to the rpmdb).
1071 if (ENTRY_IS_REGION(entry)) {
1072 int32_t * ei = ((int32_t *)entry->data) - 2;
1073 entryInfo pe = (entryInfo) (ei + 2);
1074 unsigned char * dataStart = (unsigned char *) (pe + ntohl(ei[0]));
1075 int32_t rdl = -entry->info.offset; /* negative offset */
1076 int32_t ril = rdl/sizeof(*pe);
1079 count = 2 * sizeof(*ei) + (ril * sizeof(*pe)) + rdl;
1080 if (entry->info.tag == RPMTAG_HEADERIMAGE) {
1084 count += REGION_TAG_COUNT;
1085 rdl += REGION_TAG_COUNT;
1088 td->data = xmalloc(count);
1089 ei = (int32_t *) td->data;
1093 pe = (entryInfo) memcpy(ei + 2, pe, (ril * sizeof(*pe)));
1095 dataStart = (unsigned char *) memcpy(pe + ril, dataStart, rdl);
1097 rc = regionSwab(NULL, ril, 0, pe, dataStart, dataStart + rdl, 0);
1098 /* don't return data on failure */
1100 td->data = _free(td->data);
1102 /* XXX 1 on success. */
1103 rc = (rc < 0) ? 0 : 1;
1105 count = entry->length;
1107 ? memcpy(xmalloc(count), entry->data, count)
1111 case RPM_STRING_TYPE:
1112 /* simple string, but fallthrough if its actually an array */
1113 if (count == 1 && !argvArray) {
1114 td->data = allocMem ? xstrdup(entry->data) : entry->data;
1117 case RPM_STRING_ARRAY_TYPE:
1118 case RPM_I18NSTRING_TYPE:
1119 { const char ** ptrEntry;
1120 int tableSize = (count + argvArray) * sizeof(char *);
1125 td->data = xmalloc(tableSize);
1126 ptrEntry = (const char **) td->data;
1129 t = xmalloc(tableSize + entry->length);
1130 td->data = (void *)t;
1131 ptrEntry = (const char **) td->data;
1133 memcpy(t, entry->data, entry->length);
1135 for (i = 0; i < count; i++) {
1142 td->flags |= RPMTD_ARGV;
1147 case RPM_INT16_TYPE:
1148 case RPM_INT32_TYPE:
1149 case RPM_INT64_TYPE:
1151 td->data = xmalloc(entry->length);
1152 memcpy(td->data, entry->data, entry->length);
1154 td->data = entry->data;
1158 /* WTH? Don't mess with unknown data types... */
1163 td->type = entry->info.type;
1166 if (td->data && entry->data != td->data) {
1167 td->flags |= RPMTD_ALLOCED;
1174 * Does locale match entry in header i18n table?
1177 * The range [l,le) contains the next locale to match:
1178 * ll[_CC][.EEEEE][@dddd]
1180 * ll ISO language code (in lowercase).
1181 * CC (optional) ISO coutnry code (in uppercase).
1182 * EEEEE (optional) encoding (not really standardized).
1183 * dddd (optional) dialect.
1186 * @param td header i18n table data, NUL terminated
1187 * @param l start of locale to match
1188 * @param le end of locale to match
1189 * @return 1 on good match, 2 on weak match, 0 on no match
1191 static int headerMatchLocale(const char *td, const char *l, const char *le)
1195 /* First try a complete match. */
1196 if (strlen(td) == (le-l) && rstreqn(td, l, (le - l)))
1199 /* Next, try stripping optional dialect and matching. */
1200 for (fe = l; fe < le && *fe != '@'; fe++)
1202 if (fe < le && rstreqn(td, l, (fe - l)))
1205 /* Next, try stripping optional codeset and matching. */
1206 for (fe = l; fe < le && *fe != '.'; fe++)
1208 if (fe < le && rstreqn(td, l, (fe - l)))
1211 /* Finally, try stripping optional country code and matching. */
1212 for (fe = l; fe < le && *fe != '_'; fe++)
1214 if (fe < le && rstreqn(td, l, (fe - l)))
1221 * Return i18n string from header that matches locale.
1223 * @param entry i18n string data
1224 * @retval td tag data container
1225 * @param flags flags to control allocation
1228 static int copyI18NEntry(Header h, indexEntry entry, rpmtd td,
1229 headerGetFlags flags)
1231 const char *lang, *l, *le;
1234 td->type = RPM_STRING_TYPE;
1236 /* if no match, just return the first string */
1237 td->data = entry->data;
1239 /* XXX Drepper sez' this is the order. */
1240 if ((lang = getenv("LANGUAGE")) == NULL &&
1241 (lang = getenv("LC_ALL")) == NULL &&
1242 (lang = getenv("LC_MESSAGES")) == NULL &&
1243 (lang = getenv("LANG")) == NULL)
1246 if ((table = findEntry(h, RPMTAG_HEADERI18NTABLE, RPM_STRING_ARRAY_TYPE)) == NULL)
1249 for (l = lang; *l != '\0'; l = le) {
1251 char *ed, *ed_weak = NULL;
1254 while (*l && *l == ':') /* skip leading colons */
1258 for (le = l; *le && *le != ':'; le++) /* find end of this locale */
1261 /* For each entry in the header ... */
1262 for (langNum = 0, t = table->data, ed = entry->data;
1263 langNum < entry->info.count;
1264 langNum++, t += strlen(t) + 1, ed += strlen(ed) + 1) {
1266 int match = headerMatchLocale(t, l, le);
1270 } else if (match == 2) {
1281 if (flags & HEADERGET_ALLOC) {
1282 td->data = xstrdup(td->data);
1283 td->flags |= RPMTD_ALLOCED;
1290 * Retrieve tag data from header.
1292 * @retval td tag data container
1293 * @param flags flags to control retrieval
1294 * @return 1 on success, 0 on not found
1296 static int intGetTdEntry(Header h, rpmtd td, headerGetFlags flags)
1301 /* First find the tag */
1302 /* FIX: h modified by sort. */
1303 entry = findEntry(h, td->tag, RPM_NULL_TYPE);
1304 if (entry == NULL) {
1305 /* Td is zeroed above, just return... */
1309 if (flags & HEADERGET_RAW) {
1310 rc = copyTdEntry(entry, td, flags);
1312 switch (entry->info.type) {
1313 case RPM_I18NSTRING_TYPE:
1314 rc = copyI18NEntry(h, entry, td, flags);
1317 rc = copyTdEntry(entry, td, flags);
1322 /* XXX 1 on success */
1323 return ((rc == 1) ? 1 : 0);
1326 int headerGet(Header h, rpmTagVal tag, rpmtd td, headerGetFlags flags)
1329 headerTagTagFunction tagfunc = intGetTdEntry;
1331 if (td == NULL) return 0;
1336 if (flags & HEADERGET_EXT) {
1337 headerTagTagFunction extfunc = rpmHeaderTagFunc(tag);
1338 if (extfunc) tagfunc = extfunc;
1340 rc = tagfunc(h, td, flags);
1342 assert(tag == td->tag);
1348 static void copyData(rpm_tagtype_t type, rpm_data_t dstPtr,
1349 rpm_constdata_t srcPtr, rpm_count_t cnt, int dataLength)
1352 case RPM_STRING_ARRAY_TYPE:
1353 case RPM_I18NSTRING_TYPE:
1354 { const char ** av = (const char **) srcPtr;
1357 while (cnt-- > 0 && dataLength > 0) {
1359 if ((s = *av++) == NULL)
1363 } while (s[-1] && --dataLength > 0);
1368 memmove(dstPtr, srcPtr, dataLength);
1374 * Return (malloc'ed) copy of entry data.
1375 * @param type entry data type
1376 * @param p entry data
1377 * @param c entry item count
1378 * @retval lengthPtr no. bytes in returned data
1379 * @return (malloc'ed) copy of entry data, NULL on error
1382 grabData(rpm_tagtype_t type, rpm_constdata_t p, rpm_count_t c, int * lengthPtr)
1384 rpm_data_t data = NULL;
1387 length = dataLength(type, p, c, 0, NULL);
1389 data = xmalloc(length);
1390 copyData(type, data, p, c, length);
1394 *lengthPtr = length;
1398 static int intAddEntry(Header h, rpmtd td)
1404 /* Count must always be >= 1 for headerAddEntry. */
1408 if (hdrchkType(td->type))
1410 if (hdrchkData(td->count))
1414 data = grabData(td->type, td->data, td->count, &length);
1415 if (data == NULL || length <= 0)
1418 /* Allocate more index space if necessary */
1419 if (h->indexUsed == h->indexAlloced) {
1420 h->indexAlloced += INDEX_MALLOC_SIZE;
1421 h->index = xrealloc(h->index, h->indexAlloced * sizeof(*h->index));
1424 /* Fill in the index */
1425 entry = h->index + h->indexUsed;
1426 entry->info.tag = td->tag;
1427 entry->info.type = td->type;
1428 entry->info.count = td->count;
1429 entry->info.offset = 0;
1431 entry->length = length;
1433 if (h->indexUsed > 0 && td->tag < h->index[h->indexUsed-1].info.tag)
1434 h->flags &= ~HEADERFLAG_SORTED;
1440 static int intAppendEntry(Header h, rpmtd td)
1445 if (td->type == RPM_STRING_TYPE || td->type == RPM_I18NSTRING_TYPE) {
1446 /* we can't do this */
1450 /* Find the tag entry in the header. */
1451 entry = findEntry(h, td->tag, td->type);
1455 length = dataLength(td->type, td->data, td->count, 0, NULL);
1459 if (ENTRY_IN_REGION(entry)) {
1460 char * t = xmalloc(entry->length + length);
1461 memcpy(t, entry->data, entry->length);
1463 entry->info.offset = 0;
1465 entry->data = xrealloc(entry->data, entry->length + length);
1467 copyData(td->type, ((char *) entry->data) + entry->length,
1468 td->data, td->count, length);
1470 entry->length += length;
1472 entry->info.count += td->count;
1477 int headerPut(Header h, rpmtd td, headerPutFlags flags)
1482 if (flags & HEADERPUT_APPEND) {
1483 rc = findEntry(h, td->tag, td->type) ?
1484 intAppendEntry(h, td) :
1487 rc = intAddEntry(h, td);
1492 int headerAddI18NString(Header h, rpmTagVal tag, const char * string,
1495 indexEntry table, entry;
1496 const char ** strArray;
1499 rpm_count_t i, langNum;
1502 table = findEntry(h, RPMTAG_HEADERI18NTABLE, RPM_STRING_ARRAY_TYPE);
1503 entry = findEntry(h, tag, RPM_I18NSTRING_TYPE);
1505 if (!table && entry)
1506 return 0; /* this shouldn't ever happen!! */
1508 if (!table && !entry) {
1509 const char * charArray[2];
1510 rpm_count_t count = 0;
1512 if (!lang || (lang[0] == 'C' && lang[1] == '\0')) {
1513 charArray[count++] = "C";
1515 charArray[count++] = "C";
1516 charArray[count++] = lang;
1520 td.tag = RPMTAG_HEADERI18NTABLE;
1521 td.type = RPM_STRING_ARRAY_TYPE;
1522 td.data = (void *) charArray;
1524 if (!headerPut(h, &td, HEADERPUT_DEFAULT))
1526 table = findEntry(h, RPMTAG_HEADERI18NTABLE, RPM_STRING_ARRAY_TYPE);
1531 if (!lang) lang = "C";
1533 { const char * l = table->data;
1534 for (langNum = 0; langNum < table->info.count; langNum++) {
1535 if (rstreq(l, lang)) break;
1540 if (langNum >= table->info.count) {
1541 length = strlen(lang) + 1;
1542 if (ENTRY_IN_REGION(table)) {
1543 char * t = xmalloc(table->length + length);
1544 memcpy(t, table->data, table->length);
1546 table->info.offset = 0;
1548 table->data = xrealloc(table->data, table->length + length);
1549 memmove(((char *)table->data) + table->length, lang, length);
1550 table->length += length;
1551 table->info.count++;
1557 strArray = xmalloc(sizeof(*strArray) * (langNum + 1));
1558 for (i = 0; i < langNum; i++)
1560 strArray[langNum] = string;
1564 td.type = RPM_I18NSTRING_TYPE;
1566 td.count = langNum + 1;
1567 rc = headerPut(h, &td, HEADERPUT_DEFAULT);
1570 } else if (langNum >= entry->info.count) {
1571 ghosts = langNum - entry->info.count;
1573 length = strlen(string) + 1 + ghosts;
1574 if (ENTRY_IN_REGION(entry)) {
1575 char * t = xmalloc(entry->length + length);
1576 memcpy(t, entry->data, entry->length);
1578 entry->info.offset = 0;
1580 entry->data = xrealloc(entry->data, entry->length + length);
1582 memset(((char *)entry->data) + entry->length, '\0', ghosts);
1583 memmove(((char *)entry->data) + entry->length + ghosts, string, strlen(string)+1);
1585 entry->length += length;
1586 entry->info.count = langNum + 1;
1588 char *b, *be, *e, *ee, *t;
1591 /* Set beginning/end pointers to previous data */
1592 b = be = e = ee = entry->data;
1593 for (i = 0; i < table->info.count; i++) {
1596 ee += strlen(ee) + 1;
1601 /* Get storage for new buffer */
1603 sn = strlen(string) + 1;
1605 length = bn + sn + en;
1606 t = buf = xmalloc(length);
1608 /* Copy values into new storage */
1611 memcpy(t, string, sn);
1616 /* Replace i18N string array */
1617 entry->length -= strlen(be) + 1;
1618 entry->length += sn;
1620 if (ENTRY_IN_REGION(entry)) {
1621 entry->info.offset = 0;
1623 entry->data = _free(entry->data);
1630 int headerMod(Header h, rpmtd td)
1637 /* First find the tag */
1638 entry = findEntry(h, td->tag, td->type);
1643 data = grabData(td->type, td->data, td->count, &length);
1644 if (data == NULL || length <= 0)
1647 /* make sure entry points to the first occurence of this tag */
1648 while (entry > h->index && (entry - 1)->info.tag == td->tag)
1651 /* free after we've grabbed the new data in case the two are intertwined;
1652 that's a bad idea but at least we won't break */
1653 oldData = entry->data;
1655 entry->info.count = td->count;
1656 entry->info.type = td->type;
1658 entry->length = length;
1660 if (ENTRY_IN_REGION(entry)) {
1661 entry->info.offset = 0;
1669 * Header tag iterator data structure.
1671 struct headerIterator_s {
1672 Header h; /*!< Header being iterated. */
1673 int next_index; /*!< Next tag index. */
1676 HeaderIterator headerFreeIterator(HeaderIterator hi)
1679 hi->h = headerFree(hi->h);
1685 HeaderIterator headerInitIterator(Header h)
1687 HeaderIterator hi = xmalloc(sizeof(*hi));
1691 hi->h = headerLink(h);
1696 static indexEntry nextIndex(HeaderIterator hi)
1700 indexEntry entry = NULL;
1702 for (slot = hi->next_index; slot < h->indexUsed; slot++) {
1703 entry = h->index + slot;
1704 if (!ENTRY_IS_REGION(entry))
1707 hi->next_index = slot;
1708 if (entry == NULL || slot >= h->indexUsed)
1715 rpmTagVal headerNextTag(HeaderIterator hi)
1717 indexEntry entry = nextIndex(hi);
1718 return entry ? entry->info.tag : RPMTAG_NOT_FOUND;
1721 int headerNext(HeaderIterator hi, rpmtd td)
1723 indexEntry entry = nextIndex(hi);
1728 td->tag = entry->info.tag;
1729 rc = copyTdEntry(entry, td, HEADERGET_DEFAULT);
1731 return ((rc == 1) ? 1 : 0);
1734 unsigned int headerGetInstance(Header h)
1736 return h ? h->instance : 0;
1739 void headerSetInstance(Header h, unsigned int instance)
1741 h->instance = instance;