blob: cf5f942e00930147d63b6df4bc3e89fbe623b1e5 [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
William M. Brackddf71d62004-05-06 04:17:26 +00006 * XML related specifications these include:
Daniel Veillard4255d502002-04-16 15:50:10 +00007 * - 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
Daniel Veillardcee2b3a2005-01-25 00:22:52 +000022/* #define DEBUG_ERR */
Daniel Veillardfc0b6f62005-01-09 17:48:02 +000023
Daniel Veillard4255d502002-04-16 15:50:10 +000024#include <stdio.h>
25#include <string.h>
Daniel Veillardebe48c62003-12-03 12:12:27 +000026#ifdef HAVE_LIMITS_H
27#include <limits.h>
28#endif
29
Daniel Veillard4255d502002-04-16 15:50:10 +000030#include <libxml/tree.h>
31#include <libxml/parserInternals.h>
32#include <libxml/xmlregexp.h>
33#include <libxml/xmlautomata.h>
34#include <libxml/xmlunicode.h>
35
Daniel Veillardebe48c62003-12-03 12:12:27 +000036#ifndef INT_MAX
37#define INT_MAX 123456789 /* easy to flag and big enough for our needs */
38#endif
39
Daniel Veillardc0826a72004-08-10 14:17:33 +000040/* #define DEBUG_REGEXP_GRAPH */
Daniel Veillard10752282005-08-08 13:05:13 +000041/* #define DEBUG_REGEXP_EXEC */
Daniel Veillard4255d502002-04-16 15:50:10 +000042/* #define DEBUG_PUSH */
Daniel Veillard23e73572002-09-19 19:56:43 +000043/* #define DEBUG_COMPACTION */
Daniel Veillard4255d502002-04-16 15:50:10 +000044
Daniel Veillardff46a042003-10-08 08:53:17 +000045#define ERROR(str) \
46 ctxt->error = XML_REGEXP_COMPILE_ERROR; \
47 xmlRegexpErrCompile(ctxt, str);
Daniel Veillard4255d502002-04-16 15:50:10 +000048#define NEXT ctxt->cur++
49#define CUR (*(ctxt->cur))
50#define NXT(index) (ctxt->cur[index])
51
52#define CUR_SCHAR(s, l) xmlStringCurrentChar(NULL, s, &l)
53#define NEXTL(l) ctxt->cur += l;
Daniel Veillardc0826a72004-08-10 14:17:33 +000054#define XML_REG_STRING_SEPARATOR '|'
Daniel Veillard4255d502002-04-16 15:50:10 +000055
Daniel Veillarde19fc232002-04-22 16:01:24 +000056/**
57 * TODO:
58 *
59 * macro to flag unimplemented blocks
60 */
61#define TODO \
62 xmlGenericError(xmlGenericErrorContext, \
63 "Unimplemented block at %s:%d\n", \
64 __FILE__, __LINE__);
65
Daniel Veillard4255d502002-04-16 15:50:10 +000066/************************************************************************
67 * *
68 * Datatypes and structures *
69 * *
70 ************************************************************************/
71
72typedef enum {
73 XML_REGEXP_EPSILON = 1,
74 XML_REGEXP_CHARVAL,
75 XML_REGEXP_RANGES,
76 XML_REGEXP_SUBREG,
77 XML_REGEXP_STRING,
78 XML_REGEXP_ANYCHAR, /* . */
79 XML_REGEXP_ANYSPACE, /* \s */
80 XML_REGEXP_NOTSPACE, /* \S */
81 XML_REGEXP_INITNAME, /* \l */
82 XML_REGEXP_NOTINITNAME, /* \l */
83 XML_REGEXP_NAMECHAR, /* \c */
84 XML_REGEXP_NOTNAMECHAR, /* \C */
85 XML_REGEXP_DECIMAL, /* \d */
86 XML_REGEXP_NOTDECIMAL, /* \d */
87 XML_REGEXP_REALCHAR, /* \w */
88 XML_REGEXP_NOTREALCHAR, /* \w */
89 XML_REGEXP_LETTER,
90 XML_REGEXP_LETTER_UPPERCASE,
91 XML_REGEXP_LETTER_LOWERCASE,
92 XML_REGEXP_LETTER_TITLECASE,
93 XML_REGEXP_LETTER_MODIFIER,
94 XML_REGEXP_LETTER_OTHERS,
95 XML_REGEXP_MARK,
96 XML_REGEXP_MARK_NONSPACING,
97 XML_REGEXP_MARK_SPACECOMBINING,
98 XML_REGEXP_MARK_ENCLOSING,
99 XML_REGEXP_NUMBER,
100 XML_REGEXP_NUMBER_DECIMAL,
101 XML_REGEXP_NUMBER_LETTER,
102 XML_REGEXP_NUMBER_OTHERS,
103 XML_REGEXP_PUNCT,
104 XML_REGEXP_PUNCT_CONNECTOR,
105 XML_REGEXP_PUNCT_DASH,
106 XML_REGEXP_PUNCT_OPEN,
107 XML_REGEXP_PUNCT_CLOSE,
108 XML_REGEXP_PUNCT_INITQUOTE,
109 XML_REGEXP_PUNCT_FINQUOTE,
110 XML_REGEXP_PUNCT_OTHERS,
111 XML_REGEXP_SEPAR,
112 XML_REGEXP_SEPAR_SPACE,
113 XML_REGEXP_SEPAR_LINE,
114 XML_REGEXP_SEPAR_PARA,
115 XML_REGEXP_SYMBOL,
116 XML_REGEXP_SYMBOL_MATH,
117 XML_REGEXP_SYMBOL_CURRENCY,
118 XML_REGEXP_SYMBOL_MODIFIER,
119 XML_REGEXP_SYMBOL_OTHERS,
120 XML_REGEXP_OTHER,
121 XML_REGEXP_OTHER_CONTROL,
122 XML_REGEXP_OTHER_FORMAT,
123 XML_REGEXP_OTHER_PRIVATE,
124 XML_REGEXP_OTHER_NA,
125 XML_REGEXP_BLOCK_NAME
126} xmlRegAtomType;
127
128typedef enum {
129 XML_REGEXP_QUANT_EPSILON = 1,
130 XML_REGEXP_QUANT_ONCE,
131 XML_REGEXP_QUANT_OPT,
132 XML_REGEXP_QUANT_MULT,
133 XML_REGEXP_QUANT_PLUS,
Daniel Veillard7646b182002-04-20 06:41:40 +0000134 XML_REGEXP_QUANT_ONCEONLY,
135 XML_REGEXP_QUANT_ALL,
Daniel Veillard4255d502002-04-16 15:50:10 +0000136 XML_REGEXP_QUANT_RANGE
137} xmlRegQuantType;
138
139typedef enum {
140 XML_REGEXP_START_STATE = 1,
141 XML_REGEXP_FINAL_STATE,
Daniel Veillardcc026dc2005-01-12 13:21:17 +0000142 XML_REGEXP_TRANS_STATE,
143 XML_REGEXP_SINK_STATE
Daniel Veillard4255d502002-04-16 15:50:10 +0000144} xmlRegStateType;
145
146typedef enum {
147 XML_REGEXP_MARK_NORMAL = 0,
148 XML_REGEXP_MARK_START,
149 XML_REGEXP_MARK_VISITED
150} xmlRegMarkedType;
151
152typedef struct _xmlRegRange xmlRegRange;
153typedef xmlRegRange *xmlRegRangePtr;
154
155struct _xmlRegRange {
Daniel Veillardf8b9de32003-11-24 14:27:26 +0000156 int neg; /* 0 normal, 1 not, 2 exclude */
Daniel Veillard4255d502002-04-16 15:50:10 +0000157 xmlRegAtomType type;
158 int start;
159 int end;
160 xmlChar *blockName;
161};
162
163typedef struct _xmlRegAtom xmlRegAtom;
164typedef xmlRegAtom *xmlRegAtomPtr;
165
166typedef struct _xmlAutomataState xmlRegState;
167typedef xmlRegState *xmlRegStatePtr;
168
169struct _xmlRegAtom {
170 int no;
171 xmlRegAtomType type;
172 xmlRegQuantType quant;
173 int min;
174 int max;
175
176 void *valuep;
Daniel Veillarda646cfd2002-09-17 21:50:03 +0000177 void *valuep2;
Daniel Veillard4255d502002-04-16 15:50:10 +0000178 int neg;
179 int codepoint;
180 xmlRegStatePtr start;
181 xmlRegStatePtr stop;
182 int maxRanges;
183 int nbRanges;
184 xmlRegRangePtr *ranges;
185 void *data;
186};
187
188typedef struct _xmlRegCounter xmlRegCounter;
189typedef xmlRegCounter *xmlRegCounterPtr;
190
191struct _xmlRegCounter {
192 int min;
193 int max;
194};
195
196typedef struct _xmlRegTrans xmlRegTrans;
197typedef xmlRegTrans *xmlRegTransPtr;
198
199struct _xmlRegTrans {
200 xmlRegAtomPtr atom;
201 int to;
202 int counter;
203 int count;
204};
205
206struct _xmlAutomataState {
207 xmlRegStateType type;
208 xmlRegMarkedType mark;
Daniel Veillard23e73572002-09-19 19:56:43 +0000209 xmlRegMarkedType reached;
Daniel Veillard4255d502002-04-16 15:50:10 +0000210 int no;
Daniel Veillard4255d502002-04-16 15:50:10 +0000211 int maxTrans;
212 int nbTrans;
213 xmlRegTrans *trans;
Daniel Veillarddb68b742005-07-30 13:18:24 +0000214 /* knowing states ponting to us can speed things up */
215 int maxTransTo;
216 int nbTransTo;
217 int *transTo;
Daniel Veillard4255d502002-04-16 15:50:10 +0000218};
219
220typedef struct _xmlAutomata xmlRegParserCtxt;
221typedef xmlRegParserCtxt *xmlRegParserCtxtPtr;
222
223struct _xmlAutomata {
224 xmlChar *string;
225 xmlChar *cur;
226
227 int error;
228 int neg;
229
230 xmlRegStatePtr start;
231 xmlRegStatePtr end;
232 xmlRegStatePtr state;
233
234 xmlRegAtomPtr atom;
235
236 int maxAtoms;
237 int nbAtoms;
238 xmlRegAtomPtr *atoms;
239
240 int maxStates;
241 int nbStates;
242 xmlRegStatePtr *states;
243
244 int maxCounters;
245 int nbCounters;
246 xmlRegCounter *counters;
Daniel Veillarde19fc232002-04-22 16:01:24 +0000247
248 int determinist;
Daniel Veillard6e65e152005-08-09 11:09:52 +0000249 int negs;
Daniel Veillard4255d502002-04-16 15:50:10 +0000250};
251
252struct _xmlRegexp {
253 xmlChar *string;
254 int nbStates;
255 xmlRegStatePtr *states;
256 int nbAtoms;
257 xmlRegAtomPtr *atoms;
258 int nbCounters;
259 xmlRegCounter *counters;
Daniel Veillarde19fc232002-04-22 16:01:24 +0000260 int determinist;
Daniel Veillard23e73572002-09-19 19:56:43 +0000261 /*
262 * That's the compact form for determinists automatas
263 */
264 int nbstates;
265 int *compact;
Daniel Veillard118aed72002-09-24 14:13:13 +0000266 void **transdata;
Daniel Veillard23e73572002-09-19 19:56:43 +0000267 int nbstrings;
268 xmlChar **stringMap;
Daniel Veillard4255d502002-04-16 15:50:10 +0000269};
270
271typedef struct _xmlRegExecRollback xmlRegExecRollback;
272typedef xmlRegExecRollback *xmlRegExecRollbackPtr;
273
274struct _xmlRegExecRollback {
275 xmlRegStatePtr state;/* the current state */
276 int index; /* the index in the input stack */
277 int nextbranch; /* the next transition to explore in that state */
William M. Brackddf71d62004-05-06 04:17:26 +0000278 int *counts; /* save the automata state if it has some */
Daniel Veillard4255d502002-04-16 15:50:10 +0000279};
280
281typedef struct _xmlRegInputToken xmlRegInputToken;
282typedef xmlRegInputToken *xmlRegInputTokenPtr;
283
284struct _xmlRegInputToken {
285 xmlChar *value;
286 void *data;
287};
288
289struct _xmlRegExecCtxt {
290 int status; /* execution status != 0 indicate an error */
William M. Brackddf71d62004-05-06 04:17:26 +0000291 int determinist; /* did we find an indeterministic behaviour */
Daniel Veillard4255d502002-04-16 15:50:10 +0000292 xmlRegexpPtr comp; /* the compiled regexp */
293 xmlRegExecCallbacks callback;
294 void *data;
295
296 xmlRegStatePtr state;/* the current state */
297 int transno; /* the current transition on that state */
William M. Brackddf71d62004-05-06 04:17:26 +0000298 int transcount; /* the number of chars in char counted transitions */
Daniel Veillard4255d502002-04-16 15:50:10 +0000299
300 /*
301 * A stack of rollback states
302 */
303 int maxRollbacks;
304 int nbRollbacks;
305 xmlRegExecRollback *rollbacks;
306
307 /*
308 * The state of the automata if any
309 */
310 int *counts;
311
312 /*
313 * The input stack
314 */
315 int inputStackMax;
316 int inputStackNr;
317 int index;
318 int *charStack;
319 const xmlChar *inputString; /* when operating on characters */
320 xmlRegInputTokenPtr inputStack;/* when operating on strings */
321
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +0000322 /*
323 * error handling
324 */
325 int errStateNo; /* the error state number */
326 xmlRegStatePtr errState; /* the error state */
327 xmlChar *errString; /* the string raising the error */
328 int *errCounts; /* counters at the error state */
Daniel Veillard4255d502002-04-16 15:50:10 +0000329};
330
Daniel Veillard441bc322002-04-20 17:38:48 +0000331#define REGEXP_ALL_COUNTER 0x123456
332#define REGEXP_ALL_LAX_COUNTER 0x123457
Daniel Veillard7646b182002-04-20 06:41:40 +0000333
Daniel Veillard4255d502002-04-16 15:50:10 +0000334static void xmlFAParseRegExp(xmlRegParserCtxtPtr ctxt, int top);
Daniel Veillard23e73572002-09-19 19:56:43 +0000335static void xmlRegFreeState(xmlRegStatePtr state);
336static void xmlRegFreeAtom(xmlRegAtomPtr atom);
Daniel Veillard9efc4762005-07-19 14:33:55 +0000337static int xmlRegStrEqualWildcard(const xmlChar *expStr, const xmlChar *valStr);
Daniel Veillard4255d502002-04-16 15:50:10 +0000338
339/************************************************************************
Daniel Veillardff46a042003-10-08 08:53:17 +0000340 * *
341 * Regexp memory error handler *
342 * *
343 ************************************************************************/
344/**
345 * xmlRegexpErrMemory:
William M. Brackddf71d62004-05-06 04:17:26 +0000346 * @extra: extra information
Daniel Veillardff46a042003-10-08 08:53:17 +0000347 *
348 * Handle an out of memory condition
349 */
350static void
351xmlRegexpErrMemory(xmlRegParserCtxtPtr ctxt, const char *extra)
352{
353 const char *regexp = NULL;
354 if (ctxt != NULL) {
355 regexp = (const char *) ctxt->string;
356 ctxt->error = XML_ERR_NO_MEMORY;
357 }
Daniel Veillard659e71e2003-10-10 14:10:40 +0000358 __xmlRaiseError(NULL, NULL, NULL, NULL, NULL, XML_FROM_REGEXP,
Daniel Veillardff46a042003-10-08 08:53:17 +0000359 XML_ERR_NO_MEMORY, XML_ERR_FATAL, NULL, 0, extra,
360 regexp, NULL, 0, 0,
361 "Memory allocation failed : %s\n", extra);
362}
363
364/**
365 * xmlRegexpErrCompile:
William M. Brackddf71d62004-05-06 04:17:26 +0000366 * @extra: extra information
Daniel Veillardff46a042003-10-08 08:53:17 +0000367 *
William M. Brackddf71d62004-05-06 04:17:26 +0000368 * Handle a compilation failure
Daniel Veillardff46a042003-10-08 08:53:17 +0000369 */
370static void
371xmlRegexpErrCompile(xmlRegParserCtxtPtr ctxt, const char *extra)
372{
373 const char *regexp = NULL;
374 int idx = 0;
375
376 if (ctxt != NULL) {
377 regexp = (const char *) ctxt->string;
378 idx = ctxt->cur - ctxt->string;
379 ctxt->error = XML_REGEXP_COMPILE_ERROR;
380 }
Daniel Veillard659e71e2003-10-10 14:10:40 +0000381 __xmlRaiseError(NULL, NULL, NULL, NULL, NULL, XML_FROM_REGEXP,
Daniel Veillardff46a042003-10-08 08:53:17 +0000382 XML_REGEXP_COMPILE_ERROR, XML_ERR_FATAL, NULL, 0, extra,
383 regexp, NULL, idx, 0,
384 "failed to compile: %s\n", extra);
385}
386
387/************************************************************************
Daniel Veillard4255d502002-04-16 15:50:10 +0000388 * *
389 * Allocation/Deallocation *
390 * *
391 ************************************************************************/
392
Daniel Veillard23e73572002-09-19 19:56:43 +0000393static int xmlFAComputesDeterminism(xmlRegParserCtxtPtr ctxt);
Daniel Veillard4255d502002-04-16 15:50:10 +0000394/**
395 * xmlRegEpxFromParse:
396 * @ctxt: the parser context used to build it
397 *
William M. Brackddf71d62004-05-06 04:17:26 +0000398 * Allocate a new regexp and fill it with the result from the parser
Daniel Veillard4255d502002-04-16 15:50:10 +0000399 *
400 * Returns the new regexp or NULL in case of error
401 */
402static xmlRegexpPtr
403xmlRegEpxFromParse(xmlRegParserCtxtPtr ctxt) {
404 xmlRegexpPtr ret;
405
406 ret = (xmlRegexpPtr) xmlMalloc(sizeof(xmlRegexp));
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000407 if (ret == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +0000408 xmlRegexpErrMemory(ctxt, "compiling regexp");
Daniel Veillard4255d502002-04-16 15:50:10 +0000409 return(NULL);
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000410 }
Daniel Veillard4255d502002-04-16 15:50:10 +0000411 memset(ret, 0, sizeof(xmlRegexp));
412 ret->string = ctxt->string;
Daniel Veillard4255d502002-04-16 15:50:10 +0000413 ret->nbStates = ctxt->nbStates;
Daniel Veillard4255d502002-04-16 15:50:10 +0000414 ret->states = ctxt->states;
Daniel Veillard4255d502002-04-16 15:50:10 +0000415 ret->nbAtoms = ctxt->nbAtoms;
Daniel Veillard4255d502002-04-16 15:50:10 +0000416 ret->atoms = ctxt->atoms;
Daniel Veillard4255d502002-04-16 15:50:10 +0000417 ret->nbCounters = ctxt->nbCounters;
Daniel Veillard4255d502002-04-16 15:50:10 +0000418 ret->counters = ctxt->counters;
Daniel Veillarde19fc232002-04-22 16:01:24 +0000419 ret->determinist = ctxt->determinist;
Daniel Veillard23e73572002-09-19 19:56:43 +0000420
421 if ((ret->determinist != 0) &&
422 (ret->nbCounters == 0) &&
Daniel Veillard6e65e152005-08-09 11:09:52 +0000423 (ctxt->negs == 0) &&
Daniel Veillard118aed72002-09-24 14:13:13 +0000424 (ret->atoms != NULL) &&
Daniel Veillard23e73572002-09-19 19:56:43 +0000425 (ret->atoms[0] != NULL) &&
426 (ret->atoms[0]->type == XML_REGEXP_STRING)) {
427 int i, j, nbstates = 0, nbatoms = 0;
428 int *stateRemap;
429 int *stringRemap;
430 int *transitions;
Daniel Veillard118aed72002-09-24 14:13:13 +0000431 void **transdata;
Daniel Veillard23e73572002-09-19 19:56:43 +0000432 xmlChar **stringMap;
433 xmlChar *value;
434
435 /*
436 * Switch to a compact representation
437 * 1/ counting the effective number of states left
William M. Brackddf71d62004-05-06 04:17:26 +0000438 * 2/ counting the unique number of atoms, and check that
Daniel Veillard23e73572002-09-19 19:56:43 +0000439 * they are all of the string type
440 * 3/ build a table state x atom for the transitions
441 */
442
443 stateRemap = xmlMalloc(ret->nbStates * sizeof(int));
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000444 if (stateRemap == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +0000445 xmlRegexpErrMemory(ctxt, "compiling regexp");
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000446 xmlFree(ret);
447 return(NULL);
448 }
Daniel Veillard23e73572002-09-19 19:56:43 +0000449 for (i = 0;i < ret->nbStates;i++) {
450 if (ret->states[i] != NULL) {
451 stateRemap[i] = nbstates;
452 nbstates++;
453 } else {
454 stateRemap[i] = -1;
455 }
456 }
457#ifdef DEBUG_COMPACTION
458 printf("Final: %d states\n", nbstates);
459#endif
460 stringMap = xmlMalloc(ret->nbAtoms * sizeof(char *));
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000461 if (stringMap == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +0000462 xmlRegexpErrMemory(ctxt, "compiling regexp");
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000463 xmlFree(stateRemap);
464 xmlFree(ret);
465 return(NULL);
466 }
Daniel Veillard23e73572002-09-19 19:56:43 +0000467 stringRemap = xmlMalloc(ret->nbAtoms * sizeof(int));
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000468 if (stringRemap == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +0000469 xmlRegexpErrMemory(ctxt, "compiling regexp");
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000470 xmlFree(stringMap);
471 xmlFree(stateRemap);
472 xmlFree(ret);
473 return(NULL);
474 }
Daniel Veillard23e73572002-09-19 19:56:43 +0000475 for (i = 0;i < ret->nbAtoms;i++) {
476 if ((ret->atoms[i]->type == XML_REGEXP_STRING) &&
477 (ret->atoms[i]->quant == XML_REGEXP_QUANT_ONCE)) {
478 value = ret->atoms[i]->valuep;
479 for (j = 0;j < nbatoms;j++) {
480 if (xmlStrEqual(stringMap[j], value)) {
481 stringRemap[i] = j;
482 break;
483 }
484 }
485 if (j >= nbatoms) {
486 stringRemap[i] = nbatoms;
487 stringMap[nbatoms] = xmlStrdup(value);
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000488 if (stringMap[nbatoms] == NULL) {
489 for (i = 0;i < nbatoms;i++)
490 xmlFree(stringMap[i]);
491 xmlFree(stringRemap);
492 xmlFree(stringMap);
493 xmlFree(stateRemap);
494 xmlFree(ret);
495 return(NULL);
496 }
Daniel Veillard23e73572002-09-19 19:56:43 +0000497 nbatoms++;
498 }
499 } else {
500 xmlFree(stateRemap);
501 xmlFree(stringRemap);
502 for (i = 0;i < nbatoms;i++)
503 xmlFree(stringMap[i]);
504 xmlFree(stringMap);
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000505 xmlFree(ret);
506 return(NULL);
Daniel Veillard23e73572002-09-19 19:56:43 +0000507 }
508 }
509#ifdef DEBUG_COMPACTION
510 printf("Final: %d atoms\n", nbatoms);
511#endif
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000512 transitions = (int *) xmlMalloc((nbstates + 1) *
513 (nbatoms + 1) * sizeof(int));
514 if (transitions == NULL) {
515 xmlFree(stateRemap);
516 xmlFree(stringRemap);
517 xmlFree(stringMap);
518 xmlFree(ret);
519 return(NULL);
520 }
521 memset(transitions, 0, (nbstates + 1) * (nbatoms + 1) * sizeof(int));
Daniel Veillard23e73572002-09-19 19:56:43 +0000522
523 /*
524 * Allocate the transition table. The first entry for each
William M. Brackddf71d62004-05-06 04:17:26 +0000525 * state corresponds to the state type.
Daniel Veillard23e73572002-09-19 19:56:43 +0000526 */
Daniel Veillard118aed72002-09-24 14:13:13 +0000527 transdata = NULL;
Daniel Veillard23e73572002-09-19 19:56:43 +0000528
529 for (i = 0;i < ret->nbStates;i++) {
530 int stateno, atomno, targetno, prev;
531 xmlRegStatePtr state;
532 xmlRegTransPtr trans;
533
534 stateno = stateRemap[i];
535 if (stateno == -1)
536 continue;
537 state = ret->states[i];
538
539 transitions[stateno * (nbatoms + 1)] = state->type;
540
541 for (j = 0;j < state->nbTrans;j++) {
542 trans = &(state->trans[j]);
543 if ((trans->to == -1) || (trans->atom == NULL))
544 continue;
545 atomno = stringRemap[trans->atom->no];
Daniel Veillard118aed72002-09-24 14:13:13 +0000546 if ((trans->atom->data != NULL) && (transdata == NULL)) {
547 transdata = (void **) xmlMalloc(nbstates * nbatoms *
548 sizeof(void *));
549 if (transdata != NULL)
550 memset(transdata, 0,
551 nbstates * nbatoms * sizeof(void *));
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000552 else {
Daniel Veillardff46a042003-10-08 08:53:17 +0000553 xmlRegexpErrMemory(ctxt, "compiling regexp");
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000554 break;
555 }
Daniel Veillard118aed72002-09-24 14:13:13 +0000556 }
Daniel Veillard23e73572002-09-19 19:56:43 +0000557 targetno = stateRemap[trans->to];
558 /*
William M. Brackddf71d62004-05-06 04:17:26 +0000559 * if the same atom can generate transitions to 2 different
Daniel Veillard23e73572002-09-19 19:56:43 +0000560 * states then it means the automata is not determinist and
561 * the compact form can't be used !
562 */
563 prev = transitions[stateno * (nbatoms + 1) + atomno + 1];
564 if (prev != 0) {
565 if (prev != targetno + 1) {
Daniel Veillard23e73572002-09-19 19:56:43 +0000566 ret->determinist = 0;
567#ifdef DEBUG_COMPACTION
568 printf("Indet: state %d trans %d, atom %d to %d : %d to %d\n",
569 i, j, trans->atom->no, trans->to, atomno, targetno);
570 printf(" previous to is %d\n", prev);
571#endif
572 ret->determinist = 0;
Daniel Veillard118aed72002-09-24 14:13:13 +0000573 if (transdata != NULL)
574 xmlFree(transdata);
Daniel Veillard23e73572002-09-19 19:56:43 +0000575 xmlFree(transitions);
576 xmlFree(stateRemap);
577 xmlFree(stringRemap);
578 for (i = 0;i < nbatoms;i++)
579 xmlFree(stringMap[i]);
580 xmlFree(stringMap);
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000581 goto not_determ;
Daniel Veillard23e73572002-09-19 19:56:43 +0000582 }
583 } else {
584#if 0
585 printf("State %d trans %d: atom %d to %d : %d to %d\n",
586 i, j, trans->atom->no, trans->to, atomno, targetno);
587#endif
588 transitions[stateno * (nbatoms + 1) + atomno + 1] =
Daniel Veillard118aed72002-09-24 14:13:13 +0000589 targetno + 1; /* to avoid 0 */
590 if (transdata != NULL)
591 transdata[stateno * nbatoms + atomno] =
592 trans->atom->data;
Daniel Veillard23e73572002-09-19 19:56:43 +0000593 }
594 }
595 }
596 ret->determinist = 1;
597#ifdef DEBUG_COMPACTION
598 /*
599 * Debug
600 */
601 for (i = 0;i < nbstates;i++) {
602 for (j = 0;j < nbatoms + 1;j++) {
603 printf("%02d ", transitions[i * (nbatoms + 1) + j]);
604 }
605 printf("\n");
606 }
607 printf("\n");
608#endif
609 /*
610 * Cleanup of the old data
611 */
612 if (ret->states != NULL) {
613 for (i = 0;i < ret->nbStates;i++)
614 xmlRegFreeState(ret->states[i]);
615 xmlFree(ret->states);
616 }
617 ret->states = NULL;
618 ret->nbStates = 0;
619 if (ret->atoms != NULL) {
620 for (i = 0;i < ret->nbAtoms;i++)
621 xmlRegFreeAtom(ret->atoms[i]);
622 xmlFree(ret->atoms);
623 }
624 ret->atoms = NULL;
625 ret->nbAtoms = 0;
626
627 ret->compact = transitions;
Daniel Veillard118aed72002-09-24 14:13:13 +0000628 ret->transdata = transdata;
Daniel Veillard23e73572002-09-19 19:56:43 +0000629 ret->stringMap = stringMap;
630 ret->nbstrings = nbatoms;
631 ret->nbstates = nbstates;
632 xmlFree(stateRemap);
633 xmlFree(stringRemap);
634 }
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000635not_determ:
636 ctxt->string = NULL;
637 ctxt->nbStates = 0;
638 ctxt->states = NULL;
639 ctxt->nbAtoms = 0;
640 ctxt->atoms = NULL;
641 ctxt->nbCounters = 0;
642 ctxt->counters = NULL;
Daniel Veillard4255d502002-04-16 15:50:10 +0000643 return(ret);
644}
645
646/**
647 * xmlRegNewParserCtxt:
648 * @string: the string to parse
649 *
650 * Allocate a new regexp parser context
651 *
652 * Returns the new context or NULL in case of error
653 */
654static xmlRegParserCtxtPtr
655xmlRegNewParserCtxt(const xmlChar *string) {
656 xmlRegParserCtxtPtr ret;
657
658 ret = (xmlRegParserCtxtPtr) xmlMalloc(sizeof(xmlRegParserCtxt));
659 if (ret == NULL)
660 return(NULL);
661 memset(ret, 0, sizeof(xmlRegParserCtxt));
662 if (string != NULL)
663 ret->string = xmlStrdup(string);
664 ret->cur = ret->string;
665 ret->neg = 0;
Daniel Veillard6e65e152005-08-09 11:09:52 +0000666 ret->negs = 0;
Daniel Veillard4255d502002-04-16 15:50:10 +0000667 ret->error = 0;
Daniel Veillarde19fc232002-04-22 16:01:24 +0000668 ret->determinist = -1;
Daniel Veillard4255d502002-04-16 15:50:10 +0000669 return(ret);
670}
671
672/**
673 * xmlRegNewRange:
674 * @ctxt: the regexp parser context
675 * @neg: is that negative
676 * @type: the type of range
677 * @start: the start codepoint
678 * @end: the end codepoint
679 *
680 * Allocate a new regexp range
681 *
682 * Returns the new range or NULL in case of error
683 */
684static xmlRegRangePtr
685xmlRegNewRange(xmlRegParserCtxtPtr ctxt,
686 int neg, xmlRegAtomType type, int start, int end) {
687 xmlRegRangePtr ret;
688
689 ret = (xmlRegRangePtr) xmlMalloc(sizeof(xmlRegRange));
690 if (ret == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +0000691 xmlRegexpErrMemory(ctxt, "allocating range");
Daniel Veillard4255d502002-04-16 15:50:10 +0000692 return(NULL);
693 }
694 ret->neg = neg;
695 ret->type = type;
696 ret->start = start;
697 ret->end = end;
698 return(ret);
699}
700
701/**
702 * xmlRegFreeRange:
703 * @range: the regexp range
704 *
705 * Free a regexp range
706 */
707static void
708xmlRegFreeRange(xmlRegRangePtr range) {
709 if (range == NULL)
710 return;
711
712 if (range->blockName != NULL)
713 xmlFree(range->blockName);
714 xmlFree(range);
715}
716
717/**
718 * xmlRegNewAtom:
719 * @ctxt: the regexp parser context
720 * @type: the type of atom
721 *
722 * Allocate a new regexp range
723 *
724 * Returns the new atom or NULL in case of error
725 */
726static xmlRegAtomPtr
727xmlRegNewAtom(xmlRegParserCtxtPtr ctxt, xmlRegAtomType type) {
728 xmlRegAtomPtr ret;
729
730 ret = (xmlRegAtomPtr) xmlMalloc(sizeof(xmlRegAtom));
731 if (ret == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +0000732 xmlRegexpErrMemory(ctxt, "allocating atom");
Daniel Veillard4255d502002-04-16 15:50:10 +0000733 return(NULL);
734 }
735 memset(ret, 0, sizeof(xmlRegAtom));
736 ret->type = type;
737 ret->quant = XML_REGEXP_QUANT_ONCE;
738 ret->min = 0;
739 ret->max = 0;
740 return(ret);
741}
742
743/**
744 * xmlRegFreeAtom:
745 * @atom: the regexp atom
746 *
747 * Free a regexp atom
748 */
749static void
750xmlRegFreeAtom(xmlRegAtomPtr atom) {
751 int i;
752
753 if (atom == NULL)
754 return;
755
756 for (i = 0;i < atom->nbRanges;i++)
757 xmlRegFreeRange(atom->ranges[i]);
758 if (atom->ranges != NULL)
759 xmlFree(atom->ranges);
Daniel Veillardde0e4982005-07-03 14:35:44 +0000760 if ((atom->type == XML_REGEXP_STRING) && (atom->valuep != NULL))
761 xmlFree(atom->valuep);
Daniel Veillard77005e62005-07-19 16:26:18 +0000762 if ((atom->type == XML_REGEXP_STRING) && (atom->valuep2 != NULL))
763 xmlFree(atom->valuep2);
Daniel Veillardde0e4982005-07-03 14:35:44 +0000764 if ((atom->type == XML_REGEXP_BLOCK_NAME) && (atom->valuep != NULL))
Daniel Veillard4255d502002-04-16 15:50:10 +0000765 xmlFree(atom->valuep);
766 xmlFree(atom);
767}
768
769static xmlRegStatePtr
770xmlRegNewState(xmlRegParserCtxtPtr ctxt) {
771 xmlRegStatePtr ret;
772
773 ret = (xmlRegStatePtr) xmlMalloc(sizeof(xmlRegState));
774 if (ret == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +0000775 xmlRegexpErrMemory(ctxt, "allocating state");
Daniel Veillard4255d502002-04-16 15:50:10 +0000776 return(NULL);
777 }
778 memset(ret, 0, sizeof(xmlRegState));
779 ret->type = XML_REGEXP_TRANS_STATE;
780 ret->mark = XML_REGEXP_MARK_NORMAL;
781 return(ret);
782}
783
784/**
785 * xmlRegFreeState:
786 * @state: the regexp state
787 *
788 * Free a regexp state
789 */
790static void
791xmlRegFreeState(xmlRegStatePtr state) {
792 if (state == NULL)
793 return;
794
795 if (state->trans != NULL)
796 xmlFree(state->trans);
Daniel Veillarddb68b742005-07-30 13:18:24 +0000797 if (state->transTo != NULL)
798 xmlFree(state->transTo);
Daniel Veillard4255d502002-04-16 15:50:10 +0000799 xmlFree(state);
800}
801
802/**
803 * xmlRegFreeParserCtxt:
804 * @ctxt: the regexp parser context
805 *
806 * Free a regexp parser context
807 */
808static void
809xmlRegFreeParserCtxt(xmlRegParserCtxtPtr ctxt) {
810 int i;
811 if (ctxt == NULL)
812 return;
813
814 if (ctxt->string != NULL)
815 xmlFree(ctxt->string);
816 if (ctxt->states != NULL) {
817 for (i = 0;i < ctxt->nbStates;i++)
818 xmlRegFreeState(ctxt->states[i]);
819 xmlFree(ctxt->states);
820 }
821 if (ctxt->atoms != NULL) {
822 for (i = 0;i < ctxt->nbAtoms;i++)
823 xmlRegFreeAtom(ctxt->atoms[i]);
824 xmlFree(ctxt->atoms);
825 }
826 if (ctxt->counters != NULL)
827 xmlFree(ctxt->counters);
828 xmlFree(ctxt);
829}
830
831/************************************************************************
832 * *
833 * Display of Data structures *
834 * *
835 ************************************************************************/
836
837static void
838xmlRegPrintAtomType(FILE *output, xmlRegAtomType type) {
839 switch (type) {
840 case XML_REGEXP_EPSILON:
841 fprintf(output, "epsilon "); break;
842 case XML_REGEXP_CHARVAL:
843 fprintf(output, "charval "); break;
844 case XML_REGEXP_RANGES:
845 fprintf(output, "ranges "); break;
846 case XML_REGEXP_SUBREG:
847 fprintf(output, "subexpr "); break;
848 case XML_REGEXP_STRING:
849 fprintf(output, "string "); break;
850 case XML_REGEXP_ANYCHAR:
851 fprintf(output, "anychar "); break;
852 case XML_REGEXP_ANYSPACE:
853 fprintf(output, "anyspace "); break;
854 case XML_REGEXP_NOTSPACE:
855 fprintf(output, "notspace "); break;
856 case XML_REGEXP_INITNAME:
857 fprintf(output, "initname "); break;
858 case XML_REGEXP_NOTINITNAME:
859 fprintf(output, "notinitname "); break;
860 case XML_REGEXP_NAMECHAR:
861 fprintf(output, "namechar "); break;
862 case XML_REGEXP_NOTNAMECHAR:
863 fprintf(output, "notnamechar "); break;
864 case XML_REGEXP_DECIMAL:
865 fprintf(output, "decimal "); break;
866 case XML_REGEXP_NOTDECIMAL:
867 fprintf(output, "notdecimal "); break;
868 case XML_REGEXP_REALCHAR:
869 fprintf(output, "realchar "); break;
870 case XML_REGEXP_NOTREALCHAR:
871 fprintf(output, "notrealchar "); break;
872 case XML_REGEXP_LETTER:
873 fprintf(output, "LETTER "); break;
874 case XML_REGEXP_LETTER_UPPERCASE:
875 fprintf(output, "LETTER_UPPERCASE "); break;
876 case XML_REGEXP_LETTER_LOWERCASE:
877 fprintf(output, "LETTER_LOWERCASE "); break;
878 case XML_REGEXP_LETTER_TITLECASE:
879 fprintf(output, "LETTER_TITLECASE "); break;
880 case XML_REGEXP_LETTER_MODIFIER:
881 fprintf(output, "LETTER_MODIFIER "); break;
882 case XML_REGEXP_LETTER_OTHERS:
883 fprintf(output, "LETTER_OTHERS "); break;
884 case XML_REGEXP_MARK:
885 fprintf(output, "MARK "); break;
886 case XML_REGEXP_MARK_NONSPACING:
887 fprintf(output, "MARK_NONSPACING "); break;
888 case XML_REGEXP_MARK_SPACECOMBINING:
889 fprintf(output, "MARK_SPACECOMBINING "); break;
890 case XML_REGEXP_MARK_ENCLOSING:
891 fprintf(output, "MARK_ENCLOSING "); break;
892 case XML_REGEXP_NUMBER:
893 fprintf(output, "NUMBER "); break;
894 case XML_REGEXP_NUMBER_DECIMAL:
895 fprintf(output, "NUMBER_DECIMAL "); break;
896 case XML_REGEXP_NUMBER_LETTER:
897 fprintf(output, "NUMBER_LETTER "); break;
898 case XML_REGEXP_NUMBER_OTHERS:
899 fprintf(output, "NUMBER_OTHERS "); break;
900 case XML_REGEXP_PUNCT:
901 fprintf(output, "PUNCT "); break;
902 case XML_REGEXP_PUNCT_CONNECTOR:
903 fprintf(output, "PUNCT_CONNECTOR "); break;
904 case XML_REGEXP_PUNCT_DASH:
905 fprintf(output, "PUNCT_DASH "); break;
906 case XML_REGEXP_PUNCT_OPEN:
907 fprintf(output, "PUNCT_OPEN "); break;
908 case XML_REGEXP_PUNCT_CLOSE:
909 fprintf(output, "PUNCT_CLOSE "); break;
910 case XML_REGEXP_PUNCT_INITQUOTE:
911 fprintf(output, "PUNCT_INITQUOTE "); break;
912 case XML_REGEXP_PUNCT_FINQUOTE:
913 fprintf(output, "PUNCT_FINQUOTE "); break;
914 case XML_REGEXP_PUNCT_OTHERS:
915 fprintf(output, "PUNCT_OTHERS "); break;
916 case XML_REGEXP_SEPAR:
917 fprintf(output, "SEPAR "); break;
918 case XML_REGEXP_SEPAR_SPACE:
919 fprintf(output, "SEPAR_SPACE "); break;
920 case XML_REGEXP_SEPAR_LINE:
921 fprintf(output, "SEPAR_LINE "); break;
922 case XML_REGEXP_SEPAR_PARA:
923 fprintf(output, "SEPAR_PARA "); break;
924 case XML_REGEXP_SYMBOL:
925 fprintf(output, "SYMBOL "); break;
926 case XML_REGEXP_SYMBOL_MATH:
927 fprintf(output, "SYMBOL_MATH "); break;
928 case XML_REGEXP_SYMBOL_CURRENCY:
929 fprintf(output, "SYMBOL_CURRENCY "); break;
930 case XML_REGEXP_SYMBOL_MODIFIER:
931 fprintf(output, "SYMBOL_MODIFIER "); break;
932 case XML_REGEXP_SYMBOL_OTHERS:
933 fprintf(output, "SYMBOL_OTHERS "); break;
934 case XML_REGEXP_OTHER:
935 fprintf(output, "OTHER "); break;
936 case XML_REGEXP_OTHER_CONTROL:
937 fprintf(output, "OTHER_CONTROL "); break;
938 case XML_REGEXP_OTHER_FORMAT:
939 fprintf(output, "OTHER_FORMAT "); break;
940 case XML_REGEXP_OTHER_PRIVATE:
941 fprintf(output, "OTHER_PRIVATE "); break;
942 case XML_REGEXP_OTHER_NA:
943 fprintf(output, "OTHER_NA "); break;
944 case XML_REGEXP_BLOCK_NAME:
945 fprintf(output, "BLOCK "); break;
946 }
947}
948
949static void
950xmlRegPrintQuantType(FILE *output, xmlRegQuantType type) {
951 switch (type) {
952 case XML_REGEXP_QUANT_EPSILON:
953 fprintf(output, "epsilon "); break;
954 case XML_REGEXP_QUANT_ONCE:
955 fprintf(output, "once "); break;
956 case XML_REGEXP_QUANT_OPT:
957 fprintf(output, "? "); break;
958 case XML_REGEXP_QUANT_MULT:
959 fprintf(output, "* "); break;
960 case XML_REGEXP_QUANT_PLUS:
961 fprintf(output, "+ "); break;
962 case XML_REGEXP_QUANT_RANGE:
963 fprintf(output, "range "); break;
Daniel Veillard7646b182002-04-20 06:41:40 +0000964 case XML_REGEXP_QUANT_ONCEONLY:
965 fprintf(output, "onceonly "); break;
966 case XML_REGEXP_QUANT_ALL:
967 fprintf(output, "all "); break;
Daniel Veillard4255d502002-04-16 15:50:10 +0000968 }
969}
970static void
971xmlRegPrintRange(FILE *output, xmlRegRangePtr range) {
972 fprintf(output, " range: ");
973 if (range->neg)
974 fprintf(output, "negative ");
975 xmlRegPrintAtomType(output, range->type);
976 fprintf(output, "%c - %c\n", range->start, range->end);
977}
978
979static void
980xmlRegPrintAtom(FILE *output, xmlRegAtomPtr atom) {
981 fprintf(output, " atom: ");
982 if (atom == NULL) {
983 fprintf(output, "NULL\n");
984 return;
985 }
Daniel Veillard9efc4762005-07-19 14:33:55 +0000986 if (atom->neg)
987 fprintf(output, "not ");
Daniel Veillard4255d502002-04-16 15:50:10 +0000988 xmlRegPrintAtomType(output, atom->type);
989 xmlRegPrintQuantType(output, atom->quant);
990 if (atom->quant == XML_REGEXP_QUANT_RANGE)
991 fprintf(output, "%d-%d ", atom->min, atom->max);
992 if (atom->type == XML_REGEXP_STRING)
993 fprintf(output, "'%s' ", (char *) atom->valuep);
994 if (atom->type == XML_REGEXP_CHARVAL)
995 fprintf(output, "char %c\n", atom->codepoint);
996 else if (atom->type == XML_REGEXP_RANGES) {
997 int i;
998 fprintf(output, "%d entries\n", atom->nbRanges);
999 for (i = 0; i < atom->nbRanges;i++)
1000 xmlRegPrintRange(output, atom->ranges[i]);
1001 } else if (atom->type == XML_REGEXP_SUBREG) {
1002 fprintf(output, "start %d end %d\n", atom->start->no, atom->stop->no);
1003 } else {
1004 fprintf(output, "\n");
1005 }
1006}
1007
1008static void
1009xmlRegPrintTrans(FILE *output, xmlRegTransPtr trans) {
1010 fprintf(output, " trans: ");
1011 if (trans == NULL) {
1012 fprintf(output, "NULL\n");
1013 return;
1014 }
1015 if (trans->to < 0) {
1016 fprintf(output, "removed\n");
1017 return;
1018 }
1019 if (trans->counter >= 0) {
1020 fprintf(output, "counted %d, ", trans->counter);
1021 }
Daniel Veillard8a001f62002-04-20 07:24:11 +00001022 if (trans->count == REGEXP_ALL_COUNTER) {
1023 fprintf(output, "all transition, ");
1024 } else if (trans->count >= 0) {
Daniel Veillard4255d502002-04-16 15:50:10 +00001025 fprintf(output, "count based %d, ", trans->count);
1026 }
1027 if (trans->atom == NULL) {
1028 fprintf(output, "epsilon to %d\n", trans->to);
1029 return;
1030 }
1031 if (trans->atom->type == XML_REGEXP_CHARVAL)
1032 fprintf(output, "char %c ", trans->atom->codepoint);
1033 fprintf(output, "atom %d, to %d\n", trans->atom->no, trans->to);
1034}
1035
1036static void
1037xmlRegPrintState(FILE *output, xmlRegStatePtr state) {
1038 int i;
1039
1040 fprintf(output, " state: ");
1041 if (state == NULL) {
1042 fprintf(output, "NULL\n");
1043 return;
1044 }
1045 if (state->type == XML_REGEXP_START_STATE)
1046 fprintf(output, "START ");
1047 if (state->type == XML_REGEXP_FINAL_STATE)
1048 fprintf(output, "FINAL ");
1049
1050 fprintf(output, "%d, %d transitions:\n", state->no, state->nbTrans);
1051 for (i = 0;i < state->nbTrans; i++) {
1052 xmlRegPrintTrans(output, &(state->trans[i]));
1053 }
1054}
1055
Daniel Veillard23e73572002-09-19 19:56:43 +00001056#ifdef DEBUG_REGEXP_GRAPH
Daniel Veillard4255d502002-04-16 15:50:10 +00001057static void
1058xmlRegPrintCtxt(FILE *output, xmlRegParserCtxtPtr ctxt) {
1059 int i;
1060
1061 fprintf(output, " ctxt: ");
1062 if (ctxt == NULL) {
1063 fprintf(output, "NULL\n");
1064 return;
1065 }
1066 fprintf(output, "'%s' ", ctxt->string);
1067 if (ctxt->error)
1068 fprintf(output, "error ");
1069 if (ctxt->neg)
1070 fprintf(output, "neg ");
1071 fprintf(output, "\n");
1072 fprintf(output, "%d atoms:\n", ctxt->nbAtoms);
1073 for (i = 0;i < ctxt->nbAtoms; i++) {
1074 fprintf(output, " %02d ", i);
1075 xmlRegPrintAtom(output, ctxt->atoms[i]);
1076 }
1077 if (ctxt->atom != NULL) {
1078 fprintf(output, "current atom:\n");
1079 xmlRegPrintAtom(output, ctxt->atom);
1080 }
1081 fprintf(output, "%d states:", ctxt->nbStates);
1082 if (ctxt->start != NULL)
1083 fprintf(output, " start: %d", ctxt->start->no);
1084 if (ctxt->end != NULL)
1085 fprintf(output, " end: %d", ctxt->end->no);
1086 fprintf(output, "\n");
1087 for (i = 0;i < ctxt->nbStates; i++) {
1088 xmlRegPrintState(output, ctxt->states[i]);
1089 }
1090 fprintf(output, "%d counters:\n", ctxt->nbCounters);
1091 for (i = 0;i < ctxt->nbCounters; i++) {
1092 fprintf(output, " %d: min %d max %d\n", i, ctxt->counters[i].min,
1093 ctxt->counters[i].max);
1094 }
1095}
Daniel Veillard23e73572002-09-19 19:56:43 +00001096#endif
Daniel Veillard4255d502002-04-16 15:50:10 +00001097
1098/************************************************************************
1099 * *
1100 * Finite Automata structures manipulations *
1101 * *
1102 ************************************************************************/
1103
1104static void
1105xmlRegAtomAddRange(xmlRegParserCtxtPtr ctxt, xmlRegAtomPtr atom,
1106 int neg, xmlRegAtomType type, int start, int end,
1107 xmlChar *blockName) {
1108 xmlRegRangePtr range;
1109
1110 if (atom == NULL) {
1111 ERROR("add range: atom is NULL");
1112 return;
1113 }
1114 if (atom->type != XML_REGEXP_RANGES) {
1115 ERROR("add range: atom is not ranges");
1116 return;
1117 }
1118 if (atom->maxRanges == 0) {
1119 atom->maxRanges = 4;
1120 atom->ranges = (xmlRegRangePtr *) xmlMalloc(atom->maxRanges *
1121 sizeof(xmlRegRangePtr));
1122 if (atom->ranges == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00001123 xmlRegexpErrMemory(ctxt, "adding ranges");
Daniel Veillard4255d502002-04-16 15:50:10 +00001124 atom->maxRanges = 0;
1125 return;
1126 }
1127 } else if (atom->nbRanges >= atom->maxRanges) {
1128 xmlRegRangePtr *tmp;
1129 atom->maxRanges *= 2;
1130 tmp = (xmlRegRangePtr *) xmlRealloc(atom->ranges, atom->maxRanges *
1131 sizeof(xmlRegRangePtr));
1132 if (tmp == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00001133 xmlRegexpErrMemory(ctxt, "adding ranges");
Daniel Veillard4255d502002-04-16 15:50:10 +00001134 atom->maxRanges /= 2;
1135 return;
1136 }
1137 atom->ranges = tmp;
1138 }
1139 range = xmlRegNewRange(ctxt, neg, type, start, end);
1140 if (range == NULL)
1141 return;
1142 range->blockName = blockName;
1143 atom->ranges[atom->nbRanges++] = range;
1144
1145}
1146
1147static int
1148xmlRegGetCounter(xmlRegParserCtxtPtr ctxt) {
1149 if (ctxt->maxCounters == 0) {
1150 ctxt->maxCounters = 4;
1151 ctxt->counters = (xmlRegCounter *) xmlMalloc(ctxt->maxCounters *
1152 sizeof(xmlRegCounter));
1153 if (ctxt->counters == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00001154 xmlRegexpErrMemory(ctxt, "allocating counter");
Daniel Veillard4255d502002-04-16 15:50:10 +00001155 ctxt->maxCounters = 0;
1156 return(-1);
1157 }
1158 } else if (ctxt->nbCounters >= ctxt->maxCounters) {
1159 xmlRegCounter *tmp;
1160 ctxt->maxCounters *= 2;
1161 tmp = (xmlRegCounter *) xmlRealloc(ctxt->counters, ctxt->maxCounters *
1162 sizeof(xmlRegCounter));
1163 if (tmp == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00001164 xmlRegexpErrMemory(ctxt, "allocating counter");
Daniel Veillard4255d502002-04-16 15:50:10 +00001165 ctxt->maxCounters /= 2;
1166 return(-1);
1167 }
1168 ctxt->counters = tmp;
1169 }
1170 ctxt->counters[ctxt->nbCounters].min = -1;
1171 ctxt->counters[ctxt->nbCounters].max = -1;
1172 return(ctxt->nbCounters++);
1173}
1174
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001175static int
Daniel Veillard4255d502002-04-16 15:50:10 +00001176xmlRegAtomPush(xmlRegParserCtxtPtr ctxt, xmlRegAtomPtr atom) {
1177 if (atom == NULL) {
1178 ERROR("atom push: atom is NULL");
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001179 return(-1);
Daniel Veillard4255d502002-04-16 15:50:10 +00001180 }
1181 if (ctxt->maxAtoms == 0) {
1182 ctxt->maxAtoms = 4;
1183 ctxt->atoms = (xmlRegAtomPtr *) xmlMalloc(ctxt->maxAtoms *
1184 sizeof(xmlRegAtomPtr));
1185 if (ctxt->atoms == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00001186 xmlRegexpErrMemory(ctxt, "pushing atom");
Daniel Veillard4255d502002-04-16 15:50:10 +00001187 ctxt->maxAtoms = 0;
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001188 return(-1);
Daniel Veillard4255d502002-04-16 15:50:10 +00001189 }
1190 } else if (ctxt->nbAtoms >= ctxt->maxAtoms) {
1191 xmlRegAtomPtr *tmp;
1192 ctxt->maxAtoms *= 2;
1193 tmp = (xmlRegAtomPtr *) xmlRealloc(ctxt->atoms, ctxt->maxAtoms *
1194 sizeof(xmlRegAtomPtr));
1195 if (tmp == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00001196 xmlRegexpErrMemory(ctxt, "allocating counter");
Daniel Veillard4255d502002-04-16 15:50:10 +00001197 ctxt->maxAtoms /= 2;
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001198 return(-1);
Daniel Veillard4255d502002-04-16 15:50:10 +00001199 }
1200 ctxt->atoms = tmp;
1201 }
1202 atom->no = ctxt->nbAtoms;
1203 ctxt->atoms[ctxt->nbAtoms++] = atom;
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001204 return(0);
Daniel Veillard4255d502002-04-16 15:50:10 +00001205}
1206
1207static void
Daniel Veillarddb68b742005-07-30 13:18:24 +00001208xmlRegStateAddTransTo(xmlRegParserCtxtPtr ctxt, xmlRegStatePtr target,
1209 int from) {
1210 if (target->maxTransTo == 0) {
1211 target->maxTransTo = 8;
1212 target->transTo = (int *) xmlMalloc(target->maxTransTo *
1213 sizeof(int));
1214 if (target->transTo == NULL) {
1215 xmlRegexpErrMemory(ctxt, "adding transition");
1216 target->maxTransTo = 0;
1217 return;
1218 }
1219 } else if (target->nbTransTo >= target->maxTransTo) {
1220 int *tmp;
1221 target->maxTransTo *= 2;
1222 tmp = (int *) xmlRealloc(target->transTo, target->maxTransTo *
1223 sizeof(int));
1224 if (tmp == NULL) {
1225 xmlRegexpErrMemory(ctxt, "adding transition");
1226 target->maxTransTo /= 2;
1227 return;
1228 }
1229 target->transTo = tmp;
1230 }
1231 target->transTo[target->nbTransTo] = from;
1232 target->nbTransTo++;
1233}
1234
1235static void
Daniel Veillard4255d502002-04-16 15:50:10 +00001236xmlRegStateAddTrans(xmlRegParserCtxtPtr ctxt, xmlRegStatePtr state,
1237 xmlRegAtomPtr atom, xmlRegStatePtr target,
Daniel Veillarddb68b742005-07-30 13:18:24 +00001238 int counter, int count, int nchk) {
William M. Brackf9b5fa22004-05-10 07:52:15 +00001239
1240 int nrtrans;
1241
Daniel Veillard4255d502002-04-16 15:50:10 +00001242 if (state == NULL) {
1243 ERROR("add state: state is NULL");
1244 return;
1245 }
1246 if (target == NULL) {
1247 ERROR("add state: target is NULL");
1248 return;
1249 }
William M. Brackf9b5fa22004-05-10 07:52:15 +00001250 /*
1251 * Other routines follow the philosophy 'When in doubt, add a transition'
1252 * so we check here whether such a transition is already present and, if
1253 * so, silently ignore this request.
1254 */
1255
Daniel Veillarddb68b742005-07-30 13:18:24 +00001256 if (nchk == 0) {
1257 for (nrtrans = state->nbTrans - 1; nrtrans >= 0; nrtrans--) {
1258 xmlRegTransPtr trans = &(state->trans[nrtrans]);
1259 if ((trans->atom == atom) &&
1260 (trans->to == target->no) &&
1261 (trans->counter == counter) &&
1262 (trans->count == count)) {
William M. Brackf9b5fa22004-05-10 07:52:15 +00001263#ifdef DEBUG_REGEXP_GRAPH
Daniel Veillarddb68b742005-07-30 13:18:24 +00001264 printf("Ignoring duplicate transition from %d to %d\n",
1265 state->no, target->no);
William M. Brackf9b5fa22004-05-10 07:52:15 +00001266#endif
Daniel Veillarddb68b742005-07-30 13:18:24 +00001267 return;
1268 }
1269 }
William M. Brackf9b5fa22004-05-10 07:52:15 +00001270 }
1271
Daniel Veillard4255d502002-04-16 15:50:10 +00001272 if (state->maxTrans == 0) {
Daniel Veillarddb68b742005-07-30 13:18:24 +00001273 state->maxTrans = 8;
Daniel Veillard4255d502002-04-16 15:50:10 +00001274 state->trans = (xmlRegTrans *) xmlMalloc(state->maxTrans *
1275 sizeof(xmlRegTrans));
1276 if (state->trans == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00001277 xmlRegexpErrMemory(ctxt, "adding transition");
Daniel Veillard4255d502002-04-16 15:50:10 +00001278 state->maxTrans = 0;
1279 return;
1280 }
1281 } else if (state->nbTrans >= state->maxTrans) {
1282 xmlRegTrans *tmp;
1283 state->maxTrans *= 2;
1284 tmp = (xmlRegTrans *) xmlRealloc(state->trans, state->maxTrans *
1285 sizeof(xmlRegTrans));
1286 if (tmp == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00001287 xmlRegexpErrMemory(ctxt, "adding transition");
Daniel Veillard4255d502002-04-16 15:50:10 +00001288 state->maxTrans /= 2;
1289 return;
1290 }
1291 state->trans = tmp;
1292 }
1293#ifdef DEBUG_REGEXP_GRAPH
1294 printf("Add trans from %d to %d ", state->no, target->no);
Daniel Veillard8a001f62002-04-20 07:24:11 +00001295 if (count == REGEXP_ALL_COUNTER)
Daniel Veillard2cbf5962004-03-31 15:50:43 +00001296 printf("all transition\n");
Daniel Veillard4402ab42002-09-12 16:02:56 +00001297 else if (count >= 0)
Daniel Veillard2cbf5962004-03-31 15:50:43 +00001298 printf("count based %d\n", count);
Daniel Veillard4255d502002-04-16 15:50:10 +00001299 else if (counter >= 0)
Daniel Veillard2cbf5962004-03-31 15:50:43 +00001300 printf("counted %d\n", counter);
Daniel Veillard4255d502002-04-16 15:50:10 +00001301 else if (atom == NULL)
Daniel Veillard2cbf5962004-03-31 15:50:43 +00001302 printf("epsilon transition\n");
1303 else if (atom != NULL)
1304 xmlRegPrintAtom(stdout, atom);
Daniel Veillard4255d502002-04-16 15:50:10 +00001305#endif
1306
1307 state->trans[state->nbTrans].atom = atom;
1308 state->trans[state->nbTrans].to = target->no;
1309 state->trans[state->nbTrans].counter = counter;
1310 state->trans[state->nbTrans].count = count;
1311 state->nbTrans++;
Daniel Veillarddb68b742005-07-30 13:18:24 +00001312 xmlRegStateAddTransTo(ctxt, target, state->no);
Daniel Veillard4255d502002-04-16 15:50:10 +00001313}
1314
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001315static int
Daniel Veillard4255d502002-04-16 15:50:10 +00001316xmlRegStatePush(xmlRegParserCtxtPtr ctxt, xmlRegStatePtr state) {
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001317 if (state == NULL) return(-1);
Daniel Veillard4255d502002-04-16 15:50:10 +00001318 if (ctxt->maxStates == 0) {
1319 ctxt->maxStates = 4;
1320 ctxt->states = (xmlRegStatePtr *) xmlMalloc(ctxt->maxStates *
1321 sizeof(xmlRegStatePtr));
1322 if (ctxt->states == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00001323 xmlRegexpErrMemory(ctxt, "adding state");
Daniel Veillard4255d502002-04-16 15:50:10 +00001324 ctxt->maxStates = 0;
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001325 return(-1);
Daniel Veillard4255d502002-04-16 15:50:10 +00001326 }
1327 } else if (ctxt->nbStates >= ctxt->maxStates) {
1328 xmlRegStatePtr *tmp;
1329 ctxt->maxStates *= 2;
1330 tmp = (xmlRegStatePtr *) xmlRealloc(ctxt->states, ctxt->maxStates *
1331 sizeof(xmlRegStatePtr));
1332 if (tmp == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00001333 xmlRegexpErrMemory(ctxt, "adding state");
Daniel Veillard4255d502002-04-16 15:50:10 +00001334 ctxt->maxStates /= 2;
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001335 return(-1);
Daniel Veillard4255d502002-04-16 15:50:10 +00001336 }
1337 ctxt->states = tmp;
1338 }
1339 state->no = ctxt->nbStates;
1340 ctxt->states[ctxt->nbStates++] = state;
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001341 return(0);
Daniel Veillard4255d502002-04-16 15:50:10 +00001342}
1343
1344/**
Daniel Veillard7646b182002-04-20 06:41:40 +00001345 * xmlFAGenerateAllTransition:
Daniel Veillard441bc322002-04-20 17:38:48 +00001346 * @ctxt: a regexp parser context
1347 * @from: the from state
1348 * @to: the target state or NULL for building a new one
1349 * @lax:
Daniel Veillard7646b182002-04-20 06:41:40 +00001350 *
1351 */
1352static void
1353xmlFAGenerateAllTransition(xmlRegParserCtxtPtr ctxt,
Daniel Veillard441bc322002-04-20 17:38:48 +00001354 xmlRegStatePtr from, xmlRegStatePtr to,
1355 int lax) {
Daniel Veillard7646b182002-04-20 06:41:40 +00001356 if (to == NULL) {
1357 to = xmlRegNewState(ctxt);
1358 xmlRegStatePush(ctxt, to);
1359 ctxt->state = to;
1360 }
Daniel Veillard441bc322002-04-20 17:38:48 +00001361 if (lax)
Daniel Veillarddb68b742005-07-30 13:18:24 +00001362 xmlRegStateAddTrans(ctxt, from, NULL, to, -1, REGEXP_ALL_LAX_COUNTER, 0);
Daniel Veillard441bc322002-04-20 17:38:48 +00001363 else
Daniel Veillarddb68b742005-07-30 13:18:24 +00001364 xmlRegStateAddTrans(ctxt, from, NULL, to, -1, REGEXP_ALL_COUNTER, 0);
Daniel Veillard7646b182002-04-20 06:41:40 +00001365}
1366
1367/**
Daniel Veillard4255d502002-04-16 15:50:10 +00001368 * xmlFAGenerateEpsilonTransition:
Daniel Veillard441bc322002-04-20 17:38:48 +00001369 * @ctxt: a regexp parser context
1370 * @from: the from state
1371 * @to: the target state or NULL for building a new one
Daniel Veillard4255d502002-04-16 15:50:10 +00001372 *
1373 */
1374static void
1375xmlFAGenerateEpsilonTransition(xmlRegParserCtxtPtr ctxt,
1376 xmlRegStatePtr from, xmlRegStatePtr to) {
1377 if (to == NULL) {
1378 to = xmlRegNewState(ctxt);
1379 xmlRegStatePush(ctxt, to);
1380 ctxt->state = to;
1381 }
Daniel Veillarddb68b742005-07-30 13:18:24 +00001382 xmlRegStateAddTrans(ctxt, from, NULL, to, -1, -1, 0);
Daniel Veillard4255d502002-04-16 15:50:10 +00001383}
1384
1385/**
1386 * xmlFAGenerateCountedEpsilonTransition:
Daniel Veillard441bc322002-04-20 17:38:48 +00001387 * @ctxt: a regexp parser context
1388 * @from: the from state
1389 * @to: the target state or NULL for building a new one
Daniel Veillard4255d502002-04-16 15:50:10 +00001390 * counter: the counter for that transition
1391 *
1392 */
1393static void
1394xmlFAGenerateCountedEpsilonTransition(xmlRegParserCtxtPtr ctxt,
1395 xmlRegStatePtr from, xmlRegStatePtr to, int counter) {
1396 if (to == NULL) {
1397 to = xmlRegNewState(ctxt);
1398 xmlRegStatePush(ctxt, to);
1399 ctxt->state = to;
1400 }
Daniel Veillarddb68b742005-07-30 13:18:24 +00001401 xmlRegStateAddTrans(ctxt, from, NULL, to, counter, -1, 0);
Daniel Veillard4255d502002-04-16 15:50:10 +00001402}
1403
1404/**
1405 * xmlFAGenerateCountedTransition:
Daniel Veillard441bc322002-04-20 17:38:48 +00001406 * @ctxt: a regexp parser context
1407 * @from: the from state
1408 * @to: the target state or NULL for building a new one
Daniel Veillard4255d502002-04-16 15:50:10 +00001409 * counter: the counter for that transition
1410 *
1411 */
1412static void
1413xmlFAGenerateCountedTransition(xmlRegParserCtxtPtr ctxt,
1414 xmlRegStatePtr from, xmlRegStatePtr to, int counter) {
1415 if (to == NULL) {
1416 to = xmlRegNewState(ctxt);
1417 xmlRegStatePush(ctxt, to);
1418 ctxt->state = to;
1419 }
Daniel Veillarddb68b742005-07-30 13:18:24 +00001420 xmlRegStateAddTrans(ctxt, from, NULL, to, -1, counter, 0);
Daniel Veillard4255d502002-04-16 15:50:10 +00001421}
1422
1423/**
1424 * xmlFAGenerateTransitions:
Daniel Veillard441bc322002-04-20 17:38:48 +00001425 * @ctxt: a regexp parser context
1426 * @from: the from state
1427 * @to: the target state or NULL for building a new one
1428 * @atom: the atom generating the transition
Daniel Veillard4255d502002-04-16 15:50:10 +00001429 *
William M. Brackddf71d62004-05-06 04:17:26 +00001430 * Returns 0 if success and -1 in case of error.
Daniel Veillard4255d502002-04-16 15:50:10 +00001431 */
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001432static int
Daniel Veillard4255d502002-04-16 15:50:10 +00001433xmlFAGenerateTransitions(xmlRegParserCtxtPtr ctxt, xmlRegStatePtr from,
1434 xmlRegStatePtr to, xmlRegAtomPtr atom) {
1435 if (atom == NULL) {
1436 ERROR("genrate transition: atom == NULL");
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001437 return(-1);
Daniel Veillard4255d502002-04-16 15:50:10 +00001438 }
1439 if (atom->type == XML_REGEXP_SUBREG) {
1440 /*
1441 * this is a subexpression handling one should not need to
William M. Brackddf71d62004-05-06 04:17:26 +00001442 * create a new node except for XML_REGEXP_QUANT_RANGE.
Daniel Veillard4255d502002-04-16 15:50:10 +00001443 */
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001444 if (xmlRegAtomPush(ctxt, atom) < 0) {
1445 return(-1);
1446 }
Daniel Veillard4255d502002-04-16 15:50:10 +00001447 if ((to != NULL) && (atom->stop != to) &&
1448 (atom->quant != XML_REGEXP_QUANT_RANGE)) {
1449 /*
1450 * Generate an epsilon transition to link to the target
1451 */
1452 xmlFAGenerateEpsilonTransition(ctxt, atom->stop, to);
1453 }
1454 switch (atom->quant) {
1455 case XML_REGEXP_QUANT_OPT:
1456 atom->quant = XML_REGEXP_QUANT_ONCE;
1457 xmlFAGenerateEpsilonTransition(ctxt, atom->start, atom->stop);
1458 break;
1459 case XML_REGEXP_QUANT_MULT:
1460 atom->quant = XML_REGEXP_QUANT_ONCE;
1461 xmlFAGenerateEpsilonTransition(ctxt, atom->start, atom->stop);
1462 xmlFAGenerateEpsilonTransition(ctxt, atom->stop, atom->start);
1463 break;
1464 case XML_REGEXP_QUANT_PLUS:
1465 atom->quant = XML_REGEXP_QUANT_ONCE;
1466 xmlFAGenerateEpsilonTransition(ctxt, atom->stop, atom->start);
1467 break;
1468 case XML_REGEXP_QUANT_RANGE: {
1469 int counter;
1470 xmlRegStatePtr newstate;
1471
1472 /*
1473 * This one is nasty:
William M. Brackddf71d62004-05-06 04:17:26 +00001474 * 1/ if range has minOccurs == 0, create a new state
1475 * and create epsilon transitions from atom->start
1476 * to atom->stop, as well as atom->start to the new
1477 * state
1478 * 2/ register a new counter
1479 * 3/ register an epsilon transition associated to
Daniel Veillard4255d502002-04-16 15:50:10 +00001480 * this counter going from atom->stop to atom->start
William M. Brackddf71d62004-05-06 04:17:26 +00001481 * 4/ create a new state
1482 * 5/ generate a counted transition from atom->stop to
Daniel Veillard4255d502002-04-16 15:50:10 +00001483 * that state
1484 */
William M. Brackddf71d62004-05-06 04:17:26 +00001485 if (atom->min == 0) {
1486 xmlFAGenerateEpsilonTransition(ctxt, atom->start,
1487 atom->stop);
1488 newstate = xmlRegNewState(ctxt);
1489 xmlRegStatePush(ctxt, newstate);
1490 ctxt->state = newstate;
1491 xmlFAGenerateEpsilonTransition(ctxt, atom->start,
1492 newstate);
1493 }
Daniel Veillard4255d502002-04-16 15:50:10 +00001494 counter = xmlRegGetCounter(ctxt);
1495 ctxt->counters[counter].min = atom->min - 1;
1496 ctxt->counters[counter].max = atom->max - 1;
1497 atom->min = 0;
1498 atom->max = 0;
1499 atom->quant = XML_REGEXP_QUANT_ONCE;
1500 xmlFAGenerateCountedEpsilonTransition(ctxt, atom->stop,
1501 atom->start, counter);
1502 if (to != NULL) {
1503 newstate = to;
1504 } else {
1505 newstate = xmlRegNewState(ctxt);
1506 xmlRegStatePush(ctxt, newstate);
1507 ctxt->state = newstate;
1508 }
1509 xmlFAGenerateCountedTransition(ctxt, atom->stop,
1510 newstate, counter);
1511 }
1512 default:
1513 break;
1514 }
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001515 return(0);
Daniel Veillard99c394d2005-07-14 12:58:49 +00001516 } else if ((atom->min == 0) && (atom->max == 0) &&
1517 (atom->quant == XML_REGEXP_QUANT_RANGE)) {
1518 /*
1519 * we can discard the atom and generate an epsilon transition instead
1520 */
1521 if (to == NULL) {
1522 to = xmlRegNewState(ctxt);
1523 if (to != NULL)
1524 xmlRegStatePush(ctxt, to);
1525 else {
1526 return(-1);
1527 }
1528 }
1529 xmlFAGenerateEpsilonTransition(ctxt, from, to);
1530 ctxt->state = to;
1531 xmlRegFreeAtom(atom);
1532 return(0);
Daniel Veillard4255d502002-04-16 15:50:10 +00001533 } else {
1534 if (to == NULL) {
1535 to = xmlRegNewState(ctxt);
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001536 if (to != NULL)
1537 xmlRegStatePush(ctxt, to);
1538 else {
1539 return(-1);
1540 }
1541 }
1542 if (xmlRegAtomPush(ctxt, atom) < 0) {
1543 return(-1);
Daniel Veillard4255d502002-04-16 15:50:10 +00001544 }
Daniel Veillarddb68b742005-07-30 13:18:24 +00001545 xmlRegStateAddTrans(ctxt, from, atom, to, -1, -1, 0);
Daniel Veillard4255d502002-04-16 15:50:10 +00001546 ctxt->state = to;
1547 }
1548 switch (atom->quant) {
1549 case XML_REGEXP_QUANT_OPT:
1550 atom->quant = XML_REGEXP_QUANT_ONCE;
1551 xmlFAGenerateEpsilonTransition(ctxt, from, to);
1552 break;
1553 case XML_REGEXP_QUANT_MULT:
1554 atom->quant = XML_REGEXP_QUANT_ONCE;
1555 xmlFAGenerateEpsilonTransition(ctxt, from, to);
Daniel Veillarddb68b742005-07-30 13:18:24 +00001556 xmlRegStateAddTrans(ctxt, to, atom, to, -1, -1, 0);
Daniel Veillard4255d502002-04-16 15:50:10 +00001557 break;
1558 case XML_REGEXP_QUANT_PLUS:
1559 atom->quant = XML_REGEXP_QUANT_ONCE;
Daniel Veillarddb68b742005-07-30 13:18:24 +00001560 xmlRegStateAddTrans(ctxt, to, atom, to, -1, -1, 0);
Daniel Veillard4255d502002-04-16 15:50:10 +00001561 break;
1562 default:
1563 break;
1564 }
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001565 return(0);
Daniel Veillard4255d502002-04-16 15:50:10 +00001566}
1567
1568/**
1569 * xmlFAReduceEpsilonTransitions:
Daniel Veillard441bc322002-04-20 17:38:48 +00001570 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00001571 * @fromnr: the from state
1572 * @tonr: the to state
William M. Brackddf71d62004-05-06 04:17:26 +00001573 * @counter: should that transition be associated to a counted
Daniel Veillard4255d502002-04-16 15:50:10 +00001574 *
1575 */
1576static void
1577xmlFAReduceEpsilonTransitions(xmlRegParserCtxtPtr ctxt, int fromnr,
1578 int tonr, int counter) {
1579 int transnr;
1580 xmlRegStatePtr from;
1581 xmlRegStatePtr to;
1582
1583#ifdef DEBUG_REGEXP_GRAPH
1584 printf("xmlFAReduceEpsilonTransitions(%d, %d)\n", fromnr, tonr);
1585#endif
1586 from = ctxt->states[fromnr];
1587 if (from == NULL)
1588 return;
1589 to = ctxt->states[tonr];
1590 if (to == NULL)
1591 return;
1592 if ((to->mark == XML_REGEXP_MARK_START) ||
1593 (to->mark == XML_REGEXP_MARK_VISITED))
1594 return;
1595
1596 to->mark = XML_REGEXP_MARK_VISITED;
1597 if (to->type == XML_REGEXP_FINAL_STATE) {
1598#ifdef DEBUG_REGEXP_GRAPH
1599 printf("State %d is final, so %d becomes final\n", tonr, fromnr);
1600#endif
1601 from->type = XML_REGEXP_FINAL_STATE;
1602 }
1603 for (transnr = 0;transnr < to->nbTrans;transnr++) {
Daniel Veillarddb68b742005-07-30 13:18:24 +00001604 if (to->trans[transnr].to < 0)
1605 continue;
Daniel Veillard4255d502002-04-16 15:50:10 +00001606 if (to->trans[transnr].atom == NULL) {
1607 /*
1608 * Don't remove counted transitions
1609 * Don't loop either
1610 */
Daniel Veillardb509f152002-04-17 16:28:10 +00001611 if (to->trans[transnr].to != fromnr) {
1612 if (to->trans[transnr].count >= 0) {
1613 int newto = to->trans[transnr].to;
1614
1615 xmlRegStateAddTrans(ctxt, from, NULL,
1616 ctxt->states[newto],
Daniel Veillarddb68b742005-07-30 13:18:24 +00001617 -1, to->trans[transnr].count, 0);
Daniel Veillardb509f152002-04-17 16:28:10 +00001618 } else {
Daniel Veillard4255d502002-04-16 15:50:10 +00001619#ifdef DEBUG_REGEXP_GRAPH
Daniel Veillardb509f152002-04-17 16:28:10 +00001620 printf("Found epsilon trans %d from %d to %d\n",
1621 transnr, tonr, to->trans[transnr].to);
Daniel Veillard4255d502002-04-16 15:50:10 +00001622#endif
Daniel Veillardb509f152002-04-17 16:28:10 +00001623 if (to->trans[transnr].counter >= 0) {
1624 xmlFAReduceEpsilonTransitions(ctxt, fromnr,
1625 to->trans[transnr].to,
1626 to->trans[transnr].counter);
1627 } else {
1628 xmlFAReduceEpsilonTransitions(ctxt, fromnr,
1629 to->trans[transnr].to,
1630 counter);
1631 }
1632 }
Daniel Veillard4255d502002-04-16 15:50:10 +00001633 }
1634 } else {
1635 int newto = to->trans[transnr].to;
1636
Daniel Veillardb509f152002-04-17 16:28:10 +00001637 if (to->trans[transnr].counter >= 0) {
1638 xmlRegStateAddTrans(ctxt, from, to->trans[transnr].atom,
1639 ctxt->states[newto],
Daniel Veillarddb68b742005-07-30 13:18:24 +00001640 to->trans[transnr].counter, -1, 1);
Daniel Veillardb509f152002-04-17 16:28:10 +00001641 } else {
1642 xmlRegStateAddTrans(ctxt, from, to->trans[transnr].atom,
Daniel Veillarddb68b742005-07-30 13:18:24 +00001643 ctxt->states[newto], counter, -1, 1);
Daniel Veillardb509f152002-04-17 16:28:10 +00001644 }
Daniel Veillard4255d502002-04-16 15:50:10 +00001645 }
1646 }
1647 to->mark = XML_REGEXP_MARK_NORMAL;
1648}
1649
1650/**
Daniel Veillarddb68b742005-07-30 13:18:24 +00001651 * xmlFAEliminateSimpleEpsilonTransitions:
1652 * @ctxt: a regexp parser context
1653 *
1654 * Eliminating general epsilon transitions can get costly in the general
1655 * algorithm due to the large amount of generated new transitions and
1656 * associated comparisons. However for simple epsilon transition used just
1657 * to separate building blocks when generating the automata this can be
1658 * reduced to state elimination:
1659 * - if there exists an epsilon from X to Y
1660 * - if there is no other transition from X
1661 * then X and Y are semantically equivalent and X can be eliminated
1662 * If X is the start state then make Y the start state, else replace the
1663 * target of all transitions to X by transitions to Y.
1664 */
1665static void
1666xmlFAEliminateSimpleEpsilonTransitions(xmlRegParserCtxtPtr ctxt) {
1667 int statenr, i, j, newto;
1668 xmlRegStatePtr state, tmp;
1669
1670 for (statenr = 0;statenr < ctxt->nbStates;statenr++) {
1671 state = ctxt->states[statenr];
1672 if (state == NULL)
1673 continue;
1674 if (state->nbTrans != 1)
1675 continue;
1676 /* is the only transition out a basic transition */
1677 if ((state->trans[0].atom == NULL) &&
1678 (state->trans[0].to >= 0) &&
1679 (state->trans[0].to != statenr) &&
1680 (state->trans[0].counter < 0) &&
1681 (state->trans[0].count < 0)) {
1682 newto = state->trans[0].to;
1683
1684 if (state->type == XML_REGEXP_START_STATE) {
1685#ifdef DEBUG_REGEXP_GRAPH
1686 printf("Found simple epsilon trans from start %d to %d\n",
1687 statenr, newto);
1688#endif
1689 } else {
1690#ifdef DEBUG_REGEXP_GRAPH
1691 printf("Found simple epsilon trans from %d to %d\n",
1692 statenr, newto);
1693#endif
1694 for (i = 0;i < state->nbTransTo;i++) {
1695 tmp = ctxt->states[state->transTo[i]];
1696 for (j = 0;j < tmp->nbTrans;j++) {
1697 if (tmp->trans[j].to == statenr) {
1698 tmp->trans[j].to = newto;
1699#ifdef DEBUG_REGEXP_GRAPH
1700 printf("Changed transition %d on %d to go to %d\n",
1701 j, tmp->no, newto);
1702#endif
1703 xmlRegStateAddTransTo(ctxt, ctxt->states[newto],
1704 tmp->no);
1705 }
1706 }
1707 }
1708#if 0
1709 for (i = 0;i < ctxt->nbStates;i++) {
1710 tmp = ctxt->states[i];
1711 for (j = 0;j < tmp->nbTrans;j++) {
1712 if (tmp->trans[j].to == statenr) {
1713 tmp->trans[j].to = newto;
1714#ifdef DEBUG_REGEXP_GRAPH
1715 printf("Changed transition %d on %d to go to %d\n",
1716 j, tmp->no, newto);
1717#endif
1718 }
1719 }
1720 }
1721#endif
1722 if (state->type == XML_REGEXP_FINAL_STATE)
1723 ctxt->states[newto]->type = XML_REGEXP_FINAL_STATE;
1724 /* eliminate the transition completely */
1725 state->nbTrans = 0;
1726
1727
1728 }
1729
1730 }
1731 }
1732}
1733/**
Daniel Veillard4255d502002-04-16 15:50:10 +00001734 * xmlFAEliminateEpsilonTransitions:
Daniel Veillard441bc322002-04-20 17:38:48 +00001735 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00001736 *
1737 */
1738static void
1739xmlFAEliminateEpsilonTransitions(xmlRegParserCtxtPtr ctxt) {
1740 int statenr, transnr;
1741 xmlRegStatePtr state;
Daniel Veillarddb68b742005-07-30 13:18:24 +00001742 int has_epsilon;
Daniel Veillard4255d502002-04-16 15:50:10 +00001743
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001744 if (ctxt->states == NULL) return;
1745
Daniel Veillarddb68b742005-07-30 13:18:24 +00001746 xmlFAEliminateSimpleEpsilonTransitions(ctxt);
1747
1748 has_epsilon = 0;
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001749
Daniel Veillard4255d502002-04-16 15:50:10 +00001750 /*
1751 * build the completed transitions bypassing the epsilons
1752 * Use a marking algorithm to avoid loops
Daniel Veillardcc026dc2005-01-12 13:21:17 +00001753 * mark sink states too.
Daniel Veillard4255d502002-04-16 15:50:10 +00001754 */
1755 for (statenr = 0;statenr < ctxt->nbStates;statenr++) {
1756 state = ctxt->states[statenr];
1757 if (state == NULL)
1758 continue;
Daniel Veillardcc026dc2005-01-12 13:21:17 +00001759 if ((state->nbTrans == 0) &&
1760 (state->type != XML_REGEXP_FINAL_STATE)) {
1761 state->type = XML_REGEXP_SINK_STATE;
1762 }
Daniel Veillard4255d502002-04-16 15:50:10 +00001763 for (transnr = 0;transnr < state->nbTrans;transnr++) {
1764 if ((state->trans[transnr].atom == NULL) &&
1765 (state->trans[transnr].to >= 0)) {
1766 if (state->trans[transnr].to == statenr) {
1767 state->trans[transnr].to = -1;
1768#ifdef DEBUG_REGEXP_GRAPH
1769 printf("Removed loopback epsilon trans %d on %d\n",
1770 transnr, statenr);
1771#endif
1772 } else if (state->trans[transnr].count < 0) {
1773 int newto = state->trans[transnr].to;
1774
1775#ifdef DEBUG_REGEXP_GRAPH
1776 printf("Found epsilon trans %d from %d to %d\n",
1777 transnr, statenr, newto);
1778#endif
1779 state->mark = XML_REGEXP_MARK_START;
Daniel Veillarddb68b742005-07-30 13:18:24 +00001780 has_epsilon = 1;
Daniel Veillard4255d502002-04-16 15:50:10 +00001781 xmlFAReduceEpsilonTransitions(ctxt, statenr,
1782 newto, state->trans[transnr].counter);
1783 state->mark = XML_REGEXP_MARK_NORMAL;
1784#ifdef DEBUG_REGEXP_GRAPH
1785 } else {
1786 printf("Found counted transition %d on %d\n",
1787 transnr, statenr);
1788#endif
1789 }
1790 }
1791 }
1792 }
1793 /*
1794 * Eliminate the epsilon transitions
1795 */
Daniel Veillarddb68b742005-07-30 13:18:24 +00001796 if (has_epsilon) {
1797 for (statenr = 0;statenr < ctxt->nbStates;statenr++) {
1798 state = ctxt->states[statenr];
1799 if (state == NULL)
1800 continue;
1801 for (transnr = 0;transnr < state->nbTrans;transnr++) {
1802 xmlRegTransPtr trans = &(state->trans[transnr]);
1803 if ((trans->atom == NULL) &&
1804 (trans->count < 0) &&
1805 (trans->to >= 0)) {
1806 trans->to = -1;
1807 }
Daniel Veillard4255d502002-04-16 15:50:10 +00001808 }
1809 }
1810 }
Daniel Veillard23e73572002-09-19 19:56:43 +00001811
1812 /*
1813 * Use this pass to detect unreachable states too
1814 */
1815 for (statenr = 0;statenr < ctxt->nbStates;statenr++) {
1816 state = ctxt->states[statenr];
1817 if (state != NULL)
William M. Brack779af002003-08-01 15:55:39 +00001818 state->reached = XML_REGEXP_MARK_NORMAL;
Daniel Veillard23e73572002-09-19 19:56:43 +00001819 }
1820 state = ctxt->states[0];
1821 if (state != NULL)
William M. Brack779af002003-08-01 15:55:39 +00001822 state->reached = XML_REGEXP_MARK_START;
Daniel Veillard23e73572002-09-19 19:56:43 +00001823 while (state != NULL) {
1824 xmlRegStatePtr target = NULL;
William M. Brack779af002003-08-01 15:55:39 +00001825 state->reached = XML_REGEXP_MARK_VISITED;
Daniel Veillard23e73572002-09-19 19:56:43 +00001826 /*
William M. Brackddf71d62004-05-06 04:17:26 +00001827 * Mark all states reachable from the current reachable state
Daniel Veillard23e73572002-09-19 19:56:43 +00001828 */
1829 for (transnr = 0;transnr < state->nbTrans;transnr++) {
1830 if ((state->trans[transnr].to >= 0) &&
1831 ((state->trans[transnr].atom != NULL) ||
1832 (state->trans[transnr].count >= 0))) {
1833 int newto = state->trans[transnr].to;
1834
1835 if (ctxt->states[newto] == NULL)
1836 continue;
William M. Brack779af002003-08-01 15:55:39 +00001837 if (ctxt->states[newto]->reached == XML_REGEXP_MARK_NORMAL) {
1838 ctxt->states[newto]->reached = XML_REGEXP_MARK_START;
Daniel Veillard23e73572002-09-19 19:56:43 +00001839 target = ctxt->states[newto];
1840 }
1841 }
1842 }
Daniel Veillardcc026dc2005-01-12 13:21:17 +00001843
Daniel Veillard23e73572002-09-19 19:56:43 +00001844 /*
1845 * find the next accessible state not explored
1846 */
1847 if (target == NULL) {
1848 for (statenr = 1;statenr < ctxt->nbStates;statenr++) {
1849 state = ctxt->states[statenr];
William M. Brack779af002003-08-01 15:55:39 +00001850 if ((state != NULL) && (state->reached ==
1851 XML_REGEXP_MARK_START)) {
Daniel Veillard23e73572002-09-19 19:56:43 +00001852 target = state;
1853 break;
1854 }
1855 }
1856 }
1857 state = target;
1858 }
1859 for (statenr = 0;statenr < ctxt->nbStates;statenr++) {
1860 state = ctxt->states[statenr];
William M. Brack779af002003-08-01 15:55:39 +00001861 if ((state != NULL) && (state->reached == XML_REGEXP_MARK_NORMAL)) {
Daniel Veillard23e73572002-09-19 19:56:43 +00001862#ifdef DEBUG_REGEXP_GRAPH
1863 printf("Removed unreachable state %d\n", statenr);
1864#endif
1865 xmlRegFreeState(state);
1866 ctxt->states[statenr] = NULL;
1867 }
1868 }
1869
Daniel Veillard4255d502002-04-16 15:50:10 +00001870}
1871
Daniel Veillarde19fc232002-04-22 16:01:24 +00001872/**
1873 * xmlFACompareAtoms:
1874 * @atom1: an atom
1875 * @atom2: an atom
1876 *
William M. Brackddf71d62004-05-06 04:17:26 +00001877 * Compares two atoms to check whether they are equivalents
Daniel Veillarde19fc232002-04-22 16:01:24 +00001878 *
1879 * Returns 1 if yes and 0 otherwise
1880 */
1881static int
1882xmlFACompareAtoms(xmlRegAtomPtr atom1, xmlRegAtomPtr atom2) {
Daniel Veillard9efc4762005-07-19 14:33:55 +00001883 int ret;
1884
Daniel Veillarde19fc232002-04-22 16:01:24 +00001885 if (atom1 == atom2)
1886 return(1);
1887 if ((atom1 == NULL) || (atom2 == NULL))
1888 return(0);
1889
1890 if (atom1->type != atom2->type)
1891 return(0);
1892 switch (atom1->type) {
1893 case XML_REGEXP_STRING:
Daniel Veillard9efc4762005-07-19 14:33:55 +00001894 ret = xmlRegStrEqualWildcard((xmlChar *)atom1->valuep,
1895 (xmlChar *)atom2->valuep);
1896 break;
Daniel Veillarde19fc232002-04-22 16:01:24 +00001897 case XML_REGEXP_EPSILON:
1898 return(1);
1899 case XML_REGEXP_CHARVAL:
Daniel Veillard9efc4762005-07-19 14:33:55 +00001900 ret = atom1->codepoint == atom2->codepoint;
1901 break;
Daniel Veillarde19fc232002-04-22 16:01:24 +00001902 case XML_REGEXP_RANGES:
1903 TODO;
1904 return(0);
1905 default:
Daniel Veillard9efc4762005-07-19 14:33:55 +00001906 return(1);
Daniel Veillarde19fc232002-04-22 16:01:24 +00001907 }
Daniel Veillard6e65e152005-08-09 11:09:52 +00001908 if (atom1->neg != atom2->neg) {
Daniel Veillard9efc4762005-07-19 14:33:55 +00001909 ret = !ret;
Daniel Veillard6e65e152005-08-09 11:09:52 +00001910 }
Daniel Veillard9efc4762005-07-19 14:33:55 +00001911 return(ret);
Daniel Veillarde19fc232002-04-22 16:01:24 +00001912}
1913
1914/**
1915 * xmlFARecurseDeterminism:
1916 * @ctxt: a regexp parser context
1917 *
1918 * Check whether the associated regexp is determinist,
1919 * should be called after xmlFAEliminateEpsilonTransitions()
1920 *
1921 */
1922static int
1923xmlFARecurseDeterminism(xmlRegParserCtxtPtr ctxt, xmlRegStatePtr state,
1924 int to, xmlRegAtomPtr atom) {
1925 int ret = 1;
1926 int transnr;
1927 xmlRegTransPtr t1;
1928
1929 if (state == NULL)
1930 return(ret);
1931 for (transnr = 0;transnr < state->nbTrans;transnr++) {
1932 t1 = &(state->trans[transnr]);
1933 /*
1934 * check transitions conflicting with the one looked at
1935 */
1936 if (t1->atom == NULL) {
1937 if (t1->to == -1)
1938 continue;
1939 ret = xmlFARecurseDeterminism(ctxt, ctxt->states[t1->to],
1940 to, atom);
1941 if (ret == 0)
1942 return(0);
1943 continue;
1944 }
1945 if (t1->to != to)
1946 continue;
1947 if (xmlFACompareAtoms(t1->atom, atom))
1948 return(0);
1949 }
1950 return(ret);
1951}
1952
1953/**
1954 * xmlFAComputesDeterminism:
1955 * @ctxt: a regexp parser context
1956 *
1957 * Check whether the associated regexp is determinist,
1958 * should be called after xmlFAEliminateEpsilonTransitions()
1959 *
1960 */
1961static int
1962xmlFAComputesDeterminism(xmlRegParserCtxtPtr ctxt) {
1963 int statenr, transnr;
1964 xmlRegStatePtr state;
1965 xmlRegTransPtr t1, t2;
1966 int i;
1967 int ret = 1;
1968
Daniel Veillard4402ab42002-09-12 16:02:56 +00001969#ifdef DEBUG_REGEXP_GRAPH
1970 printf("xmlFAComputesDeterminism\n");
1971 xmlRegPrintCtxt(stdout, ctxt);
1972#endif
Daniel Veillarde19fc232002-04-22 16:01:24 +00001973 if (ctxt->determinist != -1)
1974 return(ctxt->determinist);
1975
1976 /*
William M. Brackddf71d62004-05-06 04:17:26 +00001977 * Check for all states that there aren't 2 transitions
Daniel Veillarde19fc232002-04-22 16:01:24 +00001978 * with the same atom and a different target.
1979 */
1980 for (statenr = 0;statenr < ctxt->nbStates;statenr++) {
1981 state = ctxt->states[statenr];
1982 if (state == NULL)
1983 continue;
1984 for (transnr = 0;transnr < state->nbTrans;transnr++) {
1985 t1 = &(state->trans[transnr]);
1986 /*
1987 * Determinism checks in case of counted or all transitions
1988 * will have to be handled separately
1989 */
1990 if (t1->atom == NULL)
1991 continue;
1992 if (t1->to == -1) /* eliminated */
1993 continue;
1994 for (i = 0;i < transnr;i++) {
1995 t2 = &(state->trans[i]);
1996 if (t2->to == -1) /* eliminated */
1997 continue;
1998 if (t2->atom != NULL) {
1999 if (t1->to == t2->to) {
2000 if (xmlFACompareAtoms(t1->atom, t2->atom))
William M. Brackddf71d62004-05-06 04:17:26 +00002001 t2->to = -1; /* eliminated */
Daniel Veillarde19fc232002-04-22 16:01:24 +00002002 } else {
2003 /* not determinist ! */
2004 if (xmlFACompareAtoms(t1->atom, t2->atom))
2005 ret = 0;
2006 }
2007 } else if (t1->to != -1) {
2008 /*
2009 * do the closure in case of remaining specific
2010 * epsilon transitions like choices or all
2011 */
2012 ret = xmlFARecurseDeterminism(ctxt, ctxt->states[t1->to],
2013 t2->to, t2->atom);
2014 if (ret == 0)
2015 return(0);
2016 }
2017 }
2018 if (ret == 0)
2019 break;
2020 }
2021 if (ret == 0)
2022 break;
2023 }
2024 ctxt->determinist = ret;
2025 return(ret);
2026}
2027
Daniel Veillard4255d502002-04-16 15:50:10 +00002028/************************************************************************
2029 * *
2030 * Routines to check input against transition atoms *
2031 * *
2032 ************************************************************************/
2033
2034static int
2035xmlRegCheckCharacterRange(xmlRegAtomType type, int codepoint, int neg,
2036 int start, int end, const xmlChar *blockName) {
2037 int ret = 0;
2038
2039 switch (type) {
2040 case XML_REGEXP_STRING:
2041 case XML_REGEXP_SUBREG:
2042 case XML_REGEXP_RANGES:
2043 case XML_REGEXP_EPSILON:
2044 return(-1);
2045 case XML_REGEXP_ANYCHAR:
2046 ret = ((codepoint != '\n') && (codepoint != '\r'));
2047 break;
2048 case XML_REGEXP_CHARVAL:
2049 ret = ((codepoint >= start) && (codepoint <= end));
2050 break;
2051 case XML_REGEXP_NOTSPACE:
2052 neg = !neg;
2053 case XML_REGEXP_ANYSPACE:
2054 ret = ((codepoint == '\n') || (codepoint == '\r') ||
2055 (codepoint == '\t') || (codepoint == ' '));
2056 break;
2057 case XML_REGEXP_NOTINITNAME:
2058 neg = !neg;
2059 case XML_REGEXP_INITNAME:
William M. Brack871611b2003-10-18 04:53:14 +00002060 ret = (IS_LETTER(codepoint) ||
Daniel Veillard4255d502002-04-16 15:50:10 +00002061 (codepoint == '_') || (codepoint == ':'));
2062 break;
2063 case XML_REGEXP_NOTNAMECHAR:
2064 neg = !neg;
2065 case XML_REGEXP_NAMECHAR:
William M. Brack871611b2003-10-18 04:53:14 +00002066 ret = (IS_LETTER(codepoint) || IS_DIGIT(codepoint) ||
Daniel Veillard4255d502002-04-16 15:50:10 +00002067 (codepoint == '.') || (codepoint == '-') ||
2068 (codepoint == '_') || (codepoint == ':') ||
William M. Brack871611b2003-10-18 04:53:14 +00002069 IS_COMBINING(codepoint) || IS_EXTENDER(codepoint));
Daniel Veillard4255d502002-04-16 15:50:10 +00002070 break;
2071 case XML_REGEXP_NOTDECIMAL:
2072 neg = !neg;
2073 case XML_REGEXP_DECIMAL:
2074 ret = xmlUCSIsCatNd(codepoint);
2075 break;
2076 case XML_REGEXP_REALCHAR:
2077 neg = !neg;
2078 case XML_REGEXP_NOTREALCHAR:
2079 ret = xmlUCSIsCatP(codepoint);
2080 if (ret == 0)
2081 ret = xmlUCSIsCatZ(codepoint);
2082 if (ret == 0)
2083 ret = xmlUCSIsCatC(codepoint);
2084 break;
2085 case XML_REGEXP_LETTER:
2086 ret = xmlUCSIsCatL(codepoint);
2087 break;
2088 case XML_REGEXP_LETTER_UPPERCASE:
2089 ret = xmlUCSIsCatLu(codepoint);
2090 break;
2091 case XML_REGEXP_LETTER_LOWERCASE:
2092 ret = xmlUCSIsCatLl(codepoint);
2093 break;
2094 case XML_REGEXP_LETTER_TITLECASE:
2095 ret = xmlUCSIsCatLt(codepoint);
2096 break;
2097 case XML_REGEXP_LETTER_MODIFIER:
2098 ret = xmlUCSIsCatLm(codepoint);
2099 break;
2100 case XML_REGEXP_LETTER_OTHERS:
2101 ret = xmlUCSIsCatLo(codepoint);
2102 break;
2103 case XML_REGEXP_MARK:
2104 ret = xmlUCSIsCatM(codepoint);
2105 break;
2106 case XML_REGEXP_MARK_NONSPACING:
2107 ret = xmlUCSIsCatMn(codepoint);
2108 break;
2109 case XML_REGEXP_MARK_SPACECOMBINING:
2110 ret = xmlUCSIsCatMc(codepoint);
2111 break;
2112 case XML_REGEXP_MARK_ENCLOSING:
2113 ret = xmlUCSIsCatMe(codepoint);
2114 break;
2115 case XML_REGEXP_NUMBER:
2116 ret = xmlUCSIsCatN(codepoint);
2117 break;
2118 case XML_REGEXP_NUMBER_DECIMAL:
2119 ret = xmlUCSIsCatNd(codepoint);
2120 break;
2121 case XML_REGEXP_NUMBER_LETTER:
2122 ret = xmlUCSIsCatNl(codepoint);
2123 break;
2124 case XML_REGEXP_NUMBER_OTHERS:
2125 ret = xmlUCSIsCatNo(codepoint);
2126 break;
2127 case XML_REGEXP_PUNCT:
2128 ret = xmlUCSIsCatP(codepoint);
2129 break;
2130 case XML_REGEXP_PUNCT_CONNECTOR:
2131 ret = xmlUCSIsCatPc(codepoint);
2132 break;
2133 case XML_REGEXP_PUNCT_DASH:
2134 ret = xmlUCSIsCatPd(codepoint);
2135 break;
2136 case XML_REGEXP_PUNCT_OPEN:
2137 ret = xmlUCSIsCatPs(codepoint);
2138 break;
2139 case XML_REGEXP_PUNCT_CLOSE:
2140 ret = xmlUCSIsCatPe(codepoint);
2141 break;
2142 case XML_REGEXP_PUNCT_INITQUOTE:
2143 ret = xmlUCSIsCatPi(codepoint);
2144 break;
2145 case XML_REGEXP_PUNCT_FINQUOTE:
2146 ret = xmlUCSIsCatPf(codepoint);
2147 break;
2148 case XML_REGEXP_PUNCT_OTHERS:
2149 ret = xmlUCSIsCatPo(codepoint);
2150 break;
2151 case XML_REGEXP_SEPAR:
2152 ret = xmlUCSIsCatZ(codepoint);
2153 break;
2154 case XML_REGEXP_SEPAR_SPACE:
2155 ret = xmlUCSIsCatZs(codepoint);
2156 break;
2157 case XML_REGEXP_SEPAR_LINE:
2158 ret = xmlUCSIsCatZl(codepoint);
2159 break;
2160 case XML_REGEXP_SEPAR_PARA:
2161 ret = xmlUCSIsCatZp(codepoint);
2162 break;
2163 case XML_REGEXP_SYMBOL:
2164 ret = xmlUCSIsCatS(codepoint);
2165 break;
2166 case XML_REGEXP_SYMBOL_MATH:
2167 ret = xmlUCSIsCatSm(codepoint);
2168 break;
2169 case XML_REGEXP_SYMBOL_CURRENCY:
2170 ret = xmlUCSIsCatSc(codepoint);
2171 break;
2172 case XML_REGEXP_SYMBOL_MODIFIER:
2173 ret = xmlUCSIsCatSk(codepoint);
2174 break;
2175 case XML_REGEXP_SYMBOL_OTHERS:
2176 ret = xmlUCSIsCatSo(codepoint);
2177 break;
2178 case XML_REGEXP_OTHER:
2179 ret = xmlUCSIsCatC(codepoint);
2180 break;
2181 case XML_REGEXP_OTHER_CONTROL:
2182 ret = xmlUCSIsCatCc(codepoint);
2183 break;
2184 case XML_REGEXP_OTHER_FORMAT:
2185 ret = xmlUCSIsCatCf(codepoint);
2186 break;
2187 case XML_REGEXP_OTHER_PRIVATE:
2188 ret = xmlUCSIsCatCo(codepoint);
2189 break;
2190 case XML_REGEXP_OTHER_NA:
2191 /* ret = xmlUCSIsCatCn(codepoint); */
2192 /* Seems it doesn't exist anymore in recent Unicode releases */
2193 ret = 0;
2194 break;
2195 case XML_REGEXP_BLOCK_NAME:
2196 ret = xmlUCSIsBlock(codepoint, (const char *) blockName);
2197 break;
2198 }
2199 if (neg)
2200 return(!ret);
2201 return(ret);
2202}
2203
2204static int
2205xmlRegCheckCharacter(xmlRegAtomPtr atom, int codepoint) {
2206 int i, ret = 0;
2207 xmlRegRangePtr range;
2208
William M. Brack871611b2003-10-18 04:53:14 +00002209 if ((atom == NULL) || (!IS_CHAR(codepoint)))
Daniel Veillard4255d502002-04-16 15:50:10 +00002210 return(-1);
2211
2212 switch (atom->type) {
2213 case XML_REGEXP_SUBREG:
2214 case XML_REGEXP_EPSILON:
2215 return(-1);
2216 case XML_REGEXP_CHARVAL:
2217 return(codepoint == atom->codepoint);
2218 case XML_REGEXP_RANGES: {
2219 int accept = 0;
Daniel Veillardf2a12832003-11-24 13:04:35 +00002220
Daniel Veillard4255d502002-04-16 15:50:10 +00002221 for (i = 0;i < atom->nbRanges;i++) {
2222 range = atom->ranges[i];
Daniel Veillardf8b9de32003-11-24 14:27:26 +00002223 if (range->neg == 2) {
Daniel Veillard4255d502002-04-16 15:50:10 +00002224 ret = xmlRegCheckCharacterRange(range->type, codepoint,
2225 0, range->start, range->end,
2226 range->blockName);
2227 if (ret != 0)
2228 return(0); /* excluded char */
Daniel Veillardf8b9de32003-11-24 14:27:26 +00002229 } else if (range->neg) {
2230 ret = xmlRegCheckCharacterRange(range->type, codepoint,
2231 0, range->start, range->end,
2232 range->blockName);
2233 if (ret == 0)
Daniel Veillardf2a12832003-11-24 13:04:35 +00002234 accept = 1;
Daniel Veillardf8b9de32003-11-24 14:27:26 +00002235 else
2236 return(0);
Daniel Veillard4255d502002-04-16 15:50:10 +00002237 } else {
2238 ret = xmlRegCheckCharacterRange(range->type, codepoint,
2239 0, range->start, range->end,
2240 range->blockName);
2241 if (ret != 0)
2242 accept = 1; /* might still be excluded */
2243 }
2244 }
2245 return(accept);
2246 }
2247 case XML_REGEXP_STRING:
2248 printf("TODO: XML_REGEXP_STRING\n");
2249 return(-1);
2250 case XML_REGEXP_ANYCHAR:
2251 case XML_REGEXP_ANYSPACE:
2252 case XML_REGEXP_NOTSPACE:
2253 case XML_REGEXP_INITNAME:
2254 case XML_REGEXP_NOTINITNAME:
2255 case XML_REGEXP_NAMECHAR:
2256 case XML_REGEXP_NOTNAMECHAR:
2257 case XML_REGEXP_DECIMAL:
2258 case XML_REGEXP_NOTDECIMAL:
2259 case XML_REGEXP_REALCHAR:
2260 case XML_REGEXP_NOTREALCHAR:
2261 case XML_REGEXP_LETTER:
2262 case XML_REGEXP_LETTER_UPPERCASE:
2263 case XML_REGEXP_LETTER_LOWERCASE:
2264 case XML_REGEXP_LETTER_TITLECASE:
2265 case XML_REGEXP_LETTER_MODIFIER:
2266 case XML_REGEXP_LETTER_OTHERS:
2267 case XML_REGEXP_MARK:
2268 case XML_REGEXP_MARK_NONSPACING:
2269 case XML_REGEXP_MARK_SPACECOMBINING:
2270 case XML_REGEXP_MARK_ENCLOSING:
2271 case XML_REGEXP_NUMBER:
2272 case XML_REGEXP_NUMBER_DECIMAL:
2273 case XML_REGEXP_NUMBER_LETTER:
2274 case XML_REGEXP_NUMBER_OTHERS:
2275 case XML_REGEXP_PUNCT:
2276 case XML_REGEXP_PUNCT_CONNECTOR:
2277 case XML_REGEXP_PUNCT_DASH:
2278 case XML_REGEXP_PUNCT_OPEN:
2279 case XML_REGEXP_PUNCT_CLOSE:
2280 case XML_REGEXP_PUNCT_INITQUOTE:
2281 case XML_REGEXP_PUNCT_FINQUOTE:
2282 case XML_REGEXP_PUNCT_OTHERS:
2283 case XML_REGEXP_SEPAR:
2284 case XML_REGEXP_SEPAR_SPACE:
2285 case XML_REGEXP_SEPAR_LINE:
2286 case XML_REGEXP_SEPAR_PARA:
2287 case XML_REGEXP_SYMBOL:
2288 case XML_REGEXP_SYMBOL_MATH:
2289 case XML_REGEXP_SYMBOL_CURRENCY:
2290 case XML_REGEXP_SYMBOL_MODIFIER:
2291 case XML_REGEXP_SYMBOL_OTHERS:
2292 case XML_REGEXP_OTHER:
2293 case XML_REGEXP_OTHER_CONTROL:
2294 case XML_REGEXP_OTHER_FORMAT:
2295 case XML_REGEXP_OTHER_PRIVATE:
2296 case XML_REGEXP_OTHER_NA:
2297 case XML_REGEXP_BLOCK_NAME:
2298 ret = xmlRegCheckCharacterRange(atom->type, codepoint, 0, 0, 0,
2299 (const xmlChar *)atom->valuep);
2300 if (atom->neg)
2301 ret = !ret;
2302 break;
2303 }
2304 return(ret);
2305}
2306
2307/************************************************************************
2308 * *
William M. Brackddf71d62004-05-06 04:17:26 +00002309 * Saving and restoring state of an execution context *
Daniel Veillard4255d502002-04-16 15:50:10 +00002310 * *
2311 ************************************************************************/
2312
2313#ifdef DEBUG_REGEXP_EXEC
2314static void
2315xmlFARegDebugExec(xmlRegExecCtxtPtr exec) {
2316 printf("state: %d:%d:idx %d", exec->state->no, exec->transno, exec->index);
2317 if (exec->inputStack != NULL) {
2318 int i;
2319 printf(": ");
2320 for (i = 0;(i < 3) && (i < exec->inputStackNr);i++)
2321 printf("%s ", exec->inputStack[exec->inputStackNr - (i + 1)]);
2322 } else {
2323 printf(": %s", &(exec->inputString[exec->index]));
2324 }
2325 printf("\n");
2326}
2327#endif
2328
2329static void
2330xmlFARegExecSave(xmlRegExecCtxtPtr exec) {
2331#ifdef DEBUG_REGEXP_EXEC
2332 printf("saving ");
2333 exec->transno++;
2334 xmlFARegDebugExec(exec);
2335 exec->transno--;
2336#endif
2337
2338 if (exec->maxRollbacks == 0) {
2339 exec->maxRollbacks = 4;
2340 exec->rollbacks = (xmlRegExecRollback *) xmlMalloc(exec->maxRollbacks *
2341 sizeof(xmlRegExecRollback));
2342 if (exec->rollbacks == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00002343 xmlRegexpErrMemory(NULL, "saving regexp");
Daniel Veillard4255d502002-04-16 15:50:10 +00002344 exec->maxRollbacks = 0;
2345 return;
2346 }
2347 memset(exec->rollbacks, 0,
2348 exec->maxRollbacks * sizeof(xmlRegExecRollback));
2349 } else if (exec->nbRollbacks >= exec->maxRollbacks) {
2350 xmlRegExecRollback *tmp;
2351 int len = exec->maxRollbacks;
2352
2353 exec->maxRollbacks *= 2;
2354 tmp = (xmlRegExecRollback *) xmlRealloc(exec->rollbacks,
2355 exec->maxRollbacks * sizeof(xmlRegExecRollback));
2356 if (tmp == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00002357 xmlRegexpErrMemory(NULL, "saving regexp");
Daniel Veillard4255d502002-04-16 15:50:10 +00002358 exec->maxRollbacks /= 2;
2359 return;
2360 }
2361 exec->rollbacks = tmp;
2362 tmp = &exec->rollbacks[len];
2363 memset(tmp, 0, (exec->maxRollbacks - len) * sizeof(xmlRegExecRollback));
2364 }
2365 exec->rollbacks[exec->nbRollbacks].state = exec->state;
2366 exec->rollbacks[exec->nbRollbacks].index = exec->index;
2367 exec->rollbacks[exec->nbRollbacks].nextbranch = exec->transno + 1;
2368 if (exec->comp->nbCounters > 0) {
2369 if (exec->rollbacks[exec->nbRollbacks].counts == NULL) {
2370 exec->rollbacks[exec->nbRollbacks].counts = (int *)
2371 xmlMalloc(exec->comp->nbCounters * sizeof(int));
2372 if (exec->rollbacks[exec->nbRollbacks].counts == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00002373 xmlRegexpErrMemory(NULL, "saving regexp");
Daniel Veillard4255d502002-04-16 15:50:10 +00002374 exec->status = -5;
2375 return;
2376 }
2377 }
2378 memcpy(exec->rollbacks[exec->nbRollbacks].counts, exec->counts,
2379 exec->comp->nbCounters * sizeof(int));
2380 }
2381 exec->nbRollbacks++;
2382}
2383
2384static void
2385xmlFARegExecRollBack(xmlRegExecCtxtPtr exec) {
2386 if (exec->nbRollbacks <= 0) {
2387 exec->status = -1;
2388#ifdef DEBUG_REGEXP_EXEC
2389 printf("rollback failed on empty stack\n");
2390#endif
2391 return;
2392 }
2393 exec->nbRollbacks--;
2394 exec->state = exec->rollbacks[exec->nbRollbacks].state;
2395 exec->index = exec->rollbacks[exec->nbRollbacks].index;
2396 exec->transno = exec->rollbacks[exec->nbRollbacks].nextbranch;
2397 if (exec->comp->nbCounters > 0) {
2398 if (exec->rollbacks[exec->nbRollbacks].counts == NULL) {
2399 fprintf(stderr, "exec save: allocation failed");
2400 exec->status = -6;
2401 return;
2402 }
2403 memcpy(exec->counts, exec->rollbacks[exec->nbRollbacks].counts,
2404 exec->comp->nbCounters * sizeof(int));
2405 }
2406
2407#ifdef DEBUG_REGEXP_EXEC
2408 printf("restored ");
2409 xmlFARegDebugExec(exec);
2410#endif
2411}
2412
2413/************************************************************************
2414 * *
William M. Brackddf71d62004-05-06 04:17:26 +00002415 * Verifier, running an input against a compiled regexp *
Daniel Veillard4255d502002-04-16 15:50:10 +00002416 * *
2417 ************************************************************************/
2418
2419static int
2420xmlFARegExec(xmlRegexpPtr comp, const xmlChar *content) {
2421 xmlRegExecCtxt execval;
2422 xmlRegExecCtxtPtr exec = &execval;
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00002423 int ret, codepoint = 0, len;
Daniel Veillard4255d502002-04-16 15:50:10 +00002424
2425 exec->inputString = content;
2426 exec->index = 0;
2427 exec->determinist = 1;
2428 exec->maxRollbacks = 0;
2429 exec->nbRollbacks = 0;
2430 exec->rollbacks = NULL;
2431 exec->status = 0;
2432 exec->comp = comp;
2433 exec->state = comp->states[0];
2434 exec->transno = 0;
2435 exec->transcount = 0;
Daniel Veillardf2a12832003-11-24 13:04:35 +00002436 exec->inputStack = NULL;
2437 exec->inputStackMax = 0;
Daniel Veillard4255d502002-04-16 15:50:10 +00002438 if (comp->nbCounters > 0) {
2439 exec->counts = (int *) xmlMalloc(comp->nbCounters * sizeof(int));
Daniel Veillardff46a042003-10-08 08:53:17 +00002440 if (exec->counts == NULL) {
2441 xmlRegexpErrMemory(NULL, "running regexp");
Daniel Veillard4255d502002-04-16 15:50:10 +00002442 return(-1);
Daniel Veillardff46a042003-10-08 08:53:17 +00002443 }
Daniel Veillard4255d502002-04-16 15:50:10 +00002444 memset(exec->counts, 0, comp->nbCounters * sizeof(int));
2445 } else
2446 exec->counts = NULL;
2447 while ((exec->status == 0) &&
2448 ((exec->inputString[exec->index] != 0) ||
2449 (exec->state->type != XML_REGEXP_FINAL_STATE))) {
2450 xmlRegTransPtr trans;
2451 xmlRegAtomPtr atom;
2452
2453 /*
William M. Brack0e00b282004-04-26 15:40:47 +00002454 * If end of input on non-terminal state, rollback, however we may
Daniel Veillard4255d502002-04-16 15:50:10 +00002455 * still have epsilon like transition for counted transitions
William M. Brack0e00b282004-04-26 15:40:47 +00002456 * on counters, in that case don't break too early. Additionally,
2457 * if we are working on a range like "AB{0,2}", where B is not present,
2458 * we don't want to break.
Daniel Veillard4255d502002-04-16 15:50:10 +00002459 */
William M. Brack0e00b282004-04-26 15:40:47 +00002460 if ((exec->inputString[exec->index] == 0) && (exec->counts == NULL)) {
William M. Brackddf71d62004-05-06 04:17:26 +00002461 /*
2462 * if there is a transition, we must check if
2463 * atom allows minOccurs of 0
2464 */
2465 if (exec->transno < exec->state->nbTrans) {
William M. Brack0e00b282004-04-26 15:40:47 +00002466 trans = &exec->state->trans[exec->transno];
2467 if (trans->to >=0) {
2468 atom = trans->atom;
2469 if (!((atom->min == 0) && (atom->max > 0)))
2470 goto rollback;
2471 }
2472 } else
2473 goto rollback;
2474 }
Daniel Veillard4255d502002-04-16 15:50:10 +00002475
2476 exec->transcount = 0;
2477 for (;exec->transno < exec->state->nbTrans;exec->transno++) {
2478 trans = &exec->state->trans[exec->transno];
2479 if (trans->to < 0)
2480 continue;
2481 atom = trans->atom;
2482 ret = 0;
2483 if (trans->count >= 0) {
2484 int count;
2485 xmlRegCounterPtr counter;
2486
2487 /*
2488 * A counted transition.
2489 */
2490
2491 count = exec->counts[trans->count];
2492 counter = &exec->comp->counters[trans->count];
2493#ifdef DEBUG_REGEXP_EXEC
2494 printf("testing count %d: val %d, min %d, max %d\n",
2495 trans->count, count, counter->min, counter->max);
2496#endif
2497 ret = ((count >= counter->min) && (count <= counter->max));
2498 } else if (atom == NULL) {
2499 fprintf(stderr, "epsilon transition left at runtime\n");
2500 exec->status = -2;
2501 break;
2502 } else if (exec->inputString[exec->index] != 0) {
2503 codepoint = CUR_SCHAR(&(exec->inputString[exec->index]), len);
2504 ret = xmlRegCheckCharacter(atom, codepoint);
William M. Brack0e00b282004-04-26 15:40:47 +00002505 if ((ret == 1) && (atom->min >= 0) && (atom->max > 0)) {
Daniel Veillard4255d502002-04-16 15:50:10 +00002506 xmlRegStatePtr to = comp->states[trans->to];
2507
2508 /*
2509 * this is a multiple input sequence
2510 */
2511 if (exec->state->nbTrans > exec->transno + 1) {
2512 xmlFARegExecSave(exec);
2513 }
2514 exec->transcount = 1;
2515 do {
2516 /*
2517 * Try to progress as much as possible on the input
2518 */
2519 if (exec->transcount == atom->max) {
2520 break;
2521 }
2522 exec->index += len;
2523 /*
2524 * End of input: stop here
2525 */
2526 if (exec->inputString[exec->index] == 0) {
2527 exec->index -= len;
2528 break;
2529 }
2530 if (exec->transcount >= atom->min) {
2531 int transno = exec->transno;
2532 xmlRegStatePtr state = exec->state;
2533
2534 /*
2535 * The transition is acceptable save it
2536 */
2537 exec->transno = -1; /* trick */
2538 exec->state = to;
2539 xmlFARegExecSave(exec);
2540 exec->transno = transno;
2541 exec->state = state;
2542 }
2543 codepoint = CUR_SCHAR(&(exec->inputString[exec->index]),
2544 len);
2545 ret = xmlRegCheckCharacter(atom, codepoint);
2546 exec->transcount++;
2547 } while (ret == 1);
2548 if (exec->transcount < atom->min)
2549 ret = 0;
2550
2551 /*
2552 * If the last check failed but one transition was found
2553 * possible, rollback
2554 */
2555 if (ret < 0)
2556 ret = 0;
2557 if (ret == 0) {
2558 goto rollback;
2559 }
William M. Brack0e00b282004-04-26 15:40:47 +00002560 } else if ((ret == 0) && (atom->min == 0) && (atom->max > 0)) {
2561 /*
2562 * we don't match on the codepoint, but minOccurs of 0
2563 * says that's ok. Setting len to 0 inhibits stepping
2564 * over the codepoint.
2565 */
2566 exec->transcount = 1;
2567 len = 0;
2568 ret = 1;
Daniel Veillard4255d502002-04-16 15:50:10 +00002569 }
William M. Brack0e00b282004-04-26 15:40:47 +00002570 } else if ((atom->min == 0) && (atom->max > 0)) {
2571 /* another spot to match when minOccurs is 0 */
2572 exec->transcount = 1;
2573 len = 0;
2574 ret = 1;
Daniel Veillard4255d502002-04-16 15:50:10 +00002575 }
2576 if (ret == 1) {
2577 if (exec->state->nbTrans > exec->transno + 1) {
2578 xmlFARegExecSave(exec);
2579 }
2580 if (trans->counter >= 0) {
2581#ifdef DEBUG_REGEXP_EXEC
2582 printf("Increasing count %d\n", trans->counter);
2583#endif
2584 exec->counts[trans->counter]++;
2585 }
Daniel Veillard10752282005-08-08 13:05:13 +00002586 if ((trans->count >= 0) &&
2587 (trans->count < REGEXP_ALL_COUNTER)) {
2588#ifdef DEBUG_REGEXP_EXEC
2589 printf("resetting count %d on transition\n",
2590 trans->count);
2591#endif
2592 exec->counts[trans->count] = 0;
2593 }
Daniel Veillard4255d502002-04-16 15:50:10 +00002594#ifdef DEBUG_REGEXP_EXEC
2595 printf("entering state %d\n", trans->to);
2596#endif
2597 exec->state = comp->states[trans->to];
2598 exec->transno = 0;
2599 if (trans->atom != NULL) {
2600 exec->index += len;
2601 }
2602 goto progress;
2603 } else if (ret < 0) {
2604 exec->status = -4;
2605 break;
2606 }
2607 }
2608 if ((exec->transno != 0) || (exec->state->nbTrans == 0)) {
2609rollback:
2610 /*
2611 * Failed to find a way out
2612 */
2613 exec->determinist = 0;
2614 xmlFARegExecRollBack(exec);
2615 }
2616progress:
2617 continue;
2618 }
2619 if (exec->rollbacks != NULL) {
2620 if (exec->counts != NULL) {
2621 int i;
2622
2623 for (i = 0;i < exec->maxRollbacks;i++)
2624 if (exec->rollbacks[i].counts != NULL)
2625 xmlFree(exec->rollbacks[i].counts);
2626 }
2627 xmlFree(exec->rollbacks);
2628 }
2629 if (exec->counts != NULL)
2630 xmlFree(exec->counts);
2631 if (exec->status == 0)
2632 return(1);
2633 if (exec->status == -1)
2634 return(0);
2635 return(exec->status);
2636}
2637
2638/************************************************************************
2639 * *
William M. Brackddf71d62004-05-06 04:17:26 +00002640 * Progressive interface to the verifier one atom at a time *
Daniel Veillard4255d502002-04-16 15:50:10 +00002641 * *
2642 ************************************************************************/
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00002643#ifdef DEBUG_ERR
2644static void testerr(xmlRegExecCtxtPtr exec);
2645#endif
Daniel Veillard4255d502002-04-16 15:50:10 +00002646
2647/**
Daniel Veillard01c13b52002-12-10 15:19:08 +00002648 * xmlRegNewExecCtxt:
Daniel Veillard4255d502002-04-16 15:50:10 +00002649 * @comp: a precompiled regular expression
2650 * @callback: a callback function used for handling progresses in the
2651 * automata matching phase
2652 * @data: the context data associated to the callback in this context
2653 *
2654 * Build a context used for progressive evaluation of a regexp.
Daniel Veillard01c13b52002-12-10 15:19:08 +00002655 *
2656 * Returns the new context
Daniel Veillard4255d502002-04-16 15:50:10 +00002657 */
2658xmlRegExecCtxtPtr
2659xmlRegNewExecCtxt(xmlRegexpPtr comp, xmlRegExecCallbacks callback, void *data) {
2660 xmlRegExecCtxtPtr exec;
2661
2662 if (comp == NULL)
2663 return(NULL);
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00002664 if ((comp->compact == NULL) && (comp->states == NULL))
2665 return(NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +00002666 exec = (xmlRegExecCtxtPtr) xmlMalloc(sizeof(xmlRegExecCtxt));
2667 if (exec == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00002668 xmlRegexpErrMemory(NULL, "creating execution context");
Daniel Veillard4255d502002-04-16 15:50:10 +00002669 return(NULL);
2670 }
2671 memset(exec, 0, sizeof(xmlRegExecCtxt));
2672 exec->inputString = NULL;
2673 exec->index = 0;
2674 exec->determinist = 1;
2675 exec->maxRollbacks = 0;
2676 exec->nbRollbacks = 0;
2677 exec->rollbacks = NULL;
2678 exec->status = 0;
2679 exec->comp = comp;
Daniel Veillard23e73572002-09-19 19:56:43 +00002680 if (comp->compact == NULL)
2681 exec->state = comp->states[0];
Daniel Veillard4255d502002-04-16 15:50:10 +00002682 exec->transno = 0;
2683 exec->transcount = 0;
2684 exec->callback = callback;
2685 exec->data = data;
2686 if (comp->nbCounters > 0) {
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00002687 /*
2688 * For error handling, exec->counts is allocated twice the size
2689 * the second half is used to store the data in case of rollback
2690 */
2691 exec->counts = (int *) xmlMalloc(comp->nbCounters * sizeof(int)
2692 * 2);
Daniel Veillard4255d502002-04-16 15:50:10 +00002693 if (exec->counts == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00002694 xmlRegexpErrMemory(NULL, "creating execution context");
Daniel Veillard4255d502002-04-16 15:50:10 +00002695 xmlFree(exec);
2696 return(NULL);
2697 }
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00002698 memset(exec->counts, 0, comp->nbCounters * sizeof(int) * 2);
2699 exec->errCounts = &exec->counts[comp->nbCounters];
2700 } else {
Daniel Veillard4255d502002-04-16 15:50:10 +00002701 exec->counts = NULL;
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00002702 exec->errCounts = NULL;
2703 }
Daniel Veillard4255d502002-04-16 15:50:10 +00002704 exec->inputStackMax = 0;
2705 exec->inputStackNr = 0;
2706 exec->inputStack = NULL;
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00002707 exec->errStateNo = -1;
2708 exec->errString = NULL;
Daniel Veillard4255d502002-04-16 15:50:10 +00002709 return(exec);
2710}
2711
2712/**
2713 * xmlRegFreeExecCtxt:
2714 * @exec: a regular expression evaulation context
2715 *
2716 * Free the structures associated to a regular expression evaulation context.
2717 */
2718void
2719xmlRegFreeExecCtxt(xmlRegExecCtxtPtr exec) {
2720 if (exec == NULL)
2721 return;
2722
2723 if (exec->rollbacks != NULL) {
2724 if (exec->counts != NULL) {
2725 int i;
2726
2727 for (i = 0;i < exec->maxRollbacks;i++)
2728 if (exec->rollbacks[i].counts != NULL)
2729 xmlFree(exec->rollbacks[i].counts);
2730 }
2731 xmlFree(exec->rollbacks);
2732 }
2733 if (exec->counts != NULL)
2734 xmlFree(exec->counts);
2735 if (exec->inputStack != NULL) {
2736 int i;
2737
Daniel Veillard32370232002-10-16 14:08:14 +00002738 for (i = 0;i < exec->inputStackNr;i++) {
2739 if (exec->inputStack[i].value != NULL)
2740 xmlFree(exec->inputStack[i].value);
2741 }
Daniel Veillard4255d502002-04-16 15:50:10 +00002742 xmlFree(exec->inputStack);
2743 }
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00002744 if (exec->errString != NULL)
2745 xmlFree(exec->errString);
Daniel Veillard4255d502002-04-16 15:50:10 +00002746 xmlFree(exec);
2747}
2748
2749static void
2750xmlFARegExecSaveInputString(xmlRegExecCtxtPtr exec, const xmlChar *value,
2751 void *data) {
2752#ifdef DEBUG_PUSH
2753 printf("saving value: %d:%s\n", exec->inputStackNr, value);
2754#endif
2755 if (exec->inputStackMax == 0) {
2756 exec->inputStackMax = 4;
2757 exec->inputStack = (xmlRegInputTokenPtr)
2758 xmlMalloc(exec->inputStackMax * sizeof(xmlRegInputToken));
2759 if (exec->inputStack == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00002760 xmlRegexpErrMemory(NULL, "pushing input string");
Daniel Veillard4255d502002-04-16 15:50:10 +00002761 exec->inputStackMax = 0;
2762 return;
2763 }
2764 } else if (exec->inputStackNr + 1 >= exec->inputStackMax) {
2765 xmlRegInputTokenPtr tmp;
2766
2767 exec->inputStackMax *= 2;
2768 tmp = (xmlRegInputTokenPtr) xmlRealloc(exec->inputStack,
2769 exec->inputStackMax * sizeof(xmlRegInputToken));
2770 if (tmp == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00002771 xmlRegexpErrMemory(NULL, "pushing input string");
Daniel Veillard4255d502002-04-16 15:50:10 +00002772 exec->inputStackMax /= 2;
2773 return;
2774 }
2775 exec->inputStack = tmp;
2776 }
2777 exec->inputStack[exec->inputStackNr].value = xmlStrdup(value);
2778 exec->inputStack[exec->inputStackNr].data = data;
2779 exec->inputStackNr++;
2780 exec->inputStack[exec->inputStackNr].value = NULL;
2781 exec->inputStack[exec->inputStackNr].data = NULL;
2782}
2783
Daniel Veillardc0826a72004-08-10 14:17:33 +00002784/**
2785 * xmlRegStrEqualWildcard:
2786 * @expStr: the string to be evaluated
2787 * @valStr: the validation string
2788 *
2789 * Checks if both strings are equal or have the same content. "*"
2790 * can be used as a wildcard in @valStr; "|" is used as a seperator of
2791 * substrings in both @expStr and @valStr.
2792 *
2793 * Returns 1 if the comparison is satisfied and the number of substrings
2794 * is equal, 0 otherwise.
2795 */
2796
2797static int
2798xmlRegStrEqualWildcard(const xmlChar *expStr, const xmlChar *valStr) {
2799 if (expStr == valStr) return(1);
2800 if (expStr == NULL) return(0);
2801 if (valStr == NULL) return(0);
2802 do {
2803 /*
2804 * Eval if we have a wildcard for the current item.
2805 */
2806 if (*expStr != *valStr) {
2807 if ((*valStr != 0) && (*expStr != 0) && (*expStr++ == '*')) {
2808 do {
2809 if (*valStr == XML_REG_STRING_SEPARATOR)
2810 break;
Kasimier T. Buchcikc0e833f2005-04-19 15:02:20 +00002811 valStr++;
Daniel Veillardc0826a72004-08-10 14:17:33 +00002812 } while (*valStr != 0);
2813 continue;
2814 } else
2815 return(0);
2816 }
Kasimier T. Buchcikc0e833f2005-04-19 15:02:20 +00002817 expStr++;
2818 valStr++;
Daniel Veillardc0826a72004-08-10 14:17:33 +00002819 } while (*valStr != 0);
2820 if (*expStr != 0)
2821 return (0);
2822 else
2823 return (1);
2824}
Daniel Veillard4255d502002-04-16 15:50:10 +00002825
2826/**
Daniel Veillard23e73572002-09-19 19:56:43 +00002827 * xmlRegCompactPushString:
2828 * @exec: a regexp execution context
2829 * @comp: the precompiled exec with a compact table
2830 * @value: a string token input
2831 * @data: data associated to the token to reuse in callbacks
2832 *
2833 * Push one input token in the execution context
2834 *
2835 * Returns: 1 if the regexp reached a final state, 0 if non-final, and
2836 * a negative value in case of error.
2837 */
2838static int
2839xmlRegCompactPushString(xmlRegExecCtxtPtr exec,
2840 xmlRegexpPtr comp,
2841 const xmlChar *value,
2842 void *data) {
2843 int state = exec->index;
2844 int i, target;
2845
2846 if ((comp == NULL) || (comp->compact == NULL) || (comp->stringMap == NULL))
2847 return(-1);
2848
2849 if (value == NULL) {
2850 /*
2851 * are we at a final state ?
2852 */
2853 if (comp->compact[state * (comp->nbstrings + 1)] ==
2854 XML_REGEXP_FINAL_STATE)
2855 return(1);
2856 return(0);
2857 }
2858
2859#ifdef DEBUG_PUSH
2860 printf("value pushed: %s\n", value);
2861#endif
2862
2863 /*
William M. Brackddf71d62004-05-06 04:17:26 +00002864 * Examine all outside transitions from current state
Daniel Veillard23e73572002-09-19 19:56:43 +00002865 */
2866 for (i = 0;i < comp->nbstrings;i++) {
2867 target = comp->compact[state * (comp->nbstrings + 1) + i + 1];
2868 if ((target > 0) && (target <= comp->nbstates)) {
Daniel Veillardc0826a72004-08-10 14:17:33 +00002869 target--; /* to avoid 0 */
2870 if (xmlRegStrEqualWildcard(comp->stringMap[i], value)) {
2871 exec->index = target;
Daniel Veillard118aed72002-09-24 14:13:13 +00002872 if ((exec->callback != NULL) && (comp->transdata != NULL)) {
2873 exec->callback(exec->data, value,
2874 comp->transdata[state * comp->nbstrings + i], data);
2875 }
Daniel Veillard23e73572002-09-19 19:56:43 +00002876#ifdef DEBUG_PUSH
2877 printf("entering state %d\n", target);
2878#endif
2879 if (comp->compact[target * (comp->nbstrings + 1)] ==
Daniel Veillardcc026dc2005-01-12 13:21:17 +00002880 XML_REGEXP_SINK_STATE)
2881 goto error;
2882
2883 if (comp->compact[target * (comp->nbstrings + 1)] ==
Daniel Veillard23e73572002-09-19 19:56:43 +00002884 XML_REGEXP_FINAL_STATE)
2885 return(1);
2886 return(0);
2887 }
2888 }
2889 }
2890 /*
2891 * Failed to find an exit transition out from current state for the
2892 * current token
2893 */
2894#ifdef DEBUG_PUSH
2895 printf("failed to find a transition for %s on state %d\n", value, state);
2896#endif
Daniel Veillardcc026dc2005-01-12 13:21:17 +00002897error:
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00002898 if (exec->errString != NULL)
2899 xmlFree(exec->errString);
2900 exec->errString = xmlStrdup(value);
2901 exec->errStateNo = state;
Daniel Veillard23e73572002-09-19 19:56:43 +00002902 exec->status = -1;
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00002903#ifdef DEBUG_ERR
2904 testerr(exec);
2905#endif
Daniel Veillard23e73572002-09-19 19:56:43 +00002906 return(-1);
2907}
2908
2909/**
Daniel Veillard6e65e152005-08-09 11:09:52 +00002910 * xmlRegExecPushStringInternal:
Daniel Veillardea7751d2002-12-20 00:16:24 +00002911 * @exec: a regexp execution context or NULL to indicate the end
Daniel Veillard4255d502002-04-16 15:50:10 +00002912 * @value: a string token input
2913 * @data: data associated to the token to reuse in callbacks
Daniel Veillard6e65e152005-08-09 11:09:52 +00002914 * @compound: value was assembled from 2 strings
Daniel Veillard4255d502002-04-16 15:50:10 +00002915 *
2916 * Push one input token in the execution context
2917 *
2918 * Returns: 1 if the regexp reached a final state, 0 if non-final, and
2919 * a negative value in case of error.
2920 */
Daniel Veillard6e65e152005-08-09 11:09:52 +00002921static int
2922xmlRegExecPushStringInternal(xmlRegExecCtxtPtr exec, const xmlChar *value,
2923 void *data, int compound) {
Daniel Veillard4255d502002-04-16 15:50:10 +00002924 xmlRegTransPtr trans;
2925 xmlRegAtomPtr atom;
2926 int ret;
2927 int final = 0;
Daniel Veillard90700152005-01-08 22:05:09 +00002928 int progress = 1;
Daniel Veillard4255d502002-04-16 15:50:10 +00002929
2930 if (exec == NULL)
2931 return(-1);
Daniel Veillard23e73572002-09-19 19:56:43 +00002932 if (exec->comp == NULL)
2933 return(-1);
Daniel Veillard4255d502002-04-16 15:50:10 +00002934 if (exec->status != 0)
2935 return(exec->status);
2936
Daniel Veillard23e73572002-09-19 19:56:43 +00002937 if (exec->comp->compact != NULL)
2938 return(xmlRegCompactPushString(exec, exec->comp, value, data));
2939
Daniel Veillard4255d502002-04-16 15:50:10 +00002940 if (value == NULL) {
2941 if (exec->state->type == XML_REGEXP_FINAL_STATE)
2942 return(1);
2943 final = 1;
2944 }
2945
2946#ifdef DEBUG_PUSH
2947 printf("value pushed: %s\n", value);
2948#endif
2949 /*
2950 * If we have an active rollback stack push the new value there
2951 * and get back to where we were left
2952 */
2953 if ((value != NULL) && (exec->inputStackNr > 0)) {
2954 xmlFARegExecSaveInputString(exec, value, data);
2955 value = exec->inputStack[exec->index].value;
2956 data = exec->inputStack[exec->index].data;
2957#ifdef DEBUG_PUSH
2958 printf("value loaded: %s\n", value);
2959#endif
2960 }
2961
2962 while ((exec->status == 0) &&
2963 ((value != NULL) ||
2964 ((final == 1) &&
2965 (exec->state->type != XML_REGEXP_FINAL_STATE)))) {
2966
2967 /*
2968 * End of input on non-terminal state, rollback, however we may
2969 * still have epsilon like transition for counted transitions
2970 * on counters, in that case don't break too early.
2971 */
Daniel Veillardb509f152002-04-17 16:28:10 +00002972 if ((value == NULL) && (exec->counts == NULL))
Daniel Veillard4255d502002-04-16 15:50:10 +00002973 goto rollback;
2974
2975 exec->transcount = 0;
2976 for (;exec->transno < exec->state->nbTrans;exec->transno++) {
2977 trans = &exec->state->trans[exec->transno];
2978 if (trans->to < 0)
2979 continue;
2980 atom = trans->atom;
2981 ret = 0;
Daniel Veillard441bc322002-04-20 17:38:48 +00002982 if (trans->count == REGEXP_ALL_LAX_COUNTER) {
2983 int i;
2984 int count;
2985 xmlRegTransPtr t;
2986 xmlRegCounterPtr counter;
2987
2988 ret = 0;
2989
2990#ifdef DEBUG_PUSH
2991 printf("testing all lax %d\n", trans->count);
2992#endif
2993 /*
2994 * Check all counted transitions from the current state
2995 */
2996 if ((value == NULL) && (final)) {
2997 ret = 1;
2998 } else if (value != NULL) {
2999 for (i = 0;i < exec->state->nbTrans;i++) {
3000 t = &exec->state->trans[i];
3001 if ((t->counter < 0) || (t == trans))
3002 continue;
3003 counter = &exec->comp->counters[t->counter];
3004 count = exec->counts[t->counter];
3005 if ((count < counter->max) &&
3006 (t->atom != NULL) &&
3007 (xmlStrEqual(value, t->atom->valuep))) {
3008 ret = 0;
3009 break;
3010 }
3011 if ((count >= counter->min) &&
3012 (count < counter->max) &&
3013 (xmlStrEqual(value, t->atom->valuep))) {
3014 ret = 1;
3015 break;
3016 }
3017 }
3018 }
3019 } else if (trans->count == REGEXP_ALL_COUNTER) {
Daniel Veillard8a001f62002-04-20 07:24:11 +00003020 int i;
3021 int count;
3022 xmlRegTransPtr t;
3023 xmlRegCounterPtr counter;
3024
3025 ret = 1;
3026
3027#ifdef DEBUG_PUSH
3028 printf("testing all %d\n", trans->count);
3029#endif
3030 /*
3031 * Check all counted transitions from the current state
3032 */
3033 for (i = 0;i < exec->state->nbTrans;i++) {
3034 t = &exec->state->trans[i];
3035 if ((t->counter < 0) || (t == trans))
3036 continue;
3037 counter = &exec->comp->counters[t->counter];
3038 count = exec->counts[t->counter];
3039 if ((count < counter->min) || (count > counter->max)) {
3040 ret = 0;
3041 break;
3042 }
3043 }
3044 } else if (trans->count >= 0) {
Daniel Veillard4255d502002-04-16 15:50:10 +00003045 int count;
3046 xmlRegCounterPtr counter;
3047
3048 /*
3049 * A counted transition.
3050 */
3051
3052 count = exec->counts[trans->count];
3053 counter = &exec->comp->counters[trans->count];
3054#ifdef DEBUG_PUSH
3055 printf("testing count %d: val %d, min %d, max %d\n",
3056 trans->count, count, counter->min, counter->max);
3057#endif
3058 ret = ((count >= counter->min) && (count <= counter->max));
3059 } else if (atom == NULL) {
3060 fprintf(stderr, "epsilon transition left at runtime\n");
3061 exec->status = -2;
3062 break;
3063 } else if (value != NULL) {
Daniel Veillardc0826a72004-08-10 14:17:33 +00003064 ret = xmlRegStrEqualWildcard(atom->valuep, value);
Daniel Veillard6e65e152005-08-09 11:09:52 +00003065 if (atom->neg) {
Daniel Veillard9efc4762005-07-19 14:33:55 +00003066 ret = !ret;
Daniel Veillard6e65e152005-08-09 11:09:52 +00003067 if (!compound)
3068 ret = 0;
3069 }
Daniel Veillard441bc322002-04-20 17:38:48 +00003070 if ((ret == 1) && (trans->counter >= 0)) {
3071 xmlRegCounterPtr counter;
3072 int count;
3073
3074 count = exec->counts[trans->counter];
3075 counter = &exec->comp->counters[trans->counter];
3076 if (count >= counter->max)
3077 ret = 0;
3078 }
3079
Daniel Veillard4255d502002-04-16 15:50:10 +00003080 if ((ret == 1) && (atom->min > 0) && (atom->max > 0)) {
3081 xmlRegStatePtr to = exec->comp->states[trans->to];
3082
3083 /*
3084 * this is a multiple input sequence
3085 */
3086 if (exec->state->nbTrans > exec->transno + 1) {
3087 if (exec->inputStackNr <= 0) {
3088 xmlFARegExecSaveInputString(exec, value, data);
3089 }
3090 xmlFARegExecSave(exec);
3091 }
3092 exec->transcount = 1;
3093 do {
3094 /*
3095 * Try to progress as much as possible on the input
3096 */
3097 if (exec->transcount == atom->max) {
3098 break;
3099 }
3100 exec->index++;
3101 value = exec->inputStack[exec->index].value;
3102 data = exec->inputStack[exec->index].data;
3103#ifdef DEBUG_PUSH
3104 printf("value loaded: %s\n", value);
3105#endif
3106
3107 /*
3108 * End of input: stop here
3109 */
3110 if (value == NULL) {
3111 exec->index --;
3112 break;
3113 }
3114 if (exec->transcount >= atom->min) {
3115 int transno = exec->transno;
3116 xmlRegStatePtr state = exec->state;
3117
3118 /*
3119 * The transition is acceptable save it
3120 */
3121 exec->transno = -1; /* trick */
3122 exec->state = to;
3123 if (exec->inputStackNr <= 0) {
3124 xmlFARegExecSaveInputString(exec, value, data);
3125 }
3126 xmlFARegExecSave(exec);
3127 exec->transno = transno;
3128 exec->state = state;
3129 }
3130 ret = xmlStrEqual(value, atom->valuep);
3131 exec->transcount++;
3132 } while (ret == 1);
3133 if (exec->transcount < atom->min)
3134 ret = 0;
3135
3136 /*
3137 * If the last check failed but one transition was found
3138 * possible, rollback
3139 */
3140 if (ret < 0)
3141 ret = 0;
3142 if (ret == 0) {
3143 goto rollback;
3144 }
3145 }
3146 }
3147 if (ret == 1) {
William M. Brack98873952003-12-26 06:03:14 +00003148 if ((exec->callback != NULL) && (atom != NULL) &&
3149 (data != NULL)) {
Daniel Veillard4255d502002-04-16 15:50:10 +00003150 exec->callback(exec->data, atom->valuep,
3151 atom->data, data);
3152 }
3153 if (exec->state->nbTrans > exec->transno + 1) {
3154 if (exec->inputStackNr <= 0) {
3155 xmlFARegExecSaveInputString(exec, value, data);
3156 }
3157 xmlFARegExecSave(exec);
3158 }
3159 if (trans->counter >= 0) {
3160#ifdef DEBUG_PUSH
3161 printf("Increasing count %d\n", trans->counter);
3162#endif
3163 exec->counts[trans->counter]++;
3164 }
Daniel Veillard10752282005-08-08 13:05:13 +00003165 if ((trans->count >= 0) &&
3166 (trans->count < REGEXP_ALL_COUNTER)) {
3167#ifdef DEBUG_REGEXP_EXEC
3168 printf("resetting count %d on transition\n",
3169 trans->count);
3170#endif
3171 exec->counts[trans->count] = 0;
3172 }
Daniel Veillard4255d502002-04-16 15:50:10 +00003173#ifdef DEBUG_PUSH
3174 printf("entering state %d\n", trans->to);
3175#endif
Daniel Veillardcc026dc2005-01-12 13:21:17 +00003176 if ((exec->comp->states[trans->to] != NULL) &&
3177 (exec->comp->states[trans->to]->type ==
3178 XML_REGEXP_SINK_STATE)) {
3179 /*
3180 * entering a sink state, save the current state as error
3181 * state.
3182 */
3183 if (exec->errString != NULL)
3184 xmlFree(exec->errString);
3185 exec->errString = xmlStrdup(value);
3186 exec->errState = exec->state;
3187 memcpy(exec->errCounts, exec->counts,
3188 exec->comp->nbCounters * sizeof(int));
3189 }
Daniel Veillard4255d502002-04-16 15:50:10 +00003190 exec->state = exec->comp->states[trans->to];
3191 exec->transno = 0;
3192 if (trans->atom != NULL) {
3193 if (exec->inputStack != NULL) {
3194 exec->index++;
3195 if (exec->index < exec->inputStackNr) {
3196 value = exec->inputStack[exec->index].value;
3197 data = exec->inputStack[exec->index].data;
3198#ifdef DEBUG_PUSH
3199 printf("value loaded: %s\n", value);
3200#endif
3201 } else {
3202 value = NULL;
3203 data = NULL;
3204#ifdef DEBUG_PUSH
3205 printf("end of input\n");
3206#endif
3207 }
3208 } else {
3209 value = NULL;
3210 data = NULL;
3211#ifdef DEBUG_PUSH
3212 printf("end of input\n");
3213#endif
3214 }
3215 }
3216 goto progress;
3217 } else if (ret < 0) {
3218 exec->status = -4;
3219 break;
3220 }
3221 }
3222 if ((exec->transno != 0) || (exec->state->nbTrans == 0)) {
3223rollback:
Daniel Veillard90700152005-01-08 22:05:09 +00003224 /*
Daniel Veillardcc026dc2005-01-12 13:21:17 +00003225 * if we didn't yet rollback on the current input
3226 * store the current state as the error state.
Daniel Veillard90700152005-01-08 22:05:09 +00003227 */
Daniel Veillardcc026dc2005-01-12 13:21:17 +00003228 if ((progress) && (exec->state != NULL) &&
3229 (exec->state->type != XML_REGEXP_SINK_STATE)) {
Daniel Veillard90700152005-01-08 22:05:09 +00003230 progress = 0;
3231 if (exec->errString != NULL)
3232 xmlFree(exec->errString);
3233 exec->errString = xmlStrdup(value);
3234 exec->errState = exec->state;
3235 memcpy(exec->errCounts, exec->counts,
3236 exec->comp->nbCounters * sizeof(int));
3237 }
3238
Daniel Veillard4255d502002-04-16 15:50:10 +00003239 /*
3240 * Failed to find a way out
3241 */
3242 exec->determinist = 0;
3243 xmlFARegExecRollBack(exec);
3244 if (exec->status == 0) {
3245 value = exec->inputStack[exec->index].value;
3246 data = exec->inputStack[exec->index].data;
3247#ifdef DEBUG_PUSH
3248 printf("value loaded: %s\n", value);
3249#endif
3250 }
3251 }
Daniel Veillard90700152005-01-08 22:05:09 +00003252 continue;
Daniel Veillard4255d502002-04-16 15:50:10 +00003253progress:
Daniel Veillard90700152005-01-08 22:05:09 +00003254 progress = 1;
Daniel Veillard4255d502002-04-16 15:50:10 +00003255 continue;
3256 }
3257 if (exec->status == 0) {
3258 return(exec->state->type == XML_REGEXP_FINAL_STATE);
3259 }
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003260#ifdef DEBUG_ERR
Daniel Veillard90700152005-01-08 22:05:09 +00003261 if (exec->status < 0) {
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003262 testerr(exec);
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003263 }
Daniel Veillard90700152005-01-08 22:05:09 +00003264#endif
Daniel Veillard4255d502002-04-16 15:50:10 +00003265 return(exec->status);
3266}
3267
Daniel Veillard52b48c72003-04-13 19:53:42 +00003268/**
Daniel Veillard6e65e152005-08-09 11:09:52 +00003269 * xmlRegExecPushString:
3270 * @exec: a regexp execution context or NULL to indicate the end
3271 * @value: a string token input
3272 * @data: data associated to the token to reuse in callbacks
3273 *
3274 * Push one input token in the execution context
3275 *
3276 * Returns: 1 if the regexp reached a final state, 0 if non-final, and
3277 * a negative value in case of error.
3278 */
3279int
3280xmlRegExecPushString(xmlRegExecCtxtPtr exec, const xmlChar *value,
3281 void *data) {
3282 return(xmlRegExecPushStringInternal(exec, value, data, 0));
3283}
3284
3285/**
Daniel Veillard52b48c72003-04-13 19:53:42 +00003286 * xmlRegExecPushString2:
3287 * @exec: a regexp execution context or NULL to indicate the end
3288 * @value: the first string token input
3289 * @value2: the second string token input
3290 * @data: data associated to the token to reuse in callbacks
3291 *
3292 * Push one input token in the execution context
3293 *
3294 * Returns: 1 if the regexp reached a final state, 0 if non-final, and
3295 * a negative value in case of error.
3296 */
3297int
3298xmlRegExecPushString2(xmlRegExecCtxtPtr exec, const xmlChar *value,
3299 const xmlChar *value2, void *data) {
3300 xmlChar buf[150];
3301 int lenn, lenp, ret;
3302 xmlChar *str;
3303
3304 if (exec == NULL)
3305 return(-1);
3306 if (exec->comp == NULL)
3307 return(-1);
3308 if (exec->status != 0)
3309 return(exec->status);
3310
3311 if (value2 == NULL)
3312 return(xmlRegExecPushString(exec, value, data));
3313
3314 lenn = strlen((char *) value2);
3315 lenp = strlen((char *) value);
3316
3317 if (150 < lenn + lenp + 2) {
Daniel Veillard3c908dc2003-04-19 00:07:51 +00003318 str = (xmlChar *) xmlMallocAtomic(lenn + lenp + 2);
Daniel Veillard52b48c72003-04-13 19:53:42 +00003319 if (str == NULL) {
3320 exec->status = -1;
3321 return(-1);
3322 }
3323 } else {
3324 str = buf;
3325 }
3326 memcpy(&str[0], value, lenp);
Daniel Veillardc0826a72004-08-10 14:17:33 +00003327 str[lenp] = XML_REG_STRING_SEPARATOR;
Daniel Veillard52b48c72003-04-13 19:53:42 +00003328 memcpy(&str[lenp + 1], value2, lenn);
3329 str[lenn + lenp + 1] = 0;
3330
3331 if (exec->comp->compact != NULL)
3332 ret = xmlRegCompactPushString(exec, exec->comp, str, data);
3333 else
Daniel Veillard6e65e152005-08-09 11:09:52 +00003334 ret = xmlRegExecPushStringInternal(exec, str, data, 1);
Daniel Veillard52b48c72003-04-13 19:53:42 +00003335
3336 if (str != buf)
3337 xmlFree(buf);
3338 return(ret);
3339}
3340
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003341/**
Daniel Veillard77005e62005-07-19 16:26:18 +00003342 * xmlRegExecGetValues:
Daniel Veillardfc0b6f62005-01-09 17:48:02 +00003343 * @exec: a regexp execution context
3344 * @err: error extraction or normal one
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003345 * @nbval: pointer to the number of accepted values IN/OUT
Daniel Veillardcc026dc2005-01-12 13:21:17 +00003346 * @nbneg: return number of negative transitions
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003347 * @values: pointer to the array of acceptable values
Daniel Veillardfc0b6f62005-01-09 17:48:02 +00003348 * @terminal: return value if this was a terminal state
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003349 *
Daniel Veillardfc0b6f62005-01-09 17:48:02 +00003350 * Extract informations from the regexp execution, internal routine to
3351 * implement xmlRegExecNextValues() and xmlRegExecErrInfo()
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003352 *
3353 * Returns: 0 in case of success or -1 in case of error.
3354 */
Daniel Veillardfc0b6f62005-01-09 17:48:02 +00003355static int
3356xmlRegExecGetValues(xmlRegExecCtxtPtr exec, int err,
Daniel Veillardcc026dc2005-01-12 13:21:17 +00003357 int *nbval, int *nbneg,
3358 xmlChar **values, int *terminal) {
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003359 int maxval;
Daniel Veillardcc026dc2005-01-12 13:21:17 +00003360 int nb = 0;
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003361
Daniel Veillardcc026dc2005-01-12 13:21:17 +00003362 if ((exec == NULL) || (nbval == NULL) || (nbneg == NULL) ||
3363 (values == NULL) || (*nbval <= 0))
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003364 return(-1);
Daniel Veillardfc0b6f62005-01-09 17:48:02 +00003365
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003366 maxval = *nbval;
3367 *nbval = 0;
Daniel Veillardcc026dc2005-01-12 13:21:17 +00003368 *nbneg = 0;
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003369 if ((exec->comp != NULL) && (exec->comp->compact != NULL)) {
3370 xmlRegexpPtr comp;
3371 int target, i, state;
3372
3373 comp = exec->comp;
Daniel Veillardfc0b6f62005-01-09 17:48:02 +00003374
3375 if (err) {
3376 if (exec->errStateNo == -1) return(-1);
3377 state = exec->errStateNo;
3378 } else {
3379 state = exec->index;
3380 }
3381 if (terminal != NULL) {
3382 if (comp->compact[state * (comp->nbstrings + 1)] ==
3383 XML_REGEXP_FINAL_STATE)
3384 *terminal = 1;
3385 else
3386 *terminal = 0;
3387 }
Daniel Veillardcc026dc2005-01-12 13:21:17 +00003388 for (i = 0;(i < comp->nbstrings) && (nb < maxval);i++) {
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003389 target = comp->compact[state * (comp->nbstrings + 1) + i + 1];
Daniel Veillardcc026dc2005-01-12 13:21:17 +00003390 if ((target > 0) && (target <= comp->nbstates) &&
3391 (comp->compact[(target - 1) * (comp->nbstrings + 1)] !=
3392 XML_REGEXP_SINK_STATE)) {
3393 values[nb++] = comp->stringMap[i];
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003394 (*nbval)++;
3395 }
3396 }
Daniel Veillardcc026dc2005-01-12 13:21:17 +00003397 for (i = 0;(i < comp->nbstrings) && (nb < maxval);i++) {
3398 target = comp->compact[state * (comp->nbstrings + 1) + i + 1];
3399 if ((target > 0) && (target <= comp->nbstates) &&
3400 (comp->compact[(target - 1) * (comp->nbstrings + 1)] ==
3401 XML_REGEXP_SINK_STATE)) {
3402 values[nb++] = comp->stringMap[i];
3403 (*nbneg)++;
3404 }
3405 }
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003406 } else {
3407 int transno;
3408 xmlRegTransPtr trans;
3409 xmlRegAtomPtr atom;
Daniel Veillardfc0b6f62005-01-09 17:48:02 +00003410 xmlRegStatePtr state;
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003411
Daniel Veillardfc0b6f62005-01-09 17:48:02 +00003412 if (terminal != NULL) {
3413 if (exec->state->type == XML_REGEXP_FINAL_STATE)
3414 *terminal = 1;
3415 else
3416 *terminal = 0;
3417 }
3418
3419 if (err) {
3420 if (exec->errState == NULL) return(-1);
3421 state = exec->errState;
3422 } else {
3423 if (exec->state == NULL) return(-1);
3424 state = exec->state;
3425 }
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003426 for (transno = 0;
Daniel Veillardcc026dc2005-01-12 13:21:17 +00003427 (transno < state->nbTrans) && (nb < maxval);
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003428 transno++) {
Daniel Veillardfc0b6f62005-01-09 17:48:02 +00003429 trans = &state->trans[transno];
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003430 if (trans->to < 0)
3431 continue;
3432 atom = trans->atom;
3433 if ((atom == NULL) || (atom->valuep == NULL))
3434 continue;
3435 if (trans->count == REGEXP_ALL_LAX_COUNTER) {
Daniel Veillardcc026dc2005-01-12 13:21:17 +00003436 /* this should not be reached but ... */
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003437 TODO;
3438 } else if (trans->count == REGEXP_ALL_COUNTER) {
Daniel Veillardcc026dc2005-01-12 13:21:17 +00003439 /* this should not be reached but ... */
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003440 TODO;
3441 } else if (trans->counter >= 0) {
3442 xmlRegCounterPtr counter;
3443 int count;
3444
Daniel Veillardfc0b6f62005-01-09 17:48:02 +00003445 if (err)
3446 count = exec->errCounts[trans->counter];
3447 else
3448 count = exec->counts[trans->counter];
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003449 counter = &exec->comp->counters[trans->counter];
3450 if (count < counter->max) {
Daniel Veillard77005e62005-07-19 16:26:18 +00003451 if (atom->neg)
3452 values[nb++] = (xmlChar *) atom->valuep2;
3453 else
3454 values[nb++] = (xmlChar *) atom->valuep;
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003455 (*nbval)++;
3456 }
3457 } else {
Daniel Veillardcc026dc2005-01-12 13:21:17 +00003458 if ((exec->comp->states[trans->to] != NULL) &&
3459 (exec->comp->states[trans->to]->type !=
3460 XML_REGEXP_SINK_STATE)) {
Daniel Veillard77005e62005-07-19 16:26:18 +00003461 if (atom->neg)
3462 values[nb++] = (xmlChar *) atom->valuep2;
3463 else
3464 values[nb++] = (xmlChar *) atom->valuep;
Daniel Veillardcc026dc2005-01-12 13:21:17 +00003465 (*nbval)++;
3466 }
3467 }
3468 }
3469 for (transno = 0;
3470 (transno < state->nbTrans) && (nb < maxval);
3471 transno++) {
3472 trans = &state->trans[transno];
3473 if (trans->to < 0)
3474 continue;
3475 atom = trans->atom;
3476 if ((atom == NULL) || (atom->valuep == NULL))
3477 continue;
3478 if (trans->count == REGEXP_ALL_LAX_COUNTER) {
3479 continue;
3480 } else if (trans->count == REGEXP_ALL_COUNTER) {
3481 continue;
3482 } else if (trans->counter >= 0) {
3483 continue;
3484 } else {
3485 if ((exec->comp->states[trans->to] != NULL) &&
3486 (exec->comp->states[trans->to]->type ==
3487 XML_REGEXP_SINK_STATE)) {
Daniel Veillard77005e62005-07-19 16:26:18 +00003488 if (atom->neg)
3489 values[nb++] = (xmlChar *) atom->valuep2;
3490 else
3491 values[nb++] = (xmlChar *) atom->valuep;
Daniel Veillardcc026dc2005-01-12 13:21:17 +00003492 (*nbneg)++;
3493 }
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003494 }
3495 }
3496 }
3497 return(0);
3498}
3499
Daniel Veillardfc0b6f62005-01-09 17:48:02 +00003500/**
3501 * xmlRegExecNextValues:
3502 * @exec: a regexp execution context
3503 * @nbval: pointer to the number of accepted values IN/OUT
Daniel Veillardcc026dc2005-01-12 13:21:17 +00003504 * @nbneg: return number of negative transitions
Daniel Veillardfc0b6f62005-01-09 17:48:02 +00003505 * @values: pointer to the array of acceptable values
3506 * @terminal: return value if this was a terminal state
3507 *
3508 * Extract informations from the regexp execution,
3509 * the parameter @values must point to an array of @nbval string pointers
3510 * on return nbval will contain the number of possible strings in that
3511 * state and the @values array will be updated with them. The string values
3512 * returned will be freed with the @exec context and don't need to be
3513 * deallocated.
3514 *
3515 * Returns: 0 in case of success or -1 in case of error.
3516 */
3517int
Daniel Veillardcc026dc2005-01-12 13:21:17 +00003518xmlRegExecNextValues(xmlRegExecCtxtPtr exec, int *nbval, int *nbneg,
3519 xmlChar **values, int *terminal) {
3520 return(xmlRegExecGetValues(exec, 0, nbval, nbneg, values, terminal));
Daniel Veillardfc0b6f62005-01-09 17:48:02 +00003521}
3522
3523/**
3524 * xmlRegExecErrInfo:
3525 * @exec: a regexp execution context generating an error
3526 * @string: return value for the error string
3527 * @nbval: pointer to the number of accepted values IN/OUT
Daniel Veillardcc026dc2005-01-12 13:21:17 +00003528 * @nbneg: return number of negative transitions
Daniel Veillardfc0b6f62005-01-09 17:48:02 +00003529 * @values: pointer to the array of acceptable values
3530 * @terminal: return value if this was a terminal state
3531 *
3532 * Extract error informations from the regexp execution, the parameter
3533 * @string will be updated with the value pushed and not accepted,
3534 * the parameter @values must point to an array of @nbval string pointers
3535 * on return nbval will contain the number of possible strings in that
3536 * state and the @values array will be updated with them. The string values
3537 * returned will be freed with the @exec context and don't need to be
3538 * deallocated.
3539 *
3540 * Returns: 0 in case of success or -1 in case of error.
3541 */
3542int
3543xmlRegExecErrInfo(xmlRegExecCtxtPtr exec, const xmlChar **string,
Daniel Veillardcc026dc2005-01-12 13:21:17 +00003544 int *nbval, int *nbneg, xmlChar **values, int *terminal) {
Daniel Veillardfc0b6f62005-01-09 17:48:02 +00003545 if (exec == NULL)
3546 return(-1);
3547 if (string != NULL) {
3548 if (exec->status != 0)
3549 *string = exec->errString;
3550 else
3551 *string = NULL;
3552 }
Daniel Veillardcc026dc2005-01-12 13:21:17 +00003553 return(xmlRegExecGetValues(exec, 1, nbval, nbneg, values, terminal));
Daniel Veillardfc0b6f62005-01-09 17:48:02 +00003554}
3555
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003556#ifdef DEBUG_ERR
3557static void testerr(xmlRegExecCtxtPtr exec) {
3558 const xmlChar *string;
Daniel Veillardcee2b3a2005-01-25 00:22:52 +00003559 xmlChar *values[5];
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003560 int nb = 5;
Daniel Veillardcc026dc2005-01-12 13:21:17 +00003561 int nbneg;
Daniel Veillardfc0b6f62005-01-09 17:48:02 +00003562 int terminal;
Daniel Veillardcc026dc2005-01-12 13:21:17 +00003563 xmlRegExecErrInfo(exec, &string, &nb, &nbneg, &values[0], &terminal);
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003564}
3565#endif
3566
Daniel Veillard4255d502002-04-16 15:50:10 +00003567#if 0
3568static int
3569xmlRegExecPushChar(xmlRegExecCtxtPtr exec, int UCS) {
3570 xmlRegTransPtr trans;
3571 xmlRegAtomPtr atom;
3572 int ret;
3573 int codepoint, len;
3574
3575 if (exec == NULL)
3576 return(-1);
3577 if (exec->status != 0)
3578 return(exec->status);
3579
3580 while ((exec->status == 0) &&
3581 ((exec->inputString[exec->index] != 0) ||
3582 (exec->state->type != XML_REGEXP_FINAL_STATE))) {
3583
3584 /*
3585 * End of input on non-terminal state, rollback, however we may
3586 * still have epsilon like transition for counted transitions
3587 * on counters, in that case don't break too early.
3588 */
3589 if ((exec->inputString[exec->index] == 0) && (exec->counts == NULL))
3590 goto rollback;
3591
3592 exec->transcount = 0;
3593 for (;exec->transno < exec->state->nbTrans;exec->transno++) {
3594 trans = &exec->state->trans[exec->transno];
3595 if (trans->to < 0)
3596 continue;
3597 atom = trans->atom;
3598 ret = 0;
3599 if (trans->count >= 0) {
3600 int count;
3601 xmlRegCounterPtr counter;
3602
3603 /*
3604 * A counted transition.
3605 */
3606
3607 count = exec->counts[trans->count];
3608 counter = &exec->comp->counters[trans->count];
3609#ifdef DEBUG_REGEXP_EXEC
3610 printf("testing count %d: val %d, min %d, max %d\n",
3611 trans->count, count, counter->min, counter->max);
3612#endif
3613 ret = ((count >= counter->min) && (count <= counter->max));
3614 } else if (atom == NULL) {
3615 fprintf(stderr, "epsilon transition left at runtime\n");
3616 exec->status = -2;
3617 break;
3618 } else if (exec->inputString[exec->index] != 0) {
3619 codepoint = CUR_SCHAR(&(exec->inputString[exec->index]), len);
3620 ret = xmlRegCheckCharacter(atom, codepoint);
3621 if ((ret == 1) && (atom->min > 0) && (atom->max > 0)) {
3622 xmlRegStatePtr to = exec->comp->states[trans->to];
3623
3624 /*
3625 * this is a multiple input sequence
3626 */
3627 if (exec->state->nbTrans > exec->transno + 1) {
3628 xmlFARegExecSave(exec);
3629 }
3630 exec->transcount = 1;
3631 do {
3632 /*
3633 * Try to progress as much as possible on the input
3634 */
3635 if (exec->transcount == atom->max) {
3636 break;
3637 }
3638 exec->index += len;
3639 /*
3640 * End of input: stop here
3641 */
3642 if (exec->inputString[exec->index] == 0) {
3643 exec->index -= len;
3644 break;
3645 }
3646 if (exec->transcount >= atom->min) {
3647 int transno = exec->transno;
3648 xmlRegStatePtr state = exec->state;
3649
3650 /*
3651 * The transition is acceptable save it
3652 */
3653 exec->transno = -1; /* trick */
3654 exec->state = to;
3655 xmlFARegExecSave(exec);
3656 exec->transno = transno;
3657 exec->state = state;
3658 }
3659 codepoint = CUR_SCHAR(&(exec->inputString[exec->index]),
3660 len);
3661 ret = xmlRegCheckCharacter(atom, codepoint);
3662 exec->transcount++;
3663 } while (ret == 1);
3664 if (exec->transcount < atom->min)
3665 ret = 0;
3666
3667 /*
3668 * If the last check failed but one transition was found
3669 * possible, rollback
3670 */
3671 if (ret < 0)
3672 ret = 0;
3673 if (ret == 0) {
3674 goto rollback;
3675 }
3676 }
3677 }
3678 if (ret == 1) {
3679 if (exec->state->nbTrans > exec->transno + 1) {
3680 xmlFARegExecSave(exec);
3681 }
3682 if (trans->counter >= 0) {
3683#ifdef DEBUG_REGEXP_EXEC
3684 printf("Increasing count %d\n", trans->counter);
3685#endif
3686 exec->counts[trans->counter]++;
3687 }
3688#ifdef DEBUG_REGEXP_EXEC
3689 printf("entering state %d\n", trans->to);
3690#endif
3691 exec->state = exec->comp->states[trans->to];
3692 exec->transno = 0;
3693 if (trans->atom != NULL) {
3694 exec->index += len;
3695 }
3696 goto progress;
3697 } else if (ret < 0) {
3698 exec->status = -4;
3699 break;
3700 }
3701 }
3702 if ((exec->transno != 0) || (exec->state->nbTrans == 0)) {
3703rollback:
3704 /*
3705 * Failed to find a way out
3706 */
3707 exec->determinist = 0;
3708 xmlFARegExecRollBack(exec);
3709 }
3710progress:
3711 continue;
3712 }
3713}
3714#endif
3715/************************************************************************
3716 * *
William M. Brackddf71d62004-05-06 04:17:26 +00003717 * Parser for the Schemas Datatype Regular Expressions *
Daniel Veillard4255d502002-04-16 15:50:10 +00003718 * http://www.w3.org/TR/2001/REC-xmlschema-2-20010502/#regexs *
3719 * *
3720 ************************************************************************/
3721
3722/**
3723 * xmlFAIsChar:
Daniel Veillard441bc322002-04-20 17:38:48 +00003724 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00003725 *
3726 * [10] Char ::= [^.\?*+()|#x5B#x5D]
3727 */
3728static int
3729xmlFAIsChar(xmlRegParserCtxtPtr ctxt) {
3730 int cur;
3731 int len;
3732
3733 cur = CUR_SCHAR(ctxt->cur, len);
3734 if ((cur == '.') || (cur == '\\') || (cur == '?') ||
3735 (cur == '*') || (cur == '+') || (cur == '(') ||
3736 (cur == ')') || (cur == '|') || (cur == 0x5B) ||
3737 (cur == 0x5D) || (cur == 0))
3738 return(-1);
3739 return(cur);
3740}
3741
3742/**
3743 * xmlFAParseCharProp:
Daniel Veillard441bc322002-04-20 17:38:48 +00003744 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00003745 *
3746 * [27] charProp ::= IsCategory | IsBlock
3747 * [28] IsCategory ::= Letters | Marks | Numbers | Punctuation |
3748 * Separators | Symbols | Others
3749 * [29] Letters ::= 'L' [ultmo]?
3750 * [30] Marks ::= 'M' [nce]?
3751 * [31] Numbers ::= 'N' [dlo]?
3752 * [32] Punctuation ::= 'P' [cdseifo]?
3753 * [33] Separators ::= 'Z' [slp]?
3754 * [34] Symbols ::= 'S' [mcko]?
3755 * [35] Others ::= 'C' [cfon]?
3756 * [36] IsBlock ::= 'Is' [a-zA-Z0-9#x2D]+
3757 */
3758static void
3759xmlFAParseCharProp(xmlRegParserCtxtPtr ctxt) {
3760 int cur;
William M. Brack779af002003-08-01 15:55:39 +00003761 xmlRegAtomType type = (xmlRegAtomType) 0;
Daniel Veillard4255d502002-04-16 15:50:10 +00003762 xmlChar *blockName = NULL;
3763
3764 cur = CUR;
3765 if (cur == 'L') {
3766 NEXT;
3767 cur = CUR;
3768 if (cur == 'u') {
3769 NEXT;
3770 type = XML_REGEXP_LETTER_UPPERCASE;
3771 } else if (cur == 'l') {
3772 NEXT;
3773 type = XML_REGEXP_LETTER_LOWERCASE;
3774 } else if (cur == 't') {
3775 NEXT;
3776 type = XML_REGEXP_LETTER_TITLECASE;
3777 } else if (cur == 'm') {
3778 NEXT;
3779 type = XML_REGEXP_LETTER_MODIFIER;
3780 } else if (cur == 'o') {
3781 NEXT;
3782 type = XML_REGEXP_LETTER_OTHERS;
3783 } else {
3784 type = XML_REGEXP_LETTER;
3785 }
3786 } else if (cur == 'M') {
3787 NEXT;
3788 cur = CUR;
3789 if (cur == 'n') {
3790 NEXT;
3791 /* nonspacing */
3792 type = XML_REGEXP_MARK_NONSPACING;
3793 } else if (cur == 'c') {
3794 NEXT;
3795 /* spacing combining */
3796 type = XML_REGEXP_MARK_SPACECOMBINING;
3797 } else if (cur == 'e') {
3798 NEXT;
3799 /* enclosing */
3800 type = XML_REGEXP_MARK_ENCLOSING;
3801 } else {
3802 /* all marks */
3803 type = XML_REGEXP_MARK;
3804 }
3805 } else if (cur == 'N') {
3806 NEXT;
3807 cur = CUR;
3808 if (cur == 'd') {
3809 NEXT;
3810 /* digital */
3811 type = XML_REGEXP_NUMBER_DECIMAL;
3812 } else if (cur == 'l') {
3813 NEXT;
3814 /* letter */
3815 type = XML_REGEXP_NUMBER_LETTER;
3816 } else if (cur == 'o') {
3817 NEXT;
3818 /* other */
3819 type = XML_REGEXP_NUMBER_OTHERS;
3820 } else {
3821 /* all numbers */
3822 type = XML_REGEXP_NUMBER;
3823 }
3824 } else if (cur == 'P') {
3825 NEXT;
3826 cur = CUR;
3827 if (cur == 'c') {
3828 NEXT;
3829 /* connector */
3830 type = XML_REGEXP_PUNCT_CONNECTOR;
3831 } else if (cur == 'd') {
3832 NEXT;
3833 /* dash */
3834 type = XML_REGEXP_PUNCT_DASH;
3835 } else if (cur == 's') {
3836 NEXT;
3837 /* open */
3838 type = XML_REGEXP_PUNCT_OPEN;
3839 } else if (cur == 'e') {
3840 NEXT;
3841 /* close */
3842 type = XML_REGEXP_PUNCT_CLOSE;
3843 } else if (cur == 'i') {
3844 NEXT;
3845 /* initial quote */
3846 type = XML_REGEXP_PUNCT_INITQUOTE;
3847 } else if (cur == 'f') {
3848 NEXT;
3849 /* final quote */
3850 type = XML_REGEXP_PUNCT_FINQUOTE;
3851 } else if (cur == 'o') {
3852 NEXT;
3853 /* other */
3854 type = XML_REGEXP_PUNCT_OTHERS;
3855 } else {
3856 /* all punctuation */
3857 type = XML_REGEXP_PUNCT;
3858 }
3859 } else if (cur == 'Z') {
3860 NEXT;
3861 cur = CUR;
3862 if (cur == 's') {
3863 NEXT;
3864 /* space */
3865 type = XML_REGEXP_SEPAR_SPACE;
3866 } else if (cur == 'l') {
3867 NEXT;
3868 /* line */
3869 type = XML_REGEXP_SEPAR_LINE;
3870 } else if (cur == 'p') {
3871 NEXT;
3872 /* paragraph */
3873 type = XML_REGEXP_SEPAR_PARA;
3874 } else {
3875 /* all separators */
3876 type = XML_REGEXP_SEPAR;
3877 }
3878 } else if (cur == 'S') {
3879 NEXT;
3880 cur = CUR;
3881 if (cur == 'm') {
3882 NEXT;
3883 type = XML_REGEXP_SYMBOL_MATH;
3884 /* math */
3885 } else if (cur == 'c') {
3886 NEXT;
3887 type = XML_REGEXP_SYMBOL_CURRENCY;
3888 /* currency */
3889 } else if (cur == 'k') {
3890 NEXT;
3891 type = XML_REGEXP_SYMBOL_MODIFIER;
3892 /* modifiers */
3893 } else if (cur == 'o') {
3894 NEXT;
3895 type = XML_REGEXP_SYMBOL_OTHERS;
3896 /* other */
3897 } else {
3898 /* all symbols */
3899 type = XML_REGEXP_SYMBOL;
3900 }
3901 } else if (cur == 'C') {
3902 NEXT;
3903 cur = CUR;
3904 if (cur == 'c') {
3905 NEXT;
3906 /* control */
3907 type = XML_REGEXP_OTHER_CONTROL;
3908 } else if (cur == 'f') {
3909 NEXT;
3910 /* format */
3911 type = XML_REGEXP_OTHER_FORMAT;
3912 } else if (cur == 'o') {
3913 NEXT;
3914 /* private use */
3915 type = XML_REGEXP_OTHER_PRIVATE;
3916 } else if (cur == 'n') {
3917 NEXT;
3918 /* not assigned */
3919 type = XML_REGEXP_OTHER_NA;
3920 } else {
3921 /* all others */
3922 type = XML_REGEXP_OTHER;
3923 }
3924 } else if (cur == 'I') {
3925 const xmlChar *start;
3926 NEXT;
3927 cur = CUR;
3928 if (cur != 's') {
3929 ERROR("IsXXXX expected");
3930 return;
3931 }
3932 NEXT;
3933 start = ctxt->cur;
3934 cur = CUR;
3935 if (((cur >= 'a') && (cur <= 'z')) ||
3936 ((cur >= 'A') && (cur <= 'Z')) ||
3937 ((cur >= '0') && (cur <= '9')) ||
3938 (cur == 0x2D)) {
3939 NEXT;
3940 cur = CUR;
3941 while (((cur >= 'a') && (cur <= 'z')) ||
3942 ((cur >= 'A') && (cur <= 'Z')) ||
3943 ((cur >= '0') && (cur <= '9')) ||
3944 (cur == 0x2D)) {
3945 NEXT;
3946 cur = CUR;
3947 }
3948 }
3949 type = XML_REGEXP_BLOCK_NAME;
3950 blockName = xmlStrndup(start, ctxt->cur - start);
3951 } else {
3952 ERROR("Unknown char property");
3953 return;
3954 }
3955 if (ctxt->atom == NULL) {
3956 ctxt->atom = xmlRegNewAtom(ctxt, type);
3957 if (ctxt->atom != NULL)
3958 ctxt->atom->valuep = blockName;
3959 } else if (ctxt->atom->type == XML_REGEXP_RANGES) {
3960 xmlRegAtomAddRange(ctxt, ctxt->atom, ctxt->neg,
3961 type, 0, 0, blockName);
3962 }
3963}
3964
3965/**
3966 * xmlFAParseCharClassEsc:
Daniel Veillard441bc322002-04-20 17:38:48 +00003967 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00003968 *
3969 * [23] charClassEsc ::= ( SingleCharEsc | MultiCharEsc | catEsc | complEsc )
3970 * [24] SingleCharEsc ::= '\' [nrt\|.?*+(){}#x2D#x5B#x5D#x5E]
3971 * [25] catEsc ::= '\p{' charProp '}'
3972 * [26] complEsc ::= '\P{' charProp '}'
3973 * [37] MultiCharEsc ::= '.' | ('\' [sSiIcCdDwW])
3974 */
3975static void
3976xmlFAParseCharClassEsc(xmlRegParserCtxtPtr ctxt) {
3977 int cur;
3978
3979 if (CUR == '.') {
3980 if (ctxt->atom == NULL) {
3981 ctxt->atom = xmlRegNewAtom(ctxt, XML_REGEXP_ANYCHAR);
3982 } else if (ctxt->atom->type == XML_REGEXP_RANGES) {
3983 xmlRegAtomAddRange(ctxt, ctxt->atom, ctxt->neg,
3984 XML_REGEXP_ANYCHAR, 0, 0, NULL);
3985 }
3986 NEXT;
3987 return;
3988 }
3989 if (CUR != '\\') {
3990 ERROR("Escaped sequence: expecting \\");
3991 return;
3992 }
3993 NEXT;
3994 cur = CUR;
3995 if (cur == 'p') {
3996 NEXT;
3997 if (CUR != '{') {
3998 ERROR("Expecting '{'");
3999 return;
4000 }
4001 NEXT;
4002 xmlFAParseCharProp(ctxt);
4003 if (CUR != '}') {
4004 ERROR("Expecting '}'");
4005 return;
4006 }
4007 NEXT;
4008 } else if (cur == 'P') {
4009 NEXT;
4010 if (CUR != '{') {
4011 ERROR("Expecting '{'");
4012 return;
4013 }
4014 NEXT;
4015 xmlFAParseCharProp(ctxt);
4016 ctxt->atom->neg = 1;
4017 if (CUR != '}') {
4018 ERROR("Expecting '}'");
4019 return;
4020 }
4021 NEXT;
4022 } else if ((cur == 'n') || (cur == 'r') || (cur == 't') || (cur == '\\') ||
4023 (cur == '|') || (cur == '.') || (cur == '?') || (cur == '*') ||
4024 (cur == '+') || (cur == '(') || (cur == ')') || (cur == '{') ||
4025 (cur == '}') || (cur == 0x2D) || (cur == 0x5B) || (cur == 0x5D) ||
4026 (cur == 0x5E)) {
4027 if (ctxt->atom == NULL) {
4028 ctxt->atom = xmlRegNewAtom(ctxt, XML_REGEXP_CHARVAL);
Daniel Veillard99c394d2005-07-14 12:58:49 +00004029 if (ctxt->atom != NULL) {
4030 switch (cur) {
4031 case 'n':
4032 ctxt->atom->codepoint = '\n';
4033 break;
4034 case 'r':
4035 ctxt->atom->codepoint = '\r';
4036 break;
4037 case 't':
4038 ctxt->atom->codepoint = '\t';
4039 break;
4040 default:
4041 ctxt->atom->codepoint = cur;
4042 }
4043 }
Daniel Veillard4255d502002-04-16 15:50:10 +00004044 } else if (ctxt->atom->type == XML_REGEXP_RANGES) {
4045 xmlRegAtomAddRange(ctxt, ctxt->atom, ctxt->neg,
4046 XML_REGEXP_CHARVAL, cur, cur, NULL);
4047 }
4048 NEXT;
4049 } else if ((cur == 's') || (cur == 'S') || (cur == 'i') || (cur == 'I') ||
4050 (cur == 'c') || (cur == 'C') || (cur == 'd') || (cur == 'D') ||
4051 (cur == 'w') || (cur == 'W')) {
Daniel Veillardb509f152002-04-17 16:28:10 +00004052 xmlRegAtomType type = XML_REGEXP_ANYSPACE;
Daniel Veillard4255d502002-04-16 15:50:10 +00004053
4054 switch (cur) {
4055 case 's':
4056 type = XML_REGEXP_ANYSPACE;
4057 break;
4058 case 'S':
4059 type = XML_REGEXP_NOTSPACE;
4060 break;
4061 case 'i':
4062 type = XML_REGEXP_INITNAME;
4063 break;
4064 case 'I':
4065 type = XML_REGEXP_NOTINITNAME;
4066 break;
4067 case 'c':
4068 type = XML_REGEXP_NAMECHAR;
4069 break;
4070 case 'C':
4071 type = XML_REGEXP_NOTNAMECHAR;
4072 break;
4073 case 'd':
4074 type = XML_REGEXP_DECIMAL;
4075 break;
4076 case 'D':
4077 type = XML_REGEXP_NOTDECIMAL;
4078 break;
4079 case 'w':
4080 type = XML_REGEXP_REALCHAR;
4081 break;
4082 case 'W':
4083 type = XML_REGEXP_NOTREALCHAR;
4084 break;
4085 }
4086 NEXT;
4087 if (ctxt->atom == NULL) {
4088 ctxt->atom = xmlRegNewAtom(ctxt, type);
4089 } else if (ctxt->atom->type == XML_REGEXP_RANGES) {
4090 xmlRegAtomAddRange(ctxt, ctxt->atom, ctxt->neg,
4091 type, 0, 0, NULL);
4092 }
4093 }
4094}
4095
4096/**
4097 * xmlFAParseCharRef:
Daniel Veillard441bc322002-04-20 17:38:48 +00004098 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00004099 *
4100 * [19] XmlCharRef ::= ( '&#' [0-9]+ ';' ) | (' &#x' [0-9a-fA-F]+ ';' )
4101 */
4102static int
4103xmlFAParseCharRef(xmlRegParserCtxtPtr ctxt) {
4104 int ret = 0, cur;
4105
4106 if ((CUR != '&') || (NXT(1) != '#'))
4107 return(-1);
4108 NEXT;
4109 NEXT;
4110 cur = CUR;
4111 if (cur == 'x') {
4112 NEXT;
4113 cur = CUR;
4114 if (((cur >= '0') && (cur <= '9')) ||
4115 ((cur >= 'a') && (cur <= 'f')) ||
4116 ((cur >= 'A') && (cur <= 'F'))) {
4117 while (((cur >= '0') && (cur <= '9')) ||
4118 ((cur >= 'A') && (cur <= 'F'))) {
4119 if ((cur >= '0') && (cur <= '9'))
4120 ret = ret * 16 + cur - '0';
4121 else if ((cur >= 'a') && (cur <= 'f'))
4122 ret = ret * 16 + 10 + (cur - 'a');
4123 else
4124 ret = ret * 16 + 10 + (cur - 'A');
4125 NEXT;
4126 cur = CUR;
4127 }
4128 } else {
4129 ERROR("Char ref: expecting [0-9A-F]");
4130 return(-1);
4131 }
4132 } else {
4133 if ((cur >= '0') && (cur <= '9')) {
4134 while ((cur >= '0') && (cur <= '9')) {
4135 ret = ret * 10 + cur - '0';
4136 NEXT;
4137 cur = CUR;
4138 }
4139 } else {
4140 ERROR("Char ref: expecting [0-9]");
4141 return(-1);
4142 }
4143 }
4144 if (cur != ';') {
4145 ERROR("Char ref: expecting ';'");
4146 return(-1);
4147 } else {
4148 NEXT;
4149 }
4150 return(ret);
4151}
4152
4153/**
4154 * xmlFAParseCharRange:
Daniel Veillard441bc322002-04-20 17:38:48 +00004155 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00004156 *
4157 * [17] charRange ::= seRange | XmlCharRef | XmlCharIncDash
4158 * [18] seRange ::= charOrEsc '-' charOrEsc
4159 * [20] charOrEsc ::= XmlChar | SingleCharEsc
4160 * [21] XmlChar ::= [^\#x2D#x5B#x5D]
4161 * [22] XmlCharIncDash ::= [^\#x5B#x5D]
4162 */
4163static void
4164xmlFAParseCharRange(xmlRegParserCtxtPtr ctxt) {
William M. Brackdc99df92003-12-27 01:54:25 +00004165 int cur, len;
Daniel Veillard4255d502002-04-16 15:50:10 +00004166 int start = -1;
4167 int end = -1;
4168
4169 if ((CUR == '&') && (NXT(1) == '#')) {
4170 end = start = xmlFAParseCharRef(ctxt);
4171 xmlRegAtomAddRange(ctxt, ctxt->atom, ctxt->neg,
4172 XML_REGEXP_CHARVAL, start, end, NULL);
4173 return;
4174 }
4175 cur = CUR;
4176 if (cur == '\\') {
4177 NEXT;
4178 cur = CUR;
4179 switch (cur) {
4180 case 'n': start = 0xA; break;
4181 case 'r': start = 0xD; break;
4182 case 't': start = 0x9; break;
4183 case '\\': case '|': case '.': case '-': case '^': case '?':
4184 case '*': case '+': case '{': case '}': case '(': case ')':
4185 case '[': case ']':
4186 start = cur; break;
4187 default:
4188 ERROR("Invalid escape value");
4189 return;
4190 }
4191 end = start;
William M. Brackdc99df92003-12-27 01:54:25 +00004192 len = 1;
Daniel Veillard4255d502002-04-16 15:50:10 +00004193 } else if ((cur != 0x5B) && (cur != 0x5D)) {
William M. Brackdc99df92003-12-27 01:54:25 +00004194 end = start = CUR_SCHAR(ctxt->cur, len);
Daniel Veillard4255d502002-04-16 15:50:10 +00004195 } else {
4196 ERROR("Expecting a char range");
4197 return;
4198 }
William M. Brackdc99df92003-12-27 01:54:25 +00004199 NEXTL(len);
Daniel Veillard4255d502002-04-16 15:50:10 +00004200 if (start == '-') {
4201 return;
4202 }
4203 cur = CUR;
William M. Brack10f1ef42004-03-20 14:51:25 +00004204 if ((cur != '-') || (NXT(1) == ']')) {
Daniel Veillard4255d502002-04-16 15:50:10 +00004205 xmlRegAtomAddRange(ctxt, ctxt->atom, ctxt->neg,
4206 XML_REGEXP_CHARVAL, start, end, NULL);
4207 return;
4208 }
4209 NEXT;
4210 cur = CUR;
4211 if (cur == '\\') {
4212 NEXT;
4213 cur = CUR;
4214 switch (cur) {
4215 case 'n': end = 0xA; break;
4216 case 'r': end = 0xD; break;
4217 case 't': end = 0x9; break;
4218 case '\\': case '|': case '.': case '-': case '^': case '?':
4219 case '*': case '+': case '{': case '}': case '(': case ')':
4220 case '[': case ']':
4221 end = cur; break;
4222 default:
4223 ERROR("Invalid escape value");
4224 return;
4225 }
William M. Brackdc99df92003-12-27 01:54:25 +00004226 len = 1;
Daniel Veillard4255d502002-04-16 15:50:10 +00004227 } else if ((cur != 0x5B) && (cur != 0x5D)) {
William M. Brackdc99df92003-12-27 01:54:25 +00004228 end = CUR_SCHAR(ctxt->cur, len);
Daniel Veillard4255d502002-04-16 15:50:10 +00004229 } else {
4230 ERROR("Expecting the end of a char range");
4231 return;
4232 }
William M. Brackdc99df92003-12-27 01:54:25 +00004233 NEXTL(len);
Daniel Veillard4255d502002-04-16 15:50:10 +00004234 /* TODO check that the values are acceptable character ranges for XML */
4235 if (end < start) {
4236 ERROR("End of range is before start of range");
4237 } else {
4238 xmlRegAtomAddRange(ctxt, ctxt->atom, ctxt->neg,
4239 XML_REGEXP_CHARVAL, start, end, NULL);
4240 }
4241 return;
4242}
4243
4244/**
4245 * xmlFAParsePosCharGroup:
Daniel Veillard441bc322002-04-20 17:38:48 +00004246 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00004247 *
4248 * [14] posCharGroup ::= ( charRange | charClassEsc )+
4249 */
4250static void
4251xmlFAParsePosCharGroup(xmlRegParserCtxtPtr ctxt) {
4252 do {
4253 if ((CUR == '\\') || (CUR == '.')) {
4254 xmlFAParseCharClassEsc(ctxt);
4255 } else {
4256 xmlFAParseCharRange(ctxt);
4257 }
4258 } while ((CUR != ']') && (CUR != '^') && (CUR != '-') &&
4259 (ctxt->error == 0));
4260}
4261
4262/**
4263 * xmlFAParseCharGroup:
Daniel Veillard441bc322002-04-20 17:38:48 +00004264 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00004265 *
4266 * [13] charGroup ::= posCharGroup | negCharGroup | charClassSub
4267 * [15] negCharGroup ::= '^' posCharGroup
4268 * [16] charClassSub ::= ( posCharGroup | negCharGroup ) '-' charClassExpr
4269 * [12] charClassExpr ::= '[' charGroup ']'
4270 */
4271static void
4272xmlFAParseCharGroup(xmlRegParserCtxtPtr ctxt) {
4273 int n = ctxt->neg;
4274 while ((CUR != ']') && (ctxt->error == 0)) {
4275 if (CUR == '^') {
4276 int neg = ctxt->neg;
4277
4278 NEXT;
4279 ctxt->neg = !ctxt->neg;
4280 xmlFAParsePosCharGroup(ctxt);
4281 ctxt->neg = neg;
William M. Brack10f1ef42004-03-20 14:51:25 +00004282 } else if ((CUR == '-') && (NXT(1) == '[')) {
Daniel Veillardf8b9de32003-11-24 14:27:26 +00004283 int neg = ctxt->neg;
Daniel Veillardf8b9de32003-11-24 14:27:26 +00004284 ctxt->neg = 2;
William M. Brack10f1ef42004-03-20 14:51:25 +00004285 NEXT; /* eat the '-' */
4286 NEXT; /* eat the '[' */
Daniel Veillard4255d502002-04-16 15:50:10 +00004287 xmlFAParseCharGroup(ctxt);
4288 if (CUR == ']') {
4289 NEXT;
4290 } else {
4291 ERROR("charClassExpr: ']' expected");
4292 break;
4293 }
Daniel Veillardf8b9de32003-11-24 14:27:26 +00004294 ctxt->neg = neg;
Daniel Veillard4255d502002-04-16 15:50:10 +00004295 break;
4296 } else if (CUR != ']') {
4297 xmlFAParsePosCharGroup(ctxt);
4298 }
4299 }
4300 ctxt->neg = n;
4301}
4302
4303/**
4304 * xmlFAParseCharClass:
Daniel Veillard441bc322002-04-20 17:38:48 +00004305 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00004306 *
4307 * [11] charClass ::= charClassEsc | charClassExpr
4308 * [12] charClassExpr ::= '[' charGroup ']'
4309 */
4310static void
4311xmlFAParseCharClass(xmlRegParserCtxtPtr ctxt) {
4312 if (CUR == '[') {
4313 NEXT;
4314 ctxt->atom = xmlRegNewAtom(ctxt, XML_REGEXP_RANGES);
4315 if (ctxt->atom == NULL)
4316 return;
4317 xmlFAParseCharGroup(ctxt);
4318 if (CUR == ']') {
4319 NEXT;
4320 } else {
4321 ERROR("xmlFAParseCharClass: ']' expected");
4322 }
4323 } else {
4324 xmlFAParseCharClassEsc(ctxt);
4325 }
4326}
4327
4328/**
4329 * xmlFAParseQuantExact:
Daniel Veillard441bc322002-04-20 17:38:48 +00004330 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00004331 *
4332 * [8] QuantExact ::= [0-9]+
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00004333 *
4334 * Returns 0 if success or -1 in case of error
Daniel Veillard4255d502002-04-16 15:50:10 +00004335 */
4336static int
4337xmlFAParseQuantExact(xmlRegParserCtxtPtr ctxt) {
4338 int ret = 0;
4339 int ok = 0;
4340
4341 while ((CUR >= '0') && (CUR <= '9')) {
4342 ret = ret * 10 + (CUR - '0');
4343 ok = 1;
4344 NEXT;
4345 }
4346 if (ok != 1) {
4347 return(-1);
4348 }
4349 return(ret);
4350}
4351
4352/**
4353 * xmlFAParseQuantifier:
Daniel Veillard441bc322002-04-20 17:38:48 +00004354 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00004355 *
4356 * [4] quantifier ::= [?*+] | ( '{' quantity '}' )
4357 * [5] quantity ::= quantRange | quantMin | QuantExact
4358 * [6] quantRange ::= QuantExact ',' QuantExact
4359 * [7] quantMin ::= QuantExact ','
4360 * [8] QuantExact ::= [0-9]+
4361 */
4362static int
4363xmlFAParseQuantifier(xmlRegParserCtxtPtr ctxt) {
4364 int cur;
4365
4366 cur = CUR;
4367 if ((cur == '?') || (cur == '*') || (cur == '+')) {
4368 if (ctxt->atom != NULL) {
4369 if (cur == '?')
4370 ctxt->atom->quant = XML_REGEXP_QUANT_OPT;
4371 else if (cur == '*')
4372 ctxt->atom->quant = XML_REGEXP_QUANT_MULT;
4373 else if (cur == '+')
4374 ctxt->atom->quant = XML_REGEXP_QUANT_PLUS;
4375 }
4376 NEXT;
4377 return(1);
4378 }
4379 if (cur == '{') {
4380 int min = 0, max = 0;
4381
4382 NEXT;
4383 cur = xmlFAParseQuantExact(ctxt);
4384 if (cur >= 0)
4385 min = cur;
4386 if (CUR == ',') {
4387 NEXT;
Daniel Veillardebe48c62003-12-03 12:12:27 +00004388 if (CUR == '}')
4389 max = INT_MAX;
4390 else {
4391 cur = xmlFAParseQuantExact(ctxt);
4392 if (cur >= 0)
4393 max = cur;
4394 else {
4395 ERROR("Improper quantifier");
4396 }
4397 }
Daniel Veillard4255d502002-04-16 15:50:10 +00004398 }
4399 if (CUR == '}') {
4400 NEXT;
4401 } else {
4402 ERROR("Unterminated quantifier");
4403 }
4404 if (max == 0)
4405 max = min;
4406 if (ctxt->atom != NULL) {
4407 ctxt->atom->quant = XML_REGEXP_QUANT_RANGE;
4408 ctxt->atom->min = min;
4409 ctxt->atom->max = max;
4410 }
4411 return(1);
4412 }
4413 return(0);
4414}
4415
4416/**
4417 * xmlFAParseAtom:
Daniel Veillard441bc322002-04-20 17:38:48 +00004418 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00004419 *
4420 * [9] atom ::= Char | charClass | ( '(' regExp ')' )
4421 */
4422static int
4423xmlFAParseAtom(xmlRegParserCtxtPtr ctxt) {
4424 int codepoint, len;
4425
4426 codepoint = xmlFAIsChar(ctxt);
4427 if (codepoint > 0) {
4428 ctxt->atom = xmlRegNewAtom(ctxt, XML_REGEXP_CHARVAL);
4429 if (ctxt->atom == NULL)
4430 return(-1);
4431 codepoint = CUR_SCHAR(ctxt->cur, len);
4432 ctxt->atom->codepoint = codepoint;
4433 NEXTL(len);
4434 return(1);
4435 } else if (CUR == '|') {
4436 return(0);
4437 } else if (CUR == 0) {
4438 return(0);
4439 } else if (CUR == ')') {
4440 return(0);
4441 } else if (CUR == '(') {
4442 xmlRegStatePtr start, oldend;
4443
4444 NEXT;
4445 xmlFAGenerateEpsilonTransition(ctxt, ctxt->state, NULL);
4446 start = ctxt->state;
4447 oldend = ctxt->end;
4448 ctxt->end = NULL;
4449 ctxt->atom = NULL;
4450 xmlFAParseRegExp(ctxt, 0);
4451 if (CUR == ')') {
4452 NEXT;
4453 } else {
4454 ERROR("xmlFAParseAtom: expecting ')'");
4455 }
4456 ctxt->atom = xmlRegNewAtom(ctxt, XML_REGEXP_SUBREG);
4457 if (ctxt->atom == NULL)
4458 return(-1);
4459 ctxt->atom->start = start;
4460 ctxt->atom->stop = ctxt->state;
4461 ctxt->end = oldend;
4462 return(1);
4463 } else if ((CUR == '[') || (CUR == '\\') || (CUR == '.')) {
4464 xmlFAParseCharClass(ctxt);
4465 return(1);
4466 }
4467 return(0);
4468}
4469
4470/**
4471 * xmlFAParsePiece:
Daniel Veillard441bc322002-04-20 17:38:48 +00004472 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00004473 *
4474 * [3] piece ::= atom quantifier?
4475 */
4476static int
4477xmlFAParsePiece(xmlRegParserCtxtPtr ctxt) {
4478 int ret;
4479
4480 ctxt->atom = NULL;
4481 ret = xmlFAParseAtom(ctxt);
4482 if (ret == 0)
4483 return(0);
4484 if (ctxt->atom == NULL) {
4485 ERROR("internal: no atom generated");
4486 }
4487 xmlFAParseQuantifier(ctxt);
4488 return(1);
4489}
4490
4491/**
4492 * xmlFAParseBranch:
Daniel Veillard441bc322002-04-20 17:38:48 +00004493 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00004494 *
4495 * [2] branch ::= piece*
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00004496 8
Daniel Veillard4255d502002-04-16 15:50:10 +00004497 */
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00004498static int
Daniel Veillard2cbf5962004-03-31 15:50:43 +00004499xmlFAParseBranch(xmlRegParserCtxtPtr ctxt) {
Daniel Veillard4255d502002-04-16 15:50:10 +00004500 xmlRegStatePtr previous;
Daniel Veillard4255d502002-04-16 15:50:10 +00004501 int ret;
4502
4503 previous = ctxt->state;
4504 ret = xmlFAParsePiece(ctxt);
4505 if (ret != 0) {
Daniel Veillard2cbf5962004-03-31 15:50:43 +00004506 if (xmlFAGenerateTransitions(ctxt, previous, NULL, ctxt->atom) < 0)
4507 return(-1);
4508 previous = ctxt->state;
Daniel Veillard4255d502002-04-16 15:50:10 +00004509 ctxt->atom = NULL;
4510 }
4511 while ((ret != 0) && (ctxt->error == 0)) {
4512 ret = xmlFAParsePiece(ctxt);
4513 if (ret != 0) {
Daniel Veillard2cbf5962004-03-31 15:50:43 +00004514 if (xmlFAGenerateTransitions(ctxt, previous, NULL,
4515 ctxt->atom) < 0)
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00004516 return(-1);
Daniel Veillard4255d502002-04-16 15:50:10 +00004517 previous = ctxt->state;
4518 ctxt->atom = NULL;
4519 }
4520 }
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00004521 return(0);
Daniel Veillard4255d502002-04-16 15:50:10 +00004522}
4523
4524/**
4525 * xmlFAParseRegExp:
Daniel Veillard441bc322002-04-20 17:38:48 +00004526 * @ctxt: a regexp parser context
William M. Brackddf71d62004-05-06 04:17:26 +00004527 * @top: is this the top-level expression ?
Daniel Veillard4255d502002-04-16 15:50:10 +00004528 *
4529 * [1] regExp ::= branch ( '|' branch )*
4530 */
4531static void
4532xmlFAParseRegExp(xmlRegParserCtxtPtr ctxt, int top) {
Daniel Veillardc7e3cc42004-09-28 12:33:52 +00004533 xmlRegStatePtr start, end;
Daniel Veillard4255d502002-04-16 15:50:10 +00004534
Daniel Veillard2cbf5962004-03-31 15:50:43 +00004535 /* if not top start should have been generated by an epsilon trans */
Daniel Veillard4255d502002-04-16 15:50:10 +00004536 start = ctxt->state;
Daniel Veillard2cbf5962004-03-31 15:50:43 +00004537 ctxt->end = NULL;
4538 xmlFAParseBranch(ctxt);
4539 if (top) {
4540#ifdef DEBUG_REGEXP_GRAPH
4541 printf("State %d is final\n", ctxt->state->no);
4542#endif
4543 ctxt->state->type = XML_REGEXP_FINAL_STATE;
4544 }
Daniel Veillard4255d502002-04-16 15:50:10 +00004545 if (CUR != '|') {
4546 ctxt->end = ctxt->state;
4547 return;
4548 }
4549 end = ctxt->state;
4550 while ((CUR == '|') && (ctxt->error == 0)) {
4551 NEXT;
4552 ctxt->state = start;
Daniel Veillard2cbf5962004-03-31 15:50:43 +00004553 ctxt->end = NULL;
4554 xmlFAParseBranch(ctxt);
4555 if (top) {
4556 ctxt->state->type = XML_REGEXP_FINAL_STATE;
4557#ifdef DEBUG_REGEXP_GRAPH
4558 printf("State %d is final\n", ctxt->state->no);
4559#endif
4560 } else {
4561 xmlFAGenerateEpsilonTransition(ctxt, ctxt->state, end);
4562 }
Daniel Veillard4255d502002-04-16 15:50:10 +00004563 }
Daniel Veillard2cbf5962004-03-31 15:50:43 +00004564 if (!top) {
4565 ctxt->state = end;
4566 ctxt->end = end;
4567 }
Daniel Veillard4255d502002-04-16 15:50:10 +00004568}
4569
4570/************************************************************************
4571 * *
4572 * The basic API *
4573 * *
4574 ************************************************************************/
4575
4576/**
4577 * xmlRegexpPrint:
4578 * @output: the file for the output debug
4579 * @regexp: the compiled regexp
4580 *
4581 * Print the content of the compiled regular expression
4582 */
4583void
4584xmlRegexpPrint(FILE *output, xmlRegexpPtr regexp) {
4585 int i;
4586
Daniel Veillarda82b1822004-11-08 16:24:57 +00004587 if (output == NULL)
4588 return;
Daniel Veillard4255d502002-04-16 15:50:10 +00004589 fprintf(output, " regexp: ");
4590 if (regexp == NULL) {
4591 fprintf(output, "NULL\n");
4592 return;
4593 }
4594 fprintf(output, "'%s' ", regexp->string);
4595 fprintf(output, "\n");
4596 fprintf(output, "%d atoms:\n", regexp->nbAtoms);
4597 for (i = 0;i < regexp->nbAtoms; i++) {
4598 fprintf(output, " %02d ", i);
4599 xmlRegPrintAtom(output, regexp->atoms[i]);
4600 }
4601 fprintf(output, "%d states:", regexp->nbStates);
4602 fprintf(output, "\n");
4603 for (i = 0;i < regexp->nbStates; i++) {
4604 xmlRegPrintState(output, regexp->states[i]);
4605 }
4606 fprintf(output, "%d counters:\n", regexp->nbCounters);
4607 for (i = 0;i < regexp->nbCounters; i++) {
4608 fprintf(output, " %d: min %d max %d\n", i, regexp->counters[i].min,
4609 regexp->counters[i].max);
4610 }
4611}
4612
4613/**
4614 * xmlRegexpCompile:
4615 * @regexp: a regular expression string
4616 *
4617 * Parses a regular expression conforming to XML Schemas Part 2 Datatype
William M. Brackddf71d62004-05-06 04:17:26 +00004618 * Appendix F and builds an automata suitable for testing strings against
Daniel Veillard4255d502002-04-16 15:50:10 +00004619 * that regular expression
4620 *
4621 * Returns the compiled expression or NULL in case of error
4622 */
4623xmlRegexpPtr
4624xmlRegexpCompile(const xmlChar *regexp) {
4625 xmlRegexpPtr ret;
4626 xmlRegParserCtxtPtr ctxt;
4627
4628 ctxt = xmlRegNewParserCtxt(regexp);
4629 if (ctxt == NULL)
4630 return(NULL);
4631
4632 /* initialize the parser */
4633 ctxt->end = NULL;
4634 ctxt->start = ctxt->state = xmlRegNewState(ctxt);
4635 xmlRegStatePush(ctxt, ctxt->start);
4636
4637 /* parse the expression building an automata */
4638 xmlFAParseRegExp(ctxt, 1);
4639 if (CUR != 0) {
4640 ERROR("xmlFAParseRegExp: extra characters");
4641 }
4642 ctxt->end = ctxt->state;
4643 ctxt->start->type = XML_REGEXP_START_STATE;
4644 ctxt->end->type = XML_REGEXP_FINAL_STATE;
4645
4646 /* remove the Epsilon except for counted transitions */
4647 xmlFAEliminateEpsilonTransitions(ctxt);
4648
4649
4650 if (ctxt->error != 0) {
4651 xmlRegFreeParserCtxt(ctxt);
4652 return(NULL);
4653 }
4654 ret = xmlRegEpxFromParse(ctxt);
4655 xmlRegFreeParserCtxt(ctxt);
4656 return(ret);
4657}
4658
4659/**
4660 * xmlRegexpExec:
4661 * @comp: the compiled regular expression
4662 * @content: the value to check against the regular expression
4663 *
William M. Brackddf71d62004-05-06 04:17:26 +00004664 * Check if the regular expression generates the value
Daniel Veillard4255d502002-04-16 15:50:10 +00004665 *
William M. Brackddf71d62004-05-06 04:17:26 +00004666 * Returns 1 if it matches, 0 if not and a negative value in case of error
Daniel Veillard4255d502002-04-16 15:50:10 +00004667 */
4668int
4669xmlRegexpExec(xmlRegexpPtr comp, const xmlChar *content) {
4670 if ((comp == NULL) || (content == NULL))
4671 return(-1);
4672 return(xmlFARegExec(comp, content));
4673}
4674
4675/**
Daniel Veillard23e73572002-09-19 19:56:43 +00004676 * xmlRegexpIsDeterminist:
4677 * @comp: the compiled regular expression
4678 *
4679 * Check if the regular expression is determinist
4680 *
William M. Brackddf71d62004-05-06 04:17:26 +00004681 * Returns 1 if it yes, 0 if not and a negative value in case of error
Daniel Veillard23e73572002-09-19 19:56:43 +00004682 */
4683int
4684xmlRegexpIsDeterminist(xmlRegexpPtr comp) {
4685 xmlAutomataPtr am;
4686 int ret;
4687
4688 if (comp == NULL)
4689 return(-1);
4690 if (comp->determinist != -1)
4691 return(comp->determinist);
4692
4693 am = xmlNewAutomata();
Daniel Veillardbd9afb52002-09-25 22:25:35 +00004694 if (am->states != NULL) {
4695 int i;
4696
4697 for (i = 0;i < am->nbStates;i++)
4698 xmlRegFreeState(am->states[i]);
4699 xmlFree(am->states);
4700 }
Daniel Veillard23e73572002-09-19 19:56:43 +00004701 am->nbAtoms = comp->nbAtoms;
4702 am->atoms = comp->atoms;
4703 am->nbStates = comp->nbStates;
4704 am->states = comp->states;
4705 am->determinist = -1;
4706 ret = xmlFAComputesDeterminism(am);
4707 am->atoms = NULL;
4708 am->states = NULL;
4709 xmlFreeAutomata(am);
4710 return(ret);
4711}
4712
4713/**
Daniel Veillard4255d502002-04-16 15:50:10 +00004714 * xmlRegFreeRegexp:
4715 * @regexp: the regexp
4716 *
4717 * Free a regexp
4718 */
4719void
4720xmlRegFreeRegexp(xmlRegexpPtr regexp) {
4721 int i;
4722 if (regexp == NULL)
4723 return;
4724
4725 if (regexp->string != NULL)
4726 xmlFree(regexp->string);
4727 if (regexp->states != NULL) {
4728 for (i = 0;i < regexp->nbStates;i++)
4729 xmlRegFreeState(regexp->states[i]);
4730 xmlFree(regexp->states);
4731 }
4732 if (regexp->atoms != NULL) {
4733 for (i = 0;i < regexp->nbAtoms;i++)
4734 xmlRegFreeAtom(regexp->atoms[i]);
4735 xmlFree(regexp->atoms);
4736 }
4737 if (regexp->counters != NULL)
4738 xmlFree(regexp->counters);
Daniel Veillard23e73572002-09-19 19:56:43 +00004739 if (regexp->compact != NULL)
4740 xmlFree(regexp->compact);
Daniel Veillard118aed72002-09-24 14:13:13 +00004741 if (regexp->transdata != NULL)
4742 xmlFree(regexp->transdata);
Daniel Veillard23e73572002-09-19 19:56:43 +00004743 if (regexp->stringMap != NULL) {
4744 for (i = 0; i < regexp->nbstrings;i++)
4745 xmlFree(regexp->stringMap[i]);
4746 xmlFree(regexp->stringMap);
4747 }
4748
Daniel Veillard4255d502002-04-16 15:50:10 +00004749 xmlFree(regexp);
4750}
4751
4752#ifdef LIBXML_AUTOMATA_ENABLED
4753/************************************************************************
4754 * *
4755 * The Automata interface *
4756 * *
4757 ************************************************************************/
4758
4759/**
4760 * xmlNewAutomata:
4761 *
4762 * Create a new automata
4763 *
4764 * Returns the new object or NULL in case of failure
4765 */
4766xmlAutomataPtr
4767xmlNewAutomata(void) {
4768 xmlAutomataPtr ctxt;
4769
4770 ctxt = xmlRegNewParserCtxt(NULL);
4771 if (ctxt == NULL)
4772 return(NULL);
4773
4774 /* initialize the parser */
4775 ctxt->end = NULL;
4776 ctxt->start = ctxt->state = xmlRegNewState(ctxt);
Daniel Veillarddb68b742005-07-30 13:18:24 +00004777 ctxt->start->type = XML_REGEXP_START_STATE;
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00004778 if (ctxt->start == NULL) {
4779 xmlFreeAutomata(ctxt);
4780 return(NULL);
4781 }
4782 if (xmlRegStatePush(ctxt, ctxt->start) < 0) {
4783 xmlRegFreeState(ctxt->start);
4784 xmlFreeAutomata(ctxt);
4785 return(NULL);
4786 }
Daniel Veillard4255d502002-04-16 15:50:10 +00004787
4788 return(ctxt);
4789}
4790
4791/**
4792 * xmlFreeAutomata:
4793 * @am: an automata
4794 *
4795 * Free an automata
4796 */
4797void
4798xmlFreeAutomata(xmlAutomataPtr am) {
4799 if (am == NULL)
4800 return;
4801 xmlRegFreeParserCtxt(am);
4802}
4803
4804/**
4805 * xmlAutomataGetInitState:
4806 * @am: an automata
4807 *
Daniel Veillarda9b66d02002-12-11 14:23:49 +00004808 * Initial state lookup
4809 *
Daniel Veillard4255d502002-04-16 15:50:10 +00004810 * Returns the initial state of the automata
4811 */
4812xmlAutomataStatePtr
4813xmlAutomataGetInitState(xmlAutomataPtr am) {
4814 if (am == NULL)
4815 return(NULL);
4816 return(am->start);
4817}
4818
4819/**
4820 * xmlAutomataSetFinalState:
4821 * @am: an automata
4822 * @state: a state in this automata
4823 *
4824 * Makes that state a final state
4825 *
4826 * Returns 0 or -1 in case of error
4827 */
4828int
4829xmlAutomataSetFinalState(xmlAutomataPtr am, xmlAutomataStatePtr state) {
4830 if ((am == NULL) || (state == NULL))
4831 return(-1);
4832 state->type = XML_REGEXP_FINAL_STATE;
4833 return(0);
4834}
4835
4836/**
4837 * xmlAutomataNewTransition:
4838 * @am: an automata
4839 * @from: the starting point of the transition
4840 * @to: the target point of the transition or NULL
4841 * @token: the input string associated to that transition
4842 * @data: data passed to the callback function if the transition is activated
4843 *
William M. Brackddf71d62004-05-06 04:17:26 +00004844 * If @to is NULL, this creates first a new target state in the automata
Daniel Veillard4255d502002-04-16 15:50:10 +00004845 * and then adds a transition from the @from state to the target state
4846 * activated by the value of @token
4847 *
4848 * Returns the target state or NULL in case of error
4849 */
4850xmlAutomataStatePtr
4851xmlAutomataNewTransition(xmlAutomataPtr am, xmlAutomataStatePtr from,
4852 xmlAutomataStatePtr to, const xmlChar *token,
4853 void *data) {
4854 xmlRegAtomPtr atom;
4855
4856 if ((am == NULL) || (from == NULL) || (token == NULL))
4857 return(NULL);
4858 atom = xmlRegNewAtom(am, XML_REGEXP_STRING);
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00004859 if (atom == NULL)
4860 return(NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +00004861 atom->data = data;
4862 if (atom == NULL)
4863 return(NULL);
4864 atom->valuep = xmlStrdup(token);
4865
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00004866 if (xmlFAGenerateTransitions(am, from, to, atom) < 0) {
4867 xmlRegFreeAtom(atom);
4868 return(NULL);
4869 }
Daniel Veillard4255d502002-04-16 15:50:10 +00004870 if (to == NULL)
4871 return(am->state);
4872 return(to);
4873}
4874
4875/**
Daniel Veillard52b48c72003-04-13 19:53:42 +00004876 * xmlAutomataNewTransition2:
4877 * @am: an automata
4878 * @from: the starting point of the transition
4879 * @to: the target point of the transition or NULL
4880 * @token: the first input string associated to that transition
4881 * @token2: the second input string associated to that transition
4882 * @data: data passed to the callback function if the transition is activated
4883 *
William M. Brackddf71d62004-05-06 04:17:26 +00004884 * If @to is NULL, this creates first a new target state in the automata
Daniel Veillard52b48c72003-04-13 19:53:42 +00004885 * and then adds a transition from the @from state to the target state
4886 * activated by the value of @token
4887 *
4888 * Returns the target state or NULL in case of error
4889 */
4890xmlAutomataStatePtr
4891xmlAutomataNewTransition2(xmlAutomataPtr am, xmlAutomataStatePtr from,
4892 xmlAutomataStatePtr to, const xmlChar *token,
4893 const xmlChar *token2, void *data) {
4894 xmlRegAtomPtr atom;
4895
4896 if ((am == NULL) || (from == NULL) || (token == NULL))
4897 return(NULL);
4898 atom = xmlRegNewAtom(am, XML_REGEXP_STRING);
4899 atom->data = data;
4900 if (atom == NULL)
4901 return(NULL);
4902 if ((token2 == NULL) || (*token2 == 0)) {
4903 atom->valuep = xmlStrdup(token);
4904 } else {
4905 int lenn, lenp;
4906 xmlChar *str;
4907
4908 lenn = strlen((char *) token2);
4909 lenp = strlen((char *) token);
4910
Daniel Veillard3c908dc2003-04-19 00:07:51 +00004911 str = (xmlChar *) xmlMallocAtomic(lenn + lenp + 2);
Daniel Veillard52b48c72003-04-13 19:53:42 +00004912 if (str == NULL) {
4913 xmlRegFreeAtom(atom);
4914 return(NULL);
4915 }
4916 memcpy(&str[0], token, lenp);
4917 str[lenp] = '|';
4918 memcpy(&str[lenp + 1], token2, lenn);
4919 str[lenn + lenp + 1] = 0;
4920
4921 atom->valuep = str;
4922 }
4923
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00004924 if (xmlFAGenerateTransitions(am, from, to, atom) < 0) {
4925 xmlRegFreeAtom(atom);
4926 return(NULL);
4927 }
Daniel Veillard52b48c72003-04-13 19:53:42 +00004928 if (to == NULL)
4929 return(am->state);
4930 return(to);
4931}
4932
4933/**
Daniel Veillard9efc4762005-07-19 14:33:55 +00004934 * xmlAutomataNewNegTrans:
4935 * @am: an automata
4936 * @from: the starting point of the transition
4937 * @to: the target point of the transition or NULL
4938 * @token: the first input string associated to that transition
4939 * @token2: the second input string associated to that transition
4940 * @data: data passed to the callback function if the transition is activated
4941 *
4942 * If @to is NULL, this creates first a new target state in the automata
4943 * and then adds a transition from the @from state to the target state
4944 * activated by any value except (@token,@token2)
Daniel Veillard6e65e152005-08-09 11:09:52 +00004945 * Note that if @token2 is not NULL, then (X, NULL) won't match to follow
4946 # the semantic of XSD ##other
Daniel Veillard9efc4762005-07-19 14:33:55 +00004947 *
4948 * Returns the target state or NULL in case of error
4949 */
4950xmlAutomataStatePtr
4951xmlAutomataNewNegTrans(xmlAutomataPtr am, xmlAutomataStatePtr from,
4952 xmlAutomataStatePtr to, const xmlChar *token,
4953 const xmlChar *token2, void *data) {
4954 xmlRegAtomPtr atom;
Daniel Veillard77005e62005-07-19 16:26:18 +00004955 xmlChar err_msg[200];
Daniel Veillard9efc4762005-07-19 14:33:55 +00004956
4957 if ((am == NULL) || (from == NULL) || (token == NULL))
4958 return(NULL);
4959 atom = xmlRegNewAtom(am, XML_REGEXP_STRING);
4960 if (atom == NULL)
4961 return(NULL);
4962 atom->data = data;
4963 atom->neg = 1;
4964 if ((token2 == NULL) || (*token2 == 0)) {
4965 atom->valuep = xmlStrdup(token);
4966 } else {
4967 int lenn, lenp;
4968 xmlChar *str;
4969
4970 lenn = strlen((char *) token2);
4971 lenp = strlen((char *) token);
4972
4973 str = (xmlChar *) xmlMallocAtomic(lenn + lenp + 2);
4974 if (str == NULL) {
4975 xmlRegFreeAtom(atom);
4976 return(NULL);
4977 }
4978 memcpy(&str[0], token, lenp);
4979 str[lenp] = '|';
4980 memcpy(&str[lenp + 1], token2, lenn);
4981 str[lenn + lenp + 1] = 0;
4982
4983 atom->valuep = str;
4984 }
Daniel Veillarddb68b742005-07-30 13:18:24 +00004985 snprintf((char *) err_msg, 199, "not %s", (const char *) atom->valuep);
Daniel Veillard77005e62005-07-19 16:26:18 +00004986 err_msg[199] = 0;
4987 atom->valuep2 = xmlStrdup(err_msg);
Daniel Veillard9efc4762005-07-19 14:33:55 +00004988
4989 if (xmlFAGenerateTransitions(am, from, to, atom) < 0) {
4990 xmlRegFreeAtom(atom);
4991 return(NULL);
4992 }
Daniel Veillard6e65e152005-08-09 11:09:52 +00004993 am->negs++;
Daniel Veillard9efc4762005-07-19 14:33:55 +00004994 if (to == NULL)
4995 return(am->state);
4996 return(to);
4997}
4998
4999/**
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00005000 * xmlAutomataNewCountTrans2:
5001 * @am: an automata
5002 * @from: the starting point of the transition
5003 * @to: the target point of the transition or NULL
5004 * @token: the input string associated to that transition
5005 * @token2: the second input string associated to that transition
5006 * @min: the minimum successive occurences of token
5007 * @max: the maximum successive occurences of token
5008 * @data: data associated to the transition
5009 *
5010 * If @to is NULL, this creates first a new target state in the automata
5011 * and then adds a transition from the @from state to the target state
5012 * activated by a succession of input of value @token and @token2 and
5013 * whose number is between @min and @max
5014 *
5015 * Returns the target state or NULL in case of error
5016 */
5017xmlAutomataStatePtr
5018xmlAutomataNewCountTrans2(xmlAutomataPtr am, xmlAutomataStatePtr from,
5019 xmlAutomataStatePtr to, const xmlChar *token,
5020 const xmlChar *token2,
5021 int min, int max, void *data) {
5022 xmlRegAtomPtr atom;
5023 int counter;
5024
5025 if ((am == NULL) || (from == NULL) || (token == NULL))
5026 return(NULL);
5027 if (min < 0)
5028 return(NULL);
5029 if ((max < min) || (max < 1))
5030 return(NULL);
5031 atom = xmlRegNewAtom(am, XML_REGEXP_STRING);
5032 if (atom == NULL)
5033 return(NULL);
5034 if ((token2 == NULL) || (*token2 == 0)) {
5035 atom->valuep = xmlStrdup(token);
5036 } else {
5037 int lenn, lenp;
5038 xmlChar *str;
5039
5040 lenn = strlen((char *) token2);
5041 lenp = strlen((char *) token);
5042
5043 str = (xmlChar *) xmlMallocAtomic(lenn + lenp + 2);
5044 if (str == NULL) {
5045 xmlRegFreeAtom(atom);
5046 return(NULL);
5047 }
5048 memcpy(&str[0], token, lenp);
5049 str[lenp] = '|';
5050 memcpy(&str[lenp + 1], token2, lenn);
5051 str[lenn + lenp + 1] = 0;
5052
5053 atom->valuep = str;
5054 }
5055 atom->data = data;
5056 if (min == 0)
5057 atom->min = 1;
5058 else
5059 atom->min = min;
5060 atom->max = max;
5061
5062 /*
5063 * associate a counter to the transition.
5064 */
5065 counter = xmlRegGetCounter(am);
5066 am->counters[counter].min = min;
5067 am->counters[counter].max = max;
5068
5069 /* xmlFAGenerateTransitions(am, from, to, atom); */
5070 if (to == NULL) {
5071 to = xmlRegNewState(am);
5072 xmlRegStatePush(am, to);
5073 }
Daniel Veillarddb68b742005-07-30 13:18:24 +00005074 xmlRegStateAddTrans(am, from, atom, to, counter, -1, 0);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00005075 xmlRegAtomPush(am, atom);
5076 am->state = to;
5077
5078 if (to == NULL)
5079 to = am->state;
5080 if (to == NULL)
5081 return(NULL);
5082 if (min == 0)
5083 xmlFAGenerateEpsilonTransition(am, from, to);
5084 return(to);
5085}
5086
5087/**
Daniel Veillard4255d502002-04-16 15:50:10 +00005088 * xmlAutomataNewCountTrans:
5089 * @am: an automata
5090 * @from: the starting point of the transition
5091 * @to: the target point of the transition or NULL
5092 * @token: the input string associated to that transition
5093 * @min: the minimum successive occurences of token
Daniel Veillarda9b66d02002-12-11 14:23:49 +00005094 * @max: the maximum successive occurences of token
5095 * @data: data associated to the transition
Daniel Veillard4255d502002-04-16 15:50:10 +00005096 *
William M. Brackddf71d62004-05-06 04:17:26 +00005097 * If @to is NULL, this creates first a new target state in the automata
Daniel Veillard4255d502002-04-16 15:50:10 +00005098 * and then adds a transition from the @from state to the target state
5099 * activated by a succession of input of value @token and whose number
5100 * is between @min and @max
5101 *
5102 * Returns the target state or NULL in case of error
5103 */
5104xmlAutomataStatePtr
5105xmlAutomataNewCountTrans(xmlAutomataPtr am, xmlAutomataStatePtr from,
5106 xmlAutomataStatePtr to, const xmlChar *token,
5107 int min, int max, void *data) {
5108 xmlRegAtomPtr atom;
Daniel Veillard0ddb21c2004-02-12 12:43:49 +00005109 int counter;
Daniel Veillard4255d502002-04-16 15:50:10 +00005110
5111 if ((am == NULL) || (from == NULL) || (token == NULL))
5112 return(NULL);
5113 if (min < 0)
5114 return(NULL);
5115 if ((max < min) || (max < 1))
5116 return(NULL);
5117 atom = xmlRegNewAtom(am, XML_REGEXP_STRING);
5118 if (atom == NULL)
5119 return(NULL);
5120 atom->valuep = xmlStrdup(token);
5121 atom->data = data;
5122 if (min == 0)
5123 atom->min = 1;
5124 else
5125 atom->min = min;
5126 atom->max = max;
5127
Daniel Veillard0ddb21c2004-02-12 12:43:49 +00005128 /*
5129 * associate a counter to the transition.
5130 */
5131 counter = xmlRegGetCounter(am);
5132 am->counters[counter].min = min;
5133 am->counters[counter].max = max;
5134
5135 /* xmlFAGenerateTransitions(am, from, to, atom); */
5136 if (to == NULL) {
5137 to = xmlRegNewState(am);
5138 xmlRegStatePush(am, to);
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00005139 }
Daniel Veillarddb68b742005-07-30 13:18:24 +00005140 xmlRegStateAddTrans(am, from, atom, to, counter, -1, 0);
Daniel Veillard0ddb21c2004-02-12 12:43:49 +00005141 xmlRegAtomPush(am, atom);
5142 am->state = to;
5143
Daniel Veillard4255d502002-04-16 15:50:10 +00005144 if (to == NULL)
5145 to = am->state;
5146 if (to == NULL)
5147 return(NULL);
5148 if (min == 0)
5149 xmlFAGenerateEpsilonTransition(am, from, to);
5150 return(to);
5151}
5152
5153/**
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00005154 * xmlAutomataNewOnceTrans2:
5155 * @am: an automata
5156 * @from: the starting point of the transition
5157 * @to: the target point of the transition or NULL
5158 * @token: the input string associated to that transition
5159 * @token2: the second input string associated to that transition
5160 * @min: the minimum successive occurences of token
5161 * @max: the maximum successive occurences of token
5162 * @data: data associated to the transition
5163 *
5164 * If @to is NULL, this creates first a new target state in the automata
5165 * and then adds a transition from the @from state to the target state
5166 * activated by a succession of input of value @token and @token2 and whose
5167 * number is between @min and @max, moreover that transition can only be
5168 * crossed once.
5169 *
5170 * Returns the target state or NULL in case of error
5171 */
5172xmlAutomataStatePtr
5173xmlAutomataNewOnceTrans2(xmlAutomataPtr am, xmlAutomataStatePtr from,
5174 xmlAutomataStatePtr to, const xmlChar *token,
5175 const xmlChar *token2,
5176 int min, int max, void *data) {
5177 xmlRegAtomPtr atom;
5178 int counter;
5179
5180 if ((am == NULL) || (from == NULL) || (token == NULL))
5181 return(NULL);
5182 if (min < 1)
5183 return(NULL);
5184 if ((max < min) || (max < 1))
5185 return(NULL);
5186 atom = xmlRegNewAtom(am, XML_REGEXP_STRING);
5187 if (atom == NULL)
5188 return(NULL);
5189 if ((token2 == NULL) || (*token2 == 0)) {
5190 atom->valuep = xmlStrdup(token);
5191 } else {
5192 int lenn, lenp;
5193 xmlChar *str;
5194
5195 lenn = strlen((char *) token2);
5196 lenp = strlen((char *) token);
5197
5198 str = (xmlChar *) xmlMallocAtomic(lenn + lenp + 2);
5199 if (str == NULL) {
5200 xmlRegFreeAtom(atom);
5201 return(NULL);
5202 }
5203 memcpy(&str[0], token, lenp);
5204 str[lenp] = '|';
5205 memcpy(&str[lenp + 1], token2, lenn);
5206 str[lenn + lenp + 1] = 0;
5207
5208 atom->valuep = str;
5209 }
5210 atom->data = data;
5211 atom->quant = XML_REGEXP_QUANT_ONCEONLY;
5212 if (min == 0)
5213 atom->min = 1;
5214 else
5215 atom->min = min;
5216 atom->max = max;
5217 /*
5218 * associate a counter to the transition.
5219 */
5220 counter = xmlRegGetCounter(am);
5221 am->counters[counter].min = 1;
5222 am->counters[counter].max = 1;
5223
5224 /* xmlFAGenerateTransitions(am, from, to, atom); */
5225 if (to == NULL) {
5226 to = xmlRegNewState(am);
5227 xmlRegStatePush(am, to);
5228 }
Daniel Veillarddb68b742005-07-30 13:18:24 +00005229 xmlRegStateAddTrans(am, from, atom, to, counter, -1, 0);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00005230 xmlRegAtomPush(am, atom);
5231 am->state = to;
5232 return(to);
5233}
5234
5235
5236
5237/**
Daniel Veillard7646b182002-04-20 06:41:40 +00005238 * xmlAutomataNewOnceTrans:
5239 * @am: an automata
5240 * @from: the starting point of the transition
5241 * @to: the target point of the transition or NULL
5242 * @token: the input string associated to that transition
5243 * @min: the minimum successive occurences of token
Daniel Veillarda9b66d02002-12-11 14:23:49 +00005244 * @max: the maximum successive occurences of token
5245 * @data: data associated to the transition
Daniel Veillard7646b182002-04-20 06:41:40 +00005246 *
William M. Brackddf71d62004-05-06 04:17:26 +00005247 * If @to is NULL, this creates first a new target state in the automata
Daniel Veillard7646b182002-04-20 06:41:40 +00005248 * and then adds a transition from the @from state to the target state
5249 * activated by a succession of input of value @token and whose number
William M. Brackddf71d62004-05-06 04:17:26 +00005250 * is between @min and @max, moreover that transition can only be crossed
Daniel Veillard7646b182002-04-20 06:41:40 +00005251 * once.
5252 *
5253 * Returns the target state or NULL in case of error
5254 */
5255xmlAutomataStatePtr
5256xmlAutomataNewOnceTrans(xmlAutomataPtr am, xmlAutomataStatePtr from,
5257 xmlAutomataStatePtr to, const xmlChar *token,
5258 int min, int max, void *data) {
5259 xmlRegAtomPtr atom;
5260 int counter;
5261
5262 if ((am == NULL) || (from == NULL) || (token == NULL))
5263 return(NULL);
5264 if (min < 1)
5265 return(NULL);
5266 if ((max < min) || (max < 1))
5267 return(NULL);
5268 atom = xmlRegNewAtom(am, XML_REGEXP_STRING);
5269 if (atom == NULL)
5270 return(NULL);
5271 atom->valuep = xmlStrdup(token);
5272 atom->data = data;
5273 atom->quant = XML_REGEXP_QUANT_ONCEONLY;
5274 if (min == 0)
5275 atom->min = 1;
5276 else
5277 atom->min = min;
5278 atom->max = max;
5279 /*
5280 * associate a counter to the transition.
5281 */
5282 counter = xmlRegGetCounter(am);
5283 am->counters[counter].min = 1;
5284 am->counters[counter].max = 1;
5285
5286 /* xmlFAGenerateTransitions(am, from, to, atom); */
5287 if (to == NULL) {
5288 to = xmlRegNewState(am);
5289 xmlRegStatePush(am, to);
5290 }
Daniel Veillarddb68b742005-07-30 13:18:24 +00005291 xmlRegStateAddTrans(am, from, atom, to, counter, -1, 0);
Daniel Veillard7646b182002-04-20 06:41:40 +00005292 xmlRegAtomPush(am, atom);
5293 am->state = to;
Daniel Veillard7646b182002-04-20 06:41:40 +00005294 return(to);
5295}
5296
5297/**
Daniel Veillard4255d502002-04-16 15:50:10 +00005298 * xmlAutomataNewState:
5299 * @am: an automata
5300 *
5301 * Create a new disconnected state in the automata
5302 *
5303 * Returns the new state or NULL in case of error
5304 */
5305xmlAutomataStatePtr
5306xmlAutomataNewState(xmlAutomataPtr am) {
5307 xmlAutomataStatePtr to;
5308
5309 if (am == NULL)
5310 return(NULL);
5311 to = xmlRegNewState(am);
5312 xmlRegStatePush(am, to);
5313 return(to);
5314}
5315
5316/**
Daniel Veillarda9b66d02002-12-11 14:23:49 +00005317 * xmlAutomataNewEpsilon:
Daniel Veillard4255d502002-04-16 15:50:10 +00005318 * @am: an automata
5319 * @from: the starting point of the transition
5320 * @to: the target point of the transition or NULL
5321 *
William M. Brackddf71d62004-05-06 04:17:26 +00005322 * If @to is NULL, this creates first a new target state in the automata
5323 * and then adds an epsilon transition from the @from state to the
Daniel Veillard4255d502002-04-16 15:50:10 +00005324 * target state
5325 *
5326 * Returns the target state or NULL in case of error
5327 */
5328xmlAutomataStatePtr
5329xmlAutomataNewEpsilon(xmlAutomataPtr am, xmlAutomataStatePtr from,
5330 xmlAutomataStatePtr to) {
5331 if ((am == NULL) || (from == NULL))
5332 return(NULL);
5333 xmlFAGenerateEpsilonTransition(am, from, to);
5334 if (to == NULL)
5335 return(am->state);
5336 return(to);
5337}
5338
Daniel Veillardb509f152002-04-17 16:28:10 +00005339/**
Daniel Veillard7646b182002-04-20 06:41:40 +00005340 * xmlAutomataNewAllTrans:
5341 * @am: an automata
5342 * @from: the starting point of the transition
5343 * @to: the target point of the transition or NULL
Daniel Veillarda9b66d02002-12-11 14:23:49 +00005344 * @lax: allow to transition if not all all transitions have been activated
Daniel Veillard7646b182002-04-20 06:41:40 +00005345 *
William M. Brackddf71d62004-05-06 04:17:26 +00005346 * If @to is NULL, this creates first a new target state in the automata
Daniel Veillard7646b182002-04-20 06:41:40 +00005347 * and then adds a an ALL transition from the @from state to the
5348 * target state. That transition is an epsilon transition allowed only when
5349 * all transitions from the @from node have been activated.
5350 *
5351 * Returns the target state or NULL in case of error
5352 */
5353xmlAutomataStatePtr
5354xmlAutomataNewAllTrans(xmlAutomataPtr am, xmlAutomataStatePtr from,
Daniel Veillard441bc322002-04-20 17:38:48 +00005355 xmlAutomataStatePtr to, int lax) {
Daniel Veillard7646b182002-04-20 06:41:40 +00005356 if ((am == NULL) || (from == NULL))
5357 return(NULL);
Daniel Veillard441bc322002-04-20 17:38:48 +00005358 xmlFAGenerateAllTransition(am, from, to, lax);
Daniel Veillard7646b182002-04-20 06:41:40 +00005359 if (to == NULL)
5360 return(am->state);
5361 return(to);
5362}
5363
5364/**
Daniel Veillardb509f152002-04-17 16:28:10 +00005365 * xmlAutomataNewCounter:
5366 * @am: an automata
5367 * @min: the minimal value on the counter
5368 * @max: the maximal value on the counter
5369 *
5370 * Create a new counter
5371 *
5372 * Returns the counter number or -1 in case of error
5373 */
5374int
5375xmlAutomataNewCounter(xmlAutomataPtr am, int min, int max) {
5376 int ret;
5377
5378 if (am == NULL)
5379 return(-1);
5380
5381 ret = xmlRegGetCounter(am);
5382 if (ret < 0)
5383 return(-1);
5384 am->counters[ret].min = min;
5385 am->counters[ret].max = max;
5386 return(ret);
5387}
5388
5389/**
5390 * xmlAutomataNewCountedTrans:
5391 * @am: an automata
5392 * @from: the starting point of the transition
5393 * @to: the target point of the transition or NULL
5394 * @counter: the counter associated to that transition
5395 *
William M. Brackddf71d62004-05-06 04:17:26 +00005396 * If @to is NULL, this creates first a new target state in the automata
Daniel Veillardb509f152002-04-17 16:28:10 +00005397 * and then adds an epsilon transition from the @from state to the target state
5398 * which will increment the counter provided
5399 *
5400 * Returns the target state or NULL in case of error
5401 */
5402xmlAutomataStatePtr
5403xmlAutomataNewCountedTrans(xmlAutomataPtr am, xmlAutomataStatePtr from,
5404 xmlAutomataStatePtr to, int counter) {
5405 if ((am == NULL) || (from == NULL) || (counter < 0))
5406 return(NULL);
5407 xmlFAGenerateCountedEpsilonTransition(am, from, to, counter);
5408 if (to == NULL)
5409 return(am->state);
5410 return(to);
5411}
5412
5413/**
5414 * xmlAutomataNewCounterTrans:
5415 * @am: an automata
5416 * @from: the starting point of the transition
5417 * @to: the target point of the transition or NULL
5418 * @counter: the counter associated to that transition
5419 *
William M. Brackddf71d62004-05-06 04:17:26 +00005420 * If @to is NULL, this creates first a new target state in the automata
Daniel Veillardb509f152002-04-17 16:28:10 +00005421 * and then adds an epsilon transition from the @from state to the target state
5422 * which will be allowed only if the counter is within the right range.
5423 *
5424 * Returns the target state or NULL in case of error
5425 */
5426xmlAutomataStatePtr
5427xmlAutomataNewCounterTrans(xmlAutomataPtr am, xmlAutomataStatePtr from,
5428 xmlAutomataStatePtr to, int counter) {
5429 if ((am == NULL) || (from == NULL) || (counter < 0))
5430 return(NULL);
5431 xmlFAGenerateCountedTransition(am, from, to, counter);
5432 if (to == NULL)
5433 return(am->state);
5434 return(to);
5435}
Daniel Veillard4255d502002-04-16 15:50:10 +00005436
5437/**
5438 * xmlAutomataCompile:
5439 * @am: an automata
5440 *
5441 * Compile the automata into a Reg Exp ready for being executed.
5442 * The automata should be free after this point.
5443 *
5444 * Returns the compiled regexp or NULL in case of error
5445 */
5446xmlRegexpPtr
5447xmlAutomataCompile(xmlAutomataPtr am) {
5448 xmlRegexpPtr ret;
5449
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00005450 if ((am == NULL) || (am->error != 0)) return(NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +00005451 xmlFAEliminateEpsilonTransitions(am);
Daniel Veillard23e73572002-09-19 19:56:43 +00005452 /* xmlFAComputesDeterminism(am); */
Daniel Veillard4255d502002-04-16 15:50:10 +00005453 ret = xmlRegEpxFromParse(am);
5454
5455 return(ret);
5456}
Daniel Veillarde19fc232002-04-22 16:01:24 +00005457
5458/**
5459 * xmlAutomataIsDeterminist:
5460 * @am: an automata
5461 *
5462 * Checks if an automata is determinist.
5463 *
5464 * Returns 1 if true, 0 if not, and -1 in case of error
5465 */
5466int
5467xmlAutomataIsDeterminist(xmlAutomataPtr am) {
5468 int ret;
5469
5470 if (am == NULL)
5471 return(-1);
5472
5473 ret = xmlFAComputesDeterminism(am);
5474 return(ret);
5475}
Daniel Veillard4255d502002-04-16 15:50:10 +00005476#endif /* LIBXML_AUTOMATA_ENABLED */
Daniel Veillard5d4644e2005-04-01 13:11:58 +00005477#define bottom_xmlregexp
5478#include "elfgcchack.h"
Daniel Veillard4255d502002-04-16 15:50:10 +00005479#endif /* LIBXML_REGEXP_ENABLED */