blob: addc800e0be7aa01fe496ebf880b0cdd2f745d80 [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
Haibo Huangcfd91dc2020-07-30 23:01:33 -070021#ifdef _WIN32
22#include <io.h>
23#endif
Daniel Veillard044fc6b2002-03-04 17:09:44 +000024#ifdef HAVE_STDLIB_H
25#include <stdlib.h>
26#endif
27
28#include <libxml/xmlmemory.h>
29#include <libxml/parser.h>
30#include <libxml/xpath.h>
31#include <libxml/xpathInternals.h>
32
33#include <libxml/c14n.h>
34
35
36static void usage(const char *name) {
37 fprintf(stderr,
38 "Usage: %s <mode> <xml-file> [<xpath-expr>] [<inclusive-ns-list>]\n",
39 name);
40 fprintf(stderr, "where <mode> is one of following:\n");
41 fprintf(stderr,
Aleksey Sanin83868242009-07-09 10:26:22 +020042 "--with-comments \t XML file canonicalization v1.0 w comments \n");
Daniel Veillard044fc6b2002-03-04 17:09:44 +000043 fprintf(stderr,
Aleksey Sanin83868242009-07-09 10:26:22 +020044 "--without-comments \t XML file canonicalization v1.0 w/o comments\n");
Daniel Veillard044fc6b2002-03-04 17:09:44 +000045 fprintf(stderr,
Aleksey Sanin83868242009-07-09 10:26:22 +020046 "--1-1-with-comments \t XML file canonicalization v1.1 w comments\n");
Daniel Veillard044fc6b2002-03-04 17:09:44 +000047 fprintf(stderr,
Aleksey Sanin83868242009-07-09 10:26:22 +020048 "--1-1-without-comments \t XML file canonicalization v1.1 w/o comments\n");
49 fprintf(stderr,
50 "--exc-with-comments \t Exclusive XML file canonicalization v1.0 w comments\n");
51 fprintf(stderr,
52 "--exc-without-comments\t Exclusive XML file canonicalization v1.0 w/o comments\n");
Daniel Veillard044fc6b2002-03-04 17:09:44 +000053}
54
Daniel Veillardfe8aab92002-12-22 10:25:41 +000055static xmlXPathObjectPtr
Daniel Veillard044fc6b2002-03-04 17:09:44 +000056load_xpath_expr (xmlDocPtr parent_doc, const char* filename);
57
Daniel Veillardfe8aab92002-12-22 10:25:41 +000058static xmlChar **parse_list(xmlChar *str);
Daniel Veillard044fc6b2002-03-04 17:09:44 +000059
William M. Brackc1939562003-08-05 15:52:22 +000060/* static void print_xpath_nodes(xmlNodeSetPtr nodes); */
Daniel Veillard044fc6b2002-03-04 17:09:44 +000061
Daniel Veillardf8e3db02012-09-11 13:26:36 +080062static int
Aleksey Sanin83868242009-07-09 10:26:22 +020063test_c14n(const char* xml_filename, int with_comments, int mode,
Daniel Veillard044fc6b2002-03-04 17:09:44 +000064 const char* xpath_filename, xmlChar **inclusive_namespaces) {
65 xmlDocPtr doc;
Daniel Veillardf8e3db02012-09-11 13:26:36 +080066 xmlXPathObjectPtr xpath = NULL;
Daniel Veillard044fc6b2002-03-04 17:09:44 +000067 xmlChar *result = NULL;
68 int ret;
69
70 /*
71 * build an XML tree from a the file; we need to add default
72 * attributes and resolve all character and entities references
73 */
74 xmlLoadExtDtdDefaultValue = XML_DETECT_IDS | XML_COMPLETE_ATTRS;
75 xmlSubstituteEntitiesDefault(1);
76
Daniel Veillardbca3ad22005-08-23 22:14:02 +000077 doc = xmlReadFile(xml_filename, NULL, XML_PARSE_DTDATTR | XML_PARSE_NOENT);
Daniel Veillard044fc6b2002-03-04 17:09:44 +000078 if (doc == NULL) {
79 fprintf(stderr, "Error: unable to parse file \"%s\"\n", xml_filename);
80 return(-1);
81 }
Daniel Veillardf8e3db02012-09-11 13:26:36 +080082
Daniel Veillard044fc6b2002-03-04 17:09:44 +000083 /*
84 * Check the document is of the right kind
Daniel Veillardf8e3db02012-09-11 13:26:36 +080085 */
Daniel Veillard044fc6b2002-03-04 17:09:44 +000086 if(xmlDocGetRootElement(doc) == NULL) {
87 fprintf(stderr,"Error: empty document for file \"%s\"\n", xml_filename);
88 xmlFreeDoc(doc);
89 return(-1);
90 }
91
Daniel Veillardf8e3db02012-09-11 13:26:36 +080092 /*
93 * load xpath file if specified
Daniel Veillard044fc6b2002-03-04 17:09:44 +000094 */
95 if(xpath_filename) {
96 xpath = load_xpath_expr(doc, xpath_filename);
97 if(xpath == NULL) {
98 fprintf(stderr,"Error: unable to evaluate xpath expression\n");
Daniel Veillardf8e3db02012-09-11 13:26:36 +080099 xmlFreeDoc(doc);
Daniel Veillard044fc6b2002-03-04 17:09:44 +0000100 return(-1);
101 }
102 }
103
104 /*
105 * Canonical form
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800106 */
Daniel Veillard044fc6b2002-03-04 17:09:44 +0000107 /* fprintf(stderr,"File \"%s\" loaded: start canonization\n", xml_filename); */
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800108 ret = xmlC14NDocDumpMemory(doc,
109 (xpath) ? xpath->nodesetval : NULL,
Aleksey Sanin83868242009-07-09 10:26:22 +0200110 mode, inclusive_namespaces,
Daniel Veillard044fc6b2002-03-04 17:09:44 +0000111 with_comments, &result);
112 if(ret >= 0) {
113 if(result != NULL) {
Stefan Kostdff8d0f2011-05-09 12:14:59 +0300114 if (write(STDOUT_FILENO, result, ret) == -1) {
115 fprintf(stderr, "Can't write data\n");
116 }
117 xmlFree(result);
Daniel Veillard044fc6b2002-03-04 17:09:44 +0000118 }
119 } else {
120 fprintf(stderr,"Error: failed to canonicalize XML file \"%s\" (ret=%d)\n", xml_filename, ret);
121 if(result != NULL) xmlFree(result);
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800122 xmlFreeDoc(doc);
Daniel Veillard044fc6b2002-03-04 17:09:44 +0000123 return(-1);
124 }
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800125
Daniel Veillard044fc6b2002-03-04 17:09:44 +0000126 /*
127 * Cleanup
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800128 */
Daniel Veillard044fc6b2002-03-04 17:09:44 +0000129 if(xpath != NULL) xmlXPathFreeObject(xpath);
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800130 xmlFreeDoc(doc);
Daniel Veillard044fc6b2002-03-04 17:09:44 +0000131
132 return(ret);
133}
134
135int main(int argc, char **argv) {
136 int ret = -1;
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800137
Daniel Veillard044fc6b2002-03-04 17:09:44 +0000138 /*
139 * Init libxml
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800140 */
Daniel Veillard044fc6b2002-03-04 17:09:44 +0000141 xmlInitParser();
142 LIBXML_TEST_VERSION
143
144 /*
145 * Parse command line and process file
146 */
147 if( argc < 3 ) {
148 fprintf(stderr, "Error: wrong number of arguments.\n");
149 usage(argv[0]);
150 } else if(strcmp(argv[1], "--with-comments") == 0) {
Aleksey Sanin83868242009-07-09 10:26:22 +0200151 ret = test_c14n(argv[2], 1, XML_C14N_1_0, (argc > 3) ? argv[3] : NULL, NULL);
Daniel Veillard044fc6b2002-03-04 17:09:44 +0000152 } else if(strcmp(argv[1], "--without-comments") == 0) {
Aleksey Sanin83868242009-07-09 10:26:22 +0200153 ret = test_c14n(argv[2], 0, XML_C14N_1_0, (argc > 3) ? argv[3] : NULL, NULL);
154 } else if(strcmp(argv[1], "--1-1-with-comments") == 0) {
155 ret = test_c14n(argv[2], 1, XML_C14N_1_1, (argc > 3) ? argv[3] : NULL, NULL);
156 } else if(strcmp(argv[1], "--1-1-without-comments") == 0) {
157 ret = test_c14n(argv[2], 0, XML_C14N_1_1, (argc > 3) ? argv[3] : NULL, NULL);
Daniel Veillard044fc6b2002-03-04 17:09:44 +0000158 } else if(strcmp(argv[1], "--exc-with-comments") == 0) {
159 xmlChar **list;
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800160
Daniel Veillard044fc6b2002-03-04 17:09:44 +0000161 /* 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], 1, 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 if(strcmp(argv[1], "--exc-without-comments") == 0) {
166 xmlChar **list;
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800167
Daniel Veillard044fc6b2002-03-04 17:09:44 +0000168 /* load exclusive namespace from command line */
169 list = (argc > 4) ? parse_list((xmlChar *)argv[4]) : NULL;
Aleksey Sanin83868242009-07-09 10:26:22 +0200170 ret = test_c14n(argv[2], 0, XML_C14N_EXCLUSIVE_1_0, (argc > 3) ? argv[3] : NULL, list);
Daniel Veillard044fc6b2002-03-04 17:09:44 +0000171 if(list != NULL) xmlFree(list);
172 } else {
173 fprintf(stderr, "Error: bad option.\n");
174 usage(argv[0]);
175 }
176
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800177 /*
Daniel Veillard044fc6b2002-03-04 17:09:44 +0000178 * Shutdown libxml
179 */
180 xmlCleanupParser();
181 xmlMemoryDump();
Daniel Veillard13cee4e2009-09-05 14:52:55 +0200182
Daniel Veillard044fc6b2002-03-04 17:09:44 +0000183 return((ret >= 0) ? 0 : 1);
184}
185
186/*
187 * Macro used to grow the current buffer.
188 */
189#define growBufferReentrant() { \
190 buffer_size *= 2; \
191 buffer = (xmlChar **) \
Daniel Veillard13cee4e2009-09-05 14:52:55 +0200192 xmlRealloc(buffer, buffer_size * sizeof(xmlChar*)); \
Daniel Veillard044fc6b2002-03-04 17:09:44 +0000193 if (buffer == NULL) { \
194 perror("realloc failed"); \
195 return(NULL); \
196 } \
197}
198
Daniel Veillard01c13b52002-12-10 15:19:08 +0000199static xmlChar **
200parse_list(xmlChar *str) {
Daniel Veillard044fc6b2002-03-04 17:09:44 +0000201 xmlChar **buffer;
202 xmlChar **out = NULL;
203 int buffer_size = 0;
Aleksey Saninf8cb6dd2002-06-04 04:27:06 +0000204 int len;
Daniel Veillard044fc6b2002-03-04 17:09:44 +0000205
206 if(str == NULL) {
207 return(NULL);
208 }
209
Daniel Veillard118aed72002-09-24 14:13:13 +0000210 len = xmlStrlen(str);
Aleksey Saninf8cb6dd2002-06-04 04:27:06 +0000211 if((str[0] == '\'') && (str[len - 1] == '\'')) {
212 str[len - 1] = '\0';
213 str++;
Aleksey Saninf8cb6dd2002-06-04 04:27:06 +0000214 }
Daniel Veillard044fc6b2002-03-04 17:09:44 +0000215 /*
216 * allocate an translation buffer.
217 */
218 buffer_size = 1000;
219 buffer = (xmlChar **) xmlMalloc(buffer_size * sizeof(xmlChar*));
220 if (buffer == NULL) {
221 perror("malloc failed");
222 return(NULL);
223 }
224 out = buffer;
Daniel Veillard13cee4e2009-09-05 14:52:55 +0200225
Daniel Veillard044fc6b2002-03-04 17:09:44 +0000226 while(*str != '\0') {
227 if (out - buffer > buffer_size - 10) {
228 int indx = out - buffer;
229
230 growBufferReentrant();
231 out = &buffer[indx];
232 }
233 (*out++) = str;
234 while(*str != ',' && *str != '\0') ++str;
235 if(*str == ',') *(str++) = '\0';
236 }
237 (*out) = NULL;
238 return buffer;
239}
240
Daniel Veillard01c13b52002-12-10 15:19:08 +0000241static xmlXPathObjectPtr
Daniel Veillard044fc6b2002-03-04 17:09:44 +0000242load_xpath_expr (xmlDocPtr parent_doc, const char* filename) {
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800243 xmlXPathObjectPtr xpath;
Daniel Veillard044fc6b2002-03-04 17:09:44 +0000244 xmlDocPtr doc;
245 xmlChar *expr;
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800246 xmlXPathContextPtr ctx;
Daniel Veillard044fc6b2002-03-04 17:09:44 +0000247 xmlNodePtr node;
248 xmlNsPtr ns;
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800249
Daniel Veillard044fc6b2002-03-04 17:09:44 +0000250 /*
251 * load XPath expr as a file
252 */
253 xmlLoadExtDtdDefaultValue = XML_DETECT_IDS | XML_COMPLETE_ATTRS;
254 xmlSubstituteEntitiesDefault(1);
255
Daniel Veillardbca3ad22005-08-23 22:14:02 +0000256 doc = xmlReadFile(filename, NULL, XML_PARSE_DTDATTR | XML_PARSE_NOENT);
Daniel Veillard044fc6b2002-03-04 17:09:44 +0000257 if (doc == NULL) {
258 fprintf(stderr, "Error: unable to parse file \"%s\"\n", filename);
259 return(NULL);
260 }
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800261
Daniel Veillard044fc6b2002-03-04 17:09:44 +0000262 /*
263 * Check the document is of the right kind
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800264 */
Daniel Veillard044fc6b2002-03-04 17:09:44 +0000265 if(xmlDocGetRootElement(doc) == NULL) {
266 fprintf(stderr,"Error: empty document for file \"%s\"\n", filename);
267 xmlFreeDoc(doc);
268 return(NULL);
269 }
270
271 node = doc->children;
272 while(node != NULL && !xmlStrEqual(node->name, (const xmlChar *)"XPath")) {
273 node = node->next;
274 }
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800275
276 if(node == NULL) {
Daniel Veillard044fc6b2002-03-04 17:09:44 +0000277 fprintf(stderr,"Error: XPath element expected in the file \"%s\"\n", filename);
278 xmlFreeDoc(doc);
279 return(NULL);
280 }
281
282 expr = xmlNodeGetContent(node);
283 if(expr == NULL) {
284 fprintf(stderr,"Error: XPath content element is NULL \"%s\"\n", filename);
285 xmlFreeDoc(doc);
286 return(NULL);
287 }
288
289 ctx = xmlXPathNewContext(parent_doc);
290 if(ctx == NULL) {
291 fprintf(stderr,"Error: unable to create new context\n");
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800292 xmlFree(expr);
293 xmlFreeDoc(doc);
Daniel Veillard044fc6b2002-03-04 17:09:44 +0000294 return(NULL);
295 }
296
297 /*
298 * Register namespaces
299 */
300 ns = node->nsDef;
301 while(ns != NULL) {
302 if(xmlXPathRegisterNs(ctx, ns->prefix, ns->href) != 0) {
303 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 +0800304 xmlFree(expr);
305 xmlXPathFreeContext(ctx);
306 xmlFreeDoc(doc);
Daniel Veillard044fc6b2002-03-04 17:09:44 +0000307 return(NULL);
308 }
309 ns = ns->next;
310 }
311
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800312 /*
Daniel Veillard044fc6b2002-03-04 17:09:44 +0000313 * Evaluate xpath
314 */
315 xpath = xmlXPathEvalExpression(expr, ctx);
316 if(xpath == NULL) {
317 fprintf(stderr,"Error: unable to evaluate xpath expression\n");
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800318 xmlFree(expr);
319 xmlXPathFreeContext(ctx);
320 xmlFreeDoc(doc);
Daniel Veillard044fc6b2002-03-04 17:09:44 +0000321 return(NULL);
322 }
323
Aleksey Saninf8cb6dd2002-06-04 04:27:06 +0000324 /* print_xpath_nodes(xpath->nodesetval); */
Daniel Veillard044fc6b2002-03-04 17:09:44 +0000325
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800326 xmlFree(expr);
327 xmlXPathFreeContext(ctx);
328 xmlFreeDoc(doc);
Daniel Veillard044fc6b2002-03-04 17:09:44 +0000329 return(xpath);
330}
331
William M. Brackc1939562003-08-05 15:52:22 +0000332/*
Daniel Veillard01c13b52002-12-10 15:19:08 +0000333static void
Aleksey Saninf8cb6dd2002-06-04 04:27:06 +0000334print_xpath_nodes(xmlNodeSetPtr nodes) {
Daniel Veillard044fc6b2002-03-04 17:09:44 +0000335 xmlNodePtr cur;
336 int i;
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800337
338 if(nodes == NULL ){
Daniel Veillard044fc6b2002-03-04 17:09:44 +0000339 fprintf(stderr, "Error: no nodes set defined\n");
340 return;
341 }
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800342
Aleksey Saninf8cb6dd2002-06-04 04:27:06 +0000343 fprintf(stderr, "Nodes Set:\n-----\n");
344 for(i = 0; i < nodes->nodeNr; ++i) {
345 if(nodes->nodeTab[i]->type == XML_NAMESPACE_DECL) {
346 xmlNsPtr ns;
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800347
Aleksey Saninf8cb6dd2002-06-04 04:27:06 +0000348 ns = (xmlNsPtr)nodes->nodeTab[i];
349 cur = (xmlNodePtr)ns->next;
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800350 fprintf(stderr, "namespace \"%s\"=\"%s\" for node %s:%s\n",
Aleksey Saninf8cb6dd2002-06-04 04:27:06 +0000351 ns->prefix, ns->href,
352 (cur->ns) ? cur->ns->prefix : BAD_CAST "", cur->name);
353 } else if(nodes->nodeTab[i]->type == XML_ELEMENT_NODE) {
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800354 cur = nodes->nodeTab[i];
355 fprintf(stderr, "element node \"%s:%s\"\n",
Aleksey Saninf8cb6dd2002-06-04 04:27:06 +0000356 (cur->ns) ? cur->ns->prefix : BAD_CAST "", cur->name);
357 } else {
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800358 cur = nodes->nodeTab[i];
Aleksey Saninf8cb6dd2002-06-04 04:27:06 +0000359 fprintf(stderr, "node \"%s\": type %d\n", cur->name, cur->type);
360 }
Daniel Veillard044fc6b2002-03-04 17:09:44 +0000361 }
362}
William M. Brackc1939562003-08-05 15:52:22 +0000363*/
Aleksey Saninf8cb6dd2002-06-04 04:27:06 +0000364
Daniel Veillard044fc6b2002-03-04 17:09:44 +0000365#else
366#include <stdio.h>
Daniel Veillarda9cce9c2003-09-29 13:20:24 +0000367int main(int argc ATTRIBUTE_UNUSED, char **argv ATTRIBUTE_UNUSED) {
368 printf("%s : XPath/Canonicalization and output support not compiled in\n", argv[0]);
Daniel Veillard044fc6b2002-03-04 17:09:44 +0000369 return(0);
370}
371#endif /* LIBXML_C14N_ENABLED */
372
373