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;
459 /* Sort entries by (offset,tag). */
462 /* Compute (il,dl) for all tags, including those deleted in region. */
463 drlen = ndribbles = 0;
464 for (i = 0, entry = h->index; i < h->indexUsed; i++, entry++) {
465 if (ENTRY_IS_REGION(entry)) {
466 int32_t rdl = -entry->info.offset; /* negative offset */
467 int32_t ril = rdl/sizeof(*pe);
468 int rid = entry->info.offset;
471 dl += entry->rdlen + entry->info.count;
472 /* XXX Legacy regions do not include the region tag and data. */
473 if (i == 0 && (h->flags & HEADERFLAG_LEGACY))
476 /* Skip rest of entries in region, but account for dribbles. */
477 for (; i < h->indexUsed && entry->info.offset <= rid+1; i++, entry++) {
478 if (entry->info.offset <= rid)
482 type = entry->info.type;
483 if (typeSizes[type] > 1) {
484 unsigned diff = typeSizes[type] - (dl % typeSizes[type]);
485 if (diff != typeSizes[type]) {
493 drlen += entry->length;
501 /* Ignore deleted drips. */
502 if (entry->data == NULL || entry->length <= 0)
506 type = entry->info.type;
507 if (typeSizes[type] > 1) {
508 unsigned diff = typeSizes[type] - (dl % typeSizes[type]);
509 if (diff != typeSizes[type]) {
518 /* Sanity checks on header intro. */
519 if (hdrchkTags(il) || hdrchkData(dl))
522 len = sizeof(il) + sizeof(dl) + (il * sizeof(*pe)) + dl;
528 pe = (entryInfo) &ei[2];
529 dataStart = te = (char *) (pe + il);
531 for (i = 0, entry = h->index; i < h->indexUsed; i++, entry++) {
537 if (entry->data == NULL || entry->length <= 0)
540 t = (unsigned char*)te;
541 pe->tag = htonl(entry->info.tag);
542 pe->type = htonl(entry->info.type);
543 pe->count = htonl(entry->info.count);
545 if (ENTRY_IS_REGION(entry)) {
546 int32_t rdl = -entry->info.offset; /* negative offset */
547 int32_t ril = rdl/sizeof(*pe) + ndribbles;
548 int rid = entry->info.offset;
550 src = (char *)entry->data;
551 rdlen = entry->rdlen;
553 /* XXX Legacy regions do not include the region tag and data. */
554 if (i == 0 && (h->flags & HEADERFLAG_LEGACY)) {
557 memcpy(pe+1, src, rdl);
558 memcpy(te, src + rdl, rdlen);
561 pe->offset = htonl(te - dataStart);
564 stei[2] = htonl(-rdl-entry->info.count);
566 memcpy(te, stei, entry->info.count);
567 te += entry->info.count;
569 rdlen += entry->info.count;
571 count = regionSwab(NULL, ril, 0, pe, t, NULL, 0);
577 memcpy(pe+1, src + sizeof(*pe), ((ril-1) * sizeof(*pe)));
578 memcpy(te, src + (ril * sizeof(*pe)), rdlen+entry->info.count+drlen);
581 entryInfo se = (entryInfo)src;
582 int off = ntohl(se->offset);
583 pe->offset = (off) ? htonl(te - dataStart) : htonl(off);
585 te += entry->info.count + drlen;
587 count = regionSwab(NULL, ril, 0, pe, t, NULL, 0);
588 if (count != (rdlen + entry->info.count + drlen))
592 /* Skip rest of entries in region. */
593 while (i < h->indexUsed && entry->info.offset <= rid+1) {
603 /* Ignore deleted drips. */
604 if (entry->data == NULL || entry->length <= 0)
608 type = entry->info.type;
609 if (typeSizes[type] > 1) {
611 diff = typeSizes[type] - ((te - dataStart) % typeSizes[type]);
612 if (diff != typeSizes[type]) {
618 pe->offset = htonl(te - dataStart);
620 /* copy data w/ endian conversions */
621 switch (entry->info.type) {
623 count = entry->info.count;
626 *((uint64_t *)te) = htonll(*((uint64_t *)src));
627 te += sizeof(uint64_t);
628 src += sizeof(uint64_t);
633 count = entry->info.count;
636 *((int32_t *)te) = htonl(*((int32_t *)src));
637 te += sizeof(int32_t);
638 src += sizeof(int32_t);
643 count = entry->info.count;
646 *((int16_t *)te) = htons(*((int16_t *)src));
647 te += sizeof(int16_t);
648 src += sizeof(int16_t);
653 memcpy(te, entry->data, entry->length);
660 /* Insure that there are no memcpy underruns/overruns. */
661 if (((char *)pe) != dataStart)
663 if ((((char *)ei)+len) != te)
669 h->flags &= ~HEADERFLAG_SORTED;
679 void * headerUnload(Header h)
682 void * uh = doHeaderUnload(h, &length);
687 * Find matching (tag,type) entry in header.
689 * @param tag entry tag
690 * @param type entry type
691 * @return header entry
694 indexEntry findEntry(Header h, rpmTag tag, rpmTagType type)
696 indexEntry entry, entry2, last;
697 struct indexEntry_s key;
699 if (h == NULL) return NULL;
700 if (!(h->flags & HEADERFLAG_SORTED)) headerSort(h);
705 bsearch(&key, h->index, h->indexUsed, sizeof(*h->index), indexCmp);
709 if (type == RPM_NULL_TYPE)
713 while (entry->info.tag == tag && entry->info.type != type &&
714 entry > h->index) entry--;
716 if (entry->info.tag == tag && entry->info.type == type)
719 last = h->index + h->indexUsed;
720 /* FIX: entry2 = entry. Code looks bogus as well. */
721 while (entry2->info.tag == tag && entry2->info.type != type &&
722 entry2 < last) entry2++;
724 if (entry->info.tag == tag && entry->info.type == type)
730 int headerDel(Header h, rpmTag tag)
732 indexEntry last = h->index + h->indexUsed;
733 indexEntry entry, first;
736 entry = findEntry(h, tag, RPM_NULL_TYPE);
737 if (!entry) return 1;
739 /* Make sure entry points to the first occurence of this tag. */
740 while (entry > h->index && (entry - 1)->info.tag == tag)
743 /* Free data for tags being removed. */
744 for (first = entry; first < last; first++) {
746 if (first->info.tag != tag)
751 if (ENTRY_IN_REGION(first))
756 ne = (first - entry);
761 memmove(entry, first, (ne * sizeof(*entry)));
767 Header headerLoad(void * uh)
769 int32_t * ei = (int32_t *) uh;
770 int32_t il = ntohl(ei[0]); /* index length */
771 int32_t dl = ntohl(ei[1]); /* data length */
772 size_t pvlen = sizeof(il) + sizeof(dl) +
773 (il * sizeof(struct entryInfo_s)) + dl;
777 unsigned char * dataStart;
778 unsigned char * dataEnd;
782 /* Sanity checks on header intro. */
783 if (hdrchkTags(il) || hdrchkData(dl))
787 pe = (entryInfo) &ei[2];
788 dataStart = (unsigned char *) (pe + il);
789 dataEnd = dataStart + dl;
791 h = xcalloc(1, sizeof(*h));
793 h->indexAlloced = il + 1;
796 h->index = xcalloc(h->indexAlloced, sizeof(*h->index));
797 h->flags |= HEADERFLAG_SORTED;
802 if (!(htonl(pe->tag) < HEADER_I18NTABLE)) {
803 h->flags |= HEADERFLAG_LEGACY;
804 entry->info.type = REGION_TAG_TYPE;
805 entry->info.tag = HEADER_IMAGE;
806 entry->info.count = REGION_TAG_COUNT;
807 entry->info.offset = ((unsigned char *)pe - dataStart); /* negative offset */
810 entry->length = pvlen - sizeof(il) - sizeof(dl);
811 rdlen = regionSwab(entry+1, il, 0, pe, dataStart, dataEnd, entry->info.offset);
814 entry->rdlen = rdlen;
820 h->flags &= ~HEADERFLAG_LEGACY;
822 entry->info.type = htonl(pe->type);
823 entry->info.count = htonl(pe->count);
825 if (hdrchkType(entry->info.type))
827 if (hdrchkTags(entry->info.count))
830 { int off = ntohl(pe->offset);
835 size_t nb = REGION_TAG_COUNT;
837 /* XXX Hmm, why the copy? */
838 memcpy(&stei, dataStart + off, nb);
839 rdl = -ntohl(stei[2]); /* negative offset */
840 ril = rdl/sizeof(*pe);
841 if (hdrchkTags(ril) || hdrchkData(rdl))
843 entry->info.tag = htonl(pe->tag);
846 rdl = (ril * sizeof(struct entryInfo_s));
847 entry->info.tag = HEADER_IMAGE;
850 entry->info.offset = -rdl; /* negative offset */
853 entry->length = pvlen - sizeof(il) - sizeof(dl);
854 rdlen = regionSwab(entry+1, ril-1, 0, pe+1, dataStart, dataEnd, entry->info.offset);
857 entry->rdlen = rdlen;
859 if (ril < h->indexUsed) {
860 indexEntry newEntry = entry + ril;
861 int ne = (h->indexUsed - ril);
862 int rid = entry->info.offset+1;
865 /* Load dribble entries from region. */
866 rc = regionSwab(newEntry, ne, 0, pe+ril, dataStart, dataEnd, rid);
871 { indexEntry firstEntry = newEntry;
872 int save = h->indexUsed;
875 /* Dribble entries replace duplicate region entries. */
877 for (j = 0; j < ne; j++, newEntry++) {
878 (void) headerDel(h, newEntry->info.tag);
879 if (newEntry->info.tag == RPMTAG_BASENAMES)
880 (void) headerDel(h, RPMTAG_OLDFILENAMES);
883 /* If any duplicate entries were replaced, move new entries down. */
884 if (h->indexUsed < (save - ne)) {
885 memmove(h->index + h->indexUsed, firstEntry,
886 (ne * sizeof(*entry)));
893 h->flags &= ~HEADERFLAG_SORTED;
895 h->flags |= HEADERFLAG_ALLOCATED;
901 h->index = _free(h->index);
907 Header headerReload(Header h, rpmTag tag)
911 void * uh = doHeaderUnload(h, &length);
921 if (ENTRY_IS_REGION(nh->index)) {
922 if (tag == HEADER_SIGNATURES || tag == HEADER_IMMUTABLE)
923 nh->index[0].info.tag = tag;
928 Header headerCopyLoad(const void * uh)
930 int32_t * ei = (int32_t *) uh;
931 int32_t il = ntohl(ei[0]); /* index length */
932 int32_t dl = ntohl(ei[1]); /* data length */
933 size_t pvlen = sizeof(il) + sizeof(dl) +
934 (il * sizeof(struct entryInfo_s)) + dl;
938 /* Sanity checks on header intro. */
939 if (!(hdrchkTags(il) || hdrchkData(dl)) && pvlen < headerMaxbytes) {
940 nuh = memcpy(xmalloc(pvlen), uh, pvlen);
941 if ((h = headerLoad(nuh)) == NULL)
948 * Read (and load) header from file handle.
949 * @param fd file handle
950 * @param magicp read (and verify) 8 bytes of (magic, 0)?
951 * @return header (or NULL on error)
953 Header headerRead(FD_t fd, enum hMagic magicp)
965 memset(block, 0, sizeof(block));
967 if (magicp == HEADER_MAGIC_YES)
971 if (timedRead(fd, (char *)block, i*sizeof(*block)) != (i * sizeof(*block)))
976 if (magicp == HEADER_MAGIC_YES) {
978 if (memcmp(&magic, rpm_header_magic, sizeof(magic)))
980 reserved = block[i++];
983 il = ntohl(block[i]); i++;
984 dl = ntohl(block[i]); i++;
986 len = sizeof(il) + sizeof(dl) + (il * sizeof(struct entryInfo_s)) + dl;
988 /* Sanity checks on header intro. */
989 if (hdrchkTags(il) || hdrchkData(dl) || len > headerMaxbytes)
995 len -= sizeof(il) + sizeof(dl);
998 if (timedRead(fd, (char *)&ei[2], len) != len)
1004 if (h == NULL && ei != NULL) {
1010 int headerWrite(FD_t fd, Header h, enum hMagic magicp)
1018 uh = doHeaderUnload(h, &length);
1022 case HEADER_MAGIC_YES:
1023 nb = Fwrite(rpm_header_magic, sizeof(uint8_t), sizeof(rpm_header_magic), fd);
1024 if (nb != sizeof(rpm_header_magic))
1027 case HEADER_MAGIC_NO:
1031 nb = Fwrite(uh, sizeof(char), length, fd);
1035 return (nb == length ? 0 : 1);
1038 int headerIsEntry(Header h, rpmTag tag)
1040 /* FIX: h modified by sort. */
1041 return (findEntry(h, tag, RPM_NULL_TYPE) ? 1 : 0);
1046 * Retrieve data from header entry.
1047 * Relevant flags (others are ignored), if neither is set allocation
1048 * behavior depends on data type(!)
1049 * HEADERGET_MINMEM: return pointers to header memory
1050 * HEADERGET_ALLOC: always return malloced memory, overrides MINMEM
1052 * @todo Permit retrieval of regions other than HEADER_IMUTABLE.
1053 * @param entry header entry
1054 * @param td tag data container
1055 * @param minMem string pointers refer to header memory?
1056 * @param flags flags to control memory allocation
1057 * @return 1 on success, otherwise error.
1059 static int copyTdEntry(const indexEntry entry, rpmtd td, headerGetFlags flags)
1061 rpm_count_t count = entry->info.count;
1062 int rc = 1; /* XXX 1 on success. */
1063 /* ALLOC overrides MINMEM */
1064 int allocMem = flags & HEADERGET_ALLOC;
1065 int minMem = allocMem ? 0 : flags & HEADERGET_MINMEM;
1066 int argvArray = (flags & HEADERGET_ARGV) ? 1 : 0;
1069 td->flags = RPMTD_IMMUTABLE;
1070 switch (entry->info.type) {
1073 * XXX This only works for
1074 * XXX "sealed" HEADER_IMMUTABLE/HEADER_SIGNATURES/HEADER_IMAGE.
1075 * XXX This will *not* work for unsealed legacy HEADER_IMAGE (i.e.
1076 * XXX a legacy header freshly read, but not yet unloaded to the rpmdb).
1078 if (ENTRY_IS_REGION(entry)) {
1079 int32_t * ei = ((int32_t *)entry->data) - 2;
1080 entryInfo pe = (entryInfo) (ei + 2);
1081 unsigned char * dataStart = (unsigned char *) (pe + ntohl(ei[0]));
1082 int32_t rdl = -entry->info.offset; /* negative offset */
1083 int32_t ril = rdl/sizeof(*pe);
1086 count = 2 * sizeof(*ei) + (ril * sizeof(*pe)) + rdl;
1087 if (entry->info.tag == HEADER_IMAGE) {
1091 count += REGION_TAG_COUNT;
1092 rdl += REGION_TAG_COUNT;
1095 td->data = xmalloc(count);
1096 ei = (int32_t *) td->data;
1100 pe = (entryInfo) memcpy(ei + 2, pe, (ril * sizeof(*pe)));
1102 dataStart = (unsigned char *) memcpy(pe + ril, dataStart, rdl);
1104 rc = regionSwab(NULL, ril, 0, pe, dataStart, dataStart + rdl, 0);
1105 /* don't return data on failure */
1107 td->data = _free(td->data);
1109 /* XXX 1 on success. */
1110 rc = (rc < 0) ? 0 : 1;
1112 count = entry->length;
1114 ? memcpy(xmalloc(count), entry->data, count)
1118 case RPM_STRING_TYPE:
1120 td->data = allocMem ? xstrdup(entry->data) : entry->data;
1123 case RPM_STRING_ARRAY_TYPE:
1124 case RPM_I18NSTRING_TYPE:
1125 { const char ** ptrEntry;
1126 int tableSize = (count + argvArray) * sizeof(char *);
1131 td->data = xmalloc(tableSize);
1132 ptrEntry = (const char **) td->data;
1135 t = xmalloc(tableSize + entry->length);
1136 td->data = (void *)t;
1137 ptrEntry = (const char **) td->data;
1139 memcpy(t, entry->data, entry->length);
1141 for (i = 0; i < count; i++) {
1148 td->flags |= RPMTD_ARGV;
1153 case RPM_INT16_TYPE:
1154 case RPM_INT32_TYPE:
1155 case RPM_INT64_TYPE:
1157 td->data = xmalloc(entry->length);
1158 memcpy(td->data, entry->data, entry->length);
1160 td->data = entry->data;
1164 /* WTH? Don't mess with unknown data types... */
1169 td->type = entry->info.type;
1172 if (td->data && entry->data != td->data) {
1173 td->flags |= RPMTD_ALLOCED;
1180 * Does locale match entry in header i18n table?
1183 * The range [l,le) contains the next locale to match:
1184 * ll[_CC][.EEEEE][@dddd]
1186 * ll ISO language code (in lowercase).
1187 * CC (optional) ISO coutnry code (in uppercase).
1188 * EEEEE (optional) encoding (not really standardized).
1189 * dddd (optional) dialect.
1192 * @param td header i18n table data, NUL terminated
1193 * @param l start of locale to match
1194 * @param le end of locale to match
1195 * @return 1 on good match, 2 on weak match, 0 on no match
1197 static int headerMatchLocale(const char *td, const char *l, const char *le)
1201 /* First try a complete match. */
1202 if (strlen(td) == (le-l) && rstreqn(td, l, (le - l)))
1205 /* Next, try stripping optional dialect and matching. */
1206 for (fe = l; fe < le && *fe != '@'; fe++)
1208 if (fe < le && rstreqn(td, l, (fe - l)))
1211 /* Next, try stripping optional codeset and matching. */
1212 for (fe = l; fe < le && *fe != '.'; fe++)
1214 if (fe < le && rstreqn(td, l, (fe - l)))
1217 /* Finally, try stripping optional country code and matching. */
1218 for (fe = l; fe < le && *fe != '_'; fe++)
1220 if (fe < le && rstreqn(td, l, (fe - l)))
1227 * Return i18n string from header that matches locale.
1229 * @param entry i18n string data
1230 * @retval td tag data container
1231 * @param flags flags to control allocation
1234 static int copyI18NEntry(Header h, indexEntry entry, rpmtd td,
1235 headerGetFlags flags)
1237 const char *lang, *l, *le;
1240 td->type = RPM_STRING_TYPE;
1242 /* if no match, just return the first string */
1243 td->data = entry->data;
1245 /* XXX Drepper sez' this is the order. */
1246 if ((lang = getenv("LANGUAGE")) == NULL &&
1247 (lang = getenv("LC_ALL")) == NULL &&
1248 (lang = getenv("LC_MESSAGES")) == NULL &&
1249 (lang = getenv("LANG")) == NULL)
1252 if ((table = findEntry(h, HEADER_I18NTABLE, RPM_STRING_ARRAY_TYPE)) == NULL)
1255 for (l = lang; *l != '\0'; l = le) {
1257 char *ed, *ed_weak = NULL;
1260 while (*l && *l == ':') /* skip leading colons */
1264 for (le = l; *le && *le != ':'; le++) /* find end of this locale */
1267 /* For each entry in the header ... */
1268 for (langNum = 0, t = table->data, ed = entry->data;
1269 langNum < entry->info.count;
1270 langNum++, t += strlen(t) + 1, ed += strlen(ed) + 1) {
1272 int match = headerMatchLocale(t, l, le);
1276 } else if (match == 2) {
1287 if (flags & HEADERGET_ALLOC) {
1288 td->data = xstrdup(td->data);
1289 td->flags |= RPMTD_ALLOCED;
1296 * Retrieve tag data from header.
1298 * @retval td tag data container
1299 * @param flags flags to control retrieval
1300 * @return 1 on success, 0 on not found
1302 static int intGetTdEntry(Header h, rpmtd td, headerGetFlags flags)
1307 /* First find the tag */
1308 /* FIX: h modified by sort. */
1309 entry = findEntry(h, td->tag, RPM_NULL_TYPE);
1310 if (entry == NULL) {
1311 /* Td is zeroed above, just return... */
1315 if (flags & HEADERGET_RAW) {
1316 rc = copyTdEntry(entry, td, flags);
1318 switch (entry->info.type) {
1319 case RPM_I18NSTRING_TYPE:
1320 rc = copyI18NEntry(h, entry, td, flags);
1323 rc = copyTdEntry(entry, td, flags);
1328 /* XXX 1 on success */
1329 return ((rc == 1) ? 1 : 0);
1332 int headerGet(Header h, rpmTag tag, rpmtd td, headerGetFlags flags)
1335 headerTagTagFunction tagfunc = intGetTdEntry;
1337 if (td == NULL) return 0;
1342 if (flags & HEADERGET_EXT) {
1343 headerTagTagFunction extfunc = rpmHeaderTagFunc(tag);
1344 if (extfunc) tagfunc = extfunc;
1346 rc = tagfunc(h, td, flags);
1348 assert(tag == td->tag);
1354 static void copyData(rpmTagType type, rpm_data_t dstPtr,
1355 rpm_constdata_t srcPtr, rpm_count_t cnt, int dataLength)
1358 case RPM_STRING_ARRAY_TYPE:
1359 case RPM_I18NSTRING_TYPE:
1360 { const char ** av = (const char **) srcPtr;
1363 while (cnt-- > 0 && dataLength > 0) {
1365 if ((s = *av++) == NULL)
1369 } while (s[-1] && --dataLength > 0);
1374 memmove(dstPtr, srcPtr, dataLength);
1380 * Return (malloc'ed) copy of entry data.
1381 * @param type entry data type
1382 * @param p entry data
1383 * @param c entry item count
1384 * @retval lengthPtr no. bytes in returned data
1385 * @return (malloc'ed) copy of entry data, NULL on error
1388 grabData(rpmTagType type, rpm_constdata_t p, rpm_count_t c, int * lengthPtr)
1390 rpm_data_t data = NULL;
1393 length = dataLength(type, p, c, 0, NULL);
1395 data = xmalloc(length);
1396 copyData(type, data, p, c, length);
1400 *lengthPtr = length;
1404 static int intAddEntry(Header h, rpmtd td)
1410 /* Count must always be >= 1 for headerAddEntry. */
1414 if (hdrchkType(td->type))
1416 if (hdrchkData(td->count))
1420 data = grabData(td->type, td->data, td->count, &length);
1421 if (data == NULL || length <= 0)
1424 /* Allocate more index space if necessary */
1425 if (h->indexUsed == h->indexAlloced) {
1426 h->indexAlloced += INDEX_MALLOC_SIZE;
1427 h->index = xrealloc(h->index, h->indexAlloced * sizeof(*h->index));
1430 /* Fill in the index */
1431 entry = h->index + h->indexUsed;
1432 entry->info.tag = td->tag;
1433 entry->info.type = td->type;
1434 entry->info.count = td->count;
1435 entry->info.offset = 0;
1437 entry->length = length;
1439 if (h->indexUsed > 0 && td->tag < h->index[h->indexUsed-1].info.tag)
1440 h->flags &= ~HEADERFLAG_SORTED;
1446 static int intAppendEntry(Header h, rpmtd td)
1451 if (td->type == RPM_STRING_TYPE || td->type == RPM_I18NSTRING_TYPE) {
1452 /* we can't do this */
1456 /* Find the tag entry in the header. */
1457 entry = findEntry(h, td->tag, td->type);
1461 length = dataLength(td->type, td->data, td->count, 0, NULL);
1465 if (ENTRY_IN_REGION(entry)) {
1466 char * t = xmalloc(entry->length + length);
1467 memcpy(t, entry->data, entry->length);
1469 entry->info.offset = 0;
1471 entry->data = xrealloc(entry->data, entry->length + length);
1473 copyData(td->type, ((char *) entry->data) + entry->length,
1474 td->data, td->count, length);
1476 entry->length += length;
1478 entry->info.count += td->count;
1483 int headerPut(Header h, rpmtd td, headerPutFlags flags)
1488 if (flags & HEADERPUT_APPEND) {
1489 rc = findEntry(h, td->tag, td->type) ?
1490 intAppendEntry(h, td) :
1493 rc = intAddEntry(h, td);
1499 * Sanity check data types against tag table before putting. Assume
1500 * append on all array-types.
1502 static int headerPutType(Header h, rpmTag tag, rpmTagType reqtype,
1503 rpm_constdata_t data, rpm_count_t size)
1506 rpmTagType type = rpmTagGetType(tag);
1507 headerPutFlags flags = HEADERPUT_APPEND;
1510 /* Basic sanity checks: type must match and there must be data to put */
1511 if ((type & RPM_MASK_TYPE) != reqtype
1512 || size < 1 || data == NULL || h == NULL) {
1517 * Non-array types can't be appended to. Binary types use size
1518 * for data length, for other non-array types size must be 1.
1520 if ((type & RPM_MASK_RETURN_TYPE) != RPM_ARRAY_RETURN_TYPE) {
1521 flags = HEADERPUT_DEFAULT;
1522 if ((type & RPM_MASK_TYPE) != RPM_BIN_TYPE && size != 1) {
1530 td.type = type & RPM_MASK_TYPE;
1531 td.data = (void *) data;
1534 valid = headerPut(h, &td, flags);
1540 int headerPutString(Header h, rpmTag tag, const char *val)
1542 rpmTagType type = rpmTagGetType(tag) & RPM_MASK_TYPE;
1543 const void *sptr = NULL;
1545 /* string arrays expect char **, arrange that */
1546 if (type == RPM_STRING_ARRAY_TYPE || type == RPM_I18NSTRING_TYPE) {
1548 } else if (type == RPM_STRING_TYPE) {
1554 return headerPutType(h, tag, type, sptr, 1);
1557 int headerPutStringArray(Header h, rpmTag tag, const char **array, rpm_count_t size)
1559 return headerPutType(h, tag, RPM_STRING_ARRAY_TYPE, array, size);
1562 int headerPutChar(Header h, rpmTag tag, char *val, rpm_count_t size)
1564 return headerPutType(h, tag, RPM_CHAR_TYPE, val, size);
1567 int headerPutUint8(Header h, rpmTag tag, uint8_t *val, rpm_count_t size)
1569 return headerPutType(h, tag, RPM_INT8_TYPE, val, size);
1572 int headerPutUint16(Header h, rpmTag tag, uint16_t *val, rpm_count_t size)
1574 return headerPutType(h, tag, RPM_INT16_TYPE, val, size);
1577 int headerPutUint32(Header h, rpmTag tag, uint32_t *val, rpm_count_t size)
1579 return headerPutType(h, tag, RPM_INT32_TYPE, val, size);
1582 int headerPutUint64(Header h, rpmTag tag, uint64_t *val, rpm_count_t size)
1584 return headerPutType(h, tag, RPM_INT64_TYPE, val, size);
1587 int headerPutBin(Header h, rpmTag tag, uint8_t *val, rpm_count_t size)
1589 return headerPutType(h, tag, RPM_BIN_TYPE, val, size);
1592 int headerAddI18NString(Header h, rpmTag tag, const char * string,
1595 indexEntry table, entry;
1596 const char ** strArray;
1599 rpm_count_t i, langNum;
1602 table = findEntry(h, HEADER_I18NTABLE, RPM_STRING_ARRAY_TYPE);
1603 entry = findEntry(h, tag, RPM_I18NSTRING_TYPE);
1605 if (!table && entry)
1606 return 0; /* this shouldn't ever happen!! */
1608 if (!table && !entry) {
1609 const char * charArray[2];
1610 rpm_count_t count = 0;
1612 if (!lang || (lang[0] == 'C' && lang[1] == '\0')) {
1613 charArray[count++] = "C";
1615 charArray[count++] = "C";
1616 charArray[count++] = lang;
1620 td.tag = HEADER_I18NTABLE;
1621 td.type = RPM_STRING_ARRAY_TYPE;
1622 td.data = (void *) charArray;
1624 if (!headerPut(h, &td, HEADERPUT_DEFAULT))
1626 table = findEntry(h, HEADER_I18NTABLE, RPM_STRING_ARRAY_TYPE);
1631 if (!lang) lang = "C";
1633 { const char * l = table->data;
1634 for (langNum = 0; langNum < table->info.count; langNum++) {
1635 if (rstreq(l, lang)) break;
1640 if (langNum >= table->info.count) {
1641 length = strlen(lang) + 1;
1642 if (ENTRY_IN_REGION(table)) {
1643 char * t = xmalloc(table->length + length);
1644 memcpy(t, table->data, table->length);
1646 table->info.offset = 0;
1648 table->data = xrealloc(table->data, table->length + length);
1649 memmove(((char *)table->data) + table->length, lang, length);
1650 table->length += length;
1651 table->info.count++;
1657 strArray = xmalloc(sizeof(*strArray) * (langNum + 1));
1658 for (i = 0; i < langNum; i++)
1660 strArray[langNum] = string;
1664 td.type = RPM_I18NSTRING_TYPE;
1666 td.count = langNum + 1;
1667 rc = headerPut(h, &td, HEADERPUT_DEFAULT);
1670 } else if (langNum >= entry->info.count) {
1671 ghosts = langNum - entry->info.count;
1673 length = strlen(string) + 1 + ghosts;
1674 if (ENTRY_IN_REGION(entry)) {
1675 char * t = xmalloc(entry->length + length);
1676 memcpy(t, entry->data, entry->length);
1678 entry->info.offset = 0;
1680 entry->data = xrealloc(entry->data, entry->length + length);
1682 memset(((char *)entry->data) + entry->length, '\0', ghosts);
1683 memmove(((char *)entry->data) + entry->length + ghosts, string, strlen(string)+1);
1685 entry->length += length;
1686 entry->info.count = langNum + 1;
1688 char *b, *be, *e, *ee, *t;
1691 /* Set beginning/end pointers to previous data */
1692 b = be = e = ee = entry->data;
1693 for (i = 0; i < table->info.count; i++) {
1696 ee += strlen(ee) + 1;
1701 /* Get storage for new buffer */
1703 sn = strlen(string) + 1;
1705 length = bn + sn + en;
1706 t = buf = xmalloc(length);
1708 /* Copy values into new storage */
1711 memcpy(t, string, sn);
1716 /* Replace i18N string array */
1717 entry->length -= strlen(be) + 1;
1718 entry->length += sn;
1720 if (ENTRY_IN_REGION(entry)) {
1721 entry->info.offset = 0;
1723 entry->data = _free(entry->data);
1730 int headerMod(Header h, rpmtd td)
1737 /* First find the tag */
1738 entry = findEntry(h, td->tag, td->type);
1743 data = grabData(td->type, td->data, td->count, &length);
1744 if (data == NULL || length <= 0)
1747 /* make sure entry points to the first occurence of this tag */
1748 while (entry > h->index && (entry - 1)->info.tag == td->tag)
1751 /* free after we've grabbed the new data in case the two are intertwined;
1752 that's a bad idea but at least we won't break */
1753 oldData = entry->data;
1755 entry->info.count = td->count;
1756 entry->info.type = td->type;
1758 entry->length = length;
1760 if (ENTRY_IN_REGION(entry)) {
1761 entry->info.offset = 0;
1763 oldData = _free(oldData);
1769 * Header tag iterator data structure.
1771 struct headerIterator_s {
1772 Header h; /*!< Header being iterated. */
1773 int next_index; /*!< Next tag index. */
1776 HeaderIterator headerFreeIterator(HeaderIterator hi)
1779 hi->h = headerFree(hi->h);
1785 HeaderIterator headerInitIterator(Header h)
1787 HeaderIterator hi = xmalloc(sizeof(*hi));
1791 hi->h = headerLink(h);
1796 int headerNext(HeaderIterator hi, rpmtd td)
1800 indexEntry entry = NULL;
1806 for (slot = hi->next_index; slot < h->indexUsed; slot++) {
1807 entry = h->index + slot;
1808 if (!ENTRY_IS_REGION(entry))
1811 hi->next_index = slot;
1812 if (entry == NULL || slot >= h->indexUsed)
1817 td->tag = entry->info.tag;
1819 rc = copyTdEntry(entry, td, HEADERGET_DEFAULT);
1821 /* XXX 1 on success */
1822 return ((rc == 1) ? 1 : 0);
1826 * Duplicate a header.
1828 * @return new header instance
1830 Header headerCopy(Header h)
1832 Header nh = headerNew();
1836 hi = headerInitIterator(h);
1837 while (headerNext(hi, &td)) {
1838 if (rpmtdCount(&td) > 0) {
1839 (void) headerPut(nh, &td, HEADERPUT_DEFAULT);
1843 hi = headerFreeIterator(hi);
1845 return headerReload(nh, HEADER_IMAGE);
1848 void headerCopyTags(Header headerFrom, Header headerTo,
1849 const rpmTag * tagstocopy)
1854 if (headerFrom == headerTo)
1857 for (p = tagstocopy; *p != 0; p++) {
1858 if (headerIsEntry(headerTo, *p))
1860 if (!headerGet(headerFrom, *p, &td, HEADERGET_MINMEM))
1862 (void) headerPut(headerTo, &td, HEADERPUT_DEFAULT);
1867 unsigned int headerGetInstance(Header h)
1869 return h ? h->instance : 0;
1872 void headerSetInstance(Header h, unsigned int instance)
1874 h->instance = instance;
1877 char * headerGetAsString(Header h, rpmTag tag)
1882 if (headerGet(h, tag, &td, HEADERGET_EXT)) {
1883 if (rpmtdCount(&td) == 1) {
1884 res = rpmtdFormat(&td, RPMTD_FORMAT_STRING, NULL);
1891 const char * headerGetString(Header h, rpmTag tag)
1893 const char *res = NULL;
1896 if (headerGet(h, tag, &td, HEADERGET_MINMEM)) {
1897 if (rpmtdCount(&td) == 1) {
1898 res = rpmtdGetString(&td);
1905 uint64_t headerGetNumber(Header h, rpmTag tag)
1910 if (headerGet(h, tag, &td, HEADERGET_EXT)) {
1911 if (rpmtdCount(&td) == 1) {
1912 res = rpmtdGetNumber(&td);