Blame xpointer.c

Packit Service a31ea6
/*
Packit Service a31ea6
 * xpointer.c : Code to handle XML Pointer
Packit Service a31ea6
 *
Packit Service a31ea6
 * Base implementation was made accordingly to
Packit Service a31ea6
 * W3C Candidate Recommendation 7 June 2000
Packit Service a31ea6
 * http://www.w3.org/TR/2000/CR-xptr-20000607
Packit Service a31ea6
 *
Packit Service a31ea6
 * Added support for the element() scheme described in:
Packit Service a31ea6
 * W3C Proposed Recommendation 13 November 2002
Packit Service a31ea6
 * http://www.w3.org/TR/2002/PR-xptr-element-20021113/
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
/* To avoid EBCDIC trouble when parsing on zOS */
Packit Service a31ea6
#if defined(__MVS__)
Packit Service a31ea6
#pragma convert("ISO8859-1")
Packit Service a31ea6
#endif
Packit Service a31ea6
Packit Service a31ea6
#define IN_LIBXML
Packit Service a31ea6
#include "libxml.h"
Packit Service a31ea6
Packit Service a31ea6
/*
Packit Service a31ea6
 * TODO: better handling of error cases, the full expression should
Packit Service a31ea6
 *       be parsed beforehand instead of a progressive evaluation
Packit Service a31ea6
 * TODO: Access into entities references are not supported now ...
Packit Service a31ea6
 *       need a start to be able to pop out of entities refs since
Packit Service a31ea6
 *       parent is the endity declaration, not the ref.
Packit Service a31ea6
 */
Packit Service a31ea6
Packit Service a31ea6
#include <string.h>
Packit Service a31ea6
#include <libxml/xpointer.h>
Packit Service a31ea6
#include <libxml/xmlmemory.h>
Packit Service a31ea6
#include <libxml/parserInternals.h>
Packit Service a31ea6
#include <libxml/uri.h>
Packit Service a31ea6
#include <libxml/xpath.h>
Packit Service a31ea6
#include <libxml/xpathInternals.h>
Packit Service a31ea6
#include <libxml/xmlerror.h>
Packit Service a31ea6
#include <libxml/globals.h>
Packit Service a31ea6
Packit Service a31ea6
#ifdef LIBXML_XPTR_ENABLED
Packit Service a31ea6
Packit Service a31ea6
/* Add support of the xmlns() xpointer scheme to initialize the namespaces */
Packit Service a31ea6
#define XPTR_XMLNS_SCHEME
Packit Service a31ea6
Packit Service a31ea6
/* #define DEBUG_RANGES */
Packit Service a31ea6
#ifdef DEBUG_RANGES
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
#define TODO								\
Packit Service a31ea6
    xmlGenericError(xmlGenericErrorContext,				\
Packit Service a31ea6
	    "Unimplemented block at %s:%d\n",				\
Packit Service a31ea6
            __FILE__, __LINE__);
Packit Service a31ea6
Packit Service a31ea6
#define STRANGE							\
Packit Service a31ea6
    xmlGenericError(xmlGenericErrorContext,				\
Packit Service a31ea6
	    "Internal error at %s:%d\n",				\
Packit Service a31ea6
            __FILE__, __LINE__);
Packit Service a31ea6
Packit Service a31ea6
/************************************************************************
Packit Service a31ea6
 *									*
Packit Service a31ea6
 *		Some factorized error routines				*
Packit Service a31ea6
 *									*
Packit Service a31ea6
 ************************************************************************/
Packit Service a31ea6
Packit Service a31ea6
/**
Packit Service a31ea6
 * xmlXPtrErrMemory:
Packit Service a31ea6
 * @extra:  extra informations
Packit Service a31ea6
 *
Packit Service a31ea6
 * Handle a redefinition of attribute error
Packit Service a31ea6
 */
Packit Service a31ea6
static void
Packit Service a31ea6
xmlXPtrErrMemory(const char *extra)
Packit Service a31ea6
{
Packit Service a31ea6
    __xmlRaiseError(NULL, NULL, NULL, NULL, NULL, XML_FROM_XPOINTER,
Packit Service a31ea6
		    XML_ERR_NO_MEMORY, XML_ERR_ERROR, NULL, 0, extra,
Packit Service a31ea6
		    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
 * xmlXPtrErr:
Packit Service a31ea6
 * @ctxt:  an XPTR evaluation context
Packit Service a31ea6
 * @extra:  extra informations
Packit Service a31ea6
 *
Packit Service a31ea6
 * Handle a redefinition of attribute error
Packit Service a31ea6
 */
Packit Service a31ea6
static void LIBXML_ATTR_FORMAT(3,0)
Packit Service a31ea6
xmlXPtrErr(xmlXPathParserContextPtr ctxt, int error,
Packit Service a31ea6
           const char * msg, const xmlChar *extra)
Packit Service a31ea6
{
Packit Service a31ea6
    if (ctxt != NULL)
Packit Service a31ea6
        ctxt->error = error;
Packit Service a31ea6
    if ((ctxt == NULL) || (ctxt->context == NULL)) {
Packit Service a31ea6
	__xmlRaiseError(NULL, NULL, NULL,
Packit Service a31ea6
			NULL, NULL, XML_FROM_XPOINTER, error,
Packit Service a31ea6
			XML_ERR_ERROR, NULL, 0,
Packit Service a31ea6
			(const char *) extra, NULL, NULL, 0, 0,
Packit Service a31ea6
			msg, extra);
Packit Service a31ea6
	return;
Packit Service a31ea6
    }
Packit Service a31ea6
Packit Service a31ea6
    /* cleanup current last error */
Packit Service a31ea6
    xmlResetError(&ctxt->context->lastError);
Packit Service a31ea6
Packit Service a31ea6
    ctxt->context->lastError.domain = XML_FROM_XPOINTER;
Packit Service a31ea6
    ctxt->context->lastError.code = error;
Packit Service a31ea6
    ctxt->context->lastError.level = XML_ERR_ERROR;
Packit Service a31ea6
    ctxt->context->lastError.str1 = (char *) xmlStrdup(ctxt->base);
Packit Service a31ea6
    ctxt->context->lastError.int1 = ctxt->cur - ctxt->base;
Packit Service a31ea6
    ctxt->context->lastError.node = ctxt->context->debugNode;
Packit Service a31ea6
    if (ctxt->context->error != NULL) {
Packit Service a31ea6
	ctxt->context->error(ctxt->context->userData,
Packit Service a31ea6
	                     &ctxt->context->lastError);
Packit Service a31ea6
    } else {
Packit Service a31ea6
	__xmlRaiseError(NULL, NULL, NULL,
Packit Service a31ea6
			NULL, ctxt->context->debugNode, XML_FROM_XPOINTER,
Packit Service a31ea6
			error, XML_ERR_ERROR, NULL, 0,
Packit Service a31ea6
			(const char *) extra, (const char *) ctxt->base, NULL,
Packit Service a31ea6
			ctxt->cur - ctxt->base, 0,
Packit Service a31ea6
			msg, extra);
Packit Service a31ea6
    }
Packit Service a31ea6
}
Packit Service a31ea6
Packit Service a31ea6
/************************************************************************
Packit Service a31ea6
 *									*
Packit Service a31ea6
 *		A few helper functions for child sequences		*
Packit Service a31ea6
 *									*
Packit Service a31ea6
 ************************************************************************/
Packit Service a31ea6
/* xmlXPtrAdvanceNode is a private function, but used by xinclude.c */
Packit Service a31ea6
xmlNodePtr xmlXPtrAdvanceNode(xmlNodePtr cur, int *level);
Packit Service a31ea6
/**
Packit Service a31ea6
 * xmlXPtrGetArity:
Packit Service a31ea6
 * @cur:  the node
Packit Service a31ea6
 *
Packit Service a31ea6
 * Returns the number of child for an element, -1 in case of error
Packit Service a31ea6
 */
Packit Service a31ea6
static int
Packit Service a31ea6
xmlXPtrGetArity(xmlNodePtr cur) {
Packit Service a31ea6
    int i;
Packit Service a31ea6
    if ((cur == NULL) || (cur->type == XML_NAMESPACE_DECL))
Packit Service a31ea6
	return(-1);
Packit Service a31ea6
    cur = cur->children;
Packit Service a31ea6
    for (i = 0;cur != NULL;cur = cur->next) {
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
	}
Packit Service a31ea6
    }
Packit Service a31ea6
    return(i);
Packit Service a31ea6
}
Packit Service a31ea6
Packit Service a31ea6
/**
Packit Service a31ea6
 * xmlXPtrGetIndex:
Packit Service a31ea6
 * @cur:  the node
Packit Service a31ea6
 *
Packit Service a31ea6
 * Returns the index of the node in its parent children list, -1
Packit Service a31ea6
 *         in case of error
Packit Service a31ea6
 */
Packit Service a31ea6
static int
Packit Service a31ea6
xmlXPtrGetIndex(xmlNodePtr cur) {
Packit Service a31ea6
    int i;
Packit Service a31ea6
    if ((cur == NULL) || (cur->type == XML_NAMESPACE_DECL))
Packit Service a31ea6
	return(-1);
Packit Service a31ea6
    for (i = 1;cur != NULL;cur = cur->prev) {
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
	}
Packit Service a31ea6
    }
Packit Service a31ea6
    return(i);
Packit Service a31ea6
}
Packit Service a31ea6
Packit Service a31ea6
/**
Packit Service a31ea6
 * xmlXPtrGetNthChild:
Packit Service a31ea6
 * @cur:  the node
Packit Service a31ea6
 * @no:  the child number
Packit Service a31ea6
 *
Packit Service a31ea6
 * Returns the @no'th element child of @cur or NULL
Packit Service a31ea6
 */
Packit Service a31ea6
static xmlNodePtr
Packit Service a31ea6
xmlXPtrGetNthChild(xmlNodePtr cur, int no) {
Packit Service a31ea6
    int i;
Packit Service a31ea6
    if ((cur == NULL) || (cur->type == XML_NAMESPACE_DECL))
Packit Service a31ea6
	return(cur);
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
/************************************************************************
Packit Service a31ea6
 *									*
Packit Service a31ea6
 *		Handling of XPointer specific types			*
Packit Service a31ea6
 *									*
Packit Service a31ea6
 ************************************************************************/
Packit Service a31ea6
Packit Service a31ea6
/**
Packit Service a31ea6
 * xmlXPtrCmpPoints:
Packit Service a31ea6
 * @node1:  the first node
Packit Service a31ea6
 * @index1:  the first index
Packit Service a31ea6
 * @node2:  the second node
Packit Service a31ea6
 * @index2:  the second index
Packit Service a31ea6
 *
Packit Service a31ea6
 * Compare two points w.r.t document order
Packit Service a31ea6
 *
Packit Service a31ea6
 * Returns -2 in case of error 1 if first point < second point, 0 if
Packit Service a31ea6
 *         that's the same point, -1 otherwise
Packit Service a31ea6
 */
Packit Service a31ea6
static int
Packit Service a31ea6
xmlXPtrCmpPoints(xmlNodePtr node1, int index1, xmlNodePtr node2, int index2) {
Packit Service a31ea6
    if ((node1 == NULL) || (node2 == NULL))
Packit Service a31ea6
	return(-2);
Packit Service a31ea6
    /*
Packit Service a31ea6
     * a couple of optimizations which will avoid computations in most cases
Packit Service a31ea6
     */
Packit Service a31ea6
    if (node1 == node2) {
Packit Service a31ea6
	if (index1 < index2)
Packit Service a31ea6
	    return(1);
Packit Service a31ea6
	if (index1 > index2)
Packit Service a31ea6
	    return(-1);
Packit Service a31ea6
	return(0);
Packit Service a31ea6
    }
Packit Service a31ea6
    return(xmlXPathCmpNodes(node1, node2));
Packit Service a31ea6
}
Packit Service a31ea6
Packit Service a31ea6
/**
Packit Service a31ea6
 * xmlXPtrNewPoint:
Packit Service a31ea6
 * @node:  the xmlNodePtr
Packit Service a31ea6
 * @indx:  the indx within the node
Packit Service a31ea6
 *
Packit Service a31ea6
 * Create a new xmlXPathObjectPtr of type point
Packit Service a31ea6
 *
Packit Service a31ea6
 * Returns the newly created object.
Packit Service a31ea6
 */
Packit Service a31ea6
static xmlXPathObjectPtr
Packit Service a31ea6
xmlXPtrNewPoint(xmlNodePtr node, int indx) {
Packit Service a31ea6
    xmlXPathObjectPtr ret;
Packit Service a31ea6
Packit Service a31ea6
    if (node == NULL)
Packit Service a31ea6
	return(NULL);
Packit Service a31ea6
    if (indx < 0)
Packit Service a31ea6
	return(NULL);
Packit Service a31ea6
Packit Service a31ea6
    ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject));
Packit Service a31ea6
    if (ret == NULL) {
Packit Service a31ea6
        xmlXPtrErrMemory("allocating point");
Packit Service a31ea6
	return(NULL);
Packit Service a31ea6
    }
Packit Service a31ea6
    memset(ret, 0 , (size_t) sizeof(xmlXPathObject));
Packit Service a31ea6
    ret->type = XPATH_POINT;
Packit Service a31ea6
    ret->user = (void *) node;
Packit Service a31ea6
    ret->index = indx;
Packit Service a31ea6
    return(ret);
Packit Service a31ea6
}
Packit Service a31ea6
Packit Service a31ea6
/**
Packit Service a31ea6
 * xmlXPtrRangeCheckOrder:
Packit Service a31ea6
 * @range:  an object range
Packit Service a31ea6
 *
Packit Service a31ea6
 * Make sure the points in the range are in the right order
Packit Service a31ea6
 */
Packit Service a31ea6
static void
Packit Service a31ea6
xmlXPtrRangeCheckOrder(xmlXPathObjectPtr range) {
Packit Service a31ea6
    int tmp;
Packit Service a31ea6
    xmlNodePtr tmp2;
Packit Service a31ea6
    if (range == NULL)
Packit Service a31ea6
	return;
Packit Service a31ea6
    if (range->type != XPATH_RANGE)
Packit Service a31ea6
	return;
Packit Service a31ea6
    if (range->user2 == NULL)
Packit Service a31ea6
	return;
Packit Service a31ea6
    tmp = xmlXPtrCmpPoints(range->user, range->index,
Packit Service a31ea6
	                     range->user2, range->index2);
Packit Service a31ea6
    if (tmp == -1) {
Packit Service a31ea6
	tmp2 = range->user;
Packit Service a31ea6
	range->user = range->user2;
Packit Service a31ea6
	range->user2 = tmp2;
Packit Service a31ea6
	tmp = range->index;
Packit Service a31ea6
	range->index = range->index2;
Packit Service a31ea6
	range->index2 = tmp;
Packit Service a31ea6
    }
Packit Service a31ea6
}
Packit Service a31ea6
Packit Service a31ea6
/**
Packit Service a31ea6
 * xmlXPtrRangesEqual:
Packit Service a31ea6
 * @range1:  the first range
Packit Service a31ea6
 * @range2:  the second range
Packit Service a31ea6
 *
Packit Service a31ea6
 * Compare two ranges
Packit Service a31ea6
 *
Packit Service a31ea6
 * Returns 1 if equal, 0 otherwise
Packit Service a31ea6
 */
Packit Service a31ea6
static int
Packit Service a31ea6
xmlXPtrRangesEqual(xmlXPathObjectPtr range1, xmlXPathObjectPtr range2) {
Packit Service a31ea6
    if (range1 == range2)
Packit Service a31ea6
	return(1);
Packit Service a31ea6
    if ((range1 == NULL) || (range2 == NULL))
Packit Service a31ea6
	return(0);
Packit Service a31ea6
    if (range1->type != range2->type)
Packit Service a31ea6
	return(0);
Packit Service a31ea6
    if (range1->type != XPATH_RANGE)
Packit Service a31ea6
	return(0);
Packit Service a31ea6
    if (range1->user != range2->user)
Packit Service a31ea6
	return(0);
Packit Service a31ea6
    if (range1->index != range2->index)
Packit Service a31ea6
	return(0);
Packit Service a31ea6
    if (range1->user2 != range2->user2)
Packit Service a31ea6
	return(0);
Packit Service a31ea6
    if (range1->index2 != range2->index2)
Packit Service a31ea6
	return(0);
Packit Service a31ea6
    return(1);
Packit Service a31ea6
}
Packit Service a31ea6
Packit Service a31ea6
/**
Packit Service a31ea6
 * xmlXPtrNewRangeInternal:
Packit Service a31ea6
 * @start:  the starting node
Packit Service a31ea6
 * @startindex:  the start index
Packit Service a31ea6
 * @end:  the ending point
Packit Service a31ea6
 * @endindex:  the ending index
Packit Service a31ea6
 *
Packit Service a31ea6
 * Internal function to create a new xmlXPathObjectPtr of type range
Packit Service a31ea6
 *
Packit Service a31ea6
 * Returns the newly created object.
Packit Service a31ea6
 */
Packit Service a31ea6
static xmlXPathObjectPtr
Packit Service a31ea6
xmlXPtrNewRangeInternal(xmlNodePtr start, int startindex,
Packit Service a31ea6
                        xmlNodePtr end, int endindex) {
Packit Service a31ea6
    xmlXPathObjectPtr ret;
Packit Service a31ea6
Packit Service a31ea6
    /*
Packit Service a31ea6
     * Namespace nodes must be copied (see xmlXPathNodeSetDupNs).
Packit Service a31ea6
     * Disallow them for now.
Packit Service a31ea6
     */
Packit Service a31ea6
    if ((start != NULL) && (start->type == XML_NAMESPACE_DECL))
Packit Service a31ea6
	return(NULL);
Packit Service a31ea6
    if ((end != NULL) && (end->type == XML_NAMESPACE_DECL))
Packit Service a31ea6
	return(NULL);
Packit Service a31ea6
Packit Service a31ea6
    ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject));
Packit Service a31ea6
    if (ret == NULL) {
Packit Service a31ea6
        xmlXPtrErrMemory("allocating range");
Packit Service a31ea6
	return(NULL);
Packit Service a31ea6
    }
Packit Service a31ea6
    memset(ret, 0, sizeof(xmlXPathObject));
Packit Service a31ea6
    ret->type = XPATH_RANGE;
Packit Service a31ea6
    ret->user = start;
Packit Service a31ea6
    ret->index = startindex;
Packit Service a31ea6
    ret->user2 = end;
Packit Service a31ea6
    ret->index2 = endindex;
Packit Service a31ea6
    return(ret);
Packit Service a31ea6
}
Packit Service a31ea6
Packit Service a31ea6
/**
Packit Service a31ea6
 * xmlXPtrNewRange:
Packit Service a31ea6
 * @start:  the starting node
Packit Service a31ea6
 * @startindex:  the start index
Packit Service a31ea6
 * @end:  the ending point
Packit Service a31ea6
 * @endindex:  the ending index
Packit Service a31ea6
 *
Packit Service a31ea6
 * Create a new xmlXPathObjectPtr of type range
Packit Service a31ea6
 *
Packit Service a31ea6
 * Returns the newly created object.
Packit Service a31ea6
 */
Packit Service a31ea6
xmlXPathObjectPtr
Packit Service a31ea6
xmlXPtrNewRange(xmlNodePtr start, int startindex,
Packit Service a31ea6
	        xmlNodePtr end, int endindex) {
Packit Service a31ea6
    xmlXPathObjectPtr ret;
Packit Service a31ea6
Packit Service a31ea6
    if (start == NULL)
Packit Service a31ea6
	return(NULL);
Packit Service a31ea6
    if (end == NULL)
Packit Service a31ea6
	return(NULL);
Packit Service a31ea6
    if (startindex < 0)
Packit Service a31ea6
	return(NULL);
Packit Service a31ea6
    if (endindex < 0)
Packit Service a31ea6
	return(NULL);
Packit Service a31ea6
Packit Service a31ea6
    ret = xmlXPtrNewRangeInternal(start, startindex, end, endindex);
Packit Service a31ea6
    xmlXPtrRangeCheckOrder(ret);
Packit Service a31ea6
    return(ret);
Packit Service a31ea6
}
Packit Service a31ea6
Packit Service a31ea6
/**
Packit Service a31ea6
 * xmlXPtrNewRangePoints:
Packit Service a31ea6
 * @start:  the starting point
Packit Service a31ea6
 * @end:  the ending point
Packit Service a31ea6
 *
Packit Service a31ea6
 * Create a new xmlXPathObjectPtr of type range using 2 Points
Packit Service a31ea6
 *
Packit Service a31ea6
 * Returns the newly created object.
Packit Service a31ea6
 */
Packit Service a31ea6
xmlXPathObjectPtr
Packit Service a31ea6
xmlXPtrNewRangePoints(xmlXPathObjectPtr start, xmlXPathObjectPtr end) {
Packit Service a31ea6
    xmlXPathObjectPtr ret;
Packit Service a31ea6
Packit Service a31ea6
    if (start == NULL)
Packit Service a31ea6
	return(NULL);
Packit Service a31ea6
    if (end == NULL)
Packit Service a31ea6
	return(NULL);
Packit Service a31ea6
    if (start->type != XPATH_POINT)
Packit Service a31ea6
	return(NULL);
Packit Service a31ea6
    if (end->type != XPATH_POINT)
Packit Service a31ea6
	return(NULL);
Packit Service a31ea6
Packit Service a31ea6
    ret = xmlXPtrNewRangeInternal(start->user, start->index, end->user,
Packit Service a31ea6
                                  end->index);
Packit Service a31ea6
    xmlXPtrRangeCheckOrder(ret);
Packit Service a31ea6
    return(ret);
Packit Service a31ea6
}
Packit Service a31ea6
Packit Service a31ea6
/**
Packit Service a31ea6
 * xmlXPtrNewRangePointNode:
Packit Service a31ea6
 * @start:  the starting point
Packit Service a31ea6
 * @end:  the ending node
Packit Service a31ea6
 *
Packit Service a31ea6
 * Create a new xmlXPathObjectPtr of type range from a point to a node
Packit Service a31ea6
 *
Packit Service a31ea6
 * Returns the newly created object.
Packit Service a31ea6
 */
Packit Service a31ea6
xmlXPathObjectPtr
Packit Service a31ea6
xmlXPtrNewRangePointNode(xmlXPathObjectPtr start, xmlNodePtr end) {
Packit Service a31ea6
    xmlXPathObjectPtr ret;
Packit Service a31ea6
Packit Service a31ea6
    if (start == NULL)
Packit Service a31ea6
	return(NULL);
Packit Service a31ea6
    if (end == NULL)
Packit Service a31ea6
	return(NULL);
Packit Service a31ea6
    if (start->type != XPATH_POINT)
Packit Service a31ea6
	return(NULL);
Packit Service a31ea6
Packit Service a31ea6
    ret = xmlXPtrNewRangeInternal(start->user, start->index, end, -1);
Packit Service a31ea6
    xmlXPtrRangeCheckOrder(ret);
Packit Service a31ea6
    return(ret);
Packit Service a31ea6
}
Packit Service a31ea6
Packit Service a31ea6
/**
Packit Service a31ea6
 * xmlXPtrNewRangeNodePoint:
Packit Service a31ea6
 * @start:  the starting node
Packit Service a31ea6
 * @end:  the ending point
Packit Service a31ea6
 *
Packit Service a31ea6
 * Create a new xmlXPathObjectPtr of type range from a node to a point
Packit Service a31ea6
 *
Packit Service a31ea6
 * Returns the newly created object.
Packit Service a31ea6
 */
Packit Service a31ea6
xmlXPathObjectPtr
Packit Service a31ea6
xmlXPtrNewRangeNodePoint(xmlNodePtr start, xmlXPathObjectPtr end) {
Packit Service a31ea6
    xmlXPathObjectPtr ret;
Packit Service a31ea6
Packit Service a31ea6
    if (start == NULL)
Packit Service a31ea6
	return(NULL);
Packit Service a31ea6
    if (end == NULL)
Packit Service a31ea6
	return(NULL);
Packit Service a31ea6
    if (start->type != XPATH_POINT)
Packit Service a31ea6
	return(NULL);
Packit Service a31ea6
    if (end->type != XPATH_POINT)
Packit Service a31ea6
	return(NULL);
Packit Service a31ea6
Packit Service a31ea6
    ret = xmlXPtrNewRangeInternal(start, -1, end->user, end->index);
Packit Service a31ea6
    xmlXPtrRangeCheckOrder(ret);
Packit Service a31ea6
    return(ret);
Packit Service a31ea6
}
Packit Service a31ea6
Packit Service a31ea6
/**
Packit Service a31ea6
 * xmlXPtrNewRangeNodes:
Packit Service a31ea6
 * @start:  the starting node
Packit Service a31ea6
 * @end:  the ending node
Packit Service a31ea6
 *
Packit Service a31ea6
 * Create a new xmlXPathObjectPtr of type range using 2 nodes
Packit Service a31ea6
 *
Packit Service a31ea6
 * Returns the newly created object.
Packit Service a31ea6
 */
Packit Service a31ea6
xmlXPathObjectPtr
Packit Service a31ea6
xmlXPtrNewRangeNodes(xmlNodePtr start, xmlNodePtr end) {
Packit Service a31ea6
    xmlXPathObjectPtr ret;
Packit Service a31ea6
Packit Service a31ea6
    if (start == NULL)
Packit Service a31ea6
	return(NULL);
Packit Service a31ea6
    if (end == NULL)
Packit Service a31ea6
	return(NULL);
Packit Service a31ea6
Packit Service a31ea6
    ret = xmlXPtrNewRangeInternal(start, -1, end, -1);
Packit Service a31ea6
    xmlXPtrRangeCheckOrder(ret);
Packit Service a31ea6
    return(ret);
Packit Service a31ea6
}
Packit Service a31ea6
Packit Service a31ea6
/**
Packit Service a31ea6
 * xmlXPtrNewCollapsedRange:
Packit Service a31ea6
 * @start:  the starting and ending node
Packit Service a31ea6
 *
Packit Service a31ea6
 * Create a new xmlXPathObjectPtr of type range using a single nodes
Packit Service a31ea6
 *
Packit Service a31ea6
 * Returns the newly created object.
Packit Service a31ea6
 */
Packit Service a31ea6
xmlXPathObjectPtr
Packit Service a31ea6
xmlXPtrNewCollapsedRange(xmlNodePtr start) {
Packit Service a31ea6
    xmlXPathObjectPtr ret;
Packit Service a31ea6
Packit Service a31ea6
    if (start == NULL)
Packit Service a31ea6
	return(NULL);
Packit Service a31ea6
Packit Service a31ea6
    ret = xmlXPtrNewRangeInternal(start, -1, NULL, -1);
Packit Service a31ea6
    return(ret);
Packit Service a31ea6
}
Packit Service a31ea6
Packit Service a31ea6
/**
Packit Service a31ea6
 * xmlXPtrNewRangeNodeObject:
Packit Service a31ea6
 * @start:  the starting node
Packit Service a31ea6
 * @end:  the ending object
Packit Service a31ea6
 *
Packit Service a31ea6
 * Create a new xmlXPathObjectPtr of type range from a not to an object
Packit Service a31ea6
 *
Packit Service a31ea6
 * Returns the newly created object.
Packit Service a31ea6
 */
Packit Service a31ea6
xmlXPathObjectPtr
Packit Service a31ea6
xmlXPtrNewRangeNodeObject(xmlNodePtr start, xmlXPathObjectPtr end) {
Packit Service a31ea6
    xmlNodePtr endNode;
Packit Service a31ea6
    int endIndex;
Packit Service a31ea6
    xmlXPathObjectPtr ret;
Packit Service a31ea6
Packit Service a31ea6
    if (start == NULL)
Packit Service a31ea6
	return(NULL);
Packit Service a31ea6
    if (end == NULL)
Packit Service a31ea6
	return(NULL);
Packit Service a31ea6
    switch (end->type) {
Packit Service a31ea6
	case XPATH_POINT:
Packit Service a31ea6
	    endNode = end->user;
Packit Service a31ea6
	    endIndex = end->index;
Packit Service a31ea6
	    break;
Packit Service a31ea6
	case XPATH_RANGE:
Packit Service a31ea6
	    endNode = end->user2;
Packit Service a31ea6
	    endIndex = end->index2;
Packit Service a31ea6
	    break;
Packit Service a31ea6
	case XPATH_NODESET:
Packit Service a31ea6
	    /*
Packit Service a31ea6
	     * Empty set ...
Packit Service a31ea6
	     */
Packit Service a31ea6
	    if ((end->nodesetval == NULL) || (end->nodesetval->nodeNr <= 0))
Packit Service a31ea6
		return(NULL);
Packit Service a31ea6
	    endNode = end->nodesetval->nodeTab[end->nodesetval->nodeNr - 1];
Packit Service a31ea6
	    endIndex = -1;
Packit Service a31ea6
	    break;
Packit Service a31ea6
	default:
Packit Service a31ea6
	    /* TODO */
Packit Service a31ea6
	    return(NULL);
Packit Service a31ea6
    }
Packit Service a31ea6
Packit Service a31ea6
    ret = xmlXPtrNewRangeInternal(start, -1, endNode, endIndex);
Packit Service a31ea6
    xmlXPtrRangeCheckOrder(ret);
Packit Service a31ea6
    return(ret);
Packit Service a31ea6
}
Packit Service a31ea6
Packit Service a31ea6
#define XML_RANGESET_DEFAULT	10
Packit Service a31ea6
Packit Service a31ea6
/**
Packit Service a31ea6
 * xmlXPtrLocationSetCreate:
Packit Service a31ea6
 * @val:  an initial xmlXPathObjectPtr, or NULL
Packit Service a31ea6
 *
Packit Service a31ea6
 * Create a new xmlLocationSetPtr of type double and of value @val
Packit Service a31ea6
 *
Packit Service a31ea6
 * Returns the newly created object.
Packit Service a31ea6
 */
Packit Service a31ea6
xmlLocationSetPtr
Packit Service a31ea6
xmlXPtrLocationSetCreate(xmlXPathObjectPtr val) {
Packit Service a31ea6
    xmlLocationSetPtr ret;
Packit Service a31ea6
Packit Service a31ea6
    ret = (xmlLocationSetPtr) xmlMalloc(sizeof(xmlLocationSet));
Packit Service a31ea6
    if (ret == NULL) {
Packit Service a31ea6
        xmlXPtrErrMemory("allocating locationset");
Packit Service a31ea6
	return(NULL);
Packit Service a31ea6
    }
Packit Service a31ea6
    memset(ret, 0 , (size_t) sizeof(xmlLocationSet));
Packit Service a31ea6
    if (val != NULL) {
Packit Service a31ea6
        ret->locTab = (xmlXPathObjectPtr *) xmlMalloc(XML_RANGESET_DEFAULT *
Packit Service a31ea6
					     sizeof(xmlXPathObjectPtr));
Packit Service a31ea6
	if (ret->locTab == NULL) {
Packit Service a31ea6
	    xmlXPtrErrMemory("allocating locationset");
Packit Service a31ea6
	    xmlFree(ret);
Packit Service a31ea6
	    return(NULL);
Packit Service a31ea6
	}
Packit Service a31ea6
	memset(ret->locTab, 0 ,
Packit Service a31ea6
	       XML_RANGESET_DEFAULT * (size_t) sizeof(xmlXPathObjectPtr));
Packit Service a31ea6
        ret->locMax = XML_RANGESET_DEFAULT;
Packit Service a31ea6
	ret->locTab[ret->locNr++] = val;
Packit Service a31ea6
    }
Packit Service a31ea6
    return(ret);
Packit Service a31ea6
}
Packit Service a31ea6
Packit Service a31ea6
/**
Packit Service a31ea6
 * xmlXPtrLocationSetAdd:
Packit Service a31ea6
 * @cur:  the initial range set
Packit Service a31ea6
 * @val:  a new xmlXPathObjectPtr
Packit Service a31ea6
 *
Packit Service a31ea6
 * add a new xmlXPathObjectPtr to an existing LocationSet
Packit Service a31ea6
 * If the location already exist in the set @val is freed.
Packit Service a31ea6
 */
Packit Service a31ea6
void
Packit Service a31ea6
xmlXPtrLocationSetAdd(xmlLocationSetPtr cur, xmlXPathObjectPtr val) {
Packit Service a31ea6
    int i;
Packit Service a31ea6
Packit Service a31ea6
    if ((cur == NULL) || (val == NULL)) return;
Packit Service a31ea6
Packit Service a31ea6
    /*
Packit Service a31ea6
     * check against doublons
Packit Service a31ea6
     */
Packit Service a31ea6
    for (i = 0;i < cur->locNr;i++) {
Packit Service a31ea6
	if (xmlXPtrRangesEqual(cur->locTab[i], val)) {
Packit Service a31ea6
	    xmlXPathFreeObject(val);
Packit Service a31ea6
	    return;
Packit Service a31ea6
	}
Packit Service a31ea6
    }
Packit Service a31ea6
Packit Service a31ea6
    /*
Packit Service a31ea6
     * grow the locTab if needed
Packit Service a31ea6
     */
Packit Service a31ea6
    if (cur->locMax == 0) {
Packit Service a31ea6
        cur->locTab = (xmlXPathObjectPtr *) xmlMalloc(XML_RANGESET_DEFAULT *
Packit Service a31ea6
					     sizeof(xmlXPathObjectPtr));
Packit Service a31ea6
	if (cur->locTab == NULL) {
Packit Service a31ea6
	    xmlXPtrErrMemory("adding location to set");
Packit Service a31ea6
	    return;
Packit Service a31ea6
	}
Packit Service a31ea6
	memset(cur->locTab, 0 ,
Packit Service a31ea6
	       XML_RANGESET_DEFAULT * (size_t) sizeof(xmlXPathObjectPtr));
Packit Service a31ea6
        cur->locMax = XML_RANGESET_DEFAULT;
Packit Service a31ea6
    } else if (cur->locNr == cur->locMax) {
Packit Service a31ea6
        xmlXPathObjectPtr *temp;
Packit Service a31ea6
Packit Service a31ea6
        cur->locMax *= 2;
Packit Service a31ea6
	temp = (xmlXPathObjectPtr *) xmlRealloc(cur->locTab, cur->locMax *
Packit Service a31ea6
				      sizeof(xmlXPathObjectPtr));
Packit Service a31ea6
	if (temp == NULL) {
Packit Service a31ea6
	    xmlXPtrErrMemory("adding location to set");
Packit Service a31ea6
	    return;
Packit Service a31ea6
	}
Packit Service a31ea6
	cur->locTab = temp;
Packit Service a31ea6
    }
Packit Service a31ea6
    cur->locTab[cur->locNr++] = val;
Packit Service a31ea6
}
Packit Service a31ea6
Packit Service a31ea6
/**
Packit Service a31ea6
 * xmlXPtrLocationSetMerge:
Packit Service a31ea6
 * @val1:  the first LocationSet
Packit Service a31ea6
 * @val2:  the second LocationSet
Packit Service a31ea6
 *
Packit Service a31ea6
 * Merges two rangesets, all ranges from @val2 are added to @val1
Packit Service a31ea6
 *
Packit Service a31ea6
 * Returns val1 once extended or NULL in case of error.
Packit Service a31ea6
 */
Packit Service a31ea6
xmlLocationSetPtr
Packit Service a31ea6
xmlXPtrLocationSetMerge(xmlLocationSetPtr val1, xmlLocationSetPtr val2) {
Packit Service a31ea6
    int i;
Packit Service a31ea6
Packit Service a31ea6
    if (val1 == NULL) return(NULL);
Packit Service a31ea6
    if (val2 == NULL) return(val1);
Packit Service a31ea6
Packit Service a31ea6
    /*
Packit Service a31ea6
     * !!!!! this can be optimized a lot, knowing that both
Packit Service a31ea6
     *       val1 and val2 already have unicity of their values.
Packit Service a31ea6
     */
Packit Service a31ea6
Packit Service a31ea6
    for (i = 0;i < val2->locNr;i++)
Packit Service a31ea6
        xmlXPtrLocationSetAdd(val1, val2->locTab[i]);
Packit Service a31ea6
Packit Service a31ea6
    return(val1);
Packit Service a31ea6
}
Packit Service a31ea6
Packit Service a31ea6
/**
Packit Service a31ea6
 * xmlXPtrLocationSetDel:
Packit Service a31ea6
 * @cur:  the initial range set
Packit Service a31ea6
 * @val:  an xmlXPathObjectPtr
Packit Service a31ea6
 *
Packit Service a31ea6
 * Removes an xmlXPathObjectPtr from an existing LocationSet
Packit Service a31ea6
 */
Packit Service a31ea6
void
Packit Service a31ea6
xmlXPtrLocationSetDel(xmlLocationSetPtr cur, xmlXPathObjectPtr val) {
Packit Service a31ea6
    int i;
Packit Service a31ea6
Packit Service a31ea6
    if (cur == NULL) return;
Packit Service a31ea6
    if (val == NULL) return;
Packit Service a31ea6
Packit Service a31ea6
    /*
Packit Service a31ea6
     * check against doublons
Packit Service a31ea6
     */
Packit Service a31ea6
    for (i = 0;i < cur->locNr;i++)
Packit Service a31ea6
        if (cur->locTab[i] == val) break;
Packit Service a31ea6
Packit Service a31ea6
    if (i >= cur->locNr) {
Packit Service a31ea6
#ifdef DEBUG
Packit Service a31ea6
        xmlGenericError(xmlGenericErrorContext,
Packit Service a31ea6
	        "xmlXPtrLocationSetDel: Range wasn't found in RangeList\n");
Packit Service a31ea6
#endif
Packit Service a31ea6
        return;
Packit Service a31ea6
    }
Packit Service a31ea6
    cur->locNr--;
Packit Service a31ea6
    for (;i < cur->locNr;i++)
Packit Service a31ea6
        cur->locTab[i] = cur->locTab[i + 1];
Packit Service a31ea6
    cur->locTab[cur->locNr] = NULL;
Packit Service a31ea6
}
Packit Service a31ea6
Packit Service a31ea6
/**
Packit Service a31ea6
 * xmlXPtrLocationSetRemove:
Packit Service a31ea6
 * @cur:  the initial range set
Packit Service a31ea6
 * @val:  the index to remove
Packit Service a31ea6
 *
Packit Service a31ea6
 * Removes an entry from an existing LocationSet list.
Packit Service a31ea6
 */
Packit Service a31ea6
void
Packit Service a31ea6
xmlXPtrLocationSetRemove(xmlLocationSetPtr cur, int val) {
Packit Service a31ea6
    if (cur == NULL) return;
Packit Service a31ea6
    if (val >= cur->locNr) return;
Packit Service a31ea6
    cur->locNr--;
Packit Service a31ea6
    for (;val < cur->locNr;val++)
Packit Service a31ea6
        cur->locTab[val] = cur->locTab[val + 1];
Packit Service a31ea6
    cur->locTab[cur->locNr] = NULL;
Packit Service a31ea6
}
Packit Service a31ea6
Packit Service a31ea6
/**
Packit Service a31ea6
 * xmlXPtrFreeLocationSet:
Packit Service a31ea6
 * @obj:  the xmlLocationSetPtr to free
Packit Service a31ea6
 *
Packit Service a31ea6
 * Free the LocationSet compound (not the actual ranges !).
Packit Service a31ea6
 */
Packit Service a31ea6
void
Packit Service a31ea6
xmlXPtrFreeLocationSet(xmlLocationSetPtr obj) {
Packit Service a31ea6
    int i;
Packit Service a31ea6
Packit Service a31ea6
    if (obj == NULL) return;
Packit Service a31ea6
    if (obj->locTab != NULL) {
Packit Service a31ea6
	for (i = 0;i < obj->locNr; i++) {
Packit Service a31ea6
            xmlXPathFreeObject(obj->locTab[i]);
Packit Service a31ea6
	}
Packit Service a31ea6
	xmlFree(obj->locTab);
Packit Service a31ea6
    }
Packit Service a31ea6
    xmlFree(obj);
Packit Service a31ea6
}
Packit Service a31ea6
Packit Service a31ea6
/**
Packit Service a31ea6
 * xmlXPtrNewLocationSetNodes:
Packit Service a31ea6
 * @start:  the start NodePtr value
Packit Service a31ea6
 * @end:  the end NodePtr value or NULL
Packit Service a31ea6
 *
Packit Service a31ea6
 * Create a new xmlXPathObjectPtr of type LocationSet and initialize
Packit Service a31ea6
 * it with the single range made of the two nodes @start and @end
Packit Service a31ea6
 *
Packit Service a31ea6
 * Returns the newly created object.
Packit Service a31ea6
 */
Packit Service a31ea6
xmlXPathObjectPtr
Packit Service a31ea6
xmlXPtrNewLocationSetNodes(xmlNodePtr start, xmlNodePtr end) {
Packit Service a31ea6
    xmlXPathObjectPtr ret;
Packit Service a31ea6
Packit Service a31ea6
    ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject));
Packit Service a31ea6
    if (ret == NULL) {
Packit Service a31ea6
        xmlXPtrErrMemory("allocating locationset");
Packit Service a31ea6
	return(NULL);
Packit Service a31ea6
    }
Packit Service a31ea6
    memset(ret, 0 , (size_t) sizeof(xmlXPathObject));
Packit Service a31ea6
    ret->type = XPATH_LOCATIONSET;
Packit Service a31ea6
    if (end == NULL)
Packit Service a31ea6
	ret->user = xmlXPtrLocationSetCreate(xmlXPtrNewCollapsedRange(start));
Packit Service a31ea6
    else
Packit Service a31ea6
	ret->user = xmlXPtrLocationSetCreate(xmlXPtrNewRangeNodes(start,end));
Packit Service a31ea6
    return(ret);
Packit Service a31ea6
}
Packit Service a31ea6
Packit Service a31ea6
/**
Packit Service a31ea6
 * xmlXPtrNewLocationSetNodeSet:
Packit Service a31ea6
 * @set:  a node set
Packit Service a31ea6
 *
Packit Service a31ea6
 * Create a new xmlXPathObjectPtr of type LocationSet and initialize
Packit Service a31ea6
 * it with all the nodes from @set
Packit Service a31ea6
 *
Packit Service a31ea6
 * Returns the newly created object.
Packit Service a31ea6
 */
Packit Service a31ea6
xmlXPathObjectPtr
Packit Service a31ea6
xmlXPtrNewLocationSetNodeSet(xmlNodeSetPtr set) {
Packit Service a31ea6
    xmlXPathObjectPtr ret;
Packit Service a31ea6
Packit Service a31ea6
    ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject));
Packit Service a31ea6
    if (ret == NULL) {
Packit Service a31ea6
        xmlXPtrErrMemory("allocating locationset");
Packit Service a31ea6
	return(NULL);
Packit Service a31ea6
    }
Packit Service a31ea6
    memset(ret, 0 , (size_t) sizeof(xmlXPathObject));
Packit Service a31ea6
    ret->type = XPATH_LOCATIONSET;
Packit Service a31ea6
    if (set != NULL) {
Packit Service a31ea6
	int i;
Packit Service a31ea6
	xmlLocationSetPtr newset;
Packit Service a31ea6
Packit Service a31ea6
	newset = xmlXPtrLocationSetCreate(NULL);
Packit Service a31ea6
	if (newset == NULL)
Packit Service a31ea6
	    return(ret);
Packit Service a31ea6
Packit Service a31ea6
	for (i = 0;i < set->nodeNr;i++)
Packit Service a31ea6
	    xmlXPtrLocationSetAdd(newset,
Packit Service a31ea6
		        xmlXPtrNewCollapsedRange(set->nodeTab[i]));
Packit Service a31ea6
Packit Service a31ea6
	ret->user = (void *) newset;
Packit Service a31ea6
    }
Packit Service a31ea6
    return(ret);
Packit Service a31ea6
}
Packit Service a31ea6
Packit Service a31ea6
/**
Packit Service a31ea6
 * xmlXPtrWrapLocationSet:
Packit Service a31ea6
 * @val:  the LocationSet value
Packit Service a31ea6
 *
Packit Service a31ea6
 * Wrap the LocationSet @val in a new xmlXPathObjectPtr
Packit Service a31ea6
 *
Packit Service a31ea6
 * Returns the newly created object.
Packit Service a31ea6
 */
Packit Service a31ea6
xmlXPathObjectPtr
Packit Service a31ea6
xmlXPtrWrapLocationSet(xmlLocationSetPtr val) {
Packit Service a31ea6
    xmlXPathObjectPtr ret;
Packit Service a31ea6
Packit Service a31ea6
    ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject));
Packit Service a31ea6
    if (ret == NULL) {
Packit Service a31ea6
        xmlXPtrErrMemory("allocating locationset");
Packit Service a31ea6
	return(NULL);
Packit Service a31ea6
    }
Packit Service a31ea6
    memset(ret, 0 , (size_t) sizeof(xmlXPathObject));
Packit Service a31ea6
    ret->type = XPATH_LOCATIONSET;
Packit Service a31ea6
    ret->user = (void *) val;
Packit Service a31ea6
    return(ret);
Packit Service a31ea6
}
Packit Service a31ea6
Packit Service a31ea6
/************************************************************************
Packit Service a31ea6
 *									*
Packit Service a31ea6
 *			The parser					*
Packit Service a31ea6
 *									*
Packit Service a31ea6
 ************************************************************************/
Packit Service a31ea6
Packit Service a31ea6
static void xmlXPtrEvalChildSeq(xmlXPathParserContextPtr ctxt, xmlChar *name);
Packit Service a31ea6
Packit Service a31ea6
/*
Packit Service a31ea6
 * Macros for accessing the content. Those should be used only by the parser,
Packit Service a31ea6
 * and not exported.
Packit Service a31ea6
 *
Packit Service a31ea6
 * Dirty macros, i.e. one need to make assumption on the context to use them
Packit Service a31ea6
 *
Packit Service a31ea6
 *   CUR_PTR return the current pointer to the xmlChar to be parsed.
Packit Service a31ea6
 *   CUR     returns the current xmlChar value, i.e. a 8 bit value
Packit Service a31ea6
 *           in ISO-Latin or UTF-8.
Packit Service a31ea6
 *           This should be used internally by the parser
Packit Service a31ea6
 *           only to compare to ASCII values otherwise it would break when
Packit Service a31ea6
 *           running with UTF-8 encoding.
Packit Service a31ea6
 *   NXT(n)  returns the n'th next xmlChar. Same as CUR is should be used only
Packit Service a31ea6
 *           to compare on ASCII based substring.
Packit Service a31ea6
 *   SKIP(n) Skip n xmlChar, and must also be used only to skip ASCII defined
Packit Service a31ea6
 *           strings within the parser.
Packit Service a31ea6
 *   CURRENT Returns the current char value, with the full decoding of
Packit Service a31ea6
 *           UTF-8 if we are using this mode. It returns an int.
Packit Service a31ea6
 *   NEXT    Skip to the next character, this does the proper decoding
Packit Service a31ea6
 *           in UTF-8 mode. It also pop-up unfinished entities on the fly.
Packit Service a31ea6
 *           It returns the pointer to the current xmlChar.
Packit Service a31ea6
 */
Packit Service a31ea6
Packit Service a31ea6
#define CUR (*ctxt->cur)
Packit Service a31ea6
#define SKIP(val) ctxt->cur += (val)
Packit Service a31ea6
#define NXT(val) ctxt->cur[(val)]
Packit Service a31ea6
#define CUR_PTR ctxt->cur
Packit Service a31ea6
Packit Service a31ea6
#define SKIP_BLANKS							\
Packit Service a31ea6
    while (IS_BLANK_CH(*(ctxt->cur))) NEXT
Packit Service a31ea6
Packit Service a31ea6
#define CURRENT (*ctxt->cur)
Packit Service a31ea6
#define NEXT ((*ctxt->cur) ?  ctxt->cur++: ctxt->cur)
Packit Service a31ea6
Packit Service a31ea6
/*
Packit Service a31ea6
 * xmlXPtrGetChildNo:
Packit Service a31ea6
 * @ctxt:  the XPointer Parser context
Packit Service a31ea6
 * @index:  the child number
Packit Service a31ea6
 *
Packit Service a31ea6
 * Move the current node of the nodeset on the stack to the
Packit Service a31ea6
 * given child if found
Packit Service a31ea6
 */
Packit Service a31ea6
static void
Packit Service a31ea6
xmlXPtrGetChildNo(xmlXPathParserContextPtr ctxt, int indx) {
Packit Service a31ea6
    xmlNodePtr cur = NULL;
Packit Service a31ea6
    xmlXPathObjectPtr obj;
Packit Service a31ea6
    xmlNodeSetPtr oldset;
Packit Service a31ea6
Packit Service a31ea6
    CHECK_TYPE(XPATH_NODESET);
Packit Service a31ea6
    obj = valuePop(ctxt);
Packit Service a31ea6
    oldset = obj->nodesetval;
Packit Service a31ea6
    if ((indx <= 0) || (oldset == NULL) || (oldset->nodeNr != 1)) {
Packit Service a31ea6
	xmlXPathFreeObject(obj);
Packit Service a31ea6
	valuePush(ctxt, xmlXPathNewNodeSet(NULL));
Packit Service a31ea6
	return;
Packit Service a31ea6
    }
Packit Service a31ea6
    cur = xmlXPtrGetNthChild(oldset->nodeTab[0], indx);
Packit Service a31ea6
    if (cur == NULL) {
Packit Service a31ea6
	xmlXPathFreeObject(obj);
Packit Service a31ea6
	valuePush(ctxt, xmlXPathNewNodeSet(NULL));
Packit Service a31ea6
	return;
Packit Service a31ea6
    }
Packit Service a31ea6
    oldset->nodeTab[0] = cur;
Packit Service a31ea6
    valuePush(ctxt, obj);
Packit Service a31ea6
}
Packit Service a31ea6
Packit Service a31ea6
/**
Packit Service a31ea6
 * xmlXPtrEvalXPtrPart:
Packit Service a31ea6
 * @ctxt:  the XPointer Parser context
Packit Service a31ea6
 * @name:  the preparsed Scheme for the XPtrPart
Packit Service a31ea6
 *
Packit Service a31ea6
 * XPtrPart ::= 'xpointer' '(' XPtrExpr ')'
Packit Service a31ea6
 *            | Scheme '(' SchemeSpecificExpr ')'
Packit Service a31ea6
 *
Packit Service a31ea6
 * Scheme   ::=  NCName - 'xpointer' [VC: Non-XPointer schemes]
Packit Service a31ea6
 *
Packit Service a31ea6
 * SchemeSpecificExpr ::= StringWithBalancedParens
Packit Service a31ea6
 *
Packit Service a31ea6
 * StringWithBalancedParens ::=
Packit Service a31ea6
 *              [^()]* ('(' StringWithBalancedParens ')' [^()]*)*
Packit Service a31ea6
 *              [VC: Parenthesis escaping]
Packit Service a31ea6
 *
Packit Service a31ea6
 * XPtrExpr ::= Expr [VC: Parenthesis escaping]
Packit Service a31ea6
 *
Packit Service a31ea6
 * VC: Parenthesis escaping:
Packit Service a31ea6
 *   The end of an XPointer part is signaled by the right parenthesis ")"
Packit Service a31ea6
 *   character that is balanced with the left parenthesis "(" character
Packit Service a31ea6
 *   that began the part. Any unbalanced parenthesis character inside the
Packit Service a31ea6
 *   expression, even within literals, must be escaped with a circumflex (^)
Packit Service a31ea6
 *   character preceding it. If the expression contains any literal
Packit Service a31ea6
 *   occurrences of the circumflex, each must be escaped with an additional
Packit Service a31ea6
 *   circumflex (that is, ^^). If the unescaped parentheses in the expression
Packit Service a31ea6
 *   are not balanced, a syntax error results.
Packit Service a31ea6
 *
Packit Service a31ea6
 * Parse and evaluate an XPtrPart. Basically it generates the unescaped
Packit Service a31ea6
 * string and if the scheme is 'xpointer' it will call the XPath interpreter.
Packit Service a31ea6
 *
Packit Service a31ea6
 * TODO: there is no new scheme registration mechanism
Packit Service a31ea6
 */
Packit Service a31ea6
Packit Service a31ea6
static void
Packit Service a31ea6
xmlXPtrEvalXPtrPart(xmlXPathParserContextPtr ctxt, xmlChar *name) {
Packit Service a31ea6
    xmlChar *buffer, *cur;
Packit Service a31ea6
    int len;
Packit Service a31ea6
    int level;
Packit Service a31ea6
Packit Service a31ea6
    if (name == NULL)
Packit Service a31ea6
    name = xmlXPathParseName(ctxt);
Packit Service a31ea6
    if (name == NULL)
Packit Service a31ea6
	XP_ERROR(XPATH_EXPR_ERROR);
Packit Service a31ea6
Packit Service a31ea6
    if (CUR != '(') {
Packit Service a31ea6
        xmlFree(name);
Packit Service a31ea6
	XP_ERROR(XPATH_EXPR_ERROR);
Packit Service a31ea6
    }
Packit Service a31ea6
    NEXT;
Packit Service a31ea6
    level = 1;
Packit Service a31ea6
Packit Service a31ea6
    len = xmlStrlen(ctxt->cur);
Packit Service a31ea6
    len++;
Packit Service a31ea6
    buffer = (xmlChar *) xmlMallocAtomic(len * sizeof (xmlChar));
Packit Service a31ea6
    if (buffer == NULL) {
Packit Service a31ea6
        xmlXPtrErrMemory("allocating buffer");
Packit Service a31ea6
        xmlFree(name);
Packit Service a31ea6
	return;
Packit Service a31ea6
    }
Packit Service a31ea6
Packit Service a31ea6
    cur = buffer;
Packit Service a31ea6
    while (CUR != 0) {
Packit Service a31ea6
	if (CUR == ')') {
Packit Service a31ea6
	    level--;
Packit Service a31ea6
	    if (level == 0) {
Packit Service a31ea6
		NEXT;
Packit Service a31ea6
		break;
Packit Service a31ea6
	    }
Packit Service a31ea6
	} else if (CUR == '(') {
Packit Service a31ea6
	    level++;
Packit Service a31ea6
	} else if (CUR == '^') {
Packit Service a31ea6
            if ((NXT(1) == ')') || (NXT(1) == '(') || (NXT(1) == '^')) {
Packit Service a31ea6
                NEXT;
Packit Service a31ea6
            }
Packit Service a31ea6
	}
Packit Service a31ea6
        *cur++ = CUR;
Packit Service a31ea6
	NEXT;
Packit Service a31ea6
    }
Packit Service a31ea6
    *cur = 0;
Packit Service a31ea6
Packit Service a31ea6
    if ((level != 0) && (CUR == 0)) {
Packit Service a31ea6
        xmlFree(name);
Packit Service a31ea6
	xmlFree(buffer);
Packit Service a31ea6
	XP_ERROR(XPTR_SYNTAX_ERROR);
Packit Service a31ea6
    }
Packit Service a31ea6
Packit Service a31ea6
    if (xmlStrEqual(name, (xmlChar *) "xpointer")) {
Packit Service a31ea6
	const xmlChar *left = CUR_PTR;
Packit Service a31ea6
Packit Service a31ea6
	CUR_PTR = buffer;
Packit Service a31ea6
	/*
Packit Service a31ea6
	 * To evaluate an xpointer scheme element (4.3) we need:
Packit Service a31ea6
	 *   context initialized to the root
Packit Service a31ea6
	 *   context position initalized to 1
Packit Service a31ea6
	 *   context size initialized to 1
Packit Service a31ea6
	 */
Packit Service a31ea6
	ctxt->context->node = (xmlNodePtr)ctxt->context->doc;
Packit Service a31ea6
	ctxt->context->proximityPosition = 1;
Packit Service a31ea6
	ctxt->context->contextSize = 1;
Packit Service a31ea6
	xmlXPathEvalExpr(ctxt);
Packit Service a31ea6
	CUR_PTR=left;
Packit Service a31ea6
    } else if (xmlStrEqual(name, (xmlChar *) "element")) {
Packit Service a31ea6
	const xmlChar *left = CUR_PTR;
Packit Service a31ea6
	xmlChar *name2;
Packit Service a31ea6
Packit Service a31ea6
	CUR_PTR = buffer;
Packit Service a31ea6
	if (buffer[0] == '/') {
Packit Service a31ea6
	    xmlXPathRoot(ctxt);
Packit Service a31ea6
	    xmlXPtrEvalChildSeq(ctxt, NULL);
Packit Service a31ea6
	} else {
Packit Service a31ea6
	    name2 = xmlXPathParseName(ctxt);
Packit Service a31ea6
	    if (name2 == NULL) {
Packit Service a31ea6
		CUR_PTR = left;
Packit Service a31ea6
		xmlFree(buffer);
Packit Service a31ea6
                xmlFree(name);
Packit Service a31ea6
		XP_ERROR(XPATH_EXPR_ERROR);
Packit Service a31ea6
	    }
Packit Service a31ea6
	    xmlXPtrEvalChildSeq(ctxt, name2);
Packit Service a31ea6
	}
Packit Service a31ea6
	CUR_PTR = left;
Packit Service a31ea6
#ifdef XPTR_XMLNS_SCHEME
Packit Service a31ea6
    } else if (xmlStrEqual(name, (xmlChar *) "xmlns")) {
Packit Service a31ea6
	const xmlChar *left = CUR_PTR;
Packit Service a31ea6
	xmlChar *prefix;
Packit Service a31ea6
	xmlChar *URI;
Packit Service a31ea6
	xmlURIPtr value;
Packit Service a31ea6
Packit Service a31ea6
	CUR_PTR = buffer;
Packit Service a31ea6
        prefix = xmlXPathParseNCName(ctxt);
Packit Service a31ea6
	if (prefix == NULL) {
Packit Service a31ea6
	    xmlFree(buffer);
Packit Service a31ea6
	    xmlFree(name);
Packit Service a31ea6
	    XP_ERROR(XPTR_SYNTAX_ERROR);
Packit Service a31ea6
	}
Packit Service a31ea6
	SKIP_BLANKS;
Packit Service a31ea6
	if (CUR != '=') {
Packit Service a31ea6
	    xmlFree(prefix);
Packit Service a31ea6
	    xmlFree(buffer);
Packit Service a31ea6
	    xmlFree(name);
Packit Service a31ea6
	    XP_ERROR(XPTR_SYNTAX_ERROR);
Packit Service a31ea6
	}
Packit Service a31ea6
	NEXT;
Packit Service a31ea6
	SKIP_BLANKS;
Packit Service a31ea6
	/* @@ check escaping in the XPointer WD */
Packit Service a31ea6
Packit Service a31ea6
	value = xmlParseURI((const char *)ctxt->cur);
Packit Service a31ea6
	if (value == NULL) {
Packit Service a31ea6
	    xmlFree(prefix);
Packit Service a31ea6
	    xmlFree(buffer);
Packit Service a31ea6
	    xmlFree(name);
Packit Service a31ea6
	    XP_ERROR(XPTR_SYNTAX_ERROR);
Packit Service a31ea6
	}
Packit Service a31ea6
	URI = xmlSaveUri(value);
Packit Service a31ea6
	xmlFreeURI(value);
Packit Service a31ea6
	if (URI == NULL) {
Packit Service a31ea6
	    xmlFree(prefix);
Packit Service a31ea6
	    xmlFree(buffer);
Packit Service a31ea6
	    xmlFree(name);
Packit Service a31ea6
	    XP_ERROR(XPATH_MEMORY_ERROR);
Packit Service a31ea6
	}
Packit Service a31ea6
Packit Service a31ea6
	xmlXPathRegisterNs(ctxt->context, prefix, URI);
Packit Service a31ea6
	CUR_PTR = left;
Packit Service a31ea6
	xmlFree(URI);
Packit Service a31ea6
	xmlFree(prefix);
Packit Service a31ea6
#endif /* XPTR_XMLNS_SCHEME */
Packit Service a31ea6
    } else {
Packit Service a31ea6
        xmlXPtrErr(ctxt, XML_XPTR_UNKNOWN_SCHEME,
Packit Service a31ea6
		   "unsupported scheme '%s'\n", name);
Packit Service a31ea6
    }
Packit Service a31ea6
    xmlFree(buffer);
Packit Service a31ea6
    xmlFree(name);
Packit Service a31ea6
}
Packit Service a31ea6
Packit Service a31ea6
/**
Packit Service a31ea6
 * xmlXPtrEvalFullXPtr:
Packit Service a31ea6
 * @ctxt:  the XPointer Parser context
Packit Service a31ea6
 * @name:  the preparsed Scheme for the first XPtrPart
Packit Service a31ea6
 *
Packit Service a31ea6
 * FullXPtr ::= XPtrPart (S? XPtrPart)*
Packit Service a31ea6
 *
Packit Service a31ea6
 * As the specs says:
Packit Service a31ea6
 * -----------
Packit Service a31ea6
 * When multiple XPtrParts are provided, they must be evaluated in
Packit Service a31ea6
 * left-to-right order. If evaluation of one part fails, the nexti
Packit Service a31ea6
 * is evaluated. The following conditions cause XPointer part failure:
Packit Service a31ea6
 *
Packit Service a31ea6
 * - An unknown scheme
Packit Service a31ea6
 * - A scheme that does not locate any sub-resource present in the resource
Packit Service a31ea6
 * - A scheme that is not applicable to the media type of the resource
Packit Service a31ea6
 *
Packit Service a31ea6
 * The XPointer application must consume a failed XPointer part and
Packit Service a31ea6
 * attempt to evaluate the next one, if any. The result of the first
Packit Service a31ea6
 * XPointer part whose evaluation succeeds is taken to be the fragment
Packit Service a31ea6
 * located by the XPointer as a whole. If all the parts fail, the result
Packit Service a31ea6
 * for the XPointer as a whole is a sub-resource error.
Packit Service a31ea6
 * -----------
Packit Service a31ea6
 *
Packit Service a31ea6
 * Parse and evaluate a Full XPtr i.e. possibly a cascade of XPath based
Packit Service a31ea6
 * expressions or other schemes.
Packit Service a31ea6
 */
Packit Service a31ea6
static void
Packit Service a31ea6
xmlXPtrEvalFullXPtr(xmlXPathParserContextPtr ctxt, xmlChar *name) {
Packit Service a31ea6
    if (name == NULL)
Packit Service a31ea6
    name = xmlXPathParseName(ctxt);
Packit Service a31ea6
    if (name == NULL)
Packit Service a31ea6
	XP_ERROR(XPATH_EXPR_ERROR);
Packit Service a31ea6
    while (name != NULL) {
Packit Service a31ea6
	ctxt->error = XPATH_EXPRESSION_OK;
Packit Service a31ea6
	xmlXPtrEvalXPtrPart(ctxt, name);
Packit Service a31ea6
Packit Service a31ea6
	/* in case of syntax error, break here */
Packit Service a31ea6
	if ((ctxt->error != XPATH_EXPRESSION_OK) &&
Packit Service a31ea6
            (ctxt->error != XML_XPTR_UNKNOWN_SCHEME))
Packit Service a31ea6
	    return;
Packit Service a31ea6
Packit Service a31ea6
	/*
Packit Service a31ea6
	 * If the returned value is a non-empty nodeset
Packit Service a31ea6
	 * or location set, return here.
Packit Service a31ea6
	 */
Packit Service a31ea6
	if (ctxt->value != NULL) {
Packit Service a31ea6
	    xmlXPathObjectPtr obj = ctxt->value;
Packit Service a31ea6
Packit Service a31ea6
	    switch (obj->type) {
Packit Service a31ea6
		case XPATH_LOCATIONSET: {
Packit Service a31ea6
		    xmlLocationSetPtr loc = ctxt->value->user;
Packit Service a31ea6
		    if ((loc != NULL) && (loc->locNr > 0))
Packit Service a31ea6
			return;
Packit Service a31ea6
		    break;
Packit Service a31ea6
		}
Packit Service a31ea6
		case XPATH_NODESET: {
Packit Service a31ea6
		    xmlNodeSetPtr loc = ctxt->value->nodesetval;
Packit Service a31ea6
		    if ((loc != NULL) && (loc->nodeNr > 0))
Packit Service a31ea6
			return;
Packit Service a31ea6
		    break;
Packit Service a31ea6
		}
Packit Service a31ea6
		default:
Packit Service a31ea6
		    break;
Packit Service a31ea6
	    }
Packit Service a31ea6
Packit Service a31ea6
	    /*
Packit Service a31ea6
	     * Evaluating to improper values is equivalent to
Packit Service a31ea6
	     * a sub-resource error, clean-up the stack
Packit Service a31ea6
	     */
Packit Service a31ea6
	    do {
Packit Service a31ea6
		obj = valuePop(ctxt);
Packit Service a31ea6
		if (obj != NULL) {
Packit Service a31ea6
		    xmlXPathFreeObject(obj);
Packit Service a31ea6
		}
Packit Service a31ea6
	    } while (obj != NULL);
Packit Service a31ea6
	}
Packit Service a31ea6
Packit Service a31ea6
	/*
Packit Service a31ea6
	 * Is there another XPointer part.
Packit Service a31ea6
	 */
Packit Service a31ea6
	SKIP_BLANKS;
Packit Service a31ea6
	name = xmlXPathParseName(ctxt);
Packit Service a31ea6
    }
Packit Service a31ea6
}
Packit Service a31ea6
Packit Service a31ea6
/**
Packit Service a31ea6
 * xmlXPtrEvalChildSeq:
Packit Service a31ea6
 * @ctxt:  the XPointer Parser context
Packit Service a31ea6
 * @name:  a possible ID name of the child sequence
Packit Service a31ea6
 *
Packit Service a31ea6
 *  ChildSeq ::= '/1' ('/' [0-9]*)*
Packit Service a31ea6
 *             | Name ('/' [0-9]*)+
Packit Service a31ea6
 *
Packit Service a31ea6
 * Parse and evaluate a Child Sequence. This routine also handle the
Packit Service a31ea6
 * case of a Bare Name used to get a document ID.
Packit Service a31ea6
 */
Packit Service a31ea6
static void
Packit Service a31ea6
xmlXPtrEvalChildSeq(xmlXPathParserContextPtr ctxt, xmlChar *name) {
Packit Service a31ea6
    /*
Packit Service a31ea6
     * XPointer don't allow by syntax to address in mutirooted trees
Packit Service a31ea6
     * this might prove useful in some cases, warn about it.
Packit Service a31ea6
     */
Packit Service a31ea6
    if ((name == NULL) && (CUR == '/') && (NXT(1) != '1')) {
Packit Service a31ea6
        xmlXPtrErr(ctxt, XML_XPTR_CHILDSEQ_START,
Packit Service a31ea6
		   "warning: ChildSeq not starting by /1\n", NULL);
Packit Service a31ea6
    }
Packit Service a31ea6
Packit Service a31ea6
    if (name != NULL) {
Packit Service a31ea6
	valuePush(ctxt, xmlXPathNewString(name));
Packit Service a31ea6
	xmlFree(name);
Packit Service a31ea6
	xmlXPathIdFunction(ctxt, 1);
Packit Service a31ea6
	CHECK_ERROR;
Packit Service a31ea6
    }
Packit Service a31ea6
Packit Service a31ea6
    while (CUR == '/') {
Packit Service a31ea6
	int child = 0;
Packit Service a31ea6
	NEXT;
Packit Service a31ea6
Packit Service a31ea6
	while ((CUR >= '0') && (CUR <= '9')) {
Packit Service a31ea6
	    child = child * 10 + (CUR - '0');
Packit Service a31ea6
	    NEXT;
Packit Service a31ea6
	}
Packit Service a31ea6
	xmlXPtrGetChildNo(ctxt, child);
Packit Service a31ea6
    }
Packit Service a31ea6
}
Packit Service a31ea6
Packit Service a31ea6
Packit Service a31ea6
/**
Packit Service a31ea6
 * xmlXPtrEvalXPointer:
Packit Service a31ea6
 * @ctxt:  the XPointer Parser context
Packit Service a31ea6
 *
Packit Service a31ea6
 *  XPointer ::= Name
Packit Service a31ea6
 *             | ChildSeq
Packit Service a31ea6
 *             | FullXPtr
Packit Service a31ea6
 *
Packit Service a31ea6
 * Parse and evaluate an XPointer
Packit Service a31ea6
 */
Packit Service a31ea6
static void
Packit Service a31ea6
xmlXPtrEvalXPointer(xmlXPathParserContextPtr ctxt) {
Packit Service a31ea6
    if (ctxt->valueTab == NULL) {
Packit Service a31ea6
	/* Allocate the value stack */
Packit Service a31ea6
	ctxt->valueTab = (xmlXPathObjectPtr *)
Packit Service a31ea6
			 xmlMalloc(10 * sizeof(xmlXPathObjectPtr));
Packit Service a31ea6
	if (ctxt->valueTab == NULL) {
Packit Service a31ea6
	    xmlXPtrErrMemory("allocating evaluation context");
Packit Service a31ea6
	    return;
Packit Service a31ea6
	}
Packit Service a31ea6
	ctxt->valueNr = 0;
Packit Service a31ea6
	ctxt->valueMax = 10;
Packit Service a31ea6
	ctxt->value = NULL;
Packit Service a31ea6
	ctxt->valueFrame = 0;
Packit Service a31ea6
    }
Packit Service a31ea6
    SKIP_BLANKS;
Packit Service a31ea6
    if (CUR == '/') {
Packit Service a31ea6
	xmlXPathRoot(ctxt);
Packit Service a31ea6
        xmlXPtrEvalChildSeq(ctxt, NULL);
Packit Service a31ea6
    } else {
Packit Service a31ea6
	xmlChar *name;
Packit Service a31ea6
Packit Service a31ea6
	name = xmlXPathParseName(ctxt);
Packit Service a31ea6
	if (name == NULL)
Packit Service a31ea6
	    XP_ERROR(XPATH_EXPR_ERROR);
Packit Service a31ea6
	if (CUR == '(') {
Packit Service a31ea6
	    xmlXPtrEvalFullXPtr(ctxt, name);
Packit Service a31ea6
	    /* Short evaluation */
Packit Service a31ea6
	    return;
Packit Service a31ea6
	} else {
Packit Service a31ea6
	    /* this handle both Bare Names and Child Sequences */
Packit Service a31ea6
	    xmlXPtrEvalChildSeq(ctxt, name);
Packit Service a31ea6
	}
Packit Service a31ea6
    }
Packit Service a31ea6
    SKIP_BLANKS;
Packit Service a31ea6
    if (CUR != 0)
Packit Service a31ea6
	XP_ERROR(XPATH_EXPR_ERROR);
Packit Service a31ea6
}
Packit Service a31ea6
Packit Service a31ea6
Packit Service a31ea6
/************************************************************************
Packit Service a31ea6
 *									*
Packit Service a31ea6
 *			General routines				*
Packit Service a31ea6
 *									*
Packit Service a31ea6
 ************************************************************************/
Packit Service a31ea6
Packit Service a31ea6
static
Packit Service a31ea6
void xmlXPtrStringRangeFunction(xmlXPathParserContextPtr ctxt, int nargs);
Packit Service a31ea6
static
Packit Service a31ea6
void xmlXPtrStartPointFunction(xmlXPathParserContextPtr ctxt, int nargs);
Packit Service a31ea6
static
Packit Service a31ea6
void xmlXPtrEndPointFunction(xmlXPathParserContextPtr ctxt, int nargs);
Packit Service a31ea6
static
Packit Service a31ea6
void xmlXPtrHereFunction(xmlXPathParserContextPtr ctxt, int nargs);
Packit Service a31ea6
static
Packit Service a31ea6
void xmlXPtrOriginFunction(xmlXPathParserContextPtr ctxt, int nargs);
Packit Service a31ea6
static
Packit Service a31ea6
void xmlXPtrRangeInsideFunction(xmlXPathParserContextPtr ctxt, int nargs);
Packit Service a31ea6
static
Packit Service a31ea6
void xmlXPtrRangeFunction(xmlXPathParserContextPtr ctxt, int nargs);
Packit Service a31ea6
Packit Service a31ea6
/**
Packit Service a31ea6
 * xmlXPtrNewContext:
Packit Service a31ea6
 * @doc:  the XML document
Packit Service a31ea6
 * @here:  the node that directly contains the XPointer being evaluated or NULL
Packit Service a31ea6
 * @origin:  the element from which a user or program initiated traversal of
Packit Service a31ea6
 *           the link, or NULL.
Packit Service a31ea6
 *
Packit Service a31ea6
 * Create a new XPointer context
Packit Service a31ea6
 *
Packit Service a31ea6
 * Returns the xmlXPathContext just allocated.
Packit Service a31ea6
 */
Packit Service a31ea6
xmlXPathContextPtr
Packit Service a31ea6
xmlXPtrNewContext(xmlDocPtr doc, xmlNodePtr here, xmlNodePtr origin) {
Packit Service a31ea6
    xmlXPathContextPtr ret;
Packit Service a31ea6
Packit Service a31ea6
    ret = xmlXPathNewContext(doc);
Packit Service a31ea6
    if (ret == NULL)
Packit Service a31ea6
	return(ret);
Packit Service a31ea6
    ret->xptr = 1;
Packit Service a31ea6
    ret->here = here;
Packit Service a31ea6
    ret->origin = origin;
Packit Service a31ea6
Packit Service a31ea6
    xmlXPathRegisterFunc(ret, (xmlChar *)"range",
Packit Service a31ea6
	                 xmlXPtrRangeFunction);
Packit Service a31ea6
    xmlXPathRegisterFunc(ret, (xmlChar *)"range-inside",
Packit Service a31ea6
	                 xmlXPtrRangeInsideFunction);
Packit Service a31ea6
    xmlXPathRegisterFunc(ret, (xmlChar *)"string-range",
Packit Service a31ea6
	                 xmlXPtrStringRangeFunction);
Packit Service a31ea6
    xmlXPathRegisterFunc(ret, (xmlChar *)"start-point",
Packit Service a31ea6
	                 xmlXPtrStartPointFunction);
Packit Service a31ea6
    xmlXPathRegisterFunc(ret, (xmlChar *)"end-point",
Packit Service a31ea6
	                 xmlXPtrEndPointFunction);
Packit Service a31ea6
    xmlXPathRegisterFunc(ret, (xmlChar *)"here",
Packit Service a31ea6
	                 xmlXPtrHereFunction);
Packit Service a31ea6
    xmlXPathRegisterFunc(ret, (xmlChar *)" origin",
Packit Service a31ea6
	                 xmlXPtrOriginFunction);
Packit Service a31ea6
Packit Service a31ea6
    return(ret);
Packit Service a31ea6
}
Packit Service a31ea6
Packit Service a31ea6
/**
Packit Service a31ea6
 * xmlXPtrEval:
Packit Service a31ea6
 * @str:  the XPointer expression
Packit Service a31ea6
 * @ctx:  the XPointer context
Packit Service a31ea6
 *
Packit Service a31ea6
 * Evaluate the XPath Location Path in the given context.
Packit Service a31ea6
 *
Packit Service a31ea6
 * Returns the xmlXPathObjectPtr resulting from the evaluation or NULL.
Packit Service a31ea6
 *         the caller has to free the object.
Packit Service a31ea6
 */
Packit Service a31ea6
xmlXPathObjectPtr
Packit Service a31ea6
xmlXPtrEval(const xmlChar *str, xmlXPathContextPtr ctx) {
Packit Service a31ea6
    xmlXPathParserContextPtr ctxt;
Packit Service a31ea6
    xmlXPathObjectPtr res = NULL, tmp;
Packit Service a31ea6
    xmlXPathObjectPtr init = NULL;
Packit Service a31ea6
    int stack = 0;
Packit Service a31ea6
Packit Service a31ea6
    xmlXPathInit();
Packit Service a31ea6
Packit Service a31ea6
    if ((ctx == NULL) || (str == NULL))
Packit Service a31ea6
	return(NULL);
Packit Service a31ea6
Packit Service a31ea6
    ctxt = xmlXPathNewParserContext(str, ctx);
Packit Service a31ea6
    if (ctxt == NULL)
Packit Service a31ea6
	return(NULL);
Packit Service a31ea6
    ctxt->xptr = 1;
Packit Service a31ea6
    xmlXPtrEvalXPointer(ctxt);
Packit Service a31ea6
Packit Service a31ea6
    if ((ctxt->value != NULL) &&
Packit Service a31ea6
	(ctxt->value->type != XPATH_NODESET) &&
Packit Service a31ea6
	(ctxt->value->type != XPATH_LOCATIONSET)) {
Packit Service a31ea6
        xmlXPtrErr(ctxt, XML_XPTR_EVAL_FAILED,
Packit Service a31ea6
		"xmlXPtrEval: evaluation failed to return a node set\n",
Packit Service a31ea6
		   NULL);
Packit Service a31ea6
    } else {
Packit Service a31ea6
	res = valuePop(ctxt);
Packit Service a31ea6
    }
Packit Service a31ea6
Packit Service a31ea6
    do {
Packit Service a31ea6
        tmp = valuePop(ctxt);
Packit Service a31ea6
	if (tmp != NULL) {
Packit Service a31ea6
	    if (tmp != init) {
Packit Service a31ea6
		if (tmp->type == XPATH_NODESET) {
Packit Service a31ea6
		    /*
Packit Service a31ea6
		     * Evaluation may push a root nodeset which is unused
Packit Service a31ea6
		     */
Packit Service a31ea6
		    xmlNodeSetPtr set;
Packit Service a31ea6
		    set = tmp->nodesetval;
Packit Service a31ea6
		    if ((set == NULL) || (set->nodeNr != 1) ||
Packit Service a31ea6
			(set->nodeTab[0] != (xmlNodePtr) ctx->doc))
Packit Service a31ea6
			stack++;
Packit Service a31ea6
		} else
Packit Service a31ea6
		    stack++;
Packit Service a31ea6
	    }
Packit Service a31ea6
	    xmlXPathFreeObject(tmp);
Packit Service a31ea6
        }
Packit Service a31ea6
    } while (tmp != NULL);
Packit Service a31ea6
    if (stack != 0) {
Packit Service a31ea6
        xmlXPtrErr(ctxt, XML_XPTR_EXTRA_OBJECTS,
Packit Service a31ea6
		   "xmlXPtrEval: object(s) left on the eval stack\n",
Packit Service a31ea6
		   NULL);
Packit Service a31ea6
    }
Packit Service a31ea6
    if (ctxt->error != XPATH_EXPRESSION_OK) {
Packit Service a31ea6
	xmlXPathFreeObject(res);
Packit Service a31ea6
	res = NULL;
Packit Service a31ea6
    }
Packit Service a31ea6
Packit Service a31ea6
    xmlXPathFreeParserContext(ctxt);
Packit Service a31ea6
    return(res);
Packit Service a31ea6
}
Packit Service a31ea6
Packit Service a31ea6
/**
Packit Service a31ea6
 * xmlXPtrBuildRangeNodeList:
Packit Service a31ea6
 * @range:  a range object
Packit Service a31ea6
 *
Packit Service a31ea6
 * Build a node list tree copy of the range
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
xmlXPtrBuildRangeNodeList(xmlXPathObjectPtr range) {
Packit Service a31ea6
    /* pointers to generated nodes */
Packit Service a31ea6
    xmlNodePtr list = NULL, last = NULL, parent = NULL, tmp;
Packit Service a31ea6
    /* pointers to traversal nodes */
Packit Service a31ea6
    xmlNodePtr start, cur, end;
Packit Service a31ea6
    int index1, index2;
Packit Service a31ea6
Packit Service a31ea6
    if (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(xmlCopyNode(start, 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
    while (cur != NULL) {
Packit Service a31ea6
	if (cur == end) {
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
			index1 = 0;
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 (last != NULL)
Packit Service a31ea6
		    xmlAddNextSibling(last, tmp);
Packit Service a31ea6
		else
Packit Service a31ea6
		    xmlAddChild(parent, tmp);
Packit Service a31ea6
		return(list);
Packit Service a31ea6
	    } else {
Packit Service a31ea6
		tmp = xmlCopyNode(cur, 0);
Packit Service a31ea6
		if (list == NULL)
Packit Service a31ea6
		    list = tmp;
Packit Service a31ea6
		else {
Packit Service a31ea6
		    if (last != NULL)
Packit Service a31ea6
			xmlAddNextSibling(last, tmp);
Packit Service a31ea6
		    else
Packit Service a31ea6
			xmlAddChild(parent, tmp);
Packit Service a31ea6
		}
Packit Service a31ea6
		last = NULL;
Packit Service a31ea6
		parent = tmp;
Packit Service a31ea6
Packit Service a31ea6
		if (index2 > 1) {
Packit Service a31ea6
		    end = xmlXPtrGetNthChild(cur, index2 - 1);
Packit Service a31ea6
		    index2 = 0;
Packit Service a31ea6
		}
Packit Service a31ea6
		if ((cur == start) && (index1 > 1)) {
Packit Service a31ea6
		    cur = xmlXPtrGetNthChild(cur, index1 - 1);
Packit Service a31ea6
		    index1 = 0;
Packit Service a31ea6
		} else {
Packit Service a31ea6
		    cur = cur->children;
Packit Service a31ea6
		}
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) &&
Packit Service a31ea6
		   (list == NULL) /* looks superfluous but ... */ ) {
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
		    }
Packit Service a31ea6
		    tmp = xmlNewText(content);
Packit Service a31ea6
		}
Packit Service a31ea6
		last = list = tmp;
Packit Service a31ea6
	    } else {
Packit Service a31ea6
		if ((cur == start) && (index1 > 1)) {
Packit Service a31ea6
		    tmp = xmlCopyNode(cur, 0);
Packit Service a31ea6
		    list = tmp;
Packit Service a31ea6
		    parent = tmp;
Packit Service a31ea6
		    last = NULL;
Packit Service a31ea6
		    cur = xmlXPtrGetNthChild(cur, index1 - 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
		tmp = xmlCopyNode(cur, 1);
Packit Service a31ea6
		list = tmp;
Packit Service a31ea6
		parent = NULL;
Packit Service a31ea6
		last = tmp;
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
		    TODO /* 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
		    STRANGE
Packit Service a31ea6
		    break;
Packit Service a31ea6
		default:
Packit Service a31ea6
		    tmp = xmlCopyNode(cur, 1);
Packit Service a31ea6
		    break;
Packit Service a31ea6
	    }
Packit Service a31ea6
	    if (tmp != NULL) {
Packit Service a31ea6
		if ((list == NULL) || ((last == NULL) && (parent == NULL)))  {
Packit Service a31ea6
		    STRANGE
Packit Service a31ea6
		    return(NULL);
Packit Service a31ea6
		}
Packit Service a31ea6
		if (last != NULL)
Packit Service a31ea6
		    xmlAddNextSibling(last, tmp);
Packit Service a31ea6
		else {
Packit Service a31ea6
		    xmlAddChild(parent, tmp);
Packit Service a31ea6
		    last = tmp;
Packit Service a31ea6
		}
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
	if ((list == NULL) || ((last == NULL) && (parent == NULL)))  {
Packit Service a31ea6
	    STRANGE
Packit Service a31ea6
	    return(NULL);
Packit Service a31ea6
	}
Packit Service a31ea6
	cur = xmlXPtrAdvanceNode(cur, NULL);
Packit Service a31ea6
    }
Packit Service a31ea6
    return(list);
Packit Service a31ea6
}
Packit Service a31ea6
Packit Service a31ea6
/**
Packit Service a31ea6
 * xmlXPtrBuildNodeList:
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
xmlNodePtr
Packit Service a31ea6
xmlXPtrBuildNodeList(xmlXPathObjectPtr obj) {
Packit Service a31ea6
    xmlNodePtr list = NULL, last = NULL;
Packit Service a31ea6
    int i;
Packit Service a31ea6
Packit Service a31ea6
    if (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_START:
Packit Service a31ea6
		    case XML_XINCLUDE_END:
Packit Service a31ea6
			break;
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 = xmlCopyNode(set->nodeTab[i], 1);
Packit Service a31ea6
		else {
Packit Service a31ea6
		    xmlAddNextSibling(last, xmlCopyNode(set->nodeTab[i], 1));
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
	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 = xmlXPtrBuildNodeList(set->locTab[i]);
Packit Service a31ea6
		else
Packit Service a31ea6
		    xmlAddNextSibling(last,
Packit Service a31ea6
			    xmlXPtrBuildNodeList(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(xmlXPtrBuildRangeNodeList(obj));
Packit Service a31ea6
	case XPATH_POINT:
Packit Service a31ea6
	    return(xmlCopyNode(obj->user, 0));
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
 *									*
Packit Service a31ea6
 *			XPointer functions				*
Packit Service a31ea6
 *									*
Packit Service a31ea6
 ************************************************************************/
Packit Service a31ea6
Packit Service a31ea6
/**
Packit Service a31ea6
 * xmlXPtrNbLocChildren:
Packit Service a31ea6
 * @node:  an xmlNodePtr
Packit Service a31ea6
 *
Packit Service a31ea6
 * Count the number of location children of @node or the length of the
Packit Service a31ea6
 * string value in case of text/PI/Comments nodes
Packit Service a31ea6
 *
Packit Service a31ea6
 * Returns the number of location children
Packit Service a31ea6
 */
Packit Service a31ea6
static int
Packit Service a31ea6
xmlXPtrNbLocChildren(xmlNodePtr node) {
Packit Service a31ea6
    int ret = 0;
Packit Service a31ea6
    if (node == NULL)
Packit Service a31ea6
	return(-1);
Packit Service a31ea6
    switch (node->type) {
Packit Service a31ea6
        case XML_HTML_DOCUMENT_NODE:
Packit Service a31ea6
        case XML_DOCUMENT_NODE:
Packit Service a31ea6
        case XML_ELEMENT_NODE:
Packit Service a31ea6
	    node = node->children;
Packit Service a31ea6
	    while (node != NULL) {
Packit Service a31ea6
		if (node->type == XML_ELEMENT_NODE)
Packit Service a31ea6
		    ret++;
Packit Service a31ea6
		node = node->next;
Packit Service a31ea6
	    }
Packit Service a31ea6
	    break;
Packit Service a31ea6
        case XML_ATTRIBUTE_NODE:
Packit Service a31ea6
	    return(-1);
Packit Service a31ea6
Packit Service a31ea6
        case XML_PI_NODE:
Packit Service a31ea6
        case XML_COMMENT_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
	    ret = xmlStrlen(node->content);
Packit Service a31ea6
	    break;
Packit Service a31ea6
	default:
Packit Service a31ea6
	    return(-1);
Packit Service a31ea6
    }
Packit Service a31ea6
    return(ret);
Packit Service a31ea6
}
Packit Service a31ea6
Packit Service a31ea6
/**
Packit Service a31ea6
 * xmlXPtrHereFunction:
Packit Service a31ea6
 * @ctxt:  the XPointer Parser context
Packit Service a31ea6
 * @nargs:  the number of args
Packit Service a31ea6
 *
Packit Service a31ea6
 * Function implementing here() operation
Packit Service a31ea6
 * as described in 5.4.3
Packit Service a31ea6
 */
Packit Service a31ea6
static void
Packit Service a31ea6
xmlXPtrHereFunction(xmlXPathParserContextPtr ctxt, int nargs) {
Packit Service a31ea6
    CHECK_ARITY(0);
Packit Service a31ea6
Packit Service a31ea6
    if (ctxt->context->here == NULL)
Packit Service a31ea6
	XP_ERROR(XPTR_SYNTAX_ERROR);
Packit Service a31ea6
Packit Service a31ea6
    valuePush(ctxt, xmlXPtrNewLocationSetNodes(ctxt->context->here, NULL));
Packit Service a31ea6
}
Packit Service a31ea6
Packit Service a31ea6
/**
Packit Service a31ea6
 * xmlXPtrOriginFunction:
Packit Service a31ea6
 * @ctxt:  the XPointer Parser context
Packit Service a31ea6
 * @nargs:  the number of args
Packit Service a31ea6
 *
Packit Service a31ea6
 * Function implementing origin() operation
Packit Service a31ea6
 * as described in 5.4.3
Packit Service a31ea6
 */
Packit Service a31ea6
static void
Packit Service a31ea6
xmlXPtrOriginFunction(xmlXPathParserContextPtr ctxt, int nargs) {
Packit Service a31ea6
    CHECK_ARITY(0);
Packit Service a31ea6
Packit Service a31ea6
    if (ctxt->context->origin == NULL)
Packit Service a31ea6
	XP_ERROR(XPTR_SYNTAX_ERROR);
Packit Service a31ea6
Packit Service a31ea6
    valuePush(ctxt, xmlXPtrNewLocationSetNodes(ctxt->context->origin, NULL));
Packit Service a31ea6
}
Packit Service a31ea6
Packit Service a31ea6
/**
Packit Service a31ea6
 * xmlXPtrStartPointFunction:
Packit Service a31ea6
 * @ctxt:  the XPointer Parser context
Packit Service a31ea6
 * @nargs:  the number of args
Packit Service a31ea6
 *
Packit Service a31ea6
 * Function implementing start-point() operation
Packit Service a31ea6
 * as described in 5.4.3
Packit Service a31ea6
 * ----------------
Packit Service a31ea6
 * location-set start-point(location-set)
Packit Service a31ea6
 *
Packit Service a31ea6
 * For each location x in the argument location-set, start-point adds a
Packit Service a31ea6
 * location of type point to the result location-set. That point represents
Packit Service a31ea6
 * the start point of location x and is determined by the following rules:
Packit Service a31ea6
 *
Packit Service a31ea6
 * - If x is of type point, the start point is x.
Packit Service a31ea6
 * - If x is of type range, the start point is the start point of x.
Packit Service a31ea6
 * - If x is of type root, element, text, comment, or processing instruction,
Packit Service a31ea6
 * - the container node of the start point is x and the index is 0.
Packit Service a31ea6
 * - If x is of type attribute or namespace, the function must signal a
Packit Service a31ea6
 *   syntax error.
Packit Service a31ea6
 * ----------------
Packit Service a31ea6
 *
Packit Service a31ea6
 */
Packit Service a31ea6
static void
Packit Service a31ea6
xmlXPtrStartPointFunction(xmlXPathParserContextPtr ctxt, int nargs) {
Packit Service a31ea6
    xmlXPathObjectPtr tmp, obj, point;
Packit Service a31ea6
    xmlLocationSetPtr newset = NULL;
Packit Service a31ea6
    xmlLocationSetPtr oldset = NULL;
Packit Service a31ea6
Packit Service a31ea6
    CHECK_ARITY(1);
Packit Service a31ea6
    if ((ctxt->value == NULL) ||
Packit Service a31ea6
	((ctxt->value->type != XPATH_LOCATIONSET) &&
Packit Service a31ea6
	 (ctxt->value->type != XPATH_NODESET)))
Packit Service a31ea6
        XP_ERROR(XPATH_INVALID_TYPE)
Packit Service a31ea6
Packit Service a31ea6
    obj = valuePop(ctxt);
Packit Service a31ea6
    if (obj->type == XPATH_NODESET) {
Packit Service a31ea6
	/*
Packit Service a31ea6
	 * First convert to a location set
Packit Service a31ea6
	 */
Packit Service a31ea6
	tmp = xmlXPtrNewLocationSetNodeSet(obj->nodesetval);
Packit Service a31ea6
	xmlXPathFreeObject(obj);
Packit Service a31ea6
	if (tmp == NULL)
Packit Service a31ea6
            XP_ERROR(XPATH_MEMORY_ERROR)
Packit Service a31ea6
	obj = tmp;
Packit Service a31ea6
    }
Packit Service a31ea6
Packit Service a31ea6
    newset = xmlXPtrLocationSetCreate(NULL);
Packit Service a31ea6
    if (newset == NULL) {
Packit Service a31ea6
	xmlXPathFreeObject(obj);
Packit Service a31ea6
        XP_ERROR(XPATH_MEMORY_ERROR);
Packit Service a31ea6
    }
Packit Service a31ea6
    oldset = (xmlLocationSetPtr) obj->user;
Packit Service a31ea6
    if (oldset != NULL) {
Packit Service a31ea6
	int i;
Packit Service a31ea6
Packit Service a31ea6
	for (i = 0; i < oldset->locNr; i++) {
Packit Service a31ea6
	    tmp = oldset->locTab[i];
Packit Service a31ea6
	    if (tmp == NULL)
Packit Service a31ea6
		continue;
Packit Service a31ea6
	    point = NULL;
Packit Service a31ea6
	    switch (tmp->type) {
Packit Service a31ea6
		case XPATH_POINT:
Packit Service a31ea6
		    point = xmlXPtrNewPoint(tmp->user, tmp->index);
Packit Service a31ea6
		    break;
Packit Service a31ea6
		case XPATH_RANGE: {
Packit Service a31ea6
		    xmlNodePtr node = tmp->user;
Packit Service a31ea6
		    if (node != NULL) {
Packit Service a31ea6
			if ((node->type == XML_ATTRIBUTE_NODE) ||
Packit Service a31ea6
                            (node->type == XML_NAMESPACE_DECL)) {
Packit Service a31ea6
			    xmlXPathFreeObject(obj);
Packit Service a31ea6
			    xmlXPtrFreeLocationSet(newset);
Packit Service a31ea6
			    XP_ERROR(XPTR_SYNTAX_ERROR);
Packit Service a31ea6
			}
Packit Service a31ea6
			point = xmlXPtrNewPoint(node, tmp->index);
Packit Service a31ea6
		    }
Packit Service a31ea6
		    break;
Packit Service a31ea6
	        }
Packit Service a31ea6
		default:
Packit Service a31ea6
		    /*** Should we raise an error ?
Packit Service a31ea6
		    xmlXPathFreeObject(obj);
Packit Service a31ea6
		    xmlXPathFreeObject(newset);
Packit Service a31ea6
		    XP_ERROR(XPATH_INVALID_TYPE)
Packit Service a31ea6
		    ***/
Packit Service a31ea6
		    break;
Packit Service a31ea6
	    }
Packit Service a31ea6
            if (point != NULL)
Packit Service a31ea6
		xmlXPtrLocationSetAdd(newset, point);
Packit Service a31ea6
	}
Packit Service a31ea6
    }
Packit Service a31ea6
    xmlXPathFreeObject(obj);
Packit Service a31ea6
    valuePush(ctxt, xmlXPtrWrapLocationSet(newset));
Packit Service a31ea6
}
Packit Service a31ea6
Packit Service a31ea6
/**
Packit Service a31ea6
 * xmlXPtrEndPointFunction:
Packit Service a31ea6
 * @ctxt:  the XPointer Parser context
Packit Service a31ea6
 * @nargs:  the number of args
Packit Service a31ea6
 *
Packit Service a31ea6
 * Function implementing end-point() operation
Packit Service a31ea6
 * as described in 5.4.3
Packit Service a31ea6
 * ----------------------------
Packit Service a31ea6
 * location-set end-point(location-set)
Packit Service a31ea6
 *
Packit Service a31ea6
 * For each location x in the argument location-set, end-point adds a
Packit Service a31ea6
 * location of type point to the result location-set. That point represents
Packit Service a31ea6
 * the end point of location x and is determined by the following rules:
Packit Service a31ea6
 *
Packit Service a31ea6
 * - If x is of type point, the resulting point is x.
Packit Service a31ea6
 * - If x is of type range, the resulting point is the end point of x.
Packit Service a31ea6
 * - If x is of type root or element, the container node of the resulting
Packit Service a31ea6
 *   point is x and the index is the number of location children of x.
Packit Service a31ea6
 * - If x is of type text, comment, or processing instruction, the container
Packit Service a31ea6
 *   node of the resulting point is x and the index is the length of the
Packit Service a31ea6
 *   string-value of x.
Packit Service a31ea6
 * - If x is of type attribute or namespace, the function must signal a
Packit Service a31ea6
 *   syntax error.
Packit Service a31ea6
 * ----------------------------
Packit Service a31ea6
 */
Packit Service a31ea6
static void
Packit Service a31ea6
xmlXPtrEndPointFunction(xmlXPathParserContextPtr ctxt, int nargs) {
Packit Service a31ea6
    xmlXPathObjectPtr tmp, obj, point;
Packit Service a31ea6
    xmlLocationSetPtr newset = NULL;
Packit Service a31ea6
    xmlLocationSetPtr oldset = NULL;
Packit Service a31ea6
Packit Service a31ea6
    CHECK_ARITY(1);
Packit Service a31ea6
    if ((ctxt->value == NULL) ||
Packit Service a31ea6
	((ctxt->value->type != XPATH_LOCATIONSET) &&
Packit Service a31ea6
	 (ctxt->value->type != XPATH_NODESET)))
Packit Service a31ea6
        XP_ERROR(XPATH_INVALID_TYPE)
Packit Service a31ea6
Packit Service a31ea6
    obj = valuePop(ctxt);
Packit Service a31ea6
    if (obj->type == XPATH_NODESET) {
Packit Service a31ea6
	/*
Packit Service a31ea6
	 * First convert to a location set
Packit Service a31ea6
	 */
Packit Service a31ea6
	tmp = xmlXPtrNewLocationSetNodeSet(obj->nodesetval);
Packit Service a31ea6
	xmlXPathFreeObject(obj);
Packit Service a31ea6
	if (tmp == NULL)
Packit Service a31ea6
            XP_ERROR(XPATH_MEMORY_ERROR)
Packit Service a31ea6
	obj = tmp;
Packit Service a31ea6
    }
Packit Service a31ea6
Packit Service a31ea6
    newset = xmlXPtrLocationSetCreate(NULL);
Packit Service a31ea6
    if (newset == NULL) {
Packit Service a31ea6
	xmlXPathFreeObject(obj);
Packit Service a31ea6
        XP_ERROR(XPATH_MEMORY_ERROR);
Packit Service a31ea6
    }
Packit Service a31ea6
    oldset = (xmlLocationSetPtr) obj->user;
Packit Service a31ea6
    if (oldset != NULL) {
Packit Service a31ea6
	int i;
Packit Service a31ea6
Packit Service a31ea6
	for (i = 0; i < oldset->locNr; i++) {
Packit Service a31ea6
	    tmp = oldset->locTab[i];
Packit Service a31ea6
	    if (tmp == NULL)
Packit Service a31ea6
		continue;
Packit Service a31ea6
	    point = NULL;
Packit Service a31ea6
	    switch (tmp->type) {
Packit Service a31ea6
		case XPATH_POINT:
Packit Service a31ea6
		    point = xmlXPtrNewPoint(tmp->user, tmp->index);
Packit Service a31ea6
		    break;
Packit Service a31ea6
		case XPATH_RANGE: {
Packit Service a31ea6
		    xmlNodePtr node = tmp->user2;
Packit Service a31ea6
		    if (node != NULL) {
Packit Service a31ea6
			if ((node->type == XML_ATTRIBUTE_NODE) ||
Packit Service a31ea6
                            (node->type == XML_NAMESPACE_DECL)) {
Packit Service a31ea6
			    xmlXPathFreeObject(obj);
Packit Service a31ea6
			    xmlXPtrFreeLocationSet(newset);
Packit Service a31ea6
			    XP_ERROR(XPTR_SYNTAX_ERROR);
Packit Service a31ea6
			}
Packit Service a31ea6
			point = xmlXPtrNewPoint(node, tmp->index2);
Packit Service a31ea6
		    } else if (tmp->user == NULL) {
Packit Service a31ea6
			point = xmlXPtrNewPoint(node,
Packit Service a31ea6
				       xmlXPtrNbLocChildren(node));
Packit Service a31ea6
		    }
Packit Service a31ea6
		    break;
Packit Service a31ea6
	        }
Packit Service a31ea6
		default:
Packit Service a31ea6
		    /*** Should we raise an error ?
Packit Service a31ea6
		    xmlXPathFreeObject(obj);
Packit Service a31ea6
		    xmlXPathFreeObject(newset);
Packit Service a31ea6
		    XP_ERROR(XPATH_INVALID_TYPE)
Packit Service a31ea6
		    ***/
Packit Service a31ea6
		    break;
Packit Service a31ea6
	    }
Packit Service a31ea6
            if (point != NULL)
Packit Service a31ea6
		xmlXPtrLocationSetAdd(newset, point);
Packit Service a31ea6
	}
Packit Service a31ea6
    }
Packit Service a31ea6
    xmlXPathFreeObject(obj);
Packit Service a31ea6
    valuePush(ctxt, xmlXPtrWrapLocationSet(newset));
Packit Service a31ea6
}
Packit Service a31ea6
Packit Service a31ea6
Packit Service a31ea6
/**
Packit Service a31ea6
 * xmlXPtrCoveringRange:
Packit Service a31ea6
 * @ctxt:  the XPointer Parser context
Packit Service a31ea6
 * @loc:  the location for which the covering range must be computed
Packit Service a31ea6
 *
Packit Service a31ea6
 * A covering range is a range that wholly encompasses a location
Packit Service a31ea6
 * Section 5.3.3. Covering Ranges for All Location Types
Packit Service a31ea6
 *        http://www.w3.org/TR/xptr#N2267
Packit Service a31ea6
 *
Packit Service a31ea6
 * Returns a new location or NULL in case of error
Packit Service a31ea6
 */
Packit Service a31ea6
static xmlXPathObjectPtr
Packit Service a31ea6
xmlXPtrCoveringRange(xmlXPathParserContextPtr ctxt, xmlXPathObjectPtr loc) {
Packit Service a31ea6
    if (loc == NULL)
Packit Service a31ea6
	return(NULL);
Packit Service a31ea6
    if ((ctxt == NULL) || (ctxt->context == NULL) ||
Packit Service a31ea6
	(ctxt->context->doc == NULL))
Packit Service a31ea6
	return(NULL);
Packit Service a31ea6
    switch (loc->type) {
Packit Service a31ea6
        case XPATH_POINT:
Packit Service a31ea6
	    return(xmlXPtrNewRange(loc->user, loc->index,
Packit Service a31ea6
			           loc->user, loc->index));
Packit Service a31ea6
        case XPATH_RANGE:
Packit Service a31ea6
	    if (loc->user2 != NULL) {
Packit Service a31ea6
		return(xmlXPtrNewRange(loc->user, loc->index,
Packit Service a31ea6
			              loc->user2, loc->index2));
Packit Service a31ea6
	    } else {
Packit Service a31ea6
		xmlNodePtr node = (xmlNodePtr) loc->user;
Packit Service a31ea6
		if (node == (xmlNodePtr) ctxt->context->doc) {
Packit Service a31ea6
		    return(xmlXPtrNewRange(node, 0, node,
Packit Service a31ea6
					   xmlXPtrGetArity(node)));
Packit Service a31ea6
		} else {
Packit Service a31ea6
		    switch (node->type) {
Packit Service a31ea6
			case XML_ATTRIBUTE_NODE:
Packit Service a31ea6
			/* !!! our model is slightly different than XPath */
Packit Service a31ea6
			    return(xmlXPtrNewRange(node, 0, node,
Packit Service a31ea6
					           xmlXPtrGetArity(node)));
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_PI_NODE:
Packit Service a31ea6
			case XML_COMMENT_NODE:
Packit Service a31ea6
			case XML_DOCUMENT_NODE:
Packit Service a31ea6
			case XML_NOTATION_NODE:
Packit Service a31ea6
			case XML_HTML_DOCUMENT_NODE: {
Packit Service a31ea6
			    int indx = xmlXPtrGetIndex(node);
Packit Service a31ea6
Packit Service a31ea6
			    node = node->parent;
Packit Service a31ea6
			    return(xmlXPtrNewRange(node, indx - 1,
Packit Service a31ea6
					           node, indx + 1));
Packit Service a31ea6
			}
Packit Service a31ea6
			default:
Packit Service a31ea6
			    return(NULL);
Packit Service a31ea6
		    }
Packit Service a31ea6
		}
Packit Service a31ea6
	    }
Packit Service a31ea6
	default:
Packit Service a31ea6
	    TODO /* missed one case ??? */
Packit Service a31ea6
    }
Packit Service a31ea6
    return(NULL);
Packit Service a31ea6
}
Packit Service a31ea6
Packit Service a31ea6
/**
Packit Service a31ea6
 * xmlXPtrRangeFunction:
Packit Service a31ea6
 * @ctxt:  the XPointer Parser context
Packit Service a31ea6
 * @nargs:  the number of args
Packit Service a31ea6
 *
Packit Service a31ea6
 * Function implementing the range() function 5.4.3
Packit Service a31ea6
 *  location-set range(location-set )
Packit Service a31ea6
 *
Packit Service a31ea6
 *  The range function returns ranges covering the locations in
Packit Service a31ea6
 *  the argument location-set. For each location x in the argument
Packit Service a31ea6
 *  location-set, a range location representing the covering range of
Packit Service a31ea6
 *  x is added to the result location-set.
Packit Service a31ea6
 */
Packit Service a31ea6
static void
Packit Service a31ea6
xmlXPtrRangeFunction(xmlXPathParserContextPtr ctxt, int nargs) {
Packit Service a31ea6
    int i;
Packit Service a31ea6
    xmlXPathObjectPtr set;
Packit Service a31ea6
    xmlLocationSetPtr oldset;
Packit Service a31ea6
    xmlLocationSetPtr newset;
Packit Service a31ea6
Packit Service a31ea6
    CHECK_ARITY(1);
Packit Service a31ea6
    if ((ctxt->value == NULL) ||
Packit Service a31ea6
	((ctxt->value->type != XPATH_LOCATIONSET) &&
Packit Service a31ea6
	 (ctxt->value->type != XPATH_NODESET)))
Packit Service a31ea6
        XP_ERROR(XPATH_INVALID_TYPE)
Packit Service a31ea6
Packit Service a31ea6
    set = valuePop(ctxt);
Packit Service a31ea6
    if (set->type == XPATH_NODESET) {
Packit Service a31ea6
	xmlXPathObjectPtr tmp;
Packit Service a31ea6
Packit Service a31ea6
	/*
Packit Service a31ea6
	 * First convert to a location set
Packit Service a31ea6
	 */
Packit Service a31ea6
	tmp = xmlXPtrNewLocationSetNodeSet(set->nodesetval);
Packit Service a31ea6
	xmlXPathFreeObject(set);
Packit Service a31ea6
	if (tmp == NULL)
Packit Service a31ea6
            XP_ERROR(XPATH_MEMORY_ERROR)
Packit Service a31ea6
	set = tmp;
Packit Service a31ea6
    }
Packit Service a31ea6
    oldset = (xmlLocationSetPtr) set->user;
Packit Service a31ea6
Packit Service a31ea6
    /*
Packit Service a31ea6
     * The loop is to compute the covering range for each item and add it
Packit Service a31ea6
     */
Packit Service a31ea6
    newset = xmlXPtrLocationSetCreate(NULL);
Packit Service a31ea6
    if (newset == NULL) {
Packit Service a31ea6
	xmlXPathFreeObject(set);
Packit Service a31ea6
        XP_ERROR(XPATH_MEMORY_ERROR);
Packit Service a31ea6
    }
Packit Service a31ea6
    if (oldset != NULL) {
Packit Service a31ea6
        for (i = 0;i < oldset->locNr;i++) {
Packit Service a31ea6
            xmlXPtrLocationSetAdd(newset,
Packit Service a31ea6
                    xmlXPtrCoveringRange(ctxt, oldset->locTab[i]));
Packit Service a31ea6
        }
Packit Service a31ea6
    }
Packit Service a31ea6
Packit Service a31ea6
    /*
Packit Service a31ea6
     * Save the new value and cleanup
Packit Service a31ea6
     */
Packit Service a31ea6
    valuePush(ctxt, xmlXPtrWrapLocationSet(newset));
Packit Service a31ea6
    xmlXPathFreeObject(set);
Packit Service a31ea6
}
Packit Service a31ea6
Packit Service a31ea6
/**
Packit Service a31ea6
 * xmlXPtrInsideRange:
Packit Service a31ea6
 * @ctxt:  the XPointer Parser context
Packit Service a31ea6
 * @loc:  the location for which the inside range must be computed
Packit Service a31ea6
 *
Packit Service a31ea6
 * A inside range is a range described in the range-inside() description
Packit Service a31ea6
 *
Packit Service a31ea6
 * Returns a new location or NULL in case of error
Packit Service a31ea6
 */
Packit Service a31ea6
static xmlXPathObjectPtr
Packit Service a31ea6
xmlXPtrInsideRange(xmlXPathParserContextPtr ctxt, xmlXPathObjectPtr loc) {
Packit Service a31ea6
    if (loc == NULL)
Packit Service a31ea6
	return(NULL);
Packit Service a31ea6
    if ((ctxt == NULL) || (ctxt->context == NULL) ||
Packit Service a31ea6
	(ctxt->context->doc == NULL))
Packit Service a31ea6
	return(NULL);
Packit Service a31ea6
    switch (loc->type) {
Packit Service a31ea6
        case XPATH_POINT: {
Packit Service a31ea6
	    xmlNodePtr node = (xmlNodePtr) loc->user;
Packit Service a31ea6
	    switch (node->type) {
Packit Service a31ea6
		case XML_PI_NODE:
Packit Service a31ea6
		case XML_COMMENT_NODE:
Packit Service a31ea6
		case XML_TEXT_NODE:
Packit Service a31ea6
		case XML_CDATA_SECTION_NODE: {
Packit Service a31ea6
		    if (node->content == NULL) {
Packit Service a31ea6
			return(xmlXPtrNewRange(node, 0, node, 0));
Packit Service a31ea6
		    } else {
Packit Service a31ea6
			return(xmlXPtrNewRange(node, 0, node,
Packit Service a31ea6
					       xmlStrlen(node->content)));
Packit Service a31ea6
		    }
Packit Service a31ea6
		}
Packit Service a31ea6
		case XML_ATTRIBUTE_NODE:
Packit Service a31ea6
		case XML_ELEMENT_NODE:
Packit Service a31ea6
		case XML_ENTITY_REF_NODE:
Packit Service a31ea6
		case XML_DOCUMENT_NODE:
Packit Service a31ea6
		case XML_NOTATION_NODE:
Packit Service a31ea6
		case XML_HTML_DOCUMENT_NODE: {
Packit Service a31ea6
		    return(xmlXPtrNewRange(node, 0, node,
Packit Service a31ea6
					   xmlXPtrGetArity(node)));
Packit Service a31ea6
		}
Packit Service a31ea6
		default:
Packit Service a31ea6
		    break;
Packit Service a31ea6
	    }
Packit Service a31ea6
	    return(NULL);
Packit Service a31ea6
	}
Packit Service a31ea6
        case XPATH_RANGE: {
Packit Service a31ea6
	    xmlNodePtr node = (xmlNodePtr) loc->user;
Packit Service a31ea6
	    if (loc->user2 != NULL) {
Packit Service a31ea6
		return(xmlXPtrNewRange(node, loc->index,
Packit Service a31ea6
			               loc->user2, loc->index2));
Packit Service a31ea6
	    } else {
Packit Service a31ea6
		switch (node->type) {
Packit Service a31ea6
		    case XML_PI_NODE:
Packit Service a31ea6
		    case XML_COMMENT_NODE:
Packit Service a31ea6
		    case XML_TEXT_NODE:
Packit Service a31ea6
		    case XML_CDATA_SECTION_NODE: {
Packit Service a31ea6
			if (node->content == NULL) {
Packit Service a31ea6
			    return(xmlXPtrNewRange(node, 0, node, 0));
Packit Service a31ea6
			} else {
Packit Service a31ea6
			    return(xmlXPtrNewRange(node, 0, node,
Packit Service a31ea6
						   xmlStrlen(node->content)));
Packit Service a31ea6
			}
Packit Service a31ea6
		    }
Packit Service a31ea6
		    case XML_ATTRIBUTE_NODE:
Packit Service a31ea6
		    case XML_ELEMENT_NODE:
Packit Service a31ea6
		    case XML_ENTITY_REF_NODE:
Packit Service a31ea6
		    case XML_DOCUMENT_NODE:
Packit Service a31ea6
		    case XML_NOTATION_NODE:
Packit Service a31ea6
		    case XML_HTML_DOCUMENT_NODE: {
Packit Service a31ea6
			return(xmlXPtrNewRange(node, 0, node,
Packit Service a31ea6
					       xmlXPtrGetArity(node)));
Packit Service a31ea6
		    }
Packit Service a31ea6
		    default:
Packit Service a31ea6
			break;
Packit Service a31ea6
		}
Packit Service a31ea6
		return(NULL);
Packit Service a31ea6
	    }
Packit Service a31ea6
        }
Packit Service a31ea6
	default:
Packit Service a31ea6
	    TODO /* missed one case ??? */
Packit Service a31ea6
    }
Packit Service a31ea6
    return(NULL);
Packit Service a31ea6
}
Packit Service a31ea6
Packit Service a31ea6
/**
Packit Service a31ea6
 * xmlXPtrRangeInsideFunction:
Packit Service a31ea6
 * @ctxt:  the XPointer Parser context
Packit Service a31ea6
 * @nargs:  the number of args
Packit Service a31ea6
 *
Packit Service a31ea6
 * Function implementing the range-inside() function 5.4.3
Packit Service a31ea6
 *  location-set range-inside(location-set )
Packit Service a31ea6
 *
Packit Service a31ea6
 *  The range-inside function returns ranges covering the contents of
Packit Service a31ea6
 *  the locations in the argument location-set. For each location x in
Packit Service a31ea6
 *  the argument location-set, a range location is added to the result
Packit Service a31ea6
 *  location-set. If x is a range location, then x is added to the
Packit Service a31ea6
 *  result location-set. If x is not a range location, then x is used
Packit Service a31ea6
 *  as the container location of the start and end points of the range
Packit Service a31ea6
 *  location to be added; the index of the start point of the range is
Packit Service a31ea6
 *  zero; if the end point is a character point then its index is the
Packit Service a31ea6
 *  length of the string-value of x, and otherwise is the number of
Packit Service a31ea6
 *  location children of x.
Packit Service a31ea6
 *
Packit Service a31ea6
 */
Packit Service a31ea6
static void
Packit Service a31ea6
xmlXPtrRangeInsideFunction(xmlXPathParserContextPtr ctxt, int nargs) {
Packit Service a31ea6
    int i;
Packit Service a31ea6
    xmlXPathObjectPtr set;
Packit Service a31ea6
    xmlLocationSetPtr oldset;
Packit Service a31ea6
    xmlLocationSetPtr newset;
Packit Service a31ea6
Packit Service a31ea6
    CHECK_ARITY(1);
Packit Service a31ea6
    if ((ctxt->value == NULL) ||
Packit Service a31ea6
	((ctxt->value->type != XPATH_LOCATIONSET) &&
Packit Service a31ea6
	 (ctxt->value->type != XPATH_NODESET)))
Packit Service a31ea6
        XP_ERROR(XPATH_INVALID_TYPE)
Packit Service a31ea6
Packit Service a31ea6
    set = valuePop(ctxt);
Packit Service a31ea6
    if (set->type == XPATH_NODESET) {
Packit Service a31ea6
	xmlXPathObjectPtr tmp;
Packit Service a31ea6
Packit Service a31ea6
	/*
Packit Service a31ea6
	 * First convert to a location set
Packit Service a31ea6
	 */
Packit Service a31ea6
	tmp = xmlXPtrNewLocationSetNodeSet(set->nodesetval);
Packit Service a31ea6
	xmlXPathFreeObject(set);
Packit Service a31ea6
	if (tmp == NULL)
Packit Service a31ea6
	     XP_ERROR(XPATH_MEMORY_ERROR)
Packit Service a31ea6
	set = tmp;
Packit Service a31ea6
    }
Packit Service a31ea6
    oldset = (xmlLocationSetPtr) set->user;
Packit Service a31ea6
Packit Service a31ea6
    /*
Packit Service a31ea6
     * The loop is to compute the covering range for each item and add it
Packit Service a31ea6
     */
Packit Service a31ea6
    newset = xmlXPtrLocationSetCreate(NULL);
Packit Service a31ea6
    if (newset == NULL) {
Packit Service a31ea6
	xmlXPathFreeObject(set);
Packit Service a31ea6
        XP_ERROR(XPATH_MEMORY_ERROR);
Packit Service a31ea6
    }
Packit Service a31ea6
    for (i = 0;i < oldset->locNr;i++) {
Packit Service a31ea6
	xmlXPtrLocationSetAdd(newset,
Packit Service a31ea6
		xmlXPtrInsideRange(ctxt, oldset->locTab[i]));
Packit Service a31ea6
    }
Packit Service a31ea6
Packit Service a31ea6
    /*
Packit Service a31ea6
     * Save the new value and cleanup
Packit Service a31ea6
     */
Packit Service a31ea6
    valuePush(ctxt, xmlXPtrWrapLocationSet(newset));
Packit Service a31ea6
    xmlXPathFreeObject(set);
Packit Service a31ea6
}
Packit Service a31ea6
Packit Service a31ea6
/**
Packit Service a31ea6
 * xmlXPtrRangeToFunction:
Packit Service a31ea6
 * @ctxt:  the XPointer Parser context
Packit Service a31ea6
 * @nargs:  the number of args
Packit Service a31ea6
 *
Packit Service a31ea6
 * Implement the range-to() XPointer function
Packit Service a31ea6
 *
Packit Service a31ea6
 * Obsolete. range-to is not a real function but a special type of location
Packit Service a31ea6
 * step which is handled in xpath.c.
Packit Service a31ea6
 */
Packit Service a31ea6
void
Packit Service a31ea6
xmlXPtrRangeToFunction(xmlXPathParserContextPtr ctxt,
Packit Service a31ea6
                       int nargs ATTRIBUTE_UNUSED) {
Packit Service a31ea6
    XP_ERROR(XPATH_EXPR_ERROR);
Packit Service a31ea6
}
Packit Service a31ea6
Packit Service a31ea6
/**
Packit Service a31ea6
 * xmlXPtrAdvanceNode:
Packit Service a31ea6
 * @cur:  the node
Packit Service a31ea6
 * @level: incremented/decremented to show level in tree
Packit Service a31ea6
 *
Packit Service a31ea6
 * Advance to the next element or text node in document order
Packit Service a31ea6
 * TODO: add a stack for entering/exiting entities
Packit Service a31ea6
 *
Packit Service a31ea6
 * Returns -1 in case of failure, 0 otherwise
Packit Service a31ea6
 */
Packit Service a31ea6
xmlNodePtr
Packit Service a31ea6
xmlXPtrAdvanceNode(xmlNodePtr cur, int *level) {
Packit Service a31ea6
next:
Packit Service a31ea6
    if ((cur == NULL) || (cur->type == XML_NAMESPACE_DECL))
Packit Service a31ea6
	return(NULL);
Packit Service a31ea6
    if (cur->children != NULL) {
Packit Service a31ea6
        cur = cur->children ;
Packit Service a31ea6
	if (level != NULL)
Packit Service a31ea6
	    (*level)++;
Packit Service a31ea6
	goto found;
Packit Service a31ea6
    }
Packit Service a31ea6
skip:		/* This label should only be needed if something is wrong! */
Packit Service a31ea6
    if (cur->next != NULL) {
Packit Service a31ea6
	cur = cur->next;
Packit Service a31ea6
	goto found;
Packit Service a31ea6
    }
Packit Service a31ea6
    do {
Packit Service a31ea6
        cur = cur->parent;
Packit Service a31ea6
	if (level != NULL)
Packit Service a31ea6
	    (*level)--;
Packit Service a31ea6
        if (cur == NULL) return(NULL);
Packit Service a31ea6
        if (cur->next != NULL) {
Packit Service a31ea6
	    cur = cur->next;
Packit Service a31ea6
	    goto found;
Packit Service a31ea6
	}
Packit Service a31ea6
    } while (cur != NULL);
Packit Service a31ea6
Packit Service a31ea6
found:
Packit Service a31ea6
    if ((cur->type != XML_ELEMENT_NODE) &&
Packit Service a31ea6
	(cur->type != XML_TEXT_NODE) &&
Packit Service a31ea6
	(cur->type != XML_DOCUMENT_NODE) &&
Packit Service a31ea6
	(cur->type != XML_HTML_DOCUMENT_NODE) &&
Packit Service a31ea6
	(cur->type != XML_CDATA_SECTION_NODE)) {
Packit Service a31ea6
	    if (cur->type == XML_ENTITY_REF_NODE) {	/* Shouldn't happen */
Packit Service a31ea6
		TODO
Packit Service a31ea6
		goto skip;
Packit Service a31ea6
	    }
Packit Service a31ea6
	    goto next;
Packit Service a31ea6
	}
Packit Service a31ea6
    return(cur);
Packit Service a31ea6
}
Packit Service a31ea6
Packit Service a31ea6
/**
Packit Service a31ea6
 * xmlXPtrAdvanceChar:
Packit Service a31ea6
 * @node:  the node
Packit Service a31ea6
 * @indx:  the indx
Packit Service a31ea6
 * @bytes:  the number of bytes
Packit Service a31ea6
 *
Packit Service a31ea6
 * Advance a point of the associated number of bytes (not UTF8 chars)
Packit Service a31ea6
 *
Packit Service a31ea6
 * Returns -1 in case of failure, 0 otherwise
Packit Service a31ea6
 */
Packit Service a31ea6
static int
Packit Service a31ea6
xmlXPtrAdvanceChar(xmlNodePtr *node, int *indx, int bytes) {
Packit Service a31ea6
    xmlNodePtr cur;
Packit Service a31ea6
    int pos;
Packit Service a31ea6
    int len;
Packit Service a31ea6
Packit Service a31ea6
    if ((node == NULL) || (indx == NULL))
Packit Service a31ea6
	return(-1);
Packit Service a31ea6
    cur = *node;
Packit Service a31ea6
    if ((cur == NULL) || (cur->type == XML_NAMESPACE_DECL))
Packit Service a31ea6
	return(-1);
Packit Service a31ea6
    pos = *indx;
Packit Service a31ea6
Packit Service a31ea6
    while (bytes >= 0) {
Packit Service a31ea6
	/*
Packit Service a31ea6
	 * First position to the beginning of the first text node
Packit Service a31ea6
	 * corresponding to this point
Packit Service a31ea6
	 */
Packit Service a31ea6
	while ((cur != NULL) &&
Packit Service a31ea6
	       ((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
	    if (pos > 0) {
Packit Service a31ea6
		cur = xmlXPtrGetNthChild(cur, pos);
Packit Service a31ea6
		pos = 0;
Packit Service a31ea6
	    } else {
Packit Service a31ea6
		cur = xmlXPtrAdvanceNode(cur, NULL);
Packit Service a31ea6
		pos = 0;
Packit Service a31ea6
	    }
Packit Service a31ea6
	}
Packit Service a31ea6
Packit Service a31ea6
	if (cur == NULL) {
Packit Service a31ea6
	    *node = NULL;
Packit Service a31ea6
	    *indx = 0;
Packit Service a31ea6
	    return(-1);
Packit Service a31ea6
	}
Packit Service a31ea6
Packit Service a31ea6
	/*
Packit Service a31ea6
	 * if there is no move needed return the current value.
Packit Service a31ea6
	 */
Packit Service a31ea6
	if (pos == 0) pos = 1;
Packit Service a31ea6
	if (bytes == 0) {
Packit Service a31ea6
	    *node = cur;
Packit Service a31ea6
	    *indx = pos;
Packit Service a31ea6
	    return(0);
Packit Service a31ea6
	}
Packit Service a31ea6
	/*
Packit Service a31ea6
	 * We should have a text (or cdata) node ...
Packit Service a31ea6
	 */
Packit Service a31ea6
	len = 0;
Packit Service a31ea6
	if ((cur->type != XML_ELEMENT_NODE) &&
Packit Service a31ea6
            (cur->content != NULL)) {
Packit Service a31ea6
	    len = xmlStrlen(cur->content);
Packit Service a31ea6
	}
Packit Service a31ea6
	if (pos > len) {
Packit Service a31ea6
	    /* Strange, the indx in the text node is greater than it's len */
Packit Service a31ea6
	    STRANGE
Packit Service a31ea6
	    pos = len;
Packit Service a31ea6
	}
Packit Service a31ea6
	if (pos + bytes >= len) {
Packit Service a31ea6
	    bytes -= (len - pos);
Packit Service a31ea6
	    cur = xmlXPtrAdvanceNode(cur, NULL);
Packit Service a31ea6
	    pos = 0;
Packit Service a31ea6
	} else if (pos + bytes < len) {
Packit Service a31ea6
	    pos += bytes;
Packit Service a31ea6
	    *node = cur;
Packit Service a31ea6
	    *indx = pos;
Packit Service a31ea6
	    return(0);
Packit Service a31ea6
	}
Packit Service a31ea6
    }
Packit Service a31ea6
    return(-1);
Packit Service a31ea6
}
Packit Service a31ea6
Packit Service a31ea6
/**
Packit Service a31ea6
 * xmlXPtrMatchString:
Packit Service a31ea6
 * @string:  the string to search
Packit Service a31ea6
 * @start:  the start textnode
Packit Service a31ea6
 * @startindex:  the start index
Packit Service a31ea6
 * @end:  the end textnode IN/OUT
Packit Service a31ea6
 * @endindex:  the end index IN/OUT
Packit Service a31ea6
 *
Packit Service a31ea6
 * Check whether the document contains @string at the position
Packit Service a31ea6
 * (@start, @startindex) and limited by the (@end, @endindex) point
Packit Service a31ea6
 *
Packit Service a31ea6
 * Returns -1 in case of failure, 0 if not found, 1 if found in which case
Packit Service a31ea6
 *            (@start, @startindex) will indicate the position of the beginning
Packit Service a31ea6
 *            of the range and (@end, @endindex) will indicate the end
Packit Service a31ea6
 *            of the range
Packit Service a31ea6
 */
Packit Service a31ea6
static int
Packit Service a31ea6
xmlXPtrMatchString(const xmlChar *string, xmlNodePtr start, int startindex,
Packit Service a31ea6
	            xmlNodePtr *end, int *endindex) {
Packit Service a31ea6
    xmlNodePtr cur;
Packit Service a31ea6
    int pos; /* 0 based */
Packit Service a31ea6
    int len; /* in bytes */
Packit Service a31ea6
    int stringlen; /* in bytes */
Packit Service a31ea6
    int match;
Packit Service a31ea6
Packit Service a31ea6
    if (string == NULL)
Packit Service a31ea6
	return(-1);
Packit Service a31ea6
    if ((start == NULL) || (start->type == XML_NAMESPACE_DECL))
Packit Service a31ea6
	return(-1);
Packit Service a31ea6
    if ((end == NULL) || (*end == NULL) ||
Packit Service a31ea6
        ((*end)->type == XML_NAMESPACE_DECL) || (endindex == NULL))
Packit Service a31ea6
	return(-1);
Packit Service a31ea6
    cur = start;
Packit Service a31ea6
    pos = startindex - 1;
Packit Service a31ea6
    stringlen = xmlStrlen(string);
Packit Service a31ea6
Packit Service a31ea6
    while (stringlen > 0) {
Packit Service a31ea6
	if ((cur == *end) && (pos + stringlen > *endindex))
Packit Service a31ea6
	    return(0);
Packit Service a31ea6
Packit Service a31ea6
	if ((cur->type != XML_ELEMENT_NODE) && (cur->content != NULL)) {
Packit Service a31ea6
	    len = xmlStrlen(cur->content);
Packit Service a31ea6
	    if (len >= pos + stringlen) {
Packit Service a31ea6
		match = (!xmlStrncmp(&cur->content[pos], string, stringlen));
Packit Service a31ea6
		if (match) {
Packit Service a31ea6
#ifdef DEBUG_RANGES
Packit Service a31ea6
		    xmlGenericError(xmlGenericErrorContext,
Packit Service a31ea6
			    "found range %d bytes at index %d of ->",
Packit Service a31ea6
			    stringlen, pos + 1);
Packit Service a31ea6
		    xmlDebugDumpString(stdout, cur->content);
Packit Service a31ea6
		    xmlGenericError(xmlGenericErrorContext, "\n");
Packit Service a31ea6
#endif
Packit Service a31ea6
		    *end = cur;
Packit Service a31ea6
		    *endindex = pos + stringlen;
Packit Service a31ea6
		    return(1);
Packit Service a31ea6
		} else {
Packit Service a31ea6
		    return(0);
Packit Service a31ea6
		}
Packit Service a31ea6
	    } else {
Packit Service a31ea6
                int sub = len - pos;
Packit Service a31ea6
		match = (!xmlStrncmp(&cur->content[pos], string, sub));
Packit Service a31ea6
		if (match) {
Packit Service a31ea6
#ifdef DEBUG_RANGES
Packit Service a31ea6
		    xmlGenericError(xmlGenericErrorContext,
Packit Service a31ea6
			    "found subrange %d bytes at index %d of ->",
Packit Service a31ea6
			    sub, pos + 1);
Packit Service a31ea6
		    xmlDebugDumpString(stdout, cur->content);
Packit Service a31ea6
		    xmlGenericError(xmlGenericErrorContext, "\n");
Packit Service a31ea6
#endif
Packit Service a31ea6
                    string = &string[sub];
Packit Service a31ea6
		    stringlen -= sub;
Packit Service a31ea6
		} else {
Packit Service a31ea6
		    return(0);
Packit Service a31ea6
		}
Packit Service a31ea6
	    }
Packit Service a31ea6
	}
Packit Service a31ea6
	cur = xmlXPtrAdvanceNode(cur, NULL);
Packit Service a31ea6
	if (cur == NULL)
Packit Service a31ea6
	    return(0);
Packit Service a31ea6
	pos = 0;
Packit Service a31ea6
    }
Packit Service a31ea6
    return(1);
Packit Service a31ea6
}
Packit Service a31ea6
Packit Service a31ea6
/**
Packit Service a31ea6
 * xmlXPtrSearchString:
Packit Service a31ea6
 * @string:  the string to search
Packit Service a31ea6
 * @start:  the start textnode IN/OUT
Packit Service a31ea6
 * @startindex:  the start index IN/OUT
Packit Service a31ea6
 * @end:  the end textnode
Packit Service a31ea6
 * @endindex:  the end index
Packit Service a31ea6
 *
Packit Service a31ea6
 * Search the next occurrence of @string within the document content
Packit Service a31ea6
 * until the (@end, @endindex) point is reached
Packit Service a31ea6
 *
Packit Service a31ea6
 * Returns -1 in case of failure, 0 if not found, 1 if found in which case
Packit Service a31ea6
 *            (@start, @startindex) will indicate the position of the beginning
Packit Service a31ea6
 *            of the range and (@end, @endindex) will indicate the end
Packit Service a31ea6
 *            of the range
Packit Service a31ea6
 */
Packit Service a31ea6
static int
Packit Service a31ea6
xmlXPtrSearchString(const xmlChar *string, xmlNodePtr *start, int *startindex,
Packit Service a31ea6
	            xmlNodePtr *end, int *endindex) {
Packit Service a31ea6
    xmlNodePtr cur;
Packit Service a31ea6
    const xmlChar *str;
Packit Service a31ea6
    int pos; /* 0 based */
Packit Service a31ea6
    int len; /* in bytes */
Packit Service a31ea6
    xmlChar first;
Packit Service a31ea6
Packit Service a31ea6
    if (string == NULL)
Packit Service a31ea6
	return(-1);
Packit Service a31ea6
    if ((start == NULL) || (*start == NULL) ||
Packit Service a31ea6
        ((*start)->type == XML_NAMESPACE_DECL) || (startindex == NULL))
Packit Service a31ea6
	return(-1);
Packit Service a31ea6
    if ((end == NULL) || (endindex == NULL))
Packit Service a31ea6
	return(-1);
Packit Service a31ea6
    cur = *start;
Packit Service a31ea6
    pos = *startindex - 1;
Packit Service a31ea6
    first = string[0];
Packit Service a31ea6
Packit Service a31ea6
    while (cur != NULL) {
Packit Service a31ea6
	if ((cur->type != XML_ELEMENT_NODE) && (cur->content != NULL)) {
Packit Service a31ea6
	    len = xmlStrlen(cur->content);
Packit Service a31ea6
	    while (pos <= len) {
Packit Service a31ea6
		if (first != 0) {
Packit Service a31ea6
		    str = xmlStrchr(&cur->content[pos], first);
Packit Service a31ea6
		    if (str != NULL) {
Packit Service a31ea6
			pos = (str - (xmlChar *)(cur->content));
Packit Service a31ea6
#ifdef DEBUG_RANGES
Packit Service a31ea6
			xmlGenericError(xmlGenericErrorContext,
Packit Service a31ea6
				"found '%c' at index %d of ->",
Packit Service a31ea6
				first, pos + 1);
Packit Service a31ea6
			xmlDebugDumpString(stdout, cur->content);
Packit Service a31ea6
			xmlGenericError(xmlGenericErrorContext, "\n");
Packit Service a31ea6
#endif
Packit Service a31ea6
			if (xmlXPtrMatchString(string, cur, pos + 1,
Packit Service a31ea6
					       end, endindex)) {
Packit Service a31ea6
			    *start = cur;
Packit Service a31ea6
			    *startindex = pos + 1;
Packit Service a31ea6
			    return(1);
Packit Service a31ea6
			}
Packit Service a31ea6
			pos++;
Packit Service a31ea6
		    } else {
Packit Service a31ea6
			pos = len + 1;
Packit Service a31ea6
		    }
Packit Service a31ea6
		} else {
Packit Service a31ea6
		    /*
Packit Service a31ea6
		     * An empty string is considered to match before each
Packit Service a31ea6
		     * character of the string-value and after the final
Packit Service a31ea6
		     * character.
Packit Service a31ea6
		     */
Packit Service a31ea6
#ifdef DEBUG_RANGES
Packit Service a31ea6
		    xmlGenericError(xmlGenericErrorContext,
Packit Service a31ea6
			    "found '' at index %d of ->",
Packit Service a31ea6
			    pos + 1);
Packit Service a31ea6
		    xmlDebugDumpString(stdout, cur->content);
Packit Service a31ea6
		    xmlGenericError(xmlGenericErrorContext, "\n");
Packit Service a31ea6
#endif
Packit Service a31ea6
		    *start = cur;
Packit Service a31ea6
		    *startindex = pos + 1;
Packit Service a31ea6
		    *end = cur;
Packit Service a31ea6
		    *endindex = pos + 1;
Packit Service a31ea6
		    return(1);
Packit Service a31ea6
		}
Packit Service a31ea6
	    }
Packit Service a31ea6
	}
Packit Service a31ea6
	if ((cur == *end) && (pos >= *endindex))
Packit Service a31ea6
	    return(0);
Packit Service a31ea6
	cur = xmlXPtrAdvanceNode(cur, NULL);
Packit Service a31ea6
	if (cur == NULL)
Packit Service a31ea6
	    return(0);
Packit Service a31ea6
	pos = 1;
Packit Service a31ea6
    }
Packit Service a31ea6
    return(0);
Packit Service a31ea6
}
Packit Service a31ea6
Packit Service a31ea6
/**
Packit Service a31ea6
 * xmlXPtrGetLastChar:
Packit Service a31ea6
 * @node:  the node
Packit Service a31ea6
 * @index:  the index
Packit Service a31ea6
 *
Packit Service a31ea6
 * Computes the point coordinates of the last char of this point
Packit Service a31ea6
 *
Packit Service a31ea6
 * Returns -1 in case of failure, 0 otherwise
Packit Service a31ea6
 */
Packit Service a31ea6
static int
Packit Service a31ea6
xmlXPtrGetLastChar(xmlNodePtr *node, int *indx) {
Packit Service a31ea6
    xmlNodePtr cur;
Packit Service a31ea6
    int pos, len = 0;
Packit Service a31ea6
Packit Service a31ea6
    if ((node == NULL) || (*node == NULL) ||
Packit Service a31ea6
        ((*node)->type == XML_NAMESPACE_DECL) || (indx == NULL))
Packit Service a31ea6
	return(-1);
Packit Service a31ea6
    cur = *node;
Packit Service a31ea6
    pos = *indx;
Packit Service a31ea6
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
	if (pos > 0) {
Packit Service a31ea6
	    cur = xmlXPtrGetNthChild(cur, pos);
Packit Service a31ea6
	}
Packit Service a31ea6
    }
Packit Service a31ea6
    while (cur != NULL) {
Packit Service a31ea6
	if (cur->last != NULL)
Packit Service a31ea6
	    cur = cur->last;
Packit Service a31ea6
	else if ((cur->type != XML_ELEMENT_NODE) &&
Packit Service a31ea6
	         (cur->content != NULL)) {
Packit Service a31ea6
	    len = xmlStrlen(cur->content);
Packit Service a31ea6
	    break;
Packit Service a31ea6
	} else {
Packit Service a31ea6
	    return(-1);
Packit Service a31ea6
	}
Packit Service a31ea6
    }
Packit Service a31ea6
    if (cur == NULL)
Packit Service a31ea6
	return(-1);
Packit Service a31ea6
    *node = cur;
Packit Service a31ea6
    *indx = len;
Packit Service a31ea6
    return(0);
Packit Service a31ea6
}
Packit Service a31ea6
Packit Service a31ea6
/**
Packit Service a31ea6
 * xmlXPtrGetStartPoint:
Packit Service a31ea6
 * @obj:  an range
Packit Service a31ea6
 * @node:  the resulting node
Packit Service a31ea6
 * @indx:  the resulting index
Packit Service a31ea6
 *
Packit Service a31ea6
 * read the object and return the start point coordinates.
Packit Service a31ea6
 *
Packit Service a31ea6
 * Returns -1 in case of failure, 0 otherwise
Packit Service a31ea6
 */
Packit Service a31ea6
static int
Packit Service a31ea6
xmlXPtrGetStartPoint(xmlXPathObjectPtr obj, xmlNodePtr *node, int *indx) {
Packit Service a31ea6
    if ((obj == NULL) || (node == NULL) || (indx == NULL))
Packit Service a31ea6
	return(-1);
Packit Service a31ea6
Packit Service a31ea6
    switch (obj->type) {
Packit Service a31ea6
        case XPATH_POINT:
Packit Service a31ea6
	    *node = obj->user;
Packit Service a31ea6
	    if (obj->index <= 0)
Packit Service a31ea6
		*indx = 0;
Packit Service a31ea6
	    else
Packit Service a31ea6
		*indx = obj->index;
Packit Service a31ea6
	    return(0);
Packit Service a31ea6
        case XPATH_RANGE:
Packit Service a31ea6
	    *node = obj->user;
Packit Service a31ea6
	    if (obj->index <= 0)
Packit Service a31ea6
		*indx = 0;
Packit Service a31ea6
	    else
Packit Service a31ea6
		*indx = obj->index;
Packit Service a31ea6
	    return(0);
Packit Service a31ea6
	default:
Packit Service a31ea6
	    break;
Packit Service a31ea6
    }
Packit Service a31ea6
    return(-1);
Packit Service a31ea6
}
Packit Service a31ea6
Packit Service a31ea6
/**
Packit Service a31ea6
 * xmlXPtrGetEndPoint:
Packit Service a31ea6
 * @obj:  an range
Packit Service a31ea6
 * @node:  the resulting node
Packit Service a31ea6
 * @indx:  the resulting indx
Packit Service a31ea6
 *
Packit Service a31ea6
 * read the object and return the end point coordinates.
Packit Service a31ea6
 *
Packit Service a31ea6
 * Returns -1 in case of failure, 0 otherwise
Packit Service a31ea6
 */
Packit Service a31ea6
static int
Packit Service a31ea6
xmlXPtrGetEndPoint(xmlXPathObjectPtr obj, xmlNodePtr *node, int *indx) {
Packit Service a31ea6
    if ((obj == NULL) || (node == NULL) || (indx == NULL))
Packit Service a31ea6
	return(-1);
Packit Service a31ea6
Packit Service a31ea6
    switch (obj->type) {
Packit Service a31ea6
        case XPATH_POINT:
Packit Service a31ea6
	    *node = obj->user;
Packit Service a31ea6
	    if (obj->index <= 0)
Packit Service a31ea6
		*indx = 0;
Packit Service a31ea6
	    else
Packit Service a31ea6
		*indx = obj->index;
Packit Service a31ea6
	    return(0);
Packit Service a31ea6
        case XPATH_RANGE:
Packit Service a31ea6
	    *node = obj->user;
Packit Service a31ea6
	    if (obj->index <= 0)
Packit Service a31ea6
		*indx = 0;
Packit Service a31ea6
	    else
Packit Service a31ea6
		*indx = obj->index;
Packit Service a31ea6
	    return(0);
Packit Service a31ea6
	default:
Packit Service a31ea6
	    break;
Packit Service a31ea6
    }
Packit Service a31ea6
    return(-1);
Packit Service a31ea6
}
Packit Service a31ea6
Packit Service a31ea6
/**
Packit Service a31ea6
 * xmlXPtrStringRangeFunction:
Packit Service a31ea6
 * @ctxt:  the XPointer Parser context
Packit Service a31ea6
 * @nargs:  the number of args
Packit Service a31ea6
 *
Packit Service a31ea6
 * Function implementing the string-range() function
Packit Service a31ea6
 * range as described in 5.4.2
Packit Service a31ea6
 *
Packit Service a31ea6
 * ------------------------------
Packit Service a31ea6
 * [Definition: For each location in the location-set argument,
Packit Service a31ea6
 * string-range returns a set of string ranges, a set of substrings in a
Packit Service a31ea6
 * string. Specifically, the string-value of the location is searched for
Packit Service a31ea6
 * substrings that match the string argument, and the resulting location-set
Packit Service a31ea6
 * will contain a range location for each non-overlapping match.]
Packit Service a31ea6
 * An empty string is considered to match before each character of the
Packit Service a31ea6
 * string-value and after the final character. Whitespace in a string
Packit Service a31ea6
 * is matched literally, with no normalization except that provided by
Packit Service a31ea6
 * XML for line ends. The third argument gives the position of the first
Packit Service a31ea6
 * character to be in the resulting range, relative to the start of the
Packit Service a31ea6
 * match. The default value is 1, which makes the range start immediately
Packit Service a31ea6
 * before the first character of the matched string. The fourth argument
Packit Service a31ea6
 * gives the number of characters in the range; the default is that the
Packit Service a31ea6
 * range extends to the end of the matched string.
Packit Service a31ea6
 *
Packit Service a31ea6
 * Element boundaries, as well as entire embedded nodes such as processing
Packit Service a31ea6
 * instructions and comments, are ignored as defined in [XPath].
Packit Service a31ea6
 *
Packit Service a31ea6
 * If the string in the second argument is not found in the string-value
Packit Service a31ea6
 * of the location, or if a value in the third or fourth argument indicates
Packit Service a31ea6
 * a string that is beyond the beginning or end of the document, the
Packit Service a31ea6
 * expression fails.
Packit Service a31ea6
 *
Packit Service a31ea6
 * The points of the range-locations in the returned location-set will
Packit Service a31ea6
 * all be character points.
Packit Service a31ea6
 * ------------------------------
Packit Service a31ea6
 */
Packit Service a31ea6
static void
Packit Service a31ea6
xmlXPtrStringRangeFunction(xmlXPathParserContextPtr ctxt, int nargs) {
Packit Service a31ea6
    int i, startindex, endindex = 0, fendindex;
Packit Service a31ea6
    xmlNodePtr start, end = 0, fend;
Packit Service a31ea6
    xmlXPathObjectPtr set;
Packit Service a31ea6
    xmlLocationSetPtr oldset;
Packit Service a31ea6
    xmlLocationSetPtr newset;
Packit Service a31ea6
    xmlXPathObjectPtr string;
Packit Service a31ea6
    xmlXPathObjectPtr position = NULL;
Packit Service a31ea6
    xmlXPathObjectPtr number = NULL;
Packit Service a31ea6
    int found, pos = 0, num = 0;
Packit Service a31ea6
Packit Service a31ea6
    /*
Packit Service a31ea6
     * Grab the arguments
Packit Service a31ea6
     */
Packit Service a31ea6
    if ((nargs < 2) || (nargs > 4))
Packit Service a31ea6
	XP_ERROR(XPATH_INVALID_ARITY);
Packit Service a31ea6
Packit Service a31ea6
    if (nargs >= 4) {
Packit Service a31ea6
	CHECK_TYPE(XPATH_NUMBER);
Packit Service a31ea6
	number = valuePop(ctxt);
Packit Service a31ea6
	if (number != NULL)
Packit Service a31ea6
	    num = (int) number->floatval;
Packit Service a31ea6
    }
Packit Service a31ea6
    if (nargs >= 3) {
Packit Service a31ea6
	CHECK_TYPE(XPATH_NUMBER);
Packit Service a31ea6
	position = valuePop(ctxt);
Packit Service a31ea6
	if (position != NULL)
Packit Service a31ea6
	    pos = (int) position->floatval;
Packit Service a31ea6
    }
Packit Service a31ea6
    CHECK_TYPE(XPATH_STRING);
Packit Service a31ea6
    string = valuePop(ctxt);
Packit Service a31ea6
    if ((ctxt->value == NULL) ||
Packit Service a31ea6
	((ctxt->value->type != XPATH_LOCATIONSET) &&
Packit Service a31ea6
	 (ctxt->value->type != XPATH_NODESET)))
Packit Service a31ea6
        XP_ERROR(XPATH_INVALID_TYPE)
Packit Service a31ea6
Packit Service a31ea6
    set = valuePop(ctxt);
Packit Service a31ea6
    newset = xmlXPtrLocationSetCreate(NULL);
Packit Service a31ea6
    if (newset == NULL) {
Packit Service a31ea6
	xmlXPathFreeObject(set);
Packit Service a31ea6
        XP_ERROR(XPATH_MEMORY_ERROR);
Packit Service a31ea6
    }
Packit Service a31ea6
    if (set->nodesetval == NULL) {
Packit Service a31ea6
        goto error;
Packit Service a31ea6
    }
Packit Service a31ea6
    if (set->type == XPATH_NODESET) {
Packit Service a31ea6
	xmlXPathObjectPtr tmp;
Packit Service a31ea6
Packit Service a31ea6
	/*
Packit Service a31ea6
	 * First convert to a location set
Packit Service a31ea6
	 */
Packit Service a31ea6
	tmp = xmlXPtrNewLocationSetNodeSet(set->nodesetval);
Packit Service a31ea6
	xmlXPathFreeObject(set);
Packit Service a31ea6
	if (tmp == NULL)
Packit Service a31ea6
	     XP_ERROR(XPATH_MEMORY_ERROR)
Packit Service a31ea6
	set = tmp;
Packit Service a31ea6
    }
Packit Service a31ea6
    oldset = (xmlLocationSetPtr) set->user;
Packit Service a31ea6
Packit Service a31ea6
    /*
Packit Service a31ea6
     * The loop is to search for each element in the location set
Packit Service a31ea6
     * the list of location set corresponding to that search
Packit Service a31ea6
     */
Packit Service a31ea6
    for (i = 0;i < oldset->locNr;i++) {
Packit Service a31ea6
#ifdef DEBUG_RANGES
Packit Service a31ea6
	xmlXPathDebugDumpObject(stdout, oldset->locTab[i], 0);
Packit Service a31ea6
#endif
Packit Service a31ea6
Packit Service a31ea6
	xmlXPtrGetStartPoint(oldset->locTab[i], &start, &startindex);
Packit Service a31ea6
	xmlXPtrGetEndPoint(oldset->locTab[i], &end, &endindex);
Packit Service a31ea6
	xmlXPtrAdvanceChar(&start, &startindex, 0);
Packit Service a31ea6
	xmlXPtrGetLastChar(&end, &endindex);
Packit Service a31ea6
Packit Service a31ea6
#ifdef DEBUG_RANGES
Packit Service a31ea6
	xmlGenericError(xmlGenericErrorContext,
Packit Service a31ea6
		"from index %d of ->", startindex);
Packit Service a31ea6
	xmlDebugDumpString(stdout, start->content);
Packit Service a31ea6
	xmlGenericError(xmlGenericErrorContext, "\n");
Packit Service a31ea6
	xmlGenericError(xmlGenericErrorContext,
Packit Service a31ea6
		"to index %d of ->", endindex);
Packit Service a31ea6
	xmlDebugDumpString(stdout, end->content);
Packit Service a31ea6
	xmlGenericError(xmlGenericErrorContext, "\n");
Packit Service a31ea6
#endif
Packit Service a31ea6
	do {
Packit Service a31ea6
            fend = end;
Packit Service a31ea6
            fendindex = endindex;
Packit Service a31ea6
	    found = xmlXPtrSearchString(string->stringval, &start, &startindex,
Packit Service a31ea6
		                        &fend, &fendindex);
Packit Service a31ea6
	    if (found == 1) {
Packit Service a31ea6
		if (position == NULL) {
Packit Service a31ea6
		    xmlXPtrLocationSetAdd(newset,
Packit Service a31ea6
			 xmlXPtrNewRange(start, startindex, fend, fendindex));
Packit Service a31ea6
		} else if (xmlXPtrAdvanceChar(&start, &startindex,
Packit Service a31ea6
			                      pos - 1) == 0) {
Packit Service a31ea6
		    if ((number != NULL) && (num > 0)) {
Packit Service a31ea6
			int rindx;
Packit Service a31ea6
			xmlNodePtr rend;
Packit Service a31ea6
			rend = start;
Packit Service a31ea6
			rindx = startindex - 1;
Packit Service a31ea6
			if (xmlXPtrAdvanceChar(&rend, &rindx,
Packit Service a31ea6
				               num) == 0) {
Packit Service a31ea6
			    xmlXPtrLocationSetAdd(newset,
Packit Service a31ea6
					xmlXPtrNewRange(start, startindex,
Packit Service a31ea6
							rend, rindx));
Packit Service a31ea6
			}
Packit Service a31ea6
		    } else if ((number != NULL) && (num <= 0)) {
Packit Service a31ea6
			xmlXPtrLocationSetAdd(newset,
Packit Service a31ea6
				    xmlXPtrNewRange(start, startindex,
Packit Service a31ea6
						    start, startindex));
Packit Service a31ea6
		    } else {
Packit Service a31ea6
			xmlXPtrLocationSetAdd(newset,
Packit Service a31ea6
				    xmlXPtrNewRange(start, startindex,
Packit Service a31ea6
						    fend, fendindex));
Packit Service a31ea6
		    }
Packit Service a31ea6
		}
Packit Service a31ea6
		start = fend;
Packit Service a31ea6
		startindex = fendindex;
Packit Service a31ea6
		if (string->stringval[0] == 0)
Packit Service a31ea6
		    startindex++;
Packit Service a31ea6
	    }
Packit Service a31ea6
	} while (found == 1);
Packit Service a31ea6
    }
Packit Service a31ea6
Packit Service a31ea6
    /*
Packit Service a31ea6
     * Save the new value and cleanup
Packit Service a31ea6
     */
Packit Service a31ea6
error:
Packit Service a31ea6
    valuePush(ctxt, xmlXPtrWrapLocationSet(newset));
Packit Service a31ea6
    xmlXPathFreeObject(set);
Packit Service a31ea6
    xmlXPathFreeObject(string);
Packit Service a31ea6
    if (position) xmlXPathFreeObject(position);
Packit Service a31ea6
    if (number) xmlXPathFreeObject(number);
Packit Service a31ea6
}
Packit Service a31ea6
Packit Service a31ea6
/**
Packit Service a31ea6
 * xmlXPtrEvalRangePredicate:
Packit Service a31ea6
 * @ctxt:  the XPointer Parser context
Packit Service a31ea6
 *
Packit Service a31ea6
 *  [8]   Predicate ::=   '[' PredicateExpr ']'
Packit Service a31ea6
 *  [9]   PredicateExpr ::=   Expr
Packit Service a31ea6
 *
Packit Service a31ea6
 * Evaluate a predicate as in xmlXPathEvalPredicate() but for
Packit Service a31ea6
 * a Location Set instead of a node set
Packit Service a31ea6
 */
Packit Service a31ea6
void
Packit Service a31ea6
xmlXPtrEvalRangePredicate(xmlXPathParserContextPtr ctxt) {
Packit Service a31ea6
    const xmlChar *cur;
Packit Service a31ea6
    xmlXPathObjectPtr res;
Packit Service a31ea6
    xmlXPathObjectPtr obj, tmp;
Packit Service a31ea6
    xmlLocationSetPtr newset = NULL;
Packit Service a31ea6
    xmlLocationSetPtr oldset;
Packit Service a31ea6
    int i;
Packit Service a31ea6
Packit Service a31ea6
    if (ctxt == NULL) return;
Packit Service a31ea6
Packit Service a31ea6
    SKIP_BLANKS;
Packit Service a31ea6
    if (CUR != '[') {
Packit Service a31ea6
	XP_ERROR(XPATH_INVALID_PREDICATE_ERROR);
Packit Service a31ea6
    }
Packit Service a31ea6
    NEXT;
Packit Service a31ea6
    SKIP_BLANKS;
Packit Service a31ea6
Packit Service a31ea6
    /*
Packit Service a31ea6
     * Extract the old set, and then evaluate the result of the
Packit Service a31ea6
     * expression for all the element in the set. use it to grow
Packit Service a31ea6
     * up a new set.
Packit Service a31ea6
     */
Packit Service a31ea6
    CHECK_TYPE(XPATH_LOCATIONSET);
Packit Service a31ea6
    obj = valuePop(ctxt);
Packit Service a31ea6
    oldset = obj->user;
Packit Service a31ea6
    ctxt->context->node = NULL;
Packit Service a31ea6
Packit Service a31ea6
    if ((oldset == NULL) || (oldset->locNr == 0)) {
Packit Service a31ea6
	ctxt->context->contextSize = 0;
Packit Service a31ea6
	ctxt->context->proximityPosition = 0;
Packit Service a31ea6
	xmlXPathEvalExpr(ctxt);
Packit Service a31ea6
	res = valuePop(ctxt);
Packit Service a31ea6
	if (res != NULL)
Packit Service a31ea6
	    xmlXPathFreeObject(res);
Packit Service a31ea6
	valuePush(ctxt, obj);
Packit Service a31ea6
	CHECK_ERROR;
Packit Service a31ea6
    } else {
Packit Service a31ea6
	/*
Packit Service a31ea6
	 * Save the expression pointer since we will have to evaluate
Packit Service a31ea6
	 * it multiple times. Initialize the new set.
Packit Service a31ea6
	 */
Packit Service a31ea6
        cur = ctxt->cur;
Packit Service a31ea6
	newset = xmlXPtrLocationSetCreate(NULL);
Packit Service a31ea6
Packit Service a31ea6
        for (i = 0; i < oldset->locNr; i++) {
Packit Service a31ea6
	    ctxt->cur = cur;
Packit Service a31ea6
Packit Service a31ea6
	    /*
Packit Service a31ea6
	     * Run the evaluation with a node list made of a single item
Packit Service a31ea6
	     * in the nodeset.
Packit Service a31ea6
	     */
Packit Service a31ea6
	    ctxt->context->node = oldset->locTab[i]->user;
Packit Service a31ea6
	    tmp = xmlXPathNewNodeSet(ctxt->context->node);
Packit Service a31ea6
	    valuePush(ctxt, tmp);
Packit Service a31ea6
	    ctxt->context->contextSize = oldset->locNr;
Packit Service a31ea6
	    ctxt->context->proximityPosition = i + 1;
Packit Service a31ea6
Packit Service a31ea6
	    xmlXPathEvalExpr(ctxt);
Packit Service a31ea6
	    CHECK_ERROR;
Packit Service a31ea6
Packit Service a31ea6
	    /*
Packit Service a31ea6
	     * The result of the evaluation need to be tested to
Packit Service a31ea6
	     * decided whether the filter succeeded or not
Packit Service a31ea6
	     */
Packit Service a31ea6
	    res = valuePop(ctxt);
Packit Service a31ea6
	    if (xmlXPathEvaluatePredicateResult(ctxt, res)) {
Packit Service a31ea6
	        xmlXPtrLocationSetAdd(newset,
Packit Service a31ea6
			xmlXPathObjectCopy(oldset->locTab[i]));
Packit Service a31ea6
	    }
Packit Service a31ea6
Packit Service a31ea6
	    /*
Packit Service a31ea6
	     * Cleanup
Packit Service a31ea6
	     */
Packit Service a31ea6
	    if (res != NULL)
Packit Service a31ea6
		xmlXPathFreeObject(res);
Packit Service a31ea6
	    if (ctxt->value == tmp) {
Packit Service a31ea6
		res = valuePop(ctxt);
Packit Service a31ea6
		xmlXPathFreeObject(res);
Packit Service a31ea6
	    }
Packit Service a31ea6
Packit Service a31ea6
	    ctxt->context->node = NULL;
Packit Service a31ea6
	}
Packit Service a31ea6
Packit Service a31ea6
	/*
Packit Service a31ea6
	 * The result is used as the new evaluation set.
Packit Service a31ea6
	 */
Packit Service a31ea6
	xmlXPathFreeObject(obj);
Packit Service a31ea6
	ctxt->context->node = NULL;
Packit Service a31ea6
	ctxt->context->contextSize = -1;
Packit Service a31ea6
	ctxt->context->proximityPosition = -1;
Packit Service a31ea6
	valuePush(ctxt, xmlXPtrWrapLocationSet(newset));
Packit Service a31ea6
    }
Packit Service a31ea6
    if (CUR != ']') {
Packit Service a31ea6
	XP_ERROR(XPATH_INVALID_PREDICATE_ERROR);
Packit Service a31ea6
    }
Packit Service a31ea6
Packit Service a31ea6
    NEXT;
Packit Service a31ea6
    SKIP_BLANKS;
Packit Service a31ea6
}
Packit Service a31ea6
Packit Service a31ea6
#define bottom_xpointer
Packit Service a31ea6
#include "elfgcchack.h"
Packit Service a31ea6
#endif
Packit Service a31ea6