1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
|
/**
* \file rpmdb/tagname.c
*/
#include "system.h"
#include <rpmlib.h>
#include <rpmstring.h>
#include "debug.h"
struct headerTagIndices_s {
int (*loadIndex) (headerTagTableEntry ** ipp, int * np,
int (*cmp) (const void * avp, const void * bvp));
/*!< load sorted tag index. */
headerTagTableEntry * byName; /*!< header tags sorted by name. */
int byNameSize; /*!< no. of entries. */
int (*byNameCmp) (const void * avp, const void * bvp); /*!< compare entries by name. */
int (*tagValue) (const char * name); /* return value from name. */
headerTagTableEntry * byValue; /*!< header tags sorted by value. */
int byValueSize; /*!< no. of entries. */
int (*byValueCmp) (const void * avp, const void * bvp); /*!< compare entries by value. */
const char * (*tagName) (int value); /* Return name from value. */
int (*tagType) (int value); /* Return type from value. */
};
/**
* Compare tag table entries by name.
* @param *avp tag table entry a
* @param *bvp tag table entry b
* @return comparison
*/
static int tagCmpName(const void * avp, const void * bvp)
{
headerTagTableEntry a = *(headerTagTableEntry *) avp;
headerTagTableEntry b = *(headerTagTableEntry *) bvp;
return strcmp(a->name, b->name);
}
/**
* Compare tag table entries by value.
* @param *avp tag table entry a
* @param *bvp tag table entry b
* @return comparison
*/
static int tagCmpValue(const void * avp, const void * bvp)
{
headerTagTableEntry a = *(headerTagTableEntry *) avp;
headerTagTableEntry b = *(headerTagTableEntry *) bvp;
int ret = (a->val - b->val);
/* Make sure that sort is stable, longest name first. */
if (ret == 0)
ret = (strlen(b->name) - strlen(a->name));
return ret;
}
/**
* Load/sort a tag index.
* @retval *ipp tag index
* @retval *np no. of tags
* @param cmp sort compare routine
* @return 0 always
*/
static int tagLoadIndex(headerTagTableEntry ** ipp, int * np,
int (*cmp) (const void * avp, const void * bvp))
{
headerTagTableEntry tte, *ip;
int n = 0;
ip = xcalloc(rpmTagTableSize, sizeof(*ip));
n = 0;
for (tte = (headerTagTableEntry)rpmTagTable; tte->name != NULL; tte++) {
ip[n] = tte;
n++;
}
assert(n == rpmTagTableSize);
if (n > 1)
qsort(ip, n, sizeof(*ip), cmp);
*ipp = ip;
*np = n;
return 0;
}
/* forward refs */
static const char * _tagName(int tag);
static int _tagType(int tag);
static int _tagValue(const char * tagstr);
static struct headerTagIndices_s _rpmTags = {
tagLoadIndex,
NULL, 0, tagCmpName, _tagValue,
NULL, 0, tagCmpValue, _tagName, _tagType,
};
headerTagIndices rpmTags = &_rpmTags;
static const char * _tagName(int tag)
{
static char nameBuf[128]; /* XXX yuk */
const struct headerTagTableEntry_s *t;
int comparison, i, l, u;
int xx;
char *s;
if (_rpmTags.byValue == NULL)
xx = tagLoadIndex(&_rpmTags.byValue, &_rpmTags.byValueSize, tagCmpValue);
switch (tag) {
case RPMDBI_PACKAGES:
strcpy(nameBuf, "Packages");
break;
case RPMDBI_DEPENDS:
strcpy(nameBuf, "Depends");
break;
case RPMDBI_ADDED:
strcpy(nameBuf, "Added");
break;
case RPMDBI_REMOVED:
strcpy(nameBuf, "Removed");
break;
case RPMDBI_AVAILABLE:
strcpy(nameBuf, "Available");
break;
case RPMDBI_HDLIST:
strcpy(nameBuf, "Hdlist");
break;
case RPMDBI_ARGLIST:
strcpy(nameBuf, "Arglist");
break;
case RPMDBI_FTSWALK:
strcpy(nameBuf, "Ftswalk");
break;
/* XXX make sure rpmdb indices are identically named. */
case RPMTAG_CONFLICTS:
strcpy(nameBuf, "Conflictname");
break;
case RPMTAG_HDRID:
strcpy(nameBuf, "Sha1header");
break;
default:
strcpy(nameBuf, "(unknown)");
if (_rpmTags.byValue == NULL)
break;
l = 0;
u = _rpmTags.byValueSize;
while (l < u) {
i = (l + u) / 2;
t = _rpmTags.byValue[i];
comparison = (tag - t->val);
if (comparison < 0)
u = i;
else if (comparison > 0)
l = i + 1;
else {
nameBuf[0] = nameBuf[1] = '\0';
/* Make sure that the bsearch retrieve is stable. */
while (i > 0 && tag == _rpmTags.byValue[i-1]->val) {
i--;
}
t = _rpmTags.byValue[i];
if (t->name != NULL)
strcpy(nameBuf, t->name + (sizeof("RPMTAG_")-1));
for (s = nameBuf+1; *s != '\0'; s++)
*s = xtolower(*s);
break;
}
}
break;
}
return nameBuf;
}
static int _tagType(int tag)
{
const struct headerTagTableEntry_s *t;
int comparison, i, l, u;
int xx;
if (_rpmTags.byValue == NULL)
xx = tagLoadIndex(&_rpmTags.byValue, &_rpmTags.byValueSize, tagCmpValue);
switch (tag) {
case RPMDBI_PACKAGES:
case RPMDBI_DEPENDS:
case RPMDBI_ADDED:
case RPMDBI_REMOVED:
case RPMDBI_AVAILABLE:
case RPMDBI_HDLIST:
case RPMDBI_ARGLIST:
case RPMDBI_FTSWALK:
break;
default:
if (_rpmTags.byValue == NULL)
break;
l = 0;
u = _rpmTags.byValueSize;
while (l < u) {
i = (l + u) / 2;
t = _rpmTags.byValue[i];
comparison = (tag - t->val);
if (comparison < 0)
u = i;
else if (comparison > 0)
l = i + 1;
else {
/* Make sure that the bsearch retrieve is stable. */
while (i > 0 && t->val == _rpmTags.byValue[i-1]->val) {
i--;
}
t = _rpmTags.byValue[i];
return t->type;
}
}
break;
}
return RPM_NULL_TYPE;
}
static int _tagValue(const char * tagstr)
{
const struct headerTagTableEntry_s *t;
int comparison, i, l, u;
int xx;
if (!xstrcasecmp(tagstr, "Packages"))
return RPMDBI_PACKAGES;
if (!xstrcasecmp(tagstr, "Depends"))
return RPMDBI_DEPENDS;
if (!xstrcasecmp(tagstr, "Added"))
return RPMDBI_ADDED;
if (!xstrcasecmp(tagstr, "Removed"))
return RPMDBI_REMOVED;
if (!xstrcasecmp(tagstr, "Available"))
return RPMDBI_AVAILABLE;
if (!xstrcasecmp(tagstr, "Hdlist"))
return RPMDBI_HDLIST;
if (!xstrcasecmp(tagstr, "Arglist"))
return RPMDBI_ARGLIST;
if (!xstrcasecmp(tagstr, "Ftswalk"))
return RPMDBI_FTSWALK;
if (_rpmTags.byName == NULL)
xx = tagLoadIndex(&_rpmTags.byName, &_rpmTags.byNameSize, tagCmpName);
if (_rpmTags.byName == NULL)
return -1;
l = 0;
u = _rpmTags.byNameSize;
while (l < u) {
i = (l + u) / 2;
t = _rpmTags.byName[i];
comparison = xstrcasecmp(tagstr, t->name + (sizeof("RPMTAG_")-1));
if (comparison < 0)
u = i;
else if (comparison > 0)
l = i + 1;
else
return t->val;
}
return -1;
}
const char * rpmTagGetName(int tag)
{
return ((*rpmTags->tagName)(tag));
}
/**
* Return tag data type from value.
* @param tag tag value
* @return tag data type, RPM_NULL_TYPE on not found.
*/
int rpmTagGetType(int tag)
{
return ((*rpmTags->tagType)(tag));
}
/**
* Return tag value from name.
* @param tagstr name of tag
* @return tag value, -1 on not found
*/
int rpmTagGetValue(const char * tagstr)
{
return ((*rpmTags->tagValue)(tagstr));
}
|