/work/workdir/UnpackedTarball/libxml2/xmlregexp.c
Line | Count | Source |
1 | | /* |
2 | | * regexp.c: generic and extensible Regular Expression engine |
3 | | * |
4 | | * Basically designed with the purpose of compiling regexps for |
5 | | * the variety of validation/schemas mechanisms now available in |
6 | | * XML related specifications these include: |
7 | | * - XML-1.0 DTD validation |
8 | | * - XML Schemas structure part 1 |
9 | | * - XML Schemas Datatypes part 2 especially Appendix F |
10 | | * - RELAX-NG/TREX i.e. the counter proposal |
11 | | * |
12 | | * See Copyright for the status of this software. |
13 | | * |
14 | | * Daniel Veillard <veillard@redhat.com> |
15 | | */ |
16 | | |
17 | | #define IN_LIBXML |
18 | | #include "libxml.h" |
19 | | |
20 | | #ifdef LIBXML_REGEXP_ENABLED |
21 | | |
22 | | #include <stdio.h> |
23 | | #include <string.h> |
24 | | #include <limits.h> |
25 | | |
26 | | #include <libxml/tree.h> |
27 | | #include <libxml/parserInternals.h> |
28 | | #include <libxml/xmlregexp.h> |
29 | | #include <libxml/xmlautomata.h> |
30 | | |
31 | | #include "private/error.h" |
32 | | #include "private/memory.h" |
33 | | #include "private/regexp.h" |
34 | | #include "private/unicode.h" |
35 | | |
36 | | #ifndef SIZE_MAX |
37 | | #define SIZE_MAX ((size_t) -1) |
38 | | #endif |
39 | | |
40 | | /* #define DEBUG_REGEXP */ |
41 | | |
42 | 0 | #define MAX_PUSH 10000000 |
43 | | |
44 | | #ifdef ERROR |
45 | | #undef ERROR |
46 | | #endif |
47 | | #define ERROR(str) \ |
48 | 0 | ctxt->error = XML_REGEXP_COMPILE_ERROR; \ |
49 | 0 | xmlRegexpErrCompile(ctxt, str); |
50 | 0 | #define NEXT ctxt->cur++ |
51 | 0 | #define CUR (*(ctxt->cur)) |
52 | 0 | #define NXT(index) (ctxt->cur[index]) |
53 | | |
54 | 0 | #define NEXTL(l) ctxt->cur += l; |
55 | 0 | #define XML_REG_STRING_SEPARATOR '|' |
56 | | /* |
57 | | * Need PREV to check on a '-' within a Character Group. May only be used |
58 | | * when it's guaranteed that cur is not at the beginning of ctxt->string! |
59 | | */ |
60 | 0 | #define PREV (ctxt->cur[-1]) |
61 | | |
62 | | /************************************************************************ |
63 | | * * |
64 | | * Datatypes and structures * |
65 | | * * |
66 | | ************************************************************************/ |
67 | | |
68 | | /* |
69 | | * Note: the order of the enums below is significant, do not shuffle |
70 | | */ |
71 | | typedef enum { |
72 | | XML_REGEXP_EPSILON = 1, |
73 | | XML_REGEXP_CHARVAL, |
74 | | XML_REGEXP_RANGES, |
75 | | XML_REGEXP_SUBREG, /* used for () sub regexps */ |
76 | | XML_REGEXP_STRING, |
77 | | XML_REGEXP_ANYCHAR, /* . */ |
78 | | XML_REGEXP_ANYSPACE, /* \s */ |
79 | | XML_REGEXP_NOTSPACE, /* \S */ |
80 | | XML_REGEXP_INITNAME, /* \l */ |
81 | | XML_REGEXP_NOTINITNAME, /* \L */ |
82 | | XML_REGEXP_NAMECHAR, /* \c */ |
83 | | XML_REGEXP_NOTNAMECHAR, /* \C */ |
84 | | XML_REGEXP_DECIMAL, /* \d */ |
85 | | XML_REGEXP_NOTDECIMAL, /* \D */ |
86 | | XML_REGEXP_REALCHAR, /* \w */ |
87 | | XML_REGEXP_NOTREALCHAR, /* \W */ |
88 | | XML_REGEXP_LETTER = 100, |
89 | | XML_REGEXP_LETTER_UPPERCASE, |
90 | | XML_REGEXP_LETTER_LOWERCASE, |
91 | | XML_REGEXP_LETTER_TITLECASE, |
92 | | XML_REGEXP_LETTER_MODIFIER, |
93 | | XML_REGEXP_LETTER_OTHERS, |
94 | | XML_REGEXP_MARK, |
95 | | XML_REGEXP_MARK_NONSPACING, |
96 | | XML_REGEXP_MARK_SPACECOMBINING, |
97 | | XML_REGEXP_MARK_ENCLOSING, |
98 | | XML_REGEXP_NUMBER, |
99 | | XML_REGEXP_NUMBER_DECIMAL, |
100 | | XML_REGEXP_NUMBER_LETTER, |
101 | | XML_REGEXP_NUMBER_OTHERS, |
102 | | XML_REGEXP_PUNCT, |
103 | | XML_REGEXP_PUNCT_CONNECTOR, |
104 | | XML_REGEXP_PUNCT_DASH, |
105 | | XML_REGEXP_PUNCT_OPEN, |
106 | | XML_REGEXP_PUNCT_CLOSE, |
107 | | XML_REGEXP_PUNCT_INITQUOTE, |
108 | | XML_REGEXP_PUNCT_FINQUOTE, |
109 | | XML_REGEXP_PUNCT_OTHERS, |
110 | | XML_REGEXP_SEPAR, |
111 | | XML_REGEXP_SEPAR_SPACE, |
112 | | XML_REGEXP_SEPAR_LINE, |
113 | | XML_REGEXP_SEPAR_PARA, |
114 | | XML_REGEXP_SYMBOL, |
115 | | XML_REGEXP_SYMBOL_MATH, |
116 | | XML_REGEXP_SYMBOL_CURRENCY, |
117 | | XML_REGEXP_SYMBOL_MODIFIER, |
118 | | XML_REGEXP_SYMBOL_OTHERS, |
119 | | XML_REGEXP_OTHER, |
120 | | XML_REGEXP_OTHER_CONTROL, |
121 | | XML_REGEXP_OTHER_FORMAT, |
122 | | XML_REGEXP_OTHER_PRIVATE, |
123 | | XML_REGEXP_OTHER_NA, |
124 | | XML_REGEXP_BLOCK_NAME |
125 | | } xmlRegAtomType; |
126 | | |
127 | | typedef enum { |
128 | | XML_REGEXP_QUANT_EPSILON = 1, |
129 | | XML_REGEXP_QUANT_ONCE, |
130 | | XML_REGEXP_QUANT_OPT, |
131 | | XML_REGEXP_QUANT_MULT, |
132 | | XML_REGEXP_QUANT_PLUS, |
133 | | XML_REGEXP_QUANT_ONCEONLY, |
134 | | XML_REGEXP_QUANT_ALL, |
135 | | XML_REGEXP_QUANT_RANGE |
136 | | } xmlRegQuantType; |
137 | | |
138 | | typedef enum { |
139 | | XML_REGEXP_START_STATE = 1, |
140 | | XML_REGEXP_FINAL_STATE, |
141 | | XML_REGEXP_TRANS_STATE, |
142 | | XML_REGEXP_SINK_STATE, |
143 | | XML_REGEXP_UNREACH_STATE |
144 | | } xmlRegStateType; |
145 | | |
146 | | typedef enum { |
147 | | XML_REGEXP_MARK_NORMAL = 0, |
148 | | XML_REGEXP_MARK_START, |
149 | | XML_REGEXP_MARK_VISITED |
150 | | } xmlRegMarkedType; |
151 | | |
152 | | typedef struct _xmlRegRange xmlRegRange; |
153 | | typedef xmlRegRange *xmlRegRangePtr; |
154 | | |
155 | | struct _xmlRegRange { |
156 | | int neg; /* 0 normal, 1 not, 2 exclude */ |
157 | | xmlRegAtomType type; |
158 | | int start; |
159 | | int end; |
160 | | xmlChar *blockName; |
161 | | }; |
162 | | |
163 | | typedef struct _xmlRegAtom xmlRegAtom; |
164 | | typedef xmlRegAtom *xmlRegAtomPtr; |
165 | | |
166 | | typedef struct _xmlAutomataState xmlRegState; |
167 | | typedef xmlRegState *xmlRegStatePtr; |
168 | | |
169 | | struct _xmlRegAtom { |
170 | | int no; |
171 | | xmlRegAtomType type; |
172 | | xmlRegQuantType quant; |
173 | | int min; |
174 | | int max; |
175 | | |
176 | | void *valuep; |
177 | | void *valuep2; |
178 | | int neg; |
179 | | int codepoint; |
180 | | xmlRegStatePtr start; |
181 | | xmlRegStatePtr start0; |
182 | | xmlRegStatePtr stop; |
183 | | int maxRanges; |
184 | | int nbRanges; |
185 | | xmlRegRangePtr *ranges; |
186 | | void *data; |
187 | | }; |
188 | | |
189 | | typedef struct _xmlRegCounter xmlRegCounter; |
190 | | typedef xmlRegCounter *xmlRegCounterPtr; |
191 | | |
192 | | struct _xmlRegCounter { |
193 | | int min; |
194 | | int max; |
195 | | }; |
196 | | |
197 | | typedef struct _xmlRegTrans xmlRegTrans; |
198 | | typedef xmlRegTrans *xmlRegTransPtr; |
199 | | |
200 | | struct _xmlRegTrans { |
201 | | xmlRegAtomPtr atom; |
202 | | int to; |
203 | | int counter; |
204 | | int count; |
205 | | int nd; |
206 | | }; |
207 | | |
208 | | struct _xmlAutomataState { |
209 | | xmlRegStateType type; |
210 | | xmlRegMarkedType mark; |
211 | | xmlRegMarkedType markd; |
212 | | xmlRegMarkedType reached; |
213 | | int no; |
214 | | int maxTrans; |
215 | | int nbTrans; |
216 | | xmlRegTrans *trans; |
217 | | /* knowing states pointing to us can speed things up */ |
218 | | int maxTransTo; |
219 | | int nbTransTo; |
220 | | int *transTo; |
221 | | }; |
222 | | |
223 | | typedef struct _xmlAutomata xmlRegParserCtxt; |
224 | | typedef xmlRegParserCtxt *xmlRegParserCtxtPtr; |
225 | | |
226 | 0 | #define AM_AUTOMATA_RNG 1 |
227 | | |
228 | | struct _xmlAutomata { |
229 | | xmlChar *string; |
230 | | xmlChar *cur; |
231 | | |
232 | | int error; |
233 | | int neg; |
234 | | |
235 | | xmlRegStatePtr start; |
236 | | xmlRegStatePtr end; |
237 | | xmlRegStatePtr state; |
238 | | |
239 | | xmlRegAtomPtr atom; |
240 | | |
241 | | int maxAtoms; |
242 | | int nbAtoms; |
243 | | xmlRegAtomPtr *atoms; |
244 | | |
245 | | int maxStates; |
246 | | int nbStates; |
247 | | xmlRegStatePtr *states; |
248 | | |
249 | | int maxCounters; |
250 | | int nbCounters; |
251 | | xmlRegCounter *counters; |
252 | | |
253 | | int determinist; |
254 | | int negs; |
255 | | int flags; |
256 | | |
257 | | int depth; |
258 | | }; |
259 | | |
260 | | struct _xmlRegexp { |
261 | | xmlChar *string; |
262 | | int nbStates; |
263 | | xmlRegStatePtr *states; |
264 | | int nbAtoms; |
265 | | xmlRegAtomPtr *atoms; |
266 | | int nbCounters; |
267 | | xmlRegCounter *counters; |
268 | | int determinist; |
269 | | int flags; |
270 | | /* |
271 | | * That's the compact form for determinists automatas |
272 | | */ |
273 | | int nbstates; |
274 | | int *compact; |
275 | | void **transdata; |
276 | | int nbstrings; |
277 | | xmlChar **stringMap; |
278 | | }; |
279 | | |
280 | | typedef struct _xmlRegExecRollback xmlRegExecRollback; |
281 | | typedef xmlRegExecRollback *xmlRegExecRollbackPtr; |
282 | | |
283 | | struct _xmlRegExecRollback { |
284 | | xmlRegStatePtr state;/* the current state */ |
285 | | int index; /* the index in the input stack */ |
286 | | int nextbranch; /* the next transition to explore in that state */ |
287 | | int *counts; /* save the automata state if it has some */ |
288 | | }; |
289 | | |
290 | | typedef struct _xmlRegInputToken xmlRegInputToken; |
291 | | typedef xmlRegInputToken *xmlRegInputTokenPtr; |
292 | | |
293 | | struct _xmlRegInputToken { |
294 | | xmlChar *value; |
295 | | void *data; |
296 | | }; |
297 | | |
298 | | struct _xmlRegExecCtxt { |
299 | | int status; /* execution status != 0 indicate an error */ |
300 | | int determinist; /* did we find an indeterministic behaviour */ |
301 | | xmlRegexpPtr comp; /* the compiled regexp */ |
302 | | xmlRegExecCallbacks callback; |
303 | | void *data; |
304 | | |
305 | | xmlRegStatePtr state;/* the current state */ |
306 | | int transno; /* the current transition on that state */ |
307 | | int transcount; /* the number of chars in char counted transitions */ |
308 | | |
309 | | /* |
310 | | * A stack of rollback states |
311 | | */ |
312 | | int maxRollbacks; |
313 | | int nbRollbacks; |
314 | | xmlRegExecRollback *rollbacks; |
315 | | |
316 | | /* |
317 | | * The state of the automata if any |
318 | | */ |
319 | | int *counts; |
320 | | |
321 | | /* |
322 | | * The input stack |
323 | | */ |
324 | | int inputStackMax; |
325 | | int inputStackNr; |
326 | | int index; |
327 | | int *charStack; |
328 | | const xmlChar *inputString; /* when operating on characters */ |
329 | | xmlRegInputTokenPtr inputStack;/* when operating on strings */ |
330 | | |
331 | | /* |
332 | | * error handling |
333 | | */ |
334 | | int errStateNo; /* the error state number */ |
335 | | xmlRegStatePtr errState; /* the error state */ |
336 | | xmlChar *errString; /* the string raising the error */ |
337 | | int *errCounts; /* counters at the error state */ |
338 | | int nbPush; |
339 | | }; |
340 | | |
341 | 0 | #define REGEXP_ALL_COUNTER 0x123456 |
342 | 0 | #define REGEXP_ALL_LAX_COUNTER 0x123457 |
343 | | |
344 | | static void xmlFAParseRegExp(xmlRegParserCtxtPtr ctxt, int top); |
345 | | static void xmlRegFreeState(xmlRegStatePtr state); |
346 | | static void xmlRegFreeAtom(xmlRegAtomPtr atom); |
347 | | static int xmlRegStrEqualWildcard(const xmlChar *expStr, const xmlChar *valStr); |
348 | | static int xmlRegCheckCharacter(xmlRegAtomPtr atom, int codepoint); |
349 | | static int xmlRegCheckCharacterRange(xmlRegAtomType type, int codepoint, |
350 | | int neg, int start, int end, const xmlChar *blockName); |
351 | | |
352 | | /************************************************************************ |
353 | | * * |
354 | | * Regexp memory error handler * |
355 | | * * |
356 | | ************************************************************************/ |
357 | | /** |
358 | | * xmlRegexpErrMemory: |
359 | | * @extra: extra information |
360 | | * |
361 | | * Handle an out of memory condition |
362 | | */ |
363 | | static void |
364 | | xmlRegexpErrMemory(xmlRegParserCtxtPtr ctxt) |
365 | 0 | { |
366 | 0 | if (ctxt != NULL) |
367 | 0 | ctxt->error = XML_ERR_NO_MEMORY; |
368 | |
|
369 | 0 | xmlRaiseMemoryError(NULL, NULL, NULL, XML_FROM_REGEXP, NULL); |
370 | 0 | } |
371 | | |
372 | | /** |
373 | | * xmlRegexpErrCompile: |
374 | | * @extra: extra information |
375 | | * |
376 | | * Handle a compilation failure |
377 | | */ |
378 | | static void |
379 | | xmlRegexpErrCompile(xmlRegParserCtxtPtr ctxt, const char *extra) |
380 | 0 | { |
381 | 0 | const char *regexp = NULL; |
382 | 0 | int idx = 0; |
383 | 0 | int res; |
384 | |
|
385 | 0 | if (ctxt != NULL) { |
386 | 0 | regexp = (const char *) ctxt->string; |
387 | 0 | idx = ctxt->cur - ctxt->string; |
388 | 0 | ctxt->error = XML_REGEXP_COMPILE_ERROR; |
389 | 0 | } |
390 | |
|
391 | 0 | res = xmlRaiseError(NULL, NULL, NULL, NULL, NULL, XML_FROM_REGEXP, |
392 | 0 | XML_REGEXP_COMPILE_ERROR, XML_ERR_FATAL, |
393 | 0 | NULL, 0, extra, regexp, NULL, idx, 0, |
394 | 0 | "failed to compile: %s\n", extra); |
395 | 0 | if (res < 0) |
396 | 0 | xmlRegexpErrMemory(ctxt); |
397 | 0 | } |
398 | | |
399 | | /************************************************************************ |
400 | | * * |
401 | | * Allocation/Deallocation * |
402 | | * * |
403 | | ************************************************************************/ |
404 | | |
405 | | static int xmlFAComputesDeterminism(xmlRegParserCtxtPtr ctxt); |
406 | | |
407 | | /** |
408 | | * xmlRegCalloc2: |
409 | | * @dim1: size of first dimension |
410 | | * @dim2: size of second dimension |
411 | | * @elemSize: size of element |
412 | | * |
413 | | * Allocate a two-dimensional array and set all elements to zero. |
414 | | * |
415 | | * Returns the new array or NULL in case of error. |
416 | | */ |
417 | | static void* |
418 | 0 | xmlRegCalloc2(size_t dim1, size_t dim2, size_t elemSize) { |
419 | 0 | size_t numElems, totalSize; |
420 | 0 | void *ret; |
421 | | |
422 | | /* Check for overflow */ |
423 | 0 | if ((dim2 == 0) || (elemSize == 0) || |
424 | 0 | (dim1 > SIZE_MAX / dim2 / elemSize)) |
425 | 0 | return (NULL); |
426 | 0 | numElems = dim1 * dim2; |
427 | 0 | if (numElems > XML_MAX_ITEMS) |
428 | 0 | return NULL; |
429 | 0 | totalSize = numElems * elemSize; |
430 | 0 | ret = xmlMalloc(totalSize); |
431 | 0 | if (ret != NULL) |
432 | 0 | memset(ret, 0, totalSize); |
433 | 0 | return (ret); |
434 | 0 | } |
435 | | |
436 | | /** |
437 | | * xmlRegEpxFromParse: |
438 | | * @ctxt: the parser context used to build it |
439 | | * |
440 | | * Allocate a new regexp and fill it with the result from the parser |
441 | | * |
442 | | * Returns the new regexp or NULL in case of error |
443 | | */ |
444 | | static xmlRegexpPtr |
445 | 0 | xmlRegEpxFromParse(xmlRegParserCtxtPtr ctxt) { |
446 | 0 | xmlRegexpPtr ret; |
447 | |
|
448 | 0 | ret = (xmlRegexpPtr) xmlMalloc(sizeof(xmlRegexp)); |
449 | 0 | if (ret == NULL) { |
450 | 0 | xmlRegexpErrMemory(ctxt); |
451 | 0 | return(NULL); |
452 | 0 | } |
453 | 0 | memset(ret, 0, sizeof(xmlRegexp)); |
454 | 0 | ret->string = ctxt->string; |
455 | 0 | ret->nbStates = ctxt->nbStates; |
456 | 0 | ret->states = ctxt->states; |
457 | 0 | ret->nbAtoms = ctxt->nbAtoms; |
458 | 0 | ret->atoms = ctxt->atoms; |
459 | 0 | ret->nbCounters = ctxt->nbCounters; |
460 | 0 | ret->counters = ctxt->counters; |
461 | 0 | ret->determinist = ctxt->determinist; |
462 | 0 | ret->flags = ctxt->flags; |
463 | 0 | if (ret->determinist == -1) { |
464 | 0 | if (xmlRegexpIsDeterminist(ret) < 0) { |
465 | 0 | xmlRegexpErrMemory(ctxt); |
466 | 0 | xmlFree(ret); |
467 | 0 | return(NULL); |
468 | 0 | } |
469 | 0 | } |
470 | | |
471 | 0 | if ((ret->determinist != 0) && |
472 | 0 | (ret->nbCounters == 0) && |
473 | 0 | (ctxt->negs == 0) && |
474 | 0 | (ret->atoms != NULL) && |
475 | 0 | (ret->atoms[0] != NULL) && |
476 | 0 | (ret->atoms[0]->type == XML_REGEXP_STRING)) { |
477 | 0 | int i, j, nbstates = 0, nbatoms = 0; |
478 | 0 | int *stateRemap; |
479 | 0 | int *stringRemap; |
480 | 0 | int *transitions; |
481 | 0 | void **transdata; |
482 | 0 | xmlChar **stringMap; |
483 | 0 | xmlChar *value; |
484 | | |
485 | | /* |
486 | | * Switch to a compact representation |
487 | | * 1/ counting the effective number of states left |
488 | | * 2/ counting the unique number of atoms, and check that |
489 | | * they are all of the string type |
490 | | * 3/ build a table state x atom for the transitions |
491 | | */ |
492 | |
|
493 | 0 | stateRemap = xmlMalloc(ret->nbStates * sizeof(int)); |
494 | 0 | if (stateRemap == NULL) { |
495 | 0 | xmlRegexpErrMemory(ctxt); |
496 | 0 | xmlFree(ret); |
497 | 0 | return(NULL); |
498 | 0 | } |
499 | 0 | for (i = 0;i < ret->nbStates;i++) { |
500 | 0 | if (ret->states[i] != NULL) { |
501 | 0 | stateRemap[i] = nbstates; |
502 | 0 | nbstates++; |
503 | 0 | } else { |
504 | 0 | stateRemap[i] = -1; |
505 | 0 | } |
506 | 0 | } |
507 | 0 | stringMap = xmlMalloc(ret->nbAtoms * sizeof(char *)); |
508 | 0 | if (stringMap == NULL) { |
509 | 0 | xmlRegexpErrMemory(ctxt); |
510 | 0 | xmlFree(stateRemap); |
511 | 0 | xmlFree(ret); |
512 | 0 | return(NULL); |
513 | 0 | } |
514 | 0 | stringRemap = xmlMalloc(ret->nbAtoms * sizeof(int)); |
515 | 0 | if (stringRemap == NULL) { |
516 | 0 | xmlRegexpErrMemory(ctxt); |
517 | 0 | xmlFree(stringMap); |
518 | 0 | xmlFree(stateRemap); |
519 | 0 | xmlFree(ret); |
520 | 0 | return(NULL); |
521 | 0 | } |
522 | 0 | for (i = 0;i < ret->nbAtoms;i++) { |
523 | 0 | if ((ret->atoms[i]->type == XML_REGEXP_STRING) && |
524 | 0 | (ret->atoms[i]->quant == XML_REGEXP_QUANT_ONCE)) { |
525 | 0 | value = ret->atoms[i]->valuep; |
526 | 0 | for (j = 0;j < nbatoms;j++) { |
527 | 0 | if (xmlStrEqual(stringMap[j], value)) { |
528 | 0 | stringRemap[i] = j; |
529 | 0 | break; |
530 | 0 | } |
531 | 0 | } |
532 | 0 | if (j >= nbatoms) { |
533 | 0 | stringRemap[i] = nbatoms; |
534 | 0 | stringMap[nbatoms] = xmlStrdup(value); |
535 | 0 | if (stringMap[nbatoms] == NULL) { |
536 | 0 | for (i = 0;i < nbatoms;i++) |
537 | 0 | xmlFree(stringMap[i]); |
538 | 0 | xmlFree(stringRemap); |
539 | 0 | xmlFree(stringMap); |
540 | 0 | xmlFree(stateRemap); |
541 | 0 | xmlFree(ret); |
542 | 0 | return(NULL); |
543 | 0 | } |
544 | 0 | nbatoms++; |
545 | 0 | } |
546 | 0 | } else { |
547 | 0 | xmlFree(stateRemap); |
548 | 0 | xmlFree(stringRemap); |
549 | 0 | for (i = 0;i < nbatoms;i++) |
550 | 0 | xmlFree(stringMap[i]); |
551 | 0 | xmlFree(stringMap); |
552 | 0 | xmlFree(ret); |
553 | 0 | return(NULL); |
554 | 0 | } |
555 | 0 | } |
556 | 0 | transitions = (int *) xmlRegCalloc2(nbstates + 1, nbatoms + 1, |
557 | 0 | sizeof(int)); |
558 | 0 | if (transitions == NULL) { |
559 | 0 | xmlFree(stateRemap); |
560 | 0 | xmlFree(stringRemap); |
561 | 0 | for (i = 0;i < nbatoms;i++) |
562 | 0 | xmlFree(stringMap[i]); |
563 | 0 | xmlFree(stringMap); |
564 | 0 | xmlFree(ret); |
565 | 0 | return(NULL); |
566 | 0 | } |
567 | | |
568 | | /* |
569 | | * Allocate the transition table. The first entry for each |
570 | | * state corresponds to the state type. |
571 | | */ |
572 | 0 | transdata = NULL; |
573 | |
|
574 | 0 | for (i = 0;i < ret->nbStates;i++) { |
575 | 0 | int stateno, atomno, targetno, prev; |
576 | 0 | xmlRegStatePtr state; |
577 | 0 | xmlRegTransPtr trans; |
578 | |
|
579 | 0 | stateno = stateRemap[i]; |
580 | 0 | if (stateno == -1) |
581 | 0 | continue; |
582 | 0 | state = ret->states[i]; |
583 | |
|
584 | 0 | transitions[stateno * (nbatoms + 1)] = state->type; |
585 | |
|
586 | 0 | for (j = 0;j < state->nbTrans;j++) { |
587 | 0 | trans = &(state->trans[j]); |
588 | 0 | if ((trans->to < 0) || (trans->atom == NULL)) |
589 | 0 | continue; |
590 | 0 | atomno = stringRemap[trans->atom->no]; |
591 | 0 | if ((trans->atom->data != NULL) && (transdata == NULL)) { |
592 | 0 | transdata = (void **) xmlRegCalloc2(nbstates, nbatoms, |
593 | 0 | sizeof(void *)); |
594 | 0 | if (transdata == NULL) { |
595 | 0 | xmlRegexpErrMemory(ctxt); |
596 | 0 | break; |
597 | 0 | } |
598 | 0 | } |
599 | 0 | targetno = stateRemap[trans->to]; |
600 | | /* |
601 | | * if the same atom can generate transitions to 2 different |
602 | | * states then it means the automata is not deterministic and |
603 | | * the compact form can't be used ! |
604 | | */ |
605 | 0 | prev = transitions[stateno * (nbatoms + 1) + atomno + 1]; |
606 | 0 | if (prev != 0) { |
607 | 0 | if (prev != targetno + 1) { |
608 | 0 | ret->determinist = 0; |
609 | 0 | if (transdata != NULL) |
610 | 0 | xmlFree(transdata); |
611 | 0 | xmlFree(transitions); |
612 | 0 | xmlFree(stateRemap); |
613 | 0 | xmlFree(stringRemap); |
614 | 0 | for (i = 0;i < nbatoms;i++) |
615 | 0 | xmlFree(stringMap[i]); |
616 | 0 | xmlFree(stringMap); |
617 | 0 | goto not_determ; |
618 | 0 | } |
619 | 0 | } else { |
620 | 0 | transitions[stateno * (nbatoms + 1) + atomno + 1] = |
621 | 0 | targetno + 1; /* to avoid 0 */ |
622 | 0 | if (transdata != NULL) |
623 | 0 | transdata[stateno * nbatoms + atomno] = |
624 | 0 | trans->atom->data; |
625 | 0 | } |
626 | 0 | } |
627 | 0 | } |
628 | 0 | ret->determinist = 1; |
629 | | /* |
630 | | * Cleanup of the old data |
631 | | */ |
632 | 0 | if (ret->states != NULL) { |
633 | 0 | for (i = 0;i < ret->nbStates;i++) |
634 | 0 | xmlRegFreeState(ret->states[i]); |
635 | 0 | xmlFree(ret->states); |
636 | 0 | } |
637 | 0 | ret->states = NULL; |
638 | 0 | ret->nbStates = 0; |
639 | 0 | if (ret->atoms != NULL) { |
640 | 0 | for (i = 0;i < ret->nbAtoms;i++) |
641 | 0 | xmlRegFreeAtom(ret->atoms[i]); |
642 | 0 | xmlFree(ret->atoms); |
643 | 0 | } |
644 | 0 | ret->atoms = NULL; |
645 | 0 | ret->nbAtoms = 0; |
646 | |
|
647 | 0 | ret->compact = transitions; |
648 | 0 | ret->transdata = transdata; |
649 | 0 | ret->stringMap = stringMap; |
650 | 0 | ret->nbstrings = nbatoms; |
651 | 0 | ret->nbstates = nbstates; |
652 | 0 | xmlFree(stateRemap); |
653 | 0 | xmlFree(stringRemap); |
654 | 0 | } |
655 | 0 | not_determ: |
656 | 0 | ctxt->string = NULL; |
657 | 0 | ctxt->nbStates = 0; |
658 | 0 | ctxt->states = NULL; |
659 | 0 | ctxt->nbAtoms = 0; |
660 | 0 | ctxt->atoms = NULL; |
661 | 0 | ctxt->nbCounters = 0; |
662 | 0 | ctxt->counters = NULL; |
663 | 0 | return(ret); |
664 | 0 | } |
665 | | |
666 | | /** |
667 | | * xmlRegNewParserCtxt: |
668 | | * @string: the string to parse |
669 | | * |
670 | | * Allocate a new regexp parser context |
671 | | * |
672 | | * Returns the new context or NULL in case of error |
673 | | */ |
674 | | static xmlRegParserCtxtPtr |
675 | 0 | xmlRegNewParserCtxt(const xmlChar *string) { |
676 | 0 | xmlRegParserCtxtPtr ret; |
677 | |
|
678 | 0 | ret = (xmlRegParserCtxtPtr) xmlMalloc(sizeof(xmlRegParserCtxt)); |
679 | 0 | if (ret == NULL) |
680 | 0 | return(NULL); |
681 | 0 | memset(ret, 0, sizeof(xmlRegParserCtxt)); |
682 | 0 | if (string != NULL) { |
683 | 0 | ret->string = xmlStrdup(string); |
684 | 0 | if (ret->string == NULL) { |
685 | 0 | xmlFree(ret); |
686 | 0 | return(NULL); |
687 | 0 | } |
688 | 0 | } |
689 | 0 | ret->cur = ret->string; |
690 | 0 | ret->neg = 0; |
691 | 0 | ret->negs = 0; |
692 | 0 | ret->error = 0; |
693 | 0 | ret->determinist = -1; |
694 | 0 | return(ret); |
695 | 0 | } |
696 | | |
697 | | /** |
698 | | * xmlRegNewRange: |
699 | | * @ctxt: the regexp parser context |
700 | | * @neg: is that negative |
701 | | * @type: the type of range |
702 | | * @start: the start codepoint |
703 | | * @end: the end codepoint |
704 | | * |
705 | | * Allocate a new regexp range |
706 | | * |
707 | | * Returns the new range or NULL in case of error |
708 | | */ |
709 | | static xmlRegRangePtr |
710 | | xmlRegNewRange(xmlRegParserCtxtPtr ctxt, |
711 | 0 | int neg, xmlRegAtomType type, int start, int end) { |
712 | 0 | xmlRegRangePtr ret; |
713 | |
|
714 | 0 | ret = (xmlRegRangePtr) xmlMalloc(sizeof(xmlRegRange)); |
715 | 0 | if (ret == NULL) { |
716 | 0 | xmlRegexpErrMemory(ctxt); |
717 | 0 | return(NULL); |
718 | 0 | } |
719 | 0 | ret->neg = neg; |
720 | 0 | ret->type = type; |
721 | 0 | ret->start = start; |
722 | 0 | ret->end = end; |
723 | 0 | return(ret); |
724 | 0 | } |
725 | | |
726 | | /** |
727 | | * xmlRegFreeRange: |
728 | | * @range: the regexp range |
729 | | * |
730 | | * Free a regexp range |
731 | | */ |
732 | | static void |
733 | 0 | xmlRegFreeRange(xmlRegRangePtr range) { |
734 | 0 | if (range == NULL) |
735 | 0 | return; |
736 | | |
737 | 0 | if (range->blockName != NULL) |
738 | 0 | xmlFree(range->blockName); |
739 | 0 | xmlFree(range); |
740 | 0 | } |
741 | | |
742 | | /** |
743 | | * xmlRegCopyRange: |
744 | | * @range: the regexp range |
745 | | * |
746 | | * Copy a regexp range |
747 | | * |
748 | | * Returns the new copy or NULL in case of error. |
749 | | */ |
750 | | static xmlRegRangePtr |
751 | 0 | xmlRegCopyRange(xmlRegParserCtxtPtr ctxt, xmlRegRangePtr range) { |
752 | 0 | xmlRegRangePtr ret; |
753 | |
|
754 | 0 | if (range == NULL) |
755 | 0 | return(NULL); |
756 | | |
757 | 0 | ret = xmlRegNewRange(ctxt, range->neg, range->type, range->start, |
758 | 0 | range->end); |
759 | 0 | if (ret == NULL) |
760 | 0 | return(NULL); |
761 | 0 | if (range->blockName != NULL) { |
762 | 0 | ret->blockName = xmlStrdup(range->blockName); |
763 | 0 | if (ret->blockName == NULL) { |
764 | 0 | xmlRegexpErrMemory(ctxt); |
765 | 0 | xmlRegFreeRange(ret); |
766 | 0 | return(NULL); |
767 | 0 | } |
768 | 0 | } |
769 | 0 | return(ret); |
770 | 0 | } |
771 | | |
772 | | /** |
773 | | * xmlRegNewAtom: |
774 | | * @ctxt: the regexp parser context |
775 | | * @type: the type of atom |
776 | | * |
777 | | * Allocate a new atom |
778 | | * |
779 | | * Returns the new atom or NULL in case of error |
780 | | */ |
781 | | static xmlRegAtomPtr |
782 | 0 | xmlRegNewAtom(xmlRegParserCtxtPtr ctxt, xmlRegAtomType type) { |
783 | 0 | xmlRegAtomPtr ret; |
784 | |
|
785 | 0 | ret = (xmlRegAtomPtr) xmlMalloc(sizeof(xmlRegAtom)); |
786 | 0 | if (ret == NULL) { |
787 | 0 | xmlRegexpErrMemory(ctxt); |
788 | 0 | return(NULL); |
789 | 0 | } |
790 | 0 | memset(ret, 0, sizeof(xmlRegAtom)); |
791 | 0 | ret->type = type; |
792 | 0 | ret->quant = XML_REGEXP_QUANT_ONCE; |
793 | 0 | ret->min = 0; |
794 | 0 | ret->max = 0; |
795 | 0 | return(ret); |
796 | 0 | } |
797 | | |
798 | | /** |
799 | | * xmlRegFreeAtom: |
800 | | * @atom: the regexp atom |
801 | | * |
802 | | * Free a regexp atom |
803 | | */ |
804 | | static void |
805 | 0 | xmlRegFreeAtom(xmlRegAtomPtr atom) { |
806 | 0 | int i; |
807 | |
|
808 | 0 | if (atom == NULL) |
809 | 0 | return; |
810 | | |
811 | 0 | for (i = 0;i < atom->nbRanges;i++) |
812 | 0 | xmlRegFreeRange(atom->ranges[i]); |
813 | 0 | if (atom->ranges != NULL) |
814 | 0 | xmlFree(atom->ranges); |
815 | 0 | if ((atom->type == XML_REGEXP_STRING) && (atom->valuep != NULL)) |
816 | 0 | xmlFree(atom->valuep); |
817 | 0 | if ((atom->type == XML_REGEXP_STRING) && (atom->valuep2 != NULL)) |
818 | 0 | xmlFree(atom->valuep2); |
819 | 0 | if ((atom->type == XML_REGEXP_BLOCK_NAME) && (atom->valuep != NULL)) |
820 | 0 | xmlFree(atom->valuep); |
821 | 0 | xmlFree(atom); |
822 | 0 | } |
823 | | |
824 | | /** |
825 | | * xmlRegCopyAtom: |
826 | | * @ctxt: the regexp parser context |
827 | | * @atom: the original atom |
828 | | * |
829 | | * Allocate a new regexp range |
830 | | * |
831 | | * Returns the new atom or NULL in case of error |
832 | | */ |
833 | | static xmlRegAtomPtr |
834 | 0 | xmlRegCopyAtom(xmlRegParserCtxtPtr ctxt, xmlRegAtomPtr atom) { |
835 | 0 | xmlRegAtomPtr ret; |
836 | |
|
837 | 0 | ret = (xmlRegAtomPtr) xmlMalloc(sizeof(xmlRegAtom)); |
838 | 0 | if (ret == NULL) { |
839 | 0 | xmlRegexpErrMemory(ctxt); |
840 | 0 | return(NULL); |
841 | 0 | } |
842 | 0 | memset(ret, 0, sizeof(xmlRegAtom)); |
843 | 0 | ret->type = atom->type; |
844 | 0 | ret->quant = atom->quant; |
845 | 0 | ret->min = atom->min; |
846 | 0 | ret->max = atom->max; |
847 | 0 | if (atom->nbRanges > 0) { |
848 | 0 | int i; |
849 | |
|
850 | 0 | ret->ranges = (xmlRegRangePtr *) xmlMalloc(sizeof(xmlRegRangePtr) * |
851 | 0 | atom->nbRanges); |
852 | 0 | if (ret->ranges == NULL) { |
853 | 0 | xmlRegexpErrMemory(ctxt); |
854 | 0 | goto error; |
855 | 0 | } |
856 | 0 | for (i = 0;i < atom->nbRanges;i++) { |
857 | 0 | ret->ranges[i] = xmlRegCopyRange(ctxt, atom->ranges[i]); |
858 | 0 | if (ret->ranges[i] == NULL) |
859 | 0 | goto error; |
860 | 0 | ret->nbRanges = i + 1; |
861 | 0 | } |
862 | 0 | } |
863 | 0 | return(ret); |
864 | | |
865 | 0 | error: |
866 | 0 | xmlRegFreeAtom(ret); |
867 | 0 | return(NULL); |
868 | 0 | } |
869 | | |
870 | | static xmlRegStatePtr |
871 | 0 | xmlRegNewState(xmlRegParserCtxtPtr ctxt) { |
872 | 0 | xmlRegStatePtr ret; |
873 | |
|
874 | 0 | ret = (xmlRegStatePtr) xmlMalloc(sizeof(xmlRegState)); |
875 | 0 | if (ret == NULL) { |
876 | 0 | xmlRegexpErrMemory(ctxt); |
877 | 0 | return(NULL); |
878 | 0 | } |
879 | 0 | memset(ret, 0, sizeof(xmlRegState)); |
880 | 0 | ret->type = XML_REGEXP_TRANS_STATE; |
881 | 0 | ret->mark = XML_REGEXP_MARK_NORMAL; |
882 | 0 | return(ret); |
883 | 0 | } |
884 | | |
885 | | /** |
886 | | * xmlRegFreeState: |
887 | | * @state: the regexp state |
888 | | * |
889 | | * Free a regexp state |
890 | | */ |
891 | | static void |
892 | 0 | xmlRegFreeState(xmlRegStatePtr state) { |
893 | 0 | if (state == NULL) |
894 | 0 | return; |
895 | | |
896 | 0 | if (state->trans != NULL) |
897 | 0 | xmlFree(state->trans); |
898 | 0 | if (state->transTo != NULL) |
899 | 0 | xmlFree(state->transTo); |
900 | 0 | xmlFree(state); |
901 | 0 | } |
902 | | |
903 | | /** |
904 | | * xmlRegFreeParserCtxt: |
905 | | * @ctxt: the regexp parser context |
906 | | * |
907 | | * Free a regexp parser context |
908 | | */ |
909 | | static void |
910 | 0 | xmlRegFreeParserCtxt(xmlRegParserCtxtPtr ctxt) { |
911 | 0 | int i; |
912 | 0 | if (ctxt == NULL) |
913 | 0 | return; |
914 | | |
915 | 0 | if (ctxt->string != NULL) |
916 | 0 | xmlFree(ctxt->string); |
917 | 0 | if (ctxt->states != NULL) { |
918 | 0 | for (i = 0;i < ctxt->nbStates;i++) |
919 | 0 | xmlRegFreeState(ctxt->states[i]); |
920 | 0 | xmlFree(ctxt->states); |
921 | 0 | } |
922 | 0 | if (ctxt->atoms != NULL) { |
923 | 0 | for (i = 0;i < ctxt->nbAtoms;i++) |
924 | 0 | xmlRegFreeAtom(ctxt->atoms[i]); |
925 | 0 | xmlFree(ctxt->atoms); |
926 | 0 | } |
927 | 0 | if (ctxt->counters != NULL) |
928 | 0 | xmlFree(ctxt->counters); |
929 | 0 | xmlFree(ctxt); |
930 | 0 | } |
931 | | |
932 | | /************************************************************************ |
933 | | * * |
934 | | * Display of Data structures * |
935 | | * * |
936 | | ************************************************************************/ |
937 | | |
938 | | #ifdef DEBUG_REGEXP |
939 | | static void |
940 | | xmlRegPrintAtomType(FILE *output, xmlRegAtomType type) { |
941 | | switch (type) { |
942 | | case XML_REGEXP_EPSILON: |
943 | | fprintf(output, "epsilon "); break; |
944 | | case XML_REGEXP_CHARVAL: |
945 | | fprintf(output, "charval "); break; |
946 | | case XML_REGEXP_RANGES: |
947 | | fprintf(output, "ranges "); break; |
948 | | case XML_REGEXP_SUBREG: |
949 | | fprintf(output, "subexpr "); break; |
950 | | case XML_REGEXP_STRING: |
951 | | fprintf(output, "string "); break; |
952 | | case XML_REGEXP_ANYCHAR: |
953 | | fprintf(output, "anychar "); break; |
954 | | case XML_REGEXP_ANYSPACE: |
955 | | fprintf(output, "anyspace "); break; |
956 | | case XML_REGEXP_NOTSPACE: |
957 | | fprintf(output, "notspace "); break; |
958 | | case XML_REGEXP_INITNAME: |
959 | | fprintf(output, "initname "); break; |
960 | | case XML_REGEXP_NOTINITNAME: |
961 | | fprintf(output, "notinitname "); break; |
962 | | case XML_REGEXP_NAMECHAR: |
963 | | fprintf(output, "namechar "); break; |
964 | | case XML_REGEXP_NOTNAMECHAR: |
965 | | fprintf(output, "notnamechar "); break; |
966 | | case XML_REGEXP_DECIMAL: |
967 | | fprintf(output, "decimal "); break; |
968 | | case XML_REGEXP_NOTDECIMAL: |
969 | | fprintf(output, "notdecimal "); break; |
970 | | case XML_REGEXP_REALCHAR: |
971 | | fprintf(output, "realchar "); break; |
972 | | case XML_REGEXP_NOTREALCHAR: |
973 | | fprintf(output, "notrealchar "); break; |
974 | | case XML_REGEXP_LETTER: |
975 | | fprintf(output, "LETTER "); break; |
976 | | case XML_REGEXP_LETTER_UPPERCASE: |
977 | | fprintf(output, "LETTER_UPPERCASE "); break; |
978 | | case XML_REGEXP_LETTER_LOWERCASE: |
979 | | fprintf(output, "LETTER_LOWERCASE "); break; |
980 | | case XML_REGEXP_LETTER_TITLECASE: |
981 | | fprintf(output, "LETTER_TITLECASE "); break; |
982 | | case XML_REGEXP_LETTER_MODIFIER: |
983 | | fprintf(output, "LETTER_MODIFIER "); break; |
984 | | case XML_REGEXP_LETTER_OTHERS: |
985 | | fprintf(output, "LETTER_OTHERS "); break; |
986 | | case XML_REGEXP_MARK: |
987 | | fprintf(output, "MARK "); break; |
988 | | case XML_REGEXP_MARK_NONSPACING: |
989 | | fprintf(output, "MARK_NONSPACING "); break; |
990 | | case XML_REGEXP_MARK_SPACECOMBINING: |
991 | | fprintf(output, "MARK_SPACECOMBINING "); break; |
992 | | case XML_REGEXP_MARK_ENCLOSING: |
993 | | fprintf(output, "MARK_ENCLOSING "); break; |
994 | | case XML_REGEXP_NUMBER: |
995 | | fprintf(output, "NUMBER "); break; |
996 | | case XML_REGEXP_NUMBER_DECIMAL: |
997 | | fprintf(output, "NUMBER_DECIMAL "); break; |
998 | | case XML_REGEXP_NUMBER_LETTER: |
999 | | fprintf(output, "NUMBER_LETTER "); break; |
1000 | | case XML_REGEXP_NUMBER_OTHERS: |
1001 | | fprintf(output, "NUMBER_OTHERS "); break; |
1002 | | case XML_REGEXP_PUNCT: |
1003 | | fprintf(output, "PUNCT "); break; |
1004 | | case XML_REGEXP_PUNCT_CONNECTOR: |
1005 | | fprintf(output, "PUNCT_CONNECTOR "); break; |
1006 | | case XML_REGEXP_PUNCT_DASH: |
1007 | | fprintf(output, "PUNCT_DASH "); break; |
1008 | | case XML_REGEXP_PUNCT_OPEN: |
1009 | | fprintf(output, "PUNCT_OPEN "); break; |
1010 | | case XML_REGEXP_PUNCT_CLOSE: |
1011 | | fprintf(output, "PUNCT_CLOSE "); break; |
1012 | | case XML_REGEXP_PUNCT_INITQUOTE: |
1013 | | fprintf(output, "PUNCT_INITQUOTE "); break; |
1014 | | case XML_REGEXP_PUNCT_FINQUOTE: |
1015 | | fprintf(output, "PUNCT_FINQUOTE "); break; |
1016 | | case XML_REGEXP_PUNCT_OTHERS: |
1017 | | fprintf(output, "PUNCT_OTHERS "); break; |
1018 | | case XML_REGEXP_SEPAR: |
1019 | | fprintf(output, "SEPAR "); break; |
1020 | | case XML_REGEXP_SEPAR_SPACE: |
1021 | | fprintf(output, "SEPAR_SPACE "); break; |
1022 | | case XML_REGEXP_SEPAR_LINE: |
1023 | | fprintf(output, "SEPAR_LINE "); break; |
1024 | | case XML_REGEXP_SEPAR_PARA: |
1025 | | fprintf(output, "SEPAR_PARA "); break; |
1026 | | case XML_REGEXP_SYMBOL: |
1027 | | fprintf(output, "SYMBOL "); break; |
1028 | | case XML_REGEXP_SYMBOL_MATH: |
1029 | | fprintf(output, "SYMBOL_MATH "); break; |
1030 | | case XML_REGEXP_SYMBOL_CURRENCY: |
1031 | | fprintf(output, "SYMBOL_CURRENCY "); break; |
1032 | | case XML_REGEXP_SYMBOL_MODIFIER: |
1033 | | fprintf(output, "SYMBOL_MODIFIER "); break; |
1034 | | case XML_REGEXP_SYMBOL_OTHERS: |
1035 | | fprintf(output, "SYMBOL_OTHERS "); break; |
1036 | | case XML_REGEXP_OTHER: |
1037 | | fprintf(output, "OTHER "); break; |
1038 | | case XML_REGEXP_OTHER_CONTROL: |
1039 | | fprintf(output, "OTHER_CONTROL "); break; |
1040 | | case XML_REGEXP_OTHER_FORMAT: |
1041 | | fprintf(output, "OTHER_FORMAT "); break; |
1042 | | case XML_REGEXP_OTHER_PRIVATE: |
1043 | | fprintf(output, "OTHER_PRIVATE "); break; |
1044 | | case XML_REGEXP_OTHER_NA: |
1045 | | fprintf(output, "OTHER_NA "); break; |
1046 | | case XML_REGEXP_BLOCK_NAME: |
1047 | | fprintf(output, "BLOCK "); break; |
1048 | | } |
1049 | | } |
1050 | | |
1051 | | static void |
1052 | | xmlRegPrintQuantType(FILE *output, xmlRegQuantType type) { |
1053 | | switch (type) { |
1054 | | case XML_REGEXP_QUANT_EPSILON: |
1055 | | fprintf(output, "epsilon "); break; |
1056 | | case XML_REGEXP_QUANT_ONCE: |
1057 | | fprintf(output, "once "); break; |
1058 | | case XML_REGEXP_QUANT_OPT: |
1059 | | fprintf(output, "? "); break; |
1060 | | case XML_REGEXP_QUANT_MULT: |
1061 | | fprintf(output, "* "); break; |
1062 | | case XML_REGEXP_QUANT_PLUS: |
1063 | | fprintf(output, "+ "); break; |
1064 | | case XML_REGEXP_QUANT_RANGE: |
1065 | | fprintf(output, "range "); break; |
1066 | | case XML_REGEXP_QUANT_ONCEONLY: |
1067 | | fprintf(output, "onceonly "); break; |
1068 | | case XML_REGEXP_QUANT_ALL: |
1069 | | fprintf(output, "all "); break; |
1070 | | } |
1071 | | } |
1072 | | static void |
1073 | | xmlRegPrintRange(FILE *output, xmlRegRangePtr range) { |
1074 | | fprintf(output, " range: "); |
1075 | | if (range->neg) |
1076 | | fprintf(output, "negative "); |
1077 | | xmlRegPrintAtomType(output, range->type); |
1078 | | fprintf(output, "%c - %c\n", range->start, range->end); |
1079 | | } |
1080 | | |
1081 | | static void |
1082 | | xmlRegPrintAtom(FILE *output, xmlRegAtomPtr atom) { |
1083 | | fprintf(output, " atom: "); |
1084 | | if (atom == NULL) { |
1085 | | fprintf(output, "NULL\n"); |
1086 | | return; |
1087 | | } |
1088 | | if (atom->neg) |
1089 | | fprintf(output, "not "); |
1090 | | xmlRegPrintAtomType(output, atom->type); |
1091 | | xmlRegPrintQuantType(output, atom->quant); |
1092 | | if (atom->quant == XML_REGEXP_QUANT_RANGE) |
1093 | | fprintf(output, "%d-%d ", atom->min, atom->max); |
1094 | | if (atom->type == XML_REGEXP_STRING) |
1095 | | fprintf(output, "'%s' ", (char *) atom->valuep); |
1096 | | if (atom->type == XML_REGEXP_CHARVAL) |
1097 | | fprintf(output, "char %c\n", atom->codepoint); |
1098 | | else if (atom->type == XML_REGEXP_RANGES) { |
1099 | | int i; |
1100 | | fprintf(output, "%d entries\n", atom->nbRanges); |
1101 | | for (i = 0; i < atom->nbRanges;i++) |
1102 | | xmlRegPrintRange(output, atom->ranges[i]); |
1103 | | } else { |
1104 | | fprintf(output, "\n"); |
1105 | | } |
1106 | | } |
1107 | | |
1108 | | static void |
1109 | | xmlRegPrintAtomCompact(FILE* output, xmlRegexpPtr regexp, int atom) |
1110 | | { |
1111 | | if (output == NULL || regexp == NULL || atom < 0 || |
1112 | | atom >= regexp->nbstrings) { |
1113 | | return; |
1114 | | } |
1115 | | fprintf(output, " atom: "); |
1116 | | |
1117 | | xmlRegPrintAtomType(output, XML_REGEXP_STRING); |
1118 | | xmlRegPrintQuantType(output, XML_REGEXP_QUANT_ONCE); |
1119 | | fprintf(output, "'%s' ", (char *) regexp->stringMap[atom]); |
1120 | | fprintf(output, "\n"); |
1121 | | } |
1122 | | |
1123 | | static void |
1124 | | xmlRegPrintTrans(FILE *output, xmlRegTransPtr trans) { |
1125 | | fprintf(output, " trans: "); |
1126 | | if (trans == NULL) { |
1127 | | fprintf(output, "NULL\n"); |
1128 | | return; |
1129 | | } |
1130 | | if (trans->to < 0) { |
1131 | | fprintf(output, "removed\n"); |
1132 | | return; |
1133 | | } |
1134 | | if (trans->nd != 0) { |
1135 | | if (trans->nd == 2) |
1136 | | fprintf(output, "last not determinist, "); |
1137 | | else |
1138 | | fprintf(output, "not determinist, "); |
1139 | | } |
1140 | | if (trans->counter >= 0) { |
1141 | | fprintf(output, "counted %d, ", trans->counter); |
1142 | | } |
1143 | | if (trans->count == REGEXP_ALL_COUNTER) { |
1144 | | fprintf(output, "all transition, "); |
1145 | | } else if (trans->count >= 0) { |
1146 | | fprintf(output, "count based %d, ", trans->count); |
1147 | | } |
1148 | | if (trans->atom == NULL) { |
1149 | | fprintf(output, "epsilon to %d\n", trans->to); |
1150 | | return; |
1151 | | } |
1152 | | if (trans->atom->type == XML_REGEXP_CHARVAL) |
1153 | | fprintf(output, "char %c ", trans->atom->codepoint); |
1154 | | fprintf(output, "atom %d, to %d\n", trans->atom->no, trans->to); |
1155 | | } |
1156 | | |
1157 | | static void |
1158 | | xmlRegPrintTransCompact( |
1159 | | FILE* output, |
1160 | | xmlRegexpPtr regexp, |
1161 | | int state, |
1162 | | int atom |
1163 | | ) |
1164 | | { |
1165 | | int target; |
1166 | | if (output == NULL || regexp == NULL || regexp->compact == NULL || |
1167 | | state < 0 || atom < 0) { |
1168 | | return; |
1169 | | } |
1170 | | target = regexp->compact[state * (regexp->nbstrings + 1) + atom + 1]; |
1171 | | fprintf(output, " trans: "); |
1172 | | |
1173 | | /* TODO maybe skip 'removed' transitions, because they actually never existed */ |
1174 | | if (target < 0) { |
1175 | | fprintf(output, "removed\n"); |
1176 | | return; |
1177 | | } |
1178 | | |
1179 | | /* We will ignore most of the attributes used in xmlRegPrintTrans, |
1180 | | * since the compact form is much simpler and uses only a part of the |
1181 | | * features provided by the libxml2 regexp libary |
1182 | | * (no rollbacks, counters etc.) */ |
1183 | | |
1184 | | /* Compared to the standard representation, an automata written using the |
1185 | | * compact form will ALWAYS be deterministic! |
1186 | | * From xmlRegPrintTrans: |
1187 | | if (trans->nd != 0) { |
1188 | | ... |
1189 | | * trans->nd will always be 0! */ |
1190 | | |
1191 | | /* In automata represented in compact form, the transitions will not use |
1192 | | * counters. |
1193 | | * From xmlRegPrintTrans: |
1194 | | if (trans->counter >= 0) { |
1195 | | ... |
1196 | | * regexp->counters == NULL, so trans->counter < 0 */ |
1197 | | |
1198 | | /* In compact form, we won't use */ |
1199 | | |
1200 | | /* An automata in the compact representation will always use string |
1201 | | * atoms. |
1202 | | * From xmlRegPrintTrans: |
1203 | | if (trans->atom->type == XML_REGEXP_CHARVAL) |
1204 | | ... |
1205 | | * trans->atom != NULL && trans->atom->type == XML_REGEXP_STRING */ |
1206 | | |
1207 | | fprintf(output, "atom %d, to %d\n", atom, target); |
1208 | | } |
1209 | | |
1210 | | static void |
1211 | | xmlRegPrintState(FILE *output, xmlRegStatePtr state) { |
1212 | | int i; |
1213 | | |
1214 | | fprintf(output, " state: "); |
1215 | | if (state == NULL) { |
1216 | | fprintf(output, "NULL\n"); |
1217 | | return; |
1218 | | } |
1219 | | if (state->type == XML_REGEXP_START_STATE) |
1220 | | fprintf(output, "START "); |
1221 | | if (state->type == XML_REGEXP_FINAL_STATE) |
1222 | | fprintf(output, "FINAL "); |
1223 | | |
1224 | | fprintf(output, "%d, %d transitions:\n", state->no, state->nbTrans); |
1225 | | for (i = 0;i < state->nbTrans; i++) { |
1226 | | xmlRegPrintTrans(output, &(state->trans[i])); |
1227 | | } |
1228 | | } |
1229 | | |
1230 | | static void |
1231 | | xmlRegPrintStateCompact(FILE* output, xmlRegexpPtr regexp, int state) |
1232 | | { |
1233 | | int nbTrans = 0; |
1234 | | int i; |
1235 | | int target; |
1236 | | xmlRegStateType stateType; |
1237 | | |
1238 | | if (output == NULL || regexp == NULL || regexp->compact == NULL || |
1239 | | state < 0) { |
1240 | | return; |
1241 | | } |
1242 | | |
1243 | | fprintf(output, " state: "); |
1244 | | |
1245 | | stateType = regexp->compact[state * (regexp->nbstrings + 1)]; |
1246 | | if (stateType == XML_REGEXP_START_STATE) { |
1247 | | fprintf(output, " START "); |
1248 | | } |
1249 | | |
1250 | | if (stateType == XML_REGEXP_FINAL_STATE) { |
1251 | | fprintf(output, " FINAL "); |
1252 | | } |
1253 | | |
1254 | | /* Print all atoms. */ |
1255 | | for (i = 0; i < regexp->nbstrings; i++) { |
1256 | | xmlRegPrintAtomCompact(output, regexp, i); |
1257 | | } |
1258 | | |
1259 | | /* Count all the transitions from the compact representation. */ |
1260 | | for (i = 0; i < regexp->nbstrings; i++) { |
1261 | | target = regexp->compact[state * (regexp->nbstrings + 1) + i + 1]; |
1262 | | if (target > 0 && target <= regexp->nbstates && |
1263 | | regexp->compact[(target - 1) * (regexp->nbstrings + 1)] == |
1264 | | XML_REGEXP_SINK_STATE) { |
1265 | | nbTrans++; |
1266 | | } |
1267 | | } |
1268 | | |
1269 | | fprintf(output, "%d, %d transitions:\n", state, nbTrans); |
1270 | | |
1271 | | /* Print all transitions */ |
1272 | | for (i = 0; i < regexp->nbstrings; i++) { |
1273 | | xmlRegPrintTransCompact(output, regexp, state, i); |
1274 | | } |
1275 | | } |
1276 | | |
1277 | | /* |
1278 | | * xmlRegPrintCompact |
1279 | | * @output an output stream |
1280 | | * @regexp the regexp instance |
1281 | | * |
1282 | | * Print the compact representation of a regexp, in the same fashion as the |
1283 | | * public xmlRegexpPrint function. |
1284 | | */ |
1285 | | static void |
1286 | | xmlRegPrintCompact(FILE* output, xmlRegexpPtr regexp) |
1287 | | { |
1288 | | int i; |
1289 | | if (output == NULL || regexp == NULL || regexp->compact == NULL) { |
1290 | | return; |
1291 | | } |
1292 | | |
1293 | | fprintf(output, "'%s' ", regexp->string); |
1294 | | |
1295 | | fprintf(output, "%d atoms:\n", regexp->nbstrings); |
1296 | | fprintf(output, "\n"); |
1297 | | for (i = 0; i < regexp->nbstrings; i++) { |
1298 | | fprintf(output, " %02d ", i); |
1299 | | xmlRegPrintAtomCompact(output, regexp, i); |
1300 | | } |
1301 | | |
1302 | | fprintf(output, "%d states:", regexp->nbstates); |
1303 | | fprintf(output, "\n"); |
1304 | | for (i = 0; i < regexp->nbstates; i++) { |
1305 | | xmlRegPrintStateCompact(output, regexp, i); |
1306 | | } |
1307 | | |
1308 | | fprintf(output, "%d counters:\n", 0); |
1309 | | } |
1310 | | |
1311 | | static void |
1312 | | xmlRegexpPrintInternal(FILE *output, xmlRegexpPtr regexp) { |
1313 | | int i; |
1314 | | |
1315 | | if (output == NULL) |
1316 | | return; |
1317 | | fprintf(output, " regexp: "); |
1318 | | if (regexp == NULL) { |
1319 | | fprintf(output, "NULL\n"); |
1320 | | return; |
1321 | | } |
1322 | | if (regexp->compact) { |
1323 | | xmlRegPrintCompact(output, regexp); |
1324 | | return; |
1325 | | } |
1326 | | |
1327 | | fprintf(output, "'%s' ", regexp->string); |
1328 | | fprintf(output, "\n"); |
1329 | | fprintf(output, "%d atoms:\n", regexp->nbAtoms); |
1330 | | for (i = 0;i < regexp->nbAtoms; i++) { |
1331 | | fprintf(output, " %02d ", i); |
1332 | | xmlRegPrintAtom(output, regexp->atoms[i]); |
1333 | | } |
1334 | | fprintf(output, "%d states:", regexp->nbStates); |
1335 | | fprintf(output, "\n"); |
1336 | | for (i = 0;i < regexp->nbStates; i++) { |
1337 | | xmlRegPrintState(output, regexp->states[i]); |
1338 | | } |
1339 | | fprintf(output, "%d counters:\n", regexp->nbCounters); |
1340 | | for (i = 0;i < regexp->nbCounters; i++) { |
1341 | | fprintf(output, " %d: min %d max %d\n", i, regexp->counters[i].min, |
1342 | | regexp->counters[i].max); |
1343 | | } |
1344 | | } |
1345 | | #endif /* DEBUG_REGEXP */ |
1346 | | |
1347 | | /************************************************************************ |
1348 | | * * |
1349 | | * Finite Automata structures manipulations * |
1350 | | * * |
1351 | | ************************************************************************/ |
1352 | | |
1353 | | static xmlRegRangePtr |
1354 | | xmlRegAtomAddRange(xmlRegParserCtxtPtr ctxt, xmlRegAtomPtr atom, |
1355 | | int neg, xmlRegAtomType type, int start, int end, |
1356 | 0 | xmlChar *blockName) { |
1357 | 0 | xmlRegRangePtr range; |
1358 | |
|
1359 | 0 | if (atom == NULL) { |
1360 | 0 | ERROR("add range: atom is NULL"); |
1361 | 0 | return(NULL); |
1362 | 0 | } |
1363 | 0 | if (atom->type != XML_REGEXP_RANGES) { |
1364 | 0 | ERROR("add range: atom is not ranges"); |
1365 | 0 | return(NULL); |
1366 | 0 | } |
1367 | 0 | if (atom->nbRanges >= atom->maxRanges) { |
1368 | 0 | xmlRegRangePtr *tmp; |
1369 | 0 | int newSize; |
1370 | |
|
1371 | 0 | newSize = xmlGrowCapacity(atom->maxRanges, sizeof(tmp[0]), |
1372 | 0 | 4, XML_MAX_ITEMS); |
1373 | 0 | if (newSize < 0) { |
1374 | 0 | xmlRegexpErrMemory(ctxt); |
1375 | 0 | return(NULL); |
1376 | 0 | } |
1377 | 0 | tmp = xmlRealloc(atom->ranges, newSize * sizeof(tmp[0])); |
1378 | 0 | if (tmp == NULL) { |
1379 | 0 | xmlRegexpErrMemory(ctxt); |
1380 | 0 | return(NULL); |
1381 | 0 | } |
1382 | 0 | atom->ranges = tmp; |
1383 | 0 | atom->maxRanges = newSize; |
1384 | 0 | } |
1385 | 0 | range = xmlRegNewRange(ctxt, neg, type, start, end); |
1386 | 0 | if (range == NULL) |
1387 | 0 | return(NULL); |
1388 | 0 | range->blockName = blockName; |
1389 | 0 | atom->ranges[atom->nbRanges++] = range; |
1390 | |
|
1391 | 0 | return(range); |
1392 | 0 | } |
1393 | | |
1394 | | static int |
1395 | 0 | xmlRegGetCounter(xmlRegParserCtxtPtr ctxt) { |
1396 | 0 | if (ctxt->nbCounters >= ctxt->maxCounters) { |
1397 | 0 | xmlRegCounter *tmp; |
1398 | 0 | int newSize; |
1399 | |
|
1400 | 0 | newSize = xmlGrowCapacity(ctxt->maxCounters, sizeof(tmp[0]), |
1401 | 0 | 4, XML_MAX_ITEMS); |
1402 | 0 | if (newSize < 0) { |
1403 | 0 | xmlRegexpErrMemory(ctxt); |
1404 | 0 | return(-1); |
1405 | 0 | } |
1406 | 0 | tmp = xmlRealloc(ctxt->counters, newSize * sizeof(tmp[0])); |
1407 | 0 | if (tmp == NULL) { |
1408 | 0 | xmlRegexpErrMemory(ctxt); |
1409 | 0 | return(-1); |
1410 | 0 | } |
1411 | 0 | ctxt->counters = tmp; |
1412 | 0 | ctxt->maxCounters = newSize; |
1413 | 0 | } |
1414 | 0 | ctxt->counters[ctxt->nbCounters].min = -1; |
1415 | 0 | ctxt->counters[ctxt->nbCounters].max = -1; |
1416 | 0 | return(ctxt->nbCounters++); |
1417 | 0 | } |
1418 | | |
1419 | | static int |
1420 | 0 | xmlRegAtomPush(xmlRegParserCtxtPtr ctxt, xmlRegAtomPtr atom) { |
1421 | 0 | if (atom == NULL) { |
1422 | 0 | ERROR("atom push: atom is NULL"); |
1423 | 0 | return(-1); |
1424 | 0 | } |
1425 | 0 | if (ctxt->nbAtoms >= ctxt->maxAtoms) { |
1426 | 0 | xmlRegAtomPtr *tmp; |
1427 | 0 | int newSize; |
1428 | |
|
1429 | 0 | newSize = xmlGrowCapacity(ctxt->maxAtoms, sizeof(tmp[0]), |
1430 | 0 | 4, XML_MAX_ITEMS); |
1431 | 0 | if (newSize < 0) { |
1432 | 0 | xmlRegexpErrMemory(ctxt); |
1433 | 0 | return(-1); |
1434 | 0 | } |
1435 | 0 | tmp = xmlRealloc(ctxt->atoms, newSize * sizeof(tmp[0])); |
1436 | 0 | if (tmp == NULL) { |
1437 | 0 | xmlRegexpErrMemory(ctxt); |
1438 | 0 | return(-1); |
1439 | 0 | } |
1440 | 0 | ctxt->atoms = tmp; |
1441 | 0 | ctxt->maxAtoms = newSize; |
1442 | 0 | } |
1443 | 0 | atom->no = ctxt->nbAtoms; |
1444 | 0 | ctxt->atoms[ctxt->nbAtoms++] = atom; |
1445 | 0 | return(0); |
1446 | 0 | } |
1447 | | |
1448 | | static void |
1449 | | xmlRegStateAddTransTo(xmlRegParserCtxtPtr ctxt, xmlRegStatePtr target, |
1450 | 0 | int from) { |
1451 | 0 | if (target->nbTransTo >= target->maxTransTo) { |
1452 | 0 | int *tmp; |
1453 | 0 | int newSize; |
1454 | |
|
1455 | 0 | newSize = xmlGrowCapacity(target->maxTransTo, sizeof(tmp[0]), |
1456 | 0 | 8, XML_MAX_ITEMS); |
1457 | 0 | if (newSize < 0) { |
1458 | 0 | xmlRegexpErrMemory(ctxt); |
1459 | 0 | return; |
1460 | 0 | } |
1461 | 0 | tmp = xmlRealloc(target->transTo, newSize * sizeof(tmp[0])); |
1462 | 0 | if (tmp == NULL) { |
1463 | 0 | xmlRegexpErrMemory(ctxt); |
1464 | 0 | return; |
1465 | 0 | } |
1466 | 0 | target->transTo = tmp; |
1467 | 0 | target->maxTransTo = newSize; |
1468 | 0 | } |
1469 | 0 | target->transTo[target->nbTransTo] = from; |
1470 | 0 | target->nbTransTo++; |
1471 | 0 | } |
1472 | | |
1473 | | static void |
1474 | | xmlRegStateAddTrans(xmlRegParserCtxtPtr ctxt, xmlRegStatePtr state, |
1475 | | xmlRegAtomPtr atom, xmlRegStatePtr target, |
1476 | 0 | int counter, int count) { |
1477 | |
|
1478 | 0 | int nrtrans; |
1479 | |
|
1480 | 0 | if (state == NULL) { |
1481 | 0 | ERROR("add state: state is NULL"); |
1482 | 0 | return; |
1483 | 0 | } |
1484 | 0 | if (target == NULL) { |
1485 | 0 | ERROR("add state: target is NULL"); |
1486 | 0 | return; |
1487 | 0 | } |
1488 | | /* |
1489 | | * Other routines follow the philosophy 'When in doubt, add a transition' |
1490 | | * so we check here whether such a transition is already present and, if |
1491 | | * so, silently ignore this request. |
1492 | | */ |
1493 | | |
1494 | 0 | for (nrtrans = state->nbTrans - 1; nrtrans >= 0; nrtrans--) { |
1495 | 0 | xmlRegTransPtr trans = &(state->trans[nrtrans]); |
1496 | 0 | if ((trans->atom == atom) && |
1497 | 0 | (trans->to == target->no) && |
1498 | 0 | (trans->counter == counter) && |
1499 | 0 | (trans->count == count)) { |
1500 | 0 | return; |
1501 | 0 | } |
1502 | 0 | } |
1503 | | |
1504 | 0 | if (state->nbTrans >= state->maxTrans) { |
1505 | 0 | xmlRegTrans *tmp; |
1506 | 0 | int newSize; |
1507 | |
|
1508 | 0 | newSize = xmlGrowCapacity(state->maxTrans, sizeof(tmp[0]), |
1509 | 0 | 8, XML_MAX_ITEMS); |
1510 | 0 | if (newSize < 0) { |
1511 | 0 | xmlRegexpErrMemory(ctxt); |
1512 | 0 | return; |
1513 | 0 | } |
1514 | 0 | tmp = xmlRealloc(state->trans, newSize * sizeof(tmp[0])); |
1515 | 0 | if (tmp == NULL) { |
1516 | 0 | xmlRegexpErrMemory(ctxt); |
1517 | 0 | return; |
1518 | 0 | } |
1519 | 0 | state->trans = tmp; |
1520 | 0 | state->maxTrans = newSize; |
1521 | 0 | } |
1522 | | |
1523 | 0 | state->trans[state->nbTrans].atom = atom; |
1524 | 0 | state->trans[state->nbTrans].to = target->no; |
1525 | 0 | state->trans[state->nbTrans].counter = counter; |
1526 | 0 | state->trans[state->nbTrans].count = count; |
1527 | 0 | state->trans[state->nbTrans].nd = 0; |
1528 | 0 | state->nbTrans++; |
1529 | 0 | xmlRegStateAddTransTo(ctxt, target, state->no); |
1530 | 0 | } |
1531 | | |
1532 | | static xmlRegStatePtr |
1533 | 0 | xmlRegStatePush(xmlRegParserCtxtPtr ctxt) { |
1534 | 0 | xmlRegStatePtr state; |
1535 | |
|
1536 | 0 | if (ctxt->nbStates >= ctxt->maxStates) { |
1537 | 0 | xmlRegStatePtr *tmp; |
1538 | 0 | int newSize; |
1539 | |
|
1540 | 0 | newSize = xmlGrowCapacity(ctxt->maxStates, sizeof(tmp[0]), |
1541 | 0 | 4, XML_MAX_ITEMS); |
1542 | 0 | if (newSize < 0) { |
1543 | 0 | xmlRegexpErrMemory(ctxt); |
1544 | 0 | return(NULL); |
1545 | 0 | } |
1546 | 0 | tmp = xmlRealloc(ctxt->states, newSize * sizeof(tmp[0])); |
1547 | 0 | if (tmp == NULL) { |
1548 | 0 | xmlRegexpErrMemory(ctxt); |
1549 | 0 | return(NULL); |
1550 | 0 | } |
1551 | 0 | ctxt->states = tmp; |
1552 | 0 | ctxt->maxStates = newSize; |
1553 | 0 | } |
1554 | | |
1555 | 0 | state = xmlRegNewState(ctxt); |
1556 | 0 | if (state == NULL) |
1557 | 0 | return(NULL); |
1558 | | |
1559 | 0 | state->no = ctxt->nbStates; |
1560 | 0 | ctxt->states[ctxt->nbStates++] = state; |
1561 | |
|
1562 | 0 | return(state); |
1563 | 0 | } |
1564 | | |
1565 | | /** |
1566 | | * xmlFAGenerateAllTransition: |
1567 | | * @ctxt: a regexp parser context |
1568 | | * @from: the from state |
1569 | | * @to: the target state or NULL for building a new one |
1570 | | * @lax: |
1571 | | * |
1572 | | */ |
1573 | | static int |
1574 | | xmlFAGenerateAllTransition(xmlRegParserCtxtPtr ctxt, |
1575 | | xmlRegStatePtr from, xmlRegStatePtr to, |
1576 | 0 | int lax) { |
1577 | 0 | if (to == NULL) { |
1578 | 0 | to = xmlRegStatePush(ctxt); |
1579 | 0 | if (to == NULL) |
1580 | 0 | return(-1); |
1581 | 0 | ctxt->state = to; |
1582 | 0 | } |
1583 | 0 | if (lax) |
1584 | 0 | xmlRegStateAddTrans(ctxt, from, NULL, to, -1, REGEXP_ALL_LAX_COUNTER); |
1585 | 0 | else |
1586 | 0 | xmlRegStateAddTrans(ctxt, from, NULL, to, -1, REGEXP_ALL_COUNTER); |
1587 | 0 | return(0); |
1588 | 0 | } |
1589 | | |
1590 | | /** |
1591 | | * xmlFAGenerateEpsilonTransition: |
1592 | | * @ctxt: a regexp parser context |
1593 | | * @from: the from state |
1594 | | * @to: the target state or NULL for building a new one |
1595 | | * |
1596 | | */ |
1597 | | static int |
1598 | | xmlFAGenerateEpsilonTransition(xmlRegParserCtxtPtr ctxt, |
1599 | 0 | xmlRegStatePtr from, xmlRegStatePtr to) { |
1600 | 0 | if (to == NULL) { |
1601 | 0 | to = xmlRegStatePush(ctxt); |
1602 | 0 | if (to == NULL) |
1603 | 0 | return(-1); |
1604 | 0 | ctxt->state = to; |
1605 | 0 | } |
1606 | 0 | xmlRegStateAddTrans(ctxt, from, NULL, to, -1, -1); |
1607 | 0 | return(0); |
1608 | 0 | } |
1609 | | |
1610 | | /** |
1611 | | * xmlFAGenerateCountedEpsilonTransition: |
1612 | | * @ctxt: a regexp parser context |
1613 | | * @from: the from state |
1614 | | * @to: the target state or NULL for building a new one |
1615 | | * counter: the counter for that transition |
1616 | | * |
1617 | | */ |
1618 | | static int |
1619 | | xmlFAGenerateCountedEpsilonTransition(xmlRegParserCtxtPtr ctxt, |
1620 | 0 | xmlRegStatePtr from, xmlRegStatePtr to, int counter) { |
1621 | 0 | if (to == NULL) { |
1622 | 0 | to = xmlRegStatePush(ctxt); |
1623 | 0 | if (to == NULL) |
1624 | 0 | return(-1); |
1625 | 0 | ctxt->state = to; |
1626 | 0 | } |
1627 | 0 | xmlRegStateAddTrans(ctxt, from, NULL, to, counter, -1); |
1628 | 0 | return(0); |
1629 | 0 | } |
1630 | | |
1631 | | /** |
1632 | | * xmlFAGenerateCountedTransition: |
1633 | | * @ctxt: a regexp parser context |
1634 | | * @from: the from state |
1635 | | * @to: the target state or NULL for building a new one |
1636 | | * counter: the counter for that transition |
1637 | | * |
1638 | | */ |
1639 | | static int |
1640 | | xmlFAGenerateCountedTransition(xmlRegParserCtxtPtr ctxt, |
1641 | 0 | xmlRegStatePtr from, xmlRegStatePtr to, int counter) { |
1642 | 0 | if (to == NULL) { |
1643 | 0 | to = xmlRegStatePush(ctxt); |
1644 | 0 | if (to == NULL) |
1645 | 0 | return(-1); |
1646 | 0 | ctxt->state = to; |
1647 | 0 | } |
1648 | 0 | xmlRegStateAddTrans(ctxt, from, NULL, to, -1, counter); |
1649 | 0 | return(0); |
1650 | 0 | } |
1651 | | |
1652 | | /** |
1653 | | * xmlFAGenerateTransitions: |
1654 | | * @ctxt: a regexp parser context |
1655 | | * @from: the from state |
1656 | | * @to: the target state or NULL for building a new one |
1657 | | * @atom: the atom generating the transition |
1658 | | * |
1659 | | * Returns 0 if success and -1 in case of error. |
1660 | | */ |
1661 | | static int |
1662 | | xmlFAGenerateTransitions(xmlRegParserCtxtPtr ctxt, xmlRegStatePtr from, |
1663 | 0 | xmlRegStatePtr to, xmlRegAtomPtr atom) { |
1664 | 0 | xmlRegStatePtr end; |
1665 | 0 | int nullable = 0; |
1666 | |
|
1667 | 0 | if (atom == NULL) { |
1668 | 0 | ERROR("generate transition: atom == NULL"); |
1669 | 0 | return(-1); |
1670 | 0 | } |
1671 | 0 | if (atom->type == XML_REGEXP_SUBREG) { |
1672 | | /* |
1673 | | * this is a subexpression handling one should not need to |
1674 | | * create a new node except for XML_REGEXP_QUANT_RANGE. |
1675 | | */ |
1676 | 0 | if ((to != NULL) && (atom->stop != to) && |
1677 | 0 | (atom->quant != XML_REGEXP_QUANT_RANGE)) { |
1678 | | /* |
1679 | | * Generate an epsilon transition to link to the target |
1680 | | */ |
1681 | 0 | xmlFAGenerateEpsilonTransition(ctxt, atom->stop, to); |
1682 | | #ifdef DV |
1683 | | } else if ((to == NULL) && (atom->quant != XML_REGEXP_QUANT_RANGE) && |
1684 | | (atom->quant != XML_REGEXP_QUANT_ONCE)) { |
1685 | | to = xmlRegStatePush(ctxt, to); |
1686 | | if (to == NULL) |
1687 | | return(-1); |
1688 | | ctxt->state = to; |
1689 | | xmlFAGenerateEpsilonTransition(ctxt, atom->stop, to); |
1690 | | #endif |
1691 | 0 | } |
1692 | 0 | switch (atom->quant) { |
1693 | 0 | case XML_REGEXP_QUANT_OPT: |
1694 | 0 | atom->quant = XML_REGEXP_QUANT_ONCE; |
1695 | | /* |
1696 | | * transition done to the state after end of atom. |
1697 | | * 1. set transition from atom start to new state |
1698 | | * 2. set transition from atom end to this state. |
1699 | | */ |
1700 | 0 | if (to == NULL) { |
1701 | 0 | xmlFAGenerateEpsilonTransition(ctxt, atom->start, 0); |
1702 | 0 | xmlFAGenerateEpsilonTransition(ctxt, atom->stop, |
1703 | 0 | ctxt->state); |
1704 | 0 | } else { |
1705 | 0 | xmlFAGenerateEpsilonTransition(ctxt, atom->start, to); |
1706 | 0 | } |
1707 | 0 | break; |
1708 | 0 | case XML_REGEXP_QUANT_MULT: |
1709 | 0 | atom->quant = XML_REGEXP_QUANT_ONCE; |
1710 | 0 | xmlFAGenerateEpsilonTransition(ctxt, atom->start, atom->stop); |
1711 | 0 | xmlFAGenerateEpsilonTransition(ctxt, atom->stop, atom->start); |
1712 | 0 | break; |
1713 | 0 | case XML_REGEXP_QUANT_PLUS: |
1714 | 0 | atom->quant = XML_REGEXP_QUANT_ONCE; |
1715 | 0 | xmlFAGenerateEpsilonTransition(ctxt, atom->stop, atom->start); |
1716 | 0 | break; |
1717 | 0 | case XML_REGEXP_QUANT_RANGE: { |
1718 | 0 | int counter; |
1719 | 0 | xmlRegStatePtr inter, newstate; |
1720 | | |
1721 | | /* |
1722 | | * create the final state now if needed |
1723 | | */ |
1724 | 0 | if (to != NULL) { |
1725 | 0 | newstate = to; |
1726 | 0 | } else { |
1727 | 0 | newstate = xmlRegStatePush(ctxt); |
1728 | 0 | if (newstate == NULL) |
1729 | 0 | return(-1); |
1730 | 0 | } |
1731 | | |
1732 | | /* |
1733 | | * The principle here is to use counted transition |
1734 | | * to avoid explosion in the number of states in the |
1735 | | * graph. This is clearly more complex but should not |
1736 | | * be exploitable at runtime. |
1737 | | */ |
1738 | 0 | if ((atom->min == 0) && (atom->start0 == NULL)) { |
1739 | 0 | xmlRegAtomPtr copy; |
1740 | | /* |
1741 | | * duplicate a transition based on atom to count next |
1742 | | * occurrences after 1. We cannot loop to atom->start |
1743 | | * directly because we need an epsilon transition to |
1744 | | * newstate. |
1745 | | */ |
1746 | | /* ???? For some reason it seems we never reach that |
1747 | | case, I suppose this got optimized out before when |
1748 | | building the automata */ |
1749 | 0 | copy = xmlRegCopyAtom(ctxt, atom); |
1750 | 0 | if (copy == NULL) |
1751 | 0 | return(-1); |
1752 | 0 | copy->quant = XML_REGEXP_QUANT_ONCE; |
1753 | 0 | copy->min = 0; |
1754 | 0 | copy->max = 0; |
1755 | |
|
1756 | 0 | if (xmlFAGenerateTransitions(ctxt, atom->start, NULL, copy) |
1757 | 0 | < 0) { |
1758 | 0 | xmlRegFreeAtom(copy); |
1759 | 0 | return(-1); |
1760 | 0 | } |
1761 | 0 | inter = ctxt->state; |
1762 | 0 | counter = xmlRegGetCounter(ctxt); |
1763 | 0 | if (counter < 0) |
1764 | 0 | return(-1); |
1765 | 0 | ctxt->counters[counter].min = atom->min - 1; |
1766 | 0 | ctxt->counters[counter].max = atom->max - 1; |
1767 | | /* count the number of times we see it again */ |
1768 | 0 | xmlFAGenerateCountedEpsilonTransition(ctxt, inter, |
1769 | 0 | atom->stop, counter); |
1770 | | /* allow a way out based on the count */ |
1771 | 0 | xmlFAGenerateCountedTransition(ctxt, inter, |
1772 | 0 | newstate, counter); |
1773 | | /* and also allow a direct exit for 0 */ |
1774 | 0 | xmlFAGenerateEpsilonTransition(ctxt, atom->start, |
1775 | 0 | newstate); |
1776 | 0 | } else { |
1777 | | /* |
1778 | | * either we need the atom at least once or there |
1779 | | * is an atom->start0 allowing to easily plug the |
1780 | | * epsilon transition. |
1781 | | */ |
1782 | 0 | counter = xmlRegGetCounter(ctxt); |
1783 | 0 | if (counter < 0) |
1784 | 0 | return(-1); |
1785 | 0 | ctxt->counters[counter].min = atom->min - 1; |
1786 | 0 | ctxt->counters[counter].max = atom->max - 1; |
1787 | | /* allow a way out based on the count */ |
1788 | 0 | xmlFAGenerateCountedTransition(ctxt, atom->stop, |
1789 | 0 | newstate, counter); |
1790 | | /* count the number of times we see it again */ |
1791 | 0 | xmlFAGenerateCountedEpsilonTransition(ctxt, atom->stop, |
1792 | 0 | atom->start, counter); |
1793 | | /* and if needed allow a direct exit for 0 */ |
1794 | 0 | if (atom->min == 0) |
1795 | 0 | xmlFAGenerateEpsilonTransition(ctxt, atom->start0, |
1796 | 0 | newstate); |
1797 | |
|
1798 | 0 | } |
1799 | 0 | atom->min = 0; |
1800 | 0 | atom->max = 0; |
1801 | 0 | atom->quant = XML_REGEXP_QUANT_ONCE; |
1802 | 0 | ctxt->state = newstate; |
1803 | 0 | } |
1804 | 0 | default: |
1805 | 0 | break; |
1806 | 0 | } |
1807 | 0 | atom->start = NULL; |
1808 | 0 | atom->start0 = NULL; |
1809 | 0 | atom->stop = NULL; |
1810 | 0 | if (xmlRegAtomPush(ctxt, atom) < 0) |
1811 | 0 | return(-1); |
1812 | 0 | return(0); |
1813 | 0 | } |
1814 | 0 | if ((atom->min == 0) && (atom->max == 0) && |
1815 | 0 | (atom->quant == XML_REGEXP_QUANT_RANGE)) { |
1816 | | /* |
1817 | | * we can discard the atom and generate an epsilon transition instead |
1818 | | */ |
1819 | 0 | if (to == NULL) { |
1820 | 0 | to = xmlRegStatePush(ctxt); |
1821 | 0 | if (to == NULL) |
1822 | 0 | return(-1); |
1823 | 0 | } |
1824 | 0 | xmlFAGenerateEpsilonTransition(ctxt, from, to); |
1825 | 0 | ctxt->state = to; |
1826 | 0 | xmlRegFreeAtom(atom); |
1827 | 0 | return(0); |
1828 | 0 | } |
1829 | 0 | if (to == NULL) { |
1830 | 0 | to = xmlRegStatePush(ctxt); |
1831 | 0 | if (to == NULL) |
1832 | 0 | return(-1); |
1833 | 0 | } |
1834 | 0 | end = to; |
1835 | 0 | if ((atom->quant == XML_REGEXP_QUANT_MULT) || |
1836 | 0 | (atom->quant == XML_REGEXP_QUANT_PLUS)) { |
1837 | | /* |
1838 | | * Do not pollute the target state by adding transitions from |
1839 | | * it as it is likely to be the shared target of multiple branches. |
1840 | | * So isolate with an epsilon transition. |
1841 | | */ |
1842 | 0 | xmlRegStatePtr tmp; |
1843 | |
|
1844 | 0 | tmp = xmlRegStatePush(ctxt); |
1845 | 0 | if (tmp == NULL) |
1846 | 0 | return(-1); |
1847 | 0 | xmlFAGenerateEpsilonTransition(ctxt, tmp, to); |
1848 | 0 | to = tmp; |
1849 | 0 | } |
1850 | 0 | if ((atom->quant == XML_REGEXP_QUANT_RANGE) && |
1851 | 0 | (atom->min == 0) && (atom->max > 0)) { |
1852 | 0 | nullable = 1; |
1853 | 0 | atom->min = 1; |
1854 | 0 | if (atom->max == 1) |
1855 | 0 | atom->quant = XML_REGEXP_QUANT_OPT; |
1856 | 0 | } |
1857 | 0 | xmlRegStateAddTrans(ctxt, from, atom, to, -1, -1); |
1858 | 0 | ctxt->state = end; |
1859 | 0 | switch (atom->quant) { |
1860 | 0 | case XML_REGEXP_QUANT_OPT: |
1861 | 0 | atom->quant = XML_REGEXP_QUANT_ONCE; |
1862 | 0 | xmlFAGenerateEpsilonTransition(ctxt, from, to); |
1863 | 0 | break; |
1864 | 0 | case XML_REGEXP_QUANT_MULT: |
1865 | 0 | atom->quant = XML_REGEXP_QUANT_ONCE; |
1866 | 0 | xmlFAGenerateEpsilonTransition(ctxt, from, to); |
1867 | 0 | xmlRegStateAddTrans(ctxt, to, atom, to, -1, -1); |
1868 | 0 | break; |
1869 | 0 | case XML_REGEXP_QUANT_PLUS: |
1870 | 0 | atom->quant = XML_REGEXP_QUANT_ONCE; |
1871 | 0 | xmlRegStateAddTrans(ctxt, to, atom, to, -1, -1); |
1872 | 0 | break; |
1873 | 0 | case XML_REGEXP_QUANT_RANGE: |
1874 | 0 | if (nullable) |
1875 | 0 | xmlFAGenerateEpsilonTransition(ctxt, from, to); |
1876 | 0 | break; |
1877 | 0 | default: |
1878 | 0 | break; |
1879 | 0 | } |
1880 | 0 | if (xmlRegAtomPush(ctxt, atom) < 0) |
1881 | 0 | return(-1); |
1882 | 0 | return(0); |
1883 | 0 | } |
1884 | | |
1885 | | /** |
1886 | | * xmlFAReduceEpsilonTransitions: |
1887 | | * @ctxt: a regexp parser context |
1888 | | * @fromnr: the from state |
1889 | | * @tonr: the to state |
1890 | | * @counter: should that transition be associated to a counted |
1891 | | * |
1892 | | */ |
1893 | | static void |
1894 | | xmlFAReduceEpsilonTransitions(xmlRegParserCtxtPtr ctxt, int fromnr, |
1895 | 0 | int tonr, int counter) { |
1896 | 0 | int transnr; |
1897 | 0 | xmlRegStatePtr from; |
1898 | 0 | xmlRegStatePtr to; |
1899 | |
|
1900 | 0 | from = ctxt->states[fromnr]; |
1901 | 0 | if (from == NULL) |
1902 | 0 | return; |
1903 | 0 | to = ctxt->states[tonr]; |
1904 | 0 | if (to == NULL) |
1905 | 0 | return; |
1906 | 0 | if ((to->mark == XML_REGEXP_MARK_START) || |
1907 | 0 | (to->mark == XML_REGEXP_MARK_VISITED)) |
1908 | 0 | return; |
1909 | | |
1910 | 0 | to->mark = XML_REGEXP_MARK_VISITED; |
1911 | 0 | if (to->type == XML_REGEXP_FINAL_STATE) { |
1912 | 0 | from->type = XML_REGEXP_FINAL_STATE; |
1913 | 0 | } |
1914 | 0 | for (transnr = 0;transnr < to->nbTrans;transnr++) { |
1915 | 0 | xmlRegTransPtr t1 = &to->trans[transnr]; |
1916 | 0 | int tcounter; |
1917 | |
|
1918 | 0 | if (t1->to < 0) |
1919 | 0 | continue; |
1920 | 0 | if (t1->counter >= 0) { |
1921 | | /* assert(counter < 0); */ |
1922 | 0 | tcounter = t1->counter; |
1923 | 0 | } else { |
1924 | 0 | tcounter = counter; |
1925 | 0 | } |
1926 | 0 | if (t1->atom == NULL) { |
1927 | | /* |
1928 | | * Don't remove counted transitions |
1929 | | * Don't loop either |
1930 | | */ |
1931 | 0 | if (t1->to != fromnr) { |
1932 | 0 | if (t1->count >= 0) { |
1933 | 0 | xmlRegStateAddTrans(ctxt, from, NULL, ctxt->states[t1->to], |
1934 | 0 | -1, t1->count); |
1935 | 0 | } else { |
1936 | 0 | xmlFAReduceEpsilonTransitions(ctxt, fromnr, t1->to, |
1937 | 0 | tcounter); |
1938 | 0 | } |
1939 | 0 | } |
1940 | 0 | } else { |
1941 | 0 | xmlRegStateAddTrans(ctxt, from, t1->atom, |
1942 | 0 | ctxt->states[t1->to], tcounter, -1); |
1943 | 0 | } |
1944 | 0 | } |
1945 | 0 | } |
1946 | | |
1947 | | /** |
1948 | | * xmlFAFinishReduceEpsilonTransitions: |
1949 | | * @ctxt: a regexp parser context |
1950 | | * @fromnr: the from state |
1951 | | * @tonr: the to state |
1952 | | * @counter: should that transition be associated to a counted |
1953 | | * |
1954 | | */ |
1955 | | static void |
1956 | 0 | xmlFAFinishReduceEpsilonTransitions(xmlRegParserCtxtPtr ctxt, int tonr) { |
1957 | 0 | int transnr; |
1958 | 0 | xmlRegStatePtr to; |
1959 | |
|
1960 | 0 | to = ctxt->states[tonr]; |
1961 | 0 | if (to == NULL) |
1962 | 0 | return; |
1963 | 0 | if ((to->mark == XML_REGEXP_MARK_START) || |
1964 | 0 | (to->mark == XML_REGEXP_MARK_NORMAL)) |
1965 | 0 | return; |
1966 | | |
1967 | 0 | to->mark = XML_REGEXP_MARK_NORMAL; |
1968 | 0 | for (transnr = 0;transnr < to->nbTrans;transnr++) { |
1969 | 0 | xmlRegTransPtr t1 = &to->trans[transnr]; |
1970 | 0 | if ((t1->to >= 0) && (t1->atom == NULL)) |
1971 | 0 | xmlFAFinishReduceEpsilonTransitions(ctxt, t1->to); |
1972 | 0 | } |
1973 | 0 | } |
1974 | | |
1975 | | /** |
1976 | | * xmlFAEliminateSimpleEpsilonTransitions: |
1977 | | * @ctxt: a regexp parser context |
1978 | | * |
1979 | | * Eliminating general epsilon transitions can get costly in the general |
1980 | | * algorithm due to the large amount of generated new transitions and |
1981 | | * associated comparisons. However for simple epsilon transition used just |
1982 | | * to separate building blocks when generating the automata this can be |
1983 | | * reduced to state elimination: |
1984 | | * - if there exists an epsilon from X to Y |
1985 | | * - if there is no other transition from X |
1986 | | * then X and Y are semantically equivalent and X can be eliminated |
1987 | | * If X is the start state then make Y the start state, else replace the |
1988 | | * target of all transitions to X by transitions to Y. |
1989 | | * |
1990 | | * If X is a final state, skip it. |
1991 | | * Otherwise it would be necessary to manipulate counters for this case when |
1992 | | * eliminating state 2: |
1993 | | * State 1 has a transition with an atom to state 2. |
1994 | | * State 2 is final and has an epsilon transition to state 1. |
1995 | | */ |
1996 | | static void |
1997 | 0 | xmlFAEliminateSimpleEpsilonTransitions(xmlRegParserCtxtPtr ctxt) { |
1998 | 0 | int statenr, i, j, newto; |
1999 | 0 | xmlRegStatePtr state, tmp; |
2000 | |
|
2001 | 0 | for (statenr = 0;statenr < ctxt->nbStates;statenr++) { |
2002 | 0 | state = ctxt->states[statenr]; |
2003 | 0 | if (state == NULL) |
2004 | 0 | continue; |
2005 | 0 | if (state->nbTrans != 1) |
2006 | 0 | continue; |
2007 | 0 | if (state->type == XML_REGEXP_UNREACH_STATE || |
2008 | 0 | state->type == XML_REGEXP_FINAL_STATE) |
2009 | 0 | continue; |
2010 | | /* is the only transition out a basic transition */ |
2011 | 0 | if ((state->trans[0].atom == NULL) && |
2012 | 0 | (state->trans[0].to >= 0) && |
2013 | 0 | (state->trans[0].to != statenr) && |
2014 | 0 | (state->trans[0].counter < 0) && |
2015 | 0 | (state->trans[0].count < 0)) { |
2016 | 0 | newto = state->trans[0].to; |
2017 | |
|
2018 | 0 | if (state->type == XML_REGEXP_START_STATE) { |
2019 | 0 | } else { |
2020 | 0 | for (i = 0;i < state->nbTransTo;i++) { |
2021 | 0 | tmp = ctxt->states[state->transTo[i]]; |
2022 | 0 | for (j = 0;j < tmp->nbTrans;j++) { |
2023 | 0 | if (tmp->trans[j].to == statenr) { |
2024 | 0 | tmp->trans[j].to = -1; |
2025 | 0 | xmlRegStateAddTrans(ctxt, tmp, tmp->trans[j].atom, |
2026 | 0 | ctxt->states[newto], |
2027 | 0 | tmp->trans[j].counter, |
2028 | 0 | tmp->trans[j].count); |
2029 | 0 | } |
2030 | 0 | } |
2031 | 0 | } |
2032 | 0 | if (state->type == XML_REGEXP_FINAL_STATE) |
2033 | 0 | ctxt->states[newto]->type = XML_REGEXP_FINAL_STATE; |
2034 | | /* eliminate the transition completely */ |
2035 | 0 | state->nbTrans = 0; |
2036 | |
|
2037 | 0 | state->type = XML_REGEXP_UNREACH_STATE; |
2038 | |
|
2039 | 0 | } |
2040 | |
|
2041 | 0 | } |
2042 | 0 | } |
2043 | 0 | } |
2044 | | /** |
2045 | | * xmlFAEliminateEpsilonTransitions: |
2046 | | * @ctxt: a regexp parser context |
2047 | | * |
2048 | | */ |
2049 | | static void |
2050 | 0 | xmlFAEliminateEpsilonTransitions(xmlRegParserCtxtPtr ctxt) { |
2051 | 0 | int statenr, transnr; |
2052 | 0 | xmlRegStatePtr state; |
2053 | 0 | int has_epsilon; |
2054 | |
|
2055 | 0 | if (ctxt->states == NULL) return; |
2056 | | |
2057 | | /* |
2058 | | * Eliminate simple epsilon transition and the associated unreachable |
2059 | | * states. |
2060 | | */ |
2061 | 0 | xmlFAEliminateSimpleEpsilonTransitions(ctxt); |
2062 | 0 | for (statenr = 0;statenr < ctxt->nbStates;statenr++) { |
2063 | 0 | state = ctxt->states[statenr]; |
2064 | 0 | if ((state != NULL) && (state->type == XML_REGEXP_UNREACH_STATE)) { |
2065 | 0 | xmlRegFreeState(state); |
2066 | 0 | ctxt->states[statenr] = NULL; |
2067 | 0 | } |
2068 | 0 | } |
2069 | |
|
2070 | 0 | has_epsilon = 0; |
2071 | | |
2072 | | /* |
2073 | | * Build the completed transitions bypassing the epsilons |
2074 | | * Use a marking algorithm to avoid loops |
2075 | | * Mark sink states too. |
2076 | | * Process from the latest states backward to the start when |
2077 | | * there is long cascading epsilon chains this minimize the |
2078 | | * recursions and transition compares when adding the new ones |
2079 | | */ |
2080 | 0 | for (statenr = ctxt->nbStates - 1;statenr >= 0;statenr--) { |
2081 | 0 | state = ctxt->states[statenr]; |
2082 | 0 | if (state == NULL) |
2083 | 0 | continue; |
2084 | 0 | if ((state->nbTrans == 0) && |
2085 | 0 | (state->type != XML_REGEXP_FINAL_STATE)) { |
2086 | 0 | state->type = XML_REGEXP_SINK_STATE; |
2087 | 0 | } |
2088 | 0 | for (transnr = 0;transnr < state->nbTrans;transnr++) { |
2089 | 0 | if ((state->trans[transnr].atom == NULL) && |
2090 | 0 | (state->trans[transnr].to >= 0)) { |
2091 | 0 | if (state->trans[transnr].to == statenr) { |
2092 | 0 | state->trans[transnr].to = -1; |
2093 | 0 | } else if (state->trans[transnr].count < 0) { |
2094 | 0 | int newto = state->trans[transnr].to; |
2095 | |
|
2096 | 0 | has_epsilon = 1; |
2097 | 0 | state->trans[transnr].to = -2; |
2098 | 0 | state->mark = XML_REGEXP_MARK_START; |
2099 | 0 | xmlFAReduceEpsilonTransitions(ctxt, statenr, |
2100 | 0 | newto, state->trans[transnr].counter); |
2101 | 0 | xmlFAFinishReduceEpsilonTransitions(ctxt, newto); |
2102 | 0 | state->mark = XML_REGEXP_MARK_NORMAL; |
2103 | 0 | } |
2104 | 0 | } |
2105 | 0 | } |
2106 | 0 | } |
2107 | | /* |
2108 | | * Eliminate the epsilon transitions |
2109 | | */ |
2110 | 0 | if (has_epsilon) { |
2111 | 0 | for (statenr = 0;statenr < ctxt->nbStates;statenr++) { |
2112 | 0 | state = ctxt->states[statenr]; |
2113 | 0 | if (state == NULL) |
2114 | 0 | continue; |
2115 | 0 | for (transnr = 0;transnr < state->nbTrans;transnr++) { |
2116 | 0 | xmlRegTransPtr trans = &(state->trans[transnr]); |
2117 | 0 | if ((trans->atom == NULL) && |
2118 | 0 | (trans->count < 0) && |
2119 | 0 | (trans->to >= 0)) { |
2120 | 0 | trans->to = -1; |
2121 | 0 | } |
2122 | 0 | } |
2123 | 0 | } |
2124 | 0 | } |
2125 | | |
2126 | | /* |
2127 | | * Use this pass to detect unreachable states too |
2128 | | */ |
2129 | 0 | for (statenr = 0;statenr < ctxt->nbStates;statenr++) { |
2130 | 0 | state = ctxt->states[statenr]; |
2131 | 0 | if (state != NULL) |
2132 | 0 | state->reached = XML_REGEXP_MARK_NORMAL; |
2133 | 0 | } |
2134 | 0 | state = ctxt->states[0]; |
2135 | 0 | if (state != NULL) |
2136 | 0 | state->reached = XML_REGEXP_MARK_START; |
2137 | 0 | while (state != NULL) { |
2138 | 0 | xmlRegStatePtr target = NULL; |
2139 | 0 | state->reached = XML_REGEXP_MARK_VISITED; |
2140 | | /* |
2141 | | * Mark all states reachable from the current reachable state |
2142 | | */ |
2143 | 0 | for (transnr = 0;transnr < state->nbTrans;transnr++) { |
2144 | 0 | if ((state->trans[transnr].to >= 0) && |
2145 | 0 | ((state->trans[transnr].atom != NULL) || |
2146 | 0 | (state->trans[transnr].count >= 0))) { |
2147 | 0 | int newto = state->trans[transnr].to; |
2148 | |
|
2149 | 0 | if (ctxt->states[newto] == NULL) |
2150 | 0 | continue; |
2151 | 0 | if (ctxt->states[newto]->reached == XML_REGEXP_MARK_NORMAL) { |
2152 | 0 | ctxt->states[newto]->reached = XML_REGEXP_MARK_START; |
2153 | 0 | target = ctxt->states[newto]; |
2154 | 0 | } |
2155 | 0 | } |
2156 | 0 | } |
2157 | | |
2158 | | /* |
2159 | | * find the next accessible state not explored |
2160 | | */ |
2161 | 0 | if (target == NULL) { |
2162 | 0 | for (statenr = 1;statenr < ctxt->nbStates;statenr++) { |
2163 | 0 | state = ctxt->states[statenr]; |
2164 | 0 | if ((state != NULL) && (state->reached == |
2165 | 0 | XML_REGEXP_MARK_START)) { |
2166 | 0 | target = state; |
2167 | 0 | break; |
2168 | 0 | } |
2169 | 0 | } |
2170 | 0 | } |
2171 | 0 | state = target; |
2172 | 0 | } |
2173 | 0 | for (statenr = 0;statenr < ctxt->nbStates;statenr++) { |
2174 | 0 | state = ctxt->states[statenr]; |
2175 | 0 | if ((state != NULL) && (state->reached == XML_REGEXP_MARK_NORMAL)) { |
2176 | 0 | xmlRegFreeState(state); |
2177 | 0 | ctxt->states[statenr] = NULL; |
2178 | 0 | } |
2179 | 0 | } |
2180 | |
|
2181 | 0 | } |
2182 | | |
2183 | | static int |
2184 | 0 | xmlFACompareRanges(xmlRegRangePtr range1, xmlRegRangePtr range2) { |
2185 | 0 | int ret = 0; |
2186 | |
|
2187 | 0 | if ((range1->type == XML_REGEXP_RANGES) || |
2188 | 0 | (range2->type == XML_REGEXP_RANGES) || |
2189 | 0 | (range2->type == XML_REGEXP_SUBREG) || |
2190 | 0 | (range1->type == XML_REGEXP_SUBREG) || |
2191 | 0 | (range1->type == XML_REGEXP_STRING) || |
2192 | 0 | (range2->type == XML_REGEXP_STRING)) |
2193 | 0 | return(-1); |
2194 | | |
2195 | | /* put them in order */ |
2196 | 0 | if (range1->type > range2->type) { |
2197 | 0 | xmlRegRangePtr tmp; |
2198 | |
|
2199 | 0 | tmp = range1; |
2200 | 0 | range1 = range2; |
2201 | 0 | range2 = tmp; |
2202 | 0 | } |
2203 | 0 | if ((range1->type == XML_REGEXP_ANYCHAR) || |
2204 | 0 | (range2->type == XML_REGEXP_ANYCHAR)) { |
2205 | 0 | ret = 1; |
2206 | 0 | } else if ((range1->type == XML_REGEXP_EPSILON) || |
2207 | 0 | (range2->type == XML_REGEXP_EPSILON)) { |
2208 | 0 | return(0); |
2209 | 0 | } else if (range1->type == range2->type) { |
2210 | 0 | if (range1->type != XML_REGEXP_CHARVAL) |
2211 | 0 | ret = 1; |
2212 | 0 | else if ((range1->end < range2->start) || |
2213 | 0 | (range2->end < range1->start)) |
2214 | 0 | ret = 0; |
2215 | 0 | else |
2216 | 0 | ret = 1; |
2217 | 0 | } else if (range1->type == XML_REGEXP_CHARVAL) { |
2218 | 0 | int codepoint; |
2219 | 0 | int neg = 0; |
2220 | | |
2221 | | /* |
2222 | | * just check all codepoints in the range for acceptance, |
2223 | | * this is usually way cheaper since done only once at |
2224 | | * compilation than testing over and over at runtime or |
2225 | | * pushing too many states when evaluating. |
2226 | | */ |
2227 | 0 | if (((range1->neg == 0) && (range2->neg != 0)) || |
2228 | 0 | ((range1->neg != 0) && (range2->neg == 0))) |
2229 | 0 | neg = 1; |
2230 | |
|
2231 | 0 | for (codepoint = range1->start;codepoint <= range1->end ;codepoint++) { |
2232 | 0 | ret = xmlRegCheckCharacterRange(range2->type, codepoint, |
2233 | 0 | 0, range2->start, range2->end, |
2234 | 0 | range2->blockName); |
2235 | 0 | if (ret < 0) |
2236 | 0 | return(-1); |
2237 | 0 | if (((neg == 1) && (ret == 0)) || |
2238 | 0 | ((neg == 0) && (ret == 1))) |
2239 | 0 | return(1); |
2240 | 0 | } |
2241 | 0 | return(0); |
2242 | 0 | } else if ((range1->type == XML_REGEXP_BLOCK_NAME) || |
2243 | 0 | (range2->type == XML_REGEXP_BLOCK_NAME)) { |
2244 | 0 | if (range1->type == range2->type) { |
2245 | 0 | ret = xmlStrEqual(range1->blockName, range2->blockName); |
2246 | 0 | } else { |
2247 | | /* |
2248 | | * comparing a block range with anything else is way |
2249 | | * too costly, and maintaining the table is like too much |
2250 | | * memory too, so let's force the automata to save state |
2251 | | * here. |
2252 | | */ |
2253 | 0 | return(1); |
2254 | 0 | } |
2255 | 0 | } else if ((range1->type < XML_REGEXP_LETTER) || |
2256 | 0 | (range2->type < XML_REGEXP_LETTER)) { |
2257 | 0 | if ((range1->type == XML_REGEXP_ANYSPACE) && |
2258 | 0 | (range2->type == XML_REGEXP_NOTSPACE)) |
2259 | 0 | ret = 0; |
2260 | 0 | else if ((range1->type == XML_REGEXP_INITNAME) && |
2261 | 0 | (range2->type == XML_REGEXP_NOTINITNAME)) |
2262 | 0 | ret = 0; |
2263 | 0 | else if ((range1->type == XML_REGEXP_NAMECHAR) && |
2264 | 0 | (range2->type == XML_REGEXP_NOTNAMECHAR)) |
2265 | 0 | ret = 0; |
2266 | 0 | else if ((range1->type == XML_REGEXP_DECIMAL) && |
2267 | 0 | (range2->type == XML_REGEXP_NOTDECIMAL)) |
2268 | 0 | ret = 0; |
2269 | 0 | else if ((range1->type == XML_REGEXP_REALCHAR) && |
2270 | 0 | (range2->type == XML_REGEXP_NOTREALCHAR)) |
2271 | 0 | ret = 0; |
2272 | 0 | else { |
2273 | | /* same thing to limit complexity */ |
2274 | 0 | return(1); |
2275 | 0 | } |
2276 | 0 | } else { |
2277 | 0 | ret = 0; |
2278 | | /* range1->type < range2->type here */ |
2279 | 0 | switch (range1->type) { |
2280 | 0 | case XML_REGEXP_LETTER: |
2281 | | /* all disjoint except in the subgroups */ |
2282 | 0 | if ((range2->type == XML_REGEXP_LETTER_UPPERCASE) || |
2283 | 0 | (range2->type == XML_REGEXP_LETTER_LOWERCASE) || |
2284 | 0 | (range2->type == XML_REGEXP_LETTER_TITLECASE) || |
2285 | 0 | (range2->type == XML_REGEXP_LETTER_MODIFIER) || |
2286 | 0 | (range2->type == XML_REGEXP_LETTER_OTHERS)) |
2287 | 0 | ret = 1; |
2288 | 0 | break; |
2289 | 0 | case XML_REGEXP_MARK: |
2290 | 0 | if ((range2->type == XML_REGEXP_MARK_NONSPACING) || |
2291 | 0 | (range2->type == XML_REGEXP_MARK_SPACECOMBINING) || |
2292 | 0 | (range2->type == XML_REGEXP_MARK_ENCLOSING)) |
2293 | 0 | ret = 1; |
2294 | 0 | break; |
2295 | 0 | case XML_REGEXP_NUMBER: |
2296 | 0 | if ((range2->type == XML_REGEXP_NUMBER_DECIMAL) || |
2297 | 0 | (range2->type == XML_REGEXP_NUMBER_LETTER) || |
2298 | 0 | (range2->type == XML_REGEXP_NUMBER_OTHERS)) |
2299 | 0 | ret = 1; |
2300 | 0 | break; |
2301 | 0 | case XML_REGEXP_PUNCT: |
2302 | 0 | if ((range2->type == XML_REGEXP_PUNCT_CONNECTOR) || |
2303 | 0 | (range2->type == XML_REGEXP_PUNCT_DASH) || |
2304 | 0 | (range2->type == XML_REGEXP_PUNCT_OPEN) || |
2305 | 0 | (range2->type == XML_REGEXP_PUNCT_CLOSE) || |
2306 | 0 | (range2->type == XML_REGEXP_PUNCT_INITQUOTE) || |
2307 | 0 | (range2->type == XML_REGEXP_PUNCT_FINQUOTE) || |
2308 | 0 | (range2->type == XML_REGEXP_PUNCT_OTHERS)) |
2309 | 0 | ret = 1; |
2310 | 0 | break; |
2311 | 0 | case XML_REGEXP_SEPAR: |
2312 | 0 | if ((range2->type == XML_REGEXP_SEPAR_SPACE) || |
2313 | 0 | (range2->type == XML_REGEXP_SEPAR_LINE) || |
2314 | 0 | (range2->type == XML_REGEXP_SEPAR_PARA)) |
2315 | 0 | ret = 1; |
2316 | 0 | break; |
2317 | 0 | case XML_REGEXP_SYMBOL: |
2318 | 0 | if ((range2->type == XML_REGEXP_SYMBOL_MATH) || |
2319 | 0 | (range2->type == XML_REGEXP_SYMBOL_CURRENCY) || |
2320 | 0 | (range2->type == XML_REGEXP_SYMBOL_MODIFIER) || |
2321 | 0 | (range2->type == XML_REGEXP_SYMBOL_OTHERS)) |
2322 | 0 | ret = 1; |
2323 | 0 | break; |
2324 | 0 | case XML_REGEXP_OTHER: |
2325 | 0 | if ((range2->type == XML_REGEXP_OTHER_CONTROL) || |
2326 | 0 | (range2->type == XML_REGEXP_OTHER_FORMAT) || |
2327 | 0 | (range2->type == XML_REGEXP_OTHER_PRIVATE)) |
2328 | 0 | ret = 1; |
2329 | 0 | break; |
2330 | 0 | default: |
2331 | 0 | if ((range2->type >= XML_REGEXP_LETTER) && |
2332 | 0 | (range2->type < XML_REGEXP_BLOCK_NAME)) |
2333 | 0 | ret = 0; |
2334 | 0 | else { |
2335 | | /* safety net ! */ |
2336 | 0 | return(1); |
2337 | 0 | } |
2338 | 0 | } |
2339 | 0 | } |
2340 | 0 | if (((range1->neg == 0) && (range2->neg != 0)) || |
2341 | 0 | ((range1->neg != 0) && (range2->neg == 0))) |
2342 | 0 | ret = !ret; |
2343 | 0 | return(ret); |
2344 | 0 | } |
2345 | | |
2346 | | /** |
2347 | | * xmlFACompareAtomTypes: |
2348 | | * @type1: an atom type |
2349 | | * @type2: an atom type |
2350 | | * |
2351 | | * Compares two atoms type to check whether they intersect in some ways, |
2352 | | * this is used by xmlFACompareAtoms only |
2353 | | * |
2354 | | * Returns 1 if they may intersect and 0 otherwise |
2355 | | */ |
2356 | | static int |
2357 | 0 | xmlFACompareAtomTypes(xmlRegAtomType type1, xmlRegAtomType type2) { |
2358 | 0 | if ((type1 == XML_REGEXP_EPSILON) || |
2359 | 0 | (type1 == XML_REGEXP_CHARVAL) || |
2360 | 0 | (type1 == XML_REGEXP_RANGES) || |
2361 | 0 | (type1 == XML_REGEXP_SUBREG) || |
2362 | 0 | (type1 == XML_REGEXP_STRING) || |
2363 | 0 | (type1 == XML_REGEXP_ANYCHAR)) |
2364 | 0 | return(1); |
2365 | 0 | if ((type2 == XML_REGEXP_EPSILON) || |
2366 | 0 | (type2 == XML_REGEXP_CHARVAL) || |
2367 | 0 | (type2 == XML_REGEXP_RANGES) || |
2368 | 0 | (type2 == XML_REGEXP_SUBREG) || |
2369 | 0 | (type2 == XML_REGEXP_STRING) || |
2370 | 0 | (type2 == XML_REGEXP_ANYCHAR)) |
2371 | 0 | return(1); |
2372 | | |
2373 | 0 | if (type1 == type2) return(1); |
2374 | | |
2375 | | /* simplify subsequent compares by making sure type1 < type2 */ |
2376 | 0 | if (type1 > type2) { |
2377 | 0 | xmlRegAtomType tmp = type1; |
2378 | 0 | type1 = type2; |
2379 | 0 | type2 = tmp; |
2380 | 0 | } |
2381 | 0 | switch (type1) { |
2382 | 0 | case XML_REGEXP_ANYSPACE: /* \s */ |
2383 | | /* can't be a letter, number, mark, punctuation, symbol */ |
2384 | 0 | if ((type2 == XML_REGEXP_NOTSPACE) || |
2385 | 0 | ((type2 >= XML_REGEXP_LETTER) && |
2386 | 0 | (type2 <= XML_REGEXP_LETTER_OTHERS)) || |
2387 | 0 | ((type2 >= XML_REGEXP_NUMBER) && |
2388 | 0 | (type2 <= XML_REGEXP_NUMBER_OTHERS)) || |
2389 | 0 | ((type2 >= XML_REGEXP_MARK) && |
2390 | 0 | (type2 <= XML_REGEXP_MARK_ENCLOSING)) || |
2391 | 0 | ((type2 >= XML_REGEXP_PUNCT) && |
2392 | 0 | (type2 <= XML_REGEXP_PUNCT_OTHERS)) || |
2393 | 0 | ((type2 >= XML_REGEXP_SYMBOL) && |
2394 | 0 | (type2 <= XML_REGEXP_SYMBOL_OTHERS)) |
2395 | 0 | ) return(0); |
2396 | 0 | break; |
2397 | 0 | case XML_REGEXP_NOTSPACE: /* \S */ |
2398 | 0 | break; |
2399 | 0 | case XML_REGEXP_INITNAME: /* \l */ |
2400 | | /* can't be a number, mark, separator, punctuation, symbol or other */ |
2401 | 0 | if ((type2 == XML_REGEXP_NOTINITNAME) || |
2402 | 0 | ((type2 >= XML_REGEXP_NUMBER) && |
2403 | 0 | (type2 <= XML_REGEXP_NUMBER_OTHERS)) || |
2404 | 0 | ((type2 >= XML_REGEXP_MARK) && |
2405 | 0 | (type2 <= XML_REGEXP_MARK_ENCLOSING)) || |
2406 | 0 | ((type2 >= XML_REGEXP_SEPAR) && |
2407 | 0 | (type2 <= XML_REGEXP_SEPAR_PARA)) || |
2408 | 0 | ((type2 >= XML_REGEXP_PUNCT) && |
2409 | 0 | (type2 <= XML_REGEXP_PUNCT_OTHERS)) || |
2410 | 0 | ((type2 >= XML_REGEXP_SYMBOL) && |
2411 | 0 | (type2 <= XML_REGEXP_SYMBOL_OTHERS)) || |
2412 | 0 | ((type2 >= XML_REGEXP_OTHER) && |
2413 | 0 | (type2 <= XML_REGEXP_OTHER_NA)) |
2414 | 0 | ) return(0); |
2415 | 0 | break; |
2416 | 0 | case XML_REGEXP_NOTINITNAME: /* \L */ |
2417 | 0 | break; |
2418 | 0 | case XML_REGEXP_NAMECHAR: /* \c */ |
2419 | | /* can't be a mark, separator, punctuation, symbol or other */ |
2420 | 0 | if ((type2 == XML_REGEXP_NOTNAMECHAR) || |
2421 | 0 | ((type2 >= XML_REGEXP_MARK) && |
2422 | 0 | (type2 <= XML_REGEXP_MARK_ENCLOSING)) || |
2423 | 0 | ((type2 >= XML_REGEXP_PUNCT) && |
2424 | 0 | (type2 <= XML_REGEXP_PUNCT_OTHERS)) || |
2425 | 0 | ((type2 >= XML_REGEXP_SEPAR) && |
2426 | 0 | (type2 <= XML_REGEXP_SEPAR_PARA)) || |
2427 | 0 | ((type2 >= XML_REGEXP_SYMBOL) && |
2428 | 0 | (type2 <= XML_REGEXP_SYMBOL_OTHERS)) || |
2429 | 0 | ((type2 >= XML_REGEXP_OTHER) && |
2430 | 0 | (type2 <= XML_REGEXP_OTHER_NA)) |
2431 | 0 | ) return(0); |
2432 | 0 | break; |
2433 | 0 | case XML_REGEXP_NOTNAMECHAR: /* \C */ |
2434 | 0 | break; |
2435 | 0 | case XML_REGEXP_DECIMAL: /* \d */ |
2436 | | /* can't be a letter, mark, separator, punctuation, symbol or other */ |
2437 | 0 | if ((type2 == XML_REGEXP_NOTDECIMAL) || |
2438 | 0 | (type2 == XML_REGEXP_REALCHAR) || |
2439 | 0 | ((type2 >= XML_REGEXP_LETTER) && |
2440 | 0 | (type2 <= XML_REGEXP_LETTER_OTHERS)) || |
2441 | 0 | ((type2 >= XML_REGEXP_MARK) && |
2442 | 0 | (type2 <= XML_REGEXP_MARK_ENCLOSING)) || |
2443 | 0 | ((type2 >= XML_REGEXP_PUNCT) && |
2444 | 0 | (type2 <= XML_REGEXP_PUNCT_OTHERS)) || |
2445 | 0 | ((type2 >= XML_REGEXP_SEPAR) && |
2446 | 0 | (type2 <= XML_REGEXP_SEPAR_PARA)) || |
2447 | 0 | ((type2 >= XML_REGEXP_SYMBOL) && |
2448 | 0 | (type2 <= XML_REGEXP_SYMBOL_OTHERS)) || |
2449 | 0 | ((type2 >= XML_REGEXP_OTHER) && |
2450 | 0 | (type2 <= XML_REGEXP_OTHER_NA)) |
2451 | 0 | )return(0); |
2452 | 0 | break; |
2453 | 0 | case XML_REGEXP_NOTDECIMAL: /* \D */ |
2454 | 0 | break; |
2455 | 0 | case XML_REGEXP_REALCHAR: /* \w */ |
2456 | | /* can't be a mark, separator, punctuation, symbol or other */ |
2457 | 0 | if ((type2 == XML_REGEXP_NOTDECIMAL) || |
2458 | 0 | ((type2 >= XML_REGEXP_MARK) && |
2459 | 0 | (type2 <= XML_REGEXP_MARK_ENCLOSING)) || |
2460 | 0 | ((type2 >= XML_REGEXP_PUNCT) && |
2461 | 0 | (type2 <= XML_REGEXP_PUNCT_OTHERS)) || |
2462 | 0 | ((type2 >= XML_REGEXP_SEPAR) && |
2463 | 0 | (type2 <= XML_REGEXP_SEPAR_PARA)) || |
2464 | 0 | ((type2 >= XML_REGEXP_SYMBOL) && |
2465 | 0 | (type2 <= XML_REGEXP_SYMBOL_OTHERS)) || |
2466 | 0 | ((type2 >= XML_REGEXP_OTHER) && |
2467 | 0 | (type2 <= XML_REGEXP_OTHER_NA)) |
2468 | 0 | )return(0); |
2469 | 0 | break; |
2470 | 0 | case XML_REGEXP_NOTREALCHAR: /* \W */ |
2471 | 0 | break; |
2472 | | /* |
2473 | | * at that point we know both type 1 and type2 are from |
2474 | | * character categories are ordered and are different, |
2475 | | * it becomes simple because this is a partition |
2476 | | */ |
2477 | 0 | case XML_REGEXP_LETTER: |
2478 | 0 | if (type2 <= XML_REGEXP_LETTER_OTHERS) |
2479 | 0 | return(1); |
2480 | 0 | return(0); |
2481 | 0 | case XML_REGEXP_LETTER_UPPERCASE: |
2482 | 0 | case XML_REGEXP_LETTER_LOWERCASE: |
2483 | 0 | case XML_REGEXP_LETTER_TITLECASE: |
2484 | 0 | case XML_REGEXP_LETTER_MODIFIER: |
2485 | 0 | case XML_REGEXP_LETTER_OTHERS: |
2486 | 0 | return(0); |
2487 | 0 | case XML_REGEXP_MARK: |
2488 | 0 | if (type2 <= XML_REGEXP_MARK_ENCLOSING) |
2489 | 0 | return(1); |
2490 | 0 | return(0); |
2491 | 0 | case XML_REGEXP_MARK_NONSPACING: |
2492 | 0 | case XML_REGEXP_MARK_SPACECOMBINING: |
2493 | 0 | case XML_REGEXP_MARK_ENCLOSING: |
2494 | 0 | return(0); |
2495 | 0 | case XML_REGEXP_NUMBER: |
2496 | 0 | if (type2 <= XML_REGEXP_NUMBER_OTHERS) |
2497 | 0 | return(1); |
2498 | 0 | return(0); |
2499 | 0 | case XML_REGEXP_NUMBER_DECIMAL: |
2500 | 0 | case XML_REGEXP_NUMBER_LETTER: |
2501 | 0 | case XML_REGEXP_NUMBER_OTHERS: |
2502 | 0 | return(0); |
2503 | 0 | case XML_REGEXP_PUNCT: |
2504 | 0 | if (type2 <= XML_REGEXP_PUNCT_OTHERS) |
2505 | 0 | return(1); |
2506 | 0 | return(0); |
2507 | 0 | case XML_REGEXP_PUNCT_CONNECTOR: |
2508 | 0 | case XML_REGEXP_PUNCT_DASH: |
2509 | 0 | case XML_REGEXP_PUNCT_OPEN: |
2510 | 0 | case XML_REGEXP_PUNCT_CLOSE: |
2511 | 0 | case XML_REGEXP_PUNCT_INITQUOTE: |
2512 | 0 | case XML_REGEXP_PUNCT_FINQUOTE: |
2513 | 0 | case XML_REGEXP_PUNCT_OTHERS: |
2514 | 0 | return(0); |
2515 | 0 | case XML_REGEXP_SEPAR: |
2516 | 0 | if (type2 <= XML_REGEXP_SEPAR_PARA) |
2517 | 0 | return(1); |
2518 | 0 | return(0); |
2519 | 0 | case XML_REGEXP_SEPAR_SPACE: |
2520 | 0 | case XML_REGEXP_SEPAR_LINE: |
2521 | 0 | case XML_REGEXP_SEPAR_PARA: |
2522 | 0 | return(0); |
2523 | 0 | case XML_REGEXP_SYMBOL: |
2524 | 0 | if (type2 <= XML_REGEXP_SYMBOL_OTHERS) |
2525 | 0 | return(1); |
2526 | 0 | return(0); |
2527 | 0 | case XML_REGEXP_SYMBOL_MATH: |
2528 | 0 | case XML_REGEXP_SYMBOL_CURRENCY: |
2529 | 0 | case XML_REGEXP_SYMBOL_MODIFIER: |
2530 | 0 | case XML_REGEXP_SYMBOL_OTHERS: |
2531 | 0 | return(0); |
2532 | 0 | case XML_REGEXP_OTHER: |
2533 | 0 | if (type2 <= XML_REGEXP_OTHER_NA) |
2534 | 0 | return(1); |
2535 | 0 | return(0); |
2536 | 0 | case XML_REGEXP_OTHER_CONTROL: |
2537 | 0 | case XML_REGEXP_OTHER_FORMAT: |
2538 | 0 | case XML_REGEXP_OTHER_PRIVATE: |
2539 | 0 | case XML_REGEXP_OTHER_NA: |
2540 | 0 | return(0); |
2541 | 0 | default: |
2542 | 0 | break; |
2543 | 0 | } |
2544 | 0 | return(1); |
2545 | 0 | } |
2546 | | |
2547 | | /** |
2548 | | * xmlFAEqualAtoms: |
2549 | | * @atom1: an atom |
2550 | | * @atom2: an atom |
2551 | | * @deep: if not set only compare string pointers |
2552 | | * |
2553 | | * Compares two atoms to check whether they are the same exactly |
2554 | | * this is used to remove equivalent transitions |
2555 | | * |
2556 | | * Returns 1 if same and 0 otherwise |
2557 | | */ |
2558 | | static int |
2559 | 0 | xmlFAEqualAtoms(xmlRegAtomPtr atom1, xmlRegAtomPtr atom2, int deep) { |
2560 | 0 | int ret = 0; |
2561 | |
|
2562 | 0 | if (atom1 == atom2) |
2563 | 0 | return(1); |
2564 | 0 | if ((atom1 == NULL) || (atom2 == NULL)) |
2565 | 0 | return(0); |
2566 | | |
2567 | 0 | if (atom1->type != atom2->type) |
2568 | 0 | return(0); |
2569 | 0 | switch (atom1->type) { |
2570 | 0 | case XML_REGEXP_EPSILON: |
2571 | 0 | ret = 0; |
2572 | 0 | break; |
2573 | 0 | case XML_REGEXP_STRING: |
2574 | 0 | if (!deep) |
2575 | 0 | ret = (atom1->valuep == atom2->valuep); |
2576 | 0 | else |
2577 | 0 | ret = xmlStrEqual((xmlChar *)atom1->valuep, |
2578 | 0 | (xmlChar *)atom2->valuep); |
2579 | 0 | break; |
2580 | 0 | case XML_REGEXP_CHARVAL: |
2581 | 0 | ret = (atom1->codepoint == atom2->codepoint); |
2582 | 0 | break; |
2583 | 0 | case XML_REGEXP_RANGES: |
2584 | | /* too hard to do in the general case */ |
2585 | 0 | ret = 0; |
2586 | 0 | default: |
2587 | 0 | break; |
2588 | 0 | } |
2589 | 0 | return(ret); |
2590 | 0 | } |
2591 | | |
2592 | | /** |
2593 | | * xmlFACompareAtoms: |
2594 | | * @atom1: an atom |
2595 | | * @atom2: an atom |
2596 | | * @deep: if not set only compare string pointers |
2597 | | * |
2598 | | * Compares two atoms to check whether they intersect in some ways, |
2599 | | * this is used by xmlFAComputesDeterminism and xmlFARecurseDeterminism only |
2600 | | * |
2601 | | * Returns 1 if yes and 0 otherwise |
2602 | | */ |
2603 | | static int |
2604 | 0 | xmlFACompareAtoms(xmlRegAtomPtr atom1, xmlRegAtomPtr atom2, int deep) { |
2605 | 0 | int ret = 1; |
2606 | |
|
2607 | 0 | if (atom1 == atom2) |
2608 | 0 | return(1); |
2609 | 0 | if ((atom1 == NULL) || (atom2 == NULL)) |
2610 | 0 | return(0); |
2611 | | |
2612 | 0 | if ((atom1->type == XML_REGEXP_ANYCHAR) || |
2613 | 0 | (atom2->type == XML_REGEXP_ANYCHAR)) |
2614 | 0 | return(1); |
2615 | | |
2616 | 0 | if (atom1->type > atom2->type) { |
2617 | 0 | xmlRegAtomPtr tmp; |
2618 | 0 | tmp = atom1; |
2619 | 0 | atom1 = atom2; |
2620 | 0 | atom2 = tmp; |
2621 | 0 | } |
2622 | 0 | if (atom1->type != atom2->type) { |
2623 | 0 | ret = xmlFACompareAtomTypes(atom1->type, atom2->type); |
2624 | | /* if they can't intersect at the type level break now */ |
2625 | 0 | if (ret == 0) |
2626 | 0 | return(0); |
2627 | 0 | } |
2628 | 0 | switch (atom1->type) { |
2629 | 0 | case XML_REGEXP_STRING: |
2630 | 0 | if (!deep) |
2631 | 0 | ret = (atom1->valuep != atom2->valuep); |
2632 | 0 | else { |
2633 | 0 | xmlChar *val1 = (xmlChar *)atom1->valuep; |
2634 | 0 | xmlChar *val2 = (xmlChar *)atom2->valuep; |
2635 | 0 | int compound1 = (xmlStrchr(val1, '|') != NULL); |
2636 | 0 | int compound2 = (xmlStrchr(val2, '|') != NULL); |
2637 | | |
2638 | | /* Ignore negative match flag for ##other namespaces */ |
2639 | 0 | if (compound1 != compound2) |
2640 | 0 | return(0); |
2641 | | |
2642 | 0 | ret = xmlRegStrEqualWildcard(val1, val2); |
2643 | 0 | } |
2644 | 0 | break; |
2645 | 0 | case XML_REGEXP_EPSILON: |
2646 | 0 | goto not_determinist; |
2647 | 0 | case XML_REGEXP_CHARVAL: |
2648 | 0 | if (atom2->type == XML_REGEXP_CHARVAL) { |
2649 | 0 | ret = (atom1->codepoint == atom2->codepoint); |
2650 | 0 | } else { |
2651 | 0 | ret = xmlRegCheckCharacter(atom2, atom1->codepoint); |
2652 | 0 | if (ret < 0) |
2653 | 0 | ret = 1; |
2654 | 0 | } |
2655 | 0 | break; |
2656 | 0 | case XML_REGEXP_RANGES: |
2657 | 0 | if (atom2->type == XML_REGEXP_RANGES) { |
2658 | 0 | int i, j, res; |
2659 | 0 | xmlRegRangePtr r1, r2; |
2660 | | |
2661 | | /* |
2662 | | * need to check that none of the ranges eventually matches |
2663 | | */ |
2664 | 0 | for (i = 0;i < atom1->nbRanges;i++) { |
2665 | 0 | for (j = 0;j < atom2->nbRanges;j++) { |
2666 | 0 | r1 = atom1->ranges[i]; |
2667 | 0 | r2 = atom2->ranges[j]; |
2668 | 0 | res = xmlFACompareRanges(r1, r2); |
2669 | 0 | if (res == 1) { |
2670 | 0 | ret = 1; |
2671 | 0 | goto done; |
2672 | 0 | } |
2673 | 0 | } |
2674 | 0 | } |
2675 | 0 | ret = 0; |
2676 | 0 | } |
2677 | 0 | break; |
2678 | 0 | default: |
2679 | 0 | goto not_determinist; |
2680 | 0 | } |
2681 | 0 | done: |
2682 | 0 | if (atom1->neg != atom2->neg) { |
2683 | 0 | ret = !ret; |
2684 | 0 | } |
2685 | 0 | if (ret == 0) |
2686 | 0 | return(0); |
2687 | 0 | not_determinist: |
2688 | 0 | return(1); |
2689 | 0 | } |
2690 | | |
2691 | | /** |
2692 | | * xmlFARecurseDeterminism: |
2693 | | * @ctxt: a regexp parser context |
2694 | | * |
2695 | | * Check whether the associated regexp is determinist, |
2696 | | * should be called after xmlFAEliminateEpsilonTransitions() |
2697 | | * |
2698 | | */ |
2699 | | static int |
2700 | | xmlFARecurseDeterminism(xmlRegParserCtxtPtr ctxt, xmlRegStatePtr state, |
2701 | 0 | int fromnr, int tonr, xmlRegAtomPtr atom) { |
2702 | 0 | int ret = 1; |
2703 | 0 | int res; |
2704 | 0 | int transnr, nbTrans; |
2705 | 0 | xmlRegTransPtr t1; |
2706 | 0 | int deep = 1; |
2707 | |
|
2708 | 0 | if (state == NULL) |
2709 | 0 | return(ret); |
2710 | 0 | if (state->markd == XML_REGEXP_MARK_VISITED) |
2711 | 0 | return(ret); |
2712 | | |
2713 | 0 | if (ctxt->flags & AM_AUTOMATA_RNG) |
2714 | 0 | deep = 0; |
2715 | | |
2716 | | /* |
2717 | | * don't recurse on transitions potentially added in the course of |
2718 | | * the elimination. |
2719 | | */ |
2720 | 0 | nbTrans = state->nbTrans; |
2721 | 0 | for (transnr = 0;transnr < nbTrans;transnr++) { |
2722 | 0 | t1 = &(state->trans[transnr]); |
2723 | | /* |
2724 | | * check transitions conflicting with the one looked at |
2725 | | */ |
2726 | 0 | if ((t1->to < 0) || (t1->to == fromnr)) |
2727 | 0 | continue; |
2728 | 0 | if (t1->atom == NULL) { |
2729 | 0 | state->markd = XML_REGEXP_MARK_VISITED; |
2730 | 0 | res = xmlFARecurseDeterminism(ctxt, ctxt->states[t1->to], |
2731 | 0 | fromnr, tonr, atom); |
2732 | 0 | if (res == 0) { |
2733 | 0 | ret = 0; |
2734 | | /* t1->nd = 1; */ |
2735 | 0 | } |
2736 | 0 | continue; |
2737 | 0 | } |
2738 | 0 | if (xmlFACompareAtoms(t1->atom, atom, deep)) { |
2739 | | /* Treat equal transitions as deterministic. */ |
2740 | 0 | if ((t1->to != tonr) || |
2741 | 0 | (!xmlFAEqualAtoms(t1->atom, atom, deep))) |
2742 | 0 | ret = 0; |
2743 | | /* mark the transition as non-deterministic */ |
2744 | 0 | t1->nd = 1; |
2745 | 0 | } |
2746 | 0 | } |
2747 | 0 | return(ret); |
2748 | 0 | } |
2749 | | |
2750 | | /** |
2751 | | * xmlFAFinishRecurseDeterminism: |
2752 | | * @ctxt: a regexp parser context |
2753 | | * |
2754 | | * Reset flags after checking determinism. |
2755 | | */ |
2756 | | static void |
2757 | 0 | xmlFAFinishRecurseDeterminism(xmlRegParserCtxtPtr ctxt, xmlRegStatePtr state) { |
2758 | 0 | int transnr, nbTrans; |
2759 | |
|
2760 | 0 | if (state == NULL) |
2761 | 0 | return; |
2762 | 0 | if (state->markd != XML_REGEXP_MARK_VISITED) |
2763 | 0 | return; |
2764 | 0 | state->markd = 0; |
2765 | |
|
2766 | 0 | nbTrans = state->nbTrans; |
2767 | 0 | for (transnr = 0; transnr < nbTrans; transnr++) { |
2768 | 0 | xmlRegTransPtr t1 = &state->trans[transnr]; |
2769 | 0 | if ((t1->atom == NULL) && (t1->to >= 0)) |
2770 | 0 | xmlFAFinishRecurseDeterminism(ctxt, ctxt->states[t1->to]); |
2771 | 0 | } |
2772 | 0 | } |
2773 | | |
2774 | | /** |
2775 | | * xmlFAComputesDeterminism: |
2776 | | * @ctxt: a regexp parser context |
2777 | | * |
2778 | | * Check whether the associated regexp is determinist, |
2779 | | * should be called after xmlFAEliminateEpsilonTransitions() |
2780 | | * |
2781 | | */ |
2782 | | static int |
2783 | 0 | xmlFAComputesDeterminism(xmlRegParserCtxtPtr ctxt) { |
2784 | 0 | int statenr, transnr; |
2785 | 0 | xmlRegStatePtr state; |
2786 | 0 | xmlRegTransPtr t1, t2, last; |
2787 | 0 | int i; |
2788 | 0 | int ret = 1; |
2789 | 0 | int deep = 1; |
2790 | |
|
2791 | 0 | if (ctxt->determinist != -1) |
2792 | 0 | return(ctxt->determinist); |
2793 | | |
2794 | 0 | if (ctxt->flags & AM_AUTOMATA_RNG) |
2795 | 0 | deep = 0; |
2796 | | |
2797 | | /* |
2798 | | * First cleanup the automata removing cancelled transitions |
2799 | | */ |
2800 | 0 | for (statenr = 0;statenr < ctxt->nbStates;statenr++) { |
2801 | 0 | state = ctxt->states[statenr]; |
2802 | 0 | if (state == NULL) |
2803 | 0 | continue; |
2804 | 0 | if (state->nbTrans < 2) |
2805 | 0 | continue; |
2806 | 0 | for (transnr = 0;transnr < state->nbTrans;transnr++) { |
2807 | 0 | t1 = &(state->trans[transnr]); |
2808 | | /* |
2809 | | * Determinism checks in case of counted or all transitions |
2810 | | * will have to be handled separately |
2811 | | */ |
2812 | 0 | if (t1->atom == NULL) { |
2813 | | /* t1->nd = 1; */ |
2814 | 0 | continue; |
2815 | 0 | } |
2816 | 0 | if (t1->to < 0) /* eliminated */ |
2817 | 0 | continue; |
2818 | 0 | for (i = 0;i < transnr;i++) { |
2819 | 0 | t2 = &(state->trans[i]); |
2820 | 0 | if (t2->to < 0) /* eliminated */ |
2821 | 0 | continue; |
2822 | 0 | if (t2->atom != NULL) { |
2823 | 0 | if (t1->to == t2->to) { |
2824 | | /* |
2825 | | * Here we use deep because we want to keep the |
2826 | | * transitions which indicate a conflict |
2827 | | */ |
2828 | 0 | if (xmlFAEqualAtoms(t1->atom, t2->atom, deep) && |
2829 | 0 | (t1->counter == t2->counter) && |
2830 | 0 | (t1->count == t2->count)) |
2831 | 0 | t2->to = -1; /* eliminated */ |
2832 | 0 | } |
2833 | 0 | } |
2834 | 0 | } |
2835 | 0 | } |
2836 | 0 | } |
2837 | | |
2838 | | /* |
2839 | | * Check for all states that there aren't 2 transitions |
2840 | | * with the same atom and a different target. |
2841 | | */ |
2842 | 0 | for (statenr = 0;statenr < ctxt->nbStates;statenr++) { |
2843 | 0 | state = ctxt->states[statenr]; |
2844 | 0 | if (state == NULL) |
2845 | 0 | continue; |
2846 | 0 | if (state->nbTrans < 2) |
2847 | 0 | continue; |
2848 | 0 | last = NULL; |
2849 | 0 | for (transnr = 0;transnr < state->nbTrans;transnr++) { |
2850 | 0 | t1 = &(state->trans[transnr]); |
2851 | | /* |
2852 | | * Determinism checks in case of counted or all transitions |
2853 | | * will have to be handled separately |
2854 | | */ |
2855 | 0 | if (t1->atom == NULL) { |
2856 | 0 | continue; |
2857 | 0 | } |
2858 | 0 | if (t1->to < 0) /* eliminated */ |
2859 | 0 | continue; |
2860 | 0 | for (i = 0;i < transnr;i++) { |
2861 | 0 | t2 = &(state->trans[i]); |
2862 | 0 | if (t2->to < 0) /* eliminated */ |
2863 | 0 | continue; |
2864 | 0 | if (t2->atom != NULL) { |
2865 | | /* |
2866 | | * But here we don't use deep because we want to |
2867 | | * find transitions which indicate a conflict |
2868 | | */ |
2869 | 0 | if (xmlFACompareAtoms(t1->atom, t2->atom, 1)) { |
2870 | | /* |
2871 | | * Treat equal counter transitions that couldn't be |
2872 | | * eliminated as deterministic. |
2873 | | */ |
2874 | 0 | if ((t1->to != t2->to) || |
2875 | 0 | (t1->counter == t2->counter) || |
2876 | 0 | (!xmlFAEqualAtoms(t1->atom, t2->atom, deep))) |
2877 | 0 | ret = 0; |
2878 | | /* mark the transitions as non-deterministic ones */ |
2879 | 0 | t1->nd = 1; |
2880 | 0 | t2->nd = 1; |
2881 | 0 | last = t1; |
2882 | 0 | } |
2883 | 0 | } else { |
2884 | 0 | int res; |
2885 | | |
2886 | | /* |
2887 | | * do the closure in case of remaining specific |
2888 | | * epsilon transitions like choices or all |
2889 | | */ |
2890 | 0 | res = xmlFARecurseDeterminism(ctxt, ctxt->states[t2->to], |
2891 | 0 | statenr, t1->to, t1->atom); |
2892 | 0 | xmlFAFinishRecurseDeterminism(ctxt, ctxt->states[t2->to]); |
2893 | | /* don't shortcut the computation so all non deterministic |
2894 | | transition get marked down |
2895 | | if (ret == 0) |
2896 | | return(0); |
2897 | | */ |
2898 | 0 | if (res == 0) { |
2899 | 0 | t1->nd = 1; |
2900 | | /* t2->nd = 1; */ |
2901 | 0 | last = t1; |
2902 | 0 | ret = 0; |
2903 | 0 | } |
2904 | 0 | } |
2905 | 0 | } |
2906 | | /* don't shortcut the computation so all non deterministic |
2907 | | transition get marked down |
2908 | | if (ret == 0) |
2909 | | break; */ |
2910 | 0 | } |
2911 | | |
2912 | | /* |
2913 | | * mark specifically the last non-deterministic transition |
2914 | | * from a state since there is no need to set-up rollback |
2915 | | * from it |
2916 | | */ |
2917 | 0 | if (last != NULL) { |
2918 | 0 | last->nd = 2; |
2919 | 0 | } |
2920 | | |
2921 | | /* don't shortcut the computation so all non deterministic |
2922 | | transition get marked down |
2923 | | if (ret == 0) |
2924 | | break; */ |
2925 | 0 | } |
2926 | |
|
2927 | 0 | ctxt->determinist = ret; |
2928 | 0 | return(ret); |
2929 | 0 | } |
2930 | | |
2931 | | /************************************************************************ |
2932 | | * * |
2933 | | * Routines to check input against transition atoms * |
2934 | | * * |
2935 | | ************************************************************************/ |
2936 | | |
2937 | | static int |
2938 | | xmlRegCheckCharacterRange(xmlRegAtomType type, int codepoint, int neg, |
2939 | 0 | int start, int end, const xmlChar *blockName) { |
2940 | 0 | int ret = 0; |
2941 | |
|
2942 | 0 | switch (type) { |
2943 | 0 | case XML_REGEXP_STRING: |
2944 | 0 | case XML_REGEXP_SUBREG: |
2945 | 0 | case XML_REGEXP_RANGES: |
2946 | 0 | case XML_REGEXP_EPSILON: |
2947 | 0 | return(-1); |
2948 | 0 | case XML_REGEXP_ANYCHAR: |
2949 | 0 | ret = ((codepoint != '\n') && (codepoint != '\r')); |
2950 | 0 | break; |
2951 | 0 | case XML_REGEXP_CHARVAL: |
2952 | 0 | ret = ((codepoint >= start) && (codepoint <= end)); |
2953 | 0 | break; |
2954 | 0 | case XML_REGEXP_NOTSPACE: |
2955 | 0 | neg = !neg; |
2956 | | /* Falls through. */ |
2957 | 0 | case XML_REGEXP_ANYSPACE: |
2958 | 0 | ret = ((codepoint == '\n') || (codepoint == '\r') || |
2959 | 0 | (codepoint == '\t') || (codepoint == ' ')); |
2960 | 0 | break; |
2961 | 0 | case XML_REGEXP_NOTINITNAME: |
2962 | 0 | neg = !neg; |
2963 | | /* Falls through. */ |
2964 | 0 | case XML_REGEXP_INITNAME: |
2965 | 0 | ret = (IS_LETTER(codepoint) || |
2966 | 0 | (codepoint == '_') || (codepoint == ':')); |
2967 | 0 | break; |
2968 | 0 | case XML_REGEXP_NOTNAMECHAR: |
2969 | 0 | neg = !neg; |
2970 | | /* Falls through. */ |
2971 | 0 | case XML_REGEXP_NAMECHAR: |
2972 | 0 | ret = (IS_LETTER(codepoint) || IS_DIGIT(codepoint) || |
2973 | 0 | (codepoint == '.') || (codepoint == '-') || |
2974 | 0 | (codepoint == '_') || (codepoint == ':') || |
2975 | 0 | IS_COMBINING(codepoint) || IS_EXTENDER(codepoint)); |
2976 | 0 | break; |
2977 | 0 | case XML_REGEXP_NOTDECIMAL: |
2978 | 0 | neg = !neg; |
2979 | | /* Falls through. */ |
2980 | 0 | case XML_REGEXP_DECIMAL: |
2981 | 0 | ret = xmlUCSIsCatNd(codepoint); |
2982 | 0 | break; |
2983 | 0 | case XML_REGEXP_REALCHAR: |
2984 | 0 | neg = !neg; |
2985 | | /* Falls through. */ |
2986 | 0 | case XML_REGEXP_NOTREALCHAR: |
2987 | 0 | ret = xmlUCSIsCatP(codepoint); |
2988 | 0 | if (ret == 0) |
2989 | 0 | ret = xmlUCSIsCatZ(codepoint); |
2990 | 0 | if (ret == 0) |
2991 | 0 | ret = xmlUCSIsCatC(codepoint); |
2992 | 0 | break; |
2993 | 0 | case XML_REGEXP_LETTER: |
2994 | 0 | ret = xmlUCSIsCatL(codepoint); |
2995 | 0 | break; |
2996 | 0 | case XML_REGEXP_LETTER_UPPERCASE: |
2997 | 0 | ret = xmlUCSIsCatLu(codepoint); |
2998 | 0 | break; |
2999 | 0 | case XML_REGEXP_LETTER_LOWERCASE: |
3000 | 0 | ret = xmlUCSIsCatLl(codepoint); |
3001 | 0 | break; |
3002 | 0 | case XML_REGEXP_LETTER_TITLECASE: |
3003 | 0 | ret = xmlUCSIsCatLt(codepoint); |
3004 | 0 | break; |
3005 | 0 | case XML_REGEXP_LETTER_MODIFIER: |
3006 | 0 | ret = xmlUCSIsCatLm(codepoint); |
3007 | 0 | break; |
3008 | 0 | case XML_REGEXP_LETTER_OTHERS: |
3009 | 0 | ret = xmlUCSIsCatLo(codepoint); |
3010 | 0 | break; |
3011 | 0 | case XML_REGEXP_MARK: |
3012 | 0 | ret = xmlUCSIsCatM(codepoint); |
3013 | 0 | break; |
3014 | 0 | case XML_REGEXP_MARK_NONSPACING: |
3015 | 0 | ret = xmlUCSIsCatMn(codepoint); |
3016 | 0 | break; |
3017 | 0 | case XML_REGEXP_MARK_SPACECOMBINING: |
3018 | 0 | ret = xmlUCSIsCatMc(codepoint); |
3019 | 0 | break; |
3020 | 0 | case XML_REGEXP_MARK_ENCLOSING: |
3021 | 0 | ret = xmlUCSIsCatMe(codepoint); |
3022 | 0 | break; |
3023 | 0 | case XML_REGEXP_NUMBER: |
3024 | 0 | ret = xmlUCSIsCatN(codepoint); |
3025 | 0 | break; |
3026 | 0 | case XML_REGEXP_NUMBER_DECIMAL: |
3027 | 0 | ret = xmlUCSIsCatNd(codepoint); |
3028 | 0 | break; |
3029 | 0 | case XML_REGEXP_NUMBER_LETTER: |
3030 | 0 | ret = xmlUCSIsCatNl(codepoint); |
3031 | 0 | break; |
3032 | 0 | case XML_REGEXP_NUMBER_OTHERS: |
3033 | 0 | ret = xmlUCSIsCatNo(codepoint); |
3034 | 0 | break; |
3035 | 0 | case XML_REGEXP_PUNCT: |
3036 | 0 | ret = xmlUCSIsCatP(codepoint); |
3037 | 0 | break; |
3038 | 0 | case XML_REGEXP_PUNCT_CONNECTOR: |
3039 | 0 | ret = xmlUCSIsCatPc(codepoint); |
3040 | 0 | break; |
3041 | 0 | case XML_REGEXP_PUNCT_DASH: |
3042 | 0 | ret = xmlUCSIsCatPd(codepoint); |
3043 | 0 | break; |
3044 | 0 | case XML_REGEXP_PUNCT_OPEN: |
3045 | 0 | ret = xmlUCSIsCatPs(codepoint); |
3046 | 0 | break; |
3047 | 0 | case XML_REGEXP_PUNCT_CLOSE: |
3048 | 0 | ret = xmlUCSIsCatPe(codepoint); |
3049 | 0 | break; |
3050 | 0 | case XML_REGEXP_PUNCT_INITQUOTE: |
3051 | 0 | ret = xmlUCSIsCatPi(codepoint); |
3052 | 0 | break; |
3053 | 0 | case XML_REGEXP_PUNCT_FINQUOTE: |
3054 | 0 | ret = xmlUCSIsCatPf(codepoint); |
3055 | 0 | break; |
3056 | 0 | case XML_REGEXP_PUNCT_OTHERS: |
3057 | 0 | ret = xmlUCSIsCatPo(codepoint); |
3058 | 0 | break; |
3059 | 0 | case XML_REGEXP_SEPAR: |
3060 | 0 | ret = xmlUCSIsCatZ(codepoint); |
3061 | 0 | break; |
3062 | 0 | case XML_REGEXP_SEPAR_SPACE: |
3063 | 0 | ret = xmlUCSIsCatZs(codepoint); |
3064 | 0 | break; |
3065 | 0 | case XML_REGEXP_SEPAR_LINE: |
3066 | 0 | ret = xmlUCSIsCatZl(codepoint); |
3067 | 0 | break; |
3068 | 0 | case XML_REGEXP_SEPAR_PARA: |
3069 | 0 | ret = xmlUCSIsCatZp(codepoint); |
3070 | 0 | break; |
3071 | 0 | case XML_REGEXP_SYMBOL: |
3072 | 0 | ret = xmlUCSIsCatS(codepoint); |
3073 | 0 | break; |
3074 | 0 | case XML_REGEXP_SYMBOL_MATH: |
3075 | 0 | ret = xmlUCSIsCatSm(codepoint); |
3076 | 0 | break; |
3077 | 0 | case XML_REGEXP_SYMBOL_CURRENCY: |
3078 | 0 | ret = xmlUCSIsCatSc(codepoint); |
3079 | 0 | break; |
3080 | 0 | case XML_REGEXP_SYMBOL_MODIFIER: |
3081 | 0 | ret = xmlUCSIsCatSk(codepoint); |
3082 | 0 | break; |
3083 | 0 | case XML_REGEXP_SYMBOL_OTHERS: |
3084 | 0 | ret = xmlUCSIsCatSo(codepoint); |
3085 | 0 | break; |
3086 | 0 | case XML_REGEXP_OTHER: |
3087 | 0 | ret = xmlUCSIsCatC(codepoint); |
3088 | 0 | break; |
3089 | 0 | case XML_REGEXP_OTHER_CONTROL: |
3090 | 0 | ret = xmlUCSIsCatCc(codepoint); |
3091 | 0 | break; |
3092 | 0 | case XML_REGEXP_OTHER_FORMAT: |
3093 | 0 | ret = xmlUCSIsCatCf(codepoint); |
3094 | 0 | break; |
3095 | 0 | case XML_REGEXP_OTHER_PRIVATE: |
3096 | 0 | ret = xmlUCSIsCatCo(codepoint); |
3097 | 0 | break; |
3098 | 0 | case XML_REGEXP_OTHER_NA: |
3099 | | /* ret = xmlUCSIsCatCn(codepoint); */ |
3100 | | /* Seems it doesn't exist anymore in recent Unicode releases */ |
3101 | 0 | ret = 0; |
3102 | 0 | break; |
3103 | 0 | case XML_REGEXP_BLOCK_NAME: |
3104 | 0 | ret = xmlUCSIsBlock(codepoint, (const char *) blockName); |
3105 | 0 | break; |
3106 | 0 | } |
3107 | 0 | if (neg) |
3108 | 0 | return(!ret); |
3109 | 0 | return(ret); |
3110 | 0 | } |
3111 | | |
3112 | | static int |
3113 | 0 | xmlRegCheckCharacter(xmlRegAtomPtr atom, int codepoint) { |
3114 | 0 | int i, ret = 0; |
3115 | 0 | xmlRegRangePtr range; |
3116 | |
|
3117 | 0 | if ((atom == NULL) || (!IS_CHAR(codepoint))) |
3118 | 0 | return(-1); |
3119 | | |
3120 | 0 | switch (atom->type) { |
3121 | 0 | case XML_REGEXP_SUBREG: |
3122 | 0 | case XML_REGEXP_EPSILON: |
3123 | 0 | return(-1); |
3124 | 0 | case XML_REGEXP_CHARVAL: |
3125 | 0 | return(codepoint == atom->codepoint); |
3126 | 0 | case XML_REGEXP_RANGES: { |
3127 | 0 | int accept = 0; |
3128 | |
|
3129 | 0 | for (i = 0;i < atom->nbRanges;i++) { |
3130 | 0 | range = atom->ranges[i]; |
3131 | 0 | if (range->neg == 2) { |
3132 | 0 | ret = xmlRegCheckCharacterRange(range->type, codepoint, |
3133 | 0 | 0, range->start, range->end, |
3134 | 0 | range->blockName); |
3135 | 0 | if (ret != 0) |
3136 | 0 | return(0); /* excluded char */ |
3137 | 0 | } else if (range->neg) { |
3138 | 0 | ret = xmlRegCheckCharacterRange(range->type, codepoint, |
3139 | 0 | 0, range->start, range->end, |
3140 | 0 | range->blockName); |
3141 | 0 | if (ret == 0) |
3142 | 0 | accept = 1; |
3143 | 0 | else |
3144 | 0 | return(0); |
3145 | 0 | } else { |
3146 | 0 | ret = xmlRegCheckCharacterRange(range->type, codepoint, |
3147 | 0 | 0, range->start, range->end, |
3148 | 0 | range->blockName); |
3149 | 0 | if (ret != 0) |
3150 | 0 | accept = 1; /* might still be excluded */ |
3151 | 0 | } |
3152 | 0 | } |
3153 | 0 | return(accept); |
3154 | 0 | } |
3155 | 0 | case XML_REGEXP_STRING: |
3156 | 0 | return(-1); |
3157 | 0 | case XML_REGEXP_ANYCHAR: |
3158 | 0 | case XML_REGEXP_ANYSPACE: |
3159 | 0 | case XML_REGEXP_NOTSPACE: |
3160 | 0 | case XML_REGEXP_INITNAME: |
3161 | 0 | case XML_REGEXP_NOTINITNAME: |
3162 | 0 | case XML_REGEXP_NAMECHAR: |
3163 | 0 | case XML_REGEXP_NOTNAMECHAR: |
3164 | 0 | case XML_REGEXP_DECIMAL: |
3165 | 0 | case XML_REGEXP_NOTDECIMAL: |
3166 | 0 | case XML_REGEXP_REALCHAR: |
3167 | 0 | case XML_REGEXP_NOTREALCHAR: |
3168 | 0 | case XML_REGEXP_LETTER: |
3169 | 0 | case XML_REGEXP_LETTER_UPPERCASE: |
3170 | 0 | case XML_REGEXP_LETTER_LOWERCASE: |
3171 | 0 | case XML_REGEXP_LETTER_TITLECASE: |
3172 | 0 | case XML_REGEXP_LETTER_MODIFIER: |
3173 | 0 | case XML_REGEXP_LETTER_OTHERS: |
3174 | 0 | case XML_REGEXP_MARK: |
3175 | 0 | case XML_REGEXP_MARK_NONSPACING: |
3176 | 0 | case XML_REGEXP_MARK_SPACECOMBINING: |
3177 | 0 | case XML_REGEXP_MARK_ENCLOSING: |
3178 | 0 | case XML_REGEXP_NUMBER: |
3179 | 0 | case XML_REGEXP_NUMBER_DECIMAL: |
3180 | 0 | case XML_REGEXP_NUMBER_LETTER: |
3181 | 0 | case XML_REGEXP_NUMBER_OTHERS: |
3182 | 0 | case XML_REGEXP_PUNCT: |
3183 | 0 | case XML_REGEXP_PUNCT_CONNECTOR: |
3184 | 0 | case XML_REGEXP_PUNCT_DASH: |
3185 | 0 | case XML_REGEXP_PUNCT_OPEN: |
3186 | 0 | case XML_REGEXP_PUNCT_CLOSE: |
3187 | 0 | case XML_REGEXP_PUNCT_INITQUOTE: |
3188 | 0 | case XML_REGEXP_PUNCT_FINQUOTE: |
3189 | 0 | case XML_REGEXP_PUNCT_OTHERS: |
3190 | 0 | case XML_REGEXP_SEPAR: |
3191 | 0 | case XML_REGEXP_SEPAR_SPACE: |
3192 | 0 | case XML_REGEXP_SEPAR_LINE: |
3193 | 0 | case XML_REGEXP_SEPAR_PARA: |
3194 | 0 | case XML_REGEXP_SYMBOL: |
3195 | 0 | case XML_REGEXP_SYMBOL_MATH: |
3196 | 0 | case XML_REGEXP_SYMBOL_CURRENCY: |
3197 | 0 | case XML_REGEXP_SYMBOL_MODIFIER: |
3198 | 0 | case XML_REGEXP_SYMBOL_OTHERS: |
3199 | 0 | case XML_REGEXP_OTHER: |
3200 | 0 | case XML_REGEXP_OTHER_CONTROL: |
3201 | 0 | case XML_REGEXP_OTHER_FORMAT: |
3202 | 0 | case XML_REGEXP_OTHER_PRIVATE: |
3203 | 0 | case XML_REGEXP_OTHER_NA: |
3204 | 0 | case XML_REGEXP_BLOCK_NAME: |
3205 | 0 | ret = xmlRegCheckCharacterRange(atom->type, codepoint, 0, 0, 0, |
3206 | 0 | (const xmlChar *)atom->valuep); |
3207 | 0 | if (atom->neg) |
3208 | 0 | ret = !ret; |
3209 | 0 | break; |
3210 | 0 | } |
3211 | 0 | return(ret); |
3212 | 0 | } |
3213 | | |
3214 | | /************************************************************************ |
3215 | | * * |
3216 | | * Saving and restoring state of an execution context * |
3217 | | * * |
3218 | | ************************************************************************/ |
3219 | | |
3220 | | static void |
3221 | 0 | xmlFARegExecSave(xmlRegExecCtxtPtr exec) { |
3222 | 0 | #ifdef MAX_PUSH |
3223 | 0 | if (exec->nbPush > MAX_PUSH) { |
3224 | 0 | exec->status = XML_REGEXP_INTERNAL_LIMIT; |
3225 | 0 | return; |
3226 | 0 | } |
3227 | 0 | exec->nbPush++; |
3228 | 0 | #endif |
3229 | |
|
3230 | 0 | if (exec->nbRollbacks >= exec->maxRollbacks) { |
3231 | 0 | xmlRegExecRollback *tmp; |
3232 | 0 | int newSize; |
3233 | 0 | int len = exec->nbRollbacks; |
3234 | |
|
3235 | 0 | newSize = xmlGrowCapacity(exec->maxRollbacks, sizeof(tmp[0]), |
3236 | 0 | 4, XML_MAX_ITEMS); |
3237 | 0 | if (newSize < 0) { |
3238 | 0 | exec->status = XML_REGEXP_OUT_OF_MEMORY; |
3239 | 0 | return; |
3240 | 0 | } |
3241 | 0 | tmp = xmlRealloc(exec->rollbacks, newSize * sizeof(tmp[0])); |
3242 | 0 | if (tmp == NULL) { |
3243 | 0 | exec->status = XML_REGEXP_OUT_OF_MEMORY; |
3244 | 0 | return; |
3245 | 0 | } |
3246 | 0 | exec->rollbacks = tmp; |
3247 | 0 | exec->maxRollbacks = newSize; |
3248 | 0 | tmp = &exec->rollbacks[len]; |
3249 | 0 | memset(tmp, 0, (exec->maxRollbacks - len) * sizeof(xmlRegExecRollback)); |
3250 | 0 | } |
3251 | 0 | exec->rollbacks[exec->nbRollbacks].state = exec->state; |
3252 | 0 | exec->rollbacks[exec->nbRollbacks].index = exec->index; |
3253 | 0 | exec->rollbacks[exec->nbRollbacks].nextbranch = exec->transno + 1; |
3254 | 0 | if (exec->comp->nbCounters > 0) { |
3255 | 0 | if (exec->rollbacks[exec->nbRollbacks].counts == NULL) { |
3256 | 0 | exec->rollbacks[exec->nbRollbacks].counts = (int *) |
3257 | 0 | xmlMalloc(exec->comp->nbCounters * sizeof(int)); |
3258 | 0 | if (exec->rollbacks[exec->nbRollbacks].counts == NULL) { |
3259 | 0 | exec->status = XML_REGEXP_OUT_OF_MEMORY; |
3260 | 0 | return; |
3261 | 0 | } |
3262 | 0 | } |
3263 | 0 | memcpy(exec->rollbacks[exec->nbRollbacks].counts, exec->counts, |
3264 | 0 | exec->comp->nbCounters * sizeof(int)); |
3265 | 0 | } |
3266 | 0 | exec->nbRollbacks++; |
3267 | 0 | } |
3268 | | |
3269 | | static void |
3270 | 0 | xmlFARegExecRollBack(xmlRegExecCtxtPtr exec) { |
3271 | 0 | if (exec->status != XML_REGEXP_OK) |
3272 | 0 | return; |
3273 | 0 | if (exec->nbRollbacks <= 0) { |
3274 | 0 | exec->status = XML_REGEXP_NOT_FOUND; |
3275 | 0 | return; |
3276 | 0 | } |
3277 | 0 | exec->nbRollbacks--; |
3278 | 0 | exec->state = exec->rollbacks[exec->nbRollbacks].state; |
3279 | 0 | exec->index = exec->rollbacks[exec->nbRollbacks].index; |
3280 | 0 | exec->transno = exec->rollbacks[exec->nbRollbacks].nextbranch; |
3281 | 0 | if (exec->comp->nbCounters > 0) { |
3282 | 0 | if (exec->rollbacks[exec->nbRollbacks].counts == NULL) { |
3283 | 0 | exec->status = XML_REGEXP_INTERNAL_ERROR; |
3284 | 0 | return; |
3285 | 0 | } |
3286 | 0 | if (exec->counts) { |
3287 | 0 | memcpy(exec->counts, exec->rollbacks[exec->nbRollbacks].counts, |
3288 | 0 | exec->comp->nbCounters * sizeof(int)); |
3289 | 0 | } |
3290 | 0 | } |
3291 | 0 | } |
3292 | | |
3293 | | /************************************************************************ |
3294 | | * * |
3295 | | * Verifier, running an input against a compiled regexp * |
3296 | | * * |
3297 | | ************************************************************************/ |
3298 | | |
3299 | | static int |
3300 | 0 | xmlFARegExec(xmlRegexpPtr comp, const xmlChar *content) { |
3301 | 0 | xmlRegExecCtxt execval; |
3302 | 0 | xmlRegExecCtxtPtr exec = &execval; |
3303 | 0 | int ret, codepoint = 0, len, deter; |
3304 | |
|
3305 | 0 | exec->inputString = content; |
3306 | 0 | exec->index = 0; |
3307 | 0 | exec->nbPush = 0; |
3308 | 0 | exec->determinist = 1; |
3309 | 0 | exec->maxRollbacks = 0; |
3310 | 0 | exec->nbRollbacks = 0; |
3311 | 0 | exec->rollbacks = NULL; |
3312 | 0 | exec->status = XML_REGEXP_OK; |
3313 | 0 | exec->comp = comp; |
3314 | 0 | exec->state = comp->states[0]; |
3315 | 0 | exec->transno = 0; |
3316 | 0 | exec->transcount = 0; |
3317 | 0 | exec->inputStack = NULL; |
3318 | 0 | exec->inputStackMax = 0; |
3319 | 0 | if (comp->nbCounters > 0) { |
3320 | 0 | exec->counts = (int *) xmlMalloc(comp->nbCounters * sizeof(int)); |
3321 | 0 | if (exec->counts == NULL) { |
3322 | 0 | return(XML_REGEXP_OUT_OF_MEMORY); |
3323 | 0 | } |
3324 | 0 | memset(exec->counts, 0, comp->nbCounters * sizeof(int)); |
3325 | 0 | } else |
3326 | 0 | exec->counts = NULL; |
3327 | 0 | while ((exec->status == XML_REGEXP_OK) && (exec->state != NULL) && |
3328 | 0 | ((exec->inputString[exec->index] != 0) || |
3329 | 0 | ((exec->state != NULL) && |
3330 | 0 | (exec->state->type != XML_REGEXP_FINAL_STATE)))) { |
3331 | 0 | xmlRegTransPtr trans; |
3332 | 0 | xmlRegAtomPtr atom; |
3333 | | |
3334 | | /* |
3335 | | * If end of input on non-terminal state, rollback, however we may |
3336 | | * still have epsilon like transition for counted transitions |
3337 | | * on counters, in that case don't break too early. Additionally, |
3338 | | * if we are working on a range like "AB{0,2}", where B is not present, |
3339 | | * we don't want to break. |
3340 | | */ |
3341 | 0 | len = 1; |
3342 | 0 | if ((exec->inputString[exec->index] == 0) && (exec->counts == NULL)) { |
3343 | | /* |
3344 | | * if there is a transition, we must check if |
3345 | | * atom allows minOccurs of 0 |
3346 | | */ |
3347 | 0 | if (exec->transno < exec->state->nbTrans) { |
3348 | 0 | trans = &exec->state->trans[exec->transno]; |
3349 | 0 | if (trans->to >=0) { |
3350 | 0 | atom = trans->atom; |
3351 | 0 | if (!((atom->min == 0) && (atom->max > 0))) |
3352 | 0 | goto rollback; |
3353 | 0 | } |
3354 | 0 | } else |
3355 | 0 | goto rollback; |
3356 | 0 | } |
3357 | | |
3358 | 0 | exec->transcount = 0; |
3359 | 0 | for (;exec->transno < exec->state->nbTrans;exec->transno++) { |
3360 | 0 | trans = &exec->state->trans[exec->transno]; |
3361 | 0 | if (trans->to < 0) |
3362 | 0 | continue; |
3363 | 0 | atom = trans->atom; |
3364 | 0 | ret = 0; |
3365 | 0 | deter = 1; |
3366 | 0 | if (trans->count >= 0) { |
3367 | 0 | int count; |
3368 | 0 | xmlRegCounterPtr counter; |
3369 | |
|
3370 | 0 | if (exec->counts == NULL) { |
3371 | 0 | exec->status = XML_REGEXP_INTERNAL_ERROR; |
3372 | 0 | goto error; |
3373 | 0 | } |
3374 | | /* |
3375 | | * A counted transition. |
3376 | | */ |
3377 | | |
3378 | 0 | count = exec->counts[trans->count]; |
3379 | 0 | counter = &exec->comp->counters[trans->count]; |
3380 | 0 | ret = ((count >= counter->min) && (count <= counter->max)); |
3381 | 0 | if ((ret) && (counter->min != counter->max)) |
3382 | 0 | deter = 0; |
3383 | 0 | } else if (atom == NULL) { |
3384 | 0 | exec->status = XML_REGEXP_INTERNAL_ERROR; |
3385 | 0 | break; |
3386 | 0 | } else if (exec->inputString[exec->index] != 0) { |
3387 | 0 | len = 4; |
3388 | 0 | codepoint = xmlGetUTF8Char(&exec->inputString[exec->index], |
3389 | 0 | &len); |
3390 | 0 | if (codepoint < 0) { |
3391 | 0 | exec->status = XML_REGEXP_INVALID_UTF8; |
3392 | 0 | goto error; |
3393 | 0 | } |
3394 | 0 | ret = xmlRegCheckCharacter(atom, codepoint); |
3395 | 0 | if ((ret == 1) && (atom->min >= 0) && (atom->max > 0)) { |
3396 | 0 | xmlRegStatePtr to = comp->states[trans->to]; |
3397 | | |
3398 | | /* |
3399 | | * this is a multiple input sequence |
3400 | | * If there is a counter associated increment it now. |
3401 | | * do not increment if the counter is already over the |
3402 | | * maximum limit in which case get to next transition |
3403 | | */ |
3404 | 0 | if (trans->counter >= 0) { |
3405 | 0 | xmlRegCounterPtr counter; |
3406 | |
|
3407 | 0 | if ((exec->counts == NULL) || |
3408 | 0 | (exec->comp == NULL) || |
3409 | 0 | (exec->comp->counters == NULL)) { |
3410 | 0 | exec->status = XML_REGEXP_INTERNAL_ERROR; |
3411 | 0 | goto error; |
3412 | 0 | } |
3413 | 0 | counter = &exec->comp->counters[trans->counter]; |
3414 | 0 | if (exec->counts[trans->counter] >= counter->max) |
3415 | 0 | continue; /* for loop on transitions */ |
3416 | 0 | } |
3417 | | /* Save before incrementing */ |
3418 | 0 | if (exec->state->nbTrans > exec->transno + 1) { |
3419 | 0 | xmlFARegExecSave(exec); |
3420 | 0 | if (exec->status != XML_REGEXP_OK) |
3421 | 0 | goto error; |
3422 | 0 | } |
3423 | 0 | if (trans->counter >= 0) { |
3424 | 0 | exec->counts[trans->counter]++; |
3425 | 0 | } |
3426 | 0 | exec->transcount = 1; |
3427 | 0 | do { |
3428 | | /* |
3429 | | * Try to progress as much as possible on the input |
3430 | | */ |
3431 | 0 | if (exec->transcount == atom->max) { |
3432 | 0 | break; |
3433 | 0 | } |
3434 | 0 | exec->index += len; |
3435 | | /* |
3436 | | * End of input: stop here |
3437 | | */ |
3438 | 0 | if (exec->inputString[exec->index] == 0) { |
3439 | 0 | exec->index -= len; |
3440 | 0 | break; |
3441 | 0 | } |
3442 | 0 | if (exec->transcount >= atom->min) { |
3443 | 0 | int transno = exec->transno; |
3444 | 0 | xmlRegStatePtr state = exec->state; |
3445 | | |
3446 | | /* |
3447 | | * The transition is acceptable save it |
3448 | | */ |
3449 | 0 | exec->transno = -1; /* trick */ |
3450 | 0 | exec->state = to; |
3451 | 0 | xmlFARegExecSave(exec); |
3452 | 0 | if (exec->status != XML_REGEXP_OK) |
3453 | 0 | goto error; |
3454 | 0 | exec->transno = transno; |
3455 | 0 | exec->state = state; |
3456 | 0 | } |
3457 | 0 | len = 4; |
3458 | 0 | codepoint = xmlGetUTF8Char( |
3459 | 0 | &exec->inputString[exec->index], &len); |
3460 | 0 | if (codepoint < 0) { |
3461 | 0 | exec->status = XML_REGEXP_INVALID_UTF8; |
3462 | 0 | goto error; |
3463 | 0 | } |
3464 | 0 | ret = xmlRegCheckCharacter(atom, codepoint); |
3465 | 0 | exec->transcount++; |
3466 | 0 | } while (ret == 1); |
3467 | 0 | if (exec->transcount < atom->min) |
3468 | 0 | ret = 0; |
3469 | | |
3470 | | /* |
3471 | | * If the last check failed but one transition was found |
3472 | | * possible, rollback |
3473 | | */ |
3474 | 0 | if (ret < 0) |
3475 | 0 | ret = 0; |
3476 | 0 | if (ret == 0) { |
3477 | 0 | goto rollback; |
3478 | 0 | } |
3479 | 0 | if (trans->counter >= 0) { |
3480 | 0 | if (exec->counts == NULL) { |
3481 | 0 | exec->status = XML_REGEXP_INTERNAL_ERROR; |
3482 | 0 | goto error; |
3483 | 0 | } |
3484 | 0 | exec->counts[trans->counter]--; |
3485 | 0 | } |
3486 | 0 | } else if ((ret == 0) && (atom->min == 0) && (atom->max > 0)) { |
3487 | | /* |
3488 | | * we don't match on the codepoint, but minOccurs of 0 |
3489 | | * says that's ok. Setting len to 0 inhibits stepping |
3490 | | * over the codepoint. |
3491 | | */ |
3492 | 0 | exec->transcount = 1; |
3493 | 0 | len = 0; |
3494 | 0 | ret = 1; |
3495 | 0 | } |
3496 | 0 | } else if ((atom->min == 0) && (atom->max > 0)) { |
3497 | | /* another spot to match when minOccurs is 0 */ |
3498 | 0 | exec->transcount = 1; |
3499 | 0 | len = 0; |
3500 | 0 | ret = 1; |
3501 | 0 | } |
3502 | 0 | if (ret == 1) { |
3503 | 0 | if ((trans->nd == 1) || |
3504 | 0 | ((trans->count >= 0) && (deter == 0) && |
3505 | 0 | (exec->state->nbTrans > exec->transno + 1))) { |
3506 | 0 | xmlFARegExecSave(exec); |
3507 | 0 | if (exec->status != XML_REGEXP_OK) |
3508 | 0 | goto error; |
3509 | 0 | } |
3510 | 0 | if (trans->counter >= 0) { |
3511 | 0 | xmlRegCounterPtr counter; |
3512 | | |
3513 | | /* make sure we don't go over the counter maximum value */ |
3514 | 0 | if ((exec->counts == NULL) || |
3515 | 0 | (exec->comp == NULL) || |
3516 | 0 | (exec->comp->counters == NULL)) { |
3517 | 0 | exec->status = XML_REGEXP_INTERNAL_ERROR; |
3518 | 0 | goto error; |
3519 | 0 | } |
3520 | 0 | counter = &exec->comp->counters[trans->counter]; |
3521 | 0 | if (exec->counts[trans->counter] >= counter->max) |
3522 | 0 | continue; /* for loop on transitions */ |
3523 | 0 | exec->counts[trans->counter]++; |
3524 | 0 | } |
3525 | 0 | if ((trans->count >= 0) && |
3526 | 0 | (trans->count < REGEXP_ALL_COUNTER)) { |
3527 | 0 | if (exec->counts == NULL) { |
3528 | 0 | exec->status = XML_REGEXP_INTERNAL_ERROR; |
3529 | 0 | goto error; |
3530 | 0 | } |
3531 | 0 | exec->counts[trans->count] = 0; |
3532 | 0 | } |
3533 | 0 | exec->state = comp->states[trans->to]; |
3534 | 0 | exec->transno = 0; |
3535 | 0 | if (trans->atom != NULL) { |
3536 | 0 | exec->index += len; |
3537 | 0 | } |
3538 | 0 | goto progress; |
3539 | 0 | } else if (ret < 0) { |
3540 | 0 | exec->status = XML_REGEXP_INTERNAL_ERROR; |
3541 | 0 | break; |
3542 | 0 | } |
3543 | 0 | } |
3544 | 0 | if ((exec->transno != 0) || (exec->state->nbTrans == 0)) { |
3545 | 0 | rollback: |
3546 | | /* |
3547 | | * Failed to find a way out |
3548 | | */ |
3549 | 0 | exec->determinist = 0; |
3550 | 0 | xmlFARegExecRollBack(exec); |
3551 | 0 | } |
3552 | 0 | progress: |
3553 | 0 | continue; |
3554 | 0 | } |
3555 | 0 | error: |
3556 | 0 | if (exec->rollbacks != NULL) { |
3557 | 0 | if (exec->counts != NULL) { |
3558 | 0 | int i; |
3559 | |
|
3560 | 0 | for (i = 0;i < exec->maxRollbacks;i++) |
3561 | 0 | if (exec->rollbacks[i].counts != NULL) |
3562 | 0 | xmlFree(exec->rollbacks[i].counts); |
3563 | 0 | } |
3564 | 0 | xmlFree(exec->rollbacks); |
3565 | 0 | } |
3566 | 0 | if (exec->state == NULL) |
3567 | 0 | return(XML_REGEXP_INTERNAL_ERROR); |
3568 | 0 | if (exec->counts != NULL) |
3569 | 0 | xmlFree(exec->counts); |
3570 | 0 | if (exec->status == XML_REGEXP_OK) |
3571 | 0 | return(1); |
3572 | 0 | if (exec->status == XML_REGEXP_NOT_FOUND) |
3573 | 0 | return(0); |
3574 | 0 | return(exec->status); |
3575 | 0 | } |
3576 | | |
3577 | | /************************************************************************ |
3578 | | * * |
3579 | | * Progressive interface to the verifier one atom at a time * |
3580 | | * * |
3581 | | ************************************************************************/ |
3582 | | |
3583 | | /** |
3584 | | * xmlRegNewExecCtxt: |
3585 | | * @comp: a precompiled regular expression |
3586 | | * @callback: a callback function used for handling progresses in the |
3587 | | * automata matching phase |
3588 | | * @data: the context data associated to the callback in this context |
3589 | | * |
3590 | | * Build a context used for progressive evaluation of a regexp. |
3591 | | * |
3592 | | * Returns the new context |
3593 | | */ |
3594 | | xmlRegExecCtxtPtr |
3595 | 0 | xmlRegNewExecCtxt(xmlRegexpPtr comp, xmlRegExecCallbacks callback, void *data) { |
3596 | 0 | xmlRegExecCtxtPtr exec; |
3597 | |
|
3598 | 0 | if (comp == NULL) |
3599 | 0 | return(NULL); |
3600 | 0 | if ((comp->compact == NULL) && (comp->states == NULL)) |
3601 | 0 | return(NULL); |
3602 | 0 | exec = (xmlRegExecCtxtPtr) xmlMalloc(sizeof(xmlRegExecCtxt)); |
3603 | 0 | if (exec == NULL) |
3604 | 0 | return(NULL); |
3605 | 0 | memset(exec, 0, sizeof(xmlRegExecCtxt)); |
3606 | 0 | exec->inputString = NULL; |
3607 | 0 | exec->index = 0; |
3608 | 0 | exec->determinist = 1; |
3609 | 0 | exec->maxRollbacks = 0; |
3610 | 0 | exec->nbRollbacks = 0; |
3611 | 0 | exec->rollbacks = NULL; |
3612 | 0 | exec->status = XML_REGEXP_OK; |
3613 | 0 | exec->comp = comp; |
3614 | 0 | if (comp->compact == NULL) |
3615 | 0 | exec->state = comp->states[0]; |
3616 | 0 | exec->transno = 0; |
3617 | 0 | exec->transcount = 0; |
3618 | 0 | exec->callback = callback; |
3619 | 0 | exec->data = data; |
3620 | 0 | if (comp->nbCounters > 0) { |
3621 | | /* |
3622 | | * For error handling, exec->counts is allocated twice the size |
3623 | | * the second half is used to store the data in case of rollback |
3624 | | */ |
3625 | 0 | exec->counts = (int *) xmlMalloc(comp->nbCounters * sizeof(int) |
3626 | 0 | * 2); |
3627 | 0 | if (exec->counts == NULL) { |
3628 | 0 | xmlFree(exec); |
3629 | 0 | return(NULL); |
3630 | 0 | } |
3631 | 0 | memset(exec->counts, 0, comp->nbCounters * sizeof(int) * 2); |
3632 | 0 | exec->errCounts = &exec->counts[comp->nbCounters]; |
3633 | 0 | } else { |
3634 | 0 | exec->counts = NULL; |
3635 | 0 | exec->errCounts = NULL; |
3636 | 0 | } |
3637 | 0 | exec->inputStackMax = 0; |
3638 | 0 | exec->inputStackNr = 0; |
3639 | 0 | exec->inputStack = NULL; |
3640 | 0 | exec->errStateNo = -1; |
3641 | 0 | exec->errString = NULL; |
3642 | 0 | exec->nbPush = 0; |
3643 | 0 | return(exec); |
3644 | 0 | } |
3645 | | |
3646 | | /** |
3647 | | * xmlRegFreeExecCtxt: |
3648 | | * @exec: a regular expression evaluation context |
3649 | | * |
3650 | | * Free the structures associated to a regular expression evaluation context. |
3651 | | */ |
3652 | | void |
3653 | 0 | xmlRegFreeExecCtxt(xmlRegExecCtxtPtr exec) { |
3654 | 0 | if (exec == NULL) |
3655 | 0 | return; |
3656 | | |
3657 | 0 | if (exec->rollbacks != NULL) { |
3658 | 0 | if (exec->counts != NULL) { |
3659 | 0 | int i; |
3660 | |
|
3661 | 0 | for (i = 0;i < exec->maxRollbacks;i++) |
3662 | 0 | if (exec->rollbacks[i].counts != NULL) |
3663 | 0 | xmlFree(exec->rollbacks[i].counts); |
3664 | 0 | } |
3665 | 0 | xmlFree(exec->rollbacks); |
3666 | 0 | } |
3667 | 0 | if (exec->counts != NULL) |
3668 | 0 | xmlFree(exec->counts); |
3669 | 0 | if (exec->inputStack != NULL) { |
3670 | 0 | int i; |
3671 | |
|
3672 | 0 | for (i = 0;i < exec->inputStackNr;i++) { |
3673 | 0 | if (exec->inputStack[i].value != NULL) |
3674 | 0 | xmlFree(exec->inputStack[i].value); |
3675 | 0 | } |
3676 | 0 | xmlFree(exec->inputStack); |
3677 | 0 | } |
3678 | 0 | if (exec->errString != NULL) |
3679 | 0 | xmlFree(exec->errString); |
3680 | 0 | xmlFree(exec); |
3681 | 0 | } |
3682 | | |
3683 | | static int |
3684 | 0 | xmlRegExecSetErrString(xmlRegExecCtxtPtr exec, const xmlChar *value) { |
3685 | 0 | if (exec->errString != NULL) |
3686 | 0 | xmlFree(exec->errString); |
3687 | 0 | if (value == NULL) { |
3688 | 0 | exec->errString = NULL; |
3689 | 0 | } else { |
3690 | 0 | exec->errString = xmlStrdup(value); |
3691 | 0 | if (exec->errString == NULL) { |
3692 | 0 | exec->status = XML_REGEXP_OUT_OF_MEMORY; |
3693 | 0 | return(-1); |
3694 | 0 | } |
3695 | 0 | } |
3696 | 0 | return(0); |
3697 | 0 | } |
3698 | | |
3699 | | static void |
3700 | | xmlFARegExecSaveInputString(xmlRegExecCtxtPtr exec, const xmlChar *value, |
3701 | 0 | void *data) { |
3702 | 0 | if (exec->inputStackNr + 1 >= exec->inputStackMax) { |
3703 | 0 | xmlRegInputTokenPtr tmp; |
3704 | 0 | int newSize; |
3705 | |
|
3706 | 0 | newSize = xmlGrowCapacity(exec->inputStackMax, sizeof(tmp[0]), |
3707 | 0 | 4, XML_MAX_ITEMS); |
3708 | 0 | if (newSize < 0) { |
3709 | 0 | exec->status = XML_REGEXP_OUT_OF_MEMORY; |
3710 | 0 | return; |
3711 | 0 | } |
3712 | 0 | #ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION |
3713 | 0 | if (newSize < 2) |
3714 | 0 | newSize = 2; |
3715 | 0 | #endif |
3716 | 0 | tmp = xmlRealloc(exec->inputStack, newSize * sizeof(tmp[0])); |
3717 | 0 | if (tmp == NULL) { |
3718 | 0 | exec->status = XML_REGEXP_OUT_OF_MEMORY; |
3719 | 0 | return; |
3720 | 0 | } |
3721 | 0 | exec->inputStack = tmp; |
3722 | 0 | exec->inputStackMax = newSize; |
3723 | 0 | } |
3724 | 0 | if (value == NULL) { |
3725 | 0 | exec->inputStack[exec->inputStackNr].value = NULL; |
3726 | 0 | } else { |
3727 | 0 | exec->inputStack[exec->inputStackNr].value = xmlStrdup(value); |
3728 | 0 | if (exec->inputStack[exec->inputStackNr].value == NULL) { |
3729 | 0 | exec->status = XML_REGEXP_OUT_OF_MEMORY; |
3730 | 0 | return; |
3731 | 0 | } |
3732 | 0 | } |
3733 | 0 | exec->inputStack[exec->inputStackNr].data = data; |
3734 | 0 | exec->inputStackNr++; |
3735 | 0 | exec->inputStack[exec->inputStackNr].value = NULL; |
3736 | 0 | exec->inputStack[exec->inputStackNr].data = NULL; |
3737 | 0 | } |
3738 | | |
3739 | | /** |
3740 | | * xmlRegStrEqualWildcard: |
3741 | | * @expStr: the string to be evaluated |
3742 | | * @valStr: the validation string |
3743 | | * |
3744 | | * Checks if both strings are equal or have the same content. "*" |
3745 | | * can be used as a wildcard in @valStr; "|" is used as a separator of |
3746 | | * substrings in both @expStr and @valStr. |
3747 | | * |
3748 | | * Returns 1 if the comparison is satisfied and the number of substrings |
3749 | | * is equal, 0 otherwise. |
3750 | | */ |
3751 | | |
3752 | | static int |
3753 | 0 | xmlRegStrEqualWildcard(const xmlChar *expStr, const xmlChar *valStr) { |
3754 | 0 | if (expStr == valStr) return(1); |
3755 | 0 | if (expStr == NULL) return(0); |
3756 | 0 | if (valStr == NULL) return(0); |
3757 | 0 | do { |
3758 | | /* |
3759 | | * Eval if we have a wildcard for the current item. |
3760 | | */ |
3761 | 0 | if (*expStr != *valStr) { |
3762 | | /* if one of them starts with a wildcard make valStr be it */ |
3763 | 0 | if (*valStr == '*') { |
3764 | 0 | const xmlChar *tmp; |
3765 | |
|
3766 | 0 | tmp = valStr; |
3767 | 0 | valStr = expStr; |
3768 | 0 | expStr = tmp; |
3769 | 0 | } |
3770 | 0 | if ((*valStr != 0) && (*expStr != 0) && (*expStr++ == '*')) { |
3771 | 0 | do { |
3772 | 0 | if (*valStr == XML_REG_STRING_SEPARATOR) |
3773 | 0 | break; |
3774 | 0 | valStr++; |
3775 | 0 | } while (*valStr != 0); |
3776 | 0 | continue; |
3777 | 0 | } else |
3778 | 0 | return(0); |
3779 | 0 | } |
3780 | 0 | expStr++; |
3781 | 0 | valStr++; |
3782 | 0 | } while (*valStr != 0); |
3783 | 0 | if (*expStr != 0) |
3784 | 0 | return (0); |
3785 | 0 | else |
3786 | 0 | return (1); |
3787 | 0 | } |
3788 | | |
3789 | | /** |
3790 | | * xmlRegCompactPushString: |
3791 | | * @exec: a regexp execution context |
3792 | | * @comp: the precompiled exec with a compact table |
3793 | | * @value: a string token input |
3794 | | * @data: data associated to the token to reuse in callbacks |
3795 | | * |
3796 | | * Push one input token in the execution context |
3797 | | * |
3798 | | * Returns: 1 if the regexp reached a final state, 0 if non-final, and |
3799 | | * a negative value in case of error. |
3800 | | */ |
3801 | | static int |
3802 | | xmlRegCompactPushString(xmlRegExecCtxtPtr exec, |
3803 | | xmlRegexpPtr comp, |
3804 | | const xmlChar *value, |
3805 | 0 | void *data) { |
3806 | 0 | int state = exec->index; |
3807 | 0 | int i, target; |
3808 | |
|
3809 | 0 | if ((comp == NULL) || (comp->compact == NULL) || (comp->stringMap == NULL)) |
3810 | 0 | return(-1); |
3811 | | |
3812 | 0 | if (value == NULL) { |
3813 | | /* |
3814 | | * are we at a final state ? |
3815 | | */ |
3816 | 0 | if (comp->compact[state * (comp->nbstrings + 1)] == |
3817 | 0 | XML_REGEXP_FINAL_STATE) |
3818 | 0 | return(1); |
3819 | 0 | return(0); |
3820 | 0 | } |
3821 | | |
3822 | | /* |
3823 | | * Examine all outside transitions from current state |
3824 | | */ |
3825 | 0 | for (i = 0;i < comp->nbstrings;i++) { |
3826 | 0 | target = comp->compact[state * (comp->nbstrings + 1) + i + 1]; |
3827 | 0 | if ((target > 0) && (target <= comp->nbstates)) { |
3828 | 0 | target--; /* to avoid 0 */ |
3829 | 0 | if (xmlRegStrEqualWildcard(comp->stringMap[i], value)) { |
3830 | 0 | exec->index = target; |
3831 | 0 | if ((exec->callback != NULL) && (comp->transdata != NULL)) { |
3832 | 0 | exec->callback(exec->data, value, |
3833 | 0 | comp->transdata[state * comp->nbstrings + i], data); |
3834 | 0 | } |
3835 | 0 | if (comp->compact[target * (comp->nbstrings + 1)] == |
3836 | 0 | XML_REGEXP_SINK_STATE) |
3837 | 0 | goto error; |
3838 | | |
3839 | 0 | if (comp->compact[target * (comp->nbstrings + 1)] == |
3840 | 0 | XML_REGEXP_FINAL_STATE) |
3841 | 0 | return(1); |
3842 | 0 | return(0); |
3843 | 0 | } |
3844 | 0 | } |
3845 | 0 | } |
3846 | | /* |
3847 | | * Failed to find an exit transition out from current state for the |
3848 | | * current token |
3849 | | */ |
3850 | 0 | error: |
3851 | 0 | exec->errStateNo = state; |
3852 | 0 | exec->status = XML_REGEXP_NOT_FOUND; |
3853 | 0 | xmlRegExecSetErrString(exec, value); |
3854 | 0 | return(exec->status); |
3855 | 0 | } |
3856 | | |
3857 | | /** |
3858 | | * xmlRegExecPushStringInternal: |
3859 | | * @exec: a regexp execution context or NULL to indicate the end |
3860 | | * @value: a string token input |
3861 | | * @data: data associated to the token to reuse in callbacks |
3862 | | * @compound: value was assembled from 2 strings |
3863 | | * |
3864 | | * Push one input token in the execution context |
3865 | | * |
3866 | | * Returns: 1 if the regexp reached a final state, 0 if non-final, and |
3867 | | * a negative value in case of error. |
3868 | | */ |
3869 | | static int |
3870 | | xmlRegExecPushStringInternal(xmlRegExecCtxtPtr exec, const xmlChar *value, |
3871 | 0 | void *data, int compound) { |
3872 | 0 | xmlRegTransPtr trans; |
3873 | 0 | xmlRegAtomPtr atom; |
3874 | 0 | int ret; |
3875 | 0 | int final = 0; |
3876 | 0 | int progress = 1; |
3877 | |
|
3878 | 0 | if (exec == NULL) |
3879 | 0 | return(-1); |
3880 | 0 | if (exec->comp == NULL) |
3881 | 0 | return(-1); |
3882 | 0 | if (exec->status != XML_REGEXP_OK) |
3883 | 0 | return(exec->status); |
3884 | | |
3885 | 0 | if (exec->comp->compact != NULL) |
3886 | 0 | return(xmlRegCompactPushString(exec, exec->comp, value, data)); |
3887 | | |
3888 | 0 | if (value == NULL) { |
3889 | 0 | if (exec->state->type == XML_REGEXP_FINAL_STATE) |
3890 | 0 | return(1); |
3891 | 0 | final = 1; |
3892 | 0 | } |
3893 | | |
3894 | | /* |
3895 | | * If we have an active rollback stack push the new value there |
3896 | | * and get back to where we were left |
3897 | | */ |
3898 | 0 | if ((value != NULL) && (exec->inputStackNr > 0)) { |
3899 | 0 | xmlFARegExecSaveInputString(exec, value, data); |
3900 | 0 | value = exec->inputStack[exec->index].value; |
3901 | 0 | data = exec->inputStack[exec->index].data; |
3902 | 0 | } |
3903 | |
|
3904 | 0 | while ((exec->status == XML_REGEXP_OK) && |
3905 | 0 | ((value != NULL) || |
3906 | 0 | ((final == 1) && |
3907 | 0 | (exec->state->type != XML_REGEXP_FINAL_STATE)))) { |
3908 | | |
3909 | | /* |
3910 | | * End of input on non-terminal state, rollback, however we may |
3911 | | * still have epsilon like transition for counted transitions |
3912 | | * on counters, in that case don't break too early. |
3913 | | */ |
3914 | 0 | if ((value == NULL) && (exec->counts == NULL)) |
3915 | 0 | goto rollback; |
3916 | | |
3917 | 0 | exec->transcount = 0; |
3918 | 0 | for (;exec->transno < exec->state->nbTrans;exec->transno++) { |
3919 | 0 | trans = &exec->state->trans[exec->transno]; |
3920 | 0 | if (trans->to < 0) |
3921 | 0 | continue; |
3922 | 0 | atom = trans->atom; |
3923 | 0 | ret = 0; |
3924 | 0 | if (trans->count == REGEXP_ALL_LAX_COUNTER) { |
3925 | 0 | int i; |
3926 | 0 | int count; |
3927 | 0 | xmlRegTransPtr t; |
3928 | 0 | xmlRegCounterPtr counter; |
3929 | |
|
3930 | 0 | ret = 0; |
3931 | | |
3932 | | /* |
3933 | | * Check all counted transitions from the current state |
3934 | | */ |
3935 | 0 | if ((value == NULL) && (final)) { |
3936 | 0 | ret = 1; |
3937 | 0 | } else if (value != NULL) { |
3938 | 0 | for (i = 0;i < exec->state->nbTrans;i++) { |
3939 | 0 | t = &exec->state->trans[i]; |
3940 | 0 | if ((t->counter < 0) || (t == trans)) |
3941 | 0 | continue; |
3942 | 0 | counter = &exec->comp->counters[t->counter]; |
3943 | 0 | count = exec->counts[t->counter]; |
3944 | 0 | if ((count < counter->max) && |
3945 | 0 | (t->atom != NULL) && |
3946 | 0 | (xmlStrEqual(value, t->atom->valuep))) { |
3947 | 0 | ret = 0; |
3948 | 0 | break; |
3949 | 0 | } |
3950 | 0 | if ((count >= counter->min) && |
3951 | 0 | (count < counter->max) && |
3952 | 0 | (t->atom != NULL) && |
3953 | 0 | (xmlStrEqual(value, t->atom->valuep))) { |
3954 | 0 | ret = 1; |
3955 | 0 | break; |
3956 | 0 | } |
3957 | 0 | } |
3958 | 0 | } |
3959 | 0 | } else if (trans->count == REGEXP_ALL_COUNTER) { |
3960 | 0 | int i; |
3961 | 0 | int count; |
3962 | 0 | xmlRegTransPtr t; |
3963 | 0 | xmlRegCounterPtr counter; |
3964 | |
|
3965 | 0 | ret = 1; |
3966 | | |
3967 | | /* |
3968 | | * Check all counted transitions from the current state |
3969 | | */ |
3970 | 0 | for (i = 0;i < exec->state->nbTrans;i++) { |
3971 | 0 | t = &exec->state->trans[i]; |
3972 | 0 | if ((t->counter < 0) || (t == trans)) |
3973 | 0 | continue; |
3974 | 0 | counter = &exec->comp->counters[t->counter]; |
3975 | 0 | count = exec->counts[t->counter]; |
3976 | 0 | if ((count < counter->min) || (count > counter->max)) { |
3977 | 0 | ret = 0; |
3978 | 0 | break; |
3979 | 0 | } |
3980 | 0 | } |
3981 | 0 | } else if (trans->count >= 0) { |
3982 | 0 | int count; |
3983 | 0 | xmlRegCounterPtr counter; |
3984 | | |
3985 | | /* |
3986 | | * A counted transition. |
3987 | | */ |
3988 | |
|
3989 | 0 | count = exec->counts[trans->count]; |
3990 | 0 | counter = &exec->comp->counters[trans->count]; |
3991 | 0 | ret = ((count >= counter->min) && (count <= counter->max)); |
3992 | 0 | } else if (atom == NULL) { |
3993 | 0 | exec->status = XML_REGEXP_INTERNAL_ERROR; |
3994 | 0 | break; |
3995 | 0 | } else if (value != NULL) { |
3996 | 0 | ret = xmlRegStrEqualWildcard(atom->valuep, value); |
3997 | 0 | if (atom->neg) { |
3998 | 0 | ret = !ret; |
3999 | 0 | if (!compound) |
4000 | 0 | ret = 0; |
4001 | 0 | } |
4002 | 0 | if ((ret == 1) && (trans->counter >= 0)) { |
4003 | 0 | xmlRegCounterPtr counter; |
4004 | 0 | int count; |
4005 | |
|
4006 | 0 | count = exec->counts[trans->counter]; |
4007 | 0 | counter = &exec->comp->counters[trans->counter]; |
4008 | 0 | if (count >= counter->max) |
4009 | 0 | ret = 0; |
4010 | 0 | } |
4011 | |
|
4012 | 0 | if ((ret == 1) && (atom->min > 0) && (atom->max > 0)) { |
4013 | 0 | xmlRegStatePtr to = exec->comp->states[trans->to]; |
4014 | | |
4015 | | /* |
4016 | | * this is a multiple input sequence |
4017 | | */ |
4018 | 0 | if (exec->state->nbTrans > exec->transno + 1) { |
4019 | 0 | if (exec->inputStackNr <= 0) { |
4020 | 0 | xmlFARegExecSaveInputString(exec, value, data); |
4021 | 0 | } |
4022 | 0 | xmlFARegExecSave(exec); |
4023 | 0 | } |
4024 | 0 | exec->transcount = 1; |
4025 | 0 | do { |
4026 | | /* |
4027 | | * Try to progress as much as possible on the input |
4028 | | */ |
4029 | 0 | if (exec->transcount == atom->max) { |
4030 | 0 | break; |
4031 | 0 | } |
4032 | 0 | exec->index++; |
4033 | 0 | value = exec->inputStack[exec->index].value; |
4034 | 0 | data = exec->inputStack[exec->index].data; |
4035 | | |
4036 | | /* |
4037 | | * End of input: stop here |
4038 | | */ |
4039 | 0 | if (value == NULL) { |
4040 | 0 | exec->index --; |
4041 | 0 | break; |
4042 | 0 | } |
4043 | 0 | if (exec->transcount >= atom->min) { |
4044 | 0 | int transno = exec->transno; |
4045 | 0 | xmlRegStatePtr state = exec->state; |
4046 | | |
4047 | | /* |
4048 | | * The transition is acceptable save it |
4049 | | */ |
4050 | 0 | exec->transno = -1; /* trick */ |
4051 | 0 | exec->state = to; |
4052 | 0 | if (exec->inputStackNr <= 0) { |
4053 | 0 | xmlFARegExecSaveInputString(exec, value, data); |
4054 | 0 | } |
4055 | 0 | xmlFARegExecSave(exec); |
4056 | 0 | exec->transno = transno; |
4057 | 0 | exec->state = state; |
4058 | 0 | } |
4059 | 0 | ret = xmlStrEqual(value, atom->valuep); |
4060 | 0 | exec->transcount++; |
4061 | 0 | } while (ret == 1); |
4062 | 0 | if (exec->transcount < atom->min) |
4063 | 0 | ret = 0; |
4064 | | |
4065 | | /* |
4066 | | * If the last check failed but one transition was found |
4067 | | * possible, rollback |
4068 | | */ |
4069 | 0 | if (ret < 0) |
4070 | 0 | ret = 0; |
4071 | 0 | if (ret == 0) { |
4072 | 0 | goto rollback; |
4073 | 0 | } |
4074 | 0 | } |
4075 | 0 | } |
4076 | 0 | if (ret == 1) { |
4077 | 0 | if ((exec->callback != NULL) && (atom != NULL) && |
4078 | 0 | (data != NULL)) { |
4079 | 0 | exec->callback(exec->data, atom->valuep, |
4080 | 0 | atom->data, data); |
4081 | 0 | } |
4082 | 0 | if (exec->state->nbTrans > exec->transno + 1) { |
4083 | 0 | if (exec->inputStackNr <= 0) { |
4084 | 0 | xmlFARegExecSaveInputString(exec, value, data); |
4085 | 0 | } |
4086 | 0 | xmlFARegExecSave(exec); |
4087 | 0 | } |
4088 | 0 | if (trans->counter >= 0) { |
4089 | 0 | exec->counts[trans->counter]++; |
4090 | 0 | } |
4091 | 0 | if ((trans->count >= 0) && |
4092 | 0 | (trans->count < REGEXP_ALL_COUNTER)) { |
4093 | 0 | exec->counts[trans->count] = 0; |
4094 | 0 | } |
4095 | 0 | if ((exec->comp->states[trans->to] != NULL) && |
4096 | 0 | (exec->comp->states[trans->to]->type == |
4097 | 0 | XML_REGEXP_SINK_STATE)) { |
4098 | | /* |
4099 | | * entering a sink state, save the current state as error |
4100 | | * state. |
4101 | | */ |
4102 | 0 | if (xmlRegExecSetErrString(exec, value) < 0) |
4103 | 0 | break; |
4104 | 0 | exec->errState = exec->state; |
4105 | 0 | memcpy(exec->errCounts, exec->counts, |
4106 | 0 | exec->comp->nbCounters * sizeof(int)); |
4107 | 0 | } |
4108 | 0 | exec->state = exec->comp->states[trans->to]; |
4109 | 0 | exec->transno = 0; |
4110 | 0 | if (trans->atom != NULL) { |
4111 | 0 | if (exec->inputStack != NULL) { |
4112 | 0 | exec->index++; |
4113 | 0 | if (exec->index < exec->inputStackNr) { |
4114 | 0 | value = exec->inputStack[exec->index].value; |
4115 | 0 | data = exec->inputStack[exec->index].data; |
4116 | 0 | } else { |
4117 | 0 | value = NULL; |
4118 | 0 | data = NULL; |
4119 | 0 | } |
4120 | 0 | } else { |
4121 | 0 | value = NULL; |
4122 | 0 | data = NULL; |
4123 | 0 | } |
4124 | 0 | } |
4125 | 0 | goto progress; |
4126 | 0 | } else if (ret < 0) { |
4127 | 0 | exec->status = XML_REGEXP_INTERNAL_ERROR; |
4128 | 0 | break; |
4129 | 0 | } |
4130 | 0 | } |
4131 | 0 | if ((exec->transno != 0) || (exec->state->nbTrans == 0)) { |
4132 | 0 | rollback: |
4133 | | /* |
4134 | | * if we didn't yet rollback on the current input |
4135 | | * store the current state as the error state. |
4136 | | */ |
4137 | 0 | if ((progress) && (exec->state != NULL) && |
4138 | 0 | (exec->state->type != XML_REGEXP_SINK_STATE)) { |
4139 | 0 | progress = 0; |
4140 | 0 | if (xmlRegExecSetErrString(exec, value) < 0) |
4141 | 0 | break; |
4142 | 0 | exec->errState = exec->state; |
4143 | 0 | if (exec->comp->nbCounters) |
4144 | 0 | memcpy(exec->errCounts, exec->counts, |
4145 | 0 | exec->comp->nbCounters * sizeof(int)); |
4146 | 0 | } |
4147 | | |
4148 | | /* |
4149 | | * Failed to find a way out |
4150 | | */ |
4151 | 0 | exec->determinist = 0; |
4152 | 0 | xmlFARegExecRollBack(exec); |
4153 | 0 | if ((exec->inputStack != NULL ) && |
4154 | 0 | (exec->status == XML_REGEXP_OK)) { |
4155 | 0 | value = exec->inputStack[exec->index].value; |
4156 | 0 | data = exec->inputStack[exec->index].data; |
4157 | 0 | } |
4158 | 0 | } |
4159 | 0 | continue; |
4160 | 0 | progress: |
4161 | 0 | progress = 1; |
4162 | 0 | } |
4163 | 0 | if (exec->status == XML_REGEXP_OK) { |
4164 | 0 | return(exec->state->type == XML_REGEXP_FINAL_STATE); |
4165 | 0 | } |
4166 | 0 | return(exec->status); |
4167 | 0 | } |
4168 | | |
4169 | | /** |
4170 | | * xmlRegExecPushString: |
4171 | | * @exec: a regexp execution context or NULL to indicate the end |
4172 | | * @value: a string token input |
4173 | | * @data: data associated to the token to reuse in callbacks |
4174 | | * |
4175 | | * Push one input token in the execution context |
4176 | | * |
4177 | | * Returns: 1 if the regexp reached a final state, 0 if non-final, and |
4178 | | * a negative value in case of error. |
4179 | | */ |
4180 | | int |
4181 | | xmlRegExecPushString(xmlRegExecCtxtPtr exec, const xmlChar *value, |
4182 | 0 | void *data) { |
4183 | 0 | return(xmlRegExecPushStringInternal(exec, value, data, 0)); |
4184 | 0 | } |
4185 | | |
4186 | | /** |
4187 | | * xmlRegExecPushString2: |
4188 | | * @exec: a regexp execution context or NULL to indicate the end |
4189 | | * @value: the first string token input |
4190 | | * @value2: the second string token input |
4191 | | * @data: data associated to the token to reuse in callbacks |
4192 | | * |
4193 | | * Push one input token in the execution context |
4194 | | * |
4195 | | * Returns: 1 if the regexp reached a final state, 0 if non-final, and |
4196 | | * a negative value in case of error. |
4197 | | */ |
4198 | | int |
4199 | | xmlRegExecPushString2(xmlRegExecCtxtPtr exec, const xmlChar *value, |
4200 | 0 | const xmlChar *value2, void *data) { |
4201 | 0 | xmlChar buf[150]; |
4202 | 0 | int lenn, lenp, ret; |
4203 | 0 | xmlChar *str; |
4204 | |
|
4205 | 0 | if (exec == NULL) |
4206 | 0 | return(-1); |
4207 | 0 | if (exec->comp == NULL) |
4208 | 0 | return(-1); |
4209 | 0 | if (exec->status != XML_REGEXP_OK) |
4210 | 0 | return(exec->status); |
4211 | | |
4212 | 0 | if (value2 == NULL) |
4213 | 0 | return(xmlRegExecPushString(exec, value, data)); |
4214 | | |
4215 | 0 | lenn = strlen((char *) value2); |
4216 | 0 | lenp = strlen((char *) value); |
4217 | |
|
4218 | 0 | if (150 < lenn + lenp + 2) { |
4219 | 0 | str = xmlMalloc(lenn + lenp + 2); |
4220 | 0 | if (str == NULL) { |
4221 | 0 | exec->status = XML_REGEXP_OUT_OF_MEMORY; |
4222 | 0 | return(-1); |
4223 | 0 | } |
4224 | 0 | } else { |
4225 | 0 | str = buf; |
4226 | 0 | } |
4227 | 0 | memcpy(&str[0], value, lenp); |
4228 | 0 | str[lenp] = XML_REG_STRING_SEPARATOR; |
4229 | 0 | memcpy(&str[lenp + 1], value2, lenn); |
4230 | 0 | str[lenn + lenp + 1] = 0; |
4231 | |
|
4232 | 0 | if (exec->comp->compact != NULL) |
4233 | 0 | ret = xmlRegCompactPushString(exec, exec->comp, str, data); |
4234 | 0 | else |
4235 | 0 | ret = xmlRegExecPushStringInternal(exec, str, data, 1); |
4236 | |
|
4237 | 0 | if (str != buf) |
4238 | 0 | xmlFree(str); |
4239 | 0 | return(ret); |
4240 | 0 | } |
4241 | | |
4242 | | /** |
4243 | | * xmlRegExecGetValues: |
4244 | | * @exec: a regexp execution context |
4245 | | * @err: error extraction or normal one |
4246 | | * @nbval: pointer to the number of accepted values IN/OUT |
4247 | | * @nbneg: return number of negative transitions |
4248 | | * @values: pointer to the array of acceptable values |
4249 | | * @terminal: return value if this was a terminal state |
4250 | | * |
4251 | | * Extract information from the regexp execution, internal routine to |
4252 | | * implement xmlRegExecNextValues() and xmlRegExecErrInfo() |
4253 | | * |
4254 | | * Returns: 0 in case of success or -1 in case of error. |
4255 | | */ |
4256 | | static int |
4257 | | xmlRegExecGetValues(xmlRegExecCtxtPtr exec, int err, |
4258 | | int *nbval, int *nbneg, |
4259 | 0 | xmlChar **values, int *terminal) { |
4260 | 0 | int maxval; |
4261 | 0 | int nb = 0; |
4262 | |
|
4263 | 0 | if ((exec == NULL) || (nbval == NULL) || (nbneg == NULL) || |
4264 | 0 | (values == NULL) || (*nbval <= 0)) |
4265 | 0 | return(-1); |
4266 | | |
4267 | 0 | maxval = *nbval; |
4268 | 0 | *nbval = 0; |
4269 | 0 | *nbneg = 0; |
4270 | 0 | if ((exec->comp != NULL) && (exec->comp->compact != NULL)) { |
4271 | 0 | xmlRegexpPtr comp; |
4272 | 0 | int target, i, state; |
4273 | |
|
4274 | 0 | comp = exec->comp; |
4275 | |
|
4276 | 0 | if (err) { |
4277 | 0 | if (exec->errStateNo == -1) return(-1); |
4278 | 0 | state = exec->errStateNo; |
4279 | 0 | } else { |
4280 | 0 | state = exec->index; |
4281 | 0 | } |
4282 | 0 | if (terminal != NULL) { |
4283 | 0 | if (comp->compact[state * (comp->nbstrings + 1)] == |
4284 | 0 | XML_REGEXP_FINAL_STATE) |
4285 | 0 | *terminal = 1; |
4286 | 0 | else |
4287 | 0 | *terminal = 0; |
4288 | 0 | } |
4289 | 0 | for (i = 0;(i < comp->nbstrings) && (nb < maxval);i++) { |
4290 | 0 | target = comp->compact[state * (comp->nbstrings + 1) + i + 1]; |
4291 | 0 | if ((target > 0) && (target <= comp->nbstates) && |
4292 | 0 | (comp->compact[(target - 1) * (comp->nbstrings + 1)] != |
4293 | 0 | XML_REGEXP_SINK_STATE)) { |
4294 | 0 | values[nb++] = comp->stringMap[i]; |
4295 | 0 | (*nbval)++; |
4296 | 0 | } |
4297 | 0 | } |
4298 | 0 | for (i = 0;(i < comp->nbstrings) && (nb < maxval);i++) { |
4299 | 0 | target = comp->compact[state * (comp->nbstrings + 1) + i + 1]; |
4300 | 0 | if ((target > 0) && (target <= comp->nbstates) && |
4301 | 0 | (comp->compact[(target - 1) * (comp->nbstrings + 1)] == |
4302 | 0 | XML_REGEXP_SINK_STATE)) { |
4303 | 0 | values[nb++] = comp->stringMap[i]; |
4304 | 0 | (*nbneg)++; |
4305 | 0 | } |
4306 | 0 | } |
4307 | 0 | } else { |
4308 | 0 | int transno; |
4309 | 0 | xmlRegTransPtr trans; |
4310 | 0 | xmlRegAtomPtr atom; |
4311 | 0 | xmlRegStatePtr state; |
4312 | |
|
4313 | 0 | if (terminal != NULL) { |
4314 | 0 | if (exec->state->type == XML_REGEXP_FINAL_STATE) |
4315 | 0 | *terminal = 1; |
4316 | 0 | else |
4317 | 0 | *terminal = 0; |
4318 | 0 | } |
4319 | |
|
4320 | 0 | if (err) { |
4321 | 0 | if (exec->errState == NULL) return(-1); |
4322 | 0 | state = exec->errState; |
4323 | 0 | } else { |
4324 | 0 | if (exec->state == NULL) return(-1); |
4325 | 0 | state = exec->state; |
4326 | 0 | } |
4327 | 0 | for (transno = 0; |
4328 | 0 | (transno < state->nbTrans) && (nb < maxval); |
4329 | 0 | transno++) { |
4330 | 0 | trans = &state->trans[transno]; |
4331 | 0 | if (trans->to < 0) |
4332 | 0 | continue; |
4333 | 0 | atom = trans->atom; |
4334 | 0 | if ((atom == NULL) || (atom->valuep == NULL)) |
4335 | 0 | continue; |
4336 | 0 | if (trans->count == REGEXP_ALL_LAX_COUNTER) { |
4337 | | /* this should not be reached but ... */ |
4338 | 0 | } else if (trans->count == REGEXP_ALL_COUNTER) { |
4339 | | /* this should not be reached but ... */ |
4340 | 0 | } else if (trans->counter >= 0) { |
4341 | 0 | xmlRegCounterPtr counter = NULL; |
4342 | 0 | int count; |
4343 | |
|
4344 | 0 | if (err) |
4345 | 0 | count = exec->errCounts[trans->counter]; |
4346 | 0 | else |
4347 | 0 | count = exec->counts[trans->counter]; |
4348 | 0 | if (exec->comp != NULL) |
4349 | 0 | counter = &exec->comp->counters[trans->counter]; |
4350 | 0 | if ((counter == NULL) || (count < counter->max)) { |
4351 | 0 | if (atom->neg) |
4352 | 0 | values[nb++] = (xmlChar *) atom->valuep2; |
4353 | 0 | else |
4354 | 0 | values[nb++] = (xmlChar *) atom->valuep; |
4355 | 0 | (*nbval)++; |
4356 | 0 | } |
4357 | 0 | } else { |
4358 | 0 | if ((exec->comp != NULL) && (exec->comp->states[trans->to] != NULL) && |
4359 | 0 | (exec->comp->states[trans->to]->type != |
4360 | 0 | XML_REGEXP_SINK_STATE)) { |
4361 | 0 | if (atom->neg) |
4362 | 0 | values[nb++] = (xmlChar *) atom->valuep2; |
4363 | 0 | else |
4364 | 0 | values[nb++] = (xmlChar *) atom->valuep; |
4365 | 0 | (*nbval)++; |
4366 | 0 | } |
4367 | 0 | } |
4368 | 0 | } |
4369 | 0 | for (transno = 0; |
4370 | 0 | (transno < state->nbTrans) && (nb < maxval); |
4371 | 0 | transno++) { |
4372 | 0 | trans = &state->trans[transno]; |
4373 | 0 | if (trans->to < 0) |
4374 | 0 | continue; |
4375 | 0 | atom = trans->atom; |
4376 | 0 | if ((atom == NULL) || (atom->valuep == NULL)) |
4377 | 0 | continue; |
4378 | 0 | if (trans->count == REGEXP_ALL_LAX_COUNTER) { |
4379 | 0 | continue; |
4380 | 0 | } else if (trans->count == REGEXP_ALL_COUNTER) { |
4381 | 0 | continue; |
4382 | 0 | } else if (trans->counter >= 0) { |
4383 | 0 | continue; |
4384 | 0 | } else { |
4385 | 0 | if ((exec->comp->states[trans->to] != NULL) && |
4386 | 0 | (exec->comp->states[trans->to]->type == |
4387 | 0 | XML_REGEXP_SINK_STATE)) { |
4388 | 0 | if (atom->neg) |
4389 | 0 | values[nb++] = (xmlChar *) atom->valuep2; |
4390 | 0 | else |
4391 | 0 | values[nb++] = (xmlChar *) atom->valuep; |
4392 | 0 | (*nbneg)++; |
4393 | 0 | } |
4394 | 0 | } |
4395 | 0 | } |
4396 | 0 | } |
4397 | 0 | return(0); |
4398 | 0 | } |
4399 | | |
4400 | | /** |
4401 | | * xmlRegExecNextValues: |
4402 | | * @exec: a regexp execution context |
4403 | | * @nbval: pointer to the number of accepted values IN/OUT |
4404 | | * @nbneg: return number of negative transitions |
4405 | | * @values: pointer to the array of acceptable values |
4406 | | * @terminal: return value if this was a terminal state |
4407 | | * |
4408 | | * Extract information from the regexp execution, |
4409 | | * the parameter @values must point to an array of @nbval string pointers |
4410 | | * on return nbval will contain the number of possible strings in that |
4411 | | * state and the @values array will be updated with them. The string values |
4412 | | * returned will be freed with the @exec context and don't need to be |
4413 | | * deallocated. |
4414 | | * |
4415 | | * Returns: 0 in case of success or -1 in case of error. |
4416 | | */ |
4417 | | int |
4418 | | xmlRegExecNextValues(xmlRegExecCtxtPtr exec, int *nbval, int *nbneg, |
4419 | 0 | xmlChar **values, int *terminal) { |
4420 | 0 | return(xmlRegExecGetValues(exec, 0, nbval, nbneg, values, terminal)); |
4421 | 0 | } |
4422 | | |
4423 | | /** |
4424 | | * xmlRegExecErrInfo: |
4425 | | * @exec: a regexp execution context generating an error |
4426 | | * @string: return value for the error string |
4427 | | * @nbval: pointer to the number of accepted values IN/OUT |
4428 | | * @nbneg: return number of negative transitions |
4429 | | * @values: pointer to the array of acceptable values |
4430 | | * @terminal: return value if this was a terminal state |
4431 | | * |
4432 | | * Extract error information from the regexp execution, the parameter |
4433 | | * @string will be updated with the value pushed and not accepted, |
4434 | | * the parameter @values must point to an array of @nbval string pointers |
4435 | | * on return nbval will contain the number of possible strings in that |
4436 | | * state and the @values array will be updated with them. The string values |
4437 | | * returned will be freed with the @exec context and don't need to be |
4438 | | * deallocated. |
4439 | | * |
4440 | | * Returns: 0 in case of success or -1 in case of error. |
4441 | | */ |
4442 | | int |
4443 | | xmlRegExecErrInfo(xmlRegExecCtxtPtr exec, const xmlChar **string, |
4444 | 0 | int *nbval, int *nbneg, xmlChar **values, int *terminal) { |
4445 | 0 | if (exec == NULL) |
4446 | 0 | return(-1); |
4447 | 0 | if (string != NULL) { |
4448 | 0 | if (exec->status != XML_REGEXP_OK) |
4449 | 0 | *string = exec->errString; |
4450 | 0 | else |
4451 | 0 | *string = NULL; |
4452 | 0 | } |
4453 | 0 | return(xmlRegExecGetValues(exec, 1, nbval, nbneg, values, terminal)); |
4454 | 0 | } |
4455 | | |
4456 | | /************************************************************************ |
4457 | | * * |
4458 | | * Parser for the Schemas Datatype Regular Expressions * |
4459 | | * http://www.w3.org/TR/2001/REC-xmlschema-2-20010502/#regexs * |
4460 | | * * |
4461 | | ************************************************************************/ |
4462 | | |
4463 | | /** |
4464 | | * xmlFAIsChar: |
4465 | | * @ctxt: a regexp parser context |
4466 | | * |
4467 | | * [10] Char ::= [^.\?*+()|#x5B#x5D] |
4468 | | */ |
4469 | | static int |
4470 | 0 | xmlFAIsChar(xmlRegParserCtxtPtr ctxt) { |
4471 | 0 | int cur; |
4472 | 0 | int len; |
4473 | |
|
4474 | 0 | len = 4; |
4475 | 0 | cur = xmlGetUTF8Char(ctxt->cur, &len); |
4476 | 0 | if (cur < 0) { |
4477 | 0 | ERROR("Invalid UTF-8"); |
4478 | 0 | return(0); |
4479 | 0 | } |
4480 | 0 | if ((cur == '.') || (cur == '\\') || (cur == '?') || |
4481 | 0 | (cur == '*') || (cur == '+') || (cur == '(') || |
4482 | 0 | (cur == ')') || (cur == '|') || (cur == 0x5B) || |
4483 | 0 | (cur == 0x5D) || (cur == 0)) |
4484 | 0 | return(-1); |
4485 | 0 | return(cur); |
4486 | 0 | } |
4487 | | |
4488 | | /** |
4489 | | * xmlFAParseCharProp: |
4490 | | * @ctxt: a regexp parser context |
4491 | | * |
4492 | | * [27] charProp ::= IsCategory | IsBlock |
4493 | | * [28] IsCategory ::= Letters | Marks | Numbers | Punctuation | |
4494 | | * Separators | Symbols | Others |
4495 | | * [29] Letters ::= 'L' [ultmo]? |
4496 | | * [30] Marks ::= 'M' [nce]? |
4497 | | * [31] Numbers ::= 'N' [dlo]? |
4498 | | * [32] Punctuation ::= 'P' [cdseifo]? |
4499 | | * [33] Separators ::= 'Z' [slp]? |
4500 | | * [34] Symbols ::= 'S' [mcko]? |
4501 | | * [35] Others ::= 'C' [cfon]? |
4502 | | * [36] IsBlock ::= 'Is' [a-zA-Z0-9#x2D]+ |
4503 | | */ |
4504 | | static void |
4505 | 0 | xmlFAParseCharProp(xmlRegParserCtxtPtr ctxt) { |
4506 | 0 | int cur; |
4507 | 0 | xmlRegAtomType type = (xmlRegAtomType) 0; |
4508 | 0 | xmlChar *blockName = NULL; |
4509 | |
|
4510 | 0 | cur = CUR; |
4511 | 0 | if (cur == 'L') { |
4512 | 0 | NEXT; |
4513 | 0 | cur = CUR; |
4514 | 0 | if (cur == 'u') { |
4515 | 0 | NEXT; |
4516 | 0 | type = XML_REGEXP_LETTER_UPPERCASE; |
4517 | 0 | } else if (cur == 'l') { |
4518 | 0 | NEXT; |
4519 | 0 | type = XML_REGEXP_LETTER_LOWERCASE; |
4520 | 0 | } else if (cur == 't') { |
4521 | 0 | NEXT; |
4522 | 0 | type = XML_REGEXP_LETTER_TITLECASE; |
4523 | 0 | } else if (cur == 'm') { |
4524 | 0 | NEXT; |
4525 | 0 | type = XML_REGEXP_LETTER_MODIFIER; |
4526 | 0 | } else if (cur == 'o') { |
4527 | 0 | NEXT; |
4528 | 0 | type = XML_REGEXP_LETTER_OTHERS; |
4529 | 0 | } else { |
4530 | 0 | type = XML_REGEXP_LETTER; |
4531 | 0 | } |
4532 | 0 | } else if (cur == 'M') { |
4533 | 0 | NEXT; |
4534 | 0 | cur = CUR; |
4535 | 0 | if (cur == 'n') { |
4536 | 0 | NEXT; |
4537 | | /* nonspacing */ |
4538 | 0 | type = XML_REGEXP_MARK_NONSPACING; |
4539 | 0 | } else if (cur == 'c') { |
4540 | 0 | NEXT; |
4541 | | /* spacing combining */ |
4542 | 0 | type = XML_REGEXP_MARK_SPACECOMBINING; |
4543 | 0 | } else if (cur == 'e') { |
4544 | 0 | NEXT; |
4545 | | /* enclosing */ |
4546 | 0 | type = XML_REGEXP_MARK_ENCLOSING; |
4547 | 0 | } else { |
4548 | | /* all marks */ |
4549 | 0 | type = XML_REGEXP_MARK; |
4550 | 0 | } |
4551 | 0 | } else if (cur == 'N') { |
4552 | 0 | NEXT; |
4553 | 0 | cur = CUR; |
4554 | 0 | if (cur == 'd') { |
4555 | 0 | NEXT; |
4556 | | /* digital */ |
4557 | 0 | type = XML_REGEXP_NUMBER_DECIMAL; |
4558 | 0 | } else if (cur == 'l') { |
4559 | 0 | NEXT; |
4560 | | /* letter */ |
4561 | 0 | type = XML_REGEXP_NUMBER_LETTER; |
4562 | 0 | } else if (cur == 'o') { |
4563 | 0 | NEXT; |
4564 | | /* other */ |
4565 | 0 | type = XML_REGEXP_NUMBER_OTHERS; |
4566 | 0 | } else { |
4567 | | /* all numbers */ |
4568 | 0 | type = XML_REGEXP_NUMBER; |
4569 | 0 | } |
4570 | 0 | } else if (cur == 'P') { |
4571 | 0 | NEXT; |
4572 | 0 | cur = CUR; |
4573 | 0 | if (cur == 'c') { |
4574 | 0 | NEXT; |
4575 | | /* connector */ |
4576 | 0 | type = XML_REGEXP_PUNCT_CONNECTOR; |
4577 | 0 | } else if (cur == 'd') { |
4578 | 0 | NEXT; |
4579 | | /* dash */ |
4580 | 0 | type = XML_REGEXP_PUNCT_DASH; |
4581 | 0 | } else if (cur == 's') { |
4582 | 0 | NEXT; |
4583 | | /* open */ |
4584 | 0 | type = XML_REGEXP_PUNCT_OPEN; |
4585 | 0 | } else if (cur == 'e') { |
4586 | 0 | NEXT; |
4587 | | /* close */ |
4588 | 0 | type = XML_REGEXP_PUNCT_CLOSE; |
4589 | 0 | } else if (cur == 'i') { |
4590 | 0 | NEXT; |
4591 | | /* initial quote */ |
4592 | 0 | type = XML_REGEXP_PUNCT_INITQUOTE; |
4593 | 0 | } else if (cur == 'f') { |
4594 | 0 | NEXT; |
4595 | | /* final quote */ |
4596 | 0 | type = XML_REGEXP_PUNCT_FINQUOTE; |
4597 | 0 | } else if (cur == 'o') { |
4598 | 0 | NEXT; |
4599 | | /* other */ |
4600 | 0 | type = XML_REGEXP_PUNCT_OTHERS; |
4601 | 0 | } else { |
4602 | | /* all punctuation */ |
4603 | 0 | type = XML_REGEXP_PUNCT; |
4604 | 0 | } |
4605 | 0 | } else if (cur == 'Z') { |
4606 | 0 | NEXT; |
4607 | 0 | cur = CUR; |
4608 | 0 | if (cur == 's') { |
4609 | 0 | NEXT; |
4610 | | /* space */ |
4611 | 0 | type = XML_REGEXP_SEPAR_SPACE; |
4612 | 0 | } else if (cur == 'l') { |
4613 | 0 | NEXT; |
4614 | | /* line */ |
4615 | 0 | type = XML_REGEXP_SEPAR_LINE; |
4616 | 0 | } else if (cur == 'p') { |
4617 | 0 | NEXT; |
4618 | | /* paragraph */ |
4619 | 0 | type = XML_REGEXP_SEPAR_PARA; |
4620 | 0 | } else { |
4621 | | /* all separators */ |
4622 | 0 | type = XML_REGEXP_SEPAR; |
4623 | 0 | } |
4624 | 0 | } else if (cur == 'S') { |
4625 | 0 | NEXT; |
4626 | 0 | cur = CUR; |
4627 | 0 | if (cur == 'm') { |
4628 | 0 | NEXT; |
4629 | 0 | type = XML_REGEXP_SYMBOL_MATH; |
4630 | | /* math */ |
4631 | 0 | } else if (cur == 'c') { |
4632 | 0 | NEXT; |
4633 | 0 | type = XML_REGEXP_SYMBOL_CURRENCY; |
4634 | | /* currency */ |
4635 | 0 | } else if (cur == 'k') { |
4636 | 0 | NEXT; |
4637 | 0 | type = XML_REGEXP_SYMBOL_MODIFIER; |
4638 | | /* modifiers */ |
4639 | 0 | } else if (cur == 'o') { |
4640 | 0 | NEXT; |
4641 | 0 | type = XML_REGEXP_SYMBOL_OTHERS; |
4642 | | /* other */ |
4643 | 0 | } else { |
4644 | | /* all symbols */ |
4645 | 0 | type = XML_REGEXP_SYMBOL; |
4646 | 0 | } |
4647 | 0 | } else if (cur == 'C') { |
4648 | 0 | NEXT; |
4649 | 0 | cur = CUR; |
4650 | 0 | if (cur == 'c') { |
4651 | 0 | NEXT; |
4652 | | /* control */ |
4653 | 0 | type = XML_REGEXP_OTHER_CONTROL; |
4654 | 0 | } else if (cur == 'f') { |
4655 | 0 | NEXT; |
4656 | | /* format */ |
4657 | 0 | type = XML_REGEXP_OTHER_FORMAT; |
4658 | 0 | } else if (cur == 'o') { |
4659 | 0 | NEXT; |
4660 | | /* private use */ |
4661 | 0 | type = XML_REGEXP_OTHER_PRIVATE; |
4662 | 0 | } else if (cur == 'n') { |
4663 | 0 | NEXT; |
4664 | | /* not assigned */ |
4665 | 0 | type = XML_REGEXP_OTHER_NA; |
4666 | 0 | } else { |
4667 | | /* all others */ |
4668 | 0 | type = XML_REGEXP_OTHER; |
4669 | 0 | } |
4670 | 0 | } else if (cur == 'I') { |
4671 | 0 | const xmlChar *start; |
4672 | 0 | NEXT; |
4673 | 0 | cur = CUR; |
4674 | 0 | if (cur != 's') { |
4675 | 0 | ERROR("IsXXXX expected"); |
4676 | 0 | return; |
4677 | 0 | } |
4678 | 0 | NEXT; |
4679 | 0 | start = ctxt->cur; |
4680 | 0 | cur = CUR; |
4681 | 0 | if (((cur >= 'a') && (cur <= 'z')) || |
4682 | 0 | ((cur >= 'A') && (cur <= 'Z')) || |
4683 | 0 | ((cur >= '0') && (cur <= '9')) || |
4684 | 0 | (cur == 0x2D)) { |
4685 | 0 | NEXT; |
4686 | 0 | cur = CUR; |
4687 | 0 | while (((cur >= 'a') && (cur <= 'z')) || |
4688 | 0 | ((cur >= 'A') && (cur <= 'Z')) || |
4689 | 0 | ((cur >= '0') && (cur <= '9')) || |
4690 | 0 | (cur == 0x2D)) { |
4691 | 0 | NEXT; |
4692 | 0 | cur = CUR; |
4693 | 0 | } |
4694 | 0 | } |
4695 | 0 | type = XML_REGEXP_BLOCK_NAME; |
4696 | 0 | blockName = xmlStrndup(start, ctxt->cur - start); |
4697 | 0 | if (blockName == NULL) |
4698 | 0 | xmlRegexpErrMemory(ctxt); |
4699 | 0 | } else { |
4700 | 0 | ERROR("Unknown char property"); |
4701 | 0 | return; |
4702 | 0 | } |
4703 | 0 | if (ctxt->atom == NULL) { |
4704 | 0 | ctxt->atom = xmlRegNewAtom(ctxt, type); |
4705 | 0 | if (ctxt->atom == NULL) { |
4706 | 0 | xmlFree(blockName); |
4707 | 0 | return; |
4708 | 0 | } |
4709 | 0 | ctxt->atom->valuep = blockName; |
4710 | 0 | } else if (ctxt->atom->type == XML_REGEXP_RANGES) { |
4711 | 0 | if (xmlRegAtomAddRange(ctxt, ctxt->atom, ctxt->neg, |
4712 | 0 | type, 0, 0, blockName) == NULL) { |
4713 | 0 | xmlFree(blockName); |
4714 | 0 | } |
4715 | 0 | } |
4716 | 0 | } |
4717 | | |
4718 | | static int parse_escaped_codeunit(xmlRegParserCtxtPtr ctxt) |
4719 | 0 | { |
4720 | 0 | int val = 0, i, cur; |
4721 | 0 | for (i = 0; i < 4; i++) { |
4722 | 0 | NEXT; |
4723 | 0 | val *= 16; |
4724 | 0 | cur = CUR; |
4725 | 0 | if (cur >= '0' && cur <= '9') { |
4726 | 0 | val += cur - '0'; |
4727 | 0 | } else if (cur >= 'A' && cur <= 'F') { |
4728 | 0 | val += cur - 'A' + 10; |
4729 | 0 | } else if (cur >= 'a' && cur <= 'f') { |
4730 | 0 | val += cur - 'a' + 10; |
4731 | 0 | } else { |
4732 | 0 | ERROR("Expecting hex digit"); |
4733 | 0 | return -1; |
4734 | 0 | } |
4735 | 0 | } |
4736 | 0 | return val; |
4737 | 0 | } |
4738 | | |
4739 | | static int parse_escaped_codepoint(xmlRegParserCtxtPtr ctxt) |
4740 | 0 | { |
4741 | 0 | int val = parse_escaped_codeunit(ctxt); |
4742 | 0 | if (0xD800 <= val && val <= 0xDBFF) { |
4743 | 0 | NEXT; |
4744 | 0 | if (CUR == '\\') { |
4745 | 0 | NEXT; |
4746 | 0 | if (CUR == 'u') { |
4747 | 0 | int low = parse_escaped_codeunit(ctxt); |
4748 | 0 | if (0xDC00 <= low && low <= 0xDFFF) { |
4749 | 0 | return (val - 0xD800) * 0x400 + (low - 0xDC00) + 0x10000; |
4750 | 0 | } |
4751 | 0 | } |
4752 | 0 | } |
4753 | 0 | ERROR("Invalid low surrogate pair code unit"); |
4754 | 0 | val = -1; |
4755 | 0 | } |
4756 | 0 | return val; |
4757 | 0 | } |
4758 | | |
4759 | | /** |
4760 | | * xmlFAParseCharClassEsc: |
4761 | | * @ctxt: a regexp parser context |
4762 | | * |
4763 | | * [23] charClassEsc ::= ( SingleCharEsc | MultiCharEsc | catEsc | complEsc ) |
4764 | | * [24] SingleCharEsc ::= '\' [nrt\|.?*+(){}#x2D#x5B#x5D#x5E] |
4765 | | * [25] catEsc ::= '\p{' charProp '}' |
4766 | | * [26] complEsc ::= '\P{' charProp '}' |
4767 | | * [37] MultiCharEsc ::= '.' | ('\' [sSiIcCdDwW]) |
4768 | | */ |
4769 | | static void |
4770 | 0 | xmlFAParseCharClassEsc(xmlRegParserCtxtPtr ctxt) { |
4771 | 0 | int cur; |
4772 | |
|
4773 | 0 | if (CUR == '.') { |
4774 | 0 | if (ctxt->atom == NULL) { |
4775 | 0 | ctxt->atom = xmlRegNewAtom(ctxt, XML_REGEXP_ANYCHAR); |
4776 | 0 | } else if (ctxt->atom->type == XML_REGEXP_RANGES) { |
4777 | 0 | xmlRegAtomAddRange(ctxt, ctxt->atom, ctxt->neg, |
4778 | 0 | XML_REGEXP_ANYCHAR, 0, 0, NULL); |
4779 | 0 | } |
4780 | 0 | NEXT; |
4781 | 0 | return; |
4782 | 0 | } |
4783 | 0 | if (CUR != '\\') { |
4784 | 0 | ERROR("Escaped sequence: expecting \\"); |
4785 | 0 | return; |
4786 | 0 | } |
4787 | 0 | NEXT; |
4788 | 0 | cur = CUR; |
4789 | 0 | if (cur == 'p') { |
4790 | 0 | NEXT; |
4791 | 0 | if (CUR != '{') { |
4792 | 0 | ERROR("Expecting '{'"); |
4793 | 0 | return; |
4794 | 0 | } |
4795 | 0 | NEXT; |
4796 | 0 | xmlFAParseCharProp(ctxt); |
4797 | 0 | if (CUR != '}') { |
4798 | 0 | ERROR("Expecting '}'"); |
4799 | 0 | return; |
4800 | 0 | } |
4801 | 0 | NEXT; |
4802 | 0 | } else if (cur == 'P') { |
4803 | 0 | NEXT; |
4804 | 0 | if (CUR != '{') { |
4805 | 0 | ERROR("Expecting '{'"); |
4806 | 0 | return; |
4807 | 0 | } |
4808 | 0 | NEXT; |
4809 | 0 | xmlFAParseCharProp(ctxt); |
4810 | 0 | if (ctxt->atom != NULL) |
4811 | 0 | ctxt->atom->neg = 1; |
4812 | 0 | if (CUR != '}') { |
4813 | 0 | ERROR("Expecting '}'"); |
4814 | 0 | return; |
4815 | 0 | } |
4816 | 0 | NEXT; |
4817 | 0 | } else if ((cur == 'n') || (cur == 'r') || (cur == 't') || (cur == '\\') || |
4818 | 0 | (cur == '|') || (cur == '.') || (cur == '?') || (cur == '*') || |
4819 | 0 | (cur == '+') || (cur == '(') || (cur == ')') || (cur == '{') || |
4820 | 0 | (cur == '}') || (cur == 0x2D) || (cur == 0x5B) || (cur == 0x5D) || |
4821 | 0 | (cur == 0x5E) || |
4822 | | |
4823 | | /* Non-standard escape sequences: |
4824 | | * Java 1.8|.NET Core 3.1|MSXML 6 */ |
4825 | 0 | (cur == '!') || /* + | + | + */ |
4826 | 0 | (cur == '"') || /* + | + | + */ |
4827 | 0 | (cur == '#') || /* + | + | + */ |
4828 | 0 | (cur == '$') || /* + | + | + */ |
4829 | 0 | (cur == '%') || /* + | + | + */ |
4830 | 0 | (cur == ',') || /* + | + | + */ |
4831 | 0 | (cur == '/') || /* + | + | + */ |
4832 | 0 | (cur == ':') || /* + | + | + */ |
4833 | 0 | (cur == ';') || /* + | + | + */ |
4834 | 0 | (cur == '=') || /* + | + | + */ |
4835 | 0 | (cur == '>') || /* | + | + */ |
4836 | 0 | (cur == '@') || /* + | + | + */ |
4837 | 0 | (cur == '`') || /* + | + | + */ |
4838 | 0 | (cur == '~') || /* + | + | + */ |
4839 | 0 | (cur == 'u')) { /* | + | + */ |
4840 | 0 | if (ctxt->atom == NULL) { |
4841 | 0 | ctxt->atom = xmlRegNewAtom(ctxt, XML_REGEXP_CHARVAL); |
4842 | 0 | if (ctxt->atom != NULL) { |
4843 | 0 | switch (cur) { |
4844 | 0 | case 'n': |
4845 | 0 | ctxt->atom->codepoint = '\n'; |
4846 | 0 | break; |
4847 | 0 | case 'r': |
4848 | 0 | ctxt->atom->codepoint = '\r'; |
4849 | 0 | break; |
4850 | 0 | case 't': |
4851 | 0 | ctxt->atom->codepoint = '\t'; |
4852 | 0 | break; |
4853 | 0 | case 'u': |
4854 | 0 | cur = parse_escaped_codepoint(ctxt); |
4855 | 0 | if (cur < 0) { |
4856 | 0 | return; |
4857 | 0 | } |
4858 | 0 | ctxt->atom->codepoint = cur; |
4859 | 0 | break; |
4860 | 0 | default: |
4861 | 0 | ctxt->atom->codepoint = cur; |
4862 | 0 | } |
4863 | 0 | } |
4864 | 0 | } else if (ctxt->atom->type == XML_REGEXP_RANGES) { |
4865 | 0 | switch (cur) { |
4866 | 0 | case 'n': |
4867 | 0 | cur = '\n'; |
4868 | 0 | break; |
4869 | 0 | case 'r': |
4870 | 0 | cur = '\r'; |
4871 | 0 | break; |
4872 | 0 | case 't': |
4873 | 0 | cur = '\t'; |
4874 | 0 | break; |
4875 | 0 | } |
4876 | 0 | xmlRegAtomAddRange(ctxt, ctxt->atom, ctxt->neg, |
4877 | 0 | XML_REGEXP_CHARVAL, cur, cur, NULL); |
4878 | 0 | } |
4879 | 0 | NEXT; |
4880 | 0 | } else if ((cur == 's') || (cur == 'S') || (cur == 'i') || (cur == 'I') || |
4881 | 0 | (cur == 'c') || (cur == 'C') || (cur == 'd') || (cur == 'D') || |
4882 | 0 | (cur == 'w') || (cur == 'W')) { |
4883 | 0 | xmlRegAtomType type = XML_REGEXP_ANYSPACE; |
4884 | |
|
4885 | 0 | switch (cur) { |
4886 | 0 | case 's': |
4887 | 0 | type = XML_REGEXP_ANYSPACE; |
4888 | 0 | break; |
4889 | 0 | case 'S': |
4890 | 0 | type = XML_REGEXP_NOTSPACE; |
4891 | 0 | break; |
4892 | 0 | case 'i': |
4893 | 0 | type = XML_REGEXP_INITNAME; |
4894 | 0 | break; |
4895 | 0 | case 'I': |
4896 | 0 | type = XML_REGEXP_NOTINITNAME; |
4897 | 0 | break; |
4898 | 0 | case 'c': |
4899 | 0 | type = XML_REGEXP_NAMECHAR; |
4900 | 0 | break; |
4901 | 0 | case 'C': |
4902 | 0 | type = XML_REGEXP_NOTNAMECHAR; |
4903 | 0 | break; |
4904 | 0 | case 'd': |
4905 | 0 | type = XML_REGEXP_DECIMAL; |
4906 | 0 | break; |
4907 | 0 | case 'D': |
4908 | 0 | type = XML_REGEXP_NOTDECIMAL; |
4909 | 0 | break; |
4910 | 0 | case 'w': |
4911 | 0 | type = XML_REGEXP_REALCHAR; |
4912 | 0 | break; |
4913 | 0 | case 'W': |
4914 | 0 | type = XML_REGEXP_NOTREALCHAR; |
4915 | 0 | break; |
4916 | 0 | } |
4917 | 0 | NEXT; |
4918 | 0 | if (ctxt->atom == NULL) { |
4919 | 0 | ctxt->atom = xmlRegNewAtom(ctxt, type); |
4920 | 0 | } else if (ctxt->atom->type == XML_REGEXP_RANGES) { |
4921 | 0 | xmlRegAtomAddRange(ctxt, ctxt->atom, ctxt->neg, |
4922 | 0 | type, 0, 0, NULL); |
4923 | 0 | } |
4924 | 0 | } else { |
4925 | 0 | ERROR("Wrong escape sequence, misuse of character '\\'"); |
4926 | 0 | } |
4927 | 0 | } |
4928 | | |
4929 | | /** |
4930 | | * xmlFAParseCharRange: |
4931 | | * @ctxt: a regexp parser context |
4932 | | * |
4933 | | * [17] charRange ::= seRange | XmlCharRef | XmlCharIncDash |
4934 | | * [18] seRange ::= charOrEsc '-' charOrEsc |
4935 | | * [20] charOrEsc ::= XmlChar | SingleCharEsc |
4936 | | * [21] XmlChar ::= [^\#x2D#x5B#x5D] |
4937 | | * [22] XmlCharIncDash ::= [^\#x5B#x5D] |
4938 | | */ |
4939 | | static void |
4940 | 0 | xmlFAParseCharRange(xmlRegParserCtxtPtr ctxt) { |
4941 | 0 | int cur, len; |
4942 | 0 | int start = -1; |
4943 | 0 | int end = -1; |
4944 | |
|
4945 | 0 | if (CUR == '\0') { |
4946 | 0 | ERROR("Expecting ']'"); |
4947 | 0 | return; |
4948 | 0 | } |
4949 | | |
4950 | 0 | cur = CUR; |
4951 | 0 | if (cur == '\\') { |
4952 | 0 | NEXT; |
4953 | 0 | cur = CUR; |
4954 | 0 | switch (cur) { |
4955 | 0 | case 'n': start = 0xA; break; |
4956 | 0 | case 'r': start = 0xD; break; |
4957 | 0 | case 't': start = 0x9; break; |
4958 | 0 | case '\\': case '|': case '.': case '-': case '^': case '?': |
4959 | 0 | case '*': case '+': case '{': case '}': case '(': case ')': |
4960 | 0 | case '[': case ']': |
4961 | 0 | start = cur; break; |
4962 | 0 | default: |
4963 | 0 | ERROR("Invalid escape value"); |
4964 | 0 | return; |
4965 | 0 | } |
4966 | 0 | end = start; |
4967 | 0 | len = 1; |
4968 | 0 | } else if ((cur != 0x5B) && (cur != 0x5D)) { |
4969 | 0 | len = 4; |
4970 | 0 | end = start = xmlGetUTF8Char(ctxt->cur, &len); |
4971 | 0 | if (start < 0) { |
4972 | 0 | ERROR("Invalid UTF-8"); |
4973 | 0 | return; |
4974 | 0 | } |
4975 | 0 | } else { |
4976 | 0 | ERROR("Expecting a char range"); |
4977 | 0 | return; |
4978 | 0 | } |
4979 | | /* |
4980 | | * Since we are "inside" a range, we can assume ctxt->cur is past |
4981 | | * the start of ctxt->string, and PREV should be safe |
4982 | | */ |
4983 | 0 | if ((start == '-') && (NXT(1) != ']') && (PREV != '[') && (PREV != '^')) { |
4984 | 0 | NEXTL(len); |
4985 | 0 | return; |
4986 | 0 | } |
4987 | 0 | NEXTL(len); |
4988 | 0 | cur = CUR; |
4989 | 0 | if ((cur != '-') || (NXT(1) == '[') || (NXT(1) == ']')) { |
4990 | 0 | xmlRegAtomAddRange(ctxt, ctxt->atom, ctxt->neg, |
4991 | 0 | XML_REGEXP_CHARVAL, start, end, NULL); |
4992 | 0 | return; |
4993 | 0 | } |
4994 | 0 | NEXT; |
4995 | 0 | cur = CUR; |
4996 | 0 | if (cur == '\\') { |
4997 | 0 | NEXT; |
4998 | 0 | cur = CUR; |
4999 | 0 | switch (cur) { |
5000 | 0 | case 'n': end = 0xA; break; |
5001 | 0 | case 'r': end = 0xD; break; |
5002 | 0 | case 't': end = 0x9; break; |
5003 | 0 | case '\\': case '|': case '.': case '-': case '^': case '?': |
5004 | 0 | case '*': case '+': case '{': case '}': case '(': case ')': |
5005 | 0 | case '[': case ']': |
5006 | 0 | end = cur; break; |
5007 | 0 | default: |
5008 | 0 | ERROR("Invalid escape value"); |
5009 | 0 | return; |
5010 | 0 | } |
5011 | 0 | len = 1; |
5012 | 0 | } else if ((cur != '\0') && (cur != 0x5B) && (cur != 0x5D)) { |
5013 | 0 | len = 4; |
5014 | 0 | end = xmlGetUTF8Char(ctxt->cur, &len); |
5015 | 0 | if (end < 0) { |
5016 | 0 | ERROR("Invalid UTF-8"); |
5017 | 0 | return; |
5018 | 0 | } |
5019 | 0 | } else { |
5020 | 0 | ERROR("Expecting the end of a char range"); |
5021 | 0 | return; |
5022 | 0 | } |
5023 | | |
5024 | | /* TODO check that the values are acceptable character ranges for XML */ |
5025 | 0 | if (end < start) { |
5026 | 0 | ERROR("End of range is before start of range"); |
5027 | 0 | } else { |
5028 | 0 | NEXTL(len); |
5029 | 0 | xmlRegAtomAddRange(ctxt, ctxt->atom, ctxt->neg, |
5030 | 0 | XML_REGEXP_CHARVAL, start, end, NULL); |
5031 | 0 | } |
5032 | 0 | } |
5033 | | |
5034 | | /** |
5035 | | * xmlFAParsePosCharGroup: |
5036 | | * @ctxt: a regexp parser context |
5037 | | * |
5038 | | * [14] posCharGroup ::= ( charRange | charClassEsc )+ |
5039 | | */ |
5040 | | static void |
5041 | 0 | xmlFAParsePosCharGroup(xmlRegParserCtxtPtr ctxt) { |
5042 | 0 | do { |
5043 | 0 | if (CUR == '\\') { |
5044 | 0 | xmlFAParseCharClassEsc(ctxt); |
5045 | 0 | } else { |
5046 | 0 | xmlFAParseCharRange(ctxt); |
5047 | 0 | } |
5048 | 0 | } while ((CUR != ']') && (CUR != '-') && |
5049 | 0 | (CUR != 0) && (ctxt->error == 0)); |
5050 | 0 | } |
5051 | | |
5052 | | /** |
5053 | | * xmlFAParseCharGroup: |
5054 | | * @ctxt: a regexp parser context |
5055 | | * |
5056 | | * [13] charGroup ::= posCharGroup | negCharGroup | charClassSub |
5057 | | * [15] negCharGroup ::= '^' posCharGroup |
5058 | | * [16] charClassSub ::= ( posCharGroup | negCharGroup ) '-' charClassExpr |
5059 | | * [12] charClassExpr ::= '[' charGroup ']' |
5060 | | */ |
5061 | | static void |
5062 | 0 | xmlFAParseCharGroup(xmlRegParserCtxtPtr ctxt) { |
5063 | 0 | int neg = ctxt->neg; |
5064 | |
|
5065 | 0 | if (CUR == '^') { |
5066 | 0 | NEXT; |
5067 | 0 | ctxt->neg = !ctxt->neg; |
5068 | 0 | xmlFAParsePosCharGroup(ctxt); |
5069 | 0 | ctxt->neg = neg; |
5070 | 0 | } |
5071 | 0 | while ((CUR != ']') && (ctxt->error == 0)) { |
5072 | 0 | if ((CUR == '-') && (NXT(1) == '[')) { |
5073 | 0 | NEXT; /* eat the '-' */ |
5074 | 0 | NEXT; /* eat the '[' */ |
5075 | 0 | ctxt->neg = 2; |
5076 | 0 | xmlFAParseCharGroup(ctxt); |
5077 | 0 | ctxt->neg = neg; |
5078 | 0 | if (CUR == ']') { |
5079 | 0 | NEXT; |
5080 | 0 | } else { |
5081 | 0 | ERROR("charClassExpr: ']' expected"); |
5082 | 0 | } |
5083 | 0 | break; |
5084 | 0 | } else { |
5085 | 0 | xmlFAParsePosCharGroup(ctxt); |
5086 | 0 | } |
5087 | 0 | } |
5088 | 0 | } |
5089 | | |
5090 | | /** |
5091 | | * xmlFAParseCharClass: |
5092 | | * @ctxt: a regexp parser context |
5093 | | * |
5094 | | * [11] charClass ::= charClassEsc | charClassExpr |
5095 | | * [12] charClassExpr ::= '[' charGroup ']' |
5096 | | */ |
5097 | | static void |
5098 | 0 | xmlFAParseCharClass(xmlRegParserCtxtPtr ctxt) { |
5099 | 0 | if (CUR == '[') { |
5100 | 0 | NEXT; |
5101 | 0 | ctxt->atom = xmlRegNewAtom(ctxt, XML_REGEXP_RANGES); |
5102 | 0 | if (ctxt->atom == NULL) |
5103 | 0 | return; |
5104 | 0 | xmlFAParseCharGroup(ctxt); |
5105 | 0 | if (CUR == ']') { |
5106 | 0 | NEXT; |
5107 | 0 | } else { |
5108 | 0 | ERROR("xmlFAParseCharClass: ']' expected"); |
5109 | 0 | } |
5110 | 0 | } else { |
5111 | 0 | xmlFAParseCharClassEsc(ctxt); |
5112 | 0 | } |
5113 | 0 | } |
5114 | | |
5115 | | /** |
5116 | | * xmlFAParseQuantExact: |
5117 | | * @ctxt: a regexp parser context |
5118 | | * |
5119 | | * [8] QuantExact ::= [0-9]+ |
5120 | | * |
5121 | | * Returns 0 if success or -1 in case of error |
5122 | | */ |
5123 | | static int |
5124 | 0 | xmlFAParseQuantExact(xmlRegParserCtxtPtr ctxt) { |
5125 | 0 | int ret = 0; |
5126 | 0 | int ok = 0; |
5127 | 0 | int overflow = 0; |
5128 | |
|
5129 | 0 | while ((CUR >= '0') && (CUR <= '9')) { |
5130 | 0 | if (ret > INT_MAX / 10) { |
5131 | 0 | overflow = 1; |
5132 | 0 | } else { |
5133 | 0 | int digit = CUR - '0'; |
5134 | |
|
5135 | 0 | ret *= 10; |
5136 | 0 | if (ret > INT_MAX - digit) |
5137 | 0 | overflow = 1; |
5138 | 0 | else |
5139 | 0 | ret += digit; |
5140 | 0 | } |
5141 | 0 | ok = 1; |
5142 | 0 | NEXT; |
5143 | 0 | } |
5144 | 0 | if ((ok != 1) || (overflow == 1)) { |
5145 | 0 | return(-1); |
5146 | 0 | } |
5147 | 0 | return(ret); |
5148 | 0 | } |
5149 | | |
5150 | | /** |
5151 | | * xmlFAParseQuantifier: |
5152 | | * @ctxt: a regexp parser context |
5153 | | * |
5154 | | * [4] quantifier ::= [?*+] | ( '{' quantity '}' ) |
5155 | | * [5] quantity ::= quantRange | quantMin | QuantExact |
5156 | | * [6] quantRange ::= QuantExact ',' QuantExact |
5157 | | * [7] quantMin ::= QuantExact ',' |
5158 | | * [8] QuantExact ::= [0-9]+ |
5159 | | */ |
5160 | | static int |
5161 | 0 | xmlFAParseQuantifier(xmlRegParserCtxtPtr ctxt) { |
5162 | 0 | int cur; |
5163 | |
|
5164 | 0 | cur = CUR; |
5165 | 0 | if ((cur == '?') || (cur == '*') || (cur == '+')) { |
5166 | 0 | if (ctxt->atom != NULL) { |
5167 | 0 | if (cur == '?') |
5168 | 0 | ctxt->atom->quant = XML_REGEXP_QUANT_OPT; |
5169 | 0 | else if (cur == '*') |
5170 | 0 | ctxt->atom->quant = XML_REGEXP_QUANT_MULT; |
5171 | 0 | else if (cur == '+') |
5172 | 0 | ctxt->atom->quant = XML_REGEXP_QUANT_PLUS; |
5173 | 0 | } |
5174 | 0 | NEXT; |
5175 | 0 | return(1); |
5176 | 0 | } |
5177 | 0 | if (cur == '{') { |
5178 | 0 | int min = 0, max = 0; |
5179 | |
|
5180 | 0 | NEXT; |
5181 | 0 | cur = xmlFAParseQuantExact(ctxt); |
5182 | 0 | if (cur >= 0) |
5183 | 0 | min = cur; |
5184 | 0 | else { |
5185 | 0 | ERROR("Improper quantifier"); |
5186 | 0 | } |
5187 | 0 | if (CUR == ',') { |
5188 | 0 | NEXT; |
5189 | 0 | if (CUR == '}') |
5190 | 0 | max = INT_MAX; |
5191 | 0 | else { |
5192 | 0 | cur = xmlFAParseQuantExact(ctxt); |
5193 | 0 | if (cur >= 0) |
5194 | 0 | max = cur; |
5195 | 0 | else { |
5196 | 0 | ERROR("Improper quantifier"); |
5197 | 0 | } |
5198 | 0 | } |
5199 | 0 | } |
5200 | 0 | if (CUR == '}') { |
5201 | 0 | NEXT; |
5202 | 0 | } else { |
5203 | 0 | ERROR("Unterminated quantifier"); |
5204 | 0 | } |
5205 | 0 | if (max == 0) |
5206 | 0 | max = min; |
5207 | 0 | if (ctxt->atom != NULL) { |
5208 | 0 | ctxt->atom->quant = XML_REGEXP_QUANT_RANGE; |
5209 | 0 | ctxt->atom->min = min; |
5210 | 0 | ctxt->atom->max = max; |
5211 | 0 | } |
5212 | 0 | return(1); |
5213 | 0 | } |
5214 | 0 | return(0); |
5215 | 0 | } |
5216 | | |
5217 | | /** |
5218 | | * xmlFAParseAtom: |
5219 | | * @ctxt: a regexp parser context |
5220 | | * |
5221 | | * [9] atom ::= Char | charClass | ( '(' regExp ')' ) |
5222 | | */ |
5223 | | static int |
5224 | 0 | xmlFAParseAtom(xmlRegParserCtxtPtr ctxt) { |
5225 | 0 | int codepoint, len; |
5226 | |
|
5227 | 0 | codepoint = xmlFAIsChar(ctxt); |
5228 | 0 | if (codepoint > 0) { |
5229 | 0 | ctxt->atom = xmlRegNewAtom(ctxt, XML_REGEXP_CHARVAL); |
5230 | 0 | if (ctxt->atom == NULL) |
5231 | 0 | return(-1); |
5232 | 0 | len = 4; |
5233 | 0 | codepoint = xmlGetUTF8Char(ctxt->cur, &len); |
5234 | 0 | if (codepoint < 0) { |
5235 | 0 | ERROR("Invalid UTF-8"); |
5236 | 0 | return(-1); |
5237 | 0 | } |
5238 | 0 | ctxt->atom->codepoint = codepoint; |
5239 | 0 | NEXTL(len); |
5240 | 0 | return(1); |
5241 | 0 | } else if (CUR == '|') { |
5242 | 0 | return(0); |
5243 | 0 | } else if (CUR == 0) { |
5244 | 0 | return(0); |
5245 | 0 | } else if (CUR == ')') { |
5246 | 0 | return(0); |
5247 | 0 | } else if (CUR == '(') { |
5248 | 0 | xmlRegStatePtr start, oldend, start0; |
5249 | |
|
5250 | 0 | NEXT; |
5251 | 0 | if (ctxt->depth >= 50) { |
5252 | 0 | ERROR("xmlFAParseAtom: maximum nesting depth exceeded"); |
5253 | 0 | return(-1); |
5254 | 0 | } |
5255 | | /* |
5256 | | * this extra Epsilon transition is needed if we count with 0 allowed |
5257 | | * unfortunately this can't be known at that point |
5258 | | */ |
5259 | 0 | xmlFAGenerateEpsilonTransition(ctxt, ctxt->state, NULL); |
5260 | 0 | start0 = ctxt->state; |
5261 | 0 | xmlFAGenerateEpsilonTransition(ctxt, ctxt->state, NULL); |
5262 | 0 | start = ctxt->state; |
5263 | 0 | oldend = ctxt->end; |
5264 | 0 | ctxt->end = NULL; |
5265 | 0 | ctxt->atom = NULL; |
5266 | 0 | ctxt->depth++; |
5267 | 0 | xmlFAParseRegExp(ctxt, 0); |
5268 | 0 | ctxt->depth--; |
5269 | 0 | if (CUR == ')') { |
5270 | 0 | NEXT; |
5271 | 0 | } else { |
5272 | 0 | ERROR("xmlFAParseAtom: expecting ')'"); |
5273 | 0 | } |
5274 | 0 | ctxt->atom = xmlRegNewAtom(ctxt, XML_REGEXP_SUBREG); |
5275 | 0 | if (ctxt->atom == NULL) |
5276 | 0 | return(-1); |
5277 | 0 | ctxt->atom->start = start; |
5278 | 0 | ctxt->atom->start0 = start0; |
5279 | 0 | ctxt->atom->stop = ctxt->state; |
5280 | 0 | ctxt->end = oldend; |
5281 | 0 | return(1); |
5282 | 0 | } else if ((CUR == '[') || (CUR == '\\') || (CUR == '.')) { |
5283 | 0 | xmlFAParseCharClass(ctxt); |
5284 | 0 | return(1); |
5285 | 0 | } |
5286 | 0 | return(0); |
5287 | 0 | } |
5288 | | |
5289 | | /** |
5290 | | * xmlFAParsePiece: |
5291 | | * @ctxt: a regexp parser context |
5292 | | * |
5293 | | * [3] piece ::= atom quantifier? |
5294 | | */ |
5295 | | static int |
5296 | 0 | xmlFAParsePiece(xmlRegParserCtxtPtr ctxt) { |
5297 | 0 | int ret; |
5298 | |
|
5299 | 0 | ctxt->atom = NULL; |
5300 | 0 | ret = xmlFAParseAtom(ctxt); |
5301 | 0 | if (ret == 0) |
5302 | 0 | return(0); |
5303 | 0 | if (ctxt->atom == NULL) { |
5304 | 0 | ERROR("internal: no atom generated"); |
5305 | 0 | } |
5306 | 0 | xmlFAParseQuantifier(ctxt); |
5307 | 0 | return(1); |
5308 | 0 | } |
5309 | | |
5310 | | /** |
5311 | | * xmlFAParseBranch: |
5312 | | * @ctxt: a regexp parser context |
5313 | | * @to: optional target to the end of the branch |
5314 | | * |
5315 | | * @to is used to optimize by removing duplicate path in automata |
5316 | | * in expressions like (a|b)(c|d) |
5317 | | * |
5318 | | * [2] branch ::= piece* |
5319 | | */ |
5320 | | static int |
5321 | 0 | xmlFAParseBranch(xmlRegParserCtxtPtr ctxt, xmlRegStatePtr to) { |
5322 | 0 | xmlRegStatePtr previous; |
5323 | 0 | int ret; |
5324 | |
|
5325 | 0 | previous = ctxt->state; |
5326 | 0 | ret = xmlFAParsePiece(ctxt); |
5327 | 0 | if (ret == 0) { |
5328 | | /* Empty branch */ |
5329 | 0 | xmlFAGenerateEpsilonTransition(ctxt, previous, to); |
5330 | 0 | } else { |
5331 | 0 | if (xmlFAGenerateTransitions(ctxt, previous, |
5332 | 0 | (CUR=='|' || CUR==')' || CUR==0) ? to : NULL, |
5333 | 0 | ctxt->atom) < 0) { |
5334 | 0 | xmlRegFreeAtom(ctxt->atom); |
5335 | 0 | ctxt->atom = NULL; |
5336 | 0 | return(-1); |
5337 | 0 | } |
5338 | 0 | previous = ctxt->state; |
5339 | 0 | ctxt->atom = NULL; |
5340 | 0 | } |
5341 | 0 | while ((ret != 0) && (ctxt->error == 0)) { |
5342 | 0 | ret = xmlFAParsePiece(ctxt); |
5343 | 0 | if (ret != 0) { |
5344 | 0 | if (xmlFAGenerateTransitions(ctxt, previous, |
5345 | 0 | (CUR=='|' || CUR==')' || CUR==0) ? to : NULL, |
5346 | 0 | ctxt->atom) < 0) { |
5347 | 0 | xmlRegFreeAtom(ctxt->atom); |
5348 | 0 | ctxt->atom = NULL; |
5349 | 0 | return(-1); |
5350 | 0 | } |
5351 | 0 | previous = ctxt->state; |
5352 | 0 | ctxt->atom = NULL; |
5353 | 0 | } |
5354 | 0 | } |
5355 | 0 | return(0); |
5356 | 0 | } |
5357 | | |
5358 | | /** |
5359 | | * xmlFAParseRegExp: |
5360 | | * @ctxt: a regexp parser context |
5361 | | * @top: is this the top-level expression ? |
5362 | | * |
5363 | | * [1] regExp ::= branch ( '|' branch )* |
5364 | | */ |
5365 | | static void |
5366 | 0 | xmlFAParseRegExp(xmlRegParserCtxtPtr ctxt, int top) { |
5367 | 0 | xmlRegStatePtr start, end; |
5368 | | |
5369 | | /* if not top start should have been generated by an epsilon trans */ |
5370 | 0 | start = ctxt->state; |
5371 | 0 | ctxt->end = NULL; |
5372 | 0 | xmlFAParseBranch(ctxt, NULL); |
5373 | 0 | if (top) { |
5374 | 0 | ctxt->state->type = XML_REGEXP_FINAL_STATE; |
5375 | 0 | } |
5376 | 0 | if (CUR != '|') { |
5377 | 0 | ctxt->end = ctxt->state; |
5378 | 0 | return; |
5379 | 0 | } |
5380 | 0 | end = ctxt->state; |
5381 | 0 | while ((CUR == '|') && (ctxt->error == 0)) { |
5382 | 0 | NEXT; |
5383 | 0 | ctxt->state = start; |
5384 | 0 | ctxt->end = NULL; |
5385 | 0 | xmlFAParseBranch(ctxt, end); |
5386 | 0 | } |
5387 | 0 | if (!top) { |
5388 | 0 | ctxt->state = end; |
5389 | 0 | ctxt->end = end; |
5390 | 0 | } |
5391 | 0 | } |
5392 | | |
5393 | | /************************************************************************ |
5394 | | * * |
5395 | | * The basic API * |
5396 | | * * |
5397 | | ************************************************************************/ |
5398 | | |
5399 | | /** |
5400 | | * xmlRegexpPrint: |
5401 | | * @output: the file for the output debug |
5402 | | * @regexp: the compiled regexp |
5403 | | * |
5404 | | * DEPRECATED: Don't use. |
5405 | | * |
5406 | | * No-op since 2.14.0. |
5407 | | */ |
5408 | | void |
5409 | | xmlRegexpPrint(FILE *output ATTRIBUTE_UNUSED, |
5410 | 0 | xmlRegexpPtr regexp ATTRIBUTE_UNUSED) { |
5411 | 0 | } |
5412 | | |
5413 | | /** |
5414 | | * xmlRegexpCompile: |
5415 | | * @regexp: a regular expression string |
5416 | | * |
5417 | | * Parses a regular expression conforming to XML Schemas Part 2 Datatype |
5418 | | * Appendix F and builds an automata suitable for testing strings against |
5419 | | * that regular expression |
5420 | | * |
5421 | | * Returns the compiled expression or NULL in case of error |
5422 | | */ |
5423 | | xmlRegexpPtr |
5424 | 0 | xmlRegexpCompile(const xmlChar *regexp) { |
5425 | 0 | xmlRegexpPtr ret = NULL; |
5426 | 0 | xmlRegParserCtxtPtr ctxt; |
5427 | |
|
5428 | 0 | if (regexp == NULL) |
5429 | 0 | return(NULL); |
5430 | | |
5431 | 0 | ctxt = xmlRegNewParserCtxt(regexp); |
5432 | 0 | if (ctxt == NULL) |
5433 | 0 | return(NULL); |
5434 | | |
5435 | | /* initialize the parser */ |
5436 | 0 | ctxt->state = xmlRegStatePush(ctxt); |
5437 | 0 | if (ctxt->state == NULL) |
5438 | 0 | goto error; |
5439 | 0 | ctxt->start = ctxt->state; |
5440 | 0 | ctxt->end = NULL; |
5441 | | |
5442 | | /* parse the expression building an automata */ |
5443 | 0 | xmlFAParseRegExp(ctxt, 1); |
5444 | 0 | if (CUR != 0) { |
5445 | 0 | ERROR("xmlFAParseRegExp: extra characters"); |
5446 | 0 | } |
5447 | 0 | if (ctxt->error != 0) |
5448 | 0 | goto error; |
5449 | 0 | ctxt->end = ctxt->state; |
5450 | 0 | ctxt->start->type = XML_REGEXP_START_STATE; |
5451 | 0 | ctxt->end->type = XML_REGEXP_FINAL_STATE; |
5452 | | |
5453 | | /* remove the Epsilon except for counted transitions */ |
5454 | 0 | xmlFAEliminateEpsilonTransitions(ctxt); |
5455 | | |
5456 | |
|
5457 | 0 | if (ctxt->error != 0) |
5458 | 0 | goto error; |
5459 | 0 | ret = xmlRegEpxFromParse(ctxt); |
5460 | |
|
5461 | 0 | error: |
5462 | 0 | xmlRegFreeParserCtxt(ctxt); |
5463 | 0 | return(ret); |
5464 | 0 | } |
5465 | | |
5466 | | /** |
5467 | | * xmlRegexpExec: |
5468 | | * @comp: the compiled regular expression |
5469 | | * @content: the value to check against the regular expression |
5470 | | * |
5471 | | * Check if the regular expression generates the value |
5472 | | * |
5473 | | * Returns 1 if it matches, 0 if not and a negative value in case of error |
5474 | | */ |
5475 | | int |
5476 | 0 | xmlRegexpExec(xmlRegexpPtr comp, const xmlChar *content) { |
5477 | 0 | if ((comp == NULL) || (content == NULL)) |
5478 | 0 | return(-1); |
5479 | 0 | return(xmlFARegExec(comp, content)); |
5480 | 0 | } |
5481 | | |
5482 | | /** |
5483 | | * xmlRegexpIsDeterminist: |
5484 | | * @comp: the compiled regular expression |
5485 | | * |
5486 | | * Check if the regular expression is determinist |
5487 | | * |
5488 | | * Returns 1 if it yes, 0 if not and a negative value in case of error |
5489 | | */ |
5490 | | int |
5491 | 0 | xmlRegexpIsDeterminist(xmlRegexpPtr comp) { |
5492 | 0 | xmlAutomataPtr am; |
5493 | 0 | int ret; |
5494 | |
|
5495 | 0 | if (comp == NULL) |
5496 | 0 | return(-1); |
5497 | 0 | if (comp->determinist != -1) |
5498 | 0 | return(comp->determinist); |
5499 | | |
5500 | 0 | am = xmlNewAutomata(); |
5501 | 0 | if (am == NULL) |
5502 | 0 | return(-1); |
5503 | 0 | if (am->states != NULL) { |
5504 | 0 | int i; |
5505 | |
|
5506 | 0 | for (i = 0;i < am->nbStates;i++) |
5507 | 0 | xmlRegFreeState(am->states[i]); |
5508 | 0 | xmlFree(am->states); |
5509 | 0 | } |
5510 | 0 | am->nbAtoms = comp->nbAtoms; |
5511 | 0 | am->atoms = comp->atoms; |
5512 | 0 | am->nbStates = comp->nbStates; |
5513 | 0 | am->states = comp->states; |
5514 | 0 | am->determinist = -1; |
5515 | 0 | am->flags = comp->flags; |
5516 | 0 | ret = xmlFAComputesDeterminism(am); |
5517 | 0 | am->atoms = NULL; |
5518 | 0 | am->states = NULL; |
5519 | 0 | xmlFreeAutomata(am); |
5520 | 0 | comp->determinist = ret; |
5521 | 0 | return(ret); |
5522 | 0 | } |
5523 | | |
5524 | | /** |
5525 | | * xmlRegFreeRegexp: |
5526 | | * @regexp: the regexp |
5527 | | * |
5528 | | * Free a regexp |
5529 | | */ |
5530 | | void |
5531 | 0 | xmlRegFreeRegexp(xmlRegexpPtr regexp) { |
5532 | 0 | int i; |
5533 | 0 | if (regexp == NULL) |
5534 | 0 | return; |
5535 | | |
5536 | 0 | if (regexp->string != NULL) |
5537 | 0 | xmlFree(regexp->string); |
5538 | 0 | if (regexp->states != NULL) { |
5539 | 0 | for (i = 0;i < regexp->nbStates;i++) |
5540 | 0 | xmlRegFreeState(regexp->states[i]); |
5541 | 0 | xmlFree(regexp->states); |
5542 | 0 | } |
5543 | 0 | if (regexp->atoms != NULL) { |
5544 | 0 | for (i = 0;i < regexp->nbAtoms;i++) |
5545 | 0 | xmlRegFreeAtom(regexp->atoms[i]); |
5546 | 0 | xmlFree(regexp->atoms); |
5547 | 0 | } |
5548 | 0 | if (regexp->counters != NULL) |
5549 | 0 | xmlFree(regexp->counters); |
5550 | 0 | if (regexp->compact != NULL) |
5551 | 0 | xmlFree(regexp->compact); |
5552 | 0 | if (regexp->transdata != NULL) |
5553 | 0 | xmlFree(regexp->transdata); |
5554 | 0 | if (regexp->stringMap != NULL) { |
5555 | 0 | for (i = 0; i < regexp->nbstrings;i++) |
5556 | 0 | xmlFree(regexp->stringMap[i]); |
5557 | 0 | xmlFree(regexp->stringMap); |
5558 | 0 | } |
5559 | |
|
5560 | 0 | xmlFree(regexp); |
5561 | 0 | } |
5562 | | |
5563 | | /************************************************************************ |
5564 | | * * |
5565 | | * The Automata interface * |
5566 | | * * |
5567 | | ************************************************************************/ |
5568 | | |
5569 | | /** |
5570 | | * xmlNewAutomata: |
5571 | | * |
5572 | | * Create a new automata |
5573 | | * |
5574 | | * Returns the new object or NULL in case of failure |
5575 | | */ |
5576 | | xmlAutomataPtr |
5577 | 0 | xmlNewAutomata(void) { |
5578 | 0 | xmlAutomataPtr ctxt; |
5579 | |
|
5580 | 0 | ctxt = xmlRegNewParserCtxt(NULL); |
5581 | 0 | if (ctxt == NULL) |
5582 | 0 | return(NULL); |
5583 | | |
5584 | | /* initialize the parser */ |
5585 | 0 | ctxt->state = xmlRegStatePush(ctxt); |
5586 | 0 | if (ctxt->state == NULL) { |
5587 | 0 | xmlFreeAutomata(ctxt); |
5588 | 0 | return(NULL); |
5589 | 0 | } |
5590 | 0 | ctxt->start = ctxt->state; |
5591 | 0 | ctxt->end = NULL; |
5592 | |
|
5593 | 0 | ctxt->start->type = XML_REGEXP_START_STATE; |
5594 | 0 | ctxt->flags = 0; |
5595 | |
|
5596 | 0 | return(ctxt); |
5597 | 0 | } |
5598 | | |
5599 | | /** |
5600 | | * xmlFreeAutomata: |
5601 | | * @am: an automata |
5602 | | * |
5603 | | * Free an automata |
5604 | | */ |
5605 | | void |
5606 | 0 | xmlFreeAutomata(xmlAutomataPtr am) { |
5607 | 0 | if (am == NULL) |
5608 | 0 | return; |
5609 | 0 | xmlRegFreeParserCtxt(am); |
5610 | 0 | } |
5611 | | |
5612 | | /** |
5613 | | * xmlAutomataSetFlags: |
5614 | | * @am: an automata |
5615 | | * @flags: a set of internal flags |
5616 | | * |
5617 | | * Set some flags on the automata |
5618 | | */ |
5619 | | void |
5620 | 0 | xmlAutomataSetFlags(xmlAutomataPtr am, int flags) { |
5621 | 0 | if (am == NULL) |
5622 | 0 | return; |
5623 | 0 | am->flags |= flags; |
5624 | 0 | } |
5625 | | |
5626 | | /** |
5627 | | * xmlAutomataGetInitState: |
5628 | | * @am: an automata |
5629 | | * |
5630 | | * Initial state lookup |
5631 | | * |
5632 | | * Returns the initial state of the automata |
5633 | | */ |
5634 | | xmlAutomataStatePtr |
5635 | 0 | xmlAutomataGetInitState(xmlAutomataPtr am) { |
5636 | 0 | if (am == NULL) |
5637 | 0 | return(NULL); |
5638 | 0 | return(am->start); |
5639 | 0 | } |
5640 | | |
5641 | | /** |
5642 | | * xmlAutomataSetFinalState: |
5643 | | * @am: an automata |
5644 | | * @state: a state in this automata |
5645 | | * |
5646 | | * Makes that state a final state |
5647 | | * |
5648 | | * Returns 0 or -1 in case of error |
5649 | | */ |
5650 | | int |
5651 | 0 | xmlAutomataSetFinalState(xmlAutomataPtr am, xmlAutomataStatePtr state) { |
5652 | 0 | if ((am == NULL) || (state == NULL)) |
5653 | 0 | return(-1); |
5654 | 0 | state->type = XML_REGEXP_FINAL_STATE; |
5655 | 0 | return(0); |
5656 | 0 | } |
5657 | | |
5658 | | /** |
5659 | | * xmlAutomataNewTransition: |
5660 | | * @am: an automata |
5661 | | * @from: the starting point of the transition |
5662 | | * @to: the target point of the transition or NULL |
5663 | | * @token: the input string associated to that transition |
5664 | | * @data: data passed to the callback function if the transition is activated |
5665 | | * |
5666 | | * If @to is NULL, this creates first a new target state in the automata |
5667 | | * and then adds a transition from the @from state to the target state |
5668 | | * activated by the value of @token |
5669 | | * |
5670 | | * Returns the target state or NULL in case of error |
5671 | | */ |
5672 | | xmlAutomataStatePtr |
5673 | | xmlAutomataNewTransition(xmlAutomataPtr am, xmlAutomataStatePtr from, |
5674 | | xmlAutomataStatePtr to, const xmlChar *token, |
5675 | 0 | void *data) { |
5676 | 0 | xmlRegAtomPtr atom; |
5677 | |
|
5678 | 0 | if ((am == NULL) || (from == NULL) || (token == NULL)) |
5679 | 0 | return(NULL); |
5680 | 0 | atom = xmlRegNewAtom(am, XML_REGEXP_STRING); |
5681 | 0 | if (atom == NULL) |
5682 | 0 | return(NULL); |
5683 | 0 | atom->data = data; |
5684 | 0 | atom->valuep = xmlStrdup(token); |
5685 | 0 | if (atom->valuep == NULL) { |
5686 | 0 | xmlRegFreeAtom(atom); |
5687 | 0 | xmlRegexpErrMemory(am); |
5688 | 0 | return(NULL); |
5689 | 0 | } |
5690 | | |
5691 | 0 | if (xmlFAGenerateTransitions(am, from, to, atom) < 0) { |
5692 | 0 | xmlRegFreeAtom(atom); |
5693 | 0 | return(NULL); |
5694 | 0 | } |
5695 | 0 | if (to == NULL) |
5696 | 0 | return(am->state); |
5697 | 0 | return(to); |
5698 | 0 | } |
5699 | | |
5700 | | /** |
5701 | | * xmlAutomataNewTransition2: |
5702 | | * @am: an automata |
5703 | | * @from: the starting point of the transition |
5704 | | * @to: the target point of the transition or NULL |
5705 | | * @token: the first input string associated to that transition |
5706 | | * @token2: the second input string associated to that transition |
5707 | | * @data: data passed to the callback function if the transition is activated |
5708 | | * |
5709 | | * If @to is NULL, this creates first a new target state in the automata |
5710 | | * and then adds a transition from the @from state to the target state |
5711 | | * activated by the value of @token |
5712 | | * |
5713 | | * Returns the target state or NULL in case of error |
5714 | | */ |
5715 | | xmlAutomataStatePtr |
5716 | | xmlAutomataNewTransition2(xmlAutomataPtr am, xmlAutomataStatePtr from, |
5717 | | xmlAutomataStatePtr to, const xmlChar *token, |
5718 | 0 | const xmlChar *token2, void *data) { |
5719 | 0 | xmlRegAtomPtr atom; |
5720 | |
|
5721 | 0 | if ((am == NULL) || (from == NULL) || (token == NULL)) |
5722 | 0 | return(NULL); |
5723 | 0 | atom = xmlRegNewAtom(am, XML_REGEXP_STRING); |
5724 | 0 | if (atom == NULL) |
5725 | 0 | return(NULL); |
5726 | 0 | atom->data = data; |
5727 | 0 | if ((token2 == NULL) || (*token2 == 0)) { |
5728 | 0 | atom->valuep = xmlStrdup(token); |
5729 | 0 | } else { |
5730 | 0 | int lenn, lenp; |
5731 | 0 | xmlChar *str; |
5732 | |
|
5733 | 0 | lenn = strlen((char *) token2); |
5734 | 0 | lenp = strlen((char *) token); |
5735 | |
|
5736 | 0 | str = xmlMalloc(lenn + lenp + 2); |
5737 | 0 | if (str == NULL) { |
5738 | 0 | xmlRegFreeAtom(atom); |
5739 | 0 | return(NULL); |
5740 | 0 | } |
5741 | 0 | memcpy(&str[0], token, lenp); |
5742 | 0 | str[lenp] = '|'; |
5743 | 0 | memcpy(&str[lenp + 1], token2, lenn); |
5744 | 0 | str[lenn + lenp + 1] = 0; |
5745 | |
|
5746 | 0 | atom->valuep = str; |
5747 | 0 | } |
5748 | | |
5749 | 0 | if (xmlFAGenerateTransitions(am, from, to, atom) < 0) { |
5750 | 0 | xmlRegFreeAtom(atom); |
5751 | 0 | return(NULL); |
5752 | 0 | } |
5753 | 0 | if (to == NULL) |
5754 | 0 | return(am->state); |
5755 | 0 | return(to); |
5756 | 0 | } |
5757 | | |
5758 | | /** |
5759 | | * xmlAutomataNewNegTrans: |
5760 | | * @am: an automata |
5761 | | * @from: the starting point of the transition |
5762 | | * @to: the target point of the transition or NULL |
5763 | | * @token: the first input string associated to that transition |
5764 | | * @token2: the second input string associated to that transition |
5765 | | * @data: data passed to the callback function if the transition is activated |
5766 | | * |
5767 | | * If @to is NULL, this creates first a new target state in the automata |
5768 | | * and then adds a transition from the @from state to the target state |
5769 | | * activated by any value except (@token,@token2) |
5770 | | * Note that if @token2 is not NULL, then (X, NULL) won't match to follow |
5771 | | # the semantic of XSD ##other |
5772 | | * |
5773 | | * Returns the target state or NULL in case of error |
5774 | | */ |
5775 | | xmlAutomataStatePtr |
5776 | | xmlAutomataNewNegTrans(xmlAutomataPtr am, xmlAutomataStatePtr from, |
5777 | | xmlAutomataStatePtr to, const xmlChar *token, |
5778 | 0 | const xmlChar *token2, void *data) { |
5779 | 0 | xmlRegAtomPtr atom; |
5780 | 0 | xmlChar err_msg[200]; |
5781 | |
|
5782 | 0 | if ((am == NULL) || (from == NULL) || (token == NULL)) |
5783 | 0 | return(NULL); |
5784 | 0 | atom = xmlRegNewAtom(am, XML_REGEXP_STRING); |
5785 | 0 | if (atom == NULL) |
5786 | 0 | return(NULL); |
5787 | 0 | atom->data = data; |
5788 | 0 | atom->neg = 1; |
5789 | 0 | if ((token2 == NULL) || (*token2 == 0)) { |
5790 | 0 | atom->valuep = xmlStrdup(token); |
5791 | 0 | } else { |
5792 | 0 | int lenn, lenp; |
5793 | 0 | xmlChar *str; |
5794 | |
|
5795 | 0 | lenn = strlen((char *) token2); |
5796 | 0 | lenp = strlen((char *) token); |
5797 | |
|
5798 | 0 | str = xmlMalloc(lenn + lenp + 2); |
5799 | 0 | if (str == NULL) { |
5800 | 0 | xmlRegFreeAtom(atom); |
5801 | 0 | return(NULL); |
5802 | 0 | } |
5803 | 0 | memcpy(&str[0], token, lenp); |
5804 | 0 | str[lenp] = '|'; |
5805 | 0 | memcpy(&str[lenp + 1], token2, lenn); |
5806 | 0 | str[lenn + lenp + 1] = 0; |
5807 | |
|
5808 | 0 | atom->valuep = str; |
5809 | 0 | } |
5810 | 0 | snprintf((char *) err_msg, 199, "not %s", (const char *) atom->valuep); |
5811 | 0 | err_msg[199] = 0; |
5812 | 0 | atom->valuep2 = xmlStrdup(err_msg); |
5813 | |
|
5814 | 0 | if (xmlFAGenerateTransitions(am, from, to, atom) < 0) { |
5815 | 0 | xmlRegFreeAtom(atom); |
5816 | 0 | return(NULL); |
5817 | 0 | } |
5818 | 0 | am->negs++; |
5819 | 0 | if (to == NULL) |
5820 | 0 | return(am->state); |
5821 | 0 | return(to); |
5822 | 0 | } |
5823 | | |
5824 | | /** |
5825 | | * xmlAutomataNewCountTrans2: |
5826 | | * @am: an automata |
5827 | | * @from: the starting point of the transition |
5828 | | * @to: the target point of the transition or NULL |
5829 | | * @token: the input string associated to that transition |
5830 | | * @token2: the second input string associated to that transition |
5831 | | * @min: the minimum successive occurrences of token |
5832 | | * @max: the maximum successive occurrences of token |
5833 | | * @data: data associated to the transition |
5834 | | * |
5835 | | * If @to is NULL, this creates first a new target state in the automata |
5836 | | * and then adds a transition from the @from state to the target state |
5837 | | * activated by a succession of input of value @token and @token2 and |
5838 | | * whose number is between @min and @max |
5839 | | * |
5840 | | * Returns the target state or NULL in case of error |
5841 | | */ |
5842 | | xmlAutomataStatePtr |
5843 | | xmlAutomataNewCountTrans2(xmlAutomataPtr am, xmlAutomataStatePtr from, |
5844 | | xmlAutomataStatePtr to, const xmlChar *token, |
5845 | | const xmlChar *token2, |
5846 | 0 | int min, int max, void *data) { |
5847 | 0 | xmlRegAtomPtr atom; |
5848 | 0 | int counter; |
5849 | |
|
5850 | 0 | if ((am == NULL) || (from == NULL) || (token == NULL)) |
5851 | 0 | return(NULL); |
5852 | 0 | if (min < 0) |
5853 | 0 | return(NULL); |
5854 | 0 | if ((max < min) || (max < 1)) |
5855 | 0 | return(NULL); |
5856 | 0 | atom = xmlRegNewAtom(am, XML_REGEXP_STRING); |
5857 | 0 | if (atom == NULL) |
5858 | 0 | return(NULL); |
5859 | 0 | if ((token2 == NULL) || (*token2 == 0)) { |
5860 | 0 | atom->valuep = xmlStrdup(token); |
5861 | 0 | if (atom->valuep == NULL) |
5862 | 0 | goto error; |
5863 | 0 | } else { |
5864 | 0 | int lenn, lenp; |
5865 | 0 | xmlChar *str; |
5866 | |
|
5867 | 0 | lenn = strlen((char *) token2); |
5868 | 0 | lenp = strlen((char *) token); |
5869 | |
|
5870 | 0 | str = xmlMalloc(lenn + lenp + 2); |
5871 | 0 | if (str == NULL) |
5872 | 0 | goto error; |
5873 | 0 | memcpy(&str[0], token, lenp); |
5874 | 0 | str[lenp] = '|'; |
5875 | 0 | memcpy(&str[lenp + 1], token2, lenn); |
5876 | 0 | str[lenn + lenp + 1] = 0; |
5877 | |
|
5878 | 0 | atom->valuep = str; |
5879 | 0 | } |
5880 | 0 | atom->data = data; |
5881 | 0 | if (min == 0) |
5882 | 0 | atom->min = 1; |
5883 | 0 | else |
5884 | 0 | atom->min = min; |
5885 | 0 | atom->max = max; |
5886 | | |
5887 | | /* |
5888 | | * associate a counter to the transition. |
5889 | | */ |
5890 | 0 | counter = xmlRegGetCounter(am); |
5891 | 0 | if (counter < 0) |
5892 | 0 | goto error; |
5893 | 0 | am->counters[counter].min = min; |
5894 | 0 | am->counters[counter].max = max; |
5895 | | |
5896 | | /* xmlFAGenerateTransitions(am, from, to, atom); */ |
5897 | 0 | if (to == NULL) { |
5898 | 0 | to = xmlRegStatePush(am); |
5899 | 0 | if (to == NULL) |
5900 | 0 | goto error; |
5901 | 0 | } |
5902 | 0 | xmlRegStateAddTrans(am, from, atom, to, counter, -1); |
5903 | 0 | if (xmlRegAtomPush(am, atom) < 0) |
5904 | 0 | goto error; |
5905 | 0 | am->state = to; |
5906 | |
|
5907 | 0 | if (to == NULL) |
5908 | 0 | to = am->state; |
5909 | 0 | if (to == NULL) |
5910 | 0 | return(NULL); |
5911 | 0 | if (min == 0) |
5912 | 0 | xmlFAGenerateEpsilonTransition(am, from, to); |
5913 | 0 | return(to); |
5914 | | |
5915 | 0 | error: |
5916 | 0 | xmlRegFreeAtom(atom); |
5917 | 0 | return(NULL); |
5918 | 0 | } |
5919 | | |
5920 | | /** |
5921 | | * xmlAutomataNewCountTrans: |
5922 | | * @am: an automata |
5923 | | * @from: the starting point of the transition |
5924 | | * @to: the target point of the transition or NULL |
5925 | | * @token: the input string associated to that transition |
5926 | | * @min: the minimum successive occurrences of token |
5927 | | * @max: the maximum successive occurrences of token |
5928 | | * @data: data associated to the transition |
5929 | | * |
5930 | | * If @to is NULL, this creates first a new target state in the automata |
5931 | | * and then adds a transition from the @from state to the target state |
5932 | | * activated by a succession of input of value @token and whose number |
5933 | | * is between @min and @max |
5934 | | * |
5935 | | * Returns the target state or NULL in case of error |
5936 | | */ |
5937 | | xmlAutomataStatePtr |
5938 | | xmlAutomataNewCountTrans(xmlAutomataPtr am, xmlAutomataStatePtr from, |
5939 | | xmlAutomataStatePtr to, const xmlChar *token, |
5940 | 0 | int min, int max, void *data) { |
5941 | 0 | xmlRegAtomPtr atom; |
5942 | 0 | int counter; |
5943 | |
|
5944 | 0 | if ((am == NULL) || (from == NULL) || (token == NULL)) |
5945 | 0 | return(NULL); |
5946 | 0 | if (min < 0) |
5947 | 0 | return(NULL); |
5948 | 0 | if ((max < min) || (max < 1)) |
5949 | 0 | return(NULL); |
5950 | 0 | atom = xmlRegNewAtom(am, XML_REGEXP_STRING); |
5951 | 0 | if (atom == NULL) |
5952 | 0 | return(NULL); |
5953 | 0 | atom->valuep = xmlStrdup(token); |
5954 | 0 | if (atom->valuep == NULL) |
5955 | 0 | goto error; |
5956 | 0 | atom->data = data; |
5957 | 0 | if (min == 0) |
5958 | 0 | atom->min = 1; |
5959 | 0 | else |
5960 | 0 | atom->min = min; |
5961 | 0 | atom->max = max; |
5962 | | |
5963 | | /* |
5964 | | * associate a counter to the transition. |
5965 | | */ |
5966 | 0 | counter = xmlRegGetCounter(am); |
5967 | 0 | if (counter < 0) |
5968 | 0 | goto error; |
5969 | 0 | am->counters[counter].min = min; |
5970 | 0 | am->counters[counter].max = max; |
5971 | | |
5972 | | /* xmlFAGenerateTransitions(am, from, to, atom); */ |
5973 | 0 | if (to == NULL) { |
5974 | 0 | to = xmlRegStatePush(am); |
5975 | 0 | if (to == NULL) |
5976 | 0 | goto error; |
5977 | 0 | } |
5978 | 0 | xmlRegStateAddTrans(am, from, atom, to, counter, -1); |
5979 | 0 | if (xmlRegAtomPush(am, atom) < 0) |
5980 | 0 | goto error; |
5981 | 0 | am->state = to; |
5982 | |
|
5983 | 0 | if (to == NULL) |
5984 | 0 | to = am->state; |
5985 | 0 | if (to == NULL) |
5986 | 0 | return(NULL); |
5987 | 0 | if (min == 0) |
5988 | 0 | xmlFAGenerateEpsilonTransition(am, from, to); |
5989 | 0 | return(to); |
5990 | | |
5991 | 0 | error: |
5992 | 0 | xmlRegFreeAtom(atom); |
5993 | 0 | return(NULL); |
5994 | 0 | } |
5995 | | |
5996 | | /** |
5997 | | * xmlAutomataNewOnceTrans2: |
5998 | | * @am: an automata |
5999 | | * @from: the starting point of the transition |
6000 | | * @to: the target point of the transition or NULL |
6001 | | * @token: the input string associated to that transition |
6002 | | * @token2: the second input string associated to that transition |
6003 | | * @min: the minimum successive occurrences of token |
6004 | | * @max: the maximum successive occurrences of token |
6005 | | * @data: data associated to the transition |
6006 | | * |
6007 | | * If @to is NULL, this creates first a new target state in the automata |
6008 | | * and then adds a transition from the @from state to the target state |
6009 | | * activated by a succession of input of value @token and @token2 and whose |
6010 | | * number is between @min and @max, moreover that transition can only be |
6011 | | * crossed once. |
6012 | | * |
6013 | | * Returns the target state or NULL in case of error |
6014 | | */ |
6015 | | xmlAutomataStatePtr |
6016 | | xmlAutomataNewOnceTrans2(xmlAutomataPtr am, xmlAutomataStatePtr from, |
6017 | | xmlAutomataStatePtr to, const xmlChar *token, |
6018 | | const xmlChar *token2, |
6019 | 0 | int min, int max, void *data) { |
6020 | 0 | xmlRegAtomPtr atom; |
6021 | 0 | int counter; |
6022 | |
|
6023 | 0 | if ((am == NULL) || (from == NULL) || (token == NULL)) |
6024 | 0 | return(NULL); |
6025 | 0 | if (min < 1) |
6026 | 0 | return(NULL); |
6027 | 0 | if (max < min) |
6028 | 0 | return(NULL); |
6029 | 0 | atom = xmlRegNewAtom(am, XML_REGEXP_STRING); |
6030 | 0 | if (atom == NULL) |
6031 | 0 | return(NULL); |
6032 | 0 | if ((token2 == NULL) || (*token2 == 0)) { |
6033 | 0 | atom->valuep = xmlStrdup(token); |
6034 | 0 | if (atom->valuep == NULL) |
6035 | 0 | goto error; |
6036 | 0 | } else { |
6037 | 0 | int lenn, lenp; |
6038 | 0 | xmlChar *str; |
6039 | |
|
6040 | 0 | lenn = strlen((char *) token2); |
6041 | 0 | lenp = strlen((char *) token); |
6042 | |
|
6043 | 0 | str = xmlMalloc(lenn + lenp + 2); |
6044 | 0 | if (str == NULL) |
6045 | 0 | goto error; |
6046 | 0 | memcpy(&str[0], token, lenp); |
6047 | 0 | str[lenp] = '|'; |
6048 | 0 | memcpy(&str[lenp + 1], token2, lenn); |
6049 | 0 | str[lenn + lenp + 1] = 0; |
6050 | |
|
6051 | 0 | atom->valuep = str; |
6052 | 0 | } |
6053 | 0 | atom->data = data; |
6054 | 0 | atom->quant = XML_REGEXP_QUANT_ONCEONLY; |
6055 | 0 | atom->min = min; |
6056 | 0 | atom->max = max; |
6057 | | /* |
6058 | | * associate a counter to the transition. |
6059 | | */ |
6060 | 0 | counter = xmlRegGetCounter(am); |
6061 | 0 | if (counter < 0) |
6062 | 0 | goto error; |
6063 | 0 | am->counters[counter].min = 1; |
6064 | 0 | am->counters[counter].max = 1; |
6065 | | |
6066 | | /* xmlFAGenerateTransitions(am, from, to, atom); */ |
6067 | 0 | if (to == NULL) { |
6068 | 0 | to = xmlRegStatePush(am); |
6069 | 0 | if (to == NULL) |
6070 | 0 | goto error; |
6071 | 0 | } |
6072 | 0 | xmlRegStateAddTrans(am, from, atom, to, counter, -1); |
6073 | 0 | if (xmlRegAtomPush(am, atom) < 0) |
6074 | 0 | goto error; |
6075 | 0 | am->state = to; |
6076 | 0 | return(to); |
6077 | | |
6078 | 0 | error: |
6079 | 0 | xmlRegFreeAtom(atom); |
6080 | 0 | return(NULL); |
6081 | 0 | } |
6082 | | |
6083 | | |
6084 | | |
6085 | | /** |
6086 | | * xmlAutomataNewOnceTrans: |
6087 | | * @am: an automata |
6088 | | * @from: the starting point of the transition |
6089 | | * @to: the target point of the transition or NULL |
6090 | | * @token: the input string associated to that transition |
6091 | | * @min: the minimum successive occurrences of token |
6092 | | * @max: the maximum successive occurrences of token |
6093 | | * @data: data associated to the transition |
6094 | | * |
6095 | | * If @to is NULL, this creates first a new target state in the automata |
6096 | | * and then adds a transition from the @from state to the target state |
6097 | | * activated by a succession of input of value @token and whose number |
6098 | | * is between @min and @max, moreover that transition can only be crossed |
6099 | | * once. |
6100 | | * |
6101 | | * Returns the target state or NULL in case of error |
6102 | | */ |
6103 | | xmlAutomataStatePtr |
6104 | | xmlAutomataNewOnceTrans(xmlAutomataPtr am, xmlAutomataStatePtr from, |
6105 | | xmlAutomataStatePtr to, const xmlChar *token, |
6106 | 0 | int min, int max, void *data) { |
6107 | 0 | xmlRegAtomPtr atom; |
6108 | 0 | int counter; |
6109 | |
|
6110 | 0 | if ((am == NULL) || (from == NULL) || (token == NULL)) |
6111 | 0 | return(NULL); |
6112 | 0 | if (min < 1) |
6113 | 0 | return(NULL); |
6114 | 0 | if (max < min) |
6115 | 0 | return(NULL); |
6116 | 0 | atom = xmlRegNewAtom(am, XML_REGEXP_STRING); |
6117 | 0 | if (atom == NULL) |
6118 | 0 | return(NULL); |
6119 | 0 | atom->valuep = xmlStrdup(token); |
6120 | 0 | atom->data = data; |
6121 | 0 | atom->quant = XML_REGEXP_QUANT_ONCEONLY; |
6122 | 0 | atom->min = min; |
6123 | 0 | atom->max = max; |
6124 | | /* |
6125 | | * associate a counter to the transition. |
6126 | | */ |
6127 | 0 | counter = xmlRegGetCounter(am); |
6128 | 0 | if (counter < 0) |
6129 | 0 | goto error; |
6130 | 0 | am->counters[counter].min = 1; |
6131 | 0 | am->counters[counter].max = 1; |
6132 | | |
6133 | | /* xmlFAGenerateTransitions(am, from, to, atom); */ |
6134 | 0 | if (to == NULL) { |
6135 | 0 | to = xmlRegStatePush(am); |
6136 | 0 | if (to == NULL) |
6137 | 0 | goto error; |
6138 | 0 | } |
6139 | 0 | xmlRegStateAddTrans(am, from, atom, to, counter, -1); |
6140 | 0 | if (xmlRegAtomPush(am, atom) < 0) |
6141 | 0 | goto error; |
6142 | 0 | am->state = to; |
6143 | 0 | return(to); |
6144 | | |
6145 | 0 | error: |
6146 | 0 | xmlRegFreeAtom(atom); |
6147 | 0 | return(NULL); |
6148 | 0 | } |
6149 | | |
6150 | | /** |
6151 | | * xmlAutomataNewState: |
6152 | | * @am: an automata |
6153 | | * |
6154 | | * Create a new disconnected state in the automata |
6155 | | * |
6156 | | * Returns the new state or NULL in case of error |
6157 | | */ |
6158 | | xmlAutomataStatePtr |
6159 | 0 | xmlAutomataNewState(xmlAutomataPtr am) { |
6160 | 0 | if (am == NULL) |
6161 | 0 | return(NULL); |
6162 | 0 | return(xmlRegStatePush(am)); |
6163 | 0 | } |
6164 | | |
6165 | | /** |
6166 | | * xmlAutomataNewEpsilon: |
6167 | | * @am: an automata |
6168 | | * @from: the starting point of the transition |
6169 | | * @to: the target point of the transition or NULL |
6170 | | * |
6171 | | * If @to is NULL, this creates first a new target state in the automata |
6172 | | * and then adds an epsilon transition from the @from state to the |
6173 | | * target state |
6174 | | * |
6175 | | * Returns the target state or NULL in case of error |
6176 | | */ |
6177 | | xmlAutomataStatePtr |
6178 | | xmlAutomataNewEpsilon(xmlAutomataPtr am, xmlAutomataStatePtr from, |
6179 | 0 | xmlAutomataStatePtr to) { |
6180 | 0 | if ((am == NULL) || (from == NULL)) |
6181 | 0 | return(NULL); |
6182 | 0 | xmlFAGenerateEpsilonTransition(am, from, to); |
6183 | 0 | if (to == NULL) |
6184 | 0 | return(am->state); |
6185 | 0 | return(to); |
6186 | 0 | } |
6187 | | |
6188 | | /** |
6189 | | * xmlAutomataNewAllTrans: |
6190 | | * @am: an automata |
6191 | | * @from: the starting point of the transition |
6192 | | * @to: the target point of the transition or NULL |
6193 | | * @lax: allow to transition if not all all transitions have been activated |
6194 | | * |
6195 | | * If @to is NULL, this creates first a new target state in the automata |
6196 | | * and then adds a an ALL transition from the @from state to the |
6197 | | * target state. That transition is an epsilon transition allowed only when |
6198 | | * all transitions from the @from node have been activated. |
6199 | | * |
6200 | | * Returns the target state or NULL in case of error |
6201 | | */ |
6202 | | xmlAutomataStatePtr |
6203 | | xmlAutomataNewAllTrans(xmlAutomataPtr am, xmlAutomataStatePtr from, |
6204 | 0 | xmlAutomataStatePtr to, int lax) { |
6205 | 0 | if ((am == NULL) || (from == NULL)) |
6206 | 0 | return(NULL); |
6207 | 0 | xmlFAGenerateAllTransition(am, from, to, lax); |
6208 | 0 | if (to == NULL) |
6209 | 0 | return(am->state); |
6210 | 0 | return(to); |
6211 | 0 | } |
6212 | | |
6213 | | /** |
6214 | | * xmlAutomataNewCounter: |
6215 | | * @am: an automata |
6216 | | * @min: the minimal value on the counter |
6217 | | * @max: the maximal value on the counter |
6218 | | * |
6219 | | * Create a new counter |
6220 | | * |
6221 | | * Returns the counter number or -1 in case of error |
6222 | | */ |
6223 | | int |
6224 | 0 | xmlAutomataNewCounter(xmlAutomataPtr am, int min, int max) { |
6225 | 0 | int ret; |
6226 | |
|
6227 | 0 | if (am == NULL) |
6228 | 0 | return(-1); |
6229 | | |
6230 | 0 | ret = xmlRegGetCounter(am); |
6231 | 0 | if (ret < 0) |
6232 | 0 | return(-1); |
6233 | 0 | am->counters[ret].min = min; |
6234 | 0 | am->counters[ret].max = max; |
6235 | 0 | return(ret); |
6236 | 0 | } |
6237 | | |
6238 | | /** |
6239 | | * xmlAutomataNewCountedTrans: |
6240 | | * @am: an automata |
6241 | | * @from: the starting point of the transition |
6242 | | * @to: the target point of the transition or NULL |
6243 | | * @counter: the counter associated to that transition |
6244 | | * |
6245 | | * If @to is NULL, this creates first a new target state in the automata |
6246 | | * and then adds an epsilon transition from the @from state to the target state |
6247 | | * which will increment the counter provided |
6248 | | * |
6249 | | * Returns the target state or NULL in case of error |
6250 | | */ |
6251 | | xmlAutomataStatePtr |
6252 | | xmlAutomataNewCountedTrans(xmlAutomataPtr am, xmlAutomataStatePtr from, |
6253 | 0 | xmlAutomataStatePtr to, int counter) { |
6254 | 0 | if ((am == NULL) || (from == NULL) || (counter < 0)) |
6255 | 0 | return(NULL); |
6256 | 0 | xmlFAGenerateCountedEpsilonTransition(am, from, to, counter); |
6257 | 0 | if (to == NULL) |
6258 | 0 | return(am->state); |
6259 | 0 | return(to); |
6260 | 0 | } |
6261 | | |
6262 | | /** |
6263 | | * xmlAutomataNewCounterTrans: |
6264 | | * @am: an automata |
6265 | | * @from: the starting point of the transition |
6266 | | * @to: the target point of the transition or NULL |
6267 | | * @counter: the counter associated to that transition |
6268 | | * |
6269 | | * If @to is NULL, this creates first a new target state in the automata |
6270 | | * and then adds an epsilon transition from the @from state to the target state |
6271 | | * which will be allowed only if the counter is within the right range. |
6272 | | * |
6273 | | * Returns the target state or NULL in case of error |
6274 | | */ |
6275 | | xmlAutomataStatePtr |
6276 | | xmlAutomataNewCounterTrans(xmlAutomataPtr am, xmlAutomataStatePtr from, |
6277 | 0 | xmlAutomataStatePtr to, int counter) { |
6278 | 0 | if ((am == NULL) || (from == NULL) || (counter < 0)) |
6279 | 0 | return(NULL); |
6280 | 0 | xmlFAGenerateCountedTransition(am, from, to, counter); |
6281 | 0 | if (to == NULL) |
6282 | 0 | return(am->state); |
6283 | 0 | return(to); |
6284 | 0 | } |
6285 | | |
6286 | | /** |
6287 | | * xmlAutomataCompile: |
6288 | | * @am: an automata |
6289 | | * |
6290 | | * Compile the automata into a Reg Exp ready for being executed. |
6291 | | * The automata should be free after this point. |
6292 | | * |
6293 | | * Returns the compiled regexp or NULL in case of error |
6294 | | */ |
6295 | | xmlRegexpPtr |
6296 | 0 | xmlAutomataCompile(xmlAutomataPtr am) { |
6297 | 0 | xmlRegexpPtr ret; |
6298 | |
|
6299 | 0 | if ((am == NULL) || (am->error != 0)) return(NULL); |
6300 | 0 | xmlFAEliminateEpsilonTransitions(am); |
6301 | 0 | if (am->error != 0) |
6302 | 0 | return(NULL); |
6303 | | /* xmlFAComputesDeterminism(am); */ |
6304 | 0 | ret = xmlRegEpxFromParse(am); |
6305 | |
|
6306 | 0 | return(ret); |
6307 | 0 | } |
6308 | | |
6309 | | /** |
6310 | | * xmlAutomataIsDeterminist: |
6311 | | * @am: an automata |
6312 | | * |
6313 | | * Checks if an automata is determinist. |
6314 | | * |
6315 | | * Returns 1 if true, 0 if not, and -1 in case of error |
6316 | | */ |
6317 | | int |
6318 | 0 | xmlAutomataIsDeterminist(xmlAutomataPtr am) { |
6319 | 0 | int ret; |
6320 | |
|
6321 | 0 | if (am == NULL) |
6322 | 0 | return(-1); |
6323 | | |
6324 | 0 | ret = xmlFAComputesDeterminism(am); |
6325 | 0 | return(ret); |
6326 | 0 | } |
6327 | | |
6328 | | #ifdef LIBXML_EXPR_ENABLED |
6329 | | /** DOC_DISABLE */ |
6330 | | /************************************************************************ |
6331 | | * * |
6332 | | * Formal Expression handling code * |
6333 | | * * |
6334 | | ************************************************************************/ |
6335 | | |
6336 | | /* |
6337 | | * Formal regular expression handling |
6338 | | * Its goal is to do some formal work on content models |
6339 | | */ |
6340 | | |
6341 | | /* expressions are used within a context */ |
6342 | | typedef struct _xmlExpCtxt xmlExpCtxt; |
6343 | | typedef xmlExpCtxt *xmlExpCtxtPtr; |
6344 | | |
6345 | | XMLPUBFUN void |
6346 | | xmlExpFreeCtxt (xmlExpCtxtPtr ctxt); |
6347 | | XMLPUBFUN xmlExpCtxtPtr |
6348 | | xmlExpNewCtxt (int maxNodes, |
6349 | | xmlDictPtr dict); |
6350 | | |
6351 | | XMLPUBFUN int |
6352 | | xmlExpCtxtNbNodes(xmlExpCtxtPtr ctxt); |
6353 | | XMLPUBFUN int |
6354 | | xmlExpCtxtNbCons(xmlExpCtxtPtr ctxt); |
6355 | | |
6356 | | /* Expressions are trees but the tree is opaque */ |
6357 | | typedef struct _xmlExpNode xmlExpNode; |
6358 | | typedef xmlExpNode *xmlExpNodePtr; |
6359 | | |
6360 | | typedef enum { |
6361 | | XML_EXP_EMPTY = 0, |
6362 | | XML_EXP_FORBID = 1, |
6363 | | XML_EXP_ATOM = 2, |
6364 | | XML_EXP_SEQ = 3, |
6365 | | XML_EXP_OR = 4, |
6366 | | XML_EXP_COUNT = 5 |
6367 | | } xmlExpNodeType; |
6368 | | |
6369 | | /* |
6370 | | * 2 core expressions shared by all for the empty language set |
6371 | | * and for the set with just the empty token |
6372 | | */ |
6373 | | XMLPUBVAR xmlExpNodePtr forbiddenExp; |
6374 | | XMLPUBVAR xmlExpNodePtr emptyExp; |
6375 | | |
6376 | | /* |
6377 | | * Expressions are reference counted internally |
6378 | | */ |
6379 | | XMLPUBFUN void |
6380 | | xmlExpFree (xmlExpCtxtPtr ctxt, |
6381 | | xmlExpNodePtr expr); |
6382 | | XMLPUBFUN void |
6383 | | xmlExpRef (xmlExpNodePtr expr); |
6384 | | |
6385 | | /* |
6386 | | * constructors can be either manual or from a string |
6387 | | */ |
6388 | | XMLPUBFUN xmlExpNodePtr |
6389 | | xmlExpParse (xmlExpCtxtPtr ctxt, |
6390 | | const char *expr); |
6391 | | XMLPUBFUN xmlExpNodePtr |
6392 | | xmlExpNewAtom (xmlExpCtxtPtr ctxt, |
6393 | | const xmlChar *name, |
6394 | | int len); |
6395 | | XMLPUBFUN xmlExpNodePtr |
6396 | | xmlExpNewOr (xmlExpCtxtPtr ctxt, |
6397 | | xmlExpNodePtr left, |
6398 | | xmlExpNodePtr right); |
6399 | | XMLPUBFUN xmlExpNodePtr |
6400 | | xmlExpNewSeq (xmlExpCtxtPtr ctxt, |
6401 | | xmlExpNodePtr left, |
6402 | | xmlExpNodePtr right); |
6403 | | XMLPUBFUN xmlExpNodePtr |
6404 | | xmlExpNewRange (xmlExpCtxtPtr ctxt, |
6405 | | xmlExpNodePtr subset, |
6406 | | int min, |
6407 | | int max); |
6408 | | /* |
6409 | | * The really interesting APIs |
6410 | | */ |
6411 | | XMLPUBFUN int |
6412 | | xmlExpIsNillable(xmlExpNodePtr expr); |
6413 | | XMLPUBFUN int |
6414 | | xmlExpMaxToken (xmlExpNodePtr expr); |
6415 | | XMLPUBFUN int |
6416 | | xmlExpGetLanguage(xmlExpCtxtPtr ctxt, |
6417 | | xmlExpNodePtr expr, |
6418 | | const xmlChar**langList, |
6419 | | int len); |
6420 | | XMLPUBFUN int |
6421 | | xmlExpGetStart (xmlExpCtxtPtr ctxt, |
6422 | | xmlExpNodePtr expr, |
6423 | | const xmlChar**tokList, |
6424 | | int len); |
6425 | | XMLPUBFUN xmlExpNodePtr |
6426 | | xmlExpStringDerive(xmlExpCtxtPtr ctxt, |
6427 | | xmlExpNodePtr expr, |
6428 | | const xmlChar *str, |
6429 | | int len); |
6430 | | XMLPUBFUN xmlExpNodePtr |
6431 | | xmlExpExpDerive (xmlExpCtxtPtr ctxt, |
6432 | | xmlExpNodePtr expr, |
6433 | | xmlExpNodePtr sub); |
6434 | | XMLPUBFUN int |
6435 | | xmlExpSubsume (xmlExpCtxtPtr ctxt, |
6436 | | xmlExpNodePtr expr, |
6437 | | xmlExpNodePtr sub); |
6438 | | XMLPUBFUN void |
6439 | | xmlExpDump (xmlBufferPtr buf, |
6440 | | xmlExpNodePtr expr); |
6441 | | |
6442 | | /************************************************************************ |
6443 | | * * |
6444 | | * Expression handling context * |
6445 | | * * |
6446 | | ************************************************************************/ |
6447 | | |
6448 | | struct _xmlExpCtxt { |
6449 | | xmlDictPtr dict; |
6450 | | xmlExpNodePtr *table; |
6451 | | int size; |
6452 | | int nbElems; |
6453 | | int nb_nodes; |
6454 | | int maxNodes; |
6455 | | const char *expr; |
6456 | | const char *cur; |
6457 | | int nb_cons; |
6458 | | int tabSize; |
6459 | | }; |
6460 | | |
6461 | | /** |
6462 | | * xmlExpNewCtxt: |
6463 | | * @maxNodes: the maximum number of nodes |
6464 | | * @dict: optional dictionary to use internally |
6465 | | * |
6466 | | * Creates a new context for manipulating expressions |
6467 | | * |
6468 | | * Returns the context or NULL in case of error |
6469 | | */ |
6470 | | xmlExpCtxtPtr |
6471 | | xmlExpNewCtxt(int maxNodes, xmlDictPtr dict) { |
6472 | | xmlExpCtxtPtr ret; |
6473 | | int size = 256; |
6474 | | |
6475 | | if (maxNodes <= 4096) |
6476 | | maxNodes = 4096; |
6477 | | |
6478 | | ret = (xmlExpCtxtPtr) xmlMalloc(sizeof(xmlExpCtxt)); |
6479 | | if (ret == NULL) |
6480 | | return(NULL); |
6481 | | memset(ret, 0, sizeof(xmlExpCtxt)); |
6482 | | ret->size = size; |
6483 | | ret->nbElems = 0; |
6484 | | ret->maxNodes = maxNodes; |
6485 | | ret->table = xmlMalloc(size * sizeof(xmlExpNodePtr)); |
6486 | | if (ret->table == NULL) { |
6487 | | xmlFree(ret); |
6488 | | return(NULL); |
6489 | | } |
6490 | | memset(ret->table, 0, size * sizeof(xmlExpNodePtr)); |
6491 | | if (dict == NULL) { |
6492 | | ret->dict = xmlDictCreate(); |
6493 | | if (ret->dict == NULL) { |
6494 | | xmlFree(ret->table); |
6495 | | xmlFree(ret); |
6496 | | return(NULL); |
6497 | | } |
6498 | | } else { |
6499 | | ret->dict = dict; |
6500 | | xmlDictReference(ret->dict); |
6501 | | } |
6502 | | return(ret); |
6503 | | } |
6504 | | |
6505 | | /** |
6506 | | * xmlExpFreeCtxt: |
6507 | | * @ctxt: an expression context |
6508 | | * |
6509 | | * Free an expression context |
6510 | | */ |
6511 | | void |
6512 | | xmlExpFreeCtxt(xmlExpCtxtPtr ctxt) { |
6513 | | if (ctxt == NULL) |
6514 | | return; |
6515 | | xmlDictFree(ctxt->dict); |
6516 | | if (ctxt->table != NULL) |
6517 | | xmlFree(ctxt->table); |
6518 | | xmlFree(ctxt); |
6519 | | } |
6520 | | |
6521 | | /************************************************************************ |
6522 | | * * |
6523 | | * Structure associated to an expression node * |
6524 | | * * |
6525 | | ************************************************************************/ |
6526 | | #define MAX_NODES 10000 |
6527 | | |
6528 | | /* |
6529 | | * TODO: |
6530 | | * - Wildcards |
6531 | | * - public API for creation |
6532 | | * |
6533 | | * Started |
6534 | | * - regression testing |
6535 | | * |
6536 | | * Done |
6537 | | * - split into module and test tool |
6538 | | * - memleaks |
6539 | | */ |
6540 | | |
6541 | | typedef enum { |
6542 | | XML_EXP_NILABLE = (1 << 0) |
6543 | | } xmlExpNodeInfo; |
6544 | | |
6545 | | #define IS_NILLABLE(node) ((node)->info & XML_EXP_NILABLE) |
6546 | | |
6547 | | struct _xmlExpNode { |
6548 | | unsigned char type;/* xmlExpNodeType */ |
6549 | | unsigned char info;/* OR of xmlExpNodeInfo */ |
6550 | | unsigned short key; /* the hash key */ |
6551 | | unsigned int ref; /* The number of references */ |
6552 | | int c_max; /* the maximum length it can consume */ |
6553 | | xmlExpNodePtr exp_left; |
6554 | | xmlExpNodePtr next;/* the next node in the hash table or free list */ |
6555 | | union { |
6556 | | struct { |
6557 | | int f_min; |
6558 | | int f_max; |
6559 | | } count; |
6560 | | struct { |
6561 | | xmlExpNodePtr f_right; |
6562 | | } children; |
6563 | | const xmlChar *f_str; |
6564 | | } field; |
6565 | | }; |
6566 | | |
6567 | | #define exp_min field.count.f_min |
6568 | | #define exp_max field.count.f_max |
6569 | | /* #define exp_left field.children.f_left */ |
6570 | | #define exp_right field.children.f_right |
6571 | | #define exp_str field.f_str |
6572 | | |
6573 | | static xmlExpNodePtr xmlExpNewNode(xmlExpCtxtPtr ctxt, xmlExpNodeType type); |
6574 | | static xmlExpNode forbiddenExpNode = { |
6575 | | XML_EXP_FORBID, 0, 0, 0, 0, NULL, NULL, {{ 0, 0}} |
6576 | | }; |
6577 | | xmlExpNodePtr forbiddenExp = &forbiddenExpNode; |
6578 | | static xmlExpNode emptyExpNode = { |
6579 | | XML_EXP_EMPTY, 1, 0, 0, 0, NULL, NULL, {{ 0, 0}} |
6580 | | }; |
6581 | | xmlExpNodePtr emptyExp = &emptyExpNode; |
6582 | | |
6583 | | /************************************************************************ |
6584 | | * * |
6585 | | * The custom hash table for unicity and canonicalization * |
6586 | | * of sub-expressions pointers * |
6587 | | * * |
6588 | | ************************************************************************/ |
6589 | | /* |
6590 | | * xmlExpHashNameComputeKey: |
6591 | | * Calculate the hash key for a token |
6592 | | */ |
6593 | | static unsigned short |
6594 | | xmlExpHashNameComputeKey(const xmlChar *name) { |
6595 | | unsigned short value = 0L; |
6596 | | char ch; |
6597 | | |
6598 | | if (name != NULL) { |
6599 | | value += 30 * (*name); |
6600 | | while ((ch = *name++) != 0) { |
6601 | | value = value ^ ((value << 5) + (value >> 3) + (unsigned long)ch); |
6602 | | } |
6603 | | } |
6604 | | return (value); |
6605 | | } |
6606 | | |
6607 | | /* |
6608 | | * xmlExpHashComputeKey: |
6609 | | * Calculate the hash key for a compound expression |
6610 | | */ |
6611 | | static unsigned short |
6612 | | xmlExpHashComputeKey(xmlExpNodeType type, xmlExpNodePtr left, |
6613 | | xmlExpNodePtr right) { |
6614 | | unsigned long value; |
6615 | | unsigned short ret; |
6616 | | |
6617 | | switch (type) { |
6618 | | case XML_EXP_SEQ: |
6619 | | value = left->key; |
6620 | | value += right->key; |
6621 | | value *= 3; |
6622 | | ret = (unsigned short) value; |
6623 | | break; |
6624 | | case XML_EXP_OR: |
6625 | | value = left->key; |
6626 | | value += right->key; |
6627 | | value *= 7; |
6628 | | ret = (unsigned short) value; |
6629 | | break; |
6630 | | case XML_EXP_COUNT: |
6631 | | value = left->key; |
6632 | | value += right->key; |
6633 | | ret = (unsigned short) value; |
6634 | | break; |
6635 | | default: |
6636 | | ret = 0; |
6637 | | } |
6638 | | return(ret); |
6639 | | } |
6640 | | |
6641 | | |
6642 | | static xmlExpNodePtr |
6643 | | xmlExpNewNode(xmlExpCtxtPtr ctxt, xmlExpNodeType type) { |
6644 | | xmlExpNodePtr ret; |
6645 | | |
6646 | | if (ctxt->nb_nodes >= MAX_NODES) |
6647 | | return(NULL); |
6648 | | ret = (xmlExpNodePtr) xmlMalloc(sizeof(xmlExpNode)); |
6649 | | if (ret == NULL) |
6650 | | return(NULL); |
6651 | | memset(ret, 0, sizeof(xmlExpNode)); |
6652 | | ret->type = type; |
6653 | | ret->next = NULL; |
6654 | | ctxt->nb_nodes++; |
6655 | | ctxt->nb_cons++; |
6656 | | return(ret); |
6657 | | } |
6658 | | |
6659 | | /** |
6660 | | * xmlExpHashGetEntry: |
6661 | | * @table: the hash table |
6662 | | * |
6663 | | * Get the unique entry from the hash table. The entry is created if |
6664 | | * needed. @left and @right are consumed, i.e. their ref count will |
6665 | | * be decremented by the operation. |
6666 | | * |
6667 | | * Returns the pointer or NULL in case of error |
6668 | | */ |
6669 | | static xmlExpNodePtr |
6670 | | xmlExpHashGetEntry(xmlExpCtxtPtr ctxt, xmlExpNodeType type, |
6671 | | xmlExpNodePtr left, xmlExpNodePtr right, |
6672 | | const xmlChar *name, int min, int max) { |
6673 | | unsigned short kbase, key; |
6674 | | xmlExpNodePtr entry; |
6675 | | xmlExpNodePtr insert; |
6676 | | |
6677 | | if (ctxt == NULL) |
6678 | | return(NULL); |
6679 | | |
6680 | | /* |
6681 | | * Check for duplicate and insertion location. |
6682 | | */ |
6683 | | if (type == XML_EXP_ATOM) { |
6684 | | kbase = xmlExpHashNameComputeKey(name); |
6685 | | } else if (type == XML_EXP_COUNT) { |
6686 | | /* COUNT reduction rule 1 */ |
6687 | | /* a{1} -> a */ |
6688 | | if (min == max) { |
6689 | | if (min == 1) { |
6690 | | return(left); |
6691 | | } |
6692 | | if (min == 0) { |
6693 | | xmlExpFree(ctxt, left); |
6694 | | return(emptyExp); |
6695 | | } |
6696 | | } |
6697 | | if (min < 0) { |
6698 | | xmlExpFree(ctxt, left); |
6699 | | return(forbiddenExp); |
6700 | | } |
6701 | | if (max == -1) |
6702 | | kbase = min + 79; |
6703 | | else |
6704 | | kbase = max - min; |
6705 | | kbase += left->key; |
6706 | | } else if (type == XML_EXP_OR) { |
6707 | | /* Forbid reduction rules */ |
6708 | | if (left->type == XML_EXP_FORBID) { |
6709 | | xmlExpFree(ctxt, left); |
6710 | | return(right); |
6711 | | } |
6712 | | if (right->type == XML_EXP_FORBID) { |
6713 | | xmlExpFree(ctxt, right); |
6714 | | return(left); |
6715 | | } |
6716 | | |
6717 | | /* OR reduction rule 1 */ |
6718 | | /* a | a reduced to a */ |
6719 | | if (left == right) { |
6720 | | xmlExpFree(ctxt, right); |
6721 | | return(left); |
6722 | | } |
6723 | | /* OR canonicalization rule 1 */ |
6724 | | /* linearize (a | b) | c into a | (b | c) */ |
6725 | | if ((left->type == XML_EXP_OR) && (right->type != XML_EXP_OR)) { |
6726 | | xmlExpNodePtr tmp = left; |
6727 | | left = right; |
6728 | | right = tmp; |
6729 | | } |
6730 | | /* OR reduction rule 2 */ |
6731 | | /* a | (a | b) and b | (a | b) are reduced to a | b */ |
6732 | | if (right->type == XML_EXP_OR) { |
6733 | | if ((left == right->exp_left) || |
6734 | | (left == right->exp_right)) { |
6735 | | xmlExpFree(ctxt, left); |
6736 | | return(right); |
6737 | | } |
6738 | | } |
6739 | | /* OR canonicalization rule 2 */ |
6740 | | /* linearize (a | b) | c into a | (b | c) */ |
6741 | | if (left->type == XML_EXP_OR) { |
6742 | | xmlExpNodePtr tmp; |
6743 | | |
6744 | | /* OR canonicalization rule 2 */ |
6745 | | if ((left->exp_right->type != XML_EXP_OR) && |
6746 | | (left->exp_right->key < left->exp_left->key)) { |
6747 | | tmp = left->exp_right; |
6748 | | left->exp_right = left->exp_left; |
6749 | | left->exp_left = tmp; |
6750 | | } |
6751 | | left->exp_right->ref++; |
6752 | | tmp = xmlExpHashGetEntry(ctxt, XML_EXP_OR, left->exp_right, right, |
6753 | | NULL, 0, 0); |
6754 | | left->exp_left->ref++; |
6755 | | tmp = xmlExpHashGetEntry(ctxt, XML_EXP_OR, left->exp_left, tmp, |
6756 | | NULL, 0, 0); |
6757 | | |
6758 | | xmlExpFree(ctxt, left); |
6759 | | return(tmp); |
6760 | | } |
6761 | | if (right->type == XML_EXP_OR) { |
6762 | | /* Ordering in the tree */ |
6763 | | /* C | (A | B) -> A | (B | C) */ |
6764 | | if (left->key > right->exp_right->key) { |
6765 | | xmlExpNodePtr tmp; |
6766 | | right->exp_right->ref++; |
6767 | | tmp = xmlExpHashGetEntry(ctxt, XML_EXP_OR, right->exp_right, |
6768 | | left, NULL, 0, 0); |
6769 | | right->exp_left->ref++; |
6770 | | tmp = xmlExpHashGetEntry(ctxt, XML_EXP_OR, right->exp_left, |
6771 | | tmp, NULL, 0, 0); |
6772 | | xmlExpFree(ctxt, right); |
6773 | | return(tmp); |
6774 | | } |
6775 | | /* Ordering in the tree */ |
6776 | | /* B | (A | C) -> A | (B | C) */ |
6777 | | if (left->key > right->exp_left->key) { |
6778 | | xmlExpNodePtr tmp; |
6779 | | right->exp_right->ref++; |
6780 | | tmp = xmlExpHashGetEntry(ctxt, XML_EXP_OR, left, |
6781 | | right->exp_right, NULL, 0, 0); |
6782 | | right->exp_left->ref++; |
6783 | | tmp = xmlExpHashGetEntry(ctxt, XML_EXP_OR, right->exp_left, |
6784 | | tmp, NULL, 0, 0); |
6785 | | xmlExpFree(ctxt, right); |
6786 | | return(tmp); |
6787 | | } |
6788 | | } |
6789 | | /* we know both types are != XML_EXP_OR here */ |
6790 | | else if (left->key > right->key) { |
6791 | | xmlExpNodePtr tmp = left; |
6792 | | left = right; |
6793 | | right = tmp; |
6794 | | } |
6795 | | kbase = xmlExpHashComputeKey(type, left, right); |
6796 | | } else if (type == XML_EXP_SEQ) { |
6797 | | /* Forbid reduction rules */ |
6798 | | if (left->type == XML_EXP_FORBID) { |
6799 | | xmlExpFree(ctxt, right); |
6800 | | return(left); |
6801 | | } |
6802 | | if (right->type == XML_EXP_FORBID) { |
6803 | | xmlExpFree(ctxt, left); |
6804 | | return(right); |
6805 | | } |
6806 | | /* Empty reduction rules */ |
6807 | | if (right->type == XML_EXP_EMPTY) { |
6808 | | return(left); |
6809 | | } |
6810 | | if (left->type == XML_EXP_EMPTY) { |
6811 | | return(right); |
6812 | | } |
6813 | | kbase = xmlExpHashComputeKey(type, left, right); |
6814 | | } else |
6815 | | return(NULL); |
6816 | | |
6817 | | key = kbase % ctxt->size; |
6818 | | if (ctxt->table[key] != NULL) { |
6819 | | for (insert = ctxt->table[key]; insert != NULL; |
6820 | | insert = insert->next) { |
6821 | | if ((insert->key == kbase) && |
6822 | | (insert->type == type)) { |
6823 | | if (type == XML_EXP_ATOM) { |
6824 | | if (name == insert->exp_str) { |
6825 | | insert->ref++; |
6826 | | return(insert); |
6827 | | } |
6828 | | } else if (type == XML_EXP_COUNT) { |
6829 | | if ((insert->exp_min == min) && (insert->exp_max == max) && |
6830 | | (insert->exp_left == left)) { |
6831 | | insert->ref++; |
6832 | | left->ref--; |
6833 | | return(insert); |
6834 | | } |
6835 | | } else if ((insert->exp_left == left) && |
6836 | | (insert->exp_right == right)) { |
6837 | | insert->ref++; |
6838 | | left->ref--; |
6839 | | right->ref--; |
6840 | | return(insert); |
6841 | | } |
6842 | | } |
6843 | | } |
6844 | | } |
6845 | | |
6846 | | entry = xmlExpNewNode(ctxt, type); |
6847 | | if (entry == NULL) |
6848 | | return(NULL); |
6849 | | entry->key = kbase; |
6850 | | if (type == XML_EXP_ATOM) { |
6851 | | entry->exp_str = name; |
6852 | | entry->c_max = 1; |
6853 | | } else if (type == XML_EXP_COUNT) { |
6854 | | entry->exp_min = min; |
6855 | | entry->exp_max = max; |
6856 | | entry->exp_left = left; |
6857 | | if ((min == 0) || (IS_NILLABLE(left))) |
6858 | | entry->info |= XML_EXP_NILABLE; |
6859 | | if (max < 0) |
6860 | | entry->c_max = -1; |
6861 | | else |
6862 | | entry->c_max = max * entry->exp_left->c_max; |
6863 | | } else { |
6864 | | entry->exp_left = left; |
6865 | | entry->exp_right = right; |
6866 | | if (type == XML_EXP_OR) { |
6867 | | if ((IS_NILLABLE(left)) || (IS_NILLABLE(right))) |
6868 | | entry->info |= XML_EXP_NILABLE; |
6869 | | if ((entry->exp_left->c_max == -1) || |
6870 | | (entry->exp_right->c_max == -1)) |
6871 | | entry->c_max = -1; |
6872 | | else if (entry->exp_left->c_max > entry->exp_right->c_max) |
6873 | | entry->c_max = entry->exp_left->c_max; |
6874 | | else |
6875 | | entry->c_max = entry->exp_right->c_max; |
6876 | | } else { |
6877 | | if ((IS_NILLABLE(left)) && (IS_NILLABLE(right))) |
6878 | | entry->info |= XML_EXP_NILABLE; |
6879 | | if ((entry->exp_left->c_max == -1) || |
6880 | | (entry->exp_right->c_max == -1)) |
6881 | | entry->c_max = -1; |
6882 | | else |
6883 | | entry->c_max = entry->exp_left->c_max + entry->exp_right->c_max; |
6884 | | } |
6885 | | } |
6886 | | entry->ref = 1; |
6887 | | if (ctxt->table[key] != NULL) |
6888 | | entry->next = ctxt->table[key]; |
6889 | | |
6890 | | ctxt->table[key] = entry; |
6891 | | ctxt->nbElems++; |
6892 | | |
6893 | | return(entry); |
6894 | | } |
6895 | | |
6896 | | /** |
6897 | | * xmlExpFree: |
6898 | | * @ctxt: the expression context |
6899 | | * @exp: the expression |
6900 | | * |
6901 | | * Dereference the expression |
6902 | | */ |
6903 | | void |
6904 | | xmlExpFree(xmlExpCtxtPtr ctxt, xmlExpNodePtr exp) { |
6905 | | if ((exp == NULL) || (exp == forbiddenExp) || (exp == emptyExp)) |
6906 | | return; |
6907 | | exp->ref--; |
6908 | | if (exp->ref == 0) { |
6909 | | unsigned short key; |
6910 | | |
6911 | | /* Unlink it first from the hash table */ |
6912 | | key = exp->key % ctxt->size; |
6913 | | if (ctxt->table[key] == exp) { |
6914 | | ctxt->table[key] = exp->next; |
6915 | | } else { |
6916 | | xmlExpNodePtr tmp; |
6917 | | |
6918 | | tmp = ctxt->table[key]; |
6919 | | while (tmp != NULL) { |
6920 | | if (tmp->next == exp) { |
6921 | | tmp->next = exp->next; |
6922 | | break; |
6923 | | } |
6924 | | tmp = tmp->next; |
6925 | | } |
6926 | | } |
6927 | | |
6928 | | if ((exp->type == XML_EXP_SEQ) || (exp->type == XML_EXP_OR)) { |
6929 | | xmlExpFree(ctxt, exp->exp_left); |
6930 | | xmlExpFree(ctxt, exp->exp_right); |
6931 | | } else if (exp->type == XML_EXP_COUNT) { |
6932 | | xmlExpFree(ctxt, exp->exp_left); |
6933 | | } |
6934 | | xmlFree(exp); |
6935 | | ctxt->nb_nodes--; |
6936 | | } |
6937 | | } |
6938 | | |
6939 | | /** |
6940 | | * xmlExpRef: |
6941 | | * @exp: the expression |
6942 | | * |
6943 | | * Increase the reference count of the expression |
6944 | | */ |
6945 | | void |
6946 | | xmlExpRef(xmlExpNodePtr exp) { |
6947 | | if (exp != NULL) |
6948 | | exp->ref++; |
6949 | | } |
6950 | | |
6951 | | /** |
6952 | | * xmlExpNewAtom: |
6953 | | * @ctxt: the expression context |
6954 | | * @name: the atom name |
6955 | | * @len: the atom name length in byte (or -1); |
6956 | | * |
6957 | | * Get the atom associated to this name from that context |
6958 | | * |
6959 | | * Returns the node or NULL in case of error |
6960 | | */ |
6961 | | xmlExpNodePtr |
6962 | | xmlExpNewAtom(xmlExpCtxtPtr ctxt, const xmlChar *name, int len) { |
6963 | | if ((ctxt == NULL) || (name == NULL)) |
6964 | | return(NULL); |
6965 | | name = xmlDictLookup(ctxt->dict, name, len); |
6966 | | if (name == NULL) |
6967 | | return(NULL); |
6968 | | return(xmlExpHashGetEntry(ctxt, XML_EXP_ATOM, NULL, NULL, name, 0, 0)); |
6969 | | } |
6970 | | |
6971 | | /** |
6972 | | * xmlExpNewOr: |
6973 | | * @ctxt: the expression context |
6974 | | * @left: left expression |
6975 | | * @right: right expression |
6976 | | * |
6977 | | * Get the atom associated to the choice @left | @right |
6978 | | * Note that @left and @right are consumed in the operation, to keep |
6979 | | * an handle on them use xmlExpRef() and use xmlExpFree() to release them, |
6980 | | * this is true even in case of failure (unless ctxt == NULL). |
6981 | | * |
6982 | | * Returns the node or NULL in case of error |
6983 | | */ |
6984 | | xmlExpNodePtr |
6985 | | xmlExpNewOr(xmlExpCtxtPtr ctxt, xmlExpNodePtr left, xmlExpNodePtr right) { |
6986 | | if (ctxt == NULL) |
6987 | | return(NULL); |
6988 | | if ((left == NULL) || (right == NULL)) { |
6989 | | xmlExpFree(ctxt, left); |
6990 | | xmlExpFree(ctxt, right); |
6991 | | return(NULL); |
6992 | | } |
6993 | | return(xmlExpHashGetEntry(ctxt, XML_EXP_OR, left, right, NULL, 0, 0)); |
6994 | | } |
6995 | | |
6996 | | /** |
6997 | | * xmlExpNewSeq: |
6998 | | * @ctxt: the expression context |
6999 | | * @left: left expression |
7000 | | * @right: right expression |
7001 | | * |
7002 | | * Get the atom associated to the sequence @left , @right |
7003 | | * Note that @left and @right are consumed in the operation, to keep |
7004 | | * an handle on them use xmlExpRef() and use xmlExpFree() to release them, |
7005 | | * this is true even in case of failure (unless ctxt == NULL). |
7006 | | * |
7007 | | * Returns the node or NULL in case of error |
7008 | | */ |
7009 | | xmlExpNodePtr |
7010 | | xmlExpNewSeq(xmlExpCtxtPtr ctxt, xmlExpNodePtr left, xmlExpNodePtr right) { |
7011 | | if (ctxt == NULL) |
7012 | | return(NULL); |
7013 | | if ((left == NULL) || (right == NULL)) { |
7014 | | xmlExpFree(ctxt, left); |
7015 | | xmlExpFree(ctxt, right); |
7016 | | return(NULL); |
7017 | | } |
7018 | | return(xmlExpHashGetEntry(ctxt, XML_EXP_SEQ, left, right, NULL, 0, 0)); |
7019 | | } |
7020 | | |
7021 | | /** |
7022 | | * xmlExpNewRange: |
7023 | | * @ctxt: the expression context |
7024 | | * @subset: the expression to be repeated |
7025 | | * @min: the lower bound for the repetition |
7026 | | * @max: the upper bound for the repetition, -1 means infinite |
7027 | | * |
7028 | | * Get the atom associated to the range (@subset){@min, @max} |
7029 | | * Note that @subset is consumed in the operation, to keep |
7030 | | * an handle on it use xmlExpRef() and use xmlExpFree() to release it, |
7031 | | * this is true even in case of failure (unless ctxt == NULL). |
7032 | | * |
7033 | | * Returns the node or NULL in case of error |
7034 | | */ |
7035 | | xmlExpNodePtr |
7036 | | xmlExpNewRange(xmlExpCtxtPtr ctxt, xmlExpNodePtr subset, int min, int max) { |
7037 | | if (ctxt == NULL) |
7038 | | return(NULL); |
7039 | | if ((subset == NULL) || (min < 0) || (max < -1) || |
7040 | | ((max >= 0) && (min > max))) { |
7041 | | xmlExpFree(ctxt, subset); |
7042 | | return(NULL); |
7043 | | } |
7044 | | return(xmlExpHashGetEntry(ctxt, XML_EXP_COUNT, subset, |
7045 | | NULL, NULL, min, max)); |
7046 | | } |
7047 | | |
7048 | | /************************************************************************ |
7049 | | * * |
7050 | | * Public API for operations on expressions * |
7051 | | * * |
7052 | | ************************************************************************/ |
7053 | | |
7054 | | static int |
7055 | | xmlExpGetLanguageInt(xmlExpCtxtPtr ctxt, xmlExpNodePtr exp, |
7056 | | const xmlChar**list, int len, int nb) { |
7057 | | int tmp, tmp2; |
7058 | | tail: |
7059 | | switch (exp->type) { |
7060 | | case XML_EXP_EMPTY: |
7061 | | return(0); |
7062 | | case XML_EXP_ATOM: |
7063 | | for (tmp = 0;tmp < nb;tmp++) |
7064 | | if (list[tmp] == exp->exp_str) |
7065 | | return(0); |
7066 | | if (nb >= len) |
7067 | | return(-2); |
7068 | | list[nb] = exp->exp_str; |
7069 | | return(1); |
7070 | | case XML_EXP_COUNT: |
7071 | | exp = exp->exp_left; |
7072 | | goto tail; |
7073 | | case XML_EXP_SEQ: |
7074 | | case XML_EXP_OR: |
7075 | | tmp = xmlExpGetLanguageInt(ctxt, exp->exp_left, list, len, nb); |
7076 | | if (tmp < 0) |
7077 | | return(tmp); |
7078 | | tmp2 = xmlExpGetLanguageInt(ctxt, exp->exp_right, list, len, |
7079 | | nb + tmp); |
7080 | | if (tmp2 < 0) |
7081 | | return(tmp2); |
7082 | | return(tmp + tmp2); |
7083 | | } |
7084 | | return(-1); |
7085 | | } |
7086 | | |
7087 | | /** |
7088 | | * xmlExpGetLanguage: |
7089 | | * @ctxt: the expression context |
7090 | | * @exp: the expression |
7091 | | * @langList: where to store the tokens |
7092 | | * @len: the allocated length of @list |
7093 | | * |
7094 | | * Find all the strings used in @exp and store them in @list |
7095 | | * |
7096 | | * Returns the number of unique strings found, -1 in case of errors and |
7097 | | * -2 if there is more than @len strings |
7098 | | */ |
7099 | | int |
7100 | | xmlExpGetLanguage(xmlExpCtxtPtr ctxt, xmlExpNodePtr exp, |
7101 | | const xmlChar**langList, int len) { |
7102 | | if ((ctxt == NULL) || (exp == NULL) || (langList == NULL) || (len <= 0)) |
7103 | | return(-1); |
7104 | | return(xmlExpGetLanguageInt(ctxt, exp, langList, len, 0)); |
7105 | | } |
7106 | | |
7107 | | static int |
7108 | | xmlExpGetStartInt(xmlExpCtxtPtr ctxt, xmlExpNodePtr exp, |
7109 | | const xmlChar**list, int len, int nb) { |
7110 | | int tmp, tmp2; |
7111 | | tail: |
7112 | | switch (exp->type) { |
7113 | | case XML_EXP_FORBID: |
7114 | | return(0); |
7115 | | case XML_EXP_EMPTY: |
7116 | | return(0); |
7117 | | case XML_EXP_ATOM: |
7118 | | for (tmp = 0;tmp < nb;tmp++) |
7119 | | if (list[tmp] == exp->exp_str) |
7120 | | return(0); |
7121 | | if (nb >= len) |
7122 | | return(-2); |
7123 | | list[nb] = exp->exp_str; |
7124 | | return(1); |
7125 | | case XML_EXP_COUNT: |
7126 | | exp = exp->exp_left; |
7127 | | goto tail; |
7128 | | case XML_EXP_SEQ: |
7129 | | tmp = xmlExpGetStartInt(ctxt, exp->exp_left, list, len, nb); |
7130 | | if (tmp < 0) |
7131 | | return(tmp); |
7132 | | if (IS_NILLABLE(exp->exp_left)) { |
7133 | | tmp2 = xmlExpGetStartInt(ctxt, exp->exp_right, list, len, |
7134 | | nb + tmp); |
7135 | | if (tmp2 < 0) |
7136 | | return(tmp2); |
7137 | | tmp += tmp2; |
7138 | | } |
7139 | | return(tmp); |
7140 | | case XML_EXP_OR: |
7141 | | tmp = xmlExpGetStartInt(ctxt, exp->exp_left, list, len, nb); |
7142 | | if (tmp < 0) |
7143 | | return(tmp); |
7144 | | tmp2 = xmlExpGetStartInt(ctxt, exp->exp_right, list, len, |
7145 | | nb + tmp); |
7146 | | if (tmp2 < 0) |
7147 | | return(tmp2); |
7148 | | return(tmp + tmp2); |
7149 | | } |
7150 | | return(-1); |
7151 | | } |
7152 | | |
7153 | | /** |
7154 | | * xmlExpGetStart: |
7155 | | * @ctxt: the expression context |
7156 | | * @exp: the expression |
7157 | | * @tokList: where to store the tokens |
7158 | | * @len: the allocated length of @list |
7159 | | * |
7160 | | * Find all the strings that appears at the start of the languages |
7161 | | * accepted by @exp and store them in @list. E.g. for (a, b) | c |
7162 | | * it will return the list [a, c] |
7163 | | * |
7164 | | * Returns the number of unique strings found, -1 in case of errors and |
7165 | | * -2 if there is more than @len strings |
7166 | | */ |
7167 | | int |
7168 | | xmlExpGetStart(xmlExpCtxtPtr ctxt, xmlExpNodePtr exp, |
7169 | | const xmlChar**tokList, int len) { |
7170 | | if ((ctxt == NULL) || (exp == NULL) || (tokList == NULL) || (len <= 0)) |
7171 | | return(-1); |
7172 | | return(xmlExpGetStartInt(ctxt, exp, tokList, len, 0)); |
7173 | | } |
7174 | | |
7175 | | /** |
7176 | | * xmlExpIsNillable: |
7177 | | * @exp: the expression |
7178 | | * |
7179 | | * Finds if the expression is nillable, i.e. if it accepts the empty sequence |
7180 | | * |
7181 | | * Returns 1 if nillable, 0 if not and -1 in case of error |
7182 | | */ |
7183 | | int |
7184 | | xmlExpIsNillable(xmlExpNodePtr exp) { |
7185 | | if (exp == NULL) |
7186 | | return(-1); |
7187 | | return(IS_NILLABLE(exp) != 0); |
7188 | | } |
7189 | | |
7190 | | static xmlExpNodePtr |
7191 | | xmlExpStringDeriveInt(xmlExpCtxtPtr ctxt, xmlExpNodePtr exp, const xmlChar *str) |
7192 | | { |
7193 | | xmlExpNodePtr ret; |
7194 | | |
7195 | | switch (exp->type) { |
7196 | | case XML_EXP_EMPTY: |
7197 | | return(forbiddenExp); |
7198 | | case XML_EXP_FORBID: |
7199 | | return(forbiddenExp); |
7200 | | case XML_EXP_ATOM: |
7201 | | if (exp->exp_str == str) { |
7202 | | ret = emptyExp; |
7203 | | } else { |
7204 | | /* TODO wildcards here */ |
7205 | | ret = forbiddenExp; |
7206 | | } |
7207 | | return(ret); |
7208 | | case XML_EXP_OR: { |
7209 | | xmlExpNodePtr tmp; |
7210 | | |
7211 | | tmp = xmlExpStringDeriveInt(ctxt, exp->exp_left, str); |
7212 | | if (tmp == NULL) { |
7213 | | return(NULL); |
7214 | | } |
7215 | | ret = xmlExpStringDeriveInt(ctxt, exp->exp_right, str); |
7216 | | if (ret == NULL) { |
7217 | | xmlExpFree(ctxt, tmp); |
7218 | | return(NULL); |
7219 | | } |
7220 | | ret = xmlExpHashGetEntry(ctxt, XML_EXP_OR, tmp, ret, |
7221 | | NULL, 0, 0); |
7222 | | return(ret); |
7223 | | } |
7224 | | case XML_EXP_SEQ: |
7225 | | ret = xmlExpStringDeriveInt(ctxt, exp->exp_left, str); |
7226 | | if (ret == NULL) { |
7227 | | return(NULL); |
7228 | | } else if (ret == forbiddenExp) { |
7229 | | if (IS_NILLABLE(exp->exp_left)) { |
7230 | | ret = xmlExpStringDeriveInt(ctxt, exp->exp_right, str); |
7231 | | } |
7232 | | } else { |
7233 | | exp->exp_right->ref++; |
7234 | | ret = xmlExpHashGetEntry(ctxt, XML_EXP_SEQ, ret, exp->exp_right, |
7235 | | NULL, 0, 0); |
7236 | | } |
7237 | | return(ret); |
7238 | | case XML_EXP_COUNT: { |
7239 | | int min, max; |
7240 | | xmlExpNodePtr tmp; |
7241 | | |
7242 | | if (exp->exp_max == 0) |
7243 | | return(forbiddenExp); |
7244 | | ret = xmlExpStringDeriveInt(ctxt, exp->exp_left, str); |
7245 | | if (ret == NULL) |
7246 | | return(NULL); |
7247 | | if (ret == forbiddenExp) { |
7248 | | return(ret); |
7249 | | } |
7250 | | if (exp->exp_max == 1) |
7251 | | return(ret); |
7252 | | if (exp->exp_max < 0) /* unbounded */ |
7253 | | max = -1; |
7254 | | else |
7255 | | max = exp->exp_max - 1; |
7256 | | if (exp->exp_min > 0) |
7257 | | min = exp->exp_min - 1; |
7258 | | else |
7259 | | min = 0; |
7260 | | exp->exp_left->ref++; |
7261 | | tmp = xmlExpHashGetEntry(ctxt, XML_EXP_COUNT, exp->exp_left, NULL, |
7262 | | NULL, min, max); |
7263 | | if (ret == emptyExp) { |
7264 | | return(tmp); |
7265 | | } |
7266 | | return(xmlExpHashGetEntry(ctxt, XML_EXP_SEQ, ret, tmp, |
7267 | | NULL, 0, 0)); |
7268 | | } |
7269 | | } |
7270 | | return(NULL); |
7271 | | } |
7272 | | |
7273 | | /** |
7274 | | * xmlExpStringDerive: |
7275 | | * @ctxt: the expression context |
7276 | | * @exp: the expression |
7277 | | * @str: the string |
7278 | | * @len: the string len in bytes if available |
7279 | | * |
7280 | | * Do one step of Brzozowski derivation of the expression @exp with |
7281 | | * respect to the input string |
7282 | | * |
7283 | | * Returns the resulting expression or NULL in case of internal error |
7284 | | */ |
7285 | | xmlExpNodePtr |
7286 | | xmlExpStringDerive(xmlExpCtxtPtr ctxt, xmlExpNodePtr exp, |
7287 | | const xmlChar *str, int len) { |
7288 | | const xmlChar *input; |
7289 | | |
7290 | | if ((exp == NULL) || (ctxt == NULL) || (str == NULL)) { |
7291 | | return(NULL); |
7292 | | } |
7293 | | /* |
7294 | | * check the string is in the dictionary, if yes use an interned |
7295 | | * copy, otherwise we know it's not an acceptable input |
7296 | | */ |
7297 | | input = xmlDictExists(ctxt->dict, str, len); |
7298 | | if (input == NULL) { |
7299 | | return(forbiddenExp); |
7300 | | } |
7301 | | return(xmlExpStringDeriveInt(ctxt, exp, input)); |
7302 | | } |
7303 | | |
7304 | | static int |
7305 | | xmlExpCheckCard(xmlExpNodePtr exp, xmlExpNodePtr sub) { |
7306 | | int ret = 1; |
7307 | | |
7308 | | if (sub->c_max == -1) { |
7309 | | if (exp->c_max != -1) |
7310 | | ret = 0; |
7311 | | } else if ((exp->c_max >= 0) && (exp->c_max < sub->c_max)) { |
7312 | | ret = 0; |
7313 | | } |
7314 | | return(ret); |
7315 | | } |
7316 | | |
7317 | | static xmlExpNodePtr xmlExpExpDeriveInt(xmlExpCtxtPtr ctxt, xmlExpNodePtr exp, |
7318 | | xmlExpNodePtr sub); |
7319 | | /** |
7320 | | * xmlExpDivide: |
7321 | | * @ctxt: the expressions context |
7322 | | * @exp: the englobing expression |
7323 | | * @sub: the subexpression |
7324 | | * @mult: the multiple expression |
7325 | | * @remain: the remain from the derivation of the multiple |
7326 | | * |
7327 | | * Check if exp is a multiple of sub, i.e. if there is a finite number n |
7328 | | * so that sub{n} subsume exp |
7329 | | * |
7330 | | * Returns the multiple value if successful, 0 if it is not a multiple |
7331 | | * and -1 in case of internal error. |
7332 | | */ |
7333 | | |
7334 | | static int |
7335 | | xmlExpDivide(xmlExpCtxtPtr ctxt, xmlExpNodePtr exp, xmlExpNodePtr sub, |
7336 | | xmlExpNodePtr *mult, xmlExpNodePtr *remain) { |
7337 | | int i; |
7338 | | xmlExpNodePtr tmp, tmp2; |
7339 | | |
7340 | | if (mult != NULL) *mult = NULL; |
7341 | | if (remain != NULL) *remain = NULL; |
7342 | | if (exp->c_max == -1) return(0); |
7343 | | if (IS_NILLABLE(exp) && (!IS_NILLABLE(sub))) return(0); |
7344 | | |
7345 | | for (i = 1;i <= exp->c_max;i++) { |
7346 | | sub->ref++; |
7347 | | tmp = xmlExpHashGetEntry(ctxt, XML_EXP_COUNT, |
7348 | | sub, NULL, NULL, i, i); |
7349 | | if (tmp == NULL) { |
7350 | | return(-1); |
7351 | | } |
7352 | | if (!xmlExpCheckCard(tmp, exp)) { |
7353 | | xmlExpFree(ctxt, tmp); |
7354 | | continue; |
7355 | | } |
7356 | | tmp2 = xmlExpExpDeriveInt(ctxt, tmp, exp); |
7357 | | if (tmp2 == NULL) { |
7358 | | xmlExpFree(ctxt, tmp); |
7359 | | return(-1); |
7360 | | } |
7361 | | if ((tmp2 != forbiddenExp) && (IS_NILLABLE(tmp2))) { |
7362 | | if (remain != NULL) |
7363 | | *remain = tmp2; |
7364 | | else |
7365 | | xmlExpFree(ctxt, tmp2); |
7366 | | if (mult != NULL) |
7367 | | *mult = tmp; |
7368 | | else |
7369 | | xmlExpFree(ctxt, tmp); |
7370 | | return(i); |
7371 | | } |
7372 | | xmlExpFree(ctxt, tmp); |
7373 | | xmlExpFree(ctxt, tmp2); |
7374 | | } |
7375 | | return(0); |
7376 | | } |
7377 | | |
7378 | | /** |
7379 | | * xmlExpExpDeriveInt: |
7380 | | * @ctxt: the expressions context |
7381 | | * @exp: the englobing expression |
7382 | | * @sub: the subexpression |
7383 | | * |
7384 | | * Try to do a step of Brzozowski derivation but at a higher level |
7385 | | * the input being a subexpression. |
7386 | | * |
7387 | | * Returns the resulting expression or NULL in case of internal error |
7388 | | */ |
7389 | | static xmlExpNodePtr |
7390 | | xmlExpExpDeriveInt(xmlExpCtxtPtr ctxt, xmlExpNodePtr exp, xmlExpNodePtr sub) { |
7391 | | xmlExpNodePtr ret, tmp, tmp2, tmp3; |
7392 | | const xmlChar **tab; |
7393 | | int len, i; |
7394 | | |
7395 | | /* |
7396 | | * In case of equality and if the expression can only consume a finite |
7397 | | * amount, then the derivation is empty |
7398 | | */ |
7399 | | if ((exp == sub) && (exp->c_max >= 0)) { |
7400 | | return(emptyExp); |
7401 | | } |
7402 | | /* |
7403 | | * decompose sub sequence first |
7404 | | */ |
7405 | | if (sub->type == XML_EXP_EMPTY) { |
7406 | | exp->ref++; |
7407 | | return(exp); |
7408 | | } |
7409 | | if (sub->type == XML_EXP_SEQ) { |
7410 | | tmp = xmlExpExpDeriveInt(ctxt, exp, sub->exp_left); |
7411 | | if (tmp == NULL) |
7412 | | return(NULL); |
7413 | | if (tmp == forbiddenExp) |
7414 | | return(tmp); |
7415 | | ret = xmlExpExpDeriveInt(ctxt, tmp, sub->exp_right); |
7416 | | xmlExpFree(ctxt, tmp); |
7417 | | return(ret); |
7418 | | } |
7419 | | if (sub->type == XML_EXP_OR) { |
7420 | | tmp = xmlExpExpDeriveInt(ctxt, exp, sub->exp_left); |
7421 | | if (tmp == forbiddenExp) |
7422 | | return(tmp); |
7423 | | if (tmp == NULL) |
7424 | | return(NULL); |
7425 | | ret = xmlExpExpDeriveInt(ctxt, exp, sub->exp_right); |
7426 | | if ((ret == NULL) || (ret == forbiddenExp)) { |
7427 | | xmlExpFree(ctxt, tmp); |
7428 | | return(ret); |
7429 | | } |
7430 | | return(xmlExpHashGetEntry(ctxt, XML_EXP_OR, tmp, ret, NULL, 0, 0)); |
7431 | | } |
7432 | | if (!xmlExpCheckCard(exp, sub)) { |
7433 | | return(forbiddenExp); |
7434 | | } |
7435 | | switch (exp->type) { |
7436 | | case XML_EXP_EMPTY: |
7437 | | if (sub == emptyExp) |
7438 | | return(emptyExp); |
7439 | | return(forbiddenExp); |
7440 | | case XML_EXP_FORBID: |
7441 | | return(forbiddenExp); |
7442 | | case XML_EXP_ATOM: |
7443 | | if (sub->type == XML_EXP_ATOM) { |
7444 | | /* TODO: handle wildcards */ |
7445 | | if (exp->exp_str == sub->exp_str) { |
7446 | | return(emptyExp); |
7447 | | } |
7448 | | return(forbiddenExp); |
7449 | | } |
7450 | | if ((sub->type == XML_EXP_COUNT) && |
7451 | | (sub->exp_max == 1) && |
7452 | | (sub->exp_left->type == XML_EXP_ATOM)) { |
7453 | | /* TODO: handle wildcards */ |
7454 | | if (exp->exp_str == sub->exp_left->exp_str) { |
7455 | | return(emptyExp); |
7456 | | } |
7457 | | return(forbiddenExp); |
7458 | | } |
7459 | | return(forbiddenExp); |
7460 | | case XML_EXP_SEQ: |
7461 | | /* try to get the sequence consumed only if possible */ |
7462 | | if (xmlExpCheckCard(exp->exp_left, sub)) { |
7463 | | /* See if the sequence can be consumed directly */ |
7464 | | ret = xmlExpExpDeriveInt(ctxt, exp->exp_left, sub); |
7465 | | if ((ret != forbiddenExp) && (ret != NULL)) { |
7466 | | /* |
7467 | | * TODO: assumption here that we are determinist |
7468 | | * i.e. we won't get to a nillable exp left |
7469 | | * subset which could be matched by the right |
7470 | | * part too. |
7471 | | * e.g.: (a | b)+,(a | c) and 'a+,a' |
7472 | | */ |
7473 | | exp->exp_right->ref++; |
7474 | | return(xmlExpHashGetEntry(ctxt, XML_EXP_SEQ, ret, |
7475 | | exp->exp_right, NULL, 0, 0)); |
7476 | | } |
7477 | | } |
7478 | | /* Try instead to decompose */ |
7479 | | if (sub->type == XML_EXP_COUNT) { |
7480 | | int min, max; |
7481 | | |
7482 | | ret = xmlExpExpDeriveInt(ctxt, exp->exp_left, sub->exp_left); |
7483 | | if (ret == NULL) |
7484 | | return(NULL); |
7485 | | if (ret != forbiddenExp) { |
7486 | | if (sub->exp_max < 0) |
7487 | | max = -1; |
7488 | | else |
7489 | | max = sub->exp_max -1; |
7490 | | if (sub->exp_min > 0) |
7491 | | min = sub->exp_min -1; |
7492 | | else |
7493 | | min = 0; |
7494 | | exp->exp_right->ref++; |
7495 | | tmp = xmlExpHashGetEntry(ctxt, XML_EXP_SEQ, ret, |
7496 | | exp->exp_right, NULL, 0, 0); |
7497 | | if (tmp == NULL) |
7498 | | return(NULL); |
7499 | | |
7500 | | sub->exp_left->ref++; |
7501 | | tmp2 = xmlExpHashGetEntry(ctxt, XML_EXP_COUNT, |
7502 | | sub->exp_left, NULL, NULL, min, max); |
7503 | | if (tmp2 == NULL) { |
7504 | | xmlExpFree(ctxt, tmp); |
7505 | | return(NULL); |
7506 | | } |
7507 | | ret = xmlExpExpDeriveInt(ctxt, tmp, tmp2); |
7508 | | xmlExpFree(ctxt, tmp); |
7509 | | xmlExpFree(ctxt, tmp2); |
7510 | | return(ret); |
7511 | | } |
7512 | | } |
7513 | | /* we made no progress on structured operations */ |
7514 | | break; |
7515 | | case XML_EXP_OR: |
7516 | | ret = xmlExpExpDeriveInt(ctxt, exp->exp_left, sub); |
7517 | | if (ret == NULL) |
7518 | | return(NULL); |
7519 | | tmp = xmlExpExpDeriveInt(ctxt, exp->exp_right, sub); |
7520 | | if (tmp == NULL) { |
7521 | | xmlExpFree(ctxt, ret); |
7522 | | return(NULL); |
7523 | | } |
7524 | | return(xmlExpHashGetEntry(ctxt, XML_EXP_OR, ret, tmp, NULL, 0, 0)); |
7525 | | case XML_EXP_COUNT: { |
7526 | | int min, max; |
7527 | | |
7528 | | if (sub->type == XML_EXP_COUNT) { |
7529 | | /* |
7530 | | * Try to see if the loop is completely subsumed |
7531 | | */ |
7532 | | tmp = xmlExpExpDeriveInt(ctxt, exp->exp_left, sub->exp_left); |
7533 | | if (tmp == NULL) |
7534 | | return(NULL); |
7535 | | if (tmp == forbiddenExp) { |
7536 | | int mult; |
7537 | | |
7538 | | mult = xmlExpDivide(ctxt, sub->exp_left, exp->exp_left, |
7539 | | NULL, &tmp); |
7540 | | if (mult <= 0) { |
7541 | | return(forbiddenExp); |
7542 | | } |
7543 | | if (sub->exp_max == -1) { |
7544 | | max = -1; |
7545 | | if (exp->exp_max == -1) { |
7546 | | if (exp->exp_min <= sub->exp_min * mult) |
7547 | | min = 0; |
7548 | | else |
7549 | | min = exp->exp_min - sub->exp_min * mult; |
7550 | | } else { |
7551 | | xmlExpFree(ctxt, tmp); |
7552 | | return(forbiddenExp); |
7553 | | } |
7554 | | } else { |
7555 | | if (exp->exp_max == -1) { |
7556 | | if (exp->exp_min > sub->exp_min * mult) { |
7557 | | max = -1; |
7558 | | min = exp->exp_min - sub->exp_min * mult; |
7559 | | } else { |
7560 | | max = -1; |
7561 | | min = 0; |
7562 | | } |
7563 | | } else { |
7564 | | if (exp->exp_max < sub->exp_max * mult) { |
7565 | | xmlExpFree(ctxt, tmp); |
7566 | | return(forbiddenExp); |
7567 | | } |
7568 | | if (sub->exp_max * mult > exp->exp_min) |
7569 | | min = 0; |
7570 | | else |
7571 | | min = exp->exp_min - sub->exp_max * mult; |
7572 | | max = exp->exp_max - sub->exp_max * mult; |
7573 | | } |
7574 | | } |
7575 | | } else if (!IS_NILLABLE(tmp)) { |
7576 | | /* |
7577 | | * TODO: loop here to try to grow if working on finite |
7578 | | * blocks. |
7579 | | */ |
7580 | | xmlExpFree(ctxt, tmp); |
7581 | | return(forbiddenExp); |
7582 | | } else if (sub->exp_max == -1) { |
7583 | | if (exp->exp_max == -1) { |
7584 | | if (exp->exp_min <= sub->exp_min) { |
7585 | | max = -1; |
7586 | | min = 0; |
7587 | | } else { |
7588 | | max = -1; |
7589 | | min = exp->exp_min - sub->exp_min; |
7590 | | } |
7591 | | } else if (exp->exp_min > sub->exp_min) { |
7592 | | xmlExpFree(ctxt, tmp); |
7593 | | return(forbiddenExp); |
7594 | | } else { |
7595 | | max = -1; |
7596 | | min = 0; |
7597 | | } |
7598 | | } else { |
7599 | | if (exp->exp_max == -1) { |
7600 | | if (exp->exp_min > sub->exp_min) { |
7601 | | max = -1; |
7602 | | min = exp->exp_min - sub->exp_min; |
7603 | | } else { |
7604 | | max = -1; |
7605 | | min = 0; |
7606 | | } |
7607 | | } else { |
7608 | | if (exp->exp_max < sub->exp_max) { |
7609 | | xmlExpFree(ctxt, tmp); |
7610 | | return(forbiddenExp); |
7611 | | } |
7612 | | if (sub->exp_max > exp->exp_min) |
7613 | | min = 0; |
7614 | | else |
7615 | | min = exp->exp_min - sub->exp_max; |
7616 | | max = exp->exp_max - sub->exp_max; |
7617 | | } |
7618 | | } |
7619 | | exp->exp_left->ref++; |
7620 | | tmp2 = xmlExpHashGetEntry(ctxt, XML_EXP_COUNT, exp->exp_left, |
7621 | | NULL, NULL, min, max); |
7622 | | if (tmp2 == NULL) { |
7623 | | return(NULL); |
7624 | | } |
7625 | | ret = xmlExpHashGetEntry(ctxt, XML_EXP_SEQ, tmp, tmp2, |
7626 | | NULL, 0, 0); |
7627 | | return(ret); |
7628 | | } |
7629 | | tmp = xmlExpExpDeriveInt(ctxt, exp->exp_left, sub); |
7630 | | if (tmp == NULL) |
7631 | | return(NULL); |
7632 | | if (tmp == forbiddenExp) { |
7633 | | return(forbiddenExp); |
7634 | | } |
7635 | | if (exp->exp_min > 0) |
7636 | | min = exp->exp_min - 1; |
7637 | | else |
7638 | | min = 0; |
7639 | | if (exp->exp_max < 0) |
7640 | | max = -1; |
7641 | | else |
7642 | | max = exp->exp_max - 1; |
7643 | | |
7644 | | exp->exp_left->ref++; |
7645 | | tmp2 = xmlExpHashGetEntry(ctxt, XML_EXP_COUNT, exp->exp_left, |
7646 | | NULL, NULL, min, max); |
7647 | | if (tmp2 == NULL) |
7648 | | return(NULL); |
7649 | | ret = xmlExpHashGetEntry(ctxt, XML_EXP_SEQ, tmp, tmp2, |
7650 | | NULL, 0, 0); |
7651 | | return(ret); |
7652 | | } |
7653 | | } |
7654 | | |
7655 | | if (IS_NILLABLE(sub)) { |
7656 | | if (!(IS_NILLABLE(exp))) |
7657 | | return(forbiddenExp); |
7658 | | else |
7659 | | ret = emptyExp; |
7660 | | } else |
7661 | | ret = NULL; |
7662 | | /* |
7663 | | * here the structured derivation made no progress so |
7664 | | * we use the default token based derivation to force one more step |
7665 | | */ |
7666 | | if (ctxt->tabSize == 0) |
7667 | | ctxt->tabSize = 40; |
7668 | | |
7669 | | tab = (const xmlChar **) xmlMalloc(ctxt->tabSize * |
7670 | | sizeof(const xmlChar *)); |
7671 | | if (tab == NULL) { |
7672 | | return(NULL); |
7673 | | } |
7674 | | |
7675 | | /* |
7676 | | * collect all the strings accepted by the subexpression on input |
7677 | | */ |
7678 | | len = xmlExpGetStartInt(ctxt, sub, tab, ctxt->tabSize, 0); |
7679 | | while (len < 0) { |
7680 | | const xmlChar **temp; |
7681 | | int newSize; |
7682 | | |
7683 | | newSize = xmlGrowCapacity(ctxt->tabSize, sizeof(temp[0]), |
7684 | | 40, XML_MAX_ITEMS); |
7685 | | if (newSize < 0) { |
7686 | | xmlFree(tab); |
7687 | | return(NULL); |
7688 | | } |
7689 | | temp = xmlRealloc(tab, newSize * sizeof(temp[0])); |
7690 | | if (temp == NULL) { |
7691 | | xmlFree(tab); |
7692 | | return(NULL); |
7693 | | } |
7694 | | tab = temp; |
7695 | | ctxt->tabSize = newSize; |
7696 | | len = xmlExpGetStartInt(ctxt, sub, tab, ctxt->tabSize, 0); |
7697 | | } |
7698 | | for (i = 0;i < len;i++) { |
7699 | | tmp = xmlExpStringDeriveInt(ctxt, exp, tab[i]); |
7700 | | if ((tmp == NULL) || (tmp == forbiddenExp)) { |
7701 | | xmlExpFree(ctxt, ret); |
7702 | | xmlFree((xmlChar **) tab); |
7703 | | return(tmp); |
7704 | | } |
7705 | | tmp2 = xmlExpStringDeriveInt(ctxt, sub, tab[i]); |
7706 | | if ((tmp2 == NULL) || (tmp2 == forbiddenExp)) { |
7707 | | xmlExpFree(ctxt, tmp); |
7708 | | xmlExpFree(ctxt, ret); |
7709 | | xmlFree((xmlChar **) tab); |
7710 | | return(tmp); |
7711 | | } |
7712 | | tmp3 = xmlExpExpDeriveInt(ctxt, tmp, tmp2); |
7713 | | xmlExpFree(ctxt, tmp); |
7714 | | xmlExpFree(ctxt, tmp2); |
7715 | | |
7716 | | if ((tmp3 == NULL) || (tmp3 == forbiddenExp)) { |
7717 | | xmlExpFree(ctxt, ret); |
7718 | | xmlFree((xmlChar **) tab); |
7719 | | return(tmp3); |
7720 | | } |
7721 | | |
7722 | | if (ret == NULL) |
7723 | | ret = tmp3; |
7724 | | else { |
7725 | | ret = xmlExpHashGetEntry(ctxt, XML_EXP_OR, ret, tmp3, NULL, 0, 0); |
7726 | | if (ret == NULL) { |
7727 | | xmlFree((xmlChar **) tab); |
7728 | | return(NULL); |
7729 | | } |
7730 | | } |
7731 | | } |
7732 | | xmlFree((xmlChar **) tab); |
7733 | | return(ret); |
7734 | | } |
7735 | | |
7736 | | /** |
7737 | | * xmlExpExpDerive: |
7738 | | * @ctxt: the expressions context |
7739 | | * @exp: the englobing expression |
7740 | | * @sub: the subexpression |
7741 | | * |
7742 | | * Evaluates the expression resulting from @exp consuming a sub expression @sub |
7743 | | * Based on algebraic derivation and sometimes direct Brzozowski derivation |
7744 | | * it usually takes less than linear time and can handle expressions generating |
7745 | | * infinite languages. |
7746 | | * |
7747 | | * Returns the resulting expression or NULL in case of internal error, the |
7748 | | * result must be freed |
7749 | | */ |
7750 | | xmlExpNodePtr |
7751 | | xmlExpExpDerive(xmlExpCtxtPtr ctxt, xmlExpNodePtr exp, xmlExpNodePtr sub) { |
7752 | | if ((exp == NULL) || (ctxt == NULL) || (sub == NULL)) |
7753 | | return(NULL); |
7754 | | |
7755 | | /* |
7756 | | * O(1) speedups |
7757 | | */ |
7758 | | if (IS_NILLABLE(sub) && (!IS_NILLABLE(exp))) { |
7759 | | return(forbiddenExp); |
7760 | | } |
7761 | | if (xmlExpCheckCard(exp, sub) == 0) { |
7762 | | return(forbiddenExp); |
7763 | | } |
7764 | | return(xmlExpExpDeriveInt(ctxt, exp, sub)); |
7765 | | } |
7766 | | |
7767 | | /** |
7768 | | * xmlExpSubsume: |
7769 | | * @ctxt: the expressions context |
7770 | | * @exp: the englobing expression |
7771 | | * @sub: the subexpression |
7772 | | * |
7773 | | * Check whether @exp accepts all the languages accepted by @sub |
7774 | | * the input being a subexpression. |
7775 | | * |
7776 | | * Returns 1 if true 0 if false and -1 in case of failure. |
7777 | | */ |
7778 | | int |
7779 | | xmlExpSubsume(xmlExpCtxtPtr ctxt, xmlExpNodePtr exp, xmlExpNodePtr sub) { |
7780 | | xmlExpNodePtr tmp; |
7781 | | |
7782 | | if ((exp == NULL) || (ctxt == NULL) || (sub == NULL)) |
7783 | | return(-1); |
7784 | | |
7785 | | /* |
7786 | | * TODO: speedup by checking the language of sub is a subset of the |
7787 | | * language of exp |
7788 | | */ |
7789 | | /* |
7790 | | * O(1) speedups |
7791 | | */ |
7792 | | if (IS_NILLABLE(sub) && (!IS_NILLABLE(exp))) { |
7793 | | return(0); |
7794 | | } |
7795 | | if (xmlExpCheckCard(exp, sub) == 0) { |
7796 | | return(0); |
7797 | | } |
7798 | | tmp = xmlExpExpDeriveInt(ctxt, exp, sub); |
7799 | | if (tmp == NULL) |
7800 | | return(-1); |
7801 | | if (tmp == forbiddenExp) |
7802 | | return(0); |
7803 | | if (tmp == emptyExp) |
7804 | | return(1); |
7805 | | if ((tmp != NULL) && (IS_NILLABLE(tmp))) { |
7806 | | xmlExpFree(ctxt, tmp); |
7807 | | return(1); |
7808 | | } |
7809 | | xmlExpFree(ctxt, tmp); |
7810 | | return(0); |
7811 | | } |
7812 | | |
7813 | | /************************************************************************ |
7814 | | * * |
7815 | | * Parsing expression * |
7816 | | * * |
7817 | | ************************************************************************/ |
7818 | | |
7819 | | static xmlExpNodePtr xmlExpParseExpr(xmlExpCtxtPtr ctxt); |
7820 | | |
7821 | | #undef CUR |
7822 | | #define CUR (*ctxt->cur) |
7823 | | #undef NEXT |
7824 | | #define NEXT ctxt->cur++; |
7825 | | #undef IS_BLANK |
7826 | | #define IS_BLANK(c) ((c == ' ') || (c == '\n') || (c == '\r') || (c == '\t')) |
7827 | | #define SKIP_BLANKS while (IS_BLANK(*ctxt->cur)) ctxt->cur++; |
7828 | | |
7829 | | static int |
7830 | | xmlExpParseNumber(xmlExpCtxtPtr ctxt) { |
7831 | | int ret = 0; |
7832 | | |
7833 | | SKIP_BLANKS |
7834 | | if (CUR == '*') { |
7835 | | NEXT |
7836 | | return(-1); |
7837 | | } |
7838 | | if ((CUR < '0') || (CUR > '9')) |
7839 | | return(-1); |
7840 | | while ((CUR >= '0') && (CUR <= '9')) { |
7841 | | ret = ret * 10 + (CUR - '0'); |
7842 | | NEXT |
7843 | | } |
7844 | | return(ret); |
7845 | | } |
7846 | | |
7847 | | static xmlExpNodePtr |
7848 | | xmlExpParseOr(xmlExpCtxtPtr ctxt) { |
7849 | | const char *base; |
7850 | | xmlExpNodePtr ret; |
7851 | | const xmlChar *val; |
7852 | | |
7853 | | SKIP_BLANKS |
7854 | | base = ctxt->cur; |
7855 | | if (*ctxt->cur == '(') { |
7856 | | NEXT |
7857 | | ret = xmlExpParseExpr(ctxt); |
7858 | | SKIP_BLANKS |
7859 | | if (*ctxt->cur != ')') { |
7860 | | xmlExpFree(ctxt, ret); |
7861 | | return(NULL); |
7862 | | } |
7863 | | NEXT; |
7864 | | SKIP_BLANKS |
7865 | | goto parse_quantifier; |
7866 | | } |
7867 | | while ((CUR != 0) && (!(IS_BLANK(CUR))) && (CUR != '(') && |
7868 | | (CUR != ')') && (CUR != '|') && (CUR != ',') && (CUR != '{') && |
7869 | | (CUR != '*') && (CUR != '+') && (CUR != '?') && (CUR != '}')) |
7870 | | NEXT; |
7871 | | val = xmlDictLookup(ctxt->dict, BAD_CAST base, ctxt->cur - base); |
7872 | | if (val == NULL) |
7873 | | return(NULL); |
7874 | | ret = xmlExpHashGetEntry(ctxt, XML_EXP_ATOM, NULL, NULL, val, 0, 0); |
7875 | | if (ret == NULL) |
7876 | | return(NULL); |
7877 | | SKIP_BLANKS |
7878 | | parse_quantifier: |
7879 | | if (CUR == '{') { |
7880 | | int min, max; |
7881 | | |
7882 | | NEXT |
7883 | | min = xmlExpParseNumber(ctxt); |
7884 | | if (min < 0) { |
7885 | | xmlExpFree(ctxt, ret); |
7886 | | return(NULL); |
7887 | | } |
7888 | | SKIP_BLANKS |
7889 | | if (CUR == ',') { |
7890 | | NEXT |
7891 | | max = xmlExpParseNumber(ctxt); |
7892 | | SKIP_BLANKS |
7893 | | } else |
7894 | | max = min; |
7895 | | if (CUR != '}') { |
7896 | | xmlExpFree(ctxt, ret); |
7897 | | return(NULL); |
7898 | | } |
7899 | | NEXT |
7900 | | ret = xmlExpHashGetEntry(ctxt, XML_EXP_COUNT, ret, NULL, NULL, |
7901 | | min, max); |
7902 | | SKIP_BLANKS |
7903 | | } else if (CUR == '?') { |
7904 | | NEXT |
7905 | | ret = xmlExpHashGetEntry(ctxt, XML_EXP_COUNT, ret, NULL, NULL, |
7906 | | 0, 1); |
7907 | | SKIP_BLANKS |
7908 | | } else if (CUR == '+') { |
7909 | | NEXT |
7910 | | ret = xmlExpHashGetEntry(ctxt, XML_EXP_COUNT, ret, NULL, NULL, |
7911 | | 1, -1); |
7912 | | SKIP_BLANKS |
7913 | | } else if (CUR == '*') { |
7914 | | NEXT |
7915 | | ret = xmlExpHashGetEntry(ctxt, XML_EXP_COUNT, ret, NULL, NULL, |
7916 | | 0, -1); |
7917 | | SKIP_BLANKS |
7918 | | } |
7919 | | return(ret); |
7920 | | } |
7921 | | |
7922 | | |
7923 | | static xmlExpNodePtr |
7924 | | xmlExpParseSeq(xmlExpCtxtPtr ctxt) { |
7925 | | xmlExpNodePtr ret, right; |
7926 | | |
7927 | | ret = xmlExpParseOr(ctxt); |
7928 | | SKIP_BLANKS |
7929 | | while (CUR == '|') { |
7930 | | NEXT |
7931 | | right = xmlExpParseOr(ctxt); |
7932 | | if (right == NULL) { |
7933 | | xmlExpFree(ctxt, ret); |
7934 | | return(NULL); |
7935 | | } |
7936 | | ret = xmlExpHashGetEntry(ctxt, XML_EXP_OR, ret, right, NULL, 0, 0); |
7937 | | if (ret == NULL) |
7938 | | return(NULL); |
7939 | | } |
7940 | | return(ret); |
7941 | | } |
7942 | | |
7943 | | static xmlExpNodePtr |
7944 | | xmlExpParseExpr(xmlExpCtxtPtr ctxt) { |
7945 | | xmlExpNodePtr ret, right; |
7946 | | |
7947 | | ret = xmlExpParseSeq(ctxt); |
7948 | | SKIP_BLANKS |
7949 | | while (CUR == ',') { |
7950 | | NEXT |
7951 | | right = xmlExpParseSeq(ctxt); |
7952 | | if (right == NULL) { |
7953 | | xmlExpFree(ctxt, ret); |
7954 | | return(NULL); |
7955 | | } |
7956 | | ret = xmlExpHashGetEntry(ctxt, XML_EXP_SEQ, ret, right, NULL, 0, 0); |
7957 | | if (ret == NULL) |
7958 | | return(NULL); |
7959 | | } |
7960 | | return(ret); |
7961 | | } |
7962 | | |
7963 | | /** |
7964 | | * xmlExpParse: |
7965 | | * @ctxt: the expressions context |
7966 | | * @expr: the 0 terminated string |
7967 | | * |
7968 | | * Minimal parser for regexps, it understand the following constructs |
7969 | | * - string terminals |
7970 | | * - choice operator | |
7971 | | * - sequence operator , |
7972 | | * - subexpressions (...) |
7973 | | * - usual cardinality operators + * and ? |
7974 | | * - finite sequences { min, max } |
7975 | | * - infinite sequences { min, * } |
7976 | | * There is minimal checkings made especially no checking on strings values |
7977 | | * |
7978 | | * Returns a new expression or NULL in case of failure |
7979 | | */ |
7980 | | xmlExpNodePtr |
7981 | | xmlExpParse(xmlExpCtxtPtr ctxt, const char *expr) { |
7982 | | xmlExpNodePtr ret; |
7983 | | |
7984 | | ctxt->expr = expr; |
7985 | | ctxt->cur = expr; |
7986 | | |
7987 | | ret = xmlExpParseExpr(ctxt); |
7988 | | SKIP_BLANKS |
7989 | | if (*ctxt->cur != 0) { |
7990 | | xmlExpFree(ctxt, ret); |
7991 | | return(NULL); |
7992 | | } |
7993 | | return(ret); |
7994 | | } |
7995 | | |
7996 | | static void |
7997 | | xmlExpDumpInt(xmlBufferPtr buf, xmlExpNodePtr expr, int glob) { |
7998 | | xmlExpNodePtr c; |
7999 | | |
8000 | | if (expr == NULL) return; |
8001 | | if (glob) xmlBufferWriteChar(buf, "("); |
8002 | | switch (expr->type) { |
8003 | | case XML_EXP_EMPTY: |
8004 | | xmlBufferWriteChar(buf, "empty"); |
8005 | | break; |
8006 | | case XML_EXP_FORBID: |
8007 | | xmlBufferWriteChar(buf, "forbidden"); |
8008 | | break; |
8009 | | case XML_EXP_ATOM: |
8010 | | xmlBufferWriteCHAR(buf, expr->exp_str); |
8011 | | break; |
8012 | | case XML_EXP_SEQ: |
8013 | | c = expr->exp_left; |
8014 | | if ((c->type == XML_EXP_SEQ) || (c->type == XML_EXP_OR)) |
8015 | | xmlExpDumpInt(buf, c, 1); |
8016 | | else |
8017 | | xmlExpDumpInt(buf, c, 0); |
8018 | | xmlBufferWriteChar(buf, " , "); |
8019 | | c = expr->exp_right; |
8020 | | if ((c->type == XML_EXP_SEQ) || (c->type == XML_EXP_OR)) |
8021 | | xmlExpDumpInt(buf, c, 1); |
8022 | | else |
8023 | | xmlExpDumpInt(buf, c, 0); |
8024 | | break; |
8025 | | case XML_EXP_OR: |
8026 | | c = expr->exp_left; |
8027 | | if ((c->type == XML_EXP_SEQ) || (c->type == XML_EXP_OR)) |
8028 | | xmlExpDumpInt(buf, c, 1); |
8029 | | else |
8030 | | xmlExpDumpInt(buf, c, 0); |
8031 | | xmlBufferWriteChar(buf, " | "); |
8032 | | c = expr->exp_right; |
8033 | | if ((c->type == XML_EXP_SEQ) || (c->type == XML_EXP_OR)) |
8034 | | xmlExpDumpInt(buf, c, 1); |
8035 | | else |
8036 | | xmlExpDumpInt(buf, c, 0); |
8037 | | break; |
8038 | | case XML_EXP_COUNT: { |
8039 | | char rep[40]; |
8040 | | |
8041 | | c = expr->exp_left; |
8042 | | if ((c->type == XML_EXP_SEQ) || (c->type == XML_EXP_OR)) |
8043 | | xmlExpDumpInt(buf, c, 1); |
8044 | | else |
8045 | | xmlExpDumpInt(buf, c, 0); |
8046 | | if ((expr->exp_min == 0) && (expr->exp_max == 1)) { |
8047 | | rep[0] = '?'; |
8048 | | rep[1] = 0; |
8049 | | } else if ((expr->exp_min == 0) && (expr->exp_max == -1)) { |
8050 | | rep[0] = '*'; |
8051 | | rep[1] = 0; |
8052 | | } else if ((expr->exp_min == 1) && (expr->exp_max == -1)) { |
8053 | | rep[0] = '+'; |
8054 | | rep[1] = 0; |
8055 | | } else if (expr->exp_max == expr->exp_min) { |
8056 | | snprintf(rep, 39, "{%d}", expr->exp_min); |
8057 | | } else if (expr->exp_max < 0) { |
8058 | | snprintf(rep, 39, "{%d,inf}", expr->exp_min); |
8059 | | } else { |
8060 | | snprintf(rep, 39, "{%d,%d}", expr->exp_min, expr->exp_max); |
8061 | | } |
8062 | | rep[39] = 0; |
8063 | | xmlBufferWriteChar(buf, rep); |
8064 | | break; |
8065 | | } |
8066 | | default: |
8067 | | break; |
8068 | | } |
8069 | | if (glob) |
8070 | | xmlBufferWriteChar(buf, ")"); |
8071 | | } |
8072 | | /** |
8073 | | * xmlExpDump: |
8074 | | * @buf: a buffer to receive the output |
8075 | | * @expr: the compiled expression |
8076 | | * |
8077 | | * Serialize the expression as compiled to the buffer |
8078 | | */ |
8079 | | void |
8080 | | xmlExpDump(xmlBufferPtr buf, xmlExpNodePtr expr) { |
8081 | | if ((buf == NULL) || (expr == NULL)) |
8082 | | return; |
8083 | | xmlExpDumpInt(buf, expr, 0); |
8084 | | } |
8085 | | |
8086 | | /** |
8087 | | * xmlExpMaxToken: |
8088 | | * @expr: a compiled expression |
8089 | | * |
8090 | | * Indicate the maximum number of input a expression can accept |
8091 | | * |
8092 | | * Returns the maximum length or -1 in case of error |
8093 | | */ |
8094 | | int |
8095 | | xmlExpMaxToken(xmlExpNodePtr expr) { |
8096 | | if (expr == NULL) |
8097 | | return(-1); |
8098 | | return(expr->c_max); |
8099 | | } |
8100 | | |
8101 | | /** |
8102 | | * xmlExpCtxtNbNodes: |
8103 | | * @ctxt: an expression context |
8104 | | * |
8105 | | * Debugging facility provides the number of allocated nodes at a that point |
8106 | | * |
8107 | | * Returns the number of nodes in use or -1 in case of error |
8108 | | */ |
8109 | | int |
8110 | | xmlExpCtxtNbNodes(xmlExpCtxtPtr ctxt) { |
8111 | | if (ctxt == NULL) |
8112 | | return(-1); |
8113 | | return(ctxt->nb_nodes); |
8114 | | } |
8115 | | |
8116 | | /** |
8117 | | * xmlExpCtxtNbCons: |
8118 | | * @ctxt: an expression context |
8119 | | * |
8120 | | * Debugging facility provides the number of allocated nodes over lifetime |
8121 | | * |
8122 | | * Returns the number of nodes ever allocated or -1 in case of error |
8123 | | */ |
8124 | | int |
8125 | | xmlExpCtxtNbCons(xmlExpCtxtPtr ctxt) { |
8126 | | if (ctxt == NULL) |
8127 | | return(-1); |
8128 | | return(ctxt->nb_cons); |
8129 | | } |
8130 | | |
8131 | | /** DOC_ENABLE */ |
8132 | | #endif /* LIBXML_EXPR_ENABLED */ |
8133 | | |
8134 | | #endif /* LIBXML_REGEXP_ENABLED */ |