5 #undef REMALLOC_HEADER_REGION
9 /* RPM - Copyright (C) 1995-2000 Red Hat Software */
11 /* Data written to file descriptors is in network byte order. */
12 /* Data read from file descriptors is expected to be in */
13 /* network byte order and is converted on the fly to host order. */
17 #if !defined(__LCLINT__)
18 #include <netinet/in.h>
19 #endif /* __LCLINT__ */
25 /* XXX avoid rpmlib.h, need for debugging. */
26 /*@observer@*/ const char *const tagName(int tag) /*@*/;
29 * Teach header.c about legacy tags.
31 #define HEADER_OLDFILENAMES 1027
32 #define HEADER_BASENAMES 1117
34 #define INDEX_MALLOC_SIZE 8
36 #define PARSER_BEGIN 0
37 #define PARSER_IN_ARRAY 1
38 #define PARSER_IN_EXPR 2
40 static unsigned char header_magic[8] = {
41 0x8e, 0xad, 0xe8, 0x01, 0x00, 0x00, 0x00, 0x00
45 * Alignment needs (and sizeof scalars types) for internal rpm data types.
47 static int typeSizes[] = {
48 0, /*!< RPM_NULL_TYPE */
49 1, /*!< RPM_CHAR_TYPE */
50 1, /*!< RPM_INT8_TYPE */
51 2, /*!< RPM_INT16_TYPE */
52 4, /*!< RPM_INT32_TYPE */
53 -1, /*!< RPM_INT64_TYPE */
54 -1, /*!< RPM_STRING_TYPE */
55 1, /*!< RPM_BIN_TYPE */
56 -1, /*!< RPM_STRING_ARRAY_TYPE */
57 -1 /*!< RPM_I18NSTRING_TYPE */
61 * Description of tag data.
64 int_32 tag; /*!< Tag identifier. */
65 int_32 type; /*!< Tag data type. */
66 int_32 offset; /*!< Offset into data segment (ondisk only). */
67 int_32 count; /*!< Number of tag elements. */
70 #define REGION_TAG_TYPE RPM_BIN_TYPE
71 #define REGION_TAG_COUNT sizeof(struct entryInfo)
73 #define ENTRY_IS_REGION(_e) ((_e)->info.tag < HEADER_I18NTABLE)
74 #define ENTRY_IN_REGION(_e) ((_e)->info.offset < 0)
77 * A single tag from a Header.
80 struct entryInfo info; /*!< Description of tag data. */
81 /*@owned@*/ void * data; /*!< Location of tag data. */
82 int length; /*!< No. bytes of data. */
83 int rdlen; /*!< No. bytes of data in region. */
87 * The Header data structure.
90 /*@owned@*/ struct indexEntry *index; /*!< Array of tags. */
91 int indexUsed; /*!< Current size of tag array. */
92 int indexAlloced; /*!< Allocated size of tag array. */
93 int region_allocated; /*!< Is 1st header region allocated? */
94 int sorted; /*!< Are header entries sorted? */
95 int legacy; /*!< Header came from legacy source? */
96 /*@refs@*/ int nrefs; /*!< Reference count. */
102 headerTagTagFunction ext; /*!< if NULL tag element is invalid */
107 /*@kept@*/ char * format;
108 /*@kept@*/ char * type;
114 struct extensionCache {
119 /*@owned@*/ const void * data;
124 struct sprintfToken {
134 /*@only@*/ struct sprintfToken * format;
137 struct sprintfTag tag;
139 /*@dependent@*/ char * string;
143 /*@only@*/ struct sprintfToken * ifFormat;
145 /*@only@*/ struct sprintfToken * elseFormat;
147 struct sprintfTag tag;
153 * Return length of entry data.
154 * @param type entry data type
155 * @param p entry data
156 * @param count entry item count
157 * @param onDisk data is concatenated strings (with NUL's))?
158 * @return no. bytes in data
161 static int dataLength(int_32 type, const void * p, int_32 count, int onDisk)
167 case RPM_STRING_TYPE:
168 if (count == 1) { /* Special case -- p is just the string */
169 length = strlen(p) + 1;
172 /* This should not be allowed */
173 fprintf(stderr, _("dataLength() RPM_STRING_TYPE count must be 1.\n"));
175 /*@notreached@*/ break;
177 case RPM_STRING_ARRAY_TYPE:
178 case RPM_I18NSTRING_TYPE:
181 /* This is like RPM_STRING_TYPE, except it's *always* an array */
182 /* Compute sum of length of all strings, including null terminators */
186 const char * chptr = p;
190 thisLen = strlen(chptr) + 1;
195 const char ** src = (const char **)p;
197 /* add one for null termination */
198 length += strlen(*src++) + 1;
204 if (typeSizes[type] != -1) {
205 length = typeSizes[type] * count;
208 fprintf(stderr, _("Data type %d not supported\n"), (int) type);
210 /*@notreached@*/ break;
217 * Swap int_32 and int_16 arrays within header region.
219 * This code is way more twisty than I would like.
221 * A bug with RPM_I18NSTRING_TYPE in rpm-2.5.x (fixed in August 1998)
222 * causes the offset and length of elements in a header region to disagree
223 * regarding the total length of the region data.
225 * The "fix" is to compute the size using both offset and length and
226 * return the larger of the two numbers as the size of the region.
227 * Kinda like computing left and right Riemann sums of the data elements
228 * to determine the size of a data structure, go figger :-).
230 * There's one other twist if a header region tag is in the set to be swabbed,
231 * as the data for a header region is located after all other tag data.
233 * @param entry header entry
234 * @param il no. of entries
235 * @param dl start no. bytes of data
236 * @param pe header physical entry pointer (swapped)
237 * @param dataStart header data
238 * @param regionid region offset
239 * @return no. bytes of data in region
241 static int regionSwab(struct indexEntry * entry, int il, int dl,
242 const struct entryInfo * pe, char * dataStart, int regionid)
248 for (; il > 0; il--, pe++) {
249 struct indexEntry ie;
252 ie.info.tag = ntohl(pe->tag);
253 ie.info.type = ntohl(pe->type);
254 ie.info.count = ntohl(pe->count);
255 ie.info.offset = ntohl(pe->offset);
256 ie.data = t = dataStart + ie.info.offset;
257 ie.length = dataLength(ie.info.type, ie.data, ie.info.count, 1);
260 assert(ie.info.type >= RPM_MIN_TYPE && ie.info.type <= RPM_MAX_TYPE);
263 ie.info.offset = regionid;
264 *entry = ie; /* structure assignment */
270 if (typeSizes[type] > 1) {
272 diff = typeSizes[type] - (dl % typeSizes[type]);
273 if (diff != typeSizes[type]) {
277 tdel = (tprev ? (t - tprev) : 0);
280 tprev = (ie.info.tag < HEADER_I18NTABLE)
283 /* Perform endian conversions */
284 switch (ntohl(pe->type)) {
286 { int_32 * it = (int_32 *)t;
287 for (; ie.info.count > 0; ie.info.count--, it += 1)
292 { int_16 * it = (int_16 *) t;
293 for (; ie.info.count > 0; ie.info.count--, it += 1)
302 tdel = (tprev ? (t - tprev) : 0);
310 * Retrieve data from header entry.
311 * @todo Permit retrieval of regions other than HEADER_IMUTABLE.
312 * @param entry header entry
313 * @retval type address of type (or NULL)
314 * @retval p address of data (or NULL)
315 * @retval c address of count (or NULL)
316 * @param minMem string pointers refer to header memory?
318 static void copyEntry(const struct indexEntry * entry, /*@out@*/ int_32 * type,
319 /*@out@*/ const void ** p, /*@out@*/ int_32 * c, int minMem)
320 /*@modifies *type, *p, *c @*/
322 int_32 count = entry->info.count;
325 switch (entry->info.type) {
327 /* XXX this only works for HEADER_IMMUTABLE */
328 if (ENTRY_IS_REGION(entry)) {
329 int_32 * ei = ((int_32 *)entry->data) - 2;
330 struct entryInfo * pe = (struct entryInfo *) (ei + 2);
331 char * dataStart = (char *) (pe + ntohl(ei[0]));
332 int_32 rdl = -entry->info.offset; /* negative offset */
333 int_32 ril = rdl/sizeof(*pe);
335 count = 2 * sizeof(*ei) + (ril * sizeof(*pe)) +
336 entry->rdlen + REGION_TAG_COUNT;
340 ei[1] = htonl(entry->rdlen + REGION_TAG_COUNT);
341 pe = (struct entryInfo *) memcpy(ei + 2, pe, (ril * sizeof(*pe)));
342 dataStart = (char *) memcpy(pe + ril, dataStart,
343 (entry->rdlen + REGION_TAG_COUNT));
345 (void) regionSwab(NULL, ril, 0, pe, dataStart, 0);
347 count = entry->length;
349 ? memcpy(xmalloc(count), entry->data, count)
353 case RPM_STRING_TYPE:
359 case RPM_STRING_ARRAY_TYPE:
360 case RPM_I18NSTRING_TYPE:
361 { const char ** ptrEntry;
362 int tableSize = count * sizeof(char *);
367 *p = xmalloc(tableSize);
368 ptrEntry = (const char **) *p;
371 t = xmalloc(tableSize + entry->length);
373 ptrEntry = (const char **) *p;
375 memcpy(t, entry->data, entry->length);
377 for (i = 0; i < count; i++) {
388 if (type) *type = entry->info.type;
393 * Header tag iterator data structure.
395 struct headerIteratorS {
396 Header h; /*!< Header being iterated. */
397 int next_index; /*!< Next tag index. */
400 HeaderIterator headerInitIterator(Header h)
402 HeaderIterator hi = xmalloc(sizeof(struct headerIteratorS));
406 hi->h = headerLink(h);
411 void headerFreeIterator(HeaderIterator iter)
417 int headerNextIterator(HeaderIterator hi,
418 int_32 * tag, int_32 * type, const void ** p, int_32 * c)
421 int slot = hi->next_index;
422 struct indexEntry * entry = NULL;;
424 for (slot = hi->next_index; slot < h->indexUsed; slot++) {
425 entry = h->index + slot;
426 if (!ENTRY_IS_REGION(entry))
429 hi->next_index = slot;
430 if (entry == NULL || slot >= h->indexUsed)
435 *tag = entry->info.tag;
437 copyEntry(entry, type, p, c, 0);
442 static int indexCmp(const void *avp, const void *bvp) /*@*/
444 const struct indexEntry * ap = avp, * bp = bvp;
445 return (ap->info.tag - bp->info.tag);
448 void headerSort(Header h)
451 qsort(h->index, h->indexUsed, sizeof(*h->index), indexCmp);
456 static int offsetCmp(const void *avp, const void *bvp) /*@*/
458 const struct indexEntry * ap = avp, * bp = bvp;
459 int rc = (ap->info.offset - bp->info.offset);
462 rc = (ap->info.tag - bp->info.tag);
466 void headerUnsort(Header h)
468 qsort(h->index, h->indexUsed, sizeof(*h->index), offsetCmp);
471 Header headerCopy(Header h)
473 Header nh = headerNew();
475 int_32 tag, type, count;
478 for (hi = headerInitIterator(h);
479 headerNextIterator(hi, &tag, &type, &ptr, &count);
480 ptr = headerFreeData((void *)ptr, type))
482 headerAddEntry(nh, tag, type, ptr, count);
484 headerFreeIterator(hi);
486 return headerReload(nh, HEADER_IMAGE);
489 Header headerLoad(void *uh)
491 int_32 *ei = (int_32 *) uh;
492 int_32 il = ntohl(ei[0]); /* index length */
493 int_32 dl = ntohl(ei[1]); /* data length */
494 int pvlen = sizeof(il) + sizeof(dl) +
495 (il * sizeof(struct entryInfo)) + dl;
496 #ifdef REMALLOC_HEADER_REGION
497 void * pv = memcpy(xmalloc(pvlen), uh, pvlen);
501 Header h = xcalloc(1, sizeof(*h));
502 struct entryInfo * pe;
504 struct indexEntry * entry;
509 pe = (struct entryInfo *) &ei[2];
510 dataStart = (char *) (pe + il);
512 h->indexAlloced = il + 1;
514 h->index = xcalloc(h->indexAlloced, sizeof(*h->index));
516 #ifdef REMALLOC_HEADER_REGION
517 h->region_allocated = 1;
519 h->region_allocated = 0;
524 * XXX XFree86-libs, ash, and pdksh from Red Hat 5.2 have bogus
525 * %verifyscript tag that needs to be diddled.
527 if (ntohl(pe->tag) == 15 &&
528 ntohl(pe->type) == RPM_STRING_TYPE &&
529 ntohl(pe->count) == 1)
531 pe->tag = htonl(1079);
536 if (!(htonl(pe->tag) < HEADER_I18NTABLE)) {
538 entry->info.type = REGION_TAG_TYPE;
539 entry->info.tag = HEADER_IMAGE;
540 entry->info.count = REGION_TAG_COUNT;
541 entry->info.offset = ((char *)pe - dataStart); /* negative offset */
544 entry->length = pvlen - sizeof(il) - sizeof(dl);
545 rdlen = regionSwab(entry+1, il, 0, pe, dataStart, entry->info.offset);
546 entry->rdlen = rdlen;
552 int nb = ntohl(pe->count);
557 entry->info.type = htonl(pe->type);
558 if (entry->info.type < RPM_MIN_TYPE || entry->info.type > RPM_MAX_TYPE)
560 entry->info.count = htonl(pe->count);
562 { int off = ntohl(pe->offset);
564 int_32 * stei = memcpy(alloca(nb), dataStart + off, nb);
565 rdl = -ntohl(stei[2]); /* negative offset */
566 ril = rdl/sizeof(*pe);
567 entry->info.tag = htonl(pe->tag);
570 rdl = (ril * sizeof(struct entryInfo));
571 entry->info.tag = HEADER_IMAGE;
574 entry->info.offset = -rdl; /* negative offset */
577 entry->length = pvlen - sizeof(il) - sizeof(dl);
578 rdlen = regionSwab(entry+1, ril-1, 0, pe+1, dataStart, entry->info.offset);
579 entry->rdlen = rdlen;
581 if (ril < h->indexUsed) {
582 struct indexEntry * newEntry = entry + ril;
583 int ne = (h->indexUsed - ril);
584 int rid = entry->info.offset+1;
586 /* Load dribble entries from region. */
587 rdlen += regionSwab(newEntry, ne, 0, pe+ril, dataStart, rid);
589 { struct indexEntry * firstEntry = newEntry;
590 int save = h->indexUsed;
593 /* Dribble entries replace duplicate region entries. */
595 for (j = 0; j < ne; j++, newEntry++) {
596 headerRemoveEntry(h, newEntry->info.tag);
597 if (newEntry->info.tag == HEADER_BASENAMES)
598 headerRemoveEntry(h, HEADER_OLDFILENAMES);
601 /* If any duplicate entries were replaced, move new entries down. */
602 if (h->indexUsed < (save - ne)) {
603 memmove(h->index + h->indexUsed, firstEntry,
604 (ne * sizeof(*entry)));
617 Header headerCopyLoad(void *uh)
619 int_32 *ei = (int_32 *) uh;
620 int_32 il = ntohl(ei[0]); /* index length */
621 int_32 dl = ntohl(ei[1]); /* data length */
622 int pvlen = sizeof(il) + sizeof(dl) +
623 (il * sizeof(struct entryInfo)) + dl;
624 void * nuh = memcpy(xmalloc(pvlen), uh, pvlen);
632 h->region_allocated = 1;
637 int headerDrips(const Header h)
639 struct indexEntry * entry;
642 for (i = 0, entry = h->index; i < h->indexUsed; i++, entry++) {
643 if (ENTRY_IS_REGION(entry)) {
644 int rid = entry->info.offset;
646 for (; i < h->indexUsed && entry->info.offset <= rid+1; i++, entry++) {
647 if (entry->info.offset <= rid)
655 /* Ignore deleted drips. */
656 if (entry->data == NULL || entry->length <= 0)
663 static /*@only@*/ void * doHeaderUnload(Header h, /*@out@*/ int * lengthPtr)
664 /*@modifies h, *lengthPtr @*/
667 struct entryInfo * pe;
674 struct indexEntry * entry;
677 int drlen, ndribbles;
681 /* Sort entries by (offset,tag). */
684 /* Compute (il,dl) for all tags, including those deleted in region. */
686 drlen = ndribbles = driplen = ndrips = 0;
687 for (i = 0, entry = h->index; i < h->indexUsed; i++, entry++) {
688 if (ENTRY_IS_REGION(entry)) {
689 int_32 rdl = -entry->info.offset; /* negative offset */
690 int_32 ril = rdl/sizeof(*pe);
691 int rid = entry->info.offset;
694 dl += entry->rdlen + entry->info.count;
695 /* XXX Legacy regions do not include the region tag and data. */
696 if (i == 0 && h->legacy)
699 /* Skip rest of entries in region, but account for dribbles. */
700 for (; i < h->indexUsed && entry->info.offset <= rid+1; i++, entry++) {
701 if (entry->info.offset <= rid)
705 type = entry->info.type;
706 if (typeSizes[type] > 1) {
708 diff = typeSizes[type] - (dl % typeSizes[type]);
709 if (diff != typeSizes[type]) {
718 drlen += entry->length;
726 /* Ignore deleted drips. */
727 if (entry->data == NULL || entry->length <= 0)
731 type = entry->info.type;
732 if (typeSizes[type] > 1) {
734 diff = typeSizes[type] - (dl % typeSizes[type]);
735 if (diff != typeSizes[type]) {
745 driplen += entry->length;
748 len = sizeof(il) + sizeof(dl) + (il * sizeof(*pe)) + dl;
754 pe = (struct entryInfo *) &ei[2];
755 dataStart = te = (char *) (pe + il);
758 for (i = 0, entry = h->index; i < h->indexUsed; i++, entry++) {
764 if (entry->data == NULL || entry->length <= 0)
768 pe->tag = htonl(entry->info.tag);
769 pe->type = htonl(entry->info.type);
770 pe->count = htonl(entry->info.count);
772 if (ENTRY_IS_REGION(entry)) {
773 int_32 rdl = -entry->info.offset; /* negative offset */
774 int_32 ril = rdl/sizeof(*pe) + ndribbles;
775 int rid = entry->info.offset;
777 src = (char *)entry->data;
778 rdlen = entry->rdlen;
780 /* XXX Legacy regions do not include the region tag and data. */
781 if (i == 0 && h->legacy) {
785 memcpy(pe+1, src, rdl);
786 memcpy(te, src + rdl, rdlen);
789 pe->offset = htonl(te - dataStart);
792 stei[2] = htonl(-rdl-entry->info.count);
794 memcpy(te, stei, entry->info.count);
795 te += entry->info.count;
797 rdlen += entry->info.count;
799 count = regionSwab(NULL, ril, 0, pe, t, 0);
800 assert(count == rdlen);
804 memcpy(pe+1, src + sizeof(*pe), ((ril-1) * sizeof(*pe)));
805 memcpy(te, src + (ril * sizeof(*pe)), rdlen+entry->info.count+drlen);
807 { struct entryInfo * se = (struct entryInfo *)src;
808 int off = ntohl(se->offset);
809 pe->offset = (off) ? htonl(te - dataStart) : htonl(off);
811 te += entry->info.count + drlen;
813 count = regionSwab(NULL, ril, 0, pe, t, 0);
814 assert(count == rdlen+entry->info.count+drlen);
817 /* Skip rest of entries in region. */
818 while (i < h->indexUsed && entry->info.offset <= rid+1) {
828 /* Ignore deleted drips. */
829 if (entry->data == NULL || entry->length <= 0)
833 type = entry->info.type;
834 if (typeSizes[type] > 1) {
836 diff = typeSizes[type] - ((te - dataStart) % typeSizes[type]);
837 if (diff != typeSizes[type]) {
844 pe->offset = htonl(te - dataStart);
846 /* copy data w/ endian conversions */
847 switch (entry->info.type) {
849 count = entry->info.count;
852 *((int_32 *)te) = htonl(*((int_32 *)src));
853 te += sizeof(int_32);
854 src += sizeof(int_32);
859 count = entry->info.count;
862 *((int_16 *)te) = htons(*((int_16 *)src));
863 te += sizeof(int_16);
864 src += sizeof(int_16);
869 memcpy(te, entry->data, entry->length);
876 /* Insure that there are no memcpy underruns/overruns. */
877 assert(((char *)pe) == dataStart);
878 assert((((char *)ei)+len) == te);
889 void *headerUnload(Header h)
892 void * uh = doHeaderUnload(h, &length);
896 Header headerReload(Header h, int tag)
900 void * uh = doHeaderUnload(h, &length);
908 if (nh->region_allocated)
910 nh->region_allocated = 1;
911 if (ENTRY_IS_REGION(nh->index)) {
912 if (tag == HEADER_SIGNATURES || tag == HEADER_IMMUTABLE)
913 nh->index[0].info.tag = tag;
918 int headerWrite(FD_t fd, Header h, enum hMagic magicp)
924 uh = doHeaderUnload(h, &length);
926 case HEADER_MAGIC_YES:
927 nb = Fwrite(header_magic, sizeof(char), sizeof(header_magic), fd);
928 if (nb != sizeof(header_magic))
931 case HEADER_MAGIC_NO:
935 nb = Fwrite(uh, sizeof(char), length, fd);
939 return (nb == length ? 0 : 1);
942 Header headerRead(FD_t fd, enum hMagic magicp)
954 memset(block, 0, sizeof(block));
956 if (magicp == HEADER_MAGIC_YES)
959 if (timedRead(fd, (char *)block, i*sizeof(*block)) != (i * sizeof(*block)))
964 if (magicp == HEADER_MAGIC_YES) {
966 if (memcmp(&magic, header_magic, sizeof(magic)))
968 reserved = block[i++];
971 il = ntohl(block[i++]);
972 dl = ntohl(block[i++]);
974 len = sizeof(il) + sizeof(dl) + (il * sizeof(struct entryInfo)) + dl;
977 * XXX Limit total size of header to 32Mb (~16 times largest known size).
979 if (len > (32*1024*1024))
985 len -= sizeof(il) + sizeof(dl);
987 if (timedRead(fd, (char *)&ei[2], len) != len)
994 if (h->region_allocated)
996 h->region_allocated = 1;
1002 void headerDump(Header h, FILE *f, int flags,
1003 const struct headerTagTableEntry * tags)
1006 struct indexEntry *p;
1007 const struct headerTagTableEntry * tage;
1011 /* First write out the length of the index (count of index entries) */
1012 fprintf(f, "Entry count: %d\n", h->indexUsed);
1014 /* Now write the index */
1016 fprintf(f, "\n CT TAG TYPE "
1018 for (i = 0; i < h->indexUsed; i++) {
1019 switch (p->info.type) {
1020 case RPM_NULL_TYPE: type = "NULL_TYPE"; break;
1021 case RPM_CHAR_TYPE: type = "CHAR_TYPE"; break;
1022 case RPM_BIN_TYPE: type = "BIN_TYPE"; break;
1023 case RPM_INT8_TYPE: type = "INT8_TYPE"; break;
1024 case RPM_INT16_TYPE: type = "INT16_TYPE"; break;
1025 case RPM_INT32_TYPE: type = "INT32_TYPE"; break;
1026 /*case RPM_INT64_TYPE: type = "INT64_TYPE"; break;*/
1027 case RPM_STRING_TYPE: type = "STRING_TYPE"; break;
1028 case RPM_STRING_ARRAY_TYPE: type = "STRING_ARRAY_TYPE"; break;
1029 case RPM_I18NSTRING_TYPE: type = "I18N_STRING_TYPE"; break;
1030 default: type = "(unknown)"; break;
1034 while (tage->name && tage->val != p->info.tag) tage++;
1041 fprintf(f, "Entry : %.3d (%d)%-14s %-18s 0x%.8x %.8d\n", i,
1042 p->info.tag, tag, type, (unsigned) p->info.offset, (int)
1045 if (flags & HEADER_DUMP_INLINE) {
1047 int c = p->info.count;
1050 /* Print the data inline */
1051 switch (p->info.type) {
1052 case RPM_INT32_TYPE:
1054 fprintf(f, " Data: %.3d 0x%08x (%d)\n", ct++,
1055 (unsigned) *((int_32 *) dp),
1056 (int) *((int_32 *) dp));
1057 dp += sizeof(int_32);
1061 case RPM_INT16_TYPE:
1063 fprintf(f, " Data: %.3d 0x%04x (%d)\n", ct++,
1064 (unsigned) (*((int_16 *) dp) & 0xffff),
1065 (int) *((int_16 *) dp));
1066 dp += sizeof(int_16);
1071 fprintf(f, " Data: %.3d 0x%02x (%d)\n", ct++,
1072 (unsigned) (*((int_8 *) dp) & 0xff),
1073 (int) *((int_8 *) dp));
1074 dp += sizeof(int_8);
1079 fprintf(f, " Data: %.3d ", ct);
1081 fprintf(f, "%02x ", (unsigned) (*(int_8 *)dp & 0xff));
1083 dp += sizeof(int_8);
1093 char ch = (char) *((char *) dp);
1094 fprintf(f, " Data: %.3d 0x%2x %c (%d)\n", ct++,
1095 (unsigned)(ch & 0xff),
1096 (isprint(ch) ? ch : ' '),
1097 (int) *((char *) dp));
1101 case RPM_STRING_TYPE:
1102 case RPM_STRING_ARRAY_TYPE:
1103 case RPM_I18NSTRING_TYPE:
1105 fprintf(f, " Data: %.3d %s\n", ct++, (char *) dp);
1111 fprintf(stderr, _("Data type %d not supported\n"),
1112 (int) p->info.type);
1114 /*@notreached@*/ break;
1122 * Find matching (tag,type) entry in header.
1124 * @param tag entry tag
1125 * @param type entry type
1126 * @return header entry
1128 static struct indexEntry *findEntry(Header h, int_32 tag, int_32 type)
1130 struct indexEntry * entry, * entry2, * last;
1131 struct indexEntry key;
1133 if (!h->sorted) headerSort(h);
1138 bsearch(&key, h->index, h->indexUsed, sizeof(*h->index), indexCmp);
1142 if (type == RPM_NULL_TYPE)
1145 /* look backwards */
1146 while (entry->info.tag == tag && entry->info.type != type &&
1147 entry > h->index) entry--;
1149 if (entry->info.tag == tag && entry->info.type == type)
1152 last = h->index + h->indexUsed;
1153 while (entry2->info.tag == tag && entry2->info.type != type &&
1154 entry2 < last) entry2++;
1156 if (entry->info.tag == tag && entry->info.type == type)
1162 int headerIsEntry(Header h, int_32 tag)
1164 return (findEntry(h, tag, RPM_NULL_TYPE) ? 1 : 0);
1167 int headerGetRawEntry(Header h, int_32 tag, int_32 * type, const void ** p,
1170 struct indexEntry * entry;
1172 if (p == NULL) return headerIsEntry(h, tag);
1174 /* First find the tag */
1175 entry = findEntry(h, tag, RPM_NULL_TYPE);
1182 copyEntry(entry, type, p, c, 0);
1188 * Does locale match entry in header i18n table?
1191 * The range [l,le) contains the next locale to match:
1192 * ll[_CC][.EEEEE][@dddd]
1194 * ll ISO language code (in lowercase).
1195 * CC (optional) ISO coutnry code (in uppercase).
1196 * EEEEE (optional) encoding (not really standardized).
1197 * dddd (optional) dialect.
1200 * @param td header i18n table data, NUL terminated
1201 * @param l start of locale to match
1202 * @param le end of locale to match
1203 * @return 1 on match, 0 on no match
1205 static int headerMatchLocale(const char *td, const char *l, const char *le)
1212 { const char *s, *ll, *CC, *EE, *dd;
1215 /* Copy the buffer and parse out components on the fly. */
1216 lbuf = alloca(le - l + 1);
1217 for (s = l, ll = t = lbuf; *s; s++, t++) {
1237 if (ll) /* ISO language should be lower case */
1238 for (t = ll; *t; t++) *t = tolower(*t);
1239 if (CC) /* ISO country code should be upper case */
1240 for (t = CC; *t; t++) *t = toupper(*t);
1242 /* There are a total of 16 cases to attempt to match. */
1246 /* First try a complete match. */
1247 if (strlen(td) == (le-l) && !strncmp(td, l, (le - l)))
1250 /* Next, try stripping optional dialect and matching. */
1251 for (fe = l; fe < le && *fe != '@'; fe++)
1253 if (fe < le && !strncmp(td, l, (fe - l)))
1256 /* Next, try stripping optional codeset and matching. */
1257 for (fe = l; fe < le && *fe != '.'; fe++)
1259 if (fe < le && !strncmp(td, l, (fe - l)))
1262 /* Finally, try stripping optional country code and matching. */
1263 for (fe = l; fe < le && *fe != '_'; fe++)
1265 if (fe < le && !strncmp(td, l, (fe - l)))
1272 * Return i18n string from header that matches locale.
1274 * @param entry i18n string data
1275 * @return matching i18n string (or 1st string if no match)
1277 /*@dependent@*/ static char *
1278 headerFindI18NString(Header h, struct indexEntry *entry)
1280 const char *lang, *l, *le;
1281 struct indexEntry * table;
1283 /* XXX Drepper sez' this is the order. */
1284 if ((lang = getenv("LANGUAGE")) == NULL &&
1285 (lang = getenv("LC_ALL")) == NULL &&
1286 (lang = getenv("LC_MESSAGES")) == NULL &&
1287 (lang = getenv("LANG")) == NULL)
1290 if ((table = findEntry(h, HEADER_I18NTABLE, RPM_STRING_ARRAY_TYPE)) == NULL)
1293 for (l = lang; *l != '\0'; l = le) {
1298 while (*l && *l == ':') /* skip leading colons */
1302 for (le = l; *le && *le != ':'; le++) /* find end of this locale */
1305 /* For each entry in the header ... */
1306 for (langNum = 0, td = table->data, ed = entry->data;
1307 langNum < entry->info.count;
1308 langNum++, td += strlen(td) + 1, ed += strlen(ed) + 1) {
1310 if (headerMatchLocale(td, l, le))
1320 * Retrieve tag data from header.
1322 * @param tag tag to retrieve
1323 * @retval type address of type (or NULL)
1324 * @retval p address of data (or NULL)
1325 * @retval c address of count (or NULL)
1326 * @param minMem string pointers reference header memory?
1327 * @return 1 on success, 0 on not found
1329 static int intGetEntry(Header h, int_32 tag, /*@out@*/ int_32 *type,
1330 /*@out@*/ const void **p, /*@out@*/ int_32 *c, int minMem)
1331 /*@modifies *type, *p, *c @*/
1333 struct indexEntry * entry;
1335 /* First find the tag */
1336 entry = findEntry(h, tag, RPM_NULL_TYPE);
1337 if (entry == NULL) {
1344 switch (entry->info.type) {
1345 case RPM_I18NSTRING_TYPE:
1346 if (type) *type = RPM_STRING_TYPE;
1348 /*@-dependenttrans@*/
1349 if (p) *p = headerFindI18NString(h, entry);
1350 /*@=dependenttrans@*/
1353 copyEntry(entry, type, p, c, minMem);
1360 int headerGetEntryMinMemory(Header h, int_32 tag, int_32 *type, const void **p,
1363 return intGetEntry(h, tag, type, p, c, 1);
1366 int headerGetEntry(Header h, int_32 tag, int_32 * type, void **p, int_32 * c)
1368 return intGetEntry(h, tag, type, (const void **)p, c, 0);
1373 Header h = xcalloc(1, sizeof(*h));
1375 h->indexAlloced = INDEX_MALLOC_SIZE;
1377 h->region_allocated = 0;
1382 h->index = (h->indexAlloced
1383 ? xcalloc(h->indexAlloced, sizeof(*h->index))
1389 void headerFree(Header h)
1391 if (h == NULL || --h->nrefs > 0)
1395 struct indexEntry * entry = h->index;
1397 for (i = 0; i < h->indexUsed; i++, entry++) {
1398 if (h->region_allocated && ENTRY_IS_REGION(entry)) {
1399 if (entry->length > 0) {
1400 int_32 * ei = entry->data;
1401 ei -= 2; /* XXX HACK: adjust to beginning of header. */
1404 } else if (!ENTRY_IN_REGION(entry)) {
1413 /*@-refcounttrans@*/ free(h); /*@=refcounttrans@*/
1416 Header headerLink(Header h)
1419 /*@-refcounttrans@*/ return h; /*@=refcounttrans@*/
1422 int headerUsageCount(Header h)
1427 unsigned int headerSizeof(Header h, enum hMagic magicp)
1429 struct indexEntry * entry;
1430 unsigned int size = 0, pad = 0;
1436 case HEADER_MAGIC_YES:
1437 size += sizeof(header_magic);
1439 case HEADER_MAGIC_NO:
1443 size += 2 * sizeof(int_32); /* count of index entries */
1445 for (i = 0, entry = h->index; i < h->indexUsed; i++, entry++) {
1449 /* Regions go in as is ... */
1450 if (ENTRY_IS_REGION(entry)) {
1451 size += entry->length;
1452 /* XXX Legacy regions do not include the region tag and data. */
1453 if (i == 0 && h->legacy)
1454 size += sizeof(struct entryInfo) + entry->info.count;
1458 /* ... and region elements are skipped. */
1459 if (entry->info.offset < 0)
1463 type = entry->info.type;
1464 if (typeSizes[type] > 1) {
1465 diff = typeSizes[type] - (size % typeSizes[type]);
1466 if (diff != typeSizes[type]) {
1472 size += sizeof(struct entryInfo) + entry->length;
1478 static void copyData(int_32 type, /*@out@*/ void * dstPtr, const void * srcPtr,
1479 int_32 c, int dataLength)
1480 /*@modifies *dstPtr @*/
1487 case RPM_STRING_ARRAY_TYPE:
1488 case RPM_I18NSTRING_TYPE:
1489 /* Otherwise, p is char** */
1491 src = (const char **) srcPtr;
1494 len = *src ? strlen(*src) + 1 : 0;
1495 memcpy(dst, *src, len);
1502 memcpy(dstPtr, srcPtr, dataLength);
1508 * Return (malloc'ed) copy of entry data.
1509 * @param type entry data type
1510 * @param p entry data
1511 * @param c entry item count
1512 * @retval lengthPtr no. bytes in returned data
1513 * @return (malloc'ed) copy of entry data
1515 static void * grabData(int_32 type, const void * p, int_32 c,
1516 /*@out@*/ int * lengthPtr)
1517 /*@modifies *lengthPtr @*/
1519 int length = dataLength(type, p, c, 0);
1520 void * data = xmalloc(length);
1522 copyData(type, data, p, c, length);
1525 *lengthPtr = length;
1529 int headerAddEntry(Header h, int_32 tag, int_32 type, const void *p, int_32 c)
1531 struct indexEntry *entry;
1534 fprintf(stderr, _("Bad count for headerAddEntry(): %d\n"), (int) c);
1539 /* Allocate more index space if necessary */
1540 if (h->indexUsed == h->indexAlloced) {
1541 h->indexAlloced += INDEX_MALLOC_SIZE;
1542 h->index = xrealloc(h->index,
1543 h->indexAlloced * sizeof(struct indexEntry));
1546 /* Fill in the index */
1547 entry = h->index + h->indexUsed;
1548 entry->info.tag = tag;
1549 entry->info.type = type;
1550 entry->info.count = c;
1551 entry->info.offset = 0;
1552 entry->data = grabData(type, p, c, &entry->length);
1554 if (h->indexUsed > 0 && tag < h->index[h->indexUsed-1].info.tag)
1562 headerGetLangs(Header h)
1564 char **s, *e, **table;
1567 if (!headerGetRawEntry(h, HEADER_I18NTABLE, &type, (const void **)&s, &count))
1570 if ((table = (char **)xcalloc((count+1), sizeof(char *))) == NULL)
1573 for (i = 0, e = *s; i < count > 0; i++, e += strlen(e)+1)
1575 table[count] = NULL;
1580 int headerAddI18NString(Header h, int_32 tag, const char * string, const char * lang)
1582 struct indexEntry * table, * entry;
1584 const char ** strArray;
1590 table = findEntry(h, HEADER_I18NTABLE, RPM_STRING_ARRAY_TYPE);
1591 entry = findEntry(h, tag, RPM_I18NSTRING_TYPE);
1593 if (!table && entry)
1594 return 0; /* this shouldn't ever happen!! */
1596 if (!table && !entry) {
1597 const char * charArray[2];
1599 if (!lang || (lang[0] == 'C' && lang[1] == '\0')) {
1600 /*@-observertrans@*/
1601 charArray[count++] = "C";
1602 /*@=observertrans@*/
1604 /*@-observertrans@*/
1605 charArray[count++] = "C";
1606 /*@=observertrans@*/
1607 charArray[count++] = lang;
1609 if (!headerAddEntry(h, HEADER_I18NTABLE, RPM_STRING_ARRAY_TYPE,
1612 table = findEntry(h, HEADER_I18NTABLE, RPM_STRING_ARRAY_TYPE);
1615 if (!lang) lang = "C";
1617 chptr = table->data;
1618 for (langNum = 0; langNum < table->info.count; langNum++) {
1619 if (!strcmp(chptr, lang)) break;
1620 chptr += strlen(chptr) + 1;
1623 if (langNum >= table->info.count) {
1624 length = strlen(lang) + 1;
1625 if (ENTRY_IN_REGION(table)) {
1626 char * t = xmalloc(table->length + length);
1627 memcpy(t, table->data, table->length);
1629 table->info.offset = 0;
1631 table->data = xrealloc(table->data, table->length + length);
1632 memcpy(((char *)table->data) + table->length, lang, length);
1633 table->length += length;
1634 table->info.count++;
1638 strArray = alloca(sizeof(*strArray) * (langNum + 1));
1639 for (i = 0; i < langNum; i++)
1641 strArray[langNum] = string;
1642 return headerAddEntry(h, tag, RPM_I18NSTRING_TYPE, strArray,
1644 } else if (langNum >= entry->info.count) {
1645 ghosts = langNum - entry->info.count;
1647 length = strlen(string) + 1 + ghosts;
1648 if (ENTRY_IN_REGION(entry)) {
1649 char * t = xmalloc(entry->length + length);
1650 memcpy(t, entry->data, entry->length);
1652 entry->info.offset = 0;
1654 entry->data = xrealloc(entry->data, entry->length + length);
1656 memset(((char *)entry->data) + entry->length, '\0', ghosts);
1657 strcpy(((char *)entry->data) + entry->length + ghosts, string);
1659 entry->length += length;
1660 entry->info.count = langNum + 1;
1662 char *b, *be, *e, *ee, *t;
1665 /* Set beginning/end pointers to previous data */
1666 b = be = e = ee = entry->data;
1667 for (i = 0; i < table->info.count; i++) {
1670 ee += strlen(ee) + 1;
1675 /* Get storage for new buffer */
1677 sn = strlen(string) + 1;
1679 length = bn + sn + en;
1680 t = buf = xmalloc(length);
1682 /* Copy values into new storage */
1685 memcpy(t, string, sn);
1690 /* Replace I18N string array */
1691 entry->length -= strlen(be) + 1;
1692 entry->length += sn;
1694 if (ENTRY_IN_REGION(entry)) {
1695 entry->info.offset = 0;
1704 /* if there are multiple entries with this tag, the first one gets replaced */
1705 int headerModifyEntry(Header h, int_32 tag, int_32 type, void *p, int_32 c)
1707 struct indexEntry *entry;
1710 /* First find the tag */
1711 entry = findEntry(h, tag, type);
1715 /* make sure entry points to the first occurence of this tag */
1716 while (entry > h->index && (entry - 1)->info.tag == tag)
1719 /* free after we've grabbed the new data in case the two are intertwined;
1720 that's a bad idea but at least we won't break */
1721 oldData = entry->data;
1723 entry->info.count = c;
1724 entry->info.type = type;
1725 entry->data = grabData(type, p, c, &entry->length);
1727 if (ENTRY_IN_REGION(entry)) {
1728 entry->info.offset = 0;
1735 int headerAddOrAppendEntry(Header h, int_32 tag, int_32 type,
1738 return (findEntry(h, tag, type)
1739 ? headerAppendEntry(h, tag, type, p, c)
1740 : headerAddEntry(h, tag, type, p, c));
1743 int headerAppendEntry(Header h, int_32 tag, int_32 type, void * p, int_32 c)
1745 struct indexEntry *entry;
1748 /* First find the tag */
1749 entry = findEntry(h, tag, type);
1753 if (type == RPM_STRING_TYPE || type == RPM_I18NSTRING_TYPE) {
1754 /* we can't do this */
1758 length = dataLength(type, p, c, 0);
1760 if (ENTRY_IN_REGION(entry)) {
1761 char * t = xmalloc(entry->length + length);
1762 memcpy(t, entry->data, entry->length);
1764 entry->info.offset = 0;
1766 entry->data = xrealloc(entry->data, entry->length + length);
1768 copyData(type, ((char *) entry->data) + entry->length, p, c, length);
1770 entry->length += length;
1772 entry->info.count += c;
1777 int headerRemoveEntry(Header h, int_32 tag)
1779 struct indexEntry * last = h->index + h->indexUsed;
1780 struct indexEntry * entry, * first;
1783 entry = findEntry(h, tag, RPM_NULL_TYPE);
1784 if (!entry) return 1;
1786 /* Make sure entry points to the first occurence of this tag. */
1787 while (entry > h->index && (entry - 1)->info.tag == tag)
1790 /* Free data for tags being removed. */
1791 for (first = entry; first < last; first++) {
1793 if (first->info.tag != tag)
1798 if (ENTRY_IN_REGION(first))
1803 ne = (first - entry);
1808 memmove(entry, first, (ne * sizeof(*entry)));
1814 static char escapedChar(const char ch) /*@*/
1817 case 'a': return '\a';
1818 case 'b': return '\b';
1819 case 'f': return '\f';
1820 case 'n': return '\n';
1821 case 'r': return '\r';
1822 case 't': return '\t';
1823 case 'v': return '\v';
1828 static void freeFormat( /*@only@*/ struct sprintfToken * format, int num)
1832 for (i = 0; i < num; i++) {
1833 switch (format[i].type) {
1835 freeFormat(format[i].u.array.format, format[i].u.array.numTokens);
1838 freeFormat(format[i].u.cond.ifFormat,
1839 format[i].u.cond.numIfTokens);
1840 freeFormat(format[i].u.cond.elseFormat,
1841 format[i].u.cond.numElseTokens);
1853 static void findTag(char * name, const struct headerTagTableEntry * tags,
1854 const struct headerSprintfExtension * extensions,
1855 /*@out@*/const struct headerTagTableEntry ** tagMatch,
1856 /*@out@*/const struct headerSprintfExtension ** extMatch)
1857 /*@modifies *tagMatch, *extMatch @*/
1859 const struct headerTagTableEntry * entry;
1860 const struct headerSprintfExtension * ext;
1861 const char * tagname;
1866 if (strncmp("RPMTAG_", name, sizeof("RPMTAG_")-1)) {
1867 char * t = alloca(strlen(name) + sizeof("RPMTAG_"));
1868 (void) stpcpy( stpcpy(t, "RPMTAG_"), name);
1874 /* Search extensions first to permit overriding header tags. */
1876 while (ext->type != HEADER_EXT_LAST) {
1877 if (ext->type == HEADER_EXT_TAG && !strcasecmp(ext->name, tagname))
1880 if (ext->type == HEADER_EXT_MORE)
1886 if (ext->type == HEADER_EXT_TAG) {
1891 /* Search header tags. */
1892 for (entry = tags; entry->name; entry++)
1893 if (!strcasecmp(entry->name, tagname)) break;
1902 static int parseExpression(struct sprintfToken * token, char * str,
1903 const struct headerTagTableEntry * tags,
1904 const struct headerSprintfExtension * extensions,
1905 /*@out@*/char ** endPtr, /*@out@*/const char ** errmsg)
1906 /*@modifies str, *str, *token, *endPtr, *errmsg @*/;
1908 static int parseFormat(char * str, const struct headerTagTableEntry * tags,
1909 const struct headerSprintfExtension * extensions,
1910 /*@out@*/struct sprintfToken ** formatPtr, /*@out@*/int * numTokensPtr,
1911 /*@out@*/char ** endPtr, int state, /*@out@*/const char ** errmsg)
1912 /*@modifies str, *str, *formatPtr, *numTokensPtr, *endPtr, *errmsg @*/
1914 char * chptr, * start, * next, * dst;
1915 struct sprintfToken * format;
1918 const struct headerTagTableEntry * tag;
1919 const struct headerSprintfExtension * ext;
1923 /* upper limit on number of individual formats */
1925 for (chptr = str; *chptr != '\0'; chptr++)
1926 if (*chptr == '%') numTokens++;
1927 numTokens = numTokens * 2 + 1;
1929 format = xcalloc(numTokens, sizeof(*format));
1930 if (endPtr) *endPtr = NULL;
1935 while (*start != '\0') {
1939 if (*(start + 1) == '%') {
1940 if (currToken < 0 || format[currToken].type != PTOK_STRING) {
1942 format[currToken].type = PTOK_STRING;
1943 dst = format[currToken].u.string.string = start;
1950 break; /* out of switch */
1957 if (*start == '|') {
1961 if (parseExpression(format + currToken, start, tags,
1962 extensions, &newEnd, errmsg)) {
1963 freeFormat(format, numTokens);
1967 break; /* out of switch */
1970 format[currToken].u.tag.format = start;
1971 format[currToken].u.tag.pad = 0;
1972 format[currToken].u.tag.justOne = 0;
1973 format[currToken].u.tag.arrayCount = 0;
1976 while (*chptr && *chptr != '{' && *chptr != '%') chptr++;
1977 if (!*chptr || *chptr == '%') {
1978 /*@-observertrans@*/
1979 *errmsg = _("missing { after %");
1980 /*@=observertrans@*/
1981 freeFormat(format, numTokens);
1987 while (start < chptr) {
1988 if (xisdigit(*start)) {
1989 i = strtoul(start, &start, 10);
1990 format[currToken].u.tag.pad += i;
1996 if (*start == '=') {
1997 format[currToken].u.tag.justOne = 1;
1999 } else if (*start == '#') {
2000 format[currToken].u.tag.justOne = 1;
2001 format[currToken].u.tag.arrayCount = 1;
2006 while (*next && *next != '}') next++;
2008 /*@-observertrans@*/
2009 *errmsg = _("missing } after %{");
2010 /*@=observertrans@*/
2011 freeFormat(format, numTokens);
2017 while (*chptr && *chptr != ':') chptr++;
2019 if (*chptr != '\0') {
2022 /*@-observertrans@*/
2023 *errmsg = _("empty tag format");
2024 /*@=observertrans@*/
2025 freeFormat(format, numTokens);
2028 format[currToken].u.tag.type = chptr;
2030 format[currToken].u.tag.type = NULL;
2034 /*@-observertrans@*/
2035 *errmsg = _("empty tag name");
2036 /*@=observertrans@*/
2037 freeFormat(format, numTokens);
2042 findTag(start, tags, extensions, &tag, &ext);
2045 format[currToken].u.tag.ext = NULL;
2046 format[currToken].u.tag.tag = tag->val;
2048 format[currToken].u.tag.ext = ext->u.tagFunction;
2049 format[currToken].u.tag.extNum = ext - extensions;
2051 /*@-observertrans@*/
2052 *errmsg = _("unknown tag");
2053 /*@=observertrans@*/
2054 freeFormat(format, numTokens);
2058 format[currToken].type = PTOK_TAG;
2069 if (parseFormat(start, tags, extensions,
2070 &format[currToken].u.array.format,
2071 &format[currToken].u.array.numTokens,
2072 &start, PARSER_IN_ARRAY, errmsg)) {
2073 freeFormat(format, numTokens);
2078 /*@-observertrans@*/
2079 *errmsg = _("] expected at end of array");
2080 /*@=observertrans@*/
2081 freeFormat(format, numTokens);
2087 format[currToken].type = PTOK_ARRAY;
2093 if ((*start == ']' && state != PARSER_IN_ARRAY) ||
2094 (*start == '}' && state != PARSER_IN_EXPR)) {
2096 /*@-observertrans@*/
2097 *errmsg = _("unexpected ]");
2098 /*@=observertrans@*/
2100 /*@-observertrans@*/
2101 *errmsg = _("unexpected }");
2102 /*@=observertrans@*/
2103 freeFormat(format, numTokens);
2112 if (currToken < 0 || format[currToken].type != PTOK_STRING) {
2114 format[currToken].type = PTOK_STRING;
2115 dst = format[currToken].u.string.string = start;
2118 if (*start == '\\') {
2120 *dst++ = escapedChar(*start++);
2134 for (i = 0; i < currToken; i++) {
2135 if (format[i].type == PTOK_STRING)
2136 format[i].u.string.len = strlen(format[i].u.string.string);
2139 *numTokensPtr = currToken;
2140 *formatPtr = format;
2145 static int parseExpression(struct sprintfToken * token, char * str,
2146 const struct headerTagTableEntry * tags,
2147 const struct headerSprintfExtension * extensions,
2148 /*@out@*/ char ** endPtr, /*@out@*/ const char ** errmsg)
2150 const struct headerTagTableEntry * tag;
2151 const struct headerSprintfExtension * ext;
2157 while (*chptr && *chptr != '?') chptr++;
2159 if (*chptr != '?') {
2160 /*@-observertrans@*/
2161 *errmsg = _("? expected in expression");
2162 /*@=observertrans@*/
2168 if (*chptr != '{') {
2169 /*@-observertrans@*/
2170 *errmsg = _("{ expected after ? in expression");
2171 /*@=observertrans@*/
2177 if (parseFormat(chptr, tags, extensions, &token->u.cond.ifFormat,
2178 &token->u.cond.numIfTokens, &end, PARSER_IN_EXPR, errmsg))
2182 /*@-observertrans@*/
2183 *errmsg = _("} expected in expression");
2184 /*@=observertrans@*/
2185 freeFormat(token->u.cond.ifFormat, token->u.cond.numIfTokens);
2186 token->u.cond.ifFormat = NULL;
2191 if (*chptr != ':' && *chptr != '|') {
2192 /*@-observertrans@*/
2193 *errmsg = _(": expected following ? subexpression");
2194 /*@=observertrans@*/
2195 freeFormat(token->u.cond.ifFormat, token->u.cond.numIfTokens);
2196 token->u.cond.ifFormat = NULL;
2200 if (*chptr == '|') {
2201 parseFormat(xstrdup(""), tags, extensions, &token->u.cond.elseFormat,
2202 &token->u.cond.numElseTokens, &end, PARSER_IN_EXPR,
2207 if (*chptr != '{') {
2208 /*@-observertrans@*/
2209 *errmsg = _("{ expected after : in expression");
2210 /*@=observertrans@*/
2211 freeFormat(token->u.cond.ifFormat, token->u.cond.numIfTokens);
2212 token->u.cond.ifFormat = NULL;
2218 if (parseFormat(chptr, tags, extensions, &token->u.cond.elseFormat,
2219 &token->u.cond.numElseTokens, &end, PARSER_IN_EXPR,
2223 /*@-observertrans@*/
2224 *errmsg = _("} expected in expression");
2225 /*@=observertrans@*/
2226 freeFormat(token->u.cond.ifFormat, token->u.cond.numIfTokens);
2227 token->u.cond.ifFormat = NULL;
2232 if (*chptr != '|') {
2233 /*@-observertrans@*/
2234 *errmsg = _("| expected at end of expression");
2235 /*@=observertrans@*/
2236 freeFormat(token->u.cond.ifFormat, token->u.cond.numIfTokens);
2237 token->u.cond.ifFormat = NULL;
2238 freeFormat(token->u.cond.elseFormat, token->u.cond.numElseTokens);
2239 token->u.cond.elseFormat = NULL;
2248 findTag(str, tags, extensions, &tag, &ext);
2251 token->u.cond.tag.ext = NULL;
2252 token->u.cond.tag.tag = tag->val;
2254 token->u.cond.tag.ext = ext->u.tagFunction;
2255 token->u.cond.tag.extNum = ext - extensions;
2257 token->u.cond.tag.ext = NULL;
2258 token->u.cond.tag.tag = -1;
2261 token->type = PTOK_COND;
2266 static int getExtension(Header h, headerTagTagFunction fn,
2267 /*@out@*/ int_32 * typeptr, /*@out@*/ const void ** data,
2268 /*@out@*/ int_32 * countptr, struct extensionCache * ext)
2269 /*@modifies *typeptr, *data, *countptr, ext->avail @*/
2272 if (fn(h, &ext->type, &ext->data, &ext->count, &ext->freeit))
2277 *typeptr = ext->type;
2279 *countptr = ext->count;
2284 static char * formatValue(struct sprintfTag * tag, Header h,
2285 const struct headerSprintfExtension * extensions,
2286 struct extensionCache * extCache, int element)
2287 /*@modifies h, extCache->avail @*/
2293 unsigned int intVal;
2295 const char ** strarray;
2298 headerTagFormatFunction tagtype = NULL;
2299 const struct headerSprintfExtension * ext;
2302 if (getExtension(h, tag->ext, &type, &data, &count,
2303 extCache + tag->extNum)) {
2305 type = RPM_STRING_TYPE;
2306 data = "(none)"; /* XXX i18n? NO!, sez; gafton */
2309 if (!headerGetEntry(h, tag->tag, &type, (void **)&data, &count)){
2311 type = RPM_STRING_TYPE;
2312 data = "(none)"; /* XXX i18n? NO!, sez; gafton */
2318 if (tag->arrayCount) {
2319 /*@-observertrans -modobserver@*/
2320 data = headerFreeData(data, type);
2321 /*@=observertrans =modobserver@*/
2326 type = RPM_INT32_TYPE;
2329 (void) stpcpy( stpcpy(buf, "%"), tag->format);
2333 while (ext->type != HEADER_EXT_LAST) {
2334 if (ext->type == HEADER_EXT_FORMAT &&
2335 !strcmp(ext->name, tag->type)) {
2336 tagtype = ext->u.formatFunction;
2340 if (ext->type == HEADER_EXT_MORE)
2348 case RPM_STRING_ARRAY_TYPE:
2349 strarray = (const char **)data;
2352 val = tagtype(RPM_STRING_TYPE, strarray[element], buf, tag->pad, 0);
2357 len = strlen(strarray[element]) + tag->pad + 20;
2359 sprintf(val, buf, strarray[element]);
2362 /*@-observertrans -modobserver@*/
2363 if (mayfree) free((void *)data);
2364 /*@=observertrans =modobserver@*/
2368 case RPM_STRING_TYPE:
2370 val = tagtype(RPM_STRING_ARRAY_TYPE, data, buf, tag->pad, 0);
2375 len = strlen(data) + tag->pad + 20;
2377 sprintf(val, buf, data);
2383 case RPM_INT16_TYPE:
2384 case RPM_INT32_TYPE:
2387 case RPM_INT8_TYPE: intVal = *(((int_8 *) data) + element); break;
2388 case RPM_INT16_TYPE: intVal = *(((uint_16 *) data) + element); break;
2389 default: /* keep -Wall quiet */
2390 case RPM_INT32_TYPE: intVal = *(((int_32 *) data) + element); break;
2394 val = tagtype(RPM_INT32_TYPE, &intVal, buf, tag->pad, element);
2398 len = 10 + tag->pad + 20;
2400 sprintf(val, buf, intVal);
2405 val = xstrdup(_("(unknown type)"));
2412 static const char * singleSprintf(Header h, struct sprintfToken * token,
2413 const struct headerSprintfExtension * extensions,
2414 struct extensionCache * extCache, int element)
2415 /*@modifies h, extCache->avail @*/
2418 const char * thisItem;
2424 struct sprintfToken * condFormat;
2427 /* we assume the token and header have been validated already! */
2429 switch (token->type) {
2434 val = xmalloc(token->u.string.len + 1);
2435 strcpy(val, token->u.string.string);
2439 val = formatValue(&token->u.tag, h, extensions, extCache,
2440 token->u.tag.justOne ? 0 : element);
2444 if (token->u.cond.tag.ext ||
2445 headerIsEntry(h, token->u.cond.tag.tag)) {
2446 condFormat = token->u.cond.ifFormat;
2447 condNumFormats = token->u.cond.numIfTokens;
2449 condFormat = token->u.cond.elseFormat;
2450 condNumFormats = token->u.cond.numElseTokens;
2453 alloced = condNumFormats * 20;
2454 val = xmalloc(alloced ? alloced : 1);
2458 for (i = 0; i < condNumFormats; i++) {
2459 thisItem = singleSprintf(h, condFormat + i,
2460 extensions, extCache, element);
2461 thisItemLen = strlen(thisItem);
2462 if ((thisItemLen + len) >= alloced) {
2463 alloced = (thisItemLen + len) + 200;
2464 val = xrealloc(val, alloced);
2466 strcat(val, thisItem);
2468 free((void *)thisItem);
2475 for (i = 0; i < token->u.array.numTokens; i++) {
2476 if (token->u.array.format[i].type != PTOK_TAG ||
2477 token->u.array.format[i].u.tag.arrayCount ||
2478 token->u.array.format[i].u.tag.justOne) continue;
2480 if (token->u.array.format[i].u.tag.ext) {
2482 if (getExtension(h, token->u.array.format[i].u.tag.ext,
2483 &type, &data, &numElements,
2485 token->u.array.format[i].u.tag.extNum))
2488 if (!headerGetEntry(h, token->u.array.format[i].u.tag.tag,
2489 &type, (void **) &val, &numElements))
2491 val = headerFreeData(val, type);
2496 if (numElements == -1) {
2497 val = xstrdup("(none)"); /* XXX i18n? NO!, sez; gafton */
2499 alloced = numElements * token->u.array.numTokens * 20;
2500 val = xmalloc(alloced);
2504 for (j = 0; j < numElements; j++) {
2505 for (i = 0; i < token->u.array.numTokens; i++) {
2506 thisItem = singleSprintf(h, token->u.array.format + i,
2507 extensions, extCache, j);
2508 thisItemLen = strlen(thisItem);
2509 if ((thisItemLen + len) >= alloced) {
2510 alloced = (thisItemLen + len) + 200;
2511 val = xrealloc(val, alloced);
2513 strcat(val, thisItem);
2515 free((void *)thisItem);
2526 static struct extensionCache * allocateExtensionCache(
2527 const struct headerSprintfExtension * extensions)
2530 const struct headerSprintfExtension * ext = extensions;
2533 while (ext->type != HEADER_EXT_LAST) {
2535 if (ext->type == HEADER_EXT_MORE)
2541 return xcalloc(i, sizeof(struct extensionCache));
2544 static void freeExtensionCache(const struct headerSprintfExtension * extensions,
2545 /*@only@*/struct extensionCache * cache)
2547 const struct headerSprintfExtension * ext = extensions;
2550 while (ext->type != HEADER_EXT_LAST) {
2551 if (cache[i].freeit) free((void *)cache[i].data);
2554 if (ext->type == HEADER_EXT_MORE)
2563 char * headerSprintf(Header h, const char * origFmt,
2564 const struct headerTagTableEntry * tags,
2565 const struct headerSprintfExtension * extensions,
2566 const char ** errmsg)
2569 struct sprintfToken * format;
2575 struct extensionCache * extCache;
2577 /*fmtString = escapeString(origFmt);*/
2578 fmtString = xstrdup(origFmt);
2580 if (parseFormat(fmtString, tags, extensions, &format, &numTokens,
2581 NULL, PARSER_BEGIN, errmsg)) {
2586 extCache = allocateExtensionCache(extensions);
2588 answerAlloced = 1024;
2590 answer = xmalloc(answerAlloced);
2593 for (i = 0; i < numTokens; i++) {
2597 piece = singleSprintf(h, format + i, extensions, extCache, 0);
2599 pieceLength = strlen(piece);
2600 if ((answerLength + pieceLength) >= answerAlloced) {
2601 while ((answerLength + pieceLength) >= answerAlloced)
2602 answerAlloced += 1024;
2603 answer = xrealloc(answer, answerAlloced);
2606 strcat(answer, piece);
2607 answerLength += pieceLength;
2608 free((void *)piece);
2613 freeExtensionCache(extensions, extCache);
2619 static char * octalFormat(int_32 type, const void * data,
2620 char * formatPrefix, int padding, /*@unused@*/int element)
2621 /*@modifies formatPrefix @*/
2625 if (type != RPM_INT32_TYPE) {
2626 val = xstrdup(_("(not a number)"));
2628 val = xmalloc(20 + padding);
2629 strcat(formatPrefix, "o");
2630 sprintf(val, formatPrefix, *((int_32 *) data));
2636 static char * hexFormat(int_32 type, const void * data,
2637 char * formatPrefix, int padding, /*@unused@*/int element)
2638 /*@modifies formatPrefix @*/
2642 if (type != RPM_INT32_TYPE) {
2643 val = xstrdup(_("(not a number)"));
2645 val = xmalloc(20 + padding);
2646 strcat(formatPrefix, "x");
2647 sprintf(val, formatPrefix, *((int_32 *) data));
2653 static char * realDateFormat(int_32 type, const void * data,
2654 char * formatPrefix, int padding, /*@unused@*/int element,
2655 char * strftimeFormat)
2656 /*@modifies formatPrefix @*/
2659 struct tm * tstruct;
2662 if (type != RPM_INT32_TYPE) {
2663 val = xstrdup(_("(not a number)"));
2665 val = xmalloc(50 + padding);
2666 strcat(formatPrefix, "s");
2668 /* this is important if sizeof(int_32) ! sizeof(time_t) */
2669 { time_t dateint = *((int_32 *) data);
2670 tstruct = localtime(&dateint);
2672 (void)strftime(buf, sizeof(buf) - 1, strftimeFormat, tstruct);
2673 sprintf(val, formatPrefix, buf);
2679 static char * dateFormat(int_32 type, const void * data,
2680 char * formatPrefix, int padding, int element)
2681 /*@modifies formatPrefix @*/
2683 return realDateFormat(type, data, formatPrefix, padding, element, "%c");
2686 static char * dayFormat(int_32 type, const void * data,
2687 char * formatPrefix, int padding, int element)
2688 /*@modifies formatPrefix @*/
2690 return realDateFormat(type, data, formatPrefix, padding, element,
2694 static char * shescapeFormat(int_32 type, const void * data,
2695 char * formatPrefix, int padding, /*@unused@*/int element)
2696 /*@modifies formatPrefix @*/
2698 char * result, * dst, * src, * buf;
2700 if (type == RPM_INT32_TYPE) {
2701 result = xmalloc(padding + 20);
2702 strcat(formatPrefix, "d");
2703 sprintf(result, formatPrefix, *((int_32 *) data));
2705 buf = alloca(strlen(data) + padding + 2);
2706 strcat(formatPrefix, "s");
2707 sprintf(buf, formatPrefix, data);
2709 result = dst = xmalloc(strlen(buf) * 4 + 3);
2711 for (src = buf; *src != '\0'; src++) {
2729 const struct headerSprintfExtension headerDefaultFormats[] = {
2730 { HEADER_EXT_FORMAT, "octal", { octalFormat } },
2731 { HEADER_EXT_FORMAT, "hex", { hexFormat } },
2732 { HEADER_EXT_FORMAT, "date", { dateFormat } },
2733 { HEADER_EXT_FORMAT, "day", { dayFormat } },
2734 { HEADER_EXT_FORMAT, "shescape", { shescapeFormat } },
2735 { HEADER_EXT_LAST, NULL, { NULL } }
2738 void headerCopyTags(Header headerFrom, Header headerTo, int *tagstocopy)
2742 if (headerFrom == headerTo)
2745 for (p = tagstocopy; *p != 0; p++) {
2748 if (headerIsEntry(headerTo, *p))
2750 if (!headerGetEntryMinMemory(headerFrom, *p, &type,
2751 (const void **) &s, &count))
2753 headerAddEntry(headerTo, *p, type, s, count);
2754 s = headerFreeData(s, type);