Coverage Report

Created: 2025-10-10 06:45

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libxml2/pattern.c
Line
Count
Source
1
/*
2
 * pattern.c: Implementation of selectors for nodes
3
 *
4
 * Reference:
5
 *   http://www.w3.org/TR/2001/REC-xmlschema-1-20010502/
6
 *   to some extent
7
 *   http://www.w3.org/TR/1999/REC-xml-19991116
8
 *
9
 * See Copyright for the status of this software.
10
 *
11
 * Author: Daniel Veillard
12
 */
13
14
/*
15
 * TODO:
16
 * - compilation flags to check for specific syntaxes
17
 *   using flags of #xmlPatterncompile
18
 * - making clear how pattern starting with / or . need to be handled,
19
 *   currently push(NULL, NULL) means a reset of the streaming context
20
 *   and indicating we are on / (the document node), probably need
21
 *   something similar for .
22
 * - get rid of the "compile" starting with lowercase
23
 * - DONE (2006-05-16): get rid of the Strdup/Strndup in case of dictionary
24
 */
25
26
#define IN_LIBXML
27
#include "libxml.h"
28
29
#include <string.h>
30
#include <libxml/pattern.h>
31
#include <libxml/xmlmemory.h>
32
#include <libxml/tree.h>
33
#include <libxml/dict.h>
34
#include <libxml/xmlerror.h>
35
#include <libxml/parserInternals.h>
36
37
#include "private/memory.h"
38
#include "private/parser.h"
39
40
#ifdef LIBXML_PATTERN_ENABLED
41
42
#ifdef ERROR
43
#undef ERROR
44
#endif
45
#define ERROR(a, b, c, d)
46
#define ERROR5(a, b, c, d, e)
47
48
1.41M
#define XML_STREAM_STEP_DESC  1
49
246k
#define XML_STREAM_STEP_FINAL 2
50
487k
#define XML_STREAM_STEP_ROOT  4
51
24.7k
#define XML_STREAM_STEP_ATTR  8
52
2.37k
#define XML_STREAM_STEP_NODE  16
53
1.51k
#define XML_STREAM_STEP_IN_SET  32
54
55
/*
56
* NOTE: Those private flags (XML_STREAM_xxx) are used
57
*   in _xmlStreamCtxt->flag. They extend the public
58
*   xmlPatternFlags, so be careful not to interfere with the
59
*   reserved values for xmlPatternFlags.
60
*/
61
4.89k
#define XML_STREAM_FINAL_IS_ANY_NODE 1<<14
62
11.7k
#define XML_STREAM_FROM_ROOT 1<<15
63
693k
#define XML_STREAM_DESC 1<<16
64
65
/*
66
* XML_STREAM_ANY_NODE is used for comparison against
67
* xmlElementType enums, to indicate a node of any type.
68
*/
69
219k
#define XML_STREAM_ANY_NODE 100
70
71
703k
#define XML_PATTERN_NOTPATTERN  (XML_PATTERN_XPATH | \
72
703k
         XML_PATTERN_XSSEL | \
73
703k
         XML_PATTERN_XSFIELD)
74
75
191k
#define XML_STREAM_XS_IDC(c) ((c)->flags & \
76
191k
    (XML_PATTERN_XSSEL | XML_PATTERN_XSFIELD))
77
78
40.4k
#define XML_STREAM_XS_IDC_SEL(c) ((c)->flags & XML_PATTERN_XSSEL)
79
80
#define XML_STREAM_XS_IDC_FIELD(c) ((c)->flags & XML_PATTERN_XSFIELD)
81
82
#define XML_PAT_COPY_NSNAME(c, r, nsname) \
83
29.0k
    if ((c)->comp->dict) \
84
29.0k
  r = (xmlChar *) xmlDictLookup((c)->comp->dict, BAD_CAST nsname, -1); \
85
29.0k
    else r = xmlStrdup(BAD_CAST nsname);
86
87
51.4k
#define XML_PAT_FREE_STRING(c, r) if ((c)->comp->dict == NULL) xmlFree(r);
88
89
typedef struct _xmlStreamStep xmlStreamStep;
90
typedef xmlStreamStep *xmlStreamStepPtr;
91
struct _xmlStreamStep {
92
    int flags;      /* properties of that step */
93
    const xmlChar *name;  /* first string value if NULL accept all */
94
    const xmlChar *ns;    /* second string value */
95
    int nodeType;   /* type of node */
96
};
97
98
typedef struct _xmlStreamComp xmlStreamComp;
99
typedef xmlStreamComp *xmlStreamCompPtr;
100
struct _xmlStreamComp {
101
    xmlDict *dict;    /* the dictionary if any */
102
    int nbStep;     /* number of steps in the automata */
103
    int maxStep;    /* allocated number of steps */
104
    xmlStreamStepPtr steps; /* the array of steps */
105
    int flags;
106
};
107
108
struct _xmlStreamCtxt {
109
    struct _xmlStreamCtxt *next;/* link to next sub pattern if | */
110
    xmlStreamCompPtr comp;  /* the compiled stream */
111
    int nbState;    /* number of states in the automata */
112
    int maxState;   /* allocated number of states */
113
    int level;      /* how deep are we ? */
114
    int *states;    /* the array of step indexes */
115
    int flags;      /* validation options */
116
    int blockLevel;
117
};
118
119
static void xmlFreeStreamComp(xmlStreamCompPtr comp);
120
121
/*
122
 * Types are private:
123
 */
124
125
typedef enum {
126
    XML_OP_END=0,
127
    XML_OP_ROOT,
128
    XML_OP_ELEM,
129
    XML_OP_CHILD,
130
    XML_OP_ATTR,
131
    XML_OP_PARENT,
132
    XML_OP_ANCESTOR,
133
    XML_OP_NS,
134
    XML_OP_ALL
135
} xmlPatOp;
136
137
138
typedef struct _xmlStepState xmlStepState;
139
typedef xmlStepState *xmlStepStatePtr;
140
struct _xmlStepState {
141
    int step;
142
    xmlNodePtr node;
143
};
144
145
typedef struct _xmlStepStates xmlStepStates;
146
typedef xmlStepStates *xmlStepStatesPtr;
147
struct _xmlStepStates {
148
    int nbstates;
149
    int maxstates;
150
    xmlStepStatePtr states;
151
};
152
153
typedef struct _xmlStepOp xmlStepOp;
154
typedef xmlStepOp *xmlStepOpPtr;
155
struct _xmlStepOp {
156
    xmlPatOp op;
157
    const xmlChar *value;
158
    const xmlChar *value2; /* The namespace name */
159
};
160
161
100k
#define PAT_FROM_ROOT (1<<8)
162
100k
#define PAT_FROM_CUR  (1<<9)
163
164
struct _xmlPattern {
165
    void *data;   /* the associated template */
166
    xmlDictPtr dict;    /* the optional dictionary */
167
    struct _xmlPattern *next; /* next pattern if | is used */
168
    const xmlChar *pattern; /* the pattern */
169
    int flags;      /* flags */
170
    int nbStep;
171
    int maxStep;
172
    xmlStepOpPtr steps;        /* ops for computation */
173
    xmlStreamCompPtr stream;  /* the streaming data if any */
174
};
175
176
typedef struct _xmlPatParserContext xmlPatParserContext;
177
typedef xmlPatParserContext *xmlPatParserContextPtr;
178
struct _xmlPatParserContext {
179
    const xmlChar *cur;     /* the current char being parsed */
180
    const xmlChar *base;    /* the full expression */
181
    int            error;   /* error code */
182
    xmlDictPtr     dict;    /* the dictionary if any */
183
    xmlPatternPtr  comp;    /* the result */
184
    xmlNodePtr     elem;    /* the current node if any */
185
    const xmlChar **namespaces;   /* the namespaces definitions */
186
    int   nb_namespaces;    /* the number of namespaces */
187
};
188
189
/************************************************************************
190
 *                  *
191
 *      Type functions          *
192
 *                  *
193
 ************************************************************************/
194
195
/**
196
 * Create a new XSLT Pattern
197
 *
198
 * @returns the newly allocated xmlPattern or NULL in case of error
199
 */
200
static xmlPatternPtr
201
70.4k
xmlNewPattern(void) {
202
70.4k
    xmlPatternPtr cur;
203
204
70.4k
    cur = (xmlPatternPtr) xmlMalloc(sizeof(xmlPattern));
205
70.4k
    if (cur == NULL) {
206
6
  ERROR(NULL, NULL, NULL,
207
6
    "xmlNewPattern : malloc failed\n");
208
6
  return(NULL);
209
6
    }
210
70.4k
    memset(cur, 0, sizeof(xmlPattern));
211
70.4k
    cur->steps = NULL;
212
70.4k
    cur->maxStep = 0;
213
70.4k
    return(cur);
214
70.4k
}
215
216
/**
217
 * Free up the memory allocated by `comp`
218
 *
219
 * @param comp  an XSLT comp
220
 */
221
void
222
34.5k
xmlFreePattern(xmlPattern *comp) {
223
34.5k
    xmlFreePatternList(comp);
224
34.5k
}
225
226
static void
227
70.4k
xmlFreePatternInternal(xmlPatternPtr comp) {
228
70.4k
    xmlStepOpPtr op;
229
70.4k
    int i;
230
231
70.4k
    if (comp == NULL)
232
0
  return;
233
70.4k
    if (comp->stream != NULL)
234
41.3k
        xmlFreeStreamComp(comp->stream);
235
70.4k
    if (comp->pattern != NULL)
236
0
  xmlFree((xmlChar *)comp->pattern);
237
70.4k
    if (comp->steps != NULL) {
238
61.4k
        if (comp->dict == NULL) {
239
24.7M
      for (i = 0;i < comp->nbStep;i++) {
240
24.7M
    op = &comp->steps[i];
241
24.7M
    if (op->value != NULL)
242
6.83M
        xmlFree((xmlChar *) op->value);
243
24.7M
    if (op->value2 != NULL)
244
11.2k
        xmlFree((xmlChar *) op->value2);
245
24.7M
      }
246
61.4k
  }
247
61.4k
  xmlFree(comp->steps);
248
61.4k
    }
249
70.4k
    if (comp->dict != NULL)
250
0
        xmlDictFree(comp->dict);
251
252
70.4k
    memset(comp, -1, sizeof(xmlPattern));
253
70.4k
    xmlFree(comp);
254
70.4k
}
255
256
/**
257
 * Free up the memory allocated by all the elements of `comp`
258
 *
259
 * @param comp  an XSLT comp list
260
 */
261
void
262
34.5k
xmlFreePatternList(xmlPattern *comp) {
263
34.5k
    xmlPatternPtr cur;
264
265
105k
    while (comp != NULL) {
266
70.4k
  cur = comp;
267
70.4k
  comp = comp->next;
268
70.4k
  cur->next = NULL;
269
70.4k
  xmlFreePatternInternal(cur);
270
70.4k
    }
271
34.5k
}
272
273
/**
274
 * Create a new XML pattern parser context
275
 *
276
 * @param pattern  the pattern context
277
 * @param dict  the inherited dictionary or NULL
278
 * @param namespaces  the prefix definitions, array of [URI, prefix] terminated
279
 *              with [NULL, NULL] or NULL if no namespace is used
280
 * @returns the newly allocated xmlPatParserContext or NULL in case of error
281
 */
282
static xmlPatParserContextPtr
283
xmlNewPatParserContext(const xmlChar *pattern, xmlDictPtr dict,
284
70.4k
                       const xmlChar **namespaces) {
285
70.4k
    xmlPatParserContextPtr cur;
286
287
70.4k
    if (pattern == NULL)
288
0
        return(NULL);
289
290
70.4k
    cur = (xmlPatParserContextPtr) xmlMalloc(sizeof(xmlPatParserContext));
291
70.4k
    if (cur == NULL) {
292
29
  ERROR(NULL, NULL, NULL,
293
29
    "xmlNewPatParserContext : malloc failed\n");
294
29
  return(NULL);
295
29
    }
296
70.4k
    memset(cur, 0, sizeof(xmlPatParserContext));
297
70.4k
    cur->dict = dict;
298
70.4k
    cur->cur = pattern;
299
70.4k
    cur->base = pattern;
300
70.4k
    if (namespaces != NULL) {
301
64.5k
        int i;
302
125k
        for (i = 0;namespaces[2 * i] != NULL;i++)
303
61.1k
            ;
304
64.5k
        cur->nb_namespaces = i;
305
64.5k
    } else {
306
5.94k
        cur->nb_namespaces = 0;
307
5.94k
    }
308
70.4k
    cur->namespaces = namespaces;
309
70.4k
    return(cur);
310
70.4k
}
311
312
/**
313
 * Free up the memory allocated by `ctxt`
314
 *
315
 * @param ctxt  an XSLT parser context
316
 */
317
static void
318
70.4k
xmlFreePatParserContext(xmlPatParserContextPtr ctxt) {
319
70.4k
    if (ctxt == NULL)
320
0
  return;
321
70.4k
    memset(ctxt, -1, sizeof(xmlPatParserContext));
322
70.4k
    xmlFree(ctxt);
323
70.4k
}
324
325
static int
326
192k
xmlPatternGrow(xmlPatternPtr comp) {
327
192k
    xmlStepOpPtr temp;
328
192k
    int newSize;
329
330
192k
    newSize = xmlGrowCapacity(comp->maxStep, sizeof(temp[0]),
331
192k
                              10, XML_MAX_ITEMS);
332
192k
    if (newSize < 0)
333
0
        return(-1);
334
192k
    temp = xmlRealloc(comp->steps, newSize * sizeof(temp[0]));
335
192k
    if (temp == NULL)
336
41
        return(-1);
337
192k
    comp->steps = temp;
338
192k
    comp->maxStep = newSize;
339
340
192k
    return(0);
341
192k
}
342
343
/**
344
 * Add a step to an XSLT Compiled Match
345
 *
346
 * @param ctxt  the pattern parser context
347
 * @param comp  the compiled match expression
348
 * @param op  an op
349
 * @param value  the first value
350
 * @param value2  the second value
351
 * @returns -1 in case of failure, 0 otherwise.
352
 */
353
static int
354
xmlPatternAdd(xmlPatParserContextPtr ctxt, xmlPatternPtr comp,
355
              xmlPatOp op, xmlChar * value, xmlChar * value2)
356
24.6M
{
357
24.6M
    if (comp->nbStep >= comp->maxStep) {
358
158k
        if (xmlPatternGrow(comp) < 0) {
359
34
            ctxt->error = -1;
360
34
            return(-1);
361
34
        }
362
158k
    }
363
24.6M
    comp->steps[comp->nbStep].op = op;
364
24.6M
    comp->steps[comp->nbStep].value = value;
365
24.6M
    comp->steps[comp->nbStep].value2 = value2;
366
24.6M
    comp->nbStep++;
367
24.6M
    return(0);
368
24.6M
}
369
370
/**
371
 * reverse all the stack of expressions
372
 *
373
 * @param comp  the compiled match expression
374
 * @returns 0 in case of success and -1 in case of error.
375
 */
376
static int
377
43.1k
xmlReversePattern(xmlPatternPtr comp) {
378
43.1k
    int i, j;
379
380
    /*
381
     * remove the leading // for //a or .//a
382
     */
383
43.1k
    if ((comp->nbStep > 0) && (comp->steps[0].op == XML_OP_ANCESTOR)) {
384
6.20M
        for (i = 0, j = 1;j < comp->nbStep;i++,j++) {
385
6.19M
      comp->steps[i].value = comp->steps[j].value;
386
6.19M
      comp->steps[i].value2 = comp->steps[j].value2;
387
6.19M
      comp->steps[i].op = comp->steps[j].op;
388
6.19M
  }
389
5.13k
  comp->nbStep--;
390
5.13k
    }
391
392
    /*
393
     * Grow to add OP_END later
394
     */
395
43.1k
    if (comp->nbStep >= comp->maxStep) {
396
33.7k
        if (xmlPatternGrow(comp) < 0)
397
7
            return(-1);
398
33.7k
    }
399
400
43.1k
    i = 0;
401
43.1k
    j = comp->nbStep - 1;
402
10.1M
    while (j > i) {
403
10.0M
  register const xmlChar *tmp;
404
10.0M
  register xmlPatOp op;
405
10.0M
  tmp = comp->steps[i].value;
406
10.0M
  comp->steps[i].value = comp->steps[j].value;
407
10.0M
  comp->steps[j].value = tmp;
408
10.0M
  tmp = comp->steps[i].value2;
409
10.0M
  comp->steps[i].value2 = comp->steps[j].value2;
410
10.0M
  comp->steps[j].value2 = tmp;
411
10.0M
  op = comp->steps[i].op;
412
10.0M
  comp->steps[i].op = comp->steps[j].op;
413
10.0M
  comp->steps[j].op = op;
414
10.0M
  j--;
415
10.0M
  i++;
416
10.0M
    }
417
418
43.1k
    comp->steps[comp->nbStep].value = NULL;
419
43.1k
    comp->steps[comp->nbStep].value2 = NULL;
420
43.1k
    comp->steps[comp->nbStep++].op = XML_OP_END;
421
43.1k
    return(0);
422
43.1k
}
423
424
/************************************************************************
425
 *                  *
426
 *    The interpreter for the precompiled patterns    *
427
 *                  *
428
 ************************************************************************/
429
430
static int
431
0
xmlPatPushState(xmlStepStates *states, int step, xmlNodePtr node) {
432
0
    if (states->maxstates <= states->nbstates) {
433
0
        xmlStepState *tmp;
434
0
        int newSize;
435
436
0
        newSize = xmlGrowCapacity(states->maxstates, sizeof(tmp[0]),
437
0
                                  4, XML_MAX_ITEMS);
438
0
        if (newSize < 0)
439
0
      return(-1);
440
0
  tmp = xmlRealloc(states->states, newSize * sizeof(tmp[0]));
441
0
  if (tmp == NULL)
442
0
      return(-1);
443
0
  states->states = tmp;
444
0
  states->maxstates = newSize;
445
0
    }
446
0
    states->states[states->nbstates].step = step;
447
0
    states->states[states->nbstates++].node = node;
448
0
    return(0);
449
0
}
450
451
/**
452
 * Test whether the node matches the pattern
453
 *
454
 * @param comp  the precompiled pattern
455
 * @param node  a node
456
 * @returns 1 if it matches, 0 if it doesn't and -1 in case of failure
457
 */
458
static int
459
4.99k
xmlPatMatch(xmlPatternPtr comp, xmlNodePtr node) {
460
4.99k
    int i;
461
4.99k
    xmlStepOpPtr step;
462
4.99k
    xmlStepStates states = {0, 0, NULL}; /* // may require backtrack */
463
464
4.99k
    if ((comp == NULL) || (node == NULL)) return(-1);
465
4.99k
    i = 0;
466
4.99k
restart:
467
7.88k
    for (;i < comp->nbStep;i++) {
468
7.88k
  step = &comp->steps[i];
469
7.88k
  switch (step->op) {
470
94
            case XML_OP_END:
471
94
    goto found;
472
25
            case XML_OP_ROOT:
473
25
    if (node->type == XML_NAMESPACE_DECL)
474
0
        goto rollback;
475
25
    node = node->parent;
476
25
    if ((node->type == XML_DOCUMENT_NODE) ||
477
0
        (node->type == XML_HTML_DOCUMENT_NODE))
478
25
        continue;
479
0
    goto rollback;
480
3.35k
            case XML_OP_ELEM:
481
3.35k
    if (node->type != XML_ELEMENT_NODE)
482
402
        goto rollback;
483
2.95k
    if (step->value == NULL)
484
350
        continue;
485
2.60k
    if (step->value[0] != node->name[0])
486
1.58k
        goto rollback;
487
1.01k
    if (!xmlStrEqual(step->value, node->name))
488
559
        goto rollback;
489
490
    /* Namespace test */
491
460
    if (node->ns == NULL) {
492
460
        if (step->value2 != NULL)
493
251
      goto rollback;
494
460
    } else if (node->ns->href != NULL) {
495
0
        if (step->value2 == NULL)
496
0
      goto rollback;
497
0
        if (!xmlStrEqual(step->value2, node->ns->href))
498
0
      goto rollback;
499
0
    }
500
209
    continue;
501
209
            case XML_OP_CHILD: {
502
0
    xmlNodePtr lst;
503
504
0
    if ((node->type != XML_ELEMENT_NODE) &&
505
0
        (node->type != XML_DOCUMENT_NODE) &&
506
0
        (node->type != XML_HTML_DOCUMENT_NODE))
507
0
        goto rollback;
508
509
0
    lst = node->children;
510
511
0
    if (step->value != NULL) {
512
0
        while (lst != NULL) {
513
0
      if ((lst->type == XML_ELEMENT_NODE) &&
514
0
          (step->value[0] == lst->name[0]) &&
515
0
          (xmlStrEqual(step->value, lst->name)))
516
0
          break;
517
0
      lst = lst->next;
518
0
        }
519
0
        if (lst != NULL)
520
0
      continue;
521
0
    }
522
0
    goto rollback;
523
0
      }
524
628
            case XML_OP_ATTR:
525
628
    if (node->type != XML_ATTRIBUTE_NODE)
526
628
        goto rollback;
527
0
    if (step->value != NULL) {
528
0
        if (step->value[0] != node->name[0])
529
0
      goto rollback;
530
0
        if (!xmlStrEqual(step->value, node->name))
531
0
      goto rollback;
532
0
    }
533
    /* Namespace test */
534
0
    if (node->ns == NULL) {
535
0
        if (step->value2 != NULL)
536
0
      goto rollback;
537
0
    } else if (step->value2 != NULL) {
538
0
        if (!xmlStrEqual(step->value2, node->ns->href))
539
0
      goto rollback;
540
0
    }
541
0
    continue;
542
1.13k
            case XML_OP_PARENT:
543
1.13k
    if ((node->type == XML_DOCUMENT_NODE) ||
544
1.13k
        (node->type == XML_HTML_DOCUMENT_NODE) ||
545
1.13k
        (node->type == XML_NAMESPACE_DECL))
546
0
        goto rollback;
547
1.13k
    node = node->parent;
548
1.13k
    if (node == NULL)
549
0
        goto rollback;
550
1.13k
    if (step->value == NULL)
551
1.13k
        continue;
552
0
    if (step->value[0] != node->name[0])
553
0
        goto rollback;
554
0
    if (!xmlStrEqual(step->value, node->name))
555
0
        goto rollback;
556
    /* Namespace test */
557
0
    if (node->ns == NULL) {
558
0
        if (step->value2 != NULL)
559
0
      goto rollback;
560
0
    } else if (node->ns->href != NULL) {
561
0
        if (step->value2 == NULL)
562
0
      goto rollback;
563
0
        if (!xmlStrEqual(step->value2, node->ns->href))
564
0
      goto rollback;
565
0
    }
566
0
    continue;
567
462
            case XML_OP_ANCESTOR:
568
    /* TODO: implement coalescing of ANCESTOR/NODE ops */
569
462
    if (step->value == NULL) {
570
462
        i++;
571
462
        step = &comp->steps[i];
572
462
        if (step->op == XML_OP_ROOT)
573
0
      goto found;
574
462
        if (step->op != XML_OP_ELEM)
575
222
      goto rollback;
576
240
        if (step->value == NULL)
577
6
      return(-1);
578
240
    }
579
234
    if (node == NULL)
580
0
        goto rollback;
581
234
    if ((node->type == XML_DOCUMENT_NODE) ||
582
234
        (node->type == XML_HTML_DOCUMENT_NODE) ||
583
234
        (node->type == XML_NAMESPACE_DECL))
584
0
        goto rollback;
585
234
    node = node->parent;
586
468
    while (node != NULL) {
587
234
        if ((node->type == XML_ELEMENT_NODE) &&
588
0
      (step->value[0] == node->name[0]) &&
589
0
      (xmlStrEqual(step->value, node->name))) {
590
      /* Namespace test */
591
0
      if (node->ns == NULL) {
592
0
          if (step->value2 == NULL)
593
0
        break;
594
0
      } else if (node->ns->href != NULL) {
595
0
          if ((step->value2 != NULL) &&
596
0
              (xmlStrEqual(step->value2, node->ns->href)))
597
0
        break;
598
0
      }
599
0
        }
600
234
        node = node->parent;
601
234
    }
602
234
    if (node == NULL)
603
234
        goto rollback;
604
    /*
605
     * prepare a potential rollback from here
606
     * for ancestors of that node.
607
     */
608
0
    if (step->op == XML_OP_ANCESTOR)
609
0
        xmlPatPushState(&states, i, node);
610
0
    else
611
0
        xmlPatPushState(&states, i - 1, node);
612
0
    continue;
613
485
            case XML_OP_NS:
614
485
    if (node->type != XML_ELEMENT_NODE)
615
201
        goto rollback;
616
284
    if (node->ns == NULL) {
617
284
        if (step->value != NULL)
618
284
      goto rollback;
619
284
    } else if (node->ns->href != NULL) {
620
0
        if (step->value == NULL)
621
0
      goto rollback;
622
0
        if (!xmlStrEqual(step->value, node->ns->href))
623
0
      goto rollback;
624
0
    }
625
0
    break;
626
1.69k
            case XML_OP_ALL:
627
1.69k
    if (node->type != XML_ELEMENT_NODE)
628
534
        goto rollback;
629
1.16k
    break;
630
7.88k
  }
631
7.88k
    }
632
94
found:
633
94
    if (states.states != NULL) {
634
        /* Free the rollback states */
635
0
  xmlFree(states.states);
636
0
    }
637
94
    return(1);
638
4.89k
rollback:
639
    /* got an error try to rollback */
640
4.89k
    if (states.states == NULL)
641
4.89k
  return(0);
642
0
    if (states.nbstates <= 0) {
643
0
  xmlFree(states.states);
644
0
  return(0);
645
0
    }
646
0
    states.nbstates--;
647
0
    i = states.states[states.nbstates].step;
648
0
    node = states.states[states.nbstates].node;
649
0
    goto restart;
650
0
}
651
652
/************************************************************************
653
 *                  *
654
 *      Dedicated parser for templates      *
655
 *                  *
656
 ************************************************************************/
657
658
62.1M
#define CUR (*ctxt->cur)
659
#define SKIP(val) ctxt->cur += (val)
660
12.2M
#define NXT(val) ctxt->cur[(val)]
661
#define PEEKPREV(val) ctxt->cur[-(val)]
662
16.1M
#define CUR_PTR ctxt->cur
663
664
#define SKIP_BLANKS             \
665
34.7M
    while (IS_BLANK_CH(CUR)) NEXT
666
667
#define CURRENT (*ctxt->cur)
668
31.9M
#define NEXT ((*ctxt->cur) ?  ctxt->cur++: ctxt->cur)
669
670
671
#define PUSH(op, val, val2)           \
672
24.6M
    if (xmlPatternAdd(ctxt, ctxt->comp, (op), (val), (val2))) goto error;
673
674
/**
675
 * [4] NameChar ::= Letter | Digit | '.' | '-' | '_' |
676
 *                  CombiningChar | Extender
677
 *
678
 * [5] Name ::= (Letter | '_' | ':') (NameChar)*
679
 *
680
 * [6] Names ::= Name (S Name)*
681
 *
682
 * @param ctxt  the XPath Parser context
683
 * @returns the Name parsed or NULL
684
 */
685
686
static xmlChar *
687
40.3k
xmlPatScanName(xmlPatParserContextPtr ctxt) {
688
40.3k
    const xmlChar *q, *cur;
689
40.3k
    xmlChar *ret = NULL;
690
691
40.3k
    SKIP_BLANKS;
692
693
40.3k
    q = CUR_PTR;
694
40.3k
    cur = xmlScanName(q, XML_MAX_NAME_LENGTH, 0);
695
40.3k
    if ((cur == NULL) || (cur == q))
696
23.9k
        return(NULL);
697
16.3k
    if (ctxt->dict)
698
0
  ret = (xmlChar *) xmlDictLookup(ctxt->dict, q, cur - q);
699
16.3k
    else
700
16.3k
  ret = xmlStrndup(q, cur - q);
701
16.3k
    CUR_PTR = cur;
702
16.3k
    return(ret);
703
40.3k
}
704
705
/**
706
 * Parses a non qualified name
707
 *
708
 * @param ctxt  the XPath Parser context
709
 * @returns the Name parsed or NULL
710
 */
711
712
static xmlChar *
713
9.21M
xmlPatScanNCName(xmlPatParserContextPtr ctxt) {
714
9.21M
    const xmlChar *q, *cur;
715
9.21M
    xmlChar *ret = NULL;
716
717
9.21M
    SKIP_BLANKS;
718
719
9.21M
    q = CUR_PTR;
720
9.21M
    cur = xmlScanName(q, XML_MAX_NAME_LENGTH, XML_SCAN_NC);
721
9.21M
    if ((cur == NULL) || (cur == q))
722
2.36M
        return(NULL);
723
6.85M
    if (ctxt->dict)
724
0
  ret = (xmlChar *) xmlDictLookup(ctxt->dict, q, cur - q);
725
6.85M
    else
726
6.85M
  ret = xmlStrndup(q, cur - q);
727
6.85M
    if (ret == NULL)
728
9
        ctxt->error = -1;
729
6.85M
    CUR_PTR = cur;
730
6.85M
    return(ret);
731
9.21M
}
732
733
/**
734
 * Compile an attribute test.
735
 *
736
 * @param ctxt  the compilation context
737
 */
738
static void
739
40.7k
xmlCompileAttributeTest(xmlPatParserContextPtr ctxt) {
740
40.7k
    xmlChar *token = NULL;
741
40.7k
    xmlChar *name = NULL;
742
40.7k
    xmlChar *URL = NULL;
743
744
40.7k
    SKIP_BLANKS;
745
40.7k
    name = xmlPatScanNCName(ctxt);
746
40.7k
    if (ctxt->error < 0)
747
2
        return;
748
40.7k
    if (name == NULL) {
749
7.56k
  if (CUR == '*') {
750
5.18k
      PUSH(XML_OP_ATTR, NULL, NULL);
751
5.18k
      NEXT;
752
5.18k
  } else {
753
2.37k
      ERROR(NULL, NULL, NULL,
754
2.37k
    "xmlCompileAttributeTest : Name expected\n");
755
2.37k
      ctxt->error = 1;
756
2.37k
  }
757
7.55k
  return;
758
7.56k
    }
759
33.1k
    if (CUR == ':') {
760
11.7k
  int i;
761
11.7k
  xmlChar *prefix = name;
762
763
11.7k
  NEXT;
764
765
11.7k
  if (IS_BLANK_CH(CUR)) {
766
1.01k
      ERROR5(NULL, NULL, NULL, "Invalid QName.\n", NULL);
767
1.01k
      ctxt->error = 1;
768
1.01k
      goto error;
769
1.01k
  }
770
  /*
771
  * This is a namespace match
772
  */
773
10.6k
  token = xmlPatScanName(ctxt);
774
10.6k
  if ((prefix[0] == 'x') &&
775
10.5k
      (prefix[1] == 'm') &&
776
7.03k
      (prefix[2] == 'l') &&
777
6.66k
      (prefix[3] == 0))
778
6.41k
  {
779
6.41k
      XML_PAT_COPY_NSNAME(ctxt, URL, XML_XML_NAMESPACE);
780
6.41k
  } else {
781
6.51k
      for (i = 0;i < ctxt->nb_namespaces;i++) {
782
5.39k
    if (xmlStrEqual(ctxt->namespaces[2 * i + 1], prefix)) {
783
3.15k
        XML_PAT_COPY_NSNAME(ctxt, URL, ctxt->namespaces[2 * i])
784
3.15k
        break;
785
3.15k
    }
786
5.39k
      }
787
4.28k
      if (i >= ctxt->nb_namespaces) {
788
1.12k
    ERROR5(NULL, NULL, NULL,
789
1.12k
        "xmlCompileAttributeTest : no namespace bound to prefix %s\n",
790
1.12k
        prefix);
791
1.12k
    ctxt->error = 1;
792
1.12k
    goto error;
793
1.12k
      }
794
4.28k
  }
795
9.57k
        XML_PAT_FREE_STRING(ctxt, name);
796
9.57k
        name = NULL;
797
9.57k
  if (token == NULL) {
798
4.27k
      if (CUR == '*') {
799
3.32k
    NEXT;
800
3.32k
    PUSH(XML_OP_ATTR, NULL, URL);
801
3.32k
      } else {
802
952
    ERROR(NULL, NULL, NULL,
803
952
        "xmlCompileAttributeTest : Name expected\n");
804
952
    ctxt->error = 1;
805
952
    goto error;
806
952
      }
807
5.29k
  } else {
808
5.29k
      PUSH(XML_OP_ATTR, token, URL);
809
5.28k
  }
810
21.4k
    } else {
811
21.4k
  PUSH(XML_OP_ATTR, name, NULL);
812
21.4k
    }
813
30.0k
    return;
814
30.0k
error:
815
3.09k
    if (name != NULL)
816
2.13k
  XML_PAT_FREE_STRING(ctxt, name);
817
3.09k
    if (URL != NULL)
818
956
  XML_PAT_FREE_STRING(ctxt, URL)
819
3.09k
    if (token != NULL)
820
513
  XML_PAT_FREE_STRING(ctxt, token);
821
3.09k
}
822
823
/**
824
 * Compile the Step Pattern and generates a precompiled
825
 * form suitable for fast matching.
826
 *
827
 * [3]    Step    ::=    '.' | NameTest
828
 * [4]    NameTest    ::=    QName | '*' | NCName ':' '*'
829
 *
830
 * @param ctxt  the compilation context
831
 */
832
833
static void
834
12.3M
xmlCompileStepPattern(xmlPatParserContextPtr ctxt) {
835
12.3M
    xmlChar *token = NULL;
836
12.3M
    xmlChar *name = NULL;
837
12.3M
    xmlChar *URL = NULL;
838
12.3M
    int hasBlanks = 0;
839
840
12.3M
    SKIP_BLANKS;
841
12.3M
    if (CUR == '.') {
842
  /*
843
  * Context node.
844
  */
845
3.16M
  NEXT;
846
3.16M
  PUSH(XML_OP_ELEM, NULL, NULL);
847
3.16M
  return;
848
3.16M
    }
849
9.21M
    if (CUR == '@') {
850
  /*
851
  * Attribute test.
852
  */
853
38.0k
  if (XML_STREAM_XS_IDC_SEL(ctxt->comp)) {
854
237
      ERROR5(NULL, NULL, NULL,
855
237
    "Unexpected attribute axis in '%s'.\n", ctxt->base);
856
237
      ctxt->error = 1;
857
237
      return;
858
237
  }
859
37.7k
  NEXT;
860
37.7k
  xmlCompileAttributeTest(ctxt);
861
37.7k
  if (ctxt->error != 0)
862
4.24k
      goto error;
863
33.5k
  return;
864
37.7k
    }
865
9.17M
    name = xmlPatScanNCName(ctxt);
866
9.17M
    if (ctxt->error < 0)
867
7
        return;
868
9.17M
    if (name == NULL) {
869
2.35M
  if (CUR == '*') {
870
2.35M
      NEXT;
871
2.35M
      PUSH(XML_OP_ALL, NULL, NULL);
872
2.35M
      return;
873
2.35M
  } else {
874
3.93k
      ERROR(NULL, NULL, NULL,
875
3.93k
        "xmlCompileStepPattern : Name expected\n");
876
3.93k
      ctxt->error = 1;
877
3.93k
      return;
878
3.93k
  }
879
2.35M
    }
880
6.81M
    if (IS_BLANK_CH(CUR)) {
881
522k
  hasBlanks = 1;
882
522k
  SKIP_BLANKS;
883
522k
    }
884
6.81M
    if (CUR == ':') {
885
34.1k
  NEXT;
886
34.1k
  if (CUR != ':') {
887
24.9k
      xmlChar *prefix = name;
888
24.9k
      int i;
889
890
24.9k
      if (hasBlanks || IS_BLANK_CH(CUR)) {
891
791
    ERROR5(NULL, NULL, NULL, "Invalid QName.\n", NULL);
892
791
    ctxt->error = 1;
893
791
    goto error;
894
791
      }
895
      /*
896
       * This is a namespace match
897
       */
898
24.1k
      token = xmlPatScanName(ctxt);
899
24.1k
      if ((prefix[0] == 'x') &&
900
19.6k
    (prefix[1] == 'm') &&
901
5.21k
    (prefix[2] == 'l') &&
902
5.12k
    (prefix[3] == 0))
903
4.87k
      {
904
4.87k
    XML_PAT_COPY_NSNAME(ctxt, URL, XML_XML_NAMESPACE)
905
19.2k
      } else {
906
26.2k
    for (i = 0;i < ctxt->nb_namespaces;i++) {
907
21.5k
        if (xmlStrEqual(ctxt->namespaces[2 * i + 1], prefix)) {
908
14.5k
      XML_PAT_COPY_NSNAME(ctxt, URL, ctxt->namespaces[2 * i])
909
14.5k
      break;
910
14.5k
        }
911
21.5k
    }
912
19.2k
    if (i >= ctxt->nb_namespaces) {
913
4.72k
        ERROR5(NULL, NULL, NULL,
914
4.72k
      "xmlCompileStepPattern : no namespace bound to prefix %s\n",
915
4.72k
      prefix);
916
4.72k
        ctxt->error = 1;
917
4.72k
        goto error;
918
4.72k
    }
919
19.2k
      }
920
19.4k
      XML_PAT_FREE_STRING(ctxt, prefix);
921
19.4k
      name = NULL;
922
19.4k
      if (token == NULL) {
923
16.7k
    if (CUR == '*') {
924
16.3k
        NEXT;
925
16.3k
        PUSH(XML_OP_NS, URL, NULL);
926
16.3k
    } else {
927
435
        ERROR(NULL, NULL, NULL,
928
435
          "xmlCompileStepPattern : Name expected\n");
929
435
        ctxt->error = 1;
930
435
        goto error;
931
435
    }
932
16.7k
      } else {
933
2.65k
    PUSH(XML_OP_ELEM, token, URL);
934
2.65k
      }
935
19.4k
  } else {
936
9.15k
      NEXT;
937
9.15k
      if (xmlStrEqual(name, (const xmlChar *) "child")) {
938
5.44k
    XML_PAT_FREE_STRING(ctxt, name);
939
5.44k
    name = xmlPatScanName(ctxt);
940
5.44k
    if (name == NULL) {
941
733
        if (CUR == '*') {
942
342
      NEXT;
943
342
      PUSH(XML_OP_ALL, NULL, NULL);
944
341
      return;
945
391
        } else {
946
391
      ERROR(NULL, NULL, NULL,
947
391
          "xmlCompileStepPattern : QName expected\n");
948
391
      ctxt->error = 1;
949
391
      goto error;
950
391
        }
951
733
    }
952
4.70k
    if (CUR == ':') {
953
0
        xmlChar *prefix = name;
954
0
        int i;
955
956
0
        NEXT;
957
0
        if (IS_BLANK_CH(CUR)) {
958
0
      ERROR5(NULL, NULL, NULL, "Invalid QName.\n", NULL);
959
0
      ctxt->error = 1;
960
0
      goto error;
961
0
        }
962
        /*
963
        * This is a namespace match
964
        */
965
0
        token = xmlPatScanName(ctxt);
966
0
        if ((prefix[0] == 'x') &&
967
0
      (prefix[1] == 'm') &&
968
0
      (prefix[2] == 'l') &&
969
0
      (prefix[3] == 0))
970
0
        {
971
0
      XML_PAT_COPY_NSNAME(ctxt, URL, XML_XML_NAMESPACE)
972
0
        } else {
973
0
      for (i = 0;i < ctxt->nb_namespaces;i++) {
974
0
          if (xmlStrEqual(ctxt->namespaces[2 * i + 1], prefix)) {
975
0
        XML_PAT_COPY_NSNAME(ctxt, URL, ctxt->namespaces[2 * i])
976
0
        break;
977
0
          }
978
0
      }
979
0
      if (i >= ctxt->nb_namespaces) {
980
0
          ERROR5(NULL, NULL, NULL,
981
0
        "xmlCompileStepPattern : no namespace bound "
982
0
        "to prefix %s\n", prefix);
983
0
          ctxt->error = 1;
984
0
          goto error;
985
0
      }
986
0
        }
987
0
        XML_PAT_FREE_STRING(ctxt, prefix);
988
0
        name = NULL;
989
0
        if (token == NULL) {
990
0
      if (CUR == '*') {
991
0
          NEXT;
992
0
          PUSH(XML_OP_NS, URL, NULL);
993
0
      } else {
994
0
          ERROR(NULL, NULL, NULL,
995
0
        "xmlCompileStepPattern : Name expected\n");
996
0
          ctxt->error = 1;
997
0
          goto error;
998
0
      }
999
0
        } else {
1000
0
      PUSH(XML_OP_ELEM, token, URL);
1001
0
        }
1002
0
    } else
1003
4.70k
        PUSH(XML_OP_ELEM, name, NULL);
1004
4.70k
    return;
1005
4.70k
      } else if (xmlStrEqual(name, (const xmlChar *) "attribute")) {
1006
2.41k
    XML_PAT_FREE_STRING(ctxt, name)
1007
2.41k
    name = NULL;
1008
2.41k
    if (XML_STREAM_XS_IDC_SEL(ctxt->comp)) {
1009
666
        ERROR5(NULL, NULL, NULL,
1010
666
      "Unexpected attribute axis in '%s'.\n", ctxt->base);
1011
666
        ctxt->error = 1;
1012
666
        goto error;
1013
666
    }
1014
1.74k
    xmlCompileAttributeTest(ctxt);
1015
1.74k
    if (ctxt->error != 0)
1016
1.16k
        goto error;
1017
587
    return;
1018
1.74k
      } else {
1019
1.29k
    ERROR5(NULL, NULL, NULL,
1020
1.29k
        "The 'element' or 'attribute' axis is expected.\n", NULL);
1021
1.29k
    ctxt->error = 1;
1022
1.29k
    goto error;
1023
1.29k
      }
1024
9.15k
  }
1025
6.78M
    } else if (CUR == '*') {
1026
539
        if (name != NULL) {
1027
539
      ctxt->error = 1;
1028
539
      goto error;
1029
539
  }
1030
0
  NEXT;
1031
0
  PUSH(XML_OP_ALL, token, NULL);
1032
6.78M
    } else {
1033
6.78M
  PUSH(XML_OP_ELEM, name, NULL);
1034
6.78M
    }
1035
6.80M
    return;
1036
6.80M
error:
1037
14.2k
    if (URL != NULL)
1038
438
  XML_PAT_FREE_STRING(ctxt, URL)
1039
14.2k
    if (token != NULL)
1040
3.17k
  XML_PAT_FREE_STRING(ctxt, token)
1041
14.2k
    if (name != NULL)
1042
7.35k
  XML_PAT_FREE_STRING(ctxt, name)
1043
14.2k
}
1044
1045
/**
1046
 * Compile the Path Pattern and generates a precompiled
1047
 * form suitable for fast matching.
1048
 *
1049
 * [5]    Path    ::=    ('.//')? ( Step '/' )* ( Step | '@' NameTest )
1050
 *
1051
 * @param ctxt  the compilation context
1052
 */
1053
static void
1054
14.2k
xmlCompilePathPattern(xmlPatParserContextPtr ctxt) {
1055
14.2k
    SKIP_BLANKS;
1056
14.2k
    if (CUR == '/') {
1057
4.98k
        ctxt->comp->flags |= PAT_FROM_ROOT;
1058
9.24k
    } else if ((CUR == '.') || (ctxt->comp->flags & XML_PATTERN_NOTPATTERN)) {
1059
2.03k
        ctxt->comp->flags |= PAT_FROM_CUR;
1060
2.03k
    }
1061
1062
14.2k
    if ((CUR == '/') && (NXT(1) == '/')) {
1063
893
  PUSH(XML_OP_ANCESTOR, NULL, NULL);
1064
893
  NEXT;
1065
893
  NEXT;
1066
13.3k
    } else if ((CUR == '.') && (NXT(1) == '/') && (NXT(2) == '/')) {
1067
311
  PUSH(XML_OP_ANCESTOR, NULL, NULL);
1068
311
  NEXT;
1069
311
  NEXT;
1070
311
  NEXT;
1071
  /* Check for incompleteness. */
1072
311
  SKIP_BLANKS;
1073
311
  if (CUR == 0) {
1074
25
      ERROR5(NULL, NULL, NULL,
1075
25
         "Incomplete expression '%s'.\n", ctxt->base);
1076
25
      ctxt->error = 1;
1077
25
      goto error;
1078
25
  }
1079
311
    }
1080
14.2k
    if (CUR == '@') {
1081
1.23k
  NEXT;
1082
1.23k
  xmlCompileAttributeTest(ctxt);
1083
1.23k
        if (ctxt->error != 0)
1084
74
            goto error;
1085
1.15k
  SKIP_BLANKS;
1086
  /* TODO: check for incompleteness */
1087
1.15k
  if (CUR != 0) {
1088
494
      xmlCompileStepPattern(ctxt);
1089
494
      if (ctxt->error != 0)
1090
31
    goto error;
1091
494
  }
1092
12.9k
    } else {
1093
12.9k
        if (CUR == '/') {
1094
4.31k
      PUSH(XML_OP_ROOT, NULL, NULL);
1095
4.31k
      NEXT;
1096
      /* Check for incompleteness. */
1097
4.31k
      SKIP_BLANKS;
1098
4.31k
      if (CUR == 0) {
1099
31
    ERROR5(NULL, NULL, NULL,
1100
31
        "Incomplete expression '%s'.\n", ctxt->base);
1101
31
    ctxt->error = 1;
1102
31
    goto error;
1103
31
      }
1104
4.31k
  }
1105
12.9k
  xmlCompileStepPattern(ctxt);
1106
12.9k
  if (ctxt->error != 0)
1107
247
      goto error;
1108
12.6k
  SKIP_BLANKS;
1109
12.2M
  while (CUR == '/') {
1110
12.2M
      if (NXT(1) == '/') {
1111
617k
          PUSH(XML_OP_ANCESTOR, NULL, NULL);
1112
617k
    NEXT;
1113
617k
    NEXT;
1114
617k
    SKIP_BLANKS;
1115
617k
    xmlCompileStepPattern(ctxt);
1116
617k
    if (ctxt->error != 0)
1117
60
        goto error;
1118
11.6M
      } else {
1119
11.6M
          PUSH(XML_OP_PARENT, NULL, NULL);
1120
11.6M
    NEXT;
1121
11.6M
    SKIP_BLANKS;
1122
11.6M
    if (CUR == 0) {
1123
68
        ERROR5(NULL, NULL, NULL,
1124
68
        "Incomplete expression '%s'.\n", ctxt->base);
1125
68
        ctxt->error = 1;
1126
68
        goto error;
1127
68
    }
1128
11.6M
    xmlCompileStepPattern(ctxt);
1129
11.6M
    if (ctxt->error != 0)
1130
32
        goto error;
1131
11.6M
      }
1132
12.2M
  }
1133
12.6k
    }
1134
13.6k
    if (CUR != 0) {
1135
124
  ERROR5(NULL, NULL, NULL,
1136
124
         "Failed to compile pattern %s\n", ctxt->base);
1137
124
  ctxt->error = 1;
1138
124
    }
1139
14.2k
error:
1140
14.2k
    return;
1141
13.6k
}
1142
1143
/**
1144
 * Compile the Path Pattern and generates a precompiled
1145
 * form suitable for fast matching.
1146
 *
1147
 * [5]    Path    ::=    ('.//')? ( Step '/' )* ( Step | '@' NameTest )
1148
 *
1149
 * @param ctxt  the compilation context
1150
 */
1151
static void
1152
56.2k
xmlCompileIDCXPathPath(xmlPatParserContextPtr ctxt) {
1153
56.2k
    SKIP_BLANKS;
1154
56.2k
    if (CUR == '/') {
1155
630
  ERROR5(NULL, NULL, NULL,
1156
630
      "Unexpected selection of the document root in '%s'.\n",
1157
630
      ctxt->base);
1158
630
  goto error;
1159
630
    }
1160
55.5k
    ctxt->comp->flags |= PAT_FROM_CUR;
1161
1162
55.5k
    if (CUR == '.') {
1163
  /* "." - "self::node()" */
1164
10.4k
  NEXT;
1165
10.4k
  SKIP_BLANKS;
1166
10.4k
  if (CUR == 0) {
1167
      /*
1168
      * Selection of the context node.
1169
      */
1170
1.58k
      PUSH(XML_OP_ELEM, NULL, NULL);
1171
1.58k
      return;
1172
1.58k
  }
1173
8.83k
  if (CUR != '/') {
1174
      /* TODO: A more meaningful error message. */
1175
541
      ERROR5(NULL, NULL, NULL,
1176
541
      "Unexpected token after '.' in '%s'.\n", ctxt->base);
1177
541
      goto error;
1178
541
  }
1179
  /* "./" - "self::node()/" */
1180
8.29k
  NEXT;
1181
8.29k
  SKIP_BLANKS;
1182
8.29k
  if (CUR == '/') {
1183
5.93k
      if (IS_BLANK_CH(PEEKPREV(1))) {
1184
    /*
1185
    * Disallow "./ /"
1186
    */
1187
213
    ERROR5(NULL, NULL, NULL,
1188
213
        "Unexpected '/' token in '%s'.\n", ctxt->base);
1189
213
    goto error;
1190
213
      }
1191
      /* ".//" - "self:node()/descendant-or-self::node()/" */
1192
5.72k
      PUSH(XML_OP_ANCESTOR, NULL, NULL);
1193
5.72k
      NEXT;
1194
5.72k
      SKIP_BLANKS;
1195
5.72k
  }
1196
8.07k
  if (CUR == 0)
1197
433
      goto error_unfinished;
1198
8.07k
    }
1199
    /*
1200
    * Process steps.
1201
    */
1202
120k
    do {
1203
120k
  xmlCompileStepPattern(ctxt);
1204
120k
  if (ctxt->error != 0)
1205
18.0k
      goto error;
1206
102k
  SKIP_BLANKS;
1207
102k
  if (CUR != '/')
1208
33.7k
      break;
1209
68.6k
  PUSH(XML_OP_PARENT, NULL, NULL);
1210
68.6k
  NEXT;
1211
68.6k
  SKIP_BLANKS;
1212
68.6k
  if (CUR == '/') {
1213
      /*
1214
      * Disallow subsequent '//'.
1215
      */
1216
699
      ERROR5(NULL, NULL, NULL,
1217
699
    "Unexpected subsequent '//' in '%s'.\n",
1218
699
    ctxt->base);
1219
699
      goto error;
1220
699
  }
1221
67.9k
  if (CUR == 0)
1222
281
      goto error_unfinished;
1223
1224
67.9k
    } while (CUR != 0);
1225
1226
33.7k
    if (CUR != 0) {
1227
5.69k
  ERROR5(NULL, NULL, NULL,
1228
5.69k
      "Failed to compile expression '%s'.\n", ctxt->base);
1229
5.69k
  ctxt->error = 1;
1230
5.69k
    }
1231
33.7k
    return;
1232
20.1k
error:
1233
20.1k
    ctxt->error = 1;
1234
20.1k
    return;
1235
1236
714
error_unfinished:
1237
714
    ctxt->error = 1;
1238
714
    ERROR5(NULL, NULL, NULL,
1239
714
  "Unfinished expression '%s'.\n", ctxt->base);
1240
714
}
1241
1242
/************************************************************************
1243
 *                  *
1244
 *      The streaming code        *
1245
 *                  *
1246
 ************************************************************************/
1247
1248
/**
1249
 * build a new compiled pattern for streaming
1250
 *
1251
 * @param size  the number of expected steps
1252
 * @returns the new structure or NULL in case of error.
1253
 */
1254
static xmlStreamCompPtr
1255
42.4k
xmlNewStreamComp(int size) {
1256
42.4k
    xmlStreamCompPtr cur;
1257
1258
42.4k
    if (size < 4)
1259
37.4k
        size  = 4;
1260
1261
42.4k
    cur = (xmlStreamCompPtr) xmlMalloc(sizeof(xmlStreamComp));
1262
42.4k
    if (cur == NULL) {
1263
10
  ERROR(NULL, NULL, NULL,
1264
10
    "xmlNewStreamComp: malloc failed\n");
1265
10
  return(NULL);
1266
10
    }
1267
42.4k
    memset(cur, 0, sizeof(xmlStreamComp));
1268
42.4k
    cur->steps = (xmlStreamStepPtr) xmlMalloc(size * sizeof(xmlStreamStep));
1269
42.4k
    if (cur->steps == NULL) {
1270
7
  xmlFree(cur);
1271
7
  ERROR(NULL, NULL, NULL,
1272
7
        "xmlNewStreamComp: malloc failed\n");
1273
7
  return(NULL);
1274
7
    }
1275
42.4k
    cur->nbStep = 0;
1276
42.4k
    cur->maxStep = size;
1277
42.4k
    return(cur);
1278
42.4k
}
1279
1280
/**
1281
 * Free the compiled pattern for streaming
1282
 *
1283
 * @param comp  the compiled pattern for streaming
1284
 */
1285
static void
1286
42.4k
xmlFreeStreamComp(xmlStreamCompPtr comp) {
1287
42.4k
    if (comp != NULL) {
1288
42.4k
        if (comp->steps != NULL)
1289
42.4k
      xmlFree(comp->steps);
1290
42.4k
  if (comp->dict != NULL)
1291
0
      xmlDictFree(comp->dict);
1292
42.4k
        xmlFree(comp);
1293
42.4k
    }
1294
42.4k
}
1295
1296
/**
1297
 * Add a new step to the compiled pattern
1298
 *
1299
 * @param comp  the compiled pattern for streaming
1300
 * @param name  the first string, the name, or NULL for *
1301
 * @param ns  the second step, the namespace name
1302
 * @param nodeType  the node type
1303
 * @param flags  the flags for that step
1304
 * @returns -1 in case of error or the step index if successful
1305
 */
1306
static int
1307
xmlStreamCompAddStep(xmlStreamCompPtr comp, const xmlChar *name,
1308
7.66M
                     const xmlChar *ns, int nodeType, int flags) {
1309
7.66M
    xmlStreamStepPtr cur;
1310
1311
7.66M
    if (comp->nbStep >= comp->maxStep) {
1312
0
        xmlStreamStepPtr tmp;
1313
0
        int newSize;
1314
1315
0
        newSize = xmlGrowCapacity(comp->maxStep, sizeof(tmp[0]),
1316
0
                                  4, XML_MAX_ITEMS);
1317
0
        if (newSize < 0) {
1318
0
      ERROR(NULL, NULL, NULL,
1319
0
      "xmlNewStreamComp: growCapacity failed\n");
1320
0
      return(-1);
1321
0
        }
1322
0
  cur = xmlRealloc(comp->steps, newSize * sizeof(tmp[0]));
1323
0
  if (cur == NULL) {
1324
0
      ERROR(NULL, NULL, NULL,
1325
0
      "xmlNewStreamComp: malloc failed\n");
1326
0
      return(-1);
1327
0
  }
1328
0
  comp->steps = cur;
1329
0
        comp->maxStep = newSize;
1330
0
    }
1331
7.66M
    cur = &comp->steps[comp->nbStep++];
1332
7.66M
    cur->flags = flags;
1333
7.66M
    cur->name = name;
1334
7.66M
    cur->ns = ns;
1335
7.66M
    cur->nodeType = nodeType;
1336
7.66M
    return(comp->nbStep - 1);
1337
7.66M
}
1338
1339
/**
1340
 * Tries to stream compile a pattern
1341
 *
1342
 * @param comp  the precompiled pattern
1343
 * @returns -1 in case of failure and 0 in case of success.
1344
 */
1345
static int
1346
42.4k
xmlStreamCompile(xmlPatternPtr comp) {
1347
42.4k
    xmlStreamCompPtr stream;
1348
42.4k
    int i, s = 0, root = 0, flags = 0, prevs = -1;
1349
42.4k
    xmlStepOp step;
1350
1351
42.4k
    if ((comp == NULL) || (comp->steps == NULL))
1352
0
        return(-1);
1353
    /*
1354
     * special case for .
1355
     */
1356
42.4k
    if ((comp->nbStep == 1) &&
1357
22.4k
        (comp->steps[0].op == XML_OP_ELEM) &&
1358
18.3k
  (comp->steps[0].value == NULL) &&
1359
2.52k
  (comp->steps[0].value2 == NULL)) {
1360
2.52k
  stream = xmlNewStreamComp(0);
1361
2.52k
  if (stream == NULL)
1362
2
      return(-1);
1363
  /* Note that the stream will have no steps in this case. */
1364
2.52k
  stream->flags |= XML_STREAM_FINAL_IS_ANY_NODE;
1365
2.52k
  comp->stream = stream;
1366
2.52k
  return(0);
1367
2.52k
    }
1368
1369
39.9k
    stream = xmlNewStreamComp((comp->nbStep / 2) + 1);
1370
39.9k
    if (stream == NULL)
1371
15
        return(-1);
1372
39.9k
    if (comp->dict != NULL) {
1373
0
        stream->dict = comp->dict;
1374
0
  xmlDictReference(stream->dict);
1375
0
    }
1376
1377
39.9k
    i = 0;
1378
39.9k
    if (comp->flags & PAT_FROM_ROOT)
1379
4.80k
  stream->flags |= XML_STREAM_FROM_ROOT;
1380
1381
20.1M
    for (;i < comp->nbStep;i++) {
1382
20.1M
  step = comp->steps[i];
1383
20.1M
        switch (step.op) {
1384
0
      case XML_OP_END:
1385
0
          break;
1386
4.17k
      case XML_OP_ROOT:
1387
4.17k
          if (i != 0)
1388
206
        goto error;
1389
3.97k
    root = 1;
1390
3.97k
    break;
1391
5.64k
      case XML_OP_NS:
1392
5.64k
    s = xmlStreamCompAddStep(stream, NULL, step.value,
1393
5.64k
        XML_ELEMENT_NODE, flags);
1394
5.64k
    if (s < 0)
1395
0
        goto error;
1396
5.64k
    prevs = s;
1397
5.64k
    flags = 0;
1398
5.64k
    break;
1399
24.7k
      case XML_OP_ATTR:
1400
24.7k
    flags |= XML_STREAM_STEP_ATTR;
1401
24.7k
    prevs = -1;
1402
24.7k
    s = xmlStreamCompAddStep(stream,
1403
24.7k
        step.value, step.value2, XML_ATTRIBUTE_NODE, flags);
1404
24.7k
    flags = 0;
1405
24.7k
    if (s < 0)
1406
0
        goto error;
1407
24.7k
    break;
1408
8.17M
      case XML_OP_ELEM:
1409
8.17M
          if ((step.value == NULL) && (step.value2 == NULL)) {
1410
        /*
1411
        * We have a "." or "self::node()" here.
1412
        * Eliminate redundant self::node() tests like in "/./."
1413
        * or "//./"
1414
        * The only case we won't eliminate is "//.", i.e. if
1415
        * self::node() is the last node test and we had
1416
        * continuation somewhere beforehand.
1417
        */
1418
2.43M
        if ((comp->nbStep == i + 1) &&
1419
3.88k
      (flags & XML_STREAM_STEP_DESC)) {
1420
      /*
1421
      * Mark the special case where the expression resolves
1422
      * to any type of node.
1423
      */
1424
2.37k
      if (comp->nbStep == i + 1) {
1425
2.37k
          stream->flags |= XML_STREAM_FINAL_IS_ANY_NODE;
1426
2.37k
      }
1427
2.37k
      flags |= XML_STREAM_STEP_NODE;
1428
2.37k
      s = xmlStreamCompAddStep(stream, NULL, NULL,
1429
2.37k
          XML_STREAM_ANY_NODE, flags);
1430
2.37k
      if (s < 0)
1431
0
          goto error;
1432
2.37k
      flags = 0;
1433
      /*
1434
      * If there was a previous step, mark it to be added to
1435
      * the result node-set; this is needed since only
1436
      * the last step will be marked as "final" and only
1437
      * "final" nodes are added to the resulting set.
1438
      */
1439
2.37k
      if (prevs != -1) {
1440
425
          stream->steps[prevs].flags |= XML_STREAM_STEP_IN_SET;
1441
425
          prevs = -1;
1442
425
      }
1443
2.37k
      break;
1444
1445
2.43M
        } else {
1446
      /* Just skip this one. */
1447
2.43M
      continue;
1448
2.43M
        }
1449
2.43M
    }
1450
    /* An element node. */
1451
5.73M
          s = xmlStreamCompAddStep(stream, step.value, step.value2,
1452
5.73M
        XML_ELEMENT_NODE, flags);
1453
5.73M
    if (s < 0)
1454
0
        goto error;
1455
5.73M
    prevs = s;
1456
5.73M
    flags = 0;
1457
5.73M
    break;
1458
0
      case XML_OP_CHILD:
1459
    /* An element node child. */
1460
0
          s = xmlStreamCompAddStep(stream, step.value, step.value2,
1461
0
        XML_ELEMENT_NODE, flags);
1462
0
    if (s < 0)
1463
0
        goto error;
1464
0
    prevs = s;
1465
0
    flags = 0;
1466
0
    break;
1467
1.89M
      case XML_OP_ALL:
1468
1.89M
          s = xmlStreamCompAddStep(stream, NULL, NULL,
1469
1.89M
        XML_ELEMENT_NODE, flags);
1470
1.89M
    if (s < 0)
1471
0
        goto error;
1472
1.89M
    prevs = s;
1473
1.89M
    flags = 0;
1474
1.89M
    break;
1475
9.60M
      case XML_OP_PARENT:
1476
9.60M
          break;
1477
458k
      case XML_OP_ANCESTOR:
1478
    /* Skip redundant continuations. */
1479
458k
    if (flags & XML_STREAM_STEP_DESC)
1480
302
        break;
1481
458k
          flags |= XML_STREAM_STEP_DESC;
1482
    /*
1483
    * Mark the expression as having "//".
1484
    */
1485
458k
    if ((stream->flags & XML_STREAM_DESC) == 0)
1486
6.55k
        stream->flags |= XML_STREAM_DESC;
1487
458k
    break;
1488
20.1M
  }
1489
20.1M
    }
1490
39.6k
    if ((! root) && (comp->flags & XML_PATTERN_NOTPATTERN) == 0) {
1491
  /*
1492
  * If this should behave like a real pattern, we will mark
1493
  * the first step as having "//", to be reentrant on every
1494
  * tree level.
1495
  */
1496
7.68k
  if ((stream->flags & XML_STREAM_DESC) == 0)
1497
6.00k
      stream->flags |= XML_STREAM_DESC;
1498
1499
7.68k
  if (stream->nbStep > 0) {
1500
7.44k
      if ((stream->steps[0].flags & XML_STREAM_STEP_DESC) == 0)
1501
6.57k
    stream->steps[0].flags |= XML_STREAM_STEP_DESC;
1502
7.44k
  }
1503
7.68k
    }
1504
39.6k
    if (stream->nbStep <= s)
1505
587
  goto error;
1506
39.1k
    stream->steps[s].flags |= XML_STREAM_STEP_FINAL;
1507
39.1k
    if (root)
1508
3.72k
  stream->steps[0].flags |= XML_STREAM_STEP_ROOT;
1509
39.1k
    comp->stream = stream;
1510
39.1k
    return(0);
1511
793
error:
1512
793
    xmlFreeStreamComp(stream);
1513
793
    return(0);
1514
39.6k
}
1515
1516
/**
1517
 * build a new stream context
1518
 *
1519
 * @param stream  the copmiled stream
1520
 * @returns the new structure or NULL in case of error.
1521
 */
1522
static xmlStreamCtxtPtr
1523
110k
xmlNewStreamCtxt(xmlStreamCompPtr stream) {
1524
110k
    xmlStreamCtxtPtr cur;
1525
1526
110k
    cur = (xmlStreamCtxtPtr) xmlMalloc(sizeof(xmlStreamCtxt));
1527
110k
    if (cur == NULL) {
1528
13
  ERROR(NULL, NULL, NULL,
1529
13
    "xmlNewStreamCtxt: malloc failed\n");
1530
13
  return(NULL);
1531
13
    }
1532
110k
    memset(cur, 0, sizeof(xmlStreamCtxt));
1533
110k
    cur->states = NULL;
1534
110k
    cur->nbState = 0;
1535
110k
    cur->maxState = 0;
1536
110k
    cur->level = 0;
1537
110k
    cur->comp = stream;
1538
110k
    cur->blockLevel = -1;
1539
110k
    return(cur);
1540
110k
}
1541
1542
/**
1543
 * Free the stream context
1544
 *
1545
 * @param stream  the stream context
1546
 */
1547
void
1548
97.6k
xmlFreeStreamCtxt(xmlStreamCtxt *stream) {
1549
97.6k
    xmlStreamCtxtPtr next;
1550
1551
207k
    while (stream != NULL) {
1552
110k
        next = stream->next;
1553
110k
        if (stream->states != NULL)
1554
3.32k
      xmlFree(stream->states);
1555
110k
        xmlFree(stream);
1556
110k
  stream = next;
1557
110k
    }
1558
97.6k
}
1559
1560
/**
1561
 * Add a new state to the stream context
1562
 *
1563
 * @param comp  the stream context
1564
 * @param idx  the step index for that streaming state
1565
 * @param level  the level
1566
 * @returns -1 in case of error or the state index if successful
1567
 */
1568
static int
1569
4.13k
xmlStreamCtxtAddState(xmlStreamCtxtPtr comp, int idx, int level) {
1570
4.13k
    int i;
1571
4.76k
    for (i = 0;i < comp->nbState;i++) {
1572
628
        if (comp->states[2 * i] < 0) {
1573
0
      comp->states[2 * i] = idx;
1574
0
      comp->states[2 * i + 1] = level;
1575
0
      return(i);
1576
0
  }
1577
628
    }
1578
4.13k
    if (comp->nbState >= comp->maxState) {
1579
3.94k
        int *tmp;
1580
3.94k
        int newSize;
1581
1582
3.94k
        newSize = xmlGrowCapacity(comp->maxState, sizeof(tmp[0]) * 2,
1583
3.94k
                                  4, XML_MAX_ITEMS);
1584
3.94k
        if (newSize < 0) {
1585
0
      ERROR(NULL, NULL, NULL,
1586
0
      "xmlNewStreamCtxt: growCapacity failed\n");
1587
0
      return(-1);
1588
0
        }
1589
3.94k
  tmp = xmlRealloc(comp->states, newSize * sizeof(tmp[0]) * 2);
1590
3.94k
  if (tmp == NULL) {
1591
1
      ERROR(NULL, NULL, NULL,
1592
1
      "xmlNewStreamCtxt: malloc failed\n");
1593
1
      return(-1);
1594
1
  }
1595
3.94k
  comp->states = tmp;
1596
3.94k
        comp->maxState = newSize;
1597
3.94k
    }
1598
4.13k
    comp->states[2 * comp->nbState] = idx;
1599
4.13k
    comp->states[2 * comp->nbState++ + 1] = level;
1600
4.13k
    return(comp->nbState - 1);
1601
4.13k
}
1602
1603
/**
1604
 * Push new data onto the stream. NOTE: if the call #xmlPatterncompile
1605
 * indicated a dictionary, then strings for name and ns will be expected
1606
 * to come from the dictionary.
1607
 * Both `name` and `ns` being NULL means the / i.e. the root of the document.
1608
 * This can also act as a reset.
1609
 *
1610
 * @param stream  the stream context
1611
 * @param name  the current name
1612
 * @param ns  the namespace name
1613
 * @param nodeType  the type of the node
1614
 * @returns -1 in case of error, 1 if the current state in the stream is a
1615
 *    match and 0 otherwise.
1616
 */
1617
static int
1618
xmlStreamPushInternal(xmlStreamCtxtPtr stream,
1619
          const xmlChar *name, const xmlChar *ns,
1620
870k
          int nodeType) {
1621
870k
    int ret = 0, final = 0, tmp, i, m, match, stepNr, desc;
1622
870k
    xmlStreamCompPtr comp;
1623
870k
    xmlStreamStep step;
1624
1625
870k
    if ((stream == NULL) || (stream->nbState < 0))
1626
0
        return(-1);
1627
1628
1.97M
    while (stream != NULL) {
1629
1.10M
  comp = stream->comp;
1630
1631
1.10M
  if ((nodeType == XML_ELEMENT_NODE) &&
1632
923k
      (name == NULL) && (ns == NULL)) {
1633
      /* We have a document node here (or a reset). */
1634
6.99k
      stream->nbState = 0;
1635
6.99k
      stream->level = 0;
1636
6.99k
      stream->blockLevel = -1;
1637
6.99k
      if (comp->flags & XML_STREAM_FROM_ROOT) {
1638
3.12k
    if (comp->nbStep == 0) {
1639
        /* TODO: We have a "/." here? */
1640
0
        ret = 1;
1641
3.12k
    } else {
1642
3.12k
        if ((comp->nbStep == 1) &&
1643
1.90k
      (comp->steps[0].nodeType == XML_STREAM_ANY_NODE) &&
1644
199
      (comp->steps[0].flags & XML_STREAM_STEP_DESC))
1645
199
        {
1646
      /*
1647
      * In the case of "//." the document node will match
1648
      * as well.
1649
      */
1650
199
      ret = 1;
1651
2.92k
        } else if (comp->steps[0].flags & XML_STREAM_STEP_ROOT) {
1652
2.69k
      if (xmlStreamCtxtAddState(stream, 0, 0) < 0)
1653
0
                            return(-1);
1654
2.69k
        }
1655
3.12k
    }
1656
3.12k
      }
1657
6.99k
      stream = stream->next;
1658
6.99k
      continue; /* while */
1659
6.99k
  }
1660
1661
  /*
1662
  * Fast check for ".".
1663
  */
1664
1.09M
  if (comp->nbStep == 0) {
1665
      /*
1666
       * / and . are handled at the XPath node set creation
1667
       * level by checking min depth
1668
       */
1669
216k
      if (stream->flags & XML_PATTERN_XPATH) {
1670
0
    stream = stream->next;
1671
0
    continue; /* while */
1672
0
      }
1673
      /*
1674
      * For non-pattern like evaluation like XML Schema IDCs
1675
      * or traditional XPath expressions, this will match if
1676
      * we are at the first level only, otherwise on every level.
1677
      */
1678
216k
      if ((nodeType != XML_ATTRIBUTE_NODE) &&
1679
182k
    (((stream->flags & XML_PATTERN_NOTPATTERN) == 0) ||
1680
182k
    (stream->level == 0))) {
1681
46.7k
        ret = 1;
1682
46.7k
      }
1683
216k
      stream->level++;
1684
216k
      goto stream_next;
1685
216k
  }
1686
876k
  if (stream->blockLevel != -1) {
1687
      /*
1688
      * Skip blocked expressions.
1689
      */
1690
395k
      stream->level++;
1691
395k
      goto stream_next;
1692
395k
  }
1693
1694
480k
  if ((nodeType != XML_ELEMENT_NODE) &&
1695
81.1k
      (nodeType != XML_ATTRIBUTE_NODE) &&
1696
0
      ((comp->flags & XML_STREAM_FINAL_IS_ANY_NODE) == 0)) {
1697
      /*
1698
      * No need to process nodes of other types if we don't
1699
      * resolve to those types.
1700
      * TODO: Do we need to block the context here?
1701
      */
1702
0
      stream->level++;
1703
0
      goto stream_next;
1704
0
  }
1705
1706
  /*
1707
   * Check evolution of existing states
1708
   */
1709
480k
  i = 0;
1710
480k
  m = stream->nbState;
1711
484k
  while (i < m) {
1712
4.27k
      if ((comp->flags & XML_STREAM_DESC) == 0) {
1713
    /*
1714
    * If there is no "//", then only the last
1715
    * added state is of interest.
1716
    */
1717
3.74k
    stepNr = stream->states[2 * (stream->nbState -1)];
1718
    /*
1719
    * TODO: Security check, should not happen, remove it.
1720
    */
1721
3.74k
    if (stream->states[(2 * (stream->nbState -1)) + 1] <
1722
3.74k
        stream->level) {
1723
0
        return (-1);
1724
0
    }
1725
3.74k
    desc = 0;
1726
    /* loop-stopper */
1727
3.74k
    i = m;
1728
3.74k
      } else {
1729
    /*
1730
    * If there are "//", then we need to process every "//"
1731
    * occurring in the states, plus any other state for this
1732
    * level.
1733
    */
1734
529
    stepNr = stream->states[2 * i];
1735
1736
    /* TODO: should not happen anymore: dead states */
1737
529
    if (stepNr < 0)
1738
0
        goto next_state;
1739
1740
529
    tmp = stream->states[(2 * i) + 1];
1741
1742
    /* skip new states just added */
1743
529
    if (tmp > stream->level)
1744
0
        goto next_state;
1745
1746
    /* skip states at ancestor levels, except if "//" */
1747
529
    desc = comp->steps[stepNr].flags & XML_STREAM_STEP_DESC;
1748
529
    if ((tmp < stream->level) && (!desc))
1749
0
        goto next_state;
1750
529
      }
1751
      /*
1752
      * Check for correct node-type.
1753
      */
1754
4.27k
      step = comp->steps[stepNr];
1755
4.27k
      if (step.nodeType != nodeType) {
1756
477
    if (step.nodeType == XML_ATTRIBUTE_NODE) {
1757
        /*
1758
        * Block this expression for deeper evaluation.
1759
        */
1760
474
        if ((comp->flags & XML_STREAM_DESC) == 0)
1761
276
      stream->blockLevel = stream->level +1;
1762
474
        goto next_state;
1763
474
    } else if (step.nodeType != XML_STREAM_ANY_NODE)
1764
3
        goto next_state;
1765
477
      }
1766
      /*
1767
      * Compare local/namespace-name.
1768
      */
1769
3.79k
      match = 0;
1770
3.79k
      if (step.nodeType == XML_STREAM_ANY_NODE) {
1771
0
    match = 1;
1772
3.79k
      } else if (step.name == NULL) {
1773
1.09k
    if (step.ns == NULL) {
1774
        /*
1775
        * This lets through all elements/attributes.
1776
        */
1777
875
        match = 1;
1778
875
    } else if (ns != NULL)
1779
0
        match = xmlStrEqual(step.ns, ns);
1780
2.70k
      } else if (((step.ns != NULL) == (ns != NULL)) &&
1781
2.48k
    (name != NULL) &&
1782
2.48k
    (step.name[0] == name[0]) &&
1783
1.10k
    xmlStrEqual(step.name, name) &&
1784
365
    ((step.ns == ns) || xmlStrEqual(step.ns, ns)))
1785
365
      {
1786
365
    match = 1;
1787
365
      }
1788
3.79k
      if (match) {
1789
1.24k
    final = step.flags & XML_STREAM_STEP_FINAL;
1790
1.24k
                if (final) {
1791
612
                    ret = 1;
1792
628
                } else if (xmlStreamCtxtAddState(stream, stepNr + 1,
1793
628
                                                 stream->level + 1) < 0) {
1794
0
                    return(-1);
1795
0
                }
1796
1.24k
    if ((ret != 1) && (step.flags & XML_STREAM_STEP_IN_SET)) {
1797
        /*
1798
        * Check if we have a special case like "foo/bar//.", where
1799
        * "foo" is selected as well.
1800
        */
1801
5
        ret = 1;
1802
5
    }
1803
1.24k
      }
1804
3.79k
      if (((comp->flags & XML_STREAM_DESC) == 0) &&
1805
3.47k
    ((! match) || final))  {
1806
    /*
1807
    * Mark this expression as blocked for any evaluation at
1808
    * deeper levels. Note that this includes "/foo"
1809
    * expressions if the *pattern* behaviour is used.
1810
    */
1811
3.07k
    stream->blockLevel = stream->level +1;
1812
3.07k
      }
1813
4.27k
next_state:
1814
4.27k
      i++;
1815
4.27k
  }
1816
1817
480k
  stream->level++;
1818
1819
  /*
1820
  * Re/enter the expression.
1821
  * Don't reenter if it's an absolute expression like "/foo",
1822
  *   except "//foo".
1823
  */
1824
480k
  step = comp->steps[0];
1825
480k
  if (step.flags & XML_STREAM_STEP_ROOT)
1826
2.69k
      goto stream_next;
1827
1828
477k
  desc = step.flags & XML_STREAM_STEP_DESC;
1829
477k
  if (stream->flags & XML_PATTERN_NOTPATTERN) {
1830
      /*
1831
      * Re/enter the expression if it is a "descendant" one,
1832
      * or if we are at the 1st level of evaluation.
1833
      */
1834
1835
473k
      if (stream->level == 1) {
1836
56.3k
    if (XML_STREAM_XS_IDC(stream)) {
1837
        /*
1838
        * XS-IDC: The missing "self::node()" will always
1839
        * match the first given node.
1840
        */
1841
56.3k
        goto stream_next;
1842
56.3k
    } else
1843
0
        goto compare;
1844
56.3k
      }
1845
      /*
1846
      * A "//" is always reentrant.
1847
      */
1848
417k
      if (desc)
1849
187k
    goto compare;
1850
1851
      /*
1852
      * XS-IDC: Process the 2nd level, since the missing
1853
      * "self::node()" is responsible for the 2nd level being
1854
      * the real start level.
1855
      */
1856
229k
      if ((stream->level == 2) && XML_STREAM_XS_IDC(stream))
1857
64.4k
    goto compare;
1858
1859
165k
      goto stream_next;
1860
229k
  }
1861
1862
256k
compare:
1863
  /*
1864
  * Check expected node-type.
1865
  */
1866
256k
  if (step.nodeType != nodeType) {
1867
50.2k
      if (nodeType == XML_ATTRIBUTE_NODE)
1868
45.3k
    goto stream_next;
1869
4.87k
      else if (step.nodeType != XML_STREAM_ANY_NODE)
1870
4.67k
    goto stream_next;
1871
50.2k
  }
1872
  /*
1873
  * Compare local/namespace-name.
1874
  */
1875
206k
  match = 0;
1876
206k
  if (step.nodeType == XML_STREAM_ANY_NODE) {
1877
199
      match = 1;
1878
205k
  } else if (step.name == NULL) {
1879
1.54k
      if (step.ns == NULL) {
1880
    /*
1881
    * This lets through all elements/attributes.
1882
    */
1883
1.17k
    match = 1;
1884
1.17k
      } else if (ns != NULL)
1885
0
    match = xmlStrEqual(step.ns, ns);
1886
204k
  } else if (((step.ns != NULL) == (ns != NULL)) &&
1887
203k
      (name != NULL) &&
1888
203k
      (step.name[0] == name[0]) &&
1889
122k
      xmlStrEqual(step.name, name) &&
1890
85.9k
      ((step.ns == ns) || xmlStrEqual(step.ns, ns)))
1891
85.9k
  {
1892
85.9k
      match = 1;
1893
85.9k
  }
1894
206k
  final = step.flags & XML_STREAM_STEP_FINAL;
1895
206k
  if (match) {
1896
87.3k
      if (final) {
1897
86.5k
    ret = 1;
1898
86.5k
            } else if (xmlStreamCtxtAddState(stream, 1, stream->level) < 0) {
1899
1
                return(-1);
1900
1
            }
1901
87.3k
      if ((ret != 1) && (step.flags & XML_STREAM_STEP_IN_SET)) {
1902
    /*
1903
    * Check if we have a special case like "foo//.", where
1904
    * "foo" is selected as well.
1905
    */
1906
2
    ret = 1;
1907
2
      }
1908
87.3k
  }
1909
206k
  if (((comp->flags & XML_STREAM_DESC) == 0) &&
1910
54.4k
      ((! match) || final))  {
1911
      /*
1912
      * Mark this expression as blocked for any evaluation at
1913
      * deeper levels.
1914
      */
1915
54.0k
      stream->blockLevel = stream->level;
1916
54.0k
  }
1917
1918
1.09M
stream_next:
1919
1.09M
        stream = stream->next;
1920
1.09M
    } /* while stream != NULL */
1921
1922
870k
    return(ret);
1923
870k
}
1924
1925
/**
1926
 * Push new data onto the stream. NOTE: if the call #xmlPatterncompile
1927
 * indicated a dictionary, then strings for name and ns will be expected
1928
 * to come from the dictionary.
1929
 * Both `name` and `ns` being NULL means the / i.e. the root of the document.
1930
 * This can also act as a reset.
1931
 * Otherwise the function will act as if it has been given an element-node.
1932
 *
1933
 * @param stream  the stream context
1934
 * @param name  the current name
1935
 * @param ns  the namespace name
1936
 * @returns -1 in case of error, 1 if the current state in the stream is a
1937
 *    match and 0 otherwise.
1938
 */
1939
int
1940
xmlStreamPush(xmlStreamCtxt *stream,
1941
751k
              const xmlChar *name, const xmlChar *ns) {
1942
751k
    return (xmlStreamPushInternal(stream, name, ns, XML_ELEMENT_NODE));
1943
751k
}
1944
1945
/**
1946
 * Push new data onto the stream. NOTE: if the call #xmlPatterncompile
1947
 * indicated a dictionary, then strings for name and ns will be expected
1948
 * to come from the dictionary.
1949
 * Both `name` and `ns` being NULL means the / i.e. the root of the document.
1950
 * This can also act as a reset.
1951
 * Different from #xmlStreamPush this function can be fed with nodes of type:
1952
 * element-, attribute-, text-, cdata-section-, comment- and
1953
 * processing-instruction-node.
1954
 *
1955
 * @param stream  the stream context
1956
 * @param name  the current name
1957
 * @param ns  the namespace name
1958
 * @param nodeType  the type of the node being pushed
1959
 * @returns -1 in case of error, 1 if the current state in the stream is a
1960
 *    match and 0 otherwise.
1961
 */
1962
int
1963
xmlStreamPushNode(xmlStreamCtxt *stream,
1964
      const xmlChar *name, const xmlChar *ns,
1965
      int nodeType)
1966
0
{
1967
0
    return (xmlStreamPushInternal(stream, name, ns,
1968
0
  nodeType));
1969
0
}
1970
1971
/**
1972
 * Push new attribute data onto the stream.
1973
 *
1974
 * NOTE: If the call to #xmlPatterncompile indicated a dictionary,
1975
 * then strings for `name` and `ns` will be expected to come from
1976
 * the dictionary.
1977
 *
1978
 * Both `name` and `ns` being NULL means the root of the document.
1979
 * This can also act as a reset. Otherwise the function will act as
1980
 * if it has been given an attribute-node.
1981
 *
1982
 * @param stream  the stream context
1983
 * @param name  the current name
1984
 * @param ns  the namespace name
1985
 * @returns -1 in case of error, 1 if the current state in the stream
1986
 * is a match and 0 otherwise.
1987
 */
1988
int
1989
xmlStreamPushAttr(xmlStreamCtxt *stream,
1990
118k
      const xmlChar *name, const xmlChar *ns) {
1991
118k
    return (xmlStreamPushInternal(stream, name, ns, XML_ATTRIBUTE_NODE));
1992
118k
}
1993
1994
/**
1995
 * push one level from the stream.
1996
 *
1997
 * @param stream  the stream context
1998
 * @returns -1 in case of error, 0 otherwise.
1999
 */
2000
int
2001
845k
xmlStreamPop(xmlStreamCtxt *stream) {
2002
845k
    int i, lev;
2003
2004
845k
    if (stream == NULL)
2005
0
        return(-1);
2006
1.90M
    while (stream != NULL) {
2007
  /*
2008
  * Reset block-level.
2009
  */
2010
1.05M
  if (stream->blockLevel == stream->level)
2011
56.9k
      stream->blockLevel = -1;
2012
2013
  /*
2014
   *  stream->level can be zero when XML_FINAL_IS_ANY_NODE is set
2015
   *  (see the thread at
2016
   *  http://mail.gnome.org/archives/xslt/2008-July/msg00027.html)
2017
   */
2018
1.05M
  if (stream->level)
2019
1.05M
      stream->level--;
2020
  /*
2021
   * Check evolution of existing states
2022
   */
2023
1.05M
  for (i = stream->nbState -1; i >= 0; i--) {
2024
      /* discard obsoleted states */
2025
8.30k
      lev = stream->states[(2 * i) + 1];
2026
8.30k
      if (lev > stream->level)
2027
1.42k
    stream->nbState--;
2028
8.30k
      if (lev <= stream->level)
2029
6.88k
    break;
2030
8.30k
  }
2031
1.05M
  stream = stream->next;
2032
1.05M
    }
2033
845k
    return(0);
2034
845k
}
2035
2036
/**
2037
 * Query if the streaming pattern additionally needs to be fed with
2038
 * text-, cdata-section-, comment- and processing-instruction-nodes.
2039
 * If the result is 0 then only element-nodes and attribute-nodes
2040
 * need to be pushed.
2041
 *
2042
 * @param streamCtxt  the stream context
2043
 * @returns 1 in case of need of nodes of the above described types,
2044
 *          0 otherwise. -1 on API errors.
2045
 */
2046
int
2047
xmlStreamWantsAnyNode(xmlStreamCtxt *streamCtxt)
2048
0
{
2049
0
    if (streamCtxt == NULL)
2050
0
  return(-1);
2051
0
    while (streamCtxt != NULL) {
2052
0
  if (streamCtxt->comp->flags & XML_STREAM_FINAL_IS_ANY_NODE)
2053
0
      return(1);
2054
0
  streamCtxt = streamCtxt->next;
2055
0
    }
2056
0
    return(0);
2057
0
}
2058
2059
/************************************************************************
2060
 *                  *
2061
 *      The public interfaces       *
2062
 *                  *
2063
 ************************************************************************/
2064
2065
/**
2066
 * Compile a pattern.
2067
 *
2068
 * @since 2.13.0
2069
 *
2070
 * @param pattern  the pattern to compile
2071
 * @param dict  an optional dictionary for interned strings
2072
 * @param flags  compilation flags, see xmlPatternFlags
2073
 * @param namespaces  the prefix definitions, array of [URI, prefix] or NULL
2074
 * @param patternOut  output pattern
2075
 * @returns 0 on success, 1 on error, -1 if a memory allocation failed.
2076
 */
2077
int
2078
xmlPatternCompileSafe(const xmlChar *pattern, xmlDict *dict, int flags,
2079
34.7k
                      const xmlChar **namespaces, xmlPattern **patternOut) {
2080
34.7k
    xmlPatternPtr ret = NULL, cur;
2081
34.7k
    xmlPatParserContextPtr ctxt = NULL;
2082
34.7k
    const xmlChar *or, *start;
2083
34.7k
    xmlChar *tmp = NULL;
2084
34.7k
    int type = 0;
2085
34.7k
    int streamable = 1;
2086
34.7k
    int error;
2087
2088
34.7k
    if (patternOut == NULL)
2089
0
        return(1);
2090
2091
34.7k
    if (pattern == NULL) {
2092
0
        error = 1;
2093
0
        goto error;
2094
0
    }
2095
2096
34.7k
    start = pattern;
2097
34.7k
    or = start;
2098
77.8k
    while (*or != 0) {
2099
70.5k
  tmp = NULL;
2100
31.8M
  while ((*or != 0) && (*or != '|')) or++;
2101
70.5k
        if (*or == 0)
2102
22.3k
      ctxt = xmlNewPatParserContext(start, dict, namespaces);
2103
48.1k
  else {
2104
48.1k
      tmp = xmlStrndup(start, or - start);
2105
48.1k
      if (tmp != NULL) {
2106
48.1k
    ctxt = xmlNewPatParserContext(tmp, dict, namespaces);
2107
48.1k
      }
2108
48.1k
      or++;
2109
48.1k
  }
2110
70.5k
  if (ctxt == NULL) {
2111
48
            error = -1;
2112
48
            goto error;
2113
48
        }
2114
70.4k
  cur = xmlNewPattern();
2115
70.4k
  if (cur == NULL) {
2116
6
            error = -1;
2117
6
            goto error;
2118
6
        }
2119
  /*
2120
  * Assign string dict.
2121
  */
2122
70.4k
  if (dict) {
2123
0
      cur->dict = dict;
2124
0
      xmlDictReference(dict);
2125
0
  }
2126
70.4k
  if (ret == NULL)
2127
34.5k
      ret = cur;
2128
35.8k
  else {
2129
35.8k
      cur->next = ret->next;
2130
35.8k
      ret->next = cur;
2131
35.8k
  }
2132
70.4k
  cur->flags = flags;
2133
70.4k
  ctxt->comp = cur;
2134
2135
70.4k
  if (XML_STREAM_XS_IDC(cur))
2136
56.2k
      xmlCompileIDCXPathPath(ctxt);
2137
14.2k
  else
2138
14.2k
      xmlCompilePathPattern(ctxt);
2139
70.4k
  if (ctxt->error != 0) {
2140
27.2k
            error = ctxt->error;
2141
27.2k
      goto error;
2142
27.2k
        }
2143
43.1k
  xmlFreePatParserContext(ctxt);
2144
43.1k
  ctxt = NULL;
2145
2146
2147
43.1k
        if (streamable) {
2148
42.4k
      if (type == 0) {
2149
23.2k
          type = cur->flags & (PAT_FROM_ROOT | PAT_FROM_CUR);
2150
23.2k
      } else if (type == PAT_FROM_ROOT) {
2151
6.18k
          if (cur->flags & PAT_FROM_CUR)
2152
25
        streamable = 0;
2153
13.0k
      } else if (type == PAT_FROM_CUR) {
2154
13.0k
          if (cur->flags & PAT_FROM_ROOT)
2155
12
        streamable = 0;
2156
13.0k
      }
2157
42.4k
  }
2158
43.1k
  if (streamable) {
2159
42.4k
      error = xmlStreamCompile(cur);
2160
42.4k
            if (error != 0)
2161
17
                goto error;
2162
42.4k
        }
2163
43.1k
  error = xmlReversePattern(cur);
2164
43.1k
        if (error != 0)
2165
7
      goto error;
2166
43.1k
  if (tmp != NULL) {
2167
35.9k
      xmlFree(tmp);
2168
35.9k
      tmp = NULL;
2169
35.9k
  }
2170
43.1k
  start = or;
2171
43.1k
    }
2172
7.37k
    if (streamable == 0) {
2173
35
        cur = ret;
2174
994
  while (cur != NULL) {
2175
959
      if (cur->stream != NULL) {
2176
261
    xmlFreeStreamComp(cur->stream);
2177
261
    cur->stream = NULL;
2178
261
      }
2179
959
      cur = cur->next;
2180
959
  }
2181
35
    }
2182
2183
7.37k
    *patternOut = ret;
2184
7.37k
    return(0);
2185
27.3k
error:
2186
27.3k
    if (ctxt != NULL) xmlFreePatParserContext(ctxt);
2187
27.3k
    if (ret != NULL) xmlFreePattern(ret);
2188
27.3k
    if (tmp != NULL) xmlFree(tmp);
2189
27.3k
    *patternOut = NULL;
2190
27.3k
    return(error);
2191
34.7k
}
2192
2193
/**
2194
 * Compile a pattern.
2195
 *
2196
 * @param pattern  the pattern to compile
2197
 * @param dict  an optional dictionary for interned strings
2198
 * @param flags  compilation flags, see xmlPatternFlags
2199
 * @param namespaces  the prefix definitions, array of [URI, prefix] or NULL
2200
 * @returns the compiled form of the pattern or NULL in case of error
2201
 */
2202
xmlPattern *
2203
xmlPatterncompile(const xmlChar *pattern, xmlDict *dict, int flags,
2204
33.0k
                  const xmlChar **namespaces) {
2205
33.0k
    xmlPatternPtr ret;
2206
33.0k
    xmlPatternCompileSafe(pattern, dict, flags, namespaces, &ret);
2207
33.0k
    return(ret);
2208
33.0k
}
2209
2210
/**
2211
 * Test whether the node matches the pattern
2212
 *
2213
 * @param comp  the precompiled pattern
2214
 * @param node  a node
2215
 * @returns 1 if it matches, 0 if it doesn't and -1 in case of failure
2216
 */
2217
int
2218
xmlPatternMatch(xmlPattern *comp, xmlNode *node)
2219
425
{
2220
425
    int ret = 0;
2221
2222
425
    if ((comp == NULL) || (node == NULL))
2223
0
        return(-1);
2224
2225
5.32k
    while (comp != NULL) {
2226
4.99k
        ret = xmlPatMatch(comp, node);
2227
4.99k
  if (ret != 0)
2228
100
      return(ret);
2229
4.89k
  comp = comp->next;
2230
4.89k
    }
2231
325
    return(ret);
2232
425
}
2233
2234
/**
2235
 * Get a streaming context for that pattern
2236
 * Use #xmlFreeStreamCtxt to free the context.
2237
 *
2238
 * @param comp  the precompiled pattern
2239
 * @returns a pointer to the context or NULL in case of failure
2240
 */
2241
xmlStreamCtxt *
2242
xmlPatternGetStreamCtxt(xmlPattern *comp)
2243
97.7k
{
2244
97.7k
    xmlStreamCtxtPtr ret = NULL, cur;
2245
2246
97.7k
    if ((comp == NULL) || (comp->stream == NULL))
2247
22
        return(NULL);
2248
2249
207k
    while (comp != NULL) {
2250
110k
        if (comp->stream == NULL)
2251
12
      goto failed;
2252
110k
  cur = xmlNewStreamCtxt(comp->stream);
2253
110k
  if (cur == NULL)
2254
13
      goto failed;
2255
110k
  if (ret == NULL)
2256
97.6k
      ret = cur;
2257
12.4k
  else {
2258
12.4k
      cur->next = ret->next;
2259
12.4k
      ret->next = cur;
2260
12.4k
  }
2261
110k
  cur->flags = comp->flags;
2262
110k
  comp = comp->next;
2263
110k
    }
2264
97.6k
    return(ret);
2265
25
failed:
2266
25
    xmlFreeStreamCtxt(ret);
2267
25
    return(NULL);
2268
97.6k
}
2269
2270
/**
2271
 * Check if the pattern is streamable i.e. #xmlPatternGetStreamCtxt
2272
 * should work.
2273
 *
2274
 * @param comp  the precompiled pattern
2275
 * @returns 1 if streamable, 0 if not and -1 in case of error.
2276
 */
2277
int
2278
0
xmlPatternStreamable(xmlPattern *comp) {
2279
0
    if (comp == NULL)
2280
0
        return(-1);
2281
0
    while (comp != NULL) {
2282
0
        if (comp->stream == NULL)
2283
0
      return(0);
2284
0
  comp = comp->next;
2285
0
    }
2286
0
    return(1);
2287
0
}
2288
2289
/**
2290
 * Check the maximum depth reachable by a pattern
2291
 *
2292
 * @param comp  the precompiled pattern
2293
 * @returns -2 if no limit (using //), otherwise the depth,
2294
 *         and -1 in case of error
2295
 */
2296
int
2297
0
xmlPatternMaxDepth(xmlPattern *comp) {
2298
0
    int ret = 0, i;
2299
0
    if (comp == NULL)
2300
0
        return(-1);
2301
0
    while (comp != NULL) {
2302
0
        if (comp->stream == NULL)
2303
0
      return(-1);
2304
0
  for (i = 0;i < comp->stream->nbStep;i++)
2305
0
      if (comp->stream->steps[i].flags & XML_STREAM_STEP_DESC)
2306
0
          return(-2);
2307
0
  if (comp->stream->nbStep > ret)
2308
0
      ret = comp->stream->nbStep;
2309
0
  comp = comp->next;
2310
0
    }
2311
0
    return(ret);
2312
0
}
2313
2314
/**
2315
 * Check the minimum depth reachable by a pattern, 0 mean the / or . are
2316
 * part of the set.
2317
 *
2318
 * @param comp  the precompiled pattern
2319
 * @returns -1 in case of error otherwise the depth,
2320
 */
2321
int
2322
0
xmlPatternMinDepth(xmlPattern *comp) {
2323
0
    int ret = 12345678;
2324
0
    if (comp == NULL)
2325
0
        return(-1);
2326
0
    while (comp != NULL) {
2327
0
        if (comp->stream == NULL)
2328
0
      return(-1);
2329
0
  if (comp->stream->nbStep < ret)
2330
0
      ret = comp->stream->nbStep;
2331
0
  if (ret == 0)
2332
0
      return(0);
2333
0
  comp = comp->next;
2334
0
    }
2335
0
    return(ret);
2336
0
}
2337
2338
/**
2339
 * Check if the pattern must be looked at from the root.
2340
 *
2341
 * @param comp  the precompiled pattern
2342
 * @returns 1 if true, 0 if false and -1 in case of error
2343
 */
2344
int
2345
0
xmlPatternFromRoot(xmlPattern *comp) {
2346
0
    if (comp == NULL)
2347
0
        return(-1);
2348
0
    while (comp != NULL) {
2349
0
        if (comp->stream == NULL)
2350
0
      return(-1);
2351
0
  if (comp->flags & PAT_FROM_ROOT)
2352
0
      return(1);
2353
0
  comp = comp->next;
2354
0
    }
2355
0
    return(0);
2356
2357
0
}
2358
2359
#endif /* LIBXML_PATTERN_ENABLED */