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