blob: fe4bd3892afa0b1bf2f082219c3ca66a1dea1da7 [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 Veillard567a45b2005-10-18 19:11:55 +000045#define MAX_PUSH 10000000
Daniel Veillard94cc1032005-09-15 13:09:00 +000046
Daniel Veillardff46a042003-10-08 08:53:17 +000047#define ERROR(str) \
48 ctxt->error = XML_REGEXP_COMPILE_ERROR; \
49 xmlRegexpErrCompile(ctxt, str);
Daniel Veillard4255d502002-04-16 15:50:10 +000050#define NEXT ctxt->cur++
51#define CUR (*(ctxt->cur))
52#define NXT(index) (ctxt->cur[index])
53
54#define CUR_SCHAR(s, l) xmlStringCurrentChar(NULL, s, &l)
55#define NEXTL(l) ctxt->cur += l;
Daniel Veillardc0826a72004-08-10 14:17:33 +000056#define XML_REG_STRING_SEPARATOR '|'
William M. Bracka9cbf282007-03-21 13:16:33 +000057/*
58 * Need PREV to check on a '-' within a Character Group. May only be used
59 * when it's guaranteed that cur is not at the beginning of ctxt->string!
60 */
61#define PREV (ctxt->cur[-1])
Daniel Veillard4255d502002-04-16 15:50:10 +000062
Daniel Veillarde19fc232002-04-22 16:01:24 +000063/**
64 * TODO:
65 *
66 * macro to flag unimplemented blocks
67 */
68#define TODO \
69 xmlGenericError(xmlGenericErrorContext, \
70 "Unimplemented block at %s:%d\n", \
71 __FILE__, __LINE__);
72
Daniel Veillard4255d502002-04-16 15:50:10 +000073/************************************************************************
74 * *
75 * Datatypes and structures *
76 * *
77 ************************************************************************/
78
Daniel Veillardfc011b72006-02-12 19:14:15 +000079/*
80 * Note: the order of the enums below is significant, do not shuffle
81 */
Daniel Veillard4255d502002-04-16 15:50:10 +000082typedef enum {
83 XML_REGEXP_EPSILON = 1,
84 XML_REGEXP_CHARVAL,
85 XML_REGEXP_RANGES,
Daniel Veillard567a45b2005-10-18 19:11:55 +000086 XML_REGEXP_SUBREG, /* used for () sub regexps */
Daniel Veillard4255d502002-04-16 15:50:10 +000087 XML_REGEXP_STRING,
88 XML_REGEXP_ANYCHAR, /* . */
89 XML_REGEXP_ANYSPACE, /* \s */
90 XML_REGEXP_NOTSPACE, /* \S */
91 XML_REGEXP_INITNAME, /* \l */
Daniel Veillard567a45b2005-10-18 19:11:55 +000092 XML_REGEXP_NOTINITNAME, /* \L */
Daniel Veillard4255d502002-04-16 15:50:10 +000093 XML_REGEXP_NAMECHAR, /* \c */
94 XML_REGEXP_NOTNAMECHAR, /* \C */
95 XML_REGEXP_DECIMAL, /* \d */
Daniel Veillard567a45b2005-10-18 19:11:55 +000096 XML_REGEXP_NOTDECIMAL, /* \D */
Daniel Veillard4255d502002-04-16 15:50:10 +000097 XML_REGEXP_REALCHAR, /* \w */
Daniel Veillard567a45b2005-10-18 19:11:55 +000098 XML_REGEXP_NOTREALCHAR, /* \W */
99 XML_REGEXP_LETTER = 100,
Daniel Veillard4255d502002-04-16 15:50:10 +0000100 XML_REGEXP_LETTER_UPPERCASE,
101 XML_REGEXP_LETTER_LOWERCASE,
102 XML_REGEXP_LETTER_TITLECASE,
103 XML_REGEXP_LETTER_MODIFIER,
104 XML_REGEXP_LETTER_OTHERS,
105 XML_REGEXP_MARK,
106 XML_REGEXP_MARK_NONSPACING,
107 XML_REGEXP_MARK_SPACECOMBINING,
108 XML_REGEXP_MARK_ENCLOSING,
109 XML_REGEXP_NUMBER,
110 XML_REGEXP_NUMBER_DECIMAL,
111 XML_REGEXP_NUMBER_LETTER,
112 XML_REGEXP_NUMBER_OTHERS,
113 XML_REGEXP_PUNCT,
114 XML_REGEXP_PUNCT_CONNECTOR,
115 XML_REGEXP_PUNCT_DASH,
116 XML_REGEXP_PUNCT_OPEN,
117 XML_REGEXP_PUNCT_CLOSE,
118 XML_REGEXP_PUNCT_INITQUOTE,
119 XML_REGEXP_PUNCT_FINQUOTE,
120 XML_REGEXP_PUNCT_OTHERS,
121 XML_REGEXP_SEPAR,
122 XML_REGEXP_SEPAR_SPACE,
123 XML_REGEXP_SEPAR_LINE,
124 XML_REGEXP_SEPAR_PARA,
125 XML_REGEXP_SYMBOL,
126 XML_REGEXP_SYMBOL_MATH,
127 XML_REGEXP_SYMBOL_CURRENCY,
128 XML_REGEXP_SYMBOL_MODIFIER,
129 XML_REGEXP_SYMBOL_OTHERS,
130 XML_REGEXP_OTHER,
131 XML_REGEXP_OTHER_CONTROL,
132 XML_REGEXP_OTHER_FORMAT,
133 XML_REGEXP_OTHER_PRIVATE,
134 XML_REGEXP_OTHER_NA,
135 XML_REGEXP_BLOCK_NAME
136} xmlRegAtomType;
137
138typedef enum {
139 XML_REGEXP_QUANT_EPSILON = 1,
140 XML_REGEXP_QUANT_ONCE,
141 XML_REGEXP_QUANT_OPT,
142 XML_REGEXP_QUANT_MULT,
143 XML_REGEXP_QUANT_PLUS,
Daniel Veillard7646b182002-04-20 06:41:40 +0000144 XML_REGEXP_QUANT_ONCEONLY,
145 XML_REGEXP_QUANT_ALL,
Daniel Veillard4255d502002-04-16 15:50:10 +0000146 XML_REGEXP_QUANT_RANGE
147} xmlRegQuantType;
148
149typedef enum {
150 XML_REGEXP_START_STATE = 1,
151 XML_REGEXP_FINAL_STATE,
Daniel Veillardcc026dc2005-01-12 13:21:17 +0000152 XML_REGEXP_TRANS_STATE,
Daniel Veillard0e05f4c2006-11-01 15:33:04 +0000153 XML_REGEXP_SINK_STATE,
154 XML_REGEXP_UNREACH_STATE
Daniel Veillard4255d502002-04-16 15:50:10 +0000155} xmlRegStateType;
156
157typedef enum {
158 XML_REGEXP_MARK_NORMAL = 0,
159 XML_REGEXP_MARK_START,
160 XML_REGEXP_MARK_VISITED
161} xmlRegMarkedType;
162
163typedef struct _xmlRegRange xmlRegRange;
164typedef xmlRegRange *xmlRegRangePtr;
165
166struct _xmlRegRange {
Daniel Veillardf8b9de32003-11-24 14:27:26 +0000167 int neg; /* 0 normal, 1 not, 2 exclude */
Daniel Veillard4255d502002-04-16 15:50:10 +0000168 xmlRegAtomType type;
169 int start;
170 int end;
171 xmlChar *blockName;
172};
173
174typedef struct _xmlRegAtom xmlRegAtom;
175typedef xmlRegAtom *xmlRegAtomPtr;
176
177typedef struct _xmlAutomataState xmlRegState;
178typedef xmlRegState *xmlRegStatePtr;
179
180struct _xmlRegAtom {
181 int no;
182 xmlRegAtomType type;
183 xmlRegQuantType quant;
184 int min;
185 int max;
186
187 void *valuep;
Daniel Veillarda646cfd2002-09-17 21:50:03 +0000188 void *valuep2;
Daniel Veillard4255d502002-04-16 15:50:10 +0000189 int neg;
190 int codepoint;
191 xmlRegStatePtr start;
Daniel Veillard76d59b62007-08-22 16:29:21 +0000192 xmlRegStatePtr start0;
Daniel Veillard4255d502002-04-16 15:50:10 +0000193 xmlRegStatePtr stop;
194 int maxRanges;
195 int nbRanges;
196 xmlRegRangePtr *ranges;
197 void *data;
198};
199
200typedef struct _xmlRegCounter xmlRegCounter;
201typedef xmlRegCounter *xmlRegCounterPtr;
202
203struct _xmlRegCounter {
204 int min;
205 int max;
206};
207
208typedef struct _xmlRegTrans xmlRegTrans;
209typedef xmlRegTrans *xmlRegTransPtr;
210
211struct _xmlRegTrans {
212 xmlRegAtomPtr atom;
213 int to;
214 int counter;
215 int count;
Daniel Veillard567a45b2005-10-18 19:11:55 +0000216 int nd;
Daniel Veillard4255d502002-04-16 15:50:10 +0000217};
218
219struct _xmlAutomataState {
220 xmlRegStateType type;
221 xmlRegMarkedType mark;
Daniel Veillard23e73572002-09-19 19:56:43 +0000222 xmlRegMarkedType reached;
Daniel Veillard4255d502002-04-16 15:50:10 +0000223 int no;
Daniel Veillard4255d502002-04-16 15:50:10 +0000224 int maxTrans;
225 int nbTrans;
226 xmlRegTrans *trans;
Daniel Veillarddb68b742005-07-30 13:18:24 +0000227 /* knowing states ponting to us can speed things up */
228 int maxTransTo;
229 int nbTransTo;
230 int *transTo;
Daniel Veillard4255d502002-04-16 15:50:10 +0000231};
232
233typedef struct _xmlAutomata xmlRegParserCtxt;
234typedef xmlRegParserCtxt *xmlRegParserCtxtPtr;
235
Daniel Veillard1ba2aca2009-08-31 16:47:39 +0200236#define AM_AUTOMATA_RNG 1
237
Daniel Veillard4255d502002-04-16 15:50:10 +0000238struct _xmlAutomata {
239 xmlChar *string;
240 xmlChar *cur;
241
242 int error;
243 int neg;
244
245 xmlRegStatePtr start;
246 xmlRegStatePtr end;
247 xmlRegStatePtr state;
248
249 xmlRegAtomPtr atom;
250
251 int maxAtoms;
252 int nbAtoms;
253 xmlRegAtomPtr *atoms;
254
255 int maxStates;
256 int nbStates;
257 xmlRegStatePtr *states;
258
259 int maxCounters;
260 int nbCounters;
261 xmlRegCounter *counters;
Daniel Veillarde19fc232002-04-22 16:01:24 +0000262
263 int determinist;
Daniel Veillard6e65e152005-08-09 11:09:52 +0000264 int negs;
Daniel Veillard1ba2aca2009-08-31 16:47:39 +0200265 int flags;
Daniel Veillard4255d502002-04-16 15:50:10 +0000266};
267
268struct _xmlRegexp {
269 xmlChar *string;
270 int nbStates;
271 xmlRegStatePtr *states;
272 int nbAtoms;
273 xmlRegAtomPtr *atoms;
274 int nbCounters;
275 xmlRegCounter *counters;
Daniel Veillarde19fc232002-04-22 16:01:24 +0000276 int determinist;
Daniel Veillard1ba2aca2009-08-31 16:47:39 +0200277 int flags;
Daniel Veillard23e73572002-09-19 19:56:43 +0000278 /*
279 * That's the compact form for determinists automatas
280 */
281 int nbstates;
282 int *compact;
Daniel Veillard118aed72002-09-24 14:13:13 +0000283 void **transdata;
Daniel Veillard23e73572002-09-19 19:56:43 +0000284 int nbstrings;
285 xmlChar **stringMap;
Daniel Veillard4255d502002-04-16 15:50:10 +0000286};
287
288typedef struct _xmlRegExecRollback xmlRegExecRollback;
289typedef xmlRegExecRollback *xmlRegExecRollbackPtr;
290
291struct _xmlRegExecRollback {
292 xmlRegStatePtr state;/* the current state */
293 int index; /* the index in the input stack */
294 int nextbranch; /* the next transition to explore in that state */
William M. Brackddf71d62004-05-06 04:17:26 +0000295 int *counts; /* save the automata state if it has some */
Daniel Veillard4255d502002-04-16 15:50:10 +0000296};
297
298typedef struct _xmlRegInputToken xmlRegInputToken;
299typedef xmlRegInputToken *xmlRegInputTokenPtr;
300
301struct _xmlRegInputToken {
302 xmlChar *value;
303 void *data;
304};
305
306struct _xmlRegExecCtxt {
307 int status; /* execution status != 0 indicate an error */
William M. Brackddf71d62004-05-06 04:17:26 +0000308 int determinist; /* did we find an indeterministic behaviour */
Daniel Veillard4255d502002-04-16 15:50:10 +0000309 xmlRegexpPtr comp; /* the compiled regexp */
310 xmlRegExecCallbacks callback;
311 void *data;
312
313 xmlRegStatePtr state;/* the current state */
314 int transno; /* the current transition on that state */
William M. Brackddf71d62004-05-06 04:17:26 +0000315 int transcount; /* the number of chars in char counted transitions */
Daniel Veillard4255d502002-04-16 15:50:10 +0000316
317 /*
318 * A stack of rollback states
319 */
320 int maxRollbacks;
321 int nbRollbacks;
322 xmlRegExecRollback *rollbacks;
323
324 /*
325 * The state of the automata if any
326 */
327 int *counts;
328
329 /*
330 * The input stack
331 */
332 int inputStackMax;
333 int inputStackNr;
334 int index;
335 int *charStack;
336 const xmlChar *inputString; /* when operating on characters */
337 xmlRegInputTokenPtr inputStack;/* when operating on strings */
338
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +0000339 /*
340 * error handling
341 */
342 int errStateNo; /* the error state number */
343 xmlRegStatePtr errState; /* the error state */
344 xmlChar *errString; /* the string raising the error */
345 int *errCounts; /* counters at the error state */
Daniel Veillard94cc1032005-09-15 13:09:00 +0000346 int nbPush;
Daniel Veillard4255d502002-04-16 15:50:10 +0000347};
348
Daniel Veillard441bc322002-04-20 17:38:48 +0000349#define REGEXP_ALL_COUNTER 0x123456
350#define REGEXP_ALL_LAX_COUNTER 0x123457
Daniel Veillard7646b182002-04-20 06:41:40 +0000351
Daniel Veillard4255d502002-04-16 15:50:10 +0000352static void xmlFAParseRegExp(xmlRegParserCtxtPtr ctxt, int top);
Daniel Veillard23e73572002-09-19 19:56:43 +0000353static void xmlRegFreeState(xmlRegStatePtr state);
354static void xmlRegFreeAtom(xmlRegAtomPtr atom);
Daniel Veillard9efc4762005-07-19 14:33:55 +0000355static int xmlRegStrEqualWildcard(const xmlChar *expStr, const xmlChar *valStr);
Daniel Veillard567a45b2005-10-18 19:11:55 +0000356static int xmlRegCheckCharacter(xmlRegAtomPtr atom, int codepoint);
357static int xmlRegCheckCharacterRange(xmlRegAtomType type, int codepoint,
358 int neg, int start, int end, const xmlChar *blockName);
Daniel Veillard4255d502002-04-16 15:50:10 +0000359
Daniel Veillard1ba2aca2009-08-31 16:47:39 +0200360void xmlAutomataSetFlags(xmlAutomataPtr am, int flags);
361
Daniel Veillard4255d502002-04-16 15:50:10 +0000362/************************************************************************
Daniel Veillardff46a042003-10-08 08:53:17 +0000363 * *
364 * Regexp memory error handler *
365 * *
366 ************************************************************************/
367/**
368 * xmlRegexpErrMemory:
William M. Brackddf71d62004-05-06 04:17:26 +0000369 * @extra: extra information
Daniel Veillardff46a042003-10-08 08:53:17 +0000370 *
371 * Handle an out of memory condition
372 */
373static void
374xmlRegexpErrMemory(xmlRegParserCtxtPtr ctxt, const char *extra)
375{
376 const char *regexp = NULL;
377 if (ctxt != NULL) {
378 regexp = (const char *) ctxt->string;
379 ctxt->error = XML_ERR_NO_MEMORY;
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_ERR_NO_MEMORY, XML_ERR_FATAL, NULL, 0, extra,
383 regexp, NULL, 0, 0,
384 "Memory allocation failed : %s\n", extra);
385}
386
387/**
388 * xmlRegexpErrCompile:
William M. Brackddf71d62004-05-06 04:17:26 +0000389 * @extra: extra information
Daniel Veillardff46a042003-10-08 08:53:17 +0000390 *
William M. Brackddf71d62004-05-06 04:17:26 +0000391 * Handle a compilation failure
Daniel Veillardff46a042003-10-08 08:53:17 +0000392 */
393static void
394xmlRegexpErrCompile(xmlRegParserCtxtPtr ctxt, const char *extra)
395{
396 const char *regexp = NULL;
397 int idx = 0;
398
399 if (ctxt != NULL) {
400 regexp = (const char *) ctxt->string;
401 idx = ctxt->cur - ctxt->string;
402 ctxt->error = XML_REGEXP_COMPILE_ERROR;
403 }
Daniel Veillard659e71e2003-10-10 14:10:40 +0000404 __xmlRaiseError(NULL, NULL, NULL, NULL, NULL, XML_FROM_REGEXP,
Daniel Veillardff46a042003-10-08 08:53:17 +0000405 XML_REGEXP_COMPILE_ERROR, XML_ERR_FATAL, NULL, 0, extra,
406 regexp, NULL, idx, 0,
407 "failed to compile: %s\n", extra);
408}
409
410/************************************************************************
Daniel Veillard4255d502002-04-16 15:50:10 +0000411 * *
412 * Allocation/Deallocation *
413 * *
414 ************************************************************************/
415
Daniel Veillard23e73572002-09-19 19:56:43 +0000416static int xmlFAComputesDeterminism(xmlRegParserCtxtPtr ctxt);
Daniel Veillard4255d502002-04-16 15:50:10 +0000417/**
418 * xmlRegEpxFromParse:
419 * @ctxt: the parser context used to build it
420 *
William M. Brackddf71d62004-05-06 04:17:26 +0000421 * Allocate a new regexp and fill it with the result from the parser
Daniel Veillard4255d502002-04-16 15:50:10 +0000422 *
423 * Returns the new regexp or NULL in case of error
424 */
425static xmlRegexpPtr
426xmlRegEpxFromParse(xmlRegParserCtxtPtr ctxt) {
427 xmlRegexpPtr ret;
428
429 ret = (xmlRegexpPtr) xmlMalloc(sizeof(xmlRegexp));
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000430 if (ret == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +0000431 xmlRegexpErrMemory(ctxt, "compiling regexp");
Daniel Veillard4255d502002-04-16 15:50:10 +0000432 return(NULL);
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000433 }
Daniel Veillard4255d502002-04-16 15:50:10 +0000434 memset(ret, 0, sizeof(xmlRegexp));
435 ret->string = ctxt->string;
Daniel Veillard4255d502002-04-16 15:50:10 +0000436 ret->nbStates = ctxt->nbStates;
Daniel Veillard4255d502002-04-16 15:50:10 +0000437 ret->states = ctxt->states;
Daniel Veillard4255d502002-04-16 15:50:10 +0000438 ret->nbAtoms = ctxt->nbAtoms;
Daniel Veillard4255d502002-04-16 15:50:10 +0000439 ret->atoms = ctxt->atoms;
Daniel Veillard4255d502002-04-16 15:50:10 +0000440 ret->nbCounters = ctxt->nbCounters;
Daniel Veillard4255d502002-04-16 15:50:10 +0000441 ret->counters = ctxt->counters;
Daniel Veillarde19fc232002-04-22 16:01:24 +0000442 ret->determinist = ctxt->determinist;
Daniel Veillard1ba2aca2009-08-31 16:47:39 +0200443 ret->flags = ctxt->flags;
Daniel Veillard567a45b2005-10-18 19:11:55 +0000444 if (ret->determinist == -1) {
445 xmlRegexpIsDeterminist(ret);
446 }
Daniel Veillard23e73572002-09-19 19:56:43 +0000447
448 if ((ret->determinist != 0) &&
449 (ret->nbCounters == 0) &&
Daniel Veillard6e65e152005-08-09 11:09:52 +0000450 (ctxt->negs == 0) &&
Daniel Veillard118aed72002-09-24 14:13:13 +0000451 (ret->atoms != NULL) &&
Daniel Veillard23e73572002-09-19 19:56:43 +0000452 (ret->atoms[0] != NULL) &&
453 (ret->atoms[0]->type == XML_REGEXP_STRING)) {
454 int i, j, nbstates = 0, nbatoms = 0;
455 int *stateRemap;
456 int *stringRemap;
457 int *transitions;
Daniel Veillard118aed72002-09-24 14:13:13 +0000458 void **transdata;
Daniel Veillard23e73572002-09-19 19:56:43 +0000459 xmlChar **stringMap;
460 xmlChar *value;
461
462 /*
463 * Switch to a compact representation
464 * 1/ counting the effective number of states left
William M. Brackddf71d62004-05-06 04:17:26 +0000465 * 2/ counting the unique number of atoms, and check that
Daniel Veillard23e73572002-09-19 19:56:43 +0000466 * they are all of the string type
467 * 3/ build a table state x atom for the transitions
468 */
469
470 stateRemap = xmlMalloc(ret->nbStates * sizeof(int));
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000471 if (stateRemap == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +0000472 xmlRegexpErrMemory(ctxt, "compiling regexp");
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000473 xmlFree(ret);
474 return(NULL);
475 }
Daniel Veillard23e73572002-09-19 19:56:43 +0000476 for (i = 0;i < ret->nbStates;i++) {
477 if (ret->states[i] != NULL) {
478 stateRemap[i] = nbstates;
479 nbstates++;
480 } else {
481 stateRemap[i] = -1;
482 }
483 }
484#ifdef DEBUG_COMPACTION
485 printf("Final: %d states\n", nbstates);
486#endif
487 stringMap = xmlMalloc(ret->nbAtoms * sizeof(char *));
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000488 if (stringMap == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +0000489 xmlRegexpErrMemory(ctxt, "compiling regexp");
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000490 xmlFree(stateRemap);
491 xmlFree(ret);
492 return(NULL);
493 }
Daniel Veillard23e73572002-09-19 19:56:43 +0000494 stringRemap = xmlMalloc(ret->nbAtoms * sizeof(int));
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000495 if (stringRemap == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +0000496 xmlRegexpErrMemory(ctxt, "compiling regexp");
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000497 xmlFree(stringMap);
498 xmlFree(stateRemap);
499 xmlFree(ret);
500 return(NULL);
501 }
Daniel Veillard23e73572002-09-19 19:56:43 +0000502 for (i = 0;i < ret->nbAtoms;i++) {
503 if ((ret->atoms[i]->type == XML_REGEXP_STRING) &&
504 (ret->atoms[i]->quant == XML_REGEXP_QUANT_ONCE)) {
505 value = ret->atoms[i]->valuep;
506 for (j = 0;j < nbatoms;j++) {
507 if (xmlStrEqual(stringMap[j], value)) {
508 stringRemap[i] = j;
509 break;
510 }
511 }
512 if (j >= nbatoms) {
513 stringRemap[i] = nbatoms;
514 stringMap[nbatoms] = xmlStrdup(value);
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000515 if (stringMap[nbatoms] == NULL) {
516 for (i = 0;i < nbatoms;i++)
517 xmlFree(stringMap[i]);
518 xmlFree(stringRemap);
519 xmlFree(stringMap);
520 xmlFree(stateRemap);
521 xmlFree(ret);
522 return(NULL);
523 }
Daniel Veillard23e73572002-09-19 19:56:43 +0000524 nbatoms++;
525 }
526 } else {
527 xmlFree(stateRemap);
528 xmlFree(stringRemap);
529 for (i = 0;i < nbatoms;i++)
530 xmlFree(stringMap[i]);
531 xmlFree(stringMap);
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000532 xmlFree(ret);
533 return(NULL);
Daniel Veillard23e73572002-09-19 19:56:43 +0000534 }
535 }
536#ifdef DEBUG_COMPACTION
537 printf("Final: %d atoms\n", nbatoms);
538#endif
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000539 transitions = (int *) xmlMalloc((nbstates + 1) *
540 (nbatoms + 1) * sizeof(int));
541 if (transitions == NULL) {
542 xmlFree(stateRemap);
543 xmlFree(stringRemap);
544 xmlFree(stringMap);
545 xmlFree(ret);
546 return(NULL);
547 }
548 memset(transitions, 0, (nbstates + 1) * (nbatoms + 1) * sizeof(int));
Daniel Veillard23e73572002-09-19 19:56:43 +0000549
550 /*
551 * Allocate the transition table. The first entry for each
William M. Brackddf71d62004-05-06 04:17:26 +0000552 * state corresponds to the state type.
Daniel Veillard23e73572002-09-19 19:56:43 +0000553 */
Daniel Veillard118aed72002-09-24 14:13:13 +0000554 transdata = NULL;
Daniel Veillard23e73572002-09-19 19:56:43 +0000555
556 for (i = 0;i < ret->nbStates;i++) {
557 int stateno, atomno, targetno, prev;
558 xmlRegStatePtr state;
559 xmlRegTransPtr trans;
560
561 stateno = stateRemap[i];
562 if (stateno == -1)
563 continue;
564 state = ret->states[i];
565
566 transitions[stateno * (nbatoms + 1)] = state->type;
567
568 for (j = 0;j < state->nbTrans;j++) {
569 trans = &(state->trans[j]);
570 if ((trans->to == -1) || (trans->atom == NULL))
571 continue;
572 atomno = stringRemap[trans->atom->no];
Daniel Veillard118aed72002-09-24 14:13:13 +0000573 if ((trans->atom->data != NULL) && (transdata == NULL)) {
574 transdata = (void **) xmlMalloc(nbstates * nbatoms *
575 sizeof(void *));
576 if (transdata != NULL)
577 memset(transdata, 0,
578 nbstates * nbatoms * sizeof(void *));
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000579 else {
Daniel Veillardff46a042003-10-08 08:53:17 +0000580 xmlRegexpErrMemory(ctxt, "compiling regexp");
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000581 break;
582 }
Daniel Veillard118aed72002-09-24 14:13:13 +0000583 }
Daniel Veillard23e73572002-09-19 19:56:43 +0000584 targetno = stateRemap[trans->to];
585 /*
William M. Brackddf71d62004-05-06 04:17:26 +0000586 * if the same atom can generate transitions to 2 different
Daniel Veillard23e73572002-09-19 19:56:43 +0000587 * states then it means the automata is not determinist and
588 * the compact form can't be used !
589 */
590 prev = transitions[stateno * (nbatoms + 1) + atomno + 1];
591 if (prev != 0) {
592 if (prev != targetno + 1) {
Daniel Veillard23e73572002-09-19 19:56:43 +0000593 ret->determinist = 0;
594#ifdef DEBUG_COMPACTION
595 printf("Indet: state %d trans %d, atom %d to %d : %d to %d\n",
596 i, j, trans->atom->no, trans->to, atomno, targetno);
597 printf(" previous to is %d\n", prev);
598#endif
Daniel Veillard118aed72002-09-24 14:13:13 +0000599 if (transdata != NULL)
600 xmlFree(transdata);
Daniel Veillard23e73572002-09-19 19:56:43 +0000601 xmlFree(transitions);
602 xmlFree(stateRemap);
603 xmlFree(stringRemap);
604 for (i = 0;i < nbatoms;i++)
605 xmlFree(stringMap[i]);
606 xmlFree(stringMap);
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000607 goto not_determ;
Daniel Veillard23e73572002-09-19 19:56:43 +0000608 }
609 } else {
610#if 0
611 printf("State %d trans %d: atom %d to %d : %d to %d\n",
612 i, j, trans->atom->no, trans->to, atomno, targetno);
613#endif
614 transitions[stateno * (nbatoms + 1) + atomno + 1] =
Daniel Veillard118aed72002-09-24 14:13:13 +0000615 targetno + 1; /* to avoid 0 */
616 if (transdata != NULL)
617 transdata[stateno * nbatoms + atomno] =
618 trans->atom->data;
Daniel Veillard23e73572002-09-19 19:56:43 +0000619 }
620 }
621 }
622 ret->determinist = 1;
623#ifdef DEBUG_COMPACTION
624 /*
625 * Debug
626 */
627 for (i = 0;i < nbstates;i++) {
628 for (j = 0;j < nbatoms + 1;j++) {
629 printf("%02d ", transitions[i * (nbatoms + 1) + j]);
630 }
631 printf("\n");
632 }
633 printf("\n");
634#endif
635 /*
636 * Cleanup of the old data
637 */
638 if (ret->states != NULL) {
639 for (i = 0;i < ret->nbStates;i++)
640 xmlRegFreeState(ret->states[i]);
641 xmlFree(ret->states);
642 }
643 ret->states = NULL;
644 ret->nbStates = 0;
645 if (ret->atoms != NULL) {
646 for (i = 0;i < ret->nbAtoms;i++)
647 xmlRegFreeAtom(ret->atoms[i]);
648 xmlFree(ret->atoms);
649 }
650 ret->atoms = NULL;
651 ret->nbAtoms = 0;
652
653 ret->compact = transitions;
Daniel Veillard118aed72002-09-24 14:13:13 +0000654 ret->transdata = transdata;
Daniel Veillard23e73572002-09-19 19:56:43 +0000655 ret->stringMap = stringMap;
656 ret->nbstrings = nbatoms;
657 ret->nbstates = nbstates;
658 xmlFree(stateRemap);
659 xmlFree(stringRemap);
660 }
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000661not_determ:
662 ctxt->string = NULL;
663 ctxt->nbStates = 0;
664 ctxt->states = NULL;
665 ctxt->nbAtoms = 0;
666 ctxt->atoms = NULL;
667 ctxt->nbCounters = 0;
668 ctxt->counters = NULL;
Daniel Veillard4255d502002-04-16 15:50:10 +0000669 return(ret);
670}
671
672/**
673 * xmlRegNewParserCtxt:
674 * @string: the string to parse
675 *
676 * Allocate a new regexp parser context
677 *
678 * Returns the new context or NULL in case of error
679 */
680static xmlRegParserCtxtPtr
681xmlRegNewParserCtxt(const xmlChar *string) {
682 xmlRegParserCtxtPtr ret;
683
684 ret = (xmlRegParserCtxtPtr) xmlMalloc(sizeof(xmlRegParserCtxt));
685 if (ret == NULL)
686 return(NULL);
687 memset(ret, 0, sizeof(xmlRegParserCtxt));
688 if (string != NULL)
689 ret->string = xmlStrdup(string);
690 ret->cur = ret->string;
691 ret->neg = 0;
Daniel Veillard6e65e152005-08-09 11:09:52 +0000692 ret->negs = 0;
Daniel Veillard4255d502002-04-16 15:50:10 +0000693 ret->error = 0;
Daniel Veillarde19fc232002-04-22 16:01:24 +0000694 ret->determinist = -1;
Daniel Veillard4255d502002-04-16 15:50:10 +0000695 return(ret);
696}
697
698/**
699 * xmlRegNewRange:
700 * @ctxt: the regexp parser context
701 * @neg: is that negative
702 * @type: the type of range
703 * @start: the start codepoint
704 * @end: the end codepoint
705 *
706 * Allocate a new regexp range
707 *
708 * Returns the new range or NULL in case of error
709 */
710static xmlRegRangePtr
711xmlRegNewRange(xmlRegParserCtxtPtr ctxt,
712 int neg, xmlRegAtomType type, int start, int end) {
713 xmlRegRangePtr ret;
714
715 ret = (xmlRegRangePtr) xmlMalloc(sizeof(xmlRegRange));
716 if (ret == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +0000717 xmlRegexpErrMemory(ctxt, "allocating range");
Daniel Veillard4255d502002-04-16 15:50:10 +0000718 return(NULL);
719 }
720 ret->neg = neg;
721 ret->type = type;
722 ret->start = start;
723 ret->end = end;
724 return(ret);
725}
726
727/**
728 * xmlRegFreeRange:
729 * @range: the regexp range
730 *
731 * Free a regexp range
732 */
733static void
734xmlRegFreeRange(xmlRegRangePtr range) {
735 if (range == NULL)
736 return;
737
738 if (range->blockName != NULL)
739 xmlFree(range->blockName);
740 xmlFree(range);
741}
742
743/**
Daniel Veillard76d59b62007-08-22 16:29:21 +0000744 * xmlRegCopyRange:
745 * @range: the regexp range
746 *
747 * Copy a regexp range
748 *
749 * Returns the new copy or NULL in case of error.
750 */
751static xmlRegRangePtr
752xmlRegCopyRange(xmlRegParserCtxtPtr ctxt, xmlRegRangePtr range) {
753 xmlRegRangePtr ret;
754
755 if (range == NULL)
756 return(NULL);
757
758 ret = xmlRegNewRange(ctxt, range->neg, range->type, range->start,
759 range->end);
760 if (ret == NULL)
761 return(NULL);
762 if (range->blockName != NULL) {
763 ret->blockName = xmlStrdup(range->blockName);
764 if (ret->blockName == NULL) {
765 xmlRegexpErrMemory(ctxt, "allocating range");
766 xmlRegFreeRange(ret);
767 return(NULL);
768 }
769 }
770 return(ret);
771}
772
773/**
Daniel Veillard4255d502002-04-16 15:50:10 +0000774 * xmlRegNewAtom:
775 * @ctxt: the regexp parser context
776 * @type: the type of atom
777 *
Daniel Veillard76d59b62007-08-22 16:29:21 +0000778 * Allocate a new atom
Daniel Veillard4255d502002-04-16 15:50:10 +0000779 *
780 * Returns the new atom or NULL in case of error
781 */
782static xmlRegAtomPtr
783xmlRegNewAtom(xmlRegParserCtxtPtr ctxt, xmlRegAtomType type) {
784 xmlRegAtomPtr ret;
785
786 ret = (xmlRegAtomPtr) xmlMalloc(sizeof(xmlRegAtom));
787 if (ret == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +0000788 xmlRegexpErrMemory(ctxt, "allocating atom");
Daniel Veillard4255d502002-04-16 15:50:10 +0000789 return(NULL);
790 }
791 memset(ret, 0, sizeof(xmlRegAtom));
792 ret->type = type;
793 ret->quant = XML_REGEXP_QUANT_ONCE;
794 ret->min = 0;
795 ret->max = 0;
796 return(ret);
797}
798
799/**
800 * xmlRegFreeAtom:
801 * @atom: the regexp atom
802 *
803 * Free a regexp atom
804 */
805static void
806xmlRegFreeAtom(xmlRegAtomPtr atom) {
807 int i;
808
809 if (atom == NULL)
810 return;
811
812 for (i = 0;i < atom->nbRanges;i++)
813 xmlRegFreeRange(atom->ranges[i]);
814 if (atom->ranges != NULL)
815 xmlFree(atom->ranges);
Daniel Veillardde0e4982005-07-03 14:35:44 +0000816 if ((atom->type == XML_REGEXP_STRING) && (atom->valuep != NULL))
817 xmlFree(atom->valuep);
Daniel Veillard77005e62005-07-19 16:26:18 +0000818 if ((atom->type == XML_REGEXP_STRING) && (atom->valuep2 != NULL))
819 xmlFree(atom->valuep2);
Daniel Veillardde0e4982005-07-03 14:35:44 +0000820 if ((atom->type == XML_REGEXP_BLOCK_NAME) && (atom->valuep != NULL))
Daniel Veillard4255d502002-04-16 15:50:10 +0000821 xmlFree(atom->valuep);
822 xmlFree(atom);
823}
824
Daniel Veillard76d59b62007-08-22 16:29:21 +0000825/**
826 * xmlRegCopyAtom:
827 * @ctxt: the regexp parser context
828 * @atom: the oiginal atom
829 *
830 * Allocate a new regexp range
831 *
832 * Returns the new atom or NULL in case of error
833 */
834static xmlRegAtomPtr
835xmlRegCopyAtom(xmlRegParserCtxtPtr ctxt, xmlRegAtomPtr atom) {
836 xmlRegAtomPtr ret;
837
838 ret = (xmlRegAtomPtr) xmlMalloc(sizeof(xmlRegAtom));
839 if (ret == NULL) {
840 xmlRegexpErrMemory(ctxt, "copying atom");
841 return(NULL);
842 }
843 memset(ret, 0, sizeof(xmlRegAtom));
844 ret->type = atom->type;
845 ret->quant = atom->quant;
846 ret->min = atom->min;
847 ret->max = atom->max;
848 if (atom->nbRanges > 0) {
849 int i;
850
851 ret->ranges = (xmlRegRangePtr *) xmlMalloc(sizeof(xmlRegRangePtr) *
852 atom->nbRanges);
853 if (ret->ranges == NULL) {
854 xmlRegexpErrMemory(ctxt, "copying atom");
855 goto error;
856 }
857 for (i = 0;i < atom->nbRanges;i++) {
858 ret->ranges[i] = xmlRegCopyRange(ctxt, atom->ranges[i]);
859 if (ret->ranges[i] == NULL)
860 goto error;
861 ret->nbRanges = i + 1;
862 }
863 }
864 return(ret);
865
866error:
867 xmlRegFreeAtom(ret);
868 return(NULL);
869}
870
Daniel Veillard4255d502002-04-16 15:50:10 +0000871static xmlRegStatePtr
872xmlRegNewState(xmlRegParserCtxtPtr ctxt) {
873 xmlRegStatePtr ret;
874
875 ret = (xmlRegStatePtr) xmlMalloc(sizeof(xmlRegState));
876 if (ret == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +0000877 xmlRegexpErrMemory(ctxt, "allocating state");
Daniel Veillard4255d502002-04-16 15:50:10 +0000878 return(NULL);
879 }
880 memset(ret, 0, sizeof(xmlRegState));
881 ret->type = XML_REGEXP_TRANS_STATE;
882 ret->mark = XML_REGEXP_MARK_NORMAL;
883 return(ret);
884}
885
886/**
887 * xmlRegFreeState:
888 * @state: the regexp state
889 *
890 * Free a regexp state
891 */
892static void
893xmlRegFreeState(xmlRegStatePtr state) {
894 if (state == NULL)
895 return;
896
897 if (state->trans != NULL)
898 xmlFree(state->trans);
Daniel Veillarddb68b742005-07-30 13:18:24 +0000899 if (state->transTo != NULL)
900 xmlFree(state->transTo);
Daniel Veillard4255d502002-04-16 15:50:10 +0000901 xmlFree(state);
902}
903
904/**
905 * xmlRegFreeParserCtxt:
906 * @ctxt: the regexp parser context
907 *
908 * Free a regexp parser context
909 */
910static void
911xmlRegFreeParserCtxt(xmlRegParserCtxtPtr ctxt) {
912 int i;
913 if (ctxt == NULL)
914 return;
915
916 if (ctxt->string != NULL)
917 xmlFree(ctxt->string);
918 if (ctxt->states != NULL) {
919 for (i = 0;i < ctxt->nbStates;i++)
920 xmlRegFreeState(ctxt->states[i]);
921 xmlFree(ctxt->states);
922 }
923 if (ctxt->atoms != NULL) {
924 for (i = 0;i < ctxt->nbAtoms;i++)
925 xmlRegFreeAtom(ctxt->atoms[i]);
926 xmlFree(ctxt->atoms);
927 }
928 if (ctxt->counters != NULL)
929 xmlFree(ctxt->counters);
930 xmlFree(ctxt);
931}
932
933/************************************************************************
934 * *
935 * Display of Data structures *
936 * *
937 ************************************************************************/
938
939static void
940xmlRegPrintAtomType(FILE *output, xmlRegAtomType type) {
941 switch (type) {
942 case XML_REGEXP_EPSILON:
943 fprintf(output, "epsilon "); break;
944 case XML_REGEXP_CHARVAL:
945 fprintf(output, "charval "); break;
946 case XML_REGEXP_RANGES:
947 fprintf(output, "ranges "); break;
948 case XML_REGEXP_SUBREG:
949 fprintf(output, "subexpr "); break;
950 case XML_REGEXP_STRING:
951 fprintf(output, "string "); break;
952 case XML_REGEXP_ANYCHAR:
953 fprintf(output, "anychar "); break;
954 case XML_REGEXP_ANYSPACE:
955 fprintf(output, "anyspace "); break;
956 case XML_REGEXP_NOTSPACE:
957 fprintf(output, "notspace "); break;
958 case XML_REGEXP_INITNAME:
959 fprintf(output, "initname "); break;
960 case XML_REGEXP_NOTINITNAME:
961 fprintf(output, "notinitname "); break;
962 case XML_REGEXP_NAMECHAR:
963 fprintf(output, "namechar "); break;
964 case XML_REGEXP_NOTNAMECHAR:
965 fprintf(output, "notnamechar "); break;
966 case XML_REGEXP_DECIMAL:
967 fprintf(output, "decimal "); break;
968 case XML_REGEXP_NOTDECIMAL:
969 fprintf(output, "notdecimal "); break;
970 case XML_REGEXP_REALCHAR:
971 fprintf(output, "realchar "); break;
972 case XML_REGEXP_NOTREALCHAR:
973 fprintf(output, "notrealchar "); break;
974 case XML_REGEXP_LETTER:
975 fprintf(output, "LETTER "); break;
976 case XML_REGEXP_LETTER_UPPERCASE:
977 fprintf(output, "LETTER_UPPERCASE "); break;
978 case XML_REGEXP_LETTER_LOWERCASE:
979 fprintf(output, "LETTER_LOWERCASE "); break;
980 case XML_REGEXP_LETTER_TITLECASE:
981 fprintf(output, "LETTER_TITLECASE "); break;
982 case XML_REGEXP_LETTER_MODIFIER:
983 fprintf(output, "LETTER_MODIFIER "); break;
984 case XML_REGEXP_LETTER_OTHERS:
985 fprintf(output, "LETTER_OTHERS "); break;
986 case XML_REGEXP_MARK:
987 fprintf(output, "MARK "); break;
988 case XML_REGEXP_MARK_NONSPACING:
989 fprintf(output, "MARK_NONSPACING "); break;
990 case XML_REGEXP_MARK_SPACECOMBINING:
991 fprintf(output, "MARK_SPACECOMBINING "); break;
992 case XML_REGEXP_MARK_ENCLOSING:
993 fprintf(output, "MARK_ENCLOSING "); break;
994 case XML_REGEXP_NUMBER:
995 fprintf(output, "NUMBER "); break;
996 case XML_REGEXP_NUMBER_DECIMAL:
997 fprintf(output, "NUMBER_DECIMAL "); break;
998 case XML_REGEXP_NUMBER_LETTER:
999 fprintf(output, "NUMBER_LETTER "); break;
1000 case XML_REGEXP_NUMBER_OTHERS:
1001 fprintf(output, "NUMBER_OTHERS "); break;
1002 case XML_REGEXP_PUNCT:
1003 fprintf(output, "PUNCT "); break;
1004 case XML_REGEXP_PUNCT_CONNECTOR:
1005 fprintf(output, "PUNCT_CONNECTOR "); break;
1006 case XML_REGEXP_PUNCT_DASH:
1007 fprintf(output, "PUNCT_DASH "); break;
1008 case XML_REGEXP_PUNCT_OPEN:
1009 fprintf(output, "PUNCT_OPEN "); break;
1010 case XML_REGEXP_PUNCT_CLOSE:
1011 fprintf(output, "PUNCT_CLOSE "); break;
1012 case XML_REGEXP_PUNCT_INITQUOTE:
1013 fprintf(output, "PUNCT_INITQUOTE "); break;
1014 case XML_REGEXP_PUNCT_FINQUOTE:
1015 fprintf(output, "PUNCT_FINQUOTE "); break;
1016 case XML_REGEXP_PUNCT_OTHERS:
1017 fprintf(output, "PUNCT_OTHERS "); break;
1018 case XML_REGEXP_SEPAR:
1019 fprintf(output, "SEPAR "); break;
1020 case XML_REGEXP_SEPAR_SPACE:
1021 fprintf(output, "SEPAR_SPACE "); break;
1022 case XML_REGEXP_SEPAR_LINE:
1023 fprintf(output, "SEPAR_LINE "); break;
1024 case XML_REGEXP_SEPAR_PARA:
1025 fprintf(output, "SEPAR_PARA "); break;
1026 case XML_REGEXP_SYMBOL:
1027 fprintf(output, "SYMBOL "); break;
1028 case XML_REGEXP_SYMBOL_MATH:
1029 fprintf(output, "SYMBOL_MATH "); break;
1030 case XML_REGEXP_SYMBOL_CURRENCY:
1031 fprintf(output, "SYMBOL_CURRENCY "); break;
1032 case XML_REGEXP_SYMBOL_MODIFIER:
1033 fprintf(output, "SYMBOL_MODIFIER "); break;
1034 case XML_REGEXP_SYMBOL_OTHERS:
1035 fprintf(output, "SYMBOL_OTHERS "); break;
1036 case XML_REGEXP_OTHER:
1037 fprintf(output, "OTHER "); break;
1038 case XML_REGEXP_OTHER_CONTROL:
1039 fprintf(output, "OTHER_CONTROL "); break;
1040 case XML_REGEXP_OTHER_FORMAT:
1041 fprintf(output, "OTHER_FORMAT "); break;
1042 case XML_REGEXP_OTHER_PRIVATE:
1043 fprintf(output, "OTHER_PRIVATE "); break;
1044 case XML_REGEXP_OTHER_NA:
1045 fprintf(output, "OTHER_NA "); break;
1046 case XML_REGEXP_BLOCK_NAME:
1047 fprintf(output, "BLOCK "); break;
1048 }
1049}
1050
1051static void
1052xmlRegPrintQuantType(FILE *output, xmlRegQuantType type) {
1053 switch (type) {
1054 case XML_REGEXP_QUANT_EPSILON:
1055 fprintf(output, "epsilon "); break;
1056 case XML_REGEXP_QUANT_ONCE:
1057 fprintf(output, "once "); break;
1058 case XML_REGEXP_QUANT_OPT:
1059 fprintf(output, "? "); break;
1060 case XML_REGEXP_QUANT_MULT:
1061 fprintf(output, "* "); break;
1062 case XML_REGEXP_QUANT_PLUS:
1063 fprintf(output, "+ "); break;
1064 case XML_REGEXP_QUANT_RANGE:
1065 fprintf(output, "range "); break;
Daniel Veillard7646b182002-04-20 06:41:40 +00001066 case XML_REGEXP_QUANT_ONCEONLY:
1067 fprintf(output, "onceonly "); break;
1068 case XML_REGEXP_QUANT_ALL:
1069 fprintf(output, "all "); break;
Daniel Veillard4255d502002-04-16 15:50:10 +00001070 }
1071}
1072static void
1073xmlRegPrintRange(FILE *output, xmlRegRangePtr range) {
1074 fprintf(output, " range: ");
1075 if (range->neg)
1076 fprintf(output, "negative ");
1077 xmlRegPrintAtomType(output, range->type);
1078 fprintf(output, "%c - %c\n", range->start, range->end);
1079}
1080
1081static void
1082xmlRegPrintAtom(FILE *output, xmlRegAtomPtr atom) {
1083 fprintf(output, " atom: ");
1084 if (atom == NULL) {
1085 fprintf(output, "NULL\n");
1086 return;
1087 }
Daniel Veillard9efc4762005-07-19 14:33:55 +00001088 if (atom->neg)
1089 fprintf(output, "not ");
Daniel Veillard4255d502002-04-16 15:50:10 +00001090 xmlRegPrintAtomType(output, atom->type);
1091 xmlRegPrintQuantType(output, atom->quant);
1092 if (atom->quant == XML_REGEXP_QUANT_RANGE)
1093 fprintf(output, "%d-%d ", atom->min, atom->max);
1094 if (atom->type == XML_REGEXP_STRING)
1095 fprintf(output, "'%s' ", (char *) atom->valuep);
1096 if (atom->type == XML_REGEXP_CHARVAL)
1097 fprintf(output, "char %c\n", atom->codepoint);
1098 else if (atom->type == XML_REGEXP_RANGES) {
1099 int i;
1100 fprintf(output, "%d entries\n", atom->nbRanges);
1101 for (i = 0; i < atom->nbRanges;i++)
1102 xmlRegPrintRange(output, atom->ranges[i]);
1103 } else if (atom->type == XML_REGEXP_SUBREG) {
1104 fprintf(output, "start %d end %d\n", atom->start->no, atom->stop->no);
1105 } else {
1106 fprintf(output, "\n");
1107 }
1108}
1109
1110static void
1111xmlRegPrintTrans(FILE *output, xmlRegTransPtr trans) {
1112 fprintf(output, " trans: ");
1113 if (trans == NULL) {
1114 fprintf(output, "NULL\n");
1115 return;
1116 }
1117 if (trans->to < 0) {
1118 fprintf(output, "removed\n");
1119 return;
1120 }
Daniel Veillard567a45b2005-10-18 19:11:55 +00001121 if (trans->nd != 0) {
1122 if (trans->nd == 2)
1123 fprintf(output, "last not determinist, ");
1124 else
1125 fprintf(output, "not determinist, ");
1126 }
Daniel Veillard4255d502002-04-16 15:50:10 +00001127 if (trans->counter >= 0) {
1128 fprintf(output, "counted %d, ", trans->counter);
1129 }
Daniel Veillard8a001f62002-04-20 07:24:11 +00001130 if (trans->count == REGEXP_ALL_COUNTER) {
1131 fprintf(output, "all transition, ");
1132 } else if (trans->count >= 0) {
Daniel Veillard4255d502002-04-16 15:50:10 +00001133 fprintf(output, "count based %d, ", trans->count);
1134 }
1135 if (trans->atom == NULL) {
1136 fprintf(output, "epsilon to %d\n", trans->to);
1137 return;
1138 }
1139 if (trans->atom->type == XML_REGEXP_CHARVAL)
1140 fprintf(output, "char %c ", trans->atom->codepoint);
1141 fprintf(output, "atom %d, to %d\n", trans->atom->no, trans->to);
1142}
1143
1144static void
1145xmlRegPrintState(FILE *output, xmlRegStatePtr state) {
1146 int i;
1147
1148 fprintf(output, " state: ");
1149 if (state == NULL) {
1150 fprintf(output, "NULL\n");
1151 return;
1152 }
1153 if (state->type == XML_REGEXP_START_STATE)
1154 fprintf(output, "START ");
1155 if (state->type == XML_REGEXP_FINAL_STATE)
1156 fprintf(output, "FINAL ");
1157
1158 fprintf(output, "%d, %d transitions:\n", state->no, state->nbTrans);
1159 for (i = 0;i < state->nbTrans; i++) {
1160 xmlRegPrintTrans(output, &(state->trans[i]));
1161 }
1162}
1163
Daniel Veillard23e73572002-09-19 19:56:43 +00001164#ifdef DEBUG_REGEXP_GRAPH
Daniel Veillard4255d502002-04-16 15:50:10 +00001165static void
1166xmlRegPrintCtxt(FILE *output, xmlRegParserCtxtPtr ctxt) {
1167 int i;
1168
1169 fprintf(output, " ctxt: ");
1170 if (ctxt == NULL) {
1171 fprintf(output, "NULL\n");
1172 return;
1173 }
1174 fprintf(output, "'%s' ", ctxt->string);
1175 if (ctxt->error)
1176 fprintf(output, "error ");
1177 if (ctxt->neg)
1178 fprintf(output, "neg ");
1179 fprintf(output, "\n");
1180 fprintf(output, "%d atoms:\n", ctxt->nbAtoms);
1181 for (i = 0;i < ctxt->nbAtoms; i++) {
1182 fprintf(output, " %02d ", i);
1183 xmlRegPrintAtom(output, ctxt->atoms[i]);
1184 }
1185 if (ctxt->atom != NULL) {
1186 fprintf(output, "current atom:\n");
1187 xmlRegPrintAtom(output, ctxt->atom);
1188 }
1189 fprintf(output, "%d states:", ctxt->nbStates);
1190 if (ctxt->start != NULL)
1191 fprintf(output, " start: %d", ctxt->start->no);
1192 if (ctxt->end != NULL)
1193 fprintf(output, " end: %d", ctxt->end->no);
1194 fprintf(output, "\n");
1195 for (i = 0;i < ctxt->nbStates; i++) {
1196 xmlRegPrintState(output, ctxt->states[i]);
1197 }
1198 fprintf(output, "%d counters:\n", ctxt->nbCounters);
1199 for (i = 0;i < ctxt->nbCounters; i++) {
1200 fprintf(output, " %d: min %d max %d\n", i, ctxt->counters[i].min,
1201 ctxt->counters[i].max);
1202 }
1203}
Daniel Veillard23e73572002-09-19 19:56:43 +00001204#endif
Daniel Veillard4255d502002-04-16 15:50:10 +00001205
1206/************************************************************************
1207 * *
1208 * Finite Automata structures manipulations *
1209 * *
1210 ************************************************************************/
1211
1212static void
1213xmlRegAtomAddRange(xmlRegParserCtxtPtr ctxt, xmlRegAtomPtr atom,
1214 int neg, xmlRegAtomType type, int start, int end,
1215 xmlChar *blockName) {
1216 xmlRegRangePtr range;
1217
1218 if (atom == NULL) {
1219 ERROR("add range: atom is NULL");
1220 return;
1221 }
1222 if (atom->type != XML_REGEXP_RANGES) {
1223 ERROR("add range: atom is not ranges");
1224 return;
1225 }
1226 if (atom->maxRanges == 0) {
1227 atom->maxRanges = 4;
1228 atom->ranges = (xmlRegRangePtr *) xmlMalloc(atom->maxRanges *
1229 sizeof(xmlRegRangePtr));
1230 if (atom->ranges == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00001231 xmlRegexpErrMemory(ctxt, "adding ranges");
Daniel Veillard4255d502002-04-16 15:50:10 +00001232 atom->maxRanges = 0;
1233 return;
1234 }
1235 } else if (atom->nbRanges >= atom->maxRanges) {
1236 xmlRegRangePtr *tmp;
1237 atom->maxRanges *= 2;
1238 tmp = (xmlRegRangePtr *) xmlRealloc(atom->ranges, atom->maxRanges *
1239 sizeof(xmlRegRangePtr));
1240 if (tmp == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00001241 xmlRegexpErrMemory(ctxt, "adding ranges");
Daniel Veillard4255d502002-04-16 15:50:10 +00001242 atom->maxRanges /= 2;
1243 return;
1244 }
1245 atom->ranges = tmp;
1246 }
1247 range = xmlRegNewRange(ctxt, neg, type, start, end);
1248 if (range == NULL)
1249 return;
1250 range->blockName = blockName;
1251 atom->ranges[atom->nbRanges++] = range;
1252
1253}
1254
1255static int
1256xmlRegGetCounter(xmlRegParserCtxtPtr ctxt) {
1257 if (ctxt->maxCounters == 0) {
1258 ctxt->maxCounters = 4;
1259 ctxt->counters = (xmlRegCounter *) xmlMalloc(ctxt->maxCounters *
1260 sizeof(xmlRegCounter));
1261 if (ctxt->counters == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00001262 xmlRegexpErrMemory(ctxt, "allocating counter");
Daniel Veillard4255d502002-04-16 15:50:10 +00001263 ctxt->maxCounters = 0;
1264 return(-1);
1265 }
1266 } else if (ctxt->nbCounters >= ctxt->maxCounters) {
1267 xmlRegCounter *tmp;
1268 ctxt->maxCounters *= 2;
1269 tmp = (xmlRegCounter *) xmlRealloc(ctxt->counters, ctxt->maxCounters *
1270 sizeof(xmlRegCounter));
1271 if (tmp == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00001272 xmlRegexpErrMemory(ctxt, "allocating counter");
Daniel Veillard4255d502002-04-16 15:50:10 +00001273 ctxt->maxCounters /= 2;
1274 return(-1);
1275 }
1276 ctxt->counters = tmp;
1277 }
1278 ctxt->counters[ctxt->nbCounters].min = -1;
1279 ctxt->counters[ctxt->nbCounters].max = -1;
1280 return(ctxt->nbCounters++);
1281}
1282
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001283static int
Daniel Veillard4255d502002-04-16 15:50:10 +00001284xmlRegAtomPush(xmlRegParserCtxtPtr ctxt, xmlRegAtomPtr atom) {
1285 if (atom == NULL) {
1286 ERROR("atom push: atom is NULL");
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001287 return(-1);
Daniel Veillard4255d502002-04-16 15:50:10 +00001288 }
1289 if (ctxt->maxAtoms == 0) {
1290 ctxt->maxAtoms = 4;
1291 ctxt->atoms = (xmlRegAtomPtr *) xmlMalloc(ctxt->maxAtoms *
1292 sizeof(xmlRegAtomPtr));
1293 if (ctxt->atoms == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00001294 xmlRegexpErrMemory(ctxt, "pushing atom");
Daniel Veillard4255d502002-04-16 15:50:10 +00001295 ctxt->maxAtoms = 0;
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001296 return(-1);
Daniel Veillard4255d502002-04-16 15:50:10 +00001297 }
1298 } else if (ctxt->nbAtoms >= ctxt->maxAtoms) {
1299 xmlRegAtomPtr *tmp;
1300 ctxt->maxAtoms *= 2;
1301 tmp = (xmlRegAtomPtr *) xmlRealloc(ctxt->atoms, ctxt->maxAtoms *
1302 sizeof(xmlRegAtomPtr));
1303 if (tmp == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00001304 xmlRegexpErrMemory(ctxt, "allocating counter");
Daniel Veillard4255d502002-04-16 15:50:10 +00001305 ctxt->maxAtoms /= 2;
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001306 return(-1);
Daniel Veillard4255d502002-04-16 15:50:10 +00001307 }
1308 ctxt->atoms = tmp;
1309 }
1310 atom->no = ctxt->nbAtoms;
1311 ctxt->atoms[ctxt->nbAtoms++] = atom;
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001312 return(0);
Daniel Veillard4255d502002-04-16 15:50:10 +00001313}
1314
1315static void
Daniel Veillarddb68b742005-07-30 13:18:24 +00001316xmlRegStateAddTransTo(xmlRegParserCtxtPtr ctxt, xmlRegStatePtr target,
1317 int from) {
1318 if (target->maxTransTo == 0) {
1319 target->maxTransTo = 8;
1320 target->transTo = (int *) xmlMalloc(target->maxTransTo *
1321 sizeof(int));
1322 if (target->transTo == NULL) {
1323 xmlRegexpErrMemory(ctxt, "adding transition");
1324 target->maxTransTo = 0;
1325 return;
1326 }
1327 } else if (target->nbTransTo >= target->maxTransTo) {
1328 int *tmp;
1329 target->maxTransTo *= 2;
1330 tmp = (int *) xmlRealloc(target->transTo, target->maxTransTo *
1331 sizeof(int));
1332 if (tmp == NULL) {
1333 xmlRegexpErrMemory(ctxt, "adding transition");
1334 target->maxTransTo /= 2;
1335 return;
1336 }
1337 target->transTo = tmp;
1338 }
1339 target->transTo[target->nbTransTo] = from;
1340 target->nbTransTo++;
1341}
1342
1343static void
Daniel Veillard4255d502002-04-16 15:50:10 +00001344xmlRegStateAddTrans(xmlRegParserCtxtPtr ctxt, xmlRegStatePtr state,
1345 xmlRegAtomPtr atom, xmlRegStatePtr target,
Daniel Veillard5de09382005-09-26 17:18:17 +00001346 int counter, int count) {
William M. Brackf9b5fa22004-05-10 07:52:15 +00001347
1348 int nrtrans;
1349
Daniel Veillard4255d502002-04-16 15:50:10 +00001350 if (state == NULL) {
1351 ERROR("add state: state is NULL");
1352 return;
1353 }
1354 if (target == NULL) {
1355 ERROR("add state: target is NULL");
1356 return;
1357 }
William M. Brackf9b5fa22004-05-10 07:52:15 +00001358 /*
1359 * Other routines follow the philosophy 'When in doubt, add a transition'
1360 * so we check here whether such a transition is already present and, if
1361 * so, silently ignore this request.
1362 */
1363
Daniel Veillard5de09382005-09-26 17:18:17 +00001364 for (nrtrans = state->nbTrans - 1; nrtrans >= 0; nrtrans--) {
1365 xmlRegTransPtr trans = &(state->trans[nrtrans]);
1366 if ((trans->atom == atom) &&
1367 (trans->to == target->no) &&
1368 (trans->counter == counter) &&
1369 (trans->count == count)) {
William M. Brackf9b5fa22004-05-10 07:52:15 +00001370#ifdef DEBUG_REGEXP_GRAPH
Daniel Veillard5de09382005-09-26 17:18:17 +00001371 printf("Ignoring duplicate transition from %d to %d\n",
1372 state->no, target->no);
William M. Brackf9b5fa22004-05-10 07:52:15 +00001373#endif
Daniel Veillard5de09382005-09-26 17:18:17 +00001374 return;
Daniel Veillarddb68b742005-07-30 13:18:24 +00001375 }
William M. Brackf9b5fa22004-05-10 07:52:15 +00001376 }
1377
Daniel Veillard4255d502002-04-16 15:50:10 +00001378 if (state->maxTrans == 0) {
Daniel Veillarddb68b742005-07-30 13:18:24 +00001379 state->maxTrans = 8;
Daniel Veillard4255d502002-04-16 15:50:10 +00001380 state->trans = (xmlRegTrans *) xmlMalloc(state->maxTrans *
1381 sizeof(xmlRegTrans));
1382 if (state->trans == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00001383 xmlRegexpErrMemory(ctxt, "adding transition");
Daniel Veillard4255d502002-04-16 15:50:10 +00001384 state->maxTrans = 0;
1385 return;
1386 }
1387 } else if (state->nbTrans >= state->maxTrans) {
1388 xmlRegTrans *tmp;
1389 state->maxTrans *= 2;
1390 tmp = (xmlRegTrans *) xmlRealloc(state->trans, state->maxTrans *
1391 sizeof(xmlRegTrans));
1392 if (tmp == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00001393 xmlRegexpErrMemory(ctxt, "adding transition");
Daniel Veillard4255d502002-04-16 15:50:10 +00001394 state->maxTrans /= 2;
1395 return;
1396 }
1397 state->trans = tmp;
1398 }
1399#ifdef DEBUG_REGEXP_GRAPH
1400 printf("Add trans from %d to %d ", state->no, target->no);
Daniel Veillard8a001f62002-04-20 07:24:11 +00001401 if (count == REGEXP_ALL_COUNTER)
Daniel Veillard2cbf5962004-03-31 15:50:43 +00001402 printf("all transition\n");
Daniel Veillard4402ab42002-09-12 16:02:56 +00001403 else if (count >= 0)
Daniel Veillard2cbf5962004-03-31 15:50:43 +00001404 printf("count based %d\n", count);
Daniel Veillard4255d502002-04-16 15:50:10 +00001405 else if (counter >= 0)
Daniel Veillard2cbf5962004-03-31 15:50:43 +00001406 printf("counted %d\n", counter);
Daniel Veillard4255d502002-04-16 15:50:10 +00001407 else if (atom == NULL)
Daniel Veillard2cbf5962004-03-31 15:50:43 +00001408 printf("epsilon transition\n");
1409 else if (atom != NULL)
1410 xmlRegPrintAtom(stdout, atom);
Daniel Veillard4255d502002-04-16 15:50:10 +00001411#endif
1412
1413 state->trans[state->nbTrans].atom = atom;
1414 state->trans[state->nbTrans].to = target->no;
1415 state->trans[state->nbTrans].counter = counter;
1416 state->trans[state->nbTrans].count = count;
Daniel Veillard567a45b2005-10-18 19:11:55 +00001417 state->trans[state->nbTrans].nd = 0;
Daniel Veillard4255d502002-04-16 15:50:10 +00001418 state->nbTrans++;
Daniel Veillarddb68b742005-07-30 13:18:24 +00001419 xmlRegStateAddTransTo(ctxt, target, state->no);
Daniel Veillard4255d502002-04-16 15:50:10 +00001420}
1421
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001422static int
Daniel Veillard4255d502002-04-16 15:50:10 +00001423xmlRegStatePush(xmlRegParserCtxtPtr ctxt, xmlRegStatePtr state) {
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001424 if (state == NULL) return(-1);
Daniel Veillard4255d502002-04-16 15:50:10 +00001425 if (ctxt->maxStates == 0) {
1426 ctxt->maxStates = 4;
1427 ctxt->states = (xmlRegStatePtr *) xmlMalloc(ctxt->maxStates *
1428 sizeof(xmlRegStatePtr));
1429 if (ctxt->states == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00001430 xmlRegexpErrMemory(ctxt, "adding state");
Daniel Veillard4255d502002-04-16 15:50:10 +00001431 ctxt->maxStates = 0;
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001432 return(-1);
Daniel Veillard4255d502002-04-16 15:50:10 +00001433 }
1434 } else if (ctxt->nbStates >= ctxt->maxStates) {
1435 xmlRegStatePtr *tmp;
1436 ctxt->maxStates *= 2;
1437 tmp = (xmlRegStatePtr *) xmlRealloc(ctxt->states, ctxt->maxStates *
1438 sizeof(xmlRegStatePtr));
1439 if (tmp == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00001440 xmlRegexpErrMemory(ctxt, "adding state");
Daniel Veillard4255d502002-04-16 15:50:10 +00001441 ctxt->maxStates /= 2;
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001442 return(-1);
Daniel Veillard4255d502002-04-16 15:50:10 +00001443 }
1444 ctxt->states = tmp;
1445 }
1446 state->no = ctxt->nbStates;
1447 ctxt->states[ctxt->nbStates++] = state;
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001448 return(0);
Daniel Veillard4255d502002-04-16 15:50:10 +00001449}
1450
1451/**
Daniel Veillard7646b182002-04-20 06:41:40 +00001452 * xmlFAGenerateAllTransition:
Daniel Veillard441bc322002-04-20 17:38:48 +00001453 * @ctxt: a regexp parser context
1454 * @from: the from state
1455 * @to: the target state or NULL for building a new one
1456 * @lax:
Daniel Veillard7646b182002-04-20 06:41:40 +00001457 *
1458 */
1459static void
1460xmlFAGenerateAllTransition(xmlRegParserCtxtPtr ctxt,
Daniel Veillard441bc322002-04-20 17:38:48 +00001461 xmlRegStatePtr from, xmlRegStatePtr to,
1462 int lax) {
Daniel Veillard7646b182002-04-20 06:41:40 +00001463 if (to == NULL) {
1464 to = xmlRegNewState(ctxt);
1465 xmlRegStatePush(ctxt, to);
1466 ctxt->state = to;
1467 }
Daniel Veillard441bc322002-04-20 17:38:48 +00001468 if (lax)
Daniel Veillard5de09382005-09-26 17:18:17 +00001469 xmlRegStateAddTrans(ctxt, from, NULL, to, -1, REGEXP_ALL_LAX_COUNTER);
Daniel Veillard441bc322002-04-20 17:38:48 +00001470 else
Daniel Veillard5de09382005-09-26 17:18:17 +00001471 xmlRegStateAddTrans(ctxt, from, NULL, to, -1, REGEXP_ALL_COUNTER);
Daniel Veillard7646b182002-04-20 06:41:40 +00001472}
1473
1474/**
Daniel Veillard4255d502002-04-16 15:50:10 +00001475 * xmlFAGenerateEpsilonTransition:
Daniel Veillard441bc322002-04-20 17:38:48 +00001476 * @ctxt: a regexp parser context
1477 * @from: the from state
1478 * @to: the target state or NULL for building a new one
Daniel Veillard4255d502002-04-16 15:50:10 +00001479 *
1480 */
1481static void
1482xmlFAGenerateEpsilonTransition(xmlRegParserCtxtPtr ctxt,
1483 xmlRegStatePtr from, xmlRegStatePtr to) {
1484 if (to == NULL) {
1485 to = xmlRegNewState(ctxt);
1486 xmlRegStatePush(ctxt, to);
1487 ctxt->state = to;
1488 }
Daniel Veillard5de09382005-09-26 17:18:17 +00001489 xmlRegStateAddTrans(ctxt, from, NULL, to, -1, -1);
Daniel Veillard4255d502002-04-16 15:50:10 +00001490}
1491
1492/**
1493 * xmlFAGenerateCountedEpsilonTransition:
Daniel Veillard441bc322002-04-20 17:38:48 +00001494 * @ctxt: a regexp parser context
1495 * @from: the from state
1496 * @to: the target state or NULL for building a new one
Daniel Veillard4255d502002-04-16 15:50:10 +00001497 * counter: the counter for that transition
1498 *
1499 */
1500static void
1501xmlFAGenerateCountedEpsilonTransition(xmlRegParserCtxtPtr ctxt,
1502 xmlRegStatePtr from, xmlRegStatePtr to, int counter) {
1503 if (to == NULL) {
1504 to = xmlRegNewState(ctxt);
1505 xmlRegStatePush(ctxt, to);
1506 ctxt->state = to;
1507 }
Daniel Veillard5de09382005-09-26 17:18:17 +00001508 xmlRegStateAddTrans(ctxt, from, NULL, to, counter, -1);
Daniel Veillard4255d502002-04-16 15:50:10 +00001509}
1510
1511/**
1512 * xmlFAGenerateCountedTransition:
Daniel Veillard441bc322002-04-20 17:38:48 +00001513 * @ctxt: a regexp parser context
1514 * @from: the from state
1515 * @to: the target state or NULL for building a new one
Daniel Veillard4255d502002-04-16 15:50:10 +00001516 * counter: the counter for that transition
1517 *
1518 */
1519static void
1520xmlFAGenerateCountedTransition(xmlRegParserCtxtPtr ctxt,
1521 xmlRegStatePtr from, xmlRegStatePtr to, int counter) {
1522 if (to == NULL) {
1523 to = xmlRegNewState(ctxt);
1524 xmlRegStatePush(ctxt, to);
1525 ctxt->state = to;
1526 }
Daniel Veillard5de09382005-09-26 17:18:17 +00001527 xmlRegStateAddTrans(ctxt, from, NULL, to, -1, counter);
Daniel Veillard4255d502002-04-16 15:50:10 +00001528}
1529
1530/**
1531 * xmlFAGenerateTransitions:
Daniel Veillard441bc322002-04-20 17:38:48 +00001532 * @ctxt: a regexp parser context
1533 * @from: the from state
1534 * @to: the target state or NULL for building a new one
1535 * @atom: the atom generating the transition
Daniel Veillard4255d502002-04-16 15:50:10 +00001536 *
William M. Brackddf71d62004-05-06 04:17:26 +00001537 * Returns 0 if success and -1 in case of error.
Daniel Veillard4255d502002-04-16 15:50:10 +00001538 */
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001539static int
Daniel Veillard4255d502002-04-16 15:50:10 +00001540xmlFAGenerateTransitions(xmlRegParserCtxtPtr ctxt, xmlRegStatePtr from,
1541 xmlRegStatePtr to, xmlRegAtomPtr atom) {
Daniel Veillard10bda622008-03-13 07:27:24 +00001542 xmlRegStatePtr end;
1543
Daniel Veillard4255d502002-04-16 15:50:10 +00001544 if (atom == NULL) {
1545 ERROR("genrate transition: atom == NULL");
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001546 return(-1);
Daniel Veillard4255d502002-04-16 15:50:10 +00001547 }
1548 if (atom->type == XML_REGEXP_SUBREG) {
1549 /*
1550 * this is a subexpression handling one should not need to
William M. Brackddf71d62004-05-06 04:17:26 +00001551 * create a new node except for XML_REGEXP_QUANT_RANGE.
Daniel Veillard4255d502002-04-16 15:50:10 +00001552 */
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001553 if (xmlRegAtomPush(ctxt, atom) < 0) {
1554 return(-1);
1555 }
Daniel Veillard4255d502002-04-16 15:50:10 +00001556 if ((to != NULL) && (atom->stop != to) &&
1557 (atom->quant != XML_REGEXP_QUANT_RANGE)) {
1558 /*
1559 * Generate an epsilon transition to link to the target
1560 */
1561 xmlFAGenerateEpsilonTransition(ctxt, atom->stop, to);
Daniel Veillardaa622012005-10-20 15:55:25 +00001562#ifdef DV
1563 } else if ((to == NULL) && (atom->quant != XML_REGEXP_QUANT_RANGE) &&
1564 (atom->quant != XML_REGEXP_QUANT_ONCE)) {
1565 to = xmlRegNewState(ctxt);
1566 xmlRegStatePush(ctxt, to);
1567 ctxt->state = to;
1568 xmlFAGenerateEpsilonTransition(ctxt, atom->stop, to);
1569#endif
Daniel Veillard4255d502002-04-16 15:50:10 +00001570 }
1571 switch (atom->quant) {
1572 case XML_REGEXP_QUANT_OPT:
1573 atom->quant = XML_REGEXP_QUANT_ONCE;
Daniel Veillard54eb0242006-03-21 23:17:57 +00001574 /*
1575 * transition done to the state after end of atom.
1576 * 1. set transition from atom start to new state
1577 * 2. set transition from atom end to this state.
1578 */
Daniel Veillardd80d0722009-08-22 18:56:01 +02001579 if (to == NULL) {
1580 xmlFAGenerateEpsilonTransition(ctxt, atom->start, 0);
1581 xmlFAGenerateEpsilonTransition(ctxt, atom->stop,
1582 ctxt->state);
1583 } else {
1584 xmlFAGenerateEpsilonTransition(ctxt, atom->start, to);
1585 }
Daniel Veillard4255d502002-04-16 15:50:10 +00001586 break;
1587 case XML_REGEXP_QUANT_MULT:
1588 atom->quant = XML_REGEXP_QUANT_ONCE;
1589 xmlFAGenerateEpsilonTransition(ctxt, atom->start, atom->stop);
1590 xmlFAGenerateEpsilonTransition(ctxt, atom->stop, atom->start);
1591 break;
1592 case XML_REGEXP_QUANT_PLUS:
1593 atom->quant = XML_REGEXP_QUANT_ONCE;
1594 xmlFAGenerateEpsilonTransition(ctxt, atom->stop, atom->start);
1595 break;
1596 case XML_REGEXP_QUANT_RANGE: {
1597 int counter;
Daniel Veillard76d59b62007-08-22 16:29:21 +00001598 xmlRegStatePtr inter, newstate;
Daniel Veillard4255d502002-04-16 15:50:10 +00001599
1600 /*
Daniel Veillard76d59b62007-08-22 16:29:21 +00001601 * create the final state now if needed
Daniel Veillard4255d502002-04-16 15:50:10 +00001602 */
Daniel Veillard4255d502002-04-16 15:50:10 +00001603 if (to != NULL) {
1604 newstate = to;
1605 } else {
1606 newstate = xmlRegNewState(ctxt);
1607 xmlRegStatePush(ctxt, newstate);
Daniel Veillard4255d502002-04-16 15:50:10 +00001608 }
Daniel Veillard76d59b62007-08-22 16:29:21 +00001609
1610 /*
1611 * The principle here is to use counted transition
1612 * to avoid explosion in the number of states in the
1613 * graph. This is clearly more complex but should not
1614 * be exploitable at runtime.
Daniel Veillard54eb0242006-03-21 23:17:57 +00001615 */
Daniel Veillard76d59b62007-08-22 16:29:21 +00001616 if ((atom->min == 0) && (atom->start0 == NULL)) {
1617 xmlRegAtomPtr copy;
1618 /*
1619 * duplicate a transition based on atom to count next
1620 * occurences after 1. We cannot loop to atom->start
1621 * directly because we need an epsilon transition to
1622 * newstate.
1623 */
1624 /* ???? For some reason it seems we never reach that
1625 case, I suppose this got optimized out before when
1626 building the automata */
Daniel Veillardc821e032007-08-28 17:33:45 +00001627 copy = xmlRegCopyAtom(ctxt, atom);
Daniel Veillard76d59b62007-08-22 16:29:21 +00001628 if (copy == NULL)
1629 return(-1);
Daniel Veillard76d59b62007-08-22 16:29:21 +00001630 copy->quant = XML_REGEXP_QUANT_ONCE;
1631 copy->min = 0;
1632 copy->max = 0;
1633
1634 if (xmlFAGenerateTransitions(ctxt, atom->start, NULL, copy)
1635 < 0)
1636 return(-1);
1637 inter = ctxt->state;
1638 counter = xmlRegGetCounter(ctxt);
1639 ctxt->counters[counter].min = atom->min - 1;
1640 ctxt->counters[counter].max = atom->max - 1;
1641 /* count the number of times we see it again */
1642 xmlFAGenerateCountedEpsilonTransition(ctxt, inter,
1643 atom->stop, counter);
1644 /* allow a way out based on the count */
1645 xmlFAGenerateCountedTransition(ctxt, inter,
1646 newstate, counter);
1647 /* and also allow a direct exit for 0 */
1648 xmlFAGenerateEpsilonTransition(ctxt, atom->start,
1649 newstate);
1650 } else {
1651 /*
1652 * either we need the atom at least once or there
1653 * is an atom->start0 allowing to easilly plug the
1654 * epsilon transition.
1655 */
1656 counter = xmlRegGetCounter(ctxt);
1657 ctxt->counters[counter].min = atom->min - 1;
1658 ctxt->counters[counter].max = atom->max - 1;
1659 /* count the number of times we see it again */
1660 xmlFAGenerateCountedEpsilonTransition(ctxt, atom->stop,
1661 atom->start, counter);
1662 /* allow a way out based on the count */
1663 xmlFAGenerateCountedTransition(ctxt, atom->stop,
1664 newstate, counter);
1665 /* and if needed allow a direct exit for 0 */
1666 if (atom->min == 0)
1667 xmlFAGenerateEpsilonTransition(ctxt, atom->start0,
1668 newstate);
1669
1670 }
1671 atom->min = 0;
1672 atom->max = 0;
1673 atom->quant = XML_REGEXP_QUANT_ONCE;
1674 ctxt->state = newstate;
Daniel Veillard4255d502002-04-16 15:50:10 +00001675 }
1676 default:
1677 break;
1678 }
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001679 return(0);
Daniel Veillard567a45b2005-10-18 19:11:55 +00001680 }
1681 if ((atom->min == 0) && (atom->max == 0) &&
Daniel Veillard99c394d2005-07-14 12:58:49 +00001682 (atom->quant == XML_REGEXP_QUANT_RANGE)) {
1683 /*
1684 * we can discard the atom and generate an epsilon transition instead
1685 */
1686 if (to == NULL) {
1687 to = xmlRegNewState(ctxt);
1688 if (to != NULL)
1689 xmlRegStatePush(ctxt, to);
1690 else {
1691 return(-1);
1692 }
1693 }
1694 xmlFAGenerateEpsilonTransition(ctxt, from, to);
1695 ctxt->state = to;
1696 xmlRegFreeAtom(atom);
1697 return(0);
Daniel Veillard567a45b2005-10-18 19:11:55 +00001698 }
1699 if (to == NULL) {
1700 to = xmlRegNewState(ctxt);
1701 if (to != NULL)
1702 xmlRegStatePush(ctxt, to);
1703 else {
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001704 return(-1);
Daniel Veillard4255d502002-04-16 15:50:10 +00001705 }
Daniel Veillard10bda622008-03-13 07:27:24 +00001706 }
1707 end = to;
1708 if ((atom->quant == XML_REGEXP_QUANT_MULT) ||
1709 (atom->quant == XML_REGEXP_QUANT_PLUS)) {
1710 /*
1711 * Do not pollute the target state by adding transitions from
1712 * it as it is likely to be the shared target of multiple branches.
1713 * So isolate with an epsilon transition.
1714 */
1715 xmlRegStatePtr tmp;
1716
1717 tmp = xmlRegNewState(ctxt);
1718 if (tmp != NULL)
1719 xmlRegStatePush(ctxt, tmp);
1720 else {
1721 return(-1);
1722 }
1723 xmlFAGenerateEpsilonTransition(ctxt, tmp, to);
1724 to = tmp;
Daniel Veillard4255d502002-04-16 15:50:10 +00001725 }
Daniel Veillard567a45b2005-10-18 19:11:55 +00001726 if (xmlRegAtomPush(ctxt, atom) < 0) {
1727 return(-1);
1728 }
1729 xmlRegStateAddTrans(ctxt, from, atom, to, -1, -1);
Daniel Veillard10bda622008-03-13 07:27:24 +00001730 ctxt->state = end;
Daniel Veillard4255d502002-04-16 15:50:10 +00001731 switch (atom->quant) {
1732 case XML_REGEXP_QUANT_OPT:
1733 atom->quant = XML_REGEXP_QUANT_ONCE;
1734 xmlFAGenerateEpsilonTransition(ctxt, from, to);
1735 break;
1736 case XML_REGEXP_QUANT_MULT:
1737 atom->quant = XML_REGEXP_QUANT_ONCE;
1738 xmlFAGenerateEpsilonTransition(ctxt, from, to);
Daniel Veillard5de09382005-09-26 17:18:17 +00001739 xmlRegStateAddTrans(ctxt, to, atom, to, -1, -1);
Daniel Veillard4255d502002-04-16 15:50:10 +00001740 break;
1741 case XML_REGEXP_QUANT_PLUS:
1742 atom->quant = XML_REGEXP_QUANT_ONCE;
Daniel Veillard5de09382005-09-26 17:18:17 +00001743 xmlRegStateAddTrans(ctxt, to, atom, to, -1, -1);
Daniel Veillard4255d502002-04-16 15:50:10 +00001744 break;
William M. Brack56578372007-04-11 14:33:46 +00001745 case XML_REGEXP_QUANT_RANGE:
Daniel Veillardc821e032007-08-28 17:33:45 +00001746#if DV_test
William M. Brack56578372007-04-11 14:33:46 +00001747 if (atom->min == 0) {
1748 xmlFAGenerateEpsilonTransition(ctxt, from, to);
1749 }
Daniel Veillardc821e032007-08-28 17:33:45 +00001750#endif
William M. Brack56578372007-04-11 14:33:46 +00001751 break;
Daniel Veillard4255d502002-04-16 15:50:10 +00001752 default:
1753 break;
1754 }
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001755 return(0);
Daniel Veillard4255d502002-04-16 15:50:10 +00001756}
1757
1758/**
1759 * xmlFAReduceEpsilonTransitions:
Daniel Veillard441bc322002-04-20 17:38:48 +00001760 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00001761 * @fromnr: the from state
1762 * @tonr: the to state
William M. Brackddf71d62004-05-06 04:17:26 +00001763 * @counter: should that transition be associated to a counted
Daniel Veillard4255d502002-04-16 15:50:10 +00001764 *
1765 */
1766static void
1767xmlFAReduceEpsilonTransitions(xmlRegParserCtxtPtr ctxt, int fromnr,
1768 int tonr, int counter) {
1769 int transnr;
1770 xmlRegStatePtr from;
1771 xmlRegStatePtr to;
1772
1773#ifdef DEBUG_REGEXP_GRAPH
1774 printf("xmlFAReduceEpsilonTransitions(%d, %d)\n", fromnr, tonr);
1775#endif
1776 from = ctxt->states[fromnr];
1777 if (from == NULL)
1778 return;
1779 to = ctxt->states[tonr];
1780 if (to == NULL)
1781 return;
1782 if ((to->mark == XML_REGEXP_MARK_START) ||
1783 (to->mark == XML_REGEXP_MARK_VISITED))
1784 return;
1785
1786 to->mark = XML_REGEXP_MARK_VISITED;
1787 if (to->type == XML_REGEXP_FINAL_STATE) {
1788#ifdef DEBUG_REGEXP_GRAPH
1789 printf("State %d is final, so %d becomes final\n", tonr, fromnr);
1790#endif
1791 from->type = XML_REGEXP_FINAL_STATE;
1792 }
1793 for (transnr = 0;transnr < to->nbTrans;transnr++) {
Daniel Veillarddb68b742005-07-30 13:18:24 +00001794 if (to->trans[transnr].to < 0)
1795 continue;
Daniel Veillard4255d502002-04-16 15:50:10 +00001796 if (to->trans[transnr].atom == NULL) {
1797 /*
1798 * Don't remove counted transitions
1799 * Don't loop either
1800 */
Daniel Veillardb509f152002-04-17 16:28:10 +00001801 if (to->trans[transnr].to != fromnr) {
1802 if (to->trans[transnr].count >= 0) {
1803 int newto = to->trans[transnr].to;
1804
1805 xmlRegStateAddTrans(ctxt, from, NULL,
1806 ctxt->states[newto],
Daniel Veillard5de09382005-09-26 17:18:17 +00001807 -1, to->trans[transnr].count);
Daniel Veillardb509f152002-04-17 16:28:10 +00001808 } else {
Daniel Veillard4255d502002-04-16 15:50:10 +00001809#ifdef DEBUG_REGEXP_GRAPH
Daniel Veillardb509f152002-04-17 16:28:10 +00001810 printf("Found epsilon trans %d from %d to %d\n",
1811 transnr, tonr, to->trans[transnr].to);
Daniel Veillard4255d502002-04-16 15:50:10 +00001812#endif
Daniel Veillardb509f152002-04-17 16:28:10 +00001813 if (to->trans[transnr].counter >= 0) {
1814 xmlFAReduceEpsilonTransitions(ctxt, fromnr,
1815 to->trans[transnr].to,
1816 to->trans[transnr].counter);
1817 } else {
1818 xmlFAReduceEpsilonTransitions(ctxt, fromnr,
1819 to->trans[transnr].to,
1820 counter);
1821 }
1822 }
Daniel Veillard4255d502002-04-16 15:50:10 +00001823 }
1824 } else {
1825 int newto = to->trans[transnr].to;
1826
Daniel Veillardb509f152002-04-17 16:28:10 +00001827 if (to->trans[transnr].counter >= 0) {
1828 xmlRegStateAddTrans(ctxt, from, to->trans[transnr].atom,
1829 ctxt->states[newto],
Daniel Veillard5de09382005-09-26 17:18:17 +00001830 to->trans[transnr].counter, -1);
Daniel Veillardb509f152002-04-17 16:28:10 +00001831 } else {
1832 xmlRegStateAddTrans(ctxt, from, to->trans[transnr].atom,
Daniel Veillard5de09382005-09-26 17:18:17 +00001833 ctxt->states[newto], counter, -1);
Daniel Veillardb509f152002-04-17 16:28:10 +00001834 }
Daniel Veillard4255d502002-04-16 15:50:10 +00001835 }
1836 }
1837 to->mark = XML_REGEXP_MARK_NORMAL;
1838}
1839
1840/**
Daniel Veillarddb68b742005-07-30 13:18:24 +00001841 * xmlFAEliminateSimpleEpsilonTransitions:
1842 * @ctxt: a regexp parser context
1843 *
1844 * Eliminating general epsilon transitions can get costly in the general
1845 * algorithm due to the large amount of generated new transitions and
1846 * associated comparisons. However for simple epsilon transition used just
1847 * to separate building blocks when generating the automata this can be
1848 * reduced to state elimination:
1849 * - if there exists an epsilon from X to Y
1850 * - if there is no other transition from X
1851 * then X and Y are semantically equivalent and X can be eliminated
1852 * If X is the start state then make Y the start state, else replace the
1853 * target of all transitions to X by transitions to Y.
1854 */
1855static void
1856xmlFAEliminateSimpleEpsilonTransitions(xmlRegParserCtxtPtr ctxt) {
1857 int statenr, i, j, newto;
1858 xmlRegStatePtr state, tmp;
1859
1860 for (statenr = 0;statenr < ctxt->nbStates;statenr++) {
1861 state = ctxt->states[statenr];
1862 if (state == NULL)
1863 continue;
1864 if (state->nbTrans != 1)
1865 continue;
Daniel Veillard0e05f4c2006-11-01 15:33:04 +00001866 if (state->type == XML_REGEXP_UNREACH_STATE)
1867 continue;
Daniel Veillarddb68b742005-07-30 13:18:24 +00001868 /* is the only transition out a basic transition */
1869 if ((state->trans[0].atom == NULL) &&
1870 (state->trans[0].to >= 0) &&
1871 (state->trans[0].to != statenr) &&
1872 (state->trans[0].counter < 0) &&
1873 (state->trans[0].count < 0)) {
1874 newto = state->trans[0].to;
1875
1876 if (state->type == XML_REGEXP_START_STATE) {
1877#ifdef DEBUG_REGEXP_GRAPH
1878 printf("Found simple epsilon trans from start %d to %d\n",
1879 statenr, newto);
1880#endif
1881 } else {
1882#ifdef DEBUG_REGEXP_GRAPH
1883 printf("Found simple epsilon trans from %d to %d\n",
1884 statenr, newto);
1885#endif
1886 for (i = 0;i < state->nbTransTo;i++) {
1887 tmp = ctxt->states[state->transTo[i]];
1888 for (j = 0;j < tmp->nbTrans;j++) {
1889 if (tmp->trans[j].to == statenr) {
Daniel Veillarddb68b742005-07-30 13:18:24 +00001890#ifdef DEBUG_REGEXP_GRAPH
1891 printf("Changed transition %d on %d to go to %d\n",
1892 j, tmp->no, newto);
1893#endif
Daniel Veillard0e05f4c2006-11-01 15:33:04 +00001894 tmp->trans[j].to = -1;
1895 xmlRegStateAddTrans(ctxt, tmp, tmp->trans[j].atom,
1896 ctxt->states[newto],
1897 tmp->trans[j].counter,
1898 tmp->trans[j].count);
Daniel Veillarddb68b742005-07-30 13:18:24 +00001899 }
1900 }
1901 }
Daniel Veillarddb68b742005-07-30 13:18:24 +00001902 if (state->type == XML_REGEXP_FINAL_STATE)
1903 ctxt->states[newto]->type = XML_REGEXP_FINAL_STATE;
1904 /* eliminate the transition completely */
1905 state->nbTrans = 0;
1906
Daniel Veillard0e05f4c2006-11-01 15:33:04 +00001907 state->type = XML_REGEXP_UNREACH_STATE;
Daniel Veillarddb68b742005-07-30 13:18:24 +00001908
1909 }
1910
1911 }
1912 }
1913}
1914/**
Daniel Veillard4255d502002-04-16 15:50:10 +00001915 * xmlFAEliminateEpsilonTransitions:
Daniel Veillard441bc322002-04-20 17:38:48 +00001916 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00001917 *
1918 */
1919static void
1920xmlFAEliminateEpsilonTransitions(xmlRegParserCtxtPtr ctxt) {
1921 int statenr, transnr;
1922 xmlRegStatePtr state;
Daniel Veillarddb68b742005-07-30 13:18:24 +00001923 int has_epsilon;
Daniel Veillard4255d502002-04-16 15:50:10 +00001924
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001925 if (ctxt->states == NULL) return;
1926
Daniel Veillard0e05f4c2006-11-01 15:33:04 +00001927 /*
1928 * Eliminate simple epsilon transition and the associated unreachable
1929 * states.
1930 */
Daniel Veillarddb68b742005-07-30 13:18:24 +00001931 xmlFAEliminateSimpleEpsilonTransitions(ctxt);
Daniel Veillard0e05f4c2006-11-01 15:33:04 +00001932 for (statenr = 0;statenr < ctxt->nbStates;statenr++) {
1933 state = ctxt->states[statenr];
1934 if ((state != NULL) && (state->type == XML_REGEXP_UNREACH_STATE)) {
1935#ifdef DEBUG_REGEXP_GRAPH
1936 printf("Removed unreachable state %d\n", statenr);
1937#endif
1938 xmlRegFreeState(state);
1939 ctxt->states[statenr] = NULL;
1940 }
1941 }
Daniel Veillarddb68b742005-07-30 13:18:24 +00001942
1943 has_epsilon = 0;
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001944
Daniel Veillard4255d502002-04-16 15:50:10 +00001945 /*
Daniel Veillardfcd18ff2006-11-02 10:28:04 +00001946 * Build the completed transitions bypassing the epsilons
Daniel Veillard4255d502002-04-16 15:50:10 +00001947 * Use a marking algorithm to avoid loops
Daniel Veillardfcd18ff2006-11-02 10:28:04 +00001948 * Mark sink states too.
1949 * Process from the latests states backward to the start when
1950 * there is long cascading epsilon chains this minimize the
1951 * recursions and transition compares when adding the new ones
Daniel Veillard4255d502002-04-16 15:50:10 +00001952 */
Daniel Veillardfcd18ff2006-11-02 10:28:04 +00001953 for (statenr = ctxt->nbStates - 1;statenr >= 0;statenr--) {
Daniel Veillard4255d502002-04-16 15:50:10 +00001954 state = ctxt->states[statenr];
1955 if (state == NULL)
1956 continue;
Daniel Veillardcc026dc2005-01-12 13:21:17 +00001957 if ((state->nbTrans == 0) &&
1958 (state->type != XML_REGEXP_FINAL_STATE)) {
1959 state->type = XML_REGEXP_SINK_STATE;
1960 }
Daniel Veillard4255d502002-04-16 15:50:10 +00001961 for (transnr = 0;transnr < state->nbTrans;transnr++) {
1962 if ((state->trans[transnr].atom == NULL) &&
1963 (state->trans[transnr].to >= 0)) {
1964 if (state->trans[transnr].to == statenr) {
1965 state->trans[transnr].to = -1;
1966#ifdef DEBUG_REGEXP_GRAPH
1967 printf("Removed loopback epsilon trans %d on %d\n",
1968 transnr, statenr);
1969#endif
1970 } else if (state->trans[transnr].count < 0) {
1971 int newto = state->trans[transnr].to;
1972
1973#ifdef DEBUG_REGEXP_GRAPH
1974 printf("Found epsilon trans %d from %d to %d\n",
1975 transnr, statenr, newto);
1976#endif
Daniel Veillarddb68b742005-07-30 13:18:24 +00001977 has_epsilon = 1;
Daniel Veillard0e05f4c2006-11-01 15:33:04 +00001978 state->trans[transnr].to = -2;
1979 state->mark = XML_REGEXP_MARK_START;
Daniel Veillard4255d502002-04-16 15:50:10 +00001980 xmlFAReduceEpsilonTransitions(ctxt, statenr,
1981 newto, state->trans[transnr].counter);
1982 state->mark = XML_REGEXP_MARK_NORMAL;
1983#ifdef DEBUG_REGEXP_GRAPH
1984 } else {
1985 printf("Found counted transition %d on %d\n",
1986 transnr, statenr);
1987#endif
1988 }
1989 }
1990 }
1991 }
1992 /*
1993 * Eliminate the epsilon transitions
1994 */
Daniel Veillarddb68b742005-07-30 13:18:24 +00001995 if (has_epsilon) {
1996 for (statenr = 0;statenr < ctxt->nbStates;statenr++) {
1997 state = ctxt->states[statenr];
1998 if (state == NULL)
1999 continue;
2000 for (transnr = 0;transnr < state->nbTrans;transnr++) {
2001 xmlRegTransPtr trans = &(state->trans[transnr]);
2002 if ((trans->atom == NULL) &&
2003 (trans->count < 0) &&
2004 (trans->to >= 0)) {
2005 trans->to = -1;
2006 }
Daniel Veillard4255d502002-04-16 15:50:10 +00002007 }
2008 }
2009 }
Daniel Veillard23e73572002-09-19 19:56:43 +00002010
2011 /*
2012 * Use this pass to detect unreachable states too
2013 */
2014 for (statenr = 0;statenr < ctxt->nbStates;statenr++) {
2015 state = ctxt->states[statenr];
2016 if (state != NULL)
William M. Brack779af002003-08-01 15:55:39 +00002017 state->reached = XML_REGEXP_MARK_NORMAL;
Daniel Veillard23e73572002-09-19 19:56:43 +00002018 }
2019 state = ctxt->states[0];
2020 if (state != NULL)
William M. Brack779af002003-08-01 15:55:39 +00002021 state->reached = XML_REGEXP_MARK_START;
Daniel Veillard23e73572002-09-19 19:56:43 +00002022 while (state != NULL) {
2023 xmlRegStatePtr target = NULL;
William M. Brack779af002003-08-01 15:55:39 +00002024 state->reached = XML_REGEXP_MARK_VISITED;
Daniel Veillard23e73572002-09-19 19:56:43 +00002025 /*
William M. Brackddf71d62004-05-06 04:17:26 +00002026 * Mark all states reachable from the current reachable state
Daniel Veillard23e73572002-09-19 19:56:43 +00002027 */
2028 for (transnr = 0;transnr < state->nbTrans;transnr++) {
2029 if ((state->trans[transnr].to >= 0) &&
2030 ((state->trans[transnr].atom != NULL) ||
2031 (state->trans[transnr].count >= 0))) {
2032 int newto = state->trans[transnr].to;
2033
2034 if (ctxt->states[newto] == NULL)
2035 continue;
William M. Brack779af002003-08-01 15:55:39 +00002036 if (ctxt->states[newto]->reached == XML_REGEXP_MARK_NORMAL) {
2037 ctxt->states[newto]->reached = XML_REGEXP_MARK_START;
Daniel Veillard23e73572002-09-19 19:56:43 +00002038 target = ctxt->states[newto];
2039 }
2040 }
2041 }
Daniel Veillardcc026dc2005-01-12 13:21:17 +00002042
Daniel Veillard23e73572002-09-19 19:56:43 +00002043 /*
2044 * find the next accessible state not explored
2045 */
2046 if (target == NULL) {
2047 for (statenr = 1;statenr < ctxt->nbStates;statenr++) {
2048 state = ctxt->states[statenr];
William M. Brack779af002003-08-01 15:55:39 +00002049 if ((state != NULL) && (state->reached ==
2050 XML_REGEXP_MARK_START)) {
Daniel Veillard23e73572002-09-19 19:56:43 +00002051 target = state;
2052 break;
2053 }
2054 }
2055 }
2056 state = target;
2057 }
2058 for (statenr = 0;statenr < ctxt->nbStates;statenr++) {
2059 state = ctxt->states[statenr];
William M. Brack779af002003-08-01 15:55:39 +00002060 if ((state != NULL) && (state->reached == XML_REGEXP_MARK_NORMAL)) {
Daniel Veillard23e73572002-09-19 19:56:43 +00002061#ifdef DEBUG_REGEXP_GRAPH
2062 printf("Removed unreachable state %d\n", statenr);
2063#endif
2064 xmlRegFreeState(state);
2065 ctxt->states[statenr] = NULL;
2066 }
2067 }
2068
Daniel Veillard4255d502002-04-16 15:50:10 +00002069}
2070
Daniel Veillard567a45b2005-10-18 19:11:55 +00002071static int
2072xmlFACompareRanges(xmlRegRangePtr range1, xmlRegRangePtr range2) {
2073 int ret = 0;
2074
2075 if ((range1->type == XML_REGEXP_RANGES) ||
2076 (range2->type == XML_REGEXP_RANGES) ||
2077 (range2->type == XML_REGEXP_SUBREG) ||
2078 (range1->type == XML_REGEXP_SUBREG) ||
2079 (range1->type == XML_REGEXP_STRING) ||
2080 (range2->type == XML_REGEXP_STRING))
2081 return(-1);
2082
2083 /* put them in order */
2084 if (range1->type > range2->type) {
2085 xmlRegRangePtr tmp;
2086
2087 tmp = range1;
2088 range1 = range2;
2089 range2 = tmp;
2090 }
2091 if ((range1->type == XML_REGEXP_ANYCHAR) ||
2092 (range2->type == XML_REGEXP_ANYCHAR)) {
2093 ret = 1;
2094 } else if ((range1->type == XML_REGEXP_EPSILON) ||
2095 (range2->type == XML_REGEXP_EPSILON)) {
2096 return(0);
2097 } else if (range1->type == range2->type) {
2098 if ((range1->type != XML_REGEXP_CHARVAL) ||
2099 (range1->end < range2->start) ||
2100 (range2->end < range1->start))
2101 ret = 1;
2102 else
2103 ret = 0;
2104 } else if (range1->type == XML_REGEXP_CHARVAL) {
2105 int codepoint;
2106 int neg = 0;
2107
2108 /*
2109 * just check all codepoints in the range for acceptance,
2110 * this is usually way cheaper since done only once at
2111 * compilation than testing over and over at runtime or
2112 * pushing too many states when evaluating.
2113 */
2114 if (((range1->neg == 0) && (range2->neg != 0)) ||
2115 ((range1->neg != 0) && (range2->neg == 0)))
2116 neg = 1;
2117
2118 for (codepoint = range1->start;codepoint <= range1->end ;codepoint++) {
2119 ret = xmlRegCheckCharacterRange(range2->type, codepoint,
2120 0, range2->start, range2->end,
2121 range2->blockName);
2122 if (ret < 0)
2123 return(-1);
2124 if (((neg == 1) && (ret == 0)) ||
2125 ((neg == 0) && (ret == 1)))
2126 return(1);
2127 }
2128 return(0);
2129 } else if ((range1->type == XML_REGEXP_BLOCK_NAME) ||
2130 (range2->type == XML_REGEXP_BLOCK_NAME)) {
2131 if (range1->type == range2->type) {
2132 ret = xmlStrEqual(range1->blockName, range2->blockName);
2133 } else {
2134 /*
2135 * comparing a block range with anything else is way
2136 * too costly, and maintining the table is like too much
2137 * memory too, so let's force the automata to save state
2138 * here.
2139 */
2140 return(1);
2141 }
2142 } else if ((range1->type < XML_REGEXP_LETTER) ||
2143 (range2->type < XML_REGEXP_LETTER)) {
2144 if ((range1->type == XML_REGEXP_ANYSPACE) &&
2145 (range2->type == XML_REGEXP_NOTSPACE))
2146 ret = 0;
2147 else if ((range1->type == XML_REGEXP_INITNAME) &&
2148 (range2->type == XML_REGEXP_NOTINITNAME))
2149 ret = 0;
2150 else if ((range1->type == XML_REGEXP_NAMECHAR) &&
2151 (range2->type == XML_REGEXP_NOTNAMECHAR))
2152 ret = 0;
2153 else if ((range1->type == XML_REGEXP_DECIMAL) &&
2154 (range2->type == XML_REGEXP_NOTDECIMAL))
2155 ret = 0;
2156 else if ((range1->type == XML_REGEXP_REALCHAR) &&
2157 (range2->type == XML_REGEXP_NOTREALCHAR))
2158 ret = 0;
2159 else {
2160 /* same thing to limit complexity */
2161 return(1);
2162 }
2163 } else {
2164 ret = 0;
2165 /* range1->type < range2->type here */
2166 switch (range1->type) {
2167 case XML_REGEXP_LETTER:
2168 /* all disjoint except in the subgroups */
2169 if ((range2->type == XML_REGEXP_LETTER_UPPERCASE) ||
2170 (range2->type == XML_REGEXP_LETTER_LOWERCASE) ||
2171 (range2->type == XML_REGEXP_LETTER_TITLECASE) ||
2172 (range2->type == XML_REGEXP_LETTER_MODIFIER) ||
2173 (range2->type == XML_REGEXP_LETTER_OTHERS))
2174 ret = 1;
2175 break;
2176 case XML_REGEXP_MARK:
2177 if ((range2->type == XML_REGEXP_MARK_NONSPACING) ||
2178 (range2->type == XML_REGEXP_MARK_SPACECOMBINING) ||
2179 (range2->type == XML_REGEXP_MARK_ENCLOSING))
2180 ret = 1;
2181 break;
2182 case XML_REGEXP_NUMBER:
2183 if ((range2->type == XML_REGEXP_NUMBER_DECIMAL) ||
2184 (range2->type == XML_REGEXP_NUMBER_LETTER) ||
2185 (range2->type == XML_REGEXP_NUMBER_OTHERS))
2186 ret = 1;
2187 break;
2188 case XML_REGEXP_PUNCT:
2189 if ((range2->type == XML_REGEXP_PUNCT_CONNECTOR) ||
2190 (range2->type == XML_REGEXP_PUNCT_DASH) ||
2191 (range2->type == XML_REGEXP_PUNCT_OPEN) ||
2192 (range2->type == XML_REGEXP_PUNCT_CLOSE) ||
2193 (range2->type == XML_REGEXP_PUNCT_INITQUOTE) ||
2194 (range2->type == XML_REGEXP_PUNCT_FINQUOTE) ||
2195 (range2->type == XML_REGEXP_PUNCT_OTHERS))
2196 ret = 1;
2197 break;
2198 case XML_REGEXP_SEPAR:
2199 if ((range2->type == XML_REGEXP_SEPAR_SPACE) ||
2200 (range2->type == XML_REGEXP_SEPAR_LINE) ||
2201 (range2->type == XML_REGEXP_SEPAR_PARA))
2202 ret = 1;
2203 break;
2204 case XML_REGEXP_SYMBOL:
2205 if ((range2->type == XML_REGEXP_SYMBOL_MATH) ||
2206 (range2->type == XML_REGEXP_SYMBOL_CURRENCY) ||
2207 (range2->type == XML_REGEXP_SYMBOL_MODIFIER) ||
2208 (range2->type == XML_REGEXP_SYMBOL_OTHERS))
2209 ret = 1;
2210 break;
2211 case XML_REGEXP_OTHER:
2212 if ((range2->type == XML_REGEXP_OTHER_CONTROL) ||
2213 (range2->type == XML_REGEXP_OTHER_FORMAT) ||
2214 (range2->type == XML_REGEXP_OTHER_PRIVATE))
2215 ret = 1;
2216 break;
2217 default:
2218 if ((range2->type >= XML_REGEXP_LETTER) &&
2219 (range2->type < XML_REGEXP_BLOCK_NAME))
2220 ret = 0;
2221 else {
2222 /* safety net ! */
2223 return(1);
2224 }
2225 }
2226 }
2227 if (((range1->neg == 0) && (range2->neg != 0)) ||
2228 ((range1->neg != 0) && (range2->neg == 0)))
2229 ret = !ret;
Daniel Veillard594e5df2009-09-07 14:58:47 +02002230 return(ret);
Daniel Veillard567a45b2005-10-18 19:11:55 +00002231}
2232
Daniel Veillarde19fc232002-04-22 16:01:24 +00002233/**
Daniel Veillardfc011b72006-02-12 19:14:15 +00002234 * xmlFACompareAtomTypes:
2235 * @type1: an atom type
2236 * @type2: an atom type
2237 *
2238 * Compares two atoms type to check whether they intersect in some ways,
2239 * this is used by xmlFACompareAtoms only
2240 *
2241 * Returns 1 if they may intersect and 0 otherwise
2242 */
2243static int
2244xmlFACompareAtomTypes(xmlRegAtomType type1, xmlRegAtomType type2) {
2245 if ((type1 == XML_REGEXP_EPSILON) ||
2246 (type1 == XML_REGEXP_CHARVAL) ||
2247 (type1 == XML_REGEXP_RANGES) ||
2248 (type1 == XML_REGEXP_SUBREG) ||
2249 (type1 == XML_REGEXP_STRING) ||
2250 (type1 == XML_REGEXP_ANYCHAR))
2251 return(1);
2252 if ((type2 == XML_REGEXP_EPSILON) ||
2253 (type2 == XML_REGEXP_CHARVAL) ||
2254 (type2 == XML_REGEXP_RANGES) ||
2255 (type2 == XML_REGEXP_SUBREG) ||
2256 (type2 == XML_REGEXP_STRING) ||
2257 (type2 == XML_REGEXP_ANYCHAR))
2258 return(1);
2259
2260 if (type1 == type2) return(1);
2261
2262 /* simplify subsequent compares by making sure type1 < type2 */
2263 if (type1 > type2) {
2264 xmlRegAtomType tmp = type1;
2265 type1 = type2;
2266 type2 = tmp;
2267 }
2268 switch (type1) {
2269 case XML_REGEXP_ANYSPACE: /* \s */
2270 /* can't be a letter, number, mark, pontuation, symbol */
2271 if ((type2 == XML_REGEXP_NOTSPACE) ||
2272 ((type2 >= XML_REGEXP_LETTER) &&
2273 (type2 <= XML_REGEXP_LETTER_OTHERS)) ||
2274 ((type2 >= XML_REGEXP_NUMBER) &&
2275 (type2 <= XML_REGEXP_NUMBER_OTHERS)) ||
2276 ((type2 >= XML_REGEXP_MARK) &&
2277 (type2 <= XML_REGEXP_MARK_ENCLOSING)) ||
2278 ((type2 >= XML_REGEXP_PUNCT) &&
2279 (type2 <= XML_REGEXP_PUNCT_OTHERS)) ||
2280 ((type2 >= XML_REGEXP_SYMBOL) &&
2281 (type2 <= XML_REGEXP_SYMBOL_OTHERS))
2282 ) return(0);
2283 break;
2284 case XML_REGEXP_NOTSPACE: /* \S */
2285 break;
2286 case XML_REGEXP_INITNAME: /* \l */
2287 /* can't be a number, mark, separator, pontuation, symbol or other */
2288 if ((type2 == XML_REGEXP_NOTINITNAME) ||
2289 ((type2 >= XML_REGEXP_NUMBER) &&
2290 (type2 <= XML_REGEXP_NUMBER_OTHERS)) ||
2291 ((type2 >= XML_REGEXP_MARK) &&
2292 (type2 <= XML_REGEXP_MARK_ENCLOSING)) ||
2293 ((type2 >= XML_REGEXP_SEPAR) &&
2294 (type2 <= XML_REGEXP_SEPAR_PARA)) ||
2295 ((type2 >= XML_REGEXP_PUNCT) &&
2296 (type2 <= XML_REGEXP_PUNCT_OTHERS)) ||
2297 ((type2 >= XML_REGEXP_SYMBOL) &&
2298 (type2 <= XML_REGEXP_SYMBOL_OTHERS)) ||
2299 ((type2 >= XML_REGEXP_OTHER) &&
2300 (type2 <= XML_REGEXP_OTHER_NA))
2301 ) return(0);
2302 break;
2303 case XML_REGEXP_NOTINITNAME: /* \L */
2304 break;
2305 case XML_REGEXP_NAMECHAR: /* \c */
2306 /* can't be a mark, separator, pontuation, symbol or other */
2307 if ((type2 == XML_REGEXP_NOTNAMECHAR) ||
2308 ((type2 >= XML_REGEXP_MARK) &&
2309 (type2 <= XML_REGEXP_MARK_ENCLOSING)) ||
2310 ((type2 >= XML_REGEXP_PUNCT) &&
2311 (type2 <= XML_REGEXP_PUNCT_OTHERS)) ||
2312 ((type2 >= XML_REGEXP_SEPAR) &&
2313 (type2 <= XML_REGEXP_SEPAR_PARA)) ||
2314 ((type2 >= XML_REGEXP_SYMBOL) &&
2315 (type2 <= XML_REGEXP_SYMBOL_OTHERS)) ||
2316 ((type2 >= XML_REGEXP_OTHER) &&
2317 (type2 <= XML_REGEXP_OTHER_NA))
2318 ) return(0);
2319 break;
2320 case XML_REGEXP_NOTNAMECHAR: /* \C */
2321 break;
2322 case XML_REGEXP_DECIMAL: /* \d */
2323 /* can't be a letter, mark, separator, pontuation, symbol or other */
2324 if ((type2 == XML_REGEXP_NOTDECIMAL) ||
2325 (type2 == XML_REGEXP_REALCHAR) ||
2326 ((type2 >= XML_REGEXP_LETTER) &&
2327 (type2 <= XML_REGEXP_LETTER_OTHERS)) ||
2328 ((type2 >= XML_REGEXP_MARK) &&
2329 (type2 <= XML_REGEXP_MARK_ENCLOSING)) ||
2330 ((type2 >= XML_REGEXP_PUNCT) &&
2331 (type2 <= XML_REGEXP_PUNCT_OTHERS)) ||
2332 ((type2 >= XML_REGEXP_SEPAR) &&
2333 (type2 <= XML_REGEXP_SEPAR_PARA)) ||
2334 ((type2 >= XML_REGEXP_SYMBOL) &&
2335 (type2 <= XML_REGEXP_SYMBOL_OTHERS)) ||
2336 ((type2 >= XML_REGEXP_OTHER) &&
2337 (type2 <= XML_REGEXP_OTHER_NA))
2338 )return(0);
2339 break;
2340 case XML_REGEXP_NOTDECIMAL: /* \D */
2341 break;
2342 case XML_REGEXP_REALCHAR: /* \w */
2343 /* can't be a mark, separator, pontuation, symbol or other */
2344 if ((type2 == XML_REGEXP_NOTDECIMAL) ||
2345 ((type2 >= XML_REGEXP_MARK) &&
2346 (type2 <= XML_REGEXP_MARK_ENCLOSING)) ||
2347 ((type2 >= XML_REGEXP_PUNCT) &&
2348 (type2 <= XML_REGEXP_PUNCT_OTHERS)) ||
2349 ((type2 >= XML_REGEXP_SEPAR) &&
2350 (type2 <= XML_REGEXP_SEPAR_PARA)) ||
2351 ((type2 >= XML_REGEXP_SYMBOL) &&
2352 (type2 <= XML_REGEXP_SYMBOL_OTHERS)) ||
2353 ((type2 >= XML_REGEXP_OTHER) &&
2354 (type2 <= XML_REGEXP_OTHER_NA))
2355 )return(0);
2356 break;
2357 case XML_REGEXP_NOTREALCHAR: /* \W */
2358 break;
2359 /*
2360 * at that point we know both type 1 and type2 are from
2361 * character categories are ordered and are different,
2362 * it becomes simple because this is a partition
2363 */
2364 case XML_REGEXP_LETTER:
2365 if (type2 <= XML_REGEXP_LETTER_OTHERS)
2366 return(1);
2367 return(0);
2368 case XML_REGEXP_LETTER_UPPERCASE:
2369 case XML_REGEXP_LETTER_LOWERCASE:
2370 case XML_REGEXP_LETTER_TITLECASE:
2371 case XML_REGEXP_LETTER_MODIFIER:
2372 case XML_REGEXP_LETTER_OTHERS:
2373 return(0);
2374 case XML_REGEXP_MARK:
2375 if (type2 <= XML_REGEXP_MARK_ENCLOSING)
2376 return(1);
2377 return(0);
2378 case XML_REGEXP_MARK_NONSPACING:
2379 case XML_REGEXP_MARK_SPACECOMBINING:
2380 case XML_REGEXP_MARK_ENCLOSING:
2381 return(0);
2382 case XML_REGEXP_NUMBER:
2383 if (type2 <= XML_REGEXP_NUMBER_OTHERS)
2384 return(1);
2385 return(0);
2386 case XML_REGEXP_NUMBER_DECIMAL:
2387 case XML_REGEXP_NUMBER_LETTER:
2388 case XML_REGEXP_NUMBER_OTHERS:
2389 return(0);
2390 case XML_REGEXP_PUNCT:
2391 if (type2 <= XML_REGEXP_PUNCT_OTHERS)
2392 return(1);
2393 return(0);
2394 case XML_REGEXP_PUNCT_CONNECTOR:
2395 case XML_REGEXP_PUNCT_DASH:
2396 case XML_REGEXP_PUNCT_OPEN:
2397 case XML_REGEXP_PUNCT_CLOSE:
2398 case XML_REGEXP_PUNCT_INITQUOTE:
2399 case XML_REGEXP_PUNCT_FINQUOTE:
2400 case XML_REGEXP_PUNCT_OTHERS:
2401 return(0);
2402 case XML_REGEXP_SEPAR:
2403 if (type2 <= XML_REGEXP_SEPAR_PARA)
2404 return(1);
2405 return(0);
2406 case XML_REGEXP_SEPAR_SPACE:
2407 case XML_REGEXP_SEPAR_LINE:
2408 case XML_REGEXP_SEPAR_PARA:
2409 return(0);
2410 case XML_REGEXP_SYMBOL:
2411 if (type2 <= XML_REGEXP_SYMBOL_OTHERS)
2412 return(1);
2413 return(0);
2414 case XML_REGEXP_SYMBOL_MATH:
2415 case XML_REGEXP_SYMBOL_CURRENCY:
2416 case XML_REGEXP_SYMBOL_MODIFIER:
2417 case XML_REGEXP_SYMBOL_OTHERS:
2418 return(0);
2419 case XML_REGEXP_OTHER:
2420 if (type2 <= XML_REGEXP_OTHER_NA)
2421 return(1);
2422 return(0);
2423 case XML_REGEXP_OTHER_CONTROL:
2424 case XML_REGEXP_OTHER_FORMAT:
2425 case XML_REGEXP_OTHER_PRIVATE:
2426 case XML_REGEXP_OTHER_NA:
2427 return(0);
2428 default:
2429 break;
2430 }
2431 return(1);
2432}
2433
2434/**
2435 * xmlFAEqualAtoms:
Daniel Veillarde19fc232002-04-22 16:01:24 +00002436 * @atom1: an atom
2437 * @atom2: an atom
Daniel Veillard1ba2aca2009-08-31 16:47:39 +02002438 * @deep: if not set only compare string pointers
Daniel Veillarde19fc232002-04-22 16:01:24 +00002439 *
Daniel Veillardfc011b72006-02-12 19:14:15 +00002440 * Compares two atoms to check whether they are the same exactly
2441 * this is used to remove equivalent transitions
Daniel Veillarde19fc232002-04-22 16:01:24 +00002442 *
Daniel Veillardfc011b72006-02-12 19:14:15 +00002443 * Returns 1 if same and 0 otherwise
Daniel Veillarde19fc232002-04-22 16:01:24 +00002444 */
2445static int
Daniel Veillard1ba2aca2009-08-31 16:47:39 +02002446xmlFAEqualAtoms(xmlRegAtomPtr atom1, xmlRegAtomPtr atom2, int deep) {
Daniel Veillardfc011b72006-02-12 19:14:15 +00002447 int ret = 0;
Daniel Veillard9efc4762005-07-19 14:33:55 +00002448
Daniel Veillarde19fc232002-04-22 16:01:24 +00002449 if (atom1 == atom2)
2450 return(1);
2451 if ((atom1 == NULL) || (atom2 == NULL))
2452 return(0);
2453
Daniel Veillardfc011b72006-02-12 19:14:15 +00002454 if (atom1->type != atom2->type)
2455 return(0);
2456 switch (atom1->type) {
2457 case XML_REGEXP_EPSILON:
2458 ret = 0;
2459 break;
2460 case XML_REGEXP_STRING:
Daniel Veillard1ba2aca2009-08-31 16:47:39 +02002461 if (!deep)
2462 ret = (atom1->valuep == atom2->valuep);
2463 else
2464 ret = xmlStrEqual((xmlChar *)atom1->valuep,
2465 (xmlChar *)atom2->valuep);
Daniel Veillardfc011b72006-02-12 19:14:15 +00002466 break;
2467 case XML_REGEXP_CHARVAL:
2468 ret = (atom1->codepoint == atom2->codepoint);
2469 break;
2470 case XML_REGEXP_RANGES:
2471 /* too hard to do in the general case */
2472 ret = 0;
2473 default:
2474 break;
2475 }
2476 return(ret);
2477}
2478
2479/**
2480 * xmlFACompareAtoms:
2481 * @atom1: an atom
2482 * @atom2: an atom
Daniel Veillard1ba2aca2009-08-31 16:47:39 +02002483 * @deep: if not set only compare string pointers
Daniel Veillardfc011b72006-02-12 19:14:15 +00002484 *
2485 * Compares two atoms to check whether they intersect in some ways,
2486 * this is used by xmlFAComputesDeterminism and xmlFARecurseDeterminism only
2487 *
2488 * Returns 1 if yes and 0 otherwise
2489 */
2490static int
Daniel Veillard1ba2aca2009-08-31 16:47:39 +02002491xmlFACompareAtoms(xmlRegAtomPtr atom1, xmlRegAtomPtr atom2, int deep) {
Daniel Veillardfc011b72006-02-12 19:14:15 +00002492 int ret = 1;
2493
2494 if (atom1 == atom2)
2495 return(1);
2496 if ((atom1 == NULL) || (atom2 == NULL))
2497 return(0);
2498
2499 if ((atom1->type == XML_REGEXP_ANYCHAR) ||
2500 (atom2->type == XML_REGEXP_ANYCHAR))
2501 return(1);
2502
2503 if (atom1->type > atom2->type) {
Daniel Veillard567a45b2005-10-18 19:11:55 +00002504 xmlRegAtomPtr tmp;
2505 tmp = atom1;
2506 atom1 = atom2;
2507 atom2 = tmp;
Daniel Veillardfc011b72006-02-12 19:14:15 +00002508 }
2509 if (atom1->type != atom2->type) {
2510 ret = xmlFACompareAtomTypes(atom1->type, atom2->type);
2511 /* if they can't intersect at the type level break now */
2512 if (ret == 0)
2513 return(0);
Daniel Veillard567a45b2005-10-18 19:11:55 +00002514 }
Daniel Veillarde19fc232002-04-22 16:01:24 +00002515 switch (atom1->type) {
2516 case XML_REGEXP_STRING:
Daniel Veillard1ba2aca2009-08-31 16:47:39 +02002517 if (!deep)
2518 ret = (atom1->valuep != atom2->valuep);
2519 else
2520 ret = xmlRegStrEqualWildcard((xmlChar *)atom1->valuep,
2521 (xmlChar *)atom2->valuep);
Daniel Veillard9efc4762005-07-19 14:33:55 +00002522 break;
Daniel Veillarde19fc232002-04-22 16:01:24 +00002523 case XML_REGEXP_EPSILON:
Daniel Veillard567a45b2005-10-18 19:11:55 +00002524 goto not_determinist;
Daniel Veillarde19fc232002-04-22 16:01:24 +00002525 case XML_REGEXP_CHARVAL:
Daniel Veillardfc011b72006-02-12 19:14:15 +00002526 if (atom2->type == XML_REGEXP_CHARVAL) {
2527 ret = (atom1->codepoint == atom2->codepoint);
2528 } else {
2529 ret = xmlRegCheckCharacter(atom2, atom1->codepoint);
2530 if (ret < 0)
2531 ret = 1;
2532 }
Daniel Veillard9efc4762005-07-19 14:33:55 +00002533 break;
Daniel Veillarde19fc232002-04-22 16:01:24 +00002534 case XML_REGEXP_RANGES:
Daniel Veillardfc011b72006-02-12 19:14:15 +00002535 if (atom2->type == XML_REGEXP_RANGES) {
Daniel Veillard567a45b2005-10-18 19:11:55 +00002536 int i, j, res;
2537 xmlRegRangePtr r1, r2;
2538
2539 /*
2540 * need to check that none of the ranges eventually matches
2541 */
2542 for (i = 0;i < atom1->nbRanges;i++) {
2543 for (j = 0;j < atom2->nbRanges;j++) {
2544 r1 = atom1->ranges[i];
2545 r2 = atom2->ranges[j];
2546 res = xmlFACompareRanges(r1, r2);
2547 if (res == 1) {
2548 ret = 1;
2549 goto done;
2550 }
2551 }
2552 }
2553 ret = 0;
2554 }
2555 break;
Daniel Veillarde19fc232002-04-22 16:01:24 +00002556 default:
Daniel Veillard567a45b2005-10-18 19:11:55 +00002557 goto not_determinist;
Daniel Veillarde19fc232002-04-22 16:01:24 +00002558 }
Daniel Veillard567a45b2005-10-18 19:11:55 +00002559done:
Daniel Veillard6e65e152005-08-09 11:09:52 +00002560 if (atom1->neg != atom2->neg) {
Daniel Veillard9efc4762005-07-19 14:33:55 +00002561 ret = !ret;
Daniel Veillard6e65e152005-08-09 11:09:52 +00002562 }
Daniel Veillard567a45b2005-10-18 19:11:55 +00002563 if (ret == 0)
2564 return(0);
2565not_determinist:
2566 return(1);
Daniel Veillarde19fc232002-04-22 16:01:24 +00002567}
2568
2569/**
2570 * xmlFARecurseDeterminism:
2571 * @ctxt: a regexp parser context
2572 *
2573 * Check whether the associated regexp is determinist,
2574 * should be called after xmlFAEliminateEpsilonTransitions()
2575 *
2576 */
2577static int
2578xmlFARecurseDeterminism(xmlRegParserCtxtPtr ctxt, xmlRegStatePtr state,
2579 int to, xmlRegAtomPtr atom) {
2580 int ret = 1;
Daniel Veillard567a45b2005-10-18 19:11:55 +00002581 int res;
Daniel Veillard5de09382005-09-26 17:18:17 +00002582 int transnr, nbTrans;
Daniel Veillarde19fc232002-04-22 16:01:24 +00002583 xmlRegTransPtr t1;
Daniel Veillard1ba2aca2009-08-31 16:47:39 +02002584 int deep = 1;
Daniel Veillarde19fc232002-04-22 16:01:24 +00002585
2586 if (state == NULL)
2587 return(ret);
Daniel Veillard1ba2aca2009-08-31 16:47:39 +02002588
2589 if (ctxt->flags & AM_AUTOMATA_RNG)
2590 deep = 0;
2591
Daniel Veillard5de09382005-09-26 17:18:17 +00002592 /*
2593 * don't recurse on transitions potentially added in the course of
2594 * the elimination.
2595 */
2596 nbTrans = state->nbTrans;
2597 for (transnr = 0;transnr < nbTrans;transnr++) {
Daniel Veillarde19fc232002-04-22 16:01:24 +00002598 t1 = &(state->trans[transnr]);
2599 /*
2600 * check transitions conflicting with the one looked at
2601 */
2602 if (t1->atom == NULL) {
Daniel Veillard0e05f4c2006-11-01 15:33:04 +00002603 if (t1->to < 0)
Daniel Veillarde19fc232002-04-22 16:01:24 +00002604 continue;
Daniel Veillard567a45b2005-10-18 19:11:55 +00002605 res = xmlFARecurseDeterminism(ctxt, ctxt->states[t1->to],
Daniel Veillarde19fc232002-04-22 16:01:24 +00002606 to, atom);
Daniel Veillard567a45b2005-10-18 19:11:55 +00002607 if (res == 0) {
2608 ret = 0;
Daniel Veillardaa622012005-10-20 15:55:25 +00002609 /* t1->nd = 1; */
Daniel Veillard567a45b2005-10-18 19:11:55 +00002610 }
Daniel Veillarde19fc232002-04-22 16:01:24 +00002611 continue;
2612 }
2613 if (t1->to != to)
2614 continue;
Daniel Veillard1ba2aca2009-08-31 16:47:39 +02002615 if (xmlFACompareAtoms(t1->atom, atom, deep)) {
Daniel Veillard567a45b2005-10-18 19:11:55 +00002616 ret = 0;
2617 /* mark the transition as non-deterministic */
2618 t1->nd = 1;
2619 }
Daniel Veillarde19fc232002-04-22 16:01:24 +00002620 }
2621 return(ret);
2622}
2623
2624/**
2625 * xmlFAComputesDeterminism:
2626 * @ctxt: a regexp parser context
2627 *
2628 * Check whether the associated regexp is determinist,
2629 * should be called after xmlFAEliminateEpsilonTransitions()
2630 *
2631 */
2632static int
2633xmlFAComputesDeterminism(xmlRegParserCtxtPtr ctxt) {
2634 int statenr, transnr;
2635 xmlRegStatePtr state;
Daniel Veillard567a45b2005-10-18 19:11:55 +00002636 xmlRegTransPtr t1, t2, last;
Daniel Veillarde19fc232002-04-22 16:01:24 +00002637 int i;
2638 int ret = 1;
Daniel Veillard1ba2aca2009-08-31 16:47:39 +02002639 int deep = 1;
Daniel Veillarde19fc232002-04-22 16:01:24 +00002640
Daniel Veillard4402ab42002-09-12 16:02:56 +00002641#ifdef DEBUG_REGEXP_GRAPH
2642 printf("xmlFAComputesDeterminism\n");
2643 xmlRegPrintCtxt(stdout, ctxt);
2644#endif
Daniel Veillarde19fc232002-04-22 16:01:24 +00002645 if (ctxt->determinist != -1)
2646 return(ctxt->determinist);
2647
Daniel Veillard1ba2aca2009-08-31 16:47:39 +02002648 if (ctxt->flags & AM_AUTOMATA_RNG)
2649 deep = 0;
2650
Daniel Veillarde19fc232002-04-22 16:01:24 +00002651 /*
Daniel Veillard567a45b2005-10-18 19:11:55 +00002652 * First cleanup the automata removing cancelled transitions
Daniel Veillarde19fc232002-04-22 16:01:24 +00002653 */
2654 for (statenr = 0;statenr < ctxt->nbStates;statenr++) {
2655 state = ctxt->states[statenr];
2656 if (state == NULL)
2657 continue;
Daniel Veillard4f82c8a2005-08-09 21:40:08 +00002658 if (state->nbTrans < 2)
2659 continue;
Daniel Veillarde19fc232002-04-22 16:01:24 +00002660 for (transnr = 0;transnr < state->nbTrans;transnr++) {
2661 t1 = &(state->trans[transnr]);
2662 /*
2663 * Determinism checks in case of counted or all transitions
2664 * will have to be handled separately
2665 */
Daniel Veillard567a45b2005-10-18 19:11:55 +00002666 if (t1->atom == NULL) {
Daniel Veillardaa622012005-10-20 15:55:25 +00002667 /* t1->nd = 1; */
Daniel Veillarde19fc232002-04-22 16:01:24 +00002668 continue;
Daniel Veillard567a45b2005-10-18 19:11:55 +00002669 }
Daniel Veillarde19fc232002-04-22 16:01:24 +00002670 if (t1->to == -1) /* eliminated */
2671 continue;
2672 for (i = 0;i < transnr;i++) {
2673 t2 = &(state->trans[i]);
2674 if (t2->to == -1) /* eliminated */
2675 continue;
2676 if (t2->atom != NULL) {
2677 if (t1->to == t2->to) {
Daniel Veillard1ba2aca2009-08-31 16:47:39 +02002678 /*
2679 * Here we use deep because we want to keep the
2680 * transitions which indicate a conflict
2681 */
2682 if (xmlFAEqualAtoms(t1->atom, t2->atom, deep) &&
Daniel Veillard11e28e42009-08-12 12:21:42 +02002683 (t1->counter == t2->counter) &&
2684 (t1->count == t2->count))
William M. Brackddf71d62004-05-06 04:17:26 +00002685 t2->to = -1; /* eliminated */
Daniel Veillard567a45b2005-10-18 19:11:55 +00002686 }
2687 }
2688 }
2689 }
2690 }
2691
2692 /*
2693 * Check for all states that there aren't 2 transitions
2694 * with the same atom and a different target.
2695 */
2696 for (statenr = 0;statenr < ctxt->nbStates;statenr++) {
2697 state = ctxt->states[statenr];
2698 if (state == NULL)
2699 continue;
2700 if (state->nbTrans < 2)
2701 continue;
2702 last = NULL;
2703 for (transnr = 0;transnr < state->nbTrans;transnr++) {
2704 t1 = &(state->trans[transnr]);
2705 /*
2706 * Determinism checks in case of counted or all transitions
2707 * will have to be handled separately
2708 */
2709 if (t1->atom == NULL) {
2710 continue;
2711 }
2712 if (t1->to == -1) /* eliminated */
2713 continue;
2714 for (i = 0;i < transnr;i++) {
2715 t2 = &(state->trans[i]);
2716 if (t2->to == -1) /* eliminated */
2717 continue;
2718 if (t2->atom != NULL) {
Daniel Veillard1ba2aca2009-08-31 16:47:39 +02002719 /*
2720 * But here we don't use deep because we want to
2721 * find transitions which indicate a conflict
2722 */
2723 if (xmlFACompareAtoms(t1->atom, t2->atom, 1)) {
Daniel Veillard567a45b2005-10-18 19:11:55 +00002724 ret = 0;
2725 /* mark the transitions as non-deterministic ones */
2726 t1->nd = 1;
2727 t2->nd = 1;
2728 last = t1;
Daniel Veillarde19fc232002-04-22 16:01:24 +00002729 }
2730 } else if (t1->to != -1) {
2731 /*
2732 * do the closure in case of remaining specific
2733 * epsilon transitions like choices or all
2734 */
2735 ret = xmlFARecurseDeterminism(ctxt, ctxt->states[t1->to],
2736 t2->to, t2->atom);
Daniel Veillard567a45b2005-10-18 19:11:55 +00002737 /* don't shortcut the computation so all non deterministic
2738 transition get marked down
Daniel Veillarde19fc232002-04-22 16:01:24 +00002739 if (ret == 0)
Daniel Veillardaa622012005-10-20 15:55:25 +00002740 return(0);
2741 */
Daniel Veillard567a45b2005-10-18 19:11:55 +00002742 if (ret == 0) {
2743 t1->nd = 1;
Daniel Veillardaa622012005-10-20 15:55:25 +00002744 /* t2->nd = 1; */
Daniel Veillard567a45b2005-10-18 19:11:55 +00002745 last = t1;
2746 }
Daniel Veillarde19fc232002-04-22 16:01:24 +00002747 }
2748 }
Daniel Veillard567a45b2005-10-18 19:11:55 +00002749 /* don't shortcut the computation so all non deterministic
2750 transition get marked down
Daniel Veillarde19fc232002-04-22 16:01:24 +00002751 if (ret == 0)
Daniel Veillard567a45b2005-10-18 19:11:55 +00002752 break; */
Daniel Veillarde19fc232002-04-22 16:01:24 +00002753 }
Daniel Veillard567a45b2005-10-18 19:11:55 +00002754
2755 /*
2756 * mark specifically the last non-deterministic transition
2757 * from a state since there is no need to set-up rollback
2758 * from it
2759 */
2760 if (last != NULL) {
2761 last->nd = 2;
2762 }
2763
2764 /* don't shortcut the computation so all non deterministic
2765 transition get marked down
Daniel Veillarde19fc232002-04-22 16:01:24 +00002766 if (ret == 0)
Daniel Veillard567a45b2005-10-18 19:11:55 +00002767 break; */
Daniel Veillarde19fc232002-04-22 16:01:24 +00002768 }
Daniel Veillard567a45b2005-10-18 19:11:55 +00002769
Daniel Veillarde19fc232002-04-22 16:01:24 +00002770 ctxt->determinist = ret;
2771 return(ret);
2772}
2773
Daniel Veillard4255d502002-04-16 15:50:10 +00002774/************************************************************************
2775 * *
2776 * Routines to check input against transition atoms *
2777 * *
2778 ************************************************************************/
2779
2780static int
2781xmlRegCheckCharacterRange(xmlRegAtomType type, int codepoint, int neg,
2782 int start, int end, const xmlChar *blockName) {
2783 int ret = 0;
2784
2785 switch (type) {
2786 case XML_REGEXP_STRING:
2787 case XML_REGEXP_SUBREG:
2788 case XML_REGEXP_RANGES:
2789 case XML_REGEXP_EPSILON:
2790 return(-1);
2791 case XML_REGEXP_ANYCHAR:
2792 ret = ((codepoint != '\n') && (codepoint != '\r'));
2793 break;
2794 case XML_REGEXP_CHARVAL:
2795 ret = ((codepoint >= start) && (codepoint <= end));
2796 break;
2797 case XML_REGEXP_NOTSPACE:
2798 neg = !neg;
2799 case XML_REGEXP_ANYSPACE:
2800 ret = ((codepoint == '\n') || (codepoint == '\r') ||
2801 (codepoint == '\t') || (codepoint == ' '));
2802 break;
2803 case XML_REGEXP_NOTINITNAME:
2804 neg = !neg;
2805 case XML_REGEXP_INITNAME:
William M. Brack871611b2003-10-18 04:53:14 +00002806 ret = (IS_LETTER(codepoint) ||
Daniel Veillard4255d502002-04-16 15:50:10 +00002807 (codepoint == '_') || (codepoint == ':'));
2808 break;
2809 case XML_REGEXP_NOTNAMECHAR:
2810 neg = !neg;
2811 case XML_REGEXP_NAMECHAR:
William M. Brack871611b2003-10-18 04:53:14 +00002812 ret = (IS_LETTER(codepoint) || IS_DIGIT(codepoint) ||
Daniel Veillard4255d502002-04-16 15:50:10 +00002813 (codepoint == '.') || (codepoint == '-') ||
2814 (codepoint == '_') || (codepoint == ':') ||
William M. Brack871611b2003-10-18 04:53:14 +00002815 IS_COMBINING(codepoint) || IS_EXTENDER(codepoint));
Daniel Veillard4255d502002-04-16 15:50:10 +00002816 break;
2817 case XML_REGEXP_NOTDECIMAL:
2818 neg = !neg;
2819 case XML_REGEXP_DECIMAL:
2820 ret = xmlUCSIsCatNd(codepoint);
2821 break;
2822 case XML_REGEXP_REALCHAR:
2823 neg = !neg;
2824 case XML_REGEXP_NOTREALCHAR:
2825 ret = xmlUCSIsCatP(codepoint);
2826 if (ret == 0)
2827 ret = xmlUCSIsCatZ(codepoint);
2828 if (ret == 0)
2829 ret = xmlUCSIsCatC(codepoint);
2830 break;
2831 case XML_REGEXP_LETTER:
2832 ret = xmlUCSIsCatL(codepoint);
2833 break;
2834 case XML_REGEXP_LETTER_UPPERCASE:
2835 ret = xmlUCSIsCatLu(codepoint);
2836 break;
2837 case XML_REGEXP_LETTER_LOWERCASE:
2838 ret = xmlUCSIsCatLl(codepoint);
2839 break;
2840 case XML_REGEXP_LETTER_TITLECASE:
2841 ret = xmlUCSIsCatLt(codepoint);
2842 break;
2843 case XML_REGEXP_LETTER_MODIFIER:
2844 ret = xmlUCSIsCatLm(codepoint);
2845 break;
2846 case XML_REGEXP_LETTER_OTHERS:
2847 ret = xmlUCSIsCatLo(codepoint);
2848 break;
2849 case XML_REGEXP_MARK:
2850 ret = xmlUCSIsCatM(codepoint);
2851 break;
2852 case XML_REGEXP_MARK_NONSPACING:
2853 ret = xmlUCSIsCatMn(codepoint);
2854 break;
2855 case XML_REGEXP_MARK_SPACECOMBINING:
2856 ret = xmlUCSIsCatMc(codepoint);
2857 break;
2858 case XML_REGEXP_MARK_ENCLOSING:
2859 ret = xmlUCSIsCatMe(codepoint);
2860 break;
2861 case XML_REGEXP_NUMBER:
2862 ret = xmlUCSIsCatN(codepoint);
2863 break;
2864 case XML_REGEXP_NUMBER_DECIMAL:
2865 ret = xmlUCSIsCatNd(codepoint);
2866 break;
2867 case XML_REGEXP_NUMBER_LETTER:
2868 ret = xmlUCSIsCatNl(codepoint);
2869 break;
2870 case XML_REGEXP_NUMBER_OTHERS:
2871 ret = xmlUCSIsCatNo(codepoint);
2872 break;
2873 case XML_REGEXP_PUNCT:
2874 ret = xmlUCSIsCatP(codepoint);
2875 break;
2876 case XML_REGEXP_PUNCT_CONNECTOR:
2877 ret = xmlUCSIsCatPc(codepoint);
2878 break;
2879 case XML_REGEXP_PUNCT_DASH:
2880 ret = xmlUCSIsCatPd(codepoint);
2881 break;
2882 case XML_REGEXP_PUNCT_OPEN:
2883 ret = xmlUCSIsCatPs(codepoint);
2884 break;
2885 case XML_REGEXP_PUNCT_CLOSE:
2886 ret = xmlUCSIsCatPe(codepoint);
2887 break;
2888 case XML_REGEXP_PUNCT_INITQUOTE:
2889 ret = xmlUCSIsCatPi(codepoint);
2890 break;
2891 case XML_REGEXP_PUNCT_FINQUOTE:
2892 ret = xmlUCSIsCatPf(codepoint);
2893 break;
2894 case XML_REGEXP_PUNCT_OTHERS:
2895 ret = xmlUCSIsCatPo(codepoint);
2896 break;
2897 case XML_REGEXP_SEPAR:
2898 ret = xmlUCSIsCatZ(codepoint);
2899 break;
2900 case XML_REGEXP_SEPAR_SPACE:
2901 ret = xmlUCSIsCatZs(codepoint);
2902 break;
2903 case XML_REGEXP_SEPAR_LINE:
2904 ret = xmlUCSIsCatZl(codepoint);
2905 break;
2906 case XML_REGEXP_SEPAR_PARA:
2907 ret = xmlUCSIsCatZp(codepoint);
2908 break;
2909 case XML_REGEXP_SYMBOL:
2910 ret = xmlUCSIsCatS(codepoint);
2911 break;
2912 case XML_REGEXP_SYMBOL_MATH:
2913 ret = xmlUCSIsCatSm(codepoint);
2914 break;
2915 case XML_REGEXP_SYMBOL_CURRENCY:
2916 ret = xmlUCSIsCatSc(codepoint);
2917 break;
2918 case XML_REGEXP_SYMBOL_MODIFIER:
2919 ret = xmlUCSIsCatSk(codepoint);
2920 break;
2921 case XML_REGEXP_SYMBOL_OTHERS:
2922 ret = xmlUCSIsCatSo(codepoint);
2923 break;
2924 case XML_REGEXP_OTHER:
2925 ret = xmlUCSIsCatC(codepoint);
2926 break;
2927 case XML_REGEXP_OTHER_CONTROL:
2928 ret = xmlUCSIsCatCc(codepoint);
2929 break;
2930 case XML_REGEXP_OTHER_FORMAT:
2931 ret = xmlUCSIsCatCf(codepoint);
2932 break;
2933 case XML_REGEXP_OTHER_PRIVATE:
2934 ret = xmlUCSIsCatCo(codepoint);
2935 break;
2936 case XML_REGEXP_OTHER_NA:
2937 /* ret = xmlUCSIsCatCn(codepoint); */
2938 /* Seems it doesn't exist anymore in recent Unicode releases */
2939 ret = 0;
2940 break;
2941 case XML_REGEXP_BLOCK_NAME:
2942 ret = xmlUCSIsBlock(codepoint, (const char *) blockName);
2943 break;
2944 }
2945 if (neg)
2946 return(!ret);
2947 return(ret);
2948}
2949
2950static int
2951xmlRegCheckCharacter(xmlRegAtomPtr atom, int codepoint) {
2952 int i, ret = 0;
2953 xmlRegRangePtr range;
2954
William M. Brack871611b2003-10-18 04:53:14 +00002955 if ((atom == NULL) || (!IS_CHAR(codepoint)))
Daniel Veillard4255d502002-04-16 15:50:10 +00002956 return(-1);
2957
2958 switch (atom->type) {
2959 case XML_REGEXP_SUBREG:
2960 case XML_REGEXP_EPSILON:
2961 return(-1);
2962 case XML_REGEXP_CHARVAL:
2963 return(codepoint == atom->codepoint);
2964 case XML_REGEXP_RANGES: {
2965 int accept = 0;
Daniel Veillardf2a12832003-11-24 13:04:35 +00002966
Daniel Veillard4255d502002-04-16 15:50:10 +00002967 for (i = 0;i < atom->nbRanges;i++) {
2968 range = atom->ranges[i];
Daniel Veillardf8b9de32003-11-24 14:27:26 +00002969 if (range->neg == 2) {
Daniel Veillard4255d502002-04-16 15:50:10 +00002970 ret = xmlRegCheckCharacterRange(range->type, codepoint,
2971 0, range->start, range->end,
2972 range->blockName);
2973 if (ret != 0)
2974 return(0); /* excluded char */
Daniel Veillardf8b9de32003-11-24 14:27:26 +00002975 } else if (range->neg) {
2976 ret = xmlRegCheckCharacterRange(range->type, codepoint,
2977 0, range->start, range->end,
2978 range->blockName);
2979 if (ret == 0)
Daniel Veillardf2a12832003-11-24 13:04:35 +00002980 accept = 1;
Daniel Veillardf8b9de32003-11-24 14:27:26 +00002981 else
2982 return(0);
Daniel Veillard4255d502002-04-16 15:50:10 +00002983 } else {
2984 ret = xmlRegCheckCharacterRange(range->type, codepoint,
2985 0, range->start, range->end,
2986 range->blockName);
2987 if (ret != 0)
2988 accept = 1; /* might still be excluded */
2989 }
2990 }
2991 return(accept);
2992 }
2993 case XML_REGEXP_STRING:
2994 printf("TODO: XML_REGEXP_STRING\n");
2995 return(-1);
2996 case XML_REGEXP_ANYCHAR:
2997 case XML_REGEXP_ANYSPACE:
2998 case XML_REGEXP_NOTSPACE:
2999 case XML_REGEXP_INITNAME:
3000 case XML_REGEXP_NOTINITNAME:
3001 case XML_REGEXP_NAMECHAR:
3002 case XML_REGEXP_NOTNAMECHAR:
3003 case XML_REGEXP_DECIMAL:
3004 case XML_REGEXP_NOTDECIMAL:
3005 case XML_REGEXP_REALCHAR:
3006 case XML_REGEXP_NOTREALCHAR:
3007 case XML_REGEXP_LETTER:
3008 case XML_REGEXP_LETTER_UPPERCASE:
3009 case XML_REGEXP_LETTER_LOWERCASE:
3010 case XML_REGEXP_LETTER_TITLECASE:
3011 case XML_REGEXP_LETTER_MODIFIER:
3012 case XML_REGEXP_LETTER_OTHERS:
3013 case XML_REGEXP_MARK:
3014 case XML_REGEXP_MARK_NONSPACING:
3015 case XML_REGEXP_MARK_SPACECOMBINING:
3016 case XML_REGEXP_MARK_ENCLOSING:
3017 case XML_REGEXP_NUMBER:
3018 case XML_REGEXP_NUMBER_DECIMAL:
3019 case XML_REGEXP_NUMBER_LETTER:
3020 case XML_REGEXP_NUMBER_OTHERS:
3021 case XML_REGEXP_PUNCT:
3022 case XML_REGEXP_PUNCT_CONNECTOR:
3023 case XML_REGEXP_PUNCT_DASH:
3024 case XML_REGEXP_PUNCT_OPEN:
3025 case XML_REGEXP_PUNCT_CLOSE:
3026 case XML_REGEXP_PUNCT_INITQUOTE:
3027 case XML_REGEXP_PUNCT_FINQUOTE:
3028 case XML_REGEXP_PUNCT_OTHERS:
3029 case XML_REGEXP_SEPAR:
3030 case XML_REGEXP_SEPAR_SPACE:
3031 case XML_REGEXP_SEPAR_LINE:
3032 case XML_REGEXP_SEPAR_PARA:
3033 case XML_REGEXP_SYMBOL:
3034 case XML_REGEXP_SYMBOL_MATH:
3035 case XML_REGEXP_SYMBOL_CURRENCY:
3036 case XML_REGEXP_SYMBOL_MODIFIER:
3037 case XML_REGEXP_SYMBOL_OTHERS:
3038 case XML_REGEXP_OTHER:
3039 case XML_REGEXP_OTHER_CONTROL:
3040 case XML_REGEXP_OTHER_FORMAT:
3041 case XML_REGEXP_OTHER_PRIVATE:
3042 case XML_REGEXP_OTHER_NA:
3043 case XML_REGEXP_BLOCK_NAME:
3044 ret = xmlRegCheckCharacterRange(atom->type, codepoint, 0, 0, 0,
3045 (const xmlChar *)atom->valuep);
3046 if (atom->neg)
3047 ret = !ret;
3048 break;
3049 }
3050 return(ret);
3051}
3052
3053/************************************************************************
3054 * *
William M. Brackddf71d62004-05-06 04:17:26 +00003055 * Saving and restoring state of an execution context *
Daniel Veillard4255d502002-04-16 15:50:10 +00003056 * *
3057 ************************************************************************/
3058
3059#ifdef DEBUG_REGEXP_EXEC
3060static void
3061xmlFARegDebugExec(xmlRegExecCtxtPtr exec) {
3062 printf("state: %d:%d:idx %d", exec->state->no, exec->transno, exec->index);
3063 if (exec->inputStack != NULL) {
3064 int i;
3065 printf(": ");
3066 for (i = 0;(i < 3) && (i < exec->inputStackNr);i++)
Daniel Veillard0e05f4c2006-11-01 15:33:04 +00003067 printf("%s ", (const char *)
3068 exec->inputStack[exec->inputStackNr - (i + 1)].value);
Daniel Veillard4255d502002-04-16 15:50:10 +00003069 } else {
3070 printf(": %s", &(exec->inputString[exec->index]));
3071 }
3072 printf("\n");
3073}
3074#endif
3075
3076static void
3077xmlFARegExecSave(xmlRegExecCtxtPtr exec) {
3078#ifdef DEBUG_REGEXP_EXEC
3079 printf("saving ");
3080 exec->transno++;
3081 xmlFARegDebugExec(exec);
3082 exec->transno--;
3083#endif
Daniel Veillard94cc1032005-09-15 13:09:00 +00003084#ifdef MAX_PUSH
3085 if (exec->nbPush > MAX_PUSH) {
3086 return;
3087 }
3088 exec->nbPush++;
3089#endif
Daniel Veillard4255d502002-04-16 15:50:10 +00003090
3091 if (exec->maxRollbacks == 0) {
3092 exec->maxRollbacks = 4;
3093 exec->rollbacks = (xmlRegExecRollback *) xmlMalloc(exec->maxRollbacks *
3094 sizeof(xmlRegExecRollback));
3095 if (exec->rollbacks == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00003096 xmlRegexpErrMemory(NULL, "saving regexp");
Daniel Veillard4255d502002-04-16 15:50:10 +00003097 exec->maxRollbacks = 0;
3098 return;
3099 }
3100 memset(exec->rollbacks, 0,
3101 exec->maxRollbacks * sizeof(xmlRegExecRollback));
3102 } else if (exec->nbRollbacks >= exec->maxRollbacks) {
3103 xmlRegExecRollback *tmp;
3104 int len = exec->maxRollbacks;
3105
3106 exec->maxRollbacks *= 2;
3107 tmp = (xmlRegExecRollback *) xmlRealloc(exec->rollbacks,
3108 exec->maxRollbacks * sizeof(xmlRegExecRollback));
3109 if (tmp == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00003110 xmlRegexpErrMemory(NULL, "saving regexp");
Daniel Veillard4255d502002-04-16 15:50:10 +00003111 exec->maxRollbacks /= 2;
3112 return;
3113 }
3114 exec->rollbacks = tmp;
3115 tmp = &exec->rollbacks[len];
3116 memset(tmp, 0, (exec->maxRollbacks - len) * sizeof(xmlRegExecRollback));
3117 }
3118 exec->rollbacks[exec->nbRollbacks].state = exec->state;
3119 exec->rollbacks[exec->nbRollbacks].index = exec->index;
3120 exec->rollbacks[exec->nbRollbacks].nextbranch = exec->transno + 1;
3121 if (exec->comp->nbCounters > 0) {
3122 if (exec->rollbacks[exec->nbRollbacks].counts == NULL) {
3123 exec->rollbacks[exec->nbRollbacks].counts = (int *)
3124 xmlMalloc(exec->comp->nbCounters * sizeof(int));
3125 if (exec->rollbacks[exec->nbRollbacks].counts == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00003126 xmlRegexpErrMemory(NULL, "saving regexp");
Daniel Veillard4255d502002-04-16 15:50:10 +00003127 exec->status = -5;
3128 return;
3129 }
3130 }
3131 memcpy(exec->rollbacks[exec->nbRollbacks].counts, exec->counts,
3132 exec->comp->nbCounters * sizeof(int));
3133 }
3134 exec->nbRollbacks++;
3135}
3136
3137static void
3138xmlFARegExecRollBack(xmlRegExecCtxtPtr exec) {
3139 if (exec->nbRollbacks <= 0) {
3140 exec->status = -1;
3141#ifdef DEBUG_REGEXP_EXEC
3142 printf("rollback failed on empty stack\n");
3143#endif
3144 return;
3145 }
3146 exec->nbRollbacks--;
3147 exec->state = exec->rollbacks[exec->nbRollbacks].state;
3148 exec->index = exec->rollbacks[exec->nbRollbacks].index;
3149 exec->transno = exec->rollbacks[exec->nbRollbacks].nextbranch;
3150 if (exec->comp->nbCounters > 0) {
3151 if (exec->rollbacks[exec->nbRollbacks].counts == NULL) {
3152 fprintf(stderr, "exec save: allocation failed");
3153 exec->status = -6;
3154 return;
3155 }
3156 memcpy(exec->counts, exec->rollbacks[exec->nbRollbacks].counts,
3157 exec->comp->nbCounters * sizeof(int));
3158 }
3159
3160#ifdef DEBUG_REGEXP_EXEC
3161 printf("restored ");
3162 xmlFARegDebugExec(exec);
3163#endif
3164}
3165
3166/************************************************************************
3167 * *
William M. Brackddf71d62004-05-06 04:17:26 +00003168 * Verifier, running an input against a compiled regexp *
Daniel Veillard4255d502002-04-16 15:50:10 +00003169 * *
3170 ************************************************************************/
3171
3172static int
3173xmlFARegExec(xmlRegexpPtr comp, const xmlChar *content) {
3174 xmlRegExecCtxt execval;
3175 xmlRegExecCtxtPtr exec = &execval;
Daniel Veillard567a45b2005-10-18 19:11:55 +00003176 int ret, codepoint = 0, len, deter;
Daniel Veillard4255d502002-04-16 15:50:10 +00003177
3178 exec->inputString = content;
3179 exec->index = 0;
Daniel Veillard94cc1032005-09-15 13:09:00 +00003180 exec->nbPush = 0;
Daniel Veillard4255d502002-04-16 15:50:10 +00003181 exec->determinist = 1;
3182 exec->maxRollbacks = 0;
3183 exec->nbRollbacks = 0;
3184 exec->rollbacks = NULL;
3185 exec->status = 0;
3186 exec->comp = comp;
3187 exec->state = comp->states[0];
3188 exec->transno = 0;
3189 exec->transcount = 0;
Daniel Veillardf2a12832003-11-24 13:04:35 +00003190 exec->inputStack = NULL;
3191 exec->inputStackMax = 0;
Daniel Veillard4255d502002-04-16 15:50:10 +00003192 if (comp->nbCounters > 0) {
3193 exec->counts = (int *) xmlMalloc(comp->nbCounters * sizeof(int));
Daniel Veillardff46a042003-10-08 08:53:17 +00003194 if (exec->counts == NULL) {
3195 xmlRegexpErrMemory(NULL, "running regexp");
Daniel Veillard4255d502002-04-16 15:50:10 +00003196 return(-1);
Daniel Veillardff46a042003-10-08 08:53:17 +00003197 }
Daniel Veillard4255d502002-04-16 15:50:10 +00003198 memset(exec->counts, 0, comp->nbCounters * sizeof(int));
3199 } else
3200 exec->counts = NULL;
3201 while ((exec->status == 0) &&
3202 ((exec->inputString[exec->index] != 0) ||
Daniel Veillardad559982008-05-12 13:15:35 +00003203 ((exec->state != NULL) &&
3204 (exec->state->type != XML_REGEXP_FINAL_STATE)))) {
Daniel Veillard4255d502002-04-16 15:50:10 +00003205 xmlRegTransPtr trans;
3206 xmlRegAtomPtr atom;
3207
3208 /*
William M. Brack0e00b282004-04-26 15:40:47 +00003209 * If end of input on non-terminal state, rollback, however we may
Daniel Veillard4255d502002-04-16 15:50:10 +00003210 * still have epsilon like transition for counted transitions
William M. Brack0e00b282004-04-26 15:40:47 +00003211 * on counters, in that case don't break too early. Additionally,
3212 * if we are working on a range like "AB{0,2}", where B is not present,
3213 * we don't want to break.
Daniel Veillard4255d502002-04-16 15:50:10 +00003214 */
Daniel Veillard11ce4002006-03-10 00:36:23 +00003215 len = 1;
William M. Brack0e00b282004-04-26 15:40:47 +00003216 if ((exec->inputString[exec->index] == 0) && (exec->counts == NULL)) {
William M. Brackddf71d62004-05-06 04:17:26 +00003217 /*
3218 * if there is a transition, we must check if
3219 * atom allows minOccurs of 0
3220 */
3221 if (exec->transno < exec->state->nbTrans) {
William M. Brack0e00b282004-04-26 15:40:47 +00003222 trans = &exec->state->trans[exec->transno];
3223 if (trans->to >=0) {
3224 atom = trans->atom;
3225 if (!((atom->min == 0) && (atom->max > 0)))
3226 goto rollback;
3227 }
3228 } else
3229 goto rollback;
3230 }
Daniel Veillard4255d502002-04-16 15:50:10 +00003231
3232 exec->transcount = 0;
3233 for (;exec->transno < exec->state->nbTrans;exec->transno++) {
3234 trans = &exec->state->trans[exec->transno];
3235 if (trans->to < 0)
3236 continue;
3237 atom = trans->atom;
3238 ret = 0;
Daniel Veillard567a45b2005-10-18 19:11:55 +00003239 deter = 1;
Daniel Veillard4255d502002-04-16 15:50:10 +00003240 if (trans->count >= 0) {
3241 int count;
3242 xmlRegCounterPtr counter;
3243
Daniel Veillard11ce4002006-03-10 00:36:23 +00003244 if (exec->counts == NULL) {
3245 exec->status = -1;
3246 goto error;
3247 }
Daniel Veillard4255d502002-04-16 15:50:10 +00003248 /*
3249 * A counted transition.
3250 */
3251
3252 count = exec->counts[trans->count];
3253 counter = &exec->comp->counters[trans->count];
3254#ifdef DEBUG_REGEXP_EXEC
3255 printf("testing count %d: val %d, min %d, max %d\n",
3256 trans->count, count, counter->min, counter->max);
3257#endif
3258 ret = ((count >= counter->min) && (count <= counter->max));
Daniel Veillard567a45b2005-10-18 19:11:55 +00003259 if ((ret) && (counter->min != counter->max))
3260 deter = 0;
Daniel Veillard4255d502002-04-16 15:50:10 +00003261 } else if (atom == NULL) {
3262 fprintf(stderr, "epsilon transition left at runtime\n");
3263 exec->status = -2;
3264 break;
3265 } else if (exec->inputString[exec->index] != 0) {
3266 codepoint = CUR_SCHAR(&(exec->inputString[exec->index]), len);
3267 ret = xmlRegCheckCharacter(atom, codepoint);
William M. Brack0e00b282004-04-26 15:40:47 +00003268 if ((ret == 1) && (atom->min >= 0) && (atom->max > 0)) {
Daniel Veillard4255d502002-04-16 15:50:10 +00003269 xmlRegStatePtr to = comp->states[trans->to];
3270
3271 /*
3272 * this is a multiple input sequence
Daniel Veillardfc6eca02005-11-01 15:24:02 +00003273 * If there is a counter associated increment it now.
3274 * before potentially saving and rollback
Daniel Veillardc821e032007-08-28 17:33:45 +00003275 * do not increment if the counter is already over the
3276 * maximum limit in which case get to next transition
Daniel Veillard4255d502002-04-16 15:50:10 +00003277 */
Daniel Veillardfc6eca02005-11-01 15:24:02 +00003278 if (trans->counter >= 0) {
Daniel Veillardc821e032007-08-28 17:33:45 +00003279 xmlRegCounterPtr counter;
3280
3281 if ((exec->counts == NULL) ||
3282 (exec->comp == NULL) ||
3283 (exec->comp->counters == NULL)) {
Daniel Veillard11ce4002006-03-10 00:36:23 +00003284 exec->status = -1;
3285 goto error;
3286 }
Daniel Veillardc821e032007-08-28 17:33:45 +00003287 counter = &exec->comp->counters[trans->counter];
3288 if (exec->counts[trans->counter] >= counter->max)
3289 continue; /* for loop on transitions */
3290
Daniel Veillardfc6eca02005-11-01 15:24:02 +00003291#ifdef DEBUG_REGEXP_EXEC
3292 printf("Increasing count %d\n", trans->counter);
3293#endif
3294 exec->counts[trans->counter]++;
3295 }
Daniel Veillard4255d502002-04-16 15:50:10 +00003296 if (exec->state->nbTrans > exec->transno + 1) {
3297 xmlFARegExecSave(exec);
3298 }
3299 exec->transcount = 1;
3300 do {
3301 /*
3302 * Try to progress as much as possible on the input
3303 */
3304 if (exec->transcount == atom->max) {
3305 break;
3306 }
3307 exec->index += len;
3308 /*
3309 * End of input: stop here
3310 */
3311 if (exec->inputString[exec->index] == 0) {
3312 exec->index -= len;
3313 break;
3314 }
3315 if (exec->transcount >= atom->min) {
3316 int transno = exec->transno;
3317 xmlRegStatePtr state = exec->state;
3318
3319 /*
3320 * The transition is acceptable save it
3321 */
3322 exec->transno = -1; /* trick */
3323 exec->state = to;
3324 xmlFARegExecSave(exec);
3325 exec->transno = transno;
3326 exec->state = state;
3327 }
3328 codepoint = CUR_SCHAR(&(exec->inputString[exec->index]),
3329 len);
3330 ret = xmlRegCheckCharacter(atom, codepoint);
3331 exec->transcount++;
3332 } while (ret == 1);
3333 if (exec->transcount < atom->min)
3334 ret = 0;
3335
3336 /*
3337 * If the last check failed but one transition was found
3338 * possible, rollback
3339 */
3340 if (ret < 0)
3341 ret = 0;
3342 if (ret == 0) {
3343 goto rollback;
3344 }
Daniel Veillardfc6eca02005-11-01 15:24:02 +00003345 if (trans->counter >= 0) {
Daniel Veillard11ce4002006-03-10 00:36:23 +00003346 if (exec->counts == NULL) {
3347 exec->status = -1;
3348 goto error;
3349 }
Daniel Veillardfc6eca02005-11-01 15:24:02 +00003350#ifdef DEBUG_REGEXP_EXEC
3351 printf("Decreasing count %d\n", trans->counter);
3352#endif
3353 exec->counts[trans->counter]--;
3354 }
William M. Brack0e00b282004-04-26 15:40:47 +00003355 } else if ((ret == 0) && (atom->min == 0) && (atom->max > 0)) {
3356 /*
3357 * we don't match on the codepoint, but minOccurs of 0
3358 * says that's ok. Setting len to 0 inhibits stepping
3359 * over the codepoint.
3360 */
3361 exec->transcount = 1;
3362 len = 0;
3363 ret = 1;
Daniel Veillard4255d502002-04-16 15:50:10 +00003364 }
William M. Brack0e00b282004-04-26 15:40:47 +00003365 } else if ((atom->min == 0) && (atom->max > 0)) {
3366 /* another spot to match when minOccurs is 0 */
3367 exec->transcount = 1;
3368 len = 0;
3369 ret = 1;
Daniel Veillard4255d502002-04-16 15:50:10 +00003370 }
3371 if (ret == 1) {
Daniel Veillard567a45b2005-10-18 19:11:55 +00003372 if ((trans->nd == 1) ||
3373 ((trans->count >= 0) && (deter == 0) &&
3374 (exec->state->nbTrans > exec->transno + 1))) {
Daniel Veillardaa622012005-10-20 15:55:25 +00003375#ifdef DEBUG_REGEXP_EXEC
3376 if (trans->nd == 1)
3377 printf("Saving on nd transition atom %d for %c at %d\n",
3378 trans->atom->no, codepoint, exec->index);
3379 else
3380 printf("Saving on counted transition count %d for %c at %d\n",
3381 trans->count, codepoint, exec->index);
3382#endif
Daniel Veillard4255d502002-04-16 15:50:10 +00003383 xmlFARegExecSave(exec);
3384 }
3385 if (trans->counter >= 0) {
Daniel Veillardc821e032007-08-28 17:33:45 +00003386 xmlRegCounterPtr counter;
3387
3388 /* make sure we don't go over the counter maximum value */
3389 if ((exec->counts == NULL) ||
3390 (exec->comp == NULL) ||
3391 (exec->comp->counters == NULL)) {
3392 exec->status = -1;
Daniel Veillard11ce4002006-03-10 00:36:23 +00003393 goto error;
3394 }
Daniel Veillardc821e032007-08-28 17:33:45 +00003395 counter = &exec->comp->counters[trans->counter];
3396 if (exec->counts[trans->counter] >= counter->max)
3397 continue; /* for loop on transitions */
Daniel Veillard4255d502002-04-16 15:50:10 +00003398#ifdef DEBUG_REGEXP_EXEC
3399 printf("Increasing count %d\n", trans->counter);
3400#endif
3401 exec->counts[trans->counter]++;
3402 }
Daniel Veillard10752282005-08-08 13:05:13 +00003403 if ((trans->count >= 0) &&
3404 (trans->count < REGEXP_ALL_COUNTER)) {
Daniel Veillard11ce4002006-03-10 00:36:23 +00003405 if (exec->counts == NULL) {
3406 exec->status = -1;
3407 goto error;
3408 }
Daniel Veillard10752282005-08-08 13:05:13 +00003409#ifdef DEBUG_REGEXP_EXEC
3410 printf("resetting count %d on transition\n",
3411 trans->count);
3412#endif
3413 exec->counts[trans->count] = 0;
3414 }
Daniel Veillard4255d502002-04-16 15:50:10 +00003415#ifdef DEBUG_REGEXP_EXEC
3416 printf("entering state %d\n", trans->to);
3417#endif
3418 exec->state = comp->states[trans->to];
3419 exec->transno = 0;
3420 if (trans->atom != NULL) {
3421 exec->index += len;
3422 }
3423 goto progress;
3424 } else if (ret < 0) {
3425 exec->status = -4;
3426 break;
3427 }
3428 }
3429 if ((exec->transno != 0) || (exec->state->nbTrans == 0)) {
3430rollback:
3431 /*
3432 * Failed to find a way out
3433 */
3434 exec->determinist = 0;
Daniel Veillardaa622012005-10-20 15:55:25 +00003435#ifdef DEBUG_REGEXP_EXEC
3436 printf("rollback from state %d on %d:%c\n", exec->state->no,
3437 codepoint,codepoint);
3438#endif
Daniel Veillard4255d502002-04-16 15:50:10 +00003439 xmlFARegExecRollBack(exec);
3440 }
3441progress:
3442 continue;
3443 }
Daniel Veillard11ce4002006-03-10 00:36:23 +00003444error:
Daniel Veillard4255d502002-04-16 15:50:10 +00003445 if (exec->rollbacks != NULL) {
3446 if (exec->counts != NULL) {
3447 int i;
3448
3449 for (i = 0;i < exec->maxRollbacks;i++)
3450 if (exec->rollbacks[i].counts != NULL)
3451 xmlFree(exec->rollbacks[i].counts);
3452 }
3453 xmlFree(exec->rollbacks);
3454 }
3455 if (exec->counts != NULL)
3456 xmlFree(exec->counts);
3457 if (exec->status == 0)
3458 return(1);
Daniel Veillard94cc1032005-09-15 13:09:00 +00003459 if (exec->status == -1) {
3460 if (exec->nbPush > MAX_PUSH)
3461 return(-1);
Daniel Veillard4255d502002-04-16 15:50:10 +00003462 return(0);
Daniel Veillard94cc1032005-09-15 13:09:00 +00003463 }
Daniel Veillard4255d502002-04-16 15:50:10 +00003464 return(exec->status);
3465}
3466
3467/************************************************************************
3468 * *
William M. Brackddf71d62004-05-06 04:17:26 +00003469 * Progressive interface to the verifier one atom at a time *
Daniel Veillard4255d502002-04-16 15:50:10 +00003470 * *
3471 ************************************************************************/
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003472#ifdef DEBUG_ERR
3473static void testerr(xmlRegExecCtxtPtr exec);
3474#endif
Daniel Veillard4255d502002-04-16 15:50:10 +00003475
3476/**
Daniel Veillard01c13b52002-12-10 15:19:08 +00003477 * xmlRegNewExecCtxt:
Daniel Veillard4255d502002-04-16 15:50:10 +00003478 * @comp: a precompiled regular expression
3479 * @callback: a callback function used for handling progresses in the
3480 * automata matching phase
3481 * @data: the context data associated to the callback in this context
3482 *
3483 * Build a context used for progressive evaluation of a regexp.
Daniel Veillard01c13b52002-12-10 15:19:08 +00003484 *
3485 * Returns the new context
Daniel Veillard4255d502002-04-16 15:50:10 +00003486 */
3487xmlRegExecCtxtPtr
3488xmlRegNewExecCtxt(xmlRegexpPtr comp, xmlRegExecCallbacks callback, void *data) {
3489 xmlRegExecCtxtPtr exec;
3490
3491 if (comp == NULL)
3492 return(NULL);
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00003493 if ((comp->compact == NULL) && (comp->states == NULL))
3494 return(NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +00003495 exec = (xmlRegExecCtxtPtr) xmlMalloc(sizeof(xmlRegExecCtxt));
3496 if (exec == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00003497 xmlRegexpErrMemory(NULL, "creating execution context");
Daniel Veillard4255d502002-04-16 15:50:10 +00003498 return(NULL);
3499 }
3500 memset(exec, 0, sizeof(xmlRegExecCtxt));
3501 exec->inputString = NULL;
3502 exec->index = 0;
3503 exec->determinist = 1;
3504 exec->maxRollbacks = 0;
3505 exec->nbRollbacks = 0;
3506 exec->rollbacks = NULL;
3507 exec->status = 0;
3508 exec->comp = comp;
Daniel Veillard23e73572002-09-19 19:56:43 +00003509 if (comp->compact == NULL)
3510 exec->state = comp->states[0];
Daniel Veillard4255d502002-04-16 15:50:10 +00003511 exec->transno = 0;
3512 exec->transcount = 0;
3513 exec->callback = callback;
3514 exec->data = data;
3515 if (comp->nbCounters > 0) {
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003516 /*
3517 * For error handling, exec->counts is allocated twice the size
3518 * the second half is used to store the data in case of rollback
3519 */
3520 exec->counts = (int *) xmlMalloc(comp->nbCounters * sizeof(int)
3521 * 2);
Daniel Veillard4255d502002-04-16 15:50:10 +00003522 if (exec->counts == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00003523 xmlRegexpErrMemory(NULL, "creating execution context");
Daniel Veillard4255d502002-04-16 15:50:10 +00003524 xmlFree(exec);
3525 return(NULL);
3526 }
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003527 memset(exec->counts, 0, comp->nbCounters * sizeof(int) * 2);
3528 exec->errCounts = &exec->counts[comp->nbCounters];
3529 } else {
Daniel Veillard4255d502002-04-16 15:50:10 +00003530 exec->counts = NULL;
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003531 exec->errCounts = NULL;
3532 }
Daniel Veillard4255d502002-04-16 15:50:10 +00003533 exec->inputStackMax = 0;
3534 exec->inputStackNr = 0;
3535 exec->inputStack = NULL;
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003536 exec->errStateNo = -1;
3537 exec->errString = NULL;
Daniel Veillard94cc1032005-09-15 13:09:00 +00003538 exec->nbPush = 0;
Daniel Veillard4255d502002-04-16 15:50:10 +00003539 return(exec);
3540}
3541
3542/**
3543 * xmlRegFreeExecCtxt:
3544 * @exec: a regular expression evaulation context
3545 *
3546 * Free the structures associated to a regular expression evaulation context.
3547 */
3548void
3549xmlRegFreeExecCtxt(xmlRegExecCtxtPtr exec) {
3550 if (exec == NULL)
3551 return;
3552
3553 if (exec->rollbacks != NULL) {
3554 if (exec->counts != NULL) {
3555 int i;
3556
3557 for (i = 0;i < exec->maxRollbacks;i++)
3558 if (exec->rollbacks[i].counts != NULL)
3559 xmlFree(exec->rollbacks[i].counts);
3560 }
3561 xmlFree(exec->rollbacks);
3562 }
3563 if (exec->counts != NULL)
3564 xmlFree(exec->counts);
3565 if (exec->inputStack != NULL) {
3566 int i;
3567
Daniel Veillard32370232002-10-16 14:08:14 +00003568 for (i = 0;i < exec->inputStackNr;i++) {
3569 if (exec->inputStack[i].value != NULL)
3570 xmlFree(exec->inputStack[i].value);
3571 }
Daniel Veillard4255d502002-04-16 15:50:10 +00003572 xmlFree(exec->inputStack);
3573 }
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003574 if (exec->errString != NULL)
3575 xmlFree(exec->errString);
Daniel Veillard4255d502002-04-16 15:50:10 +00003576 xmlFree(exec);
3577}
3578
3579static void
3580xmlFARegExecSaveInputString(xmlRegExecCtxtPtr exec, const xmlChar *value,
3581 void *data) {
3582#ifdef DEBUG_PUSH
3583 printf("saving value: %d:%s\n", exec->inputStackNr, value);
3584#endif
3585 if (exec->inputStackMax == 0) {
3586 exec->inputStackMax = 4;
3587 exec->inputStack = (xmlRegInputTokenPtr)
3588 xmlMalloc(exec->inputStackMax * sizeof(xmlRegInputToken));
3589 if (exec->inputStack == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00003590 xmlRegexpErrMemory(NULL, "pushing input string");
Daniel Veillard4255d502002-04-16 15:50:10 +00003591 exec->inputStackMax = 0;
3592 return;
3593 }
3594 } else if (exec->inputStackNr + 1 >= exec->inputStackMax) {
3595 xmlRegInputTokenPtr tmp;
3596
3597 exec->inputStackMax *= 2;
3598 tmp = (xmlRegInputTokenPtr) xmlRealloc(exec->inputStack,
3599 exec->inputStackMax * sizeof(xmlRegInputToken));
3600 if (tmp == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00003601 xmlRegexpErrMemory(NULL, "pushing input string");
Daniel Veillard4255d502002-04-16 15:50:10 +00003602 exec->inputStackMax /= 2;
3603 return;
3604 }
3605 exec->inputStack = tmp;
3606 }
3607 exec->inputStack[exec->inputStackNr].value = xmlStrdup(value);
3608 exec->inputStack[exec->inputStackNr].data = data;
3609 exec->inputStackNr++;
3610 exec->inputStack[exec->inputStackNr].value = NULL;
3611 exec->inputStack[exec->inputStackNr].data = NULL;
3612}
3613
Daniel Veillardc0826a72004-08-10 14:17:33 +00003614/**
3615 * xmlRegStrEqualWildcard:
3616 * @expStr: the string to be evaluated
3617 * @valStr: the validation string
3618 *
3619 * Checks if both strings are equal or have the same content. "*"
3620 * can be used as a wildcard in @valStr; "|" is used as a seperator of
3621 * substrings in both @expStr and @valStr.
3622 *
3623 * Returns 1 if the comparison is satisfied and the number of substrings
3624 * is equal, 0 otherwise.
3625 */
3626
3627static int
3628xmlRegStrEqualWildcard(const xmlChar *expStr, const xmlChar *valStr) {
3629 if (expStr == valStr) return(1);
3630 if (expStr == NULL) return(0);
3631 if (valStr == NULL) return(0);
3632 do {
3633 /*
3634 * Eval if we have a wildcard for the current item.
3635 */
3636 if (*expStr != *valStr) {
Daniel Veillard4f82c8a2005-08-09 21:40:08 +00003637 /* if one of them starts with a wildcard make valStr be it */
3638 if (*valStr == '*') {
3639 const xmlChar *tmp;
3640
3641 tmp = valStr;
3642 valStr = expStr;
3643 expStr = tmp;
3644 }
Daniel Veillardc0826a72004-08-10 14:17:33 +00003645 if ((*valStr != 0) && (*expStr != 0) && (*expStr++ == '*')) {
3646 do {
3647 if (*valStr == XML_REG_STRING_SEPARATOR)
3648 break;
Kasimier T. Buchcikc0e833f2005-04-19 15:02:20 +00003649 valStr++;
Daniel Veillardc0826a72004-08-10 14:17:33 +00003650 } while (*valStr != 0);
3651 continue;
3652 } else
3653 return(0);
3654 }
Kasimier T. Buchcikc0e833f2005-04-19 15:02:20 +00003655 expStr++;
3656 valStr++;
Daniel Veillardc0826a72004-08-10 14:17:33 +00003657 } while (*valStr != 0);
3658 if (*expStr != 0)
3659 return (0);
3660 else
3661 return (1);
3662}
Daniel Veillard4255d502002-04-16 15:50:10 +00003663
3664/**
Daniel Veillard23e73572002-09-19 19:56:43 +00003665 * xmlRegCompactPushString:
3666 * @exec: a regexp execution context
3667 * @comp: the precompiled exec with a compact table
3668 * @value: a string token input
3669 * @data: data associated to the token to reuse in callbacks
3670 *
3671 * Push one input token in the execution context
3672 *
3673 * Returns: 1 if the regexp reached a final state, 0 if non-final, and
3674 * a negative value in case of error.
3675 */
3676static int
3677xmlRegCompactPushString(xmlRegExecCtxtPtr exec,
3678 xmlRegexpPtr comp,
3679 const xmlChar *value,
3680 void *data) {
3681 int state = exec->index;
3682 int i, target;
3683
3684 if ((comp == NULL) || (comp->compact == NULL) || (comp->stringMap == NULL))
3685 return(-1);
3686
3687 if (value == NULL) {
3688 /*
3689 * are we at a final state ?
3690 */
3691 if (comp->compact[state * (comp->nbstrings + 1)] ==
3692 XML_REGEXP_FINAL_STATE)
3693 return(1);
3694 return(0);
3695 }
3696
3697#ifdef DEBUG_PUSH
3698 printf("value pushed: %s\n", value);
3699#endif
3700
3701 /*
William M. Brackddf71d62004-05-06 04:17:26 +00003702 * Examine all outside transitions from current state
Daniel Veillard23e73572002-09-19 19:56:43 +00003703 */
3704 for (i = 0;i < comp->nbstrings;i++) {
3705 target = comp->compact[state * (comp->nbstrings + 1) + i + 1];
3706 if ((target > 0) && (target <= comp->nbstates)) {
Daniel Veillardc0826a72004-08-10 14:17:33 +00003707 target--; /* to avoid 0 */
3708 if (xmlRegStrEqualWildcard(comp->stringMap[i], value)) {
3709 exec->index = target;
Daniel Veillard118aed72002-09-24 14:13:13 +00003710 if ((exec->callback != NULL) && (comp->transdata != NULL)) {
3711 exec->callback(exec->data, value,
3712 comp->transdata[state * comp->nbstrings + i], data);
3713 }
Daniel Veillard23e73572002-09-19 19:56:43 +00003714#ifdef DEBUG_PUSH
3715 printf("entering state %d\n", target);
3716#endif
3717 if (comp->compact[target * (comp->nbstrings + 1)] ==
Daniel Veillardcc026dc2005-01-12 13:21:17 +00003718 XML_REGEXP_SINK_STATE)
3719 goto error;
3720
3721 if (comp->compact[target * (comp->nbstrings + 1)] ==
Daniel Veillard23e73572002-09-19 19:56:43 +00003722 XML_REGEXP_FINAL_STATE)
3723 return(1);
3724 return(0);
3725 }
3726 }
3727 }
3728 /*
3729 * Failed to find an exit transition out from current state for the
3730 * current token
3731 */
3732#ifdef DEBUG_PUSH
3733 printf("failed to find a transition for %s on state %d\n", value, state);
3734#endif
Daniel Veillardcc026dc2005-01-12 13:21:17 +00003735error:
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003736 if (exec->errString != NULL)
3737 xmlFree(exec->errString);
3738 exec->errString = xmlStrdup(value);
3739 exec->errStateNo = state;
Daniel Veillard23e73572002-09-19 19:56:43 +00003740 exec->status = -1;
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003741#ifdef DEBUG_ERR
3742 testerr(exec);
3743#endif
Daniel Veillard23e73572002-09-19 19:56:43 +00003744 return(-1);
3745}
3746
3747/**
Daniel Veillard6e65e152005-08-09 11:09:52 +00003748 * xmlRegExecPushStringInternal:
Daniel Veillardea7751d2002-12-20 00:16:24 +00003749 * @exec: a regexp execution context or NULL to indicate the end
Daniel Veillard4255d502002-04-16 15:50:10 +00003750 * @value: a string token input
3751 * @data: data associated to the token to reuse in callbacks
Daniel Veillard6e65e152005-08-09 11:09:52 +00003752 * @compound: value was assembled from 2 strings
Daniel Veillard4255d502002-04-16 15:50:10 +00003753 *
3754 * Push one input token in the execution context
3755 *
3756 * Returns: 1 if the regexp reached a final state, 0 if non-final, and
3757 * a negative value in case of error.
3758 */
Daniel Veillard6e65e152005-08-09 11:09:52 +00003759static int
3760xmlRegExecPushStringInternal(xmlRegExecCtxtPtr exec, const xmlChar *value,
3761 void *data, int compound) {
Daniel Veillard4255d502002-04-16 15:50:10 +00003762 xmlRegTransPtr trans;
3763 xmlRegAtomPtr atom;
3764 int ret;
3765 int final = 0;
Daniel Veillard90700152005-01-08 22:05:09 +00003766 int progress = 1;
Daniel Veillard4255d502002-04-16 15:50:10 +00003767
3768 if (exec == NULL)
3769 return(-1);
Daniel Veillard23e73572002-09-19 19:56:43 +00003770 if (exec->comp == NULL)
3771 return(-1);
Daniel Veillard4255d502002-04-16 15:50:10 +00003772 if (exec->status != 0)
3773 return(exec->status);
3774
Daniel Veillard23e73572002-09-19 19:56:43 +00003775 if (exec->comp->compact != NULL)
3776 return(xmlRegCompactPushString(exec, exec->comp, value, data));
3777
Daniel Veillard4255d502002-04-16 15:50:10 +00003778 if (value == NULL) {
3779 if (exec->state->type == XML_REGEXP_FINAL_STATE)
3780 return(1);
3781 final = 1;
3782 }
3783
3784#ifdef DEBUG_PUSH
3785 printf("value pushed: %s\n", value);
3786#endif
3787 /*
3788 * If we have an active rollback stack push the new value there
3789 * and get back to where we were left
3790 */
3791 if ((value != NULL) && (exec->inputStackNr > 0)) {
3792 xmlFARegExecSaveInputString(exec, value, data);
3793 value = exec->inputStack[exec->index].value;
3794 data = exec->inputStack[exec->index].data;
3795#ifdef DEBUG_PUSH
3796 printf("value loaded: %s\n", value);
3797#endif
3798 }
3799
3800 while ((exec->status == 0) &&
3801 ((value != NULL) ||
3802 ((final == 1) &&
3803 (exec->state->type != XML_REGEXP_FINAL_STATE)))) {
3804
3805 /*
3806 * End of input on non-terminal state, rollback, however we may
3807 * still have epsilon like transition for counted transitions
3808 * on counters, in that case don't break too early.
3809 */
Daniel Veillardb509f152002-04-17 16:28:10 +00003810 if ((value == NULL) && (exec->counts == NULL))
Daniel Veillard4255d502002-04-16 15:50:10 +00003811 goto rollback;
3812
3813 exec->transcount = 0;
3814 for (;exec->transno < exec->state->nbTrans;exec->transno++) {
3815 trans = &exec->state->trans[exec->transno];
3816 if (trans->to < 0)
3817 continue;
3818 atom = trans->atom;
3819 ret = 0;
Daniel Veillard441bc322002-04-20 17:38:48 +00003820 if (trans->count == REGEXP_ALL_LAX_COUNTER) {
3821 int i;
3822 int count;
3823 xmlRegTransPtr t;
3824 xmlRegCounterPtr counter;
3825
3826 ret = 0;
3827
3828#ifdef DEBUG_PUSH
3829 printf("testing all lax %d\n", trans->count);
3830#endif
3831 /*
3832 * Check all counted transitions from the current state
3833 */
3834 if ((value == NULL) && (final)) {
3835 ret = 1;
3836 } else if (value != NULL) {
3837 for (i = 0;i < exec->state->nbTrans;i++) {
3838 t = &exec->state->trans[i];
3839 if ((t->counter < 0) || (t == trans))
3840 continue;
3841 counter = &exec->comp->counters[t->counter];
3842 count = exec->counts[t->counter];
3843 if ((count < counter->max) &&
3844 (t->atom != NULL) &&
3845 (xmlStrEqual(value, t->atom->valuep))) {
3846 ret = 0;
3847 break;
3848 }
3849 if ((count >= counter->min) &&
3850 (count < counter->max) &&
Daniel Veillard11ce4002006-03-10 00:36:23 +00003851 (t->atom != NULL) &&
Daniel Veillard441bc322002-04-20 17:38:48 +00003852 (xmlStrEqual(value, t->atom->valuep))) {
3853 ret = 1;
3854 break;
3855 }
3856 }
3857 }
3858 } else if (trans->count == REGEXP_ALL_COUNTER) {
Daniel Veillard8a001f62002-04-20 07:24:11 +00003859 int i;
3860 int count;
3861 xmlRegTransPtr t;
3862 xmlRegCounterPtr counter;
3863
3864 ret = 1;
3865
3866#ifdef DEBUG_PUSH
3867 printf("testing all %d\n", trans->count);
3868#endif
3869 /*
3870 * Check all counted transitions from the current state
3871 */
3872 for (i = 0;i < exec->state->nbTrans;i++) {
3873 t = &exec->state->trans[i];
3874 if ((t->counter < 0) || (t == trans))
3875 continue;
3876 counter = &exec->comp->counters[t->counter];
3877 count = exec->counts[t->counter];
3878 if ((count < counter->min) || (count > counter->max)) {
3879 ret = 0;
3880 break;
3881 }
3882 }
3883 } else if (trans->count >= 0) {
Daniel Veillard4255d502002-04-16 15:50:10 +00003884 int count;
3885 xmlRegCounterPtr counter;
3886
3887 /*
3888 * A counted transition.
3889 */
3890
3891 count = exec->counts[trans->count];
3892 counter = &exec->comp->counters[trans->count];
3893#ifdef DEBUG_PUSH
3894 printf("testing count %d: val %d, min %d, max %d\n",
3895 trans->count, count, counter->min, counter->max);
3896#endif
3897 ret = ((count >= counter->min) && (count <= counter->max));
3898 } else if (atom == NULL) {
3899 fprintf(stderr, "epsilon transition left at runtime\n");
3900 exec->status = -2;
3901 break;
3902 } else if (value != NULL) {
Daniel Veillardc0826a72004-08-10 14:17:33 +00003903 ret = xmlRegStrEqualWildcard(atom->valuep, value);
Daniel Veillard6e65e152005-08-09 11:09:52 +00003904 if (atom->neg) {
Daniel Veillard9efc4762005-07-19 14:33:55 +00003905 ret = !ret;
Daniel Veillard6e65e152005-08-09 11:09:52 +00003906 if (!compound)
3907 ret = 0;
3908 }
Daniel Veillard441bc322002-04-20 17:38:48 +00003909 if ((ret == 1) && (trans->counter >= 0)) {
3910 xmlRegCounterPtr counter;
3911 int count;
3912
3913 count = exec->counts[trans->counter];
3914 counter = &exec->comp->counters[trans->counter];
3915 if (count >= counter->max)
3916 ret = 0;
3917 }
3918
Daniel Veillard4255d502002-04-16 15:50:10 +00003919 if ((ret == 1) && (atom->min > 0) && (atom->max > 0)) {
3920 xmlRegStatePtr to = exec->comp->states[trans->to];
3921
3922 /*
3923 * this is a multiple input sequence
3924 */
3925 if (exec->state->nbTrans > exec->transno + 1) {
3926 if (exec->inputStackNr <= 0) {
3927 xmlFARegExecSaveInputString(exec, value, data);
3928 }
3929 xmlFARegExecSave(exec);
3930 }
3931 exec->transcount = 1;
3932 do {
3933 /*
3934 * Try to progress as much as possible on the input
3935 */
3936 if (exec->transcount == atom->max) {
3937 break;
3938 }
3939 exec->index++;
3940 value = exec->inputStack[exec->index].value;
3941 data = exec->inputStack[exec->index].data;
3942#ifdef DEBUG_PUSH
3943 printf("value loaded: %s\n", value);
3944#endif
3945
3946 /*
3947 * End of input: stop here
3948 */
3949 if (value == NULL) {
3950 exec->index --;
3951 break;
3952 }
3953 if (exec->transcount >= atom->min) {
3954 int transno = exec->transno;
3955 xmlRegStatePtr state = exec->state;
3956
3957 /*
3958 * The transition is acceptable save it
3959 */
3960 exec->transno = -1; /* trick */
3961 exec->state = to;
3962 if (exec->inputStackNr <= 0) {
3963 xmlFARegExecSaveInputString(exec, value, data);
3964 }
3965 xmlFARegExecSave(exec);
3966 exec->transno = transno;
3967 exec->state = state;
3968 }
3969 ret = xmlStrEqual(value, atom->valuep);
3970 exec->transcount++;
3971 } while (ret == 1);
3972 if (exec->transcount < atom->min)
3973 ret = 0;
3974
3975 /*
3976 * If the last check failed but one transition was found
3977 * possible, rollback
3978 */
3979 if (ret < 0)
3980 ret = 0;
3981 if (ret == 0) {
3982 goto rollback;
3983 }
3984 }
3985 }
3986 if (ret == 1) {
William M. Brack98873952003-12-26 06:03:14 +00003987 if ((exec->callback != NULL) && (atom != NULL) &&
3988 (data != NULL)) {
Daniel Veillard4255d502002-04-16 15:50:10 +00003989 exec->callback(exec->data, atom->valuep,
3990 atom->data, data);
3991 }
3992 if (exec->state->nbTrans > exec->transno + 1) {
3993 if (exec->inputStackNr <= 0) {
3994 xmlFARegExecSaveInputString(exec, value, data);
3995 }
3996 xmlFARegExecSave(exec);
3997 }
3998 if (trans->counter >= 0) {
3999#ifdef DEBUG_PUSH
4000 printf("Increasing count %d\n", trans->counter);
4001#endif
4002 exec->counts[trans->counter]++;
4003 }
Daniel Veillard10752282005-08-08 13:05:13 +00004004 if ((trans->count >= 0) &&
4005 (trans->count < REGEXP_ALL_COUNTER)) {
4006#ifdef DEBUG_REGEXP_EXEC
4007 printf("resetting count %d on transition\n",
4008 trans->count);
4009#endif
4010 exec->counts[trans->count] = 0;
4011 }
Daniel Veillard4255d502002-04-16 15:50:10 +00004012#ifdef DEBUG_PUSH
4013 printf("entering state %d\n", trans->to);
4014#endif
Daniel Veillardcc026dc2005-01-12 13:21:17 +00004015 if ((exec->comp->states[trans->to] != NULL) &&
4016 (exec->comp->states[trans->to]->type ==
4017 XML_REGEXP_SINK_STATE)) {
4018 /*
4019 * entering a sink state, save the current state as error
4020 * state.
4021 */
4022 if (exec->errString != NULL)
4023 xmlFree(exec->errString);
4024 exec->errString = xmlStrdup(value);
4025 exec->errState = exec->state;
4026 memcpy(exec->errCounts, exec->counts,
4027 exec->comp->nbCounters * sizeof(int));
4028 }
Daniel Veillard4255d502002-04-16 15:50:10 +00004029 exec->state = exec->comp->states[trans->to];
4030 exec->transno = 0;
4031 if (trans->atom != NULL) {
4032 if (exec->inputStack != NULL) {
4033 exec->index++;
4034 if (exec->index < exec->inputStackNr) {
4035 value = exec->inputStack[exec->index].value;
4036 data = exec->inputStack[exec->index].data;
4037#ifdef DEBUG_PUSH
4038 printf("value loaded: %s\n", value);
4039#endif
4040 } else {
4041 value = NULL;
4042 data = NULL;
4043#ifdef DEBUG_PUSH
4044 printf("end of input\n");
4045#endif
4046 }
4047 } else {
4048 value = NULL;
4049 data = NULL;
4050#ifdef DEBUG_PUSH
4051 printf("end of input\n");
4052#endif
4053 }
4054 }
4055 goto progress;
4056 } else if (ret < 0) {
4057 exec->status = -4;
4058 break;
4059 }
4060 }
4061 if ((exec->transno != 0) || (exec->state->nbTrans == 0)) {
4062rollback:
Daniel Veillard90700152005-01-08 22:05:09 +00004063 /*
Daniel Veillardcc026dc2005-01-12 13:21:17 +00004064 * if we didn't yet rollback on the current input
4065 * store the current state as the error state.
Daniel Veillard90700152005-01-08 22:05:09 +00004066 */
Daniel Veillardcc026dc2005-01-12 13:21:17 +00004067 if ((progress) && (exec->state != NULL) &&
4068 (exec->state->type != XML_REGEXP_SINK_STATE)) {
Daniel Veillard90700152005-01-08 22:05:09 +00004069 progress = 0;
4070 if (exec->errString != NULL)
4071 xmlFree(exec->errString);
4072 exec->errString = xmlStrdup(value);
4073 exec->errState = exec->state;
4074 memcpy(exec->errCounts, exec->counts,
4075 exec->comp->nbCounters * sizeof(int));
4076 }
4077
Daniel Veillard4255d502002-04-16 15:50:10 +00004078 /*
4079 * Failed to find a way out
4080 */
4081 exec->determinist = 0;
4082 xmlFARegExecRollBack(exec);
4083 if (exec->status == 0) {
4084 value = exec->inputStack[exec->index].value;
4085 data = exec->inputStack[exec->index].data;
4086#ifdef DEBUG_PUSH
4087 printf("value loaded: %s\n", value);
4088#endif
4089 }
4090 }
Daniel Veillard90700152005-01-08 22:05:09 +00004091 continue;
Daniel Veillard4255d502002-04-16 15:50:10 +00004092progress:
Daniel Veillard90700152005-01-08 22:05:09 +00004093 progress = 1;
Daniel Veillard4255d502002-04-16 15:50:10 +00004094 continue;
4095 }
4096 if (exec->status == 0) {
4097 return(exec->state->type == XML_REGEXP_FINAL_STATE);
4098 }
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00004099#ifdef DEBUG_ERR
Daniel Veillard90700152005-01-08 22:05:09 +00004100 if (exec->status < 0) {
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00004101 testerr(exec);
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00004102 }
Daniel Veillard90700152005-01-08 22:05:09 +00004103#endif
Daniel Veillard4255d502002-04-16 15:50:10 +00004104 return(exec->status);
4105}
4106
Daniel Veillard52b48c72003-04-13 19:53:42 +00004107/**
Daniel Veillard6e65e152005-08-09 11:09:52 +00004108 * xmlRegExecPushString:
4109 * @exec: a regexp execution context or NULL to indicate the end
4110 * @value: a string token input
4111 * @data: data associated to the token to reuse in callbacks
4112 *
4113 * Push one input token in the execution context
4114 *
4115 * Returns: 1 if the regexp reached a final state, 0 if non-final, and
4116 * a negative value in case of error.
4117 */
4118int
4119xmlRegExecPushString(xmlRegExecCtxtPtr exec, const xmlChar *value,
4120 void *data) {
4121 return(xmlRegExecPushStringInternal(exec, value, data, 0));
4122}
4123
4124/**
Daniel Veillard52b48c72003-04-13 19:53:42 +00004125 * xmlRegExecPushString2:
4126 * @exec: a regexp execution context or NULL to indicate the end
4127 * @value: the first string token input
4128 * @value2: the second string token input
4129 * @data: data associated to the token to reuse in callbacks
4130 *
4131 * Push one input token in the execution context
4132 *
4133 * Returns: 1 if the regexp reached a final state, 0 if non-final, and
4134 * a negative value in case of error.
4135 */
4136int
4137xmlRegExecPushString2(xmlRegExecCtxtPtr exec, const xmlChar *value,
4138 const xmlChar *value2, void *data) {
4139 xmlChar buf[150];
4140 int lenn, lenp, ret;
4141 xmlChar *str;
4142
4143 if (exec == NULL)
4144 return(-1);
4145 if (exec->comp == NULL)
4146 return(-1);
4147 if (exec->status != 0)
4148 return(exec->status);
4149
4150 if (value2 == NULL)
4151 return(xmlRegExecPushString(exec, value, data));
4152
4153 lenn = strlen((char *) value2);
4154 lenp = strlen((char *) value);
4155
4156 if (150 < lenn + lenp + 2) {
Daniel Veillard3c908dc2003-04-19 00:07:51 +00004157 str = (xmlChar *) xmlMallocAtomic(lenn + lenp + 2);
Daniel Veillard52b48c72003-04-13 19:53:42 +00004158 if (str == NULL) {
4159 exec->status = -1;
4160 return(-1);
4161 }
4162 } else {
4163 str = buf;
4164 }
4165 memcpy(&str[0], value, lenp);
Daniel Veillardc0826a72004-08-10 14:17:33 +00004166 str[lenp] = XML_REG_STRING_SEPARATOR;
Daniel Veillard52b48c72003-04-13 19:53:42 +00004167 memcpy(&str[lenp + 1], value2, lenn);
4168 str[lenn + lenp + 1] = 0;
4169
4170 if (exec->comp->compact != NULL)
4171 ret = xmlRegCompactPushString(exec, exec->comp, str, data);
4172 else
Daniel Veillard6e65e152005-08-09 11:09:52 +00004173 ret = xmlRegExecPushStringInternal(exec, str, data, 1);
Daniel Veillard52b48c72003-04-13 19:53:42 +00004174
4175 if (str != buf)
Daniel Veillard0b1ff142005-12-28 21:13:33 +00004176 xmlFree(str);
Daniel Veillard52b48c72003-04-13 19:53:42 +00004177 return(ret);
4178}
4179
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00004180/**
Daniel Veillard77005e62005-07-19 16:26:18 +00004181 * xmlRegExecGetValues:
Daniel Veillardfc0b6f62005-01-09 17:48:02 +00004182 * @exec: a regexp execution context
4183 * @err: error extraction or normal one
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00004184 * @nbval: pointer to the number of accepted values IN/OUT
Daniel Veillardcc026dc2005-01-12 13:21:17 +00004185 * @nbneg: return number of negative transitions
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00004186 * @values: pointer to the array of acceptable values
Daniel Veillardfc0b6f62005-01-09 17:48:02 +00004187 * @terminal: return value if this was a terminal state
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00004188 *
Daniel Veillardfc0b6f62005-01-09 17:48:02 +00004189 * Extract informations from the regexp execution, internal routine to
4190 * implement xmlRegExecNextValues() and xmlRegExecErrInfo()
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00004191 *
4192 * Returns: 0 in case of success or -1 in case of error.
4193 */
Daniel Veillardfc0b6f62005-01-09 17:48:02 +00004194static int
4195xmlRegExecGetValues(xmlRegExecCtxtPtr exec, int err,
Daniel Veillardcc026dc2005-01-12 13:21:17 +00004196 int *nbval, int *nbneg,
4197 xmlChar **values, int *terminal) {
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00004198 int maxval;
Daniel Veillardcc026dc2005-01-12 13:21:17 +00004199 int nb = 0;
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00004200
Daniel Veillardcc026dc2005-01-12 13:21:17 +00004201 if ((exec == NULL) || (nbval == NULL) || (nbneg == NULL) ||
4202 (values == NULL) || (*nbval <= 0))
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00004203 return(-1);
Daniel Veillardfc0b6f62005-01-09 17:48:02 +00004204
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00004205 maxval = *nbval;
4206 *nbval = 0;
Daniel Veillardcc026dc2005-01-12 13:21:17 +00004207 *nbneg = 0;
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00004208 if ((exec->comp != NULL) && (exec->comp->compact != NULL)) {
4209 xmlRegexpPtr comp;
4210 int target, i, state;
4211
4212 comp = exec->comp;
Daniel Veillardfc0b6f62005-01-09 17:48:02 +00004213
4214 if (err) {
4215 if (exec->errStateNo == -1) return(-1);
4216 state = exec->errStateNo;
4217 } else {
4218 state = exec->index;
4219 }
4220 if (terminal != NULL) {
4221 if (comp->compact[state * (comp->nbstrings + 1)] ==
4222 XML_REGEXP_FINAL_STATE)
4223 *terminal = 1;
4224 else
4225 *terminal = 0;
4226 }
Daniel Veillardcc026dc2005-01-12 13:21:17 +00004227 for (i = 0;(i < comp->nbstrings) && (nb < maxval);i++) {
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00004228 target = comp->compact[state * (comp->nbstrings + 1) + i + 1];
Daniel Veillardcc026dc2005-01-12 13:21:17 +00004229 if ((target > 0) && (target <= comp->nbstates) &&
4230 (comp->compact[(target - 1) * (comp->nbstrings + 1)] !=
4231 XML_REGEXP_SINK_STATE)) {
4232 values[nb++] = comp->stringMap[i];
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00004233 (*nbval)++;
4234 }
4235 }
Daniel Veillardcc026dc2005-01-12 13:21:17 +00004236 for (i = 0;(i < comp->nbstrings) && (nb < maxval);i++) {
4237 target = comp->compact[state * (comp->nbstrings + 1) + i + 1];
4238 if ((target > 0) && (target <= comp->nbstates) &&
4239 (comp->compact[(target - 1) * (comp->nbstrings + 1)] ==
4240 XML_REGEXP_SINK_STATE)) {
4241 values[nb++] = comp->stringMap[i];
4242 (*nbneg)++;
4243 }
4244 }
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00004245 } else {
4246 int transno;
4247 xmlRegTransPtr trans;
4248 xmlRegAtomPtr atom;
Daniel Veillardfc0b6f62005-01-09 17:48:02 +00004249 xmlRegStatePtr state;
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00004250
Daniel Veillardfc0b6f62005-01-09 17:48:02 +00004251 if (terminal != NULL) {
4252 if (exec->state->type == XML_REGEXP_FINAL_STATE)
4253 *terminal = 1;
4254 else
4255 *terminal = 0;
4256 }
4257
4258 if (err) {
4259 if (exec->errState == NULL) return(-1);
4260 state = exec->errState;
4261 } else {
4262 if (exec->state == NULL) return(-1);
4263 state = exec->state;
4264 }
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00004265 for (transno = 0;
Daniel Veillardcc026dc2005-01-12 13:21:17 +00004266 (transno < state->nbTrans) && (nb < maxval);
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00004267 transno++) {
Daniel Veillardfc0b6f62005-01-09 17:48:02 +00004268 trans = &state->trans[transno];
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00004269 if (trans->to < 0)
4270 continue;
4271 atom = trans->atom;
4272 if ((atom == NULL) || (atom->valuep == NULL))
4273 continue;
4274 if (trans->count == REGEXP_ALL_LAX_COUNTER) {
Daniel Veillardcc026dc2005-01-12 13:21:17 +00004275 /* this should not be reached but ... */
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00004276 TODO;
4277 } else if (trans->count == REGEXP_ALL_COUNTER) {
Daniel Veillardcc026dc2005-01-12 13:21:17 +00004278 /* this should not be reached but ... */
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00004279 TODO;
4280 } else if (trans->counter >= 0) {
Daniel Veillard11ce4002006-03-10 00:36:23 +00004281 xmlRegCounterPtr counter = NULL;
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00004282 int count;
4283
Daniel Veillardfc0b6f62005-01-09 17:48:02 +00004284 if (err)
4285 count = exec->errCounts[trans->counter];
4286 else
4287 count = exec->counts[trans->counter];
Daniel Veillard11ce4002006-03-10 00:36:23 +00004288 if (exec->comp != NULL)
4289 counter = &exec->comp->counters[trans->counter];
4290 if ((counter == NULL) || (count < counter->max)) {
Daniel Veillard77005e62005-07-19 16:26:18 +00004291 if (atom->neg)
4292 values[nb++] = (xmlChar *) atom->valuep2;
4293 else
4294 values[nb++] = (xmlChar *) atom->valuep;
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00004295 (*nbval)++;
4296 }
4297 } else {
Daniel Veillardcc026dc2005-01-12 13:21:17 +00004298 if ((exec->comp->states[trans->to] != NULL) &&
4299 (exec->comp->states[trans->to]->type !=
4300 XML_REGEXP_SINK_STATE)) {
Daniel Veillard77005e62005-07-19 16:26:18 +00004301 if (atom->neg)
4302 values[nb++] = (xmlChar *) atom->valuep2;
4303 else
4304 values[nb++] = (xmlChar *) atom->valuep;
Daniel Veillardcc026dc2005-01-12 13:21:17 +00004305 (*nbval)++;
4306 }
4307 }
4308 }
4309 for (transno = 0;
4310 (transno < state->nbTrans) && (nb < maxval);
4311 transno++) {
4312 trans = &state->trans[transno];
4313 if (trans->to < 0)
4314 continue;
4315 atom = trans->atom;
4316 if ((atom == NULL) || (atom->valuep == NULL))
4317 continue;
4318 if (trans->count == REGEXP_ALL_LAX_COUNTER) {
4319 continue;
4320 } else if (trans->count == REGEXP_ALL_COUNTER) {
4321 continue;
4322 } else if (trans->counter >= 0) {
4323 continue;
4324 } else {
4325 if ((exec->comp->states[trans->to] != NULL) &&
4326 (exec->comp->states[trans->to]->type ==
4327 XML_REGEXP_SINK_STATE)) {
Daniel Veillard77005e62005-07-19 16:26:18 +00004328 if (atom->neg)
4329 values[nb++] = (xmlChar *) atom->valuep2;
4330 else
4331 values[nb++] = (xmlChar *) atom->valuep;
Daniel Veillardcc026dc2005-01-12 13:21:17 +00004332 (*nbneg)++;
4333 }
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00004334 }
4335 }
4336 }
4337 return(0);
4338}
4339
Daniel Veillardfc0b6f62005-01-09 17:48:02 +00004340/**
4341 * xmlRegExecNextValues:
4342 * @exec: a regexp execution context
4343 * @nbval: pointer to the number of accepted values IN/OUT
Daniel Veillardcc026dc2005-01-12 13:21:17 +00004344 * @nbneg: return number of negative transitions
Daniel Veillardfc0b6f62005-01-09 17:48:02 +00004345 * @values: pointer to the array of acceptable values
4346 * @terminal: return value if this was a terminal state
4347 *
4348 * Extract informations from the regexp execution,
4349 * the parameter @values must point to an array of @nbval string pointers
4350 * on return nbval will contain the number of possible strings in that
4351 * state and the @values array will be updated with them. The string values
4352 * returned will be freed with the @exec context and don't need to be
4353 * deallocated.
4354 *
4355 * Returns: 0 in case of success or -1 in case of error.
4356 */
4357int
Daniel Veillardcc026dc2005-01-12 13:21:17 +00004358xmlRegExecNextValues(xmlRegExecCtxtPtr exec, int *nbval, int *nbneg,
4359 xmlChar **values, int *terminal) {
4360 return(xmlRegExecGetValues(exec, 0, nbval, nbneg, values, terminal));
Daniel Veillardfc0b6f62005-01-09 17:48:02 +00004361}
4362
4363/**
4364 * xmlRegExecErrInfo:
4365 * @exec: a regexp execution context generating an error
4366 * @string: return value for the error string
4367 * @nbval: pointer to the number of accepted values IN/OUT
Daniel Veillardcc026dc2005-01-12 13:21:17 +00004368 * @nbneg: return number of negative transitions
Daniel Veillardfc0b6f62005-01-09 17:48:02 +00004369 * @values: pointer to the array of acceptable values
4370 * @terminal: return value if this was a terminal state
4371 *
4372 * Extract error informations from the regexp execution, the parameter
4373 * @string will be updated with the value pushed and not accepted,
4374 * the parameter @values must point to an array of @nbval string pointers
4375 * on return nbval will contain the number of possible strings in that
4376 * state and the @values array will be updated with them. The string values
4377 * returned will be freed with the @exec context and don't need to be
4378 * deallocated.
4379 *
4380 * Returns: 0 in case of success or -1 in case of error.
4381 */
4382int
4383xmlRegExecErrInfo(xmlRegExecCtxtPtr exec, const xmlChar **string,
Daniel Veillardcc026dc2005-01-12 13:21:17 +00004384 int *nbval, int *nbneg, xmlChar **values, int *terminal) {
Daniel Veillardfc0b6f62005-01-09 17:48:02 +00004385 if (exec == NULL)
4386 return(-1);
4387 if (string != NULL) {
4388 if (exec->status != 0)
4389 *string = exec->errString;
4390 else
4391 *string = NULL;
4392 }
Daniel Veillardcc026dc2005-01-12 13:21:17 +00004393 return(xmlRegExecGetValues(exec, 1, nbval, nbneg, values, terminal));
Daniel Veillardfc0b6f62005-01-09 17:48:02 +00004394}
4395
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00004396#ifdef DEBUG_ERR
4397static void testerr(xmlRegExecCtxtPtr exec) {
4398 const xmlChar *string;
Daniel Veillardcee2b3a2005-01-25 00:22:52 +00004399 xmlChar *values[5];
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00004400 int nb = 5;
Daniel Veillardcc026dc2005-01-12 13:21:17 +00004401 int nbneg;
Daniel Veillardfc0b6f62005-01-09 17:48:02 +00004402 int terminal;
Daniel Veillardcc026dc2005-01-12 13:21:17 +00004403 xmlRegExecErrInfo(exec, &string, &nb, &nbneg, &values[0], &terminal);
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00004404}
4405#endif
4406
Daniel Veillard4255d502002-04-16 15:50:10 +00004407#if 0
4408static int
4409xmlRegExecPushChar(xmlRegExecCtxtPtr exec, int UCS) {
4410 xmlRegTransPtr trans;
4411 xmlRegAtomPtr atom;
4412 int ret;
4413 int codepoint, len;
4414
4415 if (exec == NULL)
4416 return(-1);
4417 if (exec->status != 0)
4418 return(exec->status);
4419
4420 while ((exec->status == 0) &&
4421 ((exec->inputString[exec->index] != 0) ||
4422 (exec->state->type != XML_REGEXP_FINAL_STATE))) {
4423
4424 /*
4425 * End of input on non-terminal state, rollback, however we may
4426 * still have epsilon like transition for counted transitions
4427 * on counters, in that case don't break too early.
4428 */
4429 if ((exec->inputString[exec->index] == 0) && (exec->counts == NULL))
4430 goto rollback;
4431
4432 exec->transcount = 0;
4433 for (;exec->transno < exec->state->nbTrans;exec->transno++) {
4434 trans = &exec->state->trans[exec->transno];
4435 if (trans->to < 0)
4436 continue;
4437 atom = trans->atom;
4438 ret = 0;
4439 if (trans->count >= 0) {
4440 int count;
4441 xmlRegCounterPtr counter;
4442
4443 /*
4444 * A counted transition.
4445 */
4446
4447 count = exec->counts[trans->count];
4448 counter = &exec->comp->counters[trans->count];
4449#ifdef DEBUG_REGEXP_EXEC
4450 printf("testing count %d: val %d, min %d, max %d\n",
4451 trans->count, count, counter->min, counter->max);
4452#endif
4453 ret = ((count >= counter->min) && (count <= counter->max));
4454 } else if (atom == NULL) {
4455 fprintf(stderr, "epsilon transition left at runtime\n");
4456 exec->status = -2;
4457 break;
4458 } else if (exec->inputString[exec->index] != 0) {
4459 codepoint = CUR_SCHAR(&(exec->inputString[exec->index]), len);
4460 ret = xmlRegCheckCharacter(atom, codepoint);
4461 if ((ret == 1) && (atom->min > 0) && (atom->max > 0)) {
4462 xmlRegStatePtr to = exec->comp->states[trans->to];
4463
4464 /*
4465 * this is a multiple input sequence
4466 */
4467 if (exec->state->nbTrans > exec->transno + 1) {
4468 xmlFARegExecSave(exec);
4469 }
4470 exec->transcount = 1;
4471 do {
4472 /*
4473 * Try to progress as much as possible on the input
4474 */
4475 if (exec->transcount == atom->max) {
4476 break;
4477 }
4478 exec->index += len;
4479 /*
4480 * End of input: stop here
4481 */
4482 if (exec->inputString[exec->index] == 0) {
4483 exec->index -= len;
4484 break;
4485 }
4486 if (exec->transcount >= atom->min) {
4487 int transno = exec->transno;
4488 xmlRegStatePtr state = exec->state;
4489
4490 /*
4491 * The transition is acceptable save it
4492 */
4493 exec->transno = -1; /* trick */
4494 exec->state = to;
4495 xmlFARegExecSave(exec);
4496 exec->transno = transno;
4497 exec->state = state;
4498 }
4499 codepoint = CUR_SCHAR(&(exec->inputString[exec->index]),
4500 len);
4501 ret = xmlRegCheckCharacter(atom, codepoint);
4502 exec->transcount++;
4503 } while (ret == 1);
4504 if (exec->transcount < atom->min)
4505 ret = 0;
4506
4507 /*
4508 * If the last check failed but one transition was found
4509 * possible, rollback
4510 */
4511 if (ret < 0)
4512 ret = 0;
4513 if (ret == 0) {
4514 goto rollback;
4515 }
4516 }
4517 }
4518 if (ret == 1) {
4519 if (exec->state->nbTrans > exec->transno + 1) {
4520 xmlFARegExecSave(exec);
4521 }
Daniel Veillard54eb0242006-03-21 23:17:57 +00004522 /*
4523 * restart count for expressions like this ((abc){2})*
4524 */
4525 if (trans->count >= 0) {
4526#ifdef DEBUG_REGEXP_EXEC
4527 printf("Reset count %d\n", trans->count);
4528#endif
4529 exec->counts[trans->count] = 0;
4530 }
Daniel Veillard4255d502002-04-16 15:50:10 +00004531 if (trans->counter >= 0) {
4532#ifdef DEBUG_REGEXP_EXEC
4533 printf("Increasing count %d\n", trans->counter);
4534#endif
4535 exec->counts[trans->counter]++;
4536 }
4537#ifdef DEBUG_REGEXP_EXEC
4538 printf("entering state %d\n", trans->to);
4539#endif
4540 exec->state = exec->comp->states[trans->to];
4541 exec->transno = 0;
4542 if (trans->atom != NULL) {
4543 exec->index += len;
4544 }
4545 goto progress;
4546 } else if (ret < 0) {
4547 exec->status = -4;
4548 break;
4549 }
4550 }
4551 if ((exec->transno != 0) || (exec->state->nbTrans == 0)) {
4552rollback:
4553 /*
4554 * Failed to find a way out
4555 */
4556 exec->determinist = 0;
4557 xmlFARegExecRollBack(exec);
4558 }
4559progress:
4560 continue;
4561 }
4562}
4563#endif
4564/************************************************************************
4565 * *
William M. Brackddf71d62004-05-06 04:17:26 +00004566 * Parser for the Schemas Datatype Regular Expressions *
Daniel Veillard4255d502002-04-16 15:50:10 +00004567 * http://www.w3.org/TR/2001/REC-xmlschema-2-20010502/#regexs *
4568 * *
4569 ************************************************************************/
4570
4571/**
4572 * xmlFAIsChar:
Daniel Veillard441bc322002-04-20 17:38:48 +00004573 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00004574 *
4575 * [10] Char ::= [^.\?*+()|#x5B#x5D]
4576 */
4577static int
4578xmlFAIsChar(xmlRegParserCtxtPtr ctxt) {
4579 int cur;
4580 int len;
4581
4582 cur = CUR_SCHAR(ctxt->cur, len);
4583 if ((cur == '.') || (cur == '\\') || (cur == '?') ||
4584 (cur == '*') || (cur == '+') || (cur == '(') ||
4585 (cur == ')') || (cur == '|') || (cur == 0x5B) ||
4586 (cur == 0x5D) || (cur == 0))
4587 return(-1);
4588 return(cur);
4589}
4590
4591/**
4592 * xmlFAParseCharProp:
Daniel Veillard441bc322002-04-20 17:38:48 +00004593 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00004594 *
4595 * [27] charProp ::= IsCategory | IsBlock
4596 * [28] IsCategory ::= Letters | Marks | Numbers | Punctuation |
4597 * Separators | Symbols | Others
4598 * [29] Letters ::= 'L' [ultmo]?
4599 * [30] Marks ::= 'M' [nce]?
4600 * [31] Numbers ::= 'N' [dlo]?
4601 * [32] Punctuation ::= 'P' [cdseifo]?
4602 * [33] Separators ::= 'Z' [slp]?
4603 * [34] Symbols ::= 'S' [mcko]?
4604 * [35] Others ::= 'C' [cfon]?
4605 * [36] IsBlock ::= 'Is' [a-zA-Z0-9#x2D]+
4606 */
4607static void
4608xmlFAParseCharProp(xmlRegParserCtxtPtr ctxt) {
4609 int cur;
William M. Brack779af002003-08-01 15:55:39 +00004610 xmlRegAtomType type = (xmlRegAtomType) 0;
Daniel Veillard4255d502002-04-16 15:50:10 +00004611 xmlChar *blockName = NULL;
4612
4613 cur = CUR;
4614 if (cur == 'L') {
4615 NEXT;
4616 cur = CUR;
4617 if (cur == 'u') {
4618 NEXT;
4619 type = XML_REGEXP_LETTER_UPPERCASE;
4620 } else if (cur == 'l') {
4621 NEXT;
4622 type = XML_REGEXP_LETTER_LOWERCASE;
4623 } else if (cur == 't') {
4624 NEXT;
4625 type = XML_REGEXP_LETTER_TITLECASE;
4626 } else if (cur == 'm') {
4627 NEXT;
4628 type = XML_REGEXP_LETTER_MODIFIER;
4629 } else if (cur == 'o') {
4630 NEXT;
4631 type = XML_REGEXP_LETTER_OTHERS;
4632 } else {
4633 type = XML_REGEXP_LETTER;
4634 }
4635 } else if (cur == 'M') {
4636 NEXT;
4637 cur = CUR;
4638 if (cur == 'n') {
4639 NEXT;
4640 /* nonspacing */
4641 type = XML_REGEXP_MARK_NONSPACING;
4642 } else if (cur == 'c') {
4643 NEXT;
4644 /* spacing combining */
4645 type = XML_REGEXP_MARK_SPACECOMBINING;
4646 } else if (cur == 'e') {
4647 NEXT;
4648 /* enclosing */
4649 type = XML_REGEXP_MARK_ENCLOSING;
4650 } else {
4651 /* all marks */
4652 type = XML_REGEXP_MARK;
4653 }
4654 } else if (cur == 'N') {
4655 NEXT;
4656 cur = CUR;
4657 if (cur == 'd') {
4658 NEXT;
4659 /* digital */
4660 type = XML_REGEXP_NUMBER_DECIMAL;
4661 } else if (cur == 'l') {
4662 NEXT;
4663 /* letter */
4664 type = XML_REGEXP_NUMBER_LETTER;
4665 } else if (cur == 'o') {
4666 NEXT;
4667 /* other */
4668 type = XML_REGEXP_NUMBER_OTHERS;
4669 } else {
4670 /* all numbers */
4671 type = XML_REGEXP_NUMBER;
4672 }
4673 } else if (cur == 'P') {
4674 NEXT;
4675 cur = CUR;
4676 if (cur == 'c') {
4677 NEXT;
4678 /* connector */
4679 type = XML_REGEXP_PUNCT_CONNECTOR;
4680 } else if (cur == 'd') {
4681 NEXT;
4682 /* dash */
4683 type = XML_REGEXP_PUNCT_DASH;
4684 } else if (cur == 's') {
4685 NEXT;
4686 /* open */
4687 type = XML_REGEXP_PUNCT_OPEN;
4688 } else if (cur == 'e') {
4689 NEXT;
4690 /* close */
4691 type = XML_REGEXP_PUNCT_CLOSE;
4692 } else if (cur == 'i') {
4693 NEXT;
4694 /* initial quote */
4695 type = XML_REGEXP_PUNCT_INITQUOTE;
4696 } else if (cur == 'f') {
4697 NEXT;
4698 /* final quote */
4699 type = XML_REGEXP_PUNCT_FINQUOTE;
4700 } else if (cur == 'o') {
4701 NEXT;
4702 /* other */
4703 type = XML_REGEXP_PUNCT_OTHERS;
4704 } else {
4705 /* all punctuation */
4706 type = XML_REGEXP_PUNCT;
4707 }
4708 } else if (cur == 'Z') {
4709 NEXT;
4710 cur = CUR;
4711 if (cur == 's') {
4712 NEXT;
4713 /* space */
4714 type = XML_REGEXP_SEPAR_SPACE;
4715 } else if (cur == 'l') {
4716 NEXT;
4717 /* line */
4718 type = XML_REGEXP_SEPAR_LINE;
4719 } else if (cur == 'p') {
4720 NEXT;
4721 /* paragraph */
4722 type = XML_REGEXP_SEPAR_PARA;
4723 } else {
4724 /* all separators */
4725 type = XML_REGEXP_SEPAR;
4726 }
4727 } else if (cur == 'S') {
4728 NEXT;
4729 cur = CUR;
4730 if (cur == 'm') {
4731 NEXT;
4732 type = XML_REGEXP_SYMBOL_MATH;
4733 /* math */
4734 } else if (cur == 'c') {
4735 NEXT;
4736 type = XML_REGEXP_SYMBOL_CURRENCY;
4737 /* currency */
4738 } else if (cur == 'k') {
4739 NEXT;
4740 type = XML_REGEXP_SYMBOL_MODIFIER;
4741 /* modifiers */
4742 } else if (cur == 'o') {
4743 NEXT;
4744 type = XML_REGEXP_SYMBOL_OTHERS;
4745 /* other */
4746 } else {
4747 /* all symbols */
4748 type = XML_REGEXP_SYMBOL;
4749 }
4750 } else if (cur == 'C') {
4751 NEXT;
4752 cur = CUR;
4753 if (cur == 'c') {
4754 NEXT;
4755 /* control */
4756 type = XML_REGEXP_OTHER_CONTROL;
4757 } else if (cur == 'f') {
4758 NEXT;
4759 /* format */
4760 type = XML_REGEXP_OTHER_FORMAT;
4761 } else if (cur == 'o') {
4762 NEXT;
4763 /* private use */
4764 type = XML_REGEXP_OTHER_PRIVATE;
4765 } else if (cur == 'n') {
4766 NEXT;
4767 /* not assigned */
4768 type = XML_REGEXP_OTHER_NA;
4769 } else {
4770 /* all others */
4771 type = XML_REGEXP_OTHER;
4772 }
4773 } else if (cur == 'I') {
4774 const xmlChar *start;
4775 NEXT;
4776 cur = CUR;
4777 if (cur != 's') {
4778 ERROR("IsXXXX expected");
4779 return;
4780 }
4781 NEXT;
4782 start = ctxt->cur;
4783 cur = CUR;
4784 if (((cur >= 'a') && (cur <= 'z')) ||
4785 ((cur >= 'A') && (cur <= 'Z')) ||
4786 ((cur >= '0') && (cur <= '9')) ||
4787 (cur == 0x2D)) {
4788 NEXT;
4789 cur = CUR;
4790 while (((cur >= 'a') && (cur <= 'z')) ||
4791 ((cur >= 'A') && (cur <= 'Z')) ||
4792 ((cur >= '0') && (cur <= '9')) ||
4793 (cur == 0x2D)) {
4794 NEXT;
4795 cur = CUR;
4796 }
4797 }
4798 type = XML_REGEXP_BLOCK_NAME;
4799 blockName = xmlStrndup(start, ctxt->cur - start);
4800 } else {
4801 ERROR("Unknown char property");
4802 return;
4803 }
4804 if (ctxt->atom == NULL) {
4805 ctxt->atom = xmlRegNewAtom(ctxt, type);
4806 if (ctxt->atom != NULL)
4807 ctxt->atom->valuep = blockName;
4808 } else if (ctxt->atom->type == XML_REGEXP_RANGES) {
4809 xmlRegAtomAddRange(ctxt, ctxt->atom, ctxt->neg,
4810 type, 0, 0, blockName);
4811 }
4812}
4813
4814/**
4815 * xmlFAParseCharClassEsc:
Daniel Veillard441bc322002-04-20 17:38:48 +00004816 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00004817 *
4818 * [23] charClassEsc ::= ( SingleCharEsc | MultiCharEsc | catEsc | complEsc )
4819 * [24] SingleCharEsc ::= '\' [nrt\|.?*+(){}#x2D#x5B#x5D#x5E]
4820 * [25] catEsc ::= '\p{' charProp '}'
4821 * [26] complEsc ::= '\P{' charProp '}'
4822 * [37] MultiCharEsc ::= '.' | ('\' [sSiIcCdDwW])
4823 */
4824static void
4825xmlFAParseCharClassEsc(xmlRegParserCtxtPtr ctxt) {
4826 int cur;
4827
4828 if (CUR == '.') {
4829 if (ctxt->atom == NULL) {
4830 ctxt->atom = xmlRegNewAtom(ctxt, XML_REGEXP_ANYCHAR);
4831 } else if (ctxt->atom->type == XML_REGEXP_RANGES) {
4832 xmlRegAtomAddRange(ctxt, ctxt->atom, ctxt->neg,
4833 XML_REGEXP_ANYCHAR, 0, 0, NULL);
4834 }
4835 NEXT;
4836 return;
4837 }
4838 if (CUR != '\\') {
4839 ERROR("Escaped sequence: expecting \\");
4840 return;
4841 }
4842 NEXT;
4843 cur = CUR;
4844 if (cur == 'p') {
4845 NEXT;
4846 if (CUR != '{') {
4847 ERROR("Expecting '{'");
4848 return;
4849 }
4850 NEXT;
4851 xmlFAParseCharProp(ctxt);
4852 if (CUR != '}') {
4853 ERROR("Expecting '}'");
4854 return;
4855 }
4856 NEXT;
4857 } else if (cur == 'P') {
4858 NEXT;
4859 if (CUR != '{') {
4860 ERROR("Expecting '{'");
4861 return;
4862 }
4863 NEXT;
4864 xmlFAParseCharProp(ctxt);
4865 ctxt->atom->neg = 1;
4866 if (CUR != '}') {
4867 ERROR("Expecting '}'");
4868 return;
4869 }
4870 NEXT;
4871 } else if ((cur == 'n') || (cur == 'r') || (cur == 't') || (cur == '\\') ||
4872 (cur == '|') || (cur == '.') || (cur == '?') || (cur == '*') ||
4873 (cur == '+') || (cur == '(') || (cur == ')') || (cur == '{') ||
4874 (cur == '}') || (cur == 0x2D) || (cur == 0x5B) || (cur == 0x5D) ||
4875 (cur == 0x5E)) {
4876 if (ctxt->atom == NULL) {
4877 ctxt->atom = xmlRegNewAtom(ctxt, XML_REGEXP_CHARVAL);
Daniel Veillard99c394d2005-07-14 12:58:49 +00004878 if (ctxt->atom != NULL) {
4879 switch (cur) {
4880 case 'n':
4881 ctxt->atom->codepoint = '\n';
4882 break;
4883 case 'r':
4884 ctxt->atom->codepoint = '\r';
4885 break;
4886 case 't':
4887 ctxt->atom->codepoint = '\t';
4888 break;
4889 default:
4890 ctxt->atom->codepoint = cur;
4891 }
4892 }
Daniel Veillard4255d502002-04-16 15:50:10 +00004893 } else if (ctxt->atom->type == XML_REGEXP_RANGES) {
4894 xmlRegAtomAddRange(ctxt, ctxt->atom, ctxt->neg,
4895 XML_REGEXP_CHARVAL, cur, cur, NULL);
4896 }
4897 NEXT;
4898 } else if ((cur == 's') || (cur == 'S') || (cur == 'i') || (cur == 'I') ||
4899 (cur == 'c') || (cur == 'C') || (cur == 'd') || (cur == 'D') ||
4900 (cur == 'w') || (cur == 'W')) {
Daniel Veillardb509f152002-04-17 16:28:10 +00004901 xmlRegAtomType type = XML_REGEXP_ANYSPACE;
Daniel Veillard4255d502002-04-16 15:50:10 +00004902
4903 switch (cur) {
4904 case 's':
4905 type = XML_REGEXP_ANYSPACE;
4906 break;
4907 case 'S':
4908 type = XML_REGEXP_NOTSPACE;
4909 break;
4910 case 'i':
4911 type = XML_REGEXP_INITNAME;
4912 break;
4913 case 'I':
4914 type = XML_REGEXP_NOTINITNAME;
4915 break;
4916 case 'c':
4917 type = XML_REGEXP_NAMECHAR;
4918 break;
4919 case 'C':
4920 type = XML_REGEXP_NOTNAMECHAR;
4921 break;
4922 case 'd':
4923 type = XML_REGEXP_DECIMAL;
4924 break;
4925 case 'D':
4926 type = XML_REGEXP_NOTDECIMAL;
4927 break;
4928 case 'w':
4929 type = XML_REGEXP_REALCHAR;
4930 break;
4931 case 'W':
4932 type = XML_REGEXP_NOTREALCHAR;
4933 break;
4934 }
4935 NEXT;
4936 if (ctxt->atom == NULL) {
4937 ctxt->atom = xmlRegNewAtom(ctxt, type);
4938 } else if (ctxt->atom->type == XML_REGEXP_RANGES) {
4939 xmlRegAtomAddRange(ctxt, ctxt->atom, ctxt->neg,
4940 type, 0, 0, NULL);
4941 }
Daniel Veillardcb4284e2007-04-25 13:55:20 +00004942 } else {
4943 ERROR("Wrong escape sequence, misuse of character '\\'");
Daniel Veillard4255d502002-04-16 15:50:10 +00004944 }
4945}
4946
4947/**
Daniel Veillard4255d502002-04-16 15:50:10 +00004948 * xmlFAParseCharRange:
Daniel Veillard441bc322002-04-20 17:38:48 +00004949 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00004950 *
4951 * [17] charRange ::= seRange | XmlCharRef | XmlCharIncDash
4952 * [18] seRange ::= charOrEsc '-' charOrEsc
4953 * [20] charOrEsc ::= XmlChar | SingleCharEsc
4954 * [21] XmlChar ::= [^\#x2D#x5B#x5D]
4955 * [22] XmlCharIncDash ::= [^\#x5B#x5D]
4956 */
4957static void
4958xmlFAParseCharRange(xmlRegParserCtxtPtr ctxt) {
William M. Brackdc99df92003-12-27 01:54:25 +00004959 int cur, len;
Daniel Veillard4255d502002-04-16 15:50:10 +00004960 int start = -1;
4961 int end = -1;
4962
Daniel Veillard777737e2006-10-17 21:23:17 +00004963 if (CUR == '\0') {
4964 ERROR("Expecting ']'");
4965 return;
4966 }
4967
Daniel Veillard4255d502002-04-16 15:50:10 +00004968 cur = CUR;
4969 if (cur == '\\') {
4970 NEXT;
4971 cur = CUR;
4972 switch (cur) {
4973 case 'n': start = 0xA; break;
4974 case 'r': start = 0xD; break;
4975 case 't': start = 0x9; break;
4976 case '\\': case '|': case '.': case '-': case '^': case '?':
4977 case '*': case '+': case '{': case '}': case '(': case ')':
4978 case '[': case ']':
4979 start = cur; break;
4980 default:
4981 ERROR("Invalid escape value");
4982 return;
4983 }
4984 end = start;
William M. Brackdc99df92003-12-27 01:54:25 +00004985 len = 1;
Daniel Veillard4255d502002-04-16 15:50:10 +00004986 } else if ((cur != 0x5B) && (cur != 0x5D)) {
William M. Brackdc99df92003-12-27 01:54:25 +00004987 end = start = CUR_SCHAR(ctxt->cur, len);
Daniel Veillard4255d502002-04-16 15:50:10 +00004988 } else {
4989 ERROR("Expecting a char range");
4990 return;
4991 }
William M. Bracka9cbf282007-03-21 13:16:33 +00004992 /*
4993 * Since we are "inside" a range, we can assume ctxt->cur is past
4994 * the start of ctxt->string, and PREV should be safe
4995 */
4996 if ((start == '-') && (NXT(1) != ']') && (PREV != '[') && (PREV != '^')) {
4997 NEXTL(len);
Daniel Veillard4255d502002-04-16 15:50:10 +00004998 return;
4999 }
William M. Bracka9cbf282007-03-21 13:16:33 +00005000 NEXTL(len);
Daniel Veillard4255d502002-04-16 15:50:10 +00005001 cur = CUR;
William M. Brack10f1ef42004-03-20 14:51:25 +00005002 if ((cur != '-') || (NXT(1) == ']')) {
Daniel Veillard4255d502002-04-16 15:50:10 +00005003 xmlRegAtomAddRange(ctxt, ctxt->atom, ctxt->neg,
5004 XML_REGEXP_CHARVAL, start, end, NULL);
5005 return;
5006 }
5007 NEXT;
5008 cur = CUR;
5009 if (cur == '\\') {
5010 NEXT;
5011 cur = CUR;
5012 switch (cur) {
5013 case 'n': end = 0xA; break;
5014 case 'r': end = 0xD; break;
5015 case 't': end = 0x9; break;
5016 case '\\': case '|': case '.': case '-': case '^': case '?':
5017 case '*': case '+': case '{': case '}': case '(': case ')':
5018 case '[': case ']':
5019 end = cur; break;
5020 default:
5021 ERROR("Invalid escape value");
5022 return;
5023 }
William M. Brackdc99df92003-12-27 01:54:25 +00005024 len = 1;
Daniel Veillard4255d502002-04-16 15:50:10 +00005025 } else if ((cur != 0x5B) && (cur != 0x5D)) {
William M. Brackdc99df92003-12-27 01:54:25 +00005026 end = CUR_SCHAR(ctxt->cur, len);
Daniel Veillard4255d502002-04-16 15:50:10 +00005027 } else {
5028 ERROR("Expecting the end of a char range");
5029 return;
5030 }
William M. Brackdc99df92003-12-27 01:54:25 +00005031 NEXTL(len);
Daniel Veillard4255d502002-04-16 15:50:10 +00005032 /* TODO check that the values are acceptable character ranges for XML */
5033 if (end < start) {
5034 ERROR("End of range is before start of range");
5035 } else {
5036 xmlRegAtomAddRange(ctxt, ctxt->atom, ctxt->neg,
5037 XML_REGEXP_CHARVAL, start, end, NULL);
5038 }
5039 return;
5040}
5041
5042/**
5043 * xmlFAParsePosCharGroup:
Daniel Veillard441bc322002-04-20 17:38:48 +00005044 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00005045 *
5046 * [14] posCharGroup ::= ( charRange | charClassEsc )+
5047 */
5048static void
5049xmlFAParsePosCharGroup(xmlRegParserCtxtPtr ctxt) {
5050 do {
Daniel Veillard041b6872008-02-08 10:37:18 +00005051 if (CUR == '\\') {
Daniel Veillard4255d502002-04-16 15:50:10 +00005052 xmlFAParseCharClassEsc(ctxt);
5053 } else {
5054 xmlFAParseCharRange(ctxt);
5055 }
5056 } while ((CUR != ']') && (CUR != '^') && (CUR != '-') &&
Daniel Veillard777737e2006-10-17 21:23:17 +00005057 (CUR != 0) && (ctxt->error == 0));
Daniel Veillard4255d502002-04-16 15:50:10 +00005058}
5059
5060/**
5061 * xmlFAParseCharGroup:
Daniel Veillard441bc322002-04-20 17:38:48 +00005062 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00005063 *
5064 * [13] charGroup ::= posCharGroup | negCharGroup | charClassSub
5065 * [15] negCharGroup ::= '^' posCharGroup
5066 * [16] charClassSub ::= ( posCharGroup | negCharGroup ) '-' charClassExpr
5067 * [12] charClassExpr ::= '[' charGroup ']'
5068 */
5069static void
5070xmlFAParseCharGroup(xmlRegParserCtxtPtr ctxt) {
5071 int n = ctxt->neg;
5072 while ((CUR != ']') && (ctxt->error == 0)) {
5073 if (CUR == '^') {
5074 int neg = ctxt->neg;
5075
5076 NEXT;
5077 ctxt->neg = !ctxt->neg;
5078 xmlFAParsePosCharGroup(ctxt);
5079 ctxt->neg = neg;
William M. Brack10f1ef42004-03-20 14:51:25 +00005080 } else if ((CUR == '-') && (NXT(1) == '[')) {
Daniel Veillardf8b9de32003-11-24 14:27:26 +00005081 int neg = ctxt->neg;
Daniel Veillardf8b9de32003-11-24 14:27:26 +00005082 ctxt->neg = 2;
William M. Brack10f1ef42004-03-20 14:51:25 +00005083 NEXT; /* eat the '-' */
5084 NEXT; /* eat the '[' */
Daniel Veillard4255d502002-04-16 15:50:10 +00005085 xmlFAParseCharGroup(ctxt);
5086 if (CUR == ']') {
5087 NEXT;
5088 } else {
5089 ERROR("charClassExpr: ']' expected");
5090 break;
5091 }
Daniel Veillardf8b9de32003-11-24 14:27:26 +00005092 ctxt->neg = neg;
Daniel Veillard4255d502002-04-16 15:50:10 +00005093 break;
5094 } else if (CUR != ']') {
5095 xmlFAParsePosCharGroup(ctxt);
5096 }
5097 }
5098 ctxt->neg = n;
5099}
5100
5101/**
5102 * xmlFAParseCharClass:
Daniel Veillard441bc322002-04-20 17:38:48 +00005103 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00005104 *
5105 * [11] charClass ::= charClassEsc | charClassExpr
5106 * [12] charClassExpr ::= '[' charGroup ']'
5107 */
5108static void
5109xmlFAParseCharClass(xmlRegParserCtxtPtr ctxt) {
5110 if (CUR == '[') {
5111 NEXT;
5112 ctxt->atom = xmlRegNewAtom(ctxt, XML_REGEXP_RANGES);
5113 if (ctxt->atom == NULL)
5114 return;
5115 xmlFAParseCharGroup(ctxt);
5116 if (CUR == ']') {
5117 NEXT;
5118 } else {
5119 ERROR("xmlFAParseCharClass: ']' expected");
5120 }
5121 } else {
5122 xmlFAParseCharClassEsc(ctxt);
5123 }
5124}
5125
5126/**
5127 * xmlFAParseQuantExact:
Daniel Veillard441bc322002-04-20 17:38:48 +00005128 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00005129 *
5130 * [8] QuantExact ::= [0-9]+
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00005131 *
5132 * Returns 0 if success or -1 in case of error
Daniel Veillard4255d502002-04-16 15:50:10 +00005133 */
5134static int
5135xmlFAParseQuantExact(xmlRegParserCtxtPtr ctxt) {
5136 int ret = 0;
5137 int ok = 0;
5138
5139 while ((CUR >= '0') && (CUR <= '9')) {
5140 ret = ret * 10 + (CUR - '0');
5141 ok = 1;
5142 NEXT;
5143 }
5144 if (ok != 1) {
5145 return(-1);
5146 }
5147 return(ret);
5148}
5149
5150/**
5151 * xmlFAParseQuantifier:
Daniel Veillard441bc322002-04-20 17:38:48 +00005152 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00005153 *
5154 * [4] quantifier ::= [?*+] | ( '{' quantity '}' )
5155 * [5] quantity ::= quantRange | quantMin | QuantExact
5156 * [6] quantRange ::= QuantExact ',' QuantExact
5157 * [7] quantMin ::= QuantExact ','
5158 * [8] QuantExact ::= [0-9]+
5159 */
5160static int
5161xmlFAParseQuantifier(xmlRegParserCtxtPtr ctxt) {
5162 int cur;
5163
5164 cur = CUR;
5165 if ((cur == '?') || (cur == '*') || (cur == '+')) {
5166 if (ctxt->atom != NULL) {
5167 if (cur == '?')
5168 ctxt->atom->quant = XML_REGEXP_QUANT_OPT;
5169 else if (cur == '*')
5170 ctxt->atom->quant = XML_REGEXP_QUANT_MULT;
5171 else if (cur == '+')
5172 ctxt->atom->quant = XML_REGEXP_QUANT_PLUS;
5173 }
5174 NEXT;
5175 return(1);
5176 }
5177 if (cur == '{') {
5178 int min = 0, max = 0;
5179
5180 NEXT;
5181 cur = xmlFAParseQuantExact(ctxt);
5182 if (cur >= 0)
5183 min = cur;
5184 if (CUR == ',') {
5185 NEXT;
Daniel Veillardebe48c62003-12-03 12:12:27 +00005186 if (CUR == '}')
5187 max = INT_MAX;
5188 else {
5189 cur = xmlFAParseQuantExact(ctxt);
5190 if (cur >= 0)
5191 max = cur;
5192 else {
5193 ERROR("Improper quantifier");
5194 }
5195 }
Daniel Veillard4255d502002-04-16 15:50:10 +00005196 }
5197 if (CUR == '}') {
5198 NEXT;
5199 } else {
5200 ERROR("Unterminated quantifier");
5201 }
5202 if (max == 0)
5203 max = min;
5204 if (ctxt->atom != NULL) {
5205 ctxt->atom->quant = XML_REGEXP_QUANT_RANGE;
5206 ctxt->atom->min = min;
5207 ctxt->atom->max = max;
5208 }
5209 return(1);
5210 }
5211 return(0);
5212}
5213
5214/**
5215 * xmlFAParseAtom:
Daniel Veillard441bc322002-04-20 17:38:48 +00005216 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00005217 *
5218 * [9] atom ::= Char | charClass | ( '(' regExp ')' )
5219 */
5220static int
5221xmlFAParseAtom(xmlRegParserCtxtPtr ctxt) {
5222 int codepoint, len;
5223
5224 codepoint = xmlFAIsChar(ctxt);
5225 if (codepoint > 0) {
5226 ctxt->atom = xmlRegNewAtom(ctxt, XML_REGEXP_CHARVAL);
5227 if (ctxt->atom == NULL)
5228 return(-1);
5229 codepoint = CUR_SCHAR(ctxt->cur, len);
5230 ctxt->atom->codepoint = codepoint;
5231 NEXTL(len);
5232 return(1);
5233 } else if (CUR == '|') {
5234 return(0);
5235 } else if (CUR == 0) {
5236 return(0);
5237 } else if (CUR == ')') {
5238 return(0);
5239 } else if (CUR == '(') {
Daniel Veillard76d59b62007-08-22 16:29:21 +00005240 xmlRegStatePtr start, oldend, start0;
Daniel Veillard4255d502002-04-16 15:50:10 +00005241
5242 NEXT;
Daniel Veillard76d59b62007-08-22 16:29:21 +00005243 /*
5244 * this extra Epsilon transition is needed if we count with 0 allowed
5245 * unfortunately this can't be known at that point
5246 */
5247 xmlFAGenerateEpsilonTransition(ctxt, ctxt->state, NULL);
5248 start0 = ctxt->state;
Daniel Veillard4255d502002-04-16 15:50:10 +00005249 xmlFAGenerateEpsilonTransition(ctxt, ctxt->state, NULL);
5250 start = ctxt->state;
5251 oldend = ctxt->end;
5252 ctxt->end = NULL;
5253 ctxt->atom = NULL;
5254 xmlFAParseRegExp(ctxt, 0);
5255 if (CUR == ')') {
5256 NEXT;
5257 } else {
5258 ERROR("xmlFAParseAtom: expecting ')'");
5259 }
5260 ctxt->atom = xmlRegNewAtom(ctxt, XML_REGEXP_SUBREG);
5261 if (ctxt->atom == NULL)
5262 return(-1);
5263 ctxt->atom->start = start;
Daniel Veillard76d59b62007-08-22 16:29:21 +00005264 ctxt->atom->start0 = start0;
Daniel Veillard4255d502002-04-16 15:50:10 +00005265 ctxt->atom->stop = ctxt->state;
5266 ctxt->end = oldend;
5267 return(1);
5268 } else if ((CUR == '[') || (CUR == '\\') || (CUR == '.')) {
5269 xmlFAParseCharClass(ctxt);
5270 return(1);
5271 }
5272 return(0);
5273}
5274
5275/**
5276 * xmlFAParsePiece:
Daniel Veillard441bc322002-04-20 17:38:48 +00005277 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00005278 *
5279 * [3] piece ::= atom quantifier?
5280 */
5281static int
5282xmlFAParsePiece(xmlRegParserCtxtPtr ctxt) {
5283 int ret;
5284
5285 ctxt->atom = NULL;
5286 ret = xmlFAParseAtom(ctxt);
5287 if (ret == 0)
5288 return(0);
5289 if (ctxt->atom == NULL) {
5290 ERROR("internal: no atom generated");
5291 }
5292 xmlFAParseQuantifier(ctxt);
5293 return(1);
5294}
5295
5296/**
5297 * xmlFAParseBranch:
Daniel Veillard441bc322002-04-20 17:38:48 +00005298 * @ctxt: a regexp parser context
Daniel Veillard54eb0242006-03-21 23:17:57 +00005299 * @to: optional target to the end of the branch
5300 *
5301 * @to is used to optimize by removing duplicate path in automata
5302 * in expressions like (a|b)(c|d)
Daniel Veillard4255d502002-04-16 15:50:10 +00005303 *
5304 * [2] branch ::= piece*
5305 */
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00005306static int
Daniel Veillard54eb0242006-03-21 23:17:57 +00005307xmlFAParseBranch(xmlRegParserCtxtPtr ctxt, xmlRegStatePtr to) {
Daniel Veillard4255d502002-04-16 15:50:10 +00005308 xmlRegStatePtr previous;
Daniel Veillard4255d502002-04-16 15:50:10 +00005309 int ret;
5310
5311 previous = ctxt->state;
5312 ret = xmlFAParsePiece(ctxt);
5313 if (ret != 0) {
Daniel Veillard54eb0242006-03-21 23:17:57 +00005314 if (xmlFAGenerateTransitions(ctxt, previous,
5315 (CUR=='|' || CUR==')') ? to : NULL, ctxt->atom) < 0)
Daniel Veillard2cbf5962004-03-31 15:50:43 +00005316 return(-1);
5317 previous = ctxt->state;
Daniel Veillard4255d502002-04-16 15:50:10 +00005318 ctxt->atom = NULL;
5319 }
5320 while ((ret != 0) && (ctxt->error == 0)) {
5321 ret = xmlFAParsePiece(ctxt);
5322 if (ret != 0) {
Daniel Veillard54eb0242006-03-21 23:17:57 +00005323 if (xmlFAGenerateTransitions(ctxt, previous,
5324 (CUR=='|' || CUR==')') ? to : NULL, ctxt->atom) < 0)
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00005325 return(-1);
Daniel Veillard4255d502002-04-16 15:50:10 +00005326 previous = ctxt->state;
5327 ctxt->atom = NULL;
5328 }
5329 }
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00005330 return(0);
Daniel Veillard4255d502002-04-16 15:50:10 +00005331}
5332
5333/**
5334 * xmlFAParseRegExp:
Daniel Veillard441bc322002-04-20 17:38:48 +00005335 * @ctxt: a regexp parser context
William M. Brackddf71d62004-05-06 04:17:26 +00005336 * @top: is this the top-level expression ?
Daniel Veillard4255d502002-04-16 15:50:10 +00005337 *
5338 * [1] regExp ::= branch ( '|' branch )*
5339 */
5340static void
5341xmlFAParseRegExp(xmlRegParserCtxtPtr ctxt, int top) {
Daniel Veillardc7e3cc42004-09-28 12:33:52 +00005342 xmlRegStatePtr start, end;
Daniel Veillard4255d502002-04-16 15:50:10 +00005343
Daniel Veillard2cbf5962004-03-31 15:50:43 +00005344 /* if not top start should have been generated by an epsilon trans */
Daniel Veillard4255d502002-04-16 15:50:10 +00005345 start = ctxt->state;
Daniel Veillard2cbf5962004-03-31 15:50:43 +00005346 ctxt->end = NULL;
Daniel Veillard54eb0242006-03-21 23:17:57 +00005347 xmlFAParseBranch(ctxt, NULL);
Daniel Veillard2cbf5962004-03-31 15:50:43 +00005348 if (top) {
5349#ifdef DEBUG_REGEXP_GRAPH
5350 printf("State %d is final\n", ctxt->state->no);
5351#endif
5352 ctxt->state->type = XML_REGEXP_FINAL_STATE;
5353 }
Daniel Veillard4255d502002-04-16 15:50:10 +00005354 if (CUR != '|') {
5355 ctxt->end = ctxt->state;
5356 return;
5357 }
5358 end = ctxt->state;
5359 while ((CUR == '|') && (ctxt->error == 0)) {
5360 NEXT;
5361 ctxt->state = start;
Daniel Veillard2cbf5962004-03-31 15:50:43 +00005362 ctxt->end = NULL;
Daniel Veillard54eb0242006-03-21 23:17:57 +00005363 xmlFAParseBranch(ctxt, end);
Daniel Veillard4255d502002-04-16 15:50:10 +00005364 }
Daniel Veillard2cbf5962004-03-31 15:50:43 +00005365 if (!top) {
5366 ctxt->state = end;
5367 ctxt->end = end;
5368 }
Daniel Veillard4255d502002-04-16 15:50:10 +00005369}
5370
5371/************************************************************************
5372 * *
5373 * The basic API *
5374 * *
5375 ************************************************************************/
5376
5377/**
5378 * xmlRegexpPrint:
5379 * @output: the file for the output debug
5380 * @regexp: the compiled regexp
5381 *
5382 * Print the content of the compiled regular expression
5383 */
5384void
5385xmlRegexpPrint(FILE *output, xmlRegexpPtr regexp) {
5386 int i;
5387
Daniel Veillarda82b1822004-11-08 16:24:57 +00005388 if (output == NULL)
5389 return;
Daniel Veillard4255d502002-04-16 15:50:10 +00005390 fprintf(output, " regexp: ");
5391 if (regexp == NULL) {
5392 fprintf(output, "NULL\n");
5393 return;
5394 }
5395 fprintf(output, "'%s' ", regexp->string);
5396 fprintf(output, "\n");
5397 fprintf(output, "%d atoms:\n", regexp->nbAtoms);
5398 for (i = 0;i < regexp->nbAtoms; i++) {
5399 fprintf(output, " %02d ", i);
5400 xmlRegPrintAtom(output, regexp->atoms[i]);
5401 }
5402 fprintf(output, "%d states:", regexp->nbStates);
5403 fprintf(output, "\n");
5404 for (i = 0;i < regexp->nbStates; i++) {
5405 xmlRegPrintState(output, regexp->states[i]);
5406 }
5407 fprintf(output, "%d counters:\n", regexp->nbCounters);
5408 for (i = 0;i < regexp->nbCounters; i++) {
5409 fprintf(output, " %d: min %d max %d\n", i, regexp->counters[i].min,
5410 regexp->counters[i].max);
5411 }
5412}
5413
5414/**
5415 * xmlRegexpCompile:
5416 * @regexp: a regular expression string
5417 *
5418 * Parses a regular expression conforming to XML Schemas Part 2 Datatype
William M. Brackddf71d62004-05-06 04:17:26 +00005419 * Appendix F and builds an automata suitable for testing strings against
Daniel Veillard4255d502002-04-16 15:50:10 +00005420 * that regular expression
5421 *
5422 * Returns the compiled expression or NULL in case of error
5423 */
5424xmlRegexpPtr
5425xmlRegexpCompile(const xmlChar *regexp) {
5426 xmlRegexpPtr ret;
5427 xmlRegParserCtxtPtr ctxt;
5428
5429 ctxt = xmlRegNewParserCtxt(regexp);
5430 if (ctxt == NULL)
5431 return(NULL);
5432
5433 /* initialize the parser */
5434 ctxt->end = NULL;
5435 ctxt->start = ctxt->state = xmlRegNewState(ctxt);
5436 xmlRegStatePush(ctxt, ctxt->start);
5437
5438 /* parse the expression building an automata */
5439 xmlFAParseRegExp(ctxt, 1);
5440 if (CUR != 0) {
5441 ERROR("xmlFAParseRegExp: extra characters");
5442 }
Daniel Veillardcb4284e2007-04-25 13:55:20 +00005443 if (ctxt->error != 0) {
5444 xmlRegFreeParserCtxt(ctxt);
5445 return(NULL);
5446 }
Daniel Veillard4255d502002-04-16 15:50:10 +00005447 ctxt->end = ctxt->state;
5448 ctxt->start->type = XML_REGEXP_START_STATE;
5449 ctxt->end->type = XML_REGEXP_FINAL_STATE;
5450
5451 /* remove the Epsilon except for counted transitions */
5452 xmlFAEliminateEpsilonTransitions(ctxt);
5453
5454
5455 if (ctxt->error != 0) {
5456 xmlRegFreeParserCtxt(ctxt);
5457 return(NULL);
5458 }
5459 ret = xmlRegEpxFromParse(ctxt);
5460 xmlRegFreeParserCtxt(ctxt);
5461 return(ret);
5462}
5463
5464/**
5465 * xmlRegexpExec:
5466 * @comp: the compiled regular expression
5467 * @content: the value to check against the regular expression
5468 *
William M. Brackddf71d62004-05-06 04:17:26 +00005469 * Check if the regular expression generates the value
Daniel Veillard4255d502002-04-16 15:50:10 +00005470 *
William M. Brackddf71d62004-05-06 04:17:26 +00005471 * Returns 1 if it matches, 0 if not and a negative value in case of error
Daniel Veillard4255d502002-04-16 15:50:10 +00005472 */
5473int
5474xmlRegexpExec(xmlRegexpPtr comp, const xmlChar *content) {
5475 if ((comp == NULL) || (content == NULL))
5476 return(-1);
5477 return(xmlFARegExec(comp, content));
5478}
5479
5480/**
Daniel Veillard23e73572002-09-19 19:56:43 +00005481 * xmlRegexpIsDeterminist:
5482 * @comp: the compiled regular expression
5483 *
5484 * Check if the regular expression is determinist
5485 *
William M. Brackddf71d62004-05-06 04:17:26 +00005486 * Returns 1 if it yes, 0 if not and a negative value in case of error
Daniel Veillard23e73572002-09-19 19:56:43 +00005487 */
5488int
5489xmlRegexpIsDeterminist(xmlRegexpPtr comp) {
5490 xmlAutomataPtr am;
5491 int ret;
5492
5493 if (comp == NULL)
5494 return(-1);
5495 if (comp->determinist != -1)
5496 return(comp->determinist);
5497
5498 am = xmlNewAutomata();
Daniel Veillardbd9afb52002-09-25 22:25:35 +00005499 if (am->states != NULL) {
5500 int i;
5501
5502 for (i = 0;i < am->nbStates;i++)
5503 xmlRegFreeState(am->states[i]);
5504 xmlFree(am->states);
5505 }
Daniel Veillard23e73572002-09-19 19:56:43 +00005506 am->nbAtoms = comp->nbAtoms;
5507 am->atoms = comp->atoms;
5508 am->nbStates = comp->nbStates;
5509 am->states = comp->states;
5510 am->determinist = -1;
Daniel Veillard1ba2aca2009-08-31 16:47:39 +02005511 am->flags = comp->flags;
Daniel Veillard23e73572002-09-19 19:56:43 +00005512 ret = xmlFAComputesDeterminism(am);
5513 am->atoms = NULL;
5514 am->states = NULL;
5515 xmlFreeAutomata(am);
Daniel Veillard1ba2aca2009-08-31 16:47:39 +02005516 comp->determinist = ret;
Daniel Veillard23e73572002-09-19 19:56:43 +00005517 return(ret);
5518}
5519
5520/**
Daniel Veillard4255d502002-04-16 15:50:10 +00005521 * xmlRegFreeRegexp:
5522 * @regexp: the regexp
5523 *
5524 * Free a regexp
5525 */
5526void
5527xmlRegFreeRegexp(xmlRegexpPtr regexp) {
5528 int i;
5529 if (regexp == NULL)
5530 return;
5531
5532 if (regexp->string != NULL)
5533 xmlFree(regexp->string);
5534 if (regexp->states != NULL) {
5535 for (i = 0;i < regexp->nbStates;i++)
5536 xmlRegFreeState(regexp->states[i]);
5537 xmlFree(regexp->states);
5538 }
5539 if (regexp->atoms != NULL) {
5540 for (i = 0;i < regexp->nbAtoms;i++)
5541 xmlRegFreeAtom(regexp->atoms[i]);
5542 xmlFree(regexp->atoms);
5543 }
5544 if (regexp->counters != NULL)
5545 xmlFree(regexp->counters);
Daniel Veillard23e73572002-09-19 19:56:43 +00005546 if (regexp->compact != NULL)
5547 xmlFree(regexp->compact);
Daniel Veillard118aed72002-09-24 14:13:13 +00005548 if (regexp->transdata != NULL)
5549 xmlFree(regexp->transdata);
Daniel Veillard23e73572002-09-19 19:56:43 +00005550 if (regexp->stringMap != NULL) {
5551 for (i = 0; i < regexp->nbstrings;i++)
5552 xmlFree(regexp->stringMap[i]);
5553 xmlFree(regexp->stringMap);
5554 }
5555
Daniel Veillard4255d502002-04-16 15:50:10 +00005556 xmlFree(regexp);
5557}
5558
5559#ifdef LIBXML_AUTOMATA_ENABLED
5560/************************************************************************
5561 * *
5562 * The Automata interface *
5563 * *
5564 ************************************************************************/
5565
5566/**
5567 * xmlNewAutomata:
5568 *
5569 * Create a new automata
5570 *
5571 * Returns the new object or NULL in case of failure
5572 */
5573xmlAutomataPtr
5574xmlNewAutomata(void) {
5575 xmlAutomataPtr ctxt;
5576
5577 ctxt = xmlRegNewParserCtxt(NULL);
5578 if (ctxt == NULL)
5579 return(NULL);
5580
5581 /* initialize the parser */
5582 ctxt->end = NULL;
5583 ctxt->start = ctxt->state = xmlRegNewState(ctxt);
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00005584 if (ctxt->start == NULL) {
5585 xmlFreeAutomata(ctxt);
5586 return(NULL);
5587 }
Daniel Veillardd0271472006-01-02 10:22:02 +00005588 ctxt->start->type = XML_REGEXP_START_STATE;
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00005589 if (xmlRegStatePush(ctxt, ctxt->start) < 0) {
5590 xmlRegFreeState(ctxt->start);
5591 xmlFreeAutomata(ctxt);
5592 return(NULL);
5593 }
Daniel Veillard1ba2aca2009-08-31 16:47:39 +02005594 ctxt->flags = 0;
Daniel Veillard4255d502002-04-16 15:50:10 +00005595
5596 return(ctxt);
5597}
5598
5599/**
5600 * xmlFreeAutomata:
5601 * @am: an automata
5602 *
5603 * Free an automata
5604 */
5605void
5606xmlFreeAutomata(xmlAutomataPtr am) {
5607 if (am == NULL)
5608 return;
5609 xmlRegFreeParserCtxt(am);
5610}
5611
5612/**
Daniel Veillard1ba2aca2009-08-31 16:47:39 +02005613 * xmlAutomataSetFlags
5614 * @am: an automata
5615 * @flags: a set of internal flags
5616 *
5617 * Set some flags on the automata
5618 */
5619void
5620xmlAutomataSetFlags(xmlAutomataPtr am, int flags) {
5621 if (am == NULL)
5622 return;
5623 am->flags |= flags;
5624}
5625
5626/**
Daniel Veillard4255d502002-04-16 15:50:10 +00005627 * xmlAutomataGetInitState:
5628 * @am: an automata
5629 *
Daniel Veillarda9b66d02002-12-11 14:23:49 +00005630 * Initial state lookup
5631 *
Daniel Veillard4255d502002-04-16 15:50:10 +00005632 * Returns the initial state of the automata
5633 */
5634xmlAutomataStatePtr
5635xmlAutomataGetInitState(xmlAutomataPtr am) {
5636 if (am == NULL)
5637 return(NULL);
5638 return(am->start);
5639}
5640
5641/**
5642 * xmlAutomataSetFinalState:
5643 * @am: an automata
5644 * @state: a state in this automata
5645 *
5646 * Makes that state a final state
5647 *
5648 * Returns 0 or -1 in case of error
5649 */
5650int
5651xmlAutomataSetFinalState(xmlAutomataPtr am, xmlAutomataStatePtr state) {
5652 if ((am == NULL) || (state == NULL))
5653 return(-1);
5654 state->type = XML_REGEXP_FINAL_STATE;
5655 return(0);
5656}
5657
5658/**
5659 * xmlAutomataNewTransition:
5660 * @am: an automata
5661 * @from: the starting point of the transition
5662 * @to: the target point of the transition or NULL
5663 * @token: the input string associated to that transition
5664 * @data: data passed to the callback function if the transition is activated
5665 *
William M. Brackddf71d62004-05-06 04:17:26 +00005666 * If @to is NULL, this creates first a new target state in the automata
Daniel Veillard4255d502002-04-16 15:50:10 +00005667 * and then adds a transition from the @from state to the target state
5668 * activated by the value of @token
5669 *
5670 * Returns the target state or NULL in case of error
5671 */
5672xmlAutomataStatePtr
5673xmlAutomataNewTransition(xmlAutomataPtr am, xmlAutomataStatePtr from,
5674 xmlAutomataStatePtr to, const xmlChar *token,
5675 void *data) {
5676 xmlRegAtomPtr atom;
5677
5678 if ((am == NULL) || (from == NULL) || (token == NULL))
5679 return(NULL);
5680 atom = xmlRegNewAtom(am, XML_REGEXP_STRING);
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00005681 if (atom == NULL)
5682 return(NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +00005683 atom->data = data;
5684 if (atom == NULL)
5685 return(NULL);
5686 atom->valuep = xmlStrdup(token);
5687
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00005688 if (xmlFAGenerateTransitions(am, from, to, atom) < 0) {
5689 xmlRegFreeAtom(atom);
5690 return(NULL);
5691 }
Daniel Veillard4255d502002-04-16 15:50:10 +00005692 if (to == NULL)
5693 return(am->state);
5694 return(to);
5695}
5696
5697/**
Daniel Veillard52b48c72003-04-13 19:53:42 +00005698 * xmlAutomataNewTransition2:
5699 * @am: an automata
5700 * @from: the starting point of the transition
5701 * @to: the target point of the transition or NULL
5702 * @token: the first input string associated to that transition
5703 * @token2: the second input string associated to that transition
5704 * @data: data passed to the callback function if the transition is activated
5705 *
William M. Brackddf71d62004-05-06 04:17:26 +00005706 * If @to is NULL, this creates first a new target state in the automata
Daniel Veillard52b48c72003-04-13 19:53:42 +00005707 * and then adds a transition from the @from state to the target state
5708 * activated by the value of @token
5709 *
5710 * Returns the target state or NULL in case of error
5711 */
5712xmlAutomataStatePtr
5713xmlAutomataNewTransition2(xmlAutomataPtr am, xmlAutomataStatePtr from,
5714 xmlAutomataStatePtr to, const xmlChar *token,
5715 const xmlChar *token2, void *data) {
5716 xmlRegAtomPtr atom;
5717
5718 if ((am == NULL) || (from == NULL) || (token == NULL))
5719 return(NULL);
5720 atom = xmlRegNewAtom(am, XML_REGEXP_STRING);
Daniel Veillard52b48c72003-04-13 19:53:42 +00005721 if (atom == NULL)
5722 return(NULL);
Daniel Veillard11ce4002006-03-10 00:36:23 +00005723 atom->data = data;
Daniel Veillard52b48c72003-04-13 19:53:42 +00005724 if ((token2 == NULL) || (*token2 == 0)) {
5725 atom->valuep = xmlStrdup(token);
5726 } else {
5727 int lenn, lenp;
5728 xmlChar *str;
5729
5730 lenn = strlen((char *) token2);
5731 lenp = strlen((char *) token);
5732
Daniel Veillard3c908dc2003-04-19 00:07:51 +00005733 str = (xmlChar *) xmlMallocAtomic(lenn + lenp + 2);
Daniel Veillard52b48c72003-04-13 19:53:42 +00005734 if (str == NULL) {
5735 xmlRegFreeAtom(atom);
5736 return(NULL);
5737 }
5738 memcpy(&str[0], token, lenp);
5739 str[lenp] = '|';
5740 memcpy(&str[lenp + 1], token2, lenn);
5741 str[lenn + lenp + 1] = 0;
5742
5743 atom->valuep = str;
5744 }
5745
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00005746 if (xmlFAGenerateTransitions(am, from, to, atom) < 0) {
5747 xmlRegFreeAtom(atom);
5748 return(NULL);
5749 }
Daniel Veillard52b48c72003-04-13 19:53:42 +00005750 if (to == NULL)
5751 return(am->state);
5752 return(to);
5753}
5754
5755/**
Daniel Veillard9efc4762005-07-19 14:33:55 +00005756 * xmlAutomataNewNegTrans:
5757 * @am: an automata
5758 * @from: the starting point of the transition
5759 * @to: the target point of the transition or NULL
5760 * @token: the first input string associated to that transition
5761 * @token2: the second input string associated to that transition
5762 * @data: data passed to the callback function if the transition is activated
5763 *
5764 * If @to is NULL, this creates first a new target state in the automata
5765 * and then adds a transition from the @from state to the target state
5766 * activated by any value except (@token,@token2)
Daniel Veillard6e65e152005-08-09 11:09:52 +00005767 * Note that if @token2 is not NULL, then (X, NULL) won't match to follow
5768 # the semantic of XSD ##other
Daniel Veillard9efc4762005-07-19 14:33:55 +00005769 *
5770 * Returns the target state or NULL in case of error
5771 */
5772xmlAutomataStatePtr
5773xmlAutomataNewNegTrans(xmlAutomataPtr am, xmlAutomataStatePtr from,
5774 xmlAutomataStatePtr to, const xmlChar *token,
5775 const xmlChar *token2, void *data) {
5776 xmlRegAtomPtr atom;
Daniel Veillard77005e62005-07-19 16:26:18 +00005777 xmlChar err_msg[200];
Daniel Veillard9efc4762005-07-19 14:33:55 +00005778
5779 if ((am == NULL) || (from == NULL) || (token == NULL))
5780 return(NULL);
5781 atom = xmlRegNewAtom(am, XML_REGEXP_STRING);
5782 if (atom == NULL)
5783 return(NULL);
5784 atom->data = data;
5785 atom->neg = 1;
5786 if ((token2 == NULL) || (*token2 == 0)) {
5787 atom->valuep = xmlStrdup(token);
5788 } else {
5789 int lenn, lenp;
5790 xmlChar *str;
5791
5792 lenn = strlen((char *) token2);
5793 lenp = strlen((char *) token);
5794
5795 str = (xmlChar *) xmlMallocAtomic(lenn + lenp + 2);
5796 if (str == NULL) {
5797 xmlRegFreeAtom(atom);
5798 return(NULL);
5799 }
5800 memcpy(&str[0], token, lenp);
5801 str[lenp] = '|';
5802 memcpy(&str[lenp + 1], token2, lenn);
5803 str[lenn + lenp + 1] = 0;
5804
5805 atom->valuep = str;
5806 }
Daniel Veillarddb68b742005-07-30 13:18:24 +00005807 snprintf((char *) err_msg, 199, "not %s", (const char *) atom->valuep);
Daniel Veillard77005e62005-07-19 16:26:18 +00005808 err_msg[199] = 0;
5809 atom->valuep2 = xmlStrdup(err_msg);
Daniel Veillard9efc4762005-07-19 14:33:55 +00005810
5811 if (xmlFAGenerateTransitions(am, from, to, atom) < 0) {
5812 xmlRegFreeAtom(atom);
5813 return(NULL);
5814 }
Daniel Veillard6e65e152005-08-09 11:09:52 +00005815 am->negs++;
Daniel Veillard9efc4762005-07-19 14:33:55 +00005816 if (to == NULL)
5817 return(am->state);
5818 return(to);
5819}
5820
5821/**
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00005822 * xmlAutomataNewCountTrans2:
5823 * @am: an automata
5824 * @from: the starting point of the transition
5825 * @to: the target point of the transition or NULL
5826 * @token: the input string associated to that transition
5827 * @token2: the second input string associated to that transition
5828 * @min: the minimum successive occurences of token
5829 * @max: the maximum successive occurences of token
5830 * @data: data associated to the transition
5831 *
5832 * If @to is NULL, this creates first a new target state in the automata
5833 * and then adds a transition from the @from state to the target state
5834 * activated by a succession of input of value @token and @token2 and
5835 * whose number is between @min and @max
5836 *
5837 * Returns the target state or NULL in case of error
5838 */
5839xmlAutomataStatePtr
5840xmlAutomataNewCountTrans2(xmlAutomataPtr am, xmlAutomataStatePtr from,
5841 xmlAutomataStatePtr to, const xmlChar *token,
5842 const xmlChar *token2,
5843 int min, int max, void *data) {
5844 xmlRegAtomPtr atom;
5845 int counter;
5846
5847 if ((am == NULL) || (from == NULL) || (token == NULL))
5848 return(NULL);
5849 if (min < 0)
5850 return(NULL);
5851 if ((max < min) || (max < 1))
5852 return(NULL);
5853 atom = xmlRegNewAtom(am, XML_REGEXP_STRING);
5854 if (atom == NULL)
5855 return(NULL);
5856 if ((token2 == NULL) || (*token2 == 0)) {
5857 atom->valuep = xmlStrdup(token);
5858 } else {
5859 int lenn, lenp;
5860 xmlChar *str;
5861
5862 lenn = strlen((char *) token2);
5863 lenp = strlen((char *) token);
5864
5865 str = (xmlChar *) xmlMallocAtomic(lenn + lenp + 2);
5866 if (str == NULL) {
5867 xmlRegFreeAtom(atom);
5868 return(NULL);
5869 }
5870 memcpy(&str[0], token, lenp);
5871 str[lenp] = '|';
5872 memcpy(&str[lenp + 1], token2, lenn);
5873 str[lenn + lenp + 1] = 0;
5874
5875 atom->valuep = str;
5876 }
5877 atom->data = data;
5878 if (min == 0)
5879 atom->min = 1;
5880 else
5881 atom->min = min;
5882 atom->max = max;
5883
5884 /*
5885 * associate a counter to the transition.
5886 */
5887 counter = xmlRegGetCounter(am);
5888 am->counters[counter].min = min;
5889 am->counters[counter].max = max;
5890
5891 /* xmlFAGenerateTransitions(am, from, to, atom); */
5892 if (to == NULL) {
5893 to = xmlRegNewState(am);
5894 xmlRegStatePush(am, to);
5895 }
Daniel Veillard5de09382005-09-26 17:18:17 +00005896 xmlRegStateAddTrans(am, from, atom, to, counter, -1);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00005897 xmlRegAtomPush(am, atom);
5898 am->state = to;
5899
5900 if (to == NULL)
5901 to = am->state;
5902 if (to == NULL)
5903 return(NULL);
5904 if (min == 0)
5905 xmlFAGenerateEpsilonTransition(am, from, to);
5906 return(to);
5907}
5908
5909/**
Daniel Veillard4255d502002-04-16 15:50:10 +00005910 * xmlAutomataNewCountTrans:
5911 * @am: an automata
5912 * @from: the starting point of the transition
5913 * @to: the target point of the transition or NULL
5914 * @token: the input string associated to that transition
5915 * @min: the minimum successive occurences of token
Daniel Veillarda9b66d02002-12-11 14:23:49 +00005916 * @max: the maximum successive occurences of token
5917 * @data: data associated to the transition
Daniel Veillard4255d502002-04-16 15:50:10 +00005918 *
William M. Brackddf71d62004-05-06 04:17:26 +00005919 * If @to is NULL, this creates first a new target state in the automata
Daniel Veillard4255d502002-04-16 15:50:10 +00005920 * and then adds a transition from the @from state to the target state
5921 * activated by a succession of input of value @token and whose number
5922 * is between @min and @max
5923 *
5924 * Returns the target state or NULL in case of error
5925 */
5926xmlAutomataStatePtr
5927xmlAutomataNewCountTrans(xmlAutomataPtr am, xmlAutomataStatePtr from,
5928 xmlAutomataStatePtr to, const xmlChar *token,
5929 int min, int max, void *data) {
5930 xmlRegAtomPtr atom;
Daniel Veillard0ddb21c2004-02-12 12:43:49 +00005931 int counter;
Daniel Veillard4255d502002-04-16 15:50:10 +00005932
5933 if ((am == NULL) || (from == NULL) || (token == NULL))
5934 return(NULL);
5935 if (min < 0)
5936 return(NULL);
5937 if ((max < min) || (max < 1))
5938 return(NULL);
5939 atom = xmlRegNewAtom(am, XML_REGEXP_STRING);
5940 if (atom == NULL)
5941 return(NULL);
5942 atom->valuep = xmlStrdup(token);
5943 atom->data = data;
5944 if (min == 0)
5945 atom->min = 1;
5946 else
5947 atom->min = min;
5948 atom->max = max;
5949
Daniel Veillard0ddb21c2004-02-12 12:43:49 +00005950 /*
5951 * associate a counter to the transition.
5952 */
5953 counter = xmlRegGetCounter(am);
5954 am->counters[counter].min = min;
5955 am->counters[counter].max = max;
5956
5957 /* xmlFAGenerateTransitions(am, from, to, atom); */
5958 if (to == NULL) {
5959 to = xmlRegNewState(am);
5960 xmlRegStatePush(am, to);
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00005961 }
Daniel Veillard5de09382005-09-26 17:18:17 +00005962 xmlRegStateAddTrans(am, from, atom, to, counter, -1);
Daniel Veillard0ddb21c2004-02-12 12:43:49 +00005963 xmlRegAtomPush(am, atom);
5964 am->state = to;
5965
Daniel Veillard4255d502002-04-16 15:50:10 +00005966 if (to == NULL)
5967 to = am->state;
5968 if (to == NULL)
5969 return(NULL);
5970 if (min == 0)
5971 xmlFAGenerateEpsilonTransition(am, from, to);
5972 return(to);
5973}
5974
5975/**
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00005976 * xmlAutomataNewOnceTrans2:
5977 * @am: an automata
5978 * @from: the starting point of the transition
5979 * @to: the target point of the transition or NULL
5980 * @token: the input string associated to that transition
5981 * @token2: the second input string associated to that transition
5982 * @min: the minimum successive occurences of token
5983 * @max: the maximum successive occurences of token
5984 * @data: data associated to the transition
5985 *
5986 * If @to is NULL, this creates first a new target state in the automata
5987 * and then adds a transition from the @from state to the target state
5988 * activated by a succession of input of value @token and @token2 and whose
5989 * number is between @min and @max, moreover that transition can only be
5990 * crossed once.
5991 *
5992 * Returns the target state or NULL in case of error
5993 */
5994xmlAutomataStatePtr
5995xmlAutomataNewOnceTrans2(xmlAutomataPtr am, xmlAutomataStatePtr from,
5996 xmlAutomataStatePtr to, const xmlChar *token,
5997 const xmlChar *token2,
5998 int min, int max, void *data) {
5999 xmlRegAtomPtr atom;
6000 int counter;
6001
6002 if ((am == NULL) || (from == NULL) || (token == NULL))
6003 return(NULL);
6004 if (min < 1)
6005 return(NULL);
6006 if ((max < min) || (max < 1))
6007 return(NULL);
6008 atom = xmlRegNewAtom(am, XML_REGEXP_STRING);
6009 if (atom == NULL)
6010 return(NULL);
6011 if ((token2 == NULL) || (*token2 == 0)) {
6012 atom->valuep = xmlStrdup(token);
6013 } else {
6014 int lenn, lenp;
6015 xmlChar *str;
6016
6017 lenn = strlen((char *) token2);
6018 lenp = strlen((char *) token);
6019
6020 str = (xmlChar *) xmlMallocAtomic(lenn + lenp + 2);
6021 if (str == NULL) {
6022 xmlRegFreeAtom(atom);
6023 return(NULL);
6024 }
6025 memcpy(&str[0], token, lenp);
6026 str[lenp] = '|';
6027 memcpy(&str[lenp + 1], token2, lenn);
6028 str[lenn + lenp + 1] = 0;
6029
6030 atom->valuep = str;
6031 }
6032 atom->data = data;
6033 atom->quant = XML_REGEXP_QUANT_ONCEONLY;
Daniel Veillard11ce4002006-03-10 00:36:23 +00006034 atom->min = min;
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00006035 atom->max = max;
6036 /*
6037 * associate a counter to the transition.
6038 */
6039 counter = xmlRegGetCounter(am);
6040 am->counters[counter].min = 1;
6041 am->counters[counter].max = 1;
6042
6043 /* xmlFAGenerateTransitions(am, from, to, atom); */
6044 if (to == NULL) {
6045 to = xmlRegNewState(am);
6046 xmlRegStatePush(am, to);
6047 }
Daniel Veillard5de09382005-09-26 17:18:17 +00006048 xmlRegStateAddTrans(am, from, atom, to, counter, -1);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00006049 xmlRegAtomPush(am, atom);
6050 am->state = to;
6051 return(to);
6052}
6053
6054
6055
6056/**
Daniel Veillard7646b182002-04-20 06:41:40 +00006057 * xmlAutomataNewOnceTrans:
6058 * @am: an automata
6059 * @from: the starting point of the transition
6060 * @to: the target point of the transition or NULL
6061 * @token: the input string associated to that transition
6062 * @min: the minimum successive occurences of token
Daniel Veillarda9b66d02002-12-11 14:23:49 +00006063 * @max: the maximum successive occurences of token
6064 * @data: data associated to the transition
Daniel Veillard7646b182002-04-20 06:41:40 +00006065 *
William M. Brackddf71d62004-05-06 04:17:26 +00006066 * If @to is NULL, this creates first a new target state in the automata
Daniel Veillard7646b182002-04-20 06:41:40 +00006067 * and then adds a transition from the @from state to the target state
6068 * activated by a succession of input of value @token and whose number
William M. Brackddf71d62004-05-06 04:17:26 +00006069 * is between @min and @max, moreover that transition can only be crossed
Daniel Veillard7646b182002-04-20 06:41:40 +00006070 * once.
6071 *
6072 * Returns the target state or NULL in case of error
6073 */
6074xmlAutomataStatePtr
6075xmlAutomataNewOnceTrans(xmlAutomataPtr am, xmlAutomataStatePtr from,
6076 xmlAutomataStatePtr to, const xmlChar *token,
6077 int min, int max, void *data) {
6078 xmlRegAtomPtr atom;
6079 int counter;
6080
6081 if ((am == NULL) || (from == NULL) || (token == NULL))
6082 return(NULL);
6083 if (min < 1)
6084 return(NULL);
6085 if ((max < min) || (max < 1))
6086 return(NULL);
6087 atom = xmlRegNewAtom(am, XML_REGEXP_STRING);
6088 if (atom == NULL)
6089 return(NULL);
6090 atom->valuep = xmlStrdup(token);
6091 atom->data = data;
6092 atom->quant = XML_REGEXP_QUANT_ONCEONLY;
Daniel Veillard11ce4002006-03-10 00:36:23 +00006093 atom->min = min;
Daniel Veillard7646b182002-04-20 06:41:40 +00006094 atom->max = max;
6095 /*
6096 * associate a counter to the transition.
6097 */
6098 counter = xmlRegGetCounter(am);
6099 am->counters[counter].min = 1;
6100 am->counters[counter].max = 1;
6101
6102 /* xmlFAGenerateTransitions(am, from, to, atom); */
6103 if (to == NULL) {
6104 to = xmlRegNewState(am);
6105 xmlRegStatePush(am, to);
6106 }
Daniel Veillard5de09382005-09-26 17:18:17 +00006107 xmlRegStateAddTrans(am, from, atom, to, counter, -1);
Daniel Veillard7646b182002-04-20 06:41:40 +00006108 xmlRegAtomPush(am, atom);
6109 am->state = to;
Daniel Veillard7646b182002-04-20 06:41:40 +00006110 return(to);
6111}
6112
6113/**
Daniel Veillard4255d502002-04-16 15:50:10 +00006114 * xmlAutomataNewState:
6115 * @am: an automata
6116 *
6117 * Create a new disconnected state in the automata
6118 *
6119 * Returns the new state or NULL in case of error
6120 */
6121xmlAutomataStatePtr
6122xmlAutomataNewState(xmlAutomataPtr am) {
6123 xmlAutomataStatePtr to;
6124
6125 if (am == NULL)
6126 return(NULL);
6127 to = xmlRegNewState(am);
6128 xmlRegStatePush(am, to);
6129 return(to);
6130}
6131
6132/**
Daniel Veillarda9b66d02002-12-11 14:23:49 +00006133 * xmlAutomataNewEpsilon:
Daniel Veillard4255d502002-04-16 15:50:10 +00006134 * @am: an automata
6135 * @from: the starting point of the transition
6136 * @to: the target point of the transition or NULL
6137 *
William M. Brackddf71d62004-05-06 04:17:26 +00006138 * If @to is NULL, this creates first a new target state in the automata
6139 * and then adds an epsilon transition from the @from state to the
Daniel Veillard4255d502002-04-16 15:50:10 +00006140 * target state
6141 *
6142 * Returns the target state or NULL in case of error
6143 */
6144xmlAutomataStatePtr
6145xmlAutomataNewEpsilon(xmlAutomataPtr am, xmlAutomataStatePtr from,
6146 xmlAutomataStatePtr to) {
6147 if ((am == NULL) || (from == NULL))
6148 return(NULL);
6149 xmlFAGenerateEpsilonTransition(am, from, to);
6150 if (to == NULL)
6151 return(am->state);
6152 return(to);
6153}
6154
Daniel Veillardb509f152002-04-17 16:28:10 +00006155/**
Daniel Veillard7646b182002-04-20 06:41:40 +00006156 * xmlAutomataNewAllTrans:
6157 * @am: an automata
6158 * @from: the starting point of the transition
6159 * @to: the target point of the transition or NULL
Daniel Veillarda9b66d02002-12-11 14:23:49 +00006160 * @lax: allow to transition if not all all transitions have been activated
Daniel Veillard7646b182002-04-20 06:41:40 +00006161 *
William M. Brackddf71d62004-05-06 04:17:26 +00006162 * If @to is NULL, this creates first a new target state in the automata
Daniel Veillard7646b182002-04-20 06:41:40 +00006163 * and then adds a an ALL transition from the @from state to the
6164 * target state. That transition is an epsilon transition allowed only when
6165 * all transitions from the @from node have been activated.
6166 *
6167 * Returns the target state or NULL in case of error
6168 */
6169xmlAutomataStatePtr
6170xmlAutomataNewAllTrans(xmlAutomataPtr am, xmlAutomataStatePtr from,
Daniel Veillard441bc322002-04-20 17:38:48 +00006171 xmlAutomataStatePtr to, int lax) {
Daniel Veillard7646b182002-04-20 06:41:40 +00006172 if ((am == NULL) || (from == NULL))
6173 return(NULL);
Daniel Veillard441bc322002-04-20 17:38:48 +00006174 xmlFAGenerateAllTransition(am, from, to, lax);
Daniel Veillard7646b182002-04-20 06:41:40 +00006175 if (to == NULL)
6176 return(am->state);
6177 return(to);
6178}
6179
6180/**
Daniel Veillardb509f152002-04-17 16:28:10 +00006181 * xmlAutomataNewCounter:
6182 * @am: an automata
6183 * @min: the minimal value on the counter
6184 * @max: the maximal value on the counter
6185 *
6186 * Create a new counter
6187 *
6188 * Returns the counter number or -1 in case of error
6189 */
6190int
6191xmlAutomataNewCounter(xmlAutomataPtr am, int min, int max) {
6192 int ret;
6193
6194 if (am == NULL)
6195 return(-1);
6196
6197 ret = xmlRegGetCounter(am);
6198 if (ret < 0)
6199 return(-1);
6200 am->counters[ret].min = min;
6201 am->counters[ret].max = max;
6202 return(ret);
6203}
6204
6205/**
6206 * xmlAutomataNewCountedTrans:
6207 * @am: an automata
6208 * @from: the starting point of the transition
6209 * @to: the target point of the transition or NULL
6210 * @counter: the counter associated to that transition
6211 *
William M. Brackddf71d62004-05-06 04:17:26 +00006212 * If @to is NULL, this creates first a new target state in the automata
Daniel Veillardb509f152002-04-17 16:28:10 +00006213 * and then adds an epsilon transition from the @from state to the target state
6214 * which will increment the counter provided
6215 *
6216 * Returns the target state or NULL in case of error
6217 */
6218xmlAutomataStatePtr
6219xmlAutomataNewCountedTrans(xmlAutomataPtr am, xmlAutomataStatePtr from,
6220 xmlAutomataStatePtr to, int counter) {
6221 if ((am == NULL) || (from == NULL) || (counter < 0))
6222 return(NULL);
6223 xmlFAGenerateCountedEpsilonTransition(am, from, to, counter);
6224 if (to == NULL)
6225 return(am->state);
6226 return(to);
6227}
6228
6229/**
6230 * xmlAutomataNewCounterTrans:
6231 * @am: an automata
6232 * @from: the starting point of the transition
6233 * @to: the target point of the transition or NULL
6234 * @counter: the counter associated to that transition
6235 *
William M. Brackddf71d62004-05-06 04:17:26 +00006236 * If @to is NULL, this creates first a new target state in the automata
Daniel Veillardb509f152002-04-17 16:28:10 +00006237 * and then adds an epsilon transition from the @from state to the target state
6238 * which will be allowed only if the counter is within the right range.
6239 *
6240 * Returns the target state or NULL in case of error
6241 */
6242xmlAutomataStatePtr
6243xmlAutomataNewCounterTrans(xmlAutomataPtr am, xmlAutomataStatePtr from,
6244 xmlAutomataStatePtr to, int counter) {
6245 if ((am == NULL) || (from == NULL) || (counter < 0))
6246 return(NULL);
6247 xmlFAGenerateCountedTransition(am, from, to, counter);
6248 if (to == NULL)
6249 return(am->state);
6250 return(to);
6251}
Daniel Veillard4255d502002-04-16 15:50:10 +00006252
6253/**
6254 * xmlAutomataCompile:
6255 * @am: an automata
6256 *
6257 * Compile the automata into a Reg Exp ready for being executed.
6258 * The automata should be free after this point.
6259 *
6260 * Returns the compiled regexp or NULL in case of error
6261 */
6262xmlRegexpPtr
6263xmlAutomataCompile(xmlAutomataPtr am) {
6264 xmlRegexpPtr ret;
6265
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00006266 if ((am == NULL) || (am->error != 0)) return(NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +00006267 xmlFAEliminateEpsilonTransitions(am);
Daniel Veillard23e73572002-09-19 19:56:43 +00006268 /* xmlFAComputesDeterminism(am); */
Daniel Veillard4255d502002-04-16 15:50:10 +00006269 ret = xmlRegEpxFromParse(am);
6270
6271 return(ret);
6272}
Daniel Veillarde19fc232002-04-22 16:01:24 +00006273
6274/**
6275 * xmlAutomataIsDeterminist:
6276 * @am: an automata
6277 *
6278 * Checks if an automata is determinist.
6279 *
6280 * Returns 1 if true, 0 if not, and -1 in case of error
6281 */
6282int
6283xmlAutomataIsDeterminist(xmlAutomataPtr am) {
6284 int ret;
6285
6286 if (am == NULL)
6287 return(-1);
6288
6289 ret = xmlFAComputesDeterminism(am);
6290 return(ret);
6291}
Daniel Veillard4255d502002-04-16 15:50:10 +00006292#endif /* LIBXML_AUTOMATA_ENABLED */
Daniel Veillard81a8ec62005-08-22 00:20:58 +00006293
6294#ifdef LIBXML_EXPR_ENABLED
6295/************************************************************************
6296 * *
6297 * Formal Expression handling code *
6298 * *
6299 ************************************************************************/
Daniel Veillard81a8ec62005-08-22 00:20:58 +00006300/************************************************************************
6301 * *
6302 * Expression handling context *
6303 * *
6304 ************************************************************************/
6305
6306struct _xmlExpCtxt {
6307 xmlDictPtr dict;
6308 xmlExpNodePtr *table;
6309 int size;
6310 int nbElems;
6311 int nb_nodes;
Daniel Veillard594e5df2009-09-07 14:58:47 +02006312 int maxNodes;
Daniel Veillard81a8ec62005-08-22 00:20:58 +00006313 const char *expr;
6314 const char *cur;
6315 int nb_cons;
Daniel Veillard81a8ec62005-08-22 00:20:58 +00006316 int tabSize;
6317};
6318
6319/**
6320 * xmlExpNewCtxt:
6321 * @maxNodes: the maximum number of nodes
6322 * @dict: optional dictionnary to use internally
6323 *
6324 * Creates a new context for manipulating expressions
6325 *
6326 * Returns the context or NULL in case of error
6327 */
6328xmlExpCtxtPtr
6329xmlExpNewCtxt(int maxNodes, xmlDictPtr dict) {
6330 xmlExpCtxtPtr ret;
6331 int size = 256;
6332
6333 if (maxNodes <= 4096)
6334 maxNodes = 4096;
6335
6336 ret = (xmlExpCtxtPtr) xmlMalloc(sizeof(xmlExpCtxt));
6337 if (ret == NULL)
6338 return(NULL);
6339 memset(ret, 0, sizeof(xmlExpCtxt));
6340 ret->size = size;
6341 ret->nbElems = 0;
Daniel Veillard594e5df2009-09-07 14:58:47 +02006342 ret->maxNodes = maxNodes;
Daniel Veillard81a8ec62005-08-22 00:20:58 +00006343 ret->table = xmlMalloc(size * sizeof(xmlExpNodePtr));
6344 if (ret->table == NULL) {
6345 xmlFree(ret);
6346 return(NULL);
6347 }
6348 memset(ret->table, 0, size * sizeof(xmlExpNodePtr));
6349 if (dict == NULL) {
6350 ret->dict = xmlDictCreate();
6351 if (ret->dict == NULL) {
6352 xmlFree(ret->table);
6353 xmlFree(ret);
6354 return(NULL);
6355 }
6356 } else {
6357 ret->dict = dict;
6358 xmlDictReference(ret->dict);
6359 }
6360 return(ret);
6361}
6362
6363/**
6364 * xmlExpFreeCtxt:
6365 * @ctxt: an expression context
6366 *
6367 * Free an expression context
6368 */
6369void
6370xmlExpFreeCtxt(xmlExpCtxtPtr ctxt) {
6371 if (ctxt == NULL)
6372 return;
6373 xmlDictFree(ctxt->dict);
6374 if (ctxt->table != NULL)
6375 xmlFree(ctxt->table);
6376 xmlFree(ctxt);
6377}
6378
6379/************************************************************************
6380 * *
6381 * Structure associated to an expression node *
6382 * *
6383 ************************************************************************/
Daniel Veillard465a0002005-08-22 12:07:04 +00006384#define MAX_NODES 10000
6385
6386/* #define DEBUG_DERIV */
6387
6388/*
6389 * TODO:
6390 * - Wildcards
6391 * - public API for creation
6392 *
6393 * Started
6394 * - regression testing
6395 *
6396 * Done
6397 * - split into module and test tool
6398 * - memleaks
6399 */
Daniel Veillard81a8ec62005-08-22 00:20:58 +00006400
6401typedef enum {
6402 XML_EXP_NILABLE = (1 << 0)
6403} xmlExpNodeInfo;
6404
6405#define IS_NILLABLE(node) ((node)->info & XML_EXP_NILABLE)
6406
6407struct _xmlExpNode {
6408 unsigned char type;/* xmlExpNodeType */
6409 unsigned char info;/* OR of xmlExpNodeInfo */
6410 unsigned short key; /* the hash key */
6411 unsigned int ref; /* The number of references */
6412 int c_max; /* the maximum length it can consume */
6413 xmlExpNodePtr exp_left;
6414 xmlExpNodePtr next;/* the next node in the hash table or free list */
6415 union {
6416 struct {
6417 int f_min;
6418 int f_max;
6419 } count;
6420 struct {
6421 xmlExpNodePtr f_right;
6422 } children;
6423 const xmlChar *f_str;
6424 } field;
6425};
6426
6427#define exp_min field.count.f_min
6428#define exp_max field.count.f_max
6429/* #define exp_left field.children.f_left */
6430#define exp_right field.children.f_right
6431#define exp_str field.f_str
6432
6433static xmlExpNodePtr xmlExpNewNode(xmlExpCtxtPtr ctxt, xmlExpNodeType type);
6434static xmlExpNode forbiddenExpNode = {
6435 XML_EXP_FORBID, 0, 0, 0, 0, NULL, NULL, {{ 0, 0}}
6436};
6437xmlExpNodePtr forbiddenExp = &forbiddenExpNode;
6438static xmlExpNode emptyExpNode = {
6439 XML_EXP_EMPTY, 1, 0, 0, 0, NULL, NULL, {{ 0, 0}}
6440};
6441xmlExpNodePtr emptyExp = &emptyExpNode;
6442
6443/************************************************************************
6444 * *
6445 * The custom hash table for unicity and canonicalization *
6446 * of sub-expressions pointers *
6447 * *
6448 ************************************************************************/
6449/*
6450 * xmlExpHashNameComputeKey:
6451 * Calculate the hash key for a token
6452 */
6453static unsigned short
6454xmlExpHashNameComputeKey(const xmlChar *name) {
6455 unsigned short value = 0L;
6456 char ch;
6457
6458 if (name != NULL) {
6459 value += 30 * (*name);
6460 while ((ch = *name++) != 0) {
6461 value = value ^ ((value << 5) + (value >> 3) + (unsigned long)ch);
6462 }
6463 }
6464 return (value);
6465}
6466
6467/*
6468 * xmlExpHashComputeKey:
6469 * Calculate the hash key for a compound expression
6470 */
6471static unsigned short
6472xmlExpHashComputeKey(xmlExpNodeType type, xmlExpNodePtr left,
6473 xmlExpNodePtr right) {
6474 unsigned long value;
6475 unsigned short ret;
6476
6477 switch (type) {
6478 case XML_EXP_SEQ:
6479 value = left->key;
6480 value += right->key;
6481 value *= 3;
6482 ret = (unsigned short) value;
6483 break;
6484 case XML_EXP_OR:
6485 value = left->key;
6486 value += right->key;
6487 value *= 7;
6488 ret = (unsigned short) value;
6489 break;
6490 case XML_EXP_COUNT:
6491 value = left->key;
6492 value += right->key;
6493 ret = (unsigned short) value;
6494 break;
6495 default:
6496 ret = 0;
6497 }
6498 return(ret);
6499}
6500
6501
6502static xmlExpNodePtr
6503xmlExpNewNode(xmlExpCtxtPtr ctxt, xmlExpNodeType type) {
6504 xmlExpNodePtr ret;
6505
6506 if (ctxt->nb_nodes >= MAX_NODES)
6507 return(NULL);
6508 ret = (xmlExpNodePtr) xmlMalloc(sizeof(xmlExpNode));
6509 if (ret == NULL)
6510 return(NULL);
6511 memset(ret, 0, sizeof(xmlExpNode));
6512 ret->type = type;
6513 ret->next = NULL;
6514 ctxt->nb_nodes++;
6515 ctxt->nb_cons++;
6516 return(ret);
6517}
6518
6519/**
6520 * xmlExpHashGetEntry:
6521 * @table: the hash table
6522 *
6523 * Get the unique entry from the hash table. The entry is created if
6524 * needed. @left and @right are consumed, i.e. their ref count will
6525 * be decremented by the operation.
6526 *
6527 * Returns the pointer or NULL in case of error
6528 */
6529static xmlExpNodePtr
6530xmlExpHashGetEntry(xmlExpCtxtPtr ctxt, xmlExpNodeType type,
6531 xmlExpNodePtr left, xmlExpNodePtr right,
6532 const xmlChar *name, int min, int max) {
6533 unsigned short kbase, key;
6534 xmlExpNodePtr entry;
6535 xmlExpNodePtr insert;
6536
6537 if (ctxt == NULL)
6538 return(NULL);
6539
6540 /*
6541 * Check for duplicate and insertion location.
6542 */
6543 if (type == XML_EXP_ATOM) {
6544 kbase = xmlExpHashNameComputeKey(name);
6545 } else if (type == XML_EXP_COUNT) {
6546 /* COUNT reduction rule 1 */
6547 /* a{1} -> a */
6548 if (min == max) {
6549 if (min == 1) {
6550 return(left);
6551 }
6552 if (min == 0) {
6553 xmlExpFree(ctxt, left);
6554 return(emptyExp);
6555 }
6556 }
6557 if (min < 0) {
6558 xmlExpFree(ctxt, left);
6559 return(forbiddenExp);
6560 }
6561 if (max == -1)
6562 kbase = min + 79;
6563 else
6564 kbase = max - min;
6565 kbase += left->key;
6566 } else if (type == XML_EXP_OR) {
6567 /* Forbid reduction rules */
6568 if (left->type == XML_EXP_FORBID) {
6569 xmlExpFree(ctxt, left);
6570 return(right);
6571 }
6572 if (right->type == XML_EXP_FORBID) {
6573 xmlExpFree(ctxt, right);
6574 return(left);
6575 }
6576
6577 /* OR reduction rule 1 */
6578 /* a | a reduced to a */
6579 if (left == right) {
6580 left->ref--;
6581 return(left);
6582 }
6583 /* OR canonicalization rule 1 */
6584 /* linearize (a | b) | c into a | (b | c) */
6585 if ((left->type == XML_EXP_OR) && (right->type != XML_EXP_OR)) {
6586 xmlExpNodePtr tmp = left;
6587 left = right;
6588 right = tmp;
6589 }
6590 /* OR reduction rule 2 */
6591 /* a | (a | b) and b | (a | b) are reduced to a | b */
6592 if (right->type == XML_EXP_OR) {
6593 if ((left == right->exp_left) ||
6594 (left == right->exp_right)) {
6595 xmlExpFree(ctxt, left);
6596 return(right);
6597 }
6598 }
6599 /* OR canonicalization rule 2 */
6600 /* linearize (a | b) | c into a | (b | c) */
6601 if (left->type == XML_EXP_OR) {
6602 xmlExpNodePtr tmp;
6603
6604 /* OR canonicalization rule 2 */
6605 if ((left->exp_right->type != XML_EXP_OR) &&
6606 (left->exp_right->key < left->exp_left->key)) {
6607 tmp = left->exp_right;
6608 left->exp_right = left->exp_left;
6609 left->exp_left = tmp;
6610 }
6611 left->exp_right->ref++;
6612 tmp = xmlExpHashGetEntry(ctxt, XML_EXP_OR, left->exp_right, right,
6613 NULL, 0, 0);
6614 left->exp_left->ref++;
6615 tmp = xmlExpHashGetEntry(ctxt, XML_EXP_OR, left->exp_left, tmp,
6616 NULL, 0, 0);
6617
6618 xmlExpFree(ctxt, left);
6619 return(tmp);
6620 }
6621 if (right->type == XML_EXP_OR) {
6622 /* Ordering in the tree */
6623 /* C | (A | B) -> A | (B | C) */
6624 if (left->key > right->exp_right->key) {
6625 xmlExpNodePtr tmp;
6626 right->exp_right->ref++;
6627 tmp = xmlExpHashGetEntry(ctxt, XML_EXP_OR, right->exp_right,
6628 left, NULL, 0, 0);
6629 right->exp_left->ref++;
6630 tmp = xmlExpHashGetEntry(ctxt, XML_EXP_OR, right->exp_left,
6631 tmp, NULL, 0, 0);
6632 xmlExpFree(ctxt, right);
6633 return(tmp);
6634 }
6635 /* Ordering in the tree */
6636 /* B | (A | C) -> A | (B | C) */
6637 if (left->key > right->exp_left->key) {
6638 xmlExpNodePtr tmp;
6639 right->exp_right->ref++;
6640 tmp = xmlExpHashGetEntry(ctxt, XML_EXP_OR, left,
6641 right->exp_right, NULL, 0, 0);
6642 right->exp_left->ref++;
6643 tmp = xmlExpHashGetEntry(ctxt, XML_EXP_OR, right->exp_left,
6644 tmp, NULL, 0, 0);
6645 xmlExpFree(ctxt, right);
6646 return(tmp);
6647 }
6648 }
6649 /* we know both types are != XML_EXP_OR here */
6650 else if (left->key > right->key) {
6651 xmlExpNodePtr tmp = left;
6652 left = right;
6653 right = tmp;
6654 }
6655 kbase = xmlExpHashComputeKey(type, left, right);
6656 } else if (type == XML_EXP_SEQ) {
6657 /* Forbid reduction rules */
6658 if (left->type == XML_EXP_FORBID) {
6659 xmlExpFree(ctxt, right);
6660 return(left);
6661 }
6662 if (right->type == XML_EXP_FORBID) {
6663 xmlExpFree(ctxt, left);
6664 return(right);
6665 }
6666 /* Empty reduction rules */
6667 if (right->type == XML_EXP_EMPTY) {
6668 return(left);
6669 }
6670 if (left->type == XML_EXP_EMPTY) {
6671 return(right);
6672 }
6673 kbase = xmlExpHashComputeKey(type, left, right);
6674 } else
6675 return(NULL);
6676
6677 key = kbase % ctxt->size;
6678 if (ctxt->table[key] != NULL) {
6679 for (insert = ctxt->table[key]; insert != NULL;
6680 insert = insert->next) {
6681 if ((insert->key == kbase) &&
6682 (insert->type == type)) {
6683 if (type == XML_EXP_ATOM) {
6684 if (name == insert->exp_str) {
6685 insert->ref++;
6686 return(insert);
6687 }
6688 } else if (type == XML_EXP_COUNT) {
6689 if ((insert->exp_min == min) && (insert->exp_max == max) &&
6690 (insert->exp_left == left)) {
6691 insert->ref++;
6692 left->ref--;
6693 return(insert);
6694 }
6695 } else if ((insert->exp_left == left) &&
6696 (insert->exp_right == right)) {
6697 insert->ref++;
6698 left->ref--;
6699 right->ref--;
6700 return(insert);
6701 }
6702 }
6703 }
6704 }
6705
6706 entry = xmlExpNewNode(ctxt, type);
6707 if (entry == NULL)
6708 return(NULL);
6709 entry->key = kbase;
6710 if (type == XML_EXP_ATOM) {
6711 entry->exp_str = name;
6712 entry->c_max = 1;
6713 } else if (type == XML_EXP_COUNT) {
6714 entry->exp_min = min;
6715 entry->exp_max = max;
6716 entry->exp_left = left;
6717 if ((min == 0) || (IS_NILLABLE(left)))
6718 entry->info |= XML_EXP_NILABLE;
6719 if (max < 0)
6720 entry->c_max = -1;
6721 else
6722 entry->c_max = max * entry->exp_left->c_max;
6723 } else {
6724 entry->exp_left = left;
6725 entry->exp_right = right;
6726 if (type == XML_EXP_OR) {
6727 if ((IS_NILLABLE(left)) || (IS_NILLABLE(right)))
6728 entry->info |= XML_EXP_NILABLE;
6729 if ((entry->exp_left->c_max == -1) ||
6730 (entry->exp_right->c_max == -1))
6731 entry->c_max = -1;
6732 else if (entry->exp_left->c_max > entry->exp_right->c_max)
6733 entry->c_max = entry->exp_left->c_max;
6734 else
6735 entry->c_max = entry->exp_right->c_max;
6736 } else {
6737 if ((IS_NILLABLE(left)) && (IS_NILLABLE(right)))
6738 entry->info |= XML_EXP_NILABLE;
6739 if ((entry->exp_left->c_max == -1) ||
6740 (entry->exp_right->c_max == -1))
6741 entry->c_max = -1;
6742 else
6743 entry->c_max = entry->exp_left->c_max + entry->exp_right->c_max;
6744 }
6745 }
6746 entry->ref = 1;
6747 if (ctxt->table[key] != NULL)
6748 entry->next = ctxt->table[key];
6749
6750 ctxt->table[key] = entry;
6751 ctxt->nbElems++;
6752
6753 return(entry);
6754}
6755
6756/**
6757 * xmlExpFree:
6758 * @ctxt: the expression context
6759 * @exp: the expression
6760 *
6761 * Dereference the expression
6762 */
6763void
6764xmlExpFree(xmlExpCtxtPtr ctxt, xmlExpNodePtr exp) {
6765 if ((exp == NULL) || (exp == forbiddenExp) || (exp == emptyExp))
6766 return;
6767 exp->ref--;
Daniel Veillard81a8ec62005-08-22 00:20:58 +00006768 if (exp->ref == 0) {
6769 unsigned short key;
6770
6771 /* Unlink it first from the hash table */
6772 key = exp->key % ctxt->size;
6773 if (ctxt->table[key] == exp) {
6774 ctxt->table[key] = exp->next;
6775 } else {
6776 xmlExpNodePtr tmp;
6777
6778 tmp = ctxt->table[key];
6779 while (tmp != NULL) {
6780 if (tmp->next == exp) {
6781 tmp->next = exp->next;
6782 break;
6783 }
6784 tmp = tmp->next;
6785 }
6786 }
6787
6788 if ((exp->type == XML_EXP_SEQ) || (exp->type == XML_EXP_OR)) {
6789 xmlExpFree(ctxt, exp->exp_left);
6790 xmlExpFree(ctxt, exp->exp_right);
6791 } else if (exp->type == XML_EXP_COUNT) {
6792 xmlExpFree(ctxt, exp->exp_left);
6793 }
6794 xmlFree(exp);
Daniel Veillard81a8ec62005-08-22 00:20:58 +00006795 ctxt->nb_nodes--;
6796 }
6797}
6798
6799/**
6800 * xmlExpRef:
6801 * @exp: the expression
6802 *
6803 * Increase the reference count of the expression
6804 */
6805void
6806xmlExpRef(xmlExpNodePtr exp) {
6807 if (exp != NULL)
6808 exp->ref++;
6809}
6810
Daniel Veillardccb4d412005-08-23 13:41:17 +00006811/**
6812 * xmlExpNewAtom:
6813 * @ctxt: the expression context
6814 * @name: the atom name
6815 * @len: the atom name lenght in byte (or -1);
6816 *
6817 * Get the atom associated to this name from that context
6818 *
6819 * Returns the node or NULL in case of error
6820 */
6821xmlExpNodePtr
6822xmlExpNewAtom(xmlExpCtxtPtr ctxt, const xmlChar *name, int len) {
6823 if ((ctxt == NULL) || (name == NULL))
6824 return(NULL);
6825 name = xmlDictLookup(ctxt->dict, name, len);
6826 if (name == NULL)
6827 return(NULL);
6828 return(xmlExpHashGetEntry(ctxt, XML_EXP_ATOM, NULL, NULL, name, 0, 0));
6829}
6830
6831/**
6832 * xmlExpNewOr:
6833 * @ctxt: the expression context
6834 * @left: left expression
6835 * @right: right expression
6836 *
6837 * Get the atom associated to the choice @left | @right
6838 * Note that @left and @right are consumed in the operation, to keep
6839 * an handle on them use xmlExpRef() and use xmlExpFree() to release them,
6840 * this is true even in case of failure (unless ctxt == NULL).
6841 *
6842 * Returns the node or NULL in case of error
6843 */
6844xmlExpNodePtr
6845xmlExpNewOr(xmlExpCtxtPtr ctxt, xmlExpNodePtr left, xmlExpNodePtr right) {
Daniel Veillard11ce4002006-03-10 00:36:23 +00006846 if (ctxt == NULL)
6847 return(NULL);
6848 if ((left == NULL) || (right == NULL)) {
Daniel Veillardccb4d412005-08-23 13:41:17 +00006849 xmlExpFree(ctxt, left);
6850 xmlExpFree(ctxt, right);
6851 return(NULL);
6852 }
6853 return(xmlExpHashGetEntry(ctxt, XML_EXP_OR, left, right, NULL, 0, 0));
6854}
6855
6856/**
6857 * xmlExpNewSeq:
6858 * @ctxt: the expression context
6859 * @left: left expression
6860 * @right: right expression
6861 *
6862 * Get the atom associated to the sequence @left , @right
6863 * Note that @left and @right are consumed in the operation, to keep
6864 * an handle on them use xmlExpRef() and use xmlExpFree() to release them,
6865 * this is true even in case of failure (unless ctxt == NULL).
6866 *
6867 * Returns the node or NULL in case of error
6868 */
6869xmlExpNodePtr
6870xmlExpNewSeq(xmlExpCtxtPtr ctxt, xmlExpNodePtr left, xmlExpNodePtr right) {
Daniel Veillard11ce4002006-03-10 00:36:23 +00006871 if (ctxt == NULL)
6872 return(NULL);
6873 if ((left == NULL) || (right == NULL)) {
Daniel Veillardccb4d412005-08-23 13:41:17 +00006874 xmlExpFree(ctxt, left);
6875 xmlExpFree(ctxt, right);
6876 return(NULL);
6877 }
6878 return(xmlExpHashGetEntry(ctxt, XML_EXP_SEQ, left, right, NULL, 0, 0));
6879}
6880
6881/**
6882 * xmlExpNewRange:
6883 * @ctxt: the expression context
6884 * @subset: the expression to be repeated
6885 * @min: the lower bound for the repetition
6886 * @max: the upper bound for the repetition, -1 means infinite
6887 *
6888 * Get the atom associated to the range (@subset){@min, @max}
6889 * Note that @subset is consumed in the operation, to keep
6890 * an handle on it use xmlExpRef() and use xmlExpFree() to release it,
6891 * this is true even in case of failure (unless ctxt == NULL).
6892 *
6893 * Returns the node or NULL in case of error
6894 */
6895xmlExpNodePtr
6896xmlExpNewRange(xmlExpCtxtPtr ctxt, xmlExpNodePtr subset, int min, int max) {
Daniel Veillard11ce4002006-03-10 00:36:23 +00006897 if (ctxt == NULL)
6898 return(NULL);
6899 if ((subset == NULL) || (min < 0) || (max < -1) ||
Daniel Veillardccb4d412005-08-23 13:41:17 +00006900 ((max >= 0) && (min > max))) {
6901 xmlExpFree(ctxt, subset);
6902 return(NULL);
6903 }
6904 return(xmlExpHashGetEntry(ctxt, XML_EXP_COUNT, subset,
6905 NULL, NULL, min, max));
6906}
6907
Daniel Veillard81a8ec62005-08-22 00:20:58 +00006908/************************************************************************
6909 * *
6910 * Public API for operations on expressions *
6911 * *
6912 ************************************************************************/
6913
6914static int
6915xmlExpGetLanguageInt(xmlExpCtxtPtr ctxt, xmlExpNodePtr exp,
6916 const xmlChar**list, int len, int nb) {
6917 int tmp, tmp2;
6918tail:
6919 switch (exp->type) {
6920 case XML_EXP_EMPTY:
6921 return(0);
6922 case XML_EXP_ATOM:
6923 for (tmp = 0;tmp < nb;tmp++)
6924 if (list[tmp] == exp->exp_str)
6925 return(0);
6926 if (nb >= len)
6927 return(-2);
Daniel Veillard13cee4e2009-09-05 14:52:55 +02006928 list[nb] = exp->exp_str;
Daniel Veillard81a8ec62005-08-22 00:20:58 +00006929 return(1);
6930 case XML_EXP_COUNT:
6931 exp = exp->exp_left;
6932 goto tail;
6933 case XML_EXP_SEQ:
6934 case XML_EXP_OR:
6935 tmp = xmlExpGetLanguageInt(ctxt, exp->exp_left, list, len, nb);
6936 if (tmp < 0)
6937 return(tmp);
6938 tmp2 = xmlExpGetLanguageInt(ctxt, exp->exp_right, list, len,
6939 nb + tmp);
6940 if (tmp2 < 0)
6941 return(tmp2);
6942 return(tmp + tmp2);
6943 }
6944 return(-1);
6945}
6946
6947/**
6948 * xmlExpGetLanguage:
6949 * @ctxt: the expression context
6950 * @exp: the expression
Daniel Veillard7802ba52005-10-27 11:56:20 +00006951 * @langList: where to store the tokens
Daniel Veillard81a8ec62005-08-22 00:20:58 +00006952 * @len: the allocated lenght of @list
6953 *
6954 * Find all the strings used in @exp and store them in @list
6955 *
6956 * Returns the number of unique strings found, -1 in case of errors and
6957 * -2 if there is more than @len strings
6958 */
6959int
6960xmlExpGetLanguage(xmlExpCtxtPtr ctxt, xmlExpNodePtr exp,
Daniel Veillard7802ba52005-10-27 11:56:20 +00006961 const xmlChar**langList, int len) {
6962 if ((ctxt == NULL) || (exp == NULL) || (langList == NULL) || (len <= 0))
Daniel Veillard81a8ec62005-08-22 00:20:58 +00006963 return(-1);
Daniel Veillard7802ba52005-10-27 11:56:20 +00006964 return(xmlExpGetLanguageInt(ctxt, exp, langList, len, 0));
Daniel Veillard81a8ec62005-08-22 00:20:58 +00006965}
6966
6967static int
6968xmlExpGetStartInt(xmlExpCtxtPtr ctxt, xmlExpNodePtr exp,
6969 const xmlChar**list, int len, int nb) {
6970 int tmp, tmp2;
6971tail:
6972 switch (exp->type) {
6973 case XML_EXP_FORBID:
6974 return(0);
6975 case XML_EXP_EMPTY:
6976 return(0);
6977 case XML_EXP_ATOM:
6978 for (tmp = 0;tmp < nb;tmp++)
6979 if (list[tmp] == exp->exp_str)
6980 return(0);
6981 if (nb >= len)
6982 return(-2);
Daniel Veillard13cee4e2009-09-05 14:52:55 +02006983 list[nb] = exp->exp_str;
Daniel Veillard81a8ec62005-08-22 00:20:58 +00006984 return(1);
6985 case XML_EXP_COUNT:
6986 exp = exp->exp_left;
6987 goto tail;
6988 case XML_EXP_SEQ:
6989 tmp = xmlExpGetStartInt(ctxt, exp->exp_left, list, len, nb);
6990 if (tmp < 0)
6991 return(tmp);
6992 if (IS_NILLABLE(exp->exp_left)) {
6993 tmp2 = xmlExpGetStartInt(ctxt, exp->exp_right, list, len,
6994 nb + tmp);
6995 if (tmp2 < 0)
6996 return(tmp2);
6997 tmp += tmp2;
6998 }
6999 return(tmp);
7000 case XML_EXP_OR:
7001 tmp = xmlExpGetStartInt(ctxt, exp->exp_left, list, len, nb);
7002 if (tmp < 0)
7003 return(tmp);
7004 tmp2 = xmlExpGetStartInt(ctxt, exp->exp_right, list, len,
7005 nb + tmp);
7006 if (tmp2 < 0)
7007 return(tmp2);
7008 return(tmp + tmp2);
7009 }
7010 return(-1);
7011}
7012
7013/**
7014 * xmlExpGetStart:
7015 * @ctxt: the expression context
7016 * @exp: the expression
Daniel Veillard7802ba52005-10-27 11:56:20 +00007017 * @tokList: where to store the tokens
Daniel Veillard81a8ec62005-08-22 00:20:58 +00007018 * @len: the allocated lenght of @list
7019 *
7020 * Find all the strings that appears at the start of the languages
7021 * accepted by @exp and store them in @list. E.g. for (a, b) | c
7022 * it will return the list [a, c]
7023 *
7024 * Returns the number of unique strings found, -1 in case of errors and
7025 * -2 if there is more than @len strings
7026 */
7027int
7028xmlExpGetStart(xmlExpCtxtPtr ctxt, xmlExpNodePtr exp,
Daniel Veillard7802ba52005-10-27 11:56:20 +00007029 const xmlChar**tokList, int len) {
7030 if ((ctxt == NULL) || (exp == NULL) || (tokList == NULL) || (len <= 0))
Daniel Veillard81a8ec62005-08-22 00:20:58 +00007031 return(-1);
Daniel Veillard7802ba52005-10-27 11:56:20 +00007032 return(xmlExpGetStartInt(ctxt, exp, tokList, len, 0));
Daniel Veillard81a8ec62005-08-22 00:20:58 +00007033}
7034
7035/**
7036 * xmlExpIsNillable:
7037 * @exp: the expression
7038 *
7039 * Finds if the expression is nillable, i.e. if it accepts the empty sequqnce
7040 *
7041 * Returns 1 if nillable, 0 if not and -1 in case of error
7042 */
7043int
7044xmlExpIsNillable(xmlExpNodePtr exp) {
7045 if (exp == NULL)
7046 return(-1);
7047 return(IS_NILLABLE(exp) != 0);
7048}
7049
7050static xmlExpNodePtr
7051xmlExpStringDeriveInt(xmlExpCtxtPtr ctxt, xmlExpNodePtr exp, const xmlChar *str)
7052{
7053 xmlExpNodePtr ret;
7054
7055 switch (exp->type) {
7056 case XML_EXP_EMPTY:
7057 return(forbiddenExp);
7058 case XML_EXP_FORBID:
7059 return(forbiddenExp);
7060 case XML_EXP_ATOM:
7061 if (exp->exp_str == str) {
7062#ifdef DEBUG_DERIV
7063 printf("deriv atom: equal => Empty\n");
7064#endif
7065 ret = emptyExp;
7066 } else {
7067#ifdef DEBUG_DERIV
7068 printf("deriv atom: mismatch => forbid\n");
7069#endif
7070 /* TODO wildcards here */
7071 ret = forbiddenExp;
7072 }
7073 return(ret);
7074 case XML_EXP_OR: {
7075 xmlExpNodePtr tmp;
7076
7077#ifdef DEBUG_DERIV
7078 printf("deriv or: => or(derivs)\n");
7079#endif
7080 tmp = xmlExpStringDeriveInt(ctxt, exp->exp_left, str);
7081 if (tmp == NULL) {
7082 return(NULL);
7083 }
7084 ret = xmlExpStringDeriveInt(ctxt, exp->exp_right, str);
7085 if (ret == NULL) {
7086 xmlExpFree(ctxt, tmp);
7087 return(NULL);
7088 }
7089 ret = xmlExpHashGetEntry(ctxt, XML_EXP_OR, tmp, ret,
7090 NULL, 0, 0);
7091 return(ret);
7092 }
7093 case XML_EXP_SEQ:
7094#ifdef DEBUG_DERIV
7095 printf("deriv seq: starting with left\n");
7096#endif
7097 ret = xmlExpStringDeriveInt(ctxt, exp->exp_left, str);
7098 if (ret == NULL) {
7099 return(NULL);
7100 } else if (ret == forbiddenExp) {
7101 if (IS_NILLABLE(exp->exp_left)) {
7102#ifdef DEBUG_DERIV
7103 printf("deriv seq: left failed but nillable\n");
7104#endif
7105 ret = xmlExpStringDeriveInt(ctxt, exp->exp_right, str);
7106 }
7107 } else {
7108#ifdef DEBUG_DERIV
7109 printf("deriv seq: left match => sequence\n");
7110#endif
7111 exp->exp_right->ref++;
7112 ret = xmlExpHashGetEntry(ctxt, XML_EXP_SEQ, ret, exp->exp_right,
7113 NULL, 0, 0);
7114 }
7115 return(ret);
7116 case XML_EXP_COUNT: {
7117 int min, max;
7118 xmlExpNodePtr tmp;
7119
7120 if (exp->exp_max == 0)
7121 return(forbiddenExp);
7122 ret = xmlExpStringDeriveInt(ctxt, exp->exp_left, str);
7123 if (ret == NULL)
7124 return(NULL);
7125 if (ret == forbiddenExp) {
7126#ifdef DEBUG_DERIV
7127 printf("deriv count: pattern mismatch => forbid\n");
7128#endif
7129 return(ret);
7130 }
7131 if (exp->exp_max == 1)
7132 return(ret);
7133 if (exp->exp_max < 0) /* unbounded */
7134 max = -1;
7135 else
7136 max = exp->exp_max - 1;
7137 if (exp->exp_min > 0)
7138 min = exp->exp_min - 1;
7139 else
7140 min = 0;
7141 exp->exp_left->ref++;
7142 tmp = xmlExpHashGetEntry(ctxt, XML_EXP_COUNT, exp->exp_left, NULL,
7143 NULL, min, max);
7144 if (ret == emptyExp) {
7145#ifdef DEBUG_DERIV
7146 printf("deriv count: match to empty => new count\n");
7147#endif
7148 return(tmp);
7149 }
7150#ifdef DEBUG_DERIV
7151 printf("deriv count: match => sequence with new count\n");
7152#endif
7153 return(xmlExpHashGetEntry(ctxt, XML_EXP_SEQ, ret, tmp,
7154 NULL, 0, 0));
7155 }
7156 }
7157 return(NULL);
7158}
7159
7160/**
7161 * xmlExpStringDerive:
7162 * @ctxt: the expression context
7163 * @exp: the expression
7164 * @str: the string
7165 * @len: the string len in bytes if available
7166 *
7167 * Do one step of Brzozowski derivation of the expression @exp with
7168 * respect to the input string
7169 *
7170 * Returns the resulting expression or NULL in case of internal error
7171 */
7172xmlExpNodePtr
7173xmlExpStringDerive(xmlExpCtxtPtr ctxt, xmlExpNodePtr exp,
7174 const xmlChar *str, int len) {
7175 const xmlChar *input;
7176
7177 if ((exp == NULL) || (ctxt == NULL) || (str == NULL)) {
7178 return(NULL);
7179 }
7180 /*
7181 * check the string is in the dictionnary, if yes use an interned
7182 * copy, otherwise we know it's not an acceptable input
7183 */
7184 input = xmlDictExists(ctxt->dict, str, len);
7185 if (input == NULL) {
7186 return(forbiddenExp);
7187 }
7188 return(xmlExpStringDeriveInt(ctxt, exp, input));
7189}
7190
7191static int
7192xmlExpCheckCard(xmlExpNodePtr exp, xmlExpNodePtr sub) {
7193 int ret = 1;
7194
7195 if (sub->c_max == -1) {
7196 if (exp->c_max != -1)
7197 ret = 0;
7198 } else if ((exp->c_max >= 0) && (exp->c_max < sub->c_max)) {
7199 ret = 0;
7200 }
7201#if 0
7202 if ((IS_NILLABLE(sub)) && (!IS_NILLABLE(exp)))
7203 ret = 0;
7204#endif
7205 return(ret);
7206}
7207
7208static xmlExpNodePtr xmlExpExpDeriveInt(xmlExpCtxtPtr ctxt, xmlExpNodePtr exp,
7209 xmlExpNodePtr sub);
7210/**
7211 * xmlExpDivide:
7212 * @ctxt: the expressions context
7213 * @exp: the englobing expression
7214 * @sub: the subexpression
7215 * @mult: the multiple expression
7216 * @remain: the remain from the derivation of the multiple
7217 *
7218 * Check if exp is a multiple of sub, i.e. if there is a finite number n
7219 * so that sub{n} subsume exp
7220 *
7221 * Returns the multiple value if successful, 0 if it is not a multiple
7222 * and -1 in case of internel error.
7223 */
7224
7225static int
7226xmlExpDivide(xmlExpCtxtPtr ctxt, xmlExpNodePtr exp, xmlExpNodePtr sub,
7227 xmlExpNodePtr *mult, xmlExpNodePtr *remain) {
7228 int i;
7229 xmlExpNodePtr tmp, tmp2;
7230
7231 if (mult != NULL) *mult = NULL;
7232 if (remain != NULL) *remain = NULL;
7233 if (exp->c_max == -1) return(0);
7234 if (IS_NILLABLE(exp) && (!IS_NILLABLE(sub))) return(0);
7235
7236 for (i = 1;i <= exp->c_max;i++) {
7237 sub->ref++;
7238 tmp = xmlExpHashGetEntry(ctxt, XML_EXP_COUNT,
7239 sub, NULL, NULL, i, i);
7240 if (tmp == NULL) {
7241 return(-1);
7242 }
7243 if (!xmlExpCheckCard(tmp, exp)) {
7244 xmlExpFree(ctxt, tmp);
7245 continue;
7246 }
7247 tmp2 = xmlExpExpDeriveInt(ctxt, tmp, exp);
7248 if (tmp2 == NULL) {
7249 xmlExpFree(ctxt, tmp);
7250 return(-1);
7251 }
7252 if ((tmp2 != forbiddenExp) && (IS_NILLABLE(tmp2))) {
7253 if (remain != NULL)
7254 *remain = tmp2;
7255 else
7256 xmlExpFree(ctxt, tmp2);
7257 if (mult != NULL)
7258 *mult = tmp;
7259 else
7260 xmlExpFree(ctxt, tmp);
7261#ifdef DEBUG_DERIV
7262 printf("Divide succeeded %d\n", i);
7263#endif
7264 return(i);
7265 }
7266 xmlExpFree(ctxt, tmp);
7267 xmlExpFree(ctxt, tmp2);
7268 }
7269#ifdef DEBUG_DERIV
7270 printf("Divide failed\n");
7271#endif
7272 return(0);
7273}
7274
7275/**
7276 * xmlExpExpDeriveInt:
7277 * @ctxt: the expressions context
7278 * @exp: the englobing expression
7279 * @sub: the subexpression
7280 *
7281 * Try to do a step of Brzozowski derivation but at a higher level
7282 * the input being a subexpression.
7283 *
7284 * Returns the resulting expression or NULL in case of internal error
7285 */
7286static xmlExpNodePtr
7287xmlExpExpDeriveInt(xmlExpCtxtPtr ctxt, xmlExpNodePtr exp, xmlExpNodePtr sub) {
7288 xmlExpNodePtr ret, tmp, tmp2, tmp3;
7289 const xmlChar **tab;
7290 int len, i;
7291
7292 /*
7293 * In case of equality and if the expression can only consume a finite
7294 * amount, then the derivation is empty
7295 */
7296 if ((exp == sub) && (exp->c_max >= 0)) {
7297#ifdef DEBUG_DERIV
7298 printf("Equal(exp, sub) and finite -> Empty\n");
7299#endif
7300 return(emptyExp);
7301 }
7302 /*
7303 * decompose sub sequence first
7304 */
7305 if (sub->type == XML_EXP_EMPTY) {
7306#ifdef DEBUG_DERIV
7307 printf("Empty(sub) -> Empty\n");
7308#endif
7309 exp->ref++;
7310 return(exp);
7311 }
7312 if (sub->type == XML_EXP_SEQ) {
7313#ifdef DEBUG_DERIV
7314 printf("Seq(sub) -> decompose\n");
7315#endif
7316 tmp = xmlExpExpDeriveInt(ctxt, exp, sub->exp_left);
7317 if (tmp == NULL)
7318 return(NULL);
7319 if (tmp == forbiddenExp)
7320 return(tmp);
7321 ret = xmlExpExpDeriveInt(ctxt, tmp, sub->exp_right);
7322 xmlExpFree(ctxt, tmp);
7323 return(ret);
7324 }
7325 if (sub->type == XML_EXP_OR) {
7326#ifdef DEBUG_DERIV
7327 printf("Or(sub) -> decompose\n");
7328#endif
7329 tmp = xmlExpExpDeriveInt(ctxt, exp, sub->exp_left);
7330 if (tmp == forbiddenExp)
7331 return(tmp);
7332 if (tmp == NULL)
7333 return(NULL);
7334 ret = xmlExpExpDeriveInt(ctxt, exp, sub->exp_right);
7335 if ((ret == NULL) || (ret == forbiddenExp)) {
7336 xmlExpFree(ctxt, tmp);
7337 return(ret);
7338 }
7339 return(xmlExpHashGetEntry(ctxt, XML_EXP_OR, tmp, ret, NULL, 0, 0));
7340 }
7341 if (!xmlExpCheckCard(exp, sub)) {
7342#ifdef DEBUG_DERIV
7343 printf("CheckCard(exp, sub) failed -> Forbid\n");
7344#endif
7345 return(forbiddenExp);
7346 }
7347 switch (exp->type) {
7348 case XML_EXP_EMPTY:
7349 if (sub == emptyExp)
7350 return(emptyExp);
7351#ifdef DEBUG_DERIV
7352 printf("Empty(exp) -> Forbid\n");
7353#endif
7354 return(forbiddenExp);
7355 case XML_EXP_FORBID:
7356#ifdef DEBUG_DERIV
7357 printf("Forbid(exp) -> Forbid\n");
7358#endif
7359 return(forbiddenExp);
7360 case XML_EXP_ATOM:
7361 if (sub->type == XML_EXP_ATOM) {
7362 /* TODO: handle wildcards */
7363 if (exp->exp_str == sub->exp_str) {
7364#ifdef DEBUG_DERIV
7365 printf("Atom match -> Empty\n");
7366#endif
7367 return(emptyExp);
7368 }
7369#ifdef DEBUG_DERIV
7370 printf("Atom mismatch -> Forbid\n");
7371#endif
7372 return(forbiddenExp);
7373 }
7374 if ((sub->type == XML_EXP_COUNT) &&
7375 (sub->exp_max == 1) &&
7376 (sub->exp_left->type == XML_EXP_ATOM)) {
7377 /* TODO: handle wildcards */
7378 if (exp->exp_str == sub->exp_left->exp_str) {
7379#ifdef DEBUG_DERIV
7380 printf("Atom match -> Empty\n");
7381#endif
7382 return(emptyExp);
7383 }
7384#ifdef DEBUG_DERIV
7385 printf("Atom mismatch -> Forbid\n");
7386#endif
7387 return(forbiddenExp);
7388 }
7389#ifdef DEBUG_DERIV
7390 printf("Compex exp vs Atom -> Forbid\n");
7391#endif
7392 return(forbiddenExp);
7393 case XML_EXP_SEQ:
7394 /* try to get the sequence consumed only if possible */
7395 if (xmlExpCheckCard(exp->exp_left, sub)) {
7396 /* See if the sequence can be consumed directly */
7397#ifdef DEBUG_DERIV
7398 printf("Seq trying left only\n");
7399#endif
7400 ret = xmlExpExpDeriveInt(ctxt, exp->exp_left, sub);
7401 if ((ret != forbiddenExp) && (ret != NULL)) {
7402#ifdef DEBUG_DERIV
7403 printf("Seq trying left only worked\n");
7404#endif
7405 /*
7406 * TODO: assumption here that we are determinist
7407 * i.e. we won't get to a nillable exp left
7408 * subset which could be matched by the right
7409 * part too.
7410 * e.g.: (a | b)+,(a | c) and 'a+,a'
7411 */
7412 exp->exp_right->ref++;
7413 return(xmlExpHashGetEntry(ctxt, XML_EXP_SEQ, ret,
7414 exp->exp_right, NULL, 0, 0));
7415 }
7416#ifdef DEBUG_DERIV
7417 } else {
7418 printf("Seq: left too short\n");
7419#endif
7420 }
7421 /* Try instead to decompose */
7422 if (sub->type == XML_EXP_COUNT) {
7423 int min, max;
7424
7425#ifdef DEBUG_DERIV
7426 printf("Seq: sub is a count\n");
7427#endif
7428 ret = xmlExpExpDeriveInt(ctxt, exp->exp_left, sub->exp_left);
7429 if (ret == NULL)
7430 return(NULL);
7431 if (ret != forbiddenExp) {
7432#ifdef DEBUG_DERIV
7433 printf("Seq , Count match on left\n");
7434#endif
7435 if (sub->exp_max < 0)
7436 max = -1;
7437 else
7438 max = sub->exp_max -1;
7439 if (sub->exp_min > 0)
7440 min = sub->exp_min -1;
7441 else
7442 min = 0;
7443 exp->exp_right->ref++;
7444 tmp = xmlExpHashGetEntry(ctxt, XML_EXP_SEQ, ret,
7445 exp->exp_right, NULL, 0, 0);
7446 if (tmp == NULL)
7447 return(NULL);
7448
7449 sub->exp_left->ref++;
7450 tmp2 = xmlExpHashGetEntry(ctxt, XML_EXP_COUNT,
7451 sub->exp_left, NULL, NULL, min, max);
7452 if (tmp2 == NULL) {
7453 xmlExpFree(ctxt, tmp);
7454 return(NULL);
7455 }
7456 ret = xmlExpExpDeriveInt(ctxt, tmp, tmp2);
7457 xmlExpFree(ctxt, tmp);
7458 xmlExpFree(ctxt, tmp2);
7459 return(ret);
7460 }
7461 }
7462 /* we made no progress on structured operations */
7463 break;
7464 case XML_EXP_OR:
7465#ifdef DEBUG_DERIV
7466 printf("Or , trying both side\n");
7467#endif
7468 ret = xmlExpExpDeriveInt(ctxt, exp->exp_left, sub);
7469 if (ret == NULL)
7470 return(NULL);
7471 tmp = xmlExpExpDeriveInt(ctxt, exp->exp_right, sub);
7472 if (tmp == NULL) {
7473 xmlExpFree(ctxt, ret);
7474 return(NULL);
7475 }
7476 return(xmlExpHashGetEntry(ctxt, XML_EXP_OR, ret, tmp, NULL, 0, 0));
7477 case XML_EXP_COUNT: {
7478 int min, max;
7479
7480 if (sub->type == XML_EXP_COUNT) {
7481 /*
7482 * Try to see if the loop is completely subsumed
7483 */
7484 tmp = xmlExpExpDeriveInt(ctxt, exp->exp_left, sub->exp_left);
7485 if (tmp == NULL)
7486 return(NULL);
7487 if (tmp == forbiddenExp) {
7488 int mult;
7489
7490#ifdef DEBUG_DERIV
7491 printf("Count, Count inner don't subsume\n");
7492#endif
7493 mult = xmlExpDivide(ctxt, sub->exp_left, exp->exp_left,
7494 NULL, &tmp);
7495 if (mult <= 0) {
7496#ifdef DEBUG_DERIV
7497 printf("Count, Count not multiple => forbidden\n");
7498#endif
7499 return(forbiddenExp);
7500 }
7501 if (sub->exp_max == -1) {
7502 max = -1;
7503 if (exp->exp_max == -1) {
7504 if (exp->exp_min <= sub->exp_min * mult)
7505 min = 0;
7506 else
7507 min = exp->exp_min - sub->exp_min * mult;
7508 } else {
7509#ifdef DEBUG_DERIV
7510 printf("Count, Count finite can't subsume infinite\n");
7511#endif
7512 xmlExpFree(ctxt, tmp);
7513 return(forbiddenExp);
7514 }
7515 } else {
7516 if (exp->exp_max == -1) {
7517#ifdef DEBUG_DERIV
7518 printf("Infinite loop consume mult finite loop\n");
7519#endif
7520 if (exp->exp_min > sub->exp_min * mult) {
7521 max = -1;
7522 min = exp->exp_min - sub->exp_min * mult;
7523 } else {
7524 max = -1;
7525 min = 0;
7526 }
7527 } else {
7528 if (exp->exp_max < sub->exp_max * mult) {
7529#ifdef DEBUG_DERIV
7530 printf("loops max mult mismatch => forbidden\n");
7531#endif
7532 xmlExpFree(ctxt, tmp);
7533 return(forbiddenExp);
7534 }
7535 if (sub->exp_max * mult > exp->exp_min)
7536 min = 0;
7537 else
7538 min = exp->exp_min - sub->exp_max * mult;
7539 max = exp->exp_max - sub->exp_max * mult;
7540 }
7541 }
7542 } else if (!IS_NILLABLE(tmp)) {
7543 /*
7544 * TODO: loop here to try to grow if working on finite
7545 * blocks.
7546 */
7547#ifdef DEBUG_DERIV
7548 printf("Count, Count remain not nillable => forbidden\n");
7549#endif
7550 xmlExpFree(ctxt, tmp);
7551 return(forbiddenExp);
7552 } else if (sub->exp_max == -1) {
7553 if (exp->exp_max == -1) {
7554 if (exp->exp_min <= sub->exp_min) {
7555#ifdef DEBUG_DERIV
7556 printf("Infinite loops Okay => COUNT(0,Inf)\n");
7557#endif
7558 max = -1;
7559 min = 0;
7560 } else {
7561#ifdef DEBUG_DERIV
7562 printf("Infinite loops min => Count(X,Inf)\n");
7563#endif
7564 max = -1;
7565 min = exp->exp_min - sub->exp_min;
7566 }
7567 } else if (exp->exp_min > sub->exp_min) {
7568#ifdef DEBUG_DERIV
7569 printf("loops min mismatch 1 => forbidden ???\n");
7570#endif
7571 xmlExpFree(ctxt, tmp);
7572 return(forbiddenExp);
7573 } else {
7574 max = -1;
7575 min = 0;
7576 }
7577 } else {
7578 if (exp->exp_max == -1) {
7579#ifdef DEBUG_DERIV
7580 printf("Infinite loop consume finite loop\n");
7581#endif
7582 if (exp->exp_min > sub->exp_min) {
7583 max = -1;
7584 min = exp->exp_min - sub->exp_min;
7585 } else {
7586 max = -1;
7587 min = 0;
7588 }
7589 } else {
7590 if (exp->exp_max < sub->exp_max) {
7591#ifdef DEBUG_DERIV
7592 printf("loops max mismatch => forbidden\n");
7593#endif
7594 xmlExpFree(ctxt, tmp);
7595 return(forbiddenExp);
7596 }
7597 if (sub->exp_max > exp->exp_min)
7598 min = 0;
7599 else
7600 min = exp->exp_min - sub->exp_max;
7601 max = exp->exp_max - sub->exp_max;
7602 }
7603 }
7604#ifdef DEBUG_DERIV
7605 printf("loops match => SEQ(COUNT())\n");
7606#endif
7607 exp->exp_left->ref++;
7608 tmp2 = xmlExpHashGetEntry(ctxt, XML_EXP_COUNT, exp->exp_left,
7609 NULL, NULL, min, max);
7610 if (tmp2 == NULL) {
7611 return(NULL);
7612 }
7613 ret = xmlExpHashGetEntry(ctxt, XML_EXP_SEQ, tmp, tmp2,
7614 NULL, 0, 0);
7615 return(ret);
7616 }
7617 tmp = xmlExpExpDeriveInt(ctxt, exp->exp_left, sub);
7618 if (tmp == NULL)
7619 return(NULL);
7620 if (tmp == forbiddenExp) {
7621#ifdef DEBUG_DERIV
7622 printf("loop mismatch => forbidden\n");
7623#endif
7624 return(forbiddenExp);
7625 }
7626 if (exp->exp_min > 0)
7627 min = exp->exp_min - 1;
7628 else
7629 min = 0;
7630 if (exp->exp_max < 0)
7631 max = -1;
7632 else
7633 max = exp->exp_max - 1;
7634
7635#ifdef DEBUG_DERIV
7636 printf("loop match => SEQ(COUNT())\n");
7637#endif
7638 exp->exp_left->ref++;
7639 tmp2 = xmlExpHashGetEntry(ctxt, XML_EXP_COUNT, exp->exp_left,
7640 NULL, NULL, min, max);
7641 if (tmp2 == NULL)
7642 return(NULL);
7643 ret = xmlExpHashGetEntry(ctxt, XML_EXP_SEQ, tmp, tmp2,
7644 NULL, 0, 0);
7645 return(ret);
7646 }
7647 }
7648
Daniel Veillardccb4d412005-08-23 13:41:17 +00007649#ifdef DEBUG_DERIV
7650 printf("Fallback to derivative\n");
7651#endif
7652 if (IS_NILLABLE(sub)) {
7653 if (!(IS_NILLABLE(exp)))
7654 return(forbiddenExp);
7655 else
7656 ret = emptyExp;
7657 } else
7658 ret = NULL;
Daniel Veillard81a8ec62005-08-22 00:20:58 +00007659 /*
7660 * here the structured derivation made no progress so
7661 * we use the default token based derivation to force one more step
7662 */
7663 if (ctxt->tabSize == 0)
7664 ctxt->tabSize = 40;
7665
7666 tab = (const xmlChar **) xmlMalloc(ctxt->tabSize *
7667 sizeof(const xmlChar *));
7668 if (tab == NULL) {
7669 return(NULL);
7670 }
7671
Daniel Veillard81a8ec62005-08-22 00:20:58 +00007672 /*
7673 * collect all the strings accepted by the subexpression on input
7674 */
7675 len = xmlExpGetStartInt(ctxt, sub, tab, ctxt->tabSize, 0);
7676 while (len < 0) {
7677 const xmlChar **temp;
Rob Richards54a8f672005-10-07 02:33:00 +00007678 temp = (const xmlChar **) xmlRealloc((xmlChar **) tab, ctxt->tabSize * 2 *
Daniel Veillard81a8ec62005-08-22 00:20:58 +00007679 sizeof(const xmlChar *));
7680 if (temp == NULL) {
Rob Richards54a8f672005-10-07 02:33:00 +00007681 xmlFree((xmlChar **) tab);
Daniel Veillard81a8ec62005-08-22 00:20:58 +00007682 return(NULL);
7683 }
7684 tab = temp;
7685 ctxt->tabSize *= 2;
7686 len = xmlExpGetStartInt(ctxt, sub, tab, ctxt->tabSize, 0);
7687 }
Daniel Veillard81a8ec62005-08-22 00:20:58 +00007688 for (i = 0;i < len;i++) {
7689 tmp = xmlExpStringDeriveInt(ctxt, exp, tab[i]);
7690 if ((tmp == NULL) || (tmp == forbiddenExp)) {
7691 xmlExpFree(ctxt, ret);
Rob Richards54a8f672005-10-07 02:33:00 +00007692 xmlFree((xmlChar **) tab);
Daniel Veillard81a8ec62005-08-22 00:20:58 +00007693 return(tmp);
7694 }
7695 tmp2 = xmlExpStringDeriveInt(ctxt, sub, tab[i]);
7696 if ((tmp2 == NULL) || (tmp2 == forbiddenExp)) {
7697 xmlExpFree(ctxt, tmp);
7698 xmlExpFree(ctxt, ret);
Rob Richards54a8f672005-10-07 02:33:00 +00007699 xmlFree((xmlChar **) tab);
Daniel Veillard81a8ec62005-08-22 00:20:58 +00007700 return(tmp);
7701 }
7702 tmp3 = xmlExpExpDeriveInt(ctxt, tmp, tmp2);
7703 xmlExpFree(ctxt, tmp);
7704 xmlExpFree(ctxt, tmp2);
7705
7706 if ((tmp3 == NULL) || (tmp3 == forbiddenExp)) {
7707 xmlExpFree(ctxt, ret);
Rob Richards54a8f672005-10-07 02:33:00 +00007708 xmlFree((xmlChar **) tab);
Daniel Veillard81a8ec62005-08-22 00:20:58 +00007709 return(tmp3);
7710 }
7711
7712 if (ret == NULL)
7713 ret = tmp3;
7714 else {
7715 ret = xmlExpHashGetEntry(ctxt, XML_EXP_OR, ret, tmp3, NULL, 0, 0);
7716 if (ret == NULL) {
Rob Richards54a8f672005-10-07 02:33:00 +00007717 xmlFree((xmlChar **) tab);
Daniel Veillard81a8ec62005-08-22 00:20:58 +00007718 return(NULL);
7719 }
7720 }
7721 }
Rob Richards54a8f672005-10-07 02:33:00 +00007722 xmlFree((xmlChar **) tab);
Daniel Veillard81a8ec62005-08-22 00:20:58 +00007723 return(ret);
7724}
7725
7726/**
Daniel Veillard0090bd52005-08-22 14:43:43 +00007727 * xmlExpExpDerive:
7728 * @ctxt: the expressions context
7729 * @exp: the englobing expression
7730 * @sub: the subexpression
7731 *
7732 * Evaluates the expression resulting from @exp consuming a sub expression @sub
7733 * Based on algebraic derivation and sometimes direct Brzozowski derivation
7734 * it usually tatkes less than linear time and can handle expressions generating
7735 * infinite languages.
7736 *
7737 * Returns the resulting expression or NULL in case of internal error, the
7738 * result must be freed
7739 */
7740xmlExpNodePtr
7741xmlExpExpDerive(xmlExpCtxtPtr ctxt, xmlExpNodePtr exp, xmlExpNodePtr sub) {
7742 if ((exp == NULL) || (ctxt == NULL) || (sub == NULL))
7743 return(NULL);
7744
7745 /*
7746 * O(1) speedups
7747 */
7748 if (IS_NILLABLE(sub) && (!IS_NILLABLE(exp))) {
7749#ifdef DEBUG_DERIV
7750 printf("Sub nillable and not exp : can't subsume\n");
7751#endif
7752 return(forbiddenExp);
7753 }
7754 if (xmlExpCheckCard(exp, sub) == 0) {
7755#ifdef DEBUG_DERIV
7756 printf("sub generate longuer sequances than exp : can't subsume\n");
7757#endif
7758 return(forbiddenExp);
7759 }
7760 return(xmlExpExpDeriveInt(ctxt, exp, sub));
7761}
7762
7763/**
Daniel Veillard81a8ec62005-08-22 00:20:58 +00007764 * xmlExpSubsume:
7765 * @ctxt: the expressions context
7766 * @exp: the englobing expression
7767 * @sub: the subexpression
7768 *
7769 * Check whether @exp accepts all the languages accexpted by @sub
7770 * the input being a subexpression.
7771 *
7772 * Returns 1 if true 0 if false and -1 in case of failure.
7773 */
7774int
7775xmlExpSubsume(xmlExpCtxtPtr ctxt, xmlExpNodePtr exp, xmlExpNodePtr sub) {
7776 xmlExpNodePtr tmp;
7777
7778 if ((exp == NULL) || (ctxt == NULL) || (sub == NULL))
7779 return(-1);
7780
7781 /*
7782 * TODO: speedup by checking the language of sub is a subset of the
7783 * language of exp
7784 */
7785 /*
7786 * O(1) speedups
7787 */
7788 if (IS_NILLABLE(sub) && (!IS_NILLABLE(exp))) {
7789#ifdef DEBUG_DERIV
7790 printf("Sub nillable and not exp : can't subsume\n");
7791#endif
7792 return(0);
7793 }
7794 if (xmlExpCheckCard(exp, sub) == 0) {
7795#ifdef DEBUG_DERIV
7796 printf("sub generate longuer sequances than exp : can't subsume\n");
7797#endif
7798 return(0);
7799 }
7800 tmp = xmlExpExpDeriveInt(ctxt, exp, sub);
7801#ifdef DEBUG_DERIV
7802 printf("Result derivation :\n");
7803 PRINT_EXP(tmp);
7804#endif
7805 if (tmp == NULL)
7806 return(-1);
7807 if (tmp == forbiddenExp)
7808 return(0);
7809 if (tmp == emptyExp)
7810 return(1);
7811 if ((tmp != NULL) && (IS_NILLABLE(tmp))) {
7812 xmlExpFree(ctxt, tmp);
7813 return(1);
7814 }
7815 xmlExpFree(ctxt, tmp);
7816 return(0);
7817}
Daniel Veillard465a0002005-08-22 12:07:04 +00007818
7819/************************************************************************
7820 * *
7821 * Parsing expression *
7822 * *
7823 ************************************************************************/
7824
7825static xmlExpNodePtr xmlExpParseExpr(xmlExpCtxtPtr ctxt);
7826
7827#undef CUR
7828#define CUR (*ctxt->cur)
7829#undef NEXT
7830#define NEXT ctxt->cur++;
7831#undef IS_BLANK
7832#define IS_BLANK(c) ((c == ' ') || (c == '\n') || (c == '\r') || (c == '\t'))
7833#define SKIP_BLANKS while (IS_BLANK(*ctxt->cur)) ctxt->cur++;
7834
7835static int
7836xmlExpParseNumber(xmlExpCtxtPtr ctxt) {
7837 int ret = 0;
7838
7839 SKIP_BLANKS
7840 if (CUR == '*') {
7841 NEXT
7842 return(-1);
7843 }
7844 if ((CUR < '0') || (CUR > '9'))
7845 return(-1);
7846 while ((CUR >= '0') && (CUR <= '9')) {
7847 ret = ret * 10 + (CUR - '0');
7848 NEXT
7849 }
7850 return(ret);
7851}
7852
7853static xmlExpNodePtr
7854xmlExpParseOr(xmlExpCtxtPtr ctxt) {
7855 const char *base;
7856 xmlExpNodePtr ret;
7857 const xmlChar *val;
7858
7859 SKIP_BLANKS
7860 base = ctxt->cur;
7861 if (*ctxt->cur == '(') {
7862 NEXT
7863 ret = xmlExpParseExpr(ctxt);
7864 SKIP_BLANKS
7865 if (*ctxt->cur != ')') {
7866 fprintf(stderr, "unbalanced '(' : %s\n", base);
7867 xmlExpFree(ctxt, ret);
7868 return(NULL);
7869 }
7870 NEXT;
7871 SKIP_BLANKS
7872 goto parse_quantifier;
7873 }
7874 while ((CUR != 0) && (!(IS_BLANK(CUR))) && (CUR != '(') &&
7875 (CUR != ')') && (CUR != '|') && (CUR != ',') && (CUR != '{') &&
7876 (CUR != '*') && (CUR != '+') && (CUR != '?') && (CUR != '}'))
7877 NEXT;
7878 val = xmlDictLookup(ctxt->dict, BAD_CAST base, ctxt->cur - base);
7879 if (val == NULL)
7880 return(NULL);
7881 ret = xmlExpHashGetEntry(ctxt, XML_EXP_ATOM, NULL, NULL, val, 0, 0);
7882 if (ret == NULL)
7883 return(NULL);
7884 SKIP_BLANKS
7885parse_quantifier:
7886 if (CUR == '{') {
7887 int min, max;
7888
7889 NEXT
7890 min = xmlExpParseNumber(ctxt);
7891 if (min < 0) {
7892 xmlExpFree(ctxt, ret);
7893 return(NULL);
7894 }
7895 SKIP_BLANKS
7896 if (CUR == ',') {
7897 NEXT
7898 max = xmlExpParseNumber(ctxt);
7899 SKIP_BLANKS
7900 } else
7901 max = min;
7902 if (CUR != '}') {
7903 xmlExpFree(ctxt, ret);
7904 return(NULL);
7905 }
7906 NEXT
7907 ret = xmlExpHashGetEntry(ctxt, XML_EXP_COUNT, ret, NULL, NULL,
7908 min, max);
7909 SKIP_BLANKS
7910 } else if (CUR == '?') {
7911 NEXT
7912 ret = xmlExpHashGetEntry(ctxt, XML_EXP_COUNT, ret, NULL, NULL,
7913 0, 1);
7914 SKIP_BLANKS
7915 } else if (CUR == '+') {
7916 NEXT
7917 ret = xmlExpHashGetEntry(ctxt, XML_EXP_COUNT, ret, NULL, NULL,
7918 1, -1);
7919 SKIP_BLANKS
7920 } else if (CUR == '*') {
7921 NEXT
7922 ret = xmlExpHashGetEntry(ctxt, XML_EXP_COUNT, ret, NULL, NULL,
7923 0, -1);
7924 SKIP_BLANKS
7925 }
7926 return(ret);
7927}
7928
7929
7930static xmlExpNodePtr
7931xmlExpParseSeq(xmlExpCtxtPtr ctxt) {
7932 xmlExpNodePtr ret, right;
7933
7934 ret = xmlExpParseOr(ctxt);
7935 SKIP_BLANKS
7936 while (CUR == '|') {
7937 NEXT
7938 right = xmlExpParseOr(ctxt);
7939 if (right == NULL) {
7940 xmlExpFree(ctxt, ret);
7941 return(NULL);
7942 }
7943 ret = xmlExpHashGetEntry(ctxt, XML_EXP_OR, ret, right, NULL, 0, 0);
7944 if (ret == NULL)
7945 return(NULL);
7946 }
7947 return(ret);
7948}
7949
7950static xmlExpNodePtr
7951xmlExpParseExpr(xmlExpCtxtPtr ctxt) {
7952 xmlExpNodePtr ret, right;
7953
7954 ret = xmlExpParseSeq(ctxt);
7955 SKIP_BLANKS
7956 while (CUR == ',') {
7957 NEXT
7958 right = xmlExpParseSeq(ctxt);
7959 if (right == NULL) {
7960 xmlExpFree(ctxt, ret);
7961 return(NULL);
7962 }
7963 ret = xmlExpHashGetEntry(ctxt, XML_EXP_SEQ, ret, right, NULL, 0, 0);
7964 if (ret == NULL)
7965 return(NULL);
7966 }
7967 return(ret);
7968}
7969
7970/**
7971 * xmlExpParse:
7972 * @ctxt: the expressions context
7973 * @expr: the 0 terminated string
7974 *
7975 * Minimal parser for regexps, it understand the following constructs
7976 * - string terminals
7977 * - choice operator |
7978 * - sequence operator ,
7979 * - subexpressions (...)
7980 * - usual cardinality operators + * and ?
7981 * - finite sequences { min, max }
7982 * - infinite sequences { min, * }
7983 * There is minimal checkings made especially no checking on strings values
7984 *
7985 * Returns a new expression or NULL in case of failure
7986 */
7987xmlExpNodePtr
7988xmlExpParse(xmlExpCtxtPtr ctxt, const char *expr) {
7989 xmlExpNodePtr ret;
7990
7991 ctxt->expr = expr;
7992 ctxt->cur = expr;
7993
7994 ret = xmlExpParseExpr(ctxt);
7995 SKIP_BLANKS
7996 if (*ctxt->cur != 0) {
7997 xmlExpFree(ctxt, ret);
7998 return(NULL);
7999 }
8000 return(ret);
8001}
8002
8003static void
8004xmlExpDumpInt(xmlBufferPtr buf, xmlExpNodePtr expr, int glob) {
8005 xmlExpNodePtr c;
8006
8007 if (expr == NULL) return;
8008 if (glob) xmlBufferWriteChar(buf, "(");
8009 switch (expr->type) {
8010 case XML_EXP_EMPTY:
8011 xmlBufferWriteChar(buf, "empty");
8012 break;
8013 case XML_EXP_FORBID:
8014 xmlBufferWriteChar(buf, "forbidden");
8015 break;
8016 case XML_EXP_ATOM:
8017 xmlBufferWriteCHAR(buf, expr->exp_str);
8018 break;
8019 case XML_EXP_SEQ:
8020 c = expr->exp_left;
8021 if ((c->type == XML_EXP_SEQ) || (c->type == XML_EXP_OR))
8022 xmlExpDumpInt(buf, c, 1);
8023 else
8024 xmlExpDumpInt(buf, c, 0);
8025 xmlBufferWriteChar(buf, " , ");
8026 c = expr->exp_right;
8027 if ((c->type == XML_EXP_SEQ) || (c->type == XML_EXP_OR))
8028 xmlExpDumpInt(buf, c, 1);
8029 else
8030 xmlExpDumpInt(buf, c, 0);
8031 break;
8032 case XML_EXP_OR:
8033 c = expr->exp_left;
8034 if ((c->type == XML_EXP_SEQ) || (c->type == XML_EXP_OR))
8035 xmlExpDumpInt(buf, c, 1);
8036 else
8037 xmlExpDumpInt(buf, c, 0);
8038 xmlBufferWriteChar(buf, " | ");
8039 c = expr->exp_right;
8040 if ((c->type == XML_EXP_SEQ) || (c->type == XML_EXP_OR))
8041 xmlExpDumpInt(buf, c, 1);
8042 else
8043 xmlExpDumpInt(buf, c, 0);
8044 break;
8045 case XML_EXP_COUNT: {
8046 char rep[40];
8047
8048 c = expr->exp_left;
8049 if ((c->type == XML_EXP_SEQ) || (c->type == XML_EXP_OR))
8050 xmlExpDumpInt(buf, c, 1);
8051 else
8052 xmlExpDumpInt(buf, c, 0);
8053 if ((expr->exp_min == 0) && (expr->exp_max == 1)) {
8054 rep[0] = '?';
8055 rep[1] = 0;
8056 } else if ((expr->exp_min == 0) && (expr->exp_max == -1)) {
8057 rep[0] = '*';
8058 rep[1] = 0;
8059 } else if ((expr->exp_min == 1) && (expr->exp_max == -1)) {
8060 rep[0] = '+';
8061 rep[1] = 0;
8062 } else if (expr->exp_max == expr->exp_min) {
8063 snprintf(rep, 39, "{%d}", expr->exp_min);
8064 } else if (expr->exp_max < 0) {
8065 snprintf(rep, 39, "{%d,inf}", expr->exp_min);
8066 } else {
8067 snprintf(rep, 39, "{%d,%d}", expr->exp_min, expr->exp_max);
8068 }
8069 rep[39] = 0;
8070 xmlBufferWriteChar(buf, rep);
8071 break;
8072 }
8073 default:
8074 fprintf(stderr, "Error in tree\n");
8075 }
8076 if (glob)
8077 xmlBufferWriteChar(buf, ")");
8078}
8079/**
8080 * xmlExpDump:
8081 * @buf: a buffer to receive the output
8082 * @expr: the compiled expression
8083 *
8084 * Serialize the expression as compiled to the buffer
8085 */
8086void
Daniel Veillard5eee7672005-08-22 21:22:27 +00008087xmlExpDump(xmlBufferPtr buf, xmlExpNodePtr expr) {
8088 if ((buf == NULL) || (expr == NULL))
Daniel Veillard465a0002005-08-22 12:07:04 +00008089 return;
Daniel Veillard5eee7672005-08-22 21:22:27 +00008090 xmlExpDumpInt(buf, expr, 0);
Daniel Veillard465a0002005-08-22 12:07:04 +00008091}
8092
8093/**
8094 * xmlExpMaxToken:
8095 * @expr: a compiled expression
8096 *
8097 * Indicate the maximum number of input a expression can accept
8098 *
8099 * Returns the maximum length or -1 in case of error
8100 */
8101int
8102xmlExpMaxToken(xmlExpNodePtr expr) {
8103 if (expr == NULL)
8104 return(-1);
8105 return(expr->c_max);
8106}
8107
8108/**
8109 * xmlExpCtxtNbNodes:
8110 * @ctxt: an expression context
8111 *
8112 * Debugging facility provides the number of allocated nodes at a that point
8113 *
8114 * Returns the number of nodes in use or -1 in case of error
8115 */
8116int
8117xmlExpCtxtNbNodes(xmlExpCtxtPtr ctxt) {
8118 if (ctxt == NULL)
8119 return(-1);
8120 return(ctxt->nb_nodes);
8121}
8122
8123/**
8124 * xmlExpCtxtNbCons:
8125 * @ctxt: an expression context
8126 *
8127 * Debugging facility provides the number of allocated nodes over lifetime
8128 *
8129 * Returns the number of nodes ever allocated or -1 in case of error
8130 */
8131int
8132xmlExpCtxtNbCons(xmlExpCtxtPtr ctxt) {
8133 if (ctxt == NULL)
8134 return(-1);
8135 return(ctxt->nb_cons);
8136}
8137
Daniel Veillard81a8ec62005-08-22 00:20:58 +00008138#endif /* LIBXML_EXPR_ENABLED */
Daniel Veillard5d4644e2005-04-01 13:11:58 +00008139#define bottom_xmlregexp
8140#include "elfgcchack.h"
Daniel Veillard4255d502002-04-16 15:50:10 +00008141#endif /* LIBXML_REGEXP_ENABLED */