Coverage Report

Created: 2024-02-04 06:18

/src/libxml2/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
  SKIP_BLANKS;
1246
  /* TODO: check for incompleteness */
1247
0
  if (CUR != 0) {
1248
0
      xmlCompileStepPattern(ctxt);
1249
0
      if (ctxt->error != 0)
1250
0
    goto error;
1251
0
  }
1252
0
    } else {
1253
0
        if (CUR == '/') {
1254
0
      PUSH(XML_OP_ROOT, NULL, NULL);
1255
0
      NEXT;
1256
      /* Check for incompleteness. */
1257
0
      SKIP_BLANKS;
1258
0
      if (CUR == 0) {
1259
0
    ERROR5(NULL, NULL, NULL,
1260
0
        "Incomplete expression '%s'.\n", ctxt->base);
1261
0
    ctxt->error = 1;
1262
0
    goto error;
1263
0
      }
1264
0
  }
1265
0
  xmlCompileStepPattern(ctxt);
1266
0
  if (ctxt->error != 0)
1267
0
      goto error;
1268
0
  SKIP_BLANKS;
1269
0
  while (CUR == '/') {
1270
0
      if (NXT(1) == '/') {
1271
0
          PUSH(XML_OP_ANCESTOR, NULL, NULL);
1272
0
    NEXT;
1273
0
    NEXT;
1274
0
    SKIP_BLANKS;
1275
0
    xmlCompileStepPattern(ctxt);
1276
0
    if (ctxt->error != 0)
1277
0
        goto error;
1278
0
      } else {
1279
0
          PUSH(XML_OP_PARENT, NULL, NULL);
1280
0
    NEXT;
1281
0
    SKIP_BLANKS;
1282
0
    if (CUR == 0) {
1283
0
        ERROR5(NULL, NULL, NULL,
1284
0
        "Incomplete expression '%s'.\n", ctxt->base);
1285
0
        ctxt->error = 1;
1286
0
        goto error;
1287
0
    }
1288
0
    xmlCompileStepPattern(ctxt);
1289
0
    if (ctxt->error != 0)
1290
0
        goto error;
1291
0
      }
1292
0
  }
1293
0
    }
1294
0
    if (CUR != 0) {
1295
0
  ERROR5(NULL, NULL, NULL,
1296
0
         "Failed to compile pattern %s\n", ctxt->base);
1297
0
  ctxt->error = 1;
1298
0
    }
1299
0
error:
1300
0
    return;
1301
0
}
1302
1303
/**
1304
 * xmlCompileIDCXPathPath:
1305
 * @ctxt:  the compilation context
1306
 *
1307
 * Compile the Path Pattern and generates a precompiled
1308
 * form suitable for fast matching.
1309
 *
1310
 * [5]    Path    ::=    ('.//')? ( Step '/' )* ( Step | '@' NameTest )
1311
 */
1312
static void
1313
0
xmlCompileIDCXPathPath(xmlPatParserContextPtr ctxt) {
1314
0
    SKIP_BLANKS;
1315
0
    if (CUR == '/') {
1316
0
  ERROR5(NULL, NULL, NULL,
1317
0
      "Unexpected selection of the document root in '%s'.\n",
1318
0
      ctxt->base);
1319
0
  goto error;
1320
0
    }
1321
0
    ctxt->comp->flags |= PAT_FROM_CUR;
1322
1323
0
    if (CUR == '.') {
1324
  /* "." - "self::node()" */
1325
0
  NEXT;
1326
0
  SKIP_BLANKS;
1327
0
  if (CUR == 0) {
1328
      /*
1329
      * Selection of the context node.
1330
      */
1331
0
      PUSH(XML_OP_ELEM, NULL, NULL);
1332
0
      return;
1333
0
  }
1334
0
  if (CUR != '/') {
1335
      /* TODO: A more meaningful error message. */
1336
0
      ERROR5(NULL, NULL, NULL,
1337
0
      "Unexpected token after '.' in '%s'.\n", ctxt->base);
1338
0
      goto error;
1339
0
  }
1340
  /* "./" - "self::node()/" */
1341
0
  NEXT;
1342
0
  SKIP_BLANKS;
1343
0
  if (CUR == '/') {
1344
0
      if (IS_BLANK_CH(PEEKPREV(1))) {
1345
    /*
1346
    * Disallow "./ /"
1347
    */
1348
0
    ERROR5(NULL, NULL, NULL,
1349
0
        "Unexpected '/' token in '%s'.\n", ctxt->base);
1350
0
    goto error;
1351
0
      }
1352
      /* ".//" - "self:node()/descendant-or-self::node()/" */
1353
0
      PUSH(XML_OP_ANCESTOR, NULL, NULL);
1354
0
      NEXT;
1355
0
      SKIP_BLANKS;
1356
0
  }
1357
0
  if (CUR == 0)
1358
0
      goto error_unfinished;
1359
0
    }
1360
    /*
1361
    * Process steps.
1362
    */
1363
0
    do {
1364
0
  xmlCompileStepPattern(ctxt);
1365
0
  if (ctxt->error != 0)
1366
0
      goto error;
1367
0
  SKIP_BLANKS;
1368
0
  if (CUR != '/')
1369
0
      break;
1370
0
  PUSH(XML_OP_PARENT, NULL, NULL);
1371
0
  NEXT;
1372
0
  SKIP_BLANKS;
1373
0
  if (CUR == '/') {
1374
      /*
1375
      * Disallow subsequent '//'.
1376
      */
1377
0
      ERROR5(NULL, NULL, NULL,
1378
0
    "Unexpected subsequent '//' in '%s'.\n",
1379
0
    ctxt->base);
1380
0
      goto error;
1381
0
  }
1382
0
  if (CUR == 0)
1383
0
      goto error_unfinished;
1384
1385
0
    } while (CUR != 0);
1386
1387
0
    if (CUR != 0) {
1388
0
  ERROR5(NULL, NULL, NULL,
1389
0
      "Failed to compile expression '%s'.\n", ctxt->base);
1390
0
  ctxt->error = 1;
1391
0
    }
1392
0
    return;
1393
0
error:
1394
0
    ctxt->error = 1;
1395
0
    return;
1396
1397
0
error_unfinished:
1398
0
    ctxt->error = 1;
1399
0
    ERROR5(NULL, NULL, NULL,
1400
0
  "Unfinished expression '%s'.\n", ctxt->base);
1401
0
    return;
1402
0
}
1403
1404
/************************************************************************
1405
 *                  *
1406
 *      The streaming code        *
1407
 *                  *
1408
 ************************************************************************/
1409
1410
/**
1411
 * xmlNewStreamComp:
1412
 * @size: the number of expected steps
1413
 *
1414
 * build a new compiled pattern for streaming
1415
 *
1416
 * Returns the new structure or NULL in case of error.
1417
 */
1418
static xmlStreamCompPtr
1419
0
xmlNewStreamComp(int size) {
1420
0
    xmlStreamCompPtr cur;
1421
1422
0
    if (size < 4)
1423
0
        size  = 4;
1424
1425
0
    cur = (xmlStreamCompPtr) xmlMalloc(sizeof(xmlStreamComp));
1426
0
    if (cur == NULL) {
1427
0
  ERROR(NULL, NULL, NULL,
1428
0
    "xmlNewStreamComp: malloc failed\n");
1429
0
  return(NULL);
1430
0
    }
1431
0
    memset(cur, 0, sizeof(xmlStreamComp));
1432
0
    cur->steps = (xmlStreamStepPtr) xmlMalloc(size * sizeof(xmlStreamStep));
1433
0
    if (cur->steps == NULL) {
1434
0
  xmlFree(cur);
1435
0
  ERROR(NULL, NULL, NULL,
1436
0
        "xmlNewStreamComp: malloc failed\n");
1437
0
  return(NULL);
1438
0
    }
1439
0
    cur->nbStep = 0;
1440
0
    cur->maxStep = size;
1441
0
    return(cur);
1442
0
}
1443
1444
/**
1445
 * xmlFreeStreamComp:
1446
 * @comp: the compiled pattern for streaming
1447
 *
1448
 * Free the compiled pattern for streaming
1449
 */
1450
static void
1451
0
xmlFreeStreamComp(xmlStreamCompPtr comp) {
1452
0
    if (comp != NULL) {
1453
0
        if (comp->steps != NULL)
1454
0
      xmlFree(comp->steps);
1455
0
  if (comp->dict != NULL)
1456
0
      xmlDictFree(comp->dict);
1457
0
        xmlFree(comp);
1458
0
    }
1459
0
}
1460
1461
/**
1462
 * xmlStreamCompAddStep:
1463
 * @comp: the compiled pattern for streaming
1464
 * @name: the first string, the name, or NULL for *
1465
 * @ns: the second step, the namespace name
1466
 * @flags: the flags for that step
1467
 *
1468
 * Add a new step to the compiled pattern
1469
 *
1470
 * Returns -1 in case of error or the step index if successful
1471
 */
1472
static int
1473
xmlStreamCompAddStep(xmlStreamCompPtr comp, const xmlChar *name,
1474
0
                     const xmlChar *ns, int nodeType, int flags) {
1475
0
    xmlStreamStepPtr cur;
1476
1477
0
    if (comp->nbStep >= comp->maxStep) {
1478
0
  cur = (xmlStreamStepPtr) xmlRealloc(comp->steps,
1479
0
         comp->maxStep * 2 * sizeof(xmlStreamStep));
1480
0
  if (cur == NULL) {
1481
0
      ERROR(NULL, NULL, NULL,
1482
0
      "xmlNewStreamComp: malloc failed\n");
1483
0
      return(-1);
1484
0
  }
1485
0
  comp->steps = cur;
1486
0
        comp->maxStep *= 2;
1487
0
    }
1488
0
    cur = &comp->steps[comp->nbStep++];
1489
0
    cur->flags = flags;
1490
0
    cur->name = name;
1491
0
    cur->ns = ns;
1492
0
    cur->nodeType = nodeType;
1493
0
    return(comp->nbStep - 1);
1494
0
}
1495
1496
/**
1497
 * xmlStreamCompile:
1498
 * @comp: the precompiled pattern
1499
 *
1500
 * Tries to stream compile a pattern
1501
 *
1502
 * Returns -1 in case of failure and 0 in case of success.
1503
 */
1504
static int
1505
0
xmlStreamCompile(xmlPatternPtr comp) {
1506
0
    xmlStreamCompPtr stream;
1507
0
    int i, s = 0, root = 0, flags = 0, prevs = -1;
1508
0
    xmlStepOp step;
1509
1510
0
    if ((comp == NULL) || (comp->steps == NULL))
1511
0
        return(-1);
1512
    /*
1513
     * special case for .
1514
     */
1515
0
    if ((comp->nbStep == 1) &&
1516
0
        (comp->steps[0].op == XML_OP_ELEM) &&
1517
0
  (comp->steps[0].value == NULL) &&
1518
0
  (comp->steps[0].value2 == NULL)) {
1519
0
  stream = xmlNewStreamComp(0);
1520
0
  if (stream == NULL)
1521
0
      return(-1);
1522
  /* Note that the stream will have no steps in this case. */
1523
0
  stream->flags |= XML_STREAM_FINAL_IS_ANY_NODE;
1524
0
  comp->stream = stream;
1525
0
  return(0);
1526
0
    }
1527
1528
0
    stream = xmlNewStreamComp((comp->nbStep / 2) + 1);
1529
0
    if (stream == NULL)
1530
0
        return(-1);
1531
0
    if (comp->dict != NULL) {
1532
0
        stream->dict = comp->dict;
1533
0
  xmlDictReference(stream->dict);
1534
0
    }
1535
1536
0
    i = 0;
1537
0
    if (comp->flags & PAT_FROM_ROOT)
1538
0
  stream->flags |= XML_STREAM_FROM_ROOT;
1539
1540
0
    for (;i < comp->nbStep;i++) {
1541
0
  step = comp->steps[i];
1542
0
        switch (step.op) {
1543
0
      case XML_OP_END:
1544
0
          break;
1545
0
      case XML_OP_ROOT:
1546
0
          if (i != 0)
1547
0
        goto error;
1548
0
    root = 1;
1549
0
    break;
1550
0
      case XML_OP_NS:
1551
0
    s = xmlStreamCompAddStep(stream, NULL, step.value,
1552
0
        XML_ELEMENT_NODE, flags);
1553
0
    if (s < 0)
1554
0
        goto error;
1555
0
    prevs = s;
1556
0
    flags = 0;
1557
0
    break;
1558
0
      case XML_OP_ATTR:
1559
0
    flags |= XML_STREAM_STEP_ATTR;
1560
0
    prevs = -1;
1561
0
    s = xmlStreamCompAddStep(stream,
1562
0
        step.value, step.value2, XML_ATTRIBUTE_NODE, flags);
1563
0
    flags = 0;
1564
0
    if (s < 0)
1565
0
        goto error;
1566
0
    break;
1567
0
      case XML_OP_ELEM:
1568
0
          if ((step.value == NULL) && (step.value2 == NULL)) {
1569
        /*
1570
        * We have a "." or "self::node()" here.
1571
        * Eliminate redundant self::node() tests like in "/./."
1572
        * or "//./"
1573
        * The only case we won't eliminate is "//.", i.e. if
1574
        * self::node() is the last node test and we had
1575
        * continuation somewhere beforehand.
1576
        */
1577
0
        if ((comp->nbStep == i + 1) &&
1578
0
      (flags & XML_STREAM_STEP_DESC)) {
1579
      /*
1580
      * Mark the special case where the expression resolves
1581
      * to any type of node.
1582
      */
1583
0
      if (comp->nbStep == i + 1) {
1584
0
          stream->flags |= XML_STREAM_FINAL_IS_ANY_NODE;
1585
0
      }
1586
0
      flags |= XML_STREAM_STEP_NODE;
1587
0
      s = xmlStreamCompAddStep(stream, NULL, NULL,
1588
0
          XML_STREAM_ANY_NODE, flags);
1589
0
      if (s < 0)
1590
0
          goto error;
1591
0
      flags = 0;
1592
      /*
1593
      * If there was a previous step, mark it to be added to
1594
      * the result node-set; this is needed since only
1595
      * the last step will be marked as "final" and only
1596
      * "final" nodes are added to the resulting set.
1597
      */
1598
0
      if (prevs != -1) {
1599
0
          stream->steps[prevs].flags |= XML_STREAM_STEP_IN_SET;
1600
0
          prevs = -1;
1601
0
      }
1602
0
      break;
1603
1604
0
        } else {
1605
      /* Just skip this one. */
1606
0
      continue;
1607
0
        }
1608
0
    }
1609
    /* An element node. */
1610
0
          s = xmlStreamCompAddStep(stream, step.value, step.value2,
1611
0
        XML_ELEMENT_NODE, flags);
1612
0
    if (s < 0)
1613
0
        goto error;
1614
0
    prevs = s;
1615
0
    flags = 0;
1616
0
    break;
1617
0
      case XML_OP_CHILD:
1618
    /* An element node child. */
1619
0
          s = xmlStreamCompAddStep(stream, step.value, step.value2,
1620
0
        XML_ELEMENT_NODE, flags);
1621
0
    if (s < 0)
1622
0
        goto error;
1623
0
    prevs = s;
1624
0
    flags = 0;
1625
0
    break;
1626
0
      case XML_OP_ALL:
1627
0
          s = xmlStreamCompAddStep(stream, NULL, NULL,
1628
0
        XML_ELEMENT_NODE, flags);
1629
0
    if (s < 0)
1630
0
        goto error;
1631
0
    prevs = s;
1632
0
    flags = 0;
1633
0
    break;
1634
0
      case XML_OP_PARENT:
1635
0
          break;
1636
0
      case XML_OP_ANCESTOR:
1637
    /* Skip redundant continuations. */
1638
0
    if (flags & XML_STREAM_STEP_DESC)
1639
0
        break;
1640
0
          flags |= XML_STREAM_STEP_DESC;
1641
    /*
1642
    * Mark the expression as having "//".
1643
    */
1644
0
    if ((stream->flags & XML_STREAM_DESC) == 0)
1645
0
        stream->flags |= XML_STREAM_DESC;
1646
0
    break;
1647
0
  }
1648
0
    }
1649
0
    if ((! root) && (comp->flags & XML_PATTERN_NOTPATTERN) == 0) {
1650
  /*
1651
  * If this should behave like a real pattern, we will mark
1652
  * the first step as having "//", to be reentrant on every
1653
  * tree level.
1654
  */
1655
0
  if ((stream->flags & XML_STREAM_DESC) == 0)
1656
0
      stream->flags |= XML_STREAM_DESC;
1657
1658
0
  if (stream->nbStep > 0) {
1659
0
      if ((stream->steps[0].flags & XML_STREAM_STEP_DESC) == 0)
1660
0
    stream->steps[0].flags |= XML_STREAM_STEP_DESC;
1661
0
  }
1662
0
    }
1663
0
    if (stream->nbStep <= s)
1664
0
  goto error;
1665
0
    stream->steps[s].flags |= XML_STREAM_STEP_FINAL;
1666
0
    if (root)
1667
0
  stream->steps[0].flags |= XML_STREAM_STEP_ROOT;
1668
0
    comp->stream = stream;
1669
0
    return(0);
1670
0
error:
1671
0
    xmlFreeStreamComp(stream);
1672
0
    return(0);
1673
0
}
1674
1675
/**
1676
 * xmlNewStreamCtxt:
1677
 * @size: the number of expected states
1678
 *
1679
 * build a new stream context
1680
 *
1681
 * Returns the new structure or NULL in case of error.
1682
 */
1683
static xmlStreamCtxtPtr
1684
0
xmlNewStreamCtxt(xmlStreamCompPtr stream) {
1685
0
    xmlStreamCtxtPtr cur;
1686
1687
0
    cur = (xmlStreamCtxtPtr) xmlMalloc(sizeof(xmlStreamCtxt));
1688
0
    if (cur == NULL) {
1689
0
  ERROR(NULL, NULL, NULL,
1690
0
    "xmlNewStreamCtxt: malloc failed\n");
1691
0
  return(NULL);
1692
0
    }
1693
0
    memset(cur, 0, sizeof(xmlStreamCtxt));
1694
0
    cur->states = (int *) xmlMalloc(4 * 2 * sizeof(int));
1695
0
    if (cur->states == NULL) {
1696
0
  xmlFree(cur);
1697
0
  ERROR(NULL, NULL, NULL,
1698
0
        "xmlNewStreamCtxt: malloc failed\n");
1699
0
  return(NULL);
1700
0
    }
1701
0
    cur->nbState = 0;
1702
0
    cur->maxState = 4;
1703
0
    cur->level = 0;
1704
0
    cur->comp = stream;
1705
0
    cur->blockLevel = -1;
1706
0
    return(cur);
1707
0
}
1708
1709
/**
1710
 * xmlFreeStreamCtxt:
1711
 * @stream: the stream context
1712
 *
1713
 * Free the stream context
1714
 */
1715
void
1716
0
xmlFreeStreamCtxt(xmlStreamCtxtPtr stream) {
1717
0
    xmlStreamCtxtPtr next;
1718
1719
0
    while (stream != NULL) {
1720
0
        next = stream->next;
1721
0
        if (stream->states != NULL)
1722
0
      xmlFree(stream->states);
1723
0
        xmlFree(stream);
1724
0
  stream = next;
1725
0
    }
1726
0
}
1727
1728
/**
1729
 * xmlStreamCtxtAddState:
1730
 * @comp: the stream context
1731
 * @idx: the step index for that streaming state
1732
 *
1733
 * Add a new state to the stream context
1734
 *
1735
 * Returns -1 in case of error or the state index if successful
1736
 */
1737
static int
1738
0
xmlStreamCtxtAddState(xmlStreamCtxtPtr comp, int idx, int level) {
1739
0
    int i;
1740
0
    for (i = 0;i < comp->nbState;i++) {
1741
0
        if (comp->states[2 * i] < 0) {
1742
0
      comp->states[2 * i] = idx;
1743
0
      comp->states[2 * i + 1] = level;
1744
0
      return(i);
1745
0
  }
1746
0
    }
1747
0
    if (comp->nbState >= comp->maxState) {
1748
0
        int *cur;
1749
1750
0
  cur = (int *) xmlRealloc(comp->states,
1751
0
         comp->maxState * 4 * sizeof(int));
1752
0
  if (cur == NULL) {
1753
0
      ERROR(NULL, NULL, NULL,
1754
0
      "xmlNewStreamCtxt: malloc failed\n");
1755
0
      return(-1);
1756
0
  }
1757
0
  comp->states = cur;
1758
0
        comp->maxState *= 2;
1759
0
    }
1760
0
    comp->states[2 * comp->nbState] = idx;
1761
0
    comp->states[2 * comp->nbState++ + 1] = level;
1762
0
    return(comp->nbState - 1);
1763
0
}
1764
1765
/**
1766
 * xmlStreamPushInternal:
1767
 * @stream: the stream context
1768
 * @name: the current name
1769
 * @ns: the namespace name
1770
 * @nodeType: the type of the node
1771
 *
1772
 * Push new data onto the stream. NOTE: if the call xmlPatterncompile()
1773
 * indicated a dictionary, then strings for name and ns will be expected
1774
 * to come from the dictionary.
1775
 * Both @name and @ns being NULL means the / i.e. the root of the document.
1776
 * This can also act as a reset.
1777
 *
1778
 * Returns: -1 in case of error, 1 if the current state in the stream is a
1779
 *    match and 0 otherwise.
1780
 */
1781
static int
1782
xmlStreamPushInternal(xmlStreamCtxtPtr stream,
1783
          const xmlChar *name, const xmlChar *ns,
1784
0
          int nodeType) {
1785
0
    int ret = 0, final = 0, tmp, i, m, match, stepNr, desc;
1786
0
    xmlStreamCompPtr comp;
1787
0
    xmlStreamStep step;
1788
1789
0
    if ((stream == NULL) || (stream->nbState < 0))
1790
0
        return(-1);
1791
1792
0
    while (stream != NULL) {
1793
0
  comp = stream->comp;
1794
1795
0
  if ((nodeType == XML_ELEMENT_NODE) &&
1796
0
      (name == NULL) && (ns == NULL)) {
1797
      /* We have a document node here (or a reset). */
1798
0
      stream->nbState = 0;
1799
0
      stream->level = 0;
1800
0
      stream->blockLevel = -1;
1801
0
      if (comp->flags & XML_STREAM_FROM_ROOT) {
1802
0
    if (comp->nbStep == 0) {
1803
        /* TODO: We have a "/." here? */
1804
0
        ret = 1;
1805
0
    } else {
1806
0
        if ((comp->nbStep == 1) &&
1807
0
      (comp->steps[0].nodeType == XML_STREAM_ANY_NODE) &&
1808
0
      (comp->steps[0].flags & XML_STREAM_STEP_DESC))
1809
0
        {
1810
      /*
1811
      * In the case of "//." the document node will match
1812
      * as well.
1813
      */
1814
0
      ret = 1;
1815
0
        } else if (comp->steps[0].flags & XML_STREAM_STEP_ROOT) {
1816
0
      if (xmlStreamCtxtAddState(stream, 0, 0) < 0)
1817
0
                            return(-1);
1818
0
        }
1819
0
    }
1820
0
      }
1821
0
      stream = stream->next;
1822
0
      continue; /* while */
1823
0
  }
1824
1825
  /*
1826
  * Fast check for ".".
1827
  */
1828
0
  if (comp->nbStep == 0) {
1829
      /*
1830
       * / and . are handled at the XPath node set creation
1831
       * level by checking min depth
1832
       */
1833
0
      if (stream->flags & XML_PATTERN_XPATH) {
1834
0
    stream = stream->next;
1835
0
    continue; /* while */
1836
0
      }
1837
      /*
1838
      * For non-pattern like evaluation like XML Schema IDCs
1839
      * or traditional XPath expressions, this will match if
1840
      * we are at the first level only, otherwise on every level.
1841
      */
1842
0
      if ((nodeType != XML_ATTRIBUTE_NODE) &&
1843
0
    (((stream->flags & XML_PATTERN_NOTPATTERN) == 0) ||
1844
0
    (stream->level == 0))) {
1845
0
        ret = 1;
1846
0
      }
1847
0
      stream->level++;
1848
0
      goto stream_next;
1849
0
  }
1850
0
  if (stream->blockLevel != -1) {
1851
      /*
1852
      * Skip blocked expressions.
1853
      */
1854
0
      stream->level++;
1855
0
      goto stream_next;
1856
0
  }
1857
1858
0
  if ((nodeType != XML_ELEMENT_NODE) &&
1859
0
      (nodeType != XML_ATTRIBUTE_NODE) &&
1860
0
      ((comp->flags & XML_STREAM_FINAL_IS_ANY_NODE) == 0)) {
1861
      /*
1862
      * No need to process nodes of other types if we don't
1863
      * resolve to those types.
1864
      * TODO: Do we need to block the context here?
1865
      */
1866
0
      stream->level++;
1867
0
      goto stream_next;
1868
0
  }
1869
1870
  /*
1871
   * Check evolution of existing states
1872
   */
1873
0
  i = 0;
1874
0
  m = stream->nbState;
1875
0
  while (i < m) {
1876
0
      if ((comp->flags & XML_STREAM_DESC) == 0) {
1877
    /*
1878
    * If there is no "//", then only the last
1879
    * added state is of interest.
1880
    */
1881
0
    stepNr = stream->states[2 * (stream->nbState -1)];
1882
    /*
1883
    * TODO: Security check, should not happen, remove it.
1884
    */
1885
0
    if (stream->states[(2 * (stream->nbState -1)) + 1] <
1886
0
        stream->level) {
1887
0
        return (-1);
1888
0
    }
1889
0
    desc = 0;
1890
    /* loop-stopper */
1891
0
    i = m;
1892
0
      } else {
1893
    /*
1894
    * If there are "//", then we need to process every "//"
1895
    * occurring in the states, plus any other state for this
1896
    * level.
1897
    */
1898
0
    stepNr = stream->states[2 * i];
1899
1900
    /* TODO: should not happen anymore: dead states */
1901
0
    if (stepNr < 0)
1902
0
        goto next_state;
1903
1904
0
    tmp = stream->states[(2 * i) + 1];
1905
1906
    /* skip new states just added */
1907
0
    if (tmp > stream->level)
1908
0
        goto next_state;
1909
1910
    /* skip states at ancestor levels, except if "//" */
1911
0
    desc = comp->steps[stepNr].flags & XML_STREAM_STEP_DESC;
1912
0
    if ((tmp < stream->level) && (!desc))
1913
0
        goto next_state;
1914
0
      }
1915
      /*
1916
      * Check for correct node-type.
1917
      */
1918
0
      step = comp->steps[stepNr];
1919
0
      if (step.nodeType != nodeType) {
1920
0
    if (step.nodeType == XML_ATTRIBUTE_NODE) {
1921
        /*
1922
        * Block this expression for deeper evaluation.
1923
        */
1924
0
        if ((comp->flags & XML_STREAM_DESC) == 0)
1925
0
      stream->blockLevel = stream->level +1;
1926
0
        goto next_state;
1927
0
    } else if (step.nodeType != XML_STREAM_ANY_NODE)
1928
0
        goto next_state;
1929
0
      }
1930
      /*
1931
      * Compare local/namespace-name.
1932
      */
1933
0
      match = 0;
1934
0
      if (step.nodeType == XML_STREAM_ANY_NODE) {
1935
0
    match = 1;
1936
0
      } else if (step.name == NULL) {
1937
0
    if (step.ns == NULL) {
1938
        /*
1939
        * This lets through all elements/attributes.
1940
        */
1941
0
        match = 1;
1942
0
    } else if (ns != NULL)
1943
0
        match = xmlStrEqual(step.ns, ns);
1944
0
      } else if (((step.ns != NULL) == (ns != NULL)) &&
1945
0
    (name != NULL) &&
1946
0
    (step.name[0] == name[0]) &&
1947
0
    xmlStrEqual(step.name, name) &&
1948
0
    ((step.ns == ns) || xmlStrEqual(step.ns, ns)))
1949
0
      {
1950
0
    match = 1;
1951
0
      }
1952
#if 0
1953
/*
1954
* TODO: Pointer comparison won't work, since not guaranteed that the given
1955
*  values are in the same dict; especially if it's the namespace name,
1956
*  normally coming from ns->href. We need a namespace dict mechanism !
1957
*/
1958
      } else if (comp->dict) {
1959
    if (step.name == NULL) {
1960
        if (step.ns == NULL)
1961
      match = 1;
1962
        else
1963
      match = (step.ns == ns);
1964
    } else {
1965
        match = ((step.name == name) && (step.ns == ns));
1966
    }
1967
#endif /* if 0 ------------------------------------------------------- */
1968
0
      if (match) {
1969
0
    final = step.flags & XML_STREAM_STEP_FINAL;
1970
0
                if (final) {
1971
0
                    ret = 1;
1972
0
                } else if (xmlStreamCtxtAddState(stream, stepNr + 1,
1973
0
                                                 stream->level + 1) < 0) {
1974
0
                    return(-1);
1975
0
                }
1976
0
    if ((ret != 1) && (step.flags & XML_STREAM_STEP_IN_SET)) {
1977
        /*
1978
        * Check if we have a special case like "foo/bar//.", where
1979
        * "foo" is selected as well.
1980
        */
1981
0
        ret = 1;
1982
0
    }
1983
0
      }
1984
0
      if (((comp->flags & XML_STREAM_DESC) == 0) &&
1985
0
    ((! match) || final))  {
1986
    /*
1987
    * Mark this expression as blocked for any evaluation at
1988
    * deeper levels. Note that this includes "/foo"
1989
    * expressions if the *pattern* behaviour is used.
1990
    */
1991
0
    stream->blockLevel = stream->level +1;
1992
0
      }
1993
0
next_state:
1994
0
      i++;
1995
0
  }
1996
1997
0
  stream->level++;
1998
1999
  /*
2000
  * Re/enter the expression.
2001
  * Don't reenter if it's an absolute expression like "/foo",
2002
  *   except "//foo".
2003
  */
2004
0
  step = comp->steps[0];
2005
0
  if (step.flags & XML_STREAM_STEP_ROOT)
2006
0
      goto stream_next;
2007
2008
0
  desc = step.flags & XML_STREAM_STEP_DESC;
2009
0
  if (stream->flags & XML_PATTERN_NOTPATTERN) {
2010
      /*
2011
      * Re/enter the expression if it is a "descendant" one,
2012
      * or if we are at the 1st level of evaluation.
2013
      */
2014
2015
0
      if (stream->level == 1) {
2016
0
    if (XML_STREAM_XS_IDC(stream)) {
2017
        /*
2018
        * XS-IDC: The missing "self::node()" will always
2019
        * match the first given node.
2020
        */
2021
0
        goto stream_next;
2022
0
    } else
2023
0
        goto compare;
2024
0
      }
2025
      /*
2026
      * A "//" is always reentrant.
2027
      */
2028
0
      if (desc)
2029
0
    goto compare;
2030
2031
      /*
2032
      * XS-IDC: Process the 2nd level, since the missing
2033
      * "self::node()" is responsible for the 2nd level being
2034
      * the real start level.
2035
      */
2036
0
      if ((stream->level == 2) && XML_STREAM_XS_IDC(stream))
2037
0
    goto compare;
2038
2039
0
      goto stream_next;
2040
0
  }
2041
2042
0
compare:
2043
  /*
2044
  * Check expected node-type.
2045
  */
2046
0
  if (step.nodeType != nodeType) {
2047
0
      if (nodeType == XML_ATTRIBUTE_NODE)
2048
0
    goto stream_next;
2049
0
      else if (step.nodeType != XML_STREAM_ANY_NODE)
2050
0
    goto stream_next;
2051
0
  }
2052
  /*
2053
  * Compare local/namespace-name.
2054
  */
2055
0
  match = 0;
2056
0
  if (step.nodeType == XML_STREAM_ANY_NODE) {
2057
0
      match = 1;
2058
0
  } else if (step.name == NULL) {
2059
0
      if (step.ns == NULL) {
2060
    /*
2061
    * This lets through all elements/attributes.
2062
    */
2063
0
    match = 1;
2064
0
      } else if (ns != NULL)
2065
0
    match = xmlStrEqual(step.ns, ns);
2066
0
  } else if (((step.ns != NULL) == (ns != NULL)) &&
2067
0
      (name != NULL) &&
2068
0
      (step.name[0] == name[0]) &&
2069
0
      xmlStrEqual(step.name, name) &&
2070
0
      ((step.ns == ns) || xmlStrEqual(step.ns, ns)))
2071
0
  {
2072
0
      match = 1;
2073
0
  }
2074
0
  final = step.flags & XML_STREAM_STEP_FINAL;
2075
0
  if (match) {
2076
0
      if (final) {
2077
0
    ret = 1;
2078
0
            } else if (xmlStreamCtxtAddState(stream, 1, stream->level) < 0) {
2079
0
                return(-1);
2080
0
            }
2081
0
      if ((ret != 1) && (step.flags & XML_STREAM_STEP_IN_SET)) {
2082
    /*
2083
    * Check if we have a special case like "foo//.", where
2084
    * "foo" is selected as well.
2085
    */
2086
0
    ret = 1;
2087
0
      }
2088
0
  }
2089
0
  if (((comp->flags & XML_STREAM_DESC) == 0) &&
2090
0
      ((! match) || final))  {
2091
      /*
2092
      * Mark this expression as blocked for any evaluation at
2093
      * deeper levels.
2094
      */
2095
0
      stream->blockLevel = stream->level;
2096
0
  }
2097
2098
0
stream_next:
2099
0
        stream = stream->next;
2100
0
    } /* while stream != NULL */
2101
2102
0
    return(ret);
2103
0
}
2104
2105
/**
2106
 * xmlStreamPush:
2107
 * @stream: the stream context
2108
 * @name: the current name
2109
 * @ns: the namespace name
2110
 *
2111
 * Push new data onto the stream. NOTE: if the call xmlPatterncompile()
2112
 * indicated a dictionary, then strings for name and ns will be expected
2113
 * to come from the dictionary.
2114
 * Both @name and @ns being NULL means the / i.e. the root of the document.
2115
 * This can also act as a reset.
2116
 * Otherwise the function will act as if it has been given an element-node.
2117
 *
2118
 * Returns: -1 in case of error, 1 if the current state in the stream is a
2119
 *    match and 0 otherwise.
2120
 */
2121
int
2122
xmlStreamPush(xmlStreamCtxtPtr stream,
2123
0
              const xmlChar *name, const xmlChar *ns) {
2124
0
    return (xmlStreamPushInternal(stream, name, ns, XML_ELEMENT_NODE));
2125
0
}
2126
2127
/**
2128
 * xmlStreamPushNode:
2129
 * @stream: the stream context
2130
 * @name: the current name
2131
 * @ns: the namespace name
2132
 * @nodeType: the type of the node being pushed
2133
 *
2134
 * Push new data onto the stream. NOTE: if the call xmlPatterncompile()
2135
 * indicated a dictionary, then strings for name and ns will be expected
2136
 * to come from the dictionary.
2137
 * Both @name and @ns being NULL means the / i.e. the root of the document.
2138
 * This can also act as a reset.
2139
 * Different from xmlStreamPush() this function can be fed with nodes of type:
2140
 * element-, attribute-, text-, cdata-section-, comment- and
2141
 * processing-instruction-node.
2142
 *
2143
 * Returns: -1 in case of error, 1 if the current state in the stream is a
2144
 *    match and 0 otherwise.
2145
 */
2146
int
2147
xmlStreamPushNode(xmlStreamCtxtPtr stream,
2148
      const xmlChar *name, const xmlChar *ns,
2149
      int nodeType)
2150
0
{
2151
0
    return (xmlStreamPushInternal(stream, name, ns,
2152
0
  nodeType));
2153
0
}
2154
2155
/**
2156
* xmlStreamPushAttr:
2157
* @stream: the stream context
2158
* @name: the current name
2159
* @ns: the namespace name
2160
*
2161
* Push new attribute data onto the stream. NOTE: if the call xmlPatterncompile()
2162
* indicated a dictionary, then strings for name and ns will be expected
2163
* to come from the dictionary.
2164
* Both @name and @ns being NULL means the / i.e. the root of the document.
2165
* This can also act as a reset.
2166
* Otherwise the function will act as if it has been given an attribute-node.
2167
*
2168
* Returns: -1 in case of error, 1 if the current state in the stream is a
2169
*    match and 0 otherwise.
2170
*/
2171
int
2172
xmlStreamPushAttr(xmlStreamCtxtPtr stream,
2173
0
      const xmlChar *name, const xmlChar *ns) {
2174
0
    return (xmlStreamPushInternal(stream, name, ns, XML_ATTRIBUTE_NODE));
2175
0
}
2176
2177
/**
2178
 * xmlStreamPop:
2179
 * @stream: the stream context
2180
 *
2181
 * push one level from the stream.
2182
 *
2183
 * Returns: -1 in case of error, 0 otherwise.
2184
 */
2185
int
2186
0
xmlStreamPop(xmlStreamCtxtPtr stream) {
2187
0
    int i, lev;
2188
2189
0
    if (stream == NULL)
2190
0
        return(-1);
2191
0
    while (stream != NULL) {
2192
  /*
2193
  * Reset block-level.
2194
  */
2195
0
  if (stream->blockLevel == stream->level)
2196
0
      stream->blockLevel = -1;
2197
2198
  /*
2199
   *  stream->level can be zero when XML_FINAL_IS_ANY_NODE is set
2200
   *  (see the thread at
2201
   *  http://mail.gnome.org/archives/xslt/2008-July/msg00027.html)
2202
   */
2203
0
  if (stream->level)
2204
0
      stream->level--;
2205
  /*
2206
   * Check evolution of existing states
2207
   */
2208
0
  for (i = stream->nbState -1; i >= 0; i--) {
2209
      /* discard obsoleted states */
2210
0
      lev = stream->states[(2 * i) + 1];
2211
0
      if (lev > stream->level)
2212
0
    stream->nbState--;
2213
0
      if (lev <= stream->level)
2214
0
    break;
2215
0
  }
2216
0
  stream = stream->next;
2217
0
    }
2218
0
    return(0);
2219
0
}
2220
2221
/**
2222
 * xmlStreamWantsAnyNode:
2223
 * @streamCtxt: the stream context
2224
 *
2225
 * Query if the streaming pattern additionally needs to be fed with
2226
 * text-, cdata-section-, comment- and processing-instruction-nodes.
2227
 * If the result is 0 then only element-nodes and attribute-nodes
2228
 * need to be pushed.
2229
 *
2230
 * Returns: 1 in case of need of nodes of the above described types,
2231
 *          0 otherwise. -1 on API errors.
2232
 */
2233
int
2234
xmlStreamWantsAnyNode(xmlStreamCtxtPtr streamCtxt)
2235
0
{
2236
0
    if (streamCtxt == NULL)
2237
0
  return(-1);
2238
0
    while (streamCtxt != NULL) {
2239
0
  if (streamCtxt->comp->flags & XML_STREAM_FINAL_IS_ANY_NODE)
2240
0
      return(1);
2241
0
  streamCtxt = streamCtxt->next;
2242
0
    }
2243
0
    return(0);
2244
0
}
2245
2246
/************************************************************************
2247
 *                  *
2248
 *      The public interfaces       *
2249
 *                  *
2250
 ************************************************************************/
2251
2252
/**
2253
 * xmlPatterncompile:
2254
 * @pattern: the pattern to compile
2255
 * @dict: an optional dictionary for interned strings
2256
 * @flags: compilation flags, see xmlPatternFlags
2257
 * @namespaces: the prefix definitions, array of [URI, prefix] or NULL
2258
 * @patternOut: output pattern
2259
 *
2260
 * Compile a pattern.
2261
 *
2262
 * Returns 0 on success, 1 on error, -1 if a memory allocation failed.
2263
 */
2264
int
2265
xmlPatternCompileSafe(const xmlChar *pattern, xmlDict *dict, int flags,
2266
0
                      const xmlChar **namespaces, xmlPatternPtr *patternOut) {
2267
0
    xmlPatternPtr ret = NULL, cur;
2268
0
    xmlPatParserContextPtr ctxt = NULL;
2269
0
    const xmlChar *or, *start;
2270
0
    xmlChar *tmp = NULL;
2271
0
    int type = 0;
2272
0
    int streamable = 1;
2273
0
    int error;
2274
2275
0
    if (pattern == NULL) {
2276
0
        error = 1;
2277
0
        goto error;
2278
0
    }
2279
2280
0
    start = pattern;
2281
0
    or = start;
2282
0
    while (*or != 0) {
2283
0
  tmp = NULL;
2284
0
  while ((*or != 0) && (*or != '|')) or++;
2285
0
        if (*or == 0)
2286
0
      ctxt = xmlNewPatParserContext(start, dict, namespaces);
2287
0
  else {
2288
0
      tmp = xmlStrndup(start, or - start);
2289
0
      if (tmp != NULL) {
2290
0
    ctxt = xmlNewPatParserContext(tmp, dict, namespaces);
2291
0
      }
2292
0
      or++;
2293
0
  }
2294
0
  if (ctxt == NULL) {
2295
0
            error = -1;
2296
0
            goto error;
2297
0
        }
2298
0
  cur = xmlNewPattern();
2299
0
  if (cur == NULL) {
2300
0
            error = -1;
2301
0
            goto error;
2302
0
        }
2303
  /*
2304
  * Assign string dict.
2305
  */
2306
0
  if (dict) {
2307
0
      cur->dict = dict;
2308
0
      xmlDictReference(dict);
2309
0
  }
2310
0
  if (ret == NULL)
2311
0
      ret = cur;
2312
0
  else {
2313
0
      cur->next = ret->next;
2314
0
      ret->next = cur;
2315
0
  }
2316
0
  cur->flags = flags;
2317
0
  ctxt->comp = cur;
2318
2319
0
  if (XML_STREAM_XS_IDC(cur))
2320
0
      xmlCompileIDCXPathPath(ctxt);
2321
0
  else
2322
0
      xmlCompilePathPattern(ctxt);
2323
0
  if (ctxt->error != 0) {
2324
0
            error = ctxt->error;
2325
0
      goto error;
2326
0
        }
2327
0
  xmlFreePatParserContext(ctxt);
2328
0
  ctxt = NULL;
2329
2330
2331
0
        if (streamable) {
2332
0
      if (type == 0) {
2333
0
          type = cur->flags & (PAT_FROM_ROOT | PAT_FROM_CUR);
2334
0
      } else if (type == PAT_FROM_ROOT) {
2335
0
          if (cur->flags & PAT_FROM_CUR)
2336
0
        streamable = 0;
2337
0
      } else if (type == PAT_FROM_CUR) {
2338
0
          if (cur->flags & PAT_FROM_ROOT)
2339
0
        streamable = 0;
2340
0
      }
2341
0
  }
2342
0
  if (streamable) {
2343
0
      error = xmlStreamCompile(cur);
2344
0
            if (error != 0)
2345
0
                goto error;
2346
0
        }
2347
0
  error = xmlReversePattern(cur);
2348
0
        if (error != 0)
2349
0
      goto error;
2350
0
  if (tmp != NULL) {
2351
0
      xmlFree(tmp);
2352
0
      tmp = NULL;
2353
0
  }
2354
0
  start = or;
2355
0
    }
2356
0
    if (streamable == 0) {
2357
0
        cur = ret;
2358
0
  while (cur != NULL) {
2359
0
      if (cur->stream != NULL) {
2360
0
    xmlFreeStreamComp(cur->stream);
2361
0
    cur->stream = NULL;
2362
0
      }
2363
0
      cur = cur->next;
2364
0
  }
2365
0
    }
2366
2367
0
    *patternOut = ret;
2368
0
    return(0);
2369
0
error:
2370
0
    if (ctxt != NULL) xmlFreePatParserContext(ctxt);
2371
0
    if (ret != NULL) xmlFreePattern(ret);
2372
0
    if (tmp != NULL) xmlFree(tmp);
2373
0
    *patternOut = NULL;
2374
0
    return(error);
2375
0
}
2376
2377
/**
2378
 * xmlPatterncompile:
2379
 * @pattern: the pattern to compile
2380
 * @dict: an optional dictionary for interned strings
2381
 * @flags: compilation flags, see xmlPatternFlags
2382
 * @namespaces: the prefix definitions, array of [URI, prefix] or NULL
2383
 *
2384
 * Compile a pattern.
2385
 *
2386
 * Returns the compiled form of the pattern or NULL in case of error
2387
 */
2388
xmlPatternPtr
2389
xmlPatterncompile(const xmlChar *pattern, xmlDict *dict, int flags,
2390
0
                  const xmlChar **namespaces) {
2391
0
    xmlPatternPtr ret;
2392
0
    xmlPatternCompileSafe(pattern, dict, flags, namespaces, &ret);
2393
0
    return(ret);
2394
0
}
2395
2396
/**
2397
 * xmlPatternMatch:
2398
 * @comp: the precompiled pattern
2399
 * @node: a node
2400
 *
2401
 * Test whether the node matches the pattern
2402
 *
2403
 * Returns 1 if it matches, 0 if it doesn't and -1 in case of failure
2404
 */
2405
int
2406
xmlPatternMatch(xmlPatternPtr comp, xmlNodePtr node)
2407
0
{
2408
0
    int ret = 0;
2409
2410
0
    if ((comp == NULL) || (node == NULL))
2411
0
        return(-1);
2412
2413
0
    while (comp != NULL) {
2414
0
        ret = xmlPatMatch(comp, node);
2415
0
  if (ret != 0)
2416
0
      return(ret);
2417
0
  comp = comp->next;
2418
0
    }
2419
0
    return(ret);
2420
0
}
2421
2422
/**
2423
 * xmlPatternGetStreamCtxt:
2424
 * @comp: the precompiled pattern
2425
 *
2426
 * Get a streaming context for that pattern
2427
 * Use xmlFreeStreamCtxt to free the context.
2428
 *
2429
 * Returns a pointer to the context or NULL in case of failure
2430
 */
2431
xmlStreamCtxtPtr
2432
xmlPatternGetStreamCtxt(xmlPatternPtr comp)
2433
0
{
2434
0
    xmlStreamCtxtPtr ret = NULL, cur;
2435
2436
0
    if ((comp == NULL) || (comp->stream == NULL))
2437
0
        return(NULL);
2438
2439
0
    while (comp != NULL) {
2440
0
        if (comp->stream == NULL)
2441
0
      goto failed;
2442
0
  cur = xmlNewStreamCtxt(comp->stream);
2443
0
  if (cur == NULL)
2444
0
      goto failed;
2445
0
  if (ret == NULL)
2446
0
      ret = cur;
2447
0
  else {
2448
0
      cur->next = ret->next;
2449
0
      ret->next = cur;
2450
0
  }
2451
0
  cur->flags = comp->flags;
2452
0
  comp = comp->next;
2453
0
    }
2454
0
    return(ret);
2455
0
failed:
2456
0
    xmlFreeStreamCtxt(ret);
2457
0
    return(NULL);
2458
0
}
2459
2460
/**
2461
 * xmlPatternStreamable:
2462
 * @comp: the precompiled pattern
2463
 *
2464
 * Check if the pattern is streamable i.e. xmlPatternGetStreamCtxt()
2465
 * should work.
2466
 *
2467
 * Returns 1 if streamable, 0 if not and -1 in case of error.
2468
 */
2469
int
2470
0
xmlPatternStreamable(xmlPatternPtr comp) {
2471
0
    if (comp == NULL)
2472
0
        return(-1);
2473
0
    while (comp != NULL) {
2474
0
        if (comp->stream == NULL)
2475
0
      return(0);
2476
0
  comp = comp->next;
2477
0
    }
2478
0
    return(1);
2479
0
}
2480
2481
/**
2482
 * xmlPatternMaxDepth:
2483
 * @comp: the precompiled pattern
2484
 *
2485
 * Check the maximum depth reachable by a pattern
2486
 *
2487
 * Returns -2 if no limit (using //), otherwise the depth,
2488
 *         and -1 in case of error
2489
 */
2490
int
2491
0
xmlPatternMaxDepth(xmlPatternPtr comp) {
2492
0
    int ret = 0, i;
2493
0
    if (comp == NULL)
2494
0
        return(-1);
2495
0
    while (comp != NULL) {
2496
0
        if (comp->stream == NULL)
2497
0
      return(-1);
2498
0
  for (i = 0;i < comp->stream->nbStep;i++)
2499
0
      if (comp->stream->steps[i].flags & XML_STREAM_STEP_DESC)
2500
0
          return(-2);
2501
0
  if (comp->stream->nbStep > ret)
2502
0
      ret = comp->stream->nbStep;
2503
0
  comp = comp->next;
2504
0
    }
2505
0
    return(ret);
2506
0
}
2507
2508
/**
2509
 * xmlPatternMinDepth:
2510
 * @comp: the precompiled pattern
2511
 *
2512
 * Check the minimum depth reachable by a pattern, 0 mean the / or . are
2513
 * part of the set.
2514
 *
2515
 * Returns -1 in case of error otherwise the depth,
2516
 *
2517
 */
2518
int
2519
0
xmlPatternMinDepth(xmlPatternPtr comp) {
2520
0
    int ret = 12345678;
2521
0
    if (comp == NULL)
2522
0
        return(-1);
2523
0
    while (comp != NULL) {
2524
0
        if (comp->stream == NULL)
2525
0
      return(-1);
2526
0
  if (comp->stream->nbStep < ret)
2527
0
      ret = comp->stream->nbStep;
2528
0
  if (ret == 0)
2529
0
      return(0);
2530
0
  comp = comp->next;
2531
0
    }
2532
0
    return(ret);
2533
0
}
2534
2535
/**
2536
 * xmlPatternFromRoot:
2537
 * @comp: the precompiled pattern
2538
 *
2539
 * Check if the pattern must be looked at from the root.
2540
 *
2541
 * Returns 1 if true, 0 if false and -1 in case of error
2542
 */
2543
int
2544
0
xmlPatternFromRoot(xmlPatternPtr comp) {
2545
0
    if (comp == NULL)
2546
0
        return(-1);
2547
0
    while (comp != NULL) {
2548
0
        if (comp->stream == NULL)
2549
0
      return(-1);
2550
0
  if (comp->flags & PAT_FROM_ROOT)
2551
0
      return(1);
2552
0
  comp = comp->next;
2553
0
    }
2554
0
    return(0);
2555
2556
0
}
2557
2558
#endif /* LIBXML_PATTERN_ENABLED */