4 #include <rpm/rpmstring.h>
5 #include <rpm/rpmpgp.h>
9 typedef char * (*headerTagFormatFunction) (rpmtd td, char *formatPrefix);
11 extern void *rpmHeaderFormatFuncByValue(rpmtdFormats fmt);
15 rpmtd td = xmalloc(sizeof(*td));
20 rpmtd rpmtdFree(rpmtd td)
22 /* permit free on NULL td */
24 /* XXX should we free data too - a flag maybe? */
30 void rpmtdReset(rpmtd td)
34 memset(td, 0, sizeof(*td));
38 void rpmtdFreeData(rpmtd td)
42 if (td->flags & RPMTD_ALLOCED) {
43 if (td->flags & RPMTD_PTR_ALLOCED) {
44 assert(td->data != NULL);
45 char **data = td->data;
46 for (int i = 0; i < td->count; i++) {
55 rpm_count_t rpmtdCount(rpmtd td)
58 /* fix up for binary type abusing count as data length */
59 return (td->type == RPM_BIN_TYPE) ? 1 : td->count;
62 rpmTag rpmtdTag(rpmtd td)
68 rpmTagType rpmtdType(rpmtd td)
74 int rpmtdInit(rpmtd td)
78 /* XXX check that this is an array type? */
83 int rpmtdNext(rpmtd td)
90 if (td->ix < rpmtdCount(td)) {
99 char * rpmtdGetChar(rpmtd td)
105 if (td->type == RPM_CHAR_TYPE) {
106 int ix = (td->ix >= 0 ? td->ix : 0);
107 res = (char *) td->data + ix;
111 uint16_t * rpmtdGetUint16(rpmtd td)
113 uint16_t *res = NULL;
117 if (td->type == RPM_INT16_TYPE) {
118 int ix = (td->ix >= 0 ? td->ix : 0);
119 res = (uint16_t *) td->data + ix;
124 uint32_t * rpmtdGetUint32(rpmtd td)
126 uint32_t *res = NULL;
130 if (td->type == RPM_INT32_TYPE) {
131 int ix = (td->ix >= 0 ? td->ix : 0);
132 res = (uint32_t *) td->data + ix;
136 const char * rpmtdGetString(rpmtd td)
138 const char *str = NULL;
142 if (td->type == RPM_STRING_TYPE) {
143 str = (const char *) td->data;
144 } else if (td->type == RPM_STRING_ARRAY_TYPE ||
145 td->type == RPM_I18NSTRING_TYPE) {
146 /* XXX TODO: check for array bounds */
147 int ix = (td->ix >= 0 ? td->ix : 0);
148 str = *((const char**) td->data + ix);
153 char *rpmtdFormat(rpmtd td, rpmtdFormats fmt, const char *errmsg)
155 headerTagFormatFunction func = rpmHeaderFormatFuncByValue(fmt);
156 const char *err = NULL;
160 char fmtbuf[50]; /* yuck, get rid of this */
162 str = func(td, fmtbuf);
164 err = _("Unknown format");
174 int rpmtdFromArgv(rpmtd td, rpmTag tag, ARGV_t argv)
176 int count = argvCount(argv);
177 rpmTagType type = rpmTagGetType(tag) & RPM_MASK_TYPE;
179 if (type != RPM_STRING_ARRAY_TYPE || count < 1)
191 int rpmtdFromArgi(rpmtd td, rpmTag tag, ARGI_t argi)
193 int count = argiCount(argi);
194 rpmTagType type = rpmTagGetType(tag) & RPM_MASK_TYPE;
195 rpmTagReturnType retype = rpmTagGetType(tag) & RPM_MASK_RETURN_TYPE;
197 if (type != RPM_INT32_TYPE || retype != RPM_ARRAY_RETURN_TYPE || count < 1)
205 td->data = argiData(argi);
209 rpmtd rpmtdDup(rpmtd td)
216 /* TODO: permit other types too */
217 if (td->type != RPM_STRING_ARRAY_TYPE && td->type != RPM_I18NSTRING_TYPE) {
221 /* deep-copy container and data, drop immutable flag */
223 memcpy(newtd, td, sizeof(*td));
224 newtd->flags &= ~(RPMTD_IMMUTABLE);
226 newtd->flags |= (RPMTD_ALLOCED | RPMTD_PTR_ALLOCED);
227 newtd->data = data = xmalloc(td->count * sizeof(*data));
228 while ((i = rpmtdNext(td)) >= 0) {
229 data[i] = xstrdup(rpmtdGetString(td));