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/rpmtag.h>
14 #include <rpm/rpmstring.h>
15 #include "lib/header_internal.h"
23 static unsigned char const 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 -1, /*!< RPM_INT64_TYPE */
59 -1, /*!< RPM_STRING_TYPE */
60 1, /*!< RPM_BIN_TYPE */
61 -1, /*!< RPM_STRING_ARRAY_TYPE */
62 -1, /*!< RPM_I18NSTRING_TYPE */
71 /* dumb macro to avoid 50 copies of this code while converting... */
81 * Maximum no. of bytes permitted in a header.
83 static const size_t headerMaxbytes = (32*1024*1024);
86 * HEADER_EXT_TAG format function prototype.
87 * This is allowed to fail, which indicates the tag doesn't exist.
90 * @retval td tag data container
91 * @return 0 on success
93 typedef int (*headerTagTagFunction) (Header h, rpmtd td);
95 extern void *rpmHeaderTagFunc(rpmTag tag);
97 Header headerLink(Header h)
99 if (h == NULL) return NULL;
103 fprintf(stderr, "--> h %p ++ %d at %s:%u\n", h, h->nrefs, __FILE__, __LINE__);
108 Header headerUnlink(Header h)
110 if (h == NULL) return NULL;
112 fprintf(stderr, "--> h %p -- %d at %s:%u\n", h, h->nrefs, __FILE__, __LINE__);
117 Header headerFree(Header h)
119 (void) headerUnlink(h);
121 if (h == NULL || h->nrefs > 0)
122 return NULL; /* XXX return previous header? */
125 indexEntry entry = h->index;
127 for (i = 0; i < h->indexUsed; i++, entry++) {
128 if ((h->flags & HEADERFLAG_ALLOCATED) && ENTRY_IS_REGION(entry)) {
129 if (entry->length > 0) {
130 int32_t * ei = entry->data;
131 if ((ei - 2) == h->blob) h->blob = _free(h->blob);
134 } else if (!ENTRY_IN_REGION(entry)) {
135 entry->data = _free(entry->data);
139 h->index = _free(h->index);
146 Header headerNew(void)
148 Header h = xcalloc(1, sizeof(*h));
151 h->indexAlloced = INDEX_MALLOC_SIZE;
153 h->flags |= HEADERFLAG_SORTED;
155 h->index = (h->indexAlloced
156 ? xcalloc(h->indexAlloced, sizeof(*h->index))
160 return headerLink(h);
163 int headerVerifyInfo(int il, int dl, const void * pev, void * iv, int negate)
165 entryInfo pe = (entryInfo) pev;
169 for (i = 0; i < il; i++) {
170 info->tag = ntohl(pe[i].tag);
171 info->type = ntohl(pe[i].type);
172 info->offset = ntohl(pe[i].offset);
174 info->offset = -info->offset;
175 info->count = ntohl(pe[i].count);
177 if (hdrchkType(info->type))
179 if (hdrchkAlign(info->type, info->offset))
181 if (!negate && hdrchkRange(dl, info->offset))
183 if (hdrchkData(info->count))
192 static int indexCmp(const void * avp, const void * bvp)
194 indexEntry ap = (indexEntry) avp, bp = (indexEntry) bvp;
195 return (ap->info.tag - bp->info.tag);
198 void headerSort(Header h)
200 if (!(h->flags & HEADERFLAG_SORTED)) {
201 qsort(h->index, h->indexUsed, sizeof(*h->index), indexCmp);
202 h->flags |= HEADERFLAG_SORTED;
208 static int offsetCmp(const void * avp, const void * bvp)
210 indexEntry ap = (indexEntry) avp, bp = (indexEntry) bvp;
211 int rc = (ap->info.offset - bp->info.offset);
214 /* Within a region, entries sort by address. Added drips sort by tag. */
215 if (ap->info.offset < 0)
216 rc = (((char *)ap->data) - ((char *)bp->data));
218 rc = (ap->info.tag - bp->info.tag);
224 * Restore tags in header to original ordering.
227 void headerUnsort(Header h)
229 qsort(h->index, h->indexUsed, sizeof(*h->index), offsetCmp);
232 unsigned headerSizeof(Header h, enum hMagic magicp)
235 unsigned int size = 0;
236 unsigned int pad = 0;
245 case HEADER_MAGIC_YES:
246 size += sizeof(header_magic);
248 case HEADER_MAGIC_NO:
252 size += 2 * sizeof(int32_t); /* count of index entries */
254 for (i = 0, entry = h->index; i < h->indexUsed; i++, entry++) {
258 /* Regions go in as is ... */
259 if (ENTRY_IS_REGION(entry)) {
260 size += entry->length;
261 /* XXX Legacy regions do not include the region tag and data. */
262 if (i == 0 && (h->flags & HEADERFLAG_LEGACY))
263 size += sizeof(struct entryInfo_s) + entry->info.count;
267 /* ... and region elements are skipped. */
268 if (entry->info.offset < 0)
272 type = entry->info.type;
273 if (typeSizes[type] > 1) {
274 diff = typeSizes[type] - (size % typeSizes[type]);
275 if (diff != typeSizes[type]) {
281 size += sizeof(struct entryInfo_s) + entry->length;
288 * Return length of entry data.
289 * @param type entry data type
290 * @param p entry data
291 * @param count entry item count
292 * @param onDisk data is concatenated strings (with NUL's))?
293 * @param pend pointer to end of data (or NULL)
294 * @return no. bytes in data, -1 on failure
296 static int dataLength(rpmTagType type, rpm_constdata_t p, rpm_count_t count,
297 int onDisk, rpm_constdata_t pend)
299 const unsigned char * s = p;
300 const unsigned char * se = pend;
304 case RPM_STRING_TYPE:
312 length++; /* count nul terminator too. */
315 case RPM_STRING_ARRAY_TYPE:
316 case RPM_I18NSTRING_TYPE:
317 /* These are like RPM_STRING_TYPE, except they're *always* an array */
318 /* Compute sum of length of all strings, including nul terminators */
322 length++; /* count nul terminator too */
330 const char ** av = (const char **)p;
332 /* add one for null termination */
333 length += strlen(*av++) + 1;
339 if (typeSizes[type] == -1)
341 length = typeSizes[(type & 0xf)] * count;
342 if (length < 0 || (se && (s + length) > se))
351 * Swap int32_t and int16_t arrays within header region.
353 * This code is way more twisty than I would like.
355 * A bug with RPM_I18NSTRING_TYPE in rpm-2.5.x (fixed in August 1998)
356 * causes the offset and length of elements in a header region to disagree
357 * regarding the total length of the region data.
359 * The "fix" is to compute the size using both offset and length and
360 * return the larger of the two numbers as the size of the region.
361 * Kinda like computing left and right Riemann sums of the data elements
362 * to determine the size of a data structure, go figger :-).
364 * There's one other twist if a header region tag is in the set to be swabbed,
365 * as the data for a header region is located after all other tag data.
367 * @param entry header entry
368 * @param il no. of entries
369 * @param dl start no. bytes of data
370 * @param pe header physical entry pointer (swapped)
371 * @param dataStart header data start
372 * @param dataEnd header data end
373 * @param regionid region offset
374 * @return no. bytes of data in region, -1 on error
376 static int regionSwab(indexEntry entry, int il, int dl,
378 unsigned char * dataStart,
379 const unsigned char * dataEnd,
382 unsigned char * tprev = NULL;
383 unsigned char * t = NULL;
386 struct indexEntry_s ieprev;
388 memset(&ieprev, 0, sizeof(ieprev));
389 for (; il > 0; il--, pe++) {
390 struct indexEntry_s ie;
393 ie.info.tag = ntohl(pe->tag);
394 ie.info.type = ntohl(pe->type);
395 ie.info.count = ntohl(pe->count);
396 ie.info.offset = ntohl(pe->offset);
398 if (hdrchkType(ie.info.type))
400 if (hdrchkData(ie.info.count))
402 if (hdrchkData(ie.info.offset))
404 if (hdrchkAlign(ie.info.type, ie.info.offset))
407 ie.data = t = dataStart + ie.info.offset;
408 if (dataEnd && t >= dataEnd)
411 ie.length = dataLength(ie.info.type, ie.data, ie.info.count, 1, dataEnd);
412 if (ie.length < 0 || hdrchkData(ie.length))
418 ie.info.offset = regionid;
419 *entry = ie; /* structure assignment */
425 if (typeSizes[type] > 1) {
427 diff = typeSizes[type] - (dl % typeSizes[type]);
428 if (diff != typeSizes[type]) {
430 if (ieprev.info.type == RPM_I18NSTRING_TYPE)
431 ieprev.length += diff;
434 tdel = (tprev ? (t - tprev) : 0);
435 if (ieprev.info.type == RPM_I18NSTRING_TYPE)
436 tdel = ieprev.length;
438 if (ie.info.tag >= HEADER_I18NTABLE) {
442 /* XXX HEADER_IMAGE tags don't include region sub-tag. */
443 if (ie.info.tag == HEADER_IMAGE)
444 tprev -= REGION_TAG_COUNT;
447 /* Perform endian conversions */
448 switch (ntohl(pe->type)) {
450 { int32_t * it = (int32_t *)t;
451 for (; ie.info.count > 0; ie.info.count--, it += 1) {
452 if (dataEnd && ((unsigned char *)it) >= dataEnd)
456 t = (unsigned char *) it;
459 { int16_t * it = (int16_t *) t;
460 for (; ie.info.count > 0; ie.info.count--, it += 1) {
461 if (dataEnd && ((unsigned char *)it) >= dataEnd)
465 t = (unsigned char *) it;
474 ieprev = ie; /* structure assignment */
477 tdel = (tprev ? (t - tprev) : 0);
481 * There are two hacks here:
482 * 1) tl is 16b (i.e. REGION_TAG_COUNT) short while doing headerReload().
483 * 2) the 8/98 rpm bug with inserting i18n tags needs to use tl, not dl.
485 if (tl+REGION_TAG_COUNT == dl)
486 tl += REGION_TAG_COUNT;
494 * @retval *lengthPtr no. bytes in unloaded header blob
495 * @return unloaded header blob (NULL on error)
497 static void * doHeaderUnload(Header h,
511 int drlen, ndribbles;
515 /* Sort entries by (offset,tag). */
518 /* Compute (il,dl) for all tags, including those deleted in region. */
520 drlen = ndribbles = driplen = ndrips = 0;
521 for (i = 0, entry = h->index; i < h->indexUsed; i++, entry++) {
522 if (ENTRY_IS_REGION(entry)) {
523 int32_t rdl = -entry->info.offset; /* negative offset */
524 int32_t ril = rdl/sizeof(*pe);
525 int rid = entry->info.offset;
528 dl += entry->rdlen + entry->info.count;
529 /* XXX Legacy regions do not include the region tag and data. */
530 if (i == 0 && (h->flags & HEADERFLAG_LEGACY))
533 /* Skip rest of entries in region, but account for dribbles. */
534 for (; i < h->indexUsed && entry->info.offset <= rid+1; i++, entry++) {
535 if (entry->info.offset <= rid)
539 type = entry->info.type;
540 if (typeSizes[type] > 1) {
542 diff = typeSizes[type] - (dl % typeSizes[type]);
543 if (diff != typeSizes[type]) {
552 drlen += entry->length;
560 /* Ignore deleted drips. */
561 if (entry->data == NULL || entry->length <= 0)
565 type = entry->info.type;
566 if (typeSizes[type] > 1) {
568 diff = typeSizes[type] - (dl % typeSizes[type]);
569 if (diff != typeSizes[type]) {
579 driplen += entry->length;
583 /* Sanity checks on header intro. */
584 if (hdrchkTags(il) || hdrchkData(dl))
587 len = sizeof(il) + sizeof(dl) + (il * sizeof(*pe)) + dl;
593 pe = (entryInfo) &ei[2];
594 dataStart = te = (char *) (pe + il);
597 for (i = 0, entry = h->index; i < h->indexUsed; i++, entry++) {
603 if (entry->data == NULL || entry->length <= 0)
606 t = (unsigned char*)te;
607 pe->tag = htonl(entry->info.tag);
608 pe->type = htonl(entry->info.type);
609 pe->count = htonl(entry->info.count);
611 if (ENTRY_IS_REGION(entry)) {
612 int32_t rdl = -entry->info.offset; /* negative offset */
613 int32_t ril = rdl/sizeof(*pe) + ndribbles;
614 int rid = entry->info.offset;
616 src = (char *)entry->data;
617 rdlen = entry->rdlen;
619 /* XXX Legacy regions do not include the region tag and data. */
620 if (i == 0 && (h->flags & HEADERFLAG_LEGACY)) {
624 memcpy(pe+1, src, rdl);
625 memcpy(te, src + rdl, rdlen);
628 pe->offset = htonl(te - dataStart);
631 stei[2] = htonl(-rdl-entry->info.count);
633 memcpy(te, stei, entry->info.count);
634 te += entry->info.count;
636 rdlen += entry->info.count;
638 count = regionSwab(NULL, ril, 0, pe, t, NULL, 0);
644 memcpy(pe+1, src + sizeof(*pe), ((ril-1) * sizeof(*pe)));
645 memcpy(te, src + (ril * sizeof(*pe)), rdlen+entry->info.count+drlen);
648 entryInfo se = (entryInfo)src;
649 int off = ntohl(se->offset);
650 pe->offset = (off) ? htonl(te - dataStart) : htonl(off);
652 te += entry->info.count + drlen;
654 count = regionSwab(NULL, ril, 0, pe, t, NULL, 0);
655 if (count != (rdlen + entry->info.count + drlen))
659 /* Skip rest of entries in region. */
660 while (i < h->indexUsed && entry->info.offset <= rid+1) {
670 /* Ignore deleted drips. */
671 if (entry->data == NULL || entry->length <= 0)
675 type = entry->info.type;
676 if (typeSizes[type] > 1) {
678 diff = typeSizes[type] - ((te - dataStart) % typeSizes[type]);
679 if (diff != typeSizes[type]) {
686 pe->offset = htonl(te - dataStart);
688 /* copy data w/ endian conversions */
689 switch (entry->info.type) {
691 count = entry->info.count;
694 *((int32_t *)te) = htonl(*((int32_t *)src));
695 te += sizeof(int32_t);
696 src += sizeof(int32_t);
701 count = entry->info.count;
704 *((int16_t *)te) = htons(*((int16_t *)src));
705 te += sizeof(int16_t);
706 src += sizeof(int16_t);
711 memcpy(te, entry->data, entry->length);
718 /* Insure that there are no memcpy underruns/overruns. */
719 if (((char *)pe) != dataStart)
721 if ((((char *)ei)+len) != te)
727 h->flags &= ~HEADERFLAG_SORTED;
737 void * headerUnload(Header h)
740 void * uh = doHeaderUnload(h, &length);
745 * Find matching (tag,type) entry in header.
747 * @param tag entry tag
748 * @param type entry type
749 * @return header entry
752 indexEntry findEntry(Header h, rpmTag tag, rpmTagType type)
754 indexEntry entry, entry2, last;
755 struct indexEntry_s key;
757 if (h == NULL) return NULL;
758 if (!(h->flags & HEADERFLAG_SORTED)) headerSort(h);
763 bsearch(&key, h->index, h->indexUsed, sizeof(*h->index), indexCmp);
767 if (type == RPM_NULL_TYPE)
771 while (entry->info.tag == tag && entry->info.type != type &&
772 entry > h->index) entry--;
774 if (entry->info.tag == tag && entry->info.type == type)
777 last = h->index + h->indexUsed;
778 /* FIX: entry2 = entry. Code looks bogus as well. */
779 while (entry2->info.tag == tag && entry2->info.type != type &&
780 entry2 < last) entry2++;
782 if (entry->info.tag == tag && entry->info.type == type)
788 int headerRemoveEntry(Header h, rpmTag tag)
790 indexEntry last = h->index + h->indexUsed;
791 indexEntry entry, first;
794 entry = findEntry(h, tag, RPM_NULL_TYPE);
795 if (!entry) return 1;
797 /* Make sure entry points to the first occurence of this tag. */
798 while (entry > h->index && (entry - 1)->info.tag == tag)
801 /* Free data for tags being removed. */
802 for (first = entry; first < last; first++) {
804 if (first->info.tag != tag)
809 if (ENTRY_IN_REGION(first))
814 ne = (first - entry);
819 memmove(entry, first, (ne * sizeof(*entry)));
825 Header headerLoad(void * uh)
827 int32_t * ei = (int32_t *) uh;
828 int32_t il = ntohl(ei[0]); /* index length */
829 int32_t dl = ntohl(ei[1]); /* data length */
830 size_t pvlen = sizeof(il) + sizeof(dl) +
831 (il * sizeof(struct entryInfo_s)) + dl;
835 unsigned char * dataStart;
836 unsigned char * dataEnd;
841 /* Sanity checks on header intro. */
842 if (hdrchkTags(il) || hdrchkData(dl))
846 pe = (entryInfo) &ei[2];
847 dataStart = (unsigned char *) (pe + il);
848 dataEnd = dataStart + dl;
850 h = xcalloc(1, sizeof(*h));
852 h->indexAlloced = il + 1;
854 h->index = xcalloc(h->indexAlloced, sizeof(*h->index));
855 h->flags |= HEADERFLAG_SORTED;
861 if (!(htonl(pe->tag) < HEADER_I18NTABLE)) {
862 h->flags |= HEADERFLAG_LEGACY;
863 entry->info.type = REGION_TAG_TYPE;
864 entry->info.tag = HEADER_IMAGE;
865 entry->info.count = REGION_TAG_COUNT;
866 entry->info.offset = ((unsigned char *)pe - dataStart); /* negative offset */
869 entry->length = pvlen - sizeof(il) - sizeof(dl);
870 rdlen = regionSwab(entry+1, il, 0, pe, dataStart, dataEnd, entry->info.offset);
871 #if 0 /* XXX don't check, the 8/98 i18n bug fails here. */
875 entry->rdlen = rdlen;
882 h->flags &= ~HEADERFLAG_LEGACY;
884 entry->info.type = htonl(pe->type);
885 entry->info.count = htonl(pe->count);
887 if (hdrchkType(entry->info.type))
889 if (hdrchkTags(entry->info.count))
892 { int off = ntohl(pe->offset);
897 size_t nb = REGION_TAG_COUNT;
899 /* XXX Hmm, why the copy? */
900 memcpy(&stei, dataStart + off, nb);
901 rdl = -ntohl(stei[2]); /* negative offset */
902 ril = rdl/sizeof(*pe);
903 if (hdrchkTags(ril) || hdrchkData(rdl))
905 entry->info.tag = htonl(pe->tag);
908 rdl = (ril * sizeof(struct entryInfo_s));
909 entry->info.tag = HEADER_IMAGE;
912 entry->info.offset = -rdl; /* negative offset */
915 entry->length = pvlen - sizeof(il) - sizeof(dl);
916 rdlen = regionSwab(entry+1, ril-1, 0, pe+1, dataStart, dataEnd, entry->info.offset);
919 entry->rdlen = rdlen;
921 if (ril < h->indexUsed) {
922 indexEntry newEntry = entry + ril;
923 int ne = (h->indexUsed - ril);
924 int rid = entry->info.offset+1;
927 /* Load dribble entries from region. */
928 rc = regionSwab(newEntry, ne, 0, pe+ril, dataStart, dataEnd, rid);
933 { indexEntry firstEntry = newEntry;
934 int save = h->indexUsed;
937 /* Dribble entries replace duplicate region entries. */
939 for (j = 0; j < ne; j++, newEntry++) {
940 (void) headerRemoveEntry(h, newEntry->info.tag);
941 if (newEntry->info.tag == HEADER_BASENAMES)
942 (void) headerRemoveEntry(h, HEADER_OLDFILENAMES);
945 /* If any duplicate entries were replaced, move new entries down. */
946 if (h->indexUsed < (save - ne)) {
947 memmove(h->index + h->indexUsed, firstEntry,
948 (ne * sizeof(*entry)));
955 h->flags &= ~HEADERFLAG_SORTED;
962 h->index = _free(h->index);
968 Header headerReload(Header h, rpmTag tag)
972 void * uh = doHeaderUnload(h, &length);
982 if (nh->flags & HEADERFLAG_ALLOCATED)
984 nh->flags |= HEADERFLAG_ALLOCATED;
985 if (ENTRY_IS_REGION(nh->index)) {
986 if (tag == HEADER_SIGNATURES || tag == HEADER_IMMUTABLE)
987 nh->index[0].info.tag = tag;
992 Header headerCopyLoad(const void * uh)
994 int32_t * ei = (int32_t *) uh;
995 int32_t il = ntohl(ei[0]); /* index length */
996 int32_t dl = ntohl(ei[1]); /* data length */
997 size_t pvlen = sizeof(il) + sizeof(dl) +
998 (il * sizeof(struct entryInfo_s)) + dl;
1002 /* Sanity checks on header intro. */
1003 if (!(hdrchkTags(il) || hdrchkData(dl)) && pvlen < headerMaxbytes) {
1004 nuh = memcpy(xmalloc(pvlen), uh, pvlen);
1005 if ((h = headerLoad(nuh)) != NULL)
1006 h->flags |= HEADERFLAG_ALLOCATED;
1014 * Read (and load) header from file handle.
1015 * @param fd file handle
1016 * @param magicp read (and verify) 8 bytes of (magic, 0)?
1017 * @return header (or NULL on error)
1019 Header headerRead(FD_t fd, enum hMagic magicp)
1023 int32_t * ei = NULL;
1031 memset(block, 0, sizeof(block));
1033 if (magicp == HEADER_MAGIC_YES)
1037 if (timedRead(fd, (char *)block, i*sizeof(*block)) != (i * sizeof(*block)))
1042 if (magicp == HEADER_MAGIC_YES) {
1044 if (memcmp(&magic, header_magic, sizeof(magic)))
1046 reserved = block[i++];
1049 il = ntohl(block[i]); i++;
1050 dl = ntohl(block[i]); i++;
1052 len = sizeof(il) + sizeof(dl) + (il * sizeof(struct entryInfo_s)) + dl;
1054 /* Sanity checks on header intro. */
1055 if (hdrchkTags(il) || hdrchkData(dl) || len > headerMaxbytes)
1061 len -= sizeof(il) + sizeof(dl);
1064 if (timedRead(fd, (char *)&ei[2], len) != len)
1071 if (h->flags & HEADERFLAG_ALLOCATED)
1073 h->flags |= HEADERFLAG_ALLOCATED;
1076 /* FIX: timedRead macro obscures annotation */
1080 int headerWrite(FD_t fd, Header h, enum hMagic magicp)
1088 uh = doHeaderUnload(h, &length);
1092 case HEADER_MAGIC_YES:
1093 nb = Fwrite(header_magic, sizeof(char), sizeof(header_magic), fd);
1094 if (nb != sizeof(header_magic))
1097 case HEADER_MAGIC_NO:
1101 nb = Fwrite(uh, sizeof(char), length, fd);
1105 return (nb == length ? 0 : 1);
1108 int headerIsEntry(Header h, rpmTag tag)
1110 /* FIX: h modified by sort. */
1111 return (findEntry(h, tag, RPM_NULL_TYPE) ? 1 : 0);
1116 * Retrieve data from header entry.
1117 * @todo Permit retrieval of regions other than HEADER_IMUTABLE.
1118 * @param entry header entry
1119 * @param td tag data container
1120 * @param minMem string pointers refer to header memory?
1121 * @return 1 on success, otherwise error.
1123 static int copyTdEntry(const indexEntry entry, rpmtd td, int minMem)
1125 rpm_count_t count = entry->info.count;
1126 int rc = 1; /* XXX 1 on success. */
1129 switch (entry->info.type) {
1132 * XXX This only works for
1133 * XXX "sealed" HEADER_IMMUTABLE/HEADER_SIGNATURES/HEADER_IMAGE.
1134 * XXX This will *not* work for unsealed legacy HEADER_IMAGE (i.e.
1135 * XXX a legacy header freshly read, but not yet unloaded to the rpmdb).
1137 if (ENTRY_IS_REGION(entry)) {
1138 int32_t * ei = ((int32_t *)entry->data) - 2;
1139 entryInfo pe = (entryInfo) (ei + 2);
1140 unsigned char * dataStart = (unsigned char *) (pe + ntohl(ei[0]));
1141 int32_t rdl = -entry->info.offset; /* negative offset */
1142 int32_t ril = rdl/sizeof(*pe);
1145 count = 2 * sizeof(*ei) + (ril * sizeof(*pe)) + rdl;
1146 if (entry->info.tag == HEADER_IMAGE) {
1150 count += REGION_TAG_COUNT;
1151 rdl += REGION_TAG_COUNT;
1154 td->data = xmalloc(count);
1155 ei = (int32_t *) td->data;
1159 pe = (entryInfo) memcpy(ei + 2, pe, (ril * sizeof(*pe)));
1161 dataStart = (unsigned char *) memcpy(pe + ril, dataStart, rdl);
1163 rc = regionSwab(NULL, ril, 0, pe, dataStart, dataStart + rdl, 0);
1164 /* XXX 1 on success. */
1165 rc = (rc < 0) ? 0 : 1;
1167 count = entry->length;
1169 ? memcpy(xmalloc(count), entry->data, count)
1173 case RPM_STRING_TYPE:
1175 td->data = entry->data;
1178 case RPM_STRING_ARRAY_TYPE:
1179 case RPM_I18NSTRING_TYPE:
1180 { const char ** ptrEntry;
1181 int tableSize = count * sizeof(char *);
1186 td->data = xmalloc(tableSize);
1187 ptrEntry = (const char **) td->data;
1190 t = xmalloc(tableSize + entry->length);
1191 td->data = (void *)t;
1192 ptrEntry = (const char **) td->data;
1194 memcpy(t, entry->data, entry->length);
1196 for (i = 0; i < count; i++) {
1204 td->data = entry->data;
1207 td->type = entry->info.type;
1209 td->freeData = (entry->data == td->data) ? 0 : 1;
1215 * Retrieve data from header entry.
1216 * Just a compat wrapper around copyTdEntry() until everything's converted
1218 * @param entry header entry
1219 * @retval type address of type (or NULL)
1220 * @retval p address of data (or NULL)
1221 * @retval c address of count (or NULL)
1222 * @param minMem string pointers refer to header memory?
1223 * @return 1 on success, otherwise error.
1225 static int copyEntry(const indexEntry entry,
1235 rc = copyTdEntry(entry, &td, minMem);
1241 * Does locale match entry in header i18n table?
1244 * The range [l,le) contains the next locale to match:
1245 * ll[_CC][.EEEEE][@dddd]
1247 * ll ISO language code (in lowercase).
1248 * CC (optional) ISO coutnry code (in uppercase).
1249 * EEEEE (optional) encoding (not really standardized).
1250 * dddd (optional) dialect.
1253 * @param td header i18n table data, NUL terminated
1254 * @param l start of locale to match
1255 * @param le end of locale to match
1256 * @return 1 on good match, 2 on weak match, 0 on no match
1258 static int headerMatchLocale(const char *td, const char *l, const char *le)
1262 /* First try a complete match. */
1263 if (strlen(td) == (le-l) && !strncmp(td, l, (le - l)))
1266 /* Next, try stripping optional dialect and matching. */
1267 for (fe = l; fe < le && *fe != '@'; fe++)
1269 if (fe < le && !strncmp(td, l, (fe - l)))
1272 /* Next, try stripping optional codeset and matching. */
1273 for (fe = l; fe < le && *fe != '.'; fe++)
1275 if (fe < le && !strncmp(td, l, (fe - l)))
1278 /* Finally, try stripping optional country code and matching. */
1279 for (fe = l; fe < le && *fe != '_'; fe++)
1281 if (fe < le && !strncmp(td, l, (fe - l)))
1288 * Return i18n string from header that matches locale.
1290 * @param entry i18n string data
1291 * @return matching i18n string (or 1st string if no match)
1294 headerFindI18NString(Header h, indexEntry entry)
1296 const char *lang, *l, *le;
1299 /* XXX Drepper sez' this is the order. */
1300 if ((lang = getenv("LANGUAGE")) == NULL &&
1301 (lang = getenv("LC_ALL")) == NULL &&
1302 (lang = getenv("LC_MESSAGES")) == NULL &&
1303 (lang = getenv("LANG")) == NULL)
1306 if ((table = findEntry(h, HEADER_I18NTABLE, RPM_STRING_ARRAY_TYPE)) == NULL)
1309 for (l = lang; *l != '\0'; l = le) {
1311 char *ed, *ed_weak = NULL;
1314 while (*l && *l == ':') /* skip leading colons */
1318 for (le = l; *le && *le != ':'; le++) /* find end of this locale */
1321 /* For each entry in the header ... */
1322 for (langNum = 0, td = table->data, ed = entry->data;
1323 langNum < entry->info.count;
1324 langNum++, td += strlen(td) + 1, ed += strlen(ed) + 1) {
1326 int match = headerMatchLocale(td, l, le);
1327 if (match == 1) return ed;
1328 else if (match == 2) ed_weak = ed;
1331 if (ed_weak) return ed_weak;
1338 * Retrieve tag data from header.
1340 * @param tag tag to retrieve
1341 * @retval td tag data container
1342 * @param flags flags to control retrieval
1343 * @return 1 on success, 0 on not found
1345 static int intGetTdEntry(Header h, rpmTag tag, rpmtd td, headerGetFlags flags)
1351 /* ensure clean state */
1354 /* First find the tag */
1355 /* FIX: h modified by sort. */
1356 entry = findEntry(h, tag, RPM_NULL_TYPE);
1357 if (entry == NULL) {
1358 /* Td is zeroed above, just return... */
1362 if (flags & HEADERGET_RAW) {
1363 rc = copyTdEntry(entry, td, (flags & HEADERGET_MINMEM));
1365 switch (entry->info.type) {
1366 case RPM_I18NSTRING_TYPE:
1368 td->type = RPM_STRING_TYPE;
1370 td->data = headerFindI18NString(h, entry);
1373 rc = copyTdEntry(entry, td, (flags & HEADERGET_MINMEM));
1378 /* XXX 1 on success */
1379 return ((rc == 1) ? 1 : 0);
1382 void * headerFreeTag(Header h, rpm_data_t data, rpmTagType type)
1385 if (type == RPM_FORCEFREE_TYPE ||
1386 type == RPM_STRING_ARRAY_TYPE ||
1387 type == RPM_I18NSTRING_TYPE ||
1388 type == RPM_BIN_TYPE)
1395 * XXX temporary kludgery until tag extensions have been converted to
1396 * take rpmtd as argument
1398 static int intGetTagExt(Header h, rpmTag tag, rpmtd td, headerTagTagFunction tagfunc)
1402 rc = tagfunc(h, td);
1404 /* XXX extension returns are reversed, ugh */
1408 int headerGet(Header h, rpmTag tag, rpmtd td, headerGetFlags flags)
1411 headerTagTagFunction tagfunc = NULL;
1415 if (flags & HEADERGET_EXT) {
1416 tagfunc = rpmHeaderTagFunc(tag);
1420 rc = intGetTagExt(h, tag, td, tagfunc);
1422 rc = intGetTdEntry(h, tag, td, flags);
1425 assert(tag == td->tag);
1429 static int headerGetWrap(Header h, rpmTag tag,
1433 headerGetFlags flags)
1438 rc = headerGet(h, tag, &td, flags);
1443 int headerGetEntry(Header h, rpmTag tag,
1448 return headerGetWrap(h, tag, type, p, c, HEADERGET_DEFAULT);
1451 int headerGetEntryMinMemory(Header h, rpmTag tag,
1456 return headerGetWrap(h, tag, type, (rpm_data_t) p, c, HEADERGET_MINMEM);
1459 int headerGetRawEntry(Header h, rpmTag tag, rpmTagType * type, rpm_data_t * p,
1465 if (p == NULL) return headerIsEntry(h, tag);
1467 /* First find the tag */
1468 /* FIX: h modified by sort. */
1469 entry = findEntry(h, tag, RPM_NULL_TYPE);
1476 rc = copyEntry(entry, type, p, c, 0);
1478 /* XXX 1 on success */
1479 return ((rc == 1) ? 1 : 0);
1484 static void copyData(rpmTagType type, rpm_data_t dstPtr,
1485 rpm_constdata_t srcPtr, rpm_count_t cnt, int dataLength)
1488 case RPM_STRING_ARRAY_TYPE:
1489 case RPM_I18NSTRING_TYPE:
1490 { const char ** av = (const char **) srcPtr;
1493 while (cnt-- > 0 && dataLength > 0) {
1495 if ((s = *av++) == NULL)
1499 } while (s[-1] && --dataLength > 0);
1504 memmove(dstPtr, srcPtr, dataLength);
1510 * Return (malloc'ed) copy of entry data.
1511 * @param type entry data type
1512 * @param p entry data
1513 * @param c entry item count
1514 * @retval lengthPtr no. bytes in returned data
1515 * @return (malloc'ed) copy of entry data, NULL on error
1518 grabData(rpmTagType type, rpm_constdata_t p, rpm_count_t c, int * lengthPtr)
1520 rpm_data_t data = NULL;
1523 length = dataLength(type, p, c, 0, NULL);
1525 data = xmalloc(length);
1526 copyData(type, data, p, c, length);
1530 *lengthPtr = length;
1534 int headerAddEntry(Header h, rpmTag tag, rpmTagType type,
1535 rpm_constdata_t p, rpm_count_t c)
1541 /* Count must always be >= 1 for headerAddEntry. */
1545 if (hdrchkType(type))
1551 data = grabData(type, p, c, &length);
1552 if (data == NULL || length <= 0)
1555 /* Allocate more index space if necessary */
1556 if (h->indexUsed == h->indexAlloced) {
1557 h->indexAlloced += INDEX_MALLOC_SIZE;
1558 h->index = xrealloc(h->index, h->indexAlloced * sizeof(*h->index));
1561 /* Fill in the index */
1562 entry = h->index + h->indexUsed;
1563 entry->info.tag = tag;
1564 entry->info.type = type;
1565 entry->info.count = c;
1566 entry->info.offset = 0;
1568 entry->length = length;
1570 if (h->indexUsed > 0 && tag < h->index[h->indexUsed-1].info.tag)
1571 h->flags &= ~HEADERFLAG_SORTED;
1577 int headerAppendEntry(Header h, rpmTag tag, rpmTagType type,
1578 rpm_constdata_t p, rpm_count_t c)
1583 if (type == RPM_STRING_TYPE || type == RPM_I18NSTRING_TYPE) {
1584 /* we can't do this */
1588 /* Find the tag entry in the header. */
1589 entry = findEntry(h, tag, type);
1593 length = dataLength(type, p, c, 0, NULL);
1597 if (ENTRY_IN_REGION(entry)) {
1598 char * t = xmalloc(entry->length + length);
1599 memcpy(t, entry->data, entry->length);
1601 entry->info.offset = 0;
1603 entry->data = xrealloc(entry->data, entry->length + length);
1605 copyData(type, ((char *) entry->data) + entry->length, p, c, length);
1607 entry->length += length;
1609 entry->info.count += c;
1614 int headerAddOrAppendEntry(Header h, rpmTag tag, rpmTagType type,
1615 rpm_constdata_t p, rpm_count_t c)
1617 return (findEntry(h, tag, type)
1618 ? headerAppendEntry(h, tag, type, p, c)
1619 : headerAddEntry(h, tag, type, p, c));
1622 int headerPut(Header h, rpmtd td, headerPutFlags flags)
1627 if (flags & HEADERPUT_APPEND) {
1628 rc = headerAddOrAppendEntry(h, td->tag, td->type, td->data, td->count);
1630 rc = headerAddEntry(h, td->tag, td->type, td->data, td->count);
1635 int headerAddI18NString(Header h, rpmTag tag, const char * string,
1638 indexEntry table, entry;
1639 const char ** strArray;
1642 rpm_count_t i, langNum;
1645 table = findEntry(h, HEADER_I18NTABLE, RPM_STRING_ARRAY_TYPE);
1646 entry = findEntry(h, tag, RPM_I18NSTRING_TYPE);
1648 if (!table && entry)
1649 return 0; /* this shouldn't ever happen!! */
1651 if (!table && !entry) {
1652 const char * charArray[2];
1653 rpm_count_t count = 0;
1654 if (!lang || (lang[0] == 'C' && lang[1] == '\0')) {
1655 charArray[count++] = "C";
1657 charArray[count++] = "C";
1658 charArray[count++] = lang;
1660 if (!headerAddEntry(h, HEADER_I18NTABLE, RPM_STRING_ARRAY_TYPE,
1663 table = findEntry(h, HEADER_I18NTABLE, RPM_STRING_ARRAY_TYPE);
1668 if (!lang) lang = "C";
1670 { const char * l = table->data;
1671 for (langNum = 0; langNum < table->info.count; langNum++) {
1672 if (!strcmp(l, lang)) break;
1677 if (langNum >= table->info.count) {
1678 length = strlen(lang) + 1;
1679 if (ENTRY_IN_REGION(table)) {
1680 char * t = xmalloc(table->length + length);
1681 memcpy(t, table->data, table->length);
1683 table->info.offset = 0;
1685 table->data = xrealloc(table->data, table->length + length);
1686 memmove(((char *)table->data) + table->length, lang, length);
1687 table->length += length;
1688 table->info.count++;
1693 strArray = xmalloc(sizeof(*strArray) * (langNum + 1));
1694 for (i = 0; i < langNum; i++)
1696 strArray[langNum] = string;
1697 rc = headerAddEntry(h, tag, RPM_I18NSTRING_TYPE, strArray, langNum + 1);
1700 } else if (langNum >= entry->info.count) {
1701 ghosts = langNum - entry->info.count;
1703 length = strlen(string) + 1 + ghosts;
1704 if (ENTRY_IN_REGION(entry)) {
1705 char * t = xmalloc(entry->length + length);
1706 memcpy(t, entry->data, entry->length);
1708 entry->info.offset = 0;
1710 entry->data = xrealloc(entry->data, entry->length + length);
1712 memset(((char *)entry->data) + entry->length, '\0', ghosts);
1713 memmove(((char *)entry->data) + entry->length + ghosts, string, strlen(string)+1);
1715 entry->length += length;
1716 entry->info.count = langNum + 1;
1718 char *b, *be, *e, *ee, *t;
1721 /* Set beginning/end pointers to previous data */
1722 b = be = e = ee = entry->data;
1723 for (i = 0; i < table->info.count; i++) {
1726 ee += strlen(ee) + 1;
1731 /* Get storage for new buffer */
1733 sn = strlen(string) + 1;
1735 length = bn + sn + en;
1736 t = buf = xmalloc(length);
1738 /* Copy values into new storage */
1741 memcpy(t, string, sn);
1746 /* Replace i18N string array */
1747 entry->length -= strlen(be) + 1;
1748 entry->length += sn;
1750 if (ENTRY_IN_REGION(entry)) {
1751 entry->info.offset = 0;
1753 entry->data = _free(entry->data);
1760 int headerModifyEntry(Header h, rpmTag tag, rpmTagType type,
1761 rpm_constdata_t p, rpm_count_t c)
1768 /* First find the tag */
1769 entry = findEntry(h, tag, type);
1774 data = grabData(type, p, c, &length);
1775 if (data == NULL || length <= 0)
1778 /* make sure entry points to the first occurence of this tag */
1779 while (entry > h->index && (entry - 1)->info.tag == tag)
1782 /* free after we've grabbed the new data in case the two are intertwined;
1783 that's a bad idea but at least we won't break */
1784 oldData = entry->data;
1786 entry->info.count = c;
1787 entry->info.type = type;
1789 entry->length = length;
1791 if (ENTRY_IN_REGION(entry)) {
1792 entry->info.offset = 0;
1794 oldData = _free(oldData);
1800 * Header tag iterator data structure.
1802 struct headerIterator_s {
1803 Header h; /*!< Header being iterated. */
1804 int next_index; /*!< Next tag index. */
1807 HeaderIterator headerFreeIterator(HeaderIterator hi)
1810 hi->h = headerFree(hi->h);
1816 HeaderIterator headerInitIterator(Header h)
1818 HeaderIterator hi = xmalloc(sizeof(*hi));
1822 hi->h = headerLink(h);
1827 int headerNext(HeaderIterator hi, rpmtd td)
1830 int slot = hi->next_index;
1831 indexEntry entry = NULL;
1837 for (slot = hi->next_index; slot < h->indexUsed; slot++) {
1838 entry = h->index + slot;
1839 if (!ENTRY_IS_REGION(entry))
1842 hi->next_index = slot;
1843 if (entry == NULL || slot >= h->indexUsed)
1849 td->tag = entry->info.tag;
1851 rc = copyTdEntry(entry, td, 0);
1853 /* XXX 1 on success */
1854 return ((rc == 1) ? 1 : 0);
1857 int headerNextIterator(HeaderIterator hi,
1866 rc = headerNext(hi, &td);
1874 * Duplicate a header.
1876 * @return new header instance
1878 Header headerCopy(Header h)
1880 Header nh = headerNew();
1884 hi = headerInitIterator(h);
1885 while (headerNext(hi, &td)) {
1886 if (rpmtdCount(&td) > 0) {
1887 (void) headerPut(nh, &td, HEADERPUT_DEFAULT);
1891 hi = headerFreeIterator(hi);
1893 return headerReload(nh, HEADER_IMAGE);
1896 void headerCopyTags(Header headerFrom, Header headerTo,
1897 const rpmTag * tagstocopy)
1902 if (headerFrom == headerTo)
1905 for (p = tagstocopy; *p != 0; p++) {
1906 if (headerIsEntry(headerTo, *p))
1908 if (!headerGet(headerFrom, *p, &td, HEADERGET_MINMEM))
1910 (void) headerPut(headerTo, &td, HEADERPUT_DEFAULT);
1915 void * headerFreeData(rpm_data_t data, rpmTagType type)
1918 if (type == RPM_FORCEFREE_TYPE ||
1919 type == RPM_STRING_ARRAY_TYPE ||
1920 type == RPM_I18NSTRING_TYPE ||
1921 type == RPM_BIN_TYPE)
1922 free(data); /* XXX _free() */