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;
300 /* Bounded header string (array) size calculation, return -1 on error */
301 static inline int strtaglen(const char *str, rpm_count_t c, const char *end)
303 const char *start = str;
306 while ((s = memchr(start, '\0', end-start))) {
307 if (--c == 0 || s > end)
311 return (c > 0) ? -1 : (s - str + 1);
315 * Return length of entry data.
316 * @param type entry data type
317 * @param p entry data
318 * @param count entry item count
319 * @param onDisk data is concatenated strings (with NUL's))?
320 * @param pend pointer to end of data (or NULL)
321 * @return no. bytes in data, -1 on failure
323 static int dataLength(rpm_tagtype_t type, rpm_constdata_t p, rpm_count_t count,
324 int onDisk, rpm_constdata_t pend)
327 /* Not all callers supply data end, avoid falling over edge of the world */
328 const char * se = pend ? pend : s + HEADER_DATA_MAX;
332 case RPM_STRING_TYPE:
335 length = strtaglen(s, 1, se);
338 case RPM_STRING_ARRAY_TYPE:
339 case RPM_I18NSTRING_TYPE:
340 /* These are like RPM_STRING_TYPE, except they're *always* an array */
341 /* Compute sum of length of all strings, including nul terminators */
344 length = strtaglen(s, count, se);
346 const char ** av = (const char **)p;
348 /* add one for null termination */
349 length += strlen(*av++) + 1;
355 if (typeSizes[type] == -1)
357 length = typeSizes[(type & 0xf)] * count;
358 if (length < 0 || (se && (s + length) > se))
367 * Swap int32_t and int16_t arrays within header region.
369 * If a header region tag is in the set to be swabbed, as the data for a
370 * a header region is located after all other tag data.
372 * @param entry header entry
373 * @param il no. of entries
374 * @param dl start no. bytes of data
375 * @param pe header physical entry pointer (swapped)
376 * @param dataStart header data start
377 * @param dataEnd header data end
378 * @param regionid region offset
379 * @return no. bytes of data in region, -1 on error
381 static int regionSwab(indexEntry entry, int il, int dl,
383 unsigned char * dataStart,
384 const unsigned char * dataEnd,
387 if ((entry != NULL && regionid >= 0) || (entry == NULL && regionid != 0))
390 for (; il > 0; il--, pe++) {
391 struct indexEntry_s ie;
394 ie.info.tag = ntohl(pe->tag);
395 ie.info.type = ntohl(pe->type);
396 ie.info.count = ntohl(pe->count);
397 ie.info.offset = ntohl(pe->offset);
399 if (hdrchkType(ie.info.type))
401 if (hdrchkData(ie.info.count))
403 if (hdrchkData(ie.info.offset))
405 if (hdrchkAlign(ie.info.type, ie.info.offset))
408 ie.data = dataStart + ie.info.offset;
409 if (dataEnd && (unsigned char *)ie.data >= dataEnd)
412 ie.length = dataLength(ie.info.type, ie.data, ie.info.count, 1, dataEnd);
413 if (ie.length < 0 || hdrchkData(ie.length))
419 ie.info.offset = regionid;
420 *entry = ie; /* structure assignment */
426 if (typeSizes[type] > 1) {
427 unsigned diff = typeSizes[type] - (dl % typeSizes[type]);
428 if (diff != typeSizes[type]) {
433 /* Perform endian conversions */
434 switch (ntohl(pe->type)) {
436 { uint64_t * it = ie.data;
437 for (; ie.info.count > 0; ie.info.count--, it += 1) {
438 if (dataEnd && ((unsigned char *)it) >= dataEnd)
444 { int32_t * it = ie.data;
445 for (; ie.info.count > 0; ie.info.count--, it += 1) {
446 if (dataEnd && ((unsigned char *)it) >= dataEnd)
452 { int16_t * it = ie.data;
453 for (; ie.info.count > 0; ie.info.count--, it += 1) {
454 if (dataEnd && ((unsigned char *)it) >= dataEnd)
470 * @retval *lengthPtr no. bytes in unloaded header blob
471 * @return unloaded header blob (NULL on error)
473 static void * doHeaderUnload(Header h, size_t * lengthPtr)
485 int drlen, ndribbles;
487 if (h == NULL) return NULL;
489 /* Sort entries by (offset,tag). */
492 /* Compute (il,dl) for all tags, including those deleted in region. */
493 drlen = ndribbles = 0;
494 for (i = 0, entry = h->index; i < h->indexUsed; i++, entry++) {
495 if (ENTRY_IS_REGION(entry)) {
496 int32_t rdl = -entry->info.offset; /* negative offset */
497 int32_t ril = rdl/sizeof(*pe);
498 int rid = entry->info.offset;
501 dl += entry->rdlen + entry->info.count;
502 /* XXX Legacy regions do not include the region tag and data. */
503 if (i == 0 && (h->flags & HEADERFLAG_LEGACY))
506 /* Skip rest of entries in region, but account for dribbles. */
507 for (; i < h->indexUsed && entry->info.offset <= rid+1; i++, entry++) {
508 if (entry->info.offset <= rid)
512 type = entry->info.type;
513 if (typeSizes[type] > 1) {
514 unsigned diff = typeSizes[type] - (dl % typeSizes[type]);
515 if (diff != typeSizes[type]) {
523 drlen += entry->length;
531 /* Ignore deleted drips. */
532 if (entry->data == NULL || entry->length <= 0)
536 type = entry->info.type;
537 if (typeSizes[type] > 1) {
538 unsigned diff = typeSizes[type] - (dl % typeSizes[type]);
539 if (diff != typeSizes[type]) {
548 /* Sanity checks on header intro. */
549 if (hdrchkTags(il) || hdrchkData(dl))
552 len = sizeof(il) + sizeof(dl) + (il * sizeof(*pe)) + dl;
558 pe = (entryInfo) &ei[2];
559 dataStart = te = (char *) (pe + il);
561 for (i = 0, entry = h->index; i < h->indexUsed; i++, entry++) {
567 if (entry->data == NULL || entry->length <= 0)
570 t = (unsigned char*)te;
571 pe->tag = htonl(entry->info.tag);
572 pe->type = htonl(entry->info.type);
573 pe->count = htonl(entry->info.count);
575 if (ENTRY_IS_REGION(entry)) {
576 int32_t rdl = -entry->info.offset; /* negative offset */
577 int32_t ril = rdl/sizeof(*pe) + ndribbles;
578 int rid = entry->info.offset;
580 src = (char *)entry->data;
581 rdlen = entry->rdlen;
583 /* XXX Legacy regions do not include the region tag and data. */
584 if (i == 0 && (h->flags & HEADERFLAG_LEGACY)) {
587 memcpy(pe+1, src, rdl);
588 memcpy(te, src + rdl, rdlen);
591 pe->offset = htonl(te - dataStart);
594 stei[2] = htonl(-rdl-entry->info.count);
596 memcpy(te, stei, entry->info.count);
597 te += entry->info.count;
599 rdlen += entry->info.count;
601 count = regionSwab(NULL, ril, 0, pe, t, NULL, 0);
607 memcpy(pe+1, src + sizeof(*pe), ((ril-1) * sizeof(*pe)));
608 memcpy(te, src + (ril * sizeof(*pe)), rdlen+entry->info.count+drlen);
611 entryInfo se = (entryInfo)src;
612 int off = ntohl(se->offset);
613 pe->offset = (off) ? htonl(te - dataStart) : htonl(off);
615 te += entry->info.count + drlen;
617 count = regionSwab(NULL, ril, 0, pe, t, NULL, 0);
618 if (count != (rdlen + entry->info.count + drlen))
622 /* Skip rest of entries in region. */
623 while (i < h->indexUsed && entry->info.offset <= rid+1) {
633 /* Ignore deleted drips. */
634 if (entry->data == NULL || entry->length <= 0)
638 type = entry->info.type;
639 if (typeSizes[type] > 1) {
641 diff = typeSizes[type] - ((te - dataStart) % typeSizes[type]);
642 if (diff != typeSizes[type]) {
648 pe->offset = htonl(te - dataStart);
650 /* copy data w/ endian conversions */
651 switch (entry->info.type) {
653 count = entry->info.count;
656 *((uint64_t *)te) = htonll(*((uint64_t *)src));
657 te += sizeof(uint64_t);
658 src += sizeof(uint64_t);
663 count = entry->info.count;
666 *((int32_t *)te) = htonl(*((int32_t *)src));
667 te += sizeof(int32_t);
668 src += sizeof(int32_t);
673 count = entry->info.count;
676 *((int16_t *)te) = htons(*((int16_t *)src));
677 te += sizeof(int16_t);
678 src += sizeof(int16_t);
683 memcpy(te, entry->data, entry->length);
690 /* Insure that there are no memcpy underruns/overruns. */
691 if (((char *)pe) != dataStart)
693 if ((((char *)ei)+len) != te)
708 void * headerUnload(Header h)
710 return doHeaderUnload(h, NULL);
714 * Find matching (tag,type) entry in header.
716 * @param tag entry tag
717 * @param type entry type
718 * @return header entry
721 indexEntry findEntry(Header h, rpmTagVal tag, rpm_tagtype_t type)
724 struct indexEntry_s key;
726 if (h == NULL) return NULL;
727 if (!(h->flags & HEADERFLAG_SORTED)) headerSort(h);
731 entry = bsearch(&key, h->index, h->indexUsed, sizeof(*h->index), indexCmp);
735 if (type == RPM_NULL_TYPE)
739 while (entry->info.tag == tag && entry->info.type != type &&
740 entry > h->index) entry--;
742 if (entry->info.tag == tag && entry->info.type == type)
748 int headerDel(Header h, rpmTagVal tag)
750 indexEntry last = h->index + h->indexUsed;
751 indexEntry entry, first;
754 entry = findEntry(h, tag, RPM_NULL_TYPE);
755 if (!entry) return 1;
757 /* Make sure entry points to the first occurence of this tag. */
758 while (entry > h->index && (entry - 1)->info.tag == tag)
761 /* Free data for tags being removed. */
762 for (first = entry; first < last; first++) {
764 if (first->info.tag != tag)
769 if (ENTRY_IN_REGION(first))
774 ne = (first - entry);
779 memmove(entry, first, (ne * sizeof(*entry)));
785 Header headerLoad(void * uh)
787 int32_t * ei = (int32_t *) uh;
788 int32_t il = ntohl(ei[0]); /* index length */
789 int32_t dl = ntohl(ei[1]); /* data length */
790 size_t pvlen = sizeof(il) + sizeof(dl) +
791 (il * sizeof(struct entryInfo_s)) + dl;
794 unsigned char * dataStart;
795 unsigned char * dataEnd;
799 /* Sanity checks on header intro. */
800 if (hdrchkTags(il) || hdrchkData(dl))
803 pe = (entryInfo) &ei[2];
804 dataStart = (unsigned char *) (pe + il);
805 dataEnd = dataStart + dl;
807 h = headerCreate(uh, il);
810 if (!(htonl(pe->tag) < RPMTAG_HEADERI18NTABLE)) {
811 h->flags |= HEADERFLAG_LEGACY;
812 entry->info.type = REGION_TAG_TYPE;
813 entry->info.tag = RPMTAG_HEADERIMAGE;
814 entry->info.count = REGION_TAG_COUNT;
815 entry->info.offset = ((unsigned char *)pe - dataStart); /* negative offset */
818 entry->length = pvlen - sizeof(il) - sizeof(dl);
819 rdlen = regionSwab(entry+1, il, 0, pe, dataStart, dataEnd, entry->info.offset);
822 entry->rdlen = rdlen;
828 h->flags &= ~HEADERFLAG_LEGACY;
830 entry->info.type = htonl(pe->type);
831 entry->info.count = htonl(pe->count);
833 if (hdrchkType(entry->info.type))
835 if (hdrchkTags(entry->info.count))
838 { int off = ntohl(pe->offset);
841 size_t nb = REGION_TAG_COUNT;
843 if (hdrchkRange(dl, (off + nb)))
845 /* XXX Hmm, why the copy? */
846 memcpy(&stei, dataStart + off, nb);
847 rdl = -ntohl(stei[2]); /* negative offset */
848 ril = rdl/sizeof(*pe);
849 if (hdrchkTags(ril) || hdrchkData(rdl))
851 entry->info.tag = htonl(pe->tag);
854 rdl = (ril * sizeof(struct entryInfo_s));
855 entry->info.tag = RPMTAG_HEADERIMAGE;
858 entry->info.offset = -rdl; /* negative offset */
861 entry->length = pvlen - sizeof(il) - sizeof(dl);
862 rdlen = regionSwab(entry+1, ril-1, 0, pe+1, dataStart, dataEnd, entry->info.offset);
865 entry->rdlen = rdlen;
867 if (ril < h->indexUsed) {
868 indexEntry newEntry = entry + ril;
869 int ne = (h->indexUsed - ril);
870 int rid = entry->info.offset+1;
872 /* Load dribble entries from region. */
873 rdlen = regionSwab(newEntry, ne, rdlen, pe+ril,
874 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)));
899 rdlen += REGION_TAG_COUNT;
905 h->flags &= ~HEADERFLAG_SORTED;
907 h->flags |= HEADERFLAG_ALLOCATED;
919 Header headerReload(Header h, rpmTagVal tag)
922 void * uh = doHeaderUnload(h, NULL);
932 if (ENTRY_IS_REGION(nh->index)) {
933 if (tag == RPMTAG_HEADERSIGNATURES || tag == RPMTAG_HEADERIMMUTABLE)
934 nh->index[0].info.tag = tag;
939 Header headerCopyLoad(const void * uh)
941 int32_t * ei = (int32_t *) uh;
942 int32_t il = ntohl(ei[0]); /* index length */
943 int32_t dl = ntohl(ei[1]); /* data length */
944 size_t pvlen = sizeof(il) + sizeof(dl) +
945 (il * sizeof(struct entryInfo_s)) + dl;
948 /* Sanity checks on header intro. */
949 if (!(hdrchkTags(il) || hdrchkData(dl)) && pvlen < headerMaxbytes) {
950 void * nuh = memcpy(xmalloc(pvlen), uh, pvlen);
951 if ((h = headerLoad(nuh)) == NULL)
957 Header headerRead(FD_t fd, int magicp)
966 if (magicp == HEADER_MAGIC_YES) {
969 if (Fread(block, 1, 4*sizeof(*block), fd) != 4*sizeof(*block))
974 if (memcmp(&magic, rpm_header_magic, sizeof(magic)))
977 il = ntohl(block[2]);
978 dl = ntohl(block[3]);
980 if (Fread(block, 1, 2*sizeof(*block), fd) != 2*sizeof(*block))
983 il = ntohl(block[0]);
984 dl = ntohl(block[1]);
987 len = sizeof(il) + sizeof(dl) + (il * sizeof(struct entryInfo_s)) + dl;
989 /* Sanity checks on header intro. */
990 if (hdrchkTags(il) || hdrchkData(dl) || len > headerMaxbytes)
996 len -= sizeof(il) + sizeof(dl);
998 if (Fread((char *)&ei[2], 1, len, fd) != len)
1004 if (h == NULL && ei != NULL) {
1010 int headerWrite(FD_t fd, Header h, int magicp)
1016 uh = doHeaderUnload(h, &length);
1020 case HEADER_MAGIC_YES:
1021 nb = Fwrite(rpm_header_magic, sizeof(uint8_t), sizeof(rpm_header_magic), fd);
1022 if (nb != sizeof(rpm_header_magic))
1025 case HEADER_MAGIC_NO:
1029 nb = Fwrite(uh, sizeof(char), length, fd);
1033 return (nb == length ? 0 : 1);
1036 int headerIsEntry(Header h, rpmTagVal tag)
1038 /* FIX: h modified by sort. */
1039 return (findEntry(h, tag, RPM_NULL_TYPE) ? 1 : 0);
1044 * Retrieve data from header entry.
1045 * Relevant flags (others are ignored), if neither is set allocation
1046 * behavior depends on data type(!)
1047 * HEADERGET_MINMEM: return pointers to header memory
1048 * HEADERGET_ALLOC: always return malloced memory, overrides MINMEM
1050 * @todo Permit retrieval of regions other than HEADER_IMUTABLE.
1051 * @param entry header entry
1052 * @param td tag data container
1053 * @param minMem string pointers refer to header memory?
1054 * @param flags flags to control memory allocation
1055 * @return 1 on success, otherwise error.
1057 static int copyTdEntry(const indexEntry entry, rpmtd td, headerGetFlags flags)
1059 rpm_count_t count = entry->info.count;
1060 int rc = 1; /* XXX 1 on success. */
1061 /* ALLOC overrides MINMEM */
1062 int allocMem = flags & HEADERGET_ALLOC;
1063 int minMem = allocMem ? 0 : flags & HEADERGET_MINMEM;
1064 int argvArray = (flags & HEADERGET_ARGV) ? 1 : 0;
1067 td->flags = RPMTD_IMMUTABLE;
1068 switch (entry->info.type) {
1071 * XXX This only works for
1072 * XXX "sealed" HEADER_IMMUTABLE/HEADER_SIGNATURES/HEADER_IMAGE.
1073 * XXX This will *not* work for unsealed legacy HEADER_IMAGE (i.e.
1074 * XXX a legacy header freshly read, but not yet unloaded to the rpmdb).
1076 if (ENTRY_IS_REGION(entry)) {
1077 int32_t * ei = ((int32_t *)entry->data) - 2;
1078 entryInfo pe = (entryInfo) (ei + 2);
1079 unsigned char * dataStart = (unsigned char *) (pe + ntohl(ei[0]));
1080 int32_t rdl = -entry->info.offset; /* negative offset */
1081 int32_t ril = rdl/sizeof(*pe);
1084 count = 2 * sizeof(*ei) + (ril * sizeof(*pe)) + rdl;
1085 if (entry->info.tag == RPMTAG_HEADERIMAGE) {
1089 count += REGION_TAG_COUNT;
1090 rdl += REGION_TAG_COUNT;
1093 td->data = xmalloc(count);
1094 ei = (int32_t *) td->data;
1098 pe = (entryInfo) memcpy(ei + 2, pe, (ril * sizeof(*pe)));
1100 dataStart = (unsigned char *) memcpy(pe + ril, dataStart, rdl);
1102 rc = regionSwab(NULL, ril, 0, pe, dataStart, dataStart + rdl, 0);
1103 /* don't return data on failure */
1105 td->data = _free(td->data);
1107 /* XXX 1 on success. */
1108 rc = (rc < 0) ? 0 : 1;
1110 count = entry->length;
1112 ? memcpy(xmalloc(count), entry->data, count)
1116 case RPM_STRING_TYPE:
1117 /* simple string, but fallthrough if its actually an array */
1118 if (count == 1 && !argvArray) {
1119 td->data = allocMem ? xstrdup(entry->data) : entry->data;
1122 case RPM_STRING_ARRAY_TYPE:
1123 case RPM_I18NSTRING_TYPE:
1124 { const char ** ptrEntry;
1125 int tableSize = (count + argvArray) * sizeof(char *);
1130 td->data = xmalloc(tableSize);
1131 ptrEntry = (const char **) td->data;
1134 t = xmalloc(tableSize + entry->length);
1135 td->data = (void *)t;
1136 ptrEntry = (const char **) td->data;
1138 memcpy(t, entry->data, entry->length);
1140 for (i = 0; i < count; i++) {
1147 td->flags |= RPMTD_ARGV;
1152 case RPM_INT16_TYPE:
1153 case RPM_INT32_TYPE:
1154 case RPM_INT64_TYPE:
1156 td->data = xmalloc(entry->length);
1157 memcpy(td->data, entry->data, entry->length);
1159 td->data = entry->data;
1163 /* WTH? Don't mess with unknown data types... */
1168 td->type = entry->info.type;
1171 if (td->data && entry->data != td->data) {
1172 td->flags |= RPMTD_ALLOCED;
1179 * Does locale match entry in header i18n table?
1182 * The range [l,le) contains the next locale to match:
1183 * ll[_CC][.EEEEE][@dddd]
1185 * ll ISO language code (in lowercase).
1186 * CC (optional) ISO coutnry code (in uppercase).
1187 * EEEEE (optional) encoding (not really standardized).
1188 * dddd (optional) dialect.
1191 * @param td header i18n table data, NUL terminated
1192 * @param l start of locale to match
1193 * @param le end of locale to match
1194 * @return 1 on good match, 2 on weak match, 0 on no match
1196 static int headerMatchLocale(const char *td, const char *l, const char *le)
1200 /* First try a complete match. */
1201 if (strlen(td) == (le-l) && rstreqn(td, l, (le - l)))
1204 /* Next, try stripping optional dialect and matching. */
1205 for (fe = l; fe < le && *fe != '@'; fe++)
1207 if (fe < le && rstreqn(td, l, (fe - l)))
1210 /* Next, try stripping optional codeset and matching. */
1211 for (fe = l; fe < le && *fe != '.'; fe++)
1213 if (fe < le && rstreqn(td, l, (fe - l)))
1216 /* Finally, try stripping optional country code and matching. */
1217 for (fe = l; fe < le && *fe != '_'; fe++)
1219 if (fe < le && rstreqn(td, l, (fe - l)))
1226 * Return i18n string from header that matches locale.
1228 * @param entry i18n string data
1229 * @retval td tag data container
1230 * @param flags flags to control allocation
1233 static int copyI18NEntry(Header h, indexEntry entry, rpmtd td,
1234 headerGetFlags flags)
1236 const char *lang, *l, *le;
1239 td->type = RPM_STRING_TYPE;
1241 /* if no match, just return the first string */
1242 td->data = entry->data;
1244 /* XXX Drepper sez' this is the order. */
1245 if ((lang = getenv("LANGUAGE")) == NULL &&
1246 (lang = getenv("LC_ALL")) == NULL &&
1247 (lang = getenv("LC_MESSAGES")) == NULL &&
1248 (lang = getenv("LANG")) == NULL)
1251 if ((table = findEntry(h, RPMTAG_HEADERI18NTABLE, RPM_STRING_ARRAY_TYPE)) == NULL)
1254 for (l = lang; *l != '\0'; l = le) {
1256 char *ed, *ed_weak = NULL;
1259 while (*l && *l == ':') /* skip leading colons */
1263 for (le = l; *le && *le != ':'; le++) /* find end of this locale */
1266 /* For each entry in the header ... */
1267 for (langNum = 0, t = table->data, ed = entry->data;
1268 langNum < entry->info.count;
1269 langNum++, t += strlen(t) + 1, ed += strlen(ed) + 1) {
1271 int match = headerMatchLocale(t, l, le);
1275 } else if (match == 2) {
1286 if (flags & HEADERGET_ALLOC) {
1287 td->data = xstrdup(td->data);
1288 td->flags |= RPMTD_ALLOCED;
1295 * Retrieve tag data from header.
1297 * @retval td tag data container
1298 * @param flags flags to control retrieval
1299 * @return 1 on success, 0 on not found
1301 static int intGetTdEntry(Header h, rpmtd td, headerGetFlags flags)
1306 /* First find the tag */
1307 /* FIX: h modified by sort. */
1308 entry = findEntry(h, td->tag, RPM_NULL_TYPE);
1309 if (entry == NULL) {
1310 /* Td is zeroed above, just return... */
1314 if (flags & HEADERGET_RAW) {
1315 rc = copyTdEntry(entry, td, flags);
1317 switch (entry->info.type) {
1318 case RPM_I18NSTRING_TYPE:
1319 rc = copyI18NEntry(h, entry, td, flags);
1322 rc = copyTdEntry(entry, td, flags);
1327 /* XXX 1 on success */
1328 return ((rc == 1) ? 1 : 0);
1331 int headerGet(Header h, rpmTagVal tag, rpmtd td, headerGetFlags flags)
1334 headerTagTagFunction tagfunc = intGetTdEntry;
1336 if (td == NULL) return 0;
1341 if (flags & HEADERGET_EXT) {
1342 headerTagTagFunction extfunc = rpmHeaderTagFunc(tag);
1343 if (extfunc) tagfunc = extfunc;
1345 rc = tagfunc(h, td, flags);
1347 assert(tag == td->tag);
1353 static void copyData(rpm_tagtype_t type, rpm_data_t dstPtr,
1354 rpm_constdata_t srcPtr, rpm_count_t cnt, int dataLength)
1357 case RPM_STRING_ARRAY_TYPE:
1358 case RPM_I18NSTRING_TYPE:
1359 { const char ** av = (const char **) srcPtr;
1362 while (cnt-- > 0 && dataLength > 0) {
1364 if ((s = *av++) == NULL)
1368 } while (s[-1] && --dataLength > 0);
1373 memmove(dstPtr, srcPtr, dataLength);
1379 * Return (malloc'ed) copy of entry data.
1380 * @param type entry data type
1381 * @param p entry data
1382 * @param c entry item count
1383 * @retval lengthPtr no. bytes in returned data
1384 * @return (malloc'ed) copy of entry data, NULL on error
1387 grabData(rpm_tagtype_t type, rpm_constdata_t p, rpm_count_t c, int * lengthPtr)
1389 rpm_data_t data = NULL;
1392 length = dataLength(type, p, c, 0, NULL);
1394 data = xmalloc(length);
1395 copyData(type, data, p, c, length);
1399 *lengthPtr = length;
1403 static int intAddEntry(Header h, rpmtd td)
1409 /* Count must always be >= 1 for headerAddEntry. */
1413 if (hdrchkType(td->type))
1415 if (hdrchkData(td->count))
1419 data = grabData(td->type, td->data, td->count, &length);
1420 if (data == NULL || length <= 0)
1423 /* Allocate more index space if necessary */
1424 if (h->indexUsed == h->indexAlloced) {
1425 h->indexAlloced += INDEX_MALLOC_SIZE;
1426 h->index = xrealloc(h->index, h->indexAlloced * sizeof(*h->index));
1429 /* Fill in the index */
1430 entry = h->index + h->indexUsed;
1431 entry->info.tag = td->tag;
1432 entry->info.type = td->type;
1433 entry->info.count = td->count;
1434 entry->info.offset = 0;
1436 entry->length = length;
1438 if (h->indexUsed > 0 && td->tag < h->index[h->indexUsed-1].info.tag)
1439 h->flags &= ~HEADERFLAG_SORTED;
1445 static int intAppendEntry(Header h, rpmtd td)
1450 if (td->type == RPM_STRING_TYPE || td->type == RPM_I18NSTRING_TYPE) {
1451 /* we can't do this */
1455 /* Find the tag entry in the header. */
1456 entry = findEntry(h, td->tag, td->type);
1460 length = dataLength(td->type, td->data, td->count, 0, NULL);
1464 if (ENTRY_IN_REGION(entry)) {
1465 char * t = xmalloc(entry->length + length);
1466 memcpy(t, entry->data, entry->length);
1468 entry->info.offset = 0;
1470 entry->data = xrealloc(entry->data, entry->length + length);
1472 copyData(td->type, ((char *) entry->data) + entry->length,
1473 td->data, td->count, length);
1475 entry->length += length;
1477 entry->info.count += td->count;
1482 int headerPut(Header h, rpmtd td, headerPutFlags flags)
1487 if (flags & HEADERPUT_APPEND) {
1488 rc = findEntry(h, td->tag, td->type) ?
1489 intAppendEntry(h, td) :
1492 rc = intAddEntry(h, td);
1497 int headerAddI18NString(Header h, rpmTagVal tag, const char * string,
1500 indexEntry table, entry;
1501 const char ** strArray;
1504 rpm_count_t i, langNum;
1507 table = findEntry(h, RPMTAG_HEADERI18NTABLE, RPM_STRING_ARRAY_TYPE);
1508 entry = findEntry(h, tag, RPM_I18NSTRING_TYPE);
1510 if (!table && entry)
1511 return 0; /* this shouldn't ever happen!! */
1513 if (!table && !entry) {
1514 const char * charArray[2];
1515 rpm_count_t count = 0;
1517 if (!lang || (lang[0] == 'C' && lang[1] == '\0')) {
1518 charArray[count++] = "C";
1520 charArray[count++] = "C";
1521 charArray[count++] = lang;
1525 td.tag = RPMTAG_HEADERI18NTABLE;
1526 td.type = RPM_STRING_ARRAY_TYPE;
1527 td.data = (void *) charArray;
1529 if (!headerPut(h, &td, HEADERPUT_DEFAULT))
1531 table = findEntry(h, RPMTAG_HEADERI18NTABLE, RPM_STRING_ARRAY_TYPE);
1536 if (!lang) lang = "C";
1538 { const char * l = table->data;
1539 for (langNum = 0; langNum < table->info.count; langNum++) {
1540 if (rstreq(l, lang)) break;
1545 if (langNum >= table->info.count) {
1546 length = strlen(lang) + 1;
1547 if (ENTRY_IN_REGION(table)) {
1548 char * t = xmalloc(table->length + length);
1549 memcpy(t, table->data, table->length);
1551 table->info.offset = 0;
1553 table->data = xrealloc(table->data, table->length + length);
1554 memmove(((char *)table->data) + table->length, lang, length);
1555 table->length += length;
1556 table->info.count++;
1562 strArray = xmalloc(sizeof(*strArray) * (langNum + 1));
1563 for (i = 0; i < langNum; i++)
1565 strArray[langNum] = string;
1569 td.type = RPM_I18NSTRING_TYPE;
1571 td.count = langNum + 1;
1572 rc = headerPut(h, &td, HEADERPUT_DEFAULT);
1575 } else if (langNum >= entry->info.count) {
1576 ghosts = langNum - entry->info.count;
1578 length = strlen(string) + 1 + ghosts;
1579 if (ENTRY_IN_REGION(entry)) {
1580 char * t = xmalloc(entry->length + length);
1581 memcpy(t, entry->data, entry->length);
1583 entry->info.offset = 0;
1585 entry->data = xrealloc(entry->data, entry->length + length);
1587 memset(((char *)entry->data) + entry->length, '\0', ghosts);
1588 memmove(((char *)entry->data) + entry->length + ghosts, string, strlen(string)+1);
1590 entry->length += length;
1591 entry->info.count = langNum + 1;
1593 char *b, *be, *e, *ee, *t;
1596 /* Set beginning/end pointers to previous data */
1597 b = be = e = ee = entry->data;
1598 for (i = 0; i < table->info.count; i++) {
1601 ee += strlen(ee) + 1;
1606 /* Get storage for new buffer */
1608 sn = strlen(string) + 1;
1610 length = bn + sn + en;
1611 t = buf = xmalloc(length);
1613 /* Copy values into new storage */
1616 memcpy(t, string, sn);
1621 /* Replace i18N string array */
1622 entry->length -= strlen(be) + 1;
1623 entry->length += sn;
1625 if (ENTRY_IN_REGION(entry)) {
1626 entry->info.offset = 0;
1628 entry->data = _free(entry->data);
1635 int headerMod(Header h, rpmtd td)
1642 /* First find the tag */
1643 entry = findEntry(h, td->tag, td->type);
1648 data = grabData(td->type, td->data, td->count, &length);
1649 if (data == NULL || length <= 0)
1652 /* make sure entry points to the first occurence of this tag */
1653 while (entry > h->index && (entry - 1)->info.tag == td->tag)
1656 /* free after we've grabbed the new data in case the two are intertwined;
1657 that's a bad idea but at least we won't break */
1658 oldData = entry->data;
1660 entry->info.count = td->count;
1661 entry->info.type = td->type;
1663 entry->length = length;
1665 if (ENTRY_IN_REGION(entry)) {
1666 entry->info.offset = 0;
1674 * Header tag iterator data structure.
1676 struct headerIterator_s {
1677 Header h; /*!< Header being iterated. */
1678 int next_index; /*!< Next tag index. */
1681 HeaderIterator headerFreeIterator(HeaderIterator hi)
1684 hi->h = headerFree(hi->h);
1690 HeaderIterator headerInitIterator(Header h)
1692 HeaderIterator hi = xmalloc(sizeof(*hi));
1696 hi->h = headerLink(h);
1701 static indexEntry nextIndex(HeaderIterator hi)
1705 indexEntry entry = NULL;
1707 for (slot = hi->next_index; slot < h->indexUsed; slot++) {
1708 entry = h->index + slot;
1709 if (!ENTRY_IS_REGION(entry))
1712 hi->next_index = slot;
1713 if (entry == NULL || slot >= h->indexUsed)
1720 rpmTagVal headerNextTag(HeaderIterator hi)
1722 indexEntry entry = nextIndex(hi);
1723 return entry ? entry->info.tag : RPMTAG_NOT_FOUND;
1726 int headerNext(HeaderIterator hi, rpmtd td)
1728 indexEntry entry = nextIndex(hi);
1733 td->tag = entry->info.tag;
1734 rc = copyTdEntry(entry, td, HEADERGET_DEFAULT);
1736 return ((rc == 1) ? 1 : 0);
1739 unsigned int headerGetInstance(Header h)
1741 return h ? h->instance : 0;
1744 void headerSetInstance(Header h, unsigned int instance)
1746 h->instance = instance;