Coverage Report

Created: 2025-07-18 06:08

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