Blame testC14N.c

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