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