blob: 484c76e3700ddfda97504a3b01c4cc73fe6354bb [file] [log] [blame]
Daniel Veillard4255d502002-04-16 15:50:10 +00001/*
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/shemas mechanisms now available in
6 * XML related specifications thise includes:
7 * - XML-1.0 DTD validation
8 * - XML Schemas structure part 1
9 * - XML Schemas Datatypes part 2 especially Appendix F
10 * - RELAX-NG/TREX i.e. the counter proposal
11 *
12 * See Copyright for the status of this software.
13 *
14 * Daniel Veillard <veillard@redhat.com>
15 */
16
17#define IN_LIBXML
18#include "libxml.h"
19
20#ifdef LIBXML_REGEXP_ENABLED
21
22#include <stdio.h>
23#include <string.h>
Daniel Veillardebe48c62003-12-03 12:12:27 +000024#ifdef HAVE_LIMITS_H
25#include <limits.h>
26#endif
27
Daniel Veillard4255d502002-04-16 15:50:10 +000028#include <libxml/tree.h>
29#include <libxml/parserInternals.h>
30#include <libxml/xmlregexp.h>
31#include <libxml/xmlautomata.h>
32#include <libxml/xmlunicode.h>
33
Daniel Veillardebe48c62003-12-03 12:12:27 +000034#ifndef INT_MAX
35#define INT_MAX 123456789 /* easy to flag and big enough for our needs */
36#endif
37
Daniel Veillard4255d502002-04-16 15:50:10 +000038/* #define DEBUG_REGEXP_GRAPH */
39/* #define DEBUG_REGEXP_EXEC */
40/* #define DEBUG_PUSH */
Daniel Veillard23e73572002-09-19 19:56:43 +000041/* #define DEBUG_COMPACTION */
Daniel Veillard4255d502002-04-16 15:50:10 +000042
Daniel Veillardff46a042003-10-08 08:53:17 +000043#define ERROR(str) \
44 ctxt->error = XML_REGEXP_COMPILE_ERROR; \
45 xmlRegexpErrCompile(ctxt, str);
Daniel Veillard4255d502002-04-16 15:50:10 +000046#define NEXT ctxt->cur++
47#define CUR (*(ctxt->cur))
48#define NXT(index) (ctxt->cur[index])
49
50#define CUR_SCHAR(s, l) xmlStringCurrentChar(NULL, s, &l)
51#define NEXTL(l) ctxt->cur += l;
52
Daniel Veillarde19fc232002-04-22 16:01:24 +000053/**
54 * TODO:
55 *
56 * macro to flag unimplemented blocks
57 */
58#define TODO \
59 xmlGenericError(xmlGenericErrorContext, \
60 "Unimplemented block at %s:%d\n", \
61 __FILE__, __LINE__);
62
Daniel Veillard4255d502002-04-16 15:50:10 +000063/************************************************************************
64 * *
65 * Datatypes and structures *
66 * *
67 ************************************************************************/
68
69typedef enum {
70 XML_REGEXP_EPSILON = 1,
71 XML_REGEXP_CHARVAL,
72 XML_REGEXP_RANGES,
73 XML_REGEXP_SUBREG,
74 XML_REGEXP_STRING,
75 XML_REGEXP_ANYCHAR, /* . */
76 XML_REGEXP_ANYSPACE, /* \s */
77 XML_REGEXP_NOTSPACE, /* \S */
78 XML_REGEXP_INITNAME, /* \l */
79 XML_REGEXP_NOTINITNAME, /* \l */
80 XML_REGEXP_NAMECHAR, /* \c */
81 XML_REGEXP_NOTNAMECHAR, /* \C */
82 XML_REGEXP_DECIMAL, /* \d */
83 XML_REGEXP_NOTDECIMAL, /* \d */
84 XML_REGEXP_REALCHAR, /* \w */
85 XML_REGEXP_NOTREALCHAR, /* \w */
86 XML_REGEXP_LETTER,
87 XML_REGEXP_LETTER_UPPERCASE,
88 XML_REGEXP_LETTER_LOWERCASE,
89 XML_REGEXP_LETTER_TITLECASE,
90 XML_REGEXP_LETTER_MODIFIER,
91 XML_REGEXP_LETTER_OTHERS,
92 XML_REGEXP_MARK,
93 XML_REGEXP_MARK_NONSPACING,
94 XML_REGEXP_MARK_SPACECOMBINING,
95 XML_REGEXP_MARK_ENCLOSING,
96 XML_REGEXP_NUMBER,
97 XML_REGEXP_NUMBER_DECIMAL,
98 XML_REGEXP_NUMBER_LETTER,
99 XML_REGEXP_NUMBER_OTHERS,
100 XML_REGEXP_PUNCT,
101 XML_REGEXP_PUNCT_CONNECTOR,
102 XML_REGEXP_PUNCT_DASH,
103 XML_REGEXP_PUNCT_OPEN,
104 XML_REGEXP_PUNCT_CLOSE,
105 XML_REGEXP_PUNCT_INITQUOTE,
106 XML_REGEXP_PUNCT_FINQUOTE,
107 XML_REGEXP_PUNCT_OTHERS,
108 XML_REGEXP_SEPAR,
109 XML_REGEXP_SEPAR_SPACE,
110 XML_REGEXP_SEPAR_LINE,
111 XML_REGEXP_SEPAR_PARA,
112 XML_REGEXP_SYMBOL,
113 XML_REGEXP_SYMBOL_MATH,
114 XML_REGEXP_SYMBOL_CURRENCY,
115 XML_REGEXP_SYMBOL_MODIFIER,
116 XML_REGEXP_SYMBOL_OTHERS,
117 XML_REGEXP_OTHER,
118 XML_REGEXP_OTHER_CONTROL,
119 XML_REGEXP_OTHER_FORMAT,
120 XML_REGEXP_OTHER_PRIVATE,
121 XML_REGEXP_OTHER_NA,
122 XML_REGEXP_BLOCK_NAME
123} xmlRegAtomType;
124
125typedef enum {
126 XML_REGEXP_QUANT_EPSILON = 1,
127 XML_REGEXP_QUANT_ONCE,
128 XML_REGEXP_QUANT_OPT,
129 XML_REGEXP_QUANT_MULT,
130 XML_REGEXP_QUANT_PLUS,
Daniel Veillard7646b182002-04-20 06:41:40 +0000131 XML_REGEXP_QUANT_ONCEONLY,
132 XML_REGEXP_QUANT_ALL,
Daniel Veillard4255d502002-04-16 15:50:10 +0000133 XML_REGEXP_QUANT_RANGE
134} xmlRegQuantType;
135
136typedef enum {
137 XML_REGEXP_START_STATE = 1,
138 XML_REGEXP_FINAL_STATE,
139 XML_REGEXP_TRANS_STATE
140} xmlRegStateType;
141
142typedef enum {
143 XML_REGEXP_MARK_NORMAL = 0,
144 XML_REGEXP_MARK_START,
145 XML_REGEXP_MARK_VISITED
146} xmlRegMarkedType;
147
148typedef struct _xmlRegRange xmlRegRange;
149typedef xmlRegRange *xmlRegRangePtr;
150
151struct _xmlRegRange {
Daniel Veillardf8b9de32003-11-24 14:27:26 +0000152 int neg; /* 0 normal, 1 not, 2 exclude */
Daniel Veillard4255d502002-04-16 15:50:10 +0000153 xmlRegAtomType type;
154 int start;
155 int end;
156 xmlChar *blockName;
157};
158
159typedef struct _xmlRegAtom xmlRegAtom;
160typedef xmlRegAtom *xmlRegAtomPtr;
161
162typedef struct _xmlAutomataState xmlRegState;
163typedef xmlRegState *xmlRegStatePtr;
164
165struct _xmlRegAtom {
166 int no;
167 xmlRegAtomType type;
168 xmlRegQuantType quant;
169 int min;
170 int max;
171
172 void *valuep;
Daniel Veillarda646cfd2002-09-17 21:50:03 +0000173 void *valuep2;
Daniel Veillard4255d502002-04-16 15:50:10 +0000174 int neg;
175 int codepoint;
176 xmlRegStatePtr start;
177 xmlRegStatePtr stop;
178 int maxRanges;
179 int nbRanges;
180 xmlRegRangePtr *ranges;
181 void *data;
182};
183
184typedef struct _xmlRegCounter xmlRegCounter;
185typedef xmlRegCounter *xmlRegCounterPtr;
186
187struct _xmlRegCounter {
188 int min;
189 int max;
190};
191
192typedef struct _xmlRegTrans xmlRegTrans;
193typedef xmlRegTrans *xmlRegTransPtr;
194
195struct _xmlRegTrans {
196 xmlRegAtomPtr atom;
197 int to;
198 int counter;
199 int count;
200};
201
202struct _xmlAutomataState {
203 xmlRegStateType type;
204 xmlRegMarkedType mark;
Daniel Veillard23e73572002-09-19 19:56:43 +0000205 xmlRegMarkedType reached;
Daniel Veillard4255d502002-04-16 15:50:10 +0000206 int no;
207
208 int maxTrans;
209 int nbTrans;
210 xmlRegTrans *trans;
211};
212
213typedef struct _xmlAutomata xmlRegParserCtxt;
214typedef xmlRegParserCtxt *xmlRegParserCtxtPtr;
215
216struct _xmlAutomata {
217 xmlChar *string;
218 xmlChar *cur;
219
220 int error;
221 int neg;
222
223 xmlRegStatePtr start;
224 xmlRegStatePtr end;
225 xmlRegStatePtr state;
226
227 xmlRegAtomPtr atom;
228
229 int maxAtoms;
230 int nbAtoms;
231 xmlRegAtomPtr *atoms;
232
233 int maxStates;
234 int nbStates;
235 xmlRegStatePtr *states;
236
237 int maxCounters;
238 int nbCounters;
239 xmlRegCounter *counters;
Daniel Veillarde19fc232002-04-22 16:01:24 +0000240
241 int determinist;
Daniel Veillard4255d502002-04-16 15:50:10 +0000242};
243
244struct _xmlRegexp {
245 xmlChar *string;
246 int nbStates;
247 xmlRegStatePtr *states;
248 int nbAtoms;
249 xmlRegAtomPtr *atoms;
250 int nbCounters;
251 xmlRegCounter *counters;
Daniel Veillarde19fc232002-04-22 16:01:24 +0000252 int determinist;
Daniel Veillard23e73572002-09-19 19:56:43 +0000253 /*
254 * That's the compact form for determinists automatas
255 */
256 int nbstates;
257 int *compact;
Daniel Veillard118aed72002-09-24 14:13:13 +0000258 void **transdata;
Daniel Veillard23e73572002-09-19 19:56:43 +0000259 int nbstrings;
260 xmlChar **stringMap;
Daniel Veillard4255d502002-04-16 15:50:10 +0000261};
262
263typedef struct _xmlRegExecRollback xmlRegExecRollback;
264typedef xmlRegExecRollback *xmlRegExecRollbackPtr;
265
266struct _xmlRegExecRollback {
267 xmlRegStatePtr state;/* the current state */
268 int index; /* the index in the input stack */
269 int nextbranch; /* the next transition to explore in that state */
270 int *counts; /* save the automate state if it has some */
271};
272
273typedef struct _xmlRegInputToken xmlRegInputToken;
274typedef xmlRegInputToken *xmlRegInputTokenPtr;
275
276struct _xmlRegInputToken {
277 xmlChar *value;
278 void *data;
279};
280
281struct _xmlRegExecCtxt {
282 int status; /* execution status != 0 indicate an error */
283 int determinist; /* did we found an inderterministic behaviour */
284 xmlRegexpPtr comp; /* the compiled regexp */
285 xmlRegExecCallbacks callback;
286 void *data;
287
288 xmlRegStatePtr state;/* the current state */
289 int transno; /* the current transition on that state */
290 int transcount; /* the number of char in char counted transitions */
291
292 /*
293 * A stack of rollback states
294 */
295 int maxRollbacks;
296 int nbRollbacks;
297 xmlRegExecRollback *rollbacks;
298
299 /*
300 * The state of the automata if any
301 */
302 int *counts;
303
304 /*
305 * The input stack
306 */
307 int inputStackMax;
308 int inputStackNr;
309 int index;
310 int *charStack;
311 const xmlChar *inputString; /* when operating on characters */
312 xmlRegInputTokenPtr inputStack;/* when operating on strings */
313
314};
315
Daniel Veillard441bc322002-04-20 17:38:48 +0000316#define REGEXP_ALL_COUNTER 0x123456
317#define REGEXP_ALL_LAX_COUNTER 0x123457
Daniel Veillard7646b182002-04-20 06:41:40 +0000318
Daniel Veillard4255d502002-04-16 15:50:10 +0000319static void xmlFAParseRegExp(xmlRegParserCtxtPtr ctxt, int top);
Daniel Veillard23e73572002-09-19 19:56:43 +0000320static void xmlRegFreeState(xmlRegStatePtr state);
321static void xmlRegFreeAtom(xmlRegAtomPtr atom);
Daniel Veillard4255d502002-04-16 15:50:10 +0000322
323/************************************************************************
Daniel Veillardff46a042003-10-08 08:53:17 +0000324 * *
325 * Regexp memory error handler *
326 * *
327 ************************************************************************/
328/**
329 * xmlRegexpErrMemory:
330 * @extra: extra informations
331 *
332 * Handle an out of memory condition
333 */
334static void
335xmlRegexpErrMemory(xmlRegParserCtxtPtr ctxt, const char *extra)
336{
337 const char *regexp = NULL;
338 if (ctxt != NULL) {
339 regexp = (const char *) ctxt->string;
340 ctxt->error = XML_ERR_NO_MEMORY;
341 }
Daniel Veillard659e71e2003-10-10 14:10:40 +0000342 __xmlRaiseError(NULL, NULL, NULL, NULL, NULL, XML_FROM_REGEXP,
Daniel Veillardff46a042003-10-08 08:53:17 +0000343 XML_ERR_NO_MEMORY, XML_ERR_FATAL, NULL, 0, extra,
344 regexp, NULL, 0, 0,
345 "Memory allocation failed : %s\n", extra);
346}
347
348/**
349 * xmlRegexpErrCompile:
350 * @extra: extra informations
351 *
352 * Handle an compilation failure
353 */
354static void
355xmlRegexpErrCompile(xmlRegParserCtxtPtr ctxt, const char *extra)
356{
357 const char *regexp = NULL;
358 int idx = 0;
359
360 if (ctxt != NULL) {
361 regexp = (const char *) ctxt->string;
362 idx = ctxt->cur - ctxt->string;
363 ctxt->error = XML_REGEXP_COMPILE_ERROR;
364 }
Daniel Veillard659e71e2003-10-10 14:10:40 +0000365 __xmlRaiseError(NULL, NULL, NULL, NULL, NULL, XML_FROM_REGEXP,
Daniel Veillardff46a042003-10-08 08:53:17 +0000366 XML_REGEXP_COMPILE_ERROR, XML_ERR_FATAL, NULL, 0, extra,
367 regexp, NULL, idx, 0,
368 "failed to compile: %s\n", extra);
369}
370
371/************************************************************************
Daniel Veillard4255d502002-04-16 15:50:10 +0000372 * *
373 * Allocation/Deallocation *
374 * *
375 ************************************************************************/
376
Daniel Veillard23e73572002-09-19 19:56:43 +0000377static int xmlFAComputesDeterminism(xmlRegParserCtxtPtr ctxt);
Daniel Veillard4255d502002-04-16 15:50:10 +0000378/**
379 * xmlRegEpxFromParse:
380 * @ctxt: the parser context used to build it
381 *
382 * Allocate a new regexp and fill it with the reult from the parser
383 *
384 * Returns the new regexp or NULL in case of error
385 */
386static xmlRegexpPtr
387xmlRegEpxFromParse(xmlRegParserCtxtPtr ctxt) {
388 xmlRegexpPtr ret;
389
390 ret = (xmlRegexpPtr) xmlMalloc(sizeof(xmlRegexp));
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000391 if (ret == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +0000392 xmlRegexpErrMemory(ctxt, "compiling regexp");
Daniel Veillard4255d502002-04-16 15:50:10 +0000393 return(NULL);
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000394 }
Daniel Veillard4255d502002-04-16 15:50:10 +0000395 memset(ret, 0, sizeof(xmlRegexp));
396 ret->string = ctxt->string;
Daniel Veillard4255d502002-04-16 15:50:10 +0000397 ret->nbStates = ctxt->nbStates;
Daniel Veillard4255d502002-04-16 15:50:10 +0000398 ret->states = ctxt->states;
Daniel Veillard4255d502002-04-16 15:50:10 +0000399 ret->nbAtoms = ctxt->nbAtoms;
Daniel Veillard4255d502002-04-16 15:50:10 +0000400 ret->atoms = ctxt->atoms;
Daniel Veillard4255d502002-04-16 15:50:10 +0000401 ret->nbCounters = ctxt->nbCounters;
Daniel Veillard4255d502002-04-16 15:50:10 +0000402 ret->counters = ctxt->counters;
Daniel Veillarde19fc232002-04-22 16:01:24 +0000403 ret->determinist = ctxt->determinist;
Daniel Veillard23e73572002-09-19 19:56:43 +0000404
405 if ((ret->determinist != 0) &&
406 (ret->nbCounters == 0) &&
Daniel Veillard118aed72002-09-24 14:13:13 +0000407 (ret->atoms != NULL) &&
Daniel Veillard23e73572002-09-19 19:56:43 +0000408 (ret->atoms[0] != NULL) &&
409 (ret->atoms[0]->type == XML_REGEXP_STRING)) {
410 int i, j, nbstates = 0, nbatoms = 0;
411 int *stateRemap;
412 int *stringRemap;
413 int *transitions;
Daniel Veillard118aed72002-09-24 14:13:13 +0000414 void **transdata;
Daniel Veillard23e73572002-09-19 19:56:43 +0000415 xmlChar **stringMap;
416 xmlChar *value;
417
418 /*
419 * Switch to a compact representation
420 * 1/ counting the effective number of states left
421 * 2/ conting the unique number of atoms, and check that
422 * they are all of the string type
423 * 3/ build a table state x atom for the transitions
424 */
425
426 stateRemap = xmlMalloc(ret->nbStates * sizeof(int));
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000427 if (stateRemap == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +0000428 xmlRegexpErrMemory(ctxt, "compiling regexp");
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000429 xmlFree(ret);
430 return(NULL);
431 }
Daniel Veillard23e73572002-09-19 19:56:43 +0000432 for (i = 0;i < ret->nbStates;i++) {
433 if (ret->states[i] != NULL) {
434 stateRemap[i] = nbstates;
435 nbstates++;
436 } else {
437 stateRemap[i] = -1;
438 }
439 }
440#ifdef DEBUG_COMPACTION
441 printf("Final: %d states\n", nbstates);
442#endif
443 stringMap = xmlMalloc(ret->nbAtoms * sizeof(char *));
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000444 if (stringMap == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +0000445 xmlRegexpErrMemory(ctxt, "compiling regexp");
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000446 xmlFree(stateRemap);
447 xmlFree(ret);
448 return(NULL);
449 }
Daniel Veillard23e73572002-09-19 19:56:43 +0000450 stringRemap = xmlMalloc(ret->nbAtoms * sizeof(int));
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000451 if (stringRemap == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +0000452 xmlRegexpErrMemory(ctxt, "compiling regexp");
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000453 xmlFree(stringMap);
454 xmlFree(stateRemap);
455 xmlFree(ret);
456 return(NULL);
457 }
Daniel Veillard23e73572002-09-19 19:56:43 +0000458 for (i = 0;i < ret->nbAtoms;i++) {
459 if ((ret->atoms[i]->type == XML_REGEXP_STRING) &&
460 (ret->atoms[i]->quant == XML_REGEXP_QUANT_ONCE)) {
461 value = ret->atoms[i]->valuep;
462 for (j = 0;j < nbatoms;j++) {
463 if (xmlStrEqual(stringMap[j], value)) {
464 stringRemap[i] = j;
465 break;
466 }
467 }
468 if (j >= nbatoms) {
469 stringRemap[i] = nbatoms;
470 stringMap[nbatoms] = xmlStrdup(value);
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000471 if (stringMap[nbatoms] == NULL) {
472 for (i = 0;i < nbatoms;i++)
473 xmlFree(stringMap[i]);
474 xmlFree(stringRemap);
475 xmlFree(stringMap);
476 xmlFree(stateRemap);
477 xmlFree(ret);
478 return(NULL);
479 }
Daniel Veillard23e73572002-09-19 19:56:43 +0000480 nbatoms++;
481 }
482 } else {
483 xmlFree(stateRemap);
484 xmlFree(stringRemap);
485 for (i = 0;i < nbatoms;i++)
486 xmlFree(stringMap[i]);
487 xmlFree(stringMap);
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000488 xmlFree(ret);
489 return(NULL);
Daniel Veillard23e73572002-09-19 19:56:43 +0000490 }
491 }
492#ifdef DEBUG_COMPACTION
493 printf("Final: %d atoms\n", nbatoms);
494#endif
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000495 transitions = (int *) xmlMalloc((nbstates + 1) *
496 (nbatoms + 1) * sizeof(int));
497 if (transitions == NULL) {
498 xmlFree(stateRemap);
499 xmlFree(stringRemap);
500 xmlFree(stringMap);
501 xmlFree(ret);
502 return(NULL);
503 }
504 memset(transitions, 0, (nbstates + 1) * (nbatoms + 1) * sizeof(int));
Daniel Veillard23e73572002-09-19 19:56:43 +0000505
506 /*
507 * Allocate the transition table. The first entry for each
508 * state correspond to the state type.
509 */
Daniel Veillard118aed72002-09-24 14:13:13 +0000510 transdata = NULL;
Daniel Veillard23e73572002-09-19 19:56:43 +0000511
512 for (i = 0;i < ret->nbStates;i++) {
513 int stateno, atomno, targetno, prev;
514 xmlRegStatePtr state;
515 xmlRegTransPtr trans;
516
517 stateno = stateRemap[i];
518 if (stateno == -1)
519 continue;
520 state = ret->states[i];
521
522 transitions[stateno * (nbatoms + 1)] = state->type;
523
524 for (j = 0;j < state->nbTrans;j++) {
525 trans = &(state->trans[j]);
526 if ((trans->to == -1) || (trans->atom == NULL))
527 continue;
528 atomno = stringRemap[trans->atom->no];
Daniel Veillard118aed72002-09-24 14:13:13 +0000529 if ((trans->atom->data != NULL) && (transdata == NULL)) {
530 transdata = (void **) xmlMalloc(nbstates * nbatoms *
531 sizeof(void *));
532 if (transdata != NULL)
533 memset(transdata, 0,
534 nbstates * nbatoms * sizeof(void *));
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000535 else {
Daniel Veillardff46a042003-10-08 08:53:17 +0000536 xmlRegexpErrMemory(ctxt, "compiling regexp");
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000537 break;
538 }
Daniel Veillard118aed72002-09-24 14:13:13 +0000539 }
Daniel Veillard23e73572002-09-19 19:56:43 +0000540 targetno = stateRemap[trans->to];
541 /*
542 * if the same atome can generate transition to 2 different
543 * states then it means the automata is not determinist and
544 * the compact form can't be used !
545 */
546 prev = transitions[stateno * (nbatoms + 1) + atomno + 1];
547 if (prev != 0) {
548 if (prev != targetno + 1) {
Daniel Veillard23e73572002-09-19 19:56:43 +0000549 ret->determinist = 0;
550#ifdef DEBUG_COMPACTION
551 printf("Indet: state %d trans %d, atom %d to %d : %d to %d\n",
552 i, j, trans->atom->no, trans->to, atomno, targetno);
553 printf(" previous to is %d\n", prev);
554#endif
555 ret->determinist = 0;
Daniel Veillard118aed72002-09-24 14:13:13 +0000556 if (transdata != NULL)
557 xmlFree(transdata);
Daniel Veillard23e73572002-09-19 19:56:43 +0000558 xmlFree(transitions);
559 xmlFree(stateRemap);
560 xmlFree(stringRemap);
561 for (i = 0;i < nbatoms;i++)
562 xmlFree(stringMap[i]);
563 xmlFree(stringMap);
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000564 goto not_determ;
Daniel Veillard23e73572002-09-19 19:56:43 +0000565 }
566 } else {
567#if 0
568 printf("State %d trans %d: atom %d to %d : %d to %d\n",
569 i, j, trans->atom->no, trans->to, atomno, targetno);
570#endif
571 transitions[stateno * (nbatoms + 1) + atomno + 1] =
Daniel Veillard118aed72002-09-24 14:13:13 +0000572 targetno + 1; /* to avoid 0 */
573 if (transdata != NULL)
574 transdata[stateno * nbatoms + atomno] =
575 trans->atom->data;
Daniel Veillard23e73572002-09-19 19:56:43 +0000576 }
577 }
578 }
579 ret->determinist = 1;
580#ifdef DEBUG_COMPACTION
581 /*
582 * Debug
583 */
584 for (i = 0;i < nbstates;i++) {
585 for (j = 0;j < nbatoms + 1;j++) {
586 printf("%02d ", transitions[i * (nbatoms + 1) + j]);
587 }
588 printf("\n");
589 }
590 printf("\n");
591#endif
592 /*
593 * Cleanup of the old data
594 */
595 if (ret->states != NULL) {
596 for (i = 0;i < ret->nbStates;i++)
597 xmlRegFreeState(ret->states[i]);
598 xmlFree(ret->states);
599 }
600 ret->states = NULL;
601 ret->nbStates = 0;
602 if (ret->atoms != NULL) {
603 for (i = 0;i < ret->nbAtoms;i++)
604 xmlRegFreeAtom(ret->atoms[i]);
605 xmlFree(ret->atoms);
606 }
607 ret->atoms = NULL;
608 ret->nbAtoms = 0;
609
610 ret->compact = transitions;
Daniel Veillard118aed72002-09-24 14:13:13 +0000611 ret->transdata = transdata;
Daniel Veillard23e73572002-09-19 19:56:43 +0000612 ret->stringMap = stringMap;
613 ret->nbstrings = nbatoms;
614 ret->nbstates = nbstates;
615 xmlFree(stateRemap);
616 xmlFree(stringRemap);
617 }
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000618not_determ:
619 ctxt->string = NULL;
620 ctxt->nbStates = 0;
621 ctxt->states = NULL;
622 ctxt->nbAtoms = 0;
623 ctxt->atoms = NULL;
624 ctxt->nbCounters = 0;
625 ctxt->counters = NULL;
Daniel Veillard4255d502002-04-16 15:50:10 +0000626 return(ret);
627}
628
629/**
630 * xmlRegNewParserCtxt:
631 * @string: the string to parse
632 *
633 * Allocate a new regexp parser context
634 *
635 * Returns the new context or NULL in case of error
636 */
637static xmlRegParserCtxtPtr
638xmlRegNewParserCtxt(const xmlChar *string) {
639 xmlRegParserCtxtPtr ret;
640
641 ret = (xmlRegParserCtxtPtr) xmlMalloc(sizeof(xmlRegParserCtxt));
642 if (ret == NULL)
643 return(NULL);
644 memset(ret, 0, sizeof(xmlRegParserCtxt));
645 if (string != NULL)
646 ret->string = xmlStrdup(string);
647 ret->cur = ret->string;
648 ret->neg = 0;
649 ret->error = 0;
Daniel Veillarde19fc232002-04-22 16:01:24 +0000650 ret->determinist = -1;
Daniel Veillard4255d502002-04-16 15:50:10 +0000651 return(ret);
652}
653
654/**
655 * xmlRegNewRange:
656 * @ctxt: the regexp parser context
657 * @neg: is that negative
658 * @type: the type of range
659 * @start: the start codepoint
660 * @end: the end codepoint
661 *
662 * Allocate a new regexp range
663 *
664 * Returns the new range or NULL in case of error
665 */
666static xmlRegRangePtr
667xmlRegNewRange(xmlRegParserCtxtPtr ctxt,
668 int neg, xmlRegAtomType type, int start, int end) {
669 xmlRegRangePtr ret;
670
671 ret = (xmlRegRangePtr) xmlMalloc(sizeof(xmlRegRange));
672 if (ret == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +0000673 xmlRegexpErrMemory(ctxt, "allocating range");
Daniel Veillard4255d502002-04-16 15:50:10 +0000674 return(NULL);
675 }
676 ret->neg = neg;
677 ret->type = type;
678 ret->start = start;
679 ret->end = end;
680 return(ret);
681}
682
683/**
684 * xmlRegFreeRange:
685 * @range: the regexp range
686 *
687 * Free a regexp range
688 */
689static void
690xmlRegFreeRange(xmlRegRangePtr range) {
691 if (range == NULL)
692 return;
693
694 if (range->blockName != NULL)
695 xmlFree(range->blockName);
696 xmlFree(range);
697}
698
699/**
700 * xmlRegNewAtom:
701 * @ctxt: the regexp parser context
702 * @type: the type of atom
703 *
704 * Allocate a new regexp range
705 *
706 * Returns the new atom or NULL in case of error
707 */
708static xmlRegAtomPtr
709xmlRegNewAtom(xmlRegParserCtxtPtr ctxt, xmlRegAtomType type) {
710 xmlRegAtomPtr ret;
711
712 ret = (xmlRegAtomPtr) xmlMalloc(sizeof(xmlRegAtom));
713 if (ret == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +0000714 xmlRegexpErrMemory(ctxt, "allocating atom");
Daniel Veillard4255d502002-04-16 15:50:10 +0000715 return(NULL);
716 }
717 memset(ret, 0, sizeof(xmlRegAtom));
718 ret->type = type;
719 ret->quant = XML_REGEXP_QUANT_ONCE;
720 ret->min = 0;
721 ret->max = 0;
722 return(ret);
723}
724
725/**
726 * xmlRegFreeAtom:
727 * @atom: the regexp atom
728 *
729 * Free a regexp atom
730 */
731static void
732xmlRegFreeAtom(xmlRegAtomPtr atom) {
733 int i;
734
735 if (atom == NULL)
736 return;
737
738 for (i = 0;i < atom->nbRanges;i++)
739 xmlRegFreeRange(atom->ranges[i]);
740 if (atom->ranges != NULL)
741 xmlFree(atom->ranges);
742 if (atom->type == XML_REGEXP_STRING)
743 xmlFree(atom->valuep);
744 xmlFree(atom);
745}
746
747static xmlRegStatePtr
748xmlRegNewState(xmlRegParserCtxtPtr ctxt) {
749 xmlRegStatePtr ret;
750
751 ret = (xmlRegStatePtr) xmlMalloc(sizeof(xmlRegState));
752 if (ret == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +0000753 xmlRegexpErrMemory(ctxt, "allocating state");
Daniel Veillard4255d502002-04-16 15:50:10 +0000754 return(NULL);
755 }
756 memset(ret, 0, sizeof(xmlRegState));
757 ret->type = XML_REGEXP_TRANS_STATE;
758 ret->mark = XML_REGEXP_MARK_NORMAL;
759 return(ret);
760}
761
762/**
763 * xmlRegFreeState:
764 * @state: the regexp state
765 *
766 * Free a regexp state
767 */
768static void
769xmlRegFreeState(xmlRegStatePtr state) {
770 if (state == NULL)
771 return;
772
773 if (state->trans != NULL)
774 xmlFree(state->trans);
775 xmlFree(state);
776}
777
778/**
779 * xmlRegFreeParserCtxt:
780 * @ctxt: the regexp parser context
781 *
782 * Free a regexp parser context
783 */
784static void
785xmlRegFreeParserCtxt(xmlRegParserCtxtPtr ctxt) {
786 int i;
787 if (ctxt == NULL)
788 return;
789
790 if (ctxt->string != NULL)
791 xmlFree(ctxt->string);
792 if (ctxt->states != NULL) {
793 for (i = 0;i < ctxt->nbStates;i++)
794 xmlRegFreeState(ctxt->states[i]);
795 xmlFree(ctxt->states);
796 }
797 if (ctxt->atoms != NULL) {
798 for (i = 0;i < ctxt->nbAtoms;i++)
799 xmlRegFreeAtom(ctxt->atoms[i]);
800 xmlFree(ctxt->atoms);
801 }
802 if (ctxt->counters != NULL)
803 xmlFree(ctxt->counters);
804 xmlFree(ctxt);
805}
806
807/************************************************************************
808 * *
809 * Display of Data structures *
810 * *
811 ************************************************************************/
812
813static void
814xmlRegPrintAtomType(FILE *output, xmlRegAtomType type) {
815 switch (type) {
816 case XML_REGEXP_EPSILON:
817 fprintf(output, "epsilon "); break;
818 case XML_REGEXP_CHARVAL:
819 fprintf(output, "charval "); break;
820 case XML_REGEXP_RANGES:
821 fprintf(output, "ranges "); break;
822 case XML_REGEXP_SUBREG:
823 fprintf(output, "subexpr "); break;
824 case XML_REGEXP_STRING:
825 fprintf(output, "string "); break;
826 case XML_REGEXP_ANYCHAR:
827 fprintf(output, "anychar "); break;
828 case XML_REGEXP_ANYSPACE:
829 fprintf(output, "anyspace "); break;
830 case XML_REGEXP_NOTSPACE:
831 fprintf(output, "notspace "); break;
832 case XML_REGEXP_INITNAME:
833 fprintf(output, "initname "); break;
834 case XML_REGEXP_NOTINITNAME:
835 fprintf(output, "notinitname "); break;
836 case XML_REGEXP_NAMECHAR:
837 fprintf(output, "namechar "); break;
838 case XML_REGEXP_NOTNAMECHAR:
839 fprintf(output, "notnamechar "); break;
840 case XML_REGEXP_DECIMAL:
841 fprintf(output, "decimal "); break;
842 case XML_REGEXP_NOTDECIMAL:
843 fprintf(output, "notdecimal "); break;
844 case XML_REGEXP_REALCHAR:
845 fprintf(output, "realchar "); break;
846 case XML_REGEXP_NOTREALCHAR:
847 fprintf(output, "notrealchar "); break;
848 case XML_REGEXP_LETTER:
849 fprintf(output, "LETTER "); break;
850 case XML_REGEXP_LETTER_UPPERCASE:
851 fprintf(output, "LETTER_UPPERCASE "); break;
852 case XML_REGEXP_LETTER_LOWERCASE:
853 fprintf(output, "LETTER_LOWERCASE "); break;
854 case XML_REGEXP_LETTER_TITLECASE:
855 fprintf(output, "LETTER_TITLECASE "); break;
856 case XML_REGEXP_LETTER_MODIFIER:
857 fprintf(output, "LETTER_MODIFIER "); break;
858 case XML_REGEXP_LETTER_OTHERS:
859 fprintf(output, "LETTER_OTHERS "); break;
860 case XML_REGEXP_MARK:
861 fprintf(output, "MARK "); break;
862 case XML_REGEXP_MARK_NONSPACING:
863 fprintf(output, "MARK_NONSPACING "); break;
864 case XML_REGEXP_MARK_SPACECOMBINING:
865 fprintf(output, "MARK_SPACECOMBINING "); break;
866 case XML_REGEXP_MARK_ENCLOSING:
867 fprintf(output, "MARK_ENCLOSING "); break;
868 case XML_REGEXP_NUMBER:
869 fprintf(output, "NUMBER "); break;
870 case XML_REGEXP_NUMBER_DECIMAL:
871 fprintf(output, "NUMBER_DECIMAL "); break;
872 case XML_REGEXP_NUMBER_LETTER:
873 fprintf(output, "NUMBER_LETTER "); break;
874 case XML_REGEXP_NUMBER_OTHERS:
875 fprintf(output, "NUMBER_OTHERS "); break;
876 case XML_REGEXP_PUNCT:
877 fprintf(output, "PUNCT "); break;
878 case XML_REGEXP_PUNCT_CONNECTOR:
879 fprintf(output, "PUNCT_CONNECTOR "); break;
880 case XML_REGEXP_PUNCT_DASH:
881 fprintf(output, "PUNCT_DASH "); break;
882 case XML_REGEXP_PUNCT_OPEN:
883 fprintf(output, "PUNCT_OPEN "); break;
884 case XML_REGEXP_PUNCT_CLOSE:
885 fprintf(output, "PUNCT_CLOSE "); break;
886 case XML_REGEXP_PUNCT_INITQUOTE:
887 fprintf(output, "PUNCT_INITQUOTE "); break;
888 case XML_REGEXP_PUNCT_FINQUOTE:
889 fprintf(output, "PUNCT_FINQUOTE "); break;
890 case XML_REGEXP_PUNCT_OTHERS:
891 fprintf(output, "PUNCT_OTHERS "); break;
892 case XML_REGEXP_SEPAR:
893 fprintf(output, "SEPAR "); break;
894 case XML_REGEXP_SEPAR_SPACE:
895 fprintf(output, "SEPAR_SPACE "); break;
896 case XML_REGEXP_SEPAR_LINE:
897 fprintf(output, "SEPAR_LINE "); break;
898 case XML_REGEXP_SEPAR_PARA:
899 fprintf(output, "SEPAR_PARA "); break;
900 case XML_REGEXP_SYMBOL:
901 fprintf(output, "SYMBOL "); break;
902 case XML_REGEXP_SYMBOL_MATH:
903 fprintf(output, "SYMBOL_MATH "); break;
904 case XML_REGEXP_SYMBOL_CURRENCY:
905 fprintf(output, "SYMBOL_CURRENCY "); break;
906 case XML_REGEXP_SYMBOL_MODIFIER:
907 fprintf(output, "SYMBOL_MODIFIER "); break;
908 case XML_REGEXP_SYMBOL_OTHERS:
909 fprintf(output, "SYMBOL_OTHERS "); break;
910 case XML_REGEXP_OTHER:
911 fprintf(output, "OTHER "); break;
912 case XML_REGEXP_OTHER_CONTROL:
913 fprintf(output, "OTHER_CONTROL "); break;
914 case XML_REGEXP_OTHER_FORMAT:
915 fprintf(output, "OTHER_FORMAT "); break;
916 case XML_REGEXP_OTHER_PRIVATE:
917 fprintf(output, "OTHER_PRIVATE "); break;
918 case XML_REGEXP_OTHER_NA:
919 fprintf(output, "OTHER_NA "); break;
920 case XML_REGEXP_BLOCK_NAME:
921 fprintf(output, "BLOCK "); break;
922 }
923}
924
925static void
926xmlRegPrintQuantType(FILE *output, xmlRegQuantType type) {
927 switch (type) {
928 case XML_REGEXP_QUANT_EPSILON:
929 fprintf(output, "epsilon "); break;
930 case XML_REGEXP_QUANT_ONCE:
931 fprintf(output, "once "); break;
932 case XML_REGEXP_QUANT_OPT:
933 fprintf(output, "? "); break;
934 case XML_REGEXP_QUANT_MULT:
935 fprintf(output, "* "); break;
936 case XML_REGEXP_QUANT_PLUS:
937 fprintf(output, "+ "); break;
938 case XML_REGEXP_QUANT_RANGE:
939 fprintf(output, "range "); break;
Daniel Veillard7646b182002-04-20 06:41:40 +0000940 case XML_REGEXP_QUANT_ONCEONLY:
941 fprintf(output, "onceonly "); break;
942 case XML_REGEXP_QUANT_ALL:
943 fprintf(output, "all "); break;
Daniel Veillard4255d502002-04-16 15:50:10 +0000944 }
945}
946static void
947xmlRegPrintRange(FILE *output, xmlRegRangePtr range) {
948 fprintf(output, " range: ");
949 if (range->neg)
950 fprintf(output, "negative ");
951 xmlRegPrintAtomType(output, range->type);
952 fprintf(output, "%c - %c\n", range->start, range->end);
953}
954
955static void
956xmlRegPrintAtom(FILE *output, xmlRegAtomPtr atom) {
957 fprintf(output, " atom: ");
958 if (atom == NULL) {
959 fprintf(output, "NULL\n");
960 return;
961 }
962 xmlRegPrintAtomType(output, atom->type);
963 xmlRegPrintQuantType(output, atom->quant);
964 if (atom->quant == XML_REGEXP_QUANT_RANGE)
965 fprintf(output, "%d-%d ", atom->min, atom->max);
966 if (atom->type == XML_REGEXP_STRING)
967 fprintf(output, "'%s' ", (char *) atom->valuep);
968 if (atom->type == XML_REGEXP_CHARVAL)
969 fprintf(output, "char %c\n", atom->codepoint);
970 else if (atom->type == XML_REGEXP_RANGES) {
971 int i;
972 fprintf(output, "%d entries\n", atom->nbRanges);
973 for (i = 0; i < atom->nbRanges;i++)
974 xmlRegPrintRange(output, atom->ranges[i]);
975 } else if (atom->type == XML_REGEXP_SUBREG) {
976 fprintf(output, "start %d end %d\n", atom->start->no, atom->stop->no);
977 } else {
978 fprintf(output, "\n");
979 }
980}
981
982static void
983xmlRegPrintTrans(FILE *output, xmlRegTransPtr trans) {
984 fprintf(output, " trans: ");
985 if (trans == NULL) {
986 fprintf(output, "NULL\n");
987 return;
988 }
989 if (trans->to < 0) {
990 fprintf(output, "removed\n");
991 return;
992 }
993 if (trans->counter >= 0) {
994 fprintf(output, "counted %d, ", trans->counter);
995 }
Daniel Veillard8a001f62002-04-20 07:24:11 +0000996 if (trans->count == REGEXP_ALL_COUNTER) {
997 fprintf(output, "all transition, ");
998 } else if (trans->count >= 0) {
Daniel Veillard4255d502002-04-16 15:50:10 +0000999 fprintf(output, "count based %d, ", trans->count);
1000 }
1001 if (trans->atom == NULL) {
1002 fprintf(output, "epsilon to %d\n", trans->to);
1003 return;
1004 }
1005 if (trans->atom->type == XML_REGEXP_CHARVAL)
1006 fprintf(output, "char %c ", trans->atom->codepoint);
1007 fprintf(output, "atom %d, to %d\n", trans->atom->no, trans->to);
1008}
1009
1010static void
1011xmlRegPrintState(FILE *output, xmlRegStatePtr state) {
1012 int i;
1013
1014 fprintf(output, " state: ");
1015 if (state == NULL) {
1016 fprintf(output, "NULL\n");
1017 return;
1018 }
1019 if (state->type == XML_REGEXP_START_STATE)
1020 fprintf(output, "START ");
1021 if (state->type == XML_REGEXP_FINAL_STATE)
1022 fprintf(output, "FINAL ");
1023
1024 fprintf(output, "%d, %d transitions:\n", state->no, state->nbTrans);
1025 for (i = 0;i < state->nbTrans; i++) {
1026 xmlRegPrintTrans(output, &(state->trans[i]));
1027 }
1028}
1029
Daniel Veillard23e73572002-09-19 19:56:43 +00001030#ifdef DEBUG_REGEXP_GRAPH
Daniel Veillard4255d502002-04-16 15:50:10 +00001031static void
1032xmlRegPrintCtxt(FILE *output, xmlRegParserCtxtPtr ctxt) {
1033 int i;
1034
1035 fprintf(output, " ctxt: ");
1036 if (ctxt == NULL) {
1037 fprintf(output, "NULL\n");
1038 return;
1039 }
1040 fprintf(output, "'%s' ", ctxt->string);
1041 if (ctxt->error)
1042 fprintf(output, "error ");
1043 if (ctxt->neg)
1044 fprintf(output, "neg ");
1045 fprintf(output, "\n");
1046 fprintf(output, "%d atoms:\n", ctxt->nbAtoms);
1047 for (i = 0;i < ctxt->nbAtoms; i++) {
1048 fprintf(output, " %02d ", i);
1049 xmlRegPrintAtom(output, ctxt->atoms[i]);
1050 }
1051 if (ctxt->atom != NULL) {
1052 fprintf(output, "current atom:\n");
1053 xmlRegPrintAtom(output, ctxt->atom);
1054 }
1055 fprintf(output, "%d states:", ctxt->nbStates);
1056 if (ctxt->start != NULL)
1057 fprintf(output, " start: %d", ctxt->start->no);
1058 if (ctxt->end != NULL)
1059 fprintf(output, " end: %d", ctxt->end->no);
1060 fprintf(output, "\n");
1061 for (i = 0;i < ctxt->nbStates; i++) {
1062 xmlRegPrintState(output, ctxt->states[i]);
1063 }
1064 fprintf(output, "%d counters:\n", ctxt->nbCounters);
1065 for (i = 0;i < ctxt->nbCounters; i++) {
1066 fprintf(output, " %d: min %d max %d\n", i, ctxt->counters[i].min,
1067 ctxt->counters[i].max);
1068 }
1069}
Daniel Veillard23e73572002-09-19 19:56:43 +00001070#endif
Daniel Veillard4255d502002-04-16 15:50:10 +00001071
1072/************************************************************************
1073 * *
1074 * Finite Automata structures manipulations *
1075 * *
1076 ************************************************************************/
1077
1078static void
1079xmlRegAtomAddRange(xmlRegParserCtxtPtr ctxt, xmlRegAtomPtr atom,
1080 int neg, xmlRegAtomType type, int start, int end,
1081 xmlChar *blockName) {
1082 xmlRegRangePtr range;
1083
1084 if (atom == NULL) {
1085 ERROR("add range: atom is NULL");
1086 return;
1087 }
1088 if (atom->type != XML_REGEXP_RANGES) {
1089 ERROR("add range: atom is not ranges");
1090 return;
1091 }
1092 if (atom->maxRanges == 0) {
1093 atom->maxRanges = 4;
1094 atom->ranges = (xmlRegRangePtr *) xmlMalloc(atom->maxRanges *
1095 sizeof(xmlRegRangePtr));
1096 if (atom->ranges == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00001097 xmlRegexpErrMemory(ctxt, "adding ranges");
Daniel Veillard4255d502002-04-16 15:50:10 +00001098 atom->maxRanges = 0;
1099 return;
1100 }
1101 } else if (atom->nbRanges >= atom->maxRanges) {
1102 xmlRegRangePtr *tmp;
1103 atom->maxRanges *= 2;
1104 tmp = (xmlRegRangePtr *) xmlRealloc(atom->ranges, atom->maxRanges *
1105 sizeof(xmlRegRangePtr));
1106 if (tmp == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00001107 xmlRegexpErrMemory(ctxt, "adding ranges");
Daniel Veillard4255d502002-04-16 15:50:10 +00001108 atom->maxRanges /= 2;
1109 return;
1110 }
1111 atom->ranges = tmp;
1112 }
1113 range = xmlRegNewRange(ctxt, neg, type, start, end);
1114 if (range == NULL)
1115 return;
1116 range->blockName = blockName;
1117 atom->ranges[atom->nbRanges++] = range;
1118
1119}
1120
1121static int
1122xmlRegGetCounter(xmlRegParserCtxtPtr ctxt) {
1123 if (ctxt->maxCounters == 0) {
1124 ctxt->maxCounters = 4;
1125 ctxt->counters = (xmlRegCounter *) xmlMalloc(ctxt->maxCounters *
1126 sizeof(xmlRegCounter));
1127 if (ctxt->counters == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00001128 xmlRegexpErrMemory(ctxt, "allocating counter");
Daniel Veillard4255d502002-04-16 15:50:10 +00001129 ctxt->maxCounters = 0;
1130 return(-1);
1131 }
1132 } else if (ctxt->nbCounters >= ctxt->maxCounters) {
1133 xmlRegCounter *tmp;
1134 ctxt->maxCounters *= 2;
1135 tmp = (xmlRegCounter *) xmlRealloc(ctxt->counters, ctxt->maxCounters *
1136 sizeof(xmlRegCounter));
1137 if (tmp == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00001138 xmlRegexpErrMemory(ctxt, "allocating counter");
Daniel Veillard4255d502002-04-16 15:50:10 +00001139 ctxt->maxCounters /= 2;
1140 return(-1);
1141 }
1142 ctxt->counters = tmp;
1143 }
1144 ctxt->counters[ctxt->nbCounters].min = -1;
1145 ctxt->counters[ctxt->nbCounters].max = -1;
1146 return(ctxt->nbCounters++);
1147}
1148
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001149static int
Daniel Veillard4255d502002-04-16 15:50:10 +00001150xmlRegAtomPush(xmlRegParserCtxtPtr ctxt, xmlRegAtomPtr atom) {
1151 if (atom == NULL) {
1152 ERROR("atom push: atom is NULL");
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001153 return(-1);
Daniel Veillard4255d502002-04-16 15:50:10 +00001154 }
1155 if (ctxt->maxAtoms == 0) {
1156 ctxt->maxAtoms = 4;
1157 ctxt->atoms = (xmlRegAtomPtr *) xmlMalloc(ctxt->maxAtoms *
1158 sizeof(xmlRegAtomPtr));
1159 if (ctxt->atoms == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00001160 xmlRegexpErrMemory(ctxt, "pushing atom");
Daniel Veillard4255d502002-04-16 15:50:10 +00001161 ctxt->maxAtoms = 0;
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001162 return(-1);
Daniel Veillard4255d502002-04-16 15:50:10 +00001163 }
1164 } else if (ctxt->nbAtoms >= ctxt->maxAtoms) {
1165 xmlRegAtomPtr *tmp;
1166 ctxt->maxAtoms *= 2;
1167 tmp = (xmlRegAtomPtr *) xmlRealloc(ctxt->atoms, ctxt->maxAtoms *
1168 sizeof(xmlRegAtomPtr));
1169 if (tmp == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00001170 xmlRegexpErrMemory(ctxt, "allocating counter");
Daniel Veillard4255d502002-04-16 15:50:10 +00001171 ctxt->maxAtoms /= 2;
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001172 return(-1);
Daniel Veillard4255d502002-04-16 15:50:10 +00001173 }
1174 ctxt->atoms = tmp;
1175 }
1176 atom->no = ctxt->nbAtoms;
1177 ctxt->atoms[ctxt->nbAtoms++] = atom;
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001178 return(0);
Daniel Veillard4255d502002-04-16 15:50:10 +00001179}
1180
1181static void
1182xmlRegStateAddTrans(xmlRegParserCtxtPtr ctxt, xmlRegStatePtr state,
1183 xmlRegAtomPtr atom, xmlRegStatePtr target,
1184 int counter, int count) {
1185 if (state == NULL) {
1186 ERROR("add state: state is NULL");
1187 return;
1188 }
1189 if (target == NULL) {
1190 ERROR("add state: target is NULL");
1191 return;
1192 }
1193 if (state->maxTrans == 0) {
1194 state->maxTrans = 4;
1195 state->trans = (xmlRegTrans *) xmlMalloc(state->maxTrans *
1196 sizeof(xmlRegTrans));
1197 if (state->trans == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00001198 xmlRegexpErrMemory(ctxt, "adding transition");
Daniel Veillard4255d502002-04-16 15:50:10 +00001199 state->maxTrans = 0;
1200 return;
1201 }
1202 } else if (state->nbTrans >= state->maxTrans) {
1203 xmlRegTrans *tmp;
1204 state->maxTrans *= 2;
1205 tmp = (xmlRegTrans *) xmlRealloc(state->trans, state->maxTrans *
1206 sizeof(xmlRegTrans));
1207 if (tmp == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00001208 xmlRegexpErrMemory(ctxt, "adding transition");
Daniel Veillard4255d502002-04-16 15:50:10 +00001209 state->maxTrans /= 2;
1210 return;
1211 }
1212 state->trans = tmp;
1213 }
1214#ifdef DEBUG_REGEXP_GRAPH
1215 printf("Add trans from %d to %d ", state->no, target->no);
Daniel Veillard8a001f62002-04-20 07:24:11 +00001216 if (count == REGEXP_ALL_COUNTER)
Daniel Veillard2cbf5962004-03-31 15:50:43 +00001217 printf("all transition\n");
Daniel Veillard4402ab42002-09-12 16:02:56 +00001218 else if (count >= 0)
Daniel Veillard2cbf5962004-03-31 15:50:43 +00001219 printf("count based %d\n", count);
Daniel Veillard4255d502002-04-16 15:50:10 +00001220 else if (counter >= 0)
Daniel Veillard2cbf5962004-03-31 15:50:43 +00001221 printf("counted %d\n", counter);
Daniel Veillard4255d502002-04-16 15:50:10 +00001222 else if (atom == NULL)
Daniel Veillard2cbf5962004-03-31 15:50:43 +00001223 printf("epsilon transition\n");
1224 else if (atom != NULL)
1225 xmlRegPrintAtom(stdout, atom);
Daniel Veillard4255d502002-04-16 15:50:10 +00001226#endif
1227
1228 state->trans[state->nbTrans].atom = atom;
1229 state->trans[state->nbTrans].to = target->no;
1230 state->trans[state->nbTrans].counter = counter;
1231 state->trans[state->nbTrans].count = count;
1232 state->nbTrans++;
1233}
1234
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001235static int
Daniel Veillard4255d502002-04-16 15:50:10 +00001236xmlRegStatePush(xmlRegParserCtxtPtr ctxt, xmlRegStatePtr state) {
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001237 if (state == NULL) return(-1);
Daniel Veillard4255d502002-04-16 15:50:10 +00001238 if (ctxt->maxStates == 0) {
1239 ctxt->maxStates = 4;
1240 ctxt->states = (xmlRegStatePtr *) xmlMalloc(ctxt->maxStates *
1241 sizeof(xmlRegStatePtr));
1242 if (ctxt->states == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00001243 xmlRegexpErrMemory(ctxt, "adding state");
Daniel Veillard4255d502002-04-16 15:50:10 +00001244 ctxt->maxStates = 0;
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001245 return(-1);
Daniel Veillard4255d502002-04-16 15:50:10 +00001246 }
1247 } else if (ctxt->nbStates >= ctxt->maxStates) {
1248 xmlRegStatePtr *tmp;
1249 ctxt->maxStates *= 2;
1250 tmp = (xmlRegStatePtr *) xmlRealloc(ctxt->states, ctxt->maxStates *
1251 sizeof(xmlRegStatePtr));
1252 if (tmp == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00001253 xmlRegexpErrMemory(ctxt, "adding state");
Daniel Veillard4255d502002-04-16 15:50:10 +00001254 ctxt->maxStates /= 2;
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001255 return(-1);
Daniel Veillard4255d502002-04-16 15:50:10 +00001256 }
1257 ctxt->states = tmp;
1258 }
1259 state->no = ctxt->nbStates;
1260 ctxt->states[ctxt->nbStates++] = state;
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001261 return(0);
Daniel Veillard4255d502002-04-16 15:50:10 +00001262}
1263
1264/**
Daniel Veillard7646b182002-04-20 06:41:40 +00001265 * xmlFAGenerateAllTransition:
Daniel Veillard441bc322002-04-20 17:38:48 +00001266 * @ctxt: a regexp parser context
1267 * @from: the from state
1268 * @to: the target state or NULL for building a new one
1269 * @lax:
Daniel Veillard7646b182002-04-20 06:41:40 +00001270 *
1271 */
1272static void
1273xmlFAGenerateAllTransition(xmlRegParserCtxtPtr ctxt,
Daniel Veillard441bc322002-04-20 17:38:48 +00001274 xmlRegStatePtr from, xmlRegStatePtr to,
1275 int lax) {
Daniel Veillard7646b182002-04-20 06:41:40 +00001276 if (to == NULL) {
1277 to = xmlRegNewState(ctxt);
1278 xmlRegStatePush(ctxt, to);
1279 ctxt->state = to;
1280 }
Daniel Veillard441bc322002-04-20 17:38:48 +00001281 if (lax)
1282 xmlRegStateAddTrans(ctxt, from, NULL, to, -1, REGEXP_ALL_LAX_COUNTER);
1283 else
1284 xmlRegStateAddTrans(ctxt, from, NULL, to, -1, REGEXP_ALL_COUNTER);
Daniel Veillard7646b182002-04-20 06:41:40 +00001285}
1286
1287/**
Daniel Veillard4255d502002-04-16 15:50:10 +00001288 * xmlFAGenerateEpsilonTransition:
Daniel Veillard441bc322002-04-20 17:38:48 +00001289 * @ctxt: a regexp parser context
1290 * @from: the from state
1291 * @to: the target state or NULL for building a new one
Daniel Veillard4255d502002-04-16 15:50:10 +00001292 *
1293 */
1294static void
1295xmlFAGenerateEpsilonTransition(xmlRegParserCtxtPtr ctxt,
1296 xmlRegStatePtr from, xmlRegStatePtr to) {
1297 if (to == NULL) {
1298 to = xmlRegNewState(ctxt);
1299 xmlRegStatePush(ctxt, to);
1300 ctxt->state = to;
1301 }
1302 xmlRegStateAddTrans(ctxt, from, NULL, to, -1, -1);
1303}
1304
1305/**
1306 * xmlFAGenerateCountedEpsilonTransition:
Daniel Veillard441bc322002-04-20 17:38:48 +00001307 * @ctxt: a regexp parser context
1308 * @from: the from state
1309 * @to: the target state or NULL for building a new one
Daniel Veillard4255d502002-04-16 15:50:10 +00001310 * counter: the counter for that transition
1311 *
1312 */
1313static void
1314xmlFAGenerateCountedEpsilonTransition(xmlRegParserCtxtPtr ctxt,
1315 xmlRegStatePtr from, xmlRegStatePtr to, int counter) {
1316 if (to == NULL) {
1317 to = xmlRegNewState(ctxt);
1318 xmlRegStatePush(ctxt, to);
1319 ctxt->state = to;
1320 }
1321 xmlRegStateAddTrans(ctxt, from, NULL, to, counter, -1);
1322}
1323
1324/**
1325 * xmlFAGenerateCountedTransition:
Daniel Veillard441bc322002-04-20 17:38:48 +00001326 * @ctxt: a regexp parser context
1327 * @from: the from state
1328 * @to: the target state or NULL for building a new one
Daniel Veillard4255d502002-04-16 15:50:10 +00001329 * counter: the counter for that transition
1330 *
1331 */
1332static void
1333xmlFAGenerateCountedTransition(xmlRegParserCtxtPtr ctxt,
1334 xmlRegStatePtr from, xmlRegStatePtr to, int counter) {
1335 if (to == NULL) {
1336 to = xmlRegNewState(ctxt);
1337 xmlRegStatePush(ctxt, to);
1338 ctxt->state = to;
1339 }
1340 xmlRegStateAddTrans(ctxt, from, NULL, to, -1, counter);
1341}
1342
1343/**
1344 * xmlFAGenerateTransitions:
Daniel Veillard441bc322002-04-20 17:38:48 +00001345 * @ctxt: a regexp parser context
1346 * @from: the from state
1347 * @to: the target state or NULL for building a new one
1348 * @atom: the atom generating the transition
Daniel Veillard4255d502002-04-16 15:50:10 +00001349 *
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001350 * Returns 0 if succes and -1 in case of error.
Daniel Veillard4255d502002-04-16 15:50:10 +00001351 */
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001352static int
Daniel Veillard4255d502002-04-16 15:50:10 +00001353xmlFAGenerateTransitions(xmlRegParserCtxtPtr ctxt, xmlRegStatePtr from,
1354 xmlRegStatePtr to, xmlRegAtomPtr atom) {
1355 if (atom == NULL) {
1356 ERROR("genrate transition: atom == NULL");
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001357 return(-1);
Daniel Veillard4255d502002-04-16 15:50:10 +00001358 }
1359 if (atom->type == XML_REGEXP_SUBREG) {
1360 /*
1361 * this is a subexpression handling one should not need to
1362 * create a new node excep for XML_REGEXP_QUANT_RANGE.
1363 */
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001364 if (xmlRegAtomPush(ctxt, atom) < 0) {
1365 return(-1);
1366 }
Daniel Veillard4255d502002-04-16 15:50:10 +00001367 if ((to != NULL) && (atom->stop != to) &&
1368 (atom->quant != XML_REGEXP_QUANT_RANGE)) {
1369 /*
1370 * Generate an epsilon transition to link to the target
1371 */
1372 xmlFAGenerateEpsilonTransition(ctxt, atom->stop, to);
1373 }
1374 switch (atom->quant) {
1375 case XML_REGEXP_QUANT_OPT:
1376 atom->quant = XML_REGEXP_QUANT_ONCE;
1377 xmlFAGenerateEpsilonTransition(ctxt, atom->start, atom->stop);
1378 break;
1379 case XML_REGEXP_QUANT_MULT:
1380 atom->quant = XML_REGEXP_QUANT_ONCE;
1381 xmlFAGenerateEpsilonTransition(ctxt, atom->start, atom->stop);
1382 xmlFAGenerateEpsilonTransition(ctxt, atom->stop, atom->start);
1383 break;
1384 case XML_REGEXP_QUANT_PLUS:
1385 atom->quant = XML_REGEXP_QUANT_ONCE;
1386 xmlFAGenerateEpsilonTransition(ctxt, atom->stop, atom->start);
1387 break;
1388 case XML_REGEXP_QUANT_RANGE: {
1389 int counter;
1390 xmlRegStatePtr newstate;
1391
1392 /*
1393 * This one is nasty:
1394 * 1/ register a new counter
1395 * 2/ register an epsilon transition associated to
1396 * this counter going from atom->stop to atom->start
1397 * 3/ create a new state
1398 * 4/ generate a counted transition from atom->stop to
1399 * that state
1400 */
1401 counter = xmlRegGetCounter(ctxt);
1402 ctxt->counters[counter].min = atom->min - 1;
1403 ctxt->counters[counter].max = atom->max - 1;
1404 atom->min = 0;
1405 atom->max = 0;
1406 atom->quant = XML_REGEXP_QUANT_ONCE;
1407 xmlFAGenerateCountedEpsilonTransition(ctxt, atom->stop,
1408 atom->start, counter);
1409 if (to != NULL) {
1410 newstate = to;
1411 } else {
1412 newstate = xmlRegNewState(ctxt);
1413 xmlRegStatePush(ctxt, newstate);
1414 ctxt->state = newstate;
1415 }
1416 xmlFAGenerateCountedTransition(ctxt, atom->stop,
1417 newstate, counter);
1418 }
1419 default:
1420 break;
1421 }
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001422 return(0);
Daniel Veillard4255d502002-04-16 15:50:10 +00001423 } else {
1424 if (to == NULL) {
1425 to = xmlRegNewState(ctxt);
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001426 if (to != NULL)
1427 xmlRegStatePush(ctxt, to);
1428 else {
1429 return(-1);
1430 }
1431 }
1432 if (xmlRegAtomPush(ctxt, atom) < 0) {
1433 return(-1);
Daniel Veillard4255d502002-04-16 15:50:10 +00001434 }
1435 xmlRegStateAddTrans(ctxt, from, atom, to, -1, -1);
Daniel Veillard4255d502002-04-16 15:50:10 +00001436 ctxt->state = to;
1437 }
1438 switch (atom->quant) {
1439 case XML_REGEXP_QUANT_OPT:
1440 atom->quant = XML_REGEXP_QUANT_ONCE;
1441 xmlFAGenerateEpsilonTransition(ctxt, from, to);
1442 break;
1443 case XML_REGEXP_QUANT_MULT:
1444 atom->quant = XML_REGEXP_QUANT_ONCE;
1445 xmlFAGenerateEpsilonTransition(ctxt, from, to);
1446 xmlRegStateAddTrans(ctxt, to, atom, to, -1, -1);
1447 break;
1448 case XML_REGEXP_QUANT_PLUS:
1449 atom->quant = XML_REGEXP_QUANT_ONCE;
1450 xmlRegStateAddTrans(ctxt, to, atom, to, -1, -1);
1451 break;
1452 default:
1453 break;
1454 }
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001455 return(0);
Daniel Veillard4255d502002-04-16 15:50:10 +00001456}
1457
1458/**
1459 * xmlFAReduceEpsilonTransitions:
Daniel Veillard441bc322002-04-20 17:38:48 +00001460 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00001461 * @fromnr: the from state
1462 * @tonr: the to state
1463 * @cpunter: should that transition be associted to a counted
1464 *
1465 */
1466static void
1467xmlFAReduceEpsilonTransitions(xmlRegParserCtxtPtr ctxt, int fromnr,
1468 int tonr, int counter) {
1469 int transnr;
1470 xmlRegStatePtr from;
1471 xmlRegStatePtr to;
1472
1473#ifdef DEBUG_REGEXP_GRAPH
1474 printf("xmlFAReduceEpsilonTransitions(%d, %d)\n", fromnr, tonr);
1475#endif
1476 from = ctxt->states[fromnr];
1477 if (from == NULL)
1478 return;
1479 to = ctxt->states[tonr];
1480 if (to == NULL)
1481 return;
1482 if ((to->mark == XML_REGEXP_MARK_START) ||
1483 (to->mark == XML_REGEXP_MARK_VISITED))
1484 return;
1485
1486 to->mark = XML_REGEXP_MARK_VISITED;
1487 if (to->type == XML_REGEXP_FINAL_STATE) {
1488#ifdef DEBUG_REGEXP_GRAPH
1489 printf("State %d is final, so %d becomes final\n", tonr, fromnr);
1490#endif
1491 from->type = XML_REGEXP_FINAL_STATE;
1492 }
1493 for (transnr = 0;transnr < to->nbTrans;transnr++) {
1494 if (to->trans[transnr].atom == NULL) {
1495 /*
1496 * Don't remove counted transitions
1497 * Don't loop either
1498 */
Daniel Veillardb509f152002-04-17 16:28:10 +00001499 if (to->trans[transnr].to != fromnr) {
1500 if (to->trans[transnr].count >= 0) {
1501 int newto = to->trans[transnr].to;
1502
1503 xmlRegStateAddTrans(ctxt, from, NULL,
1504 ctxt->states[newto],
1505 -1, to->trans[transnr].count);
1506 } else {
Daniel Veillard4255d502002-04-16 15:50:10 +00001507#ifdef DEBUG_REGEXP_GRAPH
Daniel Veillardb509f152002-04-17 16:28:10 +00001508 printf("Found epsilon trans %d from %d to %d\n",
1509 transnr, tonr, to->trans[transnr].to);
Daniel Veillard4255d502002-04-16 15:50:10 +00001510#endif
Daniel Veillardb509f152002-04-17 16:28:10 +00001511 if (to->trans[transnr].counter >= 0) {
1512 xmlFAReduceEpsilonTransitions(ctxt, fromnr,
1513 to->trans[transnr].to,
1514 to->trans[transnr].counter);
1515 } else {
1516 xmlFAReduceEpsilonTransitions(ctxt, fromnr,
1517 to->trans[transnr].to,
1518 counter);
1519 }
1520 }
Daniel Veillard4255d502002-04-16 15:50:10 +00001521 }
1522 } else {
1523 int newto = to->trans[transnr].to;
1524
Daniel Veillardb509f152002-04-17 16:28:10 +00001525 if (to->trans[transnr].counter >= 0) {
1526 xmlRegStateAddTrans(ctxt, from, to->trans[transnr].atom,
1527 ctxt->states[newto],
1528 to->trans[transnr].counter, -1);
1529 } else {
1530 xmlRegStateAddTrans(ctxt, from, to->trans[transnr].atom,
1531 ctxt->states[newto], counter, -1);
1532 }
Daniel Veillard4255d502002-04-16 15:50:10 +00001533 }
1534 }
1535 to->mark = XML_REGEXP_MARK_NORMAL;
1536}
1537
1538/**
1539 * xmlFAEliminateEpsilonTransitions:
Daniel Veillard441bc322002-04-20 17:38:48 +00001540 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00001541 *
1542 */
1543static void
1544xmlFAEliminateEpsilonTransitions(xmlRegParserCtxtPtr ctxt) {
1545 int statenr, transnr;
1546 xmlRegStatePtr state;
1547
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001548 if (ctxt->states == NULL) return;
1549
1550
Daniel Veillard4255d502002-04-16 15:50:10 +00001551 /*
1552 * build the completed transitions bypassing the epsilons
1553 * Use a marking algorithm to avoid loops
1554 */
1555 for (statenr = 0;statenr < ctxt->nbStates;statenr++) {
1556 state = ctxt->states[statenr];
1557 if (state == NULL)
1558 continue;
1559 for (transnr = 0;transnr < state->nbTrans;transnr++) {
1560 if ((state->trans[transnr].atom == NULL) &&
1561 (state->trans[transnr].to >= 0)) {
1562 if (state->trans[transnr].to == statenr) {
1563 state->trans[transnr].to = -1;
1564#ifdef DEBUG_REGEXP_GRAPH
1565 printf("Removed loopback epsilon trans %d on %d\n",
1566 transnr, statenr);
1567#endif
1568 } else if (state->trans[transnr].count < 0) {
1569 int newto = state->trans[transnr].to;
1570
1571#ifdef DEBUG_REGEXP_GRAPH
1572 printf("Found epsilon trans %d from %d to %d\n",
1573 transnr, statenr, newto);
1574#endif
1575 state->mark = XML_REGEXP_MARK_START;
1576 xmlFAReduceEpsilonTransitions(ctxt, statenr,
1577 newto, state->trans[transnr].counter);
1578 state->mark = XML_REGEXP_MARK_NORMAL;
1579#ifdef DEBUG_REGEXP_GRAPH
1580 } else {
1581 printf("Found counted transition %d on %d\n",
1582 transnr, statenr);
1583#endif
1584 }
1585 }
1586 }
1587 }
1588 /*
1589 * Eliminate the epsilon transitions
1590 */
1591 for (statenr = 0;statenr < ctxt->nbStates;statenr++) {
1592 state = ctxt->states[statenr];
1593 if (state == NULL)
1594 continue;
1595 for (transnr = 0;transnr < state->nbTrans;transnr++) {
1596 if ((state->trans[transnr].atom == NULL) &&
1597 (state->trans[transnr].count < 0) &&
1598 (state->trans[transnr].to >= 0)) {
1599 state->trans[transnr].to = -1;
1600 }
1601 }
1602 }
Daniel Veillard23e73572002-09-19 19:56:43 +00001603
1604 /*
1605 * Use this pass to detect unreachable states too
1606 */
1607 for (statenr = 0;statenr < ctxt->nbStates;statenr++) {
1608 state = ctxt->states[statenr];
1609 if (state != NULL)
William M. Brack779af002003-08-01 15:55:39 +00001610 state->reached = XML_REGEXP_MARK_NORMAL;
Daniel Veillard23e73572002-09-19 19:56:43 +00001611 }
1612 state = ctxt->states[0];
1613 if (state != NULL)
William M. Brack779af002003-08-01 15:55:39 +00001614 state->reached = XML_REGEXP_MARK_START;
Daniel Veillard23e73572002-09-19 19:56:43 +00001615 while (state != NULL) {
1616 xmlRegStatePtr target = NULL;
William M. Brack779af002003-08-01 15:55:39 +00001617 state->reached = XML_REGEXP_MARK_VISITED;
Daniel Veillard23e73572002-09-19 19:56:43 +00001618 /*
1619 * Mark all state reachable from the current reachable state
1620 */
1621 for (transnr = 0;transnr < state->nbTrans;transnr++) {
1622 if ((state->trans[transnr].to >= 0) &&
1623 ((state->trans[transnr].atom != NULL) ||
1624 (state->trans[transnr].count >= 0))) {
1625 int newto = state->trans[transnr].to;
1626
1627 if (ctxt->states[newto] == NULL)
1628 continue;
William M. Brack779af002003-08-01 15:55:39 +00001629 if (ctxt->states[newto]->reached == XML_REGEXP_MARK_NORMAL) {
1630 ctxt->states[newto]->reached = XML_REGEXP_MARK_START;
Daniel Veillard23e73572002-09-19 19:56:43 +00001631 target = ctxt->states[newto];
1632 }
1633 }
1634 }
1635 /*
1636 * find the next accessible state not explored
1637 */
1638 if (target == NULL) {
1639 for (statenr = 1;statenr < ctxt->nbStates;statenr++) {
1640 state = ctxt->states[statenr];
William M. Brack779af002003-08-01 15:55:39 +00001641 if ((state != NULL) && (state->reached ==
1642 XML_REGEXP_MARK_START)) {
Daniel Veillard23e73572002-09-19 19:56:43 +00001643 target = state;
1644 break;
1645 }
1646 }
1647 }
1648 state = target;
1649 }
1650 for (statenr = 0;statenr < ctxt->nbStates;statenr++) {
1651 state = ctxt->states[statenr];
William M. Brack779af002003-08-01 15:55:39 +00001652 if ((state != NULL) && (state->reached == XML_REGEXP_MARK_NORMAL)) {
Daniel Veillard23e73572002-09-19 19:56:43 +00001653#ifdef DEBUG_REGEXP_GRAPH
1654 printf("Removed unreachable state %d\n", statenr);
1655#endif
1656 xmlRegFreeState(state);
1657 ctxt->states[statenr] = NULL;
1658 }
1659 }
1660
Daniel Veillard4255d502002-04-16 15:50:10 +00001661}
1662
Daniel Veillarde19fc232002-04-22 16:01:24 +00001663/**
1664 * xmlFACompareAtoms:
1665 * @atom1: an atom
1666 * @atom2: an atom
1667 *
1668 * Compares two atoms to check whether they are equivatents
1669 *
1670 * Returns 1 if yes and 0 otherwise
1671 */
1672static int
1673xmlFACompareAtoms(xmlRegAtomPtr atom1, xmlRegAtomPtr atom2) {
1674 if (atom1 == atom2)
1675 return(1);
1676 if ((atom1 == NULL) || (atom2 == NULL))
1677 return(0);
1678
1679 if (atom1->type != atom2->type)
1680 return(0);
1681 switch (atom1->type) {
1682 case XML_REGEXP_STRING:
1683 return(xmlStrEqual((xmlChar *)atom1->valuep,
1684 (xmlChar *)atom2->valuep));
1685 case XML_REGEXP_EPSILON:
1686 return(1);
1687 case XML_REGEXP_CHARVAL:
1688 return(atom1->codepoint == atom2->codepoint);
1689 case XML_REGEXP_RANGES:
1690 TODO;
1691 return(0);
1692 default:
1693 break;
1694 }
1695 return(1);
1696}
1697
1698/**
1699 * xmlFARecurseDeterminism:
1700 * @ctxt: a regexp parser context
1701 *
1702 * Check whether the associated regexp is determinist,
1703 * should be called after xmlFAEliminateEpsilonTransitions()
1704 *
1705 */
1706static int
1707xmlFARecurseDeterminism(xmlRegParserCtxtPtr ctxt, xmlRegStatePtr state,
1708 int to, xmlRegAtomPtr atom) {
1709 int ret = 1;
1710 int transnr;
1711 xmlRegTransPtr t1;
1712
1713 if (state == NULL)
1714 return(ret);
1715 for (transnr = 0;transnr < state->nbTrans;transnr++) {
1716 t1 = &(state->trans[transnr]);
1717 /*
1718 * check transitions conflicting with the one looked at
1719 */
1720 if (t1->atom == NULL) {
1721 if (t1->to == -1)
1722 continue;
1723 ret = xmlFARecurseDeterminism(ctxt, ctxt->states[t1->to],
1724 to, atom);
1725 if (ret == 0)
1726 return(0);
1727 continue;
1728 }
1729 if (t1->to != to)
1730 continue;
1731 if (xmlFACompareAtoms(t1->atom, atom))
1732 return(0);
1733 }
1734 return(ret);
1735}
1736
1737/**
1738 * xmlFAComputesDeterminism:
1739 * @ctxt: a regexp parser context
1740 *
1741 * Check whether the associated regexp is determinist,
1742 * should be called after xmlFAEliminateEpsilonTransitions()
1743 *
1744 */
1745static int
1746xmlFAComputesDeterminism(xmlRegParserCtxtPtr ctxt) {
1747 int statenr, transnr;
1748 xmlRegStatePtr state;
1749 xmlRegTransPtr t1, t2;
1750 int i;
1751 int ret = 1;
1752
Daniel Veillard4402ab42002-09-12 16:02:56 +00001753#ifdef DEBUG_REGEXP_GRAPH
1754 printf("xmlFAComputesDeterminism\n");
1755 xmlRegPrintCtxt(stdout, ctxt);
1756#endif
Daniel Veillarde19fc232002-04-22 16:01:24 +00001757 if (ctxt->determinist != -1)
1758 return(ctxt->determinist);
1759
1760 /*
1761 * Check for all states that there isn't 2 transitions
1762 * with the same atom and a different target.
1763 */
1764 for (statenr = 0;statenr < ctxt->nbStates;statenr++) {
1765 state = ctxt->states[statenr];
1766 if (state == NULL)
1767 continue;
1768 for (transnr = 0;transnr < state->nbTrans;transnr++) {
1769 t1 = &(state->trans[transnr]);
1770 /*
1771 * Determinism checks in case of counted or all transitions
1772 * will have to be handled separately
1773 */
1774 if (t1->atom == NULL)
1775 continue;
1776 if (t1->to == -1) /* eliminated */
1777 continue;
1778 for (i = 0;i < transnr;i++) {
1779 t2 = &(state->trans[i]);
1780 if (t2->to == -1) /* eliminated */
1781 continue;
1782 if (t2->atom != NULL) {
1783 if (t1->to == t2->to) {
1784 if (xmlFACompareAtoms(t1->atom, t2->atom))
1785 t2->to = -1; /* eliminate */
1786 } else {
1787 /* not determinist ! */
1788 if (xmlFACompareAtoms(t1->atom, t2->atom))
1789 ret = 0;
1790 }
1791 } else if (t1->to != -1) {
1792 /*
1793 * do the closure in case of remaining specific
1794 * epsilon transitions like choices or all
1795 */
1796 ret = xmlFARecurseDeterminism(ctxt, ctxt->states[t1->to],
1797 t2->to, t2->atom);
1798 if (ret == 0)
1799 return(0);
1800 }
1801 }
1802 if (ret == 0)
1803 break;
1804 }
1805 if (ret == 0)
1806 break;
1807 }
1808 ctxt->determinist = ret;
1809 return(ret);
1810}
1811
Daniel Veillard4255d502002-04-16 15:50:10 +00001812/************************************************************************
1813 * *
1814 * Routines to check input against transition atoms *
1815 * *
1816 ************************************************************************/
1817
1818static int
1819xmlRegCheckCharacterRange(xmlRegAtomType type, int codepoint, int neg,
1820 int start, int end, const xmlChar *blockName) {
1821 int ret = 0;
1822
1823 switch (type) {
1824 case XML_REGEXP_STRING:
1825 case XML_REGEXP_SUBREG:
1826 case XML_REGEXP_RANGES:
1827 case XML_REGEXP_EPSILON:
1828 return(-1);
1829 case XML_REGEXP_ANYCHAR:
1830 ret = ((codepoint != '\n') && (codepoint != '\r'));
1831 break;
1832 case XML_REGEXP_CHARVAL:
1833 ret = ((codepoint >= start) && (codepoint <= end));
1834 break;
1835 case XML_REGEXP_NOTSPACE:
1836 neg = !neg;
1837 case XML_REGEXP_ANYSPACE:
1838 ret = ((codepoint == '\n') || (codepoint == '\r') ||
1839 (codepoint == '\t') || (codepoint == ' '));
1840 break;
1841 case XML_REGEXP_NOTINITNAME:
1842 neg = !neg;
1843 case XML_REGEXP_INITNAME:
William M. Brack871611b2003-10-18 04:53:14 +00001844 ret = (IS_LETTER(codepoint) ||
Daniel Veillard4255d502002-04-16 15:50:10 +00001845 (codepoint == '_') || (codepoint == ':'));
1846 break;
1847 case XML_REGEXP_NOTNAMECHAR:
1848 neg = !neg;
1849 case XML_REGEXP_NAMECHAR:
William M. Brack871611b2003-10-18 04:53:14 +00001850 ret = (IS_LETTER(codepoint) || IS_DIGIT(codepoint) ||
Daniel Veillard4255d502002-04-16 15:50:10 +00001851 (codepoint == '.') || (codepoint == '-') ||
1852 (codepoint == '_') || (codepoint == ':') ||
William M. Brack871611b2003-10-18 04:53:14 +00001853 IS_COMBINING(codepoint) || IS_EXTENDER(codepoint));
Daniel Veillard4255d502002-04-16 15:50:10 +00001854 break;
1855 case XML_REGEXP_NOTDECIMAL:
1856 neg = !neg;
1857 case XML_REGEXP_DECIMAL:
1858 ret = xmlUCSIsCatNd(codepoint);
1859 break;
1860 case XML_REGEXP_REALCHAR:
1861 neg = !neg;
1862 case XML_REGEXP_NOTREALCHAR:
1863 ret = xmlUCSIsCatP(codepoint);
1864 if (ret == 0)
1865 ret = xmlUCSIsCatZ(codepoint);
1866 if (ret == 0)
1867 ret = xmlUCSIsCatC(codepoint);
1868 break;
1869 case XML_REGEXP_LETTER:
1870 ret = xmlUCSIsCatL(codepoint);
1871 break;
1872 case XML_REGEXP_LETTER_UPPERCASE:
1873 ret = xmlUCSIsCatLu(codepoint);
1874 break;
1875 case XML_REGEXP_LETTER_LOWERCASE:
1876 ret = xmlUCSIsCatLl(codepoint);
1877 break;
1878 case XML_REGEXP_LETTER_TITLECASE:
1879 ret = xmlUCSIsCatLt(codepoint);
1880 break;
1881 case XML_REGEXP_LETTER_MODIFIER:
1882 ret = xmlUCSIsCatLm(codepoint);
1883 break;
1884 case XML_REGEXP_LETTER_OTHERS:
1885 ret = xmlUCSIsCatLo(codepoint);
1886 break;
1887 case XML_REGEXP_MARK:
1888 ret = xmlUCSIsCatM(codepoint);
1889 break;
1890 case XML_REGEXP_MARK_NONSPACING:
1891 ret = xmlUCSIsCatMn(codepoint);
1892 break;
1893 case XML_REGEXP_MARK_SPACECOMBINING:
1894 ret = xmlUCSIsCatMc(codepoint);
1895 break;
1896 case XML_REGEXP_MARK_ENCLOSING:
1897 ret = xmlUCSIsCatMe(codepoint);
1898 break;
1899 case XML_REGEXP_NUMBER:
1900 ret = xmlUCSIsCatN(codepoint);
1901 break;
1902 case XML_REGEXP_NUMBER_DECIMAL:
1903 ret = xmlUCSIsCatNd(codepoint);
1904 break;
1905 case XML_REGEXP_NUMBER_LETTER:
1906 ret = xmlUCSIsCatNl(codepoint);
1907 break;
1908 case XML_REGEXP_NUMBER_OTHERS:
1909 ret = xmlUCSIsCatNo(codepoint);
1910 break;
1911 case XML_REGEXP_PUNCT:
1912 ret = xmlUCSIsCatP(codepoint);
1913 break;
1914 case XML_REGEXP_PUNCT_CONNECTOR:
1915 ret = xmlUCSIsCatPc(codepoint);
1916 break;
1917 case XML_REGEXP_PUNCT_DASH:
1918 ret = xmlUCSIsCatPd(codepoint);
1919 break;
1920 case XML_REGEXP_PUNCT_OPEN:
1921 ret = xmlUCSIsCatPs(codepoint);
1922 break;
1923 case XML_REGEXP_PUNCT_CLOSE:
1924 ret = xmlUCSIsCatPe(codepoint);
1925 break;
1926 case XML_REGEXP_PUNCT_INITQUOTE:
1927 ret = xmlUCSIsCatPi(codepoint);
1928 break;
1929 case XML_REGEXP_PUNCT_FINQUOTE:
1930 ret = xmlUCSIsCatPf(codepoint);
1931 break;
1932 case XML_REGEXP_PUNCT_OTHERS:
1933 ret = xmlUCSIsCatPo(codepoint);
1934 break;
1935 case XML_REGEXP_SEPAR:
1936 ret = xmlUCSIsCatZ(codepoint);
1937 break;
1938 case XML_REGEXP_SEPAR_SPACE:
1939 ret = xmlUCSIsCatZs(codepoint);
1940 break;
1941 case XML_REGEXP_SEPAR_LINE:
1942 ret = xmlUCSIsCatZl(codepoint);
1943 break;
1944 case XML_REGEXP_SEPAR_PARA:
1945 ret = xmlUCSIsCatZp(codepoint);
1946 break;
1947 case XML_REGEXP_SYMBOL:
1948 ret = xmlUCSIsCatS(codepoint);
1949 break;
1950 case XML_REGEXP_SYMBOL_MATH:
1951 ret = xmlUCSIsCatSm(codepoint);
1952 break;
1953 case XML_REGEXP_SYMBOL_CURRENCY:
1954 ret = xmlUCSIsCatSc(codepoint);
1955 break;
1956 case XML_REGEXP_SYMBOL_MODIFIER:
1957 ret = xmlUCSIsCatSk(codepoint);
1958 break;
1959 case XML_REGEXP_SYMBOL_OTHERS:
1960 ret = xmlUCSIsCatSo(codepoint);
1961 break;
1962 case XML_REGEXP_OTHER:
1963 ret = xmlUCSIsCatC(codepoint);
1964 break;
1965 case XML_REGEXP_OTHER_CONTROL:
1966 ret = xmlUCSIsCatCc(codepoint);
1967 break;
1968 case XML_REGEXP_OTHER_FORMAT:
1969 ret = xmlUCSIsCatCf(codepoint);
1970 break;
1971 case XML_REGEXP_OTHER_PRIVATE:
1972 ret = xmlUCSIsCatCo(codepoint);
1973 break;
1974 case XML_REGEXP_OTHER_NA:
1975 /* ret = xmlUCSIsCatCn(codepoint); */
1976 /* Seems it doesn't exist anymore in recent Unicode releases */
1977 ret = 0;
1978 break;
1979 case XML_REGEXP_BLOCK_NAME:
1980 ret = xmlUCSIsBlock(codepoint, (const char *) blockName);
1981 break;
1982 }
1983 if (neg)
1984 return(!ret);
1985 return(ret);
1986}
1987
1988static int
1989xmlRegCheckCharacter(xmlRegAtomPtr atom, int codepoint) {
1990 int i, ret = 0;
1991 xmlRegRangePtr range;
1992
William M. Brack871611b2003-10-18 04:53:14 +00001993 if ((atom == NULL) || (!IS_CHAR(codepoint)))
Daniel Veillard4255d502002-04-16 15:50:10 +00001994 return(-1);
1995
1996 switch (atom->type) {
1997 case XML_REGEXP_SUBREG:
1998 case XML_REGEXP_EPSILON:
1999 return(-1);
2000 case XML_REGEXP_CHARVAL:
2001 return(codepoint == atom->codepoint);
2002 case XML_REGEXP_RANGES: {
2003 int accept = 0;
Daniel Veillardf2a12832003-11-24 13:04:35 +00002004
Daniel Veillard4255d502002-04-16 15:50:10 +00002005 for (i = 0;i < atom->nbRanges;i++) {
2006 range = atom->ranges[i];
Daniel Veillardf8b9de32003-11-24 14:27:26 +00002007 if (range->neg == 2) {
Daniel Veillard4255d502002-04-16 15:50:10 +00002008 ret = xmlRegCheckCharacterRange(range->type, codepoint,
2009 0, range->start, range->end,
2010 range->blockName);
2011 if (ret != 0)
2012 return(0); /* excluded char */
Daniel Veillardf8b9de32003-11-24 14:27:26 +00002013 } else if (range->neg) {
2014 ret = xmlRegCheckCharacterRange(range->type, codepoint,
2015 0, range->start, range->end,
2016 range->blockName);
2017 if (ret == 0)
Daniel Veillardf2a12832003-11-24 13:04:35 +00002018 accept = 1;
Daniel Veillardf8b9de32003-11-24 14:27:26 +00002019 else
2020 return(0);
Daniel Veillard4255d502002-04-16 15:50:10 +00002021 } else {
2022 ret = xmlRegCheckCharacterRange(range->type, codepoint,
2023 0, range->start, range->end,
2024 range->blockName);
2025 if (ret != 0)
2026 accept = 1; /* might still be excluded */
2027 }
2028 }
2029 return(accept);
2030 }
2031 case XML_REGEXP_STRING:
2032 printf("TODO: XML_REGEXP_STRING\n");
2033 return(-1);
2034 case XML_REGEXP_ANYCHAR:
2035 case XML_REGEXP_ANYSPACE:
2036 case XML_REGEXP_NOTSPACE:
2037 case XML_REGEXP_INITNAME:
2038 case XML_REGEXP_NOTINITNAME:
2039 case XML_REGEXP_NAMECHAR:
2040 case XML_REGEXP_NOTNAMECHAR:
2041 case XML_REGEXP_DECIMAL:
2042 case XML_REGEXP_NOTDECIMAL:
2043 case XML_REGEXP_REALCHAR:
2044 case XML_REGEXP_NOTREALCHAR:
2045 case XML_REGEXP_LETTER:
2046 case XML_REGEXP_LETTER_UPPERCASE:
2047 case XML_REGEXP_LETTER_LOWERCASE:
2048 case XML_REGEXP_LETTER_TITLECASE:
2049 case XML_REGEXP_LETTER_MODIFIER:
2050 case XML_REGEXP_LETTER_OTHERS:
2051 case XML_REGEXP_MARK:
2052 case XML_REGEXP_MARK_NONSPACING:
2053 case XML_REGEXP_MARK_SPACECOMBINING:
2054 case XML_REGEXP_MARK_ENCLOSING:
2055 case XML_REGEXP_NUMBER:
2056 case XML_REGEXP_NUMBER_DECIMAL:
2057 case XML_REGEXP_NUMBER_LETTER:
2058 case XML_REGEXP_NUMBER_OTHERS:
2059 case XML_REGEXP_PUNCT:
2060 case XML_REGEXP_PUNCT_CONNECTOR:
2061 case XML_REGEXP_PUNCT_DASH:
2062 case XML_REGEXP_PUNCT_OPEN:
2063 case XML_REGEXP_PUNCT_CLOSE:
2064 case XML_REGEXP_PUNCT_INITQUOTE:
2065 case XML_REGEXP_PUNCT_FINQUOTE:
2066 case XML_REGEXP_PUNCT_OTHERS:
2067 case XML_REGEXP_SEPAR:
2068 case XML_REGEXP_SEPAR_SPACE:
2069 case XML_REGEXP_SEPAR_LINE:
2070 case XML_REGEXP_SEPAR_PARA:
2071 case XML_REGEXP_SYMBOL:
2072 case XML_REGEXP_SYMBOL_MATH:
2073 case XML_REGEXP_SYMBOL_CURRENCY:
2074 case XML_REGEXP_SYMBOL_MODIFIER:
2075 case XML_REGEXP_SYMBOL_OTHERS:
2076 case XML_REGEXP_OTHER:
2077 case XML_REGEXP_OTHER_CONTROL:
2078 case XML_REGEXP_OTHER_FORMAT:
2079 case XML_REGEXP_OTHER_PRIVATE:
2080 case XML_REGEXP_OTHER_NA:
2081 case XML_REGEXP_BLOCK_NAME:
2082 ret = xmlRegCheckCharacterRange(atom->type, codepoint, 0, 0, 0,
2083 (const xmlChar *)atom->valuep);
2084 if (atom->neg)
2085 ret = !ret;
2086 break;
2087 }
2088 return(ret);
2089}
2090
2091/************************************************************************
2092 * *
2093 * Saving an restoring state of an execution context *
2094 * *
2095 ************************************************************************/
2096
2097#ifdef DEBUG_REGEXP_EXEC
2098static void
2099xmlFARegDebugExec(xmlRegExecCtxtPtr exec) {
2100 printf("state: %d:%d:idx %d", exec->state->no, exec->transno, exec->index);
2101 if (exec->inputStack != NULL) {
2102 int i;
2103 printf(": ");
2104 for (i = 0;(i < 3) && (i < exec->inputStackNr);i++)
2105 printf("%s ", exec->inputStack[exec->inputStackNr - (i + 1)]);
2106 } else {
2107 printf(": %s", &(exec->inputString[exec->index]));
2108 }
2109 printf("\n");
2110}
2111#endif
2112
2113static void
2114xmlFARegExecSave(xmlRegExecCtxtPtr exec) {
2115#ifdef DEBUG_REGEXP_EXEC
2116 printf("saving ");
2117 exec->transno++;
2118 xmlFARegDebugExec(exec);
2119 exec->transno--;
2120#endif
2121
2122 if (exec->maxRollbacks == 0) {
2123 exec->maxRollbacks = 4;
2124 exec->rollbacks = (xmlRegExecRollback *) xmlMalloc(exec->maxRollbacks *
2125 sizeof(xmlRegExecRollback));
2126 if (exec->rollbacks == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00002127 xmlRegexpErrMemory(NULL, "saving regexp");
Daniel Veillard4255d502002-04-16 15:50:10 +00002128 exec->maxRollbacks = 0;
2129 return;
2130 }
2131 memset(exec->rollbacks, 0,
2132 exec->maxRollbacks * sizeof(xmlRegExecRollback));
2133 } else if (exec->nbRollbacks >= exec->maxRollbacks) {
2134 xmlRegExecRollback *tmp;
2135 int len = exec->maxRollbacks;
2136
2137 exec->maxRollbacks *= 2;
2138 tmp = (xmlRegExecRollback *) xmlRealloc(exec->rollbacks,
2139 exec->maxRollbacks * sizeof(xmlRegExecRollback));
2140 if (tmp == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00002141 xmlRegexpErrMemory(NULL, "saving regexp");
Daniel Veillard4255d502002-04-16 15:50:10 +00002142 exec->maxRollbacks /= 2;
2143 return;
2144 }
2145 exec->rollbacks = tmp;
2146 tmp = &exec->rollbacks[len];
2147 memset(tmp, 0, (exec->maxRollbacks - len) * sizeof(xmlRegExecRollback));
2148 }
2149 exec->rollbacks[exec->nbRollbacks].state = exec->state;
2150 exec->rollbacks[exec->nbRollbacks].index = exec->index;
2151 exec->rollbacks[exec->nbRollbacks].nextbranch = exec->transno + 1;
2152 if (exec->comp->nbCounters > 0) {
2153 if (exec->rollbacks[exec->nbRollbacks].counts == NULL) {
2154 exec->rollbacks[exec->nbRollbacks].counts = (int *)
2155 xmlMalloc(exec->comp->nbCounters * sizeof(int));
2156 if (exec->rollbacks[exec->nbRollbacks].counts == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00002157 xmlRegexpErrMemory(NULL, "saving regexp");
Daniel Veillard4255d502002-04-16 15:50:10 +00002158 exec->status = -5;
2159 return;
2160 }
2161 }
2162 memcpy(exec->rollbacks[exec->nbRollbacks].counts, exec->counts,
2163 exec->comp->nbCounters * sizeof(int));
2164 }
2165 exec->nbRollbacks++;
2166}
2167
2168static void
2169xmlFARegExecRollBack(xmlRegExecCtxtPtr exec) {
2170 if (exec->nbRollbacks <= 0) {
2171 exec->status = -1;
2172#ifdef DEBUG_REGEXP_EXEC
2173 printf("rollback failed on empty stack\n");
2174#endif
2175 return;
2176 }
2177 exec->nbRollbacks--;
2178 exec->state = exec->rollbacks[exec->nbRollbacks].state;
2179 exec->index = exec->rollbacks[exec->nbRollbacks].index;
2180 exec->transno = exec->rollbacks[exec->nbRollbacks].nextbranch;
2181 if (exec->comp->nbCounters > 0) {
2182 if (exec->rollbacks[exec->nbRollbacks].counts == NULL) {
2183 fprintf(stderr, "exec save: allocation failed");
2184 exec->status = -6;
2185 return;
2186 }
2187 memcpy(exec->counts, exec->rollbacks[exec->nbRollbacks].counts,
2188 exec->comp->nbCounters * sizeof(int));
2189 }
2190
2191#ifdef DEBUG_REGEXP_EXEC
2192 printf("restored ");
2193 xmlFARegDebugExec(exec);
2194#endif
2195}
2196
2197/************************************************************************
2198 * *
2199 * Verifyer, running an input against a compiled regexp *
2200 * *
2201 ************************************************************************/
2202
2203static int
2204xmlFARegExec(xmlRegexpPtr comp, const xmlChar *content) {
2205 xmlRegExecCtxt execval;
2206 xmlRegExecCtxtPtr exec = &execval;
2207 int ret, codepoint, len;
2208
2209 exec->inputString = content;
2210 exec->index = 0;
2211 exec->determinist = 1;
2212 exec->maxRollbacks = 0;
2213 exec->nbRollbacks = 0;
2214 exec->rollbacks = NULL;
2215 exec->status = 0;
2216 exec->comp = comp;
2217 exec->state = comp->states[0];
2218 exec->transno = 0;
2219 exec->transcount = 0;
Daniel Veillardf2a12832003-11-24 13:04:35 +00002220 exec->inputStack = NULL;
2221 exec->inputStackMax = 0;
Daniel Veillard4255d502002-04-16 15:50:10 +00002222 if (comp->nbCounters > 0) {
2223 exec->counts = (int *) xmlMalloc(comp->nbCounters * sizeof(int));
Daniel Veillardff46a042003-10-08 08:53:17 +00002224 if (exec->counts == NULL) {
2225 xmlRegexpErrMemory(NULL, "running regexp");
Daniel Veillard4255d502002-04-16 15:50:10 +00002226 return(-1);
Daniel Veillardff46a042003-10-08 08:53:17 +00002227 }
Daniel Veillard4255d502002-04-16 15:50:10 +00002228 memset(exec->counts, 0, comp->nbCounters * sizeof(int));
2229 } else
2230 exec->counts = NULL;
2231 while ((exec->status == 0) &&
2232 ((exec->inputString[exec->index] != 0) ||
2233 (exec->state->type != XML_REGEXP_FINAL_STATE))) {
2234 xmlRegTransPtr trans;
2235 xmlRegAtomPtr atom;
2236
2237 /*
William M. Brack0e00b282004-04-26 15:40:47 +00002238 * If end of input on non-terminal state, rollback, however we may
Daniel Veillard4255d502002-04-16 15:50:10 +00002239 * still have epsilon like transition for counted transitions
William M. Brack0e00b282004-04-26 15:40:47 +00002240 * on counters, in that case don't break too early. Additionally,
2241 * if we are working on a range like "AB{0,2}", where B is not present,
2242 * we don't want to break.
Daniel Veillard4255d502002-04-16 15:50:10 +00002243 */
William M. Brack0e00b282004-04-26 15:40:47 +00002244 if ((exec->inputString[exec->index] == 0) && (exec->counts == NULL)) {
2245 /* must check if atom allows minOccurs of 0 */
2246 if (exec->transno < exec->state->nbTrans) { /* there is a transition */
2247 trans = &exec->state->trans[exec->transno];
2248 if (trans->to >=0) {
2249 atom = trans->atom;
2250 if (!((atom->min == 0) && (atom->max > 0)))
2251 goto rollback;
2252 }
2253 } else
2254 goto rollback;
2255 }
Daniel Veillard4255d502002-04-16 15:50:10 +00002256
2257 exec->transcount = 0;
2258 for (;exec->transno < exec->state->nbTrans;exec->transno++) {
2259 trans = &exec->state->trans[exec->transno];
2260 if (trans->to < 0)
2261 continue;
2262 atom = trans->atom;
2263 ret = 0;
2264 if (trans->count >= 0) {
2265 int count;
2266 xmlRegCounterPtr counter;
2267
2268 /*
2269 * A counted transition.
2270 */
2271
2272 count = exec->counts[trans->count];
2273 counter = &exec->comp->counters[trans->count];
2274#ifdef DEBUG_REGEXP_EXEC
2275 printf("testing count %d: val %d, min %d, max %d\n",
2276 trans->count, count, counter->min, counter->max);
2277#endif
2278 ret = ((count >= counter->min) && (count <= counter->max));
2279 } else if (atom == NULL) {
2280 fprintf(stderr, "epsilon transition left at runtime\n");
2281 exec->status = -2;
2282 break;
2283 } else if (exec->inputString[exec->index] != 0) {
2284 codepoint = CUR_SCHAR(&(exec->inputString[exec->index]), len);
2285 ret = xmlRegCheckCharacter(atom, codepoint);
William M. Brack0e00b282004-04-26 15:40:47 +00002286 if ((ret == 1) && (atom->min >= 0) && (atom->max > 0)) {
Daniel Veillard4255d502002-04-16 15:50:10 +00002287 xmlRegStatePtr to = comp->states[trans->to];
2288
2289 /*
2290 * this is a multiple input sequence
2291 */
2292 if (exec->state->nbTrans > exec->transno + 1) {
2293 xmlFARegExecSave(exec);
2294 }
2295 exec->transcount = 1;
2296 do {
2297 /*
2298 * Try to progress as much as possible on the input
2299 */
2300 if (exec->transcount == atom->max) {
2301 break;
2302 }
2303 exec->index += len;
2304 /*
2305 * End of input: stop here
2306 */
2307 if (exec->inputString[exec->index] == 0) {
2308 exec->index -= len;
2309 break;
2310 }
2311 if (exec->transcount >= atom->min) {
2312 int transno = exec->transno;
2313 xmlRegStatePtr state = exec->state;
2314
2315 /*
2316 * The transition is acceptable save it
2317 */
2318 exec->transno = -1; /* trick */
2319 exec->state = to;
2320 xmlFARegExecSave(exec);
2321 exec->transno = transno;
2322 exec->state = state;
2323 }
2324 codepoint = CUR_SCHAR(&(exec->inputString[exec->index]),
2325 len);
2326 ret = xmlRegCheckCharacter(atom, codepoint);
2327 exec->transcount++;
2328 } while (ret == 1);
2329 if (exec->transcount < atom->min)
2330 ret = 0;
2331
2332 /*
2333 * If the last check failed but one transition was found
2334 * possible, rollback
2335 */
2336 if (ret < 0)
2337 ret = 0;
2338 if (ret == 0) {
2339 goto rollback;
2340 }
William M. Brack0e00b282004-04-26 15:40:47 +00002341 } else if ((ret == 0) && (atom->min == 0) && (atom->max > 0)) {
2342 /*
2343 * we don't match on the codepoint, but minOccurs of 0
2344 * says that's ok. Setting len to 0 inhibits stepping
2345 * over the codepoint.
2346 */
2347 exec->transcount = 1;
2348 len = 0;
2349 ret = 1;
Daniel Veillard4255d502002-04-16 15:50:10 +00002350 }
William M. Brack0e00b282004-04-26 15:40:47 +00002351 } else if ((atom->min == 0) && (atom->max > 0)) {
2352 /* another spot to match when minOccurs is 0 */
2353 exec->transcount = 1;
2354 len = 0;
2355 ret = 1;
Daniel Veillard4255d502002-04-16 15:50:10 +00002356 }
2357 if (ret == 1) {
2358 if (exec->state->nbTrans > exec->transno + 1) {
2359 xmlFARegExecSave(exec);
2360 }
2361 if (trans->counter >= 0) {
2362#ifdef DEBUG_REGEXP_EXEC
2363 printf("Increasing count %d\n", trans->counter);
2364#endif
2365 exec->counts[trans->counter]++;
2366 }
2367#ifdef DEBUG_REGEXP_EXEC
2368 printf("entering state %d\n", trans->to);
2369#endif
2370 exec->state = comp->states[trans->to];
2371 exec->transno = 0;
2372 if (trans->atom != NULL) {
2373 exec->index += len;
2374 }
2375 goto progress;
2376 } else if (ret < 0) {
2377 exec->status = -4;
2378 break;
2379 }
2380 }
2381 if ((exec->transno != 0) || (exec->state->nbTrans == 0)) {
2382rollback:
2383 /*
2384 * Failed to find a way out
2385 */
2386 exec->determinist = 0;
2387 xmlFARegExecRollBack(exec);
2388 }
2389progress:
2390 continue;
2391 }
2392 if (exec->rollbacks != NULL) {
2393 if (exec->counts != NULL) {
2394 int i;
2395
2396 for (i = 0;i < exec->maxRollbacks;i++)
2397 if (exec->rollbacks[i].counts != NULL)
2398 xmlFree(exec->rollbacks[i].counts);
2399 }
2400 xmlFree(exec->rollbacks);
2401 }
2402 if (exec->counts != NULL)
2403 xmlFree(exec->counts);
2404 if (exec->status == 0)
2405 return(1);
2406 if (exec->status == -1)
2407 return(0);
2408 return(exec->status);
2409}
2410
2411/************************************************************************
2412 * *
2413 * Progressive interface to the verifyer one atom at a time *
2414 * *
2415 ************************************************************************/
2416
2417/**
Daniel Veillard01c13b52002-12-10 15:19:08 +00002418 * xmlRegNewExecCtxt:
Daniel Veillard4255d502002-04-16 15:50:10 +00002419 * @comp: a precompiled regular expression
2420 * @callback: a callback function used for handling progresses in the
2421 * automata matching phase
2422 * @data: the context data associated to the callback in this context
2423 *
2424 * Build a context used for progressive evaluation of a regexp.
Daniel Veillard01c13b52002-12-10 15:19:08 +00002425 *
2426 * Returns the new context
Daniel Veillard4255d502002-04-16 15:50:10 +00002427 */
2428xmlRegExecCtxtPtr
2429xmlRegNewExecCtxt(xmlRegexpPtr comp, xmlRegExecCallbacks callback, void *data) {
2430 xmlRegExecCtxtPtr exec;
2431
2432 if (comp == NULL)
2433 return(NULL);
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00002434 if ((comp->compact == NULL) && (comp->states == NULL))
2435 return(NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +00002436 exec = (xmlRegExecCtxtPtr) xmlMalloc(sizeof(xmlRegExecCtxt));
2437 if (exec == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00002438 xmlRegexpErrMemory(NULL, "creating execution context");
Daniel Veillard4255d502002-04-16 15:50:10 +00002439 return(NULL);
2440 }
2441 memset(exec, 0, sizeof(xmlRegExecCtxt));
2442 exec->inputString = NULL;
2443 exec->index = 0;
2444 exec->determinist = 1;
2445 exec->maxRollbacks = 0;
2446 exec->nbRollbacks = 0;
2447 exec->rollbacks = NULL;
2448 exec->status = 0;
2449 exec->comp = comp;
Daniel Veillard23e73572002-09-19 19:56:43 +00002450 if (comp->compact == NULL)
2451 exec->state = comp->states[0];
Daniel Veillard4255d502002-04-16 15:50:10 +00002452 exec->transno = 0;
2453 exec->transcount = 0;
2454 exec->callback = callback;
2455 exec->data = data;
2456 if (comp->nbCounters > 0) {
2457 exec->counts = (int *) xmlMalloc(comp->nbCounters * sizeof(int));
2458 if (exec->counts == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00002459 xmlRegexpErrMemory(NULL, "creating execution context");
Daniel Veillard4255d502002-04-16 15:50:10 +00002460 xmlFree(exec);
2461 return(NULL);
2462 }
2463 memset(exec->counts, 0, comp->nbCounters * sizeof(int));
2464 } else
2465 exec->counts = NULL;
2466 exec->inputStackMax = 0;
2467 exec->inputStackNr = 0;
2468 exec->inputStack = NULL;
2469 return(exec);
2470}
2471
2472/**
2473 * xmlRegFreeExecCtxt:
2474 * @exec: a regular expression evaulation context
2475 *
2476 * Free the structures associated to a regular expression evaulation context.
2477 */
2478void
2479xmlRegFreeExecCtxt(xmlRegExecCtxtPtr exec) {
2480 if (exec == NULL)
2481 return;
2482
2483 if (exec->rollbacks != NULL) {
2484 if (exec->counts != NULL) {
2485 int i;
2486
2487 for (i = 0;i < exec->maxRollbacks;i++)
2488 if (exec->rollbacks[i].counts != NULL)
2489 xmlFree(exec->rollbacks[i].counts);
2490 }
2491 xmlFree(exec->rollbacks);
2492 }
2493 if (exec->counts != NULL)
2494 xmlFree(exec->counts);
2495 if (exec->inputStack != NULL) {
2496 int i;
2497
Daniel Veillard32370232002-10-16 14:08:14 +00002498 for (i = 0;i < exec->inputStackNr;i++) {
2499 if (exec->inputStack[i].value != NULL)
2500 xmlFree(exec->inputStack[i].value);
2501 }
Daniel Veillard4255d502002-04-16 15:50:10 +00002502 xmlFree(exec->inputStack);
2503 }
2504 xmlFree(exec);
2505}
2506
2507static void
2508xmlFARegExecSaveInputString(xmlRegExecCtxtPtr exec, const xmlChar *value,
2509 void *data) {
2510#ifdef DEBUG_PUSH
2511 printf("saving value: %d:%s\n", exec->inputStackNr, value);
2512#endif
2513 if (exec->inputStackMax == 0) {
2514 exec->inputStackMax = 4;
2515 exec->inputStack = (xmlRegInputTokenPtr)
2516 xmlMalloc(exec->inputStackMax * sizeof(xmlRegInputToken));
2517 if (exec->inputStack == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00002518 xmlRegexpErrMemory(NULL, "pushing input string");
Daniel Veillard4255d502002-04-16 15:50:10 +00002519 exec->inputStackMax = 0;
2520 return;
2521 }
2522 } else if (exec->inputStackNr + 1 >= exec->inputStackMax) {
2523 xmlRegInputTokenPtr tmp;
2524
2525 exec->inputStackMax *= 2;
2526 tmp = (xmlRegInputTokenPtr) xmlRealloc(exec->inputStack,
2527 exec->inputStackMax * sizeof(xmlRegInputToken));
2528 if (tmp == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00002529 xmlRegexpErrMemory(NULL, "pushing input string");
Daniel Veillard4255d502002-04-16 15:50:10 +00002530 exec->inputStackMax /= 2;
2531 return;
2532 }
2533 exec->inputStack = tmp;
2534 }
2535 exec->inputStack[exec->inputStackNr].value = xmlStrdup(value);
2536 exec->inputStack[exec->inputStackNr].data = data;
2537 exec->inputStackNr++;
2538 exec->inputStack[exec->inputStackNr].value = NULL;
2539 exec->inputStack[exec->inputStackNr].data = NULL;
2540}
2541
2542
2543/**
Daniel Veillard23e73572002-09-19 19:56:43 +00002544 * xmlRegCompactPushString:
2545 * @exec: a regexp execution context
2546 * @comp: the precompiled exec with a compact table
2547 * @value: a string token input
2548 * @data: data associated to the token to reuse in callbacks
2549 *
2550 * Push one input token in the execution context
2551 *
2552 * Returns: 1 if the regexp reached a final state, 0 if non-final, and
2553 * a negative value in case of error.
2554 */
2555static int
2556xmlRegCompactPushString(xmlRegExecCtxtPtr exec,
2557 xmlRegexpPtr comp,
2558 const xmlChar *value,
2559 void *data) {
2560 int state = exec->index;
2561 int i, target;
2562
2563 if ((comp == NULL) || (comp->compact == NULL) || (comp->stringMap == NULL))
2564 return(-1);
2565
2566 if (value == NULL) {
2567 /*
2568 * are we at a final state ?
2569 */
2570 if (comp->compact[state * (comp->nbstrings + 1)] ==
2571 XML_REGEXP_FINAL_STATE)
2572 return(1);
2573 return(0);
2574 }
2575
2576#ifdef DEBUG_PUSH
2577 printf("value pushed: %s\n", value);
2578#endif
2579
2580 /*
2581 * Examine all outside transition from current state
2582 */
2583 for (i = 0;i < comp->nbstrings;i++) {
2584 target = comp->compact[state * (comp->nbstrings + 1) + i + 1];
2585 if ((target > 0) && (target <= comp->nbstates)) {
2586 target--; /* to avoid 0 */
2587 if (xmlStrEqual(comp->stringMap[i], value)) {
2588 exec->index = target;
Daniel Veillard118aed72002-09-24 14:13:13 +00002589 if ((exec->callback != NULL) && (comp->transdata != NULL)) {
2590 exec->callback(exec->data, value,
2591 comp->transdata[state * comp->nbstrings + i], data);
2592 }
Daniel Veillard23e73572002-09-19 19:56:43 +00002593#ifdef DEBUG_PUSH
2594 printf("entering state %d\n", target);
2595#endif
2596 if (comp->compact[target * (comp->nbstrings + 1)] ==
2597 XML_REGEXP_FINAL_STATE)
2598 return(1);
2599 return(0);
2600 }
2601 }
2602 }
2603 /*
2604 * Failed to find an exit transition out from current state for the
2605 * current token
2606 */
2607#ifdef DEBUG_PUSH
2608 printf("failed to find a transition for %s on state %d\n", value, state);
2609#endif
2610 exec->status = -1;
2611 return(-1);
2612}
2613
2614/**
Daniel Veillard4255d502002-04-16 15:50:10 +00002615 * xmlRegExecPushString:
Daniel Veillardea7751d2002-12-20 00:16:24 +00002616 * @exec: a regexp execution context or NULL to indicate the end
Daniel Veillard4255d502002-04-16 15:50:10 +00002617 * @value: a string token input
2618 * @data: data associated to the token to reuse in callbacks
2619 *
2620 * Push one input token in the execution context
2621 *
2622 * Returns: 1 if the regexp reached a final state, 0 if non-final, and
2623 * a negative value in case of error.
2624 */
2625int
2626xmlRegExecPushString(xmlRegExecCtxtPtr exec, const xmlChar *value,
2627 void *data) {
2628 xmlRegTransPtr trans;
2629 xmlRegAtomPtr atom;
2630 int ret;
2631 int final = 0;
2632
2633 if (exec == NULL)
2634 return(-1);
Daniel Veillard23e73572002-09-19 19:56:43 +00002635 if (exec->comp == NULL)
2636 return(-1);
Daniel Veillard4255d502002-04-16 15:50:10 +00002637 if (exec->status != 0)
2638 return(exec->status);
2639
Daniel Veillard23e73572002-09-19 19:56:43 +00002640 if (exec->comp->compact != NULL)
2641 return(xmlRegCompactPushString(exec, exec->comp, value, data));
2642
Daniel Veillard4255d502002-04-16 15:50:10 +00002643 if (value == NULL) {
2644 if (exec->state->type == XML_REGEXP_FINAL_STATE)
2645 return(1);
2646 final = 1;
2647 }
2648
2649#ifdef DEBUG_PUSH
2650 printf("value pushed: %s\n", value);
2651#endif
2652 /*
2653 * If we have an active rollback stack push the new value there
2654 * and get back to where we were left
2655 */
2656 if ((value != NULL) && (exec->inputStackNr > 0)) {
2657 xmlFARegExecSaveInputString(exec, value, data);
2658 value = exec->inputStack[exec->index].value;
2659 data = exec->inputStack[exec->index].data;
2660#ifdef DEBUG_PUSH
2661 printf("value loaded: %s\n", value);
2662#endif
2663 }
2664
2665 while ((exec->status == 0) &&
2666 ((value != NULL) ||
2667 ((final == 1) &&
2668 (exec->state->type != XML_REGEXP_FINAL_STATE)))) {
2669
2670 /*
2671 * End of input on non-terminal state, rollback, however we may
2672 * still have epsilon like transition for counted transitions
2673 * on counters, in that case don't break too early.
2674 */
Daniel Veillardb509f152002-04-17 16:28:10 +00002675 if ((value == NULL) && (exec->counts == NULL))
Daniel Veillard4255d502002-04-16 15:50:10 +00002676 goto rollback;
2677
2678 exec->transcount = 0;
2679 for (;exec->transno < exec->state->nbTrans;exec->transno++) {
2680 trans = &exec->state->trans[exec->transno];
2681 if (trans->to < 0)
2682 continue;
2683 atom = trans->atom;
2684 ret = 0;
Daniel Veillard441bc322002-04-20 17:38:48 +00002685 if (trans->count == REGEXP_ALL_LAX_COUNTER) {
2686 int i;
2687 int count;
2688 xmlRegTransPtr t;
2689 xmlRegCounterPtr counter;
2690
2691 ret = 0;
2692
2693#ifdef DEBUG_PUSH
2694 printf("testing all lax %d\n", trans->count);
2695#endif
2696 /*
2697 * Check all counted transitions from the current state
2698 */
2699 if ((value == NULL) && (final)) {
2700 ret = 1;
2701 } else if (value != NULL) {
2702 for (i = 0;i < exec->state->nbTrans;i++) {
2703 t = &exec->state->trans[i];
2704 if ((t->counter < 0) || (t == trans))
2705 continue;
2706 counter = &exec->comp->counters[t->counter];
2707 count = exec->counts[t->counter];
2708 if ((count < counter->max) &&
2709 (t->atom != NULL) &&
2710 (xmlStrEqual(value, t->atom->valuep))) {
2711 ret = 0;
2712 break;
2713 }
2714 if ((count >= counter->min) &&
2715 (count < counter->max) &&
2716 (xmlStrEqual(value, t->atom->valuep))) {
2717 ret = 1;
2718 break;
2719 }
2720 }
2721 }
2722 } else if (trans->count == REGEXP_ALL_COUNTER) {
Daniel Veillard8a001f62002-04-20 07:24:11 +00002723 int i;
2724 int count;
2725 xmlRegTransPtr t;
2726 xmlRegCounterPtr counter;
2727
2728 ret = 1;
2729
2730#ifdef DEBUG_PUSH
2731 printf("testing all %d\n", trans->count);
2732#endif
2733 /*
2734 * Check all counted transitions from the current state
2735 */
2736 for (i = 0;i < exec->state->nbTrans;i++) {
2737 t = &exec->state->trans[i];
2738 if ((t->counter < 0) || (t == trans))
2739 continue;
2740 counter = &exec->comp->counters[t->counter];
2741 count = exec->counts[t->counter];
2742 if ((count < counter->min) || (count > counter->max)) {
2743 ret = 0;
2744 break;
2745 }
2746 }
2747 } else if (trans->count >= 0) {
Daniel Veillard4255d502002-04-16 15:50:10 +00002748 int count;
2749 xmlRegCounterPtr counter;
2750
2751 /*
2752 * A counted transition.
2753 */
2754
2755 count = exec->counts[trans->count];
2756 counter = &exec->comp->counters[trans->count];
2757#ifdef DEBUG_PUSH
2758 printf("testing count %d: val %d, min %d, max %d\n",
2759 trans->count, count, counter->min, counter->max);
2760#endif
2761 ret = ((count >= counter->min) && (count <= counter->max));
2762 } else if (atom == NULL) {
2763 fprintf(stderr, "epsilon transition left at runtime\n");
2764 exec->status = -2;
2765 break;
2766 } else if (value != NULL) {
2767 ret = xmlStrEqual(value, atom->valuep);
Daniel Veillard441bc322002-04-20 17:38:48 +00002768 if ((ret == 1) && (trans->counter >= 0)) {
2769 xmlRegCounterPtr counter;
2770 int count;
2771
2772 count = exec->counts[trans->counter];
2773 counter = &exec->comp->counters[trans->counter];
2774 if (count >= counter->max)
2775 ret = 0;
2776 }
2777
Daniel Veillard4255d502002-04-16 15:50:10 +00002778 if ((ret == 1) && (atom->min > 0) && (atom->max > 0)) {
2779 xmlRegStatePtr to = exec->comp->states[trans->to];
2780
2781 /*
2782 * this is a multiple input sequence
2783 */
2784 if (exec->state->nbTrans > exec->transno + 1) {
2785 if (exec->inputStackNr <= 0) {
2786 xmlFARegExecSaveInputString(exec, value, data);
2787 }
2788 xmlFARegExecSave(exec);
2789 }
2790 exec->transcount = 1;
2791 do {
2792 /*
2793 * Try to progress as much as possible on the input
2794 */
2795 if (exec->transcount == atom->max) {
2796 break;
2797 }
2798 exec->index++;
2799 value = exec->inputStack[exec->index].value;
2800 data = exec->inputStack[exec->index].data;
2801#ifdef DEBUG_PUSH
2802 printf("value loaded: %s\n", value);
2803#endif
2804
2805 /*
2806 * End of input: stop here
2807 */
2808 if (value == NULL) {
2809 exec->index --;
2810 break;
2811 }
2812 if (exec->transcount >= atom->min) {
2813 int transno = exec->transno;
2814 xmlRegStatePtr state = exec->state;
2815
2816 /*
2817 * The transition is acceptable save it
2818 */
2819 exec->transno = -1; /* trick */
2820 exec->state = to;
2821 if (exec->inputStackNr <= 0) {
2822 xmlFARegExecSaveInputString(exec, value, data);
2823 }
2824 xmlFARegExecSave(exec);
2825 exec->transno = transno;
2826 exec->state = state;
2827 }
2828 ret = xmlStrEqual(value, atom->valuep);
2829 exec->transcount++;
2830 } while (ret == 1);
2831 if (exec->transcount < atom->min)
2832 ret = 0;
2833
2834 /*
2835 * If the last check failed but one transition was found
2836 * possible, rollback
2837 */
2838 if (ret < 0)
2839 ret = 0;
2840 if (ret == 0) {
2841 goto rollback;
2842 }
2843 }
2844 }
2845 if (ret == 1) {
William M. Brack98873952003-12-26 06:03:14 +00002846 if ((exec->callback != NULL) && (atom != NULL) &&
2847 (data != NULL)) {
Daniel Veillard4255d502002-04-16 15:50:10 +00002848 exec->callback(exec->data, atom->valuep,
2849 atom->data, data);
2850 }
2851 if (exec->state->nbTrans > exec->transno + 1) {
2852 if (exec->inputStackNr <= 0) {
2853 xmlFARegExecSaveInputString(exec, value, data);
2854 }
2855 xmlFARegExecSave(exec);
2856 }
2857 if (trans->counter >= 0) {
2858#ifdef DEBUG_PUSH
2859 printf("Increasing count %d\n", trans->counter);
2860#endif
2861 exec->counts[trans->counter]++;
2862 }
2863#ifdef DEBUG_PUSH
2864 printf("entering state %d\n", trans->to);
2865#endif
2866 exec->state = exec->comp->states[trans->to];
2867 exec->transno = 0;
2868 if (trans->atom != NULL) {
2869 if (exec->inputStack != NULL) {
2870 exec->index++;
2871 if (exec->index < exec->inputStackNr) {
2872 value = exec->inputStack[exec->index].value;
2873 data = exec->inputStack[exec->index].data;
2874#ifdef DEBUG_PUSH
2875 printf("value loaded: %s\n", value);
2876#endif
2877 } else {
2878 value = NULL;
2879 data = NULL;
2880#ifdef DEBUG_PUSH
2881 printf("end of input\n");
2882#endif
2883 }
2884 } else {
2885 value = NULL;
2886 data = NULL;
2887#ifdef DEBUG_PUSH
2888 printf("end of input\n");
2889#endif
2890 }
2891 }
2892 goto progress;
2893 } else if (ret < 0) {
2894 exec->status = -4;
2895 break;
2896 }
2897 }
2898 if ((exec->transno != 0) || (exec->state->nbTrans == 0)) {
2899rollback:
2900 /*
2901 * Failed to find a way out
2902 */
2903 exec->determinist = 0;
2904 xmlFARegExecRollBack(exec);
2905 if (exec->status == 0) {
2906 value = exec->inputStack[exec->index].value;
2907 data = exec->inputStack[exec->index].data;
2908#ifdef DEBUG_PUSH
2909 printf("value loaded: %s\n", value);
2910#endif
2911 }
2912 }
2913progress:
2914 continue;
2915 }
2916 if (exec->status == 0) {
2917 return(exec->state->type == XML_REGEXP_FINAL_STATE);
2918 }
2919 return(exec->status);
2920}
2921
Daniel Veillard52b48c72003-04-13 19:53:42 +00002922/**
2923 * xmlRegExecPushString2:
2924 * @exec: a regexp execution context or NULL to indicate the end
2925 * @value: the first string token input
2926 * @value2: the second string token input
2927 * @data: data associated to the token to reuse in callbacks
2928 *
2929 * Push one input token in the execution context
2930 *
2931 * Returns: 1 if the regexp reached a final state, 0 if non-final, and
2932 * a negative value in case of error.
2933 */
2934int
2935xmlRegExecPushString2(xmlRegExecCtxtPtr exec, const xmlChar *value,
2936 const xmlChar *value2, void *data) {
2937 xmlChar buf[150];
2938 int lenn, lenp, ret;
2939 xmlChar *str;
2940
2941 if (exec == NULL)
2942 return(-1);
2943 if (exec->comp == NULL)
2944 return(-1);
2945 if (exec->status != 0)
2946 return(exec->status);
2947
2948 if (value2 == NULL)
2949 return(xmlRegExecPushString(exec, value, data));
2950
2951 lenn = strlen((char *) value2);
2952 lenp = strlen((char *) value);
2953
2954 if (150 < lenn + lenp + 2) {
Daniel Veillard3c908dc2003-04-19 00:07:51 +00002955 str = (xmlChar *) xmlMallocAtomic(lenn + lenp + 2);
Daniel Veillard52b48c72003-04-13 19:53:42 +00002956 if (str == NULL) {
2957 exec->status = -1;
2958 return(-1);
2959 }
2960 } else {
2961 str = buf;
2962 }
2963 memcpy(&str[0], value, lenp);
2964 str[lenp] = '|';
2965 memcpy(&str[lenp + 1], value2, lenn);
2966 str[lenn + lenp + 1] = 0;
2967
2968 if (exec->comp->compact != NULL)
2969 ret = xmlRegCompactPushString(exec, exec->comp, str, data);
2970 else
2971 ret = xmlRegExecPushString(exec, str, data);
2972
2973 if (str != buf)
2974 xmlFree(buf);
2975 return(ret);
2976}
2977
Daniel Veillard4255d502002-04-16 15:50:10 +00002978#if 0
2979static int
2980xmlRegExecPushChar(xmlRegExecCtxtPtr exec, int UCS) {
2981 xmlRegTransPtr trans;
2982 xmlRegAtomPtr atom;
2983 int ret;
2984 int codepoint, len;
2985
2986 if (exec == NULL)
2987 return(-1);
2988 if (exec->status != 0)
2989 return(exec->status);
2990
2991 while ((exec->status == 0) &&
2992 ((exec->inputString[exec->index] != 0) ||
2993 (exec->state->type != XML_REGEXP_FINAL_STATE))) {
2994
2995 /*
2996 * End of input on non-terminal state, rollback, however we may
2997 * still have epsilon like transition for counted transitions
2998 * on counters, in that case don't break too early.
2999 */
3000 if ((exec->inputString[exec->index] == 0) && (exec->counts == NULL))
3001 goto rollback;
3002
3003 exec->transcount = 0;
3004 for (;exec->transno < exec->state->nbTrans;exec->transno++) {
3005 trans = &exec->state->trans[exec->transno];
3006 if (trans->to < 0)
3007 continue;
3008 atom = trans->atom;
3009 ret = 0;
3010 if (trans->count >= 0) {
3011 int count;
3012 xmlRegCounterPtr counter;
3013
3014 /*
3015 * A counted transition.
3016 */
3017
3018 count = exec->counts[trans->count];
3019 counter = &exec->comp->counters[trans->count];
3020#ifdef DEBUG_REGEXP_EXEC
3021 printf("testing count %d: val %d, min %d, max %d\n",
3022 trans->count, count, counter->min, counter->max);
3023#endif
3024 ret = ((count >= counter->min) && (count <= counter->max));
3025 } else if (atom == NULL) {
3026 fprintf(stderr, "epsilon transition left at runtime\n");
3027 exec->status = -2;
3028 break;
3029 } else if (exec->inputString[exec->index] != 0) {
3030 codepoint = CUR_SCHAR(&(exec->inputString[exec->index]), len);
3031 ret = xmlRegCheckCharacter(atom, codepoint);
3032 if ((ret == 1) && (atom->min > 0) && (atom->max > 0)) {
3033 xmlRegStatePtr to = exec->comp->states[trans->to];
3034
3035 /*
3036 * this is a multiple input sequence
3037 */
3038 if (exec->state->nbTrans > exec->transno + 1) {
3039 xmlFARegExecSave(exec);
3040 }
3041 exec->transcount = 1;
3042 do {
3043 /*
3044 * Try to progress as much as possible on the input
3045 */
3046 if (exec->transcount == atom->max) {
3047 break;
3048 }
3049 exec->index += len;
3050 /*
3051 * End of input: stop here
3052 */
3053 if (exec->inputString[exec->index] == 0) {
3054 exec->index -= len;
3055 break;
3056 }
3057 if (exec->transcount >= atom->min) {
3058 int transno = exec->transno;
3059 xmlRegStatePtr state = exec->state;
3060
3061 /*
3062 * The transition is acceptable save it
3063 */
3064 exec->transno = -1; /* trick */
3065 exec->state = to;
3066 xmlFARegExecSave(exec);
3067 exec->transno = transno;
3068 exec->state = state;
3069 }
3070 codepoint = CUR_SCHAR(&(exec->inputString[exec->index]),
3071 len);
3072 ret = xmlRegCheckCharacter(atom, codepoint);
3073 exec->transcount++;
3074 } while (ret == 1);
3075 if (exec->transcount < atom->min)
3076 ret = 0;
3077
3078 /*
3079 * If the last check failed but one transition was found
3080 * possible, rollback
3081 */
3082 if (ret < 0)
3083 ret = 0;
3084 if (ret == 0) {
3085 goto rollback;
3086 }
3087 }
3088 }
3089 if (ret == 1) {
3090 if (exec->state->nbTrans > exec->transno + 1) {
3091 xmlFARegExecSave(exec);
3092 }
3093 if (trans->counter >= 0) {
3094#ifdef DEBUG_REGEXP_EXEC
3095 printf("Increasing count %d\n", trans->counter);
3096#endif
3097 exec->counts[trans->counter]++;
3098 }
3099#ifdef DEBUG_REGEXP_EXEC
3100 printf("entering state %d\n", trans->to);
3101#endif
3102 exec->state = exec->comp->states[trans->to];
3103 exec->transno = 0;
3104 if (trans->atom != NULL) {
3105 exec->index += len;
3106 }
3107 goto progress;
3108 } else if (ret < 0) {
3109 exec->status = -4;
3110 break;
3111 }
3112 }
3113 if ((exec->transno != 0) || (exec->state->nbTrans == 0)) {
3114rollback:
3115 /*
3116 * Failed to find a way out
3117 */
3118 exec->determinist = 0;
3119 xmlFARegExecRollBack(exec);
3120 }
3121progress:
3122 continue;
3123 }
3124}
3125#endif
3126/************************************************************************
3127 * *
3128 * Parser for the Shemas Datatype Regular Expressions *
3129 * http://www.w3.org/TR/2001/REC-xmlschema-2-20010502/#regexs *
3130 * *
3131 ************************************************************************/
3132
3133/**
3134 * xmlFAIsChar:
Daniel Veillard441bc322002-04-20 17:38:48 +00003135 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00003136 *
3137 * [10] Char ::= [^.\?*+()|#x5B#x5D]
3138 */
3139static int
3140xmlFAIsChar(xmlRegParserCtxtPtr ctxt) {
3141 int cur;
3142 int len;
3143
3144 cur = CUR_SCHAR(ctxt->cur, len);
3145 if ((cur == '.') || (cur == '\\') || (cur == '?') ||
3146 (cur == '*') || (cur == '+') || (cur == '(') ||
3147 (cur == ')') || (cur == '|') || (cur == 0x5B) ||
3148 (cur == 0x5D) || (cur == 0))
3149 return(-1);
3150 return(cur);
3151}
3152
3153/**
3154 * xmlFAParseCharProp:
Daniel Veillard441bc322002-04-20 17:38:48 +00003155 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00003156 *
3157 * [27] charProp ::= IsCategory | IsBlock
3158 * [28] IsCategory ::= Letters | Marks | Numbers | Punctuation |
3159 * Separators | Symbols | Others
3160 * [29] Letters ::= 'L' [ultmo]?
3161 * [30] Marks ::= 'M' [nce]?
3162 * [31] Numbers ::= 'N' [dlo]?
3163 * [32] Punctuation ::= 'P' [cdseifo]?
3164 * [33] Separators ::= 'Z' [slp]?
3165 * [34] Symbols ::= 'S' [mcko]?
3166 * [35] Others ::= 'C' [cfon]?
3167 * [36] IsBlock ::= 'Is' [a-zA-Z0-9#x2D]+
3168 */
3169static void
3170xmlFAParseCharProp(xmlRegParserCtxtPtr ctxt) {
3171 int cur;
William M. Brack779af002003-08-01 15:55:39 +00003172 xmlRegAtomType type = (xmlRegAtomType) 0;
Daniel Veillard4255d502002-04-16 15:50:10 +00003173 xmlChar *blockName = NULL;
3174
3175 cur = CUR;
3176 if (cur == 'L') {
3177 NEXT;
3178 cur = CUR;
3179 if (cur == 'u') {
3180 NEXT;
3181 type = XML_REGEXP_LETTER_UPPERCASE;
3182 } else if (cur == 'l') {
3183 NEXT;
3184 type = XML_REGEXP_LETTER_LOWERCASE;
3185 } else if (cur == 't') {
3186 NEXT;
3187 type = XML_REGEXP_LETTER_TITLECASE;
3188 } else if (cur == 'm') {
3189 NEXT;
3190 type = XML_REGEXP_LETTER_MODIFIER;
3191 } else if (cur == 'o') {
3192 NEXT;
3193 type = XML_REGEXP_LETTER_OTHERS;
3194 } else {
3195 type = XML_REGEXP_LETTER;
3196 }
3197 } else if (cur == 'M') {
3198 NEXT;
3199 cur = CUR;
3200 if (cur == 'n') {
3201 NEXT;
3202 /* nonspacing */
3203 type = XML_REGEXP_MARK_NONSPACING;
3204 } else if (cur == 'c') {
3205 NEXT;
3206 /* spacing combining */
3207 type = XML_REGEXP_MARK_SPACECOMBINING;
3208 } else if (cur == 'e') {
3209 NEXT;
3210 /* enclosing */
3211 type = XML_REGEXP_MARK_ENCLOSING;
3212 } else {
3213 /* all marks */
3214 type = XML_REGEXP_MARK;
3215 }
3216 } else if (cur == 'N') {
3217 NEXT;
3218 cur = CUR;
3219 if (cur == 'd') {
3220 NEXT;
3221 /* digital */
3222 type = XML_REGEXP_NUMBER_DECIMAL;
3223 } else if (cur == 'l') {
3224 NEXT;
3225 /* letter */
3226 type = XML_REGEXP_NUMBER_LETTER;
3227 } else if (cur == 'o') {
3228 NEXT;
3229 /* other */
3230 type = XML_REGEXP_NUMBER_OTHERS;
3231 } else {
3232 /* all numbers */
3233 type = XML_REGEXP_NUMBER;
3234 }
3235 } else if (cur == 'P') {
3236 NEXT;
3237 cur = CUR;
3238 if (cur == 'c') {
3239 NEXT;
3240 /* connector */
3241 type = XML_REGEXP_PUNCT_CONNECTOR;
3242 } else if (cur == 'd') {
3243 NEXT;
3244 /* dash */
3245 type = XML_REGEXP_PUNCT_DASH;
3246 } else if (cur == 's') {
3247 NEXT;
3248 /* open */
3249 type = XML_REGEXP_PUNCT_OPEN;
3250 } else if (cur == 'e') {
3251 NEXT;
3252 /* close */
3253 type = XML_REGEXP_PUNCT_CLOSE;
3254 } else if (cur == 'i') {
3255 NEXT;
3256 /* initial quote */
3257 type = XML_REGEXP_PUNCT_INITQUOTE;
3258 } else if (cur == 'f') {
3259 NEXT;
3260 /* final quote */
3261 type = XML_REGEXP_PUNCT_FINQUOTE;
3262 } else if (cur == 'o') {
3263 NEXT;
3264 /* other */
3265 type = XML_REGEXP_PUNCT_OTHERS;
3266 } else {
3267 /* all punctuation */
3268 type = XML_REGEXP_PUNCT;
3269 }
3270 } else if (cur == 'Z') {
3271 NEXT;
3272 cur = CUR;
3273 if (cur == 's') {
3274 NEXT;
3275 /* space */
3276 type = XML_REGEXP_SEPAR_SPACE;
3277 } else if (cur == 'l') {
3278 NEXT;
3279 /* line */
3280 type = XML_REGEXP_SEPAR_LINE;
3281 } else if (cur == 'p') {
3282 NEXT;
3283 /* paragraph */
3284 type = XML_REGEXP_SEPAR_PARA;
3285 } else {
3286 /* all separators */
3287 type = XML_REGEXP_SEPAR;
3288 }
3289 } else if (cur == 'S') {
3290 NEXT;
3291 cur = CUR;
3292 if (cur == 'm') {
3293 NEXT;
3294 type = XML_REGEXP_SYMBOL_MATH;
3295 /* math */
3296 } else if (cur == 'c') {
3297 NEXT;
3298 type = XML_REGEXP_SYMBOL_CURRENCY;
3299 /* currency */
3300 } else if (cur == 'k') {
3301 NEXT;
3302 type = XML_REGEXP_SYMBOL_MODIFIER;
3303 /* modifiers */
3304 } else if (cur == 'o') {
3305 NEXT;
3306 type = XML_REGEXP_SYMBOL_OTHERS;
3307 /* other */
3308 } else {
3309 /* all symbols */
3310 type = XML_REGEXP_SYMBOL;
3311 }
3312 } else if (cur == 'C') {
3313 NEXT;
3314 cur = CUR;
3315 if (cur == 'c') {
3316 NEXT;
3317 /* control */
3318 type = XML_REGEXP_OTHER_CONTROL;
3319 } else if (cur == 'f') {
3320 NEXT;
3321 /* format */
3322 type = XML_REGEXP_OTHER_FORMAT;
3323 } else if (cur == 'o') {
3324 NEXT;
3325 /* private use */
3326 type = XML_REGEXP_OTHER_PRIVATE;
3327 } else if (cur == 'n') {
3328 NEXT;
3329 /* not assigned */
3330 type = XML_REGEXP_OTHER_NA;
3331 } else {
3332 /* all others */
3333 type = XML_REGEXP_OTHER;
3334 }
3335 } else if (cur == 'I') {
3336 const xmlChar *start;
3337 NEXT;
3338 cur = CUR;
3339 if (cur != 's') {
3340 ERROR("IsXXXX expected");
3341 return;
3342 }
3343 NEXT;
3344 start = ctxt->cur;
3345 cur = CUR;
3346 if (((cur >= 'a') && (cur <= 'z')) ||
3347 ((cur >= 'A') && (cur <= 'Z')) ||
3348 ((cur >= '0') && (cur <= '9')) ||
3349 (cur == 0x2D)) {
3350 NEXT;
3351 cur = CUR;
3352 while (((cur >= 'a') && (cur <= 'z')) ||
3353 ((cur >= 'A') && (cur <= 'Z')) ||
3354 ((cur >= '0') && (cur <= '9')) ||
3355 (cur == 0x2D)) {
3356 NEXT;
3357 cur = CUR;
3358 }
3359 }
3360 type = XML_REGEXP_BLOCK_NAME;
3361 blockName = xmlStrndup(start, ctxt->cur - start);
3362 } else {
3363 ERROR("Unknown char property");
3364 return;
3365 }
3366 if (ctxt->atom == NULL) {
3367 ctxt->atom = xmlRegNewAtom(ctxt, type);
3368 if (ctxt->atom != NULL)
3369 ctxt->atom->valuep = blockName;
3370 } else if (ctxt->atom->type == XML_REGEXP_RANGES) {
3371 xmlRegAtomAddRange(ctxt, ctxt->atom, ctxt->neg,
3372 type, 0, 0, blockName);
3373 }
3374}
3375
3376/**
3377 * xmlFAParseCharClassEsc:
Daniel Veillard441bc322002-04-20 17:38:48 +00003378 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00003379 *
3380 * [23] charClassEsc ::= ( SingleCharEsc | MultiCharEsc | catEsc | complEsc )
3381 * [24] SingleCharEsc ::= '\' [nrt\|.?*+(){}#x2D#x5B#x5D#x5E]
3382 * [25] catEsc ::= '\p{' charProp '}'
3383 * [26] complEsc ::= '\P{' charProp '}'
3384 * [37] MultiCharEsc ::= '.' | ('\' [sSiIcCdDwW])
3385 */
3386static void
3387xmlFAParseCharClassEsc(xmlRegParserCtxtPtr ctxt) {
3388 int cur;
3389
3390 if (CUR == '.') {
3391 if (ctxt->atom == NULL) {
3392 ctxt->atom = xmlRegNewAtom(ctxt, XML_REGEXP_ANYCHAR);
3393 } else if (ctxt->atom->type == XML_REGEXP_RANGES) {
3394 xmlRegAtomAddRange(ctxt, ctxt->atom, ctxt->neg,
3395 XML_REGEXP_ANYCHAR, 0, 0, NULL);
3396 }
3397 NEXT;
3398 return;
3399 }
3400 if (CUR != '\\') {
3401 ERROR("Escaped sequence: expecting \\");
3402 return;
3403 }
3404 NEXT;
3405 cur = CUR;
3406 if (cur == 'p') {
3407 NEXT;
3408 if (CUR != '{') {
3409 ERROR("Expecting '{'");
3410 return;
3411 }
3412 NEXT;
3413 xmlFAParseCharProp(ctxt);
3414 if (CUR != '}') {
3415 ERROR("Expecting '}'");
3416 return;
3417 }
3418 NEXT;
3419 } else if (cur == 'P') {
3420 NEXT;
3421 if (CUR != '{') {
3422 ERROR("Expecting '{'");
3423 return;
3424 }
3425 NEXT;
3426 xmlFAParseCharProp(ctxt);
3427 ctxt->atom->neg = 1;
3428 if (CUR != '}') {
3429 ERROR("Expecting '}'");
3430 return;
3431 }
3432 NEXT;
3433 } else if ((cur == 'n') || (cur == 'r') || (cur == 't') || (cur == '\\') ||
3434 (cur == '|') || (cur == '.') || (cur == '?') || (cur == '*') ||
3435 (cur == '+') || (cur == '(') || (cur == ')') || (cur == '{') ||
3436 (cur == '}') || (cur == 0x2D) || (cur == 0x5B) || (cur == 0x5D) ||
3437 (cur == 0x5E)) {
3438 if (ctxt->atom == NULL) {
3439 ctxt->atom = xmlRegNewAtom(ctxt, XML_REGEXP_CHARVAL);
3440 if (ctxt->atom != NULL)
3441 ctxt->atom->codepoint = cur;
3442 } else if (ctxt->atom->type == XML_REGEXP_RANGES) {
3443 xmlRegAtomAddRange(ctxt, ctxt->atom, ctxt->neg,
3444 XML_REGEXP_CHARVAL, cur, cur, NULL);
3445 }
3446 NEXT;
3447 } else if ((cur == 's') || (cur == 'S') || (cur == 'i') || (cur == 'I') ||
3448 (cur == 'c') || (cur == 'C') || (cur == 'd') || (cur == 'D') ||
3449 (cur == 'w') || (cur == 'W')) {
Daniel Veillardb509f152002-04-17 16:28:10 +00003450 xmlRegAtomType type = XML_REGEXP_ANYSPACE;
Daniel Veillard4255d502002-04-16 15:50:10 +00003451
3452 switch (cur) {
3453 case 's':
3454 type = XML_REGEXP_ANYSPACE;
3455 break;
3456 case 'S':
3457 type = XML_REGEXP_NOTSPACE;
3458 break;
3459 case 'i':
3460 type = XML_REGEXP_INITNAME;
3461 break;
3462 case 'I':
3463 type = XML_REGEXP_NOTINITNAME;
3464 break;
3465 case 'c':
3466 type = XML_REGEXP_NAMECHAR;
3467 break;
3468 case 'C':
3469 type = XML_REGEXP_NOTNAMECHAR;
3470 break;
3471 case 'd':
3472 type = XML_REGEXP_DECIMAL;
3473 break;
3474 case 'D':
3475 type = XML_REGEXP_NOTDECIMAL;
3476 break;
3477 case 'w':
3478 type = XML_REGEXP_REALCHAR;
3479 break;
3480 case 'W':
3481 type = XML_REGEXP_NOTREALCHAR;
3482 break;
3483 }
3484 NEXT;
3485 if (ctxt->atom == NULL) {
3486 ctxt->atom = xmlRegNewAtom(ctxt, type);
3487 } else if (ctxt->atom->type == XML_REGEXP_RANGES) {
3488 xmlRegAtomAddRange(ctxt, ctxt->atom, ctxt->neg,
3489 type, 0, 0, NULL);
3490 }
3491 }
3492}
3493
3494/**
3495 * xmlFAParseCharRef:
Daniel Veillard441bc322002-04-20 17:38:48 +00003496 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00003497 *
3498 * [19] XmlCharRef ::= ( '&#' [0-9]+ ';' ) | (' &#x' [0-9a-fA-F]+ ';' )
3499 */
3500static int
3501xmlFAParseCharRef(xmlRegParserCtxtPtr ctxt) {
3502 int ret = 0, cur;
3503
3504 if ((CUR != '&') || (NXT(1) != '#'))
3505 return(-1);
3506 NEXT;
3507 NEXT;
3508 cur = CUR;
3509 if (cur == 'x') {
3510 NEXT;
3511 cur = CUR;
3512 if (((cur >= '0') && (cur <= '9')) ||
3513 ((cur >= 'a') && (cur <= 'f')) ||
3514 ((cur >= 'A') && (cur <= 'F'))) {
3515 while (((cur >= '0') && (cur <= '9')) ||
3516 ((cur >= 'A') && (cur <= 'F'))) {
3517 if ((cur >= '0') && (cur <= '9'))
3518 ret = ret * 16 + cur - '0';
3519 else if ((cur >= 'a') && (cur <= 'f'))
3520 ret = ret * 16 + 10 + (cur - 'a');
3521 else
3522 ret = ret * 16 + 10 + (cur - 'A');
3523 NEXT;
3524 cur = CUR;
3525 }
3526 } else {
3527 ERROR("Char ref: expecting [0-9A-F]");
3528 return(-1);
3529 }
3530 } else {
3531 if ((cur >= '0') && (cur <= '9')) {
3532 while ((cur >= '0') && (cur <= '9')) {
3533 ret = ret * 10 + cur - '0';
3534 NEXT;
3535 cur = CUR;
3536 }
3537 } else {
3538 ERROR("Char ref: expecting [0-9]");
3539 return(-1);
3540 }
3541 }
3542 if (cur != ';') {
3543 ERROR("Char ref: expecting ';'");
3544 return(-1);
3545 } else {
3546 NEXT;
3547 }
3548 return(ret);
3549}
3550
3551/**
3552 * xmlFAParseCharRange:
Daniel Veillard441bc322002-04-20 17:38:48 +00003553 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00003554 *
3555 * [17] charRange ::= seRange | XmlCharRef | XmlCharIncDash
3556 * [18] seRange ::= charOrEsc '-' charOrEsc
3557 * [20] charOrEsc ::= XmlChar | SingleCharEsc
3558 * [21] XmlChar ::= [^\#x2D#x5B#x5D]
3559 * [22] XmlCharIncDash ::= [^\#x5B#x5D]
3560 */
3561static void
3562xmlFAParseCharRange(xmlRegParserCtxtPtr ctxt) {
William M. Brackdc99df92003-12-27 01:54:25 +00003563 int cur, len;
Daniel Veillard4255d502002-04-16 15:50:10 +00003564 int start = -1;
3565 int end = -1;
3566
3567 if ((CUR == '&') && (NXT(1) == '#')) {
3568 end = start = xmlFAParseCharRef(ctxt);
3569 xmlRegAtomAddRange(ctxt, ctxt->atom, ctxt->neg,
3570 XML_REGEXP_CHARVAL, start, end, NULL);
3571 return;
3572 }
3573 cur = CUR;
3574 if (cur == '\\') {
3575 NEXT;
3576 cur = CUR;
3577 switch (cur) {
3578 case 'n': start = 0xA; break;
3579 case 'r': start = 0xD; break;
3580 case 't': start = 0x9; break;
3581 case '\\': case '|': case '.': case '-': case '^': case '?':
3582 case '*': case '+': case '{': case '}': case '(': case ')':
3583 case '[': case ']':
3584 start = cur; break;
3585 default:
3586 ERROR("Invalid escape value");
3587 return;
3588 }
3589 end = start;
William M. Brackdc99df92003-12-27 01:54:25 +00003590 len = 1;
Daniel Veillard4255d502002-04-16 15:50:10 +00003591 } else if ((cur != 0x5B) && (cur != 0x5D)) {
William M. Brackdc99df92003-12-27 01:54:25 +00003592 end = start = CUR_SCHAR(ctxt->cur, len);
Daniel Veillard4255d502002-04-16 15:50:10 +00003593 } else {
3594 ERROR("Expecting a char range");
3595 return;
3596 }
William M. Brackdc99df92003-12-27 01:54:25 +00003597 NEXTL(len);
Daniel Veillard4255d502002-04-16 15:50:10 +00003598 if (start == '-') {
3599 return;
3600 }
3601 cur = CUR;
William M. Brack10f1ef42004-03-20 14:51:25 +00003602 if ((cur != '-') || (NXT(1) == ']')) {
Daniel Veillard4255d502002-04-16 15:50:10 +00003603 xmlRegAtomAddRange(ctxt, ctxt->atom, ctxt->neg,
3604 XML_REGEXP_CHARVAL, start, end, NULL);
3605 return;
3606 }
3607 NEXT;
3608 cur = CUR;
3609 if (cur == '\\') {
3610 NEXT;
3611 cur = CUR;
3612 switch (cur) {
3613 case 'n': end = 0xA; break;
3614 case 'r': end = 0xD; break;
3615 case 't': end = 0x9; break;
3616 case '\\': case '|': case '.': case '-': case '^': case '?':
3617 case '*': case '+': case '{': case '}': case '(': case ')':
3618 case '[': case ']':
3619 end = cur; break;
3620 default:
3621 ERROR("Invalid escape value");
3622 return;
3623 }
William M. Brackdc99df92003-12-27 01:54:25 +00003624 len = 1;
Daniel Veillard4255d502002-04-16 15:50:10 +00003625 } else if ((cur != 0x5B) && (cur != 0x5D)) {
William M. Brackdc99df92003-12-27 01:54:25 +00003626 end = CUR_SCHAR(ctxt->cur, len);
Daniel Veillard4255d502002-04-16 15:50:10 +00003627 } else {
3628 ERROR("Expecting the end of a char range");
3629 return;
3630 }
William M. Brackdc99df92003-12-27 01:54:25 +00003631 NEXTL(len);
Daniel Veillard4255d502002-04-16 15:50:10 +00003632 /* TODO check that the values are acceptable character ranges for XML */
3633 if (end < start) {
3634 ERROR("End of range is before start of range");
3635 } else {
3636 xmlRegAtomAddRange(ctxt, ctxt->atom, ctxt->neg,
3637 XML_REGEXP_CHARVAL, start, end, NULL);
3638 }
3639 return;
3640}
3641
3642/**
3643 * xmlFAParsePosCharGroup:
Daniel Veillard441bc322002-04-20 17:38:48 +00003644 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00003645 *
3646 * [14] posCharGroup ::= ( charRange | charClassEsc )+
3647 */
3648static void
3649xmlFAParsePosCharGroup(xmlRegParserCtxtPtr ctxt) {
3650 do {
3651 if ((CUR == '\\') || (CUR == '.')) {
3652 xmlFAParseCharClassEsc(ctxt);
3653 } else {
3654 xmlFAParseCharRange(ctxt);
3655 }
3656 } while ((CUR != ']') && (CUR != '^') && (CUR != '-') &&
3657 (ctxt->error == 0));
3658}
3659
3660/**
3661 * xmlFAParseCharGroup:
Daniel Veillard441bc322002-04-20 17:38:48 +00003662 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00003663 *
3664 * [13] charGroup ::= posCharGroup | negCharGroup | charClassSub
3665 * [15] negCharGroup ::= '^' posCharGroup
3666 * [16] charClassSub ::= ( posCharGroup | negCharGroup ) '-' charClassExpr
3667 * [12] charClassExpr ::= '[' charGroup ']'
3668 */
3669static void
3670xmlFAParseCharGroup(xmlRegParserCtxtPtr ctxt) {
3671 int n = ctxt->neg;
3672 while ((CUR != ']') && (ctxt->error == 0)) {
3673 if (CUR == '^') {
3674 int neg = ctxt->neg;
3675
3676 NEXT;
3677 ctxt->neg = !ctxt->neg;
3678 xmlFAParsePosCharGroup(ctxt);
3679 ctxt->neg = neg;
William M. Brack10f1ef42004-03-20 14:51:25 +00003680 } else if ((CUR == '-') && (NXT(1) == '[')) {
Daniel Veillardf8b9de32003-11-24 14:27:26 +00003681 int neg = ctxt->neg;
Daniel Veillardf8b9de32003-11-24 14:27:26 +00003682 ctxt->neg = 2;
William M. Brack10f1ef42004-03-20 14:51:25 +00003683 NEXT; /* eat the '-' */
3684 NEXT; /* eat the '[' */
Daniel Veillard4255d502002-04-16 15:50:10 +00003685 xmlFAParseCharGroup(ctxt);
3686 if (CUR == ']') {
3687 NEXT;
3688 } else {
3689 ERROR("charClassExpr: ']' expected");
3690 break;
3691 }
Daniel Veillardf8b9de32003-11-24 14:27:26 +00003692 ctxt->neg = neg;
Daniel Veillard4255d502002-04-16 15:50:10 +00003693 break;
3694 } else if (CUR != ']') {
3695 xmlFAParsePosCharGroup(ctxt);
3696 }
3697 }
3698 ctxt->neg = n;
3699}
3700
3701/**
3702 * xmlFAParseCharClass:
Daniel Veillard441bc322002-04-20 17:38:48 +00003703 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00003704 *
3705 * [11] charClass ::= charClassEsc | charClassExpr
3706 * [12] charClassExpr ::= '[' charGroup ']'
3707 */
3708static void
3709xmlFAParseCharClass(xmlRegParserCtxtPtr ctxt) {
3710 if (CUR == '[') {
3711 NEXT;
3712 ctxt->atom = xmlRegNewAtom(ctxt, XML_REGEXP_RANGES);
3713 if (ctxt->atom == NULL)
3714 return;
3715 xmlFAParseCharGroup(ctxt);
3716 if (CUR == ']') {
3717 NEXT;
3718 } else {
3719 ERROR("xmlFAParseCharClass: ']' expected");
3720 }
3721 } else {
3722 xmlFAParseCharClassEsc(ctxt);
3723 }
3724}
3725
3726/**
3727 * xmlFAParseQuantExact:
Daniel Veillard441bc322002-04-20 17:38:48 +00003728 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00003729 *
3730 * [8] QuantExact ::= [0-9]+
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00003731 *
3732 * Returns 0 if success or -1 in case of error
Daniel Veillard4255d502002-04-16 15:50:10 +00003733 */
3734static int
3735xmlFAParseQuantExact(xmlRegParserCtxtPtr ctxt) {
3736 int ret = 0;
3737 int ok = 0;
3738
3739 while ((CUR >= '0') && (CUR <= '9')) {
3740 ret = ret * 10 + (CUR - '0');
3741 ok = 1;
3742 NEXT;
3743 }
3744 if (ok != 1) {
3745 return(-1);
3746 }
3747 return(ret);
3748}
3749
3750/**
3751 * xmlFAParseQuantifier:
Daniel Veillard441bc322002-04-20 17:38:48 +00003752 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00003753 *
3754 * [4] quantifier ::= [?*+] | ( '{' quantity '}' )
3755 * [5] quantity ::= quantRange | quantMin | QuantExact
3756 * [6] quantRange ::= QuantExact ',' QuantExact
3757 * [7] quantMin ::= QuantExact ','
3758 * [8] QuantExact ::= [0-9]+
3759 */
3760static int
3761xmlFAParseQuantifier(xmlRegParserCtxtPtr ctxt) {
3762 int cur;
3763
3764 cur = CUR;
3765 if ((cur == '?') || (cur == '*') || (cur == '+')) {
3766 if (ctxt->atom != NULL) {
3767 if (cur == '?')
3768 ctxt->atom->quant = XML_REGEXP_QUANT_OPT;
3769 else if (cur == '*')
3770 ctxt->atom->quant = XML_REGEXP_QUANT_MULT;
3771 else if (cur == '+')
3772 ctxt->atom->quant = XML_REGEXP_QUANT_PLUS;
3773 }
3774 NEXT;
3775 return(1);
3776 }
3777 if (cur == '{') {
3778 int min = 0, max = 0;
3779
3780 NEXT;
3781 cur = xmlFAParseQuantExact(ctxt);
3782 if (cur >= 0)
3783 min = cur;
3784 if (CUR == ',') {
3785 NEXT;
Daniel Veillardebe48c62003-12-03 12:12:27 +00003786 if (CUR == '}')
3787 max = INT_MAX;
3788 else {
3789 cur = xmlFAParseQuantExact(ctxt);
3790 if (cur >= 0)
3791 max = cur;
3792 else {
3793 ERROR("Improper quantifier");
3794 }
3795 }
Daniel Veillard4255d502002-04-16 15:50:10 +00003796 }
3797 if (CUR == '}') {
3798 NEXT;
3799 } else {
3800 ERROR("Unterminated quantifier");
3801 }
3802 if (max == 0)
3803 max = min;
3804 if (ctxt->atom != NULL) {
3805 ctxt->atom->quant = XML_REGEXP_QUANT_RANGE;
3806 ctxt->atom->min = min;
3807 ctxt->atom->max = max;
3808 }
3809 return(1);
3810 }
3811 return(0);
3812}
3813
3814/**
3815 * xmlFAParseAtom:
Daniel Veillard441bc322002-04-20 17:38:48 +00003816 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00003817 *
3818 * [9] atom ::= Char | charClass | ( '(' regExp ')' )
3819 */
3820static int
3821xmlFAParseAtom(xmlRegParserCtxtPtr ctxt) {
3822 int codepoint, len;
3823
3824 codepoint = xmlFAIsChar(ctxt);
3825 if (codepoint > 0) {
3826 ctxt->atom = xmlRegNewAtom(ctxt, XML_REGEXP_CHARVAL);
3827 if (ctxt->atom == NULL)
3828 return(-1);
3829 codepoint = CUR_SCHAR(ctxt->cur, len);
3830 ctxt->atom->codepoint = codepoint;
3831 NEXTL(len);
3832 return(1);
3833 } else if (CUR == '|') {
3834 return(0);
3835 } else if (CUR == 0) {
3836 return(0);
3837 } else if (CUR == ')') {
3838 return(0);
3839 } else if (CUR == '(') {
3840 xmlRegStatePtr start, oldend;
3841
3842 NEXT;
3843 xmlFAGenerateEpsilonTransition(ctxt, ctxt->state, NULL);
3844 start = ctxt->state;
3845 oldend = ctxt->end;
3846 ctxt->end = NULL;
3847 ctxt->atom = NULL;
3848 xmlFAParseRegExp(ctxt, 0);
3849 if (CUR == ')') {
3850 NEXT;
3851 } else {
3852 ERROR("xmlFAParseAtom: expecting ')'");
3853 }
3854 ctxt->atom = xmlRegNewAtom(ctxt, XML_REGEXP_SUBREG);
3855 if (ctxt->atom == NULL)
3856 return(-1);
3857 ctxt->atom->start = start;
3858 ctxt->atom->stop = ctxt->state;
3859 ctxt->end = oldend;
3860 return(1);
3861 } else if ((CUR == '[') || (CUR == '\\') || (CUR == '.')) {
3862 xmlFAParseCharClass(ctxt);
3863 return(1);
3864 }
3865 return(0);
3866}
3867
3868/**
3869 * xmlFAParsePiece:
Daniel Veillard441bc322002-04-20 17:38:48 +00003870 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00003871 *
3872 * [3] piece ::= atom quantifier?
3873 */
3874static int
3875xmlFAParsePiece(xmlRegParserCtxtPtr ctxt) {
3876 int ret;
3877
3878 ctxt->atom = NULL;
3879 ret = xmlFAParseAtom(ctxt);
3880 if (ret == 0)
3881 return(0);
3882 if (ctxt->atom == NULL) {
3883 ERROR("internal: no atom generated");
3884 }
3885 xmlFAParseQuantifier(ctxt);
3886 return(1);
3887}
3888
3889/**
3890 * xmlFAParseBranch:
Daniel Veillard441bc322002-04-20 17:38:48 +00003891 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00003892 *
3893 * [2] branch ::= piece*
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00003894 8
Daniel Veillard4255d502002-04-16 15:50:10 +00003895 */
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00003896static int
Daniel Veillard2cbf5962004-03-31 15:50:43 +00003897xmlFAParseBranch(xmlRegParserCtxtPtr ctxt) {
Daniel Veillard4255d502002-04-16 15:50:10 +00003898 xmlRegStatePtr previous;
Daniel Veillard4255d502002-04-16 15:50:10 +00003899 int ret;
3900
3901 previous = ctxt->state;
3902 ret = xmlFAParsePiece(ctxt);
3903 if (ret != 0) {
Daniel Veillard2cbf5962004-03-31 15:50:43 +00003904 if (xmlFAGenerateTransitions(ctxt, previous, NULL, ctxt->atom) < 0)
3905 return(-1);
3906 previous = ctxt->state;
Daniel Veillard4255d502002-04-16 15:50:10 +00003907 ctxt->atom = NULL;
3908 }
3909 while ((ret != 0) && (ctxt->error == 0)) {
3910 ret = xmlFAParsePiece(ctxt);
3911 if (ret != 0) {
Daniel Veillard2cbf5962004-03-31 15:50:43 +00003912 if (xmlFAGenerateTransitions(ctxt, previous, NULL,
3913 ctxt->atom) < 0)
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00003914 return(-1);
Daniel Veillard4255d502002-04-16 15:50:10 +00003915 previous = ctxt->state;
3916 ctxt->atom = NULL;
3917 }
3918 }
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00003919 return(0);
Daniel Veillard4255d502002-04-16 15:50:10 +00003920}
3921
3922/**
3923 * xmlFAParseRegExp:
Daniel Veillard441bc322002-04-20 17:38:48 +00003924 * @ctxt: a regexp parser context
3925 * @top: is that the top-level expressions ?
Daniel Veillard4255d502002-04-16 15:50:10 +00003926 *
3927 * [1] regExp ::= branch ( '|' branch )*
3928 */
3929static void
3930xmlFAParseRegExp(xmlRegParserCtxtPtr ctxt, int top) {
Daniel Veillard2cbf5962004-03-31 15:50:43 +00003931 xmlRegStatePtr start, end, oldend, oldstart;
Daniel Veillard4255d502002-04-16 15:50:10 +00003932
3933 oldend = ctxt->end;
3934
Daniel Veillard2cbf5962004-03-31 15:50:43 +00003935 oldstart = ctxt->state;
3936 /* if not top start should have been generated by an epsilon trans */
Daniel Veillard4255d502002-04-16 15:50:10 +00003937 start = ctxt->state;
Daniel Veillard2cbf5962004-03-31 15:50:43 +00003938 ctxt->end = NULL;
3939 xmlFAParseBranch(ctxt);
3940 if (top) {
3941#ifdef DEBUG_REGEXP_GRAPH
3942 printf("State %d is final\n", ctxt->state->no);
3943#endif
3944 ctxt->state->type = XML_REGEXP_FINAL_STATE;
3945 }
Daniel Veillard4255d502002-04-16 15:50:10 +00003946 if (CUR != '|') {
3947 ctxt->end = ctxt->state;
3948 return;
3949 }
3950 end = ctxt->state;
3951 while ((CUR == '|') && (ctxt->error == 0)) {
3952 NEXT;
3953 ctxt->state = start;
Daniel Veillard2cbf5962004-03-31 15:50:43 +00003954 ctxt->end = NULL;
3955 xmlFAParseBranch(ctxt);
3956 if (top) {
3957 ctxt->state->type = XML_REGEXP_FINAL_STATE;
3958#ifdef DEBUG_REGEXP_GRAPH
3959 printf("State %d is final\n", ctxt->state->no);
3960#endif
3961 } else {
3962 xmlFAGenerateEpsilonTransition(ctxt, ctxt->state, end);
3963 }
Daniel Veillard4255d502002-04-16 15:50:10 +00003964 }
Daniel Veillard2cbf5962004-03-31 15:50:43 +00003965 if (!top) {
3966 ctxt->state = end;
3967 ctxt->end = end;
3968 }
Daniel Veillard4255d502002-04-16 15:50:10 +00003969}
3970
3971/************************************************************************
3972 * *
3973 * The basic API *
3974 * *
3975 ************************************************************************/
3976
3977/**
3978 * xmlRegexpPrint:
3979 * @output: the file for the output debug
3980 * @regexp: the compiled regexp
3981 *
3982 * Print the content of the compiled regular expression
3983 */
3984void
3985xmlRegexpPrint(FILE *output, xmlRegexpPtr regexp) {
3986 int i;
3987
3988 fprintf(output, " regexp: ");
3989 if (regexp == NULL) {
3990 fprintf(output, "NULL\n");
3991 return;
3992 }
3993 fprintf(output, "'%s' ", regexp->string);
3994 fprintf(output, "\n");
3995 fprintf(output, "%d atoms:\n", regexp->nbAtoms);
3996 for (i = 0;i < regexp->nbAtoms; i++) {
3997 fprintf(output, " %02d ", i);
3998 xmlRegPrintAtom(output, regexp->atoms[i]);
3999 }
4000 fprintf(output, "%d states:", regexp->nbStates);
4001 fprintf(output, "\n");
4002 for (i = 0;i < regexp->nbStates; i++) {
4003 xmlRegPrintState(output, regexp->states[i]);
4004 }
4005 fprintf(output, "%d counters:\n", regexp->nbCounters);
4006 for (i = 0;i < regexp->nbCounters; i++) {
4007 fprintf(output, " %d: min %d max %d\n", i, regexp->counters[i].min,
4008 regexp->counters[i].max);
4009 }
4010}
4011
4012/**
4013 * xmlRegexpCompile:
4014 * @regexp: a regular expression string
4015 *
4016 * Parses a regular expression conforming to XML Schemas Part 2 Datatype
4017 * Appendix F and build an automata suitable for testing strings against
4018 * that regular expression
4019 *
4020 * Returns the compiled expression or NULL in case of error
4021 */
4022xmlRegexpPtr
4023xmlRegexpCompile(const xmlChar *regexp) {
4024 xmlRegexpPtr ret;
4025 xmlRegParserCtxtPtr ctxt;
4026
4027 ctxt = xmlRegNewParserCtxt(regexp);
4028 if (ctxt == NULL)
4029 return(NULL);
4030
4031 /* initialize the parser */
4032 ctxt->end = NULL;
4033 ctxt->start = ctxt->state = xmlRegNewState(ctxt);
4034 xmlRegStatePush(ctxt, ctxt->start);
4035
4036 /* parse the expression building an automata */
4037 xmlFAParseRegExp(ctxt, 1);
4038 if (CUR != 0) {
4039 ERROR("xmlFAParseRegExp: extra characters");
4040 }
4041 ctxt->end = ctxt->state;
4042 ctxt->start->type = XML_REGEXP_START_STATE;
4043 ctxt->end->type = XML_REGEXP_FINAL_STATE;
4044
4045 /* remove the Epsilon except for counted transitions */
4046 xmlFAEliminateEpsilonTransitions(ctxt);
4047
4048
4049 if (ctxt->error != 0) {
4050 xmlRegFreeParserCtxt(ctxt);
4051 return(NULL);
4052 }
4053 ret = xmlRegEpxFromParse(ctxt);
4054 xmlRegFreeParserCtxt(ctxt);
4055 return(ret);
4056}
4057
4058/**
4059 * xmlRegexpExec:
4060 * @comp: the compiled regular expression
4061 * @content: the value to check against the regular expression
4062 *
4063 * Check if the regular expression generate the value
4064 *
4065 * Returns 1 if it matches, 0 if not and a negativa value in case of error
4066 */
4067int
4068xmlRegexpExec(xmlRegexpPtr comp, const xmlChar *content) {
4069 if ((comp == NULL) || (content == NULL))
4070 return(-1);
4071 return(xmlFARegExec(comp, content));
4072}
4073
4074/**
Daniel Veillard23e73572002-09-19 19:56:43 +00004075 * xmlRegexpIsDeterminist:
4076 * @comp: the compiled regular expression
4077 *
4078 * Check if the regular expression is determinist
4079 *
4080 * Returns 1 if it yes, 0 if not and a negativa value in case of error
4081 */
4082int
4083xmlRegexpIsDeterminist(xmlRegexpPtr comp) {
4084 xmlAutomataPtr am;
4085 int ret;
4086
4087 if (comp == NULL)
4088 return(-1);
4089 if (comp->determinist != -1)
4090 return(comp->determinist);
4091
4092 am = xmlNewAutomata();
Daniel Veillardbd9afb52002-09-25 22:25:35 +00004093 if (am->states != NULL) {
4094 int i;
4095
4096 for (i = 0;i < am->nbStates;i++)
4097 xmlRegFreeState(am->states[i]);
4098 xmlFree(am->states);
4099 }
Daniel Veillard23e73572002-09-19 19:56:43 +00004100 am->nbAtoms = comp->nbAtoms;
4101 am->atoms = comp->atoms;
4102 am->nbStates = comp->nbStates;
4103 am->states = comp->states;
4104 am->determinist = -1;
4105 ret = xmlFAComputesDeterminism(am);
4106 am->atoms = NULL;
4107 am->states = NULL;
4108 xmlFreeAutomata(am);
4109 return(ret);
4110}
4111
4112/**
Daniel Veillard4255d502002-04-16 15:50:10 +00004113 * xmlRegFreeRegexp:
4114 * @regexp: the regexp
4115 *
4116 * Free a regexp
4117 */
4118void
4119xmlRegFreeRegexp(xmlRegexpPtr regexp) {
4120 int i;
4121 if (regexp == NULL)
4122 return;
4123
4124 if (regexp->string != NULL)
4125 xmlFree(regexp->string);
4126 if (regexp->states != NULL) {
4127 for (i = 0;i < regexp->nbStates;i++)
4128 xmlRegFreeState(regexp->states[i]);
4129 xmlFree(regexp->states);
4130 }
4131 if (regexp->atoms != NULL) {
4132 for (i = 0;i < regexp->nbAtoms;i++)
4133 xmlRegFreeAtom(regexp->atoms[i]);
4134 xmlFree(regexp->atoms);
4135 }
4136 if (regexp->counters != NULL)
4137 xmlFree(regexp->counters);
Daniel Veillard23e73572002-09-19 19:56:43 +00004138 if (regexp->compact != NULL)
4139 xmlFree(regexp->compact);
Daniel Veillard118aed72002-09-24 14:13:13 +00004140 if (regexp->transdata != NULL)
4141 xmlFree(regexp->transdata);
Daniel Veillard23e73572002-09-19 19:56:43 +00004142 if (regexp->stringMap != NULL) {
4143 for (i = 0; i < regexp->nbstrings;i++)
4144 xmlFree(regexp->stringMap[i]);
4145 xmlFree(regexp->stringMap);
4146 }
4147
Daniel Veillard4255d502002-04-16 15:50:10 +00004148 xmlFree(regexp);
4149}
4150
4151#ifdef LIBXML_AUTOMATA_ENABLED
4152/************************************************************************
4153 * *
4154 * The Automata interface *
4155 * *
4156 ************************************************************************/
4157
4158/**
4159 * xmlNewAutomata:
4160 *
4161 * Create a new automata
4162 *
4163 * Returns the new object or NULL in case of failure
4164 */
4165xmlAutomataPtr
4166xmlNewAutomata(void) {
4167 xmlAutomataPtr ctxt;
4168
4169 ctxt = xmlRegNewParserCtxt(NULL);
4170 if (ctxt == NULL)
4171 return(NULL);
4172
4173 /* initialize the parser */
4174 ctxt->end = NULL;
4175 ctxt->start = ctxt->state = xmlRegNewState(ctxt);
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00004176 if (ctxt->start == NULL) {
4177 xmlFreeAutomata(ctxt);
4178 return(NULL);
4179 }
4180 if (xmlRegStatePush(ctxt, ctxt->start) < 0) {
4181 xmlRegFreeState(ctxt->start);
4182 xmlFreeAutomata(ctxt);
4183 return(NULL);
4184 }
Daniel Veillard4255d502002-04-16 15:50:10 +00004185
4186 return(ctxt);
4187}
4188
4189/**
4190 * xmlFreeAutomata:
4191 * @am: an automata
4192 *
4193 * Free an automata
4194 */
4195void
4196xmlFreeAutomata(xmlAutomataPtr am) {
4197 if (am == NULL)
4198 return;
4199 xmlRegFreeParserCtxt(am);
4200}
4201
4202/**
4203 * xmlAutomataGetInitState:
4204 * @am: an automata
4205 *
Daniel Veillarda9b66d02002-12-11 14:23:49 +00004206 * Initial state lookup
4207 *
Daniel Veillard4255d502002-04-16 15:50:10 +00004208 * Returns the initial state of the automata
4209 */
4210xmlAutomataStatePtr
4211xmlAutomataGetInitState(xmlAutomataPtr am) {
4212 if (am == NULL)
4213 return(NULL);
4214 return(am->start);
4215}
4216
4217/**
4218 * xmlAutomataSetFinalState:
4219 * @am: an automata
4220 * @state: a state in this automata
4221 *
4222 * Makes that state a final state
4223 *
4224 * Returns 0 or -1 in case of error
4225 */
4226int
4227xmlAutomataSetFinalState(xmlAutomataPtr am, xmlAutomataStatePtr state) {
4228 if ((am == NULL) || (state == NULL))
4229 return(-1);
4230 state->type = XML_REGEXP_FINAL_STATE;
4231 return(0);
4232}
4233
4234/**
4235 * xmlAutomataNewTransition:
4236 * @am: an automata
4237 * @from: the starting point of the transition
4238 * @to: the target point of the transition or NULL
4239 * @token: the input string associated to that transition
4240 * @data: data passed to the callback function if the transition is activated
4241 *
4242 * If @to is NULL, this create first a new target state in the automata
4243 * and then adds a transition from the @from state to the target state
4244 * activated by the value of @token
4245 *
4246 * Returns the target state or NULL in case of error
4247 */
4248xmlAutomataStatePtr
4249xmlAutomataNewTransition(xmlAutomataPtr am, xmlAutomataStatePtr from,
4250 xmlAutomataStatePtr to, const xmlChar *token,
4251 void *data) {
4252 xmlRegAtomPtr atom;
4253
4254 if ((am == NULL) || (from == NULL) || (token == NULL))
4255 return(NULL);
4256 atom = xmlRegNewAtom(am, XML_REGEXP_STRING);
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00004257 if (atom == NULL)
4258 return(NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +00004259 atom->data = data;
4260 if (atom == NULL)
4261 return(NULL);
4262 atom->valuep = xmlStrdup(token);
4263
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00004264 if (xmlFAGenerateTransitions(am, from, to, atom) < 0) {
4265 xmlRegFreeAtom(atom);
4266 return(NULL);
4267 }
Daniel Veillard4255d502002-04-16 15:50:10 +00004268 if (to == NULL)
4269 return(am->state);
4270 return(to);
4271}
4272
4273/**
Daniel Veillard52b48c72003-04-13 19:53:42 +00004274 * xmlAutomataNewTransition2:
4275 * @am: an automata
4276 * @from: the starting point of the transition
4277 * @to: the target point of the transition or NULL
4278 * @token: the first input string associated to that transition
4279 * @token2: the second input string associated to that transition
4280 * @data: data passed to the callback function if the transition is activated
4281 *
4282 * If @to is NULL, this create first a new target state in the automata
4283 * and then adds a transition from the @from state to the target state
4284 * activated by the value of @token
4285 *
4286 * Returns the target state or NULL in case of error
4287 */
4288xmlAutomataStatePtr
4289xmlAutomataNewTransition2(xmlAutomataPtr am, xmlAutomataStatePtr from,
4290 xmlAutomataStatePtr to, const xmlChar *token,
4291 const xmlChar *token2, void *data) {
4292 xmlRegAtomPtr atom;
4293
4294 if ((am == NULL) || (from == NULL) || (token == NULL))
4295 return(NULL);
4296 atom = xmlRegNewAtom(am, XML_REGEXP_STRING);
4297 atom->data = data;
4298 if (atom == NULL)
4299 return(NULL);
4300 if ((token2 == NULL) || (*token2 == 0)) {
4301 atom->valuep = xmlStrdup(token);
4302 } else {
4303 int lenn, lenp;
4304 xmlChar *str;
4305
4306 lenn = strlen((char *) token2);
4307 lenp = strlen((char *) token);
4308
Daniel Veillard3c908dc2003-04-19 00:07:51 +00004309 str = (xmlChar *) xmlMallocAtomic(lenn + lenp + 2);
Daniel Veillard52b48c72003-04-13 19:53:42 +00004310 if (str == NULL) {
4311 xmlRegFreeAtom(atom);
4312 return(NULL);
4313 }
4314 memcpy(&str[0], token, lenp);
4315 str[lenp] = '|';
4316 memcpy(&str[lenp + 1], token2, lenn);
4317 str[lenn + lenp + 1] = 0;
4318
4319 atom->valuep = str;
4320 }
4321
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00004322 if (xmlFAGenerateTransitions(am, from, to, atom) < 0) {
4323 xmlRegFreeAtom(atom);
4324 return(NULL);
4325 }
Daniel Veillard52b48c72003-04-13 19:53:42 +00004326 if (to == NULL)
4327 return(am->state);
4328 return(to);
4329}
4330
4331/**
Daniel Veillard4255d502002-04-16 15:50:10 +00004332 * xmlAutomataNewCountTrans:
4333 * @am: an automata
4334 * @from: the starting point of the transition
4335 * @to: the target point of the transition or NULL
4336 * @token: the input string associated to that transition
4337 * @min: the minimum successive occurences of token
Daniel Veillarda9b66d02002-12-11 14:23:49 +00004338 * @max: the maximum successive occurences of token
4339 * @data: data associated to the transition
Daniel Veillard4255d502002-04-16 15:50:10 +00004340 *
4341 * If @to is NULL, this create first a new target state in the automata
4342 * and then adds a transition from the @from state to the target state
4343 * activated by a succession of input of value @token and whose number
4344 * is between @min and @max
4345 *
4346 * Returns the target state or NULL in case of error
4347 */
4348xmlAutomataStatePtr
4349xmlAutomataNewCountTrans(xmlAutomataPtr am, xmlAutomataStatePtr from,
4350 xmlAutomataStatePtr to, const xmlChar *token,
4351 int min, int max, void *data) {
4352 xmlRegAtomPtr atom;
Daniel Veillard0ddb21c2004-02-12 12:43:49 +00004353 int counter;
Daniel Veillard4255d502002-04-16 15:50:10 +00004354
4355 if ((am == NULL) || (from == NULL) || (token == NULL))
4356 return(NULL);
4357 if (min < 0)
4358 return(NULL);
4359 if ((max < min) || (max < 1))
4360 return(NULL);
4361 atom = xmlRegNewAtom(am, XML_REGEXP_STRING);
4362 if (atom == NULL)
4363 return(NULL);
4364 atom->valuep = xmlStrdup(token);
4365 atom->data = data;
4366 if (min == 0)
4367 atom->min = 1;
4368 else
4369 atom->min = min;
4370 atom->max = max;
4371
Daniel Veillard0ddb21c2004-02-12 12:43:49 +00004372 /*
4373 * associate a counter to the transition.
4374 */
4375 counter = xmlRegGetCounter(am);
4376 am->counters[counter].min = min;
4377 am->counters[counter].max = max;
4378
4379 /* xmlFAGenerateTransitions(am, from, to, atom); */
4380 if (to == NULL) {
4381 to = xmlRegNewState(am);
4382 xmlRegStatePush(am, to);
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00004383 }
Daniel Veillard0ddb21c2004-02-12 12:43:49 +00004384 xmlRegStateAddTrans(am, from, atom, to, counter, -1);
4385 xmlRegAtomPush(am, atom);
4386 am->state = to;
4387
Daniel Veillard4255d502002-04-16 15:50:10 +00004388 if (to == NULL)
4389 to = am->state;
4390 if (to == NULL)
4391 return(NULL);
4392 if (min == 0)
4393 xmlFAGenerateEpsilonTransition(am, from, to);
4394 return(to);
4395}
4396
4397/**
Daniel Veillard7646b182002-04-20 06:41:40 +00004398 * xmlAutomataNewOnceTrans:
4399 * @am: an automata
4400 * @from: the starting point of the transition
4401 * @to: the target point of the transition or NULL
4402 * @token: the input string associated to that transition
4403 * @min: the minimum successive occurences of token
Daniel Veillarda9b66d02002-12-11 14:23:49 +00004404 * @max: the maximum successive occurences of token
4405 * @data: data associated to the transition
Daniel Veillard7646b182002-04-20 06:41:40 +00004406 *
4407 * If @to is NULL, this create first a new target state in the automata
4408 * and then adds a transition from the @from state to the target state
4409 * activated by a succession of input of value @token and whose number
4410 * is between @min and @max, moreover that transistion can only be crossed
4411 * once.
4412 *
4413 * Returns the target state or NULL in case of error
4414 */
4415xmlAutomataStatePtr
4416xmlAutomataNewOnceTrans(xmlAutomataPtr am, xmlAutomataStatePtr from,
4417 xmlAutomataStatePtr to, const xmlChar *token,
4418 int min, int max, void *data) {
4419 xmlRegAtomPtr atom;
4420 int counter;
4421
4422 if ((am == NULL) || (from == NULL) || (token == NULL))
4423 return(NULL);
4424 if (min < 1)
4425 return(NULL);
4426 if ((max < min) || (max < 1))
4427 return(NULL);
4428 atom = xmlRegNewAtom(am, XML_REGEXP_STRING);
4429 if (atom == NULL)
4430 return(NULL);
4431 atom->valuep = xmlStrdup(token);
4432 atom->data = data;
4433 atom->quant = XML_REGEXP_QUANT_ONCEONLY;
4434 if (min == 0)
4435 atom->min = 1;
4436 else
4437 atom->min = min;
4438 atom->max = max;
4439 /*
4440 * associate a counter to the transition.
4441 */
4442 counter = xmlRegGetCounter(am);
4443 am->counters[counter].min = 1;
4444 am->counters[counter].max = 1;
4445
4446 /* xmlFAGenerateTransitions(am, from, to, atom); */
4447 if (to == NULL) {
4448 to = xmlRegNewState(am);
4449 xmlRegStatePush(am, to);
4450 }
4451 xmlRegStateAddTrans(am, from, atom, to, counter, -1);
4452 xmlRegAtomPush(am, atom);
4453 am->state = to;
4454 if (to == NULL)
4455 to = am->state;
4456 if (to == NULL)
4457 return(NULL);
4458 return(to);
4459}
4460
4461/**
Daniel Veillard4255d502002-04-16 15:50:10 +00004462 * xmlAutomataNewState:
4463 * @am: an automata
4464 *
4465 * Create a new disconnected state in the automata
4466 *
4467 * Returns the new state or NULL in case of error
4468 */
4469xmlAutomataStatePtr
4470xmlAutomataNewState(xmlAutomataPtr am) {
4471 xmlAutomataStatePtr to;
4472
4473 if (am == NULL)
4474 return(NULL);
4475 to = xmlRegNewState(am);
4476 xmlRegStatePush(am, to);
4477 return(to);
4478}
4479
4480/**
Daniel Veillarda9b66d02002-12-11 14:23:49 +00004481 * xmlAutomataNewEpsilon:
Daniel Veillard4255d502002-04-16 15:50:10 +00004482 * @am: an automata
4483 * @from: the starting point of the transition
4484 * @to: the target point of the transition or NULL
4485 *
4486 * If @to is NULL, this create first a new target state in the automata
4487 * and then adds a an epsilon transition from the @from state to the
4488 * target state
4489 *
4490 * Returns the target state or NULL in case of error
4491 */
4492xmlAutomataStatePtr
4493xmlAutomataNewEpsilon(xmlAutomataPtr am, xmlAutomataStatePtr from,
4494 xmlAutomataStatePtr to) {
4495 if ((am == NULL) || (from == NULL))
4496 return(NULL);
4497 xmlFAGenerateEpsilonTransition(am, from, to);
4498 if (to == NULL)
4499 return(am->state);
4500 return(to);
4501}
4502
Daniel Veillardb509f152002-04-17 16:28:10 +00004503/**
Daniel Veillard7646b182002-04-20 06:41:40 +00004504 * xmlAutomataNewAllTrans:
4505 * @am: an automata
4506 * @from: the starting point of the transition
4507 * @to: the target point of the transition or NULL
Daniel Veillarda9b66d02002-12-11 14:23:49 +00004508 * @lax: allow to transition if not all all transitions have been activated
Daniel Veillard7646b182002-04-20 06:41:40 +00004509 *
4510 * If @to is NULL, this create first a new target state in the automata
4511 * and then adds a an ALL transition from the @from state to the
4512 * target state. That transition is an epsilon transition allowed only when
4513 * all transitions from the @from node have been activated.
4514 *
4515 * Returns the target state or NULL in case of error
4516 */
4517xmlAutomataStatePtr
4518xmlAutomataNewAllTrans(xmlAutomataPtr am, xmlAutomataStatePtr from,
Daniel Veillard441bc322002-04-20 17:38:48 +00004519 xmlAutomataStatePtr to, int lax) {
Daniel Veillard7646b182002-04-20 06:41:40 +00004520 if ((am == NULL) || (from == NULL))
4521 return(NULL);
Daniel Veillard441bc322002-04-20 17:38:48 +00004522 xmlFAGenerateAllTransition(am, from, to, lax);
Daniel Veillard7646b182002-04-20 06:41:40 +00004523 if (to == NULL)
4524 return(am->state);
4525 return(to);
4526}
4527
4528/**
Daniel Veillardb509f152002-04-17 16:28:10 +00004529 * xmlAutomataNewCounter:
4530 * @am: an automata
4531 * @min: the minimal value on the counter
4532 * @max: the maximal value on the counter
4533 *
4534 * Create a new counter
4535 *
4536 * Returns the counter number or -1 in case of error
4537 */
4538int
4539xmlAutomataNewCounter(xmlAutomataPtr am, int min, int max) {
4540 int ret;
4541
4542 if (am == NULL)
4543 return(-1);
4544
4545 ret = xmlRegGetCounter(am);
4546 if (ret < 0)
4547 return(-1);
4548 am->counters[ret].min = min;
4549 am->counters[ret].max = max;
4550 return(ret);
4551}
4552
4553/**
4554 * xmlAutomataNewCountedTrans:
4555 * @am: an automata
4556 * @from: the starting point of the transition
4557 * @to: the target point of the transition or NULL
4558 * @counter: the counter associated to that transition
4559 *
4560 * If @to is NULL, this create first a new target state in the automata
4561 * and then adds an epsilon transition from the @from state to the target state
4562 * which will increment the counter provided
4563 *
4564 * Returns the target state or NULL in case of error
4565 */
4566xmlAutomataStatePtr
4567xmlAutomataNewCountedTrans(xmlAutomataPtr am, xmlAutomataStatePtr from,
4568 xmlAutomataStatePtr to, int counter) {
4569 if ((am == NULL) || (from == NULL) || (counter < 0))
4570 return(NULL);
4571 xmlFAGenerateCountedEpsilonTransition(am, from, to, counter);
4572 if (to == NULL)
4573 return(am->state);
4574 return(to);
4575}
4576
4577/**
4578 * xmlAutomataNewCounterTrans:
4579 * @am: an automata
4580 * @from: the starting point of the transition
4581 * @to: the target point of the transition or NULL
4582 * @counter: the counter associated to that transition
4583 *
4584 * If @to is NULL, this create first a new target state in the automata
4585 * and then adds an epsilon transition from the @from state to the target state
4586 * which will be allowed only if the counter is within the right range.
4587 *
4588 * Returns the target state or NULL in case of error
4589 */
4590xmlAutomataStatePtr
4591xmlAutomataNewCounterTrans(xmlAutomataPtr am, xmlAutomataStatePtr from,
4592 xmlAutomataStatePtr to, int counter) {
4593 if ((am == NULL) || (from == NULL) || (counter < 0))
4594 return(NULL);
4595 xmlFAGenerateCountedTransition(am, from, to, counter);
4596 if (to == NULL)
4597 return(am->state);
4598 return(to);
4599}
Daniel Veillard4255d502002-04-16 15:50:10 +00004600
4601/**
4602 * xmlAutomataCompile:
4603 * @am: an automata
4604 *
4605 * Compile the automata into a Reg Exp ready for being executed.
4606 * The automata should be free after this point.
4607 *
4608 * Returns the compiled regexp or NULL in case of error
4609 */
4610xmlRegexpPtr
4611xmlAutomataCompile(xmlAutomataPtr am) {
4612 xmlRegexpPtr ret;
4613
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00004614 if ((am == NULL) || (am->error != 0)) return(NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +00004615 xmlFAEliminateEpsilonTransitions(am);
Daniel Veillard23e73572002-09-19 19:56:43 +00004616 /* xmlFAComputesDeterminism(am); */
Daniel Veillard4255d502002-04-16 15:50:10 +00004617 ret = xmlRegEpxFromParse(am);
4618
4619 return(ret);
4620}
Daniel Veillarde19fc232002-04-22 16:01:24 +00004621
4622/**
4623 * xmlAutomataIsDeterminist:
4624 * @am: an automata
4625 *
4626 * Checks if an automata is determinist.
4627 *
4628 * Returns 1 if true, 0 if not, and -1 in case of error
4629 */
4630int
4631xmlAutomataIsDeterminist(xmlAutomataPtr am) {
4632 int ret;
4633
4634 if (am == NULL)
4635 return(-1);
4636
4637 ret = xmlFAComputesDeterminism(am);
4638 return(ret);
4639}
Daniel Veillard4255d502002-04-16 15:50:10 +00004640#endif /* LIBXML_AUTOMATA_ENABLED */
4641#endif /* LIBXML_REGEXP_ENABLED */