Blame xinclude.c

Packit Service a31ea6
/*
Packit Service a31ea6
 * xinclude.c : Code to implement XInclude processing
Packit Service a31ea6
 *
Packit Service a31ea6
 * World Wide Web Consortium W3C Last Call Working Draft 10 November 2003
Packit Service a31ea6
 * http://www.w3.org/TR/2003/WD-xinclude-20031110
Packit Service a31ea6
 *
Packit Service a31ea6
 * See Copyright for the status of this software.
Packit Service a31ea6
 *
Packit Service a31ea6
 * daniel@veillard.com
Packit Service a31ea6
 */
Packit Service a31ea6
Packit Service a31ea6
#define IN_LIBXML
Packit Service a31ea6
#include "libxml.h"
Packit Service a31ea6
Packit Service a31ea6
#include <string.h>
Packit Service a31ea6
#include <libxml/xmlmemory.h>
Packit Service a31ea6
#include <libxml/tree.h>
Packit Service a31ea6
#include <libxml/parser.h>
Packit Service a31ea6
#include <libxml/uri.h>
Packit Service a31ea6
#include <libxml/xpath.h>
Packit Service a31ea6
#include <libxml/xpointer.h>
Packit Service a31ea6
#include <libxml/parserInternals.h>
Packit Service a31ea6
#include <libxml/xmlerror.h>
Packit Service a31ea6
#include <libxml/encoding.h>
Packit Service a31ea6
#include <libxml/globals.h>
Packit Service a31ea6
Packit Service a31ea6
#ifdef LIBXML_XINCLUDE_ENABLED
Packit Service a31ea6
#include <libxml/xinclude.h>
Packit Service a31ea6
Packit Service a31ea6
#include "buf.h"
Packit Service a31ea6
Packit Service a31ea6
#define XINCLUDE_MAX_DEPTH 40
Packit Service a31ea6
Packit Service a31ea6
/* #define DEBUG_XINCLUDE */
Packit Service a31ea6
#ifdef DEBUG_XINCLUDE
Packit Service a31ea6
#ifdef LIBXML_DEBUG_ENABLED
Packit Service a31ea6
#include <libxml/debugXML.h>
Packit Service a31ea6
#endif
Packit Service a31ea6
#endif
Packit Service a31ea6
Packit Service a31ea6
/************************************************************************
Packit Service a31ea6
 *									*
Packit Service a31ea6
 *			XInclude context handling			*
Packit Service a31ea6
 *									*
Packit Service a31ea6
 ************************************************************************/
Packit Service a31ea6
Packit Service a31ea6
/*
Packit Service a31ea6
 * An XInclude context
Packit Service a31ea6
 */
Packit Service a31ea6
typedef xmlChar *xmlURL;
Packit Service a31ea6
Packit Service a31ea6
typedef struct _xmlXIncludeRef xmlXIncludeRef;
Packit Service a31ea6
typedef xmlXIncludeRef *xmlXIncludeRefPtr;
Packit Service a31ea6
struct _xmlXIncludeRef {
Packit Service a31ea6
    xmlChar              *URI; /* the fully resolved resource URL */
Packit Service a31ea6
    xmlChar         *fragment; /* the fragment in the URI */
Packit Service a31ea6
    xmlDocPtr		  doc; /* the parsed document */
Packit Service a31ea6
    xmlNodePtr            ref; /* the node making the reference in the source */
Packit Service a31ea6
    xmlNodePtr            inc; /* the included copy */
Packit Service a31ea6
    int                   xml; /* xml or txt */
Packit Service a31ea6
    int                 count; /* how many refs use that specific doc */
Packit Service a31ea6
    xmlXPathObjectPtr    xptr; /* the xpointer if needed */
Packit Service a31ea6
    int		      emptyFb; /* flag to show fallback empty */
Packit Service a31ea6
};
Packit Service a31ea6
Packit Service a31ea6
struct _xmlXIncludeCtxt {
Packit Service a31ea6
    xmlDocPtr             doc; /* the source document */
Packit Service a31ea6
    int               incBase; /* the first include for this document */
Packit Service a31ea6
    int                 incNr; /* number of includes */
Packit Service a31ea6
    int                incMax; /* size of includes tab */
Packit Service a31ea6
    xmlXIncludeRefPtr *incTab; /* array of included references */
Packit Service a31ea6
Packit Service a31ea6
    int                 txtNr; /* number of unparsed documents */
Packit Service a31ea6
    int                txtMax; /* size of unparsed documents tab */
Packit Service a31ea6
    xmlNodePtr        *txtTab; /* array of unparsed text nodes */
Packit Service a31ea6
    xmlURL         *txturlTab; /* array of unparsed text URLs */
Packit Service a31ea6
Packit Service a31ea6
    xmlChar *             url; /* the current URL processed */
Packit Service a31ea6
    int                 urlNr; /* number of URLs stacked */
Packit Service a31ea6
    int                urlMax; /* size of URL stack */
Packit Service a31ea6
    xmlChar *         *urlTab; /* URL stack */
Packit Service a31ea6
Packit Service a31ea6
    int              nbErrors; /* the number of errors detected */
Packit Service a31ea6
    int                legacy; /* using XINCLUDE_OLD_NS */
Packit Service a31ea6
    int            parseFlags; /* the flags used for parsing XML documents */
Packit Service a31ea6
    xmlChar *		 base; /* the current xml:base */
Packit Service a31ea6
Packit Service a31ea6
    void            *_private; /* application data */
Packit Service a31ea6
};
Packit Service a31ea6
Packit Service a31ea6
static int
Packit Service a31ea6
xmlXIncludeDoProcess(xmlXIncludeCtxtPtr ctxt, xmlDocPtr doc, xmlNodePtr tree);
Packit Service a31ea6
Packit Service a31ea6
Packit Service a31ea6
/************************************************************************
Packit Service a31ea6
 *									*
Packit Service a31ea6
 *			XInclude error handler				*
Packit Service a31ea6
 *									*
Packit Service a31ea6
 ************************************************************************/
Packit Service a31ea6
Packit Service a31ea6
/**
Packit Service a31ea6
 * xmlXIncludeErrMemory:
Packit Service a31ea6
 * @extra:  extra information
Packit Service a31ea6
 *
Packit Service a31ea6
 * Handle an out of memory condition
Packit Service a31ea6
 */
Packit Service a31ea6
static void
Packit Service a31ea6
xmlXIncludeErrMemory(xmlXIncludeCtxtPtr ctxt, xmlNodePtr node,
Packit Service a31ea6
                     const char *extra)
Packit Service a31ea6
{
Packit Service a31ea6
    if (ctxt != NULL)
Packit Service a31ea6
	ctxt->nbErrors++;
Packit Service a31ea6
    __xmlRaiseError(NULL, NULL, NULL, ctxt, node, XML_FROM_XINCLUDE,
Packit Service a31ea6
                    XML_ERR_NO_MEMORY, XML_ERR_ERROR, NULL, 0,
Packit Service a31ea6
		    extra, NULL, NULL, 0, 0,
Packit Service a31ea6
		    "Memory allocation failed : %s\n", extra);
Packit Service a31ea6
}
Packit Service a31ea6
Packit Service a31ea6
/**
Packit Service a31ea6
 * xmlXIncludeErr:
Packit Service a31ea6
 * @ctxt: the XInclude context
Packit Service a31ea6
 * @node: the context node
Packit Service a31ea6
 * @msg:  the error message
Packit Service a31ea6
 * @extra:  extra information
Packit Service a31ea6
 *
Packit Service a31ea6
 * Handle an XInclude error
Packit Service a31ea6
 */
Packit Service a31ea6
static void LIBXML_ATTR_FORMAT(4,0)
Packit Service a31ea6
xmlXIncludeErr(xmlXIncludeCtxtPtr ctxt, xmlNodePtr node, int error,
Packit Service a31ea6
               const char *msg, const xmlChar *extra)
Packit Service a31ea6
{
Packit Service a31ea6
    if (ctxt != NULL)
Packit Service a31ea6
	ctxt->nbErrors++;
Packit Service a31ea6
    __xmlRaiseError(NULL, NULL, NULL, ctxt, node, XML_FROM_XINCLUDE,
Packit Service a31ea6
                    error, XML_ERR_ERROR, NULL, 0,
Packit Service a31ea6
		    (const char *) extra, NULL, NULL, 0, 0,
Packit Service a31ea6
		    msg, (const char *) extra);
Packit Service a31ea6
}
Packit Service a31ea6
Packit Service a31ea6
#if 0
Packit Service a31ea6
/**
Packit Service a31ea6
 * xmlXIncludeWarn:
Packit Service a31ea6
 * @ctxt: the XInclude context
Packit Service a31ea6
 * @node: the context node
Packit Service a31ea6
 * @msg:  the error message
Packit Service a31ea6
 * @extra:  extra information
Packit Service a31ea6
 *
Packit Service a31ea6
 * Emit an XInclude warning.
Packit Service a31ea6
 */
Packit Service a31ea6
static void LIBXML_ATTR_FORMAT(4,0)
Packit Service a31ea6
xmlXIncludeWarn(xmlXIncludeCtxtPtr ctxt, xmlNodePtr node, int error,
Packit Service a31ea6
               const char *msg, const xmlChar *extra)
Packit Service a31ea6
{
Packit Service a31ea6
    __xmlRaiseError(NULL, NULL, NULL, ctxt, node, XML_FROM_XINCLUDE,
Packit Service a31ea6
                    error, XML_ERR_WARNING, NULL, 0,
Packit Service a31ea6
		    (const char *) extra, NULL, NULL, 0, 0,
Packit Service a31ea6
		    msg, (const char *) extra);
Packit Service a31ea6
}
Packit Service a31ea6
#endif
Packit Service a31ea6
Packit Service a31ea6
/**
Packit Service a31ea6
 * xmlXIncludeGetProp:
Packit Service a31ea6
 * @ctxt:  the XInclude context
Packit Service a31ea6
 * @cur:  the node
Packit Service a31ea6
 * @name:  the attribute name
Packit Service a31ea6
 *
Packit Service a31ea6
 * Get an XInclude attribute
Packit Service a31ea6
 *
Packit Service a31ea6
 * Returns the value (to be freed) or NULL if not found
Packit Service a31ea6
 */
Packit Service a31ea6
static xmlChar *
Packit Service a31ea6
xmlXIncludeGetProp(xmlXIncludeCtxtPtr ctxt, xmlNodePtr cur,
Packit Service a31ea6
                   const xmlChar *name) {
Packit Service a31ea6
    xmlChar *ret;
Packit Service a31ea6
Packit Service a31ea6
    ret = xmlGetNsProp(cur, XINCLUDE_NS, name);
Packit Service a31ea6
    if (ret != NULL)
Packit Service a31ea6
        return(ret);
Packit Service a31ea6
    if (ctxt->legacy != 0) {
Packit Service a31ea6
	ret = xmlGetNsProp(cur, XINCLUDE_OLD_NS, name);
Packit Service a31ea6
	if (ret != NULL)
Packit Service a31ea6
	    return(ret);
Packit Service a31ea6
    }
Packit Service a31ea6
    ret = xmlGetProp(cur, name);
Packit Service a31ea6
    return(ret);
Packit Service a31ea6
}
Packit Service a31ea6
/**
Packit Service a31ea6
 * xmlXIncludeFreeRef:
Packit Service a31ea6
 * @ref: the XInclude reference
Packit Service a31ea6
 *
Packit Service a31ea6
 * Free an XInclude reference
Packit Service a31ea6
 */
Packit Service a31ea6
static void
Packit Service a31ea6
xmlXIncludeFreeRef(xmlXIncludeRefPtr ref) {
Packit Service a31ea6
    if (ref == NULL)
Packit Service a31ea6
	return;
Packit Service a31ea6
#ifdef DEBUG_XINCLUDE
Packit Service a31ea6
    xmlGenericError(xmlGenericErrorContext, "Freeing ref\n");
Packit Service a31ea6
#endif
Packit Service a31ea6
    if (ref->doc != NULL) {
Packit Service a31ea6
#ifdef DEBUG_XINCLUDE
Packit Service a31ea6
	xmlGenericError(xmlGenericErrorContext, "Freeing doc %s\n", ref->URI);
Packit Service a31ea6
#endif
Packit Service a31ea6
	xmlFreeDoc(ref->doc);
Packit Service a31ea6
    }
Packit Service a31ea6
    if (ref->URI != NULL)
Packit Service a31ea6
	xmlFree(ref->URI);
Packit Service a31ea6
    if (ref->fragment != NULL)
Packit Service a31ea6
	xmlFree(ref->fragment);
Packit Service a31ea6
    if (ref->xptr != NULL)
Packit Service a31ea6
	xmlXPathFreeObject(ref->xptr);
Packit Service a31ea6
    xmlFree(ref);
Packit Service a31ea6
}
Packit Service a31ea6
Packit Service a31ea6
/**
Packit Service a31ea6
 * xmlXIncludeNewRef:
Packit Service a31ea6
 * @ctxt: the XInclude context
Packit Service a31ea6
 * @URI:  the resource URI
Packit Service a31ea6
 *
Packit Service a31ea6
 * Creates a new reference within an XInclude context
Packit Service a31ea6
 *
Packit Service a31ea6
 * Returns the new set
Packit Service a31ea6
 */
Packit Service a31ea6
static xmlXIncludeRefPtr
Packit Service a31ea6
xmlXIncludeNewRef(xmlXIncludeCtxtPtr ctxt, const xmlChar *URI,
Packit Service a31ea6
	          xmlNodePtr ref) {
Packit Service a31ea6
    xmlXIncludeRefPtr ret;
Packit Service a31ea6
Packit Service a31ea6
#ifdef DEBUG_XINCLUDE
Packit Service a31ea6
    xmlGenericError(xmlGenericErrorContext, "New ref %s\n", URI);
Packit Service a31ea6
#endif
Packit Service a31ea6
    ret = (xmlXIncludeRefPtr) xmlMalloc(sizeof(xmlXIncludeRef));
Packit Service a31ea6
    if (ret == NULL) {
Packit Service a31ea6
        xmlXIncludeErrMemory(ctxt, ref, "growing XInclude context");
Packit Service a31ea6
	return(NULL);
Packit Service a31ea6
    }
Packit Service a31ea6
    memset(ret, 0, sizeof(xmlXIncludeRef));
Packit Service a31ea6
    if (URI == NULL)
Packit Service a31ea6
	ret->URI = NULL;
Packit Service a31ea6
    else
Packit Service a31ea6
	ret->URI = xmlStrdup(URI);
Packit Service a31ea6
    ret->fragment = NULL;
Packit Service a31ea6
    ret->ref = ref;
Packit Service a31ea6
    ret->doc = NULL;
Packit Service a31ea6
    ret->count = 0;
Packit Service a31ea6
    ret->xml = 0;
Packit Service a31ea6
    ret->inc = NULL;
Packit Service a31ea6
    if (ctxt->incMax == 0) {
Packit Service a31ea6
	ctxt->incMax = 4;
Packit Service a31ea6
        ctxt->incTab = (xmlXIncludeRefPtr *) xmlMalloc(ctxt->incMax *
Packit Service a31ea6
					      sizeof(ctxt->incTab[0]));
Packit Service a31ea6
        if (ctxt->incTab == NULL) {
Packit Service a31ea6
	    xmlXIncludeErrMemory(ctxt, ref, "growing XInclude context");
Packit Service a31ea6
	    xmlXIncludeFreeRef(ret);
Packit Service a31ea6
	    return(NULL);
Packit Service a31ea6
	}
Packit Service a31ea6
    }
Packit Service a31ea6
    if (ctxt->incNr >= ctxt->incMax) {
Packit Service a31ea6
	ctxt->incMax *= 2;
Packit Service a31ea6
        ctxt->incTab = (xmlXIncludeRefPtr *) xmlRealloc(ctxt->incTab,
Packit Service a31ea6
	             ctxt->incMax * sizeof(ctxt->incTab[0]));
Packit Service a31ea6
        if (ctxt->incTab == NULL) {
Packit Service a31ea6
	    xmlXIncludeErrMemory(ctxt, ref, "growing XInclude context");
Packit Service a31ea6
	    xmlXIncludeFreeRef(ret);
Packit Service a31ea6
	    return(NULL);
Packit Service a31ea6
	}
Packit Service a31ea6
    }
Packit Service a31ea6
    ctxt->incTab[ctxt->incNr++] = ret;
Packit Service a31ea6
    return(ret);
Packit Service a31ea6
}
Packit Service a31ea6
Packit Service a31ea6
/**
Packit Service a31ea6
 * xmlXIncludeNewContext:
Packit Service a31ea6
 * @doc:  an XML Document
Packit Service a31ea6
 *
Packit Service a31ea6
 * Creates a new XInclude context
Packit Service a31ea6
 *
Packit Service a31ea6
 * Returns the new set
Packit Service a31ea6
 */
Packit Service a31ea6
xmlXIncludeCtxtPtr
Packit Service a31ea6
xmlXIncludeNewContext(xmlDocPtr doc) {
Packit Service a31ea6
    xmlXIncludeCtxtPtr ret;
Packit Service a31ea6
Packit Service a31ea6
#ifdef DEBUG_XINCLUDE
Packit Service a31ea6
    xmlGenericError(xmlGenericErrorContext, "New context\n");
Packit Service a31ea6
#endif
Packit Service a31ea6
    if (doc == NULL)
Packit Service a31ea6
	return(NULL);
Packit Service a31ea6
    ret = (xmlXIncludeCtxtPtr) xmlMalloc(sizeof(xmlXIncludeCtxt));
Packit Service a31ea6
    if (ret == NULL) {
Packit Service a31ea6
	xmlXIncludeErrMemory(NULL, (xmlNodePtr) doc,
Packit Service a31ea6
	                     "creating XInclude context");
Packit Service a31ea6
	return(NULL);
Packit Service a31ea6
    }
Packit Service a31ea6
    memset(ret, 0, sizeof(xmlXIncludeCtxt));
Packit Service a31ea6
    ret->doc = doc;
Packit Service a31ea6
    ret->incNr = 0;
Packit Service a31ea6
    ret->incBase = 0;
Packit Service a31ea6
    ret->incMax = 0;
Packit Service a31ea6
    ret->incTab = NULL;
Packit Service a31ea6
    ret->nbErrors = 0;
Packit Service a31ea6
    return(ret);
Packit Service a31ea6
}
Packit Service a31ea6
Packit Service a31ea6
/**
Packit Service a31ea6
 * xmlXIncludeURLPush:
Packit Service a31ea6
 * @ctxt:  the parser context
Packit Service a31ea6
 * @value:  the url
Packit Service a31ea6
 *
Packit Service a31ea6
 * Pushes a new url on top of the url stack
Packit Service a31ea6
 *
Packit Service a31ea6
 * Returns -1 in case of error, the index in the stack otherwise
Packit Service a31ea6
 */
Packit Service a31ea6
static int
Packit Service a31ea6
xmlXIncludeURLPush(xmlXIncludeCtxtPtr ctxt,
Packit Service a31ea6
	           const xmlChar *value)
Packit Service a31ea6
{
Packit Service a31ea6
    if (ctxt->urlNr > XINCLUDE_MAX_DEPTH) {
Packit Service a31ea6
	xmlXIncludeErr(ctxt, NULL, XML_XINCLUDE_RECURSION,
Packit Service a31ea6
	               "detected a recursion in %s\n", value);
Packit Service a31ea6
	return(-1);
Packit Service a31ea6
    }
Packit Service a31ea6
    if (ctxt->urlTab == NULL) {
Packit Service a31ea6
	ctxt->urlMax = 4;
Packit Service a31ea6
	ctxt->urlNr = 0;
Packit Service a31ea6
	ctxt->urlTab = (xmlChar * *) xmlMalloc(
Packit Service a31ea6
		        ctxt->urlMax * sizeof(ctxt->urlTab[0]));
Packit Service a31ea6
        if (ctxt->urlTab == NULL) {
Packit Service a31ea6
	    xmlXIncludeErrMemory(ctxt, NULL, "adding URL");
Packit Service a31ea6
            return (-1);
Packit Service a31ea6
        }
Packit Service a31ea6
    }
Packit Service a31ea6
    if (ctxt->urlNr >= ctxt->urlMax) {
Packit Service a31ea6
        ctxt->urlMax *= 2;
Packit Service a31ea6
        ctxt->urlTab =
Packit Service a31ea6
            (xmlChar * *) xmlRealloc(ctxt->urlTab,
Packit Service a31ea6
                                      ctxt->urlMax *
Packit Service a31ea6
                                      sizeof(ctxt->urlTab[0]));
Packit Service a31ea6
        if (ctxt->urlTab == NULL) {
Packit Service a31ea6
	    xmlXIncludeErrMemory(ctxt, NULL, "adding URL");
Packit Service a31ea6
            return (-1);
Packit Service a31ea6
        }
Packit Service a31ea6
    }
Packit Service a31ea6
    ctxt->url = ctxt->urlTab[ctxt->urlNr] = xmlStrdup(value);
Packit Service a31ea6
    return (ctxt->urlNr++);
Packit Service a31ea6
}
Packit Service a31ea6
Packit Service a31ea6
/**
Packit Service a31ea6
 * xmlXIncludeURLPop:
Packit Service a31ea6
 * @ctxt: the parser context
Packit Service a31ea6
 *
Packit Service a31ea6
 * Pops the top URL from the URL stack
Packit Service a31ea6
 */
Packit Service a31ea6
static void
Packit Service a31ea6
xmlXIncludeURLPop(xmlXIncludeCtxtPtr ctxt)
Packit Service a31ea6
{
Packit Service a31ea6
    xmlChar * ret;
Packit Service a31ea6
Packit Service a31ea6
    if (ctxt->urlNr <= 0)
Packit Service a31ea6
        return;
Packit Service a31ea6
    ctxt->urlNr--;
Packit Service a31ea6
    if (ctxt->urlNr > 0)
Packit Service a31ea6
        ctxt->url = ctxt->urlTab[ctxt->urlNr - 1];
Packit Service a31ea6
    else
Packit Service a31ea6
        ctxt->url = NULL;
Packit Service a31ea6
    ret = ctxt->urlTab[ctxt->urlNr];
Packit Service a31ea6
    ctxt->urlTab[ctxt->urlNr] = NULL;
Packit Service a31ea6
    if (ret != NULL)
Packit Service a31ea6
	xmlFree(ret);
Packit Service a31ea6
}
Packit Service a31ea6
Packit Service a31ea6
/**
Packit Service a31ea6
 * xmlXIncludeFreeContext:
Packit Service a31ea6
 * @ctxt: the XInclude context
Packit Service a31ea6
 *
Packit Service a31ea6
 * Free an XInclude context
Packit Service a31ea6
 */
Packit Service a31ea6
void
Packit Service a31ea6
xmlXIncludeFreeContext(xmlXIncludeCtxtPtr ctxt) {
Packit Service a31ea6
    int i;
Packit Service a31ea6
Packit Service a31ea6
#ifdef DEBUG_XINCLUDE
Packit Service a31ea6
    xmlGenericError(xmlGenericErrorContext, "Freeing context\n");
Packit Service a31ea6
#endif
Packit Service a31ea6
    if (ctxt == NULL)
Packit Service a31ea6
	return;
Packit Service a31ea6
    while (ctxt->urlNr > 0)
Packit Service a31ea6
	xmlXIncludeURLPop(ctxt);
Packit Service a31ea6
    if (ctxt->urlTab != NULL)
Packit Service a31ea6
	xmlFree(ctxt->urlTab);
Packit Service a31ea6
    for (i = 0;i < ctxt->incNr;i++) {
Packit Service a31ea6
	if (ctxt->incTab[i] != NULL)
Packit Service a31ea6
	    xmlXIncludeFreeRef(ctxt->incTab[i]);
Packit Service a31ea6
    }
Packit Service a31ea6
    if (ctxt->txturlTab != NULL) {
Packit Service a31ea6
	for (i = 0;i < ctxt->txtNr;i++) {
Packit Service a31ea6
	    if (ctxt->txturlTab[i] != NULL)
Packit Service a31ea6
		xmlFree(ctxt->txturlTab[i]);
Packit Service a31ea6
	}
Packit Service a31ea6
    }
Packit Service a31ea6
    if (ctxt->incTab != NULL)
Packit Service a31ea6
	xmlFree(ctxt->incTab);
Packit Service a31ea6
    if (ctxt->txtTab != NULL)
Packit Service a31ea6
	xmlFree(ctxt->txtTab);
Packit Service a31ea6
    if (ctxt->txturlTab != NULL)
Packit Service a31ea6
	xmlFree(ctxt->txturlTab);
Packit Service a31ea6
    if (ctxt->base != NULL) {
Packit Service a31ea6
        xmlFree(ctxt->base);
Packit Service a31ea6
    }
Packit Service a31ea6
    xmlFree(ctxt);
Packit Service a31ea6
}
Packit Service a31ea6
Packit Service a31ea6
/**
Packit Service a31ea6
 * xmlXIncludeParseFile:
Packit Service a31ea6
 * @ctxt:  the XInclude context
Packit Service a31ea6
 * @URL:  the URL or file path
Packit Service a31ea6
 *
Packit Service a31ea6
 * parse a document for XInclude
Packit Service a31ea6
 */
Packit Service a31ea6
static xmlDocPtr
Packit Service a31ea6
xmlXIncludeParseFile(xmlXIncludeCtxtPtr ctxt, const char *URL) {
Packit Service a31ea6
    xmlDocPtr ret;
Packit Service a31ea6
    xmlParserCtxtPtr pctxt;
Packit Service a31ea6
    xmlParserInputPtr inputStream;
Packit Service a31ea6
Packit Service a31ea6
    xmlInitParser();
Packit Service a31ea6
Packit Service a31ea6
    pctxt = xmlNewParserCtxt();
Packit Service a31ea6
    if (pctxt == NULL) {
Packit Service a31ea6
	xmlXIncludeErrMemory(ctxt, NULL, "cannot allocate parser context");
Packit Service a31ea6
	return(NULL);
Packit Service a31ea6
    }
Packit Service a31ea6
Packit Service a31ea6
    /*
Packit Service a31ea6
     * pass in the application data to the parser context.
Packit Service a31ea6
     */
Packit Service a31ea6
    pctxt->_private = ctxt->_private;
Packit Service a31ea6
Packit Service a31ea6
    /*
Packit Service a31ea6
     * try to ensure that new documents included are actually
Packit Service a31ea6
     * built with the same dictionary as the including document.
Packit Service a31ea6
     */
Packit Service a31ea6
    if ((ctxt->doc != NULL) && (ctxt->doc->dict != NULL)) {
Packit Service a31ea6
       if (pctxt->dict != NULL)
Packit Service a31ea6
            xmlDictFree(pctxt->dict);
Packit Service a31ea6
	pctxt->dict = ctxt->doc->dict;
Packit Service a31ea6
	xmlDictReference(pctxt->dict);
Packit Service a31ea6
    }
Packit Service a31ea6
Packit Service a31ea6
    xmlCtxtUseOptions(pctxt, ctxt->parseFlags | XML_PARSE_DTDLOAD);
Packit Service a31ea6
Packit Service a31ea6
    inputStream = xmlLoadExternalEntity(URL, NULL, pctxt);
Packit Service a31ea6
    if (inputStream == NULL) {
Packit Service a31ea6
	xmlFreeParserCtxt(pctxt);
Packit Service a31ea6
	return(NULL);
Packit Service a31ea6
    }
Packit Service a31ea6
Packit Service a31ea6
    inputPush(pctxt, inputStream);
Packit Service a31ea6
Packit Service a31ea6
    if (pctxt->directory == NULL)
Packit Service a31ea6
        pctxt->directory = xmlParserGetDirectory(URL);
Packit Service a31ea6
Packit Service a31ea6
    pctxt->loadsubset |= XML_DETECT_IDS;
Packit Service a31ea6
Packit Service a31ea6
    xmlParseDocument(pctxt);
Packit Service a31ea6
Packit Service a31ea6
    if (pctxt->wellFormed) {
Packit Service a31ea6
        ret = pctxt->myDoc;
Packit Service a31ea6
    }
Packit Service a31ea6
    else {
Packit Service a31ea6
        ret = NULL;
Packit Service a31ea6
	if (pctxt->myDoc != NULL)
Packit Service a31ea6
	    xmlFreeDoc(pctxt->myDoc);
Packit Service a31ea6
        pctxt->myDoc = NULL;
Packit Service a31ea6
    }
Packit Service a31ea6
    xmlFreeParserCtxt(pctxt);
Packit Service a31ea6
Packit Service a31ea6
    return(ret);
Packit Service a31ea6
}
Packit Service a31ea6
Packit Service a31ea6
/**
Packit Service a31ea6
 * xmlXIncludeAddNode:
Packit Service a31ea6
 * @ctxt:  the XInclude context
Packit Service a31ea6
 * @cur:  the new node
Packit Service a31ea6
 *
Packit Service a31ea6
 * Add a new node to process to an XInclude context
Packit Service a31ea6
 */
Packit Service a31ea6
static int
Packit Service a31ea6
xmlXIncludeAddNode(xmlXIncludeCtxtPtr ctxt, xmlNodePtr cur) {
Packit Service a31ea6
    xmlXIncludeRefPtr ref;
Packit Service a31ea6
    xmlURIPtr uri;
Packit Service a31ea6
    xmlChar *URL;
Packit Service a31ea6
    xmlChar *fragment = NULL;
Packit Service a31ea6
    xmlChar *href;
Packit Service a31ea6
    xmlChar *parse;
Packit Service a31ea6
    xmlChar *base;
Packit Service a31ea6
    xmlChar *URI;
Packit Service a31ea6
    int xml = 1, i; /* default Issue 64 */
Packit Service a31ea6
    int local = 0;
Packit Service a31ea6
Packit Service a31ea6
Packit Service a31ea6
    if (ctxt == NULL)
Packit Service a31ea6
	return(-1);
Packit Service a31ea6
    if (cur == NULL)
Packit Service a31ea6
	return(-1);
Packit Service a31ea6
Packit Service a31ea6
#ifdef DEBUG_XINCLUDE
Packit Service a31ea6
    xmlGenericError(xmlGenericErrorContext, "Add node\n");
Packit Service a31ea6
#endif
Packit Service a31ea6
    /*
Packit Service a31ea6
     * read the attributes
Packit Service a31ea6
     */
Packit Service a31ea6
    href = xmlXIncludeGetProp(ctxt, cur, XINCLUDE_HREF);
Packit Service a31ea6
    if (href == NULL) {
Packit Service a31ea6
	href = xmlStrdup(BAD_CAST ""); /* @@@@ href is now optional */
Packit Service a31ea6
	if (href == NULL)
Packit Service a31ea6
	    return(-1);
Packit Service a31ea6
    }
Packit Service a31ea6
    if ((href[0] == '#') || (href[0] == 0))
Packit Service a31ea6
	local = 1;
Packit Service a31ea6
    parse = xmlXIncludeGetProp(ctxt, cur, XINCLUDE_PARSE);
Packit Service a31ea6
    if (parse != NULL) {
Packit Service a31ea6
	if (xmlStrEqual(parse, XINCLUDE_PARSE_XML))
Packit Service a31ea6
	    xml = 1;
Packit Service a31ea6
	else if (xmlStrEqual(parse, XINCLUDE_PARSE_TEXT))
Packit Service a31ea6
	    xml = 0;
Packit Service a31ea6
	else {
Packit Service a31ea6
	    xmlXIncludeErr(ctxt, cur, XML_XINCLUDE_PARSE_VALUE,
Packit Service a31ea6
	                   "invalid value %s for 'parse'\n", parse);
Packit Service a31ea6
	    if (href != NULL)
Packit Service a31ea6
		xmlFree(href);
Packit Service a31ea6
	    if (parse != NULL)
Packit Service a31ea6
		xmlFree(parse);
Packit Service a31ea6
	    return(-1);
Packit Service a31ea6
	}
Packit Service a31ea6
    }
Packit Service a31ea6
Packit Service a31ea6
    /*
Packit Service a31ea6
     * compute the URI
Packit Service a31ea6
     */
Packit Service a31ea6
    base = xmlNodeGetBase(ctxt->doc, cur);
Packit Service a31ea6
    if (base == NULL) {
Packit Service a31ea6
	URI = xmlBuildURI(href, ctxt->doc->URL);
Packit Service a31ea6
    } else {
Packit Service a31ea6
	URI = xmlBuildURI(href, base);
Packit Service a31ea6
    }
Packit Service a31ea6
    if (URI == NULL) {
Packit Service a31ea6
	xmlChar *escbase;
Packit Service a31ea6
	xmlChar *eschref;
Packit Service a31ea6
	/*
Packit Service a31ea6
	 * Some escaping may be needed
Packit Service a31ea6
	 */
Packit Service a31ea6
	escbase = xmlURIEscape(base);
Packit Service a31ea6
	eschref = xmlURIEscape(href);
Packit Service a31ea6
	URI = xmlBuildURI(eschref, escbase);
Packit Service a31ea6
	if (escbase != NULL)
Packit Service a31ea6
	    xmlFree(escbase);
Packit Service a31ea6
	if (eschref != NULL)
Packit Service a31ea6
	    xmlFree(eschref);
Packit Service a31ea6
    }
Packit Service a31ea6
    if (parse != NULL)
Packit Service a31ea6
	xmlFree(parse);
Packit Service a31ea6
    if (href != NULL)
Packit Service a31ea6
	xmlFree(href);
Packit Service a31ea6
    if (base != NULL)
Packit Service a31ea6
	xmlFree(base);
Packit Service a31ea6
    if (URI == NULL) {
Packit Service a31ea6
	xmlXIncludeErr(ctxt, cur, XML_XINCLUDE_HREF_URI,
Packit Service a31ea6
	               "failed build URL\n", NULL);
Packit Service a31ea6
	return(-1);
Packit Service a31ea6
    }
Packit Service a31ea6
    fragment = xmlXIncludeGetProp(ctxt, cur, XINCLUDE_PARSE_XPOINTER);
Packit Service a31ea6
Packit Service a31ea6
    /*
Packit Service a31ea6
     * Check the URL and remove any fragment identifier
Packit Service a31ea6
     */
Packit Service a31ea6
    uri = xmlParseURI((const char *)URI);
Packit Service a31ea6
    if (uri == NULL) {
Packit Service a31ea6
	xmlXIncludeErr(ctxt, cur, XML_XINCLUDE_HREF_URI,
Packit Service a31ea6
	               "invalid value URI %s\n", URI);
Packit Service a31ea6
	if (fragment != NULL)
Packit Service a31ea6
	    xmlFree(fragment);
Packit Service a31ea6
	xmlFree(URI);
Packit Service a31ea6
	return(-1);
Packit Service a31ea6
    }
Packit Service a31ea6
Packit Service a31ea6
    if (uri->fragment != NULL) {
Packit Service a31ea6
        if (ctxt->legacy != 0) {
Packit Service a31ea6
	    if (fragment == NULL) {
Packit Service a31ea6
		fragment = (xmlChar *) uri->fragment;
Packit Service a31ea6
	    } else {
Packit Service a31ea6
		xmlFree(uri->fragment);
Packit Service a31ea6
	    }
Packit Service a31ea6
	} else {
Packit Service a31ea6
	    xmlXIncludeErr(ctxt, cur, XML_XINCLUDE_FRAGMENT_ID,
Packit Service a31ea6
       "Invalid fragment identifier in URI %s use the xpointer attribute\n",
Packit Service a31ea6
                           URI);
Packit Service a31ea6
	    if (fragment != NULL)
Packit Service a31ea6
	        xmlFree(fragment);
Packit Service a31ea6
	    xmlFreeURI(uri);
Packit Service a31ea6
	    xmlFree(URI);
Packit Service a31ea6
	    return(-1);
Packit Service a31ea6
	}
Packit Service a31ea6
	uri->fragment = NULL;
Packit Service a31ea6
    }
Packit Service a31ea6
    URL = xmlSaveUri(uri);
Packit Service a31ea6
    xmlFreeURI(uri);
Packit Service a31ea6
    xmlFree(URI);
Packit Service a31ea6
    if (URL == NULL) {
Packit Service a31ea6
	xmlXIncludeErr(ctxt, cur, XML_XINCLUDE_HREF_URI,
Packit Service a31ea6
	               "invalid value URI %s\n", URI);
Packit Service a31ea6
	if (fragment != NULL)
Packit Service a31ea6
	    xmlFree(fragment);
Packit Service a31ea6
	return(-1);
Packit Service a31ea6
    }
Packit Service a31ea6
Packit Service a31ea6
    /*
Packit Service a31ea6
     * If local and xml then we need a fragment
Packit Service a31ea6
     */
Packit Service a31ea6
    if ((local == 1) && (xml == 1) &&
Packit Service a31ea6
        ((fragment == NULL) || (fragment[0] == 0))) {
Packit Service a31ea6
	xmlXIncludeErr(ctxt, cur, XML_XINCLUDE_RECURSION,
Packit Service a31ea6
	               "detected a local recursion with no xpointer in %s\n",
Packit Service a31ea6
		       URL);
Packit Service a31ea6
	if (fragment != NULL)
Packit Service a31ea6
	    xmlFree(fragment);
Packit Service a31ea6
	return(-1);
Packit Service a31ea6
    }
Packit Service a31ea6
Packit Service a31ea6
    /*
Packit Service a31ea6
     * Check the URL against the stack for recursions
Packit Service a31ea6
     */
Packit Service a31ea6
    if ((!local) && (xml == 1)) {
Packit Service a31ea6
	for (i = 0;i < ctxt->urlNr;i++) {
Packit Service a31ea6
	    if (xmlStrEqual(URL, ctxt->urlTab[i])) {
Packit Service a31ea6
		xmlXIncludeErr(ctxt, cur, XML_XINCLUDE_RECURSION,
Packit Service a31ea6
		               "detected a recursion in %s\n", URL);
Packit Service a31ea6
		return(-1);
Packit Service a31ea6
	    }
Packit Service a31ea6
	}
Packit Service a31ea6
    }
Packit Service a31ea6
Packit Service a31ea6
    ref = xmlXIncludeNewRef(ctxt, URL, cur);
Packit Service a31ea6
    if (ref == NULL) {
Packit Service a31ea6
	return(-1);
Packit Service a31ea6
    }
Packit Service a31ea6
    ref->fragment = fragment;
Packit Service a31ea6
    ref->doc = NULL;
Packit Service a31ea6
    ref->xml = xml;
Packit Service a31ea6
    ref->count = 1;
Packit Service a31ea6
    xmlFree(URL);
Packit Service a31ea6
    return(0);
Packit Service a31ea6
}
Packit Service a31ea6
Packit Service a31ea6
/**
Packit Service a31ea6
 * xmlXIncludeRecurseDoc:
Packit Service a31ea6
 * @ctxt:  the XInclude context
Packit Service a31ea6
 * @doc:  the new document
Packit Service a31ea6
 * @url:  the associated URL
Packit Service a31ea6
 *
Packit Service a31ea6
 * The XInclude recursive nature is handled at this point.
Packit Service a31ea6
 */
Packit Service a31ea6
static void
Packit Service a31ea6
xmlXIncludeRecurseDoc(xmlXIncludeCtxtPtr ctxt, xmlDocPtr doc,
Packit Service a31ea6
	              const xmlURL url ATTRIBUTE_UNUSED) {
Packit Service a31ea6
    xmlXIncludeCtxtPtr newctxt;
Packit Service a31ea6
    int i;
Packit Service a31ea6
Packit Service a31ea6
    /*
Packit Service a31ea6
     * Avoid recursion in already substitued resources
Packit Service a31ea6
    for (i = 0;i < ctxt->urlNr;i++) {
Packit Service a31ea6
	if (xmlStrEqual(doc->URL, ctxt->urlTab[i]))
Packit Service a31ea6
	    return;
Packit Service a31ea6
    }
Packit Service a31ea6
     */
Packit Service a31ea6
Packit Service a31ea6
#ifdef DEBUG_XINCLUDE
Packit Service a31ea6
    xmlGenericError(xmlGenericErrorContext, "Recursing in doc %s\n", doc->URL);
Packit Service a31ea6
#endif
Packit Service a31ea6
    /*
Packit Service a31ea6
     * Handle recursion here.
Packit Service a31ea6
     */
Packit Service a31ea6
Packit Service a31ea6
    newctxt = xmlXIncludeNewContext(doc);
Packit Service a31ea6
    if (newctxt != NULL) {
Packit Service a31ea6
	/*
Packit Service a31ea6
	 * Copy the private user data
Packit Service a31ea6
	 */
Packit Service a31ea6
	newctxt->_private = ctxt->_private;
Packit Service a31ea6
	/*
Packit Service a31ea6
	 * Copy the existing document set
Packit Service a31ea6
	 */
Packit Service a31ea6
	newctxt->incMax = ctxt->incMax;
Packit Service a31ea6
	newctxt->incNr = ctxt->incNr;
Packit Service a31ea6
        newctxt->incTab = (xmlXIncludeRefPtr *) xmlMalloc(newctxt->incMax *
Packit Service a31ea6
		                          sizeof(newctxt->incTab[0]));
Packit Service a31ea6
        if (newctxt->incTab == NULL) {
Packit Service a31ea6
	    xmlXIncludeErrMemory(ctxt, (xmlNodePtr) doc, "processing doc");
Packit Service a31ea6
	    xmlFree(newctxt);
Packit Service a31ea6
	    return;
Packit Service a31ea6
	}
Packit Service a31ea6
	/*
Packit Service a31ea6
	 * copy the urlTab
Packit Service a31ea6
	 */
Packit Service a31ea6
	newctxt->urlMax = ctxt->urlMax;
Packit Service a31ea6
	newctxt->urlNr = ctxt->urlNr;
Packit Service a31ea6
	newctxt->urlTab = ctxt->urlTab;
Packit Service a31ea6
Packit Service a31ea6
	/*
Packit Service a31ea6
	 * Inherit the existing base
Packit Service a31ea6
	 */
Packit Service a31ea6
	newctxt->base = xmlStrdup(ctxt->base);
Packit Service a31ea6
Packit Service a31ea6
	/*
Packit Service a31ea6
	 * Inherit the documents already in use by other includes
Packit Service a31ea6
	 */
Packit Service a31ea6
	newctxt->incBase = ctxt->incNr;
Packit Service a31ea6
	for (i = 0;i < ctxt->incNr;i++) {
Packit Service a31ea6
	    newctxt->incTab[i] = ctxt->incTab[i];
Packit Service a31ea6
	    newctxt->incTab[i]->count++; /* prevent the recursion from
Packit Service a31ea6
					    freeing it */
Packit Service a31ea6
	}
Packit Service a31ea6
	/*
Packit Service a31ea6
	 * The new context should also inherit the Parse Flags
Packit Service a31ea6
	 * (bug 132597)
Packit Service a31ea6
	 */
Packit Service a31ea6
	newctxt->parseFlags = ctxt->parseFlags;
Packit Service a31ea6
	xmlXIncludeDoProcess(newctxt, doc, xmlDocGetRootElement(doc));
Packit Service a31ea6
	for (i = 0;i < ctxt->incNr;i++) {
Packit Service a31ea6
	    newctxt->incTab[i]->count--;
Packit Service a31ea6
	    newctxt->incTab[i] = NULL;
Packit Service a31ea6
	}
Packit Service a31ea6
Packit Service a31ea6
	/* urlTab may have been reallocated */
Packit Service a31ea6
	ctxt->urlTab = newctxt->urlTab;
Packit Service a31ea6
	ctxt->urlMax = newctxt->urlMax;
Packit Service a31ea6
Packit Service a31ea6
	newctxt->urlMax = 0;
Packit Service a31ea6
	newctxt->urlNr = 0;
Packit Service a31ea6
	newctxt->urlTab = NULL;
Packit Service a31ea6
Packit Service a31ea6
	xmlXIncludeFreeContext(newctxt);
Packit Service a31ea6
    }
Packit Service a31ea6
#ifdef DEBUG_XINCLUDE
Packit Service a31ea6
    xmlGenericError(xmlGenericErrorContext, "Done recursing in doc %s\n", url);
Packit Service a31ea6
#endif
Packit Service a31ea6
}
Packit Service a31ea6
Packit Service a31ea6
/**
Packit Service a31ea6
 * xmlXIncludeAddTxt:
Packit Service a31ea6
 * @ctxt:  the XInclude context
Packit Service a31ea6
 * @txt:  the new text node
Packit Service a31ea6
 * @url:  the associated URL
Packit Service a31ea6
 *
Packit Service a31ea6
 * Add a new txtument to the list
Packit Service a31ea6
 */
Packit Service a31ea6
static void
Packit Service a31ea6
xmlXIncludeAddTxt(xmlXIncludeCtxtPtr ctxt, xmlNodePtr txt, const xmlURL url) {
Packit Service a31ea6
#ifdef DEBUG_XINCLUDE
Packit Service a31ea6
    xmlGenericError(xmlGenericErrorContext, "Adding text %s\n", url);
Packit Service a31ea6
#endif
Packit Service a31ea6
    if (ctxt->txtMax == 0) {
Packit Service a31ea6
	ctxt->txtMax = 4;
Packit Service a31ea6
        ctxt->txtTab = (xmlNodePtr *) xmlMalloc(ctxt->txtMax *
Packit Service a31ea6
		                          sizeof(ctxt->txtTab[0]));
Packit Service a31ea6
        if (ctxt->txtTab == NULL) {
Packit Service a31ea6
	    xmlXIncludeErrMemory(ctxt, NULL, "processing text");
Packit Service a31ea6
	    return;
Packit Service a31ea6
	}
Packit Service a31ea6
        ctxt->txturlTab = (xmlURL *) xmlMalloc(ctxt->txtMax *
Packit Service a31ea6
		                          sizeof(ctxt->txturlTab[0]));
Packit Service a31ea6
        if (ctxt->txturlTab == NULL) {
Packit Service a31ea6
	    xmlXIncludeErrMemory(ctxt, NULL, "processing text");
Packit Service a31ea6
	    return;
Packit Service a31ea6
	}
Packit Service a31ea6
    }
Packit Service a31ea6
    if (ctxt->txtNr >= ctxt->txtMax) {
Packit Service a31ea6
	ctxt->txtMax *= 2;
Packit Service a31ea6
        ctxt->txtTab = (xmlNodePtr *) xmlRealloc(ctxt->txtTab,
Packit Service a31ea6
	             ctxt->txtMax * sizeof(ctxt->txtTab[0]));
Packit Service a31ea6
        if (ctxt->txtTab == NULL) {
Packit Service a31ea6
	    xmlXIncludeErrMemory(ctxt, NULL, "processing text");
Packit Service a31ea6
	    return;
Packit Service a31ea6
	}
Packit Service a31ea6
        ctxt->txturlTab = (xmlURL *) xmlRealloc(ctxt->txturlTab,
Packit Service a31ea6
	             ctxt->txtMax * sizeof(ctxt->txturlTab[0]));
Packit Service a31ea6
        if (ctxt->txturlTab == NULL) {
Packit Service a31ea6
	    xmlXIncludeErrMemory(ctxt, NULL, "processing text");
Packit Service a31ea6
	    return;
Packit Service a31ea6
	}
Packit Service a31ea6
    }
Packit Service a31ea6
    ctxt->txtTab[ctxt->txtNr] = txt;
Packit Service a31ea6
    ctxt->txturlTab[ctxt->txtNr] = xmlStrdup(url);
Packit Service a31ea6
    ctxt->txtNr++;
Packit Service a31ea6
}
Packit Service a31ea6
Packit Service a31ea6
/************************************************************************
Packit Service a31ea6
 *									*
Packit Service a31ea6
 *			Node copy with specific semantic		*
Packit Service a31ea6
 *									*
Packit Service a31ea6
 ************************************************************************/
Packit Service a31ea6
Packit Service a31ea6
static xmlNodePtr
Packit Service a31ea6
xmlXIncludeCopyNodeList(xmlXIncludeCtxtPtr ctxt, xmlDocPtr target,
Packit Service a31ea6
	                xmlDocPtr source, xmlNodePtr elem);
Packit Service a31ea6
Packit Service a31ea6
/**
Packit Service a31ea6
 * xmlXIncludeCopyNode:
Packit Service a31ea6
 * @ctxt:  the XInclude context
Packit Service a31ea6
 * @target:  the document target
Packit Service a31ea6
 * @source:  the document source
Packit Service a31ea6
 * @elem:  the element
Packit Service a31ea6
 *
Packit Service a31ea6
 * Make a copy of the node while preserving the XInclude semantic
Packit Service a31ea6
 * of the Infoset copy
Packit Service a31ea6
 */
Packit Service a31ea6
static xmlNodePtr
Packit Service a31ea6
xmlXIncludeCopyNode(xmlXIncludeCtxtPtr ctxt, xmlDocPtr target,
Packit Service a31ea6
	            xmlDocPtr source, xmlNodePtr elem) {
Packit Service a31ea6
    xmlNodePtr result = NULL;
Packit Service a31ea6
Packit Service a31ea6
    if ((ctxt == NULL) || (target == NULL) || (source == NULL) ||
Packit Service a31ea6
	(elem == NULL))
Packit Service a31ea6
	return(NULL);
Packit Service a31ea6
    if (elem->type == XML_DTD_NODE)
Packit Service a31ea6
	return(NULL);
Packit Service a31ea6
    if (elem->type == XML_DOCUMENT_NODE)
Packit Service a31ea6
	result = xmlXIncludeCopyNodeList(ctxt, target, source, elem->children);
Packit Service a31ea6
    else
Packit Service a31ea6
        result = xmlDocCopyNode(elem, target, 1);
Packit Service a31ea6
    return(result);
Packit Service a31ea6
}
Packit Service a31ea6
Packit Service a31ea6
/**
Packit Service a31ea6
 * xmlXIncludeCopyNodeList:
Packit Service a31ea6
 * @ctxt:  the XInclude context
Packit Service a31ea6
 * @target:  the document target
Packit Service a31ea6
 * @source:  the document source
Packit Service a31ea6
 * @elem:  the element list
Packit Service a31ea6
 *
Packit Service a31ea6
 * Make a copy of the node list while preserving the XInclude semantic
Packit Service a31ea6
 * of the Infoset copy
Packit Service a31ea6
 */
Packit Service a31ea6
static xmlNodePtr
Packit Service a31ea6
xmlXIncludeCopyNodeList(xmlXIncludeCtxtPtr ctxt, xmlDocPtr target,
Packit Service a31ea6
	                xmlDocPtr source, xmlNodePtr elem) {
Packit Service a31ea6
    xmlNodePtr cur, res, result = NULL, last = NULL;
Packit Service a31ea6
Packit Service a31ea6
    if ((ctxt == NULL) || (target == NULL) || (source == NULL) ||
Packit Service a31ea6
	(elem == NULL))
Packit Service a31ea6
	return(NULL);
Packit Service a31ea6
    cur = elem;
Packit Service a31ea6
    while (cur != NULL) {
Packit Service a31ea6
	res = xmlXIncludeCopyNode(ctxt, target, source, cur);
Packit Service a31ea6
	if (res != NULL) {
Packit Service a31ea6
	    if (result == NULL) {
Packit Service a31ea6
		result = last = res;
Packit Service a31ea6
	    } else {
Packit Service a31ea6
		last->next = res;
Packit Service a31ea6
		res->prev = last;
Packit Service a31ea6
		last = res;
Packit Service a31ea6
	    }
Packit Service a31ea6
	}
Packit Service a31ea6
	cur = cur->next;
Packit Service a31ea6
    }
Packit Service a31ea6
    return(result);
Packit Service a31ea6
}
Packit Service a31ea6
Packit Service a31ea6
/**
Packit Service a31ea6
 * xmlXIncludeGetNthChild:
Packit Service a31ea6
 * @cur:  the node
Packit Service a31ea6
 * @no:  the child number
Packit Service a31ea6
 *
Packit Service a31ea6
 * Returns the @n'th element child of @cur or NULL
Packit Service a31ea6
 */
Packit Service a31ea6
static xmlNodePtr
Packit Service a31ea6
xmlXIncludeGetNthChild(xmlNodePtr cur, int no) {
Packit Service a31ea6
    int i;
Packit Service a31ea6
    if ((cur == NULL) || (cur->type == XML_NAMESPACE_DECL))
Packit Service a31ea6
        return(NULL);
Packit Service a31ea6
    cur = cur->children;
Packit Service a31ea6
    for (i = 0;i <= no;cur = cur->next) {
Packit Service a31ea6
	if (cur == NULL)
Packit Service a31ea6
	    return(cur);
Packit Service a31ea6
	if ((cur->type == XML_ELEMENT_NODE) ||
Packit Service a31ea6
	    (cur->type == XML_DOCUMENT_NODE) ||
Packit Service a31ea6
	    (cur->type == XML_HTML_DOCUMENT_NODE)) {
Packit Service a31ea6
	    i++;
Packit Service a31ea6
	    if (i == no)
Packit Service a31ea6
		break;
Packit Service a31ea6
	}
Packit Service a31ea6
    }
Packit Service a31ea6
    return(cur);
Packit Service a31ea6
}
Packit Service a31ea6
Packit Service a31ea6
xmlNodePtr xmlXPtrAdvanceNode(xmlNodePtr cur, int *level); /* in xpointer.c */
Packit Service a31ea6
/**
Packit Service a31ea6
 * xmlXIncludeCopyRange:
Packit Service a31ea6
 * @ctxt:  the XInclude context
Packit Service a31ea6
 * @target:  the document target
Packit Service a31ea6
 * @source:  the document source
Packit Service a31ea6
 * @obj:  the XPointer result from the evaluation.
Packit Service a31ea6
 *
Packit Service a31ea6
 * Build a node list tree copy of the XPointer result.
Packit Service a31ea6
 *
Packit Service a31ea6
 * Returns an xmlNodePtr list or NULL.
Packit Service a31ea6
 *         The caller has to free the node tree.
Packit Service a31ea6
 */
Packit Service a31ea6
static xmlNodePtr
Packit Service a31ea6
xmlXIncludeCopyRange(xmlXIncludeCtxtPtr ctxt, xmlDocPtr target,
Packit Service a31ea6
	                xmlDocPtr source, xmlXPathObjectPtr range) {
Packit Service a31ea6
    /* pointers to generated nodes */
Packit Service a31ea6
    xmlNodePtr list = NULL, last = NULL, listParent = NULL;
Packit Service a31ea6
    xmlNodePtr tmp, tmp2;
Packit Service a31ea6
    /* pointers to traversal nodes */
Packit Service a31ea6
    xmlNodePtr start, cur, end;
Packit Service a31ea6
    int index1, index2;
Packit Service a31ea6
    int level = 0, lastLevel = 0, endLevel = 0, endFlag = 0;
Packit Service a31ea6
Packit Service a31ea6
    if ((ctxt == NULL) || (target == NULL) || (source == NULL) ||
Packit Service a31ea6
	(range == NULL))
Packit Service a31ea6
	return(NULL);
Packit Service a31ea6
    if (range->type != XPATH_RANGE)
Packit Service a31ea6
	return(NULL);
Packit Service a31ea6
    start = (xmlNodePtr) range->user;
Packit Service a31ea6
Packit Service a31ea6
    if ((start == NULL) || (start->type == XML_NAMESPACE_DECL))
Packit Service a31ea6
	return(NULL);
Packit Service a31ea6
    end = range->user2;
Packit Service a31ea6
    if (end == NULL)
Packit Service a31ea6
	return(xmlDocCopyNode(start, target, 1));
Packit Service a31ea6
    if (end->type == XML_NAMESPACE_DECL)
Packit Service a31ea6
        return(NULL);
Packit Service a31ea6
Packit Service a31ea6
    cur = start;
Packit Service a31ea6
    index1 = range->index;
Packit Service a31ea6
    index2 = range->index2;
Packit Service a31ea6
    /*
Packit Service a31ea6
     * level is depth of the current node under consideration
Packit Service a31ea6
     * list is the pointer to the root of the output tree
Packit Service a31ea6
     * listParent is a pointer to the parent of output tree (within
Packit Service a31ea6
       the included file) in case we need to add another level
Packit Service a31ea6
     * last is a pointer to the last node added to the output tree
Packit Service a31ea6
     * lastLevel is the depth of last (relative to the root)
Packit Service a31ea6
     */
Packit Service a31ea6
    while (cur != NULL) {
Packit Service a31ea6
	/*
Packit Service a31ea6
	 * Check if our output tree needs a parent
Packit Service a31ea6
	 */
Packit Service a31ea6
	if (level < 0) {
Packit Service a31ea6
	    while (level < 0) {
Packit Service a31ea6
	        /* copy must include namespaces and properties */
Packit Service a31ea6
	        tmp2 = xmlDocCopyNode(listParent, target, 2);
Packit Service a31ea6
	        xmlAddChild(tmp2, list);
Packit Service a31ea6
	        list = tmp2;
Packit Service a31ea6
	        listParent = listParent->parent;
Packit Service a31ea6
	        level++;
Packit Service a31ea6
	    }
Packit Service a31ea6
	    last = list;
Packit Service a31ea6
	    lastLevel = 0;
Packit Service a31ea6
	}
Packit Service a31ea6
	/*
Packit Service a31ea6
	 * Check whether we need to change our insertion point
Packit Service a31ea6
	 */
Packit Service a31ea6
	while (level < lastLevel) {
Packit Service a31ea6
	    last = last->parent;
Packit Service a31ea6
	    lastLevel --;
Packit Service a31ea6
	}
Packit Service a31ea6
	if (cur == end) {	/* Are we at the end of the range? */
Packit Service a31ea6
	    if (cur->type == XML_TEXT_NODE) {
Packit Service a31ea6
		const xmlChar *content = cur->content;
Packit Service a31ea6
		int len;
Packit Service a31ea6
Packit Service a31ea6
		if (content == NULL) {
Packit Service a31ea6
		    tmp = xmlNewTextLen(NULL, 0);
Packit Service a31ea6
		} else {
Packit Service a31ea6
		    len = index2;
Packit Service a31ea6
		    if ((cur == start) && (index1 > 1)) {
Packit Service a31ea6
			content += (index1 - 1);
Packit Service a31ea6
			len -= (index1 - 1);
Packit Service a31ea6
		    } else {
Packit Service a31ea6
			len = index2;
Packit Service a31ea6
		    }
Packit Service a31ea6
		    tmp = xmlNewTextLen(content, len);
Packit Service a31ea6
		}
Packit Service a31ea6
		/* single sub text node selection */
Packit Service a31ea6
		if (list == NULL)
Packit Service a31ea6
		    return(tmp);
Packit Service a31ea6
		/* prune and return full set */
Packit Service a31ea6
		if (level == lastLevel)
Packit Service a31ea6
		    xmlAddNextSibling(last, tmp);
Packit Service a31ea6
		else
Packit Service a31ea6
		    xmlAddChild(last, tmp);
Packit Service a31ea6
		return(list);
Packit Service a31ea6
	    } else {	/* ending node not a text node */
Packit Service a31ea6
	        endLevel = level;	/* remember the level of the end node */
Packit Service a31ea6
		endFlag = 1;
Packit Service a31ea6
		/* last node - need to take care of properties + namespaces */
Packit Service a31ea6
		tmp = xmlDocCopyNode(cur, target, 2);
Packit Service a31ea6
		if (list == NULL) {
Packit Service a31ea6
		    list = tmp;
Packit Service a31ea6
		    listParent = cur->parent;
Packit Service a31ea6
		} else {
Packit Service a31ea6
		    if (level == lastLevel)
Packit Service a31ea6
			xmlAddNextSibling(last, tmp);
Packit Service a31ea6
		    else {
Packit Service a31ea6
			xmlAddChild(last, tmp);
Packit Service a31ea6
			lastLevel = level;
Packit Service a31ea6
		    }
Packit Service a31ea6
		}
Packit Service a31ea6
		last = tmp;
Packit Service a31ea6
Packit Service a31ea6
		if (index2 > 1) {
Packit Service a31ea6
		    end = xmlXIncludeGetNthChild(cur, index2 - 1);
Packit Service a31ea6
		    index2 = 0;
Packit Service a31ea6
		}
Packit Service a31ea6
		if ((cur == start) && (index1 > 1)) {
Packit Service a31ea6
		    cur = xmlXIncludeGetNthChild(cur, index1 - 1);
Packit Service a31ea6
		    index1 = 0;
Packit Service a31ea6
		}  else {
Packit Service a31ea6
		    cur = cur->children;
Packit Service a31ea6
		}
Packit Service a31ea6
		level++;	/* increment level to show change */
Packit Service a31ea6
		/*
Packit Service a31ea6
		 * Now gather the remaining nodes from cur to end
Packit Service a31ea6
		 */
Packit Service a31ea6
		continue;	/* while */
Packit Service a31ea6
	    }
Packit Service a31ea6
	} else if (cur == start) {	/* Not at the end, are we at start? */
Packit Service a31ea6
	    if ((cur->type == XML_TEXT_NODE) ||
Packit Service a31ea6
		(cur->type == XML_CDATA_SECTION_NODE)) {
Packit Service a31ea6
		const xmlChar *content = cur->content;
Packit Service a31ea6
Packit Service a31ea6
		if (content == NULL) {
Packit Service a31ea6
		    tmp = xmlNewTextLen(NULL, 0);
Packit Service a31ea6
		} else {
Packit Service a31ea6
		    if (index1 > 1) {
Packit Service a31ea6
			content += (index1 - 1);
Packit Service a31ea6
			index1 = 0;
Packit Service a31ea6
		    }
Packit Service a31ea6
		    tmp = xmlNewText(content);
Packit Service a31ea6
		}
Packit Service a31ea6
		last = list = tmp;
Packit Service a31ea6
		listParent = cur->parent;
Packit Service a31ea6
	    } else {		/* Not text node */
Packit Service a31ea6
	        /*
Packit Service a31ea6
		 * start of the range - need to take care of
Packit Service a31ea6
		 * properties and namespaces
Packit Service a31ea6
		 */
Packit Service a31ea6
		tmp = xmlDocCopyNode(cur, target, 2);
Packit Service a31ea6
		list = last = tmp;
Packit Service a31ea6
		listParent = cur->parent;
Packit Service a31ea6
		if (index1 > 1) {	/* Do we need to position? */
Packit Service a31ea6
		    cur = xmlXIncludeGetNthChild(cur, index1 - 1);
Packit Service a31ea6
		    level = lastLevel = 1;
Packit Service a31ea6
		    index1 = 0;
Packit Service a31ea6
		    /*
Packit Service a31ea6
		     * Now gather the remaining nodes from cur to end
Packit Service a31ea6
		     */
Packit Service a31ea6
		    continue; /* while */
Packit Service a31ea6
		}
Packit Service a31ea6
	    }
Packit Service a31ea6
	} else {
Packit Service a31ea6
	    tmp = NULL;
Packit Service a31ea6
	    switch (cur->type) {
Packit Service a31ea6
		case XML_DTD_NODE:
Packit Service a31ea6
		case XML_ELEMENT_DECL:
Packit Service a31ea6
		case XML_ATTRIBUTE_DECL:
Packit Service a31ea6
		case XML_ENTITY_NODE:
Packit Service a31ea6
		    /* Do not copy DTD informations */
Packit Service a31ea6
		    break;
Packit Service a31ea6
		case XML_ENTITY_DECL:
Packit Service a31ea6
		    /* handle crossing entities -> stack needed */
Packit Service a31ea6
		    break;
Packit Service a31ea6
		case XML_XINCLUDE_START:
Packit Service a31ea6
		case XML_XINCLUDE_END:
Packit Service a31ea6
		    /* don't consider it part of the tree content */
Packit Service a31ea6
		    break;
Packit Service a31ea6
		case XML_ATTRIBUTE_NODE:
Packit Service a31ea6
		    /* Humm, should not happen ! */
Packit Service a31ea6
		    break;
Packit Service a31ea6
		default:
Packit Service a31ea6
		    /*
Packit Service a31ea6
		     * Middle of the range - need to take care of
Packit Service a31ea6
		     * properties and namespaces
Packit Service a31ea6
		     */
Packit Service a31ea6
		    tmp = xmlDocCopyNode(cur, target, 2);
Packit Service a31ea6
		    break;
Packit Service a31ea6
	    }
Packit Service a31ea6
	    if (tmp != NULL) {
Packit Service a31ea6
		if (level == lastLevel)
Packit Service a31ea6
		    xmlAddNextSibling(last, tmp);
Packit Service a31ea6
		else {
Packit Service a31ea6
		    xmlAddChild(last, tmp);
Packit Service a31ea6
		    lastLevel = level;
Packit Service a31ea6
		}
Packit Service a31ea6
		last = tmp;
Packit Service a31ea6
	    }
Packit Service a31ea6
	}
Packit Service a31ea6
	/*
Packit Service a31ea6
	 * Skip to next node in document order
Packit Service a31ea6
	 */
Packit Service a31ea6
	cur = xmlXPtrAdvanceNode(cur, &level);
Packit Service a31ea6
	if (endFlag && (level >= endLevel))
Packit Service a31ea6
	    break;
Packit Service a31ea6
    }
Packit Service a31ea6
    return(list);
Packit Service a31ea6
}
Packit Service a31ea6
Packit Service a31ea6
/**
Packit Service a31ea6
 * xmlXIncludeBuildNodeList:
Packit Service a31ea6
 * @ctxt:  the XInclude context
Packit Service a31ea6
 * @target:  the document target
Packit Service a31ea6
 * @source:  the document source
Packit Service a31ea6
 * @obj:  the XPointer result from the evaluation.
Packit Service a31ea6
 *
Packit Service a31ea6
 * Build a node list tree copy of the XPointer result.
Packit Service a31ea6
 * This will drop Attributes and Namespace declarations.
Packit Service a31ea6
 *
Packit Service a31ea6
 * Returns an xmlNodePtr list or NULL.
Packit Service a31ea6
 *         the caller has to free the node tree.
Packit Service a31ea6
 */
Packit Service a31ea6
static xmlNodePtr
Packit Service a31ea6
xmlXIncludeCopyXPointer(xmlXIncludeCtxtPtr ctxt, xmlDocPtr target,
Packit Service a31ea6
	                xmlDocPtr source, xmlXPathObjectPtr obj) {
Packit Service a31ea6
    xmlNodePtr list = NULL, last = NULL;
Packit Service a31ea6
    int i;
Packit Service a31ea6
Packit Service a31ea6
    if (source == NULL)
Packit Service a31ea6
	source = ctxt->doc;
Packit Service a31ea6
    if ((ctxt == NULL) || (target == NULL) || (source == NULL) ||
Packit Service a31ea6
	(obj == NULL))
Packit Service a31ea6
	return(NULL);
Packit Service a31ea6
    switch (obj->type) {
Packit Service a31ea6
        case XPATH_NODESET: {
Packit Service a31ea6
	    xmlNodeSetPtr set = obj->nodesetval;
Packit Service a31ea6
	    if (set == NULL)
Packit Service a31ea6
		return(NULL);
Packit Service a31ea6
	    for (i = 0;i < set->nodeNr;i++) {
Packit Service a31ea6
		if (set->nodeTab[i] == NULL)
Packit Service a31ea6
		    continue;
Packit Service a31ea6
		switch (set->nodeTab[i]->type) {
Packit Service a31ea6
		    case XML_TEXT_NODE:
Packit Service a31ea6
		    case XML_CDATA_SECTION_NODE:
Packit Service a31ea6
		    case XML_ELEMENT_NODE:
Packit Service a31ea6
		    case XML_ENTITY_REF_NODE:
Packit Service a31ea6
		    case XML_ENTITY_NODE:
Packit Service a31ea6
		    case XML_PI_NODE:
Packit Service a31ea6
		    case XML_COMMENT_NODE:
Packit Service a31ea6
		    case XML_DOCUMENT_NODE:
Packit Service a31ea6
		    case XML_HTML_DOCUMENT_NODE:
Packit Service a31ea6
#ifdef LIBXML_DOCB_ENABLED
Packit Service a31ea6
		    case XML_DOCB_DOCUMENT_NODE:
Packit Service a31ea6
#endif
Packit Service a31ea6
		    case XML_XINCLUDE_END:
Packit Service a31ea6
			break;
Packit Service a31ea6
		    case XML_XINCLUDE_START: {
Packit Service a31ea6
	                xmlNodePtr tmp, cur = set->nodeTab[i];
Packit Service a31ea6
Packit Service a31ea6
			cur = cur->next;
Packit Service a31ea6
			while (cur != NULL) {
Packit Service a31ea6
			    switch(cur->type) {
Packit Service a31ea6
				case XML_TEXT_NODE:
Packit Service a31ea6
				case XML_CDATA_SECTION_NODE:
Packit Service a31ea6
				case XML_ELEMENT_NODE:
Packit Service a31ea6
				case XML_ENTITY_REF_NODE:
Packit Service a31ea6
				case XML_ENTITY_NODE:
Packit Service a31ea6
				case XML_PI_NODE:
Packit Service a31ea6
				case XML_COMMENT_NODE:
Packit Service a31ea6
				    tmp = xmlXIncludeCopyNode(ctxt, target,
Packit Service a31ea6
							      source, cur);
Packit Service a31ea6
				    if (last == NULL) {
Packit Service a31ea6
					list = last = tmp;
Packit Service a31ea6
				    } else {
Packit Service a31ea6
					xmlAddNextSibling(last, tmp);
Packit Service a31ea6
					last = tmp;
Packit Service a31ea6
				    }
Packit Service a31ea6
				    cur = cur->next;
Packit Service a31ea6
				    continue;
Packit Service a31ea6
				default:
Packit Service a31ea6
				    break;
Packit Service a31ea6
			    }
Packit Service a31ea6
			    break;
Packit Service a31ea6
			}
Packit Service a31ea6
			continue;
Packit Service a31ea6
		    }
Packit Service a31ea6
		    case XML_ATTRIBUTE_NODE:
Packit Service a31ea6
		    case XML_NAMESPACE_DECL:
Packit Service a31ea6
		    case XML_DOCUMENT_TYPE_NODE:
Packit Service a31ea6
		    case XML_DOCUMENT_FRAG_NODE:
Packit Service a31ea6
		    case XML_NOTATION_NODE:
Packit Service a31ea6
		    case XML_DTD_NODE:
Packit Service a31ea6
		    case XML_ELEMENT_DECL:
Packit Service a31ea6
		    case XML_ATTRIBUTE_DECL:
Packit Service a31ea6
		    case XML_ENTITY_DECL:
Packit Service a31ea6
			continue; /* for */
Packit Service a31ea6
		}
Packit Service a31ea6
		if (last == NULL)
Packit Service a31ea6
		    list = last = xmlXIncludeCopyNode(ctxt, target, source,
Packit Service a31ea6
			                              set->nodeTab[i]);
Packit Service a31ea6
		else {
Packit Service a31ea6
		    xmlAddNextSibling(last,
Packit Service a31ea6
			    xmlXIncludeCopyNode(ctxt, target, source,
Packit Service a31ea6
				                set->nodeTab[i]));
Packit Service a31ea6
		    if (last->next != NULL)
Packit Service a31ea6
			last = last->next;
Packit Service a31ea6
		}
Packit Service a31ea6
	    }
Packit Service a31ea6
	    break;
Packit Service a31ea6
	}
Packit Service a31ea6
#ifdef LIBXML_XPTR_ENABLED
Packit Service a31ea6
	case XPATH_LOCATIONSET: {
Packit Service a31ea6
	    xmlLocationSetPtr set = (xmlLocationSetPtr) obj->user;
Packit Service a31ea6
	    if (set == NULL)
Packit Service a31ea6
		return(NULL);
Packit Service a31ea6
	    for (i = 0;i < set->locNr;i++) {
Packit Service a31ea6
		if (last == NULL)
Packit Service a31ea6
		    list = last = xmlXIncludeCopyXPointer(ctxt, target, source,
Packit Service a31ea6
			                                  set->locTab[i]);
Packit Service a31ea6
		else
Packit Service a31ea6
		    xmlAddNextSibling(last,
Packit Service a31ea6
			    xmlXIncludeCopyXPointer(ctxt, target, source,
Packit Service a31ea6
				                    set->locTab[i]));
Packit Service a31ea6
		if (last != NULL) {
Packit Service a31ea6
		    while (last->next != NULL)
Packit Service a31ea6
			last = last->next;
Packit Service a31ea6
		}
Packit Service a31ea6
	    }
Packit Service a31ea6
	    break;
Packit Service a31ea6
	}
Packit Service a31ea6
	case XPATH_RANGE:
Packit Service a31ea6
	    return(xmlXIncludeCopyRange(ctxt, target, source, obj));
Packit Service a31ea6
#endif
Packit Service a31ea6
	case XPATH_POINT:
Packit Service a31ea6
	    /* points are ignored in XInclude */
Packit Service a31ea6
	    break;
Packit Service a31ea6
	default:
Packit Service a31ea6
	    break;
Packit Service a31ea6
    }
Packit Service a31ea6
    return(list);
Packit Service a31ea6
}
Packit Service a31ea6
/************************************************************************
Packit Service a31ea6
 *									*
Packit Service a31ea6
 *			XInclude I/O handling				*
Packit Service a31ea6
 *									*
Packit Service a31ea6
 ************************************************************************/
Packit Service a31ea6
Packit Service a31ea6
typedef struct _xmlXIncludeMergeData xmlXIncludeMergeData;
Packit Service a31ea6
typedef xmlXIncludeMergeData *xmlXIncludeMergeDataPtr;
Packit Service a31ea6
struct _xmlXIncludeMergeData {
Packit Service a31ea6
    xmlDocPtr doc;
Packit Service a31ea6
    xmlXIncludeCtxtPtr ctxt;
Packit Service a31ea6
};
Packit Service a31ea6
Packit Service a31ea6
/**
Packit Service a31ea6
 * xmlXIncludeMergeOneEntity:
Packit Service a31ea6
 * @ent: the entity
Packit Service a31ea6
 * @doc:  the including doc
Packit Service a31ea6
 * @nr: the entity name
Packit Service a31ea6
 *
Packit Service a31ea6
 * Inplements the merge of one entity
Packit Service a31ea6
 */
Packit Service a31ea6
static void
Packit Service a31ea6
xmlXIncludeMergeEntity(xmlEntityPtr ent, xmlXIncludeMergeDataPtr data,
Packit Service a31ea6
	               xmlChar *name ATTRIBUTE_UNUSED) {
Packit Service a31ea6
    xmlEntityPtr ret, prev;
Packit Service a31ea6
    xmlDocPtr doc;
Packit Service a31ea6
    xmlXIncludeCtxtPtr ctxt;
Packit Service a31ea6
Packit Service a31ea6
    if ((ent == NULL) || (data == NULL))
Packit Service a31ea6
	return;
Packit Service a31ea6
    ctxt = data->ctxt;
Packit Service a31ea6
    doc = data->doc;
Packit Service a31ea6
    if ((ctxt == NULL) || (doc == NULL))
Packit Service a31ea6
	return;
Packit Service a31ea6
    switch (ent->etype) {
Packit Service a31ea6
        case XML_INTERNAL_PARAMETER_ENTITY:
Packit Service a31ea6
        case XML_EXTERNAL_PARAMETER_ENTITY:
Packit Service a31ea6
        case XML_INTERNAL_PREDEFINED_ENTITY:
Packit Service a31ea6
	    return;
Packit Service a31ea6
        case XML_INTERNAL_GENERAL_ENTITY:
Packit Service a31ea6
        case XML_EXTERNAL_GENERAL_PARSED_ENTITY:
Packit Service a31ea6
        case XML_EXTERNAL_GENERAL_UNPARSED_ENTITY:
Packit Service a31ea6
	    break;
Packit Service a31ea6
    }
Packit Service a31ea6
    ret = xmlAddDocEntity(doc, ent->name, ent->etype, ent->ExternalID,
Packit Service a31ea6
			  ent->SystemID, ent->content);
Packit Service a31ea6
    if (ret != NULL) {
Packit Service a31ea6
	if (ent->URI != NULL)
Packit Service a31ea6
	    ret->URI = xmlStrdup(ent->URI);
Packit Service a31ea6
    } else {
Packit Service a31ea6
	prev = xmlGetDocEntity(doc, ent->name);
Packit Service a31ea6
	if (prev != NULL) {
Packit Service a31ea6
	    if (ent->etype != prev->etype)
Packit Service a31ea6
		goto error;
Packit Service a31ea6
Packit Service a31ea6
	    if ((ent->SystemID != NULL) && (prev->SystemID != NULL)) {
Packit Service a31ea6
		if (!xmlStrEqual(ent->SystemID, prev->SystemID))
Packit Service a31ea6
		    goto error;
Packit Service a31ea6
	    } else if ((ent->ExternalID != NULL) &&
Packit Service a31ea6
		       (prev->ExternalID != NULL)) {
Packit Service a31ea6
		if (!xmlStrEqual(ent->ExternalID, prev->ExternalID))
Packit Service a31ea6
		    goto error;
Packit Service a31ea6
	    } else if ((ent->content != NULL) && (prev->content != NULL)) {
Packit Service a31ea6
		if (!xmlStrEqual(ent->content, prev->content))
Packit Service a31ea6
		    goto error;
Packit Service a31ea6
	    } else {
Packit Service a31ea6
		goto error;
Packit Service a31ea6
	    }
Packit Service a31ea6
Packit Service a31ea6
	}
Packit Service a31ea6
    }
Packit Service a31ea6
    return;
Packit Service a31ea6
error:
Packit Service a31ea6
    switch (ent->etype) {
Packit Service a31ea6
        case XML_INTERNAL_PARAMETER_ENTITY:
Packit Service a31ea6
        case XML_EXTERNAL_PARAMETER_ENTITY:
Packit Service a31ea6
        case XML_INTERNAL_PREDEFINED_ENTITY:
Packit Service a31ea6
        case XML_INTERNAL_GENERAL_ENTITY:
Packit Service a31ea6
        case XML_EXTERNAL_GENERAL_PARSED_ENTITY:
Packit Service a31ea6
	    return;
Packit Service a31ea6
        case XML_EXTERNAL_GENERAL_UNPARSED_ENTITY:
Packit Service a31ea6
	    break;
Packit Service a31ea6
    }
Packit Service a31ea6
    xmlXIncludeErr(ctxt, (xmlNodePtr) ent, XML_XINCLUDE_ENTITY_DEF_MISMATCH,
Packit Service a31ea6
                   "mismatch in redefinition of entity %s\n",
Packit Service a31ea6
		   ent->name);
Packit Service a31ea6
}
Packit Service a31ea6
Packit Service a31ea6
/**
Packit Service a31ea6
 * xmlXIncludeMergeEntities:
Packit Service a31ea6
 * @ctxt: an XInclude context
Packit Service a31ea6
 * @doc:  the including doc
Packit Service a31ea6
 * @from:  the included doc
Packit Service a31ea6
 *
Packit Service a31ea6
 * Inplements the entity merge
Packit Service a31ea6
 *
Packit Service a31ea6
 * Returns 0 if merge succeeded, -1 if some processing failed
Packit Service a31ea6
 */
Packit Service a31ea6
static int
Packit Service a31ea6
xmlXIncludeMergeEntities(xmlXIncludeCtxtPtr ctxt, xmlDocPtr doc,
Packit Service a31ea6
	                 xmlDocPtr from) {
Packit Service a31ea6
    xmlNodePtr cur;
Packit Service a31ea6
    xmlDtdPtr target, source;
Packit Service a31ea6
Packit Service a31ea6
    if (ctxt == NULL)
Packit Service a31ea6
	return(-1);
Packit Service a31ea6
Packit Service a31ea6
    if ((from == NULL) || (from->intSubset == NULL))
Packit Service a31ea6
	return(0);
Packit Service a31ea6
Packit Service a31ea6
    target = doc->intSubset;
Packit Service a31ea6
    if (target == NULL) {
Packit Service a31ea6
	cur = xmlDocGetRootElement(doc);
Packit Service a31ea6
	if (cur == NULL)
Packit Service a31ea6
	    return(-1);
Packit Service a31ea6
        target = xmlCreateIntSubset(doc, cur->name, NULL, NULL);
Packit Service a31ea6
	if (target == NULL)
Packit Service a31ea6
	    return(-1);
Packit Service a31ea6
    }
Packit Service a31ea6
Packit Service a31ea6
    source = from->intSubset;
Packit Service a31ea6
    if ((source != NULL) && (source->entities != NULL)) {
Packit Service a31ea6
	xmlXIncludeMergeData data;
Packit Service a31ea6
Packit Service a31ea6
	data.ctxt = ctxt;
Packit Service a31ea6
	data.doc = doc;
Packit Service a31ea6
Packit Service a31ea6
	xmlHashScan((xmlHashTablePtr) source->entities,
Packit Service a31ea6
		    (xmlHashScanner) xmlXIncludeMergeEntity, &data);
Packit Service a31ea6
    }
Packit Service a31ea6
    source = from->extSubset;
Packit Service a31ea6
    if ((source != NULL) && (source->entities != NULL)) {
Packit Service a31ea6
	xmlXIncludeMergeData data;
Packit Service a31ea6
Packit Service a31ea6
	data.ctxt = ctxt;
Packit Service a31ea6
	data.doc = doc;
Packit Service a31ea6
Packit Service a31ea6
	/*
Packit Service a31ea6
	 * don't duplicate existing stuff when external subsets are the same
Packit Service a31ea6
	 */
Packit Service a31ea6
	if ((!xmlStrEqual(target->ExternalID, source->ExternalID)) &&
Packit Service a31ea6
	    (!xmlStrEqual(target->SystemID, source->SystemID))) {
Packit Service a31ea6
	    xmlHashScan((xmlHashTablePtr) source->entities,
Packit Service a31ea6
			(xmlHashScanner) xmlXIncludeMergeEntity, &data);
Packit Service a31ea6
	}
Packit Service a31ea6
    }
Packit Service a31ea6
    return(0);
Packit Service a31ea6
}
Packit Service a31ea6
Packit Service a31ea6
/**
Packit Service a31ea6
 * xmlXIncludeLoadDoc:
Packit Service a31ea6
 * @ctxt:  the XInclude context
Packit Service a31ea6
 * @url:  the associated URL
Packit Service a31ea6
 * @nr:  the xinclude node number
Packit Service a31ea6
 *
Packit Service a31ea6
 * Load the document, and store the result in the XInclude context
Packit Service a31ea6
 *
Packit Service a31ea6
 * Returns 0 in case of success, -1 in case of failure
Packit Service a31ea6
 */
Packit Service a31ea6
static int
Packit Service a31ea6
xmlXIncludeLoadDoc(xmlXIncludeCtxtPtr ctxt, const xmlChar *url, int nr) {
Packit Service a31ea6
    xmlDocPtr doc;
Packit Service a31ea6
    xmlURIPtr uri;
Packit Service a31ea6
    xmlChar *URL;
Packit Service a31ea6
    xmlChar *fragment = NULL;
Packit Service a31ea6
    int i = 0;
Packit Service a31ea6
#ifdef LIBXML_XPTR_ENABLED
Packit Service a31ea6
    int saveFlags;
Packit Service a31ea6
#endif
Packit Service a31ea6
Packit Service a31ea6
#ifdef DEBUG_XINCLUDE
Packit Service a31ea6
    xmlGenericError(xmlGenericErrorContext, "Loading doc %s:%d\n", url, nr);
Packit Service a31ea6
#endif
Packit Service a31ea6
    /*
Packit Service a31ea6
     * Check the URL and remove any fragment identifier
Packit Service a31ea6
     */
Packit Service a31ea6
    uri = xmlParseURI((const char *)url);
Packit Service a31ea6
    if (uri == NULL) {
Packit Service a31ea6
	xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref,
Packit Service a31ea6
	               XML_XINCLUDE_HREF_URI,
Packit Service a31ea6
		       "invalid value URI %s\n", url);
Packit Service a31ea6
	return(-1);
Packit Service a31ea6
    }
Packit Service a31ea6
    if (uri->fragment != NULL) {
Packit Service a31ea6
	fragment = (xmlChar *) uri->fragment;
Packit Service a31ea6
	uri->fragment = NULL;
Packit Service a31ea6
    }
Packit Service a31ea6
    if ((ctxt->incTab != NULL) && (ctxt->incTab[nr] != NULL) &&
Packit Service a31ea6
        (ctxt->incTab[nr]->fragment != NULL)) {
Packit Service a31ea6
	if (fragment != NULL) xmlFree(fragment);
Packit Service a31ea6
	fragment = xmlStrdup(ctxt->incTab[nr]->fragment);
Packit Service a31ea6
    }
Packit Service a31ea6
    URL = xmlSaveUri(uri);
Packit Service a31ea6
    xmlFreeURI(uri);
Packit Service a31ea6
    if (URL == NULL) {
Packit Service a31ea6
        if (ctxt->incTab != NULL)
Packit Service a31ea6
	    xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref,
Packit Service a31ea6
			   XML_XINCLUDE_HREF_URI,
Packit Service a31ea6
			   "invalid value URI %s\n", url);
Packit Service a31ea6
	else
Packit Service a31ea6
	    xmlXIncludeErr(ctxt, NULL,
Packit Service a31ea6
			   XML_XINCLUDE_HREF_URI,
Packit Service a31ea6
			   "invalid value URI %s\n", url);
Packit Service a31ea6
	if (fragment != NULL)
Packit Service a31ea6
	    xmlFree(fragment);
Packit Service a31ea6
	return(-1);
Packit Service a31ea6
    }
Packit Service a31ea6
Packit Service a31ea6
    /*
Packit Service a31ea6
     * Handling of references to the local document are done
Packit Service a31ea6
     * directly through ctxt->doc.
Packit Service a31ea6
     */
Packit Service a31ea6
    if ((URL[0] == 0) || (URL[0] == '#') ||
Packit Service a31ea6
	((ctxt->doc != NULL) && (xmlStrEqual(URL, ctxt->doc->URL)))) {
Packit Service a31ea6
	doc = NULL;
Packit Service a31ea6
        goto loaded;
Packit Service a31ea6
    }
Packit Service a31ea6
Packit Service a31ea6
    /*
Packit Service a31ea6
     * Prevent reloading twice the document.
Packit Service a31ea6
     */
Packit Service a31ea6
    for (i = 0; i < ctxt->incNr; i++) {
Packit Service a31ea6
	if ((xmlStrEqual(URL, ctxt->incTab[i]->URI)) &&
Packit Service a31ea6
	    (ctxt->incTab[i]->doc != NULL)) {
Packit Service a31ea6
	    doc = ctxt->incTab[i]->doc;
Packit Service a31ea6
#ifdef DEBUG_XINCLUDE
Packit Service a31ea6
	    printf("Already loaded %s\n", URL);
Packit Service a31ea6
#endif
Packit Service a31ea6
	    goto loaded;
Packit Service a31ea6
	}
Packit Service a31ea6
    }
Packit Service a31ea6
Packit Service a31ea6
    /*
Packit Service a31ea6
     * Load it.
Packit Service a31ea6
     */
Packit Service a31ea6
#ifdef DEBUG_XINCLUDE
Packit Service a31ea6
    printf("loading %s\n", URL);
Packit Service a31ea6
#endif
Packit Service a31ea6
#ifdef LIBXML_XPTR_ENABLED
Packit Service a31ea6
    /*
Packit Service a31ea6
     * If this is an XPointer evaluation, we want to assure that
Packit Service a31ea6
     * all entities have been resolved prior to processing the
Packit Service a31ea6
     * referenced document
Packit Service a31ea6
     */
Packit Service a31ea6
    saveFlags = ctxt->parseFlags;
Packit Service a31ea6
    if (fragment != NULL) {	/* if this is an XPointer eval */
Packit Service a31ea6
	ctxt->parseFlags |= XML_PARSE_NOENT;
Packit Service a31ea6
    }
Packit Service a31ea6
#endif
Packit Service a31ea6
Packit Service a31ea6
    doc = xmlXIncludeParseFile(ctxt, (const char *)URL);
Packit Service a31ea6
#ifdef LIBXML_XPTR_ENABLED
Packit Service a31ea6
    ctxt->parseFlags = saveFlags;
Packit Service a31ea6
#endif
Packit Service a31ea6
    if (doc == NULL) {
Packit Service a31ea6
	xmlFree(URL);
Packit Service a31ea6
	if (fragment != NULL)
Packit Service a31ea6
	    xmlFree(fragment);
Packit Service a31ea6
	return(-1);
Packit Service a31ea6
    }
Packit Service a31ea6
    ctxt->incTab[nr]->doc = doc;
Packit Service a31ea6
    /*
Packit Service a31ea6
     * It's possible that the requested URL has been mapped to a
Packit Service a31ea6
     * completely different location (e.g. through a catalog entry).
Packit Service a31ea6
     * To check for this, we compare the URL with that of the doc
Packit Service a31ea6
     * and change it if they disagree (bug 146988).
Packit Service a31ea6
     */
Packit Service a31ea6
   if (!xmlStrEqual(URL, doc->URL)) {
Packit Service a31ea6
       xmlFree(URL);
Packit Service a31ea6
       URL = xmlStrdup(doc->URL);
Packit Service a31ea6
   }
Packit Service a31ea6
    for (i = nr + 1; i < ctxt->incNr; i++) {
Packit Service a31ea6
	if (xmlStrEqual(URL, ctxt->incTab[i]->URI)) {
Packit Service a31ea6
	    ctxt->incTab[nr]->count++;
Packit Service a31ea6
#ifdef DEBUG_XINCLUDE
Packit Service a31ea6
	    printf("Increasing %s count since reused\n", URL);
Packit Service a31ea6
#endif
Packit Service a31ea6
            break;
Packit Service a31ea6
	}
Packit Service a31ea6
    }
Packit Service a31ea6
Packit Service a31ea6
    /*
Packit Service a31ea6
     * Make sure we have all entities fixed up
Packit Service a31ea6
     */
Packit Service a31ea6
    xmlXIncludeMergeEntities(ctxt, ctxt->doc, doc);
Packit Service a31ea6
Packit Service a31ea6
    /*
Packit Service a31ea6
     * We don't need the DTD anymore, free up space
Packit Service a31ea6
    if (doc->intSubset != NULL) {
Packit Service a31ea6
	xmlUnlinkNode((xmlNodePtr) doc->intSubset);
Packit Service a31ea6
	xmlFreeNode((xmlNodePtr) doc->intSubset);
Packit Service a31ea6
	doc->intSubset = NULL;
Packit Service a31ea6
    }
Packit Service a31ea6
    if (doc->extSubset != NULL) {
Packit Service a31ea6
	xmlUnlinkNode((xmlNodePtr) doc->extSubset);
Packit Service a31ea6
	xmlFreeNode((xmlNodePtr) doc->extSubset);
Packit Service a31ea6
	doc->extSubset = NULL;
Packit Service a31ea6
    }
Packit Service a31ea6
     */
Packit Service a31ea6
    xmlXIncludeRecurseDoc(ctxt, doc, URL);
Packit Service a31ea6
Packit Service a31ea6
loaded:
Packit Service a31ea6
    if (fragment == NULL) {
Packit Service a31ea6
	/*
Packit Service a31ea6
	 * Add the top children list as the replacement copy.
Packit Service a31ea6
	 */
Packit Service a31ea6
	if (doc == NULL)
Packit Service a31ea6
	{
Packit Service a31ea6
	    /* Hopefully a DTD declaration won't be copied from
Packit Service a31ea6
	     * the same document */
Packit Service a31ea6
	    ctxt->incTab[nr]->inc = xmlCopyNodeList(ctxt->doc->children);
Packit Service a31ea6
	} else {
Packit Service a31ea6
	    ctxt->incTab[nr]->inc = xmlXIncludeCopyNodeList(ctxt, ctxt->doc,
Packit Service a31ea6
		                                       doc, doc->children);
Packit Service a31ea6
	}
Packit Service a31ea6
    }
Packit Service a31ea6
#ifdef LIBXML_XPTR_ENABLED
Packit Service a31ea6
    else {
Packit Service a31ea6
	/*
Packit Service a31ea6
	 * Computes the XPointer expression and make a copy used
Packit Service a31ea6
	 * as the replacement copy.
Packit Service a31ea6
	 */
Packit Service a31ea6
	xmlXPathObjectPtr xptr;
Packit Service a31ea6
	xmlXPathContextPtr xptrctxt;
Packit Service a31ea6
	xmlNodeSetPtr set;
Packit Service a31ea6
Packit Service a31ea6
	if (doc == NULL) {
Packit Service a31ea6
	    xptrctxt = xmlXPtrNewContext(ctxt->doc, ctxt->incTab[nr]->ref,
Packit Service a31ea6
		                         NULL);
Packit Service a31ea6
	} else {
Packit Service a31ea6
	    xptrctxt = xmlXPtrNewContext(doc, NULL, NULL);
Packit Service a31ea6
	}
Packit Service a31ea6
	if (xptrctxt == NULL) {
Packit Service a31ea6
	    xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref,
Packit Service a31ea6
	                   XML_XINCLUDE_XPTR_FAILED,
Packit Service a31ea6
			   "could not create XPointer context\n", NULL);
Packit Service a31ea6
	    xmlFree(URL);
Packit Service a31ea6
	    xmlFree(fragment);
Packit Service a31ea6
	    return(-1);
Packit Service a31ea6
	}
Packit Service a31ea6
	xptr = xmlXPtrEval(fragment, xptrctxt);
Packit Service a31ea6
	if (xptr == NULL) {
Packit Service a31ea6
	    xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref,
Packit Service a31ea6
	                   XML_XINCLUDE_XPTR_FAILED,
Packit Service a31ea6
			   "XPointer evaluation failed: #%s\n",
Packit Service a31ea6
			   fragment);
Packit Service a31ea6
	    xmlXPathFreeContext(xptrctxt);
Packit Service a31ea6
	    xmlFree(URL);
Packit Service a31ea6
	    xmlFree(fragment);
Packit Service a31ea6
	    return(-1);
Packit Service a31ea6
	}
Packit Service a31ea6
	switch (xptr->type) {
Packit Service a31ea6
	    case XPATH_UNDEFINED:
Packit Service a31ea6
	    case XPATH_BOOLEAN:
Packit Service a31ea6
	    case XPATH_NUMBER:
Packit Service a31ea6
	    case XPATH_STRING:
Packit Service a31ea6
	    case XPATH_POINT:
Packit Service a31ea6
	    case XPATH_USERS:
Packit Service a31ea6
	    case XPATH_XSLT_TREE:
Packit Service a31ea6
		xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref,
Packit Service a31ea6
		               XML_XINCLUDE_XPTR_RESULT,
Packit Service a31ea6
			       "XPointer is not a range: #%s\n",
Packit Service a31ea6
			       fragment);
Packit Service a31ea6
		xmlXPathFreeContext(xptrctxt);
Packit Service a31ea6
		xmlFree(URL);
Packit Service a31ea6
		xmlFree(fragment);
Packit Service a31ea6
		return(-1);
Packit Service a31ea6
	    case XPATH_NODESET:
Packit Service a31ea6
	        if ((xptr->nodesetval == NULL) ||
Packit Service a31ea6
		    (xptr->nodesetval->nodeNr <= 0)) {
Packit Service a31ea6
		    xmlXPathFreeContext(xptrctxt);
Packit Service a31ea6
		    xmlFree(URL);
Packit Service a31ea6
		    xmlFree(fragment);
Packit Service a31ea6
		    return(-1);
Packit Service a31ea6
		}
Packit Service a31ea6
Packit Service a31ea6
	    case XPATH_RANGE:
Packit Service a31ea6
	    case XPATH_LOCATIONSET:
Packit Service a31ea6
		break;
Packit Service a31ea6
	}
Packit Service a31ea6
	set = xptr->nodesetval;
Packit Service a31ea6
	if (set != NULL) {
Packit Service a31ea6
	    for (i = 0;i < set->nodeNr;i++) {
Packit Service a31ea6
		if (set->nodeTab[i] == NULL)
Packit Service a31ea6
		    continue;
Packit Service a31ea6
		switch (set->nodeTab[i]->type) {
Packit Service a31ea6
		    case XML_ELEMENT_NODE:
Packit Service a31ea6
		    case XML_TEXT_NODE:
Packit Service a31ea6
		    case XML_CDATA_SECTION_NODE:
Packit Service a31ea6
		    case XML_ENTITY_REF_NODE:
Packit Service a31ea6
		    case XML_ENTITY_NODE:
Packit Service a31ea6
		    case XML_PI_NODE:
Packit Service a31ea6
		    case XML_COMMENT_NODE:
Packit Service a31ea6
		    case XML_DOCUMENT_NODE:
Packit Service a31ea6
		    case XML_HTML_DOCUMENT_NODE:
Packit Service a31ea6
#ifdef LIBXML_DOCB_ENABLED
Packit Service a31ea6
		    case XML_DOCB_DOCUMENT_NODE:
Packit Service a31ea6
#endif
Packit Service a31ea6
			continue;
Packit Service a31ea6
Packit Service a31ea6
		    case XML_ATTRIBUTE_NODE:
Packit Service a31ea6
			xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref,
Packit Service a31ea6
			               XML_XINCLUDE_XPTR_RESULT,
Packit Service a31ea6
				       "XPointer selects an attribute: #%s\n",
Packit Service a31ea6
				       fragment);
Packit Service a31ea6
			set->nodeTab[i] = NULL;
Packit Service a31ea6
			continue;
Packit Service a31ea6
		    case XML_NAMESPACE_DECL:
Packit Service a31ea6
			xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref,
Packit Service a31ea6
			               XML_XINCLUDE_XPTR_RESULT,
Packit Service a31ea6
				       "XPointer selects a namespace: #%s\n",
Packit Service a31ea6
				       fragment);
Packit Service a31ea6
			set->nodeTab[i] = NULL;
Packit Service a31ea6
			continue;
Packit Service a31ea6
		    case XML_DOCUMENT_TYPE_NODE:
Packit Service a31ea6
		    case XML_DOCUMENT_FRAG_NODE:
Packit Service a31ea6
		    case XML_NOTATION_NODE:
Packit Service a31ea6
		    case XML_DTD_NODE:
Packit Service a31ea6
		    case XML_ELEMENT_DECL:
Packit Service a31ea6
		    case XML_ATTRIBUTE_DECL:
Packit Service a31ea6
		    case XML_ENTITY_DECL:
Packit Service a31ea6
		    case XML_XINCLUDE_START:
Packit Service a31ea6
		    case XML_XINCLUDE_END:
Packit Service a31ea6
			xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref,
Packit Service a31ea6
			               XML_XINCLUDE_XPTR_RESULT,
Packit Service a31ea6
				   "XPointer selects unexpected nodes: #%s\n",
Packit Service a31ea6
				       fragment);
Packit Service a31ea6
			set->nodeTab[i] = NULL;
Packit Service a31ea6
			set->nodeTab[i] = NULL;
Packit Service a31ea6
			continue; /* for */
Packit Service a31ea6
		}
Packit Service a31ea6
	    }
Packit Service a31ea6
	}
Packit Service a31ea6
	if (doc == NULL) {
Packit Service a31ea6
	    ctxt->incTab[nr]->xptr = xptr;
Packit Service a31ea6
	    ctxt->incTab[nr]->inc = NULL;
Packit Service a31ea6
	} else {
Packit Service a31ea6
	    ctxt->incTab[nr]->inc =
Packit Service a31ea6
		xmlXIncludeCopyXPointer(ctxt, ctxt->doc, doc, xptr);
Packit Service a31ea6
	    xmlXPathFreeObject(xptr);
Packit Service a31ea6
	}
Packit Service a31ea6
	xmlXPathFreeContext(xptrctxt);
Packit Service a31ea6
	xmlFree(fragment);
Packit Service a31ea6
    }
Packit Service a31ea6
#endif
Packit Service a31ea6
Packit Service a31ea6
    /*
Packit Service a31ea6
     * Do the xml:base fixup if needed
Packit Service a31ea6
     */
Packit Service a31ea6
    if ((doc != NULL) && (URL != NULL) &&
Packit Service a31ea6
        (!(ctxt->parseFlags & XML_PARSE_NOBASEFIX)) &&
Packit Service a31ea6
	(!(doc->parseFlags & XML_PARSE_NOBASEFIX))) {
Packit Service a31ea6
	xmlNodePtr node;
Packit Service a31ea6
	xmlChar *base;
Packit Service a31ea6
	xmlChar *curBase;
Packit Service a31ea6
Packit Service a31ea6
	/*
Packit Service a31ea6
	 * The base is only adjusted if "necessary", i.e. if the xinclude node
Packit Service a31ea6
	 * has a base specified, or the URL is relative
Packit Service a31ea6
	 */
Packit Service a31ea6
	base = xmlGetNsProp(ctxt->incTab[nr]->ref, BAD_CAST "base",
Packit Service a31ea6
			XML_XML_NAMESPACE);
Packit Service a31ea6
	if (base == NULL) {
Packit Service a31ea6
	    /*
Packit Service a31ea6
	     * No xml:base on the xinclude node, so we check whether the
Packit Service a31ea6
	     * URI base is different than (relative to) the context base
Packit Service a31ea6
	     */
Packit Service a31ea6
	    curBase = xmlBuildRelativeURI(URL, ctxt->base);
Packit Service a31ea6
	    if (curBase == NULL) {	/* Error return */
Packit Service a31ea6
	        xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref,
Packit Service a31ea6
	               XML_XINCLUDE_HREF_URI,
Packit Service a31ea6
		       "trying to build relative URI from %s\n", URL);
Packit Service a31ea6
	    } else {
Packit Service a31ea6
		/* If the URI doesn't contain a slash, it's not relative */
Packit Service a31ea6
	        if (!xmlStrchr(curBase, (xmlChar) '/'))
Packit Service a31ea6
		    xmlFree(curBase);
Packit Service a31ea6
		else
Packit Service a31ea6
		    base = curBase;
Packit Service a31ea6
	    }
Packit Service a31ea6
	}
Packit Service a31ea6
	if (base != NULL) {	/* Adjustment may be needed */
Packit Service a31ea6
	    node = ctxt->incTab[nr]->inc;
Packit Service a31ea6
	    while (node != NULL) {
Packit Service a31ea6
		/* Only work on element nodes */
Packit Service a31ea6
		if (node->type == XML_ELEMENT_NODE) {
Packit Service a31ea6
		    curBase = xmlNodeGetBase(node->doc, node);
Packit Service a31ea6
		    /* If no current base, set it */
Packit Service a31ea6
		    if (curBase == NULL) {
Packit Service a31ea6
			xmlNodeSetBase(node, base);
Packit Service a31ea6
		    } else {
Packit Service a31ea6
			/*
Packit Service a31ea6
			 * If the current base is the same as the
Packit Service a31ea6
			 * URL of the document, then reset it to be
Packit Service a31ea6
			 * the specified xml:base or the relative URI
Packit Service a31ea6
			 */
Packit Service a31ea6
			if (xmlStrEqual(curBase, node->doc->URL)) {
Packit Service a31ea6
			    xmlNodeSetBase(node, base);
Packit Service a31ea6
			} else {
Packit Service a31ea6
			    /*
Packit Service a31ea6
			     * If the element already has an xml:base
Packit Service a31ea6
			     * set, then relativise it if necessary
Packit Service a31ea6
			     */
Packit Service a31ea6
			    xmlChar *xmlBase;
Packit Service a31ea6
			    xmlBase = xmlGetNsProp(node,
Packit Service a31ea6
					    BAD_CAST "base",
Packit Service a31ea6
					    XML_XML_NAMESPACE);
Packit Service a31ea6
			    if (xmlBase != NULL) {
Packit Service a31ea6
				xmlChar *relBase;
Packit Service a31ea6
				relBase = xmlBuildURI(xmlBase, base);
Packit Service a31ea6
				if (relBase == NULL) { /* error */
Packit Service a31ea6
				    xmlXIncludeErr(ctxt,
Packit Service a31ea6
						ctxt->incTab[nr]->ref,
Packit Service a31ea6
						XML_XINCLUDE_HREF_URI,
Packit Service a31ea6
					"trying to rebuild base from %s\n",
Packit Service a31ea6
						xmlBase);
Packit Service a31ea6
				} else {
Packit Service a31ea6
				    xmlNodeSetBase(node, relBase);
Packit Service a31ea6
				    xmlFree(relBase);
Packit Service a31ea6
				}
Packit Service a31ea6
				xmlFree(xmlBase);
Packit Service a31ea6
			    }
Packit Service a31ea6
			}
Packit Service a31ea6
			xmlFree(curBase);
Packit Service a31ea6
		    }
Packit Service a31ea6
		}
Packit Service a31ea6
	        node = node->next;
Packit Service a31ea6
	    }
Packit Service a31ea6
	    xmlFree(base);
Packit Service a31ea6
	}
Packit Service a31ea6
    }
Packit Service a31ea6
    if ((nr < ctxt->incNr) && (ctxt->incTab[nr]->doc != NULL) &&
Packit Service a31ea6
	(ctxt->incTab[nr]->count <= 1)) {
Packit Service a31ea6
#ifdef DEBUG_XINCLUDE
Packit Service a31ea6
        printf("freeing %s\n", ctxt->incTab[nr]->doc->URL);
Packit Service a31ea6
#endif
Packit Service a31ea6
	xmlFreeDoc(ctxt->incTab[nr]->doc);
Packit Service a31ea6
	ctxt->incTab[nr]->doc = NULL;
Packit Service a31ea6
    }
Packit Service a31ea6
    xmlFree(URL);
Packit Service a31ea6
    return(0);
Packit Service a31ea6
}
Packit Service a31ea6
Packit Service a31ea6
/**
Packit Service a31ea6
 * xmlXIncludeLoadTxt:
Packit Service a31ea6
 * @ctxt:  the XInclude context
Packit Service a31ea6
 * @url:  the associated URL
Packit Service a31ea6
 * @nr:  the xinclude node number
Packit Service a31ea6
 *
Packit Service a31ea6
 * Load the content, and store the result in the XInclude context
Packit Service a31ea6
 *
Packit Service a31ea6
 * Returns 0 in case of success, -1 in case of failure
Packit Service a31ea6
 */
Packit Service a31ea6
static int
Packit Service a31ea6
xmlXIncludeLoadTxt(xmlXIncludeCtxtPtr ctxt, const xmlChar *url, int nr) {
Packit Service a31ea6
    xmlParserInputBufferPtr buf;
Packit Service a31ea6
    xmlNodePtr node;
Packit Service a31ea6
    xmlURIPtr uri;
Packit Service a31ea6
    xmlChar *URL;
Packit Service a31ea6
    int i;
Packit Service a31ea6
    xmlChar *encoding = NULL;
Packit Service a31ea6
    xmlCharEncoding enc = (xmlCharEncoding) 0;
Packit Service a31ea6
    xmlParserCtxtPtr pctxt;
Packit Service a31ea6
    xmlParserInputPtr inputStream;
Packit Service a31ea6
    int xinclude_multibyte_fallback_used = 0;
Packit Service a31ea6
Packit Service a31ea6
    /*
Packit Service a31ea6
     * Check the URL and remove any fragment identifier
Packit Service a31ea6
     */
Packit Service a31ea6
    uri = xmlParseURI((const char *)url);
Packit Service a31ea6
    if (uri == NULL) {
Packit Service a31ea6
	xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref, XML_XINCLUDE_HREF_URI,
Packit Service a31ea6
	               "invalid value URI %s\n", url);
Packit Service a31ea6
	return(-1);
Packit Service a31ea6
    }
Packit Service a31ea6
    if (uri->fragment != NULL) {
Packit Service a31ea6
	xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref, XML_XINCLUDE_TEXT_FRAGMENT,
Packit Service a31ea6
	               "fragment identifier forbidden for text: %s\n",
Packit Service a31ea6
		       (const xmlChar *) uri->fragment);
Packit Service a31ea6
	xmlFreeURI(uri);
Packit Service a31ea6
	return(-1);
Packit Service a31ea6
    }
Packit Service a31ea6
    URL = xmlSaveUri(uri);
Packit Service a31ea6
    xmlFreeURI(uri);
Packit Service a31ea6
    if (URL == NULL) {
Packit Service a31ea6
	xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref, XML_XINCLUDE_HREF_URI,
Packit Service a31ea6
	               "invalid value URI %s\n", url);
Packit Service a31ea6
	return(-1);
Packit Service a31ea6
    }
Packit Service a31ea6
Packit Service a31ea6
    /*
Packit Service a31ea6
     * Handling of references to the local document are done
Packit Service a31ea6
     * directly through ctxt->doc.
Packit Service a31ea6
     */
Packit Service a31ea6
    if (URL[0] == 0) {
Packit Service a31ea6
	xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref,
Packit Service a31ea6
	               XML_XINCLUDE_TEXT_DOCUMENT,
Packit Service a31ea6
		       "text serialization of document not available\n", NULL);
Packit Service a31ea6
	xmlFree(URL);
Packit Service a31ea6
	return(-1);
Packit Service a31ea6
    }
Packit Service a31ea6
Packit Service a31ea6
    /*
Packit Service a31ea6
     * Prevent reloading twice the document.
Packit Service a31ea6
     */
Packit Service a31ea6
    for (i = 0; i < ctxt->txtNr; i++) {
Packit Service a31ea6
	if (xmlStrEqual(URL, ctxt->txturlTab[i])) {
Packit Service a31ea6
	    node = xmlCopyNode(ctxt->txtTab[i], 1);
Packit Service a31ea6
	    goto loaded;
Packit Service a31ea6
	}
Packit Service a31ea6
    }
Packit Service a31ea6
    /*
Packit Service a31ea6
     * Try to get the encoding if available
Packit Service a31ea6
     */
Packit Service a31ea6
    if ((ctxt->incTab[nr] != NULL) && (ctxt->incTab[nr]->ref != NULL)) {
Packit Service a31ea6
	encoding = xmlGetProp(ctxt->incTab[nr]->ref, XINCLUDE_PARSE_ENCODING);
Packit Service a31ea6
    }
Packit Service a31ea6
    if (encoding != NULL) {
Packit Service a31ea6
	/*
Packit Service a31ea6
	 * TODO: we should not have to remap to the xmlCharEncoding
Packit Service a31ea6
	 *       predefined set, a better interface than
Packit Service a31ea6
	 *       xmlParserInputBufferCreateFilename should allow any
Packit Service a31ea6
	 *       encoding supported by iconv
Packit Service a31ea6
	 */
Packit Service a31ea6
        enc = xmlParseCharEncoding((const char *) encoding);
Packit Service a31ea6
	if (enc == XML_CHAR_ENCODING_ERROR) {
Packit Service a31ea6
	    xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref,
Packit Service a31ea6
	                   XML_XINCLUDE_UNKNOWN_ENCODING,
Packit Service a31ea6
			   "encoding %s not supported\n", encoding);
Packit Service a31ea6
	    xmlFree(encoding);
Packit Service a31ea6
	    xmlFree(URL);
Packit Service a31ea6
	    return(-1);
Packit Service a31ea6
	}
Packit Service a31ea6
	xmlFree(encoding);
Packit Service a31ea6
    }
Packit Service a31ea6
Packit Service a31ea6
    /*
Packit Service a31ea6
     * Load it.
Packit Service a31ea6
     */
Packit Service a31ea6
    pctxt = xmlNewParserCtxt();
Packit Service a31ea6
    inputStream = xmlLoadExternalEntity((const char*)URL, NULL, pctxt);
Packit Service a31ea6
    if(inputStream == NULL) {
Packit Service a31ea6
	xmlFreeParserCtxt(pctxt);
Packit Service a31ea6
	xmlFree(URL);
Packit Service a31ea6
	return(-1);
Packit Service a31ea6
    }
Packit Service a31ea6
    buf = inputStream->buf;
Packit Service a31ea6
    if (buf == NULL) {
Packit Service a31ea6
	xmlFreeInputStream (inputStream);
Packit Service a31ea6
	xmlFreeParserCtxt(pctxt);
Packit Service a31ea6
	xmlFree(URL);
Packit Service a31ea6
	return(-1);
Packit Service a31ea6
    }
Packit Service a31ea6
    if (buf->encoder)
Packit Service a31ea6
	xmlCharEncCloseFunc(buf->encoder);
Packit Service a31ea6
    buf->encoder = xmlGetCharEncodingHandler(enc);
Packit Service a31ea6
    node = xmlNewText(NULL);
Packit Service a31ea6
Packit Service a31ea6
    /*
Packit Service a31ea6
     * Scan all chars from the resource and add the to the node
Packit Service a31ea6
     */
Packit Service a31ea6
xinclude_multibyte_fallback:
Packit Service a31ea6
    while (xmlParserInputBufferRead(buf, 128) > 0) {
Packit Service a31ea6
	int len;
Packit Service a31ea6
	const xmlChar *content;
Packit Service a31ea6
Packit Service a31ea6
	content = xmlBufContent(buf->buffer);
Packit Service a31ea6
	len = xmlBufLength(buf->buffer);
Packit Service a31ea6
	for (i = 0;i < len;) {
Packit Service a31ea6
	    int cur;
Packit Service a31ea6
	    int l;
Packit Service a31ea6
Packit Service a31ea6
	    cur = xmlStringCurrentChar(NULL, &content[i], &l);
Packit Service a31ea6
	    if (!IS_CHAR(cur)) {
Packit Service a31ea6
		/* Handle splitted multibyte char at buffer boundary */
Packit Service a31ea6
		if (((len - i) < 4) && (!xinclude_multibyte_fallback_used)) {
Packit Service a31ea6
		    xinclude_multibyte_fallback_used = 1;
Packit Service a31ea6
		    xmlBufShrink(buf->buffer, i);
Packit Service a31ea6
		    goto xinclude_multibyte_fallback;
Packit Service a31ea6
		} else {
Packit Service a31ea6
		    xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref,
Packit Service a31ea6
				   XML_XINCLUDE_INVALID_CHAR,
Packit Service a31ea6
				   "%s contains invalid char\n", URL);
Packit Service a31ea6
		    xmlFreeParserInputBuffer(buf);
Packit Service a31ea6
		    xmlFree(URL);
Packit Service a31ea6
		    return(-1);
Packit Service a31ea6
		}
Packit Service a31ea6
	    } else {
Packit Service a31ea6
		xinclude_multibyte_fallback_used = 0;
Packit Service a31ea6
		xmlNodeAddContentLen(node, &content[i], l);
Packit Service a31ea6
	    }
Packit Service a31ea6
	    i += l;
Packit Service a31ea6
	}
Packit Service a31ea6
	xmlBufShrink(buf->buffer, len);
Packit Service a31ea6
    }
Packit Service a31ea6
    xmlFreeParserCtxt(pctxt);
Packit Service a31ea6
    xmlXIncludeAddTxt(ctxt, node, URL);
Packit Service a31ea6
    xmlFreeInputStream(inputStream);
Packit Service a31ea6
Packit Service a31ea6
loaded:
Packit Service a31ea6
    /*
Packit Service a31ea6
     * Add the element as the replacement copy.
Packit Service a31ea6
     */
Packit Service a31ea6
    ctxt->incTab[nr]->inc = node;
Packit Service a31ea6
    xmlFree(URL);
Packit Service a31ea6
    return(0);
Packit Service a31ea6
}
Packit Service a31ea6
Packit Service a31ea6
/**
Packit Service a31ea6
 * xmlXIncludeLoadFallback:
Packit Service a31ea6
 * @ctxt:  the XInclude context
Packit Service a31ea6
 * @fallback:  the fallback node
Packit Service a31ea6
 * @nr:  the xinclude node number
Packit Service a31ea6
 *
Packit Service a31ea6
 * Load the content of the fallback node, and store the result
Packit Service a31ea6
 * in the XInclude context
Packit Service a31ea6
 *
Packit Service a31ea6
 * Returns 0 in case of success, -1 in case of failure
Packit Service a31ea6
 */
Packit Service a31ea6
static int
Packit Service a31ea6
xmlXIncludeLoadFallback(xmlXIncludeCtxtPtr ctxt, xmlNodePtr fallback, int nr) {
Packit Service a31ea6
    xmlXIncludeCtxtPtr newctxt;
Packit Service a31ea6
    int ret = 0;
Packit Service a31ea6
Packit Service a31ea6
    if ((fallback == NULL) || (fallback->type == XML_NAMESPACE_DECL) ||
Packit Service a31ea6
        (ctxt == NULL))
Packit Service a31ea6
	return(-1);
Packit Service a31ea6
    if (fallback->children != NULL) {
Packit Service a31ea6
	/*
Packit Service a31ea6
	 * It's possible that the fallback also has 'includes'
Packit Service a31ea6
	 * (Bug 129969), so we re-process the fallback just in case
Packit Service a31ea6
	 */
Packit Service a31ea6
	newctxt = xmlXIncludeNewContext(ctxt->doc);
Packit Service a31ea6
	if (newctxt == NULL)
Packit Service a31ea6
	    return (-1);
Packit Service a31ea6
	newctxt->_private = ctxt->_private;
Packit Service a31ea6
	newctxt->base = xmlStrdup(ctxt->base);	/* Inherit the base from the existing context */
Packit Service a31ea6
	xmlXIncludeSetFlags(newctxt, ctxt->parseFlags);
Packit Service a31ea6
	ret = xmlXIncludeDoProcess(newctxt, ctxt->doc, fallback->children);
Packit Service a31ea6
	if (ctxt->nbErrors > 0)
Packit Service a31ea6
	    ret = -1;
Packit Service a31ea6
	else if (ret > 0)
Packit Service a31ea6
	    ret = 0;	/* xmlXIncludeDoProcess can return +ve number */
Packit Service a31ea6
	xmlXIncludeFreeContext(newctxt);
Packit Service a31ea6
Packit Service a31ea6
	ctxt->incTab[nr]->inc = xmlDocCopyNodeList(ctxt->doc,
Packit Service a31ea6
	                                           fallback->children);
Packit Service a31ea6
    } else {
Packit Service a31ea6
        ctxt->incTab[nr]->inc = NULL;
Packit Service a31ea6
	ctxt->incTab[nr]->emptyFb = 1;	/* flag empty callback */
Packit Service a31ea6
    }
Packit Service a31ea6
    return(ret);
Packit Service a31ea6
}
Packit Service a31ea6
Packit Service a31ea6
/************************************************************************
Packit Service a31ea6
 *									*
Packit Service a31ea6
 *			XInclude Processing				*
Packit Service a31ea6
 *									*
Packit Service a31ea6
 ************************************************************************/
Packit Service a31ea6
Packit Service a31ea6
/**
Packit Service a31ea6
 * xmlXIncludePreProcessNode:
Packit Service a31ea6
 * @ctxt: an XInclude context
Packit Service a31ea6
 * @node: an XInclude node
Packit Service a31ea6
 *
Packit Service a31ea6
 * Implement the XInclude preprocessing, currently just adding the element
Packit Service a31ea6
 * for further processing.
Packit Service a31ea6
 *
Packit Service a31ea6
 * Returns the result list or NULL in case of error
Packit Service a31ea6
 */
Packit Service a31ea6
static xmlNodePtr
Packit Service a31ea6
xmlXIncludePreProcessNode(xmlXIncludeCtxtPtr ctxt, xmlNodePtr node) {
Packit Service a31ea6
    xmlXIncludeAddNode(ctxt, node);
Packit Service a31ea6
    return(NULL);
Packit Service a31ea6
}
Packit Service a31ea6
Packit Service a31ea6
/**
Packit Service a31ea6
 * xmlXIncludeLoadNode:
Packit Service a31ea6
 * @ctxt: an XInclude context
Packit Service a31ea6
 * @nr: the node number
Packit Service a31ea6
 *
Packit Service a31ea6
 * Find and load the infoset replacement for the given node.
Packit Service a31ea6
 *
Packit Service a31ea6
 * Returns 0 if substitution succeeded, -1 if some processing failed
Packit Service a31ea6
 */
Packit Service a31ea6
static int
Packit Service a31ea6
xmlXIncludeLoadNode(xmlXIncludeCtxtPtr ctxt, int nr) {
Packit Service a31ea6
    xmlNodePtr cur;
Packit Service a31ea6
    xmlChar *href;
Packit Service a31ea6
    xmlChar *parse;
Packit Service a31ea6
    xmlChar *base;
Packit Service a31ea6
    xmlChar *oldBase;
Packit Service a31ea6
    xmlChar *URI;
Packit Service a31ea6
    int xml = 1; /* default Issue 64 */
Packit Service a31ea6
    int ret;
Packit Service a31ea6
Packit Service a31ea6
    if (ctxt == NULL)
Packit Service a31ea6
	return(-1);
Packit Service a31ea6
    if ((nr < 0) || (nr >= ctxt->incNr))
Packit Service a31ea6
	return(-1);
Packit Service a31ea6
    cur = ctxt->incTab[nr]->ref;
Packit Service a31ea6
    if (cur == NULL)
Packit Service a31ea6
	return(-1);
Packit Service a31ea6
Packit Service a31ea6
    /*
Packit Service a31ea6
     * read the attributes
Packit Service a31ea6
     */
Packit Service a31ea6
    href = xmlXIncludeGetProp(ctxt, cur, XINCLUDE_HREF);
Packit Service a31ea6
    if (href == NULL) {
Packit Service a31ea6
	href = xmlStrdup(BAD_CAST ""); /* @@@@ href is now optional */
Packit Service a31ea6
	if (href == NULL)
Packit Service a31ea6
	    return(-1);
Packit Service a31ea6
    }
Packit Service a31ea6
    parse = xmlXIncludeGetProp(ctxt, cur, XINCLUDE_PARSE);
Packit Service a31ea6
    if (parse != NULL) {
Packit Service a31ea6
	if (xmlStrEqual(parse, XINCLUDE_PARSE_XML))
Packit Service a31ea6
	    xml = 1;
Packit Service a31ea6
	else if (xmlStrEqual(parse, XINCLUDE_PARSE_TEXT))
Packit Service a31ea6
	    xml = 0;
Packit Service a31ea6
	else {
Packit Service a31ea6
	    xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref,
Packit Service a31ea6
	                   XML_XINCLUDE_PARSE_VALUE,
Packit Service a31ea6
			   "invalid value %s for 'parse'\n", parse);
Packit Service a31ea6
	    if (href != NULL)
Packit Service a31ea6
		xmlFree(href);
Packit Service a31ea6
	    if (parse != NULL)
Packit Service a31ea6
		xmlFree(parse);
Packit Service a31ea6
	    return(-1);
Packit Service a31ea6
	}
Packit Service a31ea6
    }
Packit Service a31ea6
Packit Service a31ea6
    /*
Packit Service a31ea6
     * compute the URI
Packit Service a31ea6
     */
Packit Service a31ea6
    base = xmlNodeGetBase(ctxt->doc, cur);
Packit Service a31ea6
    if (base == NULL) {
Packit Service a31ea6
	URI = xmlBuildURI(href, ctxt->doc->URL);
Packit Service a31ea6
    } else {
Packit Service a31ea6
	URI = xmlBuildURI(href, base);
Packit Service a31ea6
    }
Packit Service a31ea6
    if (URI == NULL) {
Packit Service a31ea6
	xmlChar *escbase;
Packit Service a31ea6
	xmlChar *eschref;
Packit Service a31ea6
	/*
Packit Service a31ea6
	 * Some escaping may be needed
Packit Service a31ea6
	 */
Packit Service a31ea6
	escbase = xmlURIEscape(base);
Packit Service a31ea6
	eschref = xmlURIEscape(href);
Packit Service a31ea6
	URI = xmlBuildURI(eschref, escbase);
Packit Service a31ea6
	if (escbase != NULL)
Packit Service a31ea6
	    xmlFree(escbase);
Packit Service a31ea6
	if (eschref != NULL)
Packit Service a31ea6
	    xmlFree(eschref);
Packit Service a31ea6
    }
Packit Service a31ea6
    if (URI == NULL) {
Packit Service a31ea6
	xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref,
Packit Service a31ea6
	               XML_XINCLUDE_HREF_URI, "failed build URL\n", NULL);
Packit Service a31ea6
	if (parse != NULL)
Packit Service a31ea6
	    xmlFree(parse);
Packit Service a31ea6
	if (href != NULL)
Packit Service a31ea6
	    xmlFree(href);
Packit Service a31ea6
	if (base != NULL)
Packit Service a31ea6
	    xmlFree(base);
Packit Service a31ea6
	return(-1);
Packit Service a31ea6
    }
Packit Service a31ea6
#ifdef DEBUG_XINCLUDE
Packit Service a31ea6
    xmlGenericError(xmlGenericErrorContext, "parse: %s\n",
Packit Service a31ea6
	    xml ? "xml": "text");
Packit Service a31ea6
    xmlGenericError(xmlGenericErrorContext, "URI: %s\n", URI);
Packit Service a31ea6
#endif
Packit Service a31ea6
Packit Service a31ea6
    /*
Packit Service a31ea6
     * Save the base for this include (saving the current one)
Packit Service a31ea6
     */
Packit Service a31ea6
    oldBase = ctxt->base;
Packit Service a31ea6
    ctxt->base = base;
Packit Service a31ea6
Packit Service a31ea6
    if (xml) {
Packit Service a31ea6
	ret = xmlXIncludeLoadDoc(ctxt, URI, nr);
Packit Service a31ea6
	/* xmlXIncludeGetFragment(ctxt, cur, URI); */
Packit Service a31ea6
    } else {
Packit Service a31ea6
	ret = xmlXIncludeLoadTxt(ctxt, URI, nr);
Packit Service a31ea6
    }
Packit Service a31ea6
Packit Service a31ea6
    /*
Packit Service a31ea6
     * Restore the original base before checking for fallback
Packit Service a31ea6
     */
Packit Service a31ea6
    ctxt->base = oldBase;
Packit Service a31ea6
Packit Service a31ea6
    if (ret < 0) {
Packit Service a31ea6
	xmlNodePtr children;
Packit Service a31ea6
Packit Service a31ea6
	/*
Packit Service a31ea6
	 * Time to try a fallback if availble
Packit Service a31ea6
	 */
Packit Service a31ea6
#ifdef DEBUG_XINCLUDE
Packit Service a31ea6
	xmlGenericError(xmlGenericErrorContext, "error looking for fallback\n");
Packit Service a31ea6
#endif
Packit Service a31ea6
	children = cur->children;
Packit Service a31ea6
	while (children != NULL) {
Packit Service a31ea6
	    if ((children->type == XML_ELEMENT_NODE) &&
Packit Service a31ea6
		(children->ns != NULL) &&
Packit Service a31ea6
		(xmlStrEqual(children->name, XINCLUDE_FALLBACK)) &&
Packit Service a31ea6
		((xmlStrEqual(children->ns->href, XINCLUDE_NS)) ||
Packit Service a31ea6
		 (xmlStrEqual(children->ns->href, XINCLUDE_OLD_NS)))) {
Packit Service a31ea6
		ret = xmlXIncludeLoadFallback(ctxt, children, nr);
Packit Service a31ea6
		if (ret == 0)
Packit Service a31ea6
		    break;
Packit Service a31ea6
	    }
Packit Service a31ea6
	    children = children->next;
Packit Service a31ea6
	}
Packit Service a31ea6
    }
Packit Service a31ea6
    if (ret < 0) {
Packit Service a31ea6
	xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref,
Packit Service a31ea6
	               XML_XINCLUDE_NO_FALLBACK,
Packit Service a31ea6
		       "could not load %s, and no fallback was found\n",
Packit Service a31ea6
		       URI);
Packit Service a31ea6
    }
Packit Service a31ea6
Packit Service a31ea6
    /*
Packit Service a31ea6
     * Cleanup
Packit Service a31ea6
     */
Packit Service a31ea6
    if (URI != NULL)
Packit Service a31ea6
	xmlFree(URI);
Packit Service a31ea6
    if (parse != NULL)
Packit Service a31ea6
	xmlFree(parse);
Packit Service a31ea6
    if (href != NULL)
Packit Service a31ea6
	xmlFree(href);
Packit Service a31ea6
    if (base != NULL)
Packit Service a31ea6
	xmlFree(base);
Packit Service a31ea6
    return(0);
Packit Service a31ea6
}
Packit Service a31ea6
Packit Service a31ea6
/**
Packit Service a31ea6
 * xmlXIncludeIncludeNode:
Packit Service a31ea6
 * @ctxt: an XInclude context
Packit Service a31ea6
 * @nr: the node number
Packit Service a31ea6
 *
Packit Service a31ea6
 * Inplement the infoset replacement for the given node
Packit Service a31ea6
 *
Packit Service a31ea6
 * Returns 0 if substitution succeeded, -1 if some processing failed
Packit Service a31ea6
 */
Packit Service a31ea6
static int
Packit Service a31ea6
xmlXIncludeIncludeNode(xmlXIncludeCtxtPtr ctxt, int nr) {
Packit Service a31ea6
    xmlNodePtr cur, end, list, tmp;
Packit Service a31ea6
Packit Service a31ea6
    if (ctxt == NULL)
Packit Service a31ea6
	return(-1);
Packit Service a31ea6
    if ((nr < 0) || (nr >= ctxt->incNr))
Packit Service a31ea6
	return(-1);
Packit Service a31ea6
    cur = ctxt->incTab[nr]->ref;
Packit Service a31ea6
    if ((cur == NULL) || (cur->type == XML_NAMESPACE_DECL))
Packit Service a31ea6
	return(-1);
Packit Service a31ea6
Packit Service a31ea6
    /*
Packit Service a31ea6
     * If we stored an XPointer a late computation may be needed
Packit Service a31ea6
     */
Packit Service a31ea6
    if ((ctxt->incTab[nr]->inc == NULL) &&
Packit Service a31ea6
	(ctxt->incTab[nr]->xptr != NULL)) {
Packit Service a31ea6
	ctxt->incTab[nr]->inc =
Packit Service a31ea6
	    xmlXIncludeCopyXPointer(ctxt, ctxt->doc, ctxt->doc,
Packit Service a31ea6
		                    ctxt->incTab[nr]->xptr);
Packit Service a31ea6
	xmlXPathFreeObject(ctxt->incTab[nr]->xptr);
Packit Service a31ea6
	ctxt->incTab[nr]->xptr = NULL;
Packit Service a31ea6
    }
Packit Service a31ea6
    list = ctxt->incTab[nr]->inc;
Packit Service a31ea6
    ctxt->incTab[nr]->inc = NULL;
Packit Service a31ea6
Packit Service a31ea6
    /*
Packit Service a31ea6
     * Check against the risk of generating a multi-rooted document
Packit Service a31ea6
     */
Packit Service a31ea6
    if ((cur->parent != NULL) &&
Packit Service a31ea6
	(cur->parent->type != XML_ELEMENT_NODE)) {
Packit Service a31ea6
	int nb_elem = 0;
Packit Service a31ea6
Packit Service a31ea6
	tmp = list;
Packit Service a31ea6
	while (tmp != NULL) {
Packit Service a31ea6
	    if (tmp->type == XML_ELEMENT_NODE)
Packit Service a31ea6
		nb_elem++;
Packit Service a31ea6
	    tmp = tmp->next;
Packit Service a31ea6
	}
Packit Service a31ea6
	if (nb_elem > 1) {
Packit Service a31ea6
	    xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref,
Packit Service a31ea6
	                   XML_XINCLUDE_MULTIPLE_ROOT,
Packit Service a31ea6
		       "XInclude error: would result in multiple root nodes\n",
Packit Service a31ea6
			   NULL);
Packit Service a31ea6
	    return(-1);
Packit Service a31ea6
	}
Packit Service a31ea6
    }
Packit Service a31ea6
Packit Service a31ea6
    if (ctxt->parseFlags & XML_PARSE_NOXINCNODE) {
Packit Service a31ea6
	/*
Packit Service a31ea6
	 * Add the list of nodes
Packit Service a31ea6
	 */
Packit Service a31ea6
	while (list != NULL) {
Packit Service a31ea6
	    end = list;
Packit Service a31ea6
	    list = list->next;
Packit Service a31ea6
Packit Service a31ea6
	    xmlAddPrevSibling(cur, end);
Packit Service a31ea6
	}
Packit Service a31ea6
	xmlUnlinkNode(cur);
Packit Service a31ea6
	xmlFreeNode(cur);
Packit Service a31ea6
    } else {
Packit Service a31ea6
	/*
Packit Service a31ea6
	 * Change the current node as an XInclude start one, and add an
Packit Service a31ea6
	 * XInclude end one
Packit Service a31ea6
	 */
Packit Service a31ea6
	cur->type = XML_XINCLUDE_START;
Packit Service a31ea6
	end = xmlNewDocNode(cur->doc, cur->ns, cur->name, NULL);
Packit Service a31ea6
	if (end == NULL) {
Packit Service a31ea6
	    xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref,
Packit Service a31ea6
	                   XML_XINCLUDE_BUILD_FAILED,
Packit Service a31ea6
			   "failed to build node\n", NULL);
Packit Service a31ea6
	    return(-1);
Packit Service a31ea6
	}
Packit Service a31ea6
	end->type = XML_XINCLUDE_END;
Packit Service a31ea6
	xmlAddNextSibling(cur, end);
Packit Service a31ea6
Packit Service a31ea6
	/*
Packit Service a31ea6
	 * Add the list of nodes
Packit Service a31ea6
	 */
Packit Service a31ea6
	while (list != NULL) {
Packit Service a31ea6
	    cur = list;
Packit Service a31ea6
	    list = list->next;
Packit Service a31ea6
Packit Service a31ea6
	    xmlAddPrevSibling(end, cur);
Packit Service a31ea6
	}
Packit Service a31ea6
    }
Packit Service a31ea6
Packit Service a31ea6
Packit Service a31ea6
    return(0);
Packit Service a31ea6
}
Packit Service a31ea6
Packit Service a31ea6
/**
Packit Service a31ea6
 * xmlXIncludeTestNode:
Packit Service a31ea6
 * @ctxt: the XInclude processing context
Packit Service a31ea6
 * @node: an XInclude node
Packit Service a31ea6
 *
Packit Service a31ea6
 * test if the node is an XInclude node
Packit Service a31ea6
 *
Packit Service a31ea6
 * Returns 1 true, 0 otherwise
Packit Service a31ea6
 */
Packit Service a31ea6
static int
Packit Service a31ea6
xmlXIncludeTestNode(xmlXIncludeCtxtPtr ctxt, xmlNodePtr node) {
Packit Service a31ea6
    if (node == NULL)
Packit Service a31ea6
	return(0);
Packit Service a31ea6
    if (node->type != XML_ELEMENT_NODE)
Packit Service a31ea6
	return(0);
Packit Service a31ea6
    if (node->ns == NULL)
Packit Service a31ea6
	return(0);
Packit Service a31ea6
    if ((xmlStrEqual(node->ns->href, XINCLUDE_NS)) ||
Packit Service a31ea6
        (xmlStrEqual(node->ns->href, XINCLUDE_OLD_NS))) {
Packit Service a31ea6
	if (xmlStrEqual(node->ns->href, XINCLUDE_OLD_NS)) {
Packit Service a31ea6
	    if (ctxt->legacy == 0) {
Packit Service a31ea6
#if 0 /* wait for the XML Core Working Group to get something stable ! */
Packit Service a31ea6
		xmlXIncludeWarn(ctxt, node, XML_XINCLUDE_DEPRECATED_NS,
Packit Service a31ea6
	               "Deprecated XInclude namespace found, use %s",
Packit Service a31ea6
		                XINCLUDE_NS);
Packit Service a31ea6
#endif
Packit Service a31ea6
	        ctxt->legacy = 1;
Packit Service a31ea6
	    }
Packit Service a31ea6
	}
Packit Service a31ea6
	if (xmlStrEqual(node->name, XINCLUDE_NODE)) {
Packit Service a31ea6
	    xmlNodePtr child = node->children;
Packit Service a31ea6
	    int nb_fallback = 0;
Packit Service a31ea6
Packit Service a31ea6
	    while (child != NULL) {
Packit Service a31ea6
		if ((child->type == XML_ELEMENT_NODE) &&
Packit Service a31ea6
		    (child->ns != NULL) &&
Packit Service a31ea6
		    ((xmlStrEqual(child->ns->href, XINCLUDE_NS)) ||
Packit Service a31ea6
		     (xmlStrEqual(child->ns->href, XINCLUDE_OLD_NS)))) {
Packit Service a31ea6
		    if (xmlStrEqual(child->name, XINCLUDE_NODE)) {
Packit Service a31ea6
			xmlXIncludeErr(ctxt, node,
Packit Service a31ea6
			               XML_XINCLUDE_INCLUDE_IN_INCLUDE,
Packit Service a31ea6
				       "%s has an 'include' child\n",
Packit Service a31ea6
				       XINCLUDE_NODE);
Packit Service a31ea6
			return(0);
Packit Service a31ea6
		    }
Packit Service a31ea6
		    if (xmlStrEqual(child->name, XINCLUDE_FALLBACK)) {
Packit Service a31ea6
			nb_fallback++;
Packit Service a31ea6
		    }
Packit Service a31ea6
		}
Packit Service a31ea6
		child = child->next;
Packit Service a31ea6
	    }
Packit Service a31ea6
	    if (nb_fallback > 1) {
Packit Service a31ea6
		xmlXIncludeErr(ctxt, node, XML_XINCLUDE_FALLBACKS_IN_INCLUDE,
Packit Service a31ea6
			       "%s has multiple fallback children\n",
Packit Service a31ea6
		               XINCLUDE_NODE);
Packit Service a31ea6
		return(0);
Packit Service a31ea6
	    }
Packit Service a31ea6
	    return(1);
Packit Service a31ea6
	}
Packit Service a31ea6
	if (xmlStrEqual(node->name, XINCLUDE_FALLBACK)) {
Packit Service a31ea6
	    if ((node->parent == NULL) ||
Packit Service a31ea6
		(node->parent->type != XML_ELEMENT_NODE) ||
Packit Service a31ea6
		(node->parent->ns == NULL) ||
Packit Service a31ea6
		((!xmlStrEqual(node->parent->ns->href, XINCLUDE_NS)) &&
Packit Service a31ea6
		 (!xmlStrEqual(node->parent->ns->href, XINCLUDE_OLD_NS))) ||
Packit Service a31ea6
		(!xmlStrEqual(node->parent->name, XINCLUDE_NODE))) {
Packit Service a31ea6
		xmlXIncludeErr(ctxt, node,
Packit Service a31ea6
		               XML_XINCLUDE_FALLBACK_NOT_IN_INCLUDE,
Packit Service a31ea6
			       "%s is not the child of an 'include'\n",
Packit Service a31ea6
			       XINCLUDE_FALLBACK);
Packit Service a31ea6
	    }
Packit Service a31ea6
	}
Packit Service a31ea6
    }
Packit Service a31ea6
    return(0);
Packit Service a31ea6
}
Packit Service a31ea6
Packit Service a31ea6
/**
Packit Service a31ea6
 * xmlXIncludeDoProcess:
Packit Service a31ea6
 * @ctxt: the XInclude processing context
Packit Service a31ea6
 * @doc: an XML document
Packit Service a31ea6
 * @tree: the top of the tree to process
Packit Service a31ea6
 *
Packit Service a31ea6
 * Implement the XInclude substitution on the XML document @doc
Packit Service a31ea6
 *
Packit Service a31ea6
 * Returns 0 if no substitution were done, -1 if some processing failed
Packit Service a31ea6
 *    or the number of substitutions done.
Packit Service a31ea6
 */
Packit Service a31ea6
static int
Packit Service a31ea6
xmlXIncludeDoProcess(xmlXIncludeCtxtPtr ctxt, xmlDocPtr doc, xmlNodePtr tree) {
Packit Service a31ea6
    xmlNodePtr cur;
Packit Service a31ea6
    int ret = 0;
Packit Service a31ea6
    int i, start;
Packit Service a31ea6
Packit Service a31ea6
    if ((doc == NULL) || (tree == NULL) || (tree->type == XML_NAMESPACE_DECL))
Packit Service a31ea6
	return(-1);
Packit Service a31ea6
    if (ctxt == NULL)
Packit Service a31ea6
	return(-1);
Packit Service a31ea6
Packit Service a31ea6
    if (doc->URL != NULL) {
Packit Service a31ea6
	ret = xmlXIncludeURLPush(ctxt, doc->URL);
Packit Service a31ea6
	if (ret < 0)
Packit Service a31ea6
	    return(-1);
Packit Service a31ea6
    }
Packit Service a31ea6
    start = ctxt->incNr;
Packit Service a31ea6
Packit Service a31ea6
    /*
Packit Service a31ea6
     * First phase: lookup the elements in the document
Packit Service a31ea6
     */
Packit Service a31ea6
    cur = tree;
Packit Service a31ea6
    if (xmlXIncludeTestNode(ctxt, cur) == 1)
Packit Service a31ea6
	xmlXIncludePreProcessNode(ctxt, cur);
Packit Service a31ea6
    while ((cur != NULL) && (cur != tree->parent)) {
Packit Service a31ea6
	/* TODO: need to work on entities -> stack */
Packit Service a31ea6
	if ((cur->children != NULL) &&
Packit Service a31ea6
	    (cur->children->type != XML_ENTITY_DECL) &&
Packit Service a31ea6
	    (cur->children->type != XML_XINCLUDE_START) &&
Packit Service a31ea6
	    (cur->children->type != XML_XINCLUDE_END)) {
Packit Service a31ea6
	    cur = cur->children;
Packit Service a31ea6
	    if (xmlXIncludeTestNode(ctxt, cur))
Packit Service a31ea6
		xmlXIncludePreProcessNode(ctxt, cur);
Packit Service a31ea6
	} else if (cur->next != NULL) {
Packit Service a31ea6
	    cur = cur->next;
Packit Service a31ea6
	    if (xmlXIncludeTestNode(ctxt, cur))
Packit Service a31ea6
		xmlXIncludePreProcessNode(ctxt, cur);
Packit Service a31ea6
	} else {
Packit Service a31ea6
	    if (cur == tree)
Packit Service a31ea6
	        break;
Packit Service a31ea6
	    do {
Packit Service a31ea6
		cur = cur->parent;
Packit Service a31ea6
		if ((cur == NULL) || (cur == tree->parent))
Packit Service a31ea6
		    break; /* do */
Packit Service a31ea6
		if (cur->next != NULL) {
Packit Service a31ea6
		    cur = cur->next;
Packit Service a31ea6
		    if (xmlXIncludeTestNode(ctxt, cur))
Packit Service a31ea6
			xmlXIncludePreProcessNode(ctxt, cur);
Packit Service a31ea6
		    break; /* do */
Packit Service a31ea6
		}
Packit Service a31ea6
	    } while (cur != NULL);
Packit Service a31ea6
	}
Packit Service a31ea6
    }
Packit Service a31ea6
Packit Service a31ea6
    /*
Packit Service a31ea6
     * Second Phase : collect the infosets fragments
Packit Service a31ea6
     */
Packit Service a31ea6
    for (i = start;i < ctxt->incNr; i++) {
Packit Service a31ea6
        xmlXIncludeLoadNode(ctxt, i);
Packit Service a31ea6
	ret++;
Packit Service a31ea6
    }
Packit Service a31ea6
Packit Service a31ea6
    /*
Packit Service a31ea6
     * Third phase: extend the original document infoset.
Packit Service a31ea6
     *
Packit Service a31ea6
     * Originally we bypassed the inclusion if there were any errors
Packit Service a31ea6
     * encountered on any of the XIncludes.  A bug was raised (bug
Packit Service a31ea6
     * 132588) requesting that we output the XIncludes without error,
Packit Service a31ea6
     * so the check for inc!=NULL || xptr!=NULL was put in.  This may
Packit Service a31ea6
     * give some other problems in the future, but for now it seems to
Packit Service a31ea6
     * work ok.
Packit Service a31ea6
     *
Packit Service a31ea6
     */
Packit Service a31ea6
    for (i = ctxt->incBase;i < ctxt->incNr; i++) {
Packit Service a31ea6
	if ((ctxt->incTab[i]->inc != NULL) ||
Packit Service a31ea6
		(ctxt->incTab[i]->xptr != NULL) ||
Packit Service a31ea6
		(ctxt->incTab[i]->emptyFb != 0))	/* (empty fallback) */
Packit Service a31ea6
	    xmlXIncludeIncludeNode(ctxt, i);
Packit Service a31ea6
    }
Packit Service a31ea6
Packit Service a31ea6
    if (doc->URL != NULL)
Packit Service a31ea6
	xmlXIncludeURLPop(ctxt);
Packit Service a31ea6
    return(ret);
Packit Service a31ea6
}
Packit Service a31ea6
Packit Service a31ea6
/**
Packit Service a31ea6
 * xmlXIncludeSetFlags:
Packit Service a31ea6
 * @ctxt:  an XInclude processing context
Packit Service a31ea6
 * @flags: a set of xmlParserOption used for parsing XML includes
Packit Service a31ea6
 *
Packit Service a31ea6
 * Set the flags used for further processing of XML resources.
Packit Service a31ea6
 *
Packit Service a31ea6
 * Returns 0 in case of success and -1 in case of error.
Packit Service a31ea6
 */
Packit Service a31ea6
int
Packit Service a31ea6
xmlXIncludeSetFlags(xmlXIncludeCtxtPtr ctxt, int flags) {
Packit Service a31ea6
    if (ctxt == NULL)
Packit Service a31ea6
        return(-1);
Packit Service a31ea6
    ctxt->parseFlags = flags;
Packit Service a31ea6
    return(0);
Packit Service a31ea6
}
Packit Service a31ea6
Packit Service a31ea6
/**
Packit Service a31ea6
 * xmlXIncludeProcessTreeFlagsData:
Packit Service a31ea6
 * @tree: an XML node
Packit Service a31ea6
 * @flags: a set of xmlParserOption used for parsing XML includes
Packit Service a31ea6
 * @data: application data that will be passed to the parser context
Packit Service a31ea6
 *        in the _private field of the parser context(s)
Packit Service a31ea6
 *
Packit Service a31ea6
 * Implement the XInclude substitution on the XML node @tree
Packit Service a31ea6
 *
Packit Service a31ea6
 * Returns 0 if no substitution were done, -1 if some processing failed
Packit Service a31ea6
 *    or the number of substitutions done.
Packit Service a31ea6
 */
Packit Service a31ea6
Packit Service a31ea6
int
Packit Service a31ea6
xmlXIncludeProcessTreeFlagsData(xmlNodePtr tree, int flags, void *data) {
Packit Service a31ea6
    xmlXIncludeCtxtPtr ctxt;
Packit Service a31ea6
    int ret = 0;
Packit Service a31ea6
Packit Service a31ea6
    if ((tree == NULL) || (tree->type == XML_NAMESPACE_DECL) ||
Packit Service a31ea6
        (tree->doc == NULL))
Packit Service a31ea6
        return(-1);
Packit Service a31ea6
Packit Service a31ea6
    ctxt = xmlXIncludeNewContext(tree->doc);
Packit Service a31ea6
    if (ctxt == NULL)
Packit Service a31ea6
        return(-1);
Packit Service a31ea6
    ctxt->_private = data;
Packit Service a31ea6
    ctxt->base = xmlStrdup((xmlChar *)tree->doc->URL);
Packit Service a31ea6
    xmlXIncludeSetFlags(ctxt, flags);
Packit Service a31ea6
    ret = xmlXIncludeDoProcess(ctxt, tree->doc, tree);
Packit Service a31ea6
    if ((ret >= 0) && (ctxt->nbErrors > 0))
Packit Service a31ea6
        ret = -1;
Packit Service a31ea6
Packit Service a31ea6
    xmlXIncludeFreeContext(ctxt);
Packit Service a31ea6
    return(ret);
Packit Service a31ea6
}
Packit Service a31ea6
Packit Service a31ea6
/**
Packit Service a31ea6
 * xmlXIncludeProcessFlagsData:
Packit Service a31ea6
 * @doc: an XML document
Packit Service a31ea6
 * @flags: a set of xmlParserOption used for parsing XML includes
Packit Service a31ea6
 * @data: application data that will be passed to the parser context
Packit Service a31ea6
 *        in the _private field of the parser context(s)
Packit Service a31ea6
 *
Packit Service a31ea6
 * Implement the XInclude substitution on the XML document @doc
Packit Service a31ea6
 *
Packit Service a31ea6
 * Returns 0 if no substitution were done, -1 if some processing failed
Packit Service a31ea6
 *    or the number of substitutions done.
Packit Service a31ea6
 */
Packit Service a31ea6
int
Packit Service a31ea6
xmlXIncludeProcessFlagsData(xmlDocPtr doc, int flags, void *data) {
Packit Service a31ea6
    xmlNodePtr tree;
Packit Service a31ea6
Packit Service a31ea6
    if (doc == NULL)
Packit Service a31ea6
	return(-1);
Packit Service a31ea6
    tree = xmlDocGetRootElement(doc);
Packit Service a31ea6
    if (tree == NULL)
Packit Service a31ea6
	return(-1);
Packit Service a31ea6
    return(xmlXIncludeProcessTreeFlagsData(tree, flags, data));
Packit Service a31ea6
}
Packit Service a31ea6
Packit Service a31ea6
/**
Packit Service a31ea6
 * xmlXIncludeProcessFlags:
Packit Service a31ea6
 * @doc: an XML document
Packit Service a31ea6
 * @flags: a set of xmlParserOption used for parsing XML includes
Packit Service a31ea6
 *
Packit Service a31ea6
 * Implement the XInclude substitution on the XML document @doc
Packit Service a31ea6
 *
Packit Service a31ea6
 * Returns 0 if no substitution were done, -1 if some processing failed
Packit Service a31ea6
 *    or the number of substitutions done.
Packit Service a31ea6
 */
Packit Service a31ea6
int
Packit Service a31ea6
xmlXIncludeProcessFlags(xmlDocPtr doc, int flags) {
Packit Service a31ea6
    return xmlXIncludeProcessFlagsData(doc, flags, NULL);
Packit Service a31ea6
}
Packit Service a31ea6
Packit Service a31ea6
/**
Packit Service a31ea6
 * xmlXIncludeProcess:
Packit Service a31ea6
 * @doc: an XML document
Packit Service a31ea6
 *
Packit Service a31ea6
 * Implement the XInclude substitution on the XML document @doc
Packit Service a31ea6
 *
Packit Service a31ea6
 * Returns 0 if no substitution were done, -1 if some processing failed
Packit Service a31ea6
 *    or the number of substitutions done.
Packit Service a31ea6
 */
Packit Service a31ea6
int
Packit Service a31ea6
xmlXIncludeProcess(xmlDocPtr doc) {
Packit Service a31ea6
    return(xmlXIncludeProcessFlags(doc, 0));
Packit Service a31ea6
}
Packit Service a31ea6
Packit Service a31ea6
/**
Packit Service a31ea6
 * xmlXIncludeProcessTreeFlags:
Packit Service a31ea6
 * @tree: a node in an XML document
Packit Service a31ea6
 * @flags: a set of xmlParserOption used for parsing XML includes
Packit Service a31ea6
 *
Packit Service a31ea6
 * Implement the XInclude substitution for the given subtree
Packit Service a31ea6
 *
Packit Service a31ea6
 * Returns 0 if no substitution were done, -1 if some processing failed
Packit Service a31ea6
 *    or the number of substitutions done.
Packit Service a31ea6
 */
Packit Service a31ea6
int
Packit Service a31ea6
xmlXIncludeProcessTreeFlags(xmlNodePtr tree, int flags) {
Packit Service a31ea6
    xmlXIncludeCtxtPtr ctxt;
Packit Service a31ea6
    int ret = 0;
Packit Service a31ea6
Packit Service a31ea6
    if ((tree == NULL) || (tree->type == XML_NAMESPACE_DECL) ||
Packit Service a31ea6
        (tree->doc == NULL))
Packit Service a31ea6
	return(-1);
Packit Service a31ea6
    ctxt = xmlXIncludeNewContext(tree->doc);
Packit Service a31ea6
    if (ctxt == NULL)
Packit Service a31ea6
	return(-1);
Packit Service a31ea6
    ctxt->base = xmlNodeGetBase(tree->doc, tree);
Packit Service a31ea6
    xmlXIncludeSetFlags(ctxt, flags);
Packit Service a31ea6
    ret = xmlXIncludeDoProcess(ctxt, tree->doc, tree);
Packit Service a31ea6
    if ((ret >= 0) && (ctxt->nbErrors > 0))
Packit Service a31ea6
	ret = -1;
Packit Service a31ea6
Packit Service a31ea6
    xmlXIncludeFreeContext(ctxt);
Packit Service a31ea6
    return(ret);
Packit Service a31ea6
}
Packit Service a31ea6
Packit Service a31ea6
/**
Packit Service a31ea6
 * xmlXIncludeProcessTree:
Packit Service a31ea6
 * @tree: a node in an XML document
Packit Service a31ea6
 *
Packit Service a31ea6
 * Implement the XInclude substitution for the given subtree
Packit Service a31ea6
 *
Packit Service a31ea6
 * Returns 0 if no substitution were done, -1 if some processing failed
Packit Service a31ea6
 *    or the number of substitutions done.
Packit Service a31ea6
 */
Packit Service a31ea6
int
Packit Service a31ea6
xmlXIncludeProcessTree(xmlNodePtr tree) {
Packit Service a31ea6
    return(xmlXIncludeProcessTreeFlags(tree, 0));
Packit Service a31ea6
}
Packit Service a31ea6
Packit Service a31ea6
/**
Packit Service a31ea6
 * xmlXIncludeProcessNode:
Packit Service a31ea6
 * @ctxt: an existing XInclude context
Packit Service a31ea6
 * @node: a node in an XML document
Packit Service a31ea6
 *
Packit Service a31ea6
 * Implement the XInclude substitution for the given subtree reusing
Packit Service a31ea6
 * the informations and data coming from the given context.
Packit Service a31ea6
 *
Packit Service a31ea6
 * Returns 0 if no substitution were done, -1 if some processing failed
Packit Service a31ea6
 *    or the number of substitutions done.
Packit Service a31ea6
 */
Packit Service a31ea6
int
Packit Service a31ea6
xmlXIncludeProcessNode(xmlXIncludeCtxtPtr ctxt, xmlNodePtr node) {
Packit Service a31ea6
    int ret = 0;
Packit Service a31ea6
Packit Service a31ea6
    if ((node == NULL) || (node->type == XML_NAMESPACE_DECL) ||
Packit Service a31ea6
        (node->doc == NULL) || (ctxt == NULL))
Packit Service a31ea6
	return(-1);
Packit Service a31ea6
    ret = xmlXIncludeDoProcess(ctxt, node->doc, node);
Packit Service a31ea6
    if ((ret >= 0) && (ctxt->nbErrors > 0))
Packit Service a31ea6
	ret = -1;
Packit Service a31ea6
    return(ret);
Packit Service a31ea6
}
Packit Service a31ea6
Packit Service a31ea6
#else /* !LIBXML_XINCLUDE_ENABLED */
Packit Service a31ea6
#endif
Packit Service a31ea6
#define bottom_xinclude
Packit Service a31ea6
#include "elfgcchack.h"