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, unsigned int pvlen, int32_t indexLen)
159 Header h = xcalloc(1, sizeof(*h));
161 h->blob = (pvlen > 0) ? memcpy(xmalloc(pvlen), blob, pvlen) : blob;
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, 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 (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 static inline unsigned int alignDiff(rpm_tagtype_t type, unsigned int alignsize)
250 int typesize = typeSizes[type];
253 unsigned int diff = typesize - (alignsize % typesize);
254 if (diff != typesize)
260 unsigned headerSizeof(Header h, int magicp)
263 unsigned int size = 0;
272 case HEADER_MAGIC_YES:
273 size += sizeof(rpm_header_magic);
275 case HEADER_MAGIC_NO:
279 size += 2 * sizeof(int32_t); /* count of index entries */
281 for (i = 0, entry = h->index; i < h->indexUsed; i++, entry++) {
282 /* Regions go in as is ... */
283 if (ENTRY_IS_REGION(entry)) {
284 size += entry->length;
285 /* XXX Legacy regions do not include the region tag and data. */
286 if (i == 0 && (h->flags & HEADERFLAG_LEGACY))
287 size += sizeof(struct entryInfo_s) + entry->info.count;
291 /* ... and region elements are skipped. */
292 if (entry->info.offset < 0)
296 size += alignDiff(entry->info.type, size);
298 size += sizeof(struct entryInfo_s) + entry->length;
305 * Header string (array) size calculation, bounded if end is non-NULL.
306 * Return length (including \0 termination) on success, -1 on error.
308 static inline int strtaglen(const char *str, rpm_count_t c, const char *end)
310 const char *start = str;
314 while ((s = memchr(start, '\0', end-start))) {
315 if (--c == 0 || s > end)
320 while ((s = strchr(start, '\0'))) {
326 return (c > 0) ? -1 : (s - str + 1);
330 * Return length of entry data.
331 * @param type entry data type
332 * @param p entry data
333 * @param count entry item count
334 * @param onDisk data is concatenated strings (with NUL's))?
335 * @param pend pointer to end of data (or NULL)
336 * @return no. bytes in data, -1 on failure
338 static int dataLength(rpm_tagtype_t type, rpm_constdata_t p, rpm_count_t count,
339 int onDisk, rpm_constdata_t pend)
342 const char * se = pend;
346 case RPM_STRING_TYPE:
349 length = strtaglen(s, 1, se);
352 case RPM_STRING_ARRAY_TYPE:
353 case RPM_I18NSTRING_TYPE:
354 /* These are like RPM_STRING_TYPE, except they're *always* an array */
355 /* Compute sum of length of all strings, including nul terminators */
358 length = strtaglen(s, count, se);
360 const char ** av = (const char **)p;
362 /* add one for null termination */
363 length += strlen(*av++) + 1;
369 if (typeSizes[type] == -1)
371 length = typeSizes[(type & 0xf)] * count;
372 if (length < 0 || (se && (s + length) > se))
381 * Swap int32_t and int16_t arrays within header region.
383 * If a header region tag is in the set to be swabbed, as the data for a
384 * a header region is located after all other tag data.
386 * @param entry header entry
387 * @param il no. of entries
388 * @param dl start no. bytes of data
389 * @param pe header physical entry pointer (swapped)
390 * @param dataStart header data start
391 * @param dataEnd header data end
392 * @param regionid region offset
393 * @param fast use offsets for data sizes if possible
394 * @return no. bytes of data in region, -1 on error
396 static int regionSwab(indexEntry entry, int il, int dl,
398 unsigned char * dataStart,
399 const unsigned char * dataEnd,
400 int regionid, int fast)
402 if ((entry != NULL && regionid >= 0) || (entry == NULL && regionid != 0))
405 for (; il > 0; il--, pe++) {
406 struct indexEntry_s ie;
408 ie.info.tag = ntohl(pe->tag);
409 ie.info.type = ntohl(pe->type);
410 ie.info.count = ntohl(pe->count);
411 ie.info.offset = ntohl(pe->offset);
413 if (hdrchkType(ie.info.type))
415 if (hdrchkData(ie.info.count))
417 if (hdrchkData(ie.info.offset))
419 if (hdrchkAlign(ie.info.type, ie.info.offset))
422 ie.data = dataStart + ie.info.offset;
423 if (dataEnd && (unsigned char *)ie.data >= dataEnd)
426 if (fast && il > 1) {
427 ie.length = ntohl(pe[1].offset) - ie.info.offset;
429 ie.length = dataLength(ie.info.type, ie.data, ie.info.count,
432 if (ie.length < 0 || hdrchkData(ie.length))
438 ie.info.offset = regionid;
439 *entry = ie; /* structure assignment */
444 dl += alignDiff(ie.info.type, dl);
446 /* Perform endian conversions */
447 switch (ntohl(pe->type)) {
449 { uint64_t * it = ie.data;
450 for (; ie.info.count > 0; ie.info.count--, it += 1) {
451 if (dataEnd && ((unsigned char *)it) >= dataEnd)
457 { int32_t * it = ie.data;
458 for (; ie.info.count > 0; ie.info.count--, it += 1) {
459 if (dataEnd && ((unsigned char *)it) >= dataEnd)
465 { int16_t * it = ie.data;
466 for (; ie.info.count > 0; ie.info.count--, it += 1) {
467 if (dataEnd && ((unsigned char *)it) >= dataEnd)
480 void * headerExport(Header h, unsigned int *bsize)
491 int drlen, ndribbles;
493 if (h == NULL) return NULL;
495 /* Sort entries by (offset,tag). */
498 /* Compute (il,dl) for all tags, including those deleted in region. */
499 drlen = ndribbles = 0;
500 for (i = 0, entry = h->index; i < h->indexUsed; i++, entry++) {
501 if (ENTRY_IS_REGION(entry)) {
502 int32_t rdl = -entry->info.offset; /* negative offset */
503 int32_t ril = rdl/sizeof(*pe);
504 int rid = entry->info.offset;
507 dl += entry->rdlen + entry->info.count;
508 /* XXX Legacy regions do not include the region tag and data. */
509 if (i == 0 && (h->flags & HEADERFLAG_LEGACY))
512 /* Skip rest of entries in region, but account for dribbles. */
513 for (; i < h->indexUsed && entry->info.offset <= rid+1; i++, entry++) {
514 if (entry->info.offset <= rid)
518 diff = alignDiff(entry->info.type, dl);
526 drlen += entry->length;
534 /* Ignore deleted drips. */
535 if (entry->data == NULL || entry->length <= 0)
539 dl += alignDiff(entry->info.type, dl);
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++) {
565 if (entry->data == NULL || entry->length <= 0)
568 t = (unsigned char*)te;
569 pe->tag = htonl(entry->info.tag);
570 pe->type = htonl(entry->info.type);
571 pe->count = htonl(entry->info.count);
573 if (ENTRY_IS_REGION(entry)) {
574 int32_t rdl = -entry->info.offset; /* negative offset */
575 int32_t ril = rdl/sizeof(*pe) + ndribbles;
576 int rid = entry->info.offset;
578 src = (char *)entry->data;
579 rdlen = entry->rdlen;
581 /* XXX Legacy regions do not include the region tag and data. */
582 if (i == 0 && (h->flags & HEADERFLAG_LEGACY)) {
585 memcpy(pe+1, src, rdl);
586 memcpy(te, src + rdl, rdlen);
589 pe->offset = htonl(te - dataStart);
592 stei[2] = htonl(-rdl-entry->info.count);
594 memcpy(te, stei, entry->info.count);
595 te += entry->info.count;
597 rdlen += entry->info.count;
599 count = regionSwab(NULL, ril, 0, pe, t, NULL, 0, 0);
605 memcpy(pe+1, src + sizeof(*pe), ((ril-1) * sizeof(*pe)));
606 memcpy(te, src + (ril * sizeof(*pe)), rdlen+entry->info.count+drlen);
609 entryInfo se = (entryInfo)src;
610 int off = ntohl(se->offset);
611 pe->offset = (off) ? htonl(te - dataStart) : htonl(off);
613 te += entry->info.count + drlen;
615 count = regionSwab(NULL, ril, 0, pe, t, NULL, 0, 0);
616 if (count != (rdlen + entry->info.count + drlen))
620 /* Skip rest of entries in region. */
621 while (i < h->indexUsed && entry->info.offset <= rid+1) {
631 /* Ignore deleted drips. */
632 if (entry->data == NULL || entry->length <= 0)
636 diff = alignDiff(entry->info.type, (te - dataStart));
642 pe->offset = htonl(te - dataStart);
644 /* copy data w/ endian conversions */
645 switch (entry->info.type) {
647 count = entry->info.count;
650 *((uint64_t *)te) = htonll(*((uint64_t *)src));
651 te += sizeof(uint64_t);
652 src += sizeof(uint64_t);
657 count = entry->info.count;
660 *((int32_t *)te) = htonl(*((int32_t *)src));
661 te += sizeof(int32_t);
662 src += sizeof(int32_t);
667 count = entry->info.count;
670 *((int16_t *)te) = htons(*((int16_t *)src));
671 te += sizeof(int16_t);
672 src += sizeof(int16_t);
677 memcpy(te, entry->data, entry->length);
684 /* Insure that there are no memcpy underruns/overruns. */
685 if (((char *)pe) != dataStart)
687 if ((((char *)ei)+len) != te)
702 void * headerUnload(Header h)
704 return headerExport(h, NULL);
708 * Find matching (tag,type) entry in header.
710 * @param tag entry tag
711 * @param type entry type
712 * @return header entry
715 indexEntry findEntry(Header h, rpmTagVal tag, rpm_tagtype_t type)
718 struct indexEntry_s key;
720 if (h == NULL) return NULL;
721 if (!(h->flags & HEADERFLAG_SORTED)) headerSort(h);
725 entry = bsearch(&key, h->index, h->indexUsed, sizeof(*h->index), indexCmp);
729 if (type == RPM_NULL_TYPE)
733 while (entry->info.tag == tag && entry->info.type != type &&
734 entry > h->index) entry--;
736 if (entry->info.tag == tag && entry->info.type == type)
742 int headerDel(Header h, rpmTagVal tag)
744 indexEntry last = h->index + h->indexUsed;
745 indexEntry entry, first;
748 entry = findEntry(h, tag, RPM_NULL_TYPE);
749 if (!entry) return 1;
751 /* Make sure entry points to the first occurence of this tag. */
752 while (entry > h->index && (entry - 1)->info.tag == tag)
755 /* Free data for tags being removed. */
756 for (first = entry; first < last; first++) {
758 if (first->info.tag != tag)
763 if (ENTRY_IN_REGION(first))
768 ne = (first - entry);
773 memmove(entry, first, (ne * sizeof(*entry)));
779 Header headerImport(void * blob, unsigned int bsize, headerImportFlags flags)
781 const int32_t * ei = (int32_t *) blob;
782 int32_t il = ntohl(ei[0]); /* index length */
783 int32_t dl = ntohl(ei[1]); /* data length */
784 unsigned int pvlen = sizeof(il) + sizeof(dl) +
785 (il * sizeof(struct entryInfo_s)) + dl;;
788 unsigned char * dataStart;
789 unsigned char * dataEnd;
792 int fast = (flags & HEADERIMPORT_FAST);
794 /* Sanity checks on header intro. */
795 if (bsize && bsize != pvlen)
797 if (hdrchkTags(il) || hdrchkData(dl) || pvlen >= headerMaxbytes)
800 h = headerCreate(blob, (flags & HEADERIMPORT_COPY) ? pvlen : 0, il);
802 ei = h->blob; /* In case we had to copy */
803 pe = (entryInfo) &ei[2];
804 dataStart = (unsigned char *) (pe + il);
805 dataEnd = dataStart + dl;
808 if (!(htonl(pe->tag) < RPMTAG_HEADERI18NTABLE)) {
809 h->flags |= HEADERFLAG_LEGACY;
810 entry->info.type = REGION_TAG_TYPE;
811 entry->info.tag = RPMTAG_HEADERIMAGE;
812 entry->info.count = REGION_TAG_COUNT;
813 entry->info.offset = ((unsigned char *)pe - dataStart); /* negative offset */
816 entry->length = pvlen - sizeof(il) - sizeof(dl);
817 rdlen = regionSwab(entry+1, il, 0, pe,
818 dataStart, dataEnd, entry->info.offset, fast);
821 entry->rdlen = rdlen;
827 h->flags &= ~HEADERFLAG_LEGACY;
829 entry->info.type = htonl(pe->type);
830 entry->info.count = htonl(pe->count);
831 entry->info.tag = htonl(pe->tag);
833 if (!ENTRY_IS_REGION(entry))
835 if (entry->info.type != REGION_TAG_TYPE)
837 if (entry->info.count != REGION_TAG_COUNT)
840 { int off = ntohl(pe->offset);
843 size_t nb = REGION_TAG_COUNT;
845 if (hdrchkRange(dl, (off + nb)))
847 /* XXX Hmm, why the copy? */
848 memcpy(&stei, dataStart + off, nb);
849 rdl = -ntohl(stei[2]); /* negative offset */
850 ril = rdl/sizeof(*pe);
851 if (hdrchkTags(ril) || hdrchkData(rdl))
855 rdl = (ril * sizeof(struct entryInfo_s));
856 entry->info.tag = RPMTAG_HEADERIMAGE;
859 entry->info.offset = -rdl; /* negative offset */
862 entry->length = pvlen - sizeof(il) - sizeof(dl);
863 rdlen = regionSwab(entry+1, ril-1, 0, pe+1,
864 dataStart, dataEnd, entry->info.offset, fast);
867 entry->rdlen = rdlen;
869 if (ril < h->indexUsed) {
870 indexEntry newEntry = entry + ril;
871 int ne = (h->indexUsed - ril);
872 int rid = entry->info.offset+1;
874 /* Load dribble entries from region. */
875 rdlen = regionSwab(newEntry, ne, rdlen, pe+ril,
876 dataStart, dataEnd, rid, fast);
880 { indexEntry firstEntry = newEntry;
881 int save = h->indexUsed;
884 /* Dribble entries replace duplicate region entries. */
886 for (j = 0; j < ne; j++, newEntry++) {
887 (void) headerDel(h, newEntry->info.tag);
888 if (newEntry->info.tag == RPMTAG_BASENAMES)
889 (void) headerDel(h, RPMTAG_OLDFILENAMES);
892 /* If any duplicate entries were replaced, move new entries down. */
893 if (h->indexUsed < (save - ne)) {
894 memmove(h->index + h->indexUsed, firstEntry,
895 (ne * sizeof(*entry)));
901 rdlen += REGION_TAG_COUNT;
907 h->flags &= ~HEADERFLAG_SORTED;
909 h->flags |= HEADERFLAG_ALLOCATED;
915 if (flags & HEADERIMPORT_COPY)
923 Header headerReload(Header h, rpmTagVal tag)
927 void * uh = headerExport(h, &uc);
932 nh = headerImport(uh, uc, 0);
937 if (ENTRY_IS_REGION(nh->index)) {
938 if (tag == RPMTAG_HEADERSIGNATURES || tag == RPMTAG_HEADERIMMUTABLE)
939 nh->index[0].info.tag = tag;
944 Header headerLoad(void * uh)
946 return headerImport(uh, 0, 0);
949 Header headerCopyLoad(const void * uh)
951 /* Discards const but that's ok as we'll take a copy */
952 return headerImport((void *)uh, 0, HEADERIMPORT_COPY);
955 Header headerRead(FD_t fd, int magicp)
962 unsigned int len, blen;
964 if (magicp == HEADER_MAGIC_YES) {
967 if (Freadall(fd, block, 4*sizeof(*block)) != 4*sizeof(*block))
972 if (memcmp(&magic, rpm_header_magic, sizeof(magic)))
975 il = ntohl(block[2]);
976 dl = ntohl(block[3]);
978 if (Freadall(fd, block, 2*sizeof(*block)) != 2*sizeof(*block))
981 il = ntohl(block[0]);
982 dl = ntohl(block[1]);
985 blen = (il * sizeof(struct entryInfo_s)) + dl;
986 len = sizeof(il) + sizeof(dl) + blen;
988 /* Sanity checks on header intro. */
989 if (hdrchkTags(il) || hdrchkData(dl) || len > headerMaxbytes)
996 if (Freadall(fd, (char *)&ei[2], blen) != blen)
999 h = headerImport(ei, len, 0);
1002 if (h == NULL && ei != NULL) {
1008 int headerWrite(FD_t fd, Header h, int magicp)
1011 unsigned int length;
1014 uh = headerExport(h, &length);
1018 case HEADER_MAGIC_YES:
1019 nb = Fwrite(rpm_header_magic, sizeof(uint8_t), sizeof(rpm_header_magic), fd);
1020 if (nb != sizeof(rpm_header_magic))
1023 case HEADER_MAGIC_NO:
1027 nb = Fwrite(uh, sizeof(char), length, fd);
1031 return (nb == length ? 0 : 1);
1034 int headerIsEntry(Header h, rpmTagVal tag)
1036 /* FIX: h modified by sort. */
1037 return (findEntry(h, tag, RPM_NULL_TYPE) ? 1 : 0);
1042 * Retrieve data from header entry.
1043 * Relevant flags (others are ignored), if neither is set allocation
1044 * behavior depends on data type(!)
1045 * HEADERGET_MINMEM: return pointers to header memory
1046 * HEADERGET_ALLOC: always return malloced memory, overrides MINMEM
1048 * @todo Permit retrieval of regions other than HEADER_IMUTABLE.
1049 * @param entry header entry
1050 * @param td tag data container
1051 * @param minMem string pointers refer to header memory?
1052 * @param flags flags to control memory allocation
1053 * @return 1 on success, otherwise error.
1055 static int copyTdEntry(const indexEntry entry, rpmtd td, headerGetFlags flags)
1057 rpm_count_t count = entry->info.count;
1058 int rc = 1; /* XXX 1 on success. */
1059 /* ALLOC overrides MINMEM */
1060 int allocMem = flags & HEADERGET_ALLOC;
1061 int minMem = allocMem ? 0 : flags & HEADERGET_MINMEM;
1062 int argvArray = (flags & HEADERGET_ARGV) ? 1 : 0;
1065 td->flags = RPMTD_IMMUTABLE;
1066 switch (entry->info.type) {
1069 * XXX This only works for
1070 * XXX "sealed" HEADER_IMMUTABLE/HEADER_SIGNATURES/HEADER_IMAGE.
1071 * XXX This will *not* work for unsealed legacy HEADER_IMAGE (i.e.
1072 * XXX a legacy header freshly read, but not yet unloaded to the rpmdb).
1074 if (ENTRY_IS_REGION(entry)) {
1075 int32_t * ei = ((int32_t *)entry->data) - 2;
1076 entryInfo pe = (entryInfo) (ei + 2);
1077 unsigned char * dataStart = (unsigned char *) (pe + ntohl(ei[0]));
1078 int32_t rdl = -entry->info.offset; /* negative offset */
1079 int32_t ril = rdl/sizeof(*pe);
1082 count = 2 * sizeof(*ei) + (ril * sizeof(*pe)) + rdl;
1083 if (entry->info.tag == RPMTAG_HEADERIMAGE) {
1087 count += REGION_TAG_COUNT;
1088 rdl += REGION_TAG_COUNT;
1091 td->data = xmalloc(count);
1092 ei = (int32_t *) td->data;
1096 pe = (entryInfo) memcpy(ei + 2, pe, (ril * sizeof(*pe)));
1098 dataStart = (unsigned char *) memcpy(pe + ril, dataStart, rdl);
1100 rc = regionSwab(NULL, ril, 0, pe, dataStart, dataStart + rdl, 0, 0);
1101 /* don't return data on failure */
1103 td->data = _free(td->data);
1105 /* XXX 1 on success. */
1106 rc = (rc < 0) ? 0 : 1;
1108 count = entry->length;
1110 ? memcpy(xmalloc(count), entry->data, count)
1114 case RPM_STRING_TYPE:
1115 /* simple string, but fallthrough if its actually an array */
1116 if (count == 1 && !argvArray) {
1117 td->data = allocMem ? xstrdup(entry->data) : entry->data;
1120 case RPM_STRING_ARRAY_TYPE:
1121 case RPM_I18NSTRING_TYPE:
1122 { const char ** ptrEntry;
1123 int tableSize = (count + argvArray) * sizeof(char *);
1128 td->data = xmalloc(tableSize);
1129 ptrEntry = (const char **) td->data;
1132 t = xmalloc(tableSize + entry->length);
1133 td->data = (void *)t;
1134 ptrEntry = (const char **) td->data;
1136 memcpy(t, entry->data, entry->length);
1138 for (i = 0; i < count; i++) {
1145 td->flags |= RPMTD_ARGV;
1150 case RPM_INT16_TYPE:
1151 case RPM_INT32_TYPE:
1152 case RPM_INT64_TYPE:
1154 td->data = xmalloc(entry->length);
1155 memcpy(td->data, entry->data, entry->length);
1157 td->data = entry->data;
1161 /* WTH? Don't mess with unknown data types... */
1166 td->type = entry->info.type;
1169 if (td->data && entry->data != td->data) {
1170 td->flags |= RPMTD_ALLOCED;
1177 * Does locale match entry in header i18n table?
1180 * The range [l,le) contains the next locale to match:
1181 * ll[_CC][.EEEEE][@dddd]
1183 * ll ISO language code (in lowercase).
1184 * CC (optional) ISO coutnry code (in uppercase).
1185 * EEEEE (optional) encoding (not really standardized).
1186 * dddd (optional) dialect.
1189 * @param td header i18n table data, NUL terminated
1190 * @param l start of locale to match
1191 * @param le end of locale to match
1192 * @return 1 on good match, 2 on weak match, 0 on no match
1194 static int headerMatchLocale(const char *td, const char *l, const char *le)
1198 /* First try a complete match. */
1199 if (strlen(td) == (le-l) && rstreqn(td, l, (le - l)))
1202 /* Next, try stripping optional dialect and matching. */
1203 for (fe = l; fe < le && *fe != '@'; fe++)
1205 if (fe < le && rstreqn(td, l, (fe - l)))
1208 /* Next, try stripping optional codeset and matching. */
1209 for (fe = l; fe < le && *fe != '.'; fe++)
1211 if (fe < le && rstreqn(td, l, (fe - l)))
1214 /* Finally, try stripping optional country code and matching. */
1215 for (fe = l; fe < le && *fe != '_'; fe++)
1217 if (fe < le && rstreqn(td, l, (fe - l)))
1224 * Return i18n string from header that matches locale.
1226 * @param entry i18n string data
1227 * @retval td tag data container
1228 * @param flags flags to control allocation
1231 static int copyI18NEntry(Header h, indexEntry entry, rpmtd td,
1232 headerGetFlags flags)
1234 const char *lang, *l, *le;
1237 td->type = RPM_STRING_TYPE;
1239 /* if no match, just return the first string */
1240 td->data = entry->data;
1242 /* XXX Drepper sez' this is the order. */
1243 if ((lang = getenv("LANGUAGE")) == NULL &&
1244 (lang = getenv("LC_ALL")) == NULL &&
1245 (lang = getenv("LC_MESSAGES")) == NULL &&
1246 (lang = getenv("LANG")) == NULL)
1249 if ((table = findEntry(h, RPMTAG_HEADERI18NTABLE, RPM_STRING_ARRAY_TYPE)) == NULL)
1252 for (l = lang; *l != '\0'; l = le) {
1254 char *ed, *ed_weak = NULL;
1257 while (*l && *l == ':') /* skip leading colons */
1261 for (le = l; *le && *le != ':'; le++) /* find end of this locale */
1264 /* For each entry in the header ... */
1265 for (langNum = 0, t = table->data, ed = entry->data;
1266 langNum < entry->info.count;
1267 langNum++, t += strlen(t) + 1, ed += strlen(ed) + 1) {
1269 int match = headerMatchLocale(t, l, le);
1273 } else if (match == 2) {
1284 if (flags & HEADERGET_ALLOC) {
1285 td->data = xstrdup(td->data);
1286 td->flags |= RPMTD_ALLOCED;
1293 * Retrieve tag data from header.
1295 * @retval td tag data container
1296 * @param flags flags to control retrieval
1297 * @return 1 on success, 0 on not found
1299 static int intGetTdEntry(Header h, rpmtd td, headerGetFlags flags)
1304 /* First find the tag */
1305 /* FIX: h modified by sort. */
1306 entry = findEntry(h, td->tag, RPM_NULL_TYPE);
1307 if (entry == NULL) {
1308 /* Td is zeroed above, just return... */
1312 if (flags & HEADERGET_RAW) {
1313 rc = copyTdEntry(entry, td, flags);
1315 switch (entry->info.type) {
1316 case RPM_I18NSTRING_TYPE:
1317 rc = copyI18NEntry(h, entry, td, flags);
1320 rc = copyTdEntry(entry, td, flags);
1326 td->flags |= RPMTD_INVALID;
1328 /* XXX 1 on success */
1329 return ((rc == 1) ? 1 : 0);
1332 int headerGet(Header h, rpmTagVal tag, rpmtd td, headerGetFlags flags)
1335 headerTagTagFunction tagfunc = intGetTdEntry;
1337 if (td == NULL) return 0;
1342 if (flags & HEADERGET_EXT) {
1343 headerTagTagFunction extfunc = rpmHeaderTagFunc(tag);
1344 if (extfunc) tagfunc = extfunc;
1346 rc = tagfunc(h, td, flags);
1348 assert(tag == td->tag);
1354 static void copyData(rpm_tagtype_t type, rpm_data_t dstPtr,
1355 rpm_constdata_t srcPtr, rpm_count_t cnt, int dataLength)
1358 case RPM_STRING_ARRAY_TYPE:
1359 case RPM_I18NSTRING_TYPE:
1360 { const char ** av = (const char **) srcPtr;
1363 while (cnt-- > 0 && dataLength > 0) {
1365 if ((s = *av++) == NULL)
1369 } while (s[-1] && --dataLength > 0);
1374 memmove(dstPtr, srcPtr, dataLength);
1380 * Return (malloc'ed) copy of entry data.
1381 * @param type entry data type
1382 * @param p entry data
1383 * @param c entry item count
1384 * @retval lengthPtr no. bytes in returned data
1385 * @return (malloc'ed) copy of entry data, NULL on error
1388 grabData(rpm_tagtype_t type, rpm_constdata_t p, rpm_count_t c, int * lengthPtr)
1390 rpm_data_t data = NULL;
1393 length = dataLength(type, p, c, 0, NULL);
1395 data = xmalloc(length);
1396 copyData(type, data, p, c, length);
1400 *lengthPtr = length;
1404 static int intAddEntry(Header h, rpmtd td)
1410 /* Count must always be >= 1 for headerAddEntry. */
1414 if (hdrchkType(td->type))
1416 if (hdrchkData(td->count))
1420 data = grabData(td->type, td->data, td->count, &length);
1421 if (data == NULL || length <= 0)
1424 /* Allocate more index space if necessary */
1425 if (h->indexUsed == h->indexAlloced) {
1426 h->indexAlloced += INDEX_MALLOC_SIZE;
1427 h->index = xrealloc(h->index, h->indexAlloced * sizeof(*h->index));
1430 /* Fill in the index */
1431 entry = h->index + h->indexUsed;
1432 entry->info.tag = td->tag;
1433 entry->info.type = td->type;
1434 entry->info.count = td->count;
1435 entry->info.offset = 0;
1437 entry->length = length;
1439 if (h->indexUsed > 0 && td->tag < h->index[h->indexUsed-1].info.tag)
1440 h->flags &= ~HEADERFLAG_SORTED;
1446 static int intAppendEntry(Header h, rpmtd td)
1451 if (td->type == RPM_STRING_TYPE || td->type == RPM_I18NSTRING_TYPE) {
1452 /* we can't do this */
1456 /* Find the tag entry in the header. */
1457 entry = findEntry(h, td->tag, td->type);
1461 length = dataLength(td->type, td->data, td->count, 0, NULL);
1465 if (ENTRY_IN_REGION(entry)) {
1466 char * t = xmalloc(entry->length + length);
1467 memcpy(t, entry->data, entry->length);
1469 entry->info.offset = 0;
1471 entry->data = xrealloc(entry->data, entry->length + length);
1473 copyData(td->type, ((char *) entry->data) + entry->length,
1474 td->data, td->count, length);
1476 entry->length += length;
1478 entry->info.count += td->count;
1483 int headerPut(Header h, rpmtd td, headerPutFlags flags)
1488 if (flags & HEADERPUT_APPEND) {
1489 rc = findEntry(h, td->tag, td->type) ?
1490 intAppendEntry(h, td) :
1493 rc = intAddEntry(h, td);
1498 int headerAddI18NString(Header h, rpmTagVal tag, const char * string,
1501 indexEntry table, entry;
1502 const char ** strArray;
1505 rpm_count_t i, langNum;
1508 table = findEntry(h, RPMTAG_HEADERI18NTABLE, RPM_STRING_ARRAY_TYPE);
1509 entry = findEntry(h, tag, RPM_I18NSTRING_TYPE);
1511 if (!table && entry)
1512 return 0; /* this shouldn't ever happen!! */
1514 if (!table && !entry) {
1515 const char * charArray[2];
1516 rpm_count_t count = 0;
1518 if (!lang || (lang[0] == 'C' && lang[1] == '\0')) {
1519 charArray[count++] = "C";
1521 charArray[count++] = "C";
1522 charArray[count++] = lang;
1526 td.tag = RPMTAG_HEADERI18NTABLE;
1527 td.type = RPM_STRING_ARRAY_TYPE;
1528 td.data = (void *) charArray;
1530 if (!headerPut(h, &td, HEADERPUT_DEFAULT))
1532 table = findEntry(h, RPMTAG_HEADERI18NTABLE, RPM_STRING_ARRAY_TYPE);
1537 if (!lang) lang = "C";
1539 { const char * l = table->data;
1540 for (langNum = 0; langNum < table->info.count; langNum++) {
1541 if (rstreq(l, lang)) break;
1546 if (langNum >= table->info.count) {
1547 length = strlen(lang) + 1;
1548 if (ENTRY_IN_REGION(table)) {
1549 char * t = xmalloc(table->length + length);
1550 memcpy(t, table->data, table->length);
1552 table->info.offset = 0;
1554 table->data = xrealloc(table->data, table->length + length);
1555 memmove(((char *)table->data) + table->length, lang, length);
1556 table->length += length;
1557 table->info.count++;
1563 strArray = xmalloc(sizeof(*strArray) * (langNum + 1));
1564 for (i = 0; i < langNum; i++)
1566 strArray[langNum] = string;
1570 td.type = RPM_I18NSTRING_TYPE;
1572 td.count = langNum + 1;
1573 rc = headerPut(h, &td, HEADERPUT_DEFAULT);
1576 } else if (langNum >= entry->info.count) {
1577 ghosts = langNum - entry->info.count;
1579 length = strlen(string) + 1 + ghosts;
1580 if (ENTRY_IN_REGION(entry)) {
1581 char * t = xmalloc(entry->length + length);
1582 memcpy(t, entry->data, entry->length);
1584 entry->info.offset = 0;
1586 entry->data = xrealloc(entry->data, entry->length + length);
1588 memset(((char *)entry->data) + entry->length, '\0', ghosts);
1589 memmove(((char *)entry->data) + entry->length + ghosts, string, strlen(string)+1);
1591 entry->length += length;
1592 entry->info.count = langNum + 1;
1594 char *b, *be, *e, *ee, *t;
1597 /* Set beginning/end pointers to previous data */
1598 b = be = e = ee = entry->data;
1599 for (i = 0; i < table->info.count; i++) {
1602 ee += strlen(ee) + 1;
1607 /* Get storage for new buffer */
1609 sn = strlen(string) + 1;
1611 length = bn + sn + en;
1612 t = buf = xmalloc(length);
1614 /* Copy values into new storage */
1617 memcpy(t, string, sn);
1622 /* Replace i18N string array */
1623 entry->length -= strlen(be) + 1;
1624 entry->length += sn;
1626 if (ENTRY_IN_REGION(entry)) {
1627 entry->info.offset = 0;
1629 entry->data = _free(entry->data);
1636 int headerMod(Header h, rpmtd td)
1643 /* First find the tag */
1644 entry = findEntry(h, td->tag, td->type);
1649 data = grabData(td->type, td->data, td->count, &length);
1650 if (data == NULL || length <= 0)
1653 /* make sure entry points to the first occurence of this tag */
1654 while (entry > h->index && (entry - 1)->info.tag == td->tag)
1657 /* free after we've grabbed the new data in case the two are intertwined;
1658 that's a bad idea but at least we won't break */
1659 oldData = entry->data;
1661 entry->info.count = td->count;
1662 entry->info.type = td->type;
1664 entry->length = length;
1666 if (ENTRY_IN_REGION(entry)) {
1667 entry->info.offset = 0;
1675 * Header tag iterator data structure.
1677 struct headerIterator_s {
1678 Header h; /*!< Header being iterated. */
1679 int next_index; /*!< Next tag index. */
1682 HeaderIterator headerFreeIterator(HeaderIterator hi)
1685 hi->h = headerFree(hi->h);
1691 HeaderIterator headerInitIterator(Header h)
1693 HeaderIterator hi = xmalloc(sizeof(*hi));
1697 hi->h = headerLink(h);
1702 static indexEntry nextIndex(HeaderIterator hi)
1706 indexEntry entry = NULL;
1708 for (slot = hi->next_index; slot < h->indexUsed; slot++) {
1709 entry = h->index + slot;
1710 if (!ENTRY_IS_REGION(entry))
1713 hi->next_index = slot;
1714 if (entry == NULL || slot >= h->indexUsed)
1721 rpmTagVal headerNextTag(HeaderIterator hi)
1723 indexEntry entry = nextIndex(hi);
1724 return entry ? entry->info.tag : RPMTAG_NOT_FOUND;
1727 int headerNext(HeaderIterator hi, rpmtd td)
1729 indexEntry entry = nextIndex(hi);
1734 td->tag = entry->info.tag;
1735 rc = copyTdEntry(entry, td, HEADERGET_DEFAULT);
1737 return ((rc == 1) ? 1 : 0);
1740 unsigned int headerGetInstance(Header h)
1742 return h ? h->instance : 0;
1745 void headerSetInstance(Header h, unsigned int instance)
1747 h->instance = instance;
1750 #define RETRY_ERROR(_err) \
1751 ((_err) == EINTR || (_err) == EAGAIN || (_err) == EWOULDBLOCK)
1753 ssize_t Freadall(FD_t fd, void * buf, ssize_t size)
1759 while (total < size) {
1760 nb = Fread(bufp, 1, size - total, fd);
1762 if (nb == 0 || (nb < 0 && !RETRY_ERROR(errno))) {