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)
133 return NULL; /* XXX return previous header? */
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))
213 static int indexCmp(const void * avp, const void * bvp)
215 indexEntry ap = (indexEntry) avp, bp = (indexEntry) bvp;
216 return (ap->info.tag - bp->info.tag);
219 void headerSort(Header h)
221 if (!(h->flags & HEADERFLAG_SORTED)) {
222 qsort(h->index, h->indexUsed, sizeof(*h->index), indexCmp);
223 h->flags |= HEADERFLAG_SORTED;
229 static int offsetCmp(const void * avp, const void * bvp)
231 indexEntry ap = (indexEntry) avp, bp = (indexEntry) bvp;
232 int rc = (ap->info.offset - bp->info.offset);
235 /* Within a region, entries sort by address. Added drips sort by tag. */
236 if (ap->info.offset < 0)
237 rc = (((char *)ap->data) - ((char *)bp->data));
239 rc = (ap->info.tag - bp->info.tag);
245 * Restore tags in header to original ordering.
248 void headerUnsort(Header h)
250 if (h->flags & HEADERFLAG_SORTED) {
251 qsort(h->index, h->indexUsed, sizeof(*h->index), offsetCmp);
252 h->flags &= ~HEADERFLAG_SORTED;
256 unsigned headerSizeof(Header h, int magicp)
259 unsigned int size = 0;
268 case HEADER_MAGIC_YES:
269 size += sizeof(rpm_header_magic);
271 case HEADER_MAGIC_NO:
275 size += 2 * sizeof(int32_t); /* count of index entries */
277 for (i = 0, entry = h->index; i < h->indexUsed; i++, entry++) {
280 /* Regions go in as is ... */
281 if (ENTRY_IS_REGION(entry)) {
282 size += entry->length;
283 /* XXX Legacy regions do not include the region tag and data. */
284 if (i == 0 && (h->flags & HEADERFLAG_LEGACY))
285 size += sizeof(struct entryInfo_s) + entry->info.count;
289 /* ... and region elements are skipped. */
290 if (entry->info.offset < 0)
294 type = entry->info.type;
295 if (typeSizes[type] > 1) {
296 unsigned diff = typeSizes[type] - (size % typeSizes[type]);
297 if (diff != typeSizes[type]) {
302 size += sizeof(struct entryInfo_s) + entry->length;
309 * Return length of entry data.
310 * @param type entry data type
311 * @param p entry data
312 * @param count entry item count
313 * @param onDisk data is concatenated strings (with NUL's))?
314 * @param pend pointer to end of data (or NULL)
315 * @return no. bytes in data, -1 on failure
317 static int dataLength(rpm_tagtype_t type, rpm_constdata_t p, rpm_count_t count,
318 int onDisk, rpm_constdata_t pend)
320 const unsigned char * s = p;
321 const unsigned char * se = pend;
325 case RPM_STRING_TYPE:
333 length++; /* count nul terminator too. */
336 case RPM_STRING_ARRAY_TYPE:
337 case RPM_I18NSTRING_TYPE:
338 /* These are like RPM_STRING_TYPE, except they're *always* an array */
339 /* Compute sum of length of all strings, including nul terminators */
343 length++; /* count nul terminator too */
351 const char ** av = (const char **)p;
353 /* add one for null termination */
354 length += strlen(*av++) + 1;
360 if (typeSizes[type] == -1)
362 length = typeSizes[(type & 0xf)] * count;
363 if (length < 0 || (se && (s + length) > se))
372 * Swap int32_t and int16_t arrays within header region.
374 * If a header region tag is in the set to be swabbed, as the data for a
375 * a header region is located after all other tag data.
377 * @param entry header entry
378 * @param il no. of entries
379 * @param dl start no. bytes of data
380 * @param pe header physical entry pointer (swapped)
381 * @param dataStart header data start
382 * @param dataEnd header data end
383 * @param regionid region offset
384 * @return no. bytes of data in region, -1 on error
386 static int regionSwab(indexEntry entry, int il, int dl,
388 unsigned char * dataStart,
389 const unsigned char * dataEnd,
392 for (; il > 0; il--, pe++) {
393 struct indexEntry_s ie;
396 ie.info.tag = ntohl(pe->tag);
397 ie.info.type = ntohl(pe->type);
398 ie.info.count = ntohl(pe->count);
399 ie.info.offset = ntohl(pe->offset);
401 if (hdrchkType(ie.info.type))
403 if (hdrchkData(ie.info.count))
405 if (hdrchkData(ie.info.offset))
407 if (hdrchkAlign(ie.info.type, ie.info.offset))
410 ie.data = dataStart + ie.info.offset;
411 if (dataEnd && (unsigned char *)ie.data >= dataEnd)
414 ie.length = dataLength(ie.info.type, ie.data, ie.info.count, 1, dataEnd);
415 if (ie.length < 0 || hdrchkData(ie.length))
421 ie.info.offset = regionid;
422 *entry = ie; /* structure assignment */
428 if (typeSizes[type] > 1) {
429 unsigned diff = typeSizes[type] - (dl % typeSizes[type]);
430 if (diff != typeSizes[type]) {
435 /* Perform endian conversions */
436 switch (ntohl(pe->type)) {
438 { uint64_t * it = ie.data;
439 for (; ie.info.count > 0; ie.info.count--, it += 1) {
440 if (dataEnd && ((unsigned char *)it) >= dataEnd)
446 { int32_t * it = ie.data;
447 for (; ie.info.count > 0; ie.info.count--, it += 1) {
448 if (dataEnd && ((unsigned char *)it) >= dataEnd)
454 { int16_t * it = ie.data;
455 for (; ie.info.count > 0; ie.info.count--, it += 1) {
456 if (dataEnd && ((unsigned char *)it) >= dataEnd)
472 * @retval *lengthPtr no. bytes in unloaded header blob
473 * @return unloaded header blob (NULL on error)
475 static void * doHeaderUnload(Header h,
488 int drlen, ndribbles;
490 if (h == NULL) return NULL;
492 /* Sort entries by (offset,tag). */
495 /* Compute (il,dl) for all tags, including those deleted in region. */
496 drlen = ndribbles = 0;
497 for (i = 0, entry = h->index; i < h->indexUsed; i++, entry++) {
498 if (ENTRY_IS_REGION(entry)) {
499 int32_t rdl = -entry->info.offset; /* negative offset */
500 int32_t ril = rdl/sizeof(*pe);
501 int rid = entry->info.offset;
504 dl += entry->rdlen + entry->info.count;
505 /* XXX Legacy regions do not include the region tag and data. */
506 if (i == 0 && (h->flags & HEADERFLAG_LEGACY))
509 /* Skip rest of entries in region, but account for dribbles. */
510 for (; i < h->indexUsed && entry->info.offset <= rid+1; i++, entry++) {
511 if (entry->info.offset <= rid)
515 type = entry->info.type;
516 if (typeSizes[type] > 1) {
517 unsigned diff = typeSizes[type] - (dl % typeSizes[type]);
518 if (diff != typeSizes[type]) {
526 drlen += entry->length;
534 /* Ignore deleted drips. */
535 if (entry->data == NULL || entry->length <= 0)
539 type = entry->info.type;
540 if (typeSizes[type] > 1) {
541 unsigned diff = typeSizes[type] - (dl % typeSizes[type]);
542 if (diff != typeSizes[type]) {
551 /* Sanity checks on header intro. */
552 if (hdrchkTags(il) || hdrchkData(dl))
555 len = sizeof(il) + sizeof(dl) + (il * sizeof(*pe)) + dl;
561 pe = (entryInfo) &ei[2];
562 dataStart = te = (char *) (pe + il);
564 for (i = 0, entry = h->index; i < h->indexUsed; i++, entry++) {
570 if (entry->data == NULL || entry->length <= 0)
573 t = (unsigned char*)te;
574 pe->tag = htonl(entry->info.tag);
575 pe->type = htonl(entry->info.type);
576 pe->count = htonl(entry->info.count);
578 if (ENTRY_IS_REGION(entry)) {
579 int32_t rdl = -entry->info.offset; /* negative offset */
580 int32_t ril = rdl/sizeof(*pe) + ndribbles;
581 int rid = entry->info.offset;
583 src = (char *)entry->data;
584 rdlen = entry->rdlen;
586 /* XXX Legacy regions do not include the region tag and data. */
587 if (i == 0 && (h->flags & HEADERFLAG_LEGACY)) {
590 memcpy(pe+1, src, rdl);
591 memcpy(te, src + rdl, rdlen);
594 pe->offset = htonl(te - dataStart);
597 stei[2] = htonl(-rdl-entry->info.count);
599 memcpy(te, stei, entry->info.count);
600 te += entry->info.count;
602 rdlen += entry->info.count;
604 count = regionSwab(NULL, ril, 0, pe, t, NULL, 0);
610 memcpy(pe+1, src + sizeof(*pe), ((ril-1) * sizeof(*pe)));
611 memcpy(te, src + (ril * sizeof(*pe)), rdlen+entry->info.count+drlen);
614 entryInfo se = (entryInfo)src;
615 int off = ntohl(se->offset);
616 pe->offset = (off) ? htonl(te - dataStart) : htonl(off);
618 te += entry->info.count + drlen;
620 count = regionSwab(NULL, ril, 0, pe, t, NULL, 0);
621 if (count != (rdlen + entry->info.count + drlen))
625 /* Skip rest of entries in region. */
626 while (i < h->indexUsed && entry->info.offset <= rid+1) {
636 /* Ignore deleted drips. */
637 if (entry->data == NULL || entry->length <= 0)
641 type = entry->info.type;
642 if (typeSizes[type] > 1) {
644 diff = typeSizes[type] - ((te - dataStart) % typeSizes[type]);
645 if (diff != typeSizes[type]) {
651 pe->offset = htonl(te - dataStart);
653 /* copy data w/ endian conversions */
654 switch (entry->info.type) {
656 count = entry->info.count;
659 *((uint64_t *)te) = htonll(*((uint64_t *)src));
660 te += sizeof(uint64_t);
661 src += sizeof(uint64_t);
666 count = entry->info.count;
669 *((int32_t *)te) = htonl(*((int32_t *)src));
670 te += sizeof(int32_t);
671 src += sizeof(int32_t);
676 count = entry->info.count;
679 *((int16_t *)te) = htons(*((int16_t *)src));
680 te += sizeof(int16_t);
681 src += sizeof(int16_t);
686 memcpy(te, entry->data, entry->length);
693 /* Insure that there are no memcpy underruns/overruns. */
694 if (((char *)pe) != dataStart)
696 if ((((char *)ei)+len) != te)
711 void * headerUnload(Header h)
714 void * uh = doHeaderUnload(h, &length);
719 * Find matching (tag,type) entry in header.
721 * @param tag entry tag
722 * @param type entry type
723 * @return header entry
726 indexEntry findEntry(Header h, rpmTagVal tag, rpm_tagtype_t type)
729 struct indexEntry_s key;
731 if (h == NULL) return NULL;
732 if (!(h->flags & HEADERFLAG_SORTED)) headerSort(h);
736 entry = bsearch(&key, h->index, h->indexUsed, sizeof(*h->index), indexCmp);
740 if (type == RPM_NULL_TYPE)
744 while (entry->info.tag == tag && entry->info.type != type &&
745 entry > h->index) entry--;
747 if (entry->info.tag == tag && entry->info.type == type)
753 int headerDel(Header h, rpmTagVal tag)
755 indexEntry last = h->index + h->indexUsed;
756 indexEntry entry, first;
759 entry = findEntry(h, tag, RPM_NULL_TYPE);
760 if (!entry) return 1;
762 /* Make sure entry points to the first occurence of this tag. */
763 while (entry > h->index && (entry - 1)->info.tag == tag)
766 /* Free data for tags being removed. */
767 for (first = entry; first < last; first++) {
769 if (first->info.tag != tag)
774 if (ENTRY_IN_REGION(first))
779 ne = (first - entry);
784 memmove(entry, first, (ne * sizeof(*entry)));
790 Header headerLoad(void * uh)
792 int32_t * ei = (int32_t *) uh;
793 int32_t il = ntohl(ei[0]); /* index length */
794 int32_t dl = ntohl(ei[1]); /* data length */
795 size_t pvlen = sizeof(il) + sizeof(dl) +
796 (il * sizeof(struct entryInfo_s)) + dl;
800 unsigned char * dataStart;
801 unsigned char * dataEnd;
805 /* Sanity checks on header intro. */
806 if (hdrchkTags(il) || hdrchkData(dl))
810 pe = (entryInfo) &ei[2];
811 dataStart = (unsigned char *) (pe + il);
812 dataEnd = dataStart + dl;
814 h = headerCreate(uh, il);
817 if (!(htonl(pe->tag) < RPMTAG_HEADERI18NTABLE)) {
818 h->flags |= HEADERFLAG_LEGACY;
819 entry->info.type = REGION_TAG_TYPE;
820 entry->info.tag = RPMTAG_HEADERIMAGE;
821 entry->info.count = REGION_TAG_COUNT;
822 entry->info.offset = ((unsigned char *)pe - dataStart); /* negative offset */
825 entry->length = pvlen - sizeof(il) - sizeof(dl);
826 rdlen = regionSwab(entry+1, il, 0, pe, dataStart, dataEnd, entry->info.offset);
829 entry->rdlen = rdlen;
835 h->flags &= ~HEADERFLAG_LEGACY;
837 entry->info.type = htonl(pe->type);
838 entry->info.count = htonl(pe->count);
840 if (hdrchkType(entry->info.type))
842 if (hdrchkTags(entry->info.count))
845 { int off = ntohl(pe->offset);
850 size_t nb = REGION_TAG_COUNT;
852 /* XXX Hmm, why the copy? */
853 memcpy(&stei, dataStart + off, nb);
854 rdl = -ntohl(stei[2]); /* negative offset */
855 ril = rdl/sizeof(*pe);
856 if (hdrchkTags(ril) || hdrchkData(rdl))
858 entry->info.tag = htonl(pe->tag);
861 rdl = (ril * sizeof(struct entryInfo_s));
862 entry->info.tag = RPMTAG_HEADERIMAGE;
865 entry->info.offset = -rdl; /* negative offset */
868 entry->length = pvlen - sizeof(il) - sizeof(dl);
869 rdlen = regionSwab(entry+1, ril-1, 0, pe+1, dataStart, dataEnd, entry->info.offset);
872 entry->rdlen = rdlen;
874 if (ril < h->indexUsed) {
875 indexEntry newEntry = entry + ril;
876 int ne = (h->indexUsed - ril);
877 int rid = entry->info.offset+1;
880 /* Load dribble entries from region. */
881 rc = regionSwab(newEntry, ne, 0, pe+ril, dataStart, dataEnd, rid);
886 { indexEntry firstEntry = newEntry;
887 int save = h->indexUsed;
890 /* Dribble entries replace duplicate region entries. */
892 for (j = 0; j < ne; j++, newEntry++) {
893 (void) headerDel(h, newEntry->info.tag);
894 if (newEntry->info.tag == RPMTAG_BASENAMES)
895 (void) headerDel(h, RPMTAG_OLDFILENAMES);
898 /* If any duplicate entries were replaced, move new entries down. */
899 if (h->indexUsed < (save - ne)) {
900 memmove(h->index + h->indexUsed, firstEntry,
901 (ne * sizeof(*entry)));
908 h->flags &= ~HEADERFLAG_SORTED;
910 h->flags |= HEADERFLAG_ALLOCATED;
916 h->index = _free(h->index);
922 Header headerReload(Header h, rpmTagVal tag)
926 void * uh = doHeaderUnload(h, &length);
936 if (ENTRY_IS_REGION(nh->index)) {
937 if (tag == RPMTAG_HEADERSIGNATURES || tag == RPMTAG_HEADERIMMUTABLE)
938 nh->index[0].info.tag = tag;
943 Header headerCopyLoad(const void * uh)
945 int32_t * ei = (int32_t *) uh;
946 int32_t il = ntohl(ei[0]); /* index length */
947 int32_t dl = ntohl(ei[1]); /* data length */
948 size_t pvlen = sizeof(il) + sizeof(dl) +
949 (il * sizeof(struct entryInfo_s)) + dl;
953 /* Sanity checks on header intro. */
954 if (!(hdrchkTags(il) || hdrchkData(dl)) && pvlen < headerMaxbytes) {
955 nuh = memcpy(xmalloc(pvlen), uh, pvlen);
956 if ((h = headerLoad(nuh)) == NULL)
963 * Read (and load) header from file handle.
964 * @param fd file handle
965 * @param magicp read (and verify) 8 bytes of (magic, 0)?
966 * @return header (or NULL on error)
968 Header headerRead(FD_t fd, int magicp)
977 if (magicp == HEADER_MAGIC_YES) {
980 if (Fread(block, 1, 4*sizeof(*block), fd) != 4*sizeof(*block))
985 if (memcmp(&magic, rpm_header_magic, sizeof(magic)))
988 il = ntohl(block[2]);
989 dl = ntohl(block[3]);
991 if (Fread(block, 1, 2*sizeof(*block), fd) != 2*sizeof(*block))
994 il = ntohl(block[0]);
995 dl = ntohl(block[1]);
998 len = sizeof(il) + sizeof(dl) + (il * sizeof(struct entryInfo_s)) + dl;
1000 /* Sanity checks on header intro. */
1001 if (hdrchkTags(il) || hdrchkData(dl) || len > headerMaxbytes)
1007 len -= sizeof(il) + sizeof(dl);
1009 if (Fread((char *)&ei[2], 1, len, fd) != len)
1015 if (h == NULL && ei != NULL) {
1021 int headerWrite(FD_t fd, Header h, int magicp)
1027 uh = doHeaderUnload(h, &length);
1031 case HEADER_MAGIC_YES:
1032 nb = Fwrite(rpm_header_magic, sizeof(uint8_t), sizeof(rpm_header_magic), fd);
1033 if (nb != sizeof(rpm_header_magic))
1036 case HEADER_MAGIC_NO:
1040 nb = Fwrite(uh, sizeof(char), length, fd);
1044 return (nb == length ? 0 : 1);
1047 int headerIsEntry(Header h, rpmTagVal tag)
1049 /* FIX: h modified by sort. */
1050 return (findEntry(h, tag, RPM_NULL_TYPE) ? 1 : 0);
1055 * Retrieve data from header entry.
1056 * Relevant flags (others are ignored), if neither is set allocation
1057 * behavior depends on data type(!)
1058 * HEADERGET_MINMEM: return pointers to header memory
1059 * HEADERGET_ALLOC: always return malloced memory, overrides MINMEM
1061 * @todo Permit retrieval of regions other than HEADER_IMUTABLE.
1062 * @param entry header entry
1063 * @param td tag data container
1064 * @param minMem string pointers refer to header memory?
1065 * @param flags flags to control memory allocation
1066 * @return 1 on success, otherwise error.
1068 static int copyTdEntry(const indexEntry entry, rpmtd td, headerGetFlags flags)
1070 rpm_count_t count = entry->info.count;
1071 int rc = 1; /* XXX 1 on success. */
1072 /* ALLOC overrides MINMEM */
1073 int allocMem = flags & HEADERGET_ALLOC;
1074 int minMem = allocMem ? 0 : flags & HEADERGET_MINMEM;
1075 int argvArray = (flags & HEADERGET_ARGV) ? 1 : 0;
1078 td->flags = RPMTD_IMMUTABLE;
1079 switch (entry->info.type) {
1082 * XXX This only works for
1083 * XXX "sealed" HEADER_IMMUTABLE/HEADER_SIGNATURES/HEADER_IMAGE.
1084 * XXX This will *not* work for unsealed legacy HEADER_IMAGE (i.e.
1085 * XXX a legacy header freshly read, but not yet unloaded to the rpmdb).
1087 if (ENTRY_IS_REGION(entry)) {
1088 int32_t * ei = ((int32_t *)entry->data) - 2;
1089 entryInfo pe = (entryInfo) (ei + 2);
1090 unsigned char * dataStart = (unsigned char *) (pe + ntohl(ei[0]));
1091 int32_t rdl = -entry->info.offset; /* negative offset */
1092 int32_t ril = rdl/sizeof(*pe);
1095 count = 2 * sizeof(*ei) + (ril * sizeof(*pe)) + rdl;
1096 if (entry->info.tag == RPMTAG_HEADERIMAGE) {
1100 count += REGION_TAG_COUNT;
1101 rdl += REGION_TAG_COUNT;
1104 td->data = xmalloc(count);
1105 ei = (int32_t *) td->data;
1109 pe = (entryInfo) memcpy(ei + 2, pe, (ril * sizeof(*pe)));
1111 dataStart = (unsigned char *) memcpy(pe + ril, dataStart, rdl);
1113 rc = regionSwab(NULL, ril, 0, pe, dataStart, dataStart + rdl, 0);
1114 /* don't return data on failure */
1116 td->data = _free(td->data);
1118 /* XXX 1 on success. */
1119 rc = (rc < 0) ? 0 : 1;
1121 count = entry->length;
1123 ? memcpy(xmalloc(count), entry->data, count)
1127 case RPM_STRING_TYPE:
1128 /* simple string, but fallthrough if its actually an array */
1129 if (count == 1 && !argvArray) {
1130 td->data = allocMem ? xstrdup(entry->data) : entry->data;
1133 case RPM_STRING_ARRAY_TYPE:
1134 case RPM_I18NSTRING_TYPE:
1135 { const char ** ptrEntry;
1136 int tableSize = (count + argvArray) * sizeof(char *);
1141 td->data = xmalloc(tableSize);
1142 ptrEntry = (const char **) td->data;
1145 t = xmalloc(tableSize + entry->length);
1146 td->data = (void *)t;
1147 ptrEntry = (const char **) td->data;
1149 memcpy(t, entry->data, entry->length);
1151 for (i = 0; i < count; i++) {
1158 td->flags |= RPMTD_ARGV;
1163 case RPM_INT16_TYPE:
1164 case RPM_INT32_TYPE:
1165 case RPM_INT64_TYPE:
1167 td->data = xmalloc(entry->length);
1168 memcpy(td->data, entry->data, entry->length);
1170 td->data = entry->data;
1174 /* WTH? Don't mess with unknown data types... */
1179 td->type = entry->info.type;
1182 if (td->data && entry->data != td->data) {
1183 td->flags |= RPMTD_ALLOCED;
1190 * Does locale match entry in header i18n table?
1193 * The range [l,le) contains the next locale to match:
1194 * ll[_CC][.EEEEE][@dddd]
1196 * ll ISO language code (in lowercase).
1197 * CC (optional) ISO coutnry code (in uppercase).
1198 * EEEEE (optional) encoding (not really standardized).
1199 * dddd (optional) dialect.
1202 * @param td header i18n table data, NUL terminated
1203 * @param l start of locale to match
1204 * @param le end of locale to match
1205 * @return 1 on good match, 2 on weak match, 0 on no match
1207 static int headerMatchLocale(const char *td, const char *l, const char *le)
1211 /* First try a complete match. */
1212 if (strlen(td) == (le-l) && rstreqn(td, l, (le - l)))
1215 /* Next, try stripping optional dialect and matching. */
1216 for (fe = l; fe < le && *fe != '@'; fe++)
1218 if (fe < le && rstreqn(td, l, (fe - l)))
1221 /* Next, try stripping optional codeset and matching. */
1222 for (fe = l; fe < le && *fe != '.'; fe++)
1224 if (fe < le && rstreqn(td, l, (fe - l)))
1227 /* Finally, try stripping optional country code and matching. */
1228 for (fe = l; fe < le && *fe != '_'; fe++)
1230 if (fe < le && rstreqn(td, l, (fe - l)))
1237 * Return i18n string from header that matches locale.
1239 * @param entry i18n string data
1240 * @retval td tag data container
1241 * @param flags flags to control allocation
1244 static int copyI18NEntry(Header h, indexEntry entry, rpmtd td,
1245 headerGetFlags flags)
1247 const char *lang, *l, *le;
1250 td->type = RPM_STRING_TYPE;
1252 /* if no match, just return the first string */
1253 td->data = entry->data;
1255 /* XXX Drepper sez' this is the order. */
1256 if ((lang = getenv("LANGUAGE")) == NULL &&
1257 (lang = getenv("LC_ALL")) == NULL &&
1258 (lang = getenv("LC_MESSAGES")) == NULL &&
1259 (lang = getenv("LANG")) == NULL)
1262 if ((table = findEntry(h, RPMTAG_HEADERI18NTABLE, RPM_STRING_ARRAY_TYPE)) == NULL)
1265 for (l = lang; *l != '\0'; l = le) {
1267 char *ed, *ed_weak = NULL;
1270 while (*l && *l == ':') /* skip leading colons */
1274 for (le = l; *le && *le != ':'; le++) /* find end of this locale */
1277 /* For each entry in the header ... */
1278 for (langNum = 0, t = table->data, ed = entry->data;
1279 langNum < entry->info.count;
1280 langNum++, t += strlen(t) + 1, ed += strlen(ed) + 1) {
1282 int match = headerMatchLocale(t, l, le);
1286 } else if (match == 2) {
1297 if (flags & HEADERGET_ALLOC) {
1298 td->data = xstrdup(td->data);
1299 td->flags |= RPMTD_ALLOCED;
1306 * Retrieve tag data from header.
1308 * @retval td tag data container
1309 * @param flags flags to control retrieval
1310 * @return 1 on success, 0 on not found
1312 static int intGetTdEntry(Header h, rpmtd td, headerGetFlags flags)
1317 /* First find the tag */
1318 /* FIX: h modified by sort. */
1319 entry = findEntry(h, td->tag, RPM_NULL_TYPE);
1320 if (entry == NULL) {
1321 /* Td is zeroed above, just return... */
1325 if (flags & HEADERGET_RAW) {
1326 rc = copyTdEntry(entry, td, flags);
1328 switch (entry->info.type) {
1329 case RPM_I18NSTRING_TYPE:
1330 rc = copyI18NEntry(h, entry, td, flags);
1333 rc = copyTdEntry(entry, td, flags);
1338 /* XXX 1 on success */
1339 return ((rc == 1) ? 1 : 0);
1342 int headerGet(Header h, rpmTagVal tag, rpmtd td, headerGetFlags flags)
1345 headerTagTagFunction tagfunc = intGetTdEntry;
1347 if (td == NULL) return 0;
1352 if (flags & HEADERGET_EXT) {
1353 headerTagTagFunction extfunc = rpmHeaderTagFunc(tag);
1354 if (extfunc) tagfunc = extfunc;
1356 rc = tagfunc(h, td, flags);
1358 assert(tag == td->tag);
1364 static void copyData(rpm_tagtype_t type, rpm_data_t dstPtr,
1365 rpm_constdata_t srcPtr, rpm_count_t cnt, int dataLength)
1368 case RPM_STRING_ARRAY_TYPE:
1369 case RPM_I18NSTRING_TYPE:
1370 { const char ** av = (const char **) srcPtr;
1373 while (cnt-- > 0 && dataLength > 0) {
1375 if ((s = *av++) == NULL)
1379 } while (s[-1] && --dataLength > 0);
1384 memmove(dstPtr, srcPtr, dataLength);
1390 * Return (malloc'ed) copy of entry data.
1391 * @param type entry data type
1392 * @param p entry data
1393 * @param c entry item count
1394 * @retval lengthPtr no. bytes in returned data
1395 * @return (malloc'ed) copy of entry data, NULL on error
1398 grabData(rpm_tagtype_t type, rpm_constdata_t p, rpm_count_t c, int * lengthPtr)
1400 rpm_data_t data = NULL;
1403 length = dataLength(type, p, c, 0, NULL);
1405 data = xmalloc(length);
1406 copyData(type, data, p, c, length);
1410 *lengthPtr = length;
1414 static int intAddEntry(Header h, rpmtd td)
1420 /* Count must always be >= 1 for headerAddEntry. */
1424 if (hdrchkType(td->type))
1426 if (hdrchkData(td->count))
1430 data = grabData(td->type, td->data, td->count, &length);
1431 if (data == NULL || length <= 0)
1434 /* Allocate more index space if necessary */
1435 if (h->indexUsed == h->indexAlloced) {
1436 h->indexAlloced += INDEX_MALLOC_SIZE;
1437 h->index = xrealloc(h->index, h->indexAlloced * sizeof(*h->index));
1440 /* Fill in the index */
1441 entry = h->index + h->indexUsed;
1442 entry->info.tag = td->tag;
1443 entry->info.type = td->type;
1444 entry->info.count = td->count;
1445 entry->info.offset = 0;
1447 entry->length = length;
1449 if (h->indexUsed > 0 && td->tag < h->index[h->indexUsed-1].info.tag)
1450 h->flags &= ~HEADERFLAG_SORTED;
1456 static int intAppendEntry(Header h, rpmtd td)
1461 if (td->type == RPM_STRING_TYPE || td->type == RPM_I18NSTRING_TYPE) {
1462 /* we can't do this */
1466 /* Find the tag entry in the header. */
1467 entry = findEntry(h, td->tag, td->type);
1471 length = dataLength(td->type, td->data, td->count, 0, NULL);
1475 if (ENTRY_IN_REGION(entry)) {
1476 char * t = xmalloc(entry->length + length);
1477 memcpy(t, entry->data, entry->length);
1479 entry->info.offset = 0;
1481 entry->data = xrealloc(entry->data, entry->length + length);
1483 copyData(td->type, ((char *) entry->data) + entry->length,
1484 td->data, td->count, length);
1486 entry->length += length;
1488 entry->info.count += td->count;
1493 int headerPut(Header h, rpmtd td, headerPutFlags flags)
1498 if (flags & HEADERPUT_APPEND) {
1499 rc = findEntry(h, td->tag, td->type) ?
1500 intAppendEntry(h, td) :
1503 rc = intAddEntry(h, td);
1508 int headerAddI18NString(Header h, rpmTagVal tag, const char * string,
1511 indexEntry table, entry;
1512 const char ** strArray;
1515 rpm_count_t i, langNum;
1518 table = findEntry(h, RPMTAG_HEADERI18NTABLE, RPM_STRING_ARRAY_TYPE);
1519 entry = findEntry(h, tag, RPM_I18NSTRING_TYPE);
1521 if (!table && entry)
1522 return 0; /* this shouldn't ever happen!! */
1524 if (!table && !entry) {
1525 const char * charArray[2];
1526 rpm_count_t count = 0;
1528 if (!lang || (lang[0] == 'C' && lang[1] == '\0')) {
1529 charArray[count++] = "C";
1531 charArray[count++] = "C";
1532 charArray[count++] = lang;
1536 td.tag = RPMTAG_HEADERI18NTABLE;
1537 td.type = RPM_STRING_ARRAY_TYPE;
1538 td.data = (void *) charArray;
1540 if (!headerPut(h, &td, HEADERPUT_DEFAULT))
1542 table = findEntry(h, RPMTAG_HEADERI18NTABLE, RPM_STRING_ARRAY_TYPE);
1547 if (!lang) lang = "C";
1549 { const char * l = table->data;
1550 for (langNum = 0; langNum < table->info.count; langNum++) {
1551 if (rstreq(l, lang)) break;
1556 if (langNum >= table->info.count) {
1557 length = strlen(lang) + 1;
1558 if (ENTRY_IN_REGION(table)) {
1559 char * t = xmalloc(table->length + length);
1560 memcpy(t, table->data, table->length);
1562 table->info.offset = 0;
1564 table->data = xrealloc(table->data, table->length + length);
1565 memmove(((char *)table->data) + table->length, lang, length);
1566 table->length += length;
1567 table->info.count++;
1573 strArray = xmalloc(sizeof(*strArray) * (langNum + 1));
1574 for (i = 0; i < langNum; i++)
1576 strArray[langNum] = string;
1580 td.type = RPM_I18NSTRING_TYPE;
1582 td.count = langNum + 1;
1583 rc = headerPut(h, &td, HEADERPUT_DEFAULT);
1586 } else if (langNum >= entry->info.count) {
1587 ghosts = langNum - entry->info.count;
1589 length = strlen(string) + 1 + ghosts;
1590 if (ENTRY_IN_REGION(entry)) {
1591 char * t = xmalloc(entry->length + length);
1592 memcpy(t, entry->data, entry->length);
1594 entry->info.offset = 0;
1596 entry->data = xrealloc(entry->data, entry->length + length);
1598 memset(((char *)entry->data) + entry->length, '\0', ghosts);
1599 memmove(((char *)entry->data) + entry->length + ghosts, string, strlen(string)+1);
1601 entry->length += length;
1602 entry->info.count = langNum + 1;
1604 char *b, *be, *e, *ee, *t;
1607 /* Set beginning/end pointers to previous data */
1608 b = be = e = ee = entry->data;
1609 for (i = 0; i < table->info.count; i++) {
1612 ee += strlen(ee) + 1;
1617 /* Get storage for new buffer */
1619 sn = strlen(string) + 1;
1621 length = bn + sn + en;
1622 t = buf = xmalloc(length);
1624 /* Copy values into new storage */
1627 memcpy(t, string, sn);
1632 /* Replace i18N string array */
1633 entry->length -= strlen(be) + 1;
1634 entry->length += sn;
1636 if (ENTRY_IN_REGION(entry)) {
1637 entry->info.offset = 0;
1639 entry->data = _free(entry->data);
1646 int headerMod(Header h, rpmtd td)
1653 /* First find the tag */
1654 entry = findEntry(h, td->tag, td->type);
1659 data = grabData(td->type, td->data, td->count, &length);
1660 if (data == NULL || length <= 0)
1663 /* make sure entry points to the first occurence of this tag */
1664 while (entry > h->index && (entry - 1)->info.tag == td->tag)
1667 /* free after we've grabbed the new data in case the two are intertwined;
1668 that's a bad idea but at least we won't break */
1669 oldData = entry->data;
1671 entry->info.count = td->count;
1672 entry->info.type = td->type;
1674 entry->length = length;
1676 if (ENTRY_IN_REGION(entry)) {
1677 entry->info.offset = 0;
1679 oldData = _free(oldData);
1685 * Header tag iterator data structure.
1687 struct headerIterator_s {
1688 Header h; /*!< Header being iterated. */
1689 int next_index; /*!< Next tag index. */
1692 HeaderIterator headerFreeIterator(HeaderIterator hi)
1695 hi->h = headerFree(hi->h);
1701 HeaderIterator headerInitIterator(Header h)
1703 HeaderIterator hi = xmalloc(sizeof(*hi));
1707 hi->h = headerLink(h);
1712 static indexEntry nextIndex(HeaderIterator hi)
1716 indexEntry entry = NULL;
1718 for (slot = hi->next_index; slot < h->indexUsed; slot++) {
1719 entry = h->index + slot;
1720 if (!ENTRY_IS_REGION(entry))
1723 hi->next_index = slot;
1724 if (entry == NULL || slot >= h->indexUsed)
1731 rpmTagVal headerNextTag(HeaderIterator hi)
1733 indexEntry entry = nextIndex(hi);
1734 return entry ? entry->info.tag : RPMTAG_NOT_FOUND;
1737 int headerNext(HeaderIterator hi, rpmtd td)
1739 indexEntry entry = nextIndex(hi);
1744 td->tag = entry->info.tag;
1745 rc = copyTdEntry(entry, td, HEADERGET_DEFAULT);
1747 return ((rc == 1) ? 1 : 0);
1750 unsigned int headerGetInstance(Header h)
1752 return h ? h->instance : 0;
1755 void headerSetInstance(Header h, unsigned int instance)
1757 h->instance = instance;