Debian Codegraph @

src/common/xml_iterate.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 */
20 #ifdef HAVE_CONFIG_H
21 #include <config.h>
22 #endif
23
24 #include <string.h>
25 #include <libxml/parser.h>
26 #include "debug_priv.h"
27 #include "xml_iterate.h"
28 #include "oscap_helpers.h"
29
30
31 static int xml_element_dfs_callback(xmlNode **node, xml_iterate_callback user_fn, void *user_data)
32 {
33 int result = user_fn(node, user_data);
34 if (result == 1)
35 return result;
36 xmlNode *child = (*node)->children;
37 while (child != NULL) {
38 int res = xml_element_dfs_callback(&child, user_fn, user_data);
39 if (res == 1)
40 return res;
41 if (result == 0)
42 result = res;
43 child = child->next;
44 }
45 return result;
46 }
47
48 int xml_iterate_dfs(const char *input_text, char **output_text, xml_iterate_callback user_fn, void *user_data)
49 {
50 char *input_document = NULL;
51 xmlDocPtr doc = NULL;
52 xmlNode *root = NULL;
53 int res = 0;
54
55 if (user_fn == NULL)
56 return 1;
57
58 input_document = oscap_sprintf("<x xmlns='http://www.w3.org/1999/xhtml'>%s</x>", input_text);
59 if ((doc = xmlParseMemory(input_document, strlen(input_document))) == NULL) {
60 dW("Could not xmlParseMemory: '%s'", input_document);
61 free(input_document);
62 return 1;
63 }
64 if ((root = xmlDocGetRootElement(doc)) == NULL) {
65 dW("Could not xmlDocGetRootElement: '%s'", input_document);
66 xmlFreeDoc(doc);
67 free(input_document);
68 return 1;
69 }
70 free(input_document);
71
72 res = xml_element_dfs_callback(&root, user_fn, user_data);
73 if (output_text != NULL) {
74 // We cannot simply xmlDumpMemory, because we need to skip the upper <x/> element.
75 if ((root = xmlDocGetRootElement(doc)) == NULL) {
76 dW("Could not get xmlDocGetRootElement of result document.");
77 xmlFreeDoc(doc);
78 return 1;
79 }
80 xmlBuffer *buff = xmlBufferCreate();
81 xmlNode *child = root->children;
82 while (child != NULL) {
83 int size = xmlNodeDump(buff, doc, child, 0, 0);
84 if (size < 0) {
85 dE("xmlNodeDump failed!");
86 }
87 else if (size == 0) {
88 dD("xmlNodeDump returned zero.");
89 }
90
91 child = child->next;
92 }
93 /* Sadly, we cannot use xmlBufferDetach since we want to support older libxml. */
94 *output_text = oscap_strdup((const char *) xmlBufferContent(buff));
95 xmlBufferFree(buff);
96 }
97 xmlFreeDoc(doc);
98 return res;
99 }