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. */
15 #include <rpm/rpmtypes.h>
16 #include <rpm/rpmstring.h>
17 #include "lib/header_internal.h"
18 #include "lib/misc.h" /* tag function proto */
25 const unsigned char rpm_header_magic[8] = {
26 0x8e, 0xad, 0xe8, 0x01, 0x00, 0x00, 0x00, 0x00
30 * Alignment needed for header data types.
32 static const int typeAlign[16] = {
33 1, /*!< RPM_NULL_TYPE */
34 1, /*!< RPM_CHAR_TYPE */
35 1, /*!< RPM_INT8_TYPE */
36 2, /*!< RPM_INT16_TYPE */
37 4, /*!< RPM_INT32_TYPE */
38 8, /*!< RPM_INT64_TYPE */
39 1, /*!< RPM_STRING_TYPE */
40 1, /*!< RPM_BIN_TYPE */
41 1, /*!< RPM_STRING_ARRAY_TYPE */
42 1, /*!< RPM_I18NSTRING_TYPE */
52 * Size of header data types.
54 static const int typeSizes[16] = {
55 0, /*!< RPM_NULL_TYPE */
56 1, /*!< RPM_CHAR_TYPE */
57 1, /*!< RPM_INT8_TYPE */
58 2, /*!< RPM_INT16_TYPE */
59 4, /*!< RPM_INT32_TYPE */
60 8, /*!< RPM_INT64_TYPE */
61 -1, /*!< RPM_STRING_TYPE */
62 1, /*!< RPM_BIN_TYPE */
63 -1, /*!< RPM_STRING_ARRAY_TYPE */
64 -1, /*!< RPM_I18NSTRING_TYPE */
74 HEADERSORT_NONE = 0, /* Not sorted */
75 HEADERSORT_OFFSET = 1, /* Sorted by offset (on-disk format) */
76 HEADERSORT_INDEX = 2, /* Sorted by index */
80 HEADERFLAG_ALLOCATED = (1 << 1), /*!< Is 1st header region allocated? */
81 HEADERFLAG_LEGACY = (1 << 2), /*!< Header came from legacy source? */
82 HEADERFLAG_DEBUG = (1 << 3), /*!< Debug this header? */
85 typedef rpmFlags headerFlags;
88 * A single tag from a Header.
90 typedef struct indexEntry_s * indexEntry;
92 struct entryInfo_s info; /*!< Description of tag data. */
93 rpm_data_t data; /*!< Location of tag data. */
94 int length; /*!< No. bytes of data. */
95 int rdlen; /*!< No. bytes of data in region. */
99 * The Header data structure.
101 struct headerToken_s {
102 void * blob; /*!< Header region blob. */
103 indexEntry index; /*!< Array of tags. */
104 int indexUsed; /*!< Current size of tag array. */
105 int indexAlloced; /*!< Allocated size of tag array. */
106 unsigned int instance; /*!< Rpmdb instance (offset) */
108 int sorted; /*!< Current sort method */
109 int nrefs; /*!< Reference count. */
113 * Maximum no. of bytes permitted in a header.
115 static const size_t headerMaxbytes = (256*1024*1024);
117 #define INDEX_MALLOC_SIZE 8
119 #define ENTRY_IS_REGION(_e) \
120 (((_e)->info.tag >= RPMTAG_HEADERIMAGE) && ((_e)->info.tag < RPMTAG_HEADERREGIONS))
121 #define ENTRY_IN_REGION(_e) ((_e)->info.offset < 0)
123 #define REGION_TAG_TYPE RPM_BIN_TYPE
124 #define REGION_TAG_COUNT sizeof(struct entryInfo_s)
127 * Sanity check on no. of tags.
128 * This check imposes a limit of 65K tags, more than enough.
130 #define HEADER_TAGS_MAX 0x0000ffff
131 #define hdrchkTags(_ntags) ((_ntags) & (~HEADER_TAGS_MAX))
134 * Sanity check on tag values.
135 * Catches out nasties like negative values and multiple regions.
137 #define hdrchkTag(_tag) ((_tag) < HEADER_I18NTABLE)
140 * Sanity check on type values.
142 #define hdrchkType(_type) ((_type) < RPM_MIN_TYPE || (_type) > RPM_MAX_TYPE)
145 * Sanity check on data size and/or offset and/or count.
146 * This check imposes a limit of 256 MB -- file signatures
147 * may require a lot of space in the header.
149 #define HEADER_DATA_MAX 0x0fffffff
150 #define hdrchkData(_nbytes) ((_nbytes) & (~HEADER_DATA_MAX))
153 * Sanity check on data alignment for data type.
155 #define hdrchkAlign(_type, _off) ((_off) & (typeAlign[_type]-1))
158 * Sanity check on range of data offset.
160 #define hdrchkRange(_dl, _off) ((_off) < 0 || (_off) > (_dl))
162 static int dataLength(rpm_tagtype_t type, rpm_constdata_t p, rpm_count_t count,
163 int onDisk, rpm_constdata_t pend);
166 /* Convert a 64bit value to network byte order. */
168 static uint64_t htonll(uint64_t n)
170 #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
171 uint32_t *i = (uint32_t*)&n;
180 Header headerLink(Header h)
187 static Header headerUnlink(Header h)
194 Header headerFree(Header h)
196 (void) headerUnlink(h);
198 if (h == NULL || h->nrefs > 0)
202 indexEntry entry = h->index;
204 for (i = 0; i < h->indexUsed; i++, entry++) {
205 if ((h->flags & HEADERFLAG_ALLOCATED) && ENTRY_IS_REGION(entry)) {
206 if (entry->length > 0) {
207 int32_t * ei = entry->data;
208 if ((ei - 2) == h->blob) h->blob = _free(h->blob);
211 } else if (!ENTRY_IN_REGION(entry)) {
212 entry->data = _free(entry->data);
216 h->index = _free(h->index);
223 static Header headerCreate(void *blob, int32_t indexLen)
225 Header h = xcalloc(1, sizeof(*h));
228 h->indexAlloced = indexLen + 1;
229 h->indexUsed = indexLen;
231 h->indexAlloced = INDEX_MALLOC_SIZE;
235 h->sorted = HEADERSORT_NONE;
237 h->index = (h->indexAlloced
238 ? xcalloc(h->indexAlloced, sizeof(*h->index))
242 return headerLink(h);
245 Header headerNew(void)
247 return headerCreate(NULL, 0);
250 static rpmRC hdrblobVerifyInfo(hdrblob blob, char **emsg)
252 struct entryInfo_s info;
255 const char *ds = (const char *) blob->dataStart;
256 int32_t il = (blob->regionTag) ? blob->il-1 : blob->il;
257 entryInfo pe = (blob->regionTag) ? blob->pe+1 : blob->pe;
259 for (i = 0; i < il; i++) {
262 /* Previous data must not overlap */
263 if (end > info.offset)
266 if (hdrchkTag(info.tag))
268 if (hdrchkType(info.type))
270 if (hdrchkAlign(info.type, info.offset))
272 if (hdrchkRange(blob->dl, info.offset))
275 /* Verify the data actually fits */
276 len = dataLength(info.type, ds + info.offset,
277 info.count, 1, ds + blob->dl);
278 end = info.offset + len;
279 if (hdrchkRange(blob->dl, end) || len <= 0)
282 return 0; /* Everything ok */
287 _("tag[%d]: BAD, tag %d type %d offset %d count %d len %d"),
288 i, info.tag, info.type, info.offset, info.count, len);
293 static int indexCmp(const void * avp, const void * bvp)
295 indexEntry ap = (indexEntry) avp, bp = (indexEntry) bvp;
296 return (ap->info.tag - bp->info.tag);
299 static void headerSort(Header h)
301 if (h->sorted != HEADERSORT_INDEX) {
302 qsort(h->index, h->indexUsed, sizeof(*h->index), indexCmp);
303 h->sorted = HEADERSORT_INDEX;
307 static int offsetCmp(const void * avp, const void * bvp)
309 indexEntry ap = (indexEntry) avp, bp = (indexEntry) bvp;
310 int rc = (ap->info.offset - bp->info.offset);
313 /* Within a region, entries sort by address. Added drips sort by tag. */
314 if (ap->info.offset < 0)
315 rc = (((char *)ap->data) - ((char *)bp->data));
317 rc = (ap->info.tag - bp->info.tag);
322 static void headerUnsort(Header h)
324 if (h->sorted != HEADERSORT_OFFSET) {
325 qsort(h->index, h->indexUsed, sizeof(*h->index), offsetCmp);
326 h->sorted = HEADERSORT_OFFSET;
330 static inline unsigned int alignDiff(rpm_tagtype_t type, unsigned int alignsize)
332 int typesize = typeSizes[type];
335 unsigned int diff = typesize - (alignsize % typesize);
336 if (diff != typesize)
342 unsigned headerSizeof(Header h, int magicp)
345 unsigned int size = 0;
353 if (magicp == HEADER_MAGIC_YES)
354 size += sizeof(rpm_header_magic);
356 size += 2 * sizeof(int32_t); /* count of index entries */
358 for (i = 0, entry = h->index; i < h->indexUsed; i++, entry++) {
359 /* Regions go in as is ... */
360 if (ENTRY_IS_REGION(entry)) {
361 size += entry->length;
362 /* Reserve space for legacy region tag + data */
363 if (i == 0 && (h->flags & HEADERFLAG_LEGACY))
364 size += sizeof(struct entryInfo_s) + entry->info.count;
368 /* ... and region elements are skipped. */
369 if (entry->info.offset < 0)
373 size += alignDiff(entry->info.type, size);
375 size += sizeof(struct entryInfo_s) + entry->length;
382 * Header string (array) size calculation, bounded if end is non-NULL.
383 * Return length (including \0 termination) on success, -1 on error.
385 static inline int strtaglen(const char *str, rpm_count_t c, const char *end)
387 const char *start = str;
393 while ((s = memchr(start, '\0', end-start))) {
394 if (--c == 0 || s > end)
399 while ((s = strchr(start, '\0'))) {
405 return (c > 0) ? -1 : (s - str + 1);
409 * Return length of entry data.
410 * @param type entry data type
411 * @param p entry data
412 * @param count entry item count
413 * @param onDisk data is concatenated strings (with NUL's))?
414 * @param pend pointer to end of data (or NULL)
415 * @return no. bytes in data, -1 on failure
417 static int dataLength(rpm_tagtype_t type, rpm_constdata_t p, rpm_count_t count,
418 int onDisk, rpm_constdata_t pend)
421 const char * se = pend;
425 case RPM_STRING_TYPE:
428 length = strtaglen(s, 1, se);
431 case RPM_STRING_ARRAY_TYPE:
432 case RPM_I18NSTRING_TYPE:
433 /* These are like RPM_STRING_TYPE, except they're *always* an array */
434 /* Compute sum of length of all strings, including nul terminators */
437 length = strtaglen(s, count, se);
439 const char ** av = (const char **)p;
441 /* add one for null termination */
442 length += strlen(*av++) + 1;
448 if (typeSizes[type] == -1)
450 length = typeSizes[(type & 0xf)] * count;
451 if (length < 0 || (se && (s + length) > se))
460 * Swap int32_t and int16_t arrays within header region.
462 * If a header region tag is in the set to be swabbed, as the data for a
463 * a header region is located after all other tag data.
465 * @param entry header entry
466 * @param il no. of entries
467 * @param dl start no. bytes of data
468 * @param pe header physical entry pointer (swapped)
469 * @param dataStart header data start
470 * @param dataEnd header data end
471 * @param regionid region offset
472 * @param fast use offsets for data sizes if possible
473 * @return no. bytes of data in region, -1 on error
475 static int regionSwab(indexEntry entry, int il, int dl,
477 unsigned char * dataStart,
478 const unsigned char * dataEnd,
479 int regionid, int fast)
481 if ((entry != NULL && regionid >= 0) || (entry == NULL && regionid != 0))
484 for (; il > 0; il--, pe++) {
485 struct indexEntry_s ie;
489 if (hdrchkType(ie.info.type))
491 if (hdrchkData(ie.info.count))
493 if (hdrchkData(ie.info.offset))
495 if (hdrchkAlign(ie.info.type, ie.info.offset))
498 ie.data = dataStart + ie.info.offset;
499 if (dataEnd && (unsigned char *)ie.data >= dataEnd)
502 if (fast && il > 1) {
503 ie.length = ntohl(pe[1].offset) - ie.info.offset;
505 ie.length = dataLength(ie.info.type, ie.data, ie.info.count,
508 if (ie.length < 0 || hdrchkData(ie.length))
514 ie.info.offset = regionid;
515 *entry = ie; /* structure assignment */
520 dl += alignDiff(ie.info.type, dl);
522 /* Perform endian conversions */
523 switch (ntohl(pe->type)) {
525 { uint64_t * it = ie.data;
526 for (; ie.info.count > 0; ie.info.count--, it += 1) {
527 if (dataEnd && ((unsigned char *)it) >= dataEnd)
533 { int32_t * it = ie.data;
534 for (; ie.info.count > 0; ie.info.count--, it += 1) {
535 if (dataEnd && ((unsigned char *)it) >= dataEnd)
541 { int16_t * it = ie.data;
542 for (; ie.info.count > 0; ie.info.count--, it += 1) {
543 if (dataEnd && ((unsigned char *)it) >= dataEnd)
556 void * headerExport(Header h, unsigned int *bsize)
567 int drlen, ndribbles;
569 if (h == NULL) return NULL;
571 /* Sort entries by (offset,tag). */
574 /* Compute (il,dl) for all tags, including those deleted in region. */
575 drlen = ndribbles = 0;
576 for (i = 0, entry = h->index; i < h->indexUsed; i++, entry++) {
577 if (ENTRY_IS_REGION(entry)) {
578 int32_t rdl = -entry->info.offset; /* negative offset */
579 int32_t ril = rdl/sizeof(*pe);
580 int rid = entry->info.offset;
583 dl += entry->rdlen + entry->info.count;
584 /* Reserve space for legacy region tag */
585 if (i == 0 && (h->flags & HEADERFLAG_LEGACY))
588 /* Skip rest of entries in region, but account for dribbles. */
589 for (; i < h->indexUsed && entry->info.offset <= rid+1; i++, entry++) {
590 if (entry->info.offset <= rid)
594 diff = alignDiff(entry->info.type, dl);
602 drlen += entry->length;
610 /* Ignore deleted drips. */
611 if (entry->data == NULL || entry->length <= 0)
615 dl += alignDiff(entry->info.type, dl);
621 /* Sanity checks on header intro. */
622 if (hdrchkTags(il) || hdrchkData(dl))
625 len = sizeof(il) + sizeof(dl) + (il * sizeof(*pe)) + dl;
631 pe = (entryInfo) &ei[2];
632 dataStart = te = (char *) (pe + il);
634 for (i = 0, entry = h->index; i < h->indexUsed; i++, entry++) {
641 if (entry->data == NULL || entry->length <= 0)
644 t = (unsigned char*)te;
645 pe->tag = htonl(entry->info.tag);
646 pe->type = htonl(entry->info.type);
647 pe->count = htonl(entry->info.count);
649 if (ENTRY_IS_REGION(entry)) {
650 int32_t rdl = -entry->info.offset; /* negative offset */
651 int32_t ril = rdl/sizeof(*pe) + ndribbles;
652 int rid = entry->info.offset;
654 src = (char *)entry->data;
655 rdlen = entry->rdlen;
657 /* Legacy headers don't have regions originally, create one */
658 if (i == 0 && (h->flags & HEADERFLAG_LEGACY)) {
661 memcpy(pe+1, src, rdl);
662 memcpy(te, src + rdl, rdlen);
665 pe->offset = htonl(te - dataStart);
668 stei[2] = htonl(-rdl-entry->info.count);
670 memcpy(te, stei, entry->info.count);
671 te += entry->info.count;
673 rdlen += entry->info.count;
675 count = regionSwab(NULL, ril, 0, pe, t, NULL, 0, 0);
681 memcpy(pe+1, src + sizeof(*pe), ((ril-1) * sizeof(*pe)));
682 memcpy(te, src + (ril * sizeof(*pe)), rdlen+entry->info.count+drlen);
685 entryInfo se = (entryInfo)src;
686 int off = ntohl(se->offset);
687 pe->offset = (off) ? htonl(te - dataStart) : htonl(off);
689 te += entry->info.count + drlen;
691 count = regionSwab(NULL, ril, 0, pe, t, NULL, 0, 0);
692 if (count != (rdlen + entry->info.count + drlen))
696 /* Skip rest of entries in region. */
697 while (i < h->indexUsed && entry->info.offset <= rid+1) {
707 /* Ignore deleted drips. */
708 if (entry->data == NULL || entry->length <= 0)
712 diff = alignDiff(entry->info.type, (te - dataStart));
718 pe->offset = htonl(te - dataStart);
720 /* copy data w/ endian conversions */
721 switch (entry->info.type) {
723 count = entry->info.count;
726 *((uint64_t *)te) = htonll(*((uint64_t *)src));
727 te += sizeof(uint64_t);
728 src += sizeof(uint64_t);
733 count = entry->info.count;
736 *((int32_t *)te) = htonl(*((int32_t *)src));
737 te += sizeof(int32_t);
738 src += sizeof(int32_t);
743 count = entry->info.count;
746 *((int16_t *)te) = htons(*((int16_t *)src));
747 te += sizeof(int16_t);
748 src += sizeof(int16_t);
753 memcpy(te, entry->data, entry->length);
760 /* Insure that there are no memcpy underruns/overruns. */
761 if (((char *)pe) != dataStart)
763 if ((((char *)ei)+len) != te)
778 void * headerUnload(Header h)
780 return headerExport(h, NULL);
784 * Find matching (tag,type) entry in header.
786 * @param tag entry tag
787 * @param type entry type
788 * @return header entry
791 indexEntry findEntry(Header h, rpmTagVal tag, rpm_tagtype_t type)
794 struct indexEntry_s key;
796 if (h == NULL) return NULL;
797 if (h->sorted != HEADERSORT_INDEX)
802 entry = bsearch(&key, h->index, h->indexUsed, sizeof(*h->index), indexCmp);
806 if (type == RPM_NULL_TYPE)
810 while (entry->info.tag == tag && entry->info.type != type &&
811 entry > h->index) entry--;
813 if (entry->info.tag == tag && entry->info.type == type)
819 int headerDel(Header h, rpmTagVal tag)
821 indexEntry last = h->index + h->indexUsed;
822 indexEntry entry, first;
825 entry = findEntry(h, tag, RPM_NULL_TYPE);
826 if (!entry) return 1;
828 /* Make sure entry points to the first occurrence of this tag. */
829 while (entry > h->index && (entry - 1)->info.tag == tag)
832 /* Free data for tags being removed. */
833 for (first = entry; first < last; first++) {
835 if (first->info.tag != tag)
840 if (ENTRY_IN_REGION(first))
845 ne = (first - entry);
850 memmove(entry, first, (ne * sizeof(*entry)));
856 rpmRC hdrblobImport(hdrblob blob, int fast, Header *hdrp, char **emsg)
862 h = headerCreate(blob->ei, blob->il);
865 if (!(htonl(blob->pe->tag) < RPMTAG_HEADERI18NTABLE)) {
866 /* An original v3 header, create a legacy region entry for it */
867 h->flags |= HEADERFLAG_LEGACY;
868 entry->info.type = REGION_TAG_TYPE;
869 entry->info.tag = RPMTAG_HEADERIMAGE;
870 entry->info.count = REGION_TAG_COUNT;
871 entry->info.offset = ((unsigned char *)blob->pe - blob->dataStart); /* negative offset */
873 entry->data = blob->pe;
874 entry->length = blob->pvlen - sizeof(blob->il) - sizeof(blob->dl);
875 rdlen = regionSwab(entry+1, blob->il, 0, blob->pe,
876 blob->dataStart, blob->dataEnd,
877 entry->info.offset, fast);
878 if (rdlen != blob->dl)
880 entry->rdlen = rdlen;
883 /* Either a v4 header or an "upgraded" v3 header with a legacy region */
886 h->flags &= ~HEADERFLAG_LEGACY;
887 ei2h(blob->pe, &entry->info);
888 ril = (entry->info.offset != 0) ? blob->ril : blob->il;
890 entry->info.offset = -(ril * sizeof(*blob->pe)); /* negative offset */
891 entry->data = blob->pe;
892 entry->length = blob->pvlen - sizeof(blob->il) - sizeof(blob->dl);
893 rdlen = regionSwab(entry+1, ril-1, 0, blob->pe+1,
894 blob->dataStart, blob->dataEnd,
895 entry->info.offset, fast);
898 entry->rdlen = rdlen;
900 if (ril < h->indexUsed) {
901 indexEntry newEntry = entry + ril;
902 int ne = (h->indexUsed - ril);
903 int rid = entry->info.offset+1;
905 /* Load dribble entries from region. */
906 rdlen = regionSwab(newEntry, ne, rdlen, blob->pe+ril,
907 blob->dataStart, blob->dataEnd, rid, fast);
911 { indexEntry firstEntry = newEntry;
912 int save = h->indexUsed;
915 /* Dribble entries replace duplicate region entries. */
917 for (j = 0; j < ne; j++, newEntry++) {
918 (void) headerDel(h, newEntry->info.tag);
919 if (newEntry->info.tag == RPMTAG_BASENAMES)
920 (void) headerDel(h, RPMTAG_OLDFILENAMES);
923 /* If any duplicate entries were replaced, move new entries down. */
924 if (h->indexUsed < (save - ne)) {
925 memmove(h->index + h->indexUsed, firstEntry,
926 (ne * sizeof(*entry)));
932 rdlen += REGION_TAG_COUNT;
934 if (rdlen != blob->dl)
938 /* Force sorting, dribble lookups can cause early sort on partial header */
939 h->sorted = HEADERSORT_NONE;
941 h->flags |= HEADERFLAG_ALLOCATED;
945 /* We own the memory now, avoid double-frees */
954 rasprintf(emsg, _("hdr load: BAD"));
959 Header headerReload(Header h, rpmTagVal tag)
963 void * uh = headerExport(h, &uc);
968 nh = headerImport(uh, uc, 0);
973 if (ENTRY_IS_REGION(nh->index)) {
974 if (tag == RPMTAG_HEADERSIGNATURES || tag == RPMTAG_HEADERIMMUTABLE)
975 nh->index[0].info.tag = tag;
980 Header headerLoad(void * uh)
982 return headerImport(uh, 0, 0);
985 Header headerCopyLoad(const void * uh)
987 /* Discards const but that's ok as we'll take a copy */
988 return headerImport((void *)uh, 0, HEADERIMPORT_COPY);
991 Header headerRead(FD_t fd, int magicp)
994 struct hdrblob_s blob;
997 if (hdrblobRead(fd, magicp, 0, 0, &blob, &buf) == RPMRC_OK)
998 hdrblobImport(&blob, 0, &h, &buf);
1004 int headerWrite(FD_t fd, Header h, int magicp)
1007 unsigned int length;
1008 void * uh = headerExport(h, &length);
1013 if (magicp == HEADER_MAGIC_YES) {
1014 nb = Fwrite(rpm_header_magic, sizeof(rpm_header_magic), 1, fd);
1015 if (nb != sizeof(rpm_header_magic))
1019 nb = Fwrite(uh, sizeof(char), length, fd);
1023 return (nb == length ? 0 : 1);
1026 int headerIsEntry(Header h, rpmTagVal tag)
1028 /* FIX: h modified by sort. */
1029 return (findEntry(h, tag, RPM_NULL_TYPE) ? 1 : 0);
1033 /* simple heuristic to find out if the header is from * a source rpm
1034 * or not: source rpms contain at least the spec file and have all
1035 * files in one directory with an empty name.
1037 int headerIsSourceHeuristic(Header h)
1039 indexEntry entry = findEntry(h, RPMTAG_DIRNAMES, RPM_STRING_ARRAY_TYPE);
1040 return entry && entry->info.count == 1 && entry->data && !*(const char *)entry->data;
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 flags flags to control memory allocation
1054 * @return 1 on success, otherwise error.
1056 static int copyTdEntry(const indexEntry entry, rpmtd td, headerGetFlags flags)
1058 rpm_count_t count = entry->info.count;
1059 int rc = 1; /* XXX 1 on success. */
1060 /* ALLOC overrides MINMEM */
1061 int allocMem = flags & HEADERGET_ALLOC;
1062 int minMem = allocMem ? 0 : flags & HEADERGET_MINMEM;
1063 int argvArray = (flags & HEADERGET_ARGV) ? 1 : 0;
1066 td->flags = RPMTD_IMMUTABLE;
1067 switch (entry->info.type) {
1070 * XXX This only works for
1071 * XXX "sealed" HEADER_IMMUTABLE/HEADER_SIGNATURES/HEADER_IMAGE.
1072 * XXX This will *not* work for unsealed legacy HEADER_IMAGE (i.e.
1073 * XXX a legacy header freshly read, but not yet unloaded to the rpmdb).
1075 if (ENTRY_IS_REGION(entry)) {
1076 int32_t * ei = ((int32_t *)entry->data) - 2;
1077 entryInfo pe = (entryInfo) (ei + 2);
1078 unsigned char * dataStart = (unsigned char *) (pe + ntohl(ei[0]));
1079 int32_t rdl = -entry->info.offset; /* negative offset */
1080 int32_t ril = rdl/sizeof(*pe);
1083 count = 2 * sizeof(*ei) + (ril * sizeof(*pe)) + rdl;
1084 if (entry->info.tag == RPMTAG_HEADERIMAGE) {
1088 count += REGION_TAG_COUNT;
1089 rdl += REGION_TAG_COUNT;
1092 td->data = xmalloc(count);
1093 ei = (int32_t *) td->data;
1097 pe = (entryInfo) memcpy(ei + 2, pe, (ril * sizeof(*pe)));
1099 dataStart = (unsigned char *) memcpy(pe + ril, dataStart, rdl);
1101 rc = regionSwab(NULL, ril, 0, pe, dataStart, dataStart + rdl, 0, 0);
1102 /* don't return data on failure */
1104 td->data = _free(td->data);
1106 /* XXX 1 on success. */
1107 rc = (rc < 0) ? 0 : 1;
1109 count = entry->length;
1111 ? memcpy(xmalloc(count), entry->data, count)
1115 case RPM_STRING_TYPE:
1116 /* simple string, but fallthrough if its actually an array */
1117 if (count == 1 && !argvArray) {
1118 td->data = allocMem ? xstrdup(entry->data) : entry->data;
1121 case RPM_STRING_ARRAY_TYPE:
1122 case RPM_I18NSTRING_TYPE:
1123 { const char ** ptrEntry;
1124 int tableSize = (count + argvArray) * sizeof(char *);
1129 td->data = xmalloc(tableSize);
1130 ptrEntry = (const char **) td->data;
1133 t = xmalloc(tableSize + entry->length);
1134 td->data = (void *)t;
1135 ptrEntry = (const char **) td->data;
1137 memcpy(t, entry->data, entry->length);
1139 for (i = 0; i < count; i++) {
1146 td->flags |= RPMTD_ARGV;
1151 case RPM_INT16_TYPE:
1152 case RPM_INT32_TYPE:
1153 case RPM_INT64_TYPE:
1155 td->data = xmalloc(entry->length);
1156 memcpy(td->data, entry->data, entry->length);
1158 td->data = entry->data;
1162 /* WTH? Don't mess with unknown data types... */
1167 td->type = entry->info.type;
1169 td->size = entry->length;
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 (entry->info.type == RPM_I18NSTRING_TYPE && !(flags & HEADERGET_RAW))
1315 rc = copyI18NEntry(h, entry, td, flags);
1317 rc = copyTdEntry(entry, td, flags);
1320 td->flags |= RPMTD_INVALID;
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))
1413 data = grabData(td->type, td->data, td->count, &length);
1417 /* Allocate more index space if necessary */
1418 if (h->indexUsed == h->indexAlloced) {
1419 h->indexAlloced += INDEX_MALLOC_SIZE;
1420 h->index = xrealloc(h->index, h->indexAlloced * sizeof(*h->index));
1423 /* Fill in the index */
1424 entry = h->index + h->indexUsed;
1425 entry->info.tag = td->tag;
1426 entry->info.type = td->type;
1427 entry->info.count = td->count;
1428 entry->info.offset = 0;
1430 entry->length = length;
1432 if (h->indexUsed > 0 && td->tag < h->index[h->indexUsed-1].info.tag)
1433 h->sorted = HEADERSORT_NONE;
1439 static int intAppendEntry(Header h, rpmtd td)
1444 if (td->type == RPM_STRING_TYPE || td->type == RPM_I18NSTRING_TYPE) {
1445 /* we can't do this */
1449 /* Find the tag entry in the header. */
1450 entry = findEntry(h, td->tag, td->type);
1454 length = dataLength(td->type, td->data, td->count, 0, NULL);
1458 if (ENTRY_IN_REGION(entry)) {
1459 char * t = xmalloc(entry->length + length);
1460 memcpy(t, entry->data, entry->length);
1462 entry->info.offset = 0;
1464 entry->data = xrealloc(entry->data, entry->length + length);
1466 copyData(td->type, ((char *) entry->data) + entry->length,
1467 td->data, td->count, length);
1469 entry->length += length;
1471 entry->info.count += td->count;
1476 int headerPut(Header h, rpmtd td, headerPutFlags flags)
1481 if (flags & HEADERPUT_APPEND) {
1482 rc = findEntry(h, td->tag, td->type) ?
1483 intAppendEntry(h, td) :
1486 rc = intAddEntry(h, td);
1491 int headerAddI18NString(Header h, rpmTagVal tag, const char * string,
1494 indexEntry table, entry;
1495 const char ** strArray;
1498 rpm_count_t i, langNum;
1501 table = findEntry(h, RPMTAG_HEADERI18NTABLE, RPM_STRING_ARRAY_TYPE);
1502 entry = findEntry(h, tag, RPM_I18NSTRING_TYPE);
1504 if (!table && entry)
1505 return 0; /* this shouldn't ever happen!! */
1507 if (!table && !entry) {
1508 const char * charArray[2];
1509 rpm_count_t count = 0;
1511 if (!lang || (lang[0] == 'C' && lang[1] == '\0')) {
1512 charArray[count++] = "C";
1514 charArray[count++] = "C";
1515 charArray[count++] = lang;
1519 td.tag = RPMTAG_HEADERI18NTABLE;
1520 td.type = RPM_STRING_ARRAY_TYPE;
1521 td.data = (void *) charArray;
1523 if (!headerPut(h, &td, HEADERPUT_DEFAULT))
1525 table = findEntry(h, RPMTAG_HEADERI18NTABLE, RPM_STRING_ARRAY_TYPE);
1530 if (!lang) lang = "C";
1532 { const char * l = table->data;
1533 for (langNum = 0; langNum < table->info.count; langNum++) {
1534 if (rstreq(l, lang)) break;
1539 if (langNum >= table->info.count) {
1540 length = strlen(lang) + 1;
1541 if (ENTRY_IN_REGION(table)) {
1542 char * t = xmalloc(table->length + length);
1543 memcpy(t, table->data, table->length);
1545 table->info.offset = 0;
1547 table->data = xrealloc(table->data, table->length + length);
1548 memmove(((char *)table->data) + table->length, lang, length);
1549 table->length += length;
1550 table->info.count++;
1556 strArray = xmalloc(sizeof(*strArray) * (langNum + 1));
1557 for (i = 0; i < langNum; i++)
1559 strArray[langNum] = string;
1563 td.type = RPM_I18NSTRING_TYPE;
1565 td.count = langNum + 1;
1566 rc = headerPut(h, &td, HEADERPUT_DEFAULT);
1569 } else if (langNum >= entry->info.count) {
1570 ghosts = langNum - entry->info.count;
1572 length = strlen(string) + 1 + ghosts;
1573 if (ENTRY_IN_REGION(entry)) {
1574 char * t = xmalloc(entry->length + length);
1575 memcpy(t, entry->data, entry->length);
1577 entry->info.offset = 0;
1579 entry->data = xrealloc(entry->data, entry->length + length);
1581 memset(((char *)entry->data) + entry->length, '\0', ghosts);
1582 memmove(((char *)entry->data) + entry->length + ghosts, string, strlen(string)+1);
1584 entry->length += length;
1585 entry->info.count = langNum + 1;
1587 char *b, *be, *e, *ee, *t;
1590 /* Set beginning/end pointers to previous data */
1591 b = be = e = ee = entry->data;
1592 for (i = 0; i < table->info.count; i++) {
1595 ee += strlen(ee) + 1;
1600 /* Get storage for new buffer */
1602 sn = strlen(string) + 1;
1604 length = bn + sn + en;
1605 t = buf = xmalloc(length);
1607 /* Copy values into new storage */
1610 memcpy(t, string, sn);
1615 /* Replace i18N string array */
1616 entry->length -= strlen(be) + 1;
1617 entry->length += sn;
1619 if (ENTRY_IN_REGION(entry)) {
1620 entry->info.offset = 0;
1622 entry->data = _free(entry->data);
1629 int headerMod(Header h, rpmtd td)
1636 /* First find the tag */
1637 entry = findEntry(h, td->tag, td->type);
1641 data = grabData(td->type, td->data, td->count, &length);
1645 /* make sure entry points to the first occurrence 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;
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;
1742 #define RETRY_ERROR(_err) \
1743 ((_err) == EINTR || (_err) == EAGAIN || (_err) == EWOULDBLOCK)
1745 ssize_t Freadall(FD_t fd, void * buf, ssize_t size)
1751 while (total < size) {
1752 nb = Fread(bufp, 1, size - total, fd);
1754 if (nb == 0 || (nb < 0 && !RETRY_ERROR(errno))) {
1768 static rpmRC hdrblobVerifyRegion(rpmTagVal regionTag, int exact_size,
1769 hdrblob blob, char **buf)
1771 rpmRC rc = RPMRC_FAIL;
1772 struct entryInfo_s trailer, einfo;
1773 unsigned char * regionEnd = NULL;
1775 /* Check that we have at least on tag */
1777 rasprintf(buf, _("region: no tags"));
1781 /* Convert the 1st tag element. */
1782 ei2h(blob->pe, &einfo);
1784 if (!regionTag && (einfo.tag == RPMTAG_HEADERSIGNATURES ||
1785 einfo.tag == RPMTAG_HEADERIMMUTABLE ||
1786 einfo.tag == RPMTAG_HEADERIMAGE)) {
1787 regionTag = einfo.tag;
1790 /* Is there an immutable header region tag? */
1791 if (!(einfo.tag == regionTag)) {
1792 rc = RPMRC_NOTFOUND;
1796 /* Is the region tag sane? */
1797 if (!(einfo.type == REGION_TAG_TYPE && einfo.count == REGION_TAG_COUNT)) {
1799 _("region tag: BAD, tag %d type %d offset %d count %d"),
1800 einfo.tag, einfo.type, einfo.offset, einfo.count);
1804 /* Is the trailer within the data area? */
1805 if (hdrchkRange(blob->dl, einfo.offset + REGION_TAG_COUNT)) {
1807 _("region offset: BAD, tag %d type %d offset %d count %d"),
1808 einfo.tag, einfo.type, einfo.offset, einfo.count);
1812 /* Is there an immutable header region tag trailer? */
1813 memset(&trailer, 0, sizeof(trailer));
1814 regionEnd = blob->dataStart + einfo.offset;
1815 (void) memcpy(&trailer, regionEnd, REGION_TAG_COUNT);
1816 regionEnd += REGION_TAG_COUNT;
1817 blob->rdl = regionEnd - blob->dataStart;
1819 ei2h(&trailer, &einfo);
1820 /* Trailer offset is negative and has a special meaning */
1821 einfo.offset = -einfo.offset;
1822 if (!(einfo.tag == regionTag &&
1823 einfo.type == REGION_TAG_TYPE && einfo.count == REGION_TAG_COUNT))
1826 _("region trailer: BAD, tag %d type %d offset %d count %d"),
1827 einfo.tag, einfo.type, einfo.offset, einfo.count);
1831 /* Does the region actually fit within the header? */
1832 blob->ril = einfo.offset/sizeof(*blob->pe);
1833 if ((einfo.offset % sizeof(*blob->pe)) || hdrchkRange(blob->il, blob->ril) ||
1834 hdrchkRange(blob->dl, blob->rdl)) {
1835 rasprintf(buf, _("region %d size: BAD, ril %d il %d rdl %d dl %d"),
1836 regionTag, blob->ril, blob->il, blob->rdl, blob->dl);
1840 /* In package files region size is expected to match header size. */
1841 if (exact_size && !(blob->il == blob->ril && blob->dl == blob->rdl)) {
1843 _("region %d: tag number mismatch il %d ril %d dl %d rdl %d\n"),
1844 regionTag, blob->il, blob->ril, blob->dl, blob->rdl);
1848 blob->regionTag = regionTag;
1856 static rpmRC hdrblobVerifyLengths(rpmTagVal regionTag, uint32_t il, uint32_t dl,
1858 uint32_t il_max = HEADER_TAGS_MAX;
1859 uint32_t dl_max = HEADER_DATA_MAX;
1860 if (regionTag == RPMTAG_HEADERSIGNATURES) {
1862 dl_max = 64 * 1024 * 1024;
1864 if (hdrchkRange(il_max, il)) {
1865 rasprintf(emsg, _("hdr tags: BAD, no. of tags(%" PRIu32 ") out of range"), il);
1868 if (hdrchkRange(dl_max, dl)) {
1869 rasprintf(emsg, _("hdr data: BAD, no. of bytes(%" PRIu32 ") out of range"), dl);
1874 rpmRC hdrblobRead(FD_t fd, int magic, int exact_size, rpmTagVal regionTag, hdrblob blob, char **emsg)
1877 int32_t *bs = (magic != 0) ? &block[0] : &block[2];
1878 int blen = (magic != 0) ? sizeof(block) : sizeof(block) / 2;
1881 int32_t * ei = NULL;
1884 rpmRC rc = RPMRC_FAIL; /* assume failure */
1887 memset(block, 0, sizeof(block));
1888 if ((xx = Freadall(fd, bs, blen)) != blen) {
1890 _("hdr size(%d): BAD, read returned %d"), blen, xx);
1893 if (magic && memcmp(block, rpm_header_magic, sizeof(rpm_header_magic))) {
1894 rasprintf(emsg, _("hdr magic: BAD"));
1897 il = ntohl(block[2]);
1898 dl = ntohl(block[3]);
1899 if (hdrblobVerifyLengths(regionTag, il, dl, emsg))
1902 nb = (il * sizeof(struct entryInfo_s)) + dl;
1903 uc = sizeof(il) + sizeof(dl) + nb;
1907 if ((xx = Freadall(fd, (char *)&ei[2], nb)) != nb) {
1908 rasprintf(emsg, _("hdr blob(%zd): BAD, read returned %d"), nb, xx);
1912 if (regionTag == RPMTAG_HEADERSIGNATURES) {
1913 size_t sigSize = uc + sizeof(rpm_header_magic);
1914 size_t pad = (8 - (sigSize % 8)) % 8;
1916 if (pad && (trc = Freadall(fd, block, pad)) != pad) {
1917 rasprintf(emsg, _("sigh pad(%zd): BAD, read %zd bytes"), pad, trc);
1922 rc = hdrblobInit(ei, uc, regionTag, exact_size, blob, emsg);
1925 if (rc != RPMRC_OK) {
1928 if (emsg && *emsg && regionTag == RPMTAG_HEADERSIGNATURES) {
1929 /* rstrscat() cannot handle overlap even if it claims so */
1930 char *tmp = rstrscat(NULL, _("signature "), *emsg, NULL);
1939 rpmRC hdrblobInit(const void *uh, size_t uc,
1940 rpmTagVal regionTag, int exact_size,
1941 struct hdrblob_s *blob, char **emsg)
1943 rpmRC rc = RPMRC_FAIL;
1944 memset(blob, 0, sizeof(*blob));
1946 rasprintf(emsg, _("hdr length: BAD"));
1950 blob->ei = (int32_t *) uh; /* discards const */
1951 blob->il = ntohl((uint32_t)(blob->ei[0]));
1952 blob->dl = ntohl((uint32_t)(blob->ei[1]));
1953 if (hdrblobVerifyLengths(regionTag, blob->il, blob->dl, emsg) != RPMRC_OK)
1956 blob->pe = (entryInfo) &(blob->ei[2]);
1957 blob->pvlen = sizeof(blob->il) + sizeof(blob->dl) +
1958 (blob->il * sizeof(*blob->pe)) + blob->dl;
1959 blob->dataStart = (uint8_t *) (blob->pe + blob->il);
1960 blob->dataEnd = blob->dataStart + blob->dl;
1962 /* Is the blob the right size? */
1963 if (blob->pvlen >= headerMaxbytes || (uc && blob->pvlen != uc)) {
1964 rasprintf(emsg, _("blob size(%d): BAD, 8 + 16 * il(%d) + dl(%d)"),
1965 blob->pvlen, blob->il, blob->dl);
1969 if (hdrblobVerifyRegion(regionTag, exact_size, blob, emsg) == RPMRC_FAIL)
1972 /* Sanity check the rest of the header structure. */
1973 if (hdrblobVerifyInfo(blob, emsg))
1982 rpmRC hdrblobGet(hdrblob blob, uint32_t tag, rpmtd td)
1984 rpmRC rc = RPMRC_NOTFOUND;
1985 struct indexEntry_s entry;
1986 struct entryInfo_s einfo;
1987 const struct entryInfo_s *pe = blob->pe;
1988 uint32_t ntag = htonl(tag);
1991 memset(&einfo, 0, sizeof(einfo));
1994 for (int i = 1; i < blob->il; i++, pe++) {
1995 if (pe->tag != ntag)
1999 /* We can only handle non-byteswappable data */
2000 tsize = typeSizes[einfo.type];
2001 if (tsize != 1 && tsize != -1)
2004 entry.info = einfo; /* struct assignment */
2005 entry.data = blob->dataStart + einfo.offset;
2006 entry.length = dataLength(einfo.type, blob->dataStart + einfo.offset,
2007 einfo.count, 1, blob->dataEnd);
2009 td->tag = einfo.tag;
2010 rc = copyTdEntry(&entry, td, HEADERGET_MINMEM) ? RPMRC_OK : RPMRC_FAIL;
2016 Header headerImport(void * blob, unsigned int bsize, headerImportFlags flags)
2019 struct hdrblob_s hblob;
2023 if (flags & HEADERIMPORT_COPY) {
2024 if (bsize == 0 && hdrblobInit(b, 0, 0, 0, &hblob, &buf) == RPMRC_OK)
2025 bsize = hblob.pvlen;
2028 b = memcpy(xmalloc(bsize), b, bsize);
2031 /* Sanity checks on header intro. */
2032 if (hdrblobInit(b, bsize, 0, 0, &hblob, &buf) == RPMRC_OK)
2033 hdrblobImport(&hblob, (flags & HEADERIMPORT_FAST), &h, &buf);
2036 if (h == NULL && b != blob)