Coverage Report

Created: 2026-05-30 06:16

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libxml2/xpointer.c
Line
Count
Source
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
1.00k
{
68
1.00k
    xmlStructuredErrorFunc serror = NULL;
69
1.00k
    void *data = NULL;
70
1.00k
    xmlNodePtr node = NULL;
71
1.00k
    int res;
72
73
1.00k
    if (ctxt == NULL)
74
0
        return;
75
    /* Only report the first error */
76
1.00k
    if (ctxt->error != 0)
77
8
        return;
78
79
1.00k
    ctxt->error = code;
80
81
1.00k
    if (ctxt->context != NULL) {
82
1.00k
        xmlErrorPtr err = &ctxt->context->lastError;
83
84
        /* cleanup current last error */
85
1.00k
        xmlResetError(err);
86
87
1.00k
        err->domain = XML_FROM_XPOINTER;
88
1.00k
        err->code = code;
89
1.00k
        err->level = XML_ERR_ERROR;
90
1.00k
        err->str1 = (char *) xmlStrdup(ctxt->base);
91
1.00k
        if (err->str1 == NULL) {
92
5
            xmlXPathPErrMemory(ctxt);
93
5
            return;
94
5
        }
95
996
        err->int1 = ctxt->cur - ctxt->base;
96
996
        err->node = ctxt->context->debugNode;
97
98
996
        serror = ctxt->context->error;
99
996
        data = ctxt->context->userData;
100
996
        node = ctxt->context->debugNode;
101
996
    }
102
103
996
    res = xmlRaiseError(serror, NULL, data, NULL, node,
104
996
                        XML_FROM_XPOINTER, code, XML_ERR_ERROR, NULL, 0,
105
996
                        (const char *) extra, (const char *) ctxt->base,
106
996
                        NULL, ctxt->cur - ctxt->base, 0,
107
996
                        msg, extra);
108
996
    if (res < 0)
109
5
        xmlXPathPErrMemory(ctxt);
110
996
}
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
717
xmlXPtrGetNthChild(xmlNodePtr cur, int no) {
125
717
    int i;
126
717
    if ((cur == NULL) || (cur->type == XML_NAMESPACE_DECL))
127
0
  return(cur);
128
717
    cur = cur->children;
129
2.21k
    for (i = 0;i <= no;cur = cur->next) {
130
2.21k
  if (cur == NULL)
131
306
      return(cur);
132
1.90k
  if ((cur->type == XML_ELEMENT_NODE) ||
133
858
      (cur->type == XML_DOCUMENT_NODE) ||
134
1.04k
      (cur->type == XML_HTML_DOCUMENT_NODE)) {
135
1.04k
      i++;
136
1.04k
      if (i == no)
137
411
    break;
138
1.04k
  }
139
1.90k
    }
140
411
    return(cur);
141
717
}
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
2.96M
#define CUR (*ctxt->cur)
174
#define SKIP(val) ctxt->cur += (val)
175
2.38k
#define NXT(val) ctxt->cur[(val)]
176
177
#define SKIP_BLANKS             \
178
38.0k
    while (IS_BLANK_CH(*(ctxt->cur))) NEXT
179
180
#define CURRENT (*ctxt->cur)
181
684k
#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
2.17k
xmlXPtrGetChildNo(xmlXPathParserContextPtr ctxt, int indx) {
192
2.17k
    xmlNodePtr cur = NULL;
193
2.17k
    xmlXPathObjectPtr obj;
194
2.17k
    xmlNodeSetPtr oldset;
195
196
2.17k
    CHECK_TYPE(XPATH_NODESET);
197
1.92k
    obj = xmlXPathValuePop(ctxt);
198
1.92k
    oldset = obj->nodesetval;
199
1.92k
    if ((indx <= 0) || (oldset == NULL) || (oldset->nodeNr != 1)) {
200
1.21k
  xmlXPathFreeObject(obj);
201
1.21k
  xmlXPathValuePush(ctxt, xmlXPathNewNodeSet(NULL));
202
1.21k
  return;
203
1.21k
    }
204
717
    cur = xmlXPtrGetNthChild(oldset->nodeTab[0], indx);
205
717
    if (cur == NULL) {
206
306
  xmlXPathFreeObject(obj);
207
306
  xmlXPathValuePush(ctxt, xmlXPathNewNodeSet(NULL));
208
306
  return;
209
306
    }
210
411
    oldset->nodeTab[0] = cur;
211
411
    xmlXPathValuePush(ctxt, obj);
212
411
}
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
15.2k
xmlXPtrEvalXPtrPart(xmlXPathParserContextPtr ctxt, xmlChar *name) {
249
15.2k
    xmlChar *buffer, *cur;
250
15.2k
    int len;
251
15.2k
    int level;
252
253
15.2k
    if (name == NULL)
254
0
    name = xmlXPathParseName(ctxt);
255
15.2k
    if (name == NULL)
256
15.2k
  XP_ERROR(XPATH_EXPR_ERROR);
257
258
15.2k
    if (CUR != '(') {
259
87
        xmlFree(name);
260
87
  XP_ERROR(XPATH_EXPR_ERROR);
261
0
    }
262
15.1k
    NEXT;
263
15.1k
    level = 1;
264
265
15.1k
    len = xmlStrlen(ctxt->cur);
266
    /* Overflow in xmlStrlen */
267
15.1k
    if (len == 0 && ctxt->cur != NULL && *ctxt->cur != 0) {
268
0
        xmlXPathPErrMemory(ctxt);
269
0
        xmlFree(name);
270
0
        return;
271
0
    }
272
273
15.1k
    len++;
274
15.1k
    buffer = xmlMalloc(len);
275
15.1k
    if (buffer == NULL) {
276
1
        xmlXPathPErrMemory(ctxt);
277
1
        xmlFree(name);
278
1
  return;
279
1
    }
280
281
15.1k
    cur = buffer;
282
595k
    while (CUR != 0) {
283
595k
  if (CUR == ')') {
284
27.2k
      level--;
285
27.2k
      if (level == 0) {
286
14.9k
    NEXT;
287
14.9k
    break;
288
14.9k
      }
289
568k
  } else if (CUR == '(') {
290
12.5k
      level++;
291
555k
  } else if (CUR == '^') {
292
804
            if ((NXT(1) == ')') || (NXT(1) == '(') || (NXT(1) == '^')) {
293
590
                NEXT;
294
590
            }
295
804
  }
296
580k
        *cur++ = CUR;
297
580k
  NEXT;
298
580k
    }
299
15.1k
    *cur = 0;
300
301
15.1k
    if ((level != 0) && (CUR == 0)) {
302
205
        xmlFree(name);
303
205
  xmlFree(buffer);
304
205
  XP_ERROR(XPTR_SYNTAX_ERROR);
305
0
    }
306
307
14.9k
    if (xmlStrEqual(name, (xmlChar *) "xpointer") ||
308
13.0k
        xmlStrEqual(name, (xmlChar *) "xpath1")) {
309
13.0k
  const xmlChar *oldBase = ctxt->base;
310
13.0k
  const xmlChar *oldCur = ctxt->cur;
311
312
13.0k
  ctxt->cur = ctxt->base = buffer;
313
  /*
314
   * To evaluate an xpointer scheme element (4.3) we need:
315
   *   context initialized to the root
316
   *   context position initialized to 1
317
   *   context size initialized to 1
318
   */
319
13.0k
  ctxt->context->node = (xmlNodePtr)ctxt->context->doc;
320
13.0k
  ctxt->context->proximityPosition = 1;
321
13.0k
  ctxt->context->contextSize = 1;
322
13.0k
  xmlXPathEvalExpr(ctxt);
323
13.0k
  ctxt->base = oldBase;
324
13.0k
        ctxt->cur = oldCur;
325
13.0k
    } else if (xmlStrEqual(name, (xmlChar *) "element")) {
326
553
  const xmlChar *oldBase = ctxt->base;
327
553
  const xmlChar *oldCur = ctxt->cur;
328
553
  xmlChar *name2;
329
330
553
  ctxt->cur = ctxt->base = buffer;
331
553
  if (buffer[0] == '/') {
332
302
      xmlXPathRoot(ctxt);
333
302
      xmlXPtrEvalChildSeq(ctxt, NULL);
334
302
  } else {
335
251
      name2 = xmlXPathParseName(ctxt);
336
251
      if (name2 == NULL) {
337
1
                ctxt->base = oldBase;
338
1
                ctxt->cur = oldCur;
339
1
    xmlFree(buffer);
340
1
                xmlFree(name);
341
1
    XP_ERROR(XPATH_EXPR_ERROR);
342
0
      }
343
250
      xmlXPtrEvalChildSeq(ctxt, name2);
344
250
  }
345
552
  ctxt->base = oldBase;
346
552
        ctxt->cur = oldCur;
347
552
#ifdef XPTR_XMLNS_SCHEME
348
1.30k
    } else if (xmlStrEqual(name, (xmlChar *) "xmlns")) {
349
522
  const xmlChar *oldBase = ctxt->base;
350
522
  const xmlChar *oldCur = ctxt->cur;
351
522
  xmlChar *prefix;
352
353
522
  ctxt->cur = ctxt->base = buffer;
354
522
        prefix = xmlXPathParseNCName(ctxt);
355
522
  if (prefix == NULL) {
356
1
            ctxt->base = oldBase;
357
1
            ctxt->cur = oldCur;
358
1
      xmlFree(buffer);
359
1
      xmlFree(name);
360
1
      XP_ERROR(XPTR_SYNTAX_ERROR);
361
0
  }
362
521
  SKIP_BLANKS;
363
521
  if (CUR != '=') {
364
25
            ctxt->base = oldBase;
365
25
            ctxt->cur = oldCur;
366
25
      xmlFree(prefix);
367
25
      xmlFree(buffer);
368
25
      xmlFree(name);
369
25
      XP_ERROR(XPTR_SYNTAX_ERROR);
370
0
  }
371
496
  NEXT;
372
496
  SKIP_BLANKS;
373
374
496
  if (xmlXPathRegisterNs(ctxt->context, prefix, ctxt->cur) < 0)
375
8
            xmlXPathPErrMemory(ctxt);
376
496
        ctxt->base = oldBase;
377
496
        ctxt->cur = oldCur;
378
496
  xmlFree(prefix);
379
496
#endif /* XPTR_XMLNS_SCHEME */
380
780
    } else {
381
780
        xmlXPtrErr(ctxt, XML_XPTR_UNKNOWN_SCHEME,
382
780
       "unsupported scheme '%s'\n", name);
383
780
    }
384
14.9k
    xmlFree(buffer);
385
14.9k
    xmlFree(name);
386
14.9k
}
387
388
/**
389
 * FullXPtr ::= XPtrPart (S? XPtrPart)*
390
 *
391
 * As the specs says:
392
 * -----------
393
 * When multiple XPtrParts are provided, they must be evaluated in
394
 * left-to-right order. If evaluation of one part fails, the nexti
395
 * is evaluated. The following conditions cause XPointer part failure:
396
 *
397
 * - An unknown scheme
398
 * - A scheme that does not locate any sub-resource present in the resource
399
 * - A scheme that is not applicable to the media type of the resource
400
 *
401
 * The XPointer application must consume a failed XPointer part and
402
 * attempt to evaluate the next one, if any. The result of the first
403
 * XPointer part whose evaluation succeeds is taken to be the fragment
404
 * located by the XPointer as a whole. If all the parts fail, the result
405
 * for the XPointer as a whole is a sub-resource error.
406
 * -----------
407
 *
408
 * Parse and evaluate a Full XPtr i.e. possibly a cascade of XPath based
409
 * expressions or other schemes.
410
 *
411
 * @param ctxt  the XPointer Parser context
412
 * @param name  the preparsed Scheme for the first XPtrPart
413
 */
414
static void
415
9.98k
xmlXPtrEvalFullXPtr(xmlXPathParserContextPtr ctxt, xmlChar *name) {
416
9.98k
    if (name == NULL)
417
0
    name = xmlXPathParseName(ctxt);
418
9.98k
    if (name == NULL)
419
9.98k
  XP_ERROR(XPATH_EXPR_ERROR);
420
20.7k
    while (name != NULL) {
421
15.2k
  ctxt->error = XPATH_EXPRESSION_OK;
422
15.2k
  xmlXPtrEvalXPtrPart(ctxt, name);
423
424
  /* in case of syntax error, break here */
425
15.2k
  if ((ctxt->error != XPATH_EXPRESSION_OK) &&
426
3.94k
            (ctxt->error != XML_XPTR_UNKNOWN_SCHEME))
427
3.16k
      return;
428
429
  /*
430
   * If the returned value is a non-empty nodeset
431
   * or location set, return here.
432
   */
433
12.0k
  if (ctxt->value != NULL) {
434
10.5k
      xmlXPathObjectPtr obj = ctxt->value;
435
436
10.5k
      switch (obj->type) {
437
7.49k
    case XPATH_NODESET: {
438
7.49k
        xmlNodeSetPtr loc = ctxt->value->nodesetval;
439
7.49k
        if ((loc != NULL) && (loc->nodeNr > 0))
440
1.32k
      return;
441
6.16k
        break;
442
7.49k
    }
443
6.16k
    default:
444
3.03k
        break;
445
10.5k
      }
446
447
      /*
448
       * Evaluating to improper values is equivalent to
449
       * a sub-resource error, clean-up the stack
450
       */
451
18.4k
      do {
452
18.4k
    obj = xmlXPathValuePop(ctxt);
453
18.4k
    if (obj != NULL) {
454
9.20k
        xmlXPathFreeObject(obj);
455
9.20k
    }
456
18.4k
      } while (obj != NULL);
457
9.20k
  }
458
459
  /*
460
   * Is there another XPointer part.
461
   */
462
10.7k
  SKIP_BLANKS;
463
10.7k
  name = xmlXPathParseName(ctxt);
464
10.7k
    }
465
9.98k
}
466
467
/**
468
 *  ChildSeq ::= '/1' ('/' [0-9]*)*
469
 *             | Name ('/' [0-9]*)+
470
 *
471
 * Parse and evaluate a Child Sequence. This routine also handle the
472
 * case of a Bare Name used to get a document ID.
473
 *
474
 * @param ctxt  the XPointer Parser context
475
 * @param name  a possible ID name of the child sequence
476
 */
477
static void
478
2.14k
xmlXPtrEvalChildSeq(xmlXPathParserContextPtr ctxt, xmlChar *name) {
479
    /*
480
     * XPointer don't allow by syntax to address in multirooted trees
481
     * this might prove useful in some cases, warn about it.
482
     */
483
2.14k
    if ((name == NULL) && (CUR == '/') && (NXT(1) != '1')) {
484
229
        xmlXPtrErr(ctxt, XML_XPTR_CHILDSEQ_START,
485
229
       "warning: ChildSeq not starting by /1\n", NULL);
486
229
    }
487
488
2.14k
    if (name != NULL) {
489
1.58k
  xmlXPathValuePush(ctxt, xmlXPathNewString(name));
490
1.58k
  xmlFree(name);
491
1.58k
  xmlXPathIdFunction(ctxt, 1);
492
1.58k
  CHECK_ERROR;
493
1.58k
    }
494
495
4.29k
    while (CUR == '/') {
496
2.17k
  int child = 0, overflow = 0;
497
2.17k
  NEXT;
498
499
6.63k
  while ((CUR >= '0') && (CUR <= '9')) {
500
4.45k
            int d = CUR - '0';
501
4.45k
            if (child > INT_MAX / 10)
502
843
                overflow = 1;
503
3.61k
            else
504
3.61k
                child *= 10;
505
4.45k
            if (child > INT_MAX - d)
506
194
                overflow = 1;
507
4.26k
            else
508
4.26k
                child += d;
509
4.45k
      NEXT;
510
4.45k
  }
511
2.17k
        if (overflow)
512
226
            child = 0;
513
2.17k
  xmlXPtrGetChildNo(ctxt, child);
514
2.17k
    }
515
2.11k
}
516
517
518
/**
519
 *  XPointer ::= Name
520
 *             | ChildSeq
521
 *             | FullXPtr
522
 *
523
 * Parse and evaluate an XPointer
524
 *
525
 * @param ctxt  the XPointer Parser context
526
 */
527
static void
528
24.8k
xmlXPtrEvalXPointer(xmlXPathParserContextPtr ctxt) {
529
24.8k
    if (ctxt->valueTab == NULL) {
530
  /* Allocate the value stack */
531
24.8k
  ctxt->valueTab = (xmlXPathObjectPtr *)
532
24.8k
       xmlMalloc(10 * sizeof(xmlXPathObjectPtr));
533
24.8k
  if (ctxt->valueTab == NULL) {
534
93
      xmlXPathPErrMemory(ctxt);
535
93
      return;
536
93
  }
537
24.7k
  ctxt->valueNr = 0;
538
24.7k
  ctxt->valueMax = 10;
539
24.7k
  ctxt->value = NULL;
540
24.7k
    }
541
24.7k
    SKIP_BLANKS;
542
24.7k
    if (CUR == '/') {
543
256
  xmlXPathRoot(ctxt);
544
256
        xmlXPtrEvalChildSeq(ctxt, NULL);
545
24.4k
    } else {
546
24.4k
  xmlChar *name;
547
548
24.4k
  name = xmlXPathParseName(ctxt);
549
24.4k
  if (name == NULL)
550
13.1k
      XP_ERROR(XPATH_EXPR_ERROR);
551
11.3k
  if (CUR == '(') {
552
9.98k
      xmlXPtrEvalFullXPtr(ctxt, name);
553
      /* Short evaluation */
554
9.98k
      return;
555
9.98k
  } else {
556
      /* this handle both Bare Names and Child Sequences */
557
1.33k
      xmlXPtrEvalChildSeq(ctxt, name);
558
1.33k
  }
559
11.3k
    }
560
1.59k
    SKIP_BLANKS;
561
1.59k
    if (CUR != 0)
562
973
  XP_ERROR(XPATH_EXPR_ERROR);
563
973
}
564
565
566
/************************************************************************
567
 *                  *
568
 *      General routines        *
569
 *                  *
570
 ************************************************************************/
571
572
/**
573
 * Create a new XPointer context
574
 *
575
 * @deprecated Same as xmlXPathNewContext.
576
 *
577
 * @param doc  the XML document
578
 * @param here  unused
579
 * @param origin  unused
580
 * @returns the xmlXPathContext just allocated.
581
 */
582
xmlXPathContext *
583
0
xmlXPtrNewContext(xmlDoc *doc, xmlNode *here, xmlNode *origin) {
584
0
    xmlXPathContextPtr ret;
585
0
    (void) here;
586
0
    (void) origin;
587
588
0
    ret = xmlXPathNewContext(doc);
589
0
    if (ret == NULL)
590
0
  return(ret);
591
592
0
    return(ret);
593
0
}
594
595
/**
596
 * Evaluate an XPointer expression.
597
 *
598
 * This function can only return nodesets. The caller has to
599
 * free the object.
600
 *
601
 * @param str  an XPointer expression
602
 * @param ctx  an XPath context
603
 * @returns the xmlXPathObject resulting from the evaluation or NULL
604
 * in case of error.
605
 */
606
xmlXPathObject *
607
25.1k
xmlXPtrEval(const xmlChar *str, xmlXPathContext *ctx) {
608
25.1k
    xmlXPathParserContextPtr ctxt;
609
25.1k
    xmlXPathObjectPtr res = NULL, tmp;
610
25.1k
    xmlXPathObjectPtr init = NULL;
611
25.1k
    int stack = 0;
612
613
25.1k
    xmlInitParser();
614
615
25.1k
    if ((ctx == NULL) || (str == NULL))
616
0
  return(NULL);
617
618
25.1k
    xmlResetError(&ctx->lastError);
619
620
25.1k
    ctxt = xmlXPathNewParserContext(str, ctx);
621
25.1k
    if (ctxt == NULL) {
622
338
        xmlXPathErrMemory(ctx);
623
338
  return(NULL);
624
338
    }
625
24.8k
    xmlXPtrEvalXPointer(ctxt);
626
24.8k
    if (ctx->lastError.code != XML_ERR_OK)
627
17.3k
        goto error;
628
629
7.49k
    if ((ctxt->value != NULL) &&
630
2.11k
  (ctxt->value->type != XPATH_NODESET)) {
631
0
        xmlXPtrErr(ctxt, XML_XPTR_EVAL_FAILED,
632
0
    "xmlXPtrEval: evaluation failed to return a node set\n",
633
0
       NULL);
634
7.49k
    } else {
635
7.49k
  res = xmlXPathValuePop(ctxt);
636
7.49k
    }
637
638
7.49k
    do {
639
7.49k
        tmp = xmlXPathValuePop(ctxt);
640
7.49k
  if (tmp != NULL) {
641
0
      if (tmp != init) {
642
0
    if (tmp->type == XPATH_NODESET) {
643
        /*
644
         * Evaluation may push a root nodeset which is unused
645
         */
646
0
        xmlNodeSetPtr set;
647
0
        set = tmp->nodesetval;
648
0
        if ((set == NULL) || (set->nodeNr != 1) ||
649
0
      (set->nodeTab[0] != (xmlNodePtr) ctx->doc))
650
0
      stack++;
651
0
    } else
652
0
        stack++;
653
0
      }
654
0
      xmlXPathFreeObject(tmp);
655
0
        }
656
7.49k
    } while (tmp != NULL);
657
7.49k
    if (stack != 0) {
658
0
        xmlXPtrErr(ctxt, XML_XPTR_EXTRA_OBJECTS,
659
0
       "xmlXPtrEval: object(s) left on the eval stack\n",
660
0
       NULL);
661
0
    }
662
7.49k
    if (ctx->lastError.code != XML_ERR_OK) {
663
0
  xmlXPathFreeObject(res);
664
0
  res = NULL;
665
0
    }
666
667
24.8k
error:
668
24.8k
    xmlXPathFreeParserContext(ctxt);
669
24.8k
    return(res);
670
7.49k
}
671
672
#endif
673