rpm  5.4.15
hdrNVR.c
Go to the documentation of this file.
1 
5 #include "system.h"
6 
7 #include <rpmiotypes.h>
8 #include <rpmmacro.h>
9 
10 #define _RPMTAG_INTERNAL
11 #include "header_internal.h" /* XXX hdrchkType(), hdrchkData() */
12 
13 #include "debug.h"
14 
19 /*@observer@*/ /*@unchecked@*/
20 static struct tagMacro {
21 /*@observer@*/ /*@null@*/
22  const char *macroname;
24 } tagMacros[] = {
25  { "name", RPMTAG_NAME },
26  { "version", RPMTAG_VERSION },
27  { "release", RPMTAG_RELEASE },
28  { "distepoch", RPMTAG_DISTEPOCH },
29  { "epoch", RPMTAG_EPOCH },
30  { "arch", RPMTAG_ARCH },
31  { "os", RPMTAG_OS },
32  { NULL, (rpmTag) 0 }
33 };
34 
35 /*@-globs -mods -incondefs@*/
37  /*@globals rpmGlobalMacroContext @*/
38  /*@modifies rpmGlobalMacroContext @*/
39 {
40  HE_t he = (HE_t) memset(alloca(sizeof(*he)), 0, sizeof(*he));
41  struct tagMacro * tagm;
42  char numbuf[64];
43  const char * val;
44  rpmuint64_t ival;
45  int xx;
46 
47  numbuf[0] = '\0';
48  /* XXX pre-expand %{buildroot} (if any) */
49  { const char *s = rpmExpand("%{?buildroot}", NULL);
50  if (s && *s)
51  (void) addMacro(NULL, "..buildroot", NULL, s, -1);
52  s = _free(s);
53  }
54  { const char *s = rpmExpand("%{?_builddir}", NULL);
55  if (s && *s)
56  (void) addMacro(NULL, ".._builddir", NULL, s, -1);
57  s = _free(s);
58  }
59 
60  for (tagm = tagMacros; tagm->macroname != NULL; tagm++) {
61  he->tag = tagm->tag;
62  xx = headerGet(h, he, 0);
63  if (!xx)
64  continue;
65  val = NULL;
66  ival = 0;
67  switch (he->t) {
68  case RPM_UINT8_TYPE:
69  ival = (rpmuint64_t)he->p.ui8p[0];
70  val = numbuf;
71  /*@switchbreak@*/ break;
72  case RPM_UINT16_TYPE:
73  ival = (rpmuint64_t)he->p.ui16p[0];
74  val = numbuf;
75  /*@switchbreak@*/ break;
76  case RPM_UINT32_TYPE:
77  ival = (rpmuint64_t)he->p.ui32p[0];
78  val = numbuf;
79  /*@switchbreak@*/ break;
80  case RPM_UINT64_TYPE:
81  ival = he->p.ui64p[0];
82  val = numbuf;
83  /*@switchbreak@*/ break;
84  case RPM_STRING_TYPE:
85  val = he->p.str;
86  /*@switchbreak@*/ break;
88 #if !defined(SUPPORT_I18NSTRING_TYPE)
89 assert(0);
90 #endif
92  case RPM_BIN_TYPE:
93  default:
94  /*@switchbreak@*/ break;
95  }
96 
97  if (val) {
98 /*@-duplicatequals@*/
99  if (val == numbuf)
100  sprintf(numbuf, "%llu", (unsigned long long)ival);
101 /*@=duplicatequals@*/
102  addMacro(NULL, tagm->macroname, NULL, val, -1);
103  }
104  he->p.ptr = _free(he->p.ptr);
105  }
106  return 0;
107 }
108 /*@=globs =mods =incondefs@*/
109 
110 /*@-globs -mods -incondefs@*/
112  /*@globals rpmGlobalMacroContext @*/
113  /*@modifies rpmGlobalMacroContext @*/
114 {
115  HE_t he = (HE_t) memset(alloca(sizeof(*he)), 0, sizeof(*he));
116  struct tagMacro * tagm;
117  int xx;
118 
119  for (tagm = tagMacros; tagm->macroname != NULL; tagm++) {
120  he->tag = tagm->tag;
121  xx = headerGet(h, he, 0);
122  if (!xx)
123  continue;
124  switch (he->t) {
125  case RPM_UINT32_TYPE:
126  delMacro(NULL, tagm->macroname);
127  /*@switchbreak@*/ break;
128  case RPM_STRING_TYPE:
129  delMacro(NULL, tagm->macroname);
130  /*@switchbreak@*/ break;
131  case RPM_I18NSTRING_TYPE:
132 #if !defined(SUPPORT_I18NSTRING_TYPE)
133 assert(0);
134 #endif
136  case RPM_BIN_TYPE:
137  case RPM_UINT8_TYPE:
138  case RPM_UINT16_TYPE:
139  case RPM_UINT64_TYPE:
140  default:
141  /*@switchbreak@*/ break;
142  }
143  he->p.ptr = _free(he->p.ptr);
144  }
145 
146  /* XXX restore previous %{buildroot} (if any) */
147  { const char *s = rpmExpand("%{?_builddir}", NULL);
148  if (s && *s)
149  (void) delMacro(NULL, "_builddir");
150  s = _free(s);
151  }
152  { const char *s = rpmExpand("%{?buildroot}", NULL);
153  if (s && *s)
154  (void) delMacro(NULL, "buildroot");
155  s = _free(s);
156  }
157 
158  return 0;
159 }
160 /*@=globs =mods =incondefs@*/
161 
162 int headerNEVRA(Header h, const char **np, /*@unused@*/ const char **ep,
163  const char **vp, const char **rp, const char **ap)
164 {
165  HE_t he = (HE_t) memset(alloca(sizeof(*he)), 0, sizeof(*he));
166 
167 /*@-onlytrans@*/
168  if (np) {
169  he->tag = RPMTAG_NAME;
170  if (headerGet(h, he, 0)
171  && he->t == RPM_STRING_TYPE && he->c == 1)
172  *np = xstrdup(he->p.str);
173  else
174  *np = NULL;
175  he->p.ptr = _free(he->p.ptr);
176  }
177  if (vp) {
178  he->tag = RPMTAG_VERSION;
179  if (headerGet(h, he, 0)
180  && he->t == RPM_STRING_TYPE && he->c == 1)
181  *vp = xstrdup(he->p.str);
182  else
183  *vp = NULL;
184  he->p.ptr = _free(he->p.ptr);
185  }
186  if (rp) {
187  he->tag = RPMTAG_RELEASE;
188  if (headerGet(h, he, 0)
189  && he->t == RPM_STRING_TYPE && he->c == 1)
190  *rp = xstrdup(he->p.str);
191  else
192  *rp = NULL;
193  he->p.ptr = _free(he->p.ptr);
194  }
195  if (ap) {
196 #if !defined(RPM_VENDOR_OPENPKG) /* no-architecture-expose */
197  /* do not expose the architecture as this is too less
198  information, as in OpenPKG the "platform" is described by the
199  architecture+operating-system combination. But as the whole
200  "platform" information is actually overkill, just revert to the
201  RPM 4 behaviour and do not expose any such information at all. */
202  he->tag = RPMTAG_ARCH;
203 /*@-observertrans -readonlytrans@*/
204  if (!headerIsEntry(h, he->tag))
205  *ap = xstrdup("pubkey");
206  else
208  *ap = xstrdup("src");
209 /*@=observertrans =readonlytrans@*/
210  else
211  if (headerGet(h, he, 0)
212  && he->t == RPM_STRING_TYPE && he->c == 1)
213  *ap = xstrdup(he->p.str);
214  else
215 #endif
216  *ap = NULL;
217  he->p.ptr = _free(he->p.ptr);
218  }
219 /*@=onlytrans@*/
220  return 0;
221 }
222 
224 {
225  HE_t he = (HE_t) memset(alloca(sizeof(*he)), 0, sizeof(*he));
226  rpmuint32_t hcolor = 0;
227  int xx;
228 
229  he->tag = RPMTAG_FILECOLORS;
230  xx = headerGet(h, he, 0);
231  if (xx && he->p.ptr != NULL && he->c > 0) {
232  unsigned i;
233  for (i = 0; i < (unsigned) he->c; i++)
234  hcolor |= he->p.ui32p[i];
235  }
236  he->p.ptr = _free(he->p.ptr);
237  hcolor &= 0x0f;
238 
239  return hcolor;
240 }
241 
243 {
244  HE_t he = (HE_t) memset(alloca(sizeof(*he)), 0, sizeof(*he));
245  HeaderIterator hi;
246  int xx;
247 
248  if (h == NULL || sigh == NULL)
249  return;
250 
251  for (hi = headerInit(sigh);
252  headerNext(hi, he, 0);
253  he->p.ptr = _free(he->p.ptr))
254  {
255  /* XXX Translate legacy signature tag values. */
256  switch ((rpmSigTag)he->tag) {
257  case RPMSIGTAG_SIZE:
258  he->tag = RPMTAG_SIGSIZE;
259  /*@switchbreak@*/ break;
260  case RPMSIGTAG_MD5:
261  he->tag = RPMTAG_SIGMD5;
262  /*@switchbreak@*/ break;
264  he->tag = RPMTAG_ARCHIVESIZE;
265  /*@switchbreak@*/ break;
266  case RPMSIGTAG_SHA1:
267  case RPMSIGTAG_RSA:
268  case RPMSIGTAG_DSA:
269  case RPMSIGTAG_ECDSA:
270  default:
271  /* Skip all unknown tags that are not in the signature tag range. */
272  if (!(he->tag >= HEADER_SIGBASE && he->tag < HEADER_TAGBASE))
273  continue;
274  /*@switchbreak@*/ break;
275  }
276 assert(he->p.ptr != NULL);
277  if (!headerIsEntry(h, he->tag)) {
278  if (hdrchkType(he->t))
279  continue;
280  if (hdrchkData(he->c))
281  continue;
282  switch(he->t) {
283  default:
284 assert(0); /* XXX keep gcc quiet */
285  /*@switchbreak@*/ break;
286  case RPM_UINT8_TYPE:
287  case RPM_UINT16_TYPE:
288  case RPM_UINT32_TYPE:
289  case RPM_UINT64_TYPE:
290  if (he->c != 1)
291  continue;
292  /*@switchbreak@*/ break;
293  case RPM_STRING_TYPE:
294  case RPM_BIN_TYPE:
295  if (he->c >= 16*1024)
296  continue;
297  /*@switchbreak@*/ break;
298  case RPM_I18NSTRING_TYPE:
299 #if !defined(SUPPORT_I18NSTRING_TYPE)
300 assert(0);
301 #endif
303  continue;
304  /*@notreached@*/ /*@switchbreak@*/ break;
305  }
306  xx = headerPut(h, he, 0);
307 assert(xx == 1);
308  }
309  }
310  hi = headerFini(hi);
311 }
312 
313 Header headerRegenSigHeader(const Header h, int noArchiveSize)
314 {
315  HE_t he = (HE_t) memset(alloca(sizeof(*he)), 0, sizeof(*he));
316  Header sigh = headerNew();
317  HeaderIterator hi;
318  int xx;
319 
320  for (hi = headerInit(h);
321  headerNext(hi, he, 0);
322  he->p.ptr = _free(he->p.ptr))
323  {
324  /* XXX Translate legacy signature tag values. */
325  switch (he->tag) {
326  case RPMTAG_SIGSIZE:
327  he->tag = (rpmTag) RPMSIGTAG_SIZE;
328  /*@switchbreak@*/ break;
329  case RPMTAG_SIGMD5:
330  he->tag = (rpmTag) RPMSIGTAG_MD5;
331  /*@switchbreak@*/ break;
332  case RPMTAG_ARCHIVESIZE:
333  /* XXX rpm-4.1 and later has archive size in signature header. */
334  if (noArchiveSize)
335  continue;
337  /*@switchbreak@*/ break;
338  case RPMTAG_SHA1HEADER:
339  case RPMTAG_RSAHEADER:
340  case RPMTAG_DSAHEADER:
341  case RPMTAG_ECDSAHEADER:
342  default:
343  /* Skip all unknown tags that are not in the signature tag range. */
344  if (!(he->tag >= HEADER_SIGBASE && he->tag < HEADER_TAGBASE))
345  continue;
346  /*@switchbreak@*/ break;
347  }
348 assert(he->p.ptr != NULL);
349  if (!headerIsEntry(sigh, he->tag)) {
350  xx = headerPut(sigh, he, 0);
351 assert(xx == 1);
352  }
353  }
354  hi = headerFini(hi);
355  return sigh;
356 }
int headerMacrosUnload(Header h)
Define per-header macros.
Definition: hdrNVR.c:111
rpmTagType t
Definition: rpmtag.h:504
const char * str
Definition: rpmtag.h:73
rpmTag tag
Definition: rpmtag.h:503
int headerIsEntry(Header h, rpmTag tag)
Check if tag is in header.
Definition: header.c:1439
void headerMergeLegacySigs(Header h, const Header sigh)
Translate and merge legacy signature tags into header.
Definition: hdrNVR.c:242
enum rpmSigTag_e rpmSigTag
Definition: rpmtag.h:474
char * xstrdup(const char *str)
Definition: rpmmalloc.c:321
rpmuint32_t * ui32p
Definition: rpmtag.h:70
rpmuint32_t hGetColor(Header h)
Return header color.
Definition: hdrNVR.c:223
int headerGet(Header h, HE_t he, unsigned int flags)
Retrieve extension or tag value from a header.
Definition: header.c:2231
int headerPut(Header h, HE_t he, unsigned int flags)
Add or append tag container to header.
Definition: header.c:2294
The Header data structure.
HeaderIterator headerFini(HeaderIterator hi)
Destroy header tag container iterator.
Definition: header.c:2134
rpmuint16_t * ui16p
Definition: rpmtag.h:69
HeaderIterator headerInit(Header h)
Create header tag iterator.
Definition: header.c:2144
#define SUPPORT_I18NSTRING_TYPE
Eliminate RPM_I18NSTRING_TYPE.
Definition: system.h:775
void addMacro(MacroContext mc, const char *n, const char *o, const char *b, int level)
Add macro to context.
Definition: macro.c:2786
Header tag iterator data structure.
Definition: header.c:2129
int headerNext(HeaderIterator hi, HE_t he, unsigned int flags)
Return next tag from header.
Definition: header.c:2158
#define HEADER_TAGBASE
Definition: rpmtag.h:122
char * alloca()
Macros to be defined from per-header tag values.
Definition: hdrNVR.c:20
unsigned int rpmuint32_t
Definition: rpmiotypes.h:28
struct _HE_s * HE_t
Definition: rpmtag.h:59
void delMacro(MacroContext mc, const char *n)
Delete macro from context.
Definition: macro.c:2825
void * ptr
Definition: rpmtag.h:67
rpmTagData p
Definition: rpmtag.h:506
unsigned long long rpmuint64_t
Definition: rpmiotypes.h:29
int headerMacrosLoad(Header h)
Define per-header macros.
Definition: hdrNVR.c:36
rpmTagCount c
Definition: rpmtag.h:507
char * rpmExpand(const char *arg,...)
Return (malloc'ed) concatenated macro expansion(s).
Definition: macro.c:3252
Header headerRegenSigHeader(const Header h, int noArchiveSize)
Regenerate signature header.
Definition: hdrNVR.c:313
rpmuint8_t * ui8p
Definition: rpmtag.h:68
#define HEADER_SIGBASE
Definition: rpmtag.h:121
Definition: rpmtag.h:502
const char const int i
Definition: bson.h:778
rpmTag tag
Definition: hdrNVR.c:23
static void * _free(const void *p)
Wrapper to free(3), hides const compilation noise, permit NULL, return NULL.
Definition: rpmiotypes.h:756
int headerNEVRA(Header h, const char **np, const char **ep, const char **vp, const char **rp, const char **ap)
Return name, epoch, version, release, arch strings from header.
Definition: hdrNVR.c:162
static struct tagMacro tagMacros[]
#define hdrchkData(_nbytes)
Sanity check on data size and/or offset and/or count.
enum rpmTag_e rpmTag
Definition: rpmtag.h:470
Header headerNew(void)
Create new (empty) header instance.
Definition: header.c:180
const char * macroname
Definition: hdrNVR.c:22
#define hdrchkType(_type)
Sanity check on type values.
rpmuint64_t * ui64p
Definition: rpmtag.h:71