Line | Count | Source (jump to first uncovered line) |
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 | | * Author: Daniel Veillard |
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 | | |
35 | | #ifndef SIZE_MAX |
36 | | #define SIZE_MAX ((size_t) -1) |
37 | | #endif |
38 | | |
39 | | /* #define DEBUG_REGEXP */ |
40 | | |
41 | 0 | #define MAX_PUSH 10000000 |
42 | | |
43 | | #ifdef ERROR |
44 | | #undef ERROR |
45 | | #endif |
46 | | #define ERROR(str) \ |
47 | 0 | ctxt->error = XML_REGEXP_COMPILE_ERROR; \ |
48 | 0 | xmlRegexpErrCompile(ctxt, str); |
49 | 0 | #define NEXT ctxt->cur++ |
50 | 0 | #define CUR (*(ctxt->cur)) |
51 | 0 | #define NXT(index) (ctxt->cur[index]) |
52 | | |
53 | 0 | #define NEXTL(l) ctxt->cur += l; |
54 | 0 | #define XML_REG_STRING_SEPARATOR '|' |
55 | | /* |
56 | | * Need PREV to check on a '-' within a Character Group. May only be used |
57 | | * when it's guaranteed that cur is not at the beginning of ctxt->string! |
58 | | */ |
59 | 0 | #define PREV (ctxt->cur[-1]) |
60 | | |
61 | | /************************************************************************ |
62 | | * * |
63 | | * Unicode support * |
64 | | * * |
65 | | ************************************************************************/ |
66 | | |
67 | | typedef struct { |
68 | | const char *rangename; |
69 | | const xmlChRangeGroup group; |
70 | | } xmlUnicodeRange; |
71 | | |
72 | | #include "codegen/unicode.inc" |
73 | | |
74 | | /** |
75 | | * binary table lookup for user-supplied name |
76 | | * |
77 | | * @param sptr a table of xmlUnicodeRange structs |
78 | | * @param numentries number of table entries |
79 | | * @param tname name to be found |
80 | | * @returns pointer to range function if found, otherwise NULL |
81 | | */ |
82 | | static const xmlChRangeGroup * |
83 | | xmlUnicodeLookup(const xmlUnicodeRange *sptr, int numentries, |
84 | 0 | const char *tname) { |
85 | 0 | int low, high, mid, cmp; |
86 | |
|
87 | 0 | if (tname == NULL) return(NULL); |
88 | | |
89 | 0 | low = 0; |
90 | 0 | high = numentries - 1; |
91 | 0 | while (low <= high) { |
92 | 0 | mid = (low + high) / 2; |
93 | 0 | cmp = strcmp(tname, sptr[mid].rangename); |
94 | 0 | if (cmp == 0) |
95 | 0 | return (&sptr[mid].group); |
96 | 0 | if (cmp < 0) |
97 | 0 | high = mid - 1; |
98 | 0 | else |
99 | 0 | low = mid + 1; |
100 | 0 | } |
101 | 0 | return (NULL); |
102 | 0 | } |
103 | | |
104 | | /** |
105 | | * Check whether the character is part of the UCS Block |
106 | | * |
107 | | * @param code UCS code point |
108 | | * @param block UCS block name |
109 | | * @returns 1 if true, 0 if false and -1 on unknown block |
110 | | */ |
111 | | static int |
112 | 0 | xmlUCSIsBlock(int code, const char *block) { |
113 | 0 | const xmlChRangeGroup *group; |
114 | |
|
115 | 0 | group = xmlUnicodeLookup(xmlUnicodeBlocks, |
116 | 0 | sizeof(xmlUnicodeBlocks) / sizeof(xmlUnicodeBlocks[0]), block); |
117 | 0 | if (group == NULL) |
118 | 0 | return (-1); |
119 | 0 | return (xmlCharInRange(code, group)); |
120 | 0 | } |
121 | | |
122 | | /************************************************************************ |
123 | | * * |
124 | | * Datatypes and structures * |
125 | | * * |
126 | | ************************************************************************/ |
127 | | |
128 | | /* |
129 | | * Note: the order of the enums below is significant, do not shuffle |
130 | | */ |
131 | | typedef enum { |
132 | | XML_REGEXP_EPSILON = 1, |
133 | | XML_REGEXP_CHARVAL, |
134 | | XML_REGEXP_RANGES, |
135 | | XML_REGEXP_SUBREG, /* used for () sub regexps */ |
136 | | XML_REGEXP_STRING, |
137 | | XML_REGEXP_ANYCHAR, /* . */ |
138 | | XML_REGEXP_ANYSPACE, /* \s */ |
139 | | XML_REGEXP_NOTSPACE, /* \S */ |
140 | | XML_REGEXP_INITNAME, /* \l */ |
141 | | XML_REGEXP_NOTINITNAME, /* \L */ |
142 | | XML_REGEXP_NAMECHAR, /* \c */ |
143 | | XML_REGEXP_NOTNAMECHAR, /* \C */ |
144 | | XML_REGEXP_DECIMAL, /* \d */ |
145 | | XML_REGEXP_NOTDECIMAL, /* \D */ |
146 | | XML_REGEXP_REALCHAR, /* \w */ |
147 | | XML_REGEXP_NOTREALCHAR, /* \W */ |
148 | | XML_REGEXP_LETTER = 100, |
149 | | XML_REGEXP_LETTER_UPPERCASE, |
150 | | XML_REGEXP_LETTER_LOWERCASE, |
151 | | XML_REGEXP_LETTER_TITLECASE, |
152 | | XML_REGEXP_LETTER_MODIFIER, |
153 | | XML_REGEXP_LETTER_OTHERS, |
154 | | XML_REGEXP_MARK, |
155 | | XML_REGEXP_MARK_NONSPACING, |
156 | | XML_REGEXP_MARK_SPACECOMBINING, |
157 | | XML_REGEXP_MARK_ENCLOSING, |
158 | | XML_REGEXP_NUMBER, |
159 | | XML_REGEXP_NUMBER_DECIMAL, |
160 | | XML_REGEXP_NUMBER_LETTER, |
161 | | XML_REGEXP_NUMBER_OTHERS, |
162 | | XML_REGEXP_PUNCT, |
163 | | XML_REGEXP_PUNCT_CONNECTOR, |
164 | | XML_REGEXP_PUNCT_DASH, |
165 | | XML_REGEXP_PUNCT_OPEN, |
166 | | XML_REGEXP_PUNCT_CLOSE, |
167 | | XML_REGEXP_PUNCT_INITQUOTE, |
168 | | XML_REGEXP_PUNCT_FINQUOTE, |
169 | | XML_REGEXP_PUNCT_OTHERS, |
170 | | XML_REGEXP_SEPAR, |
171 | | XML_REGEXP_SEPAR_SPACE, |
172 | | XML_REGEXP_SEPAR_LINE, |
173 | | XML_REGEXP_SEPAR_PARA, |
174 | | XML_REGEXP_SYMBOL, |
175 | | XML_REGEXP_SYMBOL_MATH, |
176 | | XML_REGEXP_SYMBOL_CURRENCY, |
177 | | XML_REGEXP_SYMBOL_MODIFIER, |
178 | | XML_REGEXP_SYMBOL_OTHERS, |
179 | | XML_REGEXP_OTHER, |
180 | | XML_REGEXP_OTHER_CONTROL, |
181 | | XML_REGEXP_OTHER_FORMAT, |
182 | | XML_REGEXP_OTHER_PRIVATE, |
183 | | XML_REGEXP_OTHER_NA, |
184 | | XML_REGEXP_BLOCK_NAME |
185 | | } xmlRegAtomType; |
186 | | |
187 | | typedef enum { |
188 | | XML_REGEXP_QUANT_EPSILON = 1, |
189 | | XML_REGEXP_QUANT_ONCE, |
190 | | XML_REGEXP_QUANT_OPT, |
191 | | XML_REGEXP_QUANT_MULT, |
192 | | XML_REGEXP_QUANT_PLUS, |
193 | | XML_REGEXP_QUANT_ONCEONLY, |
194 | | XML_REGEXP_QUANT_ALL, |
195 | | XML_REGEXP_QUANT_RANGE |
196 | | } xmlRegQuantType; |
197 | | |
198 | | typedef enum { |
199 | | XML_REGEXP_START_STATE = 1, |
200 | | XML_REGEXP_FINAL_STATE, |
201 | | XML_REGEXP_TRANS_STATE, |
202 | | XML_REGEXP_SINK_STATE, |
203 | | XML_REGEXP_UNREACH_STATE |
204 | | } xmlRegStateType; |
205 | | |
206 | | typedef enum { |
207 | | XML_REGEXP_MARK_NORMAL = 0, |
208 | | XML_REGEXP_MARK_START, |
209 | | XML_REGEXP_MARK_VISITED |
210 | | } xmlRegMarkedType; |
211 | | |
212 | | typedef struct _xmlRegRange xmlRegRange; |
213 | | typedef xmlRegRange *xmlRegRangePtr; |
214 | | |
215 | | struct _xmlRegRange { |
216 | | int neg; /* 0 normal, 1 not, 2 exclude */ |
217 | | xmlRegAtomType type; |
218 | | int start; |
219 | | int end; |
220 | | xmlChar *blockName; |
221 | | }; |
222 | | |
223 | | typedef struct _xmlRegAtom xmlRegAtom; |
224 | | typedef xmlRegAtom *xmlRegAtomPtr; |
225 | | |
226 | | typedef struct _xmlAutomataState xmlRegState; |
227 | | typedef xmlRegState *xmlRegStatePtr; |
228 | | |
229 | | struct _xmlRegAtom { |
230 | | int no; |
231 | | xmlRegAtomType type; |
232 | | xmlRegQuantType quant; |
233 | | int min; |
234 | | int max; |
235 | | |
236 | | void *valuep; |
237 | | void *valuep2; |
238 | | int neg; |
239 | | int codepoint; |
240 | | xmlRegStatePtr start; |
241 | | xmlRegStatePtr start0; |
242 | | xmlRegStatePtr stop; |
243 | | int maxRanges; |
244 | | int nbRanges; |
245 | | xmlRegRangePtr *ranges; |
246 | | void *data; |
247 | | }; |
248 | | |
249 | | typedef struct _xmlRegCounter xmlRegCounter; |
250 | | typedef xmlRegCounter *xmlRegCounterPtr; |
251 | | |
252 | | struct _xmlRegCounter { |
253 | | int min; |
254 | | int max; |
255 | | }; |
256 | | |
257 | | typedef struct _xmlRegTrans xmlRegTrans; |
258 | | typedef xmlRegTrans *xmlRegTransPtr; |
259 | | |
260 | | struct _xmlRegTrans { |
261 | | xmlRegAtomPtr atom; |
262 | | int to; |
263 | | int counter; |
264 | | int count; |
265 | | int nd; |
266 | | }; |
267 | | |
268 | | struct _xmlAutomataState { |
269 | | xmlRegStateType type; |
270 | | xmlRegMarkedType mark; |
271 | | xmlRegMarkedType markd; |
272 | | xmlRegMarkedType reached; |
273 | | int no; |
274 | | int maxTrans; |
275 | | int nbTrans; |
276 | | xmlRegTrans *trans; |
277 | | /* knowing states pointing to us can speed things up */ |
278 | | int maxTransTo; |
279 | | int nbTransTo; |
280 | | int *transTo; |
281 | | }; |
282 | | |
283 | | typedef struct _xmlAutomata xmlRegParserCtxt; |
284 | | typedef xmlRegParserCtxt *xmlRegParserCtxtPtr; |
285 | | |
286 | 0 | #define AM_AUTOMATA_RNG 1 |
287 | | |
288 | | struct _xmlAutomata { |
289 | | xmlChar *string; |
290 | | xmlChar *cur; |
291 | | |
292 | | int error; |
293 | | int neg; |
294 | | |
295 | | xmlRegStatePtr start; |
296 | | xmlRegStatePtr end; |
297 | | xmlRegStatePtr state; |
298 | | |
299 | | xmlRegAtomPtr atom; |
300 | | |
301 | | int maxAtoms; |
302 | | int nbAtoms; |
303 | | xmlRegAtomPtr *atoms; |
304 | | |
305 | | int maxStates; |
306 | | int nbStates; |
307 | | xmlRegStatePtr *states; |
308 | | |
309 | | int maxCounters; |
310 | | int nbCounters; |
311 | | xmlRegCounter *counters; |
312 | | |
313 | | int determinist; |
314 | | int negs; |
315 | | int flags; |
316 | | |
317 | | int depth; |
318 | | }; |
319 | | |
320 | | struct _xmlRegexp { |
321 | | xmlChar *string; |
322 | | int nbStates; |
323 | | xmlRegStatePtr *states; |
324 | | int nbAtoms; |
325 | | xmlRegAtomPtr *atoms; |
326 | | int nbCounters; |
327 | | xmlRegCounter *counters; |
328 | | int determinist; |
329 | | int flags; |
330 | | /* |
331 | | * That's the compact form for determinists automatas |
332 | | */ |
333 | | int nbstates; |
334 | | int *compact; |
335 | | void **transdata; |
336 | | int nbstrings; |
337 | | xmlChar **stringMap; |
338 | | }; |
339 | | |
340 | | typedef struct _xmlRegExecRollback xmlRegExecRollback; |
341 | | typedef xmlRegExecRollback *xmlRegExecRollbackPtr; |
342 | | |
343 | | struct _xmlRegExecRollback { |
344 | | xmlRegStatePtr state;/* the current state */ |
345 | | int index; /* the index in the input stack */ |
346 | | int nextbranch; /* the next transition to explore in that state */ |
347 | | int *counts; /* save the automata state if it has some */ |
348 | | }; |
349 | | |
350 | | typedef struct _xmlRegInputToken xmlRegInputToken; |
351 | | typedef xmlRegInputToken *xmlRegInputTokenPtr; |
352 | | |
353 | | struct _xmlRegInputToken { |
354 | | xmlChar *value; |
355 | | void *data; |
356 | | }; |
357 | | |
358 | | struct _xmlRegExecCtxt { |
359 | | int status; /* execution status != 0 indicate an error */ |
360 | | int determinist; /* did we find an indeterministic behaviour */ |
361 | | xmlRegexpPtr comp; /* the compiled regexp */ |
362 | | xmlRegExecCallbacks callback; |
363 | | void *data; |
364 | | |
365 | | xmlRegStatePtr state;/* the current state */ |
366 | | int transno; /* the current transition on that state */ |
367 | | int transcount; /* the number of chars in char counted transitions */ |
368 | | |
369 | | /* |
370 | | * A stack of rollback states |
371 | | */ |
372 | | int maxRollbacks; |
373 | | int nbRollbacks; |
374 | | xmlRegExecRollback *rollbacks; |
375 | | |
376 | | /* |
377 | | * The state of the automata if any |
378 | | */ |
379 | | int *counts; |
380 | | |
381 | | /* |
382 | | * The input stack |
383 | | */ |
384 | | int inputStackMax; |
385 | | int inputStackNr; |
386 | | int index; |
387 | | int *charStack; |
388 | | const xmlChar *inputString; /* when operating on characters */ |
389 | | xmlRegInputTokenPtr inputStack;/* when operating on strings */ |
390 | | |
391 | | /* |
392 | | * error handling |
393 | | */ |
394 | | int errStateNo; /* the error state number */ |
395 | | xmlRegStatePtr errState; /* the error state */ |
396 | | xmlChar *errString; /* the string raising the error */ |
397 | | int *errCounts; /* counters at the error state */ |
398 | | int nbPush; |
399 | | }; |
400 | | |
401 | 0 | #define REGEXP_ALL_COUNTER 0x123456 |
402 | 0 | #define REGEXP_ALL_LAX_COUNTER 0x123457 |
403 | | |
404 | | static void xmlFAParseRegExp(xmlRegParserCtxtPtr ctxt, int top); |
405 | | static void xmlRegFreeState(xmlRegStatePtr state); |
406 | | static void xmlRegFreeAtom(xmlRegAtomPtr atom); |
407 | | static int xmlRegStrEqualWildcard(const xmlChar *expStr, const xmlChar *valStr); |
408 | | static int xmlRegCheckCharacter(xmlRegAtomPtr atom, int codepoint); |
409 | | static int xmlRegCheckCharacterRange(xmlRegAtomType type, int codepoint, |
410 | | int neg, int start, int end, const xmlChar *blockName); |
411 | | |
412 | | /************************************************************************ |
413 | | * * |
414 | | * Regexp memory error handler * |
415 | | * * |
416 | | ************************************************************************/ |
417 | | /** |
418 | | * Handle an out of memory condition |
419 | | * |
420 | | * @param ctxt regexp parser context |
421 | | */ |
422 | | static void |
423 | | xmlRegexpErrMemory(xmlRegParserCtxtPtr ctxt) |
424 | 0 | { |
425 | 0 | if (ctxt != NULL) |
426 | 0 | ctxt->error = XML_ERR_NO_MEMORY; |
427 | |
|
428 | 0 | xmlRaiseMemoryError(NULL, NULL, NULL, XML_FROM_REGEXP, NULL); |
429 | 0 | } |
430 | | |
431 | | /** |
432 | | * Handle a compilation failure |
433 | | * |
434 | | * @param ctxt regexp parser context |
435 | | * @param extra extra information |
436 | | */ |
437 | | static void |
438 | | xmlRegexpErrCompile(xmlRegParserCtxtPtr ctxt, const char *extra) |
439 | 0 | { |
440 | 0 | const char *regexp = NULL; |
441 | 0 | int idx = 0; |
442 | 0 | int res; |
443 | |
|
444 | 0 | if (ctxt != NULL) { |
445 | 0 | regexp = (const char *) ctxt->string; |
446 | 0 | idx = ctxt->cur - ctxt->string; |
447 | 0 | ctxt->error = XML_REGEXP_COMPILE_ERROR; |
448 | 0 | } |
449 | |
|
450 | 0 | res = xmlRaiseError(NULL, NULL, NULL, NULL, NULL, XML_FROM_REGEXP, |
451 | 0 | XML_REGEXP_COMPILE_ERROR, XML_ERR_FATAL, |
452 | 0 | NULL, 0, extra, regexp, NULL, idx, 0, |
453 | 0 | "failed to compile: %s\n", extra); |
454 | 0 | if (res < 0) |
455 | 0 | xmlRegexpErrMemory(ctxt); |
456 | 0 | } |
457 | | |
458 | | /************************************************************************ |
459 | | * * |
460 | | * Allocation/Deallocation * |
461 | | * * |
462 | | ************************************************************************/ |
463 | | |
464 | | static int xmlFAComputesDeterminism(xmlRegParserCtxtPtr ctxt); |
465 | | |
466 | | /** |
467 | | * Allocate a two-dimensional array and set all elements to zero. |
468 | | * |
469 | | * @param dim1 size of first dimension |
470 | | * @param dim2 size of second dimension |
471 | | * @param elemSize size of element |
472 | | * @returns the new array or NULL in case of error. |
473 | | */ |
474 | | static void* |
475 | 0 | xmlRegCalloc2(size_t dim1, size_t dim2, size_t elemSize) { |
476 | 0 | size_t totalSize; |
477 | 0 | void *ret; |
478 | | |
479 | | /* Check for overflow */ |
480 | 0 | if ((dim2 == 0) || (elemSize == 0) || |
481 | 0 | (dim1 > SIZE_MAX / dim2 / elemSize)) |
482 | 0 | return (NULL); |
483 | 0 | totalSize = dim1 * dim2 * elemSize; |
484 | 0 | ret = xmlMalloc(totalSize); |
485 | 0 | if (ret != NULL) |
486 | 0 | memset(ret, 0, totalSize); |
487 | 0 | return (ret); |
488 | 0 | } |
489 | | |
490 | | /** |
491 | | * Allocate a new regexp and fill it with the result from the parser |
492 | | * |
493 | | * @param ctxt the parser context used to build it |
494 | | * @returns the new regexp or NULL in case of error |
495 | | */ |
496 | | static xmlRegexpPtr |
497 | 0 | xmlRegEpxFromParse(xmlRegParserCtxtPtr ctxt) { |
498 | 0 | xmlRegexpPtr ret; |
499 | |
|
500 | 0 | ret = (xmlRegexpPtr) xmlMalloc(sizeof(xmlRegexp)); |
501 | 0 | if (ret == NULL) { |
502 | 0 | xmlRegexpErrMemory(ctxt); |
503 | 0 | return(NULL); |
504 | 0 | } |
505 | 0 | memset(ret, 0, sizeof(xmlRegexp)); |
506 | 0 | ret->string = ctxt->string; |
507 | 0 | ret->nbStates = ctxt->nbStates; |
508 | 0 | ret->states = ctxt->states; |
509 | 0 | ret->nbAtoms = ctxt->nbAtoms; |
510 | 0 | ret->atoms = ctxt->atoms; |
511 | 0 | ret->nbCounters = ctxt->nbCounters; |
512 | 0 | ret->counters = ctxt->counters; |
513 | 0 | ret->determinist = ctxt->determinist; |
514 | 0 | ret->flags = ctxt->flags; |
515 | 0 | if (ret->determinist == -1) { |
516 | 0 | if (xmlRegexpIsDeterminist(ret) < 0) { |
517 | 0 | xmlRegexpErrMemory(ctxt); |
518 | 0 | xmlFree(ret); |
519 | 0 | return(NULL); |
520 | 0 | } |
521 | 0 | } |
522 | | |
523 | 0 | if ((ret->determinist != 0) && |
524 | 0 | (ret->nbCounters == 0) && |
525 | 0 | (ctxt->negs == 0) && |
526 | 0 | (ret->atoms != NULL) && |
527 | 0 | (ret->atoms[0] != NULL) && |
528 | 0 | (ret->atoms[0]->type == XML_REGEXP_STRING)) { |
529 | 0 | int i, j, nbstates = 0, nbatoms = 0; |
530 | 0 | int *stateRemap; |
531 | 0 | int *stringRemap; |
532 | 0 | int *transitions; |
533 | 0 | void **transdata; |
534 | 0 | xmlChar **stringMap; |
535 | 0 | xmlChar *value; |
536 | | |
537 | | /* |
538 | | * Switch to a compact representation |
539 | | * 1/ counting the effective number of states left |
540 | | * 2/ counting the unique number of atoms, and check that |
541 | | * they are all of the string type |
542 | | * 3/ build a table state x atom for the transitions |
543 | | */ |
544 | |
|
545 | 0 | stateRemap = xmlMalloc(ret->nbStates * sizeof(int)); |
546 | 0 | if (stateRemap == NULL) { |
547 | 0 | xmlRegexpErrMemory(ctxt); |
548 | 0 | xmlFree(ret); |
549 | 0 | return(NULL); |
550 | 0 | } |
551 | 0 | for (i = 0;i < ret->nbStates;i++) { |
552 | 0 | if (ret->states[i] != NULL) { |
553 | 0 | stateRemap[i] = nbstates; |
554 | 0 | nbstates++; |
555 | 0 | } else { |
556 | 0 | stateRemap[i] = -1; |
557 | 0 | } |
558 | 0 | } |
559 | 0 | stringMap = xmlMalloc(ret->nbAtoms * sizeof(char *)); |
560 | 0 | if (stringMap == NULL) { |
561 | 0 | xmlRegexpErrMemory(ctxt); |
562 | 0 | xmlFree(stateRemap); |
563 | 0 | xmlFree(ret); |
564 | 0 | return(NULL); |
565 | 0 | } |
566 | 0 | stringRemap = xmlMalloc(ret->nbAtoms * sizeof(int)); |
567 | 0 | if (stringRemap == NULL) { |
568 | 0 | xmlRegexpErrMemory(ctxt); |
569 | 0 | xmlFree(stringMap); |
570 | 0 | xmlFree(stateRemap); |
571 | 0 | xmlFree(ret); |
572 | 0 | return(NULL); |
573 | 0 | } |
574 | 0 | for (i = 0;i < ret->nbAtoms;i++) { |
575 | 0 | if ((ret->atoms[i]->type == XML_REGEXP_STRING) && |
576 | 0 | (ret->atoms[i]->quant == XML_REGEXP_QUANT_ONCE)) { |
577 | 0 | value = ret->atoms[i]->valuep; |
578 | 0 | for (j = 0;j < nbatoms;j++) { |
579 | 0 | if (xmlStrEqual(stringMap[j], value)) { |
580 | 0 | stringRemap[i] = j; |
581 | 0 | break; |
582 | 0 | } |
583 | 0 | } |
584 | 0 | if (j >= nbatoms) { |
585 | 0 | stringRemap[i] = nbatoms; |
586 | 0 | stringMap[nbatoms] = xmlStrdup(value); |
587 | 0 | if (stringMap[nbatoms] == NULL) { |
588 | 0 | for (i = 0;i < nbatoms;i++) |
589 | 0 | xmlFree(stringMap[i]); |
590 | 0 | xmlFree(stringRemap); |
591 | 0 | xmlFree(stringMap); |
592 | 0 | xmlFree(stateRemap); |
593 | 0 | xmlFree(ret); |
594 | 0 | return(NULL); |
595 | 0 | } |
596 | 0 | nbatoms++; |
597 | 0 | } |
598 | 0 | } else { |
599 | 0 | xmlFree(stateRemap); |
600 | 0 | xmlFree(stringRemap); |
601 | 0 | for (i = 0;i < nbatoms;i++) |
602 | 0 | xmlFree(stringMap[i]); |
603 | 0 | xmlFree(stringMap); |
604 | 0 | xmlFree(ret); |
605 | 0 | return(NULL); |
606 | 0 | } |
607 | 0 | } |
608 | 0 | transitions = (int *) xmlRegCalloc2(nbstates + 1, nbatoms + 1, |
609 | 0 | sizeof(int)); |
610 | 0 | if (transitions == NULL) { |
611 | 0 | xmlFree(stateRemap); |
612 | 0 | xmlFree(stringRemap); |
613 | 0 | for (i = 0;i < nbatoms;i++) |
614 | 0 | xmlFree(stringMap[i]); |
615 | 0 | xmlFree(stringMap); |
616 | 0 | xmlFree(ret); |
617 | 0 | return(NULL); |
618 | 0 | } |
619 | | |
620 | | /* |
621 | | * Allocate the transition table. The first entry for each |
622 | | * state corresponds to the state type. |
623 | | */ |
624 | 0 | transdata = NULL; |
625 | |
|
626 | 0 | for (i = 0;i < ret->nbStates;i++) { |
627 | 0 | int stateno, atomno, targetno, prev; |
628 | 0 | xmlRegStatePtr state; |
629 | 0 | xmlRegTransPtr trans; |
630 | |
|
631 | 0 | stateno = stateRemap[i]; |
632 | 0 | if (stateno == -1) |
633 | 0 | continue; |
634 | 0 | state = ret->states[i]; |
635 | |
|
636 | 0 | transitions[stateno * (nbatoms + 1)] = state->type; |
637 | |
|
638 | 0 | for (j = 0;j < state->nbTrans;j++) { |
639 | 0 | trans = &(state->trans[j]); |
640 | 0 | if ((trans->to < 0) || (trans->atom == NULL)) |
641 | 0 | continue; |
642 | 0 | atomno = stringRemap[trans->atom->no]; |
643 | 0 | if ((trans->atom->data != NULL) && (transdata == NULL)) { |
644 | 0 | transdata = (void **) xmlRegCalloc2(nbstates, nbatoms, |
645 | 0 | sizeof(void *)); |
646 | 0 | if (transdata == NULL) { |
647 | 0 | xmlRegexpErrMemory(ctxt); |
648 | 0 | break; |
649 | 0 | } |
650 | 0 | } |
651 | 0 | targetno = stateRemap[trans->to]; |
652 | | /* |
653 | | * if the same atom can generate transitions to 2 different |
654 | | * states then it means the automata is not deterministic and |
655 | | * the compact form can't be used ! |
656 | | */ |
657 | 0 | prev = transitions[stateno * (nbatoms + 1) + atomno + 1]; |
658 | 0 | if (prev != 0) { |
659 | 0 | if (prev != targetno + 1) { |
660 | 0 | ret->determinist = 0; |
661 | 0 | if (transdata != NULL) |
662 | 0 | xmlFree(transdata); |
663 | 0 | xmlFree(transitions); |
664 | 0 | xmlFree(stateRemap); |
665 | 0 | xmlFree(stringRemap); |
666 | 0 | for (i = 0;i < nbatoms;i++) |
667 | 0 | xmlFree(stringMap[i]); |
668 | 0 | xmlFree(stringMap); |
669 | 0 | goto not_determ; |
670 | 0 | } |
671 | 0 | } else { |
672 | 0 | transitions[stateno * (nbatoms + 1) + atomno + 1] = |
673 | 0 | targetno + 1; /* to avoid 0 */ |
674 | 0 | if (transdata != NULL) |
675 | 0 | transdata[stateno * nbatoms + atomno] = |
676 | 0 | trans->atom->data; |
677 | 0 | } |
678 | 0 | } |
679 | 0 | } |
680 | 0 | ret->determinist = 1; |
681 | | /* |
682 | | * Cleanup of the old data |
683 | | */ |
684 | 0 | if (ret->states != NULL) { |
685 | 0 | for (i = 0;i < ret->nbStates;i++) |
686 | 0 | xmlRegFreeState(ret->states[i]); |
687 | 0 | xmlFree(ret->states); |
688 | 0 | } |
689 | 0 | ret->states = NULL; |
690 | 0 | ret->nbStates = 0; |
691 | 0 | if (ret->atoms != NULL) { |
692 | 0 | for (i = 0;i < ret->nbAtoms;i++) |
693 | 0 | xmlRegFreeAtom(ret->atoms[i]); |
694 | 0 | xmlFree(ret->atoms); |
695 | 0 | } |
696 | 0 | ret->atoms = NULL; |
697 | 0 | ret->nbAtoms = 0; |
698 | |
|
699 | 0 | ret->compact = transitions; |
700 | 0 | ret->transdata = transdata; |
701 | 0 | ret->stringMap = stringMap; |
702 | 0 | ret->nbstrings = nbatoms; |
703 | 0 | ret->nbstates = nbstates; |
704 | 0 | xmlFree(stateRemap); |
705 | 0 | xmlFree(stringRemap); |
706 | 0 | } |
707 | 0 | not_determ: |
708 | 0 | ctxt->string = NULL; |
709 | 0 | ctxt->nbStates = 0; |
710 | 0 | ctxt->states = NULL; |
711 | 0 | ctxt->nbAtoms = 0; |
712 | 0 | ctxt->atoms = NULL; |
713 | 0 | ctxt->nbCounters = 0; |
714 | 0 | ctxt->counters = NULL; |
715 | 0 | return(ret); |
716 | 0 | } |
717 | | |
718 | | /** |
719 | | * Allocate a new regexp parser context |
720 | | * |
721 | | * @param string the string to parse |
722 | | * @returns the new context or NULL in case of error |
723 | | */ |
724 | | static xmlRegParserCtxtPtr |
725 | 0 | xmlRegNewParserCtxt(const xmlChar *string) { |
726 | 0 | xmlRegParserCtxtPtr ret; |
727 | |
|
728 | 0 | ret = (xmlRegParserCtxtPtr) xmlMalloc(sizeof(xmlRegParserCtxt)); |
729 | 0 | if (ret == NULL) |
730 | 0 | return(NULL); |
731 | 0 | memset(ret, 0, sizeof(xmlRegParserCtxt)); |
732 | 0 | if (string != NULL) { |
733 | 0 | ret->string = xmlStrdup(string); |
734 | 0 | if (ret->string == NULL) { |
735 | 0 | xmlFree(ret); |
736 | 0 | return(NULL); |
737 | 0 | } |
738 | 0 | } |
739 | 0 | ret->cur = ret->string; |
740 | 0 | ret->neg = 0; |
741 | 0 | ret->negs = 0; |
742 | 0 | ret->error = 0; |
743 | 0 | ret->determinist = -1; |
744 | 0 | return(ret); |
745 | 0 | } |
746 | | |
747 | | /** |
748 | | * Allocate a new regexp range |
749 | | * |
750 | | * @param ctxt the regexp parser context |
751 | | * @param neg is that negative |
752 | | * @param type the type of range |
753 | | * @param start the start codepoint |
754 | | * @param end the end codepoint |
755 | | * @returns the new range or NULL in case of error |
756 | | */ |
757 | | static xmlRegRangePtr |
758 | | xmlRegNewRange(xmlRegParserCtxtPtr ctxt, |
759 | 0 | int neg, xmlRegAtomType type, int start, int end) { |
760 | 0 | xmlRegRangePtr ret; |
761 | |
|
762 | 0 | ret = (xmlRegRangePtr) xmlMalloc(sizeof(xmlRegRange)); |
763 | 0 | if (ret == NULL) { |
764 | 0 | xmlRegexpErrMemory(ctxt); |
765 | 0 | return(NULL); |
766 | 0 | } |
767 | 0 | ret->neg = neg; |
768 | 0 | ret->type = type; |
769 | 0 | ret->start = start; |
770 | 0 | ret->end = end; |
771 | 0 | return(ret); |
772 | 0 | } |
773 | | |
774 | | /** |
775 | | * Free a regexp range |
776 | | * |
777 | | * @param range the regexp range |
778 | | */ |
779 | | static void |
780 | 0 | xmlRegFreeRange(xmlRegRangePtr range) { |
781 | 0 | if (range == NULL) |
782 | 0 | return; |
783 | | |
784 | 0 | if (range->blockName != NULL) |
785 | 0 | xmlFree(range->blockName); |
786 | 0 | xmlFree(range); |
787 | 0 | } |
788 | | |
789 | | /** |
790 | | * Copy a regexp range |
791 | | * |
792 | | * @param ctxt regexp parser context |
793 | | * @param range the regexp range |
794 | | * @returns the new copy or NULL in case of error. |
795 | | */ |
796 | | static xmlRegRangePtr |
797 | 0 | xmlRegCopyRange(xmlRegParserCtxtPtr ctxt, xmlRegRangePtr range) { |
798 | 0 | xmlRegRangePtr ret; |
799 | |
|
800 | 0 | if (range == NULL) |
801 | 0 | return(NULL); |
802 | | |
803 | 0 | ret = xmlRegNewRange(ctxt, range->neg, range->type, range->start, |
804 | 0 | range->end); |
805 | 0 | if (ret == NULL) |
806 | 0 | return(NULL); |
807 | 0 | if (range->blockName != NULL) { |
808 | 0 | ret->blockName = xmlStrdup(range->blockName); |
809 | 0 | if (ret->blockName == NULL) { |
810 | 0 | xmlRegexpErrMemory(ctxt); |
811 | 0 | xmlRegFreeRange(ret); |
812 | 0 | return(NULL); |
813 | 0 | } |
814 | 0 | } |
815 | 0 | return(ret); |
816 | 0 | } |
817 | | |
818 | | /** |
819 | | * Allocate a new atom |
820 | | * |
821 | | * @param ctxt the regexp parser context |
822 | | * @param type the type of atom |
823 | | * @returns the new atom or NULL in case of error |
824 | | */ |
825 | | static xmlRegAtomPtr |
826 | 0 | xmlRegNewAtom(xmlRegParserCtxtPtr ctxt, xmlRegAtomType type) { |
827 | 0 | xmlRegAtomPtr ret; |
828 | |
|
829 | 0 | ret = (xmlRegAtomPtr) xmlMalloc(sizeof(xmlRegAtom)); |
830 | 0 | if (ret == NULL) { |
831 | 0 | xmlRegexpErrMemory(ctxt); |
832 | 0 | return(NULL); |
833 | 0 | } |
834 | 0 | memset(ret, 0, sizeof(xmlRegAtom)); |
835 | 0 | ret->type = type; |
836 | 0 | ret->quant = XML_REGEXP_QUANT_ONCE; |
837 | 0 | ret->min = 0; |
838 | 0 | ret->max = 0; |
839 | 0 | return(ret); |
840 | 0 | } |
841 | | |
842 | | /** |
843 | | * Free a regexp atom |
844 | | * |
845 | | * @param atom the regexp atom |
846 | | */ |
847 | | static void |
848 | 0 | xmlRegFreeAtom(xmlRegAtomPtr atom) { |
849 | 0 | int i; |
850 | |
|
851 | 0 | if (atom == NULL) |
852 | 0 | return; |
853 | | |
854 | 0 | for (i = 0;i < atom->nbRanges;i++) |
855 | 0 | xmlRegFreeRange(atom->ranges[i]); |
856 | 0 | if (atom->ranges != NULL) |
857 | 0 | xmlFree(atom->ranges); |
858 | 0 | if ((atom->type == XML_REGEXP_STRING) && (atom->valuep != NULL)) |
859 | 0 | xmlFree(atom->valuep); |
860 | 0 | if ((atom->type == XML_REGEXP_STRING) && (atom->valuep2 != NULL)) |
861 | 0 | xmlFree(atom->valuep2); |
862 | 0 | if ((atom->type == XML_REGEXP_BLOCK_NAME) && (atom->valuep != NULL)) |
863 | 0 | xmlFree(atom->valuep); |
864 | 0 | xmlFree(atom); |
865 | 0 | } |
866 | | |
867 | | /** |
868 | | * Allocate a new regexp range |
869 | | * |
870 | | * @param ctxt the regexp parser context |
871 | | * @param atom the original atom |
872 | | * @returns the new atom or NULL in case of error |
873 | | */ |
874 | | static xmlRegAtomPtr |
875 | 0 | xmlRegCopyAtom(xmlRegParserCtxtPtr ctxt, xmlRegAtomPtr atom) { |
876 | 0 | xmlRegAtomPtr ret; |
877 | |
|
878 | 0 | ret = (xmlRegAtomPtr) xmlMalloc(sizeof(xmlRegAtom)); |
879 | 0 | if (ret == NULL) { |
880 | 0 | xmlRegexpErrMemory(ctxt); |
881 | 0 | return(NULL); |
882 | 0 | } |
883 | 0 | memset(ret, 0, sizeof(xmlRegAtom)); |
884 | 0 | ret->type = atom->type; |
885 | 0 | ret->quant = atom->quant; |
886 | 0 | ret->min = atom->min; |
887 | 0 | ret->max = atom->max; |
888 | 0 | if (atom->nbRanges > 0) { |
889 | 0 | int i; |
890 | |
|
891 | 0 | ret->ranges = (xmlRegRangePtr *) xmlMalloc(sizeof(xmlRegRangePtr) * |
892 | 0 | atom->nbRanges); |
893 | 0 | if (ret->ranges == NULL) { |
894 | 0 | xmlRegexpErrMemory(ctxt); |
895 | 0 | goto error; |
896 | 0 | } |
897 | 0 | for (i = 0;i < atom->nbRanges;i++) { |
898 | 0 | ret->ranges[i] = xmlRegCopyRange(ctxt, atom->ranges[i]); |
899 | 0 | if (ret->ranges[i] == NULL) |
900 | 0 | goto error; |
901 | 0 | ret->nbRanges = i + 1; |
902 | 0 | } |
903 | 0 | } |
904 | 0 | return(ret); |
905 | | |
906 | 0 | error: |
907 | 0 | xmlRegFreeAtom(ret); |
908 | 0 | return(NULL); |
909 | 0 | } |
910 | | |
911 | | static xmlRegStatePtr |
912 | 0 | xmlRegNewState(xmlRegParserCtxtPtr ctxt) { |
913 | 0 | xmlRegStatePtr ret; |
914 | |
|
915 | 0 | ret = (xmlRegStatePtr) xmlMalloc(sizeof(xmlRegState)); |
916 | 0 | if (ret == NULL) { |
917 | 0 | xmlRegexpErrMemory(ctxt); |
918 | 0 | return(NULL); |
919 | 0 | } |
920 | 0 | memset(ret, 0, sizeof(xmlRegState)); |
921 | 0 | ret->type = XML_REGEXP_TRANS_STATE; |
922 | 0 | ret->mark = XML_REGEXP_MARK_NORMAL; |
923 | 0 | return(ret); |
924 | 0 | } |
925 | | |
926 | | /** |
927 | | * Free a regexp state |
928 | | * |
929 | | * @param state the regexp state |
930 | | */ |
931 | | static void |
932 | 0 | xmlRegFreeState(xmlRegStatePtr state) { |
933 | 0 | if (state == NULL) |
934 | 0 | return; |
935 | | |
936 | 0 | if (state->trans != NULL) |
937 | 0 | xmlFree(state->trans); |
938 | 0 | if (state->transTo != NULL) |
939 | 0 | xmlFree(state->transTo); |
940 | 0 | xmlFree(state); |
941 | 0 | } |
942 | | |
943 | | /** |
944 | | * Free a regexp parser context |
945 | | * |
946 | | * @param ctxt the regexp parser context |
947 | | */ |
948 | | static void |
949 | 0 | xmlRegFreeParserCtxt(xmlRegParserCtxtPtr ctxt) { |
950 | 0 | int i; |
951 | 0 | if (ctxt == NULL) |
952 | 0 | return; |
953 | | |
954 | 0 | if (ctxt->string != NULL) |
955 | 0 | xmlFree(ctxt->string); |
956 | 0 | if (ctxt->states != NULL) { |
957 | 0 | for (i = 0;i < ctxt->nbStates;i++) |
958 | 0 | xmlRegFreeState(ctxt->states[i]); |
959 | 0 | xmlFree(ctxt->states); |
960 | 0 | } |
961 | 0 | if (ctxt->atoms != NULL) { |
962 | 0 | for (i = 0;i < ctxt->nbAtoms;i++) |
963 | 0 | xmlRegFreeAtom(ctxt->atoms[i]); |
964 | 0 | xmlFree(ctxt->atoms); |
965 | 0 | } |
966 | 0 | if (ctxt->counters != NULL) |
967 | 0 | xmlFree(ctxt->counters); |
968 | 0 | xmlFree(ctxt); |
969 | 0 | } |
970 | | |
971 | | /************************************************************************ |
972 | | * * |
973 | | * Display of Data structures * |
974 | | * * |
975 | | ************************************************************************/ |
976 | | |
977 | | #ifdef DEBUG_REGEXP |
978 | | static void |
979 | | xmlRegPrintAtomType(FILE *output, xmlRegAtomType type) { |
980 | | switch (type) { |
981 | | case XML_REGEXP_EPSILON: |
982 | | fprintf(output, "epsilon "); break; |
983 | | case XML_REGEXP_CHARVAL: |
984 | | fprintf(output, "charval "); break; |
985 | | case XML_REGEXP_RANGES: |
986 | | fprintf(output, "ranges "); break; |
987 | | case XML_REGEXP_SUBREG: |
988 | | fprintf(output, "subexpr "); break; |
989 | | case XML_REGEXP_STRING: |
990 | | fprintf(output, "string "); break; |
991 | | case XML_REGEXP_ANYCHAR: |
992 | | fprintf(output, "anychar "); break; |
993 | | case XML_REGEXP_ANYSPACE: |
994 | | fprintf(output, "anyspace "); break; |
995 | | case XML_REGEXP_NOTSPACE: |
996 | | fprintf(output, "notspace "); break; |
997 | | case XML_REGEXP_INITNAME: |
998 | | fprintf(output, "initname "); break; |
999 | | case XML_REGEXP_NOTINITNAME: |
1000 | | fprintf(output, "notinitname "); break; |
1001 | | case XML_REGEXP_NAMECHAR: |
1002 | | fprintf(output, "namechar "); break; |
1003 | | case XML_REGEXP_NOTNAMECHAR: |
1004 | | fprintf(output, "notnamechar "); break; |
1005 | | case XML_REGEXP_DECIMAL: |
1006 | | fprintf(output, "decimal "); break; |
1007 | | case XML_REGEXP_NOTDECIMAL: |
1008 | | fprintf(output, "notdecimal "); break; |
1009 | | case XML_REGEXP_REALCHAR: |
1010 | | fprintf(output, "realchar "); break; |
1011 | | case XML_REGEXP_NOTREALCHAR: |
1012 | | fprintf(output, "notrealchar "); break; |
1013 | | case XML_REGEXP_LETTER: |
1014 | | fprintf(output, "LETTER "); break; |
1015 | | case XML_REGEXP_LETTER_UPPERCASE: |
1016 | | fprintf(output, "LETTER_UPPERCASE "); break; |
1017 | | case XML_REGEXP_LETTER_LOWERCASE: |
1018 | | fprintf(output, "LETTER_LOWERCASE "); break; |
1019 | | case XML_REGEXP_LETTER_TITLECASE: |
1020 | | fprintf(output, "LETTER_TITLECASE "); break; |
1021 | | case XML_REGEXP_LETTER_MODIFIER: |
1022 | | fprintf(output, "LETTER_MODIFIER "); break; |
1023 | | case XML_REGEXP_LETTER_OTHERS: |
1024 | | fprintf(output, "LETTER_OTHERS "); break; |
1025 | | case XML_REGEXP_MARK: |
1026 | | fprintf(output, "MARK "); break; |
1027 | | case XML_REGEXP_MARK_NONSPACING: |
1028 | | fprintf(output, "MARK_NONSPACING "); break; |
1029 | | case XML_REGEXP_MARK_SPACECOMBINING: |
1030 | | fprintf(output, "MARK_SPACECOMBINING "); break; |
1031 | | case XML_REGEXP_MARK_ENCLOSING: |
1032 | | fprintf(output, "MARK_ENCLOSING "); break; |
1033 | | case XML_REGEXP_NUMBER: |
1034 | | fprintf(output, "NUMBER "); break; |
1035 | | case XML_REGEXP_NUMBER_DECIMAL: |
1036 | | fprintf(output, "NUMBER_DECIMAL "); break; |
1037 | | case XML_REGEXP_NUMBER_LETTER: |
1038 | | fprintf(output, "NUMBER_LETTER "); break; |
1039 | | case XML_REGEXP_NUMBER_OTHERS: |
1040 | | fprintf(output, "NUMBER_OTHERS "); break; |
1041 | | case XML_REGEXP_PUNCT: |
1042 | | fprintf(output, "PUNCT "); break; |
1043 | | case XML_REGEXP_PUNCT_CONNECTOR: |
1044 | | fprintf(output, "PUNCT_CONNECTOR "); break; |
1045 | | case XML_REGEXP_PUNCT_DASH: |
1046 | | fprintf(output, "PUNCT_DASH "); break; |
1047 | | case XML_REGEXP_PUNCT_OPEN: |
1048 | | fprintf(output, "PUNCT_OPEN "); break; |
1049 | | case XML_REGEXP_PUNCT_CLOSE: |
1050 | | fprintf(output, "PUNCT_CLOSE "); break; |
1051 | | case XML_REGEXP_PUNCT_INITQUOTE: |
1052 | | fprintf(output, "PUNCT_INITQUOTE "); break; |
1053 | | case XML_REGEXP_PUNCT_FINQUOTE: |
1054 | | fprintf(output, "PUNCT_FINQUOTE "); break; |
1055 | | case XML_REGEXP_PUNCT_OTHERS: |
1056 | | fprintf(output, "PUNCT_OTHERS "); break; |
1057 | | case XML_REGEXP_SEPAR: |
1058 | | fprintf(output, "SEPAR "); break; |
1059 | | case XML_REGEXP_SEPAR_SPACE: |
1060 | | fprintf(output, "SEPAR_SPACE "); break; |
1061 | | case XML_REGEXP_SEPAR_LINE: |
1062 | | fprintf(output, "SEPAR_LINE "); break; |
1063 | | case XML_REGEXP_SEPAR_PARA: |
1064 | | fprintf(output, "SEPAR_PARA "); break; |
1065 | | case XML_REGEXP_SYMBOL: |
1066 | | fprintf(output, "SYMBOL "); break; |
1067 | | case XML_REGEXP_SYMBOL_MATH: |
1068 | | fprintf(output, "SYMBOL_MATH "); break; |
1069 | | case XML_REGEXP_SYMBOL_CURRENCY: |
1070 | | fprintf(output, "SYMBOL_CURRENCY "); break; |
1071 | | case XML_REGEXP_SYMBOL_MODIFIER: |
1072 | | fprintf(output, "SYMBOL_MODIFIER "); break; |
1073 | | case XML_REGEXP_SYMBOL_OTHERS: |
1074 | | fprintf(output, "SYMBOL_OTHERS "); break; |
1075 | | case XML_REGEXP_OTHER: |
1076 | | fprintf(output, "OTHER "); break; |
1077 | | case XML_REGEXP_OTHER_CONTROL: |
1078 | | fprintf(output, "OTHER_CONTROL "); break; |
1079 | | case XML_REGEXP_OTHER_FORMAT: |
1080 | | fprintf(output, "OTHER_FORMAT "); break; |
1081 | | case XML_REGEXP_OTHER_PRIVATE: |
1082 | | fprintf(output, "OTHER_PRIVATE "); break; |
1083 | | case XML_REGEXP_OTHER_NA: |
1084 | | fprintf(output, "OTHER_NA "); break; |
1085 | | case XML_REGEXP_BLOCK_NAME: |
1086 | | fprintf(output, "BLOCK "); break; |
1087 | | } |
1088 | | } |
1089 | | |
1090 | | static void |
1091 | | xmlRegPrintQuantType(FILE *output, xmlRegQuantType type) { |
1092 | | switch (type) { |
1093 | | case XML_REGEXP_QUANT_EPSILON: |
1094 | | fprintf(output, "epsilon "); break; |
1095 | | case XML_REGEXP_QUANT_ONCE: |
1096 | | fprintf(output, "once "); break; |
1097 | | case XML_REGEXP_QUANT_OPT: |
1098 | | fprintf(output, "? "); break; |
1099 | | case XML_REGEXP_QUANT_MULT: |
1100 | | fprintf(output, "* "); break; |
1101 | | case XML_REGEXP_QUANT_PLUS: |
1102 | | fprintf(output, "+ "); break; |
1103 | | case XML_REGEXP_QUANT_RANGE: |
1104 | | fprintf(output, "range "); break; |
1105 | | case XML_REGEXP_QUANT_ONCEONLY: |
1106 | | fprintf(output, "onceonly "); break; |
1107 | | case XML_REGEXP_QUANT_ALL: |
1108 | | fprintf(output, "all "); break; |
1109 | | } |
1110 | | } |
1111 | | static void |
1112 | | xmlRegPrintRange(FILE *output, xmlRegRangePtr range) { |
1113 | | fprintf(output, " range: "); |
1114 | | if (range->neg) |
1115 | | fprintf(output, "negative "); |
1116 | | xmlRegPrintAtomType(output, range->type); |
1117 | | fprintf(output, "%c - %c\n", range->start, range->end); |
1118 | | } |
1119 | | |
1120 | | static void |
1121 | | xmlRegPrintAtom(FILE *output, xmlRegAtomPtr atom) { |
1122 | | fprintf(output, " atom: "); |
1123 | | if (atom == NULL) { |
1124 | | fprintf(output, "NULL\n"); |
1125 | | return; |
1126 | | } |
1127 | | if (atom->neg) |
1128 | | fprintf(output, "not "); |
1129 | | xmlRegPrintAtomType(output, atom->type); |
1130 | | xmlRegPrintQuantType(output, atom->quant); |
1131 | | if (atom->quant == XML_REGEXP_QUANT_RANGE) |
1132 | | fprintf(output, "%d-%d ", atom->min, atom->max); |
1133 | | if (atom->type == XML_REGEXP_STRING) |
1134 | | fprintf(output, "'%s' ", (char *) atom->valuep); |
1135 | | if (atom->type == XML_REGEXP_CHARVAL) |
1136 | | fprintf(output, "char %c\n", atom->codepoint); |
1137 | | else if (atom->type == XML_REGEXP_RANGES) { |
1138 | | int i; |
1139 | | fprintf(output, "%d entries\n", atom->nbRanges); |
1140 | | for (i = 0; i < atom->nbRanges;i++) |
1141 | | xmlRegPrintRange(output, atom->ranges[i]); |
1142 | | } else { |
1143 | | fprintf(output, "\n"); |
1144 | | } |
1145 | | } |
1146 | | |
1147 | | static void |
1148 | | xmlRegPrintAtomCompact(FILE* output, xmlRegexpPtr regexp, int atom) |
1149 | | { |
1150 | | if (output == NULL || regexp == NULL || atom < 0 || |
1151 | | atom >= regexp->nbstrings) { |
1152 | | return; |
1153 | | } |
1154 | | fprintf(output, " atom: "); |
1155 | | |
1156 | | xmlRegPrintAtomType(output, XML_REGEXP_STRING); |
1157 | | xmlRegPrintQuantType(output, XML_REGEXP_QUANT_ONCE); |
1158 | | fprintf(output, "'%s' ", (char *) regexp->stringMap[atom]); |
1159 | | fprintf(output, "\n"); |
1160 | | } |
1161 | | |
1162 | | static void |
1163 | | xmlRegPrintTrans(FILE *output, xmlRegTransPtr trans) { |
1164 | | fprintf(output, " trans: "); |
1165 | | if (trans == NULL) { |
1166 | | fprintf(output, "NULL\n"); |
1167 | | return; |
1168 | | } |
1169 | | if (trans->to < 0) { |
1170 | | fprintf(output, "removed\n"); |
1171 | | return; |
1172 | | } |
1173 | | if (trans->nd != 0) { |
1174 | | if (trans->nd == 2) |
1175 | | fprintf(output, "last not determinist, "); |
1176 | | else |
1177 | | fprintf(output, "not determinist, "); |
1178 | | } |
1179 | | if (trans->counter >= 0) { |
1180 | | fprintf(output, "counted %d, ", trans->counter); |
1181 | | } |
1182 | | if (trans->count == REGEXP_ALL_COUNTER) { |
1183 | | fprintf(output, "all transition, "); |
1184 | | } else if (trans->count >= 0) { |
1185 | | fprintf(output, "count based %d, ", trans->count); |
1186 | | } |
1187 | | if (trans->atom == NULL) { |
1188 | | fprintf(output, "epsilon to %d\n", trans->to); |
1189 | | return; |
1190 | | } |
1191 | | if (trans->atom->type == XML_REGEXP_CHARVAL) |
1192 | | fprintf(output, "char %c ", trans->atom->codepoint); |
1193 | | fprintf(output, "atom %d, to %d\n", trans->atom->no, trans->to); |
1194 | | } |
1195 | | |
1196 | | static void |
1197 | | xmlRegPrintTransCompact( |
1198 | | FILE* output, |
1199 | | xmlRegexpPtr regexp, |
1200 | | int state, |
1201 | | int atom |
1202 | | ) |
1203 | | { |
1204 | | int target; |
1205 | | if (output == NULL || regexp == NULL || regexp->compact == NULL || |
1206 | | state < 0 || atom < 0) { |
1207 | | return; |
1208 | | } |
1209 | | target = regexp->compact[state * (regexp->nbstrings + 1) + atom + 1]; |
1210 | | fprintf(output, " trans: "); |
1211 | | |
1212 | | /* TODO maybe skip 'removed' transitions, because they actually never existed */ |
1213 | | if (target < 0) { |
1214 | | fprintf(output, "removed\n"); |
1215 | | return; |
1216 | | } |
1217 | | |
1218 | | /* We will ignore most of the attributes used in xmlRegPrintTrans, |
1219 | | * since the compact form is much simpler and uses only a part of the |
1220 | | * features provided by the libxml2 regexp libary |
1221 | | * (no rollbacks, counters etc.) */ |
1222 | | |
1223 | | /* Compared to the standard representation, an automata written using the |
1224 | | * compact form will ALWAYS be deterministic! |
1225 | | * From xmlRegPrintTrans: |
1226 | | if (trans->nd != 0) { |
1227 | | ... |
1228 | | * trans->nd will always be 0! */ |
1229 | | |
1230 | | /* In automata represented in compact form, the transitions will not use |
1231 | | * counters. |
1232 | | * From xmlRegPrintTrans: |
1233 | | if (trans->counter >= 0) { |
1234 | | ... |
1235 | | * regexp->counters == NULL, so trans->counter < 0 */ |
1236 | | |
1237 | | /* In compact form, we won't use */ |
1238 | | |
1239 | | /* An automata in the compact representation will always use string |
1240 | | * atoms. |
1241 | | * From xmlRegPrintTrans: |
1242 | | if (trans->atom->type == XML_REGEXP_CHARVAL) |
1243 | | ... |
1244 | | * trans->atom != NULL && trans->atom->type == XML_REGEXP_STRING */ |
1245 | | |
1246 | | fprintf(output, "atom %d, to %d\n", atom, target); |
1247 | | } |
1248 | | |
1249 | | static void |
1250 | | xmlRegPrintState(FILE *output, xmlRegStatePtr state) { |
1251 | | int i; |
1252 | | |
1253 | | fprintf(output, " state: "); |
1254 | | if (state == NULL) { |
1255 | | fprintf(output, "NULL\n"); |
1256 | | return; |
1257 | | } |
1258 | | if (state->type == XML_REGEXP_START_STATE) |
1259 | | fprintf(output, "START "); |
1260 | | if (state->type == XML_REGEXP_FINAL_STATE) |
1261 | | fprintf(output, "FINAL "); |
1262 | | |
1263 | | fprintf(output, "%d, %d transitions:\n", state->no, state->nbTrans); |
1264 | | for (i = 0;i < state->nbTrans; i++) { |
1265 | | xmlRegPrintTrans(output, &(state->trans[i])); |
1266 | | } |
1267 | | } |
1268 | | |
1269 | | static void |
1270 | | xmlRegPrintStateCompact(FILE* output, xmlRegexpPtr regexp, int state) |
1271 | | { |
1272 | | int nbTrans = 0; |
1273 | | int i; |
1274 | | int target; |
1275 | | xmlRegStateType stateType; |
1276 | | |
1277 | | if (output == NULL || regexp == NULL || regexp->compact == NULL || |
1278 | | state < 0) { |
1279 | | return; |
1280 | | } |
1281 | | |
1282 | | fprintf(output, " state: "); |
1283 | | |
1284 | | stateType = regexp->compact[state * (regexp->nbstrings + 1)]; |
1285 | | if (stateType == XML_REGEXP_START_STATE) { |
1286 | | fprintf(output, " START "); |
1287 | | } |
1288 | | |
1289 | | if (stateType == XML_REGEXP_FINAL_STATE) { |
1290 | | fprintf(output, " FINAL "); |
1291 | | } |
1292 | | |
1293 | | /* Print all atoms. */ |
1294 | | for (i = 0; i < regexp->nbstrings; i++) { |
1295 | | xmlRegPrintAtomCompact(output, regexp, i); |
1296 | | } |
1297 | | |
1298 | | /* Count all the transitions from the compact representation. */ |
1299 | | for (i = 0; i < regexp->nbstrings; i++) { |
1300 | | target = regexp->compact[state * (regexp->nbstrings + 1) + i + 1]; |
1301 | | if (target > 0 && target <= regexp->nbstates && |
1302 | | regexp->compact[(target - 1) * (regexp->nbstrings + 1)] == |
1303 | | XML_REGEXP_SINK_STATE) { |
1304 | | nbTrans++; |
1305 | | } |
1306 | | } |
1307 | | |
1308 | | fprintf(output, "%d, %d transitions:\n", state, nbTrans); |
1309 | | |
1310 | | /* Print all transitions */ |
1311 | | for (i = 0; i < regexp->nbstrings; i++) { |
1312 | | xmlRegPrintTransCompact(output, regexp, state, i); |
1313 | | } |
1314 | | } |
1315 | | |
1316 | | /* |
1317 | | * @param output an output stream |
1318 | | * @param regexp the regexp instance |
1319 | | * |
1320 | | * Print the compact representation of a regexp, in the same fashion as the |
1321 | | * public #xmlRegexpPrint function. |
1322 | | */ |
1323 | | static void |
1324 | | xmlRegPrintCompact(FILE* output, xmlRegexpPtr regexp) |
1325 | | { |
1326 | | int i; |
1327 | | if (output == NULL || regexp == NULL || regexp->compact == NULL) { |
1328 | | return; |
1329 | | } |
1330 | | |
1331 | | fprintf(output, "'%s' ", regexp->string); |
1332 | | |
1333 | | fprintf(output, "%d atoms:\n", regexp->nbstrings); |
1334 | | fprintf(output, "\n"); |
1335 | | for (i = 0; i < regexp->nbstrings; i++) { |
1336 | | fprintf(output, " %02d ", i); |
1337 | | xmlRegPrintAtomCompact(output, regexp, i); |
1338 | | } |
1339 | | |
1340 | | fprintf(output, "%d states:", regexp->nbstates); |
1341 | | fprintf(output, "\n"); |
1342 | | for (i = 0; i < regexp->nbstates; i++) { |
1343 | | xmlRegPrintStateCompact(output, regexp, i); |
1344 | | } |
1345 | | |
1346 | | fprintf(output, "%d counters:\n", 0); |
1347 | | } |
1348 | | |
1349 | | static void |
1350 | | xmlRegexpPrintInternal(FILE *output, xmlRegexpPtr regexp) { |
1351 | | int i; |
1352 | | |
1353 | | if (output == NULL) |
1354 | | return; |
1355 | | fprintf(output, " regexp: "); |
1356 | | if (regexp == NULL) { |
1357 | | fprintf(output, "NULL\n"); |
1358 | | return; |
1359 | | } |
1360 | | if (regexp->compact) { |
1361 | | xmlRegPrintCompact(output, regexp); |
1362 | | return; |
1363 | | } |
1364 | | |
1365 | | fprintf(output, "'%s' ", regexp->string); |
1366 | | fprintf(output, "\n"); |
1367 | | fprintf(output, "%d atoms:\n", regexp->nbAtoms); |
1368 | | for (i = 0;i < regexp->nbAtoms; i++) { |
1369 | | fprintf(output, " %02d ", i); |
1370 | | xmlRegPrintAtom(output, regexp->atoms[i]); |
1371 | | } |
1372 | | fprintf(output, "%d states:", regexp->nbStates); |
1373 | | fprintf(output, "\n"); |
1374 | | for (i = 0;i < regexp->nbStates; i++) { |
1375 | | xmlRegPrintState(output, regexp->states[i]); |
1376 | | } |
1377 | | fprintf(output, "%d counters:\n", regexp->nbCounters); |
1378 | | for (i = 0;i < regexp->nbCounters; i++) { |
1379 | | fprintf(output, " %d: min %d max %d\n", i, regexp->counters[i].min, |
1380 | | regexp->counters[i].max); |
1381 | | } |
1382 | | } |
1383 | | #endif /* DEBUG_REGEXP */ |
1384 | | |
1385 | | /************************************************************************ |
1386 | | * * |
1387 | | * Finite Automata structures manipulations * |
1388 | | * * |
1389 | | ************************************************************************/ |
1390 | | |
1391 | | static xmlRegRangePtr |
1392 | | xmlRegAtomAddRange(xmlRegParserCtxtPtr ctxt, xmlRegAtomPtr atom, |
1393 | | int neg, xmlRegAtomType type, int start, int end, |
1394 | 0 | xmlChar *blockName) { |
1395 | 0 | xmlRegRangePtr range; |
1396 | |
|
1397 | 0 | if (atom == NULL) { |
1398 | 0 | ERROR("add range: atom is NULL"); |
1399 | 0 | return(NULL); |
1400 | 0 | } |
1401 | 0 | if (atom->type != XML_REGEXP_RANGES) { |
1402 | 0 | ERROR("add range: atom is not ranges"); |
1403 | 0 | return(NULL); |
1404 | 0 | } |
1405 | 0 | if (atom->nbRanges >= atom->maxRanges) { |
1406 | 0 | xmlRegRangePtr *tmp; |
1407 | 0 | int newSize; |
1408 | |
|
1409 | 0 | newSize = xmlGrowCapacity(atom->maxRanges, sizeof(tmp[0]), |
1410 | 0 | 4, XML_MAX_ITEMS); |
1411 | 0 | if (newSize < 0) { |
1412 | 0 | xmlRegexpErrMemory(ctxt); |
1413 | 0 | return(NULL); |
1414 | 0 | } |
1415 | 0 | tmp = xmlRealloc(atom->ranges, newSize * sizeof(tmp[0])); |
1416 | 0 | if (tmp == NULL) { |
1417 | 0 | xmlRegexpErrMemory(ctxt); |
1418 | 0 | return(NULL); |
1419 | 0 | } |
1420 | 0 | atom->ranges = tmp; |
1421 | 0 | atom->maxRanges = newSize; |
1422 | 0 | } |
1423 | 0 | range = xmlRegNewRange(ctxt, neg, type, start, end); |
1424 | 0 | if (range == NULL) |
1425 | 0 | return(NULL); |
1426 | 0 | range->blockName = blockName; |
1427 | 0 | atom->ranges[atom->nbRanges++] = range; |
1428 | |
|
1429 | 0 | return(range); |
1430 | 0 | } |
1431 | | |
1432 | | static int |
1433 | 0 | xmlRegGetCounter(xmlRegParserCtxtPtr ctxt) { |
1434 | 0 | if (ctxt->nbCounters >= ctxt->maxCounters) { |
1435 | 0 | xmlRegCounter *tmp; |
1436 | 0 | int newSize; |
1437 | |
|
1438 | 0 | newSize = xmlGrowCapacity(ctxt->maxCounters, sizeof(tmp[0]), |
1439 | 0 | 4, XML_MAX_ITEMS); |
1440 | 0 | if (newSize < 0) { |
1441 | 0 | xmlRegexpErrMemory(ctxt); |
1442 | 0 | return(-1); |
1443 | 0 | } |
1444 | 0 | tmp = xmlRealloc(ctxt->counters, newSize * sizeof(tmp[0])); |
1445 | 0 | if (tmp == NULL) { |
1446 | 0 | xmlRegexpErrMemory(ctxt); |
1447 | 0 | return(-1); |
1448 | 0 | } |
1449 | 0 | ctxt->counters = tmp; |
1450 | 0 | ctxt->maxCounters = newSize; |
1451 | 0 | } |
1452 | 0 | ctxt->counters[ctxt->nbCounters].min = -1; |
1453 | 0 | ctxt->counters[ctxt->nbCounters].max = -1; |
1454 | 0 | return(ctxt->nbCounters++); |
1455 | 0 | } |
1456 | | |
1457 | | static int |
1458 | 0 | xmlRegAtomPush(xmlRegParserCtxtPtr ctxt, xmlRegAtomPtr atom) { |
1459 | 0 | if (atom == NULL) { |
1460 | 0 | ERROR("atom push: atom is NULL"); |
1461 | 0 | return(-1); |
1462 | 0 | } |
1463 | 0 | if (ctxt->nbAtoms >= ctxt->maxAtoms) { |
1464 | 0 | xmlRegAtomPtr *tmp; |
1465 | 0 | int newSize; |
1466 | |
|
1467 | 0 | newSize = xmlGrowCapacity(ctxt->maxAtoms, sizeof(tmp[0]), |
1468 | 0 | 4, XML_MAX_ITEMS); |
1469 | 0 | if (newSize < 0) { |
1470 | 0 | xmlRegexpErrMemory(ctxt); |
1471 | 0 | return(-1); |
1472 | 0 | } |
1473 | 0 | tmp = xmlRealloc(ctxt->atoms, newSize * sizeof(tmp[0])); |
1474 | 0 | if (tmp == NULL) { |
1475 | 0 | xmlRegexpErrMemory(ctxt); |
1476 | 0 | return(-1); |
1477 | 0 | } |
1478 | 0 | ctxt->atoms = tmp; |
1479 | 0 | ctxt->maxAtoms = newSize; |
1480 | 0 | } |
1481 | 0 | atom->no = ctxt->nbAtoms; |
1482 | 0 | ctxt->atoms[ctxt->nbAtoms++] = atom; |
1483 | 0 | return(0); |
1484 | 0 | } |
1485 | | |
1486 | | static void |
1487 | | xmlRegStateAddTransTo(xmlRegParserCtxtPtr ctxt, xmlRegStatePtr target, |
1488 | 0 | int from) { |
1489 | 0 | if (target->nbTransTo >= target->maxTransTo) { |
1490 | 0 | int *tmp; |
1491 | 0 | int newSize; |
1492 | |
|
1493 | 0 | newSize = xmlGrowCapacity(target->maxTransTo, sizeof(tmp[0]), |
1494 | 0 | 8, XML_MAX_ITEMS); |
1495 | 0 | if (newSize < 0) { |
1496 | 0 | xmlRegexpErrMemory(ctxt); |
1497 | 0 | return; |
1498 | 0 | } |
1499 | 0 | tmp = xmlRealloc(target->transTo, newSize * sizeof(tmp[0])); |
1500 | 0 | if (tmp == NULL) { |
1501 | 0 | xmlRegexpErrMemory(ctxt); |
1502 | 0 | return; |
1503 | 0 | } |
1504 | 0 | target->transTo = tmp; |
1505 | 0 | target->maxTransTo = newSize; |
1506 | 0 | } |
1507 | 0 | target->transTo[target->nbTransTo] = from; |
1508 | 0 | target->nbTransTo++; |
1509 | 0 | } |
1510 | | |
1511 | | static void |
1512 | | xmlRegStateAddTrans(xmlRegParserCtxtPtr ctxt, xmlRegStatePtr state, |
1513 | | xmlRegAtomPtr atom, xmlRegStatePtr target, |
1514 | 0 | int counter, int count) { |
1515 | |
|
1516 | 0 | int nrtrans; |
1517 | |
|
1518 | 0 | if (state == NULL) { |
1519 | 0 | ERROR("add state: state is NULL"); |
1520 | 0 | return; |
1521 | 0 | } |
1522 | 0 | if (target == NULL) { |
1523 | 0 | ERROR("add state: target is NULL"); |
1524 | 0 | return; |
1525 | 0 | } |
1526 | | /* |
1527 | | * Other routines follow the philosophy 'When in doubt, add a transition' |
1528 | | * so we check here whether such a transition is already present and, if |
1529 | | * so, silently ignore this request. |
1530 | | */ |
1531 | | |
1532 | 0 | for (nrtrans = state->nbTrans - 1; nrtrans >= 0; nrtrans--) { |
1533 | 0 | xmlRegTransPtr trans = &(state->trans[nrtrans]); |
1534 | 0 | if ((trans->atom == atom) && |
1535 | 0 | (trans->to == target->no) && |
1536 | 0 | (trans->counter == counter) && |
1537 | 0 | (trans->count == count)) { |
1538 | 0 | return; |
1539 | 0 | } |
1540 | 0 | } |
1541 | | |
1542 | 0 | if (state->nbTrans >= state->maxTrans) { |
1543 | 0 | xmlRegTrans *tmp; |
1544 | 0 | int newSize; |
1545 | |
|
1546 | 0 | newSize = xmlGrowCapacity(state->maxTrans, sizeof(tmp[0]), |
1547 | 0 | 8, XML_MAX_ITEMS); |
1548 | 0 | if (newSize < 0) { |
1549 | 0 | xmlRegexpErrMemory(ctxt); |
1550 | 0 | return; |
1551 | 0 | } |
1552 | 0 | tmp = xmlRealloc(state->trans, newSize * sizeof(tmp[0])); |
1553 | 0 | if (tmp == NULL) { |
1554 | 0 | xmlRegexpErrMemory(ctxt); |
1555 | 0 | return; |
1556 | 0 | } |
1557 | 0 | state->trans = tmp; |
1558 | 0 | state->maxTrans = newSize; |
1559 | 0 | } |
1560 | | |
1561 | 0 | state->trans[state->nbTrans].atom = atom; |
1562 | 0 | state->trans[state->nbTrans].to = target->no; |
1563 | 0 | state->trans[state->nbTrans].counter = counter; |
1564 | 0 | state->trans[state->nbTrans].count = count; |
1565 | 0 | state->trans[state->nbTrans].nd = 0; |
1566 | 0 | state->nbTrans++; |
1567 | 0 | xmlRegStateAddTransTo(ctxt, target, state->no); |
1568 | 0 | } |
1569 | | |
1570 | | static xmlRegStatePtr |
1571 | 0 | xmlRegStatePush(xmlRegParserCtxtPtr ctxt) { |
1572 | 0 | xmlRegStatePtr state; |
1573 | |
|
1574 | 0 | if (ctxt->nbStates >= ctxt->maxStates) { |
1575 | 0 | xmlRegStatePtr *tmp; |
1576 | 0 | int newSize; |
1577 | |
|
1578 | 0 | newSize = xmlGrowCapacity(ctxt->maxStates, sizeof(tmp[0]), |
1579 | 0 | 4, XML_MAX_ITEMS); |
1580 | 0 | if (newSize < 0) { |
1581 | 0 | xmlRegexpErrMemory(ctxt); |
1582 | 0 | return(NULL); |
1583 | 0 | } |
1584 | 0 | tmp = xmlRealloc(ctxt->states, newSize * sizeof(tmp[0])); |
1585 | 0 | if (tmp == NULL) { |
1586 | 0 | xmlRegexpErrMemory(ctxt); |
1587 | 0 | return(NULL); |
1588 | 0 | } |
1589 | 0 | ctxt->states = tmp; |
1590 | 0 | ctxt->maxStates = newSize; |
1591 | 0 | } |
1592 | | |
1593 | 0 | state = xmlRegNewState(ctxt); |
1594 | 0 | if (state == NULL) |
1595 | 0 | return(NULL); |
1596 | | |
1597 | 0 | state->no = ctxt->nbStates; |
1598 | 0 | ctxt->states[ctxt->nbStates++] = state; |
1599 | |
|
1600 | 0 | return(state); |
1601 | 0 | } |
1602 | | |
1603 | | /** |
1604 | | * @param ctxt a regexp parser context |
1605 | | * @param from the from state |
1606 | | * @param to the target state or NULL for building a new one |
1607 | | * @param lax |
1608 | | */ |
1609 | | static int |
1610 | | xmlFAGenerateAllTransition(xmlRegParserCtxtPtr ctxt, |
1611 | | xmlRegStatePtr from, xmlRegStatePtr to, |
1612 | 0 | int lax) { |
1613 | 0 | if (to == NULL) { |
1614 | 0 | to = xmlRegStatePush(ctxt); |
1615 | 0 | if (to == NULL) |
1616 | 0 | return(-1); |
1617 | 0 | ctxt->state = to; |
1618 | 0 | } |
1619 | 0 | if (lax) |
1620 | 0 | xmlRegStateAddTrans(ctxt, from, NULL, to, -1, REGEXP_ALL_LAX_COUNTER); |
1621 | 0 | else |
1622 | 0 | xmlRegStateAddTrans(ctxt, from, NULL, to, -1, REGEXP_ALL_COUNTER); |
1623 | 0 | return(0); |
1624 | 0 | } |
1625 | | |
1626 | | /** |
1627 | | * @param ctxt a regexp parser context |
1628 | | * @param from the from state |
1629 | | * @param to the target state or NULL for building a new one |
1630 | | */ |
1631 | | static int |
1632 | | xmlFAGenerateEpsilonTransition(xmlRegParserCtxtPtr ctxt, |
1633 | 0 | xmlRegStatePtr from, xmlRegStatePtr to) { |
1634 | 0 | if (to == NULL) { |
1635 | 0 | to = xmlRegStatePush(ctxt); |
1636 | 0 | if (to == NULL) |
1637 | 0 | return(-1); |
1638 | 0 | ctxt->state = to; |
1639 | 0 | } |
1640 | 0 | xmlRegStateAddTrans(ctxt, from, NULL, to, -1, -1); |
1641 | 0 | return(0); |
1642 | 0 | } |
1643 | | |
1644 | | /** |
1645 | | * @param ctxt a regexp parser context |
1646 | | * @param from the from state |
1647 | | * @param to the target state or NULL for building a new one |
1648 | | * @param counter the counter for that transition |
1649 | | */ |
1650 | | static int |
1651 | | xmlFAGenerateCountedEpsilonTransition(xmlRegParserCtxtPtr ctxt, |
1652 | 0 | xmlRegStatePtr from, xmlRegStatePtr to, int counter) { |
1653 | 0 | if (to == NULL) { |
1654 | 0 | to = xmlRegStatePush(ctxt); |
1655 | 0 | if (to == NULL) |
1656 | 0 | return(-1); |
1657 | 0 | ctxt->state = to; |
1658 | 0 | } |
1659 | 0 | xmlRegStateAddTrans(ctxt, from, NULL, to, counter, -1); |
1660 | 0 | return(0); |
1661 | 0 | } |
1662 | | |
1663 | | /** |
1664 | | * @param ctxt a regexp parser context |
1665 | | * @param from the from state |
1666 | | * @param to the target state or NULL for building a new one |
1667 | | * @param counter the counter for that transition |
1668 | | */ |
1669 | | static int |
1670 | | xmlFAGenerateCountedTransition(xmlRegParserCtxtPtr ctxt, |
1671 | 0 | xmlRegStatePtr from, xmlRegStatePtr to, int counter) { |
1672 | 0 | if (to == NULL) { |
1673 | 0 | to = xmlRegStatePush(ctxt); |
1674 | 0 | if (to == NULL) |
1675 | 0 | return(-1); |
1676 | 0 | ctxt->state = to; |
1677 | 0 | } |
1678 | 0 | xmlRegStateAddTrans(ctxt, from, NULL, to, -1, counter); |
1679 | 0 | return(0); |
1680 | 0 | } |
1681 | | |
1682 | | /** |
1683 | | * @param ctxt a regexp parser context |
1684 | | * @param from the from state |
1685 | | * @param to the target state or NULL for building a new one |
1686 | | * @param atom the atom generating the transition |
1687 | | * @returns 0 if success and -1 in case of error. |
1688 | | */ |
1689 | | static int |
1690 | | xmlFAGenerateTransitions(xmlRegParserCtxtPtr ctxt, xmlRegStatePtr from, |
1691 | 0 | xmlRegStatePtr to, xmlRegAtomPtr atom) { |
1692 | 0 | xmlRegStatePtr end; |
1693 | 0 | int nullable = 0; |
1694 | |
|
1695 | 0 | if (atom == NULL) { |
1696 | 0 | ERROR("generate transition: atom == NULL"); |
1697 | 0 | return(-1); |
1698 | 0 | } |
1699 | 0 | if (atom->type == XML_REGEXP_SUBREG) { |
1700 | | /* |
1701 | | * this is a subexpression handling one should not need to |
1702 | | * create a new node except for XML_REGEXP_QUANT_RANGE. |
1703 | | */ |
1704 | 0 | if ((to != NULL) && (atom->stop != to) && |
1705 | 0 | (atom->quant != XML_REGEXP_QUANT_RANGE)) { |
1706 | | /* |
1707 | | * Generate an epsilon transition to link to the target |
1708 | | */ |
1709 | 0 | xmlFAGenerateEpsilonTransition(ctxt, atom->stop, to); |
1710 | | #ifdef DV |
1711 | | } else if ((to == NULL) && (atom->quant != XML_REGEXP_QUANT_RANGE) && |
1712 | | (atom->quant != XML_REGEXP_QUANT_ONCE)) { |
1713 | | to = xmlRegStatePush(ctxt, to); |
1714 | | if (to == NULL) |
1715 | | return(-1); |
1716 | | ctxt->state = to; |
1717 | | xmlFAGenerateEpsilonTransition(ctxt, atom->stop, to); |
1718 | | #endif |
1719 | 0 | } |
1720 | 0 | switch (atom->quant) { |
1721 | 0 | case XML_REGEXP_QUANT_OPT: |
1722 | 0 | atom->quant = XML_REGEXP_QUANT_ONCE; |
1723 | | /* |
1724 | | * transition done to the state after end of atom. |
1725 | | * 1. set transition from atom start to new state |
1726 | | * 2. set transition from atom end to this state. |
1727 | | */ |
1728 | 0 | if (to == NULL) { |
1729 | 0 | xmlFAGenerateEpsilonTransition(ctxt, atom->start, 0); |
1730 | 0 | xmlFAGenerateEpsilonTransition(ctxt, atom->stop, |
1731 | 0 | ctxt->state); |
1732 | 0 | } else { |
1733 | 0 | xmlFAGenerateEpsilonTransition(ctxt, atom->start, to); |
1734 | 0 | } |
1735 | 0 | break; |
1736 | 0 | case XML_REGEXP_QUANT_MULT: |
1737 | 0 | atom->quant = XML_REGEXP_QUANT_ONCE; |
1738 | 0 | xmlFAGenerateEpsilonTransition(ctxt, atom->start, atom->stop); |
1739 | 0 | xmlFAGenerateEpsilonTransition(ctxt, atom->stop, atom->start); |
1740 | 0 | break; |
1741 | 0 | case XML_REGEXP_QUANT_PLUS: |
1742 | 0 | atom->quant = XML_REGEXP_QUANT_ONCE; |
1743 | 0 | xmlFAGenerateEpsilonTransition(ctxt, atom->stop, atom->start); |
1744 | 0 | break; |
1745 | 0 | case XML_REGEXP_QUANT_RANGE: { |
1746 | 0 | int counter; |
1747 | 0 | xmlRegStatePtr inter, newstate; |
1748 | | |
1749 | | /* |
1750 | | * create the final state now if needed |
1751 | | */ |
1752 | 0 | if (to != NULL) { |
1753 | 0 | newstate = to; |
1754 | 0 | } else { |
1755 | 0 | newstate = xmlRegStatePush(ctxt); |
1756 | 0 | if (newstate == NULL) |
1757 | 0 | return(-1); |
1758 | 0 | } |
1759 | | |
1760 | | /* |
1761 | | * The principle here is to use counted transition |
1762 | | * to avoid explosion in the number of states in the |
1763 | | * graph. This is clearly more complex but should not |
1764 | | * be exploitable at runtime. |
1765 | | */ |
1766 | 0 | if ((atom->min == 0) && (atom->start0 == NULL)) { |
1767 | 0 | xmlRegAtomPtr copy; |
1768 | | /* |
1769 | | * duplicate a transition based on atom to count next |
1770 | | * occurrences after 1. We cannot loop to atom->start |
1771 | | * directly because we need an epsilon transition to |
1772 | | * newstate. |
1773 | | */ |
1774 | | /* ???? For some reason it seems we never reach that |
1775 | | case, I suppose this got optimized out before when |
1776 | | building the automata */ |
1777 | 0 | copy = xmlRegCopyAtom(ctxt, atom); |
1778 | 0 | if (copy == NULL) |
1779 | 0 | return(-1); |
1780 | 0 | copy->quant = XML_REGEXP_QUANT_ONCE; |
1781 | 0 | copy->min = 0; |
1782 | 0 | copy->max = 0; |
1783 | |
|
1784 | 0 | if (xmlFAGenerateTransitions(ctxt, atom->start, NULL, copy) |
1785 | 0 | < 0) { |
1786 | 0 | xmlRegFreeAtom(copy); |
1787 | 0 | return(-1); |
1788 | 0 | } |
1789 | 0 | inter = ctxt->state; |
1790 | 0 | counter = xmlRegGetCounter(ctxt); |
1791 | 0 | if (counter < 0) |
1792 | 0 | return(-1); |
1793 | 0 | ctxt->counters[counter].min = atom->min - 1; |
1794 | 0 | ctxt->counters[counter].max = atom->max - 1; |
1795 | | /* count the number of times we see it again */ |
1796 | 0 | xmlFAGenerateCountedEpsilonTransition(ctxt, inter, |
1797 | 0 | atom->stop, counter); |
1798 | | /* allow a way out based on the count */ |
1799 | 0 | xmlFAGenerateCountedTransition(ctxt, inter, |
1800 | 0 | newstate, counter); |
1801 | | /* and also allow a direct exit for 0 */ |
1802 | 0 | xmlFAGenerateEpsilonTransition(ctxt, atom->start, |
1803 | 0 | newstate); |
1804 | 0 | } else { |
1805 | | /* |
1806 | | * either we need the atom at least once or there |
1807 | | * is an atom->start0 allowing to easily plug the |
1808 | | * epsilon transition. |
1809 | | */ |
1810 | 0 | counter = xmlRegGetCounter(ctxt); |
1811 | 0 | if (counter < 0) |
1812 | 0 | return(-1); |
1813 | 0 | ctxt->counters[counter].min = atom->min - 1; |
1814 | 0 | ctxt->counters[counter].max = atom->max - 1; |
1815 | | /* allow a way out based on the count */ |
1816 | 0 | xmlFAGenerateCountedTransition(ctxt, atom->stop, |
1817 | 0 | newstate, counter); |
1818 | | /* count the number of times we see it again */ |
1819 | 0 | xmlFAGenerateCountedEpsilonTransition(ctxt, atom->stop, |
1820 | 0 | atom->start, counter); |
1821 | | /* and if needed allow a direct exit for 0 */ |
1822 | 0 | if (atom->min == 0) |
1823 | 0 | xmlFAGenerateEpsilonTransition(ctxt, atom->start0, |
1824 | 0 | newstate); |
1825 | |
|
1826 | 0 | } |
1827 | 0 | atom->min = 0; |
1828 | 0 | atom->max = 0; |
1829 | 0 | atom->quant = XML_REGEXP_QUANT_ONCE; |
1830 | 0 | ctxt->state = newstate; |
1831 | 0 | } |
1832 | 0 | default: |
1833 | 0 | break; |
1834 | 0 | } |
1835 | 0 | atom->start = NULL; |
1836 | 0 | atom->start0 = NULL; |
1837 | 0 | atom->stop = NULL; |
1838 | 0 | if (xmlRegAtomPush(ctxt, atom) < 0) |
1839 | 0 | return(-1); |
1840 | 0 | return(0); |
1841 | 0 | } |
1842 | 0 | if ((atom->min == 0) && (atom->max == 0) && |
1843 | 0 | (atom->quant == XML_REGEXP_QUANT_RANGE)) { |
1844 | | /* |
1845 | | * we can discard the atom and generate an epsilon transition instead |
1846 | | */ |
1847 | 0 | if (to == NULL) { |
1848 | 0 | to = xmlRegStatePush(ctxt); |
1849 | 0 | if (to == NULL) |
1850 | 0 | return(-1); |
1851 | 0 | } |
1852 | 0 | xmlFAGenerateEpsilonTransition(ctxt, from, to); |
1853 | 0 | ctxt->state = to; |
1854 | 0 | xmlRegFreeAtom(atom); |
1855 | 0 | return(0); |
1856 | 0 | } |
1857 | 0 | if (to == NULL) { |
1858 | 0 | to = xmlRegStatePush(ctxt); |
1859 | 0 | if (to == NULL) |
1860 | 0 | return(-1); |
1861 | 0 | } |
1862 | 0 | end = to; |
1863 | 0 | if ((atom->quant == XML_REGEXP_QUANT_MULT) || |
1864 | 0 | (atom->quant == XML_REGEXP_QUANT_PLUS)) { |
1865 | | /* |
1866 | | * Do not pollute the target state by adding transitions from |
1867 | | * it as it is likely to be the shared target of multiple branches. |
1868 | | * So isolate with an epsilon transition. |
1869 | | */ |
1870 | 0 | xmlRegStatePtr tmp; |
1871 | |
|
1872 | 0 | tmp = xmlRegStatePush(ctxt); |
1873 | 0 | if (tmp == NULL) |
1874 | 0 | return(-1); |
1875 | 0 | xmlFAGenerateEpsilonTransition(ctxt, tmp, to); |
1876 | 0 | to = tmp; |
1877 | 0 | } |
1878 | 0 | if ((atom->quant == XML_REGEXP_QUANT_RANGE) && |
1879 | 0 | (atom->min == 0) && (atom->max > 0)) { |
1880 | 0 | nullable = 1; |
1881 | 0 | atom->min = 1; |
1882 | 0 | if (atom->max == 1) |
1883 | 0 | atom->quant = XML_REGEXP_QUANT_OPT; |
1884 | 0 | } |
1885 | 0 | xmlRegStateAddTrans(ctxt, from, atom, to, -1, -1); |
1886 | 0 | ctxt->state = end; |
1887 | 0 | switch (atom->quant) { |
1888 | 0 | case XML_REGEXP_QUANT_OPT: |
1889 | 0 | atom->quant = XML_REGEXP_QUANT_ONCE; |
1890 | 0 | xmlFAGenerateEpsilonTransition(ctxt, from, to); |
1891 | 0 | break; |
1892 | 0 | case XML_REGEXP_QUANT_MULT: |
1893 | 0 | atom->quant = XML_REGEXP_QUANT_ONCE; |
1894 | 0 | xmlFAGenerateEpsilonTransition(ctxt, from, to); |
1895 | 0 | xmlRegStateAddTrans(ctxt, to, atom, to, -1, -1); |
1896 | 0 | break; |
1897 | 0 | case XML_REGEXP_QUANT_PLUS: |
1898 | 0 | atom->quant = XML_REGEXP_QUANT_ONCE; |
1899 | 0 | xmlRegStateAddTrans(ctxt, to, atom, to, -1, -1); |
1900 | 0 | break; |
1901 | 0 | case XML_REGEXP_QUANT_RANGE: |
1902 | 0 | if (nullable) |
1903 | 0 | xmlFAGenerateEpsilonTransition(ctxt, from, to); |
1904 | 0 | break; |
1905 | 0 | default: |
1906 | 0 | break; |
1907 | 0 | } |
1908 | 0 | if (xmlRegAtomPush(ctxt, atom) < 0) |
1909 | 0 | return(-1); |
1910 | 0 | return(0); |
1911 | 0 | } |
1912 | | |
1913 | | /** |
1914 | | * @param ctxt a regexp parser context |
1915 | | * @param fromnr the from state |
1916 | | * @param tonr the to state |
1917 | | * @param counter should that transition be associated to a counted |
1918 | | */ |
1919 | | static void |
1920 | | xmlFAReduceEpsilonTransitions(xmlRegParserCtxtPtr ctxt, int fromnr, |
1921 | 0 | int tonr, int counter) { |
1922 | 0 | int transnr; |
1923 | 0 | xmlRegStatePtr from; |
1924 | 0 | xmlRegStatePtr to; |
1925 | |
|
1926 | 0 | from = ctxt->states[fromnr]; |
1927 | 0 | if (from == NULL) |
1928 | 0 | return; |
1929 | 0 | to = ctxt->states[tonr]; |
1930 | 0 | if (to == NULL) |
1931 | 0 | return; |
1932 | 0 | if ((to->mark == XML_REGEXP_MARK_START) || |
1933 | 0 | (to->mark == XML_REGEXP_MARK_VISITED)) |
1934 | 0 | return; |
1935 | | |
1936 | 0 | to->mark = XML_REGEXP_MARK_VISITED; |
1937 | 0 | if (to->type == XML_REGEXP_FINAL_STATE) { |
1938 | 0 | from->type = XML_REGEXP_FINAL_STATE; |
1939 | 0 | } |
1940 | 0 | for (transnr = 0;transnr < to->nbTrans;transnr++) { |
1941 | 0 | xmlRegTransPtr t1 = &to->trans[transnr]; |
1942 | 0 | int tcounter; |
1943 | |
|
1944 | 0 | if (t1->to < 0) |
1945 | 0 | continue; |
1946 | 0 | if (t1->counter >= 0) { |
1947 | | /* assert(counter < 0); */ |
1948 | 0 | tcounter = t1->counter; |
1949 | 0 | } else { |
1950 | 0 | tcounter = counter; |
1951 | 0 | } |
1952 | 0 | if (t1->atom == NULL) { |
1953 | | /* |
1954 | | * Don't remove counted transitions |
1955 | | * Don't loop either |
1956 | | */ |
1957 | 0 | if (t1->to != fromnr) { |
1958 | 0 | if (t1->count >= 0) { |
1959 | 0 | xmlRegStateAddTrans(ctxt, from, NULL, ctxt->states[t1->to], |
1960 | 0 | -1, t1->count); |
1961 | 0 | } else { |
1962 | 0 | xmlFAReduceEpsilonTransitions(ctxt, fromnr, t1->to, |
1963 | 0 | tcounter); |
1964 | 0 | } |
1965 | 0 | } |
1966 | 0 | } else { |
1967 | 0 | xmlRegStateAddTrans(ctxt, from, t1->atom, |
1968 | 0 | ctxt->states[t1->to], tcounter, -1); |
1969 | 0 | } |
1970 | 0 | } |
1971 | 0 | } |
1972 | | |
1973 | | /** |
1974 | | * @param ctxt a regexp parser context |
1975 | | * @param tonr the to state |
1976 | | */ |
1977 | | static void |
1978 | 0 | xmlFAFinishReduceEpsilonTransitions(xmlRegParserCtxtPtr ctxt, int tonr) { |
1979 | 0 | int transnr; |
1980 | 0 | xmlRegStatePtr to; |
1981 | |
|
1982 | 0 | to = ctxt->states[tonr]; |
1983 | 0 | if (to == NULL) |
1984 | 0 | return; |
1985 | 0 | if ((to->mark == XML_REGEXP_MARK_START) || |
1986 | 0 | (to->mark == XML_REGEXP_MARK_NORMAL)) |
1987 | 0 | return; |
1988 | | |
1989 | 0 | to->mark = XML_REGEXP_MARK_NORMAL; |
1990 | 0 | for (transnr = 0;transnr < to->nbTrans;transnr++) { |
1991 | 0 | xmlRegTransPtr t1 = &to->trans[transnr]; |
1992 | 0 | if ((t1->to >= 0) && (t1->atom == NULL)) |
1993 | 0 | xmlFAFinishReduceEpsilonTransitions(ctxt, t1->to); |
1994 | 0 | } |
1995 | 0 | } |
1996 | | |
1997 | | /** |
1998 | | * Eliminating general epsilon transitions can get costly in the general |
1999 | | * algorithm due to the large amount of generated new transitions and |
2000 | | * associated comparisons. However for simple epsilon transition used just |
2001 | | * to separate building blocks when generating the automata this can be |
2002 | | * reduced to state elimination: |
2003 | | * - if there exists an epsilon from X to Y |
2004 | | * - if there is no other transition from X |
2005 | | * then X and Y are semantically equivalent and X can be eliminated |
2006 | | * If X is the start state then make Y the start state, else replace the |
2007 | | * target of all transitions to X by transitions to Y. |
2008 | | * |
2009 | | * If X is a final state, skip it. |
2010 | | * Otherwise it would be necessary to manipulate counters for this case when |
2011 | | * eliminating state 2: |
2012 | | * State 1 has a transition with an atom to state 2. |
2013 | | * State 2 is final and has an epsilon transition to state 1. |
2014 | | * |
2015 | | * @param ctxt a regexp parser context |
2016 | | */ |
2017 | | static void |
2018 | 0 | xmlFAEliminateSimpleEpsilonTransitions(xmlRegParserCtxtPtr ctxt) { |
2019 | 0 | int statenr, i, j, newto; |
2020 | 0 | xmlRegStatePtr state, tmp; |
2021 | |
|
2022 | 0 | for (statenr = 0;statenr < ctxt->nbStates;statenr++) { |
2023 | 0 | state = ctxt->states[statenr]; |
2024 | 0 | if (state == NULL) |
2025 | 0 | continue; |
2026 | 0 | if (state->nbTrans != 1) |
2027 | 0 | continue; |
2028 | 0 | if (state->type == XML_REGEXP_UNREACH_STATE || |
2029 | 0 | state->type == XML_REGEXP_FINAL_STATE) |
2030 | 0 | continue; |
2031 | | /* is the only transition out a basic transition */ |
2032 | 0 | if ((state->trans[0].atom == NULL) && |
2033 | 0 | (state->trans[0].to >= 0) && |
2034 | 0 | (state->trans[0].to != statenr) && |
2035 | 0 | (state->trans[0].counter < 0) && |
2036 | 0 | (state->trans[0].count < 0)) { |
2037 | 0 | newto = state->trans[0].to; |
2038 | |
|
2039 | 0 | if (state->type == XML_REGEXP_START_STATE) { |
2040 | 0 | } else { |
2041 | 0 | for (i = 0;i < state->nbTransTo;i++) { |
2042 | 0 | tmp = ctxt->states[state->transTo[i]]; |
2043 | 0 | for (j = 0;j < tmp->nbTrans;j++) { |
2044 | 0 | if (tmp->trans[j].to == statenr) { |
2045 | 0 | tmp->trans[j].to = -1; |
2046 | 0 | xmlRegStateAddTrans(ctxt, tmp, tmp->trans[j].atom, |
2047 | 0 | ctxt->states[newto], |
2048 | 0 | tmp->trans[j].counter, |
2049 | 0 | tmp->trans[j].count); |
2050 | 0 | } |
2051 | 0 | } |
2052 | 0 | } |
2053 | 0 | if (state->type == XML_REGEXP_FINAL_STATE) |
2054 | 0 | ctxt->states[newto]->type = XML_REGEXP_FINAL_STATE; |
2055 | | /* eliminate the transition completely */ |
2056 | 0 | state->nbTrans = 0; |
2057 | |
|
2058 | 0 | state->type = XML_REGEXP_UNREACH_STATE; |
2059 | |
|
2060 | 0 | } |
2061 | |
|
2062 | 0 | } |
2063 | 0 | } |
2064 | 0 | } |
2065 | | /** |
2066 | | * @param ctxt a regexp parser context |
2067 | | */ |
2068 | | static void |
2069 | 0 | xmlFAEliminateEpsilonTransitions(xmlRegParserCtxtPtr ctxt) { |
2070 | 0 | int statenr, transnr; |
2071 | 0 | xmlRegStatePtr state; |
2072 | 0 | int has_epsilon; |
2073 | |
|
2074 | 0 | if (ctxt->states == NULL) return; |
2075 | | |
2076 | | /* |
2077 | | * Eliminate simple epsilon transition and the associated unreachable |
2078 | | * states. |
2079 | | */ |
2080 | 0 | xmlFAEliminateSimpleEpsilonTransitions(ctxt); |
2081 | 0 | for (statenr = 0;statenr < ctxt->nbStates;statenr++) { |
2082 | 0 | state = ctxt->states[statenr]; |
2083 | 0 | if ((state != NULL) && (state->type == XML_REGEXP_UNREACH_STATE)) { |
2084 | 0 | xmlRegFreeState(state); |
2085 | 0 | ctxt->states[statenr] = NULL; |
2086 | 0 | } |
2087 | 0 | } |
2088 | |
|
2089 | 0 | has_epsilon = 0; |
2090 | | |
2091 | | /* |
2092 | | * Build the completed transitions bypassing the epsilons |
2093 | | * Use a marking algorithm to avoid loops |
2094 | | * Mark sink states too. |
2095 | | * Process from the latest states backward to the start when |
2096 | | * there is long cascading epsilon chains this minimize the |
2097 | | * recursions and transition compares when adding the new ones |
2098 | | */ |
2099 | 0 | for (statenr = ctxt->nbStates - 1;statenr >= 0;statenr--) { |
2100 | 0 | state = ctxt->states[statenr]; |
2101 | 0 | if (state == NULL) |
2102 | 0 | continue; |
2103 | 0 | if ((state->nbTrans == 0) && |
2104 | 0 | (state->type != XML_REGEXP_FINAL_STATE)) { |
2105 | 0 | state->type = XML_REGEXP_SINK_STATE; |
2106 | 0 | } |
2107 | 0 | for (transnr = 0;transnr < state->nbTrans;transnr++) { |
2108 | 0 | if ((state->trans[transnr].atom == NULL) && |
2109 | 0 | (state->trans[transnr].to >= 0)) { |
2110 | 0 | if (state->trans[transnr].to == statenr) { |
2111 | 0 | state->trans[transnr].to = -1; |
2112 | 0 | } else if (state->trans[transnr].count < 0) { |
2113 | 0 | int newto = state->trans[transnr].to; |
2114 | |
|
2115 | 0 | has_epsilon = 1; |
2116 | 0 | state->trans[transnr].to = -2; |
2117 | 0 | state->mark = XML_REGEXP_MARK_START; |
2118 | 0 | xmlFAReduceEpsilonTransitions(ctxt, statenr, |
2119 | 0 | newto, state->trans[transnr].counter); |
2120 | 0 | xmlFAFinishReduceEpsilonTransitions(ctxt, newto); |
2121 | 0 | state->mark = XML_REGEXP_MARK_NORMAL; |
2122 | 0 | } |
2123 | 0 | } |
2124 | 0 | } |
2125 | 0 | } |
2126 | | /* |
2127 | | * Eliminate the epsilon transitions |
2128 | | */ |
2129 | 0 | if (has_epsilon) { |
2130 | 0 | for (statenr = 0;statenr < ctxt->nbStates;statenr++) { |
2131 | 0 | state = ctxt->states[statenr]; |
2132 | 0 | if (state == NULL) |
2133 | 0 | continue; |
2134 | 0 | for (transnr = 0;transnr < state->nbTrans;transnr++) { |
2135 | 0 | xmlRegTransPtr trans = &(state->trans[transnr]); |
2136 | 0 | if ((trans->atom == NULL) && |
2137 | 0 | (trans->count < 0) && |
2138 | 0 | (trans->to >= 0)) { |
2139 | 0 | trans->to = -1; |
2140 | 0 | } |
2141 | 0 | } |
2142 | 0 | } |
2143 | 0 | } |
2144 | | |
2145 | | /* |
2146 | | * Use this pass to detect unreachable states too |
2147 | | */ |
2148 | 0 | for (statenr = 0;statenr < ctxt->nbStates;statenr++) { |
2149 | 0 | state = ctxt->states[statenr]; |
2150 | 0 | if (state != NULL) |
2151 | 0 | state->reached = XML_REGEXP_MARK_NORMAL; |
2152 | 0 | } |
2153 | 0 | state = ctxt->states[0]; |
2154 | 0 | if (state != NULL) |
2155 | 0 | state->reached = XML_REGEXP_MARK_START; |
2156 | 0 | while (state != NULL) { |
2157 | 0 | xmlRegStatePtr target = NULL; |
2158 | 0 | state->reached = XML_REGEXP_MARK_VISITED; |
2159 | | /* |
2160 | | * Mark all states reachable from the current reachable state |
2161 | | */ |
2162 | 0 | for (transnr = 0;transnr < state->nbTrans;transnr++) { |
2163 | 0 | if ((state->trans[transnr].to >= 0) && |
2164 | 0 | ((state->trans[transnr].atom != NULL) || |
2165 | 0 | (state->trans[transnr].count >= 0))) { |
2166 | 0 | int newto = state->trans[transnr].to; |
2167 | |
|
2168 | 0 | if (ctxt->states[newto] == NULL) |
2169 | 0 | continue; |
2170 | 0 | if (ctxt->states[newto]->reached == XML_REGEXP_MARK_NORMAL) { |
2171 | 0 | ctxt->states[newto]->reached = XML_REGEXP_MARK_START; |
2172 | 0 | target = ctxt->states[newto]; |
2173 | 0 | } |
2174 | 0 | } |
2175 | 0 | } |
2176 | | |
2177 | | /* |
2178 | | * find the next accessible state not explored |
2179 | | */ |
2180 | 0 | if (target == NULL) { |
2181 | 0 | for (statenr = 1;statenr < ctxt->nbStates;statenr++) { |
2182 | 0 | state = ctxt->states[statenr]; |
2183 | 0 | if ((state != NULL) && (state->reached == |
2184 | 0 | XML_REGEXP_MARK_START)) { |
2185 | 0 | target = state; |
2186 | 0 | break; |
2187 | 0 | } |
2188 | 0 | } |
2189 | 0 | } |
2190 | 0 | state = target; |
2191 | 0 | } |
2192 | 0 | for (statenr = 0;statenr < ctxt->nbStates;statenr++) { |
2193 | 0 | state = ctxt->states[statenr]; |
2194 | 0 | if ((state != NULL) && (state->reached == XML_REGEXP_MARK_NORMAL)) { |
2195 | 0 | xmlRegFreeState(state); |
2196 | 0 | ctxt->states[statenr] = NULL; |
2197 | 0 | } |
2198 | 0 | } |
2199 | |
|
2200 | 0 | } |
2201 | | |
2202 | | static int |
2203 | 0 | xmlFACompareRanges(xmlRegRangePtr range1, xmlRegRangePtr range2) { |
2204 | 0 | int ret = 0; |
2205 | |
|
2206 | 0 | if ((range1->type == XML_REGEXP_RANGES) || |
2207 | 0 | (range2->type == XML_REGEXP_RANGES) || |
2208 | 0 | (range2->type == XML_REGEXP_SUBREG) || |
2209 | 0 | (range1->type == XML_REGEXP_SUBREG) || |
2210 | 0 | (range1->type == XML_REGEXP_STRING) || |
2211 | 0 | (range2->type == XML_REGEXP_STRING)) |
2212 | 0 | return(-1); |
2213 | | |
2214 | | /* put them in order */ |
2215 | 0 | if (range1->type > range2->type) { |
2216 | 0 | xmlRegRangePtr tmp; |
2217 | |
|
2218 | 0 | tmp = range1; |
2219 | 0 | range1 = range2; |
2220 | 0 | range2 = tmp; |
2221 | 0 | } |
2222 | 0 | if ((range1->type == XML_REGEXP_ANYCHAR) || |
2223 | 0 | (range2->type == XML_REGEXP_ANYCHAR)) { |
2224 | 0 | ret = 1; |
2225 | 0 | } else if ((range1->type == XML_REGEXP_EPSILON) || |
2226 | 0 | (range2->type == XML_REGEXP_EPSILON)) { |
2227 | 0 | return(0); |
2228 | 0 | } else if (range1->type == range2->type) { |
2229 | 0 | if (range1->type != XML_REGEXP_CHARVAL) |
2230 | 0 | ret = 1; |
2231 | 0 | else if ((range1->end < range2->start) || |
2232 | 0 | (range2->end < range1->start)) |
2233 | 0 | ret = 0; |
2234 | 0 | else |
2235 | 0 | ret = 1; |
2236 | 0 | } else if (range1->type == XML_REGEXP_CHARVAL) { |
2237 | 0 | int codepoint; |
2238 | 0 | int neg = 0; |
2239 | | |
2240 | | /* |
2241 | | * just check all codepoints in the range for acceptance, |
2242 | | * this is usually way cheaper since done only once at |
2243 | | * compilation than testing over and over at runtime or |
2244 | | * pushing too many states when evaluating. |
2245 | | */ |
2246 | 0 | if (((range1->neg == 0) && (range2->neg != 0)) || |
2247 | 0 | ((range1->neg != 0) && (range2->neg == 0))) |
2248 | 0 | neg = 1; |
2249 | |
|
2250 | 0 | for (codepoint = range1->start;codepoint <= range1->end ;codepoint++) { |
2251 | 0 | ret = xmlRegCheckCharacterRange(range2->type, codepoint, |
2252 | 0 | 0, range2->start, range2->end, |
2253 | 0 | range2->blockName); |
2254 | 0 | if (ret < 0) |
2255 | 0 | return(-1); |
2256 | 0 | if (((neg == 1) && (ret == 0)) || |
2257 | 0 | ((neg == 0) && (ret == 1))) |
2258 | 0 | return(1); |
2259 | 0 | } |
2260 | 0 | return(0); |
2261 | 0 | } else if ((range1->type == XML_REGEXP_BLOCK_NAME) || |
2262 | 0 | (range2->type == XML_REGEXP_BLOCK_NAME)) { |
2263 | 0 | if (range1->type == range2->type) { |
2264 | 0 | ret = xmlStrEqual(range1->blockName, range2->blockName); |
2265 | 0 | } else { |
2266 | | /* |
2267 | | * comparing a block range with anything else is way |
2268 | | * too costly, and maintaining the table is like too much |
2269 | | * memory too, so let's force the automata to save state |
2270 | | * here. |
2271 | | */ |
2272 | 0 | return(1); |
2273 | 0 | } |
2274 | 0 | } else if ((range1->type < XML_REGEXP_LETTER) || |
2275 | 0 | (range2->type < XML_REGEXP_LETTER)) { |
2276 | 0 | if ((range1->type == XML_REGEXP_ANYSPACE) && |
2277 | 0 | (range2->type == XML_REGEXP_NOTSPACE)) |
2278 | 0 | ret = 0; |
2279 | 0 | else if ((range1->type == XML_REGEXP_INITNAME) && |
2280 | 0 | (range2->type == XML_REGEXP_NOTINITNAME)) |
2281 | 0 | ret = 0; |
2282 | 0 | else if ((range1->type == XML_REGEXP_NAMECHAR) && |
2283 | 0 | (range2->type == XML_REGEXP_NOTNAMECHAR)) |
2284 | 0 | ret = 0; |
2285 | 0 | else if ((range1->type == XML_REGEXP_DECIMAL) && |
2286 | 0 | (range2->type == XML_REGEXP_NOTDECIMAL)) |
2287 | 0 | ret = 0; |
2288 | 0 | else if ((range1->type == XML_REGEXP_REALCHAR) && |
2289 | 0 | (range2->type == XML_REGEXP_NOTREALCHAR)) |
2290 | 0 | ret = 0; |
2291 | 0 | else { |
2292 | | /* same thing to limit complexity */ |
2293 | 0 | return(1); |
2294 | 0 | } |
2295 | 0 | } else { |
2296 | 0 | ret = 0; |
2297 | | /* range1->type < range2->type here */ |
2298 | 0 | switch (range1->type) { |
2299 | 0 | case XML_REGEXP_LETTER: |
2300 | | /* all disjoint except in the subgroups */ |
2301 | 0 | if ((range2->type == XML_REGEXP_LETTER_UPPERCASE) || |
2302 | 0 | (range2->type == XML_REGEXP_LETTER_LOWERCASE) || |
2303 | 0 | (range2->type == XML_REGEXP_LETTER_TITLECASE) || |
2304 | 0 | (range2->type == XML_REGEXP_LETTER_MODIFIER) || |
2305 | 0 | (range2->type == XML_REGEXP_LETTER_OTHERS)) |
2306 | 0 | ret = 1; |
2307 | 0 | break; |
2308 | 0 | case XML_REGEXP_MARK: |
2309 | 0 | if ((range2->type == XML_REGEXP_MARK_NONSPACING) || |
2310 | 0 | (range2->type == XML_REGEXP_MARK_SPACECOMBINING) || |
2311 | 0 | (range2->type == XML_REGEXP_MARK_ENCLOSING)) |
2312 | 0 | ret = 1; |
2313 | 0 | break; |
2314 | 0 | case XML_REGEXP_NUMBER: |
2315 | 0 | if ((range2->type == XML_REGEXP_NUMBER_DECIMAL) || |
2316 | 0 | (range2->type == XML_REGEXP_NUMBER_LETTER) || |
2317 | 0 | (range2->type == XML_REGEXP_NUMBER_OTHERS)) |
2318 | 0 | ret = 1; |
2319 | 0 | break; |
2320 | 0 | case XML_REGEXP_PUNCT: |
2321 | 0 | if ((range2->type == XML_REGEXP_PUNCT_CONNECTOR) || |
2322 | 0 | (range2->type == XML_REGEXP_PUNCT_DASH) || |
2323 | 0 | (range2->type == XML_REGEXP_PUNCT_OPEN) || |
2324 | 0 | (range2->type == XML_REGEXP_PUNCT_CLOSE) || |
2325 | 0 | (range2->type == XML_REGEXP_PUNCT_INITQUOTE) || |
2326 | 0 | (range2->type == XML_REGEXP_PUNCT_FINQUOTE) || |
2327 | 0 | (range2->type == XML_REGEXP_PUNCT_OTHERS)) |
2328 | 0 | ret = 1; |
2329 | 0 | break; |
2330 | 0 | case XML_REGEXP_SEPAR: |
2331 | 0 | if ((range2->type == XML_REGEXP_SEPAR_SPACE) || |
2332 | 0 | (range2->type == XML_REGEXP_SEPAR_LINE) || |
2333 | 0 | (range2->type == XML_REGEXP_SEPAR_PARA)) |
2334 | 0 | ret = 1; |
2335 | 0 | break; |
2336 | 0 | case XML_REGEXP_SYMBOL: |
2337 | 0 | if ((range2->type == XML_REGEXP_SYMBOL_MATH) || |
2338 | 0 | (range2->type == XML_REGEXP_SYMBOL_CURRENCY) || |
2339 | 0 | (range2->type == XML_REGEXP_SYMBOL_MODIFIER) || |
2340 | 0 | (range2->type == XML_REGEXP_SYMBOL_OTHERS)) |
2341 | 0 | ret = 1; |
2342 | 0 | break; |
2343 | 0 | case XML_REGEXP_OTHER: |
2344 | 0 | if ((range2->type == XML_REGEXP_OTHER_CONTROL) || |
2345 | 0 | (range2->type == XML_REGEXP_OTHER_FORMAT) || |
2346 | 0 | (range2->type == XML_REGEXP_OTHER_PRIVATE)) |
2347 | 0 | ret = 1; |
2348 | 0 | break; |
2349 | 0 | default: |
2350 | 0 | if ((range2->type >= XML_REGEXP_LETTER) && |
2351 | 0 | (range2->type < XML_REGEXP_BLOCK_NAME)) |
2352 | 0 | ret = 0; |
2353 | 0 | else { |
2354 | | /* safety net ! */ |
2355 | 0 | return(1); |
2356 | 0 | } |
2357 | 0 | } |
2358 | 0 | } |
2359 | 0 | if (((range1->neg == 0) && (range2->neg != 0)) || |
2360 | 0 | ((range1->neg != 0) && (range2->neg == 0))) |
2361 | 0 | ret = !ret; |
2362 | 0 | return(ret); |
2363 | 0 | } |
2364 | | |
2365 | | /** |
2366 | | * Compares two atoms type to check whether they intersect in some ways, |
2367 | | * this is used by xmlFACompareAtoms only |
2368 | | * |
2369 | | * @param type1 an atom type |
2370 | | * @param type2 an atom type |
2371 | | * @returns 1 if they may intersect and 0 otherwise |
2372 | | */ |
2373 | | static int |
2374 | 0 | xmlFACompareAtomTypes(xmlRegAtomType type1, xmlRegAtomType type2) { |
2375 | 0 | if ((type1 == XML_REGEXP_EPSILON) || |
2376 | 0 | (type1 == XML_REGEXP_CHARVAL) || |
2377 | 0 | (type1 == XML_REGEXP_RANGES) || |
2378 | 0 | (type1 == XML_REGEXP_SUBREG) || |
2379 | 0 | (type1 == XML_REGEXP_STRING) || |
2380 | 0 | (type1 == XML_REGEXP_ANYCHAR)) |
2381 | 0 | return(1); |
2382 | 0 | if ((type2 == XML_REGEXP_EPSILON) || |
2383 | 0 | (type2 == XML_REGEXP_CHARVAL) || |
2384 | 0 | (type2 == XML_REGEXP_RANGES) || |
2385 | 0 | (type2 == XML_REGEXP_SUBREG) || |
2386 | 0 | (type2 == XML_REGEXP_STRING) || |
2387 | 0 | (type2 == XML_REGEXP_ANYCHAR)) |
2388 | 0 | return(1); |
2389 | | |
2390 | 0 | if (type1 == type2) return(1); |
2391 | | |
2392 | | /* simplify subsequent compares by making sure type1 < type2 */ |
2393 | 0 | if (type1 > type2) { |
2394 | 0 | xmlRegAtomType tmp = type1; |
2395 | 0 | type1 = type2; |
2396 | 0 | type2 = tmp; |
2397 | 0 | } |
2398 | 0 | switch (type1) { |
2399 | 0 | case XML_REGEXP_ANYSPACE: /* \s */ |
2400 | | /* can't be a letter, number, mark, punctuation, symbol */ |
2401 | 0 | if ((type2 == XML_REGEXP_NOTSPACE) || |
2402 | 0 | ((type2 >= XML_REGEXP_LETTER) && |
2403 | 0 | (type2 <= XML_REGEXP_LETTER_OTHERS)) || |
2404 | 0 | ((type2 >= XML_REGEXP_NUMBER) && |
2405 | 0 | (type2 <= XML_REGEXP_NUMBER_OTHERS)) || |
2406 | 0 | ((type2 >= XML_REGEXP_MARK) && |
2407 | 0 | (type2 <= XML_REGEXP_MARK_ENCLOSING)) || |
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 | ) return(0); |
2413 | 0 | break; |
2414 | 0 | case XML_REGEXP_NOTSPACE: /* \S */ |
2415 | 0 | break; |
2416 | 0 | case XML_REGEXP_INITNAME: /* \l */ |
2417 | | /* can't be a number, mark, separator, punctuation, symbol or other */ |
2418 | 0 | if ((type2 == XML_REGEXP_NOTINITNAME) || |
2419 | 0 | ((type2 >= XML_REGEXP_NUMBER) && |
2420 | 0 | (type2 <= XML_REGEXP_NUMBER_OTHERS)) || |
2421 | 0 | ((type2 >= XML_REGEXP_MARK) && |
2422 | 0 | (type2 <= XML_REGEXP_MARK_ENCLOSING)) || |
2423 | 0 | ((type2 >= XML_REGEXP_SEPAR) && |
2424 | 0 | (type2 <= XML_REGEXP_SEPAR_PARA)) || |
2425 | 0 | ((type2 >= XML_REGEXP_PUNCT) && |
2426 | 0 | (type2 <= XML_REGEXP_PUNCT_OTHERS)) || |
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_NOTINITNAME: /* \L */ |
2434 | 0 | break; |
2435 | 0 | case XML_REGEXP_NAMECHAR: /* \c */ |
2436 | | /* can't be a mark, separator, punctuation, symbol or other */ |
2437 | 0 | if ((type2 == XML_REGEXP_NOTNAMECHAR) || |
2438 | 0 | ((type2 >= XML_REGEXP_MARK) && |
2439 | 0 | (type2 <= XML_REGEXP_MARK_ENCLOSING)) || |
2440 | 0 | ((type2 >= XML_REGEXP_PUNCT) && |
2441 | 0 | (type2 <= XML_REGEXP_PUNCT_OTHERS)) || |
2442 | 0 | ((type2 >= XML_REGEXP_SEPAR) && |
2443 | 0 | (type2 <= XML_REGEXP_SEPAR_PARA)) || |
2444 | 0 | ((type2 >= XML_REGEXP_SYMBOL) && |
2445 | 0 | (type2 <= XML_REGEXP_SYMBOL_OTHERS)) || |
2446 | 0 | ((type2 >= XML_REGEXP_OTHER) && |
2447 | 0 | (type2 <= XML_REGEXP_OTHER_NA)) |
2448 | 0 | ) return(0); |
2449 | 0 | break; |
2450 | 0 | case XML_REGEXP_NOTNAMECHAR: /* \C */ |
2451 | 0 | break; |
2452 | 0 | case XML_REGEXP_DECIMAL: /* \d */ |
2453 | | /* can't be a letter, mark, separator, punctuation, symbol or other */ |
2454 | 0 | if ((type2 == XML_REGEXP_NOTDECIMAL) || |
2455 | 0 | (type2 == XML_REGEXP_REALCHAR) || |
2456 | 0 | ((type2 >= XML_REGEXP_LETTER) && |
2457 | 0 | (type2 <= XML_REGEXP_LETTER_OTHERS)) || |
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_NOTDECIMAL: /* \D */ |
2471 | 0 | break; |
2472 | 0 | case XML_REGEXP_REALCHAR: /* \w */ |
2473 | | /* can't be a mark, separator, punctuation, symbol or other */ |
2474 | 0 | if ((type2 == XML_REGEXP_NOTDECIMAL) || |
2475 | 0 | ((type2 >= XML_REGEXP_MARK) && |
2476 | 0 | (type2 <= XML_REGEXP_MARK_ENCLOSING)) || |
2477 | 0 | ((type2 >= XML_REGEXP_PUNCT) && |
2478 | 0 | (type2 <= XML_REGEXP_PUNCT_OTHERS)) || |
2479 | 0 | ((type2 >= XML_REGEXP_SEPAR) && |
2480 | 0 | (type2 <= XML_REGEXP_SEPAR_PARA)) || |
2481 | 0 | ((type2 >= XML_REGEXP_SYMBOL) && |
2482 | 0 | (type2 <= XML_REGEXP_SYMBOL_OTHERS)) || |
2483 | 0 | ((type2 >= XML_REGEXP_OTHER) && |
2484 | 0 | (type2 <= XML_REGEXP_OTHER_NA)) |
2485 | 0 | )return(0); |
2486 | 0 | break; |
2487 | 0 | case XML_REGEXP_NOTREALCHAR: /* \W */ |
2488 | 0 | break; |
2489 | | /* |
2490 | | * at that point we know both type 1 and type2 are from |
2491 | | * character categories are ordered and are different, |
2492 | | * it becomes simple because this is a partition |
2493 | | */ |
2494 | 0 | case XML_REGEXP_LETTER: |
2495 | 0 | if (type2 <= XML_REGEXP_LETTER_OTHERS) |
2496 | 0 | return(1); |
2497 | 0 | return(0); |
2498 | 0 | case XML_REGEXP_LETTER_UPPERCASE: |
2499 | 0 | case XML_REGEXP_LETTER_LOWERCASE: |
2500 | 0 | case XML_REGEXP_LETTER_TITLECASE: |
2501 | 0 | case XML_REGEXP_LETTER_MODIFIER: |
2502 | 0 | case XML_REGEXP_LETTER_OTHERS: |
2503 | 0 | return(0); |
2504 | 0 | case XML_REGEXP_MARK: |
2505 | 0 | if (type2 <= XML_REGEXP_MARK_ENCLOSING) |
2506 | 0 | return(1); |
2507 | 0 | return(0); |
2508 | 0 | case XML_REGEXP_MARK_NONSPACING: |
2509 | 0 | case XML_REGEXP_MARK_SPACECOMBINING: |
2510 | 0 | case XML_REGEXP_MARK_ENCLOSING: |
2511 | 0 | return(0); |
2512 | 0 | case XML_REGEXP_NUMBER: |
2513 | 0 | if (type2 <= XML_REGEXP_NUMBER_OTHERS) |
2514 | 0 | return(1); |
2515 | 0 | return(0); |
2516 | 0 | case XML_REGEXP_NUMBER_DECIMAL: |
2517 | 0 | case XML_REGEXP_NUMBER_LETTER: |
2518 | 0 | case XML_REGEXP_NUMBER_OTHERS: |
2519 | 0 | return(0); |
2520 | 0 | case XML_REGEXP_PUNCT: |
2521 | 0 | if (type2 <= XML_REGEXP_PUNCT_OTHERS) |
2522 | 0 | return(1); |
2523 | 0 | return(0); |
2524 | 0 | case XML_REGEXP_PUNCT_CONNECTOR: |
2525 | 0 | case XML_REGEXP_PUNCT_DASH: |
2526 | 0 | case XML_REGEXP_PUNCT_OPEN: |
2527 | 0 | case XML_REGEXP_PUNCT_CLOSE: |
2528 | 0 | case XML_REGEXP_PUNCT_INITQUOTE: |
2529 | 0 | case XML_REGEXP_PUNCT_FINQUOTE: |
2530 | 0 | case XML_REGEXP_PUNCT_OTHERS: |
2531 | 0 | return(0); |
2532 | 0 | case XML_REGEXP_SEPAR: |
2533 | 0 | if (type2 <= XML_REGEXP_SEPAR_PARA) |
2534 | 0 | return(1); |
2535 | 0 | return(0); |
2536 | 0 | case XML_REGEXP_SEPAR_SPACE: |
2537 | 0 | case XML_REGEXP_SEPAR_LINE: |
2538 | 0 | case XML_REGEXP_SEPAR_PARA: |
2539 | 0 | return(0); |
2540 | 0 | case XML_REGEXP_SYMBOL: |
2541 | 0 | if (type2 <= XML_REGEXP_SYMBOL_OTHERS) |
2542 | 0 | return(1); |
2543 | 0 | return(0); |
2544 | 0 | case XML_REGEXP_SYMBOL_MATH: |
2545 | 0 | case XML_REGEXP_SYMBOL_CURRENCY: |
2546 | 0 | case XML_REGEXP_SYMBOL_MODIFIER: |
2547 | 0 | case XML_REGEXP_SYMBOL_OTHERS: |
2548 | 0 | return(0); |
2549 | 0 | case XML_REGEXP_OTHER: |
2550 | 0 | if (type2 <= XML_REGEXP_OTHER_NA) |
2551 | 0 | return(1); |
2552 | 0 | return(0); |
2553 | 0 | case XML_REGEXP_OTHER_CONTROL: |
2554 | 0 | case XML_REGEXP_OTHER_FORMAT: |
2555 | 0 | case XML_REGEXP_OTHER_PRIVATE: |
2556 | 0 | case XML_REGEXP_OTHER_NA: |
2557 | 0 | return(0); |
2558 | 0 | default: |
2559 | 0 | break; |
2560 | 0 | } |
2561 | 0 | return(1); |
2562 | 0 | } |
2563 | | |
2564 | | /** |
2565 | | * Compares two atoms to check whether they are the same exactly |
2566 | | * this is used to remove equivalent transitions |
2567 | | * |
2568 | | * @param atom1 an atom |
2569 | | * @param atom2 an atom |
2570 | | * @param deep if not set only compare string pointers |
2571 | | * @returns 1 if same and 0 otherwise |
2572 | | */ |
2573 | | static int |
2574 | 0 | xmlFAEqualAtoms(xmlRegAtomPtr atom1, xmlRegAtomPtr atom2, int deep) { |
2575 | 0 | int ret = 0; |
2576 | |
|
2577 | 0 | if (atom1 == atom2) |
2578 | 0 | return(1); |
2579 | 0 | if ((atom1 == NULL) || (atom2 == NULL)) |
2580 | 0 | return(0); |
2581 | | |
2582 | 0 | if (atom1->type != atom2->type) |
2583 | 0 | return(0); |
2584 | 0 | switch (atom1->type) { |
2585 | 0 | case XML_REGEXP_EPSILON: |
2586 | 0 | ret = 0; |
2587 | 0 | break; |
2588 | 0 | case XML_REGEXP_STRING: |
2589 | 0 | if (!deep) |
2590 | 0 | ret = (atom1->valuep == atom2->valuep); |
2591 | 0 | else |
2592 | 0 | ret = xmlStrEqual((xmlChar *)atom1->valuep, |
2593 | 0 | (xmlChar *)atom2->valuep); |
2594 | 0 | break; |
2595 | 0 | case XML_REGEXP_CHARVAL: |
2596 | 0 | ret = (atom1->codepoint == atom2->codepoint); |
2597 | 0 | break; |
2598 | 0 | case XML_REGEXP_RANGES: |
2599 | | /* too hard to do in the general case */ |
2600 | 0 | ret = 0; |
2601 | 0 | default: |
2602 | 0 | break; |
2603 | 0 | } |
2604 | 0 | return(ret); |
2605 | 0 | } |
2606 | | |
2607 | | /** |
2608 | | * Compares two atoms to check whether they intersect in some ways, |
2609 | | * this is used by xmlFAComputesDeterminism and xmlFARecurseDeterminism only |
2610 | | * |
2611 | | * @param atom1 an atom |
2612 | | * @param atom2 an atom |
2613 | | * @param deep if not set only compare string pointers |
2614 | | * @returns 1 if yes and 0 otherwise |
2615 | | */ |
2616 | | static int |
2617 | 0 | xmlFACompareAtoms(xmlRegAtomPtr atom1, xmlRegAtomPtr atom2, int deep) { |
2618 | 0 | int ret = 1; |
2619 | |
|
2620 | 0 | if (atom1 == atom2) |
2621 | 0 | return(1); |
2622 | 0 | if ((atom1 == NULL) || (atom2 == NULL)) |
2623 | 0 | return(0); |
2624 | | |
2625 | 0 | if ((atom1->type == XML_REGEXP_ANYCHAR) || |
2626 | 0 | (atom2->type == XML_REGEXP_ANYCHAR)) |
2627 | 0 | return(1); |
2628 | | |
2629 | 0 | if (atom1->type > atom2->type) { |
2630 | 0 | xmlRegAtomPtr tmp; |
2631 | 0 | tmp = atom1; |
2632 | 0 | atom1 = atom2; |
2633 | 0 | atom2 = tmp; |
2634 | 0 | } |
2635 | 0 | if (atom1->type != atom2->type) { |
2636 | 0 | ret = xmlFACompareAtomTypes(atom1->type, atom2->type); |
2637 | | /* if they can't intersect at the type level break now */ |
2638 | 0 | if (ret == 0) |
2639 | 0 | return(0); |
2640 | 0 | } |
2641 | 0 | switch (atom1->type) { |
2642 | 0 | case XML_REGEXP_STRING: |
2643 | 0 | if (!deep) |
2644 | 0 | ret = (atom1->valuep != atom2->valuep); |
2645 | 0 | else { |
2646 | 0 | xmlChar *val1 = (xmlChar *)atom1->valuep; |
2647 | 0 | xmlChar *val2 = (xmlChar *)atom2->valuep; |
2648 | 0 | int compound1 = (xmlStrchr(val1, '|') != NULL); |
2649 | 0 | int compound2 = (xmlStrchr(val2, '|') != NULL); |
2650 | | |
2651 | | /* Ignore negative match flag for ##other namespaces */ |
2652 | 0 | if (compound1 != compound2) |
2653 | 0 | return(0); |
2654 | | |
2655 | 0 | ret = xmlRegStrEqualWildcard(val1, val2); |
2656 | 0 | } |
2657 | 0 | break; |
2658 | 0 | case XML_REGEXP_EPSILON: |
2659 | 0 | goto not_determinist; |
2660 | 0 | case XML_REGEXP_CHARVAL: |
2661 | 0 | if (atom2->type == XML_REGEXP_CHARVAL) { |
2662 | 0 | ret = (atom1->codepoint == atom2->codepoint); |
2663 | 0 | } else { |
2664 | 0 | ret = xmlRegCheckCharacter(atom2, atom1->codepoint); |
2665 | 0 | if (ret < 0) |
2666 | 0 | ret = 1; |
2667 | 0 | } |
2668 | 0 | break; |
2669 | 0 | case XML_REGEXP_RANGES: |
2670 | 0 | if (atom2->type == XML_REGEXP_RANGES) { |
2671 | 0 | int i, j, res; |
2672 | 0 | xmlRegRangePtr r1, r2; |
2673 | | |
2674 | | /* |
2675 | | * need to check that none of the ranges eventually matches |
2676 | | */ |
2677 | 0 | for (i = 0;i < atom1->nbRanges;i++) { |
2678 | 0 | for (j = 0;j < atom2->nbRanges;j++) { |
2679 | 0 | r1 = atom1->ranges[i]; |
2680 | 0 | r2 = atom2->ranges[j]; |
2681 | 0 | res = xmlFACompareRanges(r1, r2); |
2682 | 0 | if (res == 1) { |
2683 | 0 | ret = 1; |
2684 | 0 | goto done; |
2685 | 0 | } |
2686 | 0 | } |
2687 | 0 | } |
2688 | 0 | ret = 0; |
2689 | 0 | } |
2690 | 0 | break; |
2691 | 0 | default: |
2692 | 0 | goto not_determinist; |
2693 | 0 | } |
2694 | 0 | done: |
2695 | 0 | if (atom1->neg != atom2->neg) { |
2696 | 0 | ret = !ret; |
2697 | 0 | } |
2698 | 0 | if (ret == 0) |
2699 | 0 | return(0); |
2700 | 0 | not_determinist: |
2701 | 0 | return(1); |
2702 | 0 | } |
2703 | | |
2704 | | /** |
2705 | | * Check whether the associated regexp is determinist, |
2706 | | * should be called after xmlFAEliminateEpsilonTransitions |
2707 | | * |
2708 | | * @param ctxt a regexp parser context |
2709 | | * @param state regexp state |
2710 | | * @param fromnr the from state |
2711 | | * @param tonr the to state |
2712 | | * @param atom the atom |
2713 | | */ |
2714 | | static int |
2715 | | xmlFARecurseDeterminism(xmlRegParserCtxtPtr ctxt, xmlRegStatePtr state, |
2716 | 0 | int fromnr, int tonr, xmlRegAtomPtr atom) { |
2717 | 0 | int ret = 1; |
2718 | 0 | int res; |
2719 | 0 | int transnr, nbTrans; |
2720 | 0 | xmlRegTransPtr t1; |
2721 | 0 | int deep = 1; |
2722 | |
|
2723 | 0 | if (state == NULL) |
2724 | 0 | return(ret); |
2725 | 0 | if (state->markd == XML_REGEXP_MARK_VISITED) |
2726 | 0 | return(ret); |
2727 | | |
2728 | 0 | if (ctxt->flags & AM_AUTOMATA_RNG) |
2729 | 0 | deep = 0; |
2730 | | |
2731 | | /* |
2732 | | * don't recurse on transitions potentially added in the course of |
2733 | | * the elimination. |
2734 | | */ |
2735 | 0 | nbTrans = state->nbTrans; |
2736 | 0 | for (transnr = 0;transnr < nbTrans;transnr++) { |
2737 | 0 | t1 = &(state->trans[transnr]); |
2738 | | /* |
2739 | | * check transitions conflicting with the one looked at |
2740 | | */ |
2741 | 0 | if ((t1->to < 0) || (t1->to == fromnr)) |
2742 | 0 | continue; |
2743 | 0 | if (t1->atom == NULL) { |
2744 | 0 | state->markd = XML_REGEXP_MARK_VISITED; |
2745 | 0 | res = xmlFARecurseDeterminism(ctxt, ctxt->states[t1->to], |
2746 | 0 | fromnr, tonr, atom); |
2747 | 0 | if (res == 0) { |
2748 | 0 | ret = 0; |
2749 | | /* t1->nd = 1; */ |
2750 | 0 | } |
2751 | 0 | continue; |
2752 | 0 | } |
2753 | 0 | if (xmlFACompareAtoms(t1->atom, atom, deep)) { |
2754 | | /* Treat equal transitions as deterministic. */ |
2755 | 0 | if ((t1->to != tonr) || |
2756 | 0 | (!xmlFAEqualAtoms(t1->atom, atom, deep))) |
2757 | 0 | ret = 0; |
2758 | | /* mark the transition as non-deterministic */ |
2759 | 0 | t1->nd = 1; |
2760 | 0 | } |
2761 | 0 | } |
2762 | 0 | return(ret); |
2763 | 0 | } |
2764 | | |
2765 | | /** |
2766 | | * Reset flags after checking determinism. |
2767 | | * |
2768 | | * @param ctxt a regexp parser context |
2769 | | * @param state regexp state |
2770 | | */ |
2771 | | static void |
2772 | 0 | xmlFAFinishRecurseDeterminism(xmlRegParserCtxtPtr ctxt, xmlRegStatePtr state) { |
2773 | 0 | int transnr, nbTrans; |
2774 | |
|
2775 | 0 | if (state == NULL) |
2776 | 0 | return; |
2777 | 0 | if (state->markd != XML_REGEXP_MARK_VISITED) |
2778 | 0 | return; |
2779 | 0 | state->markd = 0; |
2780 | |
|
2781 | 0 | nbTrans = state->nbTrans; |
2782 | 0 | for (transnr = 0; transnr < nbTrans; transnr++) { |
2783 | 0 | xmlRegTransPtr t1 = &state->trans[transnr]; |
2784 | 0 | if ((t1->atom == NULL) && (t1->to >= 0)) |
2785 | 0 | xmlFAFinishRecurseDeterminism(ctxt, ctxt->states[t1->to]); |
2786 | 0 | } |
2787 | 0 | } |
2788 | | |
2789 | | /** |
2790 | | * Check whether the associated regexp is determinist, |
2791 | | * should be called after xmlFAEliminateEpsilonTransitions |
2792 | | * |
2793 | | * @param ctxt a regexp parser context |
2794 | | */ |
2795 | | static int |
2796 | 0 | xmlFAComputesDeterminism(xmlRegParserCtxtPtr ctxt) { |
2797 | 0 | int statenr, transnr; |
2798 | 0 | xmlRegStatePtr state; |
2799 | 0 | xmlRegTransPtr t1, t2, last; |
2800 | 0 | int i; |
2801 | 0 | int ret = 1; |
2802 | 0 | int deep = 1; |
2803 | |
|
2804 | 0 | if (ctxt->determinist != -1) |
2805 | 0 | return(ctxt->determinist); |
2806 | | |
2807 | 0 | if (ctxt->flags & AM_AUTOMATA_RNG) |
2808 | 0 | deep = 0; |
2809 | | |
2810 | | /* |
2811 | | * First cleanup the automata removing cancelled transitions |
2812 | | */ |
2813 | 0 | for (statenr = 0;statenr < ctxt->nbStates;statenr++) { |
2814 | 0 | state = ctxt->states[statenr]; |
2815 | 0 | if (state == NULL) |
2816 | 0 | continue; |
2817 | 0 | if (state->nbTrans < 2) |
2818 | 0 | continue; |
2819 | 0 | for (transnr = 0;transnr < state->nbTrans;transnr++) { |
2820 | 0 | t1 = &(state->trans[transnr]); |
2821 | | /* |
2822 | | * Determinism checks in case of counted or all transitions |
2823 | | * will have to be handled separately |
2824 | | */ |
2825 | 0 | if (t1->atom == NULL) { |
2826 | | /* t1->nd = 1; */ |
2827 | 0 | continue; |
2828 | 0 | } |
2829 | 0 | if (t1->to < 0) /* eliminated */ |
2830 | 0 | continue; |
2831 | 0 | for (i = 0;i < transnr;i++) { |
2832 | 0 | t2 = &(state->trans[i]); |
2833 | 0 | if (t2->to < 0) /* eliminated */ |
2834 | 0 | continue; |
2835 | 0 | if (t2->atom != NULL) { |
2836 | 0 | if (t1->to == t2->to) { |
2837 | | /* |
2838 | | * Here we use deep because we want to keep the |
2839 | | * transitions which indicate a conflict |
2840 | | */ |
2841 | 0 | if (xmlFAEqualAtoms(t1->atom, t2->atom, deep) && |
2842 | 0 | (t1->counter == t2->counter) && |
2843 | 0 | (t1->count == t2->count)) |
2844 | 0 | t2->to = -1; /* eliminated */ |
2845 | 0 | } |
2846 | 0 | } |
2847 | 0 | } |
2848 | 0 | } |
2849 | 0 | } |
2850 | | |
2851 | | /* |
2852 | | * Check for all states that there aren't 2 transitions |
2853 | | * with the same atom and a different target. |
2854 | | */ |
2855 | 0 | for (statenr = 0;statenr < ctxt->nbStates;statenr++) { |
2856 | 0 | state = ctxt->states[statenr]; |
2857 | 0 | if (state == NULL) |
2858 | 0 | continue; |
2859 | 0 | if (state->nbTrans < 2) |
2860 | 0 | continue; |
2861 | 0 | last = NULL; |
2862 | 0 | for (transnr = 0;transnr < state->nbTrans;transnr++) { |
2863 | 0 | t1 = &(state->trans[transnr]); |
2864 | | /* |
2865 | | * Determinism checks in case of counted or all transitions |
2866 | | * will have to be handled separately |
2867 | | */ |
2868 | 0 | if (t1->atom == NULL) { |
2869 | 0 | continue; |
2870 | 0 | } |
2871 | 0 | if (t1->to < 0) /* eliminated */ |
2872 | 0 | continue; |
2873 | 0 | for (i = 0;i < transnr;i++) { |
2874 | 0 | t2 = &(state->trans[i]); |
2875 | 0 | if (t2->to < 0) /* eliminated */ |
2876 | 0 | continue; |
2877 | 0 | if (t2->atom != NULL) { |
2878 | | /* |
2879 | | * But here we don't use deep because we want to |
2880 | | * find transitions which indicate a conflict |
2881 | | */ |
2882 | 0 | if (xmlFACompareAtoms(t1->atom, t2->atom, 1)) { |
2883 | | /* |
2884 | | * Treat equal counter transitions that couldn't be |
2885 | | * eliminated as deterministic. |
2886 | | */ |
2887 | 0 | if ((t1->to != t2->to) || |
2888 | 0 | (t1->counter == t2->counter) || |
2889 | 0 | (!xmlFAEqualAtoms(t1->atom, t2->atom, deep))) |
2890 | 0 | ret = 0; |
2891 | | /* mark the transitions as non-deterministic ones */ |
2892 | 0 | t1->nd = 1; |
2893 | 0 | t2->nd = 1; |
2894 | 0 | last = t1; |
2895 | 0 | } |
2896 | 0 | } else { |
2897 | 0 | int res; |
2898 | | |
2899 | | /* |
2900 | | * do the closure in case of remaining specific |
2901 | | * epsilon transitions like choices or all |
2902 | | */ |
2903 | 0 | res = xmlFARecurseDeterminism(ctxt, ctxt->states[t2->to], |
2904 | 0 | statenr, t1->to, t1->atom); |
2905 | 0 | xmlFAFinishRecurseDeterminism(ctxt, ctxt->states[t2->to]); |
2906 | | /* don't shortcut the computation so all non deterministic |
2907 | | transition get marked down |
2908 | | if (ret == 0) |
2909 | | return(0); |
2910 | | */ |
2911 | 0 | if (res == 0) { |
2912 | 0 | t1->nd = 1; |
2913 | | /* t2->nd = 1; */ |
2914 | 0 | last = t1; |
2915 | 0 | ret = 0; |
2916 | 0 | } |
2917 | 0 | } |
2918 | 0 | } |
2919 | | /* don't shortcut the computation so all non deterministic |
2920 | | transition get marked down |
2921 | | if (ret == 0) |
2922 | | break; */ |
2923 | 0 | } |
2924 | | |
2925 | | /* |
2926 | | * mark specifically the last non-deterministic transition |
2927 | | * from a state since there is no need to set-up rollback |
2928 | | * from it |
2929 | | */ |
2930 | 0 | if (last != NULL) { |
2931 | 0 | last->nd = 2; |
2932 | 0 | } |
2933 | | |
2934 | | /* don't shortcut the computation so all non deterministic |
2935 | | transition get marked down |
2936 | | if (ret == 0) |
2937 | | break; */ |
2938 | 0 | } |
2939 | |
|
2940 | 0 | ctxt->determinist = ret; |
2941 | 0 | return(ret); |
2942 | 0 | } |
2943 | | |
2944 | | /************************************************************************ |
2945 | | * * |
2946 | | * Routines to check input against transition atoms * |
2947 | | * * |
2948 | | ************************************************************************/ |
2949 | | |
2950 | | static int |
2951 | | xmlRegCheckCharacterRange(xmlRegAtomType type, int codepoint, int neg, |
2952 | 0 | int start, int end, const xmlChar *blockName) { |
2953 | 0 | int ret = 0; |
2954 | |
|
2955 | 0 | switch (type) { |
2956 | 0 | case XML_REGEXP_STRING: |
2957 | 0 | case XML_REGEXP_SUBREG: |
2958 | 0 | case XML_REGEXP_RANGES: |
2959 | 0 | case XML_REGEXP_EPSILON: |
2960 | 0 | return(-1); |
2961 | 0 | case XML_REGEXP_ANYCHAR: |
2962 | 0 | ret = ((codepoint != '\n') && (codepoint != '\r')); |
2963 | 0 | break; |
2964 | 0 | case XML_REGEXP_CHARVAL: |
2965 | 0 | ret = ((codepoint >= start) && (codepoint <= end)); |
2966 | 0 | break; |
2967 | 0 | case XML_REGEXP_NOTSPACE: |
2968 | 0 | neg = !neg; |
2969 | | /* Falls through. */ |
2970 | 0 | case XML_REGEXP_ANYSPACE: |
2971 | 0 | ret = ((codepoint == '\n') || (codepoint == '\r') || |
2972 | 0 | (codepoint == '\t') || (codepoint == ' ')); |
2973 | 0 | break; |
2974 | 0 | case XML_REGEXP_NOTINITNAME: |
2975 | 0 | neg = !neg; |
2976 | | /* Falls through. */ |
2977 | 0 | case XML_REGEXP_INITNAME: |
2978 | 0 | ret = (IS_LETTER(codepoint) || |
2979 | 0 | (codepoint == '_') || (codepoint == ':')); |
2980 | 0 | break; |
2981 | 0 | case XML_REGEXP_NOTNAMECHAR: |
2982 | 0 | neg = !neg; |
2983 | | /* Falls through. */ |
2984 | 0 | case XML_REGEXP_NAMECHAR: |
2985 | 0 | ret = (IS_LETTER(codepoint) || IS_DIGIT(codepoint) || |
2986 | 0 | (codepoint == '.') || (codepoint == '-') || |
2987 | 0 | (codepoint == '_') || (codepoint == ':') || |
2988 | 0 | IS_COMBINING(codepoint) || IS_EXTENDER(codepoint)); |
2989 | 0 | break; |
2990 | 0 | case XML_REGEXP_NOTDECIMAL: |
2991 | 0 | neg = !neg; |
2992 | | /* Falls through. */ |
2993 | 0 | case XML_REGEXP_DECIMAL: |
2994 | 0 | ret = xmlUCSIsCatNd(codepoint); |
2995 | 0 | break; |
2996 | 0 | case XML_REGEXP_REALCHAR: |
2997 | 0 | neg = !neg; |
2998 | | /* Falls through. */ |
2999 | 0 | case XML_REGEXP_NOTREALCHAR: |
3000 | 0 | ret = xmlUCSIsCatP(codepoint); |
3001 | 0 | if (ret == 0) |
3002 | 0 | ret = xmlUCSIsCatZ(codepoint); |
3003 | 0 | if (ret == 0) |
3004 | 0 | ret = xmlUCSIsCatC(codepoint); |
3005 | 0 | break; |
3006 | 0 | case XML_REGEXP_LETTER: |
3007 | 0 | ret = xmlUCSIsCatL(codepoint); |
3008 | 0 | break; |
3009 | 0 | case XML_REGEXP_LETTER_UPPERCASE: |
3010 | 0 | ret = xmlUCSIsCatLu(codepoint); |
3011 | 0 | break; |
3012 | 0 | case XML_REGEXP_LETTER_LOWERCASE: |
3013 | 0 | ret = xmlUCSIsCatLl(codepoint); |
3014 | 0 | break; |
3015 | 0 | case XML_REGEXP_LETTER_TITLECASE: |
3016 | 0 | ret = xmlUCSIsCatLt(codepoint); |
3017 | 0 | break; |
3018 | 0 | case XML_REGEXP_LETTER_MODIFIER: |
3019 | 0 | ret = xmlUCSIsCatLm(codepoint); |
3020 | 0 | break; |
3021 | 0 | case XML_REGEXP_LETTER_OTHERS: |
3022 | 0 | ret = xmlUCSIsCatLo(codepoint); |
3023 | 0 | break; |
3024 | 0 | case XML_REGEXP_MARK: |
3025 | 0 | ret = xmlUCSIsCatM(codepoint); |
3026 | 0 | break; |
3027 | 0 | case XML_REGEXP_MARK_NONSPACING: |
3028 | 0 | ret = xmlUCSIsCatMn(codepoint); |
3029 | 0 | break; |
3030 | 0 | case XML_REGEXP_MARK_SPACECOMBINING: |
3031 | 0 | ret = xmlUCSIsCatMc(codepoint); |
3032 | 0 | break; |
3033 | 0 | case XML_REGEXP_MARK_ENCLOSING: |
3034 | 0 | ret = xmlUCSIsCatMe(codepoint); |
3035 | 0 | break; |
3036 | 0 | case XML_REGEXP_NUMBER: |
3037 | 0 | ret = xmlUCSIsCatN(codepoint); |
3038 | 0 | break; |
3039 | 0 | case XML_REGEXP_NUMBER_DECIMAL: |
3040 | 0 | ret = xmlUCSIsCatNd(codepoint); |
3041 | 0 | break; |
3042 | 0 | case XML_REGEXP_NUMBER_LETTER: |
3043 | 0 | ret = xmlUCSIsCatNl(codepoint); |
3044 | 0 | break; |
3045 | 0 | case XML_REGEXP_NUMBER_OTHERS: |
3046 | 0 | ret = xmlUCSIsCatNo(codepoint); |
3047 | 0 | break; |
3048 | 0 | case XML_REGEXP_PUNCT: |
3049 | 0 | ret = xmlUCSIsCatP(codepoint); |
3050 | 0 | break; |
3051 | 0 | case XML_REGEXP_PUNCT_CONNECTOR: |
3052 | 0 | ret = xmlUCSIsCatPc(codepoint); |
3053 | 0 | break; |
3054 | 0 | case XML_REGEXP_PUNCT_DASH: |
3055 | 0 | ret = xmlUCSIsCatPd(codepoint); |
3056 | 0 | break; |
3057 | 0 | case XML_REGEXP_PUNCT_OPEN: |
3058 | 0 | ret = xmlUCSIsCatPs(codepoint); |
3059 | 0 | break; |
3060 | 0 | case XML_REGEXP_PUNCT_CLOSE: |
3061 | 0 | ret = xmlUCSIsCatPe(codepoint); |
3062 | 0 | break; |
3063 | 0 | case XML_REGEXP_PUNCT_INITQUOTE: |
3064 | 0 | ret = xmlUCSIsCatPi(codepoint); |
3065 | 0 | break; |
3066 | 0 | case XML_REGEXP_PUNCT_FINQUOTE: |
3067 | 0 | ret = xmlUCSIsCatPf(codepoint); |
3068 | 0 | break; |
3069 | 0 | case XML_REGEXP_PUNCT_OTHERS: |
3070 | 0 | ret = xmlUCSIsCatPo(codepoint); |
3071 | 0 | break; |
3072 | 0 | case XML_REGEXP_SEPAR: |
3073 | 0 | ret = xmlUCSIsCatZ(codepoint); |
3074 | 0 | break; |
3075 | 0 | case XML_REGEXP_SEPAR_SPACE: |
3076 | 0 | ret = xmlUCSIsCatZs(codepoint); |
3077 | 0 | break; |
3078 | 0 | case XML_REGEXP_SEPAR_LINE: |
3079 | 0 | ret = xmlUCSIsCatZl(codepoint); |
3080 | 0 | break; |
3081 | 0 | case XML_REGEXP_SEPAR_PARA: |
3082 | 0 | ret = xmlUCSIsCatZp(codepoint); |
3083 | 0 | break; |
3084 | 0 | case XML_REGEXP_SYMBOL: |
3085 | 0 | ret = xmlUCSIsCatS(codepoint); |
3086 | 0 | break; |
3087 | 0 | case XML_REGEXP_SYMBOL_MATH: |
3088 | 0 | ret = xmlUCSIsCatSm(codepoint); |
3089 | 0 | break; |
3090 | 0 | case XML_REGEXP_SYMBOL_CURRENCY: |
3091 | 0 | ret = xmlUCSIsCatSc(codepoint); |
3092 | 0 | break; |
3093 | 0 | case XML_REGEXP_SYMBOL_MODIFIER: |
3094 | 0 | ret = xmlUCSIsCatSk(codepoint); |
3095 | 0 | break; |
3096 | 0 | case XML_REGEXP_SYMBOL_OTHERS: |
3097 | 0 | ret = xmlUCSIsCatSo(codepoint); |
3098 | 0 | break; |
3099 | 0 | case XML_REGEXP_OTHER: |
3100 | 0 | ret = xmlUCSIsCatC(codepoint); |
3101 | 0 | break; |
3102 | 0 | case XML_REGEXP_OTHER_CONTROL: |
3103 | 0 | ret = xmlUCSIsCatCc(codepoint); |
3104 | 0 | break; |
3105 | 0 | case XML_REGEXP_OTHER_FORMAT: |
3106 | 0 | ret = xmlUCSIsCatCf(codepoint); |
3107 | 0 | break; |
3108 | 0 | case XML_REGEXP_OTHER_PRIVATE: |
3109 | 0 | ret = xmlUCSIsCatCo(codepoint); |
3110 | 0 | break; |
3111 | 0 | case XML_REGEXP_OTHER_NA: |
3112 | | /* ret = xmlUCSIsCatCn(codepoint); */ |
3113 | | /* Seems it doesn't exist anymore in recent Unicode releases */ |
3114 | 0 | ret = 0; |
3115 | 0 | break; |
3116 | 0 | case XML_REGEXP_BLOCK_NAME: |
3117 | 0 | ret = xmlUCSIsBlock(codepoint, (const char *) blockName); |
3118 | 0 | break; |
3119 | 0 | } |
3120 | 0 | if (neg) |
3121 | 0 | return(!ret); |
3122 | 0 | return(ret); |
3123 | 0 | } |
3124 | | |
3125 | | static int |
3126 | 0 | xmlRegCheckCharacter(xmlRegAtomPtr atom, int codepoint) { |
3127 | 0 | int i, ret = 0; |
3128 | 0 | xmlRegRangePtr range; |
3129 | |
|
3130 | 0 | if ((atom == NULL) || (!IS_CHAR(codepoint))) |
3131 | 0 | return(-1); |
3132 | | |
3133 | 0 | switch (atom->type) { |
3134 | 0 | case XML_REGEXP_SUBREG: |
3135 | 0 | case XML_REGEXP_EPSILON: |
3136 | 0 | return(-1); |
3137 | 0 | case XML_REGEXP_CHARVAL: |
3138 | 0 | return(codepoint == atom->codepoint); |
3139 | 0 | case XML_REGEXP_RANGES: { |
3140 | 0 | int accept = 0; |
3141 | |
|
3142 | 0 | for (i = 0;i < atom->nbRanges;i++) { |
3143 | 0 | range = atom->ranges[i]; |
3144 | 0 | if (range->neg == 2) { |
3145 | 0 | ret = xmlRegCheckCharacterRange(range->type, codepoint, |
3146 | 0 | 0, range->start, range->end, |
3147 | 0 | range->blockName); |
3148 | 0 | if (ret != 0) |
3149 | 0 | return(0); /* excluded char */ |
3150 | 0 | } else if (range->neg) { |
3151 | 0 | ret = xmlRegCheckCharacterRange(range->type, codepoint, |
3152 | 0 | 0, range->start, range->end, |
3153 | 0 | range->blockName); |
3154 | 0 | if (ret == 0) |
3155 | 0 | accept = 1; |
3156 | 0 | else |
3157 | 0 | return(0); |
3158 | 0 | } else { |
3159 | 0 | ret = xmlRegCheckCharacterRange(range->type, codepoint, |
3160 | 0 | 0, range->start, range->end, |
3161 | 0 | range->blockName); |
3162 | 0 | if (ret != 0) |
3163 | 0 | accept = 1; /* might still be excluded */ |
3164 | 0 | } |
3165 | 0 | } |
3166 | 0 | return(accept); |
3167 | 0 | } |
3168 | 0 | case XML_REGEXP_STRING: |
3169 | 0 | return(-1); |
3170 | 0 | case XML_REGEXP_ANYCHAR: |
3171 | 0 | case XML_REGEXP_ANYSPACE: |
3172 | 0 | case XML_REGEXP_NOTSPACE: |
3173 | 0 | case XML_REGEXP_INITNAME: |
3174 | 0 | case XML_REGEXP_NOTINITNAME: |
3175 | 0 | case XML_REGEXP_NAMECHAR: |
3176 | 0 | case XML_REGEXP_NOTNAMECHAR: |
3177 | 0 | case XML_REGEXP_DECIMAL: |
3178 | 0 | case XML_REGEXP_NOTDECIMAL: |
3179 | 0 | case XML_REGEXP_REALCHAR: |
3180 | 0 | case XML_REGEXP_NOTREALCHAR: |
3181 | 0 | case XML_REGEXP_LETTER: |
3182 | 0 | case XML_REGEXP_LETTER_UPPERCASE: |
3183 | 0 | case XML_REGEXP_LETTER_LOWERCASE: |
3184 | 0 | case XML_REGEXP_LETTER_TITLECASE: |
3185 | 0 | case XML_REGEXP_LETTER_MODIFIER: |
3186 | 0 | case XML_REGEXP_LETTER_OTHERS: |
3187 | 0 | case XML_REGEXP_MARK: |
3188 | 0 | case XML_REGEXP_MARK_NONSPACING: |
3189 | 0 | case XML_REGEXP_MARK_SPACECOMBINING: |
3190 | 0 | case XML_REGEXP_MARK_ENCLOSING: |
3191 | 0 | case XML_REGEXP_NUMBER: |
3192 | 0 | case XML_REGEXP_NUMBER_DECIMAL: |
3193 | 0 | case XML_REGEXP_NUMBER_LETTER: |
3194 | 0 | case XML_REGEXP_NUMBER_OTHERS: |
3195 | 0 | case XML_REGEXP_PUNCT: |
3196 | 0 | case XML_REGEXP_PUNCT_CONNECTOR: |
3197 | 0 | case XML_REGEXP_PUNCT_DASH: |
3198 | 0 | case XML_REGEXP_PUNCT_OPEN: |
3199 | 0 | case XML_REGEXP_PUNCT_CLOSE: |
3200 | 0 | case XML_REGEXP_PUNCT_INITQUOTE: |
3201 | 0 | case XML_REGEXP_PUNCT_FINQUOTE: |
3202 | 0 | case XML_REGEXP_PUNCT_OTHERS: |
3203 | 0 | case XML_REGEXP_SEPAR: |
3204 | 0 | case XML_REGEXP_SEPAR_SPACE: |
3205 | 0 | case XML_REGEXP_SEPAR_LINE: |
3206 | 0 | case XML_REGEXP_SEPAR_PARA: |
3207 | 0 | case XML_REGEXP_SYMBOL: |
3208 | 0 | case XML_REGEXP_SYMBOL_MATH: |
3209 | 0 | case XML_REGEXP_SYMBOL_CURRENCY: |
3210 | 0 | case XML_REGEXP_SYMBOL_MODIFIER: |
3211 | 0 | case XML_REGEXP_SYMBOL_OTHERS: |
3212 | 0 | case XML_REGEXP_OTHER: |
3213 | 0 | case XML_REGEXP_OTHER_CONTROL: |
3214 | 0 | case XML_REGEXP_OTHER_FORMAT: |
3215 | 0 | case XML_REGEXP_OTHER_PRIVATE: |
3216 | 0 | case XML_REGEXP_OTHER_NA: |
3217 | 0 | case XML_REGEXP_BLOCK_NAME: |
3218 | 0 | ret = xmlRegCheckCharacterRange(atom->type, codepoint, 0, 0, 0, |
3219 | 0 | (const xmlChar *)atom->valuep); |
3220 | 0 | if (atom->neg) |
3221 | 0 | ret = !ret; |
3222 | 0 | break; |
3223 | 0 | } |
3224 | 0 | return(ret); |
3225 | 0 | } |
3226 | | |
3227 | | /************************************************************************ |
3228 | | * * |
3229 | | * Saving and restoring state of an execution context * |
3230 | | * * |
3231 | | ************************************************************************/ |
3232 | | |
3233 | | static void |
3234 | 0 | xmlFARegExecSave(xmlRegExecCtxtPtr exec) { |
3235 | 0 | #ifdef MAX_PUSH |
3236 | 0 | if (exec->nbPush > MAX_PUSH) { |
3237 | 0 | exec->status = XML_REGEXP_INTERNAL_LIMIT; |
3238 | 0 | return; |
3239 | 0 | } |
3240 | 0 | exec->nbPush++; |
3241 | 0 | #endif |
3242 | |
|
3243 | 0 | if (exec->nbRollbacks >= exec->maxRollbacks) { |
3244 | 0 | xmlRegExecRollback *tmp; |
3245 | 0 | int newSize; |
3246 | 0 | int len = exec->nbRollbacks; |
3247 | |
|
3248 | 0 | newSize = xmlGrowCapacity(exec->maxRollbacks, sizeof(tmp[0]), |
3249 | 0 | 4, XML_MAX_ITEMS); |
3250 | 0 | if (newSize < 0) { |
3251 | 0 | exec->status = XML_REGEXP_OUT_OF_MEMORY; |
3252 | 0 | return; |
3253 | 0 | } |
3254 | 0 | tmp = xmlRealloc(exec->rollbacks, newSize * sizeof(tmp[0])); |
3255 | 0 | if (tmp == NULL) { |
3256 | 0 | exec->status = XML_REGEXP_OUT_OF_MEMORY; |
3257 | 0 | return; |
3258 | 0 | } |
3259 | 0 | exec->rollbacks = tmp; |
3260 | 0 | exec->maxRollbacks = newSize; |
3261 | 0 | tmp = &exec->rollbacks[len]; |
3262 | 0 | memset(tmp, 0, (exec->maxRollbacks - len) * sizeof(xmlRegExecRollback)); |
3263 | 0 | } |
3264 | 0 | exec->rollbacks[exec->nbRollbacks].state = exec->state; |
3265 | 0 | exec->rollbacks[exec->nbRollbacks].index = exec->index; |
3266 | 0 | exec->rollbacks[exec->nbRollbacks].nextbranch = exec->transno + 1; |
3267 | 0 | if (exec->comp->nbCounters > 0) { |
3268 | 0 | if (exec->rollbacks[exec->nbRollbacks].counts == NULL) { |
3269 | 0 | exec->rollbacks[exec->nbRollbacks].counts = (int *) |
3270 | 0 | xmlMalloc(exec->comp->nbCounters * sizeof(int)); |
3271 | 0 | if (exec->rollbacks[exec->nbRollbacks].counts == NULL) { |
3272 | 0 | exec->status = XML_REGEXP_OUT_OF_MEMORY; |
3273 | 0 | return; |
3274 | 0 | } |
3275 | 0 | } |
3276 | 0 | memcpy(exec->rollbacks[exec->nbRollbacks].counts, exec->counts, |
3277 | 0 | exec->comp->nbCounters * sizeof(int)); |
3278 | 0 | } |
3279 | 0 | exec->nbRollbacks++; |
3280 | 0 | } |
3281 | | |
3282 | | static void |
3283 | 0 | xmlFARegExecRollBack(xmlRegExecCtxtPtr exec) { |
3284 | 0 | if (exec->status != XML_REGEXP_OK) |
3285 | 0 | return; |
3286 | 0 | if (exec->nbRollbacks <= 0) { |
3287 | 0 | exec->status = XML_REGEXP_NOT_FOUND; |
3288 | 0 | return; |
3289 | 0 | } |
3290 | 0 | exec->nbRollbacks--; |
3291 | 0 | exec->state = exec->rollbacks[exec->nbRollbacks].state; |
3292 | 0 | exec->index = exec->rollbacks[exec->nbRollbacks].index; |
3293 | 0 | exec->transno = exec->rollbacks[exec->nbRollbacks].nextbranch; |
3294 | 0 | if (exec->comp->nbCounters > 0) { |
3295 | 0 | if (exec->rollbacks[exec->nbRollbacks].counts == NULL) { |
3296 | 0 | exec->status = XML_REGEXP_INTERNAL_ERROR; |
3297 | 0 | return; |
3298 | 0 | } |
3299 | 0 | if (exec->counts) { |
3300 | 0 | memcpy(exec->counts, exec->rollbacks[exec->nbRollbacks].counts, |
3301 | 0 | exec->comp->nbCounters * sizeof(int)); |
3302 | 0 | } |
3303 | 0 | } |
3304 | 0 | } |
3305 | | |
3306 | | /************************************************************************ |
3307 | | * * |
3308 | | * Verifier, running an input against a compiled regexp * |
3309 | | * * |
3310 | | ************************************************************************/ |
3311 | | |
3312 | | static int |
3313 | 0 | xmlFARegExec(xmlRegexpPtr comp, const xmlChar *content) { |
3314 | 0 | xmlRegExecCtxt execval; |
3315 | 0 | xmlRegExecCtxtPtr exec = &execval; |
3316 | 0 | int ret, codepoint = 0, len, deter; |
3317 | |
|
3318 | 0 | exec->inputString = content; |
3319 | 0 | exec->index = 0; |
3320 | 0 | exec->nbPush = 0; |
3321 | 0 | exec->determinist = 1; |
3322 | 0 | exec->maxRollbacks = 0; |
3323 | 0 | exec->nbRollbacks = 0; |
3324 | 0 | exec->rollbacks = NULL; |
3325 | 0 | exec->status = XML_REGEXP_OK; |
3326 | 0 | exec->comp = comp; |
3327 | 0 | exec->state = comp->states[0]; |
3328 | 0 | exec->transno = 0; |
3329 | 0 | exec->transcount = 0; |
3330 | 0 | exec->inputStack = NULL; |
3331 | 0 | exec->inputStackMax = 0; |
3332 | 0 | if (comp->nbCounters > 0) { |
3333 | 0 | exec->counts = (int *) xmlMalloc(comp->nbCounters * sizeof(int)); |
3334 | 0 | if (exec->counts == NULL) { |
3335 | 0 | return(XML_REGEXP_OUT_OF_MEMORY); |
3336 | 0 | } |
3337 | 0 | memset(exec->counts, 0, comp->nbCounters * sizeof(int)); |
3338 | 0 | } else |
3339 | 0 | exec->counts = NULL; |
3340 | 0 | while ((exec->status == XML_REGEXP_OK) && (exec->state != NULL) && |
3341 | 0 | ((exec->inputString[exec->index] != 0) || |
3342 | 0 | ((exec->state != NULL) && |
3343 | 0 | (exec->state->type != XML_REGEXP_FINAL_STATE)))) { |
3344 | 0 | xmlRegTransPtr trans; |
3345 | 0 | xmlRegAtomPtr atom; |
3346 | | |
3347 | | /* |
3348 | | * If end of input on non-terminal state, rollback, however we may |
3349 | | * still have epsilon like transition for counted transitions |
3350 | | * on counters, in that case don't break too early. Additionally, |
3351 | | * if we are working on a range like "AB{0,2}", where B is not present, |
3352 | | * we don't want to break. |
3353 | | */ |
3354 | 0 | len = 1; |
3355 | 0 | if ((exec->inputString[exec->index] == 0) && (exec->counts == NULL)) { |
3356 | | /* |
3357 | | * if there is a transition, we must check if |
3358 | | * atom allows minOccurs of 0 |
3359 | | */ |
3360 | 0 | if (exec->transno < exec->state->nbTrans) { |
3361 | 0 | trans = &exec->state->trans[exec->transno]; |
3362 | 0 | if (trans->to >=0) { |
3363 | 0 | atom = trans->atom; |
3364 | 0 | if (!((atom->min == 0) && (atom->max > 0))) |
3365 | 0 | goto rollback; |
3366 | 0 | } |
3367 | 0 | } else |
3368 | 0 | goto rollback; |
3369 | 0 | } |
3370 | | |
3371 | 0 | exec->transcount = 0; |
3372 | 0 | for (;exec->transno < exec->state->nbTrans;exec->transno++) { |
3373 | 0 | trans = &exec->state->trans[exec->transno]; |
3374 | 0 | if (trans->to < 0) |
3375 | 0 | continue; |
3376 | 0 | atom = trans->atom; |
3377 | 0 | ret = 0; |
3378 | 0 | deter = 1; |
3379 | 0 | if (trans->count >= 0) { |
3380 | 0 | int count; |
3381 | 0 | xmlRegCounterPtr counter; |
3382 | |
|
3383 | 0 | if (exec->counts == NULL) { |
3384 | 0 | exec->status = XML_REGEXP_INTERNAL_ERROR; |
3385 | 0 | goto error; |
3386 | 0 | } |
3387 | | /* |
3388 | | * A counted transition. |
3389 | | */ |
3390 | | |
3391 | 0 | count = exec->counts[trans->count]; |
3392 | 0 | counter = &exec->comp->counters[trans->count]; |
3393 | 0 | ret = ((count >= counter->min) && (count <= counter->max)); |
3394 | 0 | if ((ret) && (counter->min != counter->max)) |
3395 | 0 | deter = 0; |
3396 | 0 | } else if (atom == NULL) { |
3397 | 0 | exec->status = XML_REGEXP_INTERNAL_ERROR; |
3398 | 0 | break; |
3399 | 0 | } else if (exec->inputString[exec->index] != 0) { |
3400 | 0 | len = 4; |
3401 | 0 | codepoint = xmlGetUTF8Char(&exec->inputString[exec->index], |
3402 | 0 | &len); |
3403 | 0 | if (codepoint < 0) { |
3404 | 0 | exec->status = XML_REGEXP_INVALID_UTF8; |
3405 | 0 | goto error; |
3406 | 0 | } |
3407 | 0 | ret = xmlRegCheckCharacter(atom, codepoint); |
3408 | 0 | if ((ret == 1) && (atom->min >= 0) && (atom->max > 0)) { |
3409 | 0 | xmlRegStatePtr to = comp->states[trans->to]; |
3410 | | |
3411 | | /* |
3412 | | * this is a multiple input sequence |
3413 | | * If there is a counter associated increment it now. |
3414 | | * do not increment if the counter is already over the |
3415 | | * maximum limit in which case get to next transition |
3416 | | */ |
3417 | 0 | if (trans->counter >= 0) { |
3418 | 0 | xmlRegCounterPtr counter; |
3419 | |
|
3420 | 0 | if ((exec->counts == NULL) || |
3421 | 0 | (exec->comp == NULL) || |
3422 | 0 | (exec->comp->counters == NULL)) { |
3423 | 0 | exec->status = XML_REGEXP_INTERNAL_ERROR; |
3424 | 0 | goto error; |
3425 | 0 | } |
3426 | 0 | counter = &exec->comp->counters[trans->counter]; |
3427 | 0 | if (exec->counts[trans->counter] >= counter->max) |
3428 | 0 | continue; /* for loop on transitions */ |
3429 | 0 | } |
3430 | | /* Save before incrementing */ |
3431 | 0 | if (exec->state->nbTrans > exec->transno + 1) { |
3432 | 0 | xmlFARegExecSave(exec); |
3433 | 0 | if (exec->status != XML_REGEXP_OK) |
3434 | 0 | goto error; |
3435 | 0 | } |
3436 | 0 | if (trans->counter >= 0) { |
3437 | 0 | exec->counts[trans->counter]++; |
3438 | 0 | } |
3439 | 0 | exec->transcount = 1; |
3440 | 0 | do { |
3441 | | /* |
3442 | | * Try to progress as much as possible on the input |
3443 | | */ |
3444 | 0 | if (exec->transcount == atom->max) { |
3445 | 0 | break; |
3446 | 0 | } |
3447 | 0 | exec->index += len; |
3448 | | /* |
3449 | | * End of input: stop here |
3450 | | */ |
3451 | 0 | if (exec->inputString[exec->index] == 0) { |
3452 | 0 | exec->index -= len; |
3453 | 0 | break; |
3454 | 0 | } |
3455 | 0 | if (exec->transcount >= atom->min) { |
3456 | 0 | int transno = exec->transno; |
3457 | 0 | xmlRegStatePtr state = exec->state; |
3458 | | |
3459 | | /* |
3460 | | * The transition is acceptable save it |
3461 | | */ |
3462 | 0 | exec->transno = -1; /* trick */ |
3463 | 0 | exec->state = to; |
3464 | 0 | xmlFARegExecSave(exec); |
3465 | 0 | if (exec->status != XML_REGEXP_OK) |
3466 | 0 | goto error; |
3467 | 0 | exec->transno = transno; |
3468 | 0 | exec->state = state; |
3469 | 0 | } |
3470 | 0 | len = 4; |
3471 | 0 | codepoint = xmlGetUTF8Char( |
3472 | 0 | &exec->inputString[exec->index], &len); |
3473 | 0 | if (codepoint < 0) { |
3474 | 0 | exec->status = XML_REGEXP_INVALID_UTF8; |
3475 | 0 | goto error; |
3476 | 0 | } |
3477 | 0 | ret = xmlRegCheckCharacter(atom, codepoint); |
3478 | 0 | exec->transcount++; |
3479 | 0 | } while (ret == 1); |
3480 | 0 | if (exec->transcount < atom->min) |
3481 | 0 | ret = 0; |
3482 | | |
3483 | | /* |
3484 | | * If the last check failed but one transition was found |
3485 | | * possible, rollback |
3486 | | */ |
3487 | 0 | if (ret < 0) |
3488 | 0 | ret = 0; |
3489 | 0 | if (ret == 0) { |
3490 | 0 | goto rollback; |
3491 | 0 | } |
3492 | 0 | if (trans->counter >= 0) { |
3493 | 0 | if (exec->counts == NULL) { |
3494 | 0 | exec->status = XML_REGEXP_INTERNAL_ERROR; |
3495 | 0 | goto error; |
3496 | 0 | } |
3497 | 0 | exec->counts[trans->counter]--; |
3498 | 0 | } |
3499 | 0 | } else if ((ret == 0) && (atom->min == 0) && (atom->max > 0)) { |
3500 | | /* |
3501 | | * we don't match on the codepoint, but minOccurs of 0 |
3502 | | * says that's ok. Setting len to 0 inhibits stepping |
3503 | | * over the codepoint. |
3504 | | */ |
3505 | 0 | exec->transcount = 1; |
3506 | 0 | len = 0; |
3507 | 0 | ret = 1; |
3508 | 0 | } |
3509 | 0 | } else if ((atom->min == 0) && (atom->max > 0)) { |
3510 | | /* another spot to match when minOccurs is 0 */ |
3511 | 0 | exec->transcount = 1; |
3512 | 0 | len = 0; |
3513 | 0 | ret = 1; |
3514 | 0 | } |
3515 | 0 | if (ret == 1) { |
3516 | 0 | if ((trans->nd == 1) || |
3517 | 0 | ((trans->count >= 0) && (deter == 0) && |
3518 | 0 | (exec->state->nbTrans > exec->transno + 1))) { |
3519 | 0 | xmlFARegExecSave(exec); |
3520 | 0 | if (exec->status != XML_REGEXP_OK) |
3521 | 0 | goto error; |
3522 | 0 | } |
3523 | 0 | if (trans->counter >= 0) { |
3524 | 0 | xmlRegCounterPtr counter; |
3525 | | |
3526 | | /* make sure we don't go over the counter maximum value */ |
3527 | 0 | if ((exec->counts == NULL) || |
3528 | 0 | (exec->comp == NULL) || |
3529 | 0 | (exec->comp->counters == NULL)) { |
3530 | 0 | exec->status = XML_REGEXP_INTERNAL_ERROR; |
3531 | 0 | goto error; |
3532 | 0 | } |
3533 | 0 | counter = &exec->comp->counters[trans->counter]; |
3534 | 0 | if (exec->counts[trans->counter] >= counter->max) |
3535 | 0 | continue; /* for loop on transitions */ |
3536 | 0 | exec->counts[trans->counter]++; |
3537 | 0 | } |
3538 | 0 | if ((trans->count >= 0) && |
3539 | 0 | (trans->count < REGEXP_ALL_COUNTER)) { |
3540 | 0 | if (exec->counts == NULL) { |
3541 | 0 | exec->status = XML_REGEXP_INTERNAL_ERROR; |
3542 | 0 | goto error; |
3543 | 0 | } |
3544 | 0 | exec->counts[trans->count] = 0; |
3545 | 0 | } |
3546 | 0 | exec->state = comp->states[trans->to]; |
3547 | 0 | exec->transno = 0; |
3548 | 0 | if (trans->atom != NULL) { |
3549 | 0 | exec->index += len; |
3550 | 0 | } |
3551 | 0 | goto progress; |
3552 | 0 | } else if (ret < 0) { |
3553 | 0 | exec->status = XML_REGEXP_INTERNAL_ERROR; |
3554 | 0 | break; |
3555 | 0 | } |
3556 | 0 | } |
3557 | 0 | if ((exec->transno != 0) || (exec->state->nbTrans == 0)) { |
3558 | 0 | rollback: |
3559 | | /* |
3560 | | * Failed to find a way out |
3561 | | */ |
3562 | 0 | exec->determinist = 0; |
3563 | 0 | xmlFARegExecRollBack(exec); |
3564 | 0 | } |
3565 | 0 | progress: |
3566 | 0 | continue; |
3567 | 0 | } |
3568 | 0 | error: |
3569 | 0 | if (exec->rollbacks != NULL) { |
3570 | 0 | if (exec->counts != NULL) { |
3571 | 0 | int i; |
3572 | |
|
3573 | 0 | for (i = 0;i < exec->maxRollbacks;i++) |
3574 | 0 | if (exec->rollbacks[i].counts != NULL) |
3575 | 0 | xmlFree(exec->rollbacks[i].counts); |
3576 | 0 | } |
3577 | 0 | xmlFree(exec->rollbacks); |
3578 | 0 | } |
3579 | 0 | if (exec->state == NULL) |
3580 | 0 | return(XML_REGEXP_INTERNAL_ERROR); |
3581 | 0 | if (exec->counts != NULL) |
3582 | 0 | xmlFree(exec->counts); |
3583 | 0 | if (exec->status == XML_REGEXP_OK) |
3584 | 0 | return(1); |
3585 | 0 | if (exec->status == XML_REGEXP_NOT_FOUND) |
3586 | 0 | return(0); |
3587 | 0 | return(exec->status); |
3588 | 0 | } |
3589 | | |
3590 | | /************************************************************************ |
3591 | | * * |
3592 | | * Progressive interface to the verifier one atom at a time * |
3593 | | * * |
3594 | | ************************************************************************/ |
3595 | | |
3596 | | /** |
3597 | | * Build a context used for progressive evaluation of a regexp. |
3598 | | * |
3599 | | * @deprecated Internal function, don't use. |
3600 | | * |
3601 | | * @param comp a precompiled regular expression |
3602 | | * @param callback a callback function used for handling progresses in the |
3603 | | * automata matching phase |
3604 | | * @param data the context data associated to the callback in this context |
3605 | | * @returns the new context |
3606 | | */ |
3607 | | xmlRegExecCtxt * |
3608 | 0 | xmlRegNewExecCtxt(xmlRegexp *comp, xmlRegExecCallbacks callback, void *data) { |
3609 | 0 | xmlRegExecCtxtPtr exec; |
3610 | |
|
3611 | 0 | if (comp == NULL) |
3612 | 0 | return(NULL); |
3613 | 0 | if ((comp->compact == NULL) && (comp->states == NULL)) |
3614 | 0 | return(NULL); |
3615 | 0 | exec = (xmlRegExecCtxtPtr) xmlMalloc(sizeof(xmlRegExecCtxt)); |
3616 | 0 | if (exec == NULL) |
3617 | 0 | return(NULL); |
3618 | 0 | memset(exec, 0, sizeof(xmlRegExecCtxt)); |
3619 | 0 | exec->inputString = NULL; |
3620 | 0 | exec->index = 0; |
3621 | 0 | exec->determinist = 1; |
3622 | 0 | exec->maxRollbacks = 0; |
3623 | 0 | exec->nbRollbacks = 0; |
3624 | 0 | exec->rollbacks = NULL; |
3625 | 0 | exec->status = XML_REGEXP_OK; |
3626 | 0 | exec->comp = comp; |
3627 | 0 | if (comp->compact == NULL) |
3628 | 0 | exec->state = comp->states[0]; |
3629 | 0 | exec->transno = 0; |
3630 | 0 | exec->transcount = 0; |
3631 | 0 | exec->callback = callback; |
3632 | 0 | exec->data = data; |
3633 | 0 | if (comp->nbCounters > 0) { |
3634 | | /* |
3635 | | * For error handling, exec->counts is allocated twice the size |
3636 | | * the second half is used to store the data in case of rollback |
3637 | | */ |
3638 | 0 | exec->counts = (int *) xmlMalloc(comp->nbCounters * sizeof(int) |
3639 | 0 | * 2); |
3640 | 0 | if (exec->counts == NULL) { |
3641 | 0 | xmlFree(exec); |
3642 | 0 | return(NULL); |
3643 | 0 | } |
3644 | 0 | memset(exec->counts, 0, comp->nbCounters * sizeof(int) * 2); |
3645 | 0 | exec->errCounts = &exec->counts[comp->nbCounters]; |
3646 | 0 | } else { |
3647 | 0 | exec->counts = NULL; |
3648 | 0 | exec->errCounts = NULL; |
3649 | 0 | } |
3650 | 0 | exec->inputStackMax = 0; |
3651 | 0 | exec->inputStackNr = 0; |
3652 | 0 | exec->inputStack = NULL; |
3653 | 0 | exec->errStateNo = -1; |
3654 | 0 | exec->errString = NULL; |
3655 | 0 | exec->nbPush = 0; |
3656 | 0 | return(exec); |
3657 | 0 | } |
3658 | | |
3659 | | /** |
3660 | | * Free the structures associated to a regular expression evaluation context. |
3661 | | * |
3662 | | * @deprecated Internal function, don't use. |
3663 | | * |
3664 | | * @param exec a regular expression evaluation context |
3665 | | */ |
3666 | | void |
3667 | 0 | xmlRegFreeExecCtxt(xmlRegExecCtxt *exec) { |
3668 | 0 | if (exec == NULL) |
3669 | 0 | return; |
3670 | | |
3671 | 0 | if (exec->rollbacks != NULL) { |
3672 | 0 | if (exec->counts != NULL) { |
3673 | 0 | int i; |
3674 | |
|
3675 | 0 | for (i = 0;i < exec->maxRollbacks;i++) |
3676 | 0 | if (exec->rollbacks[i].counts != NULL) |
3677 | 0 | xmlFree(exec->rollbacks[i].counts); |
3678 | 0 | } |
3679 | 0 | xmlFree(exec->rollbacks); |
3680 | 0 | } |
3681 | 0 | if (exec->counts != NULL) |
3682 | 0 | xmlFree(exec->counts); |
3683 | 0 | if (exec->inputStack != NULL) { |
3684 | 0 | int i; |
3685 | |
|
3686 | 0 | for (i = 0;i < exec->inputStackNr;i++) { |
3687 | 0 | if (exec->inputStack[i].value != NULL) |
3688 | 0 | xmlFree(exec->inputStack[i].value); |
3689 | 0 | } |
3690 | 0 | xmlFree(exec->inputStack); |
3691 | 0 | } |
3692 | 0 | if (exec->errString != NULL) |
3693 | 0 | xmlFree(exec->errString); |
3694 | 0 | xmlFree(exec); |
3695 | 0 | } |
3696 | | |
3697 | | static int |
3698 | 0 | xmlRegExecSetErrString(xmlRegExecCtxtPtr exec, const xmlChar *value) { |
3699 | 0 | if (exec->errString != NULL) |
3700 | 0 | xmlFree(exec->errString); |
3701 | 0 | if (value == NULL) { |
3702 | 0 | exec->errString = NULL; |
3703 | 0 | } else { |
3704 | 0 | exec->errString = xmlStrdup(value); |
3705 | 0 | if (exec->errString == NULL) { |
3706 | 0 | exec->status = XML_REGEXP_OUT_OF_MEMORY; |
3707 | 0 | return(-1); |
3708 | 0 | } |
3709 | 0 | } |
3710 | 0 | return(0); |
3711 | 0 | } |
3712 | | |
3713 | | static void |
3714 | | xmlFARegExecSaveInputString(xmlRegExecCtxtPtr exec, const xmlChar *value, |
3715 | 0 | void *data) { |
3716 | 0 | if (exec->inputStackNr + 1 >= exec->inputStackMax) { |
3717 | 0 | xmlRegInputTokenPtr tmp; |
3718 | 0 | int newSize; |
3719 | |
|
3720 | 0 | newSize = xmlGrowCapacity(exec->inputStackMax, sizeof(tmp[0]), |
3721 | 0 | 4, XML_MAX_ITEMS); |
3722 | 0 | if (newSize < 0) { |
3723 | 0 | exec->status = XML_REGEXP_OUT_OF_MEMORY; |
3724 | 0 | return; |
3725 | 0 | } |
3726 | 0 | #ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION |
3727 | 0 | if (newSize < 2) |
3728 | 0 | newSize = 2; |
3729 | 0 | #endif |
3730 | 0 | tmp = xmlRealloc(exec->inputStack, newSize * sizeof(tmp[0])); |
3731 | 0 | if (tmp == NULL) { |
3732 | 0 | exec->status = XML_REGEXP_OUT_OF_MEMORY; |
3733 | 0 | return; |
3734 | 0 | } |
3735 | 0 | exec->inputStack = tmp; |
3736 | 0 | exec->inputStackMax = newSize; |
3737 | 0 | } |
3738 | 0 | if (value == NULL) { |
3739 | 0 | exec->inputStack[exec->inputStackNr].value = NULL; |
3740 | 0 | } else { |
3741 | 0 | exec->inputStack[exec->inputStackNr].value = xmlStrdup(value); |
3742 | 0 | if (exec->inputStack[exec->inputStackNr].value == NULL) { |
3743 | 0 | exec->status = XML_REGEXP_OUT_OF_MEMORY; |
3744 | 0 | return; |
3745 | 0 | } |
3746 | 0 | } |
3747 | 0 | exec->inputStack[exec->inputStackNr].data = data; |
3748 | 0 | exec->inputStackNr++; |
3749 | 0 | exec->inputStack[exec->inputStackNr].value = NULL; |
3750 | 0 | exec->inputStack[exec->inputStackNr].data = NULL; |
3751 | 0 | } |
3752 | | |
3753 | | /** |
3754 | | * Checks if both strings are equal or have the same content. "*" |
3755 | | * can be used as a wildcard in `valStr`; "|" is used as a separator of |
3756 | | * substrings in both `expStr` and `valStr`. |
3757 | | * |
3758 | | * @param expStr the string to be evaluated |
3759 | | * @param valStr the validation string |
3760 | | * @returns 1 if the comparison is satisfied and the number of substrings |
3761 | | * is equal, 0 otherwise. |
3762 | | */ |
3763 | | |
3764 | | static int |
3765 | 0 | xmlRegStrEqualWildcard(const xmlChar *expStr, const xmlChar *valStr) { |
3766 | 0 | if (expStr == valStr) return(1); |
3767 | 0 | if (expStr == NULL) return(0); |
3768 | 0 | if (valStr == NULL) return(0); |
3769 | 0 | do { |
3770 | | /* |
3771 | | * Eval if we have a wildcard for the current item. |
3772 | | */ |
3773 | 0 | if (*expStr != *valStr) { |
3774 | | /* if one of them starts with a wildcard make valStr be it */ |
3775 | 0 | if (*valStr == '*') { |
3776 | 0 | const xmlChar *tmp; |
3777 | |
|
3778 | 0 | tmp = valStr; |
3779 | 0 | valStr = expStr; |
3780 | 0 | expStr = tmp; |
3781 | 0 | } |
3782 | 0 | if ((*valStr != 0) && (*expStr != 0) && (*expStr++ == '*')) { |
3783 | 0 | do { |
3784 | 0 | if (*valStr == XML_REG_STRING_SEPARATOR) |
3785 | 0 | break; |
3786 | 0 | valStr++; |
3787 | 0 | } while (*valStr != 0); |
3788 | 0 | continue; |
3789 | 0 | } else |
3790 | 0 | return(0); |
3791 | 0 | } |
3792 | 0 | expStr++; |
3793 | 0 | valStr++; |
3794 | 0 | } while (*valStr != 0); |
3795 | 0 | if (*expStr != 0) |
3796 | 0 | return (0); |
3797 | 0 | else |
3798 | 0 | return (1); |
3799 | 0 | } |
3800 | | |
3801 | | /** |
3802 | | * Push one input token in the execution context |
3803 | | * |
3804 | | * @param exec a regexp execution context |
3805 | | * @param comp the precompiled exec with a compact table |
3806 | | * @param value a string token input |
3807 | | * @param data data associated to the token to reuse in callbacks |
3808 | | * @returns 1 if the regexp reached a final state, 0 if non-final, and |
3809 | | * a negative value in case of error. |
3810 | | */ |
3811 | | static int |
3812 | | xmlRegCompactPushString(xmlRegExecCtxtPtr exec, |
3813 | | xmlRegexpPtr comp, |
3814 | | const xmlChar *value, |
3815 | 0 | void *data) { |
3816 | 0 | int state = exec->index; |
3817 | 0 | int i, target; |
3818 | |
|
3819 | 0 | if ((comp == NULL) || (comp->compact == NULL) || (comp->stringMap == NULL)) |
3820 | 0 | return(-1); |
3821 | | |
3822 | 0 | if (value == NULL) { |
3823 | | /* |
3824 | | * are we at a final state ? |
3825 | | */ |
3826 | 0 | if (comp->compact[state * (comp->nbstrings + 1)] == |
3827 | 0 | XML_REGEXP_FINAL_STATE) |
3828 | 0 | return(1); |
3829 | 0 | return(0); |
3830 | 0 | } |
3831 | | |
3832 | | /* |
3833 | | * Examine all outside transitions from current state |
3834 | | */ |
3835 | 0 | for (i = 0;i < comp->nbstrings;i++) { |
3836 | 0 | target = comp->compact[state * (comp->nbstrings + 1) + i + 1]; |
3837 | 0 | if ((target > 0) && (target <= comp->nbstates)) { |
3838 | 0 | target--; /* to avoid 0 */ |
3839 | 0 | if (xmlRegStrEqualWildcard(comp->stringMap[i], value)) { |
3840 | 0 | exec->index = target; |
3841 | 0 | if ((exec->callback != NULL) && (comp->transdata != NULL)) { |
3842 | 0 | exec->callback(exec->data, value, |
3843 | 0 | comp->transdata[state * comp->nbstrings + i], data); |
3844 | 0 | } |
3845 | 0 | if (comp->compact[target * (comp->nbstrings + 1)] == |
3846 | 0 | XML_REGEXP_SINK_STATE) |
3847 | 0 | goto error; |
3848 | | |
3849 | 0 | if (comp->compact[target * (comp->nbstrings + 1)] == |
3850 | 0 | XML_REGEXP_FINAL_STATE) |
3851 | 0 | return(1); |
3852 | 0 | return(0); |
3853 | 0 | } |
3854 | 0 | } |
3855 | 0 | } |
3856 | | /* |
3857 | | * Failed to find an exit transition out from current state for the |
3858 | | * current token |
3859 | | */ |
3860 | 0 | error: |
3861 | 0 | exec->errStateNo = state; |
3862 | 0 | exec->status = XML_REGEXP_NOT_FOUND; |
3863 | 0 | xmlRegExecSetErrString(exec, value); |
3864 | 0 | return(exec->status); |
3865 | 0 | } |
3866 | | |
3867 | | /** |
3868 | | * Push one input token in the execution context |
3869 | | * |
3870 | | * @param exec a regexp execution context or NULL to indicate the end |
3871 | | * @param value a string token input |
3872 | | * @param data data associated to the token to reuse in callbacks |
3873 | | * @param compound value was assembled from 2 strings |
3874 | | * @returns 1 if the regexp reached a final state, 0 if non-final, and |
3875 | | * a negative value in case of error. |
3876 | | */ |
3877 | | static int |
3878 | | xmlRegExecPushStringInternal(xmlRegExecCtxtPtr exec, const xmlChar *value, |
3879 | 0 | void *data, int compound) { |
3880 | 0 | xmlRegTransPtr trans; |
3881 | 0 | xmlRegAtomPtr atom; |
3882 | 0 | int ret; |
3883 | 0 | int final = 0; |
3884 | 0 | int progress = 1; |
3885 | |
|
3886 | 0 | if (exec == NULL) |
3887 | 0 | return(-1); |
3888 | 0 | if (exec->comp == NULL) |
3889 | 0 | return(-1); |
3890 | 0 | if (exec->status != XML_REGEXP_OK) |
3891 | 0 | return(exec->status); |
3892 | | |
3893 | 0 | if (exec->comp->compact != NULL) |
3894 | 0 | return(xmlRegCompactPushString(exec, exec->comp, value, data)); |
3895 | | |
3896 | 0 | if (value == NULL) { |
3897 | 0 | if (exec->state->type == XML_REGEXP_FINAL_STATE) |
3898 | 0 | return(1); |
3899 | 0 | final = 1; |
3900 | 0 | } |
3901 | | |
3902 | | /* |
3903 | | * If we have an active rollback stack push the new value there |
3904 | | * and get back to where we were left |
3905 | | */ |
3906 | 0 | if ((value != NULL) && (exec->inputStackNr > 0)) { |
3907 | 0 | xmlFARegExecSaveInputString(exec, value, data); |
3908 | 0 | value = exec->inputStack[exec->index].value; |
3909 | 0 | data = exec->inputStack[exec->index].data; |
3910 | 0 | } |
3911 | |
|
3912 | 0 | while ((exec->status == XML_REGEXP_OK) && |
3913 | 0 | ((value != NULL) || |
3914 | 0 | ((final == 1) && |
3915 | 0 | (exec->state->type != XML_REGEXP_FINAL_STATE)))) { |
3916 | | |
3917 | | /* |
3918 | | * End of input on non-terminal state, rollback, however we may |
3919 | | * still have epsilon like transition for counted transitions |
3920 | | * on counters, in that case don't break too early. |
3921 | | */ |
3922 | 0 | if ((value == NULL) && (exec->counts == NULL)) |
3923 | 0 | goto rollback; |
3924 | | |
3925 | 0 | exec->transcount = 0; |
3926 | 0 | for (;exec->transno < exec->state->nbTrans;exec->transno++) { |
3927 | 0 | trans = &exec->state->trans[exec->transno]; |
3928 | 0 | if (trans->to < 0) |
3929 | 0 | continue; |
3930 | 0 | atom = trans->atom; |
3931 | 0 | ret = 0; |
3932 | 0 | if (trans->count == REGEXP_ALL_LAX_COUNTER) { |
3933 | 0 | int i; |
3934 | 0 | int count; |
3935 | 0 | xmlRegTransPtr t; |
3936 | 0 | xmlRegCounterPtr counter; |
3937 | |
|
3938 | 0 | ret = 0; |
3939 | | |
3940 | | /* |
3941 | | * Check all counted transitions from the current state |
3942 | | */ |
3943 | 0 | if ((value == NULL) && (final)) { |
3944 | 0 | ret = 1; |
3945 | 0 | } else if (value != NULL) { |
3946 | 0 | for (i = 0;i < exec->state->nbTrans;i++) { |
3947 | 0 | t = &exec->state->trans[i]; |
3948 | 0 | if ((t->counter < 0) || (t == trans)) |
3949 | 0 | continue; |
3950 | 0 | counter = &exec->comp->counters[t->counter]; |
3951 | 0 | count = exec->counts[t->counter]; |
3952 | 0 | if ((count < counter->max) && |
3953 | 0 | (t->atom != NULL) && |
3954 | 0 | (xmlStrEqual(value, t->atom->valuep))) { |
3955 | 0 | ret = 0; |
3956 | 0 | break; |
3957 | 0 | } |
3958 | 0 | if ((count >= counter->min) && |
3959 | 0 | (count < counter->max) && |
3960 | 0 | (t->atom != NULL) && |
3961 | 0 | (xmlStrEqual(value, t->atom->valuep))) { |
3962 | 0 | ret = 1; |
3963 | 0 | break; |
3964 | 0 | } |
3965 | 0 | } |
3966 | 0 | } |
3967 | 0 | } else if (trans->count == REGEXP_ALL_COUNTER) { |
3968 | 0 | int i; |
3969 | 0 | int count; |
3970 | 0 | xmlRegTransPtr t; |
3971 | 0 | xmlRegCounterPtr counter; |
3972 | |
|
3973 | 0 | ret = 1; |
3974 | | |
3975 | | /* |
3976 | | * Check all counted transitions from the current state |
3977 | | */ |
3978 | 0 | for (i = 0;i < exec->state->nbTrans;i++) { |
3979 | 0 | t = &exec->state->trans[i]; |
3980 | 0 | if ((t->counter < 0) || (t == trans)) |
3981 | 0 | continue; |
3982 | 0 | counter = &exec->comp->counters[t->counter]; |
3983 | 0 | count = exec->counts[t->counter]; |
3984 | 0 | if ((count < counter->min) || (count > counter->max)) { |
3985 | 0 | ret = 0; |
3986 | 0 | break; |
3987 | 0 | } |
3988 | 0 | } |
3989 | 0 | } else if (trans->count >= 0) { |
3990 | 0 | int count; |
3991 | 0 | xmlRegCounterPtr counter; |
3992 | | |
3993 | | /* |
3994 | | * A counted transition. |
3995 | | */ |
3996 | |
|
3997 | 0 | count = exec->counts[trans->count]; |
3998 | 0 | counter = &exec->comp->counters[trans->count]; |
3999 | 0 | ret = ((count >= counter->min) && (count <= counter->max)); |
4000 | 0 | } else if (atom == NULL) { |
4001 | 0 | exec->status = XML_REGEXP_INTERNAL_ERROR; |
4002 | 0 | break; |
4003 | 0 | } else if (value != NULL) { |
4004 | 0 | ret = xmlRegStrEqualWildcard(atom->valuep, value); |
4005 | 0 | if (atom->neg) { |
4006 | 0 | ret = !ret; |
4007 | 0 | if (!compound) |
4008 | 0 | ret = 0; |
4009 | 0 | } |
4010 | 0 | if ((ret == 1) && (trans->counter >= 0)) { |
4011 | 0 | xmlRegCounterPtr counter; |
4012 | 0 | int count; |
4013 | |
|
4014 | 0 | count = exec->counts[trans->counter]; |
4015 | 0 | counter = &exec->comp->counters[trans->counter]; |
4016 | 0 | if (count >= counter->max) |
4017 | 0 | ret = 0; |
4018 | 0 | } |
4019 | |
|
4020 | 0 | if ((ret == 1) && (atom->min > 0) && (atom->max > 0)) { |
4021 | 0 | xmlRegStatePtr to = exec->comp->states[trans->to]; |
4022 | | |
4023 | | /* |
4024 | | * this is a multiple input sequence |
4025 | | */ |
4026 | 0 | if (exec->state->nbTrans > exec->transno + 1) { |
4027 | 0 | if (exec->inputStackNr <= 0) { |
4028 | 0 | xmlFARegExecSaveInputString(exec, value, data); |
4029 | 0 | } |
4030 | 0 | xmlFARegExecSave(exec); |
4031 | 0 | } |
4032 | 0 | exec->transcount = 1; |
4033 | 0 | do { |
4034 | | /* |
4035 | | * Try to progress as much as possible on the input |
4036 | | */ |
4037 | 0 | if (exec->transcount == atom->max) { |
4038 | 0 | break; |
4039 | 0 | } |
4040 | 0 | exec->index++; |
4041 | 0 | value = exec->inputStack[exec->index].value; |
4042 | 0 | data = exec->inputStack[exec->index].data; |
4043 | | |
4044 | | /* |
4045 | | * End of input: stop here |
4046 | | */ |
4047 | 0 | if (value == NULL) { |
4048 | 0 | exec->index --; |
4049 | 0 | break; |
4050 | 0 | } |
4051 | 0 | if (exec->transcount >= atom->min) { |
4052 | 0 | int transno = exec->transno; |
4053 | 0 | xmlRegStatePtr state = exec->state; |
4054 | | |
4055 | | /* |
4056 | | * The transition is acceptable save it |
4057 | | */ |
4058 | 0 | exec->transno = -1; /* trick */ |
4059 | 0 | exec->state = to; |
4060 | 0 | if (exec->inputStackNr <= 0) { |
4061 | 0 | xmlFARegExecSaveInputString(exec, value, data); |
4062 | 0 | } |
4063 | 0 | xmlFARegExecSave(exec); |
4064 | 0 | exec->transno = transno; |
4065 | 0 | exec->state = state; |
4066 | 0 | } |
4067 | 0 | ret = xmlStrEqual(value, atom->valuep); |
4068 | 0 | exec->transcount++; |
4069 | 0 | } while (ret == 1); |
4070 | 0 | if (exec->transcount < atom->min) |
4071 | 0 | ret = 0; |
4072 | | |
4073 | | /* |
4074 | | * If the last check failed but one transition was found |
4075 | | * possible, rollback |
4076 | | */ |
4077 | 0 | if (ret < 0) |
4078 | 0 | ret = 0; |
4079 | 0 | if (ret == 0) { |
4080 | 0 | goto rollback; |
4081 | 0 | } |
4082 | 0 | } |
4083 | 0 | } |
4084 | 0 | if (ret == 1) { |
4085 | 0 | if ((exec->callback != NULL) && (atom != NULL) && |
4086 | 0 | (data != NULL)) { |
4087 | 0 | exec->callback(exec->data, atom->valuep, |
4088 | 0 | atom->data, data); |
4089 | 0 | } |
4090 | 0 | if (exec->state->nbTrans > exec->transno + 1) { |
4091 | 0 | if (exec->inputStackNr <= 0) { |
4092 | 0 | xmlFARegExecSaveInputString(exec, value, data); |
4093 | 0 | } |
4094 | 0 | xmlFARegExecSave(exec); |
4095 | 0 | } |
4096 | 0 | if (trans->counter >= 0) { |
4097 | 0 | exec->counts[trans->counter]++; |
4098 | 0 | } |
4099 | 0 | if ((trans->count >= 0) && |
4100 | 0 | (trans->count < REGEXP_ALL_COUNTER)) { |
4101 | 0 | exec->counts[trans->count] = 0; |
4102 | 0 | } |
4103 | 0 | if ((exec->comp->states[trans->to] != NULL) && |
4104 | 0 | (exec->comp->states[trans->to]->type == |
4105 | 0 | XML_REGEXP_SINK_STATE)) { |
4106 | | /* |
4107 | | * entering a sink state, save the current state as error |
4108 | | * state. |
4109 | | */ |
4110 | 0 | if (xmlRegExecSetErrString(exec, value) < 0) |
4111 | 0 | break; |
4112 | 0 | exec->errState = exec->state; |
4113 | 0 | memcpy(exec->errCounts, exec->counts, |
4114 | 0 | exec->comp->nbCounters * sizeof(int)); |
4115 | 0 | } |
4116 | 0 | exec->state = exec->comp->states[trans->to]; |
4117 | 0 | exec->transno = 0; |
4118 | 0 | if (trans->atom != NULL) { |
4119 | 0 | if (exec->inputStack != NULL) { |
4120 | 0 | exec->index++; |
4121 | 0 | if (exec->index < exec->inputStackNr) { |
4122 | 0 | value = exec->inputStack[exec->index].value; |
4123 | 0 | data = exec->inputStack[exec->index].data; |
4124 | 0 | } else { |
4125 | 0 | value = NULL; |
4126 | 0 | data = NULL; |
4127 | 0 | } |
4128 | 0 | } else { |
4129 | 0 | value = NULL; |
4130 | 0 | data = NULL; |
4131 | 0 | } |
4132 | 0 | } |
4133 | 0 | goto progress; |
4134 | 0 | } else if (ret < 0) { |
4135 | 0 | exec->status = XML_REGEXP_INTERNAL_ERROR; |
4136 | 0 | break; |
4137 | 0 | } |
4138 | 0 | } |
4139 | 0 | if ((exec->transno != 0) || (exec->state->nbTrans == 0)) { |
4140 | 0 | rollback: |
4141 | | /* |
4142 | | * if we didn't yet rollback on the current input |
4143 | | * store the current state as the error state. |
4144 | | */ |
4145 | 0 | if ((progress) && (exec->state != NULL) && |
4146 | 0 | (exec->state->type != XML_REGEXP_SINK_STATE)) { |
4147 | 0 | progress = 0; |
4148 | 0 | if (xmlRegExecSetErrString(exec, value) < 0) |
4149 | 0 | break; |
4150 | 0 | exec->errState = exec->state; |
4151 | 0 | if (exec->comp->nbCounters) |
4152 | 0 | memcpy(exec->errCounts, exec->counts, |
4153 | 0 | exec->comp->nbCounters * sizeof(int)); |
4154 | 0 | } |
4155 | | |
4156 | | /* |
4157 | | * Failed to find a way out |
4158 | | */ |
4159 | 0 | exec->determinist = 0; |
4160 | 0 | xmlFARegExecRollBack(exec); |
4161 | 0 | if ((exec->inputStack != NULL ) && |
4162 | 0 | (exec->status == XML_REGEXP_OK)) { |
4163 | 0 | value = exec->inputStack[exec->index].value; |
4164 | 0 | data = exec->inputStack[exec->index].data; |
4165 | 0 | } |
4166 | 0 | } |
4167 | 0 | continue; |
4168 | 0 | progress: |
4169 | 0 | progress = 1; |
4170 | 0 | } |
4171 | 0 | if (exec->status == XML_REGEXP_OK) { |
4172 | 0 | return(exec->state->type == XML_REGEXP_FINAL_STATE); |
4173 | 0 | } |
4174 | 0 | return(exec->status); |
4175 | 0 | } |
4176 | | |
4177 | | /** |
4178 | | * Push one input token in the execution context |
4179 | | * |
4180 | | * @deprecated Internal function, don't use. |
4181 | | * |
4182 | | * @param exec a regexp execution context or NULL to indicate the end |
4183 | | * @param value a string token input |
4184 | | * @param data data associated to the token to reuse in callbacks |
4185 | | * @returns 1 if the regexp reached a final state, 0 if non-final, and |
4186 | | * a negative value in case of error. |
4187 | | */ |
4188 | | int |
4189 | | xmlRegExecPushString(xmlRegExecCtxt *exec, const xmlChar *value, |
4190 | 0 | void *data) { |
4191 | 0 | return(xmlRegExecPushStringInternal(exec, value, data, 0)); |
4192 | 0 | } |
4193 | | |
4194 | | /** |
4195 | | * Push one input token in the execution context |
4196 | | * |
4197 | | * @deprecated Internal function, don't use. |
4198 | | * |
4199 | | * @param exec a regexp execution context or NULL to indicate the end |
4200 | | * @param value the first string token input |
4201 | | * @param value2 the second string token input |
4202 | | * @param data data associated to the token to reuse in callbacks |
4203 | | * @returns 1 if the regexp reached a final state, 0 if non-final, and |
4204 | | * a negative value in case of error. |
4205 | | */ |
4206 | | int |
4207 | | xmlRegExecPushString2(xmlRegExecCtxt *exec, const xmlChar *value, |
4208 | 0 | const xmlChar *value2, void *data) { |
4209 | 0 | xmlChar buf[150]; |
4210 | 0 | int lenn, lenp, ret; |
4211 | 0 | xmlChar *str; |
4212 | |
|
4213 | 0 | if (exec == NULL) |
4214 | 0 | return(-1); |
4215 | 0 | if (exec->comp == NULL) |
4216 | 0 | return(-1); |
4217 | 0 | if (exec->status != XML_REGEXP_OK) |
4218 | 0 | return(exec->status); |
4219 | | |
4220 | 0 | if (value2 == NULL) |
4221 | 0 | return(xmlRegExecPushString(exec, value, data)); |
4222 | | |
4223 | 0 | lenn = strlen((char *) value2); |
4224 | 0 | lenp = strlen((char *) value); |
4225 | |
|
4226 | 0 | if (150 < lenn + lenp + 2) { |
4227 | 0 | str = xmlMalloc(lenn + lenp + 2); |
4228 | 0 | if (str == NULL) { |
4229 | 0 | exec->status = XML_REGEXP_OUT_OF_MEMORY; |
4230 | 0 | return(-1); |
4231 | 0 | } |
4232 | 0 | } else { |
4233 | 0 | str = buf; |
4234 | 0 | } |
4235 | 0 | memcpy(&str[0], value, lenp); |
4236 | 0 | str[lenp] = XML_REG_STRING_SEPARATOR; |
4237 | 0 | memcpy(&str[lenp + 1], value2, lenn); |
4238 | 0 | str[lenn + lenp + 1] = 0; |
4239 | |
|
4240 | 0 | if (exec->comp->compact != NULL) |
4241 | 0 | ret = xmlRegCompactPushString(exec, exec->comp, str, data); |
4242 | 0 | else |
4243 | 0 | ret = xmlRegExecPushStringInternal(exec, str, data, 1); |
4244 | |
|
4245 | 0 | if (str != buf) |
4246 | 0 | xmlFree(str); |
4247 | 0 | return(ret); |
4248 | 0 | } |
4249 | | |
4250 | | /** |
4251 | | * Extract information from the regexp execution. Internal routine to |
4252 | | * implement #xmlRegExecNextValues and #xmlRegExecErrInfo |
4253 | | * |
4254 | | * @param exec a regexp execution context |
4255 | | * @param err error extraction or normal one |
4256 | | * @param nbval pointer to the number of accepted values IN/OUT |
4257 | | * @param nbneg return number of negative transitions |
4258 | | * @param values pointer to the array of acceptable values |
4259 | | * @param terminal return value if this was a terminal state |
4260 | | * @returns 0 in case of success or -1 in case of error. |
4261 | | */ |
4262 | | static int |
4263 | | xmlRegExecGetValues(xmlRegExecCtxtPtr exec, int err, |
4264 | | int *nbval, int *nbneg, |
4265 | 0 | xmlChar **values, int *terminal) { |
4266 | 0 | int maxval; |
4267 | 0 | int nb = 0; |
4268 | |
|
4269 | 0 | if ((exec == NULL) || (nbval == NULL) || (nbneg == NULL) || |
4270 | 0 | (values == NULL) || (*nbval <= 0)) |
4271 | 0 | return(-1); |
4272 | | |
4273 | 0 | maxval = *nbval; |
4274 | 0 | *nbval = 0; |
4275 | 0 | *nbneg = 0; |
4276 | 0 | if ((exec->comp != NULL) && (exec->comp->compact != NULL)) { |
4277 | 0 | xmlRegexpPtr comp; |
4278 | 0 | int target, i, state; |
4279 | |
|
4280 | 0 | comp = exec->comp; |
4281 | |
|
4282 | 0 | if (err) { |
4283 | 0 | if (exec->errStateNo == -1) return(-1); |
4284 | 0 | state = exec->errStateNo; |
4285 | 0 | } else { |
4286 | 0 | state = exec->index; |
4287 | 0 | } |
4288 | 0 | if (terminal != NULL) { |
4289 | 0 | if (comp->compact[state * (comp->nbstrings + 1)] == |
4290 | 0 | XML_REGEXP_FINAL_STATE) |
4291 | 0 | *terminal = 1; |
4292 | 0 | else |
4293 | 0 | *terminal = 0; |
4294 | 0 | } |
4295 | 0 | for (i = 0;(i < comp->nbstrings) && (nb < maxval);i++) { |
4296 | 0 | target = comp->compact[state * (comp->nbstrings + 1) + i + 1]; |
4297 | 0 | if ((target > 0) && (target <= comp->nbstates) && |
4298 | 0 | (comp->compact[(target - 1) * (comp->nbstrings + 1)] != |
4299 | 0 | XML_REGEXP_SINK_STATE)) { |
4300 | 0 | values[nb++] = comp->stringMap[i]; |
4301 | 0 | (*nbval)++; |
4302 | 0 | } |
4303 | 0 | } |
4304 | 0 | for (i = 0;(i < comp->nbstrings) && (nb < maxval);i++) { |
4305 | 0 | target = comp->compact[state * (comp->nbstrings + 1) + i + 1]; |
4306 | 0 | if ((target > 0) && (target <= comp->nbstates) && |
4307 | 0 | (comp->compact[(target - 1) * (comp->nbstrings + 1)] == |
4308 | 0 | XML_REGEXP_SINK_STATE)) { |
4309 | 0 | values[nb++] = comp->stringMap[i]; |
4310 | 0 | (*nbneg)++; |
4311 | 0 | } |
4312 | 0 | } |
4313 | 0 | } else { |
4314 | 0 | int transno; |
4315 | 0 | xmlRegTransPtr trans; |
4316 | 0 | xmlRegAtomPtr atom; |
4317 | 0 | xmlRegStatePtr state; |
4318 | |
|
4319 | 0 | if (terminal != NULL) { |
4320 | 0 | if (exec->state->type == XML_REGEXP_FINAL_STATE) |
4321 | 0 | *terminal = 1; |
4322 | 0 | else |
4323 | 0 | *terminal = 0; |
4324 | 0 | } |
4325 | |
|
4326 | 0 | if (err) { |
4327 | 0 | if (exec->errState == NULL) return(-1); |
4328 | 0 | state = exec->errState; |
4329 | 0 | } else { |
4330 | 0 | if (exec->state == NULL) return(-1); |
4331 | 0 | state = exec->state; |
4332 | 0 | } |
4333 | 0 | for (transno = 0; |
4334 | 0 | (transno < state->nbTrans) && (nb < maxval); |
4335 | 0 | transno++) { |
4336 | 0 | trans = &state->trans[transno]; |
4337 | 0 | if (trans->to < 0) |
4338 | 0 | continue; |
4339 | 0 | atom = trans->atom; |
4340 | 0 | if ((atom == NULL) || (atom->valuep == NULL)) |
4341 | 0 | continue; |
4342 | 0 | if (trans->count == REGEXP_ALL_LAX_COUNTER) { |
4343 | | /* this should not be reached but ... */ |
4344 | 0 | } else if (trans->count == REGEXP_ALL_COUNTER) { |
4345 | | /* this should not be reached but ... */ |
4346 | 0 | } else if (trans->counter >= 0) { |
4347 | 0 | xmlRegCounterPtr counter = NULL; |
4348 | 0 | int count; |
4349 | |
|
4350 | 0 | if (err) |
4351 | 0 | count = exec->errCounts[trans->counter]; |
4352 | 0 | else |
4353 | 0 | count = exec->counts[trans->counter]; |
4354 | 0 | if (exec->comp != NULL) |
4355 | 0 | counter = &exec->comp->counters[trans->counter]; |
4356 | 0 | if ((counter == NULL) || (count < counter->max)) { |
4357 | 0 | if (atom->neg) |
4358 | 0 | values[nb++] = (xmlChar *) atom->valuep2; |
4359 | 0 | else |
4360 | 0 | values[nb++] = (xmlChar *) atom->valuep; |
4361 | 0 | (*nbval)++; |
4362 | 0 | } |
4363 | 0 | } else { |
4364 | 0 | if ((exec->comp != NULL) && (exec->comp->states[trans->to] != NULL) && |
4365 | 0 | (exec->comp->states[trans->to]->type != |
4366 | 0 | XML_REGEXP_SINK_STATE)) { |
4367 | 0 | if (atom->neg) |
4368 | 0 | values[nb++] = (xmlChar *) atom->valuep2; |
4369 | 0 | else |
4370 | 0 | values[nb++] = (xmlChar *) atom->valuep; |
4371 | 0 | (*nbval)++; |
4372 | 0 | } |
4373 | 0 | } |
4374 | 0 | } |
4375 | 0 | for (transno = 0; |
4376 | 0 | (transno < state->nbTrans) && (nb < maxval); |
4377 | 0 | transno++) { |
4378 | 0 | trans = &state->trans[transno]; |
4379 | 0 | if (trans->to < 0) |
4380 | 0 | continue; |
4381 | 0 | atom = trans->atom; |
4382 | 0 | if ((atom == NULL) || (atom->valuep == NULL)) |
4383 | 0 | continue; |
4384 | 0 | if (trans->count == REGEXP_ALL_LAX_COUNTER) { |
4385 | 0 | continue; |
4386 | 0 | } else if (trans->count == REGEXP_ALL_COUNTER) { |
4387 | 0 | continue; |
4388 | 0 | } else if (trans->counter >= 0) { |
4389 | 0 | continue; |
4390 | 0 | } else { |
4391 | 0 | if ((exec->comp->states[trans->to] != NULL) && |
4392 | 0 | (exec->comp->states[trans->to]->type == |
4393 | 0 | XML_REGEXP_SINK_STATE)) { |
4394 | 0 | if (atom->neg) |
4395 | 0 | values[nb++] = (xmlChar *) atom->valuep2; |
4396 | 0 | else |
4397 | 0 | values[nb++] = (xmlChar *) atom->valuep; |
4398 | 0 | (*nbneg)++; |
4399 | 0 | } |
4400 | 0 | } |
4401 | 0 | } |
4402 | 0 | } |
4403 | 0 | return(0); |
4404 | 0 | } |
4405 | | |
4406 | | /** |
4407 | | * Extract information from the regexp execution. |
4408 | | * The parameter `values` must point to an array of `nbval` string pointers |
4409 | | * on return nbval will contain the number of possible strings in that |
4410 | | * state and the `values` array will be updated with them. The string values |
4411 | | * returned will be freed with the `exec` context and don't need to be |
4412 | | * deallocated. |
4413 | | * |
4414 | | * @deprecated Internal function, don't use. |
4415 | | * |
4416 | | * @param exec a regexp execution context |
4417 | | * @param nbval pointer to the number of accepted values IN/OUT |
4418 | | * @param nbneg return number of negative transitions |
4419 | | * @param values pointer to the array of acceptable values |
4420 | | * @param terminal return value if this was a terminal state |
4421 | | * @returns 0 in case of success or -1 in case of error. |
4422 | | */ |
4423 | | int |
4424 | | xmlRegExecNextValues(xmlRegExecCtxt *exec, int *nbval, int *nbneg, |
4425 | 0 | xmlChar **values, int *terminal) { |
4426 | 0 | return(xmlRegExecGetValues(exec, 0, nbval, nbneg, values, terminal)); |
4427 | 0 | } |
4428 | | |
4429 | | /** |
4430 | | * Extract error information from the regexp execution. The parameter |
4431 | | * `string` will be updated with the value pushed and not accepted, |
4432 | | * the parameter `values` must point to an array of `nbval` string pointers |
4433 | | * on return nbval will contain the number of possible strings in that |
4434 | | * state and the `values` array will be updated with them. The string values |
4435 | | * returned will be freed with the `exec` context and don't need to be |
4436 | | * deallocated. |
4437 | | * |
4438 | | * @deprecated Internal function, don't use. |
4439 | | * |
4440 | | * @param exec a regexp execution context generating an error |
4441 | | * @param string return value for the error string |
4442 | | * @param nbval pointer to the number of accepted values IN/OUT |
4443 | | * @param nbneg return number of negative transitions |
4444 | | * @param values pointer to the array of acceptable values |
4445 | | * @param terminal return value if this was a terminal state |
4446 | | * @returns 0 in case of success or -1 in case of error. |
4447 | | */ |
4448 | | int |
4449 | | xmlRegExecErrInfo(xmlRegExecCtxt *exec, const xmlChar **string, |
4450 | 0 | int *nbval, int *nbneg, xmlChar **values, int *terminal) { |
4451 | 0 | if (exec == NULL) |
4452 | 0 | return(-1); |
4453 | 0 | if (string != NULL) { |
4454 | 0 | if (exec->status != XML_REGEXP_OK) |
4455 | 0 | *string = exec->errString; |
4456 | 0 | else |
4457 | 0 | *string = NULL; |
4458 | 0 | } |
4459 | 0 | return(xmlRegExecGetValues(exec, 1, nbval, nbneg, values, terminal)); |
4460 | 0 | } |
4461 | | |
4462 | | /************************************************************************ |
4463 | | * * |
4464 | | * Parser for the Schemas Datatype Regular Expressions * |
4465 | | * http://www.w3.org/TR/2001/REC-xmlschema-2-20010502/#regexs * |
4466 | | * * |
4467 | | ************************************************************************/ |
4468 | | |
4469 | | /** |
4470 | | * [10] Char ::= [^.\?*+()|\#x5B\#x5D] |
4471 | | * |
4472 | | * @param ctxt a regexp parser context |
4473 | | */ |
4474 | | static int |
4475 | 0 | xmlFAIsChar(xmlRegParserCtxtPtr ctxt) { |
4476 | 0 | int cur; |
4477 | 0 | int len; |
4478 | |
|
4479 | 0 | len = 4; |
4480 | 0 | cur = xmlGetUTF8Char(ctxt->cur, &len); |
4481 | 0 | if (cur < 0) { |
4482 | 0 | ERROR("Invalid UTF-8"); |
4483 | 0 | return(0); |
4484 | 0 | } |
4485 | 0 | if ((cur == '.') || (cur == '\\') || (cur == '?') || |
4486 | 0 | (cur == '*') || (cur == '+') || (cur == '(') || |
4487 | 0 | (cur == ')') || (cur == '|') || (cur == 0x5B) || |
4488 | 0 | (cur == 0x5D) || (cur == 0)) |
4489 | 0 | return(-1); |
4490 | 0 | return(cur); |
4491 | 0 | } |
4492 | | |
4493 | | /** |
4494 | | * [27] charProp ::= IsCategory | IsBlock |
4495 | | * [28] IsCategory ::= Letters | Marks | Numbers | Punctuation | |
4496 | | * Separators | Symbols | Others |
4497 | | * [29] Letters ::= 'L' [ultmo]? |
4498 | | * [30] Marks ::= 'M' [nce]? |
4499 | | * [31] Numbers ::= 'N' [dlo]? |
4500 | | * [32] Punctuation ::= 'P' [cdseifo]? |
4501 | | * [33] Separators ::= 'Z' [slp]? |
4502 | | * [34] Symbols ::= 'S' [mcko]? |
4503 | | * [35] Others ::= 'C' [cfon]? |
4504 | | * [36] IsBlock ::= 'Is' [a-zA-Z0-9\#x2D]+ |
4505 | | * |
4506 | | * @param ctxt a regexp parser context |
4507 | | */ |
4508 | | static void |
4509 | 0 | xmlFAParseCharProp(xmlRegParserCtxtPtr ctxt) { |
4510 | 0 | int cur; |
4511 | 0 | xmlRegAtomType type = (xmlRegAtomType) 0; |
4512 | 0 | xmlChar *blockName = NULL; |
4513 | |
|
4514 | 0 | cur = CUR; |
4515 | 0 | if (cur == 'L') { |
4516 | 0 | NEXT; |
4517 | 0 | cur = CUR; |
4518 | 0 | if (cur == 'u') { |
4519 | 0 | NEXT; |
4520 | 0 | type = XML_REGEXP_LETTER_UPPERCASE; |
4521 | 0 | } else if (cur == 'l') { |
4522 | 0 | NEXT; |
4523 | 0 | type = XML_REGEXP_LETTER_LOWERCASE; |
4524 | 0 | } else if (cur == 't') { |
4525 | 0 | NEXT; |
4526 | 0 | type = XML_REGEXP_LETTER_TITLECASE; |
4527 | 0 | } else if (cur == 'm') { |
4528 | 0 | NEXT; |
4529 | 0 | type = XML_REGEXP_LETTER_MODIFIER; |
4530 | 0 | } else if (cur == 'o') { |
4531 | 0 | NEXT; |
4532 | 0 | type = XML_REGEXP_LETTER_OTHERS; |
4533 | 0 | } else { |
4534 | 0 | type = XML_REGEXP_LETTER; |
4535 | 0 | } |
4536 | 0 | } else if (cur == 'M') { |
4537 | 0 | NEXT; |
4538 | 0 | cur = CUR; |
4539 | 0 | if (cur == 'n') { |
4540 | 0 | NEXT; |
4541 | | /* nonspacing */ |
4542 | 0 | type = XML_REGEXP_MARK_NONSPACING; |
4543 | 0 | } else if (cur == 'c') { |
4544 | 0 | NEXT; |
4545 | | /* spacing combining */ |
4546 | 0 | type = XML_REGEXP_MARK_SPACECOMBINING; |
4547 | 0 | } else if (cur == 'e') { |
4548 | 0 | NEXT; |
4549 | | /* enclosing */ |
4550 | 0 | type = XML_REGEXP_MARK_ENCLOSING; |
4551 | 0 | } else { |
4552 | | /* all marks */ |
4553 | 0 | type = XML_REGEXP_MARK; |
4554 | 0 | } |
4555 | 0 | } else if (cur == 'N') { |
4556 | 0 | NEXT; |
4557 | 0 | cur = CUR; |
4558 | 0 | if (cur == 'd') { |
4559 | 0 | NEXT; |
4560 | | /* digital */ |
4561 | 0 | type = XML_REGEXP_NUMBER_DECIMAL; |
4562 | 0 | } else if (cur == 'l') { |
4563 | 0 | NEXT; |
4564 | | /* letter */ |
4565 | 0 | type = XML_REGEXP_NUMBER_LETTER; |
4566 | 0 | } else if (cur == 'o') { |
4567 | 0 | NEXT; |
4568 | | /* other */ |
4569 | 0 | type = XML_REGEXP_NUMBER_OTHERS; |
4570 | 0 | } else { |
4571 | | /* all numbers */ |
4572 | 0 | type = XML_REGEXP_NUMBER; |
4573 | 0 | } |
4574 | 0 | } else if (cur == 'P') { |
4575 | 0 | NEXT; |
4576 | 0 | cur = CUR; |
4577 | 0 | if (cur == 'c') { |
4578 | 0 | NEXT; |
4579 | | /* connector */ |
4580 | 0 | type = XML_REGEXP_PUNCT_CONNECTOR; |
4581 | 0 | } else if (cur == 'd') { |
4582 | 0 | NEXT; |
4583 | | /* dash */ |
4584 | 0 | type = XML_REGEXP_PUNCT_DASH; |
4585 | 0 | } else if (cur == 's') { |
4586 | 0 | NEXT; |
4587 | | /* open */ |
4588 | 0 | type = XML_REGEXP_PUNCT_OPEN; |
4589 | 0 | } else if (cur == 'e') { |
4590 | 0 | NEXT; |
4591 | | /* close */ |
4592 | 0 | type = XML_REGEXP_PUNCT_CLOSE; |
4593 | 0 | } else if (cur == 'i') { |
4594 | 0 | NEXT; |
4595 | | /* initial quote */ |
4596 | 0 | type = XML_REGEXP_PUNCT_INITQUOTE; |
4597 | 0 | } else if (cur == 'f') { |
4598 | 0 | NEXT; |
4599 | | /* final quote */ |
4600 | 0 | type = XML_REGEXP_PUNCT_FINQUOTE; |
4601 | 0 | } else if (cur == 'o') { |
4602 | 0 | NEXT; |
4603 | | /* other */ |
4604 | 0 | type = XML_REGEXP_PUNCT_OTHERS; |
4605 | 0 | } else { |
4606 | | /* all punctuation */ |
4607 | 0 | type = XML_REGEXP_PUNCT; |
4608 | 0 | } |
4609 | 0 | } else if (cur == 'Z') { |
4610 | 0 | NEXT; |
4611 | 0 | cur = CUR; |
4612 | 0 | if (cur == 's') { |
4613 | 0 | NEXT; |
4614 | | /* space */ |
4615 | 0 | type = XML_REGEXP_SEPAR_SPACE; |
4616 | 0 | } else if (cur == 'l') { |
4617 | 0 | NEXT; |
4618 | | /* line */ |
4619 | 0 | type = XML_REGEXP_SEPAR_LINE; |
4620 | 0 | } else if (cur == 'p') { |
4621 | 0 | NEXT; |
4622 | | /* paragraph */ |
4623 | 0 | type = XML_REGEXP_SEPAR_PARA; |
4624 | 0 | } else { |
4625 | | /* all separators */ |
4626 | 0 | type = XML_REGEXP_SEPAR; |
4627 | 0 | } |
4628 | 0 | } else if (cur == 'S') { |
4629 | 0 | NEXT; |
4630 | 0 | cur = CUR; |
4631 | 0 | if (cur == 'm') { |
4632 | 0 | NEXT; |
4633 | 0 | type = XML_REGEXP_SYMBOL_MATH; |
4634 | | /* math */ |
4635 | 0 | } else if (cur == 'c') { |
4636 | 0 | NEXT; |
4637 | 0 | type = XML_REGEXP_SYMBOL_CURRENCY; |
4638 | | /* currency */ |
4639 | 0 | } else if (cur == 'k') { |
4640 | 0 | NEXT; |
4641 | 0 | type = XML_REGEXP_SYMBOL_MODIFIER; |
4642 | | /* modifiers */ |
4643 | 0 | } else if (cur == 'o') { |
4644 | 0 | NEXT; |
4645 | 0 | type = XML_REGEXP_SYMBOL_OTHERS; |
4646 | | /* other */ |
4647 | 0 | } else { |
4648 | | /* all symbols */ |
4649 | 0 | type = XML_REGEXP_SYMBOL; |
4650 | 0 | } |
4651 | 0 | } else if (cur == 'C') { |
4652 | 0 | NEXT; |
4653 | 0 | cur = CUR; |
4654 | 0 | if (cur == 'c') { |
4655 | 0 | NEXT; |
4656 | | /* control */ |
4657 | 0 | type = XML_REGEXP_OTHER_CONTROL; |
4658 | 0 | } else if (cur == 'f') { |
4659 | 0 | NEXT; |
4660 | | /* format */ |
4661 | 0 | type = XML_REGEXP_OTHER_FORMAT; |
4662 | 0 | } else if (cur == 'o') { |
4663 | 0 | NEXT; |
4664 | | /* private use */ |
4665 | 0 | type = XML_REGEXP_OTHER_PRIVATE; |
4666 | 0 | } else if (cur == 'n') { |
4667 | 0 | NEXT; |
4668 | | /* not assigned */ |
4669 | 0 | type = XML_REGEXP_OTHER_NA; |
4670 | 0 | } else { |
4671 | | /* all others */ |
4672 | 0 | type = XML_REGEXP_OTHER; |
4673 | 0 | } |
4674 | 0 | } else if (cur == 'I') { |
4675 | 0 | const xmlChar *start; |
4676 | 0 | NEXT; |
4677 | 0 | cur = CUR; |
4678 | 0 | if (cur != 's') { |
4679 | 0 | ERROR("IsXXXX expected"); |
4680 | 0 | return; |
4681 | 0 | } |
4682 | 0 | NEXT; |
4683 | 0 | start = ctxt->cur; |
4684 | 0 | cur = CUR; |
4685 | 0 | if (((cur >= 'a') && (cur <= 'z')) || |
4686 | 0 | ((cur >= 'A') && (cur <= 'Z')) || |
4687 | 0 | ((cur >= '0') && (cur <= '9')) || |
4688 | 0 | (cur == 0x2D)) { |
4689 | 0 | NEXT; |
4690 | 0 | cur = CUR; |
4691 | 0 | while (((cur >= 'a') && (cur <= 'z')) || |
4692 | 0 | ((cur >= 'A') && (cur <= 'Z')) || |
4693 | 0 | ((cur >= '0') && (cur <= '9')) || |
4694 | 0 | (cur == 0x2D)) { |
4695 | 0 | NEXT; |
4696 | 0 | cur = CUR; |
4697 | 0 | } |
4698 | 0 | } |
4699 | 0 | type = XML_REGEXP_BLOCK_NAME; |
4700 | 0 | blockName = xmlStrndup(start, ctxt->cur - start); |
4701 | 0 | if (blockName == NULL) |
4702 | 0 | xmlRegexpErrMemory(ctxt); |
4703 | 0 | } else { |
4704 | 0 | ERROR("Unknown char property"); |
4705 | 0 | return; |
4706 | 0 | } |
4707 | 0 | if (ctxt->atom == NULL) { |
4708 | 0 | ctxt->atom = xmlRegNewAtom(ctxt, type); |
4709 | 0 | if (ctxt->atom == NULL) { |
4710 | 0 | xmlFree(blockName); |
4711 | 0 | return; |
4712 | 0 | } |
4713 | 0 | ctxt->atom->valuep = blockName; |
4714 | 0 | } else if (ctxt->atom->type == XML_REGEXP_RANGES) { |
4715 | 0 | if (xmlRegAtomAddRange(ctxt, ctxt->atom, ctxt->neg, |
4716 | 0 | type, 0, 0, blockName) == NULL) { |
4717 | 0 | xmlFree(blockName); |
4718 | 0 | } |
4719 | 0 | } |
4720 | 0 | } |
4721 | | |
4722 | | static int parse_escaped_codeunit(xmlRegParserCtxtPtr ctxt) |
4723 | 0 | { |
4724 | 0 | int val = 0, i, cur; |
4725 | 0 | for (i = 0; i < 4; i++) { |
4726 | 0 | NEXT; |
4727 | 0 | val *= 16; |
4728 | 0 | cur = CUR; |
4729 | 0 | if (cur >= '0' && cur <= '9') { |
4730 | 0 | val += cur - '0'; |
4731 | 0 | } else if (cur >= 'A' && cur <= 'F') { |
4732 | 0 | val += cur - 'A' + 10; |
4733 | 0 | } else if (cur >= 'a' && cur <= 'f') { |
4734 | 0 | val += cur - 'a' + 10; |
4735 | 0 | } else { |
4736 | 0 | ERROR("Expecting hex digit"); |
4737 | 0 | return -1; |
4738 | 0 | } |
4739 | 0 | } |
4740 | 0 | return val; |
4741 | 0 | } |
4742 | | |
4743 | | static int parse_escaped_codepoint(xmlRegParserCtxtPtr ctxt) |
4744 | 0 | { |
4745 | 0 | int val = parse_escaped_codeunit(ctxt); |
4746 | 0 | if (0xD800 <= val && val <= 0xDBFF) { |
4747 | 0 | NEXT; |
4748 | 0 | if (CUR == '\\') { |
4749 | 0 | NEXT; |
4750 | 0 | if (CUR == 'u') { |
4751 | 0 | int low = parse_escaped_codeunit(ctxt); |
4752 | 0 | if (0xDC00 <= low && low <= 0xDFFF) { |
4753 | 0 | return (val - 0xD800) * 0x400 + (low - 0xDC00) + 0x10000; |
4754 | 0 | } |
4755 | 0 | } |
4756 | 0 | } |
4757 | 0 | ERROR("Invalid low surrogate pair code unit"); |
4758 | 0 | val = -1; |
4759 | 0 | } |
4760 | 0 | return val; |
4761 | 0 | } |
4762 | | |
4763 | | /** |
4764 | | * ``` |
4765 | | * [23] charClassEsc ::= ( SingleCharEsc | MultiCharEsc | catEsc | complEsc ) |
4766 | | * [24] SingleCharEsc ::= '\' [nrt\|.?*+(){}\#x2D\#x5B\#x5D\#x5E] |
4767 | | * [25] catEsc ::= '\p{' charProp '}' |
4768 | | * [26] complEsc ::= '\P{' charProp '}' |
4769 | | * [37] MultiCharEsc ::= '.' | ('\' [sSiIcCdDwW]) |
4770 | | * ``` |
4771 | | * |
4772 | | * @param ctxt a regexp parser context |
4773 | | */ |
4774 | | static void |
4775 | 0 | xmlFAParseCharClassEsc(xmlRegParserCtxtPtr ctxt) { |
4776 | 0 | int cur; |
4777 | |
|
4778 | 0 | if (CUR == '.') { |
4779 | 0 | if (ctxt->atom == NULL) { |
4780 | 0 | ctxt->atom = xmlRegNewAtom(ctxt, XML_REGEXP_ANYCHAR); |
4781 | 0 | } else if (ctxt->atom->type == XML_REGEXP_RANGES) { |
4782 | 0 | xmlRegAtomAddRange(ctxt, ctxt->atom, ctxt->neg, |
4783 | 0 | XML_REGEXP_ANYCHAR, 0, 0, NULL); |
4784 | 0 | } |
4785 | 0 | NEXT; |
4786 | 0 | return; |
4787 | 0 | } |
4788 | 0 | if (CUR != '\\') { |
4789 | 0 | ERROR("Escaped sequence: expecting \\"); |
4790 | 0 | return; |
4791 | 0 | } |
4792 | 0 | NEXT; |
4793 | 0 | cur = CUR; |
4794 | 0 | if (cur == 'p') { |
4795 | 0 | NEXT; |
4796 | 0 | if (CUR != '{') { |
4797 | 0 | ERROR("Expecting '{'"); |
4798 | 0 | return; |
4799 | 0 | } |
4800 | 0 | NEXT; |
4801 | 0 | xmlFAParseCharProp(ctxt); |
4802 | 0 | if (CUR != '}') { |
4803 | 0 | ERROR("Expecting '}'"); |
4804 | 0 | return; |
4805 | 0 | } |
4806 | 0 | NEXT; |
4807 | 0 | } else if (cur == 'P') { |
4808 | 0 | NEXT; |
4809 | 0 | if (CUR != '{') { |
4810 | 0 | ERROR("Expecting '{'"); |
4811 | 0 | return; |
4812 | 0 | } |
4813 | 0 | NEXT; |
4814 | 0 | xmlFAParseCharProp(ctxt); |
4815 | 0 | if (ctxt->atom != NULL) |
4816 | 0 | ctxt->atom->neg = 1; |
4817 | 0 | if (CUR != '}') { |
4818 | 0 | ERROR("Expecting '}'"); |
4819 | 0 | return; |
4820 | 0 | } |
4821 | 0 | NEXT; |
4822 | 0 | } else if ((cur == 'n') || (cur == 'r') || (cur == 't') || (cur == '\\') || |
4823 | 0 | (cur == '|') || (cur == '.') || (cur == '?') || (cur == '*') || |
4824 | 0 | (cur == '+') || (cur == '(') || (cur == ')') || (cur == '{') || |
4825 | 0 | (cur == '}') || (cur == 0x2D) || (cur == 0x5B) || (cur == 0x5D) || |
4826 | 0 | (cur == 0x5E) || |
4827 | | |
4828 | | /* Non-standard escape sequences: |
4829 | | * Java 1.8|.NET Core 3.1|MSXML 6 */ |
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 == '=') || /* + | + | + */ |
4840 | 0 | (cur == '>') || /* | + | + */ |
4841 | 0 | (cur == '@') || /* + | + | + */ |
4842 | 0 | (cur == '`') || /* + | + | + */ |
4843 | 0 | (cur == '~') || /* + | + | + */ |
4844 | 0 | (cur == 'u')) { /* | + | + */ |
4845 | 0 | if (ctxt->atom == NULL) { |
4846 | 0 | ctxt->atom = xmlRegNewAtom(ctxt, XML_REGEXP_CHARVAL); |
4847 | 0 | if (ctxt->atom != NULL) { |
4848 | 0 | switch (cur) { |
4849 | 0 | case 'n': |
4850 | 0 | ctxt->atom->codepoint = '\n'; |
4851 | 0 | break; |
4852 | 0 | case 'r': |
4853 | 0 | ctxt->atom->codepoint = '\r'; |
4854 | 0 | break; |
4855 | 0 | case 't': |
4856 | 0 | ctxt->atom->codepoint = '\t'; |
4857 | 0 | break; |
4858 | 0 | case 'u': |
4859 | 0 | cur = parse_escaped_codepoint(ctxt); |
4860 | 0 | if (cur < 0) { |
4861 | 0 | return; |
4862 | 0 | } |
4863 | 0 | ctxt->atom->codepoint = cur; |
4864 | 0 | break; |
4865 | 0 | default: |
4866 | 0 | ctxt->atom->codepoint = cur; |
4867 | 0 | } |
4868 | 0 | } |
4869 | 0 | } else if (ctxt->atom->type == XML_REGEXP_RANGES) { |
4870 | 0 | switch (cur) { |
4871 | 0 | case 'n': |
4872 | 0 | cur = '\n'; |
4873 | 0 | break; |
4874 | 0 | case 'r': |
4875 | 0 | cur = '\r'; |
4876 | 0 | break; |
4877 | 0 | case 't': |
4878 | 0 | cur = '\t'; |
4879 | 0 | break; |
4880 | 0 | } |
4881 | 0 | xmlRegAtomAddRange(ctxt, ctxt->atom, ctxt->neg, |
4882 | 0 | XML_REGEXP_CHARVAL, cur, cur, NULL); |
4883 | 0 | } |
4884 | 0 | NEXT; |
4885 | 0 | } else if ((cur == 's') || (cur == 'S') || (cur == 'i') || (cur == 'I') || |
4886 | 0 | (cur == 'c') || (cur == 'C') || (cur == 'd') || (cur == 'D') || |
4887 | 0 | (cur == 'w') || (cur == 'W')) { |
4888 | 0 | xmlRegAtomType type = XML_REGEXP_ANYSPACE; |
4889 | |
|
4890 | 0 | switch (cur) { |
4891 | 0 | case 's': |
4892 | 0 | type = XML_REGEXP_ANYSPACE; |
4893 | 0 | break; |
4894 | 0 | case 'S': |
4895 | 0 | type = XML_REGEXP_NOTSPACE; |
4896 | 0 | break; |
4897 | 0 | case 'i': |
4898 | 0 | type = XML_REGEXP_INITNAME; |
4899 | 0 | break; |
4900 | 0 | case 'I': |
4901 | 0 | type = XML_REGEXP_NOTINITNAME; |
4902 | 0 | break; |
4903 | 0 | case 'c': |
4904 | 0 | type = XML_REGEXP_NAMECHAR; |
4905 | 0 | break; |
4906 | 0 | case 'C': |
4907 | 0 | type = XML_REGEXP_NOTNAMECHAR; |
4908 | 0 | break; |
4909 | 0 | case 'd': |
4910 | 0 | type = XML_REGEXP_DECIMAL; |
4911 | 0 | break; |
4912 | 0 | case 'D': |
4913 | 0 | type = XML_REGEXP_NOTDECIMAL; |
4914 | 0 | break; |
4915 | 0 | case 'w': |
4916 | 0 | type = XML_REGEXP_REALCHAR; |
4917 | 0 | break; |
4918 | 0 | case 'W': |
4919 | 0 | type = XML_REGEXP_NOTREALCHAR; |
4920 | 0 | break; |
4921 | 0 | } |
4922 | 0 | NEXT; |
4923 | 0 | if (ctxt->atom == NULL) { |
4924 | 0 | ctxt->atom = xmlRegNewAtom(ctxt, type); |
4925 | 0 | } else if (ctxt->atom->type == XML_REGEXP_RANGES) { |
4926 | 0 | xmlRegAtomAddRange(ctxt, ctxt->atom, ctxt->neg, |
4927 | 0 | type, 0, 0, NULL); |
4928 | 0 | } |
4929 | 0 | } else { |
4930 | 0 | ERROR("Wrong escape sequence, misuse of character '\\'"); |
4931 | 0 | } |
4932 | 0 | } |
4933 | | |
4934 | | /** |
4935 | | * ``` |
4936 | | * [17] charRange ::= seRange | XmlCharRef | XmlCharIncDash |
4937 | | * [18] seRange ::= charOrEsc '-' charOrEsc |
4938 | | * [20] charOrEsc ::= XmlChar | SingleCharEsc |
4939 | | * [21] XmlChar ::= [^\\#x2D\#x5B\#x5D] |
4940 | | * [22] XmlCharIncDash ::= [^\\#x5B\#x5D] |
4941 | | * ``` |
4942 | | * |
4943 | | * @param ctxt a regexp parser context |
4944 | | */ |
4945 | | static void |
4946 | 0 | xmlFAParseCharRange(xmlRegParserCtxtPtr ctxt) { |
4947 | 0 | int cur, len; |
4948 | 0 | int start = -1; |
4949 | 0 | int end = -1; |
4950 | |
|
4951 | 0 | if (CUR == '\0') { |
4952 | 0 | ERROR("Expecting ']'"); |
4953 | 0 | return; |
4954 | 0 | } |
4955 | | |
4956 | 0 | cur = CUR; |
4957 | 0 | if (cur == '\\') { |
4958 | 0 | NEXT; |
4959 | 0 | cur = CUR; |
4960 | 0 | switch (cur) { |
4961 | 0 | case 'n': start = 0xA; break; |
4962 | 0 | case 'r': start = 0xD; break; |
4963 | 0 | case 't': start = 0x9; break; |
4964 | 0 | case '\\': case '|': case '.': case '-': case '^': case '?': |
4965 | 0 | case '*': case '+': case '{': case '}': case '(': case ')': |
4966 | 0 | case '[': case ']': |
4967 | 0 | start = cur; break; |
4968 | 0 | default: |
4969 | 0 | ERROR("Invalid escape value"); |
4970 | 0 | return; |
4971 | 0 | } |
4972 | 0 | end = start; |
4973 | 0 | len = 1; |
4974 | 0 | } else if ((cur != 0x5B) && (cur != 0x5D)) { |
4975 | 0 | len = 4; |
4976 | 0 | end = start = xmlGetUTF8Char(ctxt->cur, &len); |
4977 | 0 | if (start < 0) { |
4978 | 0 | ERROR("Invalid UTF-8"); |
4979 | 0 | return; |
4980 | 0 | } |
4981 | 0 | } else { |
4982 | 0 | ERROR("Expecting a char range"); |
4983 | 0 | return; |
4984 | 0 | } |
4985 | | /* |
4986 | | * Since we are "inside" a range, we can assume ctxt->cur is past |
4987 | | * the start of ctxt->string, and PREV should be safe |
4988 | | */ |
4989 | 0 | if ((start == '-') && (NXT(1) != ']') && (PREV != '[') && (PREV != '^')) { |
4990 | 0 | NEXTL(len); |
4991 | 0 | return; |
4992 | 0 | } |
4993 | 0 | NEXTL(len); |
4994 | 0 | cur = CUR; |
4995 | 0 | if ((cur != '-') || (NXT(1) == '[') || (NXT(1) == ']')) { |
4996 | 0 | xmlRegAtomAddRange(ctxt, ctxt->atom, ctxt->neg, |
4997 | 0 | XML_REGEXP_CHARVAL, start, end, NULL); |
4998 | 0 | return; |
4999 | 0 | } |
5000 | 0 | NEXT; |
5001 | 0 | cur = CUR; |
5002 | 0 | if (cur == '\\') { |
5003 | 0 | NEXT; |
5004 | 0 | cur = CUR; |
5005 | 0 | switch (cur) { |
5006 | 0 | case 'n': end = 0xA; break; |
5007 | 0 | case 'r': end = 0xD; break; |
5008 | 0 | case 't': end = 0x9; break; |
5009 | 0 | case '\\': case '|': case '.': case '-': case '^': case '?': |
5010 | 0 | case '*': case '+': case '{': case '}': case '(': case ')': |
5011 | 0 | case '[': case ']': |
5012 | 0 | end = cur; break; |
5013 | 0 | default: |
5014 | 0 | ERROR("Invalid escape value"); |
5015 | 0 | return; |
5016 | 0 | } |
5017 | 0 | len = 1; |
5018 | 0 | } else if ((cur != '\0') && (cur != 0x5B) && (cur != 0x5D)) { |
5019 | 0 | len = 4; |
5020 | 0 | end = xmlGetUTF8Char(ctxt->cur, &len); |
5021 | 0 | if (end < 0) { |
5022 | 0 | ERROR("Invalid UTF-8"); |
5023 | 0 | return; |
5024 | 0 | } |
5025 | 0 | } else { |
5026 | 0 | ERROR("Expecting the end of a char range"); |
5027 | 0 | return; |
5028 | 0 | } |
5029 | | |
5030 | | /* TODO check that the values are acceptable character ranges for XML */ |
5031 | 0 | if (end < start) { |
5032 | 0 | ERROR("End of range is before start of range"); |
5033 | 0 | } else { |
5034 | 0 | NEXTL(len); |
5035 | 0 | xmlRegAtomAddRange(ctxt, ctxt->atom, ctxt->neg, |
5036 | 0 | XML_REGEXP_CHARVAL, start, end, NULL); |
5037 | 0 | } |
5038 | 0 | } |
5039 | | |
5040 | | /** |
5041 | | * [14] posCharGroup ::= ( charRange | charClassEsc )+ |
5042 | | * |
5043 | | * @param ctxt a regexp parser context |
5044 | | */ |
5045 | | static void |
5046 | 0 | xmlFAParsePosCharGroup(xmlRegParserCtxtPtr ctxt) { |
5047 | 0 | do { |
5048 | 0 | if (CUR == '\\') { |
5049 | 0 | xmlFAParseCharClassEsc(ctxt); |
5050 | 0 | } else { |
5051 | 0 | xmlFAParseCharRange(ctxt); |
5052 | 0 | } |
5053 | 0 | } while ((CUR != ']') && (CUR != '-') && |
5054 | 0 | (CUR != 0) && (ctxt->error == 0)); |
5055 | 0 | } |
5056 | | |
5057 | | /** |
5058 | | * [13] charGroup ::= posCharGroup | negCharGroup | charClassSub |
5059 | | * [15] negCharGroup ::= '^' posCharGroup |
5060 | | * [16] charClassSub ::= ( posCharGroup | negCharGroup ) '-' charClassExpr |
5061 | | * [12] charClassExpr ::= '[' charGroup ']' |
5062 | | * |
5063 | | * @param ctxt a regexp parser context |
5064 | | */ |
5065 | | static void |
5066 | 0 | xmlFAParseCharGroup(xmlRegParserCtxtPtr ctxt) { |
5067 | 0 | int neg = ctxt->neg; |
5068 | |
|
5069 | 0 | if (CUR == '^') { |
5070 | 0 | NEXT; |
5071 | 0 | ctxt->neg = !ctxt->neg; |
5072 | 0 | xmlFAParsePosCharGroup(ctxt); |
5073 | 0 | ctxt->neg = neg; |
5074 | 0 | } |
5075 | 0 | while ((CUR != ']') && (ctxt->error == 0)) { |
5076 | 0 | if ((CUR == '-') && (NXT(1) == '[')) { |
5077 | 0 | NEXT; /* eat the '-' */ |
5078 | 0 | NEXT; /* eat the '[' */ |
5079 | 0 | ctxt->neg = 2; |
5080 | 0 | xmlFAParseCharGroup(ctxt); |
5081 | 0 | ctxt->neg = neg; |
5082 | 0 | if (CUR == ']') { |
5083 | 0 | NEXT; |
5084 | 0 | } else { |
5085 | 0 | ERROR("charClassExpr: ']' expected"); |
5086 | 0 | } |
5087 | 0 | break; |
5088 | 0 | } else { |
5089 | 0 | xmlFAParsePosCharGroup(ctxt); |
5090 | 0 | } |
5091 | 0 | } |
5092 | 0 | } |
5093 | | |
5094 | | /** |
5095 | | * [11] charClass ::= charClassEsc | charClassExpr |
5096 | | * [12] charClassExpr ::= '[' charGroup ']' |
5097 | | * |
5098 | | * @param ctxt a regexp parser context |
5099 | | */ |
5100 | | static void |
5101 | 0 | xmlFAParseCharClass(xmlRegParserCtxtPtr ctxt) { |
5102 | 0 | if (CUR == '[') { |
5103 | 0 | NEXT; |
5104 | 0 | ctxt->atom = xmlRegNewAtom(ctxt, XML_REGEXP_RANGES); |
5105 | 0 | if (ctxt->atom == NULL) |
5106 | 0 | return; |
5107 | 0 | xmlFAParseCharGroup(ctxt); |
5108 | 0 | if (CUR == ']') { |
5109 | 0 | NEXT; |
5110 | 0 | } else { |
5111 | 0 | ERROR("xmlFAParseCharClass: ']' expected"); |
5112 | 0 | } |
5113 | 0 | } else { |
5114 | 0 | xmlFAParseCharClassEsc(ctxt); |
5115 | 0 | } |
5116 | 0 | } |
5117 | | |
5118 | | /** |
5119 | | * [8] QuantExact ::= [0-9]+ |
5120 | | * |
5121 | | * @param ctxt a regexp parser context |
5122 | | * @returns 0 if success or -1 in case of error |
5123 | | */ |
5124 | | static int |
5125 | 0 | xmlFAParseQuantExact(xmlRegParserCtxtPtr ctxt) { |
5126 | 0 | int ret = 0; |
5127 | 0 | int ok = 0; |
5128 | 0 | int overflow = 0; |
5129 | |
|
5130 | 0 | while ((CUR >= '0') && (CUR <= '9')) { |
5131 | 0 | if (ret > INT_MAX / 10) { |
5132 | 0 | overflow = 1; |
5133 | 0 | } else { |
5134 | 0 | int digit = CUR - '0'; |
5135 | |
|
5136 | 0 | ret *= 10; |
5137 | 0 | if (ret > INT_MAX - digit) |
5138 | 0 | overflow = 1; |
5139 | 0 | else |
5140 | 0 | ret += digit; |
5141 | 0 | } |
5142 | 0 | ok = 1; |
5143 | 0 | NEXT; |
5144 | 0 | } |
5145 | 0 | if ((ok != 1) || (overflow == 1)) { |
5146 | 0 | return(-1); |
5147 | 0 | } |
5148 | 0 | return(ret); |
5149 | 0 | } |
5150 | | |
5151 | | /** |
5152 | | * [4] quantifier ::= [?*+] | ( '{' quantity '}' ) |
5153 | | * [5] quantity ::= quantRange | quantMin | QuantExact |
5154 | | * [6] quantRange ::= QuantExact ',' QuantExact |
5155 | | * [7] quantMin ::= QuantExact ',' |
5156 | | * [8] QuantExact ::= [0-9]+ |
5157 | | * |
5158 | | * @param ctxt a regexp parser context |
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 | | * [9] atom ::= Char | charClass | ( '(' regExp ')' ) |
5219 | | * |
5220 | | * @param ctxt a regexp parser context |
5221 | | */ |
5222 | | static int |
5223 | 0 | xmlFAParseAtom(xmlRegParserCtxtPtr ctxt) { |
5224 | 0 | int codepoint, len; |
5225 | |
|
5226 | 0 | codepoint = xmlFAIsChar(ctxt); |
5227 | 0 | if (codepoint > 0) { |
5228 | 0 | ctxt->atom = xmlRegNewAtom(ctxt, XML_REGEXP_CHARVAL); |
5229 | 0 | if (ctxt->atom == NULL) |
5230 | 0 | return(-1); |
5231 | 0 | len = 4; |
5232 | 0 | codepoint = xmlGetUTF8Char(ctxt->cur, &len); |
5233 | 0 | if (codepoint < 0) { |
5234 | 0 | ERROR("Invalid UTF-8"); |
5235 | 0 | return(-1); |
5236 | 0 | } |
5237 | 0 | ctxt->atom->codepoint = codepoint; |
5238 | 0 | NEXTL(len); |
5239 | 0 | return(1); |
5240 | 0 | } else if (CUR == '|') { |
5241 | 0 | return(0); |
5242 | 0 | } else if (CUR == 0) { |
5243 | 0 | return(0); |
5244 | 0 | } else if (CUR == ')') { |
5245 | 0 | return(0); |
5246 | 0 | } else if (CUR == '(') { |
5247 | 0 | xmlRegStatePtr start, oldend, start0; |
5248 | |
|
5249 | 0 | NEXT; |
5250 | 0 | if (ctxt->depth >= 50) { |
5251 | 0 | ERROR("xmlFAParseAtom: maximum nesting depth exceeded"); |
5252 | 0 | return(-1); |
5253 | 0 | } |
5254 | | /* |
5255 | | * this extra Epsilon transition is needed if we count with 0 allowed |
5256 | | * unfortunately this can't be known at that point |
5257 | | */ |
5258 | 0 | xmlFAGenerateEpsilonTransition(ctxt, ctxt->state, NULL); |
5259 | 0 | start0 = ctxt->state; |
5260 | 0 | xmlFAGenerateEpsilonTransition(ctxt, ctxt->state, NULL); |
5261 | 0 | start = ctxt->state; |
5262 | 0 | oldend = ctxt->end; |
5263 | 0 | ctxt->end = NULL; |
5264 | 0 | ctxt->atom = NULL; |
5265 | 0 | ctxt->depth++; |
5266 | 0 | xmlFAParseRegExp(ctxt, 0); |
5267 | 0 | ctxt->depth--; |
5268 | 0 | if (CUR == ')') { |
5269 | 0 | NEXT; |
5270 | 0 | } else { |
5271 | 0 | ERROR("xmlFAParseAtom: expecting ')'"); |
5272 | 0 | } |
5273 | 0 | ctxt->atom = xmlRegNewAtom(ctxt, XML_REGEXP_SUBREG); |
5274 | 0 | if (ctxt->atom == NULL) |
5275 | 0 | return(-1); |
5276 | 0 | ctxt->atom->start = start; |
5277 | 0 | ctxt->atom->start0 = start0; |
5278 | 0 | ctxt->atom->stop = ctxt->state; |
5279 | 0 | ctxt->end = oldend; |
5280 | 0 | return(1); |
5281 | 0 | } else if ((CUR == '[') || (CUR == '\\') || (CUR == '.')) { |
5282 | 0 | xmlFAParseCharClass(ctxt); |
5283 | 0 | return(1); |
5284 | 0 | } |
5285 | 0 | return(0); |
5286 | 0 | } |
5287 | | |
5288 | | /** |
5289 | | * [3] piece ::= atom quantifier? |
5290 | | * |
5291 | | * @param ctxt a regexp parser context |
5292 | | */ |
5293 | | static int |
5294 | 0 | xmlFAParsePiece(xmlRegParserCtxtPtr ctxt) { |
5295 | 0 | int ret; |
5296 | |
|
5297 | 0 | ctxt->atom = NULL; |
5298 | 0 | ret = xmlFAParseAtom(ctxt); |
5299 | 0 | if (ret == 0) |
5300 | 0 | return(0); |
5301 | 0 | if (ctxt->atom == NULL) { |
5302 | 0 | ERROR("internal: no atom generated"); |
5303 | 0 | } |
5304 | 0 | xmlFAParseQuantifier(ctxt); |
5305 | 0 | return(1); |
5306 | 0 | } |
5307 | | |
5308 | | /** |
5309 | | * `to` is used to optimize by removing duplicate path in automata |
5310 | | * in expressions like (a|b)(c|d) |
5311 | | * |
5312 | | * [2] branch ::= piece* |
5313 | | * |
5314 | | * @param ctxt a regexp parser context |
5315 | | * @param to optional target to the end of the branch |
5316 | | */ |
5317 | | static int |
5318 | 0 | xmlFAParseBranch(xmlRegParserCtxtPtr ctxt, xmlRegStatePtr to) { |
5319 | 0 | xmlRegStatePtr previous; |
5320 | 0 | int ret; |
5321 | |
|
5322 | 0 | previous = ctxt->state; |
5323 | 0 | ret = xmlFAParsePiece(ctxt); |
5324 | 0 | if (ret == 0) { |
5325 | | /* Empty branch */ |
5326 | 0 | xmlFAGenerateEpsilonTransition(ctxt, previous, to); |
5327 | 0 | } else { |
5328 | 0 | if (xmlFAGenerateTransitions(ctxt, previous, |
5329 | 0 | (CUR=='|' || CUR==')' || CUR==0) ? to : NULL, |
5330 | 0 | ctxt->atom) < 0) { |
5331 | 0 | xmlRegFreeAtom(ctxt->atom); |
5332 | 0 | ctxt->atom = NULL; |
5333 | 0 | return(-1); |
5334 | 0 | } |
5335 | 0 | previous = ctxt->state; |
5336 | 0 | ctxt->atom = NULL; |
5337 | 0 | } |
5338 | 0 | while ((ret != 0) && (ctxt->error == 0)) { |
5339 | 0 | ret = xmlFAParsePiece(ctxt); |
5340 | 0 | if (ret != 0) { |
5341 | 0 | if (xmlFAGenerateTransitions(ctxt, previous, |
5342 | 0 | (CUR=='|' || CUR==')' || CUR==0) ? to : NULL, |
5343 | 0 | ctxt->atom) < 0) { |
5344 | 0 | xmlRegFreeAtom(ctxt->atom); |
5345 | 0 | ctxt->atom = NULL; |
5346 | 0 | return(-1); |
5347 | 0 | } |
5348 | 0 | previous = ctxt->state; |
5349 | 0 | ctxt->atom = NULL; |
5350 | 0 | } |
5351 | 0 | } |
5352 | 0 | return(0); |
5353 | 0 | } |
5354 | | |
5355 | | /** |
5356 | | * [1] regExp ::= branch ( '|' branch )* |
5357 | | * |
5358 | | * @param ctxt a regexp parser context |
5359 | | * @param top is this the top-level expression ? |
5360 | | */ |
5361 | | static void |
5362 | 0 | xmlFAParseRegExp(xmlRegParserCtxtPtr ctxt, int top) { |
5363 | 0 | xmlRegStatePtr start, end; |
5364 | | |
5365 | | /* if not top start should have been generated by an epsilon trans */ |
5366 | 0 | start = ctxt->state; |
5367 | 0 | ctxt->end = NULL; |
5368 | 0 | xmlFAParseBranch(ctxt, NULL); |
5369 | 0 | if (top) { |
5370 | 0 | ctxt->state->type = XML_REGEXP_FINAL_STATE; |
5371 | 0 | } |
5372 | 0 | if (CUR != '|') { |
5373 | 0 | ctxt->end = ctxt->state; |
5374 | 0 | return; |
5375 | 0 | } |
5376 | 0 | end = ctxt->state; |
5377 | 0 | while ((CUR == '|') && (ctxt->error == 0)) { |
5378 | 0 | NEXT; |
5379 | 0 | ctxt->state = start; |
5380 | 0 | ctxt->end = NULL; |
5381 | 0 | xmlFAParseBranch(ctxt, end); |
5382 | 0 | } |
5383 | 0 | if (!top) { |
5384 | 0 | ctxt->state = end; |
5385 | 0 | ctxt->end = end; |
5386 | 0 | } |
5387 | 0 | } |
5388 | | |
5389 | | /************************************************************************ |
5390 | | * * |
5391 | | * The basic API * |
5392 | | * * |
5393 | | ************************************************************************/ |
5394 | | |
5395 | | /** |
5396 | | * No-op since 2.14.0. |
5397 | | * |
5398 | | * @deprecated Don't use. |
5399 | | * |
5400 | | * @param output the file for the output debug |
5401 | | * @param regexp the compiled regexp |
5402 | | */ |
5403 | | void |
5404 | | xmlRegexpPrint(FILE *output ATTRIBUTE_UNUSED, |
5405 | 0 | xmlRegexp *regexp ATTRIBUTE_UNUSED) { |
5406 | 0 | } |
5407 | | |
5408 | | /** |
5409 | | * Parses an XML Schemas regular expression. |
5410 | | * |
5411 | | * Parses a regular expression conforming to XML Schemas Part 2 Datatype |
5412 | | * Appendix F and builds an automata suitable for testing strings against |
5413 | | * that regular expression. |
5414 | | * |
5415 | | * @param regexp a regular expression string |
5416 | | * @returns the compiled expression or NULL in case of error |
5417 | | */ |
5418 | | xmlRegexp * |
5419 | 0 | xmlRegexpCompile(const xmlChar *regexp) { |
5420 | 0 | xmlRegexpPtr ret = NULL; |
5421 | 0 | xmlRegParserCtxtPtr ctxt; |
5422 | |
|
5423 | 0 | if (regexp == NULL) |
5424 | 0 | return(NULL); |
5425 | | |
5426 | 0 | ctxt = xmlRegNewParserCtxt(regexp); |
5427 | 0 | if (ctxt == NULL) |
5428 | 0 | return(NULL); |
5429 | | |
5430 | | /* initialize the parser */ |
5431 | 0 | ctxt->state = xmlRegStatePush(ctxt); |
5432 | 0 | if (ctxt->state == NULL) |
5433 | 0 | goto error; |
5434 | 0 | ctxt->start = ctxt->state; |
5435 | 0 | ctxt->end = NULL; |
5436 | | |
5437 | | /* parse the expression building an automata */ |
5438 | 0 | xmlFAParseRegExp(ctxt, 1); |
5439 | 0 | if (CUR != 0) { |
5440 | 0 | ERROR("xmlFAParseRegExp: extra characters"); |
5441 | 0 | } |
5442 | 0 | if (ctxt->error != 0) |
5443 | 0 | goto error; |
5444 | 0 | ctxt->end = ctxt->state; |
5445 | 0 | ctxt->start->type = XML_REGEXP_START_STATE; |
5446 | 0 | ctxt->end->type = XML_REGEXP_FINAL_STATE; |
5447 | | |
5448 | | /* remove the Epsilon except for counted transitions */ |
5449 | 0 | xmlFAEliminateEpsilonTransitions(ctxt); |
5450 | | |
5451 | |
|
5452 | 0 | if (ctxt->error != 0) |
5453 | 0 | goto error; |
5454 | 0 | ret = xmlRegEpxFromParse(ctxt); |
5455 | |
|
5456 | 0 | error: |
5457 | 0 | xmlRegFreeParserCtxt(ctxt); |
5458 | 0 | return(ret); |
5459 | 0 | } |
5460 | | |
5461 | | /** |
5462 | | * Check if the regular expression matches a string. |
5463 | | * |
5464 | | * @param comp the compiled regular expression |
5465 | | * @param content the value to check against the regular expression |
5466 | | * @returns 1 if it matches, 0 if not and a negative value in case of error |
5467 | | */ |
5468 | | int |
5469 | 0 | xmlRegexpExec(xmlRegexp *comp, const xmlChar *content) { |
5470 | 0 | if ((comp == NULL) || (content == NULL)) |
5471 | 0 | return(-1); |
5472 | 0 | return(xmlFARegExec(comp, content)); |
5473 | 0 | } |
5474 | | |
5475 | | /** |
5476 | | * Check if the regular expression is deterministic. |
5477 | | * |
5478 | | * DTD and XML Schemas require a deterministic content model, |
5479 | | * so the automaton compiled from the regex must be a DFA. |
5480 | | * |
5481 | | * The runtime of this function is quadratic in the number of |
5482 | | * outgoing edges, causing serious worst-case performance issues. |
5483 | | * |
5484 | | * @deprecated: Internal function, don't use. |
5485 | | * |
5486 | | * @param comp the compiled regular expression |
5487 | | * @returns 1 if it yes, 0 if not and a negative value in case |
5488 | | * of error |
5489 | | */ |
5490 | | int |
5491 | 0 | xmlRegexpIsDeterminist(xmlRegexp *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 | | * Free a regexp. |
5526 | | * |
5527 | | * @param regexp the regexp |
5528 | | */ |
5529 | | void |
5530 | 0 | xmlRegFreeRegexp(xmlRegexp *regexp) { |
5531 | 0 | int i; |
5532 | 0 | if (regexp == NULL) |
5533 | 0 | return; |
5534 | | |
5535 | 0 | if (regexp->string != NULL) |
5536 | 0 | xmlFree(regexp->string); |
5537 | 0 | if (regexp->states != NULL) { |
5538 | 0 | for (i = 0;i < regexp->nbStates;i++) |
5539 | 0 | xmlRegFreeState(regexp->states[i]); |
5540 | 0 | xmlFree(regexp->states); |
5541 | 0 | } |
5542 | 0 | if (regexp->atoms != NULL) { |
5543 | 0 | for (i = 0;i < regexp->nbAtoms;i++) |
5544 | 0 | xmlRegFreeAtom(regexp->atoms[i]); |
5545 | 0 | xmlFree(regexp->atoms); |
5546 | 0 | } |
5547 | 0 | if (regexp->counters != NULL) |
5548 | 0 | xmlFree(regexp->counters); |
5549 | 0 | if (regexp->compact != NULL) |
5550 | 0 | xmlFree(regexp->compact); |
5551 | 0 | if (regexp->transdata != NULL) |
5552 | 0 | xmlFree(regexp->transdata); |
5553 | 0 | if (regexp->stringMap != NULL) { |
5554 | 0 | for (i = 0; i < regexp->nbstrings;i++) |
5555 | 0 | xmlFree(regexp->stringMap[i]); |
5556 | 0 | xmlFree(regexp->stringMap); |
5557 | 0 | } |
5558 | |
|
5559 | 0 | xmlFree(regexp); |
5560 | 0 | } |
5561 | | |
5562 | | /************************************************************************ |
5563 | | * * |
5564 | | * The Automata interface * |
5565 | | * * |
5566 | | ************************************************************************/ |
5567 | | |
5568 | | /** |
5569 | | * Create a new automata |
5570 | | * |
5571 | | * @deprecated Internal function, don't use. |
5572 | | * |
5573 | | * @returns the new object or NULL in case of failure |
5574 | | */ |
5575 | | xmlAutomata * |
5576 | 0 | xmlNewAutomata(void) { |
5577 | 0 | xmlAutomataPtr ctxt; |
5578 | |
|
5579 | 0 | ctxt = xmlRegNewParserCtxt(NULL); |
5580 | 0 | if (ctxt == NULL) |
5581 | 0 | return(NULL); |
5582 | | |
5583 | | /* initialize the parser */ |
5584 | 0 | ctxt->state = xmlRegStatePush(ctxt); |
5585 | 0 | if (ctxt->state == NULL) { |
5586 | 0 | xmlFreeAutomata(ctxt); |
5587 | 0 | return(NULL); |
5588 | 0 | } |
5589 | 0 | ctxt->start = ctxt->state; |
5590 | 0 | ctxt->end = NULL; |
5591 | |
|
5592 | 0 | ctxt->start->type = XML_REGEXP_START_STATE; |
5593 | 0 | ctxt->flags = 0; |
5594 | |
|
5595 | 0 | return(ctxt); |
5596 | 0 | } |
5597 | | |
5598 | | /** |
5599 | | * Free an automata |
5600 | | * |
5601 | | * @deprecated Internal function, don't use. |
5602 | | * |
5603 | | * @param am an automata |
5604 | | */ |
5605 | | void |
5606 | 0 | xmlFreeAutomata(xmlAutomata *am) { |
5607 | 0 | if (am == NULL) |
5608 | 0 | return; |
5609 | 0 | xmlRegFreeParserCtxt(am); |
5610 | 0 | } |
5611 | | |
5612 | | /** |
5613 | | * Set some flags on the automata |
5614 | | * |
5615 | | * @deprecated Internal function, don't use. |
5616 | | * |
5617 | | * @param am an automata |
5618 | | * @param flags a set of internal flags |
5619 | | */ |
5620 | | void |
5621 | 0 | xmlAutomataSetFlags(xmlAutomata *am, int flags) { |
5622 | 0 | if (am == NULL) |
5623 | 0 | return; |
5624 | 0 | am->flags |= flags; |
5625 | 0 | } |
5626 | | |
5627 | | /** |
5628 | | * Initial state lookup |
5629 | | * |
5630 | | * @deprecated Internal function, don't use. |
5631 | | * |
5632 | | * @param am an automata |
5633 | | * @returns the initial state of the automata |
5634 | | */ |
5635 | | xmlAutomataState * |
5636 | 0 | xmlAutomataGetInitState(xmlAutomata *am) { |
5637 | 0 | if (am == NULL) |
5638 | 0 | return(NULL); |
5639 | 0 | return(am->start); |
5640 | 0 | } |
5641 | | |
5642 | | /** |
5643 | | * Makes that state a final state |
5644 | | * |
5645 | | * @deprecated Internal function, don't use. |
5646 | | * |
5647 | | * @param am an automata |
5648 | | * @param state a state in this automata |
5649 | | * @returns 0 or -1 in case of error |
5650 | | */ |
5651 | | int |
5652 | 0 | xmlAutomataSetFinalState(xmlAutomata *am, xmlAutomataState *state) { |
5653 | 0 | if ((am == NULL) || (state == NULL)) |
5654 | 0 | return(-1); |
5655 | 0 | state->type = XML_REGEXP_FINAL_STATE; |
5656 | 0 | return(0); |
5657 | 0 | } |
5658 | | |
5659 | | /** |
5660 | | * Add a transition. |
5661 | | * |
5662 | | * If `to` is NULL, this creates first a new target state in the automata |
5663 | | * and then adds a transition from the `from` state to the target state |
5664 | | * activated by the value of `token` |
5665 | | * |
5666 | | * @deprecated Internal function, don't use. |
5667 | | * |
5668 | | * @param am an automata |
5669 | | * @param from the starting point of the transition |
5670 | | * @param to the target point of the transition or NULL |
5671 | | * @param token the input string associated to that transition |
5672 | | * @param data data passed to the callback function if the transition is activated |
5673 | | * @returns the target state or NULL in case of error |
5674 | | */ |
5675 | | xmlAutomataState * |
5676 | | xmlAutomataNewTransition(xmlAutomata *am, xmlAutomataState *from, |
5677 | | xmlAutomataState *to, const xmlChar *token, |
5678 | 0 | void *data) { |
5679 | 0 | xmlRegAtomPtr atom; |
5680 | |
|
5681 | 0 | if ((am == NULL) || (from == NULL) || (token == NULL)) |
5682 | 0 | return(NULL); |
5683 | 0 | atom = xmlRegNewAtom(am, XML_REGEXP_STRING); |
5684 | 0 | if (atom == NULL) |
5685 | 0 | return(NULL); |
5686 | 0 | atom->data = data; |
5687 | 0 | atom->valuep = xmlStrdup(token); |
5688 | 0 | if (atom->valuep == NULL) { |
5689 | 0 | xmlRegFreeAtom(atom); |
5690 | 0 | xmlRegexpErrMemory(am); |
5691 | 0 | return(NULL); |
5692 | 0 | } |
5693 | | |
5694 | 0 | if (xmlFAGenerateTransitions(am, from, to, atom) < 0) { |
5695 | 0 | xmlRegFreeAtom(atom); |
5696 | 0 | return(NULL); |
5697 | 0 | } |
5698 | 0 | if (to == NULL) |
5699 | 0 | return(am->state); |
5700 | 0 | return(to); |
5701 | 0 | } |
5702 | | |
5703 | | /** |
5704 | | * If `to` is NULL, this creates first a new target state in the automata |
5705 | | * and then adds a transition from the `from` state to the target state |
5706 | | * activated by the value of `token` |
5707 | | * |
5708 | | * @deprecated Internal function, don't use. |
5709 | | * |
5710 | | * @param am an automata |
5711 | | * @param from the starting point of the transition |
5712 | | * @param to the target point of the transition or NULL |
5713 | | * @param token the first input string associated to that transition |
5714 | | * @param token2 the second input string associated to that transition |
5715 | | * @param data data passed to the callback function if the transition is activated |
5716 | | * @returns the target state or NULL in case of error |
5717 | | */ |
5718 | | xmlAutomataState * |
5719 | | xmlAutomataNewTransition2(xmlAutomata *am, xmlAutomataState *from, |
5720 | | xmlAutomataState *to, const xmlChar *token, |
5721 | 0 | const xmlChar *token2, void *data) { |
5722 | 0 | xmlRegAtomPtr atom; |
5723 | |
|
5724 | 0 | if ((am == NULL) || (from == NULL) || (token == NULL)) |
5725 | 0 | return(NULL); |
5726 | 0 | atom = xmlRegNewAtom(am, XML_REGEXP_STRING); |
5727 | 0 | if (atom == NULL) |
5728 | 0 | return(NULL); |
5729 | 0 | atom->data = data; |
5730 | 0 | if ((token2 == NULL) || (*token2 == 0)) { |
5731 | 0 | atom->valuep = xmlStrdup(token); |
5732 | 0 | } else { |
5733 | 0 | int lenn, lenp; |
5734 | 0 | xmlChar *str; |
5735 | |
|
5736 | 0 | lenn = strlen((char *) token2); |
5737 | 0 | lenp = strlen((char *) token); |
5738 | |
|
5739 | 0 | str = xmlMalloc(lenn + lenp + 2); |
5740 | 0 | if (str == NULL) { |
5741 | 0 | xmlRegFreeAtom(atom); |
5742 | 0 | return(NULL); |
5743 | 0 | } |
5744 | 0 | memcpy(&str[0], token, lenp); |
5745 | 0 | str[lenp] = '|'; |
5746 | 0 | memcpy(&str[lenp + 1], token2, lenn); |
5747 | 0 | str[lenn + lenp + 1] = 0; |
5748 | |
|
5749 | 0 | atom->valuep = str; |
5750 | 0 | } |
5751 | | |
5752 | 0 | if (xmlFAGenerateTransitions(am, from, to, atom) < 0) { |
5753 | 0 | xmlRegFreeAtom(atom); |
5754 | 0 | return(NULL); |
5755 | 0 | } |
5756 | 0 | if (to == NULL) |
5757 | 0 | return(am->state); |
5758 | 0 | return(to); |
5759 | 0 | } |
5760 | | |
5761 | | /** |
5762 | | * If `to` is NULL, this creates first a new target state in the automata |
5763 | | * and then adds a transition from the `from` state to the target state |
5764 | | * activated by any value except (`token`,`token2`) |
5765 | | * Note that if `token2` is not NULL, then (X, NULL) won't match to follow |
5766 | | * the semantic of XSD \#\#other |
5767 | | * |
5768 | | * @deprecated Internal function, don't use. |
5769 | | * |
5770 | | * @param am an automata |
5771 | | * @param from the starting point of the transition |
5772 | | * @param to the target point of the transition or NULL |
5773 | | * @param token the first input string associated to that transition |
5774 | | * @param token2 the second input string associated to that transition |
5775 | | * @param data data passed to the callback function if the transition is activated |
5776 | | * @returns the target state or NULL in case of error |
5777 | | */ |
5778 | | xmlAutomataState * |
5779 | | xmlAutomataNewNegTrans(xmlAutomata *am, xmlAutomataState *from, |
5780 | | xmlAutomataState *to, const xmlChar *token, |
5781 | 0 | const xmlChar *token2, void *data) { |
5782 | 0 | xmlRegAtomPtr atom; |
5783 | 0 | xmlChar err_msg[200]; |
5784 | |
|
5785 | 0 | if ((am == NULL) || (from == NULL) || (token == NULL)) |
5786 | 0 | return(NULL); |
5787 | 0 | atom = xmlRegNewAtom(am, XML_REGEXP_STRING); |
5788 | 0 | if (atom == NULL) |
5789 | 0 | return(NULL); |
5790 | 0 | atom->data = data; |
5791 | 0 | atom->neg = 1; |
5792 | 0 | if ((token2 == NULL) || (*token2 == 0)) { |
5793 | 0 | atom->valuep = xmlStrdup(token); |
5794 | 0 | } else { |
5795 | 0 | int lenn, lenp; |
5796 | 0 | xmlChar *str; |
5797 | |
|
5798 | 0 | lenn = strlen((char *) token2); |
5799 | 0 | lenp = strlen((char *) token); |
5800 | |
|
5801 | 0 | str = xmlMalloc(lenn + lenp + 2); |
5802 | 0 | if (str == NULL) { |
5803 | 0 | xmlRegFreeAtom(atom); |
5804 | 0 | return(NULL); |
5805 | 0 | } |
5806 | 0 | memcpy(&str[0], token, lenp); |
5807 | 0 | str[lenp] = '|'; |
5808 | 0 | memcpy(&str[lenp + 1], token2, lenn); |
5809 | 0 | str[lenn + lenp + 1] = 0; |
5810 | |
|
5811 | 0 | atom->valuep = str; |
5812 | 0 | } |
5813 | 0 | snprintf((char *) err_msg, 199, "not %s", (const char *) atom->valuep); |
5814 | 0 | err_msg[199] = 0; |
5815 | 0 | atom->valuep2 = xmlStrdup(err_msg); |
5816 | |
|
5817 | 0 | if (xmlFAGenerateTransitions(am, from, to, atom) < 0) { |
5818 | 0 | xmlRegFreeAtom(atom); |
5819 | 0 | return(NULL); |
5820 | 0 | } |
5821 | 0 | am->negs++; |
5822 | 0 | if (to == NULL) |
5823 | 0 | return(am->state); |
5824 | 0 | return(to); |
5825 | 0 | } |
5826 | | |
5827 | | /** |
5828 | | * If `to` is NULL, this creates first a new target state in the automata |
5829 | | * and then adds a transition from the `from` state to the target state |
5830 | | * activated by a succession of input of value `token` and `token2` and |
5831 | | * whose number is between `min` and `max` |
5832 | | * |
5833 | | * @deprecated Internal function, don't use. |
5834 | | * |
5835 | | * @param am an automata |
5836 | | * @param from the starting point of the transition |
5837 | | * @param to the target point of the transition or NULL |
5838 | | * @param token the input string associated to that transition |
5839 | | * @param token2 the second input string associated to that transition |
5840 | | * @param min the minimum successive occurrences of token |
5841 | | * @param max the maximum successive occurrences of token |
5842 | | * @param data data associated to the transition |
5843 | | * @returns the target state or NULL in case of error |
5844 | | */ |
5845 | | xmlAutomataState * |
5846 | | xmlAutomataNewCountTrans2(xmlAutomata *am, xmlAutomataState *from, |
5847 | | xmlAutomataState *to, const xmlChar *token, |
5848 | | const xmlChar *token2, |
5849 | 0 | int min, int max, void *data) { |
5850 | 0 | xmlRegAtomPtr atom; |
5851 | 0 | int counter; |
5852 | |
|
5853 | 0 | if ((am == NULL) || (from == NULL) || (token == NULL)) |
5854 | 0 | return(NULL); |
5855 | 0 | if (min < 0) |
5856 | 0 | return(NULL); |
5857 | 0 | if ((max < min) || (max < 1)) |
5858 | 0 | return(NULL); |
5859 | 0 | atom = xmlRegNewAtom(am, XML_REGEXP_STRING); |
5860 | 0 | if (atom == NULL) |
5861 | 0 | return(NULL); |
5862 | 0 | if ((token2 == NULL) || (*token2 == 0)) { |
5863 | 0 | atom->valuep = xmlStrdup(token); |
5864 | 0 | if (atom->valuep == NULL) |
5865 | 0 | goto error; |
5866 | 0 | } else { |
5867 | 0 | int lenn, lenp; |
5868 | 0 | xmlChar *str; |
5869 | |
|
5870 | 0 | lenn = strlen((char *) token2); |
5871 | 0 | lenp = strlen((char *) token); |
5872 | |
|
5873 | 0 | str = xmlMalloc(lenn + lenp + 2); |
5874 | 0 | if (str == NULL) |
5875 | 0 | goto error; |
5876 | 0 | memcpy(&str[0], token, lenp); |
5877 | 0 | str[lenp] = '|'; |
5878 | 0 | memcpy(&str[lenp + 1], token2, lenn); |
5879 | 0 | str[lenn + lenp + 1] = 0; |
5880 | |
|
5881 | 0 | atom->valuep = str; |
5882 | 0 | } |
5883 | 0 | atom->data = data; |
5884 | 0 | if (min == 0) |
5885 | 0 | atom->min = 1; |
5886 | 0 | else |
5887 | 0 | atom->min = min; |
5888 | 0 | atom->max = max; |
5889 | | |
5890 | | /* |
5891 | | * associate a counter to the transition. |
5892 | | */ |
5893 | 0 | counter = xmlRegGetCounter(am); |
5894 | 0 | if (counter < 0) |
5895 | 0 | goto error; |
5896 | 0 | am->counters[counter].min = min; |
5897 | 0 | am->counters[counter].max = max; |
5898 | | |
5899 | | /* xmlFAGenerateTransitions(am, from, to, atom); */ |
5900 | 0 | if (to == NULL) { |
5901 | 0 | to = xmlRegStatePush(am); |
5902 | 0 | if (to == NULL) |
5903 | 0 | goto error; |
5904 | 0 | } |
5905 | 0 | xmlRegStateAddTrans(am, from, atom, to, counter, -1); |
5906 | 0 | if (xmlRegAtomPush(am, atom) < 0) |
5907 | 0 | goto error; |
5908 | 0 | am->state = to; |
5909 | |
|
5910 | 0 | if (to == NULL) |
5911 | 0 | to = am->state; |
5912 | 0 | if (to == NULL) |
5913 | 0 | return(NULL); |
5914 | 0 | if (min == 0) |
5915 | 0 | xmlFAGenerateEpsilonTransition(am, from, to); |
5916 | 0 | return(to); |
5917 | | |
5918 | 0 | error: |
5919 | 0 | xmlRegFreeAtom(atom); |
5920 | 0 | return(NULL); |
5921 | 0 | } |
5922 | | |
5923 | | /** |
5924 | | * If `to` is NULL, this creates first a new target state in the automata |
5925 | | * and then adds a transition from the `from` state to the target state |
5926 | | * activated by a succession of input of value `token` and whose number |
5927 | | * is between `min` and `max` |
5928 | | * |
5929 | | * @deprecated Internal function, don't use. |
5930 | | * |
5931 | | * @param am an automata |
5932 | | * @param from the starting point of the transition |
5933 | | * @param to the target point of the transition or NULL |
5934 | | * @param token the input string associated to that transition |
5935 | | * @param min the minimum successive occurrences of token |
5936 | | * @param max the maximum successive occurrences of token |
5937 | | * @param data data associated to the transition |
5938 | | * @returns the target state or NULL in case of error |
5939 | | */ |
5940 | | xmlAutomataState * |
5941 | | xmlAutomataNewCountTrans(xmlAutomata *am, xmlAutomataState *from, |
5942 | | xmlAutomataState *to, const xmlChar *token, |
5943 | 0 | int min, int max, void *data) { |
5944 | 0 | xmlRegAtomPtr atom; |
5945 | 0 | int counter; |
5946 | |
|
5947 | 0 | if ((am == NULL) || (from == NULL) || (token == NULL)) |
5948 | 0 | return(NULL); |
5949 | 0 | if (min < 0) |
5950 | 0 | return(NULL); |
5951 | 0 | if ((max < min) || (max < 1)) |
5952 | 0 | return(NULL); |
5953 | 0 | atom = xmlRegNewAtom(am, XML_REGEXP_STRING); |
5954 | 0 | if (atom == NULL) |
5955 | 0 | return(NULL); |
5956 | 0 | atom->valuep = xmlStrdup(token); |
5957 | 0 | if (atom->valuep == NULL) |
5958 | 0 | goto error; |
5959 | 0 | atom->data = data; |
5960 | 0 | if (min == 0) |
5961 | 0 | atom->min = 1; |
5962 | 0 | else |
5963 | 0 | atom->min = min; |
5964 | 0 | atom->max = max; |
5965 | | |
5966 | | /* |
5967 | | * associate a counter to the transition. |
5968 | | */ |
5969 | 0 | counter = xmlRegGetCounter(am); |
5970 | 0 | if (counter < 0) |
5971 | 0 | goto error; |
5972 | 0 | am->counters[counter].min = min; |
5973 | 0 | am->counters[counter].max = max; |
5974 | | |
5975 | | /* xmlFAGenerateTransitions(am, from, to, atom); */ |
5976 | 0 | if (to == NULL) { |
5977 | 0 | to = xmlRegStatePush(am); |
5978 | 0 | if (to == NULL) |
5979 | 0 | goto error; |
5980 | 0 | } |
5981 | 0 | xmlRegStateAddTrans(am, from, atom, to, counter, -1); |
5982 | 0 | if (xmlRegAtomPush(am, atom) < 0) |
5983 | 0 | goto error; |
5984 | 0 | am->state = to; |
5985 | |
|
5986 | 0 | if (to == NULL) |
5987 | 0 | to = am->state; |
5988 | 0 | if (to == NULL) |
5989 | 0 | return(NULL); |
5990 | 0 | if (min == 0) |
5991 | 0 | xmlFAGenerateEpsilonTransition(am, from, to); |
5992 | 0 | return(to); |
5993 | | |
5994 | 0 | error: |
5995 | 0 | xmlRegFreeAtom(atom); |
5996 | 0 | return(NULL); |
5997 | 0 | } |
5998 | | |
5999 | | /** |
6000 | | * If `to` is NULL, this creates first a new target state in the automata |
6001 | | * and then adds a transition from the `from` state to the target state |
6002 | | * activated by a succession of input of value `token` and `token2` and whose |
6003 | | * number is between `min` and `max`, moreover that transition can only be |
6004 | | * crossed once. |
6005 | | * |
6006 | | * @deprecated Internal function, don't use. |
6007 | | * |
6008 | | * @param am an automata |
6009 | | * @param from the starting point of the transition |
6010 | | * @param to the target point of the transition or NULL |
6011 | | * @param token the input string associated to that transition |
6012 | | * @param token2 the second input string associated to that transition |
6013 | | * @param min the minimum successive occurrences of token |
6014 | | * @param max the maximum successive occurrences of token |
6015 | | * @param data data associated to the transition |
6016 | | * @returns the target state or NULL in case of error |
6017 | | */ |
6018 | | xmlAutomataState * |
6019 | | xmlAutomataNewOnceTrans2(xmlAutomata *am, xmlAutomataState *from, |
6020 | | xmlAutomataState *to, const xmlChar *token, |
6021 | | const xmlChar *token2, |
6022 | 0 | int min, int max, void *data) { |
6023 | 0 | xmlRegAtomPtr atom; |
6024 | 0 | int counter; |
6025 | |
|
6026 | 0 | if ((am == NULL) || (from == NULL) || (token == NULL)) |
6027 | 0 | return(NULL); |
6028 | 0 | if (min < 1) |
6029 | 0 | return(NULL); |
6030 | 0 | if (max < min) |
6031 | 0 | return(NULL); |
6032 | 0 | atom = xmlRegNewAtom(am, XML_REGEXP_STRING); |
6033 | 0 | if (atom == NULL) |
6034 | 0 | return(NULL); |
6035 | 0 | if ((token2 == NULL) || (*token2 == 0)) { |
6036 | 0 | atom->valuep = xmlStrdup(token); |
6037 | 0 | if (atom->valuep == NULL) |
6038 | 0 | goto error; |
6039 | 0 | } else { |
6040 | 0 | int lenn, lenp; |
6041 | 0 | xmlChar *str; |
6042 | |
|
6043 | 0 | lenn = strlen((char *) token2); |
6044 | 0 | lenp = strlen((char *) token); |
6045 | |
|
6046 | 0 | str = xmlMalloc(lenn + lenp + 2); |
6047 | 0 | if (str == NULL) |
6048 | 0 | goto error; |
6049 | 0 | memcpy(&str[0], token, lenp); |
6050 | 0 | str[lenp] = '|'; |
6051 | 0 | memcpy(&str[lenp + 1], token2, lenn); |
6052 | 0 | str[lenn + lenp + 1] = 0; |
6053 | |
|
6054 | 0 | atom->valuep = str; |
6055 | 0 | } |
6056 | 0 | atom->data = data; |
6057 | 0 | atom->quant = XML_REGEXP_QUANT_ONCEONLY; |
6058 | 0 | atom->min = min; |
6059 | 0 | atom->max = max; |
6060 | | /* |
6061 | | * associate a counter to the transition. |
6062 | | */ |
6063 | 0 | counter = xmlRegGetCounter(am); |
6064 | 0 | if (counter < 0) |
6065 | 0 | goto error; |
6066 | 0 | am->counters[counter].min = 1; |
6067 | 0 | am->counters[counter].max = 1; |
6068 | | |
6069 | | /* xmlFAGenerateTransitions(am, from, to, atom); */ |
6070 | 0 | if (to == NULL) { |
6071 | 0 | to = xmlRegStatePush(am); |
6072 | 0 | if (to == NULL) |
6073 | 0 | goto error; |
6074 | 0 | } |
6075 | 0 | xmlRegStateAddTrans(am, from, atom, to, counter, -1); |
6076 | 0 | if (xmlRegAtomPush(am, atom) < 0) |
6077 | 0 | goto error; |
6078 | 0 | am->state = to; |
6079 | 0 | return(to); |
6080 | | |
6081 | 0 | error: |
6082 | 0 | xmlRegFreeAtom(atom); |
6083 | 0 | return(NULL); |
6084 | 0 | } |
6085 | | |
6086 | | |
6087 | | |
6088 | | /** |
6089 | | * If `to` is NULL, this creates first a new target state in the automata |
6090 | | * and then adds a transition from the `from` state to the target state |
6091 | | * activated by a succession of input of value `token` and whose number |
6092 | | * is between `min` and `max`, moreover that transition can only be crossed |
6093 | | * once. |
6094 | | * |
6095 | | * @deprecated Internal function, don't use. |
6096 | | * |
6097 | | * @param am an automata |
6098 | | * @param from the starting point of the transition |
6099 | | * @param to the target point of the transition or NULL |
6100 | | * @param token the input string associated to that transition |
6101 | | * @param min the minimum successive occurrences of token |
6102 | | * @param max the maximum successive occurrences of token |
6103 | | * @param data data associated to the transition |
6104 | | * @returns the target state or NULL in case of error |
6105 | | */ |
6106 | | xmlAutomataState * |
6107 | | xmlAutomataNewOnceTrans(xmlAutomata *am, xmlAutomataState *from, |
6108 | | xmlAutomataState *to, const xmlChar *token, |
6109 | 0 | int min, int max, void *data) { |
6110 | 0 | xmlRegAtomPtr atom; |
6111 | 0 | int counter; |
6112 | |
|
6113 | 0 | if ((am == NULL) || (from == NULL) || (token == NULL)) |
6114 | 0 | return(NULL); |
6115 | 0 | if (min < 1) |
6116 | 0 | return(NULL); |
6117 | 0 | if (max < min) |
6118 | 0 | return(NULL); |
6119 | 0 | atom = xmlRegNewAtom(am, XML_REGEXP_STRING); |
6120 | 0 | if (atom == NULL) |
6121 | 0 | return(NULL); |
6122 | 0 | atom->valuep = xmlStrdup(token); |
6123 | 0 | atom->data = data; |
6124 | 0 | atom->quant = XML_REGEXP_QUANT_ONCEONLY; |
6125 | 0 | atom->min = min; |
6126 | 0 | atom->max = max; |
6127 | | /* |
6128 | | * associate a counter to the transition. |
6129 | | */ |
6130 | 0 | counter = xmlRegGetCounter(am); |
6131 | 0 | if (counter < 0) |
6132 | 0 | goto error; |
6133 | 0 | am->counters[counter].min = 1; |
6134 | 0 | am->counters[counter].max = 1; |
6135 | | |
6136 | | /* xmlFAGenerateTransitions(am, from, to, atom); */ |
6137 | 0 | if (to == NULL) { |
6138 | 0 | to = xmlRegStatePush(am); |
6139 | 0 | if (to == NULL) |
6140 | 0 | goto error; |
6141 | 0 | } |
6142 | 0 | xmlRegStateAddTrans(am, from, atom, to, counter, -1); |
6143 | 0 | if (xmlRegAtomPush(am, atom) < 0) |
6144 | 0 | goto error; |
6145 | 0 | am->state = to; |
6146 | 0 | return(to); |
6147 | | |
6148 | 0 | error: |
6149 | 0 | xmlRegFreeAtom(atom); |
6150 | 0 | return(NULL); |
6151 | 0 | } |
6152 | | |
6153 | | /** |
6154 | | * Create a new disconnected state in the automata |
6155 | | * |
6156 | | * @deprecated Internal function, don't use. |
6157 | | * |
6158 | | * @param am an automata |
6159 | | * @returns the new state or NULL in case of error |
6160 | | */ |
6161 | | xmlAutomataState * |
6162 | 0 | xmlAutomataNewState(xmlAutomata *am) { |
6163 | 0 | if (am == NULL) |
6164 | 0 | return(NULL); |
6165 | 0 | return(xmlRegStatePush(am)); |
6166 | 0 | } |
6167 | | |
6168 | | /** |
6169 | | * If `to` is NULL, this creates first a new target state in the automata |
6170 | | * and then adds an epsilon transition from the `from` state to the |
6171 | | * target state |
6172 | | * |
6173 | | * @deprecated Internal function, don't use. |
6174 | | * |
6175 | | * @param am an automata |
6176 | | * @param from the starting point of the transition |
6177 | | * @param to the target point of the transition or NULL |
6178 | | * @returns the target state or NULL in case of error |
6179 | | */ |
6180 | | xmlAutomataState * |
6181 | | xmlAutomataNewEpsilon(xmlAutomata *am, xmlAutomataState *from, |
6182 | 0 | xmlAutomataState *to) { |
6183 | 0 | if ((am == NULL) || (from == NULL)) |
6184 | 0 | return(NULL); |
6185 | 0 | xmlFAGenerateEpsilonTransition(am, from, to); |
6186 | 0 | if (to == NULL) |
6187 | 0 | return(am->state); |
6188 | 0 | return(to); |
6189 | 0 | } |
6190 | | |
6191 | | /** |
6192 | | * If `to` is NULL, this creates first a new target state in the automata |
6193 | | * and then adds a an ALL transition from the `from` state to the |
6194 | | * target state. That transition is an epsilon transition allowed only when |
6195 | | * all transitions from the `from` node have been activated. |
6196 | | * |
6197 | | * @deprecated Internal function, don't use. |
6198 | | * |
6199 | | * @param am an automata |
6200 | | * @param from the starting point of the transition |
6201 | | * @param to the target point of the transition or NULL |
6202 | | * @param lax allow to transition if not all all transitions have been activated |
6203 | | * @returns the target state or NULL in case of error |
6204 | | */ |
6205 | | xmlAutomataState * |
6206 | | xmlAutomataNewAllTrans(xmlAutomata *am, xmlAutomataState *from, |
6207 | 0 | xmlAutomataState *to, int lax) { |
6208 | 0 | if ((am == NULL) || (from == NULL)) |
6209 | 0 | return(NULL); |
6210 | 0 | xmlFAGenerateAllTransition(am, from, to, lax); |
6211 | 0 | if (to == NULL) |
6212 | 0 | return(am->state); |
6213 | 0 | return(to); |
6214 | 0 | } |
6215 | | |
6216 | | /** |
6217 | | * Create a new counter |
6218 | | * |
6219 | | * @deprecated Internal function, don't use. |
6220 | | * |
6221 | | * @param am an automata |
6222 | | * @param min the minimal value on the counter |
6223 | | * @param max the maximal value on the counter |
6224 | | * @returns the counter number or -1 in case of error |
6225 | | */ |
6226 | | int |
6227 | 0 | xmlAutomataNewCounter(xmlAutomata *am, int min, int max) { |
6228 | 0 | int ret; |
6229 | |
|
6230 | 0 | if (am == NULL) |
6231 | 0 | return(-1); |
6232 | | |
6233 | 0 | ret = xmlRegGetCounter(am); |
6234 | 0 | if (ret < 0) |
6235 | 0 | return(-1); |
6236 | 0 | am->counters[ret].min = min; |
6237 | 0 | am->counters[ret].max = max; |
6238 | 0 | return(ret); |
6239 | 0 | } |
6240 | | |
6241 | | /** |
6242 | | * If `to` is NULL, this creates first a new target state in the automata |
6243 | | * and then adds an epsilon transition from the `from` state to the target state |
6244 | | * which will increment the counter provided |
6245 | | * |
6246 | | * @deprecated Internal function, don't use. |
6247 | | * |
6248 | | * @param am an automata |
6249 | | * @param from the starting point of the transition |
6250 | | * @param to the target point of the transition or NULL |
6251 | | * @param counter the counter associated to that transition |
6252 | | * @returns the target state or NULL in case of error |
6253 | | */ |
6254 | | xmlAutomataState * |
6255 | | xmlAutomataNewCountedTrans(xmlAutomata *am, xmlAutomataState *from, |
6256 | 0 | xmlAutomataState *to, int counter) { |
6257 | 0 | if ((am == NULL) || (from == NULL) || (counter < 0)) |
6258 | 0 | return(NULL); |
6259 | 0 | xmlFAGenerateCountedEpsilonTransition(am, from, to, counter); |
6260 | 0 | if (to == NULL) |
6261 | 0 | return(am->state); |
6262 | 0 | return(to); |
6263 | 0 | } |
6264 | | |
6265 | | /** |
6266 | | * If `to` is NULL, this creates first a new target state in the automata |
6267 | | * and then adds an epsilon transition from the `from` state to the target state |
6268 | | * which will be allowed only if the counter is within the right range. |
6269 | | * |
6270 | | * @deprecated Internal function, don't use. |
6271 | | * |
6272 | | * @param am an automata |
6273 | | * @param from the starting point of the transition |
6274 | | * @param to the target point of the transition or NULL |
6275 | | * @param counter the counter associated to that transition |
6276 | | * @returns the target state or NULL in case of error |
6277 | | */ |
6278 | | xmlAutomataState * |
6279 | | xmlAutomataNewCounterTrans(xmlAutomata *am, xmlAutomataState *from, |
6280 | 0 | xmlAutomataState *to, int counter) { |
6281 | 0 | if ((am == NULL) || (from == NULL) || (counter < 0)) |
6282 | 0 | return(NULL); |
6283 | 0 | xmlFAGenerateCountedTransition(am, from, to, counter); |
6284 | 0 | if (to == NULL) |
6285 | 0 | return(am->state); |
6286 | 0 | return(to); |
6287 | 0 | } |
6288 | | |
6289 | | /** |
6290 | | * Compile the automata into a Reg Exp ready for being executed. |
6291 | | * The automata should be free after this point. |
6292 | | * |
6293 | | * @deprecated Internal function, don't use. |
6294 | | * |
6295 | | * @param am an automata |
6296 | | * @returns the compiled regexp or NULL in case of error |
6297 | | */ |
6298 | | xmlRegexp * |
6299 | 0 | xmlAutomataCompile(xmlAutomata *am) { |
6300 | 0 | xmlRegexpPtr ret; |
6301 | |
|
6302 | 0 | if ((am == NULL) || (am->error != 0)) return(NULL); |
6303 | 0 | xmlFAEliminateEpsilonTransitions(am); |
6304 | 0 | if (am->error != 0) |
6305 | 0 | return(NULL); |
6306 | | /* xmlFAComputesDeterminism(am); */ |
6307 | 0 | ret = xmlRegEpxFromParse(am); |
6308 | |
|
6309 | 0 | return(ret); |
6310 | 0 | } |
6311 | | |
6312 | | /** |
6313 | | * Checks if an automata is determinist. |
6314 | | * |
6315 | | * @deprecated Internal function, don't use. |
6316 | | * |
6317 | | * @param am an automata |
6318 | | * @returns 1 if true, 0 if not, and -1 in case of error |
6319 | | */ |
6320 | | int |
6321 | 0 | xmlAutomataIsDeterminist(xmlAutomata *am) { |
6322 | 0 | int ret; |
6323 | |
|
6324 | 0 | if (am == NULL) |
6325 | 0 | return(-1); |
6326 | | |
6327 | 0 | ret = xmlFAComputesDeterminism(am); |
6328 | 0 | return(ret); |
6329 | 0 | } |
6330 | | |
6331 | | #endif /* LIBXML_REGEXP_ENABLED */ |