blob: dc1a9a66dc0dd847c769a17f44de7d7d3a06f96e [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"
Daniel Veillarda9cce9c2003-09-29 13:20:24 +000010#if defined(LIBXML_C14N_ENABLED) && defined(LIBXML_OUTPUT_ENABLED)
Daniel Veillard044fc6b2002-03-04 17:09:44 +000011
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,
Aleksey Sanin83868242009-07-09 10:26:22 +020035 "--with-comments \t XML file canonicalization v1.0 w comments \n");
Daniel Veillard044fc6b2002-03-04 17:09:44 +000036 fprintf(stderr,
Aleksey Sanin83868242009-07-09 10:26:22 +020037 "--without-comments \t XML file canonicalization v1.0 w/o comments\n");
Daniel Veillard044fc6b2002-03-04 17:09:44 +000038 fprintf(stderr,
Aleksey Sanin83868242009-07-09 10:26:22 +020039 "--1-1-with-comments \t XML file canonicalization v1.1 w comments\n");
Daniel Veillard044fc6b2002-03-04 17:09:44 +000040 fprintf(stderr,
Aleksey Sanin83868242009-07-09 10:26:22 +020041 "--1-1-without-comments \t XML file canonicalization v1.1 w/o comments\n");
42 fprintf(stderr,
43 "--exc-with-comments \t Exclusive XML file canonicalization v1.0 w comments\n");
44 fprintf(stderr,
45 "--exc-without-comments\t Exclusive XML file canonicalization v1.0 w/o comments\n");
Daniel Veillard044fc6b2002-03-04 17:09:44 +000046}
47
Daniel Veillardfe8aab92002-12-22 10:25:41 +000048static xmlXPathObjectPtr
Daniel Veillard044fc6b2002-03-04 17:09:44 +000049load_xpath_expr (xmlDocPtr parent_doc, const char* filename);
50
Daniel Veillardfe8aab92002-12-22 10:25:41 +000051static xmlChar **parse_list(xmlChar *str);
Daniel Veillard044fc6b2002-03-04 17:09:44 +000052
William M. Brackc1939562003-08-05 15:52:22 +000053/* static void print_xpath_nodes(xmlNodeSetPtr nodes); */
Daniel Veillard044fc6b2002-03-04 17:09:44 +000054
55static int
Aleksey Sanin83868242009-07-09 10:26:22 +020056test_c14n(const char* xml_filename, int with_comments, int mode,
Daniel Veillard044fc6b2002-03-04 17:09:44 +000057 const char* xpath_filename, xmlChar **inclusive_namespaces) {
58 xmlDocPtr doc;
59 xmlXPathObjectPtr xpath = NULL;
60 xmlChar *result = NULL;
61 int ret;
62
63 /*
64 * build an XML tree from a the file; we need to add default
65 * attributes and resolve all character and entities references
66 */
67 xmlLoadExtDtdDefaultValue = XML_DETECT_IDS | XML_COMPLETE_ATTRS;
68 xmlSubstituteEntitiesDefault(1);
69
Daniel Veillardbca3ad22005-08-23 22:14:02 +000070 doc = xmlReadFile(xml_filename, NULL, XML_PARSE_DTDATTR | XML_PARSE_NOENT);
Daniel Veillard044fc6b2002-03-04 17:09:44 +000071 if (doc == NULL) {
72 fprintf(stderr, "Error: unable to parse file \"%s\"\n", xml_filename);
73 return(-1);
74 }
75
76 /*
77 * Check the document is of the right kind
78 */
79 if(xmlDocGetRootElement(doc) == NULL) {
80 fprintf(stderr,"Error: empty document for file \"%s\"\n", xml_filename);
81 xmlFreeDoc(doc);
82 return(-1);
83 }
84
85 /*
86 * load xpath file if specified
87 */
88 if(xpath_filename) {
89 xpath = load_xpath_expr(doc, xpath_filename);
90 if(xpath == NULL) {
91 fprintf(stderr,"Error: unable to evaluate xpath expression\n");
92 xmlFreeDoc(doc);
93 return(-1);
94 }
95 }
96
97 /*
98 * Canonical form
99 */
100 /* fprintf(stderr,"File \"%s\" loaded: start canonization\n", xml_filename); */
101 ret = xmlC14NDocDumpMemory(doc,
102 (xpath) ? xpath->nodesetval : NULL,
Aleksey Sanin83868242009-07-09 10:26:22 +0200103 mode, inclusive_namespaces,
Daniel Veillard044fc6b2002-03-04 17:09:44 +0000104 with_comments, &result);
105 if(ret >= 0) {
106 if(result != NULL) {
Stefan Kostdff8d0f2011-05-09 12:14:59 +0300107 if (write(STDOUT_FILENO, result, ret) == -1) {
108 fprintf(stderr, "Can't write data\n");
109 }
110 xmlFree(result);
Daniel Veillard044fc6b2002-03-04 17:09:44 +0000111 }
112 } else {
113 fprintf(stderr,"Error: failed to canonicalize XML file \"%s\" (ret=%d)\n", xml_filename, ret);
114 if(result != NULL) xmlFree(result);
115 xmlFreeDoc(doc);
116 return(-1);
117 }
118
119 /*
120 * Cleanup
121 */
122 if(xpath != NULL) xmlXPathFreeObject(xpath);
123 xmlFreeDoc(doc);
124
125 return(ret);
126}
127
128int main(int argc, char **argv) {
129 int ret = -1;
130
131 /*
132 * Init libxml
133 */
134 xmlInitParser();
135 LIBXML_TEST_VERSION
136
137 /*
138 * Parse command line and process file
139 */
140 if( argc < 3 ) {
141 fprintf(stderr, "Error: wrong number of arguments.\n");
142 usage(argv[0]);
143 } else if(strcmp(argv[1], "--with-comments") == 0) {
Aleksey Sanin83868242009-07-09 10:26:22 +0200144 ret = test_c14n(argv[2], 1, XML_C14N_1_0, (argc > 3) ? argv[3] : NULL, NULL);
Daniel Veillard044fc6b2002-03-04 17:09:44 +0000145 } else if(strcmp(argv[1], "--without-comments") == 0) {
Aleksey Sanin83868242009-07-09 10:26:22 +0200146 ret = test_c14n(argv[2], 0, XML_C14N_1_0, (argc > 3) ? argv[3] : NULL, NULL);
147 } else if(strcmp(argv[1], "--1-1-with-comments") == 0) {
148 ret = test_c14n(argv[2], 1, XML_C14N_1_1, (argc > 3) ? argv[3] : NULL, NULL);
149 } else if(strcmp(argv[1], "--1-1-without-comments") == 0) {
150 ret = test_c14n(argv[2], 0, XML_C14N_1_1, (argc > 3) ? argv[3] : NULL, NULL);
Daniel Veillard044fc6b2002-03-04 17:09:44 +0000151 } else if(strcmp(argv[1], "--exc-with-comments") == 0) {
152 xmlChar **list;
153
154 /* load exclusive namespace from command line */
155 list = (argc > 4) ? parse_list((xmlChar *)argv[4]) : NULL;
Aleksey Sanin83868242009-07-09 10:26:22 +0200156 ret = test_c14n(argv[2], 1, XML_C14N_EXCLUSIVE_1_0, (argc > 3) ? argv[3] : NULL, list);
Daniel Veillard044fc6b2002-03-04 17:09:44 +0000157 if(list != NULL) xmlFree(list);
158 } else if(strcmp(argv[1], "--exc-without-comments") == 0) {
159 xmlChar **list;
160
161 /* load exclusive namespace from command line */
162 list = (argc > 4) ? parse_list((xmlChar *)argv[4]) : NULL;
Aleksey Sanin83868242009-07-09 10:26:22 +0200163 ret = test_c14n(argv[2], 0, XML_C14N_EXCLUSIVE_1_0, (argc > 3) ? argv[3] : NULL, list);
Daniel Veillard044fc6b2002-03-04 17:09:44 +0000164 if(list != NULL) xmlFree(list);
165 } else {
166 fprintf(stderr, "Error: bad option.\n");
167 usage(argv[0]);
168 }
169
170 /*
171 * Shutdown libxml
172 */
173 xmlCleanupParser();
174 xmlMemoryDump();
Daniel Veillard13cee4e2009-09-05 14:52:55 +0200175
Daniel Veillard044fc6b2002-03-04 17:09:44 +0000176 return((ret >= 0) ? 0 : 1);
177}
178
179/*
180 * Macro used to grow the current buffer.
181 */
182#define growBufferReentrant() { \
183 buffer_size *= 2; \
184 buffer = (xmlChar **) \
Daniel Veillard13cee4e2009-09-05 14:52:55 +0200185 xmlRealloc(buffer, buffer_size * sizeof(xmlChar*)); \
Daniel Veillard044fc6b2002-03-04 17:09:44 +0000186 if (buffer == NULL) { \
187 perror("realloc failed"); \
188 return(NULL); \
189 } \
190}
191
Daniel Veillard01c13b52002-12-10 15:19:08 +0000192static xmlChar **
193parse_list(xmlChar *str) {
Daniel Veillard044fc6b2002-03-04 17:09:44 +0000194 xmlChar **buffer;
195 xmlChar **out = NULL;
196 int buffer_size = 0;
Aleksey Saninf8cb6dd2002-06-04 04:27:06 +0000197 int len;
Daniel Veillard044fc6b2002-03-04 17:09:44 +0000198
199 if(str == NULL) {
200 return(NULL);
201 }
202
Daniel Veillard118aed72002-09-24 14:13:13 +0000203 len = xmlStrlen(str);
Aleksey Saninf8cb6dd2002-06-04 04:27:06 +0000204 if((str[0] == '\'') && (str[len - 1] == '\'')) {
205 str[len - 1] = '\0';
206 str++;
Aleksey Saninf8cb6dd2002-06-04 04:27:06 +0000207 }
Daniel Veillard044fc6b2002-03-04 17:09:44 +0000208 /*
209 * allocate an translation buffer.
210 */
211 buffer_size = 1000;
212 buffer = (xmlChar **) xmlMalloc(buffer_size * sizeof(xmlChar*));
213 if (buffer == NULL) {
214 perror("malloc failed");
215 return(NULL);
216 }
217 out = buffer;
Daniel Veillard13cee4e2009-09-05 14:52:55 +0200218
Daniel Veillard044fc6b2002-03-04 17:09:44 +0000219 while(*str != '\0') {
220 if (out - buffer > buffer_size - 10) {
221 int indx = out - buffer;
222
223 growBufferReentrant();
224 out = &buffer[indx];
225 }
226 (*out++) = str;
227 while(*str != ',' && *str != '\0') ++str;
228 if(*str == ',') *(str++) = '\0';
229 }
230 (*out) = NULL;
231 return buffer;
232}
233
Daniel Veillard01c13b52002-12-10 15:19:08 +0000234static xmlXPathObjectPtr
Daniel Veillard044fc6b2002-03-04 17:09:44 +0000235load_xpath_expr (xmlDocPtr parent_doc, const char* filename) {
236 xmlXPathObjectPtr xpath;
237 xmlDocPtr doc;
238 xmlChar *expr;
239 xmlXPathContextPtr ctx;
240 xmlNodePtr node;
241 xmlNsPtr ns;
242
243 /*
244 * load XPath expr as a file
245 */
246 xmlLoadExtDtdDefaultValue = XML_DETECT_IDS | XML_COMPLETE_ATTRS;
247 xmlSubstituteEntitiesDefault(1);
248
Daniel Veillardbca3ad22005-08-23 22:14:02 +0000249 doc = xmlReadFile(filename, NULL, XML_PARSE_DTDATTR | XML_PARSE_NOENT);
Daniel Veillard044fc6b2002-03-04 17:09:44 +0000250 if (doc == NULL) {
251 fprintf(stderr, "Error: unable to parse file \"%s\"\n", filename);
252 return(NULL);
253 }
254
255 /*
256 * Check the document is of the right kind
257 */
258 if(xmlDocGetRootElement(doc) == NULL) {
259 fprintf(stderr,"Error: empty document for file \"%s\"\n", filename);
260 xmlFreeDoc(doc);
261 return(NULL);
262 }
263
264 node = doc->children;
265 while(node != NULL && !xmlStrEqual(node->name, (const xmlChar *)"XPath")) {
266 node = node->next;
267 }
268
269 if(node == NULL) {
270 fprintf(stderr,"Error: XPath element expected in the file \"%s\"\n", filename);
271 xmlFreeDoc(doc);
272 return(NULL);
273 }
274
275 expr = xmlNodeGetContent(node);
276 if(expr == NULL) {
277 fprintf(stderr,"Error: XPath content element is NULL \"%s\"\n", filename);
278 xmlFreeDoc(doc);
279 return(NULL);
280 }
281
282 ctx = xmlXPathNewContext(parent_doc);
283 if(ctx == NULL) {
284 fprintf(stderr,"Error: unable to create new context\n");
285 xmlFree(expr);
286 xmlFreeDoc(doc);
287 return(NULL);
288 }
289
290 /*
291 * Register namespaces
292 */
293 ns = node->nsDef;
294 while(ns != NULL) {
295 if(xmlXPathRegisterNs(ctx, ns->prefix, ns->href) != 0) {
296 fprintf(stderr,"Error: unable to register NS with prefix=\"%s\" and href=\"%s\"\n", ns->prefix, ns->href);
297 xmlFree(expr);
298 xmlXPathFreeContext(ctx);
299 xmlFreeDoc(doc);
300 return(NULL);
301 }
302 ns = ns->next;
303 }
304
305 /*
306 * Evaluate xpath
307 */
308 xpath = xmlXPathEvalExpression(expr, ctx);
309 if(xpath == NULL) {
310 fprintf(stderr,"Error: unable to evaluate xpath expression\n");
311 xmlFree(expr);
312 xmlXPathFreeContext(ctx);
313 xmlFreeDoc(doc);
314 return(NULL);
315 }
316
Aleksey Saninf8cb6dd2002-06-04 04:27:06 +0000317 /* print_xpath_nodes(xpath->nodesetval); */
Daniel Veillard044fc6b2002-03-04 17:09:44 +0000318
319 xmlFree(expr);
320 xmlXPathFreeContext(ctx);
321 xmlFreeDoc(doc);
322 return(xpath);
323}
324
William M. Brackc1939562003-08-05 15:52:22 +0000325/*
Daniel Veillard01c13b52002-12-10 15:19:08 +0000326static void
Aleksey Saninf8cb6dd2002-06-04 04:27:06 +0000327print_xpath_nodes(xmlNodeSetPtr nodes) {
Daniel Veillard044fc6b2002-03-04 17:09:44 +0000328 xmlNodePtr cur;
329 int i;
330
Aleksey Saninf8cb6dd2002-06-04 04:27:06 +0000331 if(nodes == NULL ){
Daniel Veillard044fc6b2002-03-04 17:09:44 +0000332 fprintf(stderr, "Error: no nodes set defined\n");
333 return;
334 }
335
Aleksey Saninf8cb6dd2002-06-04 04:27:06 +0000336 fprintf(stderr, "Nodes Set:\n-----\n");
337 for(i = 0; i < nodes->nodeNr; ++i) {
338 if(nodes->nodeTab[i]->type == XML_NAMESPACE_DECL) {
339 xmlNsPtr ns;
340
341 ns = (xmlNsPtr)nodes->nodeTab[i];
342 cur = (xmlNodePtr)ns->next;
343 fprintf(stderr, "namespace \"%s\"=\"%s\" for node %s:%s\n",
344 ns->prefix, ns->href,
345 (cur->ns) ? cur->ns->prefix : BAD_CAST "", cur->name);
346 } else if(nodes->nodeTab[i]->type == XML_ELEMENT_NODE) {
347 cur = nodes->nodeTab[i];
348 fprintf(stderr, "element node \"%s:%s\"\n",
349 (cur->ns) ? cur->ns->prefix : BAD_CAST "", cur->name);
350 } else {
351 cur = nodes->nodeTab[i];
352 fprintf(stderr, "node \"%s\": type %d\n", cur->name, cur->type);
353 }
Daniel Veillard044fc6b2002-03-04 17:09:44 +0000354 }
355}
William M. Brackc1939562003-08-05 15:52:22 +0000356*/
Aleksey Saninf8cb6dd2002-06-04 04:27:06 +0000357
Daniel Veillard044fc6b2002-03-04 17:09:44 +0000358#else
359#include <stdio.h>
Daniel Veillarda9cce9c2003-09-29 13:20:24 +0000360int main(int argc ATTRIBUTE_UNUSED, char **argv ATTRIBUTE_UNUSED) {
361 printf("%s : XPath/Canonicalization and output support not compiled in\n", argv[0]);
Daniel Veillard044fc6b2002-03-04 17:09:44 +0000362 return(0);
363}
364#endif /* LIBXML_C14N_ENABLED */
365
366