blob: 2a30d66f0734244f8f33a379fba0d7aaa50fefdb [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;
William M. Brack56578372007-04-11 14:33:46 +00001604 case XML_REGEXP_QUANT_RANGE:
1605 if (atom->min == 0) {
1606 xmlFAGenerateEpsilonTransition(ctxt, from, to);
1607 }
1608 break;
Daniel Veillard4255d502002-04-16 15:50:10 +00001609 default:
1610 break;
1611 }
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001612 return(0);
Daniel Veillard4255d502002-04-16 15:50:10 +00001613}
1614
1615/**
1616 * xmlFAReduceEpsilonTransitions:
Daniel Veillard441bc322002-04-20 17:38:48 +00001617 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00001618 * @fromnr: the from state
1619 * @tonr: the to state
William M. Brackddf71d62004-05-06 04:17:26 +00001620 * @counter: should that transition be associated to a counted
Daniel Veillard4255d502002-04-16 15:50:10 +00001621 *
1622 */
1623static void
1624xmlFAReduceEpsilonTransitions(xmlRegParserCtxtPtr ctxt, int fromnr,
1625 int tonr, int counter) {
1626 int transnr;
1627 xmlRegStatePtr from;
1628 xmlRegStatePtr to;
1629
1630#ifdef DEBUG_REGEXP_GRAPH
1631 printf("xmlFAReduceEpsilonTransitions(%d, %d)\n", fromnr, tonr);
1632#endif
1633 from = ctxt->states[fromnr];
1634 if (from == NULL)
1635 return;
1636 to = ctxt->states[tonr];
1637 if (to == NULL)
1638 return;
1639 if ((to->mark == XML_REGEXP_MARK_START) ||
1640 (to->mark == XML_REGEXP_MARK_VISITED))
1641 return;
1642
1643 to->mark = XML_REGEXP_MARK_VISITED;
1644 if (to->type == XML_REGEXP_FINAL_STATE) {
1645#ifdef DEBUG_REGEXP_GRAPH
1646 printf("State %d is final, so %d becomes final\n", tonr, fromnr);
1647#endif
1648 from->type = XML_REGEXP_FINAL_STATE;
1649 }
1650 for (transnr = 0;transnr < to->nbTrans;transnr++) {
Daniel Veillarddb68b742005-07-30 13:18:24 +00001651 if (to->trans[transnr].to < 0)
1652 continue;
Daniel Veillard4255d502002-04-16 15:50:10 +00001653 if (to->trans[transnr].atom == NULL) {
1654 /*
1655 * Don't remove counted transitions
1656 * Don't loop either
1657 */
Daniel Veillardb509f152002-04-17 16:28:10 +00001658 if (to->trans[transnr].to != fromnr) {
1659 if (to->trans[transnr].count >= 0) {
1660 int newto = to->trans[transnr].to;
1661
1662 xmlRegStateAddTrans(ctxt, from, NULL,
1663 ctxt->states[newto],
Daniel Veillard5de09382005-09-26 17:18:17 +00001664 -1, to->trans[transnr].count);
Daniel Veillardb509f152002-04-17 16:28:10 +00001665 } else {
Daniel Veillard4255d502002-04-16 15:50:10 +00001666#ifdef DEBUG_REGEXP_GRAPH
Daniel Veillardb509f152002-04-17 16:28:10 +00001667 printf("Found epsilon trans %d from %d to %d\n",
1668 transnr, tonr, to->trans[transnr].to);
Daniel Veillard4255d502002-04-16 15:50:10 +00001669#endif
Daniel Veillardb509f152002-04-17 16:28:10 +00001670 if (to->trans[transnr].counter >= 0) {
1671 xmlFAReduceEpsilonTransitions(ctxt, fromnr,
1672 to->trans[transnr].to,
1673 to->trans[transnr].counter);
1674 } else {
1675 xmlFAReduceEpsilonTransitions(ctxt, fromnr,
1676 to->trans[transnr].to,
1677 counter);
1678 }
1679 }
Daniel Veillard4255d502002-04-16 15:50:10 +00001680 }
1681 } else {
1682 int newto = to->trans[transnr].to;
1683
Daniel Veillardb509f152002-04-17 16:28:10 +00001684 if (to->trans[transnr].counter >= 0) {
1685 xmlRegStateAddTrans(ctxt, from, to->trans[transnr].atom,
1686 ctxt->states[newto],
Daniel Veillard5de09382005-09-26 17:18:17 +00001687 to->trans[transnr].counter, -1);
Daniel Veillardb509f152002-04-17 16:28:10 +00001688 } else {
1689 xmlRegStateAddTrans(ctxt, from, to->trans[transnr].atom,
Daniel Veillard5de09382005-09-26 17:18:17 +00001690 ctxt->states[newto], counter, -1);
Daniel Veillardb509f152002-04-17 16:28:10 +00001691 }
Daniel Veillard4255d502002-04-16 15:50:10 +00001692 }
1693 }
1694 to->mark = XML_REGEXP_MARK_NORMAL;
1695}
1696
1697/**
Daniel Veillarddb68b742005-07-30 13:18:24 +00001698 * xmlFAEliminateSimpleEpsilonTransitions:
1699 * @ctxt: a regexp parser context
1700 *
1701 * Eliminating general epsilon transitions can get costly in the general
1702 * algorithm due to the large amount of generated new transitions and
1703 * associated comparisons. However for simple epsilon transition used just
1704 * to separate building blocks when generating the automata this can be
1705 * reduced to state elimination:
1706 * - if there exists an epsilon from X to Y
1707 * - if there is no other transition from X
1708 * then X and Y are semantically equivalent and X can be eliminated
1709 * If X is the start state then make Y the start state, else replace the
1710 * target of all transitions to X by transitions to Y.
1711 */
1712static void
1713xmlFAEliminateSimpleEpsilonTransitions(xmlRegParserCtxtPtr ctxt) {
1714 int statenr, i, j, newto;
1715 xmlRegStatePtr state, tmp;
1716
1717 for (statenr = 0;statenr < ctxt->nbStates;statenr++) {
1718 state = ctxt->states[statenr];
1719 if (state == NULL)
1720 continue;
1721 if (state->nbTrans != 1)
1722 continue;
Daniel Veillard0e05f4c2006-11-01 15:33:04 +00001723 if (state->type == XML_REGEXP_UNREACH_STATE)
1724 continue;
Daniel Veillarddb68b742005-07-30 13:18:24 +00001725 /* is the only transition out a basic transition */
1726 if ((state->trans[0].atom == NULL) &&
1727 (state->trans[0].to >= 0) &&
1728 (state->trans[0].to != statenr) &&
1729 (state->trans[0].counter < 0) &&
1730 (state->trans[0].count < 0)) {
1731 newto = state->trans[0].to;
1732
1733 if (state->type == XML_REGEXP_START_STATE) {
1734#ifdef DEBUG_REGEXP_GRAPH
1735 printf("Found simple epsilon trans from start %d to %d\n",
1736 statenr, newto);
1737#endif
1738 } else {
1739#ifdef DEBUG_REGEXP_GRAPH
1740 printf("Found simple epsilon trans from %d to %d\n",
1741 statenr, newto);
1742#endif
1743 for (i = 0;i < state->nbTransTo;i++) {
1744 tmp = ctxt->states[state->transTo[i]];
1745 for (j = 0;j < tmp->nbTrans;j++) {
1746 if (tmp->trans[j].to == statenr) {
Daniel Veillarddb68b742005-07-30 13:18:24 +00001747#ifdef DEBUG_REGEXP_GRAPH
1748 printf("Changed transition %d on %d to go to %d\n",
1749 j, tmp->no, newto);
1750#endif
Daniel Veillard0e05f4c2006-11-01 15:33:04 +00001751 tmp->trans[j].to = -1;
1752 xmlRegStateAddTrans(ctxt, tmp, tmp->trans[j].atom,
1753 ctxt->states[newto],
1754 tmp->trans[j].counter,
1755 tmp->trans[j].count);
Daniel Veillarddb68b742005-07-30 13:18:24 +00001756 }
1757 }
1758 }
Daniel Veillarddb68b742005-07-30 13:18:24 +00001759 if (state->type == XML_REGEXP_FINAL_STATE)
1760 ctxt->states[newto]->type = XML_REGEXP_FINAL_STATE;
1761 /* eliminate the transition completely */
1762 state->nbTrans = 0;
1763
Daniel Veillard0e05f4c2006-11-01 15:33:04 +00001764 state->type = XML_REGEXP_UNREACH_STATE;
Daniel Veillarddb68b742005-07-30 13:18:24 +00001765
1766 }
1767
1768 }
1769 }
1770}
1771/**
Daniel Veillard4255d502002-04-16 15:50:10 +00001772 * xmlFAEliminateEpsilonTransitions:
Daniel Veillard441bc322002-04-20 17:38:48 +00001773 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00001774 *
1775 */
1776static void
1777xmlFAEliminateEpsilonTransitions(xmlRegParserCtxtPtr ctxt) {
1778 int statenr, transnr;
1779 xmlRegStatePtr state;
Daniel Veillarddb68b742005-07-30 13:18:24 +00001780 int has_epsilon;
Daniel Veillard4255d502002-04-16 15:50:10 +00001781
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001782 if (ctxt->states == NULL) return;
1783
Daniel Veillard0e05f4c2006-11-01 15:33:04 +00001784 /*
1785 * Eliminate simple epsilon transition and the associated unreachable
1786 * states.
1787 */
Daniel Veillarddb68b742005-07-30 13:18:24 +00001788 xmlFAEliminateSimpleEpsilonTransitions(ctxt);
Daniel Veillard0e05f4c2006-11-01 15:33:04 +00001789 for (statenr = 0;statenr < ctxt->nbStates;statenr++) {
1790 state = ctxt->states[statenr];
1791 if ((state != NULL) && (state->type == XML_REGEXP_UNREACH_STATE)) {
1792#ifdef DEBUG_REGEXP_GRAPH
1793 printf("Removed unreachable state %d\n", statenr);
1794#endif
1795 xmlRegFreeState(state);
1796 ctxt->states[statenr] = NULL;
1797 }
1798 }
Daniel Veillarddb68b742005-07-30 13:18:24 +00001799
1800 has_epsilon = 0;
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001801
Daniel Veillard4255d502002-04-16 15:50:10 +00001802 /*
Daniel Veillardfcd18ff2006-11-02 10:28:04 +00001803 * Build the completed transitions bypassing the epsilons
Daniel Veillard4255d502002-04-16 15:50:10 +00001804 * Use a marking algorithm to avoid loops
Daniel Veillardfcd18ff2006-11-02 10:28:04 +00001805 * Mark sink states too.
1806 * Process from the latests states backward to the start when
1807 * there is long cascading epsilon chains this minimize the
1808 * recursions and transition compares when adding the new ones
Daniel Veillard4255d502002-04-16 15:50:10 +00001809 */
Daniel Veillardfcd18ff2006-11-02 10:28:04 +00001810 for (statenr = ctxt->nbStates - 1;statenr >= 0;statenr--) {
Daniel Veillard4255d502002-04-16 15:50:10 +00001811 state = ctxt->states[statenr];
1812 if (state == NULL)
1813 continue;
Daniel Veillardcc026dc2005-01-12 13:21:17 +00001814 if ((state->nbTrans == 0) &&
1815 (state->type != XML_REGEXP_FINAL_STATE)) {
1816 state->type = XML_REGEXP_SINK_STATE;
1817 }
Daniel Veillard4255d502002-04-16 15:50:10 +00001818 for (transnr = 0;transnr < state->nbTrans;transnr++) {
1819 if ((state->trans[transnr].atom == NULL) &&
1820 (state->trans[transnr].to >= 0)) {
1821 if (state->trans[transnr].to == statenr) {
1822 state->trans[transnr].to = -1;
1823#ifdef DEBUG_REGEXP_GRAPH
1824 printf("Removed loopback epsilon trans %d on %d\n",
1825 transnr, statenr);
1826#endif
1827 } else if (state->trans[transnr].count < 0) {
1828 int newto = state->trans[transnr].to;
1829
1830#ifdef DEBUG_REGEXP_GRAPH
1831 printf("Found epsilon trans %d from %d to %d\n",
1832 transnr, statenr, newto);
1833#endif
Daniel Veillarddb68b742005-07-30 13:18:24 +00001834 has_epsilon = 1;
Daniel Veillard0e05f4c2006-11-01 15:33:04 +00001835 state->trans[transnr].to = -2;
1836 state->mark = XML_REGEXP_MARK_START;
Daniel Veillard4255d502002-04-16 15:50:10 +00001837 xmlFAReduceEpsilonTransitions(ctxt, statenr,
1838 newto, state->trans[transnr].counter);
1839 state->mark = XML_REGEXP_MARK_NORMAL;
1840#ifdef DEBUG_REGEXP_GRAPH
1841 } else {
1842 printf("Found counted transition %d on %d\n",
1843 transnr, statenr);
1844#endif
1845 }
1846 }
1847 }
1848 }
1849 /*
1850 * Eliminate the epsilon transitions
1851 */
Daniel Veillarddb68b742005-07-30 13:18:24 +00001852 if (has_epsilon) {
1853 for (statenr = 0;statenr < ctxt->nbStates;statenr++) {
1854 state = ctxt->states[statenr];
1855 if (state == NULL)
1856 continue;
1857 for (transnr = 0;transnr < state->nbTrans;transnr++) {
1858 xmlRegTransPtr trans = &(state->trans[transnr]);
1859 if ((trans->atom == NULL) &&
1860 (trans->count < 0) &&
1861 (trans->to >= 0)) {
1862 trans->to = -1;
1863 }
Daniel Veillard4255d502002-04-16 15:50:10 +00001864 }
1865 }
1866 }
Daniel Veillard23e73572002-09-19 19:56:43 +00001867
1868 /*
1869 * Use this pass to detect unreachable states too
1870 */
1871 for (statenr = 0;statenr < ctxt->nbStates;statenr++) {
1872 state = ctxt->states[statenr];
1873 if (state != NULL)
William M. Brack779af002003-08-01 15:55:39 +00001874 state->reached = XML_REGEXP_MARK_NORMAL;
Daniel Veillard23e73572002-09-19 19:56:43 +00001875 }
1876 state = ctxt->states[0];
1877 if (state != NULL)
William M. Brack779af002003-08-01 15:55:39 +00001878 state->reached = XML_REGEXP_MARK_START;
Daniel Veillard23e73572002-09-19 19:56:43 +00001879 while (state != NULL) {
1880 xmlRegStatePtr target = NULL;
William M. Brack779af002003-08-01 15:55:39 +00001881 state->reached = XML_REGEXP_MARK_VISITED;
Daniel Veillard23e73572002-09-19 19:56:43 +00001882 /*
William M. Brackddf71d62004-05-06 04:17:26 +00001883 * Mark all states reachable from the current reachable state
Daniel Veillard23e73572002-09-19 19:56:43 +00001884 */
1885 for (transnr = 0;transnr < state->nbTrans;transnr++) {
1886 if ((state->trans[transnr].to >= 0) &&
1887 ((state->trans[transnr].atom != NULL) ||
1888 (state->trans[transnr].count >= 0))) {
1889 int newto = state->trans[transnr].to;
1890
1891 if (ctxt->states[newto] == NULL)
1892 continue;
William M. Brack779af002003-08-01 15:55:39 +00001893 if (ctxt->states[newto]->reached == XML_REGEXP_MARK_NORMAL) {
1894 ctxt->states[newto]->reached = XML_REGEXP_MARK_START;
Daniel Veillard23e73572002-09-19 19:56:43 +00001895 target = ctxt->states[newto];
1896 }
1897 }
1898 }
Daniel Veillardcc026dc2005-01-12 13:21:17 +00001899
Daniel Veillard23e73572002-09-19 19:56:43 +00001900 /*
1901 * find the next accessible state not explored
1902 */
1903 if (target == NULL) {
1904 for (statenr = 1;statenr < ctxt->nbStates;statenr++) {
1905 state = ctxt->states[statenr];
William M. Brack779af002003-08-01 15:55:39 +00001906 if ((state != NULL) && (state->reached ==
1907 XML_REGEXP_MARK_START)) {
Daniel Veillard23e73572002-09-19 19:56:43 +00001908 target = state;
1909 break;
1910 }
1911 }
1912 }
1913 state = target;
1914 }
1915 for (statenr = 0;statenr < ctxt->nbStates;statenr++) {
1916 state = ctxt->states[statenr];
William M. Brack779af002003-08-01 15:55:39 +00001917 if ((state != NULL) && (state->reached == XML_REGEXP_MARK_NORMAL)) {
Daniel Veillard23e73572002-09-19 19:56:43 +00001918#ifdef DEBUG_REGEXP_GRAPH
1919 printf("Removed unreachable state %d\n", statenr);
1920#endif
1921 xmlRegFreeState(state);
1922 ctxt->states[statenr] = NULL;
1923 }
1924 }
1925
Daniel Veillard4255d502002-04-16 15:50:10 +00001926}
1927
Daniel Veillard567a45b2005-10-18 19:11:55 +00001928static int
1929xmlFACompareRanges(xmlRegRangePtr range1, xmlRegRangePtr range2) {
1930 int ret = 0;
1931
1932 if ((range1->type == XML_REGEXP_RANGES) ||
1933 (range2->type == XML_REGEXP_RANGES) ||
1934 (range2->type == XML_REGEXP_SUBREG) ||
1935 (range1->type == XML_REGEXP_SUBREG) ||
1936 (range1->type == XML_REGEXP_STRING) ||
1937 (range2->type == XML_REGEXP_STRING))
1938 return(-1);
1939
1940 /* put them in order */
1941 if (range1->type > range2->type) {
1942 xmlRegRangePtr tmp;
1943
1944 tmp = range1;
1945 range1 = range2;
1946 range2 = tmp;
1947 }
1948 if ((range1->type == XML_REGEXP_ANYCHAR) ||
1949 (range2->type == XML_REGEXP_ANYCHAR)) {
1950 ret = 1;
1951 } else if ((range1->type == XML_REGEXP_EPSILON) ||
1952 (range2->type == XML_REGEXP_EPSILON)) {
1953 return(0);
1954 } else if (range1->type == range2->type) {
1955 if ((range1->type != XML_REGEXP_CHARVAL) ||
1956 (range1->end < range2->start) ||
1957 (range2->end < range1->start))
1958 ret = 1;
1959 else
1960 ret = 0;
1961 } else if (range1->type == XML_REGEXP_CHARVAL) {
1962 int codepoint;
1963 int neg = 0;
1964
1965 /*
1966 * just check all codepoints in the range for acceptance,
1967 * this is usually way cheaper since done only once at
1968 * compilation than testing over and over at runtime or
1969 * pushing too many states when evaluating.
1970 */
1971 if (((range1->neg == 0) && (range2->neg != 0)) ||
1972 ((range1->neg != 0) && (range2->neg == 0)))
1973 neg = 1;
1974
1975 for (codepoint = range1->start;codepoint <= range1->end ;codepoint++) {
1976 ret = xmlRegCheckCharacterRange(range2->type, codepoint,
1977 0, range2->start, range2->end,
1978 range2->blockName);
1979 if (ret < 0)
1980 return(-1);
1981 if (((neg == 1) && (ret == 0)) ||
1982 ((neg == 0) && (ret == 1)))
1983 return(1);
1984 }
1985 return(0);
1986 } else if ((range1->type == XML_REGEXP_BLOCK_NAME) ||
1987 (range2->type == XML_REGEXP_BLOCK_NAME)) {
1988 if (range1->type == range2->type) {
1989 ret = xmlStrEqual(range1->blockName, range2->blockName);
1990 } else {
1991 /*
1992 * comparing a block range with anything else is way
1993 * too costly, and maintining the table is like too much
1994 * memory too, so let's force the automata to save state
1995 * here.
1996 */
1997 return(1);
1998 }
1999 } else if ((range1->type < XML_REGEXP_LETTER) ||
2000 (range2->type < XML_REGEXP_LETTER)) {
2001 if ((range1->type == XML_REGEXP_ANYSPACE) &&
2002 (range2->type == XML_REGEXP_NOTSPACE))
2003 ret = 0;
2004 else if ((range1->type == XML_REGEXP_INITNAME) &&
2005 (range2->type == XML_REGEXP_NOTINITNAME))
2006 ret = 0;
2007 else if ((range1->type == XML_REGEXP_NAMECHAR) &&
2008 (range2->type == XML_REGEXP_NOTNAMECHAR))
2009 ret = 0;
2010 else if ((range1->type == XML_REGEXP_DECIMAL) &&
2011 (range2->type == XML_REGEXP_NOTDECIMAL))
2012 ret = 0;
2013 else if ((range1->type == XML_REGEXP_REALCHAR) &&
2014 (range2->type == XML_REGEXP_NOTREALCHAR))
2015 ret = 0;
2016 else {
2017 /* same thing to limit complexity */
2018 return(1);
2019 }
2020 } else {
2021 ret = 0;
2022 /* range1->type < range2->type here */
2023 switch (range1->type) {
2024 case XML_REGEXP_LETTER:
2025 /* all disjoint except in the subgroups */
2026 if ((range2->type == XML_REGEXP_LETTER_UPPERCASE) ||
2027 (range2->type == XML_REGEXP_LETTER_LOWERCASE) ||
2028 (range2->type == XML_REGEXP_LETTER_TITLECASE) ||
2029 (range2->type == XML_REGEXP_LETTER_MODIFIER) ||
2030 (range2->type == XML_REGEXP_LETTER_OTHERS))
2031 ret = 1;
2032 break;
2033 case XML_REGEXP_MARK:
2034 if ((range2->type == XML_REGEXP_MARK_NONSPACING) ||
2035 (range2->type == XML_REGEXP_MARK_SPACECOMBINING) ||
2036 (range2->type == XML_REGEXP_MARK_ENCLOSING))
2037 ret = 1;
2038 break;
2039 case XML_REGEXP_NUMBER:
2040 if ((range2->type == XML_REGEXP_NUMBER_DECIMAL) ||
2041 (range2->type == XML_REGEXP_NUMBER_LETTER) ||
2042 (range2->type == XML_REGEXP_NUMBER_OTHERS))
2043 ret = 1;
2044 break;
2045 case XML_REGEXP_PUNCT:
2046 if ((range2->type == XML_REGEXP_PUNCT_CONNECTOR) ||
2047 (range2->type == XML_REGEXP_PUNCT_DASH) ||
2048 (range2->type == XML_REGEXP_PUNCT_OPEN) ||
2049 (range2->type == XML_REGEXP_PUNCT_CLOSE) ||
2050 (range2->type == XML_REGEXP_PUNCT_INITQUOTE) ||
2051 (range2->type == XML_REGEXP_PUNCT_FINQUOTE) ||
2052 (range2->type == XML_REGEXP_PUNCT_OTHERS))
2053 ret = 1;
2054 break;
2055 case XML_REGEXP_SEPAR:
2056 if ((range2->type == XML_REGEXP_SEPAR_SPACE) ||
2057 (range2->type == XML_REGEXP_SEPAR_LINE) ||
2058 (range2->type == XML_REGEXP_SEPAR_PARA))
2059 ret = 1;
2060 break;
2061 case XML_REGEXP_SYMBOL:
2062 if ((range2->type == XML_REGEXP_SYMBOL_MATH) ||
2063 (range2->type == XML_REGEXP_SYMBOL_CURRENCY) ||
2064 (range2->type == XML_REGEXP_SYMBOL_MODIFIER) ||
2065 (range2->type == XML_REGEXP_SYMBOL_OTHERS))
2066 ret = 1;
2067 break;
2068 case XML_REGEXP_OTHER:
2069 if ((range2->type == XML_REGEXP_OTHER_CONTROL) ||
2070 (range2->type == XML_REGEXP_OTHER_FORMAT) ||
2071 (range2->type == XML_REGEXP_OTHER_PRIVATE))
2072 ret = 1;
2073 break;
2074 default:
2075 if ((range2->type >= XML_REGEXP_LETTER) &&
2076 (range2->type < XML_REGEXP_BLOCK_NAME))
2077 ret = 0;
2078 else {
2079 /* safety net ! */
2080 return(1);
2081 }
2082 }
2083 }
2084 if (((range1->neg == 0) && (range2->neg != 0)) ||
2085 ((range1->neg != 0) && (range2->neg == 0)))
2086 ret = !ret;
2087 return(1);
2088}
2089
Daniel Veillarde19fc232002-04-22 16:01:24 +00002090/**
Daniel Veillardfc011b72006-02-12 19:14:15 +00002091 * xmlFACompareAtomTypes:
2092 * @type1: an atom type
2093 * @type2: an atom type
2094 *
2095 * Compares two atoms type to check whether they intersect in some ways,
2096 * this is used by xmlFACompareAtoms only
2097 *
2098 * Returns 1 if they may intersect and 0 otherwise
2099 */
2100static int
2101xmlFACompareAtomTypes(xmlRegAtomType type1, xmlRegAtomType type2) {
2102 if ((type1 == XML_REGEXP_EPSILON) ||
2103 (type1 == XML_REGEXP_CHARVAL) ||
2104 (type1 == XML_REGEXP_RANGES) ||
2105 (type1 == XML_REGEXP_SUBREG) ||
2106 (type1 == XML_REGEXP_STRING) ||
2107 (type1 == XML_REGEXP_ANYCHAR))
2108 return(1);
2109 if ((type2 == XML_REGEXP_EPSILON) ||
2110 (type2 == XML_REGEXP_CHARVAL) ||
2111 (type2 == XML_REGEXP_RANGES) ||
2112 (type2 == XML_REGEXP_SUBREG) ||
2113 (type2 == XML_REGEXP_STRING) ||
2114 (type2 == XML_REGEXP_ANYCHAR))
2115 return(1);
2116
2117 if (type1 == type2) return(1);
2118
2119 /* simplify subsequent compares by making sure type1 < type2 */
2120 if (type1 > type2) {
2121 xmlRegAtomType tmp = type1;
2122 type1 = type2;
2123 type2 = tmp;
2124 }
2125 switch (type1) {
2126 case XML_REGEXP_ANYSPACE: /* \s */
2127 /* can't be a letter, number, mark, pontuation, symbol */
2128 if ((type2 == XML_REGEXP_NOTSPACE) ||
2129 ((type2 >= XML_REGEXP_LETTER) &&
2130 (type2 <= XML_REGEXP_LETTER_OTHERS)) ||
2131 ((type2 >= XML_REGEXP_NUMBER) &&
2132 (type2 <= XML_REGEXP_NUMBER_OTHERS)) ||
2133 ((type2 >= XML_REGEXP_MARK) &&
2134 (type2 <= XML_REGEXP_MARK_ENCLOSING)) ||
2135 ((type2 >= XML_REGEXP_PUNCT) &&
2136 (type2 <= XML_REGEXP_PUNCT_OTHERS)) ||
2137 ((type2 >= XML_REGEXP_SYMBOL) &&
2138 (type2 <= XML_REGEXP_SYMBOL_OTHERS))
2139 ) return(0);
2140 break;
2141 case XML_REGEXP_NOTSPACE: /* \S */
2142 break;
2143 case XML_REGEXP_INITNAME: /* \l */
2144 /* can't be a number, mark, separator, pontuation, symbol or other */
2145 if ((type2 == XML_REGEXP_NOTINITNAME) ||
2146 ((type2 >= XML_REGEXP_NUMBER) &&
2147 (type2 <= XML_REGEXP_NUMBER_OTHERS)) ||
2148 ((type2 >= XML_REGEXP_MARK) &&
2149 (type2 <= XML_REGEXP_MARK_ENCLOSING)) ||
2150 ((type2 >= XML_REGEXP_SEPAR) &&
2151 (type2 <= XML_REGEXP_SEPAR_PARA)) ||
2152 ((type2 >= XML_REGEXP_PUNCT) &&
2153 (type2 <= XML_REGEXP_PUNCT_OTHERS)) ||
2154 ((type2 >= XML_REGEXP_SYMBOL) &&
2155 (type2 <= XML_REGEXP_SYMBOL_OTHERS)) ||
2156 ((type2 >= XML_REGEXP_OTHER) &&
2157 (type2 <= XML_REGEXP_OTHER_NA))
2158 ) return(0);
2159 break;
2160 case XML_REGEXP_NOTINITNAME: /* \L */
2161 break;
2162 case XML_REGEXP_NAMECHAR: /* \c */
2163 /* can't be a mark, separator, pontuation, symbol or other */
2164 if ((type2 == XML_REGEXP_NOTNAMECHAR) ||
2165 ((type2 >= XML_REGEXP_MARK) &&
2166 (type2 <= XML_REGEXP_MARK_ENCLOSING)) ||
2167 ((type2 >= XML_REGEXP_PUNCT) &&
2168 (type2 <= XML_REGEXP_PUNCT_OTHERS)) ||
2169 ((type2 >= XML_REGEXP_SEPAR) &&
2170 (type2 <= XML_REGEXP_SEPAR_PARA)) ||
2171 ((type2 >= XML_REGEXP_SYMBOL) &&
2172 (type2 <= XML_REGEXP_SYMBOL_OTHERS)) ||
2173 ((type2 >= XML_REGEXP_OTHER) &&
2174 (type2 <= XML_REGEXP_OTHER_NA))
2175 ) return(0);
2176 break;
2177 case XML_REGEXP_NOTNAMECHAR: /* \C */
2178 break;
2179 case XML_REGEXP_DECIMAL: /* \d */
2180 /* can't be a letter, mark, separator, pontuation, symbol or other */
2181 if ((type2 == XML_REGEXP_NOTDECIMAL) ||
2182 (type2 == XML_REGEXP_REALCHAR) ||
2183 ((type2 >= XML_REGEXP_LETTER) &&
2184 (type2 <= XML_REGEXP_LETTER_OTHERS)) ||
2185 ((type2 >= XML_REGEXP_MARK) &&
2186 (type2 <= XML_REGEXP_MARK_ENCLOSING)) ||
2187 ((type2 >= XML_REGEXP_PUNCT) &&
2188 (type2 <= XML_REGEXP_PUNCT_OTHERS)) ||
2189 ((type2 >= XML_REGEXP_SEPAR) &&
2190 (type2 <= XML_REGEXP_SEPAR_PARA)) ||
2191 ((type2 >= XML_REGEXP_SYMBOL) &&
2192 (type2 <= XML_REGEXP_SYMBOL_OTHERS)) ||
2193 ((type2 >= XML_REGEXP_OTHER) &&
2194 (type2 <= XML_REGEXP_OTHER_NA))
2195 )return(0);
2196 break;
2197 case XML_REGEXP_NOTDECIMAL: /* \D */
2198 break;
2199 case XML_REGEXP_REALCHAR: /* \w */
2200 /* can't be a mark, separator, pontuation, symbol or other */
2201 if ((type2 == XML_REGEXP_NOTDECIMAL) ||
2202 ((type2 >= XML_REGEXP_MARK) &&
2203 (type2 <= XML_REGEXP_MARK_ENCLOSING)) ||
2204 ((type2 >= XML_REGEXP_PUNCT) &&
2205 (type2 <= XML_REGEXP_PUNCT_OTHERS)) ||
2206 ((type2 >= XML_REGEXP_SEPAR) &&
2207 (type2 <= XML_REGEXP_SEPAR_PARA)) ||
2208 ((type2 >= XML_REGEXP_SYMBOL) &&
2209 (type2 <= XML_REGEXP_SYMBOL_OTHERS)) ||
2210 ((type2 >= XML_REGEXP_OTHER) &&
2211 (type2 <= XML_REGEXP_OTHER_NA))
2212 )return(0);
2213 break;
2214 case XML_REGEXP_NOTREALCHAR: /* \W */
2215 break;
2216 /*
2217 * at that point we know both type 1 and type2 are from
2218 * character categories are ordered and are different,
2219 * it becomes simple because this is a partition
2220 */
2221 case XML_REGEXP_LETTER:
2222 if (type2 <= XML_REGEXP_LETTER_OTHERS)
2223 return(1);
2224 return(0);
2225 case XML_REGEXP_LETTER_UPPERCASE:
2226 case XML_REGEXP_LETTER_LOWERCASE:
2227 case XML_REGEXP_LETTER_TITLECASE:
2228 case XML_REGEXP_LETTER_MODIFIER:
2229 case XML_REGEXP_LETTER_OTHERS:
2230 return(0);
2231 case XML_REGEXP_MARK:
2232 if (type2 <= XML_REGEXP_MARK_ENCLOSING)
2233 return(1);
2234 return(0);
2235 case XML_REGEXP_MARK_NONSPACING:
2236 case XML_REGEXP_MARK_SPACECOMBINING:
2237 case XML_REGEXP_MARK_ENCLOSING:
2238 return(0);
2239 case XML_REGEXP_NUMBER:
2240 if (type2 <= XML_REGEXP_NUMBER_OTHERS)
2241 return(1);
2242 return(0);
2243 case XML_REGEXP_NUMBER_DECIMAL:
2244 case XML_REGEXP_NUMBER_LETTER:
2245 case XML_REGEXP_NUMBER_OTHERS:
2246 return(0);
2247 case XML_REGEXP_PUNCT:
2248 if (type2 <= XML_REGEXP_PUNCT_OTHERS)
2249 return(1);
2250 return(0);
2251 case XML_REGEXP_PUNCT_CONNECTOR:
2252 case XML_REGEXP_PUNCT_DASH:
2253 case XML_REGEXP_PUNCT_OPEN:
2254 case XML_REGEXP_PUNCT_CLOSE:
2255 case XML_REGEXP_PUNCT_INITQUOTE:
2256 case XML_REGEXP_PUNCT_FINQUOTE:
2257 case XML_REGEXP_PUNCT_OTHERS:
2258 return(0);
2259 case XML_REGEXP_SEPAR:
2260 if (type2 <= XML_REGEXP_SEPAR_PARA)
2261 return(1);
2262 return(0);
2263 case XML_REGEXP_SEPAR_SPACE:
2264 case XML_REGEXP_SEPAR_LINE:
2265 case XML_REGEXP_SEPAR_PARA:
2266 return(0);
2267 case XML_REGEXP_SYMBOL:
2268 if (type2 <= XML_REGEXP_SYMBOL_OTHERS)
2269 return(1);
2270 return(0);
2271 case XML_REGEXP_SYMBOL_MATH:
2272 case XML_REGEXP_SYMBOL_CURRENCY:
2273 case XML_REGEXP_SYMBOL_MODIFIER:
2274 case XML_REGEXP_SYMBOL_OTHERS:
2275 return(0);
2276 case XML_REGEXP_OTHER:
2277 if (type2 <= XML_REGEXP_OTHER_NA)
2278 return(1);
2279 return(0);
2280 case XML_REGEXP_OTHER_CONTROL:
2281 case XML_REGEXP_OTHER_FORMAT:
2282 case XML_REGEXP_OTHER_PRIVATE:
2283 case XML_REGEXP_OTHER_NA:
2284 return(0);
2285 default:
2286 break;
2287 }
2288 return(1);
2289}
2290
2291/**
2292 * xmlFAEqualAtoms:
Daniel Veillarde19fc232002-04-22 16:01:24 +00002293 * @atom1: an atom
2294 * @atom2: an atom
2295 *
Daniel Veillardfc011b72006-02-12 19:14:15 +00002296 * Compares two atoms to check whether they are the same exactly
2297 * this is used to remove equivalent transitions
Daniel Veillarde19fc232002-04-22 16:01:24 +00002298 *
Daniel Veillardfc011b72006-02-12 19:14:15 +00002299 * Returns 1 if same and 0 otherwise
Daniel Veillarde19fc232002-04-22 16:01:24 +00002300 */
2301static int
Daniel Veillardfc011b72006-02-12 19:14:15 +00002302xmlFAEqualAtoms(xmlRegAtomPtr atom1, xmlRegAtomPtr atom2) {
2303 int ret = 0;
Daniel Veillard9efc4762005-07-19 14:33:55 +00002304
Daniel Veillarde19fc232002-04-22 16:01:24 +00002305 if (atom1 == atom2)
2306 return(1);
2307 if ((atom1 == NULL) || (atom2 == NULL))
2308 return(0);
2309
Daniel Veillardfc011b72006-02-12 19:14:15 +00002310 if (atom1->type != atom2->type)
2311 return(0);
2312 switch (atom1->type) {
2313 case XML_REGEXP_EPSILON:
2314 ret = 0;
2315 break;
2316 case XML_REGEXP_STRING:
2317 ret = xmlStrEqual((xmlChar *)atom1->valuep,
2318 (xmlChar *)atom2->valuep);
2319 break;
2320 case XML_REGEXP_CHARVAL:
2321 ret = (atom1->codepoint == atom2->codepoint);
2322 break;
2323 case XML_REGEXP_RANGES:
2324 /* too hard to do in the general case */
2325 ret = 0;
2326 default:
2327 break;
2328 }
2329 return(ret);
2330}
2331
2332/**
2333 * xmlFACompareAtoms:
2334 * @atom1: an atom
2335 * @atom2: an atom
2336 *
2337 * Compares two atoms to check whether they intersect in some ways,
2338 * this is used by xmlFAComputesDeterminism and xmlFARecurseDeterminism only
2339 *
2340 * Returns 1 if yes and 0 otherwise
2341 */
2342static int
2343xmlFACompareAtoms(xmlRegAtomPtr atom1, xmlRegAtomPtr atom2) {
2344 int ret = 1;
2345
2346 if (atom1 == atom2)
2347 return(1);
2348 if ((atom1 == NULL) || (atom2 == NULL))
2349 return(0);
2350
2351 if ((atom1->type == XML_REGEXP_ANYCHAR) ||
2352 (atom2->type == XML_REGEXP_ANYCHAR))
2353 return(1);
2354
2355 if (atom1->type > atom2->type) {
Daniel Veillard567a45b2005-10-18 19:11:55 +00002356 xmlRegAtomPtr tmp;
2357 tmp = atom1;
2358 atom1 = atom2;
2359 atom2 = tmp;
Daniel Veillardfc011b72006-02-12 19:14:15 +00002360 }
2361 if (atom1->type != atom2->type) {
2362 ret = xmlFACompareAtomTypes(atom1->type, atom2->type);
2363 /* if they can't intersect at the type level break now */
2364 if (ret == 0)
2365 return(0);
Daniel Veillard567a45b2005-10-18 19:11:55 +00002366 }
Daniel Veillarde19fc232002-04-22 16:01:24 +00002367 switch (atom1->type) {
2368 case XML_REGEXP_STRING:
Daniel Veillard9efc4762005-07-19 14:33:55 +00002369 ret = xmlRegStrEqualWildcard((xmlChar *)atom1->valuep,
2370 (xmlChar *)atom2->valuep);
2371 break;
Daniel Veillarde19fc232002-04-22 16:01:24 +00002372 case XML_REGEXP_EPSILON:
Daniel Veillard567a45b2005-10-18 19:11:55 +00002373 goto not_determinist;
Daniel Veillarde19fc232002-04-22 16:01:24 +00002374 case XML_REGEXP_CHARVAL:
Daniel Veillardfc011b72006-02-12 19:14:15 +00002375 if (atom2->type == XML_REGEXP_CHARVAL) {
2376 ret = (atom1->codepoint == atom2->codepoint);
2377 } else {
2378 ret = xmlRegCheckCharacter(atom2, atom1->codepoint);
2379 if (ret < 0)
2380 ret = 1;
2381 }
Daniel Veillard9efc4762005-07-19 14:33:55 +00002382 break;
Daniel Veillarde19fc232002-04-22 16:01:24 +00002383 case XML_REGEXP_RANGES:
Daniel Veillardfc011b72006-02-12 19:14:15 +00002384 if (atom2->type == XML_REGEXP_RANGES) {
Daniel Veillard567a45b2005-10-18 19:11:55 +00002385 int i, j, res;
2386 xmlRegRangePtr r1, r2;
2387
2388 /*
2389 * need to check that none of the ranges eventually matches
2390 */
2391 for (i = 0;i < atom1->nbRanges;i++) {
2392 for (j = 0;j < atom2->nbRanges;j++) {
2393 r1 = atom1->ranges[i];
2394 r2 = atom2->ranges[j];
2395 res = xmlFACompareRanges(r1, r2);
2396 if (res == 1) {
2397 ret = 1;
2398 goto done;
2399 }
2400 }
2401 }
2402 ret = 0;
2403 }
2404 break;
Daniel Veillarde19fc232002-04-22 16:01:24 +00002405 default:
Daniel Veillard567a45b2005-10-18 19:11:55 +00002406 goto not_determinist;
Daniel Veillarde19fc232002-04-22 16:01:24 +00002407 }
Daniel Veillard567a45b2005-10-18 19:11:55 +00002408done:
Daniel Veillard6e65e152005-08-09 11:09:52 +00002409 if (atom1->neg != atom2->neg) {
Daniel Veillard9efc4762005-07-19 14:33:55 +00002410 ret = !ret;
Daniel Veillard6e65e152005-08-09 11:09:52 +00002411 }
Daniel Veillard567a45b2005-10-18 19:11:55 +00002412 if (ret == 0)
2413 return(0);
2414not_determinist:
2415 return(1);
Daniel Veillarde19fc232002-04-22 16:01:24 +00002416}
2417
2418/**
2419 * xmlFARecurseDeterminism:
2420 * @ctxt: a regexp parser context
2421 *
2422 * Check whether the associated regexp is determinist,
2423 * should be called after xmlFAEliminateEpsilonTransitions()
2424 *
2425 */
2426static int
2427xmlFARecurseDeterminism(xmlRegParserCtxtPtr ctxt, xmlRegStatePtr state,
2428 int to, xmlRegAtomPtr atom) {
2429 int ret = 1;
Daniel Veillard567a45b2005-10-18 19:11:55 +00002430 int res;
Daniel Veillard5de09382005-09-26 17:18:17 +00002431 int transnr, nbTrans;
Daniel Veillarde19fc232002-04-22 16:01:24 +00002432 xmlRegTransPtr t1;
2433
2434 if (state == NULL)
2435 return(ret);
Daniel Veillard5de09382005-09-26 17:18:17 +00002436 /*
2437 * don't recurse on transitions potentially added in the course of
2438 * the elimination.
2439 */
2440 nbTrans = state->nbTrans;
2441 for (transnr = 0;transnr < nbTrans;transnr++) {
Daniel Veillarde19fc232002-04-22 16:01:24 +00002442 t1 = &(state->trans[transnr]);
2443 /*
2444 * check transitions conflicting with the one looked at
2445 */
2446 if (t1->atom == NULL) {
Daniel Veillard0e05f4c2006-11-01 15:33:04 +00002447 if (t1->to < 0)
Daniel Veillarde19fc232002-04-22 16:01:24 +00002448 continue;
Daniel Veillard567a45b2005-10-18 19:11:55 +00002449 res = xmlFARecurseDeterminism(ctxt, ctxt->states[t1->to],
Daniel Veillarde19fc232002-04-22 16:01:24 +00002450 to, atom);
Daniel Veillard567a45b2005-10-18 19:11:55 +00002451 if (res == 0) {
2452 ret = 0;
Daniel Veillardaa622012005-10-20 15:55:25 +00002453 /* t1->nd = 1; */
Daniel Veillard567a45b2005-10-18 19:11:55 +00002454 }
Daniel Veillarde19fc232002-04-22 16:01:24 +00002455 continue;
2456 }
2457 if (t1->to != to)
2458 continue;
Daniel Veillard567a45b2005-10-18 19:11:55 +00002459 if (xmlFACompareAtoms(t1->atom, atom)) {
2460 ret = 0;
2461 /* mark the transition as non-deterministic */
2462 t1->nd = 1;
2463 }
Daniel Veillarde19fc232002-04-22 16:01:24 +00002464 }
2465 return(ret);
2466}
2467
2468/**
2469 * xmlFAComputesDeterminism:
2470 * @ctxt: a regexp parser context
2471 *
2472 * Check whether the associated regexp is determinist,
2473 * should be called after xmlFAEliminateEpsilonTransitions()
2474 *
2475 */
2476static int
2477xmlFAComputesDeterminism(xmlRegParserCtxtPtr ctxt) {
2478 int statenr, transnr;
2479 xmlRegStatePtr state;
Daniel Veillard567a45b2005-10-18 19:11:55 +00002480 xmlRegTransPtr t1, t2, last;
Daniel Veillarde19fc232002-04-22 16:01:24 +00002481 int i;
2482 int ret = 1;
2483
Daniel Veillard4402ab42002-09-12 16:02:56 +00002484#ifdef DEBUG_REGEXP_GRAPH
2485 printf("xmlFAComputesDeterminism\n");
2486 xmlRegPrintCtxt(stdout, ctxt);
2487#endif
Daniel Veillarde19fc232002-04-22 16:01:24 +00002488 if (ctxt->determinist != -1)
2489 return(ctxt->determinist);
2490
2491 /*
Daniel Veillard567a45b2005-10-18 19:11:55 +00002492 * First cleanup the automata removing cancelled transitions
Daniel Veillarde19fc232002-04-22 16:01:24 +00002493 */
2494 for (statenr = 0;statenr < ctxt->nbStates;statenr++) {
2495 state = ctxt->states[statenr];
2496 if (state == NULL)
2497 continue;
Daniel Veillard4f82c8a2005-08-09 21:40:08 +00002498 if (state->nbTrans < 2)
2499 continue;
Daniel Veillarde19fc232002-04-22 16:01:24 +00002500 for (transnr = 0;transnr < state->nbTrans;transnr++) {
2501 t1 = &(state->trans[transnr]);
2502 /*
2503 * Determinism checks in case of counted or all transitions
2504 * will have to be handled separately
2505 */
Daniel Veillard567a45b2005-10-18 19:11:55 +00002506 if (t1->atom == NULL) {
Daniel Veillardaa622012005-10-20 15:55:25 +00002507 /* t1->nd = 1; */
Daniel Veillarde19fc232002-04-22 16:01:24 +00002508 continue;
Daniel Veillard567a45b2005-10-18 19:11:55 +00002509 }
Daniel Veillarde19fc232002-04-22 16:01:24 +00002510 if (t1->to == -1) /* eliminated */
2511 continue;
2512 for (i = 0;i < transnr;i++) {
2513 t2 = &(state->trans[i]);
2514 if (t2->to == -1) /* eliminated */
2515 continue;
2516 if (t2->atom != NULL) {
2517 if (t1->to == t2->to) {
Daniel Veillardfc011b72006-02-12 19:14:15 +00002518 if (xmlFAEqualAtoms(t1->atom, t2->atom))
William M. Brackddf71d62004-05-06 04:17:26 +00002519 t2->to = -1; /* eliminated */
Daniel Veillard567a45b2005-10-18 19:11:55 +00002520 }
2521 }
2522 }
2523 }
2524 }
2525
2526 /*
2527 * Check for all states that there aren't 2 transitions
2528 * with the same atom and a different target.
2529 */
2530 for (statenr = 0;statenr < ctxt->nbStates;statenr++) {
2531 state = ctxt->states[statenr];
2532 if (state == NULL)
2533 continue;
2534 if (state->nbTrans < 2)
2535 continue;
2536 last = NULL;
2537 for (transnr = 0;transnr < state->nbTrans;transnr++) {
2538 t1 = &(state->trans[transnr]);
2539 /*
2540 * Determinism checks in case of counted or all transitions
2541 * will have to be handled separately
2542 */
2543 if (t1->atom == NULL) {
2544 continue;
2545 }
2546 if (t1->to == -1) /* eliminated */
2547 continue;
2548 for (i = 0;i < transnr;i++) {
2549 t2 = &(state->trans[i]);
2550 if (t2->to == -1) /* eliminated */
2551 continue;
2552 if (t2->atom != NULL) {
2553 /* not determinist ! */
2554 if (xmlFACompareAtoms(t1->atom, t2->atom)) {
2555 ret = 0;
2556 /* mark the transitions as non-deterministic ones */
2557 t1->nd = 1;
2558 t2->nd = 1;
2559 last = t1;
Daniel Veillarde19fc232002-04-22 16:01:24 +00002560 }
2561 } else if (t1->to != -1) {
2562 /*
2563 * do the closure in case of remaining specific
2564 * epsilon transitions like choices or all
2565 */
2566 ret = xmlFARecurseDeterminism(ctxt, ctxt->states[t1->to],
2567 t2->to, t2->atom);
Daniel Veillard567a45b2005-10-18 19:11:55 +00002568 /* don't shortcut the computation so all non deterministic
2569 transition get marked down
Daniel Veillarde19fc232002-04-22 16:01:24 +00002570 if (ret == 0)
Daniel Veillardaa622012005-10-20 15:55:25 +00002571 return(0);
2572 */
Daniel Veillard567a45b2005-10-18 19:11:55 +00002573 if (ret == 0) {
2574 t1->nd = 1;
Daniel Veillardaa622012005-10-20 15:55:25 +00002575 /* t2->nd = 1; */
Daniel Veillard567a45b2005-10-18 19:11:55 +00002576 last = t1;
2577 }
Daniel Veillarde19fc232002-04-22 16:01:24 +00002578 }
2579 }
Daniel Veillard567a45b2005-10-18 19:11:55 +00002580 /* don't shortcut the computation so all non deterministic
2581 transition get marked down
Daniel Veillarde19fc232002-04-22 16:01:24 +00002582 if (ret == 0)
Daniel Veillard567a45b2005-10-18 19:11:55 +00002583 break; */
Daniel Veillarde19fc232002-04-22 16:01:24 +00002584 }
Daniel Veillard567a45b2005-10-18 19:11:55 +00002585
2586 /*
2587 * mark specifically the last non-deterministic transition
2588 * from a state since there is no need to set-up rollback
2589 * from it
2590 */
2591 if (last != NULL) {
2592 last->nd = 2;
2593 }
2594
2595 /* don't shortcut the computation so all non deterministic
2596 transition get marked down
Daniel Veillarde19fc232002-04-22 16:01:24 +00002597 if (ret == 0)
Daniel Veillard567a45b2005-10-18 19:11:55 +00002598 break; */
Daniel Veillarde19fc232002-04-22 16:01:24 +00002599 }
Daniel Veillard567a45b2005-10-18 19:11:55 +00002600
Daniel Veillarde19fc232002-04-22 16:01:24 +00002601 ctxt->determinist = ret;
2602 return(ret);
2603}
2604
Daniel Veillard4255d502002-04-16 15:50:10 +00002605/************************************************************************
2606 * *
2607 * Routines to check input against transition atoms *
2608 * *
2609 ************************************************************************/
2610
2611static int
2612xmlRegCheckCharacterRange(xmlRegAtomType type, int codepoint, int neg,
2613 int start, int end, const xmlChar *blockName) {
2614 int ret = 0;
2615
2616 switch (type) {
2617 case XML_REGEXP_STRING:
2618 case XML_REGEXP_SUBREG:
2619 case XML_REGEXP_RANGES:
2620 case XML_REGEXP_EPSILON:
2621 return(-1);
2622 case XML_REGEXP_ANYCHAR:
2623 ret = ((codepoint != '\n') && (codepoint != '\r'));
2624 break;
2625 case XML_REGEXP_CHARVAL:
2626 ret = ((codepoint >= start) && (codepoint <= end));
2627 break;
2628 case XML_REGEXP_NOTSPACE:
2629 neg = !neg;
2630 case XML_REGEXP_ANYSPACE:
2631 ret = ((codepoint == '\n') || (codepoint == '\r') ||
2632 (codepoint == '\t') || (codepoint == ' '));
2633 break;
2634 case XML_REGEXP_NOTINITNAME:
2635 neg = !neg;
2636 case XML_REGEXP_INITNAME:
William M. Brack871611b2003-10-18 04:53:14 +00002637 ret = (IS_LETTER(codepoint) ||
Daniel Veillard4255d502002-04-16 15:50:10 +00002638 (codepoint == '_') || (codepoint == ':'));
2639 break;
2640 case XML_REGEXP_NOTNAMECHAR:
2641 neg = !neg;
2642 case XML_REGEXP_NAMECHAR:
William M. Brack871611b2003-10-18 04:53:14 +00002643 ret = (IS_LETTER(codepoint) || IS_DIGIT(codepoint) ||
Daniel Veillard4255d502002-04-16 15:50:10 +00002644 (codepoint == '.') || (codepoint == '-') ||
2645 (codepoint == '_') || (codepoint == ':') ||
William M. Brack871611b2003-10-18 04:53:14 +00002646 IS_COMBINING(codepoint) || IS_EXTENDER(codepoint));
Daniel Veillard4255d502002-04-16 15:50:10 +00002647 break;
2648 case XML_REGEXP_NOTDECIMAL:
2649 neg = !neg;
2650 case XML_REGEXP_DECIMAL:
2651 ret = xmlUCSIsCatNd(codepoint);
2652 break;
2653 case XML_REGEXP_REALCHAR:
2654 neg = !neg;
2655 case XML_REGEXP_NOTREALCHAR:
2656 ret = xmlUCSIsCatP(codepoint);
2657 if (ret == 0)
2658 ret = xmlUCSIsCatZ(codepoint);
2659 if (ret == 0)
2660 ret = xmlUCSIsCatC(codepoint);
2661 break;
2662 case XML_REGEXP_LETTER:
2663 ret = xmlUCSIsCatL(codepoint);
2664 break;
2665 case XML_REGEXP_LETTER_UPPERCASE:
2666 ret = xmlUCSIsCatLu(codepoint);
2667 break;
2668 case XML_REGEXP_LETTER_LOWERCASE:
2669 ret = xmlUCSIsCatLl(codepoint);
2670 break;
2671 case XML_REGEXP_LETTER_TITLECASE:
2672 ret = xmlUCSIsCatLt(codepoint);
2673 break;
2674 case XML_REGEXP_LETTER_MODIFIER:
2675 ret = xmlUCSIsCatLm(codepoint);
2676 break;
2677 case XML_REGEXP_LETTER_OTHERS:
2678 ret = xmlUCSIsCatLo(codepoint);
2679 break;
2680 case XML_REGEXP_MARK:
2681 ret = xmlUCSIsCatM(codepoint);
2682 break;
2683 case XML_REGEXP_MARK_NONSPACING:
2684 ret = xmlUCSIsCatMn(codepoint);
2685 break;
2686 case XML_REGEXP_MARK_SPACECOMBINING:
2687 ret = xmlUCSIsCatMc(codepoint);
2688 break;
2689 case XML_REGEXP_MARK_ENCLOSING:
2690 ret = xmlUCSIsCatMe(codepoint);
2691 break;
2692 case XML_REGEXP_NUMBER:
2693 ret = xmlUCSIsCatN(codepoint);
2694 break;
2695 case XML_REGEXP_NUMBER_DECIMAL:
2696 ret = xmlUCSIsCatNd(codepoint);
2697 break;
2698 case XML_REGEXP_NUMBER_LETTER:
2699 ret = xmlUCSIsCatNl(codepoint);
2700 break;
2701 case XML_REGEXP_NUMBER_OTHERS:
2702 ret = xmlUCSIsCatNo(codepoint);
2703 break;
2704 case XML_REGEXP_PUNCT:
2705 ret = xmlUCSIsCatP(codepoint);
2706 break;
2707 case XML_REGEXP_PUNCT_CONNECTOR:
2708 ret = xmlUCSIsCatPc(codepoint);
2709 break;
2710 case XML_REGEXP_PUNCT_DASH:
2711 ret = xmlUCSIsCatPd(codepoint);
2712 break;
2713 case XML_REGEXP_PUNCT_OPEN:
2714 ret = xmlUCSIsCatPs(codepoint);
2715 break;
2716 case XML_REGEXP_PUNCT_CLOSE:
2717 ret = xmlUCSIsCatPe(codepoint);
2718 break;
2719 case XML_REGEXP_PUNCT_INITQUOTE:
2720 ret = xmlUCSIsCatPi(codepoint);
2721 break;
2722 case XML_REGEXP_PUNCT_FINQUOTE:
2723 ret = xmlUCSIsCatPf(codepoint);
2724 break;
2725 case XML_REGEXP_PUNCT_OTHERS:
2726 ret = xmlUCSIsCatPo(codepoint);
2727 break;
2728 case XML_REGEXP_SEPAR:
2729 ret = xmlUCSIsCatZ(codepoint);
2730 break;
2731 case XML_REGEXP_SEPAR_SPACE:
2732 ret = xmlUCSIsCatZs(codepoint);
2733 break;
2734 case XML_REGEXP_SEPAR_LINE:
2735 ret = xmlUCSIsCatZl(codepoint);
2736 break;
2737 case XML_REGEXP_SEPAR_PARA:
2738 ret = xmlUCSIsCatZp(codepoint);
2739 break;
2740 case XML_REGEXP_SYMBOL:
2741 ret = xmlUCSIsCatS(codepoint);
2742 break;
2743 case XML_REGEXP_SYMBOL_MATH:
2744 ret = xmlUCSIsCatSm(codepoint);
2745 break;
2746 case XML_REGEXP_SYMBOL_CURRENCY:
2747 ret = xmlUCSIsCatSc(codepoint);
2748 break;
2749 case XML_REGEXP_SYMBOL_MODIFIER:
2750 ret = xmlUCSIsCatSk(codepoint);
2751 break;
2752 case XML_REGEXP_SYMBOL_OTHERS:
2753 ret = xmlUCSIsCatSo(codepoint);
2754 break;
2755 case XML_REGEXP_OTHER:
2756 ret = xmlUCSIsCatC(codepoint);
2757 break;
2758 case XML_REGEXP_OTHER_CONTROL:
2759 ret = xmlUCSIsCatCc(codepoint);
2760 break;
2761 case XML_REGEXP_OTHER_FORMAT:
2762 ret = xmlUCSIsCatCf(codepoint);
2763 break;
2764 case XML_REGEXP_OTHER_PRIVATE:
2765 ret = xmlUCSIsCatCo(codepoint);
2766 break;
2767 case XML_REGEXP_OTHER_NA:
2768 /* ret = xmlUCSIsCatCn(codepoint); */
2769 /* Seems it doesn't exist anymore in recent Unicode releases */
2770 ret = 0;
2771 break;
2772 case XML_REGEXP_BLOCK_NAME:
2773 ret = xmlUCSIsBlock(codepoint, (const char *) blockName);
2774 break;
2775 }
2776 if (neg)
2777 return(!ret);
2778 return(ret);
2779}
2780
2781static int
2782xmlRegCheckCharacter(xmlRegAtomPtr atom, int codepoint) {
2783 int i, ret = 0;
2784 xmlRegRangePtr range;
2785
William M. Brack871611b2003-10-18 04:53:14 +00002786 if ((atom == NULL) || (!IS_CHAR(codepoint)))
Daniel Veillard4255d502002-04-16 15:50:10 +00002787 return(-1);
2788
2789 switch (atom->type) {
2790 case XML_REGEXP_SUBREG:
2791 case XML_REGEXP_EPSILON:
2792 return(-1);
2793 case XML_REGEXP_CHARVAL:
2794 return(codepoint == atom->codepoint);
2795 case XML_REGEXP_RANGES: {
2796 int accept = 0;
Daniel Veillardf2a12832003-11-24 13:04:35 +00002797
Daniel Veillard4255d502002-04-16 15:50:10 +00002798 for (i = 0;i < atom->nbRanges;i++) {
2799 range = atom->ranges[i];
Daniel Veillardf8b9de32003-11-24 14:27:26 +00002800 if (range->neg == 2) {
Daniel Veillard4255d502002-04-16 15:50:10 +00002801 ret = xmlRegCheckCharacterRange(range->type, codepoint,
2802 0, range->start, range->end,
2803 range->blockName);
2804 if (ret != 0)
2805 return(0); /* excluded char */
Daniel Veillardf8b9de32003-11-24 14:27:26 +00002806 } else if (range->neg) {
2807 ret = xmlRegCheckCharacterRange(range->type, codepoint,
2808 0, range->start, range->end,
2809 range->blockName);
2810 if (ret == 0)
Daniel Veillardf2a12832003-11-24 13:04:35 +00002811 accept = 1;
Daniel Veillardf8b9de32003-11-24 14:27:26 +00002812 else
2813 return(0);
Daniel Veillard4255d502002-04-16 15:50:10 +00002814 } else {
2815 ret = xmlRegCheckCharacterRange(range->type, codepoint,
2816 0, range->start, range->end,
2817 range->blockName);
2818 if (ret != 0)
2819 accept = 1; /* might still be excluded */
2820 }
2821 }
2822 return(accept);
2823 }
2824 case XML_REGEXP_STRING:
2825 printf("TODO: XML_REGEXP_STRING\n");
2826 return(-1);
2827 case XML_REGEXP_ANYCHAR:
2828 case XML_REGEXP_ANYSPACE:
2829 case XML_REGEXP_NOTSPACE:
2830 case XML_REGEXP_INITNAME:
2831 case XML_REGEXP_NOTINITNAME:
2832 case XML_REGEXP_NAMECHAR:
2833 case XML_REGEXP_NOTNAMECHAR:
2834 case XML_REGEXP_DECIMAL:
2835 case XML_REGEXP_NOTDECIMAL:
2836 case XML_REGEXP_REALCHAR:
2837 case XML_REGEXP_NOTREALCHAR:
2838 case XML_REGEXP_LETTER:
2839 case XML_REGEXP_LETTER_UPPERCASE:
2840 case XML_REGEXP_LETTER_LOWERCASE:
2841 case XML_REGEXP_LETTER_TITLECASE:
2842 case XML_REGEXP_LETTER_MODIFIER:
2843 case XML_REGEXP_LETTER_OTHERS:
2844 case XML_REGEXP_MARK:
2845 case XML_REGEXP_MARK_NONSPACING:
2846 case XML_REGEXP_MARK_SPACECOMBINING:
2847 case XML_REGEXP_MARK_ENCLOSING:
2848 case XML_REGEXP_NUMBER:
2849 case XML_REGEXP_NUMBER_DECIMAL:
2850 case XML_REGEXP_NUMBER_LETTER:
2851 case XML_REGEXP_NUMBER_OTHERS:
2852 case XML_REGEXP_PUNCT:
2853 case XML_REGEXP_PUNCT_CONNECTOR:
2854 case XML_REGEXP_PUNCT_DASH:
2855 case XML_REGEXP_PUNCT_OPEN:
2856 case XML_REGEXP_PUNCT_CLOSE:
2857 case XML_REGEXP_PUNCT_INITQUOTE:
2858 case XML_REGEXP_PUNCT_FINQUOTE:
2859 case XML_REGEXP_PUNCT_OTHERS:
2860 case XML_REGEXP_SEPAR:
2861 case XML_REGEXP_SEPAR_SPACE:
2862 case XML_REGEXP_SEPAR_LINE:
2863 case XML_REGEXP_SEPAR_PARA:
2864 case XML_REGEXP_SYMBOL:
2865 case XML_REGEXP_SYMBOL_MATH:
2866 case XML_REGEXP_SYMBOL_CURRENCY:
2867 case XML_REGEXP_SYMBOL_MODIFIER:
2868 case XML_REGEXP_SYMBOL_OTHERS:
2869 case XML_REGEXP_OTHER:
2870 case XML_REGEXP_OTHER_CONTROL:
2871 case XML_REGEXP_OTHER_FORMAT:
2872 case XML_REGEXP_OTHER_PRIVATE:
2873 case XML_REGEXP_OTHER_NA:
2874 case XML_REGEXP_BLOCK_NAME:
2875 ret = xmlRegCheckCharacterRange(atom->type, codepoint, 0, 0, 0,
2876 (const xmlChar *)atom->valuep);
2877 if (atom->neg)
2878 ret = !ret;
2879 break;
2880 }
2881 return(ret);
2882}
2883
2884/************************************************************************
2885 * *
William M. Brackddf71d62004-05-06 04:17:26 +00002886 * Saving and restoring state of an execution context *
Daniel Veillard4255d502002-04-16 15:50:10 +00002887 * *
2888 ************************************************************************/
2889
2890#ifdef DEBUG_REGEXP_EXEC
2891static void
2892xmlFARegDebugExec(xmlRegExecCtxtPtr exec) {
2893 printf("state: %d:%d:idx %d", exec->state->no, exec->transno, exec->index);
2894 if (exec->inputStack != NULL) {
2895 int i;
2896 printf(": ");
2897 for (i = 0;(i < 3) && (i < exec->inputStackNr);i++)
Daniel Veillard0e05f4c2006-11-01 15:33:04 +00002898 printf("%s ", (const char *)
2899 exec->inputStack[exec->inputStackNr - (i + 1)].value);
Daniel Veillard4255d502002-04-16 15:50:10 +00002900 } else {
2901 printf(": %s", &(exec->inputString[exec->index]));
2902 }
2903 printf("\n");
2904}
2905#endif
2906
2907static void
2908xmlFARegExecSave(xmlRegExecCtxtPtr exec) {
2909#ifdef DEBUG_REGEXP_EXEC
2910 printf("saving ");
2911 exec->transno++;
2912 xmlFARegDebugExec(exec);
2913 exec->transno--;
2914#endif
Daniel Veillard94cc1032005-09-15 13:09:00 +00002915#ifdef MAX_PUSH
2916 if (exec->nbPush > MAX_PUSH) {
2917 return;
2918 }
2919 exec->nbPush++;
2920#endif
Daniel Veillard4255d502002-04-16 15:50:10 +00002921
2922 if (exec->maxRollbacks == 0) {
2923 exec->maxRollbacks = 4;
2924 exec->rollbacks = (xmlRegExecRollback *) xmlMalloc(exec->maxRollbacks *
2925 sizeof(xmlRegExecRollback));
2926 if (exec->rollbacks == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00002927 xmlRegexpErrMemory(NULL, "saving regexp");
Daniel Veillard4255d502002-04-16 15:50:10 +00002928 exec->maxRollbacks = 0;
2929 return;
2930 }
2931 memset(exec->rollbacks, 0,
2932 exec->maxRollbacks * sizeof(xmlRegExecRollback));
2933 } else if (exec->nbRollbacks >= exec->maxRollbacks) {
2934 xmlRegExecRollback *tmp;
2935 int len = exec->maxRollbacks;
2936
2937 exec->maxRollbacks *= 2;
2938 tmp = (xmlRegExecRollback *) xmlRealloc(exec->rollbacks,
2939 exec->maxRollbacks * sizeof(xmlRegExecRollback));
2940 if (tmp == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00002941 xmlRegexpErrMemory(NULL, "saving regexp");
Daniel Veillard4255d502002-04-16 15:50:10 +00002942 exec->maxRollbacks /= 2;
2943 return;
2944 }
2945 exec->rollbacks = tmp;
2946 tmp = &exec->rollbacks[len];
2947 memset(tmp, 0, (exec->maxRollbacks - len) * sizeof(xmlRegExecRollback));
2948 }
2949 exec->rollbacks[exec->nbRollbacks].state = exec->state;
2950 exec->rollbacks[exec->nbRollbacks].index = exec->index;
2951 exec->rollbacks[exec->nbRollbacks].nextbranch = exec->transno + 1;
2952 if (exec->comp->nbCounters > 0) {
2953 if (exec->rollbacks[exec->nbRollbacks].counts == NULL) {
2954 exec->rollbacks[exec->nbRollbacks].counts = (int *)
2955 xmlMalloc(exec->comp->nbCounters * sizeof(int));
2956 if (exec->rollbacks[exec->nbRollbacks].counts == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00002957 xmlRegexpErrMemory(NULL, "saving regexp");
Daniel Veillard4255d502002-04-16 15:50:10 +00002958 exec->status = -5;
2959 return;
2960 }
2961 }
2962 memcpy(exec->rollbacks[exec->nbRollbacks].counts, exec->counts,
2963 exec->comp->nbCounters * sizeof(int));
2964 }
2965 exec->nbRollbacks++;
2966}
2967
2968static void
2969xmlFARegExecRollBack(xmlRegExecCtxtPtr exec) {
2970 if (exec->nbRollbacks <= 0) {
2971 exec->status = -1;
2972#ifdef DEBUG_REGEXP_EXEC
2973 printf("rollback failed on empty stack\n");
2974#endif
2975 return;
2976 }
2977 exec->nbRollbacks--;
2978 exec->state = exec->rollbacks[exec->nbRollbacks].state;
2979 exec->index = exec->rollbacks[exec->nbRollbacks].index;
2980 exec->transno = exec->rollbacks[exec->nbRollbacks].nextbranch;
2981 if (exec->comp->nbCounters > 0) {
2982 if (exec->rollbacks[exec->nbRollbacks].counts == NULL) {
2983 fprintf(stderr, "exec save: allocation failed");
2984 exec->status = -6;
2985 return;
2986 }
2987 memcpy(exec->counts, exec->rollbacks[exec->nbRollbacks].counts,
2988 exec->comp->nbCounters * sizeof(int));
2989 }
2990
2991#ifdef DEBUG_REGEXP_EXEC
2992 printf("restored ");
2993 xmlFARegDebugExec(exec);
2994#endif
2995}
2996
2997/************************************************************************
2998 * *
William M. Brackddf71d62004-05-06 04:17:26 +00002999 * Verifier, running an input against a compiled regexp *
Daniel Veillard4255d502002-04-16 15:50:10 +00003000 * *
3001 ************************************************************************/
3002
3003static int
3004xmlFARegExec(xmlRegexpPtr comp, const xmlChar *content) {
3005 xmlRegExecCtxt execval;
3006 xmlRegExecCtxtPtr exec = &execval;
Daniel Veillard567a45b2005-10-18 19:11:55 +00003007 int ret, codepoint = 0, len, deter;
Daniel Veillard4255d502002-04-16 15:50:10 +00003008
3009 exec->inputString = content;
3010 exec->index = 0;
Daniel Veillard94cc1032005-09-15 13:09:00 +00003011 exec->nbPush = 0;
Daniel Veillard4255d502002-04-16 15:50:10 +00003012 exec->determinist = 1;
3013 exec->maxRollbacks = 0;
3014 exec->nbRollbacks = 0;
3015 exec->rollbacks = NULL;
3016 exec->status = 0;
3017 exec->comp = comp;
3018 exec->state = comp->states[0];
3019 exec->transno = 0;
3020 exec->transcount = 0;
Daniel Veillardf2a12832003-11-24 13:04:35 +00003021 exec->inputStack = NULL;
3022 exec->inputStackMax = 0;
Daniel Veillard4255d502002-04-16 15:50:10 +00003023 if (comp->nbCounters > 0) {
3024 exec->counts = (int *) xmlMalloc(comp->nbCounters * sizeof(int));
Daniel Veillardff46a042003-10-08 08:53:17 +00003025 if (exec->counts == NULL) {
3026 xmlRegexpErrMemory(NULL, "running regexp");
Daniel Veillard4255d502002-04-16 15:50:10 +00003027 return(-1);
Daniel Veillardff46a042003-10-08 08:53:17 +00003028 }
Daniel Veillard4255d502002-04-16 15:50:10 +00003029 memset(exec->counts, 0, comp->nbCounters * sizeof(int));
3030 } else
3031 exec->counts = NULL;
3032 while ((exec->status == 0) &&
3033 ((exec->inputString[exec->index] != 0) ||
3034 (exec->state->type != XML_REGEXP_FINAL_STATE))) {
3035 xmlRegTransPtr trans;
3036 xmlRegAtomPtr atom;
3037
3038 /*
William M. Brack0e00b282004-04-26 15:40:47 +00003039 * If end of input on non-terminal state, rollback, however we may
Daniel Veillard4255d502002-04-16 15:50:10 +00003040 * still have epsilon like transition for counted transitions
William M. Brack0e00b282004-04-26 15:40:47 +00003041 * on counters, in that case don't break too early. Additionally,
3042 * if we are working on a range like "AB{0,2}", where B is not present,
3043 * we don't want to break.
Daniel Veillard4255d502002-04-16 15:50:10 +00003044 */
Daniel Veillard11ce4002006-03-10 00:36:23 +00003045 len = 1;
William M. Brack0e00b282004-04-26 15:40:47 +00003046 if ((exec->inputString[exec->index] == 0) && (exec->counts == NULL)) {
William M. Brackddf71d62004-05-06 04:17:26 +00003047 /*
3048 * if there is a transition, we must check if
3049 * atom allows minOccurs of 0
3050 */
3051 if (exec->transno < exec->state->nbTrans) {
William M. Brack0e00b282004-04-26 15:40:47 +00003052 trans = &exec->state->trans[exec->transno];
3053 if (trans->to >=0) {
3054 atom = trans->atom;
3055 if (!((atom->min == 0) && (atom->max > 0)))
3056 goto rollback;
3057 }
3058 } else
3059 goto rollback;
3060 }
Daniel Veillard4255d502002-04-16 15:50:10 +00003061
3062 exec->transcount = 0;
3063 for (;exec->transno < exec->state->nbTrans;exec->transno++) {
3064 trans = &exec->state->trans[exec->transno];
3065 if (trans->to < 0)
3066 continue;
3067 atom = trans->atom;
3068 ret = 0;
Daniel Veillard567a45b2005-10-18 19:11:55 +00003069 deter = 1;
Daniel Veillard4255d502002-04-16 15:50:10 +00003070 if (trans->count >= 0) {
3071 int count;
3072 xmlRegCounterPtr counter;
3073
Daniel Veillard11ce4002006-03-10 00:36:23 +00003074 if (exec->counts == NULL) {
3075 exec->status = -1;
3076 goto error;
3077 }
Daniel Veillard4255d502002-04-16 15:50:10 +00003078 /*
3079 * A counted transition.
3080 */
3081
3082 count = exec->counts[trans->count];
3083 counter = &exec->comp->counters[trans->count];
3084#ifdef DEBUG_REGEXP_EXEC
3085 printf("testing count %d: val %d, min %d, max %d\n",
3086 trans->count, count, counter->min, counter->max);
3087#endif
3088 ret = ((count >= counter->min) && (count <= counter->max));
Daniel Veillard567a45b2005-10-18 19:11:55 +00003089 if ((ret) && (counter->min != counter->max))
3090 deter = 0;
Daniel Veillard4255d502002-04-16 15:50:10 +00003091 } else if (atom == NULL) {
3092 fprintf(stderr, "epsilon transition left at runtime\n");
3093 exec->status = -2;
3094 break;
3095 } else if (exec->inputString[exec->index] != 0) {
3096 codepoint = CUR_SCHAR(&(exec->inputString[exec->index]), len);
3097 ret = xmlRegCheckCharacter(atom, codepoint);
William M. Brack0e00b282004-04-26 15:40:47 +00003098 if ((ret == 1) && (atom->min >= 0) && (atom->max > 0)) {
Daniel Veillard4255d502002-04-16 15:50:10 +00003099 xmlRegStatePtr to = comp->states[trans->to];
3100
3101 /*
3102 * this is a multiple input sequence
Daniel Veillardfc6eca02005-11-01 15:24:02 +00003103 * If there is a counter associated increment it now.
3104 * before potentially saving and rollback
Daniel Veillard4255d502002-04-16 15:50:10 +00003105 */
Daniel Veillardfc6eca02005-11-01 15:24:02 +00003106 if (trans->counter >= 0) {
Daniel Veillard11ce4002006-03-10 00:36:23 +00003107 if (exec->counts == NULL) {
3108 exec->status = -1;
3109 goto error;
3110 }
Daniel Veillardfc6eca02005-11-01 15:24:02 +00003111#ifdef DEBUG_REGEXP_EXEC
3112 printf("Increasing count %d\n", trans->counter);
3113#endif
3114 exec->counts[trans->counter]++;
3115 }
Daniel Veillard4255d502002-04-16 15:50:10 +00003116 if (exec->state->nbTrans > exec->transno + 1) {
3117 xmlFARegExecSave(exec);
3118 }
3119 exec->transcount = 1;
3120 do {
3121 /*
3122 * Try to progress as much as possible on the input
3123 */
3124 if (exec->transcount == atom->max) {
3125 break;
3126 }
3127 exec->index += len;
3128 /*
3129 * End of input: stop here
3130 */
3131 if (exec->inputString[exec->index] == 0) {
3132 exec->index -= len;
3133 break;
3134 }
3135 if (exec->transcount >= atom->min) {
3136 int transno = exec->transno;
3137 xmlRegStatePtr state = exec->state;
3138
3139 /*
3140 * The transition is acceptable save it
3141 */
3142 exec->transno = -1; /* trick */
3143 exec->state = to;
3144 xmlFARegExecSave(exec);
3145 exec->transno = transno;
3146 exec->state = state;
3147 }
3148 codepoint = CUR_SCHAR(&(exec->inputString[exec->index]),
3149 len);
3150 ret = xmlRegCheckCharacter(atom, codepoint);
3151 exec->transcount++;
3152 } while (ret == 1);
3153 if (exec->transcount < atom->min)
3154 ret = 0;
3155
3156 /*
3157 * If the last check failed but one transition was found
3158 * possible, rollback
3159 */
3160 if (ret < 0)
3161 ret = 0;
3162 if (ret == 0) {
3163 goto rollback;
3164 }
Daniel Veillardfc6eca02005-11-01 15:24:02 +00003165 if (trans->counter >= 0) {
Daniel Veillard11ce4002006-03-10 00:36:23 +00003166 if (exec->counts == NULL) {
3167 exec->status = -1;
3168 goto error;
3169 }
Daniel Veillardfc6eca02005-11-01 15:24:02 +00003170#ifdef DEBUG_REGEXP_EXEC
3171 printf("Decreasing count %d\n", trans->counter);
3172#endif
3173 exec->counts[trans->counter]--;
3174 }
William M. Brack0e00b282004-04-26 15:40:47 +00003175 } else if ((ret == 0) && (atom->min == 0) && (atom->max > 0)) {
3176 /*
3177 * we don't match on the codepoint, but minOccurs of 0
3178 * says that's ok. Setting len to 0 inhibits stepping
3179 * over the codepoint.
3180 */
3181 exec->transcount = 1;
3182 len = 0;
3183 ret = 1;
Daniel Veillard4255d502002-04-16 15:50:10 +00003184 }
William M. Brack0e00b282004-04-26 15:40:47 +00003185 } else if ((atom->min == 0) && (atom->max > 0)) {
3186 /* another spot to match when minOccurs is 0 */
3187 exec->transcount = 1;
3188 len = 0;
3189 ret = 1;
Daniel Veillard4255d502002-04-16 15:50:10 +00003190 }
3191 if (ret == 1) {
Daniel Veillard567a45b2005-10-18 19:11:55 +00003192 if ((trans->nd == 1) ||
3193 ((trans->count >= 0) && (deter == 0) &&
3194 (exec->state->nbTrans > exec->transno + 1))) {
Daniel Veillardaa622012005-10-20 15:55:25 +00003195#ifdef DEBUG_REGEXP_EXEC
3196 if (trans->nd == 1)
3197 printf("Saving on nd transition atom %d for %c at %d\n",
3198 trans->atom->no, codepoint, exec->index);
3199 else
3200 printf("Saving on counted transition count %d for %c at %d\n",
3201 trans->count, codepoint, exec->index);
3202#endif
Daniel Veillard4255d502002-04-16 15:50:10 +00003203 xmlFARegExecSave(exec);
3204 }
3205 if (trans->counter >= 0) {
Daniel Veillard11ce4002006-03-10 00:36:23 +00003206 if (exec->counts == NULL) {
3207 exec->status = -1;
3208 goto error;
3209 }
Daniel Veillard4255d502002-04-16 15:50:10 +00003210#ifdef DEBUG_REGEXP_EXEC
3211 printf("Increasing count %d\n", trans->counter);
3212#endif
3213 exec->counts[trans->counter]++;
3214 }
Daniel Veillard10752282005-08-08 13:05:13 +00003215 if ((trans->count >= 0) &&
3216 (trans->count < REGEXP_ALL_COUNTER)) {
Daniel Veillard11ce4002006-03-10 00:36:23 +00003217 if (exec->counts == NULL) {
3218 exec->status = -1;
3219 goto error;
3220 }
Daniel Veillard10752282005-08-08 13:05:13 +00003221#ifdef DEBUG_REGEXP_EXEC
3222 printf("resetting count %d on transition\n",
3223 trans->count);
3224#endif
3225 exec->counts[trans->count] = 0;
3226 }
Daniel Veillard4255d502002-04-16 15:50:10 +00003227#ifdef DEBUG_REGEXP_EXEC
3228 printf("entering state %d\n", trans->to);
3229#endif
3230 exec->state = comp->states[trans->to];
3231 exec->transno = 0;
3232 if (trans->atom != NULL) {
3233 exec->index += len;
3234 }
3235 goto progress;
3236 } else if (ret < 0) {
3237 exec->status = -4;
3238 break;
3239 }
3240 }
3241 if ((exec->transno != 0) || (exec->state->nbTrans == 0)) {
3242rollback:
3243 /*
3244 * Failed to find a way out
3245 */
3246 exec->determinist = 0;
Daniel Veillardaa622012005-10-20 15:55:25 +00003247#ifdef DEBUG_REGEXP_EXEC
3248 printf("rollback from state %d on %d:%c\n", exec->state->no,
3249 codepoint,codepoint);
3250#endif
Daniel Veillard4255d502002-04-16 15:50:10 +00003251 xmlFARegExecRollBack(exec);
3252 }
3253progress:
3254 continue;
3255 }
Daniel Veillard11ce4002006-03-10 00:36:23 +00003256error:
Daniel Veillard4255d502002-04-16 15:50:10 +00003257 if (exec->rollbacks != NULL) {
3258 if (exec->counts != NULL) {
3259 int i;
3260
3261 for (i = 0;i < exec->maxRollbacks;i++)
3262 if (exec->rollbacks[i].counts != NULL)
3263 xmlFree(exec->rollbacks[i].counts);
3264 }
3265 xmlFree(exec->rollbacks);
3266 }
3267 if (exec->counts != NULL)
3268 xmlFree(exec->counts);
3269 if (exec->status == 0)
3270 return(1);
Daniel Veillard94cc1032005-09-15 13:09:00 +00003271 if (exec->status == -1) {
3272 if (exec->nbPush > MAX_PUSH)
3273 return(-1);
Daniel Veillard4255d502002-04-16 15:50:10 +00003274 return(0);
Daniel Veillard94cc1032005-09-15 13:09:00 +00003275 }
Daniel Veillard4255d502002-04-16 15:50:10 +00003276 return(exec->status);
3277}
3278
3279/************************************************************************
3280 * *
William M. Brackddf71d62004-05-06 04:17:26 +00003281 * Progressive interface to the verifier one atom at a time *
Daniel Veillard4255d502002-04-16 15:50:10 +00003282 * *
3283 ************************************************************************/
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003284#ifdef DEBUG_ERR
3285static void testerr(xmlRegExecCtxtPtr exec);
3286#endif
Daniel Veillard4255d502002-04-16 15:50:10 +00003287
3288/**
Daniel Veillard01c13b52002-12-10 15:19:08 +00003289 * xmlRegNewExecCtxt:
Daniel Veillard4255d502002-04-16 15:50:10 +00003290 * @comp: a precompiled regular expression
3291 * @callback: a callback function used for handling progresses in the
3292 * automata matching phase
3293 * @data: the context data associated to the callback in this context
3294 *
3295 * Build a context used for progressive evaluation of a regexp.
Daniel Veillard01c13b52002-12-10 15:19:08 +00003296 *
3297 * Returns the new context
Daniel Veillard4255d502002-04-16 15:50:10 +00003298 */
3299xmlRegExecCtxtPtr
3300xmlRegNewExecCtxt(xmlRegexpPtr comp, xmlRegExecCallbacks callback, void *data) {
3301 xmlRegExecCtxtPtr exec;
3302
3303 if (comp == NULL)
3304 return(NULL);
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00003305 if ((comp->compact == NULL) && (comp->states == NULL))
3306 return(NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +00003307 exec = (xmlRegExecCtxtPtr) xmlMalloc(sizeof(xmlRegExecCtxt));
3308 if (exec == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00003309 xmlRegexpErrMemory(NULL, "creating execution context");
Daniel Veillard4255d502002-04-16 15:50:10 +00003310 return(NULL);
3311 }
3312 memset(exec, 0, sizeof(xmlRegExecCtxt));
3313 exec->inputString = NULL;
3314 exec->index = 0;
3315 exec->determinist = 1;
3316 exec->maxRollbacks = 0;
3317 exec->nbRollbacks = 0;
3318 exec->rollbacks = NULL;
3319 exec->status = 0;
3320 exec->comp = comp;
Daniel Veillard23e73572002-09-19 19:56:43 +00003321 if (comp->compact == NULL)
3322 exec->state = comp->states[0];
Daniel Veillard4255d502002-04-16 15:50:10 +00003323 exec->transno = 0;
3324 exec->transcount = 0;
3325 exec->callback = callback;
3326 exec->data = data;
3327 if (comp->nbCounters > 0) {
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003328 /*
3329 * For error handling, exec->counts is allocated twice the size
3330 * the second half is used to store the data in case of rollback
3331 */
3332 exec->counts = (int *) xmlMalloc(comp->nbCounters * sizeof(int)
3333 * 2);
Daniel Veillard4255d502002-04-16 15:50:10 +00003334 if (exec->counts == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00003335 xmlRegexpErrMemory(NULL, "creating execution context");
Daniel Veillard4255d502002-04-16 15:50:10 +00003336 xmlFree(exec);
3337 return(NULL);
3338 }
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003339 memset(exec->counts, 0, comp->nbCounters * sizeof(int) * 2);
3340 exec->errCounts = &exec->counts[comp->nbCounters];
3341 } else {
Daniel Veillard4255d502002-04-16 15:50:10 +00003342 exec->counts = NULL;
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003343 exec->errCounts = NULL;
3344 }
Daniel Veillard4255d502002-04-16 15:50:10 +00003345 exec->inputStackMax = 0;
3346 exec->inputStackNr = 0;
3347 exec->inputStack = NULL;
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003348 exec->errStateNo = -1;
3349 exec->errString = NULL;
Daniel Veillard94cc1032005-09-15 13:09:00 +00003350 exec->nbPush = 0;
Daniel Veillard4255d502002-04-16 15:50:10 +00003351 return(exec);
3352}
3353
3354/**
3355 * xmlRegFreeExecCtxt:
3356 * @exec: a regular expression evaulation context
3357 *
3358 * Free the structures associated to a regular expression evaulation context.
3359 */
3360void
3361xmlRegFreeExecCtxt(xmlRegExecCtxtPtr exec) {
3362 if (exec == NULL)
3363 return;
3364
3365 if (exec->rollbacks != NULL) {
3366 if (exec->counts != NULL) {
3367 int i;
3368
3369 for (i = 0;i < exec->maxRollbacks;i++)
3370 if (exec->rollbacks[i].counts != NULL)
3371 xmlFree(exec->rollbacks[i].counts);
3372 }
3373 xmlFree(exec->rollbacks);
3374 }
3375 if (exec->counts != NULL)
3376 xmlFree(exec->counts);
3377 if (exec->inputStack != NULL) {
3378 int i;
3379
Daniel Veillard32370232002-10-16 14:08:14 +00003380 for (i = 0;i < exec->inputStackNr;i++) {
3381 if (exec->inputStack[i].value != NULL)
3382 xmlFree(exec->inputStack[i].value);
3383 }
Daniel Veillard4255d502002-04-16 15:50:10 +00003384 xmlFree(exec->inputStack);
3385 }
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003386 if (exec->errString != NULL)
3387 xmlFree(exec->errString);
Daniel Veillard4255d502002-04-16 15:50:10 +00003388 xmlFree(exec);
3389}
3390
3391static void
3392xmlFARegExecSaveInputString(xmlRegExecCtxtPtr exec, const xmlChar *value,
3393 void *data) {
3394#ifdef DEBUG_PUSH
3395 printf("saving value: %d:%s\n", exec->inputStackNr, value);
3396#endif
3397 if (exec->inputStackMax == 0) {
3398 exec->inputStackMax = 4;
3399 exec->inputStack = (xmlRegInputTokenPtr)
3400 xmlMalloc(exec->inputStackMax * sizeof(xmlRegInputToken));
3401 if (exec->inputStack == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00003402 xmlRegexpErrMemory(NULL, "pushing input string");
Daniel Veillard4255d502002-04-16 15:50:10 +00003403 exec->inputStackMax = 0;
3404 return;
3405 }
3406 } else if (exec->inputStackNr + 1 >= exec->inputStackMax) {
3407 xmlRegInputTokenPtr tmp;
3408
3409 exec->inputStackMax *= 2;
3410 tmp = (xmlRegInputTokenPtr) xmlRealloc(exec->inputStack,
3411 exec->inputStackMax * sizeof(xmlRegInputToken));
3412 if (tmp == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00003413 xmlRegexpErrMemory(NULL, "pushing input string");
Daniel Veillard4255d502002-04-16 15:50:10 +00003414 exec->inputStackMax /= 2;
3415 return;
3416 }
3417 exec->inputStack = tmp;
3418 }
3419 exec->inputStack[exec->inputStackNr].value = xmlStrdup(value);
3420 exec->inputStack[exec->inputStackNr].data = data;
3421 exec->inputStackNr++;
3422 exec->inputStack[exec->inputStackNr].value = NULL;
3423 exec->inputStack[exec->inputStackNr].data = NULL;
3424}
3425
Daniel Veillardc0826a72004-08-10 14:17:33 +00003426/**
3427 * xmlRegStrEqualWildcard:
3428 * @expStr: the string to be evaluated
3429 * @valStr: the validation string
3430 *
3431 * Checks if both strings are equal or have the same content. "*"
3432 * can be used as a wildcard in @valStr; "|" is used as a seperator of
3433 * substrings in both @expStr and @valStr.
3434 *
3435 * Returns 1 if the comparison is satisfied and the number of substrings
3436 * is equal, 0 otherwise.
3437 */
3438
3439static int
3440xmlRegStrEqualWildcard(const xmlChar *expStr, const xmlChar *valStr) {
3441 if (expStr == valStr) return(1);
3442 if (expStr == NULL) return(0);
3443 if (valStr == NULL) return(0);
3444 do {
3445 /*
3446 * Eval if we have a wildcard for the current item.
3447 */
3448 if (*expStr != *valStr) {
Daniel Veillard4f82c8a2005-08-09 21:40:08 +00003449 /* if one of them starts with a wildcard make valStr be it */
3450 if (*valStr == '*') {
3451 const xmlChar *tmp;
3452
3453 tmp = valStr;
3454 valStr = expStr;
3455 expStr = tmp;
3456 }
Daniel Veillardc0826a72004-08-10 14:17:33 +00003457 if ((*valStr != 0) && (*expStr != 0) && (*expStr++ == '*')) {
3458 do {
3459 if (*valStr == XML_REG_STRING_SEPARATOR)
3460 break;
Kasimier T. Buchcikc0e833f2005-04-19 15:02:20 +00003461 valStr++;
Daniel Veillardc0826a72004-08-10 14:17:33 +00003462 } while (*valStr != 0);
3463 continue;
3464 } else
3465 return(0);
3466 }
Kasimier T. Buchcikc0e833f2005-04-19 15:02:20 +00003467 expStr++;
3468 valStr++;
Daniel Veillardc0826a72004-08-10 14:17:33 +00003469 } while (*valStr != 0);
3470 if (*expStr != 0)
3471 return (0);
3472 else
3473 return (1);
3474}
Daniel Veillard4255d502002-04-16 15:50:10 +00003475
3476/**
Daniel Veillard23e73572002-09-19 19:56:43 +00003477 * xmlRegCompactPushString:
3478 * @exec: a regexp execution context
3479 * @comp: the precompiled exec with a compact table
3480 * @value: a string token input
3481 * @data: data associated to the token to reuse in callbacks
3482 *
3483 * Push one input token in the execution context
3484 *
3485 * Returns: 1 if the regexp reached a final state, 0 if non-final, and
3486 * a negative value in case of error.
3487 */
3488static int
3489xmlRegCompactPushString(xmlRegExecCtxtPtr exec,
3490 xmlRegexpPtr comp,
3491 const xmlChar *value,
3492 void *data) {
3493 int state = exec->index;
3494 int i, target;
3495
3496 if ((comp == NULL) || (comp->compact == NULL) || (comp->stringMap == NULL))
3497 return(-1);
3498
3499 if (value == NULL) {
3500 /*
3501 * are we at a final state ?
3502 */
3503 if (comp->compact[state * (comp->nbstrings + 1)] ==
3504 XML_REGEXP_FINAL_STATE)
3505 return(1);
3506 return(0);
3507 }
3508
3509#ifdef DEBUG_PUSH
3510 printf("value pushed: %s\n", value);
3511#endif
3512
3513 /*
William M. Brackddf71d62004-05-06 04:17:26 +00003514 * Examine all outside transitions from current state
Daniel Veillard23e73572002-09-19 19:56:43 +00003515 */
3516 for (i = 0;i < comp->nbstrings;i++) {
3517 target = comp->compact[state * (comp->nbstrings + 1) + i + 1];
3518 if ((target > 0) && (target <= comp->nbstates)) {
Daniel Veillardc0826a72004-08-10 14:17:33 +00003519 target--; /* to avoid 0 */
3520 if (xmlRegStrEqualWildcard(comp->stringMap[i], value)) {
3521 exec->index = target;
Daniel Veillard118aed72002-09-24 14:13:13 +00003522 if ((exec->callback != NULL) && (comp->transdata != NULL)) {
3523 exec->callback(exec->data, value,
3524 comp->transdata[state * comp->nbstrings + i], data);
3525 }
Daniel Veillard23e73572002-09-19 19:56:43 +00003526#ifdef DEBUG_PUSH
3527 printf("entering state %d\n", target);
3528#endif
3529 if (comp->compact[target * (comp->nbstrings + 1)] ==
Daniel Veillardcc026dc2005-01-12 13:21:17 +00003530 XML_REGEXP_SINK_STATE)
3531 goto error;
3532
3533 if (comp->compact[target * (comp->nbstrings + 1)] ==
Daniel Veillard23e73572002-09-19 19:56:43 +00003534 XML_REGEXP_FINAL_STATE)
3535 return(1);
3536 return(0);
3537 }
3538 }
3539 }
3540 /*
3541 * Failed to find an exit transition out from current state for the
3542 * current token
3543 */
3544#ifdef DEBUG_PUSH
3545 printf("failed to find a transition for %s on state %d\n", value, state);
3546#endif
Daniel Veillardcc026dc2005-01-12 13:21:17 +00003547error:
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003548 if (exec->errString != NULL)
3549 xmlFree(exec->errString);
3550 exec->errString = xmlStrdup(value);
3551 exec->errStateNo = state;
Daniel Veillard23e73572002-09-19 19:56:43 +00003552 exec->status = -1;
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003553#ifdef DEBUG_ERR
3554 testerr(exec);
3555#endif
Daniel Veillard23e73572002-09-19 19:56:43 +00003556 return(-1);
3557}
3558
3559/**
Daniel Veillard6e65e152005-08-09 11:09:52 +00003560 * xmlRegExecPushStringInternal:
Daniel Veillardea7751d2002-12-20 00:16:24 +00003561 * @exec: a regexp execution context or NULL to indicate the end
Daniel Veillard4255d502002-04-16 15:50:10 +00003562 * @value: a string token input
3563 * @data: data associated to the token to reuse in callbacks
Daniel Veillard6e65e152005-08-09 11:09:52 +00003564 * @compound: value was assembled from 2 strings
Daniel Veillard4255d502002-04-16 15:50:10 +00003565 *
3566 * Push one input token in the execution context
3567 *
3568 * Returns: 1 if the regexp reached a final state, 0 if non-final, and
3569 * a negative value in case of error.
3570 */
Daniel Veillard6e65e152005-08-09 11:09:52 +00003571static int
3572xmlRegExecPushStringInternal(xmlRegExecCtxtPtr exec, const xmlChar *value,
3573 void *data, int compound) {
Daniel Veillard4255d502002-04-16 15:50:10 +00003574 xmlRegTransPtr trans;
3575 xmlRegAtomPtr atom;
3576 int ret;
3577 int final = 0;
Daniel Veillard90700152005-01-08 22:05:09 +00003578 int progress = 1;
Daniel Veillard4255d502002-04-16 15:50:10 +00003579
3580 if (exec == NULL)
3581 return(-1);
Daniel Veillard23e73572002-09-19 19:56:43 +00003582 if (exec->comp == NULL)
3583 return(-1);
Daniel Veillard4255d502002-04-16 15:50:10 +00003584 if (exec->status != 0)
3585 return(exec->status);
3586
Daniel Veillard23e73572002-09-19 19:56:43 +00003587 if (exec->comp->compact != NULL)
3588 return(xmlRegCompactPushString(exec, exec->comp, value, data));
3589
Daniel Veillard4255d502002-04-16 15:50:10 +00003590 if (value == NULL) {
3591 if (exec->state->type == XML_REGEXP_FINAL_STATE)
3592 return(1);
3593 final = 1;
3594 }
3595
3596#ifdef DEBUG_PUSH
3597 printf("value pushed: %s\n", value);
3598#endif
3599 /*
3600 * If we have an active rollback stack push the new value there
3601 * and get back to where we were left
3602 */
3603 if ((value != NULL) && (exec->inputStackNr > 0)) {
3604 xmlFARegExecSaveInputString(exec, value, data);
3605 value = exec->inputStack[exec->index].value;
3606 data = exec->inputStack[exec->index].data;
3607#ifdef DEBUG_PUSH
3608 printf("value loaded: %s\n", value);
3609#endif
3610 }
3611
3612 while ((exec->status == 0) &&
3613 ((value != NULL) ||
3614 ((final == 1) &&
3615 (exec->state->type != XML_REGEXP_FINAL_STATE)))) {
3616
3617 /*
3618 * End of input on non-terminal state, rollback, however we may
3619 * still have epsilon like transition for counted transitions
3620 * on counters, in that case don't break too early.
3621 */
Daniel Veillardb509f152002-04-17 16:28:10 +00003622 if ((value == NULL) && (exec->counts == NULL))
Daniel Veillard4255d502002-04-16 15:50:10 +00003623 goto rollback;
3624
3625 exec->transcount = 0;
3626 for (;exec->transno < exec->state->nbTrans;exec->transno++) {
3627 trans = &exec->state->trans[exec->transno];
3628 if (trans->to < 0)
3629 continue;
3630 atom = trans->atom;
3631 ret = 0;
Daniel Veillard441bc322002-04-20 17:38:48 +00003632 if (trans->count == REGEXP_ALL_LAX_COUNTER) {
3633 int i;
3634 int count;
3635 xmlRegTransPtr t;
3636 xmlRegCounterPtr counter;
3637
3638 ret = 0;
3639
3640#ifdef DEBUG_PUSH
3641 printf("testing all lax %d\n", trans->count);
3642#endif
3643 /*
3644 * Check all counted transitions from the current state
3645 */
3646 if ((value == NULL) && (final)) {
3647 ret = 1;
3648 } else if (value != NULL) {
3649 for (i = 0;i < exec->state->nbTrans;i++) {
3650 t = &exec->state->trans[i];
3651 if ((t->counter < 0) || (t == trans))
3652 continue;
3653 counter = &exec->comp->counters[t->counter];
3654 count = exec->counts[t->counter];
3655 if ((count < counter->max) &&
3656 (t->atom != NULL) &&
3657 (xmlStrEqual(value, t->atom->valuep))) {
3658 ret = 0;
3659 break;
3660 }
3661 if ((count >= counter->min) &&
3662 (count < counter->max) &&
Daniel Veillard11ce4002006-03-10 00:36:23 +00003663 (t->atom != NULL) &&
Daniel Veillard441bc322002-04-20 17:38:48 +00003664 (xmlStrEqual(value, t->atom->valuep))) {
3665 ret = 1;
3666 break;
3667 }
3668 }
3669 }
3670 } else if (trans->count == REGEXP_ALL_COUNTER) {
Daniel Veillard8a001f62002-04-20 07:24:11 +00003671 int i;
3672 int count;
3673 xmlRegTransPtr t;
3674 xmlRegCounterPtr counter;
3675
3676 ret = 1;
3677
3678#ifdef DEBUG_PUSH
3679 printf("testing all %d\n", trans->count);
3680#endif
3681 /*
3682 * Check all counted transitions from the current state
3683 */
3684 for (i = 0;i < exec->state->nbTrans;i++) {
3685 t = &exec->state->trans[i];
3686 if ((t->counter < 0) || (t == trans))
3687 continue;
3688 counter = &exec->comp->counters[t->counter];
3689 count = exec->counts[t->counter];
3690 if ((count < counter->min) || (count > counter->max)) {
3691 ret = 0;
3692 break;
3693 }
3694 }
3695 } else if (trans->count >= 0) {
Daniel Veillard4255d502002-04-16 15:50:10 +00003696 int count;
3697 xmlRegCounterPtr counter;
3698
3699 /*
3700 * A counted transition.
3701 */
3702
3703 count = exec->counts[trans->count];
3704 counter = &exec->comp->counters[trans->count];
3705#ifdef DEBUG_PUSH
3706 printf("testing count %d: val %d, min %d, max %d\n",
3707 trans->count, count, counter->min, counter->max);
3708#endif
3709 ret = ((count >= counter->min) && (count <= counter->max));
3710 } else if (atom == NULL) {
3711 fprintf(stderr, "epsilon transition left at runtime\n");
3712 exec->status = -2;
3713 break;
3714 } else if (value != NULL) {
Daniel Veillardc0826a72004-08-10 14:17:33 +00003715 ret = xmlRegStrEqualWildcard(atom->valuep, value);
Daniel Veillard6e65e152005-08-09 11:09:52 +00003716 if (atom->neg) {
Daniel Veillard9efc4762005-07-19 14:33:55 +00003717 ret = !ret;
Daniel Veillard6e65e152005-08-09 11:09:52 +00003718 if (!compound)
3719 ret = 0;
3720 }
Daniel Veillard441bc322002-04-20 17:38:48 +00003721 if ((ret == 1) && (trans->counter >= 0)) {
3722 xmlRegCounterPtr counter;
3723 int count;
3724
3725 count = exec->counts[trans->counter];
3726 counter = &exec->comp->counters[trans->counter];
3727 if (count >= counter->max)
3728 ret = 0;
3729 }
3730
Daniel Veillard4255d502002-04-16 15:50:10 +00003731 if ((ret == 1) && (atom->min > 0) && (atom->max > 0)) {
3732 xmlRegStatePtr to = exec->comp->states[trans->to];
3733
3734 /*
3735 * this is a multiple input sequence
3736 */
3737 if (exec->state->nbTrans > exec->transno + 1) {
3738 if (exec->inputStackNr <= 0) {
3739 xmlFARegExecSaveInputString(exec, value, data);
3740 }
3741 xmlFARegExecSave(exec);
3742 }
3743 exec->transcount = 1;
3744 do {
3745 /*
3746 * Try to progress as much as possible on the input
3747 */
3748 if (exec->transcount == atom->max) {
3749 break;
3750 }
3751 exec->index++;
3752 value = exec->inputStack[exec->index].value;
3753 data = exec->inputStack[exec->index].data;
3754#ifdef DEBUG_PUSH
3755 printf("value loaded: %s\n", value);
3756#endif
3757
3758 /*
3759 * End of input: stop here
3760 */
3761 if (value == NULL) {
3762 exec->index --;
3763 break;
3764 }
3765 if (exec->transcount >= atom->min) {
3766 int transno = exec->transno;
3767 xmlRegStatePtr state = exec->state;
3768
3769 /*
3770 * The transition is acceptable save it
3771 */
3772 exec->transno = -1; /* trick */
3773 exec->state = to;
3774 if (exec->inputStackNr <= 0) {
3775 xmlFARegExecSaveInputString(exec, value, data);
3776 }
3777 xmlFARegExecSave(exec);
3778 exec->transno = transno;
3779 exec->state = state;
3780 }
3781 ret = xmlStrEqual(value, atom->valuep);
3782 exec->transcount++;
3783 } while (ret == 1);
3784 if (exec->transcount < atom->min)
3785 ret = 0;
3786
3787 /*
3788 * If the last check failed but one transition was found
3789 * possible, rollback
3790 */
3791 if (ret < 0)
3792 ret = 0;
3793 if (ret == 0) {
3794 goto rollback;
3795 }
3796 }
3797 }
3798 if (ret == 1) {
William M. Brack98873952003-12-26 06:03:14 +00003799 if ((exec->callback != NULL) && (atom != NULL) &&
3800 (data != NULL)) {
Daniel Veillard4255d502002-04-16 15:50:10 +00003801 exec->callback(exec->data, atom->valuep,
3802 atom->data, data);
3803 }
3804 if (exec->state->nbTrans > exec->transno + 1) {
3805 if (exec->inputStackNr <= 0) {
3806 xmlFARegExecSaveInputString(exec, value, data);
3807 }
3808 xmlFARegExecSave(exec);
3809 }
3810 if (trans->counter >= 0) {
3811#ifdef DEBUG_PUSH
3812 printf("Increasing count %d\n", trans->counter);
3813#endif
3814 exec->counts[trans->counter]++;
3815 }
Daniel Veillard10752282005-08-08 13:05:13 +00003816 if ((trans->count >= 0) &&
3817 (trans->count < REGEXP_ALL_COUNTER)) {
3818#ifdef DEBUG_REGEXP_EXEC
3819 printf("resetting count %d on transition\n",
3820 trans->count);
3821#endif
3822 exec->counts[trans->count] = 0;
3823 }
Daniel Veillard4255d502002-04-16 15:50:10 +00003824#ifdef DEBUG_PUSH
3825 printf("entering state %d\n", trans->to);
3826#endif
Daniel Veillardcc026dc2005-01-12 13:21:17 +00003827 if ((exec->comp->states[trans->to] != NULL) &&
3828 (exec->comp->states[trans->to]->type ==
3829 XML_REGEXP_SINK_STATE)) {
3830 /*
3831 * entering a sink state, save the current state as error
3832 * state.
3833 */
3834 if (exec->errString != NULL)
3835 xmlFree(exec->errString);
3836 exec->errString = xmlStrdup(value);
3837 exec->errState = exec->state;
3838 memcpy(exec->errCounts, exec->counts,
3839 exec->comp->nbCounters * sizeof(int));
3840 }
Daniel Veillard4255d502002-04-16 15:50:10 +00003841 exec->state = exec->comp->states[trans->to];
3842 exec->transno = 0;
3843 if (trans->atom != NULL) {
3844 if (exec->inputStack != NULL) {
3845 exec->index++;
3846 if (exec->index < exec->inputStackNr) {
3847 value = exec->inputStack[exec->index].value;
3848 data = exec->inputStack[exec->index].data;
3849#ifdef DEBUG_PUSH
3850 printf("value loaded: %s\n", value);
3851#endif
3852 } else {
3853 value = NULL;
3854 data = NULL;
3855#ifdef DEBUG_PUSH
3856 printf("end of input\n");
3857#endif
3858 }
3859 } else {
3860 value = NULL;
3861 data = NULL;
3862#ifdef DEBUG_PUSH
3863 printf("end of input\n");
3864#endif
3865 }
3866 }
3867 goto progress;
3868 } else if (ret < 0) {
3869 exec->status = -4;
3870 break;
3871 }
3872 }
3873 if ((exec->transno != 0) || (exec->state->nbTrans == 0)) {
3874rollback:
Daniel Veillard90700152005-01-08 22:05:09 +00003875 /*
Daniel Veillardcc026dc2005-01-12 13:21:17 +00003876 * if we didn't yet rollback on the current input
3877 * store the current state as the error state.
Daniel Veillard90700152005-01-08 22:05:09 +00003878 */
Daniel Veillardcc026dc2005-01-12 13:21:17 +00003879 if ((progress) && (exec->state != NULL) &&
3880 (exec->state->type != XML_REGEXP_SINK_STATE)) {
Daniel Veillard90700152005-01-08 22:05:09 +00003881 progress = 0;
3882 if (exec->errString != NULL)
3883 xmlFree(exec->errString);
3884 exec->errString = xmlStrdup(value);
3885 exec->errState = exec->state;
3886 memcpy(exec->errCounts, exec->counts,
3887 exec->comp->nbCounters * sizeof(int));
3888 }
3889
Daniel Veillard4255d502002-04-16 15:50:10 +00003890 /*
3891 * Failed to find a way out
3892 */
3893 exec->determinist = 0;
3894 xmlFARegExecRollBack(exec);
3895 if (exec->status == 0) {
3896 value = exec->inputStack[exec->index].value;
3897 data = exec->inputStack[exec->index].data;
3898#ifdef DEBUG_PUSH
3899 printf("value loaded: %s\n", value);
3900#endif
3901 }
3902 }
Daniel Veillard90700152005-01-08 22:05:09 +00003903 continue;
Daniel Veillard4255d502002-04-16 15:50:10 +00003904progress:
Daniel Veillard90700152005-01-08 22:05:09 +00003905 progress = 1;
Daniel Veillard4255d502002-04-16 15:50:10 +00003906 continue;
3907 }
3908 if (exec->status == 0) {
3909 return(exec->state->type == XML_REGEXP_FINAL_STATE);
3910 }
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003911#ifdef DEBUG_ERR
Daniel Veillard90700152005-01-08 22:05:09 +00003912 if (exec->status < 0) {
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003913 testerr(exec);
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003914 }
Daniel Veillard90700152005-01-08 22:05:09 +00003915#endif
Daniel Veillard4255d502002-04-16 15:50:10 +00003916 return(exec->status);
3917}
3918
Daniel Veillard52b48c72003-04-13 19:53:42 +00003919/**
Daniel Veillard6e65e152005-08-09 11:09:52 +00003920 * xmlRegExecPushString:
3921 * @exec: a regexp execution context or NULL to indicate the end
3922 * @value: a string token input
3923 * @data: data associated to the token to reuse in callbacks
3924 *
3925 * Push one input token in the execution context
3926 *
3927 * Returns: 1 if the regexp reached a final state, 0 if non-final, and
3928 * a negative value in case of error.
3929 */
3930int
3931xmlRegExecPushString(xmlRegExecCtxtPtr exec, const xmlChar *value,
3932 void *data) {
3933 return(xmlRegExecPushStringInternal(exec, value, data, 0));
3934}
3935
3936/**
Daniel Veillard52b48c72003-04-13 19:53:42 +00003937 * xmlRegExecPushString2:
3938 * @exec: a regexp execution context or NULL to indicate the end
3939 * @value: the first string token input
3940 * @value2: the second string token input
3941 * @data: data associated to the token to reuse in callbacks
3942 *
3943 * Push one input token in the execution context
3944 *
3945 * Returns: 1 if the regexp reached a final state, 0 if non-final, and
3946 * a negative value in case of error.
3947 */
3948int
3949xmlRegExecPushString2(xmlRegExecCtxtPtr exec, const xmlChar *value,
3950 const xmlChar *value2, void *data) {
3951 xmlChar buf[150];
3952 int lenn, lenp, ret;
3953 xmlChar *str;
3954
3955 if (exec == NULL)
3956 return(-1);
3957 if (exec->comp == NULL)
3958 return(-1);
3959 if (exec->status != 0)
3960 return(exec->status);
3961
3962 if (value2 == NULL)
3963 return(xmlRegExecPushString(exec, value, data));
3964
3965 lenn = strlen((char *) value2);
3966 lenp = strlen((char *) value);
3967
3968 if (150 < lenn + lenp + 2) {
Daniel Veillard3c908dc2003-04-19 00:07:51 +00003969 str = (xmlChar *) xmlMallocAtomic(lenn + lenp + 2);
Daniel Veillard52b48c72003-04-13 19:53:42 +00003970 if (str == NULL) {
3971 exec->status = -1;
3972 return(-1);
3973 }
3974 } else {
3975 str = buf;
3976 }
3977 memcpy(&str[0], value, lenp);
Daniel Veillardc0826a72004-08-10 14:17:33 +00003978 str[lenp] = XML_REG_STRING_SEPARATOR;
Daniel Veillard52b48c72003-04-13 19:53:42 +00003979 memcpy(&str[lenp + 1], value2, lenn);
3980 str[lenn + lenp + 1] = 0;
3981
3982 if (exec->comp->compact != NULL)
3983 ret = xmlRegCompactPushString(exec, exec->comp, str, data);
3984 else
Daniel Veillard6e65e152005-08-09 11:09:52 +00003985 ret = xmlRegExecPushStringInternal(exec, str, data, 1);
Daniel Veillard52b48c72003-04-13 19:53:42 +00003986
3987 if (str != buf)
Daniel Veillard0b1ff142005-12-28 21:13:33 +00003988 xmlFree(str);
Daniel Veillard52b48c72003-04-13 19:53:42 +00003989 return(ret);
3990}
3991
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003992/**
Daniel Veillard77005e62005-07-19 16:26:18 +00003993 * xmlRegExecGetValues:
Daniel Veillardfc0b6f62005-01-09 17:48:02 +00003994 * @exec: a regexp execution context
3995 * @err: error extraction or normal one
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003996 * @nbval: pointer to the number of accepted values IN/OUT
Daniel Veillardcc026dc2005-01-12 13:21:17 +00003997 * @nbneg: return number of negative transitions
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003998 * @values: pointer to the array of acceptable values
Daniel Veillardfc0b6f62005-01-09 17:48:02 +00003999 * @terminal: return value if this was a terminal state
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00004000 *
Daniel Veillardfc0b6f62005-01-09 17:48:02 +00004001 * Extract informations from the regexp execution, internal routine to
4002 * implement xmlRegExecNextValues() and xmlRegExecErrInfo()
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00004003 *
4004 * Returns: 0 in case of success or -1 in case of error.
4005 */
Daniel Veillardfc0b6f62005-01-09 17:48:02 +00004006static int
4007xmlRegExecGetValues(xmlRegExecCtxtPtr exec, int err,
Daniel Veillardcc026dc2005-01-12 13:21:17 +00004008 int *nbval, int *nbneg,
4009 xmlChar **values, int *terminal) {
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00004010 int maxval;
Daniel Veillardcc026dc2005-01-12 13:21:17 +00004011 int nb = 0;
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00004012
Daniel Veillardcc026dc2005-01-12 13:21:17 +00004013 if ((exec == NULL) || (nbval == NULL) || (nbneg == NULL) ||
4014 (values == NULL) || (*nbval <= 0))
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00004015 return(-1);
Daniel Veillardfc0b6f62005-01-09 17:48:02 +00004016
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00004017 maxval = *nbval;
4018 *nbval = 0;
Daniel Veillardcc026dc2005-01-12 13:21:17 +00004019 *nbneg = 0;
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00004020 if ((exec->comp != NULL) && (exec->comp->compact != NULL)) {
4021 xmlRegexpPtr comp;
4022 int target, i, state;
4023
4024 comp = exec->comp;
Daniel Veillardfc0b6f62005-01-09 17:48:02 +00004025
4026 if (err) {
4027 if (exec->errStateNo == -1) return(-1);
4028 state = exec->errStateNo;
4029 } else {
4030 state = exec->index;
4031 }
4032 if (terminal != NULL) {
4033 if (comp->compact[state * (comp->nbstrings + 1)] ==
4034 XML_REGEXP_FINAL_STATE)
4035 *terminal = 1;
4036 else
4037 *terminal = 0;
4038 }
Daniel Veillardcc026dc2005-01-12 13:21:17 +00004039 for (i = 0;(i < comp->nbstrings) && (nb < maxval);i++) {
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00004040 target = comp->compact[state * (comp->nbstrings + 1) + i + 1];
Daniel Veillardcc026dc2005-01-12 13:21:17 +00004041 if ((target > 0) && (target <= comp->nbstates) &&
4042 (comp->compact[(target - 1) * (comp->nbstrings + 1)] !=
4043 XML_REGEXP_SINK_STATE)) {
4044 values[nb++] = comp->stringMap[i];
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00004045 (*nbval)++;
4046 }
4047 }
Daniel Veillardcc026dc2005-01-12 13:21:17 +00004048 for (i = 0;(i < comp->nbstrings) && (nb < maxval);i++) {
4049 target = comp->compact[state * (comp->nbstrings + 1) + i + 1];
4050 if ((target > 0) && (target <= comp->nbstates) &&
4051 (comp->compact[(target - 1) * (comp->nbstrings + 1)] ==
4052 XML_REGEXP_SINK_STATE)) {
4053 values[nb++] = comp->stringMap[i];
4054 (*nbneg)++;
4055 }
4056 }
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00004057 } else {
4058 int transno;
4059 xmlRegTransPtr trans;
4060 xmlRegAtomPtr atom;
Daniel Veillardfc0b6f62005-01-09 17:48:02 +00004061 xmlRegStatePtr state;
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00004062
Daniel Veillardfc0b6f62005-01-09 17:48:02 +00004063 if (terminal != NULL) {
4064 if (exec->state->type == XML_REGEXP_FINAL_STATE)
4065 *terminal = 1;
4066 else
4067 *terminal = 0;
4068 }
4069
4070 if (err) {
4071 if (exec->errState == NULL) return(-1);
4072 state = exec->errState;
4073 } else {
4074 if (exec->state == NULL) return(-1);
4075 state = exec->state;
4076 }
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00004077 for (transno = 0;
Daniel Veillardcc026dc2005-01-12 13:21:17 +00004078 (transno < state->nbTrans) && (nb < maxval);
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00004079 transno++) {
Daniel Veillardfc0b6f62005-01-09 17:48:02 +00004080 trans = &state->trans[transno];
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00004081 if (trans->to < 0)
4082 continue;
4083 atom = trans->atom;
4084 if ((atom == NULL) || (atom->valuep == NULL))
4085 continue;
4086 if (trans->count == REGEXP_ALL_LAX_COUNTER) {
Daniel Veillardcc026dc2005-01-12 13:21:17 +00004087 /* this should not be reached but ... */
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00004088 TODO;
4089 } else if (trans->count == REGEXP_ALL_COUNTER) {
Daniel Veillardcc026dc2005-01-12 13:21:17 +00004090 /* this should not be reached but ... */
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00004091 TODO;
4092 } else if (trans->counter >= 0) {
Daniel Veillard11ce4002006-03-10 00:36:23 +00004093 xmlRegCounterPtr counter = NULL;
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00004094 int count;
4095
Daniel Veillardfc0b6f62005-01-09 17:48:02 +00004096 if (err)
4097 count = exec->errCounts[trans->counter];
4098 else
4099 count = exec->counts[trans->counter];
Daniel Veillard11ce4002006-03-10 00:36:23 +00004100 if (exec->comp != NULL)
4101 counter = &exec->comp->counters[trans->counter];
4102 if ((counter == NULL) || (count < counter->max)) {
Daniel Veillard77005e62005-07-19 16:26:18 +00004103 if (atom->neg)
4104 values[nb++] = (xmlChar *) atom->valuep2;
4105 else
4106 values[nb++] = (xmlChar *) atom->valuep;
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00004107 (*nbval)++;
4108 }
4109 } else {
Daniel Veillardcc026dc2005-01-12 13:21:17 +00004110 if ((exec->comp->states[trans->to] != NULL) &&
4111 (exec->comp->states[trans->to]->type !=
4112 XML_REGEXP_SINK_STATE)) {
Daniel Veillard77005e62005-07-19 16:26:18 +00004113 if (atom->neg)
4114 values[nb++] = (xmlChar *) atom->valuep2;
4115 else
4116 values[nb++] = (xmlChar *) atom->valuep;
Daniel Veillardcc026dc2005-01-12 13:21:17 +00004117 (*nbval)++;
4118 }
4119 }
4120 }
4121 for (transno = 0;
4122 (transno < state->nbTrans) && (nb < maxval);
4123 transno++) {
4124 trans = &state->trans[transno];
4125 if (trans->to < 0)
4126 continue;
4127 atom = trans->atom;
4128 if ((atom == NULL) || (atom->valuep == NULL))
4129 continue;
4130 if (trans->count == REGEXP_ALL_LAX_COUNTER) {
4131 continue;
4132 } else if (trans->count == REGEXP_ALL_COUNTER) {
4133 continue;
4134 } else if (trans->counter >= 0) {
4135 continue;
4136 } else {
4137 if ((exec->comp->states[trans->to] != NULL) &&
4138 (exec->comp->states[trans->to]->type ==
4139 XML_REGEXP_SINK_STATE)) {
Daniel Veillard77005e62005-07-19 16:26:18 +00004140 if (atom->neg)
4141 values[nb++] = (xmlChar *) atom->valuep2;
4142 else
4143 values[nb++] = (xmlChar *) atom->valuep;
Daniel Veillardcc026dc2005-01-12 13:21:17 +00004144 (*nbneg)++;
4145 }
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00004146 }
4147 }
4148 }
4149 return(0);
4150}
4151
Daniel Veillardfc0b6f62005-01-09 17:48:02 +00004152/**
4153 * xmlRegExecNextValues:
4154 * @exec: a regexp execution context
4155 * @nbval: pointer to the number of accepted values IN/OUT
Daniel Veillardcc026dc2005-01-12 13:21:17 +00004156 * @nbneg: return number of negative transitions
Daniel Veillardfc0b6f62005-01-09 17:48:02 +00004157 * @values: pointer to the array of acceptable values
4158 * @terminal: return value if this was a terminal state
4159 *
4160 * Extract informations from the regexp execution,
4161 * the parameter @values must point to an array of @nbval string pointers
4162 * on return nbval will contain the number of possible strings in that
4163 * state and the @values array will be updated with them. The string values
4164 * returned will be freed with the @exec context and don't need to be
4165 * deallocated.
4166 *
4167 * Returns: 0 in case of success or -1 in case of error.
4168 */
4169int
Daniel Veillardcc026dc2005-01-12 13:21:17 +00004170xmlRegExecNextValues(xmlRegExecCtxtPtr exec, int *nbval, int *nbneg,
4171 xmlChar **values, int *terminal) {
4172 return(xmlRegExecGetValues(exec, 0, nbval, nbneg, values, terminal));
Daniel Veillardfc0b6f62005-01-09 17:48:02 +00004173}
4174
4175/**
4176 * xmlRegExecErrInfo:
4177 * @exec: a regexp execution context generating an error
4178 * @string: return value for the error string
4179 * @nbval: pointer to the number of accepted values IN/OUT
Daniel Veillardcc026dc2005-01-12 13:21:17 +00004180 * @nbneg: return number of negative transitions
Daniel Veillardfc0b6f62005-01-09 17:48:02 +00004181 * @values: pointer to the array of acceptable values
4182 * @terminal: return value if this was a terminal state
4183 *
4184 * Extract error informations from the regexp execution, the parameter
4185 * @string will be updated with the value pushed and not accepted,
4186 * the parameter @values must point to an array of @nbval string pointers
4187 * on return nbval will contain the number of possible strings in that
4188 * state and the @values array will be updated with them. The string values
4189 * returned will be freed with the @exec context and don't need to be
4190 * deallocated.
4191 *
4192 * Returns: 0 in case of success or -1 in case of error.
4193 */
4194int
4195xmlRegExecErrInfo(xmlRegExecCtxtPtr exec, const xmlChar **string,
Daniel Veillardcc026dc2005-01-12 13:21:17 +00004196 int *nbval, int *nbneg, xmlChar **values, int *terminal) {
Daniel Veillardfc0b6f62005-01-09 17:48:02 +00004197 if (exec == NULL)
4198 return(-1);
4199 if (string != NULL) {
4200 if (exec->status != 0)
4201 *string = exec->errString;
4202 else
4203 *string = NULL;
4204 }
Daniel Veillardcc026dc2005-01-12 13:21:17 +00004205 return(xmlRegExecGetValues(exec, 1, nbval, nbneg, values, terminal));
Daniel Veillardfc0b6f62005-01-09 17:48:02 +00004206}
4207
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00004208#ifdef DEBUG_ERR
4209static void testerr(xmlRegExecCtxtPtr exec) {
4210 const xmlChar *string;
Daniel Veillardcee2b3a2005-01-25 00:22:52 +00004211 xmlChar *values[5];
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00004212 int nb = 5;
Daniel Veillardcc026dc2005-01-12 13:21:17 +00004213 int nbneg;
Daniel Veillardfc0b6f62005-01-09 17:48:02 +00004214 int terminal;
Daniel Veillardcc026dc2005-01-12 13:21:17 +00004215 xmlRegExecErrInfo(exec, &string, &nb, &nbneg, &values[0], &terminal);
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00004216}
4217#endif
4218
Daniel Veillard4255d502002-04-16 15:50:10 +00004219#if 0
4220static int
4221xmlRegExecPushChar(xmlRegExecCtxtPtr exec, int UCS) {
4222 xmlRegTransPtr trans;
4223 xmlRegAtomPtr atom;
4224 int ret;
4225 int codepoint, len;
4226
4227 if (exec == NULL)
4228 return(-1);
4229 if (exec->status != 0)
4230 return(exec->status);
4231
4232 while ((exec->status == 0) &&
4233 ((exec->inputString[exec->index] != 0) ||
4234 (exec->state->type != XML_REGEXP_FINAL_STATE))) {
4235
4236 /*
4237 * End of input on non-terminal state, rollback, however we may
4238 * still have epsilon like transition for counted transitions
4239 * on counters, in that case don't break too early.
4240 */
4241 if ((exec->inputString[exec->index] == 0) && (exec->counts == NULL))
4242 goto rollback;
4243
4244 exec->transcount = 0;
4245 for (;exec->transno < exec->state->nbTrans;exec->transno++) {
4246 trans = &exec->state->trans[exec->transno];
4247 if (trans->to < 0)
4248 continue;
4249 atom = trans->atom;
4250 ret = 0;
4251 if (trans->count >= 0) {
4252 int count;
4253 xmlRegCounterPtr counter;
4254
4255 /*
4256 * A counted transition.
4257 */
4258
4259 count = exec->counts[trans->count];
4260 counter = &exec->comp->counters[trans->count];
4261#ifdef DEBUG_REGEXP_EXEC
4262 printf("testing count %d: val %d, min %d, max %d\n",
4263 trans->count, count, counter->min, counter->max);
4264#endif
4265 ret = ((count >= counter->min) && (count <= counter->max));
4266 } else if (atom == NULL) {
4267 fprintf(stderr, "epsilon transition left at runtime\n");
4268 exec->status = -2;
4269 break;
4270 } else if (exec->inputString[exec->index] != 0) {
4271 codepoint = CUR_SCHAR(&(exec->inputString[exec->index]), len);
4272 ret = xmlRegCheckCharacter(atom, codepoint);
4273 if ((ret == 1) && (atom->min > 0) && (atom->max > 0)) {
4274 xmlRegStatePtr to = exec->comp->states[trans->to];
4275
4276 /*
4277 * this is a multiple input sequence
4278 */
4279 if (exec->state->nbTrans > exec->transno + 1) {
4280 xmlFARegExecSave(exec);
4281 }
4282 exec->transcount = 1;
4283 do {
4284 /*
4285 * Try to progress as much as possible on the input
4286 */
4287 if (exec->transcount == atom->max) {
4288 break;
4289 }
4290 exec->index += len;
4291 /*
4292 * End of input: stop here
4293 */
4294 if (exec->inputString[exec->index] == 0) {
4295 exec->index -= len;
4296 break;
4297 }
4298 if (exec->transcount >= atom->min) {
4299 int transno = exec->transno;
4300 xmlRegStatePtr state = exec->state;
4301
4302 /*
4303 * The transition is acceptable save it
4304 */
4305 exec->transno = -1; /* trick */
4306 exec->state = to;
4307 xmlFARegExecSave(exec);
4308 exec->transno = transno;
4309 exec->state = state;
4310 }
4311 codepoint = CUR_SCHAR(&(exec->inputString[exec->index]),
4312 len);
4313 ret = xmlRegCheckCharacter(atom, codepoint);
4314 exec->transcount++;
4315 } while (ret == 1);
4316 if (exec->transcount < atom->min)
4317 ret = 0;
4318
4319 /*
4320 * If the last check failed but one transition was found
4321 * possible, rollback
4322 */
4323 if (ret < 0)
4324 ret = 0;
4325 if (ret == 0) {
4326 goto rollback;
4327 }
4328 }
4329 }
4330 if (ret == 1) {
4331 if (exec->state->nbTrans > exec->transno + 1) {
4332 xmlFARegExecSave(exec);
4333 }
Daniel Veillard54eb0242006-03-21 23:17:57 +00004334 /*
4335 * restart count for expressions like this ((abc){2})*
4336 */
4337 if (trans->count >= 0) {
4338#ifdef DEBUG_REGEXP_EXEC
4339 printf("Reset count %d\n", trans->count);
4340#endif
4341 exec->counts[trans->count] = 0;
4342 }
Daniel Veillard4255d502002-04-16 15:50:10 +00004343 if (trans->counter >= 0) {
4344#ifdef DEBUG_REGEXP_EXEC
4345 printf("Increasing count %d\n", trans->counter);
4346#endif
4347 exec->counts[trans->counter]++;
4348 }
4349#ifdef DEBUG_REGEXP_EXEC
4350 printf("entering state %d\n", trans->to);
4351#endif
4352 exec->state = exec->comp->states[trans->to];
4353 exec->transno = 0;
4354 if (trans->atom != NULL) {
4355 exec->index += len;
4356 }
4357 goto progress;
4358 } else if (ret < 0) {
4359 exec->status = -4;
4360 break;
4361 }
4362 }
4363 if ((exec->transno != 0) || (exec->state->nbTrans == 0)) {
4364rollback:
4365 /*
4366 * Failed to find a way out
4367 */
4368 exec->determinist = 0;
4369 xmlFARegExecRollBack(exec);
4370 }
4371progress:
4372 continue;
4373 }
4374}
4375#endif
4376/************************************************************************
4377 * *
William M. Brackddf71d62004-05-06 04:17:26 +00004378 * Parser for the Schemas Datatype Regular Expressions *
Daniel Veillard4255d502002-04-16 15:50:10 +00004379 * http://www.w3.org/TR/2001/REC-xmlschema-2-20010502/#regexs *
4380 * *
4381 ************************************************************************/
4382
4383/**
4384 * xmlFAIsChar:
Daniel Veillard441bc322002-04-20 17:38:48 +00004385 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00004386 *
4387 * [10] Char ::= [^.\?*+()|#x5B#x5D]
4388 */
4389static int
4390xmlFAIsChar(xmlRegParserCtxtPtr ctxt) {
4391 int cur;
4392 int len;
4393
4394 cur = CUR_SCHAR(ctxt->cur, len);
4395 if ((cur == '.') || (cur == '\\') || (cur == '?') ||
4396 (cur == '*') || (cur == '+') || (cur == '(') ||
4397 (cur == ')') || (cur == '|') || (cur == 0x5B) ||
4398 (cur == 0x5D) || (cur == 0))
4399 return(-1);
4400 return(cur);
4401}
4402
4403/**
4404 * xmlFAParseCharProp:
Daniel Veillard441bc322002-04-20 17:38:48 +00004405 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00004406 *
4407 * [27] charProp ::= IsCategory | IsBlock
4408 * [28] IsCategory ::= Letters | Marks | Numbers | Punctuation |
4409 * Separators | Symbols | Others
4410 * [29] Letters ::= 'L' [ultmo]?
4411 * [30] Marks ::= 'M' [nce]?
4412 * [31] Numbers ::= 'N' [dlo]?
4413 * [32] Punctuation ::= 'P' [cdseifo]?
4414 * [33] Separators ::= 'Z' [slp]?
4415 * [34] Symbols ::= 'S' [mcko]?
4416 * [35] Others ::= 'C' [cfon]?
4417 * [36] IsBlock ::= 'Is' [a-zA-Z0-9#x2D]+
4418 */
4419static void
4420xmlFAParseCharProp(xmlRegParserCtxtPtr ctxt) {
4421 int cur;
William M. Brack779af002003-08-01 15:55:39 +00004422 xmlRegAtomType type = (xmlRegAtomType) 0;
Daniel Veillard4255d502002-04-16 15:50:10 +00004423 xmlChar *blockName = NULL;
4424
4425 cur = CUR;
4426 if (cur == 'L') {
4427 NEXT;
4428 cur = CUR;
4429 if (cur == 'u') {
4430 NEXT;
4431 type = XML_REGEXP_LETTER_UPPERCASE;
4432 } else if (cur == 'l') {
4433 NEXT;
4434 type = XML_REGEXP_LETTER_LOWERCASE;
4435 } else if (cur == 't') {
4436 NEXT;
4437 type = XML_REGEXP_LETTER_TITLECASE;
4438 } else if (cur == 'm') {
4439 NEXT;
4440 type = XML_REGEXP_LETTER_MODIFIER;
4441 } else if (cur == 'o') {
4442 NEXT;
4443 type = XML_REGEXP_LETTER_OTHERS;
4444 } else {
4445 type = XML_REGEXP_LETTER;
4446 }
4447 } else if (cur == 'M') {
4448 NEXT;
4449 cur = CUR;
4450 if (cur == 'n') {
4451 NEXT;
4452 /* nonspacing */
4453 type = XML_REGEXP_MARK_NONSPACING;
4454 } else if (cur == 'c') {
4455 NEXT;
4456 /* spacing combining */
4457 type = XML_REGEXP_MARK_SPACECOMBINING;
4458 } else if (cur == 'e') {
4459 NEXT;
4460 /* enclosing */
4461 type = XML_REGEXP_MARK_ENCLOSING;
4462 } else {
4463 /* all marks */
4464 type = XML_REGEXP_MARK;
4465 }
4466 } else if (cur == 'N') {
4467 NEXT;
4468 cur = CUR;
4469 if (cur == 'd') {
4470 NEXT;
4471 /* digital */
4472 type = XML_REGEXP_NUMBER_DECIMAL;
4473 } else if (cur == 'l') {
4474 NEXT;
4475 /* letter */
4476 type = XML_REGEXP_NUMBER_LETTER;
4477 } else if (cur == 'o') {
4478 NEXT;
4479 /* other */
4480 type = XML_REGEXP_NUMBER_OTHERS;
4481 } else {
4482 /* all numbers */
4483 type = XML_REGEXP_NUMBER;
4484 }
4485 } else if (cur == 'P') {
4486 NEXT;
4487 cur = CUR;
4488 if (cur == 'c') {
4489 NEXT;
4490 /* connector */
4491 type = XML_REGEXP_PUNCT_CONNECTOR;
4492 } else if (cur == 'd') {
4493 NEXT;
4494 /* dash */
4495 type = XML_REGEXP_PUNCT_DASH;
4496 } else if (cur == 's') {
4497 NEXT;
4498 /* open */
4499 type = XML_REGEXP_PUNCT_OPEN;
4500 } else if (cur == 'e') {
4501 NEXT;
4502 /* close */
4503 type = XML_REGEXP_PUNCT_CLOSE;
4504 } else if (cur == 'i') {
4505 NEXT;
4506 /* initial quote */
4507 type = XML_REGEXP_PUNCT_INITQUOTE;
4508 } else if (cur == 'f') {
4509 NEXT;
4510 /* final quote */
4511 type = XML_REGEXP_PUNCT_FINQUOTE;
4512 } else if (cur == 'o') {
4513 NEXT;
4514 /* other */
4515 type = XML_REGEXP_PUNCT_OTHERS;
4516 } else {
4517 /* all punctuation */
4518 type = XML_REGEXP_PUNCT;
4519 }
4520 } else if (cur == 'Z') {
4521 NEXT;
4522 cur = CUR;
4523 if (cur == 's') {
4524 NEXT;
4525 /* space */
4526 type = XML_REGEXP_SEPAR_SPACE;
4527 } else if (cur == 'l') {
4528 NEXT;
4529 /* line */
4530 type = XML_REGEXP_SEPAR_LINE;
4531 } else if (cur == 'p') {
4532 NEXT;
4533 /* paragraph */
4534 type = XML_REGEXP_SEPAR_PARA;
4535 } else {
4536 /* all separators */
4537 type = XML_REGEXP_SEPAR;
4538 }
4539 } else if (cur == 'S') {
4540 NEXT;
4541 cur = CUR;
4542 if (cur == 'm') {
4543 NEXT;
4544 type = XML_REGEXP_SYMBOL_MATH;
4545 /* math */
4546 } else if (cur == 'c') {
4547 NEXT;
4548 type = XML_REGEXP_SYMBOL_CURRENCY;
4549 /* currency */
4550 } else if (cur == 'k') {
4551 NEXT;
4552 type = XML_REGEXP_SYMBOL_MODIFIER;
4553 /* modifiers */
4554 } else if (cur == 'o') {
4555 NEXT;
4556 type = XML_REGEXP_SYMBOL_OTHERS;
4557 /* other */
4558 } else {
4559 /* all symbols */
4560 type = XML_REGEXP_SYMBOL;
4561 }
4562 } else if (cur == 'C') {
4563 NEXT;
4564 cur = CUR;
4565 if (cur == 'c') {
4566 NEXT;
4567 /* control */
4568 type = XML_REGEXP_OTHER_CONTROL;
4569 } else if (cur == 'f') {
4570 NEXT;
4571 /* format */
4572 type = XML_REGEXP_OTHER_FORMAT;
4573 } else if (cur == 'o') {
4574 NEXT;
4575 /* private use */
4576 type = XML_REGEXP_OTHER_PRIVATE;
4577 } else if (cur == 'n') {
4578 NEXT;
4579 /* not assigned */
4580 type = XML_REGEXP_OTHER_NA;
4581 } else {
4582 /* all others */
4583 type = XML_REGEXP_OTHER;
4584 }
4585 } else if (cur == 'I') {
4586 const xmlChar *start;
4587 NEXT;
4588 cur = CUR;
4589 if (cur != 's') {
4590 ERROR("IsXXXX expected");
4591 return;
4592 }
4593 NEXT;
4594 start = ctxt->cur;
4595 cur = CUR;
4596 if (((cur >= 'a') && (cur <= 'z')) ||
4597 ((cur >= 'A') && (cur <= 'Z')) ||
4598 ((cur >= '0') && (cur <= '9')) ||
4599 (cur == 0x2D)) {
4600 NEXT;
4601 cur = CUR;
4602 while (((cur >= 'a') && (cur <= 'z')) ||
4603 ((cur >= 'A') && (cur <= 'Z')) ||
4604 ((cur >= '0') && (cur <= '9')) ||
4605 (cur == 0x2D)) {
4606 NEXT;
4607 cur = CUR;
4608 }
4609 }
4610 type = XML_REGEXP_BLOCK_NAME;
4611 blockName = xmlStrndup(start, ctxt->cur - start);
4612 } else {
4613 ERROR("Unknown char property");
4614 return;
4615 }
4616 if (ctxt->atom == NULL) {
4617 ctxt->atom = xmlRegNewAtom(ctxt, type);
4618 if (ctxt->atom != NULL)
4619 ctxt->atom->valuep = blockName;
4620 } else if (ctxt->atom->type == XML_REGEXP_RANGES) {
4621 xmlRegAtomAddRange(ctxt, ctxt->atom, ctxt->neg,
4622 type, 0, 0, blockName);
4623 }
4624}
4625
4626/**
4627 * xmlFAParseCharClassEsc:
Daniel Veillard441bc322002-04-20 17:38:48 +00004628 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00004629 *
4630 * [23] charClassEsc ::= ( SingleCharEsc | MultiCharEsc | catEsc | complEsc )
4631 * [24] SingleCharEsc ::= '\' [nrt\|.?*+(){}#x2D#x5B#x5D#x5E]
4632 * [25] catEsc ::= '\p{' charProp '}'
4633 * [26] complEsc ::= '\P{' charProp '}'
4634 * [37] MultiCharEsc ::= '.' | ('\' [sSiIcCdDwW])
4635 */
4636static void
4637xmlFAParseCharClassEsc(xmlRegParserCtxtPtr ctxt) {
4638 int cur;
4639
4640 if (CUR == '.') {
4641 if (ctxt->atom == NULL) {
4642 ctxt->atom = xmlRegNewAtom(ctxt, XML_REGEXP_ANYCHAR);
4643 } else if (ctxt->atom->type == XML_REGEXP_RANGES) {
4644 xmlRegAtomAddRange(ctxt, ctxt->atom, ctxt->neg,
4645 XML_REGEXP_ANYCHAR, 0, 0, NULL);
4646 }
4647 NEXT;
4648 return;
4649 }
4650 if (CUR != '\\') {
4651 ERROR("Escaped sequence: expecting \\");
4652 return;
4653 }
4654 NEXT;
4655 cur = CUR;
4656 if (cur == 'p') {
4657 NEXT;
4658 if (CUR != '{') {
4659 ERROR("Expecting '{'");
4660 return;
4661 }
4662 NEXT;
4663 xmlFAParseCharProp(ctxt);
4664 if (CUR != '}') {
4665 ERROR("Expecting '}'");
4666 return;
4667 }
4668 NEXT;
4669 } else if (cur == 'P') {
4670 NEXT;
4671 if (CUR != '{') {
4672 ERROR("Expecting '{'");
4673 return;
4674 }
4675 NEXT;
4676 xmlFAParseCharProp(ctxt);
4677 ctxt->atom->neg = 1;
4678 if (CUR != '}') {
4679 ERROR("Expecting '}'");
4680 return;
4681 }
4682 NEXT;
4683 } else if ((cur == 'n') || (cur == 'r') || (cur == 't') || (cur == '\\') ||
4684 (cur == '|') || (cur == '.') || (cur == '?') || (cur == '*') ||
4685 (cur == '+') || (cur == '(') || (cur == ')') || (cur == '{') ||
4686 (cur == '}') || (cur == 0x2D) || (cur == 0x5B) || (cur == 0x5D) ||
4687 (cur == 0x5E)) {
4688 if (ctxt->atom == NULL) {
4689 ctxt->atom = xmlRegNewAtom(ctxt, XML_REGEXP_CHARVAL);
Daniel Veillard99c394d2005-07-14 12:58:49 +00004690 if (ctxt->atom != NULL) {
4691 switch (cur) {
4692 case 'n':
4693 ctxt->atom->codepoint = '\n';
4694 break;
4695 case 'r':
4696 ctxt->atom->codepoint = '\r';
4697 break;
4698 case 't':
4699 ctxt->atom->codepoint = '\t';
4700 break;
4701 default:
4702 ctxt->atom->codepoint = cur;
4703 }
4704 }
Daniel Veillard4255d502002-04-16 15:50:10 +00004705 } else if (ctxt->atom->type == XML_REGEXP_RANGES) {
4706 xmlRegAtomAddRange(ctxt, ctxt->atom, ctxt->neg,
4707 XML_REGEXP_CHARVAL, cur, cur, NULL);
4708 }
4709 NEXT;
4710 } else if ((cur == 's') || (cur == 'S') || (cur == 'i') || (cur == 'I') ||
4711 (cur == 'c') || (cur == 'C') || (cur == 'd') || (cur == 'D') ||
4712 (cur == 'w') || (cur == 'W')) {
Daniel Veillardb509f152002-04-17 16:28:10 +00004713 xmlRegAtomType type = XML_REGEXP_ANYSPACE;
Daniel Veillard4255d502002-04-16 15:50:10 +00004714
4715 switch (cur) {
4716 case 's':
4717 type = XML_REGEXP_ANYSPACE;
4718 break;
4719 case 'S':
4720 type = XML_REGEXP_NOTSPACE;
4721 break;
4722 case 'i':
4723 type = XML_REGEXP_INITNAME;
4724 break;
4725 case 'I':
4726 type = XML_REGEXP_NOTINITNAME;
4727 break;
4728 case 'c':
4729 type = XML_REGEXP_NAMECHAR;
4730 break;
4731 case 'C':
4732 type = XML_REGEXP_NOTNAMECHAR;
4733 break;
4734 case 'd':
4735 type = XML_REGEXP_DECIMAL;
4736 break;
4737 case 'D':
4738 type = XML_REGEXP_NOTDECIMAL;
4739 break;
4740 case 'w':
4741 type = XML_REGEXP_REALCHAR;
4742 break;
4743 case 'W':
4744 type = XML_REGEXP_NOTREALCHAR;
4745 break;
4746 }
4747 NEXT;
4748 if (ctxt->atom == NULL) {
4749 ctxt->atom = xmlRegNewAtom(ctxt, type);
4750 } else if (ctxt->atom->type == XML_REGEXP_RANGES) {
4751 xmlRegAtomAddRange(ctxt, ctxt->atom, ctxt->neg,
4752 type, 0, 0, NULL);
4753 }
4754 }
4755}
4756
4757/**
4758 * xmlFAParseCharRef:
Daniel Veillard441bc322002-04-20 17:38:48 +00004759 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00004760 *
4761 * [19] XmlCharRef ::= ( '&#' [0-9]+ ';' ) | (' &#x' [0-9a-fA-F]+ ';' )
4762 */
4763static int
4764xmlFAParseCharRef(xmlRegParserCtxtPtr ctxt) {
4765 int ret = 0, cur;
4766
4767 if ((CUR != '&') || (NXT(1) != '#'))
4768 return(-1);
4769 NEXT;
4770 NEXT;
4771 cur = CUR;
4772 if (cur == 'x') {
4773 NEXT;
4774 cur = CUR;
4775 if (((cur >= '0') && (cur <= '9')) ||
4776 ((cur >= 'a') && (cur <= 'f')) ||
4777 ((cur >= 'A') && (cur <= 'F'))) {
4778 while (((cur >= '0') && (cur <= '9')) ||
Daniel Veillard11ce4002006-03-10 00:36:23 +00004779 ((cur >= 'a') && (cur <= 'f')) ||
Daniel Veillard4255d502002-04-16 15:50:10 +00004780 ((cur >= 'A') && (cur <= 'F'))) {
4781 if ((cur >= '0') && (cur <= '9'))
4782 ret = ret * 16 + cur - '0';
4783 else if ((cur >= 'a') && (cur <= 'f'))
4784 ret = ret * 16 + 10 + (cur - 'a');
4785 else
4786 ret = ret * 16 + 10 + (cur - 'A');
4787 NEXT;
4788 cur = CUR;
4789 }
4790 } else {
4791 ERROR("Char ref: expecting [0-9A-F]");
4792 return(-1);
4793 }
4794 } else {
4795 if ((cur >= '0') && (cur <= '9')) {
4796 while ((cur >= '0') && (cur <= '9')) {
4797 ret = ret * 10 + cur - '0';
4798 NEXT;
4799 cur = CUR;
4800 }
4801 } else {
4802 ERROR("Char ref: expecting [0-9]");
4803 return(-1);
4804 }
4805 }
4806 if (cur != ';') {
4807 ERROR("Char ref: expecting ';'");
4808 return(-1);
4809 } else {
4810 NEXT;
4811 }
4812 return(ret);
4813}
4814
4815/**
4816 * xmlFAParseCharRange:
Daniel Veillard441bc322002-04-20 17:38:48 +00004817 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00004818 *
4819 * [17] charRange ::= seRange | XmlCharRef | XmlCharIncDash
4820 * [18] seRange ::= charOrEsc '-' charOrEsc
4821 * [20] charOrEsc ::= XmlChar | SingleCharEsc
4822 * [21] XmlChar ::= [^\#x2D#x5B#x5D]
4823 * [22] XmlCharIncDash ::= [^\#x5B#x5D]
4824 */
4825static void
4826xmlFAParseCharRange(xmlRegParserCtxtPtr ctxt) {
William M. Brackdc99df92003-12-27 01:54:25 +00004827 int cur, len;
Daniel Veillard4255d502002-04-16 15:50:10 +00004828 int start = -1;
4829 int end = -1;
4830
Daniel Veillard777737e2006-10-17 21:23:17 +00004831 if (CUR == '\0') {
4832 ERROR("Expecting ']'");
4833 return;
4834 }
4835
Daniel Veillard4255d502002-04-16 15:50:10 +00004836 if ((CUR == '&') && (NXT(1) == '#')) {
4837 end = start = xmlFAParseCharRef(ctxt);
4838 xmlRegAtomAddRange(ctxt, ctxt->atom, ctxt->neg,
4839 XML_REGEXP_CHARVAL, start, end, NULL);
4840 return;
4841 }
4842 cur = CUR;
4843 if (cur == '\\') {
4844 NEXT;
4845 cur = CUR;
4846 switch (cur) {
4847 case 'n': start = 0xA; break;
4848 case 'r': start = 0xD; break;
4849 case 't': start = 0x9; break;
4850 case '\\': case '|': case '.': case '-': case '^': case '?':
4851 case '*': case '+': case '{': case '}': case '(': case ')':
4852 case '[': case ']':
4853 start = cur; break;
4854 default:
4855 ERROR("Invalid escape value");
4856 return;
4857 }
4858 end = start;
William M. Brackdc99df92003-12-27 01:54:25 +00004859 len = 1;
Daniel Veillard4255d502002-04-16 15:50:10 +00004860 } else if ((cur != 0x5B) && (cur != 0x5D)) {
William M. Brackdc99df92003-12-27 01:54:25 +00004861 end = start = CUR_SCHAR(ctxt->cur, len);
Daniel Veillard4255d502002-04-16 15:50:10 +00004862 } else {
4863 ERROR("Expecting a char range");
4864 return;
4865 }
William M. Bracka9cbf282007-03-21 13:16:33 +00004866 /*
4867 * Since we are "inside" a range, we can assume ctxt->cur is past
4868 * the start of ctxt->string, and PREV should be safe
4869 */
4870 if ((start == '-') && (NXT(1) != ']') && (PREV != '[') && (PREV != '^')) {
4871 NEXTL(len);
Daniel Veillard4255d502002-04-16 15:50:10 +00004872 return;
4873 }
William M. Bracka9cbf282007-03-21 13:16:33 +00004874 NEXTL(len);
Daniel Veillard4255d502002-04-16 15:50:10 +00004875 cur = CUR;
William M. Brack10f1ef42004-03-20 14:51:25 +00004876 if ((cur != '-') || (NXT(1) == ']')) {
Daniel Veillard4255d502002-04-16 15:50:10 +00004877 xmlRegAtomAddRange(ctxt, ctxt->atom, ctxt->neg,
4878 XML_REGEXP_CHARVAL, start, end, NULL);
4879 return;
4880 }
4881 NEXT;
4882 cur = CUR;
4883 if (cur == '\\') {
4884 NEXT;
4885 cur = CUR;
4886 switch (cur) {
4887 case 'n': end = 0xA; break;
4888 case 'r': end = 0xD; break;
4889 case 't': end = 0x9; break;
4890 case '\\': case '|': case '.': case '-': case '^': case '?':
4891 case '*': case '+': case '{': case '}': case '(': case ')':
4892 case '[': case ']':
4893 end = cur; break;
4894 default:
4895 ERROR("Invalid escape value");
4896 return;
4897 }
William M. Brackdc99df92003-12-27 01:54:25 +00004898 len = 1;
Daniel Veillard4255d502002-04-16 15:50:10 +00004899 } else if ((cur != 0x5B) && (cur != 0x5D)) {
William M. Brackdc99df92003-12-27 01:54:25 +00004900 end = CUR_SCHAR(ctxt->cur, len);
Daniel Veillard4255d502002-04-16 15:50:10 +00004901 } else {
4902 ERROR("Expecting the end of a char range");
4903 return;
4904 }
William M. Brackdc99df92003-12-27 01:54:25 +00004905 NEXTL(len);
Daniel Veillard4255d502002-04-16 15:50:10 +00004906 /* TODO check that the values are acceptable character ranges for XML */
4907 if (end < start) {
4908 ERROR("End of range is before start of range");
4909 } else {
4910 xmlRegAtomAddRange(ctxt, ctxt->atom, ctxt->neg,
4911 XML_REGEXP_CHARVAL, start, end, NULL);
4912 }
4913 return;
4914}
4915
4916/**
4917 * xmlFAParsePosCharGroup:
Daniel Veillard441bc322002-04-20 17:38:48 +00004918 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00004919 *
4920 * [14] posCharGroup ::= ( charRange | charClassEsc )+
4921 */
4922static void
4923xmlFAParsePosCharGroup(xmlRegParserCtxtPtr ctxt) {
4924 do {
4925 if ((CUR == '\\') || (CUR == '.')) {
4926 xmlFAParseCharClassEsc(ctxt);
4927 } else {
4928 xmlFAParseCharRange(ctxt);
4929 }
4930 } while ((CUR != ']') && (CUR != '^') && (CUR != '-') &&
Daniel Veillard777737e2006-10-17 21:23:17 +00004931 (CUR != 0) && (ctxt->error == 0));
Daniel Veillard4255d502002-04-16 15:50:10 +00004932}
4933
4934/**
4935 * xmlFAParseCharGroup:
Daniel Veillard441bc322002-04-20 17:38:48 +00004936 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00004937 *
4938 * [13] charGroup ::= posCharGroup | negCharGroup | charClassSub
4939 * [15] negCharGroup ::= '^' posCharGroup
4940 * [16] charClassSub ::= ( posCharGroup | negCharGroup ) '-' charClassExpr
4941 * [12] charClassExpr ::= '[' charGroup ']'
4942 */
4943static void
4944xmlFAParseCharGroup(xmlRegParserCtxtPtr ctxt) {
4945 int n = ctxt->neg;
4946 while ((CUR != ']') && (ctxt->error == 0)) {
4947 if (CUR == '^') {
4948 int neg = ctxt->neg;
4949
4950 NEXT;
4951 ctxt->neg = !ctxt->neg;
4952 xmlFAParsePosCharGroup(ctxt);
4953 ctxt->neg = neg;
William M. Brack10f1ef42004-03-20 14:51:25 +00004954 } else if ((CUR == '-') && (NXT(1) == '[')) {
Daniel Veillardf8b9de32003-11-24 14:27:26 +00004955 int neg = ctxt->neg;
Daniel Veillardf8b9de32003-11-24 14:27:26 +00004956 ctxt->neg = 2;
William M. Brack10f1ef42004-03-20 14:51:25 +00004957 NEXT; /* eat the '-' */
4958 NEXT; /* eat the '[' */
Daniel Veillard4255d502002-04-16 15:50:10 +00004959 xmlFAParseCharGroup(ctxt);
4960 if (CUR == ']') {
4961 NEXT;
4962 } else {
4963 ERROR("charClassExpr: ']' expected");
4964 break;
4965 }
Daniel Veillardf8b9de32003-11-24 14:27:26 +00004966 ctxt->neg = neg;
Daniel Veillard4255d502002-04-16 15:50:10 +00004967 break;
4968 } else if (CUR != ']') {
4969 xmlFAParsePosCharGroup(ctxt);
4970 }
4971 }
4972 ctxt->neg = n;
4973}
4974
4975/**
4976 * xmlFAParseCharClass:
Daniel Veillard441bc322002-04-20 17:38:48 +00004977 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00004978 *
4979 * [11] charClass ::= charClassEsc | charClassExpr
4980 * [12] charClassExpr ::= '[' charGroup ']'
4981 */
4982static void
4983xmlFAParseCharClass(xmlRegParserCtxtPtr ctxt) {
4984 if (CUR == '[') {
4985 NEXT;
4986 ctxt->atom = xmlRegNewAtom(ctxt, XML_REGEXP_RANGES);
4987 if (ctxt->atom == NULL)
4988 return;
4989 xmlFAParseCharGroup(ctxt);
4990 if (CUR == ']') {
4991 NEXT;
4992 } else {
4993 ERROR("xmlFAParseCharClass: ']' expected");
4994 }
4995 } else {
4996 xmlFAParseCharClassEsc(ctxt);
4997 }
4998}
4999
5000/**
5001 * xmlFAParseQuantExact:
Daniel Veillard441bc322002-04-20 17:38:48 +00005002 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00005003 *
5004 * [8] QuantExact ::= [0-9]+
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00005005 *
5006 * Returns 0 if success or -1 in case of error
Daniel Veillard4255d502002-04-16 15:50:10 +00005007 */
5008static int
5009xmlFAParseQuantExact(xmlRegParserCtxtPtr ctxt) {
5010 int ret = 0;
5011 int ok = 0;
5012
5013 while ((CUR >= '0') && (CUR <= '9')) {
5014 ret = ret * 10 + (CUR - '0');
5015 ok = 1;
5016 NEXT;
5017 }
5018 if (ok != 1) {
5019 return(-1);
5020 }
5021 return(ret);
5022}
5023
5024/**
5025 * xmlFAParseQuantifier:
Daniel Veillard441bc322002-04-20 17:38:48 +00005026 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00005027 *
5028 * [4] quantifier ::= [?*+] | ( '{' quantity '}' )
5029 * [5] quantity ::= quantRange | quantMin | QuantExact
5030 * [6] quantRange ::= QuantExact ',' QuantExact
5031 * [7] quantMin ::= QuantExact ','
5032 * [8] QuantExact ::= [0-9]+
5033 */
5034static int
5035xmlFAParseQuantifier(xmlRegParserCtxtPtr ctxt) {
5036 int cur;
5037
5038 cur = CUR;
5039 if ((cur == '?') || (cur == '*') || (cur == '+')) {
5040 if (ctxt->atom != NULL) {
5041 if (cur == '?')
5042 ctxt->atom->quant = XML_REGEXP_QUANT_OPT;
5043 else if (cur == '*')
5044 ctxt->atom->quant = XML_REGEXP_QUANT_MULT;
5045 else if (cur == '+')
5046 ctxt->atom->quant = XML_REGEXP_QUANT_PLUS;
5047 }
5048 NEXT;
5049 return(1);
5050 }
5051 if (cur == '{') {
5052 int min = 0, max = 0;
5053
5054 NEXT;
5055 cur = xmlFAParseQuantExact(ctxt);
5056 if (cur >= 0)
5057 min = cur;
5058 if (CUR == ',') {
5059 NEXT;
Daniel Veillardebe48c62003-12-03 12:12:27 +00005060 if (CUR == '}')
5061 max = INT_MAX;
5062 else {
5063 cur = xmlFAParseQuantExact(ctxt);
5064 if (cur >= 0)
5065 max = cur;
5066 else {
5067 ERROR("Improper quantifier");
5068 }
5069 }
Daniel Veillard4255d502002-04-16 15:50:10 +00005070 }
5071 if (CUR == '}') {
5072 NEXT;
5073 } else {
5074 ERROR("Unterminated quantifier");
5075 }
5076 if (max == 0)
5077 max = min;
5078 if (ctxt->atom != NULL) {
5079 ctxt->atom->quant = XML_REGEXP_QUANT_RANGE;
5080 ctxt->atom->min = min;
5081 ctxt->atom->max = max;
5082 }
5083 return(1);
5084 }
5085 return(0);
5086}
5087
5088/**
5089 * xmlFAParseAtom:
Daniel Veillard441bc322002-04-20 17:38:48 +00005090 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00005091 *
5092 * [9] atom ::= Char | charClass | ( '(' regExp ')' )
5093 */
5094static int
5095xmlFAParseAtom(xmlRegParserCtxtPtr ctxt) {
5096 int codepoint, len;
5097
5098 codepoint = xmlFAIsChar(ctxt);
5099 if (codepoint > 0) {
5100 ctxt->atom = xmlRegNewAtom(ctxt, XML_REGEXP_CHARVAL);
5101 if (ctxt->atom == NULL)
5102 return(-1);
5103 codepoint = CUR_SCHAR(ctxt->cur, len);
5104 ctxt->atom->codepoint = codepoint;
5105 NEXTL(len);
5106 return(1);
5107 } else if (CUR == '|') {
5108 return(0);
5109 } else if (CUR == 0) {
5110 return(0);
5111 } else if (CUR == ')') {
5112 return(0);
5113 } else if (CUR == '(') {
5114 xmlRegStatePtr start, oldend;
5115
5116 NEXT;
5117 xmlFAGenerateEpsilonTransition(ctxt, ctxt->state, NULL);
5118 start = ctxt->state;
5119 oldend = ctxt->end;
5120 ctxt->end = NULL;
5121 ctxt->atom = NULL;
5122 xmlFAParseRegExp(ctxt, 0);
5123 if (CUR == ')') {
5124 NEXT;
5125 } else {
5126 ERROR("xmlFAParseAtom: expecting ')'");
5127 }
5128 ctxt->atom = xmlRegNewAtom(ctxt, XML_REGEXP_SUBREG);
5129 if (ctxt->atom == NULL)
5130 return(-1);
5131 ctxt->atom->start = start;
5132 ctxt->atom->stop = ctxt->state;
5133 ctxt->end = oldend;
5134 return(1);
5135 } else if ((CUR == '[') || (CUR == '\\') || (CUR == '.')) {
5136 xmlFAParseCharClass(ctxt);
5137 return(1);
5138 }
5139 return(0);
5140}
5141
5142/**
5143 * xmlFAParsePiece:
Daniel Veillard441bc322002-04-20 17:38:48 +00005144 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00005145 *
5146 * [3] piece ::= atom quantifier?
5147 */
5148static int
5149xmlFAParsePiece(xmlRegParserCtxtPtr ctxt) {
5150 int ret;
5151
5152 ctxt->atom = NULL;
5153 ret = xmlFAParseAtom(ctxt);
5154 if (ret == 0)
5155 return(0);
5156 if (ctxt->atom == NULL) {
5157 ERROR("internal: no atom generated");
5158 }
5159 xmlFAParseQuantifier(ctxt);
5160 return(1);
5161}
5162
5163/**
5164 * xmlFAParseBranch:
Daniel Veillard441bc322002-04-20 17:38:48 +00005165 * @ctxt: a regexp parser context
Daniel Veillard54eb0242006-03-21 23:17:57 +00005166 * @to: optional target to the end of the branch
5167 *
5168 * @to is used to optimize by removing duplicate path in automata
5169 * in expressions like (a|b)(c|d)
Daniel Veillard4255d502002-04-16 15:50:10 +00005170 *
5171 * [2] branch ::= piece*
5172 */
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00005173static int
Daniel Veillard54eb0242006-03-21 23:17:57 +00005174xmlFAParseBranch(xmlRegParserCtxtPtr ctxt, xmlRegStatePtr to) {
Daniel Veillard4255d502002-04-16 15:50:10 +00005175 xmlRegStatePtr previous;
Daniel Veillard4255d502002-04-16 15:50:10 +00005176 int ret;
5177
5178 previous = ctxt->state;
5179 ret = xmlFAParsePiece(ctxt);
5180 if (ret != 0) {
Daniel Veillard54eb0242006-03-21 23:17:57 +00005181 if (xmlFAGenerateTransitions(ctxt, previous,
5182 (CUR=='|' || CUR==')') ? to : NULL, ctxt->atom) < 0)
Daniel Veillard2cbf5962004-03-31 15:50:43 +00005183 return(-1);
5184 previous = ctxt->state;
Daniel Veillard4255d502002-04-16 15:50:10 +00005185 ctxt->atom = NULL;
5186 }
5187 while ((ret != 0) && (ctxt->error == 0)) {
5188 ret = xmlFAParsePiece(ctxt);
5189 if (ret != 0) {
Daniel Veillard54eb0242006-03-21 23:17:57 +00005190 if (xmlFAGenerateTransitions(ctxt, previous,
5191 (CUR=='|' || CUR==')') ? to : NULL, ctxt->atom) < 0)
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00005192 return(-1);
Daniel Veillard4255d502002-04-16 15:50:10 +00005193 previous = ctxt->state;
5194 ctxt->atom = NULL;
5195 }
5196 }
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00005197 return(0);
Daniel Veillard4255d502002-04-16 15:50:10 +00005198}
5199
5200/**
5201 * xmlFAParseRegExp:
Daniel Veillard441bc322002-04-20 17:38:48 +00005202 * @ctxt: a regexp parser context
William M. Brackddf71d62004-05-06 04:17:26 +00005203 * @top: is this the top-level expression ?
Daniel Veillard4255d502002-04-16 15:50:10 +00005204 *
5205 * [1] regExp ::= branch ( '|' branch )*
5206 */
5207static void
5208xmlFAParseRegExp(xmlRegParserCtxtPtr ctxt, int top) {
Daniel Veillardc7e3cc42004-09-28 12:33:52 +00005209 xmlRegStatePtr start, end;
Daniel Veillard4255d502002-04-16 15:50:10 +00005210
Daniel Veillard2cbf5962004-03-31 15:50:43 +00005211 /* if not top start should have been generated by an epsilon trans */
Daniel Veillard4255d502002-04-16 15:50:10 +00005212 start = ctxt->state;
Daniel Veillard2cbf5962004-03-31 15:50:43 +00005213 ctxt->end = NULL;
Daniel Veillard54eb0242006-03-21 23:17:57 +00005214 xmlFAParseBranch(ctxt, NULL);
Daniel Veillard2cbf5962004-03-31 15:50:43 +00005215 if (top) {
5216#ifdef DEBUG_REGEXP_GRAPH
5217 printf("State %d is final\n", ctxt->state->no);
5218#endif
5219 ctxt->state->type = XML_REGEXP_FINAL_STATE;
5220 }
Daniel Veillard4255d502002-04-16 15:50:10 +00005221 if (CUR != '|') {
5222 ctxt->end = ctxt->state;
5223 return;
5224 }
5225 end = ctxt->state;
5226 while ((CUR == '|') && (ctxt->error == 0)) {
5227 NEXT;
5228 ctxt->state = start;
Daniel Veillard2cbf5962004-03-31 15:50:43 +00005229 ctxt->end = NULL;
Daniel Veillard54eb0242006-03-21 23:17:57 +00005230 xmlFAParseBranch(ctxt, end);
Daniel Veillard4255d502002-04-16 15:50:10 +00005231 }
Daniel Veillard2cbf5962004-03-31 15:50:43 +00005232 if (!top) {
5233 ctxt->state = end;
5234 ctxt->end = end;
5235 }
Daniel Veillard4255d502002-04-16 15:50:10 +00005236}
5237
5238/************************************************************************
5239 * *
5240 * The basic API *
5241 * *
5242 ************************************************************************/
5243
5244/**
5245 * xmlRegexpPrint:
5246 * @output: the file for the output debug
5247 * @regexp: the compiled regexp
5248 *
5249 * Print the content of the compiled regular expression
5250 */
5251void
5252xmlRegexpPrint(FILE *output, xmlRegexpPtr regexp) {
5253 int i;
5254
Daniel Veillarda82b1822004-11-08 16:24:57 +00005255 if (output == NULL)
5256 return;
Daniel Veillard4255d502002-04-16 15:50:10 +00005257 fprintf(output, " regexp: ");
5258 if (regexp == NULL) {
5259 fprintf(output, "NULL\n");
5260 return;
5261 }
5262 fprintf(output, "'%s' ", regexp->string);
5263 fprintf(output, "\n");
5264 fprintf(output, "%d atoms:\n", regexp->nbAtoms);
5265 for (i = 0;i < regexp->nbAtoms; i++) {
5266 fprintf(output, " %02d ", i);
5267 xmlRegPrintAtom(output, regexp->atoms[i]);
5268 }
5269 fprintf(output, "%d states:", regexp->nbStates);
5270 fprintf(output, "\n");
5271 for (i = 0;i < regexp->nbStates; i++) {
5272 xmlRegPrintState(output, regexp->states[i]);
5273 }
5274 fprintf(output, "%d counters:\n", regexp->nbCounters);
5275 for (i = 0;i < regexp->nbCounters; i++) {
5276 fprintf(output, " %d: min %d max %d\n", i, regexp->counters[i].min,
5277 regexp->counters[i].max);
5278 }
5279}
5280
5281/**
5282 * xmlRegexpCompile:
5283 * @regexp: a regular expression string
5284 *
5285 * Parses a regular expression conforming to XML Schemas Part 2 Datatype
William M. Brackddf71d62004-05-06 04:17:26 +00005286 * Appendix F and builds an automata suitable for testing strings against
Daniel Veillard4255d502002-04-16 15:50:10 +00005287 * that regular expression
5288 *
5289 * Returns the compiled expression or NULL in case of error
5290 */
5291xmlRegexpPtr
5292xmlRegexpCompile(const xmlChar *regexp) {
5293 xmlRegexpPtr ret;
5294 xmlRegParserCtxtPtr ctxt;
5295
5296 ctxt = xmlRegNewParserCtxt(regexp);
5297 if (ctxt == NULL)
5298 return(NULL);
5299
5300 /* initialize the parser */
5301 ctxt->end = NULL;
5302 ctxt->start = ctxt->state = xmlRegNewState(ctxt);
5303 xmlRegStatePush(ctxt, ctxt->start);
5304
5305 /* parse the expression building an automata */
5306 xmlFAParseRegExp(ctxt, 1);
5307 if (CUR != 0) {
5308 ERROR("xmlFAParseRegExp: extra characters");
5309 }
5310 ctxt->end = ctxt->state;
5311 ctxt->start->type = XML_REGEXP_START_STATE;
5312 ctxt->end->type = XML_REGEXP_FINAL_STATE;
5313
5314 /* remove the Epsilon except for counted transitions */
5315 xmlFAEliminateEpsilonTransitions(ctxt);
5316
5317
5318 if (ctxt->error != 0) {
5319 xmlRegFreeParserCtxt(ctxt);
5320 return(NULL);
5321 }
5322 ret = xmlRegEpxFromParse(ctxt);
5323 xmlRegFreeParserCtxt(ctxt);
5324 return(ret);
5325}
5326
5327/**
5328 * xmlRegexpExec:
5329 * @comp: the compiled regular expression
5330 * @content: the value to check against the regular expression
5331 *
William M. Brackddf71d62004-05-06 04:17:26 +00005332 * Check if the regular expression generates the value
Daniel Veillard4255d502002-04-16 15:50:10 +00005333 *
William M. Brackddf71d62004-05-06 04:17:26 +00005334 * Returns 1 if it matches, 0 if not and a negative value in case of error
Daniel Veillard4255d502002-04-16 15:50:10 +00005335 */
5336int
5337xmlRegexpExec(xmlRegexpPtr comp, const xmlChar *content) {
5338 if ((comp == NULL) || (content == NULL))
5339 return(-1);
5340 return(xmlFARegExec(comp, content));
5341}
5342
5343/**
Daniel Veillard23e73572002-09-19 19:56:43 +00005344 * xmlRegexpIsDeterminist:
5345 * @comp: the compiled regular expression
5346 *
5347 * Check if the regular expression is determinist
5348 *
William M. Brackddf71d62004-05-06 04:17:26 +00005349 * Returns 1 if it yes, 0 if not and a negative value in case of error
Daniel Veillard23e73572002-09-19 19:56:43 +00005350 */
5351int
5352xmlRegexpIsDeterminist(xmlRegexpPtr comp) {
5353 xmlAutomataPtr am;
5354 int ret;
5355
5356 if (comp == NULL)
5357 return(-1);
5358 if (comp->determinist != -1)
5359 return(comp->determinist);
5360
5361 am = xmlNewAutomata();
Daniel Veillardbd9afb52002-09-25 22:25:35 +00005362 if (am->states != NULL) {
5363 int i;
5364
5365 for (i = 0;i < am->nbStates;i++)
5366 xmlRegFreeState(am->states[i]);
5367 xmlFree(am->states);
5368 }
Daniel Veillard23e73572002-09-19 19:56:43 +00005369 am->nbAtoms = comp->nbAtoms;
5370 am->atoms = comp->atoms;
5371 am->nbStates = comp->nbStates;
5372 am->states = comp->states;
5373 am->determinist = -1;
5374 ret = xmlFAComputesDeterminism(am);
5375 am->atoms = NULL;
5376 am->states = NULL;
5377 xmlFreeAutomata(am);
5378 return(ret);
5379}
5380
5381/**
Daniel Veillard4255d502002-04-16 15:50:10 +00005382 * xmlRegFreeRegexp:
5383 * @regexp: the regexp
5384 *
5385 * Free a regexp
5386 */
5387void
5388xmlRegFreeRegexp(xmlRegexpPtr regexp) {
5389 int i;
5390 if (regexp == NULL)
5391 return;
5392
5393 if (regexp->string != NULL)
5394 xmlFree(regexp->string);
5395 if (regexp->states != NULL) {
5396 for (i = 0;i < regexp->nbStates;i++)
5397 xmlRegFreeState(regexp->states[i]);
5398 xmlFree(regexp->states);
5399 }
5400 if (regexp->atoms != NULL) {
5401 for (i = 0;i < regexp->nbAtoms;i++)
5402 xmlRegFreeAtom(regexp->atoms[i]);
5403 xmlFree(regexp->atoms);
5404 }
5405 if (regexp->counters != NULL)
5406 xmlFree(regexp->counters);
Daniel Veillard23e73572002-09-19 19:56:43 +00005407 if (regexp->compact != NULL)
5408 xmlFree(regexp->compact);
Daniel Veillard118aed72002-09-24 14:13:13 +00005409 if (regexp->transdata != NULL)
5410 xmlFree(regexp->transdata);
Daniel Veillard23e73572002-09-19 19:56:43 +00005411 if (regexp->stringMap != NULL) {
5412 for (i = 0; i < regexp->nbstrings;i++)
5413 xmlFree(regexp->stringMap[i]);
5414 xmlFree(regexp->stringMap);
5415 }
5416
Daniel Veillard4255d502002-04-16 15:50:10 +00005417 xmlFree(regexp);
5418}
5419
5420#ifdef LIBXML_AUTOMATA_ENABLED
5421/************************************************************************
5422 * *
5423 * The Automata interface *
5424 * *
5425 ************************************************************************/
5426
5427/**
5428 * xmlNewAutomata:
5429 *
5430 * Create a new automata
5431 *
5432 * Returns the new object or NULL in case of failure
5433 */
5434xmlAutomataPtr
5435xmlNewAutomata(void) {
5436 xmlAutomataPtr ctxt;
5437
5438 ctxt = xmlRegNewParserCtxt(NULL);
5439 if (ctxt == NULL)
5440 return(NULL);
5441
5442 /* initialize the parser */
5443 ctxt->end = NULL;
5444 ctxt->start = ctxt->state = xmlRegNewState(ctxt);
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00005445 if (ctxt->start == NULL) {
5446 xmlFreeAutomata(ctxt);
5447 return(NULL);
5448 }
Daniel Veillardd0271472006-01-02 10:22:02 +00005449 ctxt->start->type = XML_REGEXP_START_STATE;
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00005450 if (xmlRegStatePush(ctxt, ctxt->start) < 0) {
5451 xmlRegFreeState(ctxt->start);
5452 xmlFreeAutomata(ctxt);
5453 return(NULL);
5454 }
Daniel Veillard4255d502002-04-16 15:50:10 +00005455
5456 return(ctxt);
5457}
5458
5459/**
5460 * xmlFreeAutomata:
5461 * @am: an automata
5462 *
5463 * Free an automata
5464 */
5465void
5466xmlFreeAutomata(xmlAutomataPtr am) {
5467 if (am == NULL)
5468 return;
5469 xmlRegFreeParserCtxt(am);
5470}
5471
5472/**
5473 * xmlAutomataGetInitState:
5474 * @am: an automata
5475 *
Daniel Veillarda9b66d02002-12-11 14:23:49 +00005476 * Initial state lookup
5477 *
Daniel Veillard4255d502002-04-16 15:50:10 +00005478 * Returns the initial state of the automata
5479 */
5480xmlAutomataStatePtr
5481xmlAutomataGetInitState(xmlAutomataPtr am) {
5482 if (am == NULL)
5483 return(NULL);
5484 return(am->start);
5485}
5486
5487/**
5488 * xmlAutomataSetFinalState:
5489 * @am: an automata
5490 * @state: a state in this automata
5491 *
5492 * Makes that state a final state
5493 *
5494 * Returns 0 or -1 in case of error
5495 */
5496int
5497xmlAutomataSetFinalState(xmlAutomataPtr am, xmlAutomataStatePtr state) {
5498 if ((am == NULL) || (state == NULL))
5499 return(-1);
5500 state->type = XML_REGEXP_FINAL_STATE;
5501 return(0);
5502}
5503
5504/**
5505 * xmlAutomataNewTransition:
5506 * @am: an automata
5507 * @from: the starting point of the transition
5508 * @to: the target point of the transition or NULL
5509 * @token: the input string associated to that transition
5510 * @data: data passed to the callback function if the transition is activated
5511 *
William M. Brackddf71d62004-05-06 04:17:26 +00005512 * If @to is NULL, this creates first a new target state in the automata
Daniel Veillard4255d502002-04-16 15:50:10 +00005513 * and then adds a transition from the @from state to the target state
5514 * activated by the value of @token
5515 *
5516 * Returns the target state or NULL in case of error
5517 */
5518xmlAutomataStatePtr
5519xmlAutomataNewTransition(xmlAutomataPtr am, xmlAutomataStatePtr from,
5520 xmlAutomataStatePtr to, const xmlChar *token,
5521 void *data) {
5522 xmlRegAtomPtr atom;
5523
5524 if ((am == NULL) || (from == NULL) || (token == NULL))
5525 return(NULL);
5526 atom = xmlRegNewAtom(am, XML_REGEXP_STRING);
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00005527 if (atom == NULL)
5528 return(NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +00005529 atom->data = data;
5530 if (atom == NULL)
5531 return(NULL);
5532 atom->valuep = xmlStrdup(token);
5533
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00005534 if (xmlFAGenerateTransitions(am, from, to, atom) < 0) {
5535 xmlRegFreeAtom(atom);
5536 return(NULL);
5537 }
Daniel Veillard4255d502002-04-16 15:50:10 +00005538 if (to == NULL)
5539 return(am->state);
5540 return(to);
5541}
5542
5543/**
Daniel Veillard52b48c72003-04-13 19:53:42 +00005544 * xmlAutomataNewTransition2:
5545 * @am: an automata
5546 * @from: the starting point of the transition
5547 * @to: the target point of the transition or NULL
5548 * @token: the first input string associated to that transition
5549 * @token2: the second input string associated to that transition
5550 * @data: data passed to the callback function if the transition is activated
5551 *
William M. Brackddf71d62004-05-06 04:17:26 +00005552 * If @to is NULL, this creates first a new target state in the automata
Daniel Veillard52b48c72003-04-13 19:53:42 +00005553 * and then adds a transition from the @from state to the target state
5554 * activated by the value of @token
5555 *
5556 * Returns the target state or NULL in case of error
5557 */
5558xmlAutomataStatePtr
5559xmlAutomataNewTransition2(xmlAutomataPtr am, xmlAutomataStatePtr from,
5560 xmlAutomataStatePtr to, const xmlChar *token,
5561 const xmlChar *token2, void *data) {
5562 xmlRegAtomPtr atom;
5563
5564 if ((am == NULL) || (from == NULL) || (token == NULL))
5565 return(NULL);
5566 atom = xmlRegNewAtom(am, XML_REGEXP_STRING);
Daniel Veillard52b48c72003-04-13 19:53:42 +00005567 if (atom == NULL)
5568 return(NULL);
Daniel Veillard11ce4002006-03-10 00:36:23 +00005569 atom->data = data;
Daniel Veillard52b48c72003-04-13 19:53:42 +00005570 if ((token2 == NULL) || (*token2 == 0)) {
5571 atom->valuep = xmlStrdup(token);
5572 } else {
5573 int lenn, lenp;
5574 xmlChar *str;
5575
5576 lenn = strlen((char *) token2);
5577 lenp = strlen((char *) token);
5578
Daniel Veillard3c908dc2003-04-19 00:07:51 +00005579 str = (xmlChar *) xmlMallocAtomic(lenn + lenp + 2);
Daniel Veillard52b48c72003-04-13 19:53:42 +00005580 if (str == NULL) {
5581 xmlRegFreeAtom(atom);
5582 return(NULL);
5583 }
5584 memcpy(&str[0], token, lenp);
5585 str[lenp] = '|';
5586 memcpy(&str[lenp + 1], token2, lenn);
5587 str[lenn + lenp + 1] = 0;
5588
5589 atom->valuep = str;
5590 }
5591
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00005592 if (xmlFAGenerateTransitions(am, from, to, atom) < 0) {
5593 xmlRegFreeAtom(atom);
5594 return(NULL);
5595 }
Daniel Veillard52b48c72003-04-13 19:53:42 +00005596 if (to == NULL)
5597 return(am->state);
5598 return(to);
5599}
5600
5601/**
Daniel Veillard9efc4762005-07-19 14:33:55 +00005602 * xmlAutomataNewNegTrans:
5603 * @am: an automata
5604 * @from: the starting point of the transition
5605 * @to: the target point of the transition or NULL
5606 * @token: the first input string associated to that transition
5607 * @token2: the second input string associated to that transition
5608 * @data: data passed to the callback function if the transition is activated
5609 *
5610 * If @to is NULL, this creates first a new target state in the automata
5611 * and then adds a transition from the @from state to the target state
5612 * activated by any value except (@token,@token2)
Daniel Veillard6e65e152005-08-09 11:09:52 +00005613 * Note that if @token2 is not NULL, then (X, NULL) won't match to follow
5614 # the semantic of XSD ##other
Daniel Veillard9efc4762005-07-19 14:33:55 +00005615 *
5616 * Returns the target state or NULL in case of error
5617 */
5618xmlAutomataStatePtr
5619xmlAutomataNewNegTrans(xmlAutomataPtr am, xmlAutomataStatePtr from,
5620 xmlAutomataStatePtr to, const xmlChar *token,
5621 const xmlChar *token2, void *data) {
5622 xmlRegAtomPtr atom;
Daniel Veillard77005e62005-07-19 16:26:18 +00005623 xmlChar err_msg[200];
Daniel Veillard9efc4762005-07-19 14:33:55 +00005624
5625 if ((am == NULL) || (from == NULL) || (token == NULL))
5626 return(NULL);
5627 atom = xmlRegNewAtom(am, XML_REGEXP_STRING);
5628 if (atom == NULL)
5629 return(NULL);
5630 atom->data = data;
5631 atom->neg = 1;
5632 if ((token2 == NULL) || (*token2 == 0)) {
5633 atom->valuep = xmlStrdup(token);
5634 } else {
5635 int lenn, lenp;
5636 xmlChar *str;
5637
5638 lenn = strlen((char *) token2);
5639 lenp = strlen((char *) token);
5640
5641 str = (xmlChar *) xmlMallocAtomic(lenn + lenp + 2);
5642 if (str == NULL) {
5643 xmlRegFreeAtom(atom);
5644 return(NULL);
5645 }
5646 memcpy(&str[0], token, lenp);
5647 str[lenp] = '|';
5648 memcpy(&str[lenp + 1], token2, lenn);
5649 str[lenn + lenp + 1] = 0;
5650
5651 atom->valuep = str;
5652 }
Daniel Veillarddb68b742005-07-30 13:18:24 +00005653 snprintf((char *) err_msg, 199, "not %s", (const char *) atom->valuep);
Daniel Veillard77005e62005-07-19 16:26:18 +00005654 err_msg[199] = 0;
5655 atom->valuep2 = xmlStrdup(err_msg);
Daniel Veillard9efc4762005-07-19 14:33:55 +00005656
5657 if (xmlFAGenerateTransitions(am, from, to, atom) < 0) {
5658 xmlRegFreeAtom(atom);
5659 return(NULL);
5660 }
Daniel Veillard6e65e152005-08-09 11:09:52 +00005661 am->negs++;
Daniel Veillard9efc4762005-07-19 14:33:55 +00005662 if (to == NULL)
5663 return(am->state);
5664 return(to);
5665}
5666
5667/**
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00005668 * xmlAutomataNewCountTrans2:
5669 * @am: an automata
5670 * @from: the starting point of the transition
5671 * @to: the target point of the transition or NULL
5672 * @token: the input string associated to that transition
5673 * @token2: the second input string associated to that transition
5674 * @min: the minimum successive occurences of token
5675 * @max: the maximum successive occurences of token
5676 * @data: data associated to the transition
5677 *
5678 * If @to is NULL, this creates first a new target state in the automata
5679 * and then adds a transition from the @from state to the target state
5680 * activated by a succession of input of value @token and @token2 and
5681 * whose number is between @min and @max
5682 *
5683 * Returns the target state or NULL in case of error
5684 */
5685xmlAutomataStatePtr
5686xmlAutomataNewCountTrans2(xmlAutomataPtr am, xmlAutomataStatePtr from,
5687 xmlAutomataStatePtr to, const xmlChar *token,
5688 const xmlChar *token2,
5689 int min, int max, void *data) {
5690 xmlRegAtomPtr atom;
5691 int counter;
5692
5693 if ((am == NULL) || (from == NULL) || (token == NULL))
5694 return(NULL);
5695 if (min < 0)
5696 return(NULL);
5697 if ((max < min) || (max < 1))
5698 return(NULL);
5699 atom = xmlRegNewAtom(am, XML_REGEXP_STRING);
5700 if (atom == NULL)
5701 return(NULL);
5702 if ((token2 == NULL) || (*token2 == 0)) {
5703 atom->valuep = xmlStrdup(token);
5704 } else {
5705 int lenn, lenp;
5706 xmlChar *str;
5707
5708 lenn = strlen((char *) token2);
5709 lenp = strlen((char *) token);
5710
5711 str = (xmlChar *) xmlMallocAtomic(lenn + lenp + 2);
5712 if (str == NULL) {
5713 xmlRegFreeAtom(atom);
5714 return(NULL);
5715 }
5716 memcpy(&str[0], token, lenp);
5717 str[lenp] = '|';
5718 memcpy(&str[lenp + 1], token2, lenn);
5719 str[lenn + lenp + 1] = 0;
5720
5721 atom->valuep = str;
5722 }
5723 atom->data = data;
5724 if (min == 0)
5725 atom->min = 1;
5726 else
5727 atom->min = min;
5728 atom->max = max;
5729
5730 /*
5731 * associate a counter to the transition.
5732 */
5733 counter = xmlRegGetCounter(am);
5734 am->counters[counter].min = min;
5735 am->counters[counter].max = max;
5736
5737 /* xmlFAGenerateTransitions(am, from, to, atom); */
5738 if (to == NULL) {
5739 to = xmlRegNewState(am);
5740 xmlRegStatePush(am, to);
5741 }
Daniel Veillard5de09382005-09-26 17:18:17 +00005742 xmlRegStateAddTrans(am, from, atom, to, counter, -1);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00005743 xmlRegAtomPush(am, atom);
5744 am->state = to;
5745
5746 if (to == NULL)
5747 to = am->state;
5748 if (to == NULL)
5749 return(NULL);
5750 if (min == 0)
5751 xmlFAGenerateEpsilonTransition(am, from, to);
5752 return(to);
5753}
5754
5755/**
Daniel Veillard4255d502002-04-16 15:50:10 +00005756 * xmlAutomataNewCountTrans:
5757 * @am: an automata
5758 * @from: the starting point of the transition
5759 * @to: the target point of the transition or NULL
5760 * @token: the input string associated to that transition
5761 * @min: the minimum successive occurences of token
Daniel Veillarda9b66d02002-12-11 14:23:49 +00005762 * @max: the maximum successive occurences of token
5763 * @data: data associated to the transition
Daniel Veillard4255d502002-04-16 15:50:10 +00005764 *
William M. Brackddf71d62004-05-06 04:17:26 +00005765 * If @to is NULL, this creates first a new target state in the automata
Daniel Veillard4255d502002-04-16 15:50:10 +00005766 * and then adds a transition from the @from state to the target state
5767 * activated by a succession of input of value @token and whose number
5768 * is between @min and @max
5769 *
5770 * Returns the target state or NULL in case of error
5771 */
5772xmlAutomataStatePtr
5773xmlAutomataNewCountTrans(xmlAutomataPtr am, xmlAutomataStatePtr from,
5774 xmlAutomataStatePtr to, const xmlChar *token,
5775 int min, int max, void *data) {
5776 xmlRegAtomPtr atom;
Daniel Veillard0ddb21c2004-02-12 12:43:49 +00005777 int counter;
Daniel Veillard4255d502002-04-16 15:50:10 +00005778
5779 if ((am == NULL) || (from == NULL) || (token == NULL))
5780 return(NULL);
5781 if (min < 0)
5782 return(NULL);
5783 if ((max < min) || (max < 1))
5784 return(NULL);
5785 atom = xmlRegNewAtom(am, XML_REGEXP_STRING);
5786 if (atom == NULL)
5787 return(NULL);
5788 atom->valuep = xmlStrdup(token);
5789 atom->data = data;
5790 if (min == 0)
5791 atom->min = 1;
5792 else
5793 atom->min = min;
5794 atom->max = max;
5795
Daniel Veillard0ddb21c2004-02-12 12:43:49 +00005796 /*
5797 * associate a counter to the transition.
5798 */
5799 counter = xmlRegGetCounter(am);
5800 am->counters[counter].min = min;
5801 am->counters[counter].max = max;
5802
5803 /* xmlFAGenerateTransitions(am, from, to, atom); */
5804 if (to == NULL) {
5805 to = xmlRegNewState(am);
5806 xmlRegStatePush(am, to);
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00005807 }
Daniel Veillard5de09382005-09-26 17:18:17 +00005808 xmlRegStateAddTrans(am, from, atom, to, counter, -1);
Daniel Veillard0ddb21c2004-02-12 12:43:49 +00005809 xmlRegAtomPush(am, atom);
5810 am->state = to;
5811
Daniel Veillard4255d502002-04-16 15:50:10 +00005812 if (to == NULL)
5813 to = am->state;
5814 if (to == NULL)
5815 return(NULL);
5816 if (min == 0)
5817 xmlFAGenerateEpsilonTransition(am, from, to);
5818 return(to);
5819}
5820
5821/**
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00005822 * xmlAutomataNewOnceTrans2:
5823 * @am: an automata
5824 * @from: the starting point of the transition
5825 * @to: the target point of the transition or NULL
5826 * @token: the input string associated to that transition
5827 * @token2: the second input string associated to that transition
5828 * @min: the minimum successive occurences of token
5829 * @max: the maximum successive occurences of token
5830 * @data: data associated to the transition
5831 *
5832 * If @to is NULL, this creates first a new target state in the automata
5833 * and then adds a transition from the @from state to the target state
5834 * activated by a succession of input of value @token and @token2 and whose
5835 * number is between @min and @max, moreover that transition can only be
5836 * crossed once.
5837 *
5838 * Returns the target state or NULL in case of error
5839 */
5840xmlAutomataStatePtr
5841xmlAutomataNewOnceTrans2(xmlAutomataPtr am, xmlAutomataStatePtr from,
5842 xmlAutomataStatePtr to, const xmlChar *token,
5843 const xmlChar *token2,
5844 int min, int max, void *data) {
5845 xmlRegAtomPtr atom;
5846 int counter;
5847
5848 if ((am == NULL) || (from == NULL) || (token == NULL))
5849 return(NULL);
5850 if (min < 1)
5851 return(NULL);
5852 if ((max < min) || (max < 1))
5853 return(NULL);
5854 atom = xmlRegNewAtom(am, XML_REGEXP_STRING);
5855 if (atom == NULL)
5856 return(NULL);
5857 if ((token2 == NULL) || (*token2 == 0)) {
5858 atom->valuep = xmlStrdup(token);
5859 } else {
5860 int lenn, lenp;
5861 xmlChar *str;
5862
5863 lenn = strlen((char *) token2);
5864 lenp = strlen((char *) token);
5865
5866 str = (xmlChar *) xmlMallocAtomic(lenn + lenp + 2);
5867 if (str == NULL) {
5868 xmlRegFreeAtom(atom);
5869 return(NULL);
5870 }
5871 memcpy(&str[0], token, lenp);
5872 str[lenp] = '|';
5873 memcpy(&str[lenp + 1], token2, lenn);
5874 str[lenn + lenp + 1] = 0;
5875
5876 atom->valuep = str;
5877 }
5878 atom->data = data;
5879 atom->quant = XML_REGEXP_QUANT_ONCEONLY;
Daniel Veillard11ce4002006-03-10 00:36:23 +00005880 atom->min = min;
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00005881 atom->max = max;
5882 /*
5883 * associate a counter to the transition.
5884 */
5885 counter = xmlRegGetCounter(am);
5886 am->counters[counter].min = 1;
5887 am->counters[counter].max = 1;
5888
5889 /* xmlFAGenerateTransitions(am, from, to, atom); */
5890 if (to == NULL) {
5891 to = xmlRegNewState(am);
5892 xmlRegStatePush(am, to);
5893 }
Daniel Veillard5de09382005-09-26 17:18:17 +00005894 xmlRegStateAddTrans(am, from, atom, to, counter, -1);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00005895 xmlRegAtomPush(am, atom);
5896 am->state = to;
5897 return(to);
5898}
5899
5900
5901
5902/**
Daniel Veillard7646b182002-04-20 06:41:40 +00005903 * xmlAutomataNewOnceTrans:
5904 * @am: an automata
5905 * @from: the starting point of the transition
5906 * @to: the target point of the transition or NULL
5907 * @token: the input string associated to that transition
5908 * @min: the minimum successive occurences of token
Daniel Veillarda9b66d02002-12-11 14:23:49 +00005909 * @max: the maximum successive occurences of token
5910 * @data: data associated to the transition
Daniel Veillard7646b182002-04-20 06:41:40 +00005911 *
William M. Brackddf71d62004-05-06 04:17:26 +00005912 * If @to is NULL, this creates first a new target state in the automata
Daniel Veillard7646b182002-04-20 06:41:40 +00005913 * and then adds a transition from the @from state to the target state
5914 * activated by a succession of input of value @token and whose number
William M. Brackddf71d62004-05-06 04:17:26 +00005915 * is between @min and @max, moreover that transition can only be crossed
Daniel Veillard7646b182002-04-20 06:41:40 +00005916 * once.
5917 *
5918 * Returns the target state or NULL in case of error
5919 */
5920xmlAutomataStatePtr
5921xmlAutomataNewOnceTrans(xmlAutomataPtr am, xmlAutomataStatePtr from,
5922 xmlAutomataStatePtr to, const xmlChar *token,
5923 int min, int max, void *data) {
5924 xmlRegAtomPtr atom;
5925 int counter;
5926
5927 if ((am == NULL) || (from == NULL) || (token == NULL))
5928 return(NULL);
5929 if (min < 1)
5930 return(NULL);
5931 if ((max < min) || (max < 1))
5932 return(NULL);
5933 atom = xmlRegNewAtom(am, XML_REGEXP_STRING);
5934 if (atom == NULL)
5935 return(NULL);
5936 atom->valuep = xmlStrdup(token);
5937 atom->data = data;
5938 atom->quant = XML_REGEXP_QUANT_ONCEONLY;
Daniel Veillard11ce4002006-03-10 00:36:23 +00005939 atom->min = min;
Daniel Veillard7646b182002-04-20 06:41:40 +00005940 atom->max = max;
5941 /*
5942 * associate a counter to the transition.
5943 */
5944 counter = xmlRegGetCounter(am);
5945 am->counters[counter].min = 1;
5946 am->counters[counter].max = 1;
5947
5948 /* xmlFAGenerateTransitions(am, from, to, atom); */
5949 if (to == NULL) {
5950 to = xmlRegNewState(am);
5951 xmlRegStatePush(am, to);
5952 }
Daniel Veillard5de09382005-09-26 17:18:17 +00005953 xmlRegStateAddTrans(am, from, atom, to, counter, -1);
Daniel Veillard7646b182002-04-20 06:41:40 +00005954 xmlRegAtomPush(am, atom);
5955 am->state = to;
Daniel Veillard7646b182002-04-20 06:41:40 +00005956 return(to);
5957}
5958
5959/**
Daniel Veillard4255d502002-04-16 15:50:10 +00005960 * xmlAutomataNewState:
5961 * @am: an automata
5962 *
5963 * Create a new disconnected state in the automata
5964 *
5965 * Returns the new state or NULL in case of error
5966 */
5967xmlAutomataStatePtr
5968xmlAutomataNewState(xmlAutomataPtr am) {
5969 xmlAutomataStatePtr to;
5970
5971 if (am == NULL)
5972 return(NULL);
5973 to = xmlRegNewState(am);
5974 xmlRegStatePush(am, to);
5975 return(to);
5976}
5977
5978/**
Daniel Veillarda9b66d02002-12-11 14:23:49 +00005979 * xmlAutomataNewEpsilon:
Daniel Veillard4255d502002-04-16 15:50:10 +00005980 * @am: an automata
5981 * @from: the starting point of the transition
5982 * @to: the target point of the transition or NULL
5983 *
William M. Brackddf71d62004-05-06 04:17:26 +00005984 * If @to is NULL, this creates first a new target state in the automata
5985 * and then adds an epsilon transition from the @from state to the
Daniel Veillard4255d502002-04-16 15:50:10 +00005986 * target state
5987 *
5988 * Returns the target state or NULL in case of error
5989 */
5990xmlAutomataStatePtr
5991xmlAutomataNewEpsilon(xmlAutomataPtr am, xmlAutomataStatePtr from,
5992 xmlAutomataStatePtr to) {
5993 if ((am == NULL) || (from == NULL))
5994 return(NULL);
5995 xmlFAGenerateEpsilonTransition(am, from, to);
5996 if (to == NULL)
5997 return(am->state);
5998 return(to);
5999}
6000
Daniel Veillardb509f152002-04-17 16:28:10 +00006001/**
Daniel Veillard7646b182002-04-20 06:41:40 +00006002 * xmlAutomataNewAllTrans:
6003 * @am: an automata
6004 * @from: the starting point of the transition
6005 * @to: the target point of the transition or NULL
Daniel Veillarda9b66d02002-12-11 14:23:49 +00006006 * @lax: allow to transition if not all all transitions have been activated
Daniel Veillard7646b182002-04-20 06:41:40 +00006007 *
William M. Brackddf71d62004-05-06 04:17:26 +00006008 * If @to is NULL, this creates first a new target state in the automata
Daniel Veillard7646b182002-04-20 06:41:40 +00006009 * and then adds a an ALL transition from the @from state to the
6010 * target state. That transition is an epsilon transition allowed only when
6011 * all transitions from the @from node have been activated.
6012 *
6013 * Returns the target state or NULL in case of error
6014 */
6015xmlAutomataStatePtr
6016xmlAutomataNewAllTrans(xmlAutomataPtr am, xmlAutomataStatePtr from,
Daniel Veillard441bc322002-04-20 17:38:48 +00006017 xmlAutomataStatePtr to, int lax) {
Daniel Veillard7646b182002-04-20 06:41:40 +00006018 if ((am == NULL) || (from == NULL))
6019 return(NULL);
Daniel Veillard441bc322002-04-20 17:38:48 +00006020 xmlFAGenerateAllTransition(am, from, to, lax);
Daniel Veillard7646b182002-04-20 06:41:40 +00006021 if (to == NULL)
6022 return(am->state);
6023 return(to);
6024}
6025
6026/**
Daniel Veillardb509f152002-04-17 16:28:10 +00006027 * xmlAutomataNewCounter:
6028 * @am: an automata
6029 * @min: the minimal value on the counter
6030 * @max: the maximal value on the counter
6031 *
6032 * Create a new counter
6033 *
6034 * Returns the counter number or -1 in case of error
6035 */
6036int
6037xmlAutomataNewCounter(xmlAutomataPtr am, int min, int max) {
6038 int ret;
6039
6040 if (am == NULL)
6041 return(-1);
6042
6043 ret = xmlRegGetCounter(am);
6044 if (ret < 0)
6045 return(-1);
6046 am->counters[ret].min = min;
6047 am->counters[ret].max = max;
6048 return(ret);
6049}
6050
6051/**
6052 * xmlAutomataNewCountedTrans:
6053 * @am: an automata
6054 * @from: the starting point of the transition
6055 * @to: the target point of the transition or NULL
6056 * @counter: the counter associated to that transition
6057 *
William M. Brackddf71d62004-05-06 04:17:26 +00006058 * If @to is NULL, this creates first a new target state in the automata
Daniel Veillardb509f152002-04-17 16:28:10 +00006059 * and then adds an epsilon transition from the @from state to the target state
6060 * which will increment the counter provided
6061 *
6062 * Returns the target state or NULL in case of error
6063 */
6064xmlAutomataStatePtr
6065xmlAutomataNewCountedTrans(xmlAutomataPtr am, xmlAutomataStatePtr from,
6066 xmlAutomataStatePtr to, int counter) {
6067 if ((am == NULL) || (from == NULL) || (counter < 0))
6068 return(NULL);
6069 xmlFAGenerateCountedEpsilonTransition(am, from, to, counter);
6070 if (to == NULL)
6071 return(am->state);
6072 return(to);
6073}
6074
6075/**
6076 * xmlAutomataNewCounterTrans:
6077 * @am: an automata
6078 * @from: the starting point of the transition
6079 * @to: the target point of the transition or NULL
6080 * @counter: the counter associated to that transition
6081 *
William M. Brackddf71d62004-05-06 04:17:26 +00006082 * If @to is NULL, this creates first a new target state in the automata
Daniel Veillardb509f152002-04-17 16:28:10 +00006083 * and then adds an epsilon transition from the @from state to the target state
6084 * which will be allowed only if the counter is within the right range.
6085 *
6086 * Returns the target state or NULL in case of error
6087 */
6088xmlAutomataStatePtr
6089xmlAutomataNewCounterTrans(xmlAutomataPtr am, xmlAutomataStatePtr from,
6090 xmlAutomataStatePtr to, int counter) {
6091 if ((am == NULL) || (from == NULL) || (counter < 0))
6092 return(NULL);
6093 xmlFAGenerateCountedTransition(am, from, to, counter);
6094 if (to == NULL)
6095 return(am->state);
6096 return(to);
6097}
Daniel Veillard4255d502002-04-16 15:50:10 +00006098
6099/**
6100 * xmlAutomataCompile:
6101 * @am: an automata
6102 *
6103 * Compile the automata into a Reg Exp ready for being executed.
6104 * The automata should be free after this point.
6105 *
6106 * Returns the compiled regexp or NULL in case of error
6107 */
6108xmlRegexpPtr
6109xmlAutomataCompile(xmlAutomataPtr am) {
6110 xmlRegexpPtr ret;
6111
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00006112 if ((am == NULL) || (am->error != 0)) return(NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +00006113 xmlFAEliminateEpsilonTransitions(am);
Daniel Veillard23e73572002-09-19 19:56:43 +00006114 /* xmlFAComputesDeterminism(am); */
Daniel Veillard4255d502002-04-16 15:50:10 +00006115 ret = xmlRegEpxFromParse(am);
6116
6117 return(ret);
6118}
Daniel Veillarde19fc232002-04-22 16:01:24 +00006119
6120/**
6121 * xmlAutomataIsDeterminist:
6122 * @am: an automata
6123 *
6124 * Checks if an automata is determinist.
6125 *
6126 * Returns 1 if true, 0 if not, and -1 in case of error
6127 */
6128int
6129xmlAutomataIsDeterminist(xmlAutomataPtr am) {
6130 int ret;
6131
6132 if (am == NULL)
6133 return(-1);
6134
6135 ret = xmlFAComputesDeterminism(am);
6136 return(ret);
6137}
Daniel Veillard4255d502002-04-16 15:50:10 +00006138#endif /* LIBXML_AUTOMATA_ENABLED */
Daniel Veillard81a8ec62005-08-22 00:20:58 +00006139
6140#ifdef LIBXML_EXPR_ENABLED
6141/************************************************************************
6142 * *
6143 * Formal Expression handling code *
6144 * *
6145 ************************************************************************/
Daniel Veillard81a8ec62005-08-22 00:20:58 +00006146/************************************************************************
6147 * *
6148 * Expression handling context *
6149 * *
6150 ************************************************************************/
6151
6152struct _xmlExpCtxt {
6153 xmlDictPtr dict;
6154 xmlExpNodePtr *table;
6155 int size;
6156 int nbElems;
6157 int nb_nodes;
6158 const char *expr;
6159 const char *cur;
6160 int nb_cons;
Daniel Veillard81a8ec62005-08-22 00:20:58 +00006161 int tabSize;
6162};
6163
6164/**
6165 * xmlExpNewCtxt:
6166 * @maxNodes: the maximum number of nodes
6167 * @dict: optional dictionnary to use internally
6168 *
6169 * Creates a new context for manipulating expressions
6170 *
6171 * Returns the context or NULL in case of error
6172 */
6173xmlExpCtxtPtr
6174xmlExpNewCtxt(int maxNodes, xmlDictPtr dict) {
6175 xmlExpCtxtPtr ret;
6176 int size = 256;
6177
6178 if (maxNodes <= 4096)
6179 maxNodes = 4096;
6180
6181 ret = (xmlExpCtxtPtr) xmlMalloc(sizeof(xmlExpCtxt));
6182 if (ret == NULL)
6183 return(NULL);
6184 memset(ret, 0, sizeof(xmlExpCtxt));
6185 ret->size = size;
6186 ret->nbElems = 0;
6187 ret->table = xmlMalloc(size * sizeof(xmlExpNodePtr));
6188 if (ret->table == NULL) {
6189 xmlFree(ret);
6190 return(NULL);
6191 }
6192 memset(ret->table, 0, size * sizeof(xmlExpNodePtr));
6193 if (dict == NULL) {
6194 ret->dict = xmlDictCreate();
6195 if (ret->dict == NULL) {
6196 xmlFree(ret->table);
6197 xmlFree(ret);
6198 return(NULL);
6199 }
6200 } else {
6201 ret->dict = dict;
6202 xmlDictReference(ret->dict);
6203 }
6204 return(ret);
6205}
6206
6207/**
6208 * xmlExpFreeCtxt:
6209 * @ctxt: an expression context
6210 *
6211 * Free an expression context
6212 */
6213void
6214xmlExpFreeCtxt(xmlExpCtxtPtr ctxt) {
6215 if (ctxt == NULL)
6216 return;
6217 xmlDictFree(ctxt->dict);
6218 if (ctxt->table != NULL)
6219 xmlFree(ctxt->table);
6220 xmlFree(ctxt);
6221}
6222
6223/************************************************************************
6224 * *
6225 * Structure associated to an expression node *
6226 * *
6227 ************************************************************************/
Daniel Veillard465a0002005-08-22 12:07:04 +00006228#define MAX_NODES 10000
6229
6230/* #define DEBUG_DERIV */
6231
6232/*
6233 * TODO:
6234 * - Wildcards
6235 * - public API for creation
6236 *
6237 * Started
6238 * - regression testing
6239 *
6240 * Done
6241 * - split into module and test tool
6242 * - memleaks
6243 */
Daniel Veillard81a8ec62005-08-22 00:20:58 +00006244
6245typedef enum {
6246 XML_EXP_NILABLE = (1 << 0)
6247} xmlExpNodeInfo;
6248
6249#define IS_NILLABLE(node) ((node)->info & XML_EXP_NILABLE)
6250
6251struct _xmlExpNode {
6252 unsigned char type;/* xmlExpNodeType */
6253 unsigned char info;/* OR of xmlExpNodeInfo */
6254 unsigned short key; /* the hash key */
6255 unsigned int ref; /* The number of references */
6256 int c_max; /* the maximum length it can consume */
6257 xmlExpNodePtr exp_left;
6258 xmlExpNodePtr next;/* the next node in the hash table or free list */
6259 union {
6260 struct {
6261 int f_min;
6262 int f_max;
6263 } count;
6264 struct {
6265 xmlExpNodePtr f_right;
6266 } children;
6267 const xmlChar *f_str;
6268 } field;
6269};
6270
6271#define exp_min field.count.f_min
6272#define exp_max field.count.f_max
6273/* #define exp_left field.children.f_left */
6274#define exp_right field.children.f_right
6275#define exp_str field.f_str
6276
6277static xmlExpNodePtr xmlExpNewNode(xmlExpCtxtPtr ctxt, xmlExpNodeType type);
6278static xmlExpNode forbiddenExpNode = {
6279 XML_EXP_FORBID, 0, 0, 0, 0, NULL, NULL, {{ 0, 0}}
6280};
6281xmlExpNodePtr forbiddenExp = &forbiddenExpNode;
6282static xmlExpNode emptyExpNode = {
6283 XML_EXP_EMPTY, 1, 0, 0, 0, NULL, NULL, {{ 0, 0}}
6284};
6285xmlExpNodePtr emptyExp = &emptyExpNode;
6286
6287/************************************************************************
6288 * *
6289 * The custom hash table for unicity and canonicalization *
6290 * of sub-expressions pointers *
6291 * *
6292 ************************************************************************/
6293/*
6294 * xmlExpHashNameComputeKey:
6295 * Calculate the hash key for a token
6296 */
6297static unsigned short
6298xmlExpHashNameComputeKey(const xmlChar *name) {
6299 unsigned short value = 0L;
6300 char ch;
6301
6302 if (name != NULL) {
6303 value += 30 * (*name);
6304 while ((ch = *name++) != 0) {
6305 value = value ^ ((value << 5) + (value >> 3) + (unsigned long)ch);
6306 }
6307 }
6308 return (value);
6309}
6310
6311/*
6312 * xmlExpHashComputeKey:
6313 * Calculate the hash key for a compound expression
6314 */
6315static unsigned short
6316xmlExpHashComputeKey(xmlExpNodeType type, xmlExpNodePtr left,
6317 xmlExpNodePtr right) {
6318 unsigned long value;
6319 unsigned short ret;
6320
6321 switch (type) {
6322 case XML_EXP_SEQ:
6323 value = left->key;
6324 value += right->key;
6325 value *= 3;
6326 ret = (unsigned short) value;
6327 break;
6328 case XML_EXP_OR:
6329 value = left->key;
6330 value += right->key;
6331 value *= 7;
6332 ret = (unsigned short) value;
6333 break;
6334 case XML_EXP_COUNT:
6335 value = left->key;
6336 value += right->key;
6337 ret = (unsigned short) value;
6338 break;
6339 default:
6340 ret = 0;
6341 }
6342 return(ret);
6343}
6344
6345
6346static xmlExpNodePtr
6347xmlExpNewNode(xmlExpCtxtPtr ctxt, xmlExpNodeType type) {
6348 xmlExpNodePtr ret;
6349
6350 if (ctxt->nb_nodes >= MAX_NODES)
6351 return(NULL);
6352 ret = (xmlExpNodePtr) xmlMalloc(sizeof(xmlExpNode));
6353 if (ret == NULL)
6354 return(NULL);
6355 memset(ret, 0, sizeof(xmlExpNode));
6356 ret->type = type;
6357 ret->next = NULL;
6358 ctxt->nb_nodes++;
6359 ctxt->nb_cons++;
6360 return(ret);
6361}
6362
6363/**
6364 * xmlExpHashGetEntry:
6365 * @table: the hash table
6366 *
6367 * Get the unique entry from the hash table. The entry is created if
6368 * needed. @left and @right are consumed, i.e. their ref count will
6369 * be decremented by the operation.
6370 *
6371 * Returns the pointer or NULL in case of error
6372 */
6373static xmlExpNodePtr
6374xmlExpHashGetEntry(xmlExpCtxtPtr ctxt, xmlExpNodeType type,
6375 xmlExpNodePtr left, xmlExpNodePtr right,
6376 const xmlChar *name, int min, int max) {
6377 unsigned short kbase, key;
6378 xmlExpNodePtr entry;
6379 xmlExpNodePtr insert;
6380
6381 if (ctxt == NULL)
6382 return(NULL);
6383
6384 /*
6385 * Check for duplicate and insertion location.
6386 */
6387 if (type == XML_EXP_ATOM) {
6388 kbase = xmlExpHashNameComputeKey(name);
6389 } else if (type == XML_EXP_COUNT) {
6390 /* COUNT reduction rule 1 */
6391 /* a{1} -> a */
6392 if (min == max) {
6393 if (min == 1) {
6394 return(left);
6395 }
6396 if (min == 0) {
6397 xmlExpFree(ctxt, left);
6398 return(emptyExp);
6399 }
6400 }
6401 if (min < 0) {
6402 xmlExpFree(ctxt, left);
6403 return(forbiddenExp);
6404 }
6405 if (max == -1)
6406 kbase = min + 79;
6407 else
6408 kbase = max - min;
6409 kbase += left->key;
6410 } else if (type == XML_EXP_OR) {
6411 /* Forbid reduction rules */
6412 if (left->type == XML_EXP_FORBID) {
6413 xmlExpFree(ctxt, left);
6414 return(right);
6415 }
6416 if (right->type == XML_EXP_FORBID) {
6417 xmlExpFree(ctxt, right);
6418 return(left);
6419 }
6420
6421 /* OR reduction rule 1 */
6422 /* a | a reduced to a */
6423 if (left == right) {
6424 left->ref--;
6425 return(left);
6426 }
6427 /* OR canonicalization rule 1 */
6428 /* linearize (a | b) | c into a | (b | c) */
6429 if ((left->type == XML_EXP_OR) && (right->type != XML_EXP_OR)) {
6430 xmlExpNodePtr tmp = left;
6431 left = right;
6432 right = tmp;
6433 }
6434 /* OR reduction rule 2 */
6435 /* a | (a | b) and b | (a | b) are reduced to a | b */
6436 if (right->type == XML_EXP_OR) {
6437 if ((left == right->exp_left) ||
6438 (left == right->exp_right)) {
6439 xmlExpFree(ctxt, left);
6440 return(right);
6441 }
6442 }
6443 /* OR canonicalization rule 2 */
6444 /* linearize (a | b) | c into a | (b | c) */
6445 if (left->type == XML_EXP_OR) {
6446 xmlExpNodePtr tmp;
6447
6448 /* OR canonicalization rule 2 */
6449 if ((left->exp_right->type != XML_EXP_OR) &&
6450 (left->exp_right->key < left->exp_left->key)) {
6451 tmp = left->exp_right;
6452 left->exp_right = left->exp_left;
6453 left->exp_left = tmp;
6454 }
6455 left->exp_right->ref++;
6456 tmp = xmlExpHashGetEntry(ctxt, XML_EXP_OR, left->exp_right, right,
6457 NULL, 0, 0);
6458 left->exp_left->ref++;
6459 tmp = xmlExpHashGetEntry(ctxt, XML_EXP_OR, left->exp_left, tmp,
6460 NULL, 0, 0);
6461
6462 xmlExpFree(ctxt, left);
6463 return(tmp);
6464 }
6465 if (right->type == XML_EXP_OR) {
6466 /* Ordering in the tree */
6467 /* C | (A | B) -> A | (B | C) */
6468 if (left->key > right->exp_right->key) {
6469 xmlExpNodePtr tmp;
6470 right->exp_right->ref++;
6471 tmp = xmlExpHashGetEntry(ctxt, XML_EXP_OR, right->exp_right,
6472 left, NULL, 0, 0);
6473 right->exp_left->ref++;
6474 tmp = xmlExpHashGetEntry(ctxt, XML_EXP_OR, right->exp_left,
6475 tmp, NULL, 0, 0);
6476 xmlExpFree(ctxt, right);
6477 return(tmp);
6478 }
6479 /* Ordering in the tree */
6480 /* B | (A | C) -> A | (B | C) */
6481 if (left->key > right->exp_left->key) {
6482 xmlExpNodePtr tmp;
6483 right->exp_right->ref++;
6484 tmp = xmlExpHashGetEntry(ctxt, XML_EXP_OR, left,
6485 right->exp_right, NULL, 0, 0);
6486 right->exp_left->ref++;
6487 tmp = xmlExpHashGetEntry(ctxt, XML_EXP_OR, right->exp_left,
6488 tmp, NULL, 0, 0);
6489 xmlExpFree(ctxt, right);
6490 return(tmp);
6491 }
6492 }
6493 /* we know both types are != XML_EXP_OR here */
6494 else if (left->key > right->key) {
6495 xmlExpNodePtr tmp = left;
6496 left = right;
6497 right = tmp;
6498 }
6499 kbase = xmlExpHashComputeKey(type, left, right);
6500 } else if (type == XML_EXP_SEQ) {
6501 /* Forbid reduction rules */
6502 if (left->type == XML_EXP_FORBID) {
6503 xmlExpFree(ctxt, right);
6504 return(left);
6505 }
6506 if (right->type == XML_EXP_FORBID) {
6507 xmlExpFree(ctxt, left);
6508 return(right);
6509 }
6510 /* Empty reduction rules */
6511 if (right->type == XML_EXP_EMPTY) {
6512 return(left);
6513 }
6514 if (left->type == XML_EXP_EMPTY) {
6515 return(right);
6516 }
6517 kbase = xmlExpHashComputeKey(type, left, right);
6518 } else
6519 return(NULL);
6520
6521 key = kbase % ctxt->size;
6522 if (ctxt->table[key] != NULL) {
6523 for (insert = ctxt->table[key]; insert != NULL;
6524 insert = insert->next) {
6525 if ((insert->key == kbase) &&
6526 (insert->type == type)) {
6527 if (type == XML_EXP_ATOM) {
6528 if (name == insert->exp_str) {
6529 insert->ref++;
6530 return(insert);
6531 }
6532 } else if (type == XML_EXP_COUNT) {
6533 if ((insert->exp_min == min) && (insert->exp_max == max) &&
6534 (insert->exp_left == left)) {
6535 insert->ref++;
6536 left->ref--;
6537 return(insert);
6538 }
6539 } else if ((insert->exp_left == left) &&
6540 (insert->exp_right == right)) {
6541 insert->ref++;
6542 left->ref--;
6543 right->ref--;
6544 return(insert);
6545 }
6546 }
6547 }
6548 }
6549
6550 entry = xmlExpNewNode(ctxt, type);
6551 if (entry == NULL)
6552 return(NULL);
6553 entry->key = kbase;
6554 if (type == XML_EXP_ATOM) {
6555 entry->exp_str = name;
6556 entry->c_max = 1;
6557 } else if (type == XML_EXP_COUNT) {
6558 entry->exp_min = min;
6559 entry->exp_max = max;
6560 entry->exp_left = left;
6561 if ((min == 0) || (IS_NILLABLE(left)))
6562 entry->info |= XML_EXP_NILABLE;
6563 if (max < 0)
6564 entry->c_max = -1;
6565 else
6566 entry->c_max = max * entry->exp_left->c_max;
6567 } else {
6568 entry->exp_left = left;
6569 entry->exp_right = right;
6570 if (type == XML_EXP_OR) {
6571 if ((IS_NILLABLE(left)) || (IS_NILLABLE(right)))
6572 entry->info |= XML_EXP_NILABLE;
6573 if ((entry->exp_left->c_max == -1) ||
6574 (entry->exp_right->c_max == -1))
6575 entry->c_max = -1;
6576 else if (entry->exp_left->c_max > entry->exp_right->c_max)
6577 entry->c_max = entry->exp_left->c_max;
6578 else
6579 entry->c_max = entry->exp_right->c_max;
6580 } else {
6581 if ((IS_NILLABLE(left)) && (IS_NILLABLE(right)))
6582 entry->info |= XML_EXP_NILABLE;
6583 if ((entry->exp_left->c_max == -1) ||
6584 (entry->exp_right->c_max == -1))
6585 entry->c_max = -1;
6586 else
6587 entry->c_max = entry->exp_left->c_max + entry->exp_right->c_max;
6588 }
6589 }
6590 entry->ref = 1;
6591 if (ctxt->table[key] != NULL)
6592 entry->next = ctxt->table[key];
6593
6594 ctxt->table[key] = entry;
6595 ctxt->nbElems++;
6596
6597 return(entry);
6598}
6599
6600/**
6601 * xmlExpFree:
6602 * @ctxt: the expression context
6603 * @exp: the expression
6604 *
6605 * Dereference the expression
6606 */
6607void
6608xmlExpFree(xmlExpCtxtPtr ctxt, xmlExpNodePtr exp) {
6609 if ((exp == NULL) || (exp == forbiddenExp) || (exp == emptyExp))
6610 return;
6611 exp->ref--;
Daniel Veillard81a8ec62005-08-22 00:20:58 +00006612 if (exp->ref == 0) {
6613 unsigned short key;
6614
6615 /* Unlink it first from the hash table */
6616 key = exp->key % ctxt->size;
6617 if (ctxt->table[key] == exp) {
6618 ctxt->table[key] = exp->next;
6619 } else {
6620 xmlExpNodePtr tmp;
6621
6622 tmp = ctxt->table[key];
6623 while (tmp != NULL) {
6624 if (tmp->next == exp) {
6625 tmp->next = exp->next;
6626 break;
6627 }
6628 tmp = tmp->next;
6629 }
6630 }
6631
6632 if ((exp->type == XML_EXP_SEQ) || (exp->type == XML_EXP_OR)) {
6633 xmlExpFree(ctxt, exp->exp_left);
6634 xmlExpFree(ctxt, exp->exp_right);
6635 } else if (exp->type == XML_EXP_COUNT) {
6636 xmlExpFree(ctxt, exp->exp_left);
6637 }
6638 xmlFree(exp);
Daniel Veillard81a8ec62005-08-22 00:20:58 +00006639 ctxt->nb_nodes--;
6640 }
6641}
6642
6643/**
6644 * xmlExpRef:
6645 * @exp: the expression
6646 *
6647 * Increase the reference count of the expression
6648 */
6649void
6650xmlExpRef(xmlExpNodePtr exp) {
6651 if (exp != NULL)
6652 exp->ref++;
6653}
6654
Daniel Veillardccb4d412005-08-23 13:41:17 +00006655/**
6656 * xmlExpNewAtom:
6657 * @ctxt: the expression context
6658 * @name: the atom name
6659 * @len: the atom name lenght in byte (or -1);
6660 *
6661 * Get the atom associated to this name from that context
6662 *
6663 * Returns the node or NULL in case of error
6664 */
6665xmlExpNodePtr
6666xmlExpNewAtom(xmlExpCtxtPtr ctxt, const xmlChar *name, int len) {
6667 if ((ctxt == NULL) || (name == NULL))
6668 return(NULL);
6669 name = xmlDictLookup(ctxt->dict, name, len);
6670 if (name == NULL)
6671 return(NULL);
6672 return(xmlExpHashGetEntry(ctxt, XML_EXP_ATOM, NULL, NULL, name, 0, 0));
6673}
6674
6675/**
6676 * xmlExpNewOr:
6677 * @ctxt: the expression context
6678 * @left: left expression
6679 * @right: right expression
6680 *
6681 * Get the atom associated to the choice @left | @right
6682 * Note that @left and @right are consumed in the operation, to keep
6683 * an handle on them use xmlExpRef() and use xmlExpFree() to release them,
6684 * this is true even in case of failure (unless ctxt == NULL).
6685 *
6686 * Returns the node or NULL in case of error
6687 */
6688xmlExpNodePtr
6689xmlExpNewOr(xmlExpCtxtPtr ctxt, xmlExpNodePtr left, xmlExpNodePtr right) {
Daniel Veillard11ce4002006-03-10 00:36:23 +00006690 if (ctxt == NULL)
6691 return(NULL);
6692 if ((left == NULL) || (right == NULL)) {
Daniel Veillardccb4d412005-08-23 13:41:17 +00006693 xmlExpFree(ctxt, left);
6694 xmlExpFree(ctxt, right);
6695 return(NULL);
6696 }
6697 return(xmlExpHashGetEntry(ctxt, XML_EXP_OR, left, right, NULL, 0, 0));
6698}
6699
6700/**
6701 * xmlExpNewSeq:
6702 * @ctxt: the expression context
6703 * @left: left expression
6704 * @right: right expression
6705 *
6706 * Get the atom associated to the sequence @left , @right
6707 * Note that @left and @right are consumed in the operation, to keep
6708 * an handle on them use xmlExpRef() and use xmlExpFree() to release them,
6709 * this is true even in case of failure (unless ctxt == NULL).
6710 *
6711 * Returns the node or NULL in case of error
6712 */
6713xmlExpNodePtr
6714xmlExpNewSeq(xmlExpCtxtPtr ctxt, xmlExpNodePtr left, xmlExpNodePtr right) {
Daniel Veillard11ce4002006-03-10 00:36:23 +00006715 if (ctxt == NULL)
6716 return(NULL);
6717 if ((left == NULL) || (right == NULL)) {
Daniel Veillardccb4d412005-08-23 13:41:17 +00006718 xmlExpFree(ctxt, left);
6719 xmlExpFree(ctxt, right);
6720 return(NULL);
6721 }
6722 return(xmlExpHashGetEntry(ctxt, XML_EXP_SEQ, left, right, NULL, 0, 0));
6723}
6724
6725/**
6726 * xmlExpNewRange:
6727 * @ctxt: the expression context
6728 * @subset: the expression to be repeated
6729 * @min: the lower bound for the repetition
6730 * @max: the upper bound for the repetition, -1 means infinite
6731 *
6732 * Get the atom associated to the range (@subset){@min, @max}
6733 * Note that @subset is consumed in the operation, to keep
6734 * an handle on it use xmlExpRef() and use xmlExpFree() to release it,
6735 * this is true even in case of failure (unless ctxt == NULL).
6736 *
6737 * Returns the node or NULL in case of error
6738 */
6739xmlExpNodePtr
6740xmlExpNewRange(xmlExpCtxtPtr ctxt, xmlExpNodePtr subset, int min, int max) {
Daniel Veillard11ce4002006-03-10 00:36:23 +00006741 if (ctxt == NULL)
6742 return(NULL);
6743 if ((subset == NULL) || (min < 0) || (max < -1) ||
Daniel Veillardccb4d412005-08-23 13:41:17 +00006744 ((max >= 0) && (min > max))) {
6745 xmlExpFree(ctxt, subset);
6746 return(NULL);
6747 }
6748 return(xmlExpHashGetEntry(ctxt, XML_EXP_COUNT, subset,
6749 NULL, NULL, min, max));
6750}
6751
Daniel Veillard81a8ec62005-08-22 00:20:58 +00006752/************************************************************************
6753 * *
6754 * Public API for operations on expressions *
6755 * *
6756 ************************************************************************/
6757
6758static int
6759xmlExpGetLanguageInt(xmlExpCtxtPtr ctxt, xmlExpNodePtr exp,
6760 const xmlChar**list, int len, int nb) {
6761 int tmp, tmp2;
6762tail:
6763 switch (exp->type) {
6764 case XML_EXP_EMPTY:
6765 return(0);
6766 case XML_EXP_ATOM:
6767 for (tmp = 0;tmp < nb;tmp++)
6768 if (list[tmp] == exp->exp_str)
6769 return(0);
6770 if (nb >= len)
6771 return(-2);
6772 list[nb++] = exp->exp_str;
6773 return(1);
6774 case XML_EXP_COUNT:
6775 exp = exp->exp_left;
6776 goto tail;
6777 case XML_EXP_SEQ:
6778 case XML_EXP_OR:
6779 tmp = xmlExpGetLanguageInt(ctxt, exp->exp_left, list, len, nb);
6780 if (tmp < 0)
6781 return(tmp);
6782 tmp2 = xmlExpGetLanguageInt(ctxt, exp->exp_right, list, len,
6783 nb + tmp);
6784 if (tmp2 < 0)
6785 return(tmp2);
6786 return(tmp + tmp2);
6787 }
6788 return(-1);
6789}
6790
6791/**
6792 * xmlExpGetLanguage:
6793 * @ctxt: the expression context
6794 * @exp: the expression
Daniel Veillard7802ba52005-10-27 11:56:20 +00006795 * @langList: where to store the tokens
Daniel Veillard81a8ec62005-08-22 00:20:58 +00006796 * @len: the allocated lenght of @list
6797 *
6798 * Find all the strings used in @exp and store them in @list
6799 *
6800 * Returns the number of unique strings found, -1 in case of errors and
6801 * -2 if there is more than @len strings
6802 */
6803int
6804xmlExpGetLanguage(xmlExpCtxtPtr ctxt, xmlExpNodePtr exp,
Daniel Veillard7802ba52005-10-27 11:56:20 +00006805 const xmlChar**langList, int len) {
6806 if ((ctxt == NULL) || (exp == NULL) || (langList == NULL) || (len <= 0))
Daniel Veillard81a8ec62005-08-22 00:20:58 +00006807 return(-1);
Daniel Veillard7802ba52005-10-27 11:56:20 +00006808 return(xmlExpGetLanguageInt(ctxt, exp, langList, len, 0));
Daniel Veillard81a8ec62005-08-22 00:20:58 +00006809}
6810
6811static int
6812xmlExpGetStartInt(xmlExpCtxtPtr ctxt, xmlExpNodePtr exp,
6813 const xmlChar**list, int len, int nb) {
6814 int tmp, tmp2;
6815tail:
6816 switch (exp->type) {
6817 case XML_EXP_FORBID:
6818 return(0);
6819 case XML_EXP_EMPTY:
6820 return(0);
6821 case XML_EXP_ATOM:
6822 for (tmp = 0;tmp < nb;tmp++)
6823 if (list[tmp] == exp->exp_str)
6824 return(0);
6825 if (nb >= len)
6826 return(-2);
6827 list[nb++] = exp->exp_str;
6828 return(1);
6829 case XML_EXP_COUNT:
6830 exp = exp->exp_left;
6831 goto tail;
6832 case XML_EXP_SEQ:
6833 tmp = xmlExpGetStartInt(ctxt, exp->exp_left, list, len, nb);
6834 if (tmp < 0)
6835 return(tmp);
6836 if (IS_NILLABLE(exp->exp_left)) {
6837 tmp2 = xmlExpGetStartInt(ctxt, exp->exp_right, list, len,
6838 nb + tmp);
6839 if (tmp2 < 0)
6840 return(tmp2);
6841 tmp += tmp2;
6842 }
6843 return(tmp);
6844 case XML_EXP_OR:
6845 tmp = xmlExpGetStartInt(ctxt, exp->exp_left, list, len, nb);
6846 if (tmp < 0)
6847 return(tmp);
6848 tmp2 = xmlExpGetStartInt(ctxt, exp->exp_right, list, len,
6849 nb + tmp);
6850 if (tmp2 < 0)
6851 return(tmp2);
6852 return(tmp + tmp2);
6853 }
6854 return(-1);
6855}
6856
6857/**
6858 * xmlExpGetStart:
6859 * @ctxt: the expression context
6860 * @exp: the expression
Daniel Veillard7802ba52005-10-27 11:56:20 +00006861 * @tokList: where to store the tokens
Daniel Veillard81a8ec62005-08-22 00:20:58 +00006862 * @len: the allocated lenght of @list
6863 *
6864 * Find all the strings that appears at the start of the languages
6865 * accepted by @exp and store them in @list. E.g. for (a, b) | c
6866 * it will return the list [a, c]
6867 *
6868 * Returns the number of unique strings found, -1 in case of errors and
6869 * -2 if there is more than @len strings
6870 */
6871int
6872xmlExpGetStart(xmlExpCtxtPtr ctxt, xmlExpNodePtr exp,
Daniel Veillard7802ba52005-10-27 11:56:20 +00006873 const xmlChar**tokList, int len) {
6874 if ((ctxt == NULL) || (exp == NULL) || (tokList == NULL) || (len <= 0))
Daniel Veillard81a8ec62005-08-22 00:20:58 +00006875 return(-1);
Daniel Veillard7802ba52005-10-27 11:56:20 +00006876 return(xmlExpGetStartInt(ctxt, exp, tokList, len, 0));
Daniel Veillard81a8ec62005-08-22 00:20:58 +00006877}
6878
6879/**
6880 * xmlExpIsNillable:
6881 * @exp: the expression
6882 *
6883 * Finds if the expression is nillable, i.e. if it accepts the empty sequqnce
6884 *
6885 * Returns 1 if nillable, 0 if not and -1 in case of error
6886 */
6887int
6888xmlExpIsNillable(xmlExpNodePtr exp) {
6889 if (exp == NULL)
6890 return(-1);
6891 return(IS_NILLABLE(exp) != 0);
6892}
6893
6894static xmlExpNodePtr
6895xmlExpStringDeriveInt(xmlExpCtxtPtr ctxt, xmlExpNodePtr exp, const xmlChar *str)
6896{
6897 xmlExpNodePtr ret;
6898
6899 switch (exp->type) {
6900 case XML_EXP_EMPTY:
6901 return(forbiddenExp);
6902 case XML_EXP_FORBID:
6903 return(forbiddenExp);
6904 case XML_EXP_ATOM:
6905 if (exp->exp_str == str) {
6906#ifdef DEBUG_DERIV
6907 printf("deriv atom: equal => Empty\n");
6908#endif
6909 ret = emptyExp;
6910 } else {
6911#ifdef DEBUG_DERIV
6912 printf("deriv atom: mismatch => forbid\n");
6913#endif
6914 /* TODO wildcards here */
6915 ret = forbiddenExp;
6916 }
6917 return(ret);
6918 case XML_EXP_OR: {
6919 xmlExpNodePtr tmp;
6920
6921#ifdef DEBUG_DERIV
6922 printf("deriv or: => or(derivs)\n");
6923#endif
6924 tmp = xmlExpStringDeriveInt(ctxt, exp->exp_left, str);
6925 if (tmp == NULL) {
6926 return(NULL);
6927 }
6928 ret = xmlExpStringDeriveInt(ctxt, exp->exp_right, str);
6929 if (ret == NULL) {
6930 xmlExpFree(ctxt, tmp);
6931 return(NULL);
6932 }
6933 ret = xmlExpHashGetEntry(ctxt, XML_EXP_OR, tmp, ret,
6934 NULL, 0, 0);
6935 return(ret);
6936 }
6937 case XML_EXP_SEQ:
6938#ifdef DEBUG_DERIV
6939 printf("deriv seq: starting with left\n");
6940#endif
6941 ret = xmlExpStringDeriveInt(ctxt, exp->exp_left, str);
6942 if (ret == NULL) {
6943 return(NULL);
6944 } else if (ret == forbiddenExp) {
6945 if (IS_NILLABLE(exp->exp_left)) {
6946#ifdef DEBUG_DERIV
6947 printf("deriv seq: left failed but nillable\n");
6948#endif
6949 ret = xmlExpStringDeriveInt(ctxt, exp->exp_right, str);
6950 }
6951 } else {
6952#ifdef DEBUG_DERIV
6953 printf("deriv seq: left match => sequence\n");
6954#endif
6955 exp->exp_right->ref++;
6956 ret = xmlExpHashGetEntry(ctxt, XML_EXP_SEQ, ret, exp->exp_right,
6957 NULL, 0, 0);
6958 }
6959 return(ret);
6960 case XML_EXP_COUNT: {
6961 int min, max;
6962 xmlExpNodePtr tmp;
6963
6964 if (exp->exp_max == 0)
6965 return(forbiddenExp);
6966 ret = xmlExpStringDeriveInt(ctxt, exp->exp_left, str);
6967 if (ret == NULL)
6968 return(NULL);
6969 if (ret == forbiddenExp) {
6970#ifdef DEBUG_DERIV
6971 printf("deriv count: pattern mismatch => forbid\n");
6972#endif
6973 return(ret);
6974 }
6975 if (exp->exp_max == 1)
6976 return(ret);
6977 if (exp->exp_max < 0) /* unbounded */
6978 max = -1;
6979 else
6980 max = exp->exp_max - 1;
6981 if (exp->exp_min > 0)
6982 min = exp->exp_min - 1;
6983 else
6984 min = 0;
6985 exp->exp_left->ref++;
6986 tmp = xmlExpHashGetEntry(ctxt, XML_EXP_COUNT, exp->exp_left, NULL,
6987 NULL, min, max);
6988 if (ret == emptyExp) {
6989#ifdef DEBUG_DERIV
6990 printf("deriv count: match to empty => new count\n");
6991#endif
6992 return(tmp);
6993 }
6994#ifdef DEBUG_DERIV
6995 printf("deriv count: match => sequence with new count\n");
6996#endif
6997 return(xmlExpHashGetEntry(ctxt, XML_EXP_SEQ, ret, tmp,
6998 NULL, 0, 0));
6999 }
7000 }
7001 return(NULL);
7002}
7003
7004/**
7005 * xmlExpStringDerive:
7006 * @ctxt: the expression context
7007 * @exp: the expression
7008 * @str: the string
7009 * @len: the string len in bytes if available
7010 *
7011 * Do one step of Brzozowski derivation of the expression @exp with
7012 * respect to the input string
7013 *
7014 * Returns the resulting expression or NULL in case of internal error
7015 */
7016xmlExpNodePtr
7017xmlExpStringDerive(xmlExpCtxtPtr ctxt, xmlExpNodePtr exp,
7018 const xmlChar *str, int len) {
7019 const xmlChar *input;
7020
7021 if ((exp == NULL) || (ctxt == NULL) || (str == NULL)) {
7022 return(NULL);
7023 }
7024 /*
7025 * check the string is in the dictionnary, if yes use an interned
7026 * copy, otherwise we know it's not an acceptable input
7027 */
7028 input = xmlDictExists(ctxt->dict, str, len);
7029 if (input == NULL) {
7030 return(forbiddenExp);
7031 }
7032 return(xmlExpStringDeriveInt(ctxt, exp, input));
7033}
7034
7035static int
7036xmlExpCheckCard(xmlExpNodePtr exp, xmlExpNodePtr sub) {
7037 int ret = 1;
7038
7039 if (sub->c_max == -1) {
7040 if (exp->c_max != -1)
7041 ret = 0;
7042 } else if ((exp->c_max >= 0) && (exp->c_max < sub->c_max)) {
7043 ret = 0;
7044 }
7045#if 0
7046 if ((IS_NILLABLE(sub)) && (!IS_NILLABLE(exp)))
7047 ret = 0;
7048#endif
7049 return(ret);
7050}
7051
7052static xmlExpNodePtr xmlExpExpDeriveInt(xmlExpCtxtPtr ctxt, xmlExpNodePtr exp,
7053 xmlExpNodePtr sub);
7054/**
7055 * xmlExpDivide:
7056 * @ctxt: the expressions context
7057 * @exp: the englobing expression
7058 * @sub: the subexpression
7059 * @mult: the multiple expression
7060 * @remain: the remain from the derivation of the multiple
7061 *
7062 * Check if exp is a multiple of sub, i.e. if there is a finite number n
7063 * so that sub{n} subsume exp
7064 *
7065 * Returns the multiple value if successful, 0 if it is not a multiple
7066 * and -1 in case of internel error.
7067 */
7068
7069static int
7070xmlExpDivide(xmlExpCtxtPtr ctxt, xmlExpNodePtr exp, xmlExpNodePtr sub,
7071 xmlExpNodePtr *mult, xmlExpNodePtr *remain) {
7072 int i;
7073 xmlExpNodePtr tmp, tmp2;
7074
7075 if (mult != NULL) *mult = NULL;
7076 if (remain != NULL) *remain = NULL;
7077 if (exp->c_max == -1) return(0);
7078 if (IS_NILLABLE(exp) && (!IS_NILLABLE(sub))) return(0);
7079
7080 for (i = 1;i <= exp->c_max;i++) {
7081 sub->ref++;
7082 tmp = xmlExpHashGetEntry(ctxt, XML_EXP_COUNT,
7083 sub, NULL, NULL, i, i);
7084 if (tmp == NULL) {
7085 return(-1);
7086 }
7087 if (!xmlExpCheckCard(tmp, exp)) {
7088 xmlExpFree(ctxt, tmp);
7089 continue;
7090 }
7091 tmp2 = xmlExpExpDeriveInt(ctxt, tmp, exp);
7092 if (tmp2 == NULL) {
7093 xmlExpFree(ctxt, tmp);
7094 return(-1);
7095 }
7096 if ((tmp2 != forbiddenExp) && (IS_NILLABLE(tmp2))) {
7097 if (remain != NULL)
7098 *remain = tmp2;
7099 else
7100 xmlExpFree(ctxt, tmp2);
7101 if (mult != NULL)
7102 *mult = tmp;
7103 else
7104 xmlExpFree(ctxt, tmp);
7105#ifdef DEBUG_DERIV
7106 printf("Divide succeeded %d\n", i);
7107#endif
7108 return(i);
7109 }
7110 xmlExpFree(ctxt, tmp);
7111 xmlExpFree(ctxt, tmp2);
7112 }
7113#ifdef DEBUG_DERIV
7114 printf("Divide failed\n");
7115#endif
7116 return(0);
7117}
7118
7119/**
7120 * xmlExpExpDeriveInt:
7121 * @ctxt: the expressions context
7122 * @exp: the englobing expression
7123 * @sub: the subexpression
7124 *
7125 * Try to do a step of Brzozowski derivation but at a higher level
7126 * the input being a subexpression.
7127 *
7128 * Returns the resulting expression or NULL in case of internal error
7129 */
7130static xmlExpNodePtr
7131xmlExpExpDeriveInt(xmlExpCtxtPtr ctxt, xmlExpNodePtr exp, xmlExpNodePtr sub) {
7132 xmlExpNodePtr ret, tmp, tmp2, tmp3;
7133 const xmlChar **tab;
7134 int len, i;
7135
7136 /*
7137 * In case of equality and if the expression can only consume a finite
7138 * amount, then the derivation is empty
7139 */
7140 if ((exp == sub) && (exp->c_max >= 0)) {
7141#ifdef DEBUG_DERIV
7142 printf("Equal(exp, sub) and finite -> Empty\n");
7143#endif
7144 return(emptyExp);
7145 }
7146 /*
7147 * decompose sub sequence first
7148 */
7149 if (sub->type == XML_EXP_EMPTY) {
7150#ifdef DEBUG_DERIV
7151 printf("Empty(sub) -> Empty\n");
7152#endif
7153 exp->ref++;
7154 return(exp);
7155 }
7156 if (sub->type == XML_EXP_SEQ) {
7157#ifdef DEBUG_DERIV
7158 printf("Seq(sub) -> decompose\n");
7159#endif
7160 tmp = xmlExpExpDeriveInt(ctxt, exp, sub->exp_left);
7161 if (tmp == NULL)
7162 return(NULL);
7163 if (tmp == forbiddenExp)
7164 return(tmp);
7165 ret = xmlExpExpDeriveInt(ctxt, tmp, sub->exp_right);
7166 xmlExpFree(ctxt, tmp);
7167 return(ret);
7168 }
7169 if (sub->type == XML_EXP_OR) {
7170#ifdef DEBUG_DERIV
7171 printf("Or(sub) -> decompose\n");
7172#endif
7173 tmp = xmlExpExpDeriveInt(ctxt, exp, sub->exp_left);
7174 if (tmp == forbiddenExp)
7175 return(tmp);
7176 if (tmp == NULL)
7177 return(NULL);
7178 ret = xmlExpExpDeriveInt(ctxt, exp, sub->exp_right);
7179 if ((ret == NULL) || (ret == forbiddenExp)) {
7180 xmlExpFree(ctxt, tmp);
7181 return(ret);
7182 }
7183 return(xmlExpHashGetEntry(ctxt, XML_EXP_OR, tmp, ret, NULL, 0, 0));
7184 }
7185 if (!xmlExpCheckCard(exp, sub)) {
7186#ifdef DEBUG_DERIV
7187 printf("CheckCard(exp, sub) failed -> Forbid\n");
7188#endif
7189 return(forbiddenExp);
7190 }
7191 switch (exp->type) {
7192 case XML_EXP_EMPTY:
7193 if (sub == emptyExp)
7194 return(emptyExp);
7195#ifdef DEBUG_DERIV
7196 printf("Empty(exp) -> Forbid\n");
7197#endif
7198 return(forbiddenExp);
7199 case XML_EXP_FORBID:
7200#ifdef DEBUG_DERIV
7201 printf("Forbid(exp) -> Forbid\n");
7202#endif
7203 return(forbiddenExp);
7204 case XML_EXP_ATOM:
7205 if (sub->type == XML_EXP_ATOM) {
7206 /* TODO: handle wildcards */
7207 if (exp->exp_str == sub->exp_str) {
7208#ifdef DEBUG_DERIV
7209 printf("Atom match -> Empty\n");
7210#endif
7211 return(emptyExp);
7212 }
7213#ifdef DEBUG_DERIV
7214 printf("Atom mismatch -> Forbid\n");
7215#endif
7216 return(forbiddenExp);
7217 }
7218 if ((sub->type == XML_EXP_COUNT) &&
7219 (sub->exp_max == 1) &&
7220 (sub->exp_left->type == XML_EXP_ATOM)) {
7221 /* TODO: handle wildcards */
7222 if (exp->exp_str == sub->exp_left->exp_str) {
7223#ifdef DEBUG_DERIV
7224 printf("Atom match -> Empty\n");
7225#endif
7226 return(emptyExp);
7227 }
7228#ifdef DEBUG_DERIV
7229 printf("Atom mismatch -> Forbid\n");
7230#endif
7231 return(forbiddenExp);
7232 }
7233#ifdef DEBUG_DERIV
7234 printf("Compex exp vs Atom -> Forbid\n");
7235#endif
7236 return(forbiddenExp);
7237 case XML_EXP_SEQ:
7238 /* try to get the sequence consumed only if possible */
7239 if (xmlExpCheckCard(exp->exp_left, sub)) {
7240 /* See if the sequence can be consumed directly */
7241#ifdef DEBUG_DERIV
7242 printf("Seq trying left only\n");
7243#endif
7244 ret = xmlExpExpDeriveInt(ctxt, exp->exp_left, sub);
7245 if ((ret != forbiddenExp) && (ret != NULL)) {
7246#ifdef DEBUG_DERIV
7247 printf("Seq trying left only worked\n");
7248#endif
7249 /*
7250 * TODO: assumption here that we are determinist
7251 * i.e. we won't get to a nillable exp left
7252 * subset which could be matched by the right
7253 * part too.
7254 * e.g.: (a | b)+,(a | c) and 'a+,a'
7255 */
7256 exp->exp_right->ref++;
7257 return(xmlExpHashGetEntry(ctxt, XML_EXP_SEQ, ret,
7258 exp->exp_right, NULL, 0, 0));
7259 }
7260#ifdef DEBUG_DERIV
7261 } else {
7262 printf("Seq: left too short\n");
7263#endif
7264 }
7265 /* Try instead to decompose */
7266 if (sub->type == XML_EXP_COUNT) {
7267 int min, max;
7268
7269#ifdef DEBUG_DERIV
7270 printf("Seq: sub is a count\n");
7271#endif
7272 ret = xmlExpExpDeriveInt(ctxt, exp->exp_left, sub->exp_left);
7273 if (ret == NULL)
7274 return(NULL);
7275 if (ret != forbiddenExp) {
7276#ifdef DEBUG_DERIV
7277 printf("Seq , Count match on left\n");
7278#endif
7279 if (sub->exp_max < 0)
7280 max = -1;
7281 else
7282 max = sub->exp_max -1;
7283 if (sub->exp_min > 0)
7284 min = sub->exp_min -1;
7285 else
7286 min = 0;
7287 exp->exp_right->ref++;
7288 tmp = xmlExpHashGetEntry(ctxt, XML_EXP_SEQ, ret,
7289 exp->exp_right, NULL, 0, 0);
7290 if (tmp == NULL)
7291 return(NULL);
7292
7293 sub->exp_left->ref++;
7294 tmp2 = xmlExpHashGetEntry(ctxt, XML_EXP_COUNT,
7295 sub->exp_left, NULL, NULL, min, max);
7296 if (tmp2 == NULL) {
7297 xmlExpFree(ctxt, tmp);
7298 return(NULL);
7299 }
7300 ret = xmlExpExpDeriveInt(ctxt, tmp, tmp2);
7301 xmlExpFree(ctxt, tmp);
7302 xmlExpFree(ctxt, tmp2);
7303 return(ret);
7304 }
7305 }
7306 /* we made no progress on structured operations */
7307 break;
7308 case XML_EXP_OR:
7309#ifdef DEBUG_DERIV
7310 printf("Or , trying both side\n");
7311#endif
7312 ret = xmlExpExpDeriveInt(ctxt, exp->exp_left, sub);
7313 if (ret == NULL)
7314 return(NULL);
7315 tmp = xmlExpExpDeriveInt(ctxt, exp->exp_right, sub);
7316 if (tmp == NULL) {
7317 xmlExpFree(ctxt, ret);
7318 return(NULL);
7319 }
7320 return(xmlExpHashGetEntry(ctxt, XML_EXP_OR, ret, tmp, NULL, 0, 0));
7321 case XML_EXP_COUNT: {
7322 int min, max;
7323
7324 if (sub->type == XML_EXP_COUNT) {
7325 /*
7326 * Try to see if the loop is completely subsumed
7327 */
7328 tmp = xmlExpExpDeriveInt(ctxt, exp->exp_left, sub->exp_left);
7329 if (tmp == NULL)
7330 return(NULL);
7331 if (tmp == forbiddenExp) {
7332 int mult;
7333
7334#ifdef DEBUG_DERIV
7335 printf("Count, Count inner don't subsume\n");
7336#endif
7337 mult = xmlExpDivide(ctxt, sub->exp_left, exp->exp_left,
7338 NULL, &tmp);
7339 if (mult <= 0) {
7340#ifdef DEBUG_DERIV
7341 printf("Count, Count not multiple => forbidden\n");
7342#endif
7343 return(forbiddenExp);
7344 }
7345 if (sub->exp_max == -1) {
7346 max = -1;
7347 if (exp->exp_max == -1) {
7348 if (exp->exp_min <= sub->exp_min * mult)
7349 min = 0;
7350 else
7351 min = exp->exp_min - sub->exp_min * mult;
7352 } else {
7353#ifdef DEBUG_DERIV
7354 printf("Count, Count finite can't subsume infinite\n");
7355#endif
7356 xmlExpFree(ctxt, tmp);
7357 return(forbiddenExp);
7358 }
7359 } else {
7360 if (exp->exp_max == -1) {
7361#ifdef DEBUG_DERIV
7362 printf("Infinite loop consume mult finite loop\n");
7363#endif
7364 if (exp->exp_min > sub->exp_min * mult) {
7365 max = -1;
7366 min = exp->exp_min - sub->exp_min * mult;
7367 } else {
7368 max = -1;
7369 min = 0;
7370 }
7371 } else {
7372 if (exp->exp_max < sub->exp_max * mult) {
7373#ifdef DEBUG_DERIV
7374 printf("loops max mult mismatch => forbidden\n");
7375#endif
7376 xmlExpFree(ctxt, tmp);
7377 return(forbiddenExp);
7378 }
7379 if (sub->exp_max * mult > exp->exp_min)
7380 min = 0;
7381 else
7382 min = exp->exp_min - sub->exp_max * mult;
7383 max = exp->exp_max - sub->exp_max * mult;
7384 }
7385 }
7386 } else if (!IS_NILLABLE(tmp)) {
7387 /*
7388 * TODO: loop here to try to grow if working on finite
7389 * blocks.
7390 */
7391#ifdef DEBUG_DERIV
7392 printf("Count, Count remain not nillable => forbidden\n");
7393#endif
7394 xmlExpFree(ctxt, tmp);
7395 return(forbiddenExp);
7396 } else if (sub->exp_max == -1) {
7397 if (exp->exp_max == -1) {
7398 if (exp->exp_min <= sub->exp_min) {
7399#ifdef DEBUG_DERIV
7400 printf("Infinite loops Okay => COUNT(0,Inf)\n");
7401#endif
7402 max = -1;
7403 min = 0;
7404 } else {
7405#ifdef DEBUG_DERIV
7406 printf("Infinite loops min => Count(X,Inf)\n");
7407#endif
7408 max = -1;
7409 min = exp->exp_min - sub->exp_min;
7410 }
7411 } else if (exp->exp_min > sub->exp_min) {
7412#ifdef DEBUG_DERIV
7413 printf("loops min mismatch 1 => forbidden ???\n");
7414#endif
7415 xmlExpFree(ctxt, tmp);
7416 return(forbiddenExp);
7417 } else {
7418 max = -1;
7419 min = 0;
7420 }
7421 } else {
7422 if (exp->exp_max == -1) {
7423#ifdef DEBUG_DERIV
7424 printf("Infinite loop consume finite loop\n");
7425#endif
7426 if (exp->exp_min > sub->exp_min) {
7427 max = -1;
7428 min = exp->exp_min - sub->exp_min;
7429 } else {
7430 max = -1;
7431 min = 0;
7432 }
7433 } else {
7434 if (exp->exp_max < sub->exp_max) {
7435#ifdef DEBUG_DERIV
7436 printf("loops max mismatch => forbidden\n");
7437#endif
7438 xmlExpFree(ctxt, tmp);
7439 return(forbiddenExp);
7440 }
7441 if (sub->exp_max > exp->exp_min)
7442 min = 0;
7443 else
7444 min = exp->exp_min - sub->exp_max;
7445 max = exp->exp_max - sub->exp_max;
7446 }
7447 }
7448#ifdef DEBUG_DERIV
7449 printf("loops match => SEQ(COUNT())\n");
7450#endif
7451 exp->exp_left->ref++;
7452 tmp2 = xmlExpHashGetEntry(ctxt, XML_EXP_COUNT, exp->exp_left,
7453 NULL, NULL, min, max);
7454 if (tmp2 == NULL) {
7455 return(NULL);
7456 }
7457 ret = xmlExpHashGetEntry(ctxt, XML_EXP_SEQ, tmp, tmp2,
7458 NULL, 0, 0);
7459 return(ret);
7460 }
7461 tmp = xmlExpExpDeriveInt(ctxt, exp->exp_left, sub);
7462 if (tmp == NULL)
7463 return(NULL);
7464 if (tmp == forbiddenExp) {
7465#ifdef DEBUG_DERIV
7466 printf("loop mismatch => forbidden\n");
7467#endif
7468 return(forbiddenExp);
7469 }
7470 if (exp->exp_min > 0)
7471 min = exp->exp_min - 1;
7472 else
7473 min = 0;
7474 if (exp->exp_max < 0)
7475 max = -1;
7476 else
7477 max = exp->exp_max - 1;
7478
7479#ifdef DEBUG_DERIV
7480 printf("loop match => SEQ(COUNT())\n");
7481#endif
7482 exp->exp_left->ref++;
7483 tmp2 = xmlExpHashGetEntry(ctxt, XML_EXP_COUNT, exp->exp_left,
7484 NULL, NULL, min, max);
7485 if (tmp2 == NULL)
7486 return(NULL);
7487 ret = xmlExpHashGetEntry(ctxt, XML_EXP_SEQ, tmp, tmp2,
7488 NULL, 0, 0);
7489 return(ret);
7490 }
7491 }
7492
Daniel Veillardccb4d412005-08-23 13:41:17 +00007493#ifdef DEBUG_DERIV
7494 printf("Fallback to derivative\n");
7495#endif
7496 if (IS_NILLABLE(sub)) {
7497 if (!(IS_NILLABLE(exp)))
7498 return(forbiddenExp);
7499 else
7500 ret = emptyExp;
7501 } else
7502 ret = NULL;
Daniel Veillard81a8ec62005-08-22 00:20:58 +00007503 /*
7504 * here the structured derivation made no progress so
7505 * we use the default token based derivation to force one more step
7506 */
7507 if (ctxt->tabSize == 0)
7508 ctxt->tabSize = 40;
7509
7510 tab = (const xmlChar **) xmlMalloc(ctxt->tabSize *
7511 sizeof(const xmlChar *));
7512 if (tab == NULL) {
7513 return(NULL);
7514 }
7515
Daniel Veillard81a8ec62005-08-22 00:20:58 +00007516 /*
7517 * collect all the strings accepted by the subexpression on input
7518 */
7519 len = xmlExpGetStartInt(ctxt, sub, tab, ctxt->tabSize, 0);
7520 while (len < 0) {
7521 const xmlChar **temp;
Rob Richards54a8f672005-10-07 02:33:00 +00007522 temp = (const xmlChar **) xmlRealloc((xmlChar **) tab, ctxt->tabSize * 2 *
Daniel Veillard81a8ec62005-08-22 00:20:58 +00007523 sizeof(const xmlChar *));
7524 if (temp == NULL) {
Rob Richards54a8f672005-10-07 02:33:00 +00007525 xmlFree((xmlChar **) tab);
Daniel Veillard81a8ec62005-08-22 00:20:58 +00007526 return(NULL);
7527 }
7528 tab = temp;
7529 ctxt->tabSize *= 2;
7530 len = xmlExpGetStartInt(ctxt, sub, tab, ctxt->tabSize, 0);
7531 }
Daniel Veillard81a8ec62005-08-22 00:20:58 +00007532 for (i = 0;i < len;i++) {
7533 tmp = xmlExpStringDeriveInt(ctxt, exp, tab[i]);
7534 if ((tmp == NULL) || (tmp == forbiddenExp)) {
7535 xmlExpFree(ctxt, ret);
Rob Richards54a8f672005-10-07 02:33:00 +00007536 xmlFree((xmlChar **) tab);
Daniel Veillard81a8ec62005-08-22 00:20:58 +00007537 return(tmp);
7538 }
7539 tmp2 = xmlExpStringDeriveInt(ctxt, sub, tab[i]);
7540 if ((tmp2 == NULL) || (tmp2 == forbiddenExp)) {
7541 xmlExpFree(ctxt, tmp);
7542 xmlExpFree(ctxt, ret);
Rob Richards54a8f672005-10-07 02:33:00 +00007543 xmlFree((xmlChar **) tab);
Daniel Veillard81a8ec62005-08-22 00:20:58 +00007544 return(tmp);
7545 }
7546 tmp3 = xmlExpExpDeriveInt(ctxt, tmp, tmp2);
7547 xmlExpFree(ctxt, tmp);
7548 xmlExpFree(ctxt, tmp2);
7549
7550 if ((tmp3 == NULL) || (tmp3 == forbiddenExp)) {
7551 xmlExpFree(ctxt, ret);
Rob Richards54a8f672005-10-07 02:33:00 +00007552 xmlFree((xmlChar **) tab);
Daniel Veillard81a8ec62005-08-22 00:20:58 +00007553 return(tmp3);
7554 }
7555
7556 if (ret == NULL)
7557 ret = tmp3;
7558 else {
7559 ret = xmlExpHashGetEntry(ctxt, XML_EXP_OR, ret, tmp3, NULL, 0, 0);
7560 if (ret == NULL) {
Rob Richards54a8f672005-10-07 02:33:00 +00007561 xmlFree((xmlChar **) tab);
Daniel Veillard81a8ec62005-08-22 00:20:58 +00007562 return(NULL);
7563 }
7564 }
7565 }
Rob Richards54a8f672005-10-07 02:33:00 +00007566 xmlFree((xmlChar **) tab);
Daniel Veillard81a8ec62005-08-22 00:20:58 +00007567 return(ret);
7568}
7569
7570/**
Daniel Veillard0090bd52005-08-22 14:43:43 +00007571 * xmlExpExpDerive:
7572 * @ctxt: the expressions context
7573 * @exp: the englobing expression
7574 * @sub: the subexpression
7575 *
7576 * Evaluates the expression resulting from @exp consuming a sub expression @sub
7577 * Based on algebraic derivation and sometimes direct Brzozowski derivation
7578 * it usually tatkes less than linear time and can handle expressions generating
7579 * infinite languages.
7580 *
7581 * Returns the resulting expression or NULL in case of internal error, the
7582 * result must be freed
7583 */
7584xmlExpNodePtr
7585xmlExpExpDerive(xmlExpCtxtPtr ctxt, xmlExpNodePtr exp, xmlExpNodePtr sub) {
7586 if ((exp == NULL) || (ctxt == NULL) || (sub == NULL))
7587 return(NULL);
7588
7589 /*
7590 * O(1) speedups
7591 */
7592 if (IS_NILLABLE(sub) && (!IS_NILLABLE(exp))) {
7593#ifdef DEBUG_DERIV
7594 printf("Sub nillable and not exp : can't subsume\n");
7595#endif
7596 return(forbiddenExp);
7597 }
7598 if (xmlExpCheckCard(exp, sub) == 0) {
7599#ifdef DEBUG_DERIV
7600 printf("sub generate longuer sequances than exp : can't subsume\n");
7601#endif
7602 return(forbiddenExp);
7603 }
7604 return(xmlExpExpDeriveInt(ctxt, exp, sub));
7605}
7606
7607/**
Daniel Veillard81a8ec62005-08-22 00:20:58 +00007608 * xmlExpSubsume:
7609 * @ctxt: the expressions context
7610 * @exp: the englobing expression
7611 * @sub: the subexpression
7612 *
7613 * Check whether @exp accepts all the languages accexpted by @sub
7614 * the input being a subexpression.
7615 *
7616 * Returns 1 if true 0 if false and -1 in case of failure.
7617 */
7618int
7619xmlExpSubsume(xmlExpCtxtPtr ctxt, xmlExpNodePtr exp, xmlExpNodePtr sub) {
7620 xmlExpNodePtr tmp;
7621
7622 if ((exp == NULL) || (ctxt == NULL) || (sub == NULL))
7623 return(-1);
7624
7625 /*
7626 * TODO: speedup by checking the language of sub is a subset of the
7627 * language of exp
7628 */
7629 /*
7630 * O(1) speedups
7631 */
7632 if (IS_NILLABLE(sub) && (!IS_NILLABLE(exp))) {
7633#ifdef DEBUG_DERIV
7634 printf("Sub nillable and not exp : can't subsume\n");
7635#endif
7636 return(0);
7637 }
7638 if (xmlExpCheckCard(exp, sub) == 0) {
7639#ifdef DEBUG_DERIV
7640 printf("sub generate longuer sequances than exp : can't subsume\n");
7641#endif
7642 return(0);
7643 }
7644 tmp = xmlExpExpDeriveInt(ctxt, exp, sub);
7645#ifdef DEBUG_DERIV
7646 printf("Result derivation :\n");
7647 PRINT_EXP(tmp);
7648#endif
7649 if (tmp == NULL)
7650 return(-1);
7651 if (tmp == forbiddenExp)
7652 return(0);
7653 if (tmp == emptyExp)
7654 return(1);
7655 if ((tmp != NULL) && (IS_NILLABLE(tmp))) {
7656 xmlExpFree(ctxt, tmp);
7657 return(1);
7658 }
7659 xmlExpFree(ctxt, tmp);
7660 return(0);
7661}
Daniel Veillard465a0002005-08-22 12:07:04 +00007662
7663/************************************************************************
7664 * *
7665 * Parsing expression *
7666 * *
7667 ************************************************************************/
7668
7669static xmlExpNodePtr xmlExpParseExpr(xmlExpCtxtPtr ctxt);
7670
7671#undef CUR
7672#define CUR (*ctxt->cur)
7673#undef NEXT
7674#define NEXT ctxt->cur++;
7675#undef IS_BLANK
7676#define IS_BLANK(c) ((c == ' ') || (c == '\n') || (c == '\r') || (c == '\t'))
7677#define SKIP_BLANKS while (IS_BLANK(*ctxt->cur)) ctxt->cur++;
7678
7679static int
7680xmlExpParseNumber(xmlExpCtxtPtr ctxt) {
7681 int ret = 0;
7682
7683 SKIP_BLANKS
7684 if (CUR == '*') {
7685 NEXT
7686 return(-1);
7687 }
7688 if ((CUR < '0') || (CUR > '9'))
7689 return(-1);
7690 while ((CUR >= '0') && (CUR <= '9')) {
7691 ret = ret * 10 + (CUR - '0');
7692 NEXT
7693 }
7694 return(ret);
7695}
7696
7697static xmlExpNodePtr
7698xmlExpParseOr(xmlExpCtxtPtr ctxt) {
7699 const char *base;
7700 xmlExpNodePtr ret;
7701 const xmlChar *val;
7702
7703 SKIP_BLANKS
7704 base = ctxt->cur;
7705 if (*ctxt->cur == '(') {
7706 NEXT
7707 ret = xmlExpParseExpr(ctxt);
7708 SKIP_BLANKS
7709 if (*ctxt->cur != ')') {
7710 fprintf(stderr, "unbalanced '(' : %s\n", base);
7711 xmlExpFree(ctxt, ret);
7712 return(NULL);
7713 }
7714 NEXT;
7715 SKIP_BLANKS
7716 goto parse_quantifier;
7717 }
7718 while ((CUR != 0) && (!(IS_BLANK(CUR))) && (CUR != '(') &&
7719 (CUR != ')') && (CUR != '|') && (CUR != ',') && (CUR != '{') &&
7720 (CUR != '*') && (CUR != '+') && (CUR != '?') && (CUR != '}'))
7721 NEXT;
7722 val = xmlDictLookup(ctxt->dict, BAD_CAST base, ctxt->cur - base);
7723 if (val == NULL)
7724 return(NULL);
7725 ret = xmlExpHashGetEntry(ctxt, XML_EXP_ATOM, NULL, NULL, val, 0, 0);
7726 if (ret == NULL)
7727 return(NULL);
7728 SKIP_BLANKS
7729parse_quantifier:
7730 if (CUR == '{') {
7731 int min, max;
7732
7733 NEXT
7734 min = xmlExpParseNumber(ctxt);
7735 if (min < 0) {
7736 xmlExpFree(ctxt, ret);
7737 return(NULL);
7738 }
7739 SKIP_BLANKS
7740 if (CUR == ',') {
7741 NEXT
7742 max = xmlExpParseNumber(ctxt);
7743 SKIP_BLANKS
7744 } else
7745 max = min;
7746 if (CUR != '}') {
7747 xmlExpFree(ctxt, ret);
7748 return(NULL);
7749 }
7750 NEXT
7751 ret = xmlExpHashGetEntry(ctxt, XML_EXP_COUNT, ret, NULL, NULL,
7752 min, max);
7753 SKIP_BLANKS
7754 } else if (CUR == '?') {
7755 NEXT
7756 ret = xmlExpHashGetEntry(ctxt, XML_EXP_COUNT, ret, NULL, NULL,
7757 0, 1);
7758 SKIP_BLANKS
7759 } else if (CUR == '+') {
7760 NEXT
7761 ret = xmlExpHashGetEntry(ctxt, XML_EXP_COUNT, ret, NULL, NULL,
7762 1, -1);
7763 SKIP_BLANKS
7764 } else if (CUR == '*') {
7765 NEXT
7766 ret = xmlExpHashGetEntry(ctxt, XML_EXP_COUNT, ret, NULL, NULL,
7767 0, -1);
7768 SKIP_BLANKS
7769 }
7770 return(ret);
7771}
7772
7773
7774static xmlExpNodePtr
7775xmlExpParseSeq(xmlExpCtxtPtr ctxt) {
7776 xmlExpNodePtr ret, right;
7777
7778 ret = xmlExpParseOr(ctxt);
7779 SKIP_BLANKS
7780 while (CUR == '|') {
7781 NEXT
7782 right = xmlExpParseOr(ctxt);
7783 if (right == NULL) {
7784 xmlExpFree(ctxt, ret);
7785 return(NULL);
7786 }
7787 ret = xmlExpHashGetEntry(ctxt, XML_EXP_OR, ret, right, NULL, 0, 0);
7788 if (ret == NULL)
7789 return(NULL);
7790 }
7791 return(ret);
7792}
7793
7794static xmlExpNodePtr
7795xmlExpParseExpr(xmlExpCtxtPtr ctxt) {
7796 xmlExpNodePtr ret, right;
7797
7798 ret = xmlExpParseSeq(ctxt);
7799 SKIP_BLANKS
7800 while (CUR == ',') {
7801 NEXT
7802 right = xmlExpParseSeq(ctxt);
7803 if (right == NULL) {
7804 xmlExpFree(ctxt, ret);
7805 return(NULL);
7806 }
7807 ret = xmlExpHashGetEntry(ctxt, XML_EXP_SEQ, ret, right, NULL, 0, 0);
7808 if (ret == NULL)
7809 return(NULL);
7810 }
7811 return(ret);
7812}
7813
7814/**
7815 * xmlExpParse:
7816 * @ctxt: the expressions context
7817 * @expr: the 0 terminated string
7818 *
7819 * Minimal parser for regexps, it understand the following constructs
7820 * - string terminals
7821 * - choice operator |
7822 * - sequence operator ,
7823 * - subexpressions (...)
7824 * - usual cardinality operators + * and ?
7825 * - finite sequences { min, max }
7826 * - infinite sequences { min, * }
7827 * There is minimal checkings made especially no checking on strings values
7828 *
7829 * Returns a new expression or NULL in case of failure
7830 */
7831xmlExpNodePtr
7832xmlExpParse(xmlExpCtxtPtr ctxt, const char *expr) {
7833 xmlExpNodePtr ret;
7834
7835 ctxt->expr = expr;
7836 ctxt->cur = expr;
7837
7838 ret = xmlExpParseExpr(ctxt);
7839 SKIP_BLANKS
7840 if (*ctxt->cur != 0) {
7841 xmlExpFree(ctxt, ret);
7842 return(NULL);
7843 }
7844 return(ret);
7845}
7846
7847static void
7848xmlExpDumpInt(xmlBufferPtr buf, xmlExpNodePtr expr, int glob) {
7849 xmlExpNodePtr c;
7850
7851 if (expr == NULL) return;
7852 if (glob) xmlBufferWriteChar(buf, "(");
7853 switch (expr->type) {
7854 case XML_EXP_EMPTY:
7855 xmlBufferWriteChar(buf, "empty");
7856 break;
7857 case XML_EXP_FORBID:
7858 xmlBufferWriteChar(buf, "forbidden");
7859 break;
7860 case XML_EXP_ATOM:
7861 xmlBufferWriteCHAR(buf, expr->exp_str);
7862 break;
7863 case XML_EXP_SEQ:
7864 c = expr->exp_left;
7865 if ((c->type == XML_EXP_SEQ) || (c->type == XML_EXP_OR))
7866 xmlExpDumpInt(buf, c, 1);
7867 else
7868 xmlExpDumpInt(buf, c, 0);
7869 xmlBufferWriteChar(buf, " , ");
7870 c = expr->exp_right;
7871 if ((c->type == XML_EXP_SEQ) || (c->type == XML_EXP_OR))
7872 xmlExpDumpInt(buf, c, 1);
7873 else
7874 xmlExpDumpInt(buf, c, 0);
7875 break;
7876 case XML_EXP_OR:
7877 c = expr->exp_left;
7878 if ((c->type == XML_EXP_SEQ) || (c->type == XML_EXP_OR))
7879 xmlExpDumpInt(buf, c, 1);
7880 else
7881 xmlExpDumpInt(buf, c, 0);
7882 xmlBufferWriteChar(buf, " | ");
7883 c = expr->exp_right;
7884 if ((c->type == XML_EXP_SEQ) || (c->type == XML_EXP_OR))
7885 xmlExpDumpInt(buf, c, 1);
7886 else
7887 xmlExpDumpInt(buf, c, 0);
7888 break;
7889 case XML_EXP_COUNT: {
7890 char rep[40];
7891
7892 c = expr->exp_left;
7893 if ((c->type == XML_EXP_SEQ) || (c->type == XML_EXP_OR))
7894 xmlExpDumpInt(buf, c, 1);
7895 else
7896 xmlExpDumpInt(buf, c, 0);
7897 if ((expr->exp_min == 0) && (expr->exp_max == 1)) {
7898 rep[0] = '?';
7899 rep[1] = 0;
7900 } else if ((expr->exp_min == 0) && (expr->exp_max == -1)) {
7901 rep[0] = '*';
7902 rep[1] = 0;
7903 } else if ((expr->exp_min == 1) && (expr->exp_max == -1)) {
7904 rep[0] = '+';
7905 rep[1] = 0;
7906 } else if (expr->exp_max == expr->exp_min) {
7907 snprintf(rep, 39, "{%d}", expr->exp_min);
7908 } else if (expr->exp_max < 0) {
7909 snprintf(rep, 39, "{%d,inf}", expr->exp_min);
7910 } else {
7911 snprintf(rep, 39, "{%d,%d}", expr->exp_min, expr->exp_max);
7912 }
7913 rep[39] = 0;
7914 xmlBufferWriteChar(buf, rep);
7915 break;
7916 }
7917 default:
7918 fprintf(stderr, "Error in tree\n");
7919 }
7920 if (glob)
7921 xmlBufferWriteChar(buf, ")");
7922}
7923/**
7924 * xmlExpDump:
7925 * @buf: a buffer to receive the output
7926 * @expr: the compiled expression
7927 *
7928 * Serialize the expression as compiled to the buffer
7929 */
7930void
Daniel Veillard5eee7672005-08-22 21:22:27 +00007931xmlExpDump(xmlBufferPtr buf, xmlExpNodePtr expr) {
7932 if ((buf == NULL) || (expr == NULL))
Daniel Veillard465a0002005-08-22 12:07:04 +00007933 return;
Daniel Veillard5eee7672005-08-22 21:22:27 +00007934 xmlExpDumpInt(buf, expr, 0);
Daniel Veillard465a0002005-08-22 12:07:04 +00007935}
7936
7937/**
7938 * xmlExpMaxToken:
7939 * @expr: a compiled expression
7940 *
7941 * Indicate the maximum number of input a expression can accept
7942 *
7943 * Returns the maximum length or -1 in case of error
7944 */
7945int
7946xmlExpMaxToken(xmlExpNodePtr expr) {
7947 if (expr == NULL)
7948 return(-1);
7949 return(expr->c_max);
7950}
7951
7952/**
7953 * xmlExpCtxtNbNodes:
7954 * @ctxt: an expression context
7955 *
7956 * Debugging facility provides the number of allocated nodes at a that point
7957 *
7958 * Returns the number of nodes in use or -1 in case of error
7959 */
7960int
7961xmlExpCtxtNbNodes(xmlExpCtxtPtr ctxt) {
7962 if (ctxt == NULL)
7963 return(-1);
7964 return(ctxt->nb_nodes);
7965}
7966
7967/**
7968 * xmlExpCtxtNbCons:
7969 * @ctxt: an expression context
7970 *
7971 * Debugging facility provides the number of allocated nodes over lifetime
7972 *
7973 * Returns the number of nodes ever allocated or -1 in case of error
7974 */
7975int
7976xmlExpCtxtNbCons(xmlExpCtxtPtr ctxt) {
7977 if (ctxt == NULL)
7978 return(-1);
7979 return(ctxt->nb_cons);
7980}
7981
Daniel Veillard81a8ec62005-08-22 00:20:58 +00007982#endif /* LIBXML_EXPR_ENABLED */
Daniel Veillard5d4644e2005-04-01 13:11:58 +00007983#define bottom_xmlregexp
7984#include "elfgcchack.h"
Daniel Veillard4255d502002-04-16 15:50:10 +00007985#endif /* LIBXML_REGEXP_ENABLED */