Sanitize python object -> tag number exception handling
[platform/upstream/rpm.git] / lib / header_internal.c
1 /** \ingroup header
2  * \file lib/header_internal.c
3  */
4
5 #include "system.h"
6
7 #include <rpm/rpmtypes.h>
8 #include "lib/header_internal.h"
9
10 #include "debug.h"
11
12 uint64_t htonll( uint64_t n ) {
13     uint32_t *i = (uint32_t*)&n;
14     uint32_t b = i[0];
15     i[0] = htonl(i[1]);
16     i[1] = htonl(b);
17     return n;
18 }
19
20 /*
21  * Backwards compatibility wrappers for legacy interfaces.
22  * Remove these some day...
23  */
24 #define _RPM_4_4_COMPAT
25 #include <rpm/rpmlegacy.h>
26
27 /* dumb macro to avoid 50 copies of this code while converting... */
28 #define TDWRAP() \
29     if (type) \
30         *type = td.type; \
31     if (p) \
32         *p = td.data; \
33     else \
34         rpmtdFreeData(&td); \
35     if (c) \
36         *c = td.count
37
38 int headerRemoveEntry(Header h, rpmTag tag)
39 {
40     return headerDel(h, tag);
41 }
42
43 static void *_headerFreeData(rpm_data_t data, rpmTagType type)
44 {
45     if (data) {
46         if (type == RPM_FORCEFREE_TYPE ||
47             type == RPM_STRING_ARRAY_TYPE ||
48             type == RPM_I18NSTRING_TYPE ||
49             type == RPM_BIN_TYPE)
50                 free(data);
51     }
52     return NULL;
53 }
54
55 void * headerFreeData(rpm_data_t data, rpmTagType type)
56 {
57     return _headerFreeData(data, type);
58 }
59
60 void * headerFreeTag(Header h, rpm_data_t data, rpmTagType type)
61 {
62     return _headerFreeData(data, type);
63 }
64
65 static int headerGetWrap(Header h, rpmTag tag,
66                 rpmTagType * type,
67                 rpm_data_t * p,
68                 rpm_count_t * c,
69                 headerGetFlags flags)
70 {
71     struct rpmtd_s td;
72     int rc;
73
74     rc = headerGet(h, tag, &td, flags);
75     TDWRAP();
76     return rc;
77 }
78
79 int headerGetEntry(Header h, rpmTag tag,
80                         rpmTagType * type,
81                         rpm_data_t * p,
82                         rpm_count_t * c)
83 {
84     return headerGetWrap(h, tag, type, p, c, HEADERGET_DEFAULT);
85 }
86
87 int headerGetEntryMinMemory(Header h, rpmTag tag,
88                         rpmTagType * type,
89                         rpm_data_t * p,
90                         rpm_count_t * c)
91 {
92     return headerGetWrap(h, tag, type, (rpm_data_t) p, c, HEADERGET_MINMEM);
93 }
94
95 int headerGetRawEntry(Header h, rpmTag tag, rpmTagType * type, rpm_data_t * p,
96                 rpm_count_t * c)
97 {
98     if (p == NULL) 
99         return headerIsEntry(h, tag);
100
101     return headerGetWrap(h, tag, type, p, c, HEADERGET_RAW);
102 }
103
104 int headerNextIterator(HeaderIterator hi,
105                 rpmTag * tag,
106                 rpmTagType * type,
107                 rpm_data_t * p,
108                 rpm_count_t * c)
109 {
110     struct rpmtd_s td;
111     int rc;
112
113     rc = headerNext(hi, &td);
114     if (tag)
115         *tag = td.tag;
116     TDWRAP();
117     return rc;
118 }
119
120 int headerModifyEntry(Header h, rpmTag tag, rpmTagType type,
121                         rpm_constdata_t p, rpm_count_t c)
122 {
123     struct rpmtd_s td = {
124         .tag = tag,
125         .type = type,
126         .data = (void *) p,
127         .count = c,
128     };
129     return headerMod(h, &td);
130 }
131
132 static int headerPutWrap(Header h, rpmTag tag, rpmTagType type,
133                 rpm_constdata_t p, rpm_count_t c, headerPutFlags flags)
134 {
135     struct rpmtd_s td = {
136         .tag = tag,
137         .type = type,
138         .data = (void *) p,
139         .count = c,
140     };
141     return headerPut(h, &td, flags);
142 }
143
144 int headerAddOrAppendEntry(Header h, rpmTag tag, rpmTagType type,
145                 rpm_constdata_t p, rpm_count_t c)
146 {
147     return headerPutWrap(h, tag, type, p, c, HEADERPUT_APPEND);
148 }
149
150 int headerAppendEntry(Header h, rpmTag tag, rpmTagType type,
151                 rpm_constdata_t p, rpm_count_t c)
152 {
153     return headerPutWrap(h, tag, type, p, c, HEADERPUT_APPEND);
154 }
155
156 int headerAddEntry(Header h, rpmTag tag, rpmTagType type,
157                 rpm_constdata_t p, rpm_count_t c)
158 {
159     return headerPutWrap(h, tag, type, p, c, HEADERPUT_DEFAULT);
160 }
161 #undef _RPM_4_4_COMPAT