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;
237 case HEADER_MAGIC_YES:
238 size += sizeof(rpm_header_magic);
240 case HEADER_MAGIC_NO:
244 size += 2 * sizeof(int32_t); /* count of index entries */
246 for (i = 0, entry = h->index; i < h->indexUsed; i++, entry++) {
249 /* Regions go in as is ... */
250 if (ENTRY_IS_REGION(entry)) {
251 size += entry->length;
252 /* XXX Legacy regions do not include the region tag and data. */
253 if (i == 0 && (h->flags & HEADERFLAG_LEGACY))
254 size += sizeof(struct entryInfo_s) + entry->info.count;
258 /* ... and region elements are skipped. */
259 if (entry->info.offset < 0)
263 type = entry->info.type;
264 if (typeSizes[type] > 1) {
265 unsigned diff = typeSizes[type] - (size % typeSizes[type]);
266 if (diff != typeSizes[type]) {
271 size += sizeof(struct entryInfo_s) + entry->length;
278 * Return length of entry data.
279 * @param type entry data type
280 * @param p entry data
281 * @param count entry item count
282 * @param onDisk data is concatenated strings (with NUL's))?
283 * @param pend pointer to end of data (or NULL)
284 * @return no. bytes in data, -1 on failure
286 static int dataLength(rpmTagType type, rpm_constdata_t p, rpm_count_t count,
287 int onDisk, rpm_constdata_t pend)
289 const unsigned char * s = p;
290 const unsigned char * se = pend;
294 case RPM_STRING_TYPE:
302 length++; /* count nul terminator too. */
305 case RPM_STRING_ARRAY_TYPE:
306 case RPM_I18NSTRING_TYPE:
307 /* These are like RPM_STRING_TYPE, except they're *always* an array */
308 /* Compute sum of length of all strings, including nul terminators */
312 length++; /* count nul terminator too */
320 const char ** av = (const char **)p;
322 /* add one for null termination */
323 length += strlen(*av++) + 1;
329 if (typeSizes[type] == -1)
331 length = typeSizes[(type & 0xf)] * count;
332 if (length < 0 || (se && (s + length) > se))
341 * Swap int32_t and int16_t arrays within header region.
343 * If a header region tag is in the set to be swabbed, as the data for a
344 * a header region is located after all other tag data.
346 * @param entry header entry
347 * @param il no. of entries
348 * @param dl start no. bytes of data
349 * @param pe header physical entry pointer (swapped)
350 * @param dataStart header data start
351 * @param dataEnd header data end
352 * @param regionid region offset
353 * @return no. bytes of data in region, -1 on error
355 static int regionSwab(indexEntry entry, int il, int dl,
357 unsigned char * dataStart,
358 const unsigned char * dataEnd,
361 for (; il > 0; il--, pe++) {
362 struct indexEntry_s ie;
365 ie.info.tag = ntohl(pe->tag);
366 ie.info.type = ntohl(pe->type);
367 ie.info.count = ntohl(pe->count);
368 ie.info.offset = ntohl(pe->offset);
370 if (hdrchkType(ie.info.type))
372 if (hdrchkData(ie.info.count))
374 if (hdrchkData(ie.info.offset))
376 if (hdrchkAlign(ie.info.type, ie.info.offset))
379 ie.data = dataStart + ie.info.offset;
380 if (dataEnd && (unsigned char *)ie.data >= dataEnd)
383 ie.length = dataLength(ie.info.type, ie.data, ie.info.count, 1, dataEnd);
384 if (ie.length < 0 || hdrchkData(ie.length))
390 ie.info.offset = regionid;
391 *entry = ie; /* structure assignment */
397 if (typeSizes[type] > 1) {
398 unsigned diff = typeSizes[type] - (dl % typeSizes[type]);
399 if (diff != typeSizes[type]) {
404 /* Perform endian conversions */
405 switch (ntohl(pe->type)) {
407 { uint64_t * it = ie.data;
408 for (; ie.info.count > 0; ie.info.count--, it += 1) {
409 if (dataEnd && ((unsigned char *)it) >= dataEnd)
415 { int32_t * it = ie.data;
416 for (; ie.info.count > 0; ie.info.count--, it += 1) {
417 if (dataEnd && ((unsigned char *)it) >= dataEnd)
423 { int16_t * it = ie.data;
424 for (; ie.info.count > 0; ie.info.count--, it += 1) {
425 if (dataEnd && ((unsigned char *)it) >= dataEnd)
441 * @retval *lengthPtr no. bytes in unloaded header blob
442 * @return unloaded header blob (NULL on error)
444 static void * doHeaderUnload(Header h,
457 int drlen, ndribbles;
460 /* Sort entries by (offset,tag). */
463 /* Compute (il,dl) for all tags, including those deleted in region. */
464 drlen = ndribbles = driplen = ndrips = 0;
465 for (i = 0, entry = h->index; i < h->indexUsed; i++, entry++) {
466 if (ENTRY_IS_REGION(entry)) {
467 int32_t rdl = -entry->info.offset; /* negative offset */
468 int32_t ril = rdl/sizeof(*pe);
469 int rid = entry->info.offset;
472 dl += entry->rdlen + entry->info.count;
473 /* XXX Legacy regions do not include the region tag and data. */
474 if (i == 0 && (h->flags & HEADERFLAG_LEGACY))
477 /* Skip rest of entries in region, but account for dribbles. */
478 for (; i < h->indexUsed && entry->info.offset <= rid+1; i++, entry++) {
479 if (entry->info.offset <= rid)
483 type = entry->info.type;
484 if (typeSizes[type] > 1) {
485 unsigned diff = typeSizes[type] - (dl % typeSizes[type]);
486 if (diff != typeSizes[type]) {
494 drlen += entry->length;
502 /* Ignore deleted drips. */
503 if (entry->data == NULL || entry->length <= 0)
507 type = entry->info.type;
508 if (typeSizes[type] > 1) {
509 unsigned diff = typeSizes[type] - (dl % typeSizes[type]);
510 if (diff != typeSizes[type]) {
518 driplen += entry->length;
522 /* Sanity checks on header intro. */
523 if (hdrchkTags(il) || hdrchkData(dl))
526 len = sizeof(il) + sizeof(dl) + (il * sizeof(*pe)) + dl;
532 pe = (entryInfo) &ei[2];
533 dataStart = te = (char *) (pe + il);
535 for (i = 0, entry = h->index; i < h->indexUsed; i++, entry++) {
541 if (entry->data == NULL || entry->length <= 0)
544 t = (unsigned char*)te;
545 pe->tag = htonl(entry->info.tag);
546 pe->type = htonl(entry->info.type);
547 pe->count = htonl(entry->info.count);
549 if (ENTRY_IS_REGION(entry)) {
550 int32_t rdl = -entry->info.offset; /* negative offset */
551 int32_t ril = rdl/sizeof(*pe) + ndribbles;
552 int rid = entry->info.offset;
554 src = (char *)entry->data;
555 rdlen = entry->rdlen;
557 /* XXX Legacy regions do not include the region tag and data. */
558 if (i == 0 && (h->flags & HEADERFLAG_LEGACY)) {
561 memcpy(pe+1, src, rdl);
562 memcpy(te, src + rdl, rdlen);
565 pe->offset = htonl(te - dataStart);
568 stei[2] = htonl(-rdl-entry->info.count);
570 memcpy(te, stei, entry->info.count);
571 te += entry->info.count;
573 rdlen += entry->info.count;
575 count = regionSwab(NULL, ril, 0, pe, t, NULL, 0);
581 memcpy(pe+1, src + sizeof(*pe), ((ril-1) * sizeof(*pe)));
582 memcpy(te, src + (ril * sizeof(*pe)), rdlen+entry->info.count+drlen);
585 entryInfo se = (entryInfo)src;
586 int off = ntohl(se->offset);
587 pe->offset = (off) ? htonl(te - dataStart) : htonl(off);
589 te += entry->info.count + drlen;
591 count = regionSwab(NULL, ril, 0, pe, t, NULL, 0);
592 if (count != (rdlen + entry->info.count + drlen))
596 /* Skip rest of entries in region. */
597 while (i < h->indexUsed && entry->info.offset <= rid+1) {
607 /* Ignore deleted drips. */
608 if (entry->data == NULL || entry->length <= 0)
612 type = entry->info.type;
613 if (typeSizes[type] > 1) {
615 diff = typeSizes[type] - ((te - dataStart) % typeSizes[type]);
616 if (diff != typeSizes[type]) {
622 pe->offset = htonl(te - dataStart);
624 /* copy data w/ endian conversions */
625 switch (entry->info.type) {
627 count = entry->info.count;
630 *((uint64_t *)te) = htonll(*((uint64_t *)src));
631 te += sizeof(uint64_t);
632 src += sizeof(uint64_t);
637 count = entry->info.count;
640 *((int32_t *)te) = htonl(*((int32_t *)src));
641 te += sizeof(int32_t);
642 src += sizeof(int32_t);
647 count = entry->info.count;
650 *((int16_t *)te) = htons(*((int16_t *)src));
651 te += sizeof(int16_t);
652 src += sizeof(int16_t);
657 memcpy(te, entry->data, entry->length);
664 /* Insure that there are no memcpy underruns/overruns. */
665 if (((char *)pe) != dataStart)
667 if ((((char *)ei)+len) != te)
673 h->flags &= ~HEADERFLAG_SORTED;
683 void * headerUnload(Header h)
686 void * uh = doHeaderUnload(h, &length);
691 * Find matching (tag,type) entry in header.
693 * @param tag entry tag
694 * @param type entry type
695 * @return header entry
698 indexEntry findEntry(Header h, rpmTag tag, rpmTagType type)
700 indexEntry entry, entry2, last;
701 struct indexEntry_s key;
703 if (h == NULL) return NULL;
704 if (!(h->flags & HEADERFLAG_SORTED)) headerSort(h);
709 bsearch(&key, h->index, h->indexUsed, sizeof(*h->index), indexCmp);
713 if (type == RPM_NULL_TYPE)
717 while (entry->info.tag == tag && entry->info.type != type &&
718 entry > h->index) entry--;
720 if (entry->info.tag == tag && entry->info.type == type)
723 last = h->index + h->indexUsed;
724 /* FIX: entry2 = entry. Code looks bogus as well. */
725 while (entry2->info.tag == tag && entry2->info.type != type &&
726 entry2 < last) entry2++;
728 if (entry->info.tag == tag && entry->info.type == type)
734 int headerDel(Header h, rpmTag tag)
736 indexEntry last = h->index + h->indexUsed;
737 indexEntry entry, first;
740 entry = findEntry(h, tag, RPM_NULL_TYPE);
741 if (!entry) return 1;
743 /* Make sure entry points to the first occurence of this tag. */
744 while (entry > h->index && (entry - 1)->info.tag == tag)
747 /* Free data for tags being removed. */
748 for (first = entry; first < last; first++) {
750 if (first->info.tag != tag)
755 if (ENTRY_IN_REGION(first))
760 ne = (first - entry);
765 memmove(entry, first, (ne * sizeof(*entry)));
771 Header headerLoad(void * uh)
773 int32_t * ei = (int32_t *) uh;
774 int32_t il = ntohl(ei[0]); /* index length */
775 int32_t dl = ntohl(ei[1]); /* data length */
776 size_t pvlen = sizeof(il) + sizeof(dl) +
777 (il * sizeof(struct entryInfo_s)) + dl;
781 unsigned char * dataStart;
782 unsigned char * dataEnd;
786 /* Sanity checks on header intro. */
787 if (hdrchkTags(il) || hdrchkData(dl))
791 pe = (entryInfo) &ei[2];
792 dataStart = (unsigned char *) (pe + il);
793 dataEnd = dataStart + dl;
795 h = xcalloc(1, sizeof(*h));
797 h->indexAlloced = il + 1;
800 h->index = xcalloc(h->indexAlloced, sizeof(*h->index));
801 h->flags |= HEADERFLAG_SORTED;
806 if (!(htonl(pe->tag) < HEADER_I18NTABLE)) {
807 h->flags |= HEADERFLAG_LEGACY;
808 entry->info.type = REGION_TAG_TYPE;
809 entry->info.tag = HEADER_IMAGE;
810 entry->info.count = REGION_TAG_COUNT;
811 entry->info.offset = ((unsigned char *)pe - dataStart); /* negative offset */
814 entry->length = pvlen - sizeof(il) - sizeof(dl);
815 rdlen = regionSwab(entry+1, il, 0, pe, dataStart, dataEnd, entry->info.offset);
818 entry->rdlen = rdlen;
825 h->flags &= ~HEADERFLAG_LEGACY;
827 entry->info.type = htonl(pe->type);
828 entry->info.count = htonl(pe->count);
830 if (hdrchkType(entry->info.type))
832 if (hdrchkTags(entry->info.count))
835 { int off = ntohl(pe->offset);
840 size_t nb = REGION_TAG_COUNT;
842 /* XXX Hmm, why the copy? */
843 memcpy(&stei, dataStart + off, nb);
844 rdl = -ntohl(stei[2]); /* negative offset */
845 ril = rdl/sizeof(*pe);
846 if (hdrchkTags(ril) || hdrchkData(rdl))
848 entry->info.tag = htonl(pe->tag);
851 rdl = (ril * sizeof(struct entryInfo_s));
852 entry->info.tag = HEADER_IMAGE;
855 entry->info.offset = -rdl; /* negative offset */
858 entry->length = pvlen - sizeof(il) - sizeof(dl);
859 rdlen = regionSwab(entry+1, ril-1, 0, pe+1, dataStart, dataEnd, entry->info.offset);
862 entry->rdlen = rdlen;
864 if (ril < h->indexUsed) {
865 indexEntry newEntry = entry + ril;
866 int ne = (h->indexUsed - ril);
867 int rid = entry->info.offset+1;
870 /* Load dribble entries from region. */
871 rc = regionSwab(newEntry, ne, 0, pe+ril, dataStart, dataEnd, rid);
876 { indexEntry firstEntry = newEntry;
877 int save = h->indexUsed;
880 /* Dribble entries replace duplicate region entries. */
882 for (j = 0; j < ne; j++, newEntry++) {
883 (void) headerDel(h, newEntry->info.tag);
884 if (newEntry->info.tag == RPMTAG_BASENAMES)
885 (void) headerDel(h, RPMTAG_OLDFILENAMES);
888 /* If any duplicate entries were replaced, move new entries down. */
889 if (h->indexUsed < (save - ne)) {
890 memmove(h->index + h->indexUsed, firstEntry,
891 (ne * sizeof(*entry)));
898 h->flags &= ~HEADERFLAG_SORTED;
900 h->flags |= HEADERFLAG_ALLOCATED;
906 h->index = _free(h->index);
912 Header headerReload(Header h, rpmTag tag)
916 void * uh = doHeaderUnload(h, &length);
926 if (ENTRY_IS_REGION(nh->index)) {
927 if (tag == HEADER_SIGNATURES || tag == HEADER_IMMUTABLE)
928 nh->index[0].info.tag = tag;
933 Header headerCopyLoad(const void * uh)
935 int32_t * ei = (int32_t *) uh;
936 int32_t il = ntohl(ei[0]); /* index length */
937 int32_t dl = ntohl(ei[1]); /* data length */
938 size_t pvlen = sizeof(il) + sizeof(dl) +
939 (il * sizeof(struct entryInfo_s)) + dl;
943 /* Sanity checks on header intro. */
944 if (!(hdrchkTags(il) || hdrchkData(dl)) && pvlen < headerMaxbytes) {
945 nuh = memcpy(xmalloc(pvlen), uh, pvlen);
946 if ((h = headerLoad(nuh)) == NULL)
953 * Read (and load) header from file handle.
954 * @param fd file handle
955 * @param magicp read (and verify) 8 bytes of (magic, 0)?
956 * @return header (or NULL on error)
958 Header headerRead(FD_t fd, enum hMagic magicp)
970 memset(block, 0, sizeof(block));
972 if (magicp == HEADER_MAGIC_YES)
976 if (timedRead(fd, (char *)block, i*sizeof(*block)) != (i * sizeof(*block)))
981 if (magicp == HEADER_MAGIC_YES) {
983 if (memcmp(&magic, rpm_header_magic, sizeof(magic)))
985 reserved = block[i++];
988 il = ntohl(block[i]); i++;
989 dl = ntohl(block[i]); i++;
991 len = sizeof(il) + sizeof(dl) + (il * sizeof(struct entryInfo_s)) + dl;
993 /* Sanity checks on header intro. */
994 if (hdrchkTags(il) || hdrchkData(dl) || len > headerMaxbytes)
1000 len -= sizeof(il) + sizeof(dl);
1003 if (timedRead(fd, (char *)&ei[2], len) != len)
1009 if (h == NULL && ei != NULL) {
1015 int headerWrite(FD_t fd, Header h, enum hMagic magicp)
1023 uh = doHeaderUnload(h, &length);
1027 case HEADER_MAGIC_YES:
1028 nb = Fwrite(rpm_header_magic, sizeof(uint8_t), sizeof(rpm_header_magic), fd);
1029 if (nb != sizeof(rpm_header_magic))
1032 case HEADER_MAGIC_NO:
1036 nb = Fwrite(uh, sizeof(char), length, fd);
1040 return (nb == length ? 0 : 1);
1043 int headerIsEntry(Header h, rpmTag tag)
1045 /* FIX: h modified by sort. */
1046 return (findEntry(h, tag, RPM_NULL_TYPE) ? 1 : 0);
1051 * Retrieve data from header entry.
1052 * Relevant flags (others are ignored), if neither is set allocation
1053 * behavior depends on data type(!)
1054 * HEADERGET_MINMEM: return pointers to header memory
1055 * HEADERGET_ALLOC: always return malloced memory, overrides MINMEM
1057 * @todo Permit retrieval of regions other than HEADER_IMUTABLE.
1058 * @param entry header entry
1059 * @param td tag data container
1060 * @param minMem string pointers refer to header memory?
1061 * @param flags flags to control memory allocation
1062 * @return 1 on success, otherwise error.
1064 static int copyTdEntry(const indexEntry entry, rpmtd td, headerGetFlags flags)
1066 rpm_count_t count = entry->info.count;
1067 int rc = 1; /* XXX 1 on success. */
1068 /* ALLOC overrides MINMEM */
1069 int allocMem = flags & HEADERGET_ALLOC;
1070 int minMem = allocMem ? 0 : flags & HEADERGET_MINMEM;
1071 int argvArray = (flags & HEADERGET_ARGV) ? 1 : 0;
1074 td->flags = RPMTD_IMMUTABLE;
1075 switch (entry->info.type) {
1078 * XXX This only works for
1079 * XXX "sealed" HEADER_IMMUTABLE/HEADER_SIGNATURES/HEADER_IMAGE.
1080 * XXX This will *not* work for unsealed legacy HEADER_IMAGE (i.e.
1081 * XXX a legacy header freshly read, but not yet unloaded to the rpmdb).
1083 if (ENTRY_IS_REGION(entry)) {
1084 int32_t * ei = ((int32_t *)entry->data) - 2;
1085 entryInfo pe = (entryInfo) (ei + 2);
1086 unsigned char * dataStart = (unsigned char *) (pe + ntohl(ei[0]));
1087 int32_t rdl = -entry->info.offset; /* negative offset */
1088 int32_t ril = rdl/sizeof(*pe);
1091 count = 2 * sizeof(*ei) + (ril * sizeof(*pe)) + rdl;
1092 if (entry->info.tag == HEADER_IMAGE) {
1096 count += REGION_TAG_COUNT;
1097 rdl += REGION_TAG_COUNT;
1100 td->data = xmalloc(count);
1101 ei = (int32_t *) td->data;
1105 pe = (entryInfo) memcpy(ei + 2, pe, (ril * sizeof(*pe)));
1107 dataStart = (unsigned char *) memcpy(pe + ril, dataStart, rdl);
1109 rc = regionSwab(NULL, ril, 0, pe, dataStart, dataStart + rdl, 0);
1110 /* don't return data on failure */
1112 td->data = _free(td->data);
1114 /* XXX 1 on success. */
1115 rc = (rc < 0) ? 0 : 1;
1117 count = entry->length;
1119 ? memcpy(xmalloc(count), entry->data, count)
1123 case RPM_STRING_TYPE:
1125 td->data = allocMem ? xstrdup(entry->data) : entry->data;
1128 case RPM_STRING_ARRAY_TYPE:
1129 case RPM_I18NSTRING_TYPE:
1130 { const char ** ptrEntry;
1131 int tableSize = (count + argvArray) * sizeof(char *);
1136 td->data = xmalloc(tableSize);
1137 ptrEntry = (const char **) td->data;
1140 t = xmalloc(tableSize + entry->length);
1141 td->data = (void *)t;
1142 ptrEntry = (const char **) td->data;
1144 memcpy(t, entry->data, entry->length);
1146 for (i = 0; i < count; i++) {
1153 td->flags |= RPMTD_ARGV;
1158 case RPM_INT16_TYPE:
1159 case RPM_INT32_TYPE:
1160 case RPM_INT64_TYPE:
1162 td->data = xmalloc(entry->length);
1163 memcpy(td->data, entry->data, entry->length);
1165 td->data = entry->data;
1169 /* WTH? Don't mess with unknown data types... */
1174 td->type = entry->info.type;
1177 if (td->data && entry->data != td->data) {
1178 td->flags |= RPMTD_ALLOCED;
1185 * Does locale match entry in header i18n table?
1188 * The range [l,le) contains the next locale to match:
1189 * ll[_CC][.EEEEE][@dddd]
1191 * ll ISO language code (in lowercase).
1192 * CC (optional) ISO coutnry code (in uppercase).
1193 * EEEEE (optional) encoding (not really standardized).
1194 * dddd (optional) dialect.
1197 * @param td header i18n table data, NUL terminated
1198 * @param l start of locale to match
1199 * @param le end of locale to match
1200 * @return 1 on good match, 2 on weak match, 0 on no match
1202 static int headerMatchLocale(const char *td, const char *l, const char *le)
1206 /* First try a complete match. */
1207 if (strlen(td) == (le-l) && rstreqn(td, l, (le - l)))
1210 /* Next, try stripping optional dialect and matching. */
1211 for (fe = l; fe < le && *fe != '@'; fe++)
1213 if (fe < le && rstreqn(td, l, (fe - l)))
1216 /* Next, try stripping optional codeset and matching. */
1217 for (fe = l; fe < le && *fe != '.'; fe++)
1219 if (fe < le && rstreqn(td, l, (fe - l)))
1222 /* Finally, try stripping optional country code and matching. */
1223 for (fe = l; fe < le && *fe != '_'; fe++)
1225 if (fe < le && rstreqn(td, l, (fe - l)))
1232 * Return i18n string from header that matches locale.
1234 * @param entry i18n string data
1235 * @retval td tag data container
1236 * @param flags flags to control allocation
1239 static int copyI18NEntry(Header h, indexEntry entry, rpmtd td,
1240 headerGetFlags flags)
1242 const char *lang, *l, *le;
1245 td->type = RPM_STRING_TYPE;
1247 /* if no match, just return the first string */
1248 td->data = entry->data;
1250 /* XXX Drepper sez' this is the order. */
1251 if ((lang = getenv("LANGUAGE")) == NULL &&
1252 (lang = getenv("LC_ALL")) == NULL &&
1253 (lang = getenv("LC_MESSAGES")) == NULL &&
1254 (lang = getenv("LANG")) == NULL)
1257 if ((table = findEntry(h, HEADER_I18NTABLE, RPM_STRING_ARRAY_TYPE)) == NULL)
1260 for (l = lang; *l != '\0'; l = le) {
1262 char *ed, *ed_weak = NULL;
1265 while (*l && *l == ':') /* skip leading colons */
1269 for (le = l; *le && *le != ':'; le++) /* find end of this locale */
1272 /* For each entry in the header ... */
1273 for (langNum = 0, t = table->data, ed = entry->data;
1274 langNum < entry->info.count;
1275 langNum++, t += strlen(t) + 1, ed += strlen(ed) + 1) {
1277 int match = headerMatchLocale(t, l, le);
1281 } else if (match == 2) {
1292 if (flags & HEADERGET_ALLOC) {
1293 td->data = xstrdup(td->data);
1294 td->flags |= RPMTD_ALLOCED;
1301 * Retrieve tag data from header.
1303 * @retval td tag data container
1304 * @param flags flags to control retrieval
1305 * @return 1 on success, 0 on not found
1307 static int intGetTdEntry(Header h, rpmtd td, headerGetFlags flags)
1312 /* First find the tag */
1313 /* FIX: h modified by sort. */
1314 entry = findEntry(h, td->tag, RPM_NULL_TYPE);
1315 if (entry == NULL) {
1316 /* Td is zeroed above, just return... */
1320 if (flags & HEADERGET_RAW) {
1321 rc = copyTdEntry(entry, td, flags);
1323 switch (entry->info.type) {
1324 case RPM_I18NSTRING_TYPE:
1325 rc = copyI18NEntry(h, entry, td, flags);
1328 rc = copyTdEntry(entry, td, flags);
1333 /* XXX 1 on success */
1334 return ((rc == 1) ? 1 : 0);
1337 int headerGet(Header h, rpmTag tag, rpmtd td, headerGetFlags flags)
1340 headerTagTagFunction tagfunc = intGetTdEntry;
1342 if (td == NULL) return 0;
1347 if (flags & HEADERGET_EXT) {
1348 headerTagTagFunction extfunc = rpmHeaderTagFunc(tag);
1349 if (extfunc) tagfunc = extfunc;
1351 rc = tagfunc(h, td, flags);
1353 assert(tag == td->tag);
1359 static void copyData(rpmTagType type, rpm_data_t dstPtr,
1360 rpm_constdata_t srcPtr, rpm_count_t cnt, int dataLength)
1363 case RPM_STRING_ARRAY_TYPE:
1364 case RPM_I18NSTRING_TYPE:
1365 { const char ** av = (const char **) srcPtr;
1368 while (cnt-- > 0 && dataLength > 0) {
1370 if ((s = *av++) == NULL)
1374 } while (s[-1] && --dataLength > 0);
1379 memmove(dstPtr, srcPtr, dataLength);
1385 * Return (malloc'ed) copy of entry data.
1386 * @param type entry data type
1387 * @param p entry data
1388 * @param c entry item count
1389 * @retval lengthPtr no. bytes in returned data
1390 * @return (malloc'ed) copy of entry data, NULL on error
1393 grabData(rpmTagType type, rpm_constdata_t p, rpm_count_t c, int * lengthPtr)
1395 rpm_data_t data = NULL;
1398 length = dataLength(type, p, c, 0, NULL);
1400 data = xmalloc(length);
1401 copyData(type, data, p, c, length);
1405 *lengthPtr = length;
1409 static int intAddEntry(Header h, rpmtd td)
1415 /* Count must always be >= 1 for headerAddEntry. */
1419 if (hdrchkType(td->type))
1421 if (hdrchkData(td->count))
1425 data = grabData(td->type, td->data, td->count, &length);
1426 if (data == NULL || length <= 0)
1429 /* Allocate more index space if necessary */
1430 if (h->indexUsed == h->indexAlloced) {
1431 h->indexAlloced += INDEX_MALLOC_SIZE;
1432 h->index = xrealloc(h->index, h->indexAlloced * sizeof(*h->index));
1435 /* Fill in the index */
1436 entry = h->index + h->indexUsed;
1437 entry->info.tag = td->tag;
1438 entry->info.type = td->type;
1439 entry->info.count = td->count;
1440 entry->info.offset = 0;
1442 entry->length = length;
1444 if (h->indexUsed > 0 && td->tag < h->index[h->indexUsed-1].info.tag)
1445 h->flags &= ~HEADERFLAG_SORTED;
1451 static int intAppendEntry(Header h, rpmtd td)
1456 if (td->type == RPM_STRING_TYPE || td->type == RPM_I18NSTRING_TYPE) {
1457 /* we can't do this */
1461 /* Find the tag entry in the header. */
1462 entry = findEntry(h, td->tag, td->type);
1466 length = dataLength(td->type, td->data, td->count, 0, NULL);
1470 if (ENTRY_IN_REGION(entry)) {
1471 char * t = xmalloc(entry->length + length);
1472 memcpy(t, entry->data, entry->length);
1474 entry->info.offset = 0;
1476 entry->data = xrealloc(entry->data, entry->length + length);
1478 copyData(td->type, ((char *) entry->data) + entry->length,
1479 td->data, td->count, length);
1481 entry->length += length;
1483 entry->info.count += td->count;
1488 int headerPut(Header h, rpmtd td, headerPutFlags flags)
1493 if (flags & HEADERPUT_APPEND) {
1494 rc = findEntry(h, td->tag, td->type) ?
1495 intAppendEntry(h, td) :
1498 rc = intAddEntry(h, td);
1504 * Sanity check data types against tag table before putting. Assume
1505 * append on all array-types.
1507 static int headerPutType(Header h, rpmTag tag, rpmTagType reqtype,
1508 rpm_constdata_t data, rpm_count_t size)
1511 rpmTagType type = rpmTagGetType(tag);
1512 headerPutFlags flags = HEADERPUT_APPEND;
1515 /* Basic sanity checks: type must match and there must be data to put */
1516 if ((type & RPM_MASK_TYPE) != reqtype
1517 || size < 1 || data == NULL || h == NULL) {
1522 * Non-array types can't be appended to. Binary types use size
1523 * for data length, for other non-array types size must be 1.
1525 if ((type & RPM_MASK_RETURN_TYPE) != RPM_ARRAY_RETURN_TYPE) {
1526 flags = HEADERPUT_DEFAULT;
1527 if ((type & RPM_MASK_TYPE) != RPM_BIN_TYPE && size != 1) {
1535 td.type = type & RPM_MASK_TYPE;
1536 td.data = (void *) data;
1539 valid = headerPut(h, &td, flags);
1545 int headerPutString(Header h, rpmTag tag, const char *val)
1547 rpmTagType type = rpmTagGetType(tag) & RPM_MASK_TYPE;
1548 const void *sptr = NULL;
1550 /* string arrays expect char **, arrange that */
1551 if (type == RPM_STRING_ARRAY_TYPE || type == RPM_I18NSTRING_TYPE) {
1553 } else if (type == RPM_STRING_TYPE) {
1559 return headerPutType(h, tag, type, sptr, 1);
1562 int headerPutStringArray(Header h, rpmTag tag, const char **array, rpm_count_t size)
1564 return headerPutType(h, tag, RPM_STRING_ARRAY_TYPE, array, size);
1567 int headerPutChar(Header h, rpmTag tag, char *val, rpm_count_t size)
1569 return headerPutType(h, tag, RPM_CHAR_TYPE, val, size);
1572 int headerPutUint8(Header h, rpmTag tag, uint8_t *val, rpm_count_t size)
1574 return headerPutType(h, tag, RPM_INT8_TYPE, val, size);
1577 int headerPutUint16(Header h, rpmTag tag, uint16_t *val, rpm_count_t size)
1579 return headerPutType(h, tag, RPM_INT16_TYPE, val, size);
1582 int headerPutUint32(Header h, rpmTag tag, uint32_t *val, rpm_count_t size)
1584 return headerPutType(h, tag, RPM_INT32_TYPE, val, size);
1587 int headerPutUint64(Header h, rpmTag tag, uint64_t *val, rpm_count_t size)
1589 return headerPutType(h, tag, RPM_INT64_TYPE, val, size);
1592 int headerPutBin(Header h, rpmTag tag, uint8_t *val, rpm_count_t size)
1594 return headerPutType(h, tag, RPM_BIN_TYPE, val, size);
1597 int headerAddI18NString(Header h, rpmTag tag, const char * string,
1600 indexEntry table, entry;
1601 const char ** strArray;
1604 rpm_count_t i, langNum;
1607 table = findEntry(h, HEADER_I18NTABLE, RPM_STRING_ARRAY_TYPE);
1608 entry = findEntry(h, tag, RPM_I18NSTRING_TYPE);
1610 if (!table && entry)
1611 return 0; /* this shouldn't ever happen!! */
1613 if (!table && !entry) {
1614 const char * charArray[2];
1615 rpm_count_t count = 0;
1617 if (!lang || (lang[0] == 'C' && lang[1] == '\0')) {
1618 charArray[count++] = "C";
1620 charArray[count++] = "C";
1621 charArray[count++] = lang;
1625 td.tag = HEADER_I18NTABLE;
1626 td.type = RPM_STRING_ARRAY_TYPE;
1627 td.data = (void *) charArray;
1629 if (!headerPut(h, &td, HEADERPUT_DEFAULT))
1631 table = findEntry(h, HEADER_I18NTABLE, RPM_STRING_ARRAY_TYPE);
1636 if (!lang) lang = "C";
1638 { const char * l = table->data;
1639 for (langNum = 0; langNum < table->info.count; langNum++) {
1640 if (rstreq(l, lang)) break;
1645 if (langNum >= table->info.count) {
1646 length = strlen(lang) + 1;
1647 if (ENTRY_IN_REGION(table)) {
1648 char * t = xmalloc(table->length + length);
1649 memcpy(t, table->data, table->length);
1651 table->info.offset = 0;
1653 table->data = xrealloc(table->data, table->length + length);
1654 memmove(((char *)table->data) + table->length, lang, length);
1655 table->length += length;
1656 table->info.count++;
1662 strArray = xmalloc(sizeof(*strArray) * (langNum + 1));
1663 for (i = 0; i < langNum; i++)
1665 strArray[langNum] = string;
1669 td.type = RPM_I18NSTRING_TYPE;
1671 td.count = langNum + 1;
1672 rc = headerPut(h, &td, HEADERPUT_DEFAULT);
1675 } else if (langNum >= entry->info.count) {
1676 ghosts = langNum - entry->info.count;
1678 length = strlen(string) + 1 + ghosts;
1679 if (ENTRY_IN_REGION(entry)) {
1680 char * t = xmalloc(entry->length + length);
1681 memcpy(t, entry->data, entry->length);
1683 entry->info.offset = 0;
1685 entry->data = xrealloc(entry->data, entry->length + length);
1687 memset(((char *)entry->data) + entry->length, '\0', ghosts);
1688 memmove(((char *)entry->data) + entry->length + ghosts, string, strlen(string)+1);
1690 entry->length += length;
1691 entry->info.count = langNum + 1;
1693 char *b, *be, *e, *ee, *t;
1696 /* Set beginning/end pointers to previous data */
1697 b = be = e = ee = entry->data;
1698 for (i = 0; i < table->info.count; i++) {
1701 ee += strlen(ee) + 1;
1706 /* Get storage for new buffer */
1708 sn = strlen(string) + 1;
1710 length = bn + sn + en;
1711 t = buf = xmalloc(length);
1713 /* Copy values into new storage */
1716 memcpy(t, string, sn);
1721 /* Replace i18N string array */
1722 entry->length -= strlen(be) + 1;
1723 entry->length += sn;
1725 if (ENTRY_IN_REGION(entry)) {
1726 entry->info.offset = 0;
1728 entry->data = _free(entry->data);
1735 int headerMod(Header h, rpmtd td)
1742 /* First find the tag */
1743 entry = findEntry(h, td->tag, td->type);
1748 data = grabData(td->type, td->data, td->count, &length);
1749 if (data == NULL || length <= 0)
1752 /* make sure entry points to the first occurence of this tag */
1753 while (entry > h->index && (entry - 1)->info.tag == td->tag)
1756 /* free after we've grabbed the new data in case the two are intertwined;
1757 that's a bad idea but at least we won't break */
1758 oldData = entry->data;
1760 entry->info.count = td->count;
1761 entry->info.type = td->type;
1763 entry->length = length;
1765 if (ENTRY_IN_REGION(entry)) {
1766 entry->info.offset = 0;
1768 oldData = _free(oldData);
1774 * Header tag iterator data structure.
1776 struct headerIterator_s {
1777 Header h; /*!< Header being iterated. */
1778 int next_index; /*!< Next tag index. */
1781 HeaderIterator headerFreeIterator(HeaderIterator hi)
1784 hi->h = headerFree(hi->h);
1790 HeaderIterator headerInitIterator(Header h)
1792 HeaderIterator hi = xmalloc(sizeof(*hi));
1796 hi->h = headerLink(h);
1801 int headerNext(HeaderIterator hi, rpmtd td)
1805 indexEntry entry = NULL;
1811 for (slot = hi->next_index; slot < h->indexUsed; slot++) {
1812 entry = h->index + slot;
1813 if (!ENTRY_IS_REGION(entry))
1816 hi->next_index = slot;
1817 if (entry == NULL || slot >= h->indexUsed)
1822 td->tag = entry->info.tag;
1824 rc = copyTdEntry(entry, td, HEADERGET_DEFAULT);
1826 /* XXX 1 on success */
1827 return ((rc == 1) ? 1 : 0);
1831 * Duplicate a header.
1833 * @return new header instance
1835 Header headerCopy(Header h)
1837 Header nh = headerNew();
1841 hi = headerInitIterator(h);
1842 while (headerNext(hi, &td)) {
1843 if (rpmtdCount(&td) > 0) {
1844 (void) headerPut(nh, &td, HEADERPUT_DEFAULT);
1848 hi = headerFreeIterator(hi);
1850 return headerReload(nh, HEADER_IMAGE);
1853 void headerCopyTags(Header headerFrom, Header headerTo,
1854 const rpmTag * tagstocopy)
1859 if (headerFrom == headerTo)
1862 for (p = tagstocopy; *p != 0; p++) {
1863 if (headerIsEntry(headerTo, *p))
1865 if (!headerGet(headerFrom, *p, &td, HEADERGET_MINMEM))
1867 (void) headerPut(headerTo, &td, HEADERPUT_DEFAULT);
1872 unsigned int headerGetInstance(Header h)
1874 return h ? h->instance : 0;
1877 void headerSetInstance(Header h, unsigned int instance)
1879 h->instance = instance;
1882 char * headerGetAsString(Header h, rpmTag tag)
1887 if (headerGet(h, tag, &td, HEADERGET_EXT)) {
1888 if (rpmtdCount(&td) == 1) {
1889 res = rpmtdFormat(&td, RPMTD_FORMAT_STRING, NULL);
1896 const char * headerGetString(Header h, rpmTag tag)
1898 const char *res = NULL;
1901 if (headerGet(h, tag, &td, HEADERGET_MINMEM)) {
1902 if (rpmtdCount(&td) == 1) {
1903 res = rpmtdGetString(&td);
1910 uint64_t headerGetNumber(Header h, rpmTag tag)
1915 if (headerGet(h, tag, &td, HEADERGET_EXT)) {
1916 if (rpmtdCount(&td) == 1) {
1917 res = rpmtdGetNumber(&td);