Debian Codegraph @

src/CPE/cpedict_ext_priv.c

raw symbols SCIP
1 /*
2 * Copyright 2013 Red Hat Inc., Durham, North Carolina.
3 * All Rights Reserved.
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 *
19 * Authors:
20 * Šimon Lukašík
21 */
22
23 #ifdef HAVE_CONFIG_H
24 #include <config.h>
25 #endif
26
27 #include <libxml/xmlreader.h>
28 #include <libxml/xmlwriter.h>
29
30 #include "public/cpe_name.h"
31 #include "cpe_ctx_priv.h"
32 #include "cpedict_ext_priv.h"
33
34 #include "common/_error.h"
35 #include "common/list.h"
36 #include "common/util.h"
37 #include "common/xmlns_priv.h"
38 #include "common/xmltext_priv.h"
39
40 #define ATTR_NAME_STR "name"
41 #define ATTR_DATE_STR "date"
42 #define ATTR_TYPE_STR "type"
43 #define TAG_CPE_EXT_DEPRECATION_STR "deprecation"
44 #define TAG_CPE_EXT_DEPRECATEDBY_STR "deprecated-by"
45
46 enum cpe_deprecation_type { ///< cpe_dict_ext:deprecationTypeType
47 CPE_DEP_NAME_CORRECTION = 1,
48 CPE_DEP_NAME_REMOVAL = 2,
49 CPE_DEP_ADDITIONAL_INFORMATION = 3,
50 };
51
52 struct cpe_ext_deprecatedby { ///< <cpe_dict_ext:deprecated-by> node
53 char *name; ///< @name attribute
54 enum cpe_deprecation_type type; ///< @type attribute
55 };
56
57 struct cpe_ext_deprecation { ///< <cpe_dict_ext:deprecation> node
58 char *date; ///< @date attribute
59 struct oscap_list *deprecatedbys; ///< <deprecated-by> sub-nodes
60 };
61
62 struct cpe23_item { ///< <cpe23-item> node
63 char *name; ///< @name attribute
64 struct oscap_list *deprecations; ///< <deprecation> sub-nodes
65 };
66
67 static const struct oscap_string_map CPE_EXT_DEPRECATION_MAP[] = {
68 {CPE_DEP_NAME_CORRECTION, "NAME_CORRECTION"},
69 {CPE_DEP_NAME_REMOVAL, "NAME_REMOVAL"},
70 {CPE_DEP_ADDITIONAL_INFORMATION, "ADDITIONAL_INFORMATION"},
71 {0, NULL}
72 };
73
74 struct cpe_ext_deprecatedby_iterator;
75 static bool cpe_ext_deprecatedby_iterator_has_more(struct cpe_ext_deprecatedby_iterator *it);
76 static struct cpe_ext_deprecatedby *cpe_ext_deprecatedby_iterator_next(struct cpe_ext_deprecatedby_iterator *it);
77 static void cpe_ext_deprecatedby_iterator_free(struct cpe_ext_deprecatedby_iterator *it);
78 void cpe_ext_deprecatedby_iterator_reset(struct cpe_ext_deprecatedby_iterator *it);
79 static void cpe_ext_deprecatedby_free(struct cpe_ext_deprecatedby *deprecatedby);
80 struct cpe_ext_deprecatedby_iterator *cpe_ext_deprecation_get_deprecatedbys(const struct cpe_ext_deprecation *deprecation);
81 bool cpe_ext_deprecation_add_deprecatedby(struct cpe_ext_deprecation *deprecation, struct cpe_ext_deprecatedby *deprecatedby);
82
83 struct cpe_ext_deprecation_iterator;
84 static bool cpe_ext_deprecation_iterator_has_more(struct cpe_ext_deprecation_iterator *it);
85 static struct cpe_ext_deprecation *cpe_ext_deprecation_iterator_next(struct cpe_ext_deprecation_iterator *it);
86 static void cpe_ext_deprecation_iterator_free(struct cpe_ext_deprecation_iterator *it);
87 void cpe_ext_deprecation_iterator_reset(struct cpe_ext_deprecation_iterator *it);
88 static void cpe_ext_deprecation_free(struct cpe_ext_deprecation *deprecation);
89 struct cpe_ext_deprecation_iterator *cpe23_item_get_deprecations(const struct cpe23_item *item);
90 bool cpe23_item_add_deprecation(struct cpe23_item *item, struct cpe_ext_deprecation *deprecation);
91
92 static struct cpe_ext_deprecatedby *cpe_ext_deprecatedby_new()
93 {
94 return calloc(1, sizeof(struct cpe_ext_deprecatedby));
95 }
96
97 static struct cpe_ext_deprecation *cpe_ext_deprecation_new()
98 {
99 struct cpe_ext_deprecation *deprecation = calloc(1, sizeof(struct cpe_ext_deprecation));
100 deprecation->deprecatedbys = oscap_list_new();
101 return deprecation;
102 }
103
104 static struct cpe23_item *cpe23_item_new()
105 {
106 struct cpe23_item *item = calloc(1, sizeof(struct cpe23_item));
107 item->deprecations = oscap_list_new();
108 return item;
109 }
110
111 static struct cpe_ext_deprecatedby *cpe_ext_deprecatedby_parse(xmlTextReaderPtr reader)
112 {
113 __attribute__nonnull__(reader);
114
115 if (xmlStrcmp(xmlTextReaderConstLocalName(reader), BAD_CAST TAG_CPE_EXT_DEPRECATEDBY_STR) != 0 ||
116 xmlTextReaderNodeType(reader) != 1) {
117 oscap_seterr(OSCAP_EFAMILY_OSCAP, "Found '%s' node when expecting: '%s'!",
118 xmlTextReaderConstLocalName(reader), TAG_CPE_EXT_DEPRECATEDBY_STR);
119 }
120 const xmlChar* nsuri = xmlTextReaderConstNamespaceUri(reader);
121 if (nsuri && xmlStrcmp(nsuri, BAD_CAST XMLNS_CPE2D3_EXTENSION) != 0) {
122 oscap_seterr(OSCAP_EFAMILY_OSCAP, "Found '%s' namespace when expecting: '%s'!",
123 nsuri, XMLNS_CPE2D3_EXTENSION);
124 return NULL;
125 }
126
127 struct cpe_ext_deprecatedby *deprecatedby = cpe_ext_deprecatedby_new();
128 deprecatedby->name = (char *) xmlTextReaderGetAttribute(reader, BAD_CAST ATTR_NAME_STR);
129 char *type = (char *) xmlTextReaderGetAttribute(reader, BAD_CAST ATTR_TYPE_STR);
130 if (type == NULL) {
131 oscap_seterr(OSCAP_EFAMILY_OSCAP, "Compulsory attribute '%s' missing at '%s' element.",
132 ATTR_TYPE_STR, TAG_CPE_EXT_DEPRECATEDBY_STR);
133 cpe_ext_deprecatedby_free(deprecatedby);
134 return NULL;
135 }
136 deprecatedby->type = oscap_string_to_enum(CPE_EXT_DEPRECATION_MAP, type);
137 free(type);
138 return deprecatedby;
139 }
140
141 static int cpe_ext_deprecatedby_export(const struct cpe_ext_deprecatedby *deprecatedby, xmlTextWriterPtr writer)
142 {
143 __attribute__nonnull__(writer);
144 __attribute__nonnull__(deprecatedby);
145
146 xmlTextWriterStartElementNS(writer, NULL, BAD_CAST TAG_CPE_EXT_DEPRECATEDBY_STR,
147 BAD_CAST XMLNS_CPE2D3_EXTENSION);
148
149 if (deprecatedby->name != NULL) {
150 xmlTextWriterWriteAttribute(writer, BAD_CAST ATTR_NAME_STR, BAD_CAST deprecatedby->name);
151 }
152 if (deprecatedby->type != 0) {
153 xmlTextWriterWriteAttribute(writer, BAD_CAST ATTR_TYPE_STR,
154 BAD_CAST oscap_enum_to_string(CPE_EXT_DEPRECATION_MAP, deprecatedby->type));
155 }
156 xmlTextWriterEndElement(writer);
157 if (xmlGetLastError() != NULL)
158 oscap_setxmlerr(xmlGetLastError());
159 return 0;
160 }
161
162 static struct cpe_ext_deprecation *cpe_ext_deprecation_parse(xmlTextReaderPtr reader)
163 {
164 __attribute__nonnull__(reader);
165
166 if (xmlStrcmp(xmlTextReaderConstLocalName(reader), BAD_CAST TAG_CPE_EXT_DEPRECATION_STR) != 0 ||
167 xmlTextReaderNodeType(reader) != 1) {
168 oscap_seterr(OSCAP_EFAMILY_OSCAP, "Found '%s' node when expecting: '%s'!",
169 xmlTextReaderConstLocalName(reader), TAG_CPE_EXT_DEPRECATION_STR);
170 }
171 const xmlChar* nsuri = xmlTextReaderConstNamespaceUri(reader);
172 if (nsuri && xmlStrcmp(nsuri, BAD_CAST XMLNS_CPE2D3_EXTENSION) != 0) {
173 oscap_seterr(OSCAP_EFAMILY_OSCAP, "Found '%s' namespace when expecting: '%s'!",
174 nsuri, XMLNS_CPE2D3_EXTENSION);
175 return NULL;
176 }
177
178 struct cpe_ext_deprecation *deprecation = cpe_ext_deprecation_new();
179 deprecation->date = (char *) xmlTextReaderGetAttribute(reader, BAD_CAST ATTR_DATE_STR);
180 if (xmlTextReaderIsEmptyElement(reader) == 0) { // the element contains child nodes
181 xmlTextReaderNextNode(reader);
182 while (xmlStrcmp(xmlTextReaderConstLocalName(reader), BAD_CAST TAG_CPE_EXT_DEPRECATION_STR) != 0) {
183 if (xmlTextReaderNodeType(reader) != XML_READER_TYPE_ELEMENT) {
184 xmlTextReaderNextNode(reader);
185 continue;
186 }
187
188 if (xmlStrcmp(xmlTextReaderConstLocalName(reader), BAD_CAST TAG_CPE_EXT_DEPRECATEDBY_STR) == 0) {
189 struct cpe_ext_deprecatedby *deprecatedby = cpe_ext_deprecatedby_parse(reader);
190 if (deprecatedby == NULL) {
191 cpe_ext_deprecation_free(deprecation);
192 return NULL;
193 }
194 oscap_list_add(deprecation->deprecatedbys, deprecatedby);
195 }
196 else {
197 oscap_seterr(OSCAP_EFAMILY_OSCAP, "Unexpected element within deprecation element: '%s'",
198 xmlTextReaderConstLocalName(reader));
199 cpe_ext_deprecation_free(deprecation);
200 return NULL;
201 }
202 xmlTextReaderNextNode(reader);
203 }
204 }
205 return deprecation;
206 }
207
208 static int cpe_ext_deprecation_export(const struct cpe_ext_deprecation *deprecation, xmlTextWriterPtr writer)
209 {
210 __attribute__nonnull__(writer);
211 __attribute__nonnull__(deprecation);
212
213 xmlTextWriterStartElementNS(writer, NULL, BAD_CAST TAG_CPE_EXT_DEPRECATION_STR,
214 BAD_CAST XMLNS_CPE2D3_EXTENSION);
215
216 if (deprecation->date != NULL) {
217 xmlTextWriterWriteAttribute(writer, BAD_CAST ATTR_DATE_STR, BAD_CAST deprecation->date);
218 }
219 OSCAP_FOREACH(cpe_ext_deprecatedby, d, oscap_iterator_new(deprecation->deprecatedbys),
220 cpe_ext_deprecatedby_export(d, writer););
221 xmlTextWriterEndElement(writer);
222 if (xmlGetLastError() != NULL)
223 oscap_setxmlerr(xmlGetLastError());
224 return 0;
225 }
226
227 struct cpe23_item *cpe23_item_parse(xmlTextReaderPtr reader)
228 {
229 __attribute__nonnull__(reader);
230
231 if (xmlStrcmp(xmlTextReaderConstLocalName(reader), BAD_CAST TAG_CPE23_ITEM_STR) != 0 ||
232 xmlTextReaderNodeType(reader) != 1) {
233 oscap_seterr(OSCAP_EFAMILY_OSCAP, "Found '%s' node when expecting: '%s'!",
234 xmlTextReaderConstLocalName(reader), TAG_CPE23_ITEM_STR);
235 return NULL;
236 }
237 const xmlChar* nsuri = xmlTextReaderConstNamespaceUri(reader);
238 if (nsuri && xmlStrcmp(nsuri, BAD_CAST XMLNS_CPE2D3_EXTENSION) != 0) {
239 oscap_seterr(OSCAP_EFAMILY_OSCAP, "Found '%s' namespace when expecting: '%s'!",
240 nsuri, XMLNS_CPE2D3_EXTENSION);
241 return NULL;
242 }
243
244 struct cpe23_item *item = cpe23_item_new();
245 item->name = (char *) xmlTextReaderGetAttribute(reader, BAD_CAST ATTR_NAME_STR);
246 if (xmlTextReaderIsEmptyElement(reader) == 0) { // the element contains child nodes
247 xmlTextReaderNextNode(reader);
248 while (xmlStrcmp(xmlTextReaderConstLocalName(reader), BAD_CAST TAG_CPE23_ITEM_STR) != 0) {
249 if (xmlTextReaderNodeType(reader) != XML_READER_TYPE_ELEMENT) {
250 xmlTextReaderNextNode(reader);
251 continue;
252 }
253
254 if (xmlStrcmp(xmlTextReaderConstLocalName(reader), BAD_CAST TAG_CPE_EXT_DEPRECATION_STR) == 0) {
255 struct cpe_ext_deprecation *deprecation = cpe_ext_deprecation_parse(reader);
256 if (deprecation == NULL) {
257 cpe23_item_free(item);
258 return NULL;
259 }
260 oscap_list_add(item->deprecations, deprecation);
261 }
262 else {
263 oscap_seterr(OSCAP_EFAMILY_OSCAP, "Unexpected element within cpe23-item[@name='%s']: '%s'",
264 item->name, xmlTextReaderConstLocalName(reader));
265 cpe23_item_free(item);
266 return NULL;
267 }
268 xmlTextReaderNextNode(reader);
269 }
270 }
271
272 return item;
273 }
274
275 int cpe23_item_export(const struct cpe23_item *item, xmlTextWriterPtr writer)
276 {
277 __attribute__nonnull__(writer);
278 __attribute__nonnull__(item);
279
280 xmlTextWriterStartElementNS(writer, NULL, BAD_CAST TAG_CPE23_ITEM_STR,
281 BAD_CAST XMLNS_CPE2D3_EXTENSION);
282
283 if (item->name != NULL) {
284 xmlTextWriterWriteAttribute(writer, BAD_CAST ATTR_NAME_STR, BAD_CAST item->name);
285 }
286 OSCAP_FOREACH(cpe_ext_deprecation, d, oscap_iterator_new(item->deprecations),
287 cpe_ext_deprecation_export(d, writer););
288 xmlTextWriterEndElement(writer);
289 if (xmlGetLastError() != NULL)
290 oscap_setxmlerr(xmlGetLastError());
291 return 0;
292 }
293
294 static void cpe_ext_deprecatedby_free(struct cpe_ext_deprecatedby *deprecatedby)
295 {
296 free(deprecatedby->name);
297 free(deprecatedby);
298 }
299
300 static void cpe_ext_deprecation_free(struct cpe_ext_deprecation *deprecation)
301 {
302 if (deprecation != NULL) {
303 free(deprecation->date);
304 oscap_list_free(deprecation->deprecatedbys, (oscap_destruct_func) cpe_ext_deprecatedby_free);
305 free(deprecation);
306 }
307 }
308
309 void cpe23_item_free(struct cpe23_item *item)
310 {
311 if (item != NULL) {
312 oscap_list_free(item->deprecations, (oscap_destruct_func) cpe_ext_deprecation_free);
313 free(item->name);
314 free(item);
315 }
316 }
317
318 OSCAP_IGETINS_GEN(cpe_ext_deprecatedby, cpe_ext_deprecation, deprecatedbys, deprecatedby);
319 OSCAP_IGETINS_GEN(cpe_ext_deprecation, cpe23_item, deprecations, deprecation);