Blame xpointer.c

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