Bug Summary

File:lib/header.c
Warning:line 963, column 5
Value stored to 'h' is never read

Annotated Source Code

1/** \ingroup header
2 * \file lib/header.c
3 */
4
5/* RPM - Copyright (C) 1995-2002 Red Hat Software */
6
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. */
10
11#include "system.h"
12#include <netdb.h>
13#include <errno(*__errno_location ()).h>
14#include <rpm/rpmtypes.h>
15#include <rpm/rpmstring.h>
16#include "lib/header_internal.h"
17#include "lib/misc.h" /* tag function proto */
18
19#include "debug.h"
20
21/** \ingroup header
22 */
23const unsigned char rpm_header_magic[8] = {
24 0x8e, 0xad, 0xe8, 0x01, 0x00, 0x00, 0x00, 0x00
25};
26
27/** \ingroup header
28 * Alignment needed for header data types.
29 */
30static 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 */
41 0,
42 0,
43 0,
44 0,
45 0,
46 0
47};
48
49/** \ingroup header
50 * Size of header data types.
51 */
52static 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 */
63 0,
64 0,
65 0,
66 0,
67 0,
68 0
69};
70
71enum headerSorted_e {
72 HEADERSORT_NONE = 0, /* Not sorted */
73 HEADERSORT_OFFSET = 1, /* Sorted by offset (on-disk format) */
74 HEADERSORT_INDEX = 2, /* Sorted by index */
75};
76
77enum headerFlags_e {
78 HEADERFLAG_ALLOCATED = (1 << 1), /*!< Is 1st header region allocated? */
79 HEADERFLAG_LEGACY = (1 << 2), /*!< Header came from legacy source? */
80 HEADERFLAG_DEBUG = (1 << 3), /*!< Debug this header? */
81};
82
83typedef rpmFlags headerFlags;
84
85/** \ingroup header
86 * A single tag from a Header.
87 */
88typedef struct indexEntry_s * indexEntry;
89struct indexEntry_s {
90 struct entryInfo_s info; /*!< Description of tag data. */
91 rpm_data_t data; /*!< Location of tag data. */
92 int length; /*!< No. bytes of data. */
93 int rdlen; /*!< No. bytes of data in region. */
94};
95
96/** \ingroup header
97 * The Header data structure.
98 */
99struct headerToken_s {
100 void * blob; /*!< Header region blob. */
101 indexEntry index; /*!< Array of tags. */
102 int indexUsed; /*!< Current size of tag array. */
103 int indexAlloced; /*!< Allocated size of tag array. */
104 unsigned int instance; /*!< Rpmdb instance (offset) */
105 headerFlags flags;
106 int sorted; /*!< Current sort method */
107 int nrefs; /*!< Reference count. */
108};
109
110/** \ingroup header
111 * Maximum no. of bytes permitted in a header.
112 */
113static const size_t headerMaxbytes = (256*1024*1024);
114
115#define INDEX_MALLOC_SIZE8 8
116
117#define ENTRY_IS_REGION(_e)(((_e)->info.tag >= RPMTAG_HEADERIMAGE) && ((_e
)->info.tag < RPMTAG_HEADERREGIONS))
\
118 (((_e)->info.tag >= RPMTAG_HEADERIMAGE) && ((_e)->info.tag < RPMTAG_HEADERREGIONS))
119#define ENTRY_IN_REGION(_e)((_e)->info.offset < 0) ((_e)->info.offset < 0)
120
121#define REGION_TAG_TYPERPM_BIN_TYPE RPM_BIN_TYPE
122#define REGION_TAG_COUNTsizeof(struct entryInfo_s) sizeof(struct entryInfo_s)
123
124/**
125 * Sanity check on no. of tags.
126 * This check imposes a limit of 65K tags, more than enough.
127 */
128#define HEADER_TAGS_MAX0x0000ffff 0x0000ffff
129#define hdrchkTags(_ntags)((_ntags) & (~0x0000ffff)) ((_ntags) & (~HEADER_TAGS_MAX0x0000ffff))
130
131/**
132 * Sanity check on tag values.
133 * Catches out nasties like negative values and multiple regions.
134 **/
135#define hdrchkTag(_tag)((_tag) < 100) ((_tag) < HEADER_I18NTABLE100)
136
137/**
138 * Sanity check on type values.
139 */
140#define hdrchkType(_type)((_type) < 0 || (_type) > 9) ((_type) < RPM_MIN_TYPE0 || (_type) > RPM_MAX_TYPE9)
141
142/**
143 * Sanity check on data size and/or offset and/or count.
144 * This check imposes a limit of 256 MB -- file signatures
145 * may require a lot of space in the header.
146 */
147#define HEADER_DATA_MAX0x0fffffff 0x0fffffff
148#define hdrchkData(_nbytes)((_nbytes) & (~0x0fffffff)) ((_nbytes) & (~HEADER_DATA_MAX0x0fffffff))
149
150/**
151 * Sanity check on data alignment for data type.
152 */
153#define hdrchkAlign(_type, _off)((_off) & (typeAlign[_type]-1)) ((_off) & (typeAlign[_type]-1))
154
155/**
156 * Sanity check on range of data offset.
157 */
158#define hdrchkRange(_dl, _off)((_off) < 0 || (_off) > (_dl)) ((_off) < 0 || (_off) > (_dl))
159
160static int dataLength(rpm_tagtype_t type, rpm_constdata_t p, rpm_count_t count,
161 int onDisk, rpm_constdata_t pend);
162
163#ifndef htonll
164/* Convert a 64bit value to network byte order. */
165RPM_GNUC_CONST__attribute__((__const__))
166static uint64_t htonll(uint64_t n)
167{
168#if __BYTE_ORDER__1234 == __ORDER_LITTLE_ENDIAN__1234
169 uint32_t *i = (uint32_t*)&n;
170 uint32_t b = i[0];
171 i[0] = htonl(i[1])(__extension__ ({ unsigned int __v, __x = (i[1]); if (__builtin_constant_p
(__x)) __v = ((((__x) & 0xff000000) >> 24) | (((__x
) & 0x00ff0000) >> 8) | (((__x) & 0x0000ff00) <<
8) | (((__x) & 0x000000ff) << 24)); else __asm__ (
"bswap %0" : "=r" (__v) : "0" (__x)); __v; }))
;
172 i[1] = htonl(b)(__extension__ ({ unsigned int __v, __x = (b); if (__builtin_constant_p
(__x)) __v = ((((__x) & 0xff000000) >> 24) | (((__x
) & 0x00ff0000) >> 8) | (((__x) & 0x0000ff00) <<
8) | (((__x) & 0x000000ff) << 24)); else __asm__ (
"bswap %0" : "=r" (__v) : "0" (__x)); __v; }))
;
173#endif
174 return n;
175}
176#endif
177
178Header headerLink(Header h)
179{
180 if (h != NULL((void*)0))
181 h->nrefs++;
182 return h;
183}
184
185static Header headerUnlink(Header h)
186{
187 if (h != NULL((void*)0))
188 h->nrefs--;
189 return NULL((void*)0);
190}
191
192Header headerFree(Header h)
193{
194 (void) headerUnlink(h);
195
196 if (h == NULL((void*)0) || h->nrefs > 0)
197 return NULL((void*)0);
198
199 if (h->index) {
200 indexEntry entry = h->index;
201 int i;
202 for (i = 0; i < h->indexUsed; i++, entry++) {
203 if ((h->flags & HEADERFLAG_ALLOCATED) && ENTRY_IS_REGION(entry)(((entry)->info.tag >= RPMTAG_HEADERIMAGE) && (
(entry)->info.tag < RPMTAG_HEADERREGIONS))
) {
204 if (entry->length > 0) {
205 int32_t * ei = entry->data;
206 if ((ei - 2) == h->blob) h->blob = _free(h->blob)rfree((h->blob));
207 entry->data = NULL((void*)0);
208 }
209 } else if (!ENTRY_IN_REGION(entry)((entry)->info.offset < 0)) {
210 entry->data = _free(entry->data)rfree((entry->data));
211 }
212 entry->data = NULL((void*)0);
213 }
214 h->index = _free(h->index)rfree((h->index));
215 }
216
217 h = _free(h)rfree((h));
218 return NULL((void*)0);
219}
220
221static Header headerCreate(void *blob, int32_t indexLen)
222{
223 Header h = xcalloc(1, sizeof(*h))rcalloc((1), (sizeof(*h)));
224 if (blob) {
225 h->blob = blob;
226 h->indexAlloced = indexLen + 1;
227 h->indexUsed = indexLen;
228 } else {
229 h->indexAlloced = INDEX_MALLOC_SIZE8;
230 h->indexUsed = 0;
231 }
232 h->instance = 0;
233 h->sorted = HEADERSORT_NONE;
234
235 h->index = (h->indexAlloced
236 ? xcalloc(h->indexAlloced, sizeof(*h->index))rcalloc((h->indexAlloced), (sizeof(*h->index)))
237 : NULL((void*)0));
238
239 h->nrefs = 0;
240 return headerLink(h);
241}
242
243Header headerNew(void)
244{
245 return headerCreate(NULL((void*)0), 0);
246}
247
248static rpmRC hdrblobVerifyInfo(hdrblob blob, char **emsg)
249{
250 struct entryInfo_s info;
251 int i, len = 0;
252 int32_t end = 0;
253 const char *ds = (const char *) blob->dataStart;
254 int32_t il = (blob->regionTag) ? blob->il-1 : blob->il;
255 entryInfo pe = (blob->regionTag) ? blob->pe+1 : blob->pe;
256
257 for (i = 0; i < il; i++) {
258 ei2h(&pe[i], &info);
259
260 /* Previous data must not overlap */
261 if (end > info.offset)
262 goto err;
263
264 if (hdrchkTag(info.tag)((info.tag) < 100))
265 goto err;
266 if (hdrchkType(info.type)((info.type) < 0 || (info.type) > 9))
267 goto err;
268 if (hdrchkAlign(info.type, info.offset)((info.offset) & (typeAlign[info.type]-1)))
269 goto err;
270 if (hdrchkRange(blob->dl, info.offset)((info.offset) < 0 || (info.offset) > (blob->dl)))
271 goto err;
272
273 /* Verify the data actually fits */
274 len = dataLength(info.type, ds + info.offset,
275 info.count, 1, ds + blob->dl);
276 end = info.offset + len;
277 if (hdrchkRange(blob->dl, end)((end) < 0 || (end) > (blob->dl)) || len <= 0)
278 goto err;
279 }
280 return 0; /* Everything ok */
281
282err:
283 if (emsg) {
284 rasprintf(emsg,
285 _("tag[%d]: BAD, tag %d type %d offset %d count %d len %d")dcgettext ("rpm", "tag[%d]: BAD, tag %d type %d offset %d count %d len %d"
, 5)
,
286 i, info.tag, info.type, info.offset, info.count, len);
287 }
288 return i + 1;
289}
290
291static int indexCmp(const void * avp, const void * bvp)
292{
293 indexEntry ap = (indexEntry) avp, bp = (indexEntry) bvp;
294 return (ap->info.tag - bp->info.tag);
295}
296
297static void headerSort(Header h)
298{
299 if (h->sorted != HEADERSORT_INDEX) {
300 qsort(h->index, h->indexUsed, sizeof(*h->index), indexCmp);
301 h->sorted = HEADERSORT_INDEX;
302 }
303}
304
305static int offsetCmp(const void * avp, const void * bvp)
306{
307 indexEntry ap = (indexEntry) avp, bp = (indexEntry) bvp;
308 int rc = (ap->info.offset - bp->info.offset);
309
310 if (rc == 0) {
311 /* Within a region, entries sort by address. Added drips sort by tag. */
312 if (ap->info.offset < 0)
313 rc = (((char *)ap->data) - ((char *)bp->data));
314 else
315 rc = (ap->info.tag - bp->info.tag);
316 }
317 return rc;
318}
319
320static void headerUnsort(Header h)
321{
322 if (h->sorted != HEADERSORT_OFFSET) {
323 qsort(h->index, h->indexUsed, sizeof(*h->index), offsetCmp);
324 h->sorted = HEADERSORT_OFFSET;
325 }
326}
327
328static inline unsigned int alignDiff(rpm_tagtype_t type, unsigned int alignsize)
329{
330 int typesize = typeSizes[type];
331
332 if (typesize > 1) {
333 unsigned int diff = typesize - (alignsize % typesize);
334 if (diff != typesize)
335 return diff;
336 }
337 return 0;
338}
339
340unsigned headerSizeof(Header h, int magicp)
341{
342 indexEntry entry;
343 unsigned int size = 0;
344 int i;
345
346 if (h == NULL((void*)0))
347 return size;
348
349 headerSort(h);
350
351 if (magicp == HEADER_MAGIC_YES)
352 size += sizeof(rpm_header_magic);
353
354 size += 2 * sizeof(int32_t); /* count of index entries */
355
356 for (i = 0, entry = h->index; i < h->indexUsed; i++, entry++) {
357 /* Regions go in as is ... */
358 if (ENTRY_IS_REGION(entry)(((entry)->info.tag >= RPMTAG_HEADERIMAGE) && (
(entry)->info.tag < RPMTAG_HEADERREGIONS))
) {
359 size += entry->length;
360 /* Reserve space for legacy region tag + data */
361 if (i == 0 && (h->flags & HEADERFLAG_LEGACY))
362 size += sizeof(struct entryInfo_s) + entry->info.count;
363 continue;
364 }
365
366 /* ... and region elements are skipped. */
367 if (entry->info.offset < 0)
368 continue;
369
370 /* Alignment */
371 size += alignDiff(entry->info.type, size);
372
373 size += sizeof(struct entryInfo_s) + entry->length;
374 }
375
376 return size;
377}
378
379/*
380 * Header string (array) size calculation, bounded if end is non-NULL.
381 * Return length (including \0 termination) on success, -1 on error.
382 */
383static inline int strtaglen(const char *str, rpm_count_t c, const char *end)
384{
385 const char *start = str;
386 const char *s;
387
388 if (end) {
389 if (str >= end)
390 return -1;
391 while ((s = memchr(start, '\0', end-start))) {
392 if (--c == 0 || s > end)
393 break;
394 start = s + 1;
395 }
396 } else {
397 while ((s = strchr(start, '\0'))) {
398 if (--c == 0)
399 break;
400 start = s + 1;
401 }
402 }
403 return (c > 0) ? -1 : (s - str + 1);
404}
405
406/**
407 * Return length of entry data.
408 * @param type entry data type
409 * @param p entry data
410 * @param count entry item count
411 * @param onDisk data is concatenated strings (with NUL's))?
412 * @param pend pointer to end of data (or NULL)
413 * @return no. bytes in data, -1 on failure
414 */
415static int dataLength(rpm_tagtype_t type, rpm_constdata_t p, rpm_count_t count,
416 int onDisk, rpm_constdata_t pend)
417{
418 const char * s = p;
419 const char * se = pend;
420 int length = 0;
421
422 switch (type) {
423 case RPM_STRING_TYPE:
424 if (count != 1)
425 return -1;
426 length = strtaglen(s, 1, se);
427 break;
428
429 case RPM_STRING_ARRAY_TYPE:
430 case RPM_I18NSTRING_TYPE:
431 /* These are like RPM_STRING_TYPE, except they're *always* an array */
432 /* Compute sum of length of all strings, including nul terminators */
433
434 if (onDisk) {
435 length = strtaglen(s, count, se);
436 } else {
437 const char ** av = (const char **)p;
438 while (count--) {
439 /* add one for null termination */
440 length += strlen(*av++) + 1;
441 }
442 }
443 break;
444
445 default:
446 if (typeSizes[type] == -1)
447 return -1;
448 length = typeSizes[(type & 0xf)] * count;
449 if (length < 0 || (se && (s + length) > se))
450 return -1;
451 break;
452 }
453
454 return length;
455}
456
457/** \ingroup header
458 * Swap int32_t and int16_t arrays within header region.
459 *
460 * If a header region tag is in the set to be swabbed, as the data for a
461 * a header region is located after all other tag data.
462 *
463 * @param entry header entry
464 * @param il no. of entries
465 * @param dl start no. bytes of data
466 * @param pe header physical entry pointer (swapped)
467 * @param dataStart header data start
468 * @param dataEnd header data end
469 * @param regionid region offset
470 * @param fast use offsets for data sizes if possible
471 * @return no. bytes of data in region, -1 on error
472 */
473static int regionSwab(indexEntry entry, int il, int dl,
474 entryInfo pe,
475 unsigned char * dataStart,
476 const unsigned char * dataEnd,
477 int regionid, int fast)
478{
479 if ((entry != NULL((void*)0) && regionid >= 0) || (entry == NULL((void*)0) && regionid != 0))
480 return -1;
481
482 for (; il > 0; il--, pe++) {
483 struct indexEntry_s ie;
484
485 ei2h(pe, &ie.info);
486
487 if (hdrchkType(ie.info.type)((ie.info.type) < 0 || (ie.info.type) > 9))
488 return -1;
489 if (hdrchkData(ie.info.count)((ie.info.count) & (~0x0fffffff)))
490 return -1;
491 if (hdrchkData(ie.info.offset)((ie.info.offset) & (~0x0fffffff)))
492 return -1;
493 if (hdrchkAlign(ie.info.type, ie.info.offset)((ie.info.offset) & (typeAlign[ie.info.type]-1)))
494 return -1;
495
496 ie.data = dataStart + ie.info.offset;
497 if (dataEnd && (unsigned char *)ie.data >= dataEnd)
498 return -1;
499
500 if (fast && il > 1) {
501 ie.length = ntohl(pe[1].offset)(__extension__ ({ unsigned int __v, __x = (pe[1].offset); if (
__builtin_constant_p (__x)) __v = ((((__x) & 0xff000000) >>
24) | (((__x) & 0x00ff0000) >> 8) | (((__x) & 0x0000ff00
) << 8) | (((__x) & 0x000000ff) << 24)); else
__asm__ ("bswap %0" : "=r" (__v) : "0" (__x)); __v; }))
- ie.info.offset;
502 } else {
503 ie.length = dataLength(ie.info.type, ie.data, ie.info.count,
504 1, dataEnd);
505 }
506 if (ie.length < 0 || hdrchkData(ie.length)((ie.length) & (~0x0fffffff)))
507 return -1;
508
509 ie.rdlen = 0;
510
511 if (entry) {
512 ie.info.offset = regionid;
513 *entry = ie; /* structure assignment */
514 entry++;
515 }
516
517 /* Alignment */
518 dl += alignDiff(ie.info.type, dl);
519
520 /* Perform endian conversions */
521 switch (ntohl(pe->type)(__extension__ ({ unsigned int __v, __x = (pe->type); if (
__builtin_constant_p (__x)) __v = ((((__x) & 0xff000000) >>
24) | (((__x) & 0x00ff0000) >> 8) | (((__x) & 0x0000ff00
) << 8) | (((__x) & 0x000000ff) << 24)); else
__asm__ ("bswap %0" : "=r" (__v) : "0" (__x)); __v; }))
) {
522 case RPM_INT64_TYPE:
523 { uint64_t * it = ie.data;
524 for (; ie.info.count > 0; ie.info.count--, it += 1) {
525 if (dataEnd && ((unsigned char *)it) >= dataEnd)
526 return -1;
527 *it = htonll(*it);
528 }
529 } break;
530 case RPM_INT32_TYPE:
531 { int32_t * it = ie.data;
532 for (; ie.info.count > 0; ie.info.count--, it += 1) {
533 if (dataEnd && ((unsigned char *)it) >= dataEnd)
534 return -1;
535 *it = htonl(*it)(__extension__ ({ unsigned int __v, __x = (*it); if (__builtin_constant_p
(__x)) __v = ((((__x) & 0xff000000) >> 24) | (((__x
) & 0x00ff0000) >> 8) | (((__x) & 0x0000ff00) <<
8) | (((__x) & 0x000000ff) << 24)); else __asm__ (
"bswap %0" : "=r" (__v) : "0" (__x)); __v; }))
;
536 }
537 } break;
538 case RPM_INT16_TYPE:
539 { int16_t * it = ie.data;
540 for (; ie.info.count > 0; ie.info.count--, it += 1) {
541 if (dataEnd && ((unsigned char *)it) >= dataEnd)
542 return -1;
543 *it = htons(*it)(__extension__ ({ unsigned short int __v, __x = (unsigned short
int) (*it); if (__builtin_constant_p (__x)) __v = ((unsigned
short int) ((((__x) >> 8) & 0xff) | (((__x) & 0xff
) << 8))); else __asm__ ("rorw $8, %w0" : "=r" (__v) : "0"
(__x) : "cc"); __v; }))
;
544 }
545 } break;
546 }
547
548 dl += ie.length;
549 }
550
551 return dl;
552}
553
554void * headerExport(Header h, unsigned int *bsize)
555{
556 int32_t * ei = NULL((void*)0);
557 entryInfo pe;
558 char * dataStart;
559 char * te;
560 unsigned len, diff;
561 int32_t il = 0;
562 int32_t dl = 0;
563 indexEntry entry;
564 int i;
565 int drlen, ndribbles;
566
567 if (h == NULL((void*)0)) return NULL((void*)0);
568
569 /* Sort entries by (offset,tag). */
570 headerUnsort(h);
571
572 /* Compute (il,dl) for all tags, including those deleted in region. */
573 drlen = ndribbles = 0;
574 for (i = 0, entry = h->index; i < h->indexUsed; i++, entry++) {
575 if (ENTRY_IS_REGION(entry)(((entry)->info.tag >= RPMTAG_HEADERIMAGE) && (
(entry)->info.tag < RPMTAG_HEADERREGIONS))
) {
576 int32_t rdl = -entry->info.offset; /* negative offset */
577 int32_t ril = rdl/sizeof(*pe);
578 int rid = entry->info.offset;
579
580 il += ril;
581 dl += entry->rdlen + entry->info.count;
582 /* Reserve space for legacy region tag */
583 if (i == 0 && (h->flags & HEADERFLAG_LEGACY))
584 il += 1;
585
586 /* Skip rest of entries in region, but account for dribbles. */
587 for (; i < h->indexUsed && entry->info.offset <= rid+1; i++, entry++) {
588 if (entry->info.offset <= rid)
589 continue;
590
591 /* Alignment */
592 diff = alignDiff(entry->info.type, dl);
593 if (diff) {
594 drlen += diff;
595 dl += diff;
596 }
597
598 ndribbles++;
599 il++;
600 drlen += entry->length;
601 dl += entry->length;
602 }
603 i--;
604 entry--;
605 continue;
606 }
607
608 /* Ignore deleted drips. */
609 if (entry->data == NULL((void*)0) || entry->length <= 0)
610 continue;
611
612 /* Alignment */
613 dl += alignDiff(entry->info.type, dl);
614
615 il++;
616 dl += entry->length;
617 }
618
619 /* Sanity checks on header intro. */
620 if (hdrchkTags(il)((il) & (~0x0000ffff)) || hdrchkData(dl)((dl) & (~0x0fffffff)))
621 goto errxit;
622
623 len = sizeof(il) + sizeof(dl) + (il * sizeof(*pe)) + dl;
624
625 ei = xmalloc(len)rmalloc((len));
626 ei[0] = htonl(il)(__extension__ ({ unsigned int __v, __x = (il); if (__builtin_constant_p
(__x)) __v = ((((__x) & 0xff000000) >> 24) | (((__x
) & 0x00ff0000) >> 8) | (((__x) & 0x0000ff00) <<
8) | (((__x) & 0x000000ff) << 24)); else __asm__ (
"bswap %0" : "=r" (__v) : "0" (__x)); __v; }))
;
627 ei[1] = htonl(dl)(__extension__ ({ unsigned int __v, __x = (dl); if (__builtin_constant_p
(__x)) __v = ((((__x) & 0xff000000) >> 24) | (((__x
) & 0x00ff0000) >> 8) | (((__x) & 0x0000ff00) <<
8) | (((__x) & 0x000000ff) << 24)); else __asm__ (
"bswap %0" : "=r" (__v) : "0" (__x)); __v; }))
;
628
629 pe = (entryInfo) &ei[2];
630 dataStart = te = (char *) (pe + il);
631
632 for (i = 0, entry = h->index; i < h->indexUsed; i++, entry++) {
633 const char * src;
634 unsigned char *t;
635 int count;
636 int rdlen;
637 unsigned int diff;
638
639 if (entry->data == NULL((void*)0) || entry->length <= 0)
640 continue;
641
642 t = (unsigned char*)te;
643 pe->tag = htonl(entry->info.tag)(__extension__ ({ unsigned int __v, __x = (entry->info.tag
); if (__builtin_constant_p (__x)) __v = ((((__x) & 0xff000000
) >> 24) | (((__x) & 0x00ff0000) >> 8) | (((__x
) & 0x0000ff00) << 8) | (((__x) & 0x000000ff) <<
24)); else __asm__ ("bswap %0" : "=r" (__v) : "0" (__x)); __v
; }))
;
644 pe->type = htonl(entry->info.type)(__extension__ ({ unsigned int __v, __x = (entry->info.type
); if (__builtin_constant_p (__x)) __v = ((((__x) & 0xff000000
) >> 24) | (((__x) & 0x00ff0000) >> 8) | (((__x
) & 0x0000ff00) << 8) | (((__x) & 0x000000ff) <<
24)); else __asm__ ("bswap %0" : "=r" (__v) : "0" (__x)); __v
; }))
;
645 pe->count = htonl(entry->info.count)(__extension__ ({ unsigned int __v, __x = (entry->info.count
); if (__builtin_constant_p (__x)) __v = ((((__x) & 0xff000000
) >> 24) | (((__x) & 0x00ff0000) >> 8) | (((__x
) & 0x0000ff00) << 8) | (((__x) & 0x000000ff) <<
24)); else __asm__ ("bswap %0" : "=r" (__v) : "0" (__x)); __v
; }))
;
646
647 if (ENTRY_IS_REGION(entry)(((entry)->info.tag >= RPMTAG_HEADERIMAGE) && (
(entry)->info.tag < RPMTAG_HEADERREGIONS))
) {
648 int32_t rdl = -entry->info.offset; /* negative offset */
649 int32_t ril = rdl/sizeof(*pe) + ndribbles;
650 int rid = entry->info.offset;
651
652 src = (char *)entry->data;
653 rdlen = entry->rdlen;
654
655 /* Legacy headers don't have regions originally, create one */
656 if (i == 0 && (h->flags & HEADERFLAG_LEGACY)) {
657 int32_t stei[4];
658
659 memcpy(pe+1, src, rdl);
660 memcpy(te, src + rdl, rdlen);
661 te += rdlen;
662
663 pe->offset = htonl(te - dataStart)(__extension__ ({ unsigned int __v, __x = (te - dataStart); if
(__builtin_constant_p (__x)) __v = ((((__x) & 0xff000000
) >> 24) | (((__x) & 0x00ff0000) >> 8) | (((__x
) & 0x0000ff00) << 8) | (((__x) & 0x000000ff) <<
24)); else __asm__ ("bswap %0" : "=r" (__v) : "0" (__x)); __v
; }))
;
664 stei[0] = pe->tag;
665 stei[1] = pe->type;
666 stei[2] = htonl(-rdl-entry->info.count)(__extension__ ({ unsigned int __v, __x = (-rdl-entry->info
.count); if (__builtin_constant_p (__x)) __v = ((((__x) &
0xff000000) >> 24) | (((__x) & 0x00ff0000) >>
8) | (((__x) & 0x0000ff00) << 8) | (((__x) & 0x000000ff
) << 24)); else __asm__ ("bswap %0" : "=r" (__v) : "0" (
__x)); __v; }))
;
667 stei[3] = pe->count;
668 memcpy(te, stei, entry->info.count);
669 te += entry->info.count;
670 ril++;
671 rdlen += entry->info.count;
672
673 count = regionSwab(NULL((void*)0), ril, 0, pe, t, NULL((void*)0), 0, 0);
674 if (count != rdlen)
675 goto errxit;
676
677 } else {
678
679 memcpy(pe+1, src + sizeof(*pe), ((ril-1) * sizeof(*pe)));
680 memcpy(te, src + (ril * sizeof(*pe)), rdlen+entry->info.count+drlen);
681 te += rdlen;
682 {
683 entryInfo se = (entryInfo)src;
684 int off = ntohl(se->offset)(__extension__ ({ unsigned int __v, __x = (se->offset); if
(__builtin_constant_p (__x)) __v = ((((__x) & 0xff000000
) >> 24) | (((__x) & 0x00ff0000) >> 8) | (((__x
) & 0x0000ff00) << 8) | (((__x) & 0x000000ff) <<
24)); else __asm__ ("bswap %0" : "=r" (__v) : "0" (__x)); __v
; }))
;
685 pe->offset = (off) ? htonl(te - dataStart)(__extension__ ({ unsigned int __v, __x = (te - dataStart); if
(__builtin_constant_p (__x)) __v = ((((__x) & 0xff000000
) >> 24) | (((__x) & 0x00ff0000) >> 8) | (((__x
) & 0x0000ff00) << 8) | (((__x) & 0x000000ff) <<
24)); else __asm__ ("bswap %0" : "=r" (__v) : "0" (__x)); __v
; }))
: htonl(off)(__extension__ ({ unsigned int __v, __x = (off); if (__builtin_constant_p
(__x)) __v = ((((__x) & 0xff000000) >> 24) | (((__x
) & 0x00ff0000) >> 8) | (((__x) & 0x0000ff00) <<
8) | (((__x) & 0x000000ff) << 24)); else __asm__ (
"bswap %0" : "=r" (__v) : "0" (__x)); __v; }))
;
686 }
687 te += entry->info.count + drlen;
688
689 count = regionSwab(NULL((void*)0), ril, 0, pe, t, NULL((void*)0), 0, 0);
690 if (count != (rdlen + entry->info.count + drlen))
691 goto errxit;
692 }
693
694 /* Skip rest of entries in region. */
695 while (i < h->indexUsed && entry->info.offset <= rid+1) {
696 i++;
697 entry++;
698 }
699 i--;
700 entry--;
701 pe += ril;
702 continue;
703 }
704
705 /* Ignore deleted drips. */
706 if (entry->data == NULL((void*)0) || entry->length <= 0)
707 continue;
708
709 /* Alignment */
710 diff = alignDiff(entry->info.type, (te - dataStart));
711 if (diff) {
712 memset(te, 0, diff);
713 te += diff;
714 }
715
716 pe->offset = htonl(te - dataStart)(__extension__ ({ unsigned int __v, __x = (te - dataStart); if
(__builtin_constant_p (__x)) __v = ((((__x) & 0xff000000
) >> 24) | (((__x) & 0x00ff0000) >> 8) | (((__x
) & 0x0000ff00) << 8) | (((__x) & 0x000000ff) <<
24)); else __asm__ ("bswap %0" : "=r" (__v) : "0" (__x)); __v
; }))
;
717
718 /* copy data w/ endian conversions */
719 switch (entry->info.type) {
720 case RPM_INT64_TYPE:
721 count = entry->info.count;
722 src = entry->data;
723 while (count--) {
724 *((uint64_t *)te) = htonll(*((uint64_t *)src));
725 te += sizeof(uint64_t);
726 src += sizeof(uint64_t);
727 }
728 break;
729
730 case RPM_INT32_TYPE:
731 count = entry->info.count;
732 src = entry->data;
733 while (count--) {
734 *((int32_t *)te) = htonl(*((int32_t *)src))(__extension__ ({ unsigned int __v, __x = (*((int32_t *)src))
; if (__builtin_constant_p (__x)) __v = ((((__x) & 0xff000000
) >> 24) | (((__x) & 0x00ff0000) >> 8) | (((__x
) & 0x0000ff00) << 8) | (((__x) & 0x000000ff) <<
24)); else __asm__ ("bswap %0" : "=r" (__v) : "0" (__x)); __v
; }))
;
735 te += sizeof(int32_t);
736 src += sizeof(int32_t);
737 }
738 break;
739
740 case RPM_INT16_TYPE:
741 count = entry->info.count;
742 src = entry->data;
743 while (count--) {
744 *((int16_t *)te) = htons(*((int16_t *)src))(__extension__ ({ unsigned short int __v, __x = (unsigned short
int) (*((int16_t *)src)); if (__builtin_constant_p (__x)) __v
= ((unsigned short int) ((((__x) >> 8) & 0xff) | (
((__x) & 0xff) << 8))); else __asm__ ("rorw $8, %w0"
: "=r" (__v) : "0" (__x) : "cc"); __v; }))
;
745 te += sizeof(int16_t);
746 src += sizeof(int16_t);
747 }
748 break;
749
750 default:
751 memcpy(te, entry->data, entry->length);
752 te += entry->length;
753 break;
754 }
755 pe++;
756 }
757
758 /* Insure that there are no memcpy underruns/overruns. */
759 if (((char *)pe) != dataStart)
760 goto errxit;
761 if ((((char *)ei)+len) != te)
762 goto errxit;
763
764 if (bsize)
765 *bsize = len;
766
767 headerSort(h);
768
769 return (void *) ei;
770
771errxit:
772 free(ei);
773 return NULL((void*)0);
774}
775
776void * headerUnload(Header h)
777{
778 return headerExport(h, NULL((void*)0));
779}
780
781/**
782 * Find matching (tag,type) entry in header.
783 * @param h header
784 * @param tag entry tag
785 * @param type entry type
786 * @return header entry
787 */
788static
789indexEntry findEntry(Header h, rpmTagVal tag, rpm_tagtype_t type)
790{
791 indexEntry entry;
792 struct indexEntry_s key;
793
794 if (h == NULL((void*)0)) return NULL((void*)0);
795 if (h->sorted != HEADERSORT_INDEX)
796 headerSort(h);
797
798 key.info.tag = tag;
799
800 entry = bsearch(&key, h->index, h->indexUsed, sizeof(*h->index), indexCmp);
801 if (entry == NULL((void*)0))
802 return NULL((void*)0);
803
804 if (type == RPM_NULL_TYPE)
805 return entry;
806
807 /* look backwards */
808 while (entry->info.tag == tag && entry->info.type != type &&
809 entry > h->index) entry--;
810
811 if (entry->info.tag == tag && entry->info.type == type)
812 return entry;
813
814 return NULL((void*)0);
815}
816
817int headerDel(Header h, rpmTagVal tag)
818{
819 indexEntry last = h->index + h->indexUsed;
820 indexEntry entry, first;
821 int ne;
822
823 entry = findEntry(h, tag, RPM_NULL_TYPE);
824 if (!entry) return 1;
825
826 /* Make sure entry points to the first occurrence of this tag. */
827 while (entry > h->index && (entry - 1)->info.tag == tag)
828 entry--;
829
830 /* Free data for tags being removed. */
831 for (first = entry; first < last; first++) {
832 rpm_data_t data;
833 if (first->info.tag != tag)
834 break;
835 data = first->data;
836 first->data = NULL((void*)0);
837 first->length = 0;
838 if (ENTRY_IN_REGION(first)((first)->info.offset < 0))
839 continue;
840 free(data);
841 }
842
843 ne = (first - entry);
844 if (ne > 0) {
845 h->indexUsed -= ne;
846 ne = last - first;
847 if (ne > 0)
848 memmove(entry, first, (ne * sizeof(*entry)));
849 }
850
851 return 0;
852}
853
854rpmRC hdrblobImport(hdrblob blob, int fast, Header *hdrp, char **emsg)
855{
856 Header h = NULL((void*)0);
857 indexEntry entry;
858 int rdlen;
859
860 h = headerCreate(blob->ei, blob->il);
861
862 entry = h->index;
863 if (!(htonl(blob->pe->tag)(__extension__ ({ unsigned int __v, __x = (blob->pe->tag
); if (__builtin_constant_p (__x)) __v = ((((__x) & 0xff000000
) >> 24) | (((__x) & 0x00ff0000) >> 8) | (((__x
) & 0x0000ff00) << 8) | (((__x) & 0x000000ff) <<
24)); else __asm__ ("bswap %0" : "=r" (__v) : "0" (__x)); __v
; }))
< RPMTAG_HEADERI18NTABLE)) {
864 /* An original v3 header, create a legacy region entry for it */
865 h->flags |= HEADERFLAG_LEGACY;
866 entry->info.type = REGION_TAG_TYPERPM_BIN_TYPE;
867 entry->info.tag = RPMTAG_HEADERIMAGE;
868 entry->info.count = REGION_TAG_COUNTsizeof(struct entryInfo_s);
869 entry->info.offset = ((unsigned char *)blob->pe - blob->dataStart); /* negative offset */
870
871 entry->data = blob->pe;
872 entry->length = blob->pvlen - sizeof(blob->il) - sizeof(blob->dl);
873 rdlen = regionSwab(entry+1, blob->il, 0, blob->pe,
874 blob->dataStart, blob->dataEnd,
875 entry->info.offset, fast);
876 if (rdlen != blob->dl)
877 goto errxit;
878 entry->rdlen = rdlen;
879 h->indexUsed++;
880 } else {
881 /* Either a v4 header or an "upgraded" v3 header with a legacy region */
882 int32_t ril;
883
884 h->flags &= ~HEADERFLAG_LEGACY;
885 ei2h(blob->pe, &entry->info);
886 ril = (entry->info.offset != 0) ? blob->ril : blob->il;
887
888 entry->info.offset = -(ril * sizeof(*blob->pe)); /* negative offset */
889 entry->data = blob->pe;
890 entry->length = blob->pvlen - sizeof(blob->il) - sizeof(blob->dl);
891 rdlen = regionSwab(entry+1, ril-1, 0, blob->pe+1,
892 blob->dataStart, blob->dataEnd,
893 entry->info.offset, fast);
894 if (rdlen < 0)
895 goto errxit;
896 entry->rdlen = rdlen;
897
898 if (ril < h->indexUsed) {
899 indexEntry newEntry = entry + ril;
900 int ne = (h->indexUsed - ril);
901 int rid = entry->info.offset+1;
902
903 /* Load dribble entries from region. */
904 rdlen = regionSwab(newEntry, ne, rdlen, blob->pe+ril,
905 blob->dataStart, blob->dataEnd, rid, fast);
906 if (rdlen < 0)
907 goto errxit;
908
909 { indexEntry firstEntry = newEntry;
910 int save = h->indexUsed;
911 int j;
912
913 /* Dribble entries replace duplicate region entries. */
914 h->indexUsed -= ne;
915 for (j = 0; j < ne; j++, newEntry++) {
916 (void) headerDel(h, newEntry->info.tag);
917 if (newEntry->info.tag == RPMTAG_BASENAMES)
918 (void) headerDel(h, RPMTAG_OLDFILENAMES);
919 }
920
921 /* If any duplicate entries were replaced, move new entries down. */
922 if (h->indexUsed < (save - ne)) {
923 memmove(h->index + h->indexUsed, firstEntry,
924 (ne * sizeof(*entry)));
925 }
926 h->indexUsed += ne;
927 }
928 }
929
930 rdlen += REGION_TAG_COUNTsizeof(struct entryInfo_s);
931
932 if (rdlen != blob->dl)
933 goto errxit;
934 }
935
936 /* Force sorting, dribble lookups can cause early sort on partial header */
937 h->sorted = HEADERSORT_NONE;
938 headerSort(h);
939 h->flags |= HEADERFLAG_ALLOCATED;
940 if (hdrp)
941 *hdrp = h;
942
943 /* We own the memory now, avoid double-frees */
944 blob->ei = NULL((void*)0);
945
946 return RPMRC_OK;
947
948errxit:
949 if (h) {
950 free(h->index);
951 free(h);
952 rasprintf(emsg, _("hdr load: BAD")dcgettext ("rpm", "hdr load: BAD", 5));
953 }
954 return RPMRC_FAIL;
955}
956
957Header headerReload(Header h, rpmTagVal tag)
958{
959 Header nh;
960 unsigned int uc = 0;
961 void * uh = headerExport(h, &uc);
962
963 h = headerFree(h);
Value stored to 'h' is never read
964 if (uh == NULL((void*)0))
965 return NULL((void*)0);
966 nh = headerImport(uh, uc, 0);
967 if (nh == NULL((void*)0)) {
968 uh = _free(uh)rfree((uh));
969 return NULL((void*)0);
970 }
971 if (ENTRY_IS_REGION(nh->index)(((nh->index)->info.tag >= RPMTAG_HEADERIMAGE) &&
((nh->index)->info.tag < RPMTAG_HEADERREGIONS))
) {
972 if (tag == RPMTAG_HEADERSIGNATURES || tag == RPMTAG_HEADERIMMUTABLE)
973 nh->index[0].info.tag = tag;
974 }
975 return nh;
976}
977
978Header headerLoad(void * uh)
979{
980 return headerImport(uh, 0, 0);
981}
982
983Header headerCopyLoad(const void * uh)
984{
985 /* Discards const but that's ok as we'll take a copy */
986 return headerImport((void *)uh, 0, HEADERIMPORT_COPY);
987}
988
989Header headerRead(FD_t fd, int magicp)
990{
991 Header h = NULL((void*)0);
992 struct hdrblob_s blob;
993 char *buf = NULL((void*)0);
994
995 if (hdrblobRead(fd, magicp, 0, 0, &blob, &buf) == RPMRC_OK)
996 hdrblobImport(&blob, 0, &h, &buf);
997
998 free(buf);
999 return h;
1000}
1001
1002int headerWrite(FD_t fd, Header h, int magicp)
1003{
1004 ssize_t nb;
1005 unsigned int length;
1006 void * uh = headerExport(h, &length);
1007
1008 if (uh == NULL((void*)0))
1009 return 1;
1010
1011 if (magicp == HEADER_MAGIC_YES) {
1012 nb = Fwrite(rpm_header_magic, sizeof(rpm_header_magic), 1, fd);
1013 if (nb != sizeof(rpm_header_magic))
1014 goto exit;
1015 }
1016
1017 nb = Fwrite(uh, sizeof(char), length, fd);
1018
1019exit:
1020 free(uh);
1021 return (nb == length ? 0 : 1);
1022}
1023
1024int headerIsEntry(Header h, rpmTagVal tag)
1025{
1026 /* FIX: h modified by sort. */
1027 return (findEntry(h, tag, RPM_NULL_TYPE) ? 1 : 0);
1028
1029}
1030
1031/* simple heuristic to find out if the header is from * a source rpm
1032 * or not: source rpms contain at least the spec file and have all
1033 * files in one directory with an empty name.
1034 */
1035int headerIsSourceHeuristic(Header h)
1036{
1037 indexEntry entry = findEntry(h, RPMTAG_DIRNAMES, RPM_STRING_ARRAY_TYPE);
1038 return entry && entry->info.count == 1 && entry->data && !*(const char *)entry->data;
1039}
1040
1041/** \ingroup header
1042 * Retrieve data from header entry.
1043 * Relevant flags (others are ignored), if neither is set allocation
1044 * behavior depends on data type(!)
1045 * HEADERGET_MINMEM: return pointers to header memory
1046 * HEADERGET_ALLOC: always return malloced memory, overrides MINMEM
1047 *
1048 * @todo Permit retrieval of regions other than HEADER_IMUTABLE.
1049 * @param entry header entry
1050 * @param td tag data container
1051 * @param flags flags to control memory allocation
1052 * @return 1 on success, otherwise error.
1053 */
1054static int copyTdEntry(const indexEntry entry, rpmtd td, headerGetFlags flags)
1055{
1056 rpm_count_t count = entry->info.count;
1057 int rc = 1; /* XXX 1 on success. */
1058 /* ALLOC overrides MINMEM */
1059 int allocMem = flags & HEADERGET_ALLOC;
1060 int minMem = allocMem ? 0 : flags & HEADERGET_MINMEM;
1061 int argvArray = (flags & HEADERGET_ARGV) ? 1 : 0;
1062
1063 assert(td != NULL)({ if (td != ((void*)0)) ; else __assert_fail ("td != NULL", "header.c"
, 1063, __PRETTY_FUNCTION__); })
;
1064 td->flags = RPMTD_IMMUTABLE;
1065 switch (entry->info.type) {
1066 case RPM_BIN_TYPE:
1067 /*
1068 * XXX This only works for
1069 * XXX "sealed" HEADER_IMMUTABLE/HEADER_SIGNATURES/HEADER_IMAGE.
1070 * XXX This will *not* work for unsealed legacy HEADER_IMAGE (i.e.
1071 * XXX a legacy header freshly read, but not yet unloaded to the rpmdb).
1072 */
1073 if (ENTRY_IS_REGION(entry)(((entry)->info.tag >= RPMTAG_HEADERIMAGE) && (
(entry)->info.tag < RPMTAG_HEADERREGIONS))
) {
1074 int32_t * ei = ((int32_t *)entry->data) - 2;
1075 entryInfo pe = (entryInfo) (ei + 2);
1076 unsigned char * dataStart = (unsigned char *) (pe + ntohl(ei[0])(__extension__ ({ unsigned int __v, __x = (ei[0]); if (__builtin_constant_p
(__x)) __v = ((((__x) & 0xff000000) >> 24) | (((__x
) & 0x00ff0000) >> 8) | (((__x) & 0x0000ff00) <<
8) | (((__x) & 0x000000ff) << 24)); else __asm__ (
"bswap %0" : "=r" (__v) : "0" (__x)); __v; }))
);
1077 int32_t rdl = -entry->info.offset; /* negative offset */
1078 int32_t ril = rdl/sizeof(*pe);
1079
1080 rdl = entry->rdlen;
1081 count = 2 * sizeof(*ei) + (ril * sizeof(*pe)) + rdl;
1082 if (entry->info.tag == RPMTAG_HEADERIMAGE) {
1083 ril -= 1;
1084 pe += 1;
1085 } else {
1086 count += REGION_TAG_COUNTsizeof(struct entryInfo_s);
1087 rdl += REGION_TAG_COUNTsizeof(struct entryInfo_s);
1088 }
1089
1090 td->data = xmalloc(count)rmalloc((count));
1091 ei = (int32_t *) td->data;
1092 ei[0] = htonl(ril)(__extension__ ({ unsigned int __v, __x = (ril); if (__builtin_constant_p
(__x)) __v = ((((__x) & 0xff000000) >> 24) | (((__x
) & 0x00ff0000) >> 8) | (((__x) & 0x0000ff00) <<
8) | (((__x) & 0x000000ff) << 24)); else __asm__ (
"bswap %0" : "=r" (__v) : "0" (__x)); __v; }))
;
1093 ei[1] = htonl(rdl)(__extension__ ({ unsigned int __v, __x = (rdl); if (__builtin_constant_p
(__x)) __v = ((((__x) & 0xff000000) >> 24) | (((__x
) & 0x00ff0000) >> 8) | (((__x) & 0x0000ff00) <<
8) | (((__x) & 0x000000ff) << 24)); else __asm__ (
"bswap %0" : "=r" (__v) : "0" (__x)); __v; }))
;
1094
1095 pe = (entryInfo) memcpy(ei + 2, pe, (ril * sizeof(*pe)));
1096
1097 dataStart = (unsigned char *) memcpy(pe + ril, dataStart, rdl);
1098
1099 rc = regionSwab(NULL((void*)0), ril, 0, pe, dataStart, dataStart + rdl, 0, 0);
1100 /* don't return data on failure */
1101 if (rc < 0) {
1102 td->data = _free(td->data)rfree((td->data));
1103 }
1104 /* XXX 1 on success. */
1105 rc = (rc < 0) ? 0 : 1;
1106 } else {
1107 count = entry->length;
1108 td->data = (!minMem
1109 ? memcpy(xmalloc(count)rmalloc((count)), entry->data, count)
1110 : entry->data);
1111 }
1112 break;
1113 case RPM_STRING_TYPE:
1114 /* simple string, but fallthrough if its actually an array */
1115 if (count == 1 && !argvArray) {
1116 td->data = allocMem ? xstrdup(entry->data)rstrdup((entry->data)) : entry->data;
1117 break;
1118 }
1119 case RPM_STRING_ARRAY_TYPE:
1120 case RPM_I18NSTRING_TYPE:
1121 { const char ** ptrEntry;
1122 int tableSize = (count + argvArray) * sizeof(char *);
1123 char * t;
1124 int i;
1125
1126 if (minMem) {
1127 td->data = xmalloc(tableSize)rmalloc((tableSize));
1128 ptrEntry = (const char **) td->data;
1129 t = entry->data;
1130 } else {
1131 t = xmalloc(tableSize + entry->length)rmalloc((tableSize + entry->length));
1132 td->data = (void *)t;
1133 ptrEntry = (const char **) td->data;
1134 t += tableSize;
1135 memcpy(t, entry->data, entry->length);
1136 }
1137 for (i = 0; i < count; i++) {
1138 *ptrEntry++ = t;
1139 t = strchr(t, 0);
1140 t++;
1141 }
1142 if (argvArray) {
1143 *ptrEntry = NULL((void*)0);
1144 td->flags |= RPMTD_ARGV;
1145 }
1146 } break;
1147 case RPM_CHAR_TYPE:
1148 case RPM_INT8_TYPE:
1149 case RPM_INT16_TYPE:
1150 case RPM_INT32_TYPE:
1151 case RPM_INT64_TYPE:
1152 if (allocMem) {
1153 td->data = xmalloc(entry->length)rmalloc((entry->length));
1154 memcpy(td->data, entry->data, entry->length);
1155 } else {
1156 td->data = entry->data;
1157 }
1158 break;
1159 default:
1160 /* WTH? Don't mess with unknown data types... */
1161 rc = 0;
1162 td->data = NULL((void*)0);
1163 break;
1164 }
1165 td->type = entry->info.type;
1166 td->count = count;
1167 td->size = entry->length;
1168
1169 if (td->data && entry->data != td->data) {
1170 td->flags |= RPMTD_ALLOCED;
1171 }
1172
1173 return rc;
1174}
1175
1176/**
1177 * Does locale match entry in header i18n table?
1178 *
1179 * \verbatim
1180 * The range [l,le) contains the next locale to match:
1181 * ll[_CC][.EEEEE][@dddd]
1182 * where
1183 * ll ISO language code (in lowercase).
1184 * CC (optional) ISO coutnry code (in uppercase).
1185 * EEEEE (optional) encoding (not really standardized).
1186 * dddd (optional) dialect.
1187 * \endverbatim
1188 *
1189 * @param td header i18n table data, NUL terminated
1190 * @param l start of locale to match
1191 * @param le end of locale to match
1192 * @return 1 on good match, 2 on weak match, 0 on no match
1193 */
1194static int headerMatchLocale(const char *td, const char *l, const char *le)
1195{
1196 const char *fe;
1197
1198 /* First try a complete match. */
1199 if (strlen(td) == (le-l) && rstreqn(td, l, (le - l)))
1200 return 1;
1201
1202 /* Next, try stripping optional dialect and matching. */
1203 for (fe = l; fe < le && *fe != '@'; fe++)
1204 {};
1205 if (fe < le && rstreqn(td, l, (fe - l)))
1206 return 1;
1207
1208 /* Next, try stripping optional codeset and matching. */
1209 for (fe = l; fe < le && *fe != '.'; fe++)
1210 {};
1211 if (fe < le && rstreqn(td, l, (fe - l)))
1212 return 1;
1213
1214 /* Finally, try stripping optional country code and matching. */
1215 for (fe = l; fe < le && *fe != '_'; fe++)
1216 {};
1217 if (fe < le && rstreqn(td, l, (fe - l)))
1218 return 2;
1219
1220 return 0;
1221}
1222
1223/**
1224 * Return i18n string from header that matches locale.
1225 * @param h header
1226 * @param entry i18n string data
1227 * @retval td tag data container
1228 * @param flags flags to control allocation
1229 * @return 1 always
1230 */
1231static int copyI18NEntry(Header h, indexEntry entry, rpmtd td,
1232 headerGetFlags flags)
1233{
1234 const char *lang, *l, *le;
1235 indexEntry table;
1236
1237 td->type = RPM_STRING_TYPE;
1238 td->count = 1;
1239 /* if no match, just return the first string */
1240 td->data = entry->data;
1241
1242 /* XXX Drepper sez' this is the order. */
1243 if ((lang = getenv("LANGUAGE")secure_getenv("LANGUAGE")) == NULL((void*)0) &&
1244 (lang = getenv("LC_ALL")secure_getenv("LC_ALL")) == NULL((void*)0) &&
1245 (lang = getenv("LC_MESSAGES")secure_getenv("LC_MESSAGES")) == NULL((void*)0) &&
1246 (lang = getenv("LANG")secure_getenv("LANG")) == NULL((void*)0))
1247 goto exit;
1248
1249 if ((table = findEntry(h, RPMTAG_HEADERI18NTABLE, RPM_STRING_ARRAY_TYPE)) == NULL((void*)0))
1250 goto exit;
1251
1252 for (l = lang; *l != '\0'; l = le) {
1253 const char *t;
1254 char *ed, *ed_weak = NULL((void*)0);
1255 int langNum;
1256
1257 while (*l && *l == ':') /* skip leading colons */
1258 l++;
1259 if (*l == '\0')
1260 break;
1261 for (le = l; *le && *le != ':'; le++) /* find end of this locale */
1262 {};
1263
1264 /* For each entry in the header ... */
1265 for (langNum = 0, t = table->data, ed = entry->data;
1266 langNum < entry->info.count;
1267 langNum++, t += strlen(t) + 1, ed += strlen(ed) + 1) {
1268
1269 int match = headerMatchLocale(t, l, le);
1270 if (match == 1) {
1271 td->data = ed;
1272 goto exit;
1273 } else if (match == 2) {
1274 ed_weak = ed;
1275 }
1276 }
1277 if (ed_weak) {
1278 td->data = ed_weak;
1279 goto exit;
1280 }
1281 }
1282
1283exit:
1284 if (flags & HEADERGET_ALLOC) {
1285 td->data = xstrdup(td->data)rstrdup((td->data));
1286 td->flags |= RPMTD_ALLOCED;
1287 }
1288
1289 return 1;
1290}
1291
1292/**
1293 * Retrieve tag data from header.
1294 * @param h header
1295 * @retval td tag data container
1296 * @param flags flags to control retrieval
1297 * @return 1 on success, 0 on not found
1298 */
1299static int intGetTdEntry(Header h, rpmtd td, headerGetFlags flags)
1300{
1301 indexEntry entry;
1302 int rc;
1303
1304 /* First find the tag */
1305 /* FIX: h modified by sort. */
1306 entry = findEntry(h, td->tag, RPM_NULL_TYPE);
1307 if (entry == NULL((void*)0)) {
1308 /* Td is zeroed above, just return... */
1309 return 0;
1310 }
1311
1312 if (entry->info.type == RPM_I18NSTRING_TYPE && !(flags & HEADERGET_RAW))
1313 rc = copyI18NEntry(h, entry, td, flags);
1314 else
1315 rc = copyTdEntry(entry, td, flags);
1316
1317 if (rc == 0)
1318 td->flags |= RPMTD_INVALID;
1319
1320 /* XXX 1 on success */
1321 return ((rc == 1) ? 1 : 0);
1322}
1323
1324int headerGet(Header h, rpmTagVal tag, rpmtd td, headerGetFlags flags)
1325{
1326 int rc;
1327 headerTagTagFunction tagfunc = intGetTdEntry;
1328
1329 if (td == NULL((void*)0)) return 0;
1330
1331 rpmtdReset(td);
1332 td->tag = tag;
1333
1334 if (flags & HEADERGET_EXT) {
1335 headerTagTagFunction extfunc = rpmHeaderTagFunc(tag);
1336 if (extfunc) tagfunc = extfunc;
1337 }
1338 rc = tagfunc(h, td, flags);
1339
1340 assert(tag == td->tag)({ if (tag == td->tag) ; else __assert_fail ("tag == td->tag"
, "header.c", 1340, __PRETTY_FUNCTION__); })
;
1341 return rc;
1342}
1343
1344/**
1345 */
1346static void copyData(rpm_tagtype_t type, rpm_data_t dstPtr,
1347 rpm_constdata_t srcPtr, rpm_count_t cnt, int dataLength)
1348{
1349 switch (type) {
1350 case RPM_STRING_ARRAY_TYPE:
1351 case RPM_I18NSTRING_TYPE:
1352 { const char ** av = (const char **) srcPtr;
1353 char * t = dstPtr;
1354
1355 while (cnt-- > 0 && dataLength > 0) {
1356 const char * s;
1357 if ((s = *av++) == NULL((void*)0))
1358 continue;
1359 do {
1360 *t++ = *s++;
1361 } while (s[-1] && --dataLength > 0);
1362 }
1363 } break;
1364
1365 default:
1366 memmove(dstPtr, srcPtr, dataLength);
1367 break;
1368 }
1369}
1370
1371/**
1372 * Return (malloc'ed) copy of entry data.
1373 * @param type entry data type
1374 * @param p entry data
1375 * @param c entry item count
1376 * @retval lengthPtr no. bytes in returned data
1377 * @return (malloc'ed) copy of entry data, NULL on error
1378 */
1379static void *
1380grabData(rpm_tagtype_t type, rpm_constdata_t p, rpm_count_t c, int * lengthPtr)
1381{
1382 rpm_data_t data = NULL((void*)0);
1383 int length;
1384
1385 length = dataLength(type, p, c, 0, NULL((void*)0));
1386 if (length > 0) {
1387 data = xmalloc(length)rmalloc((length));
1388 copyData(type, data, p, c, length);
1389 }
1390
1391 if (lengthPtr)
1392 *lengthPtr = length;
1393 return data;
1394}
1395
1396static int intAddEntry(Header h, rpmtd td)
1397{
1398 indexEntry entry;
1399 rpm_data_t data;
1400 int length = 0;
1401
1402 /* Count must always be >= 1 for headerAddEntry. */
1403 if (td->count <= 0)
1404 return 0;
1405
1406 if (hdrchkType(td->type)((td->type) < 0 || (td->type) > 9))
1407 return 0;
1408 if (hdrchkData(td->count)((td->count) & (~0x0fffffff)))
1409 return 0;
1410
1411 data = grabData(td->type, td->data, td->count, &length);
1412 if (data == NULL((void*)0))
1413 return 0;
1414
1415 /* Allocate more index space if necessary */
1416 if (h->indexUsed == h->indexAlloced) {
1417 h->indexAlloced += INDEX_MALLOC_SIZE8;
1418 h->index = xrealloc(h->index, h->indexAlloced * sizeof(*h->index))rrealloc((h->index), (h->indexAlloced * sizeof(*h->index
)))
;
1419 }
1420
1421 /* Fill in the index */
1422 entry = h->index + h->indexUsed;
1423 entry->info.tag = td->tag;
1424 entry->info.type = td->type;
1425 entry->info.count = td->count;
1426 entry->info.offset = 0;
1427 entry->data = data;
1428 entry->length = length;
1429
1430 if (h->indexUsed > 0 && td->tag < h->index[h->indexUsed-1].info.tag)
1431 h->sorted = HEADERSORT_NONE;
1432 h->indexUsed++;
1433
1434 return 1;
1435}
1436
1437static int intAppendEntry(Header h, rpmtd td)
1438{
1439 indexEntry entry;
1440 int length;
1441
1442 if (td->type == RPM_STRING_TYPE || td->type == RPM_I18NSTRING_TYPE) {
1443 /* we can't do this */
1444 return 0;
1445 }
1446
1447 /* Find the tag entry in the header. */
1448 entry = findEntry(h, td->tag, td->type);
1449 if (!entry)
1450 return 0;
1451
1452 length = dataLength(td->type, td->data, td->count, 0, NULL((void*)0));
1453 if (length < 0)
1454 return 0;
1455
1456 if (ENTRY_IN_REGION(entry)((entry)->info.offset < 0)) {
1457 char * t = xmalloc(entry->length + length)rmalloc((entry->length + length));
1458 memcpy(t, entry->data, entry->length);
1459 entry->data = t;
1460 entry->info.offset = 0;
1461 } else
1462 entry->data = xrealloc(entry->data, entry->length + length)rrealloc((entry->data), (entry->length + length));
1463
1464 copyData(td->type, ((char *) entry->data) + entry->length,
1465 td->data, td->count, length);
1466
1467 entry->length += length;
1468
1469 entry->info.count += td->count;
1470
1471 return 1;
1472}
1473
1474int headerPut(Header h, rpmtd td, headerPutFlags flags)
1475{
1476 int rc;
1477
1478 assert(td != NULL)({ if (td != ((void*)0)) ; else __assert_fail ("td != NULL", "header.c"
, 1478, __PRETTY_FUNCTION__); })
;
1479 if (flags & HEADERPUT_APPEND) {
1480 rc = findEntry(h, td->tag, td->type) ?
1481 intAppendEntry(h, td) :
1482 intAddEntry(h, td);
1483 } else {
1484 rc = intAddEntry(h, td);
1485 }
1486 return rc;
1487}
1488
1489int headerAddI18NString(Header h, rpmTagVal tag, const char * string,
1490 const char * lang)
1491{
1492 indexEntry table, entry;
1493 const char ** strArray;
1494 int length;
1495 int ghosts;
1496 rpm_count_t i, langNum;
1497 char * buf;
1498
1499 table = findEntry(h, RPMTAG_HEADERI18NTABLE, RPM_STRING_ARRAY_TYPE);
1500 entry = findEntry(h, tag, RPM_I18NSTRING_TYPE);
1501
1502 if (!table && entry)
1503 return 0; /* this shouldn't ever happen!! */
1504
1505 if (!table && !entry) {
1506 const char * charArray[2];
1507 rpm_count_t count = 0;
1508 struct rpmtd_s td;
1509 if (!lang || (lang[0] == 'C' && lang[1] == '\0')) {
1510 charArray[count++] = "C";
1511 } else {
1512 charArray[count++] = "C";
1513 charArray[count++] = lang;
1514 }
1515
1516 rpmtdReset(&td);
1517 td.tag = RPMTAG_HEADERI18NTABLE;
1518 td.type = RPM_STRING_ARRAY_TYPE;
1519 td.data = (void *) charArray;
1520 td.count = count;
1521 if (!headerPut(h, &td, HEADERPUT_DEFAULT))
1522 return 0;
1523 table = findEntry(h, RPMTAG_HEADERI18NTABLE, RPM_STRING_ARRAY_TYPE);
1524 }
1525
1526 if (!table)
1527 return 0;
1528 if (!lang) lang = "C";
1529
1530 { const char * l = table->data;
1531 for (langNum = 0; langNum < table->info.count; langNum++) {
1532 if (rstreq(l, lang)) break;
1533 l += strlen(l) + 1;
1534 }
1535 }
1536
1537 if (langNum >= table->info.count) {
1538 length = strlen(lang) + 1;
1539 if (ENTRY_IN_REGION(table)((table)->info.offset < 0)) {
1540 char * t = xmalloc(table->length + length)rmalloc((table->length + length));
1541 memcpy(t, table->data, table->length);
1542 table->data = t;
1543 table->info.offset = 0;
1544 } else
1545 table->data = xrealloc(table->data, table->length + length)rrealloc((table->data), (table->length + length));
1546 memmove(((char *)table->data) + table->length, lang, length);
1547 table->length += length;
1548 table->info.count++;
1549 }
1550
1551 if (!entry) {
1552 int rc;
1553 struct rpmtd_s td;
1554 strArray = xmalloc(sizeof(*strArray) * (langNum + 1))rmalloc((sizeof(*strArray) * (langNum + 1)));
1555 for (i = 0; i < langNum; i++)
1556 strArray[i] = "";
1557 strArray[langNum] = string;
1558
1559 rpmtdReset(&td);
1560 td.tag = tag;
1561 td.type = RPM_I18NSTRING_TYPE;
1562 td.data = strArray;
1563 td.count = langNum + 1;
1564 rc = headerPut(h, &td, HEADERPUT_DEFAULT);
1565 free(strArray);
1566 return rc;
1567 } else if (langNum >= entry->info.count) {
1568 ghosts = langNum - entry->info.count;
1569
1570 length = strlen(string) + 1 + ghosts;
1571 if (ENTRY_IN_REGION(entry)((entry)->info.offset < 0)) {
1572 char * t = xmalloc(entry->length + length)rmalloc((entry->length + length));
1573 memcpy(t, entry->data, entry->length);
1574 entry->data = t;
1575 entry->info.offset = 0;
1576 } else
1577 entry->data = xrealloc(entry->data, entry->length + length)rrealloc((entry->data), (entry->length + length));
1578
1579 memset(((char *)entry->data) + entry->length, '\0', ghosts);
1580 memmove(((char *)entry->data) + entry->length + ghosts, string, strlen(string)+1);
1581
1582 entry->length += length;
1583 entry->info.count = langNum + 1;
1584 } else {
1585 char *b, *be, *e, *ee, *t;
1586 size_t bn, sn, en;
1587
1588 /* Set beginning/end pointers to previous data */
1589 b = be = e = ee = entry->data;
1590 for (i = 0; i < table->info.count; i++) {
1591 if (i == langNum)
1592 be = ee;
1593 ee += strlen(ee) + 1;
1594 if (i == langNum)
1595 e = ee;
1596 }
1597
1598 /* Get storage for new buffer */
1599 bn = (be-b);
1600 sn = strlen(string) + 1;
1601 en = (ee-e);
1602 length = bn + sn + en;
1603 t = buf = xmalloc(length)rmalloc((length));
1604
1605 /* Copy values into new storage */
1606 memcpy(t, b, bn);
1607 t += bn;
1608 memcpy(t, string, sn);
1609 t += sn;
1610 memcpy(t, e, en);
1611 t += en;
1612
1613 /* Replace i18N string array */
1614 entry->length -= strlen(be) + 1;
1615 entry->length += sn;
1616
1617 if (ENTRY_IN_REGION(entry)((entry)->info.offset < 0)) {
1618 entry->info.offset = 0;
1619 } else
1620 entry->data = _free(entry->data)rfree((entry->data));
1621 entry->data = buf;
1622 }
1623
1624 return 0;
1625}
1626
1627int headerMod(Header h, rpmtd td)
1628{
1629 indexEntry entry;
1630 rpm_data_t oldData;
1631 rpm_data_t data;
1632 int length = 0;
1633
1634 /* First find the tag */
1635 entry = findEntry(h, td->tag, td->type);
1636 if (!entry)
1637 return 0;
1638
1639 data = grabData(td->type, td->data, td->count, &length);
1640 if (data == NULL((void*)0))
1641 return 0;
1642
1643 /* make sure entry points to the first occurrence of this tag */
1644 while (entry > h->index && (entry - 1)->info.tag == td->tag)
1645 entry--;
1646
1647 /* free after we've grabbed the new data in case the two are intertwined;
1648 that's a bad idea but at least we won't break */
1649 oldData = entry->data;
1650
1651 entry->info.count = td->count;
1652 entry->info.type = td->type;
1653 entry->data = data;
1654 entry->length = length;
1655
1656 if (ENTRY_IN_REGION(entry)((entry)->info.offset < 0)) {
1657 entry->info.offset = 0;
1658 } else
1659 free(oldData);
1660
1661 return 1;
1662}
1663
1664/**
1665 * Header tag iterator data structure.
1666 */
1667struct headerIterator_s {
1668 Header h; /*!< Header being iterated. */
1669 int next_index; /*!< Next tag index. */
1670};
1671
1672HeaderIterator headerFreeIterator(HeaderIterator hi)
1673{
1674 if (hi != NULL((void*)0)) {
1675 hi->h = headerFree(hi->h);
1676 hi = _free(hi)rfree((hi));
1677 }
1678 return NULL((void*)0);
1679}
1680
1681HeaderIterator headerInitIterator(Header h)
1682{
1683 HeaderIterator hi = xmalloc(sizeof(*hi))rmalloc((sizeof(*hi)));
1684
1685 headerSort(h);
1686
1687 hi->h = headerLink(h);
1688 hi->next_index = 0;
1689 return hi;
1690}
1691
1692static indexEntry nextIndex(HeaderIterator hi)
1693{
1694 Header h = hi->h;
1695 int slot;
1696 indexEntry entry = NULL((void*)0);
1697
1698 for (slot = hi->next_index; slot < h->indexUsed; slot++) {
1699 entry = h->index + slot;
1700 if (!ENTRY_IS_REGION(entry)(((entry)->info.tag >= RPMTAG_HEADERIMAGE) && (
(entry)->info.tag < RPMTAG_HEADERREGIONS))
)
1701 break;
1702 }
1703 hi->next_index = slot;
1704 if (entry == NULL((void*)0) || slot >= h->indexUsed)
1705 return NULL((void*)0);
1706
1707 hi->next_index++;
1708 return entry;
1709}
1710
1711rpmTagVal headerNextTag(HeaderIterator hi)
1712{
1713 indexEntry entry = nextIndex(hi);
1714 return entry ? entry->info.tag : RPMTAG_NOT_FOUND;
1715}
1716
1717int headerNext(HeaderIterator hi, rpmtd td)
1718{
1719 indexEntry entry = nextIndex(hi);
1720 int rc = 0;
1721
1722 rpmtdReset(td);
1723 if (entry) {
1724 td->tag = entry->info.tag;
1725 rc = copyTdEntry(entry, td, HEADERGET_DEFAULT);
1726 }
1727 return ((rc == 1) ? 1 : 0);
1728}
1729
1730unsigned int headerGetInstance(Header h)
1731{
1732 return h ? h->instance : 0;
1733}
1734
1735void headerSetInstance(Header h, unsigned int instance)
1736{
1737 h->instance = instance;
1738}
1739
1740#define RETRY_ERROR(_err)((_err) == 4 || (_err) == 11 || (_err) == 11) \
1741 ((_err) == EINTR4 || (_err) == EAGAIN11 || (_err) == EWOULDBLOCK11)
1742
1743ssize_t Freadall(FD_t fd, void * buf, ssize_t size)
1744{
1745 ssize_t total = 0;
1746 ssize_t nb = 0;
1747 char * bufp = buf;
1748
1749 while (total < size) {
1750 nb = Fread(bufp, 1, size - total, fd);
1751
1752 if (nb == 0 || (nb < 0 && !RETRY_ERROR(errno)(((*__errno_location ())) == 4 || ((*__errno_location ())) ==
11 || ((*__errno_location ())) == 11)
)) {
1753 total = nb;
1754 break;
1755 }
1756
1757 if (nb > 0) {
1758 bufp += nb;
1759 total += nb;
1760 }
1761 }
1762
1763 return total;
1764}
1765
1766static rpmRC hdrblobVerifyRegion(rpmTagVal regionTag, int exact_size,
1767 hdrblob blob, char **buf)
1768{
1769 rpmRC rc = RPMRC_FAIL;
1770 struct entryInfo_s trailer, einfo;
1771 unsigned char * regionEnd = NULL((void*)0);
1772
1773 /* Check that we have at least on tag */
1774 if (blob->il < 1) {
1775 rasprintf(buf, _("region: no tags")dcgettext ("rpm", "region: no tags", 5));
1776 goto exit;
1777 }
1778
1779 /* Convert the 1st tag element. */
1780 ei2h(blob->pe, &einfo);
1781
1782 if (!regionTag && (einfo.tag == RPMTAG_HEADERSIGNATURES ||
1783 einfo.tag == RPMTAG_HEADERIMMUTABLE ||
1784 einfo.tag == RPMTAG_HEADERIMAGE)) {
1785 regionTag = einfo.tag;
1786 }
1787
1788 /* Is there an immutable header region tag? */
1789 if (!(einfo.tag == regionTag)) {
1790 rc = RPMRC_NOTFOUND;
1791 goto exit;
1792 }
1793
1794 /* Is the region tag sane? */
1795 if (!(einfo.type == REGION_TAG_TYPERPM_BIN_TYPE && einfo.count == REGION_TAG_COUNTsizeof(struct entryInfo_s))) {
1796 rasprintf(buf,
1797 _("region tag: BAD, tag %d type %d offset %d count %d")dcgettext ("rpm", "region tag: BAD, tag %d type %d offset %d count %d"
, 5)
,
1798 einfo.tag, einfo.type, einfo.offset, einfo.count);
1799 goto exit;
1800 }
1801
1802 /* Is the trailer within the data area? */
1803 if (hdrchkRange(blob->dl, einfo.offset + REGION_TAG_COUNT)((einfo.offset + sizeof(struct entryInfo_s)) < 0 || (einfo
.offset + sizeof(struct entryInfo_s)) > (blob->dl))
) {
1804 rasprintf(buf,
1805 _("region offset: BAD, tag %d type %d offset %d count %d")dcgettext ("rpm", "region offset: BAD, tag %d type %d offset %d count %d"
, 5)
,
1806 einfo.tag, einfo.type, einfo.offset, einfo.count);
1807 goto exit;
1808 }
1809
1810 /* Is there an immutable header region tag trailer? */
1811 memset(&trailer, 0, sizeof(trailer));
1812 regionEnd = blob->dataStart + einfo.offset;
1813 (void) memcpy(&trailer, regionEnd, REGION_TAG_COUNTsizeof(struct entryInfo_s));
1814 regionEnd += REGION_TAG_COUNTsizeof(struct entryInfo_s);
1815 blob->rdl = regionEnd - blob->dataStart;
1816
1817 ei2h(&trailer, &einfo);
1818 /* Trailer offset is negative and has a special meaning */
1819 einfo.offset = -einfo.offset;
1820 if (!(einfo.tag == regionTag &&
1821 einfo.type == REGION_TAG_TYPERPM_BIN_TYPE && einfo.count == REGION_TAG_COUNTsizeof(struct entryInfo_s)))
1822 {
1823 rasprintf(buf,
1824 _("region trailer: BAD, tag %d type %d offset %d count %d")dcgettext ("rpm", "region trailer: BAD, tag %d type %d offset %d count %d"
, 5)
,
1825 einfo.tag, einfo.type, einfo.offset, einfo.count);
1826 goto exit;
1827 }
1828
1829 /* Does the region actually fit within the header? */
1830 blob->ril = einfo.offset/sizeof(*blob->pe);
1831 if ((einfo.offset % sizeof(*blob->pe)) || hdrchkRange(blob->il, blob->ril)((blob->ril) < 0 || (blob->ril) > (blob->il)) ||
1832 hdrchkRange(blob->dl, blob->rdl)((blob->rdl) < 0 || (blob->rdl) > (blob->dl))) {
1833 rasprintf(buf, _("region %d size: BAD, ril %d il %d rdl %d dl %d")dcgettext ("rpm", "region %d size: BAD, ril %d il %d rdl %d dl %d"
, 5)
,
1834 regionTag, blob->ril, blob->il, blob->rdl, blob->dl);
1835 goto exit;
1836 }
1837
1838 /* In package files region size is expected to match header size. */
1839 if (exact_size && !(blob->il == blob->ril && blob->dl == blob->rdl)) {
1840 rasprintf(buf,
1841 _("region %d: tag number mismatch %d ril %d dl %d rdl %d\n")dcgettext ("rpm", "region %d: tag number mismatch %d ril %d dl %d rdl %d\n"
, 5)
,
1842 regionTag, blob->il, blob->ril, blob->dl, blob->rdl);
1843 goto exit;
1844 }
1845
1846 blob->regionTag = regionTag;
1847 rc = RPMRC_OK;
1848
1849exit:
1850 return rc;
1851}
1852
1853rpmRC hdrblobRead(FD_t fd, int magic, int exact_size, rpmTagVal regionTag, hdrblob blob, char **emsg)
1854{
1855 int32_t block[4];
1856 int32_t *bs = (magic != 0) ? &block[0] : &block[2];
1857 int blen = (magic != 0) ? sizeof(block) : sizeof(block) / 2;
1858 int32_t il;
1859 int32_t dl;
1860 int32_t * ei = NULL((void*)0);
1861 size_t uc;
1862 size_t nb;
1863 rpmRC rc = RPMRC_FAIL; /* assume failure */
1864 int xx;
1865 int32_t il_max = HEADER_TAGS_MAX0x0000ffff;
1866 int32_t dl_max = HEADER_DATA_MAX0x0fffffff;
1867
1868 if (regionTag == RPMTAG_HEADERSIGNATURES) {
1869 il_max = 32;
1870 dl_max = 8192;
1871 }
1872
1873 memset(block, 0, sizeof(block));
1874 if ((xx = Freadall(fd, bs, blen)) != blen) {
1875 rasprintf(emsg,
1876 _("hdr size(%d): BAD, read returned %d")dcgettext ("rpm", "hdr size(%d): BAD, read returned %d", 5), blen, xx);
1877 goto exit;
1878 }
1879 if (magic && memcmp(block, rpm_header_magic, sizeof(rpm_header_magic))) {
1880 rasprintf(emsg, _("hdr magic: BAD")dcgettext ("rpm", "hdr magic: BAD", 5));
1881 goto exit;
1882 }
1883 il = ntohl(block[2])(__extension__ ({ unsigned int __v, __x = (block[2]); if (__builtin_constant_p
(__x)) __v = ((((__x) & 0xff000000) >> 24) | (((__x
) & 0x00ff0000) >> 8) | (((__x) & 0x0000ff00) <<
8) | (((__x) & 0x000000ff) << 24)); else __asm__ (
"bswap %0" : "=r" (__v) : "0" (__x)); __v; }))
;
1884 if (hdrchkRange(il_max, il)((il) < 0 || (il) > (il_max))) {
1885 rasprintf(emsg, _("hdr tags: BAD, no. of tags(%d) out of range")dcgettext ("rpm", "hdr tags: BAD, no. of tags(%d) out of range"
, 5)
, il);
1886 goto exit;
1887 }
1888 dl = ntohl(block[3])(__extension__ ({ unsigned int __v, __x = (block[3]); if (__builtin_constant_p
(__x)) __v = ((((__x) & 0xff000000) >> 24) | (((__x
) & 0x00ff0000) >> 8) | (((__x) & 0x0000ff00) <<
8) | (((__x) & 0x000000ff) << 24)); else __asm__ (
"bswap %0" : "=r" (__v) : "0" (__x)); __v; }))
;
1889 if (hdrchkRange(dl_max, dl)((dl) < 0 || (dl) > (dl_max))) {
1890 rasprintf(emsg, _("hdr data: BAD, no. of bytes(%d) out of range")dcgettext ("rpm", "hdr data: BAD, no. of bytes(%d) out of range"
, 5)
, dl);
1891 goto exit;
1892 }
1893
1894 nb = (il * sizeof(struct entryInfo_s)) + dl;
1895 uc = sizeof(il) + sizeof(dl) + nb;
1896 ei = xmalloc(uc)rmalloc((uc));
1897 ei[0] = block[2];
1898 ei[1] = block[3];
1899 if ((xx = Freadall(fd, (char *)&ei[2], nb)) != nb) {
1900 rasprintf(emsg, _("hdr blob(%zd): BAD, read returned %d")dcgettext ("rpm", "hdr blob(%zd): BAD, read returned %d", 5), nb, xx);
1901 goto exit;
1902 }
1903
1904 if (regionTag == RPMTAG_HEADERSIGNATURES) {
1905 size_t sigSize = uc + sizeof(rpm_header_magic);
1906 size_t pad = (8 - (sigSize % 8)) % 8;
1907 size_t trc;
1908 if (pad && (trc = Freadall(fd, block, pad)) != pad) {
1909 rasprintf(emsg, _("sigh pad(%zd): BAD, read %zd bytes")dcgettext ("rpm", "sigh pad(%zd): BAD, read %zd bytes", 5), pad, trc);
1910 goto exit;
1911 }
1912 }
1913
1914 rc = hdrblobInit(ei, uc, regionTag, exact_size, blob, emsg);
1915
1916exit:
1917 if (rc != RPMRC_OK) {
1918 free(ei);
1919 blob->ei = NULL((void*)0);
1920 if (emsg && *emsg && regionTag == RPMTAG_HEADERSIGNATURES) {
1921 /* rstrscat() cannot handle overlap even if it claims so */
1922 char *tmp = rstrscat(NULL((void*)0), _("signature ")dcgettext ("rpm", "signature ", 5), *emsg, NULL((void*)0));
1923 free(*emsg);
1924 *emsg = tmp;
1925 }
1926 }
1927
1928 return rc;
1929}
1930
1931rpmRC hdrblobInit(const void *uh, size_t uc,
1932 rpmTagVal regionTag, int exact_size,
1933 struct hdrblob_s *blob, char **emsg)
1934{
1935 rpmRC rc = RPMRC_FAIL;
1936
1937 memset(blob, 0, sizeof(*blob));
1938 blob->ei = (int32_t *) uh; /* discards const */
1939 blob->il = ntohl(blob->ei[0])(__extension__ ({ unsigned int __v, __x = (blob->ei[0]); if
(__builtin_constant_p (__x)) __v = ((((__x) & 0xff000000
) >> 24) | (((__x) & 0x00ff0000) >> 8) | (((__x
) & 0x0000ff00) << 8) | (((__x) & 0x000000ff) <<
24)); else __asm__ ("bswap %0" : "=r" (__v) : "0" (__x)); __v
; }))
;
1940 blob->dl = ntohl(blob->ei[1])(__extension__ ({ unsigned int __v, __x = (blob->ei[1]); if
(__builtin_constant_p (__x)) __v = ((((__x) & 0xff000000
) >> 24) | (((__x) & 0x00ff0000) >> 8) | (((__x
) & 0x0000ff00) << 8) | (((__x) & 0x000000ff) <<
24)); else __asm__ ("bswap %0" : "=r" (__v) : "0" (__x)); __v
; }))
;
1941 blob->pe = (entryInfo) &(blob->ei[2]);
1942 blob->pvlen = sizeof(blob->il) + sizeof(blob->dl) +
1943 (blob->il * sizeof(*blob->pe)) + blob->dl;
1944 blob->dataStart = (uint8_t *) (blob->pe + blob->il);
1945 blob->dataEnd = blob->dataStart + blob->dl;
1946
1947 /* Is the blob the right size? */
1948 if (blob->pvlen >= headerMaxbytes || (uc && blob->pvlen != uc)) {
1949 rasprintf(emsg, _("blob size(%d): BAD, 8 + 16 * il(%d) + dl(%d)")dcgettext ("rpm", "blob size(%d): BAD, 8 + 16 * il(%d) + dl(%d)"
, 5)
,
1950 blob->pvlen, blob->il, blob->dl);
1951 goto exit;
1952 }
1953
1954 if (hdrblobVerifyRegion(regionTag, exact_size, blob, emsg) == RPMRC_FAIL)
1955 goto exit;
1956
1957 /* Sanity check the rest of the header structure. */
1958 if (hdrblobVerifyInfo(blob, emsg))
1959 goto exit;
1960
1961 rc = RPMRC_OK;
1962
1963exit:
1964 return rc;
1965}
1966
1967rpmRC hdrblobGet(hdrblob blob, uint32_t tag, rpmtd td)
1968{
1969 rpmRC rc = RPMRC_NOTFOUND;
1970 struct indexEntry_s entry;
1971 struct entryInfo_s einfo;
1972 const struct entryInfo_s *pe = blob->pe;
1973 uint32_t ntag = htonl(tag)(__extension__ ({ unsigned int __v, __x = (tag); if (__builtin_constant_p
(__x)) __v = ((((__x) & 0xff000000) >> 24) | (((__x
) & 0x00ff0000) >> 8) | (((__x) & 0x0000ff00) <<
8) | (((__x) & 0x000000ff) << 24)); else __asm__ (
"bswap %0" : "=r" (__v) : "0" (__x)); __v; }))
;
1974 int tsize;
1975
1976 memset(&einfo, 0, sizeof(einfo));
1977 rpmtdReset(td);
1978
1979 for (int i = 1; i < blob->il; i++, pe++) {
1980 if (pe->tag != ntag)
1981 continue;
1982 ei2h(pe, &einfo);
1983
1984 /* We can only handle non-byteswappable data */
1985 tsize = typeSizes[einfo.type];
1986 if (tsize != 1 && tsize != -1)
1987 return RPMRC_FAIL;
1988
1989 entry.info = einfo; /* struct assignment */
1990 entry.data = blob->dataStart + einfo.offset;
1991 entry.length = dataLength(einfo.type, blob->dataStart + einfo.offset,
1992 einfo.count, 1, blob->dataEnd);
1993 entry.rdlen = 0;
1994 td->tag = einfo.tag;
1995 rc = copyTdEntry(&entry, td, HEADERGET_MINMEM) ? RPMRC_OK : RPMRC_FAIL;
1996 break;
1997 }
1998 return rc;
1999}
2000
2001Header headerImport(void * blob, unsigned int bsize, headerImportFlags flags)
2002{
2003 Header h = NULL((void*)0);
2004 struct hdrblob_s hblob;
2005 char *buf = NULL((void*)0);
2006 void * b = blob;
2007
2008 if (flags & HEADERIMPORT_COPY) {
2009 if (bsize == 0 && hdrblobInit(b, 0, 0, 0, &hblob, &buf) == RPMRC_OK)
2010 bsize = hblob.pvlen;
2011 if (bsize == 0)
2012 goto exit;
2013 b = memcpy(xmalloc(bsize)rmalloc((bsize)), b, bsize);
2014 }
2015
2016 /* Sanity checks on header intro. */
2017 if (hdrblobInit(b, bsize, 0, 0, &hblob, &buf) == RPMRC_OK)
2018 hdrblobImport(&hblob, (flags & HEADERIMPORT_FAST), &h, &buf);
2019
2020exit:
2021 if (h == NULL((void*)0) && b != blob)
2022 free(b);
2023 free(buf);
2024
2025 return h;
2026}