version 1.1.1.2, 2013/07/22 01:22:20
|
version 1.1.1.3, 2014/06/15 19:53:29
|
Line 3
|
Line 3
|
* (http://www.w3.org/TR/2001/REC-xml-c14n-20010315) |
* (http://www.w3.org/TR/2001/REC-xml-c14n-20010315) |
* |
* |
* See Copyright for the status of this software. |
* See Copyright for the status of this software. |
* | * |
* Author: Aleksey Sanin <aleksey@aleksey.com> |
* Author: Aleksey Sanin <aleksey@aleksey.com> |
*/ |
*/ |
#include "libxml.h" |
#include "libxml.h" |
Line 11
|
Line 11
|
|
|
#include <stdio.h> |
#include <stdio.h> |
#include <string.h> |
#include <string.h> |
|
#ifndef STDOUT_FILENO |
#ifdef HAVE_UNISTD_H |
#ifdef HAVE_UNISTD_H |
#include <unistd.h> |
#include <unistd.h> |
#else |
#else |
#define STDOUT_FILENO fileno(stdout) |
#define STDOUT_FILENO fileno(stdout) |
#endif /* HAVE_UNISTD_H */ |
#endif /* HAVE_UNISTD_H */ |
|
#endif |
#ifdef HAVE_STDLIB_H |
#ifdef HAVE_STDLIB_H |
#include <stdlib.h> |
#include <stdlib.h> |
#endif |
#endif |
Line 54 static xmlChar **parse_list(xmlChar *str);
|
Line 56 static xmlChar **parse_list(xmlChar *str);
|
|
|
/* static void print_xpath_nodes(xmlNodeSetPtr nodes); */ |
/* static void print_xpath_nodes(xmlNodeSetPtr nodes); */ |
|
|
static int | static int |
test_c14n(const char* xml_filename, int with_comments, int mode, |
test_c14n(const char* xml_filename, int with_comments, int mode, |
const char* xpath_filename, xmlChar **inclusive_namespaces) { |
const char* xpath_filename, xmlChar **inclusive_namespaces) { |
xmlDocPtr doc; |
xmlDocPtr doc; |
xmlXPathObjectPtr xpath = NULL; | xmlXPathObjectPtr xpath = NULL; |
xmlChar *result = NULL; |
xmlChar *result = NULL; |
int ret; |
int ret; |
|
|
Line 74 test_c14n(const char* xml_filename, int with_comments,
|
Line 76 test_c14n(const char* xml_filename, int with_comments,
|
fprintf(stderr, "Error: unable to parse file \"%s\"\n", xml_filename); |
fprintf(stderr, "Error: unable to parse file \"%s\"\n", xml_filename); |
return(-1); |
return(-1); |
} |
} |
| |
/* |
/* |
* Check the document is of the right kind |
* Check the document is of the right kind |
*/ | */ |
if(xmlDocGetRootElement(doc) == NULL) { |
if(xmlDocGetRootElement(doc) == NULL) { |
fprintf(stderr,"Error: empty document for file \"%s\"\n", xml_filename); |
fprintf(stderr,"Error: empty document for file \"%s\"\n", xml_filename); |
xmlFreeDoc(doc); |
xmlFreeDoc(doc); |
return(-1); |
return(-1); |
} |
} |
|
|
/* | /* |
* load xpath file if specified | * load xpath file if specified |
*/ |
*/ |
if(xpath_filename) { |
if(xpath_filename) { |
xpath = load_xpath_expr(doc, xpath_filename); |
xpath = load_xpath_expr(doc, xpath_filename); |
if(xpath == NULL) { |
if(xpath == NULL) { |
fprintf(stderr,"Error: unable to evaluate xpath expression\n"); |
fprintf(stderr,"Error: unable to evaluate xpath expression\n"); |
xmlFreeDoc(doc); | xmlFreeDoc(doc); |
return(-1); |
return(-1); |
} |
} |
} |
} |
|
|
/* |
/* |
* Canonical form |
* Canonical form |
*/ | */ |
/* fprintf(stderr,"File \"%s\" loaded: start canonization\n", xml_filename); */ |
/* fprintf(stderr,"File \"%s\" loaded: start canonization\n", xml_filename); */ |
ret = xmlC14NDocDumpMemory(doc, | ret = xmlC14NDocDumpMemory(doc, |
(xpath) ? xpath->nodesetval : NULL, | (xpath) ? xpath->nodesetval : NULL, |
mode, inclusive_namespaces, |
mode, inclusive_namespaces, |
with_comments, &result); |
with_comments, &result); |
if(ret >= 0) { |
if(ret >= 0) { |
Line 114 test_c14n(const char* xml_filename, int with_comments,
|
Line 116 test_c14n(const char* xml_filename, int with_comments,
|
} else { |
} else { |
fprintf(stderr,"Error: failed to canonicalize XML file \"%s\" (ret=%d)\n", xml_filename, ret); |
fprintf(stderr,"Error: failed to canonicalize XML file \"%s\" (ret=%d)\n", xml_filename, ret); |
if(result != NULL) xmlFree(result); |
if(result != NULL) xmlFree(result); |
xmlFreeDoc(doc); | xmlFreeDoc(doc); |
return(-1); |
return(-1); |
} |
} |
| |
/* |
/* |
* Cleanup |
* Cleanup |
*/ | */ |
if(xpath != NULL) xmlXPathFreeObject(xpath); |
if(xpath != NULL) xmlXPathFreeObject(xpath); |
xmlFreeDoc(doc); | xmlFreeDoc(doc); |
|
|
return(ret); |
return(ret); |
} |
} |
|
|
int main(int argc, char **argv) { |
int main(int argc, char **argv) { |
int ret = -1; |
int ret = -1; |
| |
/* |
/* |
* Init libxml |
* Init libxml |
*/ | */ |
xmlInitParser(); |
xmlInitParser(); |
LIBXML_TEST_VERSION |
LIBXML_TEST_VERSION |
|
|
Line 152 int main(int argc, char **argv) {
|
Line 154 int main(int argc, char **argv) {
|
ret = test_c14n(argv[2], 0, XML_C14N_1_1, (argc > 3) ? argv[3] : NULL, NULL); |
ret = test_c14n(argv[2], 0, XML_C14N_1_1, (argc > 3) ? argv[3] : NULL, NULL); |
} else if(strcmp(argv[1], "--exc-with-comments") == 0) { |
} else if(strcmp(argv[1], "--exc-with-comments") == 0) { |
xmlChar **list; |
xmlChar **list; |
| |
/* load exclusive namespace from command line */ |
/* load exclusive namespace from command line */ |
list = (argc > 4) ? parse_list((xmlChar *)argv[4]) : NULL; |
list = (argc > 4) ? parse_list((xmlChar *)argv[4]) : NULL; |
ret = test_c14n(argv[2], 1, XML_C14N_EXCLUSIVE_1_0, (argc > 3) ? argv[3] : NULL, list); |
ret = test_c14n(argv[2], 1, XML_C14N_EXCLUSIVE_1_0, (argc > 3) ? argv[3] : NULL, list); |
if(list != NULL) xmlFree(list); |
if(list != NULL) xmlFree(list); |
} else if(strcmp(argv[1], "--exc-without-comments") == 0) { |
} else if(strcmp(argv[1], "--exc-without-comments") == 0) { |
xmlChar **list; |
xmlChar **list; |
| |
/* load exclusive namespace from command line */ |
/* load exclusive namespace from command line */ |
list = (argc > 4) ? parse_list((xmlChar *)argv[4]) : NULL; |
list = (argc > 4) ? parse_list((xmlChar *)argv[4]) : NULL; |
ret = test_c14n(argv[2], 0, XML_C14N_EXCLUSIVE_1_0, (argc > 3) ? argv[3] : NULL, list); |
ret = test_c14n(argv[2], 0, XML_C14N_EXCLUSIVE_1_0, (argc > 3) ? argv[3] : NULL, list); |
Line 169 int main(int argc, char **argv) {
|
Line 171 int main(int argc, char **argv) {
|
usage(argv[0]); |
usage(argv[0]); |
} |
} |
|
|
/* | /* |
* Shutdown libxml |
* Shutdown libxml |
*/ |
*/ |
xmlCleanupParser(); |
xmlCleanupParser(); |
Line 235 parse_list(xmlChar *str) {
|
Line 237 parse_list(xmlChar *str) {
|
|
|
static xmlXPathObjectPtr |
static xmlXPathObjectPtr |
load_xpath_expr (xmlDocPtr parent_doc, const char* filename) { |
load_xpath_expr (xmlDocPtr parent_doc, const char* filename) { |
xmlXPathObjectPtr xpath; | xmlXPathObjectPtr xpath; |
xmlDocPtr doc; |
xmlDocPtr doc; |
xmlChar *expr; |
xmlChar *expr; |
xmlXPathContextPtr ctx; | xmlXPathContextPtr ctx; |
xmlNodePtr node; |
xmlNodePtr node; |
xmlNsPtr ns; |
xmlNsPtr ns; |
| |
/* |
/* |
* load XPath expr as a file |
* load XPath expr as a file |
*/ |
*/ |
Line 253 load_xpath_expr (xmlDocPtr parent_doc, const char* fil
|
Line 255 load_xpath_expr (xmlDocPtr parent_doc, const char* fil
|
fprintf(stderr, "Error: unable to parse file \"%s\"\n", filename); |
fprintf(stderr, "Error: unable to parse file \"%s\"\n", filename); |
return(NULL); |
return(NULL); |
} |
} |
| |
/* |
/* |
* Check the document is of the right kind |
* Check the document is of the right kind |
*/ | */ |
if(xmlDocGetRootElement(doc) == NULL) { |
if(xmlDocGetRootElement(doc) == NULL) { |
fprintf(stderr,"Error: empty document for file \"%s\"\n", filename); |
fprintf(stderr,"Error: empty document for file \"%s\"\n", filename); |
xmlFreeDoc(doc); |
xmlFreeDoc(doc); |
Line 267 load_xpath_expr (xmlDocPtr parent_doc, const char* fil
|
Line 269 load_xpath_expr (xmlDocPtr parent_doc, const char* fil
|
while(node != NULL && !xmlStrEqual(node->name, (const xmlChar *)"XPath")) { |
while(node != NULL && !xmlStrEqual(node->name, (const xmlChar *)"XPath")) { |
node = node->next; |
node = node->next; |
} |
} |
| |
if(node == NULL) { | if(node == NULL) { |
fprintf(stderr,"Error: XPath element expected in the file \"%s\"\n", filename); |
fprintf(stderr,"Error: XPath element expected in the file \"%s\"\n", filename); |
xmlFreeDoc(doc); |
xmlFreeDoc(doc); |
return(NULL); |
return(NULL); |
Line 284 load_xpath_expr (xmlDocPtr parent_doc, const char* fil
|
Line 286 load_xpath_expr (xmlDocPtr parent_doc, const char* fil
|
ctx = xmlXPathNewContext(parent_doc); |
ctx = xmlXPathNewContext(parent_doc); |
if(ctx == NULL) { |
if(ctx == NULL) { |
fprintf(stderr,"Error: unable to create new context\n"); |
fprintf(stderr,"Error: unable to create new context\n"); |
xmlFree(expr); | xmlFree(expr); |
xmlFreeDoc(doc); | xmlFreeDoc(doc); |
return(NULL); |
return(NULL); |
} |
} |
|
|
Line 296 load_xpath_expr (xmlDocPtr parent_doc, const char* fil
|
Line 298 load_xpath_expr (xmlDocPtr parent_doc, const char* fil
|
while(ns != NULL) { |
while(ns != NULL) { |
if(xmlXPathRegisterNs(ctx, ns->prefix, ns->href) != 0) { |
if(xmlXPathRegisterNs(ctx, ns->prefix, ns->href) != 0) { |
fprintf(stderr,"Error: unable to register NS with prefix=\"%s\" and href=\"%s\"\n", ns->prefix, ns->href); |
fprintf(stderr,"Error: unable to register NS with prefix=\"%s\" and href=\"%s\"\n", ns->prefix, ns->href); |
xmlFree(expr); | xmlFree(expr); |
xmlXPathFreeContext(ctx); | xmlXPathFreeContext(ctx); |
xmlFreeDoc(doc); | xmlFreeDoc(doc); |
return(NULL); |
return(NULL); |
} |
} |
ns = ns->next; |
ns = ns->next; |
} |
} |
|
|
/* | /* |
* Evaluate xpath |
* Evaluate xpath |
*/ |
*/ |
xpath = xmlXPathEvalExpression(expr, ctx); |
xpath = xmlXPathEvalExpression(expr, ctx); |
if(xpath == NULL) { |
if(xpath == NULL) { |
fprintf(stderr,"Error: unable to evaluate xpath expression\n"); |
fprintf(stderr,"Error: unable to evaluate xpath expression\n"); |
xmlFree(expr); | xmlFree(expr); |
xmlXPathFreeContext(ctx); | xmlXPathFreeContext(ctx); |
xmlFreeDoc(doc); | xmlFreeDoc(doc); |
return(NULL); |
return(NULL); |
} |
} |
|
|
/* print_xpath_nodes(xpath->nodesetval); */ |
/* print_xpath_nodes(xpath->nodesetval); */ |
|
|
xmlFree(expr); | xmlFree(expr); |
xmlXPathFreeContext(ctx); | xmlXPathFreeContext(ctx); |
xmlFreeDoc(doc); | xmlFreeDoc(doc); |
return(xpath); |
return(xpath); |
} |
} |
|
|
Line 329 static void
|
Line 331 static void
|
print_xpath_nodes(xmlNodeSetPtr nodes) { |
print_xpath_nodes(xmlNodeSetPtr nodes) { |
xmlNodePtr cur; |
xmlNodePtr cur; |
int i; |
int i; |
| |
if(nodes == NULL ){ | if(nodes == NULL ){ |
fprintf(stderr, "Error: no nodes set defined\n"); |
fprintf(stderr, "Error: no nodes set defined\n"); |
return; |
return; |
} |
} |
| |
fprintf(stderr, "Nodes Set:\n-----\n"); |
fprintf(stderr, "Nodes Set:\n-----\n"); |
for(i = 0; i < nodes->nodeNr; ++i) { |
for(i = 0; i < nodes->nodeNr; ++i) { |
if(nodes->nodeTab[i]->type == XML_NAMESPACE_DECL) { |
if(nodes->nodeTab[i]->type == XML_NAMESPACE_DECL) { |
xmlNsPtr ns; |
xmlNsPtr ns; |
| |
ns = (xmlNsPtr)nodes->nodeTab[i]; |
ns = (xmlNsPtr)nodes->nodeTab[i]; |
cur = (xmlNodePtr)ns->next; |
cur = (xmlNodePtr)ns->next; |
fprintf(stderr, "namespace \"%s\"=\"%s\" for node %s:%s\n", | fprintf(stderr, "namespace \"%s\"=\"%s\" for node %s:%s\n", |
ns->prefix, ns->href, |
ns->prefix, ns->href, |
(cur->ns) ? cur->ns->prefix : BAD_CAST "", cur->name); |
(cur->ns) ? cur->ns->prefix : BAD_CAST "", cur->name); |
} else if(nodes->nodeTab[i]->type == XML_ELEMENT_NODE) { |
} else if(nodes->nodeTab[i]->type == XML_ELEMENT_NODE) { |
cur = nodes->nodeTab[i]; | cur = nodes->nodeTab[i]; |
fprintf(stderr, "element node \"%s:%s\"\n", | fprintf(stderr, "element node \"%s:%s\"\n", |
(cur->ns) ? cur->ns->prefix : BAD_CAST "", cur->name); |
(cur->ns) ? cur->ns->prefix : BAD_CAST "", cur->name); |
} else { |
} else { |
cur = nodes->nodeTab[i]; | cur = nodes->nodeTab[i]; |
fprintf(stderr, "node \"%s\": type %d\n", cur->name, cur->type); |
fprintf(stderr, "node \"%s\": type %d\n", cur->name, cur->type); |
} |
} |
} |
} |