Coverage Report

Created: 2025-07-11 06:13

/src/libxml2/xpointer.c
Line
Count
Source (jump to first uncovered line)
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
 * Author: Daniel Veillard
15
 */
16
17
/* To avoid EBCDIC trouble when parsing on zOS */
18
#if defined(__MVS__)
19
#pragma convert("ISO8859-1")
20
#endif
21
22
#define IN_LIBXML
23
#include "libxml.h"
24
25
/*
26
 * TODO: better handling of error cases, the full expression should
27
 *       be parsed beforehand instead of a progressive evaluation
28
 * TODO: Access into entities references are not supported now ...
29
 *       need a start to be able to pop out of entities refs since
30
 *       parent is the entity declaration, not the ref.
31
 */
32
33
#include <string.h>
34
#include <libxml/xpointer.h>
35
#include <libxml/xmlmemory.h>
36
#include <libxml/parserInternals.h>
37
#include <libxml/uri.h>
38
#include <libxml/xpath.h>
39
#include <libxml/xpathInternals.h>
40
#include <libxml/xmlerror.h>
41
42
#ifdef LIBXML_XPTR_ENABLED
43
44
/* Add support of the xmlns() xpointer scheme to initialize the namespaces */
45
#define XPTR_XMLNS_SCHEME
46
47
#include "private/error.h"
48
#include "private/xpath.h"
49
50
/************************************************************************
51
 *                  *
52
 *    Some factorized error routines        *
53
 *                  *
54
 ************************************************************************/
55
56
/**
57
 * Handle an XPointer error
58
 *
59
 * @param ctxt  an XPTR evaluation context
60
 * @param code  error code
61
 * @param msg  error message
62
 * @param extra  extra information
63
 */
64
static void LIBXML_ATTR_FORMAT(3,0)
65
xmlXPtrErr(xmlXPathParserContextPtr ctxt, int code,
66
           const char * msg, const xmlChar *extra)
67
11.7k
{
68
11.7k
    xmlStructuredErrorFunc serror = NULL;
69
11.7k
    void *data = NULL;
70
11.7k
    xmlNodePtr node = NULL;
71
11.7k
    int res;
72
73
11.7k
    if (ctxt == NULL)
74
0
        return;
75
    /* Only report the first error */
76
11.7k
    if (ctxt->error != 0)
77
2
        return;
78
79
11.7k
    ctxt->error = code;
80
81
11.7k
    if (ctxt->context != NULL) {
82
11.7k
        xmlErrorPtr err = &ctxt->context->lastError;
83
84
        /* cleanup current last error */
85
11.7k
        xmlResetError(err);
86
87
11.7k
        err->domain = XML_FROM_XPOINTER;
88
11.7k
        err->code = code;
89
11.7k
        err->level = XML_ERR_ERROR;
90
11.7k
        err->str1 = (char *) xmlStrdup(ctxt->base);
91
11.7k
        if (err->str1 == NULL) {
92
5
            xmlXPathPErrMemory(ctxt);
93
5
            return;
94
5
        }
95
11.7k
        err->int1 = ctxt->cur - ctxt->base;
96
11.7k
        err->node = ctxt->context->debugNode;
97
98
11.7k
        serror = ctxt->context->error;
99
11.7k
        data = ctxt->context->userData;
100
11.7k
        node = ctxt->context->debugNode;
101
11.7k
    }
102
103
11.7k
    res = xmlRaiseError(serror, NULL, data, NULL, node,
104
11.7k
                        XML_FROM_XPOINTER, code, XML_ERR_ERROR, NULL, 0,
105
11.7k
                        (const char *) extra, (const char *) ctxt->base,
106
11.7k
                        NULL, ctxt->cur - ctxt->base, 0,
107
11.7k
                        msg, extra);
108
11.7k
    if (res < 0)
109
3
        xmlXPathPErrMemory(ctxt);
110
11.7k
}
111
112
/************************************************************************
113
 *                  *
114
 *    A few helper functions for child sequences    *
115
 *                  *
116
 ************************************************************************/
117
118
/**
119
 * @param cur  the node
120
 * @param no  the child number
121
 * @returns the `no`'th element child of `cur` or NULL
122
 */
123
static xmlNodePtr
124
2.00k
xmlXPtrGetNthChild(xmlNodePtr cur, int no) {
125
2.00k
    int i;
126
2.00k
    if ((cur == NULL) || (cur->type == XML_NAMESPACE_DECL))
127
0
  return(cur);
128
2.00k
    cur = cur->children;
129
3.83k
    for (i = 0;i <= no;cur = cur->next) {
130
3.83k
  if (cur == NULL)
131
1.73k
      return(cur);
132
2.09k
  if ((cur->type == XML_ELEMENT_NODE) ||
133
2.09k
      (cur->type == XML_DOCUMENT_NODE) ||
134
2.09k
      (cur->type == XML_HTML_DOCUMENT_NODE)) {
135
2.00k
      i++;
136
2.00k
      if (i == no)
137
266
    break;
138
2.00k
  }
139
2.09k
    }
140
266
    return(cur);
141
2.00k
}
142
143
/************************************************************************
144
 *                  *
145
 *      The parser          *
146
 *                  *
147
 ************************************************************************/
148
149
static void xmlXPtrEvalChildSeq(xmlXPathParserContextPtr ctxt, xmlChar *name);
150
151
/*
152
 * Macros for accessing the content. Those should be used only by the parser,
153
 * and not exported.
154
 *
155
 * Dirty macros, i.e. one need to make assumption on the context to use them
156
 *
157
 *   CUR     returns the current xmlChar value, i.e. a 8 bit value
158
 *           in ISO-Latin or UTF-8.
159
 *           This should be used internally by the parser
160
 *           only to compare to ASCII values otherwise it would break when
161
 *           running with UTF-8 encoding.
162
 *   NXT(n)  returns the n'th next xmlChar. Same as CUR is should be used only
163
 *           to compare on ASCII based substring.
164
 *   SKIP(n) Skip n xmlChar, and must also be used only to skip ASCII defined
165
 *           strings within the parser.
166
 *   CURRENT Returns the current char value, with the full decoding of
167
 *           UTF-8 if we are using this mode. It returns an int.
168
 *   NEXT    Skip to the next character, this does the proper decoding
169
 *           in UTF-8 mode. It also pop-up unfinished entities on the fly.
170
 *           It returns the pointer to the current xmlChar.
171
 */
172
173
3.88M
#define CUR (*ctxt->cur)
174
#define SKIP(val) ctxt->cur += (val)
175
7.33k
#define NXT(val) ctxt->cur[(val)]
176
177
#define SKIP_BLANKS             \
178
78.5k
    while (IS_BLANK_CH(*(ctxt->cur))) NEXT
179
180
#define CURRENT (*ctxt->cur)
181
966k
#define NEXT ((*ctxt->cur) ?  ctxt->cur++: ctxt->cur)
182
183
/*
184
 * @param ctxt  the XPointer Parser context
185
 * @param index  the child number
186
 *
187
 * Move the current node of the nodeset on the stack to the
188
 * given child if found
189
 */
190
static void
191
6.45k
xmlXPtrGetChildNo(xmlXPathParserContextPtr ctxt, int indx) {
192
6.45k
    xmlNodePtr cur = NULL;
193
6.45k
    xmlXPathObjectPtr obj;
194
6.45k
    xmlNodeSetPtr oldset;
195
196
6.45k
    CHECK_TYPE(XPATH_NODESET);
197
6.18k
    obj = xmlXPathValuePop(ctxt);
198
6.18k
    oldset = obj->nodesetval;
199
6.18k
    if ((indx <= 0) || (oldset == NULL) || (oldset->nodeNr != 1)) {
200
4.18k
  xmlXPathFreeObject(obj);
201
4.18k
  xmlXPathValuePush(ctxt, xmlXPathNewNodeSet(NULL));
202
4.18k
  return;
203
4.18k
    }
204
2.00k
    cur = xmlXPtrGetNthChild(oldset->nodeTab[0], indx);
205
2.00k
    if (cur == NULL) {
206
1.73k
  xmlXPathFreeObject(obj);
207
1.73k
  xmlXPathValuePush(ctxt, xmlXPathNewNodeSet(NULL));
208
1.73k
  return;
209
1.73k
    }
210
266
    oldset->nodeTab[0] = cur;
211
266
    xmlXPathValuePush(ctxt, obj);
212
266
}
213
214
/**
215
 * XPtrPart ::= 'xpointer' '(' XPtrExpr ')'
216
 *            | Scheme '(' SchemeSpecificExpr ')'
217
 *
218
 * Scheme   ::=  NCName - 'xpointer' [VC: Non-XPointer schemes]
219
 *
220
 * SchemeSpecificExpr ::= StringWithBalancedParens
221
 *
222
 * StringWithBalancedParens ::=
223
 *              [^()]* ('(' StringWithBalancedParens ')' [^()]*)*
224
 *              [VC: Parenthesis escaping]
225
 *
226
 * XPtrExpr ::= Expr [VC: Parenthesis escaping]
227
 *
228
 * VC: Parenthesis escaping:
229
 *   The end of an XPointer part is signaled by the right parenthesis ")"
230
 *   character that is balanced with the left parenthesis "(" character
231
 *   that began the part. Any unbalanced parenthesis character inside the
232
 *   expression, even within literals, must be escaped with a circumflex (^)
233
 *   character preceding it. If the expression contains any literal
234
 *   occurrences of the circumflex, each must be escaped with an additional
235
 *   circumflex (that is, ^^). If the unescaped parentheses in the expression
236
 *   are not balanced, a syntax error results.
237
 *
238
 * Parse and evaluate an XPtrPart. Basically it generates the unescaped
239
 * string and if the scheme is 'xpointer' it will call the XPath interpreter.
240
 *
241
 * TODO: there is no new scheme registration mechanism
242
 *
243
 * @param ctxt  the XPointer Parser context
244
 * @param name  the preparsed Scheme for the XPtrPart
245
 */
246
247
static void
248
40.6k
xmlXPtrEvalXPtrPart(xmlXPathParserContextPtr ctxt, xmlChar *name) {
249
40.6k
    xmlChar *buffer, *cur;
250
40.6k
    int len;
251
40.6k
    int level;
252
253
40.6k
    if (name == NULL)
254
0
    name = xmlXPathParseName(ctxt);
255
40.6k
    if (name == NULL)
256
40.6k
  XP_ERROR(XPATH_EXPR_ERROR);
257
258
40.6k
    if (CUR != '(') {
259
2.09k
        xmlFree(name);
260
2.09k
  XP_ERROR(XPATH_EXPR_ERROR);
261
0
    }
262
38.5k
    NEXT;
263
38.5k
    level = 1;
264
265
38.5k
    len = xmlStrlen(ctxt->cur);
266
38.5k
    len++;
267
38.5k
    buffer = xmlMalloc(len);
268
38.5k
    if (buffer == NULL) {
269
1
        xmlXPathPErrMemory(ctxt);
270
1
        xmlFree(name);
271
1
  return;
272
1
    }
273
274
38.5k
    cur = buffer;
275
732k
    while (CUR != 0) {
276
729k
  if (CUR == ')') {
277
45.2k
      level--;
278
45.2k
      if (level == 0) {
279
35.6k
    NEXT;
280
35.6k
    break;
281
35.6k
      }
282
684k
  } else if (CUR == '(') {
283
9.63k
      level++;
284
674k
  } else if (CUR == '^') {
285
1.44k
            if ((NXT(1) == ')') || (NXT(1) == '(') || (NXT(1) == '^')) {
286
926
                NEXT;
287
926
            }
288
1.44k
  }
289
693k
        *cur++ = CUR;
290
693k
  NEXT;
291
693k
    }
292
38.5k
    *cur = 0;
293
294
38.5k
    if ((level != 0) && (CUR == 0)) {
295
2.93k
        xmlFree(name);
296
2.93k
  xmlFree(buffer);
297
2.93k
  XP_ERROR(XPTR_SYNTAX_ERROR);
298
0
    }
299
300
35.6k
    if (xmlStrEqual(name, (xmlChar *) "xpointer") ||
301
35.6k
        xmlStrEqual(name, (xmlChar *) "xpath1")) {
302
11.2k
  const xmlChar *oldBase = ctxt->base;
303
11.2k
  const xmlChar *oldCur = ctxt->cur;
304
305
11.2k
  ctxt->cur = ctxt->base = buffer;
306
  /*
307
   * To evaluate an xpointer scheme element (4.3) we need:
308
   *   context initialized to the root
309
   *   context position initialized to 1
310
   *   context size initialized to 1
311
   */
312
11.2k
  ctxt->context->node = (xmlNodePtr)ctxt->context->doc;
313
11.2k
  ctxt->context->proximityPosition = 1;
314
11.2k
  ctxt->context->contextSize = 1;
315
11.2k
  xmlXPathEvalExpr(ctxt);
316
11.2k
  ctxt->base = oldBase;
317
11.2k
        ctxt->cur = oldCur;
318
24.3k
    } else if (xmlStrEqual(name, (xmlChar *) "element")) {
319
2.34k
  const xmlChar *oldBase = ctxt->base;
320
2.34k
  const xmlChar *oldCur = ctxt->cur;
321
2.34k
  xmlChar *name2;
322
323
2.34k
  ctxt->cur = ctxt->base = buffer;
324
2.34k
  if (buffer[0] == '/') {
325
894
      xmlXPathRoot(ctxt);
326
894
      xmlXPtrEvalChildSeq(ctxt, NULL);
327
1.45k
  } else {
328
1.45k
      name2 = xmlXPathParseName(ctxt);
329
1.45k
      if (name2 == NULL) {
330
1.01k
                ctxt->base = oldBase;
331
1.01k
                ctxt->cur = oldCur;
332
1.01k
    xmlFree(buffer);
333
1.01k
                xmlFree(name);
334
1.01k
    XP_ERROR(XPATH_EXPR_ERROR);
335
0
      }
336
438
      xmlXPtrEvalChildSeq(ctxt, name2);
337
438
  }
338
1.33k
  ctxt->base = oldBase;
339
1.33k
        ctxt->cur = oldCur;
340
1.33k
#ifdef XPTR_XMLNS_SCHEME
341
21.9k
    } else if (xmlStrEqual(name, (xmlChar *) "xmlns")) {
342
12.6k
  const xmlChar *oldBase = ctxt->base;
343
12.6k
  const xmlChar *oldCur = ctxt->cur;
344
12.6k
  xmlChar *prefix;
345
346
12.6k
  ctxt->cur = ctxt->base = buffer;
347
12.6k
        prefix = xmlXPathParseNCName(ctxt);
348
12.6k
  if (prefix == NULL) {
349
1.86k
            ctxt->base = oldBase;
350
1.86k
            ctxt->cur = oldCur;
351
1.86k
      xmlFree(buffer);
352
1.86k
      xmlFree(name);
353
1.86k
      XP_ERROR(XPTR_SYNTAX_ERROR);
354
0
  }
355
10.7k
  SKIP_BLANKS;
356
10.7k
  if (CUR != '=') {
357
6.41k
            ctxt->base = oldBase;
358
6.41k
            ctxt->cur = oldCur;
359
6.41k
      xmlFree(prefix);
360
6.41k
      xmlFree(buffer);
361
6.41k
      xmlFree(name);
362
6.41k
      XP_ERROR(XPTR_SYNTAX_ERROR);
363
0
  }
364
4.36k
  NEXT;
365
4.36k
  SKIP_BLANKS;
366
367
4.36k
  if (xmlXPathRegisterNs(ctxt->context, prefix, ctxt->cur) < 0)
368
6
            xmlXPathPErrMemory(ctxt);
369
4.36k
        ctxt->base = oldBase;
370
4.36k
        ctxt->cur = oldCur;
371
4.36k
  xmlFree(prefix);
372
4.36k
#endif /* XPTR_XMLNS_SCHEME */
373
9.33k
    } else {
374
9.33k
        xmlXPtrErr(ctxt, XML_XPTR_UNKNOWN_SCHEME,
375
9.33k
       "unsupported scheme '%s'\n", name);
376
9.33k
    }
377
26.3k
    xmlFree(buffer);
378
26.3k
    xmlFree(name);
379
26.3k
}
380
381
/**
382
 * FullXPtr ::= XPtrPart (S? XPtrPart)*
383
 *
384
 * As the specs says:
385
 * -----------
386
 * When multiple XPtrParts are provided, they must be evaluated in
387
 * left-to-right order. If evaluation of one part fails, the nexti
388
 * is evaluated. The following conditions cause XPointer part failure:
389
 *
390
 * - An unknown scheme
391
 * - A scheme that does not locate any sub-resource present in the resource
392
 * - A scheme that is not applicable to the media type of the resource
393
 *
394
 * The XPointer application must consume a failed XPointer part and
395
 * attempt to evaluate the next one, if any. The result of the first
396
 * XPointer part whose evaluation succeeds is taken to be the fragment
397
 * located by the XPointer as a whole. If all the parts fail, the result
398
 * for the XPointer as a whole is a sub-resource error.
399
 * -----------
400
 *
401
 * Parse and evaluate a Full XPtr i.e. possibly a cascade of XPath based
402
 * expressions or other schemes.
403
 *
404
 * @param ctxt  the XPointer Parser context
405
 * @param name  the preparsed Scheme for the first XPtrPart
406
 */
407
static void
408
28.8k
xmlXPtrEvalFullXPtr(xmlXPathParserContextPtr ctxt, xmlChar *name) {
409
28.8k
    if (name == NULL)
410
0
    name = xmlXPathParseName(ctxt);
411
28.8k
    if (name == NULL)
412
28.8k
  XP_ERROR(XPATH_EXPR_ERROR);
413
46.2k
    while (name != NULL) {
414
40.6k
  ctxt->error = XPATH_EXPRESSION_OK;
415
40.6k
  xmlXPtrEvalXPtrPart(ctxt, name);
416
417
  /* in case of syntax error, break here */
418
40.6k
  if ((ctxt->error != XPATH_EXPRESSION_OK) &&
419
40.6k
            (ctxt->error != XML_XPTR_UNKNOWN_SCHEME))
420
22.9k
      return;
421
422
  /*
423
   * If the returned value is a non-empty nodeset
424
   * or location set, return here.
425
   */
426
17.6k
  if (ctxt->value != NULL) {
427
3.47k
      xmlXPathObjectPtr obj = ctxt->value;
428
429
3.47k
      switch (obj->type) {
430
711
    case XPATH_NODESET: {
431
711
        xmlNodeSetPtr loc = ctxt->value->nodesetval;
432
711
        if ((loc != NULL) && (loc->nodeNr > 0))
433
256
      return;
434
455
        break;
435
711
    }
436
2.76k
    default:
437
2.76k
        break;
438
3.47k
      }
439
440
      /*
441
       * Evaluating to improper values is equivalent to
442
       * a sub-resource error, clean-up the stack
443
       */
444
6.43k
      do {
445
6.43k
    obj = xmlXPathValuePop(ctxt);
446
6.43k
    if (obj != NULL) {
447
3.21k
        xmlXPathFreeObject(obj);
448
3.21k
    }
449
6.43k
      } while (obj != NULL);
450
3.21k
  }
451
452
  /*
453
   * Is there another XPointer part.
454
   */
455
17.4k
  SKIP_BLANKS;
456
17.4k
  name = xmlXPathParseName(ctxt);
457
17.4k
    }
458
28.8k
}
459
460
/**
461
 *  ChildSeq ::= '/1' ('/' [0-9]*)*
462
 *             | Name ('/' [0-9]*)+
463
 *
464
 * Parse and evaluate a Child Sequence. This routine also handle the
465
 * case of a Bare Name used to get a document ID.
466
 *
467
 * @param ctxt  the XPointer Parser context
468
 * @param name  a possible ID name of the child sequence
469
 */
470
static void
471
9.51k
xmlXPtrEvalChildSeq(xmlXPathParserContextPtr ctxt, xmlChar *name) {
472
    /*
473
     * XPointer don't allow by syntax to address in multirooted trees
474
     * this might prove useful in some cases, warn about it.
475
     */
476
9.51k
    if ((name == NULL) && (CUR == '/') && (NXT(1) != '1')) {
477
2.43k
        xmlXPtrErr(ctxt, XML_XPTR_CHILDSEQ_START,
478
2.43k
       "warning: ChildSeq not starting by /1\n", NULL);
479
2.43k
    }
480
481
9.51k
    if (name != NULL) {
482
5.35k
  xmlXPathValuePush(ctxt, xmlXPathNewString(name));
483
5.35k
  xmlFree(name);
484
5.35k
  xmlXPathIdFunction(ctxt, 1);
485
5.35k
  CHECK_ERROR;
486
5.35k
    }
487
488
15.9k
    while (CUR == '/') {
489
6.45k
  int child = 0, overflow = 0;
490
6.45k
  NEXT;
491
492
76.8k
  while ((CUR >= '0') && (CUR <= '9')) {
493
70.4k
            int d = CUR - '0';
494
70.4k
            if (child > INT_MAX / 10)
495
47.3k
                overflow = 1;
496
23.0k
            else
497
23.0k
                child *= 10;
498
70.4k
            if (child > INT_MAX - d)
499
239
                overflow = 1;
500
70.2k
            else
501
70.2k
                child += d;
502
70.4k
      NEXT;
503
70.4k
  }
504
6.45k
        if (overflow)
505
1.69k
            child = 0;
506
6.45k
  xmlXPtrGetChildNo(ctxt, child);
507
6.45k
    }
508
9.50k
}
509
510
511
/**
512
 *  XPointer ::= Name
513
 *             | ChildSeq
514
 *             | FullXPtr
515
 *
516
 * Parse and evaluate an XPointer
517
 *
518
 * @param ctxt  the XPointer Parser context
519
 */
520
static void
521
37.8k
xmlXPtrEvalXPointer(xmlXPathParserContextPtr ctxt) {
522
37.8k
    if (ctxt->valueTab == NULL) {
523
  /* Allocate the value stack */
524
37.8k
  ctxt->valueTab = (xmlXPathObjectPtr *)
525
37.8k
       xmlMalloc(10 * sizeof(xmlXPathObjectPtr));
526
37.8k
  if (ctxt->valueTab == NULL) {
527
5
      xmlXPathPErrMemory(ctxt);
528
5
      return;
529
5
  }
530
37.8k
  ctxt->valueNr = 0;
531
37.8k
  ctxt->valueMax = 10;
532
37.8k
  ctxt->value = NULL;
533
37.8k
    }
534
37.8k
    SKIP_BLANKS;
535
37.8k
    if (CUR == '/') {
536
3.26k
  xmlXPathRoot(ctxt);
537
3.26k
        xmlXPtrEvalChildSeq(ctxt, NULL);
538
34.5k
    } else {
539
34.5k
  xmlChar *name;
540
541
34.5k
  name = xmlXPathParseName(ctxt);
542
34.5k
  if (name == NULL)
543
33.7k
      XP_ERROR(XPATH_EXPR_ERROR);
544
33.7k
  if (CUR == '(') {
545
28.8k
      xmlXPtrEvalFullXPtr(ctxt, name);
546
      /* Short evaluation */
547
28.8k
      return;
548
28.8k
  } else {
549
      /* this handle both Bare Names and Child Sequences */
550
4.92k
      xmlXPtrEvalChildSeq(ctxt, name);
551
4.92k
  }
552
33.7k
    }
553
8.18k
    SKIP_BLANKS;
554
8.18k
    if (CUR != 0)
555
7.09k
  XP_ERROR(XPATH_EXPR_ERROR);
556
1.08k
}
557
558
559
/************************************************************************
560
 *                  *
561
 *      General routines        *
562
 *                  *
563
 ************************************************************************/
564
565
/**
566
 * Create a new XPointer context
567
 *
568
 * @deprecated Same as xmlXPathNewContext.
569
 *
570
 * @param doc  the XML document
571
 * @param here  unused
572
 * @param origin  unused
573
 * @returns the xmlXPathContext just allocated.
574
 */
575
xmlXPathContext *
576
0
xmlXPtrNewContext(xmlDoc *doc, xmlNode *here, xmlNode *origin) {
577
0
    xmlXPathContextPtr ret;
578
0
    (void) here;
579
0
    (void) origin;
580
581
0
    ret = xmlXPathNewContext(doc);
582
0
    if (ret == NULL)
583
0
  return(ret);
584
585
0
    return(ret);
586
0
}
587
588
/**
589
 * Evaluate an XPointer expression.
590
 *
591
 * This function can only return nodesets. The caller has to
592
 * free the object.
593
 *
594
 * @param str  an XPointer expression
595
 * @param ctx  an XPath context
596
 * @returns the xmlXPathObject resulting from the evaluation or NULL
597
 * in case of error.
598
 */
599
xmlXPathObject *
600
37.8k
xmlXPtrEval(const xmlChar *str, xmlXPathContext *ctx) {
601
37.8k
    xmlXPathParserContextPtr ctxt;
602
37.8k
    xmlXPathObjectPtr res = NULL, tmp;
603
37.8k
    xmlXPathObjectPtr init = NULL;
604
37.8k
    int stack = 0;
605
606
37.8k
    xmlInitParser();
607
608
37.8k
    if ((ctx == NULL) || (str == NULL))
609
0
  return(NULL);
610
611
37.8k
    xmlResetError(&ctx->lastError);
612
613
37.8k
    ctxt = xmlXPathNewParserContext(str, ctx);
614
37.8k
    if (ctxt == NULL) {
615
11
        xmlXPathErrMemory(ctx);
616
11
  return(NULL);
617
11
    }
618
37.8k
    xmlXPtrEvalXPointer(ctxt);
619
37.8k
    if (ctx->lastError.code != XML_ERR_OK)
620
34.4k
        goto error;
621
622
3.37k
    if ((ctxt->value != NULL) &&
623
3.37k
  (ctxt->value->type != XPATH_NODESET)) {
624
0
        xmlXPtrErr(ctxt, XML_XPTR_EVAL_FAILED,
625
0
    "xmlXPtrEval: evaluation failed to return a node set\n",
626
0
       NULL);
627
3.37k
    } else {
628
3.37k
  res = xmlXPathValuePop(ctxt);
629
3.37k
    }
630
631
3.37k
    do {
632
3.37k
        tmp = xmlXPathValuePop(ctxt);
633
3.37k
  if (tmp != NULL) {
634
0
      if (tmp != init) {
635
0
    if (tmp->type == XPATH_NODESET) {
636
        /*
637
         * Evaluation may push a root nodeset which is unused
638
         */
639
0
        xmlNodeSetPtr set;
640
0
        set = tmp->nodesetval;
641
0
        if ((set == NULL) || (set->nodeNr != 1) ||
642
0
      (set->nodeTab[0] != (xmlNodePtr) ctx->doc))
643
0
      stack++;
644
0
    } else
645
0
        stack++;
646
0
      }
647
0
      xmlXPathFreeObject(tmp);
648
0
        }
649
3.37k
    } while (tmp != NULL);
650
3.37k
    if (stack != 0) {
651
0
        xmlXPtrErr(ctxt, XML_XPTR_EXTRA_OBJECTS,
652
0
       "xmlXPtrEval: object(s) left on the eval stack\n",
653
0
       NULL);
654
0
    }
655
3.37k
    if (ctx->lastError.code != XML_ERR_OK) {
656
0
  xmlXPathFreeObject(res);
657
0
  res = NULL;
658
0
    }
659
660
37.8k
error:
661
37.8k
    xmlXPathFreeParserContext(ctxt);
662
37.8k
    return(res);
663
3.37k
}
664
665
#endif
666