blob: 784798d7048ce7fe570d44e6eaf8d06eaf4db336 [file] [log] [blame]
Daniel Veillard4255d502002-04-16 15:50:10 +00001/*
2 * regexp.c: generic and extensible Regular Expression engine
3 *
4 * Basically designed with the purpose of compiling regexps for
5 * the variety of validation/shemas mechanisms now available in
William M. Brackddf71d62004-05-06 04:17:26 +00006 * XML related specifications these include:
Daniel Veillard4255d502002-04-16 15:50:10 +00007 * - XML-1.0 DTD validation
8 * - XML Schemas structure part 1
9 * - XML Schemas Datatypes part 2 especially Appendix F
10 * - RELAX-NG/TREX i.e. the counter proposal
11 *
12 * See Copyright for the status of this software.
13 *
14 * Daniel Veillard <veillard@redhat.com>
15 */
16
17#define IN_LIBXML
18#include "libxml.h"
19
20#ifdef LIBXML_REGEXP_ENABLED
21
Daniel Veillardcee2b3a2005-01-25 00:22:52 +000022/* #define DEBUG_ERR */
Daniel Veillardfc0b6f62005-01-09 17:48:02 +000023
Daniel Veillard4255d502002-04-16 15:50:10 +000024#include <stdio.h>
25#include <string.h>
Daniel Veillardebe48c62003-12-03 12:12:27 +000026#ifdef HAVE_LIMITS_H
27#include <limits.h>
28#endif
29
Daniel Veillard4255d502002-04-16 15:50:10 +000030#include <libxml/tree.h>
31#include <libxml/parserInternals.h>
32#include <libxml/xmlregexp.h>
33#include <libxml/xmlautomata.h>
34#include <libxml/xmlunicode.h>
35
Daniel Veillardebe48c62003-12-03 12:12:27 +000036#ifndef INT_MAX
37#define INT_MAX 123456789 /* easy to flag and big enough for our needs */
38#endif
39
Daniel Veillardc0826a72004-08-10 14:17:33 +000040/* #define DEBUG_REGEXP_GRAPH */
Daniel Veillard10752282005-08-08 13:05:13 +000041/* #define DEBUG_REGEXP_EXEC */
Daniel Veillard4255d502002-04-16 15:50:10 +000042/* #define DEBUG_PUSH */
Daniel Veillard23e73572002-09-19 19:56:43 +000043/* #define DEBUG_COMPACTION */
Daniel Veillard4255d502002-04-16 15:50:10 +000044
Daniel Veillard567a45b2005-10-18 19:11:55 +000045#define MAX_PUSH 10000000
Daniel Veillard94cc1032005-09-15 13:09:00 +000046
Daniel Veillardff46a042003-10-08 08:53:17 +000047#define ERROR(str) \
48 ctxt->error = XML_REGEXP_COMPILE_ERROR; \
49 xmlRegexpErrCompile(ctxt, str);
Daniel Veillard4255d502002-04-16 15:50:10 +000050#define NEXT ctxt->cur++
51#define CUR (*(ctxt->cur))
52#define NXT(index) (ctxt->cur[index])
53
54#define CUR_SCHAR(s, l) xmlStringCurrentChar(NULL, s, &l)
55#define NEXTL(l) ctxt->cur += l;
Daniel Veillardc0826a72004-08-10 14:17:33 +000056#define XML_REG_STRING_SEPARATOR '|'
William M. Bracka9cbf282007-03-21 13:16:33 +000057/*
58 * Need PREV to check on a '-' within a Character Group. May only be used
59 * when it's guaranteed that cur is not at the beginning of ctxt->string!
60 */
61#define PREV (ctxt->cur[-1])
Daniel Veillard4255d502002-04-16 15:50:10 +000062
Daniel Veillarde19fc232002-04-22 16:01:24 +000063/**
64 * TODO:
65 *
66 * macro to flag unimplemented blocks
67 */
68#define TODO \
69 xmlGenericError(xmlGenericErrorContext, \
70 "Unimplemented block at %s:%d\n", \
71 __FILE__, __LINE__);
72
Daniel Veillard4255d502002-04-16 15:50:10 +000073/************************************************************************
74 * *
75 * Datatypes and structures *
76 * *
77 ************************************************************************/
78
Daniel Veillardfc011b72006-02-12 19:14:15 +000079/*
80 * Note: the order of the enums below is significant, do not shuffle
81 */
Daniel Veillard4255d502002-04-16 15:50:10 +000082typedef enum {
83 XML_REGEXP_EPSILON = 1,
84 XML_REGEXP_CHARVAL,
85 XML_REGEXP_RANGES,
Daniel Veillard567a45b2005-10-18 19:11:55 +000086 XML_REGEXP_SUBREG, /* used for () sub regexps */
Daniel Veillard4255d502002-04-16 15:50:10 +000087 XML_REGEXP_STRING,
88 XML_REGEXP_ANYCHAR, /* . */
89 XML_REGEXP_ANYSPACE, /* \s */
90 XML_REGEXP_NOTSPACE, /* \S */
91 XML_REGEXP_INITNAME, /* \l */
Daniel Veillard567a45b2005-10-18 19:11:55 +000092 XML_REGEXP_NOTINITNAME, /* \L */
Daniel Veillard4255d502002-04-16 15:50:10 +000093 XML_REGEXP_NAMECHAR, /* \c */
94 XML_REGEXP_NOTNAMECHAR, /* \C */
95 XML_REGEXP_DECIMAL, /* \d */
Daniel Veillard567a45b2005-10-18 19:11:55 +000096 XML_REGEXP_NOTDECIMAL, /* \D */
Daniel Veillard4255d502002-04-16 15:50:10 +000097 XML_REGEXP_REALCHAR, /* \w */
Daniel Veillard567a45b2005-10-18 19:11:55 +000098 XML_REGEXP_NOTREALCHAR, /* \W */
99 XML_REGEXP_LETTER = 100,
Daniel Veillard4255d502002-04-16 15:50:10 +0000100 XML_REGEXP_LETTER_UPPERCASE,
101 XML_REGEXP_LETTER_LOWERCASE,
102 XML_REGEXP_LETTER_TITLECASE,
103 XML_REGEXP_LETTER_MODIFIER,
104 XML_REGEXP_LETTER_OTHERS,
105 XML_REGEXP_MARK,
106 XML_REGEXP_MARK_NONSPACING,
107 XML_REGEXP_MARK_SPACECOMBINING,
108 XML_REGEXP_MARK_ENCLOSING,
109 XML_REGEXP_NUMBER,
110 XML_REGEXP_NUMBER_DECIMAL,
111 XML_REGEXP_NUMBER_LETTER,
112 XML_REGEXP_NUMBER_OTHERS,
113 XML_REGEXP_PUNCT,
114 XML_REGEXP_PUNCT_CONNECTOR,
115 XML_REGEXP_PUNCT_DASH,
116 XML_REGEXP_PUNCT_OPEN,
117 XML_REGEXP_PUNCT_CLOSE,
118 XML_REGEXP_PUNCT_INITQUOTE,
119 XML_REGEXP_PUNCT_FINQUOTE,
120 XML_REGEXP_PUNCT_OTHERS,
121 XML_REGEXP_SEPAR,
122 XML_REGEXP_SEPAR_SPACE,
123 XML_REGEXP_SEPAR_LINE,
124 XML_REGEXP_SEPAR_PARA,
125 XML_REGEXP_SYMBOL,
126 XML_REGEXP_SYMBOL_MATH,
127 XML_REGEXP_SYMBOL_CURRENCY,
128 XML_REGEXP_SYMBOL_MODIFIER,
129 XML_REGEXP_SYMBOL_OTHERS,
130 XML_REGEXP_OTHER,
131 XML_REGEXP_OTHER_CONTROL,
132 XML_REGEXP_OTHER_FORMAT,
133 XML_REGEXP_OTHER_PRIVATE,
134 XML_REGEXP_OTHER_NA,
135 XML_REGEXP_BLOCK_NAME
136} xmlRegAtomType;
137
138typedef enum {
139 XML_REGEXP_QUANT_EPSILON = 1,
140 XML_REGEXP_QUANT_ONCE,
141 XML_REGEXP_QUANT_OPT,
142 XML_REGEXP_QUANT_MULT,
143 XML_REGEXP_QUANT_PLUS,
Daniel Veillard7646b182002-04-20 06:41:40 +0000144 XML_REGEXP_QUANT_ONCEONLY,
145 XML_REGEXP_QUANT_ALL,
Daniel Veillard4255d502002-04-16 15:50:10 +0000146 XML_REGEXP_QUANT_RANGE
147} xmlRegQuantType;
148
149typedef enum {
150 XML_REGEXP_START_STATE = 1,
151 XML_REGEXP_FINAL_STATE,
Daniel Veillardcc026dc2005-01-12 13:21:17 +0000152 XML_REGEXP_TRANS_STATE,
Daniel Veillard0e05f4c2006-11-01 15:33:04 +0000153 XML_REGEXP_SINK_STATE,
154 XML_REGEXP_UNREACH_STATE
Daniel Veillard4255d502002-04-16 15:50:10 +0000155} xmlRegStateType;
156
157typedef enum {
158 XML_REGEXP_MARK_NORMAL = 0,
159 XML_REGEXP_MARK_START,
160 XML_REGEXP_MARK_VISITED
161} xmlRegMarkedType;
162
163typedef struct _xmlRegRange xmlRegRange;
164typedef xmlRegRange *xmlRegRangePtr;
165
166struct _xmlRegRange {
Daniel Veillardf8b9de32003-11-24 14:27:26 +0000167 int neg; /* 0 normal, 1 not, 2 exclude */
Daniel Veillard4255d502002-04-16 15:50:10 +0000168 xmlRegAtomType type;
169 int start;
170 int end;
171 xmlChar *blockName;
172};
173
174typedef struct _xmlRegAtom xmlRegAtom;
175typedef xmlRegAtom *xmlRegAtomPtr;
176
177typedef struct _xmlAutomataState xmlRegState;
178typedef xmlRegState *xmlRegStatePtr;
179
180struct _xmlRegAtom {
181 int no;
182 xmlRegAtomType type;
183 xmlRegQuantType quant;
184 int min;
185 int max;
186
187 void *valuep;
Daniel Veillarda646cfd2002-09-17 21:50:03 +0000188 void *valuep2;
Daniel Veillard4255d502002-04-16 15:50:10 +0000189 int neg;
190 int codepoint;
191 xmlRegStatePtr start;
192 xmlRegStatePtr stop;
193 int maxRanges;
194 int nbRanges;
195 xmlRegRangePtr *ranges;
196 void *data;
197};
198
199typedef struct _xmlRegCounter xmlRegCounter;
200typedef xmlRegCounter *xmlRegCounterPtr;
201
202struct _xmlRegCounter {
203 int min;
204 int max;
205};
206
207typedef struct _xmlRegTrans xmlRegTrans;
208typedef xmlRegTrans *xmlRegTransPtr;
209
210struct _xmlRegTrans {
211 xmlRegAtomPtr atom;
212 int to;
213 int counter;
214 int count;
Daniel Veillard567a45b2005-10-18 19:11:55 +0000215 int nd;
Daniel Veillard4255d502002-04-16 15:50:10 +0000216};
217
218struct _xmlAutomataState {
219 xmlRegStateType type;
220 xmlRegMarkedType mark;
Daniel Veillard23e73572002-09-19 19:56:43 +0000221 xmlRegMarkedType reached;
Daniel Veillard4255d502002-04-16 15:50:10 +0000222 int no;
Daniel Veillard4255d502002-04-16 15:50:10 +0000223 int maxTrans;
224 int nbTrans;
225 xmlRegTrans *trans;
Daniel Veillarddb68b742005-07-30 13:18:24 +0000226 /* knowing states ponting to us can speed things up */
227 int maxTransTo;
228 int nbTransTo;
229 int *transTo;
Daniel Veillard4255d502002-04-16 15:50:10 +0000230};
231
232typedef struct _xmlAutomata xmlRegParserCtxt;
233typedef xmlRegParserCtxt *xmlRegParserCtxtPtr;
234
235struct _xmlAutomata {
236 xmlChar *string;
237 xmlChar *cur;
238
239 int error;
240 int neg;
241
242 xmlRegStatePtr start;
243 xmlRegStatePtr end;
244 xmlRegStatePtr state;
245
246 xmlRegAtomPtr atom;
247
248 int maxAtoms;
249 int nbAtoms;
250 xmlRegAtomPtr *atoms;
251
252 int maxStates;
253 int nbStates;
254 xmlRegStatePtr *states;
255
256 int maxCounters;
257 int nbCounters;
258 xmlRegCounter *counters;
Daniel Veillarde19fc232002-04-22 16:01:24 +0000259
260 int determinist;
Daniel Veillard6e65e152005-08-09 11:09:52 +0000261 int negs;
Daniel Veillard4255d502002-04-16 15:50:10 +0000262};
263
264struct _xmlRegexp {
265 xmlChar *string;
266 int nbStates;
267 xmlRegStatePtr *states;
268 int nbAtoms;
269 xmlRegAtomPtr *atoms;
270 int nbCounters;
271 xmlRegCounter *counters;
Daniel Veillarde19fc232002-04-22 16:01:24 +0000272 int determinist;
Daniel Veillard23e73572002-09-19 19:56:43 +0000273 /*
274 * That's the compact form for determinists automatas
275 */
276 int nbstates;
277 int *compact;
Daniel Veillard118aed72002-09-24 14:13:13 +0000278 void **transdata;
Daniel Veillard23e73572002-09-19 19:56:43 +0000279 int nbstrings;
280 xmlChar **stringMap;
Daniel Veillard4255d502002-04-16 15:50:10 +0000281};
282
283typedef struct _xmlRegExecRollback xmlRegExecRollback;
284typedef xmlRegExecRollback *xmlRegExecRollbackPtr;
285
286struct _xmlRegExecRollback {
287 xmlRegStatePtr state;/* the current state */
288 int index; /* the index in the input stack */
289 int nextbranch; /* the next transition to explore in that state */
William M. Brackddf71d62004-05-06 04:17:26 +0000290 int *counts; /* save the automata state if it has some */
Daniel Veillard4255d502002-04-16 15:50:10 +0000291};
292
293typedef struct _xmlRegInputToken xmlRegInputToken;
294typedef xmlRegInputToken *xmlRegInputTokenPtr;
295
296struct _xmlRegInputToken {
297 xmlChar *value;
298 void *data;
299};
300
301struct _xmlRegExecCtxt {
302 int status; /* execution status != 0 indicate an error */
William M. Brackddf71d62004-05-06 04:17:26 +0000303 int determinist; /* did we find an indeterministic behaviour */
Daniel Veillard4255d502002-04-16 15:50:10 +0000304 xmlRegexpPtr comp; /* the compiled regexp */
305 xmlRegExecCallbacks callback;
306 void *data;
307
308 xmlRegStatePtr state;/* the current state */
309 int transno; /* the current transition on that state */
William M. Brackddf71d62004-05-06 04:17:26 +0000310 int transcount; /* the number of chars in char counted transitions */
Daniel Veillard4255d502002-04-16 15:50:10 +0000311
312 /*
313 * A stack of rollback states
314 */
315 int maxRollbacks;
316 int nbRollbacks;
317 xmlRegExecRollback *rollbacks;
318
319 /*
320 * The state of the automata if any
321 */
322 int *counts;
323
324 /*
325 * The input stack
326 */
327 int inputStackMax;
328 int inputStackNr;
329 int index;
330 int *charStack;
331 const xmlChar *inputString; /* when operating on characters */
332 xmlRegInputTokenPtr inputStack;/* when operating on strings */
333
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +0000334 /*
335 * error handling
336 */
337 int errStateNo; /* the error state number */
338 xmlRegStatePtr errState; /* the error state */
339 xmlChar *errString; /* the string raising the error */
340 int *errCounts; /* counters at the error state */
Daniel Veillard94cc1032005-09-15 13:09:00 +0000341 int nbPush;
Daniel Veillard4255d502002-04-16 15:50:10 +0000342};
343
Daniel Veillard441bc322002-04-20 17:38:48 +0000344#define REGEXP_ALL_COUNTER 0x123456
345#define REGEXP_ALL_LAX_COUNTER 0x123457
Daniel Veillard7646b182002-04-20 06:41:40 +0000346
Daniel Veillard4255d502002-04-16 15:50:10 +0000347static void xmlFAParseRegExp(xmlRegParserCtxtPtr ctxt, int top);
Daniel Veillard23e73572002-09-19 19:56:43 +0000348static void xmlRegFreeState(xmlRegStatePtr state);
349static void xmlRegFreeAtom(xmlRegAtomPtr atom);
Daniel Veillard9efc4762005-07-19 14:33:55 +0000350static int xmlRegStrEqualWildcard(const xmlChar *expStr, const xmlChar *valStr);
Daniel Veillard567a45b2005-10-18 19:11:55 +0000351static int xmlRegCheckCharacter(xmlRegAtomPtr atom, int codepoint);
352static int xmlRegCheckCharacterRange(xmlRegAtomType type, int codepoint,
353 int neg, int start, int end, const xmlChar *blockName);
Daniel Veillard4255d502002-04-16 15:50:10 +0000354
355/************************************************************************
Daniel Veillardff46a042003-10-08 08:53:17 +0000356 * *
357 * Regexp memory error handler *
358 * *
359 ************************************************************************/
360/**
361 * xmlRegexpErrMemory:
William M. Brackddf71d62004-05-06 04:17:26 +0000362 * @extra: extra information
Daniel Veillardff46a042003-10-08 08:53:17 +0000363 *
364 * Handle an out of memory condition
365 */
366static void
367xmlRegexpErrMemory(xmlRegParserCtxtPtr ctxt, const char *extra)
368{
369 const char *regexp = NULL;
370 if (ctxt != NULL) {
371 regexp = (const char *) ctxt->string;
372 ctxt->error = XML_ERR_NO_MEMORY;
373 }
Daniel Veillard659e71e2003-10-10 14:10:40 +0000374 __xmlRaiseError(NULL, NULL, NULL, NULL, NULL, XML_FROM_REGEXP,
Daniel Veillardff46a042003-10-08 08:53:17 +0000375 XML_ERR_NO_MEMORY, XML_ERR_FATAL, NULL, 0, extra,
376 regexp, NULL, 0, 0,
377 "Memory allocation failed : %s\n", extra);
378}
379
380/**
381 * xmlRegexpErrCompile:
William M. Brackddf71d62004-05-06 04:17:26 +0000382 * @extra: extra information
Daniel Veillardff46a042003-10-08 08:53:17 +0000383 *
William M. Brackddf71d62004-05-06 04:17:26 +0000384 * Handle a compilation failure
Daniel Veillardff46a042003-10-08 08:53:17 +0000385 */
386static void
387xmlRegexpErrCompile(xmlRegParserCtxtPtr ctxt, const char *extra)
388{
389 const char *regexp = NULL;
390 int idx = 0;
391
392 if (ctxt != NULL) {
393 regexp = (const char *) ctxt->string;
394 idx = ctxt->cur - ctxt->string;
395 ctxt->error = XML_REGEXP_COMPILE_ERROR;
396 }
Daniel Veillard659e71e2003-10-10 14:10:40 +0000397 __xmlRaiseError(NULL, NULL, NULL, NULL, NULL, XML_FROM_REGEXP,
Daniel Veillardff46a042003-10-08 08:53:17 +0000398 XML_REGEXP_COMPILE_ERROR, XML_ERR_FATAL, NULL, 0, extra,
399 regexp, NULL, idx, 0,
400 "failed to compile: %s\n", extra);
401}
402
403/************************************************************************
Daniel Veillard4255d502002-04-16 15:50:10 +0000404 * *
405 * Allocation/Deallocation *
406 * *
407 ************************************************************************/
408
Daniel Veillard23e73572002-09-19 19:56:43 +0000409static int xmlFAComputesDeterminism(xmlRegParserCtxtPtr ctxt);
Daniel Veillard4255d502002-04-16 15:50:10 +0000410/**
411 * xmlRegEpxFromParse:
412 * @ctxt: the parser context used to build it
413 *
William M. Brackddf71d62004-05-06 04:17:26 +0000414 * Allocate a new regexp and fill it with the result from the parser
Daniel Veillard4255d502002-04-16 15:50:10 +0000415 *
416 * Returns the new regexp or NULL in case of error
417 */
418static xmlRegexpPtr
419xmlRegEpxFromParse(xmlRegParserCtxtPtr ctxt) {
420 xmlRegexpPtr ret;
421
422 ret = (xmlRegexpPtr) xmlMalloc(sizeof(xmlRegexp));
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000423 if (ret == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +0000424 xmlRegexpErrMemory(ctxt, "compiling regexp");
Daniel Veillard4255d502002-04-16 15:50:10 +0000425 return(NULL);
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000426 }
Daniel Veillard4255d502002-04-16 15:50:10 +0000427 memset(ret, 0, sizeof(xmlRegexp));
428 ret->string = ctxt->string;
Daniel Veillard4255d502002-04-16 15:50:10 +0000429 ret->nbStates = ctxt->nbStates;
Daniel Veillard4255d502002-04-16 15:50:10 +0000430 ret->states = ctxt->states;
Daniel Veillard4255d502002-04-16 15:50:10 +0000431 ret->nbAtoms = ctxt->nbAtoms;
Daniel Veillard4255d502002-04-16 15:50:10 +0000432 ret->atoms = ctxt->atoms;
Daniel Veillard4255d502002-04-16 15:50:10 +0000433 ret->nbCounters = ctxt->nbCounters;
Daniel Veillard4255d502002-04-16 15:50:10 +0000434 ret->counters = ctxt->counters;
Daniel Veillarde19fc232002-04-22 16:01:24 +0000435 ret->determinist = ctxt->determinist;
Daniel Veillard567a45b2005-10-18 19:11:55 +0000436 if (ret->determinist == -1) {
437 xmlRegexpIsDeterminist(ret);
438 }
Daniel Veillard23e73572002-09-19 19:56:43 +0000439
440 if ((ret->determinist != 0) &&
441 (ret->nbCounters == 0) &&
Daniel Veillard6e65e152005-08-09 11:09:52 +0000442 (ctxt->negs == 0) &&
Daniel Veillard118aed72002-09-24 14:13:13 +0000443 (ret->atoms != NULL) &&
Daniel Veillard23e73572002-09-19 19:56:43 +0000444 (ret->atoms[0] != NULL) &&
445 (ret->atoms[0]->type == XML_REGEXP_STRING)) {
446 int i, j, nbstates = 0, nbatoms = 0;
447 int *stateRemap;
448 int *stringRemap;
449 int *transitions;
Daniel Veillard118aed72002-09-24 14:13:13 +0000450 void **transdata;
Daniel Veillard23e73572002-09-19 19:56:43 +0000451 xmlChar **stringMap;
452 xmlChar *value;
453
454 /*
455 * Switch to a compact representation
456 * 1/ counting the effective number of states left
William M. Brackddf71d62004-05-06 04:17:26 +0000457 * 2/ counting the unique number of atoms, and check that
Daniel Veillard23e73572002-09-19 19:56:43 +0000458 * they are all of the string type
459 * 3/ build a table state x atom for the transitions
460 */
461
462 stateRemap = xmlMalloc(ret->nbStates * sizeof(int));
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000463 if (stateRemap == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +0000464 xmlRegexpErrMemory(ctxt, "compiling regexp");
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000465 xmlFree(ret);
466 return(NULL);
467 }
Daniel Veillard23e73572002-09-19 19:56:43 +0000468 for (i = 0;i < ret->nbStates;i++) {
469 if (ret->states[i] != NULL) {
470 stateRemap[i] = nbstates;
471 nbstates++;
472 } else {
473 stateRemap[i] = -1;
474 }
475 }
476#ifdef DEBUG_COMPACTION
477 printf("Final: %d states\n", nbstates);
478#endif
479 stringMap = xmlMalloc(ret->nbAtoms * sizeof(char *));
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000480 if (stringMap == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +0000481 xmlRegexpErrMemory(ctxt, "compiling regexp");
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000482 xmlFree(stateRemap);
483 xmlFree(ret);
484 return(NULL);
485 }
Daniel Veillard23e73572002-09-19 19:56:43 +0000486 stringRemap = xmlMalloc(ret->nbAtoms * sizeof(int));
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000487 if (stringRemap == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +0000488 xmlRegexpErrMemory(ctxt, "compiling regexp");
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000489 xmlFree(stringMap);
490 xmlFree(stateRemap);
491 xmlFree(ret);
492 return(NULL);
493 }
Daniel Veillard23e73572002-09-19 19:56:43 +0000494 for (i = 0;i < ret->nbAtoms;i++) {
495 if ((ret->atoms[i]->type == XML_REGEXP_STRING) &&
496 (ret->atoms[i]->quant == XML_REGEXP_QUANT_ONCE)) {
497 value = ret->atoms[i]->valuep;
498 for (j = 0;j < nbatoms;j++) {
499 if (xmlStrEqual(stringMap[j], value)) {
500 stringRemap[i] = j;
501 break;
502 }
503 }
504 if (j >= nbatoms) {
505 stringRemap[i] = nbatoms;
506 stringMap[nbatoms] = xmlStrdup(value);
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000507 if (stringMap[nbatoms] == NULL) {
508 for (i = 0;i < nbatoms;i++)
509 xmlFree(stringMap[i]);
510 xmlFree(stringRemap);
511 xmlFree(stringMap);
512 xmlFree(stateRemap);
513 xmlFree(ret);
514 return(NULL);
515 }
Daniel Veillard23e73572002-09-19 19:56:43 +0000516 nbatoms++;
517 }
518 } else {
519 xmlFree(stateRemap);
520 xmlFree(stringRemap);
521 for (i = 0;i < nbatoms;i++)
522 xmlFree(stringMap[i]);
523 xmlFree(stringMap);
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000524 xmlFree(ret);
525 return(NULL);
Daniel Veillard23e73572002-09-19 19:56:43 +0000526 }
527 }
528#ifdef DEBUG_COMPACTION
529 printf("Final: %d atoms\n", nbatoms);
530#endif
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000531 transitions = (int *) xmlMalloc((nbstates + 1) *
532 (nbatoms + 1) * sizeof(int));
533 if (transitions == NULL) {
534 xmlFree(stateRemap);
535 xmlFree(stringRemap);
536 xmlFree(stringMap);
537 xmlFree(ret);
538 return(NULL);
539 }
540 memset(transitions, 0, (nbstates + 1) * (nbatoms + 1) * sizeof(int));
Daniel Veillard23e73572002-09-19 19:56:43 +0000541
542 /*
543 * Allocate the transition table. The first entry for each
William M. Brackddf71d62004-05-06 04:17:26 +0000544 * state corresponds to the state type.
Daniel Veillard23e73572002-09-19 19:56:43 +0000545 */
Daniel Veillard118aed72002-09-24 14:13:13 +0000546 transdata = NULL;
Daniel Veillard23e73572002-09-19 19:56:43 +0000547
548 for (i = 0;i < ret->nbStates;i++) {
549 int stateno, atomno, targetno, prev;
550 xmlRegStatePtr state;
551 xmlRegTransPtr trans;
552
553 stateno = stateRemap[i];
554 if (stateno == -1)
555 continue;
556 state = ret->states[i];
557
558 transitions[stateno * (nbatoms + 1)] = state->type;
559
560 for (j = 0;j < state->nbTrans;j++) {
561 trans = &(state->trans[j]);
562 if ((trans->to == -1) || (trans->atom == NULL))
563 continue;
564 atomno = stringRemap[trans->atom->no];
Daniel Veillard118aed72002-09-24 14:13:13 +0000565 if ((trans->atom->data != NULL) && (transdata == NULL)) {
566 transdata = (void **) xmlMalloc(nbstates * nbatoms *
567 sizeof(void *));
568 if (transdata != NULL)
569 memset(transdata, 0,
570 nbstates * nbatoms * sizeof(void *));
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000571 else {
Daniel Veillardff46a042003-10-08 08:53:17 +0000572 xmlRegexpErrMemory(ctxt, "compiling regexp");
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000573 break;
574 }
Daniel Veillard118aed72002-09-24 14:13:13 +0000575 }
Daniel Veillard23e73572002-09-19 19:56:43 +0000576 targetno = stateRemap[trans->to];
577 /*
William M. Brackddf71d62004-05-06 04:17:26 +0000578 * if the same atom can generate transitions to 2 different
Daniel Veillard23e73572002-09-19 19:56:43 +0000579 * states then it means the automata is not determinist and
580 * the compact form can't be used !
581 */
582 prev = transitions[stateno * (nbatoms + 1) + atomno + 1];
583 if (prev != 0) {
584 if (prev != targetno + 1) {
Daniel Veillard23e73572002-09-19 19:56:43 +0000585 ret->determinist = 0;
586#ifdef DEBUG_COMPACTION
587 printf("Indet: state %d trans %d, atom %d to %d : %d to %d\n",
588 i, j, trans->atom->no, trans->to, atomno, targetno);
589 printf(" previous to is %d\n", prev);
590#endif
Daniel Veillard118aed72002-09-24 14:13:13 +0000591 if (transdata != NULL)
592 xmlFree(transdata);
Daniel Veillard23e73572002-09-19 19:56:43 +0000593 xmlFree(transitions);
594 xmlFree(stateRemap);
595 xmlFree(stringRemap);
596 for (i = 0;i < nbatoms;i++)
597 xmlFree(stringMap[i]);
598 xmlFree(stringMap);
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000599 goto not_determ;
Daniel Veillard23e73572002-09-19 19:56:43 +0000600 }
601 } else {
602#if 0
603 printf("State %d trans %d: atom %d to %d : %d to %d\n",
604 i, j, trans->atom->no, trans->to, atomno, targetno);
605#endif
606 transitions[stateno * (nbatoms + 1) + atomno + 1] =
Daniel Veillard118aed72002-09-24 14:13:13 +0000607 targetno + 1; /* to avoid 0 */
608 if (transdata != NULL)
609 transdata[stateno * nbatoms + atomno] =
610 trans->atom->data;
Daniel Veillard23e73572002-09-19 19:56:43 +0000611 }
612 }
613 }
614 ret->determinist = 1;
615#ifdef DEBUG_COMPACTION
616 /*
617 * Debug
618 */
619 for (i = 0;i < nbstates;i++) {
620 for (j = 0;j < nbatoms + 1;j++) {
621 printf("%02d ", transitions[i * (nbatoms + 1) + j]);
622 }
623 printf("\n");
624 }
625 printf("\n");
626#endif
627 /*
628 * Cleanup of the old data
629 */
630 if (ret->states != NULL) {
631 for (i = 0;i < ret->nbStates;i++)
632 xmlRegFreeState(ret->states[i]);
633 xmlFree(ret->states);
634 }
635 ret->states = NULL;
636 ret->nbStates = 0;
637 if (ret->atoms != NULL) {
638 for (i = 0;i < ret->nbAtoms;i++)
639 xmlRegFreeAtom(ret->atoms[i]);
640 xmlFree(ret->atoms);
641 }
642 ret->atoms = NULL;
643 ret->nbAtoms = 0;
644
645 ret->compact = transitions;
Daniel Veillard118aed72002-09-24 14:13:13 +0000646 ret->transdata = transdata;
Daniel Veillard23e73572002-09-19 19:56:43 +0000647 ret->stringMap = stringMap;
648 ret->nbstrings = nbatoms;
649 ret->nbstates = nbstates;
650 xmlFree(stateRemap);
651 xmlFree(stringRemap);
652 }
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000653not_determ:
654 ctxt->string = NULL;
655 ctxt->nbStates = 0;
656 ctxt->states = NULL;
657 ctxt->nbAtoms = 0;
658 ctxt->atoms = NULL;
659 ctxt->nbCounters = 0;
660 ctxt->counters = NULL;
Daniel Veillard4255d502002-04-16 15:50:10 +0000661 return(ret);
662}
663
664/**
665 * xmlRegNewParserCtxt:
666 * @string: the string to parse
667 *
668 * Allocate a new regexp parser context
669 *
670 * Returns the new context or NULL in case of error
671 */
672static xmlRegParserCtxtPtr
673xmlRegNewParserCtxt(const xmlChar *string) {
674 xmlRegParserCtxtPtr ret;
675
676 ret = (xmlRegParserCtxtPtr) xmlMalloc(sizeof(xmlRegParserCtxt));
677 if (ret == NULL)
678 return(NULL);
679 memset(ret, 0, sizeof(xmlRegParserCtxt));
680 if (string != NULL)
681 ret->string = xmlStrdup(string);
682 ret->cur = ret->string;
683 ret->neg = 0;
Daniel Veillard6e65e152005-08-09 11:09:52 +0000684 ret->negs = 0;
Daniel Veillard4255d502002-04-16 15:50:10 +0000685 ret->error = 0;
Daniel Veillarde19fc232002-04-22 16:01:24 +0000686 ret->determinist = -1;
Daniel Veillard4255d502002-04-16 15:50:10 +0000687 return(ret);
688}
689
690/**
691 * xmlRegNewRange:
692 * @ctxt: the regexp parser context
693 * @neg: is that negative
694 * @type: the type of range
695 * @start: the start codepoint
696 * @end: the end codepoint
697 *
698 * Allocate a new regexp range
699 *
700 * Returns the new range or NULL in case of error
701 */
702static xmlRegRangePtr
703xmlRegNewRange(xmlRegParserCtxtPtr ctxt,
704 int neg, xmlRegAtomType type, int start, int end) {
705 xmlRegRangePtr ret;
706
707 ret = (xmlRegRangePtr) xmlMalloc(sizeof(xmlRegRange));
708 if (ret == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +0000709 xmlRegexpErrMemory(ctxt, "allocating range");
Daniel Veillard4255d502002-04-16 15:50:10 +0000710 return(NULL);
711 }
712 ret->neg = neg;
713 ret->type = type;
714 ret->start = start;
715 ret->end = end;
716 return(ret);
717}
718
719/**
720 * xmlRegFreeRange:
721 * @range: the regexp range
722 *
723 * Free a regexp range
724 */
725static void
726xmlRegFreeRange(xmlRegRangePtr range) {
727 if (range == NULL)
728 return;
729
730 if (range->blockName != NULL)
731 xmlFree(range->blockName);
732 xmlFree(range);
733}
734
735/**
736 * xmlRegNewAtom:
737 * @ctxt: the regexp parser context
738 * @type: the type of atom
739 *
740 * Allocate a new regexp range
741 *
742 * Returns the new atom or NULL in case of error
743 */
744static xmlRegAtomPtr
745xmlRegNewAtom(xmlRegParserCtxtPtr ctxt, xmlRegAtomType type) {
746 xmlRegAtomPtr ret;
747
748 ret = (xmlRegAtomPtr) xmlMalloc(sizeof(xmlRegAtom));
749 if (ret == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +0000750 xmlRegexpErrMemory(ctxt, "allocating atom");
Daniel Veillard4255d502002-04-16 15:50:10 +0000751 return(NULL);
752 }
753 memset(ret, 0, sizeof(xmlRegAtom));
754 ret->type = type;
755 ret->quant = XML_REGEXP_QUANT_ONCE;
756 ret->min = 0;
757 ret->max = 0;
758 return(ret);
759}
760
761/**
762 * xmlRegFreeAtom:
763 * @atom: the regexp atom
764 *
765 * Free a regexp atom
766 */
767static void
768xmlRegFreeAtom(xmlRegAtomPtr atom) {
769 int i;
770
771 if (atom == NULL)
772 return;
773
774 for (i = 0;i < atom->nbRanges;i++)
775 xmlRegFreeRange(atom->ranges[i]);
776 if (atom->ranges != NULL)
777 xmlFree(atom->ranges);
Daniel Veillardde0e4982005-07-03 14:35:44 +0000778 if ((atom->type == XML_REGEXP_STRING) && (atom->valuep != NULL))
779 xmlFree(atom->valuep);
Daniel Veillard77005e62005-07-19 16:26:18 +0000780 if ((atom->type == XML_REGEXP_STRING) && (atom->valuep2 != NULL))
781 xmlFree(atom->valuep2);
Daniel Veillardde0e4982005-07-03 14:35:44 +0000782 if ((atom->type == XML_REGEXP_BLOCK_NAME) && (atom->valuep != NULL))
Daniel Veillard4255d502002-04-16 15:50:10 +0000783 xmlFree(atom->valuep);
784 xmlFree(atom);
785}
786
787static xmlRegStatePtr
788xmlRegNewState(xmlRegParserCtxtPtr ctxt) {
789 xmlRegStatePtr ret;
790
791 ret = (xmlRegStatePtr) xmlMalloc(sizeof(xmlRegState));
792 if (ret == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +0000793 xmlRegexpErrMemory(ctxt, "allocating state");
Daniel Veillard4255d502002-04-16 15:50:10 +0000794 return(NULL);
795 }
796 memset(ret, 0, sizeof(xmlRegState));
797 ret->type = XML_REGEXP_TRANS_STATE;
798 ret->mark = XML_REGEXP_MARK_NORMAL;
799 return(ret);
800}
801
802/**
803 * xmlRegFreeState:
804 * @state: the regexp state
805 *
806 * Free a regexp state
807 */
808static void
809xmlRegFreeState(xmlRegStatePtr state) {
810 if (state == NULL)
811 return;
812
813 if (state->trans != NULL)
814 xmlFree(state->trans);
Daniel Veillarddb68b742005-07-30 13:18:24 +0000815 if (state->transTo != NULL)
816 xmlFree(state->transTo);
Daniel Veillard4255d502002-04-16 15:50:10 +0000817 xmlFree(state);
818}
819
820/**
821 * xmlRegFreeParserCtxt:
822 * @ctxt: the regexp parser context
823 *
824 * Free a regexp parser context
825 */
826static void
827xmlRegFreeParserCtxt(xmlRegParserCtxtPtr ctxt) {
828 int i;
829 if (ctxt == NULL)
830 return;
831
832 if (ctxt->string != NULL)
833 xmlFree(ctxt->string);
834 if (ctxt->states != NULL) {
835 for (i = 0;i < ctxt->nbStates;i++)
836 xmlRegFreeState(ctxt->states[i]);
837 xmlFree(ctxt->states);
838 }
839 if (ctxt->atoms != NULL) {
840 for (i = 0;i < ctxt->nbAtoms;i++)
841 xmlRegFreeAtom(ctxt->atoms[i]);
842 xmlFree(ctxt->atoms);
843 }
844 if (ctxt->counters != NULL)
845 xmlFree(ctxt->counters);
846 xmlFree(ctxt);
847}
848
849/************************************************************************
850 * *
851 * Display of Data structures *
852 * *
853 ************************************************************************/
854
855static void
856xmlRegPrintAtomType(FILE *output, xmlRegAtomType type) {
857 switch (type) {
858 case XML_REGEXP_EPSILON:
859 fprintf(output, "epsilon "); break;
860 case XML_REGEXP_CHARVAL:
861 fprintf(output, "charval "); break;
862 case XML_REGEXP_RANGES:
863 fprintf(output, "ranges "); break;
864 case XML_REGEXP_SUBREG:
865 fprintf(output, "subexpr "); break;
866 case XML_REGEXP_STRING:
867 fprintf(output, "string "); break;
868 case XML_REGEXP_ANYCHAR:
869 fprintf(output, "anychar "); break;
870 case XML_REGEXP_ANYSPACE:
871 fprintf(output, "anyspace "); break;
872 case XML_REGEXP_NOTSPACE:
873 fprintf(output, "notspace "); break;
874 case XML_REGEXP_INITNAME:
875 fprintf(output, "initname "); break;
876 case XML_REGEXP_NOTINITNAME:
877 fprintf(output, "notinitname "); break;
878 case XML_REGEXP_NAMECHAR:
879 fprintf(output, "namechar "); break;
880 case XML_REGEXP_NOTNAMECHAR:
881 fprintf(output, "notnamechar "); break;
882 case XML_REGEXP_DECIMAL:
883 fprintf(output, "decimal "); break;
884 case XML_REGEXP_NOTDECIMAL:
885 fprintf(output, "notdecimal "); break;
886 case XML_REGEXP_REALCHAR:
887 fprintf(output, "realchar "); break;
888 case XML_REGEXP_NOTREALCHAR:
889 fprintf(output, "notrealchar "); break;
890 case XML_REGEXP_LETTER:
891 fprintf(output, "LETTER "); break;
892 case XML_REGEXP_LETTER_UPPERCASE:
893 fprintf(output, "LETTER_UPPERCASE "); break;
894 case XML_REGEXP_LETTER_LOWERCASE:
895 fprintf(output, "LETTER_LOWERCASE "); break;
896 case XML_REGEXP_LETTER_TITLECASE:
897 fprintf(output, "LETTER_TITLECASE "); break;
898 case XML_REGEXP_LETTER_MODIFIER:
899 fprintf(output, "LETTER_MODIFIER "); break;
900 case XML_REGEXP_LETTER_OTHERS:
901 fprintf(output, "LETTER_OTHERS "); break;
902 case XML_REGEXP_MARK:
903 fprintf(output, "MARK "); break;
904 case XML_REGEXP_MARK_NONSPACING:
905 fprintf(output, "MARK_NONSPACING "); break;
906 case XML_REGEXP_MARK_SPACECOMBINING:
907 fprintf(output, "MARK_SPACECOMBINING "); break;
908 case XML_REGEXP_MARK_ENCLOSING:
909 fprintf(output, "MARK_ENCLOSING "); break;
910 case XML_REGEXP_NUMBER:
911 fprintf(output, "NUMBER "); break;
912 case XML_REGEXP_NUMBER_DECIMAL:
913 fprintf(output, "NUMBER_DECIMAL "); break;
914 case XML_REGEXP_NUMBER_LETTER:
915 fprintf(output, "NUMBER_LETTER "); break;
916 case XML_REGEXP_NUMBER_OTHERS:
917 fprintf(output, "NUMBER_OTHERS "); break;
918 case XML_REGEXP_PUNCT:
919 fprintf(output, "PUNCT "); break;
920 case XML_REGEXP_PUNCT_CONNECTOR:
921 fprintf(output, "PUNCT_CONNECTOR "); break;
922 case XML_REGEXP_PUNCT_DASH:
923 fprintf(output, "PUNCT_DASH "); break;
924 case XML_REGEXP_PUNCT_OPEN:
925 fprintf(output, "PUNCT_OPEN "); break;
926 case XML_REGEXP_PUNCT_CLOSE:
927 fprintf(output, "PUNCT_CLOSE "); break;
928 case XML_REGEXP_PUNCT_INITQUOTE:
929 fprintf(output, "PUNCT_INITQUOTE "); break;
930 case XML_REGEXP_PUNCT_FINQUOTE:
931 fprintf(output, "PUNCT_FINQUOTE "); break;
932 case XML_REGEXP_PUNCT_OTHERS:
933 fprintf(output, "PUNCT_OTHERS "); break;
934 case XML_REGEXP_SEPAR:
935 fprintf(output, "SEPAR "); break;
936 case XML_REGEXP_SEPAR_SPACE:
937 fprintf(output, "SEPAR_SPACE "); break;
938 case XML_REGEXP_SEPAR_LINE:
939 fprintf(output, "SEPAR_LINE "); break;
940 case XML_REGEXP_SEPAR_PARA:
941 fprintf(output, "SEPAR_PARA "); break;
942 case XML_REGEXP_SYMBOL:
943 fprintf(output, "SYMBOL "); break;
944 case XML_REGEXP_SYMBOL_MATH:
945 fprintf(output, "SYMBOL_MATH "); break;
946 case XML_REGEXP_SYMBOL_CURRENCY:
947 fprintf(output, "SYMBOL_CURRENCY "); break;
948 case XML_REGEXP_SYMBOL_MODIFIER:
949 fprintf(output, "SYMBOL_MODIFIER "); break;
950 case XML_REGEXP_SYMBOL_OTHERS:
951 fprintf(output, "SYMBOL_OTHERS "); break;
952 case XML_REGEXP_OTHER:
953 fprintf(output, "OTHER "); break;
954 case XML_REGEXP_OTHER_CONTROL:
955 fprintf(output, "OTHER_CONTROL "); break;
956 case XML_REGEXP_OTHER_FORMAT:
957 fprintf(output, "OTHER_FORMAT "); break;
958 case XML_REGEXP_OTHER_PRIVATE:
959 fprintf(output, "OTHER_PRIVATE "); break;
960 case XML_REGEXP_OTHER_NA:
961 fprintf(output, "OTHER_NA "); break;
962 case XML_REGEXP_BLOCK_NAME:
963 fprintf(output, "BLOCK "); break;
964 }
965}
966
967static void
968xmlRegPrintQuantType(FILE *output, xmlRegQuantType type) {
969 switch (type) {
970 case XML_REGEXP_QUANT_EPSILON:
971 fprintf(output, "epsilon "); break;
972 case XML_REGEXP_QUANT_ONCE:
973 fprintf(output, "once "); break;
974 case XML_REGEXP_QUANT_OPT:
975 fprintf(output, "? "); break;
976 case XML_REGEXP_QUANT_MULT:
977 fprintf(output, "* "); break;
978 case XML_REGEXP_QUANT_PLUS:
979 fprintf(output, "+ "); break;
980 case XML_REGEXP_QUANT_RANGE:
981 fprintf(output, "range "); break;
Daniel Veillard7646b182002-04-20 06:41:40 +0000982 case XML_REGEXP_QUANT_ONCEONLY:
983 fprintf(output, "onceonly "); break;
984 case XML_REGEXP_QUANT_ALL:
985 fprintf(output, "all "); break;
Daniel Veillard4255d502002-04-16 15:50:10 +0000986 }
987}
988static void
989xmlRegPrintRange(FILE *output, xmlRegRangePtr range) {
990 fprintf(output, " range: ");
991 if (range->neg)
992 fprintf(output, "negative ");
993 xmlRegPrintAtomType(output, range->type);
994 fprintf(output, "%c - %c\n", range->start, range->end);
995}
996
997static void
998xmlRegPrintAtom(FILE *output, xmlRegAtomPtr atom) {
999 fprintf(output, " atom: ");
1000 if (atom == NULL) {
1001 fprintf(output, "NULL\n");
1002 return;
1003 }
Daniel Veillard9efc4762005-07-19 14:33:55 +00001004 if (atom->neg)
1005 fprintf(output, "not ");
Daniel Veillard4255d502002-04-16 15:50:10 +00001006 xmlRegPrintAtomType(output, atom->type);
1007 xmlRegPrintQuantType(output, atom->quant);
1008 if (atom->quant == XML_REGEXP_QUANT_RANGE)
1009 fprintf(output, "%d-%d ", atom->min, atom->max);
1010 if (atom->type == XML_REGEXP_STRING)
1011 fprintf(output, "'%s' ", (char *) atom->valuep);
1012 if (atom->type == XML_REGEXP_CHARVAL)
1013 fprintf(output, "char %c\n", atom->codepoint);
1014 else if (atom->type == XML_REGEXP_RANGES) {
1015 int i;
1016 fprintf(output, "%d entries\n", atom->nbRanges);
1017 for (i = 0; i < atom->nbRanges;i++)
1018 xmlRegPrintRange(output, atom->ranges[i]);
1019 } else if (atom->type == XML_REGEXP_SUBREG) {
1020 fprintf(output, "start %d end %d\n", atom->start->no, atom->stop->no);
1021 } else {
1022 fprintf(output, "\n");
1023 }
1024}
1025
1026static void
1027xmlRegPrintTrans(FILE *output, xmlRegTransPtr trans) {
1028 fprintf(output, " trans: ");
1029 if (trans == NULL) {
1030 fprintf(output, "NULL\n");
1031 return;
1032 }
1033 if (trans->to < 0) {
1034 fprintf(output, "removed\n");
1035 return;
1036 }
Daniel Veillard567a45b2005-10-18 19:11:55 +00001037 if (trans->nd != 0) {
1038 if (trans->nd == 2)
1039 fprintf(output, "last not determinist, ");
1040 else
1041 fprintf(output, "not determinist, ");
1042 }
Daniel Veillard4255d502002-04-16 15:50:10 +00001043 if (trans->counter >= 0) {
1044 fprintf(output, "counted %d, ", trans->counter);
1045 }
Daniel Veillard8a001f62002-04-20 07:24:11 +00001046 if (trans->count == REGEXP_ALL_COUNTER) {
1047 fprintf(output, "all transition, ");
1048 } else if (trans->count >= 0) {
Daniel Veillard4255d502002-04-16 15:50:10 +00001049 fprintf(output, "count based %d, ", trans->count);
1050 }
1051 if (trans->atom == NULL) {
1052 fprintf(output, "epsilon to %d\n", trans->to);
1053 return;
1054 }
1055 if (trans->atom->type == XML_REGEXP_CHARVAL)
1056 fprintf(output, "char %c ", trans->atom->codepoint);
1057 fprintf(output, "atom %d, to %d\n", trans->atom->no, trans->to);
1058}
1059
1060static void
1061xmlRegPrintState(FILE *output, xmlRegStatePtr state) {
1062 int i;
1063
1064 fprintf(output, " state: ");
1065 if (state == NULL) {
1066 fprintf(output, "NULL\n");
1067 return;
1068 }
1069 if (state->type == XML_REGEXP_START_STATE)
1070 fprintf(output, "START ");
1071 if (state->type == XML_REGEXP_FINAL_STATE)
1072 fprintf(output, "FINAL ");
1073
1074 fprintf(output, "%d, %d transitions:\n", state->no, state->nbTrans);
1075 for (i = 0;i < state->nbTrans; i++) {
1076 xmlRegPrintTrans(output, &(state->trans[i]));
1077 }
1078}
1079
Daniel Veillard23e73572002-09-19 19:56:43 +00001080#ifdef DEBUG_REGEXP_GRAPH
Daniel Veillard4255d502002-04-16 15:50:10 +00001081static void
1082xmlRegPrintCtxt(FILE *output, xmlRegParserCtxtPtr ctxt) {
1083 int i;
1084
1085 fprintf(output, " ctxt: ");
1086 if (ctxt == NULL) {
1087 fprintf(output, "NULL\n");
1088 return;
1089 }
1090 fprintf(output, "'%s' ", ctxt->string);
1091 if (ctxt->error)
1092 fprintf(output, "error ");
1093 if (ctxt->neg)
1094 fprintf(output, "neg ");
1095 fprintf(output, "\n");
1096 fprintf(output, "%d atoms:\n", ctxt->nbAtoms);
1097 for (i = 0;i < ctxt->nbAtoms; i++) {
1098 fprintf(output, " %02d ", i);
1099 xmlRegPrintAtom(output, ctxt->atoms[i]);
1100 }
1101 if (ctxt->atom != NULL) {
1102 fprintf(output, "current atom:\n");
1103 xmlRegPrintAtom(output, ctxt->atom);
1104 }
1105 fprintf(output, "%d states:", ctxt->nbStates);
1106 if (ctxt->start != NULL)
1107 fprintf(output, " start: %d", ctxt->start->no);
1108 if (ctxt->end != NULL)
1109 fprintf(output, " end: %d", ctxt->end->no);
1110 fprintf(output, "\n");
1111 for (i = 0;i < ctxt->nbStates; i++) {
1112 xmlRegPrintState(output, ctxt->states[i]);
1113 }
1114 fprintf(output, "%d counters:\n", ctxt->nbCounters);
1115 for (i = 0;i < ctxt->nbCounters; i++) {
1116 fprintf(output, " %d: min %d max %d\n", i, ctxt->counters[i].min,
1117 ctxt->counters[i].max);
1118 }
1119}
Daniel Veillard23e73572002-09-19 19:56:43 +00001120#endif
Daniel Veillard4255d502002-04-16 15:50:10 +00001121
1122/************************************************************************
1123 * *
1124 * Finite Automata structures manipulations *
1125 * *
1126 ************************************************************************/
1127
1128static void
1129xmlRegAtomAddRange(xmlRegParserCtxtPtr ctxt, xmlRegAtomPtr atom,
1130 int neg, xmlRegAtomType type, int start, int end,
1131 xmlChar *blockName) {
1132 xmlRegRangePtr range;
1133
1134 if (atom == NULL) {
1135 ERROR("add range: atom is NULL");
1136 return;
1137 }
1138 if (atom->type != XML_REGEXP_RANGES) {
1139 ERROR("add range: atom is not ranges");
1140 return;
1141 }
1142 if (atom->maxRanges == 0) {
1143 atom->maxRanges = 4;
1144 atom->ranges = (xmlRegRangePtr *) xmlMalloc(atom->maxRanges *
1145 sizeof(xmlRegRangePtr));
1146 if (atom->ranges == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00001147 xmlRegexpErrMemory(ctxt, "adding ranges");
Daniel Veillard4255d502002-04-16 15:50:10 +00001148 atom->maxRanges = 0;
1149 return;
1150 }
1151 } else if (atom->nbRanges >= atom->maxRanges) {
1152 xmlRegRangePtr *tmp;
1153 atom->maxRanges *= 2;
1154 tmp = (xmlRegRangePtr *) xmlRealloc(atom->ranges, atom->maxRanges *
1155 sizeof(xmlRegRangePtr));
1156 if (tmp == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00001157 xmlRegexpErrMemory(ctxt, "adding ranges");
Daniel Veillard4255d502002-04-16 15:50:10 +00001158 atom->maxRanges /= 2;
1159 return;
1160 }
1161 atom->ranges = tmp;
1162 }
1163 range = xmlRegNewRange(ctxt, neg, type, start, end);
1164 if (range == NULL)
1165 return;
1166 range->blockName = blockName;
1167 atom->ranges[atom->nbRanges++] = range;
1168
1169}
1170
1171static int
1172xmlRegGetCounter(xmlRegParserCtxtPtr ctxt) {
1173 if (ctxt->maxCounters == 0) {
1174 ctxt->maxCounters = 4;
1175 ctxt->counters = (xmlRegCounter *) xmlMalloc(ctxt->maxCounters *
1176 sizeof(xmlRegCounter));
1177 if (ctxt->counters == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00001178 xmlRegexpErrMemory(ctxt, "allocating counter");
Daniel Veillard4255d502002-04-16 15:50:10 +00001179 ctxt->maxCounters = 0;
1180 return(-1);
1181 }
1182 } else if (ctxt->nbCounters >= ctxt->maxCounters) {
1183 xmlRegCounter *tmp;
1184 ctxt->maxCounters *= 2;
1185 tmp = (xmlRegCounter *) xmlRealloc(ctxt->counters, ctxt->maxCounters *
1186 sizeof(xmlRegCounter));
1187 if (tmp == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00001188 xmlRegexpErrMemory(ctxt, "allocating counter");
Daniel Veillard4255d502002-04-16 15:50:10 +00001189 ctxt->maxCounters /= 2;
1190 return(-1);
1191 }
1192 ctxt->counters = tmp;
1193 }
1194 ctxt->counters[ctxt->nbCounters].min = -1;
1195 ctxt->counters[ctxt->nbCounters].max = -1;
1196 return(ctxt->nbCounters++);
1197}
1198
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001199static int
Daniel Veillard4255d502002-04-16 15:50:10 +00001200xmlRegAtomPush(xmlRegParserCtxtPtr ctxt, xmlRegAtomPtr atom) {
1201 if (atom == NULL) {
1202 ERROR("atom push: atom is NULL");
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001203 return(-1);
Daniel Veillard4255d502002-04-16 15:50:10 +00001204 }
1205 if (ctxt->maxAtoms == 0) {
1206 ctxt->maxAtoms = 4;
1207 ctxt->atoms = (xmlRegAtomPtr *) xmlMalloc(ctxt->maxAtoms *
1208 sizeof(xmlRegAtomPtr));
1209 if (ctxt->atoms == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00001210 xmlRegexpErrMemory(ctxt, "pushing atom");
Daniel Veillard4255d502002-04-16 15:50:10 +00001211 ctxt->maxAtoms = 0;
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001212 return(-1);
Daniel Veillard4255d502002-04-16 15:50:10 +00001213 }
1214 } else if (ctxt->nbAtoms >= ctxt->maxAtoms) {
1215 xmlRegAtomPtr *tmp;
1216 ctxt->maxAtoms *= 2;
1217 tmp = (xmlRegAtomPtr *) xmlRealloc(ctxt->atoms, ctxt->maxAtoms *
1218 sizeof(xmlRegAtomPtr));
1219 if (tmp == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00001220 xmlRegexpErrMemory(ctxt, "allocating counter");
Daniel Veillard4255d502002-04-16 15:50:10 +00001221 ctxt->maxAtoms /= 2;
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001222 return(-1);
Daniel Veillard4255d502002-04-16 15:50:10 +00001223 }
1224 ctxt->atoms = tmp;
1225 }
1226 atom->no = ctxt->nbAtoms;
1227 ctxt->atoms[ctxt->nbAtoms++] = atom;
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001228 return(0);
Daniel Veillard4255d502002-04-16 15:50:10 +00001229}
1230
1231static void
Daniel Veillarddb68b742005-07-30 13:18:24 +00001232xmlRegStateAddTransTo(xmlRegParserCtxtPtr ctxt, xmlRegStatePtr target,
1233 int from) {
1234 if (target->maxTransTo == 0) {
1235 target->maxTransTo = 8;
1236 target->transTo = (int *) xmlMalloc(target->maxTransTo *
1237 sizeof(int));
1238 if (target->transTo == NULL) {
1239 xmlRegexpErrMemory(ctxt, "adding transition");
1240 target->maxTransTo = 0;
1241 return;
1242 }
1243 } else if (target->nbTransTo >= target->maxTransTo) {
1244 int *tmp;
1245 target->maxTransTo *= 2;
1246 tmp = (int *) xmlRealloc(target->transTo, target->maxTransTo *
1247 sizeof(int));
1248 if (tmp == NULL) {
1249 xmlRegexpErrMemory(ctxt, "adding transition");
1250 target->maxTransTo /= 2;
1251 return;
1252 }
1253 target->transTo = tmp;
1254 }
1255 target->transTo[target->nbTransTo] = from;
1256 target->nbTransTo++;
1257}
1258
1259static void
Daniel Veillard4255d502002-04-16 15:50:10 +00001260xmlRegStateAddTrans(xmlRegParserCtxtPtr ctxt, xmlRegStatePtr state,
1261 xmlRegAtomPtr atom, xmlRegStatePtr target,
Daniel Veillard5de09382005-09-26 17:18:17 +00001262 int counter, int count) {
William M. Brackf9b5fa22004-05-10 07:52:15 +00001263
1264 int nrtrans;
1265
Daniel Veillard4255d502002-04-16 15:50:10 +00001266 if (state == NULL) {
1267 ERROR("add state: state is NULL");
1268 return;
1269 }
1270 if (target == NULL) {
1271 ERROR("add state: target is NULL");
1272 return;
1273 }
William M. Brackf9b5fa22004-05-10 07:52:15 +00001274 /*
1275 * Other routines follow the philosophy 'When in doubt, add a transition'
1276 * so we check here whether such a transition is already present and, if
1277 * so, silently ignore this request.
1278 */
1279
Daniel Veillard5de09382005-09-26 17:18:17 +00001280 for (nrtrans = state->nbTrans - 1; nrtrans >= 0; nrtrans--) {
1281 xmlRegTransPtr trans = &(state->trans[nrtrans]);
1282 if ((trans->atom == atom) &&
1283 (trans->to == target->no) &&
1284 (trans->counter == counter) &&
1285 (trans->count == count)) {
William M. Brackf9b5fa22004-05-10 07:52:15 +00001286#ifdef DEBUG_REGEXP_GRAPH
Daniel Veillard5de09382005-09-26 17:18:17 +00001287 printf("Ignoring duplicate transition from %d to %d\n",
1288 state->no, target->no);
William M. Brackf9b5fa22004-05-10 07:52:15 +00001289#endif
Daniel Veillard5de09382005-09-26 17:18:17 +00001290 return;
Daniel Veillarddb68b742005-07-30 13:18:24 +00001291 }
William M. Brackf9b5fa22004-05-10 07:52:15 +00001292 }
1293
Daniel Veillard4255d502002-04-16 15:50:10 +00001294 if (state->maxTrans == 0) {
Daniel Veillarddb68b742005-07-30 13:18:24 +00001295 state->maxTrans = 8;
Daniel Veillard4255d502002-04-16 15:50:10 +00001296 state->trans = (xmlRegTrans *) xmlMalloc(state->maxTrans *
1297 sizeof(xmlRegTrans));
1298 if (state->trans == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00001299 xmlRegexpErrMemory(ctxt, "adding transition");
Daniel Veillard4255d502002-04-16 15:50:10 +00001300 state->maxTrans = 0;
1301 return;
1302 }
1303 } else if (state->nbTrans >= state->maxTrans) {
1304 xmlRegTrans *tmp;
1305 state->maxTrans *= 2;
1306 tmp = (xmlRegTrans *) xmlRealloc(state->trans, state->maxTrans *
1307 sizeof(xmlRegTrans));
1308 if (tmp == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00001309 xmlRegexpErrMemory(ctxt, "adding transition");
Daniel Veillard4255d502002-04-16 15:50:10 +00001310 state->maxTrans /= 2;
1311 return;
1312 }
1313 state->trans = tmp;
1314 }
1315#ifdef DEBUG_REGEXP_GRAPH
1316 printf("Add trans from %d to %d ", state->no, target->no);
Daniel Veillard8a001f62002-04-20 07:24:11 +00001317 if (count == REGEXP_ALL_COUNTER)
Daniel Veillard2cbf5962004-03-31 15:50:43 +00001318 printf("all transition\n");
Daniel Veillard4402ab42002-09-12 16:02:56 +00001319 else if (count >= 0)
Daniel Veillard2cbf5962004-03-31 15:50:43 +00001320 printf("count based %d\n", count);
Daniel Veillard4255d502002-04-16 15:50:10 +00001321 else if (counter >= 0)
Daniel Veillard2cbf5962004-03-31 15:50:43 +00001322 printf("counted %d\n", counter);
Daniel Veillard4255d502002-04-16 15:50:10 +00001323 else if (atom == NULL)
Daniel Veillard2cbf5962004-03-31 15:50:43 +00001324 printf("epsilon transition\n");
1325 else if (atom != NULL)
1326 xmlRegPrintAtom(stdout, atom);
Daniel Veillard4255d502002-04-16 15:50:10 +00001327#endif
1328
1329 state->trans[state->nbTrans].atom = atom;
1330 state->trans[state->nbTrans].to = target->no;
1331 state->trans[state->nbTrans].counter = counter;
1332 state->trans[state->nbTrans].count = count;
Daniel Veillard567a45b2005-10-18 19:11:55 +00001333 state->trans[state->nbTrans].nd = 0;
Daniel Veillard4255d502002-04-16 15:50:10 +00001334 state->nbTrans++;
Daniel Veillarddb68b742005-07-30 13:18:24 +00001335 xmlRegStateAddTransTo(ctxt, target, state->no);
Daniel Veillard4255d502002-04-16 15:50:10 +00001336}
1337
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001338static int
Daniel Veillard4255d502002-04-16 15:50:10 +00001339xmlRegStatePush(xmlRegParserCtxtPtr ctxt, xmlRegStatePtr state) {
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001340 if (state == NULL) return(-1);
Daniel Veillard4255d502002-04-16 15:50:10 +00001341 if (ctxt->maxStates == 0) {
1342 ctxt->maxStates = 4;
1343 ctxt->states = (xmlRegStatePtr *) xmlMalloc(ctxt->maxStates *
1344 sizeof(xmlRegStatePtr));
1345 if (ctxt->states == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00001346 xmlRegexpErrMemory(ctxt, "adding state");
Daniel Veillard4255d502002-04-16 15:50:10 +00001347 ctxt->maxStates = 0;
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001348 return(-1);
Daniel Veillard4255d502002-04-16 15:50:10 +00001349 }
1350 } else if (ctxt->nbStates >= ctxt->maxStates) {
1351 xmlRegStatePtr *tmp;
1352 ctxt->maxStates *= 2;
1353 tmp = (xmlRegStatePtr *) xmlRealloc(ctxt->states, ctxt->maxStates *
1354 sizeof(xmlRegStatePtr));
1355 if (tmp == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00001356 xmlRegexpErrMemory(ctxt, "adding state");
Daniel Veillard4255d502002-04-16 15:50:10 +00001357 ctxt->maxStates /= 2;
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001358 return(-1);
Daniel Veillard4255d502002-04-16 15:50:10 +00001359 }
1360 ctxt->states = tmp;
1361 }
1362 state->no = ctxt->nbStates;
1363 ctxt->states[ctxt->nbStates++] = state;
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001364 return(0);
Daniel Veillard4255d502002-04-16 15:50:10 +00001365}
1366
1367/**
Daniel Veillard7646b182002-04-20 06:41:40 +00001368 * xmlFAGenerateAllTransition:
Daniel Veillard441bc322002-04-20 17:38:48 +00001369 * @ctxt: a regexp parser context
1370 * @from: the from state
1371 * @to: the target state or NULL for building a new one
1372 * @lax:
Daniel Veillard7646b182002-04-20 06:41:40 +00001373 *
1374 */
1375static void
1376xmlFAGenerateAllTransition(xmlRegParserCtxtPtr ctxt,
Daniel Veillard441bc322002-04-20 17:38:48 +00001377 xmlRegStatePtr from, xmlRegStatePtr to,
1378 int lax) {
Daniel Veillard7646b182002-04-20 06:41:40 +00001379 if (to == NULL) {
1380 to = xmlRegNewState(ctxt);
1381 xmlRegStatePush(ctxt, to);
1382 ctxt->state = to;
1383 }
Daniel Veillard441bc322002-04-20 17:38:48 +00001384 if (lax)
Daniel Veillard5de09382005-09-26 17:18:17 +00001385 xmlRegStateAddTrans(ctxt, from, NULL, to, -1, REGEXP_ALL_LAX_COUNTER);
Daniel Veillard441bc322002-04-20 17:38:48 +00001386 else
Daniel Veillard5de09382005-09-26 17:18:17 +00001387 xmlRegStateAddTrans(ctxt, from, NULL, to, -1, REGEXP_ALL_COUNTER);
Daniel Veillard7646b182002-04-20 06:41:40 +00001388}
1389
1390/**
Daniel Veillard4255d502002-04-16 15:50:10 +00001391 * xmlFAGenerateEpsilonTransition:
Daniel Veillard441bc322002-04-20 17:38:48 +00001392 * @ctxt: a regexp parser context
1393 * @from: the from state
1394 * @to: the target state or NULL for building a new one
Daniel Veillard4255d502002-04-16 15:50:10 +00001395 *
1396 */
1397static void
1398xmlFAGenerateEpsilonTransition(xmlRegParserCtxtPtr ctxt,
1399 xmlRegStatePtr from, xmlRegStatePtr to) {
1400 if (to == NULL) {
1401 to = xmlRegNewState(ctxt);
1402 xmlRegStatePush(ctxt, to);
1403 ctxt->state = to;
1404 }
Daniel Veillard5de09382005-09-26 17:18:17 +00001405 xmlRegStateAddTrans(ctxt, from, NULL, to, -1, -1);
Daniel Veillard4255d502002-04-16 15:50:10 +00001406}
1407
1408/**
1409 * xmlFAGenerateCountedEpsilonTransition:
Daniel Veillard441bc322002-04-20 17:38:48 +00001410 * @ctxt: a regexp parser context
1411 * @from: the from state
1412 * @to: the target state or NULL for building a new one
Daniel Veillard4255d502002-04-16 15:50:10 +00001413 * counter: the counter for that transition
1414 *
1415 */
1416static void
1417xmlFAGenerateCountedEpsilonTransition(xmlRegParserCtxtPtr ctxt,
1418 xmlRegStatePtr from, xmlRegStatePtr to, int counter) {
1419 if (to == NULL) {
1420 to = xmlRegNewState(ctxt);
1421 xmlRegStatePush(ctxt, to);
1422 ctxt->state = to;
1423 }
Daniel Veillard5de09382005-09-26 17:18:17 +00001424 xmlRegStateAddTrans(ctxt, from, NULL, to, counter, -1);
Daniel Veillard4255d502002-04-16 15:50:10 +00001425}
1426
1427/**
1428 * xmlFAGenerateCountedTransition:
Daniel Veillard441bc322002-04-20 17:38:48 +00001429 * @ctxt: a regexp parser context
1430 * @from: the from state
1431 * @to: the target state or NULL for building a new one
Daniel Veillard4255d502002-04-16 15:50:10 +00001432 * counter: the counter for that transition
1433 *
1434 */
1435static void
1436xmlFAGenerateCountedTransition(xmlRegParserCtxtPtr ctxt,
1437 xmlRegStatePtr from, xmlRegStatePtr to, int counter) {
1438 if (to == NULL) {
1439 to = xmlRegNewState(ctxt);
1440 xmlRegStatePush(ctxt, to);
1441 ctxt->state = to;
1442 }
Daniel Veillard5de09382005-09-26 17:18:17 +00001443 xmlRegStateAddTrans(ctxt, from, NULL, to, -1, counter);
Daniel Veillard4255d502002-04-16 15:50:10 +00001444}
1445
1446/**
1447 * xmlFAGenerateTransitions:
Daniel Veillard441bc322002-04-20 17:38:48 +00001448 * @ctxt: a regexp parser context
1449 * @from: the from state
1450 * @to: the target state or NULL for building a new one
1451 * @atom: the atom generating the transition
Daniel Veillard4255d502002-04-16 15:50:10 +00001452 *
William M. Brackddf71d62004-05-06 04:17:26 +00001453 * Returns 0 if success and -1 in case of error.
Daniel Veillard4255d502002-04-16 15:50:10 +00001454 */
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001455static int
Daniel Veillard4255d502002-04-16 15:50:10 +00001456xmlFAGenerateTransitions(xmlRegParserCtxtPtr ctxt, xmlRegStatePtr from,
1457 xmlRegStatePtr to, xmlRegAtomPtr atom) {
1458 if (atom == NULL) {
1459 ERROR("genrate transition: atom == NULL");
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001460 return(-1);
Daniel Veillard4255d502002-04-16 15:50:10 +00001461 }
1462 if (atom->type == XML_REGEXP_SUBREG) {
1463 /*
1464 * this is a subexpression handling one should not need to
William M. Brackddf71d62004-05-06 04:17:26 +00001465 * create a new node except for XML_REGEXP_QUANT_RANGE.
Daniel Veillard4255d502002-04-16 15:50:10 +00001466 */
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001467 if (xmlRegAtomPush(ctxt, atom) < 0) {
1468 return(-1);
1469 }
Daniel Veillard4255d502002-04-16 15:50:10 +00001470 if ((to != NULL) && (atom->stop != to) &&
1471 (atom->quant != XML_REGEXP_QUANT_RANGE)) {
1472 /*
1473 * Generate an epsilon transition to link to the target
1474 */
1475 xmlFAGenerateEpsilonTransition(ctxt, atom->stop, to);
Daniel Veillardaa622012005-10-20 15:55:25 +00001476#ifdef DV
1477 } else if ((to == NULL) && (atom->quant != XML_REGEXP_QUANT_RANGE) &&
1478 (atom->quant != XML_REGEXP_QUANT_ONCE)) {
1479 to = xmlRegNewState(ctxt);
1480 xmlRegStatePush(ctxt, to);
1481 ctxt->state = to;
1482 xmlFAGenerateEpsilonTransition(ctxt, atom->stop, to);
1483#endif
Daniel Veillard4255d502002-04-16 15:50:10 +00001484 }
1485 switch (atom->quant) {
1486 case XML_REGEXP_QUANT_OPT:
1487 atom->quant = XML_REGEXP_QUANT_ONCE;
Daniel Veillard54eb0242006-03-21 23:17:57 +00001488 /*
1489 * transition done to the state after end of atom.
1490 * 1. set transition from atom start to new state
1491 * 2. set transition from atom end to this state.
1492 */
1493 xmlFAGenerateEpsilonTransition(ctxt, atom->start, 0);
1494 xmlFAGenerateEpsilonTransition(ctxt, atom->stop, ctxt->state);
Daniel Veillard4255d502002-04-16 15:50:10 +00001495 break;
1496 case XML_REGEXP_QUANT_MULT:
1497 atom->quant = XML_REGEXP_QUANT_ONCE;
1498 xmlFAGenerateEpsilonTransition(ctxt, atom->start, atom->stop);
1499 xmlFAGenerateEpsilonTransition(ctxt, atom->stop, atom->start);
1500 break;
1501 case XML_REGEXP_QUANT_PLUS:
1502 atom->quant = XML_REGEXP_QUANT_ONCE;
1503 xmlFAGenerateEpsilonTransition(ctxt, atom->stop, atom->start);
1504 break;
1505 case XML_REGEXP_QUANT_RANGE: {
1506 int counter;
1507 xmlRegStatePtr newstate;
1508
1509 /*
1510 * This one is nasty:
William M. Brackddf71d62004-05-06 04:17:26 +00001511 * 1/ if range has minOccurs == 0, create a new state
1512 * and create epsilon transitions from atom->start
1513 * to atom->stop, as well as atom->start to the new
1514 * state
1515 * 2/ register a new counter
1516 * 3/ register an epsilon transition associated to
Daniel Veillard4255d502002-04-16 15:50:10 +00001517 * this counter going from atom->stop to atom->start
William M. Brackddf71d62004-05-06 04:17:26 +00001518 * 4/ create a new state
1519 * 5/ generate a counted transition from atom->stop to
Daniel Veillard4255d502002-04-16 15:50:10 +00001520 * that state
1521 */
William M. Brackddf71d62004-05-06 04:17:26 +00001522 if (atom->min == 0) {
1523 xmlFAGenerateEpsilonTransition(ctxt, atom->start,
1524 atom->stop);
1525 newstate = xmlRegNewState(ctxt);
1526 xmlRegStatePush(ctxt, newstate);
1527 ctxt->state = newstate;
1528 xmlFAGenerateEpsilonTransition(ctxt, atom->start,
1529 newstate);
1530 }
Daniel Veillard4255d502002-04-16 15:50:10 +00001531 counter = xmlRegGetCounter(ctxt);
1532 ctxt->counters[counter].min = atom->min - 1;
1533 ctxt->counters[counter].max = atom->max - 1;
1534 atom->min = 0;
1535 atom->max = 0;
1536 atom->quant = XML_REGEXP_QUANT_ONCE;
Daniel Veillard4255d502002-04-16 15:50:10 +00001537 if (to != NULL) {
1538 newstate = to;
1539 } else {
1540 newstate = xmlRegNewState(ctxt);
1541 xmlRegStatePush(ctxt, newstate);
Daniel Veillard4255d502002-04-16 15:50:10 +00001542 }
Daniel Veillard9a00fd22005-11-09 08:56:26 +00001543 ctxt->state = newstate;
Daniel Veillard4255d502002-04-16 15:50:10 +00001544 xmlFAGenerateCountedTransition(ctxt, atom->stop,
1545 newstate, counter);
Daniel Veillard54eb0242006-03-21 23:17:57 +00001546
1547 /*
1548 * first check count and if OK jump forward;
1549 * if checking fail increment count and jump back
1550 */
1551 xmlFAGenerateCountedEpsilonTransition(ctxt, atom->stop,
1552 atom->start, counter);
Daniel Veillard4255d502002-04-16 15:50:10 +00001553 }
1554 default:
1555 break;
1556 }
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001557 return(0);
Daniel Veillard567a45b2005-10-18 19:11:55 +00001558 }
1559 if ((atom->min == 0) && (atom->max == 0) &&
Daniel Veillard99c394d2005-07-14 12:58:49 +00001560 (atom->quant == XML_REGEXP_QUANT_RANGE)) {
1561 /*
1562 * we can discard the atom and generate an epsilon transition instead
1563 */
1564 if (to == NULL) {
1565 to = xmlRegNewState(ctxt);
1566 if (to != NULL)
1567 xmlRegStatePush(ctxt, to);
1568 else {
1569 return(-1);
1570 }
1571 }
1572 xmlFAGenerateEpsilonTransition(ctxt, from, to);
1573 ctxt->state = to;
1574 xmlRegFreeAtom(atom);
1575 return(0);
Daniel Veillard567a45b2005-10-18 19:11:55 +00001576 }
1577 if (to == NULL) {
1578 to = xmlRegNewState(ctxt);
1579 if (to != NULL)
1580 xmlRegStatePush(ctxt, to);
1581 else {
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001582 return(-1);
Daniel Veillard4255d502002-04-16 15:50:10 +00001583 }
Daniel Veillard4255d502002-04-16 15:50:10 +00001584 }
Daniel Veillard567a45b2005-10-18 19:11:55 +00001585 if (xmlRegAtomPush(ctxt, atom) < 0) {
1586 return(-1);
1587 }
1588 xmlRegStateAddTrans(ctxt, from, atom, to, -1, -1);
1589 ctxt->state = to;
Daniel Veillard4255d502002-04-16 15:50:10 +00001590 switch (atom->quant) {
1591 case XML_REGEXP_QUANT_OPT:
1592 atom->quant = XML_REGEXP_QUANT_ONCE;
1593 xmlFAGenerateEpsilonTransition(ctxt, from, to);
1594 break;
1595 case XML_REGEXP_QUANT_MULT:
1596 atom->quant = XML_REGEXP_QUANT_ONCE;
1597 xmlFAGenerateEpsilonTransition(ctxt, from, to);
Daniel Veillard5de09382005-09-26 17:18:17 +00001598 xmlRegStateAddTrans(ctxt, to, atom, to, -1, -1);
Daniel Veillard4255d502002-04-16 15:50:10 +00001599 break;
1600 case XML_REGEXP_QUANT_PLUS:
1601 atom->quant = XML_REGEXP_QUANT_ONCE;
Daniel Veillard5de09382005-09-26 17:18:17 +00001602 xmlRegStateAddTrans(ctxt, to, atom, to, -1, -1);
Daniel Veillard4255d502002-04-16 15:50:10 +00001603 break;
1604 default:
1605 break;
1606 }
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001607 return(0);
Daniel Veillard4255d502002-04-16 15:50:10 +00001608}
1609
1610/**
1611 * xmlFAReduceEpsilonTransitions:
Daniel Veillard441bc322002-04-20 17:38:48 +00001612 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00001613 * @fromnr: the from state
1614 * @tonr: the to state
William M. Brackddf71d62004-05-06 04:17:26 +00001615 * @counter: should that transition be associated to a counted
Daniel Veillard4255d502002-04-16 15:50:10 +00001616 *
1617 */
1618static void
1619xmlFAReduceEpsilonTransitions(xmlRegParserCtxtPtr ctxt, int fromnr,
1620 int tonr, int counter) {
1621 int transnr;
1622 xmlRegStatePtr from;
1623 xmlRegStatePtr to;
1624
1625#ifdef DEBUG_REGEXP_GRAPH
1626 printf("xmlFAReduceEpsilonTransitions(%d, %d)\n", fromnr, tonr);
1627#endif
1628 from = ctxt->states[fromnr];
1629 if (from == NULL)
1630 return;
1631 to = ctxt->states[tonr];
1632 if (to == NULL)
1633 return;
1634 if ((to->mark == XML_REGEXP_MARK_START) ||
1635 (to->mark == XML_REGEXP_MARK_VISITED))
1636 return;
1637
1638 to->mark = XML_REGEXP_MARK_VISITED;
1639 if (to->type == XML_REGEXP_FINAL_STATE) {
1640#ifdef DEBUG_REGEXP_GRAPH
1641 printf("State %d is final, so %d becomes final\n", tonr, fromnr);
1642#endif
1643 from->type = XML_REGEXP_FINAL_STATE;
1644 }
1645 for (transnr = 0;transnr < to->nbTrans;transnr++) {
Daniel Veillarddb68b742005-07-30 13:18:24 +00001646 if (to->trans[transnr].to < 0)
1647 continue;
Daniel Veillard4255d502002-04-16 15:50:10 +00001648 if (to->trans[transnr].atom == NULL) {
1649 /*
1650 * Don't remove counted transitions
1651 * Don't loop either
1652 */
Daniel Veillardb509f152002-04-17 16:28:10 +00001653 if (to->trans[transnr].to != fromnr) {
1654 if (to->trans[transnr].count >= 0) {
1655 int newto = to->trans[transnr].to;
1656
1657 xmlRegStateAddTrans(ctxt, from, NULL,
1658 ctxt->states[newto],
Daniel Veillard5de09382005-09-26 17:18:17 +00001659 -1, to->trans[transnr].count);
Daniel Veillardb509f152002-04-17 16:28:10 +00001660 } else {
Daniel Veillard4255d502002-04-16 15:50:10 +00001661#ifdef DEBUG_REGEXP_GRAPH
Daniel Veillardb509f152002-04-17 16:28:10 +00001662 printf("Found epsilon trans %d from %d to %d\n",
1663 transnr, tonr, to->trans[transnr].to);
Daniel Veillard4255d502002-04-16 15:50:10 +00001664#endif
Daniel Veillardb509f152002-04-17 16:28:10 +00001665 if (to->trans[transnr].counter >= 0) {
1666 xmlFAReduceEpsilonTransitions(ctxt, fromnr,
1667 to->trans[transnr].to,
1668 to->trans[transnr].counter);
1669 } else {
1670 xmlFAReduceEpsilonTransitions(ctxt, fromnr,
1671 to->trans[transnr].to,
1672 counter);
1673 }
1674 }
Daniel Veillard4255d502002-04-16 15:50:10 +00001675 }
1676 } else {
1677 int newto = to->trans[transnr].to;
1678
Daniel Veillardb509f152002-04-17 16:28:10 +00001679 if (to->trans[transnr].counter >= 0) {
1680 xmlRegStateAddTrans(ctxt, from, to->trans[transnr].atom,
1681 ctxt->states[newto],
Daniel Veillard5de09382005-09-26 17:18:17 +00001682 to->trans[transnr].counter, -1);
Daniel Veillardb509f152002-04-17 16:28:10 +00001683 } else {
1684 xmlRegStateAddTrans(ctxt, from, to->trans[transnr].atom,
Daniel Veillard5de09382005-09-26 17:18:17 +00001685 ctxt->states[newto], counter, -1);
Daniel Veillardb509f152002-04-17 16:28:10 +00001686 }
Daniel Veillard4255d502002-04-16 15:50:10 +00001687 }
1688 }
1689 to->mark = XML_REGEXP_MARK_NORMAL;
1690}
1691
1692/**
Daniel Veillarddb68b742005-07-30 13:18:24 +00001693 * xmlFAEliminateSimpleEpsilonTransitions:
1694 * @ctxt: a regexp parser context
1695 *
1696 * Eliminating general epsilon transitions can get costly in the general
1697 * algorithm due to the large amount of generated new transitions and
1698 * associated comparisons. However for simple epsilon transition used just
1699 * to separate building blocks when generating the automata this can be
1700 * reduced to state elimination:
1701 * - if there exists an epsilon from X to Y
1702 * - if there is no other transition from X
1703 * then X and Y are semantically equivalent and X can be eliminated
1704 * If X is the start state then make Y the start state, else replace the
1705 * target of all transitions to X by transitions to Y.
1706 */
1707static void
1708xmlFAEliminateSimpleEpsilonTransitions(xmlRegParserCtxtPtr ctxt) {
1709 int statenr, i, j, newto;
1710 xmlRegStatePtr state, tmp;
1711
1712 for (statenr = 0;statenr < ctxt->nbStates;statenr++) {
1713 state = ctxt->states[statenr];
1714 if (state == NULL)
1715 continue;
1716 if (state->nbTrans != 1)
1717 continue;
Daniel Veillard0e05f4c2006-11-01 15:33:04 +00001718 if (state->type == XML_REGEXP_UNREACH_STATE)
1719 continue;
Daniel Veillarddb68b742005-07-30 13:18:24 +00001720 /* is the only transition out a basic transition */
1721 if ((state->trans[0].atom == NULL) &&
1722 (state->trans[0].to >= 0) &&
1723 (state->trans[0].to != statenr) &&
1724 (state->trans[0].counter < 0) &&
1725 (state->trans[0].count < 0)) {
1726 newto = state->trans[0].to;
1727
1728 if (state->type == XML_REGEXP_START_STATE) {
1729#ifdef DEBUG_REGEXP_GRAPH
1730 printf("Found simple epsilon trans from start %d to %d\n",
1731 statenr, newto);
1732#endif
1733 } else {
1734#ifdef DEBUG_REGEXP_GRAPH
1735 printf("Found simple epsilon trans from %d to %d\n",
1736 statenr, newto);
1737#endif
1738 for (i = 0;i < state->nbTransTo;i++) {
1739 tmp = ctxt->states[state->transTo[i]];
1740 for (j = 0;j < tmp->nbTrans;j++) {
1741 if (tmp->trans[j].to == statenr) {
Daniel Veillarddb68b742005-07-30 13:18:24 +00001742#ifdef DEBUG_REGEXP_GRAPH
1743 printf("Changed transition %d on %d to go to %d\n",
1744 j, tmp->no, newto);
1745#endif
Daniel Veillard0e05f4c2006-11-01 15:33:04 +00001746 tmp->trans[j].to = -1;
1747 xmlRegStateAddTrans(ctxt, tmp, tmp->trans[j].atom,
1748 ctxt->states[newto],
1749 tmp->trans[j].counter,
1750 tmp->trans[j].count);
Daniel Veillarddb68b742005-07-30 13:18:24 +00001751 }
1752 }
1753 }
Daniel Veillarddb68b742005-07-30 13:18:24 +00001754 if (state->type == XML_REGEXP_FINAL_STATE)
1755 ctxt->states[newto]->type = XML_REGEXP_FINAL_STATE;
1756 /* eliminate the transition completely */
1757 state->nbTrans = 0;
1758
Daniel Veillard0e05f4c2006-11-01 15:33:04 +00001759 state->type = XML_REGEXP_UNREACH_STATE;
Daniel Veillarddb68b742005-07-30 13:18:24 +00001760
1761 }
1762
1763 }
1764 }
1765}
1766/**
Daniel Veillard4255d502002-04-16 15:50:10 +00001767 * xmlFAEliminateEpsilonTransitions:
Daniel Veillard441bc322002-04-20 17:38:48 +00001768 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00001769 *
1770 */
1771static void
1772xmlFAEliminateEpsilonTransitions(xmlRegParserCtxtPtr ctxt) {
1773 int statenr, transnr;
1774 xmlRegStatePtr state;
Daniel Veillarddb68b742005-07-30 13:18:24 +00001775 int has_epsilon;
Daniel Veillard4255d502002-04-16 15:50:10 +00001776
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001777 if (ctxt->states == NULL) return;
1778
Daniel Veillard0e05f4c2006-11-01 15:33:04 +00001779 /*
1780 * Eliminate simple epsilon transition and the associated unreachable
1781 * states.
1782 */
Daniel Veillarddb68b742005-07-30 13:18:24 +00001783 xmlFAEliminateSimpleEpsilonTransitions(ctxt);
Daniel Veillard0e05f4c2006-11-01 15:33:04 +00001784 for (statenr = 0;statenr < ctxt->nbStates;statenr++) {
1785 state = ctxt->states[statenr];
1786 if ((state != NULL) && (state->type == XML_REGEXP_UNREACH_STATE)) {
1787#ifdef DEBUG_REGEXP_GRAPH
1788 printf("Removed unreachable state %d\n", statenr);
1789#endif
1790 xmlRegFreeState(state);
1791 ctxt->states[statenr] = NULL;
1792 }
1793 }
Daniel Veillarddb68b742005-07-30 13:18:24 +00001794
1795 has_epsilon = 0;
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001796
Daniel Veillard4255d502002-04-16 15:50:10 +00001797 /*
Daniel Veillardfcd18ff2006-11-02 10:28:04 +00001798 * Build the completed transitions bypassing the epsilons
Daniel Veillard4255d502002-04-16 15:50:10 +00001799 * Use a marking algorithm to avoid loops
Daniel Veillardfcd18ff2006-11-02 10:28:04 +00001800 * Mark sink states too.
1801 * Process from the latests states backward to the start when
1802 * there is long cascading epsilon chains this minimize the
1803 * recursions and transition compares when adding the new ones
Daniel Veillard4255d502002-04-16 15:50:10 +00001804 */
Daniel Veillardfcd18ff2006-11-02 10:28:04 +00001805 for (statenr = ctxt->nbStates - 1;statenr >= 0;statenr--) {
Daniel Veillard4255d502002-04-16 15:50:10 +00001806 state = ctxt->states[statenr];
1807 if (state == NULL)
1808 continue;
Daniel Veillardcc026dc2005-01-12 13:21:17 +00001809 if ((state->nbTrans == 0) &&
1810 (state->type != XML_REGEXP_FINAL_STATE)) {
1811 state->type = XML_REGEXP_SINK_STATE;
1812 }
Daniel Veillard4255d502002-04-16 15:50:10 +00001813 for (transnr = 0;transnr < state->nbTrans;transnr++) {
1814 if ((state->trans[transnr].atom == NULL) &&
1815 (state->trans[transnr].to >= 0)) {
1816 if (state->trans[transnr].to == statenr) {
1817 state->trans[transnr].to = -1;
1818#ifdef DEBUG_REGEXP_GRAPH
1819 printf("Removed loopback epsilon trans %d on %d\n",
1820 transnr, statenr);
1821#endif
1822 } else if (state->trans[transnr].count < 0) {
1823 int newto = state->trans[transnr].to;
1824
1825#ifdef DEBUG_REGEXP_GRAPH
1826 printf("Found epsilon trans %d from %d to %d\n",
1827 transnr, statenr, newto);
1828#endif
Daniel Veillarddb68b742005-07-30 13:18:24 +00001829 has_epsilon = 1;
Daniel Veillard0e05f4c2006-11-01 15:33:04 +00001830 state->trans[transnr].to = -2;
1831 state->mark = XML_REGEXP_MARK_START;
Daniel Veillard4255d502002-04-16 15:50:10 +00001832 xmlFAReduceEpsilonTransitions(ctxt, statenr,
1833 newto, state->trans[transnr].counter);
1834 state->mark = XML_REGEXP_MARK_NORMAL;
1835#ifdef DEBUG_REGEXP_GRAPH
1836 } else {
1837 printf("Found counted transition %d on %d\n",
1838 transnr, statenr);
1839#endif
1840 }
1841 }
1842 }
1843 }
1844 /*
1845 * Eliminate the epsilon transitions
1846 */
Daniel Veillarddb68b742005-07-30 13:18:24 +00001847 if (has_epsilon) {
1848 for (statenr = 0;statenr < ctxt->nbStates;statenr++) {
1849 state = ctxt->states[statenr];
1850 if (state == NULL)
1851 continue;
1852 for (transnr = 0;transnr < state->nbTrans;transnr++) {
1853 xmlRegTransPtr trans = &(state->trans[transnr]);
1854 if ((trans->atom == NULL) &&
1855 (trans->count < 0) &&
1856 (trans->to >= 0)) {
1857 trans->to = -1;
1858 }
Daniel Veillard4255d502002-04-16 15:50:10 +00001859 }
1860 }
1861 }
Daniel Veillard23e73572002-09-19 19:56:43 +00001862
1863 /*
1864 * Use this pass to detect unreachable states too
1865 */
1866 for (statenr = 0;statenr < ctxt->nbStates;statenr++) {
1867 state = ctxt->states[statenr];
1868 if (state != NULL)
William M. Brack779af002003-08-01 15:55:39 +00001869 state->reached = XML_REGEXP_MARK_NORMAL;
Daniel Veillard23e73572002-09-19 19:56:43 +00001870 }
1871 state = ctxt->states[0];
1872 if (state != NULL)
William M. Brack779af002003-08-01 15:55:39 +00001873 state->reached = XML_REGEXP_MARK_START;
Daniel Veillard23e73572002-09-19 19:56:43 +00001874 while (state != NULL) {
1875 xmlRegStatePtr target = NULL;
William M. Brack779af002003-08-01 15:55:39 +00001876 state->reached = XML_REGEXP_MARK_VISITED;
Daniel Veillard23e73572002-09-19 19:56:43 +00001877 /*
William M. Brackddf71d62004-05-06 04:17:26 +00001878 * Mark all states reachable from the current reachable state
Daniel Veillard23e73572002-09-19 19:56:43 +00001879 */
1880 for (transnr = 0;transnr < state->nbTrans;transnr++) {
1881 if ((state->trans[transnr].to >= 0) &&
1882 ((state->trans[transnr].atom != NULL) ||
1883 (state->trans[transnr].count >= 0))) {
1884 int newto = state->trans[transnr].to;
1885
1886 if (ctxt->states[newto] == NULL)
1887 continue;
William M. Brack779af002003-08-01 15:55:39 +00001888 if (ctxt->states[newto]->reached == XML_REGEXP_MARK_NORMAL) {
1889 ctxt->states[newto]->reached = XML_REGEXP_MARK_START;
Daniel Veillard23e73572002-09-19 19:56:43 +00001890 target = ctxt->states[newto];
1891 }
1892 }
1893 }
Daniel Veillardcc026dc2005-01-12 13:21:17 +00001894
Daniel Veillard23e73572002-09-19 19:56:43 +00001895 /*
1896 * find the next accessible state not explored
1897 */
1898 if (target == NULL) {
1899 for (statenr = 1;statenr < ctxt->nbStates;statenr++) {
1900 state = ctxt->states[statenr];
William M. Brack779af002003-08-01 15:55:39 +00001901 if ((state != NULL) && (state->reached ==
1902 XML_REGEXP_MARK_START)) {
Daniel Veillard23e73572002-09-19 19:56:43 +00001903 target = state;
1904 break;
1905 }
1906 }
1907 }
1908 state = target;
1909 }
1910 for (statenr = 0;statenr < ctxt->nbStates;statenr++) {
1911 state = ctxt->states[statenr];
William M. Brack779af002003-08-01 15:55:39 +00001912 if ((state != NULL) && (state->reached == XML_REGEXP_MARK_NORMAL)) {
Daniel Veillard23e73572002-09-19 19:56:43 +00001913#ifdef DEBUG_REGEXP_GRAPH
1914 printf("Removed unreachable state %d\n", statenr);
1915#endif
1916 xmlRegFreeState(state);
1917 ctxt->states[statenr] = NULL;
1918 }
1919 }
1920
Daniel Veillard4255d502002-04-16 15:50:10 +00001921}
1922
Daniel Veillard567a45b2005-10-18 19:11:55 +00001923static int
1924xmlFACompareRanges(xmlRegRangePtr range1, xmlRegRangePtr range2) {
1925 int ret = 0;
1926
1927 if ((range1->type == XML_REGEXP_RANGES) ||
1928 (range2->type == XML_REGEXP_RANGES) ||
1929 (range2->type == XML_REGEXP_SUBREG) ||
1930 (range1->type == XML_REGEXP_SUBREG) ||
1931 (range1->type == XML_REGEXP_STRING) ||
1932 (range2->type == XML_REGEXP_STRING))
1933 return(-1);
1934
1935 /* put them in order */
1936 if (range1->type > range2->type) {
1937 xmlRegRangePtr tmp;
1938
1939 tmp = range1;
1940 range1 = range2;
1941 range2 = tmp;
1942 }
1943 if ((range1->type == XML_REGEXP_ANYCHAR) ||
1944 (range2->type == XML_REGEXP_ANYCHAR)) {
1945 ret = 1;
1946 } else if ((range1->type == XML_REGEXP_EPSILON) ||
1947 (range2->type == XML_REGEXP_EPSILON)) {
1948 return(0);
1949 } else if (range1->type == range2->type) {
1950 if ((range1->type != XML_REGEXP_CHARVAL) ||
1951 (range1->end < range2->start) ||
1952 (range2->end < range1->start))
1953 ret = 1;
1954 else
1955 ret = 0;
1956 } else if (range1->type == XML_REGEXP_CHARVAL) {
1957 int codepoint;
1958 int neg = 0;
1959
1960 /*
1961 * just check all codepoints in the range for acceptance,
1962 * this is usually way cheaper since done only once at
1963 * compilation than testing over and over at runtime or
1964 * pushing too many states when evaluating.
1965 */
1966 if (((range1->neg == 0) && (range2->neg != 0)) ||
1967 ((range1->neg != 0) && (range2->neg == 0)))
1968 neg = 1;
1969
1970 for (codepoint = range1->start;codepoint <= range1->end ;codepoint++) {
1971 ret = xmlRegCheckCharacterRange(range2->type, codepoint,
1972 0, range2->start, range2->end,
1973 range2->blockName);
1974 if (ret < 0)
1975 return(-1);
1976 if (((neg == 1) && (ret == 0)) ||
1977 ((neg == 0) && (ret == 1)))
1978 return(1);
1979 }
1980 return(0);
1981 } else if ((range1->type == XML_REGEXP_BLOCK_NAME) ||
1982 (range2->type == XML_REGEXP_BLOCK_NAME)) {
1983 if (range1->type == range2->type) {
1984 ret = xmlStrEqual(range1->blockName, range2->blockName);
1985 } else {
1986 /*
1987 * comparing a block range with anything else is way
1988 * too costly, and maintining the table is like too much
1989 * memory too, so let's force the automata to save state
1990 * here.
1991 */
1992 return(1);
1993 }
1994 } else if ((range1->type < XML_REGEXP_LETTER) ||
1995 (range2->type < XML_REGEXP_LETTER)) {
1996 if ((range1->type == XML_REGEXP_ANYSPACE) &&
1997 (range2->type == XML_REGEXP_NOTSPACE))
1998 ret = 0;
1999 else if ((range1->type == XML_REGEXP_INITNAME) &&
2000 (range2->type == XML_REGEXP_NOTINITNAME))
2001 ret = 0;
2002 else if ((range1->type == XML_REGEXP_NAMECHAR) &&
2003 (range2->type == XML_REGEXP_NOTNAMECHAR))
2004 ret = 0;
2005 else if ((range1->type == XML_REGEXP_DECIMAL) &&
2006 (range2->type == XML_REGEXP_NOTDECIMAL))
2007 ret = 0;
2008 else if ((range1->type == XML_REGEXP_REALCHAR) &&
2009 (range2->type == XML_REGEXP_NOTREALCHAR))
2010 ret = 0;
2011 else {
2012 /* same thing to limit complexity */
2013 return(1);
2014 }
2015 } else {
2016 ret = 0;
2017 /* range1->type < range2->type here */
2018 switch (range1->type) {
2019 case XML_REGEXP_LETTER:
2020 /* all disjoint except in the subgroups */
2021 if ((range2->type == XML_REGEXP_LETTER_UPPERCASE) ||
2022 (range2->type == XML_REGEXP_LETTER_LOWERCASE) ||
2023 (range2->type == XML_REGEXP_LETTER_TITLECASE) ||
2024 (range2->type == XML_REGEXP_LETTER_MODIFIER) ||
2025 (range2->type == XML_REGEXP_LETTER_OTHERS))
2026 ret = 1;
2027 break;
2028 case XML_REGEXP_MARK:
2029 if ((range2->type == XML_REGEXP_MARK_NONSPACING) ||
2030 (range2->type == XML_REGEXP_MARK_SPACECOMBINING) ||
2031 (range2->type == XML_REGEXP_MARK_ENCLOSING))
2032 ret = 1;
2033 break;
2034 case XML_REGEXP_NUMBER:
2035 if ((range2->type == XML_REGEXP_NUMBER_DECIMAL) ||
2036 (range2->type == XML_REGEXP_NUMBER_LETTER) ||
2037 (range2->type == XML_REGEXP_NUMBER_OTHERS))
2038 ret = 1;
2039 break;
2040 case XML_REGEXP_PUNCT:
2041 if ((range2->type == XML_REGEXP_PUNCT_CONNECTOR) ||
2042 (range2->type == XML_REGEXP_PUNCT_DASH) ||
2043 (range2->type == XML_REGEXP_PUNCT_OPEN) ||
2044 (range2->type == XML_REGEXP_PUNCT_CLOSE) ||
2045 (range2->type == XML_REGEXP_PUNCT_INITQUOTE) ||
2046 (range2->type == XML_REGEXP_PUNCT_FINQUOTE) ||
2047 (range2->type == XML_REGEXP_PUNCT_OTHERS))
2048 ret = 1;
2049 break;
2050 case XML_REGEXP_SEPAR:
2051 if ((range2->type == XML_REGEXP_SEPAR_SPACE) ||
2052 (range2->type == XML_REGEXP_SEPAR_LINE) ||
2053 (range2->type == XML_REGEXP_SEPAR_PARA))
2054 ret = 1;
2055 break;
2056 case XML_REGEXP_SYMBOL:
2057 if ((range2->type == XML_REGEXP_SYMBOL_MATH) ||
2058 (range2->type == XML_REGEXP_SYMBOL_CURRENCY) ||
2059 (range2->type == XML_REGEXP_SYMBOL_MODIFIER) ||
2060 (range2->type == XML_REGEXP_SYMBOL_OTHERS))
2061 ret = 1;
2062 break;
2063 case XML_REGEXP_OTHER:
2064 if ((range2->type == XML_REGEXP_OTHER_CONTROL) ||
2065 (range2->type == XML_REGEXP_OTHER_FORMAT) ||
2066 (range2->type == XML_REGEXP_OTHER_PRIVATE))
2067 ret = 1;
2068 break;
2069 default:
2070 if ((range2->type >= XML_REGEXP_LETTER) &&
2071 (range2->type < XML_REGEXP_BLOCK_NAME))
2072 ret = 0;
2073 else {
2074 /* safety net ! */
2075 return(1);
2076 }
2077 }
2078 }
2079 if (((range1->neg == 0) && (range2->neg != 0)) ||
2080 ((range1->neg != 0) && (range2->neg == 0)))
2081 ret = !ret;
2082 return(1);
2083}
2084
Daniel Veillarde19fc232002-04-22 16:01:24 +00002085/**
Daniel Veillardfc011b72006-02-12 19:14:15 +00002086 * xmlFACompareAtomTypes:
2087 * @type1: an atom type
2088 * @type2: an atom type
2089 *
2090 * Compares two atoms type to check whether they intersect in some ways,
2091 * this is used by xmlFACompareAtoms only
2092 *
2093 * Returns 1 if they may intersect and 0 otherwise
2094 */
2095static int
2096xmlFACompareAtomTypes(xmlRegAtomType type1, xmlRegAtomType type2) {
2097 if ((type1 == XML_REGEXP_EPSILON) ||
2098 (type1 == XML_REGEXP_CHARVAL) ||
2099 (type1 == XML_REGEXP_RANGES) ||
2100 (type1 == XML_REGEXP_SUBREG) ||
2101 (type1 == XML_REGEXP_STRING) ||
2102 (type1 == XML_REGEXP_ANYCHAR))
2103 return(1);
2104 if ((type2 == XML_REGEXP_EPSILON) ||
2105 (type2 == XML_REGEXP_CHARVAL) ||
2106 (type2 == XML_REGEXP_RANGES) ||
2107 (type2 == XML_REGEXP_SUBREG) ||
2108 (type2 == XML_REGEXP_STRING) ||
2109 (type2 == XML_REGEXP_ANYCHAR))
2110 return(1);
2111
2112 if (type1 == type2) return(1);
2113
2114 /* simplify subsequent compares by making sure type1 < type2 */
2115 if (type1 > type2) {
2116 xmlRegAtomType tmp = type1;
2117 type1 = type2;
2118 type2 = tmp;
2119 }
2120 switch (type1) {
2121 case XML_REGEXP_ANYSPACE: /* \s */
2122 /* can't be a letter, number, mark, pontuation, symbol */
2123 if ((type2 == XML_REGEXP_NOTSPACE) ||
2124 ((type2 >= XML_REGEXP_LETTER) &&
2125 (type2 <= XML_REGEXP_LETTER_OTHERS)) ||
2126 ((type2 >= XML_REGEXP_NUMBER) &&
2127 (type2 <= XML_REGEXP_NUMBER_OTHERS)) ||
2128 ((type2 >= XML_REGEXP_MARK) &&
2129 (type2 <= XML_REGEXP_MARK_ENCLOSING)) ||
2130 ((type2 >= XML_REGEXP_PUNCT) &&
2131 (type2 <= XML_REGEXP_PUNCT_OTHERS)) ||
2132 ((type2 >= XML_REGEXP_SYMBOL) &&
2133 (type2 <= XML_REGEXP_SYMBOL_OTHERS))
2134 ) return(0);
2135 break;
2136 case XML_REGEXP_NOTSPACE: /* \S */
2137 break;
2138 case XML_REGEXP_INITNAME: /* \l */
2139 /* can't be a number, mark, separator, pontuation, symbol or other */
2140 if ((type2 == XML_REGEXP_NOTINITNAME) ||
2141 ((type2 >= XML_REGEXP_NUMBER) &&
2142 (type2 <= XML_REGEXP_NUMBER_OTHERS)) ||
2143 ((type2 >= XML_REGEXP_MARK) &&
2144 (type2 <= XML_REGEXP_MARK_ENCLOSING)) ||
2145 ((type2 >= XML_REGEXP_SEPAR) &&
2146 (type2 <= XML_REGEXP_SEPAR_PARA)) ||
2147 ((type2 >= XML_REGEXP_PUNCT) &&
2148 (type2 <= XML_REGEXP_PUNCT_OTHERS)) ||
2149 ((type2 >= XML_REGEXP_SYMBOL) &&
2150 (type2 <= XML_REGEXP_SYMBOL_OTHERS)) ||
2151 ((type2 >= XML_REGEXP_OTHER) &&
2152 (type2 <= XML_REGEXP_OTHER_NA))
2153 ) return(0);
2154 break;
2155 case XML_REGEXP_NOTINITNAME: /* \L */
2156 break;
2157 case XML_REGEXP_NAMECHAR: /* \c */
2158 /* can't be a mark, separator, pontuation, symbol or other */
2159 if ((type2 == XML_REGEXP_NOTNAMECHAR) ||
2160 ((type2 >= XML_REGEXP_MARK) &&
2161 (type2 <= XML_REGEXP_MARK_ENCLOSING)) ||
2162 ((type2 >= XML_REGEXP_PUNCT) &&
2163 (type2 <= XML_REGEXP_PUNCT_OTHERS)) ||
2164 ((type2 >= XML_REGEXP_SEPAR) &&
2165 (type2 <= XML_REGEXP_SEPAR_PARA)) ||
2166 ((type2 >= XML_REGEXP_SYMBOL) &&
2167 (type2 <= XML_REGEXP_SYMBOL_OTHERS)) ||
2168 ((type2 >= XML_REGEXP_OTHER) &&
2169 (type2 <= XML_REGEXP_OTHER_NA))
2170 ) return(0);
2171 break;
2172 case XML_REGEXP_NOTNAMECHAR: /* \C */
2173 break;
2174 case XML_REGEXP_DECIMAL: /* \d */
2175 /* can't be a letter, mark, separator, pontuation, symbol or other */
2176 if ((type2 == XML_REGEXP_NOTDECIMAL) ||
2177 (type2 == XML_REGEXP_REALCHAR) ||
2178 ((type2 >= XML_REGEXP_LETTER) &&
2179 (type2 <= XML_REGEXP_LETTER_OTHERS)) ||
2180 ((type2 >= XML_REGEXP_MARK) &&
2181 (type2 <= XML_REGEXP_MARK_ENCLOSING)) ||
2182 ((type2 >= XML_REGEXP_PUNCT) &&
2183 (type2 <= XML_REGEXP_PUNCT_OTHERS)) ||
2184 ((type2 >= XML_REGEXP_SEPAR) &&
2185 (type2 <= XML_REGEXP_SEPAR_PARA)) ||
2186 ((type2 >= XML_REGEXP_SYMBOL) &&
2187 (type2 <= XML_REGEXP_SYMBOL_OTHERS)) ||
2188 ((type2 >= XML_REGEXP_OTHER) &&
2189 (type2 <= XML_REGEXP_OTHER_NA))
2190 )return(0);
2191 break;
2192 case XML_REGEXP_NOTDECIMAL: /* \D */
2193 break;
2194 case XML_REGEXP_REALCHAR: /* \w */
2195 /* can't be a mark, separator, pontuation, symbol or other */
2196 if ((type2 == XML_REGEXP_NOTDECIMAL) ||
2197 ((type2 >= XML_REGEXP_MARK) &&
2198 (type2 <= XML_REGEXP_MARK_ENCLOSING)) ||
2199 ((type2 >= XML_REGEXP_PUNCT) &&
2200 (type2 <= XML_REGEXP_PUNCT_OTHERS)) ||
2201 ((type2 >= XML_REGEXP_SEPAR) &&
2202 (type2 <= XML_REGEXP_SEPAR_PARA)) ||
2203 ((type2 >= XML_REGEXP_SYMBOL) &&
2204 (type2 <= XML_REGEXP_SYMBOL_OTHERS)) ||
2205 ((type2 >= XML_REGEXP_OTHER) &&
2206 (type2 <= XML_REGEXP_OTHER_NA))
2207 )return(0);
2208 break;
2209 case XML_REGEXP_NOTREALCHAR: /* \W */
2210 break;
2211 /*
2212 * at that point we know both type 1 and type2 are from
2213 * character categories are ordered and are different,
2214 * it becomes simple because this is a partition
2215 */
2216 case XML_REGEXP_LETTER:
2217 if (type2 <= XML_REGEXP_LETTER_OTHERS)
2218 return(1);
2219 return(0);
2220 case XML_REGEXP_LETTER_UPPERCASE:
2221 case XML_REGEXP_LETTER_LOWERCASE:
2222 case XML_REGEXP_LETTER_TITLECASE:
2223 case XML_REGEXP_LETTER_MODIFIER:
2224 case XML_REGEXP_LETTER_OTHERS:
2225 return(0);
2226 case XML_REGEXP_MARK:
2227 if (type2 <= XML_REGEXP_MARK_ENCLOSING)
2228 return(1);
2229 return(0);
2230 case XML_REGEXP_MARK_NONSPACING:
2231 case XML_REGEXP_MARK_SPACECOMBINING:
2232 case XML_REGEXP_MARK_ENCLOSING:
2233 return(0);
2234 case XML_REGEXP_NUMBER:
2235 if (type2 <= XML_REGEXP_NUMBER_OTHERS)
2236 return(1);
2237 return(0);
2238 case XML_REGEXP_NUMBER_DECIMAL:
2239 case XML_REGEXP_NUMBER_LETTER:
2240 case XML_REGEXP_NUMBER_OTHERS:
2241 return(0);
2242 case XML_REGEXP_PUNCT:
2243 if (type2 <= XML_REGEXP_PUNCT_OTHERS)
2244 return(1);
2245 return(0);
2246 case XML_REGEXP_PUNCT_CONNECTOR:
2247 case XML_REGEXP_PUNCT_DASH:
2248 case XML_REGEXP_PUNCT_OPEN:
2249 case XML_REGEXP_PUNCT_CLOSE:
2250 case XML_REGEXP_PUNCT_INITQUOTE:
2251 case XML_REGEXP_PUNCT_FINQUOTE:
2252 case XML_REGEXP_PUNCT_OTHERS:
2253 return(0);
2254 case XML_REGEXP_SEPAR:
2255 if (type2 <= XML_REGEXP_SEPAR_PARA)
2256 return(1);
2257 return(0);
2258 case XML_REGEXP_SEPAR_SPACE:
2259 case XML_REGEXP_SEPAR_LINE:
2260 case XML_REGEXP_SEPAR_PARA:
2261 return(0);
2262 case XML_REGEXP_SYMBOL:
2263 if (type2 <= XML_REGEXP_SYMBOL_OTHERS)
2264 return(1);
2265 return(0);
2266 case XML_REGEXP_SYMBOL_MATH:
2267 case XML_REGEXP_SYMBOL_CURRENCY:
2268 case XML_REGEXP_SYMBOL_MODIFIER:
2269 case XML_REGEXP_SYMBOL_OTHERS:
2270 return(0);
2271 case XML_REGEXP_OTHER:
2272 if (type2 <= XML_REGEXP_OTHER_NA)
2273 return(1);
2274 return(0);
2275 case XML_REGEXP_OTHER_CONTROL:
2276 case XML_REGEXP_OTHER_FORMAT:
2277 case XML_REGEXP_OTHER_PRIVATE:
2278 case XML_REGEXP_OTHER_NA:
2279 return(0);
2280 default:
2281 break;
2282 }
2283 return(1);
2284}
2285
2286/**
2287 * xmlFAEqualAtoms:
Daniel Veillarde19fc232002-04-22 16:01:24 +00002288 * @atom1: an atom
2289 * @atom2: an atom
2290 *
Daniel Veillardfc011b72006-02-12 19:14:15 +00002291 * Compares two atoms to check whether they are the same exactly
2292 * this is used to remove equivalent transitions
Daniel Veillarde19fc232002-04-22 16:01:24 +00002293 *
Daniel Veillardfc011b72006-02-12 19:14:15 +00002294 * Returns 1 if same and 0 otherwise
Daniel Veillarde19fc232002-04-22 16:01:24 +00002295 */
2296static int
Daniel Veillardfc011b72006-02-12 19:14:15 +00002297xmlFAEqualAtoms(xmlRegAtomPtr atom1, xmlRegAtomPtr atom2) {
2298 int ret = 0;
Daniel Veillard9efc4762005-07-19 14:33:55 +00002299
Daniel Veillarde19fc232002-04-22 16:01:24 +00002300 if (atom1 == atom2)
2301 return(1);
2302 if ((atom1 == NULL) || (atom2 == NULL))
2303 return(0);
2304
Daniel Veillardfc011b72006-02-12 19:14:15 +00002305 if (atom1->type != atom2->type)
2306 return(0);
2307 switch (atom1->type) {
2308 case XML_REGEXP_EPSILON:
2309 ret = 0;
2310 break;
2311 case XML_REGEXP_STRING:
2312 ret = xmlStrEqual((xmlChar *)atom1->valuep,
2313 (xmlChar *)atom2->valuep);
2314 break;
2315 case XML_REGEXP_CHARVAL:
2316 ret = (atom1->codepoint == atom2->codepoint);
2317 break;
2318 case XML_REGEXP_RANGES:
2319 /* too hard to do in the general case */
2320 ret = 0;
2321 default:
2322 break;
2323 }
2324 return(ret);
2325}
2326
2327/**
2328 * xmlFACompareAtoms:
2329 * @atom1: an atom
2330 * @atom2: an atom
2331 *
2332 * Compares two atoms to check whether they intersect in some ways,
2333 * this is used by xmlFAComputesDeterminism and xmlFARecurseDeterminism only
2334 *
2335 * Returns 1 if yes and 0 otherwise
2336 */
2337static int
2338xmlFACompareAtoms(xmlRegAtomPtr atom1, xmlRegAtomPtr atom2) {
2339 int ret = 1;
2340
2341 if (atom1 == atom2)
2342 return(1);
2343 if ((atom1 == NULL) || (atom2 == NULL))
2344 return(0);
2345
2346 if ((atom1->type == XML_REGEXP_ANYCHAR) ||
2347 (atom2->type == XML_REGEXP_ANYCHAR))
2348 return(1);
2349
2350 if (atom1->type > atom2->type) {
Daniel Veillard567a45b2005-10-18 19:11:55 +00002351 xmlRegAtomPtr tmp;
2352 tmp = atom1;
2353 atom1 = atom2;
2354 atom2 = tmp;
Daniel Veillardfc011b72006-02-12 19:14:15 +00002355 }
2356 if (atom1->type != atom2->type) {
2357 ret = xmlFACompareAtomTypes(atom1->type, atom2->type);
2358 /* if they can't intersect at the type level break now */
2359 if (ret == 0)
2360 return(0);
Daniel Veillard567a45b2005-10-18 19:11:55 +00002361 }
Daniel Veillarde19fc232002-04-22 16:01:24 +00002362 switch (atom1->type) {
2363 case XML_REGEXP_STRING:
Daniel Veillard9efc4762005-07-19 14:33:55 +00002364 ret = xmlRegStrEqualWildcard((xmlChar *)atom1->valuep,
2365 (xmlChar *)atom2->valuep);
2366 break;
Daniel Veillarde19fc232002-04-22 16:01:24 +00002367 case XML_REGEXP_EPSILON:
Daniel Veillard567a45b2005-10-18 19:11:55 +00002368 goto not_determinist;
Daniel Veillarde19fc232002-04-22 16:01:24 +00002369 case XML_REGEXP_CHARVAL:
Daniel Veillardfc011b72006-02-12 19:14:15 +00002370 if (atom2->type == XML_REGEXP_CHARVAL) {
2371 ret = (atom1->codepoint == atom2->codepoint);
2372 } else {
2373 ret = xmlRegCheckCharacter(atom2, atom1->codepoint);
2374 if (ret < 0)
2375 ret = 1;
2376 }
Daniel Veillard9efc4762005-07-19 14:33:55 +00002377 break;
Daniel Veillarde19fc232002-04-22 16:01:24 +00002378 case XML_REGEXP_RANGES:
Daniel Veillardfc011b72006-02-12 19:14:15 +00002379 if (atom2->type == XML_REGEXP_RANGES) {
Daniel Veillard567a45b2005-10-18 19:11:55 +00002380 int i, j, res;
2381 xmlRegRangePtr r1, r2;
2382
2383 /*
2384 * need to check that none of the ranges eventually matches
2385 */
2386 for (i = 0;i < atom1->nbRanges;i++) {
2387 for (j = 0;j < atom2->nbRanges;j++) {
2388 r1 = atom1->ranges[i];
2389 r2 = atom2->ranges[j];
2390 res = xmlFACompareRanges(r1, r2);
2391 if (res == 1) {
2392 ret = 1;
2393 goto done;
2394 }
2395 }
2396 }
2397 ret = 0;
2398 }
2399 break;
Daniel Veillarde19fc232002-04-22 16:01:24 +00002400 default:
Daniel Veillard567a45b2005-10-18 19:11:55 +00002401 goto not_determinist;
Daniel Veillarde19fc232002-04-22 16:01:24 +00002402 }
Daniel Veillard567a45b2005-10-18 19:11:55 +00002403done:
Daniel Veillard6e65e152005-08-09 11:09:52 +00002404 if (atom1->neg != atom2->neg) {
Daniel Veillard9efc4762005-07-19 14:33:55 +00002405 ret = !ret;
Daniel Veillard6e65e152005-08-09 11:09:52 +00002406 }
Daniel Veillard567a45b2005-10-18 19:11:55 +00002407 if (ret == 0)
2408 return(0);
2409not_determinist:
2410 return(1);
Daniel Veillarde19fc232002-04-22 16:01:24 +00002411}
2412
2413/**
2414 * xmlFARecurseDeterminism:
2415 * @ctxt: a regexp parser context
2416 *
2417 * Check whether the associated regexp is determinist,
2418 * should be called after xmlFAEliminateEpsilonTransitions()
2419 *
2420 */
2421static int
2422xmlFARecurseDeterminism(xmlRegParserCtxtPtr ctxt, xmlRegStatePtr state,
2423 int to, xmlRegAtomPtr atom) {
2424 int ret = 1;
Daniel Veillard567a45b2005-10-18 19:11:55 +00002425 int res;
Daniel Veillard5de09382005-09-26 17:18:17 +00002426 int transnr, nbTrans;
Daniel Veillarde19fc232002-04-22 16:01:24 +00002427 xmlRegTransPtr t1;
2428
2429 if (state == NULL)
2430 return(ret);
Daniel Veillard5de09382005-09-26 17:18:17 +00002431 /*
2432 * don't recurse on transitions potentially added in the course of
2433 * the elimination.
2434 */
2435 nbTrans = state->nbTrans;
2436 for (transnr = 0;transnr < nbTrans;transnr++) {
Daniel Veillarde19fc232002-04-22 16:01:24 +00002437 t1 = &(state->trans[transnr]);
2438 /*
2439 * check transitions conflicting with the one looked at
2440 */
2441 if (t1->atom == NULL) {
Daniel Veillard0e05f4c2006-11-01 15:33:04 +00002442 if (t1->to < 0)
Daniel Veillarde19fc232002-04-22 16:01:24 +00002443 continue;
Daniel Veillard567a45b2005-10-18 19:11:55 +00002444 res = xmlFARecurseDeterminism(ctxt, ctxt->states[t1->to],
Daniel Veillarde19fc232002-04-22 16:01:24 +00002445 to, atom);
Daniel Veillard567a45b2005-10-18 19:11:55 +00002446 if (res == 0) {
2447 ret = 0;
Daniel Veillardaa622012005-10-20 15:55:25 +00002448 /* t1->nd = 1; */
Daniel Veillard567a45b2005-10-18 19:11:55 +00002449 }
Daniel Veillarde19fc232002-04-22 16:01:24 +00002450 continue;
2451 }
2452 if (t1->to != to)
2453 continue;
Daniel Veillard567a45b2005-10-18 19:11:55 +00002454 if (xmlFACompareAtoms(t1->atom, atom)) {
2455 ret = 0;
2456 /* mark the transition as non-deterministic */
2457 t1->nd = 1;
2458 }
Daniel Veillarde19fc232002-04-22 16:01:24 +00002459 }
2460 return(ret);
2461}
2462
2463/**
2464 * xmlFAComputesDeterminism:
2465 * @ctxt: a regexp parser context
2466 *
2467 * Check whether the associated regexp is determinist,
2468 * should be called after xmlFAEliminateEpsilonTransitions()
2469 *
2470 */
2471static int
2472xmlFAComputesDeterminism(xmlRegParserCtxtPtr ctxt) {
2473 int statenr, transnr;
2474 xmlRegStatePtr state;
Daniel Veillard567a45b2005-10-18 19:11:55 +00002475 xmlRegTransPtr t1, t2, last;
Daniel Veillarde19fc232002-04-22 16:01:24 +00002476 int i;
2477 int ret = 1;
2478
Daniel Veillard4402ab42002-09-12 16:02:56 +00002479#ifdef DEBUG_REGEXP_GRAPH
2480 printf("xmlFAComputesDeterminism\n");
2481 xmlRegPrintCtxt(stdout, ctxt);
2482#endif
Daniel Veillarde19fc232002-04-22 16:01:24 +00002483 if (ctxt->determinist != -1)
2484 return(ctxt->determinist);
2485
2486 /*
Daniel Veillard567a45b2005-10-18 19:11:55 +00002487 * First cleanup the automata removing cancelled transitions
Daniel Veillarde19fc232002-04-22 16:01:24 +00002488 */
2489 for (statenr = 0;statenr < ctxt->nbStates;statenr++) {
2490 state = ctxt->states[statenr];
2491 if (state == NULL)
2492 continue;
Daniel Veillard4f82c8a2005-08-09 21:40:08 +00002493 if (state->nbTrans < 2)
2494 continue;
Daniel Veillarde19fc232002-04-22 16:01:24 +00002495 for (transnr = 0;transnr < state->nbTrans;transnr++) {
2496 t1 = &(state->trans[transnr]);
2497 /*
2498 * Determinism checks in case of counted or all transitions
2499 * will have to be handled separately
2500 */
Daniel Veillard567a45b2005-10-18 19:11:55 +00002501 if (t1->atom == NULL) {
Daniel Veillardaa622012005-10-20 15:55:25 +00002502 /* t1->nd = 1; */
Daniel Veillarde19fc232002-04-22 16:01:24 +00002503 continue;
Daniel Veillard567a45b2005-10-18 19:11:55 +00002504 }
Daniel Veillarde19fc232002-04-22 16:01:24 +00002505 if (t1->to == -1) /* eliminated */
2506 continue;
2507 for (i = 0;i < transnr;i++) {
2508 t2 = &(state->trans[i]);
2509 if (t2->to == -1) /* eliminated */
2510 continue;
2511 if (t2->atom != NULL) {
2512 if (t1->to == t2->to) {
Daniel Veillardfc011b72006-02-12 19:14:15 +00002513 if (xmlFAEqualAtoms(t1->atom, t2->atom))
William M. Brackddf71d62004-05-06 04:17:26 +00002514 t2->to = -1; /* eliminated */
Daniel Veillard567a45b2005-10-18 19:11:55 +00002515 }
2516 }
2517 }
2518 }
2519 }
2520
2521 /*
2522 * Check for all states that there aren't 2 transitions
2523 * with the same atom and a different target.
2524 */
2525 for (statenr = 0;statenr < ctxt->nbStates;statenr++) {
2526 state = ctxt->states[statenr];
2527 if (state == NULL)
2528 continue;
2529 if (state->nbTrans < 2)
2530 continue;
2531 last = NULL;
2532 for (transnr = 0;transnr < state->nbTrans;transnr++) {
2533 t1 = &(state->trans[transnr]);
2534 /*
2535 * Determinism checks in case of counted or all transitions
2536 * will have to be handled separately
2537 */
2538 if (t1->atom == NULL) {
2539 continue;
2540 }
2541 if (t1->to == -1) /* eliminated */
2542 continue;
2543 for (i = 0;i < transnr;i++) {
2544 t2 = &(state->trans[i]);
2545 if (t2->to == -1) /* eliminated */
2546 continue;
2547 if (t2->atom != NULL) {
2548 /* not determinist ! */
2549 if (xmlFACompareAtoms(t1->atom, t2->atom)) {
2550 ret = 0;
2551 /* mark the transitions as non-deterministic ones */
2552 t1->nd = 1;
2553 t2->nd = 1;
2554 last = t1;
Daniel Veillarde19fc232002-04-22 16:01:24 +00002555 }
2556 } else if (t1->to != -1) {
2557 /*
2558 * do the closure in case of remaining specific
2559 * epsilon transitions like choices or all
2560 */
2561 ret = xmlFARecurseDeterminism(ctxt, ctxt->states[t1->to],
2562 t2->to, t2->atom);
Daniel Veillard567a45b2005-10-18 19:11:55 +00002563 /* don't shortcut the computation so all non deterministic
2564 transition get marked down
Daniel Veillarde19fc232002-04-22 16:01:24 +00002565 if (ret == 0)
Daniel Veillardaa622012005-10-20 15:55:25 +00002566 return(0);
2567 */
Daniel Veillard567a45b2005-10-18 19:11:55 +00002568 if (ret == 0) {
2569 t1->nd = 1;
Daniel Veillardaa622012005-10-20 15:55:25 +00002570 /* t2->nd = 1; */
Daniel Veillard567a45b2005-10-18 19:11:55 +00002571 last = t1;
2572 }
Daniel Veillarde19fc232002-04-22 16:01:24 +00002573 }
2574 }
Daniel Veillard567a45b2005-10-18 19:11:55 +00002575 /* don't shortcut the computation so all non deterministic
2576 transition get marked down
Daniel Veillarde19fc232002-04-22 16:01:24 +00002577 if (ret == 0)
Daniel Veillard567a45b2005-10-18 19:11:55 +00002578 break; */
Daniel Veillarde19fc232002-04-22 16:01:24 +00002579 }
Daniel Veillard567a45b2005-10-18 19:11:55 +00002580
2581 /*
2582 * mark specifically the last non-deterministic transition
2583 * from a state since there is no need to set-up rollback
2584 * from it
2585 */
2586 if (last != NULL) {
2587 last->nd = 2;
2588 }
2589
2590 /* don't shortcut the computation so all non deterministic
2591 transition get marked down
Daniel Veillarde19fc232002-04-22 16:01:24 +00002592 if (ret == 0)
Daniel Veillard567a45b2005-10-18 19:11:55 +00002593 break; */
Daniel Veillarde19fc232002-04-22 16:01:24 +00002594 }
Daniel Veillard567a45b2005-10-18 19:11:55 +00002595
Daniel Veillarde19fc232002-04-22 16:01:24 +00002596 ctxt->determinist = ret;
2597 return(ret);
2598}
2599
Daniel Veillard4255d502002-04-16 15:50:10 +00002600/************************************************************************
2601 * *
2602 * Routines to check input against transition atoms *
2603 * *
2604 ************************************************************************/
2605
2606static int
2607xmlRegCheckCharacterRange(xmlRegAtomType type, int codepoint, int neg,
2608 int start, int end, const xmlChar *blockName) {
2609 int ret = 0;
2610
2611 switch (type) {
2612 case XML_REGEXP_STRING:
2613 case XML_REGEXP_SUBREG:
2614 case XML_REGEXP_RANGES:
2615 case XML_REGEXP_EPSILON:
2616 return(-1);
2617 case XML_REGEXP_ANYCHAR:
2618 ret = ((codepoint != '\n') && (codepoint != '\r'));
2619 break;
2620 case XML_REGEXP_CHARVAL:
2621 ret = ((codepoint >= start) && (codepoint <= end));
2622 break;
2623 case XML_REGEXP_NOTSPACE:
2624 neg = !neg;
2625 case XML_REGEXP_ANYSPACE:
2626 ret = ((codepoint == '\n') || (codepoint == '\r') ||
2627 (codepoint == '\t') || (codepoint == ' '));
2628 break;
2629 case XML_REGEXP_NOTINITNAME:
2630 neg = !neg;
2631 case XML_REGEXP_INITNAME:
William M. Brack871611b2003-10-18 04:53:14 +00002632 ret = (IS_LETTER(codepoint) ||
Daniel Veillard4255d502002-04-16 15:50:10 +00002633 (codepoint == '_') || (codepoint == ':'));
2634 break;
2635 case XML_REGEXP_NOTNAMECHAR:
2636 neg = !neg;
2637 case XML_REGEXP_NAMECHAR:
William M. Brack871611b2003-10-18 04:53:14 +00002638 ret = (IS_LETTER(codepoint) || IS_DIGIT(codepoint) ||
Daniel Veillard4255d502002-04-16 15:50:10 +00002639 (codepoint == '.') || (codepoint == '-') ||
2640 (codepoint == '_') || (codepoint == ':') ||
William M. Brack871611b2003-10-18 04:53:14 +00002641 IS_COMBINING(codepoint) || IS_EXTENDER(codepoint));
Daniel Veillard4255d502002-04-16 15:50:10 +00002642 break;
2643 case XML_REGEXP_NOTDECIMAL:
2644 neg = !neg;
2645 case XML_REGEXP_DECIMAL:
2646 ret = xmlUCSIsCatNd(codepoint);
2647 break;
2648 case XML_REGEXP_REALCHAR:
2649 neg = !neg;
2650 case XML_REGEXP_NOTREALCHAR:
2651 ret = xmlUCSIsCatP(codepoint);
2652 if (ret == 0)
2653 ret = xmlUCSIsCatZ(codepoint);
2654 if (ret == 0)
2655 ret = xmlUCSIsCatC(codepoint);
2656 break;
2657 case XML_REGEXP_LETTER:
2658 ret = xmlUCSIsCatL(codepoint);
2659 break;
2660 case XML_REGEXP_LETTER_UPPERCASE:
2661 ret = xmlUCSIsCatLu(codepoint);
2662 break;
2663 case XML_REGEXP_LETTER_LOWERCASE:
2664 ret = xmlUCSIsCatLl(codepoint);
2665 break;
2666 case XML_REGEXP_LETTER_TITLECASE:
2667 ret = xmlUCSIsCatLt(codepoint);
2668 break;
2669 case XML_REGEXP_LETTER_MODIFIER:
2670 ret = xmlUCSIsCatLm(codepoint);
2671 break;
2672 case XML_REGEXP_LETTER_OTHERS:
2673 ret = xmlUCSIsCatLo(codepoint);
2674 break;
2675 case XML_REGEXP_MARK:
2676 ret = xmlUCSIsCatM(codepoint);
2677 break;
2678 case XML_REGEXP_MARK_NONSPACING:
2679 ret = xmlUCSIsCatMn(codepoint);
2680 break;
2681 case XML_REGEXP_MARK_SPACECOMBINING:
2682 ret = xmlUCSIsCatMc(codepoint);
2683 break;
2684 case XML_REGEXP_MARK_ENCLOSING:
2685 ret = xmlUCSIsCatMe(codepoint);
2686 break;
2687 case XML_REGEXP_NUMBER:
2688 ret = xmlUCSIsCatN(codepoint);
2689 break;
2690 case XML_REGEXP_NUMBER_DECIMAL:
2691 ret = xmlUCSIsCatNd(codepoint);
2692 break;
2693 case XML_REGEXP_NUMBER_LETTER:
2694 ret = xmlUCSIsCatNl(codepoint);
2695 break;
2696 case XML_REGEXP_NUMBER_OTHERS:
2697 ret = xmlUCSIsCatNo(codepoint);
2698 break;
2699 case XML_REGEXP_PUNCT:
2700 ret = xmlUCSIsCatP(codepoint);
2701 break;
2702 case XML_REGEXP_PUNCT_CONNECTOR:
2703 ret = xmlUCSIsCatPc(codepoint);
2704 break;
2705 case XML_REGEXP_PUNCT_DASH:
2706 ret = xmlUCSIsCatPd(codepoint);
2707 break;
2708 case XML_REGEXP_PUNCT_OPEN:
2709 ret = xmlUCSIsCatPs(codepoint);
2710 break;
2711 case XML_REGEXP_PUNCT_CLOSE:
2712 ret = xmlUCSIsCatPe(codepoint);
2713 break;
2714 case XML_REGEXP_PUNCT_INITQUOTE:
2715 ret = xmlUCSIsCatPi(codepoint);
2716 break;
2717 case XML_REGEXP_PUNCT_FINQUOTE:
2718 ret = xmlUCSIsCatPf(codepoint);
2719 break;
2720 case XML_REGEXP_PUNCT_OTHERS:
2721 ret = xmlUCSIsCatPo(codepoint);
2722 break;
2723 case XML_REGEXP_SEPAR:
2724 ret = xmlUCSIsCatZ(codepoint);
2725 break;
2726 case XML_REGEXP_SEPAR_SPACE:
2727 ret = xmlUCSIsCatZs(codepoint);
2728 break;
2729 case XML_REGEXP_SEPAR_LINE:
2730 ret = xmlUCSIsCatZl(codepoint);
2731 break;
2732 case XML_REGEXP_SEPAR_PARA:
2733 ret = xmlUCSIsCatZp(codepoint);
2734 break;
2735 case XML_REGEXP_SYMBOL:
2736 ret = xmlUCSIsCatS(codepoint);
2737 break;
2738 case XML_REGEXP_SYMBOL_MATH:
2739 ret = xmlUCSIsCatSm(codepoint);
2740 break;
2741 case XML_REGEXP_SYMBOL_CURRENCY:
2742 ret = xmlUCSIsCatSc(codepoint);
2743 break;
2744 case XML_REGEXP_SYMBOL_MODIFIER:
2745 ret = xmlUCSIsCatSk(codepoint);
2746 break;
2747 case XML_REGEXP_SYMBOL_OTHERS:
2748 ret = xmlUCSIsCatSo(codepoint);
2749 break;
2750 case XML_REGEXP_OTHER:
2751 ret = xmlUCSIsCatC(codepoint);
2752 break;
2753 case XML_REGEXP_OTHER_CONTROL:
2754 ret = xmlUCSIsCatCc(codepoint);
2755 break;
2756 case XML_REGEXP_OTHER_FORMAT:
2757 ret = xmlUCSIsCatCf(codepoint);
2758 break;
2759 case XML_REGEXP_OTHER_PRIVATE:
2760 ret = xmlUCSIsCatCo(codepoint);
2761 break;
2762 case XML_REGEXP_OTHER_NA:
2763 /* ret = xmlUCSIsCatCn(codepoint); */
2764 /* Seems it doesn't exist anymore in recent Unicode releases */
2765 ret = 0;
2766 break;
2767 case XML_REGEXP_BLOCK_NAME:
2768 ret = xmlUCSIsBlock(codepoint, (const char *) blockName);
2769 break;
2770 }
2771 if (neg)
2772 return(!ret);
2773 return(ret);
2774}
2775
2776static int
2777xmlRegCheckCharacter(xmlRegAtomPtr atom, int codepoint) {
2778 int i, ret = 0;
2779 xmlRegRangePtr range;
2780
William M. Brack871611b2003-10-18 04:53:14 +00002781 if ((atom == NULL) || (!IS_CHAR(codepoint)))
Daniel Veillard4255d502002-04-16 15:50:10 +00002782 return(-1);
2783
2784 switch (atom->type) {
2785 case XML_REGEXP_SUBREG:
2786 case XML_REGEXP_EPSILON:
2787 return(-1);
2788 case XML_REGEXP_CHARVAL:
2789 return(codepoint == atom->codepoint);
2790 case XML_REGEXP_RANGES: {
2791 int accept = 0;
Daniel Veillardf2a12832003-11-24 13:04:35 +00002792
Daniel Veillard4255d502002-04-16 15:50:10 +00002793 for (i = 0;i < atom->nbRanges;i++) {
2794 range = atom->ranges[i];
Daniel Veillardf8b9de32003-11-24 14:27:26 +00002795 if (range->neg == 2) {
Daniel Veillard4255d502002-04-16 15:50:10 +00002796 ret = xmlRegCheckCharacterRange(range->type, codepoint,
2797 0, range->start, range->end,
2798 range->blockName);
2799 if (ret != 0)
2800 return(0); /* excluded char */
Daniel Veillardf8b9de32003-11-24 14:27:26 +00002801 } else if (range->neg) {
2802 ret = xmlRegCheckCharacterRange(range->type, codepoint,
2803 0, range->start, range->end,
2804 range->blockName);
2805 if (ret == 0)
Daniel Veillardf2a12832003-11-24 13:04:35 +00002806 accept = 1;
Daniel Veillardf8b9de32003-11-24 14:27:26 +00002807 else
2808 return(0);
Daniel Veillard4255d502002-04-16 15:50:10 +00002809 } else {
2810 ret = xmlRegCheckCharacterRange(range->type, codepoint,
2811 0, range->start, range->end,
2812 range->blockName);
2813 if (ret != 0)
2814 accept = 1; /* might still be excluded */
2815 }
2816 }
2817 return(accept);
2818 }
2819 case XML_REGEXP_STRING:
2820 printf("TODO: XML_REGEXP_STRING\n");
2821 return(-1);
2822 case XML_REGEXP_ANYCHAR:
2823 case XML_REGEXP_ANYSPACE:
2824 case XML_REGEXP_NOTSPACE:
2825 case XML_REGEXP_INITNAME:
2826 case XML_REGEXP_NOTINITNAME:
2827 case XML_REGEXP_NAMECHAR:
2828 case XML_REGEXP_NOTNAMECHAR:
2829 case XML_REGEXP_DECIMAL:
2830 case XML_REGEXP_NOTDECIMAL:
2831 case XML_REGEXP_REALCHAR:
2832 case XML_REGEXP_NOTREALCHAR:
2833 case XML_REGEXP_LETTER:
2834 case XML_REGEXP_LETTER_UPPERCASE:
2835 case XML_REGEXP_LETTER_LOWERCASE:
2836 case XML_REGEXP_LETTER_TITLECASE:
2837 case XML_REGEXP_LETTER_MODIFIER:
2838 case XML_REGEXP_LETTER_OTHERS:
2839 case XML_REGEXP_MARK:
2840 case XML_REGEXP_MARK_NONSPACING:
2841 case XML_REGEXP_MARK_SPACECOMBINING:
2842 case XML_REGEXP_MARK_ENCLOSING:
2843 case XML_REGEXP_NUMBER:
2844 case XML_REGEXP_NUMBER_DECIMAL:
2845 case XML_REGEXP_NUMBER_LETTER:
2846 case XML_REGEXP_NUMBER_OTHERS:
2847 case XML_REGEXP_PUNCT:
2848 case XML_REGEXP_PUNCT_CONNECTOR:
2849 case XML_REGEXP_PUNCT_DASH:
2850 case XML_REGEXP_PUNCT_OPEN:
2851 case XML_REGEXP_PUNCT_CLOSE:
2852 case XML_REGEXP_PUNCT_INITQUOTE:
2853 case XML_REGEXP_PUNCT_FINQUOTE:
2854 case XML_REGEXP_PUNCT_OTHERS:
2855 case XML_REGEXP_SEPAR:
2856 case XML_REGEXP_SEPAR_SPACE:
2857 case XML_REGEXP_SEPAR_LINE:
2858 case XML_REGEXP_SEPAR_PARA:
2859 case XML_REGEXP_SYMBOL:
2860 case XML_REGEXP_SYMBOL_MATH:
2861 case XML_REGEXP_SYMBOL_CURRENCY:
2862 case XML_REGEXP_SYMBOL_MODIFIER:
2863 case XML_REGEXP_SYMBOL_OTHERS:
2864 case XML_REGEXP_OTHER:
2865 case XML_REGEXP_OTHER_CONTROL:
2866 case XML_REGEXP_OTHER_FORMAT:
2867 case XML_REGEXP_OTHER_PRIVATE:
2868 case XML_REGEXP_OTHER_NA:
2869 case XML_REGEXP_BLOCK_NAME:
2870 ret = xmlRegCheckCharacterRange(atom->type, codepoint, 0, 0, 0,
2871 (const xmlChar *)atom->valuep);
2872 if (atom->neg)
2873 ret = !ret;
2874 break;
2875 }
2876 return(ret);
2877}
2878
2879/************************************************************************
2880 * *
William M. Brackddf71d62004-05-06 04:17:26 +00002881 * Saving and restoring state of an execution context *
Daniel Veillard4255d502002-04-16 15:50:10 +00002882 * *
2883 ************************************************************************/
2884
2885#ifdef DEBUG_REGEXP_EXEC
2886static void
2887xmlFARegDebugExec(xmlRegExecCtxtPtr exec) {
2888 printf("state: %d:%d:idx %d", exec->state->no, exec->transno, exec->index);
2889 if (exec->inputStack != NULL) {
2890 int i;
2891 printf(": ");
2892 for (i = 0;(i < 3) && (i < exec->inputStackNr);i++)
Daniel Veillard0e05f4c2006-11-01 15:33:04 +00002893 printf("%s ", (const char *)
2894 exec->inputStack[exec->inputStackNr - (i + 1)].value);
Daniel Veillard4255d502002-04-16 15:50:10 +00002895 } else {
2896 printf(": %s", &(exec->inputString[exec->index]));
2897 }
2898 printf("\n");
2899}
2900#endif
2901
2902static void
2903xmlFARegExecSave(xmlRegExecCtxtPtr exec) {
2904#ifdef DEBUG_REGEXP_EXEC
2905 printf("saving ");
2906 exec->transno++;
2907 xmlFARegDebugExec(exec);
2908 exec->transno--;
2909#endif
Daniel Veillard94cc1032005-09-15 13:09:00 +00002910#ifdef MAX_PUSH
2911 if (exec->nbPush > MAX_PUSH) {
2912 return;
2913 }
2914 exec->nbPush++;
2915#endif
Daniel Veillard4255d502002-04-16 15:50:10 +00002916
2917 if (exec->maxRollbacks == 0) {
2918 exec->maxRollbacks = 4;
2919 exec->rollbacks = (xmlRegExecRollback *) xmlMalloc(exec->maxRollbacks *
2920 sizeof(xmlRegExecRollback));
2921 if (exec->rollbacks == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00002922 xmlRegexpErrMemory(NULL, "saving regexp");
Daniel Veillard4255d502002-04-16 15:50:10 +00002923 exec->maxRollbacks = 0;
2924 return;
2925 }
2926 memset(exec->rollbacks, 0,
2927 exec->maxRollbacks * sizeof(xmlRegExecRollback));
2928 } else if (exec->nbRollbacks >= exec->maxRollbacks) {
2929 xmlRegExecRollback *tmp;
2930 int len = exec->maxRollbacks;
2931
2932 exec->maxRollbacks *= 2;
2933 tmp = (xmlRegExecRollback *) xmlRealloc(exec->rollbacks,
2934 exec->maxRollbacks * sizeof(xmlRegExecRollback));
2935 if (tmp == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00002936 xmlRegexpErrMemory(NULL, "saving regexp");
Daniel Veillard4255d502002-04-16 15:50:10 +00002937 exec->maxRollbacks /= 2;
2938 return;
2939 }
2940 exec->rollbacks = tmp;
2941 tmp = &exec->rollbacks[len];
2942 memset(tmp, 0, (exec->maxRollbacks - len) * sizeof(xmlRegExecRollback));
2943 }
2944 exec->rollbacks[exec->nbRollbacks].state = exec->state;
2945 exec->rollbacks[exec->nbRollbacks].index = exec->index;
2946 exec->rollbacks[exec->nbRollbacks].nextbranch = exec->transno + 1;
2947 if (exec->comp->nbCounters > 0) {
2948 if (exec->rollbacks[exec->nbRollbacks].counts == NULL) {
2949 exec->rollbacks[exec->nbRollbacks].counts = (int *)
2950 xmlMalloc(exec->comp->nbCounters * sizeof(int));
2951 if (exec->rollbacks[exec->nbRollbacks].counts == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00002952 xmlRegexpErrMemory(NULL, "saving regexp");
Daniel Veillard4255d502002-04-16 15:50:10 +00002953 exec->status = -5;
2954 return;
2955 }
2956 }
2957 memcpy(exec->rollbacks[exec->nbRollbacks].counts, exec->counts,
2958 exec->comp->nbCounters * sizeof(int));
2959 }
2960 exec->nbRollbacks++;
2961}
2962
2963static void
2964xmlFARegExecRollBack(xmlRegExecCtxtPtr exec) {
2965 if (exec->nbRollbacks <= 0) {
2966 exec->status = -1;
2967#ifdef DEBUG_REGEXP_EXEC
2968 printf("rollback failed on empty stack\n");
2969#endif
2970 return;
2971 }
2972 exec->nbRollbacks--;
2973 exec->state = exec->rollbacks[exec->nbRollbacks].state;
2974 exec->index = exec->rollbacks[exec->nbRollbacks].index;
2975 exec->transno = exec->rollbacks[exec->nbRollbacks].nextbranch;
2976 if (exec->comp->nbCounters > 0) {
2977 if (exec->rollbacks[exec->nbRollbacks].counts == NULL) {
2978 fprintf(stderr, "exec save: allocation failed");
2979 exec->status = -6;
2980 return;
2981 }
2982 memcpy(exec->counts, exec->rollbacks[exec->nbRollbacks].counts,
2983 exec->comp->nbCounters * sizeof(int));
2984 }
2985
2986#ifdef DEBUG_REGEXP_EXEC
2987 printf("restored ");
2988 xmlFARegDebugExec(exec);
2989#endif
2990}
2991
2992/************************************************************************
2993 * *
William M. Brackddf71d62004-05-06 04:17:26 +00002994 * Verifier, running an input against a compiled regexp *
Daniel Veillard4255d502002-04-16 15:50:10 +00002995 * *
2996 ************************************************************************/
2997
2998static int
2999xmlFARegExec(xmlRegexpPtr comp, const xmlChar *content) {
3000 xmlRegExecCtxt execval;
3001 xmlRegExecCtxtPtr exec = &execval;
Daniel Veillard567a45b2005-10-18 19:11:55 +00003002 int ret, codepoint = 0, len, deter;
Daniel Veillard4255d502002-04-16 15:50:10 +00003003
3004 exec->inputString = content;
3005 exec->index = 0;
Daniel Veillard94cc1032005-09-15 13:09:00 +00003006 exec->nbPush = 0;
Daniel Veillard4255d502002-04-16 15:50:10 +00003007 exec->determinist = 1;
3008 exec->maxRollbacks = 0;
3009 exec->nbRollbacks = 0;
3010 exec->rollbacks = NULL;
3011 exec->status = 0;
3012 exec->comp = comp;
3013 exec->state = comp->states[0];
3014 exec->transno = 0;
3015 exec->transcount = 0;
Daniel Veillardf2a12832003-11-24 13:04:35 +00003016 exec->inputStack = NULL;
3017 exec->inputStackMax = 0;
Daniel Veillard4255d502002-04-16 15:50:10 +00003018 if (comp->nbCounters > 0) {
3019 exec->counts = (int *) xmlMalloc(comp->nbCounters * sizeof(int));
Daniel Veillardff46a042003-10-08 08:53:17 +00003020 if (exec->counts == NULL) {
3021 xmlRegexpErrMemory(NULL, "running regexp");
Daniel Veillard4255d502002-04-16 15:50:10 +00003022 return(-1);
Daniel Veillardff46a042003-10-08 08:53:17 +00003023 }
Daniel Veillard4255d502002-04-16 15:50:10 +00003024 memset(exec->counts, 0, comp->nbCounters * sizeof(int));
3025 } else
3026 exec->counts = NULL;
3027 while ((exec->status == 0) &&
3028 ((exec->inputString[exec->index] != 0) ||
3029 (exec->state->type != XML_REGEXP_FINAL_STATE))) {
3030 xmlRegTransPtr trans;
3031 xmlRegAtomPtr atom;
3032
3033 /*
William M. Brack0e00b282004-04-26 15:40:47 +00003034 * If end of input on non-terminal state, rollback, however we may
Daniel Veillard4255d502002-04-16 15:50:10 +00003035 * still have epsilon like transition for counted transitions
William M. Brack0e00b282004-04-26 15:40:47 +00003036 * on counters, in that case don't break too early. Additionally,
3037 * if we are working on a range like "AB{0,2}", where B is not present,
3038 * we don't want to break.
Daniel Veillard4255d502002-04-16 15:50:10 +00003039 */
Daniel Veillard11ce4002006-03-10 00:36:23 +00003040 len = 1;
William M. Brack0e00b282004-04-26 15:40:47 +00003041 if ((exec->inputString[exec->index] == 0) && (exec->counts == NULL)) {
William M. Brackddf71d62004-05-06 04:17:26 +00003042 /*
3043 * if there is a transition, we must check if
3044 * atom allows minOccurs of 0
3045 */
3046 if (exec->transno < exec->state->nbTrans) {
William M. Brack0e00b282004-04-26 15:40:47 +00003047 trans = &exec->state->trans[exec->transno];
3048 if (trans->to >=0) {
3049 atom = trans->atom;
3050 if (!((atom->min == 0) && (atom->max > 0)))
3051 goto rollback;
3052 }
3053 } else
3054 goto rollback;
3055 }
Daniel Veillard4255d502002-04-16 15:50:10 +00003056
3057 exec->transcount = 0;
3058 for (;exec->transno < exec->state->nbTrans;exec->transno++) {
3059 trans = &exec->state->trans[exec->transno];
3060 if (trans->to < 0)
3061 continue;
3062 atom = trans->atom;
3063 ret = 0;
Daniel Veillard567a45b2005-10-18 19:11:55 +00003064 deter = 1;
Daniel Veillard4255d502002-04-16 15:50:10 +00003065 if (trans->count >= 0) {
3066 int count;
3067 xmlRegCounterPtr counter;
3068
Daniel Veillard11ce4002006-03-10 00:36:23 +00003069 if (exec->counts == NULL) {
3070 exec->status = -1;
3071 goto error;
3072 }
Daniel Veillard4255d502002-04-16 15:50:10 +00003073 /*
3074 * A counted transition.
3075 */
3076
3077 count = exec->counts[trans->count];
3078 counter = &exec->comp->counters[trans->count];
3079#ifdef DEBUG_REGEXP_EXEC
3080 printf("testing count %d: val %d, min %d, max %d\n",
3081 trans->count, count, counter->min, counter->max);
3082#endif
3083 ret = ((count >= counter->min) && (count <= counter->max));
Daniel Veillard567a45b2005-10-18 19:11:55 +00003084 if ((ret) && (counter->min != counter->max))
3085 deter = 0;
Daniel Veillard4255d502002-04-16 15:50:10 +00003086 } else if (atom == NULL) {
3087 fprintf(stderr, "epsilon transition left at runtime\n");
3088 exec->status = -2;
3089 break;
3090 } else if (exec->inputString[exec->index] != 0) {
3091 codepoint = CUR_SCHAR(&(exec->inputString[exec->index]), len);
3092 ret = xmlRegCheckCharacter(atom, codepoint);
William M. Brack0e00b282004-04-26 15:40:47 +00003093 if ((ret == 1) && (atom->min >= 0) && (atom->max > 0)) {
Daniel Veillard4255d502002-04-16 15:50:10 +00003094 xmlRegStatePtr to = comp->states[trans->to];
3095
3096 /*
3097 * this is a multiple input sequence
Daniel Veillardfc6eca02005-11-01 15:24:02 +00003098 * If there is a counter associated increment it now.
3099 * before potentially saving and rollback
Daniel Veillard4255d502002-04-16 15:50:10 +00003100 */
Daniel Veillardfc6eca02005-11-01 15:24:02 +00003101 if (trans->counter >= 0) {
Daniel Veillard11ce4002006-03-10 00:36:23 +00003102 if (exec->counts == NULL) {
3103 exec->status = -1;
3104 goto error;
3105 }
Daniel Veillardfc6eca02005-11-01 15:24:02 +00003106#ifdef DEBUG_REGEXP_EXEC
3107 printf("Increasing count %d\n", trans->counter);
3108#endif
3109 exec->counts[trans->counter]++;
3110 }
Daniel Veillard4255d502002-04-16 15:50:10 +00003111 if (exec->state->nbTrans > exec->transno + 1) {
3112 xmlFARegExecSave(exec);
3113 }
3114 exec->transcount = 1;
3115 do {
3116 /*
3117 * Try to progress as much as possible on the input
3118 */
3119 if (exec->transcount == atom->max) {
3120 break;
3121 }
3122 exec->index += len;
3123 /*
3124 * End of input: stop here
3125 */
3126 if (exec->inputString[exec->index] == 0) {
3127 exec->index -= len;
3128 break;
3129 }
3130 if (exec->transcount >= atom->min) {
3131 int transno = exec->transno;
3132 xmlRegStatePtr state = exec->state;
3133
3134 /*
3135 * The transition is acceptable save it
3136 */
3137 exec->transno = -1; /* trick */
3138 exec->state = to;
3139 xmlFARegExecSave(exec);
3140 exec->transno = transno;
3141 exec->state = state;
3142 }
3143 codepoint = CUR_SCHAR(&(exec->inputString[exec->index]),
3144 len);
3145 ret = xmlRegCheckCharacter(atom, codepoint);
3146 exec->transcount++;
3147 } while (ret == 1);
3148 if (exec->transcount < atom->min)
3149 ret = 0;
3150
3151 /*
3152 * If the last check failed but one transition was found
3153 * possible, rollback
3154 */
3155 if (ret < 0)
3156 ret = 0;
3157 if (ret == 0) {
3158 goto rollback;
3159 }
Daniel Veillardfc6eca02005-11-01 15:24:02 +00003160 if (trans->counter >= 0) {
Daniel Veillard11ce4002006-03-10 00:36:23 +00003161 if (exec->counts == NULL) {
3162 exec->status = -1;
3163 goto error;
3164 }
Daniel Veillardfc6eca02005-11-01 15:24:02 +00003165#ifdef DEBUG_REGEXP_EXEC
3166 printf("Decreasing count %d\n", trans->counter);
3167#endif
3168 exec->counts[trans->counter]--;
3169 }
William M. Brack0e00b282004-04-26 15:40:47 +00003170 } else if ((ret == 0) && (atom->min == 0) && (atom->max > 0)) {
3171 /*
3172 * we don't match on the codepoint, but minOccurs of 0
3173 * says that's ok. Setting len to 0 inhibits stepping
3174 * over the codepoint.
3175 */
3176 exec->transcount = 1;
3177 len = 0;
3178 ret = 1;
Daniel Veillard4255d502002-04-16 15:50:10 +00003179 }
William M. Brack0e00b282004-04-26 15:40:47 +00003180 } else if ((atom->min == 0) && (atom->max > 0)) {
3181 /* another spot to match when minOccurs is 0 */
3182 exec->transcount = 1;
3183 len = 0;
3184 ret = 1;
Daniel Veillard4255d502002-04-16 15:50:10 +00003185 }
3186 if (ret == 1) {
Daniel Veillard567a45b2005-10-18 19:11:55 +00003187 if ((trans->nd == 1) ||
3188 ((trans->count >= 0) && (deter == 0) &&
3189 (exec->state->nbTrans > exec->transno + 1))) {
Daniel Veillardaa622012005-10-20 15:55:25 +00003190#ifdef DEBUG_REGEXP_EXEC
3191 if (trans->nd == 1)
3192 printf("Saving on nd transition atom %d for %c at %d\n",
3193 trans->atom->no, codepoint, exec->index);
3194 else
3195 printf("Saving on counted transition count %d for %c at %d\n",
3196 trans->count, codepoint, exec->index);
3197#endif
Daniel Veillard4255d502002-04-16 15:50:10 +00003198 xmlFARegExecSave(exec);
3199 }
3200 if (trans->counter >= 0) {
Daniel Veillard11ce4002006-03-10 00:36:23 +00003201 if (exec->counts == NULL) {
3202 exec->status = -1;
3203 goto error;
3204 }
Daniel Veillard4255d502002-04-16 15:50:10 +00003205#ifdef DEBUG_REGEXP_EXEC
3206 printf("Increasing count %d\n", trans->counter);
3207#endif
3208 exec->counts[trans->counter]++;
3209 }
Daniel Veillard10752282005-08-08 13:05:13 +00003210 if ((trans->count >= 0) &&
3211 (trans->count < REGEXP_ALL_COUNTER)) {
Daniel Veillard11ce4002006-03-10 00:36:23 +00003212 if (exec->counts == NULL) {
3213 exec->status = -1;
3214 goto error;
3215 }
Daniel Veillard10752282005-08-08 13:05:13 +00003216#ifdef DEBUG_REGEXP_EXEC
3217 printf("resetting count %d on transition\n",
3218 trans->count);
3219#endif
3220 exec->counts[trans->count] = 0;
3221 }
Daniel Veillard4255d502002-04-16 15:50:10 +00003222#ifdef DEBUG_REGEXP_EXEC
3223 printf("entering state %d\n", trans->to);
3224#endif
3225 exec->state = comp->states[trans->to];
3226 exec->transno = 0;
3227 if (trans->atom != NULL) {
3228 exec->index += len;
3229 }
3230 goto progress;
3231 } else if (ret < 0) {
3232 exec->status = -4;
3233 break;
3234 }
3235 }
3236 if ((exec->transno != 0) || (exec->state->nbTrans == 0)) {
3237rollback:
3238 /*
3239 * Failed to find a way out
3240 */
3241 exec->determinist = 0;
Daniel Veillardaa622012005-10-20 15:55:25 +00003242#ifdef DEBUG_REGEXP_EXEC
3243 printf("rollback from state %d on %d:%c\n", exec->state->no,
3244 codepoint,codepoint);
3245#endif
Daniel Veillard4255d502002-04-16 15:50:10 +00003246 xmlFARegExecRollBack(exec);
3247 }
3248progress:
3249 continue;
3250 }
Daniel Veillard11ce4002006-03-10 00:36:23 +00003251error:
Daniel Veillard4255d502002-04-16 15:50:10 +00003252 if (exec->rollbacks != NULL) {
3253 if (exec->counts != NULL) {
3254 int i;
3255
3256 for (i = 0;i < exec->maxRollbacks;i++)
3257 if (exec->rollbacks[i].counts != NULL)
3258 xmlFree(exec->rollbacks[i].counts);
3259 }
3260 xmlFree(exec->rollbacks);
3261 }
3262 if (exec->counts != NULL)
3263 xmlFree(exec->counts);
3264 if (exec->status == 0)
3265 return(1);
Daniel Veillard94cc1032005-09-15 13:09:00 +00003266 if (exec->status == -1) {
3267 if (exec->nbPush > MAX_PUSH)
3268 return(-1);
Daniel Veillard4255d502002-04-16 15:50:10 +00003269 return(0);
Daniel Veillard94cc1032005-09-15 13:09:00 +00003270 }
Daniel Veillard4255d502002-04-16 15:50:10 +00003271 return(exec->status);
3272}
3273
3274/************************************************************************
3275 * *
William M. Brackddf71d62004-05-06 04:17:26 +00003276 * Progressive interface to the verifier one atom at a time *
Daniel Veillard4255d502002-04-16 15:50:10 +00003277 * *
3278 ************************************************************************/
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003279#ifdef DEBUG_ERR
3280static void testerr(xmlRegExecCtxtPtr exec);
3281#endif
Daniel Veillard4255d502002-04-16 15:50:10 +00003282
3283/**
Daniel Veillard01c13b52002-12-10 15:19:08 +00003284 * xmlRegNewExecCtxt:
Daniel Veillard4255d502002-04-16 15:50:10 +00003285 * @comp: a precompiled regular expression
3286 * @callback: a callback function used for handling progresses in the
3287 * automata matching phase
3288 * @data: the context data associated to the callback in this context
3289 *
3290 * Build a context used for progressive evaluation of a regexp.
Daniel Veillard01c13b52002-12-10 15:19:08 +00003291 *
3292 * Returns the new context
Daniel Veillard4255d502002-04-16 15:50:10 +00003293 */
3294xmlRegExecCtxtPtr
3295xmlRegNewExecCtxt(xmlRegexpPtr comp, xmlRegExecCallbacks callback, void *data) {
3296 xmlRegExecCtxtPtr exec;
3297
3298 if (comp == NULL)
3299 return(NULL);
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00003300 if ((comp->compact == NULL) && (comp->states == NULL))
3301 return(NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +00003302 exec = (xmlRegExecCtxtPtr) xmlMalloc(sizeof(xmlRegExecCtxt));
3303 if (exec == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00003304 xmlRegexpErrMemory(NULL, "creating execution context");
Daniel Veillard4255d502002-04-16 15:50:10 +00003305 return(NULL);
3306 }
3307 memset(exec, 0, sizeof(xmlRegExecCtxt));
3308 exec->inputString = NULL;
3309 exec->index = 0;
3310 exec->determinist = 1;
3311 exec->maxRollbacks = 0;
3312 exec->nbRollbacks = 0;
3313 exec->rollbacks = NULL;
3314 exec->status = 0;
3315 exec->comp = comp;
Daniel Veillard23e73572002-09-19 19:56:43 +00003316 if (comp->compact == NULL)
3317 exec->state = comp->states[0];
Daniel Veillard4255d502002-04-16 15:50:10 +00003318 exec->transno = 0;
3319 exec->transcount = 0;
3320 exec->callback = callback;
3321 exec->data = data;
3322 if (comp->nbCounters > 0) {
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003323 /*
3324 * For error handling, exec->counts is allocated twice the size
3325 * the second half is used to store the data in case of rollback
3326 */
3327 exec->counts = (int *) xmlMalloc(comp->nbCounters * sizeof(int)
3328 * 2);
Daniel Veillard4255d502002-04-16 15:50:10 +00003329 if (exec->counts == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00003330 xmlRegexpErrMemory(NULL, "creating execution context");
Daniel Veillard4255d502002-04-16 15:50:10 +00003331 xmlFree(exec);
3332 return(NULL);
3333 }
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003334 memset(exec->counts, 0, comp->nbCounters * sizeof(int) * 2);
3335 exec->errCounts = &exec->counts[comp->nbCounters];
3336 } else {
Daniel Veillard4255d502002-04-16 15:50:10 +00003337 exec->counts = NULL;
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003338 exec->errCounts = NULL;
3339 }
Daniel Veillard4255d502002-04-16 15:50:10 +00003340 exec->inputStackMax = 0;
3341 exec->inputStackNr = 0;
3342 exec->inputStack = NULL;
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003343 exec->errStateNo = -1;
3344 exec->errString = NULL;
Daniel Veillard94cc1032005-09-15 13:09:00 +00003345 exec->nbPush = 0;
Daniel Veillard4255d502002-04-16 15:50:10 +00003346 return(exec);
3347}
3348
3349/**
3350 * xmlRegFreeExecCtxt:
3351 * @exec: a regular expression evaulation context
3352 *
3353 * Free the structures associated to a regular expression evaulation context.
3354 */
3355void
3356xmlRegFreeExecCtxt(xmlRegExecCtxtPtr exec) {
3357 if (exec == NULL)
3358 return;
3359
3360 if (exec->rollbacks != NULL) {
3361 if (exec->counts != NULL) {
3362 int i;
3363
3364 for (i = 0;i < exec->maxRollbacks;i++)
3365 if (exec->rollbacks[i].counts != NULL)
3366 xmlFree(exec->rollbacks[i].counts);
3367 }
3368 xmlFree(exec->rollbacks);
3369 }
3370 if (exec->counts != NULL)
3371 xmlFree(exec->counts);
3372 if (exec->inputStack != NULL) {
3373 int i;
3374
Daniel Veillard32370232002-10-16 14:08:14 +00003375 for (i = 0;i < exec->inputStackNr;i++) {
3376 if (exec->inputStack[i].value != NULL)
3377 xmlFree(exec->inputStack[i].value);
3378 }
Daniel Veillard4255d502002-04-16 15:50:10 +00003379 xmlFree(exec->inputStack);
3380 }
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003381 if (exec->errString != NULL)
3382 xmlFree(exec->errString);
Daniel Veillard4255d502002-04-16 15:50:10 +00003383 xmlFree(exec);
3384}
3385
3386static void
3387xmlFARegExecSaveInputString(xmlRegExecCtxtPtr exec, const xmlChar *value,
3388 void *data) {
3389#ifdef DEBUG_PUSH
3390 printf("saving value: %d:%s\n", exec->inputStackNr, value);
3391#endif
3392 if (exec->inputStackMax == 0) {
3393 exec->inputStackMax = 4;
3394 exec->inputStack = (xmlRegInputTokenPtr)
3395 xmlMalloc(exec->inputStackMax * sizeof(xmlRegInputToken));
3396 if (exec->inputStack == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00003397 xmlRegexpErrMemory(NULL, "pushing input string");
Daniel Veillard4255d502002-04-16 15:50:10 +00003398 exec->inputStackMax = 0;
3399 return;
3400 }
3401 } else if (exec->inputStackNr + 1 >= exec->inputStackMax) {
3402 xmlRegInputTokenPtr tmp;
3403
3404 exec->inputStackMax *= 2;
3405 tmp = (xmlRegInputTokenPtr) xmlRealloc(exec->inputStack,
3406 exec->inputStackMax * sizeof(xmlRegInputToken));
3407 if (tmp == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00003408 xmlRegexpErrMemory(NULL, "pushing input string");
Daniel Veillard4255d502002-04-16 15:50:10 +00003409 exec->inputStackMax /= 2;
3410 return;
3411 }
3412 exec->inputStack = tmp;
3413 }
3414 exec->inputStack[exec->inputStackNr].value = xmlStrdup(value);
3415 exec->inputStack[exec->inputStackNr].data = data;
3416 exec->inputStackNr++;
3417 exec->inputStack[exec->inputStackNr].value = NULL;
3418 exec->inputStack[exec->inputStackNr].data = NULL;
3419}
3420
Daniel Veillardc0826a72004-08-10 14:17:33 +00003421/**
3422 * xmlRegStrEqualWildcard:
3423 * @expStr: the string to be evaluated
3424 * @valStr: the validation string
3425 *
3426 * Checks if both strings are equal or have the same content. "*"
3427 * can be used as a wildcard in @valStr; "|" is used as a seperator of
3428 * substrings in both @expStr and @valStr.
3429 *
3430 * Returns 1 if the comparison is satisfied and the number of substrings
3431 * is equal, 0 otherwise.
3432 */
3433
3434static int
3435xmlRegStrEqualWildcard(const xmlChar *expStr, const xmlChar *valStr) {
3436 if (expStr == valStr) return(1);
3437 if (expStr == NULL) return(0);
3438 if (valStr == NULL) return(0);
3439 do {
3440 /*
3441 * Eval if we have a wildcard for the current item.
3442 */
3443 if (*expStr != *valStr) {
Daniel Veillard4f82c8a2005-08-09 21:40:08 +00003444 /* if one of them starts with a wildcard make valStr be it */
3445 if (*valStr == '*') {
3446 const xmlChar *tmp;
3447
3448 tmp = valStr;
3449 valStr = expStr;
3450 expStr = tmp;
3451 }
Daniel Veillardc0826a72004-08-10 14:17:33 +00003452 if ((*valStr != 0) && (*expStr != 0) && (*expStr++ == '*')) {
3453 do {
3454 if (*valStr == XML_REG_STRING_SEPARATOR)
3455 break;
Kasimier T. Buchcikc0e833f2005-04-19 15:02:20 +00003456 valStr++;
Daniel Veillardc0826a72004-08-10 14:17:33 +00003457 } while (*valStr != 0);
3458 continue;
3459 } else
3460 return(0);
3461 }
Kasimier T. Buchcikc0e833f2005-04-19 15:02:20 +00003462 expStr++;
3463 valStr++;
Daniel Veillardc0826a72004-08-10 14:17:33 +00003464 } while (*valStr != 0);
3465 if (*expStr != 0)
3466 return (0);
3467 else
3468 return (1);
3469}
Daniel Veillard4255d502002-04-16 15:50:10 +00003470
3471/**
Daniel Veillard23e73572002-09-19 19:56:43 +00003472 * xmlRegCompactPushString:
3473 * @exec: a regexp execution context
3474 * @comp: the precompiled exec with a compact table
3475 * @value: a string token input
3476 * @data: data associated to the token to reuse in callbacks
3477 *
3478 * Push one input token in the execution context
3479 *
3480 * Returns: 1 if the regexp reached a final state, 0 if non-final, and
3481 * a negative value in case of error.
3482 */
3483static int
3484xmlRegCompactPushString(xmlRegExecCtxtPtr exec,
3485 xmlRegexpPtr comp,
3486 const xmlChar *value,
3487 void *data) {
3488 int state = exec->index;
3489 int i, target;
3490
3491 if ((comp == NULL) || (comp->compact == NULL) || (comp->stringMap == NULL))
3492 return(-1);
3493
3494 if (value == NULL) {
3495 /*
3496 * are we at a final state ?
3497 */
3498 if (comp->compact[state * (comp->nbstrings + 1)] ==
3499 XML_REGEXP_FINAL_STATE)
3500 return(1);
3501 return(0);
3502 }
3503
3504#ifdef DEBUG_PUSH
3505 printf("value pushed: %s\n", value);
3506#endif
3507
3508 /*
William M. Brackddf71d62004-05-06 04:17:26 +00003509 * Examine all outside transitions from current state
Daniel Veillard23e73572002-09-19 19:56:43 +00003510 */
3511 for (i = 0;i < comp->nbstrings;i++) {
3512 target = comp->compact[state * (comp->nbstrings + 1) + i + 1];
3513 if ((target > 0) && (target <= comp->nbstates)) {
Daniel Veillardc0826a72004-08-10 14:17:33 +00003514 target--; /* to avoid 0 */
3515 if (xmlRegStrEqualWildcard(comp->stringMap[i], value)) {
3516 exec->index = target;
Daniel Veillard118aed72002-09-24 14:13:13 +00003517 if ((exec->callback != NULL) && (comp->transdata != NULL)) {
3518 exec->callback(exec->data, value,
3519 comp->transdata[state * comp->nbstrings + i], data);
3520 }
Daniel Veillard23e73572002-09-19 19:56:43 +00003521#ifdef DEBUG_PUSH
3522 printf("entering state %d\n", target);
3523#endif
3524 if (comp->compact[target * (comp->nbstrings + 1)] ==
Daniel Veillardcc026dc2005-01-12 13:21:17 +00003525 XML_REGEXP_SINK_STATE)
3526 goto error;
3527
3528 if (comp->compact[target * (comp->nbstrings + 1)] ==
Daniel Veillard23e73572002-09-19 19:56:43 +00003529 XML_REGEXP_FINAL_STATE)
3530 return(1);
3531 return(0);
3532 }
3533 }
3534 }
3535 /*
3536 * Failed to find an exit transition out from current state for the
3537 * current token
3538 */
3539#ifdef DEBUG_PUSH
3540 printf("failed to find a transition for %s on state %d\n", value, state);
3541#endif
Daniel Veillardcc026dc2005-01-12 13:21:17 +00003542error:
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003543 if (exec->errString != NULL)
3544 xmlFree(exec->errString);
3545 exec->errString = xmlStrdup(value);
3546 exec->errStateNo = state;
Daniel Veillard23e73572002-09-19 19:56:43 +00003547 exec->status = -1;
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003548#ifdef DEBUG_ERR
3549 testerr(exec);
3550#endif
Daniel Veillard23e73572002-09-19 19:56:43 +00003551 return(-1);
3552}
3553
3554/**
Daniel Veillard6e65e152005-08-09 11:09:52 +00003555 * xmlRegExecPushStringInternal:
Daniel Veillardea7751d2002-12-20 00:16:24 +00003556 * @exec: a regexp execution context or NULL to indicate the end
Daniel Veillard4255d502002-04-16 15:50:10 +00003557 * @value: a string token input
3558 * @data: data associated to the token to reuse in callbacks
Daniel Veillard6e65e152005-08-09 11:09:52 +00003559 * @compound: value was assembled from 2 strings
Daniel Veillard4255d502002-04-16 15:50:10 +00003560 *
3561 * Push one input token in the execution context
3562 *
3563 * Returns: 1 if the regexp reached a final state, 0 if non-final, and
3564 * a negative value in case of error.
3565 */
Daniel Veillard6e65e152005-08-09 11:09:52 +00003566static int
3567xmlRegExecPushStringInternal(xmlRegExecCtxtPtr exec, const xmlChar *value,
3568 void *data, int compound) {
Daniel Veillard4255d502002-04-16 15:50:10 +00003569 xmlRegTransPtr trans;
3570 xmlRegAtomPtr atom;
3571 int ret;
3572 int final = 0;
Daniel Veillard90700152005-01-08 22:05:09 +00003573 int progress = 1;
Daniel Veillard4255d502002-04-16 15:50:10 +00003574
3575 if (exec == NULL)
3576 return(-1);
Daniel Veillard23e73572002-09-19 19:56:43 +00003577 if (exec->comp == NULL)
3578 return(-1);
Daniel Veillard4255d502002-04-16 15:50:10 +00003579 if (exec->status != 0)
3580 return(exec->status);
3581
Daniel Veillard23e73572002-09-19 19:56:43 +00003582 if (exec->comp->compact != NULL)
3583 return(xmlRegCompactPushString(exec, exec->comp, value, data));
3584
Daniel Veillard4255d502002-04-16 15:50:10 +00003585 if (value == NULL) {
3586 if (exec->state->type == XML_REGEXP_FINAL_STATE)
3587 return(1);
3588 final = 1;
3589 }
3590
3591#ifdef DEBUG_PUSH
3592 printf("value pushed: %s\n", value);
3593#endif
3594 /*
3595 * If we have an active rollback stack push the new value there
3596 * and get back to where we were left
3597 */
3598 if ((value != NULL) && (exec->inputStackNr > 0)) {
3599 xmlFARegExecSaveInputString(exec, value, data);
3600 value = exec->inputStack[exec->index].value;
3601 data = exec->inputStack[exec->index].data;
3602#ifdef DEBUG_PUSH
3603 printf("value loaded: %s\n", value);
3604#endif
3605 }
3606
3607 while ((exec->status == 0) &&
3608 ((value != NULL) ||
3609 ((final == 1) &&
3610 (exec->state->type != XML_REGEXP_FINAL_STATE)))) {
3611
3612 /*
3613 * End of input on non-terminal state, rollback, however we may
3614 * still have epsilon like transition for counted transitions
3615 * on counters, in that case don't break too early.
3616 */
Daniel Veillardb509f152002-04-17 16:28:10 +00003617 if ((value == NULL) && (exec->counts == NULL))
Daniel Veillard4255d502002-04-16 15:50:10 +00003618 goto rollback;
3619
3620 exec->transcount = 0;
3621 for (;exec->transno < exec->state->nbTrans;exec->transno++) {
3622 trans = &exec->state->trans[exec->transno];
3623 if (trans->to < 0)
3624 continue;
3625 atom = trans->atom;
3626 ret = 0;
Daniel Veillard441bc322002-04-20 17:38:48 +00003627 if (trans->count == REGEXP_ALL_LAX_COUNTER) {
3628 int i;
3629 int count;
3630 xmlRegTransPtr t;
3631 xmlRegCounterPtr counter;
3632
3633 ret = 0;
3634
3635#ifdef DEBUG_PUSH
3636 printf("testing all lax %d\n", trans->count);
3637#endif
3638 /*
3639 * Check all counted transitions from the current state
3640 */
3641 if ((value == NULL) && (final)) {
3642 ret = 1;
3643 } else if (value != NULL) {
3644 for (i = 0;i < exec->state->nbTrans;i++) {
3645 t = &exec->state->trans[i];
3646 if ((t->counter < 0) || (t == trans))
3647 continue;
3648 counter = &exec->comp->counters[t->counter];
3649 count = exec->counts[t->counter];
3650 if ((count < counter->max) &&
3651 (t->atom != NULL) &&
3652 (xmlStrEqual(value, t->atom->valuep))) {
3653 ret = 0;
3654 break;
3655 }
3656 if ((count >= counter->min) &&
3657 (count < counter->max) &&
Daniel Veillard11ce4002006-03-10 00:36:23 +00003658 (t->atom != NULL) &&
Daniel Veillard441bc322002-04-20 17:38:48 +00003659 (xmlStrEqual(value, t->atom->valuep))) {
3660 ret = 1;
3661 break;
3662 }
3663 }
3664 }
3665 } else if (trans->count == REGEXP_ALL_COUNTER) {
Daniel Veillard8a001f62002-04-20 07:24:11 +00003666 int i;
3667 int count;
3668 xmlRegTransPtr t;
3669 xmlRegCounterPtr counter;
3670
3671 ret = 1;
3672
3673#ifdef DEBUG_PUSH
3674 printf("testing all %d\n", trans->count);
3675#endif
3676 /*
3677 * Check all counted transitions from the current state
3678 */
3679 for (i = 0;i < exec->state->nbTrans;i++) {
3680 t = &exec->state->trans[i];
3681 if ((t->counter < 0) || (t == trans))
3682 continue;
3683 counter = &exec->comp->counters[t->counter];
3684 count = exec->counts[t->counter];
3685 if ((count < counter->min) || (count > counter->max)) {
3686 ret = 0;
3687 break;
3688 }
3689 }
3690 } else if (trans->count >= 0) {
Daniel Veillard4255d502002-04-16 15:50:10 +00003691 int count;
3692 xmlRegCounterPtr counter;
3693
3694 /*
3695 * A counted transition.
3696 */
3697
3698 count = exec->counts[trans->count];
3699 counter = &exec->comp->counters[trans->count];
3700#ifdef DEBUG_PUSH
3701 printf("testing count %d: val %d, min %d, max %d\n",
3702 trans->count, count, counter->min, counter->max);
3703#endif
3704 ret = ((count >= counter->min) && (count <= counter->max));
3705 } else if (atom == NULL) {
3706 fprintf(stderr, "epsilon transition left at runtime\n");
3707 exec->status = -2;
3708 break;
3709 } else if (value != NULL) {
Daniel Veillardc0826a72004-08-10 14:17:33 +00003710 ret = xmlRegStrEqualWildcard(atom->valuep, value);
Daniel Veillard6e65e152005-08-09 11:09:52 +00003711 if (atom->neg) {
Daniel Veillard9efc4762005-07-19 14:33:55 +00003712 ret = !ret;
Daniel Veillard6e65e152005-08-09 11:09:52 +00003713 if (!compound)
3714 ret = 0;
3715 }
Daniel Veillard441bc322002-04-20 17:38:48 +00003716 if ((ret == 1) && (trans->counter >= 0)) {
3717 xmlRegCounterPtr counter;
3718 int count;
3719
3720 count = exec->counts[trans->counter];
3721 counter = &exec->comp->counters[trans->counter];
3722 if (count >= counter->max)
3723 ret = 0;
3724 }
3725
Daniel Veillard4255d502002-04-16 15:50:10 +00003726 if ((ret == 1) && (atom->min > 0) && (atom->max > 0)) {
3727 xmlRegStatePtr to = exec->comp->states[trans->to];
3728
3729 /*
3730 * this is a multiple input sequence
3731 */
3732 if (exec->state->nbTrans > exec->transno + 1) {
3733 if (exec->inputStackNr <= 0) {
3734 xmlFARegExecSaveInputString(exec, value, data);
3735 }
3736 xmlFARegExecSave(exec);
3737 }
3738 exec->transcount = 1;
3739 do {
3740 /*
3741 * Try to progress as much as possible on the input
3742 */
3743 if (exec->transcount == atom->max) {
3744 break;
3745 }
3746 exec->index++;
3747 value = exec->inputStack[exec->index].value;
3748 data = exec->inputStack[exec->index].data;
3749#ifdef DEBUG_PUSH
3750 printf("value loaded: %s\n", value);
3751#endif
3752
3753 /*
3754 * End of input: stop here
3755 */
3756 if (value == NULL) {
3757 exec->index --;
3758 break;
3759 }
3760 if (exec->transcount >= atom->min) {
3761 int transno = exec->transno;
3762 xmlRegStatePtr state = exec->state;
3763
3764 /*
3765 * The transition is acceptable save it
3766 */
3767 exec->transno = -1; /* trick */
3768 exec->state = to;
3769 if (exec->inputStackNr <= 0) {
3770 xmlFARegExecSaveInputString(exec, value, data);
3771 }
3772 xmlFARegExecSave(exec);
3773 exec->transno = transno;
3774 exec->state = state;
3775 }
3776 ret = xmlStrEqual(value, atom->valuep);
3777 exec->transcount++;
3778 } while (ret == 1);
3779 if (exec->transcount < atom->min)
3780 ret = 0;
3781
3782 /*
3783 * If the last check failed but one transition was found
3784 * possible, rollback
3785 */
3786 if (ret < 0)
3787 ret = 0;
3788 if (ret == 0) {
3789 goto rollback;
3790 }
3791 }
3792 }
3793 if (ret == 1) {
William M. Brack98873952003-12-26 06:03:14 +00003794 if ((exec->callback != NULL) && (atom != NULL) &&
3795 (data != NULL)) {
Daniel Veillard4255d502002-04-16 15:50:10 +00003796 exec->callback(exec->data, atom->valuep,
3797 atom->data, data);
3798 }
3799 if (exec->state->nbTrans > exec->transno + 1) {
3800 if (exec->inputStackNr <= 0) {
3801 xmlFARegExecSaveInputString(exec, value, data);
3802 }
3803 xmlFARegExecSave(exec);
3804 }
3805 if (trans->counter >= 0) {
3806#ifdef DEBUG_PUSH
3807 printf("Increasing count %d\n", trans->counter);
3808#endif
3809 exec->counts[trans->counter]++;
3810 }
Daniel Veillard10752282005-08-08 13:05:13 +00003811 if ((trans->count >= 0) &&
3812 (trans->count < REGEXP_ALL_COUNTER)) {
3813#ifdef DEBUG_REGEXP_EXEC
3814 printf("resetting count %d on transition\n",
3815 trans->count);
3816#endif
3817 exec->counts[trans->count] = 0;
3818 }
Daniel Veillard4255d502002-04-16 15:50:10 +00003819#ifdef DEBUG_PUSH
3820 printf("entering state %d\n", trans->to);
3821#endif
Daniel Veillardcc026dc2005-01-12 13:21:17 +00003822 if ((exec->comp->states[trans->to] != NULL) &&
3823 (exec->comp->states[trans->to]->type ==
3824 XML_REGEXP_SINK_STATE)) {
3825 /*
3826 * entering a sink state, save the current state as error
3827 * state.
3828 */
3829 if (exec->errString != NULL)
3830 xmlFree(exec->errString);
3831 exec->errString = xmlStrdup(value);
3832 exec->errState = exec->state;
3833 memcpy(exec->errCounts, exec->counts,
3834 exec->comp->nbCounters * sizeof(int));
3835 }
Daniel Veillard4255d502002-04-16 15:50:10 +00003836 exec->state = exec->comp->states[trans->to];
3837 exec->transno = 0;
3838 if (trans->atom != NULL) {
3839 if (exec->inputStack != NULL) {
3840 exec->index++;
3841 if (exec->index < exec->inputStackNr) {
3842 value = exec->inputStack[exec->index].value;
3843 data = exec->inputStack[exec->index].data;
3844#ifdef DEBUG_PUSH
3845 printf("value loaded: %s\n", value);
3846#endif
3847 } else {
3848 value = NULL;
3849 data = NULL;
3850#ifdef DEBUG_PUSH
3851 printf("end of input\n");
3852#endif
3853 }
3854 } else {
3855 value = NULL;
3856 data = NULL;
3857#ifdef DEBUG_PUSH
3858 printf("end of input\n");
3859#endif
3860 }
3861 }
3862 goto progress;
3863 } else if (ret < 0) {
3864 exec->status = -4;
3865 break;
3866 }
3867 }
3868 if ((exec->transno != 0) || (exec->state->nbTrans == 0)) {
3869rollback:
Daniel Veillard90700152005-01-08 22:05:09 +00003870 /*
Daniel Veillardcc026dc2005-01-12 13:21:17 +00003871 * if we didn't yet rollback on the current input
3872 * store the current state as the error state.
Daniel Veillard90700152005-01-08 22:05:09 +00003873 */
Daniel Veillardcc026dc2005-01-12 13:21:17 +00003874 if ((progress) && (exec->state != NULL) &&
3875 (exec->state->type != XML_REGEXP_SINK_STATE)) {
Daniel Veillard90700152005-01-08 22:05:09 +00003876 progress = 0;
3877 if (exec->errString != NULL)
3878 xmlFree(exec->errString);
3879 exec->errString = xmlStrdup(value);
3880 exec->errState = exec->state;
3881 memcpy(exec->errCounts, exec->counts,
3882 exec->comp->nbCounters * sizeof(int));
3883 }
3884
Daniel Veillard4255d502002-04-16 15:50:10 +00003885 /*
3886 * Failed to find a way out
3887 */
3888 exec->determinist = 0;
3889 xmlFARegExecRollBack(exec);
3890 if (exec->status == 0) {
3891 value = exec->inputStack[exec->index].value;
3892 data = exec->inputStack[exec->index].data;
3893#ifdef DEBUG_PUSH
3894 printf("value loaded: %s\n", value);
3895#endif
3896 }
3897 }
Daniel Veillard90700152005-01-08 22:05:09 +00003898 continue;
Daniel Veillard4255d502002-04-16 15:50:10 +00003899progress:
Daniel Veillard90700152005-01-08 22:05:09 +00003900 progress = 1;
Daniel Veillard4255d502002-04-16 15:50:10 +00003901 continue;
3902 }
3903 if (exec->status == 0) {
3904 return(exec->state->type == XML_REGEXP_FINAL_STATE);
3905 }
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003906#ifdef DEBUG_ERR
Daniel Veillard90700152005-01-08 22:05:09 +00003907 if (exec->status < 0) {
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003908 testerr(exec);
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003909 }
Daniel Veillard90700152005-01-08 22:05:09 +00003910#endif
Daniel Veillard4255d502002-04-16 15:50:10 +00003911 return(exec->status);
3912}
3913
Daniel Veillard52b48c72003-04-13 19:53:42 +00003914/**
Daniel Veillard6e65e152005-08-09 11:09:52 +00003915 * xmlRegExecPushString:
3916 * @exec: a regexp execution context or NULL to indicate the end
3917 * @value: a string token input
3918 * @data: data associated to the token to reuse in callbacks
3919 *
3920 * Push one input token in the execution context
3921 *
3922 * Returns: 1 if the regexp reached a final state, 0 if non-final, and
3923 * a negative value in case of error.
3924 */
3925int
3926xmlRegExecPushString(xmlRegExecCtxtPtr exec, const xmlChar *value,
3927 void *data) {
3928 return(xmlRegExecPushStringInternal(exec, value, data, 0));
3929}
3930
3931/**
Daniel Veillard52b48c72003-04-13 19:53:42 +00003932 * xmlRegExecPushString2:
3933 * @exec: a regexp execution context or NULL to indicate the end
3934 * @value: the first string token input
3935 * @value2: the second string token input
3936 * @data: data associated to the token to reuse in callbacks
3937 *
3938 * Push one input token in the execution context
3939 *
3940 * Returns: 1 if the regexp reached a final state, 0 if non-final, and
3941 * a negative value in case of error.
3942 */
3943int
3944xmlRegExecPushString2(xmlRegExecCtxtPtr exec, const xmlChar *value,
3945 const xmlChar *value2, void *data) {
3946 xmlChar buf[150];
3947 int lenn, lenp, ret;
3948 xmlChar *str;
3949
3950 if (exec == NULL)
3951 return(-1);
3952 if (exec->comp == NULL)
3953 return(-1);
3954 if (exec->status != 0)
3955 return(exec->status);
3956
3957 if (value2 == NULL)
3958 return(xmlRegExecPushString(exec, value, data));
3959
3960 lenn = strlen((char *) value2);
3961 lenp = strlen((char *) value);
3962
3963 if (150 < lenn + lenp + 2) {
Daniel Veillard3c908dc2003-04-19 00:07:51 +00003964 str = (xmlChar *) xmlMallocAtomic(lenn + lenp + 2);
Daniel Veillard52b48c72003-04-13 19:53:42 +00003965 if (str == NULL) {
3966 exec->status = -1;
3967 return(-1);
3968 }
3969 } else {
3970 str = buf;
3971 }
3972 memcpy(&str[0], value, lenp);
Daniel Veillardc0826a72004-08-10 14:17:33 +00003973 str[lenp] = XML_REG_STRING_SEPARATOR;
Daniel Veillard52b48c72003-04-13 19:53:42 +00003974 memcpy(&str[lenp + 1], value2, lenn);
3975 str[lenn + lenp + 1] = 0;
3976
3977 if (exec->comp->compact != NULL)
3978 ret = xmlRegCompactPushString(exec, exec->comp, str, data);
3979 else
Daniel Veillard6e65e152005-08-09 11:09:52 +00003980 ret = xmlRegExecPushStringInternal(exec, str, data, 1);
Daniel Veillard52b48c72003-04-13 19:53:42 +00003981
3982 if (str != buf)
Daniel Veillard0b1ff142005-12-28 21:13:33 +00003983 xmlFree(str);
Daniel Veillard52b48c72003-04-13 19:53:42 +00003984 return(ret);
3985}
3986
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003987/**
Daniel Veillard77005e62005-07-19 16:26:18 +00003988 * xmlRegExecGetValues:
Daniel Veillardfc0b6f62005-01-09 17:48:02 +00003989 * @exec: a regexp execution context
3990 * @err: error extraction or normal one
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003991 * @nbval: pointer to the number of accepted values IN/OUT
Daniel Veillardcc026dc2005-01-12 13:21:17 +00003992 * @nbneg: return number of negative transitions
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003993 * @values: pointer to the array of acceptable values
Daniel Veillardfc0b6f62005-01-09 17:48:02 +00003994 * @terminal: return value if this was a terminal state
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003995 *
Daniel Veillardfc0b6f62005-01-09 17:48:02 +00003996 * Extract informations from the regexp execution, internal routine to
3997 * implement xmlRegExecNextValues() and xmlRegExecErrInfo()
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003998 *
3999 * Returns: 0 in case of success or -1 in case of error.
4000 */
Daniel Veillardfc0b6f62005-01-09 17:48:02 +00004001static int
4002xmlRegExecGetValues(xmlRegExecCtxtPtr exec, int err,
Daniel Veillardcc026dc2005-01-12 13:21:17 +00004003 int *nbval, int *nbneg,
4004 xmlChar **values, int *terminal) {
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00004005 int maxval;
Daniel Veillardcc026dc2005-01-12 13:21:17 +00004006 int nb = 0;
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00004007
Daniel Veillardcc026dc2005-01-12 13:21:17 +00004008 if ((exec == NULL) || (nbval == NULL) || (nbneg == NULL) ||
4009 (values == NULL) || (*nbval <= 0))
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00004010 return(-1);
Daniel Veillardfc0b6f62005-01-09 17:48:02 +00004011
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00004012 maxval = *nbval;
4013 *nbval = 0;
Daniel Veillardcc026dc2005-01-12 13:21:17 +00004014 *nbneg = 0;
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00004015 if ((exec->comp != NULL) && (exec->comp->compact != NULL)) {
4016 xmlRegexpPtr comp;
4017 int target, i, state;
4018
4019 comp = exec->comp;
Daniel Veillardfc0b6f62005-01-09 17:48:02 +00004020
4021 if (err) {
4022 if (exec->errStateNo == -1) return(-1);
4023 state = exec->errStateNo;
4024 } else {
4025 state = exec->index;
4026 }
4027 if (terminal != NULL) {
4028 if (comp->compact[state * (comp->nbstrings + 1)] ==
4029 XML_REGEXP_FINAL_STATE)
4030 *terminal = 1;
4031 else
4032 *terminal = 0;
4033 }
Daniel Veillardcc026dc2005-01-12 13:21:17 +00004034 for (i = 0;(i < comp->nbstrings) && (nb < maxval);i++) {
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00004035 target = comp->compact[state * (comp->nbstrings + 1) + i + 1];
Daniel Veillardcc026dc2005-01-12 13:21:17 +00004036 if ((target > 0) && (target <= comp->nbstates) &&
4037 (comp->compact[(target - 1) * (comp->nbstrings + 1)] !=
4038 XML_REGEXP_SINK_STATE)) {
4039 values[nb++] = comp->stringMap[i];
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00004040 (*nbval)++;
4041 }
4042 }
Daniel Veillardcc026dc2005-01-12 13:21:17 +00004043 for (i = 0;(i < comp->nbstrings) && (nb < maxval);i++) {
4044 target = comp->compact[state * (comp->nbstrings + 1) + i + 1];
4045 if ((target > 0) && (target <= comp->nbstates) &&
4046 (comp->compact[(target - 1) * (comp->nbstrings + 1)] ==
4047 XML_REGEXP_SINK_STATE)) {
4048 values[nb++] = comp->stringMap[i];
4049 (*nbneg)++;
4050 }
4051 }
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00004052 } else {
4053 int transno;
4054 xmlRegTransPtr trans;
4055 xmlRegAtomPtr atom;
Daniel Veillardfc0b6f62005-01-09 17:48:02 +00004056 xmlRegStatePtr state;
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00004057
Daniel Veillardfc0b6f62005-01-09 17:48:02 +00004058 if (terminal != NULL) {
4059 if (exec->state->type == XML_REGEXP_FINAL_STATE)
4060 *terminal = 1;
4061 else
4062 *terminal = 0;
4063 }
4064
4065 if (err) {
4066 if (exec->errState == NULL) return(-1);
4067 state = exec->errState;
4068 } else {
4069 if (exec->state == NULL) return(-1);
4070 state = exec->state;
4071 }
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00004072 for (transno = 0;
Daniel Veillardcc026dc2005-01-12 13:21:17 +00004073 (transno < state->nbTrans) && (nb < maxval);
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00004074 transno++) {
Daniel Veillardfc0b6f62005-01-09 17:48:02 +00004075 trans = &state->trans[transno];
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00004076 if (trans->to < 0)
4077 continue;
4078 atom = trans->atom;
4079 if ((atom == NULL) || (atom->valuep == NULL))
4080 continue;
4081 if (trans->count == REGEXP_ALL_LAX_COUNTER) {
Daniel Veillardcc026dc2005-01-12 13:21:17 +00004082 /* this should not be reached but ... */
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00004083 TODO;
4084 } else if (trans->count == REGEXP_ALL_COUNTER) {
Daniel Veillardcc026dc2005-01-12 13:21:17 +00004085 /* this should not be reached but ... */
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00004086 TODO;
4087 } else if (trans->counter >= 0) {
Daniel Veillard11ce4002006-03-10 00:36:23 +00004088 xmlRegCounterPtr counter = NULL;
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00004089 int count;
4090
Daniel Veillardfc0b6f62005-01-09 17:48:02 +00004091 if (err)
4092 count = exec->errCounts[trans->counter];
4093 else
4094 count = exec->counts[trans->counter];
Daniel Veillard11ce4002006-03-10 00:36:23 +00004095 if (exec->comp != NULL)
4096 counter = &exec->comp->counters[trans->counter];
4097 if ((counter == NULL) || (count < counter->max)) {
Daniel Veillard77005e62005-07-19 16:26:18 +00004098 if (atom->neg)
4099 values[nb++] = (xmlChar *) atom->valuep2;
4100 else
4101 values[nb++] = (xmlChar *) atom->valuep;
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00004102 (*nbval)++;
4103 }
4104 } else {
Daniel Veillardcc026dc2005-01-12 13:21:17 +00004105 if ((exec->comp->states[trans->to] != NULL) &&
4106 (exec->comp->states[trans->to]->type !=
4107 XML_REGEXP_SINK_STATE)) {
Daniel Veillard77005e62005-07-19 16:26:18 +00004108 if (atom->neg)
4109 values[nb++] = (xmlChar *) atom->valuep2;
4110 else
4111 values[nb++] = (xmlChar *) atom->valuep;
Daniel Veillardcc026dc2005-01-12 13:21:17 +00004112 (*nbval)++;
4113 }
4114 }
4115 }
4116 for (transno = 0;
4117 (transno < state->nbTrans) && (nb < maxval);
4118 transno++) {
4119 trans = &state->trans[transno];
4120 if (trans->to < 0)
4121 continue;
4122 atom = trans->atom;
4123 if ((atom == NULL) || (atom->valuep == NULL))
4124 continue;
4125 if (trans->count == REGEXP_ALL_LAX_COUNTER) {
4126 continue;
4127 } else if (trans->count == REGEXP_ALL_COUNTER) {
4128 continue;
4129 } else if (trans->counter >= 0) {
4130 continue;
4131 } else {
4132 if ((exec->comp->states[trans->to] != NULL) &&
4133 (exec->comp->states[trans->to]->type ==
4134 XML_REGEXP_SINK_STATE)) {
Daniel Veillard77005e62005-07-19 16:26:18 +00004135 if (atom->neg)
4136 values[nb++] = (xmlChar *) atom->valuep2;
4137 else
4138 values[nb++] = (xmlChar *) atom->valuep;
Daniel Veillardcc026dc2005-01-12 13:21:17 +00004139 (*nbneg)++;
4140 }
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00004141 }
4142 }
4143 }
4144 return(0);
4145}
4146
Daniel Veillardfc0b6f62005-01-09 17:48:02 +00004147/**
4148 * xmlRegExecNextValues:
4149 * @exec: a regexp execution context
4150 * @nbval: pointer to the number of accepted values IN/OUT
Daniel Veillardcc026dc2005-01-12 13:21:17 +00004151 * @nbneg: return number of negative transitions
Daniel Veillardfc0b6f62005-01-09 17:48:02 +00004152 * @values: pointer to the array of acceptable values
4153 * @terminal: return value if this was a terminal state
4154 *
4155 * Extract informations from the regexp execution,
4156 * the parameter @values must point to an array of @nbval string pointers
4157 * on return nbval will contain the number of possible strings in that
4158 * state and the @values array will be updated with them. The string values
4159 * returned will be freed with the @exec context and don't need to be
4160 * deallocated.
4161 *
4162 * Returns: 0 in case of success or -1 in case of error.
4163 */
4164int
Daniel Veillardcc026dc2005-01-12 13:21:17 +00004165xmlRegExecNextValues(xmlRegExecCtxtPtr exec, int *nbval, int *nbneg,
4166 xmlChar **values, int *terminal) {
4167 return(xmlRegExecGetValues(exec, 0, nbval, nbneg, values, terminal));
Daniel Veillardfc0b6f62005-01-09 17:48:02 +00004168}
4169
4170/**
4171 * xmlRegExecErrInfo:
4172 * @exec: a regexp execution context generating an error
4173 * @string: return value for the error string
4174 * @nbval: pointer to the number of accepted values IN/OUT
Daniel Veillardcc026dc2005-01-12 13:21:17 +00004175 * @nbneg: return number of negative transitions
Daniel Veillardfc0b6f62005-01-09 17:48:02 +00004176 * @values: pointer to the array of acceptable values
4177 * @terminal: return value if this was a terminal state
4178 *
4179 * Extract error informations from the regexp execution, the parameter
4180 * @string will be updated with the value pushed and not accepted,
4181 * the parameter @values must point to an array of @nbval string pointers
4182 * on return nbval will contain the number of possible strings in that
4183 * state and the @values array will be updated with them. The string values
4184 * returned will be freed with the @exec context and don't need to be
4185 * deallocated.
4186 *
4187 * Returns: 0 in case of success or -1 in case of error.
4188 */
4189int
4190xmlRegExecErrInfo(xmlRegExecCtxtPtr exec, const xmlChar **string,
Daniel Veillardcc026dc2005-01-12 13:21:17 +00004191 int *nbval, int *nbneg, xmlChar **values, int *terminal) {
Daniel Veillardfc0b6f62005-01-09 17:48:02 +00004192 if (exec == NULL)
4193 return(-1);
4194 if (string != NULL) {
4195 if (exec->status != 0)
4196 *string = exec->errString;
4197 else
4198 *string = NULL;
4199 }
Daniel Veillardcc026dc2005-01-12 13:21:17 +00004200 return(xmlRegExecGetValues(exec, 1, nbval, nbneg, values, terminal));
Daniel Veillardfc0b6f62005-01-09 17:48:02 +00004201}
4202
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00004203#ifdef DEBUG_ERR
4204static void testerr(xmlRegExecCtxtPtr exec) {
4205 const xmlChar *string;
Daniel Veillardcee2b3a2005-01-25 00:22:52 +00004206 xmlChar *values[5];
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00004207 int nb = 5;
Daniel Veillardcc026dc2005-01-12 13:21:17 +00004208 int nbneg;
Daniel Veillardfc0b6f62005-01-09 17:48:02 +00004209 int terminal;
Daniel Veillardcc026dc2005-01-12 13:21:17 +00004210 xmlRegExecErrInfo(exec, &string, &nb, &nbneg, &values[0], &terminal);
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00004211}
4212#endif
4213
Daniel Veillard4255d502002-04-16 15:50:10 +00004214#if 0
4215static int
4216xmlRegExecPushChar(xmlRegExecCtxtPtr exec, int UCS) {
4217 xmlRegTransPtr trans;
4218 xmlRegAtomPtr atom;
4219 int ret;
4220 int codepoint, len;
4221
4222 if (exec == NULL)
4223 return(-1);
4224 if (exec->status != 0)
4225 return(exec->status);
4226
4227 while ((exec->status == 0) &&
4228 ((exec->inputString[exec->index] != 0) ||
4229 (exec->state->type != XML_REGEXP_FINAL_STATE))) {
4230
4231 /*
4232 * End of input on non-terminal state, rollback, however we may
4233 * still have epsilon like transition for counted transitions
4234 * on counters, in that case don't break too early.
4235 */
4236 if ((exec->inputString[exec->index] == 0) && (exec->counts == NULL))
4237 goto rollback;
4238
4239 exec->transcount = 0;
4240 for (;exec->transno < exec->state->nbTrans;exec->transno++) {
4241 trans = &exec->state->trans[exec->transno];
4242 if (trans->to < 0)
4243 continue;
4244 atom = trans->atom;
4245 ret = 0;
4246 if (trans->count >= 0) {
4247 int count;
4248 xmlRegCounterPtr counter;
4249
4250 /*
4251 * A counted transition.
4252 */
4253
4254 count = exec->counts[trans->count];
4255 counter = &exec->comp->counters[trans->count];
4256#ifdef DEBUG_REGEXP_EXEC
4257 printf("testing count %d: val %d, min %d, max %d\n",
4258 trans->count, count, counter->min, counter->max);
4259#endif
4260 ret = ((count >= counter->min) && (count <= counter->max));
4261 } else if (atom == NULL) {
4262 fprintf(stderr, "epsilon transition left at runtime\n");
4263 exec->status = -2;
4264 break;
4265 } else if (exec->inputString[exec->index] != 0) {
4266 codepoint = CUR_SCHAR(&(exec->inputString[exec->index]), len);
4267 ret = xmlRegCheckCharacter(atom, codepoint);
4268 if ((ret == 1) && (atom->min > 0) && (atom->max > 0)) {
4269 xmlRegStatePtr to = exec->comp->states[trans->to];
4270
4271 /*
4272 * this is a multiple input sequence
4273 */
4274 if (exec->state->nbTrans > exec->transno + 1) {
4275 xmlFARegExecSave(exec);
4276 }
4277 exec->transcount = 1;
4278 do {
4279 /*
4280 * Try to progress as much as possible on the input
4281 */
4282 if (exec->transcount == atom->max) {
4283 break;
4284 }
4285 exec->index += len;
4286 /*
4287 * End of input: stop here
4288 */
4289 if (exec->inputString[exec->index] == 0) {
4290 exec->index -= len;
4291 break;
4292 }
4293 if (exec->transcount >= atom->min) {
4294 int transno = exec->transno;
4295 xmlRegStatePtr state = exec->state;
4296
4297 /*
4298 * The transition is acceptable save it
4299 */
4300 exec->transno = -1; /* trick */
4301 exec->state = to;
4302 xmlFARegExecSave(exec);
4303 exec->transno = transno;
4304 exec->state = state;
4305 }
4306 codepoint = CUR_SCHAR(&(exec->inputString[exec->index]),
4307 len);
4308 ret = xmlRegCheckCharacter(atom, codepoint);
4309 exec->transcount++;
4310 } while (ret == 1);
4311 if (exec->transcount < atom->min)
4312 ret = 0;
4313
4314 /*
4315 * If the last check failed but one transition was found
4316 * possible, rollback
4317 */
4318 if (ret < 0)
4319 ret = 0;
4320 if (ret == 0) {
4321 goto rollback;
4322 }
4323 }
4324 }
4325 if (ret == 1) {
4326 if (exec->state->nbTrans > exec->transno + 1) {
4327 xmlFARegExecSave(exec);
4328 }
Daniel Veillard54eb0242006-03-21 23:17:57 +00004329 /*
4330 * restart count for expressions like this ((abc){2})*
4331 */
4332 if (trans->count >= 0) {
4333#ifdef DEBUG_REGEXP_EXEC
4334 printf("Reset count %d\n", trans->count);
4335#endif
4336 exec->counts[trans->count] = 0;
4337 }
Daniel Veillard4255d502002-04-16 15:50:10 +00004338 if (trans->counter >= 0) {
4339#ifdef DEBUG_REGEXP_EXEC
4340 printf("Increasing count %d\n", trans->counter);
4341#endif
4342 exec->counts[trans->counter]++;
4343 }
4344#ifdef DEBUG_REGEXP_EXEC
4345 printf("entering state %d\n", trans->to);
4346#endif
4347 exec->state = exec->comp->states[trans->to];
4348 exec->transno = 0;
4349 if (trans->atom != NULL) {
4350 exec->index += len;
4351 }
4352 goto progress;
4353 } else if (ret < 0) {
4354 exec->status = -4;
4355 break;
4356 }
4357 }
4358 if ((exec->transno != 0) || (exec->state->nbTrans == 0)) {
4359rollback:
4360 /*
4361 * Failed to find a way out
4362 */
4363 exec->determinist = 0;
4364 xmlFARegExecRollBack(exec);
4365 }
4366progress:
4367 continue;
4368 }
4369}
4370#endif
4371/************************************************************************
4372 * *
William M. Brackddf71d62004-05-06 04:17:26 +00004373 * Parser for the Schemas Datatype Regular Expressions *
Daniel Veillard4255d502002-04-16 15:50:10 +00004374 * http://www.w3.org/TR/2001/REC-xmlschema-2-20010502/#regexs *
4375 * *
4376 ************************************************************************/
4377
4378/**
4379 * xmlFAIsChar:
Daniel Veillard441bc322002-04-20 17:38:48 +00004380 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00004381 *
4382 * [10] Char ::= [^.\?*+()|#x5B#x5D]
4383 */
4384static int
4385xmlFAIsChar(xmlRegParserCtxtPtr ctxt) {
4386 int cur;
4387 int len;
4388
4389 cur = CUR_SCHAR(ctxt->cur, len);
4390 if ((cur == '.') || (cur == '\\') || (cur == '?') ||
4391 (cur == '*') || (cur == '+') || (cur == '(') ||
4392 (cur == ')') || (cur == '|') || (cur == 0x5B) ||
4393 (cur == 0x5D) || (cur == 0))
4394 return(-1);
4395 return(cur);
4396}
4397
4398/**
4399 * xmlFAParseCharProp:
Daniel Veillard441bc322002-04-20 17:38:48 +00004400 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00004401 *
4402 * [27] charProp ::= IsCategory | IsBlock
4403 * [28] IsCategory ::= Letters | Marks | Numbers | Punctuation |
4404 * Separators | Symbols | Others
4405 * [29] Letters ::= 'L' [ultmo]?
4406 * [30] Marks ::= 'M' [nce]?
4407 * [31] Numbers ::= 'N' [dlo]?
4408 * [32] Punctuation ::= 'P' [cdseifo]?
4409 * [33] Separators ::= 'Z' [slp]?
4410 * [34] Symbols ::= 'S' [mcko]?
4411 * [35] Others ::= 'C' [cfon]?
4412 * [36] IsBlock ::= 'Is' [a-zA-Z0-9#x2D]+
4413 */
4414static void
4415xmlFAParseCharProp(xmlRegParserCtxtPtr ctxt) {
4416 int cur;
William M. Brack779af002003-08-01 15:55:39 +00004417 xmlRegAtomType type = (xmlRegAtomType) 0;
Daniel Veillard4255d502002-04-16 15:50:10 +00004418 xmlChar *blockName = NULL;
4419
4420 cur = CUR;
4421 if (cur == 'L') {
4422 NEXT;
4423 cur = CUR;
4424 if (cur == 'u') {
4425 NEXT;
4426 type = XML_REGEXP_LETTER_UPPERCASE;
4427 } else if (cur == 'l') {
4428 NEXT;
4429 type = XML_REGEXP_LETTER_LOWERCASE;
4430 } else if (cur == 't') {
4431 NEXT;
4432 type = XML_REGEXP_LETTER_TITLECASE;
4433 } else if (cur == 'm') {
4434 NEXT;
4435 type = XML_REGEXP_LETTER_MODIFIER;
4436 } else if (cur == 'o') {
4437 NEXT;
4438 type = XML_REGEXP_LETTER_OTHERS;
4439 } else {
4440 type = XML_REGEXP_LETTER;
4441 }
4442 } else if (cur == 'M') {
4443 NEXT;
4444 cur = CUR;
4445 if (cur == 'n') {
4446 NEXT;
4447 /* nonspacing */
4448 type = XML_REGEXP_MARK_NONSPACING;
4449 } else if (cur == 'c') {
4450 NEXT;
4451 /* spacing combining */
4452 type = XML_REGEXP_MARK_SPACECOMBINING;
4453 } else if (cur == 'e') {
4454 NEXT;
4455 /* enclosing */
4456 type = XML_REGEXP_MARK_ENCLOSING;
4457 } else {
4458 /* all marks */
4459 type = XML_REGEXP_MARK;
4460 }
4461 } else if (cur == 'N') {
4462 NEXT;
4463 cur = CUR;
4464 if (cur == 'd') {
4465 NEXT;
4466 /* digital */
4467 type = XML_REGEXP_NUMBER_DECIMAL;
4468 } else if (cur == 'l') {
4469 NEXT;
4470 /* letter */
4471 type = XML_REGEXP_NUMBER_LETTER;
4472 } else if (cur == 'o') {
4473 NEXT;
4474 /* other */
4475 type = XML_REGEXP_NUMBER_OTHERS;
4476 } else {
4477 /* all numbers */
4478 type = XML_REGEXP_NUMBER;
4479 }
4480 } else if (cur == 'P') {
4481 NEXT;
4482 cur = CUR;
4483 if (cur == 'c') {
4484 NEXT;
4485 /* connector */
4486 type = XML_REGEXP_PUNCT_CONNECTOR;
4487 } else if (cur == 'd') {
4488 NEXT;
4489 /* dash */
4490 type = XML_REGEXP_PUNCT_DASH;
4491 } else if (cur == 's') {
4492 NEXT;
4493 /* open */
4494 type = XML_REGEXP_PUNCT_OPEN;
4495 } else if (cur == 'e') {
4496 NEXT;
4497 /* close */
4498 type = XML_REGEXP_PUNCT_CLOSE;
4499 } else if (cur == 'i') {
4500 NEXT;
4501 /* initial quote */
4502 type = XML_REGEXP_PUNCT_INITQUOTE;
4503 } else if (cur == 'f') {
4504 NEXT;
4505 /* final quote */
4506 type = XML_REGEXP_PUNCT_FINQUOTE;
4507 } else if (cur == 'o') {
4508 NEXT;
4509 /* other */
4510 type = XML_REGEXP_PUNCT_OTHERS;
4511 } else {
4512 /* all punctuation */
4513 type = XML_REGEXP_PUNCT;
4514 }
4515 } else if (cur == 'Z') {
4516 NEXT;
4517 cur = CUR;
4518 if (cur == 's') {
4519 NEXT;
4520 /* space */
4521 type = XML_REGEXP_SEPAR_SPACE;
4522 } else if (cur == 'l') {
4523 NEXT;
4524 /* line */
4525 type = XML_REGEXP_SEPAR_LINE;
4526 } else if (cur == 'p') {
4527 NEXT;
4528 /* paragraph */
4529 type = XML_REGEXP_SEPAR_PARA;
4530 } else {
4531 /* all separators */
4532 type = XML_REGEXP_SEPAR;
4533 }
4534 } else if (cur == 'S') {
4535 NEXT;
4536 cur = CUR;
4537 if (cur == 'm') {
4538 NEXT;
4539 type = XML_REGEXP_SYMBOL_MATH;
4540 /* math */
4541 } else if (cur == 'c') {
4542 NEXT;
4543 type = XML_REGEXP_SYMBOL_CURRENCY;
4544 /* currency */
4545 } else if (cur == 'k') {
4546 NEXT;
4547 type = XML_REGEXP_SYMBOL_MODIFIER;
4548 /* modifiers */
4549 } else if (cur == 'o') {
4550 NEXT;
4551 type = XML_REGEXP_SYMBOL_OTHERS;
4552 /* other */
4553 } else {
4554 /* all symbols */
4555 type = XML_REGEXP_SYMBOL;
4556 }
4557 } else if (cur == 'C') {
4558 NEXT;
4559 cur = CUR;
4560 if (cur == 'c') {
4561 NEXT;
4562 /* control */
4563 type = XML_REGEXP_OTHER_CONTROL;
4564 } else if (cur == 'f') {
4565 NEXT;
4566 /* format */
4567 type = XML_REGEXP_OTHER_FORMAT;
4568 } else if (cur == 'o') {
4569 NEXT;
4570 /* private use */
4571 type = XML_REGEXP_OTHER_PRIVATE;
4572 } else if (cur == 'n') {
4573 NEXT;
4574 /* not assigned */
4575 type = XML_REGEXP_OTHER_NA;
4576 } else {
4577 /* all others */
4578 type = XML_REGEXP_OTHER;
4579 }
4580 } else if (cur == 'I') {
4581 const xmlChar *start;
4582 NEXT;
4583 cur = CUR;
4584 if (cur != 's') {
4585 ERROR("IsXXXX expected");
4586 return;
4587 }
4588 NEXT;
4589 start = ctxt->cur;
4590 cur = CUR;
4591 if (((cur >= 'a') && (cur <= 'z')) ||
4592 ((cur >= 'A') && (cur <= 'Z')) ||
4593 ((cur >= '0') && (cur <= '9')) ||
4594 (cur == 0x2D)) {
4595 NEXT;
4596 cur = CUR;
4597 while (((cur >= 'a') && (cur <= 'z')) ||
4598 ((cur >= 'A') && (cur <= 'Z')) ||
4599 ((cur >= '0') && (cur <= '9')) ||
4600 (cur == 0x2D)) {
4601 NEXT;
4602 cur = CUR;
4603 }
4604 }
4605 type = XML_REGEXP_BLOCK_NAME;
4606 blockName = xmlStrndup(start, ctxt->cur - start);
4607 } else {
4608 ERROR("Unknown char property");
4609 return;
4610 }
4611 if (ctxt->atom == NULL) {
4612 ctxt->atom = xmlRegNewAtom(ctxt, type);
4613 if (ctxt->atom != NULL)
4614 ctxt->atom->valuep = blockName;
4615 } else if (ctxt->atom->type == XML_REGEXP_RANGES) {
4616 xmlRegAtomAddRange(ctxt, ctxt->atom, ctxt->neg,
4617 type, 0, 0, blockName);
4618 }
4619}
4620
4621/**
4622 * xmlFAParseCharClassEsc:
Daniel Veillard441bc322002-04-20 17:38:48 +00004623 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00004624 *
4625 * [23] charClassEsc ::= ( SingleCharEsc | MultiCharEsc | catEsc | complEsc )
4626 * [24] SingleCharEsc ::= '\' [nrt\|.?*+(){}#x2D#x5B#x5D#x5E]
4627 * [25] catEsc ::= '\p{' charProp '}'
4628 * [26] complEsc ::= '\P{' charProp '}'
4629 * [37] MultiCharEsc ::= '.' | ('\' [sSiIcCdDwW])
4630 */
4631static void
4632xmlFAParseCharClassEsc(xmlRegParserCtxtPtr ctxt) {
4633 int cur;
4634
4635 if (CUR == '.') {
4636 if (ctxt->atom == NULL) {
4637 ctxt->atom = xmlRegNewAtom(ctxt, XML_REGEXP_ANYCHAR);
4638 } else if (ctxt->atom->type == XML_REGEXP_RANGES) {
4639 xmlRegAtomAddRange(ctxt, ctxt->atom, ctxt->neg,
4640 XML_REGEXP_ANYCHAR, 0, 0, NULL);
4641 }
4642 NEXT;
4643 return;
4644 }
4645 if (CUR != '\\') {
4646 ERROR("Escaped sequence: expecting \\");
4647 return;
4648 }
4649 NEXT;
4650 cur = CUR;
4651 if (cur == 'p') {
4652 NEXT;
4653 if (CUR != '{') {
4654 ERROR("Expecting '{'");
4655 return;
4656 }
4657 NEXT;
4658 xmlFAParseCharProp(ctxt);
4659 if (CUR != '}') {
4660 ERROR("Expecting '}'");
4661 return;
4662 }
4663 NEXT;
4664 } else if (cur == 'P') {
4665 NEXT;
4666 if (CUR != '{') {
4667 ERROR("Expecting '{'");
4668 return;
4669 }
4670 NEXT;
4671 xmlFAParseCharProp(ctxt);
4672 ctxt->atom->neg = 1;
4673 if (CUR != '}') {
4674 ERROR("Expecting '}'");
4675 return;
4676 }
4677 NEXT;
4678 } else if ((cur == 'n') || (cur == 'r') || (cur == 't') || (cur == '\\') ||
4679 (cur == '|') || (cur == '.') || (cur == '?') || (cur == '*') ||
4680 (cur == '+') || (cur == '(') || (cur == ')') || (cur == '{') ||
4681 (cur == '}') || (cur == 0x2D) || (cur == 0x5B) || (cur == 0x5D) ||
4682 (cur == 0x5E)) {
4683 if (ctxt->atom == NULL) {
4684 ctxt->atom = xmlRegNewAtom(ctxt, XML_REGEXP_CHARVAL);
Daniel Veillard99c394d2005-07-14 12:58:49 +00004685 if (ctxt->atom != NULL) {
4686 switch (cur) {
4687 case 'n':
4688 ctxt->atom->codepoint = '\n';
4689 break;
4690 case 'r':
4691 ctxt->atom->codepoint = '\r';
4692 break;
4693 case 't':
4694 ctxt->atom->codepoint = '\t';
4695 break;
4696 default:
4697 ctxt->atom->codepoint = cur;
4698 }
4699 }
Daniel Veillard4255d502002-04-16 15:50:10 +00004700 } else if (ctxt->atom->type == XML_REGEXP_RANGES) {
4701 xmlRegAtomAddRange(ctxt, ctxt->atom, ctxt->neg,
4702 XML_REGEXP_CHARVAL, cur, cur, NULL);
4703 }
4704 NEXT;
4705 } else if ((cur == 's') || (cur == 'S') || (cur == 'i') || (cur == 'I') ||
4706 (cur == 'c') || (cur == 'C') || (cur == 'd') || (cur == 'D') ||
4707 (cur == 'w') || (cur == 'W')) {
Daniel Veillardb509f152002-04-17 16:28:10 +00004708 xmlRegAtomType type = XML_REGEXP_ANYSPACE;
Daniel Veillard4255d502002-04-16 15:50:10 +00004709
4710 switch (cur) {
4711 case 's':
4712 type = XML_REGEXP_ANYSPACE;
4713 break;
4714 case 'S':
4715 type = XML_REGEXP_NOTSPACE;
4716 break;
4717 case 'i':
4718 type = XML_REGEXP_INITNAME;
4719 break;
4720 case 'I':
4721 type = XML_REGEXP_NOTINITNAME;
4722 break;
4723 case 'c':
4724 type = XML_REGEXP_NAMECHAR;
4725 break;
4726 case 'C':
4727 type = XML_REGEXP_NOTNAMECHAR;
4728 break;
4729 case 'd':
4730 type = XML_REGEXP_DECIMAL;
4731 break;
4732 case 'D':
4733 type = XML_REGEXP_NOTDECIMAL;
4734 break;
4735 case 'w':
4736 type = XML_REGEXP_REALCHAR;
4737 break;
4738 case 'W':
4739 type = XML_REGEXP_NOTREALCHAR;
4740 break;
4741 }
4742 NEXT;
4743 if (ctxt->atom == NULL) {
4744 ctxt->atom = xmlRegNewAtom(ctxt, type);
4745 } else if (ctxt->atom->type == XML_REGEXP_RANGES) {
4746 xmlRegAtomAddRange(ctxt, ctxt->atom, ctxt->neg,
4747 type, 0, 0, NULL);
4748 }
4749 }
4750}
4751
4752/**
4753 * xmlFAParseCharRef:
Daniel Veillard441bc322002-04-20 17:38:48 +00004754 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00004755 *
4756 * [19] XmlCharRef ::= ( '&#' [0-9]+ ';' ) | (' &#x' [0-9a-fA-F]+ ';' )
4757 */
4758static int
4759xmlFAParseCharRef(xmlRegParserCtxtPtr ctxt) {
4760 int ret = 0, cur;
4761
4762 if ((CUR != '&') || (NXT(1) != '#'))
4763 return(-1);
4764 NEXT;
4765 NEXT;
4766 cur = CUR;
4767 if (cur == 'x') {
4768 NEXT;
4769 cur = CUR;
4770 if (((cur >= '0') && (cur <= '9')) ||
4771 ((cur >= 'a') && (cur <= 'f')) ||
4772 ((cur >= 'A') && (cur <= 'F'))) {
4773 while (((cur >= '0') && (cur <= '9')) ||
Daniel Veillard11ce4002006-03-10 00:36:23 +00004774 ((cur >= 'a') && (cur <= 'f')) ||
Daniel Veillard4255d502002-04-16 15:50:10 +00004775 ((cur >= 'A') && (cur <= 'F'))) {
4776 if ((cur >= '0') && (cur <= '9'))
4777 ret = ret * 16 + cur - '0';
4778 else if ((cur >= 'a') && (cur <= 'f'))
4779 ret = ret * 16 + 10 + (cur - 'a');
4780 else
4781 ret = ret * 16 + 10 + (cur - 'A');
4782 NEXT;
4783 cur = CUR;
4784 }
4785 } else {
4786 ERROR("Char ref: expecting [0-9A-F]");
4787 return(-1);
4788 }
4789 } else {
4790 if ((cur >= '0') && (cur <= '9')) {
4791 while ((cur >= '0') && (cur <= '9')) {
4792 ret = ret * 10 + cur - '0';
4793 NEXT;
4794 cur = CUR;
4795 }
4796 } else {
4797 ERROR("Char ref: expecting [0-9]");
4798 return(-1);
4799 }
4800 }
4801 if (cur != ';') {
4802 ERROR("Char ref: expecting ';'");
4803 return(-1);
4804 } else {
4805 NEXT;
4806 }
4807 return(ret);
4808}
4809
4810/**
4811 * xmlFAParseCharRange:
Daniel Veillard441bc322002-04-20 17:38:48 +00004812 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00004813 *
4814 * [17] charRange ::= seRange | XmlCharRef | XmlCharIncDash
4815 * [18] seRange ::= charOrEsc '-' charOrEsc
4816 * [20] charOrEsc ::= XmlChar | SingleCharEsc
4817 * [21] XmlChar ::= [^\#x2D#x5B#x5D]
4818 * [22] XmlCharIncDash ::= [^\#x5B#x5D]
4819 */
4820static void
4821xmlFAParseCharRange(xmlRegParserCtxtPtr ctxt) {
William M. Brackdc99df92003-12-27 01:54:25 +00004822 int cur, len;
Daniel Veillard4255d502002-04-16 15:50:10 +00004823 int start = -1;
4824 int end = -1;
4825
Daniel Veillard777737e2006-10-17 21:23:17 +00004826 if (CUR == '\0') {
4827 ERROR("Expecting ']'");
4828 return;
4829 }
4830
Daniel Veillard4255d502002-04-16 15:50:10 +00004831 if ((CUR == '&') && (NXT(1) == '#')) {
4832 end = start = xmlFAParseCharRef(ctxt);
4833 xmlRegAtomAddRange(ctxt, ctxt->atom, ctxt->neg,
4834 XML_REGEXP_CHARVAL, start, end, NULL);
4835 return;
4836 }
4837 cur = CUR;
4838 if (cur == '\\') {
4839 NEXT;
4840 cur = CUR;
4841 switch (cur) {
4842 case 'n': start = 0xA; break;
4843 case 'r': start = 0xD; break;
4844 case 't': start = 0x9; break;
4845 case '\\': case '|': case '.': case '-': case '^': case '?':
4846 case '*': case '+': case '{': case '}': case '(': case ')':
4847 case '[': case ']':
4848 start = cur; break;
4849 default:
4850 ERROR("Invalid escape value");
4851 return;
4852 }
4853 end = start;
William M. Brackdc99df92003-12-27 01:54:25 +00004854 len = 1;
Daniel Veillard4255d502002-04-16 15:50:10 +00004855 } else if ((cur != 0x5B) && (cur != 0x5D)) {
William M. Brackdc99df92003-12-27 01:54:25 +00004856 end = start = CUR_SCHAR(ctxt->cur, len);
Daniel Veillard4255d502002-04-16 15:50:10 +00004857 } else {
4858 ERROR("Expecting a char range");
4859 return;
4860 }
William M. Bracka9cbf282007-03-21 13:16:33 +00004861 /*
4862 * Since we are "inside" a range, we can assume ctxt->cur is past
4863 * the start of ctxt->string, and PREV should be safe
4864 */
4865 if ((start == '-') && (NXT(1) != ']') && (PREV != '[') && (PREV != '^')) {
4866 NEXTL(len);
Daniel Veillard4255d502002-04-16 15:50:10 +00004867 return;
4868 }
William M. Bracka9cbf282007-03-21 13:16:33 +00004869 NEXTL(len);
Daniel Veillard4255d502002-04-16 15:50:10 +00004870 cur = CUR;
William M. Brack10f1ef42004-03-20 14:51:25 +00004871 if ((cur != '-') || (NXT(1) == ']')) {
Daniel Veillard4255d502002-04-16 15:50:10 +00004872 xmlRegAtomAddRange(ctxt, ctxt->atom, ctxt->neg,
4873 XML_REGEXP_CHARVAL, start, end, NULL);
4874 return;
4875 }
4876 NEXT;
4877 cur = CUR;
4878 if (cur == '\\') {
4879 NEXT;
4880 cur = CUR;
4881 switch (cur) {
4882 case 'n': end = 0xA; break;
4883 case 'r': end = 0xD; break;
4884 case 't': end = 0x9; break;
4885 case '\\': case '|': case '.': case '-': case '^': case '?':
4886 case '*': case '+': case '{': case '}': case '(': case ')':
4887 case '[': case ']':
4888 end = cur; break;
4889 default:
4890 ERROR("Invalid escape value");
4891 return;
4892 }
William M. Brackdc99df92003-12-27 01:54:25 +00004893 len = 1;
Daniel Veillard4255d502002-04-16 15:50:10 +00004894 } else if ((cur != 0x5B) && (cur != 0x5D)) {
William M. Brackdc99df92003-12-27 01:54:25 +00004895 end = CUR_SCHAR(ctxt->cur, len);
Daniel Veillard4255d502002-04-16 15:50:10 +00004896 } else {
4897 ERROR("Expecting the end of a char range");
4898 return;
4899 }
William M. Brackdc99df92003-12-27 01:54:25 +00004900 NEXTL(len);
Daniel Veillard4255d502002-04-16 15:50:10 +00004901 /* TODO check that the values are acceptable character ranges for XML */
4902 if (end < start) {
4903 ERROR("End of range is before start of range");
4904 } else {
4905 xmlRegAtomAddRange(ctxt, ctxt->atom, ctxt->neg,
4906 XML_REGEXP_CHARVAL, start, end, NULL);
4907 }
4908 return;
4909}
4910
4911/**
4912 * xmlFAParsePosCharGroup:
Daniel Veillard441bc322002-04-20 17:38:48 +00004913 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00004914 *
4915 * [14] posCharGroup ::= ( charRange | charClassEsc )+
4916 */
4917static void
4918xmlFAParsePosCharGroup(xmlRegParserCtxtPtr ctxt) {
4919 do {
4920 if ((CUR == '\\') || (CUR == '.')) {
4921 xmlFAParseCharClassEsc(ctxt);
4922 } else {
4923 xmlFAParseCharRange(ctxt);
4924 }
4925 } while ((CUR != ']') && (CUR != '^') && (CUR != '-') &&
Daniel Veillard777737e2006-10-17 21:23:17 +00004926 (CUR != 0) && (ctxt->error == 0));
Daniel Veillard4255d502002-04-16 15:50:10 +00004927}
4928
4929/**
4930 * xmlFAParseCharGroup:
Daniel Veillard441bc322002-04-20 17:38:48 +00004931 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00004932 *
4933 * [13] charGroup ::= posCharGroup | negCharGroup | charClassSub
4934 * [15] negCharGroup ::= '^' posCharGroup
4935 * [16] charClassSub ::= ( posCharGroup | negCharGroup ) '-' charClassExpr
4936 * [12] charClassExpr ::= '[' charGroup ']'
4937 */
4938static void
4939xmlFAParseCharGroup(xmlRegParserCtxtPtr ctxt) {
4940 int n = ctxt->neg;
4941 while ((CUR != ']') && (ctxt->error == 0)) {
4942 if (CUR == '^') {
4943 int neg = ctxt->neg;
4944
4945 NEXT;
4946 ctxt->neg = !ctxt->neg;
4947 xmlFAParsePosCharGroup(ctxt);
4948 ctxt->neg = neg;
William M. Brack10f1ef42004-03-20 14:51:25 +00004949 } else if ((CUR == '-') && (NXT(1) == '[')) {
Daniel Veillardf8b9de32003-11-24 14:27:26 +00004950 int neg = ctxt->neg;
Daniel Veillardf8b9de32003-11-24 14:27:26 +00004951 ctxt->neg = 2;
William M. Brack10f1ef42004-03-20 14:51:25 +00004952 NEXT; /* eat the '-' */
4953 NEXT; /* eat the '[' */
Daniel Veillard4255d502002-04-16 15:50:10 +00004954 xmlFAParseCharGroup(ctxt);
4955 if (CUR == ']') {
4956 NEXT;
4957 } else {
4958 ERROR("charClassExpr: ']' expected");
4959 break;
4960 }
Daniel Veillardf8b9de32003-11-24 14:27:26 +00004961 ctxt->neg = neg;
Daniel Veillard4255d502002-04-16 15:50:10 +00004962 break;
4963 } else if (CUR != ']') {
4964 xmlFAParsePosCharGroup(ctxt);
4965 }
4966 }
4967 ctxt->neg = n;
4968}
4969
4970/**
4971 * xmlFAParseCharClass:
Daniel Veillard441bc322002-04-20 17:38:48 +00004972 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00004973 *
4974 * [11] charClass ::= charClassEsc | charClassExpr
4975 * [12] charClassExpr ::= '[' charGroup ']'
4976 */
4977static void
4978xmlFAParseCharClass(xmlRegParserCtxtPtr ctxt) {
4979 if (CUR == '[') {
4980 NEXT;
4981 ctxt->atom = xmlRegNewAtom(ctxt, XML_REGEXP_RANGES);
4982 if (ctxt->atom == NULL)
4983 return;
4984 xmlFAParseCharGroup(ctxt);
4985 if (CUR == ']') {
4986 NEXT;
4987 } else {
4988 ERROR("xmlFAParseCharClass: ']' expected");
4989 }
4990 } else {
4991 xmlFAParseCharClassEsc(ctxt);
4992 }
4993}
4994
4995/**
4996 * xmlFAParseQuantExact:
Daniel Veillard441bc322002-04-20 17:38:48 +00004997 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00004998 *
4999 * [8] QuantExact ::= [0-9]+
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00005000 *
5001 * Returns 0 if success or -1 in case of error
Daniel Veillard4255d502002-04-16 15:50:10 +00005002 */
5003static int
5004xmlFAParseQuantExact(xmlRegParserCtxtPtr ctxt) {
5005 int ret = 0;
5006 int ok = 0;
5007
5008 while ((CUR >= '0') && (CUR <= '9')) {
5009 ret = ret * 10 + (CUR - '0');
5010 ok = 1;
5011 NEXT;
5012 }
5013 if (ok != 1) {
5014 return(-1);
5015 }
5016 return(ret);
5017}
5018
5019/**
5020 * xmlFAParseQuantifier:
Daniel Veillard441bc322002-04-20 17:38:48 +00005021 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00005022 *
5023 * [4] quantifier ::= [?*+] | ( '{' quantity '}' )
5024 * [5] quantity ::= quantRange | quantMin | QuantExact
5025 * [6] quantRange ::= QuantExact ',' QuantExact
5026 * [7] quantMin ::= QuantExact ','
5027 * [8] QuantExact ::= [0-9]+
5028 */
5029static int
5030xmlFAParseQuantifier(xmlRegParserCtxtPtr ctxt) {
5031 int cur;
5032
5033 cur = CUR;
5034 if ((cur == '?') || (cur == '*') || (cur == '+')) {
5035 if (ctxt->atom != NULL) {
5036 if (cur == '?')
5037 ctxt->atom->quant = XML_REGEXP_QUANT_OPT;
5038 else if (cur == '*')
5039 ctxt->atom->quant = XML_REGEXP_QUANT_MULT;
5040 else if (cur == '+')
5041 ctxt->atom->quant = XML_REGEXP_QUANT_PLUS;
5042 }
5043 NEXT;
5044 return(1);
5045 }
5046 if (cur == '{') {
5047 int min = 0, max = 0;
5048
5049 NEXT;
5050 cur = xmlFAParseQuantExact(ctxt);
5051 if (cur >= 0)
5052 min = cur;
5053 if (CUR == ',') {
5054 NEXT;
Daniel Veillardebe48c62003-12-03 12:12:27 +00005055 if (CUR == '}')
5056 max = INT_MAX;
5057 else {
5058 cur = xmlFAParseQuantExact(ctxt);
5059 if (cur >= 0)
5060 max = cur;
5061 else {
5062 ERROR("Improper quantifier");
5063 }
5064 }
Daniel Veillard4255d502002-04-16 15:50:10 +00005065 }
5066 if (CUR == '}') {
5067 NEXT;
5068 } else {
5069 ERROR("Unterminated quantifier");
5070 }
5071 if (max == 0)
5072 max = min;
5073 if (ctxt->atom != NULL) {
5074 ctxt->atom->quant = XML_REGEXP_QUANT_RANGE;
5075 ctxt->atom->min = min;
5076 ctxt->atom->max = max;
5077 }
5078 return(1);
5079 }
5080 return(0);
5081}
5082
5083/**
5084 * xmlFAParseAtom:
Daniel Veillard441bc322002-04-20 17:38:48 +00005085 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00005086 *
5087 * [9] atom ::= Char | charClass | ( '(' regExp ')' )
5088 */
5089static int
5090xmlFAParseAtom(xmlRegParserCtxtPtr ctxt) {
5091 int codepoint, len;
5092
5093 codepoint = xmlFAIsChar(ctxt);
5094 if (codepoint > 0) {
5095 ctxt->atom = xmlRegNewAtom(ctxt, XML_REGEXP_CHARVAL);
5096 if (ctxt->atom == NULL)
5097 return(-1);
5098 codepoint = CUR_SCHAR(ctxt->cur, len);
5099 ctxt->atom->codepoint = codepoint;
5100 NEXTL(len);
5101 return(1);
5102 } else if (CUR == '|') {
5103 return(0);
5104 } else if (CUR == 0) {
5105 return(0);
5106 } else if (CUR == ')') {
5107 return(0);
5108 } else if (CUR == '(') {
5109 xmlRegStatePtr start, oldend;
5110
5111 NEXT;
5112 xmlFAGenerateEpsilonTransition(ctxt, ctxt->state, NULL);
5113 start = ctxt->state;
5114 oldend = ctxt->end;
5115 ctxt->end = NULL;
5116 ctxt->atom = NULL;
5117 xmlFAParseRegExp(ctxt, 0);
5118 if (CUR == ')') {
5119 NEXT;
5120 } else {
5121 ERROR("xmlFAParseAtom: expecting ')'");
5122 }
5123 ctxt->atom = xmlRegNewAtom(ctxt, XML_REGEXP_SUBREG);
5124 if (ctxt->atom == NULL)
5125 return(-1);
5126 ctxt->atom->start = start;
5127 ctxt->atom->stop = ctxt->state;
5128 ctxt->end = oldend;
5129 return(1);
5130 } else if ((CUR == '[') || (CUR == '\\') || (CUR == '.')) {
5131 xmlFAParseCharClass(ctxt);
5132 return(1);
5133 }
5134 return(0);
5135}
5136
5137/**
5138 * xmlFAParsePiece:
Daniel Veillard441bc322002-04-20 17:38:48 +00005139 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00005140 *
5141 * [3] piece ::= atom quantifier?
5142 */
5143static int
5144xmlFAParsePiece(xmlRegParserCtxtPtr ctxt) {
5145 int ret;
5146
5147 ctxt->atom = NULL;
5148 ret = xmlFAParseAtom(ctxt);
5149 if (ret == 0)
5150 return(0);
5151 if (ctxt->atom == NULL) {
5152 ERROR("internal: no atom generated");
5153 }
5154 xmlFAParseQuantifier(ctxt);
5155 return(1);
5156}
5157
5158/**
5159 * xmlFAParseBranch:
Daniel Veillard441bc322002-04-20 17:38:48 +00005160 * @ctxt: a regexp parser context
Daniel Veillard54eb0242006-03-21 23:17:57 +00005161 * @to: optional target to the end of the branch
5162 *
5163 * @to is used to optimize by removing duplicate path in automata
5164 * in expressions like (a|b)(c|d)
Daniel Veillard4255d502002-04-16 15:50:10 +00005165 *
5166 * [2] branch ::= piece*
5167 */
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00005168static int
Daniel Veillard54eb0242006-03-21 23:17:57 +00005169xmlFAParseBranch(xmlRegParserCtxtPtr ctxt, xmlRegStatePtr to) {
Daniel Veillard4255d502002-04-16 15:50:10 +00005170 xmlRegStatePtr previous;
Daniel Veillard4255d502002-04-16 15:50:10 +00005171 int ret;
5172
5173 previous = ctxt->state;
5174 ret = xmlFAParsePiece(ctxt);
5175 if (ret != 0) {
Daniel Veillard54eb0242006-03-21 23:17:57 +00005176 if (xmlFAGenerateTransitions(ctxt, previous,
5177 (CUR=='|' || CUR==')') ? to : NULL, ctxt->atom) < 0)
Daniel Veillard2cbf5962004-03-31 15:50:43 +00005178 return(-1);
5179 previous = ctxt->state;
Daniel Veillard4255d502002-04-16 15:50:10 +00005180 ctxt->atom = NULL;
5181 }
5182 while ((ret != 0) && (ctxt->error == 0)) {
5183 ret = xmlFAParsePiece(ctxt);
5184 if (ret != 0) {
Daniel Veillard54eb0242006-03-21 23:17:57 +00005185 if (xmlFAGenerateTransitions(ctxt, previous,
5186 (CUR=='|' || CUR==')') ? to : NULL, ctxt->atom) < 0)
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00005187 return(-1);
Daniel Veillard4255d502002-04-16 15:50:10 +00005188 previous = ctxt->state;
5189 ctxt->atom = NULL;
5190 }
5191 }
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00005192 return(0);
Daniel Veillard4255d502002-04-16 15:50:10 +00005193}
5194
5195/**
5196 * xmlFAParseRegExp:
Daniel Veillard441bc322002-04-20 17:38:48 +00005197 * @ctxt: a regexp parser context
William M. Brackddf71d62004-05-06 04:17:26 +00005198 * @top: is this the top-level expression ?
Daniel Veillard4255d502002-04-16 15:50:10 +00005199 *
5200 * [1] regExp ::= branch ( '|' branch )*
5201 */
5202static void
5203xmlFAParseRegExp(xmlRegParserCtxtPtr ctxt, int top) {
Daniel Veillardc7e3cc42004-09-28 12:33:52 +00005204 xmlRegStatePtr start, end;
Daniel Veillard4255d502002-04-16 15:50:10 +00005205
Daniel Veillard2cbf5962004-03-31 15:50:43 +00005206 /* if not top start should have been generated by an epsilon trans */
Daniel Veillard4255d502002-04-16 15:50:10 +00005207 start = ctxt->state;
Daniel Veillard2cbf5962004-03-31 15:50:43 +00005208 ctxt->end = NULL;
Daniel Veillard54eb0242006-03-21 23:17:57 +00005209 xmlFAParseBranch(ctxt, NULL);
Daniel Veillard2cbf5962004-03-31 15:50:43 +00005210 if (top) {
5211#ifdef DEBUG_REGEXP_GRAPH
5212 printf("State %d is final\n", ctxt->state->no);
5213#endif
5214 ctxt->state->type = XML_REGEXP_FINAL_STATE;
5215 }
Daniel Veillard4255d502002-04-16 15:50:10 +00005216 if (CUR != '|') {
5217 ctxt->end = ctxt->state;
5218 return;
5219 }
5220 end = ctxt->state;
5221 while ((CUR == '|') && (ctxt->error == 0)) {
5222 NEXT;
5223 ctxt->state = start;
Daniel Veillard2cbf5962004-03-31 15:50:43 +00005224 ctxt->end = NULL;
Daniel Veillard54eb0242006-03-21 23:17:57 +00005225 xmlFAParseBranch(ctxt, end);
Daniel Veillard4255d502002-04-16 15:50:10 +00005226 }
Daniel Veillard2cbf5962004-03-31 15:50:43 +00005227 if (!top) {
5228 ctxt->state = end;
5229 ctxt->end = end;
5230 }
Daniel Veillard4255d502002-04-16 15:50:10 +00005231}
5232
5233/************************************************************************
5234 * *
5235 * The basic API *
5236 * *
5237 ************************************************************************/
5238
5239/**
5240 * xmlRegexpPrint:
5241 * @output: the file for the output debug
5242 * @regexp: the compiled regexp
5243 *
5244 * Print the content of the compiled regular expression
5245 */
5246void
5247xmlRegexpPrint(FILE *output, xmlRegexpPtr regexp) {
5248 int i;
5249
Daniel Veillarda82b1822004-11-08 16:24:57 +00005250 if (output == NULL)
5251 return;
Daniel Veillard4255d502002-04-16 15:50:10 +00005252 fprintf(output, " regexp: ");
5253 if (regexp == NULL) {
5254 fprintf(output, "NULL\n");
5255 return;
5256 }
5257 fprintf(output, "'%s' ", regexp->string);
5258 fprintf(output, "\n");
5259 fprintf(output, "%d atoms:\n", regexp->nbAtoms);
5260 for (i = 0;i < regexp->nbAtoms; i++) {
5261 fprintf(output, " %02d ", i);
5262 xmlRegPrintAtom(output, regexp->atoms[i]);
5263 }
5264 fprintf(output, "%d states:", regexp->nbStates);
5265 fprintf(output, "\n");
5266 for (i = 0;i < regexp->nbStates; i++) {
5267 xmlRegPrintState(output, regexp->states[i]);
5268 }
5269 fprintf(output, "%d counters:\n", regexp->nbCounters);
5270 for (i = 0;i < regexp->nbCounters; i++) {
5271 fprintf(output, " %d: min %d max %d\n", i, regexp->counters[i].min,
5272 regexp->counters[i].max);
5273 }
5274}
5275
5276/**
5277 * xmlRegexpCompile:
5278 * @regexp: a regular expression string
5279 *
5280 * Parses a regular expression conforming to XML Schemas Part 2 Datatype
William M. Brackddf71d62004-05-06 04:17:26 +00005281 * Appendix F and builds an automata suitable for testing strings against
Daniel Veillard4255d502002-04-16 15:50:10 +00005282 * that regular expression
5283 *
5284 * Returns the compiled expression or NULL in case of error
5285 */
5286xmlRegexpPtr
5287xmlRegexpCompile(const xmlChar *regexp) {
5288 xmlRegexpPtr ret;
5289 xmlRegParserCtxtPtr ctxt;
5290
5291 ctxt = xmlRegNewParserCtxt(regexp);
5292 if (ctxt == NULL)
5293 return(NULL);
5294
5295 /* initialize the parser */
5296 ctxt->end = NULL;
5297 ctxt->start = ctxt->state = xmlRegNewState(ctxt);
5298 xmlRegStatePush(ctxt, ctxt->start);
5299
5300 /* parse the expression building an automata */
5301 xmlFAParseRegExp(ctxt, 1);
5302 if (CUR != 0) {
5303 ERROR("xmlFAParseRegExp: extra characters");
5304 }
5305 ctxt->end = ctxt->state;
5306 ctxt->start->type = XML_REGEXP_START_STATE;
5307 ctxt->end->type = XML_REGEXP_FINAL_STATE;
5308
5309 /* remove the Epsilon except for counted transitions */
5310 xmlFAEliminateEpsilonTransitions(ctxt);
5311
5312
5313 if (ctxt->error != 0) {
5314 xmlRegFreeParserCtxt(ctxt);
5315 return(NULL);
5316 }
5317 ret = xmlRegEpxFromParse(ctxt);
5318 xmlRegFreeParserCtxt(ctxt);
5319 return(ret);
5320}
5321
5322/**
5323 * xmlRegexpExec:
5324 * @comp: the compiled regular expression
5325 * @content: the value to check against the regular expression
5326 *
William M. Brackddf71d62004-05-06 04:17:26 +00005327 * Check if the regular expression generates the value
Daniel Veillard4255d502002-04-16 15:50:10 +00005328 *
William M. Brackddf71d62004-05-06 04:17:26 +00005329 * Returns 1 if it matches, 0 if not and a negative value in case of error
Daniel Veillard4255d502002-04-16 15:50:10 +00005330 */
5331int
5332xmlRegexpExec(xmlRegexpPtr comp, const xmlChar *content) {
5333 if ((comp == NULL) || (content == NULL))
5334 return(-1);
5335 return(xmlFARegExec(comp, content));
5336}
5337
5338/**
Daniel Veillard23e73572002-09-19 19:56:43 +00005339 * xmlRegexpIsDeterminist:
5340 * @comp: the compiled regular expression
5341 *
5342 * Check if the regular expression is determinist
5343 *
William M. Brackddf71d62004-05-06 04:17:26 +00005344 * Returns 1 if it yes, 0 if not and a negative value in case of error
Daniel Veillard23e73572002-09-19 19:56:43 +00005345 */
5346int
5347xmlRegexpIsDeterminist(xmlRegexpPtr comp) {
5348 xmlAutomataPtr am;
5349 int ret;
5350
5351 if (comp == NULL)
5352 return(-1);
5353 if (comp->determinist != -1)
5354 return(comp->determinist);
5355
5356 am = xmlNewAutomata();
Daniel Veillardbd9afb52002-09-25 22:25:35 +00005357 if (am->states != NULL) {
5358 int i;
5359
5360 for (i = 0;i < am->nbStates;i++)
5361 xmlRegFreeState(am->states[i]);
5362 xmlFree(am->states);
5363 }
Daniel Veillard23e73572002-09-19 19:56:43 +00005364 am->nbAtoms = comp->nbAtoms;
5365 am->atoms = comp->atoms;
5366 am->nbStates = comp->nbStates;
5367 am->states = comp->states;
5368 am->determinist = -1;
5369 ret = xmlFAComputesDeterminism(am);
5370 am->atoms = NULL;
5371 am->states = NULL;
5372 xmlFreeAutomata(am);
5373 return(ret);
5374}
5375
5376/**
Daniel Veillard4255d502002-04-16 15:50:10 +00005377 * xmlRegFreeRegexp:
5378 * @regexp: the regexp
5379 *
5380 * Free a regexp
5381 */
5382void
5383xmlRegFreeRegexp(xmlRegexpPtr regexp) {
5384 int i;
5385 if (regexp == NULL)
5386 return;
5387
5388 if (regexp->string != NULL)
5389 xmlFree(regexp->string);
5390 if (regexp->states != NULL) {
5391 for (i = 0;i < regexp->nbStates;i++)
5392 xmlRegFreeState(regexp->states[i]);
5393 xmlFree(regexp->states);
5394 }
5395 if (regexp->atoms != NULL) {
5396 for (i = 0;i < regexp->nbAtoms;i++)
5397 xmlRegFreeAtom(regexp->atoms[i]);
5398 xmlFree(regexp->atoms);
5399 }
5400 if (regexp->counters != NULL)
5401 xmlFree(regexp->counters);
Daniel Veillard23e73572002-09-19 19:56:43 +00005402 if (regexp->compact != NULL)
5403 xmlFree(regexp->compact);
Daniel Veillard118aed72002-09-24 14:13:13 +00005404 if (regexp->transdata != NULL)
5405 xmlFree(regexp->transdata);
Daniel Veillard23e73572002-09-19 19:56:43 +00005406 if (regexp->stringMap != NULL) {
5407 for (i = 0; i < regexp->nbstrings;i++)
5408 xmlFree(regexp->stringMap[i]);
5409 xmlFree(regexp->stringMap);
5410 }
5411
Daniel Veillard4255d502002-04-16 15:50:10 +00005412 xmlFree(regexp);
5413}
5414
5415#ifdef LIBXML_AUTOMATA_ENABLED
5416/************************************************************************
5417 * *
5418 * The Automata interface *
5419 * *
5420 ************************************************************************/
5421
5422/**
5423 * xmlNewAutomata:
5424 *
5425 * Create a new automata
5426 *
5427 * Returns the new object or NULL in case of failure
5428 */
5429xmlAutomataPtr
5430xmlNewAutomata(void) {
5431 xmlAutomataPtr ctxt;
5432
5433 ctxt = xmlRegNewParserCtxt(NULL);
5434 if (ctxt == NULL)
5435 return(NULL);
5436
5437 /* initialize the parser */
5438 ctxt->end = NULL;
5439 ctxt->start = ctxt->state = xmlRegNewState(ctxt);
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00005440 if (ctxt->start == NULL) {
5441 xmlFreeAutomata(ctxt);
5442 return(NULL);
5443 }
Daniel Veillardd0271472006-01-02 10:22:02 +00005444 ctxt->start->type = XML_REGEXP_START_STATE;
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00005445 if (xmlRegStatePush(ctxt, ctxt->start) < 0) {
5446 xmlRegFreeState(ctxt->start);
5447 xmlFreeAutomata(ctxt);
5448 return(NULL);
5449 }
Daniel Veillard4255d502002-04-16 15:50:10 +00005450
5451 return(ctxt);
5452}
5453
5454/**
5455 * xmlFreeAutomata:
5456 * @am: an automata
5457 *
5458 * Free an automata
5459 */
5460void
5461xmlFreeAutomata(xmlAutomataPtr am) {
5462 if (am == NULL)
5463 return;
5464 xmlRegFreeParserCtxt(am);
5465}
5466
5467/**
5468 * xmlAutomataGetInitState:
5469 * @am: an automata
5470 *
Daniel Veillarda9b66d02002-12-11 14:23:49 +00005471 * Initial state lookup
5472 *
Daniel Veillard4255d502002-04-16 15:50:10 +00005473 * Returns the initial state of the automata
5474 */
5475xmlAutomataStatePtr
5476xmlAutomataGetInitState(xmlAutomataPtr am) {
5477 if (am == NULL)
5478 return(NULL);
5479 return(am->start);
5480}
5481
5482/**
5483 * xmlAutomataSetFinalState:
5484 * @am: an automata
5485 * @state: a state in this automata
5486 *
5487 * Makes that state a final state
5488 *
5489 * Returns 0 or -1 in case of error
5490 */
5491int
5492xmlAutomataSetFinalState(xmlAutomataPtr am, xmlAutomataStatePtr state) {
5493 if ((am == NULL) || (state == NULL))
5494 return(-1);
5495 state->type = XML_REGEXP_FINAL_STATE;
5496 return(0);
5497}
5498
5499/**
5500 * xmlAutomataNewTransition:
5501 * @am: an automata
5502 * @from: the starting point of the transition
5503 * @to: the target point of the transition or NULL
5504 * @token: the input string associated to that transition
5505 * @data: data passed to the callback function if the transition is activated
5506 *
William M. Brackddf71d62004-05-06 04:17:26 +00005507 * If @to is NULL, this creates first a new target state in the automata
Daniel Veillard4255d502002-04-16 15:50:10 +00005508 * and then adds a transition from the @from state to the target state
5509 * activated by the value of @token
5510 *
5511 * Returns the target state or NULL in case of error
5512 */
5513xmlAutomataStatePtr
5514xmlAutomataNewTransition(xmlAutomataPtr am, xmlAutomataStatePtr from,
5515 xmlAutomataStatePtr to, const xmlChar *token,
5516 void *data) {
5517 xmlRegAtomPtr atom;
5518
5519 if ((am == NULL) || (from == NULL) || (token == NULL))
5520 return(NULL);
5521 atom = xmlRegNewAtom(am, XML_REGEXP_STRING);
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00005522 if (atom == NULL)
5523 return(NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +00005524 atom->data = data;
5525 if (atom == NULL)
5526 return(NULL);
5527 atom->valuep = xmlStrdup(token);
5528
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00005529 if (xmlFAGenerateTransitions(am, from, to, atom) < 0) {
5530 xmlRegFreeAtom(atom);
5531 return(NULL);
5532 }
Daniel Veillard4255d502002-04-16 15:50:10 +00005533 if (to == NULL)
5534 return(am->state);
5535 return(to);
5536}
5537
5538/**
Daniel Veillard52b48c72003-04-13 19:53:42 +00005539 * xmlAutomataNewTransition2:
5540 * @am: an automata
5541 * @from: the starting point of the transition
5542 * @to: the target point of the transition or NULL
5543 * @token: the first input string associated to that transition
5544 * @token2: the second input string associated to that transition
5545 * @data: data passed to the callback function if the transition is activated
5546 *
William M. Brackddf71d62004-05-06 04:17:26 +00005547 * If @to is NULL, this creates first a new target state in the automata
Daniel Veillard52b48c72003-04-13 19:53:42 +00005548 * and then adds a transition from the @from state to the target state
5549 * activated by the value of @token
5550 *
5551 * Returns the target state or NULL in case of error
5552 */
5553xmlAutomataStatePtr
5554xmlAutomataNewTransition2(xmlAutomataPtr am, xmlAutomataStatePtr from,
5555 xmlAutomataStatePtr to, const xmlChar *token,
5556 const xmlChar *token2, void *data) {
5557 xmlRegAtomPtr atom;
5558
5559 if ((am == NULL) || (from == NULL) || (token == NULL))
5560 return(NULL);
5561 atom = xmlRegNewAtom(am, XML_REGEXP_STRING);
Daniel Veillard52b48c72003-04-13 19:53:42 +00005562 if (atom == NULL)
5563 return(NULL);
Daniel Veillard11ce4002006-03-10 00:36:23 +00005564 atom->data = data;
Daniel Veillard52b48c72003-04-13 19:53:42 +00005565 if ((token2 == NULL) || (*token2 == 0)) {
5566 atom->valuep = xmlStrdup(token);
5567 } else {
5568 int lenn, lenp;
5569 xmlChar *str;
5570
5571 lenn = strlen((char *) token2);
5572 lenp = strlen((char *) token);
5573
Daniel Veillard3c908dc2003-04-19 00:07:51 +00005574 str = (xmlChar *) xmlMallocAtomic(lenn + lenp + 2);
Daniel Veillard52b48c72003-04-13 19:53:42 +00005575 if (str == NULL) {
5576 xmlRegFreeAtom(atom);
5577 return(NULL);
5578 }
5579 memcpy(&str[0], token, lenp);
5580 str[lenp] = '|';
5581 memcpy(&str[lenp + 1], token2, lenn);
5582 str[lenn + lenp + 1] = 0;
5583
5584 atom->valuep = str;
5585 }
5586
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00005587 if (xmlFAGenerateTransitions(am, from, to, atom) < 0) {
5588 xmlRegFreeAtom(atom);
5589 return(NULL);
5590 }
Daniel Veillard52b48c72003-04-13 19:53:42 +00005591 if (to == NULL)
5592 return(am->state);
5593 return(to);
5594}
5595
5596/**
Daniel Veillard9efc4762005-07-19 14:33:55 +00005597 * xmlAutomataNewNegTrans:
5598 * @am: an automata
5599 * @from: the starting point of the transition
5600 * @to: the target point of the transition or NULL
5601 * @token: the first input string associated to that transition
5602 * @token2: the second input string associated to that transition
5603 * @data: data passed to the callback function if the transition is activated
5604 *
5605 * If @to is NULL, this creates first a new target state in the automata
5606 * and then adds a transition from the @from state to the target state
5607 * activated by any value except (@token,@token2)
Daniel Veillard6e65e152005-08-09 11:09:52 +00005608 * Note that if @token2 is not NULL, then (X, NULL) won't match to follow
5609 # the semantic of XSD ##other
Daniel Veillard9efc4762005-07-19 14:33:55 +00005610 *
5611 * Returns the target state or NULL in case of error
5612 */
5613xmlAutomataStatePtr
5614xmlAutomataNewNegTrans(xmlAutomataPtr am, xmlAutomataStatePtr from,
5615 xmlAutomataStatePtr to, const xmlChar *token,
5616 const xmlChar *token2, void *data) {
5617 xmlRegAtomPtr atom;
Daniel Veillard77005e62005-07-19 16:26:18 +00005618 xmlChar err_msg[200];
Daniel Veillard9efc4762005-07-19 14:33:55 +00005619
5620 if ((am == NULL) || (from == NULL) || (token == NULL))
5621 return(NULL);
5622 atom = xmlRegNewAtom(am, XML_REGEXP_STRING);
5623 if (atom == NULL)
5624 return(NULL);
5625 atom->data = data;
5626 atom->neg = 1;
5627 if ((token2 == NULL) || (*token2 == 0)) {
5628 atom->valuep = xmlStrdup(token);
5629 } else {
5630 int lenn, lenp;
5631 xmlChar *str;
5632
5633 lenn = strlen((char *) token2);
5634 lenp = strlen((char *) token);
5635
5636 str = (xmlChar *) xmlMallocAtomic(lenn + lenp + 2);
5637 if (str == NULL) {
5638 xmlRegFreeAtom(atom);
5639 return(NULL);
5640 }
5641 memcpy(&str[0], token, lenp);
5642 str[lenp] = '|';
5643 memcpy(&str[lenp + 1], token2, lenn);
5644 str[lenn + lenp + 1] = 0;
5645
5646 atom->valuep = str;
5647 }
Daniel Veillarddb68b742005-07-30 13:18:24 +00005648 snprintf((char *) err_msg, 199, "not %s", (const char *) atom->valuep);
Daniel Veillard77005e62005-07-19 16:26:18 +00005649 err_msg[199] = 0;
5650 atom->valuep2 = xmlStrdup(err_msg);
Daniel Veillard9efc4762005-07-19 14:33:55 +00005651
5652 if (xmlFAGenerateTransitions(am, from, to, atom) < 0) {
5653 xmlRegFreeAtom(atom);
5654 return(NULL);
5655 }
Daniel Veillard6e65e152005-08-09 11:09:52 +00005656 am->negs++;
Daniel Veillard9efc4762005-07-19 14:33:55 +00005657 if (to == NULL)
5658 return(am->state);
5659 return(to);
5660}
5661
5662/**
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00005663 * xmlAutomataNewCountTrans2:
5664 * @am: an automata
5665 * @from: the starting point of the transition
5666 * @to: the target point of the transition or NULL
5667 * @token: the input string associated to that transition
5668 * @token2: the second input string associated to that transition
5669 * @min: the minimum successive occurences of token
5670 * @max: the maximum successive occurences of token
5671 * @data: data associated to the transition
5672 *
5673 * If @to is NULL, this creates first a new target state in the automata
5674 * and then adds a transition from the @from state to the target state
5675 * activated by a succession of input of value @token and @token2 and
5676 * whose number is between @min and @max
5677 *
5678 * Returns the target state or NULL in case of error
5679 */
5680xmlAutomataStatePtr
5681xmlAutomataNewCountTrans2(xmlAutomataPtr am, xmlAutomataStatePtr from,
5682 xmlAutomataStatePtr to, const xmlChar *token,
5683 const xmlChar *token2,
5684 int min, int max, void *data) {
5685 xmlRegAtomPtr atom;
5686 int counter;
5687
5688 if ((am == NULL) || (from == NULL) || (token == NULL))
5689 return(NULL);
5690 if (min < 0)
5691 return(NULL);
5692 if ((max < min) || (max < 1))
5693 return(NULL);
5694 atom = xmlRegNewAtom(am, XML_REGEXP_STRING);
5695 if (atom == NULL)
5696 return(NULL);
5697 if ((token2 == NULL) || (*token2 == 0)) {
5698 atom->valuep = xmlStrdup(token);
5699 } else {
5700 int lenn, lenp;
5701 xmlChar *str;
5702
5703 lenn = strlen((char *) token2);
5704 lenp = strlen((char *) token);
5705
5706 str = (xmlChar *) xmlMallocAtomic(lenn + lenp + 2);
5707 if (str == NULL) {
5708 xmlRegFreeAtom(atom);
5709 return(NULL);
5710 }
5711 memcpy(&str[0], token, lenp);
5712 str[lenp] = '|';
5713 memcpy(&str[lenp + 1], token2, lenn);
5714 str[lenn + lenp + 1] = 0;
5715
5716 atom->valuep = str;
5717 }
5718 atom->data = data;
5719 if (min == 0)
5720 atom->min = 1;
5721 else
5722 atom->min = min;
5723 atom->max = max;
5724
5725 /*
5726 * associate a counter to the transition.
5727 */
5728 counter = xmlRegGetCounter(am);
5729 am->counters[counter].min = min;
5730 am->counters[counter].max = max;
5731
5732 /* xmlFAGenerateTransitions(am, from, to, atom); */
5733 if (to == NULL) {
5734 to = xmlRegNewState(am);
5735 xmlRegStatePush(am, to);
5736 }
Daniel Veillard5de09382005-09-26 17:18:17 +00005737 xmlRegStateAddTrans(am, from, atom, to, counter, -1);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00005738 xmlRegAtomPush(am, atom);
5739 am->state = to;
5740
5741 if (to == NULL)
5742 to = am->state;
5743 if (to == NULL)
5744 return(NULL);
5745 if (min == 0)
5746 xmlFAGenerateEpsilonTransition(am, from, to);
5747 return(to);
5748}
5749
5750/**
Daniel Veillard4255d502002-04-16 15:50:10 +00005751 * xmlAutomataNewCountTrans:
5752 * @am: an automata
5753 * @from: the starting point of the transition
5754 * @to: the target point of the transition or NULL
5755 * @token: the input string associated to that transition
5756 * @min: the minimum successive occurences of token
Daniel Veillarda9b66d02002-12-11 14:23:49 +00005757 * @max: the maximum successive occurences of token
5758 * @data: data associated to the transition
Daniel Veillard4255d502002-04-16 15:50:10 +00005759 *
William M. Brackddf71d62004-05-06 04:17:26 +00005760 * If @to is NULL, this creates first a new target state in the automata
Daniel Veillard4255d502002-04-16 15:50:10 +00005761 * and then adds a transition from the @from state to the target state
5762 * activated by a succession of input of value @token and whose number
5763 * is between @min and @max
5764 *
5765 * Returns the target state or NULL in case of error
5766 */
5767xmlAutomataStatePtr
5768xmlAutomataNewCountTrans(xmlAutomataPtr am, xmlAutomataStatePtr from,
5769 xmlAutomataStatePtr to, const xmlChar *token,
5770 int min, int max, void *data) {
5771 xmlRegAtomPtr atom;
Daniel Veillard0ddb21c2004-02-12 12:43:49 +00005772 int counter;
Daniel Veillard4255d502002-04-16 15:50:10 +00005773
5774 if ((am == NULL) || (from == NULL) || (token == NULL))
5775 return(NULL);
5776 if (min < 0)
5777 return(NULL);
5778 if ((max < min) || (max < 1))
5779 return(NULL);
5780 atom = xmlRegNewAtom(am, XML_REGEXP_STRING);
5781 if (atom == NULL)
5782 return(NULL);
5783 atom->valuep = xmlStrdup(token);
5784 atom->data = data;
5785 if (min == 0)
5786 atom->min = 1;
5787 else
5788 atom->min = min;
5789 atom->max = max;
5790
Daniel Veillard0ddb21c2004-02-12 12:43:49 +00005791 /*
5792 * associate a counter to the transition.
5793 */
5794 counter = xmlRegGetCounter(am);
5795 am->counters[counter].min = min;
5796 am->counters[counter].max = max;
5797
5798 /* xmlFAGenerateTransitions(am, from, to, atom); */
5799 if (to == NULL) {
5800 to = xmlRegNewState(am);
5801 xmlRegStatePush(am, to);
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00005802 }
Daniel Veillard5de09382005-09-26 17:18:17 +00005803 xmlRegStateAddTrans(am, from, atom, to, counter, -1);
Daniel Veillard0ddb21c2004-02-12 12:43:49 +00005804 xmlRegAtomPush(am, atom);
5805 am->state = to;
5806
Daniel Veillard4255d502002-04-16 15:50:10 +00005807 if (to == NULL)
5808 to = am->state;
5809 if (to == NULL)
5810 return(NULL);
5811 if (min == 0)
5812 xmlFAGenerateEpsilonTransition(am, from, to);
5813 return(to);
5814}
5815
5816/**
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00005817 * xmlAutomataNewOnceTrans2:
5818 * @am: an automata
5819 * @from: the starting point of the transition
5820 * @to: the target point of the transition or NULL
5821 * @token: the input string associated to that transition
5822 * @token2: the second input string associated to that transition
5823 * @min: the minimum successive occurences of token
5824 * @max: the maximum successive occurences of token
5825 * @data: data associated to the transition
5826 *
5827 * If @to is NULL, this creates first a new target state in the automata
5828 * and then adds a transition from the @from state to the target state
5829 * activated by a succession of input of value @token and @token2 and whose
5830 * number is between @min and @max, moreover that transition can only be
5831 * crossed once.
5832 *
5833 * Returns the target state or NULL in case of error
5834 */
5835xmlAutomataStatePtr
5836xmlAutomataNewOnceTrans2(xmlAutomataPtr am, xmlAutomataStatePtr from,
5837 xmlAutomataStatePtr to, const xmlChar *token,
5838 const xmlChar *token2,
5839 int min, int max, void *data) {
5840 xmlRegAtomPtr atom;
5841 int counter;
5842
5843 if ((am == NULL) || (from == NULL) || (token == NULL))
5844 return(NULL);
5845 if (min < 1)
5846 return(NULL);
5847 if ((max < min) || (max < 1))
5848 return(NULL);
5849 atom = xmlRegNewAtom(am, XML_REGEXP_STRING);
5850 if (atom == NULL)
5851 return(NULL);
5852 if ((token2 == NULL) || (*token2 == 0)) {
5853 atom->valuep = xmlStrdup(token);
5854 } else {
5855 int lenn, lenp;
5856 xmlChar *str;
5857
5858 lenn = strlen((char *) token2);
5859 lenp = strlen((char *) token);
5860
5861 str = (xmlChar *) xmlMallocAtomic(lenn + lenp + 2);
5862 if (str == NULL) {
5863 xmlRegFreeAtom(atom);
5864 return(NULL);
5865 }
5866 memcpy(&str[0], token, lenp);
5867 str[lenp] = '|';
5868 memcpy(&str[lenp + 1], token2, lenn);
5869 str[lenn + lenp + 1] = 0;
5870
5871 atom->valuep = str;
5872 }
5873 atom->data = data;
5874 atom->quant = XML_REGEXP_QUANT_ONCEONLY;
Daniel Veillard11ce4002006-03-10 00:36:23 +00005875 atom->min = min;
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00005876 atom->max = max;
5877 /*
5878 * associate a counter to the transition.
5879 */
5880 counter = xmlRegGetCounter(am);
5881 am->counters[counter].min = 1;
5882 am->counters[counter].max = 1;
5883
5884 /* xmlFAGenerateTransitions(am, from, to, atom); */
5885 if (to == NULL) {
5886 to = xmlRegNewState(am);
5887 xmlRegStatePush(am, to);
5888 }
Daniel Veillard5de09382005-09-26 17:18:17 +00005889 xmlRegStateAddTrans(am, from, atom, to, counter, -1);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00005890 xmlRegAtomPush(am, atom);
5891 am->state = to;
5892 return(to);
5893}
5894
5895
5896
5897/**
Daniel Veillard7646b182002-04-20 06:41:40 +00005898 * xmlAutomataNewOnceTrans:
5899 * @am: an automata
5900 * @from: the starting point of the transition
5901 * @to: the target point of the transition or NULL
5902 * @token: the input string associated to that transition
5903 * @min: the minimum successive occurences of token
Daniel Veillarda9b66d02002-12-11 14:23:49 +00005904 * @max: the maximum successive occurences of token
5905 * @data: data associated to the transition
Daniel Veillard7646b182002-04-20 06:41:40 +00005906 *
William M. Brackddf71d62004-05-06 04:17:26 +00005907 * If @to is NULL, this creates first a new target state in the automata
Daniel Veillard7646b182002-04-20 06:41:40 +00005908 * and then adds a transition from the @from state to the target state
5909 * activated by a succession of input of value @token and whose number
William M. Brackddf71d62004-05-06 04:17:26 +00005910 * is between @min and @max, moreover that transition can only be crossed
Daniel Veillard7646b182002-04-20 06:41:40 +00005911 * once.
5912 *
5913 * Returns the target state or NULL in case of error
5914 */
5915xmlAutomataStatePtr
5916xmlAutomataNewOnceTrans(xmlAutomataPtr am, xmlAutomataStatePtr from,
5917 xmlAutomataStatePtr to, const xmlChar *token,
5918 int min, int max, void *data) {
5919 xmlRegAtomPtr atom;
5920 int counter;
5921
5922 if ((am == NULL) || (from == NULL) || (token == NULL))
5923 return(NULL);
5924 if (min < 1)
5925 return(NULL);
5926 if ((max < min) || (max < 1))
5927 return(NULL);
5928 atom = xmlRegNewAtom(am, XML_REGEXP_STRING);
5929 if (atom == NULL)
5930 return(NULL);
5931 atom->valuep = xmlStrdup(token);
5932 atom->data = data;
5933 atom->quant = XML_REGEXP_QUANT_ONCEONLY;
Daniel Veillard11ce4002006-03-10 00:36:23 +00005934 atom->min = min;
Daniel Veillard7646b182002-04-20 06:41:40 +00005935 atom->max = max;
5936 /*
5937 * associate a counter to the transition.
5938 */
5939 counter = xmlRegGetCounter(am);
5940 am->counters[counter].min = 1;
5941 am->counters[counter].max = 1;
5942
5943 /* xmlFAGenerateTransitions(am, from, to, atom); */
5944 if (to == NULL) {
5945 to = xmlRegNewState(am);
5946 xmlRegStatePush(am, to);
5947 }
Daniel Veillard5de09382005-09-26 17:18:17 +00005948 xmlRegStateAddTrans(am, from, atom, to, counter, -1);
Daniel Veillard7646b182002-04-20 06:41:40 +00005949 xmlRegAtomPush(am, atom);
5950 am->state = to;
Daniel Veillard7646b182002-04-20 06:41:40 +00005951 return(to);
5952}
5953
5954/**
Daniel Veillard4255d502002-04-16 15:50:10 +00005955 * xmlAutomataNewState:
5956 * @am: an automata
5957 *
5958 * Create a new disconnected state in the automata
5959 *
5960 * Returns the new state or NULL in case of error
5961 */
5962xmlAutomataStatePtr
5963xmlAutomataNewState(xmlAutomataPtr am) {
5964 xmlAutomataStatePtr to;
5965
5966 if (am == NULL)
5967 return(NULL);
5968 to = xmlRegNewState(am);
5969 xmlRegStatePush(am, to);
5970 return(to);
5971}
5972
5973/**
Daniel Veillarda9b66d02002-12-11 14:23:49 +00005974 * xmlAutomataNewEpsilon:
Daniel Veillard4255d502002-04-16 15:50:10 +00005975 * @am: an automata
5976 * @from: the starting point of the transition
5977 * @to: the target point of the transition or NULL
5978 *
William M. Brackddf71d62004-05-06 04:17:26 +00005979 * If @to is NULL, this creates first a new target state in the automata
5980 * and then adds an epsilon transition from the @from state to the
Daniel Veillard4255d502002-04-16 15:50:10 +00005981 * target state
5982 *
5983 * Returns the target state or NULL in case of error
5984 */
5985xmlAutomataStatePtr
5986xmlAutomataNewEpsilon(xmlAutomataPtr am, xmlAutomataStatePtr from,
5987 xmlAutomataStatePtr to) {
5988 if ((am == NULL) || (from == NULL))
5989 return(NULL);
5990 xmlFAGenerateEpsilonTransition(am, from, to);
5991 if (to == NULL)
5992 return(am->state);
5993 return(to);
5994}
5995
Daniel Veillardb509f152002-04-17 16:28:10 +00005996/**
Daniel Veillard7646b182002-04-20 06:41:40 +00005997 * xmlAutomataNewAllTrans:
5998 * @am: an automata
5999 * @from: the starting point of the transition
6000 * @to: the target point of the transition or NULL
Daniel Veillarda9b66d02002-12-11 14:23:49 +00006001 * @lax: allow to transition if not all all transitions have been activated
Daniel Veillard7646b182002-04-20 06:41:40 +00006002 *
William M. Brackddf71d62004-05-06 04:17:26 +00006003 * If @to is NULL, this creates first a new target state in the automata
Daniel Veillard7646b182002-04-20 06:41:40 +00006004 * and then adds a an ALL transition from the @from state to the
6005 * target state. That transition is an epsilon transition allowed only when
6006 * all transitions from the @from node have been activated.
6007 *
6008 * Returns the target state or NULL in case of error
6009 */
6010xmlAutomataStatePtr
6011xmlAutomataNewAllTrans(xmlAutomataPtr am, xmlAutomataStatePtr from,
Daniel Veillard441bc322002-04-20 17:38:48 +00006012 xmlAutomataStatePtr to, int lax) {
Daniel Veillard7646b182002-04-20 06:41:40 +00006013 if ((am == NULL) || (from == NULL))
6014 return(NULL);
Daniel Veillard441bc322002-04-20 17:38:48 +00006015 xmlFAGenerateAllTransition(am, from, to, lax);
Daniel Veillard7646b182002-04-20 06:41:40 +00006016 if (to == NULL)
6017 return(am->state);
6018 return(to);
6019}
6020
6021/**
Daniel Veillardb509f152002-04-17 16:28:10 +00006022 * xmlAutomataNewCounter:
6023 * @am: an automata
6024 * @min: the minimal value on the counter
6025 * @max: the maximal value on the counter
6026 *
6027 * Create a new counter
6028 *
6029 * Returns the counter number or -1 in case of error
6030 */
6031int
6032xmlAutomataNewCounter(xmlAutomataPtr am, int min, int max) {
6033 int ret;
6034
6035 if (am == NULL)
6036 return(-1);
6037
6038 ret = xmlRegGetCounter(am);
6039 if (ret < 0)
6040 return(-1);
6041 am->counters[ret].min = min;
6042 am->counters[ret].max = max;
6043 return(ret);
6044}
6045
6046/**
6047 * xmlAutomataNewCountedTrans:
6048 * @am: an automata
6049 * @from: the starting point of the transition
6050 * @to: the target point of the transition or NULL
6051 * @counter: the counter associated to that transition
6052 *
William M. Brackddf71d62004-05-06 04:17:26 +00006053 * If @to is NULL, this creates first a new target state in the automata
Daniel Veillardb509f152002-04-17 16:28:10 +00006054 * and then adds an epsilon transition from the @from state to the target state
6055 * which will increment the counter provided
6056 *
6057 * Returns the target state or NULL in case of error
6058 */
6059xmlAutomataStatePtr
6060xmlAutomataNewCountedTrans(xmlAutomataPtr am, xmlAutomataStatePtr from,
6061 xmlAutomataStatePtr to, int counter) {
6062 if ((am == NULL) || (from == NULL) || (counter < 0))
6063 return(NULL);
6064 xmlFAGenerateCountedEpsilonTransition(am, from, to, counter);
6065 if (to == NULL)
6066 return(am->state);
6067 return(to);
6068}
6069
6070/**
6071 * xmlAutomataNewCounterTrans:
6072 * @am: an automata
6073 * @from: the starting point of the transition
6074 * @to: the target point of the transition or NULL
6075 * @counter: the counter associated to that transition
6076 *
William M. Brackddf71d62004-05-06 04:17:26 +00006077 * If @to is NULL, this creates first a new target state in the automata
Daniel Veillardb509f152002-04-17 16:28:10 +00006078 * and then adds an epsilon transition from the @from state to the target state
6079 * which will be allowed only if the counter is within the right range.
6080 *
6081 * Returns the target state or NULL in case of error
6082 */
6083xmlAutomataStatePtr
6084xmlAutomataNewCounterTrans(xmlAutomataPtr am, xmlAutomataStatePtr from,
6085 xmlAutomataStatePtr to, int counter) {
6086 if ((am == NULL) || (from == NULL) || (counter < 0))
6087 return(NULL);
6088 xmlFAGenerateCountedTransition(am, from, to, counter);
6089 if (to == NULL)
6090 return(am->state);
6091 return(to);
6092}
Daniel Veillard4255d502002-04-16 15:50:10 +00006093
6094/**
6095 * xmlAutomataCompile:
6096 * @am: an automata
6097 *
6098 * Compile the automata into a Reg Exp ready for being executed.
6099 * The automata should be free after this point.
6100 *
6101 * Returns the compiled regexp or NULL in case of error
6102 */
6103xmlRegexpPtr
6104xmlAutomataCompile(xmlAutomataPtr am) {
6105 xmlRegexpPtr ret;
6106
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00006107 if ((am == NULL) || (am->error != 0)) return(NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +00006108 xmlFAEliminateEpsilonTransitions(am);
Daniel Veillard23e73572002-09-19 19:56:43 +00006109 /* xmlFAComputesDeterminism(am); */
Daniel Veillard4255d502002-04-16 15:50:10 +00006110 ret = xmlRegEpxFromParse(am);
6111
6112 return(ret);
6113}
Daniel Veillarde19fc232002-04-22 16:01:24 +00006114
6115/**
6116 * xmlAutomataIsDeterminist:
6117 * @am: an automata
6118 *
6119 * Checks if an automata is determinist.
6120 *
6121 * Returns 1 if true, 0 if not, and -1 in case of error
6122 */
6123int
6124xmlAutomataIsDeterminist(xmlAutomataPtr am) {
6125 int ret;
6126
6127 if (am == NULL)
6128 return(-1);
6129
6130 ret = xmlFAComputesDeterminism(am);
6131 return(ret);
6132}
Daniel Veillard4255d502002-04-16 15:50:10 +00006133#endif /* LIBXML_AUTOMATA_ENABLED */
Daniel Veillard81a8ec62005-08-22 00:20:58 +00006134
6135#ifdef LIBXML_EXPR_ENABLED
6136/************************************************************************
6137 * *
6138 * Formal Expression handling code *
6139 * *
6140 ************************************************************************/
Daniel Veillard81a8ec62005-08-22 00:20:58 +00006141/************************************************************************
6142 * *
6143 * Expression handling context *
6144 * *
6145 ************************************************************************/
6146
6147struct _xmlExpCtxt {
6148 xmlDictPtr dict;
6149 xmlExpNodePtr *table;
6150 int size;
6151 int nbElems;
6152 int nb_nodes;
6153 const char *expr;
6154 const char *cur;
6155 int nb_cons;
Daniel Veillard81a8ec62005-08-22 00:20:58 +00006156 int tabSize;
6157};
6158
6159/**
6160 * xmlExpNewCtxt:
6161 * @maxNodes: the maximum number of nodes
6162 * @dict: optional dictionnary to use internally
6163 *
6164 * Creates a new context for manipulating expressions
6165 *
6166 * Returns the context or NULL in case of error
6167 */
6168xmlExpCtxtPtr
6169xmlExpNewCtxt(int maxNodes, xmlDictPtr dict) {
6170 xmlExpCtxtPtr ret;
6171 int size = 256;
6172
6173 if (maxNodes <= 4096)
6174 maxNodes = 4096;
6175
6176 ret = (xmlExpCtxtPtr) xmlMalloc(sizeof(xmlExpCtxt));
6177 if (ret == NULL)
6178 return(NULL);
6179 memset(ret, 0, sizeof(xmlExpCtxt));
6180 ret->size = size;
6181 ret->nbElems = 0;
6182 ret->table = xmlMalloc(size * sizeof(xmlExpNodePtr));
6183 if (ret->table == NULL) {
6184 xmlFree(ret);
6185 return(NULL);
6186 }
6187 memset(ret->table, 0, size * sizeof(xmlExpNodePtr));
6188 if (dict == NULL) {
6189 ret->dict = xmlDictCreate();
6190 if (ret->dict == NULL) {
6191 xmlFree(ret->table);
6192 xmlFree(ret);
6193 return(NULL);
6194 }
6195 } else {
6196 ret->dict = dict;
6197 xmlDictReference(ret->dict);
6198 }
6199 return(ret);
6200}
6201
6202/**
6203 * xmlExpFreeCtxt:
6204 * @ctxt: an expression context
6205 *
6206 * Free an expression context
6207 */
6208void
6209xmlExpFreeCtxt(xmlExpCtxtPtr ctxt) {
6210 if (ctxt == NULL)
6211 return;
6212 xmlDictFree(ctxt->dict);
6213 if (ctxt->table != NULL)
6214 xmlFree(ctxt->table);
6215 xmlFree(ctxt);
6216}
6217
6218/************************************************************************
6219 * *
6220 * Structure associated to an expression node *
6221 * *
6222 ************************************************************************/
Daniel Veillard465a0002005-08-22 12:07:04 +00006223#define MAX_NODES 10000
6224
6225/* #define DEBUG_DERIV */
6226
6227/*
6228 * TODO:
6229 * - Wildcards
6230 * - public API for creation
6231 *
6232 * Started
6233 * - regression testing
6234 *
6235 * Done
6236 * - split into module and test tool
6237 * - memleaks
6238 */
Daniel Veillard81a8ec62005-08-22 00:20:58 +00006239
6240typedef enum {
6241 XML_EXP_NILABLE = (1 << 0)
6242} xmlExpNodeInfo;
6243
6244#define IS_NILLABLE(node) ((node)->info & XML_EXP_NILABLE)
6245
6246struct _xmlExpNode {
6247 unsigned char type;/* xmlExpNodeType */
6248 unsigned char info;/* OR of xmlExpNodeInfo */
6249 unsigned short key; /* the hash key */
6250 unsigned int ref; /* The number of references */
6251 int c_max; /* the maximum length it can consume */
6252 xmlExpNodePtr exp_left;
6253 xmlExpNodePtr next;/* the next node in the hash table or free list */
6254 union {
6255 struct {
6256 int f_min;
6257 int f_max;
6258 } count;
6259 struct {
6260 xmlExpNodePtr f_right;
6261 } children;
6262 const xmlChar *f_str;
6263 } field;
6264};
6265
6266#define exp_min field.count.f_min
6267#define exp_max field.count.f_max
6268/* #define exp_left field.children.f_left */
6269#define exp_right field.children.f_right
6270#define exp_str field.f_str
6271
6272static xmlExpNodePtr xmlExpNewNode(xmlExpCtxtPtr ctxt, xmlExpNodeType type);
6273static xmlExpNode forbiddenExpNode = {
6274 XML_EXP_FORBID, 0, 0, 0, 0, NULL, NULL, {{ 0, 0}}
6275};
6276xmlExpNodePtr forbiddenExp = &forbiddenExpNode;
6277static xmlExpNode emptyExpNode = {
6278 XML_EXP_EMPTY, 1, 0, 0, 0, NULL, NULL, {{ 0, 0}}
6279};
6280xmlExpNodePtr emptyExp = &emptyExpNode;
6281
6282/************************************************************************
6283 * *
6284 * The custom hash table for unicity and canonicalization *
6285 * of sub-expressions pointers *
6286 * *
6287 ************************************************************************/
6288/*
6289 * xmlExpHashNameComputeKey:
6290 * Calculate the hash key for a token
6291 */
6292static unsigned short
6293xmlExpHashNameComputeKey(const xmlChar *name) {
6294 unsigned short value = 0L;
6295 char ch;
6296
6297 if (name != NULL) {
6298 value += 30 * (*name);
6299 while ((ch = *name++) != 0) {
6300 value = value ^ ((value << 5) + (value >> 3) + (unsigned long)ch);
6301 }
6302 }
6303 return (value);
6304}
6305
6306/*
6307 * xmlExpHashComputeKey:
6308 * Calculate the hash key for a compound expression
6309 */
6310static unsigned short
6311xmlExpHashComputeKey(xmlExpNodeType type, xmlExpNodePtr left,
6312 xmlExpNodePtr right) {
6313 unsigned long value;
6314 unsigned short ret;
6315
6316 switch (type) {
6317 case XML_EXP_SEQ:
6318 value = left->key;
6319 value += right->key;
6320 value *= 3;
6321 ret = (unsigned short) value;
6322 break;
6323 case XML_EXP_OR:
6324 value = left->key;
6325 value += right->key;
6326 value *= 7;
6327 ret = (unsigned short) value;
6328 break;
6329 case XML_EXP_COUNT:
6330 value = left->key;
6331 value += right->key;
6332 ret = (unsigned short) value;
6333 break;
6334 default:
6335 ret = 0;
6336 }
6337 return(ret);
6338}
6339
6340
6341static xmlExpNodePtr
6342xmlExpNewNode(xmlExpCtxtPtr ctxt, xmlExpNodeType type) {
6343 xmlExpNodePtr ret;
6344
6345 if (ctxt->nb_nodes >= MAX_NODES)
6346 return(NULL);
6347 ret = (xmlExpNodePtr) xmlMalloc(sizeof(xmlExpNode));
6348 if (ret == NULL)
6349 return(NULL);
6350 memset(ret, 0, sizeof(xmlExpNode));
6351 ret->type = type;
6352 ret->next = NULL;
6353 ctxt->nb_nodes++;
6354 ctxt->nb_cons++;
6355 return(ret);
6356}
6357
6358/**
6359 * xmlExpHashGetEntry:
6360 * @table: the hash table
6361 *
6362 * Get the unique entry from the hash table. The entry is created if
6363 * needed. @left and @right are consumed, i.e. their ref count will
6364 * be decremented by the operation.
6365 *
6366 * Returns the pointer or NULL in case of error
6367 */
6368static xmlExpNodePtr
6369xmlExpHashGetEntry(xmlExpCtxtPtr ctxt, xmlExpNodeType type,
6370 xmlExpNodePtr left, xmlExpNodePtr right,
6371 const xmlChar *name, int min, int max) {
6372 unsigned short kbase, key;
6373 xmlExpNodePtr entry;
6374 xmlExpNodePtr insert;
6375
6376 if (ctxt == NULL)
6377 return(NULL);
6378
6379 /*
6380 * Check for duplicate and insertion location.
6381 */
6382 if (type == XML_EXP_ATOM) {
6383 kbase = xmlExpHashNameComputeKey(name);
6384 } else if (type == XML_EXP_COUNT) {
6385 /* COUNT reduction rule 1 */
6386 /* a{1} -> a */
6387 if (min == max) {
6388 if (min == 1) {
6389 return(left);
6390 }
6391 if (min == 0) {
6392 xmlExpFree(ctxt, left);
6393 return(emptyExp);
6394 }
6395 }
6396 if (min < 0) {
6397 xmlExpFree(ctxt, left);
6398 return(forbiddenExp);
6399 }
6400 if (max == -1)
6401 kbase = min + 79;
6402 else
6403 kbase = max - min;
6404 kbase += left->key;
6405 } else if (type == XML_EXP_OR) {
6406 /* Forbid reduction rules */
6407 if (left->type == XML_EXP_FORBID) {
6408 xmlExpFree(ctxt, left);
6409 return(right);
6410 }
6411 if (right->type == XML_EXP_FORBID) {
6412 xmlExpFree(ctxt, right);
6413 return(left);
6414 }
6415
6416 /* OR reduction rule 1 */
6417 /* a | a reduced to a */
6418 if (left == right) {
6419 left->ref--;
6420 return(left);
6421 }
6422 /* OR canonicalization rule 1 */
6423 /* linearize (a | b) | c into a | (b | c) */
6424 if ((left->type == XML_EXP_OR) && (right->type != XML_EXP_OR)) {
6425 xmlExpNodePtr tmp = left;
6426 left = right;
6427 right = tmp;
6428 }
6429 /* OR reduction rule 2 */
6430 /* a | (a | b) and b | (a | b) are reduced to a | b */
6431 if (right->type == XML_EXP_OR) {
6432 if ((left == right->exp_left) ||
6433 (left == right->exp_right)) {
6434 xmlExpFree(ctxt, left);
6435 return(right);
6436 }
6437 }
6438 /* OR canonicalization rule 2 */
6439 /* linearize (a | b) | c into a | (b | c) */
6440 if (left->type == XML_EXP_OR) {
6441 xmlExpNodePtr tmp;
6442
6443 /* OR canonicalization rule 2 */
6444 if ((left->exp_right->type != XML_EXP_OR) &&
6445 (left->exp_right->key < left->exp_left->key)) {
6446 tmp = left->exp_right;
6447 left->exp_right = left->exp_left;
6448 left->exp_left = tmp;
6449 }
6450 left->exp_right->ref++;
6451 tmp = xmlExpHashGetEntry(ctxt, XML_EXP_OR, left->exp_right, right,
6452 NULL, 0, 0);
6453 left->exp_left->ref++;
6454 tmp = xmlExpHashGetEntry(ctxt, XML_EXP_OR, left->exp_left, tmp,
6455 NULL, 0, 0);
6456
6457 xmlExpFree(ctxt, left);
6458 return(tmp);
6459 }
6460 if (right->type == XML_EXP_OR) {
6461 /* Ordering in the tree */
6462 /* C | (A | B) -> A | (B | C) */
6463 if (left->key > right->exp_right->key) {
6464 xmlExpNodePtr tmp;
6465 right->exp_right->ref++;
6466 tmp = xmlExpHashGetEntry(ctxt, XML_EXP_OR, right->exp_right,
6467 left, NULL, 0, 0);
6468 right->exp_left->ref++;
6469 tmp = xmlExpHashGetEntry(ctxt, XML_EXP_OR, right->exp_left,
6470 tmp, NULL, 0, 0);
6471 xmlExpFree(ctxt, right);
6472 return(tmp);
6473 }
6474 /* Ordering in the tree */
6475 /* B | (A | C) -> A | (B | C) */
6476 if (left->key > right->exp_left->key) {
6477 xmlExpNodePtr tmp;
6478 right->exp_right->ref++;
6479 tmp = xmlExpHashGetEntry(ctxt, XML_EXP_OR, left,
6480 right->exp_right, NULL, 0, 0);
6481 right->exp_left->ref++;
6482 tmp = xmlExpHashGetEntry(ctxt, XML_EXP_OR, right->exp_left,
6483 tmp, NULL, 0, 0);
6484 xmlExpFree(ctxt, right);
6485 return(tmp);
6486 }
6487 }
6488 /* we know both types are != XML_EXP_OR here */
6489 else if (left->key > right->key) {
6490 xmlExpNodePtr tmp = left;
6491 left = right;
6492 right = tmp;
6493 }
6494 kbase = xmlExpHashComputeKey(type, left, right);
6495 } else if (type == XML_EXP_SEQ) {
6496 /* Forbid reduction rules */
6497 if (left->type == XML_EXP_FORBID) {
6498 xmlExpFree(ctxt, right);
6499 return(left);
6500 }
6501 if (right->type == XML_EXP_FORBID) {
6502 xmlExpFree(ctxt, left);
6503 return(right);
6504 }
6505 /* Empty reduction rules */
6506 if (right->type == XML_EXP_EMPTY) {
6507 return(left);
6508 }
6509 if (left->type == XML_EXP_EMPTY) {
6510 return(right);
6511 }
6512 kbase = xmlExpHashComputeKey(type, left, right);
6513 } else
6514 return(NULL);
6515
6516 key = kbase % ctxt->size;
6517 if (ctxt->table[key] != NULL) {
6518 for (insert = ctxt->table[key]; insert != NULL;
6519 insert = insert->next) {
6520 if ((insert->key == kbase) &&
6521 (insert->type == type)) {
6522 if (type == XML_EXP_ATOM) {
6523 if (name == insert->exp_str) {
6524 insert->ref++;
6525 return(insert);
6526 }
6527 } else if (type == XML_EXP_COUNT) {
6528 if ((insert->exp_min == min) && (insert->exp_max == max) &&
6529 (insert->exp_left == left)) {
6530 insert->ref++;
6531 left->ref--;
6532 return(insert);
6533 }
6534 } else if ((insert->exp_left == left) &&
6535 (insert->exp_right == right)) {
6536 insert->ref++;
6537 left->ref--;
6538 right->ref--;
6539 return(insert);
6540 }
6541 }
6542 }
6543 }
6544
6545 entry = xmlExpNewNode(ctxt, type);
6546 if (entry == NULL)
6547 return(NULL);
6548 entry->key = kbase;
6549 if (type == XML_EXP_ATOM) {
6550 entry->exp_str = name;
6551 entry->c_max = 1;
6552 } else if (type == XML_EXP_COUNT) {
6553 entry->exp_min = min;
6554 entry->exp_max = max;
6555 entry->exp_left = left;
6556 if ((min == 0) || (IS_NILLABLE(left)))
6557 entry->info |= XML_EXP_NILABLE;
6558 if (max < 0)
6559 entry->c_max = -1;
6560 else
6561 entry->c_max = max * entry->exp_left->c_max;
6562 } else {
6563 entry->exp_left = left;
6564 entry->exp_right = right;
6565 if (type == XML_EXP_OR) {
6566 if ((IS_NILLABLE(left)) || (IS_NILLABLE(right)))
6567 entry->info |= XML_EXP_NILABLE;
6568 if ((entry->exp_left->c_max == -1) ||
6569 (entry->exp_right->c_max == -1))
6570 entry->c_max = -1;
6571 else if (entry->exp_left->c_max > entry->exp_right->c_max)
6572 entry->c_max = entry->exp_left->c_max;
6573 else
6574 entry->c_max = entry->exp_right->c_max;
6575 } else {
6576 if ((IS_NILLABLE(left)) && (IS_NILLABLE(right)))
6577 entry->info |= XML_EXP_NILABLE;
6578 if ((entry->exp_left->c_max == -1) ||
6579 (entry->exp_right->c_max == -1))
6580 entry->c_max = -1;
6581 else
6582 entry->c_max = entry->exp_left->c_max + entry->exp_right->c_max;
6583 }
6584 }
6585 entry->ref = 1;
6586 if (ctxt->table[key] != NULL)
6587 entry->next = ctxt->table[key];
6588
6589 ctxt->table[key] = entry;
6590 ctxt->nbElems++;
6591
6592 return(entry);
6593}
6594
6595/**
6596 * xmlExpFree:
6597 * @ctxt: the expression context
6598 * @exp: the expression
6599 *
6600 * Dereference the expression
6601 */
6602void
6603xmlExpFree(xmlExpCtxtPtr ctxt, xmlExpNodePtr exp) {
6604 if ((exp == NULL) || (exp == forbiddenExp) || (exp == emptyExp))
6605 return;
6606 exp->ref--;
Daniel Veillard81a8ec62005-08-22 00:20:58 +00006607 if (exp->ref == 0) {
6608 unsigned short key;
6609
6610 /* Unlink it first from the hash table */
6611 key = exp->key % ctxt->size;
6612 if (ctxt->table[key] == exp) {
6613 ctxt->table[key] = exp->next;
6614 } else {
6615 xmlExpNodePtr tmp;
6616
6617 tmp = ctxt->table[key];
6618 while (tmp != NULL) {
6619 if (tmp->next == exp) {
6620 tmp->next = exp->next;
6621 break;
6622 }
6623 tmp = tmp->next;
6624 }
6625 }
6626
6627 if ((exp->type == XML_EXP_SEQ) || (exp->type == XML_EXP_OR)) {
6628 xmlExpFree(ctxt, exp->exp_left);
6629 xmlExpFree(ctxt, exp->exp_right);
6630 } else if (exp->type == XML_EXP_COUNT) {
6631 xmlExpFree(ctxt, exp->exp_left);
6632 }
6633 xmlFree(exp);
Daniel Veillard81a8ec62005-08-22 00:20:58 +00006634 ctxt->nb_nodes--;
6635 }
6636}
6637
6638/**
6639 * xmlExpRef:
6640 * @exp: the expression
6641 *
6642 * Increase the reference count of the expression
6643 */
6644void
6645xmlExpRef(xmlExpNodePtr exp) {
6646 if (exp != NULL)
6647 exp->ref++;
6648}
6649
Daniel Veillardccb4d412005-08-23 13:41:17 +00006650/**
6651 * xmlExpNewAtom:
6652 * @ctxt: the expression context
6653 * @name: the atom name
6654 * @len: the atom name lenght in byte (or -1);
6655 *
6656 * Get the atom associated to this name from that context
6657 *
6658 * Returns the node or NULL in case of error
6659 */
6660xmlExpNodePtr
6661xmlExpNewAtom(xmlExpCtxtPtr ctxt, const xmlChar *name, int len) {
6662 if ((ctxt == NULL) || (name == NULL))
6663 return(NULL);
6664 name = xmlDictLookup(ctxt->dict, name, len);
6665 if (name == NULL)
6666 return(NULL);
6667 return(xmlExpHashGetEntry(ctxt, XML_EXP_ATOM, NULL, NULL, name, 0, 0));
6668}
6669
6670/**
6671 * xmlExpNewOr:
6672 * @ctxt: the expression context
6673 * @left: left expression
6674 * @right: right expression
6675 *
6676 * Get the atom associated to the choice @left | @right
6677 * Note that @left and @right are consumed in the operation, to keep
6678 * an handle on them use xmlExpRef() and use xmlExpFree() to release them,
6679 * this is true even in case of failure (unless ctxt == NULL).
6680 *
6681 * Returns the node or NULL in case of error
6682 */
6683xmlExpNodePtr
6684xmlExpNewOr(xmlExpCtxtPtr ctxt, xmlExpNodePtr left, xmlExpNodePtr right) {
Daniel Veillard11ce4002006-03-10 00:36:23 +00006685 if (ctxt == NULL)
6686 return(NULL);
6687 if ((left == NULL) || (right == NULL)) {
Daniel Veillardccb4d412005-08-23 13:41:17 +00006688 xmlExpFree(ctxt, left);
6689 xmlExpFree(ctxt, right);
6690 return(NULL);
6691 }
6692 return(xmlExpHashGetEntry(ctxt, XML_EXP_OR, left, right, NULL, 0, 0));
6693}
6694
6695/**
6696 * xmlExpNewSeq:
6697 * @ctxt: the expression context
6698 * @left: left expression
6699 * @right: right expression
6700 *
6701 * Get the atom associated to the sequence @left , @right
6702 * Note that @left and @right are consumed in the operation, to keep
6703 * an handle on them use xmlExpRef() and use xmlExpFree() to release them,
6704 * this is true even in case of failure (unless ctxt == NULL).
6705 *
6706 * Returns the node or NULL in case of error
6707 */
6708xmlExpNodePtr
6709xmlExpNewSeq(xmlExpCtxtPtr ctxt, xmlExpNodePtr left, xmlExpNodePtr right) {
Daniel Veillard11ce4002006-03-10 00:36:23 +00006710 if (ctxt == NULL)
6711 return(NULL);
6712 if ((left == NULL) || (right == NULL)) {
Daniel Veillardccb4d412005-08-23 13:41:17 +00006713 xmlExpFree(ctxt, left);
6714 xmlExpFree(ctxt, right);
6715 return(NULL);
6716 }
6717 return(xmlExpHashGetEntry(ctxt, XML_EXP_SEQ, left, right, NULL, 0, 0));
6718}
6719
6720/**
6721 * xmlExpNewRange:
6722 * @ctxt: the expression context
6723 * @subset: the expression to be repeated
6724 * @min: the lower bound for the repetition
6725 * @max: the upper bound for the repetition, -1 means infinite
6726 *
6727 * Get the atom associated to the range (@subset){@min, @max}
6728 * Note that @subset is consumed in the operation, to keep
6729 * an handle on it use xmlExpRef() and use xmlExpFree() to release it,
6730 * this is true even in case of failure (unless ctxt == NULL).
6731 *
6732 * Returns the node or NULL in case of error
6733 */
6734xmlExpNodePtr
6735xmlExpNewRange(xmlExpCtxtPtr ctxt, xmlExpNodePtr subset, int min, int max) {
Daniel Veillard11ce4002006-03-10 00:36:23 +00006736 if (ctxt == NULL)
6737 return(NULL);
6738 if ((subset == NULL) || (min < 0) || (max < -1) ||
Daniel Veillardccb4d412005-08-23 13:41:17 +00006739 ((max >= 0) && (min > max))) {
6740 xmlExpFree(ctxt, subset);
6741 return(NULL);
6742 }
6743 return(xmlExpHashGetEntry(ctxt, XML_EXP_COUNT, subset,
6744 NULL, NULL, min, max));
6745}
6746
Daniel Veillard81a8ec62005-08-22 00:20:58 +00006747/************************************************************************
6748 * *
6749 * Public API for operations on expressions *
6750 * *
6751 ************************************************************************/
6752
6753static int
6754xmlExpGetLanguageInt(xmlExpCtxtPtr ctxt, xmlExpNodePtr exp,
6755 const xmlChar**list, int len, int nb) {
6756 int tmp, tmp2;
6757tail:
6758 switch (exp->type) {
6759 case XML_EXP_EMPTY:
6760 return(0);
6761 case XML_EXP_ATOM:
6762 for (tmp = 0;tmp < nb;tmp++)
6763 if (list[tmp] == exp->exp_str)
6764 return(0);
6765 if (nb >= len)
6766 return(-2);
6767 list[nb++] = exp->exp_str;
6768 return(1);
6769 case XML_EXP_COUNT:
6770 exp = exp->exp_left;
6771 goto tail;
6772 case XML_EXP_SEQ:
6773 case XML_EXP_OR:
6774 tmp = xmlExpGetLanguageInt(ctxt, exp->exp_left, list, len, nb);
6775 if (tmp < 0)
6776 return(tmp);
6777 tmp2 = xmlExpGetLanguageInt(ctxt, exp->exp_right, list, len,
6778 nb + tmp);
6779 if (tmp2 < 0)
6780 return(tmp2);
6781 return(tmp + tmp2);
6782 }
6783 return(-1);
6784}
6785
6786/**
6787 * xmlExpGetLanguage:
6788 * @ctxt: the expression context
6789 * @exp: the expression
Daniel Veillard7802ba52005-10-27 11:56:20 +00006790 * @langList: where to store the tokens
Daniel Veillard81a8ec62005-08-22 00:20:58 +00006791 * @len: the allocated lenght of @list
6792 *
6793 * Find all the strings used in @exp and store them in @list
6794 *
6795 * Returns the number of unique strings found, -1 in case of errors and
6796 * -2 if there is more than @len strings
6797 */
6798int
6799xmlExpGetLanguage(xmlExpCtxtPtr ctxt, xmlExpNodePtr exp,
Daniel Veillard7802ba52005-10-27 11:56:20 +00006800 const xmlChar**langList, int len) {
6801 if ((ctxt == NULL) || (exp == NULL) || (langList == NULL) || (len <= 0))
Daniel Veillard81a8ec62005-08-22 00:20:58 +00006802 return(-1);
Daniel Veillard7802ba52005-10-27 11:56:20 +00006803 return(xmlExpGetLanguageInt(ctxt, exp, langList, len, 0));
Daniel Veillard81a8ec62005-08-22 00:20:58 +00006804}
6805
6806static int
6807xmlExpGetStartInt(xmlExpCtxtPtr ctxt, xmlExpNodePtr exp,
6808 const xmlChar**list, int len, int nb) {
6809 int tmp, tmp2;
6810tail:
6811 switch (exp->type) {
6812 case XML_EXP_FORBID:
6813 return(0);
6814 case XML_EXP_EMPTY:
6815 return(0);
6816 case XML_EXP_ATOM:
6817 for (tmp = 0;tmp < nb;tmp++)
6818 if (list[tmp] == exp->exp_str)
6819 return(0);
6820 if (nb >= len)
6821 return(-2);
6822 list[nb++] = exp->exp_str;
6823 return(1);
6824 case XML_EXP_COUNT:
6825 exp = exp->exp_left;
6826 goto tail;
6827 case XML_EXP_SEQ:
6828 tmp = xmlExpGetStartInt(ctxt, exp->exp_left, list, len, nb);
6829 if (tmp < 0)
6830 return(tmp);
6831 if (IS_NILLABLE(exp->exp_left)) {
6832 tmp2 = xmlExpGetStartInt(ctxt, exp->exp_right, list, len,
6833 nb + tmp);
6834 if (tmp2 < 0)
6835 return(tmp2);
6836 tmp += tmp2;
6837 }
6838 return(tmp);
6839 case XML_EXP_OR:
6840 tmp = xmlExpGetStartInt(ctxt, exp->exp_left, list, len, nb);
6841 if (tmp < 0)
6842 return(tmp);
6843 tmp2 = xmlExpGetStartInt(ctxt, exp->exp_right, list, len,
6844 nb + tmp);
6845 if (tmp2 < 0)
6846 return(tmp2);
6847 return(tmp + tmp2);
6848 }
6849 return(-1);
6850}
6851
6852/**
6853 * xmlExpGetStart:
6854 * @ctxt: the expression context
6855 * @exp: the expression
Daniel Veillard7802ba52005-10-27 11:56:20 +00006856 * @tokList: where to store the tokens
Daniel Veillard81a8ec62005-08-22 00:20:58 +00006857 * @len: the allocated lenght of @list
6858 *
6859 * Find all the strings that appears at the start of the languages
6860 * accepted by @exp and store them in @list. E.g. for (a, b) | c
6861 * it will return the list [a, c]
6862 *
6863 * Returns the number of unique strings found, -1 in case of errors and
6864 * -2 if there is more than @len strings
6865 */
6866int
6867xmlExpGetStart(xmlExpCtxtPtr ctxt, xmlExpNodePtr exp,
Daniel Veillard7802ba52005-10-27 11:56:20 +00006868 const xmlChar**tokList, int len) {
6869 if ((ctxt == NULL) || (exp == NULL) || (tokList == NULL) || (len <= 0))
Daniel Veillard81a8ec62005-08-22 00:20:58 +00006870 return(-1);
Daniel Veillard7802ba52005-10-27 11:56:20 +00006871 return(xmlExpGetStartInt(ctxt, exp, tokList, len, 0));
Daniel Veillard81a8ec62005-08-22 00:20:58 +00006872}
6873
6874/**
6875 * xmlExpIsNillable:
6876 * @exp: the expression
6877 *
6878 * Finds if the expression is nillable, i.e. if it accepts the empty sequqnce
6879 *
6880 * Returns 1 if nillable, 0 if not and -1 in case of error
6881 */
6882int
6883xmlExpIsNillable(xmlExpNodePtr exp) {
6884 if (exp == NULL)
6885 return(-1);
6886 return(IS_NILLABLE(exp) != 0);
6887}
6888
6889static xmlExpNodePtr
6890xmlExpStringDeriveInt(xmlExpCtxtPtr ctxt, xmlExpNodePtr exp, const xmlChar *str)
6891{
6892 xmlExpNodePtr ret;
6893
6894 switch (exp->type) {
6895 case XML_EXP_EMPTY:
6896 return(forbiddenExp);
6897 case XML_EXP_FORBID:
6898 return(forbiddenExp);
6899 case XML_EXP_ATOM:
6900 if (exp->exp_str == str) {
6901#ifdef DEBUG_DERIV
6902 printf("deriv atom: equal => Empty\n");
6903#endif
6904 ret = emptyExp;
6905 } else {
6906#ifdef DEBUG_DERIV
6907 printf("deriv atom: mismatch => forbid\n");
6908#endif
6909 /* TODO wildcards here */
6910 ret = forbiddenExp;
6911 }
6912 return(ret);
6913 case XML_EXP_OR: {
6914 xmlExpNodePtr tmp;
6915
6916#ifdef DEBUG_DERIV
6917 printf("deriv or: => or(derivs)\n");
6918#endif
6919 tmp = xmlExpStringDeriveInt(ctxt, exp->exp_left, str);
6920 if (tmp == NULL) {
6921 return(NULL);
6922 }
6923 ret = xmlExpStringDeriveInt(ctxt, exp->exp_right, str);
6924 if (ret == NULL) {
6925 xmlExpFree(ctxt, tmp);
6926 return(NULL);
6927 }
6928 ret = xmlExpHashGetEntry(ctxt, XML_EXP_OR, tmp, ret,
6929 NULL, 0, 0);
6930 return(ret);
6931 }
6932 case XML_EXP_SEQ:
6933#ifdef DEBUG_DERIV
6934 printf("deriv seq: starting with left\n");
6935#endif
6936 ret = xmlExpStringDeriveInt(ctxt, exp->exp_left, str);
6937 if (ret == NULL) {
6938 return(NULL);
6939 } else if (ret == forbiddenExp) {
6940 if (IS_NILLABLE(exp->exp_left)) {
6941#ifdef DEBUG_DERIV
6942 printf("deriv seq: left failed but nillable\n");
6943#endif
6944 ret = xmlExpStringDeriveInt(ctxt, exp->exp_right, str);
6945 }
6946 } else {
6947#ifdef DEBUG_DERIV
6948 printf("deriv seq: left match => sequence\n");
6949#endif
6950 exp->exp_right->ref++;
6951 ret = xmlExpHashGetEntry(ctxt, XML_EXP_SEQ, ret, exp->exp_right,
6952 NULL, 0, 0);
6953 }
6954 return(ret);
6955 case XML_EXP_COUNT: {
6956 int min, max;
6957 xmlExpNodePtr tmp;
6958
6959 if (exp->exp_max == 0)
6960 return(forbiddenExp);
6961 ret = xmlExpStringDeriveInt(ctxt, exp->exp_left, str);
6962 if (ret == NULL)
6963 return(NULL);
6964 if (ret == forbiddenExp) {
6965#ifdef DEBUG_DERIV
6966 printf("deriv count: pattern mismatch => forbid\n");
6967#endif
6968 return(ret);
6969 }
6970 if (exp->exp_max == 1)
6971 return(ret);
6972 if (exp->exp_max < 0) /* unbounded */
6973 max = -1;
6974 else
6975 max = exp->exp_max - 1;
6976 if (exp->exp_min > 0)
6977 min = exp->exp_min - 1;
6978 else
6979 min = 0;
6980 exp->exp_left->ref++;
6981 tmp = xmlExpHashGetEntry(ctxt, XML_EXP_COUNT, exp->exp_left, NULL,
6982 NULL, min, max);
6983 if (ret == emptyExp) {
6984#ifdef DEBUG_DERIV
6985 printf("deriv count: match to empty => new count\n");
6986#endif
6987 return(tmp);
6988 }
6989#ifdef DEBUG_DERIV
6990 printf("deriv count: match => sequence with new count\n");
6991#endif
6992 return(xmlExpHashGetEntry(ctxt, XML_EXP_SEQ, ret, tmp,
6993 NULL, 0, 0));
6994 }
6995 }
6996 return(NULL);
6997}
6998
6999/**
7000 * xmlExpStringDerive:
7001 * @ctxt: the expression context
7002 * @exp: the expression
7003 * @str: the string
7004 * @len: the string len in bytes if available
7005 *
7006 * Do one step of Brzozowski derivation of the expression @exp with
7007 * respect to the input string
7008 *
7009 * Returns the resulting expression or NULL in case of internal error
7010 */
7011xmlExpNodePtr
7012xmlExpStringDerive(xmlExpCtxtPtr ctxt, xmlExpNodePtr exp,
7013 const xmlChar *str, int len) {
7014 const xmlChar *input;
7015
7016 if ((exp == NULL) || (ctxt == NULL) || (str == NULL)) {
7017 return(NULL);
7018 }
7019 /*
7020 * check the string is in the dictionnary, if yes use an interned
7021 * copy, otherwise we know it's not an acceptable input
7022 */
7023 input = xmlDictExists(ctxt->dict, str, len);
7024 if (input == NULL) {
7025 return(forbiddenExp);
7026 }
7027 return(xmlExpStringDeriveInt(ctxt, exp, input));
7028}
7029
7030static int
7031xmlExpCheckCard(xmlExpNodePtr exp, xmlExpNodePtr sub) {
7032 int ret = 1;
7033
7034 if (sub->c_max == -1) {
7035 if (exp->c_max != -1)
7036 ret = 0;
7037 } else if ((exp->c_max >= 0) && (exp->c_max < sub->c_max)) {
7038 ret = 0;
7039 }
7040#if 0
7041 if ((IS_NILLABLE(sub)) && (!IS_NILLABLE(exp)))
7042 ret = 0;
7043#endif
7044 return(ret);
7045}
7046
7047static xmlExpNodePtr xmlExpExpDeriveInt(xmlExpCtxtPtr ctxt, xmlExpNodePtr exp,
7048 xmlExpNodePtr sub);
7049/**
7050 * xmlExpDivide:
7051 * @ctxt: the expressions context
7052 * @exp: the englobing expression
7053 * @sub: the subexpression
7054 * @mult: the multiple expression
7055 * @remain: the remain from the derivation of the multiple
7056 *
7057 * Check if exp is a multiple of sub, i.e. if there is a finite number n
7058 * so that sub{n} subsume exp
7059 *
7060 * Returns the multiple value if successful, 0 if it is not a multiple
7061 * and -1 in case of internel error.
7062 */
7063
7064static int
7065xmlExpDivide(xmlExpCtxtPtr ctxt, xmlExpNodePtr exp, xmlExpNodePtr sub,
7066 xmlExpNodePtr *mult, xmlExpNodePtr *remain) {
7067 int i;
7068 xmlExpNodePtr tmp, tmp2;
7069
7070 if (mult != NULL) *mult = NULL;
7071 if (remain != NULL) *remain = NULL;
7072 if (exp->c_max == -1) return(0);
7073 if (IS_NILLABLE(exp) && (!IS_NILLABLE(sub))) return(0);
7074
7075 for (i = 1;i <= exp->c_max;i++) {
7076 sub->ref++;
7077 tmp = xmlExpHashGetEntry(ctxt, XML_EXP_COUNT,
7078 sub, NULL, NULL, i, i);
7079 if (tmp == NULL) {
7080 return(-1);
7081 }
7082 if (!xmlExpCheckCard(tmp, exp)) {
7083 xmlExpFree(ctxt, tmp);
7084 continue;
7085 }
7086 tmp2 = xmlExpExpDeriveInt(ctxt, tmp, exp);
7087 if (tmp2 == NULL) {
7088 xmlExpFree(ctxt, tmp);
7089 return(-1);
7090 }
7091 if ((tmp2 != forbiddenExp) && (IS_NILLABLE(tmp2))) {
7092 if (remain != NULL)
7093 *remain = tmp2;
7094 else
7095 xmlExpFree(ctxt, tmp2);
7096 if (mult != NULL)
7097 *mult = tmp;
7098 else
7099 xmlExpFree(ctxt, tmp);
7100#ifdef DEBUG_DERIV
7101 printf("Divide succeeded %d\n", i);
7102#endif
7103 return(i);
7104 }
7105 xmlExpFree(ctxt, tmp);
7106 xmlExpFree(ctxt, tmp2);
7107 }
7108#ifdef DEBUG_DERIV
7109 printf("Divide failed\n");
7110#endif
7111 return(0);
7112}
7113
7114/**
7115 * xmlExpExpDeriveInt:
7116 * @ctxt: the expressions context
7117 * @exp: the englobing expression
7118 * @sub: the subexpression
7119 *
7120 * Try to do a step of Brzozowski derivation but at a higher level
7121 * the input being a subexpression.
7122 *
7123 * Returns the resulting expression or NULL in case of internal error
7124 */
7125static xmlExpNodePtr
7126xmlExpExpDeriveInt(xmlExpCtxtPtr ctxt, xmlExpNodePtr exp, xmlExpNodePtr sub) {
7127 xmlExpNodePtr ret, tmp, tmp2, tmp3;
7128 const xmlChar **tab;
7129 int len, i;
7130
7131 /*
7132 * In case of equality and if the expression can only consume a finite
7133 * amount, then the derivation is empty
7134 */
7135 if ((exp == sub) && (exp->c_max >= 0)) {
7136#ifdef DEBUG_DERIV
7137 printf("Equal(exp, sub) and finite -> Empty\n");
7138#endif
7139 return(emptyExp);
7140 }
7141 /*
7142 * decompose sub sequence first
7143 */
7144 if (sub->type == XML_EXP_EMPTY) {
7145#ifdef DEBUG_DERIV
7146 printf("Empty(sub) -> Empty\n");
7147#endif
7148 exp->ref++;
7149 return(exp);
7150 }
7151 if (sub->type == XML_EXP_SEQ) {
7152#ifdef DEBUG_DERIV
7153 printf("Seq(sub) -> decompose\n");
7154#endif
7155 tmp = xmlExpExpDeriveInt(ctxt, exp, sub->exp_left);
7156 if (tmp == NULL)
7157 return(NULL);
7158 if (tmp == forbiddenExp)
7159 return(tmp);
7160 ret = xmlExpExpDeriveInt(ctxt, tmp, sub->exp_right);
7161 xmlExpFree(ctxt, tmp);
7162 return(ret);
7163 }
7164 if (sub->type == XML_EXP_OR) {
7165#ifdef DEBUG_DERIV
7166 printf("Or(sub) -> decompose\n");
7167#endif
7168 tmp = xmlExpExpDeriveInt(ctxt, exp, sub->exp_left);
7169 if (tmp == forbiddenExp)
7170 return(tmp);
7171 if (tmp == NULL)
7172 return(NULL);
7173 ret = xmlExpExpDeriveInt(ctxt, exp, sub->exp_right);
7174 if ((ret == NULL) || (ret == forbiddenExp)) {
7175 xmlExpFree(ctxt, tmp);
7176 return(ret);
7177 }
7178 return(xmlExpHashGetEntry(ctxt, XML_EXP_OR, tmp, ret, NULL, 0, 0));
7179 }
7180 if (!xmlExpCheckCard(exp, sub)) {
7181#ifdef DEBUG_DERIV
7182 printf("CheckCard(exp, sub) failed -> Forbid\n");
7183#endif
7184 return(forbiddenExp);
7185 }
7186 switch (exp->type) {
7187 case XML_EXP_EMPTY:
7188 if (sub == emptyExp)
7189 return(emptyExp);
7190#ifdef DEBUG_DERIV
7191 printf("Empty(exp) -> Forbid\n");
7192#endif
7193 return(forbiddenExp);
7194 case XML_EXP_FORBID:
7195#ifdef DEBUG_DERIV
7196 printf("Forbid(exp) -> Forbid\n");
7197#endif
7198 return(forbiddenExp);
7199 case XML_EXP_ATOM:
7200 if (sub->type == XML_EXP_ATOM) {
7201 /* TODO: handle wildcards */
7202 if (exp->exp_str == sub->exp_str) {
7203#ifdef DEBUG_DERIV
7204 printf("Atom match -> Empty\n");
7205#endif
7206 return(emptyExp);
7207 }
7208#ifdef DEBUG_DERIV
7209 printf("Atom mismatch -> Forbid\n");
7210#endif
7211 return(forbiddenExp);
7212 }
7213 if ((sub->type == XML_EXP_COUNT) &&
7214 (sub->exp_max == 1) &&
7215 (sub->exp_left->type == XML_EXP_ATOM)) {
7216 /* TODO: handle wildcards */
7217 if (exp->exp_str == sub->exp_left->exp_str) {
7218#ifdef DEBUG_DERIV
7219 printf("Atom match -> Empty\n");
7220#endif
7221 return(emptyExp);
7222 }
7223#ifdef DEBUG_DERIV
7224 printf("Atom mismatch -> Forbid\n");
7225#endif
7226 return(forbiddenExp);
7227 }
7228#ifdef DEBUG_DERIV
7229 printf("Compex exp vs Atom -> Forbid\n");
7230#endif
7231 return(forbiddenExp);
7232 case XML_EXP_SEQ:
7233 /* try to get the sequence consumed only if possible */
7234 if (xmlExpCheckCard(exp->exp_left, sub)) {
7235 /* See if the sequence can be consumed directly */
7236#ifdef DEBUG_DERIV
7237 printf("Seq trying left only\n");
7238#endif
7239 ret = xmlExpExpDeriveInt(ctxt, exp->exp_left, sub);
7240 if ((ret != forbiddenExp) && (ret != NULL)) {
7241#ifdef DEBUG_DERIV
7242 printf("Seq trying left only worked\n");
7243#endif
7244 /*
7245 * TODO: assumption here that we are determinist
7246 * i.e. we won't get to a nillable exp left
7247 * subset which could be matched by the right
7248 * part too.
7249 * e.g.: (a | b)+,(a | c) and 'a+,a'
7250 */
7251 exp->exp_right->ref++;
7252 return(xmlExpHashGetEntry(ctxt, XML_EXP_SEQ, ret,
7253 exp->exp_right, NULL, 0, 0));
7254 }
7255#ifdef DEBUG_DERIV
7256 } else {
7257 printf("Seq: left too short\n");
7258#endif
7259 }
7260 /* Try instead to decompose */
7261 if (sub->type == XML_EXP_COUNT) {
7262 int min, max;
7263
7264#ifdef DEBUG_DERIV
7265 printf("Seq: sub is a count\n");
7266#endif
7267 ret = xmlExpExpDeriveInt(ctxt, exp->exp_left, sub->exp_left);
7268 if (ret == NULL)
7269 return(NULL);
7270 if (ret != forbiddenExp) {
7271#ifdef DEBUG_DERIV
7272 printf("Seq , Count match on left\n");
7273#endif
7274 if (sub->exp_max < 0)
7275 max = -1;
7276 else
7277 max = sub->exp_max -1;
7278 if (sub->exp_min > 0)
7279 min = sub->exp_min -1;
7280 else
7281 min = 0;
7282 exp->exp_right->ref++;
7283 tmp = xmlExpHashGetEntry(ctxt, XML_EXP_SEQ, ret,
7284 exp->exp_right, NULL, 0, 0);
7285 if (tmp == NULL)
7286 return(NULL);
7287
7288 sub->exp_left->ref++;
7289 tmp2 = xmlExpHashGetEntry(ctxt, XML_EXP_COUNT,
7290 sub->exp_left, NULL, NULL, min, max);
7291 if (tmp2 == NULL) {
7292 xmlExpFree(ctxt, tmp);
7293 return(NULL);
7294 }
7295 ret = xmlExpExpDeriveInt(ctxt, tmp, tmp2);
7296 xmlExpFree(ctxt, tmp);
7297 xmlExpFree(ctxt, tmp2);
7298 return(ret);
7299 }
7300 }
7301 /* we made no progress on structured operations */
7302 break;
7303 case XML_EXP_OR:
7304#ifdef DEBUG_DERIV
7305 printf("Or , trying both side\n");
7306#endif
7307 ret = xmlExpExpDeriveInt(ctxt, exp->exp_left, sub);
7308 if (ret == NULL)
7309 return(NULL);
7310 tmp = xmlExpExpDeriveInt(ctxt, exp->exp_right, sub);
7311 if (tmp == NULL) {
7312 xmlExpFree(ctxt, ret);
7313 return(NULL);
7314 }
7315 return(xmlExpHashGetEntry(ctxt, XML_EXP_OR, ret, tmp, NULL, 0, 0));
7316 case XML_EXP_COUNT: {
7317 int min, max;
7318
7319 if (sub->type == XML_EXP_COUNT) {
7320 /*
7321 * Try to see if the loop is completely subsumed
7322 */
7323 tmp = xmlExpExpDeriveInt(ctxt, exp->exp_left, sub->exp_left);
7324 if (tmp == NULL)
7325 return(NULL);
7326 if (tmp == forbiddenExp) {
7327 int mult;
7328
7329#ifdef DEBUG_DERIV
7330 printf("Count, Count inner don't subsume\n");
7331#endif
7332 mult = xmlExpDivide(ctxt, sub->exp_left, exp->exp_left,
7333 NULL, &tmp);
7334 if (mult <= 0) {
7335#ifdef DEBUG_DERIV
7336 printf("Count, Count not multiple => forbidden\n");
7337#endif
7338 return(forbiddenExp);
7339 }
7340 if (sub->exp_max == -1) {
7341 max = -1;
7342 if (exp->exp_max == -1) {
7343 if (exp->exp_min <= sub->exp_min * mult)
7344 min = 0;
7345 else
7346 min = exp->exp_min - sub->exp_min * mult;
7347 } else {
7348#ifdef DEBUG_DERIV
7349 printf("Count, Count finite can't subsume infinite\n");
7350#endif
7351 xmlExpFree(ctxt, tmp);
7352 return(forbiddenExp);
7353 }
7354 } else {
7355 if (exp->exp_max == -1) {
7356#ifdef DEBUG_DERIV
7357 printf("Infinite loop consume mult finite loop\n");
7358#endif
7359 if (exp->exp_min > sub->exp_min * mult) {
7360 max = -1;
7361 min = exp->exp_min - sub->exp_min * mult;
7362 } else {
7363 max = -1;
7364 min = 0;
7365 }
7366 } else {
7367 if (exp->exp_max < sub->exp_max * mult) {
7368#ifdef DEBUG_DERIV
7369 printf("loops max mult mismatch => forbidden\n");
7370#endif
7371 xmlExpFree(ctxt, tmp);
7372 return(forbiddenExp);
7373 }
7374 if (sub->exp_max * mult > exp->exp_min)
7375 min = 0;
7376 else
7377 min = exp->exp_min - sub->exp_max * mult;
7378 max = exp->exp_max - sub->exp_max * mult;
7379 }
7380 }
7381 } else if (!IS_NILLABLE(tmp)) {
7382 /*
7383 * TODO: loop here to try to grow if working on finite
7384 * blocks.
7385 */
7386#ifdef DEBUG_DERIV
7387 printf("Count, Count remain not nillable => forbidden\n");
7388#endif
7389 xmlExpFree(ctxt, tmp);
7390 return(forbiddenExp);
7391 } else if (sub->exp_max == -1) {
7392 if (exp->exp_max == -1) {
7393 if (exp->exp_min <= sub->exp_min) {
7394#ifdef DEBUG_DERIV
7395 printf("Infinite loops Okay => COUNT(0,Inf)\n");
7396#endif
7397 max = -1;
7398 min = 0;
7399 } else {
7400#ifdef DEBUG_DERIV
7401 printf("Infinite loops min => Count(X,Inf)\n");
7402#endif
7403 max = -1;
7404 min = exp->exp_min - sub->exp_min;
7405 }
7406 } else if (exp->exp_min > sub->exp_min) {
7407#ifdef DEBUG_DERIV
7408 printf("loops min mismatch 1 => forbidden ???\n");
7409#endif
7410 xmlExpFree(ctxt, tmp);
7411 return(forbiddenExp);
7412 } else {
7413 max = -1;
7414 min = 0;
7415 }
7416 } else {
7417 if (exp->exp_max == -1) {
7418#ifdef DEBUG_DERIV
7419 printf("Infinite loop consume finite loop\n");
7420#endif
7421 if (exp->exp_min > sub->exp_min) {
7422 max = -1;
7423 min = exp->exp_min - sub->exp_min;
7424 } else {
7425 max = -1;
7426 min = 0;
7427 }
7428 } else {
7429 if (exp->exp_max < sub->exp_max) {
7430#ifdef DEBUG_DERIV
7431 printf("loops max mismatch => forbidden\n");
7432#endif
7433 xmlExpFree(ctxt, tmp);
7434 return(forbiddenExp);
7435 }
7436 if (sub->exp_max > exp->exp_min)
7437 min = 0;
7438 else
7439 min = exp->exp_min - sub->exp_max;
7440 max = exp->exp_max - sub->exp_max;
7441 }
7442 }
7443#ifdef DEBUG_DERIV
7444 printf("loops match => SEQ(COUNT())\n");
7445#endif
7446 exp->exp_left->ref++;
7447 tmp2 = xmlExpHashGetEntry(ctxt, XML_EXP_COUNT, exp->exp_left,
7448 NULL, NULL, min, max);
7449 if (tmp2 == NULL) {
7450 return(NULL);
7451 }
7452 ret = xmlExpHashGetEntry(ctxt, XML_EXP_SEQ, tmp, tmp2,
7453 NULL, 0, 0);
7454 return(ret);
7455 }
7456 tmp = xmlExpExpDeriveInt(ctxt, exp->exp_left, sub);
7457 if (tmp == NULL)
7458 return(NULL);
7459 if (tmp == forbiddenExp) {
7460#ifdef DEBUG_DERIV
7461 printf("loop mismatch => forbidden\n");
7462#endif
7463 return(forbiddenExp);
7464 }
7465 if (exp->exp_min > 0)
7466 min = exp->exp_min - 1;
7467 else
7468 min = 0;
7469 if (exp->exp_max < 0)
7470 max = -1;
7471 else
7472 max = exp->exp_max - 1;
7473
7474#ifdef DEBUG_DERIV
7475 printf("loop match => SEQ(COUNT())\n");
7476#endif
7477 exp->exp_left->ref++;
7478 tmp2 = xmlExpHashGetEntry(ctxt, XML_EXP_COUNT, exp->exp_left,
7479 NULL, NULL, min, max);
7480 if (tmp2 == NULL)
7481 return(NULL);
7482 ret = xmlExpHashGetEntry(ctxt, XML_EXP_SEQ, tmp, tmp2,
7483 NULL, 0, 0);
7484 return(ret);
7485 }
7486 }
7487
Daniel Veillardccb4d412005-08-23 13:41:17 +00007488#ifdef DEBUG_DERIV
7489 printf("Fallback to derivative\n");
7490#endif
7491 if (IS_NILLABLE(sub)) {
7492 if (!(IS_NILLABLE(exp)))
7493 return(forbiddenExp);
7494 else
7495 ret = emptyExp;
7496 } else
7497 ret = NULL;
Daniel Veillard81a8ec62005-08-22 00:20:58 +00007498 /*
7499 * here the structured derivation made no progress so
7500 * we use the default token based derivation to force one more step
7501 */
7502 if (ctxt->tabSize == 0)
7503 ctxt->tabSize = 40;
7504
7505 tab = (const xmlChar **) xmlMalloc(ctxt->tabSize *
7506 sizeof(const xmlChar *));
7507 if (tab == NULL) {
7508 return(NULL);
7509 }
7510
Daniel Veillard81a8ec62005-08-22 00:20:58 +00007511 /*
7512 * collect all the strings accepted by the subexpression on input
7513 */
7514 len = xmlExpGetStartInt(ctxt, sub, tab, ctxt->tabSize, 0);
7515 while (len < 0) {
7516 const xmlChar **temp;
Rob Richards54a8f672005-10-07 02:33:00 +00007517 temp = (const xmlChar **) xmlRealloc((xmlChar **) tab, ctxt->tabSize * 2 *
Daniel Veillard81a8ec62005-08-22 00:20:58 +00007518 sizeof(const xmlChar *));
7519 if (temp == NULL) {
Rob Richards54a8f672005-10-07 02:33:00 +00007520 xmlFree((xmlChar **) tab);
Daniel Veillard81a8ec62005-08-22 00:20:58 +00007521 return(NULL);
7522 }
7523 tab = temp;
7524 ctxt->tabSize *= 2;
7525 len = xmlExpGetStartInt(ctxt, sub, tab, ctxt->tabSize, 0);
7526 }
Daniel Veillard81a8ec62005-08-22 00:20:58 +00007527 for (i = 0;i < len;i++) {
7528 tmp = xmlExpStringDeriveInt(ctxt, exp, tab[i]);
7529 if ((tmp == NULL) || (tmp == forbiddenExp)) {
7530 xmlExpFree(ctxt, ret);
Rob Richards54a8f672005-10-07 02:33:00 +00007531 xmlFree((xmlChar **) tab);
Daniel Veillard81a8ec62005-08-22 00:20:58 +00007532 return(tmp);
7533 }
7534 tmp2 = xmlExpStringDeriveInt(ctxt, sub, tab[i]);
7535 if ((tmp2 == NULL) || (tmp2 == forbiddenExp)) {
7536 xmlExpFree(ctxt, tmp);
7537 xmlExpFree(ctxt, ret);
Rob Richards54a8f672005-10-07 02:33:00 +00007538 xmlFree((xmlChar **) tab);
Daniel Veillard81a8ec62005-08-22 00:20:58 +00007539 return(tmp);
7540 }
7541 tmp3 = xmlExpExpDeriveInt(ctxt, tmp, tmp2);
7542 xmlExpFree(ctxt, tmp);
7543 xmlExpFree(ctxt, tmp2);
7544
7545 if ((tmp3 == NULL) || (tmp3 == forbiddenExp)) {
7546 xmlExpFree(ctxt, ret);
Rob Richards54a8f672005-10-07 02:33:00 +00007547 xmlFree((xmlChar **) tab);
Daniel Veillard81a8ec62005-08-22 00:20:58 +00007548 return(tmp3);
7549 }
7550
7551 if (ret == NULL)
7552 ret = tmp3;
7553 else {
7554 ret = xmlExpHashGetEntry(ctxt, XML_EXP_OR, ret, tmp3, NULL, 0, 0);
7555 if (ret == NULL) {
Rob Richards54a8f672005-10-07 02:33:00 +00007556 xmlFree((xmlChar **) tab);
Daniel Veillard81a8ec62005-08-22 00:20:58 +00007557 return(NULL);
7558 }
7559 }
7560 }
Rob Richards54a8f672005-10-07 02:33:00 +00007561 xmlFree((xmlChar **) tab);
Daniel Veillard81a8ec62005-08-22 00:20:58 +00007562 return(ret);
7563}
7564
7565/**
Daniel Veillard0090bd52005-08-22 14:43:43 +00007566 * xmlExpExpDerive:
7567 * @ctxt: the expressions context
7568 * @exp: the englobing expression
7569 * @sub: the subexpression
7570 *
7571 * Evaluates the expression resulting from @exp consuming a sub expression @sub
7572 * Based on algebraic derivation and sometimes direct Brzozowski derivation
7573 * it usually tatkes less than linear time and can handle expressions generating
7574 * infinite languages.
7575 *
7576 * Returns the resulting expression or NULL in case of internal error, the
7577 * result must be freed
7578 */
7579xmlExpNodePtr
7580xmlExpExpDerive(xmlExpCtxtPtr ctxt, xmlExpNodePtr exp, xmlExpNodePtr sub) {
7581 if ((exp == NULL) || (ctxt == NULL) || (sub == NULL))
7582 return(NULL);
7583
7584 /*
7585 * O(1) speedups
7586 */
7587 if (IS_NILLABLE(sub) && (!IS_NILLABLE(exp))) {
7588#ifdef DEBUG_DERIV
7589 printf("Sub nillable and not exp : can't subsume\n");
7590#endif
7591 return(forbiddenExp);
7592 }
7593 if (xmlExpCheckCard(exp, sub) == 0) {
7594#ifdef DEBUG_DERIV
7595 printf("sub generate longuer sequances than exp : can't subsume\n");
7596#endif
7597 return(forbiddenExp);
7598 }
7599 return(xmlExpExpDeriveInt(ctxt, exp, sub));
7600}
7601
7602/**
Daniel Veillard81a8ec62005-08-22 00:20:58 +00007603 * xmlExpSubsume:
7604 * @ctxt: the expressions context
7605 * @exp: the englobing expression
7606 * @sub: the subexpression
7607 *
7608 * Check whether @exp accepts all the languages accexpted by @sub
7609 * the input being a subexpression.
7610 *
7611 * Returns 1 if true 0 if false and -1 in case of failure.
7612 */
7613int
7614xmlExpSubsume(xmlExpCtxtPtr ctxt, xmlExpNodePtr exp, xmlExpNodePtr sub) {
7615 xmlExpNodePtr tmp;
7616
7617 if ((exp == NULL) || (ctxt == NULL) || (sub == NULL))
7618 return(-1);
7619
7620 /*
7621 * TODO: speedup by checking the language of sub is a subset of the
7622 * language of exp
7623 */
7624 /*
7625 * O(1) speedups
7626 */
7627 if (IS_NILLABLE(sub) && (!IS_NILLABLE(exp))) {
7628#ifdef DEBUG_DERIV
7629 printf("Sub nillable and not exp : can't subsume\n");
7630#endif
7631 return(0);
7632 }
7633 if (xmlExpCheckCard(exp, sub) == 0) {
7634#ifdef DEBUG_DERIV
7635 printf("sub generate longuer sequances than exp : can't subsume\n");
7636#endif
7637 return(0);
7638 }
7639 tmp = xmlExpExpDeriveInt(ctxt, exp, sub);
7640#ifdef DEBUG_DERIV
7641 printf("Result derivation :\n");
7642 PRINT_EXP(tmp);
7643#endif
7644 if (tmp == NULL)
7645 return(-1);
7646 if (tmp == forbiddenExp)
7647 return(0);
7648 if (tmp == emptyExp)
7649 return(1);
7650 if ((tmp != NULL) && (IS_NILLABLE(tmp))) {
7651 xmlExpFree(ctxt, tmp);
7652 return(1);
7653 }
7654 xmlExpFree(ctxt, tmp);
7655 return(0);
7656}
Daniel Veillard465a0002005-08-22 12:07:04 +00007657
7658/************************************************************************
7659 * *
7660 * Parsing expression *
7661 * *
7662 ************************************************************************/
7663
7664static xmlExpNodePtr xmlExpParseExpr(xmlExpCtxtPtr ctxt);
7665
7666#undef CUR
7667#define CUR (*ctxt->cur)
7668#undef NEXT
7669#define NEXT ctxt->cur++;
7670#undef IS_BLANK
7671#define IS_BLANK(c) ((c == ' ') || (c == '\n') || (c == '\r') || (c == '\t'))
7672#define SKIP_BLANKS while (IS_BLANK(*ctxt->cur)) ctxt->cur++;
7673
7674static int
7675xmlExpParseNumber(xmlExpCtxtPtr ctxt) {
7676 int ret = 0;
7677
7678 SKIP_BLANKS
7679 if (CUR == '*') {
7680 NEXT
7681 return(-1);
7682 }
7683 if ((CUR < '0') || (CUR > '9'))
7684 return(-1);
7685 while ((CUR >= '0') && (CUR <= '9')) {
7686 ret = ret * 10 + (CUR - '0');
7687 NEXT
7688 }
7689 return(ret);
7690}
7691
7692static xmlExpNodePtr
7693xmlExpParseOr(xmlExpCtxtPtr ctxt) {
7694 const char *base;
7695 xmlExpNodePtr ret;
7696 const xmlChar *val;
7697
7698 SKIP_BLANKS
7699 base = ctxt->cur;
7700 if (*ctxt->cur == '(') {
7701 NEXT
7702 ret = xmlExpParseExpr(ctxt);
7703 SKIP_BLANKS
7704 if (*ctxt->cur != ')') {
7705 fprintf(stderr, "unbalanced '(' : %s\n", base);
7706 xmlExpFree(ctxt, ret);
7707 return(NULL);
7708 }
7709 NEXT;
7710 SKIP_BLANKS
7711 goto parse_quantifier;
7712 }
7713 while ((CUR != 0) && (!(IS_BLANK(CUR))) && (CUR != '(') &&
7714 (CUR != ')') && (CUR != '|') && (CUR != ',') && (CUR != '{') &&
7715 (CUR != '*') && (CUR != '+') && (CUR != '?') && (CUR != '}'))
7716 NEXT;
7717 val = xmlDictLookup(ctxt->dict, BAD_CAST base, ctxt->cur - base);
7718 if (val == NULL)
7719 return(NULL);
7720 ret = xmlExpHashGetEntry(ctxt, XML_EXP_ATOM, NULL, NULL, val, 0, 0);
7721 if (ret == NULL)
7722 return(NULL);
7723 SKIP_BLANKS
7724parse_quantifier:
7725 if (CUR == '{') {
7726 int min, max;
7727
7728 NEXT
7729 min = xmlExpParseNumber(ctxt);
7730 if (min < 0) {
7731 xmlExpFree(ctxt, ret);
7732 return(NULL);
7733 }
7734 SKIP_BLANKS
7735 if (CUR == ',') {
7736 NEXT
7737 max = xmlExpParseNumber(ctxt);
7738 SKIP_BLANKS
7739 } else
7740 max = min;
7741 if (CUR != '}') {
7742 xmlExpFree(ctxt, ret);
7743 return(NULL);
7744 }
7745 NEXT
7746 ret = xmlExpHashGetEntry(ctxt, XML_EXP_COUNT, ret, NULL, NULL,
7747 min, max);
7748 SKIP_BLANKS
7749 } else if (CUR == '?') {
7750 NEXT
7751 ret = xmlExpHashGetEntry(ctxt, XML_EXP_COUNT, ret, NULL, NULL,
7752 0, 1);
7753 SKIP_BLANKS
7754 } else if (CUR == '+') {
7755 NEXT
7756 ret = xmlExpHashGetEntry(ctxt, XML_EXP_COUNT, ret, NULL, NULL,
7757 1, -1);
7758 SKIP_BLANKS
7759 } else if (CUR == '*') {
7760 NEXT
7761 ret = xmlExpHashGetEntry(ctxt, XML_EXP_COUNT, ret, NULL, NULL,
7762 0, -1);
7763 SKIP_BLANKS
7764 }
7765 return(ret);
7766}
7767
7768
7769static xmlExpNodePtr
7770xmlExpParseSeq(xmlExpCtxtPtr ctxt) {
7771 xmlExpNodePtr ret, right;
7772
7773 ret = xmlExpParseOr(ctxt);
7774 SKIP_BLANKS
7775 while (CUR == '|') {
7776 NEXT
7777 right = xmlExpParseOr(ctxt);
7778 if (right == NULL) {
7779 xmlExpFree(ctxt, ret);
7780 return(NULL);
7781 }
7782 ret = xmlExpHashGetEntry(ctxt, XML_EXP_OR, ret, right, NULL, 0, 0);
7783 if (ret == NULL)
7784 return(NULL);
7785 }
7786 return(ret);
7787}
7788
7789static xmlExpNodePtr
7790xmlExpParseExpr(xmlExpCtxtPtr ctxt) {
7791 xmlExpNodePtr ret, right;
7792
7793 ret = xmlExpParseSeq(ctxt);
7794 SKIP_BLANKS
7795 while (CUR == ',') {
7796 NEXT
7797 right = xmlExpParseSeq(ctxt);
7798 if (right == NULL) {
7799 xmlExpFree(ctxt, ret);
7800 return(NULL);
7801 }
7802 ret = xmlExpHashGetEntry(ctxt, XML_EXP_SEQ, ret, right, NULL, 0, 0);
7803 if (ret == NULL)
7804 return(NULL);
7805 }
7806 return(ret);
7807}
7808
7809/**
7810 * xmlExpParse:
7811 * @ctxt: the expressions context
7812 * @expr: the 0 terminated string
7813 *
7814 * Minimal parser for regexps, it understand the following constructs
7815 * - string terminals
7816 * - choice operator |
7817 * - sequence operator ,
7818 * - subexpressions (...)
7819 * - usual cardinality operators + * and ?
7820 * - finite sequences { min, max }
7821 * - infinite sequences { min, * }
7822 * There is minimal checkings made especially no checking on strings values
7823 *
7824 * Returns a new expression or NULL in case of failure
7825 */
7826xmlExpNodePtr
7827xmlExpParse(xmlExpCtxtPtr ctxt, const char *expr) {
7828 xmlExpNodePtr ret;
7829
7830 ctxt->expr = expr;
7831 ctxt->cur = expr;
7832
7833 ret = xmlExpParseExpr(ctxt);
7834 SKIP_BLANKS
7835 if (*ctxt->cur != 0) {
7836 xmlExpFree(ctxt, ret);
7837 return(NULL);
7838 }
7839 return(ret);
7840}
7841
7842static void
7843xmlExpDumpInt(xmlBufferPtr buf, xmlExpNodePtr expr, int glob) {
7844 xmlExpNodePtr c;
7845
7846 if (expr == NULL) return;
7847 if (glob) xmlBufferWriteChar(buf, "(");
7848 switch (expr->type) {
7849 case XML_EXP_EMPTY:
7850 xmlBufferWriteChar(buf, "empty");
7851 break;
7852 case XML_EXP_FORBID:
7853 xmlBufferWriteChar(buf, "forbidden");
7854 break;
7855 case XML_EXP_ATOM:
7856 xmlBufferWriteCHAR(buf, expr->exp_str);
7857 break;
7858 case XML_EXP_SEQ:
7859 c = expr->exp_left;
7860 if ((c->type == XML_EXP_SEQ) || (c->type == XML_EXP_OR))
7861 xmlExpDumpInt(buf, c, 1);
7862 else
7863 xmlExpDumpInt(buf, c, 0);
7864 xmlBufferWriteChar(buf, " , ");
7865 c = expr->exp_right;
7866 if ((c->type == XML_EXP_SEQ) || (c->type == XML_EXP_OR))
7867 xmlExpDumpInt(buf, c, 1);
7868 else
7869 xmlExpDumpInt(buf, c, 0);
7870 break;
7871 case XML_EXP_OR:
7872 c = expr->exp_left;
7873 if ((c->type == XML_EXP_SEQ) || (c->type == XML_EXP_OR))
7874 xmlExpDumpInt(buf, c, 1);
7875 else
7876 xmlExpDumpInt(buf, c, 0);
7877 xmlBufferWriteChar(buf, " | ");
7878 c = expr->exp_right;
7879 if ((c->type == XML_EXP_SEQ) || (c->type == XML_EXP_OR))
7880 xmlExpDumpInt(buf, c, 1);
7881 else
7882 xmlExpDumpInt(buf, c, 0);
7883 break;
7884 case XML_EXP_COUNT: {
7885 char rep[40];
7886
7887 c = expr->exp_left;
7888 if ((c->type == XML_EXP_SEQ) || (c->type == XML_EXP_OR))
7889 xmlExpDumpInt(buf, c, 1);
7890 else
7891 xmlExpDumpInt(buf, c, 0);
7892 if ((expr->exp_min == 0) && (expr->exp_max == 1)) {
7893 rep[0] = '?';
7894 rep[1] = 0;
7895 } else if ((expr->exp_min == 0) && (expr->exp_max == -1)) {
7896 rep[0] = '*';
7897 rep[1] = 0;
7898 } else if ((expr->exp_min == 1) && (expr->exp_max == -1)) {
7899 rep[0] = '+';
7900 rep[1] = 0;
7901 } else if (expr->exp_max == expr->exp_min) {
7902 snprintf(rep, 39, "{%d}", expr->exp_min);
7903 } else if (expr->exp_max < 0) {
7904 snprintf(rep, 39, "{%d,inf}", expr->exp_min);
7905 } else {
7906 snprintf(rep, 39, "{%d,%d}", expr->exp_min, expr->exp_max);
7907 }
7908 rep[39] = 0;
7909 xmlBufferWriteChar(buf, rep);
7910 break;
7911 }
7912 default:
7913 fprintf(stderr, "Error in tree\n");
7914 }
7915 if (glob)
7916 xmlBufferWriteChar(buf, ")");
7917}
7918/**
7919 * xmlExpDump:
7920 * @buf: a buffer to receive the output
7921 * @expr: the compiled expression
7922 *
7923 * Serialize the expression as compiled to the buffer
7924 */
7925void
Daniel Veillard5eee7672005-08-22 21:22:27 +00007926xmlExpDump(xmlBufferPtr buf, xmlExpNodePtr expr) {
7927 if ((buf == NULL) || (expr == NULL))
Daniel Veillard465a0002005-08-22 12:07:04 +00007928 return;
Daniel Veillard5eee7672005-08-22 21:22:27 +00007929 xmlExpDumpInt(buf, expr, 0);
Daniel Veillard465a0002005-08-22 12:07:04 +00007930}
7931
7932/**
7933 * xmlExpMaxToken:
7934 * @expr: a compiled expression
7935 *
7936 * Indicate the maximum number of input a expression can accept
7937 *
7938 * Returns the maximum length or -1 in case of error
7939 */
7940int
7941xmlExpMaxToken(xmlExpNodePtr expr) {
7942 if (expr == NULL)
7943 return(-1);
7944 return(expr->c_max);
7945}
7946
7947/**
7948 * xmlExpCtxtNbNodes:
7949 * @ctxt: an expression context
7950 *
7951 * Debugging facility provides the number of allocated nodes at a that point
7952 *
7953 * Returns the number of nodes in use or -1 in case of error
7954 */
7955int
7956xmlExpCtxtNbNodes(xmlExpCtxtPtr ctxt) {
7957 if (ctxt == NULL)
7958 return(-1);
7959 return(ctxt->nb_nodes);
7960}
7961
7962/**
7963 * xmlExpCtxtNbCons:
7964 * @ctxt: an expression context
7965 *
7966 * Debugging facility provides the number of allocated nodes over lifetime
7967 *
7968 * Returns the number of nodes ever allocated or -1 in case of error
7969 */
7970int
7971xmlExpCtxtNbCons(xmlExpCtxtPtr ctxt) {
7972 if (ctxt == NULL)
7973 return(-1);
7974 return(ctxt->nb_cons);
7975}
7976
Daniel Veillard81a8ec62005-08-22 00:20:58 +00007977#endif /* LIBXML_EXPR_ENABLED */
Daniel Veillard5d4644e2005-04-01 13:11:58 +00007978#define bottom_xmlregexp
7979#include "elfgcchack.h"
Daniel Veillard4255d502002-04-16 15:50:10 +00007980#endif /* LIBXML_REGEXP_ENABLED */