Bug Summary

File:xpointer.c
Location:line 1475, column 4
Description:Value stored to 'index1' is never read

Annotated Source Code

1/*
2 * xpointer.c : Code to handle XML Pointer
3 *
4 * Base implementation was made accordingly to
5 * W3C Candidate Recommendation 7 June 2000
6 * http://www.w3.org/TR/2000/CR-xptr-20000607
7 *
8 * Added support for the element() scheme described in:
9 * W3C Proposed Recommendation 13 November 2002
10 * http://www.w3.org/TR/2002/PR-xptr-element-20021113/
11 *
12 * See Copyright for the status of this software.
13 *
14 * daniel@veillard.com
15 */
16
17#define IN_LIBXML
18#include "libxml.h"
19
20/*
21 * TODO: better handling of error cases, the full expression should
22 * be parsed beforehand instead of a progressive evaluation
23 * TODO: Access into entities references are not supported now ...
24 * need a start to be able to pop out of entities refs since
25 * parent is the endity declaration, not the ref.
26 */
27
28#include <string.h>
29#include <libxml/xpointer.h>
30#include <libxml/xmlmemory.h>
31#include <libxml/parserInternals.h>
32#include <libxml/uri.h>
33#include <libxml/xpath.h>
34#include <libxml/xpathInternals.h>
35#include <libxml/xmlerror.h>
36#include <libxml/globals.h>
37
38#ifdef LIBXML_XPTR_ENABLED
39
40/* Add support of the xmlns() xpointer scheme to initialize the namespaces */
41#define XPTR_XMLNS_SCHEME
42
43/* #define DEBUG_RANGES */
44#ifdef DEBUG_RANGES
45#ifdef LIBXML_DEBUG_ENABLED
46#include <libxml/debugXML.h>
47#endif
48#endif
49
50#define TODO(*(__xmlGenericError()))((*(__xmlGenericErrorContext())), "Unimplemented block at %s:%d\n"
, "xpointer.c", 50);
\
51 xmlGenericError(*(__xmlGenericError()))(xmlGenericErrorContext(*(__xmlGenericErrorContext())), \
52 "Unimplemented block at %s:%d\n", \
53 __FILE__"xpointer.c", __LINE__53);
54
55#define STRANGE(*(__xmlGenericError()))((*(__xmlGenericErrorContext())), "Internal error at %s:%d\n"
, "xpointer.c", 55);
\
56 xmlGenericError(*(__xmlGenericError()))(xmlGenericErrorContext(*(__xmlGenericErrorContext())), \
57 "Internal error at %s:%d\n", \
58 __FILE__"xpointer.c", __LINE__58);
59
60/************************************************************************
61 * *
62 * Some factorized error routines *
63 * *
64 ************************************************************************/
65
66/**
67 * xmlXPtrErrMemory:
68 * @extra: extra informations
69 *
70 * Handle a redefinition of attribute error
71 */
72static void
73xmlXPtrErrMemory(const char *extra)
74{
75 __xmlRaiseError(NULL((void*)0), NULL((void*)0), NULL((void*)0), NULL((void*)0), NULL((void*)0), XML_FROM_XPOINTER,
76 XML_ERR_NO_MEMORY, XML_ERR_ERROR, NULL((void*)0), 0, extra,
77 NULL((void*)0), NULL((void*)0), 0, 0,
78 "Memory allocation failed : %s\n", extra);
79}
80
81/**
82 * xmlXPtrErr:
83 * @ctxt: an XPTR evaluation context
84 * @extra: extra informations
85 *
86 * Handle a redefinition of attribute error
87 */
88static void
89xmlXPtrErr(xmlXPathParserContextPtr ctxt, int error,
90 const char * msg, const xmlChar *extra)
91{
92 if (ctxt != NULL((void*)0))
93 ctxt->error = error;
94 if ((ctxt == NULL((void*)0)) || (ctxt->context == NULL((void*)0))) {
95 __xmlRaiseError(NULL((void*)0), NULL((void*)0), NULL((void*)0),
96 NULL((void*)0), NULL((void*)0), XML_FROM_XPOINTER, error,
97 XML_ERR_ERROR, NULL((void*)0), 0,
98 (const char *) extra, NULL((void*)0), NULL((void*)0), 0, 0,
99 msg, extra);
100 return;
101 }
102 ctxt->context->lastError.domain = XML_FROM_XPOINTER;
103 ctxt->context->lastError.code = error;
104 ctxt->context->lastError.level = XML_ERR_ERROR;
105 ctxt->context->lastError.str1 = (char *) xmlStrdup(ctxt->base);
106 ctxt->context->lastError.int1 = ctxt->cur - ctxt->base;
107 ctxt->context->lastError.node = ctxt->context->debugNode;
108 if (ctxt->context->error != NULL((void*)0)) {
109 ctxt->context->error(ctxt->context->userData,
110 &ctxt->context->lastError);
111 } else {
112 __xmlRaiseError(NULL((void*)0), NULL((void*)0), NULL((void*)0),
113 NULL((void*)0), ctxt->context->debugNode, XML_FROM_XPOINTER,
114 error, XML_ERR_ERROR, NULL((void*)0), 0,
115 (const char *) extra, (const char *) ctxt->base, NULL((void*)0),
116 ctxt->cur - ctxt->base, 0,
117 msg, extra);
118 }
119}
120
121/************************************************************************
122 * *
123 * A few helper functions for child sequences *
124 * *
125 ************************************************************************/
126/* xmlXPtrAdvanceNode is a private function, but used by xinclude.c */
127xmlNodePtr xmlXPtrAdvanceNode(xmlNodePtr cur, int *level);
128/**
129 * xmlXPtrGetArity:
130 * @cur: the node
131 *
132 * Returns the number of child for an element, -1 in case of error
133 */
134static int
135xmlXPtrGetArity(xmlNodePtr cur) {
136 int i;
137 if (cur == NULL((void*)0))
138 return(-1);
139 cur = cur->children;
140 for (i = 0;cur != NULL((void*)0);cur = cur->next) {
141 if ((cur->type == XML_ELEMENT_NODE) ||
142 (cur->type == XML_DOCUMENT_NODE) ||
143 (cur->type == XML_HTML_DOCUMENT_NODE)) {
144 i++;
145 }
146 }
147 return(i);
148}
149
150/**
151 * xmlXPtrGetIndex:
152 * @cur: the node
153 *
154 * Returns the index of the node in its parent children list, -1
155 * in case of error
156 */
157static int
158xmlXPtrGetIndex(xmlNodePtr cur) {
159 int i;
160 if (cur == NULL((void*)0))
161 return(-1);
162 for (i = 1;cur != NULL((void*)0);cur = cur->prev) {
163 if ((cur->type == XML_ELEMENT_NODE) ||
164 (cur->type == XML_DOCUMENT_NODE) ||
165 (cur->type == XML_HTML_DOCUMENT_NODE)) {
166 i++;
167 }
168 }
169 return(i);
170}
171
172/**
173 * xmlXPtrGetNthChild:
174 * @cur: the node
175 * @no: the child number
176 *
177 * Returns the @no'th element child of @cur or NULL
178 */
179static xmlNodePtr
180xmlXPtrGetNthChild(xmlNodePtr cur, int no) {
181 int i;
182 if (cur == NULL((void*)0))
183 return(cur);
184 cur = cur->children;
185 for (i = 0;i <= no;cur = cur->next) {
186 if (cur == NULL((void*)0))
187 return(cur);
188 if ((cur->type == XML_ELEMENT_NODE) ||
189 (cur->type == XML_DOCUMENT_NODE) ||
190 (cur->type == XML_HTML_DOCUMENT_NODE)) {
191 i++;
192 if (i == no)
193 break;
194 }
195 }
196 return(cur);
197}
198
199/************************************************************************
200 * *
201 * Handling of XPointer specific types *
202 * *
203 ************************************************************************/
204
205/**
206 * xmlXPtrCmpPoints:
207 * @node1: the first node
208 * @index1: the first index
209 * @node2: the second node
210 * @index2: the second index
211 *
212 * Compare two points w.r.t document order
213 *
214 * Returns -2 in case of error 1 if first point < second point, 0 if
215 * that's the same point, -1 otherwise
216 */
217static int
218xmlXPtrCmpPoints(xmlNodePtr node1, int index1, xmlNodePtr node2, int index2) {
219 if ((node1 == NULL((void*)0)) || (node2 == NULL((void*)0)))
220 return(-2);
221 /*
222 * a couple of optimizations which will avoid computations in most cases
223 */
224 if (node1 == node2) {
225 if (index1 < index2)
226 return(1);
227 if (index1 > index2)
228 return(-1);
229 return(0);
230 }
231 return(xmlXPathCmpNodes(node1, node2));
232}
233
234/**
235 * xmlXPtrNewPoint:
236 * @node: the xmlNodePtr
237 * @indx: the indx within the node
238 *
239 * Create a new xmlXPathObjectPtr of type point
240 *
241 * Returns the newly created object.
242 */
243static xmlXPathObjectPtr
244xmlXPtrNewPoint(xmlNodePtr node, int indx) {
245 xmlXPathObjectPtr ret;
246
247 if (node == NULL((void*)0))
248 return(NULL((void*)0));
249 if (indx < 0)
250 return(NULL((void*)0));
251
252 ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject));
253 if (ret == NULL((void*)0)) {
254 xmlXPtrErrMemory("allocating point");
255 return(NULL((void*)0));
256 }
257 memset(ret, 0 , (size_t) sizeof(xmlXPathObject));
258 ret->type = XPATH_POINT;
259 ret->user = (void *) node;
260 ret->index = indx;
261 return(ret);
262}
263
264/**
265 * xmlXPtrRangeCheckOrder:
266 * @range: an object range
267 *
268 * Make sure the points in the range are in the right order
269 */
270static void
271xmlXPtrRangeCheckOrder(xmlXPathObjectPtr range) {
272 int tmp;
273 xmlNodePtr tmp2;
274 if (range == NULL((void*)0))
275 return;
276 if (range->type != XPATH_RANGE)
277 return;
278 if (range->user2 == NULL((void*)0))
279 return;
280 tmp = xmlXPtrCmpPoints(range->user, range->index,
281 range->user2, range->index2);
282 if (tmp == -1) {
283 tmp2 = range->user;
284 range->user = range->user2;
285 range->user2 = tmp2;
286 tmp = range->index;
287 range->index = range->index2;
288 range->index2 = tmp;
289 }
290}
291
292/**
293 * xmlXPtrRangesEqual:
294 * @range1: the first range
295 * @range2: the second range
296 *
297 * Compare two ranges
298 *
299 * Returns 1 if equal, 0 otherwise
300 */
301static int
302xmlXPtrRangesEqual(xmlXPathObjectPtr range1, xmlXPathObjectPtr range2) {
303 if (range1 == range2)
304 return(1);
305 if ((range1 == NULL((void*)0)) || (range2 == NULL((void*)0)))
306 return(0);
307 if (range1->type != range2->type)
308 return(0);
309 if (range1->type != XPATH_RANGE)
310 return(0);
311 if (range1->user != range2->user)
312 return(0);
313 if (range1->index != range2->index)
314 return(0);
315 if (range1->user2 != range2->user2)
316 return(0);
317 if (range1->index2 != range2->index2)
318 return(0);
319 return(1);
320}
321
322/**
323 * xmlXPtrNewRange:
324 * @start: the starting node
325 * @startindex: the start index
326 * @end: the ending point
327 * @endindex: the ending index
328 *
329 * Create a new xmlXPathObjectPtr of type range
330 *
331 * Returns the newly created object.
332 */
333xmlXPathObjectPtr
334xmlXPtrNewRange(xmlNodePtr start, int startindex,
335 xmlNodePtr end, int endindex) {
336 xmlXPathObjectPtr ret;
337
338 if (start == NULL((void*)0))
339 return(NULL((void*)0));
340 if (end == NULL((void*)0))
341 return(NULL((void*)0));
342 if (startindex < 0)
343 return(NULL((void*)0));
344 if (endindex < 0)
345 return(NULL((void*)0));
346
347 ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject));
348 if (ret == NULL((void*)0)) {
349 xmlXPtrErrMemory("allocating range");
350 return(NULL((void*)0));
351 }
352 memset(ret, 0 , (size_t) sizeof(xmlXPathObject));
353 ret->type = XPATH_RANGE;
354 ret->user = start;
355 ret->index = startindex;
356 ret->user2 = end;
357 ret->index2 = endindex;
358 xmlXPtrRangeCheckOrder(ret);
359 return(ret);
360}
361
362/**
363 * xmlXPtrNewRangePoints:
364 * @start: the starting point
365 * @end: the ending point
366 *
367 * Create a new xmlXPathObjectPtr of type range using 2 Points
368 *
369 * Returns the newly created object.
370 */
371xmlXPathObjectPtr
372xmlXPtrNewRangePoints(xmlXPathObjectPtr start, xmlXPathObjectPtr end) {
373 xmlXPathObjectPtr ret;
374
375 if (start == NULL((void*)0))
376 return(NULL((void*)0));
377 if (end == NULL((void*)0))
378 return(NULL((void*)0));
379 if (start->type != XPATH_POINT)
380 return(NULL((void*)0));
381 if (end->type != XPATH_POINT)
382 return(NULL((void*)0));
383
384 ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject));
385 if (ret == NULL((void*)0)) {
386 xmlXPtrErrMemory("allocating range");
387 return(NULL((void*)0));
388 }
389 memset(ret, 0 , (size_t) sizeof(xmlXPathObject));
390 ret->type = XPATH_RANGE;
391 ret->user = start->user;
392 ret->index = start->index;
393 ret->user2 = end->user;
394 ret->index2 = end->index;
395 xmlXPtrRangeCheckOrder(ret);
396 return(ret);
397}
398
399/**
400 * xmlXPtrNewRangePointNode:
401 * @start: the starting point
402 * @end: the ending node
403 *
404 * Create a new xmlXPathObjectPtr of type range from a point to a node
405 *
406 * Returns the newly created object.
407 */
408xmlXPathObjectPtr
409xmlXPtrNewRangePointNode(xmlXPathObjectPtr start, xmlNodePtr end) {
410 xmlXPathObjectPtr ret;
411
412 if (start == NULL((void*)0))
413 return(NULL((void*)0));
414 if (end == NULL((void*)0))
415 return(NULL((void*)0));
416 if (start->type != XPATH_POINT)
417 return(NULL((void*)0));
418
419 ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject));
420 if (ret == NULL((void*)0)) {
421 xmlXPtrErrMemory("allocating range");
422 return(NULL((void*)0));
423 }
424 memset(ret, 0 , (size_t) sizeof(xmlXPathObject));
425 ret->type = XPATH_RANGE;
426 ret->user = start->user;
427 ret->index = start->index;
428 ret->user2 = end;
429 ret->index2 = -1;
430 xmlXPtrRangeCheckOrder(ret);
431 return(ret);
432}
433
434/**
435 * xmlXPtrNewRangeNodePoint:
436 * @start: the starting node
437 * @end: the ending point
438 *
439 * Create a new xmlXPathObjectPtr of type range from a node to a point
440 *
441 * Returns the newly created object.
442 */
443xmlXPathObjectPtr
444xmlXPtrNewRangeNodePoint(xmlNodePtr start, xmlXPathObjectPtr end) {
445 xmlXPathObjectPtr ret;
446
447 if (start == NULL((void*)0))
448 return(NULL((void*)0));
449 if (end == NULL((void*)0))
450 return(NULL((void*)0));
451 if (start->type != XPATH_POINT)
452 return(NULL((void*)0));
453 if (end->type != XPATH_POINT)
454 return(NULL((void*)0));
455
456 ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject));
457 if (ret == NULL((void*)0)) {
458 xmlXPtrErrMemory("allocating range");
459 return(NULL((void*)0));
460 }
461 memset(ret, 0 , (size_t) sizeof(xmlXPathObject));
462 ret->type = XPATH_RANGE;
463 ret->user = start;
464 ret->index = -1;
465 ret->user2 = end->user;
466 ret->index2 = end->index;
467 xmlXPtrRangeCheckOrder(ret);
468 return(ret);
469}
470
471/**
472 * xmlXPtrNewRangeNodes:
473 * @start: the starting node
474 * @end: the ending node
475 *
476 * Create a new xmlXPathObjectPtr of type range using 2 nodes
477 *
478 * Returns the newly created object.
479 */
480xmlXPathObjectPtr
481xmlXPtrNewRangeNodes(xmlNodePtr start, xmlNodePtr end) {
482 xmlXPathObjectPtr ret;
483
484 if (start == NULL((void*)0))
485 return(NULL((void*)0));
486 if (end == NULL((void*)0))
487 return(NULL((void*)0));
488
489 ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject));
490 if (ret == NULL((void*)0)) {
491 xmlXPtrErrMemory("allocating range");
492 return(NULL((void*)0));
493 }
494 memset(ret, 0 , (size_t) sizeof(xmlXPathObject));
495 ret->type = XPATH_RANGE;
496 ret->user = start;
497 ret->index = -1;
498 ret->user2 = end;
499 ret->index2 = -1;
500 xmlXPtrRangeCheckOrder(ret);
501 return(ret);
502}
503
504/**
505 * xmlXPtrNewCollapsedRange:
506 * @start: the starting and ending node
507 *
508 * Create a new xmlXPathObjectPtr of type range using a single nodes
509 *
510 * Returns the newly created object.
511 */
512xmlXPathObjectPtr
513xmlXPtrNewCollapsedRange(xmlNodePtr start) {
514 xmlXPathObjectPtr ret;
515
516 if (start == NULL((void*)0))
517 return(NULL((void*)0));
518
519 ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject));
520 if (ret == NULL((void*)0)) {
521 xmlXPtrErrMemory("allocating range");
522 return(NULL((void*)0));
523 }
524 memset(ret, 0 , (size_t) sizeof(xmlXPathObject));
525 ret->type = XPATH_RANGE;
526 ret->user = start;
527 ret->index = -1;
528 ret->user2 = NULL((void*)0);
529 ret->index2 = -1;
530 return(ret);
531}
532
533/**
534 * xmlXPtrNewRangeNodeObject:
535 * @start: the starting node
536 * @end: the ending object
537 *
538 * Create a new xmlXPathObjectPtr of type range from a not to an object
539 *
540 * Returns the newly created object.
541 */
542xmlXPathObjectPtr
543xmlXPtrNewRangeNodeObject(xmlNodePtr start, xmlXPathObjectPtr end) {
544 xmlXPathObjectPtr ret;
545
546 if (start == NULL((void*)0))
547 return(NULL((void*)0));
548 if (end == NULL((void*)0))
549 return(NULL((void*)0));
550 switch (end->type) {
551 case XPATH_POINT:
552 case XPATH_RANGE:
553 break;
554 case XPATH_NODESET:
555 /*
556 * Empty set ...
557 */
558 if (end->nodesetval->nodeNr <= 0)
559 return(NULL((void*)0));
560 break;
561 default:
562 /* TODO */
563 return(NULL((void*)0));
564 }
565
566 ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject));
567 if (ret == NULL((void*)0)) {
568 xmlXPtrErrMemory("allocating range");
569 return(NULL((void*)0));
570 }
571 memset(ret, 0 , (size_t) sizeof(xmlXPathObject));
572 ret->type = XPATH_RANGE;
573 ret->user = start;
574 ret->index = -1;
575 switch (end->type) {
576 case XPATH_POINT:
577 ret->user2 = end->user;
578 ret->index2 = end->index;
579 break;
580 case XPATH_RANGE:
581 ret->user2 = end->user2;
582 ret->index2 = end->index2;
583 break;
584 case XPATH_NODESET: {
585 ret->user2 = end->nodesetval->nodeTab[end->nodesetval->nodeNr - 1];
586 ret->index2 = -1;
587 break;
588 }
589 default:
590 STRANGE(*(__xmlGenericError()))((*(__xmlGenericErrorContext())), "Internal error at %s:%d\n"
, "xpointer.c", 590);
591 return(NULL((void*)0));
592 }
593 xmlXPtrRangeCheckOrder(ret);
594 return(ret);
595}
596
597#define XML_RANGESET_DEFAULT10 10
598
599/**
600 * xmlXPtrLocationSetCreate:
601 * @val: an initial xmlXPathObjectPtr, or NULL
602 *
603 * Create a new xmlLocationSetPtr of type double and of value @val
604 *
605 * Returns the newly created object.
606 */
607xmlLocationSetPtr
608xmlXPtrLocationSetCreate(xmlXPathObjectPtr val) {
609 xmlLocationSetPtr ret;
610
611 ret = (xmlLocationSetPtr) xmlMalloc(sizeof(xmlLocationSet));
612 if (ret == NULL((void*)0)) {
613 xmlXPtrErrMemory("allocating locationset");
614 return(NULL((void*)0));
615 }
616 memset(ret, 0 , (size_t) sizeof(xmlLocationSet));
617 if (val != NULL((void*)0)) {
618 ret->locTab = (xmlXPathObjectPtr *) xmlMalloc(XML_RANGESET_DEFAULT10 *
619 sizeof(xmlXPathObjectPtr));
620 if (ret->locTab == NULL((void*)0)) {
621 xmlXPtrErrMemory("allocating locationset");
622 xmlFree(ret);
623 return(NULL((void*)0));
624 }
625 memset(ret->locTab, 0 ,
626 XML_RANGESET_DEFAULT10 * (size_t) sizeof(xmlXPathObjectPtr));
627 ret->locMax = XML_RANGESET_DEFAULT10;
628 ret->locTab[ret->locNr++] = val;
629 }
630 return(ret);
631}
632
633/**
634 * xmlXPtrLocationSetAdd:
635 * @cur: the initial range set
636 * @val: a new xmlXPathObjectPtr
637 *
638 * add a new xmlXPathObjectPtr to an existing LocationSet
639 * If the location already exist in the set @val is freed.
640 */
641void
642xmlXPtrLocationSetAdd(xmlLocationSetPtr cur, xmlXPathObjectPtr val) {
643 int i;
644
645 if ((cur == NULL((void*)0)) || (val == NULL((void*)0))) return;
646
647 /*
648 * check against doublons
649 */
650 for (i = 0;i < cur->locNr;i++) {
651 if (xmlXPtrRangesEqual(cur->locTab[i], val)) {
652 xmlXPathFreeObject(val);
653 return;
654 }
655 }
656
657 /*
658 * grow the locTab if needed
659 */
660 if (cur->locMax == 0) {
661 cur->locTab = (xmlXPathObjectPtr *) xmlMalloc(XML_RANGESET_DEFAULT10 *
662 sizeof(xmlXPathObjectPtr));
663 if (cur->locTab == NULL((void*)0)) {
664 xmlXPtrErrMemory("adding location to set");
665 return;
666 }
667 memset(cur->locTab, 0 ,
668 XML_RANGESET_DEFAULT10 * (size_t) sizeof(xmlXPathObjectPtr));
669 cur->locMax = XML_RANGESET_DEFAULT10;
670 } else if (cur->locNr == cur->locMax) {
671 xmlXPathObjectPtr *temp;
672
673 cur->locMax *= 2;
674 temp = (xmlXPathObjectPtr *) xmlRealloc(cur->locTab, cur->locMax *
675 sizeof(xmlXPathObjectPtr));
676 if (temp == NULL((void*)0)) {
677 xmlXPtrErrMemory("adding location to set");
678 return;
679 }
680 cur->locTab = temp;
681 }
682 cur->locTab[cur->locNr++] = val;
683}
684
685/**
686 * xmlXPtrLocationSetMerge:
687 * @val1: the first LocationSet
688 * @val2: the second LocationSet
689 *
690 * Merges two rangesets, all ranges from @val2 are added to @val1
691 *
692 * Returns val1 once extended or NULL in case of error.
693 */
694xmlLocationSetPtr
695xmlXPtrLocationSetMerge(xmlLocationSetPtr val1, xmlLocationSetPtr val2) {
696 int i;
697
698 if (val1 == NULL((void*)0)) return(NULL((void*)0));
699 if (val2 == NULL((void*)0)) return(val1);
700
701 /*
702 * !!!!! this can be optimized a lot, knowing that both
703 * val1 and val2 already have unicity of their values.
704 */
705
706 for (i = 0;i < val2->locNr;i++)
707 xmlXPtrLocationSetAdd(val1, val2->locTab[i]);
708
709 return(val1);
710}
711
712/**
713 * xmlXPtrLocationSetDel:
714 * @cur: the initial range set
715 * @val: an xmlXPathObjectPtr
716 *
717 * Removes an xmlXPathObjectPtr from an existing LocationSet
718 */
719void
720xmlXPtrLocationSetDel(xmlLocationSetPtr cur, xmlXPathObjectPtr val) {
721 int i;
722
723 if (cur == NULL((void*)0)) return;
724 if (val == NULL((void*)0)) return;
725
726 /*
727 * check against doublons
728 */
729 for (i = 0;i < cur->locNr;i++)
730 if (cur->locTab[i] == val) break;
731
732 if (i >= cur->locNr) {
733#ifdef DEBUG
734 xmlGenericError(*(__xmlGenericError()))(xmlGenericErrorContext(*(__xmlGenericErrorContext())),
735 "xmlXPtrLocationSetDel: Range wasn't found in RangeList\n");
736#endif
737 return;
738 }
739 cur->locNr--;
740 for (;i < cur->locNr;i++)
741 cur->locTab[i] = cur->locTab[i + 1];
742 cur->locTab[cur->locNr] = NULL((void*)0);
743}
744
745/**
746 * xmlXPtrLocationSetRemove:
747 * @cur: the initial range set
748 * @val: the index to remove
749 *
750 * Removes an entry from an existing LocationSet list.
751 */
752void
753xmlXPtrLocationSetRemove(xmlLocationSetPtr cur, int val) {
754 if (cur == NULL((void*)0)) return;
755 if (val >= cur->locNr) return;
756 cur->locNr--;
757 for (;val < cur->locNr;val++)
758 cur->locTab[val] = cur->locTab[val + 1];
759 cur->locTab[cur->locNr] = NULL((void*)0);
760}
761
762/**
763 * xmlXPtrFreeLocationSet:
764 * @obj: the xmlLocationSetPtr to free
765 *
766 * Free the LocationSet compound (not the actual ranges !).
767 */
768void
769xmlXPtrFreeLocationSet(xmlLocationSetPtr obj) {
770 int i;
771
772 if (obj == NULL((void*)0)) return;
773 if (obj->locTab != NULL((void*)0)) {
774 for (i = 0;i < obj->locNr; i++) {
775 xmlXPathFreeObject(obj->locTab[i]);
776 }
777 xmlFree(obj->locTab);
778 }
779 xmlFree(obj);
780}
781
782/**
783 * xmlXPtrNewLocationSetNodes:
784 * @start: the start NodePtr value
785 * @end: the end NodePtr value or NULL
786 *
787 * Create a new xmlXPathObjectPtr of type LocationSet and initialize
788 * it with the single range made of the two nodes @start and @end
789 *
790 * Returns the newly created object.
791 */
792xmlXPathObjectPtr
793xmlXPtrNewLocationSetNodes(xmlNodePtr start, xmlNodePtr end) {
794 xmlXPathObjectPtr ret;
795
796 ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject));
797 if (ret == NULL((void*)0)) {
798 xmlXPtrErrMemory("allocating locationset");
799 return(NULL((void*)0));
800 }
801 memset(ret, 0 , (size_t) sizeof(xmlXPathObject));
802 ret->type = XPATH_LOCATIONSET;
803 if (end == NULL((void*)0))
804 ret->user = xmlXPtrLocationSetCreate(xmlXPtrNewCollapsedRange(start));
805 else
806 ret->user = xmlXPtrLocationSetCreate(xmlXPtrNewRangeNodes(start,end));
807 return(ret);
808}
809
810/**
811 * xmlXPtrNewLocationSetNodeSet:
812 * @set: a node set
813 *
814 * Create a new xmlXPathObjectPtr of type LocationSet and initialize
815 * it with all the nodes from @set
816 *
817 * Returns the newly created object.
818 */
819xmlXPathObjectPtr
820xmlXPtrNewLocationSetNodeSet(xmlNodeSetPtr set) {
821 xmlXPathObjectPtr ret;
822
823 ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject));
824 if (ret == NULL((void*)0)) {
825 xmlXPtrErrMemory("allocating locationset");
826 return(NULL((void*)0));
827 }
828 memset(ret, 0 , (size_t) sizeof(xmlXPathObject));
829 ret->type = XPATH_LOCATIONSET;
830 if (set != NULL((void*)0)) {
831 int i;
832 xmlLocationSetPtr newset;
833
834 newset = xmlXPtrLocationSetCreate(NULL((void*)0));
835 if (newset == NULL((void*)0))
836 return(ret);
837
838 for (i = 0;i < set->nodeNr;i++)
839 xmlXPtrLocationSetAdd(newset,
840 xmlXPtrNewCollapsedRange(set->nodeTab[i]));
841
842 ret->user = (void *) newset;
843 }
844 return(ret);
845}
846
847/**
848 * xmlXPtrWrapLocationSet:
849 * @val: the LocationSet value
850 *
851 * Wrap the LocationSet @val in a new xmlXPathObjectPtr
852 *
853 * Returns the newly created object.
854 */
855xmlXPathObjectPtr
856xmlXPtrWrapLocationSet(xmlLocationSetPtr val) {
857 xmlXPathObjectPtr ret;
858
859 ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject));
860 if (ret == NULL((void*)0)) {
861 xmlXPtrErrMemory("allocating locationset");
862 return(NULL((void*)0));
863 }
864 memset(ret, 0 , (size_t) sizeof(xmlXPathObject));
865 ret->type = XPATH_LOCATIONSET;
866 ret->user = (void *) val;
867 return(ret);
868}
869
870/************************************************************************
871 * *
872 * The parser *
873 * *
874 ************************************************************************/
875
876static void xmlXPtrEvalChildSeq(xmlXPathParserContextPtr ctxt, xmlChar *name);
877
878/*
879 * Macros for accessing the content. Those should be used only by the parser,
880 * and not exported.
881 *
882 * Dirty macros, i.e. one need to make assumption on the context to use them
883 *
884 * CUR_PTR return the current pointer to the xmlChar to be parsed.
885 * CUR returns the current xmlChar value, i.e. a 8 bit value
886 * in ISO-Latin or UTF-8.
887 * This should be used internally by the parser
888 * only to compare to ASCII values otherwise it would break when
889 * running with UTF-8 encoding.
890 * NXT(n) returns the n'th next xmlChar. Same as CUR is should be used only
891 * to compare on ASCII based substring.
892 * SKIP(n) Skip n xmlChar, and must also be used only to skip ASCII defined
893 * strings within the parser.
894 * CURRENT Returns the current char value, with the full decoding of
895 * UTF-8 if we are using this mode. It returns an int.
896 * NEXT Skip to the next character, this does the proper decoding
897 * in UTF-8 mode. It also pop-up unfinished entities on the fly.
898 * It returns the pointer to the current xmlChar.
899 */
900
901#define CUR(*ctxt->cur) (*ctxt->cur)
902#define SKIP(val)ctxt->cur += (val) ctxt->cur += (val)
903#define NXT(val)ctxt->cur[(val)] ctxt->cur[(val)]
904#define CUR_PTRctxt->cur ctxt->cur
905
906#define SKIP_BLANKSwhile ((((*(ctxt->cur)) == 0x20) || ((0x9 <= (*(ctxt->
cur))) && ((*(ctxt->cur)) <= 0xa)) || ((*(ctxt->
cur)) == 0xd))) ((*ctxt->cur) ? ctxt->cur++: ctxt->cur
)
\
907 while (IS_BLANK_CH(*(ctxt->cur))(((*(ctxt->cur)) == 0x20) || ((0x9 <= (*(ctxt->cur))
) && ((*(ctxt->cur)) <= 0xa)) || ((*(ctxt->cur
)) == 0xd))
) NEXT((*ctxt->cur) ? ctxt->cur++: ctxt->cur)
908
909#define CURRENT(*ctxt->cur) (*ctxt->cur)
910#define NEXT((*ctxt->cur) ? ctxt->cur++: ctxt->cur) ((*ctxt->cur) ? ctxt->cur++: ctxt->cur)
911
912/*
913 * xmlXPtrGetChildNo:
914 * @ctxt: the XPointer Parser context
915 * @index: the child number
916 *
917 * Move the current node of the nodeset on the stack to the
918 * given child if found
919 */
920static void
921xmlXPtrGetChildNo(xmlXPathParserContextPtr ctxt, int indx) {
922 xmlNodePtr cur = NULL((void*)0);
923 xmlXPathObjectPtr obj;
924 xmlNodeSetPtr oldset;
925
926 CHECK_TYPE(XPATH_NODESET)if ((ctxt->value == ((void*)0)) || (ctxt->value->type
!= XPATH_NODESET)) { xmlXPathErr(ctxt, XPATH_INVALID_TYPE); return
; }
;
927 obj = valuePop(ctxt);
928 oldset = obj->nodesetval;
929 if ((indx <= 0) || (oldset == NULL((void*)0)) || (oldset->nodeNr != 1)) {
930 xmlXPathFreeObject(obj);
931 valuePush(ctxt, xmlXPathNewNodeSet(NULL((void*)0)));
932 return;
933 }
934 cur = xmlXPtrGetNthChild(oldset->nodeTab[0], indx);
935 if (cur == NULL((void*)0)) {
936 xmlXPathFreeObject(obj);
937 valuePush(ctxt, xmlXPathNewNodeSet(NULL((void*)0)));
938 return;
939 }
940 oldset->nodeTab[0] = cur;
941 valuePush(ctxt, obj);
942}
943
944/**
945 * xmlXPtrEvalXPtrPart:
946 * @ctxt: the XPointer Parser context
947 * @name: the preparsed Scheme for the XPtrPart
948 *
949 * XPtrPart ::= 'xpointer' '(' XPtrExpr ')'
950 * | Scheme '(' SchemeSpecificExpr ')'
951 *
952 * Scheme ::= NCName - 'xpointer' [VC: Non-XPointer schemes]
953 *
954 * SchemeSpecificExpr ::= StringWithBalancedParens
955 *
956 * StringWithBalancedParens ::=
957 * [^()]* ('(' StringWithBalancedParens ')' [^()]*)*
958 * [VC: Parenthesis escaping]
959 *
960 * XPtrExpr ::= Expr [VC: Parenthesis escaping]
961 *
962 * VC: Parenthesis escaping:
963 * The end of an XPointer part is signaled by the right parenthesis ")"
964 * character that is balanced with the left parenthesis "(" character
965 * that began the part. Any unbalanced parenthesis character inside the
966 * expression, even within literals, must be escaped with a circumflex (^)
967 * character preceding it. If the expression contains any literal
968 * occurrences of the circumflex, each must be escaped with an additional
969 * circumflex (that is, ^^). If the unescaped parentheses in the expression
970 * are not balanced, a syntax error results.
971 *
972 * Parse and evaluate an XPtrPart. Basically it generates the unescaped
973 * string and if the scheme is 'xpointer' it will call the XPath interpreter.
974 *
975 * TODO: there is no new scheme registration mechanism
976 */
977
978static void
979xmlXPtrEvalXPtrPart(xmlXPathParserContextPtr ctxt, xmlChar *name) {
980 xmlChar *buffer, *cur;
981 int len;
982 int level;
983
984 if (name == NULL((void*)0))
985 name = xmlXPathParseName(ctxt);
986 if (name == NULL((void*)0))
987 XP_ERROR(XPATH_EXPR_ERROR){ xmlXPathErr(ctxt, XPATH_EXPR_ERROR); return; };
988
989 if (CUR(*ctxt->cur) != '(')
990 XP_ERROR(XPATH_EXPR_ERROR){ xmlXPathErr(ctxt, XPATH_EXPR_ERROR); return; };
991 NEXT((*ctxt->cur) ? ctxt->cur++: ctxt->cur);
992 level = 1;
993
994 len = xmlStrlen(ctxt->cur);
995 len++;
996 buffer = (xmlChar *) xmlMallocAtomic(len * sizeof (xmlChar));
997 if (buffer == NULL((void*)0)) {
998 xmlXPtrErrMemory("allocating buffer");
999 return;
1000 }
1001
1002 cur = buffer;
1003 while (CUR(*ctxt->cur) != 0) {
1004 if (CUR(*ctxt->cur) == ')') {
1005 level--;
1006 if (level == 0) {
1007 NEXT((*ctxt->cur) ? ctxt->cur++: ctxt->cur);
1008 break;
1009 }
1010 *cur++ = CUR(*ctxt->cur);
1011 } else if (CUR(*ctxt->cur) == '(') {
1012 level++;
1013 *cur++ = CUR(*ctxt->cur);
1014 } else if (CUR(*ctxt->cur) == '^') {
1015 NEXT((*ctxt->cur) ? ctxt->cur++: ctxt->cur);
1016 if ((CUR(*ctxt->cur) == ')') || (CUR(*ctxt->cur) == '(') || (CUR(*ctxt->cur) == '^')) {
1017 *cur++ = CUR(*ctxt->cur);
1018 } else {
1019 *cur++ = '^';
1020 *cur++ = CUR(*ctxt->cur);
1021 }
1022 } else {
1023 *cur++ = CUR(*ctxt->cur);
1024 }
1025 NEXT((*ctxt->cur) ? ctxt->cur++: ctxt->cur);
1026 }
1027 *cur = 0;
1028
1029 if ((level != 0) && (CUR(*ctxt->cur) == 0)) {
1030 xmlFree(buffer);
1031 XP_ERROR(XPTR_SYNTAX_ERROR){ xmlXPathErr(ctxt, XPTR_SYNTAX_ERROR); return; };
1032 }
1033
1034 if (xmlStrEqual(name, (xmlChar *) "xpointer")) {
1035 const xmlChar *left = CUR_PTRctxt->cur;
1036
1037 CUR_PTRctxt->cur = buffer;
1038 /*
1039 * To evaluate an xpointer scheme element (4.3) we need:
1040 * context initialized to the root
1041 * context position initalized to 1
1042 * context size initialized to 1
1043 */
1044 ctxt->context->node = (xmlNodePtr)ctxt->context->doc;
1045 ctxt->context->proximityPosition = 1;
1046 ctxt->context->contextSize = 1;
1047 xmlXPathEvalExpr(ctxt);
1048 CUR_PTRctxt->cur=left;
1049 } else if (xmlStrEqual(name, (xmlChar *) "element")) {
1050 const xmlChar *left = CUR_PTRctxt->cur;
1051 xmlChar *name2;
1052
1053 CUR_PTRctxt->cur = buffer;
1054 if (buffer[0] == '/') {
1055 xmlXPathRoot(ctxt);
1056 xmlXPtrEvalChildSeq(ctxt, NULL((void*)0));
1057 } else {
1058 name2 = xmlXPathParseName(ctxt);
1059 if (name2 == NULL((void*)0)) {
1060 CUR_PTRctxt->cur = left;
1061 xmlFree(buffer);
1062 XP_ERROR(XPATH_EXPR_ERROR){ xmlXPathErr(ctxt, XPATH_EXPR_ERROR); return; };
1063 }
1064 xmlXPtrEvalChildSeq(ctxt, name2);
1065 }
1066 CUR_PTRctxt->cur = left;
1067#ifdef XPTR_XMLNS_SCHEME
1068 } else if (xmlStrEqual(name, (xmlChar *) "xmlns")) {
1069 const xmlChar *left = CUR_PTRctxt->cur;
1070 xmlChar *prefix;
1071 xmlChar *URI;
1072 xmlURIPtr value;
1073
1074 CUR_PTRctxt->cur = buffer;
1075 prefix = xmlXPathParseNCName(ctxt);
1076 if (prefix == NULL((void*)0)) {
1077 xmlFree(buffer);
1078 xmlFree(name);
1079 XP_ERROR(XPTR_SYNTAX_ERROR){ xmlXPathErr(ctxt, XPTR_SYNTAX_ERROR); return; };
1080 }
1081 SKIP_BLANKSwhile ((((*(ctxt->cur)) == 0x20) || ((0x9 <= (*(ctxt->
cur))) && ((*(ctxt->cur)) <= 0xa)) || ((*(ctxt->
cur)) == 0xd))) ((*ctxt->cur) ? ctxt->cur++: ctxt->cur
)
;
1082 if (CUR(*ctxt->cur) != '=') {
1083 xmlFree(prefix);
1084 xmlFree(buffer);
1085 xmlFree(name);
1086 XP_ERROR(XPTR_SYNTAX_ERROR){ xmlXPathErr(ctxt, XPTR_SYNTAX_ERROR); return; };
1087 }
1088 NEXT((*ctxt->cur) ? ctxt->cur++: ctxt->cur);
1089 SKIP_BLANKSwhile ((((*(ctxt->cur)) == 0x20) || ((0x9 <= (*(ctxt->
cur))) && ((*(ctxt->cur)) <= 0xa)) || ((*(ctxt->
cur)) == 0xd))) ((*ctxt->cur) ? ctxt->cur++: ctxt->cur
)
;
1090 /* @@ check escaping in the XPointer WD */
1091
1092 value = xmlParseURI((const char *)ctxt->cur);
1093 if (value == NULL((void*)0)) {
1094 xmlFree(prefix);
1095 xmlFree(buffer);
1096 xmlFree(name);
1097 XP_ERROR(XPTR_SYNTAX_ERROR){ xmlXPathErr(ctxt, XPTR_SYNTAX_ERROR); return; };
1098 }
1099 URI = xmlSaveUri(value);
1100 xmlFreeURI(value);
1101 if (URI == NULL((void*)0)) {
1102 xmlFree(prefix);
1103 xmlFree(buffer);
1104 xmlFree(name);
1105 XP_ERROR(XPATH_MEMORY_ERROR){ xmlXPathErr(ctxt, XPATH_MEMORY_ERROR); return; };
1106 }
1107
1108 xmlXPathRegisterNs(ctxt->context, prefix, URI);
1109 CUR_PTRctxt->cur = left;
1110 xmlFree(URI);
1111 xmlFree(prefix);
1112#endif /* XPTR_XMLNS_SCHEME */
1113 } else {
1114 xmlXPtrErr(ctxt, XML_XPTR_UNKNOWN_SCHEME,
1115 "unsupported scheme '%s'\n", name);
1116 }
1117 xmlFree(buffer);
1118 xmlFree(name);
1119}
1120
1121/**
1122 * xmlXPtrEvalFullXPtr:
1123 * @ctxt: the XPointer Parser context
1124 * @name: the preparsed Scheme for the first XPtrPart
1125 *
1126 * FullXPtr ::= XPtrPart (S? XPtrPart)*
1127 *
1128 * As the specs says:
1129 * -----------
1130 * When multiple XPtrParts are provided, they must be evaluated in
1131 * left-to-right order. If evaluation of one part fails, the nexti
1132 * is evaluated. The following conditions cause XPointer part failure:
1133 *
1134 * - An unknown scheme
1135 * - A scheme that does not locate any sub-resource present in the resource
1136 * - A scheme that is not applicable to the media type of the resource
1137 *
1138 * The XPointer application must consume a failed XPointer part and
1139 * attempt to evaluate the next one, if any. The result of the first
1140 * XPointer part whose evaluation succeeds is taken to be the fragment
1141 * located by the XPointer as a whole. If all the parts fail, the result
1142 * for the XPointer as a whole is a sub-resource error.
1143 * -----------
1144 *
1145 * Parse and evaluate a Full XPtr i.e. possibly a cascade of XPath based
1146 * expressions or other schemes.
1147 */
1148static void
1149xmlXPtrEvalFullXPtr(xmlXPathParserContextPtr ctxt, xmlChar *name) {
1150 if (name == NULL((void*)0))
1151 name = xmlXPathParseName(ctxt);
1152 if (name == NULL((void*)0))
1153 XP_ERROR(XPATH_EXPR_ERROR){ xmlXPathErr(ctxt, XPATH_EXPR_ERROR); return; };
1154 while (name != NULL((void*)0)) {
1155 ctxt->error = XPATH_EXPRESSION_OK;
1156 xmlXPtrEvalXPtrPart(ctxt, name);
1157
1158 /* in case of syntax error, break here */
1159 if ((ctxt->error != XPATH_EXPRESSION_OK) &&
1160 (ctxt->error != XML_XPTR_UNKNOWN_SCHEME))
1161 return;
1162
1163 /*
1164 * If the returned value is a non-empty nodeset
1165 * or location set, return here.
1166 */
1167 if (ctxt->value != NULL((void*)0)) {
1168 xmlXPathObjectPtr obj = ctxt->value;
1169
1170 switch (obj->type) {
1171 case XPATH_LOCATIONSET: {
1172 xmlLocationSetPtr loc = ctxt->value->user;
1173 if ((loc != NULL((void*)0)) && (loc->locNr > 0))
1174 return;
1175 break;
1176 }
1177 case XPATH_NODESET: {
1178 xmlNodeSetPtr loc = ctxt->value->nodesetval;
1179 if ((loc != NULL((void*)0)) && (loc->nodeNr > 0))
1180 return;
1181 break;
1182 }
1183 default:
1184 break;
1185 }
1186
1187 /*
1188 * Evaluating to improper values is equivalent to
1189 * a sub-resource error, clean-up the stack
1190 */
1191 do {
1192 obj = valuePop(ctxt);
1193 if (obj != NULL((void*)0)) {
1194 xmlXPathFreeObject(obj);
1195 }
1196 } while (obj != NULL((void*)0));
1197 }
1198
1199 /*
1200 * Is there another XPointer part.
1201 */
1202 SKIP_BLANKSwhile ((((*(ctxt->cur)) == 0x20) || ((0x9 <= (*(ctxt->
cur))) && ((*(ctxt->cur)) <= 0xa)) || ((*(ctxt->
cur)) == 0xd))) ((*ctxt->cur) ? ctxt->cur++: ctxt->cur
)
;
1203 name = xmlXPathParseName(ctxt);
1204 }
1205}
1206
1207/**
1208 * xmlXPtrEvalChildSeq:
1209 * @ctxt: the XPointer Parser context
1210 * @name: a possible ID name of the child sequence
1211 *
1212 * ChildSeq ::= '/1' ('/' [0-9]*)*
1213 * | Name ('/' [0-9]*)+
1214 *
1215 * Parse and evaluate a Child Sequence. This routine also handle the
1216 * case of a Bare Name used to get a document ID.
1217 */
1218static void
1219xmlXPtrEvalChildSeq(xmlXPathParserContextPtr ctxt, xmlChar *name) {
1220 /*
1221 * XPointer don't allow by syntax to address in mutirooted trees
1222 * this might prove useful in some cases, warn about it.
1223 */
1224 if ((name == NULL((void*)0)) && (CUR(*ctxt->cur) == '/') && (NXT(1)ctxt->cur[(1)] != '1')) {
1225 xmlXPtrErr(ctxt, XML_XPTR_CHILDSEQ_START,
1226 "warning: ChildSeq not starting by /1\n", NULL((void*)0));
1227 }
1228
1229 if (name != NULL((void*)0)) {
1230 valuePush(ctxt, xmlXPathNewString(name));
1231 xmlFree(name);
1232 xmlXPathIdFunction(ctxt, 1);
1233 CHECK_ERRORif (ctxt->error != XPATH_EXPRESSION_OK) return;
1234 }
1235
1236 while (CUR(*ctxt->cur) == '/') {
1237 int child = 0;
1238 NEXT((*ctxt->cur) ? ctxt->cur++: ctxt->cur);
1239
1240 while ((CUR(*ctxt->cur) >= '0') && (CUR(*ctxt->cur) <= '9')) {
1241 child = child * 10 + (CUR(*ctxt->cur) - '0');
1242 NEXT((*ctxt->cur) ? ctxt->cur++: ctxt->cur);
1243 }
1244 xmlXPtrGetChildNo(ctxt, child);
1245 }
1246}
1247
1248
1249/**
1250 * xmlXPtrEvalXPointer:
1251 * @ctxt: the XPointer Parser context
1252 *
1253 * XPointer ::= Name
1254 * | ChildSeq
1255 * | FullXPtr
1256 *
1257 * Parse and evaluate an XPointer
1258 */
1259static void
1260xmlXPtrEvalXPointer(xmlXPathParserContextPtr ctxt) {
1261 if (ctxt->valueTab == NULL((void*)0)) {
1262 /* Allocate the value stack */
1263 ctxt->valueTab = (xmlXPathObjectPtr *)
1264 xmlMalloc(10 * sizeof(xmlXPathObjectPtr));
1265 if (ctxt->valueTab == NULL((void*)0)) {
1266 xmlXPtrErrMemory("allocating evaluation context");
1267 return;
1268 }
1269 ctxt->valueNr = 0;
1270 ctxt->valueMax = 10;
1271 ctxt->value = NULL((void*)0);
1272 }
1273 SKIP_BLANKSwhile ((((*(ctxt->cur)) == 0x20) || ((0x9 <= (*(ctxt->
cur))) && ((*(ctxt->cur)) <= 0xa)) || ((*(ctxt->
cur)) == 0xd))) ((*ctxt->cur) ? ctxt->cur++: ctxt->cur
)
;
1274 if (CUR(*ctxt->cur) == '/') {
1275 xmlXPathRoot(ctxt);
1276 xmlXPtrEvalChildSeq(ctxt, NULL((void*)0));
1277 } else {
1278 xmlChar *name;
1279
1280 name = xmlXPathParseName(ctxt);
1281 if (name == NULL((void*)0))
1282 XP_ERROR(XPATH_EXPR_ERROR){ xmlXPathErr(ctxt, XPATH_EXPR_ERROR); return; };
1283 if (CUR(*ctxt->cur) == '(') {
1284 xmlXPtrEvalFullXPtr(ctxt, name);
1285 /* Short evaluation */
1286 return;
1287 } else {
1288 /* this handle both Bare Names and Child Sequences */
1289 xmlXPtrEvalChildSeq(ctxt, name);
1290 }
1291 }
1292 SKIP_BLANKSwhile ((((*(ctxt->cur)) == 0x20) || ((0x9 <= (*(ctxt->
cur))) && ((*(ctxt->cur)) <= 0xa)) || ((*(ctxt->
cur)) == 0xd))) ((*ctxt->cur) ? ctxt->cur++: ctxt->cur
)
;
1293 if (CUR(*ctxt->cur) != 0)
1294 XP_ERROR(XPATH_EXPR_ERROR){ xmlXPathErr(ctxt, XPATH_EXPR_ERROR); return; };
1295}
1296
1297
1298/************************************************************************
1299 * *
1300 * General routines *
1301 * *
1302 ************************************************************************/
1303
1304static
1305void xmlXPtrStringRangeFunction(xmlXPathParserContextPtr ctxt, int nargs);
1306static
1307void xmlXPtrStartPointFunction(xmlXPathParserContextPtr ctxt, int nargs);
1308static
1309void xmlXPtrEndPointFunction(xmlXPathParserContextPtr ctxt, int nargs);
1310static
1311void xmlXPtrHereFunction(xmlXPathParserContextPtr ctxt, int nargs);
1312static
1313void xmlXPtrOriginFunction(xmlXPathParserContextPtr ctxt, int nargs);
1314static
1315void xmlXPtrRangeInsideFunction(xmlXPathParserContextPtr ctxt, int nargs);
1316static
1317void xmlXPtrRangeFunction(xmlXPathParserContextPtr ctxt, int nargs);
1318
1319/**
1320 * xmlXPtrNewContext:
1321 * @doc: the XML document
1322 * @here: the node that directly contains the XPointer being evaluated or NULL
1323 * @origin: the element from which a user or program initiated traversal of
1324 * the link, or NULL.
1325 *
1326 * Create a new XPointer context
1327 *
1328 * Returns the xmlXPathContext just allocated.
1329 */
1330xmlXPathContextPtr
1331xmlXPtrNewContext(xmlDocPtr doc, xmlNodePtr here, xmlNodePtr origin) {
1332 xmlXPathContextPtr ret;
1333
1334 ret = xmlXPathNewContext(doc);
1335 if (ret == NULL((void*)0))
1336 return(ret);
1337 ret->xptr = 1;
1338 ret->here = here;
1339 ret->origin = origin;
1340
1341 xmlXPathRegisterFunc(ret, (xmlChar *)"range-to",
1342 xmlXPtrRangeToFunction);
1343 xmlXPathRegisterFunc(ret, (xmlChar *)"range",
1344 xmlXPtrRangeFunction);
1345 xmlXPathRegisterFunc(ret, (xmlChar *)"range-inside",
1346 xmlXPtrRangeInsideFunction);
1347 xmlXPathRegisterFunc(ret, (xmlChar *)"string-range",
1348 xmlXPtrStringRangeFunction);
1349 xmlXPathRegisterFunc(ret, (xmlChar *)"start-point",
1350 xmlXPtrStartPointFunction);
1351 xmlXPathRegisterFunc(ret, (xmlChar *)"end-point",
1352 xmlXPtrEndPointFunction);
1353 xmlXPathRegisterFunc(ret, (xmlChar *)"here",
1354 xmlXPtrHereFunction);
1355 xmlXPathRegisterFunc(ret, (xmlChar *)" origin",
1356 xmlXPtrOriginFunction);
1357
1358 return(ret);
1359}
1360
1361/**
1362 * xmlXPtrEval:
1363 * @str: the XPointer expression
1364 * @ctx: the XPointer context
1365 *
1366 * Evaluate the XPath Location Path in the given context.
1367 *
1368 * Returns the xmlXPathObjectPtr resulting from the evaluation or NULL.
1369 * the caller has to free the object.
1370 */
1371xmlXPathObjectPtr
1372xmlXPtrEval(const xmlChar *str, xmlXPathContextPtr ctx) {
1373 xmlXPathParserContextPtr ctxt;
1374 xmlXPathObjectPtr res = NULL((void*)0), tmp;
1375 xmlXPathObjectPtr init = NULL((void*)0);
1376 int stack = 0;
1377
1378 xmlXPathInit();
1379
1380 if ((ctx == NULL((void*)0)) || (str == NULL((void*)0)))
1381 return(NULL((void*)0));
1382
1383 ctxt = xmlXPathNewParserContext(str, ctx);
1384 ctxt->xptr = 1;
1385 xmlXPtrEvalXPointer(ctxt);
1386
1387 if ((ctxt->value != NULL((void*)0)) &&
1388 (ctxt->value->type != XPATH_NODESET) &&
1389 (ctxt->value->type != XPATH_LOCATIONSET)) {
1390 xmlXPtrErr(ctxt, XML_XPTR_EVAL_FAILED,
1391 "xmlXPtrEval: evaluation failed to return a node set\n",
1392 NULL((void*)0));
1393 } else {
1394 res = valuePop(ctxt);
1395 }
1396
1397 do {
1398 tmp = valuePop(ctxt);
1399 if (tmp != NULL((void*)0)) {
1400 if (tmp != init) {
1401 if (tmp->type == XPATH_NODESET) {
1402 /*
1403 * Evaluation may push a root nodeset which is unused
1404 */
1405 xmlNodeSetPtr set;
1406 set = tmp->nodesetval;
1407 if ((set->nodeNr != 1) ||
1408 (set->nodeTab[0] != (xmlNodePtr) ctx->doc))
1409 stack++;
1410 } else
1411 stack++;
1412 }
1413 xmlXPathFreeObject(tmp);
1414 }
1415 } while (tmp != NULL((void*)0));
1416 if (stack != 0) {
1417 xmlXPtrErr(ctxt, XML_XPTR_EXTRA_OBJECTS,
1418 "xmlXPtrEval: object(s) left on the eval stack\n",
1419 NULL((void*)0));
1420 }
1421 if (ctxt->error != XPATH_EXPRESSION_OK) {
1422 xmlXPathFreeObject(res);
1423 res = NULL((void*)0);
1424 }
1425
1426 xmlXPathFreeParserContext(ctxt);
1427 return(res);
1428}
1429
1430/**
1431 * xmlXPtrBuildRangeNodeList:
1432 * @range: a range object
1433 *
1434 * Build a node list tree copy of the range
1435 *
1436 * Returns an xmlNodePtr list or NULL.
1437 * the caller has to free the node tree.
1438 */
1439static xmlNodePtr
1440xmlXPtrBuildRangeNodeList(xmlXPathObjectPtr range) {
1441 /* pointers to generated nodes */
1442 xmlNodePtr list = NULL((void*)0), last = NULL((void*)0), parent = NULL((void*)0), tmp;
1443 /* pointers to traversal nodes */
1444 xmlNodePtr start, cur, end;
1445 int index1, index2;
1446
1447 if (range == NULL((void*)0))
1448 return(NULL((void*)0));
1449 if (range->type != XPATH_RANGE)
1450 return(NULL((void*)0));
1451 start = (xmlNodePtr) range->user;
1452
1453 if (start == NULL((void*)0))
1454 return(NULL((void*)0));
1455 end = range->user2;
1456 if (end == NULL((void*)0))
1457 return(xmlCopyNode(start, 1));
1458
1459 cur = start;
1460 index1 = range->index;
1461 index2 = range->index2;
1462 while (cur != NULL((void*)0)) {
1463 if (cur == end) {
1464 if (cur->type == XML_TEXT_NODE) {
1465 const xmlChar *content = cur->content;
1466 int len;
1467
1468 if (content == NULL((void*)0)) {
1469 tmp = xmlNewTextLen(NULL((void*)0), 0);
1470 } else {
1471 len = index2;
1472 if ((cur == start) && (index1 > 1)) {
1473 content += (index1 - 1);
1474 len -= (index1 - 1);
1475 index1 = 0;
Value stored to 'index1' is never read
1476 } else {
1477 len = index2;
1478 }
1479 tmp = xmlNewTextLen(content, len);
1480 }
1481 /* single sub text node selection */
1482 if (list == NULL((void*)0))
1483 return(tmp);
1484 /* prune and return full set */
1485 if (last != NULL((void*)0))
1486 xmlAddNextSibling(last, tmp);
1487 else
1488 xmlAddChild(parent, tmp);
1489 return(list);
1490 } else {
1491 tmp = xmlCopyNode(cur, 0);
1492 if (list == NULL((void*)0))
1493 list = tmp;
1494 else {
1495 if (last != NULL((void*)0))
1496 xmlAddNextSibling(last, tmp);
1497 else
1498 xmlAddChild(parent, tmp);
1499 }
1500 last = NULL((void*)0);
1501 parent = tmp;
1502
1503 if (index2 > 1) {
1504 end = xmlXPtrGetNthChild(cur, index2 - 1);
1505 index2 = 0;
1506 }
1507 if ((cur == start) && (index1 > 1)) {
1508 cur = xmlXPtrGetNthChild(cur, index1 - 1);
1509 index1 = 0;
1510 } else {
1511 cur = cur->children;
1512 }
1513 /*
1514 * Now gather the remaining nodes from cur to end
1515 */
1516 continue; /* while */
1517 }
1518 } else if ((cur == start) &&
1519 (list == NULL((void*)0)) /* looks superfluous but ... */ ) {
1520 if ((cur->type == XML_TEXT_NODE) ||
1521 (cur->type == XML_CDATA_SECTION_NODE)) {
1522 const xmlChar *content = cur->content;
1523
1524 if (content == NULL((void*)0)) {
1525 tmp = xmlNewTextLen(NULL((void*)0), 0);
1526 } else {
1527 if (index1 > 1) {
1528 content += (index1 - 1);
1529 }
1530 tmp = xmlNewText(content);
1531 }
1532 last = list = tmp;
1533 } else {
1534 if ((cur == start) && (index1 > 1)) {
1535 tmp = xmlCopyNode(cur, 0);
1536 list = tmp;
1537 parent = tmp;
1538 last = NULL((void*)0);
1539 cur = xmlXPtrGetNthChild(cur, index1 - 1);
1540 index1 = 0;
1541 /*
1542 * Now gather the remaining nodes from cur to end
1543 */
1544 continue; /* while */
1545 }
1546 tmp = xmlCopyNode(cur, 1);
1547 list = tmp;
1548 parent = NULL((void*)0);
1549 last = tmp;
1550 }
1551 } else {
1552 tmp = NULL((void*)0);
1553 switch (cur->type) {
1554 case XML_DTD_NODE:
1555 case XML_ELEMENT_DECL:
1556 case XML_ATTRIBUTE_DECL:
1557 case XML_ENTITY_NODE:
1558 /* Do not copy DTD informations */
1559 break;
1560 case XML_ENTITY_DECL:
1561 TODO(*(__xmlGenericError()))((*(__xmlGenericErrorContext())), "Unimplemented block at %s:%d\n"
, "xpointer.c", 1561);
/* handle crossing entities -> stack needed */
1562 break;
1563 case XML_XINCLUDE_START:
1564 case XML_XINCLUDE_END:
1565 /* don't consider it part of the tree content */
1566 break;
1567 case XML_ATTRIBUTE_NODE:
1568 /* Humm, should not happen ! */
1569 STRANGE(*(__xmlGenericError()))((*(__xmlGenericErrorContext())), "Internal error at %s:%d\n"
, "xpointer.c", 1569);
1570 break;
1571 default:
1572 tmp = xmlCopyNode(cur, 1);
1573 break;
1574 }
1575 if (tmp != NULL((void*)0)) {
1576 if ((list == NULL((void*)0)) || ((last == NULL((void*)0)) && (parent == NULL((void*)0)))) {
1577 STRANGE(*(__xmlGenericError()))((*(__xmlGenericErrorContext())), "Internal error at %s:%d\n"
, "xpointer.c", 1577);
1578 return(NULL((void*)0));
1579 }
1580 if (last != NULL((void*)0))
1581 xmlAddNextSibling(last, tmp);
1582 else {
1583 xmlAddChild(parent, tmp);
1584 last = tmp;
1585 }
1586 }
1587 }
1588 /*
1589 * Skip to next node in document order
1590 */
1591 if ((list == NULL((void*)0)) || ((last == NULL((void*)0)) && (parent == NULL((void*)0)))) {
1592 STRANGE(*(__xmlGenericError()))((*(__xmlGenericErrorContext())), "Internal error at %s:%d\n"
, "xpointer.c", 1592);
1593 return(NULL((void*)0));
1594 }
1595 cur = xmlXPtrAdvanceNode(cur, NULL((void*)0));
1596 }
1597 return(list);
1598}
1599
1600/**
1601 * xmlXPtrBuildNodeList:
1602 * @obj: the XPointer result from the evaluation.
1603 *
1604 * Build a node list tree copy of the XPointer result.
1605 * This will drop Attributes and Namespace declarations.
1606 *
1607 * Returns an xmlNodePtr list or NULL.
1608 * the caller has to free the node tree.
1609 */
1610xmlNodePtr
1611xmlXPtrBuildNodeList(xmlXPathObjectPtr obj) {
1612 xmlNodePtr list = NULL((void*)0), last = NULL((void*)0);
1613 int i;
1614
1615 if (obj == NULL((void*)0))
1616 return(NULL((void*)0));
1617 switch (obj->type) {
1618 case XPATH_NODESET: {
1619 xmlNodeSetPtr set = obj->nodesetval;
1620 if (set == NULL((void*)0))
1621 return(NULL((void*)0));
1622 for (i = 0;i < set->nodeNr;i++) {
1623 if (set->nodeTab[i] == NULL((void*)0))
1624 continue;
1625 switch (set->nodeTab[i]->type) {
1626 case XML_TEXT_NODE:
1627 case XML_CDATA_SECTION_NODE:
1628 case XML_ELEMENT_NODE:
1629 case XML_ENTITY_REF_NODE:
1630 case XML_ENTITY_NODE:
1631 case XML_PI_NODE:
1632 case XML_COMMENT_NODE:
1633 case XML_DOCUMENT_NODE:
1634 case XML_HTML_DOCUMENT_NODE:
1635#ifdef LIBXML_DOCB_ENABLED
1636 case XML_DOCB_DOCUMENT_NODE:
1637#endif
1638 case XML_XINCLUDE_START:
1639 case XML_XINCLUDE_END:
1640 break;
1641 case XML_ATTRIBUTE_NODE:
1642 case XML_NAMESPACE_DECL:
1643 case XML_DOCUMENT_TYPE_NODE:
1644 case XML_DOCUMENT_FRAG_NODE:
1645 case XML_NOTATION_NODE:
1646 case XML_DTD_NODE:
1647 case XML_ELEMENT_DECL:
1648 case XML_ATTRIBUTE_DECL:
1649 case XML_ENTITY_DECL:
1650 continue; /* for */
1651 }
1652 if (last == NULL((void*)0))
1653 list = last = xmlCopyNode(set->nodeTab[i], 1);
1654 else {
1655 xmlAddNextSibling(last, xmlCopyNode(set->nodeTab[i], 1));
1656 if (last->next != NULL((void*)0))
1657 last = last->next;
1658 }
1659 }
1660 break;
1661 }
1662 case XPATH_LOCATIONSET: {
1663 xmlLocationSetPtr set = (xmlLocationSetPtr) obj->user;
1664 if (set == NULL((void*)0))
1665 return(NULL((void*)0));
1666 for (i = 0;i < set->locNr;i++) {
1667 if (last == NULL((void*)0))
1668 list = last = xmlXPtrBuildNodeList(set->locTab[i]);
1669 else
1670 xmlAddNextSibling(last,
1671 xmlXPtrBuildNodeList(set->locTab[i]));
1672 if (last != NULL((void*)0)) {
1673 while (last->next != NULL((void*)0))
1674 last = last->next;
1675 }
1676 }
1677 break;
1678 }
1679 case XPATH_RANGE:
1680 return(xmlXPtrBuildRangeNodeList(obj));
1681 case XPATH_POINT:
1682 return(xmlCopyNode(obj->user, 0));
1683 default:
1684 break;
1685 }
1686 return(list);
1687}
1688
1689/************************************************************************
1690 * *
1691 * XPointer functions *
1692 * *
1693 ************************************************************************/
1694
1695/**
1696 * xmlXPtrNbLocChildren:
1697 * @node: an xmlNodePtr
1698 *
1699 * Count the number of location children of @node or the length of the
1700 * string value in case of text/PI/Comments nodes
1701 *
1702 * Returns the number of location children
1703 */
1704static int
1705xmlXPtrNbLocChildren(xmlNodePtr node) {
1706 int ret = 0;
1707 if (node == NULL((void*)0))
1708 return(-1);
1709 switch (node->type) {
1710 case XML_HTML_DOCUMENT_NODE:
1711 case XML_DOCUMENT_NODE:
1712 case XML_ELEMENT_NODE:
1713 node = node->children;
1714 while (node != NULL((void*)0)) {
1715 if (node->type == XML_ELEMENT_NODE)
1716 ret++;
1717 node = node->next;
1718 }
1719 break;
1720 case XML_ATTRIBUTE_NODE:
1721 return(-1);
1722
1723 case XML_PI_NODE:
1724 case XML_COMMENT_NODE:
1725 case XML_TEXT_NODE:
1726 case XML_CDATA_SECTION_NODE:
1727 case XML_ENTITY_REF_NODE:
1728 ret = xmlStrlen(node->content);
1729 break;
1730 default:
1731 return(-1);
1732 }
1733 return(ret);
1734}
1735
1736/**
1737 * xmlXPtrHereFunction:
1738 * @ctxt: the XPointer Parser context
1739 * @nargs: the number of args
1740 *
1741 * Function implementing here() operation
1742 * as described in 5.4.3
1743 */
1744static void
1745xmlXPtrHereFunction(xmlXPathParserContextPtr ctxt, int nargs) {
1746 CHECK_ARITY(0)if (ctxt == ((void*)0)) return; if (nargs != (0)) { xmlXPathErr
(ctxt, XPATH_INVALID_ARITY); return; };
;
1747
1748 if (ctxt->context->here == NULL((void*)0))
1749 XP_ERROR(XPTR_SYNTAX_ERROR){ xmlXPathErr(ctxt, XPTR_SYNTAX_ERROR); return; };
1750
1751 valuePush(ctxt, xmlXPtrNewLocationSetNodes(ctxt->context->here, NULL((void*)0)));
1752}
1753
1754/**
1755 * xmlXPtrOriginFunction:
1756 * @ctxt: the XPointer Parser context
1757 * @nargs: the number of args
1758 *
1759 * Function implementing origin() operation
1760 * as described in 5.4.3
1761 */
1762static void
1763xmlXPtrOriginFunction(xmlXPathParserContextPtr ctxt, int nargs) {
1764 CHECK_ARITY(0)if (ctxt == ((void*)0)) return; if (nargs != (0)) { xmlXPathErr
(ctxt, XPATH_INVALID_ARITY); return; };
;
1765
1766 if (ctxt->context->origin == NULL((void*)0))
1767 XP_ERROR(XPTR_SYNTAX_ERROR){ xmlXPathErr(ctxt, XPTR_SYNTAX_ERROR); return; };
1768
1769 valuePush(ctxt, xmlXPtrNewLocationSetNodes(ctxt->context->origin, NULL((void*)0)));
1770}
1771
1772/**
1773 * xmlXPtrStartPointFunction:
1774 * @ctxt: the XPointer Parser context
1775 * @nargs: the number of args
1776 *
1777 * Function implementing start-point() operation
1778 * as described in 5.4.3
1779 * ----------------
1780 * location-set start-point(location-set)
1781 *
1782 * For each location x in the argument location-set, start-point adds a
1783 * location of type point to the result location-set. That point represents
1784 * the start point of location x and is determined by the following rules:
1785 *
1786 * - If x is of type point, the start point is x.
1787 * - If x is of type range, the start point is the start point of x.
1788 * - If x is of type root, element, text, comment, or processing instruction,
1789 * - the container node of the start point is x and the index is 0.
1790 * - If x is of type attribute or namespace, the function must signal a
1791 * syntax error.
1792 * ----------------
1793 *
1794 */
1795static void
1796xmlXPtrStartPointFunction(xmlXPathParserContextPtr ctxt, int nargs) {
1797 xmlXPathObjectPtr tmp, obj, point;
1798 xmlLocationSetPtr newset = NULL((void*)0);
1799 xmlLocationSetPtr oldset = NULL((void*)0);
1800
1801 CHECK_ARITY(1)if (ctxt == ((void*)0)) return; if (nargs != (1)) { xmlXPathErr
(ctxt, XPATH_INVALID_ARITY); return; };
;
1802 if ((ctxt->value == NULL((void*)0)) ||
1803 ((ctxt->value->type != XPATH_LOCATIONSET) &&
1804 (ctxt->value->type != XPATH_NODESET)))
1805 XP_ERROR(XPATH_INVALID_TYPE){ xmlXPathErr(ctxt, XPATH_INVALID_TYPE); return; }
1806
1807 obj = valuePop(ctxt);
1808 if (obj->type == XPATH_NODESET) {
1809 /*
1810 * First convert to a location set
1811 */
1812 tmp = xmlXPtrNewLocationSetNodeSet(obj->nodesetval);
1813 xmlXPathFreeObject(obj);
1814 obj = tmp;
1815 }
1816
1817 newset = xmlXPtrLocationSetCreate(NULL((void*)0));
1818 if (newset == NULL((void*)0)) {
1819 xmlXPathFreeObject(obj);
1820 XP_ERROR(XPATH_MEMORY_ERROR){ xmlXPathErr(ctxt, XPATH_MEMORY_ERROR); return; };
1821 }
1822 oldset = (xmlLocationSetPtr) obj->user;
1823 if (oldset != NULL((void*)0)) {
1824 int i;
1825
1826 for (i = 0; i < oldset->locNr; i++) {
1827 tmp = oldset->locTab[i];
1828 if (tmp == NULL((void*)0))
1829 continue;
1830 point = NULL((void*)0);
1831 switch (tmp->type) {
1832 case XPATH_POINT:
1833 point = xmlXPtrNewPoint(tmp->user, tmp->index);
1834 break;
1835 case XPATH_RANGE: {
1836 xmlNodePtr node = tmp->user;
1837 if (node != NULL((void*)0)) {
1838 if (node->type == XML_ATTRIBUTE_NODE) {
1839 /* TODO: Namespace Nodes ??? */
1840 xmlXPathFreeObject(obj);
1841 xmlXPtrFreeLocationSet(newset);
1842 XP_ERROR(XPTR_SYNTAX_ERROR){ xmlXPathErr(ctxt, XPTR_SYNTAX_ERROR); return; };
1843 }
1844 point = xmlXPtrNewPoint(node, tmp->index);
1845 }
1846 break;
1847 }
1848 default:
1849 /*** Should we raise an error ?
1850 xmlXPathFreeObject(obj);
1851 xmlXPathFreeObject(newset);
1852 XP_ERROR(XPATH_INVALID_TYPE)
1853 ***/
1854 break;
1855 }
1856 if (point != NULL((void*)0))
1857 xmlXPtrLocationSetAdd(newset, point);
1858 }
1859 }
1860 xmlXPathFreeObject(obj);
1861 valuePush(ctxt, xmlXPtrWrapLocationSet(newset));
1862}
1863
1864/**
1865 * xmlXPtrEndPointFunction:
1866 * @ctxt: the XPointer Parser context
1867 * @nargs: the number of args
1868 *
1869 * Function implementing end-point() operation
1870 * as described in 5.4.3
1871 * ----------------------------
1872 * location-set end-point(location-set)
1873 *
1874 * For each location x in the argument location-set, end-point adds a
1875 * location of type point to the result location-set. That point represents
1876 * the end point of location x and is determined by the following rules:
1877 *
1878 * - If x is of type point, the resulting point is x.
1879 * - If x is of type range, the resulting point is the end point of x.
1880 * - If x is of type root or element, the container node of the resulting
1881 * point is x and the index is the number of location children of x.
1882 * - If x is of type text, comment, or processing instruction, the container
1883 * node of the resulting point is x and the index is the length of the
1884 * string-value of x.
1885 * - If x is of type attribute or namespace, the function must signal a
1886 * syntax error.
1887 * ----------------------------
1888 */
1889static void
1890xmlXPtrEndPointFunction(xmlXPathParserContextPtr ctxt, int nargs) {
1891 xmlXPathObjectPtr tmp, obj, point;
1892 xmlLocationSetPtr newset = NULL((void*)0);
1893 xmlLocationSetPtr oldset = NULL((void*)0);
1894
1895 CHECK_ARITY(1)if (ctxt == ((void*)0)) return; if (nargs != (1)) { xmlXPathErr
(ctxt, XPATH_INVALID_ARITY); return; };
;
1896 if ((ctxt->value == NULL((void*)0)) ||
1897 ((ctxt->value->type != XPATH_LOCATIONSET) &&
1898 (ctxt->value->type != XPATH_NODESET)))
1899 XP_ERROR(XPATH_INVALID_TYPE){ xmlXPathErr(ctxt, XPATH_INVALID_TYPE); return; }
1900
1901 obj = valuePop(ctxt);
1902 if (obj->type == XPATH_NODESET) {
1903 /*
1904 * First convert to a location set
1905 */
1906 tmp = xmlXPtrNewLocationSetNodeSet(obj->nodesetval);
1907 xmlXPathFreeObject(obj);
1908 obj = tmp;
1909 }
1910
1911 newset = xmlXPtrLocationSetCreate(NULL((void*)0));
1912 oldset = (xmlLocationSetPtr) obj->user;
1913 if (oldset != NULL((void*)0)) {
1914 int i;
1915
1916 for (i = 0; i < oldset->locNr; i++) {
1917 tmp = oldset->locTab[i];
1918 if (tmp == NULL((void*)0))
1919 continue;
1920 point = NULL((void*)0);
1921 switch (tmp->type) {
1922 case XPATH_POINT:
1923 point = xmlXPtrNewPoint(tmp->user, tmp->index);
1924 break;
1925 case XPATH_RANGE: {
1926 xmlNodePtr node = tmp->user2;
1927 if (node != NULL((void*)0)) {
1928 if (node->type == XML_ATTRIBUTE_NODE) {
1929 /* TODO: Namespace Nodes ??? */
1930 xmlXPathFreeObject(obj);
1931 xmlXPtrFreeLocationSet(newset);
1932 XP_ERROR(XPTR_SYNTAX_ERROR){ xmlXPathErr(ctxt, XPTR_SYNTAX_ERROR); return; };
1933 }
1934 point = xmlXPtrNewPoint(node, tmp->index2);
1935 } else if (tmp->user == NULL((void*)0)) {
1936 point = xmlXPtrNewPoint(node,
1937 xmlXPtrNbLocChildren(node));
1938 }
1939 break;
1940 }
1941 default:
1942 /*** Should we raise an error ?
1943 xmlXPathFreeObject(obj);
1944 xmlXPathFreeObject(newset);
1945 XP_ERROR(XPATH_INVALID_TYPE)
1946 ***/
1947 break;
1948 }
1949 if (point != NULL((void*)0))
1950 xmlXPtrLocationSetAdd(newset, point);
1951 }
1952 }
1953 xmlXPathFreeObject(obj);
1954 valuePush(ctxt, xmlXPtrWrapLocationSet(newset));
1955}
1956
1957
1958/**
1959 * xmlXPtrCoveringRange:
1960 * @ctxt: the XPointer Parser context
1961 * @loc: the location for which the covering range must be computed
1962 *
1963 * A covering range is a range that wholly encompasses a location
1964 * Section 5.3.3. Covering Ranges for All Location Types
1965 * http://www.w3.org/TR/xptr#N2267
1966 *
1967 * Returns a new location or NULL in case of error
1968 */
1969static xmlXPathObjectPtr
1970xmlXPtrCoveringRange(xmlXPathParserContextPtr ctxt, xmlXPathObjectPtr loc) {
1971 if (loc == NULL((void*)0))
1972 return(NULL((void*)0));
1973 if ((ctxt == NULL((void*)0)) || (ctxt->context == NULL((void*)0)) ||
1974 (ctxt->context->doc == NULL((void*)0)))
1975 return(NULL((void*)0));
1976 switch (loc->type) {
1977 case XPATH_POINT:
1978 return(xmlXPtrNewRange(loc->user, loc->index,
1979 loc->user, loc->index));
1980 case XPATH_RANGE:
1981 if (loc->user2 != NULL((void*)0)) {
1982 return(xmlXPtrNewRange(loc->user, loc->index,
1983 loc->user2, loc->index2));
1984 } else {
1985 xmlNodePtr node = (xmlNodePtr) loc->user;
1986 if (node == (xmlNodePtr) ctxt->context->doc) {
1987 return(xmlXPtrNewRange(node, 0, node,
1988 xmlXPtrGetArity(node)));
1989 } else {
1990 switch (node->type) {
1991 case XML_ATTRIBUTE_NODE:
1992 /* !!! our model is slightly different than XPath */
1993 return(xmlXPtrNewRange(node, 0, node,
1994 xmlXPtrGetArity(node)));
1995 case XML_ELEMENT_NODE:
1996 case XML_TEXT_NODE:
1997 case XML_CDATA_SECTION_NODE:
1998 case XML_ENTITY_REF_NODE:
1999 case XML_PI_NODE:
2000 case XML_COMMENT_NODE:
2001 case XML_DOCUMENT_NODE:
2002 case XML_NOTATION_NODE:
2003 case XML_HTML_DOCUMENT_NODE: {
2004 int indx = xmlXPtrGetIndex(node);
2005
2006 node = node->parent;
2007 return(xmlXPtrNewRange(node, indx - 1,
2008 node, indx + 1));
2009 }
2010 default:
2011 return(NULL((void*)0));
2012 }
2013 }
2014 }
2015 default:
2016 TODO(*(__xmlGenericError()))((*(__xmlGenericErrorContext())), "Unimplemented block at %s:%d\n"
, "xpointer.c", 2016);
/* missed one case ??? */
2017 }
2018 return(NULL((void*)0));
2019}
2020
2021/**
2022 * xmlXPtrRangeFunction:
2023 * @ctxt: the XPointer Parser context
2024 * @nargs: the number of args
2025 *
2026 * Function implementing the range() function 5.4.3
2027 * location-set range(location-set )
2028 *
2029 * The range function returns ranges covering the locations in
2030 * the argument location-set. For each location x in the argument
2031 * location-set, a range location representing the covering range of
2032 * x is added to the result location-set.
2033 */
2034static void
2035xmlXPtrRangeFunction(xmlXPathParserContextPtr ctxt, int nargs) {
2036 int i;
2037 xmlXPathObjectPtr set;
2038 xmlLocationSetPtr oldset;
2039 xmlLocationSetPtr newset;
2040
2041 CHECK_ARITY(1)if (ctxt == ((void*)0)) return; if (nargs != (1)) { xmlXPathErr
(ctxt, XPATH_INVALID_ARITY); return; };
;
2042 if ((ctxt->value == NULL((void*)0)) ||
2043 ((ctxt->value->type != XPATH_LOCATIONSET) &&
2044 (ctxt->value->type != XPATH_NODESET)))
2045 XP_ERROR(XPATH_INVALID_TYPE){ xmlXPathErr(ctxt, XPATH_INVALID_TYPE); return; }
2046
2047 set = valuePop(ctxt);
2048 if (set->type == XPATH_NODESET) {
2049 xmlXPathObjectPtr tmp;
2050
2051 /*
2052 * First convert to a location set
2053 */
2054 tmp = xmlXPtrNewLocationSetNodeSet(set->nodesetval);
2055 xmlXPathFreeObject(set);
2056 set = tmp;
2057 }
2058 oldset = (xmlLocationSetPtr) set->user;
2059
2060 /*
2061 * The loop is to compute the covering range for each item and add it
2062 */
2063 newset = xmlXPtrLocationSetCreate(NULL((void*)0));
2064 for (i = 0;i < oldset->locNr;i++) {
2065 xmlXPtrLocationSetAdd(newset,
2066 xmlXPtrCoveringRange(ctxt, oldset->locTab[i]));
2067 }
2068
2069 /*
2070 * Save the new value and cleanup
2071 */
2072 valuePush(ctxt, xmlXPtrWrapLocationSet(newset));
2073 xmlXPathFreeObject(set);
2074}
2075
2076/**
2077 * xmlXPtrInsideRange:
2078 * @ctxt: the XPointer Parser context
2079 * @loc: the location for which the inside range must be computed
2080 *
2081 * A inside range is a range described in the range-inside() description
2082 *
2083 * Returns a new location or NULL in case of error
2084 */
2085static xmlXPathObjectPtr
2086xmlXPtrInsideRange(xmlXPathParserContextPtr ctxt, xmlXPathObjectPtr loc) {
2087 if (loc == NULL((void*)0))
2088 return(NULL((void*)0));
2089 if ((ctxt == NULL((void*)0)) || (ctxt->context == NULL((void*)0)) ||
2090 (ctxt->context->doc == NULL((void*)0)))
2091 return(NULL((void*)0));
2092 switch (loc->type) {
2093 case XPATH_POINT: {
2094 xmlNodePtr node = (xmlNodePtr) loc->user;
2095 switch (node->type) {
2096 case XML_PI_NODE:
2097 case XML_COMMENT_NODE:
2098 case XML_TEXT_NODE:
2099 case XML_CDATA_SECTION_NODE: {
2100 if (node->content == NULL((void*)0)) {
2101 return(xmlXPtrNewRange(node, 0, node, 0));
2102 } else {
2103 return(xmlXPtrNewRange(node, 0, node,
2104 xmlStrlen(node->content)));
2105 }
2106 }
2107 case XML_ATTRIBUTE_NODE:
2108 case XML_ELEMENT_NODE:
2109 case XML_ENTITY_REF_NODE:
2110 case XML_DOCUMENT_NODE:
2111 case XML_NOTATION_NODE:
2112 case XML_HTML_DOCUMENT_NODE: {
2113 return(xmlXPtrNewRange(node, 0, node,
2114 xmlXPtrGetArity(node)));
2115 }
2116 default:
2117 break;
2118 }
2119 return(NULL((void*)0));
2120 }
2121 case XPATH_RANGE: {
2122 xmlNodePtr node = (xmlNodePtr) loc->user;
2123 if (loc->user2 != NULL((void*)0)) {
2124 return(xmlXPtrNewRange(node, loc->index,
2125 loc->user2, loc->index2));
2126 } else {
2127 switch (node->type) {
2128 case XML_PI_NODE:
2129 case XML_COMMENT_NODE:
2130 case XML_TEXT_NODE:
2131 case XML_CDATA_SECTION_NODE: {
2132 if (node->content == NULL((void*)0)) {
2133 return(xmlXPtrNewRange(node, 0, node, 0));
2134 } else {
2135 return(xmlXPtrNewRange(node, 0, node,
2136 xmlStrlen(node->content)));
2137 }
2138 }
2139 case XML_ATTRIBUTE_NODE:
2140 case XML_ELEMENT_NODE:
2141 case XML_ENTITY_REF_NODE:
2142 case XML_DOCUMENT_NODE:
2143 case XML_NOTATION_NODE:
2144 case XML_HTML_DOCUMENT_NODE: {
2145 return(xmlXPtrNewRange(node, 0, node,
2146 xmlXPtrGetArity(node)));
2147 }
2148 default:
2149 break;
2150 }
2151 return(NULL((void*)0));
2152 }
2153 }
2154 default:
2155 TODO(*(__xmlGenericError()))((*(__xmlGenericErrorContext())), "Unimplemented block at %s:%d\n"
, "xpointer.c", 2155);
/* missed one case ??? */
2156 }
2157 return(NULL((void*)0));
2158}
2159
2160/**
2161 * xmlXPtrRangeInsideFunction:
2162 * @ctxt: the XPointer Parser context
2163 * @nargs: the number of args
2164 *
2165 * Function implementing the range-inside() function 5.4.3
2166 * location-set range-inside(location-set )
2167 *
2168 * The range-inside function returns ranges covering the contents of
2169 * the locations in the argument location-set. For each location x in
2170 * the argument location-set, a range location is added to the result
2171 * location-set. If x is a range location, then x is added to the
2172 * result location-set. If x is not a range location, then x is used
2173 * as the container location of the start and end points of the range
2174 * location to be added; the index of the start point of the range is
2175 * zero; if the end point is a character point then its index is the
2176 * length of the string-value of x, and otherwise is the number of
2177 * location children of x.
2178 *
2179 */
2180static void
2181xmlXPtrRangeInsideFunction(xmlXPathParserContextPtr ctxt, int nargs) {
2182 int i;
2183 xmlXPathObjectPtr set;
2184 xmlLocationSetPtr oldset;
2185 xmlLocationSetPtr newset;
2186
2187 CHECK_ARITY(1)if (ctxt == ((void*)0)) return; if (nargs != (1)) { xmlXPathErr
(ctxt, XPATH_INVALID_ARITY); return; };
;
2188 if ((ctxt->value == NULL((void*)0)) ||
2189 ((ctxt->value->type != XPATH_LOCATIONSET) &&
2190 (ctxt->value->type != XPATH_NODESET)))
2191 XP_ERROR(XPATH_INVALID_TYPE){ xmlXPathErr(ctxt, XPATH_INVALID_TYPE); return; }
2192
2193 set = valuePop(ctxt);
2194 if (set->type == XPATH_NODESET) {
2195 xmlXPathObjectPtr tmp;
2196
2197 /*
2198 * First convert to a location set
2199 */
2200 tmp = xmlXPtrNewLocationSetNodeSet(set->nodesetval);
2201 xmlXPathFreeObject(set);
2202 set = tmp;
2203 }
2204 oldset = (xmlLocationSetPtr) set->user;
2205
2206 /*
2207 * The loop is to compute the covering range for each item and add it
2208 */
2209 newset = xmlXPtrLocationSetCreate(NULL((void*)0));
2210 for (i = 0;i < oldset->locNr;i++) {
2211 xmlXPtrLocationSetAdd(newset,
2212 xmlXPtrInsideRange(ctxt, oldset->locTab[i]));
2213 }
2214
2215 /*
2216 * Save the new value and cleanup
2217 */
2218 valuePush(ctxt, xmlXPtrWrapLocationSet(newset));
2219 xmlXPathFreeObject(set);
2220}
2221
2222/**
2223 * xmlXPtrRangeToFunction:
2224 * @ctxt: the XPointer Parser context
2225 * @nargs: the number of args
2226 *
2227 * Implement the range-to() XPointer function
2228 */
2229void
2230xmlXPtrRangeToFunction(xmlXPathParserContextPtr ctxt, int nargs) {
2231 xmlXPathObjectPtr range;
2232 const xmlChar *cur;
2233 xmlXPathObjectPtr res, obj;
2234 xmlXPathObjectPtr tmp;
2235 xmlLocationSetPtr newset = NULL((void*)0);
2236 xmlNodeSetPtr oldset;
2237 int i;
2238
2239 if (ctxt == NULL((void*)0)) return;
2240 CHECK_ARITY(1)if (ctxt == ((void*)0)) return; if (nargs != (1)) { xmlXPathErr
(ctxt, XPATH_INVALID_ARITY); return; };
;
2241 /*
2242 * Save the expression pointer since we will have to evaluate
2243 * it multiple times. Initialize the new set.
2244 */
2245 CHECK_TYPE(XPATH_NODESET)if ((ctxt->value == ((void*)0)) || (ctxt->value->type
!= XPATH_NODESET)) { xmlXPathErr(ctxt, XPATH_INVALID_TYPE); return
; }
;
2246 obj = valuePop(ctxt);
2247 oldset = obj->nodesetval;
2248 ctxt->context->node = NULL((void*)0);
2249
2250 cur = ctxt->cur;
2251 newset = xmlXPtrLocationSetCreate(NULL((void*)0));
2252
2253 for (i = 0; i < oldset->nodeNr; i++) {
2254 ctxt->cur = cur;
2255
2256 /*
2257 * Run the evaluation with a node list made of a single item
2258 * in the nodeset.
2259 */
2260 ctxt->context->node = oldset->nodeTab[i];
2261 tmp = xmlXPathNewNodeSet(ctxt->context->node);
2262 valuePush(ctxt, tmp);
2263
2264 xmlXPathEvalExpr(ctxt);
2265 CHECK_ERRORif (ctxt->error != XPATH_EXPRESSION_OK) return;
2266
2267 /*
2268 * The result of the evaluation need to be tested to
2269 * decided whether the filter succeeded or not
2270 */
2271 res = valuePop(ctxt);
2272 range = xmlXPtrNewRangeNodeObject(oldset->nodeTab[i], res);
2273 if (range != NULL((void*)0)) {
2274 xmlXPtrLocationSetAdd(newset, range);
2275 }
2276
2277 /*
2278 * Cleanup
2279 */
2280 if (res != NULL((void*)0))
2281 xmlXPathFreeObject(res);
2282 if (ctxt->value == tmp) {
2283 res = valuePop(ctxt);
2284 xmlXPathFreeObject(res);
2285 }
2286
2287 ctxt->context->node = NULL((void*)0);
2288 }
2289
2290 /*
2291 * The result is used as the new evaluation set.
2292 */
2293 xmlXPathFreeObject(obj);
2294 ctxt->context->node = NULL((void*)0);
2295 ctxt->context->contextSize = -1;
2296 ctxt->context->proximityPosition = -1;
2297 valuePush(ctxt, xmlXPtrWrapLocationSet(newset));
2298}
2299
2300/**
2301 * xmlXPtrAdvanceNode:
2302 * @cur: the node
2303 * @level: incremented/decremented to show level in tree
2304 *
2305 * Advance to the next element or text node in document order
2306 * TODO: add a stack for entering/exiting entities
2307 *
2308 * Returns -1 in case of failure, 0 otherwise
2309 */
2310xmlNodePtr
2311xmlXPtrAdvanceNode(xmlNodePtr cur, int *level) {
2312next:
2313 if (cur == NULL((void*)0))
2314 return(NULL((void*)0));
2315 if (cur->children != NULL((void*)0)) {
2316 cur = cur->children ;
2317 if (level != NULL((void*)0))
2318 (*level)++;
2319 goto found;
2320 }
2321skip: /* This label should only be needed if something is wrong! */
2322 if (cur->next != NULL((void*)0)) {
2323 cur = cur->next;
2324 goto found;
2325 }
2326 do {
2327 cur = cur->parent;
2328 if (level != NULL((void*)0))
2329 (*level)--;
2330 if (cur == NULL((void*)0)) return(NULL((void*)0));
2331 if (cur->next != NULL((void*)0)) {
2332 cur = cur->next;
2333 goto found;
2334 }
2335 } while (cur != NULL((void*)0));
2336
2337found:
2338 if ((cur->type != XML_ELEMENT_NODE) &&
2339 (cur->type != XML_TEXT_NODE) &&
2340 (cur->type != XML_DOCUMENT_NODE) &&
2341 (cur->type != XML_HTML_DOCUMENT_NODE) &&
2342 (cur->type != XML_CDATA_SECTION_NODE)) {
2343 if (cur->type == XML_ENTITY_REF_NODE) { /* Shouldn't happen */
2344 TODO(*(__xmlGenericError()))((*(__xmlGenericErrorContext())), "Unimplemented block at %s:%d\n"
, "xpointer.c", 2344);
2345 goto skip;
2346 }
2347 goto next;
2348 }
2349 return(cur);
2350}
2351
2352/**
2353 * xmlXPtrAdvanceChar:
2354 * @node: the node
2355 * @indx: the indx
2356 * @bytes: the number of bytes
2357 *
2358 * Advance a point of the associated number of bytes (not UTF8 chars)
2359 *
2360 * Returns -1 in case of failure, 0 otherwise
2361 */
2362static int
2363xmlXPtrAdvanceChar(xmlNodePtr *node, int *indx, int bytes) {
2364 xmlNodePtr cur;
2365 int pos;
2366 int len;
2367
2368 if ((node == NULL((void*)0)) || (indx == NULL((void*)0)))
2369 return(-1);
2370 cur = *node;
2371 if (cur == NULL((void*)0))
2372 return(-1);
2373 pos = *indx;
2374
2375 while (bytes >= 0) {
2376 /*
2377 * First position to the beginning of the first text node
2378 * corresponding to this point
2379 */
2380 while ((cur != NULL((void*)0)) &&
2381 ((cur->type == XML_ELEMENT_NODE) ||
2382 (cur->type == XML_DOCUMENT_NODE) ||
2383 (cur->type == XML_HTML_DOCUMENT_NODE))) {
2384 if (pos > 0) {
2385 cur = xmlXPtrGetNthChild(cur, pos);
2386 pos = 0;
2387 } else {
2388 cur = xmlXPtrAdvanceNode(cur, NULL((void*)0));
2389 pos = 0;
2390 }
2391 }
2392
2393 if (cur == NULL((void*)0)) {
2394 *node = NULL((void*)0);
2395 *indx = 0;
2396 return(-1);
2397 }
2398
2399 /*
2400 * if there is no move needed return the current value.
2401 */
2402 if (pos == 0) pos = 1;
2403 if (bytes == 0) {
2404 *node = cur;
2405 *indx = pos;
2406 return(0);
2407 }
2408 /*
2409 * We should have a text (or cdata) node ...
2410 */
2411 len = 0;
2412 if ((cur->type != XML_ELEMENT_NODE) &&
2413 (cur->content != NULL((void*)0))) {
2414 len = xmlStrlen(cur->content);
2415 }
2416 if (pos > len) {
2417 /* Strange, the indx in the text node is greater than it's len */
2418 STRANGE(*(__xmlGenericError()))((*(__xmlGenericErrorContext())), "Internal error at %s:%d\n"
, "xpointer.c", 2418);
2419 pos = len;
2420 }
2421 if (pos + bytes >= len) {
2422 bytes -= (len - pos);
2423 cur = xmlXPtrAdvanceNode(cur, NULL((void*)0));
2424 pos = 0;
2425 } else if (pos + bytes < len) {
2426 pos += bytes;
2427 *node = cur;
2428 *indx = pos;
2429 return(0);
2430 }
2431 }
2432 return(-1);
2433}
2434
2435/**
2436 * xmlXPtrMatchString:
2437 * @string: the string to search
2438 * @start: the start textnode
2439 * @startindex: the start index
2440 * @end: the end textnode IN/OUT
2441 * @endindex: the end index IN/OUT
2442 *
2443 * Check whether the document contains @string at the position
2444 * (@start, @startindex) and limited by the (@end, @endindex) point
2445 *
2446 * Returns -1 in case of failure, 0 if not found, 1 if found in which case
2447 * (@start, @startindex) will indicate the position of the beginning
2448 * of the range and (@end, @endindex) will indicate the end
2449 * of the range
2450 */
2451static int
2452xmlXPtrMatchString(const xmlChar *string, xmlNodePtr start, int startindex,
2453 xmlNodePtr *end, int *endindex) {
2454 xmlNodePtr cur;
2455 int pos; /* 0 based */
2456 int len; /* in bytes */
2457 int stringlen; /* in bytes */
2458 int match;
2459
2460 if (string == NULL((void*)0))
2461 return(-1);
2462 if (start == NULL((void*)0))
2463 return(-1);
2464 if ((end == NULL((void*)0)) || (endindex == NULL((void*)0)))
2465 return(-1);
2466 cur = start;
2467 if (cur == NULL((void*)0))
2468 return(-1);
2469 pos = startindex - 1;
2470 stringlen = xmlStrlen(string);
2471
2472 while (stringlen > 0) {
2473 if ((cur == *end) && (pos + stringlen > *endindex))
2474 return(0);
2475
2476 if ((cur->type != XML_ELEMENT_NODE) && (cur->content != NULL((void*)0))) {
2477 len = xmlStrlen(cur->content);
2478 if (len >= pos + stringlen) {
2479 match = (!xmlStrncmp(&cur->content[pos], string, stringlen));
2480 if (match) {
2481#ifdef DEBUG_RANGES
2482 xmlGenericError(*(__xmlGenericError()))(xmlGenericErrorContext(*(__xmlGenericErrorContext())),
2483 "found range %d bytes at index %d of ->",
2484 stringlen, pos + 1);
2485 xmlDebugDumpString(stdoutstdout, cur->content);
2486 xmlGenericError(*(__xmlGenericError()))(xmlGenericErrorContext(*(__xmlGenericErrorContext())), "\n");
2487#endif
2488 *end = cur;
2489 *endindex = pos + stringlen;
2490 return(1);
2491 } else {
2492 return(0);
2493 }
2494 } else {
2495 int sub = len - pos;
2496 match = (!xmlStrncmp(&cur->content[pos], string, sub));
2497 if (match) {
2498#ifdef DEBUG_RANGES
2499 xmlGenericError(*(__xmlGenericError()))(xmlGenericErrorContext(*(__xmlGenericErrorContext())),
2500 "found subrange %d bytes at index %d of ->",
2501 sub, pos + 1);
2502 xmlDebugDumpString(stdoutstdout, cur->content);
2503 xmlGenericError(*(__xmlGenericError()))(xmlGenericErrorContext(*(__xmlGenericErrorContext())), "\n");
2504#endif
2505 string = &string[sub];
2506 stringlen -= sub;
2507 } else {
2508 return(0);
2509 }
2510 }
2511 }
2512 cur = xmlXPtrAdvanceNode(cur, NULL((void*)0));
2513 if (cur == NULL((void*)0))
2514 return(0);
2515 pos = 0;
2516 }
2517 return(1);
2518}
2519
2520/**
2521 * xmlXPtrSearchString:
2522 * @string: the string to search
2523 * @start: the start textnode IN/OUT
2524 * @startindex: the start index IN/OUT
2525 * @end: the end textnode
2526 * @endindex: the end index
2527 *
2528 * Search the next occurrence of @string within the document content
2529 * until the (@end, @endindex) point is reached
2530 *
2531 * Returns -1 in case of failure, 0 if not found, 1 if found in which case
2532 * (@start, @startindex) will indicate the position of the beginning
2533 * of the range and (@end, @endindex) will indicate the end
2534 * of the range
2535 */
2536static int
2537xmlXPtrSearchString(const xmlChar *string, xmlNodePtr *start, int *startindex,
2538 xmlNodePtr *end, int *endindex) {
2539 xmlNodePtr cur;
2540 const xmlChar *str;
2541 int pos; /* 0 based */
2542 int len; /* in bytes */
2543 xmlChar first;
2544
2545 if (string == NULL((void*)0))
2546 return(-1);
2547 if ((start == NULL((void*)0)) || (startindex == NULL((void*)0)))
2548 return(-1);
2549 if ((end == NULL((void*)0)) || (endindex == NULL((void*)0)))
2550 return(-1);
2551 cur = *start;
2552 if (cur == NULL((void*)0))
2553 return(-1);
2554 pos = *startindex - 1;
2555 first = string[0];
2556
2557 while (cur != NULL((void*)0)) {
2558 if ((cur->type != XML_ELEMENT_NODE) && (cur->content != NULL((void*)0))) {
2559 len = xmlStrlen(cur->content);
2560 while (pos <= len) {
2561 if (first != 0) {
2562 str = xmlStrchr(&cur->content[pos], first);
2563 if (str != NULL((void*)0)) {
2564 pos = (str - (xmlChar *)(cur->content));
2565#ifdef DEBUG_RANGES
2566 xmlGenericError(*(__xmlGenericError()))(xmlGenericErrorContext(*(__xmlGenericErrorContext())),
2567 "found '%c' at index %d of ->",
2568 first, pos + 1);
2569 xmlDebugDumpString(stdoutstdout, cur->content);
2570 xmlGenericError(*(__xmlGenericError()))(xmlGenericErrorContext(*(__xmlGenericErrorContext())), "\n");
2571#endif
2572 if (xmlXPtrMatchString(string, cur, pos + 1,
2573 end, endindex)) {
2574 *start = cur;
2575 *startindex = pos + 1;
2576 return(1);
2577 }
2578 pos++;
2579 } else {
2580 pos = len + 1;
2581 }
2582 } else {
2583 /*
2584 * An empty string is considered to match before each
2585 * character of the string-value and after the final
2586 * character.
2587 */
2588#ifdef DEBUG_RANGES
2589 xmlGenericError(*(__xmlGenericError()))(xmlGenericErrorContext(*(__xmlGenericErrorContext())),
2590 "found '' at index %d of ->",
2591 pos + 1);
2592 xmlDebugDumpString(stdoutstdout, cur->content);
2593 xmlGenericError(*(__xmlGenericError()))(xmlGenericErrorContext(*(__xmlGenericErrorContext())), "\n");
2594#endif
2595 *start = cur;
2596 *startindex = pos + 1;
2597 *end = cur;
2598 *endindex = pos + 1;
2599 return(1);
2600 }
2601 }
2602 }
2603 if ((cur == *end) && (pos >= *endindex))
2604 return(0);
2605 cur = xmlXPtrAdvanceNode(cur, NULL((void*)0));
2606 if (cur == NULL((void*)0))
2607 return(0);
2608 pos = 1;
2609 }
2610 return(0);
2611}
2612
2613/**
2614 * xmlXPtrGetLastChar:
2615 * @node: the node
2616 * @index: the index
2617 *
2618 * Computes the point coordinates of the last char of this point
2619 *
2620 * Returns -1 in case of failure, 0 otherwise
2621 */
2622static int
2623xmlXPtrGetLastChar(xmlNodePtr *node, int *indx) {
2624 xmlNodePtr cur;
2625 int pos, len = 0;
2626
2627 if ((node == NULL((void*)0)) || (indx == NULL((void*)0)))
2628 return(-1);
2629 cur = *node;
2630 pos = *indx;
2631
2632 if (cur == NULL((void*)0))
2633 return(-1);
2634
2635 if ((cur->type == XML_ELEMENT_NODE) ||
2636 (cur->type == XML_DOCUMENT_NODE) ||
2637 (cur->type == XML_HTML_DOCUMENT_NODE)) {
2638 if (pos > 0) {
2639 cur = xmlXPtrGetNthChild(cur, pos);
2640 pos = 0;
2641 }
2642 }
2643 while (cur != NULL((void*)0)) {
2644 if (cur->last != NULL((void*)0))
2645 cur = cur->last;
2646 else if ((cur->type != XML_ELEMENT_NODE) &&
2647 (cur->content != NULL((void*)0))) {
2648 len = xmlStrlen(cur->content);
2649 break;
2650 } else {
2651 return(-1);
2652 }
2653 }
2654 if (cur == NULL((void*)0))
2655 return(-1);
2656 *node = cur;
2657 *indx = len;
2658 return(0);
2659}
2660
2661/**
2662 * xmlXPtrGetStartPoint:
2663 * @obj: an range
2664 * @node: the resulting node
2665 * @indx: the resulting index
2666 *
2667 * read the object and return the start point coordinates.
2668 *
2669 * Returns -1 in case of failure, 0 otherwise
2670 */
2671static int
2672xmlXPtrGetStartPoint(xmlXPathObjectPtr obj, xmlNodePtr *node, int *indx) {
2673 if ((obj == NULL((void*)0)) || (node == NULL((void*)0)) || (indx == NULL((void*)0)))
2674 return(-1);
2675
2676 switch (obj->type) {
2677 case XPATH_POINT:
2678 *node = obj->user;
2679 if (obj->index <= 0)
2680 *indx = 0;
2681 else
2682 *indx = obj->index;
2683 return(0);
2684 case XPATH_RANGE:
2685 *node = obj->user;
2686 if (obj->index <= 0)
2687 *indx = 0;
2688 else
2689 *indx = obj->index;
2690 return(0);
2691 default:
2692 break;
2693 }
2694 return(-1);
2695}
2696
2697/**
2698 * xmlXPtrGetEndPoint:
2699 * @obj: an range
2700 * @node: the resulting node
2701 * @indx: the resulting indx
2702 *
2703 * read the object and return the end point coordinates.
2704 *
2705 * Returns -1 in case of failure, 0 otherwise
2706 */
2707static int
2708xmlXPtrGetEndPoint(xmlXPathObjectPtr obj, xmlNodePtr *node, int *indx) {
2709 if ((obj == NULL((void*)0)) || (node == NULL((void*)0)) || (indx == NULL((void*)0)))
2710 return(-1);
2711
2712 switch (obj->type) {
2713 case XPATH_POINT:
2714 *node = obj->user;
2715 if (obj->index <= 0)
2716 *indx = 0;
2717 else
2718 *indx = obj->index;
2719 return(0);
2720 case XPATH_RANGE:
2721 *node = obj->user;
2722 if (obj->index <= 0)
2723 *indx = 0;
2724 else
2725 *indx = obj->index;
2726 return(0);
2727 default:
2728 break;
2729 }
2730 return(-1);
2731}
2732
2733/**
2734 * xmlXPtrStringRangeFunction:
2735 * @ctxt: the XPointer Parser context
2736 * @nargs: the number of args
2737 *
2738 * Function implementing the string-range() function
2739 * range as described in 5.4.2
2740 *
2741 * ------------------------------
2742 * [Definition: For each location in the location-set argument,
2743 * string-range returns a set of string ranges, a set of substrings in a
2744 * string. Specifically, the string-value of the location is searched for
2745 * substrings that match the string argument, and the resulting location-set
2746 * will contain a range location for each non-overlapping match.]
2747 * An empty string is considered to match before each character of the
2748 * string-value and after the final character. Whitespace in a string
2749 * is matched literally, with no normalization except that provided by
2750 * XML for line ends. The third argument gives the position of the first
2751 * character to be in the resulting range, relative to the start of the
2752 * match. The default value is 1, which makes the range start immediately
2753 * before the first character of the matched string. The fourth argument
2754 * gives the number of characters in the range; the default is that the
2755 * range extends to the end of the matched string.
2756 *
2757 * Element boundaries, as well as entire embedded nodes such as processing
2758 * instructions and comments, are ignored as defined in [XPath].
2759 *
2760 * If the string in the second argument is not found in the string-value
2761 * of the location, or if a value in the third or fourth argument indicates
2762 * a string that is beyond the beginning or end of the document, the
2763 * expression fails.
2764 *
2765 * The points of the range-locations in the returned location-set will
2766 * all be character points.
2767 * ------------------------------
2768 */
2769static void
2770xmlXPtrStringRangeFunction(xmlXPathParserContextPtr ctxt, int nargs) {
2771 int i, startindex, endindex = 0, fendindex;
2772 xmlNodePtr start, end = 0, fend;
2773 xmlXPathObjectPtr set;
2774 xmlLocationSetPtr oldset;
2775 xmlLocationSetPtr newset;
2776 xmlXPathObjectPtr string;
2777 xmlXPathObjectPtr position = NULL((void*)0);
2778 xmlXPathObjectPtr number = NULL((void*)0);
2779 int found, pos = 0, num = 0;
2780
2781 /*
2782 * Grab the arguments
2783 */
2784 if ((nargs < 2) || (nargs > 4))
2785 XP_ERROR(XPATH_INVALID_ARITY){ xmlXPathErr(ctxt, XPATH_INVALID_ARITY); return; };
2786
2787 if (nargs >= 4) {
2788 CHECK_TYPE(XPATH_NUMBER)if ((ctxt->value == ((void*)0)) || (ctxt->value->type
!= XPATH_NUMBER)) { xmlXPathErr(ctxt, XPATH_INVALID_TYPE); return
; }
;
2789 number = valuePop(ctxt);
2790 if (number != NULL((void*)0))
2791 num = (int) number->floatval;
2792 }
2793 if (nargs >= 3) {
2794 CHECK_TYPE(XPATH_NUMBER)if ((ctxt->value == ((void*)0)) || (ctxt->value->type
!= XPATH_NUMBER)) { xmlXPathErr(ctxt, XPATH_INVALID_TYPE); return
; }
;
2795 position = valuePop(ctxt);
2796 if (position != NULL((void*)0))
2797 pos = (int) position->floatval;
2798 }
2799 CHECK_TYPE(XPATH_STRING)if ((ctxt->value == ((void*)0)) || (ctxt->value->type
!= XPATH_STRING)) { xmlXPathErr(ctxt, XPATH_INVALID_TYPE); return
; }
;
2800 string = valuePop(ctxt);
2801 if ((ctxt->value == NULL((void*)0)) ||
2802 ((ctxt->value->type != XPATH_LOCATIONSET) &&
2803 (ctxt->value->type != XPATH_NODESET)))
2804 XP_ERROR(XPATH_INVALID_TYPE){ xmlXPathErr(ctxt, XPATH_INVALID_TYPE); return; }
2805
2806 set = valuePop(ctxt);
2807 newset = xmlXPtrLocationSetCreate(NULL((void*)0));
2808 if (set->nodesetval == NULL((void*)0)) {
2809 goto error;
2810 }
2811 if (set->type == XPATH_NODESET) {
2812 xmlXPathObjectPtr tmp;
2813
2814 /*
2815 * First convert to a location set
2816 */
2817 tmp = xmlXPtrNewLocationSetNodeSet(set->nodesetval);
2818 xmlXPathFreeObject(set);
2819 set = tmp;
2820 }
2821 oldset = (xmlLocationSetPtr) set->user;
2822
2823 /*
2824 * The loop is to search for each element in the location set
2825 * the list of location set corresponding to that search
2826 */
2827 for (i = 0;i < oldset->locNr;i++) {
2828#ifdef DEBUG_RANGES
2829 xmlXPathDebugDumpObject(stdoutstdout, oldset->locTab[i], 0);
2830#endif
2831
2832 xmlXPtrGetStartPoint(oldset->locTab[i], &start, &startindex);
2833 xmlXPtrGetEndPoint(oldset->locTab[i], &end, &endindex);
2834 xmlXPtrAdvanceChar(&start, &startindex, 0);
2835 xmlXPtrGetLastChar(&end, &endindex);
2836
2837#ifdef DEBUG_RANGES
2838 xmlGenericError(*(__xmlGenericError()))(xmlGenericErrorContext(*(__xmlGenericErrorContext())),
2839 "from index %d of ->", startindex);
2840 xmlDebugDumpString(stdoutstdout, start->content);
2841 xmlGenericError(*(__xmlGenericError()))(xmlGenericErrorContext(*(__xmlGenericErrorContext())), "\n");
2842 xmlGenericError(*(__xmlGenericError()))(xmlGenericErrorContext(*(__xmlGenericErrorContext())),
2843 "to index %d of ->", endindex);
2844 xmlDebugDumpString(stdoutstdout, end->content);
2845 xmlGenericError(*(__xmlGenericError()))(xmlGenericErrorContext(*(__xmlGenericErrorContext())), "\n");
2846#endif
2847 do {
2848 fend = end;
2849 fendindex = endindex;
2850 found = xmlXPtrSearchString(string->stringval, &start, &startindex,
2851 &fend, &fendindex);
2852 if (found == 1) {
2853 if (position == NULL((void*)0)) {
2854 xmlXPtrLocationSetAdd(newset,
2855 xmlXPtrNewRange(start, startindex, fend, fendindex));
2856 } else if (xmlXPtrAdvanceChar(&start, &startindex,
2857 pos - 1) == 0) {
2858 if ((number != NULL((void*)0)) && (num > 0)) {
2859 int rindx;
2860 xmlNodePtr rend;
2861 rend = start;
2862 rindx = startindex - 1;
2863 if (xmlXPtrAdvanceChar(&rend, &rindx,
2864 num) == 0) {
2865 xmlXPtrLocationSetAdd(newset,
2866 xmlXPtrNewRange(start, startindex,
2867 rend, rindx));
2868 }
2869 } else if ((number != NULL((void*)0)) && (num <= 0)) {
2870 xmlXPtrLocationSetAdd(newset,
2871 xmlXPtrNewRange(start, startindex,
2872 start, startindex));
2873 } else {
2874 xmlXPtrLocationSetAdd(newset,
2875 xmlXPtrNewRange(start, startindex,
2876 fend, fendindex));
2877 }
2878 }
2879 start = fend;
2880 startindex = fendindex;
2881 if (string->stringval[0] == 0)
2882 startindex++;
2883 }
2884 } while (found == 1);
2885 }
2886
2887 /*
2888 * Save the new value and cleanup
2889 */
2890error:
2891 valuePush(ctxt, xmlXPtrWrapLocationSet(newset));
2892 xmlXPathFreeObject(set);
2893 xmlXPathFreeObject(string);
2894 if (position) xmlXPathFreeObject(position);
2895 if (number) xmlXPathFreeObject(number);
2896}
2897
2898/**
2899 * xmlXPtrEvalRangePredicate:
2900 * @ctxt: the XPointer Parser context
2901 *
2902 * [8] Predicate ::= '[' PredicateExpr ']'
2903 * [9] PredicateExpr ::= Expr
2904 *
2905 * Evaluate a predicate as in xmlXPathEvalPredicate() but for
2906 * a Location Set instead of a node set
2907 */
2908void
2909xmlXPtrEvalRangePredicate(xmlXPathParserContextPtr ctxt) {
2910 const xmlChar *cur;
2911 xmlXPathObjectPtr res;
2912 xmlXPathObjectPtr obj, tmp;
2913 xmlLocationSetPtr newset = NULL((void*)0);
2914 xmlLocationSetPtr oldset;
2915 int i;
2916
2917 if (ctxt == NULL((void*)0)) return;
2918
2919 SKIP_BLANKSwhile ((((*(ctxt->cur)) == 0x20) || ((0x9 <= (*(ctxt->
cur))) && ((*(ctxt->cur)) <= 0xa)) || ((*(ctxt->
cur)) == 0xd))) ((*ctxt->cur) ? ctxt->cur++: ctxt->cur
)
;
2920 if (CUR(*ctxt->cur) != '[') {
2921 XP_ERROR(XPATH_INVALID_PREDICATE_ERROR){ xmlXPathErr(ctxt, XPATH_INVALID_PREDICATE_ERROR); return; };
2922 }
2923 NEXT((*ctxt->cur) ? ctxt->cur++: ctxt->cur);
2924 SKIP_BLANKSwhile ((((*(ctxt->cur)) == 0x20) || ((0x9 <= (*(ctxt->
cur))) && ((*(ctxt->cur)) <= 0xa)) || ((*(ctxt->
cur)) == 0xd))) ((*ctxt->cur) ? ctxt->cur++: ctxt->cur
)
;
2925
2926 /*
2927 * Extract the old set, and then evaluate the result of the
2928 * expression for all the element in the set. use it to grow
2929 * up a new set.
2930 */
2931 CHECK_TYPE(XPATH_LOCATIONSET)if ((ctxt->value == ((void*)0)) || (ctxt->value->type
!= XPATH_LOCATIONSET)) { xmlXPathErr(ctxt, XPATH_INVALID_TYPE
); return; }
;
2932 obj = valuePop(ctxt);
2933 oldset = obj->user;
2934 ctxt->context->node = NULL((void*)0);
2935
2936 if ((oldset == NULL((void*)0)) || (oldset->locNr == 0)) {
2937 ctxt->context->contextSize = 0;
2938 ctxt->context->proximityPosition = 0;
2939 xmlXPathEvalExpr(ctxt);
2940 res = valuePop(ctxt);
2941 if (res != NULL((void*)0))
2942 xmlXPathFreeObject(res);
2943 valuePush(ctxt, obj);
2944 CHECK_ERRORif (ctxt->error != XPATH_EXPRESSION_OK) return;
2945 } else {
2946 /*
2947 * Save the expression pointer since we will have to evaluate
2948 * it multiple times. Initialize the new set.
2949 */
2950 cur = ctxt->cur;
2951 newset = xmlXPtrLocationSetCreate(NULL((void*)0));
2952
2953 for (i = 0; i < oldset->locNr; i++) {
2954 ctxt->cur = cur;
2955
2956 /*
2957 * Run the evaluation with a node list made of a single item
2958 * in the nodeset.
2959 */
2960 ctxt->context->node = oldset->locTab[i]->user;
2961 tmp = xmlXPathNewNodeSet(ctxt->context->node);
2962 valuePush(ctxt, tmp);
2963 ctxt->context->contextSize = oldset->locNr;
2964 ctxt->context->proximityPosition = i + 1;
2965
2966 xmlXPathEvalExpr(ctxt);
2967 CHECK_ERRORif (ctxt->error != XPATH_EXPRESSION_OK) return;
2968
2969 /*
2970 * The result of the evaluation need to be tested to
2971 * decided whether the filter succeeded or not
2972 */
2973 res = valuePop(ctxt);
2974 if (xmlXPathEvaluatePredicateResult(ctxt, res)) {
2975 xmlXPtrLocationSetAdd(newset,
2976 xmlXPathObjectCopy(oldset->locTab[i]));
2977 }
2978
2979 /*
2980 * Cleanup
2981 */
2982 if (res != NULL((void*)0))
2983 xmlXPathFreeObject(res);
2984 if (ctxt->value == tmp) {
2985 res = valuePop(ctxt);
2986 xmlXPathFreeObject(res);
2987 }
2988
2989 ctxt->context->node = NULL((void*)0);
2990 }
2991
2992 /*
2993 * The result is used as the new evaluation set.
2994 */
2995 xmlXPathFreeObject(obj);
2996 ctxt->context->node = NULL((void*)0);
2997 ctxt->context->contextSize = -1;
2998 ctxt->context->proximityPosition = -1;
2999 valuePush(ctxt, xmlXPtrWrapLocationSet(newset));
3000 }
3001 if (CUR(*ctxt->cur) != ']') {
3002 XP_ERROR(XPATH_INVALID_PREDICATE_ERROR){ xmlXPathErr(ctxt, XPATH_INVALID_PREDICATE_ERROR); return; };
3003 }
3004
3005 NEXT((*ctxt->cur) ? ctxt->cur++: ctxt->cur);
3006 SKIP_BLANKSwhile ((((*(ctxt->cur)) == 0x20) || ((0x9 <= (*(ctxt->
cur))) && ((*(ctxt->cur)) <= 0xa)) || ((*(ctxt->
cur)) == 0xd))) ((*ctxt->cur) ? ctxt->cur++: ctxt->cur
)
;
3007}
3008
3009#define bottom_xpointer
3010#include "elfgcchack.h"
3011#endif
3012