testC14N.c revision 38ae7e4efe803ea78b6499cd05a394db32623e41
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync/*
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync * Canonical XML implementation test program
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync * (http://www.w3.org/TR/2001/REC-xml-c14n-20010315)
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync *
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync * See Copyright for the status of this software.
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync *
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync * Author: Aleksey Sanin <aleksey@aleksey.com>
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync */
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync#include "libxml.h"
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync#if defined(LIBXML_C14N_ENABLED) && defined(LIBXML_OUTPUT_ENABLED)
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync#include <stdio.h>
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync#include <string.h>
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync#ifdef HAVE_UNISTD_H
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync#include <unistd.h>
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync#endif
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync#ifdef HAVE_STDLIB_H
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync#include <stdlib.h>
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync#endif
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync#include <libxml/xmlmemory.h>
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync#include <libxml/parser.h>
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync#include <libxml/xpath.h>
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync#include <libxml/xpathInternals.h>
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync#include <libxml/c14n.h>
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsyncstatic void usage(const char *name) {
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync fprintf(stderr,
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync "Usage: %s <mode> <xml-file> [<xpath-expr>] [<inclusive-ns-list>]\n",
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync name);
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync fprintf(stderr, "where <mode> is one of following:\n");
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync fprintf(stderr,
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync "--with-comments \t XML file canonization w comments\n");
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync fprintf(stderr,
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync "--without-comments \t XML file canonization w/o comments\n");
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync fprintf(stderr,
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync "--exc-with-comments \t Exclusive XML file canonization w comments\n");
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync fprintf(stderr,
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync "--exc-without-comments\t Exclusive XML file canonization w/o comments\n");
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync}
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsyncstatic xmlXPathObjectPtr
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsyncload_xpath_expr (xmlDocPtr parent_doc, const char* filename);
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsyncstatic xmlChar **parse_list(xmlChar *str);
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync/* static void print_xpath_nodes(xmlNodeSetPtr nodes); */
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsyncstatic int
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsynctest_c14n(const char* xml_filename, int with_comments, int exclusive,
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync const char* xpath_filename, xmlChar **inclusive_namespaces) {
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync xmlDocPtr doc;
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync xmlXPathObjectPtr xpath = NULL;
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync xmlChar *result = NULL;
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync int ret;
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync /*
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync * build an XML tree from a the file; we need to add default
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync * attributes and resolve all character and entities references
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync */
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync xmlLoadExtDtdDefaultValue = XML_DETECT_IDS | XML_COMPLETE_ATTRS;
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync xmlSubstituteEntitiesDefault(1);
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync doc = xmlReadFile(xml_filename, NULL, XML_PARSE_DTDATTR | XML_PARSE_NOENT);
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync if (doc == NULL) {
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync fprintf(stderr, "Error: unable to parse file \"%s\"\n", xml_filename);
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync return(-1);
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync }
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync /*
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync * Check the document is of the right kind
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync */
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync if(xmlDocGetRootElement(doc) == NULL) {
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync fprintf(stderr,"Error: empty document for file \"%s\"\n", xml_filename);
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync xmlFreeDoc(doc);
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync return(-1);
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync }
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync /*
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync * load xpath file if specified
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync */
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync if(xpath_filename) {
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync xpath = load_xpath_expr(doc, xpath_filename);
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync if(xpath == NULL) {
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync fprintf(stderr,"Error: unable to evaluate xpath expression\n");
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync xmlFreeDoc(doc);
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync return(-1);
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync }
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync }
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync /*
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync * Canonical form
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync */
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync /* fprintf(stderr,"File \"%s\" loaded: start canonization\n", xml_filename); */
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync ret = xmlC14NDocDumpMemory(doc,
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync (xpath) ? xpath->nodesetval : NULL,
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync exclusive, inclusive_namespaces,
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync with_comments, &result);
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync if(ret >= 0) {
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync if(result != NULL) {
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync write(1, result, ret);
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync xmlFree(result);
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync }
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync } else {
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync fprintf(stderr,"Error: failed to canonicalize XML file \"%s\" (ret=%d)\n", xml_filename, ret);
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync if(result != NULL) xmlFree(result);
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync xmlFreeDoc(doc);
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync return(-1);
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync }
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync /*
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync * Cleanup
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync */
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync if(xpath != NULL) xmlXPathFreeObject(xpath);
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync xmlFreeDoc(doc);
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync return(ret);
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync}
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsyncint main(int argc, char **argv) {
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync int ret = -1;
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync /*
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync * Init libxml
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync */
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync xmlInitParser();
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync LIBXML_TEST_VERSION
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync /*
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync * Parse command line and process file
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync */
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync if( argc < 3 ) {
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync fprintf(stderr, "Error: wrong number of arguments.\n");
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync usage(argv[0]);
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync } else if(strcmp(argv[1], "--with-comments") == 0) {
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync ret = test_c14n(argv[2], 1, 0, (argc > 3) ? argv[3] : NULL, NULL);
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync } else if(strcmp(argv[1], "--without-comments") == 0) {
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync ret = test_c14n(argv[2], 0, 0, (argc > 3) ? argv[3] : NULL, NULL);
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync } else if(strcmp(argv[1], "--exc-with-comments") == 0) {
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync xmlChar **list;
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync /* load exclusive namespace from command line */
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync list = (argc > 4) ? parse_list((xmlChar *)argv[4]) : NULL;
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync ret = test_c14n(argv[2], 1, 1, (argc > 3) ? argv[3] : NULL, list);
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync if(list != NULL) xmlFree(list);
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync } else if(strcmp(argv[1], "--exc-without-comments") == 0) {
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync xmlChar **list;
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync /* load exclusive namespace from command line */
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync list = (argc > 4) ? parse_list((xmlChar *)argv[4]) : NULL;
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync ret = test_c14n(argv[2], 0, 1, (argc > 3) ? argv[3] : NULL, list);
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync if(list != NULL) xmlFree(list);
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync } else {
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync fprintf(stderr, "Error: bad option.\n");
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync usage(argv[0]);
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync }
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync /*
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync * Shutdown libxml
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync */
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync xmlCleanupParser();
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync xmlMemoryDump();
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync return((ret >= 0) ? 0 : 1);
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync}
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync/*
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync * Macro used to grow the current buffer.
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync */
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync#define growBufferReentrant() { \
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync buffer_size *= 2; \
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync buffer = (xmlChar **) \
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync xmlRealloc(buffer, buffer_size * sizeof(xmlChar*)); \
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync if (buffer == NULL) { \
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync perror("realloc failed"); \
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync return(NULL); \
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync } \
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync}
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsyncstatic xmlChar **
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsyncparse_list(xmlChar *str) {
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync xmlChar **buffer;
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync xmlChar **out = NULL;
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync int buffer_size = 0;
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync int len;
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync if(str == NULL) {
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync return(NULL);
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync }
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync len = xmlStrlen(str);
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync if((str[0] == '\'') && (str[len - 1] == '\'')) {
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync str[len - 1] = '\0';
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync str++;
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync len -= 2;
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync }
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync /*
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync * allocate an translation buffer.
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync */
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync buffer_size = 1000;
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync buffer = (xmlChar **) xmlMalloc(buffer_size * sizeof(xmlChar*));
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync if (buffer == NULL) {
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync perror("malloc failed");
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync return(NULL);
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync }
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync out = buffer;
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync while(*str != '\0') {
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync if (out - buffer > buffer_size - 10) {
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync int indx = out - buffer;
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync growBufferReentrant();
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync out = &buffer[indx];
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync }
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync (*out++) = str;
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync while(*str != ',' && *str != '\0') ++str;
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync if(*str == ',') *(str++) = '\0';
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync }
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync (*out) = NULL;
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync return buffer;
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync}
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsyncstatic xmlXPathObjectPtr
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsyncload_xpath_expr (xmlDocPtr parent_doc, const char* filename) {
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync xmlXPathObjectPtr xpath;
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync xmlDocPtr doc;
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync xmlChar *expr;
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync xmlXPathContextPtr ctx;
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync xmlNodePtr node;
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync xmlNsPtr ns;
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync /*
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync * load XPath expr as a file
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync */
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync xmlLoadExtDtdDefaultValue = XML_DETECT_IDS | XML_COMPLETE_ATTRS;
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync xmlSubstituteEntitiesDefault(1);
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync doc = xmlReadFile(filename, NULL, XML_PARSE_DTDATTR | XML_PARSE_NOENT);
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync if (doc == NULL) {
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync fprintf(stderr, "Error: unable to parse file \"%s\"\n", filename);
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync return(NULL);
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync }
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync /*
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync * Check the document is of the right kind
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync */
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync if(xmlDocGetRootElement(doc) == NULL) {
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync fprintf(stderr,"Error: empty document for file \"%s\"\n", filename);
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync xmlFreeDoc(doc);
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync return(NULL);
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync }
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync node = doc->children;
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync while(node != NULL && !xmlStrEqual(node->name, (const xmlChar *)"XPath")) {
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync node = node->next;
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync }
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync if(node == NULL) {
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync fprintf(stderr,"Error: XPath element expected in the file \"%s\"\n", filename);
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync xmlFreeDoc(doc);
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync return(NULL);
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync }
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync expr = xmlNodeGetContent(node);
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync if(expr == NULL) {
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync fprintf(stderr,"Error: XPath content element is NULL \"%s\"\n", filename);
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync xmlFreeDoc(doc);
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync return(NULL);
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync }
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync ctx = xmlXPathNewContext(parent_doc);
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync if(ctx == NULL) {
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync fprintf(stderr,"Error: unable to create new context\n");
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync xmlFree(expr);
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync xmlFreeDoc(doc);
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync return(NULL);
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync }
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync /*
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync * Register namespaces
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync */
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync ns = node->nsDef;
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync while(ns != NULL) {
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync if(xmlXPathRegisterNs(ctx, ns->prefix, ns->href) != 0) {
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync fprintf(stderr,"Error: unable to register NS with prefix=\"%s\" and href=\"%s\"\n", ns->prefix, ns->href);
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync xmlFree(expr);
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync xmlXPathFreeContext(ctx);
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync xmlFreeDoc(doc);
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync return(NULL);
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync }
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync ns = ns->next;
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync }
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync /*
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync * Evaluate xpath
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync */
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync xpath = xmlXPathEvalExpression(expr, ctx);
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync if(xpath == NULL) {
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync fprintf(stderr,"Error: unable to evaluate xpath expression\n");
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync xmlFree(expr);
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync xmlXPathFreeContext(ctx);
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync xmlFreeDoc(doc);
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync return(NULL);
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync }
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync /* print_xpath_nodes(xpath->nodesetval); */
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync xmlFree(expr);
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync xmlXPathFreeContext(ctx);
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync xmlFreeDoc(doc);
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync return(xpath);
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync}
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync/*
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsyncstatic void
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsyncprint_xpath_nodes(xmlNodeSetPtr nodes) {
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync xmlNodePtr cur;
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync int i;
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync if(nodes == NULL ){
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync fprintf(stderr, "Error: no nodes set defined\n");
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync return;
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync }
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync fprintf(stderr, "Nodes Set:\n-----\n");
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync for(i = 0; i < nodes->nodeNr; ++i) {
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync if(nodes->nodeTab[i]->type == XML_NAMESPACE_DECL) {
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync xmlNsPtr ns;
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync ns = (xmlNsPtr)nodes->nodeTab[i];
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync cur = (xmlNodePtr)ns->next;
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync fprintf(stderr, "namespace \"%s\"=\"%s\" for node %s:%s\n",
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync ns->prefix, ns->href,
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync (cur->ns) ? cur->ns->prefix : BAD_CAST "", cur->name);
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync } else if(nodes->nodeTab[i]->type == XML_ELEMENT_NODE) {
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync cur = nodes->nodeTab[i];
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync fprintf(stderr, "element node \"%s:%s\"\n",
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync (cur->ns) ? cur->ns->prefix : BAD_CAST "", cur->name);
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync } else {
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync cur = nodes->nodeTab[i];
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync fprintf(stderr, "node \"%s\": type %d\n", cur->name, cur->type);
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync }
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync }
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync}
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync*/
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync#else
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync#include <stdio.h>
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsyncint main(int argc ATTRIBUTE_UNUSED, char **argv ATTRIBUTE_UNUSED) {
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync printf("%s : XPath/Canonicalization and output support not compiled in\n", argv[0]);
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync return(0);
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync}
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync#endif /* LIBXML_C14N_ENABLED */
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync