Debian Codegraph @

src/xml_C14N.c

raw ◀ ▶
1 Debian patch touch this file
1 /*
2 * $Id: xml_C14N.c,v 1.12 2004/11/24 03:00:10 mgrouch Exp $
3 *
4 * Canonical XML implementation test program
5 * (http://www.w3.org/TR/2001/REC-xml-c14n-20010315)
6 *
7 * See Copyright for the status of this software.
8 *
9 * Author: Aleksey Sanin <[email protected]>
10 */
11
12 #include <libxml/xmlversion.h>
13 #include <config.h>
14
15 #if defined(LIBXML_C14N_ENABLED)
16
17 #include <stdio.h>
18 #include <string.h>
19 #ifdef HAVE_STDLIB_H
20 #include <stdlib.h>
21 #endif
22
23 #include <libxml/xmlmemory.h>
24 #include <libxml/parser.h>
25 #include <libxml/xpath.h>
26 #include <libxml/xpathInternals.h>
27
28 #include <libxml/c14n.h>
29
30 #include "xmlstar.h"
31
32 static void c14nUsage(const char *name, exit_status status)
33 {
34 extern void fprint_c14n_usage(FILE* o, const char* argv0);
35 extern const char more_info[];
36 FILE *o = (status == EXIT_SUCCESS)? stdout : stderr;
37 fprint_c14n_usage(o, name);
38 fprintf(o, "%s", more_info);
39 exit(status);
40 }
41
42 static xmlXPathObjectPtr
43 load_xpath_expr (xmlDocPtr parent_doc, const char* filename);
44
45 static xmlChar **parse_list(xmlChar *str);
46
47 #if 0
48 static void print_xpath_nodes(xmlNodeSetPtr nodes);
49 #endif
50
51 static int
52 run_c14n(const char* xml_filename, int with_comments, int exclusive,
53 const char* xpath_filename, xmlChar **inclusive_namespaces,
54 int nonet) {
55 xmlDocPtr doc;
56 xmlXPathObjectPtr xpath = NULL;
57 int ret;
58
59 /*
60 * build an XML tree from a the file; we need to add default
61 * attributes and resolve all character and entities references
62 */
63
64 doc = xmlReadFile(xml_filename, NULL,
65 XML_PARSE_NOENT | XML_PARSE_DTDLOAD |
66 XML_PARSE_DTDATTR | (nonet? XML_PARSE_NONET:0));
67 if (doc == NULL) {
68 fprintf(stderr, "Error: unable to parse file \"%s\"\n", xml_filename);
69 return(EXIT_BAD_FILE);
70 }
71
72 /*
73 * Check the document is of the right kind
74 */
75 if(xmlDocGetRootElement(doc) == NULL) {
76 fprintf(stderr,"Error: empty document for file \"%s\"\n", xml_filename);
77 xmlFreeDoc(doc);
78 return(EXIT_BAD_FILE);
79 }
80
81 /*
82 * load xpath file if specified
83 */
84 if(xpath_filename) {
85 xpath = load_xpath_expr(doc, xpath_filename);
86 if(xpath == NULL) {
•87 fprintf(stderr, "%s", "Error: unable to evaluate xpath expression\n");
88 xmlFreeDoc(doc);
89 return(EXIT_BAD_FILE);
90 }
91 }
92
93 /*
94 * Canonical form
95 */
96 set_stdout_binary(); /* avoid line ending conversion */
97 ret = xmlC14NDocSave(doc,
98 (xpath) ? xpath->nodesetval : NULL,
99 exclusive, inclusive_namespaces,
100 with_comments, "-", 0);
101 if(ret < 0) {
102 fprintf(stderr,"Error: failed to canonicalize XML file \"%s\" (ret=%d)\n",
103 xml_filename, ret);
104 xmlFreeDoc(doc);
105 return(EXIT_FAILURE);
106 }
107
108 /*
109 * Cleanup
110 */
111 if(xpath != NULL) xmlXPathFreeObject(xpath);
112 xmlFreeDoc(doc);
113
114 return(ret >= 0? EXIT_SUCCESS : EXIT_FAILURE);
115 }
116
117 int c14nMain(int argc, char **argv) {
118 int ret = -1, nonet = 1;
119
120 /*
121 * Init libxml
122 */
123 xmlInitParser();
124 LIBXML_TEST_VERSION
125
126 /*
127 * Parse command line and process file
128 */
129
130 if (argc > 2 && strcmp(argv[2], "--net") == 0) {
131 nonet = 0;
132 /* TODO: parse options properly */
133 argc--;
134 argv++;
135 }
136
137 if (argc < 4) {
138 if (argc >= 3)
139 {
140 if (strcmp(argv[2], "--help") == 0 || strcmp(argv[2], "-h") == 0)
141 c14nUsage(argv[0], EXIT_SUCCESS);
142 }
143 ret = run_c14n((argc > 2)? argv[2] : "-", 1, 0, NULL, NULL, nonet);
144 } else if(strcmp(argv[2], "--with-comments") == 0) {
145 ret = run_c14n(argv[3], 1, 0, (argc > 4) ? argv[4] : NULL, NULL, nonet);
146 } else if(strcmp(argv[2], "--without-comments") == 0) {
147 ret = run_c14n(argv[3], 0, 0, (argc > 4) ? argv[4] : NULL, NULL, nonet);
148 } else if(strcmp(argv[2], "--exc-with-comments") == 0) {
149 xmlChar **list;
150
151 /* load exclusive namespace from command line */
152 list = (argc > 5) ? parse_list((xmlChar *)argv[5]) : NULL;
153 ret = run_c14n(argv[3], 1, 1, (argc > 4) ? argv[4] : NULL, list, nonet);
154 if(list != NULL) xmlFree(list);
155 } else if(strcmp(argv[2], "--exc-without-comments") == 0) {
156 xmlChar **list;
157
158 /* load exclusive namespace from command line */
159 list = (argc > 5) ? parse_list((xmlChar *)argv[5]) : NULL;
160 ret = run_c14n(argv[3], 0, 1, (argc > 4) ? argv[4] : NULL, list, nonet);
161 if(list != NULL) xmlFree(list);
162 } else {
•163 fprintf(stderr, "%s", "error: bad arguments.\n");
164 c14nUsage(argv[0], EXIT_BAD_ARGS);
165 }
166
167 /*
168 * Shutdown libxml
169 */
170 xmlCleanupParser();
171 xmlMemoryDump();
172
173 return ret;
174 }
175
176 /*
177 * Macro used to grow the current buffer.
178 */
179 #define growBufferReentrant() { \
180 buffer_size *= 2; \
181 buffer = (xmlChar **) \
182 xmlRealloc(buffer, buffer_size * sizeof(xmlChar*)); \
183 if (buffer == NULL) { \
184 perror("realloc failed"); \
185 return(NULL); \
186 } \
187 }
188
189 static xmlChar **
190 parse_list(xmlChar *str) {
191 xmlChar **buffer;
192 xmlChar **out = NULL;
193 int buffer_size = 0;
194 int len;
195
196 if(str == NULL) {
197 return(NULL);
198 }
199
200 len = xmlStrlen(str);
201 if((str[0] == '\'') && (str[len - 1] == '\'')) {
202 str[len - 1] = '\0';
203 str++;
204 len -= 2;
205 }
206 /*
207 * allocate an translation buffer.
208 */
209 buffer_size = 1000;
210 buffer = xmlMalloc(buffer_size * sizeof(xmlChar*));
211 out = buffer;
212
213 while(*str != '\0') {
214 if (out - buffer > buffer_size - 10) {
215 int indx = out - buffer;
216
217 growBufferReentrant();
218 out = &buffer[indx];
219 }
220 (*out++) = str;
221 while(*str != ',' && *str != '\0') ++str;
222 if(*str == ',') *(str++) = '\0';
223 }
224 (*out) = NULL;
225 return buffer;
226 }
227
228 static xmlXPathObjectPtr
229 load_xpath_expr (xmlDocPtr parent_doc, const char* filename) {
230 xmlXPathObjectPtr xpath;
231 xmlDocPtr doc;
232 xmlChar *expr;
233 xmlXPathContextPtr ctx;
234 xmlNodePtr node;
235 xmlNsPtr ns;
236
237 /*
238 * load XPath expr as a file
239 */
240 xmlLoadExtDtdDefaultValue = XML_DETECT_IDS | XML_COMPLETE_ATTRS;
241 xmlSubstituteEntitiesDefault(1);
242
243 doc = xmlReadFile(filename, NULL, XML_PARSE_DTDLOAD | XML_PARSE_DTDATTR);
244 if (doc == NULL) {
245 fprintf(stderr, "Error: unable to parse file \"%s\"\n", filename);
246 return(NULL);
247 }
248
249 /*
250 * Check the document is of the right kind
251 */
252 if(xmlDocGetRootElement(doc) == NULL) {
253 fprintf(stderr,"Error: empty document for file \"%s\"\n", filename);
254 xmlFreeDoc(doc);
255 return(NULL);
256 }
257
258 node = doc->children;
259 while(node != NULL && !xmlStrEqual(node->name, (const xmlChar *)"XPath")) {
260 node = node->next;
261 }
262
263 if(node == NULL) {
264 fprintf(stderr,"Error: XPath element expected in the file \"%s\"\n", filename);
265 xmlFreeDoc(doc);
266 return(NULL);
267 }
268
269 expr = xmlNodeGetContent(node);
270 if(expr == NULL) {
271 fprintf(stderr,"Error: XPath content element is NULL \"%s\"\n", filename);
272 xmlFreeDoc(doc);
273 return(NULL);
274 }
275
276 ctx = xmlXPathNewContext(parent_doc);
277 if(ctx == NULL) {
•278 fprintf(stderr, "%s", "Error: unable to create new context\n");
279 xmlFree(expr);
280 xmlFreeDoc(doc);
281 return(NULL);
282 }
283
284 /*
285 * Register namespaces
286 */
287 ns = node->nsDef;
288 while(ns != NULL) {
289 if(xmlXPathRegisterNs(ctx, ns->prefix, ns->href) != 0) {
290 fprintf(stderr,"Error: unable to register NS with prefix=\"%s\" and href=\"%s\"\n", ns->prefix, ns->href);
291 xmlFree(expr);
292 xmlXPathFreeContext(ctx);
293 xmlFreeDoc(doc);
294 return(NULL);
295 }
296 ns = ns->next;
297 }
298
299 /*
300 * Evaluate xpath
301 */
302 xpath = xmlXPathEvalExpression(expr, ctx);
303 if(xpath == NULL) {
•304 fprintf(stderr, "%s", "Error: unable to evaluate xpath expression\n");
305 xmlFree(expr);
306 xmlXPathFreeContext(ctx);
307 xmlFreeDoc(doc);
308 return(NULL);
309 }
310
311 /* print_xpath_nodes(xpath->nodesetval); */
312
313 xmlFree(expr);
314 xmlXPathFreeContext(ctx);
315 xmlFreeDoc(doc);
316 return(xpath);
317 }
318
319 #if 0
320 static void
321 print_xpath_nodes(xmlNodeSetPtr nodes) {
322 xmlNodePtr cur;
323 int i;
324
325 if(nodes == NULL ){
•326 fprintf(stderr, "%s", "Error: no nodes set defined\n");
327 return;
328 }
329
•330 fprintf(stderr, "%s", "Nodes Set:\n-----\n");
331 for(i = 0; i < nodes->nodeNr; ++i) {
332 if(nodes->nodeTab[i]->type == XML_NAMESPACE_DECL) {
333 xmlNsPtr ns;
334
335 ns = (xmlNsPtr)nodes->nodeTab[i];
336 cur = (xmlNodePtr)ns->next;
337 fprintf(stderr, "namespace \"%s\"=\"%s\" for node %s:%s\n",
338 ns->prefix, ns->href,
339 (cur->ns) ? cur->ns->prefix : BAD_CAST "", cur->name);
340 } else if(nodes->nodeTab[i]->type == XML_ELEMENT_NODE) {
341 cur = nodes->nodeTab[i];
342 fprintf(stderr, "element node \"%s:%s\"\n",
343 (cur->ns) ? cur->ns->prefix : BAD_CAST "", cur->name);
344 } else {
345 cur = nodes->nodeTab[i];
346 fprintf(stderr, "node \"%s\": type %d\n", cur->name, cur->type);
347 }
348 }
349 }
350 #endif
351
352 #else
353 #include <stdio.h>
354 int c14nMain(int argc, char **argv) {
355 printf("%s : XPath/Canonicalization support not compiled in\n", argv[0]);
356 return 2;
357 }
358 #endif /* LIBXML_C14N_ENABLED */
359