2 * Canonical XML implementation test program
3 * (http://www.w3.org/TR/2001/REC-xml-c14n-20010315)
5 * See Copyright for the status of this software.
7 * Author: Aleksey Sanin <aleksey@aleksey.com>
10 #if defined(LIBXML_C14N_ENABLED) && defined(LIBXML_OUTPUT_ENABLED)
17 #define STDOUT_FILENO fileno(stdout)
18 #endif /* HAVE_UNISTD_H */
23 #include <libxml/xmlmemory.h>
24 #include <libxml/parser.h>
25 #include <libxml/xpath.h>
26 #include <libxml/xpathInternals.h>
28 #include <libxml/c14n.h>
31 static void usage(const char *name) {
33 "Usage: %s <mode> <xml-file> [<xpath-expr>] [<inclusive-ns-list>]\n",
35 fprintf(stderr, "where <mode> is one of following:\n");
37 "--with-comments \t XML file canonicalization v1.0 w comments \n");
39 "--without-comments \t XML file canonicalization v1.0 w/o comments\n");
41 "--1-1-with-comments \t XML file canonicalization v1.1 w comments\n");
43 "--1-1-without-comments \t XML file canonicalization v1.1 w/o comments\n");
45 "--exc-with-comments \t Exclusive XML file canonicalization v1.0 w comments\n");
47 "--exc-without-comments\t Exclusive XML file canonicalization v1.0 w/o comments\n");
50 static xmlXPathObjectPtr
51 load_xpath_expr (xmlDocPtr parent_doc, const char* filename);
53 static xmlChar **parse_list(xmlChar *str);
55 /* static void print_xpath_nodes(xmlNodeSetPtr nodes); */
58 test_c14n(const char* xml_filename, int with_comments, int mode,
59 const char* xpath_filename, xmlChar **inclusive_namespaces) {
61 xmlXPathObjectPtr xpath = NULL;
62 xmlChar *result = NULL;
66 * build an XML tree from a the file; we need to add default
67 * attributes and resolve all character and entities references
69 xmlLoadExtDtdDefaultValue = XML_DETECT_IDS | XML_COMPLETE_ATTRS;
70 xmlSubstituteEntitiesDefault(1);
72 doc = xmlReadFile(xml_filename, NULL, XML_PARSE_DTDATTR | XML_PARSE_NOENT);
74 fprintf(stderr, "Error: unable to parse file \"%s\"\n", xml_filename);
79 * Check the document is of the right kind
81 if(xmlDocGetRootElement(doc) == NULL) {
82 fprintf(stderr,"Error: empty document for file \"%s\"\n", xml_filename);
88 * load xpath file if specified
91 xpath = load_xpath_expr(doc, xpath_filename);
93 fprintf(stderr,"Error: unable to evaluate xpath expression\n");
102 /* fprintf(stderr,"File \"%s\" loaded: start canonization\n", xml_filename); */
103 ret = xmlC14NDocDumpMemory(doc,
104 (xpath) ? xpath->nodesetval : NULL,
105 mode, inclusive_namespaces,
106 with_comments, &result);
109 if (write(STDOUT_FILENO, result, ret) == -1) {
110 fprintf(stderr, "Can't write data\n");
115 fprintf(stderr,"Error: failed to canonicalize XML file \"%s\" (ret=%d)\n", xml_filename, ret);
116 if(result != NULL) xmlFree(result);
124 if(xpath != NULL) xmlXPathFreeObject(xpath);
130 int main(int argc, char **argv) {
140 * Parse command line and process file
143 fprintf(stderr, "Error: wrong number of arguments.\n");
145 } else if(strcmp(argv[1], "--with-comments") == 0) {
146 ret = test_c14n(argv[2], 1, XML_C14N_1_0, (argc > 3) ? argv[3] : NULL, NULL);
147 } else if(strcmp(argv[1], "--without-comments") == 0) {
148 ret = test_c14n(argv[2], 0, XML_C14N_1_0, (argc > 3) ? argv[3] : NULL, NULL);
149 } else if(strcmp(argv[1], "--1-1-with-comments") == 0) {
150 ret = test_c14n(argv[2], 1, XML_C14N_1_1, (argc > 3) ? argv[3] : NULL, NULL);
151 } else if(strcmp(argv[1], "--1-1-without-comments") == 0) {
152 ret = test_c14n(argv[2], 0, XML_C14N_1_1, (argc > 3) ? argv[3] : NULL, NULL);
153 } else if(strcmp(argv[1], "--exc-with-comments") == 0) {
156 /* load exclusive namespace from command line */
157 list = (argc > 4) ? parse_list((xmlChar *)argv[4]) : NULL;
158 ret = test_c14n(argv[2], 1, XML_C14N_EXCLUSIVE_1_0, (argc > 3) ? argv[3] : NULL, list);
159 if(list != NULL) xmlFree(list);
160 } else if(strcmp(argv[1], "--exc-without-comments") == 0) {
163 /* load exclusive namespace from command line */
164 list = (argc > 4) ? parse_list((xmlChar *)argv[4]) : NULL;
165 ret = test_c14n(argv[2], 0, XML_C14N_EXCLUSIVE_1_0, (argc > 3) ? argv[3] : NULL, list);
166 if(list != NULL) xmlFree(list);
168 fprintf(stderr, "Error: bad option.\n");
178 return((ret >= 0) ? 0 : 1);
182 * Macro used to grow the current buffer.
184 #define growBufferReentrant() { \
186 buffer = (xmlChar **) \
187 xmlRealloc(buffer, buffer_size * sizeof(xmlChar*)); \
188 if (buffer == NULL) { \
189 perror("realloc failed"); \
195 parse_list(xmlChar *str) {
197 xmlChar **out = NULL;
205 len = xmlStrlen(str);
206 if((str[0] == '\'') && (str[len - 1] == '\'')) {
211 * allocate an translation buffer.
214 buffer = (xmlChar **) xmlMalloc(buffer_size * sizeof(xmlChar*));
215 if (buffer == NULL) {
216 perror("malloc failed");
221 while(*str != '\0') {
222 if (out - buffer > buffer_size - 10) {
223 int indx = out - buffer;
225 growBufferReentrant();
229 while(*str != ',' && *str != '\0') ++str;
230 if(*str == ',') *(str++) = '\0';
236 static xmlXPathObjectPtr
237 load_xpath_expr (xmlDocPtr parent_doc, const char* filename) {
238 xmlXPathObjectPtr xpath;
241 xmlXPathContextPtr ctx;
246 * load XPath expr as a file
248 xmlLoadExtDtdDefaultValue = XML_DETECT_IDS | XML_COMPLETE_ATTRS;
249 xmlSubstituteEntitiesDefault(1);
251 doc = xmlReadFile(filename, NULL, XML_PARSE_DTDATTR | XML_PARSE_NOENT);
253 fprintf(stderr, "Error: unable to parse file \"%s\"\n", filename);
258 * Check the document is of the right kind
260 if(xmlDocGetRootElement(doc) == NULL) {
261 fprintf(stderr,"Error: empty document for file \"%s\"\n", filename);
266 node = doc->children;
267 while(node != NULL && !xmlStrEqual(node->name, (const xmlChar *)"XPath")) {
272 fprintf(stderr,"Error: XPath element expected in the file \"%s\"\n", filename);
277 expr = xmlNodeGetContent(node);
279 fprintf(stderr,"Error: XPath content element is NULL \"%s\"\n", filename);
284 ctx = xmlXPathNewContext(parent_doc);
286 fprintf(stderr,"Error: unable to create new context\n");
293 * Register namespaces
297 if(xmlXPathRegisterNs(ctx, ns->prefix, ns->href) != 0) {
298 fprintf(stderr,"Error: unable to register NS with prefix=\"%s\" and href=\"%s\"\n", ns->prefix, ns->href);
300 xmlXPathFreeContext(ctx);
310 xpath = xmlXPathEvalExpression(expr, ctx);
312 fprintf(stderr,"Error: unable to evaluate xpath expression\n");
314 xmlXPathFreeContext(ctx);
319 /* print_xpath_nodes(xpath->nodesetval); */
322 xmlXPathFreeContext(ctx);
329 print_xpath_nodes(xmlNodeSetPtr nodes) {
334 fprintf(stderr, "Error: no nodes set defined\n");
338 fprintf(stderr, "Nodes Set:\n-----\n");
339 for(i = 0; i < nodes->nodeNr; ++i) {
340 if(nodes->nodeTab[i]->type == XML_NAMESPACE_DECL) {
343 ns = (xmlNsPtr)nodes->nodeTab[i];
344 cur = (xmlNodePtr)ns->next;
345 fprintf(stderr, "namespace \"%s\"=\"%s\" for node %s:%s\n",
346 ns->prefix, ns->href,
347 (cur->ns) ? cur->ns->prefix : BAD_CAST "", cur->name);
348 } else if(nodes->nodeTab[i]->type == XML_ELEMENT_NODE) {
349 cur = nodes->nodeTab[i];
350 fprintf(stderr, "element node \"%s:%s\"\n",
351 (cur->ns) ? cur->ns->prefix : BAD_CAST "", cur->name);
353 cur = nodes->nodeTab[i];
354 fprintf(stderr, "node \"%s\": type %d\n", cur->name, cur->type);
362 int main(int argc ATTRIBUTE_UNUSED, char **argv ATTRIBUTE_UNUSED) {
363 printf("%s : XPath/Canonicalization and output support not compiled in\n", argv[0]);
366 #endif /* LIBXML_C14N_ENABLED */