blob: ad2c288277e0f575727be86b82e14505b2b4fd2e [file] [log] [blame]
Owen Taylor3473f882001-02-23 17:55:21 +00001/*
2 * xpointer.c : Code to handle XML Pointer
3 *
Daniel Veillard9a237c92003-02-13 15:52:58 +00004 * Base implementation was made accordingly to
5 * W3C Candidate Recommendation 7 June 2000
Owen Taylor3473f882001-02-23 17:55:21 +00006 * http://www.w3.org/TR/2000/CR-xptr-20000607
7 *
Daniel Veillard9a237c92003-02-13 15:52:58 +00008 * Added support for the element() scheme described in:
9 * W3C Proposed Recommendation 13 November 2002
Daniel Veillardf8e3db02012-09-11 13:26:36 +080010 * http://www.w3.org/TR/2002/PR-xptr-element-20021113/
Daniel Veillard9a237c92003-02-13 15:52:58 +000011 *
Owen Taylor3473f882001-02-23 17:55:21 +000012 * See Copyright for the status of this software.
13 *
Daniel Veillardc5d64342001-06-24 12:13:24 +000014 * daniel@veillard.com
Owen Taylor3473f882001-02-23 17:55:21 +000015 */
16
Stéphane Michaut454e3972017-08-28 14:30:43 +020017/* To avoid EBCDIC trouble when parsing on zOS */
18#if defined(__MVS__)
19#pragma convert("ISO8859-1")
20#endif
21
Daniel Veillard34ce8be2002-03-18 19:37:11 +000022#define IN_LIBXML
Bjorn Reese70a9da52001-04-21 16:57:29 +000023#include "libxml.h"
Owen Taylor3473f882001-02-23 17:55:21 +000024
Daniel Veillardf69bb4b2001-05-19 13:24:56 +000025/*
Owen Taylor3473f882001-02-23 17:55:21 +000026 * TODO: better handling of error cases, the full expression should
27 * be parsed beforehand instead of a progressive evaluation
28 * TODO: Access into entities references are not supported now ...
29 * need a start to be able to pop out of entities refs since
Haibo Huangcfd91dc2020-07-30 23:01:33 -070030 * parent is the entity declaration, not the ref.
Owen Taylor3473f882001-02-23 17:55:21 +000031 */
32
Owen Taylor3473f882001-02-23 17:55:21 +000033#include <string.h>
34#include <libxml/xpointer.h>
35#include <libxml/xmlmemory.h>
36#include <libxml/parserInternals.h>
37#include <libxml/uri.h>
38#include <libxml/xpath.h>
39#include <libxml/xpathInternals.h>
Owen Taylor3473f882001-02-23 17:55:21 +000040#include <libxml/xmlerror.h>
Daniel Veillard3c01b1d2001-10-17 15:58:35 +000041#include <libxml/globals.h>
Owen Taylor3473f882001-02-23 17:55:21 +000042
43#ifdef LIBXML_XPTR_ENABLED
44
45/* Add support of the xmlns() xpointer scheme to initialize the namespaces */
46#define XPTR_XMLNS_SCHEME
47
48/* #define DEBUG_RANGES */
Daniel Veillard017b1082001-06-21 11:20:21 +000049#ifdef DEBUG_RANGES
50#ifdef LIBXML_DEBUG_ENABLED
51#include <libxml/debugXML.h>
52#endif
53#endif
Owen Taylor3473f882001-02-23 17:55:21 +000054
Daniel Veillardf8e3db02012-09-11 13:26:36 +080055#define TODO \
Owen Taylor3473f882001-02-23 17:55:21 +000056 xmlGenericError(xmlGenericErrorContext, \
57 "Unimplemented block at %s:%d\n", \
58 __FILE__, __LINE__);
59
Daniel Veillardf8e3db02012-09-11 13:26:36 +080060#define STRANGE \
Owen Taylor3473f882001-02-23 17:55:21 +000061 xmlGenericError(xmlGenericErrorContext, \
62 "Internal error at %s:%d\n", \
63 __FILE__, __LINE__);
64
65/************************************************************************
66 * *
Daniel Veillardf8e3db02012-09-11 13:26:36 +080067 * Some factorized error routines *
Daniel Veillardfcf719c2003-10-10 11:42:17 +000068 * *
69 ************************************************************************/
70
71/**
72 * xmlXPtrErrMemory:
Haibo Huangcfd91dc2020-07-30 23:01:33 -070073 * @extra: extra information
Daniel Veillardfcf719c2003-10-10 11:42:17 +000074 *
75 * Handle a redefinition of attribute error
76 */
77static void
78xmlXPtrErrMemory(const char *extra)
79{
Daniel Veillard659e71e2003-10-10 14:10:40 +000080 __xmlRaiseError(NULL, NULL, NULL, NULL, NULL, XML_FROM_XPOINTER,
Daniel Veillardfcf719c2003-10-10 11:42:17 +000081 XML_ERR_NO_MEMORY, XML_ERR_ERROR, NULL, 0, extra,
82 NULL, NULL, 0, 0,
83 "Memory allocation failed : %s\n", extra);
84}
85
86/**
87 * xmlXPtrErr:
88 * @ctxt: an XPTR evaluation context
Haibo Huangcfd91dc2020-07-30 23:01:33 -070089 * @extra: extra information
Daniel Veillardfcf719c2003-10-10 11:42:17 +000090 *
91 * Handle a redefinition of attribute error
92 */
David Kilzer4472c3a2016-05-13 15:13:17 +080093static void LIBXML_ATTR_FORMAT(3,0)
Daniel Veillardfcf719c2003-10-10 11:42:17 +000094xmlXPtrErr(xmlXPathParserContextPtr ctxt, int error,
95 const char * msg, const xmlChar *extra)
96{
97 if (ctxt != NULL)
98 ctxt->error = error;
99 if ((ctxt == NULL) || (ctxt->context == NULL)) {
Daniel Veillard659e71e2003-10-10 14:10:40 +0000100 __xmlRaiseError(NULL, NULL, NULL,
Daniel Veillardfcf719c2003-10-10 11:42:17 +0000101 NULL, NULL, XML_FROM_XPOINTER, error,
102 XML_ERR_ERROR, NULL, 0,
103 (const char *) extra, NULL, NULL, 0, 0,
104 msg, extra);
105 return;
106 }
Nick Wellnhoferbd1571c2017-05-22 00:33:12 +0200107
108 /* cleanup current last error */
109 xmlResetError(&ctxt->context->lastError);
110
Daniel Veillardfcf719c2003-10-10 11:42:17 +0000111 ctxt->context->lastError.domain = XML_FROM_XPOINTER;
112 ctxt->context->lastError.code = error;
113 ctxt->context->lastError.level = XML_ERR_ERROR;
114 ctxt->context->lastError.str1 = (char *) xmlStrdup(ctxt->base);
115 ctxt->context->lastError.int1 = ctxt->cur - ctxt->base;
116 ctxt->context->lastError.node = ctxt->context->debugNode;
117 if (ctxt->context->error != NULL) {
118 ctxt->context->error(ctxt->context->userData,
119 &ctxt->context->lastError);
120 } else {
Daniel Veillard659e71e2003-10-10 14:10:40 +0000121 __xmlRaiseError(NULL, NULL, NULL,
Daniel Veillardfcf719c2003-10-10 11:42:17 +0000122 NULL, ctxt->context->debugNode, XML_FROM_XPOINTER,
123 error, XML_ERR_ERROR, NULL, 0,
124 (const char *) extra, (const char *) ctxt->base, NULL,
125 ctxt->cur - ctxt->base, 0,
126 msg, extra);
127 }
128}
129
130/************************************************************************
131 * *
Owen Taylor3473f882001-02-23 17:55:21 +0000132 * A few helper functions for child sequences *
133 * *
134 ************************************************************************/
William M. Brackf7eb7942003-12-31 07:59:17 +0000135/* xmlXPtrAdvanceNode is a private function, but used by xinclude.c */
136xmlNodePtr xmlXPtrAdvanceNode(xmlNodePtr cur, int *level);
Owen Taylor3473f882001-02-23 17:55:21 +0000137/**
138 * xmlXPtrGetArity:
139 * @cur: the node
140 *
141 * Returns the number of child for an element, -1 in case of error
142 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000143static int
Owen Taylor3473f882001-02-23 17:55:21 +0000144xmlXPtrGetArity(xmlNodePtr cur) {
145 int i;
Daniel Veillard3e62adb2012-08-09 14:24:02 +0800146 if ((cur == NULL) || (cur->type == XML_NAMESPACE_DECL))
Owen Taylor3473f882001-02-23 17:55:21 +0000147 return(-1);
148 cur = cur->children;
149 for (i = 0;cur != NULL;cur = cur->next) {
150 if ((cur->type == XML_ELEMENT_NODE) ||
151 (cur->type == XML_DOCUMENT_NODE) ||
152 (cur->type == XML_HTML_DOCUMENT_NODE)) {
153 i++;
154 }
155 }
156 return(i);
157}
158
159/**
160 * xmlXPtrGetIndex:
161 * @cur: the node
162 *
163 * Returns the index of the node in its parent children list, -1
164 * in case of error
165 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000166static int
Owen Taylor3473f882001-02-23 17:55:21 +0000167xmlXPtrGetIndex(xmlNodePtr cur) {
168 int i;
Daniel Veillard3e62adb2012-08-09 14:24:02 +0800169 if ((cur == NULL) || (cur->type == XML_NAMESPACE_DECL))
Owen Taylor3473f882001-02-23 17:55:21 +0000170 return(-1);
171 for (i = 1;cur != NULL;cur = cur->prev) {
172 if ((cur->type == XML_ELEMENT_NODE) ||
173 (cur->type == XML_DOCUMENT_NODE) ||
174 (cur->type == XML_HTML_DOCUMENT_NODE)) {
175 i++;
176 }
177 }
178 return(i);
179}
180
181/**
182 * xmlXPtrGetNthChild:
183 * @cur: the node
184 * @no: the child number
185 *
186 * Returns the @no'th element child of @cur or NULL
187 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000188static xmlNodePtr
Owen Taylor3473f882001-02-23 17:55:21 +0000189xmlXPtrGetNthChild(xmlNodePtr cur, int no) {
190 int i;
Daniel Veillard3e62adb2012-08-09 14:24:02 +0800191 if ((cur == NULL) || (cur->type == XML_NAMESPACE_DECL))
Owen Taylor3473f882001-02-23 17:55:21 +0000192 return(cur);
193 cur = cur->children;
194 for (i = 0;i <= no;cur = cur->next) {
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800195 if (cur == NULL)
Owen Taylor3473f882001-02-23 17:55:21 +0000196 return(cur);
197 if ((cur->type == XML_ELEMENT_NODE) ||
198 (cur->type == XML_DOCUMENT_NODE) ||
199 (cur->type == XML_HTML_DOCUMENT_NODE)) {
200 i++;
201 if (i == no)
202 break;
203 }
204 }
205 return(cur);
206}
207
208/************************************************************************
209 * *
210 * Handling of XPointer specific types *
211 * *
212 ************************************************************************/
213
214/**
215 * xmlXPtrCmpPoints:
216 * @node1: the first node
217 * @index1: the first index
218 * @node2: the second node
219 * @index2: the second index
220 *
221 * Compare two points w.r.t document order
222 *
223 * Returns -2 in case of error 1 if first point < second point, 0 if
224 * that's the same point, -1 otherwise
225 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000226static int
Owen Taylor3473f882001-02-23 17:55:21 +0000227xmlXPtrCmpPoints(xmlNodePtr node1, int index1, xmlNodePtr node2, int index2) {
228 if ((node1 == NULL) || (node2 == NULL))
229 return(-2);
230 /*
231 * a couple of optimizations which will avoid computations in most cases
232 */
233 if (node1 == node2) {
234 if (index1 < index2)
235 return(1);
236 if (index1 > index2)
237 return(-1);
238 return(0);
239 }
240 return(xmlXPathCmpNodes(node1, node2));
241}
242
243/**
244 * xmlXPtrNewPoint:
245 * @node: the xmlNodePtr
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000246 * @indx: the indx within the node
Owen Taylor3473f882001-02-23 17:55:21 +0000247 *
248 * Create a new xmlXPathObjectPtr of type point
249 *
250 * Returns the newly created object.
251 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000252static xmlXPathObjectPtr
253xmlXPtrNewPoint(xmlNodePtr node, int indx) {
Owen Taylor3473f882001-02-23 17:55:21 +0000254 xmlXPathObjectPtr ret;
255
256 if (node == NULL)
257 return(NULL);
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000258 if (indx < 0)
Owen Taylor3473f882001-02-23 17:55:21 +0000259 return(NULL);
260
261 ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject));
262 if (ret == NULL) {
Daniel Veillardfcf719c2003-10-10 11:42:17 +0000263 xmlXPtrErrMemory("allocating point");
Owen Taylor3473f882001-02-23 17:55:21 +0000264 return(NULL);
265 }
266 memset(ret, 0 , (size_t) sizeof(xmlXPathObject));
267 ret->type = XPATH_POINT;
268 ret->user = (void *) node;
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000269 ret->index = indx;
Owen Taylor3473f882001-02-23 17:55:21 +0000270 return(ret);
271}
272
273/**
274 * xmlXPtrRangeCheckOrder:
275 * @range: an object range
276 *
277 * Make sure the points in the range are in the right order
278 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000279static void
Owen Taylor3473f882001-02-23 17:55:21 +0000280xmlXPtrRangeCheckOrder(xmlXPathObjectPtr range) {
281 int tmp;
282 xmlNodePtr tmp2;
283 if (range == NULL)
284 return;
285 if (range->type != XPATH_RANGE)
286 return;
287 if (range->user2 == NULL)
288 return;
289 tmp = xmlXPtrCmpPoints(range->user, range->index,
290 range->user2, range->index2);
291 if (tmp == -1) {
292 tmp2 = range->user;
293 range->user = range->user2;
294 range->user2 = tmp2;
295 tmp = range->index;
296 range->index = range->index2;
297 range->index2 = tmp;
298 }
299}
300
301/**
302 * xmlXPtrRangesEqual:
303 * @range1: the first range
304 * @range2: the second range
305 *
306 * Compare two ranges
307 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000308 * Returns 1 if equal, 0 otherwise
Owen Taylor3473f882001-02-23 17:55:21 +0000309 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000310static int
Owen Taylor3473f882001-02-23 17:55:21 +0000311xmlXPtrRangesEqual(xmlXPathObjectPtr range1, xmlXPathObjectPtr range2) {
312 if (range1 == range2)
313 return(1);
314 if ((range1 == NULL) || (range2 == NULL))
315 return(0);
316 if (range1->type != range2->type)
317 return(0);
318 if (range1->type != XPATH_RANGE)
319 return(0);
320 if (range1->user != range2->user)
321 return(0);
322 if (range1->index != range2->index)
323 return(0);
324 if (range1->user2 != range2->user2)
325 return(0);
326 if (range1->index2 != range2->index2)
327 return(0);
328 return(1);
329}
330
331/**
Nick Wellnhoferc1d1f712016-06-28 18:34:52 +0200332 * xmlXPtrNewRangeInternal:
333 * @start: the starting node
334 * @startindex: the start index
335 * @end: the ending point
336 * @endindex: the ending index
337 *
338 * Internal function to create a new xmlXPathObjectPtr of type range
339 *
340 * Returns the newly created object.
341 */
342static xmlXPathObjectPtr
343xmlXPtrNewRangeInternal(xmlNodePtr start, int startindex,
344 xmlNodePtr end, int endindex) {
345 xmlXPathObjectPtr ret;
346
347 /*
348 * Namespace nodes must be copied (see xmlXPathNodeSetDupNs).
349 * Disallow them for now.
350 */
351 if ((start != NULL) && (start->type == XML_NAMESPACE_DECL))
352 return(NULL);
353 if ((end != NULL) && (end->type == XML_NAMESPACE_DECL))
354 return(NULL);
355
356 ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject));
357 if (ret == NULL) {
358 xmlXPtrErrMemory("allocating range");
359 return(NULL);
360 }
361 memset(ret, 0, sizeof(xmlXPathObject));
362 ret->type = XPATH_RANGE;
363 ret->user = start;
364 ret->index = startindex;
365 ret->user2 = end;
366 ret->index2 = endindex;
367 return(ret);
368}
369
370/**
Owen Taylor3473f882001-02-23 17:55:21 +0000371 * xmlXPtrNewRange:
372 * @start: the starting node
373 * @startindex: the start index
374 * @end: the ending point
375 * @endindex: the ending index
376 *
377 * Create a new xmlXPathObjectPtr of type range
378 *
379 * Returns the newly created object.
380 */
381xmlXPathObjectPtr
382xmlXPtrNewRange(xmlNodePtr start, int startindex,
383 xmlNodePtr end, int endindex) {
384 xmlXPathObjectPtr ret;
385
386 if (start == NULL)
387 return(NULL);
388 if (end == NULL)
389 return(NULL);
390 if (startindex < 0)
391 return(NULL);
392 if (endindex < 0)
393 return(NULL);
394
Nick Wellnhoferc1d1f712016-06-28 18:34:52 +0200395 ret = xmlXPtrNewRangeInternal(start, startindex, end, endindex);
Owen Taylor3473f882001-02-23 17:55:21 +0000396 xmlXPtrRangeCheckOrder(ret);
397 return(ret);
398}
399
400/**
401 * xmlXPtrNewRangePoints:
402 * @start: the starting point
403 * @end: the ending point
404 *
405 * Create a new xmlXPathObjectPtr of type range using 2 Points
406 *
407 * Returns the newly created object.
408 */
409xmlXPathObjectPtr
410xmlXPtrNewRangePoints(xmlXPathObjectPtr start, xmlXPathObjectPtr end) {
411 xmlXPathObjectPtr ret;
412
413 if (start == NULL)
414 return(NULL);
415 if (end == NULL)
416 return(NULL);
417 if (start->type != XPATH_POINT)
418 return(NULL);
419 if (end->type != XPATH_POINT)
420 return(NULL);
421
Nick Wellnhoferc1d1f712016-06-28 18:34:52 +0200422 ret = xmlXPtrNewRangeInternal(start->user, start->index, end->user,
423 end->index);
Owen Taylor3473f882001-02-23 17:55:21 +0000424 xmlXPtrRangeCheckOrder(ret);
425 return(ret);
426}
427
428/**
429 * xmlXPtrNewRangePointNode:
430 * @start: the starting point
431 * @end: the ending node
432 *
433 * Create a new xmlXPathObjectPtr of type range from a point to a node
434 *
435 * Returns the newly created object.
436 */
437xmlXPathObjectPtr
438xmlXPtrNewRangePointNode(xmlXPathObjectPtr start, xmlNodePtr end) {
439 xmlXPathObjectPtr ret;
440
441 if (start == NULL)
442 return(NULL);
443 if (end == NULL)
444 return(NULL);
445 if (start->type != XPATH_POINT)
446 return(NULL);
447
Nick Wellnhoferc1d1f712016-06-28 18:34:52 +0200448 ret = xmlXPtrNewRangeInternal(start->user, start->index, end, -1);
Owen Taylor3473f882001-02-23 17:55:21 +0000449 xmlXPtrRangeCheckOrder(ret);
450 return(ret);
451}
452
453/**
454 * xmlXPtrNewRangeNodePoint:
455 * @start: the starting node
456 * @end: the ending point
457 *
458 * Create a new xmlXPathObjectPtr of type range from a node to a point
459 *
460 * Returns the newly created object.
461 */
462xmlXPathObjectPtr
463xmlXPtrNewRangeNodePoint(xmlNodePtr start, xmlXPathObjectPtr end) {
464 xmlXPathObjectPtr ret;
465
466 if (start == NULL)
467 return(NULL);
468 if (end == NULL)
469 return(NULL);
Owen Taylor3473f882001-02-23 17:55:21 +0000470 if (end->type != XPATH_POINT)
471 return(NULL);
472
Nick Wellnhoferc1d1f712016-06-28 18:34:52 +0200473 ret = xmlXPtrNewRangeInternal(start, -1, end->user, end->index);
Owen Taylor3473f882001-02-23 17:55:21 +0000474 xmlXPtrRangeCheckOrder(ret);
475 return(ret);
476}
477
478/**
479 * xmlXPtrNewRangeNodes:
480 * @start: the starting node
481 * @end: the ending node
482 *
483 * Create a new xmlXPathObjectPtr of type range using 2 nodes
484 *
485 * Returns the newly created object.
486 */
487xmlXPathObjectPtr
488xmlXPtrNewRangeNodes(xmlNodePtr start, xmlNodePtr end) {
489 xmlXPathObjectPtr ret;
490
491 if (start == NULL)
492 return(NULL);
493 if (end == NULL)
494 return(NULL);
495
Nick Wellnhoferc1d1f712016-06-28 18:34:52 +0200496 ret = xmlXPtrNewRangeInternal(start, -1, end, -1);
Owen Taylor3473f882001-02-23 17:55:21 +0000497 xmlXPtrRangeCheckOrder(ret);
498 return(ret);
499}
500
501/**
502 * xmlXPtrNewCollapsedRange:
503 * @start: the starting and ending node
504 *
505 * Create a new xmlXPathObjectPtr of type range using a single nodes
506 *
507 * Returns the newly created object.
508 */
509xmlXPathObjectPtr
510xmlXPtrNewCollapsedRange(xmlNodePtr start) {
511 xmlXPathObjectPtr ret;
512
513 if (start == NULL)
514 return(NULL);
515
Nick Wellnhoferc1d1f712016-06-28 18:34:52 +0200516 ret = xmlXPtrNewRangeInternal(start, -1, NULL, -1);
Owen Taylor3473f882001-02-23 17:55:21 +0000517 return(ret);
518}
519
520/**
521 * xmlXPtrNewRangeNodeObject:
522 * @start: the starting node
523 * @end: the ending object
524 *
525 * Create a new xmlXPathObjectPtr of type range from a not to an object
526 *
527 * Returns the newly created object.
528 */
529xmlXPathObjectPtr
530xmlXPtrNewRangeNodeObject(xmlNodePtr start, xmlXPathObjectPtr end) {
Nick Wellnhoferc1d1f712016-06-28 18:34:52 +0200531 xmlNodePtr endNode;
532 int endIndex;
Owen Taylor3473f882001-02-23 17:55:21 +0000533 xmlXPathObjectPtr ret;
534
535 if (start == NULL)
536 return(NULL);
537 if (end == NULL)
538 return(NULL);
539 switch (end->type) {
540 case XPATH_POINT:
Nick Wellnhoferc1d1f712016-06-28 18:34:52 +0200541 endNode = end->user;
542 endIndex = end->index;
543 break;
William M. Brack72ee48d2003-12-30 08:30:19 +0000544 case XPATH_RANGE:
Nick Wellnhoferc1d1f712016-06-28 18:34:52 +0200545 endNode = end->user2;
546 endIndex = end->index2;
Owen Taylor3473f882001-02-23 17:55:21 +0000547 break;
548 case XPATH_NODESET:
549 /*
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800550 * Empty set ...
Owen Taylor3473f882001-02-23 17:55:21 +0000551 */
Nick Wellnhofere905f082016-06-26 12:38:28 +0200552 if ((end->nodesetval == NULL) || (end->nodesetval->nodeNr <= 0))
Owen Taylor3473f882001-02-23 17:55:21 +0000553 return(NULL);
Nick Wellnhoferc1d1f712016-06-28 18:34:52 +0200554 endNode = end->nodesetval->nodeTab[end->nodesetval->nodeNr - 1];
555 endIndex = -1;
Owen Taylor3473f882001-02-23 17:55:21 +0000556 break;
557 default:
Daniel Veillard3d97e662004-11-04 10:49:00 +0000558 /* TODO */
Owen Taylor3473f882001-02-23 17:55:21 +0000559 return(NULL);
560 }
561
Nick Wellnhoferc1d1f712016-06-28 18:34:52 +0200562 ret = xmlXPtrNewRangeInternal(start, -1, endNode, endIndex);
Owen Taylor3473f882001-02-23 17:55:21 +0000563 xmlXPtrRangeCheckOrder(ret);
564 return(ret);
565}
566
567#define XML_RANGESET_DEFAULT 10
568
569/**
570 * xmlXPtrLocationSetCreate:
571 * @val: an initial xmlXPathObjectPtr, or NULL
572 *
573 * Create a new xmlLocationSetPtr of type double and of value @val
574 *
575 * Returns the newly created object.
576 */
577xmlLocationSetPtr
578xmlXPtrLocationSetCreate(xmlXPathObjectPtr val) {
579 xmlLocationSetPtr ret;
580
581 ret = (xmlLocationSetPtr) xmlMalloc(sizeof(xmlLocationSet));
582 if (ret == NULL) {
Daniel Veillardfcf719c2003-10-10 11:42:17 +0000583 xmlXPtrErrMemory("allocating locationset");
Owen Taylor3473f882001-02-23 17:55:21 +0000584 return(NULL);
585 }
586 memset(ret, 0 , (size_t) sizeof(xmlLocationSet));
587 if (val != NULL) {
588 ret->locTab = (xmlXPathObjectPtr *) xmlMalloc(XML_RANGESET_DEFAULT *
589 sizeof(xmlXPathObjectPtr));
590 if (ret->locTab == NULL) {
Daniel Veillardfcf719c2003-10-10 11:42:17 +0000591 xmlXPtrErrMemory("allocating locationset");
592 xmlFree(ret);
Owen Taylor3473f882001-02-23 17:55:21 +0000593 return(NULL);
594 }
595 memset(ret->locTab, 0 ,
596 XML_RANGESET_DEFAULT * (size_t) sizeof(xmlXPathObjectPtr));
597 ret->locMax = XML_RANGESET_DEFAULT;
598 ret->locTab[ret->locNr++] = val;
599 }
600 return(ret);
601}
602
603/**
604 * xmlXPtrLocationSetAdd:
605 * @cur: the initial range set
606 * @val: a new xmlXPathObjectPtr
607 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000608 * add a new xmlXPathObjectPtr to an existing LocationSet
Owen Taylor3473f882001-02-23 17:55:21 +0000609 * If the location already exist in the set @val is freed.
610 */
611void
612xmlXPtrLocationSetAdd(xmlLocationSetPtr cur, xmlXPathObjectPtr val) {
613 int i;
614
Daniel Veillardce682bc2004-11-05 17:22:25 +0000615 if ((cur == NULL) || (val == NULL)) return;
Owen Taylor3473f882001-02-23 17:55:21 +0000616
617 /*
618 * check against doublons
619 */
620 for (i = 0;i < cur->locNr;i++) {
621 if (xmlXPtrRangesEqual(cur->locTab[i], val)) {
622 xmlXPathFreeObject(val);
623 return;
624 }
625 }
626
627 /*
628 * grow the locTab if needed
629 */
630 if (cur->locMax == 0) {
631 cur->locTab = (xmlXPathObjectPtr *) xmlMalloc(XML_RANGESET_DEFAULT *
632 sizeof(xmlXPathObjectPtr));
633 if (cur->locTab == NULL) {
Daniel Veillardfcf719c2003-10-10 11:42:17 +0000634 xmlXPtrErrMemory("adding location to set");
Owen Taylor3473f882001-02-23 17:55:21 +0000635 return;
636 }
637 memset(cur->locTab, 0 ,
638 XML_RANGESET_DEFAULT * (size_t) sizeof(xmlXPathObjectPtr));
639 cur->locMax = XML_RANGESET_DEFAULT;
640 } else if (cur->locNr == cur->locMax) {
641 xmlXPathObjectPtr *temp;
642
643 cur->locMax *= 2;
644 temp = (xmlXPathObjectPtr *) xmlRealloc(cur->locTab, cur->locMax *
645 sizeof(xmlXPathObjectPtr));
646 if (temp == NULL) {
Daniel Veillardfcf719c2003-10-10 11:42:17 +0000647 xmlXPtrErrMemory("adding location to set");
Owen Taylor3473f882001-02-23 17:55:21 +0000648 return;
649 }
650 cur->locTab = temp;
651 }
652 cur->locTab[cur->locNr++] = val;
653}
654
655/**
656 * xmlXPtrLocationSetMerge:
657 * @val1: the first LocationSet
658 * @val2: the second LocationSet
659 *
660 * Merges two rangesets, all ranges from @val2 are added to @val1
661 *
662 * Returns val1 once extended or NULL in case of error.
663 */
664xmlLocationSetPtr
665xmlXPtrLocationSetMerge(xmlLocationSetPtr val1, xmlLocationSetPtr val2) {
666 int i;
667
668 if (val1 == NULL) return(NULL);
669 if (val2 == NULL) return(val1);
670
671 /*
672 * !!!!! this can be optimized a lot, knowing that both
673 * val1 and val2 already have unicity of their values.
674 */
675
676 for (i = 0;i < val2->locNr;i++)
677 xmlXPtrLocationSetAdd(val1, val2->locTab[i]);
678
679 return(val1);
680}
681
682/**
683 * xmlXPtrLocationSetDel:
684 * @cur: the initial range set
685 * @val: an xmlXPathObjectPtr
686 *
687 * Removes an xmlXPathObjectPtr from an existing LocationSet
688 */
689void
690xmlXPtrLocationSetDel(xmlLocationSetPtr cur, xmlXPathObjectPtr val) {
691 int i;
692
693 if (cur == NULL) return;
694 if (val == NULL) return;
695
696 /*
697 * check against doublons
698 */
699 for (i = 0;i < cur->locNr;i++)
700 if (cur->locTab[i] == val) break;
701
702 if (i >= cur->locNr) {
703#ifdef DEBUG
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800704 xmlGenericError(xmlGenericErrorContext,
Daniel Veillard913d6e02001-11-28 14:53:53 +0000705 "xmlXPtrLocationSetDel: Range wasn't found in RangeList\n");
Owen Taylor3473f882001-02-23 17:55:21 +0000706#endif
707 return;
708 }
709 cur->locNr--;
710 for (;i < cur->locNr;i++)
711 cur->locTab[i] = cur->locTab[i + 1];
712 cur->locTab[cur->locNr] = NULL;
713}
714
715/**
716 * xmlXPtrLocationSetRemove:
717 * @cur: the initial range set
718 * @val: the index to remove
719 *
720 * Removes an entry from an existing LocationSet list.
721 */
722void
723xmlXPtrLocationSetRemove(xmlLocationSetPtr cur, int val) {
724 if (cur == NULL) return;
725 if (val >= cur->locNr) return;
726 cur->locNr--;
727 for (;val < cur->locNr;val++)
728 cur->locTab[val] = cur->locTab[val + 1];
729 cur->locTab[cur->locNr] = NULL;
730}
731
732/**
733 * xmlXPtrFreeLocationSet:
734 * @obj: the xmlLocationSetPtr to free
735 *
736 * Free the LocationSet compound (not the actual ranges !).
737 */
738void
739xmlXPtrFreeLocationSet(xmlLocationSetPtr obj) {
740 int i;
741
742 if (obj == NULL) return;
743 if (obj->locTab != NULL) {
744 for (i = 0;i < obj->locNr; i++) {
745 xmlXPathFreeObject(obj->locTab[i]);
746 }
Owen Taylor3473f882001-02-23 17:55:21 +0000747 xmlFree(obj->locTab);
748 }
Owen Taylor3473f882001-02-23 17:55:21 +0000749 xmlFree(obj);
750}
751
752/**
753 * xmlXPtrNewLocationSetNodes:
754 * @start: the start NodePtr value
755 * @end: the end NodePtr value or NULL
756 *
757 * Create a new xmlXPathObjectPtr of type LocationSet and initialize
758 * it with the single range made of the two nodes @start and @end
759 *
760 * Returns the newly created object.
761 */
762xmlXPathObjectPtr
763xmlXPtrNewLocationSetNodes(xmlNodePtr start, xmlNodePtr end) {
764 xmlXPathObjectPtr ret;
765
766 ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject));
767 if (ret == NULL) {
Daniel Veillardfcf719c2003-10-10 11:42:17 +0000768 xmlXPtrErrMemory("allocating locationset");
Owen Taylor3473f882001-02-23 17:55:21 +0000769 return(NULL);
770 }
771 memset(ret, 0 , (size_t) sizeof(xmlXPathObject));
772 ret->type = XPATH_LOCATIONSET;
773 if (end == NULL)
774 ret->user = xmlXPtrLocationSetCreate(xmlXPtrNewCollapsedRange(start));
775 else
776 ret->user = xmlXPtrLocationSetCreate(xmlXPtrNewRangeNodes(start,end));
777 return(ret);
778}
779
780/**
781 * xmlXPtrNewLocationSetNodeSet:
782 * @set: a node set
783 *
784 * Create a new xmlXPathObjectPtr of type LocationSet and initialize
785 * it with all the nodes from @set
786 *
787 * Returns the newly created object.
788 */
789xmlXPathObjectPtr
790xmlXPtrNewLocationSetNodeSet(xmlNodeSetPtr set) {
791 xmlXPathObjectPtr ret;
792
793 ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject));
794 if (ret == NULL) {
Daniel Veillardfcf719c2003-10-10 11:42:17 +0000795 xmlXPtrErrMemory("allocating locationset");
Owen Taylor3473f882001-02-23 17:55:21 +0000796 return(NULL);
797 }
798 memset(ret, 0 , (size_t) sizeof(xmlXPathObject));
799 ret->type = XPATH_LOCATIONSET;
800 if (set != NULL) {
801 int i;
802 xmlLocationSetPtr newset;
803
804 newset = xmlXPtrLocationSetCreate(NULL);
805 if (newset == NULL)
806 return(ret);
807
808 for (i = 0;i < set->nodeNr;i++)
809 xmlXPtrLocationSetAdd(newset,
810 xmlXPtrNewCollapsedRange(set->nodeTab[i]));
811
812 ret->user = (void *) newset;
813 }
814 return(ret);
815}
816
817/**
818 * xmlXPtrWrapLocationSet:
819 * @val: the LocationSet value
820 *
821 * Wrap the LocationSet @val in a new xmlXPathObjectPtr
822 *
823 * Returns the newly created object.
824 */
825xmlXPathObjectPtr
826xmlXPtrWrapLocationSet(xmlLocationSetPtr val) {
827 xmlXPathObjectPtr ret;
828
829 ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject));
830 if (ret == NULL) {
Daniel Veillardfcf719c2003-10-10 11:42:17 +0000831 xmlXPtrErrMemory("allocating locationset");
Owen Taylor3473f882001-02-23 17:55:21 +0000832 return(NULL);
833 }
834 memset(ret, 0 , (size_t) sizeof(xmlXPathObject));
835 ret->type = XPATH_LOCATIONSET;
836 ret->user = (void *) val;
837 return(ret);
838}
839
840/************************************************************************
841 * *
842 * The parser *
843 * *
844 ************************************************************************/
845
Daniel Veillard9a237c92003-02-13 15:52:58 +0000846static void xmlXPtrEvalChildSeq(xmlXPathParserContextPtr ctxt, xmlChar *name);
847
Owen Taylor3473f882001-02-23 17:55:21 +0000848/*
849 * Macros for accessing the content. Those should be used only by the parser,
850 * and not exported.
851 *
852 * Dirty macros, i.e. one need to make assumption on the context to use them
853 *
854 * CUR_PTR return the current pointer to the xmlChar to be parsed.
855 * CUR returns the current xmlChar value, i.e. a 8 bit value
856 * in ISO-Latin or UTF-8.
857 * This should be used internally by the parser
858 * only to compare to ASCII values otherwise it would break when
859 * running with UTF-8 encoding.
860 * NXT(n) returns the n'th next xmlChar. Same as CUR is should be used only
861 * to compare on ASCII based substring.
862 * SKIP(n) Skip n xmlChar, and must also be used only to skip ASCII defined
863 * strings within the parser.
864 * CURRENT Returns the current char value, with the full decoding of
865 * UTF-8 if we are using this mode. It returns an int.
866 * NEXT Skip to the next character, this does the proper decoding
867 * in UTF-8 mode. It also pop-up unfinished entities on the fly.
868 * It returns the pointer to the current xmlChar.
869 */
870
871#define CUR (*ctxt->cur)
872#define SKIP(val) ctxt->cur += (val)
873#define NXT(val) ctxt->cur[(val)]
874#define CUR_PTR ctxt->cur
875
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800876#define SKIP_BLANKS \
William M. Brack272693c2003-11-14 16:20:34 +0000877 while (IS_BLANK_CH(*(ctxt->cur))) NEXT
Owen Taylor3473f882001-02-23 17:55:21 +0000878
879#define CURRENT (*ctxt->cur)
880#define NEXT ((*ctxt->cur) ? ctxt->cur++: ctxt->cur)
881
882/*
883 * xmlXPtrGetChildNo:
884 * @ctxt: the XPointer Parser context
885 * @index: the child number
886 *
887 * Move the current node of the nodeset on the stack to the
888 * given child if found
889 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000890static void
891xmlXPtrGetChildNo(xmlXPathParserContextPtr ctxt, int indx) {
Owen Taylor3473f882001-02-23 17:55:21 +0000892 xmlNodePtr cur = NULL;
893 xmlXPathObjectPtr obj;
894 xmlNodeSetPtr oldset;
895
896 CHECK_TYPE(XPATH_NODESET);
897 obj = valuePop(ctxt);
898 oldset = obj->nodesetval;
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000899 if ((indx <= 0) || (oldset == NULL) || (oldset->nodeNr != 1)) {
Owen Taylor3473f882001-02-23 17:55:21 +0000900 xmlXPathFreeObject(obj);
901 valuePush(ctxt, xmlXPathNewNodeSet(NULL));
902 return;
903 }
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000904 cur = xmlXPtrGetNthChild(oldset->nodeTab[0], indx);
Owen Taylor3473f882001-02-23 17:55:21 +0000905 if (cur == NULL) {
906 xmlXPathFreeObject(obj);
907 valuePush(ctxt, xmlXPathNewNodeSet(NULL));
908 return;
909 }
910 oldset->nodeTab[0] = cur;
911 valuePush(ctxt, obj);
912}
913
914/**
915 * xmlXPtrEvalXPtrPart:
916 * @ctxt: the XPointer Parser context
917 * @name: the preparsed Scheme for the XPtrPart
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800918 *
Owen Taylor3473f882001-02-23 17:55:21 +0000919 * XPtrPart ::= 'xpointer' '(' XPtrExpr ')'
920 * | Scheme '(' SchemeSpecificExpr ')'
921 *
922 * Scheme ::= NCName - 'xpointer' [VC: Non-XPointer schemes]
923 *
924 * SchemeSpecificExpr ::= StringWithBalancedParens
925 *
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800926 * StringWithBalancedParens ::=
Owen Taylor3473f882001-02-23 17:55:21 +0000927 * [^()]* ('(' StringWithBalancedParens ')' [^()]*)*
928 * [VC: Parenthesis escaping]
929 *
930 * XPtrExpr ::= Expr [VC: Parenthesis escaping]
931 *
932 * VC: Parenthesis escaping:
933 * The end of an XPointer part is signaled by the right parenthesis ")"
934 * character that is balanced with the left parenthesis "(" character
935 * that began the part. Any unbalanced parenthesis character inside the
936 * expression, even within literals, must be escaped with a circumflex (^)
937 * character preceding it. If the expression contains any literal
938 * occurrences of the circumflex, each must be escaped with an additional
939 * circumflex (that is, ^^). If the unescaped parentheses in the expression
940 * are not balanced, a syntax error results.
941 *
942 * Parse and evaluate an XPtrPart. Basically it generates the unescaped
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000943 * string and if the scheme is 'xpointer' it will call the XPath interpreter.
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800944 *
Owen Taylor3473f882001-02-23 17:55:21 +0000945 * TODO: there is no new scheme registration mechanism
946 */
947
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000948static void
Owen Taylor3473f882001-02-23 17:55:21 +0000949xmlXPtrEvalXPtrPart(xmlXPathParserContextPtr ctxt, xmlChar *name) {
950 xmlChar *buffer, *cur;
951 int len;
952 int level;
953
954 if (name == NULL)
955 name = xmlXPathParseName(ctxt);
956 if (name == NULL)
957 XP_ERROR(XPATH_EXPR_ERROR);
958
Nick Wellnhoferbd1571c2017-05-22 00:33:12 +0200959 if (CUR != '(') {
960 xmlFree(name);
Owen Taylor3473f882001-02-23 17:55:21 +0000961 XP_ERROR(XPATH_EXPR_ERROR);
Nick Wellnhoferbd1571c2017-05-22 00:33:12 +0200962 }
Owen Taylor3473f882001-02-23 17:55:21 +0000963 NEXT;
964 level = 1;
965
966 len = xmlStrlen(ctxt->cur);
967 len++;
Daniel Veillard3c908dc2003-04-19 00:07:51 +0000968 buffer = (xmlChar *) xmlMallocAtomic(len * sizeof (xmlChar));
Owen Taylor3473f882001-02-23 17:55:21 +0000969 if (buffer == NULL) {
Daniel Veillardfcf719c2003-10-10 11:42:17 +0000970 xmlXPtrErrMemory("allocating buffer");
Nick Wellnhoferbd1571c2017-05-22 00:33:12 +0200971 xmlFree(name);
Owen Taylor3473f882001-02-23 17:55:21 +0000972 return;
973 }
974
975 cur = buffer;
976 while (CUR != 0) {
977 if (CUR == ')') {
978 level--;
979 if (level == 0) {
980 NEXT;
981 break;
982 }
Owen Taylor3473f882001-02-23 17:55:21 +0000983 } else if (CUR == '(') {
984 level++;
Owen Taylor3473f882001-02-23 17:55:21 +0000985 } else if (CUR == '^') {
Jüri Aedlad8e1fae2012-05-07 15:06:56 +0800986 if ((NXT(1) == ')') || (NXT(1) == '(') || (NXT(1) == '^')) {
987 NEXT;
988 }
Owen Taylor3473f882001-02-23 17:55:21 +0000989 }
Jüri Aedlad8e1fae2012-05-07 15:06:56 +0800990 *cur++ = CUR;
Owen Taylor3473f882001-02-23 17:55:21 +0000991 NEXT;
992 }
993 *cur = 0;
994
995 if ((level != 0) && (CUR == 0)) {
Nick Wellnhoferbd1571c2017-05-22 00:33:12 +0200996 xmlFree(name);
Owen Taylor3473f882001-02-23 17:55:21 +0000997 xmlFree(buffer);
998 XP_ERROR(XPTR_SYNTAX_ERROR);
999 }
1000
1001 if (xmlStrEqual(name, (xmlChar *) "xpointer")) {
1002 const xmlChar *left = CUR_PTR;
1003
1004 CUR_PTR = buffer;
William M. Brack72ee48d2003-12-30 08:30:19 +00001005 /*
1006 * To evaluate an xpointer scheme element (4.3) we need:
1007 * context initialized to the root
Haibo Huangcfd91dc2020-07-30 23:01:33 -07001008 * context position initialized to 1
William M. Brack72ee48d2003-12-30 08:30:19 +00001009 * context size initialized to 1
1010 */
1011 ctxt->context->node = (xmlNodePtr)ctxt->context->doc;
1012 ctxt->context->proximityPosition = 1;
1013 ctxt->context->contextSize = 1;
Owen Taylor3473f882001-02-23 17:55:21 +00001014 xmlXPathEvalExpr(ctxt);
1015 CUR_PTR=left;
Daniel Veillard9a237c92003-02-13 15:52:58 +00001016 } else if (xmlStrEqual(name, (xmlChar *) "element")) {
1017 const xmlChar *left = CUR_PTR;
1018 xmlChar *name2;
1019
1020 CUR_PTR = buffer;
1021 if (buffer[0] == '/') {
1022 xmlXPathRoot(ctxt);
1023 xmlXPtrEvalChildSeq(ctxt, NULL);
1024 } else {
1025 name2 = xmlXPathParseName(ctxt);
1026 if (name2 == NULL) {
1027 CUR_PTR = left;
1028 xmlFree(buffer);
Nick Wellnhoferbd1571c2017-05-22 00:33:12 +02001029 xmlFree(name);
Daniel Veillard9a237c92003-02-13 15:52:58 +00001030 XP_ERROR(XPATH_EXPR_ERROR);
1031 }
1032 xmlXPtrEvalChildSeq(ctxt, name2);
1033 }
1034 CUR_PTR = left;
Owen Taylor3473f882001-02-23 17:55:21 +00001035#ifdef XPTR_XMLNS_SCHEME
1036 } else if (xmlStrEqual(name, (xmlChar *) "xmlns")) {
1037 const xmlChar *left = CUR_PTR;
1038 xmlChar *prefix;
1039 xmlChar *URI;
1040 xmlURIPtr value;
1041
1042 CUR_PTR = buffer;
1043 prefix = xmlXPathParseNCName(ctxt);
1044 if (prefix == NULL) {
1045 xmlFree(buffer);
1046 xmlFree(name);
1047 XP_ERROR(XPTR_SYNTAX_ERROR);
1048 }
1049 SKIP_BLANKS;
1050 if (CUR != '=') {
1051 xmlFree(prefix);
1052 xmlFree(buffer);
1053 xmlFree(name);
1054 XP_ERROR(XPTR_SYNTAX_ERROR);
1055 }
1056 NEXT;
1057 SKIP_BLANKS;
1058 /* @@ check escaping in the XPointer WD */
1059
1060 value = xmlParseURI((const char *)ctxt->cur);
1061 if (value == NULL) {
1062 xmlFree(prefix);
1063 xmlFree(buffer);
1064 xmlFree(name);
1065 XP_ERROR(XPTR_SYNTAX_ERROR);
1066 }
1067 URI = xmlSaveUri(value);
1068 xmlFreeURI(value);
1069 if (URI == NULL) {
1070 xmlFree(prefix);
1071 xmlFree(buffer);
1072 xmlFree(name);
1073 XP_ERROR(XPATH_MEMORY_ERROR);
1074 }
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001075
Owen Taylor3473f882001-02-23 17:55:21 +00001076 xmlXPathRegisterNs(ctxt->context, prefix, URI);
1077 CUR_PTR = left;
Daniel Veillard56f21f22002-11-06 15:49:46 +00001078 xmlFree(URI);
1079 xmlFree(prefix);
Owen Taylor3473f882001-02-23 17:55:21 +00001080#endif /* XPTR_XMLNS_SCHEME */
1081 } else {
Daniel Veillardfcf719c2003-10-10 11:42:17 +00001082 xmlXPtrErr(ctxt, XML_XPTR_UNKNOWN_SCHEME,
1083 "unsupported scheme '%s'\n", name);
Owen Taylor3473f882001-02-23 17:55:21 +00001084 }
1085 xmlFree(buffer);
1086 xmlFree(name);
1087}
1088
1089/**
1090 * xmlXPtrEvalFullXPtr:
1091 * @ctxt: the XPointer Parser context
1092 * @name: the preparsed Scheme for the first XPtrPart
1093 *
1094 * FullXPtr ::= XPtrPart (S? XPtrPart)*
1095 *
1096 * As the specs says:
1097 * -----------
1098 * When multiple XPtrParts are provided, they must be evaluated in
1099 * left-to-right order. If evaluation of one part fails, the nexti
1100 * is evaluated. The following conditions cause XPointer part failure:
1101 *
1102 * - An unknown scheme
1103 * - A scheme that does not locate any sub-resource present in the resource
1104 * - A scheme that is not applicable to the media type of the resource
1105 *
1106 * The XPointer application must consume a failed XPointer part and
1107 * attempt to evaluate the next one, if any. The result of the first
1108 * XPointer part whose evaluation succeeds is taken to be the fragment
1109 * located by the XPointer as a whole. If all the parts fail, the result
1110 * for the XPointer as a whole is a sub-resource error.
1111 * -----------
1112 *
1113 * Parse and evaluate a Full XPtr i.e. possibly a cascade of XPath based
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001114 * expressions or other schemes.
Owen Taylor3473f882001-02-23 17:55:21 +00001115 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001116static void
Owen Taylor3473f882001-02-23 17:55:21 +00001117xmlXPtrEvalFullXPtr(xmlXPathParserContextPtr ctxt, xmlChar *name) {
1118 if (name == NULL)
1119 name = xmlXPathParseName(ctxt);
1120 if (name == NULL)
1121 XP_ERROR(XPATH_EXPR_ERROR);
1122 while (name != NULL) {
Jakub Wilk928d7032009-07-29 12:24:11 +02001123 ctxt->error = XPATH_EXPRESSION_OK;
Owen Taylor3473f882001-02-23 17:55:21 +00001124 xmlXPtrEvalXPtrPart(ctxt, name);
1125
1126 /* in case of syntax error, break here */
Jakub Wilk928d7032009-07-29 12:24:11 +02001127 if ((ctxt->error != XPATH_EXPRESSION_OK) &&
1128 (ctxt->error != XML_XPTR_UNKNOWN_SCHEME))
Owen Taylor3473f882001-02-23 17:55:21 +00001129 return;
1130
1131 /*
1132 * If the returned value is a non-empty nodeset
1133 * or location set, return here.
1134 */
1135 if (ctxt->value != NULL) {
1136 xmlXPathObjectPtr obj = ctxt->value;
1137
1138 switch (obj->type) {
1139 case XPATH_LOCATIONSET: {
1140 xmlLocationSetPtr loc = ctxt->value->user;
1141 if ((loc != NULL) && (loc->locNr > 0))
1142 return;
1143 break;
1144 }
1145 case XPATH_NODESET: {
1146 xmlNodeSetPtr loc = ctxt->value->nodesetval;
1147 if ((loc != NULL) && (loc->nodeNr > 0))
1148 return;
1149 break;
1150 }
1151 default:
1152 break;
1153 }
1154
1155 /*
1156 * Evaluating to improper values is equivalent to
1157 * a sub-resource error, clean-up the stack
1158 */
1159 do {
1160 obj = valuePop(ctxt);
1161 if (obj != NULL) {
1162 xmlXPathFreeObject(obj);
1163 }
1164 } while (obj != NULL);
1165 }
1166
1167 /*
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001168 * Is there another XPointer part.
Owen Taylor3473f882001-02-23 17:55:21 +00001169 */
1170 SKIP_BLANKS;
1171 name = xmlXPathParseName(ctxt);
1172 }
1173}
1174
1175/**
1176 * xmlXPtrEvalChildSeq:
1177 * @ctxt: the XPointer Parser context
1178 * @name: a possible ID name of the child sequence
1179 *
1180 * ChildSeq ::= '/1' ('/' [0-9]*)*
1181 * | Name ('/' [0-9]*)+
1182 *
1183 * Parse and evaluate a Child Sequence. This routine also handle the
1184 * case of a Bare Name used to get a document ID.
1185 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001186static void
Owen Taylor3473f882001-02-23 17:55:21 +00001187xmlXPtrEvalChildSeq(xmlXPathParserContextPtr ctxt, xmlChar *name) {
1188 /*
Haibo Huangcfd91dc2020-07-30 23:01:33 -07001189 * XPointer don't allow by syntax to address in multirooted trees
Owen Taylor3473f882001-02-23 17:55:21 +00001190 * this might prove useful in some cases, warn about it.
1191 */
1192 if ((name == NULL) && (CUR == '/') && (NXT(1) != '1')) {
Daniel Veillardfcf719c2003-10-10 11:42:17 +00001193 xmlXPtrErr(ctxt, XML_XPTR_CHILDSEQ_START,
1194 "warning: ChildSeq not starting by /1\n", NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001195 }
1196
1197 if (name != NULL) {
1198 valuePush(ctxt, xmlXPathNewString(name));
1199 xmlFree(name);
1200 xmlXPathIdFunction(ctxt, 1);
1201 CHECK_ERROR;
1202 }
1203
1204 while (CUR == '/') {
Haibo Huangcfd91dc2020-07-30 23:01:33 -07001205 int child = 0, overflow = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00001206 NEXT;
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001207
Owen Taylor3473f882001-02-23 17:55:21 +00001208 while ((CUR >= '0') && (CUR <= '9')) {
Haibo Huangcfd91dc2020-07-30 23:01:33 -07001209 int d = CUR - '0';
1210 if (child > INT_MAX / 10)
1211 overflow = 1;
1212 else
1213 child *= 10;
1214 if (child > INT_MAX - d)
1215 overflow = 1;
1216 else
1217 child += d;
Owen Taylor3473f882001-02-23 17:55:21 +00001218 NEXT;
1219 }
Haibo Huangcfd91dc2020-07-30 23:01:33 -07001220 if (overflow)
1221 child = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00001222 xmlXPtrGetChildNo(ctxt, child);
1223 }
1224}
1225
1226
1227/**
1228 * xmlXPtrEvalXPointer:
1229 * @ctxt: the XPointer Parser context
1230 *
1231 * XPointer ::= Name
1232 * | ChildSeq
1233 * | FullXPtr
1234 *
1235 * Parse and evaluate an XPointer
1236 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001237static void
Owen Taylor3473f882001-02-23 17:55:21 +00001238xmlXPtrEvalXPointer(xmlXPathParserContextPtr ctxt) {
Daniel Veillard9e7160d2001-03-18 23:17:47 +00001239 if (ctxt->valueTab == NULL) {
1240 /* Allocate the value stack */
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001241 ctxt->valueTab = (xmlXPathObjectPtr *)
Daniel Veillard9e7160d2001-03-18 23:17:47 +00001242 xmlMalloc(10 * sizeof(xmlXPathObjectPtr));
1243 if (ctxt->valueTab == NULL) {
Daniel Veillardfcf719c2003-10-10 11:42:17 +00001244 xmlXPtrErrMemory("allocating evaluation context");
Daniel Veillard9e7160d2001-03-18 23:17:47 +00001245 return;
1246 }
1247 ctxt->valueNr = 0;
1248 ctxt->valueMax = 10;
1249 ctxt->value = NULL;
Daniel Veillardf5048b32011-08-18 17:10:13 +08001250 ctxt->valueFrame = 0;
Daniel Veillard9e7160d2001-03-18 23:17:47 +00001251 }
Owen Taylor3473f882001-02-23 17:55:21 +00001252 SKIP_BLANKS;
1253 if (CUR == '/') {
1254 xmlXPathRoot(ctxt);
1255 xmlXPtrEvalChildSeq(ctxt, NULL);
1256 } else {
1257 xmlChar *name;
1258
1259 name = xmlXPathParseName(ctxt);
1260 if (name == NULL)
1261 XP_ERROR(XPATH_EXPR_ERROR);
1262 if (CUR == '(') {
1263 xmlXPtrEvalFullXPtr(ctxt, name);
1264 /* Short evaluation */
1265 return;
1266 } else {
1267 /* this handle both Bare Names and Child Sequences */
1268 xmlXPtrEvalChildSeq(ctxt, name);
1269 }
1270 }
1271 SKIP_BLANKS;
1272 if (CUR != 0)
1273 XP_ERROR(XPATH_EXPR_ERROR);
1274}
1275
1276
1277/************************************************************************
1278 * *
1279 * General routines *
1280 * *
1281 ************************************************************************/
1282
Daniel Veillard8ed10722009-08-20 19:17:36 +02001283static
Owen Taylor3473f882001-02-23 17:55:21 +00001284void xmlXPtrStringRangeFunction(xmlXPathParserContextPtr ctxt, int nargs);
Daniel Veillard8ed10722009-08-20 19:17:36 +02001285static
Owen Taylor3473f882001-02-23 17:55:21 +00001286void xmlXPtrStartPointFunction(xmlXPathParserContextPtr ctxt, int nargs);
Daniel Veillard8ed10722009-08-20 19:17:36 +02001287static
Owen Taylor3473f882001-02-23 17:55:21 +00001288void xmlXPtrEndPointFunction(xmlXPathParserContextPtr ctxt, int nargs);
Daniel Veillard8ed10722009-08-20 19:17:36 +02001289static
Owen Taylor3473f882001-02-23 17:55:21 +00001290void xmlXPtrHereFunction(xmlXPathParserContextPtr ctxt, int nargs);
Daniel Veillard8ed10722009-08-20 19:17:36 +02001291static
Owen Taylor3473f882001-02-23 17:55:21 +00001292void xmlXPtrOriginFunction(xmlXPathParserContextPtr ctxt, int nargs);
Daniel Veillard8ed10722009-08-20 19:17:36 +02001293static
Owen Taylor3473f882001-02-23 17:55:21 +00001294void xmlXPtrRangeInsideFunction(xmlXPathParserContextPtr ctxt, int nargs);
Daniel Veillard8ed10722009-08-20 19:17:36 +02001295static
Owen Taylor3473f882001-02-23 17:55:21 +00001296void xmlXPtrRangeFunction(xmlXPathParserContextPtr ctxt, int nargs);
1297
1298/**
1299 * xmlXPtrNewContext:
1300 * @doc: the XML document
1301 * @here: the node that directly contains the XPointer being evaluated or NULL
1302 * @origin: the element from which a user or program initiated traversal of
1303 * the link, or NULL.
1304 *
1305 * Create a new XPointer context
1306 *
1307 * Returns the xmlXPathContext just allocated.
1308 */
1309xmlXPathContextPtr
1310xmlXPtrNewContext(xmlDocPtr doc, xmlNodePtr here, xmlNodePtr origin) {
1311 xmlXPathContextPtr ret;
1312
1313 ret = xmlXPathNewContext(doc);
1314 if (ret == NULL)
1315 return(ret);
1316 ret->xptr = 1;
1317 ret->here = here;
1318 ret->origin = origin;
1319
Owen Taylor3473f882001-02-23 17:55:21 +00001320 xmlXPathRegisterFunc(ret, (xmlChar *)"range",
1321 xmlXPtrRangeFunction);
1322 xmlXPathRegisterFunc(ret, (xmlChar *)"range-inside",
1323 xmlXPtrRangeInsideFunction);
1324 xmlXPathRegisterFunc(ret, (xmlChar *)"string-range",
1325 xmlXPtrStringRangeFunction);
1326 xmlXPathRegisterFunc(ret, (xmlChar *)"start-point",
1327 xmlXPtrStartPointFunction);
1328 xmlXPathRegisterFunc(ret, (xmlChar *)"end-point",
1329 xmlXPtrEndPointFunction);
1330 xmlXPathRegisterFunc(ret, (xmlChar *)"here",
1331 xmlXPtrHereFunction);
1332 xmlXPathRegisterFunc(ret, (xmlChar *)" origin",
1333 xmlXPtrOriginFunction);
1334
1335 return(ret);
1336}
1337
1338/**
1339 * xmlXPtrEval:
1340 * @str: the XPointer expression
1341 * @ctx: the XPointer context
1342 *
1343 * Evaluate the XPath Location Path in the given context.
1344 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001345 * Returns the xmlXPathObjectPtr resulting from the evaluation or NULL.
Owen Taylor3473f882001-02-23 17:55:21 +00001346 * the caller has to free the object.
1347 */
1348xmlXPathObjectPtr
1349xmlXPtrEval(const xmlChar *str, xmlXPathContextPtr ctx) {
1350 xmlXPathParserContextPtr ctxt;
1351 xmlXPathObjectPtr res = NULL, tmp;
1352 xmlXPathObjectPtr init = NULL;
1353 int stack = 0;
1354
Haibo Huangd23e46c2020-10-28 22:26:09 -07001355 xmlInitParser();
Owen Taylor3473f882001-02-23 17:55:21 +00001356
1357 if ((ctx == NULL) || (str == NULL))
1358 return(NULL);
1359
1360 ctxt = xmlXPathNewParserContext(str, ctx);
Gaurav Guptaf5496a12014-10-07 17:09:35 +08001361 if (ctxt == NULL)
1362 return(NULL);
Daniel Veillardfbf8a2d2001-03-19 15:58:54 +00001363 ctxt->xptr = 1;
Owen Taylor3473f882001-02-23 17:55:21 +00001364 xmlXPtrEvalXPointer(ctxt);
1365
1366 if ((ctxt->value != NULL) &&
1367 (ctxt->value->type != XPATH_NODESET) &&
1368 (ctxt->value->type != XPATH_LOCATIONSET)) {
Daniel Veillardfcf719c2003-10-10 11:42:17 +00001369 xmlXPtrErr(ctxt, XML_XPTR_EVAL_FAILED,
1370 "xmlXPtrEval: evaluation failed to return a node set\n",
1371 NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001372 } else {
1373 res = valuePop(ctxt);
1374 }
1375
1376 do {
1377 tmp = valuePop(ctxt);
1378 if (tmp != NULL) {
1379 if (tmp != init) {
1380 if (tmp->type == XPATH_NODESET) {
1381 /*
1382 * Evaluation may push a root nodeset which is unused
1383 */
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001384 xmlNodeSetPtr set;
Owen Taylor3473f882001-02-23 17:55:21 +00001385 set = tmp->nodesetval;
Nick Wellnhofere905f082016-06-26 12:38:28 +02001386 if ((set == NULL) || (set->nodeNr != 1) ||
Owen Taylor3473f882001-02-23 17:55:21 +00001387 (set->nodeTab[0] != (xmlNodePtr) ctx->doc))
1388 stack++;
1389 } else
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001390 stack++;
Owen Taylor3473f882001-02-23 17:55:21 +00001391 }
1392 xmlXPathFreeObject(tmp);
1393 }
1394 } while (tmp != NULL);
1395 if (stack != 0) {
Daniel Veillardfcf719c2003-10-10 11:42:17 +00001396 xmlXPtrErr(ctxt, XML_XPTR_EXTRA_OBJECTS,
1397 "xmlXPtrEval: object(s) left on the eval stack\n",
1398 NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001399 }
1400 if (ctxt->error != XPATH_EXPRESSION_OK) {
1401 xmlXPathFreeObject(res);
1402 res = NULL;
1403 }
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001404
Owen Taylor3473f882001-02-23 17:55:21 +00001405 xmlXPathFreeParserContext(ctxt);
1406 return(res);
1407}
1408
1409/**
1410 * xmlXPtrBuildRangeNodeList:
1411 * @range: a range object
1412 *
1413 * Build a node list tree copy of the range
1414 *
1415 * Returns an xmlNodePtr list or NULL.
1416 * the caller has to free the node tree.
1417 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001418static xmlNodePtr
Owen Taylor3473f882001-02-23 17:55:21 +00001419xmlXPtrBuildRangeNodeList(xmlXPathObjectPtr range) {
1420 /* pointers to generated nodes */
1421 xmlNodePtr list = NULL, last = NULL, parent = NULL, tmp;
1422 /* pointers to traversal nodes */
1423 xmlNodePtr start, cur, end;
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001424 int index1, index2;
Owen Taylor3473f882001-02-23 17:55:21 +00001425
1426 if (range == NULL)
1427 return(NULL);
1428 if (range->type != XPATH_RANGE)
1429 return(NULL);
1430 start = (xmlNodePtr) range->user;
1431
Daniel Veillard3e62adb2012-08-09 14:24:02 +08001432 if ((start == NULL) || (start->type == XML_NAMESPACE_DECL))
Owen Taylor3473f882001-02-23 17:55:21 +00001433 return(NULL);
1434 end = range->user2;
1435 if (end == NULL)
1436 return(xmlCopyNode(start, 1));
Daniel Veillard3e62adb2012-08-09 14:24:02 +08001437 if (end->type == XML_NAMESPACE_DECL)
1438 return(NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001439
1440 cur = start;
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001441 index1 = range->index;
Owen Taylor3473f882001-02-23 17:55:21 +00001442 index2 = range->index2;
1443 while (cur != NULL) {
1444 if (cur == end) {
1445 if (cur->type == XML_TEXT_NODE) {
1446 const xmlChar *content = cur->content;
1447 int len;
1448
1449 if (content == NULL) {
1450 tmp = xmlNewTextLen(NULL, 0);
1451 } else {
1452 len = index2;
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001453 if ((cur == start) && (index1 > 1)) {
1454 content += (index1 - 1);
1455 len -= (index1 - 1);
1456 index1 = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00001457 } else {
1458 len = index2;
1459 }
1460 tmp = xmlNewTextLen(content, len);
1461 }
1462 /* single sub text node selection */
1463 if (list == NULL)
1464 return(tmp);
1465 /* prune and return full set */
1466 if (last != NULL)
1467 xmlAddNextSibling(last, tmp);
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001468 else
Owen Taylor3473f882001-02-23 17:55:21 +00001469 xmlAddChild(parent, tmp);
1470 return(list);
1471 } else {
1472 tmp = xmlCopyNode(cur, 0);
1473 if (list == NULL)
1474 list = tmp;
1475 else {
1476 if (last != NULL)
1477 xmlAddNextSibling(last, tmp);
1478 else
1479 xmlAddChild(parent, tmp);
1480 }
1481 last = NULL;
1482 parent = tmp;
1483
1484 if (index2 > 1) {
1485 end = xmlXPtrGetNthChild(cur, index2 - 1);
1486 index2 = 0;
1487 }
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001488 if ((cur == start) && (index1 > 1)) {
1489 cur = xmlXPtrGetNthChild(cur, index1 - 1);
1490 index1 = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00001491 } else {
1492 cur = cur->children;
1493 }
1494 /*
1495 * Now gather the remaining nodes from cur to end
1496 */
1497 continue; /* while */
1498 }
1499 } else if ((cur == start) &&
1500 (list == NULL) /* looks superfluous but ... */ ) {
Daniel Veillard7db37732001-07-12 01:20:08 +00001501 if ((cur->type == XML_TEXT_NODE) ||
1502 (cur->type == XML_CDATA_SECTION_NODE)) {
Owen Taylor3473f882001-02-23 17:55:21 +00001503 const xmlChar *content = cur->content;
1504
1505 if (content == NULL) {
1506 tmp = xmlNewTextLen(NULL, 0);
1507 } else {
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001508 if (index1 > 1) {
1509 content += (index1 - 1);
Owen Taylor3473f882001-02-23 17:55:21 +00001510 }
1511 tmp = xmlNewText(content);
1512 }
1513 last = list = tmp;
1514 } else {
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001515 if ((cur == start) && (index1 > 1)) {
Owen Taylor3473f882001-02-23 17:55:21 +00001516 tmp = xmlCopyNode(cur, 0);
1517 list = tmp;
1518 parent = tmp;
1519 last = NULL;
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001520 cur = xmlXPtrGetNthChild(cur, index1 - 1);
1521 index1 = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00001522 /*
1523 * Now gather the remaining nodes from cur to end
1524 */
1525 continue; /* while */
1526 }
1527 tmp = xmlCopyNode(cur, 1);
1528 list = tmp;
1529 parent = NULL;
1530 last = tmp;
1531 }
1532 } else {
1533 tmp = NULL;
1534 switch (cur->type) {
1535 case XML_DTD_NODE:
1536 case XML_ELEMENT_DECL:
1537 case XML_ATTRIBUTE_DECL:
1538 case XML_ENTITY_NODE:
Haibo Huangcfd91dc2020-07-30 23:01:33 -07001539 /* Do not copy DTD information */
Owen Taylor3473f882001-02-23 17:55:21 +00001540 break;
1541 case XML_ENTITY_DECL:
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001542 TODO /* handle crossing entities -> stack needed */
Owen Taylor3473f882001-02-23 17:55:21 +00001543 break;
1544 case XML_XINCLUDE_START:
1545 case XML_XINCLUDE_END:
1546 /* don't consider it part of the tree content */
1547 break;
1548 case XML_ATTRIBUTE_NODE:
1549 /* Humm, should not happen ! */
1550 STRANGE
1551 break;
1552 default:
1553 tmp = xmlCopyNode(cur, 1);
1554 break;
1555 }
1556 if (tmp != NULL) {
1557 if ((list == NULL) || ((last == NULL) && (parent == NULL))) {
1558 STRANGE
1559 return(NULL);
1560 }
1561 if (last != NULL)
1562 xmlAddNextSibling(last, tmp);
1563 else {
1564 xmlAddChild(parent, tmp);
1565 last = tmp;
1566 }
1567 }
1568 }
1569 /*
1570 * Skip to next node in document order
1571 */
1572 if ((list == NULL) || ((last == NULL) && (parent == NULL))) {
1573 STRANGE
1574 return(NULL);
1575 }
William M. Brackf7eb7942003-12-31 07:59:17 +00001576 cur = xmlXPtrAdvanceNode(cur, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001577 }
1578 return(list);
1579}
1580
1581/**
1582 * xmlXPtrBuildNodeList:
1583 * @obj: the XPointer result from the evaluation.
1584 *
1585 * Build a node list tree copy of the XPointer result.
Daniel Veillard39196eb2001-06-19 18:09:42 +00001586 * This will drop Attributes and Namespace declarations.
Owen Taylor3473f882001-02-23 17:55:21 +00001587 *
1588 * Returns an xmlNodePtr list or NULL.
1589 * the caller has to free the node tree.
1590 */
1591xmlNodePtr
1592xmlXPtrBuildNodeList(xmlXPathObjectPtr obj) {
1593 xmlNodePtr list = NULL, last = NULL;
1594 int i;
1595
1596 if (obj == NULL)
1597 return(NULL);
1598 switch (obj->type) {
1599 case XPATH_NODESET: {
1600 xmlNodeSetPtr set = obj->nodesetval;
1601 if (set == NULL)
1602 return(NULL);
1603 for (i = 0;i < set->nodeNr;i++) {
Daniel Veillard39196eb2001-06-19 18:09:42 +00001604 if (set->nodeTab[i] == NULL)
1605 continue;
1606 switch (set->nodeTab[i]->type) {
1607 case XML_TEXT_NODE:
1608 case XML_CDATA_SECTION_NODE:
1609 case XML_ELEMENT_NODE:
1610 case XML_ENTITY_REF_NODE:
1611 case XML_ENTITY_NODE:
1612 case XML_PI_NODE:
1613 case XML_COMMENT_NODE:
1614 case XML_DOCUMENT_NODE:
1615 case XML_HTML_DOCUMENT_NODE:
1616#ifdef LIBXML_DOCB_ENABLED
1617 case XML_DOCB_DOCUMENT_NODE:
1618#endif
1619 case XML_XINCLUDE_START:
1620 case XML_XINCLUDE_END:
1621 break;
1622 case XML_ATTRIBUTE_NODE:
1623 case XML_NAMESPACE_DECL:
1624 case XML_DOCUMENT_TYPE_NODE:
1625 case XML_DOCUMENT_FRAG_NODE:
1626 case XML_NOTATION_NODE:
1627 case XML_DTD_NODE:
1628 case XML_ELEMENT_DECL:
1629 case XML_ATTRIBUTE_DECL:
1630 case XML_ENTITY_DECL:
1631 continue; /* for */
1632 }
Owen Taylor3473f882001-02-23 17:55:21 +00001633 if (last == NULL)
1634 list = last = xmlCopyNode(set->nodeTab[i], 1);
1635 else {
1636 xmlAddNextSibling(last, xmlCopyNode(set->nodeTab[i], 1));
1637 if (last->next != NULL)
1638 last = last->next;
1639 }
1640 }
1641 break;
1642 }
1643 case XPATH_LOCATIONSET: {
1644 xmlLocationSetPtr set = (xmlLocationSetPtr) obj->user;
1645 if (set == NULL)
1646 return(NULL);
1647 for (i = 0;i < set->locNr;i++) {
1648 if (last == NULL)
1649 list = last = xmlXPtrBuildNodeList(set->locTab[i]);
1650 else
1651 xmlAddNextSibling(last,
1652 xmlXPtrBuildNodeList(set->locTab[i]));
1653 if (last != NULL) {
1654 while (last->next != NULL)
1655 last = last->next;
1656 }
1657 }
1658 break;
1659 }
1660 case XPATH_RANGE:
1661 return(xmlXPtrBuildRangeNodeList(obj));
1662 case XPATH_POINT:
1663 return(xmlCopyNode(obj->user, 0));
1664 default:
1665 break;
1666 }
1667 return(list);
1668}
1669
1670/************************************************************************
1671 * *
1672 * XPointer functions *
1673 * *
1674 ************************************************************************/
1675
1676/**
1677 * xmlXPtrNbLocChildren:
1678 * @node: an xmlNodePtr
1679 *
Daniel Veillard60087f32001-10-10 09:45:09 +00001680 * Count the number of location children of @node or the length of the
Owen Taylor3473f882001-02-23 17:55:21 +00001681 * string value in case of text/PI/Comments nodes
1682 *
1683 * Returns the number of location children
1684 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001685static int
Owen Taylor3473f882001-02-23 17:55:21 +00001686xmlXPtrNbLocChildren(xmlNodePtr node) {
1687 int ret = 0;
1688 if (node == NULL)
1689 return(-1);
1690 switch (node->type) {
1691 case XML_HTML_DOCUMENT_NODE:
1692 case XML_DOCUMENT_NODE:
1693 case XML_ELEMENT_NODE:
1694 node = node->children;
1695 while (node != NULL) {
1696 if (node->type == XML_ELEMENT_NODE)
1697 ret++;
1698 node = node->next;
1699 }
1700 break;
1701 case XML_ATTRIBUTE_NODE:
1702 return(-1);
1703
1704 case XML_PI_NODE:
1705 case XML_COMMENT_NODE:
1706 case XML_TEXT_NODE:
1707 case XML_CDATA_SECTION_NODE:
1708 case XML_ENTITY_REF_NODE:
Owen Taylor3473f882001-02-23 17:55:21 +00001709 ret = xmlStrlen(node->content);
Owen Taylor3473f882001-02-23 17:55:21 +00001710 break;
1711 default:
1712 return(-1);
1713 }
1714 return(ret);
1715}
1716
1717/**
1718 * xmlXPtrHereFunction:
1719 * @ctxt: the XPointer Parser context
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001720 * @nargs: the number of args
Owen Taylor3473f882001-02-23 17:55:21 +00001721 *
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001722 * Function implementing here() operation
Owen Taylor3473f882001-02-23 17:55:21 +00001723 * as described in 5.4.3
1724 */
Daniel Veillard8ed10722009-08-20 19:17:36 +02001725static void
Owen Taylor3473f882001-02-23 17:55:21 +00001726xmlXPtrHereFunction(xmlXPathParserContextPtr ctxt, int nargs) {
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001727 CHECK_ARITY(0);
1728
Owen Taylor3473f882001-02-23 17:55:21 +00001729 if (ctxt->context->here == NULL)
1730 XP_ERROR(XPTR_SYNTAX_ERROR);
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001731
Owen Taylor3473f882001-02-23 17:55:21 +00001732 valuePush(ctxt, xmlXPtrNewLocationSetNodes(ctxt->context->here, NULL));
1733}
1734
1735/**
1736 * xmlXPtrOriginFunction:
1737 * @ctxt: the XPointer Parser context
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001738 * @nargs: the number of args
Owen Taylor3473f882001-02-23 17:55:21 +00001739 *
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001740 * Function implementing origin() operation
Owen Taylor3473f882001-02-23 17:55:21 +00001741 * as described in 5.4.3
1742 */
Daniel Veillard8ed10722009-08-20 19:17:36 +02001743static void
Owen Taylor3473f882001-02-23 17:55:21 +00001744xmlXPtrOriginFunction(xmlXPathParserContextPtr ctxt, int nargs) {
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001745 CHECK_ARITY(0);
1746
Owen Taylor3473f882001-02-23 17:55:21 +00001747 if (ctxt->context->origin == NULL)
1748 XP_ERROR(XPTR_SYNTAX_ERROR);
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001749
Owen Taylor3473f882001-02-23 17:55:21 +00001750 valuePush(ctxt, xmlXPtrNewLocationSetNodes(ctxt->context->origin, NULL));
1751}
1752
1753/**
1754 * xmlXPtrStartPointFunction:
1755 * @ctxt: the XPointer Parser context
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001756 * @nargs: the number of args
Owen Taylor3473f882001-02-23 17:55:21 +00001757 *
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001758 * Function implementing start-point() operation
Owen Taylor3473f882001-02-23 17:55:21 +00001759 * as described in 5.4.3
1760 * ----------------
1761 * location-set start-point(location-set)
1762 *
1763 * For each location x in the argument location-set, start-point adds a
1764 * location of type point to the result location-set. That point represents
1765 * the start point of location x and is determined by the following rules:
1766 *
1767 * - If x is of type point, the start point is x.
1768 * - If x is of type range, the start point is the start point of x.
1769 * - If x is of type root, element, text, comment, or processing instruction,
1770 * - the container node of the start point is x and the index is 0.
1771 * - If x is of type attribute or namespace, the function must signal a
1772 * syntax error.
1773 * ----------------
1774 *
1775 */
Daniel Veillard8ed10722009-08-20 19:17:36 +02001776static void
Owen Taylor3473f882001-02-23 17:55:21 +00001777xmlXPtrStartPointFunction(xmlXPathParserContextPtr ctxt, int nargs) {
1778 xmlXPathObjectPtr tmp, obj, point;
1779 xmlLocationSetPtr newset = NULL;
1780 xmlLocationSetPtr oldset = NULL;
1781
1782 CHECK_ARITY(1);
1783 if ((ctxt->value == NULL) ||
1784 ((ctxt->value->type != XPATH_LOCATIONSET) &&
1785 (ctxt->value->type != XPATH_NODESET)))
1786 XP_ERROR(XPATH_INVALID_TYPE)
1787
1788 obj = valuePop(ctxt);
1789 if (obj->type == XPATH_NODESET) {
1790 /*
1791 * First convert to a location set
1792 */
1793 tmp = xmlXPtrNewLocationSetNodeSet(obj->nodesetval);
1794 xmlXPathFreeObject(obj);
Gaurav Guptaf5496a12014-10-07 17:09:35 +08001795 if (tmp == NULL)
1796 XP_ERROR(XPATH_MEMORY_ERROR)
Owen Taylor3473f882001-02-23 17:55:21 +00001797 obj = tmp;
1798 }
1799
1800 newset = xmlXPtrLocationSetCreate(NULL);
1801 if (newset == NULL) {
1802 xmlXPathFreeObject(obj);
1803 XP_ERROR(XPATH_MEMORY_ERROR);
1804 }
1805 oldset = (xmlLocationSetPtr) obj->user;
1806 if (oldset != NULL) {
1807 int i;
1808
1809 for (i = 0; i < oldset->locNr; i++) {
1810 tmp = oldset->locTab[i];
1811 if (tmp == NULL)
1812 continue;
1813 point = NULL;
1814 switch (tmp->type) {
1815 case XPATH_POINT:
1816 point = xmlXPtrNewPoint(tmp->user, tmp->index);
1817 break;
1818 case XPATH_RANGE: {
1819 xmlNodePtr node = tmp->user;
1820 if (node != NULL) {
Nick Wellnhofer3f8a9102016-06-28 15:55:09 +02001821 if ((node->type == XML_ATTRIBUTE_NODE) ||
1822 (node->type == XML_NAMESPACE_DECL)) {
Owen Taylor3473f882001-02-23 17:55:21 +00001823 xmlXPathFreeObject(obj);
1824 xmlXPtrFreeLocationSet(newset);
1825 XP_ERROR(XPTR_SYNTAX_ERROR);
1826 }
1827 point = xmlXPtrNewPoint(node, tmp->index);
1828 }
1829 break;
1830 }
1831 default:
1832 /*** Should we raise an error ?
1833 xmlXPathFreeObject(obj);
1834 xmlXPathFreeObject(newset);
1835 XP_ERROR(XPATH_INVALID_TYPE)
1836 ***/
1837 break;
1838 }
1839 if (point != NULL)
1840 xmlXPtrLocationSetAdd(newset, point);
1841 }
1842 }
1843 xmlXPathFreeObject(obj);
1844 valuePush(ctxt, xmlXPtrWrapLocationSet(newset));
1845}
1846
1847/**
1848 * xmlXPtrEndPointFunction:
1849 * @ctxt: the XPointer Parser context
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001850 * @nargs: the number of args
Owen Taylor3473f882001-02-23 17:55:21 +00001851 *
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001852 * Function implementing end-point() operation
Owen Taylor3473f882001-02-23 17:55:21 +00001853 * as described in 5.4.3
1854 * ----------------------------
1855 * location-set end-point(location-set)
1856 *
1857 * For each location x in the argument location-set, end-point adds a
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001858 * location of type point to the result location-set. That point represents
Owen Taylor3473f882001-02-23 17:55:21 +00001859 * the end point of location x and is determined by the following rules:
1860 *
1861 * - If x is of type point, the resulting point is x.
1862 * - If x is of type range, the resulting point is the end point of x.
1863 * - If x is of type root or element, the container node of the resulting
1864 * point is x and the index is the number of location children of x.
1865 * - If x is of type text, comment, or processing instruction, the container
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001866 * node of the resulting point is x and the index is the length of the
Owen Taylor3473f882001-02-23 17:55:21 +00001867 * string-value of x.
1868 * - If x is of type attribute or namespace, the function must signal a
1869 * syntax error.
1870 * ----------------------------
1871 */
Daniel Veillard8ed10722009-08-20 19:17:36 +02001872static void
Owen Taylor3473f882001-02-23 17:55:21 +00001873xmlXPtrEndPointFunction(xmlXPathParserContextPtr ctxt, int nargs) {
1874 xmlXPathObjectPtr tmp, obj, point;
1875 xmlLocationSetPtr newset = NULL;
1876 xmlLocationSetPtr oldset = NULL;
1877
1878 CHECK_ARITY(1);
1879 if ((ctxt->value == NULL) ||
1880 ((ctxt->value->type != XPATH_LOCATIONSET) &&
1881 (ctxt->value->type != XPATH_NODESET)))
1882 XP_ERROR(XPATH_INVALID_TYPE)
1883
1884 obj = valuePop(ctxt);
1885 if (obj->type == XPATH_NODESET) {
1886 /*
1887 * First convert to a location set
1888 */
1889 tmp = xmlXPtrNewLocationSetNodeSet(obj->nodesetval);
1890 xmlXPathFreeObject(obj);
Gaurav Guptaf5496a12014-10-07 17:09:35 +08001891 if (tmp == NULL)
1892 XP_ERROR(XPATH_MEMORY_ERROR)
Owen Taylor3473f882001-02-23 17:55:21 +00001893 obj = tmp;
1894 }
1895
1896 newset = xmlXPtrLocationSetCreate(NULL);
Gaurav Guptaf5496a12014-10-07 17:09:35 +08001897 if (newset == NULL) {
1898 xmlXPathFreeObject(obj);
1899 XP_ERROR(XPATH_MEMORY_ERROR);
1900 }
Owen Taylor3473f882001-02-23 17:55:21 +00001901 oldset = (xmlLocationSetPtr) obj->user;
1902 if (oldset != NULL) {
1903 int i;
1904
1905 for (i = 0; i < oldset->locNr; i++) {
1906 tmp = oldset->locTab[i];
1907 if (tmp == NULL)
1908 continue;
1909 point = NULL;
1910 switch (tmp->type) {
1911 case XPATH_POINT:
1912 point = xmlXPtrNewPoint(tmp->user, tmp->index);
1913 break;
1914 case XPATH_RANGE: {
1915 xmlNodePtr node = tmp->user2;
1916 if (node != NULL) {
Nick Wellnhofer3f8a9102016-06-28 15:55:09 +02001917 if ((node->type == XML_ATTRIBUTE_NODE) ||
1918 (node->type == XML_NAMESPACE_DECL)) {
Owen Taylor3473f882001-02-23 17:55:21 +00001919 xmlXPathFreeObject(obj);
1920 xmlXPtrFreeLocationSet(newset);
1921 XP_ERROR(XPTR_SYNTAX_ERROR);
1922 }
1923 point = xmlXPtrNewPoint(node, tmp->index2);
1924 } else if (tmp->user == NULL) {
1925 point = xmlXPtrNewPoint(node,
1926 xmlXPtrNbLocChildren(node));
1927 }
1928 break;
1929 }
1930 default:
1931 /*** Should we raise an error ?
1932 xmlXPathFreeObject(obj);
1933 xmlXPathFreeObject(newset);
1934 XP_ERROR(XPATH_INVALID_TYPE)
1935 ***/
1936 break;
1937 }
1938 if (point != NULL)
1939 xmlXPtrLocationSetAdd(newset, point);
1940 }
1941 }
1942 xmlXPathFreeObject(obj);
1943 valuePush(ctxt, xmlXPtrWrapLocationSet(newset));
1944}
1945
1946
1947/**
1948 * xmlXPtrCoveringRange:
1949 * @ctxt: the XPointer Parser context
1950 * @loc: the location for which the covering range must be computed
1951 *
1952 * A covering range is a range that wholly encompasses a location
1953 * Section 5.3.3. Covering Ranges for All Location Types
1954 * http://www.w3.org/TR/xptr#N2267
1955 *
1956 * Returns a new location or NULL in case of error
1957 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001958static xmlXPathObjectPtr
Owen Taylor3473f882001-02-23 17:55:21 +00001959xmlXPtrCoveringRange(xmlXPathParserContextPtr ctxt, xmlXPathObjectPtr loc) {
1960 if (loc == NULL)
1961 return(NULL);
1962 if ((ctxt == NULL) || (ctxt->context == NULL) ||
1963 (ctxt->context->doc == NULL))
1964 return(NULL);
1965 switch (loc->type) {
1966 case XPATH_POINT:
1967 return(xmlXPtrNewRange(loc->user, loc->index,
1968 loc->user, loc->index));
1969 case XPATH_RANGE:
1970 if (loc->user2 != NULL) {
1971 return(xmlXPtrNewRange(loc->user, loc->index,
1972 loc->user2, loc->index2));
1973 } else {
1974 xmlNodePtr node = (xmlNodePtr) loc->user;
1975 if (node == (xmlNodePtr) ctxt->context->doc) {
1976 return(xmlXPtrNewRange(node, 0, node,
1977 xmlXPtrGetArity(node)));
1978 } else {
1979 switch (node->type) {
1980 case XML_ATTRIBUTE_NODE:
1981 /* !!! our model is slightly different than XPath */
1982 return(xmlXPtrNewRange(node, 0, node,
1983 xmlXPtrGetArity(node)));
1984 case XML_ELEMENT_NODE:
1985 case XML_TEXT_NODE:
1986 case XML_CDATA_SECTION_NODE:
1987 case XML_ENTITY_REF_NODE:
1988 case XML_PI_NODE:
1989 case XML_COMMENT_NODE:
1990 case XML_DOCUMENT_NODE:
1991 case XML_NOTATION_NODE:
1992 case XML_HTML_DOCUMENT_NODE: {
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001993 int indx = xmlXPtrGetIndex(node);
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001994
Owen Taylor3473f882001-02-23 17:55:21 +00001995 node = node->parent;
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001996 return(xmlXPtrNewRange(node, indx - 1,
1997 node, indx + 1));
Owen Taylor3473f882001-02-23 17:55:21 +00001998 }
1999 default:
2000 return(NULL);
2001 }
2002 }
2003 }
2004 default:
2005 TODO /* missed one case ??? */
2006 }
2007 return(NULL);
2008}
2009
2010/**
2011 * xmlXPtrRangeFunction:
2012 * @ctxt: the XPointer Parser context
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002013 * @nargs: the number of args
Owen Taylor3473f882001-02-23 17:55:21 +00002014 *
2015 * Function implementing the range() function 5.4.3
2016 * location-set range(location-set )
2017 *
2018 * The range function returns ranges covering the locations in
2019 * the argument location-set. For each location x in the argument
2020 * location-set, a range location representing the covering range of
2021 * x is added to the result location-set.
2022 */
Daniel Veillard8ed10722009-08-20 19:17:36 +02002023static void
Owen Taylor3473f882001-02-23 17:55:21 +00002024xmlXPtrRangeFunction(xmlXPathParserContextPtr ctxt, int nargs) {
2025 int i;
2026 xmlXPathObjectPtr set;
2027 xmlLocationSetPtr oldset;
2028 xmlLocationSetPtr newset;
2029
2030 CHECK_ARITY(1);
2031 if ((ctxt->value == NULL) ||
2032 ((ctxt->value->type != XPATH_LOCATIONSET) &&
2033 (ctxt->value->type != XPATH_NODESET)))
2034 XP_ERROR(XPATH_INVALID_TYPE)
2035
2036 set = valuePop(ctxt);
2037 if (set->type == XPATH_NODESET) {
2038 xmlXPathObjectPtr tmp;
2039
2040 /*
2041 * First convert to a location set
2042 */
2043 tmp = xmlXPtrNewLocationSetNodeSet(set->nodesetval);
2044 xmlXPathFreeObject(set);
Gaurav Guptaf5496a12014-10-07 17:09:35 +08002045 if (tmp == NULL)
2046 XP_ERROR(XPATH_MEMORY_ERROR)
Owen Taylor3473f882001-02-23 17:55:21 +00002047 set = tmp;
2048 }
2049 oldset = (xmlLocationSetPtr) set->user;
2050
2051 /*
2052 * The loop is to compute the covering range for each item and add it
2053 */
2054 newset = xmlXPtrLocationSetCreate(NULL);
Gaurav Guptaf5496a12014-10-07 17:09:35 +08002055 if (newset == NULL) {
2056 xmlXPathFreeObject(set);
2057 XP_ERROR(XPATH_MEMORY_ERROR);
2058 }
Nick Wellnhofere905f082016-06-26 12:38:28 +02002059 if (oldset != NULL) {
2060 for (i = 0;i < oldset->locNr;i++) {
2061 xmlXPtrLocationSetAdd(newset,
2062 xmlXPtrCoveringRange(ctxt, oldset->locTab[i]));
2063 }
Owen Taylor3473f882001-02-23 17:55:21 +00002064 }
2065
2066 /*
2067 * Save the new value and cleanup
2068 */
2069 valuePush(ctxt, xmlXPtrWrapLocationSet(newset));
2070 xmlXPathFreeObject(set);
2071}
2072
2073/**
2074 * xmlXPtrInsideRange:
2075 * @ctxt: the XPointer Parser context
2076 * @loc: the location for which the inside range must be computed
2077 *
2078 * A inside range is a range described in the range-inside() description
2079 *
2080 * Returns a new location or NULL in case of error
2081 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002082static xmlXPathObjectPtr
Owen Taylor3473f882001-02-23 17:55:21 +00002083xmlXPtrInsideRange(xmlXPathParserContextPtr ctxt, xmlXPathObjectPtr loc) {
2084 if (loc == NULL)
2085 return(NULL);
2086 if ((ctxt == NULL) || (ctxt->context == NULL) ||
2087 (ctxt->context->doc == NULL))
2088 return(NULL);
2089 switch (loc->type) {
2090 case XPATH_POINT: {
2091 xmlNodePtr node = (xmlNodePtr) loc->user;
2092 switch (node->type) {
2093 case XML_PI_NODE:
2094 case XML_COMMENT_NODE:
2095 case XML_TEXT_NODE:
2096 case XML_CDATA_SECTION_NODE: {
2097 if (node->content == NULL) {
2098 return(xmlXPtrNewRange(node, 0, node, 0));
2099 } else {
Owen Taylor3473f882001-02-23 17:55:21 +00002100 return(xmlXPtrNewRange(node, 0, node,
2101 xmlStrlen(node->content)));
Owen Taylor3473f882001-02-23 17:55:21 +00002102 }
2103 }
2104 case XML_ATTRIBUTE_NODE:
2105 case XML_ELEMENT_NODE:
2106 case XML_ENTITY_REF_NODE:
2107 case XML_DOCUMENT_NODE:
2108 case XML_NOTATION_NODE:
2109 case XML_HTML_DOCUMENT_NODE: {
2110 return(xmlXPtrNewRange(node, 0, node,
2111 xmlXPtrGetArity(node)));
2112 }
2113 default:
Daniel Veillardb44025c2001-10-11 22:55:55 +00002114 break;
Owen Taylor3473f882001-02-23 17:55:21 +00002115 }
2116 return(NULL);
2117 }
2118 case XPATH_RANGE: {
2119 xmlNodePtr node = (xmlNodePtr) loc->user;
2120 if (loc->user2 != NULL) {
2121 return(xmlXPtrNewRange(node, loc->index,
2122 loc->user2, loc->index2));
2123 } else {
2124 switch (node->type) {
2125 case XML_PI_NODE:
2126 case XML_COMMENT_NODE:
2127 case XML_TEXT_NODE:
2128 case XML_CDATA_SECTION_NODE: {
2129 if (node->content == NULL) {
2130 return(xmlXPtrNewRange(node, 0, node, 0));
2131 } else {
Owen Taylor3473f882001-02-23 17:55:21 +00002132 return(xmlXPtrNewRange(node, 0, node,
2133 xmlStrlen(node->content)));
Owen Taylor3473f882001-02-23 17:55:21 +00002134 }
2135 }
2136 case XML_ATTRIBUTE_NODE:
2137 case XML_ELEMENT_NODE:
2138 case XML_ENTITY_REF_NODE:
2139 case XML_DOCUMENT_NODE:
2140 case XML_NOTATION_NODE:
2141 case XML_HTML_DOCUMENT_NODE: {
2142 return(xmlXPtrNewRange(node, 0, node,
2143 xmlXPtrGetArity(node)));
2144 }
2145 default:
Daniel Veillardb44025c2001-10-11 22:55:55 +00002146 break;
Owen Taylor3473f882001-02-23 17:55:21 +00002147 }
2148 return(NULL);
2149 }
2150 }
2151 default:
2152 TODO /* missed one case ??? */
2153 }
2154 return(NULL);
2155}
2156
2157/**
2158 * xmlXPtrRangeInsideFunction:
2159 * @ctxt: the XPointer Parser context
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002160 * @nargs: the number of args
Owen Taylor3473f882001-02-23 17:55:21 +00002161 *
2162 * Function implementing the range-inside() function 5.4.3
2163 * location-set range-inside(location-set )
2164 *
2165 * The range-inside function returns ranges covering the contents of
2166 * the locations in the argument location-set. For each location x in
2167 * the argument location-set, a range location is added to the result
2168 * location-set. If x is a range location, then x is added to the
2169 * result location-set. If x is not a range location, then x is used
2170 * as the container location of the start and end points of the range
2171 * location to be added; the index of the start point of the range is
2172 * zero; if the end point is a character point then its index is the
2173 * length of the string-value of x, and otherwise is the number of
2174 * location children of x.
2175 *
2176 */
Daniel Veillard8ed10722009-08-20 19:17:36 +02002177static void
Owen Taylor3473f882001-02-23 17:55:21 +00002178xmlXPtrRangeInsideFunction(xmlXPathParserContextPtr ctxt, int nargs) {
2179 int i;
2180 xmlXPathObjectPtr set;
2181 xmlLocationSetPtr oldset;
2182 xmlLocationSetPtr newset;
2183
2184 CHECK_ARITY(1);
2185 if ((ctxt->value == NULL) ||
2186 ((ctxt->value->type != XPATH_LOCATIONSET) &&
2187 (ctxt->value->type != XPATH_NODESET)))
2188 XP_ERROR(XPATH_INVALID_TYPE)
2189
2190 set = valuePop(ctxt);
2191 if (set->type == XPATH_NODESET) {
2192 xmlXPathObjectPtr tmp;
2193
2194 /*
2195 * First convert to a location set
2196 */
2197 tmp = xmlXPtrNewLocationSetNodeSet(set->nodesetval);
2198 xmlXPathFreeObject(set);
Gaurav Guptaf5496a12014-10-07 17:09:35 +08002199 if (tmp == NULL)
2200 XP_ERROR(XPATH_MEMORY_ERROR)
Owen Taylor3473f882001-02-23 17:55:21 +00002201 set = tmp;
2202 }
2203 oldset = (xmlLocationSetPtr) set->user;
2204
2205 /*
2206 * The loop is to compute the covering range for each item and add it
2207 */
2208 newset = xmlXPtrLocationSetCreate(NULL);
Gaurav Guptaf5496a12014-10-07 17:09:35 +08002209 if (newset == NULL) {
2210 xmlXPathFreeObject(set);
2211 XP_ERROR(XPATH_MEMORY_ERROR);
2212 }
Owen Taylor3473f882001-02-23 17:55:21 +00002213 for (i = 0;i < oldset->locNr;i++) {
2214 xmlXPtrLocationSetAdd(newset,
2215 xmlXPtrInsideRange(ctxt, oldset->locTab[i]));
2216 }
2217
2218 /*
2219 * Save the new value and cleanup
2220 */
2221 valuePush(ctxt, xmlXPtrWrapLocationSet(newset));
2222 xmlXPathFreeObject(set);
2223}
2224
2225/**
2226 * xmlXPtrRangeToFunction:
2227 * @ctxt: the XPointer Parser context
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002228 * @nargs: the number of args
Owen Taylor3473f882001-02-23 17:55:21 +00002229 *
2230 * Implement the range-to() XPointer function
Nick Wellnhofer9ab01a22016-06-28 14:22:23 +02002231 *
2232 * Obsolete. range-to is not a real function but a special type of location
2233 * step which is handled in xpath.c.
Owen Taylor3473f882001-02-23 17:55:21 +00002234 */
2235void
Nick Wellnhofer9ab01a22016-06-28 14:22:23 +02002236xmlXPtrRangeToFunction(xmlXPathParserContextPtr ctxt,
2237 int nargs ATTRIBUTE_UNUSED) {
2238 XP_ERROR(XPATH_EXPR_ERROR);
Owen Taylor3473f882001-02-23 17:55:21 +00002239}
2240
2241/**
2242 * xmlXPtrAdvanceNode:
2243 * @cur: the node
William M. Brackce4fc562004-01-22 02:47:18 +00002244 * @level: incremented/decremented to show level in tree
Owen Taylor3473f882001-02-23 17:55:21 +00002245 *
2246 * Advance to the next element or text node in document order
Daniel Veillardf8e3db02012-09-11 13:26:36 +08002247 * TODO: add a stack for entering/exiting entities
Owen Taylor3473f882001-02-23 17:55:21 +00002248 *
2249 * Returns -1 in case of failure, 0 otherwise
2250 */
2251xmlNodePtr
William M. Brackf7eb7942003-12-31 07:59:17 +00002252xmlXPtrAdvanceNode(xmlNodePtr cur, int *level) {
Owen Taylor3473f882001-02-23 17:55:21 +00002253next:
Daniel Veillard3e62adb2012-08-09 14:24:02 +08002254 if ((cur == NULL) || (cur->type == XML_NAMESPACE_DECL))
Owen Taylor3473f882001-02-23 17:55:21 +00002255 return(NULL);
2256 if (cur->children != NULL) {
2257 cur = cur->children ;
William M. Brackf7eb7942003-12-31 07:59:17 +00002258 if (level != NULL)
2259 (*level)++;
Owen Taylor3473f882001-02-23 17:55:21 +00002260 goto found;
2261 }
William M. Brack4d59e222004-03-08 14:42:31 +00002262skip: /* This label should only be needed if something is wrong! */
Owen Taylor3473f882001-02-23 17:55:21 +00002263 if (cur->next != NULL) {
2264 cur = cur->next;
2265 goto found;
2266 }
2267 do {
2268 cur = cur->parent;
William M. Brackf7eb7942003-12-31 07:59:17 +00002269 if (level != NULL)
2270 (*level)--;
Owen Taylor3473f882001-02-23 17:55:21 +00002271 if (cur == NULL) return(NULL);
2272 if (cur->next != NULL) {
2273 cur = cur->next;
2274 goto found;
2275 }
2276 } while (cur != NULL);
2277
2278found:
2279 if ((cur->type != XML_ELEMENT_NODE) &&
2280 (cur->type != XML_TEXT_NODE) &&
2281 (cur->type != XML_DOCUMENT_NODE) &&
2282 (cur->type != XML_HTML_DOCUMENT_NODE) &&
William M. Brack4d59e222004-03-08 14:42:31 +00002283 (cur->type != XML_CDATA_SECTION_NODE)) {
2284 if (cur->type == XML_ENTITY_REF_NODE) { /* Shouldn't happen */
2285 TODO
2286 goto skip;
2287 }
2288 goto next;
2289 }
Owen Taylor3473f882001-02-23 17:55:21 +00002290 return(cur);
2291}
2292
2293/**
2294 * xmlXPtrAdvanceChar:
2295 * @node: the node
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002296 * @indx: the indx
Owen Taylor3473f882001-02-23 17:55:21 +00002297 * @bytes: the number of bytes
2298 *
2299 * Advance a point of the associated number of bytes (not UTF8 chars)
2300 *
2301 * Returns -1 in case of failure, 0 otherwise
2302 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002303static int
2304xmlXPtrAdvanceChar(xmlNodePtr *node, int *indx, int bytes) {
Owen Taylor3473f882001-02-23 17:55:21 +00002305 xmlNodePtr cur;
2306 int pos;
2307 int len;
2308
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002309 if ((node == NULL) || (indx == NULL))
Owen Taylor3473f882001-02-23 17:55:21 +00002310 return(-1);
2311 cur = *node;
Daniel Veillard3e62adb2012-08-09 14:24:02 +08002312 if ((cur == NULL) || (cur->type == XML_NAMESPACE_DECL))
Owen Taylor3473f882001-02-23 17:55:21 +00002313 return(-1);
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002314 pos = *indx;
Owen Taylor3473f882001-02-23 17:55:21 +00002315
2316 while (bytes >= 0) {
2317 /*
2318 * First position to the beginning of the first text node
2319 * corresponding to this point
2320 */
2321 while ((cur != NULL) &&
2322 ((cur->type == XML_ELEMENT_NODE) ||
2323 (cur->type == XML_DOCUMENT_NODE) ||
2324 (cur->type == XML_HTML_DOCUMENT_NODE))) {
2325 if (pos > 0) {
2326 cur = xmlXPtrGetNthChild(cur, pos);
2327 pos = 0;
2328 } else {
William M. Brackf7eb7942003-12-31 07:59:17 +00002329 cur = xmlXPtrAdvanceNode(cur, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00002330 pos = 0;
2331 }
2332 }
2333
2334 if (cur == NULL) {
2335 *node = NULL;
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002336 *indx = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00002337 return(-1);
2338 }
2339
2340 /*
2341 * if there is no move needed return the current value.
2342 */
2343 if (pos == 0) pos = 1;
2344 if (bytes == 0) {
2345 *node = cur;
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002346 *indx = pos;
Owen Taylor3473f882001-02-23 17:55:21 +00002347 return(0);
2348 }
2349 /*
Daniel Veillardf8e3db02012-09-11 13:26:36 +08002350 * We should have a text (or cdata) node ...
Owen Taylor3473f882001-02-23 17:55:21 +00002351 */
2352 len = 0;
Daniel Veillard7db37732001-07-12 01:20:08 +00002353 if ((cur->type != XML_ELEMENT_NODE) &&
2354 (cur->content != NULL)) {
Owen Taylor3473f882001-02-23 17:55:21 +00002355 len = xmlStrlen(cur->content);
Owen Taylor3473f882001-02-23 17:55:21 +00002356 }
2357 if (pos > len) {
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002358 /* Strange, the indx in the text node is greater than it's len */
Owen Taylor3473f882001-02-23 17:55:21 +00002359 STRANGE
2360 pos = len;
2361 }
2362 if (pos + bytes >= len) {
2363 bytes -= (len - pos);
William M. Brackf7eb7942003-12-31 07:59:17 +00002364 cur = xmlXPtrAdvanceNode(cur, NULL);
Daniel Veillard24505b02005-07-28 23:49:35 +00002365 pos = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00002366 } else if (pos + bytes < len) {
2367 pos += bytes;
2368 *node = cur;
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002369 *indx = pos;
Owen Taylor3473f882001-02-23 17:55:21 +00002370 return(0);
2371 }
2372 }
2373 return(-1);
2374}
2375
2376/**
2377 * xmlXPtrMatchString:
2378 * @string: the string to search
2379 * @start: the start textnode
2380 * @startindex: the start index
2381 * @end: the end textnode IN/OUT
2382 * @endindex: the end index IN/OUT
2383 *
2384 * Check whether the document contains @string at the position
2385 * (@start, @startindex) and limited by the (@end, @endindex) point
2386 *
2387 * Returns -1 in case of failure, 0 if not found, 1 if found in which case
2388 * (@start, @startindex) will indicate the position of the beginning
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002389 * of the range and (@end, @endindex) will indicate the end
Owen Taylor3473f882001-02-23 17:55:21 +00002390 * of the range
2391 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002392static int
Owen Taylor3473f882001-02-23 17:55:21 +00002393xmlXPtrMatchString(const xmlChar *string, xmlNodePtr start, int startindex,
2394 xmlNodePtr *end, int *endindex) {
2395 xmlNodePtr cur;
2396 int pos; /* 0 based */
2397 int len; /* in bytes */
2398 int stringlen; /* in bytes */
2399 int match;
2400
2401 if (string == NULL)
2402 return(-1);
Daniel Veillard3e62adb2012-08-09 14:24:02 +08002403 if ((start == NULL) || (start->type == XML_NAMESPACE_DECL))
Owen Taylor3473f882001-02-23 17:55:21 +00002404 return(-1);
Daniel Veillard3e62adb2012-08-09 14:24:02 +08002405 if ((end == NULL) || (*end == NULL) ||
2406 ((*end)->type == XML_NAMESPACE_DECL) || (endindex == NULL))
Owen Taylor3473f882001-02-23 17:55:21 +00002407 return(-1);
2408 cur = start;
Owen Taylor3473f882001-02-23 17:55:21 +00002409 pos = startindex - 1;
2410 stringlen = xmlStrlen(string);
2411
2412 while (stringlen > 0) {
2413 if ((cur == *end) && (pos + stringlen > *endindex))
2414 return(0);
Daniel Veillard7db37732001-07-12 01:20:08 +00002415
2416 if ((cur->type != XML_ELEMENT_NODE) && (cur->content != NULL)) {
Owen Taylor3473f882001-02-23 17:55:21 +00002417 len = xmlStrlen(cur->content);
Owen Taylor3473f882001-02-23 17:55:21 +00002418 if (len >= pos + stringlen) {
Owen Taylor3473f882001-02-23 17:55:21 +00002419 match = (!xmlStrncmp(&cur->content[pos], string, stringlen));
Owen Taylor3473f882001-02-23 17:55:21 +00002420 if (match) {
2421#ifdef DEBUG_RANGES
2422 xmlGenericError(xmlGenericErrorContext,
2423 "found range %d bytes at index %d of ->",
2424 stringlen, pos + 1);
2425 xmlDebugDumpString(stdout, cur->content);
2426 xmlGenericError(xmlGenericErrorContext, "\n");
2427#endif
2428 *end = cur;
2429 *endindex = pos + stringlen;
2430 return(1);
2431 } else {
2432 return(0);
2433 }
2434 } else {
2435 int sub = len - pos;
Owen Taylor3473f882001-02-23 17:55:21 +00002436 match = (!xmlStrncmp(&cur->content[pos], string, sub));
Owen Taylor3473f882001-02-23 17:55:21 +00002437 if (match) {
2438#ifdef DEBUG_RANGES
2439 xmlGenericError(xmlGenericErrorContext,
2440 "found subrange %d bytes at index %d of ->",
2441 sub, pos + 1);
2442 xmlDebugDumpString(stdout, cur->content);
2443 xmlGenericError(xmlGenericErrorContext, "\n");
2444#endif
2445 string = &string[sub];
2446 stringlen -= sub;
2447 } else {
2448 return(0);
2449 }
2450 }
2451 }
William M. Brackf7eb7942003-12-31 07:59:17 +00002452 cur = xmlXPtrAdvanceNode(cur, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00002453 if (cur == NULL)
2454 return(0);
2455 pos = 0;
2456 }
2457 return(1);
2458}
2459
2460/**
2461 * xmlXPtrSearchString:
2462 * @string: the string to search
2463 * @start: the start textnode IN/OUT
2464 * @startindex: the start index IN/OUT
2465 * @end: the end textnode
2466 * @endindex: the end index
2467 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002468 * Search the next occurrence of @string within the document content
Owen Taylor3473f882001-02-23 17:55:21 +00002469 * until the (@end, @endindex) point is reached
2470 *
2471 * Returns -1 in case of failure, 0 if not found, 1 if found in which case
2472 * (@start, @startindex) will indicate the position of the beginning
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002473 * of the range and (@end, @endindex) will indicate the end
Owen Taylor3473f882001-02-23 17:55:21 +00002474 * of the range
2475 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002476static int
Owen Taylor3473f882001-02-23 17:55:21 +00002477xmlXPtrSearchString(const xmlChar *string, xmlNodePtr *start, int *startindex,
2478 xmlNodePtr *end, int *endindex) {
2479 xmlNodePtr cur;
2480 const xmlChar *str;
2481 int pos; /* 0 based */
2482 int len; /* in bytes */
Owen Taylor3473f882001-02-23 17:55:21 +00002483 xmlChar first;
2484
2485 if (string == NULL)
2486 return(-1);
Daniel Veillard3e62adb2012-08-09 14:24:02 +08002487 if ((start == NULL) || (*start == NULL) ||
2488 ((*start)->type == XML_NAMESPACE_DECL) || (startindex == NULL))
Owen Taylor3473f882001-02-23 17:55:21 +00002489 return(-1);
2490 if ((end == NULL) || (endindex == NULL))
2491 return(-1);
2492 cur = *start;
Owen Taylor3473f882001-02-23 17:55:21 +00002493 pos = *startindex - 1;
2494 first = string[0];
Owen Taylor3473f882001-02-23 17:55:21 +00002495
2496 while (cur != NULL) {
Daniel Veillard7db37732001-07-12 01:20:08 +00002497 if ((cur->type != XML_ELEMENT_NODE) && (cur->content != NULL)) {
Owen Taylor3473f882001-02-23 17:55:21 +00002498 len = xmlStrlen(cur->content);
Owen Taylor3473f882001-02-23 17:55:21 +00002499 while (pos <= len) {
2500 if (first != 0) {
Owen Taylor3473f882001-02-23 17:55:21 +00002501 str = xmlStrchr(&cur->content[pos], first);
Owen Taylor3473f882001-02-23 17:55:21 +00002502 if (str != NULL) {
2503 pos = (str - (xmlChar *)(cur->content));
2504#ifdef DEBUG_RANGES
2505 xmlGenericError(xmlGenericErrorContext,
2506 "found '%c' at index %d of ->",
2507 first, pos + 1);
2508 xmlDebugDumpString(stdout, cur->content);
2509 xmlGenericError(xmlGenericErrorContext, "\n");
2510#endif
2511 if (xmlXPtrMatchString(string, cur, pos + 1,
2512 end, endindex)) {
2513 *start = cur;
2514 *startindex = pos + 1;
2515 return(1);
2516 }
2517 pos++;
2518 } else {
2519 pos = len + 1;
2520 }
2521 } else {
2522 /*
2523 * An empty string is considered to match before each
2524 * character of the string-value and after the final
Daniel Veillardf8e3db02012-09-11 13:26:36 +08002525 * character.
Owen Taylor3473f882001-02-23 17:55:21 +00002526 */
2527#ifdef DEBUG_RANGES
2528 xmlGenericError(xmlGenericErrorContext,
2529 "found '' at index %d of ->",
2530 pos + 1);
2531 xmlDebugDumpString(stdout, cur->content);
2532 xmlGenericError(xmlGenericErrorContext, "\n");
2533#endif
2534 *start = cur;
2535 *startindex = pos + 1;
2536 *end = cur;
2537 *endindex = pos + 1;
2538 return(1);
2539 }
2540 }
2541 }
2542 if ((cur == *end) && (pos >= *endindex))
2543 return(0);
William M. Brackf7eb7942003-12-31 07:59:17 +00002544 cur = xmlXPtrAdvanceNode(cur, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00002545 if (cur == NULL)
2546 return(0);
2547 pos = 1;
2548 }
2549 return(0);
2550}
2551
2552/**
2553 * xmlXPtrGetLastChar:
2554 * @node: the node
2555 * @index: the index
2556 *
2557 * Computes the point coordinates of the last char of this point
2558 *
2559 * Returns -1 in case of failure, 0 otherwise
2560 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002561static int
2562xmlXPtrGetLastChar(xmlNodePtr *node, int *indx) {
Owen Taylor3473f882001-02-23 17:55:21 +00002563 xmlNodePtr cur;
2564 int pos, len = 0;
2565
Daniel Veillard3e62adb2012-08-09 14:24:02 +08002566 if ((node == NULL) || (*node == NULL) ||
2567 ((*node)->type == XML_NAMESPACE_DECL) || (indx == NULL))
Owen Taylor3473f882001-02-23 17:55:21 +00002568 return(-1);
2569 cur = *node;
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002570 pos = *indx;
Owen Taylor3473f882001-02-23 17:55:21 +00002571
Owen Taylor3473f882001-02-23 17:55:21 +00002572 if ((cur->type == XML_ELEMENT_NODE) ||
2573 (cur->type == XML_DOCUMENT_NODE) ||
2574 (cur->type == XML_HTML_DOCUMENT_NODE)) {
2575 if (pos > 0) {
2576 cur = xmlXPtrGetNthChild(cur, pos);
Owen Taylor3473f882001-02-23 17:55:21 +00002577 }
2578 }
2579 while (cur != NULL) {
2580 if (cur->last != NULL)
2581 cur = cur->last;
Daniel Veillard7db37732001-07-12 01:20:08 +00002582 else if ((cur->type != XML_ELEMENT_NODE) &&
2583 (cur->content != NULL)) {
Owen Taylor3473f882001-02-23 17:55:21 +00002584 len = xmlStrlen(cur->content);
Owen Taylor3473f882001-02-23 17:55:21 +00002585 break;
2586 } else {
2587 return(-1);
2588 }
2589 }
2590 if (cur == NULL)
2591 return(-1);
2592 *node = cur;
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002593 *indx = len;
Owen Taylor3473f882001-02-23 17:55:21 +00002594 return(0);
2595}
2596
2597/**
2598 * xmlXPtrGetStartPoint:
2599 * @obj: an range
2600 * @node: the resulting node
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002601 * @indx: the resulting index
Owen Taylor3473f882001-02-23 17:55:21 +00002602 *
2603 * read the object and return the start point coordinates.
2604 *
2605 * Returns -1 in case of failure, 0 otherwise
2606 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002607static int
2608xmlXPtrGetStartPoint(xmlXPathObjectPtr obj, xmlNodePtr *node, int *indx) {
2609 if ((obj == NULL) || (node == NULL) || (indx == NULL))
Owen Taylor3473f882001-02-23 17:55:21 +00002610 return(-1);
2611
2612 switch (obj->type) {
2613 case XPATH_POINT:
2614 *node = obj->user;
2615 if (obj->index <= 0)
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002616 *indx = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00002617 else
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002618 *indx = obj->index;
Owen Taylor3473f882001-02-23 17:55:21 +00002619 return(0);
2620 case XPATH_RANGE:
2621 *node = obj->user;
2622 if (obj->index <= 0)
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002623 *indx = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00002624 else
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002625 *indx = obj->index;
Owen Taylor3473f882001-02-23 17:55:21 +00002626 return(0);
2627 default:
Daniel Veillardb44025c2001-10-11 22:55:55 +00002628 break;
Owen Taylor3473f882001-02-23 17:55:21 +00002629 }
2630 return(-1);
2631}
2632
2633/**
2634 * xmlXPtrGetEndPoint:
2635 * @obj: an range
2636 * @node: the resulting node
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002637 * @indx: the resulting indx
Owen Taylor3473f882001-02-23 17:55:21 +00002638 *
2639 * read the object and return the end point coordinates.
2640 *
2641 * Returns -1 in case of failure, 0 otherwise
2642 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002643static int
2644xmlXPtrGetEndPoint(xmlXPathObjectPtr obj, xmlNodePtr *node, int *indx) {
2645 if ((obj == NULL) || (node == NULL) || (indx == NULL))
Owen Taylor3473f882001-02-23 17:55:21 +00002646 return(-1);
2647
2648 switch (obj->type) {
2649 case XPATH_POINT:
2650 *node = obj->user;
2651 if (obj->index <= 0)
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002652 *indx = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00002653 else
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002654 *indx = obj->index;
Owen Taylor3473f882001-02-23 17:55:21 +00002655 return(0);
2656 case XPATH_RANGE:
2657 *node = obj->user;
2658 if (obj->index <= 0)
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002659 *indx = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00002660 else
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002661 *indx = obj->index;
Owen Taylor3473f882001-02-23 17:55:21 +00002662 return(0);
2663 default:
Daniel Veillardb44025c2001-10-11 22:55:55 +00002664 break;
Owen Taylor3473f882001-02-23 17:55:21 +00002665 }
2666 return(-1);
2667}
2668
2669/**
2670 * xmlXPtrStringRangeFunction:
2671 * @ctxt: the XPointer Parser context
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002672 * @nargs: the number of args
Owen Taylor3473f882001-02-23 17:55:21 +00002673 *
2674 * Function implementing the string-range() function
Daniel Veillardf8e3db02012-09-11 13:26:36 +08002675 * range as described in 5.4.2
Owen Taylor3473f882001-02-23 17:55:21 +00002676 *
2677 * ------------------------------
2678 * [Definition: For each location in the location-set argument,
2679 * string-range returns a set of string ranges, a set of substrings in a
2680 * string. Specifically, the string-value of the location is searched for
2681 * substrings that match the string argument, and the resulting location-set
2682 * will contain a range location for each non-overlapping match.]
2683 * An empty string is considered to match before each character of the
2684 * string-value and after the final character. Whitespace in a string
2685 * is matched literally, with no normalization except that provided by
2686 * XML for line ends. The third argument gives the position of the first
2687 * character to be in the resulting range, relative to the start of the
2688 * match. The default value is 1, which makes the range start immediately
2689 * before the first character of the matched string. The fourth argument
2690 * gives the number of characters in the range; the default is that the
2691 * range extends to the end of the matched string.
2692 *
2693 * Element boundaries, as well as entire embedded nodes such as processing
2694 * instructions and comments, are ignored as defined in [XPath].
2695 *
2696 * If the string in the second argument is not found in the string-value
2697 * of the location, or if a value in the third or fourth argument indicates
2698 * a string that is beyond the beginning or end of the document, the
2699 * expression fails.
2700 *
2701 * The points of the range-locations in the returned location-set will
2702 * all be character points.
2703 * ------------------------------
2704 */
Daniel Veillard8ed10722009-08-20 19:17:36 +02002705static void
Owen Taylor3473f882001-02-23 17:55:21 +00002706xmlXPtrStringRangeFunction(xmlXPathParserContextPtr ctxt, int nargs) {
Daniel Veillardaac7c682006-03-10 13:40:16 +00002707 int i, startindex, endindex = 0, fendindex;
2708 xmlNodePtr start, end = 0, fend;
Haibo Huangd23e46c2020-10-28 22:26:09 -07002709 xmlXPathObjectPtr set = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +00002710 xmlLocationSetPtr oldset;
Haibo Huangd23e46c2020-10-28 22:26:09 -07002711 xmlLocationSetPtr newset = NULL;
2712 xmlXPathObjectPtr string = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +00002713 xmlXPathObjectPtr position = NULL;
2714 xmlXPathObjectPtr number = NULL;
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002715 int found, pos = 0, num = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00002716
2717 /*
2718 * Grab the arguments
2719 */
2720 if ((nargs < 2) || (nargs > 4))
2721 XP_ERROR(XPATH_INVALID_ARITY);
2722
2723 if (nargs >= 4) {
Haibo Huangd23e46c2020-10-28 22:26:09 -07002724 if ((ctxt->value == NULL) || (ctxt->value->type != XPATH_NUMBER)) {
2725 xmlXPathErr(ctxt, XPATH_INVALID_TYPE);
2726 goto error;
2727 }
Owen Taylor3473f882001-02-23 17:55:21 +00002728 number = valuePop(ctxt);
2729 if (number != NULL)
Daniel Veillard87ee9142001-06-28 12:54:16 +00002730 num = (int) number->floatval;
Owen Taylor3473f882001-02-23 17:55:21 +00002731 }
2732 if (nargs >= 3) {
Haibo Huangd23e46c2020-10-28 22:26:09 -07002733 if ((ctxt->value == NULL) || (ctxt->value->type != XPATH_NUMBER)) {
2734 xmlXPathErr(ctxt, XPATH_INVALID_TYPE);
2735 goto error;
2736 }
Owen Taylor3473f882001-02-23 17:55:21 +00002737 position = valuePop(ctxt);
2738 if (position != NULL)
Daniel Veillard87ee9142001-06-28 12:54:16 +00002739 pos = (int) position->floatval;
Owen Taylor3473f882001-02-23 17:55:21 +00002740 }
Haibo Huangd23e46c2020-10-28 22:26:09 -07002741 if ((ctxt->value == NULL) || (ctxt->value->type != XPATH_STRING)) {
2742 xmlXPathErr(ctxt, XPATH_INVALID_TYPE);
2743 goto error;
2744 }
Owen Taylor3473f882001-02-23 17:55:21 +00002745 string = valuePop(ctxt);
2746 if ((ctxt->value == NULL) ||
2747 ((ctxt->value->type != XPATH_LOCATIONSET) &&
Haibo Huangd23e46c2020-10-28 22:26:09 -07002748 (ctxt->value->type != XPATH_NODESET))) {
2749 xmlXPathErr(ctxt, XPATH_INVALID_TYPE);
2750 goto error;
2751 }
Owen Taylor3473f882001-02-23 17:55:21 +00002752 set = valuePop(ctxt);
William M. Brack08171912003-12-29 02:52:11 +00002753 newset = xmlXPtrLocationSetCreate(NULL);
Gaurav Guptaf5496a12014-10-07 17:09:35 +08002754 if (newset == NULL) {
Haibo Huangd23e46c2020-10-28 22:26:09 -07002755 xmlXPathErr(ctxt, XPATH_MEMORY_ERROR);
2756 goto error;
Gaurav Guptaf5496a12014-10-07 17:09:35 +08002757 }
William M. Brack08171912003-12-29 02:52:11 +00002758 if (set->nodesetval == NULL) {
2759 goto error;
2760 }
Owen Taylor3473f882001-02-23 17:55:21 +00002761 if (set->type == XPATH_NODESET) {
2762 xmlXPathObjectPtr tmp;
2763
2764 /*
2765 * First convert to a location set
2766 */
2767 tmp = xmlXPtrNewLocationSetNodeSet(set->nodesetval);
2768 xmlXPathFreeObject(set);
Haibo Huangd23e46c2020-10-28 22:26:09 -07002769 if (tmp == NULL) {
2770 xmlXPathErr(ctxt, XPATH_MEMORY_ERROR);
2771 goto error;
2772 }
Owen Taylor3473f882001-02-23 17:55:21 +00002773 set = tmp;
2774 }
2775 oldset = (xmlLocationSetPtr) set->user;
2776
2777 /*
2778 * The loop is to search for each element in the location set
2779 * the list of location set corresponding to that search
2780 */
Owen Taylor3473f882001-02-23 17:55:21 +00002781 for (i = 0;i < oldset->locNr;i++) {
2782#ifdef DEBUG_RANGES
2783 xmlXPathDebugDumpObject(stdout, oldset->locTab[i], 0);
2784#endif
2785
2786 xmlXPtrGetStartPoint(oldset->locTab[i], &start, &startindex);
2787 xmlXPtrGetEndPoint(oldset->locTab[i], &end, &endindex);
2788 xmlXPtrAdvanceChar(&start, &startindex, 0);
2789 xmlXPtrGetLastChar(&end, &endindex);
2790
2791#ifdef DEBUG_RANGES
2792 xmlGenericError(xmlGenericErrorContext,
2793 "from index %d of ->", startindex);
2794 xmlDebugDumpString(stdout, start->content);
2795 xmlGenericError(xmlGenericErrorContext, "\n");
2796 xmlGenericError(xmlGenericErrorContext,
2797 "to index %d of ->", endindex);
2798 xmlDebugDumpString(stdout, end->content);
2799 xmlGenericError(xmlGenericErrorContext, "\n");
2800#endif
2801 do {
2802 fend = end;
2803 fendindex = endindex;
2804 found = xmlXPtrSearchString(string->stringval, &start, &startindex,
2805 &fend, &fendindex);
2806 if (found == 1) {
2807 if (position == NULL) {
2808 xmlXPtrLocationSetAdd(newset,
2809 xmlXPtrNewRange(start, startindex, fend, fendindex));
2810 } else if (xmlXPtrAdvanceChar(&start, &startindex,
2811 pos - 1) == 0) {
2812 if ((number != NULL) && (num > 0)) {
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002813 int rindx;
Owen Taylor3473f882001-02-23 17:55:21 +00002814 xmlNodePtr rend;
2815 rend = start;
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002816 rindx = startindex - 1;
2817 if (xmlXPtrAdvanceChar(&rend, &rindx,
Owen Taylor3473f882001-02-23 17:55:21 +00002818 num) == 0) {
2819 xmlXPtrLocationSetAdd(newset,
2820 xmlXPtrNewRange(start, startindex,
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002821 rend, rindx));
Owen Taylor3473f882001-02-23 17:55:21 +00002822 }
2823 } else if ((number != NULL) && (num <= 0)) {
2824 xmlXPtrLocationSetAdd(newset,
2825 xmlXPtrNewRange(start, startindex,
2826 start, startindex));
2827 } else {
2828 xmlXPtrLocationSetAdd(newset,
2829 xmlXPtrNewRange(start, startindex,
2830 fend, fendindex));
2831 }
2832 }
2833 start = fend;
2834 startindex = fendindex;
2835 if (string->stringval[0] == 0)
2836 startindex++;
2837 }
2838 } while (found == 1);
2839 }
2840
2841 /*
2842 * Save the new value and cleanup
2843 */
William M. Brack08171912003-12-29 02:52:11 +00002844error:
Haibo Huangd23e46c2020-10-28 22:26:09 -07002845 if (newset != NULL)
2846 valuePush(ctxt, xmlXPtrWrapLocationSet(newset));
Owen Taylor3473f882001-02-23 17:55:21 +00002847 xmlXPathFreeObject(set);
2848 xmlXPathFreeObject(string);
2849 if (position) xmlXPathFreeObject(position);
2850 if (number) xmlXPathFreeObject(number);
2851}
2852
2853/**
2854 * xmlXPtrEvalRangePredicate:
2855 * @ctxt: the XPointer Parser context
2856 *
2857 * [8] Predicate ::= '[' PredicateExpr ']'
Daniel Veillardf8e3db02012-09-11 13:26:36 +08002858 * [9] PredicateExpr ::= Expr
Owen Taylor3473f882001-02-23 17:55:21 +00002859 *
2860 * Evaluate a predicate as in xmlXPathEvalPredicate() but for
2861 * a Location Set instead of a node set
2862 */
2863void
2864xmlXPtrEvalRangePredicate(xmlXPathParserContextPtr ctxt) {
2865 const xmlChar *cur;
2866 xmlXPathObjectPtr res;
2867 xmlXPathObjectPtr obj, tmp;
2868 xmlLocationSetPtr newset = NULL;
2869 xmlLocationSetPtr oldset;
2870 int i;
2871
Daniel Veillardce682bc2004-11-05 17:22:25 +00002872 if (ctxt == NULL) return;
2873
Owen Taylor3473f882001-02-23 17:55:21 +00002874 SKIP_BLANKS;
2875 if (CUR != '[') {
2876 XP_ERROR(XPATH_INVALID_PREDICATE_ERROR);
2877 }
2878 NEXT;
2879 SKIP_BLANKS;
2880
2881 /*
2882 * Extract the old set, and then evaluate the result of the
2883 * expression for all the element in the set. use it to grow
2884 * up a new set.
2885 */
2886 CHECK_TYPE(XPATH_LOCATIONSET);
2887 obj = valuePop(ctxt);
2888 oldset = obj->user;
2889 ctxt->context->node = NULL;
2890
2891 if ((oldset == NULL) || (oldset->locNr == 0)) {
2892 ctxt->context->contextSize = 0;
2893 ctxt->context->proximityPosition = 0;
2894 xmlXPathEvalExpr(ctxt);
2895 res = valuePop(ctxt);
2896 if (res != NULL)
2897 xmlXPathFreeObject(res);
2898 valuePush(ctxt, obj);
2899 CHECK_ERROR;
2900 } else {
2901 /*
2902 * Save the expression pointer since we will have to evaluate
2903 * it multiple times. Initialize the new set.
2904 */
2905 cur = ctxt->cur;
2906 newset = xmlXPtrLocationSetCreate(NULL);
Daniel Veillardf8e3db02012-09-11 13:26:36 +08002907
Owen Taylor3473f882001-02-23 17:55:21 +00002908 for (i = 0; i < oldset->locNr; i++) {
2909 ctxt->cur = cur;
2910
2911 /*
2912 * Run the evaluation with a node list made of a single item
2913 * in the nodeset.
2914 */
2915 ctxt->context->node = oldset->locTab[i]->user;
2916 tmp = xmlXPathNewNodeSet(ctxt->context->node);
2917 valuePush(ctxt, tmp);
2918 ctxt->context->contextSize = oldset->locNr;
2919 ctxt->context->proximityPosition = i + 1;
2920
2921 xmlXPathEvalExpr(ctxt);
2922 CHECK_ERROR;
2923
2924 /*
2925 * The result of the evaluation need to be tested to
2926 * decided whether the filter succeeded or not
2927 */
2928 res = valuePop(ctxt);
2929 if (xmlXPathEvaluatePredicateResult(ctxt, res)) {
2930 xmlXPtrLocationSetAdd(newset,
2931 xmlXPathObjectCopy(oldset->locTab[i]));
2932 }
2933
2934 /*
2935 * Cleanup
2936 */
2937 if (res != NULL)
2938 xmlXPathFreeObject(res);
2939 if (ctxt->value == tmp) {
2940 res = valuePop(ctxt);
2941 xmlXPathFreeObject(res);
2942 }
Daniel Veillardf8e3db02012-09-11 13:26:36 +08002943
Owen Taylor3473f882001-02-23 17:55:21 +00002944 ctxt->context->node = NULL;
2945 }
2946
2947 /*
2948 * The result is used as the new evaluation set.
2949 */
2950 xmlXPathFreeObject(obj);
2951 ctxt->context->node = NULL;
2952 ctxt->context->contextSize = -1;
2953 ctxt->context->proximityPosition = -1;
2954 valuePush(ctxt, xmlXPtrWrapLocationSet(newset));
2955 }
2956 if (CUR != ']') {
2957 XP_ERROR(XPATH_INVALID_PREDICATE_ERROR);
2958 }
2959
2960 NEXT;
2961 SKIP_BLANKS;
2962}
2963
Daniel Veillard5d4644e2005-04-01 13:11:58 +00002964#define bottom_xpointer
2965#include "elfgcchack.h"
Owen Taylor3473f882001-02-23 17:55:21 +00002966#endif
2967