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)
18 #define STDOUT_FILENO fileno(stdout)
19 #endif /* HAVE_UNISTD_H */
25 #include <libxml/xmlmemory.h>
26 #include <libxml/parser.h>
27 #include <libxml/xpath.h>
28 #include <libxml/xpathInternals.h>
30 #include <libxml/c14n.h>
33 static void usage(const char *name) {
35 "Usage: %s <mode> <xml-file> [<xpath-expr>] [<inclusive-ns-list>]\n",
37 fprintf(stderr, "where <mode> is one of following:\n");
39 "--with-comments \t XML file canonicalization v1.0 w comments \n");
41 "--without-comments \t XML file canonicalization v1.0 w/o comments\n");
43 "--1-1-with-comments \t XML file canonicalization v1.1 w comments\n");
45 "--1-1-without-comments \t XML file canonicalization v1.1 w/o comments\n");
47 "--exc-with-comments \t Exclusive XML file canonicalization v1.0 w comments\n");
49 "--exc-without-comments\t Exclusive XML file canonicalization v1.0 w/o comments\n");
52 static xmlXPathObjectPtr
53 load_xpath_expr (xmlDocPtr parent_doc, const char* filename);
55 static xmlChar **parse_list(xmlChar *str);
57 /* static void print_xpath_nodes(xmlNodeSetPtr nodes); */
60 test_c14n(const char* xml_filename, int with_comments, int mode,
61 const char* xpath_filename, xmlChar **inclusive_namespaces) {
63 xmlXPathObjectPtr xpath = NULL;
64 xmlChar *result = NULL;
68 * build an XML tree from a the file; we need to add default
69 * attributes and resolve all character and entities references
71 xmlLoadExtDtdDefaultValue = XML_DETECT_IDS | XML_COMPLETE_ATTRS;
72 xmlSubstituteEntitiesDefault(1);
74 doc = xmlReadFile(xml_filename, NULL, XML_PARSE_DTDATTR | XML_PARSE_NOENT);
76 fprintf(stderr, "Error: unable to parse file \"%s\"\n", xml_filename);
81 * Check the document is of the right kind
83 if(xmlDocGetRootElement(doc) == NULL) {
84 fprintf(stderr,"Error: empty document for file \"%s\"\n", xml_filename);
90 * load xpath file if specified
93 xpath = load_xpath_expr(doc, xpath_filename);
95 fprintf(stderr,"Error: unable to evaluate xpath expression\n");
104 /* fprintf(stderr,"File \"%s\" loaded: start canonization\n", xml_filename); */
105 ret = xmlC14NDocDumpMemory(doc,
106 (xpath) ? xpath->nodesetval : NULL,
107 mode, inclusive_namespaces,
108 with_comments, &result);
111 if (write(STDOUT_FILENO, result, ret) == -1) {
112 fprintf(stderr, "Can't write data\n");
117 fprintf(stderr,"Error: failed to canonicalize XML file \"%s\" (ret=%d)\n", xml_filename, ret);
118 if(result != NULL) xmlFree(result);
126 if(xpath != NULL) xmlXPathFreeObject(xpath);
132 int main(int argc, char **argv) {
142 * Parse command line and process file
145 fprintf(stderr, "Error: wrong number of arguments.\n");
147 } else if(strcmp(argv[1], "--with-comments") == 0) {
148 ret = test_c14n(argv[2], 1, XML_C14N_1_0, (argc > 3) ? argv[3] : NULL, NULL);
149 } else if(strcmp(argv[1], "--without-comments") == 0) {
150 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);
155 } else if(strcmp(argv[1], "--exc-with-comments") == 0) {
158 /* load exclusive namespace from command line */
159 list = (argc > 4) ? parse_list((xmlChar *)argv[4]) : NULL;
160 ret = test_c14n(argv[2], 1, XML_C14N_EXCLUSIVE_1_0, (argc > 3) ? argv[3] : NULL, list);
161 if(list != NULL) xmlFree(list);
162 } else if(strcmp(argv[1], "--exc-without-comments") == 0) {
165 /* load exclusive namespace from command line */
166 list = (argc > 4) ? parse_list((xmlChar *)argv[4]) : NULL;
167 ret = test_c14n(argv[2], 0, XML_C14N_EXCLUSIVE_1_0, (argc > 3) ? argv[3] : NULL, list);
168 if(list != NULL) xmlFree(list);
170 fprintf(stderr, "Error: bad option.\n");
180 return((ret >= 0) ? 0 : 1);
184 * Macro used to grow the current buffer.
186 #define growBufferReentrant() { \
188 buffer = (xmlChar **) \
189 xmlRealloc(buffer, buffer_size * sizeof(xmlChar*)); \
190 if (buffer == NULL) { \
191 perror("realloc failed"); \
197 parse_list(xmlChar *str) {
199 xmlChar **out = NULL;
207 len = xmlStrlen(str);
208 if((str[0] == '\'') && (str[len - 1] == '\'')) {
213 * allocate an translation buffer.
216 buffer = (xmlChar **) xmlMalloc(buffer_size * sizeof(xmlChar*));
217 if (buffer == NULL) {
218 perror("malloc failed");
223 while(*str != '\0') {
224 if (out - buffer > buffer_size - 10) {
225 int indx = out - buffer;
227 growBufferReentrant();
231 while(*str != ',' && *str != '\0') ++str;
232 if(*str == ',') *(str++) = '\0';
238 static xmlXPathObjectPtr
239 load_xpath_expr (xmlDocPtr parent_doc, const char* filename) {
240 xmlXPathObjectPtr xpath;
243 xmlXPathContextPtr ctx;
248 * load XPath expr as a file
250 xmlLoadExtDtdDefaultValue = XML_DETECT_IDS | XML_COMPLETE_ATTRS;
251 xmlSubstituteEntitiesDefault(1);
253 doc = xmlReadFile(filename, NULL, XML_PARSE_DTDATTR | XML_PARSE_NOENT);
255 fprintf(stderr, "Error: unable to parse file \"%s\"\n", filename);
260 * Check the document is of the right kind
262 if(xmlDocGetRootElement(doc) == NULL) {
263 fprintf(stderr,"Error: empty document for file \"%s\"\n", filename);
268 node = doc->children;
269 while(node != NULL && !xmlStrEqual(node->name, (const xmlChar *)"XPath")) {
274 fprintf(stderr,"Error: XPath element expected in the file \"%s\"\n", filename);
279 expr = xmlNodeGetContent(node);
281 fprintf(stderr,"Error: XPath content element is NULL \"%s\"\n", filename);
286 ctx = xmlXPathNewContext(parent_doc);
288 fprintf(stderr,"Error: unable to create new context\n");
295 * Register namespaces
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);
302 xmlXPathFreeContext(ctx);
312 xpath = xmlXPathEvalExpression(expr, ctx);
314 fprintf(stderr,"Error: unable to evaluate xpath expression\n");
316 xmlXPathFreeContext(ctx);
321 /* print_xpath_nodes(xpath->nodesetval); */
324 xmlXPathFreeContext(ctx);
331 print_xpath_nodes(xmlNodeSetPtr nodes) {
336 fprintf(stderr, "Error: no nodes set defined\n");
340 fprintf(stderr, "Nodes Set:\n-----\n");
341 for(i = 0; i < nodes->nodeNr; ++i) {
342 if(nodes->nodeTab[i]->type == XML_NAMESPACE_DECL) {
345 ns = (xmlNsPtr)nodes->nodeTab[i];
346 cur = (xmlNodePtr)ns->next;
347 fprintf(stderr, "namespace \"%s\"=\"%s\" for node %s:%s\n",
348 ns->prefix, ns->href,
349 (cur->ns) ? cur->ns->prefix : BAD_CAST "", cur->name);
350 } else if(nodes->nodeTab[i]->type == XML_ELEMENT_NODE) {
351 cur = nodes->nodeTab[i];
352 fprintf(stderr, "element node \"%s:%s\"\n",
353 (cur->ns) ? cur->ns->prefix : BAD_CAST "", cur->name);
355 cur = nodes->nodeTab[i];
356 fprintf(stderr, "node \"%s\": type %d\n", cur->name, cur->type);
364 int main(int argc ATTRIBUTE_UNUSED, char **argv ATTRIBUTE_UNUSED) {
365 printf("%s : XPath/Canonicalization and output support not compiled in\n", argv[0]);
368 #endif /* LIBXML_C14N_ENABLED */