blob: 7dc6eeaf61be2a4c9103bf91da9d8eea66a76b07 [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;
Daniel Veillard34b35002016-05-09 09:28:38 +08001547 int nullable = 0;
Daniel Veillard10bda622008-03-13 07:27:24 +00001548
Daniel Veillard4255d502002-04-16 15:50:10 +00001549 if (atom == NULL) {
1550 ERROR("genrate transition: atom == NULL");
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001551 return(-1);
Daniel Veillard4255d502002-04-16 15:50:10 +00001552 }
1553 if (atom->type == XML_REGEXP_SUBREG) {
1554 /*
1555 * this is a subexpression handling one should not need to
William M. Brackddf71d62004-05-06 04:17:26 +00001556 * create a new node except for XML_REGEXP_QUANT_RANGE.
Daniel Veillard4255d502002-04-16 15:50:10 +00001557 */
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001558 if (xmlRegAtomPush(ctxt, atom) < 0) {
1559 return(-1);
1560 }
Daniel Veillard4255d502002-04-16 15:50:10 +00001561 if ((to != NULL) && (atom->stop != to) &&
1562 (atom->quant != XML_REGEXP_QUANT_RANGE)) {
1563 /*
1564 * Generate an epsilon transition to link to the target
1565 */
1566 xmlFAGenerateEpsilonTransition(ctxt, atom->stop, to);
Daniel Veillardaa622012005-10-20 15:55:25 +00001567#ifdef DV
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001568 } else if ((to == NULL) && (atom->quant != XML_REGEXP_QUANT_RANGE) &&
Daniel Veillardaa622012005-10-20 15:55:25 +00001569 (atom->quant != XML_REGEXP_QUANT_ONCE)) {
1570 to = xmlRegNewState(ctxt);
1571 xmlRegStatePush(ctxt, to);
1572 ctxt->state = to;
1573 xmlFAGenerateEpsilonTransition(ctxt, atom->stop, to);
1574#endif
Daniel Veillard4255d502002-04-16 15:50:10 +00001575 }
1576 switch (atom->quant) {
1577 case XML_REGEXP_QUANT_OPT:
1578 atom->quant = XML_REGEXP_QUANT_ONCE;
Daniel Veillard54eb0242006-03-21 23:17:57 +00001579 /*
1580 * transition done to the state after end of atom.
1581 * 1. set transition from atom start to new state
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001582 * 2. set transition from atom end to this state.
Daniel Veillard54eb0242006-03-21 23:17:57 +00001583 */
Daniel Veillardd80d0722009-08-22 18:56:01 +02001584 if (to == NULL) {
1585 xmlFAGenerateEpsilonTransition(ctxt, atom->start, 0);
1586 xmlFAGenerateEpsilonTransition(ctxt, atom->stop,
1587 ctxt->state);
1588 } else {
1589 xmlFAGenerateEpsilonTransition(ctxt, atom->start, to);
1590 }
Daniel Veillard4255d502002-04-16 15:50:10 +00001591 break;
1592 case XML_REGEXP_QUANT_MULT:
1593 atom->quant = XML_REGEXP_QUANT_ONCE;
1594 xmlFAGenerateEpsilonTransition(ctxt, atom->start, atom->stop);
1595 xmlFAGenerateEpsilonTransition(ctxt, atom->stop, atom->start);
1596 break;
1597 case XML_REGEXP_QUANT_PLUS:
1598 atom->quant = XML_REGEXP_QUANT_ONCE;
1599 xmlFAGenerateEpsilonTransition(ctxt, atom->stop, atom->start);
1600 break;
1601 case XML_REGEXP_QUANT_RANGE: {
1602 int counter;
Daniel Veillard76d59b62007-08-22 16:29:21 +00001603 xmlRegStatePtr inter, newstate;
Daniel Veillard4255d502002-04-16 15:50:10 +00001604
1605 /*
Daniel Veillard76d59b62007-08-22 16:29:21 +00001606 * create the final state now if needed
Daniel Veillard4255d502002-04-16 15:50:10 +00001607 */
Daniel Veillard4255d502002-04-16 15:50:10 +00001608 if (to != NULL) {
1609 newstate = to;
1610 } else {
1611 newstate = xmlRegNewState(ctxt);
1612 xmlRegStatePush(ctxt, newstate);
Daniel Veillard4255d502002-04-16 15:50:10 +00001613 }
Daniel Veillard76d59b62007-08-22 16:29:21 +00001614
1615 /*
1616 * The principle here is to use counted transition
1617 * to avoid explosion in the number of states in the
1618 * graph. This is clearly more complex but should not
1619 * be exploitable at runtime.
Daniel Veillard54eb0242006-03-21 23:17:57 +00001620 */
Daniel Veillard76d59b62007-08-22 16:29:21 +00001621 if ((atom->min == 0) && (atom->start0 == NULL)) {
1622 xmlRegAtomPtr copy;
1623 /*
1624 * duplicate a transition based on atom to count next
1625 * occurences after 1. We cannot loop to atom->start
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001626 * directly because we need an epsilon transition to
Daniel Veillard76d59b62007-08-22 16:29:21 +00001627 * newstate.
1628 */
1629 /* ???? For some reason it seems we never reach that
1630 case, I suppose this got optimized out before when
1631 building the automata */
Daniel Veillardc821e032007-08-28 17:33:45 +00001632 copy = xmlRegCopyAtom(ctxt, atom);
Daniel Veillard76d59b62007-08-22 16:29:21 +00001633 if (copy == NULL)
1634 return(-1);
Daniel Veillard76d59b62007-08-22 16:29:21 +00001635 copy->quant = XML_REGEXP_QUANT_ONCE;
1636 copy->min = 0;
1637 copy->max = 0;
1638
1639 if (xmlFAGenerateTransitions(ctxt, atom->start, NULL, copy)
1640 < 0)
1641 return(-1);
1642 inter = ctxt->state;
1643 counter = xmlRegGetCounter(ctxt);
1644 ctxt->counters[counter].min = atom->min - 1;
1645 ctxt->counters[counter].max = atom->max - 1;
1646 /* count the number of times we see it again */
1647 xmlFAGenerateCountedEpsilonTransition(ctxt, inter,
1648 atom->stop, counter);
1649 /* allow a way out based on the count */
1650 xmlFAGenerateCountedTransition(ctxt, inter,
1651 newstate, counter);
1652 /* and also allow a direct exit for 0 */
1653 xmlFAGenerateEpsilonTransition(ctxt, atom->start,
1654 newstate);
1655 } else {
1656 /*
1657 * either we need the atom at least once or there
1658 * is an atom->start0 allowing to easilly plug the
1659 * epsilon transition.
1660 */
1661 counter = xmlRegGetCounter(ctxt);
1662 ctxt->counters[counter].min = atom->min - 1;
1663 ctxt->counters[counter].max = atom->max - 1;
1664 /* count the number of times we see it again */
1665 xmlFAGenerateCountedEpsilonTransition(ctxt, atom->stop,
1666 atom->start, counter);
1667 /* allow a way out based on the count */
1668 xmlFAGenerateCountedTransition(ctxt, atom->stop,
1669 newstate, counter);
1670 /* and if needed allow a direct exit for 0 */
1671 if (atom->min == 0)
1672 xmlFAGenerateEpsilonTransition(ctxt, atom->start0,
1673 newstate);
1674
1675 }
1676 atom->min = 0;
1677 atom->max = 0;
1678 atom->quant = XML_REGEXP_QUANT_ONCE;
1679 ctxt->state = newstate;
Daniel Veillard4255d502002-04-16 15:50:10 +00001680 }
1681 default:
1682 break;
1683 }
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001684 return(0);
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001685 }
Daniel Veillard567a45b2005-10-18 19:11:55 +00001686 if ((atom->min == 0) && (atom->max == 0) &&
Daniel Veillard99c394d2005-07-14 12:58:49 +00001687 (atom->quant == XML_REGEXP_QUANT_RANGE)) {
1688 /*
1689 * we can discard the atom and generate an epsilon transition instead
1690 */
1691 if (to == NULL) {
1692 to = xmlRegNewState(ctxt);
1693 if (to != NULL)
1694 xmlRegStatePush(ctxt, to);
1695 else {
1696 return(-1);
1697 }
1698 }
1699 xmlFAGenerateEpsilonTransition(ctxt, from, to);
1700 ctxt->state = to;
1701 xmlRegFreeAtom(atom);
1702 return(0);
Daniel Veillard567a45b2005-10-18 19:11:55 +00001703 }
1704 if (to == NULL) {
1705 to = xmlRegNewState(ctxt);
1706 if (to != NULL)
1707 xmlRegStatePush(ctxt, to);
1708 else {
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001709 return(-1);
Daniel Veillard4255d502002-04-16 15:50:10 +00001710 }
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001711 }
Daniel Veillard10bda622008-03-13 07:27:24 +00001712 end = to;
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001713 if ((atom->quant == XML_REGEXP_QUANT_MULT) ||
Daniel Veillard10bda622008-03-13 07:27:24 +00001714 (atom->quant == XML_REGEXP_QUANT_PLUS)) {
1715 /*
1716 * Do not pollute the target state by adding transitions from
1717 * it as it is likely to be the shared target of multiple branches.
1718 * So isolate with an epsilon transition.
1719 */
1720 xmlRegStatePtr tmp;
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001721
Daniel Veillard10bda622008-03-13 07:27:24 +00001722 tmp = xmlRegNewState(ctxt);
1723 if (tmp != NULL)
1724 xmlRegStatePush(ctxt, tmp);
1725 else {
1726 return(-1);
1727 }
1728 xmlFAGenerateEpsilonTransition(ctxt, tmp, to);
1729 to = tmp;
Daniel Veillard4255d502002-04-16 15:50:10 +00001730 }
Daniel Veillard567a45b2005-10-18 19:11:55 +00001731 if (xmlRegAtomPush(ctxt, atom) < 0) {
1732 return(-1);
1733 }
Daniel Veillard34b35002016-05-09 09:28:38 +08001734 if ((atom->quant == XML_REGEXP_QUANT_RANGE) &&
1735 (atom->min == 0) && (atom->max > 0)) {
1736 nullable = 1;
1737 atom->min = 1;
1738 if (atom->max == 1)
1739 atom->quant = XML_REGEXP_QUANT_OPT;
1740 }
Daniel Veillard567a45b2005-10-18 19:11:55 +00001741 xmlRegStateAddTrans(ctxt, from, atom, to, -1, -1);
Daniel Veillard10bda622008-03-13 07:27:24 +00001742 ctxt->state = end;
Daniel Veillard4255d502002-04-16 15:50:10 +00001743 switch (atom->quant) {
1744 case XML_REGEXP_QUANT_OPT:
1745 atom->quant = XML_REGEXP_QUANT_ONCE;
1746 xmlFAGenerateEpsilonTransition(ctxt, from, to);
1747 break;
1748 case XML_REGEXP_QUANT_MULT:
1749 atom->quant = XML_REGEXP_QUANT_ONCE;
1750 xmlFAGenerateEpsilonTransition(ctxt, from, to);
Daniel Veillard5de09382005-09-26 17:18:17 +00001751 xmlRegStateAddTrans(ctxt, to, atom, to, -1, -1);
Daniel Veillard4255d502002-04-16 15:50:10 +00001752 break;
1753 case XML_REGEXP_QUANT_PLUS:
1754 atom->quant = XML_REGEXP_QUANT_ONCE;
Daniel Veillard5de09382005-09-26 17:18:17 +00001755 xmlRegStateAddTrans(ctxt, to, atom, to, -1, -1);
Daniel Veillard4255d502002-04-16 15:50:10 +00001756 break;
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001757 case XML_REGEXP_QUANT_RANGE:
Daniel Veillard34b35002016-05-09 09:28:38 +08001758 if (nullable)
William M. Brack56578372007-04-11 14:33:46 +00001759 xmlFAGenerateEpsilonTransition(ctxt, from, to);
William M. Brack56578372007-04-11 14:33:46 +00001760 break;
Daniel Veillard4255d502002-04-16 15:50:10 +00001761 default:
1762 break;
1763 }
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001764 return(0);
Daniel Veillard4255d502002-04-16 15:50:10 +00001765}
1766
1767/**
1768 * xmlFAReduceEpsilonTransitions:
Daniel Veillard441bc322002-04-20 17:38:48 +00001769 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00001770 * @fromnr: the from state
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001771 * @tonr: the to state
William M. Brackddf71d62004-05-06 04:17:26 +00001772 * @counter: should that transition be associated to a counted
Daniel Veillard4255d502002-04-16 15:50:10 +00001773 *
1774 */
1775static void
1776xmlFAReduceEpsilonTransitions(xmlRegParserCtxtPtr ctxt, int fromnr,
1777 int tonr, int counter) {
1778 int transnr;
1779 xmlRegStatePtr from;
1780 xmlRegStatePtr to;
1781
1782#ifdef DEBUG_REGEXP_GRAPH
1783 printf("xmlFAReduceEpsilonTransitions(%d, %d)\n", fromnr, tonr);
1784#endif
1785 from = ctxt->states[fromnr];
1786 if (from == NULL)
1787 return;
1788 to = ctxt->states[tonr];
1789 if (to == NULL)
1790 return;
1791 if ((to->mark == XML_REGEXP_MARK_START) ||
1792 (to->mark == XML_REGEXP_MARK_VISITED))
1793 return;
1794
1795 to->mark = XML_REGEXP_MARK_VISITED;
1796 if (to->type == XML_REGEXP_FINAL_STATE) {
1797#ifdef DEBUG_REGEXP_GRAPH
1798 printf("State %d is final, so %d becomes final\n", tonr, fromnr);
1799#endif
1800 from->type = XML_REGEXP_FINAL_STATE;
1801 }
1802 for (transnr = 0;transnr < to->nbTrans;transnr++) {
Daniel Veillarddb68b742005-07-30 13:18:24 +00001803 if (to->trans[transnr].to < 0)
1804 continue;
Daniel Veillard4255d502002-04-16 15:50:10 +00001805 if (to->trans[transnr].atom == NULL) {
1806 /*
1807 * Don't remove counted transitions
1808 * Don't loop either
1809 */
Daniel Veillardb509f152002-04-17 16:28:10 +00001810 if (to->trans[transnr].to != fromnr) {
1811 if (to->trans[transnr].count >= 0) {
1812 int newto = to->trans[transnr].to;
1813
1814 xmlRegStateAddTrans(ctxt, from, NULL,
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001815 ctxt->states[newto],
Daniel Veillard5de09382005-09-26 17:18:17 +00001816 -1, to->trans[transnr].count);
Daniel Veillardb509f152002-04-17 16:28:10 +00001817 } else {
Daniel Veillard4255d502002-04-16 15:50:10 +00001818#ifdef DEBUG_REGEXP_GRAPH
Daniel Veillardb509f152002-04-17 16:28:10 +00001819 printf("Found epsilon trans %d from %d to %d\n",
1820 transnr, tonr, to->trans[transnr].to);
Daniel Veillard4255d502002-04-16 15:50:10 +00001821#endif
Daniel Veillardb509f152002-04-17 16:28:10 +00001822 if (to->trans[transnr].counter >= 0) {
1823 xmlFAReduceEpsilonTransitions(ctxt, fromnr,
1824 to->trans[transnr].to,
1825 to->trans[transnr].counter);
1826 } else {
1827 xmlFAReduceEpsilonTransitions(ctxt, fromnr,
1828 to->trans[transnr].to,
1829 counter);
1830 }
1831 }
Daniel Veillard4255d502002-04-16 15:50:10 +00001832 }
1833 } else {
1834 int newto = to->trans[transnr].to;
1835
Daniel Veillardb509f152002-04-17 16:28:10 +00001836 if (to->trans[transnr].counter >= 0) {
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001837 xmlRegStateAddTrans(ctxt, from, to->trans[transnr].atom,
1838 ctxt->states[newto],
Daniel Veillard5de09382005-09-26 17:18:17 +00001839 to->trans[transnr].counter, -1);
Daniel Veillardb509f152002-04-17 16:28:10 +00001840 } else {
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001841 xmlRegStateAddTrans(ctxt, from, to->trans[transnr].atom,
Daniel Veillard5de09382005-09-26 17:18:17 +00001842 ctxt->states[newto], counter, -1);
Daniel Veillardb509f152002-04-17 16:28:10 +00001843 }
Daniel Veillard4255d502002-04-16 15:50:10 +00001844 }
1845 }
1846 to->mark = XML_REGEXP_MARK_NORMAL;
1847}
1848
1849/**
Daniel Veillarddb68b742005-07-30 13:18:24 +00001850 * xmlFAEliminateSimpleEpsilonTransitions:
1851 * @ctxt: a regexp parser context
1852 *
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001853 * Eliminating general epsilon transitions can get costly in the general
Daniel Veillarddb68b742005-07-30 13:18:24 +00001854 * algorithm due to the large amount of generated new transitions and
1855 * associated comparisons. However for simple epsilon transition used just
1856 * to separate building blocks when generating the automata this can be
1857 * reduced to state elimination:
1858 * - if there exists an epsilon from X to Y
1859 * - if there is no other transition from X
1860 * then X and Y are semantically equivalent and X can be eliminated
1861 * If X is the start state then make Y the start state, else replace the
1862 * target of all transitions to X by transitions to Y.
1863 */
1864static void
1865xmlFAEliminateSimpleEpsilonTransitions(xmlRegParserCtxtPtr ctxt) {
1866 int statenr, i, j, newto;
1867 xmlRegStatePtr state, tmp;
1868
1869 for (statenr = 0;statenr < ctxt->nbStates;statenr++) {
1870 state = ctxt->states[statenr];
1871 if (state == NULL)
1872 continue;
1873 if (state->nbTrans != 1)
1874 continue;
Daniel Veillard0e05f4c2006-11-01 15:33:04 +00001875 if (state->type == XML_REGEXP_UNREACH_STATE)
1876 continue;
Daniel Veillarddb68b742005-07-30 13:18:24 +00001877 /* is the only transition out a basic transition */
1878 if ((state->trans[0].atom == NULL) &&
1879 (state->trans[0].to >= 0) &&
1880 (state->trans[0].to != statenr) &&
1881 (state->trans[0].counter < 0) &&
1882 (state->trans[0].count < 0)) {
1883 newto = state->trans[0].to;
1884
1885 if (state->type == XML_REGEXP_START_STATE) {
1886#ifdef DEBUG_REGEXP_GRAPH
1887 printf("Found simple epsilon trans from start %d to %d\n",
1888 statenr, newto);
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001889#endif
Daniel Veillarddb68b742005-07-30 13:18:24 +00001890 } else {
1891#ifdef DEBUG_REGEXP_GRAPH
1892 printf("Found simple epsilon trans from %d to %d\n",
1893 statenr, newto);
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001894#endif
Daniel Veillarddb68b742005-07-30 13:18:24 +00001895 for (i = 0;i < state->nbTransTo;i++) {
1896 tmp = ctxt->states[state->transTo[i]];
1897 for (j = 0;j < tmp->nbTrans;j++) {
1898 if (tmp->trans[j].to == statenr) {
Daniel Veillarddb68b742005-07-30 13:18:24 +00001899#ifdef DEBUG_REGEXP_GRAPH
1900 printf("Changed transition %d on %d to go to %d\n",
1901 j, tmp->no, newto);
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001902#endif
Daniel Veillard0e05f4c2006-11-01 15:33:04 +00001903 tmp->trans[j].to = -1;
1904 xmlRegStateAddTrans(ctxt, tmp, tmp->trans[j].atom,
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001905 ctxt->states[newto],
Daniel Veillard0e05f4c2006-11-01 15:33:04 +00001906 tmp->trans[j].counter,
1907 tmp->trans[j].count);
Daniel Veillarddb68b742005-07-30 13:18:24 +00001908 }
1909 }
1910 }
Daniel Veillarddb68b742005-07-30 13:18:24 +00001911 if (state->type == XML_REGEXP_FINAL_STATE)
1912 ctxt->states[newto]->type = XML_REGEXP_FINAL_STATE;
1913 /* eliminate the transition completely */
1914 state->nbTrans = 0;
1915
Daniel Veillard0e05f4c2006-11-01 15:33:04 +00001916 state->type = XML_REGEXP_UNREACH_STATE;
Daniel Veillarddb68b742005-07-30 13:18:24 +00001917
1918 }
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001919
Daniel Veillarddb68b742005-07-30 13:18:24 +00001920 }
1921 }
1922}
1923/**
Daniel Veillard4255d502002-04-16 15:50:10 +00001924 * xmlFAEliminateEpsilonTransitions:
Daniel Veillard441bc322002-04-20 17:38:48 +00001925 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00001926 *
1927 */
1928static void
1929xmlFAEliminateEpsilonTransitions(xmlRegParserCtxtPtr ctxt) {
1930 int statenr, transnr;
1931 xmlRegStatePtr state;
Daniel Veillarddb68b742005-07-30 13:18:24 +00001932 int has_epsilon;
Daniel Veillard4255d502002-04-16 15:50:10 +00001933
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001934 if (ctxt->states == NULL) return;
1935
Daniel Veillard0e05f4c2006-11-01 15:33:04 +00001936 /*
1937 * Eliminate simple epsilon transition and the associated unreachable
1938 * states.
1939 */
Daniel Veillarddb68b742005-07-30 13:18:24 +00001940 xmlFAEliminateSimpleEpsilonTransitions(ctxt);
Daniel Veillard0e05f4c2006-11-01 15:33:04 +00001941 for (statenr = 0;statenr < ctxt->nbStates;statenr++) {
1942 state = ctxt->states[statenr];
1943 if ((state != NULL) && (state->type == XML_REGEXP_UNREACH_STATE)) {
1944#ifdef DEBUG_REGEXP_GRAPH
1945 printf("Removed unreachable state %d\n", statenr);
1946#endif
1947 xmlRegFreeState(state);
1948 ctxt->states[statenr] = NULL;
1949 }
1950 }
Daniel Veillarddb68b742005-07-30 13:18:24 +00001951
1952 has_epsilon = 0;
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001953
Daniel Veillard4255d502002-04-16 15:50:10 +00001954 /*
Daniel Veillardfcd18ff2006-11-02 10:28:04 +00001955 * Build the completed transitions bypassing the epsilons
Daniel Veillard4255d502002-04-16 15:50:10 +00001956 * Use a marking algorithm to avoid loops
Daniel Veillardfcd18ff2006-11-02 10:28:04 +00001957 * Mark sink states too.
1958 * Process from the latests states backward to the start when
1959 * there is long cascading epsilon chains this minimize the
1960 * recursions and transition compares when adding the new ones
Daniel Veillard4255d502002-04-16 15:50:10 +00001961 */
Daniel Veillardfcd18ff2006-11-02 10:28:04 +00001962 for (statenr = ctxt->nbStates - 1;statenr >= 0;statenr--) {
Daniel Veillard4255d502002-04-16 15:50:10 +00001963 state = ctxt->states[statenr];
1964 if (state == NULL)
1965 continue;
Daniel Veillardcc026dc2005-01-12 13:21:17 +00001966 if ((state->nbTrans == 0) &&
1967 (state->type != XML_REGEXP_FINAL_STATE)) {
1968 state->type = XML_REGEXP_SINK_STATE;
1969 }
Daniel Veillard4255d502002-04-16 15:50:10 +00001970 for (transnr = 0;transnr < state->nbTrans;transnr++) {
1971 if ((state->trans[transnr].atom == NULL) &&
1972 (state->trans[transnr].to >= 0)) {
1973 if (state->trans[transnr].to == statenr) {
1974 state->trans[transnr].to = -1;
1975#ifdef DEBUG_REGEXP_GRAPH
1976 printf("Removed loopback epsilon trans %d on %d\n",
1977 transnr, statenr);
1978#endif
1979 } else if (state->trans[transnr].count < 0) {
1980 int newto = state->trans[transnr].to;
1981
1982#ifdef DEBUG_REGEXP_GRAPH
1983 printf("Found epsilon trans %d from %d to %d\n",
1984 transnr, statenr, newto);
1985#endif
Daniel Veillarddb68b742005-07-30 13:18:24 +00001986 has_epsilon = 1;
Daniel Veillard0e05f4c2006-11-01 15:33:04 +00001987 state->trans[transnr].to = -2;
1988 state->mark = XML_REGEXP_MARK_START;
Daniel Veillard4255d502002-04-16 15:50:10 +00001989 xmlFAReduceEpsilonTransitions(ctxt, statenr,
1990 newto, state->trans[transnr].counter);
1991 state->mark = XML_REGEXP_MARK_NORMAL;
1992#ifdef DEBUG_REGEXP_GRAPH
1993 } else {
1994 printf("Found counted transition %d on %d\n",
1995 transnr, statenr);
1996#endif
1997 }
1998 }
1999 }
2000 }
2001 /*
2002 * Eliminate the epsilon transitions
2003 */
Daniel Veillarddb68b742005-07-30 13:18:24 +00002004 if (has_epsilon) {
2005 for (statenr = 0;statenr < ctxt->nbStates;statenr++) {
2006 state = ctxt->states[statenr];
2007 if (state == NULL)
2008 continue;
2009 for (transnr = 0;transnr < state->nbTrans;transnr++) {
2010 xmlRegTransPtr trans = &(state->trans[transnr]);
2011 if ((trans->atom == NULL) &&
2012 (trans->count < 0) &&
2013 (trans->to >= 0)) {
2014 trans->to = -1;
2015 }
Daniel Veillard4255d502002-04-16 15:50:10 +00002016 }
2017 }
2018 }
Daniel Veillard23e73572002-09-19 19:56:43 +00002019
2020 /*
2021 * Use this pass to detect unreachable states too
2022 */
2023 for (statenr = 0;statenr < ctxt->nbStates;statenr++) {
2024 state = ctxt->states[statenr];
2025 if (state != NULL)
William M. Brack779af002003-08-01 15:55:39 +00002026 state->reached = XML_REGEXP_MARK_NORMAL;
Daniel Veillard23e73572002-09-19 19:56:43 +00002027 }
2028 state = ctxt->states[0];
2029 if (state != NULL)
William M. Brack779af002003-08-01 15:55:39 +00002030 state->reached = XML_REGEXP_MARK_START;
Daniel Veillard23e73572002-09-19 19:56:43 +00002031 while (state != NULL) {
2032 xmlRegStatePtr target = NULL;
William M. Brack779af002003-08-01 15:55:39 +00002033 state->reached = XML_REGEXP_MARK_VISITED;
Daniel Veillard23e73572002-09-19 19:56:43 +00002034 /*
William M. Brackddf71d62004-05-06 04:17:26 +00002035 * Mark all states reachable from the current reachable state
Daniel Veillard23e73572002-09-19 19:56:43 +00002036 */
2037 for (transnr = 0;transnr < state->nbTrans;transnr++) {
2038 if ((state->trans[transnr].to >= 0) &&
2039 ((state->trans[transnr].atom != NULL) ||
2040 (state->trans[transnr].count >= 0))) {
2041 int newto = state->trans[transnr].to;
2042
2043 if (ctxt->states[newto] == NULL)
2044 continue;
William M. Brack779af002003-08-01 15:55:39 +00002045 if (ctxt->states[newto]->reached == XML_REGEXP_MARK_NORMAL) {
2046 ctxt->states[newto]->reached = XML_REGEXP_MARK_START;
Daniel Veillard23e73572002-09-19 19:56:43 +00002047 target = ctxt->states[newto];
2048 }
2049 }
2050 }
Daniel Veillardcc026dc2005-01-12 13:21:17 +00002051
Daniel Veillard23e73572002-09-19 19:56:43 +00002052 /*
2053 * find the next accessible state not explored
2054 */
2055 if (target == NULL) {
2056 for (statenr = 1;statenr < ctxt->nbStates;statenr++) {
2057 state = ctxt->states[statenr];
William M. Brack779af002003-08-01 15:55:39 +00002058 if ((state != NULL) && (state->reached ==
2059 XML_REGEXP_MARK_START)) {
Daniel Veillard23e73572002-09-19 19:56:43 +00002060 target = state;
2061 break;
2062 }
2063 }
2064 }
2065 state = target;
2066 }
2067 for (statenr = 0;statenr < ctxt->nbStates;statenr++) {
2068 state = ctxt->states[statenr];
William M. Brack779af002003-08-01 15:55:39 +00002069 if ((state != NULL) && (state->reached == XML_REGEXP_MARK_NORMAL)) {
Daniel Veillard23e73572002-09-19 19:56:43 +00002070#ifdef DEBUG_REGEXP_GRAPH
2071 printf("Removed unreachable state %d\n", statenr);
2072#endif
2073 xmlRegFreeState(state);
2074 ctxt->states[statenr] = NULL;
2075 }
2076 }
2077
Daniel Veillard4255d502002-04-16 15:50:10 +00002078}
2079
Daniel Veillard567a45b2005-10-18 19:11:55 +00002080static int
2081xmlFACompareRanges(xmlRegRangePtr range1, xmlRegRangePtr range2) {
2082 int ret = 0;
2083
2084 if ((range1->type == XML_REGEXP_RANGES) ||
2085 (range2->type == XML_REGEXP_RANGES) ||
2086 (range2->type == XML_REGEXP_SUBREG) ||
2087 (range1->type == XML_REGEXP_SUBREG) ||
2088 (range1->type == XML_REGEXP_STRING) ||
2089 (range2->type == XML_REGEXP_STRING))
2090 return(-1);
2091
2092 /* put them in order */
2093 if (range1->type > range2->type) {
2094 xmlRegRangePtr tmp;
2095
2096 tmp = range1;
2097 range1 = range2;
2098 range2 = tmp;
2099 }
2100 if ((range1->type == XML_REGEXP_ANYCHAR) ||
2101 (range2->type == XML_REGEXP_ANYCHAR)) {
2102 ret = 1;
2103 } else if ((range1->type == XML_REGEXP_EPSILON) ||
2104 (range2->type == XML_REGEXP_EPSILON)) {
2105 return(0);
2106 } else if (range1->type == range2->type) {
Daniel Veillard9332b482009-09-23 18:28:43 +02002107 if (range1->type != XML_REGEXP_CHARVAL)
2108 ret = 1;
2109 else if ((range1->end < range2->start) ||
2110 (range2->end < range1->start))
Daniel Veillard567a45b2005-10-18 19:11:55 +00002111 ret = 0;
Daniel Veillard9332b482009-09-23 18:28:43 +02002112 else
2113 ret = 1;
Daniel Veillard567a45b2005-10-18 19:11:55 +00002114 } else if (range1->type == XML_REGEXP_CHARVAL) {
2115 int codepoint;
2116 int neg = 0;
2117
2118 /*
2119 * just check all codepoints in the range for acceptance,
2120 * this is usually way cheaper since done only once at
Daniel Veillardf8e3db02012-09-11 13:26:36 +08002121 * compilation than testing over and over at runtime or
Daniel Veillard567a45b2005-10-18 19:11:55 +00002122 * pushing too many states when evaluating.
2123 */
2124 if (((range1->neg == 0) && (range2->neg != 0)) ||
2125 ((range1->neg != 0) && (range2->neg == 0)))
2126 neg = 1;
2127
2128 for (codepoint = range1->start;codepoint <= range1->end ;codepoint++) {
2129 ret = xmlRegCheckCharacterRange(range2->type, codepoint,
2130 0, range2->start, range2->end,
2131 range2->blockName);
2132 if (ret < 0)
2133 return(-1);
2134 if (((neg == 1) && (ret == 0)) ||
2135 ((neg == 0) && (ret == 1)))
2136 return(1);
2137 }
2138 return(0);
2139 } else if ((range1->type == XML_REGEXP_BLOCK_NAME) ||
2140 (range2->type == XML_REGEXP_BLOCK_NAME)) {
2141 if (range1->type == range2->type) {
2142 ret = xmlStrEqual(range1->blockName, range2->blockName);
2143 } else {
2144 /*
2145 * comparing a block range with anything else is way
2146 * too costly, and maintining the table is like too much
2147 * memory too, so let's force the automata to save state
2148 * here.
2149 */
2150 return(1);
2151 }
2152 } else if ((range1->type < XML_REGEXP_LETTER) ||
2153 (range2->type < XML_REGEXP_LETTER)) {
2154 if ((range1->type == XML_REGEXP_ANYSPACE) &&
2155 (range2->type == XML_REGEXP_NOTSPACE))
2156 ret = 0;
2157 else if ((range1->type == XML_REGEXP_INITNAME) &&
2158 (range2->type == XML_REGEXP_NOTINITNAME))
2159 ret = 0;
2160 else if ((range1->type == XML_REGEXP_NAMECHAR) &&
2161 (range2->type == XML_REGEXP_NOTNAMECHAR))
2162 ret = 0;
2163 else if ((range1->type == XML_REGEXP_DECIMAL) &&
2164 (range2->type == XML_REGEXP_NOTDECIMAL))
2165 ret = 0;
2166 else if ((range1->type == XML_REGEXP_REALCHAR) &&
2167 (range2->type == XML_REGEXP_NOTREALCHAR))
2168 ret = 0;
2169 else {
2170 /* same thing to limit complexity */
2171 return(1);
2172 }
2173 } else {
2174 ret = 0;
2175 /* range1->type < range2->type here */
2176 switch (range1->type) {
2177 case XML_REGEXP_LETTER:
2178 /* all disjoint except in the subgroups */
2179 if ((range2->type == XML_REGEXP_LETTER_UPPERCASE) ||
2180 (range2->type == XML_REGEXP_LETTER_LOWERCASE) ||
2181 (range2->type == XML_REGEXP_LETTER_TITLECASE) ||
2182 (range2->type == XML_REGEXP_LETTER_MODIFIER) ||
2183 (range2->type == XML_REGEXP_LETTER_OTHERS))
2184 ret = 1;
2185 break;
2186 case XML_REGEXP_MARK:
2187 if ((range2->type == XML_REGEXP_MARK_NONSPACING) ||
2188 (range2->type == XML_REGEXP_MARK_SPACECOMBINING) ||
2189 (range2->type == XML_REGEXP_MARK_ENCLOSING))
2190 ret = 1;
2191 break;
2192 case XML_REGEXP_NUMBER:
2193 if ((range2->type == XML_REGEXP_NUMBER_DECIMAL) ||
2194 (range2->type == XML_REGEXP_NUMBER_LETTER) ||
2195 (range2->type == XML_REGEXP_NUMBER_OTHERS))
2196 ret = 1;
2197 break;
2198 case XML_REGEXP_PUNCT:
2199 if ((range2->type == XML_REGEXP_PUNCT_CONNECTOR) ||
2200 (range2->type == XML_REGEXP_PUNCT_DASH) ||
2201 (range2->type == XML_REGEXP_PUNCT_OPEN) ||
2202 (range2->type == XML_REGEXP_PUNCT_CLOSE) ||
2203 (range2->type == XML_REGEXP_PUNCT_INITQUOTE) ||
2204 (range2->type == XML_REGEXP_PUNCT_FINQUOTE) ||
2205 (range2->type == XML_REGEXP_PUNCT_OTHERS))
2206 ret = 1;
2207 break;
2208 case XML_REGEXP_SEPAR:
2209 if ((range2->type == XML_REGEXP_SEPAR_SPACE) ||
2210 (range2->type == XML_REGEXP_SEPAR_LINE) ||
2211 (range2->type == XML_REGEXP_SEPAR_PARA))
2212 ret = 1;
2213 break;
2214 case XML_REGEXP_SYMBOL:
2215 if ((range2->type == XML_REGEXP_SYMBOL_MATH) ||
2216 (range2->type == XML_REGEXP_SYMBOL_CURRENCY) ||
2217 (range2->type == XML_REGEXP_SYMBOL_MODIFIER) ||
2218 (range2->type == XML_REGEXP_SYMBOL_OTHERS))
2219 ret = 1;
2220 break;
2221 case XML_REGEXP_OTHER:
2222 if ((range2->type == XML_REGEXP_OTHER_CONTROL) ||
2223 (range2->type == XML_REGEXP_OTHER_FORMAT) ||
2224 (range2->type == XML_REGEXP_OTHER_PRIVATE))
2225 ret = 1;
2226 break;
2227 default:
2228 if ((range2->type >= XML_REGEXP_LETTER) &&
2229 (range2->type < XML_REGEXP_BLOCK_NAME))
2230 ret = 0;
2231 else {
2232 /* safety net ! */
2233 return(1);
2234 }
2235 }
2236 }
2237 if (((range1->neg == 0) && (range2->neg != 0)) ||
2238 ((range1->neg != 0) && (range2->neg == 0)))
2239 ret = !ret;
Daniel Veillard594e5df2009-09-07 14:58:47 +02002240 return(ret);
Daniel Veillard567a45b2005-10-18 19:11:55 +00002241}
2242
Daniel Veillarde19fc232002-04-22 16:01:24 +00002243/**
Daniel Veillardfc011b72006-02-12 19:14:15 +00002244 * xmlFACompareAtomTypes:
2245 * @type1: an atom type
2246 * @type2: an atom type
2247 *
2248 * Compares two atoms type to check whether they intersect in some ways,
2249 * this is used by xmlFACompareAtoms only
2250 *
2251 * Returns 1 if they may intersect and 0 otherwise
2252 */
2253static int
2254xmlFACompareAtomTypes(xmlRegAtomType type1, xmlRegAtomType type2) {
2255 if ((type1 == XML_REGEXP_EPSILON) ||
2256 (type1 == XML_REGEXP_CHARVAL) ||
2257 (type1 == XML_REGEXP_RANGES) ||
2258 (type1 == XML_REGEXP_SUBREG) ||
2259 (type1 == XML_REGEXP_STRING) ||
2260 (type1 == XML_REGEXP_ANYCHAR))
2261 return(1);
2262 if ((type2 == XML_REGEXP_EPSILON) ||
2263 (type2 == XML_REGEXP_CHARVAL) ||
2264 (type2 == XML_REGEXP_RANGES) ||
2265 (type2 == XML_REGEXP_SUBREG) ||
2266 (type2 == XML_REGEXP_STRING) ||
2267 (type2 == XML_REGEXP_ANYCHAR))
2268 return(1);
2269
2270 if (type1 == type2) return(1);
2271
2272 /* simplify subsequent compares by making sure type1 < type2 */
2273 if (type1 > type2) {
2274 xmlRegAtomType tmp = type1;
2275 type1 = type2;
2276 type2 = tmp;
2277 }
2278 switch (type1) {
2279 case XML_REGEXP_ANYSPACE: /* \s */
2280 /* can't be a letter, number, mark, pontuation, symbol */
2281 if ((type2 == XML_REGEXP_NOTSPACE) ||
2282 ((type2 >= XML_REGEXP_LETTER) &&
2283 (type2 <= XML_REGEXP_LETTER_OTHERS)) ||
2284 ((type2 >= XML_REGEXP_NUMBER) &&
2285 (type2 <= XML_REGEXP_NUMBER_OTHERS)) ||
2286 ((type2 >= XML_REGEXP_MARK) &&
2287 (type2 <= XML_REGEXP_MARK_ENCLOSING)) ||
2288 ((type2 >= XML_REGEXP_PUNCT) &&
2289 (type2 <= XML_REGEXP_PUNCT_OTHERS)) ||
2290 ((type2 >= XML_REGEXP_SYMBOL) &&
2291 (type2 <= XML_REGEXP_SYMBOL_OTHERS))
2292 ) return(0);
2293 break;
2294 case XML_REGEXP_NOTSPACE: /* \S */
2295 break;
2296 case XML_REGEXP_INITNAME: /* \l */
2297 /* can't be a number, mark, separator, pontuation, symbol or other */
2298 if ((type2 == XML_REGEXP_NOTINITNAME) ||
2299 ((type2 >= XML_REGEXP_NUMBER) &&
2300 (type2 <= XML_REGEXP_NUMBER_OTHERS)) ||
2301 ((type2 >= XML_REGEXP_MARK) &&
2302 (type2 <= XML_REGEXP_MARK_ENCLOSING)) ||
2303 ((type2 >= XML_REGEXP_SEPAR) &&
2304 (type2 <= XML_REGEXP_SEPAR_PARA)) ||
2305 ((type2 >= XML_REGEXP_PUNCT) &&
2306 (type2 <= XML_REGEXP_PUNCT_OTHERS)) ||
2307 ((type2 >= XML_REGEXP_SYMBOL) &&
2308 (type2 <= XML_REGEXP_SYMBOL_OTHERS)) ||
2309 ((type2 >= XML_REGEXP_OTHER) &&
2310 (type2 <= XML_REGEXP_OTHER_NA))
2311 ) return(0);
2312 break;
2313 case XML_REGEXP_NOTINITNAME: /* \L */
2314 break;
2315 case XML_REGEXP_NAMECHAR: /* \c */
2316 /* can't be a mark, separator, pontuation, symbol or other */
2317 if ((type2 == XML_REGEXP_NOTNAMECHAR) ||
2318 ((type2 >= XML_REGEXP_MARK) &&
2319 (type2 <= XML_REGEXP_MARK_ENCLOSING)) ||
2320 ((type2 >= XML_REGEXP_PUNCT) &&
2321 (type2 <= XML_REGEXP_PUNCT_OTHERS)) ||
2322 ((type2 >= XML_REGEXP_SEPAR) &&
2323 (type2 <= XML_REGEXP_SEPAR_PARA)) ||
2324 ((type2 >= XML_REGEXP_SYMBOL) &&
2325 (type2 <= XML_REGEXP_SYMBOL_OTHERS)) ||
2326 ((type2 >= XML_REGEXP_OTHER) &&
2327 (type2 <= XML_REGEXP_OTHER_NA))
2328 ) return(0);
2329 break;
2330 case XML_REGEXP_NOTNAMECHAR: /* \C */
2331 break;
2332 case XML_REGEXP_DECIMAL: /* \d */
2333 /* can't be a letter, mark, separator, pontuation, symbol or other */
2334 if ((type2 == XML_REGEXP_NOTDECIMAL) ||
2335 (type2 == XML_REGEXP_REALCHAR) ||
2336 ((type2 >= XML_REGEXP_LETTER) &&
2337 (type2 <= XML_REGEXP_LETTER_OTHERS)) ||
2338 ((type2 >= XML_REGEXP_MARK) &&
2339 (type2 <= XML_REGEXP_MARK_ENCLOSING)) ||
2340 ((type2 >= XML_REGEXP_PUNCT) &&
2341 (type2 <= XML_REGEXP_PUNCT_OTHERS)) ||
2342 ((type2 >= XML_REGEXP_SEPAR) &&
2343 (type2 <= XML_REGEXP_SEPAR_PARA)) ||
2344 ((type2 >= XML_REGEXP_SYMBOL) &&
2345 (type2 <= XML_REGEXP_SYMBOL_OTHERS)) ||
2346 ((type2 >= XML_REGEXP_OTHER) &&
2347 (type2 <= XML_REGEXP_OTHER_NA))
2348 )return(0);
2349 break;
2350 case XML_REGEXP_NOTDECIMAL: /* \D */
2351 break;
2352 case XML_REGEXP_REALCHAR: /* \w */
2353 /* can't be a mark, separator, pontuation, symbol or other */
2354 if ((type2 == XML_REGEXP_NOTDECIMAL) ||
2355 ((type2 >= XML_REGEXP_MARK) &&
2356 (type2 <= XML_REGEXP_MARK_ENCLOSING)) ||
2357 ((type2 >= XML_REGEXP_PUNCT) &&
2358 (type2 <= XML_REGEXP_PUNCT_OTHERS)) ||
2359 ((type2 >= XML_REGEXP_SEPAR) &&
2360 (type2 <= XML_REGEXP_SEPAR_PARA)) ||
2361 ((type2 >= XML_REGEXP_SYMBOL) &&
2362 (type2 <= XML_REGEXP_SYMBOL_OTHERS)) ||
2363 ((type2 >= XML_REGEXP_OTHER) &&
2364 (type2 <= XML_REGEXP_OTHER_NA))
2365 )return(0);
2366 break;
2367 case XML_REGEXP_NOTREALCHAR: /* \W */
2368 break;
2369 /*
2370 * at that point we know both type 1 and type2 are from
2371 * character categories are ordered and are different,
2372 * it becomes simple because this is a partition
2373 */
2374 case XML_REGEXP_LETTER:
2375 if (type2 <= XML_REGEXP_LETTER_OTHERS)
2376 return(1);
2377 return(0);
2378 case XML_REGEXP_LETTER_UPPERCASE:
2379 case XML_REGEXP_LETTER_LOWERCASE:
2380 case XML_REGEXP_LETTER_TITLECASE:
2381 case XML_REGEXP_LETTER_MODIFIER:
2382 case XML_REGEXP_LETTER_OTHERS:
2383 return(0);
2384 case XML_REGEXP_MARK:
2385 if (type2 <= XML_REGEXP_MARK_ENCLOSING)
2386 return(1);
2387 return(0);
2388 case XML_REGEXP_MARK_NONSPACING:
2389 case XML_REGEXP_MARK_SPACECOMBINING:
2390 case XML_REGEXP_MARK_ENCLOSING:
2391 return(0);
2392 case XML_REGEXP_NUMBER:
2393 if (type2 <= XML_REGEXP_NUMBER_OTHERS)
2394 return(1);
2395 return(0);
2396 case XML_REGEXP_NUMBER_DECIMAL:
2397 case XML_REGEXP_NUMBER_LETTER:
2398 case XML_REGEXP_NUMBER_OTHERS:
2399 return(0);
2400 case XML_REGEXP_PUNCT:
2401 if (type2 <= XML_REGEXP_PUNCT_OTHERS)
2402 return(1);
2403 return(0);
2404 case XML_REGEXP_PUNCT_CONNECTOR:
2405 case XML_REGEXP_PUNCT_DASH:
2406 case XML_REGEXP_PUNCT_OPEN:
2407 case XML_REGEXP_PUNCT_CLOSE:
2408 case XML_REGEXP_PUNCT_INITQUOTE:
2409 case XML_REGEXP_PUNCT_FINQUOTE:
2410 case XML_REGEXP_PUNCT_OTHERS:
2411 return(0);
2412 case XML_REGEXP_SEPAR:
2413 if (type2 <= XML_REGEXP_SEPAR_PARA)
2414 return(1);
2415 return(0);
2416 case XML_REGEXP_SEPAR_SPACE:
2417 case XML_REGEXP_SEPAR_LINE:
2418 case XML_REGEXP_SEPAR_PARA:
2419 return(0);
2420 case XML_REGEXP_SYMBOL:
2421 if (type2 <= XML_REGEXP_SYMBOL_OTHERS)
2422 return(1);
2423 return(0);
2424 case XML_REGEXP_SYMBOL_MATH:
2425 case XML_REGEXP_SYMBOL_CURRENCY:
2426 case XML_REGEXP_SYMBOL_MODIFIER:
2427 case XML_REGEXP_SYMBOL_OTHERS:
2428 return(0);
2429 case XML_REGEXP_OTHER:
2430 if (type2 <= XML_REGEXP_OTHER_NA)
2431 return(1);
2432 return(0);
2433 case XML_REGEXP_OTHER_CONTROL:
2434 case XML_REGEXP_OTHER_FORMAT:
2435 case XML_REGEXP_OTHER_PRIVATE:
2436 case XML_REGEXP_OTHER_NA:
2437 return(0);
2438 default:
2439 break;
2440 }
2441 return(1);
2442}
2443
2444/**
2445 * xmlFAEqualAtoms:
Daniel Veillarde19fc232002-04-22 16:01:24 +00002446 * @atom1: an atom
2447 * @atom2: an atom
Daniel Veillard1ba2aca2009-08-31 16:47:39 +02002448 * @deep: if not set only compare string pointers
Daniel Veillarde19fc232002-04-22 16:01:24 +00002449 *
Daniel Veillardfc011b72006-02-12 19:14:15 +00002450 * Compares two atoms to check whether they are the same exactly
2451 * this is used to remove equivalent transitions
Daniel Veillarde19fc232002-04-22 16:01:24 +00002452 *
Daniel Veillardfc011b72006-02-12 19:14:15 +00002453 * Returns 1 if same and 0 otherwise
Daniel Veillarde19fc232002-04-22 16:01:24 +00002454 */
2455static int
Daniel Veillard1ba2aca2009-08-31 16:47:39 +02002456xmlFAEqualAtoms(xmlRegAtomPtr atom1, xmlRegAtomPtr atom2, int deep) {
Daniel Veillardfc011b72006-02-12 19:14:15 +00002457 int ret = 0;
Daniel Veillard9efc4762005-07-19 14:33:55 +00002458
Daniel Veillarde19fc232002-04-22 16:01:24 +00002459 if (atom1 == atom2)
2460 return(1);
2461 if ((atom1 == NULL) || (atom2 == NULL))
2462 return(0);
2463
Daniel Veillardfc011b72006-02-12 19:14:15 +00002464 if (atom1->type != atom2->type)
2465 return(0);
2466 switch (atom1->type) {
2467 case XML_REGEXP_EPSILON:
2468 ret = 0;
2469 break;
2470 case XML_REGEXP_STRING:
Daniel Veillard1ba2aca2009-08-31 16:47:39 +02002471 if (!deep)
2472 ret = (atom1->valuep == atom2->valuep);
2473 else
2474 ret = xmlStrEqual((xmlChar *)atom1->valuep,
2475 (xmlChar *)atom2->valuep);
Daniel Veillardfc011b72006-02-12 19:14:15 +00002476 break;
2477 case XML_REGEXP_CHARVAL:
2478 ret = (atom1->codepoint == atom2->codepoint);
2479 break;
2480 case XML_REGEXP_RANGES:
2481 /* too hard to do in the general case */
2482 ret = 0;
2483 default:
2484 break;
2485 }
2486 return(ret);
2487}
2488
2489/**
2490 * xmlFACompareAtoms:
2491 * @atom1: an atom
2492 * @atom2: an atom
Daniel Veillard1ba2aca2009-08-31 16:47:39 +02002493 * @deep: if not set only compare string pointers
Daniel Veillardfc011b72006-02-12 19:14:15 +00002494 *
2495 * Compares two atoms to check whether they intersect in some ways,
2496 * this is used by xmlFAComputesDeterminism and xmlFARecurseDeterminism only
2497 *
2498 * Returns 1 if yes and 0 otherwise
2499 */
2500static int
Daniel Veillard1ba2aca2009-08-31 16:47:39 +02002501xmlFACompareAtoms(xmlRegAtomPtr atom1, xmlRegAtomPtr atom2, int deep) {
Daniel Veillardfc011b72006-02-12 19:14:15 +00002502 int ret = 1;
2503
2504 if (atom1 == atom2)
2505 return(1);
2506 if ((atom1 == NULL) || (atom2 == NULL))
2507 return(0);
2508
2509 if ((atom1->type == XML_REGEXP_ANYCHAR) ||
2510 (atom2->type == XML_REGEXP_ANYCHAR))
2511 return(1);
2512
2513 if (atom1->type > atom2->type) {
Daniel Veillard567a45b2005-10-18 19:11:55 +00002514 xmlRegAtomPtr tmp;
2515 tmp = atom1;
2516 atom1 = atom2;
2517 atom2 = tmp;
Daniel Veillardfc011b72006-02-12 19:14:15 +00002518 }
2519 if (atom1->type != atom2->type) {
2520 ret = xmlFACompareAtomTypes(atom1->type, atom2->type);
2521 /* if they can't intersect at the type level break now */
2522 if (ret == 0)
2523 return(0);
Daniel Veillard567a45b2005-10-18 19:11:55 +00002524 }
Daniel Veillarde19fc232002-04-22 16:01:24 +00002525 switch (atom1->type) {
2526 case XML_REGEXP_STRING:
Daniel Veillard1ba2aca2009-08-31 16:47:39 +02002527 if (!deep)
2528 ret = (atom1->valuep != atom2->valuep);
2529 else
2530 ret = xmlRegStrEqualWildcard((xmlChar *)atom1->valuep,
2531 (xmlChar *)atom2->valuep);
Daniel Veillard9efc4762005-07-19 14:33:55 +00002532 break;
Daniel Veillarde19fc232002-04-22 16:01:24 +00002533 case XML_REGEXP_EPSILON:
Daniel Veillard567a45b2005-10-18 19:11:55 +00002534 goto not_determinist;
Daniel Veillarde19fc232002-04-22 16:01:24 +00002535 case XML_REGEXP_CHARVAL:
Daniel Veillardfc011b72006-02-12 19:14:15 +00002536 if (atom2->type == XML_REGEXP_CHARVAL) {
2537 ret = (atom1->codepoint == atom2->codepoint);
2538 } else {
2539 ret = xmlRegCheckCharacter(atom2, atom1->codepoint);
2540 if (ret < 0)
2541 ret = 1;
2542 }
Daniel Veillard9efc4762005-07-19 14:33:55 +00002543 break;
Daniel Veillarde19fc232002-04-22 16:01:24 +00002544 case XML_REGEXP_RANGES:
Daniel Veillardfc011b72006-02-12 19:14:15 +00002545 if (atom2->type == XML_REGEXP_RANGES) {
Daniel Veillard567a45b2005-10-18 19:11:55 +00002546 int i, j, res;
2547 xmlRegRangePtr r1, r2;
2548
2549 /*
2550 * need to check that none of the ranges eventually matches
2551 */
2552 for (i = 0;i < atom1->nbRanges;i++) {
2553 for (j = 0;j < atom2->nbRanges;j++) {
2554 r1 = atom1->ranges[i];
2555 r2 = atom2->ranges[j];
2556 res = xmlFACompareRanges(r1, r2);
2557 if (res == 1) {
2558 ret = 1;
2559 goto done;
2560 }
2561 }
2562 }
2563 ret = 0;
2564 }
2565 break;
Daniel Veillarde19fc232002-04-22 16:01:24 +00002566 default:
Daniel Veillard567a45b2005-10-18 19:11:55 +00002567 goto not_determinist;
Daniel Veillarde19fc232002-04-22 16:01:24 +00002568 }
Daniel Veillard567a45b2005-10-18 19:11:55 +00002569done:
Daniel Veillard6e65e152005-08-09 11:09:52 +00002570 if (atom1->neg != atom2->neg) {
Daniel Veillard9efc4762005-07-19 14:33:55 +00002571 ret = !ret;
Daniel Veillard6e65e152005-08-09 11:09:52 +00002572 }
Daniel Veillard567a45b2005-10-18 19:11:55 +00002573 if (ret == 0)
2574 return(0);
2575not_determinist:
2576 return(1);
Daniel Veillarde19fc232002-04-22 16:01:24 +00002577}
2578
2579/**
2580 * xmlFARecurseDeterminism:
2581 * @ctxt: a regexp parser context
2582 *
2583 * Check whether the associated regexp is determinist,
2584 * should be called after xmlFAEliminateEpsilonTransitions()
2585 *
2586 */
2587static int
2588xmlFARecurseDeterminism(xmlRegParserCtxtPtr ctxt, xmlRegStatePtr state,
2589 int to, xmlRegAtomPtr atom) {
2590 int ret = 1;
Daniel Veillard567a45b2005-10-18 19:11:55 +00002591 int res;
Daniel Veillard5de09382005-09-26 17:18:17 +00002592 int transnr, nbTrans;
Daniel Veillarde19fc232002-04-22 16:01:24 +00002593 xmlRegTransPtr t1;
Daniel Veillard1ba2aca2009-08-31 16:47:39 +02002594 int deep = 1;
Daniel Veillarde19fc232002-04-22 16:01:24 +00002595
2596 if (state == NULL)
2597 return(ret);
Daniel Veillard466fcda2012-08-27 12:03:40 +08002598 if (state->markd == XML_REGEXP_MARK_VISITED)
2599 return(ret);
Daniel Veillard1ba2aca2009-08-31 16:47:39 +02002600
2601 if (ctxt->flags & AM_AUTOMATA_RNG)
2602 deep = 0;
2603
Daniel Veillard5de09382005-09-26 17:18:17 +00002604 /*
2605 * don't recurse on transitions potentially added in the course of
2606 * the elimination.
2607 */
2608 nbTrans = state->nbTrans;
2609 for (transnr = 0;transnr < nbTrans;transnr++) {
Daniel Veillarde19fc232002-04-22 16:01:24 +00002610 t1 = &(state->trans[transnr]);
2611 /*
2612 * check transitions conflicting with the one looked at
2613 */
2614 if (t1->atom == NULL) {
Daniel Veillard0e05f4c2006-11-01 15:33:04 +00002615 if (t1->to < 0)
Daniel Veillarde19fc232002-04-22 16:01:24 +00002616 continue;
Daniel Veillard466fcda2012-08-27 12:03:40 +08002617 state->markd = XML_REGEXP_MARK_VISITED;
Daniel Veillard567a45b2005-10-18 19:11:55 +00002618 res = xmlFARecurseDeterminism(ctxt, ctxt->states[t1->to],
Daniel Veillarde19fc232002-04-22 16:01:24 +00002619 to, atom);
Daniel Veillard466fcda2012-08-27 12:03:40 +08002620 state->markd = 0;
Daniel Veillard567a45b2005-10-18 19:11:55 +00002621 if (res == 0) {
2622 ret = 0;
Daniel Veillardaa622012005-10-20 15:55:25 +00002623 /* t1->nd = 1; */
Daniel Veillard567a45b2005-10-18 19:11:55 +00002624 }
Daniel Veillarde19fc232002-04-22 16:01:24 +00002625 continue;
2626 }
2627 if (t1->to != to)
2628 continue;
Daniel Veillard1ba2aca2009-08-31 16:47:39 +02002629 if (xmlFACompareAtoms(t1->atom, atom, deep)) {
Daniel Veillard567a45b2005-10-18 19:11:55 +00002630 ret = 0;
2631 /* mark the transition as non-deterministic */
2632 t1->nd = 1;
2633 }
Daniel Veillarde19fc232002-04-22 16:01:24 +00002634 }
2635 return(ret);
2636}
2637
2638/**
2639 * xmlFAComputesDeterminism:
2640 * @ctxt: a regexp parser context
2641 *
2642 * Check whether the associated regexp is determinist,
2643 * should be called after xmlFAEliminateEpsilonTransitions()
2644 *
2645 */
2646static int
2647xmlFAComputesDeterminism(xmlRegParserCtxtPtr ctxt) {
2648 int statenr, transnr;
2649 xmlRegStatePtr state;
Daniel Veillard567a45b2005-10-18 19:11:55 +00002650 xmlRegTransPtr t1, t2, last;
Daniel Veillarde19fc232002-04-22 16:01:24 +00002651 int i;
2652 int ret = 1;
Daniel Veillard1ba2aca2009-08-31 16:47:39 +02002653 int deep = 1;
Daniel Veillarde19fc232002-04-22 16:01:24 +00002654
Daniel Veillard4402ab42002-09-12 16:02:56 +00002655#ifdef DEBUG_REGEXP_GRAPH
2656 printf("xmlFAComputesDeterminism\n");
2657 xmlRegPrintCtxt(stdout, ctxt);
2658#endif
Daniel Veillarde19fc232002-04-22 16:01:24 +00002659 if (ctxt->determinist != -1)
2660 return(ctxt->determinist);
2661
Daniel Veillard1ba2aca2009-08-31 16:47:39 +02002662 if (ctxt->flags & AM_AUTOMATA_RNG)
2663 deep = 0;
2664
Daniel Veillarde19fc232002-04-22 16:01:24 +00002665 /*
Daniel Veillard567a45b2005-10-18 19:11:55 +00002666 * First cleanup the automata removing cancelled transitions
Daniel Veillarde19fc232002-04-22 16:01:24 +00002667 */
2668 for (statenr = 0;statenr < ctxt->nbStates;statenr++) {
2669 state = ctxt->states[statenr];
2670 if (state == NULL)
2671 continue;
Daniel Veillard4f82c8a2005-08-09 21:40:08 +00002672 if (state->nbTrans < 2)
2673 continue;
Daniel Veillarde19fc232002-04-22 16:01:24 +00002674 for (transnr = 0;transnr < state->nbTrans;transnr++) {
2675 t1 = &(state->trans[transnr]);
2676 /*
2677 * Determinism checks in case of counted or all transitions
2678 * will have to be handled separately
2679 */
Daniel Veillard567a45b2005-10-18 19:11:55 +00002680 if (t1->atom == NULL) {
Daniel Veillardaa622012005-10-20 15:55:25 +00002681 /* t1->nd = 1; */
Daniel Veillarde19fc232002-04-22 16:01:24 +00002682 continue;
Daniel Veillard567a45b2005-10-18 19:11:55 +00002683 }
Daniel Veillarde19fc232002-04-22 16:01:24 +00002684 if (t1->to == -1) /* eliminated */
2685 continue;
2686 for (i = 0;i < transnr;i++) {
2687 t2 = &(state->trans[i]);
2688 if (t2->to == -1) /* eliminated */
2689 continue;
2690 if (t2->atom != NULL) {
2691 if (t1->to == t2->to) {
Daniel Veillard1ba2aca2009-08-31 16:47:39 +02002692 /*
2693 * Here we use deep because we want to keep the
2694 * transitions which indicate a conflict
2695 */
2696 if (xmlFAEqualAtoms(t1->atom, t2->atom, deep) &&
Daniel Veillard11e28e42009-08-12 12:21:42 +02002697 (t1->counter == t2->counter) &&
2698 (t1->count == t2->count))
William M. Brackddf71d62004-05-06 04:17:26 +00002699 t2->to = -1; /* eliminated */
Daniel Veillard567a45b2005-10-18 19:11:55 +00002700 }
2701 }
2702 }
2703 }
2704 }
2705
2706 /*
2707 * Check for all states that there aren't 2 transitions
2708 * with the same atom and a different target.
2709 */
2710 for (statenr = 0;statenr < ctxt->nbStates;statenr++) {
2711 state = ctxt->states[statenr];
2712 if (state == NULL)
2713 continue;
2714 if (state->nbTrans < 2)
2715 continue;
2716 last = NULL;
2717 for (transnr = 0;transnr < state->nbTrans;transnr++) {
2718 t1 = &(state->trans[transnr]);
2719 /*
2720 * Determinism checks in case of counted or all transitions
2721 * will have to be handled separately
2722 */
2723 if (t1->atom == NULL) {
2724 continue;
2725 }
2726 if (t1->to == -1) /* eliminated */
2727 continue;
2728 for (i = 0;i < transnr;i++) {
2729 t2 = &(state->trans[i]);
2730 if (t2->to == -1) /* eliminated */
2731 continue;
2732 if (t2->atom != NULL) {
Daniel Veillard1ba2aca2009-08-31 16:47:39 +02002733 /*
2734 * But here we don't use deep because we want to
2735 * find transitions which indicate a conflict
2736 */
2737 if (xmlFACompareAtoms(t1->atom, t2->atom, 1)) {
Daniel Veillard567a45b2005-10-18 19:11:55 +00002738 ret = 0;
2739 /* mark the transitions as non-deterministic ones */
2740 t1->nd = 1;
2741 t2->nd = 1;
2742 last = t1;
Daniel Veillarde19fc232002-04-22 16:01:24 +00002743 }
2744 } else if (t1->to != -1) {
2745 /*
2746 * do the closure in case of remaining specific
2747 * epsilon transitions like choices or all
2748 */
2749 ret = xmlFARecurseDeterminism(ctxt, ctxt->states[t1->to],
2750 t2->to, t2->atom);
Daniel Veillard567a45b2005-10-18 19:11:55 +00002751 /* don't shortcut the computation so all non deterministic
2752 transition get marked down
Daniel Veillarde19fc232002-04-22 16:01:24 +00002753 if (ret == 0)
Daniel Veillardaa622012005-10-20 15:55:25 +00002754 return(0);
2755 */
Daniel Veillard567a45b2005-10-18 19:11:55 +00002756 if (ret == 0) {
2757 t1->nd = 1;
Daniel Veillardaa622012005-10-20 15:55:25 +00002758 /* t2->nd = 1; */
Daniel Veillard567a45b2005-10-18 19:11:55 +00002759 last = t1;
2760 }
Daniel Veillarde19fc232002-04-22 16:01:24 +00002761 }
2762 }
Daniel Veillard567a45b2005-10-18 19:11:55 +00002763 /* don't shortcut the computation so all non deterministic
2764 transition get marked down
Daniel Veillarde19fc232002-04-22 16:01:24 +00002765 if (ret == 0)
Daniel Veillard567a45b2005-10-18 19:11:55 +00002766 break; */
Daniel Veillarde19fc232002-04-22 16:01:24 +00002767 }
Daniel Veillard567a45b2005-10-18 19:11:55 +00002768
2769 /*
2770 * mark specifically the last non-deterministic transition
2771 * from a state since there is no need to set-up rollback
2772 * from it
2773 */
2774 if (last != NULL) {
2775 last->nd = 2;
2776 }
2777
2778 /* don't shortcut the computation so all non deterministic
2779 transition get marked down
Daniel Veillarde19fc232002-04-22 16:01:24 +00002780 if (ret == 0)
Daniel Veillard567a45b2005-10-18 19:11:55 +00002781 break; */
Daniel Veillarde19fc232002-04-22 16:01:24 +00002782 }
Daniel Veillard567a45b2005-10-18 19:11:55 +00002783
Daniel Veillarde19fc232002-04-22 16:01:24 +00002784 ctxt->determinist = ret;
2785 return(ret);
2786}
2787
Daniel Veillard4255d502002-04-16 15:50:10 +00002788/************************************************************************
Daniel Veillardf8e3db02012-09-11 13:26:36 +08002789 * *
Daniel Veillard4255d502002-04-16 15:50:10 +00002790 * Routines to check input against transition atoms *
Daniel Veillardf8e3db02012-09-11 13:26:36 +08002791 * *
Daniel Veillard4255d502002-04-16 15:50:10 +00002792 ************************************************************************/
2793
2794static int
2795xmlRegCheckCharacterRange(xmlRegAtomType type, int codepoint, int neg,
2796 int start, int end, const xmlChar *blockName) {
2797 int ret = 0;
2798
2799 switch (type) {
2800 case XML_REGEXP_STRING:
2801 case XML_REGEXP_SUBREG:
2802 case XML_REGEXP_RANGES:
2803 case XML_REGEXP_EPSILON:
2804 return(-1);
2805 case XML_REGEXP_ANYCHAR:
2806 ret = ((codepoint != '\n') && (codepoint != '\r'));
2807 break;
2808 case XML_REGEXP_CHARVAL:
2809 ret = ((codepoint >= start) && (codepoint <= end));
2810 break;
2811 case XML_REGEXP_NOTSPACE:
2812 neg = !neg;
2813 case XML_REGEXP_ANYSPACE:
2814 ret = ((codepoint == '\n') || (codepoint == '\r') ||
2815 (codepoint == '\t') || (codepoint == ' '));
2816 break;
2817 case XML_REGEXP_NOTINITNAME:
2818 neg = !neg;
2819 case XML_REGEXP_INITNAME:
Daniel Veillardf8e3db02012-09-11 13:26:36 +08002820 ret = (IS_LETTER(codepoint) ||
Daniel Veillard4255d502002-04-16 15:50:10 +00002821 (codepoint == '_') || (codepoint == ':'));
2822 break;
2823 case XML_REGEXP_NOTNAMECHAR:
2824 neg = !neg;
2825 case XML_REGEXP_NAMECHAR:
William M. Brack871611b2003-10-18 04:53:14 +00002826 ret = (IS_LETTER(codepoint) || IS_DIGIT(codepoint) ||
Daniel Veillard4255d502002-04-16 15:50:10 +00002827 (codepoint == '.') || (codepoint == '-') ||
2828 (codepoint == '_') || (codepoint == ':') ||
William M. Brack871611b2003-10-18 04:53:14 +00002829 IS_COMBINING(codepoint) || IS_EXTENDER(codepoint));
Daniel Veillard4255d502002-04-16 15:50:10 +00002830 break;
2831 case XML_REGEXP_NOTDECIMAL:
2832 neg = !neg;
2833 case XML_REGEXP_DECIMAL:
2834 ret = xmlUCSIsCatNd(codepoint);
2835 break;
2836 case XML_REGEXP_REALCHAR:
2837 neg = !neg;
2838 case XML_REGEXP_NOTREALCHAR:
2839 ret = xmlUCSIsCatP(codepoint);
2840 if (ret == 0)
2841 ret = xmlUCSIsCatZ(codepoint);
2842 if (ret == 0)
2843 ret = xmlUCSIsCatC(codepoint);
2844 break;
2845 case XML_REGEXP_LETTER:
2846 ret = xmlUCSIsCatL(codepoint);
2847 break;
2848 case XML_REGEXP_LETTER_UPPERCASE:
2849 ret = xmlUCSIsCatLu(codepoint);
2850 break;
2851 case XML_REGEXP_LETTER_LOWERCASE:
2852 ret = xmlUCSIsCatLl(codepoint);
2853 break;
2854 case XML_REGEXP_LETTER_TITLECASE:
2855 ret = xmlUCSIsCatLt(codepoint);
2856 break;
2857 case XML_REGEXP_LETTER_MODIFIER:
2858 ret = xmlUCSIsCatLm(codepoint);
2859 break;
2860 case XML_REGEXP_LETTER_OTHERS:
2861 ret = xmlUCSIsCatLo(codepoint);
2862 break;
2863 case XML_REGEXP_MARK:
2864 ret = xmlUCSIsCatM(codepoint);
2865 break;
2866 case XML_REGEXP_MARK_NONSPACING:
2867 ret = xmlUCSIsCatMn(codepoint);
2868 break;
2869 case XML_REGEXP_MARK_SPACECOMBINING:
2870 ret = xmlUCSIsCatMc(codepoint);
2871 break;
2872 case XML_REGEXP_MARK_ENCLOSING:
2873 ret = xmlUCSIsCatMe(codepoint);
2874 break;
2875 case XML_REGEXP_NUMBER:
2876 ret = xmlUCSIsCatN(codepoint);
2877 break;
2878 case XML_REGEXP_NUMBER_DECIMAL:
2879 ret = xmlUCSIsCatNd(codepoint);
2880 break;
2881 case XML_REGEXP_NUMBER_LETTER:
2882 ret = xmlUCSIsCatNl(codepoint);
2883 break;
2884 case XML_REGEXP_NUMBER_OTHERS:
2885 ret = xmlUCSIsCatNo(codepoint);
2886 break;
2887 case XML_REGEXP_PUNCT:
2888 ret = xmlUCSIsCatP(codepoint);
2889 break;
2890 case XML_REGEXP_PUNCT_CONNECTOR:
2891 ret = xmlUCSIsCatPc(codepoint);
2892 break;
2893 case XML_REGEXP_PUNCT_DASH:
2894 ret = xmlUCSIsCatPd(codepoint);
2895 break;
2896 case XML_REGEXP_PUNCT_OPEN:
2897 ret = xmlUCSIsCatPs(codepoint);
2898 break;
2899 case XML_REGEXP_PUNCT_CLOSE:
2900 ret = xmlUCSIsCatPe(codepoint);
2901 break;
2902 case XML_REGEXP_PUNCT_INITQUOTE:
2903 ret = xmlUCSIsCatPi(codepoint);
2904 break;
2905 case XML_REGEXP_PUNCT_FINQUOTE:
2906 ret = xmlUCSIsCatPf(codepoint);
2907 break;
2908 case XML_REGEXP_PUNCT_OTHERS:
2909 ret = xmlUCSIsCatPo(codepoint);
2910 break;
2911 case XML_REGEXP_SEPAR:
2912 ret = xmlUCSIsCatZ(codepoint);
2913 break;
2914 case XML_REGEXP_SEPAR_SPACE:
2915 ret = xmlUCSIsCatZs(codepoint);
2916 break;
2917 case XML_REGEXP_SEPAR_LINE:
2918 ret = xmlUCSIsCatZl(codepoint);
2919 break;
2920 case XML_REGEXP_SEPAR_PARA:
2921 ret = xmlUCSIsCatZp(codepoint);
2922 break;
2923 case XML_REGEXP_SYMBOL:
2924 ret = xmlUCSIsCatS(codepoint);
2925 break;
2926 case XML_REGEXP_SYMBOL_MATH:
2927 ret = xmlUCSIsCatSm(codepoint);
2928 break;
2929 case XML_REGEXP_SYMBOL_CURRENCY:
2930 ret = xmlUCSIsCatSc(codepoint);
2931 break;
2932 case XML_REGEXP_SYMBOL_MODIFIER:
2933 ret = xmlUCSIsCatSk(codepoint);
2934 break;
2935 case XML_REGEXP_SYMBOL_OTHERS:
2936 ret = xmlUCSIsCatSo(codepoint);
2937 break;
2938 case XML_REGEXP_OTHER:
2939 ret = xmlUCSIsCatC(codepoint);
2940 break;
2941 case XML_REGEXP_OTHER_CONTROL:
2942 ret = xmlUCSIsCatCc(codepoint);
2943 break;
2944 case XML_REGEXP_OTHER_FORMAT:
2945 ret = xmlUCSIsCatCf(codepoint);
2946 break;
2947 case XML_REGEXP_OTHER_PRIVATE:
2948 ret = xmlUCSIsCatCo(codepoint);
2949 break;
2950 case XML_REGEXP_OTHER_NA:
2951 /* ret = xmlUCSIsCatCn(codepoint); */
2952 /* Seems it doesn't exist anymore in recent Unicode releases */
2953 ret = 0;
2954 break;
2955 case XML_REGEXP_BLOCK_NAME:
2956 ret = xmlUCSIsBlock(codepoint, (const char *) blockName);
2957 break;
2958 }
2959 if (neg)
2960 return(!ret);
2961 return(ret);
2962}
2963
2964static int
2965xmlRegCheckCharacter(xmlRegAtomPtr atom, int codepoint) {
2966 int i, ret = 0;
2967 xmlRegRangePtr range;
2968
William M. Brack871611b2003-10-18 04:53:14 +00002969 if ((atom == NULL) || (!IS_CHAR(codepoint)))
Daniel Veillard4255d502002-04-16 15:50:10 +00002970 return(-1);
2971
2972 switch (atom->type) {
2973 case XML_REGEXP_SUBREG:
2974 case XML_REGEXP_EPSILON:
2975 return(-1);
2976 case XML_REGEXP_CHARVAL:
2977 return(codepoint == atom->codepoint);
2978 case XML_REGEXP_RANGES: {
2979 int accept = 0;
Daniel Veillardf2a12832003-11-24 13:04:35 +00002980
Daniel Veillard4255d502002-04-16 15:50:10 +00002981 for (i = 0;i < atom->nbRanges;i++) {
2982 range = atom->ranges[i];
Daniel Veillardf8b9de32003-11-24 14:27:26 +00002983 if (range->neg == 2) {
Daniel Veillard4255d502002-04-16 15:50:10 +00002984 ret = xmlRegCheckCharacterRange(range->type, codepoint,
2985 0, range->start, range->end,
2986 range->blockName);
2987 if (ret != 0)
2988 return(0); /* excluded char */
Daniel Veillardf8b9de32003-11-24 14:27:26 +00002989 } else if (range->neg) {
2990 ret = xmlRegCheckCharacterRange(range->type, codepoint,
2991 0, range->start, range->end,
2992 range->blockName);
2993 if (ret == 0)
Daniel Veillardf2a12832003-11-24 13:04:35 +00002994 accept = 1;
Daniel Veillardf8b9de32003-11-24 14:27:26 +00002995 else
2996 return(0);
Daniel Veillard4255d502002-04-16 15:50:10 +00002997 } else {
2998 ret = xmlRegCheckCharacterRange(range->type, codepoint,
2999 0, range->start, range->end,
3000 range->blockName);
3001 if (ret != 0)
3002 accept = 1; /* might still be excluded */
3003 }
3004 }
3005 return(accept);
3006 }
3007 case XML_REGEXP_STRING:
3008 printf("TODO: XML_REGEXP_STRING\n");
3009 return(-1);
3010 case XML_REGEXP_ANYCHAR:
3011 case XML_REGEXP_ANYSPACE:
3012 case XML_REGEXP_NOTSPACE:
3013 case XML_REGEXP_INITNAME:
3014 case XML_REGEXP_NOTINITNAME:
3015 case XML_REGEXP_NAMECHAR:
3016 case XML_REGEXP_NOTNAMECHAR:
3017 case XML_REGEXP_DECIMAL:
3018 case XML_REGEXP_NOTDECIMAL:
3019 case XML_REGEXP_REALCHAR:
3020 case XML_REGEXP_NOTREALCHAR:
3021 case XML_REGEXP_LETTER:
3022 case XML_REGEXP_LETTER_UPPERCASE:
3023 case XML_REGEXP_LETTER_LOWERCASE:
3024 case XML_REGEXP_LETTER_TITLECASE:
3025 case XML_REGEXP_LETTER_MODIFIER:
3026 case XML_REGEXP_LETTER_OTHERS:
3027 case XML_REGEXP_MARK:
3028 case XML_REGEXP_MARK_NONSPACING:
3029 case XML_REGEXP_MARK_SPACECOMBINING:
3030 case XML_REGEXP_MARK_ENCLOSING:
3031 case XML_REGEXP_NUMBER:
3032 case XML_REGEXP_NUMBER_DECIMAL:
3033 case XML_REGEXP_NUMBER_LETTER:
3034 case XML_REGEXP_NUMBER_OTHERS:
3035 case XML_REGEXP_PUNCT:
3036 case XML_REGEXP_PUNCT_CONNECTOR:
3037 case XML_REGEXP_PUNCT_DASH:
3038 case XML_REGEXP_PUNCT_OPEN:
3039 case XML_REGEXP_PUNCT_CLOSE:
3040 case XML_REGEXP_PUNCT_INITQUOTE:
3041 case XML_REGEXP_PUNCT_FINQUOTE:
3042 case XML_REGEXP_PUNCT_OTHERS:
3043 case XML_REGEXP_SEPAR:
3044 case XML_REGEXP_SEPAR_SPACE:
3045 case XML_REGEXP_SEPAR_LINE:
3046 case XML_REGEXP_SEPAR_PARA:
3047 case XML_REGEXP_SYMBOL:
3048 case XML_REGEXP_SYMBOL_MATH:
3049 case XML_REGEXP_SYMBOL_CURRENCY:
3050 case XML_REGEXP_SYMBOL_MODIFIER:
3051 case XML_REGEXP_SYMBOL_OTHERS:
3052 case XML_REGEXP_OTHER:
3053 case XML_REGEXP_OTHER_CONTROL:
3054 case XML_REGEXP_OTHER_FORMAT:
3055 case XML_REGEXP_OTHER_PRIVATE:
3056 case XML_REGEXP_OTHER_NA:
3057 case XML_REGEXP_BLOCK_NAME:
3058 ret = xmlRegCheckCharacterRange(atom->type, codepoint, 0, 0, 0,
3059 (const xmlChar *)atom->valuep);
3060 if (atom->neg)
3061 ret = !ret;
3062 break;
3063 }
3064 return(ret);
3065}
3066
3067/************************************************************************
Daniel Veillardf8e3db02012-09-11 13:26:36 +08003068 * *
William M. Brackddf71d62004-05-06 04:17:26 +00003069 * Saving and restoring state of an execution context *
Daniel Veillardf8e3db02012-09-11 13:26:36 +08003070 * *
Daniel Veillard4255d502002-04-16 15:50:10 +00003071 ************************************************************************/
3072
3073#ifdef DEBUG_REGEXP_EXEC
3074static void
3075xmlFARegDebugExec(xmlRegExecCtxtPtr exec) {
3076 printf("state: %d:%d:idx %d", exec->state->no, exec->transno, exec->index);
3077 if (exec->inputStack != NULL) {
3078 int i;
3079 printf(": ");
3080 for (i = 0;(i < 3) && (i < exec->inputStackNr);i++)
Daniel Veillard0e05f4c2006-11-01 15:33:04 +00003081 printf("%s ", (const char *)
3082 exec->inputStack[exec->inputStackNr - (i + 1)].value);
Daniel Veillard4255d502002-04-16 15:50:10 +00003083 } else {
3084 printf(": %s", &(exec->inputString[exec->index]));
3085 }
3086 printf("\n");
3087}
3088#endif
3089
3090static void
3091xmlFARegExecSave(xmlRegExecCtxtPtr exec) {
3092#ifdef DEBUG_REGEXP_EXEC
3093 printf("saving ");
3094 exec->transno++;
3095 xmlFARegDebugExec(exec);
3096 exec->transno--;
3097#endif
Daniel Veillard94cc1032005-09-15 13:09:00 +00003098#ifdef MAX_PUSH
3099 if (exec->nbPush > MAX_PUSH) {
3100 return;
3101 }
3102 exec->nbPush++;
3103#endif
Daniel Veillard4255d502002-04-16 15:50:10 +00003104
3105 if (exec->maxRollbacks == 0) {
3106 exec->maxRollbacks = 4;
3107 exec->rollbacks = (xmlRegExecRollback *) xmlMalloc(exec->maxRollbacks *
3108 sizeof(xmlRegExecRollback));
3109 if (exec->rollbacks == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00003110 xmlRegexpErrMemory(NULL, "saving regexp");
Daniel Veillard4255d502002-04-16 15:50:10 +00003111 exec->maxRollbacks = 0;
3112 return;
3113 }
3114 memset(exec->rollbacks, 0,
3115 exec->maxRollbacks * sizeof(xmlRegExecRollback));
3116 } else if (exec->nbRollbacks >= exec->maxRollbacks) {
3117 xmlRegExecRollback *tmp;
3118 int len = exec->maxRollbacks;
3119
3120 exec->maxRollbacks *= 2;
3121 tmp = (xmlRegExecRollback *) xmlRealloc(exec->rollbacks,
3122 exec->maxRollbacks * sizeof(xmlRegExecRollback));
3123 if (tmp == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00003124 xmlRegexpErrMemory(NULL, "saving regexp");
Daniel Veillard4255d502002-04-16 15:50:10 +00003125 exec->maxRollbacks /= 2;
3126 return;
3127 }
3128 exec->rollbacks = tmp;
3129 tmp = &exec->rollbacks[len];
3130 memset(tmp, 0, (exec->maxRollbacks - len) * sizeof(xmlRegExecRollback));
3131 }
3132 exec->rollbacks[exec->nbRollbacks].state = exec->state;
3133 exec->rollbacks[exec->nbRollbacks].index = exec->index;
3134 exec->rollbacks[exec->nbRollbacks].nextbranch = exec->transno + 1;
3135 if (exec->comp->nbCounters > 0) {
3136 if (exec->rollbacks[exec->nbRollbacks].counts == NULL) {
3137 exec->rollbacks[exec->nbRollbacks].counts = (int *)
3138 xmlMalloc(exec->comp->nbCounters * sizeof(int));
3139 if (exec->rollbacks[exec->nbRollbacks].counts == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00003140 xmlRegexpErrMemory(NULL, "saving regexp");
Daniel Veillard4255d502002-04-16 15:50:10 +00003141 exec->status = -5;
3142 return;
3143 }
3144 }
3145 memcpy(exec->rollbacks[exec->nbRollbacks].counts, exec->counts,
3146 exec->comp->nbCounters * sizeof(int));
3147 }
3148 exec->nbRollbacks++;
3149}
3150
3151static void
3152xmlFARegExecRollBack(xmlRegExecCtxtPtr exec) {
3153 if (exec->nbRollbacks <= 0) {
3154 exec->status = -1;
3155#ifdef DEBUG_REGEXP_EXEC
3156 printf("rollback failed on empty stack\n");
3157#endif
3158 return;
3159 }
3160 exec->nbRollbacks--;
3161 exec->state = exec->rollbacks[exec->nbRollbacks].state;
3162 exec->index = exec->rollbacks[exec->nbRollbacks].index;
3163 exec->transno = exec->rollbacks[exec->nbRollbacks].nextbranch;
3164 if (exec->comp->nbCounters > 0) {
3165 if (exec->rollbacks[exec->nbRollbacks].counts == NULL) {
3166 fprintf(stderr, "exec save: allocation failed");
3167 exec->status = -6;
3168 return;
3169 }
Gaurav2671b012013-09-11 14:59:06 +08003170 if (exec->counts) {
3171 memcpy(exec->counts, exec->rollbacks[exec->nbRollbacks].counts,
Daniel Veillard4255d502002-04-16 15:50:10 +00003172 exec->comp->nbCounters * sizeof(int));
Gaurav2671b012013-09-11 14:59:06 +08003173 }
Daniel Veillard4255d502002-04-16 15:50:10 +00003174 }
3175
3176#ifdef DEBUG_REGEXP_EXEC
3177 printf("restored ");
3178 xmlFARegDebugExec(exec);
3179#endif
3180}
3181
3182/************************************************************************
Daniel Veillardf8e3db02012-09-11 13:26:36 +08003183 * *
William M. Brackddf71d62004-05-06 04:17:26 +00003184 * Verifier, running an input against a compiled regexp *
Daniel Veillardf8e3db02012-09-11 13:26:36 +08003185 * *
Daniel Veillard4255d502002-04-16 15:50:10 +00003186 ************************************************************************/
3187
3188static int
3189xmlFARegExec(xmlRegexpPtr comp, const xmlChar *content) {
3190 xmlRegExecCtxt execval;
3191 xmlRegExecCtxtPtr exec = &execval;
Daniel Veillard567a45b2005-10-18 19:11:55 +00003192 int ret, codepoint = 0, len, deter;
Daniel Veillard4255d502002-04-16 15:50:10 +00003193
3194 exec->inputString = content;
3195 exec->index = 0;
Daniel Veillard94cc1032005-09-15 13:09:00 +00003196 exec->nbPush = 0;
Daniel Veillard4255d502002-04-16 15:50:10 +00003197 exec->determinist = 1;
3198 exec->maxRollbacks = 0;
3199 exec->nbRollbacks = 0;
3200 exec->rollbacks = NULL;
3201 exec->status = 0;
3202 exec->comp = comp;
3203 exec->state = comp->states[0];
3204 exec->transno = 0;
3205 exec->transcount = 0;
Daniel Veillardf2a12832003-11-24 13:04:35 +00003206 exec->inputStack = NULL;
3207 exec->inputStackMax = 0;
Daniel Veillard4255d502002-04-16 15:50:10 +00003208 if (comp->nbCounters > 0) {
3209 exec->counts = (int *) xmlMalloc(comp->nbCounters * sizeof(int));
Daniel Veillardff46a042003-10-08 08:53:17 +00003210 if (exec->counts == NULL) {
3211 xmlRegexpErrMemory(NULL, "running regexp");
Daniel Veillard4255d502002-04-16 15:50:10 +00003212 return(-1);
Daniel Veillardff46a042003-10-08 08:53:17 +00003213 }
Daniel Veillard4255d502002-04-16 15:50:10 +00003214 memset(exec->counts, 0, comp->nbCounters * sizeof(int));
3215 } else
3216 exec->counts = NULL;
Daniel Veillard40851d02012-08-17 20:34:05 +08003217 while ((exec->status == 0) && (exec->state != NULL) &&
Daniel Veillard4255d502002-04-16 15:50:10 +00003218 ((exec->inputString[exec->index] != 0) ||
Daniel Veillardad559982008-05-12 13:15:35 +00003219 ((exec->state != NULL) &&
3220 (exec->state->type != XML_REGEXP_FINAL_STATE)))) {
Daniel Veillard4255d502002-04-16 15:50:10 +00003221 xmlRegTransPtr trans;
3222 xmlRegAtomPtr atom;
3223
3224 /*
William M. Brack0e00b282004-04-26 15:40:47 +00003225 * If end of input on non-terminal state, rollback, however we may
Daniel Veillard4255d502002-04-16 15:50:10 +00003226 * still have epsilon like transition for counted transitions
William M. Brack0e00b282004-04-26 15:40:47 +00003227 * on counters, in that case don't break too early. Additionally,
3228 * if we are working on a range like "AB{0,2}", where B is not present,
3229 * we don't want to break.
Daniel Veillard4255d502002-04-16 15:50:10 +00003230 */
Daniel Veillard11ce4002006-03-10 00:36:23 +00003231 len = 1;
William M. Brack0e00b282004-04-26 15:40:47 +00003232 if ((exec->inputString[exec->index] == 0) && (exec->counts == NULL)) {
William M. Brackddf71d62004-05-06 04:17:26 +00003233 /*
3234 * if there is a transition, we must check if
3235 * atom allows minOccurs of 0
3236 */
3237 if (exec->transno < exec->state->nbTrans) {
William M. Brack0e00b282004-04-26 15:40:47 +00003238 trans = &exec->state->trans[exec->transno];
3239 if (trans->to >=0) {
3240 atom = trans->atom;
3241 if (!((atom->min == 0) && (atom->max > 0)))
3242 goto rollback;
3243 }
3244 } else
3245 goto rollback;
3246 }
Daniel Veillard4255d502002-04-16 15:50:10 +00003247
3248 exec->transcount = 0;
3249 for (;exec->transno < exec->state->nbTrans;exec->transno++) {
3250 trans = &exec->state->trans[exec->transno];
3251 if (trans->to < 0)
3252 continue;
3253 atom = trans->atom;
3254 ret = 0;
Daniel Veillard567a45b2005-10-18 19:11:55 +00003255 deter = 1;
Daniel Veillard4255d502002-04-16 15:50:10 +00003256 if (trans->count >= 0) {
3257 int count;
3258 xmlRegCounterPtr counter;
3259
Daniel Veillard11ce4002006-03-10 00:36:23 +00003260 if (exec->counts == NULL) {
3261 exec->status = -1;
3262 goto error;
3263 }
Daniel Veillard4255d502002-04-16 15:50:10 +00003264 /*
3265 * A counted transition.
3266 */
3267
3268 count = exec->counts[trans->count];
3269 counter = &exec->comp->counters[trans->count];
3270#ifdef DEBUG_REGEXP_EXEC
3271 printf("testing count %d: val %d, min %d, max %d\n",
3272 trans->count, count, counter->min, counter->max);
3273#endif
3274 ret = ((count >= counter->min) && (count <= counter->max));
Daniel Veillard567a45b2005-10-18 19:11:55 +00003275 if ((ret) && (counter->min != counter->max))
3276 deter = 0;
Daniel Veillard4255d502002-04-16 15:50:10 +00003277 } else if (atom == NULL) {
3278 fprintf(stderr, "epsilon transition left at runtime\n");
3279 exec->status = -2;
3280 break;
3281 } else if (exec->inputString[exec->index] != 0) {
3282 codepoint = CUR_SCHAR(&(exec->inputString[exec->index]), len);
3283 ret = xmlRegCheckCharacter(atom, codepoint);
William M. Brack0e00b282004-04-26 15:40:47 +00003284 if ((ret == 1) && (atom->min >= 0) && (atom->max > 0)) {
Daniel Veillard4255d502002-04-16 15:50:10 +00003285 xmlRegStatePtr to = comp->states[trans->to];
3286
3287 /*
3288 * this is a multiple input sequence
Daniel Veillardfc6eca02005-11-01 15:24:02 +00003289 * If there is a counter associated increment it now.
3290 * before potentially saving and rollback
Daniel Veillardc821e032007-08-28 17:33:45 +00003291 * do not increment if the counter is already over the
3292 * maximum limit in which case get to next transition
Daniel Veillard4255d502002-04-16 15:50:10 +00003293 */
Daniel Veillardfc6eca02005-11-01 15:24:02 +00003294 if (trans->counter >= 0) {
Daniel Veillardc821e032007-08-28 17:33:45 +00003295 xmlRegCounterPtr counter;
3296
3297 if ((exec->counts == NULL) ||
3298 (exec->comp == NULL) ||
3299 (exec->comp->counters == NULL)) {
Daniel Veillard11ce4002006-03-10 00:36:23 +00003300 exec->status = -1;
3301 goto error;
3302 }
Daniel Veillardc821e032007-08-28 17:33:45 +00003303 counter = &exec->comp->counters[trans->counter];
3304 if (exec->counts[trans->counter] >= counter->max)
3305 continue; /* for loop on transitions */
3306
Daniel Veillardfc6eca02005-11-01 15:24:02 +00003307#ifdef DEBUG_REGEXP_EXEC
3308 printf("Increasing count %d\n", trans->counter);
3309#endif
3310 exec->counts[trans->counter]++;
3311 }
Daniel Veillard4255d502002-04-16 15:50:10 +00003312 if (exec->state->nbTrans > exec->transno + 1) {
3313 xmlFARegExecSave(exec);
3314 }
3315 exec->transcount = 1;
3316 do {
3317 /*
3318 * Try to progress as much as possible on the input
3319 */
3320 if (exec->transcount == atom->max) {
3321 break;
3322 }
3323 exec->index += len;
3324 /*
3325 * End of input: stop here
3326 */
3327 if (exec->inputString[exec->index] == 0) {
3328 exec->index -= len;
3329 break;
3330 }
3331 if (exec->transcount >= atom->min) {
3332 int transno = exec->transno;
3333 xmlRegStatePtr state = exec->state;
3334
3335 /*
3336 * The transition is acceptable save it
3337 */
3338 exec->transno = -1; /* trick */
3339 exec->state = to;
3340 xmlFARegExecSave(exec);
3341 exec->transno = transno;
3342 exec->state = state;
3343 }
3344 codepoint = CUR_SCHAR(&(exec->inputString[exec->index]),
3345 len);
3346 ret = xmlRegCheckCharacter(atom, codepoint);
3347 exec->transcount++;
3348 } while (ret == 1);
3349 if (exec->transcount < atom->min)
3350 ret = 0;
3351
3352 /*
3353 * If the last check failed but one transition was found
3354 * possible, rollback
3355 */
3356 if (ret < 0)
3357 ret = 0;
3358 if (ret == 0) {
3359 goto rollback;
3360 }
Daniel Veillardfc6eca02005-11-01 15:24:02 +00003361 if (trans->counter >= 0) {
Daniel Veillard11ce4002006-03-10 00:36:23 +00003362 if (exec->counts == NULL) {
3363 exec->status = -1;
3364 goto error;
3365 }
Daniel Veillardfc6eca02005-11-01 15:24:02 +00003366#ifdef DEBUG_REGEXP_EXEC
3367 printf("Decreasing count %d\n", trans->counter);
3368#endif
3369 exec->counts[trans->counter]--;
3370 }
William M. Brack0e00b282004-04-26 15:40:47 +00003371 } else if ((ret == 0) && (atom->min == 0) && (atom->max > 0)) {
3372 /*
3373 * we don't match on the codepoint, but minOccurs of 0
3374 * says that's ok. Setting len to 0 inhibits stepping
3375 * over the codepoint.
3376 */
3377 exec->transcount = 1;
3378 len = 0;
3379 ret = 1;
Daniel Veillard4255d502002-04-16 15:50:10 +00003380 }
William M. Brack0e00b282004-04-26 15:40:47 +00003381 } else if ((atom->min == 0) && (atom->max > 0)) {
3382 /* another spot to match when minOccurs is 0 */
3383 exec->transcount = 1;
3384 len = 0;
3385 ret = 1;
Daniel Veillard4255d502002-04-16 15:50:10 +00003386 }
3387 if (ret == 1) {
Daniel Veillard567a45b2005-10-18 19:11:55 +00003388 if ((trans->nd == 1) ||
3389 ((trans->count >= 0) && (deter == 0) &&
3390 (exec->state->nbTrans > exec->transno + 1))) {
Daniel Veillardaa622012005-10-20 15:55:25 +00003391#ifdef DEBUG_REGEXP_EXEC
3392 if (trans->nd == 1)
3393 printf("Saving on nd transition atom %d for %c at %d\n",
3394 trans->atom->no, codepoint, exec->index);
3395 else
3396 printf("Saving on counted transition count %d for %c at %d\n",
3397 trans->count, codepoint, exec->index);
3398#endif
Daniel Veillard4255d502002-04-16 15:50:10 +00003399 xmlFARegExecSave(exec);
3400 }
3401 if (trans->counter >= 0) {
Daniel Veillardc821e032007-08-28 17:33:45 +00003402 xmlRegCounterPtr counter;
3403
3404 /* make sure we don't go over the counter maximum value */
3405 if ((exec->counts == NULL) ||
3406 (exec->comp == NULL) ||
3407 (exec->comp->counters == NULL)) {
3408 exec->status = -1;
Daniel Veillard11ce4002006-03-10 00:36:23 +00003409 goto error;
3410 }
Daniel Veillardc821e032007-08-28 17:33:45 +00003411 counter = &exec->comp->counters[trans->counter];
3412 if (exec->counts[trans->counter] >= counter->max)
3413 continue; /* for loop on transitions */
Daniel Veillard4255d502002-04-16 15:50:10 +00003414#ifdef DEBUG_REGEXP_EXEC
3415 printf("Increasing count %d\n", trans->counter);
3416#endif
3417 exec->counts[trans->counter]++;
3418 }
Daniel Veillard10752282005-08-08 13:05:13 +00003419 if ((trans->count >= 0) &&
3420 (trans->count < REGEXP_ALL_COUNTER)) {
Daniel Veillard11ce4002006-03-10 00:36:23 +00003421 if (exec->counts == NULL) {
3422 exec->status = -1;
3423 goto error;
3424 }
Daniel Veillard10752282005-08-08 13:05:13 +00003425#ifdef DEBUG_REGEXP_EXEC
3426 printf("resetting count %d on transition\n",
3427 trans->count);
3428#endif
3429 exec->counts[trans->count] = 0;
3430 }
Daniel Veillard4255d502002-04-16 15:50:10 +00003431#ifdef DEBUG_REGEXP_EXEC
3432 printf("entering state %d\n", trans->to);
3433#endif
3434 exec->state = comp->states[trans->to];
3435 exec->transno = 0;
3436 if (trans->atom != NULL) {
3437 exec->index += len;
3438 }
3439 goto progress;
3440 } else if (ret < 0) {
3441 exec->status = -4;
3442 break;
3443 }
3444 }
3445 if ((exec->transno != 0) || (exec->state->nbTrans == 0)) {
3446rollback:
3447 /*
3448 * Failed to find a way out
3449 */
3450 exec->determinist = 0;
Daniel Veillardaa622012005-10-20 15:55:25 +00003451#ifdef DEBUG_REGEXP_EXEC
3452 printf("rollback from state %d on %d:%c\n", exec->state->no,
3453 codepoint,codepoint);
3454#endif
Daniel Veillard4255d502002-04-16 15:50:10 +00003455 xmlFARegExecRollBack(exec);
3456 }
3457progress:
3458 continue;
3459 }
Daniel Veillard11ce4002006-03-10 00:36:23 +00003460error:
Daniel Veillard4255d502002-04-16 15:50:10 +00003461 if (exec->rollbacks != NULL) {
3462 if (exec->counts != NULL) {
3463 int i;
3464
3465 for (i = 0;i < exec->maxRollbacks;i++)
3466 if (exec->rollbacks[i].counts != NULL)
3467 xmlFree(exec->rollbacks[i].counts);
3468 }
3469 xmlFree(exec->rollbacks);
3470 }
Daniel Veillard40851d02012-08-17 20:34:05 +08003471 if (exec->state == NULL)
3472 return(-1);
Daniel Veillard4255d502002-04-16 15:50:10 +00003473 if (exec->counts != NULL)
3474 xmlFree(exec->counts);
3475 if (exec->status == 0)
3476 return(1);
Daniel Veillard94cc1032005-09-15 13:09:00 +00003477 if (exec->status == -1) {
3478 if (exec->nbPush > MAX_PUSH)
3479 return(-1);
Daniel Veillard4255d502002-04-16 15:50:10 +00003480 return(0);
Daniel Veillard94cc1032005-09-15 13:09:00 +00003481 }
Daniel Veillard4255d502002-04-16 15:50:10 +00003482 return(exec->status);
3483}
3484
3485/************************************************************************
Daniel Veillardf8e3db02012-09-11 13:26:36 +08003486 * *
William M. Brackddf71d62004-05-06 04:17:26 +00003487 * Progressive interface to the verifier one atom at a time *
Daniel Veillardf8e3db02012-09-11 13:26:36 +08003488 * *
Daniel Veillard4255d502002-04-16 15:50:10 +00003489 ************************************************************************/
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003490#ifdef DEBUG_ERR
3491static void testerr(xmlRegExecCtxtPtr exec);
3492#endif
Daniel Veillard4255d502002-04-16 15:50:10 +00003493
3494/**
Daniel Veillard01c13b52002-12-10 15:19:08 +00003495 * xmlRegNewExecCtxt:
Daniel Veillard4255d502002-04-16 15:50:10 +00003496 * @comp: a precompiled regular expression
3497 * @callback: a callback function used for handling progresses in the
3498 * automata matching phase
3499 * @data: the context data associated to the callback in this context
3500 *
3501 * Build a context used for progressive evaluation of a regexp.
Daniel Veillard01c13b52002-12-10 15:19:08 +00003502 *
3503 * Returns the new context
Daniel Veillard4255d502002-04-16 15:50:10 +00003504 */
3505xmlRegExecCtxtPtr
3506xmlRegNewExecCtxt(xmlRegexpPtr comp, xmlRegExecCallbacks callback, void *data) {
3507 xmlRegExecCtxtPtr exec;
3508
3509 if (comp == NULL)
3510 return(NULL);
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00003511 if ((comp->compact == NULL) && (comp->states == NULL))
3512 return(NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +00003513 exec = (xmlRegExecCtxtPtr) xmlMalloc(sizeof(xmlRegExecCtxt));
3514 if (exec == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00003515 xmlRegexpErrMemory(NULL, "creating execution context");
Daniel Veillard4255d502002-04-16 15:50:10 +00003516 return(NULL);
3517 }
3518 memset(exec, 0, sizeof(xmlRegExecCtxt));
3519 exec->inputString = NULL;
3520 exec->index = 0;
3521 exec->determinist = 1;
3522 exec->maxRollbacks = 0;
3523 exec->nbRollbacks = 0;
3524 exec->rollbacks = NULL;
3525 exec->status = 0;
3526 exec->comp = comp;
Daniel Veillard23e73572002-09-19 19:56:43 +00003527 if (comp->compact == NULL)
3528 exec->state = comp->states[0];
Daniel Veillard4255d502002-04-16 15:50:10 +00003529 exec->transno = 0;
3530 exec->transcount = 0;
3531 exec->callback = callback;
3532 exec->data = data;
3533 if (comp->nbCounters > 0) {
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003534 /*
3535 * For error handling, exec->counts is allocated twice the size
3536 * the second half is used to store the data in case of rollback
3537 */
3538 exec->counts = (int *) xmlMalloc(comp->nbCounters * sizeof(int)
3539 * 2);
Daniel Veillard4255d502002-04-16 15:50:10 +00003540 if (exec->counts == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00003541 xmlRegexpErrMemory(NULL, "creating execution context");
Daniel Veillard4255d502002-04-16 15:50:10 +00003542 xmlFree(exec);
3543 return(NULL);
3544 }
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003545 memset(exec->counts, 0, comp->nbCounters * sizeof(int) * 2);
3546 exec->errCounts = &exec->counts[comp->nbCounters];
3547 } else {
Daniel Veillard4255d502002-04-16 15:50:10 +00003548 exec->counts = NULL;
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003549 exec->errCounts = NULL;
3550 }
Daniel Veillard4255d502002-04-16 15:50:10 +00003551 exec->inputStackMax = 0;
3552 exec->inputStackNr = 0;
3553 exec->inputStack = NULL;
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003554 exec->errStateNo = -1;
3555 exec->errString = NULL;
Daniel Veillard94cc1032005-09-15 13:09:00 +00003556 exec->nbPush = 0;
Daniel Veillard4255d502002-04-16 15:50:10 +00003557 return(exec);
3558}
3559
3560/**
3561 * xmlRegFreeExecCtxt:
3562 * @exec: a regular expression evaulation context
3563 *
3564 * Free the structures associated to a regular expression evaulation context.
3565 */
3566void
3567xmlRegFreeExecCtxt(xmlRegExecCtxtPtr exec) {
3568 if (exec == NULL)
3569 return;
3570
3571 if (exec->rollbacks != NULL) {
3572 if (exec->counts != NULL) {
3573 int i;
3574
3575 for (i = 0;i < exec->maxRollbacks;i++)
3576 if (exec->rollbacks[i].counts != NULL)
3577 xmlFree(exec->rollbacks[i].counts);
3578 }
3579 xmlFree(exec->rollbacks);
3580 }
3581 if (exec->counts != NULL)
3582 xmlFree(exec->counts);
3583 if (exec->inputStack != NULL) {
3584 int i;
3585
Daniel Veillard32370232002-10-16 14:08:14 +00003586 for (i = 0;i < exec->inputStackNr;i++) {
3587 if (exec->inputStack[i].value != NULL)
3588 xmlFree(exec->inputStack[i].value);
3589 }
Daniel Veillard4255d502002-04-16 15:50:10 +00003590 xmlFree(exec->inputStack);
3591 }
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003592 if (exec->errString != NULL)
3593 xmlFree(exec->errString);
Daniel Veillard4255d502002-04-16 15:50:10 +00003594 xmlFree(exec);
3595}
3596
3597static void
3598xmlFARegExecSaveInputString(xmlRegExecCtxtPtr exec, const xmlChar *value,
3599 void *data) {
3600#ifdef DEBUG_PUSH
3601 printf("saving value: %d:%s\n", exec->inputStackNr, value);
3602#endif
3603 if (exec->inputStackMax == 0) {
3604 exec->inputStackMax = 4;
Daniel Veillardf8e3db02012-09-11 13:26:36 +08003605 exec->inputStack = (xmlRegInputTokenPtr)
Daniel Veillard4255d502002-04-16 15:50:10 +00003606 xmlMalloc(exec->inputStackMax * sizeof(xmlRegInputToken));
3607 if (exec->inputStack == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00003608 xmlRegexpErrMemory(NULL, "pushing input string");
Daniel Veillard4255d502002-04-16 15:50:10 +00003609 exec->inputStackMax = 0;
3610 return;
3611 }
3612 } else if (exec->inputStackNr + 1 >= exec->inputStackMax) {
3613 xmlRegInputTokenPtr tmp;
3614
3615 exec->inputStackMax *= 2;
3616 tmp = (xmlRegInputTokenPtr) xmlRealloc(exec->inputStack,
3617 exec->inputStackMax * sizeof(xmlRegInputToken));
3618 if (tmp == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00003619 xmlRegexpErrMemory(NULL, "pushing input string");
Daniel Veillard4255d502002-04-16 15:50:10 +00003620 exec->inputStackMax /= 2;
3621 return;
3622 }
3623 exec->inputStack = tmp;
3624 }
3625 exec->inputStack[exec->inputStackNr].value = xmlStrdup(value);
3626 exec->inputStack[exec->inputStackNr].data = data;
3627 exec->inputStackNr++;
3628 exec->inputStack[exec->inputStackNr].value = NULL;
3629 exec->inputStack[exec->inputStackNr].data = NULL;
3630}
3631
Daniel Veillardc0826a72004-08-10 14:17:33 +00003632/**
3633 * xmlRegStrEqualWildcard:
Daniel Veillardf8e3db02012-09-11 13:26:36 +08003634 * @expStr: the string to be evaluated
Daniel Veillardc0826a72004-08-10 14:17:33 +00003635 * @valStr: the validation string
3636 *
3637 * Checks if both strings are equal or have the same content. "*"
Daniel Veillardf8e3db02012-09-11 13:26:36 +08003638 * can be used as a wildcard in @valStr; "|" is used as a seperator of
Daniel Veillardc0826a72004-08-10 14:17:33 +00003639 * substrings in both @expStr and @valStr.
3640 *
3641 * Returns 1 if the comparison is satisfied and the number of substrings
3642 * is equal, 0 otherwise.
3643 */
3644
3645static int
3646xmlRegStrEqualWildcard(const xmlChar *expStr, const xmlChar *valStr) {
3647 if (expStr == valStr) return(1);
3648 if (expStr == NULL) return(0);
3649 if (valStr == NULL) return(0);
3650 do {
3651 /*
3652 * Eval if we have a wildcard for the current item.
3653 */
3654 if (*expStr != *valStr) {
Daniel Veillard4f82c8a2005-08-09 21:40:08 +00003655 /* if one of them starts with a wildcard make valStr be it */
3656 if (*valStr == '*') {
3657 const xmlChar *tmp;
3658
3659 tmp = valStr;
3660 valStr = expStr;
3661 expStr = tmp;
3662 }
Daniel Veillardc0826a72004-08-10 14:17:33 +00003663 if ((*valStr != 0) && (*expStr != 0) && (*expStr++ == '*')) {
3664 do {
3665 if (*valStr == XML_REG_STRING_SEPARATOR)
3666 break;
Kasimier T. Buchcikc0e833f2005-04-19 15:02:20 +00003667 valStr++;
Daniel Veillardc0826a72004-08-10 14:17:33 +00003668 } while (*valStr != 0);
3669 continue;
3670 } else
3671 return(0);
3672 }
Kasimier T. Buchcikc0e833f2005-04-19 15:02:20 +00003673 expStr++;
3674 valStr++;
Daniel Veillardc0826a72004-08-10 14:17:33 +00003675 } while (*valStr != 0);
3676 if (*expStr != 0)
3677 return (0);
3678 else
3679 return (1);
3680}
Daniel Veillard4255d502002-04-16 15:50:10 +00003681
3682/**
Daniel Veillard23e73572002-09-19 19:56:43 +00003683 * xmlRegCompactPushString:
3684 * @exec: a regexp execution context
3685 * @comp: the precompiled exec with a compact table
3686 * @value: a string token input
3687 * @data: data associated to the token to reuse in callbacks
3688 *
3689 * Push one input token in the execution context
3690 *
3691 * Returns: 1 if the regexp reached a final state, 0 if non-final, and
3692 * a negative value in case of error.
3693 */
3694static int
3695xmlRegCompactPushString(xmlRegExecCtxtPtr exec,
3696 xmlRegexpPtr comp,
3697 const xmlChar *value,
3698 void *data) {
3699 int state = exec->index;
3700 int i, target;
3701
3702 if ((comp == NULL) || (comp->compact == NULL) || (comp->stringMap == NULL))
3703 return(-1);
Daniel Veillardf8e3db02012-09-11 13:26:36 +08003704
Daniel Veillard23e73572002-09-19 19:56:43 +00003705 if (value == NULL) {
3706 /*
3707 * are we at a final state ?
3708 */
3709 if (comp->compact[state * (comp->nbstrings + 1)] ==
3710 XML_REGEXP_FINAL_STATE)
3711 return(1);
3712 return(0);
3713 }
3714
3715#ifdef DEBUG_PUSH
3716 printf("value pushed: %s\n", value);
3717#endif
3718
3719 /*
William M. Brackddf71d62004-05-06 04:17:26 +00003720 * Examine all outside transitions from current state
Daniel Veillard23e73572002-09-19 19:56:43 +00003721 */
3722 for (i = 0;i < comp->nbstrings;i++) {
3723 target = comp->compact[state * (comp->nbstrings + 1) + i + 1];
3724 if ((target > 0) && (target <= comp->nbstates)) {
Daniel Veillardf8e3db02012-09-11 13:26:36 +08003725 target--; /* to avoid 0 */
Daniel Veillardc0826a72004-08-10 14:17:33 +00003726 if (xmlRegStrEqualWildcard(comp->stringMap[i], value)) {
Daniel Veillardf8e3db02012-09-11 13:26:36 +08003727 exec->index = target;
Daniel Veillard118aed72002-09-24 14:13:13 +00003728 if ((exec->callback != NULL) && (comp->transdata != NULL)) {
3729 exec->callback(exec->data, value,
3730 comp->transdata[state * comp->nbstrings + i], data);
3731 }
Daniel Veillard23e73572002-09-19 19:56:43 +00003732#ifdef DEBUG_PUSH
3733 printf("entering state %d\n", target);
3734#endif
3735 if (comp->compact[target * (comp->nbstrings + 1)] ==
Daniel Veillardcc026dc2005-01-12 13:21:17 +00003736 XML_REGEXP_SINK_STATE)
3737 goto error;
3738
3739 if (comp->compact[target * (comp->nbstrings + 1)] ==
Daniel Veillard23e73572002-09-19 19:56:43 +00003740 XML_REGEXP_FINAL_STATE)
3741 return(1);
3742 return(0);
3743 }
3744 }
3745 }
3746 /*
3747 * Failed to find an exit transition out from current state for the
3748 * current token
3749 */
3750#ifdef DEBUG_PUSH
3751 printf("failed to find a transition for %s on state %d\n", value, state);
3752#endif
Daniel Veillardcc026dc2005-01-12 13:21:17 +00003753error:
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003754 if (exec->errString != NULL)
3755 xmlFree(exec->errString);
3756 exec->errString = xmlStrdup(value);
3757 exec->errStateNo = state;
Daniel Veillard23e73572002-09-19 19:56:43 +00003758 exec->status = -1;
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003759#ifdef DEBUG_ERR
3760 testerr(exec);
3761#endif
Daniel Veillard23e73572002-09-19 19:56:43 +00003762 return(-1);
3763}
3764
3765/**
Daniel Veillard6e65e152005-08-09 11:09:52 +00003766 * xmlRegExecPushStringInternal:
Daniel Veillardea7751d2002-12-20 00:16:24 +00003767 * @exec: a regexp execution context or NULL to indicate the end
Daniel Veillard4255d502002-04-16 15:50:10 +00003768 * @value: a string token input
3769 * @data: data associated to the token to reuse in callbacks
Daniel Veillard6e65e152005-08-09 11:09:52 +00003770 * @compound: value was assembled from 2 strings
Daniel Veillard4255d502002-04-16 15:50:10 +00003771 *
3772 * Push one input token in the execution context
3773 *
3774 * Returns: 1 if the regexp reached a final state, 0 if non-final, and
3775 * a negative value in case of error.
3776 */
Daniel Veillard6e65e152005-08-09 11:09:52 +00003777static int
3778xmlRegExecPushStringInternal(xmlRegExecCtxtPtr exec, const xmlChar *value,
3779 void *data, int compound) {
Daniel Veillard4255d502002-04-16 15:50:10 +00003780 xmlRegTransPtr trans;
3781 xmlRegAtomPtr atom;
3782 int ret;
3783 int final = 0;
Daniel Veillard90700152005-01-08 22:05:09 +00003784 int progress = 1;
Daniel Veillard4255d502002-04-16 15:50:10 +00003785
3786 if (exec == NULL)
3787 return(-1);
Daniel Veillard23e73572002-09-19 19:56:43 +00003788 if (exec->comp == NULL)
3789 return(-1);
Daniel Veillard4255d502002-04-16 15:50:10 +00003790 if (exec->status != 0)
3791 return(exec->status);
3792
Daniel Veillard23e73572002-09-19 19:56:43 +00003793 if (exec->comp->compact != NULL)
3794 return(xmlRegCompactPushString(exec, exec->comp, value, data));
3795
Daniel Veillard4255d502002-04-16 15:50:10 +00003796 if (value == NULL) {
3797 if (exec->state->type == XML_REGEXP_FINAL_STATE)
3798 return(1);
3799 final = 1;
3800 }
3801
3802#ifdef DEBUG_PUSH
3803 printf("value pushed: %s\n", value);
3804#endif
3805 /*
3806 * If we have an active rollback stack push the new value there
3807 * and get back to where we were left
3808 */
3809 if ((value != NULL) && (exec->inputStackNr > 0)) {
3810 xmlFARegExecSaveInputString(exec, value, data);
3811 value = exec->inputStack[exec->index].value;
3812 data = exec->inputStack[exec->index].data;
3813#ifdef DEBUG_PUSH
3814 printf("value loaded: %s\n", value);
3815#endif
3816 }
3817
3818 while ((exec->status == 0) &&
3819 ((value != NULL) ||
3820 ((final == 1) &&
3821 (exec->state->type != XML_REGEXP_FINAL_STATE)))) {
3822
3823 /*
3824 * End of input on non-terminal state, rollback, however we may
3825 * still have epsilon like transition for counted transitions
3826 * on counters, in that case don't break too early.
3827 */
Daniel Veillardb509f152002-04-17 16:28:10 +00003828 if ((value == NULL) && (exec->counts == NULL))
Daniel Veillard4255d502002-04-16 15:50:10 +00003829 goto rollback;
3830
3831 exec->transcount = 0;
3832 for (;exec->transno < exec->state->nbTrans;exec->transno++) {
3833 trans = &exec->state->trans[exec->transno];
3834 if (trans->to < 0)
3835 continue;
3836 atom = trans->atom;
3837 ret = 0;
Daniel Veillard441bc322002-04-20 17:38:48 +00003838 if (trans->count == REGEXP_ALL_LAX_COUNTER) {
3839 int i;
3840 int count;
3841 xmlRegTransPtr t;
3842 xmlRegCounterPtr counter;
3843
3844 ret = 0;
3845
3846#ifdef DEBUG_PUSH
3847 printf("testing all lax %d\n", trans->count);
3848#endif
3849 /*
3850 * Check all counted transitions from the current state
3851 */
3852 if ((value == NULL) && (final)) {
3853 ret = 1;
3854 } else if (value != NULL) {
3855 for (i = 0;i < exec->state->nbTrans;i++) {
3856 t = &exec->state->trans[i];
3857 if ((t->counter < 0) || (t == trans))
3858 continue;
3859 counter = &exec->comp->counters[t->counter];
3860 count = exec->counts[t->counter];
Daniel Veillardf8e3db02012-09-11 13:26:36 +08003861 if ((count < counter->max) &&
Daniel Veillard441bc322002-04-20 17:38:48 +00003862 (t->atom != NULL) &&
3863 (xmlStrEqual(value, t->atom->valuep))) {
3864 ret = 0;
3865 break;
3866 }
3867 if ((count >= counter->min) &&
3868 (count < counter->max) &&
Daniel Veillard11ce4002006-03-10 00:36:23 +00003869 (t->atom != NULL) &&
Daniel Veillard441bc322002-04-20 17:38:48 +00003870 (xmlStrEqual(value, t->atom->valuep))) {
3871 ret = 1;
3872 break;
3873 }
3874 }
3875 }
3876 } else if (trans->count == REGEXP_ALL_COUNTER) {
Daniel Veillard8a001f62002-04-20 07:24:11 +00003877 int i;
3878 int count;
3879 xmlRegTransPtr t;
3880 xmlRegCounterPtr counter;
3881
3882 ret = 1;
3883
3884#ifdef DEBUG_PUSH
3885 printf("testing all %d\n", trans->count);
3886#endif
3887 /*
3888 * Check all counted transitions from the current state
3889 */
3890 for (i = 0;i < exec->state->nbTrans;i++) {
3891 t = &exec->state->trans[i];
3892 if ((t->counter < 0) || (t == trans))
3893 continue;
3894 counter = &exec->comp->counters[t->counter];
3895 count = exec->counts[t->counter];
3896 if ((count < counter->min) || (count > counter->max)) {
3897 ret = 0;
3898 break;
3899 }
3900 }
3901 } else if (trans->count >= 0) {
Daniel Veillard4255d502002-04-16 15:50:10 +00003902 int count;
3903 xmlRegCounterPtr counter;
3904
3905 /*
3906 * A counted transition.
3907 */
3908
3909 count = exec->counts[trans->count];
3910 counter = &exec->comp->counters[trans->count];
3911#ifdef DEBUG_PUSH
3912 printf("testing count %d: val %d, min %d, max %d\n",
3913 trans->count, count, counter->min, counter->max);
3914#endif
3915 ret = ((count >= counter->min) && (count <= counter->max));
3916 } else if (atom == NULL) {
3917 fprintf(stderr, "epsilon transition left at runtime\n");
3918 exec->status = -2;
3919 break;
3920 } else if (value != NULL) {
Daniel Veillardc0826a72004-08-10 14:17:33 +00003921 ret = xmlRegStrEqualWildcard(atom->valuep, value);
Daniel Veillard6e65e152005-08-09 11:09:52 +00003922 if (atom->neg) {
Daniel Veillard9efc4762005-07-19 14:33:55 +00003923 ret = !ret;
Daniel Veillard6e65e152005-08-09 11:09:52 +00003924 if (!compound)
3925 ret = 0;
3926 }
Daniel Veillard441bc322002-04-20 17:38:48 +00003927 if ((ret == 1) && (trans->counter >= 0)) {
3928 xmlRegCounterPtr counter;
3929 int count;
3930
3931 count = exec->counts[trans->counter];
3932 counter = &exec->comp->counters[trans->counter];
3933 if (count >= counter->max)
3934 ret = 0;
3935 }
3936
Daniel Veillard4255d502002-04-16 15:50:10 +00003937 if ((ret == 1) && (atom->min > 0) && (atom->max > 0)) {
3938 xmlRegStatePtr to = exec->comp->states[trans->to];
3939
3940 /*
3941 * this is a multiple input sequence
3942 */
3943 if (exec->state->nbTrans > exec->transno + 1) {
3944 if (exec->inputStackNr <= 0) {
3945 xmlFARegExecSaveInputString(exec, value, data);
3946 }
3947 xmlFARegExecSave(exec);
3948 }
3949 exec->transcount = 1;
3950 do {
3951 /*
3952 * Try to progress as much as possible on the input
3953 */
3954 if (exec->transcount == atom->max) {
3955 break;
3956 }
3957 exec->index++;
3958 value = exec->inputStack[exec->index].value;
3959 data = exec->inputStack[exec->index].data;
3960#ifdef DEBUG_PUSH
3961 printf("value loaded: %s\n", value);
3962#endif
3963
3964 /*
3965 * End of input: stop here
3966 */
3967 if (value == NULL) {
3968 exec->index --;
3969 break;
3970 }
3971 if (exec->transcount >= atom->min) {
3972 int transno = exec->transno;
3973 xmlRegStatePtr state = exec->state;
3974
3975 /*
3976 * The transition is acceptable save it
3977 */
3978 exec->transno = -1; /* trick */
3979 exec->state = to;
3980 if (exec->inputStackNr <= 0) {
3981 xmlFARegExecSaveInputString(exec, value, data);
3982 }
3983 xmlFARegExecSave(exec);
3984 exec->transno = transno;
3985 exec->state = state;
3986 }
3987 ret = xmlStrEqual(value, atom->valuep);
3988 exec->transcount++;
3989 } while (ret == 1);
3990 if (exec->transcount < atom->min)
3991 ret = 0;
3992
3993 /*
3994 * If the last check failed but one transition was found
3995 * possible, rollback
3996 */
3997 if (ret < 0)
3998 ret = 0;
3999 if (ret == 0) {
4000 goto rollback;
4001 }
4002 }
4003 }
4004 if (ret == 1) {
William M. Brack98873952003-12-26 06:03:14 +00004005 if ((exec->callback != NULL) && (atom != NULL) &&
4006 (data != NULL)) {
Daniel Veillard4255d502002-04-16 15:50:10 +00004007 exec->callback(exec->data, atom->valuep,
4008 atom->data, data);
4009 }
4010 if (exec->state->nbTrans > exec->transno + 1) {
4011 if (exec->inputStackNr <= 0) {
4012 xmlFARegExecSaveInputString(exec, value, data);
4013 }
4014 xmlFARegExecSave(exec);
4015 }
4016 if (trans->counter >= 0) {
4017#ifdef DEBUG_PUSH
4018 printf("Increasing count %d\n", trans->counter);
4019#endif
4020 exec->counts[trans->counter]++;
4021 }
Daniel Veillard10752282005-08-08 13:05:13 +00004022 if ((trans->count >= 0) &&
4023 (trans->count < REGEXP_ALL_COUNTER)) {
4024#ifdef DEBUG_REGEXP_EXEC
4025 printf("resetting count %d on transition\n",
4026 trans->count);
4027#endif
4028 exec->counts[trans->count] = 0;
4029 }
Daniel Veillard4255d502002-04-16 15:50:10 +00004030#ifdef DEBUG_PUSH
4031 printf("entering state %d\n", trans->to);
4032#endif
Daniel Veillardcc026dc2005-01-12 13:21:17 +00004033 if ((exec->comp->states[trans->to] != NULL) &&
4034 (exec->comp->states[trans->to]->type ==
4035 XML_REGEXP_SINK_STATE)) {
4036 /*
4037 * entering a sink state, save the current state as error
4038 * state.
4039 */
4040 if (exec->errString != NULL)
4041 xmlFree(exec->errString);
4042 exec->errString = xmlStrdup(value);
4043 exec->errState = exec->state;
4044 memcpy(exec->errCounts, exec->counts,
4045 exec->comp->nbCounters * sizeof(int));
4046 }
Daniel Veillard4255d502002-04-16 15:50:10 +00004047 exec->state = exec->comp->states[trans->to];
4048 exec->transno = 0;
4049 if (trans->atom != NULL) {
4050 if (exec->inputStack != NULL) {
4051 exec->index++;
4052 if (exec->index < exec->inputStackNr) {
4053 value = exec->inputStack[exec->index].value;
4054 data = exec->inputStack[exec->index].data;
4055#ifdef DEBUG_PUSH
4056 printf("value loaded: %s\n", value);
4057#endif
4058 } else {
4059 value = NULL;
4060 data = NULL;
4061#ifdef DEBUG_PUSH
4062 printf("end of input\n");
4063#endif
4064 }
4065 } else {
4066 value = NULL;
4067 data = NULL;
4068#ifdef DEBUG_PUSH
4069 printf("end of input\n");
4070#endif
4071 }
4072 }
4073 goto progress;
4074 } else if (ret < 0) {
4075 exec->status = -4;
4076 break;
4077 }
4078 }
4079 if ((exec->transno != 0) || (exec->state->nbTrans == 0)) {
4080rollback:
Daniel Veillard90700152005-01-08 22:05:09 +00004081 /*
Daniel Veillardcc026dc2005-01-12 13:21:17 +00004082 * if we didn't yet rollback on the current input
4083 * store the current state as the error state.
Daniel Veillard90700152005-01-08 22:05:09 +00004084 */
Daniel Veillardcc026dc2005-01-12 13:21:17 +00004085 if ((progress) && (exec->state != NULL) &&
4086 (exec->state->type != XML_REGEXP_SINK_STATE)) {
Daniel Veillard90700152005-01-08 22:05:09 +00004087 progress = 0;
4088 if (exec->errString != NULL)
4089 xmlFree(exec->errString);
4090 exec->errString = xmlStrdup(value);
4091 exec->errState = exec->state;
Nick Wellnhofer34e44562017-05-31 16:48:27 +02004092 if (exec->comp->nbCounters)
4093 memcpy(exec->errCounts, exec->counts,
4094 exec->comp->nbCounters * sizeof(int));
Daniel Veillard90700152005-01-08 22:05:09 +00004095 }
4096
Daniel Veillard4255d502002-04-16 15:50:10 +00004097 /*
4098 * Failed to find a way out
4099 */
4100 exec->determinist = 0;
4101 xmlFARegExecRollBack(exec);
Gaurav2671b012013-09-11 14:59:06 +08004102 if ((exec->inputStack != NULL ) && (exec->status == 0)) {
Daniel Veillard4255d502002-04-16 15:50:10 +00004103 value = exec->inputStack[exec->index].value;
4104 data = exec->inputStack[exec->index].data;
4105#ifdef DEBUG_PUSH
4106 printf("value loaded: %s\n", value);
4107#endif
4108 }
4109 }
Daniel Veillard90700152005-01-08 22:05:09 +00004110 continue;
Daniel Veillard4255d502002-04-16 15:50:10 +00004111progress:
Daniel Veillard90700152005-01-08 22:05:09 +00004112 progress = 1;
Daniel Veillard4255d502002-04-16 15:50:10 +00004113 continue;
4114 }
4115 if (exec->status == 0) {
4116 return(exec->state->type == XML_REGEXP_FINAL_STATE);
4117 }
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00004118#ifdef DEBUG_ERR
Daniel Veillard90700152005-01-08 22:05:09 +00004119 if (exec->status < 0) {
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00004120 testerr(exec);
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00004121 }
Daniel Veillard90700152005-01-08 22:05:09 +00004122#endif
Daniel Veillard4255d502002-04-16 15:50:10 +00004123 return(exec->status);
4124}
4125
Daniel Veillard52b48c72003-04-13 19:53:42 +00004126/**
Daniel Veillard6e65e152005-08-09 11:09:52 +00004127 * xmlRegExecPushString:
4128 * @exec: a regexp execution context or NULL to indicate the end
4129 * @value: a string token input
4130 * @data: data associated to the token to reuse in callbacks
4131 *
4132 * Push one input token in the execution context
4133 *
4134 * Returns: 1 if the regexp reached a final state, 0 if non-final, and
4135 * a negative value in case of error.
4136 */
4137int
4138xmlRegExecPushString(xmlRegExecCtxtPtr exec, const xmlChar *value,
4139 void *data) {
4140 return(xmlRegExecPushStringInternal(exec, value, data, 0));
4141}
4142
4143/**
Daniel Veillard52b48c72003-04-13 19:53:42 +00004144 * xmlRegExecPushString2:
4145 * @exec: a regexp execution context or NULL to indicate the end
4146 * @value: the first string token input
4147 * @value2: the second string token input
4148 * @data: data associated to the token to reuse in callbacks
4149 *
4150 * Push one input token in the execution context
4151 *
4152 * Returns: 1 if the regexp reached a final state, 0 if non-final, and
4153 * a negative value in case of error.
4154 */
4155int
4156xmlRegExecPushString2(xmlRegExecCtxtPtr exec, const xmlChar *value,
4157 const xmlChar *value2, void *data) {
4158 xmlChar buf[150];
4159 int lenn, lenp, ret;
4160 xmlChar *str;
4161
4162 if (exec == NULL)
4163 return(-1);
4164 if (exec->comp == NULL)
4165 return(-1);
4166 if (exec->status != 0)
4167 return(exec->status);
4168
4169 if (value2 == NULL)
4170 return(xmlRegExecPushString(exec, value, data));
4171
4172 lenn = strlen((char *) value2);
4173 lenp = strlen((char *) value);
4174
4175 if (150 < lenn + lenp + 2) {
Daniel Veillard3c908dc2003-04-19 00:07:51 +00004176 str = (xmlChar *) xmlMallocAtomic(lenn + lenp + 2);
Daniel Veillard52b48c72003-04-13 19:53:42 +00004177 if (str == NULL) {
4178 exec->status = -1;
4179 return(-1);
4180 }
4181 } else {
4182 str = buf;
4183 }
4184 memcpy(&str[0], value, lenp);
Daniel Veillardc0826a72004-08-10 14:17:33 +00004185 str[lenp] = XML_REG_STRING_SEPARATOR;
Daniel Veillard52b48c72003-04-13 19:53:42 +00004186 memcpy(&str[lenp + 1], value2, lenn);
4187 str[lenn + lenp + 1] = 0;
4188
4189 if (exec->comp->compact != NULL)
4190 ret = xmlRegCompactPushString(exec, exec->comp, str, data);
4191 else
Daniel Veillard6e65e152005-08-09 11:09:52 +00004192 ret = xmlRegExecPushStringInternal(exec, str, data, 1);
Daniel Veillard52b48c72003-04-13 19:53:42 +00004193
4194 if (str != buf)
Daniel Veillard0b1ff142005-12-28 21:13:33 +00004195 xmlFree(str);
Daniel Veillard52b48c72003-04-13 19:53:42 +00004196 return(ret);
4197}
4198
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00004199/**
Daniel Veillard77005e62005-07-19 16:26:18 +00004200 * xmlRegExecGetValues:
Daniel Veillardfc0b6f62005-01-09 17:48:02 +00004201 * @exec: a regexp execution context
4202 * @err: error extraction or normal one
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00004203 * @nbval: pointer to the number of accepted values IN/OUT
Daniel Veillardcc026dc2005-01-12 13:21:17 +00004204 * @nbneg: return number of negative transitions
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00004205 * @values: pointer to the array of acceptable values
Daniel Veillardfc0b6f62005-01-09 17:48:02 +00004206 * @terminal: return value if this was a terminal state
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00004207 *
Daniel Veillardfc0b6f62005-01-09 17:48:02 +00004208 * Extract informations from the regexp execution, internal routine to
4209 * implement xmlRegExecNextValues() and xmlRegExecErrInfo()
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00004210 *
4211 * Returns: 0 in case of success or -1 in case of error.
4212 */
Daniel Veillardfc0b6f62005-01-09 17:48:02 +00004213static int
4214xmlRegExecGetValues(xmlRegExecCtxtPtr exec, int err,
Daniel Veillardcc026dc2005-01-12 13:21:17 +00004215 int *nbval, int *nbneg,
4216 xmlChar **values, int *terminal) {
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00004217 int maxval;
Daniel Veillardcc026dc2005-01-12 13:21:17 +00004218 int nb = 0;
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00004219
Daniel Veillardf8e3db02012-09-11 13:26:36 +08004220 if ((exec == NULL) || (nbval == NULL) || (nbneg == NULL) ||
Daniel Veillardcc026dc2005-01-12 13:21:17 +00004221 (values == NULL) || (*nbval <= 0))
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00004222 return(-1);
Daniel Veillardfc0b6f62005-01-09 17:48:02 +00004223
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00004224 maxval = *nbval;
4225 *nbval = 0;
Daniel Veillardcc026dc2005-01-12 13:21:17 +00004226 *nbneg = 0;
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00004227 if ((exec->comp != NULL) && (exec->comp->compact != NULL)) {
4228 xmlRegexpPtr comp;
4229 int target, i, state;
4230
4231 comp = exec->comp;
Daniel Veillardfc0b6f62005-01-09 17:48:02 +00004232
4233 if (err) {
4234 if (exec->errStateNo == -1) return(-1);
4235 state = exec->errStateNo;
4236 } else {
4237 state = exec->index;
4238 }
4239 if (terminal != NULL) {
4240 if (comp->compact[state * (comp->nbstrings + 1)] ==
4241 XML_REGEXP_FINAL_STATE)
4242 *terminal = 1;
4243 else
4244 *terminal = 0;
4245 }
Daniel Veillardcc026dc2005-01-12 13:21:17 +00004246 for (i = 0;(i < comp->nbstrings) && (nb < maxval);i++) {
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00004247 target = comp->compact[state * (comp->nbstrings + 1) + i + 1];
Daniel Veillardcc026dc2005-01-12 13:21:17 +00004248 if ((target > 0) && (target <= comp->nbstates) &&
4249 (comp->compact[(target - 1) * (comp->nbstrings + 1)] !=
4250 XML_REGEXP_SINK_STATE)) {
4251 values[nb++] = comp->stringMap[i];
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00004252 (*nbval)++;
4253 }
4254 }
Daniel Veillardcc026dc2005-01-12 13:21:17 +00004255 for (i = 0;(i < comp->nbstrings) && (nb < maxval);i++) {
4256 target = comp->compact[state * (comp->nbstrings + 1) + i + 1];
4257 if ((target > 0) && (target <= comp->nbstates) &&
4258 (comp->compact[(target - 1) * (comp->nbstrings + 1)] ==
4259 XML_REGEXP_SINK_STATE)) {
4260 values[nb++] = comp->stringMap[i];
4261 (*nbneg)++;
4262 }
4263 }
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00004264 } else {
4265 int transno;
4266 xmlRegTransPtr trans;
4267 xmlRegAtomPtr atom;
Daniel Veillardfc0b6f62005-01-09 17:48:02 +00004268 xmlRegStatePtr state;
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00004269
Daniel Veillardfc0b6f62005-01-09 17:48:02 +00004270 if (terminal != NULL) {
4271 if (exec->state->type == XML_REGEXP_FINAL_STATE)
4272 *terminal = 1;
4273 else
4274 *terminal = 0;
4275 }
4276
4277 if (err) {
4278 if (exec->errState == NULL) return(-1);
4279 state = exec->errState;
4280 } else {
4281 if (exec->state == NULL) return(-1);
4282 state = exec->state;
4283 }
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00004284 for (transno = 0;
Daniel Veillardcc026dc2005-01-12 13:21:17 +00004285 (transno < state->nbTrans) && (nb < maxval);
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00004286 transno++) {
Daniel Veillardfc0b6f62005-01-09 17:48:02 +00004287 trans = &state->trans[transno];
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00004288 if (trans->to < 0)
4289 continue;
4290 atom = trans->atom;
4291 if ((atom == NULL) || (atom->valuep == NULL))
4292 continue;
4293 if (trans->count == REGEXP_ALL_LAX_COUNTER) {
Daniel Veillardcc026dc2005-01-12 13:21:17 +00004294 /* this should not be reached but ... */
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00004295 TODO;
4296 } else if (trans->count == REGEXP_ALL_COUNTER) {
Daniel Veillardcc026dc2005-01-12 13:21:17 +00004297 /* this should not be reached but ... */
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00004298 TODO;
4299 } else if (trans->counter >= 0) {
Daniel Veillard11ce4002006-03-10 00:36:23 +00004300 xmlRegCounterPtr counter = NULL;
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00004301 int count;
4302
Daniel Veillardfc0b6f62005-01-09 17:48:02 +00004303 if (err)
4304 count = exec->errCounts[trans->counter];
4305 else
4306 count = exec->counts[trans->counter];
Daniel Veillard11ce4002006-03-10 00:36:23 +00004307 if (exec->comp != NULL)
4308 counter = &exec->comp->counters[trans->counter];
4309 if ((counter == NULL) || (count < counter->max)) {
Daniel Veillard77005e62005-07-19 16:26:18 +00004310 if (atom->neg)
4311 values[nb++] = (xmlChar *) atom->valuep2;
4312 else
4313 values[nb++] = (xmlChar *) atom->valuep;
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00004314 (*nbval)++;
4315 }
4316 } else {
Gaurav2671b012013-09-11 14:59:06 +08004317 if ((exec->comp != NULL) && (exec->comp->states[trans->to] != NULL) &&
Daniel Veillardcc026dc2005-01-12 13:21:17 +00004318 (exec->comp->states[trans->to]->type !=
4319 XML_REGEXP_SINK_STATE)) {
Daniel Veillard77005e62005-07-19 16:26:18 +00004320 if (atom->neg)
4321 values[nb++] = (xmlChar *) atom->valuep2;
4322 else
4323 values[nb++] = (xmlChar *) atom->valuep;
Daniel Veillardcc026dc2005-01-12 13:21:17 +00004324 (*nbval)++;
4325 }
Daniel Veillardf8e3db02012-09-11 13:26:36 +08004326 }
Daniel Veillardcc026dc2005-01-12 13:21:17 +00004327 }
4328 for (transno = 0;
4329 (transno < state->nbTrans) && (nb < maxval);
4330 transno++) {
4331 trans = &state->trans[transno];
4332 if (trans->to < 0)
4333 continue;
4334 atom = trans->atom;
4335 if ((atom == NULL) || (atom->valuep == NULL))
4336 continue;
4337 if (trans->count == REGEXP_ALL_LAX_COUNTER) {
4338 continue;
4339 } else if (trans->count == REGEXP_ALL_COUNTER) {
4340 continue;
4341 } else if (trans->counter >= 0) {
4342 continue;
4343 } else {
4344 if ((exec->comp->states[trans->to] != NULL) &&
4345 (exec->comp->states[trans->to]->type ==
4346 XML_REGEXP_SINK_STATE)) {
Daniel Veillard77005e62005-07-19 16:26:18 +00004347 if (atom->neg)
4348 values[nb++] = (xmlChar *) atom->valuep2;
4349 else
4350 values[nb++] = (xmlChar *) atom->valuep;
Daniel Veillardcc026dc2005-01-12 13:21:17 +00004351 (*nbneg)++;
4352 }
Daniel Veillardf8e3db02012-09-11 13:26:36 +08004353 }
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00004354 }
4355 }
4356 return(0);
4357}
4358
Daniel Veillardfc0b6f62005-01-09 17:48:02 +00004359/**
4360 * xmlRegExecNextValues:
4361 * @exec: a regexp execution context
4362 * @nbval: pointer to the number of accepted values IN/OUT
Daniel Veillardcc026dc2005-01-12 13:21:17 +00004363 * @nbneg: return number of negative transitions
Daniel Veillardfc0b6f62005-01-09 17:48:02 +00004364 * @values: pointer to the array of acceptable values
4365 * @terminal: return value if this was a terminal state
4366 *
4367 * Extract informations from the regexp execution,
4368 * the parameter @values must point to an array of @nbval string pointers
4369 * on return nbval will contain the number of possible strings in that
4370 * state and the @values array will be updated with them. The string values
4371 * returned will be freed with the @exec context and don't need to be
4372 * deallocated.
4373 *
4374 * Returns: 0 in case of success or -1 in case of error.
4375 */
4376int
Daniel Veillardcc026dc2005-01-12 13:21:17 +00004377xmlRegExecNextValues(xmlRegExecCtxtPtr exec, int *nbval, int *nbneg,
4378 xmlChar **values, int *terminal) {
4379 return(xmlRegExecGetValues(exec, 0, nbval, nbneg, values, terminal));
Daniel Veillardfc0b6f62005-01-09 17:48:02 +00004380}
4381
4382/**
4383 * xmlRegExecErrInfo:
4384 * @exec: a regexp execution context generating an error
4385 * @string: return value for the error string
4386 * @nbval: pointer to the number of accepted values IN/OUT
Daniel Veillardcc026dc2005-01-12 13:21:17 +00004387 * @nbneg: return number of negative transitions
Daniel Veillardfc0b6f62005-01-09 17:48:02 +00004388 * @values: pointer to the array of acceptable values
4389 * @terminal: return value if this was a terminal state
4390 *
4391 * Extract error informations from the regexp execution, the parameter
4392 * @string will be updated with the value pushed and not accepted,
4393 * the parameter @values must point to an array of @nbval string pointers
4394 * on return nbval will contain the number of possible strings in that
4395 * state and the @values array will be updated with them. The string values
4396 * returned will be freed with the @exec context and don't need to be
4397 * deallocated.
4398 *
4399 * Returns: 0 in case of success or -1 in case of error.
4400 */
4401int
4402xmlRegExecErrInfo(xmlRegExecCtxtPtr exec, const xmlChar **string,
Daniel Veillardcc026dc2005-01-12 13:21:17 +00004403 int *nbval, int *nbneg, xmlChar **values, int *terminal) {
Daniel Veillardfc0b6f62005-01-09 17:48:02 +00004404 if (exec == NULL)
4405 return(-1);
4406 if (string != NULL) {
4407 if (exec->status != 0)
4408 *string = exec->errString;
4409 else
4410 *string = NULL;
4411 }
Daniel Veillardcc026dc2005-01-12 13:21:17 +00004412 return(xmlRegExecGetValues(exec, 1, nbval, nbneg, values, terminal));
Daniel Veillardfc0b6f62005-01-09 17:48:02 +00004413}
4414
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00004415#ifdef DEBUG_ERR
4416static void testerr(xmlRegExecCtxtPtr exec) {
4417 const xmlChar *string;
Daniel Veillardcee2b3a2005-01-25 00:22:52 +00004418 xmlChar *values[5];
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00004419 int nb = 5;
Daniel Veillardcc026dc2005-01-12 13:21:17 +00004420 int nbneg;
Daniel Veillardfc0b6f62005-01-09 17:48:02 +00004421 int terminal;
Daniel Veillardcc026dc2005-01-12 13:21:17 +00004422 xmlRegExecErrInfo(exec, &string, &nb, &nbneg, &values[0], &terminal);
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00004423}
4424#endif
4425
Daniel Veillard4255d502002-04-16 15:50:10 +00004426#if 0
4427static int
4428xmlRegExecPushChar(xmlRegExecCtxtPtr exec, int UCS) {
4429 xmlRegTransPtr trans;
4430 xmlRegAtomPtr atom;
4431 int ret;
4432 int codepoint, len;
4433
4434 if (exec == NULL)
4435 return(-1);
4436 if (exec->status != 0)
4437 return(exec->status);
4438
4439 while ((exec->status == 0) &&
4440 ((exec->inputString[exec->index] != 0) ||
4441 (exec->state->type != XML_REGEXP_FINAL_STATE))) {
4442
4443 /*
4444 * End of input on non-terminal state, rollback, however we may
4445 * still have epsilon like transition for counted transitions
4446 * on counters, in that case don't break too early.
4447 */
4448 if ((exec->inputString[exec->index] == 0) && (exec->counts == NULL))
4449 goto rollback;
4450
4451 exec->transcount = 0;
4452 for (;exec->transno < exec->state->nbTrans;exec->transno++) {
4453 trans = &exec->state->trans[exec->transno];
4454 if (trans->to < 0)
4455 continue;
4456 atom = trans->atom;
4457 ret = 0;
4458 if (trans->count >= 0) {
4459 int count;
4460 xmlRegCounterPtr counter;
4461
4462 /*
4463 * A counted transition.
4464 */
4465
4466 count = exec->counts[trans->count];
4467 counter = &exec->comp->counters[trans->count];
4468#ifdef DEBUG_REGEXP_EXEC
4469 printf("testing count %d: val %d, min %d, max %d\n",
4470 trans->count, count, counter->min, counter->max);
4471#endif
4472 ret = ((count >= counter->min) && (count <= counter->max));
4473 } else if (atom == NULL) {
4474 fprintf(stderr, "epsilon transition left at runtime\n");
4475 exec->status = -2;
4476 break;
4477 } else if (exec->inputString[exec->index] != 0) {
4478 codepoint = CUR_SCHAR(&(exec->inputString[exec->index]), len);
4479 ret = xmlRegCheckCharacter(atom, codepoint);
4480 if ((ret == 1) && (atom->min > 0) && (atom->max > 0)) {
4481 xmlRegStatePtr to = exec->comp->states[trans->to];
4482
4483 /*
4484 * this is a multiple input sequence
4485 */
4486 if (exec->state->nbTrans > exec->transno + 1) {
4487 xmlFARegExecSave(exec);
4488 }
4489 exec->transcount = 1;
4490 do {
4491 /*
4492 * Try to progress as much as possible on the input
4493 */
4494 if (exec->transcount == atom->max) {
4495 break;
4496 }
4497 exec->index += len;
4498 /*
4499 * End of input: stop here
4500 */
4501 if (exec->inputString[exec->index] == 0) {
4502 exec->index -= len;
4503 break;
4504 }
4505 if (exec->transcount >= atom->min) {
4506 int transno = exec->transno;
4507 xmlRegStatePtr state = exec->state;
4508
4509 /*
4510 * The transition is acceptable save it
4511 */
4512 exec->transno = -1; /* trick */
4513 exec->state = to;
4514 xmlFARegExecSave(exec);
4515 exec->transno = transno;
4516 exec->state = state;
4517 }
4518 codepoint = CUR_SCHAR(&(exec->inputString[exec->index]),
4519 len);
4520 ret = xmlRegCheckCharacter(atom, codepoint);
4521 exec->transcount++;
4522 } while (ret == 1);
4523 if (exec->transcount < atom->min)
4524 ret = 0;
4525
4526 /*
4527 * If the last check failed but one transition was found
4528 * possible, rollback
4529 */
4530 if (ret < 0)
4531 ret = 0;
4532 if (ret == 0) {
4533 goto rollback;
4534 }
4535 }
4536 }
4537 if (ret == 1) {
4538 if (exec->state->nbTrans > exec->transno + 1) {
4539 xmlFARegExecSave(exec);
4540 }
Daniel Veillard54eb0242006-03-21 23:17:57 +00004541 /*
4542 * restart count for expressions like this ((abc){2})*
4543 */
4544 if (trans->count >= 0) {
4545#ifdef DEBUG_REGEXP_EXEC
4546 printf("Reset count %d\n", trans->count);
4547#endif
4548 exec->counts[trans->count] = 0;
4549 }
Daniel Veillard4255d502002-04-16 15:50:10 +00004550 if (trans->counter >= 0) {
4551#ifdef DEBUG_REGEXP_EXEC
4552 printf("Increasing count %d\n", trans->counter);
4553#endif
4554 exec->counts[trans->counter]++;
4555 }
4556#ifdef DEBUG_REGEXP_EXEC
4557 printf("entering state %d\n", trans->to);
4558#endif
4559 exec->state = exec->comp->states[trans->to];
4560 exec->transno = 0;
4561 if (trans->atom != NULL) {
4562 exec->index += len;
4563 }
4564 goto progress;
4565 } else if (ret < 0) {
4566 exec->status = -4;
4567 break;
4568 }
4569 }
4570 if ((exec->transno != 0) || (exec->state->nbTrans == 0)) {
4571rollback:
4572 /*
4573 * Failed to find a way out
4574 */
4575 exec->determinist = 0;
4576 xmlFARegExecRollBack(exec);
4577 }
4578progress:
4579 continue;
4580 }
4581}
4582#endif
4583/************************************************************************
Daniel Veillardf8e3db02012-09-11 13:26:36 +08004584 * *
William M. Brackddf71d62004-05-06 04:17:26 +00004585 * Parser for the Schemas Datatype Regular Expressions *
Daniel Veillard4255d502002-04-16 15:50:10 +00004586 * http://www.w3.org/TR/2001/REC-xmlschema-2-20010502/#regexs *
Daniel Veillardf8e3db02012-09-11 13:26:36 +08004587 * *
Daniel Veillard4255d502002-04-16 15:50:10 +00004588 ************************************************************************/
4589
4590/**
4591 * xmlFAIsChar:
Daniel Veillard441bc322002-04-20 17:38:48 +00004592 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00004593 *
4594 * [10] Char ::= [^.\?*+()|#x5B#x5D]
4595 */
4596static int
4597xmlFAIsChar(xmlRegParserCtxtPtr ctxt) {
4598 int cur;
4599 int len;
4600
4601 cur = CUR_SCHAR(ctxt->cur, len);
4602 if ((cur == '.') || (cur == '\\') || (cur == '?') ||
4603 (cur == '*') || (cur == '+') || (cur == '(') ||
4604 (cur == ')') || (cur == '|') || (cur == 0x5B) ||
4605 (cur == 0x5D) || (cur == 0))
4606 return(-1);
4607 return(cur);
4608}
4609
4610/**
4611 * xmlFAParseCharProp:
Daniel Veillard441bc322002-04-20 17:38:48 +00004612 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00004613 *
4614 * [27] charProp ::= IsCategory | IsBlock
4615 * [28] IsCategory ::= Letters | Marks | Numbers | Punctuation |
Daniel Veillardf8e3db02012-09-11 13:26:36 +08004616 * Separators | Symbols | Others
Daniel Veillard4255d502002-04-16 15:50:10 +00004617 * [29] Letters ::= 'L' [ultmo]?
4618 * [30] Marks ::= 'M' [nce]?
4619 * [31] Numbers ::= 'N' [dlo]?
4620 * [32] Punctuation ::= 'P' [cdseifo]?
4621 * [33] Separators ::= 'Z' [slp]?
4622 * [34] Symbols ::= 'S' [mcko]?
4623 * [35] Others ::= 'C' [cfon]?
4624 * [36] IsBlock ::= 'Is' [a-zA-Z0-9#x2D]+
4625 */
4626static void
4627xmlFAParseCharProp(xmlRegParserCtxtPtr ctxt) {
4628 int cur;
William M. Brack779af002003-08-01 15:55:39 +00004629 xmlRegAtomType type = (xmlRegAtomType) 0;
Daniel Veillard4255d502002-04-16 15:50:10 +00004630 xmlChar *blockName = NULL;
Daniel Veillardf8e3db02012-09-11 13:26:36 +08004631
Daniel Veillard4255d502002-04-16 15:50:10 +00004632 cur = CUR;
4633 if (cur == 'L') {
4634 NEXT;
4635 cur = CUR;
4636 if (cur == 'u') {
4637 NEXT;
4638 type = XML_REGEXP_LETTER_UPPERCASE;
4639 } else if (cur == 'l') {
4640 NEXT;
4641 type = XML_REGEXP_LETTER_LOWERCASE;
4642 } else if (cur == 't') {
4643 NEXT;
4644 type = XML_REGEXP_LETTER_TITLECASE;
4645 } else if (cur == 'm') {
4646 NEXT;
4647 type = XML_REGEXP_LETTER_MODIFIER;
4648 } else if (cur == 'o') {
4649 NEXT;
4650 type = XML_REGEXP_LETTER_OTHERS;
4651 } else {
4652 type = XML_REGEXP_LETTER;
4653 }
4654 } else if (cur == 'M') {
4655 NEXT;
4656 cur = CUR;
4657 if (cur == 'n') {
4658 NEXT;
4659 /* nonspacing */
4660 type = XML_REGEXP_MARK_NONSPACING;
4661 } else if (cur == 'c') {
4662 NEXT;
4663 /* spacing combining */
4664 type = XML_REGEXP_MARK_SPACECOMBINING;
4665 } else if (cur == 'e') {
4666 NEXT;
4667 /* enclosing */
4668 type = XML_REGEXP_MARK_ENCLOSING;
4669 } else {
4670 /* all marks */
4671 type = XML_REGEXP_MARK;
4672 }
4673 } else if (cur == 'N') {
4674 NEXT;
4675 cur = CUR;
4676 if (cur == 'd') {
4677 NEXT;
4678 /* digital */
4679 type = XML_REGEXP_NUMBER_DECIMAL;
4680 } else if (cur == 'l') {
4681 NEXT;
4682 /* letter */
4683 type = XML_REGEXP_NUMBER_LETTER;
4684 } else if (cur == 'o') {
4685 NEXT;
4686 /* other */
4687 type = XML_REGEXP_NUMBER_OTHERS;
4688 } else {
4689 /* all numbers */
4690 type = XML_REGEXP_NUMBER;
4691 }
4692 } else if (cur == 'P') {
4693 NEXT;
4694 cur = CUR;
4695 if (cur == 'c') {
4696 NEXT;
4697 /* connector */
4698 type = XML_REGEXP_PUNCT_CONNECTOR;
4699 } else if (cur == 'd') {
4700 NEXT;
4701 /* dash */
4702 type = XML_REGEXP_PUNCT_DASH;
4703 } else if (cur == 's') {
4704 NEXT;
4705 /* open */
4706 type = XML_REGEXP_PUNCT_OPEN;
4707 } else if (cur == 'e') {
4708 NEXT;
4709 /* close */
4710 type = XML_REGEXP_PUNCT_CLOSE;
4711 } else if (cur == 'i') {
4712 NEXT;
4713 /* initial quote */
4714 type = XML_REGEXP_PUNCT_INITQUOTE;
4715 } else if (cur == 'f') {
4716 NEXT;
4717 /* final quote */
4718 type = XML_REGEXP_PUNCT_FINQUOTE;
4719 } else if (cur == 'o') {
4720 NEXT;
4721 /* other */
4722 type = XML_REGEXP_PUNCT_OTHERS;
4723 } else {
4724 /* all punctuation */
4725 type = XML_REGEXP_PUNCT;
4726 }
4727 } else if (cur == 'Z') {
4728 NEXT;
4729 cur = CUR;
4730 if (cur == 's') {
4731 NEXT;
4732 /* space */
4733 type = XML_REGEXP_SEPAR_SPACE;
4734 } else if (cur == 'l') {
4735 NEXT;
4736 /* line */
4737 type = XML_REGEXP_SEPAR_LINE;
4738 } else if (cur == 'p') {
4739 NEXT;
4740 /* paragraph */
4741 type = XML_REGEXP_SEPAR_PARA;
4742 } else {
4743 /* all separators */
4744 type = XML_REGEXP_SEPAR;
4745 }
4746 } else if (cur == 'S') {
4747 NEXT;
4748 cur = CUR;
4749 if (cur == 'm') {
4750 NEXT;
4751 type = XML_REGEXP_SYMBOL_MATH;
4752 /* math */
4753 } else if (cur == 'c') {
4754 NEXT;
4755 type = XML_REGEXP_SYMBOL_CURRENCY;
4756 /* currency */
4757 } else if (cur == 'k') {
4758 NEXT;
4759 type = XML_REGEXP_SYMBOL_MODIFIER;
4760 /* modifiers */
4761 } else if (cur == 'o') {
4762 NEXT;
4763 type = XML_REGEXP_SYMBOL_OTHERS;
4764 /* other */
4765 } else {
4766 /* all symbols */
4767 type = XML_REGEXP_SYMBOL;
4768 }
4769 } else if (cur == 'C') {
4770 NEXT;
4771 cur = CUR;
4772 if (cur == 'c') {
4773 NEXT;
4774 /* control */
4775 type = XML_REGEXP_OTHER_CONTROL;
4776 } else if (cur == 'f') {
4777 NEXT;
4778 /* format */
4779 type = XML_REGEXP_OTHER_FORMAT;
4780 } else if (cur == 'o') {
4781 NEXT;
4782 /* private use */
4783 type = XML_REGEXP_OTHER_PRIVATE;
4784 } else if (cur == 'n') {
4785 NEXT;
4786 /* not assigned */
4787 type = XML_REGEXP_OTHER_NA;
4788 } else {
4789 /* all others */
4790 type = XML_REGEXP_OTHER;
4791 }
4792 } else if (cur == 'I') {
4793 const xmlChar *start;
4794 NEXT;
4795 cur = CUR;
4796 if (cur != 's') {
4797 ERROR("IsXXXX expected");
4798 return;
4799 }
4800 NEXT;
4801 start = ctxt->cur;
4802 cur = CUR;
Daniel Veillardf8e3db02012-09-11 13:26:36 +08004803 if (((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;
Daniel Veillardf8e3db02012-09-11 13:26:36 +08004809 while (((cur >= 'a') && (cur <= 'z')) ||
4810 ((cur >= 'A') && (cur <= 'Z')) ||
4811 ((cur >= '0') && (cur <= '9')) ||
Daniel Veillard4255d502002-04-16 15:50:10 +00004812 (cur == 0x2D)) {
4813 NEXT;
4814 cur = CUR;
4815 }
4816 }
4817 type = XML_REGEXP_BLOCK_NAME;
4818 blockName = xmlStrndup(start, ctxt->cur - start);
4819 } else {
4820 ERROR("Unknown char property");
4821 return;
4822 }
4823 if (ctxt->atom == NULL) {
4824 ctxt->atom = xmlRegNewAtom(ctxt, type);
4825 if (ctxt->atom != NULL)
4826 ctxt->atom->valuep = blockName;
4827 } else if (ctxt->atom->type == XML_REGEXP_RANGES) {
4828 xmlRegAtomAddRange(ctxt, ctxt->atom, ctxt->neg,
4829 type, 0, 0, blockName);
4830 }
4831}
4832
4833/**
4834 * xmlFAParseCharClassEsc:
Daniel Veillard441bc322002-04-20 17:38:48 +00004835 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00004836 *
Daniel Veillardf8e3db02012-09-11 13:26:36 +08004837 * [23] charClassEsc ::= ( SingleCharEsc | MultiCharEsc | catEsc | complEsc )
Daniel Veillard4255d502002-04-16 15:50:10 +00004838 * [24] SingleCharEsc ::= '\' [nrt\|.?*+(){}#x2D#x5B#x5D#x5E]
4839 * [25] catEsc ::= '\p{' charProp '}'
4840 * [26] complEsc ::= '\P{' charProp '}'
4841 * [37] MultiCharEsc ::= '.' | ('\' [sSiIcCdDwW])
4842 */
4843static void
4844xmlFAParseCharClassEsc(xmlRegParserCtxtPtr ctxt) {
4845 int cur;
4846
4847 if (CUR == '.') {
4848 if (ctxt->atom == NULL) {
4849 ctxt->atom = xmlRegNewAtom(ctxt, XML_REGEXP_ANYCHAR);
4850 } else if (ctxt->atom->type == XML_REGEXP_RANGES) {
4851 xmlRegAtomAddRange(ctxt, ctxt->atom, ctxt->neg,
4852 XML_REGEXP_ANYCHAR, 0, 0, NULL);
4853 }
4854 NEXT;
4855 return;
4856 }
4857 if (CUR != '\\') {
4858 ERROR("Escaped sequence: expecting \\");
4859 return;
4860 }
4861 NEXT;
4862 cur = CUR;
4863 if (cur == 'p') {
4864 NEXT;
4865 if (CUR != '{') {
4866 ERROR("Expecting '{'");
4867 return;
4868 }
4869 NEXT;
4870 xmlFAParseCharProp(ctxt);
4871 if (CUR != '}') {
4872 ERROR("Expecting '}'");
4873 return;
4874 }
4875 NEXT;
4876 } else if (cur == 'P') {
4877 NEXT;
4878 if (CUR != '{') {
4879 ERROR("Expecting '{'");
4880 return;
4881 }
4882 NEXT;
4883 xmlFAParseCharProp(ctxt);
4884 ctxt->atom->neg = 1;
4885 if (CUR != '}') {
4886 ERROR("Expecting '}'");
4887 return;
4888 }
4889 NEXT;
4890 } else if ((cur == 'n') || (cur == 'r') || (cur == 't') || (cur == '\\') ||
4891 (cur == '|') || (cur == '.') || (cur == '?') || (cur == '*') ||
4892 (cur == '+') || (cur == '(') || (cur == ')') || (cur == '{') ||
4893 (cur == '}') || (cur == 0x2D) || (cur == 0x5B) || (cur == 0x5D) ||
4894 (cur == 0x5E)) {
4895 if (ctxt->atom == NULL) {
4896 ctxt->atom = xmlRegNewAtom(ctxt, XML_REGEXP_CHARVAL);
Daniel Veillard99c394d2005-07-14 12:58:49 +00004897 if (ctxt->atom != NULL) {
4898 switch (cur) {
4899 case 'n':
4900 ctxt->atom->codepoint = '\n';
4901 break;
4902 case 'r':
4903 ctxt->atom->codepoint = '\r';
4904 break;
4905 case 't':
4906 ctxt->atom->codepoint = '\t';
4907 break;
4908 default:
4909 ctxt->atom->codepoint = cur;
4910 }
4911 }
Daniel Veillard4255d502002-04-16 15:50:10 +00004912 } else if (ctxt->atom->type == XML_REGEXP_RANGES) {
Daniel Veillard9543aee2010-03-15 11:13:39 +01004913 switch (cur) {
4914 case 'n':
4915 cur = '\n';
4916 break;
4917 case 'r':
4918 cur = '\r';
4919 break;
4920 case 't':
4921 cur = '\t';
4922 break;
4923 }
Daniel Veillard4255d502002-04-16 15:50:10 +00004924 xmlRegAtomAddRange(ctxt, ctxt->atom, ctxt->neg,
4925 XML_REGEXP_CHARVAL, cur, cur, NULL);
4926 }
4927 NEXT;
4928 } else if ((cur == 's') || (cur == 'S') || (cur == 'i') || (cur == 'I') ||
4929 (cur == 'c') || (cur == 'C') || (cur == 'd') || (cur == 'D') ||
4930 (cur == 'w') || (cur == 'W')) {
Daniel Veillardb509f152002-04-17 16:28:10 +00004931 xmlRegAtomType type = XML_REGEXP_ANYSPACE;
Daniel Veillard4255d502002-04-16 15:50:10 +00004932
4933 switch (cur) {
Daniel Veillardf8e3db02012-09-11 13:26:36 +08004934 case 's':
Daniel Veillard4255d502002-04-16 15:50:10 +00004935 type = XML_REGEXP_ANYSPACE;
4936 break;
Daniel Veillardf8e3db02012-09-11 13:26:36 +08004937 case 'S':
Daniel Veillard4255d502002-04-16 15:50:10 +00004938 type = XML_REGEXP_NOTSPACE;
4939 break;
Daniel Veillardf8e3db02012-09-11 13:26:36 +08004940 case 'i':
Daniel Veillard4255d502002-04-16 15:50:10 +00004941 type = XML_REGEXP_INITNAME;
4942 break;
Daniel Veillardf8e3db02012-09-11 13:26:36 +08004943 case 'I':
Daniel Veillard4255d502002-04-16 15:50:10 +00004944 type = XML_REGEXP_NOTINITNAME;
4945 break;
Daniel Veillardf8e3db02012-09-11 13:26:36 +08004946 case 'c':
Daniel Veillard4255d502002-04-16 15:50:10 +00004947 type = XML_REGEXP_NAMECHAR;
4948 break;
Daniel Veillardf8e3db02012-09-11 13:26:36 +08004949 case 'C':
Daniel Veillard4255d502002-04-16 15:50:10 +00004950 type = XML_REGEXP_NOTNAMECHAR;
4951 break;
Daniel Veillardf8e3db02012-09-11 13:26:36 +08004952 case 'd':
Daniel Veillard4255d502002-04-16 15:50:10 +00004953 type = XML_REGEXP_DECIMAL;
4954 break;
Daniel Veillardf8e3db02012-09-11 13:26:36 +08004955 case 'D':
Daniel Veillard4255d502002-04-16 15:50:10 +00004956 type = XML_REGEXP_NOTDECIMAL;
4957 break;
Daniel Veillardf8e3db02012-09-11 13:26:36 +08004958 case 'w':
Daniel Veillard4255d502002-04-16 15:50:10 +00004959 type = XML_REGEXP_REALCHAR;
4960 break;
Daniel Veillardf8e3db02012-09-11 13:26:36 +08004961 case 'W':
Daniel Veillard4255d502002-04-16 15:50:10 +00004962 type = XML_REGEXP_NOTREALCHAR;
4963 break;
4964 }
4965 NEXT;
4966 if (ctxt->atom == NULL) {
4967 ctxt->atom = xmlRegNewAtom(ctxt, type);
4968 } else if (ctxt->atom->type == XML_REGEXP_RANGES) {
4969 xmlRegAtomAddRange(ctxt, ctxt->atom, ctxt->neg,
4970 type, 0, 0, NULL);
4971 }
Daniel Veillardcb4284e2007-04-25 13:55:20 +00004972 } else {
4973 ERROR("Wrong escape sequence, misuse of character '\\'");
Daniel Veillard4255d502002-04-16 15:50:10 +00004974 }
4975}
4976
4977/**
Daniel Veillard4255d502002-04-16 15:50:10 +00004978 * xmlFAParseCharRange:
Daniel Veillard441bc322002-04-20 17:38:48 +00004979 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00004980 *
Daniel Veillardf8e3db02012-09-11 13:26:36 +08004981 * [17] charRange ::= seRange | XmlCharRef | XmlCharIncDash
Daniel Veillard4255d502002-04-16 15:50:10 +00004982 * [18] seRange ::= charOrEsc '-' charOrEsc
4983 * [20] charOrEsc ::= XmlChar | SingleCharEsc
4984 * [21] XmlChar ::= [^\#x2D#x5B#x5D]
4985 * [22] XmlCharIncDash ::= [^\#x5B#x5D]
4986 */
4987static void
4988xmlFAParseCharRange(xmlRegParserCtxtPtr ctxt) {
William M. Brackdc99df92003-12-27 01:54:25 +00004989 int cur, len;
Daniel Veillard4255d502002-04-16 15:50:10 +00004990 int start = -1;
4991 int end = -1;
4992
Daniel Veillard777737e2006-10-17 21:23:17 +00004993 if (CUR == '\0') {
4994 ERROR("Expecting ']'");
4995 return;
4996 }
4997
Daniel Veillard4255d502002-04-16 15:50:10 +00004998 cur = CUR;
4999 if (cur == '\\') {
5000 NEXT;
5001 cur = CUR;
5002 switch (cur) {
5003 case 'n': start = 0xA; break;
5004 case 'r': start = 0xD; break;
5005 case 't': start = 0x9; break;
5006 case '\\': case '|': case '.': case '-': case '^': case '?':
5007 case '*': case '+': case '{': case '}': case '(': case ')':
5008 case '[': case ']':
5009 start = cur; break;
5010 default:
5011 ERROR("Invalid escape value");
5012 return;
5013 }
5014 end = start;
William M. Brackdc99df92003-12-27 01:54:25 +00005015 len = 1;
Daniel Veillard4255d502002-04-16 15:50:10 +00005016 } else if ((cur != 0x5B) && (cur != 0x5D)) {
William M. Brackdc99df92003-12-27 01:54:25 +00005017 end = start = CUR_SCHAR(ctxt->cur, len);
Daniel Veillard4255d502002-04-16 15:50:10 +00005018 } else {
5019 ERROR("Expecting a char range");
5020 return;
5021 }
William M. Bracka9cbf282007-03-21 13:16:33 +00005022 /*
5023 * Since we are "inside" a range, we can assume ctxt->cur is past
5024 * the start of ctxt->string, and PREV should be safe
5025 */
5026 if ((start == '-') && (NXT(1) != ']') && (PREV != '[') && (PREV != '^')) {
5027 NEXTL(len);
Daniel Veillard4255d502002-04-16 15:50:10 +00005028 return;
5029 }
William M. Bracka9cbf282007-03-21 13:16:33 +00005030 NEXTL(len);
Daniel Veillard4255d502002-04-16 15:50:10 +00005031 cur = CUR;
William M. Brack10f1ef42004-03-20 14:51:25 +00005032 if ((cur != '-') || (NXT(1) == ']')) {
Daniel Veillard4255d502002-04-16 15:50:10 +00005033 xmlRegAtomAddRange(ctxt, ctxt->atom, ctxt->neg,
5034 XML_REGEXP_CHARVAL, start, end, NULL);
5035 return;
5036 }
5037 NEXT;
5038 cur = CUR;
5039 if (cur == '\\') {
5040 NEXT;
5041 cur = CUR;
5042 switch (cur) {
5043 case 'n': end = 0xA; break;
5044 case 'r': end = 0xD; break;
5045 case 't': end = 0x9; break;
5046 case '\\': case '|': case '.': case '-': case '^': case '?':
5047 case '*': case '+': case '{': case '}': case '(': case ')':
5048 case '[': case ']':
5049 end = cur; break;
5050 default:
5051 ERROR("Invalid escape value");
5052 return;
5053 }
William M. Brackdc99df92003-12-27 01:54:25 +00005054 len = 1;
Daniel Veillard4255d502002-04-16 15:50:10 +00005055 } else if ((cur != 0x5B) && (cur != 0x5D)) {
William M. Brackdc99df92003-12-27 01:54:25 +00005056 end = CUR_SCHAR(ctxt->cur, len);
Daniel Veillard4255d502002-04-16 15:50:10 +00005057 } else {
5058 ERROR("Expecting the end of a char range");
5059 return;
5060 }
Pranjal Jumdecbb27162016-03-07 06:34:26 -08005061
Daniel Veillard4255d502002-04-16 15:50:10 +00005062 /* TODO check that the values are acceptable character ranges for XML */
5063 if (end < start) {
5064 ERROR("End of range is before start of range");
5065 } else {
Pranjal Jumdecbb27162016-03-07 06:34:26 -08005066 NEXTL(len);
Daniel Veillard4255d502002-04-16 15:50:10 +00005067 xmlRegAtomAddRange(ctxt, ctxt->atom, ctxt->neg,
5068 XML_REGEXP_CHARVAL, start, end, NULL);
5069 }
5070 return;
5071}
5072
5073/**
5074 * xmlFAParsePosCharGroup:
Daniel Veillard441bc322002-04-20 17:38:48 +00005075 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00005076 *
5077 * [14] posCharGroup ::= ( charRange | charClassEsc )+
5078 */
5079static void
5080xmlFAParsePosCharGroup(xmlRegParserCtxtPtr ctxt) {
5081 do {
Daniel Veillard041b6872008-02-08 10:37:18 +00005082 if (CUR == '\\') {
Daniel Veillard4255d502002-04-16 15:50:10 +00005083 xmlFAParseCharClassEsc(ctxt);
5084 } else {
5085 xmlFAParseCharRange(ctxt);
5086 }
5087 } while ((CUR != ']') && (CUR != '^') && (CUR != '-') &&
Daniel Veillard777737e2006-10-17 21:23:17 +00005088 (CUR != 0) && (ctxt->error == 0));
Daniel Veillard4255d502002-04-16 15:50:10 +00005089}
5090
5091/**
5092 * xmlFAParseCharGroup:
Daniel Veillard441bc322002-04-20 17:38:48 +00005093 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00005094 *
5095 * [13] charGroup ::= posCharGroup | negCharGroup | charClassSub
5096 * [15] negCharGroup ::= '^' posCharGroup
Daniel Veillardf8e3db02012-09-11 13:26:36 +08005097 * [16] charClassSub ::= ( posCharGroup | negCharGroup ) '-' charClassExpr
Daniel Veillard4255d502002-04-16 15:50:10 +00005098 * [12] charClassExpr ::= '[' charGroup ']'
5099 */
5100static void
5101xmlFAParseCharGroup(xmlRegParserCtxtPtr ctxt) {
5102 int n = ctxt->neg;
5103 while ((CUR != ']') && (ctxt->error == 0)) {
5104 if (CUR == '^') {
5105 int neg = ctxt->neg;
5106
5107 NEXT;
5108 ctxt->neg = !ctxt->neg;
5109 xmlFAParsePosCharGroup(ctxt);
5110 ctxt->neg = neg;
William M. Brack10f1ef42004-03-20 14:51:25 +00005111 } else if ((CUR == '-') && (NXT(1) == '[')) {
Daniel Veillardf8b9de32003-11-24 14:27:26 +00005112 int neg = ctxt->neg;
Daniel Veillardf8b9de32003-11-24 14:27:26 +00005113 ctxt->neg = 2;
William M. Brack10f1ef42004-03-20 14:51:25 +00005114 NEXT; /* eat the '-' */
5115 NEXT; /* eat the '[' */
Daniel Veillard4255d502002-04-16 15:50:10 +00005116 xmlFAParseCharGroup(ctxt);
5117 if (CUR == ']') {
5118 NEXT;
5119 } else {
5120 ERROR("charClassExpr: ']' expected");
5121 break;
5122 }
Daniel Veillardf8b9de32003-11-24 14:27:26 +00005123 ctxt->neg = neg;
Daniel Veillard4255d502002-04-16 15:50:10 +00005124 break;
5125 } else if (CUR != ']') {
5126 xmlFAParsePosCharGroup(ctxt);
5127 }
5128 }
5129 ctxt->neg = n;
5130}
5131
5132/**
5133 * xmlFAParseCharClass:
Daniel Veillard441bc322002-04-20 17:38:48 +00005134 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00005135 *
5136 * [11] charClass ::= charClassEsc | charClassExpr
5137 * [12] charClassExpr ::= '[' charGroup ']'
5138 */
5139static void
5140xmlFAParseCharClass(xmlRegParserCtxtPtr ctxt) {
5141 if (CUR == '[') {
5142 NEXT;
5143 ctxt->atom = xmlRegNewAtom(ctxt, XML_REGEXP_RANGES);
5144 if (ctxt->atom == NULL)
5145 return;
5146 xmlFAParseCharGroup(ctxt);
5147 if (CUR == ']') {
5148 NEXT;
5149 } else {
5150 ERROR("xmlFAParseCharClass: ']' expected");
5151 }
5152 } else {
5153 xmlFAParseCharClassEsc(ctxt);
5154 }
5155}
5156
5157/**
5158 * xmlFAParseQuantExact:
Daniel Veillard441bc322002-04-20 17:38:48 +00005159 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00005160 *
5161 * [8] QuantExact ::= [0-9]+
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00005162 *
5163 * Returns 0 if success or -1 in case of error
Daniel Veillard4255d502002-04-16 15:50:10 +00005164 */
5165static int
5166xmlFAParseQuantExact(xmlRegParserCtxtPtr ctxt) {
5167 int ret = 0;
5168 int ok = 0;
5169
5170 while ((CUR >= '0') && (CUR <= '9')) {
5171 ret = ret * 10 + (CUR - '0');
5172 ok = 1;
5173 NEXT;
5174 }
5175 if (ok != 1) {
5176 return(-1);
5177 }
5178 return(ret);
5179}
5180
5181/**
5182 * xmlFAParseQuantifier:
Daniel Veillard441bc322002-04-20 17:38:48 +00005183 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00005184 *
5185 * [4] quantifier ::= [?*+] | ( '{' quantity '}' )
5186 * [5] quantity ::= quantRange | quantMin | QuantExact
5187 * [6] quantRange ::= QuantExact ',' QuantExact
5188 * [7] quantMin ::= QuantExact ','
5189 * [8] QuantExact ::= [0-9]+
5190 */
5191static int
5192xmlFAParseQuantifier(xmlRegParserCtxtPtr ctxt) {
5193 int cur;
5194
5195 cur = CUR;
5196 if ((cur == '?') || (cur == '*') || (cur == '+')) {
5197 if (ctxt->atom != NULL) {
5198 if (cur == '?')
5199 ctxt->atom->quant = XML_REGEXP_QUANT_OPT;
5200 else if (cur == '*')
5201 ctxt->atom->quant = XML_REGEXP_QUANT_MULT;
5202 else if (cur == '+')
5203 ctxt->atom->quant = XML_REGEXP_QUANT_PLUS;
5204 }
5205 NEXT;
5206 return(1);
5207 }
5208 if (cur == '{') {
5209 int min = 0, max = 0;
5210
5211 NEXT;
5212 cur = xmlFAParseQuantExact(ctxt);
5213 if (cur >= 0)
5214 min = cur;
5215 if (CUR == ',') {
5216 NEXT;
Daniel Veillardebe48c62003-12-03 12:12:27 +00005217 if (CUR == '}')
5218 max = INT_MAX;
5219 else {
5220 cur = xmlFAParseQuantExact(ctxt);
5221 if (cur >= 0)
5222 max = cur;
5223 else {
5224 ERROR("Improper quantifier");
5225 }
5226 }
Daniel Veillard4255d502002-04-16 15:50:10 +00005227 }
5228 if (CUR == '}') {
5229 NEXT;
5230 } else {
5231 ERROR("Unterminated quantifier");
5232 }
5233 if (max == 0)
5234 max = min;
5235 if (ctxt->atom != NULL) {
5236 ctxt->atom->quant = XML_REGEXP_QUANT_RANGE;
5237 ctxt->atom->min = min;
5238 ctxt->atom->max = max;
5239 }
5240 return(1);
5241 }
5242 return(0);
5243}
5244
5245/**
5246 * xmlFAParseAtom:
Daniel Veillard441bc322002-04-20 17:38:48 +00005247 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00005248 *
5249 * [9] atom ::= Char | charClass | ( '(' regExp ')' )
5250 */
5251static int
5252xmlFAParseAtom(xmlRegParserCtxtPtr ctxt) {
5253 int codepoint, len;
5254
5255 codepoint = xmlFAIsChar(ctxt);
5256 if (codepoint > 0) {
5257 ctxt->atom = xmlRegNewAtom(ctxt, XML_REGEXP_CHARVAL);
5258 if (ctxt->atom == NULL)
5259 return(-1);
5260 codepoint = CUR_SCHAR(ctxt->cur, len);
5261 ctxt->atom->codepoint = codepoint;
5262 NEXTL(len);
5263 return(1);
5264 } else if (CUR == '|') {
5265 return(0);
5266 } else if (CUR == 0) {
5267 return(0);
5268 } else if (CUR == ')') {
5269 return(0);
5270 } else if (CUR == '(') {
Daniel Veillard76d59b62007-08-22 16:29:21 +00005271 xmlRegStatePtr start, oldend, start0;
Daniel Veillard4255d502002-04-16 15:50:10 +00005272
5273 NEXT;
Daniel Veillard76d59b62007-08-22 16:29:21 +00005274 /*
5275 * this extra Epsilon transition is needed if we count with 0 allowed
5276 * unfortunately this can't be known at that point
5277 */
5278 xmlFAGenerateEpsilonTransition(ctxt, ctxt->state, NULL);
5279 start0 = ctxt->state;
Daniel Veillard4255d502002-04-16 15:50:10 +00005280 xmlFAGenerateEpsilonTransition(ctxt, ctxt->state, NULL);
5281 start = ctxt->state;
5282 oldend = ctxt->end;
5283 ctxt->end = NULL;
5284 ctxt->atom = NULL;
5285 xmlFAParseRegExp(ctxt, 0);
5286 if (CUR == ')') {
5287 NEXT;
5288 } else {
5289 ERROR("xmlFAParseAtom: expecting ')'");
5290 }
5291 ctxt->atom = xmlRegNewAtom(ctxt, XML_REGEXP_SUBREG);
5292 if (ctxt->atom == NULL)
5293 return(-1);
5294 ctxt->atom->start = start;
Daniel Veillard76d59b62007-08-22 16:29:21 +00005295 ctxt->atom->start0 = start0;
Daniel Veillard4255d502002-04-16 15:50:10 +00005296 ctxt->atom->stop = ctxt->state;
5297 ctxt->end = oldend;
5298 return(1);
5299 } else if ((CUR == '[') || (CUR == '\\') || (CUR == '.')) {
5300 xmlFAParseCharClass(ctxt);
5301 return(1);
5302 }
5303 return(0);
5304}
5305
5306/**
5307 * xmlFAParsePiece:
Daniel Veillard441bc322002-04-20 17:38:48 +00005308 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00005309 *
5310 * [3] piece ::= atom quantifier?
5311 */
5312static int
5313xmlFAParsePiece(xmlRegParserCtxtPtr ctxt) {
5314 int ret;
5315
5316 ctxt->atom = NULL;
5317 ret = xmlFAParseAtom(ctxt);
5318 if (ret == 0)
5319 return(0);
5320 if (ctxt->atom == NULL) {
5321 ERROR("internal: no atom generated");
5322 }
5323 xmlFAParseQuantifier(ctxt);
5324 return(1);
5325}
5326
5327/**
5328 * xmlFAParseBranch:
Daniel Veillard441bc322002-04-20 17:38:48 +00005329 * @ctxt: a regexp parser context
Daniel Veillard54eb0242006-03-21 23:17:57 +00005330 * @to: optional target to the end of the branch
5331 *
5332 * @to is used to optimize by removing duplicate path in automata
5333 * in expressions like (a|b)(c|d)
Daniel Veillard4255d502002-04-16 15:50:10 +00005334 *
5335 * [2] branch ::= piece*
5336 */
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00005337static int
Daniel Veillard54eb0242006-03-21 23:17:57 +00005338xmlFAParseBranch(xmlRegParserCtxtPtr ctxt, xmlRegStatePtr to) {
Daniel Veillard4255d502002-04-16 15:50:10 +00005339 xmlRegStatePtr previous;
Daniel Veillard4255d502002-04-16 15:50:10 +00005340 int ret;
5341
5342 previous = ctxt->state;
5343 ret = xmlFAParsePiece(ctxt);
5344 if (ret != 0) {
Daniel Veillardf8e3db02012-09-11 13:26:36 +08005345 if (xmlFAGenerateTransitions(ctxt, previous,
Daniel Veillard54eb0242006-03-21 23:17:57 +00005346 (CUR=='|' || CUR==')') ? to : NULL, ctxt->atom) < 0)
Daniel Veillard2cbf5962004-03-31 15:50:43 +00005347 return(-1);
5348 previous = ctxt->state;
Daniel Veillard4255d502002-04-16 15:50:10 +00005349 ctxt->atom = NULL;
5350 }
5351 while ((ret != 0) && (ctxt->error == 0)) {
5352 ret = xmlFAParsePiece(ctxt);
5353 if (ret != 0) {
Daniel Veillardf8e3db02012-09-11 13:26:36 +08005354 if (xmlFAGenerateTransitions(ctxt, previous,
Daniel Veillard54eb0242006-03-21 23:17:57 +00005355 (CUR=='|' || CUR==')') ? to : NULL, ctxt->atom) < 0)
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00005356 return(-1);
Daniel Veillard4255d502002-04-16 15:50:10 +00005357 previous = ctxt->state;
5358 ctxt->atom = NULL;
5359 }
5360 }
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00005361 return(0);
Daniel Veillard4255d502002-04-16 15:50:10 +00005362}
5363
5364/**
5365 * xmlFAParseRegExp:
Daniel Veillard441bc322002-04-20 17:38:48 +00005366 * @ctxt: a regexp parser context
William M. Brackddf71d62004-05-06 04:17:26 +00005367 * @top: is this the top-level expression ?
Daniel Veillard4255d502002-04-16 15:50:10 +00005368 *
5369 * [1] regExp ::= branch ( '|' branch )*
5370 */
5371static void
5372xmlFAParseRegExp(xmlRegParserCtxtPtr ctxt, int top) {
Daniel Veillardc7e3cc42004-09-28 12:33:52 +00005373 xmlRegStatePtr start, end;
Daniel Veillard4255d502002-04-16 15:50:10 +00005374
Daniel Veillard2cbf5962004-03-31 15:50:43 +00005375 /* if not top start should have been generated by an epsilon trans */
Daniel Veillard4255d502002-04-16 15:50:10 +00005376 start = ctxt->state;
Daniel Veillard2cbf5962004-03-31 15:50:43 +00005377 ctxt->end = NULL;
Daniel Veillard54eb0242006-03-21 23:17:57 +00005378 xmlFAParseBranch(ctxt, NULL);
Daniel Veillard2cbf5962004-03-31 15:50:43 +00005379 if (top) {
5380#ifdef DEBUG_REGEXP_GRAPH
5381 printf("State %d is final\n", ctxt->state->no);
5382#endif
5383 ctxt->state->type = XML_REGEXP_FINAL_STATE;
5384 }
Daniel Veillard4255d502002-04-16 15:50:10 +00005385 if (CUR != '|') {
5386 ctxt->end = ctxt->state;
5387 return;
5388 }
5389 end = ctxt->state;
5390 while ((CUR == '|') && (ctxt->error == 0)) {
5391 NEXT;
Daniel Veillard40851d02012-08-17 20:34:05 +08005392 if (CUR == 0) {
5393 ERROR("expecting a branch after |")
5394 return;
5395 }
Daniel Veillard4255d502002-04-16 15:50:10 +00005396 ctxt->state = start;
Daniel Veillard2cbf5962004-03-31 15:50:43 +00005397 ctxt->end = NULL;
Daniel Veillard54eb0242006-03-21 23:17:57 +00005398 xmlFAParseBranch(ctxt, end);
Daniel Veillard4255d502002-04-16 15:50:10 +00005399 }
Daniel Veillard2cbf5962004-03-31 15:50:43 +00005400 if (!top) {
5401 ctxt->state = end;
5402 ctxt->end = end;
5403 }
Daniel Veillard4255d502002-04-16 15:50:10 +00005404}
5405
5406/************************************************************************
Daniel Veillardf8e3db02012-09-11 13:26:36 +08005407 * *
5408 * The basic API *
5409 * *
Daniel Veillard4255d502002-04-16 15:50:10 +00005410 ************************************************************************/
5411
5412/**
5413 * xmlRegexpPrint:
5414 * @output: the file for the output debug
5415 * @regexp: the compiled regexp
5416 *
5417 * Print the content of the compiled regular expression
5418 */
5419void
5420xmlRegexpPrint(FILE *output, xmlRegexpPtr regexp) {
5421 int i;
5422
Daniel Veillarda82b1822004-11-08 16:24:57 +00005423 if (output == NULL)
5424 return;
Daniel Veillard4255d502002-04-16 15:50:10 +00005425 fprintf(output, " regexp: ");
5426 if (regexp == NULL) {
5427 fprintf(output, "NULL\n");
5428 return;
5429 }
5430 fprintf(output, "'%s' ", regexp->string);
5431 fprintf(output, "\n");
5432 fprintf(output, "%d atoms:\n", regexp->nbAtoms);
5433 for (i = 0;i < regexp->nbAtoms; i++) {
5434 fprintf(output, " %02d ", i);
5435 xmlRegPrintAtom(output, regexp->atoms[i]);
5436 }
5437 fprintf(output, "%d states:", regexp->nbStates);
5438 fprintf(output, "\n");
5439 for (i = 0;i < regexp->nbStates; i++) {
5440 xmlRegPrintState(output, regexp->states[i]);
5441 }
5442 fprintf(output, "%d counters:\n", regexp->nbCounters);
5443 for (i = 0;i < regexp->nbCounters; i++) {
5444 fprintf(output, " %d: min %d max %d\n", i, regexp->counters[i].min,
5445 regexp->counters[i].max);
5446 }
5447}
5448
5449/**
5450 * xmlRegexpCompile:
5451 * @regexp: a regular expression string
5452 *
5453 * Parses a regular expression conforming to XML Schemas Part 2 Datatype
William M. Brackddf71d62004-05-06 04:17:26 +00005454 * Appendix F and builds an automata suitable for testing strings against
Daniel Veillard4255d502002-04-16 15:50:10 +00005455 * that regular expression
5456 *
5457 * Returns the compiled expression or NULL in case of error
5458 */
5459xmlRegexpPtr
5460xmlRegexpCompile(const xmlChar *regexp) {
5461 xmlRegexpPtr ret;
5462 xmlRegParserCtxtPtr ctxt;
5463
5464 ctxt = xmlRegNewParserCtxt(regexp);
5465 if (ctxt == NULL)
5466 return(NULL);
5467
5468 /* initialize the parser */
5469 ctxt->end = NULL;
5470 ctxt->start = ctxt->state = xmlRegNewState(ctxt);
5471 xmlRegStatePush(ctxt, ctxt->start);
5472
5473 /* parse the expression building an automata */
5474 xmlFAParseRegExp(ctxt, 1);
5475 if (CUR != 0) {
5476 ERROR("xmlFAParseRegExp: extra characters");
5477 }
Daniel Veillardcb4284e2007-04-25 13:55:20 +00005478 if (ctxt->error != 0) {
5479 xmlRegFreeParserCtxt(ctxt);
5480 return(NULL);
5481 }
Daniel Veillard4255d502002-04-16 15:50:10 +00005482 ctxt->end = ctxt->state;
5483 ctxt->start->type = XML_REGEXP_START_STATE;
5484 ctxt->end->type = XML_REGEXP_FINAL_STATE;
5485
5486 /* remove the Epsilon except for counted transitions */
5487 xmlFAEliminateEpsilonTransitions(ctxt);
5488
5489
5490 if (ctxt->error != 0) {
5491 xmlRegFreeParserCtxt(ctxt);
5492 return(NULL);
5493 }
5494 ret = xmlRegEpxFromParse(ctxt);
5495 xmlRegFreeParserCtxt(ctxt);
5496 return(ret);
5497}
5498
5499/**
5500 * xmlRegexpExec:
5501 * @comp: the compiled regular expression
5502 * @content: the value to check against the regular expression
5503 *
William M. Brackddf71d62004-05-06 04:17:26 +00005504 * Check if the regular expression generates the value
Daniel Veillard4255d502002-04-16 15:50:10 +00005505 *
William M. Brackddf71d62004-05-06 04:17:26 +00005506 * Returns 1 if it matches, 0 if not and a negative value in case of error
Daniel Veillard4255d502002-04-16 15:50:10 +00005507 */
5508int
5509xmlRegexpExec(xmlRegexpPtr comp, const xmlChar *content) {
5510 if ((comp == NULL) || (content == NULL))
5511 return(-1);
5512 return(xmlFARegExec(comp, content));
5513}
5514
5515/**
Daniel Veillard23e73572002-09-19 19:56:43 +00005516 * xmlRegexpIsDeterminist:
5517 * @comp: the compiled regular expression
5518 *
5519 * Check if the regular expression is determinist
5520 *
William M. Brackddf71d62004-05-06 04:17:26 +00005521 * Returns 1 if it yes, 0 if not and a negative value in case of error
Daniel Veillard23e73572002-09-19 19:56:43 +00005522 */
5523int
5524xmlRegexpIsDeterminist(xmlRegexpPtr comp) {
5525 xmlAutomataPtr am;
5526 int ret;
5527
5528 if (comp == NULL)
5529 return(-1);
5530 if (comp->determinist != -1)
5531 return(comp->determinist);
5532
5533 am = xmlNewAutomata();
Daniel Veillardbd9afb52002-09-25 22:25:35 +00005534 if (am->states != NULL) {
5535 int i;
5536
5537 for (i = 0;i < am->nbStates;i++)
5538 xmlRegFreeState(am->states[i]);
5539 xmlFree(am->states);
5540 }
Daniel Veillard23e73572002-09-19 19:56:43 +00005541 am->nbAtoms = comp->nbAtoms;
5542 am->atoms = comp->atoms;
5543 am->nbStates = comp->nbStates;
5544 am->states = comp->states;
5545 am->determinist = -1;
Daniel Veillard1ba2aca2009-08-31 16:47:39 +02005546 am->flags = comp->flags;
Daniel Veillard23e73572002-09-19 19:56:43 +00005547 ret = xmlFAComputesDeterminism(am);
5548 am->atoms = NULL;
5549 am->states = NULL;
5550 xmlFreeAutomata(am);
Daniel Veillard1ba2aca2009-08-31 16:47:39 +02005551 comp->determinist = ret;
Daniel Veillard23e73572002-09-19 19:56:43 +00005552 return(ret);
5553}
5554
5555/**
Daniel Veillard4255d502002-04-16 15:50:10 +00005556 * xmlRegFreeRegexp:
5557 * @regexp: the regexp
5558 *
5559 * Free a regexp
5560 */
5561void
5562xmlRegFreeRegexp(xmlRegexpPtr regexp) {
5563 int i;
5564 if (regexp == NULL)
5565 return;
5566
5567 if (regexp->string != NULL)
5568 xmlFree(regexp->string);
5569 if (regexp->states != NULL) {
5570 for (i = 0;i < regexp->nbStates;i++)
5571 xmlRegFreeState(regexp->states[i]);
5572 xmlFree(regexp->states);
5573 }
5574 if (regexp->atoms != NULL) {
5575 for (i = 0;i < regexp->nbAtoms;i++)
5576 xmlRegFreeAtom(regexp->atoms[i]);
5577 xmlFree(regexp->atoms);
5578 }
5579 if (regexp->counters != NULL)
5580 xmlFree(regexp->counters);
Daniel Veillard23e73572002-09-19 19:56:43 +00005581 if (regexp->compact != NULL)
5582 xmlFree(regexp->compact);
Daniel Veillard118aed72002-09-24 14:13:13 +00005583 if (regexp->transdata != NULL)
5584 xmlFree(regexp->transdata);
Daniel Veillard23e73572002-09-19 19:56:43 +00005585 if (regexp->stringMap != NULL) {
5586 for (i = 0; i < regexp->nbstrings;i++)
5587 xmlFree(regexp->stringMap[i]);
5588 xmlFree(regexp->stringMap);
5589 }
5590
Daniel Veillard4255d502002-04-16 15:50:10 +00005591 xmlFree(regexp);
5592}
5593
5594#ifdef LIBXML_AUTOMATA_ENABLED
5595/************************************************************************
Daniel Veillardf8e3db02012-09-11 13:26:36 +08005596 * *
5597 * The Automata interface *
5598 * *
Daniel Veillard4255d502002-04-16 15:50:10 +00005599 ************************************************************************/
5600
5601/**
5602 * xmlNewAutomata:
5603 *
5604 * Create a new automata
5605 *
5606 * Returns the new object or NULL in case of failure
5607 */
5608xmlAutomataPtr
5609xmlNewAutomata(void) {
5610 xmlAutomataPtr ctxt;
5611
5612 ctxt = xmlRegNewParserCtxt(NULL);
5613 if (ctxt == NULL)
5614 return(NULL);
5615
5616 /* initialize the parser */
5617 ctxt->end = NULL;
5618 ctxt->start = ctxt->state = xmlRegNewState(ctxt);
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00005619 if (ctxt->start == NULL) {
5620 xmlFreeAutomata(ctxt);
5621 return(NULL);
5622 }
Daniel Veillardd0271472006-01-02 10:22:02 +00005623 ctxt->start->type = XML_REGEXP_START_STATE;
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00005624 if (xmlRegStatePush(ctxt, ctxt->start) < 0) {
5625 xmlRegFreeState(ctxt->start);
5626 xmlFreeAutomata(ctxt);
5627 return(NULL);
5628 }
Daniel Veillard1ba2aca2009-08-31 16:47:39 +02005629 ctxt->flags = 0;
Daniel Veillard4255d502002-04-16 15:50:10 +00005630
5631 return(ctxt);
5632}
5633
5634/**
5635 * xmlFreeAutomata:
5636 * @am: an automata
5637 *
5638 * Free an automata
5639 */
5640void
5641xmlFreeAutomata(xmlAutomataPtr am) {
5642 if (am == NULL)
5643 return;
5644 xmlRegFreeParserCtxt(am);
5645}
5646
5647/**
Daniel Veillard29341682009-09-10 18:23:39 +02005648 * xmlAutomataSetFlags:
Daniel Veillard1ba2aca2009-08-31 16:47:39 +02005649 * @am: an automata
5650 * @flags: a set of internal flags
5651 *
5652 * Set some flags on the automata
5653 */
5654void
5655xmlAutomataSetFlags(xmlAutomataPtr am, int flags) {
5656 if (am == NULL)
5657 return;
5658 am->flags |= flags;
5659}
5660
5661/**
Daniel Veillard4255d502002-04-16 15:50:10 +00005662 * xmlAutomataGetInitState:
5663 * @am: an automata
5664 *
Daniel Veillarda9b66d02002-12-11 14:23:49 +00005665 * Initial state lookup
5666 *
Daniel Veillard4255d502002-04-16 15:50:10 +00005667 * Returns the initial state of the automata
5668 */
5669xmlAutomataStatePtr
5670xmlAutomataGetInitState(xmlAutomataPtr am) {
5671 if (am == NULL)
5672 return(NULL);
5673 return(am->start);
5674}
5675
5676/**
5677 * xmlAutomataSetFinalState:
5678 * @am: an automata
5679 * @state: a state in this automata
5680 *
5681 * Makes that state a final state
5682 *
5683 * Returns 0 or -1 in case of error
5684 */
5685int
5686xmlAutomataSetFinalState(xmlAutomataPtr am, xmlAutomataStatePtr state) {
5687 if ((am == NULL) || (state == NULL))
5688 return(-1);
5689 state->type = XML_REGEXP_FINAL_STATE;
5690 return(0);
5691}
5692
5693/**
5694 * xmlAutomataNewTransition:
5695 * @am: an automata
5696 * @from: the starting point of the transition
5697 * @to: the target point of the transition or NULL
5698 * @token: the input string associated to that transition
5699 * @data: data passed to the callback function if the transition is activated
5700 *
William M. Brackddf71d62004-05-06 04:17:26 +00005701 * If @to is NULL, this creates first a new target state in the automata
Daniel Veillard4255d502002-04-16 15:50:10 +00005702 * and then adds a transition from the @from state to the target state
5703 * activated by the value of @token
5704 *
5705 * Returns the target state or NULL in case of error
5706 */
5707xmlAutomataStatePtr
5708xmlAutomataNewTransition(xmlAutomataPtr am, xmlAutomataStatePtr from,
5709 xmlAutomataStatePtr to, const xmlChar *token,
5710 void *data) {
5711 xmlRegAtomPtr atom;
5712
5713 if ((am == NULL) || (from == NULL) || (token == NULL))
5714 return(NULL);
5715 atom = xmlRegNewAtom(am, XML_REGEXP_STRING);
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00005716 if (atom == NULL)
5717 return(NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +00005718 atom->data = data;
Daniel Veillard4255d502002-04-16 15:50:10 +00005719 atom->valuep = xmlStrdup(token);
5720
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00005721 if (xmlFAGenerateTransitions(am, from, to, atom) < 0) {
5722 xmlRegFreeAtom(atom);
5723 return(NULL);
5724 }
Daniel Veillard4255d502002-04-16 15:50:10 +00005725 if (to == NULL)
5726 return(am->state);
5727 return(to);
5728}
5729
5730/**
Daniel Veillard52b48c72003-04-13 19:53:42 +00005731 * xmlAutomataNewTransition2:
5732 * @am: an automata
5733 * @from: the starting point of the transition
5734 * @to: the target point of the transition or NULL
5735 * @token: the first input string associated to that transition
5736 * @token2: the second input string associated to that transition
5737 * @data: data passed to the callback function if the transition is activated
5738 *
William M. Brackddf71d62004-05-06 04:17:26 +00005739 * If @to is NULL, this creates first a new target state in the automata
Daniel Veillard52b48c72003-04-13 19:53:42 +00005740 * and then adds a transition from the @from state to the target state
5741 * activated by the value of @token
5742 *
5743 * Returns the target state or NULL in case of error
5744 */
5745xmlAutomataStatePtr
5746xmlAutomataNewTransition2(xmlAutomataPtr am, xmlAutomataStatePtr from,
5747 xmlAutomataStatePtr to, const xmlChar *token,
5748 const xmlChar *token2, void *data) {
5749 xmlRegAtomPtr atom;
5750
5751 if ((am == NULL) || (from == NULL) || (token == NULL))
5752 return(NULL);
5753 atom = xmlRegNewAtom(am, XML_REGEXP_STRING);
Daniel Veillard52b48c72003-04-13 19:53:42 +00005754 if (atom == NULL)
5755 return(NULL);
Daniel Veillard11ce4002006-03-10 00:36:23 +00005756 atom->data = data;
Daniel Veillard52b48c72003-04-13 19:53:42 +00005757 if ((token2 == NULL) || (*token2 == 0)) {
5758 atom->valuep = xmlStrdup(token);
5759 } else {
5760 int lenn, lenp;
5761 xmlChar *str;
5762
5763 lenn = strlen((char *) token2);
5764 lenp = strlen((char *) token);
5765
Daniel Veillard3c908dc2003-04-19 00:07:51 +00005766 str = (xmlChar *) xmlMallocAtomic(lenn + lenp + 2);
Daniel Veillard52b48c72003-04-13 19:53:42 +00005767 if (str == NULL) {
5768 xmlRegFreeAtom(atom);
5769 return(NULL);
5770 }
5771 memcpy(&str[0], token, lenp);
5772 str[lenp] = '|';
5773 memcpy(&str[lenp + 1], token2, lenn);
5774 str[lenn + lenp + 1] = 0;
5775
5776 atom->valuep = str;
5777 }
5778
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00005779 if (xmlFAGenerateTransitions(am, from, to, atom) < 0) {
5780 xmlRegFreeAtom(atom);
5781 return(NULL);
5782 }
Daniel Veillard52b48c72003-04-13 19:53:42 +00005783 if (to == NULL)
5784 return(am->state);
5785 return(to);
5786}
5787
5788/**
Daniel Veillard9efc4762005-07-19 14:33:55 +00005789 * xmlAutomataNewNegTrans:
5790 * @am: an automata
5791 * @from: the starting point of the transition
5792 * @to: the target point of the transition or NULL
5793 * @token: the first input string associated to that transition
5794 * @token2: the second input string associated to that transition
5795 * @data: data passed to the callback function if the transition is activated
5796 *
5797 * If @to is NULL, this creates first a new target state in the automata
5798 * and then adds a transition from the @from state to the target state
5799 * activated by any value except (@token,@token2)
Daniel Veillard6e65e152005-08-09 11:09:52 +00005800 * Note that if @token2 is not NULL, then (X, NULL) won't match to follow
5801 # the semantic of XSD ##other
Daniel Veillard9efc4762005-07-19 14:33:55 +00005802 *
5803 * Returns the target state or NULL in case of error
5804 */
5805xmlAutomataStatePtr
5806xmlAutomataNewNegTrans(xmlAutomataPtr am, xmlAutomataStatePtr from,
5807 xmlAutomataStatePtr to, const xmlChar *token,
5808 const xmlChar *token2, void *data) {
5809 xmlRegAtomPtr atom;
Daniel Veillard77005e62005-07-19 16:26:18 +00005810 xmlChar err_msg[200];
Daniel Veillard9efc4762005-07-19 14:33:55 +00005811
5812 if ((am == NULL) || (from == NULL) || (token == NULL))
5813 return(NULL);
5814 atom = xmlRegNewAtom(am, XML_REGEXP_STRING);
5815 if (atom == NULL)
5816 return(NULL);
5817 atom->data = data;
5818 atom->neg = 1;
5819 if ((token2 == NULL) || (*token2 == 0)) {
5820 atom->valuep = xmlStrdup(token);
5821 } else {
5822 int lenn, lenp;
5823 xmlChar *str;
5824
5825 lenn = strlen((char *) token2);
5826 lenp = strlen((char *) token);
5827
5828 str = (xmlChar *) xmlMallocAtomic(lenn + lenp + 2);
5829 if (str == NULL) {
5830 xmlRegFreeAtom(atom);
5831 return(NULL);
5832 }
5833 memcpy(&str[0], token, lenp);
5834 str[lenp] = '|';
5835 memcpy(&str[lenp + 1], token2, lenn);
5836 str[lenn + lenp + 1] = 0;
5837
5838 atom->valuep = str;
5839 }
Daniel Veillarddb68b742005-07-30 13:18:24 +00005840 snprintf((char *) err_msg, 199, "not %s", (const char *) atom->valuep);
Daniel Veillard77005e62005-07-19 16:26:18 +00005841 err_msg[199] = 0;
5842 atom->valuep2 = xmlStrdup(err_msg);
Daniel Veillard9efc4762005-07-19 14:33:55 +00005843
5844 if (xmlFAGenerateTransitions(am, from, to, atom) < 0) {
5845 xmlRegFreeAtom(atom);
5846 return(NULL);
5847 }
Daniel Veillard6e65e152005-08-09 11:09:52 +00005848 am->negs++;
Daniel Veillard9efc4762005-07-19 14:33:55 +00005849 if (to == NULL)
5850 return(am->state);
5851 return(to);
5852}
5853
5854/**
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00005855 * xmlAutomataNewCountTrans2:
5856 * @am: an automata
5857 * @from: the starting point of the transition
5858 * @to: the target point of the transition or NULL
5859 * @token: the input string associated to that transition
5860 * @token2: the second input string associated to that transition
5861 * @min: the minimum successive occurences of token
5862 * @max: the maximum successive occurences of token
5863 * @data: data associated to the transition
5864 *
5865 * If @to is NULL, this creates first a new target state in the automata
5866 * and then adds a transition from the @from state to the target state
Daniel Veillardf8e3db02012-09-11 13:26:36 +08005867 * activated by a succession of input of value @token and @token2 and
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00005868 * whose number is between @min and @max
5869 *
5870 * Returns the target state or NULL in case of error
5871 */
5872xmlAutomataStatePtr
5873xmlAutomataNewCountTrans2(xmlAutomataPtr am, xmlAutomataStatePtr from,
5874 xmlAutomataStatePtr to, const xmlChar *token,
5875 const xmlChar *token2,
5876 int min, int max, void *data) {
5877 xmlRegAtomPtr atom;
5878 int counter;
5879
5880 if ((am == NULL) || (from == NULL) || (token == NULL))
5881 return(NULL);
5882 if (min < 0)
5883 return(NULL);
5884 if ((max < min) || (max < 1))
5885 return(NULL);
5886 atom = xmlRegNewAtom(am, XML_REGEXP_STRING);
5887 if (atom == NULL)
5888 return(NULL);
5889 if ((token2 == NULL) || (*token2 == 0)) {
5890 atom->valuep = xmlStrdup(token);
5891 } else {
5892 int lenn, lenp;
5893 xmlChar *str;
5894
5895 lenn = strlen((char *) token2);
5896 lenp = strlen((char *) token);
5897
5898 str = (xmlChar *) xmlMallocAtomic(lenn + lenp + 2);
5899 if (str == NULL) {
5900 xmlRegFreeAtom(atom);
5901 return(NULL);
5902 }
5903 memcpy(&str[0], token, lenp);
5904 str[lenp] = '|';
5905 memcpy(&str[lenp + 1], token2, lenn);
5906 str[lenn + lenp + 1] = 0;
5907
5908 atom->valuep = str;
5909 }
5910 atom->data = data;
5911 if (min == 0)
5912 atom->min = 1;
5913 else
5914 atom->min = min;
5915 atom->max = max;
5916
5917 /*
5918 * associate a counter to the transition.
5919 */
5920 counter = xmlRegGetCounter(am);
5921 am->counters[counter].min = min;
5922 am->counters[counter].max = max;
5923
5924 /* xmlFAGenerateTransitions(am, from, to, atom); */
5925 if (to == NULL) {
5926 to = xmlRegNewState(am);
5927 xmlRegStatePush(am, to);
5928 }
Daniel Veillard5de09382005-09-26 17:18:17 +00005929 xmlRegStateAddTrans(am, from, atom, to, counter, -1);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00005930 xmlRegAtomPush(am, atom);
5931 am->state = to;
5932
5933 if (to == NULL)
5934 to = am->state;
5935 if (to == NULL)
5936 return(NULL);
5937 if (min == 0)
5938 xmlFAGenerateEpsilonTransition(am, from, to);
5939 return(to);
5940}
5941
5942/**
Daniel Veillard4255d502002-04-16 15:50:10 +00005943 * xmlAutomataNewCountTrans:
5944 * @am: an automata
5945 * @from: the starting point of the transition
5946 * @to: the target point of the transition or NULL
5947 * @token: the input string associated to that transition
5948 * @min: the minimum successive occurences of token
Daniel Veillarda9b66d02002-12-11 14:23:49 +00005949 * @max: the maximum successive occurences of token
5950 * @data: data associated to the transition
Daniel Veillard4255d502002-04-16 15:50:10 +00005951 *
William M. Brackddf71d62004-05-06 04:17:26 +00005952 * If @to is NULL, this creates first a new target state in the automata
Daniel Veillard4255d502002-04-16 15:50:10 +00005953 * and then adds a transition from the @from state to the target state
5954 * activated by a succession of input of value @token and whose number
5955 * is between @min and @max
5956 *
5957 * Returns the target state or NULL in case of error
5958 */
5959xmlAutomataStatePtr
5960xmlAutomataNewCountTrans(xmlAutomataPtr am, xmlAutomataStatePtr from,
5961 xmlAutomataStatePtr to, const xmlChar *token,
5962 int min, int max, void *data) {
5963 xmlRegAtomPtr atom;
Daniel Veillard0ddb21c2004-02-12 12:43:49 +00005964 int counter;
Daniel Veillard4255d502002-04-16 15:50:10 +00005965
5966 if ((am == NULL) || (from == NULL) || (token == NULL))
5967 return(NULL);
5968 if (min < 0)
5969 return(NULL);
5970 if ((max < min) || (max < 1))
5971 return(NULL);
5972 atom = xmlRegNewAtom(am, XML_REGEXP_STRING);
5973 if (atom == NULL)
5974 return(NULL);
5975 atom->valuep = xmlStrdup(token);
5976 atom->data = data;
5977 if (min == 0)
5978 atom->min = 1;
5979 else
5980 atom->min = min;
5981 atom->max = max;
5982
Daniel Veillard0ddb21c2004-02-12 12:43:49 +00005983 /*
5984 * associate a counter to the transition.
5985 */
5986 counter = xmlRegGetCounter(am);
5987 am->counters[counter].min = min;
5988 am->counters[counter].max = max;
5989
5990 /* xmlFAGenerateTransitions(am, from, to, atom); */
5991 if (to == NULL) {
5992 to = xmlRegNewState(am);
5993 xmlRegStatePush(am, to);
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00005994 }
Daniel Veillard5de09382005-09-26 17:18:17 +00005995 xmlRegStateAddTrans(am, from, atom, to, counter, -1);
Daniel Veillard0ddb21c2004-02-12 12:43:49 +00005996 xmlRegAtomPush(am, atom);
5997 am->state = to;
5998
Daniel Veillard4255d502002-04-16 15:50:10 +00005999 if (to == NULL)
6000 to = am->state;
6001 if (to == NULL)
6002 return(NULL);
6003 if (min == 0)
6004 xmlFAGenerateEpsilonTransition(am, from, to);
6005 return(to);
6006}
6007
6008/**
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00006009 * xmlAutomataNewOnceTrans2:
6010 * @am: an automata
6011 * @from: the starting point of the transition
6012 * @to: the target point of the transition or NULL
6013 * @token: the input string associated to that transition
6014 * @token2: the second input string associated to that transition
6015 * @min: the minimum successive occurences of token
6016 * @max: the maximum successive occurences of token
6017 * @data: data associated to the transition
6018 *
6019 * If @to is NULL, this creates first a new target state in the automata
6020 * and then adds a transition from the @from state to the target state
Daniel Veillardf8e3db02012-09-11 13:26:36 +08006021 * activated by a succession of input of value @token and @token2 and whose
6022 * number is between @min and @max, moreover that transition can only be
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00006023 * crossed once.
6024 *
6025 * Returns the target state or NULL in case of error
6026 */
6027xmlAutomataStatePtr
6028xmlAutomataNewOnceTrans2(xmlAutomataPtr am, xmlAutomataStatePtr from,
6029 xmlAutomataStatePtr to, const xmlChar *token,
6030 const xmlChar *token2,
6031 int min, int max, void *data) {
6032 xmlRegAtomPtr atom;
6033 int counter;
6034
6035 if ((am == NULL) || (from == NULL) || (token == NULL))
6036 return(NULL);
6037 if (min < 1)
6038 return(NULL);
6039 if ((max < min) || (max < 1))
6040 return(NULL);
6041 atom = xmlRegNewAtom(am, XML_REGEXP_STRING);
6042 if (atom == NULL)
6043 return(NULL);
6044 if ((token2 == NULL) || (*token2 == 0)) {
6045 atom->valuep = xmlStrdup(token);
6046 } else {
6047 int lenn, lenp;
6048 xmlChar *str;
6049
6050 lenn = strlen((char *) token2);
6051 lenp = strlen((char *) token);
6052
6053 str = (xmlChar *) xmlMallocAtomic(lenn + lenp + 2);
6054 if (str == NULL) {
6055 xmlRegFreeAtom(atom);
6056 return(NULL);
6057 }
6058 memcpy(&str[0], token, lenp);
6059 str[lenp] = '|';
6060 memcpy(&str[lenp + 1], token2, lenn);
6061 str[lenn + lenp + 1] = 0;
6062
6063 atom->valuep = str;
Daniel Veillardf8e3db02012-09-11 13:26:36 +08006064 }
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00006065 atom->data = data;
6066 atom->quant = XML_REGEXP_QUANT_ONCEONLY;
Daniel Veillard11ce4002006-03-10 00:36:23 +00006067 atom->min = min;
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00006068 atom->max = max;
6069 /*
6070 * associate a counter to the transition.
6071 */
6072 counter = xmlRegGetCounter(am);
6073 am->counters[counter].min = 1;
6074 am->counters[counter].max = 1;
6075
6076 /* xmlFAGenerateTransitions(am, from, to, atom); */
6077 if (to == NULL) {
6078 to = xmlRegNewState(am);
6079 xmlRegStatePush(am, to);
6080 }
Daniel Veillard5de09382005-09-26 17:18:17 +00006081 xmlRegStateAddTrans(am, from, atom, to, counter, -1);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00006082 xmlRegAtomPush(am, atom);
6083 am->state = to;
6084 return(to);
6085}
6086
Daniel Veillardf8e3db02012-09-11 13:26:36 +08006087
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00006088
6089/**
Daniel Veillard7646b182002-04-20 06:41:40 +00006090 * xmlAutomataNewOnceTrans:
6091 * @am: an automata
6092 * @from: the starting point of the transition
6093 * @to: the target point of the transition or NULL
6094 * @token: the input string associated to that transition
6095 * @min: the minimum successive occurences of token
Daniel Veillarda9b66d02002-12-11 14:23:49 +00006096 * @max: the maximum successive occurences of token
6097 * @data: data associated to the transition
Daniel Veillard7646b182002-04-20 06:41:40 +00006098 *
William M. Brackddf71d62004-05-06 04:17:26 +00006099 * If @to is NULL, this creates first a new target state in the automata
Daniel Veillard7646b182002-04-20 06:41:40 +00006100 * and then adds a transition from the @from state to the target state
6101 * activated by a succession of input of value @token and whose number
William M. Brackddf71d62004-05-06 04:17:26 +00006102 * is between @min and @max, moreover that transition can only be crossed
Daniel Veillard7646b182002-04-20 06:41:40 +00006103 * once.
6104 *
6105 * Returns the target state or NULL in case of error
6106 */
6107xmlAutomataStatePtr
6108xmlAutomataNewOnceTrans(xmlAutomataPtr am, xmlAutomataStatePtr from,
6109 xmlAutomataStatePtr to, const xmlChar *token,
6110 int min, int max, void *data) {
6111 xmlRegAtomPtr atom;
6112 int counter;
6113
6114 if ((am == NULL) || (from == NULL) || (token == NULL))
6115 return(NULL);
6116 if (min < 1)
6117 return(NULL);
6118 if ((max < min) || (max < 1))
6119 return(NULL);
6120 atom = xmlRegNewAtom(am, XML_REGEXP_STRING);
6121 if (atom == NULL)
6122 return(NULL);
6123 atom->valuep = xmlStrdup(token);
6124 atom->data = data;
6125 atom->quant = XML_REGEXP_QUANT_ONCEONLY;
Daniel Veillard11ce4002006-03-10 00:36:23 +00006126 atom->min = min;
Daniel Veillard7646b182002-04-20 06:41:40 +00006127 atom->max = max;
6128 /*
6129 * associate a counter to the transition.
6130 */
6131 counter = xmlRegGetCounter(am);
6132 am->counters[counter].min = 1;
6133 am->counters[counter].max = 1;
6134
6135 /* xmlFAGenerateTransitions(am, from, to, atom); */
6136 if (to == NULL) {
6137 to = xmlRegNewState(am);
6138 xmlRegStatePush(am, to);
6139 }
Daniel Veillard5de09382005-09-26 17:18:17 +00006140 xmlRegStateAddTrans(am, from, atom, to, counter, -1);
Daniel Veillard7646b182002-04-20 06:41:40 +00006141 xmlRegAtomPush(am, atom);
6142 am->state = to;
Daniel Veillard7646b182002-04-20 06:41:40 +00006143 return(to);
6144}
6145
6146/**
Daniel Veillard4255d502002-04-16 15:50:10 +00006147 * xmlAutomataNewState:
6148 * @am: an automata
6149 *
6150 * Create a new disconnected state in the automata
6151 *
6152 * Returns the new state or NULL in case of error
6153 */
6154xmlAutomataStatePtr
6155xmlAutomataNewState(xmlAutomataPtr am) {
Daniel Veillardf8e3db02012-09-11 13:26:36 +08006156 xmlAutomataStatePtr to;
Daniel Veillard4255d502002-04-16 15:50:10 +00006157
6158 if (am == NULL)
6159 return(NULL);
6160 to = xmlRegNewState(am);
6161 xmlRegStatePush(am, to);
6162 return(to);
6163}
6164
6165/**
Daniel Veillarda9b66d02002-12-11 14:23:49 +00006166 * xmlAutomataNewEpsilon:
Daniel Veillard4255d502002-04-16 15:50:10 +00006167 * @am: an automata
6168 * @from: the starting point of the transition
6169 * @to: the target point of the transition or NULL
6170 *
William M. Brackddf71d62004-05-06 04:17:26 +00006171 * If @to is NULL, this creates first a new target state in the automata
6172 * and then adds an epsilon transition from the @from state to the
Daniel Veillard4255d502002-04-16 15:50:10 +00006173 * target state
6174 *
6175 * Returns the target state or NULL in case of error
6176 */
6177xmlAutomataStatePtr
6178xmlAutomataNewEpsilon(xmlAutomataPtr am, xmlAutomataStatePtr from,
6179 xmlAutomataStatePtr to) {
6180 if ((am == NULL) || (from == NULL))
6181 return(NULL);
6182 xmlFAGenerateEpsilonTransition(am, from, to);
6183 if (to == NULL)
6184 return(am->state);
6185 return(to);
6186}
6187
Daniel Veillardb509f152002-04-17 16:28:10 +00006188/**
Daniel Veillard7646b182002-04-20 06:41:40 +00006189 * xmlAutomataNewAllTrans:
6190 * @am: an automata
6191 * @from: the starting point of the transition
6192 * @to: the target point of the transition or NULL
Daniel Veillarda9b66d02002-12-11 14:23:49 +00006193 * @lax: allow to transition if not all all transitions have been activated
Daniel Veillard7646b182002-04-20 06:41:40 +00006194 *
William M. Brackddf71d62004-05-06 04:17:26 +00006195 * If @to is NULL, this creates first a new target state in the automata
Daniel Veillard7646b182002-04-20 06:41:40 +00006196 * and then adds a an ALL transition from the @from state to the
6197 * target state. That transition is an epsilon transition allowed only when
6198 * all transitions from the @from node have been activated.
6199 *
6200 * Returns the target state or NULL in case of error
6201 */
6202xmlAutomataStatePtr
6203xmlAutomataNewAllTrans(xmlAutomataPtr am, xmlAutomataStatePtr from,
Daniel Veillard441bc322002-04-20 17:38:48 +00006204 xmlAutomataStatePtr to, int lax) {
Daniel Veillard7646b182002-04-20 06:41:40 +00006205 if ((am == NULL) || (from == NULL))
6206 return(NULL);
Daniel Veillard441bc322002-04-20 17:38:48 +00006207 xmlFAGenerateAllTransition(am, from, to, lax);
Daniel Veillard7646b182002-04-20 06:41:40 +00006208 if (to == NULL)
6209 return(am->state);
6210 return(to);
6211}
6212
6213/**
Daniel Veillardb509f152002-04-17 16:28:10 +00006214 * xmlAutomataNewCounter:
6215 * @am: an automata
6216 * @min: the minimal value on the counter
6217 * @max: the maximal value on the counter
6218 *
6219 * Create a new counter
6220 *
6221 * Returns the counter number or -1 in case of error
6222 */
Daniel Veillardf8e3db02012-09-11 13:26:36 +08006223int
Daniel Veillardb509f152002-04-17 16:28:10 +00006224xmlAutomataNewCounter(xmlAutomataPtr am, int min, int max) {
6225 int ret;
6226
6227 if (am == NULL)
6228 return(-1);
6229
6230 ret = xmlRegGetCounter(am);
6231 if (ret < 0)
6232 return(-1);
6233 am->counters[ret].min = min;
6234 am->counters[ret].max = max;
6235 return(ret);
6236}
6237
6238/**
6239 * xmlAutomataNewCountedTrans:
6240 * @am: an automata
6241 * @from: the starting point of the transition
6242 * @to: the target point of the transition or NULL
6243 * @counter: the counter associated to that transition
6244 *
William M. Brackddf71d62004-05-06 04:17:26 +00006245 * If @to is NULL, this creates first a new target state in the automata
Daniel Veillardb509f152002-04-17 16:28:10 +00006246 * and then adds an epsilon transition from the @from state to the target state
6247 * which will increment the counter provided
6248 *
6249 * Returns the target state or NULL in case of error
6250 */
6251xmlAutomataStatePtr
6252xmlAutomataNewCountedTrans(xmlAutomataPtr am, xmlAutomataStatePtr from,
6253 xmlAutomataStatePtr to, int counter) {
6254 if ((am == NULL) || (from == NULL) || (counter < 0))
6255 return(NULL);
6256 xmlFAGenerateCountedEpsilonTransition(am, from, to, counter);
6257 if (to == NULL)
6258 return(am->state);
6259 return(to);
6260}
6261
6262/**
6263 * xmlAutomataNewCounterTrans:
6264 * @am: an automata
6265 * @from: the starting point of the transition
6266 * @to: the target point of the transition or NULL
6267 * @counter: the counter associated to that transition
6268 *
William M. Brackddf71d62004-05-06 04:17:26 +00006269 * If @to is NULL, this creates first a new target state in the automata
Daniel Veillardb509f152002-04-17 16:28:10 +00006270 * and then adds an epsilon transition from the @from state to the target state
6271 * which will be allowed only if the counter is within the right range.
6272 *
6273 * Returns the target state or NULL in case of error
6274 */
6275xmlAutomataStatePtr
6276xmlAutomataNewCounterTrans(xmlAutomataPtr am, xmlAutomataStatePtr from,
6277 xmlAutomataStatePtr to, int counter) {
6278 if ((am == NULL) || (from == NULL) || (counter < 0))
6279 return(NULL);
6280 xmlFAGenerateCountedTransition(am, from, to, counter);
6281 if (to == NULL)
6282 return(am->state);
6283 return(to);
6284}
Daniel Veillard4255d502002-04-16 15:50:10 +00006285
6286/**
6287 * xmlAutomataCompile:
6288 * @am: an automata
6289 *
6290 * Compile the automata into a Reg Exp ready for being executed.
6291 * The automata should be free after this point.
6292 *
6293 * Returns the compiled regexp or NULL in case of error
6294 */
Daniel Veillardf8e3db02012-09-11 13:26:36 +08006295xmlRegexpPtr
Daniel Veillard4255d502002-04-16 15:50:10 +00006296xmlAutomataCompile(xmlAutomataPtr am) {
6297 xmlRegexpPtr ret;
6298
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00006299 if ((am == NULL) || (am->error != 0)) return(NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +00006300 xmlFAEliminateEpsilonTransitions(am);
Daniel Veillard23e73572002-09-19 19:56:43 +00006301 /* xmlFAComputesDeterminism(am); */
Daniel Veillard4255d502002-04-16 15:50:10 +00006302 ret = xmlRegEpxFromParse(am);
6303
6304 return(ret);
6305}
Daniel Veillarde19fc232002-04-22 16:01:24 +00006306
6307/**
6308 * xmlAutomataIsDeterminist:
6309 * @am: an automata
6310 *
6311 * Checks if an automata is determinist.
6312 *
6313 * Returns 1 if true, 0 if not, and -1 in case of error
6314 */
Daniel Veillardf8e3db02012-09-11 13:26:36 +08006315int
Daniel Veillarde19fc232002-04-22 16:01:24 +00006316xmlAutomataIsDeterminist(xmlAutomataPtr am) {
6317 int ret;
6318
6319 if (am == NULL)
6320 return(-1);
6321
6322 ret = xmlFAComputesDeterminism(am);
6323 return(ret);
6324}
Daniel Veillard4255d502002-04-16 15:50:10 +00006325#endif /* LIBXML_AUTOMATA_ENABLED */
Daniel Veillard81a8ec62005-08-22 00:20:58 +00006326
6327#ifdef LIBXML_EXPR_ENABLED
6328/************************************************************************
6329 * *
6330 * Formal Expression handling code *
6331 * *
6332 ************************************************************************/
Daniel Veillard81a8ec62005-08-22 00:20:58 +00006333/************************************************************************
6334 * *
6335 * Expression handling context *
6336 * *
6337 ************************************************************************/
6338
6339struct _xmlExpCtxt {
6340 xmlDictPtr dict;
6341 xmlExpNodePtr *table;
6342 int size;
6343 int nbElems;
6344 int nb_nodes;
Daniel Veillard594e5df2009-09-07 14:58:47 +02006345 int maxNodes;
Daniel Veillard81a8ec62005-08-22 00:20:58 +00006346 const char *expr;
6347 const char *cur;
6348 int nb_cons;
Daniel Veillard81a8ec62005-08-22 00:20:58 +00006349 int tabSize;
6350};
6351
6352/**
6353 * xmlExpNewCtxt:
6354 * @maxNodes: the maximum number of nodes
Jan Pokornýbb654fe2016-04-13 16:56:07 +02006355 * @dict: optional dictionary to use internally
Daniel Veillard81a8ec62005-08-22 00:20:58 +00006356 *
6357 * Creates a new context for manipulating expressions
6358 *
6359 * Returns the context or NULL in case of error
6360 */
6361xmlExpCtxtPtr
6362xmlExpNewCtxt(int maxNodes, xmlDictPtr dict) {
6363 xmlExpCtxtPtr ret;
6364 int size = 256;
6365
6366 if (maxNodes <= 4096)
6367 maxNodes = 4096;
Daniel Veillardf8e3db02012-09-11 13:26:36 +08006368
Daniel Veillard81a8ec62005-08-22 00:20:58 +00006369 ret = (xmlExpCtxtPtr) xmlMalloc(sizeof(xmlExpCtxt));
6370 if (ret == NULL)
6371 return(NULL);
6372 memset(ret, 0, sizeof(xmlExpCtxt));
6373 ret->size = size;
6374 ret->nbElems = 0;
Daniel Veillard594e5df2009-09-07 14:58:47 +02006375 ret->maxNodes = maxNodes;
Daniel Veillard81a8ec62005-08-22 00:20:58 +00006376 ret->table = xmlMalloc(size * sizeof(xmlExpNodePtr));
6377 if (ret->table == NULL) {
6378 xmlFree(ret);
6379 return(NULL);
6380 }
6381 memset(ret->table, 0, size * sizeof(xmlExpNodePtr));
6382 if (dict == NULL) {
6383 ret->dict = xmlDictCreate();
6384 if (ret->dict == NULL) {
6385 xmlFree(ret->table);
6386 xmlFree(ret);
6387 return(NULL);
6388 }
6389 } else {
6390 ret->dict = dict;
6391 xmlDictReference(ret->dict);
6392 }
6393 return(ret);
6394}
6395
6396/**
6397 * xmlExpFreeCtxt:
6398 * @ctxt: an expression context
6399 *
6400 * Free an expression context
6401 */
6402void
6403xmlExpFreeCtxt(xmlExpCtxtPtr ctxt) {
6404 if (ctxt == NULL)
6405 return;
6406 xmlDictFree(ctxt->dict);
6407 if (ctxt->table != NULL)
6408 xmlFree(ctxt->table);
6409 xmlFree(ctxt);
6410}
6411
6412/************************************************************************
6413 * *
6414 * Structure associated to an expression node *
6415 * *
6416 ************************************************************************/
Daniel Veillard465a0002005-08-22 12:07:04 +00006417#define MAX_NODES 10000
6418
6419/* #define DEBUG_DERIV */
6420
6421/*
Daniel Veillardf8e3db02012-09-11 13:26:36 +08006422 * TODO:
Daniel Veillard465a0002005-08-22 12:07:04 +00006423 * - Wildcards
6424 * - public API for creation
6425 *
6426 * Started
6427 * - regression testing
6428 *
6429 * Done
6430 * - split into module and test tool
6431 * - memleaks
6432 */
Daniel Veillard81a8ec62005-08-22 00:20:58 +00006433
6434typedef enum {
6435 XML_EXP_NILABLE = (1 << 0)
6436} xmlExpNodeInfo;
6437
6438#define IS_NILLABLE(node) ((node)->info & XML_EXP_NILABLE)
6439
6440struct _xmlExpNode {
6441 unsigned char type;/* xmlExpNodeType */
6442 unsigned char info;/* OR of xmlExpNodeInfo */
6443 unsigned short key; /* the hash key */
6444 unsigned int ref; /* The number of references */
6445 int c_max; /* the maximum length it can consume */
6446 xmlExpNodePtr exp_left;
6447 xmlExpNodePtr next;/* the next node in the hash table or free list */
6448 union {
6449 struct {
6450 int f_min;
6451 int f_max;
6452 } count;
6453 struct {
6454 xmlExpNodePtr f_right;
6455 } children;
6456 const xmlChar *f_str;
6457 } field;
6458};
6459
6460#define exp_min field.count.f_min
6461#define exp_max field.count.f_max
6462/* #define exp_left field.children.f_left */
6463#define exp_right field.children.f_right
6464#define exp_str field.f_str
6465
6466static xmlExpNodePtr xmlExpNewNode(xmlExpCtxtPtr ctxt, xmlExpNodeType type);
6467static xmlExpNode forbiddenExpNode = {
6468 XML_EXP_FORBID, 0, 0, 0, 0, NULL, NULL, {{ 0, 0}}
6469};
6470xmlExpNodePtr forbiddenExp = &forbiddenExpNode;
6471static xmlExpNode emptyExpNode = {
6472 XML_EXP_EMPTY, 1, 0, 0, 0, NULL, NULL, {{ 0, 0}}
6473};
6474xmlExpNodePtr emptyExp = &emptyExpNode;
6475
6476/************************************************************************
6477 * *
6478 * The custom hash table for unicity and canonicalization *
6479 * of sub-expressions pointers *
6480 * *
6481 ************************************************************************/
6482/*
6483 * xmlExpHashNameComputeKey:
6484 * Calculate the hash key for a token
6485 */
6486static unsigned short
6487xmlExpHashNameComputeKey(const xmlChar *name) {
6488 unsigned short value = 0L;
6489 char ch;
Daniel Veillardf8e3db02012-09-11 13:26:36 +08006490
Daniel Veillard81a8ec62005-08-22 00:20:58 +00006491 if (name != NULL) {
6492 value += 30 * (*name);
6493 while ((ch = *name++) != 0) {
6494 value = value ^ ((value << 5) + (value >> 3) + (unsigned long)ch);
6495 }
6496 }
6497 return (value);
6498}
6499
6500/*
6501 * xmlExpHashComputeKey:
6502 * Calculate the hash key for a compound expression
6503 */
6504static unsigned short
6505xmlExpHashComputeKey(xmlExpNodeType type, xmlExpNodePtr left,
6506 xmlExpNodePtr right) {
6507 unsigned long value;
6508 unsigned short ret;
Daniel Veillardf8e3db02012-09-11 13:26:36 +08006509
Daniel Veillard81a8ec62005-08-22 00:20:58 +00006510 switch (type) {
6511 case XML_EXP_SEQ:
6512 value = left->key;
6513 value += right->key;
6514 value *= 3;
6515 ret = (unsigned short) value;
6516 break;
6517 case XML_EXP_OR:
6518 value = left->key;
6519 value += right->key;
6520 value *= 7;
6521 ret = (unsigned short) value;
6522 break;
6523 case XML_EXP_COUNT:
6524 value = left->key;
6525 value += right->key;
6526 ret = (unsigned short) value;
6527 break;
6528 default:
6529 ret = 0;
6530 }
6531 return(ret);
6532}
6533
6534
6535static xmlExpNodePtr
6536xmlExpNewNode(xmlExpCtxtPtr ctxt, xmlExpNodeType type) {
6537 xmlExpNodePtr ret;
6538
6539 if (ctxt->nb_nodes >= MAX_NODES)
6540 return(NULL);
6541 ret = (xmlExpNodePtr) xmlMalloc(sizeof(xmlExpNode));
6542 if (ret == NULL)
6543 return(NULL);
6544 memset(ret, 0, sizeof(xmlExpNode));
6545 ret->type = type;
6546 ret->next = NULL;
6547 ctxt->nb_nodes++;
6548 ctxt->nb_cons++;
6549 return(ret);
6550}
6551
6552/**
6553 * xmlExpHashGetEntry:
6554 * @table: the hash table
6555 *
6556 * Get the unique entry from the hash table. The entry is created if
6557 * needed. @left and @right are consumed, i.e. their ref count will
6558 * be decremented by the operation.
6559 *
6560 * Returns the pointer or NULL in case of error
6561 */
6562static xmlExpNodePtr
6563xmlExpHashGetEntry(xmlExpCtxtPtr ctxt, xmlExpNodeType type,
6564 xmlExpNodePtr left, xmlExpNodePtr right,
6565 const xmlChar *name, int min, int max) {
6566 unsigned short kbase, key;
6567 xmlExpNodePtr entry;
6568 xmlExpNodePtr insert;
6569
6570 if (ctxt == NULL)
6571 return(NULL);
6572
6573 /*
6574 * Check for duplicate and insertion location.
6575 */
6576 if (type == XML_EXP_ATOM) {
6577 kbase = xmlExpHashNameComputeKey(name);
6578 } else if (type == XML_EXP_COUNT) {
6579 /* COUNT reduction rule 1 */
6580 /* a{1} -> a */
6581 if (min == max) {
6582 if (min == 1) {
6583 return(left);
6584 }
6585 if (min == 0) {
6586 xmlExpFree(ctxt, left);
6587 return(emptyExp);
6588 }
6589 }
6590 if (min < 0) {
6591 xmlExpFree(ctxt, left);
6592 return(forbiddenExp);
6593 }
6594 if (max == -1)
6595 kbase = min + 79;
6596 else
6597 kbase = max - min;
6598 kbase += left->key;
6599 } else if (type == XML_EXP_OR) {
6600 /* Forbid reduction rules */
6601 if (left->type == XML_EXP_FORBID) {
6602 xmlExpFree(ctxt, left);
6603 return(right);
6604 }
6605 if (right->type == XML_EXP_FORBID) {
6606 xmlExpFree(ctxt, right);
6607 return(left);
6608 }
6609
6610 /* OR reduction rule 1 */
6611 /* a | a reduced to a */
6612 if (left == right) {
6613 left->ref--;
6614 return(left);
6615 }
6616 /* OR canonicalization rule 1 */
6617 /* linearize (a | b) | c into a | (b | c) */
6618 if ((left->type == XML_EXP_OR) && (right->type != XML_EXP_OR)) {
6619 xmlExpNodePtr tmp = left;
6620 left = right;
6621 right = tmp;
6622 }
6623 /* OR reduction rule 2 */
6624 /* a | (a | b) and b | (a | b) are reduced to a | b */
6625 if (right->type == XML_EXP_OR) {
6626 if ((left == right->exp_left) ||
6627 (left == right->exp_right)) {
6628 xmlExpFree(ctxt, left);
6629 return(right);
6630 }
6631 }
6632 /* OR canonicalization rule 2 */
6633 /* linearize (a | b) | c into a | (b | c) */
6634 if (left->type == XML_EXP_OR) {
6635 xmlExpNodePtr tmp;
6636
6637 /* OR canonicalization rule 2 */
6638 if ((left->exp_right->type != XML_EXP_OR) &&
6639 (left->exp_right->key < left->exp_left->key)) {
6640 tmp = left->exp_right;
6641 left->exp_right = left->exp_left;
6642 left->exp_left = tmp;
6643 }
6644 left->exp_right->ref++;
6645 tmp = xmlExpHashGetEntry(ctxt, XML_EXP_OR, left->exp_right, right,
6646 NULL, 0, 0);
6647 left->exp_left->ref++;
6648 tmp = xmlExpHashGetEntry(ctxt, XML_EXP_OR, left->exp_left, tmp,
6649 NULL, 0, 0);
Daniel Veillardf8e3db02012-09-11 13:26:36 +08006650
Daniel Veillard81a8ec62005-08-22 00:20:58 +00006651 xmlExpFree(ctxt, left);
6652 return(tmp);
6653 }
6654 if (right->type == XML_EXP_OR) {
6655 /* Ordering in the tree */
6656 /* C | (A | B) -> A | (B | C) */
6657 if (left->key > right->exp_right->key) {
6658 xmlExpNodePtr tmp;
6659 right->exp_right->ref++;
6660 tmp = xmlExpHashGetEntry(ctxt, XML_EXP_OR, right->exp_right,
6661 left, NULL, 0, 0);
6662 right->exp_left->ref++;
6663 tmp = xmlExpHashGetEntry(ctxt, XML_EXP_OR, right->exp_left,
6664 tmp, NULL, 0, 0);
6665 xmlExpFree(ctxt, right);
6666 return(tmp);
6667 }
6668 /* Ordering in the tree */
6669 /* B | (A | C) -> A | (B | C) */
6670 if (left->key > right->exp_left->key) {
6671 xmlExpNodePtr tmp;
6672 right->exp_right->ref++;
6673 tmp = xmlExpHashGetEntry(ctxt, XML_EXP_OR, left,
6674 right->exp_right, NULL, 0, 0);
6675 right->exp_left->ref++;
6676 tmp = xmlExpHashGetEntry(ctxt, XML_EXP_OR, right->exp_left,
6677 tmp, NULL, 0, 0);
6678 xmlExpFree(ctxt, right);
6679 return(tmp);
6680 }
6681 }
6682 /* we know both types are != XML_EXP_OR here */
6683 else if (left->key > right->key) {
6684 xmlExpNodePtr tmp = left;
6685 left = right;
6686 right = tmp;
6687 }
6688 kbase = xmlExpHashComputeKey(type, left, right);
6689 } else if (type == XML_EXP_SEQ) {
6690 /* Forbid reduction rules */
6691 if (left->type == XML_EXP_FORBID) {
6692 xmlExpFree(ctxt, right);
6693 return(left);
6694 }
6695 if (right->type == XML_EXP_FORBID) {
6696 xmlExpFree(ctxt, left);
6697 return(right);
6698 }
6699 /* Empty reduction rules */
6700 if (right->type == XML_EXP_EMPTY) {
6701 return(left);
6702 }
6703 if (left->type == XML_EXP_EMPTY) {
6704 return(right);
6705 }
6706 kbase = xmlExpHashComputeKey(type, left, right);
Daniel Veillardf8e3db02012-09-11 13:26:36 +08006707 } else
Daniel Veillard81a8ec62005-08-22 00:20:58 +00006708 return(NULL);
6709
6710 key = kbase % ctxt->size;
6711 if (ctxt->table[key] != NULL) {
6712 for (insert = ctxt->table[key]; insert != NULL;
6713 insert = insert->next) {
6714 if ((insert->key == kbase) &&
6715 (insert->type == type)) {
6716 if (type == XML_EXP_ATOM) {
6717 if (name == insert->exp_str) {
6718 insert->ref++;
6719 return(insert);
6720 }
6721 } else if (type == XML_EXP_COUNT) {
6722 if ((insert->exp_min == min) && (insert->exp_max == max) &&
6723 (insert->exp_left == left)) {
6724 insert->ref++;
6725 left->ref--;
6726 return(insert);
6727 }
6728 } else if ((insert->exp_left == left) &&
6729 (insert->exp_right == right)) {
6730 insert->ref++;
6731 left->ref--;
6732 right->ref--;
6733 return(insert);
6734 }
6735 }
6736 }
6737 }
6738
6739 entry = xmlExpNewNode(ctxt, type);
6740 if (entry == NULL)
6741 return(NULL);
6742 entry->key = kbase;
6743 if (type == XML_EXP_ATOM) {
6744 entry->exp_str = name;
6745 entry->c_max = 1;
6746 } else if (type == XML_EXP_COUNT) {
6747 entry->exp_min = min;
6748 entry->exp_max = max;
6749 entry->exp_left = left;
6750 if ((min == 0) || (IS_NILLABLE(left)))
6751 entry->info |= XML_EXP_NILABLE;
6752 if (max < 0)
6753 entry->c_max = -1;
6754 else
6755 entry->c_max = max * entry->exp_left->c_max;
6756 } else {
6757 entry->exp_left = left;
6758 entry->exp_right = right;
6759 if (type == XML_EXP_OR) {
6760 if ((IS_NILLABLE(left)) || (IS_NILLABLE(right)))
6761 entry->info |= XML_EXP_NILABLE;
6762 if ((entry->exp_left->c_max == -1) ||
6763 (entry->exp_right->c_max == -1))
6764 entry->c_max = -1;
6765 else if (entry->exp_left->c_max > entry->exp_right->c_max)
6766 entry->c_max = entry->exp_left->c_max;
6767 else
6768 entry->c_max = entry->exp_right->c_max;
6769 } else {
6770 if ((IS_NILLABLE(left)) && (IS_NILLABLE(right)))
6771 entry->info |= XML_EXP_NILABLE;
6772 if ((entry->exp_left->c_max == -1) ||
6773 (entry->exp_right->c_max == -1))
6774 entry->c_max = -1;
6775 else
6776 entry->c_max = entry->exp_left->c_max + entry->exp_right->c_max;
6777 }
6778 }
6779 entry->ref = 1;
6780 if (ctxt->table[key] != NULL)
6781 entry->next = ctxt->table[key];
6782
6783 ctxt->table[key] = entry;
6784 ctxt->nbElems++;
6785
6786 return(entry);
6787}
6788
6789/**
6790 * xmlExpFree:
6791 * @ctxt: the expression context
6792 * @exp: the expression
6793 *
6794 * Dereference the expression
6795 */
6796void
6797xmlExpFree(xmlExpCtxtPtr ctxt, xmlExpNodePtr exp) {
6798 if ((exp == NULL) || (exp == forbiddenExp) || (exp == emptyExp))
6799 return;
6800 exp->ref--;
Daniel Veillard81a8ec62005-08-22 00:20:58 +00006801 if (exp->ref == 0) {
6802 unsigned short key;
6803
6804 /* Unlink it first from the hash table */
6805 key = exp->key % ctxt->size;
6806 if (ctxt->table[key] == exp) {
6807 ctxt->table[key] = exp->next;
6808 } else {
6809 xmlExpNodePtr tmp;
6810
6811 tmp = ctxt->table[key];
6812 while (tmp != NULL) {
6813 if (tmp->next == exp) {
6814 tmp->next = exp->next;
6815 break;
6816 }
6817 tmp = tmp->next;
6818 }
6819 }
6820
6821 if ((exp->type == XML_EXP_SEQ) || (exp->type == XML_EXP_OR)) {
6822 xmlExpFree(ctxt, exp->exp_left);
6823 xmlExpFree(ctxt, exp->exp_right);
6824 } else if (exp->type == XML_EXP_COUNT) {
6825 xmlExpFree(ctxt, exp->exp_left);
6826 }
6827 xmlFree(exp);
Daniel Veillard81a8ec62005-08-22 00:20:58 +00006828 ctxt->nb_nodes--;
6829 }
6830}
6831
6832/**
6833 * xmlExpRef:
6834 * @exp: the expression
6835 *
6836 * Increase the reference count of the expression
6837 */
6838void
6839xmlExpRef(xmlExpNodePtr exp) {
6840 if (exp != NULL)
6841 exp->ref++;
6842}
6843
Daniel Veillardccb4d412005-08-23 13:41:17 +00006844/**
6845 * xmlExpNewAtom:
6846 * @ctxt: the expression context
6847 * @name: the atom name
Michael Woodfb27e2c2012-09-28 08:59:33 +02006848 * @len: the atom name length in byte (or -1);
Daniel Veillardccb4d412005-08-23 13:41:17 +00006849 *
6850 * Get the atom associated to this name from that context
6851 *
6852 * Returns the node or NULL in case of error
6853 */
6854xmlExpNodePtr
6855xmlExpNewAtom(xmlExpCtxtPtr ctxt, const xmlChar *name, int len) {
6856 if ((ctxt == NULL) || (name == NULL))
6857 return(NULL);
6858 name = xmlDictLookup(ctxt->dict, name, len);
6859 if (name == NULL)
6860 return(NULL);
6861 return(xmlExpHashGetEntry(ctxt, XML_EXP_ATOM, NULL, NULL, name, 0, 0));
6862}
6863
6864/**
6865 * xmlExpNewOr:
6866 * @ctxt: the expression context
6867 * @left: left expression
6868 * @right: right expression
6869 *
6870 * Get the atom associated to the choice @left | @right
6871 * Note that @left and @right are consumed in the operation, to keep
6872 * an handle on them use xmlExpRef() and use xmlExpFree() to release them,
6873 * this is true even in case of failure (unless ctxt == NULL).
6874 *
6875 * Returns the node or NULL in case of error
6876 */
6877xmlExpNodePtr
6878xmlExpNewOr(xmlExpCtxtPtr ctxt, xmlExpNodePtr left, xmlExpNodePtr right) {
Daniel Veillard11ce4002006-03-10 00:36:23 +00006879 if (ctxt == NULL)
6880 return(NULL);
6881 if ((left == NULL) || (right == NULL)) {
Daniel Veillardccb4d412005-08-23 13:41:17 +00006882 xmlExpFree(ctxt, left);
6883 xmlExpFree(ctxt, right);
6884 return(NULL);
6885 }
6886 return(xmlExpHashGetEntry(ctxt, XML_EXP_OR, left, right, NULL, 0, 0));
6887}
6888
6889/**
6890 * xmlExpNewSeq:
6891 * @ctxt: the expression context
6892 * @left: left expression
6893 * @right: right expression
6894 *
6895 * Get the atom associated to the sequence @left , @right
6896 * Note that @left and @right are consumed in the operation, to keep
6897 * an handle on them use xmlExpRef() and use xmlExpFree() to release them,
6898 * this is true even in case of failure (unless ctxt == NULL).
6899 *
6900 * Returns the node or NULL in case of error
6901 */
6902xmlExpNodePtr
6903xmlExpNewSeq(xmlExpCtxtPtr ctxt, xmlExpNodePtr left, xmlExpNodePtr right) {
Daniel Veillard11ce4002006-03-10 00:36:23 +00006904 if (ctxt == NULL)
6905 return(NULL);
6906 if ((left == NULL) || (right == NULL)) {
Daniel Veillardccb4d412005-08-23 13:41:17 +00006907 xmlExpFree(ctxt, left);
6908 xmlExpFree(ctxt, right);
6909 return(NULL);
6910 }
6911 return(xmlExpHashGetEntry(ctxt, XML_EXP_SEQ, left, right, NULL, 0, 0));
6912}
6913
6914/**
6915 * xmlExpNewRange:
6916 * @ctxt: the expression context
6917 * @subset: the expression to be repeated
6918 * @min: the lower bound for the repetition
6919 * @max: the upper bound for the repetition, -1 means infinite
6920 *
6921 * Get the atom associated to the range (@subset){@min, @max}
6922 * Note that @subset is consumed in the operation, to keep
6923 * an handle on it use xmlExpRef() and use xmlExpFree() to release it,
6924 * this is true even in case of failure (unless ctxt == NULL).
6925 *
6926 * Returns the node or NULL in case of error
6927 */
6928xmlExpNodePtr
6929xmlExpNewRange(xmlExpCtxtPtr ctxt, xmlExpNodePtr subset, int min, int max) {
Daniel Veillard11ce4002006-03-10 00:36:23 +00006930 if (ctxt == NULL)
6931 return(NULL);
6932 if ((subset == NULL) || (min < 0) || (max < -1) ||
Daniel Veillardccb4d412005-08-23 13:41:17 +00006933 ((max >= 0) && (min > max))) {
6934 xmlExpFree(ctxt, subset);
6935 return(NULL);
6936 }
6937 return(xmlExpHashGetEntry(ctxt, XML_EXP_COUNT, subset,
6938 NULL, NULL, min, max));
6939}
6940
Daniel Veillard81a8ec62005-08-22 00:20:58 +00006941/************************************************************************
6942 * *
6943 * Public API for operations on expressions *
6944 * *
6945 ************************************************************************/
6946
6947static int
Daniel Veillardf8e3db02012-09-11 13:26:36 +08006948xmlExpGetLanguageInt(xmlExpCtxtPtr ctxt, xmlExpNodePtr exp,
Daniel Veillard81a8ec62005-08-22 00:20:58 +00006949 const xmlChar**list, int len, int nb) {
6950 int tmp, tmp2;
6951tail:
6952 switch (exp->type) {
6953 case XML_EXP_EMPTY:
6954 return(0);
6955 case XML_EXP_ATOM:
6956 for (tmp = 0;tmp < nb;tmp++)
6957 if (list[tmp] == exp->exp_str)
6958 return(0);
6959 if (nb >= len)
6960 return(-2);
Daniel Veillard13cee4e2009-09-05 14:52:55 +02006961 list[nb] = exp->exp_str;
Daniel Veillard81a8ec62005-08-22 00:20:58 +00006962 return(1);
6963 case XML_EXP_COUNT:
6964 exp = exp->exp_left;
6965 goto tail;
6966 case XML_EXP_SEQ:
6967 case XML_EXP_OR:
6968 tmp = xmlExpGetLanguageInt(ctxt, exp->exp_left, list, len, nb);
6969 if (tmp < 0)
6970 return(tmp);
6971 tmp2 = xmlExpGetLanguageInt(ctxt, exp->exp_right, list, len,
6972 nb + tmp);
6973 if (tmp2 < 0)
6974 return(tmp2);
6975 return(tmp + tmp2);
6976 }
6977 return(-1);
6978}
6979
6980/**
6981 * xmlExpGetLanguage:
6982 * @ctxt: the expression context
6983 * @exp: the expression
Daniel Veillard7802ba52005-10-27 11:56:20 +00006984 * @langList: where to store the tokens
Michael Woodfb27e2c2012-09-28 08:59:33 +02006985 * @len: the allocated length of @list
Daniel Veillard81a8ec62005-08-22 00:20:58 +00006986 *
6987 * Find all the strings used in @exp and store them in @list
6988 *
6989 * Returns the number of unique strings found, -1 in case of errors and
6990 * -2 if there is more than @len strings
6991 */
6992int
Daniel Veillardf8e3db02012-09-11 13:26:36 +08006993xmlExpGetLanguage(xmlExpCtxtPtr ctxt, xmlExpNodePtr exp,
Daniel Veillard7802ba52005-10-27 11:56:20 +00006994 const xmlChar**langList, int len) {
6995 if ((ctxt == NULL) || (exp == NULL) || (langList == NULL) || (len <= 0))
Daniel Veillard81a8ec62005-08-22 00:20:58 +00006996 return(-1);
Daniel Veillard7802ba52005-10-27 11:56:20 +00006997 return(xmlExpGetLanguageInt(ctxt, exp, langList, len, 0));
Daniel Veillard81a8ec62005-08-22 00:20:58 +00006998}
6999
7000static int
Daniel Veillardf8e3db02012-09-11 13:26:36 +08007001xmlExpGetStartInt(xmlExpCtxtPtr ctxt, xmlExpNodePtr exp,
Daniel Veillard81a8ec62005-08-22 00:20:58 +00007002 const xmlChar**list, int len, int nb) {
7003 int tmp, tmp2;
7004tail:
7005 switch (exp->type) {
7006 case XML_EXP_FORBID:
7007 return(0);
7008 case XML_EXP_EMPTY:
7009 return(0);
7010 case XML_EXP_ATOM:
7011 for (tmp = 0;tmp < nb;tmp++)
7012 if (list[tmp] == exp->exp_str)
7013 return(0);
7014 if (nb >= len)
7015 return(-2);
Daniel Veillard13cee4e2009-09-05 14:52:55 +02007016 list[nb] = exp->exp_str;
Daniel Veillard81a8ec62005-08-22 00:20:58 +00007017 return(1);
7018 case XML_EXP_COUNT:
7019 exp = exp->exp_left;
7020 goto tail;
7021 case XML_EXP_SEQ:
7022 tmp = xmlExpGetStartInt(ctxt, exp->exp_left, list, len, nb);
7023 if (tmp < 0)
7024 return(tmp);
7025 if (IS_NILLABLE(exp->exp_left)) {
7026 tmp2 = xmlExpGetStartInt(ctxt, exp->exp_right, list, len,
7027 nb + tmp);
7028 if (tmp2 < 0)
7029 return(tmp2);
7030 tmp += tmp2;
7031 }
7032 return(tmp);
7033 case XML_EXP_OR:
7034 tmp = xmlExpGetStartInt(ctxt, exp->exp_left, list, len, nb);
7035 if (tmp < 0)
7036 return(tmp);
7037 tmp2 = xmlExpGetStartInt(ctxt, exp->exp_right, list, len,
7038 nb + tmp);
7039 if (tmp2 < 0)
7040 return(tmp2);
7041 return(tmp + tmp2);
7042 }
7043 return(-1);
7044}
7045
7046/**
7047 * xmlExpGetStart:
7048 * @ctxt: the expression context
7049 * @exp: the expression
Daniel Veillard7802ba52005-10-27 11:56:20 +00007050 * @tokList: where to store the tokens
Michael Woodfb27e2c2012-09-28 08:59:33 +02007051 * @len: the allocated length of @list
Daniel Veillard81a8ec62005-08-22 00:20:58 +00007052 *
7053 * Find all the strings that appears at the start of the languages
7054 * accepted by @exp and store them in @list. E.g. for (a, b) | c
7055 * it will return the list [a, c]
7056 *
7057 * Returns the number of unique strings found, -1 in case of errors and
7058 * -2 if there is more than @len strings
7059 */
7060int
Daniel Veillardf8e3db02012-09-11 13:26:36 +08007061xmlExpGetStart(xmlExpCtxtPtr ctxt, xmlExpNodePtr exp,
Daniel Veillard7802ba52005-10-27 11:56:20 +00007062 const xmlChar**tokList, int len) {
7063 if ((ctxt == NULL) || (exp == NULL) || (tokList == NULL) || (len <= 0))
Daniel Veillard81a8ec62005-08-22 00:20:58 +00007064 return(-1);
Daniel Veillard7802ba52005-10-27 11:56:20 +00007065 return(xmlExpGetStartInt(ctxt, exp, tokList, len, 0));
Daniel Veillard81a8ec62005-08-22 00:20:58 +00007066}
7067
7068/**
7069 * xmlExpIsNillable:
7070 * @exp: the expression
7071 *
7072 * Finds if the expression is nillable, i.e. if it accepts the empty sequqnce
7073 *
7074 * Returns 1 if nillable, 0 if not and -1 in case of error
7075 */
7076int
7077xmlExpIsNillable(xmlExpNodePtr exp) {
7078 if (exp == NULL)
7079 return(-1);
7080 return(IS_NILLABLE(exp) != 0);
7081}
7082
7083static xmlExpNodePtr
7084xmlExpStringDeriveInt(xmlExpCtxtPtr ctxt, xmlExpNodePtr exp, const xmlChar *str)
7085{
7086 xmlExpNodePtr ret;
7087
7088 switch (exp->type) {
7089 case XML_EXP_EMPTY:
7090 return(forbiddenExp);
7091 case XML_EXP_FORBID:
7092 return(forbiddenExp);
7093 case XML_EXP_ATOM:
7094 if (exp->exp_str == str) {
7095#ifdef DEBUG_DERIV
7096 printf("deriv atom: equal => Empty\n");
7097#endif
7098 ret = emptyExp;
7099 } else {
7100#ifdef DEBUG_DERIV
7101 printf("deriv atom: mismatch => forbid\n");
7102#endif
7103 /* TODO wildcards here */
7104 ret = forbiddenExp;
7105 }
7106 return(ret);
7107 case XML_EXP_OR: {
7108 xmlExpNodePtr tmp;
7109
7110#ifdef DEBUG_DERIV
7111 printf("deriv or: => or(derivs)\n");
7112#endif
7113 tmp = xmlExpStringDeriveInt(ctxt, exp->exp_left, str);
7114 if (tmp == NULL) {
7115 return(NULL);
7116 }
7117 ret = xmlExpStringDeriveInt(ctxt, exp->exp_right, str);
7118 if (ret == NULL) {
7119 xmlExpFree(ctxt, tmp);
7120 return(NULL);
7121 }
7122 ret = xmlExpHashGetEntry(ctxt, XML_EXP_OR, tmp, ret,
7123 NULL, 0, 0);
7124 return(ret);
7125 }
7126 case XML_EXP_SEQ:
7127#ifdef DEBUG_DERIV
7128 printf("deriv seq: starting with left\n");
7129#endif
7130 ret = xmlExpStringDeriveInt(ctxt, exp->exp_left, str);
7131 if (ret == NULL) {
7132 return(NULL);
7133 } else if (ret == forbiddenExp) {
7134 if (IS_NILLABLE(exp->exp_left)) {
7135#ifdef DEBUG_DERIV
7136 printf("deriv seq: left failed but nillable\n");
7137#endif
7138 ret = xmlExpStringDeriveInt(ctxt, exp->exp_right, str);
7139 }
7140 } else {
7141#ifdef DEBUG_DERIV
7142 printf("deriv seq: left match => sequence\n");
7143#endif
7144 exp->exp_right->ref++;
7145 ret = xmlExpHashGetEntry(ctxt, XML_EXP_SEQ, ret, exp->exp_right,
7146 NULL, 0, 0);
7147 }
7148 return(ret);
7149 case XML_EXP_COUNT: {
7150 int min, max;
7151 xmlExpNodePtr tmp;
7152
7153 if (exp->exp_max == 0)
7154 return(forbiddenExp);
7155 ret = xmlExpStringDeriveInt(ctxt, exp->exp_left, str);
7156 if (ret == NULL)
7157 return(NULL);
7158 if (ret == forbiddenExp) {
7159#ifdef DEBUG_DERIV
7160 printf("deriv count: pattern mismatch => forbid\n");
7161#endif
7162 return(ret);
7163 }
7164 if (exp->exp_max == 1)
7165 return(ret);
7166 if (exp->exp_max < 0) /* unbounded */
7167 max = -1;
7168 else
7169 max = exp->exp_max - 1;
7170 if (exp->exp_min > 0)
7171 min = exp->exp_min - 1;
7172 else
7173 min = 0;
7174 exp->exp_left->ref++;
7175 tmp = xmlExpHashGetEntry(ctxt, XML_EXP_COUNT, exp->exp_left, NULL,
7176 NULL, min, max);
7177 if (ret == emptyExp) {
7178#ifdef DEBUG_DERIV
7179 printf("deriv count: match to empty => new count\n");
7180#endif
7181 return(tmp);
7182 }
7183#ifdef DEBUG_DERIV
7184 printf("deriv count: match => sequence with new count\n");
7185#endif
7186 return(xmlExpHashGetEntry(ctxt, XML_EXP_SEQ, ret, tmp,
7187 NULL, 0, 0));
7188 }
7189 }
7190 return(NULL);
7191}
7192
7193/**
7194 * xmlExpStringDerive:
7195 * @ctxt: the expression context
7196 * @exp: the expression
7197 * @str: the string
7198 * @len: the string len in bytes if available
7199 *
7200 * Do one step of Brzozowski derivation of the expression @exp with
7201 * respect to the input string
7202 *
7203 * Returns the resulting expression or NULL in case of internal error
7204 */
7205xmlExpNodePtr
7206xmlExpStringDerive(xmlExpCtxtPtr ctxt, xmlExpNodePtr exp,
7207 const xmlChar *str, int len) {
7208 const xmlChar *input;
7209
7210 if ((exp == NULL) || (ctxt == NULL) || (str == NULL)) {
7211 return(NULL);
7212 }
7213 /*
Jan Pokornýbb654fe2016-04-13 16:56:07 +02007214 * check the string is in the dictionary, if yes use an interned
Daniel Veillard81a8ec62005-08-22 00:20:58 +00007215 * copy, otherwise we know it's not an acceptable input
7216 */
7217 input = xmlDictExists(ctxt->dict, str, len);
7218 if (input == NULL) {
7219 return(forbiddenExp);
7220 }
7221 return(xmlExpStringDeriveInt(ctxt, exp, input));
7222}
7223
7224static int
7225xmlExpCheckCard(xmlExpNodePtr exp, xmlExpNodePtr sub) {
7226 int ret = 1;
7227
7228 if (sub->c_max == -1) {
7229 if (exp->c_max != -1)
7230 ret = 0;
7231 } else if ((exp->c_max >= 0) && (exp->c_max < sub->c_max)) {
7232 ret = 0;
7233 }
7234#if 0
7235 if ((IS_NILLABLE(sub)) && (!IS_NILLABLE(exp)))
7236 ret = 0;
7237#endif
7238 return(ret);
7239}
7240
7241static xmlExpNodePtr xmlExpExpDeriveInt(xmlExpCtxtPtr ctxt, xmlExpNodePtr exp,
7242 xmlExpNodePtr sub);
7243/**
7244 * xmlExpDivide:
7245 * @ctxt: the expressions context
7246 * @exp: the englobing expression
7247 * @sub: the subexpression
7248 * @mult: the multiple expression
7249 * @remain: the remain from the derivation of the multiple
7250 *
7251 * Check if exp is a multiple of sub, i.e. if there is a finite number n
7252 * so that sub{n} subsume exp
7253 *
7254 * Returns the multiple value if successful, 0 if it is not a multiple
7255 * and -1 in case of internel error.
7256 */
7257
7258static int
7259xmlExpDivide(xmlExpCtxtPtr ctxt, xmlExpNodePtr exp, xmlExpNodePtr sub,
7260 xmlExpNodePtr *mult, xmlExpNodePtr *remain) {
7261 int i;
7262 xmlExpNodePtr tmp, tmp2;
7263
7264 if (mult != NULL) *mult = NULL;
7265 if (remain != NULL) *remain = NULL;
7266 if (exp->c_max == -1) return(0);
7267 if (IS_NILLABLE(exp) && (!IS_NILLABLE(sub))) return(0);
7268
7269 for (i = 1;i <= exp->c_max;i++) {
7270 sub->ref++;
7271 tmp = xmlExpHashGetEntry(ctxt, XML_EXP_COUNT,
7272 sub, NULL, NULL, i, i);
7273 if (tmp == NULL) {
7274 return(-1);
7275 }
7276 if (!xmlExpCheckCard(tmp, exp)) {
7277 xmlExpFree(ctxt, tmp);
7278 continue;
7279 }
7280 tmp2 = xmlExpExpDeriveInt(ctxt, tmp, exp);
7281 if (tmp2 == NULL) {
7282 xmlExpFree(ctxt, tmp);
7283 return(-1);
7284 }
7285 if ((tmp2 != forbiddenExp) && (IS_NILLABLE(tmp2))) {
7286 if (remain != NULL)
7287 *remain = tmp2;
7288 else
7289 xmlExpFree(ctxt, tmp2);
7290 if (mult != NULL)
7291 *mult = tmp;
7292 else
7293 xmlExpFree(ctxt, tmp);
7294#ifdef DEBUG_DERIV
7295 printf("Divide succeeded %d\n", i);
7296#endif
7297 return(i);
7298 }
7299 xmlExpFree(ctxt, tmp);
7300 xmlExpFree(ctxt, tmp2);
7301 }
7302#ifdef DEBUG_DERIV
7303 printf("Divide failed\n");
7304#endif
7305 return(0);
7306}
7307
7308/**
7309 * xmlExpExpDeriveInt:
7310 * @ctxt: the expressions context
7311 * @exp: the englobing expression
7312 * @sub: the subexpression
7313 *
7314 * Try to do a step of Brzozowski derivation but at a higher level
7315 * the input being a subexpression.
7316 *
7317 * Returns the resulting expression or NULL in case of internal error
7318 */
7319static xmlExpNodePtr
7320xmlExpExpDeriveInt(xmlExpCtxtPtr ctxt, xmlExpNodePtr exp, xmlExpNodePtr sub) {
7321 xmlExpNodePtr ret, tmp, tmp2, tmp3;
7322 const xmlChar **tab;
7323 int len, i;
7324
7325 /*
7326 * In case of equality and if the expression can only consume a finite
7327 * amount, then the derivation is empty
7328 */
7329 if ((exp == sub) && (exp->c_max >= 0)) {
7330#ifdef DEBUG_DERIV
7331 printf("Equal(exp, sub) and finite -> Empty\n");
7332#endif
7333 return(emptyExp);
7334 }
7335 /*
7336 * decompose sub sequence first
7337 */
7338 if (sub->type == XML_EXP_EMPTY) {
7339#ifdef DEBUG_DERIV
7340 printf("Empty(sub) -> Empty\n");
7341#endif
7342 exp->ref++;
7343 return(exp);
7344 }
7345 if (sub->type == XML_EXP_SEQ) {
7346#ifdef DEBUG_DERIV
7347 printf("Seq(sub) -> decompose\n");
7348#endif
7349 tmp = xmlExpExpDeriveInt(ctxt, exp, sub->exp_left);
7350 if (tmp == NULL)
7351 return(NULL);
7352 if (tmp == forbiddenExp)
7353 return(tmp);
7354 ret = xmlExpExpDeriveInt(ctxt, tmp, sub->exp_right);
7355 xmlExpFree(ctxt, tmp);
7356 return(ret);
7357 }
7358 if (sub->type == XML_EXP_OR) {
7359#ifdef DEBUG_DERIV
7360 printf("Or(sub) -> decompose\n");
7361#endif
7362 tmp = xmlExpExpDeriveInt(ctxt, exp, sub->exp_left);
7363 if (tmp == forbiddenExp)
7364 return(tmp);
7365 if (tmp == NULL)
7366 return(NULL);
7367 ret = xmlExpExpDeriveInt(ctxt, exp, sub->exp_right);
7368 if ((ret == NULL) || (ret == forbiddenExp)) {
7369 xmlExpFree(ctxt, tmp);
7370 return(ret);
7371 }
7372 return(xmlExpHashGetEntry(ctxt, XML_EXP_OR, tmp, ret, NULL, 0, 0));
7373 }
7374 if (!xmlExpCheckCard(exp, sub)) {
7375#ifdef DEBUG_DERIV
7376 printf("CheckCard(exp, sub) failed -> Forbid\n");
7377#endif
7378 return(forbiddenExp);
7379 }
7380 switch (exp->type) {
7381 case XML_EXP_EMPTY:
7382 if (sub == emptyExp)
7383 return(emptyExp);
7384#ifdef DEBUG_DERIV
7385 printf("Empty(exp) -> Forbid\n");
7386#endif
7387 return(forbiddenExp);
7388 case XML_EXP_FORBID:
7389#ifdef DEBUG_DERIV
7390 printf("Forbid(exp) -> Forbid\n");
7391#endif
7392 return(forbiddenExp);
7393 case XML_EXP_ATOM:
7394 if (sub->type == XML_EXP_ATOM) {
7395 /* TODO: handle wildcards */
7396 if (exp->exp_str == sub->exp_str) {
7397#ifdef DEBUG_DERIV
7398 printf("Atom match -> Empty\n");
7399#endif
7400 return(emptyExp);
7401 }
7402#ifdef DEBUG_DERIV
7403 printf("Atom mismatch -> Forbid\n");
7404#endif
7405 return(forbiddenExp);
7406 }
7407 if ((sub->type == XML_EXP_COUNT) &&
7408 (sub->exp_max == 1) &&
7409 (sub->exp_left->type == XML_EXP_ATOM)) {
7410 /* TODO: handle wildcards */
7411 if (exp->exp_str == sub->exp_left->exp_str) {
7412#ifdef DEBUG_DERIV
7413 printf("Atom match -> Empty\n");
7414#endif
7415 return(emptyExp);
7416 }
7417#ifdef DEBUG_DERIV
7418 printf("Atom mismatch -> Forbid\n");
7419#endif
7420 return(forbiddenExp);
7421 }
7422#ifdef DEBUG_DERIV
7423 printf("Compex exp vs Atom -> Forbid\n");
7424#endif
7425 return(forbiddenExp);
7426 case XML_EXP_SEQ:
7427 /* try to get the sequence consumed only if possible */
7428 if (xmlExpCheckCard(exp->exp_left, sub)) {
7429 /* See if the sequence can be consumed directly */
7430#ifdef DEBUG_DERIV
7431 printf("Seq trying left only\n");
7432#endif
7433 ret = xmlExpExpDeriveInt(ctxt, exp->exp_left, sub);
7434 if ((ret != forbiddenExp) && (ret != NULL)) {
7435#ifdef DEBUG_DERIV
7436 printf("Seq trying left only worked\n");
7437#endif
7438 /*
7439 * TODO: assumption here that we are determinist
7440 * i.e. we won't get to a nillable exp left
7441 * subset which could be matched by the right
7442 * part too.
7443 * e.g.: (a | b)+,(a | c) and 'a+,a'
7444 */
7445 exp->exp_right->ref++;
7446 return(xmlExpHashGetEntry(ctxt, XML_EXP_SEQ, ret,
7447 exp->exp_right, NULL, 0, 0));
7448 }
7449#ifdef DEBUG_DERIV
7450 } else {
7451 printf("Seq: left too short\n");
7452#endif
7453 }
7454 /* Try instead to decompose */
7455 if (sub->type == XML_EXP_COUNT) {
7456 int min, max;
7457
7458#ifdef DEBUG_DERIV
7459 printf("Seq: sub is a count\n");
7460#endif
7461 ret = xmlExpExpDeriveInt(ctxt, exp->exp_left, sub->exp_left);
7462 if (ret == NULL)
7463 return(NULL);
7464 if (ret != forbiddenExp) {
7465#ifdef DEBUG_DERIV
7466 printf("Seq , Count match on left\n");
7467#endif
7468 if (sub->exp_max < 0)
7469 max = -1;
7470 else
7471 max = sub->exp_max -1;
7472 if (sub->exp_min > 0)
7473 min = sub->exp_min -1;
7474 else
7475 min = 0;
7476 exp->exp_right->ref++;
7477 tmp = xmlExpHashGetEntry(ctxt, XML_EXP_SEQ, ret,
7478 exp->exp_right, NULL, 0, 0);
7479 if (tmp == NULL)
7480 return(NULL);
7481
7482 sub->exp_left->ref++;
7483 tmp2 = xmlExpHashGetEntry(ctxt, XML_EXP_COUNT,
7484 sub->exp_left, NULL, NULL, min, max);
7485 if (tmp2 == NULL) {
7486 xmlExpFree(ctxt, tmp);
7487 return(NULL);
7488 }
7489 ret = xmlExpExpDeriveInt(ctxt, tmp, tmp2);
7490 xmlExpFree(ctxt, tmp);
7491 xmlExpFree(ctxt, tmp2);
7492 return(ret);
7493 }
7494 }
7495 /* we made no progress on structured operations */
7496 break;
7497 case XML_EXP_OR:
7498#ifdef DEBUG_DERIV
7499 printf("Or , trying both side\n");
7500#endif
7501 ret = xmlExpExpDeriveInt(ctxt, exp->exp_left, sub);
7502 if (ret == NULL)
7503 return(NULL);
7504 tmp = xmlExpExpDeriveInt(ctxt, exp->exp_right, sub);
7505 if (tmp == NULL) {
7506 xmlExpFree(ctxt, ret);
7507 return(NULL);
7508 }
7509 return(xmlExpHashGetEntry(ctxt, XML_EXP_OR, ret, tmp, NULL, 0, 0));
7510 case XML_EXP_COUNT: {
7511 int min, max;
7512
7513 if (sub->type == XML_EXP_COUNT) {
7514 /*
7515 * Try to see if the loop is completely subsumed
7516 */
7517 tmp = xmlExpExpDeriveInt(ctxt, exp->exp_left, sub->exp_left);
7518 if (tmp == NULL)
7519 return(NULL);
7520 if (tmp == forbiddenExp) {
7521 int mult;
7522
7523#ifdef DEBUG_DERIV
7524 printf("Count, Count inner don't subsume\n");
7525#endif
7526 mult = xmlExpDivide(ctxt, sub->exp_left, exp->exp_left,
7527 NULL, &tmp);
7528 if (mult <= 0) {
7529#ifdef DEBUG_DERIV
7530 printf("Count, Count not multiple => forbidden\n");
7531#endif
7532 return(forbiddenExp);
7533 }
7534 if (sub->exp_max == -1) {
7535 max = -1;
7536 if (exp->exp_max == -1) {
7537 if (exp->exp_min <= sub->exp_min * mult)
7538 min = 0;
7539 else
7540 min = exp->exp_min - sub->exp_min * mult;
7541 } else {
7542#ifdef DEBUG_DERIV
7543 printf("Count, Count finite can't subsume infinite\n");
7544#endif
7545 xmlExpFree(ctxt, tmp);
7546 return(forbiddenExp);
7547 }
7548 } else {
7549 if (exp->exp_max == -1) {
7550#ifdef DEBUG_DERIV
7551 printf("Infinite loop consume mult finite loop\n");
7552#endif
7553 if (exp->exp_min > sub->exp_min * mult) {
7554 max = -1;
7555 min = exp->exp_min - sub->exp_min * mult;
7556 } else {
7557 max = -1;
7558 min = 0;
7559 }
7560 } else {
7561 if (exp->exp_max < sub->exp_max * mult) {
7562#ifdef DEBUG_DERIV
7563 printf("loops max mult mismatch => forbidden\n");
7564#endif
7565 xmlExpFree(ctxt, tmp);
7566 return(forbiddenExp);
7567 }
7568 if (sub->exp_max * mult > exp->exp_min)
7569 min = 0;
7570 else
7571 min = exp->exp_min - sub->exp_max * mult;
7572 max = exp->exp_max - sub->exp_max * mult;
7573 }
7574 }
7575 } else if (!IS_NILLABLE(tmp)) {
7576 /*
7577 * TODO: loop here to try to grow if working on finite
7578 * blocks.
7579 */
7580#ifdef DEBUG_DERIV
7581 printf("Count, Count remain not nillable => forbidden\n");
7582#endif
7583 xmlExpFree(ctxt, tmp);
7584 return(forbiddenExp);
7585 } else if (sub->exp_max == -1) {
7586 if (exp->exp_max == -1) {
7587 if (exp->exp_min <= sub->exp_min) {
7588#ifdef DEBUG_DERIV
7589 printf("Infinite loops Okay => COUNT(0,Inf)\n");
7590#endif
7591 max = -1;
7592 min = 0;
7593 } else {
7594#ifdef DEBUG_DERIV
7595 printf("Infinite loops min => Count(X,Inf)\n");
7596#endif
7597 max = -1;
7598 min = exp->exp_min - sub->exp_min;
7599 }
7600 } else if (exp->exp_min > sub->exp_min) {
7601#ifdef DEBUG_DERIV
7602 printf("loops min mismatch 1 => forbidden ???\n");
7603#endif
7604 xmlExpFree(ctxt, tmp);
7605 return(forbiddenExp);
7606 } else {
7607 max = -1;
7608 min = 0;
7609 }
7610 } else {
7611 if (exp->exp_max == -1) {
7612#ifdef DEBUG_DERIV
7613 printf("Infinite loop consume finite loop\n");
7614#endif
7615 if (exp->exp_min > sub->exp_min) {
7616 max = -1;
7617 min = exp->exp_min - sub->exp_min;
7618 } else {
7619 max = -1;
7620 min = 0;
7621 }
7622 } else {
7623 if (exp->exp_max < sub->exp_max) {
7624#ifdef DEBUG_DERIV
7625 printf("loops max mismatch => forbidden\n");
7626#endif
7627 xmlExpFree(ctxt, tmp);
7628 return(forbiddenExp);
7629 }
7630 if (sub->exp_max > exp->exp_min)
7631 min = 0;
7632 else
7633 min = exp->exp_min - sub->exp_max;
7634 max = exp->exp_max - sub->exp_max;
7635 }
7636 }
7637#ifdef DEBUG_DERIV
7638 printf("loops match => SEQ(COUNT())\n");
7639#endif
7640 exp->exp_left->ref++;
7641 tmp2 = xmlExpHashGetEntry(ctxt, XML_EXP_COUNT, exp->exp_left,
7642 NULL, NULL, min, max);
7643 if (tmp2 == NULL) {
7644 return(NULL);
7645 }
7646 ret = xmlExpHashGetEntry(ctxt, XML_EXP_SEQ, tmp, tmp2,
7647 NULL, 0, 0);
7648 return(ret);
7649 }
7650 tmp = xmlExpExpDeriveInt(ctxt, exp->exp_left, sub);
7651 if (tmp == NULL)
7652 return(NULL);
7653 if (tmp == forbiddenExp) {
7654#ifdef DEBUG_DERIV
7655 printf("loop mismatch => forbidden\n");
7656#endif
7657 return(forbiddenExp);
7658 }
7659 if (exp->exp_min > 0)
7660 min = exp->exp_min - 1;
7661 else
7662 min = 0;
7663 if (exp->exp_max < 0)
7664 max = -1;
7665 else
7666 max = exp->exp_max - 1;
7667
7668#ifdef DEBUG_DERIV
7669 printf("loop match => SEQ(COUNT())\n");
7670#endif
7671 exp->exp_left->ref++;
7672 tmp2 = xmlExpHashGetEntry(ctxt, XML_EXP_COUNT, exp->exp_left,
7673 NULL, NULL, min, max);
7674 if (tmp2 == NULL)
7675 return(NULL);
7676 ret = xmlExpHashGetEntry(ctxt, XML_EXP_SEQ, tmp, tmp2,
7677 NULL, 0, 0);
7678 return(ret);
7679 }
7680 }
7681
Daniel Veillardccb4d412005-08-23 13:41:17 +00007682#ifdef DEBUG_DERIV
7683 printf("Fallback to derivative\n");
7684#endif
7685 if (IS_NILLABLE(sub)) {
7686 if (!(IS_NILLABLE(exp)))
7687 return(forbiddenExp);
7688 else
7689 ret = emptyExp;
7690 } else
7691 ret = NULL;
Daniel Veillard81a8ec62005-08-22 00:20:58 +00007692 /*
7693 * here the structured derivation made no progress so
7694 * we use the default token based derivation to force one more step
7695 */
7696 if (ctxt->tabSize == 0)
7697 ctxt->tabSize = 40;
7698
7699 tab = (const xmlChar **) xmlMalloc(ctxt->tabSize *
7700 sizeof(const xmlChar *));
7701 if (tab == NULL) {
7702 return(NULL);
7703 }
7704
Daniel Veillard81a8ec62005-08-22 00:20:58 +00007705 /*
7706 * collect all the strings accepted by the subexpression on input
7707 */
7708 len = xmlExpGetStartInt(ctxt, sub, tab, ctxt->tabSize, 0);
7709 while (len < 0) {
7710 const xmlChar **temp;
Rob Richards54a8f672005-10-07 02:33:00 +00007711 temp = (const xmlChar **) xmlRealloc((xmlChar **) tab, ctxt->tabSize * 2 *
Daniel Veillard81a8ec62005-08-22 00:20:58 +00007712 sizeof(const xmlChar *));
7713 if (temp == NULL) {
Rob Richards54a8f672005-10-07 02:33:00 +00007714 xmlFree((xmlChar **) tab);
Daniel Veillard81a8ec62005-08-22 00:20:58 +00007715 return(NULL);
7716 }
7717 tab = temp;
7718 ctxt->tabSize *= 2;
7719 len = xmlExpGetStartInt(ctxt, sub, tab, ctxt->tabSize, 0);
7720 }
Daniel Veillard81a8ec62005-08-22 00:20:58 +00007721 for (i = 0;i < len;i++) {
7722 tmp = xmlExpStringDeriveInt(ctxt, exp, tab[i]);
7723 if ((tmp == NULL) || (tmp == forbiddenExp)) {
7724 xmlExpFree(ctxt, ret);
Rob Richards54a8f672005-10-07 02:33:00 +00007725 xmlFree((xmlChar **) tab);
Daniel Veillard81a8ec62005-08-22 00:20:58 +00007726 return(tmp);
7727 }
7728 tmp2 = xmlExpStringDeriveInt(ctxt, sub, tab[i]);
7729 if ((tmp2 == NULL) || (tmp2 == forbiddenExp)) {
7730 xmlExpFree(ctxt, tmp);
7731 xmlExpFree(ctxt, ret);
Rob Richards54a8f672005-10-07 02:33:00 +00007732 xmlFree((xmlChar **) tab);
Daniel Veillard81a8ec62005-08-22 00:20:58 +00007733 return(tmp);
7734 }
7735 tmp3 = xmlExpExpDeriveInt(ctxt, tmp, tmp2);
7736 xmlExpFree(ctxt, tmp);
7737 xmlExpFree(ctxt, tmp2);
7738
7739 if ((tmp3 == NULL) || (tmp3 == forbiddenExp)) {
7740 xmlExpFree(ctxt, ret);
Rob Richards54a8f672005-10-07 02:33:00 +00007741 xmlFree((xmlChar **) tab);
Daniel Veillard81a8ec62005-08-22 00:20:58 +00007742 return(tmp3);
7743 }
7744
7745 if (ret == NULL)
7746 ret = tmp3;
7747 else {
7748 ret = xmlExpHashGetEntry(ctxt, XML_EXP_OR, ret, tmp3, NULL, 0, 0);
7749 if (ret == NULL) {
Rob Richards54a8f672005-10-07 02:33:00 +00007750 xmlFree((xmlChar **) tab);
Daniel Veillard81a8ec62005-08-22 00:20:58 +00007751 return(NULL);
7752 }
7753 }
7754 }
Rob Richards54a8f672005-10-07 02:33:00 +00007755 xmlFree((xmlChar **) tab);
Daniel Veillard81a8ec62005-08-22 00:20:58 +00007756 return(ret);
7757}
Daniel Veillardf8e3db02012-09-11 13:26:36 +08007758
Daniel Veillard81a8ec62005-08-22 00:20:58 +00007759/**
Daniel Veillard0090bd52005-08-22 14:43:43 +00007760 * xmlExpExpDerive:
7761 * @ctxt: the expressions context
7762 * @exp: the englobing expression
7763 * @sub: the subexpression
7764 *
7765 * Evaluates the expression resulting from @exp consuming a sub expression @sub
7766 * Based on algebraic derivation and sometimes direct Brzozowski derivation
7767 * it usually tatkes less than linear time and can handle expressions generating
7768 * infinite languages.
7769 *
7770 * Returns the resulting expression or NULL in case of internal error, the
7771 * result must be freed
7772 */
7773xmlExpNodePtr
7774xmlExpExpDerive(xmlExpCtxtPtr ctxt, xmlExpNodePtr exp, xmlExpNodePtr sub) {
7775 if ((exp == NULL) || (ctxt == NULL) || (sub == NULL))
7776 return(NULL);
7777
7778 /*
7779 * O(1) speedups
7780 */
7781 if (IS_NILLABLE(sub) && (!IS_NILLABLE(exp))) {
7782#ifdef DEBUG_DERIV
7783 printf("Sub nillable and not exp : can't subsume\n");
7784#endif
7785 return(forbiddenExp);
7786 }
7787 if (xmlExpCheckCard(exp, sub) == 0) {
7788#ifdef DEBUG_DERIV
7789 printf("sub generate longuer sequances than exp : can't subsume\n");
7790#endif
7791 return(forbiddenExp);
7792 }
7793 return(xmlExpExpDeriveInt(ctxt, exp, sub));
7794}
7795
7796/**
Daniel Veillard81a8ec62005-08-22 00:20:58 +00007797 * xmlExpSubsume:
7798 * @ctxt: the expressions context
7799 * @exp: the englobing expression
7800 * @sub: the subexpression
7801 *
7802 * Check whether @exp accepts all the languages accexpted by @sub
7803 * the input being a subexpression.
7804 *
7805 * Returns 1 if true 0 if false and -1 in case of failure.
7806 */
7807int
7808xmlExpSubsume(xmlExpCtxtPtr ctxt, xmlExpNodePtr exp, xmlExpNodePtr sub) {
7809 xmlExpNodePtr tmp;
Daniel Veillardf8e3db02012-09-11 13:26:36 +08007810
Daniel Veillard81a8ec62005-08-22 00:20:58 +00007811 if ((exp == NULL) || (ctxt == NULL) || (sub == NULL))
7812 return(-1);
7813
7814 /*
7815 * TODO: speedup by checking the language of sub is a subset of the
7816 * language of exp
7817 */
7818 /*
7819 * O(1) speedups
7820 */
7821 if (IS_NILLABLE(sub) && (!IS_NILLABLE(exp))) {
7822#ifdef DEBUG_DERIV
7823 printf("Sub nillable and not exp : can't subsume\n");
7824#endif
7825 return(0);
7826 }
7827 if (xmlExpCheckCard(exp, sub) == 0) {
7828#ifdef DEBUG_DERIV
7829 printf("sub generate longuer sequances than exp : can't subsume\n");
7830#endif
7831 return(0);
7832 }
7833 tmp = xmlExpExpDeriveInt(ctxt, exp, sub);
7834#ifdef DEBUG_DERIV
7835 printf("Result derivation :\n");
7836 PRINT_EXP(tmp);
7837#endif
7838 if (tmp == NULL)
7839 return(-1);
7840 if (tmp == forbiddenExp)
7841 return(0);
7842 if (tmp == emptyExp)
7843 return(1);
7844 if ((tmp != NULL) && (IS_NILLABLE(tmp))) {
7845 xmlExpFree(ctxt, tmp);
7846 return(1);
7847 }
7848 xmlExpFree(ctxt, tmp);
7849 return(0);
7850}
Daniel Veillard465a0002005-08-22 12:07:04 +00007851
7852/************************************************************************
7853 * *
Daniel Veillardf8e3db02012-09-11 13:26:36 +08007854 * Parsing expression *
Daniel Veillard465a0002005-08-22 12:07:04 +00007855 * *
7856 ************************************************************************/
7857
7858static xmlExpNodePtr xmlExpParseExpr(xmlExpCtxtPtr ctxt);
7859
7860#undef CUR
7861#define CUR (*ctxt->cur)
7862#undef NEXT
7863#define NEXT ctxt->cur++;
7864#undef IS_BLANK
7865#define IS_BLANK(c) ((c == ' ') || (c == '\n') || (c == '\r') || (c == '\t'))
7866#define SKIP_BLANKS while (IS_BLANK(*ctxt->cur)) ctxt->cur++;
7867
7868static int
7869xmlExpParseNumber(xmlExpCtxtPtr ctxt) {
7870 int ret = 0;
7871
7872 SKIP_BLANKS
7873 if (CUR == '*') {
7874 NEXT
7875 return(-1);
7876 }
7877 if ((CUR < '0') || (CUR > '9'))
7878 return(-1);
7879 while ((CUR >= '0') && (CUR <= '9')) {
7880 ret = ret * 10 + (CUR - '0');
7881 NEXT
7882 }
7883 return(ret);
7884}
7885
7886static xmlExpNodePtr
7887xmlExpParseOr(xmlExpCtxtPtr ctxt) {
7888 const char *base;
7889 xmlExpNodePtr ret;
7890 const xmlChar *val;
7891
7892 SKIP_BLANKS
7893 base = ctxt->cur;
7894 if (*ctxt->cur == '(') {
7895 NEXT
7896 ret = xmlExpParseExpr(ctxt);
7897 SKIP_BLANKS
7898 if (*ctxt->cur != ')') {
7899 fprintf(stderr, "unbalanced '(' : %s\n", base);
7900 xmlExpFree(ctxt, ret);
7901 return(NULL);
7902 }
7903 NEXT;
7904 SKIP_BLANKS
7905 goto parse_quantifier;
7906 }
7907 while ((CUR != 0) && (!(IS_BLANK(CUR))) && (CUR != '(') &&
7908 (CUR != ')') && (CUR != '|') && (CUR != ',') && (CUR != '{') &&
7909 (CUR != '*') && (CUR != '+') && (CUR != '?') && (CUR != '}'))
7910 NEXT;
7911 val = xmlDictLookup(ctxt->dict, BAD_CAST base, ctxt->cur - base);
7912 if (val == NULL)
7913 return(NULL);
7914 ret = xmlExpHashGetEntry(ctxt, XML_EXP_ATOM, NULL, NULL, val, 0, 0);
7915 if (ret == NULL)
7916 return(NULL);
7917 SKIP_BLANKS
7918parse_quantifier:
7919 if (CUR == '{') {
7920 int min, max;
7921
7922 NEXT
7923 min = xmlExpParseNumber(ctxt);
7924 if (min < 0) {
7925 xmlExpFree(ctxt, ret);
7926 return(NULL);
7927 }
7928 SKIP_BLANKS
7929 if (CUR == ',') {
7930 NEXT
7931 max = xmlExpParseNumber(ctxt);
7932 SKIP_BLANKS
7933 } else
7934 max = min;
7935 if (CUR != '}') {
7936 xmlExpFree(ctxt, ret);
7937 return(NULL);
7938 }
7939 NEXT
7940 ret = xmlExpHashGetEntry(ctxt, XML_EXP_COUNT, ret, NULL, NULL,
7941 min, max);
7942 SKIP_BLANKS
7943 } else if (CUR == '?') {
7944 NEXT
7945 ret = xmlExpHashGetEntry(ctxt, XML_EXP_COUNT, ret, NULL, NULL,
7946 0, 1);
7947 SKIP_BLANKS
7948 } else if (CUR == '+') {
7949 NEXT
7950 ret = xmlExpHashGetEntry(ctxt, XML_EXP_COUNT, ret, NULL, NULL,
7951 1, -1);
7952 SKIP_BLANKS
7953 } else if (CUR == '*') {
7954 NEXT
7955 ret = xmlExpHashGetEntry(ctxt, XML_EXP_COUNT, ret, NULL, NULL,
7956 0, -1);
7957 SKIP_BLANKS
Daniel Veillardf8e3db02012-09-11 13:26:36 +08007958 }
Daniel Veillard465a0002005-08-22 12:07:04 +00007959 return(ret);
7960}
7961
7962
7963static xmlExpNodePtr
7964xmlExpParseSeq(xmlExpCtxtPtr ctxt) {
7965 xmlExpNodePtr ret, right;
7966
7967 ret = xmlExpParseOr(ctxt);
7968 SKIP_BLANKS
7969 while (CUR == '|') {
7970 NEXT
7971 right = xmlExpParseOr(ctxt);
7972 if (right == NULL) {
7973 xmlExpFree(ctxt, ret);
7974 return(NULL);
7975 }
7976 ret = xmlExpHashGetEntry(ctxt, XML_EXP_OR, ret, right, NULL, 0, 0);
7977 if (ret == NULL)
7978 return(NULL);
7979 }
7980 return(ret);
7981}
7982
7983static xmlExpNodePtr
7984xmlExpParseExpr(xmlExpCtxtPtr ctxt) {
7985 xmlExpNodePtr ret, right;
7986
7987 ret = xmlExpParseSeq(ctxt);
7988 SKIP_BLANKS
7989 while (CUR == ',') {
7990 NEXT
7991 right = xmlExpParseSeq(ctxt);
7992 if (right == NULL) {
7993 xmlExpFree(ctxt, ret);
7994 return(NULL);
7995 }
7996 ret = xmlExpHashGetEntry(ctxt, XML_EXP_SEQ, ret, right, NULL, 0, 0);
7997 if (ret == NULL)
7998 return(NULL);
7999 }
8000 return(ret);
8001}
8002
8003/**
8004 * xmlExpParse:
8005 * @ctxt: the expressions context
8006 * @expr: the 0 terminated string
8007 *
8008 * Minimal parser for regexps, it understand the following constructs
8009 * - string terminals
8010 * - choice operator |
8011 * - sequence operator ,
8012 * - subexpressions (...)
8013 * - usual cardinality operators + * and ?
8014 * - finite sequences { min, max }
8015 * - infinite sequences { min, * }
8016 * There is minimal checkings made especially no checking on strings values
8017 *
8018 * Returns a new expression or NULL in case of failure
8019 */
8020xmlExpNodePtr
8021xmlExpParse(xmlExpCtxtPtr ctxt, const char *expr) {
8022 xmlExpNodePtr ret;
8023
8024 ctxt->expr = expr;
8025 ctxt->cur = expr;
8026
8027 ret = xmlExpParseExpr(ctxt);
8028 SKIP_BLANKS
8029 if (*ctxt->cur != 0) {
8030 xmlExpFree(ctxt, ret);
8031 return(NULL);
8032 }
8033 return(ret);
8034}
8035
8036static void
8037xmlExpDumpInt(xmlBufferPtr buf, xmlExpNodePtr expr, int glob) {
8038 xmlExpNodePtr c;
8039
8040 if (expr == NULL) return;
8041 if (glob) xmlBufferWriteChar(buf, "(");
8042 switch (expr->type) {
8043 case XML_EXP_EMPTY:
8044 xmlBufferWriteChar(buf, "empty");
8045 break;
8046 case XML_EXP_FORBID:
8047 xmlBufferWriteChar(buf, "forbidden");
8048 break;
8049 case XML_EXP_ATOM:
8050 xmlBufferWriteCHAR(buf, expr->exp_str);
8051 break;
8052 case XML_EXP_SEQ:
8053 c = expr->exp_left;
8054 if ((c->type == XML_EXP_SEQ) || (c->type == XML_EXP_OR))
8055 xmlExpDumpInt(buf, c, 1);
8056 else
8057 xmlExpDumpInt(buf, c, 0);
8058 xmlBufferWriteChar(buf, " , ");
8059 c = expr->exp_right;
8060 if ((c->type == XML_EXP_SEQ) || (c->type == XML_EXP_OR))
8061 xmlExpDumpInt(buf, c, 1);
8062 else
8063 xmlExpDumpInt(buf, c, 0);
8064 break;
8065 case XML_EXP_OR:
8066 c = expr->exp_left;
8067 if ((c->type == XML_EXP_SEQ) || (c->type == XML_EXP_OR))
8068 xmlExpDumpInt(buf, c, 1);
8069 else
8070 xmlExpDumpInt(buf, c, 0);
8071 xmlBufferWriteChar(buf, " | ");
8072 c = expr->exp_right;
8073 if ((c->type == XML_EXP_SEQ) || (c->type == XML_EXP_OR))
8074 xmlExpDumpInt(buf, c, 1);
8075 else
8076 xmlExpDumpInt(buf, c, 0);
8077 break;
8078 case XML_EXP_COUNT: {
8079 char rep[40];
Daniel Veillardf8e3db02012-09-11 13:26:36 +08008080
Daniel Veillard465a0002005-08-22 12:07:04 +00008081 c = expr->exp_left;
8082 if ((c->type == XML_EXP_SEQ) || (c->type == XML_EXP_OR))
8083 xmlExpDumpInt(buf, c, 1);
8084 else
8085 xmlExpDumpInt(buf, c, 0);
8086 if ((expr->exp_min == 0) && (expr->exp_max == 1)) {
8087 rep[0] = '?';
8088 rep[1] = 0;
8089 } else if ((expr->exp_min == 0) && (expr->exp_max == -1)) {
8090 rep[0] = '*';
8091 rep[1] = 0;
8092 } else if ((expr->exp_min == 1) && (expr->exp_max == -1)) {
8093 rep[0] = '+';
8094 rep[1] = 0;
8095 } else if (expr->exp_max == expr->exp_min) {
8096 snprintf(rep, 39, "{%d}", expr->exp_min);
8097 } else if (expr->exp_max < 0) {
8098 snprintf(rep, 39, "{%d,inf}", expr->exp_min);
8099 } else {
8100 snprintf(rep, 39, "{%d,%d}", expr->exp_min, expr->exp_max);
8101 }
8102 rep[39] = 0;
8103 xmlBufferWriteChar(buf, rep);
8104 break;
8105 }
8106 default:
8107 fprintf(stderr, "Error in tree\n");
8108 }
8109 if (glob)
8110 xmlBufferWriteChar(buf, ")");
8111}
8112/**
8113 * xmlExpDump:
8114 * @buf: a buffer to receive the output
8115 * @expr: the compiled expression
8116 *
8117 * Serialize the expression as compiled to the buffer
8118 */
8119void
Daniel Veillard5eee7672005-08-22 21:22:27 +00008120xmlExpDump(xmlBufferPtr buf, xmlExpNodePtr expr) {
8121 if ((buf == NULL) || (expr == NULL))
Daniel Veillard465a0002005-08-22 12:07:04 +00008122 return;
Daniel Veillard5eee7672005-08-22 21:22:27 +00008123 xmlExpDumpInt(buf, expr, 0);
Daniel Veillard465a0002005-08-22 12:07:04 +00008124}
8125
8126/**
8127 * xmlExpMaxToken:
8128 * @expr: a compiled expression
8129 *
8130 * Indicate the maximum number of input a expression can accept
8131 *
8132 * Returns the maximum length or -1 in case of error
8133 */
8134int
8135xmlExpMaxToken(xmlExpNodePtr expr) {
8136 if (expr == NULL)
8137 return(-1);
8138 return(expr->c_max);
8139}
8140
8141/**
8142 * xmlExpCtxtNbNodes:
8143 * @ctxt: an expression context
8144 *
8145 * Debugging facility provides the number of allocated nodes at a that point
8146 *
8147 * Returns the number of nodes in use or -1 in case of error
8148 */
8149int
8150xmlExpCtxtNbNodes(xmlExpCtxtPtr ctxt) {
8151 if (ctxt == NULL)
8152 return(-1);
8153 return(ctxt->nb_nodes);
8154}
8155
8156/**
8157 * xmlExpCtxtNbCons:
8158 * @ctxt: an expression context
8159 *
8160 * Debugging facility provides the number of allocated nodes over lifetime
8161 *
8162 * Returns the number of nodes ever allocated or -1 in case of error
8163 */
8164int
8165xmlExpCtxtNbCons(xmlExpCtxtPtr ctxt) {
8166 if (ctxt == NULL)
8167 return(-1);
8168 return(ctxt->nb_cons);
8169}
8170
Daniel Veillard81a8ec62005-08-22 00:20:58 +00008171#endif /* LIBXML_EXPR_ENABLED */
Daniel Veillard5d4644e2005-04-01 13:11:58 +00008172#define bottom_xmlregexp
8173#include "elfgcchack.h"
Daniel Veillard4255d502002-04-16 15:50:10 +00008174#endif /* LIBXML_REGEXP_ENABLED */