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 qsort(h->index, h->indexUsed, sizeof(*h->index), offsetCmp);
253 unsigned headerSizeof(Header h, int magicp)
256 unsigned int size = 0;
265 case HEADER_MAGIC_YES:
266 size += sizeof(rpm_header_magic);
268 case HEADER_MAGIC_NO:
272 size += 2 * sizeof(int32_t); /* count of index entries */
274 for (i = 0, entry = h->index; i < h->indexUsed; i++, entry++) {
277 /* Regions go in as is ... */
278 if (ENTRY_IS_REGION(entry)) {
279 size += entry->length;
280 /* XXX Legacy regions do not include the region tag and data. */
281 if (i == 0 && (h->flags & HEADERFLAG_LEGACY))
282 size += sizeof(struct entryInfo_s) + entry->info.count;
286 /* ... and region elements are skipped. */
287 if (entry->info.offset < 0)
291 type = entry->info.type;
292 if (typeSizes[type] > 1) {
293 unsigned diff = typeSizes[type] - (size % typeSizes[type]);
294 if (diff != typeSizes[type]) {
299 size += sizeof(struct entryInfo_s) + entry->length;
306 * Return length of entry data.
307 * @param type entry data type
308 * @param p entry data
309 * @param count entry item count
310 * @param onDisk data is concatenated strings (with NUL's))?
311 * @param pend pointer to end of data (or NULL)
312 * @return no. bytes in data, -1 on failure
314 static int dataLength(rpm_tagtype_t type, rpm_constdata_t p, rpm_count_t count,
315 int onDisk, rpm_constdata_t pend)
317 const unsigned char * s = p;
318 const unsigned char * se = pend;
322 case RPM_STRING_TYPE:
330 length++; /* count nul terminator too. */
333 case RPM_STRING_ARRAY_TYPE:
334 case RPM_I18NSTRING_TYPE:
335 /* These are like RPM_STRING_TYPE, except they're *always* an array */
336 /* Compute sum of length of all strings, including nul terminators */
340 length++; /* count nul terminator too */
348 const char ** av = (const char **)p;
350 /* add one for null termination */
351 length += strlen(*av++) + 1;
357 if (typeSizes[type] == -1)
359 length = typeSizes[(type & 0xf)] * count;
360 if (length < 0 || (se && (s + length) > se))
369 * Swap int32_t and int16_t arrays within header region.
371 * If a header region tag is in the set to be swabbed, as the data for a
372 * a header region is located after all other tag data.
374 * @param entry header entry
375 * @param il no. of entries
376 * @param dl start no. bytes of data
377 * @param pe header physical entry pointer (swapped)
378 * @param dataStart header data start
379 * @param dataEnd header data end
380 * @param regionid region offset
381 * @return no. bytes of data in region, -1 on error
383 static int regionSwab(indexEntry entry, int il, int dl,
385 unsigned char * dataStart,
386 const unsigned char * dataEnd,
389 for (; il > 0; il--, pe++) {
390 struct indexEntry_s ie;
393 ie.info.tag = ntohl(pe->tag);
394 ie.info.type = ntohl(pe->type);
395 ie.info.count = ntohl(pe->count);
396 ie.info.offset = ntohl(pe->offset);
398 if (hdrchkType(ie.info.type))
400 if (hdrchkData(ie.info.count))
402 if (hdrchkData(ie.info.offset))
404 if (hdrchkAlign(ie.info.type, ie.info.offset))
407 ie.data = dataStart + ie.info.offset;
408 if (dataEnd && (unsigned char *)ie.data >= dataEnd)
411 ie.length = dataLength(ie.info.type, ie.data, ie.info.count, 1, dataEnd);
412 if (ie.length < 0 || hdrchkData(ie.length))
418 ie.info.offset = regionid;
419 *entry = ie; /* structure assignment */
425 if (typeSizes[type] > 1) {
426 unsigned diff = typeSizes[type] - (dl % typeSizes[type]);
427 if (diff != typeSizes[type]) {
432 /* Perform endian conversions */
433 switch (ntohl(pe->type)) {
435 { uint64_t * it = ie.data;
436 for (; ie.info.count > 0; ie.info.count--, it += 1) {
437 if (dataEnd && ((unsigned char *)it) >= dataEnd)
443 { int32_t * it = ie.data;
444 for (; ie.info.count > 0; ie.info.count--, it += 1) {
445 if (dataEnd && ((unsigned char *)it) >= dataEnd)
451 { int16_t * it = ie.data;
452 for (; ie.info.count > 0; ie.info.count--, it += 1) {
453 if (dataEnd && ((unsigned char *)it) >= dataEnd)
469 * @retval *lengthPtr no. bytes in unloaded header blob
470 * @return unloaded header blob (NULL on error)
472 static void * doHeaderUnload(Header h,
485 int drlen, ndribbles;
487 if (h == NULL) return NULL;
489 /* Sort entries by (offset,tag). */
492 /* Compute (il,dl) for all tags, including those deleted in region. */
493 drlen = ndribbles = 0;
494 for (i = 0, entry = h->index; i < h->indexUsed; i++, entry++) {
495 if (ENTRY_IS_REGION(entry)) {
496 int32_t rdl = -entry->info.offset; /* negative offset */
497 int32_t ril = rdl/sizeof(*pe);
498 int rid = entry->info.offset;
501 dl += entry->rdlen + entry->info.count;
502 /* XXX Legacy regions do not include the region tag and data. */
503 if (i == 0 && (h->flags & HEADERFLAG_LEGACY))
506 /* Skip rest of entries in region, but account for dribbles. */
507 for (; i < h->indexUsed && entry->info.offset <= rid+1; i++, entry++) {
508 if (entry->info.offset <= rid)
512 type = entry->info.type;
513 if (typeSizes[type] > 1) {
514 unsigned diff = typeSizes[type] - (dl % typeSizes[type]);
515 if (diff != typeSizes[type]) {
523 drlen += entry->length;
531 /* Ignore deleted drips. */
532 if (entry->data == NULL || entry->length <= 0)
536 type = entry->info.type;
537 if (typeSizes[type] > 1) {
538 unsigned diff = typeSizes[type] - (dl % typeSizes[type]);
539 if (diff != typeSizes[type]) {
548 /* Sanity checks on header intro. */
549 if (hdrchkTags(il) || hdrchkData(dl))
552 len = sizeof(il) + sizeof(dl) + (il * sizeof(*pe)) + dl;
558 pe = (entryInfo) &ei[2];
559 dataStart = te = (char *) (pe + il);
561 for (i = 0, entry = h->index; i < h->indexUsed; i++, entry++) {
567 if (entry->data == NULL || entry->length <= 0)
570 t = (unsigned char*)te;
571 pe->tag = htonl(entry->info.tag);
572 pe->type = htonl(entry->info.type);
573 pe->count = htonl(entry->info.count);
575 if (ENTRY_IS_REGION(entry)) {
576 int32_t rdl = -entry->info.offset; /* negative offset */
577 int32_t ril = rdl/sizeof(*pe) + ndribbles;
578 int rid = entry->info.offset;
580 src = (char *)entry->data;
581 rdlen = entry->rdlen;
583 /* XXX Legacy regions do not include the region tag and data. */
584 if (i == 0 && (h->flags & HEADERFLAG_LEGACY)) {
587 memcpy(pe+1, src, rdl);
588 memcpy(te, src + rdl, rdlen);
591 pe->offset = htonl(te - dataStart);
594 stei[2] = htonl(-rdl-entry->info.count);
596 memcpy(te, stei, entry->info.count);
597 te += entry->info.count;
599 rdlen += entry->info.count;
601 count = regionSwab(NULL, ril, 0, pe, t, NULL, 0);
607 memcpy(pe+1, src + sizeof(*pe), ((ril-1) * sizeof(*pe)));
608 memcpy(te, src + (ril * sizeof(*pe)), rdlen+entry->info.count+drlen);
611 entryInfo se = (entryInfo)src;
612 int off = ntohl(se->offset);
613 pe->offset = (off) ? htonl(te - dataStart) : htonl(off);
615 te += entry->info.count + drlen;
617 count = regionSwab(NULL, ril, 0, pe, t, NULL, 0);
618 if (count != (rdlen + entry->info.count + drlen))
622 /* Skip rest of entries in region. */
623 while (i < h->indexUsed && entry->info.offset <= rid+1) {
633 /* Ignore deleted drips. */
634 if (entry->data == NULL || entry->length <= 0)
638 type = entry->info.type;
639 if (typeSizes[type] > 1) {
641 diff = typeSizes[type] - ((te - dataStart) % typeSizes[type]);
642 if (diff != typeSizes[type]) {
648 pe->offset = htonl(te - dataStart);
650 /* copy data w/ endian conversions */
651 switch (entry->info.type) {
653 count = entry->info.count;
656 *((uint64_t *)te) = htonll(*((uint64_t *)src));
657 te += sizeof(uint64_t);
658 src += sizeof(uint64_t);
663 count = entry->info.count;
666 *((int32_t *)te) = htonl(*((int32_t *)src));
667 te += sizeof(int32_t);
668 src += sizeof(int32_t);
673 count = entry->info.count;
676 *((int16_t *)te) = htons(*((int16_t *)src));
677 te += sizeof(int16_t);
678 src += sizeof(int16_t);
683 memcpy(te, entry->data, entry->length);
690 /* Insure that there are no memcpy underruns/overruns. */
691 if (((char *)pe) != dataStart)
693 if ((((char *)ei)+len) != te)
699 h->flags &= ~HEADERFLAG_SORTED;
709 void * headerUnload(Header h)
712 void * uh = doHeaderUnload(h, &length);
717 * Find matching (tag,type) entry in header.
719 * @param tag entry tag
720 * @param type entry type
721 * @return header entry
724 indexEntry findEntry(Header h, rpmTagVal tag, rpm_tagtype_t type)
727 struct indexEntry_s key;
729 if (h == NULL) return NULL;
730 if (!(h->flags & HEADERFLAG_SORTED)) headerSort(h);
734 entry = bsearch(&key, h->index, h->indexUsed, sizeof(*h->index), indexCmp);
738 if (type == RPM_NULL_TYPE)
742 while (entry->info.tag == tag && entry->info.type != type &&
743 entry > h->index) entry--;
745 if (entry->info.tag == tag && entry->info.type == type)
751 int headerDel(Header h, rpmTagVal tag)
753 indexEntry last = h->index + h->indexUsed;
754 indexEntry entry, first;
757 entry = findEntry(h, tag, RPM_NULL_TYPE);
758 if (!entry) return 1;
760 /* Make sure entry points to the first occurence of this tag. */
761 while (entry > h->index && (entry - 1)->info.tag == tag)
764 /* Free data for tags being removed. */
765 for (first = entry; first < last; first++) {
767 if (first->info.tag != tag)
772 if (ENTRY_IN_REGION(first))
777 ne = (first - entry);
782 memmove(entry, first, (ne * sizeof(*entry)));
788 Header headerLoad(void * uh)
790 int32_t * ei = (int32_t *) uh;
791 int32_t il = ntohl(ei[0]); /* index length */
792 int32_t dl = ntohl(ei[1]); /* data length */
793 size_t pvlen = sizeof(il) + sizeof(dl) +
794 (il * sizeof(struct entryInfo_s)) + dl;
798 unsigned char * dataStart;
799 unsigned char * dataEnd;
803 /* Sanity checks on header intro. */
804 if (hdrchkTags(il) || hdrchkData(dl))
808 pe = (entryInfo) &ei[2];
809 dataStart = (unsigned char *) (pe + il);
810 dataEnd = dataStart + dl;
812 h = headerCreate(uh, il);
815 if (!(htonl(pe->tag) < RPMTAG_HEADERI18NTABLE)) {
816 h->flags |= HEADERFLAG_LEGACY;
817 entry->info.type = REGION_TAG_TYPE;
818 entry->info.tag = RPMTAG_HEADERIMAGE;
819 entry->info.count = REGION_TAG_COUNT;
820 entry->info.offset = ((unsigned char *)pe - dataStart); /* negative offset */
823 entry->length = pvlen - sizeof(il) - sizeof(dl);
824 rdlen = regionSwab(entry+1, il, 0, pe, dataStart, dataEnd, entry->info.offset);
827 entry->rdlen = rdlen;
833 h->flags &= ~HEADERFLAG_LEGACY;
835 entry->info.type = htonl(pe->type);
836 entry->info.count = htonl(pe->count);
838 if (hdrchkType(entry->info.type))
840 if (hdrchkTags(entry->info.count))
843 { int off = ntohl(pe->offset);
848 size_t nb = REGION_TAG_COUNT;
850 /* XXX Hmm, why the copy? */
851 memcpy(&stei, dataStart + off, nb);
852 rdl = -ntohl(stei[2]); /* negative offset */
853 ril = rdl/sizeof(*pe);
854 if (hdrchkTags(ril) || hdrchkData(rdl))
856 entry->info.tag = htonl(pe->tag);
859 rdl = (ril * sizeof(struct entryInfo_s));
860 entry->info.tag = RPMTAG_HEADERIMAGE;
863 entry->info.offset = -rdl; /* negative offset */
866 entry->length = pvlen - sizeof(il) - sizeof(dl);
867 rdlen = regionSwab(entry+1, ril-1, 0, pe+1, dataStart, dataEnd, entry->info.offset);
870 entry->rdlen = rdlen;
872 if (ril < h->indexUsed) {
873 indexEntry newEntry = entry + ril;
874 int ne = (h->indexUsed - ril);
875 int rid = entry->info.offset+1;
878 /* Load dribble entries from region. */
879 rc = regionSwab(newEntry, ne, 0, pe+ril, dataStart, dataEnd, rid);
884 { indexEntry firstEntry = newEntry;
885 int save = h->indexUsed;
888 /* Dribble entries replace duplicate region entries. */
890 for (j = 0; j < ne; j++, newEntry++) {
891 (void) headerDel(h, newEntry->info.tag);
892 if (newEntry->info.tag == RPMTAG_BASENAMES)
893 (void) headerDel(h, RPMTAG_OLDFILENAMES);
896 /* If any duplicate entries were replaced, move new entries down. */
897 if (h->indexUsed < (save - ne)) {
898 memmove(h->index + h->indexUsed, firstEntry,
899 (ne * sizeof(*entry)));
906 h->flags &= ~HEADERFLAG_SORTED;
908 h->flags |= HEADERFLAG_ALLOCATED;
914 h->index = _free(h->index);
920 Header headerReload(Header h, rpmTagVal tag)
924 void * uh = doHeaderUnload(h, &length);
934 if (ENTRY_IS_REGION(nh->index)) {
935 if (tag == RPMTAG_HEADERSIGNATURES || tag == RPMTAG_HEADERIMMUTABLE)
936 nh->index[0].info.tag = tag;
941 Header headerCopyLoad(const void * uh)
943 int32_t * ei = (int32_t *) uh;
944 int32_t il = ntohl(ei[0]); /* index length */
945 int32_t dl = ntohl(ei[1]); /* data length */
946 size_t pvlen = sizeof(il) + sizeof(dl) +
947 (il * sizeof(struct entryInfo_s)) + dl;
951 /* Sanity checks on header intro. */
952 if (!(hdrchkTags(il) || hdrchkData(dl)) && pvlen < headerMaxbytes) {
953 nuh = memcpy(xmalloc(pvlen), uh, pvlen);
954 if ((h = headerLoad(nuh)) == NULL)
961 * Read (and load) header from file handle.
962 * @param fd file handle
963 * @param magicp read (and verify) 8 bytes of (magic, 0)?
964 * @return header (or NULL on error)
966 Header headerRead(FD_t fd, int magicp)
975 if (magicp == HEADER_MAGIC_YES) {
978 if (Fread(block, 1, 4*sizeof(*block), fd) != 4*sizeof(*block))
983 if (memcmp(&magic, rpm_header_magic, sizeof(magic)))
986 il = ntohl(block[2]);
987 dl = ntohl(block[3]);
989 if (Fread(block, 1, 2*sizeof(*block), fd) != 2*sizeof(*block))
992 il = ntohl(block[0]);
993 dl = ntohl(block[1]);
996 len = sizeof(il) + sizeof(dl) + (il * sizeof(struct entryInfo_s)) + dl;
998 /* Sanity checks on header intro. */
999 if (hdrchkTags(il) || hdrchkData(dl) || len > headerMaxbytes)
1005 len -= sizeof(il) + sizeof(dl);
1007 if (Fread((char *)&ei[2], 1, len, fd) != len)
1013 if (h == NULL && ei != NULL) {
1019 int headerWrite(FD_t fd, Header h, int magicp)
1025 uh = doHeaderUnload(h, &length);
1029 case HEADER_MAGIC_YES:
1030 nb = Fwrite(rpm_header_magic, sizeof(uint8_t), sizeof(rpm_header_magic), fd);
1031 if (nb != sizeof(rpm_header_magic))
1034 case HEADER_MAGIC_NO:
1038 nb = Fwrite(uh, sizeof(char), length, fd);
1042 return (nb == length ? 0 : 1);
1045 int headerIsEntry(Header h, rpmTagVal tag)
1047 /* FIX: h modified by sort. */
1048 return (findEntry(h, tag, RPM_NULL_TYPE) ? 1 : 0);
1053 * Retrieve data from header entry.
1054 * Relevant flags (others are ignored), if neither is set allocation
1055 * behavior depends on data type(!)
1056 * HEADERGET_MINMEM: return pointers to header memory
1057 * HEADERGET_ALLOC: always return malloced memory, overrides MINMEM
1059 * @todo Permit retrieval of regions other than HEADER_IMUTABLE.
1060 * @param entry header entry
1061 * @param td tag data container
1062 * @param minMem string pointers refer to header memory?
1063 * @param flags flags to control memory allocation
1064 * @return 1 on success, otherwise error.
1066 static int copyTdEntry(const indexEntry entry, rpmtd td, headerGetFlags flags)
1068 rpm_count_t count = entry->info.count;
1069 int rc = 1; /* XXX 1 on success. */
1070 /* ALLOC overrides MINMEM */
1071 int allocMem = flags & HEADERGET_ALLOC;
1072 int minMem = allocMem ? 0 : flags & HEADERGET_MINMEM;
1073 int argvArray = (flags & HEADERGET_ARGV) ? 1 : 0;
1076 td->flags = RPMTD_IMMUTABLE;
1077 switch (entry->info.type) {
1080 * XXX This only works for
1081 * XXX "sealed" HEADER_IMMUTABLE/HEADER_SIGNATURES/HEADER_IMAGE.
1082 * XXX This will *not* work for unsealed legacy HEADER_IMAGE (i.e.
1083 * XXX a legacy header freshly read, but not yet unloaded to the rpmdb).
1085 if (ENTRY_IS_REGION(entry)) {
1086 int32_t * ei = ((int32_t *)entry->data) - 2;
1087 entryInfo pe = (entryInfo) (ei + 2);
1088 unsigned char * dataStart = (unsigned char *) (pe + ntohl(ei[0]));
1089 int32_t rdl = -entry->info.offset; /* negative offset */
1090 int32_t ril = rdl/sizeof(*pe);
1093 count = 2 * sizeof(*ei) + (ril * sizeof(*pe)) + rdl;
1094 if (entry->info.tag == RPMTAG_HEADERIMAGE) {
1098 count += REGION_TAG_COUNT;
1099 rdl += REGION_TAG_COUNT;
1102 td->data = xmalloc(count);
1103 ei = (int32_t *) td->data;
1107 pe = (entryInfo) memcpy(ei + 2, pe, (ril * sizeof(*pe)));
1109 dataStart = (unsigned char *) memcpy(pe + ril, dataStart, rdl);
1111 rc = regionSwab(NULL, ril, 0, pe, dataStart, dataStart + rdl, 0);
1112 /* don't return data on failure */
1114 td->data = _free(td->data);
1116 /* XXX 1 on success. */
1117 rc = (rc < 0) ? 0 : 1;
1119 count = entry->length;
1121 ? memcpy(xmalloc(count), entry->data, count)
1125 case RPM_STRING_TYPE:
1126 /* simple string, but fallthrough if its actually an array */
1127 if (count == 1 && !argvArray) {
1128 td->data = allocMem ? xstrdup(entry->data) : entry->data;
1131 case RPM_STRING_ARRAY_TYPE:
1132 case RPM_I18NSTRING_TYPE:
1133 { const char ** ptrEntry;
1134 int tableSize = (count + argvArray) * sizeof(char *);
1139 td->data = xmalloc(tableSize);
1140 ptrEntry = (const char **) td->data;
1143 t = xmalloc(tableSize + entry->length);
1144 td->data = (void *)t;
1145 ptrEntry = (const char **) td->data;
1147 memcpy(t, entry->data, entry->length);
1149 for (i = 0; i < count; i++) {
1156 td->flags |= RPMTD_ARGV;
1161 case RPM_INT16_TYPE:
1162 case RPM_INT32_TYPE:
1163 case RPM_INT64_TYPE:
1165 td->data = xmalloc(entry->length);
1166 memcpy(td->data, entry->data, entry->length);
1168 td->data = entry->data;
1172 /* WTH? Don't mess with unknown data types... */
1177 td->type = entry->info.type;
1180 if (td->data && entry->data != td->data) {
1181 td->flags |= RPMTD_ALLOCED;
1188 * Does locale match entry in header i18n table?
1191 * The range [l,le) contains the next locale to match:
1192 * ll[_CC][.EEEEE][@dddd]
1194 * ll ISO language code (in lowercase).
1195 * CC (optional) ISO coutnry code (in uppercase).
1196 * EEEEE (optional) encoding (not really standardized).
1197 * dddd (optional) dialect.
1200 * @param td header i18n table data, NUL terminated
1201 * @param l start of locale to match
1202 * @param le end of locale to match
1203 * @return 1 on good match, 2 on weak match, 0 on no match
1205 static int headerMatchLocale(const char *td, const char *l, const char *le)
1209 /* First try a complete match. */
1210 if (strlen(td) == (le-l) && rstreqn(td, l, (le - l)))
1213 /* Next, try stripping optional dialect and matching. */
1214 for (fe = l; fe < le && *fe != '@'; fe++)
1216 if (fe < le && rstreqn(td, l, (fe - l)))
1219 /* Next, try stripping optional codeset and matching. */
1220 for (fe = l; fe < le && *fe != '.'; fe++)
1222 if (fe < le && rstreqn(td, l, (fe - l)))
1225 /* Finally, try stripping optional country code and matching. */
1226 for (fe = l; fe < le && *fe != '_'; fe++)
1228 if (fe < le && rstreqn(td, l, (fe - l)))
1235 * Return i18n string from header that matches locale.
1237 * @param entry i18n string data
1238 * @retval td tag data container
1239 * @param flags flags to control allocation
1242 static int copyI18NEntry(Header h, indexEntry entry, rpmtd td,
1243 headerGetFlags flags)
1245 const char *lang, *l, *le;
1248 td->type = RPM_STRING_TYPE;
1250 /* if no match, just return the first string */
1251 td->data = entry->data;
1253 /* XXX Drepper sez' this is the order. */
1254 if ((lang = getenv("LANGUAGE")) == NULL &&
1255 (lang = getenv("LC_ALL")) == NULL &&
1256 (lang = getenv("LC_MESSAGES")) == NULL &&
1257 (lang = getenv("LANG")) == NULL)
1260 if ((table = findEntry(h, RPMTAG_HEADERI18NTABLE, RPM_STRING_ARRAY_TYPE)) == NULL)
1263 for (l = lang; *l != '\0'; l = le) {
1265 char *ed, *ed_weak = NULL;
1268 while (*l && *l == ':') /* skip leading colons */
1272 for (le = l; *le && *le != ':'; le++) /* find end of this locale */
1275 /* For each entry in the header ... */
1276 for (langNum = 0, t = table->data, ed = entry->data;
1277 langNum < entry->info.count;
1278 langNum++, t += strlen(t) + 1, ed += strlen(ed) + 1) {
1280 int match = headerMatchLocale(t, l, le);
1284 } else if (match == 2) {
1295 if (flags & HEADERGET_ALLOC) {
1296 td->data = xstrdup(td->data);
1297 td->flags |= RPMTD_ALLOCED;
1304 * Retrieve tag data from header.
1306 * @retval td tag data container
1307 * @param flags flags to control retrieval
1308 * @return 1 on success, 0 on not found
1310 static int intGetTdEntry(Header h, rpmtd td, headerGetFlags flags)
1315 /* First find the tag */
1316 /* FIX: h modified by sort. */
1317 entry = findEntry(h, td->tag, RPM_NULL_TYPE);
1318 if (entry == NULL) {
1319 /* Td is zeroed above, just return... */
1323 if (flags & HEADERGET_RAW) {
1324 rc = copyTdEntry(entry, td, flags);
1326 switch (entry->info.type) {
1327 case RPM_I18NSTRING_TYPE:
1328 rc = copyI18NEntry(h, entry, td, flags);
1331 rc = copyTdEntry(entry, td, flags);
1336 /* XXX 1 on success */
1337 return ((rc == 1) ? 1 : 0);
1340 int headerGet(Header h, rpmTagVal tag, rpmtd td, headerGetFlags flags)
1343 headerTagTagFunction tagfunc = intGetTdEntry;
1345 if (td == NULL) return 0;
1350 if (flags & HEADERGET_EXT) {
1351 headerTagTagFunction extfunc = rpmHeaderTagFunc(tag);
1352 if (extfunc) tagfunc = extfunc;
1354 rc = tagfunc(h, td, flags);
1356 assert(tag == td->tag);
1362 static void copyData(rpm_tagtype_t type, rpm_data_t dstPtr,
1363 rpm_constdata_t srcPtr, rpm_count_t cnt, int dataLength)
1366 case RPM_STRING_ARRAY_TYPE:
1367 case RPM_I18NSTRING_TYPE:
1368 { const char ** av = (const char **) srcPtr;
1371 while (cnt-- > 0 && dataLength > 0) {
1373 if ((s = *av++) == NULL)
1377 } while (s[-1] && --dataLength > 0);
1382 memmove(dstPtr, srcPtr, dataLength);
1388 * Return (malloc'ed) copy of entry data.
1389 * @param type entry data type
1390 * @param p entry data
1391 * @param c entry item count
1392 * @retval lengthPtr no. bytes in returned data
1393 * @return (malloc'ed) copy of entry data, NULL on error
1396 grabData(rpm_tagtype_t type, rpm_constdata_t p, rpm_count_t c, int * lengthPtr)
1398 rpm_data_t data = NULL;
1401 length = dataLength(type, p, c, 0, NULL);
1403 data = xmalloc(length);
1404 copyData(type, data, p, c, length);
1408 *lengthPtr = length;
1412 static int intAddEntry(Header h, rpmtd td)
1418 /* Count must always be >= 1 for headerAddEntry. */
1422 if (hdrchkType(td->type))
1424 if (hdrchkData(td->count))
1428 data = grabData(td->type, td->data, td->count, &length);
1429 if (data == NULL || length <= 0)
1432 /* Allocate more index space if necessary */
1433 if (h->indexUsed == h->indexAlloced) {
1434 h->indexAlloced += INDEX_MALLOC_SIZE;
1435 h->index = xrealloc(h->index, h->indexAlloced * sizeof(*h->index));
1438 /* Fill in the index */
1439 entry = h->index + h->indexUsed;
1440 entry->info.tag = td->tag;
1441 entry->info.type = td->type;
1442 entry->info.count = td->count;
1443 entry->info.offset = 0;
1445 entry->length = length;
1447 if (h->indexUsed > 0 && td->tag < h->index[h->indexUsed-1].info.tag)
1448 h->flags &= ~HEADERFLAG_SORTED;
1454 static int intAppendEntry(Header h, rpmtd td)
1459 if (td->type == RPM_STRING_TYPE || td->type == RPM_I18NSTRING_TYPE) {
1460 /* we can't do this */
1464 /* Find the tag entry in the header. */
1465 entry = findEntry(h, td->tag, td->type);
1469 length = dataLength(td->type, td->data, td->count, 0, NULL);
1473 if (ENTRY_IN_REGION(entry)) {
1474 char * t = xmalloc(entry->length + length);
1475 memcpy(t, entry->data, entry->length);
1477 entry->info.offset = 0;
1479 entry->data = xrealloc(entry->data, entry->length + length);
1481 copyData(td->type, ((char *) entry->data) + entry->length,
1482 td->data, td->count, length);
1484 entry->length += length;
1486 entry->info.count += td->count;
1491 int headerPut(Header h, rpmtd td, headerPutFlags flags)
1496 if (flags & HEADERPUT_APPEND) {
1497 rc = findEntry(h, td->tag, td->type) ?
1498 intAppendEntry(h, td) :
1501 rc = intAddEntry(h, td);
1506 int headerAddI18NString(Header h, rpmTagVal tag, const char * string,
1509 indexEntry table, entry;
1510 const char ** strArray;
1513 rpm_count_t i, langNum;
1516 table = findEntry(h, RPMTAG_HEADERI18NTABLE, RPM_STRING_ARRAY_TYPE);
1517 entry = findEntry(h, tag, RPM_I18NSTRING_TYPE);
1519 if (!table && entry)
1520 return 0; /* this shouldn't ever happen!! */
1522 if (!table && !entry) {
1523 const char * charArray[2];
1524 rpm_count_t count = 0;
1526 if (!lang || (lang[0] == 'C' && lang[1] == '\0')) {
1527 charArray[count++] = "C";
1529 charArray[count++] = "C";
1530 charArray[count++] = lang;
1534 td.tag = RPMTAG_HEADERI18NTABLE;
1535 td.type = RPM_STRING_ARRAY_TYPE;
1536 td.data = (void *) charArray;
1538 if (!headerPut(h, &td, HEADERPUT_DEFAULT))
1540 table = findEntry(h, RPMTAG_HEADERI18NTABLE, RPM_STRING_ARRAY_TYPE);
1545 if (!lang) lang = "C";
1547 { const char * l = table->data;
1548 for (langNum = 0; langNum < table->info.count; langNum++) {
1549 if (rstreq(l, lang)) break;
1554 if (langNum >= table->info.count) {
1555 length = strlen(lang) + 1;
1556 if (ENTRY_IN_REGION(table)) {
1557 char * t = xmalloc(table->length + length);
1558 memcpy(t, table->data, table->length);
1560 table->info.offset = 0;
1562 table->data = xrealloc(table->data, table->length + length);
1563 memmove(((char *)table->data) + table->length, lang, length);
1564 table->length += length;
1565 table->info.count++;
1571 strArray = xmalloc(sizeof(*strArray) * (langNum + 1));
1572 for (i = 0; i < langNum; i++)
1574 strArray[langNum] = string;
1578 td.type = RPM_I18NSTRING_TYPE;
1580 td.count = langNum + 1;
1581 rc = headerPut(h, &td, HEADERPUT_DEFAULT);
1584 } else if (langNum >= entry->info.count) {
1585 ghosts = langNum - entry->info.count;
1587 length = strlen(string) + 1 + ghosts;
1588 if (ENTRY_IN_REGION(entry)) {
1589 char * t = xmalloc(entry->length + length);
1590 memcpy(t, entry->data, entry->length);
1592 entry->info.offset = 0;
1594 entry->data = xrealloc(entry->data, entry->length + length);
1596 memset(((char *)entry->data) + entry->length, '\0', ghosts);
1597 memmove(((char *)entry->data) + entry->length + ghosts, string, strlen(string)+1);
1599 entry->length += length;
1600 entry->info.count = langNum + 1;
1602 char *b, *be, *e, *ee, *t;
1605 /* Set beginning/end pointers to previous data */
1606 b = be = e = ee = entry->data;
1607 for (i = 0; i < table->info.count; i++) {
1610 ee += strlen(ee) + 1;
1615 /* Get storage for new buffer */
1617 sn = strlen(string) + 1;
1619 length = bn + sn + en;
1620 t = buf = xmalloc(length);
1622 /* Copy values into new storage */
1625 memcpy(t, string, sn);
1630 /* Replace i18N string array */
1631 entry->length -= strlen(be) + 1;
1632 entry->length += sn;
1634 if (ENTRY_IN_REGION(entry)) {
1635 entry->info.offset = 0;
1637 entry->data = _free(entry->data);
1644 int headerMod(Header h, rpmtd td)
1651 /* First find the tag */
1652 entry = findEntry(h, td->tag, td->type);
1657 data = grabData(td->type, td->data, td->count, &length);
1658 if (data == NULL || length <= 0)
1661 /* make sure entry points to the first occurence of this tag */
1662 while (entry > h->index && (entry - 1)->info.tag == td->tag)
1665 /* free after we've grabbed the new data in case the two are intertwined;
1666 that's a bad idea but at least we won't break */
1667 oldData = entry->data;
1669 entry->info.count = td->count;
1670 entry->info.type = td->type;
1672 entry->length = length;
1674 if (ENTRY_IN_REGION(entry)) {
1675 entry->info.offset = 0;
1677 oldData = _free(oldData);
1683 * Header tag iterator data structure.
1685 struct headerIterator_s {
1686 Header h; /*!< Header being iterated. */
1687 int next_index; /*!< Next tag index. */
1690 HeaderIterator headerFreeIterator(HeaderIterator hi)
1693 hi->h = headerFree(hi->h);
1699 HeaderIterator headerInitIterator(Header h)
1701 HeaderIterator hi = xmalloc(sizeof(*hi));
1705 hi->h = headerLink(h);
1710 static indexEntry nextIndex(HeaderIterator hi)
1714 indexEntry entry = NULL;
1716 for (slot = hi->next_index; slot < h->indexUsed; slot++) {
1717 entry = h->index + slot;
1718 if (!ENTRY_IS_REGION(entry))
1721 hi->next_index = slot;
1722 if (entry == NULL || slot >= h->indexUsed)
1729 rpmTagVal headerNextTag(HeaderIterator hi)
1731 indexEntry entry = nextIndex(hi);
1732 return entry ? entry->info.tag : RPMTAG_NOT_FOUND;
1735 int headerNext(HeaderIterator hi, rpmtd td)
1737 indexEntry entry = nextIndex(hi);
1742 td->tag = entry->info.tag;
1743 rc = copyTdEntry(entry, td, HEADERGET_DEFAULT);
1745 return ((rc == 1) ? 1 : 0);
1748 unsigned int headerGetInstance(Header h)
1750 return h ? h->instance : 0;
1753 void headerSetInstance(Header h, unsigned int instance)
1755 h->instance = instance;