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