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"
23 const unsigned char rpm_header_magic[8] = {
24 0x8e, 0xad, 0xe8, 0x01, 0x00, 0x00, 0x00, 0x00
28 * Alignment needed for header data types.
30 static const int typeAlign[16] = {
31 1, /*!< RPM_NULL_TYPE */
32 1, /*!< RPM_CHAR_TYPE */
33 1, /*!< RPM_INT8_TYPE */
34 2, /*!< RPM_INT16_TYPE */
35 4, /*!< RPM_INT32_TYPE */
36 8, /*!< RPM_INT64_TYPE */
37 1, /*!< RPM_STRING_TYPE */
38 1, /*!< RPM_BIN_TYPE */
39 1, /*!< RPM_STRING_ARRAY_TYPE */
40 1, /*!< RPM_I18NSTRING_TYPE */
50 * Size of header data types.
52 static const int typeSizes[16] = {
53 0, /*!< RPM_NULL_TYPE */
54 1, /*!< RPM_CHAR_TYPE */
55 1, /*!< RPM_INT8_TYPE */
56 2, /*!< RPM_INT16_TYPE */
57 4, /*!< RPM_INT32_TYPE */
58 8, /*!< RPM_INT64_TYPE */
59 -1, /*!< RPM_STRING_TYPE */
60 1, /*!< RPM_BIN_TYPE */
61 -1, /*!< RPM_STRING_ARRAY_TYPE */
62 -1, /*!< RPM_I18NSTRING_TYPE */
72 * Maximum no. of bytes permitted in a header.
74 static const size_t headerMaxbytes = (32*1024*1024);
77 * HEADER_EXT_TAG format function prototype.
78 * This is allowed to fail, which indicates the tag doesn't exist.
81 * @retval td tag data container
82 * @param flags modifier flags
83 * @return 0 on success
85 typedef int (*headerTagTagFunction) (Header h, rpmtd td, headerGetFlags hgflags);
87 extern void *rpmHeaderTagFunc(rpmTag tag);
89 Header headerLink(Header h)
91 if (h == NULL) return NULL;
95 fprintf(stderr, "--> h %p ++ %d at %s:%u\n", h, h->nrefs, __FILE__, __LINE__);
100 Header headerUnlink(Header h)
102 if (h == NULL) return NULL;
104 fprintf(stderr, "--> h %p -- %d at %s:%u\n", h, h->nrefs, __FILE__, __LINE__);
109 Header headerFree(Header h)
111 (void) headerUnlink(h);
113 if (h == NULL || h->nrefs > 0)
114 return NULL; /* XXX return previous header? */
117 indexEntry entry = h->index;
119 for (i = 0; i < h->indexUsed; i++, entry++) {
120 if ((h->flags & HEADERFLAG_ALLOCATED) && ENTRY_IS_REGION(entry)) {
121 if (entry->length > 0) {
122 int32_t * ei = entry->data;
123 if ((ei - 2) == h->blob) h->blob = _free(h->blob);
126 } else if (!ENTRY_IN_REGION(entry)) {
127 entry->data = _free(entry->data);
131 h->index = _free(h->index);
138 Header headerNew(void)
140 Header h = xcalloc(1, sizeof(*h));
143 h->indexAlloced = INDEX_MALLOC_SIZE;
146 h->flags |= HEADERFLAG_SORTED;
148 h->index = (h->indexAlloced
149 ? xcalloc(h->indexAlloced, sizeof(*h->index))
153 return headerLink(h);
156 int headerVerifyInfo(int il, int dl, const void * pev, void * iv, int negate)
158 entryInfo pe = (entryInfo) pev;
162 for (i = 0; i < il; i++) {
163 info->tag = ntohl(pe[i].tag);
164 info->type = ntohl(pe[i].type);
165 info->offset = ntohl(pe[i].offset);
167 info->offset = -info->offset;
168 info->count = ntohl(pe[i].count);
170 if (hdrchkType(info->type))
172 if (hdrchkAlign(info->type, info->offset))
174 if (!negate && hdrchkRange(dl, info->offset))
176 if (hdrchkData(info->count))
185 static int indexCmp(const void * avp, const void * bvp)
187 indexEntry ap = (indexEntry) avp, bp = (indexEntry) bvp;
188 return (ap->info.tag - bp->info.tag);
191 void headerSort(Header h)
193 if (!(h->flags & HEADERFLAG_SORTED)) {
194 qsort(h->index, h->indexUsed, sizeof(*h->index), indexCmp);
195 h->flags |= HEADERFLAG_SORTED;
201 static int offsetCmp(const void * avp, const void * bvp)
203 indexEntry ap = (indexEntry) avp, bp = (indexEntry) bvp;
204 int rc = (ap->info.offset - bp->info.offset);
207 /* Within a region, entries sort by address. Added drips sort by tag. */
208 if (ap->info.offset < 0)
209 rc = (((char *)ap->data) - ((char *)bp->data));
211 rc = (ap->info.tag - bp->info.tag);
217 * Restore tags in header to original ordering.
220 void headerUnsort(Header h)
222 qsort(h->index, h->indexUsed, sizeof(*h->index), offsetCmp);
225 unsigned headerSizeof(Header h, enum hMagic magicp)
228 unsigned int size = 0;
229 unsigned int pad = 0;
238 case HEADER_MAGIC_YES:
239 size += sizeof(rpm_header_magic);
241 case HEADER_MAGIC_NO:
245 size += 2 * sizeof(int32_t); /* count of index entries */
247 for (i = 0, entry = h->index; i < h->indexUsed; i++, entry++) {
250 /* Regions go in as is ... */
251 if (ENTRY_IS_REGION(entry)) {
252 size += entry->length;
253 /* XXX Legacy regions do not include the region tag and data. */
254 if (i == 0 && (h->flags & HEADERFLAG_LEGACY))
255 size += sizeof(struct entryInfo_s) + entry->info.count;
259 /* ... and region elements are skipped. */
260 if (entry->info.offset < 0)
264 type = entry->info.type;
265 if (typeSizes[type] > 1) {
266 unsigned diff = typeSizes[type] - (size % typeSizes[type]);
267 if (diff != typeSizes[type]) {
273 size += sizeof(struct entryInfo_s) + entry->length;
280 * Return length of entry data.
281 * @param type entry data type
282 * @param p entry data
283 * @param count entry item count
284 * @param onDisk data is concatenated strings (with NUL's))?
285 * @param pend pointer to end of data (or NULL)
286 * @return no. bytes in data, -1 on failure
288 static int dataLength(rpmTagType type, rpm_constdata_t p, rpm_count_t count,
289 int onDisk, rpm_constdata_t pend)
291 const unsigned char * s = p;
292 const unsigned char * se = pend;
296 case RPM_STRING_TYPE:
304 length++; /* count nul terminator too. */
307 case RPM_STRING_ARRAY_TYPE:
308 case RPM_I18NSTRING_TYPE:
309 /* These are like RPM_STRING_TYPE, except they're *always* an array */
310 /* Compute sum of length of all strings, including nul terminators */
314 length++; /* count nul terminator too */
322 const char ** av = (const char **)p;
324 /* add one for null termination */
325 length += strlen(*av++) + 1;
331 if (typeSizes[type] == -1)
333 length = typeSizes[(type & 0xf)] * count;
334 if (length < 0 || (se && (s + length) > se))
343 * Swap int32_t and int16_t arrays within header region.
345 * If a header region tag is in the set to be swabbed, as the data for a
346 * a header region is located after all other tag data.
348 * @param entry header entry
349 * @param il no. of entries
350 * @param dl start no. bytes of data
351 * @param pe header physical entry pointer (swapped)
352 * @param dataStart header data start
353 * @param dataEnd header data end
354 * @param regionid region offset
355 * @return no. bytes of data in region, -1 on error
357 static int regionSwab(indexEntry entry, int il, int dl,
359 unsigned char * dataStart,
360 const unsigned char * dataEnd,
363 for (; il > 0; il--, pe++) {
364 struct indexEntry_s ie;
367 ie.info.tag = ntohl(pe->tag);
368 ie.info.type = ntohl(pe->type);
369 ie.info.count = ntohl(pe->count);
370 ie.info.offset = ntohl(pe->offset);
372 if (hdrchkType(ie.info.type))
374 if (hdrchkData(ie.info.count))
376 if (hdrchkData(ie.info.offset))
378 if (hdrchkAlign(ie.info.type, ie.info.offset))
381 ie.data = dataStart + ie.info.offset;
382 if (dataEnd && (unsigned char *)ie.data >= dataEnd)
385 ie.length = dataLength(ie.info.type, ie.data, ie.info.count, 1, dataEnd);
386 if (ie.length < 0 || hdrchkData(ie.length))
392 ie.info.offset = regionid;
393 *entry = ie; /* structure assignment */
399 if (typeSizes[type] > 1) {
400 unsigned diff = typeSizes[type] - (dl % typeSizes[type]);
401 if (diff != typeSizes[type]) {
406 /* Perform endian conversions */
407 switch (ntohl(pe->type)) {
409 { uint64_t * it = ie.data;
410 for (; ie.info.count > 0; ie.info.count--, it += 1) {
411 if (dataEnd && ((unsigned char *)it) >= dataEnd)
417 { int32_t * it = ie.data;
418 for (; ie.info.count > 0; ie.info.count--, it += 1) {
419 if (dataEnd && ((unsigned char *)it) >= dataEnd)
425 { int16_t * it = ie.data;
426 for (; ie.info.count > 0; ie.info.count--, it += 1) {
427 if (dataEnd && ((unsigned char *)it) >= dataEnd)
443 * @retval *lengthPtr no. bytes in unloaded header blob
444 * @return unloaded header blob (NULL on error)
446 static void * doHeaderUnload(Header h,
460 int drlen, ndribbles;
463 /* Sort entries by (offset,tag). */
466 /* Compute (il,dl) for all tags, including those deleted in region. */
468 drlen = ndribbles = driplen = ndrips = 0;
469 for (i = 0, entry = h->index; i < h->indexUsed; i++, entry++) {
470 if (ENTRY_IS_REGION(entry)) {
471 int32_t rdl = -entry->info.offset; /* negative offset */
472 int32_t ril = rdl/sizeof(*pe);
473 int rid = entry->info.offset;
476 dl += entry->rdlen + entry->info.count;
477 /* XXX Legacy regions do not include the region tag and data. */
478 if (i == 0 && (h->flags & HEADERFLAG_LEGACY))
481 /* Skip rest of entries in region, but account for dribbles. */
482 for (; i < h->indexUsed && entry->info.offset <= rid+1; i++, entry++) {
483 if (entry->info.offset <= rid)
487 type = entry->info.type;
488 if (typeSizes[type] > 1) {
489 unsigned diff = typeSizes[type] - (dl % typeSizes[type]);
490 if (diff != typeSizes[type]) {
499 drlen += entry->length;
507 /* Ignore deleted drips. */
508 if (entry->data == NULL || entry->length <= 0)
512 type = entry->info.type;
513 if (typeSizes[type] > 1) {
514 unsigned diff = typeSizes[type] - (dl % typeSizes[type]);
515 if (diff != typeSizes[type]) {
524 driplen += entry->length;
528 /* Sanity checks on header intro. */
529 if (hdrchkTags(il) || hdrchkData(dl))
532 len = sizeof(il) + sizeof(dl) + (il * sizeof(*pe)) + dl;
538 pe = (entryInfo) &ei[2];
539 dataStart = te = (char *) (pe + il);
542 for (i = 0, entry = h->index; i < h->indexUsed; i++, entry++) {
548 if (entry->data == NULL || entry->length <= 0)
551 t = (unsigned char*)te;
552 pe->tag = htonl(entry->info.tag);
553 pe->type = htonl(entry->info.type);
554 pe->count = htonl(entry->info.count);
556 if (ENTRY_IS_REGION(entry)) {
557 int32_t rdl = -entry->info.offset; /* negative offset */
558 int32_t ril = rdl/sizeof(*pe) + ndribbles;
559 int rid = entry->info.offset;
561 src = (char *)entry->data;
562 rdlen = entry->rdlen;
564 /* XXX Legacy regions do not include the region tag and data. */
565 if (i == 0 && (h->flags & HEADERFLAG_LEGACY)) {
568 memcpy(pe+1, src, rdl);
569 memcpy(te, src + rdl, rdlen);
572 pe->offset = htonl(te - dataStart);
575 stei[2] = htonl(-rdl-entry->info.count);
577 memcpy(te, stei, entry->info.count);
578 te += entry->info.count;
580 rdlen += entry->info.count;
582 count = regionSwab(NULL, ril, 0, pe, t, NULL, 0);
588 memcpy(pe+1, src + sizeof(*pe), ((ril-1) * sizeof(*pe)));
589 memcpy(te, src + (ril * sizeof(*pe)), rdlen+entry->info.count+drlen);
592 entryInfo se = (entryInfo)src;
593 int off = ntohl(se->offset);
594 pe->offset = (off) ? htonl(te - dataStart) : htonl(off);
596 te += entry->info.count + drlen;
598 count = regionSwab(NULL, ril, 0, pe, t, NULL, 0);
599 if (count != (rdlen + entry->info.count + drlen))
603 /* Skip rest of entries in region. */
604 while (i < h->indexUsed && entry->info.offset <= rid+1) {
614 /* Ignore deleted drips. */
615 if (entry->data == NULL || entry->length <= 0)
619 type = entry->info.type;
620 if (typeSizes[type] > 1) {
622 diff = typeSizes[type] - ((te - dataStart) % typeSizes[type]);
623 if (diff != typeSizes[type]) {
630 pe->offset = htonl(te - dataStart);
632 /* copy data w/ endian conversions */
633 switch (entry->info.type) {
635 count = entry->info.count;
638 *((uint64_t *)te) = htonll(*((uint64_t *)src));
639 te += sizeof(uint64_t);
640 src += sizeof(uint64_t);
645 count = entry->info.count;
648 *((int32_t *)te) = htonl(*((int32_t *)src));
649 te += sizeof(int32_t);
650 src += sizeof(int32_t);
655 count = entry->info.count;
658 *((int16_t *)te) = htons(*((int16_t *)src));
659 te += sizeof(int16_t);
660 src += sizeof(int16_t);
665 memcpy(te, entry->data, entry->length);
672 /* Insure that there are no memcpy underruns/overruns. */
673 if (((char *)pe) != dataStart)
675 if ((((char *)ei)+len) != te)
681 h->flags &= ~HEADERFLAG_SORTED;
691 void * headerUnload(Header h)
694 void * uh = doHeaderUnload(h, &length);
699 * Find matching (tag,type) entry in header.
701 * @param tag entry tag
702 * @param type entry type
703 * @return header entry
706 indexEntry findEntry(Header h, rpmTag tag, rpmTagType type)
708 indexEntry entry, entry2, last;
709 struct indexEntry_s key;
711 if (h == NULL) return NULL;
712 if (!(h->flags & HEADERFLAG_SORTED)) headerSort(h);
717 bsearch(&key, h->index, h->indexUsed, sizeof(*h->index), indexCmp);
721 if (type == RPM_NULL_TYPE)
725 while (entry->info.tag == tag && entry->info.type != type &&
726 entry > h->index) entry--;
728 if (entry->info.tag == tag && entry->info.type == type)
731 last = h->index + h->indexUsed;
732 /* FIX: entry2 = entry. Code looks bogus as well. */
733 while (entry2->info.tag == tag && entry2->info.type != type &&
734 entry2 < last) entry2++;
736 if (entry->info.tag == tag && entry->info.type == type)
742 int headerDel(Header h, rpmTag 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 headerLoad(void * uh)
781 int32_t * ei = (int32_t *) uh;
782 int32_t il = ntohl(ei[0]); /* index length */
783 int32_t dl = ntohl(ei[1]); /* data length */
784 size_t pvlen = sizeof(il) + sizeof(dl) +
785 (il * sizeof(struct entryInfo_s)) + dl;
789 unsigned char * dataStart;
790 unsigned char * dataEnd;
794 /* Sanity checks on header intro. */
795 if (hdrchkTags(il) || hdrchkData(dl))
799 pe = (entryInfo) &ei[2];
800 dataStart = (unsigned char *) (pe + il);
801 dataEnd = dataStart + dl;
803 h = xcalloc(1, sizeof(*h));
805 h->indexAlloced = il + 1;
808 h->index = xcalloc(h->indexAlloced, sizeof(*h->index));
809 h->flags |= HEADERFLAG_SORTED;
814 if (!(htonl(pe->tag) < HEADER_I18NTABLE)) {
815 h->flags |= HEADERFLAG_LEGACY;
816 entry->info.type = REGION_TAG_TYPE;
817 entry->info.tag = HEADER_IMAGE;
818 entry->info.count = REGION_TAG_COUNT;
819 entry->info.offset = ((unsigned char *)pe - dataStart); /* negative offset */
822 entry->length = pvlen - sizeof(il) - sizeof(dl);
823 rdlen = regionSwab(entry+1, il, 0, pe, dataStart, dataEnd, entry->info.offset);
826 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 = HEADER_IMAGE;
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, rpmTag tag)
924 void * uh = doHeaderUnload(h, &length);
934 if (ENTRY_IS_REGION(nh->index)) {
935 if (tag == HEADER_SIGNATURES || tag == HEADER_IMMUTABLE)
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, enum hMagic magicp)
978 memset(block, 0, sizeof(block));
980 if (magicp == HEADER_MAGIC_YES)
984 if (timedRead(fd, (char *)block, i*sizeof(*block)) != (i * sizeof(*block)))
989 if (magicp == HEADER_MAGIC_YES) {
991 if (memcmp(&magic, rpm_header_magic, sizeof(magic)))
993 reserved = block[i++];
996 il = ntohl(block[i]); i++;
997 dl = ntohl(block[i]); i++;
999 len = sizeof(il) + sizeof(dl) + (il * sizeof(struct entryInfo_s)) + dl;
1001 /* Sanity checks on header intro. */
1002 if (hdrchkTags(il) || hdrchkData(dl) || len > headerMaxbytes)
1008 len -= sizeof(il) + sizeof(dl);
1011 if (timedRead(fd, (char *)&ei[2], len) != len)
1017 if (h == NULL && ei != NULL) {
1023 int headerWrite(FD_t fd, Header h, enum hMagic magicp)
1031 uh = doHeaderUnload(h, &length);
1035 case HEADER_MAGIC_YES:
1036 nb = Fwrite(rpm_header_magic, sizeof(uint8_t), sizeof(rpm_header_magic), fd);
1037 if (nb != sizeof(rpm_header_magic))
1040 case HEADER_MAGIC_NO:
1044 nb = Fwrite(uh, sizeof(char), length, fd);
1048 return (nb == length ? 0 : 1);
1051 int headerIsEntry(Header h, rpmTag tag)
1053 /* FIX: h modified by sort. */
1054 return (findEntry(h, tag, RPM_NULL_TYPE) ? 1 : 0);
1059 * Retrieve data from header entry.
1060 * Relevant flags (others are ignored), if neither is set allocation
1061 * behavior depends on data type(!)
1062 * HEADERGET_MINMEM: return pointers to header memory
1063 * HEADERGET_ALLOC: always return malloced memory, overrides MINMEM
1065 * @todo Permit retrieval of regions other than HEADER_IMUTABLE.
1066 * @param entry header entry
1067 * @param td tag data container
1068 * @param minMem string pointers refer to header memory?
1069 * @param flags flags to control memory allocation
1070 * @return 1 on success, otherwise error.
1072 static int copyTdEntry(const indexEntry entry, rpmtd td, headerGetFlags flags)
1074 rpm_count_t count = entry->info.count;
1075 int rc = 1; /* XXX 1 on success. */
1076 /* ALLOC overrides MINMEM */
1077 int allocMem = flags & HEADERGET_ALLOC;
1078 int minMem = allocMem ? 0 : flags & HEADERGET_MINMEM;
1079 int argvArray = (flags & HEADERGET_ARGV) ? 1 : 0;
1082 td->flags = RPMTD_IMMUTABLE;
1083 switch (entry->info.type) {
1086 * XXX This only works for
1087 * XXX "sealed" HEADER_IMMUTABLE/HEADER_SIGNATURES/HEADER_IMAGE.
1088 * XXX This will *not* work for unsealed legacy HEADER_IMAGE (i.e.
1089 * XXX a legacy header freshly read, but not yet unloaded to the rpmdb).
1091 if (ENTRY_IS_REGION(entry)) {
1092 int32_t * ei = ((int32_t *)entry->data) - 2;
1093 entryInfo pe = (entryInfo) (ei + 2);
1094 unsigned char * dataStart = (unsigned char *) (pe + ntohl(ei[0]));
1095 int32_t rdl = -entry->info.offset; /* negative offset */
1096 int32_t ril = rdl/sizeof(*pe);
1099 count = 2 * sizeof(*ei) + (ril * sizeof(*pe)) + rdl;
1100 if (entry->info.tag == HEADER_IMAGE) {
1104 count += REGION_TAG_COUNT;
1105 rdl += REGION_TAG_COUNT;
1108 td->data = xmalloc(count);
1109 ei = (int32_t *) td->data;
1113 pe = (entryInfo) memcpy(ei + 2, pe, (ril * sizeof(*pe)));
1115 dataStart = (unsigned char *) memcpy(pe + ril, dataStart, rdl);
1117 rc = regionSwab(NULL, ril, 0, pe, dataStart, dataStart + rdl, 0);
1118 /* don't return data on failure */
1120 td->data = _free(td->data);
1122 /* XXX 1 on success. */
1123 rc = (rc < 0) ? 0 : 1;
1125 count = entry->length;
1127 ? memcpy(xmalloc(count), entry->data, count)
1131 case RPM_STRING_TYPE:
1133 td->data = allocMem ? xstrdup(entry->data) : entry->data;
1136 case RPM_STRING_ARRAY_TYPE:
1137 case RPM_I18NSTRING_TYPE:
1138 { const char ** ptrEntry;
1139 int tableSize = (count + argvArray) * sizeof(char *);
1144 td->data = xmalloc(tableSize);
1145 ptrEntry = (const char **) td->data;
1148 t = xmalloc(tableSize + entry->length);
1149 td->data = (void *)t;
1150 ptrEntry = (const char **) td->data;
1152 memcpy(t, entry->data, entry->length);
1154 for (i = 0; i < count; i++) {
1161 td->flags |= RPMTD_ARGV;
1166 case RPM_INT16_TYPE:
1167 case RPM_INT32_TYPE:
1168 case RPM_INT64_TYPE:
1170 td->data = xmalloc(entry->length);
1171 memcpy(td->data, entry->data, entry->length);
1173 td->data = entry->data;
1177 /* WTH? Don't mess with unknown data types... */
1182 td->type = entry->info.type;
1185 if (td->data && entry->data != td->data) {
1186 td->flags |= RPMTD_ALLOCED;
1193 * Does locale match entry in header i18n table?
1196 * The range [l,le) contains the next locale to match:
1197 * ll[_CC][.EEEEE][@dddd]
1199 * ll ISO language code (in lowercase).
1200 * CC (optional) ISO coutnry code (in uppercase).
1201 * EEEEE (optional) encoding (not really standardized).
1202 * dddd (optional) dialect.
1205 * @param td header i18n table data, NUL terminated
1206 * @param l start of locale to match
1207 * @param le end of locale to match
1208 * @return 1 on good match, 2 on weak match, 0 on no match
1210 static int headerMatchLocale(const char *td, const char *l, const char *le)
1214 /* First try a complete match. */
1215 if (strlen(td) == (le-l) && rstreqn(td, l, (le - l)))
1218 /* Next, try stripping optional dialect and matching. */
1219 for (fe = l; fe < le && *fe != '@'; fe++)
1221 if (fe < le && rstreqn(td, l, (fe - l)))
1224 /* Next, try stripping optional codeset and matching. */
1225 for (fe = l; fe < le && *fe != '.'; fe++)
1227 if (fe < le && rstreqn(td, l, (fe - l)))
1230 /* Finally, try stripping optional country code and matching. */
1231 for (fe = l; fe < le && *fe != '_'; fe++)
1233 if (fe < le && rstreqn(td, l, (fe - l)))
1240 * Return i18n string from header that matches locale.
1242 * @param entry i18n string data
1243 * @retval td tag data container
1244 * @param flags flags to control allocation
1247 static int copyI18NEntry(Header h, indexEntry entry, rpmtd td,
1248 headerGetFlags flags)
1250 const char *lang, *l, *le;
1253 td->type = RPM_STRING_TYPE;
1255 /* if no match, just return the first string */
1256 td->data = entry->data;
1258 /* XXX Drepper sez' this is the order. */
1259 if ((lang = getenv("LANGUAGE")) == NULL &&
1260 (lang = getenv("LC_ALL")) == NULL &&
1261 (lang = getenv("LC_MESSAGES")) == NULL &&
1262 (lang = getenv("LANG")) == NULL)
1265 if ((table = findEntry(h, HEADER_I18NTABLE, RPM_STRING_ARRAY_TYPE)) == NULL)
1268 for (l = lang; *l != '\0'; l = le) {
1270 char *ed, *ed_weak = NULL;
1273 while (*l && *l == ':') /* skip leading colons */
1277 for (le = l; *le && *le != ':'; le++) /* find end of this locale */
1280 /* For each entry in the header ... */
1281 for (langNum = 0, t = table->data, ed = entry->data;
1282 langNum < entry->info.count;
1283 langNum++, t += strlen(t) + 1, ed += strlen(ed) + 1) {
1285 int match = headerMatchLocale(t, l, le);
1289 } else if (match == 2) {
1300 if (flags & HEADERGET_ALLOC) {
1301 td->data = xstrdup(td->data);
1302 td->flags |= RPMTD_ALLOCED;
1309 * Retrieve tag data from header.
1311 * @retval td tag data container
1312 * @param flags flags to control retrieval
1313 * @return 1 on success, 0 on not found
1315 static int intGetTdEntry(Header h, rpmtd td, headerGetFlags flags)
1320 /* First find the tag */
1321 /* FIX: h modified by sort. */
1322 entry = findEntry(h, td->tag, RPM_NULL_TYPE);
1323 if (entry == NULL) {
1324 /* Td is zeroed above, just return... */
1328 if (flags & HEADERGET_RAW) {
1329 rc = copyTdEntry(entry, td, flags);
1331 switch (entry->info.type) {
1332 case RPM_I18NSTRING_TYPE:
1333 rc = copyI18NEntry(h, entry, td, flags);
1336 rc = copyTdEntry(entry, td, flags);
1341 /* XXX 1 on success */
1342 return ((rc == 1) ? 1 : 0);
1345 int headerGet(Header h, rpmTag tag, rpmtd td, headerGetFlags flags)
1348 headerTagTagFunction tagfunc = intGetTdEntry;
1350 if (td == NULL) return 0;
1355 if (flags & HEADERGET_EXT) {
1356 headerTagTagFunction extfunc = rpmHeaderTagFunc(tag);
1357 if (extfunc) tagfunc = extfunc;
1359 rc = tagfunc(h, td, flags);
1361 assert(tag == td->tag);
1367 static void copyData(rpmTagType type, rpm_data_t dstPtr,
1368 rpm_constdata_t srcPtr, rpm_count_t cnt, int dataLength)
1371 case RPM_STRING_ARRAY_TYPE:
1372 case RPM_I18NSTRING_TYPE:
1373 { const char ** av = (const char **) srcPtr;
1376 while (cnt-- > 0 && dataLength > 0) {
1378 if ((s = *av++) == NULL)
1382 } while (s[-1] && --dataLength > 0);
1387 memmove(dstPtr, srcPtr, dataLength);
1393 * Return (malloc'ed) copy of entry data.
1394 * @param type entry data type
1395 * @param p entry data
1396 * @param c entry item count
1397 * @retval lengthPtr no. bytes in returned data
1398 * @return (malloc'ed) copy of entry data, NULL on error
1401 grabData(rpmTagType type, rpm_constdata_t p, rpm_count_t c, int * lengthPtr)
1403 rpm_data_t data = NULL;
1406 length = dataLength(type, p, c, 0, NULL);
1408 data = xmalloc(length);
1409 copyData(type, data, p, c, length);
1413 *lengthPtr = length;
1417 static int intAddEntry(Header h, rpmtd td)
1423 /* Count must always be >= 1 for headerAddEntry. */
1427 if (hdrchkType(td->type))
1429 if (hdrchkData(td->count))
1433 data = grabData(td->type, td->data, td->count, &length);
1434 if (data == NULL || length <= 0)
1437 /* Allocate more index space if necessary */
1438 if (h->indexUsed == h->indexAlloced) {
1439 h->indexAlloced += INDEX_MALLOC_SIZE;
1440 h->index = xrealloc(h->index, h->indexAlloced * sizeof(*h->index));
1443 /* Fill in the index */
1444 entry = h->index + h->indexUsed;
1445 entry->info.tag = td->tag;
1446 entry->info.type = td->type;
1447 entry->info.count = td->count;
1448 entry->info.offset = 0;
1450 entry->length = length;
1452 if (h->indexUsed > 0 && td->tag < h->index[h->indexUsed-1].info.tag)
1453 h->flags &= ~HEADERFLAG_SORTED;
1459 static int intAppendEntry(Header h, rpmtd td)
1464 if (td->type == RPM_STRING_TYPE || td->type == RPM_I18NSTRING_TYPE) {
1465 /* we can't do this */
1469 /* Find the tag entry in the header. */
1470 entry = findEntry(h, td->tag, td->type);
1474 length = dataLength(td->type, td->data, td->count, 0, NULL);
1478 if (ENTRY_IN_REGION(entry)) {
1479 char * t = xmalloc(entry->length + length);
1480 memcpy(t, entry->data, entry->length);
1482 entry->info.offset = 0;
1484 entry->data = xrealloc(entry->data, entry->length + length);
1486 copyData(td->type, ((char *) entry->data) + entry->length,
1487 td->data, td->count, length);
1489 entry->length += length;
1491 entry->info.count += td->count;
1496 int headerPut(Header h, rpmtd td, headerPutFlags flags)
1501 if (flags & HEADERPUT_APPEND) {
1502 rc = findEntry(h, td->tag, td->type) ?
1503 intAppendEntry(h, td) :
1506 rc = intAddEntry(h, td);
1512 * Sanity check data types against tag table before putting. Assume
1513 * append on all array-types.
1515 static int headerPutType(Header h, rpmTag tag, rpmTagType reqtype,
1516 rpm_constdata_t data, rpm_count_t size)
1519 rpmTagType type = rpmTagGetType(tag);
1520 headerPutFlags flags = HEADERPUT_APPEND;
1523 /* Basic sanity checks: type must match and there must be data to put */
1524 if ((type & RPM_MASK_TYPE) != reqtype
1525 || size < 1 || data == NULL || h == NULL) {
1530 * Non-array types can't be appended to. Binary types use size
1531 * for data length, for other non-array types size must be 1.
1533 if ((type & RPM_MASK_RETURN_TYPE) != RPM_ARRAY_RETURN_TYPE) {
1534 flags = HEADERPUT_DEFAULT;
1535 if ((type & RPM_MASK_TYPE) != RPM_BIN_TYPE && size != 1) {
1543 td.type = type & RPM_MASK_TYPE;
1544 td.data = (void *) data;
1547 valid = headerPut(h, &td, flags);
1553 int headerPutString(Header h, rpmTag tag, const char *val)
1555 rpmTagType type = rpmTagGetType(tag) & RPM_MASK_TYPE;
1556 const void *sptr = NULL;
1558 /* string arrays expect char **, arrange that */
1559 if (type == RPM_STRING_ARRAY_TYPE || type == RPM_I18NSTRING_TYPE) {
1561 } else if (type == RPM_STRING_TYPE) {
1567 return headerPutType(h, tag, type, sptr, 1);
1570 int headerPutStringArray(Header h, rpmTag tag, const char **array, rpm_count_t size)
1572 return headerPutType(h, tag, RPM_STRING_ARRAY_TYPE, array, size);
1575 int headerPutChar(Header h, rpmTag tag, char *val, rpm_count_t size)
1577 return headerPutType(h, tag, RPM_CHAR_TYPE, val, size);
1580 int headerPutUint8(Header h, rpmTag tag, uint8_t *val, rpm_count_t size)
1582 return headerPutType(h, tag, RPM_INT8_TYPE, val, size);
1585 int headerPutUint16(Header h, rpmTag tag, uint16_t *val, rpm_count_t size)
1587 return headerPutType(h, tag, RPM_INT16_TYPE, val, size);
1590 int headerPutUint32(Header h, rpmTag tag, uint32_t *val, rpm_count_t size)
1592 return headerPutType(h, tag, RPM_INT32_TYPE, val, size);
1595 int headerPutUint64(Header h, rpmTag tag, uint64_t *val, rpm_count_t size)
1597 return headerPutType(h, tag, RPM_INT64_TYPE, val, size);
1600 int headerPutBin(Header h, rpmTag tag, uint8_t *val, rpm_count_t size)
1602 return headerPutType(h, tag, RPM_BIN_TYPE, val, size);
1605 int headerAddI18NString(Header h, rpmTag tag, const char * string,
1608 indexEntry table, entry;
1609 const char ** strArray;
1612 rpm_count_t i, langNum;
1615 table = findEntry(h, HEADER_I18NTABLE, RPM_STRING_ARRAY_TYPE);
1616 entry = findEntry(h, tag, RPM_I18NSTRING_TYPE);
1618 if (!table && entry)
1619 return 0; /* this shouldn't ever happen!! */
1621 if (!table && !entry) {
1622 const char * charArray[2];
1623 rpm_count_t count = 0;
1625 if (!lang || (lang[0] == 'C' && lang[1] == '\0')) {
1626 charArray[count++] = "C";
1628 charArray[count++] = "C";
1629 charArray[count++] = lang;
1633 td.tag = HEADER_I18NTABLE;
1634 td.type = RPM_STRING_ARRAY_TYPE;
1635 td.data = (void *) charArray;
1637 if (!headerPut(h, &td, HEADERPUT_DEFAULT))
1639 table = findEntry(h, HEADER_I18NTABLE, RPM_STRING_ARRAY_TYPE);
1644 if (!lang) lang = "C";
1646 { const char * l = table->data;
1647 for (langNum = 0; langNum < table->info.count; langNum++) {
1648 if (rstreq(l, lang)) break;
1653 if (langNum >= table->info.count) {
1654 length = strlen(lang) + 1;
1655 if (ENTRY_IN_REGION(table)) {
1656 char * t = xmalloc(table->length + length);
1657 memcpy(t, table->data, table->length);
1659 table->info.offset = 0;
1661 table->data = xrealloc(table->data, table->length + length);
1662 memmove(((char *)table->data) + table->length, lang, length);
1663 table->length += length;
1664 table->info.count++;
1670 strArray = xmalloc(sizeof(*strArray) * (langNum + 1));
1671 for (i = 0; i < langNum; i++)
1673 strArray[langNum] = string;
1677 td.type = RPM_I18NSTRING_TYPE;
1679 td.count = langNum + 1;
1680 rc = headerPut(h, &td, HEADERPUT_DEFAULT);
1683 } else if (langNum >= entry->info.count) {
1684 ghosts = langNum - entry->info.count;
1686 length = strlen(string) + 1 + ghosts;
1687 if (ENTRY_IN_REGION(entry)) {
1688 char * t = xmalloc(entry->length + length);
1689 memcpy(t, entry->data, entry->length);
1691 entry->info.offset = 0;
1693 entry->data = xrealloc(entry->data, entry->length + length);
1695 memset(((char *)entry->data) + entry->length, '\0', ghosts);
1696 memmove(((char *)entry->data) + entry->length + ghosts, string, strlen(string)+1);
1698 entry->length += length;
1699 entry->info.count = langNum + 1;
1701 char *b, *be, *e, *ee, *t;
1704 /* Set beginning/end pointers to previous data */
1705 b = be = e = ee = entry->data;
1706 for (i = 0; i < table->info.count; i++) {
1709 ee += strlen(ee) + 1;
1714 /* Get storage for new buffer */
1716 sn = strlen(string) + 1;
1718 length = bn + sn + en;
1719 t = buf = xmalloc(length);
1721 /* Copy values into new storage */
1724 memcpy(t, string, sn);
1729 /* Replace i18N string array */
1730 entry->length -= strlen(be) + 1;
1731 entry->length += sn;
1733 if (ENTRY_IN_REGION(entry)) {
1734 entry->info.offset = 0;
1736 entry->data = _free(entry->data);
1743 int headerMod(Header h, rpmtd td)
1750 /* First find the tag */
1751 entry = findEntry(h, td->tag, td->type);
1756 data = grabData(td->type, td->data, td->count, &length);
1757 if (data == NULL || length <= 0)
1760 /* make sure entry points to the first occurence of this tag */
1761 while (entry > h->index && (entry - 1)->info.tag == td->tag)
1764 /* free after we've grabbed the new data in case the two are intertwined;
1765 that's a bad idea but at least we won't break */
1766 oldData = entry->data;
1768 entry->info.count = td->count;
1769 entry->info.type = td->type;
1771 entry->length = length;
1773 if (ENTRY_IN_REGION(entry)) {
1774 entry->info.offset = 0;
1776 oldData = _free(oldData);
1782 * Header tag iterator data structure.
1784 struct headerIterator_s {
1785 Header h; /*!< Header being iterated. */
1786 int next_index; /*!< Next tag index. */
1789 HeaderIterator headerFreeIterator(HeaderIterator hi)
1792 hi->h = headerFree(hi->h);
1798 HeaderIterator headerInitIterator(Header h)
1800 HeaderIterator hi = xmalloc(sizeof(*hi));
1804 hi->h = headerLink(h);
1809 int headerNext(HeaderIterator hi, rpmtd td)
1813 indexEntry entry = NULL;
1819 for (slot = hi->next_index; slot < h->indexUsed; slot++) {
1820 entry = h->index + slot;
1821 if (!ENTRY_IS_REGION(entry))
1824 hi->next_index = slot;
1825 if (entry == NULL || slot >= h->indexUsed)
1830 td->tag = entry->info.tag;
1832 rc = copyTdEntry(entry, td, HEADERGET_DEFAULT);
1834 /* XXX 1 on success */
1835 return ((rc == 1) ? 1 : 0);
1839 * Duplicate a header.
1841 * @return new header instance
1843 Header headerCopy(Header h)
1845 Header nh = headerNew();
1849 hi = headerInitIterator(h);
1850 while (headerNext(hi, &td)) {
1851 if (rpmtdCount(&td) > 0) {
1852 (void) headerPut(nh, &td, HEADERPUT_DEFAULT);
1856 hi = headerFreeIterator(hi);
1858 return headerReload(nh, HEADER_IMAGE);
1861 void headerCopyTags(Header headerFrom, Header headerTo,
1862 const rpmTag * tagstocopy)
1867 if (headerFrom == headerTo)
1870 for (p = tagstocopy; *p != 0; p++) {
1871 if (headerIsEntry(headerTo, *p))
1873 if (!headerGet(headerFrom, *p, &td, HEADERGET_MINMEM))
1875 (void) headerPut(headerTo, &td, HEADERPUT_DEFAULT);
1880 unsigned int headerGetInstance(Header h)
1882 return h ? h->instance : 0;
1885 void headerSetInstance(Header h, unsigned int instance)
1887 h->instance = instance;
1890 char * headerGetAsString(Header h, rpmTag tag)
1895 if (headerGet(h, tag, &td, HEADERGET_EXT)) {
1896 if (rpmtdCount(&td) == 1) {
1897 res = rpmtdFormat(&td, RPMTD_FORMAT_STRING, NULL);
1904 const char * headerGetString(Header h, rpmTag tag)
1906 const char *res = NULL;
1909 if (headerGet(h, tag, &td, HEADERGET_MINMEM)) {
1910 if (rpmtdCount(&td) == 1) {
1911 res = rpmtdGetString(&td);
1918 uint64_t headerGetNumber(Header h, rpmTag tag)
1923 if (headerGet(h, tag, &td, HEADERGET_EXT)) {
1924 if (rpmtdCount(&td) == 1) {
1925 res = rpmtdGetNumber(&td);