blob: 8e63d74a30a2c43f7f2868157193576afc74506b [file] [log] [blame]
Daniel Veillard4255d502002-04-16 15:50:10 +00001/*
2 * regexp.c: generic and extensible Regular Expression engine
3 *
Daniel Veillardf8e3db02012-09-11 13:26:36 +08004 * Basically designed with the purpose of compiling regexps for
Daniel Veillard4255d502002-04-16 15:50:10 +00005 * 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
Patrick R. Gansterer204f1f12012-05-10 20:24:00 +080047#ifdef ERROR
48#undef ERROR
49#endif
Daniel Veillardff46a042003-10-08 08:53:17 +000050#define ERROR(str) \
51 ctxt->error = XML_REGEXP_COMPILE_ERROR; \
52 xmlRegexpErrCompile(ctxt, str);
Daniel Veillard4255d502002-04-16 15:50:10 +000053#define NEXT ctxt->cur++
54#define CUR (*(ctxt->cur))
55#define NXT(index) (ctxt->cur[index])
56
57#define CUR_SCHAR(s, l) xmlStringCurrentChar(NULL, s, &l)
58#define NEXTL(l) ctxt->cur += l;
Daniel Veillardc0826a72004-08-10 14:17:33 +000059#define XML_REG_STRING_SEPARATOR '|'
William M. Bracka9cbf282007-03-21 13:16:33 +000060/*
61 * Need PREV to check on a '-' within a Character Group. May only be used
62 * when it's guaranteed that cur is not at the beginning of ctxt->string!
63 */
64#define PREV (ctxt->cur[-1])
Daniel Veillard4255d502002-04-16 15:50:10 +000065
Daniel Veillarde19fc232002-04-22 16:01:24 +000066/**
67 * TODO:
68 *
69 * macro to flag unimplemented blocks
70 */
Daniel Veillardf8e3db02012-09-11 13:26:36 +080071#define TODO \
Daniel Veillarde19fc232002-04-22 16:01:24 +000072 xmlGenericError(xmlGenericErrorContext, \
73 "Unimplemented block at %s:%d\n", \
74 __FILE__, __LINE__);
75
Daniel Veillard4255d502002-04-16 15:50:10 +000076/************************************************************************
Daniel Veillardf8e3db02012-09-11 13:26:36 +080077 * *
78 * Datatypes and structures *
79 * *
Daniel Veillard4255d502002-04-16 15:50:10 +000080 ************************************************************************/
81
Daniel Veillardfc011b72006-02-12 19:14:15 +000082/*
83 * Note: the order of the enums below is significant, do not shuffle
84 */
Daniel Veillard4255d502002-04-16 15:50:10 +000085typedef enum {
86 XML_REGEXP_EPSILON = 1,
87 XML_REGEXP_CHARVAL,
88 XML_REGEXP_RANGES,
Daniel Veillard567a45b2005-10-18 19:11:55 +000089 XML_REGEXP_SUBREG, /* used for () sub regexps */
Daniel Veillard4255d502002-04-16 15:50:10 +000090 XML_REGEXP_STRING,
91 XML_REGEXP_ANYCHAR, /* . */
92 XML_REGEXP_ANYSPACE, /* \s */
93 XML_REGEXP_NOTSPACE, /* \S */
94 XML_REGEXP_INITNAME, /* \l */
Daniel Veillard567a45b2005-10-18 19:11:55 +000095 XML_REGEXP_NOTINITNAME, /* \L */
Daniel Veillard4255d502002-04-16 15:50:10 +000096 XML_REGEXP_NAMECHAR, /* \c */
97 XML_REGEXP_NOTNAMECHAR, /* \C */
98 XML_REGEXP_DECIMAL, /* \d */
Daniel Veillard567a45b2005-10-18 19:11:55 +000099 XML_REGEXP_NOTDECIMAL, /* \D */
Daniel Veillard4255d502002-04-16 15:50:10 +0000100 XML_REGEXP_REALCHAR, /* \w */
Daniel Veillard567a45b2005-10-18 19:11:55 +0000101 XML_REGEXP_NOTREALCHAR, /* \W */
102 XML_REGEXP_LETTER = 100,
Daniel Veillard4255d502002-04-16 15:50:10 +0000103 XML_REGEXP_LETTER_UPPERCASE,
104 XML_REGEXP_LETTER_LOWERCASE,
105 XML_REGEXP_LETTER_TITLECASE,
106 XML_REGEXP_LETTER_MODIFIER,
107 XML_REGEXP_LETTER_OTHERS,
108 XML_REGEXP_MARK,
109 XML_REGEXP_MARK_NONSPACING,
110 XML_REGEXP_MARK_SPACECOMBINING,
111 XML_REGEXP_MARK_ENCLOSING,
112 XML_REGEXP_NUMBER,
113 XML_REGEXP_NUMBER_DECIMAL,
114 XML_REGEXP_NUMBER_LETTER,
115 XML_REGEXP_NUMBER_OTHERS,
116 XML_REGEXP_PUNCT,
117 XML_REGEXP_PUNCT_CONNECTOR,
118 XML_REGEXP_PUNCT_DASH,
119 XML_REGEXP_PUNCT_OPEN,
120 XML_REGEXP_PUNCT_CLOSE,
121 XML_REGEXP_PUNCT_INITQUOTE,
122 XML_REGEXP_PUNCT_FINQUOTE,
123 XML_REGEXP_PUNCT_OTHERS,
124 XML_REGEXP_SEPAR,
125 XML_REGEXP_SEPAR_SPACE,
126 XML_REGEXP_SEPAR_LINE,
127 XML_REGEXP_SEPAR_PARA,
128 XML_REGEXP_SYMBOL,
129 XML_REGEXP_SYMBOL_MATH,
130 XML_REGEXP_SYMBOL_CURRENCY,
131 XML_REGEXP_SYMBOL_MODIFIER,
132 XML_REGEXP_SYMBOL_OTHERS,
133 XML_REGEXP_OTHER,
134 XML_REGEXP_OTHER_CONTROL,
135 XML_REGEXP_OTHER_FORMAT,
136 XML_REGEXP_OTHER_PRIVATE,
137 XML_REGEXP_OTHER_NA,
138 XML_REGEXP_BLOCK_NAME
139} xmlRegAtomType;
140
141typedef enum {
142 XML_REGEXP_QUANT_EPSILON = 1,
143 XML_REGEXP_QUANT_ONCE,
144 XML_REGEXP_QUANT_OPT,
145 XML_REGEXP_QUANT_MULT,
146 XML_REGEXP_QUANT_PLUS,
Daniel Veillard7646b182002-04-20 06:41:40 +0000147 XML_REGEXP_QUANT_ONCEONLY,
148 XML_REGEXP_QUANT_ALL,
Daniel Veillard4255d502002-04-16 15:50:10 +0000149 XML_REGEXP_QUANT_RANGE
150} xmlRegQuantType;
151
152typedef enum {
153 XML_REGEXP_START_STATE = 1,
154 XML_REGEXP_FINAL_STATE,
Daniel Veillardcc026dc2005-01-12 13:21:17 +0000155 XML_REGEXP_TRANS_STATE,
Daniel Veillard0e05f4c2006-11-01 15:33:04 +0000156 XML_REGEXP_SINK_STATE,
157 XML_REGEXP_UNREACH_STATE
Daniel Veillard4255d502002-04-16 15:50:10 +0000158} xmlRegStateType;
159
160typedef enum {
161 XML_REGEXP_MARK_NORMAL = 0,
162 XML_REGEXP_MARK_START,
163 XML_REGEXP_MARK_VISITED
164} xmlRegMarkedType;
165
166typedef struct _xmlRegRange xmlRegRange;
167typedef xmlRegRange *xmlRegRangePtr;
168
169struct _xmlRegRange {
Daniel Veillardf8b9de32003-11-24 14:27:26 +0000170 int neg; /* 0 normal, 1 not, 2 exclude */
Daniel Veillard4255d502002-04-16 15:50:10 +0000171 xmlRegAtomType type;
172 int start;
173 int end;
174 xmlChar *blockName;
175};
176
177typedef struct _xmlRegAtom xmlRegAtom;
178typedef xmlRegAtom *xmlRegAtomPtr;
179
180typedef struct _xmlAutomataState xmlRegState;
181typedef xmlRegState *xmlRegStatePtr;
182
183struct _xmlRegAtom {
184 int no;
185 xmlRegAtomType type;
186 xmlRegQuantType quant;
187 int min;
188 int max;
189
190 void *valuep;
Daniel Veillarda646cfd2002-09-17 21:50:03 +0000191 void *valuep2;
Daniel Veillard4255d502002-04-16 15:50:10 +0000192 int neg;
193 int codepoint;
194 xmlRegStatePtr start;
Daniel Veillard76d59b62007-08-22 16:29:21 +0000195 xmlRegStatePtr start0;
Daniel Veillard4255d502002-04-16 15:50:10 +0000196 xmlRegStatePtr stop;
197 int maxRanges;
198 int nbRanges;
199 xmlRegRangePtr *ranges;
200 void *data;
201};
202
203typedef struct _xmlRegCounter xmlRegCounter;
204typedef xmlRegCounter *xmlRegCounterPtr;
205
206struct _xmlRegCounter {
207 int min;
208 int max;
209};
210
211typedef struct _xmlRegTrans xmlRegTrans;
212typedef xmlRegTrans *xmlRegTransPtr;
213
214struct _xmlRegTrans {
215 xmlRegAtomPtr atom;
216 int to;
217 int counter;
218 int count;
Daniel Veillard567a45b2005-10-18 19:11:55 +0000219 int nd;
Daniel Veillard4255d502002-04-16 15:50:10 +0000220};
221
222struct _xmlAutomataState {
223 xmlRegStateType type;
224 xmlRegMarkedType mark;
Daniel Veillard466fcda2012-08-27 12:03:40 +0800225 xmlRegMarkedType markd;
Daniel Veillard23e73572002-09-19 19:56:43 +0000226 xmlRegMarkedType reached;
Daniel Veillard4255d502002-04-16 15:50:10 +0000227 int no;
Daniel Veillard4255d502002-04-16 15:50:10 +0000228 int maxTrans;
229 int nbTrans;
230 xmlRegTrans *trans;
Daniel Veillarddb68b742005-07-30 13:18:24 +0000231 /* knowing states ponting to us can speed things up */
232 int maxTransTo;
233 int nbTransTo;
234 int *transTo;
Daniel Veillard4255d502002-04-16 15:50:10 +0000235};
236
237typedef struct _xmlAutomata xmlRegParserCtxt;
238typedef xmlRegParserCtxt *xmlRegParserCtxtPtr;
239
Daniel Veillard1ba2aca2009-08-31 16:47:39 +0200240#define AM_AUTOMATA_RNG 1
241
Daniel Veillard4255d502002-04-16 15:50:10 +0000242struct _xmlAutomata {
243 xmlChar *string;
244 xmlChar *cur;
245
246 int error;
247 int neg;
248
249 xmlRegStatePtr start;
250 xmlRegStatePtr end;
251 xmlRegStatePtr state;
252
253 xmlRegAtomPtr atom;
254
255 int maxAtoms;
256 int nbAtoms;
257 xmlRegAtomPtr *atoms;
258
259 int maxStates;
260 int nbStates;
261 xmlRegStatePtr *states;
262
263 int maxCounters;
264 int nbCounters;
265 xmlRegCounter *counters;
Daniel Veillarde19fc232002-04-22 16:01:24 +0000266
267 int determinist;
Daniel Veillard6e65e152005-08-09 11:09:52 +0000268 int negs;
Daniel Veillard1ba2aca2009-08-31 16:47:39 +0200269 int flags;
Daniel Veillard4255d502002-04-16 15:50:10 +0000270};
271
272struct _xmlRegexp {
273 xmlChar *string;
274 int nbStates;
275 xmlRegStatePtr *states;
276 int nbAtoms;
277 xmlRegAtomPtr *atoms;
278 int nbCounters;
279 xmlRegCounter *counters;
Daniel Veillarde19fc232002-04-22 16:01:24 +0000280 int determinist;
Daniel Veillard1ba2aca2009-08-31 16:47:39 +0200281 int flags;
Daniel Veillard23e73572002-09-19 19:56:43 +0000282 /*
283 * That's the compact form for determinists automatas
284 */
285 int nbstates;
286 int *compact;
Daniel Veillard118aed72002-09-24 14:13:13 +0000287 void **transdata;
Daniel Veillard23e73572002-09-19 19:56:43 +0000288 int nbstrings;
289 xmlChar **stringMap;
Daniel Veillard4255d502002-04-16 15:50:10 +0000290};
291
292typedef struct _xmlRegExecRollback xmlRegExecRollback;
293typedef xmlRegExecRollback *xmlRegExecRollbackPtr;
294
295struct _xmlRegExecRollback {
296 xmlRegStatePtr state;/* the current state */
297 int index; /* the index in the input stack */
298 int nextbranch; /* the next transition to explore in that state */
William M. Brackddf71d62004-05-06 04:17:26 +0000299 int *counts; /* save the automata state if it has some */
Daniel Veillard4255d502002-04-16 15:50:10 +0000300};
301
302typedef struct _xmlRegInputToken xmlRegInputToken;
303typedef xmlRegInputToken *xmlRegInputTokenPtr;
304
305struct _xmlRegInputToken {
306 xmlChar *value;
307 void *data;
308};
309
310struct _xmlRegExecCtxt {
311 int status; /* execution status != 0 indicate an error */
William M. Brackddf71d62004-05-06 04:17:26 +0000312 int determinist; /* did we find an indeterministic behaviour */
Daniel Veillard4255d502002-04-16 15:50:10 +0000313 xmlRegexpPtr comp; /* the compiled regexp */
314 xmlRegExecCallbacks callback;
315 void *data;
316
317 xmlRegStatePtr state;/* the current state */
318 int transno; /* the current transition on that state */
William M. Brackddf71d62004-05-06 04:17:26 +0000319 int transcount; /* the number of chars in char counted transitions */
Daniel Veillard4255d502002-04-16 15:50:10 +0000320
321 /*
322 * A stack of rollback states
323 */
324 int maxRollbacks;
325 int nbRollbacks;
326 xmlRegExecRollback *rollbacks;
327
328 /*
329 * The state of the automata if any
330 */
331 int *counts;
332
333 /*
334 * The input stack
335 */
336 int inputStackMax;
337 int inputStackNr;
338 int index;
339 int *charStack;
340 const xmlChar *inputString; /* when operating on characters */
341 xmlRegInputTokenPtr inputStack;/* when operating on strings */
342
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +0000343 /*
344 * error handling
345 */
346 int errStateNo; /* the error state number */
347 xmlRegStatePtr errState; /* the error state */
348 xmlChar *errString; /* the string raising the error */
349 int *errCounts; /* counters at the error state */
Daniel Veillard94cc1032005-09-15 13:09:00 +0000350 int nbPush;
Daniel Veillard4255d502002-04-16 15:50:10 +0000351};
352
Daniel Veillard441bc322002-04-20 17:38:48 +0000353#define REGEXP_ALL_COUNTER 0x123456
354#define REGEXP_ALL_LAX_COUNTER 0x123457
Daniel Veillard7646b182002-04-20 06:41:40 +0000355
Daniel Veillard4255d502002-04-16 15:50:10 +0000356static void xmlFAParseRegExp(xmlRegParserCtxtPtr ctxt, int top);
Daniel Veillard23e73572002-09-19 19:56:43 +0000357static void xmlRegFreeState(xmlRegStatePtr state);
358static void xmlRegFreeAtom(xmlRegAtomPtr atom);
Daniel Veillard9efc4762005-07-19 14:33:55 +0000359static int xmlRegStrEqualWildcard(const xmlChar *expStr, const xmlChar *valStr);
Daniel Veillard567a45b2005-10-18 19:11:55 +0000360static int xmlRegCheckCharacter(xmlRegAtomPtr atom, int codepoint);
361static int xmlRegCheckCharacterRange(xmlRegAtomType type, int codepoint,
362 int neg, int start, int end, const xmlChar *blockName);
Daniel Veillard4255d502002-04-16 15:50:10 +0000363
Daniel Veillard1ba2aca2009-08-31 16:47:39 +0200364void xmlAutomataSetFlags(xmlAutomataPtr am, int flags);
365
Daniel Veillard4255d502002-04-16 15:50:10 +0000366/************************************************************************
Daniel Veillardff46a042003-10-08 08:53:17 +0000367 * *
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800368 * Regexp memory error handler *
Daniel Veillardff46a042003-10-08 08:53:17 +0000369 * *
370 ************************************************************************/
371/**
372 * xmlRegexpErrMemory:
William M. Brackddf71d62004-05-06 04:17:26 +0000373 * @extra: extra information
Daniel Veillardff46a042003-10-08 08:53:17 +0000374 *
375 * Handle an out of memory condition
376 */
377static void
378xmlRegexpErrMemory(xmlRegParserCtxtPtr ctxt, const char *extra)
379{
380 const char *regexp = NULL;
381 if (ctxt != NULL) {
382 regexp = (const char *) ctxt->string;
383 ctxt->error = XML_ERR_NO_MEMORY;
384 }
Daniel Veillard659e71e2003-10-10 14:10:40 +0000385 __xmlRaiseError(NULL, NULL, NULL, NULL, NULL, XML_FROM_REGEXP,
Daniel Veillardff46a042003-10-08 08:53:17 +0000386 XML_ERR_NO_MEMORY, XML_ERR_FATAL, NULL, 0, extra,
387 regexp, NULL, 0, 0,
388 "Memory allocation failed : %s\n", extra);
389}
390
391/**
392 * xmlRegexpErrCompile:
William M. Brackddf71d62004-05-06 04:17:26 +0000393 * @extra: extra information
Daniel Veillardff46a042003-10-08 08:53:17 +0000394 *
William M. Brackddf71d62004-05-06 04:17:26 +0000395 * Handle a compilation failure
Daniel Veillardff46a042003-10-08 08:53:17 +0000396 */
397static void
398xmlRegexpErrCompile(xmlRegParserCtxtPtr ctxt, const char *extra)
399{
400 const char *regexp = NULL;
401 int idx = 0;
402
403 if (ctxt != NULL) {
404 regexp = (const char *) ctxt->string;
405 idx = ctxt->cur - ctxt->string;
406 ctxt->error = XML_REGEXP_COMPILE_ERROR;
407 }
Daniel Veillard659e71e2003-10-10 14:10:40 +0000408 __xmlRaiseError(NULL, NULL, NULL, NULL, NULL, XML_FROM_REGEXP,
Daniel Veillardff46a042003-10-08 08:53:17 +0000409 XML_REGEXP_COMPILE_ERROR, XML_ERR_FATAL, NULL, 0, extra,
410 regexp, NULL, idx, 0,
411 "failed to compile: %s\n", extra);
412}
413
414/************************************************************************
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800415 * *
416 * Allocation/Deallocation *
417 * *
Daniel Veillard4255d502002-04-16 15:50:10 +0000418 ************************************************************************/
419
Daniel Veillard23e73572002-09-19 19:56:43 +0000420static int xmlFAComputesDeterminism(xmlRegParserCtxtPtr ctxt);
Daniel Veillard4255d502002-04-16 15:50:10 +0000421/**
422 * xmlRegEpxFromParse:
423 * @ctxt: the parser context used to build it
424 *
William M. Brackddf71d62004-05-06 04:17:26 +0000425 * Allocate a new regexp and fill it with the result from the parser
Daniel Veillard4255d502002-04-16 15:50:10 +0000426 *
427 * Returns the new regexp or NULL in case of error
428 */
429static xmlRegexpPtr
430xmlRegEpxFromParse(xmlRegParserCtxtPtr ctxt) {
431 xmlRegexpPtr ret;
432
433 ret = (xmlRegexpPtr) xmlMalloc(sizeof(xmlRegexp));
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000434 if (ret == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +0000435 xmlRegexpErrMemory(ctxt, "compiling regexp");
Daniel Veillard4255d502002-04-16 15:50:10 +0000436 return(NULL);
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000437 }
Daniel Veillard4255d502002-04-16 15:50:10 +0000438 memset(ret, 0, sizeof(xmlRegexp));
439 ret->string = ctxt->string;
Daniel Veillard4255d502002-04-16 15:50:10 +0000440 ret->nbStates = ctxt->nbStates;
Daniel Veillard4255d502002-04-16 15:50:10 +0000441 ret->states = ctxt->states;
Daniel Veillard4255d502002-04-16 15:50:10 +0000442 ret->nbAtoms = ctxt->nbAtoms;
Daniel Veillard4255d502002-04-16 15:50:10 +0000443 ret->atoms = ctxt->atoms;
Daniel Veillard4255d502002-04-16 15:50:10 +0000444 ret->nbCounters = ctxt->nbCounters;
Daniel Veillard4255d502002-04-16 15:50:10 +0000445 ret->counters = ctxt->counters;
Daniel Veillarde19fc232002-04-22 16:01:24 +0000446 ret->determinist = ctxt->determinist;
Daniel Veillard1ba2aca2009-08-31 16:47:39 +0200447 ret->flags = ctxt->flags;
Daniel Veillard567a45b2005-10-18 19:11:55 +0000448 if (ret->determinist == -1) {
449 xmlRegexpIsDeterminist(ret);
450 }
Daniel Veillard23e73572002-09-19 19:56:43 +0000451
452 if ((ret->determinist != 0) &&
453 (ret->nbCounters == 0) &&
Daniel Veillard6e65e152005-08-09 11:09:52 +0000454 (ctxt->negs == 0) &&
Daniel Veillard118aed72002-09-24 14:13:13 +0000455 (ret->atoms != NULL) &&
Daniel Veillard23e73572002-09-19 19:56:43 +0000456 (ret->atoms[0] != NULL) &&
457 (ret->atoms[0]->type == XML_REGEXP_STRING)) {
458 int i, j, nbstates = 0, nbatoms = 0;
459 int *stateRemap;
460 int *stringRemap;
461 int *transitions;
Daniel Veillard118aed72002-09-24 14:13:13 +0000462 void **transdata;
Daniel Veillard23e73572002-09-19 19:56:43 +0000463 xmlChar **stringMap;
464 xmlChar *value;
465
466 /*
467 * Switch to a compact representation
468 * 1/ counting the effective number of states left
William M. Brackddf71d62004-05-06 04:17:26 +0000469 * 2/ counting the unique number of atoms, and check that
Daniel Veillard23e73572002-09-19 19:56:43 +0000470 * they are all of the string type
471 * 3/ build a table state x atom for the transitions
472 */
473
474 stateRemap = xmlMalloc(ret->nbStates * sizeof(int));
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000475 if (stateRemap == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +0000476 xmlRegexpErrMemory(ctxt, "compiling regexp");
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000477 xmlFree(ret);
478 return(NULL);
479 }
Daniel Veillard23e73572002-09-19 19:56:43 +0000480 for (i = 0;i < ret->nbStates;i++) {
481 if (ret->states[i] != NULL) {
482 stateRemap[i] = nbstates;
483 nbstates++;
484 } else {
485 stateRemap[i] = -1;
486 }
487 }
488#ifdef DEBUG_COMPACTION
489 printf("Final: %d states\n", nbstates);
490#endif
491 stringMap = xmlMalloc(ret->nbAtoms * sizeof(char *));
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000492 if (stringMap == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +0000493 xmlRegexpErrMemory(ctxt, "compiling regexp");
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000494 xmlFree(stateRemap);
495 xmlFree(ret);
496 return(NULL);
497 }
Daniel Veillard23e73572002-09-19 19:56:43 +0000498 stringRemap = xmlMalloc(ret->nbAtoms * sizeof(int));
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000499 if (stringRemap == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +0000500 xmlRegexpErrMemory(ctxt, "compiling regexp");
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000501 xmlFree(stringMap);
502 xmlFree(stateRemap);
503 xmlFree(ret);
504 return(NULL);
505 }
Daniel Veillard23e73572002-09-19 19:56:43 +0000506 for (i = 0;i < ret->nbAtoms;i++) {
507 if ((ret->atoms[i]->type == XML_REGEXP_STRING) &&
508 (ret->atoms[i]->quant == XML_REGEXP_QUANT_ONCE)) {
509 value = ret->atoms[i]->valuep;
510 for (j = 0;j < nbatoms;j++) {
511 if (xmlStrEqual(stringMap[j], value)) {
512 stringRemap[i] = j;
513 break;
514 }
515 }
516 if (j >= nbatoms) {
517 stringRemap[i] = nbatoms;
518 stringMap[nbatoms] = xmlStrdup(value);
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000519 if (stringMap[nbatoms] == NULL) {
520 for (i = 0;i < nbatoms;i++)
521 xmlFree(stringMap[i]);
522 xmlFree(stringRemap);
523 xmlFree(stringMap);
524 xmlFree(stateRemap);
525 xmlFree(ret);
526 return(NULL);
527 }
Daniel Veillard23e73572002-09-19 19:56:43 +0000528 nbatoms++;
529 }
530 } else {
531 xmlFree(stateRemap);
532 xmlFree(stringRemap);
533 for (i = 0;i < nbatoms;i++)
534 xmlFree(stringMap[i]);
535 xmlFree(stringMap);
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000536 xmlFree(ret);
537 return(NULL);
Daniel Veillard23e73572002-09-19 19:56:43 +0000538 }
539 }
540#ifdef DEBUG_COMPACTION
541 printf("Final: %d atoms\n", nbatoms);
542#endif
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000543 transitions = (int *) xmlMalloc((nbstates + 1) *
544 (nbatoms + 1) * sizeof(int));
545 if (transitions == NULL) {
546 xmlFree(stateRemap);
547 xmlFree(stringRemap);
548 xmlFree(stringMap);
549 xmlFree(ret);
550 return(NULL);
551 }
552 memset(transitions, 0, (nbstates + 1) * (nbatoms + 1) * sizeof(int));
Daniel Veillard23e73572002-09-19 19:56:43 +0000553
554 /*
555 * Allocate the transition table. The first entry for each
William M. Brackddf71d62004-05-06 04:17:26 +0000556 * state corresponds to the state type.
Daniel Veillard23e73572002-09-19 19:56:43 +0000557 */
Daniel Veillard118aed72002-09-24 14:13:13 +0000558 transdata = NULL;
Daniel Veillard23e73572002-09-19 19:56:43 +0000559
560 for (i = 0;i < ret->nbStates;i++) {
561 int stateno, atomno, targetno, prev;
562 xmlRegStatePtr state;
563 xmlRegTransPtr trans;
564
565 stateno = stateRemap[i];
566 if (stateno == -1)
567 continue;
568 state = ret->states[i];
569
570 transitions[stateno * (nbatoms + 1)] = state->type;
571
572 for (j = 0;j < state->nbTrans;j++) {
573 trans = &(state->trans[j]);
574 if ((trans->to == -1) || (trans->atom == NULL))
575 continue;
576 atomno = stringRemap[trans->atom->no];
Daniel Veillard118aed72002-09-24 14:13:13 +0000577 if ((trans->atom->data != NULL) && (transdata == NULL)) {
578 transdata = (void **) xmlMalloc(nbstates * nbatoms *
579 sizeof(void *));
580 if (transdata != NULL)
581 memset(transdata, 0,
582 nbstates * nbatoms * sizeof(void *));
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000583 else {
Daniel Veillardff46a042003-10-08 08:53:17 +0000584 xmlRegexpErrMemory(ctxt, "compiling regexp");
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000585 break;
586 }
Daniel Veillard118aed72002-09-24 14:13:13 +0000587 }
Daniel Veillard23e73572002-09-19 19:56:43 +0000588 targetno = stateRemap[trans->to];
589 /*
William M. Brackddf71d62004-05-06 04:17:26 +0000590 * if the same atom can generate transitions to 2 different
Daniel Veillard23e73572002-09-19 19:56:43 +0000591 * states then it means the automata is not determinist and
592 * the compact form can't be used !
593 */
594 prev = transitions[stateno * (nbatoms + 1) + atomno + 1];
595 if (prev != 0) {
596 if (prev != targetno + 1) {
Daniel Veillard23e73572002-09-19 19:56:43 +0000597 ret->determinist = 0;
598#ifdef DEBUG_COMPACTION
599 printf("Indet: state %d trans %d, atom %d to %d : %d to %d\n",
600 i, j, trans->atom->no, trans->to, atomno, targetno);
601 printf(" previous to is %d\n", prev);
602#endif
Daniel Veillard118aed72002-09-24 14:13:13 +0000603 if (transdata != NULL)
604 xmlFree(transdata);
Daniel Veillard23e73572002-09-19 19:56:43 +0000605 xmlFree(transitions);
606 xmlFree(stateRemap);
607 xmlFree(stringRemap);
608 for (i = 0;i < nbatoms;i++)
609 xmlFree(stringMap[i]);
610 xmlFree(stringMap);
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000611 goto not_determ;
Daniel Veillard23e73572002-09-19 19:56:43 +0000612 }
613 } else {
614#if 0
615 printf("State %d trans %d: atom %d to %d : %d to %d\n",
616 i, j, trans->atom->no, trans->to, atomno, targetno);
617#endif
618 transitions[stateno * (nbatoms + 1) + atomno + 1] =
Daniel Veillard118aed72002-09-24 14:13:13 +0000619 targetno + 1; /* to avoid 0 */
620 if (transdata != NULL)
621 transdata[stateno * nbatoms + atomno] =
622 trans->atom->data;
Daniel Veillard23e73572002-09-19 19:56:43 +0000623 }
624 }
625 }
626 ret->determinist = 1;
627#ifdef DEBUG_COMPACTION
628 /*
629 * Debug
630 */
631 for (i = 0;i < nbstates;i++) {
632 for (j = 0;j < nbatoms + 1;j++) {
633 printf("%02d ", transitions[i * (nbatoms + 1) + j]);
634 }
635 printf("\n");
636 }
637 printf("\n");
638#endif
639 /*
640 * Cleanup of the old data
641 */
642 if (ret->states != NULL) {
643 for (i = 0;i < ret->nbStates;i++)
644 xmlRegFreeState(ret->states[i]);
645 xmlFree(ret->states);
646 }
647 ret->states = NULL;
648 ret->nbStates = 0;
649 if (ret->atoms != NULL) {
650 for (i = 0;i < ret->nbAtoms;i++)
651 xmlRegFreeAtom(ret->atoms[i]);
652 xmlFree(ret->atoms);
653 }
654 ret->atoms = NULL;
655 ret->nbAtoms = 0;
656
657 ret->compact = transitions;
Daniel Veillard118aed72002-09-24 14:13:13 +0000658 ret->transdata = transdata;
Daniel Veillard23e73572002-09-19 19:56:43 +0000659 ret->stringMap = stringMap;
660 ret->nbstrings = nbatoms;
661 ret->nbstates = nbstates;
662 xmlFree(stateRemap);
663 xmlFree(stringRemap);
664 }
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000665not_determ:
666 ctxt->string = NULL;
667 ctxt->nbStates = 0;
668 ctxt->states = NULL;
669 ctxt->nbAtoms = 0;
670 ctxt->atoms = NULL;
671 ctxt->nbCounters = 0;
672 ctxt->counters = NULL;
Daniel Veillard4255d502002-04-16 15:50:10 +0000673 return(ret);
674}
675
676/**
677 * xmlRegNewParserCtxt:
678 * @string: the string to parse
679 *
680 * Allocate a new regexp parser context
681 *
682 * Returns the new context or NULL in case of error
683 */
684static xmlRegParserCtxtPtr
685xmlRegNewParserCtxt(const xmlChar *string) {
686 xmlRegParserCtxtPtr ret;
687
688 ret = (xmlRegParserCtxtPtr) xmlMalloc(sizeof(xmlRegParserCtxt));
689 if (ret == NULL)
690 return(NULL);
691 memset(ret, 0, sizeof(xmlRegParserCtxt));
692 if (string != NULL)
693 ret->string = xmlStrdup(string);
694 ret->cur = ret->string;
695 ret->neg = 0;
Daniel Veillard6e65e152005-08-09 11:09:52 +0000696 ret->negs = 0;
Daniel Veillard4255d502002-04-16 15:50:10 +0000697 ret->error = 0;
Daniel Veillarde19fc232002-04-22 16:01:24 +0000698 ret->determinist = -1;
Daniel Veillard4255d502002-04-16 15:50:10 +0000699 return(ret);
700}
701
702/**
703 * xmlRegNewRange:
704 * @ctxt: the regexp parser context
705 * @neg: is that negative
706 * @type: the type of range
707 * @start: the start codepoint
708 * @end: the end codepoint
709 *
710 * Allocate a new regexp range
711 *
712 * Returns the new range or NULL in case of error
713 */
714static xmlRegRangePtr
715xmlRegNewRange(xmlRegParserCtxtPtr ctxt,
716 int neg, xmlRegAtomType type, int start, int end) {
717 xmlRegRangePtr ret;
718
719 ret = (xmlRegRangePtr) xmlMalloc(sizeof(xmlRegRange));
720 if (ret == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +0000721 xmlRegexpErrMemory(ctxt, "allocating range");
Daniel Veillard4255d502002-04-16 15:50:10 +0000722 return(NULL);
723 }
724 ret->neg = neg;
725 ret->type = type;
726 ret->start = start;
727 ret->end = end;
728 return(ret);
729}
730
731/**
732 * xmlRegFreeRange:
733 * @range: the regexp range
734 *
735 * Free a regexp range
736 */
737static void
738xmlRegFreeRange(xmlRegRangePtr range) {
739 if (range == NULL)
740 return;
741
742 if (range->blockName != NULL)
743 xmlFree(range->blockName);
744 xmlFree(range);
745}
746
747/**
Daniel Veillard76d59b62007-08-22 16:29:21 +0000748 * xmlRegCopyRange:
749 * @range: the regexp range
750 *
751 * Copy a regexp range
752 *
753 * Returns the new copy or NULL in case of error.
754 */
755static xmlRegRangePtr
756xmlRegCopyRange(xmlRegParserCtxtPtr ctxt, xmlRegRangePtr range) {
757 xmlRegRangePtr ret;
758
759 if (range == NULL)
760 return(NULL);
761
762 ret = xmlRegNewRange(ctxt, range->neg, range->type, range->start,
763 range->end);
764 if (ret == NULL)
765 return(NULL);
766 if (range->blockName != NULL) {
767 ret->blockName = xmlStrdup(range->blockName);
768 if (ret->blockName == NULL) {
769 xmlRegexpErrMemory(ctxt, "allocating range");
770 xmlRegFreeRange(ret);
771 return(NULL);
772 }
773 }
774 return(ret);
775}
776
777/**
Daniel Veillard4255d502002-04-16 15:50:10 +0000778 * xmlRegNewAtom:
779 * @ctxt: the regexp parser context
780 * @type: the type of atom
781 *
Daniel Veillard76d59b62007-08-22 16:29:21 +0000782 * Allocate a new atom
Daniel Veillard4255d502002-04-16 15:50:10 +0000783 *
784 * Returns the new atom or NULL in case of error
785 */
786static xmlRegAtomPtr
787xmlRegNewAtom(xmlRegParserCtxtPtr ctxt, xmlRegAtomType type) {
788 xmlRegAtomPtr ret;
789
790 ret = (xmlRegAtomPtr) xmlMalloc(sizeof(xmlRegAtom));
791 if (ret == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +0000792 xmlRegexpErrMemory(ctxt, "allocating atom");
Daniel Veillard4255d502002-04-16 15:50:10 +0000793 return(NULL);
794 }
795 memset(ret, 0, sizeof(xmlRegAtom));
796 ret->type = type;
797 ret->quant = XML_REGEXP_QUANT_ONCE;
798 ret->min = 0;
799 ret->max = 0;
800 return(ret);
801}
802
803/**
804 * xmlRegFreeAtom:
805 * @atom: the regexp atom
806 *
807 * Free a regexp atom
808 */
809static void
810xmlRegFreeAtom(xmlRegAtomPtr atom) {
811 int i;
812
813 if (atom == NULL)
814 return;
815
816 for (i = 0;i < atom->nbRanges;i++)
817 xmlRegFreeRange(atom->ranges[i]);
818 if (atom->ranges != NULL)
819 xmlFree(atom->ranges);
Daniel Veillardde0e4982005-07-03 14:35:44 +0000820 if ((atom->type == XML_REGEXP_STRING) && (atom->valuep != NULL))
821 xmlFree(atom->valuep);
Daniel Veillard77005e62005-07-19 16:26:18 +0000822 if ((atom->type == XML_REGEXP_STRING) && (atom->valuep2 != NULL))
823 xmlFree(atom->valuep2);
Daniel Veillardde0e4982005-07-03 14:35:44 +0000824 if ((atom->type == XML_REGEXP_BLOCK_NAME) && (atom->valuep != NULL))
Daniel Veillard4255d502002-04-16 15:50:10 +0000825 xmlFree(atom->valuep);
826 xmlFree(atom);
827}
828
Daniel Veillard76d59b62007-08-22 16:29:21 +0000829/**
830 * xmlRegCopyAtom:
831 * @ctxt: the regexp parser context
832 * @atom: the oiginal atom
833 *
834 * Allocate a new regexp range
835 *
836 * Returns the new atom or NULL in case of error
837 */
838static xmlRegAtomPtr
839xmlRegCopyAtom(xmlRegParserCtxtPtr ctxt, xmlRegAtomPtr atom) {
840 xmlRegAtomPtr ret;
841
842 ret = (xmlRegAtomPtr) xmlMalloc(sizeof(xmlRegAtom));
843 if (ret == NULL) {
844 xmlRegexpErrMemory(ctxt, "copying atom");
845 return(NULL);
846 }
847 memset(ret, 0, sizeof(xmlRegAtom));
848 ret->type = atom->type;
849 ret->quant = atom->quant;
850 ret->min = atom->min;
851 ret->max = atom->max;
852 if (atom->nbRanges > 0) {
853 int i;
854
855 ret->ranges = (xmlRegRangePtr *) xmlMalloc(sizeof(xmlRegRangePtr) *
856 atom->nbRanges);
857 if (ret->ranges == NULL) {
858 xmlRegexpErrMemory(ctxt, "copying atom");
859 goto error;
860 }
861 for (i = 0;i < atom->nbRanges;i++) {
862 ret->ranges[i] = xmlRegCopyRange(ctxt, atom->ranges[i]);
863 if (ret->ranges[i] == NULL)
864 goto error;
865 ret->nbRanges = i + 1;
866 }
867 }
868 return(ret);
869
870error:
871 xmlRegFreeAtom(ret);
872 return(NULL);
873}
874
Daniel Veillard4255d502002-04-16 15:50:10 +0000875static xmlRegStatePtr
876xmlRegNewState(xmlRegParserCtxtPtr ctxt) {
877 xmlRegStatePtr ret;
878
879 ret = (xmlRegStatePtr) xmlMalloc(sizeof(xmlRegState));
880 if (ret == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +0000881 xmlRegexpErrMemory(ctxt, "allocating state");
Daniel Veillard4255d502002-04-16 15:50:10 +0000882 return(NULL);
883 }
884 memset(ret, 0, sizeof(xmlRegState));
885 ret->type = XML_REGEXP_TRANS_STATE;
886 ret->mark = XML_REGEXP_MARK_NORMAL;
887 return(ret);
888}
889
890/**
891 * xmlRegFreeState:
892 * @state: the regexp state
893 *
894 * Free a regexp state
895 */
896static void
897xmlRegFreeState(xmlRegStatePtr state) {
898 if (state == NULL)
899 return;
900
901 if (state->trans != NULL)
902 xmlFree(state->trans);
Daniel Veillarddb68b742005-07-30 13:18:24 +0000903 if (state->transTo != NULL)
904 xmlFree(state->transTo);
Daniel Veillard4255d502002-04-16 15:50:10 +0000905 xmlFree(state);
906}
907
908/**
909 * xmlRegFreeParserCtxt:
910 * @ctxt: the regexp parser context
911 *
912 * Free a regexp parser context
913 */
914static void
915xmlRegFreeParserCtxt(xmlRegParserCtxtPtr ctxt) {
916 int i;
917 if (ctxt == NULL)
918 return;
919
920 if (ctxt->string != NULL)
921 xmlFree(ctxt->string);
922 if (ctxt->states != NULL) {
923 for (i = 0;i < ctxt->nbStates;i++)
924 xmlRegFreeState(ctxt->states[i]);
925 xmlFree(ctxt->states);
926 }
927 if (ctxt->atoms != NULL) {
928 for (i = 0;i < ctxt->nbAtoms;i++)
929 xmlRegFreeAtom(ctxt->atoms[i]);
930 xmlFree(ctxt->atoms);
931 }
932 if (ctxt->counters != NULL)
933 xmlFree(ctxt->counters);
934 xmlFree(ctxt);
935}
936
937/************************************************************************
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800938 * *
939 * Display of Data structures *
940 * *
Daniel Veillard4255d502002-04-16 15:50:10 +0000941 ************************************************************************/
942
943static void
944xmlRegPrintAtomType(FILE *output, xmlRegAtomType type) {
945 switch (type) {
946 case XML_REGEXP_EPSILON:
947 fprintf(output, "epsilon "); break;
948 case XML_REGEXP_CHARVAL:
949 fprintf(output, "charval "); break;
950 case XML_REGEXP_RANGES:
951 fprintf(output, "ranges "); break;
952 case XML_REGEXP_SUBREG:
953 fprintf(output, "subexpr "); break;
954 case XML_REGEXP_STRING:
955 fprintf(output, "string "); break;
956 case XML_REGEXP_ANYCHAR:
957 fprintf(output, "anychar "); break;
958 case XML_REGEXP_ANYSPACE:
959 fprintf(output, "anyspace "); break;
960 case XML_REGEXP_NOTSPACE:
961 fprintf(output, "notspace "); break;
962 case XML_REGEXP_INITNAME:
963 fprintf(output, "initname "); break;
964 case XML_REGEXP_NOTINITNAME:
965 fprintf(output, "notinitname "); break;
966 case XML_REGEXP_NAMECHAR:
967 fprintf(output, "namechar "); break;
968 case XML_REGEXP_NOTNAMECHAR:
969 fprintf(output, "notnamechar "); break;
970 case XML_REGEXP_DECIMAL:
971 fprintf(output, "decimal "); break;
972 case XML_REGEXP_NOTDECIMAL:
973 fprintf(output, "notdecimal "); break;
974 case XML_REGEXP_REALCHAR:
975 fprintf(output, "realchar "); break;
976 case XML_REGEXP_NOTREALCHAR:
977 fprintf(output, "notrealchar "); break;
978 case XML_REGEXP_LETTER:
979 fprintf(output, "LETTER "); break;
980 case XML_REGEXP_LETTER_UPPERCASE:
981 fprintf(output, "LETTER_UPPERCASE "); break;
982 case XML_REGEXP_LETTER_LOWERCASE:
983 fprintf(output, "LETTER_LOWERCASE "); break;
984 case XML_REGEXP_LETTER_TITLECASE:
985 fprintf(output, "LETTER_TITLECASE "); break;
986 case XML_REGEXP_LETTER_MODIFIER:
987 fprintf(output, "LETTER_MODIFIER "); break;
988 case XML_REGEXP_LETTER_OTHERS:
989 fprintf(output, "LETTER_OTHERS "); break;
990 case XML_REGEXP_MARK:
991 fprintf(output, "MARK "); break;
992 case XML_REGEXP_MARK_NONSPACING:
993 fprintf(output, "MARK_NONSPACING "); break;
994 case XML_REGEXP_MARK_SPACECOMBINING:
995 fprintf(output, "MARK_SPACECOMBINING "); break;
996 case XML_REGEXP_MARK_ENCLOSING:
997 fprintf(output, "MARK_ENCLOSING "); break;
998 case XML_REGEXP_NUMBER:
999 fprintf(output, "NUMBER "); break;
1000 case XML_REGEXP_NUMBER_DECIMAL:
1001 fprintf(output, "NUMBER_DECIMAL "); break;
1002 case XML_REGEXP_NUMBER_LETTER:
1003 fprintf(output, "NUMBER_LETTER "); break;
1004 case XML_REGEXP_NUMBER_OTHERS:
1005 fprintf(output, "NUMBER_OTHERS "); break;
1006 case XML_REGEXP_PUNCT:
1007 fprintf(output, "PUNCT "); break;
1008 case XML_REGEXP_PUNCT_CONNECTOR:
1009 fprintf(output, "PUNCT_CONNECTOR "); break;
1010 case XML_REGEXP_PUNCT_DASH:
1011 fprintf(output, "PUNCT_DASH "); break;
1012 case XML_REGEXP_PUNCT_OPEN:
1013 fprintf(output, "PUNCT_OPEN "); break;
1014 case XML_REGEXP_PUNCT_CLOSE:
1015 fprintf(output, "PUNCT_CLOSE "); break;
1016 case XML_REGEXP_PUNCT_INITQUOTE:
1017 fprintf(output, "PUNCT_INITQUOTE "); break;
1018 case XML_REGEXP_PUNCT_FINQUOTE:
1019 fprintf(output, "PUNCT_FINQUOTE "); break;
1020 case XML_REGEXP_PUNCT_OTHERS:
1021 fprintf(output, "PUNCT_OTHERS "); break;
1022 case XML_REGEXP_SEPAR:
1023 fprintf(output, "SEPAR "); break;
1024 case XML_REGEXP_SEPAR_SPACE:
1025 fprintf(output, "SEPAR_SPACE "); break;
1026 case XML_REGEXP_SEPAR_LINE:
1027 fprintf(output, "SEPAR_LINE "); break;
1028 case XML_REGEXP_SEPAR_PARA:
1029 fprintf(output, "SEPAR_PARA "); break;
1030 case XML_REGEXP_SYMBOL:
1031 fprintf(output, "SYMBOL "); break;
1032 case XML_REGEXP_SYMBOL_MATH:
1033 fprintf(output, "SYMBOL_MATH "); break;
1034 case XML_REGEXP_SYMBOL_CURRENCY:
1035 fprintf(output, "SYMBOL_CURRENCY "); break;
1036 case XML_REGEXP_SYMBOL_MODIFIER:
1037 fprintf(output, "SYMBOL_MODIFIER "); break;
1038 case XML_REGEXP_SYMBOL_OTHERS:
1039 fprintf(output, "SYMBOL_OTHERS "); break;
1040 case XML_REGEXP_OTHER:
1041 fprintf(output, "OTHER "); break;
1042 case XML_REGEXP_OTHER_CONTROL:
1043 fprintf(output, "OTHER_CONTROL "); break;
1044 case XML_REGEXP_OTHER_FORMAT:
1045 fprintf(output, "OTHER_FORMAT "); break;
1046 case XML_REGEXP_OTHER_PRIVATE:
1047 fprintf(output, "OTHER_PRIVATE "); break;
1048 case XML_REGEXP_OTHER_NA:
1049 fprintf(output, "OTHER_NA "); break;
1050 case XML_REGEXP_BLOCK_NAME:
1051 fprintf(output, "BLOCK "); break;
1052 }
1053}
1054
1055static void
1056xmlRegPrintQuantType(FILE *output, xmlRegQuantType type) {
1057 switch (type) {
1058 case XML_REGEXP_QUANT_EPSILON:
1059 fprintf(output, "epsilon "); break;
1060 case XML_REGEXP_QUANT_ONCE:
1061 fprintf(output, "once "); break;
1062 case XML_REGEXP_QUANT_OPT:
1063 fprintf(output, "? "); break;
1064 case XML_REGEXP_QUANT_MULT:
1065 fprintf(output, "* "); break;
1066 case XML_REGEXP_QUANT_PLUS:
1067 fprintf(output, "+ "); break;
1068 case XML_REGEXP_QUANT_RANGE:
1069 fprintf(output, "range "); break;
Daniel Veillard7646b182002-04-20 06:41:40 +00001070 case XML_REGEXP_QUANT_ONCEONLY:
1071 fprintf(output, "onceonly "); break;
1072 case XML_REGEXP_QUANT_ALL:
1073 fprintf(output, "all "); break;
Daniel Veillard4255d502002-04-16 15:50:10 +00001074 }
1075}
1076static void
1077xmlRegPrintRange(FILE *output, xmlRegRangePtr range) {
1078 fprintf(output, " range: ");
1079 if (range->neg)
1080 fprintf(output, "negative ");
1081 xmlRegPrintAtomType(output, range->type);
1082 fprintf(output, "%c - %c\n", range->start, range->end);
1083}
1084
1085static void
1086xmlRegPrintAtom(FILE *output, xmlRegAtomPtr atom) {
1087 fprintf(output, " atom: ");
1088 if (atom == NULL) {
1089 fprintf(output, "NULL\n");
1090 return;
1091 }
Daniel Veillard9efc4762005-07-19 14:33:55 +00001092 if (atom->neg)
1093 fprintf(output, "not ");
Daniel Veillard4255d502002-04-16 15:50:10 +00001094 xmlRegPrintAtomType(output, atom->type);
1095 xmlRegPrintQuantType(output, atom->quant);
1096 if (atom->quant == XML_REGEXP_QUANT_RANGE)
1097 fprintf(output, "%d-%d ", atom->min, atom->max);
1098 if (atom->type == XML_REGEXP_STRING)
1099 fprintf(output, "'%s' ", (char *) atom->valuep);
1100 if (atom->type == XML_REGEXP_CHARVAL)
1101 fprintf(output, "char %c\n", atom->codepoint);
1102 else if (atom->type == XML_REGEXP_RANGES) {
1103 int i;
1104 fprintf(output, "%d entries\n", atom->nbRanges);
1105 for (i = 0; i < atom->nbRanges;i++)
1106 xmlRegPrintRange(output, atom->ranges[i]);
1107 } else if (atom->type == XML_REGEXP_SUBREG) {
1108 fprintf(output, "start %d end %d\n", atom->start->no, atom->stop->no);
1109 } else {
1110 fprintf(output, "\n");
1111 }
1112}
1113
1114static void
1115xmlRegPrintTrans(FILE *output, xmlRegTransPtr trans) {
1116 fprintf(output, " trans: ");
1117 if (trans == NULL) {
1118 fprintf(output, "NULL\n");
1119 return;
1120 }
1121 if (trans->to < 0) {
1122 fprintf(output, "removed\n");
1123 return;
1124 }
Daniel Veillard567a45b2005-10-18 19:11:55 +00001125 if (trans->nd != 0) {
1126 if (trans->nd == 2)
1127 fprintf(output, "last not determinist, ");
1128 else
1129 fprintf(output, "not determinist, ");
1130 }
Daniel Veillard4255d502002-04-16 15:50:10 +00001131 if (trans->counter >= 0) {
1132 fprintf(output, "counted %d, ", trans->counter);
1133 }
Daniel Veillard8a001f62002-04-20 07:24:11 +00001134 if (trans->count == REGEXP_ALL_COUNTER) {
1135 fprintf(output, "all transition, ");
1136 } else if (trans->count >= 0) {
Daniel Veillard4255d502002-04-16 15:50:10 +00001137 fprintf(output, "count based %d, ", trans->count);
1138 }
1139 if (trans->atom == NULL) {
1140 fprintf(output, "epsilon to %d\n", trans->to);
1141 return;
1142 }
1143 if (trans->atom->type == XML_REGEXP_CHARVAL)
1144 fprintf(output, "char %c ", trans->atom->codepoint);
1145 fprintf(output, "atom %d, to %d\n", trans->atom->no, trans->to);
1146}
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001147
Daniel Veillard4255d502002-04-16 15:50:10 +00001148static void
1149xmlRegPrintState(FILE *output, xmlRegStatePtr state) {
1150 int i;
1151
1152 fprintf(output, " state: ");
1153 if (state == NULL) {
1154 fprintf(output, "NULL\n");
1155 return;
1156 }
1157 if (state->type == XML_REGEXP_START_STATE)
1158 fprintf(output, "START ");
1159 if (state->type == XML_REGEXP_FINAL_STATE)
1160 fprintf(output, "FINAL ");
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001161
Daniel Veillard4255d502002-04-16 15:50:10 +00001162 fprintf(output, "%d, %d transitions:\n", state->no, state->nbTrans);
1163 for (i = 0;i < state->nbTrans; i++) {
1164 xmlRegPrintTrans(output, &(state->trans[i]));
1165 }
1166}
1167
Daniel Veillard23e73572002-09-19 19:56:43 +00001168#ifdef DEBUG_REGEXP_GRAPH
Daniel Veillard4255d502002-04-16 15:50:10 +00001169static void
1170xmlRegPrintCtxt(FILE *output, xmlRegParserCtxtPtr ctxt) {
1171 int i;
1172
1173 fprintf(output, " ctxt: ");
1174 if (ctxt == NULL) {
1175 fprintf(output, "NULL\n");
1176 return;
1177 }
1178 fprintf(output, "'%s' ", ctxt->string);
1179 if (ctxt->error)
1180 fprintf(output, "error ");
1181 if (ctxt->neg)
1182 fprintf(output, "neg ");
1183 fprintf(output, "\n");
1184 fprintf(output, "%d atoms:\n", ctxt->nbAtoms);
1185 for (i = 0;i < ctxt->nbAtoms; i++) {
1186 fprintf(output, " %02d ", i);
1187 xmlRegPrintAtom(output, ctxt->atoms[i]);
1188 }
1189 if (ctxt->atom != NULL) {
1190 fprintf(output, "current atom:\n");
1191 xmlRegPrintAtom(output, ctxt->atom);
1192 }
1193 fprintf(output, "%d states:", ctxt->nbStates);
1194 if (ctxt->start != NULL)
1195 fprintf(output, " start: %d", ctxt->start->no);
1196 if (ctxt->end != NULL)
1197 fprintf(output, " end: %d", ctxt->end->no);
1198 fprintf(output, "\n");
1199 for (i = 0;i < ctxt->nbStates; i++) {
1200 xmlRegPrintState(output, ctxt->states[i]);
1201 }
1202 fprintf(output, "%d counters:\n", ctxt->nbCounters);
1203 for (i = 0;i < ctxt->nbCounters; i++) {
1204 fprintf(output, " %d: min %d max %d\n", i, ctxt->counters[i].min,
1205 ctxt->counters[i].max);
1206 }
1207}
Daniel Veillard23e73572002-09-19 19:56:43 +00001208#endif
Daniel Veillard4255d502002-04-16 15:50:10 +00001209
1210/************************************************************************
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001211 * *
Daniel Veillard4255d502002-04-16 15:50:10 +00001212 * Finite Automata structures manipulations *
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001213 * *
Daniel Veillard4255d502002-04-16 15:50:10 +00001214 ************************************************************************/
1215
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001216static void
Daniel Veillard4255d502002-04-16 15:50:10 +00001217xmlRegAtomAddRange(xmlRegParserCtxtPtr ctxt, xmlRegAtomPtr atom,
1218 int neg, xmlRegAtomType type, int start, int end,
1219 xmlChar *blockName) {
1220 xmlRegRangePtr range;
1221
1222 if (atom == NULL) {
1223 ERROR("add range: atom is NULL");
1224 return;
1225 }
1226 if (atom->type != XML_REGEXP_RANGES) {
1227 ERROR("add range: atom is not ranges");
1228 return;
1229 }
1230 if (atom->maxRanges == 0) {
1231 atom->maxRanges = 4;
1232 atom->ranges = (xmlRegRangePtr *) xmlMalloc(atom->maxRanges *
1233 sizeof(xmlRegRangePtr));
1234 if (atom->ranges == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00001235 xmlRegexpErrMemory(ctxt, "adding ranges");
Daniel Veillard4255d502002-04-16 15:50:10 +00001236 atom->maxRanges = 0;
1237 return;
1238 }
1239 } else if (atom->nbRanges >= atom->maxRanges) {
1240 xmlRegRangePtr *tmp;
1241 atom->maxRanges *= 2;
1242 tmp = (xmlRegRangePtr *) xmlRealloc(atom->ranges, atom->maxRanges *
1243 sizeof(xmlRegRangePtr));
1244 if (tmp == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00001245 xmlRegexpErrMemory(ctxt, "adding ranges");
Daniel Veillard4255d502002-04-16 15:50:10 +00001246 atom->maxRanges /= 2;
1247 return;
1248 }
1249 atom->ranges = tmp;
1250 }
1251 range = xmlRegNewRange(ctxt, neg, type, start, end);
1252 if (range == NULL)
1253 return;
1254 range->blockName = blockName;
1255 atom->ranges[atom->nbRanges++] = range;
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001256
Daniel Veillard4255d502002-04-16 15:50:10 +00001257}
1258
1259static int
1260xmlRegGetCounter(xmlRegParserCtxtPtr ctxt) {
1261 if (ctxt->maxCounters == 0) {
1262 ctxt->maxCounters = 4;
1263 ctxt->counters = (xmlRegCounter *) xmlMalloc(ctxt->maxCounters *
1264 sizeof(xmlRegCounter));
1265 if (ctxt->counters == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00001266 xmlRegexpErrMemory(ctxt, "allocating counter");
Daniel Veillard4255d502002-04-16 15:50:10 +00001267 ctxt->maxCounters = 0;
1268 return(-1);
1269 }
1270 } else if (ctxt->nbCounters >= ctxt->maxCounters) {
1271 xmlRegCounter *tmp;
1272 ctxt->maxCounters *= 2;
1273 tmp = (xmlRegCounter *) xmlRealloc(ctxt->counters, ctxt->maxCounters *
1274 sizeof(xmlRegCounter));
1275 if (tmp == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00001276 xmlRegexpErrMemory(ctxt, "allocating counter");
Daniel Veillard4255d502002-04-16 15:50:10 +00001277 ctxt->maxCounters /= 2;
1278 return(-1);
1279 }
1280 ctxt->counters = tmp;
1281 }
1282 ctxt->counters[ctxt->nbCounters].min = -1;
1283 ctxt->counters[ctxt->nbCounters].max = -1;
1284 return(ctxt->nbCounters++);
1285}
1286
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001287static int
Daniel Veillard4255d502002-04-16 15:50:10 +00001288xmlRegAtomPush(xmlRegParserCtxtPtr ctxt, xmlRegAtomPtr atom) {
1289 if (atom == NULL) {
1290 ERROR("atom push: atom is NULL");
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001291 return(-1);
Daniel Veillard4255d502002-04-16 15:50:10 +00001292 }
1293 if (ctxt->maxAtoms == 0) {
1294 ctxt->maxAtoms = 4;
1295 ctxt->atoms = (xmlRegAtomPtr *) xmlMalloc(ctxt->maxAtoms *
1296 sizeof(xmlRegAtomPtr));
1297 if (ctxt->atoms == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00001298 xmlRegexpErrMemory(ctxt, "pushing atom");
Daniel Veillard4255d502002-04-16 15:50:10 +00001299 ctxt->maxAtoms = 0;
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001300 return(-1);
Daniel Veillard4255d502002-04-16 15:50:10 +00001301 }
1302 } else if (ctxt->nbAtoms >= ctxt->maxAtoms) {
1303 xmlRegAtomPtr *tmp;
1304 ctxt->maxAtoms *= 2;
1305 tmp = (xmlRegAtomPtr *) xmlRealloc(ctxt->atoms, ctxt->maxAtoms *
1306 sizeof(xmlRegAtomPtr));
1307 if (tmp == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00001308 xmlRegexpErrMemory(ctxt, "allocating counter");
Daniel Veillard4255d502002-04-16 15:50:10 +00001309 ctxt->maxAtoms /= 2;
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001310 return(-1);
Daniel Veillard4255d502002-04-16 15:50:10 +00001311 }
1312 ctxt->atoms = tmp;
1313 }
1314 atom->no = ctxt->nbAtoms;
1315 ctxt->atoms[ctxt->nbAtoms++] = atom;
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001316 return(0);
Daniel Veillard4255d502002-04-16 15:50:10 +00001317}
1318
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001319static void
Daniel Veillarddb68b742005-07-30 13:18:24 +00001320xmlRegStateAddTransTo(xmlRegParserCtxtPtr ctxt, xmlRegStatePtr target,
1321 int from) {
1322 if (target->maxTransTo == 0) {
1323 target->maxTransTo = 8;
1324 target->transTo = (int *) xmlMalloc(target->maxTransTo *
1325 sizeof(int));
1326 if (target->transTo == NULL) {
1327 xmlRegexpErrMemory(ctxt, "adding transition");
1328 target->maxTransTo = 0;
1329 return;
1330 }
1331 } else if (target->nbTransTo >= target->maxTransTo) {
1332 int *tmp;
1333 target->maxTransTo *= 2;
1334 tmp = (int *) xmlRealloc(target->transTo, target->maxTransTo *
1335 sizeof(int));
1336 if (tmp == NULL) {
1337 xmlRegexpErrMemory(ctxt, "adding transition");
1338 target->maxTransTo /= 2;
1339 return;
1340 }
1341 target->transTo = tmp;
1342 }
1343 target->transTo[target->nbTransTo] = from;
1344 target->nbTransTo++;
1345}
1346
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001347static void
Daniel Veillard4255d502002-04-16 15:50:10 +00001348xmlRegStateAddTrans(xmlRegParserCtxtPtr ctxt, xmlRegStatePtr state,
1349 xmlRegAtomPtr atom, xmlRegStatePtr target,
Daniel Veillard5de09382005-09-26 17:18:17 +00001350 int counter, int count) {
William M. Brackf9b5fa22004-05-10 07:52:15 +00001351
1352 int nrtrans;
1353
Daniel Veillard4255d502002-04-16 15:50:10 +00001354 if (state == NULL) {
1355 ERROR("add state: state is NULL");
1356 return;
1357 }
1358 if (target == NULL) {
1359 ERROR("add state: target is NULL");
1360 return;
1361 }
William M. Brackf9b5fa22004-05-10 07:52:15 +00001362 /*
1363 * Other routines follow the philosophy 'When in doubt, add a transition'
1364 * so we check here whether such a transition is already present and, if
1365 * so, silently ignore this request.
1366 */
1367
Daniel Veillard5de09382005-09-26 17:18:17 +00001368 for (nrtrans = state->nbTrans - 1; nrtrans >= 0; nrtrans--) {
1369 xmlRegTransPtr trans = &(state->trans[nrtrans]);
1370 if ((trans->atom == atom) &&
1371 (trans->to == target->no) &&
1372 (trans->counter == counter) &&
1373 (trans->count == count)) {
William M. Brackf9b5fa22004-05-10 07:52:15 +00001374#ifdef DEBUG_REGEXP_GRAPH
Daniel Veillard5de09382005-09-26 17:18:17 +00001375 printf("Ignoring duplicate transition from %d to %d\n",
1376 state->no, target->no);
William M. Brackf9b5fa22004-05-10 07:52:15 +00001377#endif
Daniel Veillard5de09382005-09-26 17:18:17 +00001378 return;
Daniel Veillarddb68b742005-07-30 13:18:24 +00001379 }
William M. Brackf9b5fa22004-05-10 07:52:15 +00001380 }
1381
Daniel Veillard4255d502002-04-16 15:50:10 +00001382 if (state->maxTrans == 0) {
Daniel Veillarddb68b742005-07-30 13:18:24 +00001383 state->maxTrans = 8;
Daniel Veillard4255d502002-04-16 15:50:10 +00001384 state->trans = (xmlRegTrans *) xmlMalloc(state->maxTrans *
1385 sizeof(xmlRegTrans));
1386 if (state->trans == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00001387 xmlRegexpErrMemory(ctxt, "adding transition");
Daniel Veillard4255d502002-04-16 15:50:10 +00001388 state->maxTrans = 0;
1389 return;
1390 }
1391 } else if (state->nbTrans >= state->maxTrans) {
1392 xmlRegTrans *tmp;
1393 state->maxTrans *= 2;
1394 tmp = (xmlRegTrans *) xmlRealloc(state->trans, state->maxTrans *
1395 sizeof(xmlRegTrans));
1396 if (tmp == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00001397 xmlRegexpErrMemory(ctxt, "adding transition");
Daniel Veillard4255d502002-04-16 15:50:10 +00001398 state->maxTrans /= 2;
1399 return;
1400 }
1401 state->trans = tmp;
1402 }
1403#ifdef DEBUG_REGEXP_GRAPH
1404 printf("Add trans from %d to %d ", state->no, target->no);
Daniel Veillard8a001f62002-04-20 07:24:11 +00001405 if (count == REGEXP_ALL_COUNTER)
Daniel Veillard2cbf5962004-03-31 15:50:43 +00001406 printf("all transition\n");
Daniel Veillard4402ab42002-09-12 16:02:56 +00001407 else if (count >= 0)
Daniel Veillard2cbf5962004-03-31 15:50:43 +00001408 printf("count based %d\n", count);
Daniel Veillard4255d502002-04-16 15:50:10 +00001409 else if (counter >= 0)
Daniel Veillard2cbf5962004-03-31 15:50:43 +00001410 printf("counted %d\n", counter);
Daniel Veillard4255d502002-04-16 15:50:10 +00001411 else if (atom == NULL)
Daniel Veillard2cbf5962004-03-31 15:50:43 +00001412 printf("epsilon transition\n");
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001413 else if (atom != NULL)
Daniel Veillard2cbf5962004-03-31 15:50:43 +00001414 xmlRegPrintAtom(stdout, atom);
Daniel Veillard4255d502002-04-16 15:50:10 +00001415#endif
1416
1417 state->trans[state->nbTrans].atom = atom;
1418 state->trans[state->nbTrans].to = target->no;
1419 state->trans[state->nbTrans].counter = counter;
1420 state->trans[state->nbTrans].count = count;
Daniel Veillard567a45b2005-10-18 19:11:55 +00001421 state->trans[state->nbTrans].nd = 0;
Daniel Veillard4255d502002-04-16 15:50:10 +00001422 state->nbTrans++;
Daniel Veillarddb68b742005-07-30 13:18:24 +00001423 xmlRegStateAddTransTo(ctxt, target, state->no);
Daniel Veillard4255d502002-04-16 15:50:10 +00001424}
1425
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001426static int
Daniel Veillard4255d502002-04-16 15:50:10 +00001427xmlRegStatePush(xmlRegParserCtxtPtr ctxt, xmlRegStatePtr state) {
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001428 if (state == NULL) return(-1);
Daniel Veillard4255d502002-04-16 15:50:10 +00001429 if (ctxt->maxStates == 0) {
1430 ctxt->maxStates = 4;
1431 ctxt->states = (xmlRegStatePtr *) xmlMalloc(ctxt->maxStates *
1432 sizeof(xmlRegStatePtr));
1433 if (ctxt->states == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00001434 xmlRegexpErrMemory(ctxt, "adding state");
Daniel Veillard4255d502002-04-16 15:50:10 +00001435 ctxt->maxStates = 0;
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001436 return(-1);
Daniel Veillard4255d502002-04-16 15:50:10 +00001437 }
1438 } else if (ctxt->nbStates >= ctxt->maxStates) {
1439 xmlRegStatePtr *tmp;
1440 ctxt->maxStates *= 2;
1441 tmp = (xmlRegStatePtr *) xmlRealloc(ctxt->states, ctxt->maxStates *
1442 sizeof(xmlRegStatePtr));
1443 if (tmp == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00001444 xmlRegexpErrMemory(ctxt, "adding state");
Daniel Veillard4255d502002-04-16 15:50:10 +00001445 ctxt->maxStates /= 2;
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001446 return(-1);
Daniel Veillard4255d502002-04-16 15:50:10 +00001447 }
1448 ctxt->states = tmp;
1449 }
1450 state->no = ctxt->nbStates;
1451 ctxt->states[ctxt->nbStates++] = state;
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001452 return(0);
Daniel Veillard4255d502002-04-16 15:50:10 +00001453}
1454
1455/**
Daniel Veillard7646b182002-04-20 06:41:40 +00001456 * xmlFAGenerateAllTransition:
Daniel Veillard441bc322002-04-20 17:38:48 +00001457 * @ctxt: a regexp parser context
1458 * @from: the from state
1459 * @to: the target state or NULL for building a new one
1460 * @lax:
Daniel Veillard7646b182002-04-20 06:41:40 +00001461 *
1462 */
1463static void
1464xmlFAGenerateAllTransition(xmlRegParserCtxtPtr ctxt,
Daniel Veillard441bc322002-04-20 17:38:48 +00001465 xmlRegStatePtr from, xmlRegStatePtr to,
1466 int lax) {
Daniel Veillard7646b182002-04-20 06:41:40 +00001467 if (to == NULL) {
1468 to = xmlRegNewState(ctxt);
1469 xmlRegStatePush(ctxt, to);
1470 ctxt->state = to;
1471 }
Daniel Veillard441bc322002-04-20 17:38:48 +00001472 if (lax)
Daniel Veillard5de09382005-09-26 17:18:17 +00001473 xmlRegStateAddTrans(ctxt, from, NULL, to, -1, REGEXP_ALL_LAX_COUNTER);
Daniel Veillard441bc322002-04-20 17:38:48 +00001474 else
Daniel Veillard5de09382005-09-26 17:18:17 +00001475 xmlRegStateAddTrans(ctxt, from, NULL, to, -1, REGEXP_ALL_COUNTER);
Daniel Veillard7646b182002-04-20 06:41:40 +00001476}
1477
1478/**
Daniel Veillard4255d502002-04-16 15:50:10 +00001479 * xmlFAGenerateEpsilonTransition:
Daniel Veillard441bc322002-04-20 17:38:48 +00001480 * @ctxt: a regexp parser context
1481 * @from: the from state
1482 * @to: the target state or NULL for building a new one
Daniel Veillard4255d502002-04-16 15:50:10 +00001483 *
1484 */
1485static void
1486xmlFAGenerateEpsilonTransition(xmlRegParserCtxtPtr ctxt,
1487 xmlRegStatePtr from, xmlRegStatePtr to) {
1488 if (to == NULL) {
1489 to = xmlRegNewState(ctxt);
1490 xmlRegStatePush(ctxt, to);
1491 ctxt->state = to;
1492 }
Daniel Veillard5de09382005-09-26 17:18:17 +00001493 xmlRegStateAddTrans(ctxt, from, NULL, to, -1, -1);
Daniel Veillard4255d502002-04-16 15:50:10 +00001494}
1495
1496/**
1497 * xmlFAGenerateCountedEpsilonTransition:
Daniel Veillard441bc322002-04-20 17:38:48 +00001498 * @ctxt: a regexp parser context
1499 * @from: the from state
1500 * @to: the target state or NULL for building a new one
Daniel Veillard4255d502002-04-16 15:50:10 +00001501 * counter: the counter for that transition
1502 *
1503 */
1504static void
1505xmlFAGenerateCountedEpsilonTransition(xmlRegParserCtxtPtr ctxt,
1506 xmlRegStatePtr from, xmlRegStatePtr to, int counter) {
1507 if (to == NULL) {
1508 to = xmlRegNewState(ctxt);
1509 xmlRegStatePush(ctxt, to);
1510 ctxt->state = to;
1511 }
Daniel Veillard5de09382005-09-26 17:18:17 +00001512 xmlRegStateAddTrans(ctxt, from, NULL, to, counter, -1);
Daniel Veillard4255d502002-04-16 15:50:10 +00001513}
1514
1515/**
1516 * xmlFAGenerateCountedTransition:
Daniel Veillard441bc322002-04-20 17:38:48 +00001517 * @ctxt: a regexp parser context
1518 * @from: the from state
1519 * @to: the target state or NULL for building a new one
Daniel Veillard4255d502002-04-16 15:50:10 +00001520 * counter: the counter for that transition
1521 *
1522 */
1523static void
1524xmlFAGenerateCountedTransition(xmlRegParserCtxtPtr ctxt,
1525 xmlRegStatePtr from, xmlRegStatePtr to, int counter) {
1526 if (to == NULL) {
1527 to = xmlRegNewState(ctxt);
1528 xmlRegStatePush(ctxt, to);
1529 ctxt->state = to;
1530 }
Daniel Veillard5de09382005-09-26 17:18:17 +00001531 xmlRegStateAddTrans(ctxt, from, NULL, to, -1, counter);
Daniel Veillard4255d502002-04-16 15:50:10 +00001532}
1533
1534/**
1535 * xmlFAGenerateTransitions:
Daniel Veillard441bc322002-04-20 17:38:48 +00001536 * @ctxt: a regexp parser context
1537 * @from: the from state
1538 * @to: the target state or NULL for building a new one
1539 * @atom: the atom generating the transition
Daniel Veillard4255d502002-04-16 15:50:10 +00001540 *
William M. Brackddf71d62004-05-06 04:17:26 +00001541 * Returns 0 if success and -1 in case of error.
Daniel Veillard4255d502002-04-16 15:50:10 +00001542 */
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001543static int
Daniel Veillard4255d502002-04-16 15:50:10 +00001544xmlFAGenerateTransitions(xmlRegParserCtxtPtr ctxt, xmlRegStatePtr from,
1545 xmlRegStatePtr to, xmlRegAtomPtr atom) {
Daniel Veillard10bda622008-03-13 07:27:24 +00001546 xmlRegStatePtr end;
1547
Daniel Veillard4255d502002-04-16 15:50:10 +00001548 if (atom == NULL) {
1549 ERROR("genrate transition: atom == NULL");
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001550 return(-1);
Daniel Veillard4255d502002-04-16 15:50:10 +00001551 }
1552 if (atom->type == XML_REGEXP_SUBREG) {
1553 /*
1554 * this is a subexpression handling one should not need to
William M. Brackddf71d62004-05-06 04:17:26 +00001555 * create a new node except for XML_REGEXP_QUANT_RANGE.
Daniel Veillard4255d502002-04-16 15:50:10 +00001556 */
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001557 if (xmlRegAtomPush(ctxt, atom) < 0) {
1558 return(-1);
1559 }
Daniel Veillard4255d502002-04-16 15:50:10 +00001560 if ((to != NULL) && (atom->stop != to) &&
1561 (atom->quant != XML_REGEXP_QUANT_RANGE)) {
1562 /*
1563 * Generate an epsilon transition to link to the target
1564 */
1565 xmlFAGenerateEpsilonTransition(ctxt, atom->stop, to);
Daniel Veillardaa622012005-10-20 15:55:25 +00001566#ifdef DV
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001567 } else if ((to == NULL) && (atom->quant != XML_REGEXP_QUANT_RANGE) &&
Daniel Veillardaa622012005-10-20 15:55:25 +00001568 (atom->quant != XML_REGEXP_QUANT_ONCE)) {
1569 to = xmlRegNewState(ctxt);
1570 xmlRegStatePush(ctxt, to);
1571 ctxt->state = to;
1572 xmlFAGenerateEpsilonTransition(ctxt, atom->stop, to);
1573#endif
Daniel Veillard4255d502002-04-16 15:50:10 +00001574 }
1575 switch (atom->quant) {
1576 case XML_REGEXP_QUANT_OPT:
1577 atom->quant = XML_REGEXP_QUANT_ONCE;
Daniel Veillard54eb0242006-03-21 23:17:57 +00001578 /*
1579 * transition done to the state after end of atom.
1580 * 1. set transition from atom start to new state
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001581 * 2. set transition from atom end to this state.
Daniel Veillard54eb0242006-03-21 23:17:57 +00001582 */
Daniel Veillardd80d0722009-08-22 18:56:01 +02001583 if (to == NULL) {
1584 xmlFAGenerateEpsilonTransition(ctxt, atom->start, 0);
1585 xmlFAGenerateEpsilonTransition(ctxt, atom->stop,
1586 ctxt->state);
1587 } else {
1588 xmlFAGenerateEpsilonTransition(ctxt, atom->start, to);
1589 }
Daniel Veillard4255d502002-04-16 15:50:10 +00001590 break;
1591 case XML_REGEXP_QUANT_MULT:
1592 atom->quant = XML_REGEXP_QUANT_ONCE;
1593 xmlFAGenerateEpsilonTransition(ctxt, atom->start, atom->stop);
1594 xmlFAGenerateEpsilonTransition(ctxt, atom->stop, atom->start);
1595 break;
1596 case XML_REGEXP_QUANT_PLUS:
1597 atom->quant = XML_REGEXP_QUANT_ONCE;
1598 xmlFAGenerateEpsilonTransition(ctxt, atom->stop, atom->start);
1599 break;
1600 case XML_REGEXP_QUANT_RANGE: {
1601 int counter;
Daniel Veillard76d59b62007-08-22 16:29:21 +00001602 xmlRegStatePtr inter, newstate;
Daniel Veillard4255d502002-04-16 15:50:10 +00001603
1604 /*
Daniel Veillard76d59b62007-08-22 16:29:21 +00001605 * create the final state now if needed
Daniel Veillard4255d502002-04-16 15:50:10 +00001606 */
Daniel Veillard4255d502002-04-16 15:50:10 +00001607 if (to != NULL) {
1608 newstate = to;
1609 } else {
1610 newstate = xmlRegNewState(ctxt);
1611 xmlRegStatePush(ctxt, newstate);
Daniel Veillard4255d502002-04-16 15:50:10 +00001612 }
Daniel Veillard76d59b62007-08-22 16:29:21 +00001613
1614 /*
1615 * The principle here is to use counted transition
1616 * to avoid explosion in the number of states in the
1617 * graph. This is clearly more complex but should not
1618 * be exploitable at runtime.
Daniel Veillard54eb0242006-03-21 23:17:57 +00001619 */
Daniel Veillard76d59b62007-08-22 16:29:21 +00001620 if ((atom->min == 0) && (atom->start0 == NULL)) {
1621 xmlRegAtomPtr copy;
1622 /*
1623 * duplicate a transition based on atom to count next
1624 * occurences after 1. We cannot loop to atom->start
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001625 * directly because we need an epsilon transition to
Daniel Veillard76d59b62007-08-22 16:29:21 +00001626 * newstate.
1627 */
1628 /* ???? For some reason it seems we never reach that
1629 case, I suppose this got optimized out before when
1630 building the automata */
Daniel Veillardc821e032007-08-28 17:33:45 +00001631 copy = xmlRegCopyAtom(ctxt, atom);
Daniel Veillard76d59b62007-08-22 16:29:21 +00001632 if (copy == NULL)
1633 return(-1);
Daniel Veillard76d59b62007-08-22 16:29:21 +00001634 copy->quant = XML_REGEXP_QUANT_ONCE;
1635 copy->min = 0;
1636 copy->max = 0;
1637
1638 if (xmlFAGenerateTransitions(ctxt, atom->start, NULL, copy)
1639 < 0)
1640 return(-1);
1641 inter = ctxt->state;
1642 counter = xmlRegGetCounter(ctxt);
1643 ctxt->counters[counter].min = atom->min - 1;
1644 ctxt->counters[counter].max = atom->max - 1;
1645 /* count the number of times we see it again */
1646 xmlFAGenerateCountedEpsilonTransition(ctxt, inter,
1647 atom->stop, counter);
1648 /* allow a way out based on the count */
1649 xmlFAGenerateCountedTransition(ctxt, inter,
1650 newstate, counter);
1651 /* and also allow a direct exit for 0 */
1652 xmlFAGenerateEpsilonTransition(ctxt, atom->start,
1653 newstate);
1654 } else {
1655 /*
1656 * either we need the atom at least once or there
1657 * is an atom->start0 allowing to easilly plug the
1658 * epsilon transition.
1659 */
1660 counter = xmlRegGetCounter(ctxt);
1661 ctxt->counters[counter].min = atom->min - 1;
1662 ctxt->counters[counter].max = atom->max - 1;
1663 /* count the number of times we see it again */
1664 xmlFAGenerateCountedEpsilonTransition(ctxt, atom->stop,
1665 atom->start, counter);
1666 /* allow a way out based on the count */
1667 xmlFAGenerateCountedTransition(ctxt, atom->stop,
1668 newstate, counter);
1669 /* and if needed allow a direct exit for 0 */
1670 if (atom->min == 0)
1671 xmlFAGenerateEpsilonTransition(ctxt, atom->start0,
1672 newstate);
1673
1674 }
1675 atom->min = 0;
1676 atom->max = 0;
1677 atom->quant = XML_REGEXP_QUANT_ONCE;
1678 ctxt->state = newstate;
Daniel Veillard4255d502002-04-16 15:50:10 +00001679 }
1680 default:
1681 break;
1682 }
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001683 return(0);
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001684 }
Daniel Veillard567a45b2005-10-18 19:11:55 +00001685 if ((atom->min == 0) && (atom->max == 0) &&
Daniel Veillard99c394d2005-07-14 12:58:49 +00001686 (atom->quant == XML_REGEXP_QUANT_RANGE)) {
1687 /*
1688 * we can discard the atom and generate an epsilon transition instead
1689 */
1690 if (to == NULL) {
1691 to = xmlRegNewState(ctxt);
1692 if (to != NULL)
1693 xmlRegStatePush(ctxt, to);
1694 else {
1695 return(-1);
1696 }
1697 }
1698 xmlFAGenerateEpsilonTransition(ctxt, from, to);
1699 ctxt->state = to;
1700 xmlRegFreeAtom(atom);
1701 return(0);
Daniel Veillard567a45b2005-10-18 19:11:55 +00001702 }
1703 if (to == NULL) {
1704 to = xmlRegNewState(ctxt);
1705 if (to != NULL)
1706 xmlRegStatePush(ctxt, to);
1707 else {
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001708 return(-1);
Daniel Veillard4255d502002-04-16 15:50:10 +00001709 }
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001710 }
Daniel Veillard10bda622008-03-13 07:27:24 +00001711 end = to;
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001712 if ((atom->quant == XML_REGEXP_QUANT_MULT) ||
Daniel Veillard10bda622008-03-13 07:27:24 +00001713 (atom->quant == XML_REGEXP_QUANT_PLUS)) {
1714 /*
1715 * Do not pollute the target state by adding transitions from
1716 * it as it is likely to be the shared target of multiple branches.
1717 * So isolate with an epsilon transition.
1718 */
1719 xmlRegStatePtr tmp;
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001720
Daniel Veillard10bda622008-03-13 07:27:24 +00001721 tmp = xmlRegNewState(ctxt);
1722 if (tmp != NULL)
1723 xmlRegStatePush(ctxt, tmp);
1724 else {
1725 return(-1);
1726 }
1727 xmlFAGenerateEpsilonTransition(ctxt, tmp, to);
1728 to = tmp;
Daniel Veillard4255d502002-04-16 15:50:10 +00001729 }
Daniel Veillard567a45b2005-10-18 19:11:55 +00001730 if (xmlRegAtomPush(ctxt, atom) < 0) {
1731 return(-1);
1732 }
1733 xmlRegStateAddTrans(ctxt, from, atom, to, -1, -1);
Daniel Veillard10bda622008-03-13 07:27:24 +00001734 ctxt->state = end;
Daniel Veillard4255d502002-04-16 15:50:10 +00001735 switch (atom->quant) {
1736 case XML_REGEXP_QUANT_OPT:
1737 atom->quant = XML_REGEXP_QUANT_ONCE;
1738 xmlFAGenerateEpsilonTransition(ctxt, from, to);
1739 break;
1740 case XML_REGEXP_QUANT_MULT:
1741 atom->quant = XML_REGEXP_QUANT_ONCE;
1742 xmlFAGenerateEpsilonTransition(ctxt, from, to);
Daniel Veillard5de09382005-09-26 17:18:17 +00001743 xmlRegStateAddTrans(ctxt, to, atom, to, -1, -1);
Daniel Veillard4255d502002-04-16 15:50:10 +00001744 break;
1745 case XML_REGEXP_QUANT_PLUS:
1746 atom->quant = XML_REGEXP_QUANT_ONCE;
Daniel Veillard5de09382005-09-26 17:18:17 +00001747 xmlRegStateAddTrans(ctxt, to, atom, to, -1, -1);
Daniel Veillard4255d502002-04-16 15:50:10 +00001748 break;
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001749 case XML_REGEXP_QUANT_RANGE:
Daniel Veillardc821e032007-08-28 17:33:45 +00001750#if DV_test
William M. Brack56578372007-04-11 14:33:46 +00001751 if (atom->min == 0) {
1752 xmlFAGenerateEpsilonTransition(ctxt, from, to);
1753 }
Daniel Veillardc821e032007-08-28 17:33:45 +00001754#endif
William M. Brack56578372007-04-11 14:33:46 +00001755 break;
Daniel Veillard4255d502002-04-16 15:50:10 +00001756 default:
1757 break;
1758 }
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001759 return(0);
Daniel Veillard4255d502002-04-16 15:50:10 +00001760}
1761
1762/**
1763 * xmlFAReduceEpsilonTransitions:
Daniel Veillard441bc322002-04-20 17:38:48 +00001764 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00001765 * @fromnr: the from state
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001766 * @tonr: the to state
William M. Brackddf71d62004-05-06 04:17:26 +00001767 * @counter: should that transition be associated to a counted
Daniel Veillard4255d502002-04-16 15:50:10 +00001768 *
1769 */
1770static void
1771xmlFAReduceEpsilonTransitions(xmlRegParserCtxtPtr ctxt, int fromnr,
1772 int tonr, int counter) {
1773 int transnr;
1774 xmlRegStatePtr from;
1775 xmlRegStatePtr to;
1776
1777#ifdef DEBUG_REGEXP_GRAPH
1778 printf("xmlFAReduceEpsilonTransitions(%d, %d)\n", fromnr, tonr);
1779#endif
1780 from = ctxt->states[fromnr];
1781 if (from == NULL)
1782 return;
1783 to = ctxt->states[tonr];
1784 if (to == NULL)
1785 return;
1786 if ((to->mark == XML_REGEXP_MARK_START) ||
1787 (to->mark == XML_REGEXP_MARK_VISITED))
1788 return;
1789
1790 to->mark = XML_REGEXP_MARK_VISITED;
1791 if (to->type == XML_REGEXP_FINAL_STATE) {
1792#ifdef DEBUG_REGEXP_GRAPH
1793 printf("State %d is final, so %d becomes final\n", tonr, fromnr);
1794#endif
1795 from->type = XML_REGEXP_FINAL_STATE;
1796 }
1797 for (transnr = 0;transnr < to->nbTrans;transnr++) {
Daniel Veillarddb68b742005-07-30 13:18:24 +00001798 if (to->trans[transnr].to < 0)
1799 continue;
Daniel Veillard4255d502002-04-16 15:50:10 +00001800 if (to->trans[transnr].atom == NULL) {
1801 /*
1802 * Don't remove counted transitions
1803 * Don't loop either
1804 */
Daniel Veillardb509f152002-04-17 16:28:10 +00001805 if (to->trans[transnr].to != fromnr) {
1806 if (to->trans[transnr].count >= 0) {
1807 int newto = to->trans[transnr].to;
1808
1809 xmlRegStateAddTrans(ctxt, from, NULL,
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001810 ctxt->states[newto],
Daniel Veillard5de09382005-09-26 17:18:17 +00001811 -1, to->trans[transnr].count);
Daniel Veillardb509f152002-04-17 16:28:10 +00001812 } else {
Daniel Veillard4255d502002-04-16 15:50:10 +00001813#ifdef DEBUG_REGEXP_GRAPH
Daniel Veillardb509f152002-04-17 16:28:10 +00001814 printf("Found epsilon trans %d from %d to %d\n",
1815 transnr, tonr, to->trans[transnr].to);
Daniel Veillard4255d502002-04-16 15:50:10 +00001816#endif
Daniel Veillardb509f152002-04-17 16:28:10 +00001817 if (to->trans[transnr].counter >= 0) {
1818 xmlFAReduceEpsilonTransitions(ctxt, fromnr,
1819 to->trans[transnr].to,
1820 to->trans[transnr].counter);
1821 } else {
1822 xmlFAReduceEpsilonTransitions(ctxt, fromnr,
1823 to->trans[transnr].to,
1824 counter);
1825 }
1826 }
Daniel Veillard4255d502002-04-16 15:50:10 +00001827 }
1828 } else {
1829 int newto = to->trans[transnr].to;
1830
Daniel Veillardb509f152002-04-17 16:28:10 +00001831 if (to->trans[transnr].counter >= 0) {
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001832 xmlRegStateAddTrans(ctxt, from, to->trans[transnr].atom,
1833 ctxt->states[newto],
Daniel Veillard5de09382005-09-26 17:18:17 +00001834 to->trans[transnr].counter, -1);
Daniel Veillardb509f152002-04-17 16:28:10 +00001835 } else {
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001836 xmlRegStateAddTrans(ctxt, from, to->trans[transnr].atom,
Daniel Veillard5de09382005-09-26 17:18:17 +00001837 ctxt->states[newto], counter, -1);
Daniel Veillardb509f152002-04-17 16:28:10 +00001838 }
Daniel Veillard4255d502002-04-16 15:50:10 +00001839 }
1840 }
1841 to->mark = XML_REGEXP_MARK_NORMAL;
1842}
1843
1844/**
Daniel Veillarddb68b742005-07-30 13:18:24 +00001845 * xmlFAEliminateSimpleEpsilonTransitions:
1846 * @ctxt: a regexp parser context
1847 *
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001848 * Eliminating general epsilon transitions can get costly in the general
Daniel Veillarddb68b742005-07-30 13:18:24 +00001849 * algorithm due to the large amount of generated new transitions and
1850 * associated comparisons. However for simple epsilon transition used just
1851 * to separate building blocks when generating the automata this can be
1852 * reduced to state elimination:
1853 * - if there exists an epsilon from X to Y
1854 * - if there is no other transition from X
1855 * then X and Y are semantically equivalent and X can be eliminated
1856 * If X is the start state then make Y the start state, else replace the
1857 * target of all transitions to X by transitions to Y.
1858 */
1859static void
1860xmlFAEliminateSimpleEpsilonTransitions(xmlRegParserCtxtPtr ctxt) {
1861 int statenr, i, j, newto;
1862 xmlRegStatePtr state, tmp;
1863
1864 for (statenr = 0;statenr < ctxt->nbStates;statenr++) {
1865 state = ctxt->states[statenr];
1866 if (state == NULL)
1867 continue;
1868 if (state->nbTrans != 1)
1869 continue;
Daniel Veillard0e05f4c2006-11-01 15:33:04 +00001870 if (state->type == XML_REGEXP_UNREACH_STATE)
1871 continue;
Daniel Veillarddb68b742005-07-30 13:18:24 +00001872 /* is the only transition out a basic transition */
1873 if ((state->trans[0].atom == NULL) &&
1874 (state->trans[0].to >= 0) &&
1875 (state->trans[0].to != statenr) &&
1876 (state->trans[0].counter < 0) &&
1877 (state->trans[0].count < 0)) {
1878 newto = state->trans[0].to;
1879
1880 if (state->type == XML_REGEXP_START_STATE) {
1881#ifdef DEBUG_REGEXP_GRAPH
1882 printf("Found simple epsilon trans from start %d to %d\n",
1883 statenr, newto);
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001884#endif
Daniel Veillarddb68b742005-07-30 13:18:24 +00001885 } else {
1886#ifdef DEBUG_REGEXP_GRAPH
1887 printf("Found simple epsilon trans from %d to %d\n",
1888 statenr, newto);
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001889#endif
Daniel Veillarddb68b742005-07-30 13:18:24 +00001890 for (i = 0;i < state->nbTransTo;i++) {
1891 tmp = ctxt->states[state->transTo[i]];
1892 for (j = 0;j < tmp->nbTrans;j++) {
1893 if (tmp->trans[j].to == statenr) {
Daniel Veillarddb68b742005-07-30 13:18:24 +00001894#ifdef DEBUG_REGEXP_GRAPH
1895 printf("Changed transition %d on %d to go to %d\n",
1896 j, tmp->no, newto);
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001897#endif
Daniel Veillard0e05f4c2006-11-01 15:33:04 +00001898 tmp->trans[j].to = -1;
1899 xmlRegStateAddTrans(ctxt, tmp, tmp->trans[j].atom,
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001900 ctxt->states[newto],
Daniel Veillard0e05f4c2006-11-01 15:33:04 +00001901 tmp->trans[j].counter,
1902 tmp->trans[j].count);
Daniel Veillarddb68b742005-07-30 13:18:24 +00001903 }
1904 }
1905 }
Daniel Veillarddb68b742005-07-30 13:18:24 +00001906 if (state->type == XML_REGEXP_FINAL_STATE)
1907 ctxt->states[newto]->type = XML_REGEXP_FINAL_STATE;
1908 /* eliminate the transition completely */
1909 state->nbTrans = 0;
1910
Daniel Veillard0e05f4c2006-11-01 15:33:04 +00001911 state->type = XML_REGEXP_UNREACH_STATE;
Daniel Veillarddb68b742005-07-30 13:18:24 +00001912
1913 }
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001914
Daniel Veillarddb68b742005-07-30 13:18:24 +00001915 }
1916 }
1917}
1918/**
Daniel Veillard4255d502002-04-16 15:50:10 +00001919 * xmlFAEliminateEpsilonTransitions:
Daniel Veillard441bc322002-04-20 17:38:48 +00001920 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00001921 *
1922 */
1923static void
1924xmlFAEliminateEpsilonTransitions(xmlRegParserCtxtPtr ctxt) {
1925 int statenr, transnr;
1926 xmlRegStatePtr state;
Daniel Veillarddb68b742005-07-30 13:18:24 +00001927 int has_epsilon;
Daniel Veillard4255d502002-04-16 15:50:10 +00001928
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001929 if (ctxt->states == NULL) return;
1930
Daniel Veillard0e05f4c2006-11-01 15:33:04 +00001931 /*
1932 * Eliminate simple epsilon transition and the associated unreachable
1933 * states.
1934 */
Daniel Veillarddb68b742005-07-30 13:18:24 +00001935 xmlFAEliminateSimpleEpsilonTransitions(ctxt);
Daniel Veillard0e05f4c2006-11-01 15:33:04 +00001936 for (statenr = 0;statenr < ctxt->nbStates;statenr++) {
1937 state = ctxt->states[statenr];
1938 if ((state != NULL) && (state->type == XML_REGEXP_UNREACH_STATE)) {
1939#ifdef DEBUG_REGEXP_GRAPH
1940 printf("Removed unreachable state %d\n", statenr);
1941#endif
1942 xmlRegFreeState(state);
1943 ctxt->states[statenr] = NULL;
1944 }
1945 }
Daniel Veillarddb68b742005-07-30 13:18:24 +00001946
1947 has_epsilon = 0;
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001948
Daniel Veillard4255d502002-04-16 15:50:10 +00001949 /*
Daniel Veillardfcd18ff2006-11-02 10:28:04 +00001950 * Build the completed transitions bypassing the epsilons
Daniel Veillard4255d502002-04-16 15:50:10 +00001951 * Use a marking algorithm to avoid loops
Daniel Veillardfcd18ff2006-11-02 10:28:04 +00001952 * Mark sink states too.
1953 * Process from the latests states backward to the start when
1954 * there is long cascading epsilon chains this minimize the
1955 * recursions and transition compares when adding the new ones
Daniel Veillard4255d502002-04-16 15:50:10 +00001956 */
Daniel Veillardfcd18ff2006-11-02 10:28:04 +00001957 for (statenr = ctxt->nbStates - 1;statenr >= 0;statenr--) {
Daniel Veillard4255d502002-04-16 15:50:10 +00001958 state = ctxt->states[statenr];
1959 if (state == NULL)
1960 continue;
Daniel Veillardcc026dc2005-01-12 13:21:17 +00001961 if ((state->nbTrans == 0) &&
1962 (state->type != XML_REGEXP_FINAL_STATE)) {
1963 state->type = XML_REGEXP_SINK_STATE;
1964 }
Daniel Veillard4255d502002-04-16 15:50:10 +00001965 for (transnr = 0;transnr < state->nbTrans;transnr++) {
1966 if ((state->trans[transnr].atom == NULL) &&
1967 (state->trans[transnr].to >= 0)) {
1968 if (state->trans[transnr].to == statenr) {
1969 state->trans[transnr].to = -1;
1970#ifdef DEBUG_REGEXP_GRAPH
1971 printf("Removed loopback epsilon trans %d on %d\n",
1972 transnr, statenr);
1973#endif
1974 } else if (state->trans[transnr].count < 0) {
1975 int newto = state->trans[transnr].to;
1976
1977#ifdef DEBUG_REGEXP_GRAPH
1978 printf("Found epsilon trans %d from %d to %d\n",
1979 transnr, statenr, newto);
1980#endif
Daniel Veillarddb68b742005-07-30 13:18:24 +00001981 has_epsilon = 1;
Daniel Veillard0e05f4c2006-11-01 15:33:04 +00001982 state->trans[transnr].to = -2;
1983 state->mark = XML_REGEXP_MARK_START;
Daniel Veillard4255d502002-04-16 15:50:10 +00001984 xmlFAReduceEpsilonTransitions(ctxt, statenr,
1985 newto, state->trans[transnr].counter);
1986 state->mark = XML_REGEXP_MARK_NORMAL;
1987#ifdef DEBUG_REGEXP_GRAPH
1988 } else {
1989 printf("Found counted transition %d on %d\n",
1990 transnr, statenr);
1991#endif
1992 }
1993 }
1994 }
1995 }
1996 /*
1997 * Eliminate the epsilon transitions
1998 */
Daniel Veillarddb68b742005-07-30 13:18:24 +00001999 if (has_epsilon) {
2000 for (statenr = 0;statenr < ctxt->nbStates;statenr++) {
2001 state = ctxt->states[statenr];
2002 if (state == NULL)
2003 continue;
2004 for (transnr = 0;transnr < state->nbTrans;transnr++) {
2005 xmlRegTransPtr trans = &(state->trans[transnr]);
2006 if ((trans->atom == NULL) &&
2007 (trans->count < 0) &&
2008 (trans->to >= 0)) {
2009 trans->to = -1;
2010 }
Daniel Veillard4255d502002-04-16 15:50:10 +00002011 }
2012 }
2013 }
Daniel Veillard23e73572002-09-19 19:56:43 +00002014
2015 /*
2016 * Use this pass to detect unreachable states too
2017 */
2018 for (statenr = 0;statenr < ctxt->nbStates;statenr++) {
2019 state = ctxt->states[statenr];
2020 if (state != NULL)
William M. Brack779af002003-08-01 15:55:39 +00002021 state->reached = XML_REGEXP_MARK_NORMAL;
Daniel Veillard23e73572002-09-19 19:56:43 +00002022 }
2023 state = ctxt->states[0];
2024 if (state != NULL)
William M. Brack779af002003-08-01 15:55:39 +00002025 state->reached = XML_REGEXP_MARK_START;
Daniel Veillard23e73572002-09-19 19:56:43 +00002026 while (state != NULL) {
2027 xmlRegStatePtr target = NULL;
William M. Brack779af002003-08-01 15:55:39 +00002028 state->reached = XML_REGEXP_MARK_VISITED;
Daniel Veillard23e73572002-09-19 19:56:43 +00002029 /*
William M. Brackddf71d62004-05-06 04:17:26 +00002030 * Mark all states reachable from the current reachable state
Daniel Veillard23e73572002-09-19 19:56:43 +00002031 */
2032 for (transnr = 0;transnr < state->nbTrans;transnr++) {
2033 if ((state->trans[transnr].to >= 0) &&
2034 ((state->trans[transnr].atom != NULL) ||
2035 (state->trans[transnr].count >= 0))) {
2036 int newto = state->trans[transnr].to;
2037
2038 if (ctxt->states[newto] == NULL)
2039 continue;
William M. Brack779af002003-08-01 15:55:39 +00002040 if (ctxt->states[newto]->reached == XML_REGEXP_MARK_NORMAL) {
2041 ctxt->states[newto]->reached = XML_REGEXP_MARK_START;
Daniel Veillard23e73572002-09-19 19:56:43 +00002042 target = ctxt->states[newto];
2043 }
2044 }
2045 }
Daniel Veillardcc026dc2005-01-12 13:21:17 +00002046
Daniel Veillard23e73572002-09-19 19:56:43 +00002047 /*
2048 * find the next accessible state not explored
2049 */
2050 if (target == NULL) {
2051 for (statenr = 1;statenr < ctxt->nbStates;statenr++) {
2052 state = ctxt->states[statenr];
William M. Brack779af002003-08-01 15:55:39 +00002053 if ((state != NULL) && (state->reached ==
2054 XML_REGEXP_MARK_START)) {
Daniel Veillard23e73572002-09-19 19:56:43 +00002055 target = state;
2056 break;
2057 }
2058 }
2059 }
2060 state = target;
2061 }
2062 for (statenr = 0;statenr < ctxt->nbStates;statenr++) {
2063 state = ctxt->states[statenr];
William M. Brack779af002003-08-01 15:55:39 +00002064 if ((state != NULL) && (state->reached == XML_REGEXP_MARK_NORMAL)) {
Daniel Veillard23e73572002-09-19 19:56:43 +00002065#ifdef DEBUG_REGEXP_GRAPH
2066 printf("Removed unreachable state %d\n", statenr);
2067#endif
2068 xmlRegFreeState(state);
2069 ctxt->states[statenr] = NULL;
2070 }
2071 }
2072
Daniel Veillard4255d502002-04-16 15:50:10 +00002073}
2074
Daniel Veillard567a45b2005-10-18 19:11:55 +00002075static int
2076xmlFACompareRanges(xmlRegRangePtr range1, xmlRegRangePtr range2) {
2077 int ret = 0;
2078
2079 if ((range1->type == XML_REGEXP_RANGES) ||
2080 (range2->type == XML_REGEXP_RANGES) ||
2081 (range2->type == XML_REGEXP_SUBREG) ||
2082 (range1->type == XML_REGEXP_SUBREG) ||
2083 (range1->type == XML_REGEXP_STRING) ||
2084 (range2->type == XML_REGEXP_STRING))
2085 return(-1);
2086
2087 /* put them in order */
2088 if (range1->type > range2->type) {
2089 xmlRegRangePtr tmp;
2090
2091 tmp = range1;
2092 range1 = range2;
2093 range2 = tmp;
2094 }
2095 if ((range1->type == XML_REGEXP_ANYCHAR) ||
2096 (range2->type == XML_REGEXP_ANYCHAR)) {
2097 ret = 1;
2098 } else if ((range1->type == XML_REGEXP_EPSILON) ||
2099 (range2->type == XML_REGEXP_EPSILON)) {
2100 return(0);
2101 } else if (range1->type == range2->type) {
Daniel Veillard9332b482009-09-23 18:28:43 +02002102 if (range1->type != XML_REGEXP_CHARVAL)
2103 ret = 1;
2104 else if ((range1->end < range2->start) ||
2105 (range2->end < range1->start))
Daniel Veillard567a45b2005-10-18 19:11:55 +00002106 ret = 0;
Daniel Veillard9332b482009-09-23 18:28:43 +02002107 else
2108 ret = 1;
Daniel Veillard567a45b2005-10-18 19:11:55 +00002109 } else if (range1->type == XML_REGEXP_CHARVAL) {
2110 int codepoint;
2111 int neg = 0;
2112
2113 /*
2114 * just check all codepoints in the range for acceptance,
2115 * this is usually way cheaper since done only once at
Daniel Veillardf8e3db02012-09-11 13:26:36 +08002116 * compilation than testing over and over at runtime or
Daniel Veillard567a45b2005-10-18 19:11:55 +00002117 * pushing too many states when evaluating.
2118 */
2119 if (((range1->neg == 0) && (range2->neg != 0)) ||
2120 ((range1->neg != 0) && (range2->neg == 0)))
2121 neg = 1;
2122
2123 for (codepoint = range1->start;codepoint <= range1->end ;codepoint++) {
2124 ret = xmlRegCheckCharacterRange(range2->type, codepoint,
2125 0, range2->start, range2->end,
2126 range2->blockName);
2127 if (ret < 0)
2128 return(-1);
2129 if (((neg == 1) && (ret == 0)) ||
2130 ((neg == 0) && (ret == 1)))
2131 return(1);
2132 }
2133 return(0);
2134 } else if ((range1->type == XML_REGEXP_BLOCK_NAME) ||
2135 (range2->type == XML_REGEXP_BLOCK_NAME)) {
2136 if (range1->type == range2->type) {
2137 ret = xmlStrEqual(range1->blockName, range2->blockName);
2138 } else {
2139 /*
2140 * comparing a block range with anything else is way
2141 * too costly, and maintining the table is like too much
2142 * memory too, so let's force the automata to save state
2143 * here.
2144 */
2145 return(1);
2146 }
2147 } else if ((range1->type < XML_REGEXP_LETTER) ||
2148 (range2->type < XML_REGEXP_LETTER)) {
2149 if ((range1->type == XML_REGEXP_ANYSPACE) &&
2150 (range2->type == XML_REGEXP_NOTSPACE))
2151 ret = 0;
2152 else if ((range1->type == XML_REGEXP_INITNAME) &&
2153 (range2->type == XML_REGEXP_NOTINITNAME))
2154 ret = 0;
2155 else if ((range1->type == XML_REGEXP_NAMECHAR) &&
2156 (range2->type == XML_REGEXP_NOTNAMECHAR))
2157 ret = 0;
2158 else if ((range1->type == XML_REGEXP_DECIMAL) &&
2159 (range2->type == XML_REGEXP_NOTDECIMAL))
2160 ret = 0;
2161 else if ((range1->type == XML_REGEXP_REALCHAR) &&
2162 (range2->type == XML_REGEXP_NOTREALCHAR))
2163 ret = 0;
2164 else {
2165 /* same thing to limit complexity */
2166 return(1);
2167 }
2168 } else {
2169 ret = 0;
2170 /* range1->type < range2->type here */
2171 switch (range1->type) {
2172 case XML_REGEXP_LETTER:
2173 /* all disjoint except in the subgroups */
2174 if ((range2->type == XML_REGEXP_LETTER_UPPERCASE) ||
2175 (range2->type == XML_REGEXP_LETTER_LOWERCASE) ||
2176 (range2->type == XML_REGEXP_LETTER_TITLECASE) ||
2177 (range2->type == XML_REGEXP_LETTER_MODIFIER) ||
2178 (range2->type == XML_REGEXP_LETTER_OTHERS))
2179 ret = 1;
2180 break;
2181 case XML_REGEXP_MARK:
2182 if ((range2->type == XML_REGEXP_MARK_NONSPACING) ||
2183 (range2->type == XML_REGEXP_MARK_SPACECOMBINING) ||
2184 (range2->type == XML_REGEXP_MARK_ENCLOSING))
2185 ret = 1;
2186 break;
2187 case XML_REGEXP_NUMBER:
2188 if ((range2->type == XML_REGEXP_NUMBER_DECIMAL) ||
2189 (range2->type == XML_REGEXP_NUMBER_LETTER) ||
2190 (range2->type == XML_REGEXP_NUMBER_OTHERS))
2191 ret = 1;
2192 break;
2193 case XML_REGEXP_PUNCT:
2194 if ((range2->type == XML_REGEXP_PUNCT_CONNECTOR) ||
2195 (range2->type == XML_REGEXP_PUNCT_DASH) ||
2196 (range2->type == XML_REGEXP_PUNCT_OPEN) ||
2197 (range2->type == XML_REGEXP_PUNCT_CLOSE) ||
2198 (range2->type == XML_REGEXP_PUNCT_INITQUOTE) ||
2199 (range2->type == XML_REGEXP_PUNCT_FINQUOTE) ||
2200 (range2->type == XML_REGEXP_PUNCT_OTHERS))
2201 ret = 1;
2202 break;
2203 case XML_REGEXP_SEPAR:
2204 if ((range2->type == XML_REGEXP_SEPAR_SPACE) ||
2205 (range2->type == XML_REGEXP_SEPAR_LINE) ||
2206 (range2->type == XML_REGEXP_SEPAR_PARA))
2207 ret = 1;
2208 break;
2209 case XML_REGEXP_SYMBOL:
2210 if ((range2->type == XML_REGEXP_SYMBOL_MATH) ||
2211 (range2->type == XML_REGEXP_SYMBOL_CURRENCY) ||
2212 (range2->type == XML_REGEXP_SYMBOL_MODIFIER) ||
2213 (range2->type == XML_REGEXP_SYMBOL_OTHERS))
2214 ret = 1;
2215 break;
2216 case XML_REGEXP_OTHER:
2217 if ((range2->type == XML_REGEXP_OTHER_CONTROL) ||
2218 (range2->type == XML_REGEXP_OTHER_FORMAT) ||
2219 (range2->type == XML_REGEXP_OTHER_PRIVATE))
2220 ret = 1;
2221 break;
2222 default:
2223 if ((range2->type >= XML_REGEXP_LETTER) &&
2224 (range2->type < XML_REGEXP_BLOCK_NAME))
2225 ret = 0;
2226 else {
2227 /* safety net ! */
2228 return(1);
2229 }
2230 }
2231 }
2232 if (((range1->neg == 0) && (range2->neg != 0)) ||
2233 ((range1->neg != 0) && (range2->neg == 0)))
2234 ret = !ret;
Daniel Veillard594e5df2009-09-07 14:58:47 +02002235 return(ret);
Daniel Veillard567a45b2005-10-18 19:11:55 +00002236}
2237
Daniel Veillarde19fc232002-04-22 16:01:24 +00002238/**
Daniel Veillardfc011b72006-02-12 19:14:15 +00002239 * xmlFACompareAtomTypes:
2240 * @type1: an atom type
2241 * @type2: an atom type
2242 *
2243 * Compares two atoms type to check whether they intersect in some ways,
2244 * this is used by xmlFACompareAtoms only
2245 *
2246 * Returns 1 if they may intersect and 0 otherwise
2247 */
2248static int
2249xmlFACompareAtomTypes(xmlRegAtomType type1, xmlRegAtomType type2) {
2250 if ((type1 == XML_REGEXP_EPSILON) ||
2251 (type1 == XML_REGEXP_CHARVAL) ||
2252 (type1 == XML_REGEXP_RANGES) ||
2253 (type1 == XML_REGEXP_SUBREG) ||
2254 (type1 == XML_REGEXP_STRING) ||
2255 (type1 == XML_REGEXP_ANYCHAR))
2256 return(1);
2257 if ((type2 == XML_REGEXP_EPSILON) ||
2258 (type2 == XML_REGEXP_CHARVAL) ||
2259 (type2 == XML_REGEXP_RANGES) ||
2260 (type2 == XML_REGEXP_SUBREG) ||
2261 (type2 == XML_REGEXP_STRING) ||
2262 (type2 == XML_REGEXP_ANYCHAR))
2263 return(1);
2264
2265 if (type1 == type2) return(1);
2266
2267 /* simplify subsequent compares by making sure type1 < type2 */
2268 if (type1 > type2) {
2269 xmlRegAtomType tmp = type1;
2270 type1 = type2;
2271 type2 = tmp;
2272 }
2273 switch (type1) {
2274 case XML_REGEXP_ANYSPACE: /* \s */
2275 /* can't be a letter, number, mark, pontuation, symbol */
2276 if ((type2 == XML_REGEXP_NOTSPACE) ||
2277 ((type2 >= XML_REGEXP_LETTER) &&
2278 (type2 <= XML_REGEXP_LETTER_OTHERS)) ||
2279 ((type2 >= XML_REGEXP_NUMBER) &&
2280 (type2 <= XML_REGEXP_NUMBER_OTHERS)) ||
2281 ((type2 >= XML_REGEXP_MARK) &&
2282 (type2 <= XML_REGEXP_MARK_ENCLOSING)) ||
2283 ((type2 >= XML_REGEXP_PUNCT) &&
2284 (type2 <= XML_REGEXP_PUNCT_OTHERS)) ||
2285 ((type2 >= XML_REGEXP_SYMBOL) &&
2286 (type2 <= XML_REGEXP_SYMBOL_OTHERS))
2287 ) return(0);
2288 break;
2289 case XML_REGEXP_NOTSPACE: /* \S */
2290 break;
2291 case XML_REGEXP_INITNAME: /* \l */
2292 /* can't be a number, mark, separator, pontuation, symbol or other */
2293 if ((type2 == XML_REGEXP_NOTINITNAME) ||
2294 ((type2 >= XML_REGEXP_NUMBER) &&
2295 (type2 <= XML_REGEXP_NUMBER_OTHERS)) ||
2296 ((type2 >= XML_REGEXP_MARK) &&
2297 (type2 <= XML_REGEXP_MARK_ENCLOSING)) ||
2298 ((type2 >= XML_REGEXP_SEPAR) &&
2299 (type2 <= XML_REGEXP_SEPAR_PARA)) ||
2300 ((type2 >= XML_REGEXP_PUNCT) &&
2301 (type2 <= XML_REGEXP_PUNCT_OTHERS)) ||
2302 ((type2 >= XML_REGEXP_SYMBOL) &&
2303 (type2 <= XML_REGEXP_SYMBOL_OTHERS)) ||
2304 ((type2 >= XML_REGEXP_OTHER) &&
2305 (type2 <= XML_REGEXP_OTHER_NA))
2306 ) return(0);
2307 break;
2308 case XML_REGEXP_NOTINITNAME: /* \L */
2309 break;
2310 case XML_REGEXP_NAMECHAR: /* \c */
2311 /* can't be a mark, separator, pontuation, symbol or other */
2312 if ((type2 == XML_REGEXP_NOTNAMECHAR) ||
2313 ((type2 >= XML_REGEXP_MARK) &&
2314 (type2 <= XML_REGEXP_MARK_ENCLOSING)) ||
2315 ((type2 >= XML_REGEXP_PUNCT) &&
2316 (type2 <= XML_REGEXP_PUNCT_OTHERS)) ||
2317 ((type2 >= XML_REGEXP_SEPAR) &&
2318 (type2 <= XML_REGEXP_SEPAR_PARA)) ||
2319 ((type2 >= XML_REGEXP_SYMBOL) &&
2320 (type2 <= XML_REGEXP_SYMBOL_OTHERS)) ||
2321 ((type2 >= XML_REGEXP_OTHER) &&
2322 (type2 <= XML_REGEXP_OTHER_NA))
2323 ) return(0);
2324 break;
2325 case XML_REGEXP_NOTNAMECHAR: /* \C */
2326 break;
2327 case XML_REGEXP_DECIMAL: /* \d */
2328 /* can't be a letter, mark, separator, pontuation, symbol or other */
2329 if ((type2 == XML_REGEXP_NOTDECIMAL) ||
2330 (type2 == XML_REGEXP_REALCHAR) ||
2331 ((type2 >= XML_REGEXP_LETTER) &&
2332 (type2 <= XML_REGEXP_LETTER_OTHERS)) ||
2333 ((type2 >= XML_REGEXP_MARK) &&
2334 (type2 <= XML_REGEXP_MARK_ENCLOSING)) ||
2335 ((type2 >= XML_REGEXP_PUNCT) &&
2336 (type2 <= XML_REGEXP_PUNCT_OTHERS)) ||
2337 ((type2 >= XML_REGEXP_SEPAR) &&
2338 (type2 <= XML_REGEXP_SEPAR_PARA)) ||
2339 ((type2 >= XML_REGEXP_SYMBOL) &&
2340 (type2 <= XML_REGEXP_SYMBOL_OTHERS)) ||
2341 ((type2 >= XML_REGEXP_OTHER) &&
2342 (type2 <= XML_REGEXP_OTHER_NA))
2343 )return(0);
2344 break;
2345 case XML_REGEXP_NOTDECIMAL: /* \D */
2346 break;
2347 case XML_REGEXP_REALCHAR: /* \w */
2348 /* can't be a mark, separator, pontuation, symbol or other */
2349 if ((type2 == XML_REGEXP_NOTDECIMAL) ||
2350 ((type2 >= XML_REGEXP_MARK) &&
2351 (type2 <= XML_REGEXP_MARK_ENCLOSING)) ||
2352 ((type2 >= XML_REGEXP_PUNCT) &&
2353 (type2 <= XML_REGEXP_PUNCT_OTHERS)) ||
2354 ((type2 >= XML_REGEXP_SEPAR) &&
2355 (type2 <= XML_REGEXP_SEPAR_PARA)) ||
2356 ((type2 >= XML_REGEXP_SYMBOL) &&
2357 (type2 <= XML_REGEXP_SYMBOL_OTHERS)) ||
2358 ((type2 >= XML_REGEXP_OTHER) &&
2359 (type2 <= XML_REGEXP_OTHER_NA))
2360 )return(0);
2361 break;
2362 case XML_REGEXP_NOTREALCHAR: /* \W */
2363 break;
2364 /*
2365 * at that point we know both type 1 and type2 are from
2366 * character categories are ordered and are different,
2367 * it becomes simple because this is a partition
2368 */
2369 case XML_REGEXP_LETTER:
2370 if (type2 <= XML_REGEXP_LETTER_OTHERS)
2371 return(1);
2372 return(0);
2373 case XML_REGEXP_LETTER_UPPERCASE:
2374 case XML_REGEXP_LETTER_LOWERCASE:
2375 case XML_REGEXP_LETTER_TITLECASE:
2376 case XML_REGEXP_LETTER_MODIFIER:
2377 case XML_REGEXP_LETTER_OTHERS:
2378 return(0);
2379 case XML_REGEXP_MARK:
2380 if (type2 <= XML_REGEXP_MARK_ENCLOSING)
2381 return(1);
2382 return(0);
2383 case XML_REGEXP_MARK_NONSPACING:
2384 case XML_REGEXP_MARK_SPACECOMBINING:
2385 case XML_REGEXP_MARK_ENCLOSING:
2386 return(0);
2387 case XML_REGEXP_NUMBER:
2388 if (type2 <= XML_REGEXP_NUMBER_OTHERS)
2389 return(1);
2390 return(0);
2391 case XML_REGEXP_NUMBER_DECIMAL:
2392 case XML_REGEXP_NUMBER_LETTER:
2393 case XML_REGEXP_NUMBER_OTHERS:
2394 return(0);
2395 case XML_REGEXP_PUNCT:
2396 if (type2 <= XML_REGEXP_PUNCT_OTHERS)
2397 return(1);
2398 return(0);
2399 case XML_REGEXP_PUNCT_CONNECTOR:
2400 case XML_REGEXP_PUNCT_DASH:
2401 case XML_REGEXP_PUNCT_OPEN:
2402 case XML_REGEXP_PUNCT_CLOSE:
2403 case XML_REGEXP_PUNCT_INITQUOTE:
2404 case XML_REGEXP_PUNCT_FINQUOTE:
2405 case XML_REGEXP_PUNCT_OTHERS:
2406 return(0);
2407 case XML_REGEXP_SEPAR:
2408 if (type2 <= XML_REGEXP_SEPAR_PARA)
2409 return(1);
2410 return(0);
2411 case XML_REGEXP_SEPAR_SPACE:
2412 case XML_REGEXP_SEPAR_LINE:
2413 case XML_REGEXP_SEPAR_PARA:
2414 return(0);
2415 case XML_REGEXP_SYMBOL:
2416 if (type2 <= XML_REGEXP_SYMBOL_OTHERS)
2417 return(1);
2418 return(0);
2419 case XML_REGEXP_SYMBOL_MATH:
2420 case XML_REGEXP_SYMBOL_CURRENCY:
2421 case XML_REGEXP_SYMBOL_MODIFIER:
2422 case XML_REGEXP_SYMBOL_OTHERS:
2423 return(0);
2424 case XML_REGEXP_OTHER:
2425 if (type2 <= XML_REGEXP_OTHER_NA)
2426 return(1);
2427 return(0);
2428 case XML_REGEXP_OTHER_CONTROL:
2429 case XML_REGEXP_OTHER_FORMAT:
2430 case XML_REGEXP_OTHER_PRIVATE:
2431 case XML_REGEXP_OTHER_NA:
2432 return(0);
2433 default:
2434 break;
2435 }
2436 return(1);
2437}
2438
2439/**
2440 * xmlFAEqualAtoms:
Daniel Veillarde19fc232002-04-22 16:01:24 +00002441 * @atom1: an atom
2442 * @atom2: an atom
Daniel Veillard1ba2aca2009-08-31 16:47:39 +02002443 * @deep: if not set only compare string pointers
Daniel Veillarde19fc232002-04-22 16:01:24 +00002444 *
Daniel Veillardfc011b72006-02-12 19:14:15 +00002445 * Compares two atoms to check whether they are the same exactly
2446 * this is used to remove equivalent transitions
Daniel Veillarde19fc232002-04-22 16:01:24 +00002447 *
Daniel Veillardfc011b72006-02-12 19:14:15 +00002448 * Returns 1 if same and 0 otherwise
Daniel Veillarde19fc232002-04-22 16:01:24 +00002449 */
2450static int
Daniel Veillard1ba2aca2009-08-31 16:47:39 +02002451xmlFAEqualAtoms(xmlRegAtomPtr atom1, xmlRegAtomPtr atom2, int deep) {
Daniel Veillardfc011b72006-02-12 19:14:15 +00002452 int ret = 0;
Daniel Veillard9efc4762005-07-19 14:33:55 +00002453
Daniel Veillarde19fc232002-04-22 16:01:24 +00002454 if (atom1 == atom2)
2455 return(1);
2456 if ((atom1 == NULL) || (atom2 == NULL))
2457 return(0);
2458
Daniel Veillardfc011b72006-02-12 19:14:15 +00002459 if (atom1->type != atom2->type)
2460 return(0);
2461 switch (atom1->type) {
2462 case XML_REGEXP_EPSILON:
2463 ret = 0;
2464 break;
2465 case XML_REGEXP_STRING:
Daniel Veillard1ba2aca2009-08-31 16:47:39 +02002466 if (!deep)
2467 ret = (atom1->valuep == atom2->valuep);
2468 else
2469 ret = xmlStrEqual((xmlChar *)atom1->valuep,
2470 (xmlChar *)atom2->valuep);
Daniel Veillardfc011b72006-02-12 19:14:15 +00002471 break;
2472 case XML_REGEXP_CHARVAL:
2473 ret = (atom1->codepoint == atom2->codepoint);
2474 break;
2475 case XML_REGEXP_RANGES:
2476 /* too hard to do in the general case */
2477 ret = 0;
2478 default:
2479 break;
2480 }
2481 return(ret);
2482}
2483
2484/**
2485 * xmlFACompareAtoms:
2486 * @atom1: an atom
2487 * @atom2: an atom
Daniel Veillard1ba2aca2009-08-31 16:47:39 +02002488 * @deep: if not set only compare string pointers
Daniel Veillardfc011b72006-02-12 19:14:15 +00002489 *
2490 * Compares two atoms to check whether they intersect in some ways,
2491 * this is used by xmlFAComputesDeterminism and xmlFARecurseDeterminism only
2492 *
2493 * Returns 1 if yes and 0 otherwise
2494 */
2495static int
Daniel Veillard1ba2aca2009-08-31 16:47:39 +02002496xmlFACompareAtoms(xmlRegAtomPtr atom1, xmlRegAtomPtr atom2, int deep) {
Daniel Veillardfc011b72006-02-12 19:14:15 +00002497 int ret = 1;
2498
2499 if (atom1 == atom2)
2500 return(1);
2501 if ((atom1 == NULL) || (atom2 == NULL))
2502 return(0);
2503
2504 if ((atom1->type == XML_REGEXP_ANYCHAR) ||
2505 (atom2->type == XML_REGEXP_ANYCHAR))
2506 return(1);
2507
2508 if (atom1->type > atom2->type) {
Daniel Veillard567a45b2005-10-18 19:11:55 +00002509 xmlRegAtomPtr tmp;
2510 tmp = atom1;
2511 atom1 = atom2;
2512 atom2 = tmp;
Daniel Veillardfc011b72006-02-12 19:14:15 +00002513 }
2514 if (atom1->type != atom2->type) {
2515 ret = xmlFACompareAtomTypes(atom1->type, atom2->type);
2516 /* if they can't intersect at the type level break now */
2517 if (ret == 0)
2518 return(0);
Daniel Veillard567a45b2005-10-18 19:11:55 +00002519 }
Daniel Veillarde19fc232002-04-22 16:01:24 +00002520 switch (atom1->type) {
2521 case XML_REGEXP_STRING:
Daniel Veillard1ba2aca2009-08-31 16:47:39 +02002522 if (!deep)
2523 ret = (atom1->valuep != atom2->valuep);
2524 else
2525 ret = xmlRegStrEqualWildcard((xmlChar *)atom1->valuep,
2526 (xmlChar *)atom2->valuep);
Daniel Veillard9efc4762005-07-19 14:33:55 +00002527 break;
Daniel Veillarde19fc232002-04-22 16:01:24 +00002528 case XML_REGEXP_EPSILON:
Daniel Veillard567a45b2005-10-18 19:11:55 +00002529 goto not_determinist;
Daniel Veillarde19fc232002-04-22 16:01:24 +00002530 case XML_REGEXP_CHARVAL:
Daniel Veillardfc011b72006-02-12 19:14:15 +00002531 if (atom2->type == XML_REGEXP_CHARVAL) {
2532 ret = (atom1->codepoint == atom2->codepoint);
2533 } else {
2534 ret = xmlRegCheckCharacter(atom2, atom1->codepoint);
2535 if (ret < 0)
2536 ret = 1;
2537 }
Daniel Veillard9efc4762005-07-19 14:33:55 +00002538 break;
Daniel Veillarde19fc232002-04-22 16:01:24 +00002539 case XML_REGEXP_RANGES:
Daniel Veillardfc011b72006-02-12 19:14:15 +00002540 if (atom2->type == XML_REGEXP_RANGES) {
Daniel Veillard567a45b2005-10-18 19:11:55 +00002541 int i, j, res;
2542 xmlRegRangePtr r1, r2;
2543
2544 /*
2545 * need to check that none of the ranges eventually matches
2546 */
2547 for (i = 0;i < atom1->nbRanges;i++) {
2548 for (j = 0;j < atom2->nbRanges;j++) {
2549 r1 = atom1->ranges[i];
2550 r2 = atom2->ranges[j];
2551 res = xmlFACompareRanges(r1, r2);
2552 if (res == 1) {
2553 ret = 1;
2554 goto done;
2555 }
2556 }
2557 }
2558 ret = 0;
2559 }
2560 break;
Daniel Veillarde19fc232002-04-22 16:01:24 +00002561 default:
Daniel Veillard567a45b2005-10-18 19:11:55 +00002562 goto not_determinist;
Daniel Veillarde19fc232002-04-22 16:01:24 +00002563 }
Daniel Veillard567a45b2005-10-18 19:11:55 +00002564done:
Daniel Veillard6e65e152005-08-09 11:09:52 +00002565 if (atom1->neg != atom2->neg) {
Daniel Veillard9efc4762005-07-19 14:33:55 +00002566 ret = !ret;
Daniel Veillard6e65e152005-08-09 11:09:52 +00002567 }
Daniel Veillard567a45b2005-10-18 19:11:55 +00002568 if (ret == 0)
2569 return(0);
2570not_determinist:
2571 return(1);
Daniel Veillarde19fc232002-04-22 16:01:24 +00002572}
2573
2574/**
2575 * xmlFARecurseDeterminism:
2576 * @ctxt: a regexp parser context
2577 *
2578 * Check whether the associated regexp is determinist,
2579 * should be called after xmlFAEliminateEpsilonTransitions()
2580 *
2581 */
2582static int
2583xmlFARecurseDeterminism(xmlRegParserCtxtPtr ctxt, xmlRegStatePtr state,
2584 int to, xmlRegAtomPtr atom) {
2585 int ret = 1;
Daniel Veillard567a45b2005-10-18 19:11:55 +00002586 int res;
Daniel Veillard5de09382005-09-26 17:18:17 +00002587 int transnr, nbTrans;
Daniel Veillarde19fc232002-04-22 16:01:24 +00002588 xmlRegTransPtr t1;
Daniel Veillard1ba2aca2009-08-31 16:47:39 +02002589 int deep = 1;
Daniel Veillarde19fc232002-04-22 16:01:24 +00002590
2591 if (state == NULL)
2592 return(ret);
Daniel Veillard466fcda2012-08-27 12:03:40 +08002593 if (state->markd == XML_REGEXP_MARK_VISITED)
2594 return(ret);
Daniel Veillard1ba2aca2009-08-31 16:47:39 +02002595
2596 if (ctxt->flags & AM_AUTOMATA_RNG)
2597 deep = 0;
2598
Daniel Veillard5de09382005-09-26 17:18:17 +00002599 /*
2600 * don't recurse on transitions potentially added in the course of
2601 * the elimination.
2602 */
2603 nbTrans = state->nbTrans;
2604 for (transnr = 0;transnr < nbTrans;transnr++) {
Daniel Veillarde19fc232002-04-22 16:01:24 +00002605 t1 = &(state->trans[transnr]);
2606 /*
2607 * check transitions conflicting with the one looked at
2608 */
2609 if (t1->atom == NULL) {
Daniel Veillard0e05f4c2006-11-01 15:33:04 +00002610 if (t1->to < 0)
Daniel Veillarde19fc232002-04-22 16:01:24 +00002611 continue;
Daniel Veillard466fcda2012-08-27 12:03:40 +08002612 state->markd = XML_REGEXP_MARK_VISITED;
Daniel Veillard567a45b2005-10-18 19:11:55 +00002613 res = xmlFARecurseDeterminism(ctxt, ctxt->states[t1->to],
Daniel Veillarde19fc232002-04-22 16:01:24 +00002614 to, atom);
Daniel Veillard466fcda2012-08-27 12:03:40 +08002615 state->markd = 0;
Daniel Veillard567a45b2005-10-18 19:11:55 +00002616 if (res == 0) {
2617 ret = 0;
Daniel Veillardaa622012005-10-20 15:55:25 +00002618 /* t1->nd = 1; */
Daniel Veillard567a45b2005-10-18 19:11:55 +00002619 }
Daniel Veillarde19fc232002-04-22 16:01:24 +00002620 continue;
2621 }
2622 if (t1->to != to)
2623 continue;
Daniel Veillard1ba2aca2009-08-31 16:47:39 +02002624 if (xmlFACompareAtoms(t1->atom, atom, deep)) {
Daniel Veillard567a45b2005-10-18 19:11:55 +00002625 ret = 0;
2626 /* mark the transition as non-deterministic */
2627 t1->nd = 1;
2628 }
Daniel Veillarde19fc232002-04-22 16:01:24 +00002629 }
2630 return(ret);
2631}
2632
2633/**
2634 * xmlFAComputesDeterminism:
2635 * @ctxt: a regexp parser context
2636 *
2637 * Check whether the associated regexp is determinist,
2638 * should be called after xmlFAEliminateEpsilonTransitions()
2639 *
2640 */
2641static int
2642xmlFAComputesDeterminism(xmlRegParserCtxtPtr ctxt) {
2643 int statenr, transnr;
2644 xmlRegStatePtr state;
Daniel Veillard567a45b2005-10-18 19:11:55 +00002645 xmlRegTransPtr t1, t2, last;
Daniel Veillarde19fc232002-04-22 16:01:24 +00002646 int i;
2647 int ret = 1;
Daniel Veillard1ba2aca2009-08-31 16:47:39 +02002648 int deep = 1;
Daniel Veillarde19fc232002-04-22 16:01:24 +00002649
Daniel Veillard4402ab42002-09-12 16:02:56 +00002650#ifdef DEBUG_REGEXP_GRAPH
2651 printf("xmlFAComputesDeterminism\n");
2652 xmlRegPrintCtxt(stdout, ctxt);
2653#endif
Daniel Veillarde19fc232002-04-22 16:01:24 +00002654 if (ctxt->determinist != -1)
2655 return(ctxt->determinist);
2656
Daniel Veillard1ba2aca2009-08-31 16:47:39 +02002657 if (ctxt->flags & AM_AUTOMATA_RNG)
2658 deep = 0;
2659
Daniel Veillarde19fc232002-04-22 16:01:24 +00002660 /*
Daniel Veillard567a45b2005-10-18 19:11:55 +00002661 * First cleanup the automata removing cancelled transitions
Daniel Veillarde19fc232002-04-22 16:01:24 +00002662 */
2663 for (statenr = 0;statenr < ctxt->nbStates;statenr++) {
2664 state = ctxt->states[statenr];
2665 if (state == NULL)
2666 continue;
Daniel Veillard4f82c8a2005-08-09 21:40:08 +00002667 if (state->nbTrans < 2)
2668 continue;
Daniel Veillarde19fc232002-04-22 16:01:24 +00002669 for (transnr = 0;transnr < state->nbTrans;transnr++) {
2670 t1 = &(state->trans[transnr]);
2671 /*
2672 * Determinism checks in case of counted or all transitions
2673 * will have to be handled separately
2674 */
Daniel Veillard567a45b2005-10-18 19:11:55 +00002675 if (t1->atom == NULL) {
Daniel Veillardaa622012005-10-20 15:55:25 +00002676 /* t1->nd = 1; */
Daniel Veillarde19fc232002-04-22 16:01:24 +00002677 continue;
Daniel Veillard567a45b2005-10-18 19:11:55 +00002678 }
Daniel Veillarde19fc232002-04-22 16:01:24 +00002679 if (t1->to == -1) /* eliminated */
2680 continue;
2681 for (i = 0;i < transnr;i++) {
2682 t2 = &(state->trans[i]);
2683 if (t2->to == -1) /* eliminated */
2684 continue;
2685 if (t2->atom != NULL) {
2686 if (t1->to == t2->to) {
Daniel Veillard1ba2aca2009-08-31 16:47:39 +02002687 /*
2688 * Here we use deep because we want to keep the
2689 * transitions which indicate a conflict
2690 */
2691 if (xmlFAEqualAtoms(t1->atom, t2->atom, deep) &&
Daniel Veillard11e28e42009-08-12 12:21:42 +02002692 (t1->counter == t2->counter) &&
2693 (t1->count == t2->count))
William M. Brackddf71d62004-05-06 04:17:26 +00002694 t2->to = -1; /* eliminated */
Daniel Veillard567a45b2005-10-18 19:11:55 +00002695 }
2696 }
2697 }
2698 }
2699 }
2700
2701 /*
2702 * Check for all states that there aren't 2 transitions
2703 * with the same atom and a different target.
2704 */
2705 for (statenr = 0;statenr < ctxt->nbStates;statenr++) {
2706 state = ctxt->states[statenr];
2707 if (state == NULL)
2708 continue;
2709 if (state->nbTrans < 2)
2710 continue;
2711 last = NULL;
2712 for (transnr = 0;transnr < state->nbTrans;transnr++) {
2713 t1 = &(state->trans[transnr]);
2714 /*
2715 * Determinism checks in case of counted or all transitions
2716 * will have to be handled separately
2717 */
2718 if (t1->atom == NULL) {
2719 continue;
2720 }
2721 if (t1->to == -1) /* eliminated */
2722 continue;
2723 for (i = 0;i < transnr;i++) {
2724 t2 = &(state->trans[i]);
2725 if (t2->to == -1) /* eliminated */
2726 continue;
2727 if (t2->atom != NULL) {
Daniel Veillard1ba2aca2009-08-31 16:47:39 +02002728 /*
2729 * But here we don't use deep because we want to
2730 * find transitions which indicate a conflict
2731 */
2732 if (xmlFACompareAtoms(t1->atom, t2->atom, 1)) {
Daniel Veillard567a45b2005-10-18 19:11:55 +00002733 ret = 0;
2734 /* mark the transitions as non-deterministic ones */
2735 t1->nd = 1;
2736 t2->nd = 1;
2737 last = t1;
Daniel Veillarde19fc232002-04-22 16:01:24 +00002738 }
2739 } else if (t1->to != -1) {
2740 /*
2741 * do the closure in case of remaining specific
2742 * epsilon transitions like choices or all
2743 */
2744 ret = xmlFARecurseDeterminism(ctxt, ctxt->states[t1->to],
2745 t2->to, t2->atom);
Daniel Veillard567a45b2005-10-18 19:11:55 +00002746 /* don't shortcut the computation so all non deterministic
2747 transition get marked down
Daniel Veillarde19fc232002-04-22 16:01:24 +00002748 if (ret == 0)
Daniel Veillardaa622012005-10-20 15:55:25 +00002749 return(0);
2750 */
Daniel Veillard567a45b2005-10-18 19:11:55 +00002751 if (ret == 0) {
2752 t1->nd = 1;
Daniel Veillardaa622012005-10-20 15:55:25 +00002753 /* t2->nd = 1; */
Daniel Veillard567a45b2005-10-18 19:11:55 +00002754 last = t1;
2755 }
Daniel Veillarde19fc232002-04-22 16:01:24 +00002756 }
2757 }
Daniel Veillard567a45b2005-10-18 19:11:55 +00002758 /* don't shortcut the computation so all non deterministic
2759 transition get marked down
Daniel Veillarde19fc232002-04-22 16:01:24 +00002760 if (ret == 0)
Daniel Veillard567a45b2005-10-18 19:11:55 +00002761 break; */
Daniel Veillarde19fc232002-04-22 16:01:24 +00002762 }
Daniel Veillard567a45b2005-10-18 19:11:55 +00002763
2764 /*
2765 * mark specifically the last non-deterministic transition
2766 * from a state since there is no need to set-up rollback
2767 * from it
2768 */
2769 if (last != NULL) {
2770 last->nd = 2;
2771 }
2772
2773 /* don't shortcut the computation so all non deterministic
2774 transition get marked down
Daniel Veillarde19fc232002-04-22 16:01:24 +00002775 if (ret == 0)
Daniel Veillard567a45b2005-10-18 19:11:55 +00002776 break; */
Daniel Veillarde19fc232002-04-22 16:01:24 +00002777 }
Daniel Veillard567a45b2005-10-18 19:11:55 +00002778
Daniel Veillarde19fc232002-04-22 16:01:24 +00002779 ctxt->determinist = ret;
2780 return(ret);
2781}
2782
Daniel Veillard4255d502002-04-16 15:50:10 +00002783/************************************************************************
Daniel Veillardf8e3db02012-09-11 13:26:36 +08002784 * *
Daniel Veillard4255d502002-04-16 15:50:10 +00002785 * Routines to check input against transition atoms *
Daniel Veillardf8e3db02012-09-11 13:26:36 +08002786 * *
Daniel Veillard4255d502002-04-16 15:50:10 +00002787 ************************************************************************/
2788
2789static int
2790xmlRegCheckCharacterRange(xmlRegAtomType type, int codepoint, int neg,
2791 int start, int end, const xmlChar *blockName) {
2792 int ret = 0;
2793
2794 switch (type) {
2795 case XML_REGEXP_STRING:
2796 case XML_REGEXP_SUBREG:
2797 case XML_REGEXP_RANGES:
2798 case XML_REGEXP_EPSILON:
2799 return(-1);
2800 case XML_REGEXP_ANYCHAR:
2801 ret = ((codepoint != '\n') && (codepoint != '\r'));
2802 break;
2803 case XML_REGEXP_CHARVAL:
2804 ret = ((codepoint >= start) && (codepoint <= end));
2805 break;
2806 case XML_REGEXP_NOTSPACE:
2807 neg = !neg;
2808 case XML_REGEXP_ANYSPACE:
2809 ret = ((codepoint == '\n') || (codepoint == '\r') ||
2810 (codepoint == '\t') || (codepoint == ' '));
2811 break;
2812 case XML_REGEXP_NOTINITNAME:
2813 neg = !neg;
2814 case XML_REGEXP_INITNAME:
Daniel Veillardf8e3db02012-09-11 13:26:36 +08002815 ret = (IS_LETTER(codepoint) ||
Daniel Veillard4255d502002-04-16 15:50:10 +00002816 (codepoint == '_') || (codepoint == ':'));
2817 break;
2818 case XML_REGEXP_NOTNAMECHAR:
2819 neg = !neg;
2820 case XML_REGEXP_NAMECHAR:
William M. Brack871611b2003-10-18 04:53:14 +00002821 ret = (IS_LETTER(codepoint) || IS_DIGIT(codepoint) ||
Daniel Veillard4255d502002-04-16 15:50:10 +00002822 (codepoint == '.') || (codepoint == '-') ||
2823 (codepoint == '_') || (codepoint == ':') ||
William M. Brack871611b2003-10-18 04:53:14 +00002824 IS_COMBINING(codepoint) || IS_EXTENDER(codepoint));
Daniel Veillard4255d502002-04-16 15:50:10 +00002825 break;
2826 case XML_REGEXP_NOTDECIMAL:
2827 neg = !neg;
2828 case XML_REGEXP_DECIMAL:
2829 ret = xmlUCSIsCatNd(codepoint);
2830 break;
2831 case XML_REGEXP_REALCHAR:
2832 neg = !neg;
2833 case XML_REGEXP_NOTREALCHAR:
2834 ret = xmlUCSIsCatP(codepoint);
2835 if (ret == 0)
2836 ret = xmlUCSIsCatZ(codepoint);
2837 if (ret == 0)
2838 ret = xmlUCSIsCatC(codepoint);
2839 break;
2840 case XML_REGEXP_LETTER:
2841 ret = xmlUCSIsCatL(codepoint);
2842 break;
2843 case XML_REGEXP_LETTER_UPPERCASE:
2844 ret = xmlUCSIsCatLu(codepoint);
2845 break;
2846 case XML_REGEXP_LETTER_LOWERCASE:
2847 ret = xmlUCSIsCatLl(codepoint);
2848 break;
2849 case XML_REGEXP_LETTER_TITLECASE:
2850 ret = xmlUCSIsCatLt(codepoint);
2851 break;
2852 case XML_REGEXP_LETTER_MODIFIER:
2853 ret = xmlUCSIsCatLm(codepoint);
2854 break;
2855 case XML_REGEXP_LETTER_OTHERS:
2856 ret = xmlUCSIsCatLo(codepoint);
2857 break;
2858 case XML_REGEXP_MARK:
2859 ret = xmlUCSIsCatM(codepoint);
2860 break;
2861 case XML_REGEXP_MARK_NONSPACING:
2862 ret = xmlUCSIsCatMn(codepoint);
2863 break;
2864 case XML_REGEXP_MARK_SPACECOMBINING:
2865 ret = xmlUCSIsCatMc(codepoint);
2866 break;
2867 case XML_REGEXP_MARK_ENCLOSING:
2868 ret = xmlUCSIsCatMe(codepoint);
2869 break;
2870 case XML_REGEXP_NUMBER:
2871 ret = xmlUCSIsCatN(codepoint);
2872 break;
2873 case XML_REGEXP_NUMBER_DECIMAL:
2874 ret = xmlUCSIsCatNd(codepoint);
2875 break;
2876 case XML_REGEXP_NUMBER_LETTER:
2877 ret = xmlUCSIsCatNl(codepoint);
2878 break;
2879 case XML_REGEXP_NUMBER_OTHERS:
2880 ret = xmlUCSIsCatNo(codepoint);
2881 break;
2882 case XML_REGEXP_PUNCT:
2883 ret = xmlUCSIsCatP(codepoint);
2884 break;
2885 case XML_REGEXP_PUNCT_CONNECTOR:
2886 ret = xmlUCSIsCatPc(codepoint);
2887 break;
2888 case XML_REGEXP_PUNCT_DASH:
2889 ret = xmlUCSIsCatPd(codepoint);
2890 break;
2891 case XML_REGEXP_PUNCT_OPEN:
2892 ret = xmlUCSIsCatPs(codepoint);
2893 break;
2894 case XML_REGEXP_PUNCT_CLOSE:
2895 ret = xmlUCSIsCatPe(codepoint);
2896 break;
2897 case XML_REGEXP_PUNCT_INITQUOTE:
2898 ret = xmlUCSIsCatPi(codepoint);
2899 break;
2900 case XML_REGEXP_PUNCT_FINQUOTE:
2901 ret = xmlUCSIsCatPf(codepoint);
2902 break;
2903 case XML_REGEXP_PUNCT_OTHERS:
2904 ret = xmlUCSIsCatPo(codepoint);
2905 break;
2906 case XML_REGEXP_SEPAR:
2907 ret = xmlUCSIsCatZ(codepoint);
2908 break;
2909 case XML_REGEXP_SEPAR_SPACE:
2910 ret = xmlUCSIsCatZs(codepoint);
2911 break;
2912 case XML_REGEXP_SEPAR_LINE:
2913 ret = xmlUCSIsCatZl(codepoint);
2914 break;
2915 case XML_REGEXP_SEPAR_PARA:
2916 ret = xmlUCSIsCatZp(codepoint);
2917 break;
2918 case XML_REGEXP_SYMBOL:
2919 ret = xmlUCSIsCatS(codepoint);
2920 break;
2921 case XML_REGEXP_SYMBOL_MATH:
2922 ret = xmlUCSIsCatSm(codepoint);
2923 break;
2924 case XML_REGEXP_SYMBOL_CURRENCY:
2925 ret = xmlUCSIsCatSc(codepoint);
2926 break;
2927 case XML_REGEXP_SYMBOL_MODIFIER:
2928 ret = xmlUCSIsCatSk(codepoint);
2929 break;
2930 case XML_REGEXP_SYMBOL_OTHERS:
2931 ret = xmlUCSIsCatSo(codepoint);
2932 break;
2933 case XML_REGEXP_OTHER:
2934 ret = xmlUCSIsCatC(codepoint);
2935 break;
2936 case XML_REGEXP_OTHER_CONTROL:
2937 ret = xmlUCSIsCatCc(codepoint);
2938 break;
2939 case XML_REGEXP_OTHER_FORMAT:
2940 ret = xmlUCSIsCatCf(codepoint);
2941 break;
2942 case XML_REGEXP_OTHER_PRIVATE:
2943 ret = xmlUCSIsCatCo(codepoint);
2944 break;
2945 case XML_REGEXP_OTHER_NA:
2946 /* ret = xmlUCSIsCatCn(codepoint); */
2947 /* Seems it doesn't exist anymore in recent Unicode releases */
2948 ret = 0;
2949 break;
2950 case XML_REGEXP_BLOCK_NAME:
2951 ret = xmlUCSIsBlock(codepoint, (const char *) blockName);
2952 break;
2953 }
2954 if (neg)
2955 return(!ret);
2956 return(ret);
2957}
2958
2959static int
2960xmlRegCheckCharacter(xmlRegAtomPtr atom, int codepoint) {
2961 int i, ret = 0;
2962 xmlRegRangePtr range;
2963
William M. Brack871611b2003-10-18 04:53:14 +00002964 if ((atom == NULL) || (!IS_CHAR(codepoint)))
Daniel Veillard4255d502002-04-16 15:50:10 +00002965 return(-1);
2966
2967 switch (atom->type) {
2968 case XML_REGEXP_SUBREG:
2969 case XML_REGEXP_EPSILON:
2970 return(-1);
2971 case XML_REGEXP_CHARVAL:
2972 return(codepoint == atom->codepoint);
2973 case XML_REGEXP_RANGES: {
2974 int accept = 0;
Daniel Veillardf2a12832003-11-24 13:04:35 +00002975
Daniel Veillard4255d502002-04-16 15:50:10 +00002976 for (i = 0;i < atom->nbRanges;i++) {
2977 range = atom->ranges[i];
Daniel Veillardf8b9de32003-11-24 14:27:26 +00002978 if (range->neg == 2) {
Daniel Veillard4255d502002-04-16 15:50:10 +00002979 ret = xmlRegCheckCharacterRange(range->type, codepoint,
2980 0, range->start, range->end,
2981 range->blockName);
2982 if (ret != 0)
2983 return(0); /* excluded char */
Daniel Veillardf8b9de32003-11-24 14:27:26 +00002984 } else if (range->neg) {
2985 ret = xmlRegCheckCharacterRange(range->type, codepoint,
2986 0, range->start, range->end,
2987 range->blockName);
2988 if (ret == 0)
Daniel Veillardf2a12832003-11-24 13:04:35 +00002989 accept = 1;
Daniel Veillardf8b9de32003-11-24 14:27:26 +00002990 else
2991 return(0);
Daniel Veillard4255d502002-04-16 15:50:10 +00002992 } else {
2993 ret = xmlRegCheckCharacterRange(range->type, codepoint,
2994 0, range->start, range->end,
2995 range->blockName);
2996 if (ret != 0)
2997 accept = 1; /* might still be excluded */
2998 }
2999 }
3000 return(accept);
3001 }
3002 case XML_REGEXP_STRING:
3003 printf("TODO: XML_REGEXP_STRING\n");
3004 return(-1);
3005 case XML_REGEXP_ANYCHAR:
3006 case XML_REGEXP_ANYSPACE:
3007 case XML_REGEXP_NOTSPACE:
3008 case XML_REGEXP_INITNAME:
3009 case XML_REGEXP_NOTINITNAME:
3010 case XML_REGEXP_NAMECHAR:
3011 case XML_REGEXP_NOTNAMECHAR:
3012 case XML_REGEXP_DECIMAL:
3013 case XML_REGEXP_NOTDECIMAL:
3014 case XML_REGEXP_REALCHAR:
3015 case XML_REGEXP_NOTREALCHAR:
3016 case XML_REGEXP_LETTER:
3017 case XML_REGEXP_LETTER_UPPERCASE:
3018 case XML_REGEXP_LETTER_LOWERCASE:
3019 case XML_REGEXP_LETTER_TITLECASE:
3020 case XML_REGEXP_LETTER_MODIFIER:
3021 case XML_REGEXP_LETTER_OTHERS:
3022 case XML_REGEXP_MARK:
3023 case XML_REGEXP_MARK_NONSPACING:
3024 case XML_REGEXP_MARK_SPACECOMBINING:
3025 case XML_REGEXP_MARK_ENCLOSING:
3026 case XML_REGEXP_NUMBER:
3027 case XML_REGEXP_NUMBER_DECIMAL:
3028 case XML_REGEXP_NUMBER_LETTER:
3029 case XML_REGEXP_NUMBER_OTHERS:
3030 case XML_REGEXP_PUNCT:
3031 case XML_REGEXP_PUNCT_CONNECTOR:
3032 case XML_REGEXP_PUNCT_DASH:
3033 case XML_REGEXP_PUNCT_OPEN:
3034 case XML_REGEXP_PUNCT_CLOSE:
3035 case XML_REGEXP_PUNCT_INITQUOTE:
3036 case XML_REGEXP_PUNCT_FINQUOTE:
3037 case XML_REGEXP_PUNCT_OTHERS:
3038 case XML_REGEXP_SEPAR:
3039 case XML_REGEXP_SEPAR_SPACE:
3040 case XML_REGEXP_SEPAR_LINE:
3041 case XML_REGEXP_SEPAR_PARA:
3042 case XML_REGEXP_SYMBOL:
3043 case XML_REGEXP_SYMBOL_MATH:
3044 case XML_REGEXP_SYMBOL_CURRENCY:
3045 case XML_REGEXP_SYMBOL_MODIFIER:
3046 case XML_REGEXP_SYMBOL_OTHERS:
3047 case XML_REGEXP_OTHER:
3048 case XML_REGEXP_OTHER_CONTROL:
3049 case XML_REGEXP_OTHER_FORMAT:
3050 case XML_REGEXP_OTHER_PRIVATE:
3051 case XML_REGEXP_OTHER_NA:
3052 case XML_REGEXP_BLOCK_NAME:
3053 ret = xmlRegCheckCharacterRange(atom->type, codepoint, 0, 0, 0,
3054 (const xmlChar *)atom->valuep);
3055 if (atom->neg)
3056 ret = !ret;
3057 break;
3058 }
3059 return(ret);
3060}
3061
3062/************************************************************************
Daniel Veillardf8e3db02012-09-11 13:26:36 +08003063 * *
William M. Brackddf71d62004-05-06 04:17:26 +00003064 * Saving and restoring state of an execution context *
Daniel Veillardf8e3db02012-09-11 13:26:36 +08003065 * *
Daniel Veillard4255d502002-04-16 15:50:10 +00003066 ************************************************************************/
3067
3068#ifdef DEBUG_REGEXP_EXEC
3069static void
3070xmlFARegDebugExec(xmlRegExecCtxtPtr exec) {
3071 printf("state: %d:%d:idx %d", exec->state->no, exec->transno, exec->index);
3072 if (exec->inputStack != NULL) {
3073 int i;
3074 printf(": ");
3075 for (i = 0;(i < 3) && (i < exec->inputStackNr);i++)
Daniel Veillard0e05f4c2006-11-01 15:33:04 +00003076 printf("%s ", (const char *)
3077 exec->inputStack[exec->inputStackNr - (i + 1)].value);
Daniel Veillard4255d502002-04-16 15:50:10 +00003078 } else {
3079 printf(": %s", &(exec->inputString[exec->index]));
3080 }
3081 printf("\n");
3082}
3083#endif
3084
3085static void
3086xmlFARegExecSave(xmlRegExecCtxtPtr exec) {
3087#ifdef DEBUG_REGEXP_EXEC
3088 printf("saving ");
3089 exec->transno++;
3090 xmlFARegDebugExec(exec);
3091 exec->transno--;
3092#endif
Daniel Veillard94cc1032005-09-15 13:09:00 +00003093#ifdef MAX_PUSH
3094 if (exec->nbPush > MAX_PUSH) {
3095 return;
3096 }
3097 exec->nbPush++;
3098#endif
Daniel Veillard4255d502002-04-16 15:50:10 +00003099
3100 if (exec->maxRollbacks == 0) {
3101 exec->maxRollbacks = 4;
3102 exec->rollbacks = (xmlRegExecRollback *) xmlMalloc(exec->maxRollbacks *
3103 sizeof(xmlRegExecRollback));
3104 if (exec->rollbacks == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00003105 xmlRegexpErrMemory(NULL, "saving regexp");
Daniel Veillard4255d502002-04-16 15:50:10 +00003106 exec->maxRollbacks = 0;
3107 return;
3108 }
3109 memset(exec->rollbacks, 0,
3110 exec->maxRollbacks * sizeof(xmlRegExecRollback));
3111 } else if (exec->nbRollbacks >= exec->maxRollbacks) {
3112 xmlRegExecRollback *tmp;
3113 int len = exec->maxRollbacks;
3114
3115 exec->maxRollbacks *= 2;
3116 tmp = (xmlRegExecRollback *) xmlRealloc(exec->rollbacks,
3117 exec->maxRollbacks * sizeof(xmlRegExecRollback));
3118 if (tmp == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00003119 xmlRegexpErrMemory(NULL, "saving regexp");
Daniel Veillard4255d502002-04-16 15:50:10 +00003120 exec->maxRollbacks /= 2;
3121 return;
3122 }
3123 exec->rollbacks = tmp;
3124 tmp = &exec->rollbacks[len];
3125 memset(tmp, 0, (exec->maxRollbacks - len) * sizeof(xmlRegExecRollback));
3126 }
3127 exec->rollbacks[exec->nbRollbacks].state = exec->state;
3128 exec->rollbacks[exec->nbRollbacks].index = exec->index;
3129 exec->rollbacks[exec->nbRollbacks].nextbranch = exec->transno + 1;
3130 if (exec->comp->nbCounters > 0) {
3131 if (exec->rollbacks[exec->nbRollbacks].counts == NULL) {
3132 exec->rollbacks[exec->nbRollbacks].counts = (int *)
3133 xmlMalloc(exec->comp->nbCounters * sizeof(int));
3134 if (exec->rollbacks[exec->nbRollbacks].counts == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00003135 xmlRegexpErrMemory(NULL, "saving regexp");
Daniel Veillard4255d502002-04-16 15:50:10 +00003136 exec->status = -5;
3137 return;
3138 }
3139 }
3140 memcpy(exec->rollbacks[exec->nbRollbacks].counts, exec->counts,
3141 exec->comp->nbCounters * sizeof(int));
3142 }
3143 exec->nbRollbacks++;
3144}
3145
3146static void
3147xmlFARegExecRollBack(xmlRegExecCtxtPtr exec) {
3148 if (exec->nbRollbacks <= 0) {
3149 exec->status = -1;
3150#ifdef DEBUG_REGEXP_EXEC
3151 printf("rollback failed on empty stack\n");
3152#endif
3153 return;
3154 }
3155 exec->nbRollbacks--;
3156 exec->state = exec->rollbacks[exec->nbRollbacks].state;
3157 exec->index = exec->rollbacks[exec->nbRollbacks].index;
3158 exec->transno = exec->rollbacks[exec->nbRollbacks].nextbranch;
3159 if (exec->comp->nbCounters > 0) {
3160 if (exec->rollbacks[exec->nbRollbacks].counts == NULL) {
3161 fprintf(stderr, "exec save: allocation failed");
3162 exec->status = -6;
3163 return;
3164 }
Gaurav2671b012013-09-11 14:59:06 +08003165 if (exec->counts) {
3166 memcpy(exec->counts, exec->rollbacks[exec->nbRollbacks].counts,
Daniel Veillard4255d502002-04-16 15:50:10 +00003167 exec->comp->nbCounters * sizeof(int));
Gaurav2671b012013-09-11 14:59:06 +08003168 }
Daniel Veillard4255d502002-04-16 15:50:10 +00003169 }
3170
3171#ifdef DEBUG_REGEXP_EXEC
3172 printf("restored ");
3173 xmlFARegDebugExec(exec);
3174#endif
3175}
3176
3177/************************************************************************
Daniel Veillardf8e3db02012-09-11 13:26:36 +08003178 * *
William M. Brackddf71d62004-05-06 04:17:26 +00003179 * Verifier, running an input against a compiled regexp *
Daniel Veillardf8e3db02012-09-11 13:26:36 +08003180 * *
Daniel Veillard4255d502002-04-16 15:50:10 +00003181 ************************************************************************/
3182
3183static int
3184xmlFARegExec(xmlRegexpPtr comp, const xmlChar *content) {
3185 xmlRegExecCtxt execval;
3186 xmlRegExecCtxtPtr exec = &execval;
Daniel Veillard567a45b2005-10-18 19:11:55 +00003187 int ret, codepoint = 0, len, deter;
Daniel Veillard4255d502002-04-16 15:50:10 +00003188
3189 exec->inputString = content;
3190 exec->index = 0;
Daniel Veillard94cc1032005-09-15 13:09:00 +00003191 exec->nbPush = 0;
Daniel Veillard4255d502002-04-16 15:50:10 +00003192 exec->determinist = 1;
3193 exec->maxRollbacks = 0;
3194 exec->nbRollbacks = 0;
3195 exec->rollbacks = NULL;
3196 exec->status = 0;
3197 exec->comp = comp;
3198 exec->state = comp->states[0];
3199 exec->transno = 0;
3200 exec->transcount = 0;
Daniel Veillardf2a12832003-11-24 13:04:35 +00003201 exec->inputStack = NULL;
3202 exec->inputStackMax = 0;
Daniel Veillard4255d502002-04-16 15:50:10 +00003203 if (comp->nbCounters > 0) {
3204 exec->counts = (int *) xmlMalloc(comp->nbCounters * sizeof(int));
Daniel Veillardff46a042003-10-08 08:53:17 +00003205 if (exec->counts == NULL) {
3206 xmlRegexpErrMemory(NULL, "running regexp");
Daniel Veillard4255d502002-04-16 15:50:10 +00003207 return(-1);
Daniel Veillardff46a042003-10-08 08:53:17 +00003208 }
Daniel Veillard4255d502002-04-16 15:50:10 +00003209 memset(exec->counts, 0, comp->nbCounters * sizeof(int));
3210 } else
3211 exec->counts = NULL;
Daniel Veillard40851d02012-08-17 20:34:05 +08003212 while ((exec->status == 0) && (exec->state != NULL) &&
Daniel Veillard4255d502002-04-16 15:50:10 +00003213 ((exec->inputString[exec->index] != 0) ||
Daniel Veillardad559982008-05-12 13:15:35 +00003214 ((exec->state != NULL) &&
3215 (exec->state->type != XML_REGEXP_FINAL_STATE)))) {
Daniel Veillard4255d502002-04-16 15:50:10 +00003216 xmlRegTransPtr trans;
3217 xmlRegAtomPtr atom;
3218
3219 /*
William M. Brack0e00b282004-04-26 15:40:47 +00003220 * If end of input on non-terminal state, rollback, however we may
Daniel Veillard4255d502002-04-16 15:50:10 +00003221 * still have epsilon like transition for counted transitions
William M. Brack0e00b282004-04-26 15:40:47 +00003222 * on counters, in that case don't break too early. Additionally,
3223 * if we are working on a range like "AB{0,2}", where B is not present,
3224 * we don't want to break.
Daniel Veillard4255d502002-04-16 15:50:10 +00003225 */
Daniel Veillard11ce4002006-03-10 00:36:23 +00003226 len = 1;
William M. Brack0e00b282004-04-26 15:40:47 +00003227 if ((exec->inputString[exec->index] == 0) && (exec->counts == NULL)) {
William M. Brackddf71d62004-05-06 04:17:26 +00003228 /*
3229 * if there is a transition, we must check if
3230 * atom allows minOccurs of 0
3231 */
3232 if (exec->transno < exec->state->nbTrans) {
William M. Brack0e00b282004-04-26 15:40:47 +00003233 trans = &exec->state->trans[exec->transno];
3234 if (trans->to >=0) {
3235 atom = trans->atom;
3236 if (!((atom->min == 0) && (atom->max > 0)))
3237 goto rollback;
3238 }
3239 } else
3240 goto rollback;
3241 }
Daniel Veillard4255d502002-04-16 15:50:10 +00003242
3243 exec->transcount = 0;
3244 for (;exec->transno < exec->state->nbTrans;exec->transno++) {
3245 trans = &exec->state->trans[exec->transno];
3246 if (trans->to < 0)
3247 continue;
3248 atom = trans->atom;
3249 ret = 0;
Daniel Veillard567a45b2005-10-18 19:11:55 +00003250 deter = 1;
Daniel Veillard4255d502002-04-16 15:50:10 +00003251 if (trans->count >= 0) {
3252 int count;
3253 xmlRegCounterPtr counter;
3254
Daniel Veillard11ce4002006-03-10 00:36:23 +00003255 if (exec->counts == NULL) {
3256 exec->status = -1;
3257 goto error;
3258 }
Daniel Veillard4255d502002-04-16 15:50:10 +00003259 /*
3260 * A counted transition.
3261 */
3262
3263 count = exec->counts[trans->count];
3264 counter = &exec->comp->counters[trans->count];
3265#ifdef DEBUG_REGEXP_EXEC
3266 printf("testing count %d: val %d, min %d, max %d\n",
3267 trans->count, count, counter->min, counter->max);
3268#endif
3269 ret = ((count >= counter->min) && (count <= counter->max));
Daniel Veillard567a45b2005-10-18 19:11:55 +00003270 if ((ret) && (counter->min != counter->max))
3271 deter = 0;
Daniel Veillard4255d502002-04-16 15:50:10 +00003272 } else if (atom == NULL) {
3273 fprintf(stderr, "epsilon transition left at runtime\n");
3274 exec->status = -2;
3275 break;
3276 } else if (exec->inputString[exec->index] != 0) {
3277 codepoint = CUR_SCHAR(&(exec->inputString[exec->index]), len);
3278 ret = xmlRegCheckCharacter(atom, codepoint);
William M. Brack0e00b282004-04-26 15:40:47 +00003279 if ((ret == 1) && (atom->min >= 0) && (atom->max > 0)) {
Daniel Veillard4255d502002-04-16 15:50:10 +00003280 xmlRegStatePtr to = comp->states[trans->to];
3281
3282 /*
3283 * this is a multiple input sequence
Daniel Veillardfc6eca02005-11-01 15:24:02 +00003284 * If there is a counter associated increment it now.
3285 * before potentially saving and rollback
Daniel Veillardc821e032007-08-28 17:33:45 +00003286 * do not increment if the counter is already over the
3287 * maximum limit in which case get to next transition
Daniel Veillard4255d502002-04-16 15:50:10 +00003288 */
Daniel Veillardfc6eca02005-11-01 15:24:02 +00003289 if (trans->counter >= 0) {
Daniel Veillardc821e032007-08-28 17:33:45 +00003290 xmlRegCounterPtr counter;
3291
3292 if ((exec->counts == NULL) ||
3293 (exec->comp == NULL) ||
3294 (exec->comp->counters == NULL)) {
Daniel Veillard11ce4002006-03-10 00:36:23 +00003295 exec->status = -1;
3296 goto error;
3297 }
Daniel Veillardc821e032007-08-28 17:33:45 +00003298 counter = &exec->comp->counters[trans->counter];
3299 if (exec->counts[trans->counter] >= counter->max)
3300 continue; /* for loop on transitions */
3301
Daniel Veillardfc6eca02005-11-01 15:24:02 +00003302#ifdef DEBUG_REGEXP_EXEC
3303 printf("Increasing count %d\n", trans->counter);
3304#endif
3305 exec->counts[trans->counter]++;
3306 }
Daniel Veillard4255d502002-04-16 15:50:10 +00003307 if (exec->state->nbTrans > exec->transno + 1) {
3308 xmlFARegExecSave(exec);
3309 }
3310 exec->transcount = 1;
3311 do {
3312 /*
3313 * Try to progress as much as possible on the input
3314 */
3315 if (exec->transcount == atom->max) {
3316 break;
3317 }
3318 exec->index += len;
3319 /*
3320 * End of input: stop here
3321 */
3322 if (exec->inputString[exec->index] == 0) {
3323 exec->index -= len;
3324 break;
3325 }
3326 if (exec->transcount >= atom->min) {
3327 int transno = exec->transno;
3328 xmlRegStatePtr state = exec->state;
3329
3330 /*
3331 * The transition is acceptable save it
3332 */
3333 exec->transno = -1; /* trick */
3334 exec->state = to;
3335 xmlFARegExecSave(exec);
3336 exec->transno = transno;
3337 exec->state = state;
3338 }
3339 codepoint = CUR_SCHAR(&(exec->inputString[exec->index]),
3340 len);
3341 ret = xmlRegCheckCharacter(atom, codepoint);
3342 exec->transcount++;
3343 } while (ret == 1);
3344 if (exec->transcount < atom->min)
3345 ret = 0;
3346
3347 /*
3348 * If the last check failed but one transition was found
3349 * possible, rollback
3350 */
3351 if (ret < 0)
3352 ret = 0;
3353 if (ret == 0) {
3354 goto rollback;
3355 }
Daniel Veillardfc6eca02005-11-01 15:24:02 +00003356 if (trans->counter >= 0) {
Daniel Veillard11ce4002006-03-10 00:36:23 +00003357 if (exec->counts == NULL) {
3358 exec->status = -1;
3359 goto error;
3360 }
Daniel Veillardfc6eca02005-11-01 15:24:02 +00003361#ifdef DEBUG_REGEXP_EXEC
3362 printf("Decreasing count %d\n", trans->counter);
3363#endif
3364 exec->counts[trans->counter]--;
3365 }
William M. Brack0e00b282004-04-26 15:40:47 +00003366 } else if ((ret == 0) && (atom->min == 0) && (atom->max > 0)) {
3367 /*
3368 * we don't match on the codepoint, but minOccurs of 0
3369 * says that's ok. Setting len to 0 inhibits stepping
3370 * over the codepoint.
3371 */
3372 exec->transcount = 1;
3373 len = 0;
3374 ret = 1;
Daniel Veillard4255d502002-04-16 15:50:10 +00003375 }
William M. Brack0e00b282004-04-26 15:40:47 +00003376 } else if ((atom->min == 0) && (atom->max > 0)) {
3377 /* another spot to match when minOccurs is 0 */
3378 exec->transcount = 1;
3379 len = 0;
3380 ret = 1;
Daniel Veillard4255d502002-04-16 15:50:10 +00003381 }
3382 if (ret == 1) {
Daniel Veillard567a45b2005-10-18 19:11:55 +00003383 if ((trans->nd == 1) ||
3384 ((trans->count >= 0) && (deter == 0) &&
3385 (exec->state->nbTrans > exec->transno + 1))) {
Daniel Veillardaa622012005-10-20 15:55:25 +00003386#ifdef DEBUG_REGEXP_EXEC
3387 if (trans->nd == 1)
3388 printf("Saving on nd transition atom %d for %c at %d\n",
3389 trans->atom->no, codepoint, exec->index);
3390 else
3391 printf("Saving on counted transition count %d for %c at %d\n",
3392 trans->count, codepoint, exec->index);
3393#endif
Daniel Veillard4255d502002-04-16 15:50:10 +00003394 xmlFARegExecSave(exec);
3395 }
3396 if (trans->counter >= 0) {
Daniel Veillardc821e032007-08-28 17:33:45 +00003397 xmlRegCounterPtr counter;
3398
3399 /* make sure we don't go over the counter maximum value */
3400 if ((exec->counts == NULL) ||
3401 (exec->comp == NULL) ||
3402 (exec->comp->counters == NULL)) {
3403 exec->status = -1;
Daniel Veillard11ce4002006-03-10 00:36:23 +00003404 goto error;
3405 }
Daniel Veillardc821e032007-08-28 17:33:45 +00003406 counter = &exec->comp->counters[trans->counter];
3407 if (exec->counts[trans->counter] >= counter->max)
3408 continue; /* for loop on transitions */
Daniel Veillard4255d502002-04-16 15:50:10 +00003409#ifdef DEBUG_REGEXP_EXEC
3410 printf("Increasing count %d\n", trans->counter);
3411#endif
3412 exec->counts[trans->counter]++;
3413 }
Daniel Veillard10752282005-08-08 13:05:13 +00003414 if ((trans->count >= 0) &&
3415 (trans->count < REGEXP_ALL_COUNTER)) {
Daniel Veillard11ce4002006-03-10 00:36:23 +00003416 if (exec->counts == NULL) {
3417 exec->status = -1;
3418 goto error;
3419 }
Daniel Veillard10752282005-08-08 13:05:13 +00003420#ifdef DEBUG_REGEXP_EXEC
3421 printf("resetting count %d on transition\n",
3422 trans->count);
3423#endif
3424 exec->counts[trans->count] = 0;
3425 }
Daniel Veillard4255d502002-04-16 15:50:10 +00003426#ifdef DEBUG_REGEXP_EXEC
3427 printf("entering state %d\n", trans->to);
3428#endif
3429 exec->state = comp->states[trans->to];
3430 exec->transno = 0;
3431 if (trans->atom != NULL) {
3432 exec->index += len;
3433 }
3434 goto progress;
3435 } else if (ret < 0) {
3436 exec->status = -4;
3437 break;
3438 }
3439 }
3440 if ((exec->transno != 0) || (exec->state->nbTrans == 0)) {
3441rollback:
3442 /*
3443 * Failed to find a way out
3444 */
3445 exec->determinist = 0;
Daniel Veillardaa622012005-10-20 15:55:25 +00003446#ifdef DEBUG_REGEXP_EXEC
3447 printf("rollback from state %d on %d:%c\n", exec->state->no,
3448 codepoint,codepoint);
3449#endif
Daniel Veillard4255d502002-04-16 15:50:10 +00003450 xmlFARegExecRollBack(exec);
3451 }
3452progress:
3453 continue;
3454 }
Daniel Veillard11ce4002006-03-10 00:36:23 +00003455error:
Daniel Veillard4255d502002-04-16 15:50:10 +00003456 if (exec->rollbacks != NULL) {
3457 if (exec->counts != NULL) {
3458 int i;
3459
3460 for (i = 0;i < exec->maxRollbacks;i++)
3461 if (exec->rollbacks[i].counts != NULL)
3462 xmlFree(exec->rollbacks[i].counts);
3463 }
3464 xmlFree(exec->rollbacks);
3465 }
Daniel Veillard40851d02012-08-17 20:34:05 +08003466 if (exec->state == NULL)
3467 return(-1);
Daniel Veillard4255d502002-04-16 15:50:10 +00003468 if (exec->counts != NULL)
3469 xmlFree(exec->counts);
3470 if (exec->status == 0)
3471 return(1);
Daniel Veillard94cc1032005-09-15 13:09:00 +00003472 if (exec->status == -1) {
3473 if (exec->nbPush > MAX_PUSH)
3474 return(-1);
Daniel Veillard4255d502002-04-16 15:50:10 +00003475 return(0);
Daniel Veillard94cc1032005-09-15 13:09:00 +00003476 }
Daniel Veillard4255d502002-04-16 15:50:10 +00003477 return(exec->status);
3478}
3479
3480/************************************************************************
Daniel Veillardf8e3db02012-09-11 13:26:36 +08003481 * *
William M. Brackddf71d62004-05-06 04:17:26 +00003482 * Progressive interface to the verifier one atom at a time *
Daniel Veillardf8e3db02012-09-11 13:26:36 +08003483 * *
Daniel Veillard4255d502002-04-16 15:50:10 +00003484 ************************************************************************/
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003485#ifdef DEBUG_ERR
3486static void testerr(xmlRegExecCtxtPtr exec);
3487#endif
Daniel Veillard4255d502002-04-16 15:50:10 +00003488
3489/**
Daniel Veillard01c13b52002-12-10 15:19:08 +00003490 * xmlRegNewExecCtxt:
Daniel Veillard4255d502002-04-16 15:50:10 +00003491 * @comp: a precompiled regular expression
3492 * @callback: a callback function used for handling progresses in the
3493 * automata matching phase
3494 * @data: the context data associated to the callback in this context
3495 *
3496 * Build a context used for progressive evaluation of a regexp.
Daniel Veillard01c13b52002-12-10 15:19:08 +00003497 *
3498 * Returns the new context
Daniel Veillard4255d502002-04-16 15:50:10 +00003499 */
3500xmlRegExecCtxtPtr
3501xmlRegNewExecCtxt(xmlRegexpPtr comp, xmlRegExecCallbacks callback, void *data) {
3502 xmlRegExecCtxtPtr exec;
3503
3504 if (comp == NULL)
3505 return(NULL);
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00003506 if ((comp->compact == NULL) && (comp->states == NULL))
3507 return(NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +00003508 exec = (xmlRegExecCtxtPtr) xmlMalloc(sizeof(xmlRegExecCtxt));
3509 if (exec == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00003510 xmlRegexpErrMemory(NULL, "creating execution context");
Daniel Veillard4255d502002-04-16 15:50:10 +00003511 return(NULL);
3512 }
3513 memset(exec, 0, sizeof(xmlRegExecCtxt));
3514 exec->inputString = NULL;
3515 exec->index = 0;
3516 exec->determinist = 1;
3517 exec->maxRollbacks = 0;
3518 exec->nbRollbacks = 0;
3519 exec->rollbacks = NULL;
3520 exec->status = 0;
3521 exec->comp = comp;
Daniel Veillard23e73572002-09-19 19:56:43 +00003522 if (comp->compact == NULL)
3523 exec->state = comp->states[0];
Daniel Veillard4255d502002-04-16 15:50:10 +00003524 exec->transno = 0;
3525 exec->transcount = 0;
3526 exec->callback = callback;
3527 exec->data = data;
3528 if (comp->nbCounters > 0) {
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003529 /*
3530 * For error handling, exec->counts is allocated twice the size
3531 * the second half is used to store the data in case of rollback
3532 */
3533 exec->counts = (int *) xmlMalloc(comp->nbCounters * sizeof(int)
3534 * 2);
Daniel Veillard4255d502002-04-16 15:50:10 +00003535 if (exec->counts == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00003536 xmlRegexpErrMemory(NULL, "creating execution context");
Daniel Veillard4255d502002-04-16 15:50:10 +00003537 xmlFree(exec);
3538 return(NULL);
3539 }
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003540 memset(exec->counts, 0, comp->nbCounters * sizeof(int) * 2);
3541 exec->errCounts = &exec->counts[comp->nbCounters];
3542 } else {
Daniel Veillard4255d502002-04-16 15:50:10 +00003543 exec->counts = NULL;
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003544 exec->errCounts = NULL;
3545 }
Daniel Veillard4255d502002-04-16 15:50:10 +00003546 exec->inputStackMax = 0;
3547 exec->inputStackNr = 0;
3548 exec->inputStack = NULL;
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003549 exec->errStateNo = -1;
3550 exec->errString = NULL;
Daniel Veillard94cc1032005-09-15 13:09:00 +00003551 exec->nbPush = 0;
Daniel Veillard4255d502002-04-16 15:50:10 +00003552 return(exec);
3553}
3554
3555/**
3556 * xmlRegFreeExecCtxt:
3557 * @exec: a regular expression evaulation context
3558 *
3559 * Free the structures associated to a regular expression evaulation context.
3560 */
3561void
3562xmlRegFreeExecCtxt(xmlRegExecCtxtPtr exec) {
3563 if (exec == NULL)
3564 return;
3565
3566 if (exec->rollbacks != NULL) {
3567 if (exec->counts != NULL) {
3568 int i;
3569
3570 for (i = 0;i < exec->maxRollbacks;i++)
3571 if (exec->rollbacks[i].counts != NULL)
3572 xmlFree(exec->rollbacks[i].counts);
3573 }
3574 xmlFree(exec->rollbacks);
3575 }
3576 if (exec->counts != NULL)
3577 xmlFree(exec->counts);
3578 if (exec->inputStack != NULL) {
3579 int i;
3580
Daniel Veillard32370232002-10-16 14:08:14 +00003581 for (i = 0;i < exec->inputStackNr;i++) {
3582 if (exec->inputStack[i].value != NULL)
3583 xmlFree(exec->inputStack[i].value);
3584 }
Daniel Veillard4255d502002-04-16 15:50:10 +00003585 xmlFree(exec->inputStack);
3586 }
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003587 if (exec->errString != NULL)
3588 xmlFree(exec->errString);
Daniel Veillard4255d502002-04-16 15:50:10 +00003589 xmlFree(exec);
3590}
3591
3592static void
3593xmlFARegExecSaveInputString(xmlRegExecCtxtPtr exec, const xmlChar *value,
3594 void *data) {
3595#ifdef DEBUG_PUSH
3596 printf("saving value: %d:%s\n", exec->inputStackNr, value);
3597#endif
3598 if (exec->inputStackMax == 0) {
3599 exec->inputStackMax = 4;
Daniel Veillardf8e3db02012-09-11 13:26:36 +08003600 exec->inputStack = (xmlRegInputTokenPtr)
Daniel Veillard4255d502002-04-16 15:50:10 +00003601 xmlMalloc(exec->inputStackMax * sizeof(xmlRegInputToken));
3602 if (exec->inputStack == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00003603 xmlRegexpErrMemory(NULL, "pushing input string");
Daniel Veillard4255d502002-04-16 15:50:10 +00003604 exec->inputStackMax = 0;
3605 return;
3606 }
3607 } else if (exec->inputStackNr + 1 >= exec->inputStackMax) {
3608 xmlRegInputTokenPtr tmp;
3609
3610 exec->inputStackMax *= 2;
3611 tmp = (xmlRegInputTokenPtr) xmlRealloc(exec->inputStack,
3612 exec->inputStackMax * sizeof(xmlRegInputToken));
3613 if (tmp == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00003614 xmlRegexpErrMemory(NULL, "pushing input string");
Daniel Veillard4255d502002-04-16 15:50:10 +00003615 exec->inputStackMax /= 2;
3616 return;
3617 }
3618 exec->inputStack = tmp;
3619 }
3620 exec->inputStack[exec->inputStackNr].value = xmlStrdup(value);
3621 exec->inputStack[exec->inputStackNr].data = data;
3622 exec->inputStackNr++;
3623 exec->inputStack[exec->inputStackNr].value = NULL;
3624 exec->inputStack[exec->inputStackNr].data = NULL;
3625}
3626
Daniel Veillardc0826a72004-08-10 14:17:33 +00003627/**
3628 * xmlRegStrEqualWildcard:
Daniel Veillardf8e3db02012-09-11 13:26:36 +08003629 * @expStr: the string to be evaluated
Daniel Veillardc0826a72004-08-10 14:17:33 +00003630 * @valStr: the validation string
3631 *
3632 * Checks if both strings are equal or have the same content. "*"
Daniel Veillardf8e3db02012-09-11 13:26:36 +08003633 * can be used as a wildcard in @valStr; "|" is used as a seperator of
Daniel Veillardc0826a72004-08-10 14:17:33 +00003634 * substrings in both @expStr and @valStr.
3635 *
3636 * Returns 1 if the comparison is satisfied and the number of substrings
3637 * is equal, 0 otherwise.
3638 */
3639
3640static int
3641xmlRegStrEqualWildcard(const xmlChar *expStr, const xmlChar *valStr) {
3642 if (expStr == valStr) return(1);
3643 if (expStr == NULL) return(0);
3644 if (valStr == NULL) return(0);
3645 do {
3646 /*
3647 * Eval if we have a wildcard for the current item.
3648 */
3649 if (*expStr != *valStr) {
Daniel Veillard4f82c8a2005-08-09 21:40:08 +00003650 /* if one of them starts with a wildcard make valStr be it */
3651 if (*valStr == '*') {
3652 const xmlChar *tmp;
3653
3654 tmp = valStr;
3655 valStr = expStr;
3656 expStr = tmp;
3657 }
Daniel Veillardc0826a72004-08-10 14:17:33 +00003658 if ((*valStr != 0) && (*expStr != 0) && (*expStr++ == '*')) {
3659 do {
3660 if (*valStr == XML_REG_STRING_SEPARATOR)
3661 break;
Kasimier T. Buchcikc0e833f2005-04-19 15:02:20 +00003662 valStr++;
Daniel Veillardc0826a72004-08-10 14:17:33 +00003663 } while (*valStr != 0);
3664 continue;
3665 } else
3666 return(0);
3667 }
Kasimier T. Buchcikc0e833f2005-04-19 15:02:20 +00003668 expStr++;
3669 valStr++;
Daniel Veillardc0826a72004-08-10 14:17:33 +00003670 } while (*valStr != 0);
3671 if (*expStr != 0)
3672 return (0);
3673 else
3674 return (1);
3675}
Daniel Veillard4255d502002-04-16 15:50:10 +00003676
3677/**
Daniel Veillard23e73572002-09-19 19:56:43 +00003678 * xmlRegCompactPushString:
3679 * @exec: a regexp execution context
3680 * @comp: the precompiled exec with a compact table
3681 * @value: a string token input
3682 * @data: data associated to the token to reuse in callbacks
3683 *
3684 * Push one input token in the execution context
3685 *
3686 * Returns: 1 if the regexp reached a final state, 0 if non-final, and
3687 * a negative value in case of error.
3688 */
3689static int
3690xmlRegCompactPushString(xmlRegExecCtxtPtr exec,
3691 xmlRegexpPtr comp,
3692 const xmlChar *value,
3693 void *data) {
3694 int state = exec->index;
3695 int i, target;
3696
3697 if ((comp == NULL) || (comp->compact == NULL) || (comp->stringMap == NULL))
3698 return(-1);
Daniel Veillardf8e3db02012-09-11 13:26:36 +08003699
Daniel Veillard23e73572002-09-19 19:56:43 +00003700 if (value == NULL) {
3701 /*
3702 * are we at a final state ?
3703 */
3704 if (comp->compact[state * (comp->nbstrings + 1)] ==
3705 XML_REGEXP_FINAL_STATE)
3706 return(1);
3707 return(0);
3708 }
3709
3710#ifdef DEBUG_PUSH
3711 printf("value pushed: %s\n", value);
3712#endif
3713
3714 /*
William M. Brackddf71d62004-05-06 04:17:26 +00003715 * Examine all outside transitions from current state
Daniel Veillard23e73572002-09-19 19:56:43 +00003716 */
3717 for (i = 0;i < comp->nbstrings;i++) {
3718 target = comp->compact[state * (comp->nbstrings + 1) + i + 1];
3719 if ((target > 0) && (target <= comp->nbstates)) {
Daniel Veillardf8e3db02012-09-11 13:26:36 +08003720 target--; /* to avoid 0 */
Daniel Veillardc0826a72004-08-10 14:17:33 +00003721 if (xmlRegStrEqualWildcard(comp->stringMap[i], value)) {
Daniel Veillardf8e3db02012-09-11 13:26:36 +08003722 exec->index = target;
Daniel Veillard118aed72002-09-24 14:13:13 +00003723 if ((exec->callback != NULL) && (comp->transdata != NULL)) {
3724 exec->callback(exec->data, value,
3725 comp->transdata[state * comp->nbstrings + i], data);
3726 }
Daniel Veillard23e73572002-09-19 19:56:43 +00003727#ifdef DEBUG_PUSH
3728 printf("entering state %d\n", target);
3729#endif
3730 if (comp->compact[target * (comp->nbstrings + 1)] ==
Daniel Veillardcc026dc2005-01-12 13:21:17 +00003731 XML_REGEXP_SINK_STATE)
3732 goto error;
3733
3734 if (comp->compact[target * (comp->nbstrings + 1)] ==
Daniel Veillard23e73572002-09-19 19:56:43 +00003735 XML_REGEXP_FINAL_STATE)
3736 return(1);
3737 return(0);
3738 }
3739 }
3740 }
3741 /*
3742 * Failed to find an exit transition out from current state for the
3743 * current token
3744 */
3745#ifdef DEBUG_PUSH
3746 printf("failed to find a transition for %s on state %d\n", value, state);
3747#endif
Daniel Veillardcc026dc2005-01-12 13:21:17 +00003748error:
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003749 if (exec->errString != NULL)
3750 xmlFree(exec->errString);
3751 exec->errString = xmlStrdup(value);
3752 exec->errStateNo = state;
Daniel Veillard23e73572002-09-19 19:56:43 +00003753 exec->status = -1;
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003754#ifdef DEBUG_ERR
3755 testerr(exec);
3756#endif
Daniel Veillard23e73572002-09-19 19:56:43 +00003757 return(-1);
3758}
3759
3760/**
Daniel Veillard6e65e152005-08-09 11:09:52 +00003761 * xmlRegExecPushStringInternal:
Daniel Veillardea7751d2002-12-20 00:16:24 +00003762 * @exec: a regexp execution context or NULL to indicate the end
Daniel Veillard4255d502002-04-16 15:50:10 +00003763 * @value: a string token input
3764 * @data: data associated to the token to reuse in callbacks
Daniel Veillard6e65e152005-08-09 11:09:52 +00003765 * @compound: value was assembled from 2 strings
Daniel Veillard4255d502002-04-16 15:50:10 +00003766 *
3767 * Push one input token in the execution context
3768 *
3769 * Returns: 1 if the regexp reached a final state, 0 if non-final, and
3770 * a negative value in case of error.
3771 */
Daniel Veillard6e65e152005-08-09 11:09:52 +00003772static int
3773xmlRegExecPushStringInternal(xmlRegExecCtxtPtr exec, const xmlChar *value,
3774 void *data, int compound) {
Daniel Veillard4255d502002-04-16 15:50:10 +00003775 xmlRegTransPtr trans;
3776 xmlRegAtomPtr atom;
3777 int ret;
3778 int final = 0;
Daniel Veillard90700152005-01-08 22:05:09 +00003779 int progress = 1;
Daniel Veillard4255d502002-04-16 15:50:10 +00003780
3781 if (exec == NULL)
3782 return(-1);
Daniel Veillard23e73572002-09-19 19:56:43 +00003783 if (exec->comp == NULL)
3784 return(-1);
Daniel Veillard4255d502002-04-16 15:50:10 +00003785 if (exec->status != 0)
3786 return(exec->status);
3787
Daniel Veillard23e73572002-09-19 19:56:43 +00003788 if (exec->comp->compact != NULL)
3789 return(xmlRegCompactPushString(exec, exec->comp, value, data));
3790
Daniel Veillard4255d502002-04-16 15:50:10 +00003791 if (value == NULL) {
3792 if (exec->state->type == XML_REGEXP_FINAL_STATE)
3793 return(1);
3794 final = 1;
3795 }
3796
3797#ifdef DEBUG_PUSH
3798 printf("value pushed: %s\n", value);
3799#endif
3800 /*
3801 * If we have an active rollback stack push the new value there
3802 * and get back to where we were left
3803 */
3804 if ((value != NULL) && (exec->inputStackNr > 0)) {
3805 xmlFARegExecSaveInputString(exec, value, data);
3806 value = exec->inputStack[exec->index].value;
3807 data = exec->inputStack[exec->index].data;
3808#ifdef DEBUG_PUSH
3809 printf("value loaded: %s\n", value);
3810#endif
3811 }
3812
3813 while ((exec->status == 0) &&
3814 ((value != NULL) ||
3815 ((final == 1) &&
3816 (exec->state->type != XML_REGEXP_FINAL_STATE)))) {
3817
3818 /*
3819 * End of input on non-terminal state, rollback, however we may
3820 * still have epsilon like transition for counted transitions
3821 * on counters, in that case don't break too early.
3822 */
Daniel Veillardb509f152002-04-17 16:28:10 +00003823 if ((value == NULL) && (exec->counts == NULL))
Daniel Veillard4255d502002-04-16 15:50:10 +00003824 goto rollback;
3825
3826 exec->transcount = 0;
3827 for (;exec->transno < exec->state->nbTrans;exec->transno++) {
3828 trans = &exec->state->trans[exec->transno];
3829 if (trans->to < 0)
3830 continue;
3831 atom = trans->atom;
3832 ret = 0;
Daniel Veillard441bc322002-04-20 17:38:48 +00003833 if (trans->count == REGEXP_ALL_LAX_COUNTER) {
3834 int i;
3835 int count;
3836 xmlRegTransPtr t;
3837 xmlRegCounterPtr counter;
3838
3839 ret = 0;
3840
3841#ifdef DEBUG_PUSH
3842 printf("testing all lax %d\n", trans->count);
3843#endif
3844 /*
3845 * Check all counted transitions from the current state
3846 */
3847 if ((value == NULL) && (final)) {
3848 ret = 1;
3849 } else if (value != NULL) {
3850 for (i = 0;i < exec->state->nbTrans;i++) {
3851 t = &exec->state->trans[i];
3852 if ((t->counter < 0) || (t == trans))
3853 continue;
3854 counter = &exec->comp->counters[t->counter];
3855 count = exec->counts[t->counter];
Daniel Veillardf8e3db02012-09-11 13:26:36 +08003856 if ((count < counter->max) &&
Daniel Veillard441bc322002-04-20 17:38:48 +00003857 (t->atom != NULL) &&
3858 (xmlStrEqual(value, t->atom->valuep))) {
3859 ret = 0;
3860 break;
3861 }
3862 if ((count >= counter->min) &&
3863 (count < counter->max) &&
Daniel Veillard11ce4002006-03-10 00:36:23 +00003864 (t->atom != NULL) &&
Daniel Veillard441bc322002-04-20 17:38:48 +00003865 (xmlStrEqual(value, t->atom->valuep))) {
3866 ret = 1;
3867 break;
3868 }
3869 }
3870 }
3871 } else if (trans->count == REGEXP_ALL_COUNTER) {
Daniel Veillard8a001f62002-04-20 07:24:11 +00003872 int i;
3873 int count;
3874 xmlRegTransPtr t;
3875 xmlRegCounterPtr counter;
3876
3877 ret = 1;
3878
3879#ifdef DEBUG_PUSH
3880 printf("testing all %d\n", trans->count);
3881#endif
3882 /*
3883 * Check all counted transitions from the current state
3884 */
3885 for (i = 0;i < exec->state->nbTrans;i++) {
3886 t = &exec->state->trans[i];
3887 if ((t->counter < 0) || (t == trans))
3888 continue;
3889 counter = &exec->comp->counters[t->counter];
3890 count = exec->counts[t->counter];
3891 if ((count < counter->min) || (count > counter->max)) {
3892 ret = 0;
3893 break;
3894 }
3895 }
3896 } else if (trans->count >= 0) {
Daniel Veillard4255d502002-04-16 15:50:10 +00003897 int count;
3898 xmlRegCounterPtr counter;
3899
3900 /*
3901 * A counted transition.
3902 */
3903
3904 count = exec->counts[trans->count];
3905 counter = &exec->comp->counters[trans->count];
3906#ifdef DEBUG_PUSH
3907 printf("testing count %d: val %d, min %d, max %d\n",
3908 trans->count, count, counter->min, counter->max);
3909#endif
3910 ret = ((count >= counter->min) && (count <= counter->max));
3911 } else if (atom == NULL) {
3912 fprintf(stderr, "epsilon transition left at runtime\n");
3913 exec->status = -2;
3914 break;
3915 } else if (value != NULL) {
Daniel Veillardc0826a72004-08-10 14:17:33 +00003916 ret = xmlRegStrEqualWildcard(atom->valuep, value);
Daniel Veillard6e65e152005-08-09 11:09:52 +00003917 if (atom->neg) {
Daniel Veillard9efc4762005-07-19 14:33:55 +00003918 ret = !ret;
Daniel Veillard6e65e152005-08-09 11:09:52 +00003919 if (!compound)
3920 ret = 0;
3921 }
Daniel Veillard441bc322002-04-20 17:38:48 +00003922 if ((ret == 1) && (trans->counter >= 0)) {
3923 xmlRegCounterPtr counter;
3924 int count;
3925
3926 count = exec->counts[trans->counter];
3927 counter = &exec->comp->counters[trans->counter];
3928 if (count >= counter->max)
3929 ret = 0;
3930 }
3931
Daniel Veillard4255d502002-04-16 15:50:10 +00003932 if ((ret == 1) && (atom->min > 0) && (atom->max > 0)) {
3933 xmlRegStatePtr to = exec->comp->states[trans->to];
3934
3935 /*
3936 * this is a multiple input sequence
3937 */
3938 if (exec->state->nbTrans > exec->transno + 1) {
3939 if (exec->inputStackNr <= 0) {
3940 xmlFARegExecSaveInputString(exec, value, data);
3941 }
3942 xmlFARegExecSave(exec);
3943 }
3944 exec->transcount = 1;
3945 do {
3946 /*
3947 * Try to progress as much as possible on the input
3948 */
3949 if (exec->transcount == atom->max) {
3950 break;
3951 }
3952 exec->index++;
3953 value = exec->inputStack[exec->index].value;
3954 data = exec->inputStack[exec->index].data;
3955#ifdef DEBUG_PUSH
3956 printf("value loaded: %s\n", value);
3957#endif
3958
3959 /*
3960 * End of input: stop here
3961 */
3962 if (value == NULL) {
3963 exec->index --;
3964 break;
3965 }
3966 if (exec->transcount >= atom->min) {
3967 int transno = exec->transno;
3968 xmlRegStatePtr state = exec->state;
3969
3970 /*
3971 * The transition is acceptable save it
3972 */
3973 exec->transno = -1; /* trick */
3974 exec->state = to;
3975 if (exec->inputStackNr <= 0) {
3976 xmlFARegExecSaveInputString(exec, value, data);
3977 }
3978 xmlFARegExecSave(exec);
3979 exec->transno = transno;
3980 exec->state = state;
3981 }
3982 ret = xmlStrEqual(value, atom->valuep);
3983 exec->transcount++;
3984 } while (ret == 1);
3985 if (exec->transcount < atom->min)
3986 ret = 0;
3987
3988 /*
3989 * If the last check failed but one transition was found
3990 * possible, rollback
3991 */
3992 if (ret < 0)
3993 ret = 0;
3994 if (ret == 0) {
3995 goto rollback;
3996 }
3997 }
3998 }
3999 if (ret == 1) {
William M. Brack98873952003-12-26 06:03:14 +00004000 if ((exec->callback != NULL) && (atom != NULL) &&
4001 (data != NULL)) {
Daniel Veillard4255d502002-04-16 15:50:10 +00004002 exec->callback(exec->data, atom->valuep,
4003 atom->data, data);
4004 }
4005 if (exec->state->nbTrans > exec->transno + 1) {
4006 if (exec->inputStackNr <= 0) {
4007 xmlFARegExecSaveInputString(exec, value, data);
4008 }
4009 xmlFARegExecSave(exec);
4010 }
4011 if (trans->counter >= 0) {
4012#ifdef DEBUG_PUSH
4013 printf("Increasing count %d\n", trans->counter);
4014#endif
4015 exec->counts[trans->counter]++;
4016 }
Daniel Veillard10752282005-08-08 13:05:13 +00004017 if ((trans->count >= 0) &&
4018 (trans->count < REGEXP_ALL_COUNTER)) {
4019#ifdef DEBUG_REGEXP_EXEC
4020 printf("resetting count %d on transition\n",
4021 trans->count);
4022#endif
4023 exec->counts[trans->count] = 0;
4024 }
Daniel Veillard4255d502002-04-16 15:50:10 +00004025#ifdef DEBUG_PUSH
4026 printf("entering state %d\n", trans->to);
4027#endif
Daniel Veillardcc026dc2005-01-12 13:21:17 +00004028 if ((exec->comp->states[trans->to] != NULL) &&
4029 (exec->comp->states[trans->to]->type ==
4030 XML_REGEXP_SINK_STATE)) {
4031 /*
4032 * entering a sink state, save the current state as error
4033 * state.
4034 */
4035 if (exec->errString != NULL)
4036 xmlFree(exec->errString);
4037 exec->errString = xmlStrdup(value);
4038 exec->errState = exec->state;
4039 memcpy(exec->errCounts, exec->counts,
4040 exec->comp->nbCounters * sizeof(int));
4041 }
Daniel Veillard4255d502002-04-16 15:50:10 +00004042 exec->state = exec->comp->states[trans->to];
4043 exec->transno = 0;
4044 if (trans->atom != NULL) {
4045 if (exec->inputStack != NULL) {
4046 exec->index++;
4047 if (exec->index < exec->inputStackNr) {
4048 value = exec->inputStack[exec->index].value;
4049 data = exec->inputStack[exec->index].data;
4050#ifdef DEBUG_PUSH
4051 printf("value loaded: %s\n", value);
4052#endif
4053 } else {
4054 value = NULL;
4055 data = NULL;
4056#ifdef DEBUG_PUSH
4057 printf("end of input\n");
4058#endif
4059 }
4060 } else {
4061 value = NULL;
4062 data = NULL;
4063#ifdef DEBUG_PUSH
4064 printf("end of input\n");
4065#endif
4066 }
4067 }
4068 goto progress;
4069 } else if (ret < 0) {
4070 exec->status = -4;
4071 break;
4072 }
4073 }
4074 if ((exec->transno != 0) || (exec->state->nbTrans == 0)) {
4075rollback:
Daniel Veillard90700152005-01-08 22:05:09 +00004076 /*
Daniel Veillardcc026dc2005-01-12 13:21:17 +00004077 * if we didn't yet rollback on the current input
4078 * store the current state as the error state.
Daniel Veillard90700152005-01-08 22:05:09 +00004079 */
Daniel Veillardcc026dc2005-01-12 13:21:17 +00004080 if ((progress) && (exec->state != NULL) &&
4081 (exec->state->type != XML_REGEXP_SINK_STATE)) {
Daniel Veillard90700152005-01-08 22:05:09 +00004082 progress = 0;
4083 if (exec->errString != NULL)
4084 xmlFree(exec->errString);
4085 exec->errString = xmlStrdup(value);
4086 exec->errState = exec->state;
4087 memcpy(exec->errCounts, exec->counts,
4088 exec->comp->nbCounters * sizeof(int));
4089 }
4090
Daniel Veillard4255d502002-04-16 15:50:10 +00004091 /*
4092 * Failed to find a way out
4093 */
4094 exec->determinist = 0;
4095 xmlFARegExecRollBack(exec);
Gaurav2671b012013-09-11 14:59:06 +08004096 if ((exec->inputStack != NULL ) && (exec->status == 0)) {
Daniel Veillard4255d502002-04-16 15:50:10 +00004097 value = exec->inputStack[exec->index].value;
4098 data = exec->inputStack[exec->index].data;
4099#ifdef DEBUG_PUSH
4100 printf("value loaded: %s\n", value);
4101#endif
4102 }
4103 }
Daniel Veillard90700152005-01-08 22:05:09 +00004104 continue;
Daniel Veillard4255d502002-04-16 15:50:10 +00004105progress:
Daniel Veillard90700152005-01-08 22:05:09 +00004106 progress = 1;
Daniel Veillard4255d502002-04-16 15:50:10 +00004107 continue;
4108 }
4109 if (exec->status == 0) {
4110 return(exec->state->type == XML_REGEXP_FINAL_STATE);
4111 }
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00004112#ifdef DEBUG_ERR
Daniel Veillard90700152005-01-08 22:05:09 +00004113 if (exec->status < 0) {
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00004114 testerr(exec);
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00004115 }
Daniel Veillard90700152005-01-08 22:05:09 +00004116#endif
Daniel Veillard4255d502002-04-16 15:50:10 +00004117 return(exec->status);
4118}
4119
Daniel Veillard52b48c72003-04-13 19:53:42 +00004120/**
Daniel Veillard6e65e152005-08-09 11:09:52 +00004121 * xmlRegExecPushString:
4122 * @exec: a regexp execution context or NULL to indicate the end
4123 * @value: a string token input
4124 * @data: data associated to the token to reuse in callbacks
4125 *
4126 * Push one input token in the execution context
4127 *
4128 * Returns: 1 if the regexp reached a final state, 0 if non-final, and
4129 * a negative value in case of error.
4130 */
4131int
4132xmlRegExecPushString(xmlRegExecCtxtPtr exec, const xmlChar *value,
4133 void *data) {
4134 return(xmlRegExecPushStringInternal(exec, value, data, 0));
4135}
4136
4137/**
Daniel Veillard52b48c72003-04-13 19:53:42 +00004138 * xmlRegExecPushString2:
4139 * @exec: a regexp execution context or NULL to indicate the end
4140 * @value: the first string token input
4141 * @value2: the second string token input
4142 * @data: data associated to the token to reuse in callbacks
4143 *
4144 * Push one input token in the execution context
4145 *
4146 * Returns: 1 if the regexp reached a final state, 0 if non-final, and
4147 * a negative value in case of error.
4148 */
4149int
4150xmlRegExecPushString2(xmlRegExecCtxtPtr exec, const xmlChar *value,
4151 const xmlChar *value2, void *data) {
4152 xmlChar buf[150];
4153 int lenn, lenp, ret;
4154 xmlChar *str;
4155
4156 if (exec == NULL)
4157 return(-1);
4158 if (exec->comp == NULL)
4159 return(-1);
4160 if (exec->status != 0)
4161 return(exec->status);
4162
4163 if (value2 == NULL)
4164 return(xmlRegExecPushString(exec, value, data));
4165
4166 lenn = strlen((char *) value2);
4167 lenp = strlen((char *) value);
4168
4169 if (150 < lenn + lenp + 2) {
Daniel Veillard3c908dc2003-04-19 00:07:51 +00004170 str = (xmlChar *) xmlMallocAtomic(lenn + lenp + 2);
Daniel Veillard52b48c72003-04-13 19:53:42 +00004171 if (str == NULL) {
4172 exec->status = -1;
4173 return(-1);
4174 }
4175 } else {
4176 str = buf;
4177 }
4178 memcpy(&str[0], value, lenp);
Daniel Veillardc0826a72004-08-10 14:17:33 +00004179 str[lenp] = XML_REG_STRING_SEPARATOR;
Daniel Veillard52b48c72003-04-13 19:53:42 +00004180 memcpy(&str[lenp + 1], value2, lenn);
4181 str[lenn + lenp + 1] = 0;
4182
4183 if (exec->comp->compact != NULL)
4184 ret = xmlRegCompactPushString(exec, exec->comp, str, data);
4185 else
Daniel Veillard6e65e152005-08-09 11:09:52 +00004186 ret = xmlRegExecPushStringInternal(exec, str, data, 1);
Daniel Veillard52b48c72003-04-13 19:53:42 +00004187
4188 if (str != buf)
Daniel Veillard0b1ff142005-12-28 21:13:33 +00004189 xmlFree(str);
Daniel Veillard52b48c72003-04-13 19:53:42 +00004190 return(ret);
4191}
4192
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00004193/**
Daniel Veillard77005e62005-07-19 16:26:18 +00004194 * xmlRegExecGetValues:
Daniel Veillardfc0b6f62005-01-09 17:48:02 +00004195 * @exec: a regexp execution context
4196 * @err: error extraction or normal one
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00004197 * @nbval: pointer to the number of accepted values IN/OUT
Daniel Veillardcc026dc2005-01-12 13:21:17 +00004198 * @nbneg: return number of negative transitions
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00004199 * @values: pointer to the array of acceptable values
Daniel Veillardfc0b6f62005-01-09 17:48:02 +00004200 * @terminal: return value if this was a terminal state
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00004201 *
Daniel Veillardfc0b6f62005-01-09 17:48:02 +00004202 * Extract informations from the regexp execution, internal routine to
4203 * implement xmlRegExecNextValues() and xmlRegExecErrInfo()
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00004204 *
4205 * Returns: 0 in case of success or -1 in case of error.
4206 */
Daniel Veillardfc0b6f62005-01-09 17:48:02 +00004207static int
4208xmlRegExecGetValues(xmlRegExecCtxtPtr exec, int err,
Daniel Veillardcc026dc2005-01-12 13:21:17 +00004209 int *nbval, int *nbneg,
4210 xmlChar **values, int *terminal) {
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00004211 int maxval;
Daniel Veillardcc026dc2005-01-12 13:21:17 +00004212 int nb = 0;
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00004213
Daniel Veillardf8e3db02012-09-11 13:26:36 +08004214 if ((exec == NULL) || (nbval == NULL) || (nbneg == NULL) ||
Daniel Veillardcc026dc2005-01-12 13:21:17 +00004215 (values == NULL) || (*nbval <= 0))
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00004216 return(-1);
Daniel Veillardfc0b6f62005-01-09 17:48:02 +00004217
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00004218 maxval = *nbval;
4219 *nbval = 0;
Daniel Veillardcc026dc2005-01-12 13:21:17 +00004220 *nbneg = 0;
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00004221 if ((exec->comp != NULL) && (exec->comp->compact != NULL)) {
4222 xmlRegexpPtr comp;
4223 int target, i, state;
4224
4225 comp = exec->comp;
Daniel Veillardfc0b6f62005-01-09 17:48:02 +00004226
4227 if (err) {
4228 if (exec->errStateNo == -1) return(-1);
4229 state = exec->errStateNo;
4230 } else {
4231 state = exec->index;
4232 }
4233 if (terminal != NULL) {
4234 if (comp->compact[state * (comp->nbstrings + 1)] ==
4235 XML_REGEXP_FINAL_STATE)
4236 *terminal = 1;
4237 else
4238 *terminal = 0;
4239 }
Daniel Veillardcc026dc2005-01-12 13:21:17 +00004240 for (i = 0;(i < comp->nbstrings) && (nb < maxval);i++) {
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00004241 target = comp->compact[state * (comp->nbstrings + 1) + i + 1];
Daniel Veillardcc026dc2005-01-12 13:21:17 +00004242 if ((target > 0) && (target <= comp->nbstates) &&
4243 (comp->compact[(target - 1) * (comp->nbstrings + 1)] !=
4244 XML_REGEXP_SINK_STATE)) {
4245 values[nb++] = comp->stringMap[i];
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00004246 (*nbval)++;
4247 }
4248 }
Daniel Veillardcc026dc2005-01-12 13:21:17 +00004249 for (i = 0;(i < comp->nbstrings) && (nb < maxval);i++) {
4250 target = comp->compact[state * (comp->nbstrings + 1) + i + 1];
4251 if ((target > 0) && (target <= comp->nbstates) &&
4252 (comp->compact[(target - 1) * (comp->nbstrings + 1)] ==
4253 XML_REGEXP_SINK_STATE)) {
4254 values[nb++] = comp->stringMap[i];
4255 (*nbneg)++;
4256 }
4257 }
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00004258 } else {
4259 int transno;
4260 xmlRegTransPtr trans;
4261 xmlRegAtomPtr atom;
Daniel Veillardfc0b6f62005-01-09 17:48:02 +00004262 xmlRegStatePtr state;
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00004263
Daniel Veillardfc0b6f62005-01-09 17:48:02 +00004264 if (terminal != NULL) {
4265 if (exec->state->type == XML_REGEXP_FINAL_STATE)
4266 *terminal = 1;
4267 else
4268 *terminal = 0;
4269 }
4270
4271 if (err) {
4272 if (exec->errState == NULL) return(-1);
4273 state = exec->errState;
4274 } else {
4275 if (exec->state == NULL) return(-1);
4276 state = exec->state;
4277 }
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00004278 for (transno = 0;
Daniel Veillardcc026dc2005-01-12 13:21:17 +00004279 (transno < state->nbTrans) && (nb < maxval);
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00004280 transno++) {
Daniel Veillardfc0b6f62005-01-09 17:48:02 +00004281 trans = &state->trans[transno];
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00004282 if (trans->to < 0)
4283 continue;
4284 atom = trans->atom;
4285 if ((atom == NULL) || (atom->valuep == NULL))
4286 continue;
4287 if (trans->count == REGEXP_ALL_LAX_COUNTER) {
Daniel Veillardcc026dc2005-01-12 13:21:17 +00004288 /* this should not be reached but ... */
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00004289 TODO;
4290 } else if (trans->count == REGEXP_ALL_COUNTER) {
Daniel Veillardcc026dc2005-01-12 13:21:17 +00004291 /* this should not be reached but ... */
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00004292 TODO;
4293 } else if (trans->counter >= 0) {
Daniel Veillard11ce4002006-03-10 00:36:23 +00004294 xmlRegCounterPtr counter = NULL;
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00004295 int count;
4296
Daniel Veillardfc0b6f62005-01-09 17:48:02 +00004297 if (err)
4298 count = exec->errCounts[trans->counter];
4299 else
4300 count = exec->counts[trans->counter];
Daniel Veillard11ce4002006-03-10 00:36:23 +00004301 if (exec->comp != NULL)
4302 counter = &exec->comp->counters[trans->counter];
4303 if ((counter == NULL) || (count < counter->max)) {
Daniel Veillard77005e62005-07-19 16:26:18 +00004304 if (atom->neg)
4305 values[nb++] = (xmlChar *) atom->valuep2;
4306 else
4307 values[nb++] = (xmlChar *) atom->valuep;
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00004308 (*nbval)++;
4309 }
4310 } else {
Gaurav2671b012013-09-11 14:59:06 +08004311 if ((exec->comp != NULL) && (exec->comp->states[trans->to] != NULL) &&
Daniel Veillardcc026dc2005-01-12 13:21:17 +00004312 (exec->comp->states[trans->to]->type !=
4313 XML_REGEXP_SINK_STATE)) {
Daniel Veillard77005e62005-07-19 16:26:18 +00004314 if (atom->neg)
4315 values[nb++] = (xmlChar *) atom->valuep2;
4316 else
4317 values[nb++] = (xmlChar *) atom->valuep;
Daniel Veillardcc026dc2005-01-12 13:21:17 +00004318 (*nbval)++;
4319 }
Daniel Veillardf8e3db02012-09-11 13:26:36 +08004320 }
Daniel Veillardcc026dc2005-01-12 13:21:17 +00004321 }
4322 for (transno = 0;
4323 (transno < state->nbTrans) && (nb < maxval);
4324 transno++) {
4325 trans = &state->trans[transno];
4326 if (trans->to < 0)
4327 continue;
4328 atom = trans->atom;
4329 if ((atom == NULL) || (atom->valuep == NULL))
4330 continue;
4331 if (trans->count == REGEXP_ALL_LAX_COUNTER) {
4332 continue;
4333 } else if (trans->count == REGEXP_ALL_COUNTER) {
4334 continue;
4335 } else if (trans->counter >= 0) {
4336 continue;
4337 } else {
4338 if ((exec->comp->states[trans->to] != NULL) &&
4339 (exec->comp->states[trans->to]->type ==
4340 XML_REGEXP_SINK_STATE)) {
Daniel Veillard77005e62005-07-19 16:26:18 +00004341 if (atom->neg)
4342 values[nb++] = (xmlChar *) atom->valuep2;
4343 else
4344 values[nb++] = (xmlChar *) atom->valuep;
Daniel Veillardcc026dc2005-01-12 13:21:17 +00004345 (*nbneg)++;
4346 }
Daniel Veillardf8e3db02012-09-11 13:26:36 +08004347 }
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00004348 }
4349 }
4350 return(0);
4351}
4352
Daniel Veillardfc0b6f62005-01-09 17:48:02 +00004353/**
4354 * xmlRegExecNextValues:
4355 * @exec: a regexp execution context
4356 * @nbval: pointer to the number of accepted values IN/OUT
Daniel Veillardcc026dc2005-01-12 13:21:17 +00004357 * @nbneg: return number of negative transitions
Daniel Veillardfc0b6f62005-01-09 17:48:02 +00004358 * @values: pointer to the array of acceptable values
4359 * @terminal: return value if this was a terminal state
4360 *
4361 * Extract informations from the regexp execution,
4362 * the parameter @values must point to an array of @nbval string pointers
4363 * on return nbval will contain the number of possible strings in that
4364 * state and the @values array will be updated with them. The string values
4365 * returned will be freed with the @exec context and don't need to be
4366 * deallocated.
4367 *
4368 * Returns: 0 in case of success or -1 in case of error.
4369 */
4370int
Daniel Veillardcc026dc2005-01-12 13:21:17 +00004371xmlRegExecNextValues(xmlRegExecCtxtPtr exec, int *nbval, int *nbneg,
4372 xmlChar **values, int *terminal) {
4373 return(xmlRegExecGetValues(exec, 0, nbval, nbneg, values, terminal));
Daniel Veillardfc0b6f62005-01-09 17:48:02 +00004374}
4375
4376/**
4377 * xmlRegExecErrInfo:
4378 * @exec: a regexp execution context generating an error
4379 * @string: return value for the error string
4380 * @nbval: pointer to the number of accepted values IN/OUT
Daniel Veillardcc026dc2005-01-12 13:21:17 +00004381 * @nbneg: return number of negative transitions
Daniel Veillardfc0b6f62005-01-09 17:48:02 +00004382 * @values: pointer to the array of acceptable values
4383 * @terminal: return value if this was a terminal state
4384 *
4385 * Extract error informations from the regexp execution, the parameter
4386 * @string will be updated with the value pushed and not accepted,
4387 * the parameter @values must point to an array of @nbval string pointers
4388 * on return nbval will contain the number of possible strings in that
4389 * state and the @values array will be updated with them. The string values
4390 * returned will be freed with the @exec context and don't need to be
4391 * deallocated.
4392 *
4393 * Returns: 0 in case of success or -1 in case of error.
4394 */
4395int
4396xmlRegExecErrInfo(xmlRegExecCtxtPtr exec, const xmlChar **string,
Daniel Veillardcc026dc2005-01-12 13:21:17 +00004397 int *nbval, int *nbneg, xmlChar **values, int *terminal) {
Daniel Veillardfc0b6f62005-01-09 17:48:02 +00004398 if (exec == NULL)
4399 return(-1);
4400 if (string != NULL) {
4401 if (exec->status != 0)
4402 *string = exec->errString;
4403 else
4404 *string = NULL;
4405 }
Daniel Veillardcc026dc2005-01-12 13:21:17 +00004406 return(xmlRegExecGetValues(exec, 1, nbval, nbneg, values, terminal));
Daniel Veillardfc0b6f62005-01-09 17:48:02 +00004407}
4408
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00004409#ifdef DEBUG_ERR
4410static void testerr(xmlRegExecCtxtPtr exec) {
4411 const xmlChar *string;
Daniel Veillardcee2b3a2005-01-25 00:22:52 +00004412 xmlChar *values[5];
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00004413 int nb = 5;
Daniel Veillardcc026dc2005-01-12 13:21:17 +00004414 int nbneg;
Daniel Veillardfc0b6f62005-01-09 17:48:02 +00004415 int terminal;
Daniel Veillardcc026dc2005-01-12 13:21:17 +00004416 xmlRegExecErrInfo(exec, &string, &nb, &nbneg, &values[0], &terminal);
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00004417}
4418#endif
4419
Daniel Veillard4255d502002-04-16 15:50:10 +00004420#if 0
4421static int
4422xmlRegExecPushChar(xmlRegExecCtxtPtr exec, int UCS) {
4423 xmlRegTransPtr trans;
4424 xmlRegAtomPtr atom;
4425 int ret;
4426 int codepoint, len;
4427
4428 if (exec == NULL)
4429 return(-1);
4430 if (exec->status != 0)
4431 return(exec->status);
4432
4433 while ((exec->status == 0) &&
4434 ((exec->inputString[exec->index] != 0) ||
4435 (exec->state->type != XML_REGEXP_FINAL_STATE))) {
4436
4437 /*
4438 * End of input on non-terminal state, rollback, however we may
4439 * still have epsilon like transition for counted transitions
4440 * on counters, in that case don't break too early.
4441 */
4442 if ((exec->inputString[exec->index] == 0) && (exec->counts == NULL))
4443 goto rollback;
4444
4445 exec->transcount = 0;
4446 for (;exec->transno < exec->state->nbTrans;exec->transno++) {
4447 trans = &exec->state->trans[exec->transno];
4448 if (trans->to < 0)
4449 continue;
4450 atom = trans->atom;
4451 ret = 0;
4452 if (trans->count >= 0) {
4453 int count;
4454 xmlRegCounterPtr counter;
4455
4456 /*
4457 * A counted transition.
4458 */
4459
4460 count = exec->counts[trans->count];
4461 counter = &exec->comp->counters[trans->count];
4462#ifdef DEBUG_REGEXP_EXEC
4463 printf("testing count %d: val %d, min %d, max %d\n",
4464 trans->count, count, counter->min, counter->max);
4465#endif
4466 ret = ((count >= counter->min) && (count <= counter->max));
4467 } else if (atom == NULL) {
4468 fprintf(stderr, "epsilon transition left at runtime\n");
4469 exec->status = -2;
4470 break;
4471 } else if (exec->inputString[exec->index] != 0) {
4472 codepoint = CUR_SCHAR(&(exec->inputString[exec->index]), len);
4473 ret = xmlRegCheckCharacter(atom, codepoint);
4474 if ((ret == 1) && (atom->min > 0) && (atom->max > 0)) {
4475 xmlRegStatePtr to = exec->comp->states[trans->to];
4476
4477 /*
4478 * this is a multiple input sequence
4479 */
4480 if (exec->state->nbTrans > exec->transno + 1) {
4481 xmlFARegExecSave(exec);
4482 }
4483 exec->transcount = 1;
4484 do {
4485 /*
4486 * Try to progress as much as possible on the input
4487 */
4488 if (exec->transcount == atom->max) {
4489 break;
4490 }
4491 exec->index += len;
4492 /*
4493 * End of input: stop here
4494 */
4495 if (exec->inputString[exec->index] == 0) {
4496 exec->index -= len;
4497 break;
4498 }
4499 if (exec->transcount >= atom->min) {
4500 int transno = exec->transno;
4501 xmlRegStatePtr state = exec->state;
4502
4503 /*
4504 * The transition is acceptable save it
4505 */
4506 exec->transno = -1; /* trick */
4507 exec->state = to;
4508 xmlFARegExecSave(exec);
4509 exec->transno = transno;
4510 exec->state = state;
4511 }
4512 codepoint = CUR_SCHAR(&(exec->inputString[exec->index]),
4513 len);
4514 ret = xmlRegCheckCharacter(atom, codepoint);
4515 exec->transcount++;
4516 } while (ret == 1);
4517 if (exec->transcount < atom->min)
4518 ret = 0;
4519
4520 /*
4521 * If the last check failed but one transition was found
4522 * possible, rollback
4523 */
4524 if (ret < 0)
4525 ret = 0;
4526 if (ret == 0) {
4527 goto rollback;
4528 }
4529 }
4530 }
4531 if (ret == 1) {
4532 if (exec->state->nbTrans > exec->transno + 1) {
4533 xmlFARegExecSave(exec);
4534 }
Daniel Veillard54eb0242006-03-21 23:17:57 +00004535 /*
4536 * restart count for expressions like this ((abc){2})*
4537 */
4538 if (trans->count >= 0) {
4539#ifdef DEBUG_REGEXP_EXEC
4540 printf("Reset count %d\n", trans->count);
4541#endif
4542 exec->counts[trans->count] = 0;
4543 }
Daniel Veillard4255d502002-04-16 15:50:10 +00004544 if (trans->counter >= 0) {
4545#ifdef DEBUG_REGEXP_EXEC
4546 printf("Increasing count %d\n", trans->counter);
4547#endif
4548 exec->counts[trans->counter]++;
4549 }
4550#ifdef DEBUG_REGEXP_EXEC
4551 printf("entering state %d\n", trans->to);
4552#endif
4553 exec->state = exec->comp->states[trans->to];
4554 exec->transno = 0;
4555 if (trans->atom != NULL) {
4556 exec->index += len;
4557 }
4558 goto progress;
4559 } else if (ret < 0) {
4560 exec->status = -4;
4561 break;
4562 }
4563 }
4564 if ((exec->transno != 0) || (exec->state->nbTrans == 0)) {
4565rollback:
4566 /*
4567 * Failed to find a way out
4568 */
4569 exec->determinist = 0;
4570 xmlFARegExecRollBack(exec);
4571 }
4572progress:
4573 continue;
4574 }
4575}
4576#endif
4577/************************************************************************
Daniel Veillardf8e3db02012-09-11 13:26:36 +08004578 * *
William M. Brackddf71d62004-05-06 04:17:26 +00004579 * Parser for the Schemas Datatype Regular Expressions *
Daniel Veillard4255d502002-04-16 15:50:10 +00004580 * http://www.w3.org/TR/2001/REC-xmlschema-2-20010502/#regexs *
Daniel Veillardf8e3db02012-09-11 13:26:36 +08004581 * *
Daniel Veillard4255d502002-04-16 15:50:10 +00004582 ************************************************************************/
4583
4584/**
4585 * xmlFAIsChar:
Daniel Veillard441bc322002-04-20 17:38:48 +00004586 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00004587 *
4588 * [10] Char ::= [^.\?*+()|#x5B#x5D]
4589 */
4590static int
4591xmlFAIsChar(xmlRegParserCtxtPtr ctxt) {
4592 int cur;
4593 int len;
4594
4595 cur = CUR_SCHAR(ctxt->cur, len);
4596 if ((cur == '.') || (cur == '\\') || (cur == '?') ||
4597 (cur == '*') || (cur == '+') || (cur == '(') ||
4598 (cur == ')') || (cur == '|') || (cur == 0x5B) ||
4599 (cur == 0x5D) || (cur == 0))
4600 return(-1);
4601 return(cur);
4602}
4603
4604/**
4605 * xmlFAParseCharProp:
Daniel Veillard441bc322002-04-20 17:38:48 +00004606 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00004607 *
4608 * [27] charProp ::= IsCategory | IsBlock
4609 * [28] IsCategory ::= Letters | Marks | Numbers | Punctuation |
Daniel Veillardf8e3db02012-09-11 13:26:36 +08004610 * Separators | Symbols | Others
Daniel Veillard4255d502002-04-16 15:50:10 +00004611 * [29] Letters ::= 'L' [ultmo]?
4612 * [30] Marks ::= 'M' [nce]?
4613 * [31] Numbers ::= 'N' [dlo]?
4614 * [32] Punctuation ::= 'P' [cdseifo]?
4615 * [33] Separators ::= 'Z' [slp]?
4616 * [34] Symbols ::= 'S' [mcko]?
4617 * [35] Others ::= 'C' [cfon]?
4618 * [36] IsBlock ::= 'Is' [a-zA-Z0-9#x2D]+
4619 */
4620static void
4621xmlFAParseCharProp(xmlRegParserCtxtPtr ctxt) {
4622 int cur;
William M. Brack779af002003-08-01 15:55:39 +00004623 xmlRegAtomType type = (xmlRegAtomType) 0;
Daniel Veillard4255d502002-04-16 15:50:10 +00004624 xmlChar *blockName = NULL;
Daniel Veillardf8e3db02012-09-11 13:26:36 +08004625
Daniel Veillard4255d502002-04-16 15:50:10 +00004626 cur = CUR;
4627 if (cur == 'L') {
4628 NEXT;
4629 cur = CUR;
4630 if (cur == 'u') {
4631 NEXT;
4632 type = XML_REGEXP_LETTER_UPPERCASE;
4633 } else if (cur == 'l') {
4634 NEXT;
4635 type = XML_REGEXP_LETTER_LOWERCASE;
4636 } else if (cur == 't') {
4637 NEXT;
4638 type = XML_REGEXP_LETTER_TITLECASE;
4639 } else if (cur == 'm') {
4640 NEXT;
4641 type = XML_REGEXP_LETTER_MODIFIER;
4642 } else if (cur == 'o') {
4643 NEXT;
4644 type = XML_REGEXP_LETTER_OTHERS;
4645 } else {
4646 type = XML_REGEXP_LETTER;
4647 }
4648 } else if (cur == 'M') {
4649 NEXT;
4650 cur = CUR;
4651 if (cur == 'n') {
4652 NEXT;
4653 /* nonspacing */
4654 type = XML_REGEXP_MARK_NONSPACING;
4655 } else if (cur == 'c') {
4656 NEXT;
4657 /* spacing combining */
4658 type = XML_REGEXP_MARK_SPACECOMBINING;
4659 } else if (cur == 'e') {
4660 NEXT;
4661 /* enclosing */
4662 type = XML_REGEXP_MARK_ENCLOSING;
4663 } else {
4664 /* all marks */
4665 type = XML_REGEXP_MARK;
4666 }
4667 } else if (cur == 'N') {
4668 NEXT;
4669 cur = CUR;
4670 if (cur == 'd') {
4671 NEXT;
4672 /* digital */
4673 type = XML_REGEXP_NUMBER_DECIMAL;
4674 } else if (cur == 'l') {
4675 NEXT;
4676 /* letter */
4677 type = XML_REGEXP_NUMBER_LETTER;
4678 } else if (cur == 'o') {
4679 NEXT;
4680 /* other */
4681 type = XML_REGEXP_NUMBER_OTHERS;
4682 } else {
4683 /* all numbers */
4684 type = XML_REGEXP_NUMBER;
4685 }
4686 } else if (cur == 'P') {
4687 NEXT;
4688 cur = CUR;
4689 if (cur == 'c') {
4690 NEXT;
4691 /* connector */
4692 type = XML_REGEXP_PUNCT_CONNECTOR;
4693 } else if (cur == 'd') {
4694 NEXT;
4695 /* dash */
4696 type = XML_REGEXP_PUNCT_DASH;
4697 } else if (cur == 's') {
4698 NEXT;
4699 /* open */
4700 type = XML_REGEXP_PUNCT_OPEN;
4701 } else if (cur == 'e') {
4702 NEXT;
4703 /* close */
4704 type = XML_REGEXP_PUNCT_CLOSE;
4705 } else if (cur == 'i') {
4706 NEXT;
4707 /* initial quote */
4708 type = XML_REGEXP_PUNCT_INITQUOTE;
4709 } else if (cur == 'f') {
4710 NEXT;
4711 /* final quote */
4712 type = XML_REGEXP_PUNCT_FINQUOTE;
4713 } else if (cur == 'o') {
4714 NEXT;
4715 /* other */
4716 type = XML_REGEXP_PUNCT_OTHERS;
4717 } else {
4718 /* all punctuation */
4719 type = XML_REGEXP_PUNCT;
4720 }
4721 } else if (cur == 'Z') {
4722 NEXT;
4723 cur = CUR;
4724 if (cur == 's') {
4725 NEXT;
4726 /* space */
4727 type = XML_REGEXP_SEPAR_SPACE;
4728 } else if (cur == 'l') {
4729 NEXT;
4730 /* line */
4731 type = XML_REGEXP_SEPAR_LINE;
4732 } else if (cur == 'p') {
4733 NEXT;
4734 /* paragraph */
4735 type = XML_REGEXP_SEPAR_PARA;
4736 } else {
4737 /* all separators */
4738 type = XML_REGEXP_SEPAR;
4739 }
4740 } else if (cur == 'S') {
4741 NEXT;
4742 cur = CUR;
4743 if (cur == 'm') {
4744 NEXT;
4745 type = XML_REGEXP_SYMBOL_MATH;
4746 /* math */
4747 } else if (cur == 'c') {
4748 NEXT;
4749 type = XML_REGEXP_SYMBOL_CURRENCY;
4750 /* currency */
4751 } else if (cur == 'k') {
4752 NEXT;
4753 type = XML_REGEXP_SYMBOL_MODIFIER;
4754 /* modifiers */
4755 } else if (cur == 'o') {
4756 NEXT;
4757 type = XML_REGEXP_SYMBOL_OTHERS;
4758 /* other */
4759 } else {
4760 /* all symbols */
4761 type = XML_REGEXP_SYMBOL;
4762 }
4763 } else if (cur == 'C') {
4764 NEXT;
4765 cur = CUR;
4766 if (cur == 'c') {
4767 NEXT;
4768 /* control */
4769 type = XML_REGEXP_OTHER_CONTROL;
4770 } else if (cur == 'f') {
4771 NEXT;
4772 /* format */
4773 type = XML_REGEXP_OTHER_FORMAT;
4774 } else if (cur == 'o') {
4775 NEXT;
4776 /* private use */
4777 type = XML_REGEXP_OTHER_PRIVATE;
4778 } else if (cur == 'n') {
4779 NEXT;
4780 /* not assigned */
4781 type = XML_REGEXP_OTHER_NA;
4782 } else {
4783 /* all others */
4784 type = XML_REGEXP_OTHER;
4785 }
4786 } else if (cur == 'I') {
4787 const xmlChar *start;
4788 NEXT;
4789 cur = CUR;
4790 if (cur != 's') {
4791 ERROR("IsXXXX expected");
4792 return;
4793 }
4794 NEXT;
4795 start = ctxt->cur;
4796 cur = CUR;
Daniel Veillardf8e3db02012-09-11 13:26:36 +08004797 if (((cur >= 'a') && (cur <= 'z')) ||
4798 ((cur >= 'A') && (cur <= 'Z')) ||
4799 ((cur >= '0') && (cur <= '9')) ||
Daniel Veillard4255d502002-04-16 15:50:10 +00004800 (cur == 0x2D)) {
4801 NEXT;
4802 cur = CUR;
Daniel Veillardf8e3db02012-09-11 13:26:36 +08004803 while (((cur >= 'a') && (cur <= 'z')) ||
4804 ((cur >= 'A') && (cur <= 'Z')) ||
4805 ((cur >= '0') && (cur <= '9')) ||
Daniel Veillard4255d502002-04-16 15:50:10 +00004806 (cur == 0x2D)) {
4807 NEXT;
4808 cur = CUR;
4809 }
4810 }
4811 type = XML_REGEXP_BLOCK_NAME;
4812 blockName = xmlStrndup(start, ctxt->cur - start);
4813 } else {
4814 ERROR("Unknown char property");
4815 return;
4816 }
4817 if (ctxt->atom == NULL) {
4818 ctxt->atom = xmlRegNewAtom(ctxt, type);
4819 if (ctxt->atom != NULL)
4820 ctxt->atom->valuep = blockName;
4821 } else if (ctxt->atom->type == XML_REGEXP_RANGES) {
4822 xmlRegAtomAddRange(ctxt, ctxt->atom, ctxt->neg,
4823 type, 0, 0, blockName);
4824 }
4825}
4826
4827/**
4828 * xmlFAParseCharClassEsc:
Daniel Veillard441bc322002-04-20 17:38:48 +00004829 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00004830 *
Daniel Veillardf8e3db02012-09-11 13:26:36 +08004831 * [23] charClassEsc ::= ( SingleCharEsc | MultiCharEsc | catEsc | complEsc )
Daniel Veillard4255d502002-04-16 15:50:10 +00004832 * [24] SingleCharEsc ::= '\' [nrt\|.?*+(){}#x2D#x5B#x5D#x5E]
4833 * [25] catEsc ::= '\p{' charProp '}'
4834 * [26] complEsc ::= '\P{' charProp '}'
4835 * [37] MultiCharEsc ::= '.' | ('\' [sSiIcCdDwW])
4836 */
4837static void
4838xmlFAParseCharClassEsc(xmlRegParserCtxtPtr ctxt) {
4839 int cur;
4840
4841 if (CUR == '.') {
4842 if (ctxt->atom == NULL) {
4843 ctxt->atom = xmlRegNewAtom(ctxt, XML_REGEXP_ANYCHAR);
4844 } else if (ctxt->atom->type == XML_REGEXP_RANGES) {
4845 xmlRegAtomAddRange(ctxt, ctxt->atom, ctxt->neg,
4846 XML_REGEXP_ANYCHAR, 0, 0, NULL);
4847 }
4848 NEXT;
4849 return;
4850 }
4851 if (CUR != '\\') {
4852 ERROR("Escaped sequence: expecting \\");
4853 return;
4854 }
4855 NEXT;
4856 cur = CUR;
4857 if (cur == 'p') {
4858 NEXT;
4859 if (CUR != '{') {
4860 ERROR("Expecting '{'");
4861 return;
4862 }
4863 NEXT;
4864 xmlFAParseCharProp(ctxt);
4865 if (CUR != '}') {
4866 ERROR("Expecting '}'");
4867 return;
4868 }
4869 NEXT;
4870 } else if (cur == 'P') {
4871 NEXT;
4872 if (CUR != '{') {
4873 ERROR("Expecting '{'");
4874 return;
4875 }
4876 NEXT;
4877 xmlFAParseCharProp(ctxt);
4878 ctxt->atom->neg = 1;
4879 if (CUR != '}') {
4880 ERROR("Expecting '}'");
4881 return;
4882 }
4883 NEXT;
4884 } else if ((cur == 'n') || (cur == 'r') || (cur == 't') || (cur == '\\') ||
4885 (cur == '|') || (cur == '.') || (cur == '?') || (cur == '*') ||
4886 (cur == '+') || (cur == '(') || (cur == ')') || (cur == '{') ||
4887 (cur == '}') || (cur == 0x2D) || (cur == 0x5B) || (cur == 0x5D) ||
4888 (cur == 0x5E)) {
4889 if (ctxt->atom == NULL) {
4890 ctxt->atom = xmlRegNewAtom(ctxt, XML_REGEXP_CHARVAL);
Daniel Veillard99c394d2005-07-14 12:58:49 +00004891 if (ctxt->atom != NULL) {
4892 switch (cur) {
4893 case 'n':
4894 ctxt->atom->codepoint = '\n';
4895 break;
4896 case 'r':
4897 ctxt->atom->codepoint = '\r';
4898 break;
4899 case 't':
4900 ctxt->atom->codepoint = '\t';
4901 break;
4902 default:
4903 ctxt->atom->codepoint = cur;
4904 }
4905 }
Daniel Veillard4255d502002-04-16 15:50:10 +00004906 } else if (ctxt->atom->type == XML_REGEXP_RANGES) {
Daniel Veillard9543aee2010-03-15 11:13:39 +01004907 switch (cur) {
4908 case 'n':
4909 cur = '\n';
4910 break;
4911 case 'r':
4912 cur = '\r';
4913 break;
4914 case 't':
4915 cur = '\t';
4916 break;
4917 }
Daniel Veillard4255d502002-04-16 15:50:10 +00004918 xmlRegAtomAddRange(ctxt, ctxt->atom, ctxt->neg,
4919 XML_REGEXP_CHARVAL, cur, cur, NULL);
4920 }
4921 NEXT;
4922 } else if ((cur == 's') || (cur == 'S') || (cur == 'i') || (cur == 'I') ||
4923 (cur == 'c') || (cur == 'C') || (cur == 'd') || (cur == 'D') ||
4924 (cur == 'w') || (cur == 'W')) {
Daniel Veillardb509f152002-04-17 16:28:10 +00004925 xmlRegAtomType type = XML_REGEXP_ANYSPACE;
Daniel Veillard4255d502002-04-16 15:50:10 +00004926
4927 switch (cur) {
Daniel Veillardf8e3db02012-09-11 13:26:36 +08004928 case 's':
Daniel Veillard4255d502002-04-16 15:50:10 +00004929 type = XML_REGEXP_ANYSPACE;
4930 break;
Daniel Veillardf8e3db02012-09-11 13:26:36 +08004931 case 'S':
Daniel Veillard4255d502002-04-16 15:50:10 +00004932 type = XML_REGEXP_NOTSPACE;
4933 break;
Daniel Veillardf8e3db02012-09-11 13:26:36 +08004934 case 'i':
Daniel Veillard4255d502002-04-16 15:50:10 +00004935 type = XML_REGEXP_INITNAME;
4936 break;
Daniel Veillardf8e3db02012-09-11 13:26:36 +08004937 case 'I':
Daniel Veillard4255d502002-04-16 15:50:10 +00004938 type = XML_REGEXP_NOTINITNAME;
4939 break;
Daniel Veillardf8e3db02012-09-11 13:26:36 +08004940 case 'c':
Daniel Veillard4255d502002-04-16 15:50:10 +00004941 type = XML_REGEXP_NAMECHAR;
4942 break;
Daniel Veillardf8e3db02012-09-11 13:26:36 +08004943 case 'C':
Daniel Veillard4255d502002-04-16 15:50:10 +00004944 type = XML_REGEXP_NOTNAMECHAR;
4945 break;
Daniel Veillardf8e3db02012-09-11 13:26:36 +08004946 case 'd':
Daniel Veillard4255d502002-04-16 15:50:10 +00004947 type = XML_REGEXP_DECIMAL;
4948 break;
Daniel Veillardf8e3db02012-09-11 13:26:36 +08004949 case 'D':
Daniel Veillard4255d502002-04-16 15:50:10 +00004950 type = XML_REGEXP_NOTDECIMAL;
4951 break;
Daniel Veillardf8e3db02012-09-11 13:26:36 +08004952 case 'w':
Daniel Veillard4255d502002-04-16 15:50:10 +00004953 type = XML_REGEXP_REALCHAR;
4954 break;
Daniel Veillardf8e3db02012-09-11 13:26:36 +08004955 case 'W':
Daniel Veillard4255d502002-04-16 15:50:10 +00004956 type = XML_REGEXP_NOTREALCHAR;
4957 break;
4958 }
4959 NEXT;
4960 if (ctxt->atom == NULL) {
4961 ctxt->atom = xmlRegNewAtom(ctxt, type);
4962 } else if (ctxt->atom->type == XML_REGEXP_RANGES) {
4963 xmlRegAtomAddRange(ctxt, ctxt->atom, ctxt->neg,
4964 type, 0, 0, NULL);
4965 }
Daniel Veillardcb4284e2007-04-25 13:55:20 +00004966 } else {
4967 ERROR("Wrong escape sequence, misuse of character '\\'");
Daniel Veillard4255d502002-04-16 15:50:10 +00004968 }
4969}
4970
4971/**
Daniel Veillard4255d502002-04-16 15:50:10 +00004972 * xmlFAParseCharRange:
Daniel Veillard441bc322002-04-20 17:38:48 +00004973 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00004974 *
Daniel Veillardf8e3db02012-09-11 13:26:36 +08004975 * [17] charRange ::= seRange | XmlCharRef | XmlCharIncDash
Daniel Veillard4255d502002-04-16 15:50:10 +00004976 * [18] seRange ::= charOrEsc '-' charOrEsc
4977 * [20] charOrEsc ::= XmlChar | SingleCharEsc
4978 * [21] XmlChar ::= [^\#x2D#x5B#x5D]
4979 * [22] XmlCharIncDash ::= [^\#x5B#x5D]
4980 */
4981static void
4982xmlFAParseCharRange(xmlRegParserCtxtPtr ctxt) {
William M. Brackdc99df92003-12-27 01:54:25 +00004983 int cur, len;
Daniel Veillard4255d502002-04-16 15:50:10 +00004984 int start = -1;
4985 int end = -1;
4986
Daniel Veillard777737e2006-10-17 21:23:17 +00004987 if (CUR == '\0') {
4988 ERROR("Expecting ']'");
4989 return;
4990 }
4991
Daniel Veillard4255d502002-04-16 15:50:10 +00004992 cur = CUR;
4993 if (cur == '\\') {
4994 NEXT;
4995 cur = CUR;
4996 switch (cur) {
4997 case 'n': start = 0xA; break;
4998 case 'r': start = 0xD; break;
4999 case 't': start = 0x9; break;
5000 case '\\': case '|': case '.': case '-': case '^': case '?':
5001 case '*': case '+': case '{': case '}': case '(': case ')':
5002 case '[': case ']':
5003 start = cur; break;
5004 default:
5005 ERROR("Invalid escape value");
5006 return;
5007 }
5008 end = start;
William M. Brackdc99df92003-12-27 01:54:25 +00005009 len = 1;
Daniel Veillard4255d502002-04-16 15:50:10 +00005010 } else if ((cur != 0x5B) && (cur != 0x5D)) {
William M. Brackdc99df92003-12-27 01:54:25 +00005011 end = start = CUR_SCHAR(ctxt->cur, len);
Daniel Veillard4255d502002-04-16 15:50:10 +00005012 } else {
5013 ERROR("Expecting a char range");
5014 return;
5015 }
William M. Bracka9cbf282007-03-21 13:16:33 +00005016 /*
5017 * Since we are "inside" a range, we can assume ctxt->cur is past
5018 * the start of ctxt->string, and PREV should be safe
5019 */
5020 if ((start == '-') && (NXT(1) != ']') && (PREV != '[') && (PREV != '^')) {
5021 NEXTL(len);
Daniel Veillard4255d502002-04-16 15:50:10 +00005022 return;
5023 }
William M. Bracka9cbf282007-03-21 13:16:33 +00005024 NEXTL(len);
Daniel Veillard4255d502002-04-16 15:50:10 +00005025 cur = CUR;
William M. Brack10f1ef42004-03-20 14:51:25 +00005026 if ((cur != '-') || (NXT(1) == ']')) {
Daniel Veillard4255d502002-04-16 15:50:10 +00005027 xmlRegAtomAddRange(ctxt, ctxt->atom, ctxt->neg,
5028 XML_REGEXP_CHARVAL, start, end, NULL);
5029 return;
5030 }
5031 NEXT;
5032 cur = CUR;
5033 if (cur == '\\') {
5034 NEXT;
5035 cur = CUR;
5036 switch (cur) {
5037 case 'n': end = 0xA; break;
5038 case 'r': end = 0xD; break;
5039 case 't': end = 0x9; break;
5040 case '\\': case '|': case '.': case '-': case '^': case '?':
5041 case '*': case '+': case '{': case '}': case '(': case ')':
5042 case '[': case ']':
5043 end = cur; break;
5044 default:
5045 ERROR("Invalid escape value");
5046 return;
5047 }
William M. Brackdc99df92003-12-27 01:54:25 +00005048 len = 1;
Daniel Veillard4255d502002-04-16 15:50:10 +00005049 } else if ((cur != 0x5B) && (cur != 0x5D)) {
William M. Brackdc99df92003-12-27 01:54:25 +00005050 end = CUR_SCHAR(ctxt->cur, len);
Daniel Veillard4255d502002-04-16 15:50:10 +00005051 } else {
5052 ERROR("Expecting the end of a char range");
5053 return;
5054 }
William M. Brackdc99df92003-12-27 01:54:25 +00005055 NEXTL(len);
Daniel Veillard4255d502002-04-16 15:50:10 +00005056 /* TODO check that the values are acceptable character ranges for XML */
5057 if (end < start) {
5058 ERROR("End of range is before start of range");
5059 } else {
5060 xmlRegAtomAddRange(ctxt, ctxt->atom, ctxt->neg,
5061 XML_REGEXP_CHARVAL, start, end, NULL);
5062 }
5063 return;
5064}
5065
5066/**
5067 * xmlFAParsePosCharGroup:
Daniel Veillard441bc322002-04-20 17:38:48 +00005068 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00005069 *
5070 * [14] posCharGroup ::= ( charRange | charClassEsc )+
5071 */
5072static void
5073xmlFAParsePosCharGroup(xmlRegParserCtxtPtr ctxt) {
5074 do {
Daniel Veillard041b6872008-02-08 10:37:18 +00005075 if (CUR == '\\') {
Daniel Veillard4255d502002-04-16 15:50:10 +00005076 xmlFAParseCharClassEsc(ctxt);
5077 } else {
5078 xmlFAParseCharRange(ctxt);
5079 }
5080 } while ((CUR != ']') && (CUR != '^') && (CUR != '-') &&
Daniel Veillard777737e2006-10-17 21:23:17 +00005081 (CUR != 0) && (ctxt->error == 0));
Daniel Veillard4255d502002-04-16 15:50:10 +00005082}
5083
5084/**
5085 * xmlFAParseCharGroup:
Daniel Veillard441bc322002-04-20 17:38:48 +00005086 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00005087 *
5088 * [13] charGroup ::= posCharGroup | negCharGroup | charClassSub
5089 * [15] negCharGroup ::= '^' posCharGroup
Daniel Veillardf8e3db02012-09-11 13:26:36 +08005090 * [16] charClassSub ::= ( posCharGroup | negCharGroup ) '-' charClassExpr
Daniel Veillard4255d502002-04-16 15:50:10 +00005091 * [12] charClassExpr ::= '[' charGroup ']'
5092 */
5093static void
5094xmlFAParseCharGroup(xmlRegParserCtxtPtr ctxt) {
5095 int n = ctxt->neg;
5096 while ((CUR != ']') && (ctxt->error == 0)) {
5097 if (CUR == '^') {
5098 int neg = ctxt->neg;
5099
5100 NEXT;
5101 ctxt->neg = !ctxt->neg;
5102 xmlFAParsePosCharGroup(ctxt);
5103 ctxt->neg = neg;
William M. Brack10f1ef42004-03-20 14:51:25 +00005104 } else if ((CUR == '-') && (NXT(1) == '[')) {
Daniel Veillardf8b9de32003-11-24 14:27:26 +00005105 int neg = ctxt->neg;
Daniel Veillardf8b9de32003-11-24 14:27:26 +00005106 ctxt->neg = 2;
William M. Brack10f1ef42004-03-20 14:51:25 +00005107 NEXT; /* eat the '-' */
5108 NEXT; /* eat the '[' */
Daniel Veillard4255d502002-04-16 15:50:10 +00005109 xmlFAParseCharGroup(ctxt);
5110 if (CUR == ']') {
5111 NEXT;
5112 } else {
5113 ERROR("charClassExpr: ']' expected");
5114 break;
5115 }
Daniel Veillardf8b9de32003-11-24 14:27:26 +00005116 ctxt->neg = neg;
Daniel Veillard4255d502002-04-16 15:50:10 +00005117 break;
5118 } else if (CUR != ']') {
5119 xmlFAParsePosCharGroup(ctxt);
5120 }
5121 }
5122 ctxt->neg = n;
5123}
5124
5125/**
5126 * xmlFAParseCharClass:
Daniel Veillard441bc322002-04-20 17:38:48 +00005127 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00005128 *
5129 * [11] charClass ::= charClassEsc | charClassExpr
5130 * [12] charClassExpr ::= '[' charGroup ']'
5131 */
5132static void
5133xmlFAParseCharClass(xmlRegParserCtxtPtr ctxt) {
5134 if (CUR == '[') {
5135 NEXT;
5136 ctxt->atom = xmlRegNewAtom(ctxt, XML_REGEXP_RANGES);
5137 if (ctxt->atom == NULL)
5138 return;
5139 xmlFAParseCharGroup(ctxt);
5140 if (CUR == ']') {
5141 NEXT;
5142 } else {
5143 ERROR("xmlFAParseCharClass: ']' expected");
5144 }
5145 } else {
5146 xmlFAParseCharClassEsc(ctxt);
5147 }
5148}
5149
5150/**
5151 * xmlFAParseQuantExact:
Daniel Veillard441bc322002-04-20 17:38:48 +00005152 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00005153 *
5154 * [8] QuantExact ::= [0-9]+
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00005155 *
5156 * Returns 0 if success or -1 in case of error
Daniel Veillard4255d502002-04-16 15:50:10 +00005157 */
5158static int
5159xmlFAParseQuantExact(xmlRegParserCtxtPtr ctxt) {
5160 int ret = 0;
5161 int ok = 0;
5162
5163 while ((CUR >= '0') && (CUR <= '9')) {
5164 ret = ret * 10 + (CUR - '0');
5165 ok = 1;
5166 NEXT;
5167 }
5168 if (ok != 1) {
5169 return(-1);
5170 }
5171 return(ret);
5172}
5173
5174/**
5175 * xmlFAParseQuantifier:
Daniel Veillard441bc322002-04-20 17:38:48 +00005176 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00005177 *
5178 * [4] quantifier ::= [?*+] | ( '{' quantity '}' )
5179 * [5] quantity ::= quantRange | quantMin | QuantExact
5180 * [6] quantRange ::= QuantExact ',' QuantExact
5181 * [7] quantMin ::= QuantExact ','
5182 * [8] QuantExact ::= [0-9]+
5183 */
5184static int
5185xmlFAParseQuantifier(xmlRegParserCtxtPtr ctxt) {
5186 int cur;
5187
5188 cur = CUR;
5189 if ((cur == '?') || (cur == '*') || (cur == '+')) {
5190 if (ctxt->atom != NULL) {
5191 if (cur == '?')
5192 ctxt->atom->quant = XML_REGEXP_QUANT_OPT;
5193 else if (cur == '*')
5194 ctxt->atom->quant = XML_REGEXP_QUANT_MULT;
5195 else if (cur == '+')
5196 ctxt->atom->quant = XML_REGEXP_QUANT_PLUS;
5197 }
5198 NEXT;
5199 return(1);
5200 }
5201 if (cur == '{') {
5202 int min = 0, max = 0;
5203
5204 NEXT;
5205 cur = xmlFAParseQuantExact(ctxt);
5206 if (cur >= 0)
5207 min = cur;
5208 if (CUR == ',') {
5209 NEXT;
Daniel Veillardebe48c62003-12-03 12:12:27 +00005210 if (CUR == '}')
5211 max = INT_MAX;
5212 else {
5213 cur = xmlFAParseQuantExact(ctxt);
5214 if (cur >= 0)
5215 max = cur;
5216 else {
5217 ERROR("Improper quantifier");
5218 }
5219 }
Daniel Veillard4255d502002-04-16 15:50:10 +00005220 }
5221 if (CUR == '}') {
5222 NEXT;
5223 } else {
5224 ERROR("Unterminated quantifier");
5225 }
5226 if (max == 0)
5227 max = min;
5228 if (ctxt->atom != NULL) {
5229 ctxt->atom->quant = XML_REGEXP_QUANT_RANGE;
5230 ctxt->atom->min = min;
5231 ctxt->atom->max = max;
5232 }
5233 return(1);
5234 }
5235 return(0);
5236}
5237
5238/**
5239 * xmlFAParseAtom:
Daniel Veillard441bc322002-04-20 17:38:48 +00005240 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00005241 *
5242 * [9] atom ::= Char | charClass | ( '(' regExp ')' )
5243 */
5244static int
5245xmlFAParseAtom(xmlRegParserCtxtPtr ctxt) {
5246 int codepoint, len;
5247
5248 codepoint = xmlFAIsChar(ctxt);
5249 if (codepoint > 0) {
5250 ctxt->atom = xmlRegNewAtom(ctxt, XML_REGEXP_CHARVAL);
5251 if (ctxt->atom == NULL)
5252 return(-1);
5253 codepoint = CUR_SCHAR(ctxt->cur, len);
5254 ctxt->atom->codepoint = codepoint;
5255 NEXTL(len);
5256 return(1);
5257 } else if (CUR == '|') {
5258 return(0);
5259 } else if (CUR == 0) {
5260 return(0);
5261 } else if (CUR == ')') {
5262 return(0);
5263 } else if (CUR == '(') {
Daniel Veillard76d59b62007-08-22 16:29:21 +00005264 xmlRegStatePtr start, oldend, start0;
Daniel Veillard4255d502002-04-16 15:50:10 +00005265
5266 NEXT;
Daniel Veillard76d59b62007-08-22 16:29:21 +00005267 /*
5268 * this extra Epsilon transition is needed if we count with 0 allowed
5269 * unfortunately this can't be known at that point
5270 */
5271 xmlFAGenerateEpsilonTransition(ctxt, ctxt->state, NULL);
5272 start0 = ctxt->state;
Daniel Veillard4255d502002-04-16 15:50:10 +00005273 xmlFAGenerateEpsilonTransition(ctxt, ctxt->state, NULL);
5274 start = ctxt->state;
5275 oldend = ctxt->end;
5276 ctxt->end = NULL;
5277 ctxt->atom = NULL;
5278 xmlFAParseRegExp(ctxt, 0);
5279 if (CUR == ')') {
5280 NEXT;
5281 } else {
5282 ERROR("xmlFAParseAtom: expecting ')'");
5283 }
5284 ctxt->atom = xmlRegNewAtom(ctxt, XML_REGEXP_SUBREG);
5285 if (ctxt->atom == NULL)
5286 return(-1);
5287 ctxt->atom->start = start;
Daniel Veillard76d59b62007-08-22 16:29:21 +00005288 ctxt->atom->start0 = start0;
Daniel Veillard4255d502002-04-16 15:50:10 +00005289 ctxt->atom->stop = ctxt->state;
5290 ctxt->end = oldend;
5291 return(1);
5292 } else if ((CUR == '[') || (CUR == '\\') || (CUR == '.')) {
5293 xmlFAParseCharClass(ctxt);
5294 return(1);
5295 }
5296 return(0);
5297}
5298
5299/**
5300 * xmlFAParsePiece:
Daniel Veillard441bc322002-04-20 17:38:48 +00005301 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00005302 *
5303 * [3] piece ::= atom quantifier?
5304 */
5305static int
5306xmlFAParsePiece(xmlRegParserCtxtPtr ctxt) {
5307 int ret;
5308
5309 ctxt->atom = NULL;
5310 ret = xmlFAParseAtom(ctxt);
5311 if (ret == 0)
5312 return(0);
5313 if (ctxt->atom == NULL) {
5314 ERROR("internal: no atom generated");
5315 }
5316 xmlFAParseQuantifier(ctxt);
5317 return(1);
5318}
5319
5320/**
5321 * xmlFAParseBranch:
Daniel Veillard441bc322002-04-20 17:38:48 +00005322 * @ctxt: a regexp parser context
Daniel Veillard54eb0242006-03-21 23:17:57 +00005323 * @to: optional target to the end of the branch
5324 *
5325 * @to is used to optimize by removing duplicate path in automata
5326 * in expressions like (a|b)(c|d)
Daniel Veillard4255d502002-04-16 15:50:10 +00005327 *
5328 * [2] branch ::= piece*
5329 */
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00005330static int
Daniel Veillard54eb0242006-03-21 23:17:57 +00005331xmlFAParseBranch(xmlRegParserCtxtPtr ctxt, xmlRegStatePtr to) {
Daniel Veillard4255d502002-04-16 15:50:10 +00005332 xmlRegStatePtr previous;
Daniel Veillard4255d502002-04-16 15:50:10 +00005333 int ret;
5334
5335 previous = ctxt->state;
5336 ret = xmlFAParsePiece(ctxt);
5337 if (ret != 0) {
Daniel Veillardf8e3db02012-09-11 13:26:36 +08005338 if (xmlFAGenerateTransitions(ctxt, previous,
Daniel Veillard54eb0242006-03-21 23:17:57 +00005339 (CUR=='|' || CUR==')') ? to : NULL, ctxt->atom) < 0)
Daniel Veillard2cbf5962004-03-31 15:50:43 +00005340 return(-1);
5341 previous = ctxt->state;
Daniel Veillard4255d502002-04-16 15:50:10 +00005342 ctxt->atom = NULL;
5343 }
5344 while ((ret != 0) && (ctxt->error == 0)) {
5345 ret = xmlFAParsePiece(ctxt);
5346 if (ret != 0) {
Daniel Veillardf8e3db02012-09-11 13:26:36 +08005347 if (xmlFAGenerateTransitions(ctxt, previous,
Daniel Veillard54eb0242006-03-21 23:17:57 +00005348 (CUR=='|' || CUR==')') ? to : NULL, ctxt->atom) < 0)
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00005349 return(-1);
Daniel Veillard4255d502002-04-16 15:50:10 +00005350 previous = ctxt->state;
5351 ctxt->atom = NULL;
5352 }
5353 }
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00005354 return(0);
Daniel Veillard4255d502002-04-16 15:50:10 +00005355}
5356
5357/**
5358 * xmlFAParseRegExp:
Daniel Veillard441bc322002-04-20 17:38:48 +00005359 * @ctxt: a regexp parser context
William M. Brackddf71d62004-05-06 04:17:26 +00005360 * @top: is this the top-level expression ?
Daniel Veillard4255d502002-04-16 15:50:10 +00005361 *
5362 * [1] regExp ::= branch ( '|' branch )*
5363 */
5364static void
5365xmlFAParseRegExp(xmlRegParserCtxtPtr ctxt, int top) {
Daniel Veillardc7e3cc42004-09-28 12:33:52 +00005366 xmlRegStatePtr start, end;
Daniel Veillard4255d502002-04-16 15:50:10 +00005367
Daniel Veillard2cbf5962004-03-31 15:50:43 +00005368 /* if not top start should have been generated by an epsilon trans */
Daniel Veillard4255d502002-04-16 15:50:10 +00005369 start = ctxt->state;
Daniel Veillard2cbf5962004-03-31 15:50:43 +00005370 ctxt->end = NULL;
Daniel Veillard54eb0242006-03-21 23:17:57 +00005371 xmlFAParseBranch(ctxt, NULL);
Daniel Veillard2cbf5962004-03-31 15:50:43 +00005372 if (top) {
5373#ifdef DEBUG_REGEXP_GRAPH
5374 printf("State %d is final\n", ctxt->state->no);
5375#endif
5376 ctxt->state->type = XML_REGEXP_FINAL_STATE;
5377 }
Daniel Veillard4255d502002-04-16 15:50:10 +00005378 if (CUR != '|') {
5379 ctxt->end = ctxt->state;
5380 return;
5381 }
5382 end = ctxt->state;
5383 while ((CUR == '|') && (ctxt->error == 0)) {
5384 NEXT;
Daniel Veillard40851d02012-08-17 20:34:05 +08005385 if (CUR == 0) {
5386 ERROR("expecting a branch after |")
5387 return;
5388 }
Daniel Veillard4255d502002-04-16 15:50:10 +00005389 ctxt->state = start;
Daniel Veillard2cbf5962004-03-31 15:50:43 +00005390 ctxt->end = NULL;
Daniel Veillard54eb0242006-03-21 23:17:57 +00005391 xmlFAParseBranch(ctxt, end);
Daniel Veillard4255d502002-04-16 15:50:10 +00005392 }
Daniel Veillard2cbf5962004-03-31 15:50:43 +00005393 if (!top) {
5394 ctxt->state = end;
5395 ctxt->end = end;
5396 }
Daniel Veillard4255d502002-04-16 15:50:10 +00005397}
5398
5399/************************************************************************
Daniel Veillardf8e3db02012-09-11 13:26:36 +08005400 * *
5401 * The basic API *
5402 * *
Daniel Veillard4255d502002-04-16 15:50:10 +00005403 ************************************************************************/
5404
5405/**
5406 * xmlRegexpPrint:
5407 * @output: the file for the output debug
5408 * @regexp: the compiled regexp
5409 *
5410 * Print the content of the compiled regular expression
5411 */
5412void
5413xmlRegexpPrint(FILE *output, xmlRegexpPtr regexp) {
5414 int i;
5415
Daniel Veillarda82b1822004-11-08 16:24:57 +00005416 if (output == NULL)
5417 return;
Daniel Veillard4255d502002-04-16 15:50:10 +00005418 fprintf(output, " regexp: ");
5419 if (regexp == NULL) {
5420 fprintf(output, "NULL\n");
5421 return;
5422 }
5423 fprintf(output, "'%s' ", regexp->string);
5424 fprintf(output, "\n");
5425 fprintf(output, "%d atoms:\n", regexp->nbAtoms);
5426 for (i = 0;i < regexp->nbAtoms; i++) {
5427 fprintf(output, " %02d ", i);
5428 xmlRegPrintAtom(output, regexp->atoms[i]);
5429 }
5430 fprintf(output, "%d states:", regexp->nbStates);
5431 fprintf(output, "\n");
5432 for (i = 0;i < regexp->nbStates; i++) {
5433 xmlRegPrintState(output, regexp->states[i]);
5434 }
5435 fprintf(output, "%d counters:\n", regexp->nbCounters);
5436 for (i = 0;i < regexp->nbCounters; i++) {
5437 fprintf(output, " %d: min %d max %d\n", i, regexp->counters[i].min,
5438 regexp->counters[i].max);
5439 }
5440}
5441
5442/**
5443 * xmlRegexpCompile:
5444 * @regexp: a regular expression string
5445 *
5446 * Parses a regular expression conforming to XML Schemas Part 2 Datatype
William M. Brackddf71d62004-05-06 04:17:26 +00005447 * Appendix F and builds an automata suitable for testing strings against
Daniel Veillard4255d502002-04-16 15:50:10 +00005448 * that regular expression
5449 *
5450 * Returns the compiled expression or NULL in case of error
5451 */
5452xmlRegexpPtr
5453xmlRegexpCompile(const xmlChar *regexp) {
5454 xmlRegexpPtr ret;
5455 xmlRegParserCtxtPtr ctxt;
5456
5457 ctxt = xmlRegNewParserCtxt(regexp);
5458 if (ctxt == NULL)
5459 return(NULL);
5460
5461 /* initialize the parser */
5462 ctxt->end = NULL;
5463 ctxt->start = ctxt->state = xmlRegNewState(ctxt);
5464 xmlRegStatePush(ctxt, ctxt->start);
5465
5466 /* parse the expression building an automata */
5467 xmlFAParseRegExp(ctxt, 1);
5468 if (CUR != 0) {
5469 ERROR("xmlFAParseRegExp: extra characters");
5470 }
Daniel Veillardcb4284e2007-04-25 13:55:20 +00005471 if (ctxt->error != 0) {
5472 xmlRegFreeParserCtxt(ctxt);
5473 return(NULL);
5474 }
Daniel Veillard4255d502002-04-16 15:50:10 +00005475 ctxt->end = ctxt->state;
5476 ctxt->start->type = XML_REGEXP_START_STATE;
5477 ctxt->end->type = XML_REGEXP_FINAL_STATE;
5478
5479 /* remove the Epsilon except for counted transitions */
5480 xmlFAEliminateEpsilonTransitions(ctxt);
5481
5482
5483 if (ctxt->error != 0) {
5484 xmlRegFreeParserCtxt(ctxt);
5485 return(NULL);
5486 }
5487 ret = xmlRegEpxFromParse(ctxt);
5488 xmlRegFreeParserCtxt(ctxt);
5489 return(ret);
5490}
5491
5492/**
5493 * xmlRegexpExec:
5494 * @comp: the compiled regular expression
5495 * @content: the value to check against the regular expression
5496 *
William M. Brackddf71d62004-05-06 04:17:26 +00005497 * Check if the regular expression generates the value
Daniel Veillard4255d502002-04-16 15:50:10 +00005498 *
William M. Brackddf71d62004-05-06 04:17:26 +00005499 * Returns 1 if it matches, 0 if not and a negative value in case of error
Daniel Veillard4255d502002-04-16 15:50:10 +00005500 */
5501int
5502xmlRegexpExec(xmlRegexpPtr comp, const xmlChar *content) {
5503 if ((comp == NULL) || (content == NULL))
5504 return(-1);
5505 return(xmlFARegExec(comp, content));
5506}
5507
5508/**
Daniel Veillard23e73572002-09-19 19:56:43 +00005509 * xmlRegexpIsDeterminist:
5510 * @comp: the compiled regular expression
5511 *
5512 * Check if the regular expression is determinist
5513 *
William M. Brackddf71d62004-05-06 04:17:26 +00005514 * Returns 1 if it yes, 0 if not and a negative value in case of error
Daniel Veillard23e73572002-09-19 19:56:43 +00005515 */
5516int
5517xmlRegexpIsDeterminist(xmlRegexpPtr comp) {
5518 xmlAutomataPtr am;
5519 int ret;
5520
5521 if (comp == NULL)
5522 return(-1);
5523 if (comp->determinist != -1)
5524 return(comp->determinist);
5525
5526 am = xmlNewAutomata();
Daniel Veillardbd9afb52002-09-25 22:25:35 +00005527 if (am->states != NULL) {
5528 int i;
5529
5530 for (i = 0;i < am->nbStates;i++)
5531 xmlRegFreeState(am->states[i]);
5532 xmlFree(am->states);
5533 }
Daniel Veillard23e73572002-09-19 19:56:43 +00005534 am->nbAtoms = comp->nbAtoms;
5535 am->atoms = comp->atoms;
5536 am->nbStates = comp->nbStates;
5537 am->states = comp->states;
5538 am->determinist = -1;
Daniel Veillard1ba2aca2009-08-31 16:47:39 +02005539 am->flags = comp->flags;
Daniel Veillard23e73572002-09-19 19:56:43 +00005540 ret = xmlFAComputesDeterminism(am);
5541 am->atoms = NULL;
5542 am->states = NULL;
5543 xmlFreeAutomata(am);
Daniel Veillard1ba2aca2009-08-31 16:47:39 +02005544 comp->determinist = ret;
Daniel Veillard23e73572002-09-19 19:56:43 +00005545 return(ret);
5546}
5547
5548/**
Daniel Veillard4255d502002-04-16 15:50:10 +00005549 * xmlRegFreeRegexp:
5550 * @regexp: the regexp
5551 *
5552 * Free a regexp
5553 */
5554void
5555xmlRegFreeRegexp(xmlRegexpPtr regexp) {
5556 int i;
5557 if (regexp == NULL)
5558 return;
5559
5560 if (regexp->string != NULL)
5561 xmlFree(regexp->string);
5562 if (regexp->states != NULL) {
5563 for (i = 0;i < regexp->nbStates;i++)
5564 xmlRegFreeState(regexp->states[i]);
5565 xmlFree(regexp->states);
5566 }
5567 if (regexp->atoms != NULL) {
5568 for (i = 0;i < regexp->nbAtoms;i++)
5569 xmlRegFreeAtom(regexp->atoms[i]);
5570 xmlFree(regexp->atoms);
5571 }
5572 if (regexp->counters != NULL)
5573 xmlFree(regexp->counters);
Daniel Veillard23e73572002-09-19 19:56:43 +00005574 if (regexp->compact != NULL)
5575 xmlFree(regexp->compact);
Daniel Veillard118aed72002-09-24 14:13:13 +00005576 if (regexp->transdata != NULL)
5577 xmlFree(regexp->transdata);
Daniel Veillard23e73572002-09-19 19:56:43 +00005578 if (regexp->stringMap != NULL) {
5579 for (i = 0; i < regexp->nbstrings;i++)
5580 xmlFree(regexp->stringMap[i]);
5581 xmlFree(regexp->stringMap);
5582 }
5583
Daniel Veillard4255d502002-04-16 15:50:10 +00005584 xmlFree(regexp);
5585}
5586
5587#ifdef LIBXML_AUTOMATA_ENABLED
5588/************************************************************************
Daniel Veillardf8e3db02012-09-11 13:26:36 +08005589 * *
5590 * The Automata interface *
5591 * *
Daniel Veillard4255d502002-04-16 15:50:10 +00005592 ************************************************************************/
5593
5594/**
5595 * xmlNewAutomata:
5596 *
5597 * Create a new automata
5598 *
5599 * Returns the new object or NULL in case of failure
5600 */
5601xmlAutomataPtr
5602xmlNewAutomata(void) {
5603 xmlAutomataPtr ctxt;
5604
5605 ctxt = xmlRegNewParserCtxt(NULL);
5606 if (ctxt == NULL)
5607 return(NULL);
5608
5609 /* initialize the parser */
5610 ctxt->end = NULL;
5611 ctxt->start = ctxt->state = xmlRegNewState(ctxt);
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00005612 if (ctxt->start == NULL) {
5613 xmlFreeAutomata(ctxt);
5614 return(NULL);
5615 }
Daniel Veillardd0271472006-01-02 10:22:02 +00005616 ctxt->start->type = XML_REGEXP_START_STATE;
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00005617 if (xmlRegStatePush(ctxt, ctxt->start) < 0) {
5618 xmlRegFreeState(ctxt->start);
5619 xmlFreeAutomata(ctxt);
5620 return(NULL);
5621 }
Daniel Veillard1ba2aca2009-08-31 16:47:39 +02005622 ctxt->flags = 0;
Daniel Veillard4255d502002-04-16 15:50:10 +00005623
5624 return(ctxt);
5625}
5626
5627/**
5628 * xmlFreeAutomata:
5629 * @am: an automata
5630 *
5631 * Free an automata
5632 */
5633void
5634xmlFreeAutomata(xmlAutomataPtr am) {
5635 if (am == NULL)
5636 return;
5637 xmlRegFreeParserCtxt(am);
5638}
5639
5640/**
Daniel Veillard29341682009-09-10 18:23:39 +02005641 * xmlAutomataSetFlags:
Daniel Veillard1ba2aca2009-08-31 16:47:39 +02005642 * @am: an automata
5643 * @flags: a set of internal flags
5644 *
5645 * Set some flags on the automata
5646 */
5647void
5648xmlAutomataSetFlags(xmlAutomataPtr am, int flags) {
5649 if (am == NULL)
5650 return;
5651 am->flags |= flags;
5652}
5653
5654/**
Daniel Veillard4255d502002-04-16 15:50:10 +00005655 * xmlAutomataGetInitState:
5656 * @am: an automata
5657 *
Daniel Veillarda9b66d02002-12-11 14:23:49 +00005658 * Initial state lookup
5659 *
Daniel Veillard4255d502002-04-16 15:50:10 +00005660 * Returns the initial state of the automata
5661 */
5662xmlAutomataStatePtr
5663xmlAutomataGetInitState(xmlAutomataPtr am) {
5664 if (am == NULL)
5665 return(NULL);
5666 return(am->start);
5667}
5668
5669/**
5670 * xmlAutomataSetFinalState:
5671 * @am: an automata
5672 * @state: a state in this automata
5673 *
5674 * Makes that state a final state
5675 *
5676 * Returns 0 or -1 in case of error
5677 */
5678int
5679xmlAutomataSetFinalState(xmlAutomataPtr am, xmlAutomataStatePtr state) {
5680 if ((am == NULL) || (state == NULL))
5681 return(-1);
5682 state->type = XML_REGEXP_FINAL_STATE;
5683 return(0);
5684}
5685
5686/**
5687 * xmlAutomataNewTransition:
5688 * @am: an automata
5689 * @from: the starting point of the transition
5690 * @to: the target point of the transition or NULL
5691 * @token: the input string associated to that transition
5692 * @data: data passed to the callback function if the transition is activated
5693 *
William M. Brackddf71d62004-05-06 04:17:26 +00005694 * If @to is NULL, this creates first a new target state in the automata
Daniel Veillard4255d502002-04-16 15:50:10 +00005695 * and then adds a transition from the @from state to the target state
5696 * activated by the value of @token
5697 *
5698 * Returns the target state or NULL in case of error
5699 */
5700xmlAutomataStatePtr
5701xmlAutomataNewTransition(xmlAutomataPtr am, xmlAutomataStatePtr from,
5702 xmlAutomataStatePtr to, const xmlChar *token,
5703 void *data) {
5704 xmlRegAtomPtr atom;
5705
5706 if ((am == NULL) || (from == NULL) || (token == NULL))
5707 return(NULL);
5708 atom = xmlRegNewAtom(am, XML_REGEXP_STRING);
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00005709 if (atom == NULL)
5710 return(NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +00005711 atom->data = data;
5712 if (atom == NULL)
5713 return(NULL);
5714 atom->valuep = xmlStrdup(token);
5715
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00005716 if (xmlFAGenerateTransitions(am, from, to, atom) < 0) {
5717 xmlRegFreeAtom(atom);
5718 return(NULL);
5719 }
Daniel Veillard4255d502002-04-16 15:50:10 +00005720 if (to == NULL)
5721 return(am->state);
5722 return(to);
5723}
5724
5725/**
Daniel Veillard52b48c72003-04-13 19:53:42 +00005726 * xmlAutomataNewTransition2:
5727 * @am: an automata
5728 * @from: the starting point of the transition
5729 * @to: the target point of the transition or NULL
5730 * @token: the first input string associated to that transition
5731 * @token2: the second input string associated to that transition
5732 * @data: data passed to the callback function if the transition is activated
5733 *
William M. Brackddf71d62004-05-06 04:17:26 +00005734 * If @to is NULL, this creates first a new target state in the automata
Daniel Veillard52b48c72003-04-13 19:53:42 +00005735 * and then adds a transition from the @from state to the target state
5736 * activated by the value of @token
5737 *
5738 * Returns the target state or NULL in case of error
5739 */
5740xmlAutomataStatePtr
5741xmlAutomataNewTransition2(xmlAutomataPtr am, xmlAutomataStatePtr from,
5742 xmlAutomataStatePtr to, const xmlChar *token,
5743 const xmlChar *token2, void *data) {
5744 xmlRegAtomPtr atom;
5745
5746 if ((am == NULL) || (from == NULL) || (token == NULL))
5747 return(NULL);
5748 atom = xmlRegNewAtom(am, XML_REGEXP_STRING);
Daniel Veillard52b48c72003-04-13 19:53:42 +00005749 if (atom == NULL)
5750 return(NULL);
Daniel Veillard11ce4002006-03-10 00:36:23 +00005751 atom->data = data;
Daniel Veillard52b48c72003-04-13 19:53:42 +00005752 if ((token2 == NULL) || (*token2 == 0)) {
5753 atom->valuep = xmlStrdup(token);
5754 } else {
5755 int lenn, lenp;
5756 xmlChar *str;
5757
5758 lenn = strlen((char *) token2);
5759 lenp = strlen((char *) token);
5760
Daniel Veillard3c908dc2003-04-19 00:07:51 +00005761 str = (xmlChar *) xmlMallocAtomic(lenn + lenp + 2);
Daniel Veillard52b48c72003-04-13 19:53:42 +00005762 if (str == NULL) {
5763 xmlRegFreeAtom(atom);
5764 return(NULL);
5765 }
5766 memcpy(&str[0], token, lenp);
5767 str[lenp] = '|';
5768 memcpy(&str[lenp + 1], token2, lenn);
5769 str[lenn + lenp + 1] = 0;
5770
5771 atom->valuep = str;
5772 }
5773
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00005774 if (xmlFAGenerateTransitions(am, from, to, atom) < 0) {
5775 xmlRegFreeAtom(atom);
5776 return(NULL);
5777 }
Daniel Veillard52b48c72003-04-13 19:53:42 +00005778 if (to == NULL)
5779 return(am->state);
5780 return(to);
5781}
5782
5783/**
Daniel Veillard9efc4762005-07-19 14:33:55 +00005784 * xmlAutomataNewNegTrans:
5785 * @am: an automata
5786 * @from: the starting point of the transition
5787 * @to: the target point of the transition or NULL
5788 * @token: the first input string associated to that transition
5789 * @token2: the second input string associated to that transition
5790 * @data: data passed to the callback function if the transition is activated
5791 *
5792 * If @to is NULL, this creates first a new target state in the automata
5793 * and then adds a transition from the @from state to the target state
5794 * activated by any value except (@token,@token2)
Daniel Veillard6e65e152005-08-09 11:09:52 +00005795 * Note that if @token2 is not NULL, then (X, NULL) won't match to follow
5796 # the semantic of XSD ##other
Daniel Veillard9efc4762005-07-19 14:33:55 +00005797 *
5798 * Returns the target state or NULL in case of error
5799 */
5800xmlAutomataStatePtr
5801xmlAutomataNewNegTrans(xmlAutomataPtr am, xmlAutomataStatePtr from,
5802 xmlAutomataStatePtr to, const xmlChar *token,
5803 const xmlChar *token2, void *data) {
5804 xmlRegAtomPtr atom;
Daniel Veillard77005e62005-07-19 16:26:18 +00005805 xmlChar err_msg[200];
Daniel Veillard9efc4762005-07-19 14:33:55 +00005806
5807 if ((am == NULL) || (from == NULL) || (token == NULL))
5808 return(NULL);
5809 atom = xmlRegNewAtom(am, XML_REGEXP_STRING);
5810 if (atom == NULL)
5811 return(NULL);
5812 atom->data = data;
5813 atom->neg = 1;
5814 if ((token2 == NULL) || (*token2 == 0)) {
5815 atom->valuep = xmlStrdup(token);
5816 } else {
5817 int lenn, lenp;
5818 xmlChar *str;
5819
5820 lenn = strlen((char *) token2);
5821 lenp = strlen((char *) token);
5822
5823 str = (xmlChar *) xmlMallocAtomic(lenn + lenp + 2);
5824 if (str == NULL) {
5825 xmlRegFreeAtom(atom);
5826 return(NULL);
5827 }
5828 memcpy(&str[0], token, lenp);
5829 str[lenp] = '|';
5830 memcpy(&str[lenp + 1], token2, lenn);
5831 str[lenn + lenp + 1] = 0;
5832
5833 atom->valuep = str;
5834 }
Daniel Veillarddb68b742005-07-30 13:18:24 +00005835 snprintf((char *) err_msg, 199, "not %s", (const char *) atom->valuep);
Daniel Veillard77005e62005-07-19 16:26:18 +00005836 err_msg[199] = 0;
5837 atom->valuep2 = xmlStrdup(err_msg);
Daniel Veillard9efc4762005-07-19 14:33:55 +00005838
5839 if (xmlFAGenerateTransitions(am, from, to, atom) < 0) {
5840 xmlRegFreeAtom(atom);
5841 return(NULL);
5842 }
Daniel Veillard6e65e152005-08-09 11:09:52 +00005843 am->negs++;
Daniel Veillard9efc4762005-07-19 14:33:55 +00005844 if (to == NULL)
5845 return(am->state);
5846 return(to);
5847}
5848
5849/**
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00005850 * xmlAutomataNewCountTrans2:
5851 * @am: an automata
5852 * @from: the starting point of the transition
5853 * @to: the target point of the transition or NULL
5854 * @token: the input string associated to that transition
5855 * @token2: the second input string associated to that transition
5856 * @min: the minimum successive occurences of token
5857 * @max: the maximum successive occurences of token
5858 * @data: data associated to the transition
5859 *
5860 * If @to is NULL, this creates first a new target state in the automata
5861 * and then adds a transition from the @from state to the target state
Daniel Veillardf8e3db02012-09-11 13:26:36 +08005862 * activated by a succession of input of value @token and @token2 and
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00005863 * whose number is between @min and @max
5864 *
5865 * Returns the target state or NULL in case of error
5866 */
5867xmlAutomataStatePtr
5868xmlAutomataNewCountTrans2(xmlAutomataPtr am, xmlAutomataStatePtr from,
5869 xmlAutomataStatePtr to, const xmlChar *token,
5870 const xmlChar *token2,
5871 int min, int max, void *data) {
5872 xmlRegAtomPtr atom;
5873 int counter;
5874
5875 if ((am == NULL) || (from == NULL) || (token == NULL))
5876 return(NULL);
5877 if (min < 0)
5878 return(NULL);
5879 if ((max < min) || (max < 1))
5880 return(NULL);
5881 atom = xmlRegNewAtom(am, XML_REGEXP_STRING);
5882 if (atom == NULL)
5883 return(NULL);
5884 if ((token2 == NULL) || (*token2 == 0)) {
5885 atom->valuep = xmlStrdup(token);
5886 } else {
5887 int lenn, lenp;
5888 xmlChar *str;
5889
5890 lenn = strlen((char *) token2);
5891 lenp = strlen((char *) token);
5892
5893 str = (xmlChar *) xmlMallocAtomic(lenn + lenp + 2);
5894 if (str == NULL) {
5895 xmlRegFreeAtom(atom);
5896 return(NULL);
5897 }
5898 memcpy(&str[0], token, lenp);
5899 str[lenp] = '|';
5900 memcpy(&str[lenp + 1], token2, lenn);
5901 str[lenn + lenp + 1] = 0;
5902
5903 atom->valuep = str;
5904 }
5905 atom->data = data;
5906 if (min == 0)
5907 atom->min = 1;
5908 else
5909 atom->min = min;
5910 atom->max = max;
5911
5912 /*
5913 * associate a counter to the transition.
5914 */
5915 counter = xmlRegGetCounter(am);
5916 am->counters[counter].min = min;
5917 am->counters[counter].max = max;
5918
5919 /* xmlFAGenerateTransitions(am, from, to, atom); */
5920 if (to == NULL) {
5921 to = xmlRegNewState(am);
5922 xmlRegStatePush(am, to);
5923 }
Daniel Veillard5de09382005-09-26 17:18:17 +00005924 xmlRegStateAddTrans(am, from, atom, to, counter, -1);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00005925 xmlRegAtomPush(am, atom);
5926 am->state = to;
5927
5928 if (to == NULL)
5929 to = am->state;
5930 if (to == NULL)
5931 return(NULL);
5932 if (min == 0)
5933 xmlFAGenerateEpsilonTransition(am, from, to);
5934 return(to);
5935}
5936
5937/**
Daniel Veillard4255d502002-04-16 15:50:10 +00005938 * xmlAutomataNewCountTrans:
5939 * @am: an automata
5940 * @from: the starting point of the transition
5941 * @to: the target point of the transition or NULL
5942 * @token: the input string associated to that transition
5943 * @min: the minimum successive occurences of token
Daniel Veillarda9b66d02002-12-11 14:23:49 +00005944 * @max: the maximum successive occurences of token
5945 * @data: data associated to the transition
Daniel Veillard4255d502002-04-16 15:50:10 +00005946 *
William M. Brackddf71d62004-05-06 04:17:26 +00005947 * If @to is NULL, this creates first a new target state in the automata
Daniel Veillard4255d502002-04-16 15:50:10 +00005948 * and then adds a transition from the @from state to the target state
5949 * activated by a succession of input of value @token and whose number
5950 * is between @min and @max
5951 *
5952 * Returns the target state or NULL in case of error
5953 */
5954xmlAutomataStatePtr
5955xmlAutomataNewCountTrans(xmlAutomataPtr am, xmlAutomataStatePtr from,
5956 xmlAutomataStatePtr to, const xmlChar *token,
5957 int min, int max, void *data) {
5958 xmlRegAtomPtr atom;
Daniel Veillard0ddb21c2004-02-12 12:43:49 +00005959 int counter;
Daniel Veillard4255d502002-04-16 15:50:10 +00005960
5961 if ((am == NULL) || (from == NULL) || (token == NULL))
5962 return(NULL);
5963 if (min < 0)
5964 return(NULL);
5965 if ((max < min) || (max < 1))
5966 return(NULL);
5967 atom = xmlRegNewAtom(am, XML_REGEXP_STRING);
5968 if (atom == NULL)
5969 return(NULL);
5970 atom->valuep = xmlStrdup(token);
5971 atom->data = data;
5972 if (min == 0)
5973 atom->min = 1;
5974 else
5975 atom->min = min;
5976 atom->max = max;
5977
Daniel Veillard0ddb21c2004-02-12 12:43:49 +00005978 /*
5979 * associate a counter to the transition.
5980 */
5981 counter = xmlRegGetCounter(am);
5982 am->counters[counter].min = min;
5983 am->counters[counter].max = max;
5984
5985 /* xmlFAGenerateTransitions(am, from, to, atom); */
5986 if (to == NULL) {
5987 to = xmlRegNewState(am);
5988 xmlRegStatePush(am, to);
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00005989 }
Daniel Veillard5de09382005-09-26 17:18:17 +00005990 xmlRegStateAddTrans(am, from, atom, to, counter, -1);
Daniel Veillard0ddb21c2004-02-12 12:43:49 +00005991 xmlRegAtomPush(am, atom);
5992 am->state = to;
5993
Daniel Veillard4255d502002-04-16 15:50:10 +00005994 if (to == NULL)
5995 to = am->state;
5996 if (to == NULL)
5997 return(NULL);
5998 if (min == 0)
5999 xmlFAGenerateEpsilonTransition(am, from, to);
6000 return(to);
6001}
6002
6003/**
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00006004 * xmlAutomataNewOnceTrans2:
6005 * @am: an automata
6006 * @from: the starting point of the transition
6007 * @to: the target point of the transition or NULL
6008 * @token: the input string associated to that transition
6009 * @token2: the second input string associated to that transition
6010 * @min: the minimum successive occurences of token
6011 * @max: the maximum successive occurences of token
6012 * @data: data associated to the transition
6013 *
6014 * If @to is NULL, this creates first a new target state in the automata
6015 * and then adds a transition from the @from state to the target state
Daniel Veillardf8e3db02012-09-11 13:26:36 +08006016 * activated by a succession of input of value @token and @token2 and whose
6017 * number is between @min and @max, moreover that transition can only be
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00006018 * crossed once.
6019 *
6020 * Returns the target state or NULL in case of error
6021 */
6022xmlAutomataStatePtr
6023xmlAutomataNewOnceTrans2(xmlAutomataPtr am, xmlAutomataStatePtr from,
6024 xmlAutomataStatePtr to, const xmlChar *token,
6025 const xmlChar *token2,
6026 int min, int max, void *data) {
6027 xmlRegAtomPtr atom;
6028 int counter;
6029
6030 if ((am == NULL) || (from == NULL) || (token == NULL))
6031 return(NULL);
6032 if (min < 1)
6033 return(NULL);
6034 if ((max < min) || (max < 1))
6035 return(NULL);
6036 atom = xmlRegNewAtom(am, XML_REGEXP_STRING);
6037 if (atom == NULL)
6038 return(NULL);
6039 if ((token2 == NULL) || (*token2 == 0)) {
6040 atom->valuep = xmlStrdup(token);
6041 } else {
6042 int lenn, lenp;
6043 xmlChar *str;
6044
6045 lenn = strlen((char *) token2);
6046 lenp = strlen((char *) token);
6047
6048 str = (xmlChar *) xmlMallocAtomic(lenn + lenp + 2);
6049 if (str == NULL) {
6050 xmlRegFreeAtom(atom);
6051 return(NULL);
6052 }
6053 memcpy(&str[0], token, lenp);
6054 str[lenp] = '|';
6055 memcpy(&str[lenp + 1], token2, lenn);
6056 str[lenn + lenp + 1] = 0;
6057
6058 atom->valuep = str;
Daniel Veillardf8e3db02012-09-11 13:26:36 +08006059 }
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00006060 atom->data = data;
6061 atom->quant = XML_REGEXP_QUANT_ONCEONLY;
Daniel Veillard11ce4002006-03-10 00:36:23 +00006062 atom->min = min;
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00006063 atom->max = max;
6064 /*
6065 * associate a counter to the transition.
6066 */
6067 counter = xmlRegGetCounter(am);
6068 am->counters[counter].min = 1;
6069 am->counters[counter].max = 1;
6070
6071 /* xmlFAGenerateTransitions(am, from, to, atom); */
6072 if (to == NULL) {
6073 to = xmlRegNewState(am);
6074 xmlRegStatePush(am, to);
6075 }
Daniel Veillard5de09382005-09-26 17:18:17 +00006076 xmlRegStateAddTrans(am, from, atom, to, counter, -1);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00006077 xmlRegAtomPush(am, atom);
6078 am->state = to;
6079 return(to);
6080}
6081
Daniel Veillardf8e3db02012-09-11 13:26:36 +08006082
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00006083
6084/**
Daniel Veillard7646b182002-04-20 06:41:40 +00006085 * xmlAutomataNewOnceTrans:
6086 * @am: an automata
6087 * @from: the starting point of the transition
6088 * @to: the target point of the transition or NULL
6089 * @token: the input string associated to that transition
6090 * @min: the minimum successive occurences of token
Daniel Veillarda9b66d02002-12-11 14:23:49 +00006091 * @max: the maximum successive occurences of token
6092 * @data: data associated to the transition
Daniel Veillard7646b182002-04-20 06:41:40 +00006093 *
William M. Brackddf71d62004-05-06 04:17:26 +00006094 * If @to is NULL, this creates first a new target state in the automata
Daniel Veillard7646b182002-04-20 06:41:40 +00006095 * and then adds a transition from the @from state to the target state
6096 * activated by a succession of input of value @token and whose number
William M. Brackddf71d62004-05-06 04:17:26 +00006097 * is between @min and @max, moreover that transition can only be crossed
Daniel Veillard7646b182002-04-20 06:41:40 +00006098 * once.
6099 *
6100 * Returns the target state or NULL in case of error
6101 */
6102xmlAutomataStatePtr
6103xmlAutomataNewOnceTrans(xmlAutomataPtr am, xmlAutomataStatePtr from,
6104 xmlAutomataStatePtr to, const xmlChar *token,
6105 int min, int max, void *data) {
6106 xmlRegAtomPtr atom;
6107 int counter;
6108
6109 if ((am == NULL) || (from == NULL) || (token == NULL))
6110 return(NULL);
6111 if (min < 1)
6112 return(NULL);
6113 if ((max < min) || (max < 1))
6114 return(NULL);
6115 atom = xmlRegNewAtom(am, XML_REGEXP_STRING);
6116 if (atom == NULL)
6117 return(NULL);
6118 atom->valuep = xmlStrdup(token);
6119 atom->data = data;
6120 atom->quant = XML_REGEXP_QUANT_ONCEONLY;
Daniel Veillard11ce4002006-03-10 00:36:23 +00006121 atom->min = min;
Daniel Veillard7646b182002-04-20 06:41:40 +00006122 atom->max = max;
6123 /*
6124 * associate a counter to the transition.
6125 */
6126 counter = xmlRegGetCounter(am);
6127 am->counters[counter].min = 1;
6128 am->counters[counter].max = 1;
6129
6130 /* xmlFAGenerateTransitions(am, from, to, atom); */
6131 if (to == NULL) {
6132 to = xmlRegNewState(am);
6133 xmlRegStatePush(am, to);
6134 }
Daniel Veillard5de09382005-09-26 17:18:17 +00006135 xmlRegStateAddTrans(am, from, atom, to, counter, -1);
Daniel Veillard7646b182002-04-20 06:41:40 +00006136 xmlRegAtomPush(am, atom);
6137 am->state = to;
Daniel Veillard7646b182002-04-20 06:41:40 +00006138 return(to);
6139}
6140
6141/**
Daniel Veillard4255d502002-04-16 15:50:10 +00006142 * xmlAutomataNewState:
6143 * @am: an automata
6144 *
6145 * Create a new disconnected state in the automata
6146 *
6147 * Returns the new state or NULL in case of error
6148 */
6149xmlAutomataStatePtr
6150xmlAutomataNewState(xmlAutomataPtr am) {
Daniel Veillardf8e3db02012-09-11 13:26:36 +08006151 xmlAutomataStatePtr to;
Daniel Veillard4255d502002-04-16 15:50:10 +00006152
6153 if (am == NULL)
6154 return(NULL);
6155 to = xmlRegNewState(am);
6156 xmlRegStatePush(am, to);
6157 return(to);
6158}
6159
6160/**
Daniel Veillarda9b66d02002-12-11 14:23:49 +00006161 * xmlAutomataNewEpsilon:
Daniel Veillard4255d502002-04-16 15:50:10 +00006162 * @am: an automata
6163 * @from: the starting point of the transition
6164 * @to: the target point of the transition or NULL
6165 *
William M. Brackddf71d62004-05-06 04:17:26 +00006166 * If @to is NULL, this creates first a new target state in the automata
6167 * and then adds an epsilon transition from the @from state to the
Daniel Veillard4255d502002-04-16 15:50:10 +00006168 * target state
6169 *
6170 * Returns the target state or NULL in case of error
6171 */
6172xmlAutomataStatePtr
6173xmlAutomataNewEpsilon(xmlAutomataPtr am, xmlAutomataStatePtr from,
6174 xmlAutomataStatePtr to) {
6175 if ((am == NULL) || (from == NULL))
6176 return(NULL);
6177 xmlFAGenerateEpsilonTransition(am, from, to);
6178 if (to == NULL)
6179 return(am->state);
6180 return(to);
6181}
6182
Daniel Veillardb509f152002-04-17 16:28:10 +00006183/**
Daniel Veillard7646b182002-04-20 06:41:40 +00006184 * xmlAutomataNewAllTrans:
6185 * @am: an automata
6186 * @from: the starting point of the transition
6187 * @to: the target point of the transition or NULL
Daniel Veillarda9b66d02002-12-11 14:23:49 +00006188 * @lax: allow to transition if not all all transitions have been activated
Daniel Veillard7646b182002-04-20 06:41:40 +00006189 *
William M. Brackddf71d62004-05-06 04:17:26 +00006190 * If @to is NULL, this creates first a new target state in the automata
Daniel Veillard7646b182002-04-20 06:41:40 +00006191 * and then adds a an ALL transition from the @from state to the
6192 * target state. That transition is an epsilon transition allowed only when
6193 * all transitions from the @from node have been activated.
6194 *
6195 * Returns the target state or NULL in case of error
6196 */
6197xmlAutomataStatePtr
6198xmlAutomataNewAllTrans(xmlAutomataPtr am, xmlAutomataStatePtr from,
Daniel Veillard441bc322002-04-20 17:38:48 +00006199 xmlAutomataStatePtr to, int lax) {
Daniel Veillard7646b182002-04-20 06:41:40 +00006200 if ((am == NULL) || (from == NULL))
6201 return(NULL);
Daniel Veillard441bc322002-04-20 17:38:48 +00006202 xmlFAGenerateAllTransition(am, from, to, lax);
Daniel Veillard7646b182002-04-20 06:41:40 +00006203 if (to == NULL)
6204 return(am->state);
6205 return(to);
6206}
6207
6208/**
Daniel Veillardb509f152002-04-17 16:28:10 +00006209 * xmlAutomataNewCounter:
6210 * @am: an automata
6211 * @min: the minimal value on the counter
6212 * @max: the maximal value on the counter
6213 *
6214 * Create a new counter
6215 *
6216 * Returns the counter number or -1 in case of error
6217 */
Daniel Veillardf8e3db02012-09-11 13:26:36 +08006218int
Daniel Veillardb509f152002-04-17 16:28:10 +00006219xmlAutomataNewCounter(xmlAutomataPtr am, int min, int max) {
6220 int ret;
6221
6222 if (am == NULL)
6223 return(-1);
6224
6225 ret = xmlRegGetCounter(am);
6226 if (ret < 0)
6227 return(-1);
6228 am->counters[ret].min = min;
6229 am->counters[ret].max = max;
6230 return(ret);
6231}
6232
6233/**
6234 * xmlAutomataNewCountedTrans:
6235 * @am: an automata
6236 * @from: the starting point of the transition
6237 * @to: the target point of the transition or NULL
6238 * @counter: the counter associated to that transition
6239 *
William M. Brackddf71d62004-05-06 04:17:26 +00006240 * If @to is NULL, this creates first a new target state in the automata
Daniel Veillardb509f152002-04-17 16:28:10 +00006241 * and then adds an epsilon transition from the @from state to the target state
6242 * which will increment the counter provided
6243 *
6244 * Returns the target state or NULL in case of error
6245 */
6246xmlAutomataStatePtr
6247xmlAutomataNewCountedTrans(xmlAutomataPtr am, xmlAutomataStatePtr from,
6248 xmlAutomataStatePtr to, int counter) {
6249 if ((am == NULL) || (from == NULL) || (counter < 0))
6250 return(NULL);
6251 xmlFAGenerateCountedEpsilonTransition(am, from, to, counter);
6252 if (to == NULL)
6253 return(am->state);
6254 return(to);
6255}
6256
6257/**
6258 * xmlAutomataNewCounterTrans:
6259 * @am: an automata
6260 * @from: the starting point of the transition
6261 * @to: the target point of the transition or NULL
6262 * @counter: the counter associated to that transition
6263 *
William M. Brackddf71d62004-05-06 04:17:26 +00006264 * If @to is NULL, this creates first a new target state in the automata
Daniel Veillardb509f152002-04-17 16:28:10 +00006265 * and then adds an epsilon transition from the @from state to the target state
6266 * which will be allowed only if the counter is within the right range.
6267 *
6268 * Returns the target state or NULL in case of error
6269 */
6270xmlAutomataStatePtr
6271xmlAutomataNewCounterTrans(xmlAutomataPtr am, xmlAutomataStatePtr from,
6272 xmlAutomataStatePtr to, int counter) {
6273 if ((am == NULL) || (from == NULL) || (counter < 0))
6274 return(NULL);
6275 xmlFAGenerateCountedTransition(am, from, to, counter);
6276 if (to == NULL)
6277 return(am->state);
6278 return(to);
6279}
Daniel Veillard4255d502002-04-16 15:50:10 +00006280
6281/**
6282 * xmlAutomataCompile:
6283 * @am: an automata
6284 *
6285 * Compile the automata into a Reg Exp ready for being executed.
6286 * The automata should be free after this point.
6287 *
6288 * Returns the compiled regexp or NULL in case of error
6289 */
Daniel Veillardf8e3db02012-09-11 13:26:36 +08006290xmlRegexpPtr
Daniel Veillard4255d502002-04-16 15:50:10 +00006291xmlAutomataCompile(xmlAutomataPtr am) {
6292 xmlRegexpPtr ret;
6293
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00006294 if ((am == NULL) || (am->error != 0)) return(NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +00006295 xmlFAEliminateEpsilonTransitions(am);
Daniel Veillard23e73572002-09-19 19:56:43 +00006296 /* xmlFAComputesDeterminism(am); */
Daniel Veillard4255d502002-04-16 15:50:10 +00006297 ret = xmlRegEpxFromParse(am);
6298
6299 return(ret);
6300}
Daniel Veillarde19fc232002-04-22 16:01:24 +00006301
6302/**
6303 * xmlAutomataIsDeterminist:
6304 * @am: an automata
6305 *
6306 * Checks if an automata is determinist.
6307 *
6308 * Returns 1 if true, 0 if not, and -1 in case of error
6309 */
Daniel Veillardf8e3db02012-09-11 13:26:36 +08006310int
Daniel Veillarde19fc232002-04-22 16:01:24 +00006311xmlAutomataIsDeterminist(xmlAutomataPtr am) {
6312 int ret;
6313
6314 if (am == NULL)
6315 return(-1);
6316
6317 ret = xmlFAComputesDeterminism(am);
6318 return(ret);
6319}
Daniel Veillard4255d502002-04-16 15:50:10 +00006320#endif /* LIBXML_AUTOMATA_ENABLED */
Daniel Veillard81a8ec62005-08-22 00:20:58 +00006321
6322#ifdef LIBXML_EXPR_ENABLED
6323/************************************************************************
6324 * *
6325 * Formal Expression handling code *
6326 * *
6327 ************************************************************************/
Daniel Veillard81a8ec62005-08-22 00:20:58 +00006328/************************************************************************
6329 * *
6330 * Expression handling context *
6331 * *
6332 ************************************************************************/
6333
6334struct _xmlExpCtxt {
6335 xmlDictPtr dict;
6336 xmlExpNodePtr *table;
6337 int size;
6338 int nbElems;
6339 int nb_nodes;
Daniel Veillard594e5df2009-09-07 14:58:47 +02006340 int maxNodes;
Daniel Veillard81a8ec62005-08-22 00:20:58 +00006341 const char *expr;
6342 const char *cur;
6343 int nb_cons;
Daniel Veillard81a8ec62005-08-22 00:20:58 +00006344 int tabSize;
6345};
6346
6347/**
6348 * xmlExpNewCtxt:
6349 * @maxNodes: the maximum number of nodes
6350 * @dict: optional dictionnary to use internally
6351 *
6352 * Creates a new context for manipulating expressions
6353 *
6354 * Returns the context or NULL in case of error
6355 */
6356xmlExpCtxtPtr
6357xmlExpNewCtxt(int maxNodes, xmlDictPtr dict) {
6358 xmlExpCtxtPtr ret;
6359 int size = 256;
6360
6361 if (maxNodes <= 4096)
6362 maxNodes = 4096;
Daniel Veillardf8e3db02012-09-11 13:26:36 +08006363
Daniel Veillard81a8ec62005-08-22 00:20:58 +00006364 ret = (xmlExpCtxtPtr) xmlMalloc(sizeof(xmlExpCtxt));
6365 if (ret == NULL)
6366 return(NULL);
6367 memset(ret, 0, sizeof(xmlExpCtxt));
6368 ret->size = size;
6369 ret->nbElems = 0;
Daniel Veillard594e5df2009-09-07 14:58:47 +02006370 ret->maxNodes = maxNodes;
Daniel Veillard81a8ec62005-08-22 00:20:58 +00006371 ret->table = xmlMalloc(size * sizeof(xmlExpNodePtr));
6372 if (ret->table == NULL) {
6373 xmlFree(ret);
6374 return(NULL);
6375 }
6376 memset(ret->table, 0, size * sizeof(xmlExpNodePtr));
6377 if (dict == NULL) {
6378 ret->dict = xmlDictCreate();
6379 if (ret->dict == NULL) {
6380 xmlFree(ret->table);
6381 xmlFree(ret);
6382 return(NULL);
6383 }
6384 } else {
6385 ret->dict = dict;
6386 xmlDictReference(ret->dict);
6387 }
6388 return(ret);
6389}
6390
6391/**
6392 * xmlExpFreeCtxt:
6393 * @ctxt: an expression context
6394 *
6395 * Free an expression context
6396 */
6397void
6398xmlExpFreeCtxt(xmlExpCtxtPtr ctxt) {
6399 if (ctxt == NULL)
6400 return;
6401 xmlDictFree(ctxt->dict);
6402 if (ctxt->table != NULL)
6403 xmlFree(ctxt->table);
6404 xmlFree(ctxt);
6405}
6406
6407/************************************************************************
6408 * *
6409 * Structure associated to an expression node *
6410 * *
6411 ************************************************************************/
Daniel Veillard465a0002005-08-22 12:07:04 +00006412#define MAX_NODES 10000
6413
6414/* #define DEBUG_DERIV */
6415
6416/*
Daniel Veillardf8e3db02012-09-11 13:26:36 +08006417 * TODO:
Daniel Veillard465a0002005-08-22 12:07:04 +00006418 * - Wildcards
6419 * - public API for creation
6420 *
6421 * Started
6422 * - regression testing
6423 *
6424 * Done
6425 * - split into module and test tool
6426 * - memleaks
6427 */
Daniel Veillard81a8ec62005-08-22 00:20:58 +00006428
6429typedef enum {
6430 XML_EXP_NILABLE = (1 << 0)
6431} xmlExpNodeInfo;
6432
6433#define IS_NILLABLE(node) ((node)->info & XML_EXP_NILABLE)
6434
6435struct _xmlExpNode {
6436 unsigned char type;/* xmlExpNodeType */
6437 unsigned char info;/* OR of xmlExpNodeInfo */
6438 unsigned short key; /* the hash key */
6439 unsigned int ref; /* The number of references */
6440 int c_max; /* the maximum length it can consume */
6441 xmlExpNodePtr exp_left;
6442 xmlExpNodePtr next;/* the next node in the hash table or free list */
6443 union {
6444 struct {
6445 int f_min;
6446 int f_max;
6447 } count;
6448 struct {
6449 xmlExpNodePtr f_right;
6450 } children;
6451 const xmlChar *f_str;
6452 } field;
6453};
6454
6455#define exp_min field.count.f_min
6456#define exp_max field.count.f_max
6457/* #define exp_left field.children.f_left */
6458#define exp_right field.children.f_right
6459#define exp_str field.f_str
6460
6461static xmlExpNodePtr xmlExpNewNode(xmlExpCtxtPtr ctxt, xmlExpNodeType type);
6462static xmlExpNode forbiddenExpNode = {
6463 XML_EXP_FORBID, 0, 0, 0, 0, NULL, NULL, {{ 0, 0}}
6464};
6465xmlExpNodePtr forbiddenExp = &forbiddenExpNode;
6466static xmlExpNode emptyExpNode = {
6467 XML_EXP_EMPTY, 1, 0, 0, 0, NULL, NULL, {{ 0, 0}}
6468};
6469xmlExpNodePtr emptyExp = &emptyExpNode;
6470
6471/************************************************************************
6472 * *
6473 * The custom hash table for unicity and canonicalization *
6474 * of sub-expressions pointers *
6475 * *
6476 ************************************************************************/
6477/*
6478 * xmlExpHashNameComputeKey:
6479 * Calculate the hash key for a token
6480 */
6481static unsigned short
6482xmlExpHashNameComputeKey(const xmlChar *name) {
6483 unsigned short value = 0L;
6484 char ch;
Daniel Veillardf8e3db02012-09-11 13:26:36 +08006485
Daniel Veillard81a8ec62005-08-22 00:20:58 +00006486 if (name != NULL) {
6487 value += 30 * (*name);
6488 while ((ch = *name++) != 0) {
6489 value = value ^ ((value << 5) + (value >> 3) + (unsigned long)ch);
6490 }
6491 }
6492 return (value);
6493}
6494
6495/*
6496 * xmlExpHashComputeKey:
6497 * Calculate the hash key for a compound expression
6498 */
6499static unsigned short
6500xmlExpHashComputeKey(xmlExpNodeType type, xmlExpNodePtr left,
6501 xmlExpNodePtr right) {
6502 unsigned long value;
6503 unsigned short ret;
Daniel Veillardf8e3db02012-09-11 13:26:36 +08006504
Daniel Veillard81a8ec62005-08-22 00:20:58 +00006505 switch (type) {
6506 case XML_EXP_SEQ:
6507 value = left->key;
6508 value += right->key;
6509 value *= 3;
6510 ret = (unsigned short) value;
6511 break;
6512 case XML_EXP_OR:
6513 value = left->key;
6514 value += right->key;
6515 value *= 7;
6516 ret = (unsigned short) value;
6517 break;
6518 case XML_EXP_COUNT:
6519 value = left->key;
6520 value += right->key;
6521 ret = (unsigned short) value;
6522 break;
6523 default:
6524 ret = 0;
6525 }
6526 return(ret);
6527}
6528
6529
6530static xmlExpNodePtr
6531xmlExpNewNode(xmlExpCtxtPtr ctxt, xmlExpNodeType type) {
6532 xmlExpNodePtr ret;
6533
6534 if (ctxt->nb_nodes >= MAX_NODES)
6535 return(NULL);
6536 ret = (xmlExpNodePtr) xmlMalloc(sizeof(xmlExpNode));
6537 if (ret == NULL)
6538 return(NULL);
6539 memset(ret, 0, sizeof(xmlExpNode));
6540 ret->type = type;
6541 ret->next = NULL;
6542 ctxt->nb_nodes++;
6543 ctxt->nb_cons++;
6544 return(ret);
6545}
6546
6547/**
6548 * xmlExpHashGetEntry:
6549 * @table: the hash table
6550 *
6551 * Get the unique entry from the hash table. The entry is created if
6552 * needed. @left and @right are consumed, i.e. their ref count will
6553 * be decremented by the operation.
6554 *
6555 * Returns the pointer or NULL in case of error
6556 */
6557static xmlExpNodePtr
6558xmlExpHashGetEntry(xmlExpCtxtPtr ctxt, xmlExpNodeType type,
6559 xmlExpNodePtr left, xmlExpNodePtr right,
6560 const xmlChar *name, int min, int max) {
6561 unsigned short kbase, key;
6562 xmlExpNodePtr entry;
6563 xmlExpNodePtr insert;
6564
6565 if (ctxt == NULL)
6566 return(NULL);
6567
6568 /*
6569 * Check for duplicate and insertion location.
6570 */
6571 if (type == XML_EXP_ATOM) {
6572 kbase = xmlExpHashNameComputeKey(name);
6573 } else if (type == XML_EXP_COUNT) {
6574 /* COUNT reduction rule 1 */
6575 /* a{1} -> a */
6576 if (min == max) {
6577 if (min == 1) {
6578 return(left);
6579 }
6580 if (min == 0) {
6581 xmlExpFree(ctxt, left);
6582 return(emptyExp);
6583 }
6584 }
6585 if (min < 0) {
6586 xmlExpFree(ctxt, left);
6587 return(forbiddenExp);
6588 }
6589 if (max == -1)
6590 kbase = min + 79;
6591 else
6592 kbase = max - min;
6593 kbase += left->key;
6594 } else if (type == XML_EXP_OR) {
6595 /* Forbid reduction rules */
6596 if (left->type == XML_EXP_FORBID) {
6597 xmlExpFree(ctxt, left);
6598 return(right);
6599 }
6600 if (right->type == XML_EXP_FORBID) {
6601 xmlExpFree(ctxt, right);
6602 return(left);
6603 }
6604
6605 /* OR reduction rule 1 */
6606 /* a | a reduced to a */
6607 if (left == right) {
6608 left->ref--;
6609 return(left);
6610 }
6611 /* OR canonicalization rule 1 */
6612 /* linearize (a | b) | c into a | (b | c) */
6613 if ((left->type == XML_EXP_OR) && (right->type != XML_EXP_OR)) {
6614 xmlExpNodePtr tmp = left;
6615 left = right;
6616 right = tmp;
6617 }
6618 /* OR reduction rule 2 */
6619 /* a | (a | b) and b | (a | b) are reduced to a | b */
6620 if (right->type == XML_EXP_OR) {
6621 if ((left == right->exp_left) ||
6622 (left == right->exp_right)) {
6623 xmlExpFree(ctxt, left);
6624 return(right);
6625 }
6626 }
6627 /* OR canonicalization rule 2 */
6628 /* linearize (a | b) | c into a | (b | c) */
6629 if (left->type == XML_EXP_OR) {
6630 xmlExpNodePtr tmp;
6631
6632 /* OR canonicalization rule 2 */
6633 if ((left->exp_right->type != XML_EXP_OR) &&
6634 (left->exp_right->key < left->exp_left->key)) {
6635 tmp = left->exp_right;
6636 left->exp_right = left->exp_left;
6637 left->exp_left = tmp;
6638 }
6639 left->exp_right->ref++;
6640 tmp = xmlExpHashGetEntry(ctxt, XML_EXP_OR, left->exp_right, right,
6641 NULL, 0, 0);
6642 left->exp_left->ref++;
6643 tmp = xmlExpHashGetEntry(ctxt, XML_EXP_OR, left->exp_left, tmp,
6644 NULL, 0, 0);
Daniel Veillardf8e3db02012-09-11 13:26:36 +08006645
Daniel Veillard81a8ec62005-08-22 00:20:58 +00006646 xmlExpFree(ctxt, left);
6647 return(tmp);
6648 }
6649 if (right->type == XML_EXP_OR) {
6650 /* Ordering in the tree */
6651 /* C | (A | B) -> A | (B | C) */
6652 if (left->key > right->exp_right->key) {
6653 xmlExpNodePtr tmp;
6654 right->exp_right->ref++;
6655 tmp = xmlExpHashGetEntry(ctxt, XML_EXP_OR, right->exp_right,
6656 left, NULL, 0, 0);
6657 right->exp_left->ref++;
6658 tmp = xmlExpHashGetEntry(ctxt, XML_EXP_OR, right->exp_left,
6659 tmp, NULL, 0, 0);
6660 xmlExpFree(ctxt, right);
6661 return(tmp);
6662 }
6663 /* Ordering in the tree */
6664 /* B | (A | C) -> A | (B | C) */
6665 if (left->key > right->exp_left->key) {
6666 xmlExpNodePtr tmp;
6667 right->exp_right->ref++;
6668 tmp = xmlExpHashGetEntry(ctxt, XML_EXP_OR, left,
6669 right->exp_right, NULL, 0, 0);
6670 right->exp_left->ref++;
6671 tmp = xmlExpHashGetEntry(ctxt, XML_EXP_OR, right->exp_left,
6672 tmp, NULL, 0, 0);
6673 xmlExpFree(ctxt, right);
6674 return(tmp);
6675 }
6676 }
6677 /* we know both types are != XML_EXP_OR here */
6678 else if (left->key > right->key) {
6679 xmlExpNodePtr tmp = left;
6680 left = right;
6681 right = tmp;
6682 }
6683 kbase = xmlExpHashComputeKey(type, left, right);
6684 } else if (type == XML_EXP_SEQ) {
6685 /* Forbid reduction rules */
6686 if (left->type == XML_EXP_FORBID) {
6687 xmlExpFree(ctxt, right);
6688 return(left);
6689 }
6690 if (right->type == XML_EXP_FORBID) {
6691 xmlExpFree(ctxt, left);
6692 return(right);
6693 }
6694 /* Empty reduction rules */
6695 if (right->type == XML_EXP_EMPTY) {
6696 return(left);
6697 }
6698 if (left->type == XML_EXP_EMPTY) {
6699 return(right);
6700 }
6701 kbase = xmlExpHashComputeKey(type, left, right);
Daniel Veillardf8e3db02012-09-11 13:26:36 +08006702 } else
Daniel Veillard81a8ec62005-08-22 00:20:58 +00006703 return(NULL);
6704
6705 key = kbase % ctxt->size;
6706 if (ctxt->table[key] != NULL) {
6707 for (insert = ctxt->table[key]; insert != NULL;
6708 insert = insert->next) {
6709 if ((insert->key == kbase) &&
6710 (insert->type == type)) {
6711 if (type == XML_EXP_ATOM) {
6712 if (name == insert->exp_str) {
6713 insert->ref++;
6714 return(insert);
6715 }
6716 } else if (type == XML_EXP_COUNT) {
6717 if ((insert->exp_min == min) && (insert->exp_max == max) &&
6718 (insert->exp_left == left)) {
6719 insert->ref++;
6720 left->ref--;
6721 return(insert);
6722 }
6723 } else if ((insert->exp_left == left) &&
6724 (insert->exp_right == right)) {
6725 insert->ref++;
6726 left->ref--;
6727 right->ref--;
6728 return(insert);
6729 }
6730 }
6731 }
6732 }
6733
6734 entry = xmlExpNewNode(ctxt, type);
6735 if (entry == NULL)
6736 return(NULL);
6737 entry->key = kbase;
6738 if (type == XML_EXP_ATOM) {
6739 entry->exp_str = name;
6740 entry->c_max = 1;
6741 } else if (type == XML_EXP_COUNT) {
6742 entry->exp_min = min;
6743 entry->exp_max = max;
6744 entry->exp_left = left;
6745 if ((min == 0) || (IS_NILLABLE(left)))
6746 entry->info |= XML_EXP_NILABLE;
6747 if (max < 0)
6748 entry->c_max = -1;
6749 else
6750 entry->c_max = max * entry->exp_left->c_max;
6751 } else {
6752 entry->exp_left = left;
6753 entry->exp_right = right;
6754 if (type == XML_EXP_OR) {
6755 if ((IS_NILLABLE(left)) || (IS_NILLABLE(right)))
6756 entry->info |= XML_EXP_NILABLE;
6757 if ((entry->exp_left->c_max == -1) ||
6758 (entry->exp_right->c_max == -1))
6759 entry->c_max = -1;
6760 else if (entry->exp_left->c_max > entry->exp_right->c_max)
6761 entry->c_max = entry->exp_left->c_max;
6762 else
6763 entry->c_max = entry->exp_right->c_max;
6764 } else {
6765 if ((IS_NILLABLE(left)) && (IS_NILLABLE(right)))
6766 entry->info |= XML_EXP_NILABLE;
6767 if ((entry->exp_left->c_max == -1) ||
6768 (entry->exp_right->c_max == -1))
6769 entry->c_max = -1;
6770 else
6771 entry->c_max = entry->exp_left->c_max + entry->exp_right->c_max;
6772 }
6773 }
6774 entry->ref = 1;
6775 if (ctxt->table[key] != NULL)
6776 entry->next = ctxt->table[key];
6777
6778 ctxt->table[key] = entry;
6779 ctxt->nbElems++;
6780
6781 return(entry);
6782}
6783
6784/**
6785 * xmlExpFree:
6786 * @ctxt: the expression context
6787 * @exp: the expression
6788 *
6789 * Dereference the expression
6790 */
6791void
6792xmlExpFree(xmlExpCtxtPtr ctxt, xmlExpNodePtr exp) {
6793 if ((exp == NULL) || (exp == forbiddenExp) || (exp == emptyExp))
6794 return;
6795 exp->ref--;
Daniel Veillard81a8ec62005-08-22 00:20:58 +00006796 if (exp->ref == 0) {
6797 unsigned short key;
6798
6799 /* Unlink it first from the hash table */
6800 key = exp->key % ctxt->size;
6801 if (ctxt->table[key] == exp) {
6802 ctxt->table[key] = exp->next;
6803 } else {
6804 xmlExpNodePtr tmp;
6805
6806 tmp = ctxt->table[key];
6807 while (tmp != NULL) {
6808 if (tmp->next == exp) {
6809 tmp->next = exp->next;
6810 break;
6811 }
6812 tmp = tmp->next;
6813 }
6814 }
6815
6816 if ((exp->type == XML_EXP_SEQ) || (exp->type == XML_EXP_OR)) {
6817 xmlExpFree(ctxt, exp->exp_left);
6818 xmlExpFree(ctxt, exp->exp_right);
6819 } else if (exp->type == XML_EXP_COUNT) {
6820 xmlExpFree(ctxt, exp->exp_left);
6821 }
6822 xmlFree(exp);
Daniel Veillard81a8ec62005-08-22 00:20:58 +00006823 ctxt->nb_nodes--;
6824 }
6825}
6826
6827/**
6828 * xmlExpRef:
6829 * @exp: the expression
6830 *
6831 * Increase the reference count of the expression
6832 */
6833void
6834xmlExpRef(xmlExpNodePtr exp) {
6835 if (exp != NULL)
6836 exp->ref++;
6837}
6838
Daniel Veillardccb4d412005-08-23 13:41:17 +00006839/**
6840 * xmlExpNewAtom:
6841 * @ctxt: the expression context
6842 * @name: the atom name
Michael Woodfb27e2c2012-09-28 08:59:33 +02006843 * @len: the atom name length in byte (or -1);
Daniel Veillardccb4d412005-08-23 13:41:17 +00006844 *
6845 * Get the atom associated to this name from that context
6846 *
6847 * Returns the node or NULL in case of error
6848 */
6849xmlExpNodePtr
6850xmlExpNewAtom(xmlExpCtxtPtr ctxt, const xmlChar *name, int len) {
6851 if ((ctxt == NULL) || (name == NULL))
6852 return(NULL);
6853 name = xmlDictLookup(ctxt->dict, name, len);
6854 if (name == NULL)
6855 return(NULL);
6856 return(xmlExpHashGetEntry(ctxt, XML_EXP_ATOM, NULL, NULL, name, 0, 0));
6857}
6858
6859/**
6860 * xmlExpNewOr:
6861 * @ctxt: the expression context
6862 * @left: left expression
6863 * @right: right expression
6864 *
6865 * Get the atom associated to the choice @left | @right
6866 * Note that @left and @right are consumed in the operation, to keep
6867 * an handle on them use xmlExpRef() and use xmlExpFree() to release them,
6868 * this is true even in case of failure (unless ctxt == NULL).
6869 *
6870 * Returns the node or NULL in case of error
6871 */
6872xmlExpNodePtr
6873xmlExpNewOr(xmlExpCtxtPtr ctxt, xmlExpNodePtr left, xmlExpNodePtr right) {
Daniel Veillard11ce4002006-03-10 00:36:23 +00006874 if (ctxt == NULL)
6875 return(NULL);
6876 if ((left == NULL) || (right == NULL)) {
Daniel Veillardccb4d412005-08-23 13:41:17 +00006877 xmlExpFree(ctxt, left);
6878 xmlExpFree(ctxt, right);
6879 return(NULL);
6880 }
6881 return(xmlExpHashGetEntry(ctxt, XML_EXP_OR, left, right, NULL, 0, 0));
6882}
6883
6884/**
6885 * xmlExpNewSeq:
6886 * @ctxt: the expression context
6887 * @left: left expression
6888 * @right: right expression
6889 *
6890 * Get the atom associated to the sequence @left , @right
6891 * Note that @left and @right are consumed in the operation, to keep
6892 * an handle on them use xmlExpRef() and use xmlExpFree() to release them,
6893 * this is true even in case of failure (unless ctxt == NULL).
6894 *
6895 * Returns the node or NULL in case of error
6896 */
6897xmlExpNodePtr
6898xmlExpNewSeq(xmlExpCtxtPtr ctxt, xmlExpNodePtr left, xmlExpNodePtr right) {
Daniel Veillard11ce4002006-03-10 00:36:23 +00006899 if (ctxt == NULL)
6900 return(NULL);
6901 if ((left == NULL) || (right == NULL)) {
Daniel Veillardccb4d412005-08-23 13:41:17 +00006902 xmlExpFree(ctxt, left);
6903 xmlExpFree(ctxt, right);
6904 return(NULL);
6905 }
6906 return(xmlExpHashGetEntry(ctxt, XML_EXP_SEQ, left, right, NULL, 0, 0));
6907}
6908
6909/**
6910 * xmlExpNewRange:
6911 * @ctxt: the expression context
6912 * @subset: the expression to be repeated
6913 * @min: the lower bound for the repetition
6914 * @max: the upper bound for the repetition, -1 means infinite
6915 *
6916 * Get the atom associated to the range (@subset){@min, @max}
6917 * Note that @subset is consumed in the operation, to keep
6918 * an handle on it use xmlExpRef() and use xmlExpFree() to release it,
6919 * this is true even in case of failure (unless ctxt == NULL).
6920 *
6921 * Returns the node or NULL in case of error
6922 */
6923xmlExpNodePtr
6924xmlExpNewRange(xmlExpCtxtPtr ctxt, xmlExpNodePtr subset, int min, int max) {
Daniel Veillard11ce4002006-03-10 00:36:23 +00006925 if (ctxt == NULL)
6926 return(NULL);
6927 if ((subset == NULL) || (min < 0) || (max < -1) ||
Daniel Veillardccb4d412005-08-23 13:41:17 +00006928 ((max >= 0) && (min > max))) {
6929 xmlExpFree(ctxt, subset);
6930 return(NULL);
6931 }
6932 return(xmlExpHashGetEntry(ctxt, XML_EXP_COUNT, subset,
6933 NULL, NULL, min, max));
6934}
6935
Daniel Veillard81a8ec62005-08-22 00:20:58 +00006936/************************************************************************
6937 * *
6938 * Public API for operations on expressions *
6939 * *
6940 ************************************************************************/
6941
6942static int
Daniel Veillardf8e3db02012-09-11 13:26:36 +08006943xmlExpGetLanguageInt(xmlExpCtxtPtr ctxt, xmlExpNodePtr exp,
Daniel Veillard81a8ec62005-08-22 00:20:58 +00006944 const xmlChar**list, int len, int nb) {
6945 int tmp, tmp2;
6946tail:
6947 switch (exp->type) {
6948 case XML_EXP_EMPTY:
6949 return(0);
6950 case XML_EXP_ATOM:
6951 for (tmp = 0;tmp < nb;tmp++)
6952 if (list[tmp] == exp->exp_str)
6953 return(0);
6954 if (nb >= len)
6955 return(-2);
Daniel Veillard13cee4e2009-09-05 14:52:55 +02006956 list[nb] = exp->exp_str;
Daniel Veillard81a8ec62005-08-22 00:20:58 +00006957 return(1);
6958 case XML_EXP_COUNT:
6959 exp = exp->exp_left;
6960 goto tail;
6961 case XML_EXP_SEQ:
6962 case XML_EXP_OR:
6963 tmp = xmlExpGetLanguageInt(ctxt, exp->exp_left, list, len, nb);
6964 if (tmp < 0)
6965 return(tmp);
6966 tmp2 = xmlExpGetLanguageInt(ctxt, exp->exp_right, list, len,
6967 nb + tmp);
6968 if (tmp2 < 0)
6969 return(tmp2);
6970 return(tmp + tmp2);
6971 }
6972 return(-1);
6973}
6974
6975/**
6976 * xmlExpGetLanguage:
6977 * @ctxt: the expression context
6978 * @exp: the expression
Daniel Veillard7802ba52005-10-27 11:56:20 +00006979 * @langList: where to store the tokens
Michael Woodfb27e2c2012-09-28 08:59:33 +02006980 * @len: the allocated length of @list
Daniel Veillard81a8ec62005-08-22 00:20:58 +00006981 *
6982 * Find all the strings used in @exp and store them in @list
6983 *
6984 * Returns the number of unique strings found, -1 in case of errors and
6985 * -2 if there is more than @len strings
6986 */
6987int
Daniel Veillardf8e3db02012-09-11 13:26:36 +08006988xmlExpGetLanguage(xmlExpCtxtPtr ctxt, xmlExpNodePtr exp,
Daniel Veillard7802ba52005-10-27 11:56:20 +00006989 const xmlChar**langList, int len) {
6990 if ((ctxt == NULL) || (exp == NULL) || (langList == NULL) || (len <= 0))
Daniel Veillard81a8ec62005-08-22 00:20:58 +00006991 return(-1);
Daniel Veillard7802ba52005-10-27 11:56:20 +00006992 return(xmlExpGetLanguageInt(ctxt, exp, langList, len, 0));
Daniel Veillard81a8ec62005-08-22 00:20:58 +00006993}
6994
6995static int
Daniel Veillardf8e3db02012-09-11 13:26:36 +08006996xmlExpGetStartInt(xmlExpCtxtPtr ctxt, xmlExpNodePtr exp,
Daniel Veillard81a8ec62005-08-22 00:20:58 +00006997 const xmlChar**list, int len, int nb) {
6998 int tmp, tmp2;
6999tail:
7000 switch (exp->type) {
7001 case XML_EXP_FORBID:
7002 return(0);
7003 case XML_EXP_EMPTY:
7004 return(0);
7005 case XML_EXP_ATOM:
7006 for (tmp = 0;tmp < nb;tmp++)
7007 if (list[tmp] == exp->exp_str)
7008 return(0);
7009 if (nb >= len)
7010 return(-2);
Daniel Veillard13cee4e2009-09-05 14:52:55 +02007011 list[nb] = exp->exp_str;
Daniel Veillard81a8ec62005-08-22 00:20:58 +00007012 return(1);
7013 case XML_EXP_COUNT:
7014 exp = exp->exp_left;
7015 goto tail;
7016 case XML_EXP_SEQ:
7017 tmp = xmlExpGetStartInt(ctxt, exp->exp_left, list, len, nb);
7018 if (tmp < 0)
7019 return(tmp);
7020 if (IS_NILLABLE(exp->exp_left)) {
7021 tmp2 = xmlExpGetStartInt(ctxt, exp->exp_right, list, len,
7022 nb + tmp);
7023 if (tmp2 < 0)
7024 return(tmp2);
7025 tmp += tmp2;
7026 }
7027 return(tmp);
7028 case XML_EXP_OR:
7029 tmp = xmlExpGetStartInt(ctxt, exp->exp_left, list, len, nb);
7030 if (tmp < 0)
7031 return(tmp);
7032 tmp2 = xmlExpGetStartInt(ctxt, exp->exp_right, list, len,
7033 nb + tmp);
7034 if (tmp2 < 0)
7035 return(tmp2);
7036 return(tmp + tmp2);
7037 }
7038 return(-1);
7039}
7040
7041/**
7042 * xmlExpGetStart:
7043 * @ctxt: the expression context
7044 * @exp: the expression
Daniel Veillard7802ba52005-10-27 11:56:20 +00007045 * @tokList: where to store the tokens
Michael Woodfb27e2c2012-09-28 08:59:33 +02007046 * @len: the allocated length of @list
Daniel Veillard81a8ec62005-08-22 00:20:58 +00007047 *
7048 * Find all the strings that appears at the start of the languages
7049 * accepted by @exp and store them in @list. E.g. for (a, b) | c
7050 * it will return the list [a, c]
7051 *
7052 * Returns the number of unique strings found, -1 in case of errors and
7053 * -2 if there is more than @len strings
7054 */
7055int
Daniel Veillardf8e3db02012-09-11 13:26:36 +08007056xmlExpGetStart(xmlExpCtxtPtr ctxt, xmlExpNodePtr exp,
Daniel Veillard7802ba52005-10-27 11:56:20 +00007057 const xmlChar**tokList, int len) {
7058 if ((ctxt == NULL) || (exp == NULL) || (tokList == NULL) || (len <= 0))
Daniel Veillard81a8ec62005-08-22 00:20:58 +00007059 return(-1);
Daniel Veillard7802ba52005-10-27 11:56:20 +00007060 return(xmlExpGetStartInt(ctxt, exp, tokList, len, 0));
Daniel Veillard81a8ec62005-08-22 00:20:58 +00007061}
7062
7063/**
7064 * xmlExpIsNillable:
7065 * @exp: the expression
7066 *
7067 * Finds if the expression is nillable, i.e. if it accepts the empty sequqnce
7068 *
7069 * Returns 1 if nillable, 0 if not and -1 in case of error
7070 */
7071int
7072xmlExpIsNillable(xmlExpNodePtr exp) {
7073 if (exp == NULL)
7074 return(-1);
7075 return(IS_NILLABLE(exp) != 0);
7076}
7077
7078static xmlExpNodePtr
7079xmlExpStringDeriveInt(xmlExpCtxtPtr ctxt, xmlExpNodePtr exp, const xmlChar *str)
7080{
7081 xmlExpNodePtr ret;
7082
7083 switch (exp->type) {
7084 case XML_EXP_EMPTY:
7085 return(forbiddenExp);
7086 case XML_EXP_FORBID:
7087 return(forbiddenExp);
7088 case XML_EXP_ATOM:
7089 if (exp->exp_str == str) {
7090#ifdef DEBUG_DERIV
7091 printf("deriv atom: equal => Empty\n");
7092#endif
7093 ret = emptyExp;
7094 } else {
7095#ifdef DEBUG_DERIV
7096 printf("deriv atom: mismatch => forbid\n");
7097#endif
7098 /* TODO wildcards here */
7099 ret = forbiddenExp;
7100 }
7101 return(ret);
7102 case XML_EXP_OR: {
7103 xmlExpNodePtr tmp;
7104
7105#ifdef DEBUG_DERIV
7106 printf("deriv or: => or(derivs)\n");
7107#endif
7108 tmp = xmlExpStringDeriveInt(ctxt, exp->exp_left, str);
7109 if (tmp == NULL) {
7110 return(NULL);
7111 }
7112 ret = xmlExpStringDeriveInt(ctxt, exp->exp_right, str);
7113 if (ret == NULL) {
7114 xmlExpFree(ctxt, tmp);
7115 return(NULL);
7116 }
7117 ret = xmlExpHashGetEntry(ctxt, XML_EXP_OR, tmp, ret,
7118 NULL, 0, 0);
7119 return(ret);
7120 }
7121 case XML_EXP_SEQ:
7122#ifdef DEBUG_DERIV
7123 printf("deriv seq: starting with left\n");
7124#endif
7125 ret = xmlExpStringDeriveInt(ctxt, exp->exp_left, str);
7126 if (ret == NULL) {
7127 return(NULL);
7128 } else if (ret == forbiddenExp) {
7129 if (IS_NILLABLE(exp->exp_left)) {
7130#ifdef DEBUG_DERIV
7131 printf("deriv seq: left failed but nillable\n");
7132#endif
7133 ret = xmlExpStringDeriveInt(ctxt, exp->exp_right, str);
7134 }
7135 } else {
7136#ifdef DEBUG_DERIV
7137 printf("deriv seq: left match => sequence\n");
7138#endif
7139 exp->exp_right->ref++;
7140 ret = xmlExpHashGetEntry(ctxt, XML_EXP_SEQ, ret, exp->exp_right,
7141 NULL, 0, 0);
7142 }
7143 return(ret);
7144 case XML_EXP_COUNT: {
7145 int min, max;
7146 xmlExpNodePtr tmp;
7147
7148 if (exp->exp_max == 0)
7149 return(forbiddenExp);
7150 ret = xmlExpStringDeriveInt(ctxt, exp->exp_left, str);
7151 if (ret == NULL)
7152 return(NULL);
7153 if (ret == forbiddenExp) {
7154#ifdef DEBUG_DERIV
7155 printf("deriv count: pattern mismatch => forbid\n");
7156#endif
7157 return(ret);
7158 }
7159 if (exp->exp_max == 1)
7160 return(ret);
7161 if (exp->exp_max < 0) /* unbounded */
7162 max = -1;
7163 else
7164 max = exp->exp_max - 1;
7165 if (exp->exp_min > 0)
7166 min = exp->exp_min - 1;
7167 else
7168 min = 0;
7169 exp->exp_left->ref++;
7170 tmp = xmlExpHashGetEntry(ctxt, XML_EXP_COUNT, exp->exp_left, NULL,
7171 NULL, min, max);
7172 if (ret == emptyExp) {
7173#ifdef DEBUG_DERIV
7174 printf("deriv count: match to empty => new count\n");
7175#endif
7176 return(tmp);
7177 }
7178#ifdef DEBUG_DERIV
7179 printf("deriv count: match => sequence with new count\n");
7180#endif
7181 return(xmlExpHashGetEntry(ctxt, XML_EXP_SEQ, ret, tmp,
7182 NULL, 0, 0));
7183 }
7184 }
7185 return(NULL);
7186}
7187
7188/**
7189 * xmlExpStringDerive:
7190 * @ctxt: the expression context
7191 * @exp: the expression
7192 * @str: the string
7193 * @len: the string len in bytes if available
7194 *
7195 * Do one step of Brzozowski derivation of the expression @exp with
7196 * respect to the input string
7197 *
7198 * Returns the resulting expression or NULL in case of internal error
7199 */
7200xmlExpNodePtr
7201xmlExpStringDerive(xmlExpCtxtPtr ctxt, xmlExpNodePtr exp,
7202 const xmlChar *str, int len) {
7203 const xmlChar *input;
7204
7205 if ((exp == NULL) || (ctxt == NULL) || (str == NULL)) {
7206 return(NULL);
7207 }
7208 /*
7209 * check the string is in the dictionnary, if yes use an interned
7210 * copy, otherwise we know it's not an acceptable input
7211 */
7212 input = xmlDictExists(ctxt->dict, str, len);
7213 if (input == NULL) {
7214 return(forbiddenExp);
7215 }
7216 return(xmlExpStringDeriveInt(ctxt, exp, input));
7217}
7218
7219static int
7220xmlExpCheckCard(xmlExpNodePtr exp, xmlExpNodePtr sub) {
7221 int ret = 1;
7222
7223 if (sub->c_max == -1) {
7224 if (exp->c_max != -1)
7225 ret = 0;
7226 } else if ((exp->c_max >= 0) && (exp->c_max < sub->c_max)) {
7227 ret = 0;
7228 }
7229#if 0
7230 if ((IS_NILLABLE(sub)) && (!IS_NILLABLE(exp)))
7231 ret = 0;
7232#endif
7233 return(ret);
7234}
7235
7236static xmlExpNodePtr xmlExpExpDeriveInt(xmlExpCtxtPtr ctxt, xmlExpNodePtr exp,
7237 xmlExpNodePtr sub);
7238/**
7239 * xmlExpDivide:
7240 * @ctxt: the expressions context
7241 * @exp: the englobing expression
7242 * @sub: the subexpression
7243 * @mult: the multiple expression
7244 * @remain: the remain from the derivation of the multiple
7245 *
7246 * Check if exp is a multiple of sub, i.e. if there is a finite number n
7247 * so that sub{n} subsume exp
7248 *
7249 * Returns the multiple value if successful, 0 if it is not a multiple
7250 * and -1 in case of internel error.
7251 */
7252
7253static int
7254xmlExpDivide(xmlExpCtxtPtr ctxt, xmlExpNodePtr exp, xmlExpNodePtr sub,
7255 xmlExpNodePtr *mult, xmlExpNodePtr *remain) {
7256 int i;
7257 xmlExpNodePtr tmp, tmp2;
7258
7259 if (mult != NULL) *mult = NULL;
7260 if (remain != NULL) *remain = NULL;
7261 if (exp->c_max == -1) return(0);
7262 if (IS_NILLABLE(exp) && (!IS_NILLABLE(sub))) return(0);
7263
7264 for (i = 1;i <= exp->c_max;i++) {
7265 sub->ref++;
7266 tmp = xmlExpHashGetEntry(ctxt, XML_EXP_COUNT,
7267 sub, NULL, NULL, i, i);
7268 if (tmp == NULL) {
7269 return(-1);
7270 }
7271 if (!xmlExpCheckCard(tmp, exp)) {
7272 xmlExpFree(ctxt, tmp);
7273 continue;
7274 }
7275 tmp2 = xmlExpExpDeriveInt(ctxt, tmp, exp);
7276 if (tmp2 == NULL) {
7277 xmlExpFree(ctxt, tmp);
7278 return(-1);
7279 }
7280 if ((tmp2 != forbiddenExp) && (IS_NILLABLE(tmp2))) {
7281 if (remain != NULL)
7282 *remain = tmp2;
7283 else
7284 xmlExpFree(ctxt, tmp2);
7285 if (mult != NULL)
7286 *mult = tmp;
7287 else
7288 xmlExpFree(ctxt, tmp);
7289#ifdef DEBUG_DERIV
7290 printf("Divide succeeded %d\n", i);
7291#endif
7292 return(i);
7293 }
7294 xmlExpFree(ctxt, tmp);
7295 xmlExpFree(ctxt, tmp2);
7296 }
7297#ifdef DEBUG_DERIV
7298 printf("Divide failed\n");
7299#endif
7300 return(0);
7301}
7302
7303/**
7304 * xmlExpExpDeriveInt:
7305 * @ctxt: the expressions context
7306 * @exp: the englobing expression
7307 * @sub: the subexpression
7308 *
7309 * Try to do a step of Brzozowski derivation but at a higher level
7310 * the input being a subexpression.
7311 *
7312 * Returns the resulting expression or NULL in case of internal error
7313 */
7314static xmlExpNodePtr
7315xmlExpExpDeriveInt(xmlExpCtxtPtr ctxt, xmlExpNodePtr exp, xmlExpNodePtr sub) {
7316 xmlExpNodePtr ret, tmp, tmp2, tmp3;
7317 const xmlChar **tab;
7318 int len, i;
7319
7320 /*
7321 * In case of equality and if the expression can only consume a finite
7322 * amount, then the derivation is empty
7323 */
7324 if ((exp == sub) && (exp->c_max >= 0)) {
7325#ifdef DEBUG_DERIV
7326 printf("Equal(exp, sub) and finite -> Empty\n");
7327#endif
7328 return(emptyExp);
7329 }
7330 /*
7331 * decompose sub sequence first
7332 */
7333 if (sub->type == XML_EXP_EMPTY) {
7334#ifdef DEBUG_DERIV
7335 printf("Empty(sub) -> Empty\n");
7336#endif
7337 exp->ref++;
7338 return(exp);
7339 }
7340 if (sub->type == XML_EXP_SEQ) {
7341#ifdef DEBUG_DERIV
7342 printf("Seq(sub) -> decompose\n");
7343#endif
7344 tmp = xmlExpExpDeriveInt(ctxt, exp, sub->exp_left);
7345 if (tmp == NULL)
7346 return(NULL);
7347 if (tmp == forbiddenExp)
7348 return(tmp);
7349 ret = xmlExpExpDeriveInt(ctxt, tmp, sub->exp_right);
7350 xmlExpFree(ctxt, tmp);
7351 return(ret);
7352 }
7353 if (sub->type == XML_EXP_OR) {
7354#ifdef DEBUG_DERIV
7355 printf("Or(sub) -> decompose\n");
7356#endif
7357 tmp = xmlExpExpDeriveInt(ctxt, exp, sub->exp_left);
7358 if (tmp == forbiddenExp)
7359 return(tmp);
7360 if (tmp == NULL)
7361 return(NULL);
7362 ret = xmlExpExpDeriveInt(ctxt, exp, sub->exp_right);
7363 if ((ret == NULL) || (ret == forbiddenExp)) {
7364 xmlExpFree(ctxt, tmp);
7365 return(ret);
7366 }
7367 return(xmlExpHashGetEntry(ctxt, XML_EXP_OR, tmp, ret, NULL, 0, 0));
7368 }
7369 if (!xmlExpCheckCard(exp, sub)) {
7370#ifdef DEBUG_DERIV
7371 printf("CheckCard(exp, sub) failed -> Forbid\n");
7372#endif
7373 return(forbiddenExp);
7374 }
7375 switch (exp->type) {
7376 case XML_EXP_EMPTY:
7377 if (sub == emptyExp)
7378 return(emptyExp);
7379#ifdef DEBUG_DERIV
7380 printf("Empty(exp) -> Forbid\n");
7381#endif
7382 return(forbiddenExp);
7383 case XML_EXP_FORBID:
7384#ifdef DEBUG_DERIV
7385 printf("Forbid(exp) -> Forbid\n");
7386#endif
7387 return(forbiddenExp);
7388 case XML_EXP_ATOM:
7389 if (sub->type == XML_EXP_ATOM) {
7390 /* TODO: handle wildcards */
7391 if (exp->exp_str == sub->exp_str) {
7392#ifdef DEBUG_DERIV
7393 printf("Atom match -> Empty\n");
7394#endif
7395 return(emptyExp);
7396 }
7397#ifdef DEBUG_DERIV
7398 printf("Atom mismatch -> Forbid\n");
7399#endif
7400 return(forbiddenExp);
7401 }
7402 if ((sub->type == XML_EXP_COUNT) &&
7403 (sub->exp_max == 1) &&
7404 (sub->exp_left->type == XML_EXP_ATOM)) {
7405 /* TODO: handle wildcards */
7406 if (exp->exp_str == sub->exp_left->exp_str) {
7407#ifdef DEBUG_DERIV
7408 printf("Atom match -> Empty\n");
7409#endif
7410 return(emptyExp);
7411 }
7412#ifdef DEBUG_DERIV
7413 printf("Atom mismatch -> Forbid\n");
7414#endif
7415 return(forbiddenExp);
7416 }
7417#ifdef DEBUG_DERIV
7418 printf("Compex exp vs Atom -> Forbid\n");
7419#endif
7420 return(forbiddenExp);
7421 case XML_EXP_SEQ:
7422 /* try to get the sequence consumed only if possible */
7423 if (xmlExpCheckCard(exp->exp_left, sub)) {
7424 /* See if the sequence can be consumed directly */
7425#ifdef DEBUG_DERIV
7426 printf("Seq trying left only\n");
7427#endif
7428 ret = xmlExpExpDeriveInt(ctxt, exp->exp_left, sub);
7429 if ((ret != forbiddenExp) && (ret != NULL)) {
7430#ifdef DEBUG_DERIV
7431 printf("Seq trying left only worked\n");
7432#endif
7433 /*
7434 * TODO: assumption here that we are determinist
7435 * i.e. we won't get to a nillable exp left
7436 * subset which could be matched by the right
7437 * part too.
7438 * e.g.: (a | b)+,(a | c) and 'a+,a'
7439 */
7440 exp->exp_right->ref++;
7441 return(xmlExpHashGetEntry(ctxt, XML_EXP_SEQ, ret,
7442 exp->exp_right, NULL, 0, 0));
7443 }
7444#ifdef DEBUG_DERIV
7445 } else {
7446 printf("Seq: left too short\n");
7447#endif
7448 }
7449 /* Try instead to decompose */
7450 if (sub->type == XML_EXP_COUNT) {
7451 int min, max;
7452
7453#ifdef DEBUG_DERIV
7454 printf("Seq: sub is a count\n");
7455#endif
7456 ret = xmlExpExpDeriveInt(ctxt, exp->exp_left, sub->exp_left);
7457 if (ret == NULL)
7458 return(NULL);
7459 if (ret != forbiddenExp) {
7460#ifdef DEBUG_DERIV
7461 printf("Seq , Count match on left\n");
7462#endif
7463 if (sub->exp_max < 0)
7464 max = -1;
7465 else
7466 max = sub->exp_max -1;
7467 if (sub->exp_min > 0)
7468 min = sub->exp_min -1;
7469 else
7470 min = 0;
7471 exp->exp_right->ref++;
7472 tmp = xmlExpHashGetEntry(ctxt, XML_EXP_SEQ, ret,
7473 exp->exp_right, NULL, 0, 0);
7474 if (tmp == NULL)
7475 return(NULL);
7476
7477 sub->exp_left->ref++;
7478 tmp2 = xmlExpHashGetEntry(ctxt, XML_EXP_COUNT,
7479 sub->exp_left, NULL, NULL, min, max);
7480 if (tmp2 == NULL) {
7481 xmlExpFree(ctxt, tmp);
7482 return(NULL);
7483 }
7484 ret = xmlExpExpDeriveInt(ctxt, tmp, tmp2);
7485 xmlExpFree(ctxt, tmp);
7486 xmlExpFree(ctxt, tmp2);
7487 return(ret);
7488 }
7489 }
7490 /* we made no progress on structured operations */
7491 break;
7492 case XML_EXP_OR:
7493#ifdef DEBUG_DERIV
7494 printf("Or , trying both side\n");
7495#endif
7496 ret = xmlExpExpDeriveInt(ctxt, exp->exp_left, sub);
7497 if (ret == NULL)
7498 return(NULL);
7499 tmp = xmlExpExpDeriveInt(ctxt, exp->exp_right, sub);
7500 if (tmp == NULL) {
7501 xmlExpFree(ctxt, ret);
7502 return(NULL);
7503 }
7504 return(xmlExpHashGetEntry(ctxt, XML_EXP_OR, ret, tmp, NULL, 0, 0));
7505 case XML_EXP_COUNT: {
7506 int min, max;
7507
7508 if (sub->type == XML_EXP_COUNT) {
7509 /*
7510 * Try to see if the loop is completely subsumed
7511 */
7512 tmp = xmlExpExpDeriveInt(ctxt, exp->exp_left, sub->exp_left);
7513 if (tmp == NULL)
7514 return(NULL);
7515 if (tmp == forbiddenExp) {
7516 int mult;
7517
7518#ifdef DEBUG_DERIV
7519 printf("Count, Count inner don't subsume\n");
7520#endif
7521 mult = xmlExpDivide(ctxt, sub->exp_left, exp->exp_left,
7522 NULL, &tmp);
7523 if (mult <= 0) {
7524#ifdef DEBUG_DERIV
7525 printf("Count, Count not multiple => forbidden\n");
7526#endif
7527 return(forbiddenExp);
7528 }
7529 if (sub->exp_max == -1) {
7530 max = -1;
7531 if (exp->exp_max == -1) {
7532 if (exp->exp_min <= sub->exp_min * mult)
7533 min = 0;
7534 else
7535 min = exp->exp_min - sub->exp_min * mult;
7536 } else {
7537#ifdef DEBUG_DERIV
7538 printf("Count, Count finite can't subsume infinite\n");
7539#endif
7540 xmlExpFree(ctxt, tmp);
7541 return(forbiddenExp);
7542 }
7543 } else {
7544 if (exp->exp_max == -1) {
7545#ifdef DEBUG_DERIV
7546 printf("Infinite loop consume mult finite loop\n");
7547#endif
7548 if (exp->exp_min > sub->exp_min * mult) {
7549 max = -1;
7550 min = exp->exp_min - sub->exp_min * mult;
7551 } else {
7552 max = -1;
7553 min = 0;
7554 }
7555 } else {
7556 if (exp->exp_max < sub->exp_max * mult) {
7557#ifdef DEBUG_DERIV
7558 printf("loops max mult mismatch => forbidden\n");
7559#endif
7560 xmlExpFree(ctxt, tmp);
7561 return(forbiddenExp);
7562 }
7563 if (sub->exp_max * mult > exp->exp_min)
7564 min = 0;
7565 else
7566 min = exp->exp_min - sub->exp_max * mult;
7567 max = exp->exp_max - sub->exp_max * mult;
7568 }
7569 }
7570 } else if (!IS_NILLABLE(tmp)) {
7571 /*
7572 * TODO: loop here to try to grow if working on finite
7573 * blocks.
7574 */
7575#ifdef DEBUG_DERIV
7576 printf("Count, Count remain not nillable => forbidden\n");
7577#endif
7578 xmlExpFree(ctxt, tmp);
7579 return(forbiddenExp);
7580 } else if (sub->exp_max == -1) {
7581 if (exp->exp_max == -1) {
7582 if (exp->exp_min <= sub->exp_min) {
7583#ifdef DEBUG_DERIV
7584 printf("Infinite loops Okay => COUNT(0,Inf)\n");
7585#endif
7586 max = -1;
7587 min = 0;
7588 } else {
7589#ifdef DEBUG_DERIV
7590 printf("Infinite loops min => Count(X,Inf)\n");
7591#endif
7592 max = -1;
7593 min = exp->exp_min - sub->exp_min;
7594 }
7595 } else if (exp->exp_min > sub->exp_min) {
7596#ifdef DEBUG_DERIV
7597 printf("loops min mismatch 1 => forbidden ???\n");
7598#endif
7599 xmlExpFree(ctxt, tmp);
7600 return(forbiddenExp);
7601 } else {
7602 max = -1;
7603 min = 0;
7604 }
7605 } else {
7606 if (exp->exp_max == -1) {
7607#ifdef DEBUG_DERIV
7608 printf("Infinite loop consume finite loop\n");
7609#endif
7610 if (exp->exp_min > sub->exp_min) {
7611 max = -1;
7612 min = exp->exp_min - sub->exp_min;
7613 } else {
7614 max = -1;
7615 min = 0;
7616 }
7617 } else {
7618 if (exp->exp_max < sub->exp_max) {
7619#ifdef DEBUG_DERIV
7620 printf("loops max mismatch => forbidden\n");
7621#endif
7622 xmlExpFree(ctxt, tmp);
7623 return(forbiddenExp);
7624 }
7625 if (sub->exp_max > exp->exp_min)
7626 min = 0;
7627 else
7628 min = exp->exp_min - sub->exp_max;
7629 max = exp->exp_max - sub->exp_max;
7630 }
7631 }
7632#ifdef DEBUG_DERIV
7633 printf("loops match => SEQ(COUNT())\n");
7634#endif
7635 exp->exp_left->ref++;
7636 tmp2 = xmlExpHashGetEntry(ctxt, XML_EXP_COUNT, exp->exp_left,
7637 NULL, NULL, min, max);
7638 if (tmp2 == NULL) {
7639 return(NULL);
7640 }
7641 ret = xmlExpHashGetEntry(ctxt, XML_EXP_SEQ, tmp, tmp2,
7642 NULL, 0, 0);
7643 return(ret);
7644 }
7645 tmp = xmlExpExpDeriveInt(ctxt, exp->exp_left, sub);
7646 if (tmp == NULL)
7647 return(NULL);
7648 if (tmp == forbiddenExp) {
7649#ifdef DEBUG_DERIV
7650 printf("loop mismatch => forbidden\n");
7651#endif
7652 return(forbiddenExp);
7653 }
7654 if (exp->exp_min > 0)
7655 min = exp->exp_min - 1;
7656 else
7657 min = 0;
7658 if (exp->exp_max < 0)
7659 max = -1;
7660 else
7661 max = exp->exp_max - 1;
7662
7663#ifdef DEBUG_DERIV
7664 printf("loop match => SEQ(COUNT())\n");
7665#endif
7666 exp->exp_left->ref++;
7667 tmp2 = xmlExpHashGetEntry(ctxt, XML_EXP_COUNT, exp->exp_left,
7668 NULL, NULL, min, max);
7669 if (tmp2 == NULL)
7670 return(NULL);
7671 ret = xmlExpHashGetEntry(ctxt, XML_EXP_SEQ, tmp, tmp2,
7672 NULL, 0, 0);
7673 return(ret);
7674 }
7675 }
7676
Daniel Veillardccb4d412005-08-23 13:41:17 +00007677#ifdef DEBUG_DERIV
7678 printf("Fallback to derivative\n");
7679#endif
7680 if (IS_NILLABLE(sub)) {
7681 if (!(IS_NILLABLE(exp)))
7682 return(forbiddenExp);
7683 else
7684 ret = emptyExp;
7685 } else
7686 ret = NULL;
Daniel Veillard81a8ec62005-08-22 00:20:58 +00007687 /*
7688 * here the structured derivation made no progress so
7689 * we use the default token based derivation to force one more step
7690 */
7691 if (ctxt->tabSize == 0)
7692 ctxt->tabSize = 40;
7693
7694 tab = (const xmlChar **) xmlMalloc(ctxt->tabSize *
7695 sizeof(const xmlChar *));
7696 if (tab == NULL) {
7697 return(NULL);
7698 }
7699
Daniel Veillard81a8ec62005-08-22 00:20:58 +00007700 /*
7701 * collect all the strings accepted by the subexpression on input
7702 */
7703 len = xmlExpGetStartInt(ctxt, sub, tab, ctxt->tabSize, 0);
7704 while (len < 0) {
7705 const xmlChar **temp;
Rob Richards54a8f672005-10-07 02:33:00 +00007706 temp = (const xmlChar **) xmlRealloc((xmlChar **) tab, ctxt->tabSize * 2 *
Daniel Veillard81a8ec62005-08-22 00:20:58 +00007707 sizeof(const xmlChar *));
7708 if (temp == NULL) {
Rob Richards54a8f672005-10-07 02:33:00 +00007709 xmlFree((xmlChar **) tab);
Daniel Veillard81a8ec62005-08-22 00:20:58 +00007710 return(NULL);
7711 }
7712 tab = temp;
7713 ctxt->tabSize *= 2;
7714 len = xmlExpGetStartInt(ctxt, sub, tab, ctxt->tabSize, 0);
7715 }
Daniel Veillard81a8ec62005-08-22 00:20:58 +00007716 for (i = 0;i < len;i++) {
7717 tmp = xmlExpStringDeriveInt(ctxt, exp, tab[i]);
7718 if ((tmp == NULL) || (tmp == forbiddenExp)) {
7719 xmlExpFree(ctxt, ret);
Rob Richards54a8f672005-10-07 02:33:00 +00007720 xmlFree((xmlChar **) tab);
Daniel Veillard81a8ec62005-08-22 00:20:58 +00007721 return(tmp);
7722 }
7723 tmp2 = xmlExpStringDeriveInt(ctxt, sub, tab[i]);
7724 if ((tmp2 == NULL) || (tmp2 == forbiddenExp)) {
7725 xmlExpFree(ctxt, tmp);
7726 xmlExpFree(ctxt, ret);
Rob Richards54a8f672005-10-07 02:33:00 +00007727 xmlFree((xmlChar **) tab);
Daniel Veillard81a8ec62005-08-22 00:20:58 +00007728 return(tmp);
7729 }
7730 tmp3 = xmlExpExpDeriveInt(ctxt, tmp, tmp2);
7731 xmlExpFree(ctxt, tmp);
7732 xmlExpFree(ctxt, tmp2);
7733
7734 if ((tmp3 == NULL) || (tmp3 == forbiddenExp)) {
7735 xmlExpFree(ctxt, ret);
Rob Richards54a8f672005-10-07 02:33:00 +00007736 xmlFree((xmlChar **) tab);
Daniel Veillard81a8ec62005-08-22 00:20:58 +00007737 return(tmp3);
7738 }
7739
7740 if (ret == NULL)
7741 ret = tmp3;
7742 else {
7743 ret = xmlExpHashGetEntry(ctxt, XML_EXP_OR, ret, tmp3, NULL, 0, 0);
7744 if (ret == NULL) {
Rob Richards54a8f672005-10-07 02:33:00 +00007745 xmlFree((xmlChar **) tab);
Daniel Veillard81a8ec62005-08-22 00:20:58 +00007746 return(NULL);
7747 }
7748 }
7749 }
Rob Richards54a8f672005-10-07 02:33:00 +00007750 xmlFree((xmlChar **) tab);
Daniel Veillard81a8ec62005-08-22 00:20:58 +00007751 return(ret);
7752}
Daniel Veillardf8e3db02012-09-11 13:26:36 +08007753
Daniel Veillard81a8ec62005-08-22 00:20:58 +00007754/**
Daniel Veillard0090bd52005-08-22 14:43:43 +00007755 * xmlExpExpDerive:
7756 * @ctxt: the expressions context
7757 * @exp: the englobing expression
7758 * @sub: the subexpression
7759 *
7760 * Evaluates the expression resulting from @exp consuming a sub expression @sub
7761 * Based on algebraic derivation and sometimes direct Brzozowski derivation
7762 * it usually tatkes less than linear time and can handle expressions generating
7763 * infinite languages.
7764 *
7765 * Returns the resulting expression or NULL in case of internal error, the
7766 * result must be freed
7767 */
7768xmlExpNodePtr
7769xmlExpExpDerive(xmlExpCtxtPtr ctxt, xmlExpNodePtr exp, xmlExpNodePtr sub) {
7770 if ((exp == NULL) || (ctxt == NULL) || (sub == NULL))
7771 return(NULL);
7772
7773 /*
7774 * O(1) speedups
7775 */
7776 if (IS_NILLABLE(sub) && (!IS_NILLABLE(exp))) {
7777#ifdef DEBUG_DERIV
7778 printf("Sub nillable and not exp : can't subsume\n");
7779#endif
7780 return(forbiddenExp);
7781 }
7782 if (xmlExpCheckCard(exp, sub) == 0) {
7783#ifdef DEBUG_DERIV
7784 printf("sub generate longuer sequances than exp : can't subsume\n");
7785#endif
7786 return(forbiddenExp);
7787 }
7788 return(xmlExpExpDeriveInt(ctxt, exp, sub));
7789}
7790
7791/**
Daniel Veillard81a8ec62005-08-22 00:20:58 +00007792 * xmlExpSubsume:
7793 * @ctxt: the expressions context
7794 * @exp: the englobing expression
7795 * @sub: the subexpression
7796 *
7797 * Check whether @exp accepts all the languages accexpted by @sub
7798 * the input being a subexpression.
7799 *
7800 * Returns 1 if true 0 if false and -1 in case of failure.
7801 */
7802int
7803xmlExpSubsume(xmlExpCtxtPtr ctxt, xmlExpNodePtr exp, xmlExpNodePtr sub) {
7804 xmlExpNodePtr tmp;
Daniel Veillardf8e3db02012-09-11 13:26:36 +08007805
Daniel Veillard81a8ec62005-08-22 00:20:58 +00007806 if ((exp == NULL) || (ctxt == NULL) || (sub == NULL))
7807 return(-1);
7808
7809 /*
7810 * TODO: speedup by checking the language of sub is a subset of the
7811 * language of exp
7812 */
7813 /*
7814 * O(1) speedups
7815 */
7816 if (IS_NILLABLE(sub) && (!IS_NILLABLE(exp))) {
7817#ifdef DEBUG_DERIV
7818 printf("Sub nillable and not exp : can't subsume\n");
7819#endif
7820 return(0);
7821 }
7822 if (xmlExpCheckCard(exp, sub) == 0) {
7823#ifdef DEBUG_DERIV
7824 printf("sub generate longuer sequances than exp : can't subsume\n");
7825#endif
7826 return(0);
7827 }
7828 tmp = xmlExpExpDeriveInt(ctxt, exp, sub);
7829#ifdef DEBUG_DERIV
7830 printf("Result derivation :\n");
7831 PRINT_EXP(tmp);
7832#endif
7833 if (tmp == NULL)
7834 return(-1);
7835 if (tmp == forbiddenExp)
7836 return(0);
7837 if (tmp == emptyExp)
7838 return(1);
7839 if ((tmp != NULL) && (IS_NILLABLE(tmp))) {
7840 xmlExpFree(ctxt, tmp);
7841 return(1);
7842 }
7843 xmlExpFree(ctxt, tmp);
7844 return(0);
7845}
Daniel Veillard465a0002005-08-22 12:07:04 +00007846
7847/************************************************************************
7848 * *
Daniel Veillardf8e3db02012-09-11 13:26:36 +08007849 * Parsing expression *
Daniel Veillard465a0002005-08-22 12:07:04 +00007850 * *
7851 ************************************************************************/
7852
7853static xmlExpNodePtr xmlExpParseExpr(xmlExpCtxtPtr ctxt);
7854
7855#undef CUR
7856#define CUR (*ctxt->cur)
7857#undef NEXT
7858#define NEXT ctxt->cur++;
7859#undef IS_BLANK
7860#define IS_BLANK(c) ((c == ' ') || (c == '\n') || (c == '\r') || (c == '\t'))
7861#define SKIP_BLANKS while (IS_BLANK(*ctxt->cur)) ctxt->cur++;
7862
7863static int
7864xmlExpParseNumber(xmlExpCtxtPtr ctxt) {
7865 int ret = 0;
7866
7867 SKIP_BLANKS
7868 if (CUR == '*') {
7869 NEXT
7870 return(-1);
7871 }
7872 if ((CUR < '0') || (CUR > '9'))
7873 return(-1);
7874 while ((CUR >= '0') && (CUR <= '9')) {
7875 ret = ret * 10 + (CUR - '0');
7876 NEXT
7877 }
7878 return(ret);
7879}
7880
7881static xmlExpNodePtr
7882xmlExpParseOr(xmlExpCtxtPtr ctxt) {
7883 const char *base;
7884 xmlExpNodePtr ret;
7885 const xmlChar *val;
7886
7887 SKIP_BLANKS
7888 base = ctxt->cur;
7889 if (*ctxt->cur == '(') {
7890 NEXT
7891 ret = xmlExpParseExpr(ctxt);
7892 SKIP_BLANKS
7893 if (*ctxt->cur != ')') {
7894 fprintf(stderr, "unbalanced '(' : %s\n", base);
7895 xmlExpFree(ctxt, ret);
7896 return(NULL);
7897 }
7898 NEXT;
7899 SKIP_BLANKS
7900 goto parse_quantifier;
7901 }
7902 while ((CUR != 0) && (!(IS_BLANK(CUR))) && (CUR != '(') &&
7903 (CUR != ')') && (CUR != '|') && (CUR != ',') && (CUR != '{') &&
7904 (CUR != '*') && (CUR != '+') && (CUR != '?') && (CUR != '}'))
7905 NEXT;
7906 val = xmlDictLookup(ctxt->dict, BAD_CAST base, ctxt->cur - base);
7907 if (val == NULL)
7908 return(NULL);
7909 ret = xmlExpHashGetEntry(ctxt, XML_EXP_ATOM, NULL, NULL, val, 0, 0);
7910 if (ret == NULL)
7911 return(NULL);
7912 SKIP_BLANKS
7913parse_quantifier:
7914 if (CUR == '{') {
7915 int min, max;
7916
7917 NEXT
7918 min = xmlExpParseNumber(ctxt);
7919 if (min < 0) {
7920 xmlExpFree(ctxt, ret);
7921 return(NULL);
7922 }
7923 SKIP_BLANKS
7924 if (CUR == ',') {
7925 NEXT
7926 max = xmlExpParseNumber(ctxt);
7927 SKIP_BLANKS
7928 } else
7929 max = min;
7930 if (CUR != '}') {
7931 xmlExpFree(ctxt, ret);
7932 return(NULL);
7933 }
7934 NEXT
7935 ret = xmlExpHashGetEntry(ctxt, XML_EXP_COUNT, ret, NULL, NULL,
7936 min, max);
7937 SKIP_BLANKS
7938 } else if (CUR == '?') {
7939 NEXT
7940 ret = xmlExpHashGetEntry(ctxt, XML_EXP_COUNT, ret, NULL, NULL,
7941 0, 1);
7942 SKIP_BLANKS
7943 } else if (CUR == '+') {
7944 NEXT
7945 ret = xmlExpHashGetEntry(ctxt, XML_EXP_COUNT, ret, NULL, NULL,
7946 1, -1);
7947 SKIP_BLANKS
7948 } else if (CUR == '*') {
7949 NEXT
7950 ret = xmlExpHashGetEntry(ctxt, XML_EXP_COUNT, ret, NULL, NULL,
7951 0, -1);
7952 SKIP_BLANKS
Daniel Veillardf8e3db02012-09-11 13:26:36 +08007953 }
Daniel Veillard465a0002005-08-22 12:07:04 +00007954 return(ret);
7955}
7956
7957
7958static xmlExpNodePtr
7959xmlExpParseSeq(xmlExpCtxtPtr ctxt) {
7960 xmlExpNodePtr ret, right;
7961
7962 ret = xmlExpParseOr(ctxt);
7963 SKIP_BLANKS
7964 while (CUR == '|') {
7965 NEXT
7966 right = xmlExpParseOr(ctxt);
7967 if (right == NULL) {
7968 xmlExpFree(ctxt, ret);
7969 return(NULL);
7970 }
7971 ret = xmlExpHashGetEntry(ctxt, XML_EXP_OR, ret, right, NULL, 0, 0);
7972 if (ret == NULL)
7973 return(NULL);
7974 }
7975 return(ret);
7976}
7977
7978static xmlExpNodePtr
7979xmlExpParseExpr(xmlExpCtxtPtr ctxt) {
7980 xmlExpNodePtr ret, right;
7981
7982 ret = xmlExpParseSeq(ctxt);
7983 SKIP_BLANKS
7984 while (CUR == ',') {
7985 NEXT
7986 right = xmlExpParseSeq(ctxt);
7987 if (right == NULL) {
7988 xmlExpFree(ctxt, ret);
7989 return(NULL);
7990 }
7991 ret = xmlExpHashGetEntry(ctxt, XML_EXP_SEQ, ret, right, NULL, 0, 0);
7992 if (ret == NULL)
7993 return(NULL);
7994 }
7995 return(ret);
7996}
7997
7998/**
7999 * xmlExpParse:
8000 * @ctxt: the expressions context
8001 * @expr: the 0 terminated string
8002 *
8003 * Minimal parser for regexps, it understand the following constructs
8004 * - string terminals
8005 * - choice operator |
8006 * - sequence operator ,
8007 * - subexpressions (...)
8008 * - usual cardinality operators + * and ?
8009 * - finite sequences { min, max }
8010 * - infinite sequences { min, * }
8011 * There is minimal checkings made especially no checking on strings values
8012 *
8013 * Returns a new expression or NULL in case of failure
8014 */
8015xmlExpNodePtr
8016xmlExpParse(xmlExpCtxtPtr ctxt, const char *expr) {
8017 xmlExpNodePtr ret;
8018
8019 ctxt->expr = expr;
8020 ctxt->cur = expr;
8021
8022 ret = xmlExpParseExpr(ctxt);
8023 SKIP_BLANKS
8024 if (*ctxt->cur != 0) {
8025 xmlExpFree(ctxt, ret);
8026 return(NULL);
8027 }
8028 return(ret);
8029}
8030
8031static void
8032xmlExpDumpInt(xmlBufferPtr buf, xmlExpNodePtr expr, int glob) {
8033 xmlExpNodePtr c;
8034
8035 if (expr == NULL) return;
8036 if (glob) xmlBufferWriteChar(buf, "(");
8037 switch (expr->type) {
8038 case XML_EXP_EMPTY:
8039 xmlBufferWriteChar(buf, "empty");
8040 break;
8041 case XML_EXP_FORBID:
8042 xmlBufferWriteChar(buf, "forbidden");
8043 break;
8044 case XML_EXP_ATOM:
8045 xmlBufferWriteCHAR(buf, expr->exp_str);
8046 break;
8047 case XML_EXP_SEQ:
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 xmlBufferWriteChar(buf, " , ");
8054 c = expr->exp_right;
8055 if ((c->type == XML_EXP_SEQ) || (c->type == XML_EXP_OR))
8056 xmlExpDumpInt(buf, c, 1);
8057 else
8058 xmlExpDumpInt(buf, c, 0);
8059 break;
8060 case XML_EXP_OR:
8061 c = expr->exp_left;
8062 if ((c->type == XML_EXP_SEQ) || (c->type == XML_EXP_OR))
8063 xmlExpDumpInt(buf, c, 1);
8064 else
8065 xmlExpDumpInt(buf, c, 0);
8066 xmlBufferWriteChar(buf, " | ");
8067 c = expr->exp_right;
8068 if ((c->type == XML_EXP_SEQ) || (c->type == XML_EXP_OR))
8069 xmlExpDumpInt(buf, c, 1);
8070 else
8071 xmlExpDumpInt(buf, c, 0);
8072 break;
8073 case XML_EXP_COUNT: {
8074 char rep[40];
Daniel Veillardf8e3db02012-09-11 13:26:36 +08008075
Daniel Veillard465a0002005-08-22 12:07:04 +00008076 c = expr->exp_left;
8077 if ((c->type == XML_EXP_SEQ) || (c->type == XML_EXP_OR))
8078 xmlExpDumpInt(buf, c, 1);
8079 else
8080 xmlExpDumpInt(buf, c, 0);
8081 if ((expr->exp_min == 0) && (expr->exp_max == 1)) {
8082 rep[0] = '?';
8083 rep[1] = 0;
8084 } else if ((expr->exp_min == 0) && (expr->exp_max == -1)) {
8085 rep[0] = '*';
8086 rep[1] = 0;
8087 } else if ((expr->exp_min == 1) && (expr->exp_max == -1)) {
8088 rep[0] = '+';
8089 rep[1] = 0;
8090 } else if (expr->exp_max == expr->exp_min) {
8091 snprintf(rep, 39, "{%d}", expr->exp_min);
8092 } else if (expr->exp_max < 0) {
8093 snprintf(rep, 39, "{%d,inf}", expr->exp_min);
8094 } else {
8095 snprintf(rep, 39, "{%d,%d}", expr->exp_min, expr->exp_max);
8096 }
8097 rep[39] = 0;
8098 xmlBufferWriteChar(buf, rep);
8099 break;
8100 }
8101 default:
8102 fprintf(stderr, "Error in tree\n");
8103 }
8104 if (glob)
8105 xmlBufferWriteChar(buf, ")");
8106}
8107/**
8108 * xmlExpDump:
8109 * @buf: a buffer to receive the output
8110 * @expr: the compiled expression
8111 *
8112 * Serialize the expression as compiled to the buffer
8113 */
8114void
Daniel Veillard5eee7672005-08-22 21:22:27 +00008115xmlExpDump(xmlBufferPtr buf, xmlExpNodePtr expr) {
8116 if ((buf == NULL) || (expr == NULL))
Daniel Veillard465a0002005-08-22 12:07:04 +00008117 return;
Daniel Veillard5eee7672005-08-22 21:22:27 +00008118 xmlExpDumpInt(buf, expr, 0);
Daniel Veillard465a0002005-08-22 12:07:04 +00008119}
8120
8121/**
8122 * xmlExpMaxToken:
8123 * @expr: a compiled expression
8124 *
8125 * Indicate the maximum number of input a expression can accept
8126 *
8127 * Returns the maximum length or -1 in case of error
8128 */
8129int
8130xmlExpMaxToken(xmlExpNodePtr expr) {
8131 if (expr == NULL)
8132 return(-1);
8133 return(expr->c_max);
8134}
8135
8136/**
8137 * xmlExpCtxtNbNodes:
8138 * @ctxt: an expression context
8139 *
8140 * Debugging facility provides the number of allocated nodes at a that point
8141 *
8142 * Returns the number of nodes in use or -1 in case of error
8143 */
8144int
8145xmlExpCtxtNbNodes(xmlExpCtxtPtr ctxt) {
8146 if (ctxt == NULL)
8147 return(-1);
8148 return(ctxt->nb_nodes);
8149}
8150
8151/**
8152 * xmlExpCtxtNbCons:
8153 * @ctxt: an expression context
8154 *
8155 * Debugging facility provides the number of allocated nodes over lifetime
8156 *
8157 * Returns the number of nodes ever allocated or -1 in case of error
8158 */
8159int
8160xmlExpCtxtNbCons(xmlExpCtxtPtr ctxt) {
8161 if (ctxt == NULL)
8162 return(-1);
8163 return(ctxt->nb_cons);
8164}
8165
Daniel Veillard81a8ec62005-08-22 00:20:58 +00008166#endif /* LIBXML_EXPR_ENABLED */
Daniel Veillard5d4644e2005-04-01 13:11:58 +00008167#define bottom_xmlregexp
8168#include "elfgcchack.h"
Daniel Veillard4255d502002-04-16 15:50:10 +00008169#endif /* LIBXML_REGEXP_ENABLED */