blob: 86e58f27e405d477850da133023d2dcdc1cdfa91 [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.
Daniel Veillardf8e3db02012-09-11 13:26:36 +08006 *
Daniel Veillard044fc6b2002-03-04 17:09:44 +00007 * 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>
Eric Zurcher414f2692012-08-15 13:52:09 +080014#ifndef STDOUT_FILENO
Daniel Veillard044fc6b2002-03-04 17:09:44 +000015#ifdef HAVE_UNISTD_H
16#include <unistd.h>
Tay Ray Chuanb8428a22012-01-02 15:49:31 +080017#else
18#define STDOUT_FILENO fileno(stdout)
19#endif /* HAVE_UNISTD_H */
Eric Zurcher414f2692012-08-15 13:52:09 +080020#endif
Daniel Veillard044fc6b2002-03-04 17:09:44 +000021#ifdef HAVE_STDLIB_H
22#include <stdlib.h>
23#endif
24
25#include <libxml/xmlmemory.h>
26#include <libxml/parser.h>
27#include <libxml/xpath.h>
28#include <libxml/xpathInternals.h>
29
30#include <libxml/c14n.h>
31
32
33static void usage(const char *name) {
34 fprintf(stderr,
35 "Usage: %s <mode> <xml-file> [<xpath-expr>] [<inclusive-ns-list>]\n",
36 name);
37 fprintf(stderr, "where <mode> is one of following:\n");
38 fprintf(stderr,
Aleksey Sanin83868242009-07-09 10:26:22 +020039 "--with-comments \t XML file canonicalization v1.0 w comments \n");
Daniel Veillard044fc6b2002-03-04 17:09:44 +000040 fprintf(stderr,
Aleksey Sanin83868242009-07-09 10:26:22 +020041 "--without-comments \t XML file canonicalization v1.0 w/o comments\n");
Daniel Veillard044fc6b2002-03-04 17:09:44 +000042 fprintf(stderr,
Aleksey Sanin83868242009-07-09 10:26:22 +020043 "--1-1-with-comments \t XML file canonicalization v1.1 w comments\n");
Daniel Veillard044fc6b2002-03-04 17:09:44 +000044 fprintf(stderr,
Aleksey Sanin83868242009-07-09 10:26:22 +020045 "--1-1-without-comments \t XML file canonicalization v1.1 w/o comments\n");
46 fprintf(stderr,
47 "--exc-with-comments \t Exclusive XML file canonicalization v1.0 w comments\n");
48 fprintf(stderr,
49 "--exc-without-comments\t Exclusive XML file canonicalization v1.0 w/o comments\n");
Daniel Veillard044fc6b2002-03-04 17:09:44 +000050}
51
Daniel Veillardfe8aab92002-12-22 10:25:41 +000052static xmlXPathObjectPtr
Daniel Veillard044fc6b2002-03-04 17:09:44 +000053load_xpath_expr (xmlDocPtr parent_doc, const char* filename);
54
Daniel Veillardfe8aab92002-12-22 10:25:41 +000055static xmlChar **parse_list(xmlChar *str);
Daniel Veillard044fc6b2002-03-04 17:09:44 +000056
William M. Brackc1939562003-08-05 15:52:22 +000057/* static void print_xpath_nodes(xmlNodeSetPtr nodes); */
Daniel Veillard044fc6b2002-03-04 17:09:44 +000058
Daniel Veillardf8e3db02012-09-11 13:26:36 +080059static int
Aleksey Sanin83868242009-07-09 10:26:22 +020060test_c14n(const char* xml_filename, int with_comments, int mode,
Daniel Veillard044fc6b2002-03-04 17:09:44 +000061 const char* xpath_filename, xmlChar **inclusive_namespaces) {
62 xmlDocPtr doc;
Daniel Veillardf8e3db02012-09-11 13:26:36 +080063 xmlXPathObjectPtr xpath = NULL;
Daniel Veillard044fc6b2002-03-04 17:09:44 +000064 xmlChar *result = NULL;
65 int ret;
66
67 /*
68 * build an XML tree from a the file; we need to add default
69 * attributes and resolve all character and entities references
70 */
71 xmlLoadExtDtdDefaultValue = XML_DETECT_IDS | XML_COMPLETE_ATTRS;
72 xmlSubstituteEntitiesDefault(1);
73
Daniel Veillardbca3ad22005-08-23 22:14:02 +000074 doc = xmlReadFile(xml_filename, NULL, XML_PARSE_DTDATTR | XML_PARSE_NOENT);
Daniel Veillard044fc6b2002-03-04 17:09:44 +000075 if (doc == NULL) {
76 fprintf(stderr, "Error: unable to parse file \"%s\"\n", xml_filename);
77 return(-1);
78 }
Daniel Veillardf8e3db02012-09-11 13:26:36 +080079
Daniel Veillard044fc6b2002-03-04 17:09:44 +000080 /*
81 * Check the document is of the right kind
Daniel Veillardf8e3db02012-09-11 13:26:36 +080082 */
Daniel Veillard044fc6b2002-03-04 17:09:44 +000083 if(xmlDocGetRootElement(doc) == NULL) {
84 fprintf(stderr,"Error: empty document for file \"%s\"\n", xml_filename);
85 xmlFreeDoc(doc);
86 return(-1);
87 }
88
Daniel Veillardf8e3db02012-09-11 13:26:36 +080089 /*
90 * load xpath file if specified
Daniel Veillard044fc6b2002-03-04 17:09:44 +000091 */
92 if(xpath_filename) {
93 xpath = load_xpath_expr(doc, xpath_filename);
94 if(xpath == NULL) {
95 fprintf(stderr,"Error: unable to evaluate xpath expression\n");
Daniel Veillardf8e3db02012-09-11 13:26:36 +080096 xmlFreeDoc(doc);
Daniel Veillard044fc6b2002-03-04 17:09:44 +000097 return(-1);
98 }
99 }
100
101 /*
102 * Canonical form
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800103 */
Daniel Veillard044fc6b2002-03-04 17:09:44 +0000104 /* fprintf(stderr,"File \"%s\" loaded: start canonization\n", xml_filename); */
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800105 ret = xmlC14NDocDumpMemory(doc,
106 (xpath) ? xpath->nodesetval : NULL,
Aleksey Sanin83868242009-07-09 10:26:22 +0200107 mode, inclusive_namespaces,
Daniel Veillard044fc6b2002-03-04 17:09:44 +0000108 with_comments, &result);
109 if(ret >= 0) {
110 if(result != NULL) {
Stefan Kostdff8d0f2011-05-09 12:14:59 +0300111 if (write(STDOUT_FILENO, result, ret) == -1) {
112 fprintf(stderr, "Can't write data\n");
113 }
114 xmlFree(result);
Daniel Veillard044fc6b2002-03-04 17:09:44 +0000115 }
116 } else {
117 fprintf(stderr,"Error: failed to canonicalize XML file \"%s\" (ret=%d)\n", xml_filename, ret);
118 if(result != NULL) xmlFree(result);
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800119 xmlFreeDoc(doc);
Daniel Veillard044fc6b2002-03-04 17:09:44 +0000120 return(-1);
121 }
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800122
Daniel Veillard044fc6b2002-03-04 17:09:44 +0000123 /*
124 * Cleanup
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800125 */
Daniel Veillard044fc6b2002-03-04 17:09:44 +0000126 if(xpath != NULL) xmlXPathFreeObject(xpath);
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800127 xmlFreeDoc(doc);
Daniel Veillard044fc6b2002-03-04 17:09:44 +0000128
129 return(ret);
130}
131
132int main(int argc, char **argv) {
133 int ret = -1;
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800134
Daniel Veillard044fc6b2002-03-04 17:09:44 +0000135 /*
136 * Init libxml
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800137 */
Daniel Veillard044fc6b2002-03-04 17:09:44 +0000138 xmlInitParser();
139 LIBXML_TEST_VERSION
140
141 /*
142 * Parse command line and process file
143 */
144 if( argc < 3 ) {
145 fprintf(stderr, "Error: wrong number of arguments.\n");
146 usage(argv[0]);
147 } else if(strcmp(argv[1], "--with-comments") == 0) {
Aleksey Sanin83868242009-07-09 10:26:22 +0200148 ret = test_c14n(argv[2], 1, XML_C14N_1_0, (argc > 3) ? argv[3] : NULL, NULL);
Daniel Veillard044fc6b2002-03-04 17:09:44 +0000149 } else if(strcmp(argv[1], "--without-comments") == 0) {
Aleksey Sanin83868242009-07-09 10:26:22 +0200150 ret = test_c14n(argv[2], 0, XML_C14N_1_0, (argc > 3) ? argv[3] : NULL, NULL);
151 } else if(strcmp(argv[1], "--1-1-with-comments") == 0) {
152 ret = test_c14n(argv[2], 1, XML_C14N_1_1, (argc > 3) ? argv[3] : NULL, NULL);
153 } else if(strcmp(argv[1], "--1-1-without-comments") == 0) {
154 ret = test_c14n(argv[2], 0, XML_C14N_1_1, (argc > 3) ? argv[3] : NULL, NULL);
Daniel Veillard044fc6b2002-03-04 17:09:44 +0000155 } else if(strcmp(argv[1], "--exc-with-comments") == 0) {
156 xmlChar **list;
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800157
Daniel Veillard044fc6b2002-03-04 17:09:44 +0000158 /* load exclusive namespace from command line */
159 list = (argc > 4) ? parse_list((xmlChar *)argv[4]) : NULL;
Aleksey Sanin83868242009-07-09 10:26:22 +0200160 ret = test_c14n(argv[2], 1, XML_C14N_EXCLUSIVE_1_0, (argc > 3) ? argv[3] : NULL, list);
Daniel Veillard044fc6b2002-03-04 17:09:44 +0000161 if(list != NULL) xmlFree(list);
162 } else if(strcmp(argv[1], "--exc-without-comments") == 0) {
163 xmlChar **list;
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800164
Daniel Veillard044fc6b2002-03-04 17:09:44 +0000165 /* load exclusive namespace from command line */
166 list = (argc > 4) ? parse_list((xmlChar *)argv[4]) : NULL;
Aleksey Sanin83868242009-07-09 10:26:22 +0200167 ret = test_c14n(argv[2], 0, XML_C14N_EXCLUSIVE_1_0, (argc > 3) ? argv[3] : NULL, list);
Daniel Veillard044fc6b2002-03-04 17:09:44 +0000168 if(list != NULL) xmlFree(list);
169 } else {
170 fprintf(stderr, "Error: bad option.\n");
171 usage(argv[0]);
172 }
173
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800174 /*
Daniel Veillard044fc6b2002-03-04 17:09:44 +0000175 * Shutdown libxml
176 */
177 xmlCleanupParser();
178 xmlMemoryDump();
Daniel Veillard13cee4e2009-09-05 14:52:55 +0200179
Daniel Veillard044fc6b2002-03-04 17:09:44 +0000180 return((ret >= 0) ? 0 : 1);
181}
182
183/*
184 * Macro used to grow the current buffer.
185 */
186#define growBufferReentrant() { \
187 buffer_size *= 2; \
188 buffer = (xmlChar **) \
Daniel Veillard13cee4e2009-09-05 14:52:55 +0200189 xmlRealloc(buffer, buffer_size * sizeof(xmlChar*)); \
Daniel Veillard044fc6b2002-03-04 17:09:44 +0000190 if (buffer == NULL) { \
191 perror("realloc failed"); \
192 return(NULL); \
193 } \
194}
195
Daniel Veillard01c13b52002-12-10 15:19:08 +0000196static xmlChar **
197parse_list(xmlChar *str) {
Daniel Veillard044fc6b2002-03-04 17:09:44 +0000198 xmlChar **buffer;
199 xmlChar **out = NULL;
200 int buffer_size = 0;
Aleksey Saninf8cb6dd2002-06-04 04:27:06 +0000201 int len;
Daniel Veillard044fc6b2002-03-04 17:09:44 +0000202
203 if(str == NULL) {
204 return(NULL);
205 }
206
Daniel Veillard118aed72002-09-24 14:13:13 +0000207 len = xmlStrlen(str);
Aleksey Saninf8cb6dd2002-06-04 04:27:06 +0000208 if((str[0] == '\'') && (str[len - 1] == '\'')) {
209 str[len - 1] = '\0';
210 str++;
Aleksey Saninf8cb6dd2002-06-04 04:27:06 +0000211 }
Daniel Veillard044fc6b2002-03-04 17:09:44 +0000212 /*
213 * allocate an translation buffer.
214 */
215 buffer_size = 1000;
216 buffer = (xmlChar **) xmlMalloc(buffer_size * sizeof(xmlChar*));
217 if (buffer == NULL) {
218 perror("malloc failed");
219 return(NULL);
220 }
221 out = buffer;
Daniel Veillard13cee4e2009-09-05 14:52:55 +0200222
Daniel Veillard044fc6b2002-03-04 17:09:44 +0000223 while(*str != '\0') {
224 if (out - buffer > buffer_size - 10) {
225 int indx = out - buffer;
226
227 growBufferReentrant();
228 out = &buffer[indx];
229 }
230 (*out++) = str;
231 while(*str != ',' && *str != '\0') ++str;
232 if(*str == ',') *(str++) = '\0';
233 }
234 (*out) = NULL;
235 return buffer;
236}
237
Daniel Veillard01c13b52002-12-10 15:19:08 +0000238static xmlXPathObjectPtr
Daniel Veillard044fc6b2002-03-04 17:09:44 +0000239load_xpath_expr (xmlDocPtr parent_doc, const char* filename) {
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800240 xmlXPathObjectPtr xpath;
Daniel Veillard044fc6b2002-03-04 17:09:44 +0000241 xmlDocPtr doc;
242 xmlChar *expr;
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800243 xmlXPathContextPtr ctx;
Daniel Veillard044fc6b2002-03-04 17:09:44 +0000244 xmlNodePtr node;
245 xmlNsPtr ns;
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800246
Daniel Veillard044fc6b2002-03-04 17:09:44 +0000247 /*
248 * load XPath expr as a file
249 */
250 xmlLoadExtDtdDefaultValue = XML_DETECT_IDS | XML_COMPLETE_ATTRS;
251 xmlSubstituteEntitiesDefault(1);
252
Daniel Veillardbca3ad22005-08-23 22:14:02 +0000253 doc = xmlReadFile(filename, NULL, XML_PARSE_DTDATTR | XML_PARSE_NOENT);
Daniel Veillard044fc6b2002-03-04 17:09:44 +0000254 if (doc == NULL) {
255 fprintf(stderr, "Error: unable to parse file \"%s\"\n", filename);
256 return(NULL);
257 }
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800258
Daniel Veillard044fc6b2002-03-04 17:09:44 +0000259 /*
260 * Check the document is of the right kind
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800261 */
Daniel Veillard044fc6b2002-03-04 17:09:44 +0000262 if(xmlDocGetRootElement(doc) == NULL) {
263 fprintf(stderr,"Error: empty document for file \"%s\"\n", filename);
264 xmlFreeDoc(doc);
265 return(NULL);
266 }
267
268 node = doc->children;
269 while(node != NULL && !xmlStrEqual(node->name, (const xmlChar *)"XPath")) {
270 node = node->next;
271 }
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800272
273 if(node == NULL) {
Daniel Veillard044fc6b2002-03-04 17:09:44 +0000274 fprintf(stderr,"Error: XPath element expected in the file \"%s\"\n", filename);
275 xmlFreeDoc(doc);
276 return(NULL);
277 }
278
279 expr = xmlNodeGetContent(node);
280 if(expr == NULL) {
281 fprintf(stderr,"Error: XPath content element is NULL \"%s\"\n", filename);
282 xmlFreeDoc(doc);
283 return(NULL);
284 }
285
286 ctx = xmlXPathNewContext(parent_doc);
287 if(ctx == NULL) {
288 fprintf(stderr,"Error: unable to create new context\n");
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800289 xmlFree(expr);
290 xmlFreeDoc(doc);
Daniel Veillard044fc6b2002-03-04 17:09:44 +0000291 return(NULL);
292 }
293
294 /*
295 * Register namespaces
296 */
297 ns = node->nsDef;
298 while(ns != NULL) {
299 if(xmlXPathRegisterNs(ctx, ns->prefix, ns->href) != 0) {
300 fprintf(stderr,"Error: unable to register NS with prefix=\"%s\" and href=\"%s\"\n", ns->prefix, ns->href);
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800301 xmlFree(expr);
302 xmlXPathFreeContext(ctx);
303 xmlFreeDoc(doc);
Daniel Veillard044fc6b2002-03-04 17:09:44 +0000304 return(NULL);
305 }
306 ns = ns->next;
307 }
308
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800309 /*
Daniel Veillard044fc6b2002-03-04 17:09:44 +0000310 * Evaluate xpath
311 */
312 xpath = xmlXPathEvalExpression(expr, ctx);
313 if(xpath == NULL) {
314 fprintf(stderr,"Error: unable to evaluate xpath expression\n");
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800315 xmlFree(expr);
316 xmlXPathFreeContext(ctx);
317 xmlFreeDoc(doc);
Daniel Veillard044fc6b2002-03-04 17:09:44 +0000318 return(NULL);
319 }
320
Aleksey Saninf8cb6dd2002-06-04 04:27:06 +0000321 /* print_xpath_nodes(xpath->nodesetval); */
Daniel Veillard044fc6b2002-03-04 17:09:44 +0000322
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800323 xmlFree(expr);
324 xmlXPathFreeContext(ctx);
325 xmlFreeDoc(doc);
Daniel Veillard044fc6b2002-03-04 17:09:44 +0000326 return(xpath);
327}
328
William M. Brackc1939562003-08-05 15:52:22 +0000329/*
Daniel Veillard01c13b52002-12-10 15:19:08 +0000330static void
Aleksey Saninf8cb6dd2002-06-04 04:27:06 +0000331print_xpath_nodes(xmlNodeSetPtr nodes) {
Daniel Veillard044fc6b2002-03-04 17:09:44 +0000332 xmlNodePtr cur;
333 int i;
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800334
335 if(nodes == NULL ){
Daniel Veillard044fc6b2002-03-04 17:09:44 +0000336 fprintf(stderr, "Error: no nodes set defined\n");
337 return;
338 }
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800339
Aleksey Saninf8cb6dd2002-06-04 04:27:06 +0000340 fprintf(stderr, "Nodes Set:\n-----\n");
341 for(i = 0; i < nodes->nodeNr; ++i) {
342 if(nodes->nodeTab[i]->type == XML_NAMESPACE_DECL) {
343 xmlNsPtr ns;
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800344
Aleksey Saninf8cb6dd2002-06-04 04:27:06 +0000345 ns = (xmlNsPtr)nodes->nodeTab[i];
346 cur = (xmlNodePtr)ns->next;
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800347 fprintf(stderr, "namespace \"%s\"=\"%s\" for node %s:%s\n",
Aleksey Saninf8cb6dd2002-06-04 04:27:06 +0000348 ns->prefix, ns->href,
349 (cur->ns) ? cur->ns->prefix : BAD_CAST "", cur->name);
350 } else if(nodes->nodeTab[i]->type == XML_ELEMENT_NODE) {
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800351 cur = nodes->nodeTab[i];
352 fprintf(stderr, "element node \"%s:%s\"\n",
Aleksey Saninf8cb6dd2002-06-04 04:27:06 +0000353 (cur->ns) ? cur->ns->prefix : BAD_CAST "", cur->name);
354 } else {
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800355 cur = nodes->nodeTab[i];
Aleksey Saninf8cb6dd2002-06-04 04:27:06 +0000356 fprintf(stderr, "node \"%s\": type %d\n", cur->name, cur->type);
357 }
Daniel Veillard044fc6b2002-03-04 17:09:44 +0000358 }
359}
William M. Brackc1939562003-08-05 15:52:22 +0000360*/
Aleksey Saninf8cb6dd2002-06-04 04:27:06 +0000361
Daniel Veillard044fc6b2002-03-04 17:09:44 +0000362#else
363#include <stdio.h>
Daniel Veillarda9cce9c2003-09-29 13:20:24 +0000364int main(int argc ATTRIBUTE_UNUSED, char **argv ATTRIBUTE_UNUSED) {
365 printf("%s : XPath/Canonicalization and output support not compiled in\n", argv[0]);
Daniel Veillard044fc6b2002-03-04 17:09:44 +0000366 return(0);
367}
368#endif /* LIBXML_C14N_ENABLED */
369
370