blob: 54eb95763654ce6e6ac583b6d827e80211b25df0 [file] [log] [blame]
Daniel Veillard4255d502002-04-16 15:50:10 +00001/*
2 * regexp.c: generic and extensible Regular Expression engine
3 *
4 * Basically designed with the purpose of compiling regexps for
5 * the variety of validation/shemas mechanisms now available in
William M. Brackddf71d62004-05-06 04:17:26 +00006 * XML related specifications these include:
Daniel Veillard4255d502002-04-16 15:50:10 +00007 * - XML-1.0 DTD validation
8 * - XML Schemas structure part 1
9 * - XML Schemas Datatypes part 2 especially Appendix F
10 * - RELAX-NG/TREX i.e. the counter proposal
11 *
12 * See Copyright for the status of this software.
13 *
14 * Daniel Veillard <veillard@redhat.com>
15 */
16
17#define IN_LIBXML
18#include "libxml.h"
19
20#ifdef LIBXML_REGEXP_ENABLED
21
Daniel Veillardcee2b3a2005-01-25 00:22:52 +000022/* #define DEBUG_ERR */
Daniel Veillardfc0b6f62005-01-09 17:48:02 +000023
Daniel Veillard4255d502002-04-16 15:50:10 +000024#include <stdio.h>
25#include <string.h>
Daniel Veillardebe48c62003-12-03 12:12:27 +000026#ifdef HAVE_LIMITS_H
27#include <limits.h>
28#endif
29
Daniel Veillard4255d502002-04-16 15:50:10 +000030#include <libxml/tree.h>
31#include <libxml/parserInternals.h>
32#include <libxml/xmlregexp.h>
33#include <libxml/xmlautomata.h>
34#include <libxml/xmlunicode.h>
35
Daniel Veillardebe48c62003-12-03 12:12:27 +000036#ifndef INT_MAX
37#define INT_MAX 123456789 /* easy to flag and big enough for our needs */
38#endif
39
Daniel Veillardc0826a72004-08-10 14:17:33 +000040/* #define DEBUG_REGEXP_GRAPH */
Daniel Veillard10752282005-08-08 13:05:13 +000041/* #define DEBUG_REGEXP_EXEC */
Daniel Veillard4255d502002-04-16 15:50:10 +000042/* #define DEBUG_PUSH */
Daniel Veillard23e73572002-09-19 19:56:43 +000043/* #define DEBUG_COMPACTION */
Daniel Veillard4255d502002-04-16 15:50:10 +000044
Daniel Veillard567a45b2005-10-18 19:11:55 +000045#define MAX_PUSH 10000000
Daniel Veillard94cc1032005-09-15 13:09:00 +000046
Daniel Veillardff46a042003-10-08 08:53:17 +000047#define ERROR(str) \
48 ctxt->error = XML_REGEXP_COMPILE_ERROR; \
49 xmlRegexpErrCompile(ctxt, str);
Daniel Veillard4255d502002-04-16 15:50:10 +000050#define NEXT ctxt->cur++
51#define CUR (*(ctxt->cur))
52#define NXT(index) (ctxt->cur[index])
53
54#define CUR_SCHAR(s, l) xmlStringCurrentChar(NULL, s, &l)
55#define NEXTL(l) ctxt->cur += l;
Daniel Veillardc0826a72004-08-10 14:17:33 +000056#define XML_REG_STRING_SEPARATOR '|'
William M. Bracka9cbf282007-03-21 13:16:33 +000057/*
58 * Need PREV to check on a '-' within a Character Group. May only be used
59 * when it's guaranteed that cur is not at the beginning of ctxt->string!
60 */
61#define PREV (ctxt->cur[-1])
Daniel Veillard4255d502002-04-16 15:50:10 +000062
Daniel Veillarde19fc232002-04-22 16:01:24 +000063/**
64 * TODO:
65 *
66 * macro to flag unimplemented blocks
67 */
68#define TODO \
69 xmlGenericError(xmlGenericErrorContext, \
70 "Unimplemented block at %s:%d\n", \
71 __FILE__, __LINE__);
72
Daniel Veillard4255d502002-04-16 15:50:10 +000073/************************************************************************
74 * *
75 * Datatypes and structures *
76 * *
77 ************************************************************************/
78
Daniel Veillardfc011b72006-02-12 19:14:15 +000079/*
80 * Note: the order of the enums below is significant, do not shuffle
81 */
Daniel Veillard4255d502002-04-16 15:50:10 +000082typedef enum {
83 XML_REGEXP_EPSILON = 1,
84 XML_REGEXP_CHARVAL,
85 XML_REGEXP_RANGES,
Daniel Veillard567a45b2005-10-18 19:11:55 +000086 XML_REGEXP_SUBREG, /* used for () sub regexps */
Daniel Veillard4255d502002-04-16 15:50:10 +000087 XML_REGEXP_STRING,
88 XML_REGEXP_ANYCHAR, /* . */
89 XML_REGEXP_ANYSPACE, /* \s */
90 XML_REGEXP_NOTSPACE, /* \S */
91 XML_REGEXP_INITNAME, /* \l */
Daniel Veillard567a45b2005-10-18 19:11:55 +000092 XML_REGEXP_NOTINITNAME, /* \L */
Daniel Veillard4255d502002-04-16 15:50:10 +000093 XML_REGEXP_NAMECHAR, /* \c */
94 XML_REGEXP_NOTNAMECHAR, /* \C */
95 XML_REGEXP_DECIMAL, /* \d */
Daniel Veillard567a45b2005-10-18 19:11:55 +000096 XML_REGEXP_NOTDECIMAL, /* \D */
Daniel Veillard4255d502002-04-16 15:50:10 +000097 XML_REGEXP_REALCHAR, /* \w */
Daniel Veillard567a45b2005-10-18 19:11:55 +000098 XML_REGEXP_NOTREALCHAR, /* \W */
99 XML_REGEXP_LETTER = 100,
Daniel Veillard4255d502002-04-16 15:50:10 +0000100 XML_REGEXP_LETTER_UPPERCASE,
101 XML_REGEXP_LETTER_LOWERCASE,
102 XML_REGEXP_LETTER_TITLECASE,
103 XML_REGEXP_LETTER_MODIFIER,
104 XML_REGEXP_LETTER_OTHERS,
105 XML_REGEXP_MARK,
106 XML_REGEXP_MARK_NONSPACING,
107 XML_REGEXP_MARK_SPACECOMBINING,
108 XML_REGEXP_MARK_ENCLOSING,
109 XML_REGEXP_NUMBER,
110 XML_REGEXP_NUMBER_DECIMAL,
111 XML_REGEXP_NUMBER_LETTER,
112 XML_REGEXP_NUMBER_OTHERS,
113 XML_REGEXP_PUNCT,
114 XML_REGEXP_PUNCT_CONNECTOR,
115 XML_REGEXP_PUNCT_DASH,
116 XML_REGEXP_PUNCT_OPEN,
117 XML_REGEXP_PUNCT_CLOSE,
118 XML_REGEXP_PUNCT_INITQUOTE,
119 XML_REGEXP_PUNCT_FINQUOTE,
120 XML_REGEXP_PUNCT_OTHERS,
121 XML_REGEXP_SEPAR,
122 XML_REGEXP_SEPAR_SPACE,
123 XML_REGEXP_SEPAR_LINE,
124 XML_REGEXP_SEPAR_PARA,
125 XML_REGEXP_SYMBOL,
126 XML_REGEXP_SYMBOL_MATH,
127 XML_REGEXP_SYMBOL_CURRENCY,
128 XML_REGEXP_SYMBOL_MODIFIER,
129 XML_REGEXP_SYMBOL_OTHERS,
130 XML_REGEXP_OTHER,
131 XML_REGEXP_OTHER_CONTROL,
132 XML_REGEXP_OTHER_FORMAT,
133 XML_REGEXP_OTHER_PRIVATE,
134 XML_REGEXP_OTHER_NA,
135 XML_REGEXP_BLOCK_NAME
136} xmlRegAtomType;
137
138typedef enum {
139 XML_REGEXP_QUANT_EPSILON = 1,
140 XML_REGEXP_QUANT_ONCE,
141 XML_REGEXP_QUANT_OPT,
142 XML_REGEXP_QUANT_MULT,
143 XML_REGEXP_QUANT_PLUS,
Daniel Veillard7646b182002-04-20 06:41:40 +0000144 XML_REGEXP_QUANT_ONCEONLY,
145 XML_REGEXP_QUANT_ALL,
Daniel Veillard4255d502002-04-16 15:50:10 +0000146 XML_REGEXP_QUANT_RANGE
147} xmlRegQuantType;
148
149typedef enum {
150 XML_REGEXP_START_STATE = 1,
151 XML_REGEXP_FINAL_STATE,
Daniel Veillardcc026dc2005-01-12 13:21:17 +0000152 XML_REGEXP_TRANS_STATE,
Daniel Veillard0e05f4c2006-11-01 15:33:04 +0000153 XML_REGEXP_SINK_STATE,
154 XML_REGEXP_UNREACH_STATE
Daniel Veillard4255d502002-04-16 15:50:10 +0000155} xmlRegStateType;
156
157typedef enum {
158 XML_REGEXP_MARK_NORMAL = 0,
159 XML_REGEXP_MARK_START,
160 XML_REGEXP_MARK_VISITED
161} xmlRegMarkedType;
162
163typedef struct _xmlRegRange xmlRegRange;
164typedef xmlRegRange *xmlRegRangePtr;
165
166struct _xmlRegRange {
Daniel Veillardf8b9de32003-11-24 14:27:26 +0000167 int neg; /* 0 normal, 1 not, 2 exclude */
Daniel Veillard4255d502002-04-16 15:50:10 +0000168 xmlRegAtomType type;
169 int start;
170 int end;
171 xmlChar *blockName;
172};
173
174typedef struct _xmlRegAtom xmlRegAtom;
175typedef xmlRegAtom *xmlRegAtomPtr;
176
177typedef struct _xmlAutomataState xmlRegState;
178typedef xmlRegState *xmlRegStatePtr;
179
180struct _xmlRegAtom {
181 int no;
182 xmlRegAtomType type;
183 xmlRegQuantType quant;
184 int min;
185 int max;
186
187 void *valuep;
Daniel Veillarda646cfd2002-09-17 21:50:03 +0000188 void *valuep2;
Daniel Veillard4255d502002-04-16 15:50:10 +0000189 int neg;
190 int codepoint;
191 xmlRegStatePtr start;
Daniel Veillard76d59b62007-08-22 16:29:21 +0000192 xmlRegStatePtr start0;
Daniel Veillard4255d502002-04-16 15:50:10 +0000193 xmlRegStatePtr stop;
194 int maxRanges;
195 int nbRanges;
196 xmlRegRangePtr *ranges;
197 void *data;
198};
199
200typedef struct _xmlRegCounter xmlRegCounter;
201typedef xmlRegCounter *xmlRegCounterPtr;
202
203struct _xmlRegCounter {
204 int min;
205 int max;
206};
207
208typedef struct _xmlRegTrans xmlRegTrans;
209typedef xmlRegTrans *xmlRegTransPtr;
210
211struct _xmlRegTrans {
212 xmlRegAtomPtr atom;
213 int to;
214 int counter;
215 int count;
Daniel Veillard567a45b2005-10-18 19:11:55 +0000216 int nd;
Daniel Veillard4255d502002-04-16 15:50:10 +0000217};
218
219struct _xmlAutomataState {
220 xmlRegStateType type;
221 xmlRegMarkedType mark;
Daniel Veillard23e73572002-09-19 19:56:43 +0000222 xmlRegMarkedType reached;
Daniel Veillard4255d502002-04-16 15:50:10 +0000223 int no;
Daniel Veillard4255d502002-04-16 15:50:10 +0000224 int maxTrans;
225 int nbTrans;
226 xmlRegTrans *trans;
Daniel Veillarddb68b742005-07-30 13:18:24 +0000227 /* knowing states ponting to us can speed things up */
228 int maxTransTo;
229 int nbTransTo;
230 int *transTo;
Daniel Veillard4255d502002-04-16 15:50:10 +0000231};
232
233typedef struct _xmlAutomata xmlRegParserCtxt;
234typedef xmlRegParserCtxt *xmlRegParserCtxtPtr;
235
236struct _xmlAutomata {
237 xmlChar *string;
238 xmlChar *cur;
239
240 int error;
241 int neg;
242
243 xmlRegStatePtr start;
244 xmlRegStatePtr end;
245 xmlRegStatePtr state;
246
247 xmlRegAtomPtr atom;
248
249 int maxAtoms;
250 int nbAtoms;
251 xmlRegAtomPtr *atoms;
252
253 int maxStates;
254 int nbStates;
255 xmlRegStatePtr *states;
256
257 int maxCounters;
258 int nbCounters;
259 xmlRegCounter *counters;
Daniel Veillarde19fc232002-04-22 16:01:24 +0000260
261 int determinist;
Daniel Veillard6e65e152005-08-09 11:09:52 +0000262 int negs;
Daniel Veillard4255d502002-04-16 15:50:10 +0000263};
264
265struct _xmlRegexp {
266 xmlChar *string;
267 int nbStates;
268 xmlRegStatePtr *states;
269 int nbAtoms;
270 xmlRegAtomPtr *atoms;
271 int nbCounters;
272 xmlRegCounter *counters;
Daniel Veillarde19fc232002-04-22 16:01:24 +0000273 int determinist;
Daniel Veillard23e73572002-09-19 19:56:43 +0000274 /*
275 * That's the compact form for determinists automatas
276 */
277 int nbstates;
278 int *compact;
Daniel Veillard118aed72002-09-24 14:13:13 +0000279 void **transdata;
Daniel Veillard23e73572002-09-19 19:56:43 +0000280 int nbstrings;
281 xmlChar **stringMap;
Daniel Veillard4255d502002-04-16 15:50:10 +0000282};
283
284typedef struct _xmlRegExecRollback xmlRegExecRollback;
285typedef xmlRegExecRollback *xmlRegExecRollbackPtr;
286
287struct _xmlRegExecRollback {
288 xmlRegStatePtr state;/* the current state */
289 int index; /* the index in the input stack */
290 int nextbranch; /* the next transition to explore in that state */
William M. Brackddf71d62004-05-06 04:17:26 +0000291 int *counts; /* save the automata state if it has some */
Daniel Veillard4255d502002-04-16 15:50:10 +0000292};
293
294typedef struct _xmlRegInputToken xmlRegInputToken;
295typedef xmlRegInputToken *xmlRegInputTokenPtr;
296
297struct _xmlRegInputToken {
298 xmlChar *value;
299 void *data;
300};
301
302struct _xmlRegExecCtxt {
303 int status; /* execution status != 0 indicate an error */
William M. Brackddf71d62004-05-06 04:17:26 +0000304 int determinist; /* did we find an indeterministic behaviour */
Daniel Veillard4255d502002-04-16 15:50:10 +0000305 xmlRegexpPtr comp; /* the compiled regexp */
306 xmlRegExecCallbacks callback;
307 void *data;
308
309 xmlRegStatePtr state;/* the current state */
310 int transno; /* the current transition on that state */
William M. Brackddf71d62004-05-06 04:17:26 +0000311 int transcount; /* the number of chars in char counted transitions */
Daniel Veillard4255d502002-04-16 15:50:10 +0000312
313 /*
314 * A stack of rollback states
315 */
316 int maxRollbacks;
317 int nbRollbacks;
318 xmlRegExecRollback *rollbacks;
319
320 /*
321 * The state of the automata if any
322 */
323 int *counts;
324
325 /*
326 * The input stack
327 */
328 int inputStackMax;
329 int inputStackNr;
330 int index;
331 int *charStack;
332 const xmlChar *inputString; /* when operating on characters */
333 xmlRegInputTokenPtr inputStack;/* when operating on strings */
334
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +0000335 /*
336 * error handling
337 */
338 int errStateNo; /* the error state number */
339 xmlRegStatePtr errState; /* the error state */
340 xmlChar *errString; /* the string raising the error */
341 int *errCounts; /* counters at the error state */
Daniel Veillard94cc1032005-09-15 13:09:00 +0000342 int nbPush;
Daniel Veillard4255d502002-04-16 15:50:10 +0000343};
344
Daniel Veillard441bc322002-04-20 17:38:48 +0000345#define REGEXP_ALL_COUNTER 0x123456
346#define REGEXP_ALL_LAX_COUNTER 0x123457
Daniel Veillard7646b182002-04-20 06:41:40 +0000347
Daniel Veillard4255d502002-04-16 15:50:10 +0000348static void xmlFAParseRegExp(xmlRegParserCtxtPtr ctxt, int top);
Daniel Veillard23e73572002-09-19 19:56:43 +0000349static void xmlRegFreeState(xmlRegStatePtr state);
350static void xmlRegFreeAtom(xmlRegAtomPtr atom);
Daniel Veillard9efc4762005-07-19 14:33:55 +0000351static int xmlRegStrEqualWildcard(const xmlChar *expStr, const xmlChar *valStr);
Daniel Veillard567a45b2005-10-18 19:11:55 +0000352static int xmlRegCheckCharacter(xmlRegAtomPtr atom, int codepoint);
353static int xmlRegCheckCharacterRange(xmlRegAtomType type, int codepoint,
354 int neg, int start, int end, const xmlChar *blockName);
Daniel Veillard4255d502002-04-16 15:50:10 +0000355
356/************************************************************************
Daniel Veillardff46a042003-10-08 08:53:17 +0000357 * *
358 * Regexp memory error handler *
359 * *
360 ************************************************************************/
361/**
362 * xmlRegexpErrMemory:
William M. Brackddf71d62004-05-06 04:17:26 +0000363 * @extra: extra information
Daniel Veillardff46a042003-10-08 08:53:17 +0000364 *
365 * Handle an out of memory condition
366 */
367static void
368xmlRegexpErrMemory(xmlRegParserCtxtPtr ctxt, const char *extra)
369{
370 const char *regexp = NULL;
371 if (ctxt != NULL) {
372 regexp = (const char *) ctxt->string;
373 ctxt->error = XML_ERR_NO_MEMORY;
374 }
Daniel Veillard659e71e2003-10-10 14:10:40 +0000375 __xmlRaiseError(NULL, NULL, NULL, NULL, NULL, XML_FROM_REGEXP,
Daniel Veillardff46a042003-10-08 08:53:17 +0000376 XML_ERR_NO_MEMORY, XML_ERR_FATAL, NULL, 0, extra,
377 regexp, NULL, 0, 0,
378 "Memory allocation failed : %s\n", extra);
379}
380
381/**
382 * xmlRegexpErrCompile:
William M. Brackddf71d62004-05-06 04:17:26 +0000383 * @extra: extra information
Daniel Veillardff46a042003-10-08 08:53:17 +0000384 *
William M. Brackddf71d62004-05-06 04:17:26 +0000385 * Handle a compilation failure
Daniel Veillardff46a042003-10-08 08:53:17 +0000386 */
387static void
388xmlRegexpErrCompile(xmlRegParserCtxtPtr ctxt, const char *extra)
389{
390 const char *regexp = NULL;
391 int idx = 0;
392
393 if (ctxt != NULL) {
394 regexp = (const char *) ctxt->string;
395 idx = ctxt->cur - ctxt->string;
396 ctxt->error = XML_REGEXP_COMPILE_ERROR;
397 }
Daniel Veillard659e71e2003-10-10 14:10:40 +0000398 __xmlRaiseError(NULL, NULL, NULL, NULL, NULL, XML_FROM_REGEXP,
Daniel Veillardff46a042003-10-08 08:53:17 +0000399 XML_REGEXP_COMPILE_ERROR, XML_ERR_FATAL, NULL, 0, extra,
400 regexp, NULL, idx, 0,
401 "failed to compile: %s\n", extra);
402}
403
404/************************************************************************
Daniel Veillard4255d502002-04-16 15:50:10 +0000405 * *
406 * Allocation/Deallocation *
407 * *
408 ************************************************************************/
409
Daniel Veillard23e73572002-09-19 19:56:43 +0000410static int xmlFAComputesDeterminism(xmlRegParserCtxtPtr ctxt);
Daniel Veillard4255d502002-04-16 15:50:10 +0000411/**
412 * xmlRegEpxFromParse:
413 * @ctxt: the parser context used to build it
414 *
William M. Brackddf71d62004-05-06 04:17:26 +0000415 * Allocate a new regexp and fill it with the result from the parser
Daniel Veillard4255d502002-04-16 15:50:10 +0000416 *
417 * Returns the new regexp or NULL in case of error
418 */
419static xmlRegexpPtr
420xmlRegEpxFromParse(xmlRegParserCtxtPtr ctxt) {
421 xmlRegexpPtr ret;
422
423 ret = (xmlRegexpPtr) xmlMalloc(sizeof(xmlRegexp));
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000424 if (ret == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +0000425 xmlRegexpErrMemory(ctxt, "compiling regexp");
Daniel Veillard4255d502002-04-16 15:50:10 +0000426 return(NULL);
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000427 }
Daniel Veillard4255d502002-04-16 15:50:10 +0000428 memset(ret, 0, sizeof(xmlRegexp));
429 ret->string = ctxt->string;
Daniel Veillard4255d502002-04-16 15:50:10 +0000430 ret->nbStates = ctxt->nbStates;
Daniel Veillard4255d502002-04-16 15:50:10 +0000431 ret->states = ctxt->states;
Daniel Veillard4255d502002-04-16 15:50:10 +0000432 ret->nbAtoms = ctxt->nbAtoms;
Daniel Veillard4255d502002-04-16 15:50:10 +0000433 ret->atoms = ctxt->atoms;
Daniel Veillard4255d502002-04-16 15:50:10 +0000434 ret->nbCounters = ctxt->nbCounters;
Daniel Veillard4255d502002-04-16 15:50:10 +0000435 ret->counters = ctxt->counters;
Daniel Veillarde19fc232002-04-22 16:01:24 +0000436 ret->determinist = ctxt->determinist;
Daniel Veillard567a45b2005-10-18 19:11:55 +0000437 if (ret->determinist == -1) {
438 xmlRegexpIsDeterminist(ret);
439 }
Daniel Veillard23e73572002-09-19 19:56:43 +0000440
441 if ((ret->determinist != 0) &&
442 (ret->nbCounters == 0) &&
Daniel Veillard6e65e152005-08-09 11:09:52 +0000443 (ctxt->negs == 0) &&
Daniel Veillard118aed72002-09-24 14:13:13 +0000444 (ret->atoms != NULL) &&
Daniel Veillard23e73572002-09-19 19:56:43 +0000445 (ret->atoms[0] != NULL) &&
446 (ret->atoms[0]->type == XML_REGEXP_STRING)) {
447 int i, j, nbstates = 0, nbatoms = 0;
448 int *stateRemap;
449 int *stringRemap;
450 int *transitions;
Daniel Veillard118aed72002-09-24 14:13:13 +0000451 void **transdata;
Daniel Veillard23e73572002-09-19 19:56:43 +0000452 xmlChar **stringMap;
453 xmlChar *value;
454
455 /*
456 * Switch to a compact representation
457 * 1/ counting the effective number of states left
William M. Brackddf71d62004-05-06 04:17:26 +0000458 * 2/ counting the unique number of atoms, and check that
Daniel Veillard23e73572002-09-19 19:56:43 +0000459 * they are all of the string type
460 * 3/ build a table state x atom for the transitions
461 */
462
463 stateRemap = xmlMalloc(ret->nbStates * sizeof(int));
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000464 if (stateRemap == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +0000465 xmlRegexpErrMemory(ctxt, "compiling regexp");
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000466 xmlFree(ret);
467 return(NULL);
468 }
Daniel Veillard23e73572002-09-19 19:56:43 +0000469 for (i = 0;i < ret->nbStates;i++) {
470 if (ret->states[i] != NULL) {
471 stateRemap[i] = nbstates;
472 nbstates++;
473 } else {
474 stateRemap[i] = -1;
475 }
476 }
477#ifdef DEBUG_COMPACTION
478 printf("Final: %d states\n", nbstates);
479#endif
480 stringMap = xmlMalloc(ret->nbAtoms * sizeof(char *));
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000481 if (stringMap == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +0000482 xmlRegexpErrMemory(ctxt, "compiling regexp");
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000483 xmlFree(stateRemap);
484 xmlFree(ret);
485 return(NULL);
486 }
Daniel Veillard23e73572002-09-19 19:56:43 +0000487 stringRemap = xmlMalloc(ret->nbAtoms * sizeof(int));
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000488 if (stringRemap == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +0000489 xmlRegexpErrMemory(ctxt, "compiling regexp");
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000490 xmlFree(stringMap);
491 xmlFree(stateRemap);
492 xmlFree(ret);
493 return(NULL);
494 }
Daniel Veillard23e73572002-09-19 19:56:43 +0000495 for (i = 0;i < ret->nbAtoms;i++) {
496 if ((ret->atoms[i]->type == XML_REGEXP_STRING) &&
497 (ret->atoms[i]->quant == XML_REGEXP_QUANT_ONCE)) {
498 value = ret->atoms[i]->valuep;
499 for (j = 0;j < nbatoms;j++) {
500 if (xmlStrEqual(stringMap[j], value)) {
501 stringRemap[i] = j;
502 break;
503 }
504 }
505 if (j >= nbatoms) {
506 stringRemap[i] = nbatoms;
507 stringMap[nbatoms] = xmlStrdup(value);
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000508 if (stringMap[nbatoms] == NULL) {
509 for (i = 0;i < nbatoms;i++)
510 xmlFree(stringMap[i]);
511 xmlFree(stringRemap);
512 xmlFree(stringMap);
513 xmlFree(stateRemap);
514 xmlFree(ret);
515 return(NULL);
516 }
Daniel Veillard23e73572002-09-19 19:56:43 +0000517 nbatoms++;
518 }
519 } else {
520 xmlFree(stateRemap);
521 xmlFree(stringRemap);
522 for (i = 0;i < nbatoms;i++)
523 xmlFree(stringMap[i]);
524 xmlFree(stringMap);
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000525 xmlFree(ret);
526 return(NULL);
Daniel Veillard23e73572002-09-19 19:56:43 +0000527 }
528 }
529#ifdef DEBUG_COMPACTION
530 printf("Final: %d atoms\n", nbatoms);
531#endif
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000532 transitions = (int *) xmlMalloc((nbstates + 1) *
533 (nbatoms + 1) * sizeof(int));
534 if (transitions == NULL) {
535 xmlFree(stateRemap);
536 xmlFree(stringRemap);
537 xmlFree(stringMap);
538 xmlFree(ret);
539 return(NULL);
540 }
541 memset(transitions, 0, (nbstates + 1) * (nbatoms + 1) * sizeof(int));
Daniel Veillard23e73572002-09-19 19:56:43 +0000542
543 /*
544 * Allocate the transition table. The first entry for each
William M. Brackddf71d62004-05-06 04:17:26 +0000545 * state corresponds to the state type.
Daniel Veillard23e73572002-09-19 19:56:43 +0000546 */
Daniel Veillard118aed72002-09-24 14:13:13 +0000547 transdata = NULL;
Daniel Veillard23e73572002-09-19 19:56:43 +0000548
549 for (i = 0;i < ret->nbStates;i++) {
550 int stateno, atomno, targetno, prev;
551 xmlRegStatePtr state;
552 xmlRegTransPtr trans;
553
554 stateno = stateRemap[i];
555 if (stateno == -1)
556 continue;
557 state = ret->states[i];
558
559 transitions[stateno * (nbatoms + 1)] = state->type;
560
561 for (j = 0;j < state->nbTrans;j++) {
562 trans = &(state->trans[j]);
563 if ((trans->to == -1) || (trans->atom == NULL))
564 continue;
565 atomno = stringRemap[trans->atom->no];
Daniel Veillard118aed72002-09-24 14:13:13 +0000566 if ((trans->atom->data != NULL) && (transdata == NULL)) {
567 transdata = (void **) xmlMalloc(nbstates * nbatoms *
568 sizeof(void *));
569 if (transdata != NULL)
570 memset(transdata, 0,
571 nbstates * nbatoms * sizeof(void *));
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000572 else {
Daniel Veillardff46a042003-10-08 08:53:17 +0000573 xmlRegexpErrMemory(ctxt, "compiling regexp");
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000574 break;
575 }
Daniel Veillard118aed72002-09-24 14:13:13 +0000576 }
Daniel Veillard23e73572002-09-19 19:56:43 +0000577 targetno = stateRemap[trans->to];
578 /*
William M. Brackddf71d62004-05-06 04:17:26 +0000579 * if the same atom can generate transitions to 2 different
Daniel Veillard23e73572002-09-19 19:56:43 +0000580 * states then it means the automata is not determinist and
581 * the compact form can't be used !
582 */
583 prev = transitions[stateno * (nbatoms + 1) + atomno + 1];
584 if (prev != 0) {
585 if (prev != targetno + 1) {
Daniel Veillard23e73572002-09-19 19:56:43 +0000586 ret->determinist = 0;
587#ifdef DEBUG_COMPACTION
588 printf("Indet: state %d trans %d, atom %d to %d : %d to %d\n",
589 i, j, trans->atom->no, trans->to, atomno, targetno);
590 printf(" previous to is %d\n", prev);
591#endif
Daniel Veillard118aed72002-09-24 14:13:13 +0000592 if (transdata != NULL)
593 xmlFree(transdata);
Daniel Veillard23e73572002-09-19 19:56:43 +0000594 xmlFree(transitions);
595 xmlFree(stateRemap);
596 xmlFree(stringRemap);
597 for (i = 0;i < nbatoms;i++)
598 xmlFree(stringMap[i]);
599 xmlFree(stringMap);
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000600 goto not_determ;
Daniel Veillard23e73572002-09-19 19:56:43 +0000601 }
602 } else {
603#if 0
604 printf("State %d trans %d: atom %d to %d : %d to %d\n",
605 i, j, trans->atom->no, trans->to, atomno, targetno);
606#endif
607 transitions[stateno * (nbatoms + 1) + atomno + 1] =
Daniel Veillard118aed72002-09-24 14:13:13 +0000608 targetno + 1; /* to avoid 0 */
609 if (transdata != NULL)
610 transdata[stateno * nbatoms + atomno] =
611 trans->atom->data;
Daniel Veillard23e73572002-09-19 19:56:43 +0000612 }
613 }
614 }
615 ret->determinist = 1;
616#ifdef DEBUG_COMPACTION
617 /*
618 * Debug
619 */
620 for (i = 0;i < nbstates;i++) {
621 for (j = 0;j < nbatoms + 1;j++) {
622 printf("%02d ", transitions[i * (nbatoms + 1) + j]);
623 }
624 printf("\n");
625 }
626 printf("\n");
627#endif
628 /*
629 * Cleanup of the old data
630 */
631 if (ret->states != NULL) {
632 for (i = 0;i < ret->nbStates;i++)
633 xmlRegFreeState(ret->states[i]);
634 xmlFree(ret->states);
635 }
636 ret->states = NULL;
637 ret->nbStates = 0;
638 if (ret->atoms != NULL) {
639 for (i = 0;i < ret->nbAtoms;i++)
640 xmlRegFreeAtom(ret->atoms[i]);
641 xmlFree(ret->atoms);
642 }
643 ret->atoms = NULL;
644 ret->nbAtoms = 0;
645
646 ret->compact = transitions;
Daniel Veillard118aed72002-09-24 14:13:13 +0000647 ret->transdata = transdata;
Daniel Veillard23e73572002-09-19 19:56:43 +0000648 ret->stringMap = stringMap;
649 ret->nbstrings = nbatoms;
650 ret->nbstates = nbstates;
651 xmlFree(stateRemap);
652 xmlFree(stringRemap);
653 }
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000654not_determ:
655 ctxt->string = NULL;
656 ctxt->nbStates = 0;
657 ctxt->states = NULL;
658 ctxt->nbAtoms = 0;
659 ctxt->atoms = NULL;
660 ctxt->nbCounters = 0;
661 ctxt->counters = NULL;
Daniel Veillard4255d502002-04-16 15:50:10 +0000662 return(ret);
663}
664
665/**
666 * xmlRegNewParserCtxt:
667 * @string: the string to parse
668 *
669 * Allocate a new regexp parser context
670 *
671 * Returns the new context or NULL in case of error
672 */
673static xmlRegParserCtxtPtr
674xmlRegNewParserCtxt(const xmlChar *string) {
675 xmlRegParserCtxtPtr ret;
676
677 ret = (xmlRegParserCtxtPtr) xmlMalloc(sizeof(xmlRegParserCtxt));
678 if (ret == NULL)
679 return(NULL);
680 memset(ret, 0, sizeof(xmlRegParserCtxt));
681 if (string != NULL)
682 ret->string = xmlStrdup(string);
683 ret->cur = ret->string;
684 ret->neg = 0;
Daniel Veillard6e65e152005-08-09 11:09:52 +0000685 ret->negs = 0;
Daniel Veillard4255d502002-04-16 15:50:10 +0000686 ret->error = 0;
Daniel Veillarde19fc232002-04-22 16:01:24 +0000687 ret->determinist = -1;
Daniel Veillard4255d502002-04-16 15:50:10 +0000688 return(ret);
689}
690
691/**
692 * xmlRegNewRange:
693 * @ctxt: the regexp parser context
694 * @neg: is that negative
695 * @type: the type of range
696 * @start: the start codepoint
697 * @end: the end codepoint
698 *
699 * Allocate a new regexp range
700 *
701 * Returns the new range or NULL in case of error
702 */
703static xmlRegRangePtr
704xmlRegNewRange(xmlRegParserCtxtPtr ctxt,
705 int neg, xmlRegAtomType type, int start, int end) {
706 xmlRegRangePtr ret;
707
708 ret = (xmlRegRangePtr) xmlMalloc(sizeof(xmlRegRange));
709 if (ret == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +0000710 xmlRegexpErrMemory(ctxt, "allocating range");
Daniel Veillard4255d502002-04-16 15:50:10 +0000711 return(NULL);
712 }
713 ret->neg = neg;
714 ret->type = type;
715 ret->start = start;
716 ret->end = end;
717 return(ret);
718}
719
720/**
721 * xmlRegFreeRange:
722 * @range: the regexp range
723 *
724 * Free a regexp range
725 */
726static void
727xmlRegFreeRange(xmlRegRangePtr range) {
728 if (range == NULL)
729 return;
730
731 if (range->blockName != NULL)
732 xmlFree(range->blockName);
733 xmlFree(range);
734}
735
736/**
Daniel Veillard76d59b62007-08-22 16:29:21 +0000737 * xmlRegCopyRange:
738 * @range: the regexp range
739 *
740 * Copy a regexp range
741 *
742 * Returns the new copy or NULL in case of error.
743 */
744static xmlRegRangePtr
745xmlRegCopyRange(xmlRegParserCtxtPtr ctxt, xmlRegRangePtr range) {
746 xmlRegRangePtr ret;
747
748 if (range == NULL)
749 return(NULL);
750
751 ret = xmlRegNewRange(ctxt, range->neg, range->type, range->start,
752 range->end);
753 if (ret == NULL)
754 return(NULL);
755 if (range->blockName != NULL) {
756 ret->blockName = xmlStrdup(range->blockName);
757 if (ret->blockName == NULL) {
758 xmlRegexpErrMemory(ctxt, "allocating range");
759 xmlRegFreeRange(ret);
760 return(NULL);
761 }
762 }
763 return(ret);
764}
765
766/**
Daniel Veillard4255d502002-04-16 15:50:10 +0000767 * xmlRegNewAtom:
768 * @ctxt: the regexp parser context
769 * @type: the type of atom
770 *
Daniel Veillard76d59b62007-08-22 16:29:21 +0000771 * Allocate a new atom
Daniel Veillard4255d502002-04-16 15:50:10 +0000772 *
773 * Returns the new atom or NULL in case of error
774 */
775static xmlRegAtomPtr
776xmlRegNewAtom(xmlRegParserCtxtPtr ctxt, xmlRegAtomType type) {
777 xmlRegAtomPtr ret;
778
779 ret = (xmlRegAtomPtr) xmlMalloc(sizeof(xmlRegAtom));
780 if (ret == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +0000781 xmlRegexpErrMemory(ctxt, "allocating atom");
Daniel Veillard4255d502002-04-16 15:50:10 +0000782 return(NULL);
783 }
784 memset(ret, 0, sizeof(xmlRegAtom));
785 ret->type = type;
786 ret->quant = XML_REGEXP_QUANT_ONCE;
787 ret->min = 0;
788 ret->max = 0;
789 return(ret);
790}
791
792/**
793 * xmlRegFreeAtom:
794 * @atom: the regexp atom
795 *
796 * Free a regexp atom
797 */
798static void
799xmlRegFreeAtom(xmlRegAtomPtr atom) {
800 int i;
801
802 if (atom == NULL)
803 return;
804
805 for (i = 0;i < atom->nbRanges;i++)
806 xmlRegFreeRange(atom->ranges[i]);
807 if (atom->ranges != NULL)
808 xmlFree(atom->ranges);
Daniel Veillardde0e4982005-07-03 14:35:44 +0000809 if ((atom->type == XML_REGEXP_STRING) && (atom->valuep != NULL))
810 xmlFree(atom->valuep);
Daniel Veillard77005e62005-07-19 16:26:18 +0000811 if ((atom->type == XML_REGEXP_STRING) && (atom->valuep2 != NULL))
812 xmlFree(atom->valuep2);
Daniel Veillardde0e4982005-07-03 14:35:44 +0000813 if ((atom->type == XML_REGEXP_BLOCK_NAME) && (atom->valuep != NULL))
Daniel Veillard4255d502002-04-16 15:50:10 +0000814 xmlFree(atom->valuep);
815 xmlFree(atom);
816}
817
Daniel Veillard76d59b62007-08-22 16:29:21 +0000818/**
819 * xmlRegCopyAtom:
820 * @ctxt: the regexp parser context
821 * @atom: the oiginal atom
822 *
823 * Allocate a new regexp range
824 *
825 * Returns the new atom or NULL in case of error
826 */
827static xmlRegAtomPtr
828xmlRegCopyAtom(xmlRegParserCtxtPtr ctxt, xmlRegAtomPtr atom) {
829 xmlRegAtomPtr ret;
830
831 ret = (xmlRegAtomPtr) xmlMalloc(sizeof(xmlRegAtom));
832 if (ret == NULL) {
833 xmlRegexpErrMemory(ctxt, "copying atom");
834 return(NULL);
835 }
836 memset(ret, 0, sizeof(xmlRegAtom));
837 ret->type = atom->type;
838 ret->quant = atom->quant;
839 ret->min = atom->min;
840 ret->max = atom->max;
841 if (atom->nbRanges > 0) {
842 int i;
843
844 ret->ranges = (xmlRegRangePtr *) xmlMalloc(sizeof(xmlRegRangePtr) *
845 atom->nbRanges);
846 if (ret->ranges == NULL) {
847 xmlRegexpErrMemory(ctxt, "copying atom");
848 goto error;
849 }
850 for (i = 0;i < atom->nbRanges;i++) {
851 ret->ranges[i] = xmlRegCopyRange(ctxt, atom->ranges[i]);
852 if (ret->ranges[i] == NULL)
853 goto error;
854 ret->nbRanges = i + 1;
855 }
856 }
857 return(ret);
858
859error:
860 xmlRegFreeAtom(ret);
861 return(NULL);
862}
863
Daniel Veillard4255d502002-04-16 15:50:10 +0000864static xmlRegStatePtr
865xmlRegNewState(xmlRegParserCtxtPtr ctxt) {
866 xmlRegStatePtr ret;
867
868 ret = (xmlRegStatePtr) xmlMalloc(sizeof(xmlRegState));
869 if (ret == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +0000870 xmlRegexpErrMemory(ctxt, "allocating state");
Daniel Veillard4255d502002-04-16 15:50:10 +0000871 return(NULL);
872 }
873 memset(ret, 0, sizeof(xmlRegState));
874 ret->type = XML_REGEXP_TRANS_STATE;
875 ret->mark = XML_REGEXP_MARK_NORMAL;
876 return(ret);
877}
878
879/**
880 * xmlRegFreeState:
881 * @state: the regexp state
882 *
883 * Free a regexp state
884 */
885static void
886xmlRegFreeState(xmlRegStatePtr state) {
887 if (state == NULL)
888 return;
889
890 if (state->trans != NULL)
891 xmlFree(state->trans);
Daniel Veillarddb68b742005-07-30 13:18:24 +0000892 if (state->transTo != NULL)
893 xmlFree(state->transTo);
Daniel Veillard4255d502002-04-16 15:50:10 +0000894 xmlFree(state);
895}
896
897/**
898 * xmlRegFreeParserCtxt:
899 * @ctxt: the regexp parser context
900 *
901 * Free a regexp parser context
902 */
903static void
904xmlRegFreeParserCtxt(xmlRegParserCtxtPtr ctxt) {
905 int i;
906 if (ctxt == NULL)
907 return;
908
909 if (ctxt->string != NULL)
910 xmlFree(ctxt->string);
911 if (ctxt->states != NULL) {
912 for (i = 0;i < ctxt->nbStates;i++)
913 xmlRegFreeState(ctxt->states[i]);
914 xmlFree(ctxt->states);
915 }
916 if (ctxt->atoms != NULL) {
917 for (i = 0;i < ctxt->nbAtoms;i++)
918 xmlRegFreeAtom(ctxt->atoms[i]);
919 xmlFree(ctxt->atoms);
920 }
921 if (ctxt->counters != NULL)
922 xmlFree(ctxt->counters);
923 xmlFree(ctxt);
924}
925
926/************************************************************************
927 * *
928 * Display of Data structures *
929 * *
930 ************************************************************************/
931
932static void
933xmlRegPrintAtomType(FILE *output, xmlRegAtomType type) {
934 switch (type) {
935 case XML_REGEXP_EPSILON:
936 fprintf(output, "epsilon "); break;
937 case XML_REGEXP_CHARVAL:
938 fprintf(output, "charval "); break;
939 case XML_REGEXP_RANGES:
940 fprintf(output, "ranges "); break;
941 case XML_REGEXP_SUBREG:
942 fprintf(output, "subexpr "); break;
943 case XML_REGEXP_STRING:
944 fprintf(output, "string "); break;
945 case XML_REGEXP_ANYCHAR:
946 fprintf(output, "anychar "); break;
947 case XML_REGEXP_ANYSPACE:
948 fprintf(output, "anyspace "); break;
949 case XML_REGEXP_NOTSPACE:
950 fprintf(output, "notspace "); break;
951 case XML_REGEXP_INITNAME:
952 fprintf(output, "initname "); break;
953 case XML_REGEXP_NOTINITNAME:
954 fprintf(output, "notinitname "); break;
955 case XML_REGEXP_NAMECHAR:
956 fprintf(output, "namechar "); break;
957 case XML_REGEXP_NOTNAMECHAR:
958 fprintf(output, "notnamechar "); break;
959 case XML_REGEXP_DECIMAL:
960 fprintf(output, "decimal "); break;
961 case XML_REGEXP_NOTDECIMAL:
962 fprintf(output, "notdecimal "); break;
963 case XML_REGEXP_REALCHAR:
964 fprintf(output, "realchar "); break;
965 case XML_REGEXP_NOTREALCHAR:
966 fprintf(output, "notrealchar "); break;
967 case XML_REGEXP_LETTER:
968 fprintf(output, "LETTER "); break;
969 case XML_REGEXP_LETTER_UPPERCASE:
970 fprintf(output, "LETTER_UPPERCASE "); break;
971 case XML_REGEXP_LETTER_LOWERCASE:
972 fprintf(output, "LETTER_LOWERCASE "); break;
973 case XML_REGEXP_LETTER_TITLECASE:
974 fprintf(output, "LETTER_TITLECASE "); break;
975 case XML_REGEXP_LETTER_MODIFIER:
976 fprintf(output, "LETTER_MODIFIER "); break;
977 case XML_REGEXP_LETTER_OTHERS:
978 fprintf(output, "LETTER_OTHERS "); break;
979 case XML_REGEXP_MARK:
980 fprintf(output, "MARK "); break;
981 case XML_REGEXP_MARK_NONSPACING:
982 fprintf(output, "MARK_NONSPACING "); break;
983 case XML_REGEXP_MARK_SPACECOMBINING:
984 fprintf(output, "MARK_SPACECOMBINING "); break;
985 case XML_REGEXP_MARK_ENCLOSING:
986 fprintf(output, "MARK_ENCLOSING "); break;
987 case XML_REGEXP_NUMBER:
988 fprintf(output, "NUMBER "); break;
989 case XML_REGEXP_NUMBER_DECIMAL:
990 fprintf(output, "NUMBER_DECIMAL "); break;
991 case XML_REGEXP_NUMBER_LETTER:
992 fprintf(output, "NUMBER_LETTER "); break;
993 case XML_REGEXP_NUMBER_OTHERS:
994 fprintf(output, "NUMBER_OTHERS "); break;
995 case XML_REGEXP_PUNCT:
996 fprintf(output, "PUNCT "); break;
997 case XML_REGEXP_PUNCT_CONNECTOR:
998 fprintf(output, "PUNCT_CONNECTOR "); break;
999 case XML_REGEXP_PUNCT_DASH:
1000 fprintf(output, "PUNCT_DASH "); break;
1001 case XML_REGEXP_PUNCT_OPEN:
1002 fprintf(output, "PUNCT_OPEN "); break;
1003 case XML_REGEXP_PUNCT_CLOSE:
1004 fprintf(output, "PUNCT_CLOSE "); break;
1005 case XML_REGEXP_PUNCT_INITQUOTE:
1006 fprintf(output, "PUNCT_INITQUOTE "); break;
1007 case XML_REGEXP_PUNCT_FINQUOTE:
1008 fprintf(output, "PUNCT_FINQUOTE "); break;
1009 case XML_REGEXP_PUNCT_OTHERS:
1010 fprintf(output, "PUNCT_OTHERS "); break;
1011 case XML_REGEXP_SEPAR:
1012 fprintf(output, "SEPAR "); break;
1013 case XML_REGEXP_SEPAR_SPACE:
1014 fprintf(output, "SEPAR_SPACE "); break;
1015 case XML_REGEXP_SEPAR_LINE:
1016 fprintf(output, "SEPAR_LINE "); break;
1017 case XML_REGEXP_SEPAR_PARA:
1018 fprintf(output, "SEPAR_PARA "); break;
1019 case XML_REGEXP_SYMBOL:
1020 fprintf(output, "SYMBOL "); break;
1021 case XML_REGEXP_SYMBOL_MATH:
1022 fprintf(output, "SYMBOL_MATH "); break;
1023 case XML_REGEXP_SYMBOL_CURRENCY:
1024 fprintf(output, "SYMBOL_CURRENCY "); break;
1025 case XML_REGEXP_SYMBOL_MODIFIER:
1026 fprintf(output, "SYMBOL_MODIFIER "); break;
1027 case XML_REGEXP_SYMBOL_OTHERS:
1028 fprintf(output, "SYMBOL_OTHERS "); break;
1029 case XML_REGEXP_OTHER:
1030 fprintf(output, "OTHER "); break;
1031 case XML_REGEXP_OTHER_CONTROL:
1032 fprintf(output, "OTHER_CONTROL "); break;
1033 case XML_REGEXP_OTHER_FORMAT:
1034 fprintf(output, "OTHER_FORMAT "); break;
1035 case XML_REGEXP_OTHER_PRIVATE:
1036 fprintf(output, "OTHER_PRIVATE "); break;
1037 case XML_REGEXP_OTHER_NA:
1038 fprintf(output, "OTHER_NA "); break;
1039 case XML_REGEXP_BLOCK_NAME:
1040 fprintf(output, "BLOCK "); break;
1041 }
1042}
1043
1044static void
1045xmlRegPrintQuantType(FILE *output, xmlRegQuantType type) {
1046 switch (type) {
1047 case XML_REGEXP_QUANT_EPSILON:
1048 fprintf(output, "epsilon "); break;
1049 case XML_REGEXP_QUANT_ONCE:
1050 fprintf(output, "once "); break;
1051 case XML_REGEXP_QUANT_OPT:
1052 fprintf(output, "? "); break;
1053 case XML_REGEXP_QUANT_MULT:
1054 fprintf(output, "* "); break;
1055 case XML_REGEXP_QUANT_PLUS:
1056 fprintf(output, "+ "); break;
1057 case XML_REGEXP_QUANT_RANGE:
1058 fprintf(output, "range "); break;
Daniel Veillard7646b182002-04-20 06:41:40 +00001059 case XML_REGEXP_QUANT_ONCEONLY:
1060 fprintf(output, "onceonly "); break;
1061 case XML_REGEXP_QUANT_ALL:
1062 fprintf(output, "all "); break;
Daniel Veillard4255d502002-04-16 15:50:10 +00001063 }
1064}
1065static void
1066xmlRegPrintRange(FILE *output, xmlRegRangePtr range) {
1067 fprintf(output, " range: ");
1068 if (range->neg)
1069 fprintf(output, "negative ");
1070 xmlRegPrintAtomType(output, range->type);
1071 fprintf(output, "%c - %c\n", range->start, range->end);
1072}
1073
1074static void
1075xmlRegPrintAtom(FILE *output, xmlRegAtomPtr atom) {
1076 fprintf(output, " atom: ");
1077 if (atom == NULL) {
1078 fprintf(output, "NULL\n");
1079 return;
1080 }
Daniel Veillard9efc4762005-07-19 14:33:55 +00001081 if (atom->neg)
1082 fprintf(output, "not ");
Daniel Veillard4255d502002-04-16 15:50:10 +00001083 xmlRegPrintAtomType(output, atom->type);
1084 xmlRegPrintQuantType(output, atom->quant);
1085 if (atom->quant == XML_REGEXP_QUANT_RANGE)
1086 fprintf(output, "%d-%d ", atom->min, atom->max);
1087 if (atom->type == XML_REGEXP_STRING)
1088 fprintf(output, "'%s' ", (char *) atom->valuep);
1089 if (atom->type == XML_REGEXP_CHARVAL)
1090 fprintf(output, "char %c\n", atom->codepoint);
1091 else if (atom->type == XML_REGEXP_RANGES) {
1092 int i;
1093 fprintf(output, "%d entries\n", atom->nbRanges);
1094 for (i = 0; i < atom->nbRanges;i++)
1095 xmlRegPrintRange(output, atom->ranges[i]);
1096 } else if (atom->type == XML_REGEXP_SUBREG) {
1097 fprintf(output, "start %d end %d\n", atom->start->no, atom->stop->no);
1098 } else {
1099 fprintf(output, "\n");
1100 }
1101}
1102
1103static void
1104xmlRegPrintTrans(FILE *output, xmlRegTransPtr trans) {
1105 fprintf(output, " trans: ");
1106 if (trans == NULL) {
1107 fprintf(output, "NULL\n");
1108 return;
1109 }
1110 if (trans->to < 0) {
1111 fprintf(output, "removed\n");
1112 return;
1113 }
Daniel Veillard567a45b2005-10-18 19:11:55 +00001114 if (trans->nd != 0) {
1115 if (trans->nd == 2)
1116 fprintf(output, "last not determinist, ");
1117 else
1118 fprintf(output, "not determinist, ");
1119 }
Daniel Veillard4255d502002-04-16 15:50:10 +00001120 if (trans->counter >= 0) {
1121 fprintf(output, "counted %d, ", trans->counter);
1122 }
Daniel Veillard8a001f62002-04-20 07:24:11 +00001123 if (trans->count == REGEXP_ALL_COUNTER) {
1124 fprintf(output, "all transition, ");
1125 } else if (trans->count >= 0) {
Daniel Veillard4255d502002-04-16 15:50:10 +00001126 fprintf(output, "count based %d, ", trans->count);
1127 }
1128 if (trans->atom == NULL) {
1129 fprintf(output, "epsilon to %d\n", trans->to);
1130 return;
1131 }
1132 if (trans->atom->type == XML_REGEXP_CHARVAL)
1133 fprintf(output, "char %c ", trans->atom->codepoint);
1134 fprintf(output, "atom %d, to %d\n", trans->atom->no, trans->to);
1135}
1136
1137static void
1138xmlRegPrintState(FILE *output, xmlRegStatePtr state) {
1139 int i;
1140
1141 fprintf(output, " state: ");
1142 if (state == NULL) {
1143 fprintf(output, "NULL\n");
1144 return;
1145 }
1146 if (state->type == XML_REGEXP_START_STATE)
1147 fprintf(output, "START ");
1148 if (state->type == XML_REGEXP_FINAL_STATE)
1149 fprintf(output, "FINAL ");
1150
1151 fprintf(output, "%d, %d transitions:\n", state->no, state->nbTrans);
1152 for (i = 0;i < state->nbTrans; i++) {
1153 xmlRegPrintTrans(output, &(state->trans[i]));
1154 }
1155}
1156
Daniel Veillard23e73572002-09-19 19:56:43 +00001157#ifdef DEBUG_REGEXP_GRAPH
Daniel Veillard4255d502002-04-16 15:50:10 +00001158static void
1159xmlRegPrintCtxt(FILE *output, xmlRegParserCtxtPtr ctxt) {
1160 int i;
1161
1162 fprintf(output, " ctxt: ");
1163 if (ctxt == NULL) {
1164 fprintf(output, "NULL\n");
1165 return;
1166 }
1167 fprintf(output, "'%s' ", ctxt->string);
1168 if (ctxt->error)
1169 fprintf(output, "error ");
1170 if (ctxt->neg)
1171 fprintf(output, "neg ");
1172 fprintf(output, "\n");
1173 fprintf(output, "%d atoms:\n", ctxt->nbAtoms);
1174 for (i = 0;i < ctxt->nbAtoms; i++) {
1175 fprintf(output, " %02d ", i);
1176 xmlRegPrintAtom(output, ctxt->atoms[i]);
1177 }
1178 if (ctxt->atom != NULL) {
1179 fprintf(output, "current atom:\n");
1180 xmlRegPrintAtom(output, ctxt->atom);
1181 }
1182 fprintf(output, "%d states:", ctxt->nbStates);
1183 if (ctxt->start != NULL)
1184 fprintf(output, " start: %d", ctxt->start->no);
1185 if (ctxt->end != NULL)
1186 fprintf(output, " end: %d", ctxt->end->no);
1187 fprintf(output, "\n");
1188 for (i = 0;i < ctxt->nbStates; i++) {
1189 xmlRegPrintState(output, ctxt->states[i]);
1190 }
1191 fprintf(output, "%d counters:\n", ctxt->nbCounters);
1192 for (i = 0;i < ctxt->nbCounters; i++) {
1193 fprintf(output, " %d: min %d max %d\n", i, ctxt->counters[i].min,
1194 ctxt->counters[i].max);
1195 }
1196}
Daniel Veillard23e73572002-09-19 19:56:43 +00001197#endif
Daniel Veillard4255d502002-04-16 15:50:10 +00001198
1199/************************************************************************
1200 * *
1201 * Finite Automata structures manipulations *
1202 * *
1203 ************************************************************************/
1204
1205static void
1206xmlRegAtomAddRange(xmlRegParserCtxtPtr ctxt, xmlRegAtomPtr atom,
1207 int neg, xmlRegAtomType type, int start, int end,
1208 xmlChar *blockName) {
1209 xmlRegRangePtr range;
1210
1211 if (atom == NULL) {
1212 ERROR("add range: atom is NULL");
1213 return;
1214 }
1215 if (atom->type != XML_REGEXP_RANGES) {
1216 ERROR("add range: atom is not ranges");
1217 return;
1218 }
1219 if (atom->maxRanges == 0) {
1220 atom->maxRanges = 4;
1221 atom->ranges = (xmlRegRangePtr *) xmlMalloc(atom->maxRanges *
1222 sizeof(xmlRegRangePtr));
1223 if (atom->ranges == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00001224 xmlRegexpErrMemory(ctxt, "adding ranges");
Daniel Veillard4255d502002-04-16 15:50:10 +00001225 atom->maxRanges = 0;
1226 return;
1227 }
1228 } else if (atom->nbRanges >= atom->maxRanges) {
1229 xmlRegRangePtr *tmp;
1230 atom->maxRanges *= 2;
1231 tmp = (xmlRegRangePtr *) xmlRealloc(atom->ranges, atom->maxRanges *
1232 sizeof(xmlRegRangePtr));
1233 if (tmp == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00001234 xmlRegexpErrMemory(ctxt, "adding ranges");
Daniel Veillard4255d502002-04-16 15:50:10 +00001235 atom->maxRanges /= 2;
1236 return;
1237 }
1238 atom->ranges = tmp;
1239 }
1240 range = xmlRegNewRange(ctxt, neg, type, start, end);
1241 if (range == NULL)
1242 return;
1243 range->blockName = blockName;
1244 atom->ranges[atom->nbRanges++] = range;
1245
1246}
1247
1248static int
1249xmlRegGetCounter(xmlRegParserCtxtPtr ctxt) {
1250 if (ctxt->maxCounters == 0) {
1251 ctxt->maxCounters = 4;
1252 ctxt->counters = (xmlRegCounter *) xmlMalloc(ctxt->maxCounters *
1253 sizeof(xmlRegCounter));
1254 if (ctxt->counters == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00001255 xmlRegexpErrMemory(ctxt, "allocating counter");
Daniel Veillard4255d502002-04-16 15:50:10 +00001256 ctxt->maxCounters = 0;
1257 return(-1);
1258 }
1259 } else if (ctxt->nbCounters >= ctxt->maxCounters) {
1260 xmlRegCounter *tmp;
1261 ctxt->maxCounters *= 2;
1262 tmp = (xmlRegCounter *) xmlRealloc(ctxt->counters, ctxt->maxCounters *
1263 sizeof(xmlRegCounter));
1264 if (tmp == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00001265 xmlRegexpErrMemory(ctxt, "allocating counter");
Daniel Veillard4255d502002-04-16 15:50:10 +00001266 ctxt->maxCounters /= 2;
1267 return(-1);
1268 }
1269 ctxt->counters = tmp;
1270 }
1271 ctxt->counters[ctxt->nbCounters].min = -1;
1272 ctxt->counters[ctxt->nbCounters].max = -1;
1273 return(ctxt->nbCounters++);
1274}
1275
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001276static int
Daniel Veillard4255d502002-04-16 15:50:10 +00001277xmlRegAtomPush(xmlRegParserCtxtPtr ctxt, xmlRegAtomPtr atom) {
1278 if (atom == NULL) {
1279 ERROR("atom push: atom is NULL");
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001280 return(-1);
Daniel Veillard4255d502002-04-16 15:50:10 +00001281 }
1282 if (ctxt->maxAtoms == 0) {
1283 ctxt->maxAtoms = 4;
1284 ctxt->atoms = (xmlRegAtomPtr *) xmlMalloc(ctxt->maxAtoms *
1285 sizeof(xmlRegAtomPtr));
1286 if (ctxt->atoms == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00001287 xmlRegexpErrMemory(ctxt, "pushing atom");
Daniel Veillard4255d502002-04-16 15:50:10 +00001288 ctxt->maxAtoms = 0;
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001289 return(-1);
Daniel Veillard4255d502002-04-16 15:50:10 +00001290 }
1291 } else if (ctxt->nbAtoms >= ctxt->maxAtoms) {
1292 xmlRegAtomPtr *tmp;
1293 ctxt->maxAtoms *= 2;
1294 tmp = (xmlRegAtomPtr *) xmlRealloc(ctxt->atoms, ctxt->maxAtoms *
1295 sizeof(xmlRegAtomPtr));
1296 if (tmp == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00001297 xmlRegexpErrMemory(ctxt, "allocating counter");
Daniel Veillard4255d502002-04-16 15:50:10 +00001298 ctxt->maxAtoms /= 2;
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001299 return(-1);
Daniel Veillard4255d502002-04-16 15:50:10 +00001300 }
1301 ctxt->atoms = tmp;
1302 }
1303 atom->no = ctxt->nbAtoms;
1304 ctxt->atoms[ctxt->nbAtoms++] = atom;
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001305 return(0);
Daniel Veillard4255d502002-04-16 15:50:10 +00001306}
1307
1308static void
Daniel Veillarddb68b742005-07-30 13:18:24 +00001309xmlRegStateAddTransTo(xmlRegParserCtxtPtr ctxt, xmlRegStatePtr target,
1310 int from) {
1311 if (target->maxTransTo == 0) {
1312 target->maxTransTo = 8;
1313 target->transTo = (int *) xmlMalloc(target->maxTransTo *
1314 sizeof(int));
1315 if (target->transTo == NULL) {
1316 xmlRegexpErrMemory(ctxt, "adding transition");
1317 target->maxTransTo = 0;
1318 return;
1319 }
1320 } else if (target->nbTransTo >= target->maxTransTo) {
1321 int *tmp;
1322 target->maxTransTo *= 2;
1323 tmp = (int *) xmlRealloc(target->transTo, target->maxTransTo *
1324 sizeof(int));
1325 if (tmp == NULL) {
1326 xmlRegexpErrMemory(ctxt, "adding transition");
1327 target->maxTransTo /= 2;
1328 return;
1329 }
1330 target->transTo = tmp;
1331 }
1332 target->transTo[target->nbTransTo] = from;
1333 target->nbTransTo++;
1334}
1335
1336static void
Daniel Veillard4255d502002-04-16 15:50:10 +00001337xmlRegStateAddTrans(xmlRegParserCtxtPtr ctxt, xmlRegStatePtr state,
1338 xmlRegAtomPtr atom, xmlRegStatePtr target,
Daniel Veillard5de09382005-09-26 17:18:17 +00001339 int counter, int count) {
William M. Brackf9b5fa22004-05-10 07:52:15 +00001340
1341 int nrtrans;
1342
Daniel Veillard4255d502002-04-16 15:50:10 +00001343 if (state == NULL) {
1344 ERROR("add state: state is NULL");
1345 return;
1346 }
1347 if (target == NULL) {
1348 ERROR("add state: target is NULL");
1349 return;
1350 }
William M. Brackf9b5fa22004-05-10 07:52:15 +00001351 /*
1352 * Other routines follow the philosophy 'When in doubt, add a transition'
1353 * so we check here whether such a transition is already present and, if
1354 * so, silently ignore this request.
1355 */
1356
Daniel Veillard5de09382005-09-26 17:18:17 +00001357 for (nrtrans = state->nbTrans - 1; nrtrans >= 0; nrtrans--) {
1358 xmlRegTransPtr trans = &(state->trans[nrtrans]);
1359 if ((trans->atom == atom) &&
1360 (trans->to == target->no) &&
1361 (trans->counter == counter) &&
1362 (trans->count == count)) {
William M. Brackf9b5fa22004-05-10 07:52:15 +00001363#ifdef DEBUG_REGEXP_GRAPH
Daniel Veillard5de09382005-09-26 17:18:17 +00001364 printf("Ignoring duplicate transition from %d to %d\n",
1365 state->no, target->no);
William M. Brackf9b5fa22004-05-10 07:52:15 +00001366#endif
Daniel Veillard5de09382005-09-26 17:18:17 +00001367 return;
Daniel Veillarddb68b742005-07-30 13:18:24 +00001368 }
William M. Brackf9b5fa22004-05-10 07:52:15 +00001369 }
1370
Daniel Veillard4255d502002-04-16 15:50:10 +00001371 if (state->maxTrans == 0) {
Daniel Veillarddb68b742005-07-30 13:18:24 +00001372 state->maxTrans = 8;
Daniel Veillard4255d502002-04-16 15:50:10 +00001373 state->trans = (xmlRegTrans *) xmlMalloc(state->maxTrans *
1374 sizeof(xmlRegTrans));
1375 if (state->trans == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00001376 xmlRegexpErrMemory(ctxt, "adding transition");
Daniel Veillard4255d502002-04-16 15:50:10 +00001377 state->maxTrans = 0;
1378 return;
1379 }
1380 } else if (state->nbTrans >= state->maxTrans) {
1381 xmlRegTrans *tmp;
1382 state->maxTrans *= 2;
1383 tmp = (xmlRegTrans *) xmlRealloc(state->trans, state->maxTrans *
1384 sizeof(xmlRegTrans));
1385 if (tmp == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00001386 xmlRegexpErrMemory(ctxt, "adding transition");
Daniel Veillard4255d502002-04-16 15:50:10 +00001387 state->maxTrans /= 2;
1388 return;
1389 }
1390 state->trans = tmp;
1391 }
1392#ifdef DEBUG_REGEXP_GRAPH
1393 printf("Add trans from %d to %d ", state->no, target->no);
Daniel Veillard8a001f62002-04-20 07:24:11 +00001394 if (count == REGEXP_ALL_COUNTER)
Daniel Veillard2cbf5962004-03-31 15:50:43 +00001395 printf("all transition\n");
Daniel Veillard4402ab42002-09-12 16:02:56 +00001396 else if (count >= 0)
Daniel Veillard2cbf5962004-03-31 15:50:43 +00001397 printf("count based %d\n", count);
Daniel Veillard4255d502002-04-16 15:50:10 +00001398 else if (counter >= 0)
Daniel Veillard2cbf5962004-03-31 15:50:43 +00001399 printf("counted %d\n", counter);
Daniel Veillard4255d502002-04-16 15:50:10 +00001400 else if (atom == NULL)
Daniel Veillard2cbf5962004-03-31 15:50:43 +00001401 printf("epsilon transition\n");
1402 else if (atom != NULL)
1403 xmlRegPrintAtom(stdout, atom);
Daniel Veillard4255d502002-04-16 15:50:10 +00001404#endif
1405
1406 state->trans[state->nbTrans].atom = atom;
1407 state->trans[state->nbTrans].to = target->no;
1408 state->trans[state->nbTrans].counter = counter;
1409 state->trans[state->nbTrans].count = count;
Daniel Veillard567a45b2005-10-18 19:11:55 +00001410 state->trans[state->nbTrans].nd = 0;
Daniel Veillard4255d502002-04-16 15:50:10 +00001411 state->nbTrans++;
Daniel Veillarddb68b742005-07-30 13:18:24 +00001412 xmlRegStateAddTransTo(ctxt, target, state->no);
Daniel Veillard4255d502002-04-16 15:50:10 +00001413}
1414
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001415static int
Daniel Veillard4255d502002-04-16 15:50:10 +00001416xmlRegStatePush(xmlRegParserCtxtPtr ctxt, xmlRegStatePtr state) {
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001417 if (state == NULL) return(-1);
Daniel Veillard4255d502002-04-16 15:50:10 +00001418 if (ctxt->maxStates == 0) {
1419 ctxt->maxStates = 4;
1420 ctxt->states = (xmlRegStatePtr *) xmlMalloc(ctxt->maxStates *
1421 sizeof(xmlRegStatePtr));
1422 if (ctxt->states == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00001423 xmlRegexpErrMemory(ctxt, "adding state");
Daniel Veillard4255d502002-04-16 15:50:10 +00001424 ctxt->maxStates = 0;
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001425 return(-1);
Daniel Veillard4255d502002-04-16 15:50:10 +00001426 }
1427 } else if (ctxt->nbStates >= ctxt->maxStates) {
1428 xmlRegStatePtr *tmp;
1429 ctxt->maxStates *= 2;
1430 tmp = (xmlRegStatePtr *) xmlRealloc(ctxt->states, ctxt->maxStates *
1431 sizeof(xmlRegStatePtr));
1432 if (tmp == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00001433 xmlRegexpErrMemory(ctxt, "adding state");
Daniel Veillard4255d502002-04-16 15:50:10 +00001434 ctxt->maxStates /= 2;
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001435 return(-1);
Daniel Veillard4255d502002-04-16 15:50:10 +00001436 }
1437 ctxt->states = tmp;
1438 }
1439 state->no = ctxt->nbStates;
1440 ctxt->states[ctxt->nbStates++] = state;
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001441 return(0);
Daniel Veillard4255d502002-04-16 15:50:10 +00001442}
1443
1444/**
Daniel Veillard7646b182002-04-20 06:41:40 +00001445 * xmlFAGenerateAllTransition:
Daniel Veillard441bc322002-04-20 17:38:48 +00001446 * @ctxt: a regexp parser context
1447 * @from: the from state
1448 * @to: the target state or NULL for building a new one
1449 * @lax:
Daniel Veillard7646b182002-04-20 06:41:40 +00001450 *
1451 */
1452static void
1453xmlFAGenerateAllTransition(xmlRegParserCtxtPtr ctxt,
Daniel Veillard441bc322002-04-20 17:38:48 +00001454 xmlRegStatePtr from, xmlRegStatePtr to,
1455 int lax) {
Daniel Veillard7646b182002-04-20 06:41:40 +00001456 if (to == NULL) {
1457 to = xmlRegNewState(ctxt);
1458 xmlRegStatePush(ctxt, to);
1459 ctxt->state = to;
1460 }
Daniel Veillard441bc322002-04-20 17:38:48 +00001461 if (lax)
Daniel Veillard5de09382005-09-26 17:18:17 +00001462 xmlRegStateAddTrans(ctxt, from, NULL, to, -1, REGEXP_ALL_LAX_COUNTER);
Daniel Veillard441bc322002-04-20 17:38:48 +00001463 else
Daniel Veillard5de09382005-09-26 17:18:17 +00001464 xmlRegStateAddTrans(ctxt, from, NULL, to, -1, REGEXP_ALL_COUNTER);
Daniel Veillard7646b182002-04-20 06:41:40 +00001465}
1466
1467/**
Daniel Veillard4255d502002-04-16 15:50:10 +00001468 * xmlFAGenerateEpsilonTransition:
Daniel Veillard441bc322002-04-20 17:38:48 +00001469 * @ctxt: a regexp parser context
1470 * @from: the from state
1471 * @to: the target state or NULL for building a new one
Daniel Veillard4255d502002-04-16 15:50:10 +00001472 *
1473 */
1474static void
1475xmlFAGenerateEpsilonTransition(xmlRegParserCtxtPtr ctxt,
1476 xmlRegStatePtr from, xmlRegStatePtr to) {
1477 if (to == NULL) {
1478 to = xmlRegNewState(ctxt);
1479 xmlRegStatePush(ctxt, to);
1480 ctxt->state = to;
1481 }
Daniel Veillard5de09382005-09-26 17:18:17 +00001482 xmlRegStateAddTrans(ctxt, from, NULL, to, -1, -1);
Daniel Veillard4255d502002-04-16 15:50:10 +00001483}
1484
1485/**
1486 * xmlFAGenerateCountedEpsilonTransition:
Daniel Veillard441bc322002-04-20 17:38:48 +00001487 * @ctxt: a regexp parser context
1488 * @from: the from state
1489 * @to: the target state or NULL for building a new one
Daniel Veillard4255d502002-04-16 15:50:10 +00001490 * counter: the counter for that transition
1491 *
1492 */
1493static void
1494xmlFAGenerateCountedEpsilonTransition(xmlRegParserCtxtPtr ctxt,
1495 xmlRegStatePtr from, xmlRegStatePtr to, int counter) {
1496 if (to == NULL) {
1497 to = xmlRegNewState(ctxt);
1498 xmlRegStatePush(ctxt, to);
1499 ctxt->state = to;
1500 }
Daniel Veillard5de09382005-09-26 17:18:17 +00001501 xmlRegStateAddTrans(ctxt, from, NULL, to, counter, -1);
Daniel Veillard4255d502002-04-16 15:50:10 +00001502}
1503
1504/**
1505 * xmlFAGenerateCountedTransition:
Daniel Veillard441bc322002-04-20 17:38:48 +00001506 * @ctxt: a regexp parser context
1507 * @from: the from state
1508 * @to: the target state or NULL for building a new one
Daniel Veillard4255d502002-04-16 15:50:10 +00001509 * counter: the counter for that transition
1510 *
1511 */
1512static void
1513xmlFAGenerateCountedTransition(xmlRegParserCtxtPtr ctxt,
1514 xmlRegStatePtr from, xmlRegStatePtr to, int counter) {
1515 if (to == NULL) {
1516 to = xmlRegNewState(ctxt);
1517 xmlRegStatePush(ctxt, to);
1518 ctxt->state = to;
1519 }
Daniel Veillard5de09382005-09-26 17:18:17 +00001520 xmlRegStateAddTrans(ctxt, from, NULL, to, -1, counter);
Daniel Veillard4255d502002-04-16 15:50:10 +00001521}
1522
1523/**
1524 * xmlFAGenerateTransitions:
Daniel Veillard441bc322002-04-20 17:38:48 +00001525 * @ctxt: a regexp parser context
1526 * @from: the from state
1527 * @to: the target state or NULL for building a new one
1528 * @atom: the atom generating the transition
Daniel Veillard4255d502002-04-16 15:50:10 +00001529 *
William M. Brackddf71d62004-05-06 04:17:26 +00001530 * Returns 0 if success and -1 in case of error.
Daniel Veillard4255d502002-04-16 15:50:10 +00001531 */
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001532static int
Daniel Veillard4255d502002-04-16 15:50:10 +00001533xmlFAGenerateTransitions(xmlRegParserCtxtPtr ctxt, xmlRegStatePtr from,
1534 xmlRegStatePtr to, xmlRegAtomPtr atom) {
Daniel Veillard10bda622008-03-13 07:27:24 +00001535 xmlRegStatePtr end;
1536
Daniel Veillard4255d502002-04-16 15:50:10 +00001537 if (atom == NULL) {
1538 ERROR("genrate transition: atom == NULL");
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001539 return(-1);
Daniel Veillard4255d502002-04-16 15:50:10 +00001540 }
1541 if (atom->type == XML_REGEXP_SUBREG) {
1542 /*
1543 * this is a subexpression handling one should not need to
William M. Brackddf71d62004-05-06 04:17:26 +00001544 * create a new node except for XML_REGEXP_QUANT_RANGE.
Daniel Veillard4255d502002-04-16 15:50:10 +00001545 */
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001546 if (xmlRegAtomPush(ctxt, atom) < 0) {
1547 return(-1);
1548 }
Daniel Veillard4255d502002-04-16 15:50:10 +00001549 if ((to != NULL) && (atom->stop != to) &&
1550 (atom->quant != XML_REGEXP_QUANT_RANGE)) {
1551 /*
1552 * Generate an epsilon transition to link to the target
1553 */
1554 xmlFAGenerateEpsilonTransition(ctxt, atom->stop, to);
Daniel Veillardaa622012005-10-20 15:55:25 +00001555#ifdef DV
1556 } else if ((to == NULL) && (atom->quant != XML_REGEXP_QUANT_RANGE) &&
1557 (atom->quant != XML_REGEXP_QUANT_ONCE)) {
1558 to = xmlRegNewState(ctxt);
1559 xmlRegStatePush(ctxt, to);
1560 ctxt->state = to;
1561 xmlFAGenerateEpsilonTransition(ctxt, atom->stop, to);
1562#endif
Daniel Veillard4255d502002-04-16 15:50:10 +00001563 }
1564 switch (atom->quant) {
1565 case XML_REGEXP_QUANT_OPT:
1566 atom->quant = XML_REGEXP_QUANT_ONCE;
Daniel Veillard54eb0242006-03-21 23:17:57 +00001567 /*
1568 * transition done to the state after end of atom.
1569 * 1. set transition from atom start to new state
1570 * 2. set transition from atom end to this state.
1571 */
1572 xmlFAGenerateEpsilonTransition(ctxt, atom->start, 0);
1573 xmlFAGenerateEpsilonTransition(ctxt, atom->stop, ctxt->state);
Daniel Veillard4255d502002-04-16 15:50:10 +00001574 break;
1575 case XML_REGEXP_QUANT_MULT:
1576 atom->quant = XML_REGEXP_QUANT_ONCE;
1577 xmlFAGenerateEpsilonTransition(ctxt, atom->start, atom->stop);
1578 xmlFAGenerateEpsilonTransition(ctxt, atom->stop, atom->start);
1579 break;
1580 case XML_REGEXP_QUANT_PLUS:
1581 atom->quant = XML_REGEXP_QUANT_ONCE;
1582 xmlFAGenerateEpsilonTransition(ctxt, atom->stop, atom->start);
1583 break;
1584 case XML_REGEXP_QUANT_RANGE: {
1585 int counter;
Daniel Veillard76d59b62007-08-22 16:29:21 +00001586 xmlRegStatePtr inter, newstate;
Daniel Veillard4255d502002-04-16 15:50:10 +00001587
1588 /*
Daniel Veillard76d59b62007-08-22 16:29:21 +00001589 * create the final state now if needed
Daniel Veillard4255d502002-04-16 15:50:10 +00001590 */
Daniel Veillard4255d502002-04-16 15:50:10 +00001591 if (to != NULL) {
1592 newstate = to;
1593 } else {
1594 newstate = xmlRegNewState(ctxt);
1595 xmlRegStatePush(ctxt, newstate);
Daniel Veillard4255d502002-04-16 15:50:10 +00001596 }
Daniel Veillard76d59b62007-08-22 16:29:21 +00001597
1598 /*
1599 * The principle here is to use counted transition
1600 * to avoid explosion in the number of states in the
1601 * graph. This is clearly more complex but should not
1602 * be exploitable at runtime.
Daniel Veillard54eb0242006-03-21 23:17:57 +00001603 */
Daniel Veillard76d59b62007-08-22 16:29:21 +00001604 if ((atom->min == 0) && (atom->start0 == NULL)) {
1605 xmlRegAtomPtr copy;
1606 /*
1607 * duplicate a transition based on atom to count next
1608 * occurences after 1. We cannot loop to atom->start
1609 * directly because we need an epsilon transition to
1610 * newstate.
1611 */
1612 /* ???? For some reason it seems we never reach that
1613 case, I suppose this got optimized out before when
1614 building the automata */
Daniel Veillardc821e032007-08-28 17:33:45 +00001615 copy = xmlRegCopyAtom(ctxt, atom);
Daniel Veillard76d59b62007-08-22 16:29:21 +00001616 if (copy == NULL)
1617 return(-1);
Daniel Veillard76d59b62007-08-22 16:29:21 +00001618 copy->quant = XML_REGEXP_QUANT_ONCE;
1619 copy->min = 0;
1620 copy->max = 0;
1621
1622 if (xmlFAGenerateTransitions(ctxt, atom->start, NULL, copy)
1623 < 0)
1624 return(-1);
1625 inter = ctxt->state;
1626 counter = xmlRegGetCounter(ctxt);
1627 ctxt->counters[counter].min = atom->min - 1;
1628 ctxt->counters[counter].max = atom->max - 1;
1629 /* count the number of times we see it again */
1630 xmlFAGenerateCountedEpsilonTransition(ctxt, inter,
1631 atom->stop, counter);
1632 /* allow a way out based on the count */
1633 xmlFAGenerateCountedTransition(ctxt, inter,
1634 newstate, counter);
1635 /* and also allow a direct exit for 0 */
1636 xmlFAGenerateEpsilonTransition(ctxt, atom->start,
1637 newstate);
1638 } else {
1639 /*
1640 * either we need the atom at least once or there
1641 * is an atom->start0 allowing to easilly plug the
1642 * epsilon transition.
1643 */
1644 counter = xmlRegGetCounter(ctxt);
1645 ctxt->counters[counter].min = atom->min - 1;
1646 ctxt->counters[counter].max = atom->max - 1;
1647 /* count the number of times we see it again */
1648 xmlFAGenerateCountedEpsilonTransition(ctxt, atom->stop,
1649 atom->start, counter);
1650 /* allow a way out based on the count */
1651 xmlFAGenerateCountedTransition(ctxt, atom->stop,
1652 newstate, counter);
1653 /* and if needed allow a direct exit for 0 */
1654 if (atom->min == 0)
1655 xmlFAGenerateEpsilonTransition(ctxt, atom->start0,
1656 newstate);
1657
1658 }
1659 atom->min = 0;
1660 atom->max = 0;
1661 atom->quant = XML_REGEXP_QUANT_ONCE;
1662 ctxt->state = newstate;
Daniel Veillard4255d502002-04-16 15:50:10 +00001663 }
1664 default:
1665 break;
1666 }
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001667 return(0);
Daniel Veillard567a45b2005-10-18 19:11:55 +00001668 }
1669 if ((atom->min == 0) && (atom->max == 0) &&
Daniel Veillard99c394d2005-07-14 12:58:49 +00001670 (atom->quant == XML_REGEXP_QUANT_RANGE)) {
1671 /*
1672 * we can discard the atom and generate an epsilon transition instead
1673 */
1674 if (to == NULL) {
1675 to = xmlRegNewState(ctxt);
1676 if (to != NULL)
1677 xmlRegStatePush(ctxt, to);
1678 else {
1679 return(-1);
1680 }
1681 }
1682 xmlFAGenerateEpsilonTransition(ctxt, from, to);
1683 ctxt->state = to;
1684 xmlRegFreeAtom(atom);
1685 return(0);
Daniel Veillard567a45b2005-10-18 19:11:55 +00001686 }
1687 if (to == NULL) {
1688 to = xmlRegNewState(ctxt);
1689 if (to != NULL)
1690 xmlRegStatePush(ctxt, to);
1691 else {
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001692 return(-1);
Daniel Veillard4255d502002-04-16 15:50:10 +00001693 }
Daniel Veillard10bda622008-03-13 07:27:24 +00001694 }
1695 end = to;
1696 if ((atom->quant == XML_REGEXP_QUANT_MULT) ||
1697 (atom->quant == XML_REGEXP_QUANT_PLUS)) {
1698 /*
1699 * Do not pollute the target state by adding transitions from
1700 * it as it is likely to be the shared target of multiple branches.
1701 * So isolate with an epsilon transition.
1702 */
1703 xmlRegStatePtr tmp;
1704
1705 tmp = xmlRegNewState(ctxt);
1706 if (tmp != NULL)
1707 xmlRegStatePush(ctxt, tmp);
1708 else {
1709 return(-1);
1710 }
1711 xmlFAGenerateEpsilonTransition(ctxt, tmp, to);
1712 to = tmp;
Daniel Veillard4255d502002-04-16 15:50:10 +00001713 }
Daniel Veillard567a45b2005-10-18 19:11:55 +00001714 if (xmlRegAtomPush(ctxt, atom) < 0) {
1715 return(-1);
1716 }
1717 xmlRegStateAddTrans(ctxt, from, atom, to, -1, -1);
Daniel Veillard10bda622008-03-13 07:27:24 +00001718 ctxt->state = end;
Daniel Veillard4255d502002-04-16 15:50:10 +00001719 switch (atom->quant) {
1720 case XML_REGEXP_QUANT_OPT:
1721 atom->quant = XML_REGEXP_QUANT_ONCE;
1722 xmlFAGenerateEpsilonTransition(ctxt, from, to);
1723 break;
1724 case XML_REGEXP_QUANT_MULT:
1725 atom->quant = XML_REGEXP_QUANT_ONCE;
1726 xmlFAGenerateEpsilonTransition(ctxt, from, to);
Daniel Veillard5de09382005-09-26 17:18:17 +00001727 xmlRegStateAddTrans(ctxt, to, atom, to, -1, -1);
Daniel Veillard4255d502002-04-16 15:50:10 +00001728 break;
1729 case XML_REGEXP_QUANT_PLUS:
1730 atom->quant = XML_REGEXP_QUANT_ONCE;
Daniel Veillard5de09382005-09-26 17:18:17 +00001731 xmlRegStateAddTrans(ctxt, to, atom, to, -1, -1);
Daniel Veillard4255d502002-04-16 15:50:10 +00001732 break;
William M. Brack56578372007-04-11 14:33:46 +00001733 case XML_REGEXP_QUANT_RANGE:
Daniel Veillardc821e032007-08-28 17:33:45 +00001734#if DV_test
William M. Brack56578372007-04-11 14:33:46 +00001735 if (atom->min == 0) {
1736 xmlFAGenerateEpsilonTransition(ctxt, from, to);
1737 }
Daniel Veillardc821e032007-08-28 17:33:45 +00001738#endif
William M. Brack56578372007-04-11 14:33:46 +00001739 break;
Daniel Veillard4255d502002-04-16 15:50:10 +00001740 default:
1741 break;
1742 }
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001743 return(0);
Daniel Veillard4255d502002-04-16 15:50:10 +00001744}
1745
1746/**
1747 * xmlFAReduceEpsilonTransitions:
Daniel Veillard441bc322002-04-20 17:38:48 +00001748 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00001749 * @fromnr: the from state
1750 * @tonr: the to state
William M. Brackddf71d62004-05-06 04:17:26 +00001751 * @counter: should that transition be associated to a counted
Daniel Veillard4255d502002-04-16 15:50:10 +00001752 *
1753 */
1754static void
1755xmlFAReduceEpsilonTransitions(xmlRegParserCtxtPtr ctxt, int fromnr,
1756 int tonr, int counter) {
1757 int transnr;
1758 xmlRegStatePtr from;
1759 xmlRegStatePtr to;
1760
1761#ifdef DEBUG_REGEXP_GRAPH
1762 printf("xmlFAReduceEpsilonTransitions(%d, %d)\n", fromnr, tonr);
1763#endif
1764 from = ctxt->states[fromnr];
1765 if (from == NULL)
1766 return;
1767 to = ctxt->states[tonr];
1768 if (to == NULL)
1769 return;
1770 if ((to->mark == XML_REGEXP_MARK_START) ||
1771 (to->mark == XML_REGEXP_MARK_VISITED))
1772 return;
1773
1774 to->mark = XML_REGEXP_MARK_VISITED;
1775 if (to->type == XML_REGEXP_FINAL_STATE) {
1776#ifdef DEBUG_REGEXP_GRAPH
1777 printf("State %d is final, so %d becomes final\n", tonr, fromnr);
1778#endif
1779 from->type = XML_REGEXP_FINAL_STATE;
1780 }
1781 for (transnr = 0;transnr < to->nbTrans;transnr++) {
Daniel Veillarddb68b742005-07-30 13:18:24 +00001782 if (to->trans[transnr].to < 0)
1783 continue;
Daniel Veillard4255d502002-04-16 15:50:10 +00001784 if (to->trans[transnr].atom == NULL) {
1785 /*
1786 * Don't remove counted transitions
1787 * Don't loop either
1788 */
Daniel Veillardb509f152002-04-17 16:28:10 +00001789 if (to->trans[transnr].to != fromnr) {
1790 if (to->trans[transnr].count >= 0) {
1791 int newto = to->trans[transnr].to;
1792
1793 xmlRegStateAddTrans(ctxt, from, NULL,
1794 ctxt->states[newto],
Daniel Veillard5de09382005-09-26 17:18:17 +00001795 -1, to->trans[transnr].count);
Daniel Veillardb509f152002-04-17 16:28:10 +00001796 } else {
Daniel Veillard4255d502002-04-16 15:50:10 +00001797#ifdef DEBUG_REGEXP_GRAPH
Daniel Veillardb509f152002-04-17 16:28:10 +00001798 printf("Found epsilon trans %d from %d to %d\n",
1799 transnr, tonr, to->trans[transnr].to);
Daniel Veillard4255d502002-04-16 15:50:10 +00001800#endif
Daniel Veillardb509f152002-04-17 16:28:10 +00001801 if (to->trans[transnr].counter >= 0) {
1802 xmlFAReduceEpsilonTransitions(ctxt, fromnr,
1803 to->trans[transnr].to,
1804 to->trans[transnr].counter);
1805 } else {
1806 xmlFAReduceEpsilonTransitions(ctxt, fromnr,
1807 to->trans[transnr].to,
1808 counter);
1809 }
1810 }
Daniel Veillard4255d502002-04-16 15:50:10 +00001811 }
1812 } else {
1813 int newto = to->trans[transnr].to;
1814
Daniel Veillardb509f152002-04-17 16:28:10 +00001815 if (to->trans[transnr].counter >= 0) {
1816 xmlRegStateAddTrans(ctxt, from, to->trans[transnr].atom,
1817 ctxt->states[newto],
Daniel Veillard5de09382005-09-26 17:18:17 +00001818 to->trans[transnr].counter, -1);
Daniel Veillardb509f152002-04-17 16:28:10 +00001819 } else {
1820 xmlRegStateAddTrans(ctxt, from, to->trans[transnr].atom,
Daniel Veillard5de09382005-09-26 17:18:17 +00001821 ctxt->states[newto], counter, -1);
Daniel Veillardb509f152002-04-17 16:28:10 +00001822 }
Daniel Veillard4255d502002-04-16 15:50:10 +00001823 }
1824 }
1825 to->mark = XML_REGEXP_MARK_NORMAL;
1826}
1827
1828/**
Daniel Veillarddb68b742005-07-30 13:18:24 +00001829 * xmlFAEliminateSimpleEpsilonTransitions:
1830 * @ctxt: a regexp parser context
1831 *
1832 * Eliminating general epsilon transitions can get costly in the general
1833 * algorithm due to the large amount of generated new transitions and
1834 * associated comparisons. However for simple epsilon transition used just
1835 * to separate building blocks when generating the automata this can be
1836 * reduced to state elimination:
1837 * - if there exists an epsilon from X to Y
1838 * - if there is no other transition from X
1839 * then X and Y are semantically equivalent and X can be eliminated
1840 * If X is the start state then make Y the start state, else replace the
1841 * target of all transitions to X by transitions to Y.
1842 */
1843static void
1844xmlFAEliminateSimpleEpsilonTransitions(xmlRegParserCtxtPtr ctxt) {
1845 int statenr, i, j, newto;
1846 xmlRegStatePtr state, tmp;
1847
1848 for (statenr = 0;statenr < ctxt->nbStates;statenr++) {
1849 state = ctxt->states[statenr];
1850 if (state == NULL)
1851 continue;
1852 if (state->nbTrans != 1)
1853 continue;
Daniel Veillard0e05f4c2006-11-01 15:33:04 +00001854 if (state->type == XML_REGEXP_UNREACH_STATE)
1855 continue;
Daniel Veillarddb68b742005-07-30 13:18:24 +00001856 /* is the only transition out a basic transition */
1857 if ((state->trans[0].atom == NULL) &&
1858 (state->trans[0].to >= 0) &&
1859 (state->trans[0].to != statenr) &&
1860 (state->trans[0].counter < 0) &&
1861 (state->trans[0].count < 0)) {
1862 newto = state->trans[0].to;
1863
1864 if (state->type == XML_REGEXP_START_STATE) {
1865#ifdef DEBUG_REGEXP_GRAPH
1866 printf("Found simple epsilon trans from start %d to %d\n",
1867 statenr, newto);
1868#endif
1869 } else {
1870#ifdef DEBUG_REGEXP_GRAPH
1871 printf("Found simple epsilon trans from %d to %d\n",
1872 statenr, newto);
1873#endif
1874 for (i = 0;i < state->nbTransTo;i++) {
1875 tmp = ctxt->states[state->transTo[i]];
1876 for (j = 0;j < tmp->nbTrans;j++) {
1877 if (tmp->trans[j].to == statenr) {
Daniel Veillarddb68b742005-07-30 13:18:24 +00001878#ifdef DEBUG_REGEXP_GRAPH
1879 printf("Changed transition %d on %d to go to %d\n",
1880 j, tmp->no, newto);
1881#endif
Daniel Veillard0e05f4c2006-11-01 15:33:04 +00001882 tmp->trans[j].to = -1;
1883 xmlRegStateAddTrans(ctxt, tmp, tmp->trans[j].atom,
1884 ctxt->states[newto],
1885 tmp->trans[j].counter,
1886 tmp->trans[j].count);
Daniel Veillarddb68b742005-07-30 13:18:24 +00001887 }
1888 }
1889 }
Daniel Veillarddb68b742005-07-30 13:18:24 +00001890 if (state->type == XML_REGEXP_FINAL_STATE)
1891 ctxt->states[newto]->type = XML_REGEXP_FINAL_STATE;
1892 /* eliminate the transition completely */
1893 state->nbTrans = 0;
1894
Daniel Veillard0e05f4c2006-11-01 15:33:04 +00001895 state->type = XML_REGEXP_UNREACH_STATE;
Daniel Veillarddb68b742005-07-30 13:18:24 +00001896
1897 }
1898
1899 }
1900 }
1901}
1902/**
Daniel Veillard4255d502002-04-16 15:50:10 +00001903 * xmlFAEliminateEpsilonTransitions:
Daniel Veillard441bc322002-04-20 17:38:48 +00001904 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00001905 *
1906 */
1907static void
1908xmlFAEliminateEpsilonTransitions(xmlRegParserCtxtPtr ctxt) {
1909 int statenr, transnr;
1910 xmlRegStatePtr state;
Daniel Veillarddb68b742005-07-30 13:18:24 +00001911 int has_epsilon;
Daniel Veillard4255d502002-04-16 15:50:10 +00001912
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001913 if (ctxt->states == NULL) return;
1914
Daniel Veillard0e05f4c2006-11-01 15:33:04 +00001915 /*
1916 * Eliminate simple epsilon transition and the associated unreachable
1917 * states.
1918 */
Daniel Veillarddb68b742005-07-30 13:18:24 +00001919 xmlFAEliminateSimpleEpsilonTransitions(ctxt);
Daniel Veillard0e05f4c2006-11-01 15:33:04 +00001920 for (statenr = 0;statenr < ctxt->nbStates;statenr++) {
1921 state = ctxt->states[statenr];
1922 if ((state != NULL) && (state->type == XML_REGEXP_UNREACH_STATE)) {
1923#ifdef DEBUG_REGEXP_GRAPH
1924 printf("Removed unreachable state %d\n", statenr);
1925#endif
1926 xmlRegFreeState(state);
1927 ctxt->states[statenr] = NULL;
1928 }
1929 }
Daniel Veillarddb68b742005-07-30 13:18:24 +00001930
1931 has_epsilon = 0;
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001932
Daniel Veillard4255d502002-04-16 15:50:10 +00001933 /*
Daniel Veillardfcd18ff2006-11-02 10:28:04 +00001934 * Build the completed transitions bypassing the epsilons
Daniel Veillard4255d502002-04-16 15:50:10 +00001935 * Use a marking algorithm to avoid loops
Daniel Veillardfcd18ff2006-11-02 10:28:04 +00001936 * Mark sink states too.
1937 * Process from the latests states backward to the start when
1938 * there is long cascading epsilon chains this minimize the
1939 * recursions and transition compares when adding the new ones
Daniel Veillard4255d502002-04-16 15:50:10 +00001940 */
Daniel Veillardfcd18ff2006-11-02 10:28:04 +00001941 for (statenr = ctxt->nbStates - 1;statenr >= 0;statenr--) {
Daniel Veillard4255d502002-04-16 15:50:10 +00001942 state = ctxt->states[statenr];
1943 if (state == NULL)
1944 continue;
Daniel Veillardcc026dc2005-01-12 13:21:17 +00001945 if ((state->nbTrans == 0) &&
1946 (state->type != XML_REGEXP_FINAL_STATE)) {
1947 state->type = XML_REGEXP_SINK_STATE;
1948 }
Daniel Veillard4255d502002-04-16 15:50:10 +00001949 for (transnr = 0;transnr < state->nbTrans;transnr++) {
1950 if ((state->trans[transnr].atom == NULL) &&
1951 (state->trans[transnr].to >= 0)) {
1952 if (state->trans[transnr].to == statenr) {
1953 state->trans[transnr].to = -1;
1954#ifdef DEBUG_REGEXP_GRAPH
1955 printf("Removed loopback epsilon trans %d on %d\n",
1956 transnr, statenr);
1957#endif
1958 } else if (state->trans[transnr].count < 0) {
1959 int newto = state->trans[transnr].to;
1960
1961#ifdef DEBUG_REGEXP_GRAPH
1962 printf("Found epsilon trans %d from %d to %d\n",
1963 transnr, statenr, newto);
1964#endif
Daniel Veillarddb68b742005-07-30 13:18:24 +00001965 has_epsilon = 1;
Daniel Veillard0e05f4c2006-11-01 15:33:04 +00001966 state->trans[transnr].to = -2;
1967 state->mark = XML_REGEXP_MARK_START;
Daniel Veillard4255d502002-04-16 15:50:10 +00001968 xmlFAReduceEpsilonTransitions(ctxt, statenr,
1969 newto, state->trans[transnr].counter);
1970 state->mark = XML_REGEXP_MARK_NORMAL;
1971#ifdef DEBUG_REGEXP_GRAPH
1972 } else {
1973 printf("Found counted transition %d on %d\n",
1974 transnr, statenr);
1975#endif
1976 }
1977 }
1978 }
1979 }
1980 /*
1981 * Eliminate the epsilon transitions
1982 */
Daniel Veillarddb68b742005-07-30 13:18:24 +00001983 if (has_epsilon) {
1984 for (statenr = 0;statenr < ctxt->nbStates;statenr++) {
1985 state = ctxt->states[statenr];
1986 if (state == NULL)
1987 continue;
1988 for (transnr = 0;transnr < state->nbTrans;transnr++) {
1989 xmlRegTransPtr trans = &(state->trans[transnr]);
1990 if ((trans->atom == NULL) &&
1991 (trans->count < 0) &&
1992 (trans->to >= 0)) {
1993 trans->to = -1;
1994 }
Daniel Veillard4255d502002-04-16 15:50:10 +00001995 }
1996 }
1997 }
Daniel Veillard23e73572002-09-19 19:56:43 +00001998
1999 /*
2000 * Use this pass to detect unreachable states too
2001 */
2002 for (statenr = 0;statenr < ctxt->nbStates;statenr++) {
2003 state = ctxt->states[statenr];
2004 if (state != NULL)
William M. Brack779af002003-08-01 15:55:39 +00002005 state->reached = XML_REGEXP_MARK_NORMAL;
Daniel Veillard23e73572002-09-19 19:56:43 +00002006 }
2007 state = ctxt->states[0];
2008 if (state != NULL)
William M. Brack779af002003-08-01 15:55:39 +00002009 state->reached = XML_REGEXP_MARK_START;
Daniel Veillard23e73572002-09-19 19:56:43 +00002010 while (state != NULL) {
2011 xmlRegStatePtr target = NULL;
William M. Brack779af002003-08-01 15:55:39 +00002012 state->reached = XML_REGEXP_MARK_VISITED;
Daniel Veillard23e73572002-09-19 19:56:43 +00002013 /*
William M. Brackddf71d62004-05-06 04:17:26 +00002014 * Mark all states reachable from the current reachable state
Daniel Veillard23e73572002-09-19 19:56:43 +00002015 */
2016 for (transnr = 0;transnr < state->nbTrans;transnr++) {
2017 if ((state->trans[transnr].to >= 0) &&
2018 ((state->trans[transnr].atom != NULL) ||
2019 (state->trans[transnr].count >= 0))) {
2020 int newto = state->trans[transnr].to;
2021
2022 if (ctxt->states[newto] == NULL)
2023 continue;
William M. Brack779af002003-08-01 15:55:39 +00002024 if (ctxt->states[newto]->reached == XML_REGEXP_MARK_NORMAL) {
2025 ctxt->states[newto]->reached = XML_REGEXP_MARK_START;
Daniel Veillard23e73572002-09-19 19:56:43 +00002026 target = ctxt->states[newto];
2027 }
2028 }
2029 }
Daniel Veillardcc026dc2005-01-12 13:21:17 +00002030
Daniel Veillard23e73572002-09-19 19:56:43 +00002031 /*
2032 * find the next accessible state not explored
2033 */
2034 if (target == NULL) {
2035 for (statenr = 1;statenr < ctxt->nbStates;statenr++) {
2036 state = ctxt->states[statenr];
William M. Brack779af002003-08-01 15:55:39 +00002037 if ((state != NULL) && (state->reached ==
2038 XML_REGEXP_MARK_START)) {
Daniel Veillard23e73572002-09-19 19:56:43 +00002039 target = state;
2040 break;
2041 }
2042 }
2043 }
2044 state = target;
2045 }
2046 for (statenr = 0;statenr < ctxt->nbStates;statenr++) {
2047 state = ctxt->states[statenr];
William M. Brack779af002003-08-01 15:55:39 +00002048 if ((state != NULL) && (state->reached == XML_REGEXP_MARK_NORMAL)) {
Daniel Veillard23e73572002-09-19 19:56:43 +00002049#ifdef DEBUG_REGEXP_GRAPH
2050 printf("Removed unreachable state %d\n", statenr);
2051#endif
2052 xmlRegFreeState(state);
2053 ctxt->states[statenr] = NULL;
2054 }
2055 }
2056
Daniel Veillard4255d502002-04-16 15:50:10 +00002057}
2058
Daniel Veillard567a45b2005-10-18 19:11:55 +00002059static int
2060xmlFACompareRanges(xmlRegRangePtr range1, xmlRegRangePtr range2) {
2061 int ret = 0;
2062
2063 if ((range1->type == XML_REGEXP_RANGES) ||
2064 (range2->type == XML_REGEXP_RANGES) ||
2065 (range2->type == XML_REGEXP_SUBREG) ||
2066 (range1->type == XML_REGEXP_SUBREG) ||
2067 (range1->type == XML_REGEXP_STRING) ||
2068 (range2->type == XML_REGEXP_STRING))
2069 return(-1);
2070
2071 /* put them in order */
2072 if (range1->type > range2->type) {
2073 xmlRegRangePtr tmp;
2074
2075 tmp = range1;
2076 range1 = range2;
2077 range2 = tmp;
2078 }
2079 if ((range1->type == XML_REGEXP_ANYCHAR) ||
2080 (range2->type == XML_REGEXP_ANYCHAR)) {
2081 ret = 1;
2082 } else if ((range1->type == XML_REGEXP_EPSILON) ||
2083 (range2->type == XML_REGEXP_EPSILON)) {
2084 return(0);
2085 } else if (range1->type == range2->type) {
2086 if ((range1->type != XML_REGEXP_CHARVAL) ||
2087 (range1->end < range2->start) ||
2088 (range2->end < range1->start))
2089 ret = 1;
2090 else
2091 ret = 0;
2092 } else if (range1->type == XML_REGEXP_CHARVAL) {
2093 int codepoint;
2094 int neg = 0;
2095
2096 /*
2097 * just check all codepoints in the range for acceptance,
2098 * this is usually way cheaper since done only once at
2099 * compilation than testing over and over at runtime or
2100 * pushing too many states when evaluating.
2101 */
2102 if (((range1->neg == 0) && (range2->neg != 0)) ||
2103 ((range1->neg != 0) && (range2->neg == 0)))
2104 neg = 1;
2105
2106 for (codepoint = range1->start;codepoint <= range1->end ;codepoint++) {
2107 ret = xmlRegCheckCharacterRange(range2->type, codepoint,
2108 0, range2->start, range2->end,
2109 range2->blockName);
2110 if (ret < 0)
2111 return(-1);
2112 if (((neg == 1) && (ret == 0)) ||
2113 ((neg == 0) && (ret == 1)))
2114 return(1);
2115 }
2116 return(0);
2117 } else if ((range1->type == XML_REGEXP_BLOCK_NAME) ||
2118 (range2->type == XML_REGEXP_BLOCK_NAME)) {
2119 if (range1->type == range2->type) {
2120 ret = xmlStrEqual(range1->blockName, range2->blockName);
2121 } else {
2122 /*
2123 * comparing a block range with anything else is way
2124 * too costly, and maintining the table is like too much
2125 * memory too, so let's force the automata to save state
2126 * here.
2127 */
2128 return(1);
2129 }
2130 } else if ((range1->type < XML_REGEXP_LETTER) ||
2131 (range2->type < XML_REGEXP_LETTER)) {
2132 if ((range1->type == XML_REGEXP_ANYSPACE) &&
2133 (range2->type == XML_REGEXP_NOTSPACE))
2134 ret = 0;
2135 else if ((range1->type == XML_REGEXP_INITNAME) &&
2136 (range2->type == XML_REGEXP_NOTINITNAME))
2137 ret = 0;
2138 else if ((range1->type == XML_REGEXP_NAMECHAR) &&
2139 (range2->type == XML_REGEXP_NOTNAMECHAR))
2140 ret = 0;
2141 else if ((range1->type == XML_REGEXP_DECIMAL) &&
2142 (range2->type == XML_REGEXP_NOTDECIMAL))
2143 ret = 0;
2144 else if ((range1->type == XML_REGEXP_REALCHAR) &&
2145 (range2->type == XML_REGEXP_NOTREALCHAR))
2146 ret = 0;
2147 else {
2148 /* same thing to limit complexity */
2149 return(1);
2150 }
2151 } else {
2152 ret = 0;
2153 /* range1->type < range2->type here */
2154 switch (range1->type) {
2155 case XML_REGEXP_LETTER:
2156 /* all disjoint except in the subgroups */
2157 if ((range2->type == XML_REGEXP_LETTER_UPPERCASE) ||
2158 (range2->type == XML_REGEXP_LETTER_LOWERCASE) ||
2159 (range2->type == XML_REGEXP_LETTER_TITLECASE) ||
2160 (range2->type == XML_REGEXP_LETTER_MODIFIER) ||
2161 (range2->type == XML_REGEXP_LETTER_OTHERS))
2162 ret = 1;
2163 break;
2164 case XML_REGEXP_MARK:
2165 if ((range2->type == XML_REGEXP_MARK_NONSPACING) ||
2166 (range2->type == XML_REGEXP_MARK_SPACECOMBINING) ||
2167 (range2->type == XML_REGEXP_MARK_ENCLOSING))
2168 ret = 1;
2169 break;
2170 case XML_REGEXP_NUMBER:
2171 if ((range2->type == XML_REGEXP_NUMBER_DECIMAL) ||
2172 (range2->type == XML_REGEXP_NUMBER_LETTER) ||
2173 (range2->type == XML_REGEXP_NUMBER_OTHERS))
2174 ret = 1;
2175 break;
2176 case XML_REGEXP_PUNCT:
2177 if ((range2->type == XML_REGEXP_PUNCT_CONNECTOR) ||
2178 (range2->type == XML_REGEXP_PUNCT_DASH) ||
2179 (range2->type == XML_REGEXP_PUNCT_OPEN) ||
2180 (range2->type == XML_REGEXP_PUNCT_CLOSE) ||
2181 (range2->type == XML_REGEXP_PUNCT_INITQUOTE) ||
2182 (range2->type == XML_REGEXP_PUNCT_FINQUOTE) ||
2183 (range2->type == XML_REGEXP_PUNCT_OTHERS))
2184 ret = 1;
2185 break;
2186 case XML_REGEXP_SEPAR:
2187 if ((range2->type == XML_REGEXP_SEPAR_SPACE) ||
2188 (range2->type == XML_REGEXP_SEPAR_LINE) ||
2189 (range2->type == XML_REGEXP_SEPAR_PARA))
2190 ret = 1;
2191 break;
2192 case XML_REGEXP_SYMBOL:
2193 if ((range2->type == XML_REGEXP_SYMBOL_MATH) ||
2194 (range2->type == XML_REGEXP_SYMBOL_CURRENCY) ||
2195 (range2->type == XML_REGEXP_SYMBOL_MODIFIER) ||
2196 (range2->type == XML_REGEXP_SYMBOL_OTHERS))
2197 ret = 1;
2198 break;
2199 case XML_REGEXP_OTHER:
2200 if ((range2->type == XML_REGEXP_OTHER_CONTROL) ||
2201 (range2->type == XML_REGEXP_OTHER_FORMAT) ||
2202 (range2->type == XML_REGEXP_OTHER_PRIVATE))
2203 ret = 1;
2204 break;
2205 default:
2206 if ((range2->type >= XML_REGEXP_LETTER) &&
2207 (range2->type < XML_REGEXP_BLOCK_NAME))
2208 ret = 0;
2209 else {
2210 /* safety net ! */
2211 return(1);
2212 }
2213 }
2214 }
2215 if (((range1->neg == 0) && (range2->neg != 0)) ||
2216 ((range1->neg != 0) && (range2->neg == 0)))
2217 ret = !ret;
2218 return(1);
2219}
2220
Daniel Veillarde19fc232002-04-22 16:01:24 +00002221/**
Daniel Veillardfc011b72006-02-12 19:14:15 +00002222 * xmlFACompareAtomTypes:
2223 * @type1: an atom type
2224 * @type2: an atom type
2225 *
2226 * Compares two atoms type to check whether they intersect in some ways,
2227 * this is used by xmlFACompareAtoms only
2228 *
2229 * Returns 1 if they may intersect and 0 otherwise
2230 */
2231static int
2232xmlFACompareAtomTypes(xmlRegAtomType type1, xmlRegAtomType type2) {
2233 if ((type1 == XML_REGEXP_EPSILON) ||
2234 (type1 == XML_REGEXP_CHARVAL) ||
2235 (type1 == XML_REGEXP_RANGES) ||
2236 (type1 == XML_REGEXP_SUBREG) ||
2237 (type1 == XML_REGEXP_STRING) ||
2238 (type1 == XML_REGEXP_ANYCHAR))
2239 return(1);
2240 if ((type2 == XML_REGEXP_EPSILON) ||
2241 (type2 == XML_REGEXP_CHARVAL) ||
2242 (type2 == XML_REGEXP_RANGES) ||
2243 (type2 == XML_REGEXP_SUBREG) ||
2244 (type2 == XML_REGEXP_STRING) ||
2245 (type2 == XML_REGEXP_ANYCHAR))
2246 return(1);
2247
2248 if (type1 == type2) return(1);
2249
2250 /* simplify subsequent compares by making sure type1 < type2 */
2251 if (type1 > type2) {
2252 xmlRegAtomType tmp = type1;
2253 type1 = type2;
2254 type2 = tmp;
2255 }
2256 switch (type1) {
2257 case XML_REGEXP_ANYSPACE: /* \s */
2258 /* can't be a letter, number, mark, pontuation, symbol */
2259 if ((type2 == XML_REGEXP_NOTSPACE) ||
2260 ((type2 >= XML_REGEXP_LETTER) &&
2261 (type2 <= XML_REGEXP_LETTER_OTHERS)) ||
2262 ((type2 >= XML_REGEXP_NUMBER) &&
2263 (type2 <= XML_REGEXP_NUMBER_OTHERS)) ||
2264 ((type2 >= XML_REGEXP_MARK) &&
2265 (type2 <= XML_REGEXP_MARK_ENCLOSING)) ||
2266 ((type2 >= XML_REGEXP_PUNCT) &&
2267 (type2 <= XML_REGEXP_PUNCT_OTHERS)) ||
2268 ((type2 >= XML_REGEXP_SYMBOL) &&
2269 (type2 <= XML_REGEXP_SYMBOL_OTHERS))
2270 ) return(0);
2271 break;
2272 case XML_REGEXP_NOTSPACE: /* \S */
2273 break;
2274 case XML_REGEXP_INITNAME: /* \l */
2275 /* can't be a number, mark, separator, pontuation, symbol or other */
2276 if ((type2 == XML_REGEXP_NOTINITNAME) ||
2277 ((type2 >= XML_REGEXP_NUMBER) &&
2278 (type2 <= XML_REGEXP_NUMBER_OTHERS)) ||
2279 ((type2 >= XML_REGEXP_MARK) &&
2280 (type2 <= XML_REGEXP_MARK_ENCLOSING)) ||
2281 ((type2 >= XML_REGEXP_SEPAR) &&
2282 (type2 <= XML_REGEXP_SEPAR_PARA)) ||
2283 ((type2 >= XML_REGEXP_PUNCT) &&
2284 (type2 <= XML_REGEXP_PUNCT_OTHERS)) ||
2285 ((type2 >= XML_REGEXP_SYMBOL) &&
2286 (type2 <= XML_REGEXP_SYMBOL_OTHERS)) ||
2287 ((type2 >= XML_REGEXP_OTHER) &&
2288 (type2 <= XML_REGEXP_OTHER_NA))
2289 ) return(0);
2290 break;
2291 case XML_REGEXP_NOTINITNAME: /* \L */
2292 break;
2293 case XML_REGEXP_NAMECHAR: /* \c */
2294 /* can't be a mark, separator, pontuation, symbol or other */
2295 if ((type2 == XML_REGEXP_NOTNAMECHAR) ||
2296 ((type2 >= XML_REGEXP_MARK) &&
2297 (type2 <= XML_REGEXP_MARK_ENCLOSING)) ||
2298 ((type2 >= XML_REGEXP_PUNCT) &&
2299 (type2 <= XML_REGEXP_PUNCT_OTHERS)) ||
2300 ((type2 >= XML_REGEXP_SEPAR) &&
2301 (type2 <= XML_REGEXP_SEPAR_PARA)) ||
2302 ((type2 >= XML_REGEXP_SYMBOL) &&
2303 (type2 <= XML_REGEXP_SYMBOL_OTHERS)) ||
2304 ((type2 >= XML_REGEXP_OTHER) &&
2305 (type2 <= XML_REGEXP_OTHER_NA))
2306 ) return(0);
2307 break;
2308 case XML_REGEXP_NOTNAMECHAR: /* \C */
2309 break;
2310 case XML_REGEXP_DECIMAL: /* \d */
2311 /* can't be a letter, mark, separator, pontuation, symbol or other */
2312 if ((type2 == XML_REGEXP_NOTDECIMAL) ||
2313 (type2 == XML_REGEXP_REALCHAR) ||
2314 ((type2 >= XML_REGEXP_LETTER) &&
2315 (type2 <= XML_REGEXP_LETTER_OTHERS)) ||
2316 ((type2 >= XML_REGEXP_MARK) &&
2317 (type2 <= XML_REGEXP_MARK_ENCLOSING)) ||
2318 ((type2 >= XML_REGEXP_PUNCT) &&
2319 (type2 <= XML_REGEXP_PUNCT_OTHERS)) ||
2320 ((type2 >= XML_REGEXP_SEPAR) &&
2321 (type2 <= XML_REGEXP_SEPAR_PARA)) ||
2322 ((type2 >= XML_REGEXP_SYMBOL) &&
2323 (type2 <= XML_REGEXP_SYMBOL_OTHERS)) ||
2324 ((type2 >= XML_REGEXP_OTHER) &&
2325 (type2 <= XML_REGEXP_OTHER_NA))
2326 )return(0);
2327 break;
2328 case XML_REGEXP_NOTDECIMAL: /* \D */
2329 break;
2330 case XML_REGEXP_REALCHAR: /* \w */
2331 /* can't be a mark, separator, pontuation, symbol or other */
2332 if ((type2 == XML_REGEXP_NOTDECIMAL) ||
2333 ((type2 >= XML_REGEXP_MARK) &&
2334 (type2 <= XML_REGEXP_MARK_ENCLOSING)) ||
2335 ((type2 >= XML_REGEXP_PUNCT) &&
2336 (type2 <= XML_REGEXP_PUNCT_OTHERS)) ||
2337 ((type2 >= XML_REGEXP_SEPAR) &&
2338 (type2 <= XML_REGEXP_SEPAR_PARA)) ||
2339 ((type2 >= XML_REGEXP_SYMBOL) &&
2340 (type2 <= XML_REGEXP_SYMBOL_OTHERS)) ||
2341 ((type2 >= XML_REGEXP_OTHER) &&
2342 (type2 <= XML_REGEXP_OTHER_NA))
2343 )return(0);
2344 break;
2345 case XML_REGEXP_NOTREALCHAR: /* \W */
2346 break;
2347 /*
2348 * at that point we know both type 1 and type2 are from
2349 * character categories are ordered and are different,
2350 * it becomes simple because this is a partition
2351 */
2352 case XML_REGEXP_LETTER:
2353 if (type2 <= XML_REGEXP_LETTER_OTHERS)
2354 return(1);
2355 return(0);
2356 case XML_REGEXP_LETTER_UPPERCASE:
2357 case XML_REGEXP_LETTER_LOWERCASE:
2358 case XML_REGEXP_LETTER_TITLECASE:
2359 case XML_REGEXP_LETTER_MODIFIER:
2360 case XML_REGEXP_LETTER_OTHERS:
2361 return(0);
2362 case XML_REGEXP_MARK:
2363 if (type2 <= XML_REGEXP_MARK_ENCLOSING)
2364 return(1);
2365 return(0);
2366 case XML_REGEXP_MARK_NONSPACING:
2367 case XML_REGEXP_MARK_SPACECOMBINING:
2368 case XML_REGEXP_MARK_ENCLOSING:
2369 return(0);
2370 case XML_REGEXP_NUMBER:
2371 if (type2 <= XML_REGEXP_NUMBER_OTHERS)
2372 return(1);
2373 return(0);
2374 case XML_REGEXP_NUMBER_DECIMAL:
2375 case XML_REGEXP_NUMBER_LETTER:
2376 case XML_REGEXP_NUMBER_OTHERS:
2377 return(0);
2378 case XML_REGEXP_PUNCT:
2379 if (type2 <= XML_REGEXP_PUNCT_OTHERS)
2380 return(1);
2381 return(0);
2382 case XML_REGEXP_PUNCT_CONNECTOR:
2383 case XML_REGEXP_PUNCT_DASH:
2384 case XML_REGEXP_PUNCT_OPEN:
2385 case XML_REGEXP_PUNCT_CLOSE:
2386 case XML_REGEXP_PUNCT_INITQUOTE:
2387 case XML_REGEXP_PUNCT_FINQUOTE:
2388 case XML_REGEXP_PUNCT_OTHERS:
2389 return(0);
2390 case XML_REGEXP_SEPAR:
2391 if (type2 <= XML_REGEXP_SEPAR_PARA)
2392 return(1);
2393 return(0);
2394 case XML_REGEXP_SEPAR_SPACE:
2395 case XML_REGEXP_SEPAR_LINE:
2396 case XML_REGEXP_SEPAR_PARA:
2397 return(0);
2398 case XML_REGEXP_SYMBOL:
2399 if (type2 <= XML_REGEXP_SYMBOL_OTHERS)
2400 return(1);
2401 return(0);
2402 case XML_REGEXP_SYMBOL_MATH:
2403 case XML_REGEXP_SYMBOL_CURRENCY:
2404 case XML_REGEXP_SYMBOL_MODIFIER:
2405 case XML_REGEXP_SYMBOL_OTHERS:
2406 return(0);
2407 case XML_REGEXP_OTHER:
2408 if (type2 <= XML_REGEXP_OTHER_NA)
2409 return(1);
2410 return(0);
2411 case XML_REGEXP_OTHER_CONTROL:
2412 case XML_REGEXP_OTHER_FORMAT:
2413 case XML_REGEXP_OTHER_PRIVATE:
2414 case XML_REGEXP_OTHER_NA:
2415 return(0);
2416 default:
2417 break;
2418 }
2419 return(1);
2420}
2421
2422/**
2423 * xmlFAEqualAtoms:
Daniel Veillarde19fc232002-04-22 16:01:24 +00002424 * @atom1: an atom
2425 * @atom2: an atom
2426 *
Daniel Veillardfc011b72006-02-12 19:14:15 +00002427 * Compares two atoms to check whether they are the same exactly
2428 * this is used to remove equivalent transitions
Daniel Veillarde19fc232002-04-22 16:01:24 +00002429 *
Daniel Veillardfc011b72006-02-12 19:14:15 +00002430 * Returns 1 if same and 0 otherwise
Daniel Veillarde19fc232002-04-22 16:01:24 +00002431 */
2432static int
Daniel Veillardfc011b72006-02-12 19:14:15 +00002433xmlFAEqualAtoms(xmlRegAtomPtr atom1, xmlRegAtomPtr atom2) {
2434 int ret = 0;
Daniel Veillard9efc4762005-07-19 14:33:55 +00002435
Daniel Veillarde19fc232002-04-22 16:01:24 +00002436 if (atom1 == atom2)
2437 return(1);
2438 if ((atom1 == NULL) || (atom2 == NULL))
2439 return(0);
2440
Daniel Veillardfc011b72006-02-12 19:14:15 +00002441 if (atom1->type != atom2->type)
2442 return(0);
2443 switch (atom1->type) {
2444 case XML_REGEXP_EPSILON:
2445 ret = 0;
2446 break;
2447 case XML_REGEXP_STRING:
2448 ret = xmlStrEqual((xmlChar *)atom1->valuep,
2449 (xmlChar *)atom2->valuep);
2450 break;
2451 case XML_REGEXP_CHARVAL:
2452 ret = (atom1->codepoint == atom2->codepoint);
2453 break;
2454 case XML_REGEXP_RANGES:
2455 /* too hard to do in the general case */
2456 ret = 0;
2457 default:
2458 break;
2459 }
2460 return(ret);
2461}
2462
2463/**
2464 * xmlFACompareAtoms:
2465 * @atom1: an atom
2466 * @atom2: an atom
2467 *
2468 * Compares two atoms to check whether they intersect in some ways,
2469 * this is used by xmlFAComputesDeterminism and xmlFARecurseDeterminism only
2470 *
2471 * Returns 1 if yes and 0 otherwise
2472 */
2473static int
2474xmlFACompareAtoms(xmlRegAtomPtr atom1, xmlRegAtomPtr atom2) {
2475 int ret = 1;
2476
2477 if (atom1 == atom2)
2478 return(1);
2479 if ((atom1 == NULL) || (atom2 == NULL))
2480 return(0);
2481
2482 if ((atom1->type == XML_REGEXP_ANYCHAR) ||
2483 (atom2->type == XML_REGEXP_ANYCHAR))
2484 return(1);
2485
2486 if (atom1->type > atom2->type) {
Daniel Veillard567a45b2005-10-18 19:11:55 +00002487 xmlRegAtomPtr tmp;
2488 tmp = atom1;
2489 atom1 = atom2;
2490 atom2 = tmp;
Daniel Veillardfc011b72006-02-12 19:14:15 +00002491 }
2492 if (atom1->type != atom2->type) {
2493 ret = xmlFACompareAtomTypes(atom1->type, atom2->type);
2494 /* if they can't intersect at the type level break now */
2495 if (ret == 0)
2496 return(0);
Daniel Veillard567a45b2005-10-18 19:11:55 +00002497 }
Daniel Veillarde19fc232002-04-22 16:01:24 +00002498 switch (atom1->type) {
2499 case XML_REGEXP_STRING:
Daniel Veillard9efc4762005-07-19 14:33:55 +00002500 ret = xmlRegStrEqualWildcard((xmlChar *)atom1->valuep,
2501 (xmlChar *)atom2->valuep);
2502 break;
Daniel Veillarde19fc232002-04-22 16:01:24 +00002503 case XML_REGEXP_EPSILON:
Daniel Veillard567a45b2005-10-18 19:11:55 +00002504 goto not_determinist;
Daniel Veillarde19fc232002-04-22 16:01:24 +00002505 case XML_REGEXP_CHARVAL:
Daniel Veillardfc011b72006-02-12 19:14:15 +00002506 if (atom2->type == XML_REGEXP_CHARVAL) {
2507 ret = (atom1->codepoint == atom2->codepoint);
2508 } else {
2509 ret = xmlRegCheckCharacter(atom2, atom1->codepoint);
2510 if (ret < 0)
2511 ret = 1;
2512 }
Daniel Veillard9efc4762005-07-19 14:33:55 +00002513 break;
Daniel Veillarde19fc232002-04-22 16:01:24 +00002514 case XML_REGEXP_RANGES:
Daniel Veillardfc011b72006-02-12 19:14:15 +00002515 if (atom2->type == XML_REGEXP_RANGES) {
Daniel Veillard567a45b2005-10-18 19:11:55 +00002516 int i, j, res;
2517 xmlRegRangePtr r1, r2;
2518
2519 /*
2520 * need to check that none of the ranges eventually matches
2521 */
2522 for (i = 0;i < atom1->nbRanges;i++) {
2523 for (j = 0;j < atom2->nbRanges;j++) {
2524 r1 = atom1->ranges[i];
2525 r2 = atom2->ranges[j];
2526 res = xmlFACompareRanges(r1, r2);
2527 if (res == 1) {
2528 ret = 1;
2529 goto done;
2530 }
2531 }
2532 }
2533 ret = 0;
2534 }
2535 break;
Daniel Veillarde19fc232002-04-22 16:01:24 +00002536 default:
Daniel Veillard567a45b2005-10-18 19:11:55 +00002537 goto not_determinist;
Daniel Veillarde19fc232002-04-22 16:01:24 +00002538 }
Daniel Veillard567a45b2005-10-18 19:11:55 +00002539done:
Daniel Veillard6e65e152005-08-09 11:09:52 +00002540 if (atom1->neg != atom2->neg) {
Daniel Veillard9efc4762005-07-19 14:33:55 +00002541 ret = !ret;
Daniel Veillard6e65e152005-08-09 11:09:52 +00002542 }
Daniel Veillard567a45b2005-10-18 19:11:55 +00002543 if (ret == 0)
2544 return(0);
2545not_determinist:
2546 return(1);
Daniel Veillarde19fc232002-04-22 16:01:24 +00002547}
2548
2549/**
2550 * xmlFARecurseDeterminism:
2551 * @ctxt: a regexp parser context
2552 *
2553 * Check whether the associated regexp is determinist,
2554 * should be called after xmlFAEliminateEpsilonTransitions()
2555 *
2556 */
2557static int
2558xmlFARecurseDeterminism(xmlRegParserCtxtPtr ctxt, xmlRegStatePtr state,
2559 int to, xmlRegAtomPtr atom) {
2560 int ret = 1;
Daniel Veillard567a45b2005-10-18 19:11:55 +00002561 int res;
Daniel Veillard5de09382005-09-26 17:18:17 +00002562 int transnr, nbTrans;
Daniel Veillarde19fc232002-04-22 16:01:24 +00002563 xmlRegTransPtr t1;
2564
2565 if (state == NULL)
2566 return(ret);
Daniel Veillard5de09382005-09-26 17:18:17 +00002567 /*
2568 * don't recurse on transitions potentially added in the course of
2569 * the elimination.
2570 */
2571 nbTrans = state->nbTrans;
2572 for (transnr = 0;transnr < nbTrans;transnr++) {
Daniel Veillarde19fc232002-04-22 16:01:24 +00002573 t1 = &(state->trans[transnr]);
2574 /*
2575 * check transitions conflicting with the one looked at
2576 */
2577 if (t1->atom == NULL) {
Daniel Veillard0e05f4c2006-11-01 15:33:04 +00002578 if (t1->to < 0)
Daniel Veillarde19fc232002-04-22 16:01:24 +00002579 continue;
Daniel Veillard567a45b2005-10-18 19:11:55 +00002580 res = xmlFARecurseDeterminism(ctxt, ctxt->states[t1->to],
Daniel Veillarde19fc232002-04-22 16:01:24 +00002581 to, atom);
Daniel Veillard567a45b2005-10-18 19:11:55 +00002582 if (res == 0) {
2583 ret = 0;
Daniel Veillardaa622012005-10-20 15:55:25 +00002584 /* t1->nd = 1; */
Daniel Veillard567a45b2005-10-18 19:11:55 +00002585 }
Daniel Veillarde19fc232002-04-22 16:01:24 +00002586 continue;
2587 }
2588 if (t1->to != to)
2589 continue;
Daniel Veillard567a45b2005-10-18 19:11:55 +00002590 if (xmlFACompareAtoms(t1->atom, atom)) {
2591 ret = 0;
2592 /* mark the transition as non-deterministic */
2593 t1->nd = 1;
2594 }
Daniel Veillarde19fc232002-04-22 16:01:24 +00002595 }
2596 return(ret);
2597}
2598
2599/**
2600 * xmlFAComputesDeterminism:
2601 * @ctxt: a regexp parser context
2602 *
2603 * Check whether the associated regexp is determinist,
2604 * should be called after xmlFAEliminateEpsilonTransitions()
2605 *
2606 */
2607static int
2608xmlFAComputesDeterminism(xmlRegParserCtxtPtr ctxt) {
2609 int statenr, transnr;
2610 xmlRegStatePtr state;
Daniel Veillard567a45b2005-10-18 19:11:55 +00002611 xmlRegTransPtr t1, t2, last;
Daniel Veillarde19fc232002-04-22 16:01:24 +00002612 int i;
2613 int ret = 1;
2614
Daniel Veillard4402ab42002-09-12 16:02:56 +00002615#ifdef DEBUG_REGEXP_GRAPH
2616 printf("xmlFAComputesDeterminism\n");
2617 xmlRegPrintCtxt(stdout, ctxt);
2618#endif
Daniel Veillarde19fc232002-04-22 16:01:24 +00002619 if (ctxt->determinist != -1)
2620 return(ctxt->determinist);
2621
2622 /*
Daniel Veillard567a45b2005-10-18 19:11:55 +00002623 * First cleanup the automata removing cancelled transitions
Daniel Veillarde19fc232002-04-22 16:01:24 +00002624 */
2625 for (statenr = 0;statenr < ctxt->nbStates;statenr++) {
2626 state = ctxt->states[statenr];
2627 if (state == NULL)
2628 continue;
Daniel Veillard4f82c8a2005-08-09 21:40:08 +00002629 if (state->nbTrans < 2)
2630 continue;
Daniel Veillarde19fc232002-04-22 16:01:24 +00002631 for (transnr = 0;transnr < state->nbTrans;transnr++) {
2632 t1 = &(state->trans[transnr]);
2633 /*
2634 * Determinism checks in case of counted or all transitions
2635 * will have to be handled separately
2636 */
Daniel Veillard567a45b2005-10-18 19:11:55 +00002637 if (t1->atom == NULL) {
Daniel Veillardaa622012005-10-20 15:55:25 +00002638 /* t1->nd = 1; */
Daniel Veillarde19fc232002-04-22 16:01:24 +00002639 continue;
Daniel Veillard567a45b2005-10-18 19:11:55 +00002640 }
Daniel Veillarde19fc232002-04-22 16:01:24 +00002641 if (t1->to == -1) /* eliminated */
2642 continue;
2643 for (i = 0;i < transnr;i++) {
2644 t2 = &(state->trans[i]);
2645 if (t2->to == -1) /* eliminated */
2646 continue;
2647 if (t2->atom != NULL) {
2648 if (t1->to == t2->to) {
Daniel Veillardfc011b72006-02-12 19:14:15 +00002649 if (xmlFAEqualAtoms(t1->atom, t2->atom))
William M. Brackddf71d62004-05-06 04:17:26 +00002650 t2->to = -1; /* eliminated */
Daniel Veillard567a45b2005-10-18 19:11:55 +00002651 }
2652 }
2653 }
2654 }
2655 }
2656
2657 /*
2658 * Check for all states that there aren't 2 transitions
2659 * with the same atom and a different target.
2660 */
2661 for (statenr = 0;statenr < ctxt->nbStates;statenr++) {
2662 state = ctxt->states[statenr];
2663 if (state == NULL)
2664 continue;
2665 if (state->nbTrans < 2)
2666 continue;
2667 last = NULL;
2668 for (transnr = 0;transnr < state->nbTrans;transnr++) {
2669 t1 = &(state->trans[transnr]);
2670 /*
2671 * Determinism checks in case of counted or all transitions
2672 * will have to be handled separately
2673 */
2674 if (t1->atom == NULL) {
2675 continue;
2676 }
2677 if (t1->to == -1) /* eliminated */
2678 continue;
2679 for (i = 0;i < transnr;i++) {
2680 t2 = &(state->trans[i]);
2681 if (t2->to == -1) /* eliminated */
2682 continue;
2683 if (t2->atom != NULL) {
2684 /* not determinist ! */
2685 if (xmlFACompareAtoms(t1->atom, t2->atom)) {
2686 ret = 0;
2687 /* mark the transitions as non-deterministic ones */
2688 t1->nd = 1;
2689 t2->nd = 1;
2690 last = t1;
Daniel Veillarde19fc232002-04-22 16:01:24 +00002691 }
2692 } else if (t1->to != -1) {
2693 /*
2694 * do the closure in case of remaining specific
2695 * epsilon transitions like choices or all
2696 */
2697 ret = xmlFARecurseDeterminism(ctxt, ctxt->states[t1->to],
2698 t2->to, t2->atom);
Daniel Veillard567a45b2005-10-18 19:11:55 +00002699 /* don't shortcut the computation so all non deterministic
2700 transition get marked down
Daniel Veillarde19fc232002-04-22 16:01:24 +00002701 if (ret == 0)
Daniel Veillardaa622012005-10-20 15:55:25 +00002702 return(0);
2703 */
Daniel Veillard567a45b2005-10-18 19:11:55 +00002704 if (ret == 0) {
2705 t1->nd = 1;
Daniel Veillardaa622012005-10-20 15:55:25 +00002706 /* t2->nd = 1; */
Daniel Veillard567a45b2005-10-18 19:11:55 +00002707 last = t1;
2708 }
Daniel Veillarde19fc232002-04-22 16:01:24 +00002709 }
2710 }
Daniel Veillard567a45b2005-10-18 19:11:55 +00002711 /* don't shortcut the computation so all non deterministic
2712 transition get marked down
Daniel Veillarde19fc232002-04-22 16:01:24 +00002713 if (ret == 0)
Daniel Veillard567a45b2005-10-18 19:11:55 +00002714 break; */
Daniel Veillarde19fc232002-04-22 16:01:24 +00002715 }
Daniel Veillard567a45b2005-10-18 19:11:55 +00002716
2717 /*
2718 * mark specifically the last non-deterministic transition
2719 * from a state since there is no need to set-up rollback
2720 * from it
2721 */
2722 if (last != NULL) {
2723 last->nd = 2;
2724 }
2725
2726 /* don't shortcut the computation so all non deterministic
2727 transition get marked down
Daniel Veillarde19fc232002-04-22 16:01:24 +00002728 if (ret == 0)
Daniel Veillard567a45b2005-10-18 19:11:55 +00002729 break; */
Daniel Veillarde19fc232002-04-22 16:01:24 +00002730 }
Daniel Veillard567a45b2005-10-18 19:11:55 +00002731
Daniel Veillarde19fc232002-04-22 16:01:24 +00002732 ctxt->determinist = ret;
2733 return(ret);
2734}
2735
Daniel Veillard4255d502002-04-16 15:50:10 +00002736/************************************************************************
2737 * *
2738 * Routines to check input against transition atoms *
2739 * *
2740 ************************************************************************/
2741
2742static int
2743xmlRegCheckCharacterRange(xmlRegAtomType type, int codepoint, int neg,
2744 int start, int end, const xmlChar *blockName) {
2745 int ret = 0;
2746
2747 switch (type) {
2748 case XML_REGEXP_STRING:
2749 case XML_REGEXP_SUBREG:
2750 case XML_REGEXP_RANGES:
2751 case XML_REGEXP_EPSILON:
2752 return(-1);
2753 case XML_REGEXP_ANYCHAR:
2754 ret = ((codepoint != '\n') && (codepoint != '\r'));
2755 break;
2756 case XML_REGEXP_CHARVAL:
2757 ret = ((codepoint >= start) && (codepoint <= end));
2758 break;
2759 case XML_REGEXP_NOTSPACE:
2760 neg = !neg;
2761 case XML_REGEXP_ANYSPACE:
2762 ret = ((codepoint == '\n') || (codepoint == '\r') ||
2763 (codepoint == '\t') || (codepoint == ' '));
2764 break;
2765 case XML_REGEXP_NOTINITNAME:
2766 neg = !neg;
2767 case XML_REGEXP_INITNAME:
William M. Brack871611b2003-10-18 04:53:14 +00002768 ret = (IS_LETTER(codepoint) ||
Daniel Veillard4255d502002-04-16 15:50:10 +00002769 (codepoint == '_') || (codepoint == ':'));
2770 break;
2771 case XML_REGEXP_NOTNAMECHAR:
2772 neg = !neg;
2773 case XML_REGEXP_NAMECHAR:
William M. Brack871611b2003-10-18 04:53:14 +00002774 ret = (IS_LETTER(codepoint) || IS_DIGIT(codepoint) ||
Daniel Veillard4255d502002-04-16 15:50:10 +00002775 (codepoint == '.') || (codepoint == '-') ||
2776 (codepoint == '_') || (codepoint == ':') ||
William M. Brack871611b2003-10-18 04:53:14 +00002777 IS_COMBINING(codepoint) || IS_EXTENDER(codepoint));
Daniel Veillard4255d502002-04-16 15:50:10 +00002778 break;
2779 case XML_REGEXP_NOTDECIMAL:
2780 neg = !neg;
2781 case XML_REGEXP_DECIMAL:
2782 ret = xmlUCSIsCatNd(codepoint);
2783 break;
2784 case XML_REGEXP_REALCHAR:
2785 neg = !neg;
2786 case XML_REGEXP_NOTREALCHAR:
2787 ret = xmlUCSIsCatP(codepoint);
2788 if (ret == 0)
2789 ret = xmlUCSIsCatZ(codepoint);
2790 if (ret == 0)
2791 ret = xmlUCSIsCatC(codepoint);
2792 break;
2793 case XML_REGEXP_LETTER:
2794 ret = xmlUCSIsCatL(codepoint);
2795 break;
2796 case XML_REGEXP_LETTER_UPPERCASE:
2797 ret = xmlUCSIsCatLu(codepoint);
2798 break;
2799 case XML_REGEXP_LETTER_LOWERCASE:
2800 ret = xmlUCSIsCatLl(codepoint);
2801 break;
2802 case XML_REGEXP_LETTER_TITLECASE:
2803 ret = xmlUCSIsCatLt(codepoint);
2804 break;
2805 case XML_REGEXP_LETTER_MODIFIER:
2806 ret = xmlUCSIsCatLm(codepoint);
2807 break;
2808 case XML_REGEXP_LETTER_OTHERS:
2809 ret = xmlUCSIsCatLo(codepoint);
2810 break;
2811 case XML_REGEXP_MARK:
2812 ret = xmlUCSIsCatM(codepoint);
2813 break;
2814 case XML_REGEXP_MARK_NONSPACING:
2815 ret = xmlUCSIsCatMn(codepoint);
2816 break;
2817 case XML_REGEXP_MARK_SPACECOMBINING:
2818 ret = xmlUCSIsCatMc(codepoint);
2819 break;
2820 case XML_REGEXP_MARK_ENCLOSING:
2821 ret = xmlUCSIsCatMe(codepoint);
2822 break;
2823 case XML_REGEXP_NUMBER:
2824 ret = xmlUCSIsCatN(codepoint);
2825 break;
2826 case XML_REGEXP_NUMBER_DECIMAL:
2827 ret = xmlUCSIsCatNd(codepoint);
2828 break;
2829 case XML_REGEXP_NUMBER_LETTER:
2830 ret = xmlUCSIsCatNl(codepoint);
2831 break;
2832 case XML_REGEXP_NUMBER_OTHERS:
2833 ret = xmlUCSIsCatNo(codepoint);
2834 break;
2835 case XML_REGEXP_PUNCT:
2836 ret = xmlUCSIsCatP(codepoint);
2837 break;
2838 case XML_REGEXP_PUNCT_CONNECTOR:
2839 ret = xmlUCSIsCatPc(codepoint);
2840 break;
2841 case XML_REGEXP_PUNCT_DASH:
2842 ret = xmlUCSIsCatPd(codepoint);
2843 break;
2844 case XML_REGEXP_PUNCT_OPEN:
2845 ret = xmlUCSIsCatPs(codepoint);
2846 break;
2847 case XML_REGEXP_PUNCT_CLOSE:
2848 ret = xmlUCSIsCatPe(codepoint);
2849 break;
2850 case XML_REGEXP_PUNCT_INITQUOTE:
2851 ret = xmlUCSIsCatPi(codepoint);
2852 break;
2853 case XML_REGEXP_PUNCT_FINQUOTE:
2854 ret = xmlUCSIsCatPf(codepoint);
2855 break;
2856 case XML_REGEXP_PUNCT_OTHERS:
2857 ret = xmlUCSIsCatPo(codepoint);
2858 break;
2859 case XML_REGEXP_SEPAR:
2860 ret = xmlUCSIsCatZ(codepoint);
2861 break;
2862 case XML_REGEXP_SEPAR_SPACE:
2863 ret = xmlUCSIsCatZs(codepoint);
2864 break;
2865 case XML_REGEXP_SEPAR_LINE:
2866 ret = xmlUCSIsCatZl(codepoint);
2867 break;
2868 case XML_REGEXP_SEPAR_PARA:
2869 ret = xmlUCSIsCatZp(codepoint);
2870 break;
2871 case XML_REGEXP_SYMBOL:
2872 ret = xmlUCSIsCatS(codepoint);
2873 break;
2874 case XML_REGEXP_SYMBOL_MATH:
2875 ret = xmlUCSIsCatSm(codepoint);
2876 break;
2877 case XML_REGEXP_SYMBOL_CURRENCY:
2878 ret = xmlUCSIsCatSc(codepoint);
2879 break;
2880 case XML_REGEXP_SYMBOL_MODIFIER:
2881 ret = xmlUCSIsCatSk(codepoint);
2882 break;
2883 case XML_REGEXP_SYMBOL_OTHERS:
2884 ret = xmlUCSIsCatSo(codepoint);
2885 break;
2886 case XML_REGEXP_OTHER:
2887 ret = xmlUCSIsCatC(codepoint);
2888 break;
2889 case XML_REGEXP_OTHER_CONTROL:
2890 ret = xmlUCSIsCatCc(codepoint);
2891 break;
2892 case XML_REGEXP_OTHER_FORMAT:
2893 ret = xmlUCSIsCatCf(codepoint);
2894 break;
2895 case XML_REGEXP_OTHER_PRIVATE:
2896 ret = xmlUCSIsCatCo(codepoint);
2897 break;
2898 case XML_REGEXP_OTHER_NA:
2899 /* ret = xmlUCSIsCatCn(codepoint); */
2900 /* Seems it doesn't exist anymore in recent Unicode releases */
2901 ret = 0;
2902 break;
2903 case XML_REGEXP_BLOCK_NAME:
2904 ret = xmlUCSIsBlock(codepoint, (const char *) blockName);
2905 break;
2906 }
2907 if (neg)
2908 return(!ret);
2909 return(ret);
2910}
2911
2912static int
2913xmlRegCheckCharacter(xmlRegAtomPtr atom, int codepoint) {
2914 int i, ret = 0;
2915 xmlRegRangePtr range;
2916
William M. Brack871611b2003-10-18 04:53:14 +00002917 if ((atom == NULL) || (!IS_CHAR(codepoint)))
Daniel Veillard4255d502002-04-16 15:50:10 +00002918 return(-1);
2919
2920 switch (atom->type) {
2921 case XML_REGEXP_SUBREG:
2922 case XML_REGEXP_EPSILON:
2923 return(-1);
2924 case XML_REGEXP_CHARVAL:
2925 return(codepoint == atom->codepoint);
2926 case XML_REGEXP_RANGES: {
2927 int accept = 0;
Daniel Veillardf2a12832003-11-24 13:04:35 +00002928
Daniel Veillard4255d502002-04-16 15:50:10 +00002929 for (i = 0;i < atom->nbRanges;i++) {
2930 range = atom->ranges[i];
Daniel Veillardf8b9de32003-11-24 14:27:26 +00002931 if (range->neg == 2) {
Daniel Veillard4255d502002-04-16 15:50:10 +00002932 ret = xmlRegCheckCharacterRange(range->type, codepoint,
2933 0, range->start, range->end,
2934 range->blockName);
2935 if (ret != 0)
2936 return(0); /* excluded char */
Daniel Veillardf8b9de32003-11-24 14:27:26 +00002937 } else if (range->neg) {
2938 ret = xmlRegCheckCharacterRange(range->type, codepoint,
2939 0, range->start, range->end,
2940 range->blockName);
2941 if (ret == 0)
Daniel Veillardf2a12832003-11-24 13:04:35 +00002942 accept = 1;
Daniel Veillardf8b9de32003-11-24 14:27:26 +00002943 else
2944 return(0);
Daniel Veillard4255d502002-04-16 15:50:10 +00002945 } else {
2946 ret = xmlRegCheckCharacterRange(range->type, codepoint,
2947 0, range->start, range->end,
2948 range->blockName);
2949 if (ret != 0)
2950 accept = 1; /* might still be excluded */
2951 }
2952 }
2953 return(accept);
2954 }
2955 case XML_REGEXP_STRING:
2956 printf("TODO: XML_REGEXP_STRING\n");
2957 return(-1);
2958 case XML_REGEXP_ANYCHAR:
2959 case XML_REGEXP_ANYSPACE:
2960 case XML_REGEXP_NOTSPACE:
2961 case XML_REGEXP_INITNAME:
2962 case XML_REGEXP_NOTINITNAME:
2963 case XML_REGEXP_NAMECHAR:
2964 case XML_REGEXP_NOTNAMECHAR:
2965 case XML_REGEXP_DECIMAL:
2966 case XML_REGEXP_NOTDECIMAL:
2967 case XML_REGEXP_REALCHAR:
2968 case XML_REGEXP_NOTREALCHAR:
2969 case XML_REGEXP_LETTER:
2970 case XML_REGEXP_LETTER_UPPERCASE:
2971 case XML_REGEXP_LETTER_LOWERCASE:
2972 case XML_REGEXP_LETTER_TITLECASE:
2973 case XML_REGEXP_LETTER_MODIFIER:
2974 case XML_REGEXP_LETTER_OTHERS:
2975 case XML_REGEXP_MARK:
2976 case XML_REGEXP_MARK_NONSPACING:
2977 case XML_REGEXP_MARK_SPACECOMBINING:
2978 case XML_REGEXP_MARK_ENCLOSING:
2979 case XML_REGEXP_NUMBER:
2980 case XML_REGEXP_NUMBER_DECIMAL:
2981 case XML_REGEXP_NUMBER_LETTER:
2982 case XML_REGEXP_NUMBER_OTHERS:
2983 case XML_REGEXP_PUNCT:
2984 case XML_REGEXP_PUNCT_CONNECTOR:
2985 case XML_REGEXP_PUNCT_DASH:
2986 case XML_REGEXP_PUNCT_OPEN:
2987 case XML_REGEXP_PUNCT_CLOSE:
2988 case XML_REGEXP_PUNCT_INITQUOTE:
2989 case XML_REGEXP_PUNCT_FINQUOTE:
2990 case XML_REGEXP_PUNCT_OTHERS:
2991 case XML_REGEXP_SEPAR:
2992 case XML_REGEXP_SEPAR_SPACE:
2993 case XML_REGEXP_SEPAR_LINE:
2994 case XML_REGEXP_SEPAR_PARA:
2995 case XML_REGEXP_SYMBOL:
2996 case XML_REGEXP_SYMBOL_MATH:
2997 case XML_REGEXP_SYMBOL_CURRENCY:
2998 case XML_REGEXP_SYMBOL_MODIFIER:
2999 case XML_REGEXP_SYMBOL_OTHERS:
3000 case XML_REGEXP_OTHER:
3001 case XML_REGEXP_OTHER_CONTROL:
3002 case XML_REGEXP_OTHER_FORMAT:
3003 case XML_REGEXP_OTHER_PRIVATE:
3004 case XML_REGEXP_OTHER_NA:
3005 case XML_REGEXP_BLOCK_NAME:
3006 ret = xmlRegCheckCharacterRange(atom->type, codepoint, 0, 0, 0,
3007 (const xmlChar *)atom->valuep);
3008 if (atom->neg)
3009 ret = !ret;
3010 break;
3011 }
3012 return(ret);
3013}
3014
3015/************************************************************************
3016 * *
William M. Brackddf71d62004-05-06 04:17:26 +00003017 * Saving and restoring state of an execution context *
Daniel Veillard4255d502002-04-16 15:50:10 +00003018 * *
3019 ************************************************************************/
3020
3021#ifdef DEBUG_REGEXP_EXEC
3022static void
3023xmlFARegDebugExec(xmlRegExecCtxtPtr exec) {
3024 printf("state: %d:%d:idx %d", exec->state->no, exec->transno, exec->index);
3025 if (exec->inputStack != NULL) {
3026 int i;
3027 printf(": ");
3028 for (i = 0;(i < 3) && (i < exec->inputStackNr);i++)
Daniel Veillard0e05f4c2006-11-01 15:33:04 +00003029 printf("%s ", (const char *)
3030 exec->inputStack[exec->inputStackNr - (i + 1)].value);
Daniel Veillard4255d502002-04-16 15:50:10 +00003031 } else {
3032 printf(": %s", &(exec->inputString[exec->index]));
3033 }
3034 printf("\n");
3035}
3036#endif
3037
3038static void
3039xmlFARegExecSave(xmlRegExecCtxtPtr exec) {
3040#ifdef DEBUG_REGEXP_EXEC
3041 printf("saving ");
3042 exec->transno++;
3043 xmlFARegDebugExec(exec);
3044 exec->transno--;
3045#endif
Daniel Veillard94cc1032005-09-15 13:09:00 +00003046#ifdef MAX_PUSH
3047 if (exec->nbPush > MAX_PUSH) {
3048 return;
3049 }
3050 exec->nbPush++;
3051#endif
Daniel Veillard4255d502002-04-16 15:50:10 +00003052
3053 if (exec->maxRollbacks == 0) {
3054 exec->maxRollbacks = 4;
3055 exec->rollbacks = (xmlRegExecRollback *) xmlMalloc(exec->maxRollbacks *
3056 sizeof(xmlRegExecRollback));
3057 if (exec->rollbacks == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00003058 xmlRegexpErrMemory(NULL, "saving regexp");
Daniel Veillard4255d502002-04-16 15:50:10 +00003059 exec->maxRollbacks = 0;
3060 return;
3061 }
3062 memset(exec->rollbacks, 0,
3063 exec->maxRollbacks * sizeof(xmlRegExecRollback));
3064 } else if (exec->nbRollbacks >= exec->maxRollbacks) {
3065 xmlRegExecRollback *tmp;
3066 int len = exec->maxRollbacks;
3067
3068 exec->maxRollbacks *= 2;
3069 tmp = (xmlRegExecRollback *) xmlRealloc(exec->rollbacks,
3070 exec->maxRollbacks * sizeof(xmlRegExecRollback));
3071 if (tmp == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00003072 xmlRegexpErrMemory(NULL, "saving regexp");
Daniel Veillard4255d502002-04-16 15:50:10 +00003073 exec->maxRollbacks /= 2;
3074 return;
3075 }
3076 exec->rollbacks = tmp;
3077 tmp = &exec->rollbacks[len];
3078 memset(tmp, 0, (exec->maxRollbacks - len) * sizeof(xmlRegExecRollback));
3079 }
3080 exec->rollbacks[exec->nbRollbacks].state = exec->state;
3081 exec->rollbacks[exec->nbRollbacks].index = exec->index;
3082 exec->rollbacks[exec->nbRollbacks].nextbranch = exec->transno + 1;
3083 if (exec->comp->nbCounters > 0) {
3084 if (exec->rollbacks[exec->nbRollbacks].counts == NULL) {
3085 exec->rollbacks[exec->nbRollbacks].counts = (int *)
3086 xmlMalloc(exec->comp->nbCounters * sizeof(int));
3087 if (exec->rollbacks[exec->nbRollbacks].counts == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00003088 xmlRegexpErrMemory(NULL, "saving regexp");
Daniel Veillard4255d502002-04-16 15:50:10 +00003089 exec->status = -5;
3090 return;
3091 }
3092 }
3093 memcpy(exec->rollbacks[exec->nbRollbacks].counts, exec->counts,
3094 exec->comp->nbCounters * sizeof(int));
3095 }
3096 exec->nbRollbacks++;
3097}
3098
3099static void
3100xmlFARegExecRollBack(xmlRegExecCtxtPtr exec) {
3101 if (exec->nbRollbacks <= 0) {
3102 exec->status = -1;
3103#ifdef DEBUG_REGEXP_EXEC
3104 printf("rollback failed on empty stack\n");
3105#endif
3106 return;
3107 }
3108 exec->nbRollbacks--;
3109 exec->state = exec->rollbacks[exec->nbRollbacks].state;
3110 exec->index = exec->rollbacks[exec->nbRollbacks].index;
3111 exec->transno = exec->rollbacks[exec->nbRollbacks].nextbranch;
3112 if (exec->comp->nbCounters > 0) {
3113 if (exec->rollbacks[exec->nbRollbacks].counts == NULL) {
3114 fprintf(stderr, "exec save: allocation failed");
3115 exec->status = -6;
3116 return;
3117 }
3118 memcpy(exec->counts, exec->rollbacks[exec->nbRollbacks].counts,
3119 exec->comp->nbCounters * sizeof(int));
3120 }
3121
3122#ifdef DEBUG_REGEXP_EXEC
3123 printf("restored ");
3124 xmlFARegDebugExec(exec);
3125#endif
3126}
3127
3128/************************************************************************
3129 * *
William M. Brackddf71d62004-05-06 04:17:26 +00003130 * Verifier, running an input against a compiled regexp *
Daniel Veillard4255d502002-04-16 15:50:10 +00003131 * *
3132 ************************************************************************/
3133
3134static int
3135xmlFARegExec(xmlRegexpPtr comp, const xmlChar *content) {
3136 xmlRegExecCtxt execval;
3137 xmlRegExecCtxtPtr exec = &execval;
Daniel Veillard567a45b2005-10-18 19:11:55 +00003138 int ret, codepoint = 0, len, deter;
Daniel Veillard4255d502002-04-16 15:50:10 +00003139
3140 exec->inputString = content;
3141 exec->index = 0;
Daniel Veillard94cc1032005-09-15 13:09:00 +00003142 exec->nbPush = 0;
Daniel Veillard4255d502002-04-16 15:50:10 +00003143 exec->determinist = 1;
3144 exec->maxRollbacks = 0;
3145 exec->nbRollbacks = 0;
3146 exec->rollbacks = NULL;
3147 exec->status = 0;
3148 exec->comp = comp;
3149 exec->state = comp->states[0];
3150 exec->transno = 0;
3151 exec->transcount = 0;
Daniel Veillardf2a12832003-11-24 13:04:35 +00003152 exec->inputStack = NULL;
3153 exec->inputStackMax = 0;
Daniel Veillard4255d502002-04-16 15:50:10 +00003154 if (comp->nbCounters > 0) {
3155 exec->counts = (int *) xmlMalloc(comp->nbCounters * sizeof(int));
Daniel Veillardff46a042003-10-08 08:53:17 +00003156 if (exec->counts == NULL) {
3157 xmlRegexpErrMemory(NULL, "running regexp");
Daniel Veillard4255d502002-04-16 15:50:10 +00003158 return(-1);
Daniel Veillardff46a042003-10-08 08:53:17 +00003159 }
Daniel Veillard4255d502002-04-16 15:50:10 +00003160 memset(exec->counts, 0, comp->nbCounters * sizeof(int));
3161 } else
3162 exec->counts = NULL;
3163 while ((exec->status == 0) &&
3164 ((exec->inputString[exec->index] != 0) ||
Daniel Veillardad559982008-05-12 13:15:35 +00003165 ((exec->state != NULL) &&
3166 (exec->state->type != XML_REGEXP_FINAL_STATE)))) {
Daniel Veillard4255d502002-04-16 15:50:10 +00003167 xmlRegTransPtr trans;
3168 xmlRegAtomPtr atom;
3169
3170 /*
William M. Brack0e00b282004-04-26 15:40:47 +00003171 * If end of input on non-terminal state, rollback, however we may
Daniel Veillard4255d502002-04-16 15:50:10 +00003172 * still have epsilon like transition for counted transitions
William M. Brack0e00b282004-04-26 15:40:47 +00003173 * on counters, in that case don't break too early. Additionally,
3174 * if we are working on a range like "AB{0,2}", where B is not present,
3175 * we don't want to break.
Daniel Veillard4255d502002-04-16 15:50:10 +00003176 */
Daniel Veillard11ce4002006-03-10 00:36:23 +00003177 len = 1;
William M. Brack0e00b282004-04-26 15:40:47 +00003178 if ((exec->inputString[exec->index] == 0) && (exec->counts == NULL)) {
William M. Brackddf71d62004-05-06 04:17:26 +00003179 /*
3180 * if there is a transition, we must check if
3181 * atom allows minOccurs of 0
3182 */
3183 if (exec->transno < exec->state->nbTrans) {
William M. Brack0e00b282004-04-26 15:40:47 +00003184 trans = &exec->state->trans[exec->transno];
3185 if (trans->to >=0) {
3186 atom = trans->atom;
3187 if (!((atom->min == 0) && (atom->max > 0)))
3188 goto rollback;
3189 }
3190 } else
3191 goto rollback;
3192 }
Daniel Veillard4255d502002-04-16 15:50:10 +00003193
3194 exec->transcount = 0;
3195 for (;exec->transno < exec->state->nbTrans;exec->transno++) {
3196 trans = &exec->state->trans[exec->transno];
3197 if (trans->to < 0)
3198 continue;
3199 atom = trans->atom;
3200 ret = 0;
Daniel Veillard567a45b2005-10-18 19:11:55 +00003201 deter = 1;
Daniel Veillard4255d502002-04-16 15:50:10 +00003202 if (trans->count >= 0) {
3203 int count;
3204 xmlRegCounterPtr counter;
3205
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 /*
3211 * A counted transition.
3212 */
3213
3214 count = exec->counts[trans->count];
3215 counter = &exec->comp->counters[trans->count];
3216#ifdef DEBUG_REGEXP_EXEC
3217 printf("testing count %d: val %d, min %d, max %d\n",
3218 trans->count, count, counter->min, counter->max);
3219#endif
3220 ret = ((count >= counter->min) && (count <= counter->max));
Daniel Veillard567a45b2005-10-18 19:11:55 +00003221 if ((ret) && (counter->min != counter->max))
3222 deter = 0;
Daniel Veillard4255d502002-04-16 15:50:10 +00003223 } else if (atom == NULL) {
3224 fprintf(stderr, "epsilon transition left at runtime\n");
3225 exec->status = -2;
3226 break;
3227 } else if (exec->inputString[exec->index] != 0) {
3228 codepoint = CUR_SCHAR(&(exec->inputString[exec->index]), len);
3229 ret = xmlRegCheckCharacter(atom, codepoint);
William M. Brack0e00b282004-04-26 15:40:47 +00003230 if ((ret == 1) && (atom->min >= 0) && (atom->max > 0)) {
Daniel Veillard4255d502002-04-16 15:50:10 +00003231 xmlRegStatePtr to = comp->states[trans->to];
3232
3233 /*
3234 * this is a multiple input sequence
Daniel Veillardfc6eca02005-11-01 15:24:02 +00003235 * If there is a counter associated increment it now.
3236 * before potentially saving and rollback
Daniel Veillardc821e032007-08-28 17:33:45 +00003237 * do not increment if the counter is already over the
3238 * maximum limit in which case get to next transition
Daniel Veillard4255d502002-04-16 15:50:10 +00003239 */
Daniel Veillardfc6eca02005-11-01 15:24:02 +00003240 if (trans->counter >= 0) {
Daniel Veillardc821e032007-08-28 17:33:45 +00003241 xmlRegCounterPtr counter;
3242
3243 if ((exec->counts == NULL) ||
3244 (exec->comp == NULL) ||
3245 (exec->comp->counters == NULL)) {
Daniel Veillard11ce4002006-03-10 00:36:23 +00003246 exec->status = -1;
3247 goto error;
3248 }
Daniel Veillardc821e032007-08-28 17:33:45 +00003249 counter = &exec->comp->counters[trans->counter];
3250 if (exec->counts[trans->counter] >= counter->max)
3251 continue; /* for loop on transitions */
3252
Daniel Veillardfc6eca02005-11-01 15:24:02 +00003253#ifdef DEBUG_REGEXP_EXEC
3254 printf("Increasing count %d\n", trans->counter);
3255#endif
3256 exec->counts[trans->counter]++;
3257 }
Daniel Veillard4255d502002-04-16 15:50:10 +00003258 if (exec->state->nbTrans > exec->transno + 1) {
3259 xmlFARegExecSave(exec);
3260 }
3261 exec->transcount = 1;
3262 do {
3263 /*
3264 * Try to progress as much as possible on the input
3265 */
3266 if (exec->transcount == atom->max) {
3267 break;
3268 }
3269 exec->index += len;
3270 /*
3271 * End of input: stop here
3272 */
3273 if (exec->inputString[exec->index] == 0) {
3274 exec->index -= len;
3275 break;
3276 }
3277 if (exec->transcount >= atom->min) {
3278 int transno = exec->transno;
3279 xmlRegStatePtr state = exec->state;
3280
3281 /*
3282 * The transition is acceptable save it
3283 */
3284 exec->transno = -1; /* trick */
3285 exec->state = to;
3286 xmlFARegExecSave(exec);
3287 exec->transno = transno;
3288 exec->state = state;
3289 }
3290 codepoint = CUR_SCHAR(&(exec->inputString[exec->index]),
3291 len);
3292 ret = xmlRegCheckCharacter(atom, codepoint);
3293 exec->transcount++;
3294 } while (ret == 1);
3295 if (exec->transcount < atom->min)
3296 ret = 0;
3297
3298 /*
3299 * If the last check failed but one transition was found
3300 * possible, rollback
3301 */
3302 if (ret < 0)
3303 ret = 0;
3304 if (ret == 0) {
3305 goto rollback;
3306 }
Daniel Veillardfc6eca02005-11-01 15:24:02 +00003307 if (trans->counter >= 0) {
Daniel Veillard11ce4002006-03-10 00:36:23 +00003308 if (exec->counts == NULL) {
3309 exec->status = -1;
3310 goto error;
3311 }
Daniel Veillardfc6eca02005-11-01 15:24:02 +00003312#ifdef DEBUG_REGEXP_EXEC
3313 printf("Decreasing count %d\n", trans->counter);
3314#endif
3315 exec->counts[trans->counter]--;
3316 }
William M. Brack0e00b282004-04-26 15:40:47 +00003317 } else if ((ret == 0) && (atom->min == 0) && (atom->max > 0)) {
3318 /*
3319 * we don't match on the codepoint, but minOccurs of 0
3320 * says that's ok. Setting len to 0 inhibits stepping
3321 * over the codepoint.
3322 */
3323 exec->transcount = 1;
3324 len = 0;
3325 ret = 1;
Daniel Veillard4255d502002-04-16 15:50:10 +00003326 }
William M. Brack0e00b282004-04-26 15:40:47 +00003327 } else if ((atom->min == 0) && (atom->max > 0)) {
3328 /* another spot to match when minOccurs is 0 */
3329 exec->transcount = 1;
3330 len = 0;
3331 ret = 1;
Daniel Veillard4255d502002-04-16 15:50:10 +00003332 }
3333 if (ret == 1) {
Daniel Veillard567a45b2005-10-18 19:11:55 +00003334 if ((trans->nd == 1) ||
3335 ((trans->count >= 0) && (deter == 0) &&
3336 (exec->state->nbTrans > exec->transno + 1))) {
Daniel Veillardaa622012005-10-20 15:55:25 +00003337#ifdef DEBUG_REGEXP_EXEC
3338 if (trans->nd == 1)
3339 printf("Saving on nd transition atom %d for %c at %d\n",
3340 trans->atom->no, codepoint, exec->index);
3341 else
3342 printf("Saving on counted transition count %d for %c at %d\n",
3343 trans->count, codepoint, exec->index);
3344#endif
Daniel Veillard4255d502002-04-16 15:50:10 +00003345 xmlFARegExecSave(exec);
3346 }
3347 if (trans->counter >= 0) {
Daniel Veillardc821e032007-08-28 17:33:45 +00003348 xmlRegCounterPtr counter;
3349
3350 /* make sure we don't go over the counter maximum value */
3351 if ((exec->counts == NULL) ||
3352 (exec->comp == NULL) ||
3353 (exec->comp->counters == NULL)) {
3354 exec->status = -1;
Daniel Veillard11ce4002006-03-10 00:36:23 +00003355 goto error;
3356 }
Daniel Veillardc821e032007-08-28 17:33:45 +00003357 counter = &exec->comp->counters[trans->counter];
3358 if (exec->counts[trans->counter] >= counter->max)
3359 continue; /* for loop on transitions */
Daniel Veillard4255d502002-04-16 15:50:10 +00003360#ifdef DEBUG_REGEXP_EXEC
3361 printf("Increasing count %d\n", trans->counter);
3362#endif
3363 exec->counts[trans->counter]++;
3364 }
Daniel Veillard10752282005-08-08 13:05:13 +00003365 if ((trans->count >= 0) &&
3366 (trans->count < REGEXP_ALL_COUNTER)) {
Daniel Veillard11ce4002006-03-10 00:36:23 +00003367 if (exec->counts == NULL) {
3368 exec->status = -1;
3369 goto error;
3370 }
Daniel Veillard10752282005-08-08 13:05:13 +00003371#ifdef DEBUG_REGEXP_EXEC
3372 printf("resetting count %d on transition\n",
3373 trans->count);
3374#endif
3375 exec->counts[trans->count] = 0;
3376 }
Daniel Veillard4255d502002-04-16 15:50:10 +00003377#ifdef DEBUG_REGEXP_EXEC
3378 printf("entering state %d\n", trans->to);
3379#endif
3380 exec->state = comp->states[trans->to];
3381 exec->transno = 0;
3382 if (trans->atom != NULL) {
3383 exec->index += len;
3384 }
3385 goto progress;
3386 } else if (ret < 0) {
3387 exec->status = -4;
3388 break;
3389 }
3390 }
3391 if ((exec->transno != 0) || (exec->state->nbTrans == 0)) {
3392rollback:
3393 /*
3394 * Failed to find a way out
3395 */
3396 exec->determinist = 0;
Daniel Veillardaa622012005-10-20 15:55:25 +00003397#ifdef DEBUG_REGEXP_EXEC
3398 printf("rollback from state %d on %d:%c\n", exec->state->no,
3399 codepoint,codepoint);
3400#endif
Daniel Veillard4255d502002-04-16 15:50:10 +00003401 xmlFARegExecRollBack(exec);
3402 }
3403progress:
3404 continue;
3405 }
Daniel Veillard11ce4002006-03-10 00:36:23 +00003406error:
Daniel Veillard4255d502002-04-16 15:50:10 +00003407 if (exec->rollbacks != NULL) {
3408 if (exec->counts != NULL) {
3409 int i;
3410
3411 for (i = 0;i < exec->maxRollbacks;i++)
3412 if (exec->rollbacks[i].counts != NULL)
3413 xmlFree(exec->rollbacks[i].counts);
3414 }
3415 xmlFree(exec->rollbacks);
3416 }
3417 if (exec->counts != NULL)
3418 xmlFree(exec->counts);
3419 if (exec->status == 0)
3420 return(1);
Daniel Veillard94cc1032005-09-15 13:09:00 +00003421 if (exec->status == -1) {
3422 if (exec->nbPush > MAX_PUSH)
3423 return(-1);
Daniel Veillard4255d502002-04-16 15:50:10 +00003424 return(0);
Daniel Veillard94cc1032005-09-15 13:09:00 +00003425 }
Daniel Veillard4255d502002-04-16 15:50:10 +00003426 return(exec->status);
3427}
3428
3429/************************************************************************
3430 * *
William M. Brackddf71d62004-05-06 04:17:26 +00003431 * Progressive interface to the verifier one atom at a time *
Daniel Veillard4255d502002-04-16 15:50:10 +00003432 * *
3433 ************************************************************************/
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003434#ifdef DEBUG_ERR
3435static void testerr(xmlRegExecCtxtPtr exec);
3436#endif
Daniel Veillard4255d502002-04-16 15:50:10 +00003437
3438/**
Daniel Veillard01c13b52002-12-10 15:19:08 +00003439 * xmlRegNewExecCtxt:
Daniel Veillard4255d502002-04-16 15:50:10 +00003440 * @comp: a precompiled regular expression
3441 * @callback: a callback function used for handling progresses in the
3442 * automata matching phase
3443 * @data: the context data associated to the callback in this context
3444 *
3445 * Build a context used for progressive evaluation of a regexp.
Daniel Veillard01c13b52002-12-10 15:19:08 +00003446 *
3447 * Returns the new context
Daniel Veillard4255d502002-04-16 15:50:10 +00003448 */
3449xmlRegExecCtxtPtr
3450xmlRegNewExecCtxt(xmlRegexpPtr comp, xmlRegExecCallbacks callback, void *data) {
3451 xmlRegExecCtxtPtr exec;
3452
3453 if (comp == NULL)
3454 return(NULL);
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00003455 if ((comp->compact == NULL) && (comp->states == NULL))
3456 return(NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +00003457 exec = (xmlRegExecCtxtPtr) xmlMalloc(sizeof(xmlRegExecCtxt));
3458 if (exec == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00003459 xmlRegexpErrMemory(NULL, "creating execution context");
Daniel Veillard4255d502002-04-16 15:50:10 +00003460 return(NULL);
3461 }
3462 memset(exec, 0, sizeof(xmlRegExecCtxt));
3463 exec->inputString = NULL;
3464 exec->index = 0;
3465 exec->determinist = 1;
3466 exec->maxRollbacks = 0;
3467 exec->nbRollbacks = 0;
3468 exec->rollbacks = NULL;
3469 exec->status = 0;
3470 exec->comp = comp;
Daniel Veillard23e73572002-09-19 19:56:43 +00003471 if (comp->compact == NULL)
3472 exec->state = comp->states[0];
Daniel Veillard4255d502002-04-16 15:50:10 +00003473 exec->transno = 0;
3474 exec->transcount = 0;
3475 exec->callback = callback;
3476 exec->data = data;
3477 if (comp->nbCounters > 0) {
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003478 /*
3479 * For error handling, exec->counts is allocated twice the size
3480 * the second half is used to store the data in case of rollback
3481 */
3482 exec->counts = (int *) xmlMalloc(comp->nbCounters * sizeof(int)
3483 * 2);
Daniel Veillard4255d502002-04-16 15:50:10 +00003484 if (exec->counts == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00003485 xmlRegexpErrMemory(NULL, "creating execution context");
Daniel Veillard4255d502002-04-16 15:50:10 +00003486 xmlFree(exec);
3487 return(NULL);
3488 }
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003489 memset(exec->counts, 0, comp->nbCounters * sizeof(int) * 2);
3490 exec->errCounts = &exec->counts[comp->nbCounters];
3491 } else {
Daniel Veillard4255d502002-04-16 15:50:10 +00003492 exec->counts = NULL;
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003493 exec->errCounts = NULL;
3494 }
Daniel Veillard4255d502002-04-16 15:50:10 +00003495 exec->inputStackMax = 0;
3496 exec->inputStackNr = 0;
3497 exec->inputStack = NULL;
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003498 exec->errStateNo = -1;
3499 exec->errString = NULL;
Daniel Veillard94cc1032005-09-15 13:09:00 +00003500 exec->nbPush = 0;
Daniel Veillard4255d502002-04-16 15:50:10 +00003501 return(exec);
3502}
3503
3504/**
3505 * xmlRegFreeExecCtxt:
3506 * @exec: a regular expression evaulation context
3507 *
3508 * Free the structures associated to a regular expression evaulation context.
3509 */
3510void
3511xmlRegFreeExecCtxt(xmlRegExecCtxtPtr exec) {
3512 if (exec == NULL)
3513 return;
3514
3515 if (exec->rollbacks != NULL) {
3516 if (exec->counts != NULL) {
3517 int i;
3518
3519 for (i = 0;i < exec->maxRollbacks;i++)
3520 if (exec->rollbacks[i].counts != NULL)
3521 xmlFree(exec->rollbacks[i].counts);
3522 }
3523 xmlFree(exec->rollbacks);
3524 }
3525 if (exec->counts != NULL)
3526 xmlFree(exec->counts);
3527 if (exec->inputStack != NULL) {
3528 int i;
3529
Daniel Veillard32370232002-10-16 14:08:14 +00003530 for (i = 0;i < exec->inputStackNr;i++) {
3531 if (exec->inputStack[i].value != NULL)
3532 xmlFree(exec->inputStack[i].value);
3533 }
Daniel Veillard4255d502002-04-16 15:50:10 +00003534 xmlFree(exec->inputStack);
3535 }
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003536 if (exec->errString != NULL)
3537 xmlFree(exec->errString);
Daniel Veillard4255d502002-04-16 15:50:10 +00003538 xmlFree(exec);
3539}
3540
3541static void
3542xmlFARegExecSaveInputString(xmlRegExecCtxtPtr exec, const xmlChar *value,
3543 void *data) {
3544#ifdef DEBUG_PUSH
3545 printf("saving value: %d:%s\n", exec->inputStackNr, value);
3546#endif
3547 if (exec->inputStackMax == 0) {
3548 exec->inputStackMax = 4;
3549 exec->inputStack = (xmlRegInputTokenPtr)
3550 xmlMalloc(exec->inputStackMax * sizeof(xmlRegInputToken));
3551 if (exec->inputStack == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00003552 xmlRegexpErrMemory(NULL, "pushing input string");
Daniel Veillard4255d502002-04-16 15:50:10 +00003553 exec->inputStackMax = 0;
3554 return;
3555 }
3556 } else if (exec->inputStackNr + 1 >= exec->inputStackMax) {
3557 xmlRegInputTokenPtr tmp;
3558
3559 exec->inputStackMax *= 2;
3560 tmp = (xmlRegInputTokenPtr) xmlRealloc(exec->inputStack,
3561 exec->inputStackMax * sizeof(xmlRegInputToken));
3562 if (tmp == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00003563 xmlRegexpErrMemory(NULL, "pushing input string");
Daniel Veillard4255d502002-04-16 15:50:10 +00003564 exec->inputStackMax /= 2;
3565 return;
3566 }
3567 exec->inputStack = tmp;
3568 }
3569 exec->inputStack[exec->inputStackNr].value = xmlStrdup(value);
3570 exec->inputStack[exec->inputStackNr].data = data;
3571 exec->inputStackNr++;
3572 exec->inputStack[exec->inputStackNr].value = NULL;
3573 exec->inputStack[exec->inputStackNr].data = NULL;
3574}
3575
Daniel Veillardc0826a72004-08-10 14:17:33 +00003576/**
3577 * xmlRegStrEqualWildcard:
3578 * @expStr: the string to be evaluated
3579 * @valStr: the validation string
3580 *
3581 * Checks if both strings are equal or have the same content. "*"
3582 * can be used as a wildcard in @valStr; "|" is used as a seperator of
3583 * substrings in both @expStr and @valStr.
3584 *
3585 * Returns 1 if the comparison is satisfied and the number of substrings
3586 * is equal, 0 otherwise.
3587 */
3588
3589static int
3590xmlRegStrEqualWildcard(const xmlChar *expStr, const xmlChar *valStr) {
3591 if (expStr == valStr) return(1);
3592 if (expStr == NULL) return(0);
3593 if (valStr == NULL) return(0);
3594 do {
3595 /*
3596 * Eval if we have a wildcard for the current item.
3597 */
3598 if (*expStr != *valStr) {
Daniel Veillard4f82c8a2005-08-09 21:40:08 +00003599 /* if one of them starts with a wildcard make valStr be it */
3600 if (*valStr == '*') {
3601 const xmlChar *tmp;
3602
3603 tmp = valStr;
3604 valStr = expStr;
3605 expStr = tmp;
3606 }
Daniel Veillardc0826a72004-08-10 14:17:33 +00003607 if ((*valStr != 0) && (*expStr != 0) && (*expStr++ == '*')) {
3608 do {
3609 if (*valStr == XML_REG_STRING_SEPARATOR)
3610 break;
Kasimier T. Buchcikc0e833f2005-04-19 15:02:20 +00003611 valStr++;
Daniel Veillardc0826a72004-08-10 14:17:33 +00003612 } while (*valStr != 0);
3613 continue;
3614 } else
3615 return(0);
3616 }
Kasimier T. Buchcikc0e833f2005-04-19 15:02:20 +00003617 expStr++;
3618 valStr++;
Daniel Veillardc0826a72004-08-10 14:17:33 +00003619 } while (*valStr != 0);
3620 if (*expStr != 0)
3621 return (0);
3622 else
3623 return (1);
3624}
Daniel Veillard4255d502002-04-16 15:50:10 +00003625
3626/**
Daniel Veillard23e73572002-09-19 19:56:43 +00003627 * xmlRegCompactPushString:
3628 * @exec: a regexp execution context
3629 * @comp: the precompiled exec with a compact table
3630 * @value: a string token input
3631 * @data: data associated to the token to reuse in callbacks
3632 *
3633 * Push one input token in the execution context
3634 *
3635 * Returns: 1 if the regexp reached a final state, 0 if non-final, and
3636 * a negative value in case of error.
3637 */
3638static int
3639xmlRegCompactPushString(xmlRegExecCtxtPtr exec,
3640 xmlRegexpPtr comp,
3641 const xmlChar *value,
3642 void *data) {
3643 int state = exec->index;
3644 int i, target;
3645
3646 if ((comp == NULL) || (comp->compact == NULL) || (comp->stringMap == NULL))
3647 return(-1);
3648
3649 if (value == NULL) {
3650 /*
3651 * are we at a final state ?
3652 */
3653 if (comp->compact[state * (comp->nbstrings + 1)] ==
3654 XML_REGEXP_FINAL_STATE)
3655 return(1);
3656 return(0);
3657 }
3658
3659#ifdef DEBUG_PUSH
3660 printf("value pushed: %s\n", value);
3661#endif
3662
3663 /*
William M. Brackddf71d62004-05-06 04:17:26 +00003664 * Examine all outside transitions from current state
Daniel Veillard23e73572002-09-19 19:56:43 +00003665 */
3666 for (i = 0;i < comp->nbstrings;i++) {
3667 target = comp->compact[state * (comp->nbstrings + 1) + i + 1];
3668 if ((target > 0) && (target <= comp->nbstates)) {
Daniel Veillardc0826a72004-08-10 14:17:33 +00003669 target--; /* to avoid 0 */
3670 if (xmlRegStrEqualWildcard(comp->stringMap[i], value)) {
3671 exec->index = target;
Daniel Veillard118aed72002-09-24 14:13:13 +00003672 if ((exec->callback != NULL) && (comp->transdata != NULL)) {
3673 exec->callback(exec->data, value,
3674 comp->transdata[state * comp->nbstrings + i], data);
3675 }
Daniel Veillard23e73572002-09-19 19:56:43 +00003676#ifdef DEBUG_PUSH
3677 printf("entering state %d\n", target);
3678#endif
3679 if (comp->compact[target * (comp->nbstrings + 1)] ==
Daniel Veillardcc026dc2005-01-12 13:21:17 +00003680 XML_REGEXP_SINK_STATE)
3681 goto error;
3682
3683 if (comp->compact[target * (comp->nbstrings + 1)] ==
Daniel Veillard23e73572002-09-19 19:56:43 +00003684 XML_REGEXP_FINAL_STATE)
3685 return(1);
3686 return(0);
3687 }
3688 }
3689 }
3690 /*
3691 * Failed to find an exit transition out from current state for the
3692 * current token
3693 */
3694#ifdef DEBUG_PUSH
3695 printf("failed to find a transition for %s on state %d\n", value, state);
3696#endif
Daniel Veillardcc026dc2005-01-12 13:21:17 +00003697error:
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003698 if (exec->errString != NULL)
3699 xmlFree(exec->errString);
3700 exec->errString = xmlStrdup(value);
3701 exec->errStateNo = state;
Daniel Veillard23e73572002-09-19 19:56:43 +00003702 exec->status = -1;
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003703#ifdef DEBUG_ERR
3704 testerr(exec);
3705#endif
Daniel Veillard23e73572002-09-19 19:56:43 +00003706 return(-1);
3707}
3708
3709/**
Daniel Veillard6e65e152005-08-09 11:09:52 +00003710 * xmlRegExecPushStringInternal:
Daniel Veillardea7751d2002-12-20 00:16:24 +00003711 * @exec: a regexp execution context or NULL to indicate the end
Daniel Veillard4255d502002-04-16 15:50:10 +00003712 * @value: a string token input
3713 * @data: data associated to the token to reuse in callbacks
Daniel Veillard6e65e152005-08-09 11:09:52 +00003714 * @compound: value was assembled from 2 strings
Daniel Veillard4255d502002-04-16 15:50:10 +00003715 *
3716 * Push one input token in the execution context
3717 *
3718 * Returns: 1 if the regexp reached a final state, 0 if non-final, and
3719 * a negative value in case of error.
3720 */
Daniel Veillard6e65e152005-08-09 11:09:52 +00003721static int
3722xmlRegExecPushStringInternal(xmlRegExecCtxtPtr exec, const xmlChar *value,
3723 void *data, int compound) {
Daniel Veillard4255d502002-04-16 15:50:10 +00003724 xmlRegTransPtr trans;
3725 xmlRegAtomPtr atom;
3726 int ret;
3727 int final = 0;
Daniel Veillard90700152005-01-08 22:05:09 +00003728 int progress = 1;
Daniel Veillard4255d502002-04-16 15:50:10 +00003729
3730 if (exec == NULL)
3731 return(-1);
Daniel Veillard23e73572002-09-19 19:56:43 +00003732 if (exec->comp == NULL)
3733 return(-1);
Daniel Veillard4255d502002-04-16 15:50:10 +00003734 if (exec->status != 0)
3735 return(exec->status);
3736
Daniel Veillard23e73572002-09-19 19:56:43 +00003737 if (exec->comp->compact != NULL)
3738 return(xmlRegCompactPushString(exec, exec->comp, value, data));
3739
Daniel Veillard4255d502002-04-16 15:50:10 +00003740 if (value == NULL) {
3741 if (exec->state->type == XML_REGEXP_FINAL_STATE)
3742 return(1);
3743 final = 1;
3744 }
3745
3746#ifdef DEBUG_PUSH
3747 printf("value pushed: %s\n", value);
3748#endif
3749 /*
3750 * If we have an active rollback stack push the new value there
3751 * and get back to where we were left
3752 */
3753 if ((value != NULL) && (exec->inputStackNr > 0)) {
3754 xmlFARegExecSaveInputString(exec, value, data);
3755 value = exec->inputStack[exec->index].value;
3756 data = exec->inputStack[exec->index].data;
3757#ifdef DEBUG_PUSH
3758 printf("value loaded: %s\n", value);
3759#endif
3760 }
3761
3762 while ((exec->status == 0) &&
3763 ((value != NULL) ||
3764 ((final == 1) &&
3765 (exec->state->type != XML_REGEXP_FINAL_STATE)))) {
3766
3767 /*
3768 * End of input on non-terminal state, rollback, however we may
3769 * still have epsilon like transition for counted transitions
3770 * on counters, in that case don't break too early.
3771 */
Daniel Veillardb509f152002-04-17 16:28:10 +00003772 if ((value == NULL) && (exec->counts == NULL))
Daniel Veillard4255d502002-04-16 15:50:10 +00003773 goto rollback;
3774
3775 exec->transcount = 0;
3776 for (;exec->transno < exec->state->nbTrans;exec->transno++) {
3777 trans = &exec->state->trans[exec->transno];
3778 if (trans->to < 0)
3779 continue;
3780 atom = trans->atom;
3781 ret = 0;
Daniel Veillard441bc322002-04-20 17:38:48 +00003782 if (trans->count == REGEXP_ALL_LAX_COUNTER) {
3783 int i;
3784 int count;
3785 xmlRegTransPtr t;
3786 xmlRegCounterPtr counter;
3787
3788 ret = 0;
3789
3790#ifdef DEBUG_PUSH
3791 printf("testing all lax %d\n", trans->count);
3792#endif
3793 /*
3794 * Check all counted transitions from the current state
3795 */
3796 if ((value == NULL) && (final)) {
3797 ret = 1;
3798 } else if (value != NULL) {
3799 for (i = 0;i < exec->state->nbTrans;i++) {
3800 t = &exec->state->trans[i];
3801 if ((t->counter < 0) || (t == trans))
3802 continue;
3803 counter = &exec->comp->counters[t->counter];
3804 count = exec->counts[t->counter];
3805 if ((count < counter->max) &&
3806 (t->atom != NULL) &&
3807 (xmlStrEqual(value, t->atom->valuep))) {
3808 ret = 0;
3809 break;
3810 }
3811 if ((count >= counter->min) &&
3812 (count < counter->max) &&
Daniel Veillard11ce4002006-03-10 00:36:23 +00003813 (t->atom != NULL) &&
Daniel Veillard441bc322002-04-20 17:38:48 +00003814 (xmlStrEqual(value, t->atom->valuep))) {
3815 ret = 1;
3816 break;
3817 }
3818 }
3819 }
3820 } else if (trans->count == REGEXP_ALL_COUNTER) {
Daniel Veillard8a001f62002-04-20 07:24:11 +00003821 int i;
3822 int count;
3823 xmlRegTransPtr t;
3824 xmlRegCounterPtr counter;
3825
3826 ret = 1;
3827
3828#ifdef DEBUG_PUSH
3829 printf("testing all %d\n", trans->count);
3830#endif
3831 /*
3832 * Check all counted transitions from the current state
3833 */
3834 for (i = 0;i < exec->state->nbTrans;i++) {
3835 t = &exec->state->trans[i];
3836 if ((t->counter < 0) || (t == trans))
3837 continue;
3838 counter = &exec->comp->counters[t->counter];
3839 count = exec->counts[t->counter];
3840 if ((count < counter->min) || (count > counter->max)) {
3841 ret = 0;
3842 break;
3843 }
3844 }
3845 } else if (trans->count >= 0) {
Daniel Veillard4255d502002-04-16 15:50:10 +00003846 int count;
3847 xmlRegCounterPtr counter;
3848
3849 /*
3850 * A counted transition.
3851 */
3852
3853 count = exec->counts[trans->count];
3854 counter = &exec->comp->counters[trans->count];
3855#ifdef DEBUG_PUSH
3856 printf("testing count %d: val %d, min %d, max %d\n",
3857 trans->count, count, counter->min, counter->max);
3858#endif
3859 ret = ((count >= counter->min) && (count <= counter->max));
3860 } else if (atom == NULL) {
3861 fprintf(stderr, "epsilon transition left at runtime\n");
3862 exec->status = -2;
3863 break;
3864 } else if (value != NULL) {
Daniel Veillardc0826a72004-08-10 14:17:33 +00003865 ret = xmlRegStrEqualWildcard(atom->valuep, value);
Daniel Veillard6e65e152005-08-09 11:09:52 +00003866 if (atom->neg) {
Daniel Veillard9efc4762005-07-19 14:33:55 +00003867 ret = !ret;
Daniel Veillard6e65e152005-08-09 11:09:52 +00003868 if (!compound)
3869 ret = 0;
3870 }
Daniel Veillard441bc322002-04-20 17:38:48 +00003871 if ((ret == 1) && (trans->counter >= 0)) {
3872 xmlRegCounterPtr counter;
3873 int count;
3874
3875 count = exec->counts[trans->counter];
3876 counter = &exec->comp->counters[trans->counter];
3877 if (count >= counter->max)
3878 ret = 0;
3879 }
3880
Daniel Veillard4255d502002-04-16 15:50:10 +00003881 if ((ret == 1) && (atom->min > 0) && (atom->max > 0)) {
3882 xmlRegStatePtr to = exec->comp->states[trans->to];
3883
3884 /*
3885 * this is a multiple input sequence
3886 */
3887 if (exec->state->nbTrans > exec->transno + 1) {
3888 if (exec->inputStackNr <= 0) {
3889 xmlFARegExecSaveInputString(exec, value, data);
3890 }
3891 xmlFARegExecSave(exec);
3892 }
3893 exec->transcount = 1;
3894 do {
3895 /*
3896 * Try to progress as much as possible on the input
3897 */
3898 if (exec->transcount == atom->max) {
3899 break;
3900 }
3901 exec->index++;
3902 value = exec->inputStack[exec->index].value;
3903 data = exec->inputStack[exec->index].data;
3904#ifdef DEBUG_PUSH
3905 printf("value loaded: %s\n", value);
3906#endif
3907
3908 /*
3909 * End of input: stop here
3910 */
3911 if (value == NULL) {
3912 exec->index --;
3913 break;
3914 }
3915 if (exec->transcount >= atom->min) {
3916 int transno = exec->transno;
3917 xmlRegStatePtr state = exec->state;
3918
3919 /*
3920 * The transition is acceptable save it
3921 */
3922 exec->transno = -1; /* trick */
3923 exec->state = to;
3924 if (exec->inputStackNr <= 0) {
3925 xmlFARegExecSaveInputString(exec, value, data);
3926 }
3927 xmlFARegExecSave(exec);
3928 exec->transno = transno;
3929 exec->state = state;
3930 }
3931 ret = xmlStrEqual(value, atom->valuep);
3932 exec->transcount++;
3933 } while (ret == 1);
3934 if (exec->transcount < atom->min)
3935 ret = 0;
3936
3937 /*
3938 * If the last check failed but one transition was found
3939 * possible, rollback
3940 */
3941 if (ret < 0)
3942 ret = 0;
3943 if (ret == 0) {
3944 goto rollback;
3945 }
3946 }
3947 }
3948 if (ret == 1) {
William M. Brack98873952003-12-26 06:03:14 +00003949 if ((exec->callback != NULL) && (atom != NULL) &&
3950 (data != NULL)) {
Daniel Veillard4255d502002-04-16 15:50:10 +00003951 exec->callback(exec->data, atom->valuep,
3952 atom->data, data);
3953 }
3954 if (exec->state->nbTrans > exec->transno + 1) {
3955 if (exec->inputStackNr <= 0) {
3956 xmlFARegExecSaveInputString(exec, value, data);
3957 }
3958 xmlFARegExecSave(exec);
3959 }
3960 if (trans->counter >= 0) {
3961#ifdef DEBUG_PUSH
3962 printf("Increasing count %d\n", trans->counter);
3963#endif
3964 exec->counts[trans->counter]++;
3965 }
Daniel Veillard10752282005-08-08 13:05:13 +00003966 if ((trans->count >= 0) &&
3967 (trans->count < REGEXP_ALL_COUNTER)) {
3968#ifdef DEBUG_REGEXP_EXEC
3969 printf("resetting count %d on transition\n",
3970 trans->count);
3971#endif
3972 exec->counts[trans->count] = 0;
3973 }
Daniel Veillard4255d502002-04-16 15:50:10 +00003974#ifdef DEBUG_PUSH
3975 printf("entering state %d\n", trans->to);
3976#endif
Daniel Veillardcc026dc2005-01-12 13:21:17 +00003977 if ((exec->comp->states[trans->to] != NULL) &&
3978 (exec->comp->states[trans->to]->type ==
3979 XML_REGEXP_SINK_STATE)) {
3980 /*
3981 * entering a sink state, save the current state as error
3982 * state.
3983 */
3984 if (exec->errString != NULL)
3985 xmlFree(exec->errString);
3986 exec->errString = xmlStrdup(value);
3987 exec->errState = exec->state;
3988 memcpy(exec->errCounts, exec->counts,
3989 exec->comp->nbCounters * sizeof(int));
3990 }
Daniel Veillard4255d502002-04-16 15:50:10 +00003991 exec->state = exec->comp->states[trans->to];
3992 exec->transno = 0;
3993 if (trans->atom != NULL) {
3994 if (exec->inputStack != NULL) {
3995 exec->index++;
3996 if (exec->index < exec->inputStackNr) {
3997 value = exec->inputStack[exec->index].value;
3998 data = exec->inputStack[exec->index].data;
3999#ifdef DEBUG_PUSH
4000 printf("value loaded: %s\n", value);
4001#endif
4002 } else {
4003 value = NULL;
4004 data = NULL;
4005#ifdef DEBUG_PUSH
4006 printf("end of input\n");
4007#endif
4008 }
4009 } else {
4010 value = NULL;
4011 data = NULL;
4012#ifdef DEBUG_PUSH
4013 printf("end of input\n");
4014#endif
4015 }
4016 }
4017 goto progress;
4018 } else if (ret < 0) {
4019 exec->status = -4;
4020 break;
4021 }
4022 }
4023 if ((exec->transno != 0) || (exec->state->nbTrans == 0)) {
4024rollback:
Daniel Veillard90700152005-01-08 22:05:09 +00004025 /*
Daniel Veillardcc026dc2005-01-12 13:21:17 +00004026 * if we didn't yet rollback on the current input
4027 * store the current state as the error state.
Daniel Veillard90700152005-01-08 22:05:09 +00004028 */
Daniel Veillardcc026dc2005-01-12 13:21:17 +00004029 if ((progress) && (exec->state != NULL) &&
4030 (exec->state->type != XML_REGEXP_SINK_STATE)) {
Daniel Veillard90700152005-01-08 22:05:09 +00004031 progress = 0;
4032 if (exec->errString != NULL)
4033 xmlFree(exec->errString);
4034 exec->errString = xmlStrdup(value);
4035 exec->errState = exec->state;
4036 memcpy(exec->errCounts, exec->counts,
4037 exec->comp->nbCounters * sizeof(int));
4038 }
4039
Daniel Veillard4255d502002-04-16 15:50:10 +00004040 /*
4041 * Failed to find a way out
4042 */
4043 exec->determinist = 0;
4044 xmlFARegExecRollBack(exec);
4045 if (exec->status == 0) {
4046 value = exec->inputStack[exec->index].value;
4047 data = exec->inputStack[exec->index].data;
4048#ifdef DEBUG_PUSH
4049 printf("value loaded: %s\n", value);
4050#endif
4051 }
4052 }
Daniel Veillard90700152005-01-08 22:05:09 +00004053 continue;
Daniel Veillard4255d502002-04-16 15:50:10 +00004054progress:
Daniel Veillard90700152005-01-08 22:05:09 +00004055 progress = 1;
Daniel Veillard4255d502002-04-16 15:50:10 +00004056 continue;
4057 }
4058 if (exec->status == 0) {
4059 return(exec->state->type == XML_REGEXP_FINAL_STATE);
4060 }
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00004061#ifdef DEBUG_ERR
Daniel Veillard90700152005-01-08 22:05:09 +00004062 if (exec->status < 0) {
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00004063 testerr(exec);
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00004064 }
Daniel Veillard90700152005-01-08 22:05:09 +00004065#endif
Daniel Veillard4255d502002-04-16 15:50:10 +00004066 return(exec->status);
4067}
4068
Daniel Veillard52b48c72003-04-13 19:53:42 +00004069/**
Daniel Veillard6e65e152005-08-09 11:09:52 +00004070 * xmlRegExecPushString:
4071 * @exec: a regexp execution context or NULL to indicate the end
4072 * @value: a string token input
4073 * @data: data associated to the token to reuse in callbacks
4074 *
4075 * Push one input token in the execution context
4076 *
4077 * Returns: 1 if the regexp reached a final state, 0 if non-final, and
4078 * a negative value in case of error.
4079 */
4080int
4081xmlRegExecPushString(xmlRegExecCtxtPtr exec, const xmlChar *value,
4082 void *data) {
4083 return(xmlRegExecPushStringInternal(exec, value, data, 0));
4084}
4085
4086/**
Daniel Veillard52b48c72003-04-13 19:53:42 +00004087 * xmlRegExecPushString2:
4088 * @exec: a regexp execution context or NULL to indicate the end
4089 * @value: the first string token input
4090 * @value2: the second string token input
4091 * @data: data associated to the token to reuse in callbacks
4092 *
4093 * Push one input token in the execution context
4094 *
4095 * Returns: 1 if the regexp reached a final state, 0 if non-final, and
4096 * a negative value in case of error.
4097 */
4098int
4099xmlRegExecPushString2(xmlRegExecCtxtPtr exec, const xmlChar *value,
4100 const xmlChar *value2, void *data) {
4101 xmlChar buf[150];
4102 int lenn, lenp, ret;
4103 xmlChar *str;
4104
4105 if (exec == NULL)
4106 return(-1);
4107 if (exec->comp == NULL)
4108 return(-1);
4109 if (exec->status != 0)
4110 return(exec->status);
4111
4112 if (value2 == NULL)
4113 return(xmlRegExecPushString(exec, value, data));
4114
4115 lenn = strlen((char *) value2);
4116 lenp = strlen((char *) value);
4117
4118 if (150 < lenn + lenp + 2) {
Daniel Veillard3c908dc2003-04-19 00:07:51 +00004119 str = (xmlChar *) xmlMallocAtomic(lenn + lenp + 2);
Daniel Veillard52b48c72003-04-13 19:53:42 +00004120 if (str == NULL) {
4121 exec->status = -1;
4122 return(-1);
4123 }
4124 } else {
4125 str = buf;
4126 }
4127 memcpy(&str[0], value, lenp);
Daniel Veillardc0826a72004-08-10 14:17:33 +00004128 str[lenp] = XML_REG_STRING_SEPARATOR;
Daniel Veillard52b48c72003-04-13 19:53:42 +00004129 memcpy(&str[lenp + 1], value2, lenn);
4130 str[lenn + lenp + 1] = 0;
4131
4132 if (exec->comp->compact != NULL)
4133 ret = xmlRegCompactPushString(exec, exec->comp, str, data);
4134 else
Daniel Veillard6e65e152005-08-09 11:09:52 +00004135 ret = xmlRegExecPushStringInternal(exec, str, data, 1);
Daniel Veillard52b48c72003-04-13 19:53:42 +00004136
4137 if (str != buf)
Daniel Veillard0b1ff142005-12-28 21:13:33 +00004138 xmlFree(str);
Daniel Veillard52b48c72003-04-13 19:53:42 +00004139 return(ret);
4140}
4141
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00004142/**
Daniel Veillard77005e62005-07-19 16:26:18 +00004143 * xmlRegExecGetValues:
Daniel Veillardfc0b6f62005-01-09 17:48:02 +00004144 * @exec: a regexp execution context
4145 * @err: error extraction or normal one
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00004146 * @nbval: pointer to the number of accepted values IN/OUT
Daniel Veillardcc026dc2005-01-12 13:21:17 +00004147 * @nbneg: return number of negative transitions
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00004148 * @values: pointer to the array of acceptable values
Daniel Veillardfc0b6f62005-01-09 17:48:02 +00004149 * @terminal: return value if this was a terminal state
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00004150 *
Daniel Veillardfc0b6f62005-01-09 17:48:02 +00004151 * Extract informations from the regexp execution, internal routine to
4152 * implement xmlRegExecNextValues() and xmlRegExecErrInfo()
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00004153 *
4154 * Returns: 0 in case of success or -1 in case of error.
4155 */
Daniel Veillardfc0b6f62005-01-09 17:48:02 +00004156static int
4157xmlRegExecGetValues(xmlRegExecCtxtPtr exec, int err,
Daniel Veillardcc026dc2005-01-12 13:21:17 +00004158 int *nbval, int *nbneg,
4159 xmlChar **values, int *terminal) {
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00004160 int maxval;
Daniel Veillardcc026dc2005-01-12 13:21:17 +00004161 int nb = 0;
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00004162
Daniel Veillardcc026dc2005-01-12 13:21:17 +00004163 if ((exec == NULL) || (nbval == NULL) || (nbneg == NULL) ||
4164 (values == NULL) || (*nbval <= 0))
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00004165 return(-1);
Daniel Veillardfc0b6f62005-01-09 17:48:02 +00004166
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00004167 maxval = *nbval;
4168 *nbval = 0;
Daniel Veillardcc026dc2005-01-12 13:21:17 +00004169 *nbneg = 0;
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00004170 if ((exec->comp != NULL) && (exec->comp->compact != NULL)) {
4171 xmlRegexpPtr comp;
4172 int target, i, state;
4173
4174 comp = exec->comp;
Daniel Veillardfc0b6f62005-01-09 17:48:02 +00004175
4176 if (err) {
4177 if (exec->errStateNo == -1) return(-1);
4178 state = exec->errStateNo;
4179 } else {
4180 state = exec->index;
4181 }
4182 if (terminal != NULL) {
4183 if (comp->compact[state * (comp->nbstrings + 1)] ==
4184 XML_REGEXP_FINAL_STATE)
4185 *terminal = 1;
4186 else
4187 *terminal = 0;
4188 }
Daniel Veillardcc026dc2005-01-12 13:21:17 +00004189 for (i = 0;(i < comp->nbstrings) && (nb < maxval);i++) {
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00004190 target = comp->compact[state * (comp->nbstrings + 1) + i + 1];
Daniel Veillardcc026dc2005-01-12 13:21:17 +00004191 if ((target > 0) && (target <= comp->nbstates) &&
4192 (comp->compact[(target - 1) * (comp->nbstrings + 1)] !=
4193 XML_REGEXP_SINK_STATE)) {
4194 values[nb++] = comp->stringMap[i];
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00004195 (*nbval)++;
4196 }
4197 }
Daniel Veillardcc026dc2005-01-12 13:21:17 +00004198 for (i = 0;(i < comp->nbstrings) && (nb < maxval);i++) {
4199 target = comp->compact[state * (comp->nbstrings + 1) + i + 1];
4200 if ((target > 0) && (target <= comp->nbstates) &&
4201 (comp->compact[(target - 1) * (comp->nbstrings + 1)] ==
4202 XML_REGEXP_SINK_STATE)) {
4203 values[nb++] = comp->stringMap[i];
4204 (*nbneg)++;
4205 }
4206 }
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00004207 } else {
4208 int transno;
4209 xmlRegTransPtr trans;
4210 xmlRegAtomPtr atom;
Daniel Veillardfc0b6f62005-01-09 17:48:02 +00004211 xmlRegStatePtr state;
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00004212
Daniel Veillardfc0b6f62005-01-09 17:48:02 +00004213 if (terminal != NULL) {
4214 if (exec->state->type == XML_REGEXP_FINAL_STATE)
4215 *terminal = 1;
4216 else
4217 *terminal = 0;
4218 }
4219
4220 if (err) {
4221 if (exec->errState == NULL) return(-1);
4222 state = exec->errState;
4223 } else {
4224 if (exec->state == NULL) return(-1);
4225 state = exec->state;
4226 }
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00004227 for (transno = 0;
Daniel Veillardcc026dc2005-01-12 13:21:17 +00004228 (transno < state->nbTrans) && (nb < maxval);
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00004229 transno++) {
Daniel Veillardfc0b6f62005-01-09 17:48:02 +00004230 trans = &state->trans[transno];
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00004231 if (trans->to < 0)
4232 continue;
4233 atom = trans->atom;
4234 if ((atom == NULL) || (atom->valuep == NULL))
4235 continue;
4236 if (trans->count == REGEXP_ALL_LAX_COUNTER) {
Daniel Veillardcc026dc2005-01-12 13:21:17 +00004237 /* this should not be reached but ... */
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00004238 TODO;
4239 } else if (trans->count == REGEXP_ALL_COUNTER) {
Daniel Veillardcc026dc2005-01-12 13:21:17 +00004240 /* this should not be reached but ... */
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00004241 TODO;
4242 } else if (trans->counter >= 0) {
Daniel Veillard11ce4002006-03-10 00:36:23 +00004243 xmlRegCounterPtr counter = NULL;
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00004244 int count;
4245
Daniel Veillardfc0b6f62005-01-09 17:48:02 +00004246 if (err)
4247 count = exec->errCounts[trans->counter];
4248 else
4249 count = exec->counts[trans->counter];
Daniel Veillard11ce4002006-03-10 00:36:23 +00004250 if (exec->comp != NULL)
4251 counter = &exec->comp->counters[trans->counter];
4252 if ((counter == NULL) || (count < counter->max)) {
Daniel Veillard77005e62005-07-19 16:26:18 +00004253 if (atom->neg)
4254 values[nb++] = (xmlChar *) atom->valuep2;
4255 else
4256 values[nb++] = (xmlChar *) atom->valuep;
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00004257 (*nbval)++;
4258 }
4259 } else {
Daniel Veillardcc026dc2005-01-12 13:21:17 +00004260 if ((exec->comp->states[trans->to] != NULL) &&
4261 (exec->comp->states[trans->to]->type !=
4262 XML_REGEXP_SINK_STATE)) {
Daniel Veillard77005e62005-07-19 16:26:18 +00004263 if (atom->neg)
4264 values[nb++] = (xmlChar *) atom->valuep2;
4265 else
4266 values[nb++] = (xmlChar *) atom->valuep;
Daniel Veillardcc026dc2005-01-12 13:21:17 +00004267 (*nbval)++;
4268 }
4269 }
4270 }
4271 for (transno = 0;
4272 (transno < state->nbTrans) && (nb < maxval);
4273 transno++) {
4274 trans = &state->trans[transno];
4275 if (trans->to < 0)
4276 continue;
4277 atom = trans->atom;
4278 if ((atom == NULL) || (atom->valuep == NULL))
4279 continue;
4280 if (trans->count == REGEXP_ALL_LAX_COUNTER) {
4281 continue;
4282 } else if (trans->count == REGEXP_ALL_COUNTER) {
4283 continue;
4284 } else if (trans->counter >= 0) {
4285 continue;
4286 } else {
4287 if ((exec->comp->states[trans->to] != NULL) &&
4288 (exec->comp->states[trans->to]->type ==
4289 XML_REGEXP_SINK_STATE)) {
Daniel Veillard77005e62005-07-19 16:26:18 +00004290 if (atom->neg)
4291 values[nb++] = (xmlChar *) atom->valuep2;
4292 else
4293 values[nb++] = (xmlChar *) atom->valuep;
Daniel Veillardcc026dc2005-01-12 13:21:17 +00004294 (*nbneg)++;
4295 }
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00004296 }
4297 }
4298 }
4299 return(0);
4300}
4301
Daniel Veillardfc0b6f62005-01-09 17:48:02 +00004302/**
4303 * xmlRegExecNextValues:
4304 * @exec: a regexp execution context
4305 * @nbval: pointer to the number of accepted values IN/OUT
Daniel Veillardcc026dc2005-01-12 13:21:17 +00004306 * @nbneg: return number of negative transitions
Daniel Veillardfc0b6f62005-01-09 17:48:02 +00004307 * @values: pointer to the array of acceptable values
4308 * @terminal: return value if this was a terminal state
4309 *
4310 * Extract informations from the regexp execution,
4311 * the parameter @values must point to an array of @nbval string pointers
4312 * on return nbval will contain the number of possible strings in that
4313 * state and the @values array will be updated with them. The string values
4314 * returned will be freed with the @exec context and don't need to be
4315 * deallocated.
4316 *
4317 * Returns: 0 in case of success or -1 in case of error.
4318 */
4319int
Daniel Veillardcc026dc2005-01-12 13:21:17 +00004320xmlRegExecNextValues(xmlRegExecCtxtPtr exec, int *nbval, int *nbneg,
4321 xmlChar **values, int *terminal) {
4322 return(xmlRegExecGetValues(exec, 0, nbval, nbneg, values, terminal));
Daniel Veillardfc0b6f62005-01-09 17:48:02 +00004323}
4324
4325/**
4326 * xmlRegExecErrInfo:
4327 * @exec: a regexp execution context generating an error
4328 * @string: return value for the error string
4329 * @nbval: pointer to the number of accepted values IN/OUT
Daniel Veillardcc026dc2005-01-12 13:21:17 +00004330 * @nbneg: return number of negative transitions
Daniel Veillardfc0b6f62005-01-09 17:48:02 +00004331 * @values: pointer to the array of acceptable values
4332 * @terminal: return value if this was a terminal state
4333 *
4334 * Extract error informations from the regexp execution, the parameter
4335 * @string will be updated with the value pushed and not accepted,
4336 * the parameter @values must point to an array of @nbval string pointers
4337 * on return nbval will contain the number of possible strings in that
4338 * state and the @values array will be updated with them. The string values
4339 * returned will be freed with the @exec context and don't need to be
4340 * deallocated.
4341 *
4342 * Returns: 0 in case of success or -1 in case of error.
4343 */
4344int
4345xmlRegExecErrInfo(xmlRegExecCtxtPtr exec, const xmlChar **string,
Daniel Veillardcc026dc2005-01-12 13:21:17 +00004346 int *nbval, int *nbneg, xmlChar **values, int *terminal) {
Daniel Veillardfc0b6f62005-01-09 17:48:02 +00004347 if (exec == NULL)
4348 return(-1);
4349 if (string != NULL) {
4350 if (exec->status != 0)
4351 *string = exec->errString;
4352 else
4353 *string = NULL;
4354 }
Daniel Veillardcc026dc2005-01-12 13:21:17 +00004355 return(xmlRegExecGetValues(exec, 1, nbval, nbneg, values, terminal));
Daniel Veillardfc0b6f62005-01-09 17:48:02 +00004356}
4357
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00004358#ifdef DEBUG_ERR
4359static void testerr(xmlRegExecCtxtPtr exec) {
4360 const xmlChar *string;
Daniel Veillardcee2b3a2005-01-25 00:22:52 +00004361 xmlChar *values[5];
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00004362 int nb = 5;
Daniel Veillardcc026dc2005-01-12 13:21:17 +00004363 int nbneg;
Daniel Veillardfc0b6f62005-01-09 17:48:02 +00004364 int terminal;
Daniel Veillardcc026dc2005-01-12 13:21:17 +00004365 xmlRegExecErrInfo(exec, &string, &nb, &nbneg, &values[0], &terminal);
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00004366}
4367#endif
4368
Daniel Veillard4255d502002-04-16 15:50:10 +00004369#if 0
4370static int
4371xmlRegExecPushChar(xmlRegExecCtxtPtr exec, int UCS) {
4372 xmlRegTransPtr trans;
4373 xmlRegAtomPtr atom;
4374 int ret;
4375 int codepoint, len;
4376
4377 if (exec == NULL)
4378 return(-1);
4379 if (exec->status != 0)
4380 return(exec->status);
4381
4382 while ((exec->status == 0) &&
4383 ((exec->inputString[exec->index] != 0) ||
4384 (exec->state->type != XML_REGEXP_FINAL_STATE))) {
4385
4386 /*
4387 * End of input on non-terminal state, rollback, however we may
4388 * still have epsilon like transition for counted transitions
4389 * on counters, in that case don't break too early.
4390 */
4391 if ((exec->inputString[exec->index] == 0) && (exec->counts == NULL))
4392 goto rollback;
4393
4394 exec->transcount = 0;
4395 for (;exec->transno < exec->state->nbTrans;exec->transno++) {
4396 trans = &exec->state->trans[exec->transno];
4397 if (trans->to < 0)
4398 continue;
4399 atom = trans->atom;
4400 ret = 0;
4401 if (trans->count >= 0) {
4402 int count;
4403 xmlRegCounterPtr counter;
4404
4405 /*
4406 * A counted transition.
4407 */
4408
4409 count = exec->counts[trans->count];
4410 counter = &exec->comp->counters[trans->count];
4411#ifdef DEBUG_REGEXP_EXEC
4412 printf("testing count %d: val %d, min %d, max %d\n",
4413 trans->count, count, counter->min, counter->max);
4414#endif
4415 ret = ((count >= counter->min) && (count <= counter->max));
4416 } else if (atom == NULL) {
4417 fprintf(stderr, "epsilon transition left at runtime\n");
4418 exec->status = -2;
4419 break;
4420 } else if (exec->inputString[exec->index] != 0) {
4421 codepoint = CUR_SCHAR(&(exec->inputString[exec->index]), len);
4422 ret = xmlRegCheckCharacter(atom, codepoint);
4423 if ((ret == 1) && (atom->min > 0) && (atom->max > 0)) {
4424 xmlRegStatePtr to = exec->comp->states[trans->to];
4425
4426 /*
4427 * this is a multiple input sequence
4428 */
4429 if (exec->state->nbTrans > exec->transno + 1) {
4430 xmlFARegExecSave(exec);
4431 }
4432 exec->transcount = 1;
4433 do {
4434 /*
4435 * Try to progress as much as possible on the input
4436 */
4437 if (exec->transcount == atom->max) {
4438 break;
4439 }
4440 exec->index += len;
4441 /*
4442 * End of input: stop here
4443 */
4444 if (exec->inputString[exec->index] == 0) {
4445 exec->index -= len;
4446 break;
4447 }
4448 if (exec->transcount >= atom->min) {
4449 int transno = exec->transno;
4450 xmlRegStatePtr state = exec->state;
4451
4452 /*
4453 * The transition is acceptable save it
4454 */
4455 exec->transno = -1; /* trick */
4456 exec->state = to;
4457 xmlFARegExecSave(exec);
4458 exec->transno = transno;
4459 exec->state = state;
4460 }
4461 codepoint = CUR_SCHAR(&(exec->inputString[exec->index]),
4462 len);
4463 ret = xmlRegCheckCharacter(atom, codepoint);
4464 exec->transcount++;
4465 } while (ret == 1);
4466 if (exec->transcount < atom->min)
4467 ret = 0;
4468
4469 /*
4470 * If the last check failed but one transition was found
4471 * possible, rollback
4472 */
4473 if (ret < 0)
4474 ret = 0;
4475 if (ret == 0) {
4476 goto rollback;
4477 }
4478 }
4479 }
4480 if (ret == 1) {
4481 if (exec->state->nbTrans > exec->transno + 1) {
4482 xmlFARegExecSave(exec);
4483 }
Daniel Veillard54eb0242006-03-21 23:17:57 +00004484 /*
4485 * restart count for expressions like this ((abc){2})*
4486 */
4487 if (trans->count >= 0) {
4488#ifdef DEBUG_REGEXP_EXEC
4489 printf("Reset count %d\n", trans->count);
4490#endif
4491 exec->counts[trans->count] = 0;
4492 }
Daniel Veillard4255d502002-04-16 15:50:10 +00004493 if (trans->counter >= 0) {
4494#ifdef DEBUG_REGEXP_EXEC
4495 printf("Increasing count %d\n", trans->counter);
4496#endif
4497 exec->counts[trans->counter]++;
4498 }
4499#ifdef DEBUG_REGEXP_EXEC
4500 printf("entering state %d\n", trans->to);
4501#endif
4502 exec->state = exec->comp->states[trans->to];
4503 exec->transno = 0;
4504 if (trans->atom != NULL) {
4505 exec->index += len;
4506 }
4507 goto progress;
4508 } else if (ret < 0) {
4509 exec->status = -4;
4510 break;
4511 }
4512 }
4513 if ((exec->transno != 0) || (exec->state->nbTrans == 0)) {
4514rollback:
4515 /*
4516 * Failed to find a way out
4517 */
4518 exec->determinist = 0;
4519 xmlFARegExecRollBack(exec);
4520 }
4521progress:
4522 continue;
4523 }
4524}
4525#endif
4526/************************************************************************
4527 * *
William M. Brackddf71d62004-05-06 04:17:26 +00004528 * Parser for the Schemas Datatype Regular Expressions *
Daniel Veillard4255d502002-04-16 15:50:10 +00004529 * http://www.w3.org/TR/2001/REC-xmlschema-2-20010502/#regexs *
4530 * *
4531 ************************************************************************/
4532
4533/**
4534 * xmlFAIsChar:
Daniel Veillard441bc322002-04-20 17:38:48 +00004535 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00004536 *
4537 * [10] Char ::= [^.\?*+()|#x5B#x5D]
4538 */
4539static int
4540xmlFAIsChar(xmlRegParserCtxtPtr ctxt) {
4541 int cur;
4542 int len;
4543
4544 cur = CUR_SCHAR(ctxt->cur, len);
4545 if ((cur == '.') || (cur == '\\') || (cur == '?') ||
4546 (cur == '*') || (cur == '+') || (cur == '(') ||
4547 (cur == ')') || (cur == '|') || (cur == 0x5B) ||
4548 (cur == 0x5D) || (cur == 0))
4549 return(-1);
4550 return(cur);
4551}
4552
4553/**
4554 * xmlFAParseCharProp:
Daniel Veillard441bc322002-04-20 17:38:48 +00004555 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00004556 *
4557 * [27] charProp ::= IsCategory | IsBlock
4558 * [28] IsCategory ::= Letters | Marks | Numbers | Punctuation |
4559 * Separators | Symbols | Others
4560 * [29] Letters ::= 'L' [ultmo]?
4561 * [30] Marks ::= 'M' [nce]?
4562 * [31] Numbers ::= 'N' [dlo]?
4563 * [32] Punctuation ::= 'P' [cdseifo]?
4564 * [33] Separators ::= 'Z' [slp]?
4565 * [34] Symbols ::= 'S' [mcko]?
4566 * [35] Others ::= 'C' [cfon]?
4567 * [36] IsBlock ::= 'Is' [a-zA-Z0-9#x2D]+
4568 */
4569static void
4570xmlFAParseCharProp(xmlRegParserCtxtPtr ctxt) {
4571 int cur;
William M. Brack779af002003-08-01 15:55:39 +00004572 xmlRegAtomType type = (xmlRegAtomType) 0;
Daniel Veillard4255d502002-04-16 15:50:10 +00004573 xmlChar *blockName = NULL;
4574
4575 cur = CUR;
4576 if (cur == 'L') {
4577 NEXT;
4578 cur = CUR;
4579 if (cur == 'u') {
4580 NEXT;
4581 type = XML_REGEXP_LETTER_UPPERCASE;
4582 } else if (cur == 'l') {
4583 NEXT;
4584 type = XML_REGEXP_LETTER_LOWERCASE;
4585 } else if (cur == 't') {
4586 NEXT;
4587 type = XML_REGEXP_LETTER_TITLECASE;
4588 } else if (cur == 'm') {
4589 NEXT;
4590 type = XML_REGEXP_LETTER_MODIFIER;
4591 } else if (cur == 'o') {
4592 NEXT;
4593 type = XML_REGEXP_LETTER_OTHERS;
4594 } else {
4595 type = XML_REGEXP_LETTER;
4596 }
4597 } else if (cur == 'M') {
4598 NEXT;
4599 cur = CUR;
4600 if (cur == 'n') {
4601 NEXT;
4602 /* nonspacing */
4603 type = XML_REGEXP_MARK_NONSPACING;
4604 } else if (cur == 'c') {
4605 NEXT;
4606 /* spacing combining */
4607 type = XML_REGEXP_MARK_SPACECOMBINING;
4608 } else if (cur == 'e') {
4609 NEXT;
4610 /* enclosing */
4611 type = XML_REGEXP_MARK_ENCLOSING;
4612 } else {
4613 /* all marks */
4614 type = XML_REGEXP_MARK;
4615 }
4616 } else if (cur == 'N') {
4617 NEXT;
4618 cur = CUR;
4619 if (cur == 'd') {
4620 NEXT;
4621 /* digital */
4622 type = XML_REGEXP_NUMBER_DECIMAL;
4623 } else if (cur == 'l') {
4624 NEXT;
4625 /* letter */
4626 type = XML_REGEXP_NUMBER_LETTER;
4627 } else if (cur == 'o') {
4628 NEXT;
4629 /* other */
4630 type = XML_REGEXP_NUMBER_OTHERS;
4631 } else {
4632 /* all numbers */
4633 type = XML_REGEXP_NUMBER;
4634 }
4635 } else if (cur == 'P') {
4636 NEXT;
4637 cur = CUR;
4638 if (cur == 'c') {
4639 NEXT;
4640 /* connector */
4641 type = XML_REGEXP_PUNCT_CONNECTOR;
4642 } else if (cur == 'd') {
4643 NEXT;
4644 /* dash */
4645 type = XML_REGEXP_PUNCT_DASH;
4646 } else if (cur == 's') {
4647 NEXT;
4648 /* open */
4649 type = XML_REGEXP_PUNCT_OPEN;
4650 } else if (cur == 'e') {
4651 NEXT;
4652 /* close */
4653 type = XML_REGEXP_PUNCT_CLOSE;
4654 } else if (cur == 'i') {
4655 NEXT;
4656 /* initial quote */
4657 type = XML_REGEXP_PUNCT_INITQUOTE;
4658 } else if (cur == 'f') {
4659 NEXT;
4660 /* final quote */
4661 type = XML_REGEXP_PUNCT_FINQUOTE;
4662 } else if (cur == 'o') {
4663 NEXT;
4664 /* other */
4665 type = XML_REGEXP_PUNCT_OTHERS;
4666 } else {
4667 /* all punctuation */
4668 type = XML_REGEXP_PUNCT;
4669 }
4670 } else if (cur == 'Z') {
4671 NEXT;
4672 cur = CUR;
4673 if (cur == 's') {
4674 NEXT;
4675 /* space */
4676 type = XML_REGEXP_SEPAR_SPACE;
4677 } else if (cur == 'l') {
4678 NEXT;
4679 /* line */
4680 type = XML_REGEXP_SEPAR_LINE;
4681 } else if (cur == 'p') {
4682 NEXT;
4683 /* paragraph */
4684 type = XML_REGEXP_SEPAR_PARA;
4685 } else {
4686 /* all separators */
4687 type = XML_REGEXP_SEPAR;
4688 }
4689 } else if (cur == 'S') {
4690 NEXT;
4691 cur = CUR;
4692 if (cur == 'm') {
4693 NEXT;
4694 type = XML_REGEXP_SYMBOL_MATH;
4695 /* math */
4696 } else if (cur == 'c') {
4697 NEXT;
4698 type = XML_REGEXP_SYMBOL_CURRENCY;
4699 /* currency */
4700 } else if (cur == 'k') {
4701 NEXT;
4702 type = XML_REGEXP_SYMBOL_MODIFIER;
4703 /* modifiers */
4704 } else if (cur == 'o') {
4705 NEXT;
4706 type = XML_REGEXP_SYMBOL_OTHERS;
4707 /* other */
4708 } else {
4709 /* all symbols */
4710 type = XML_REGEXP_SYMBOL;
4711 }
4712 } else if (cur == 'C') {
4713 NEXT;
4714 cur = CUR;
4715 if (cur == 'c') {
4716 NEXT;
4717 /* control */
4718 type = XML_REGEXP_OTHER_CONTROL;
4719 } else if (cur == 'f') {
4720 NEXT;
4721 /* format */
4722 type = XML_REGEXP_OTHER_FORMAT;
4723 } else if (cur == 'o') {
4724 NEXT;
4725 /* private use */
4726 type = XML_REGEXP_OTHER_PRIVATE;
4727 } else if (cur == 'n') {
4728 NEXT;
4729 /* not assigned */
4730 type = XML_REGEXP_OTHER_NA;
4731 } else {
4732 /* all others */
4733 type = XML_REGEXP_OTHER;
4734 }
4735 } else if (cur == 'I') {
4736 const xmlChar *start;
4737 NEXT;
4738 cur = CUR;
4739 if (cur != 's') {
4740 ERROR("IsXXXX expected");
4741 return;
4742 }
4743 NEXT;
4744 start = ctxt->cur;
4745 cur = CUR;
4746 if (((cur >= 'a') && (cur <= 'z')) ||
4747 ((cur >= 'A') && (cur <= 'Z')) ||
4748 ((cur >= '0') && (cur <= '9')) ||
4749 (cur == 0x2D)) {
4750 NEXT;
4751 cur = CUR;
4752 while (((cur >= 'a') && (cur <= 'z')) ||
4753 ((cur >= 'A') && (cur <= 'Z')) ||
4754 ((cur >= '0') && (cur <= '9')) ||
4755 (cur == 0x2D)) {
4756 NEXT;
4757 cur = CUR;
4758 }
4759 }
4760 type = XML_REGEXP_BLOCK_NAME;
4761 blockName = xmlStrndup(start, ctxt->cur - start);
4762 } else {
4763 ERROR("Unknown char property");
4764 return;
4765 }
4766 if (ctxt->atom == NULL) {
4767 ctxt->atom = xmlRegNewAtom(ctxt, type);
4768 if (ctxt->atom != NULL)
4769 ctxt->atom->valuep = blockName;
4770 } else if (ctxt->atom->type == XML_REGEXP_RANGES) {
4771 xmlRegAtomAddRange(ctxt, ctxt->atom, ctxt->neg,
4772 type, 0, 0, blockName);
4773 }
4774}
4775
4776/**
4777 * xmlFAParseCharClassEsc:
Daniel Veillard441bc322002-04-20 17:38:48 +00004778 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00004779 *
4780 * [23] charClassEsc ::= ( SingleCharEsc | MultiCharEsc | catEsc | complEsc )
4781 * [24] SingleCharEsc ::= '\' [nrt\|.?*+(){}#x2D#x5B#x5D#x5E]
4782 * [25] catEsc ::= '\p{' charProp '}'
4783 * [26] complEsc ::= '\P{' charProp '}'
4784 * [37] MultiCharEsc ::= '.' | ('\' [sSiIcCdDwW])
4785 */
4786static void
4787xmlFAParseCharClassEsc(xmlRegParserCtxtPtr ctxt) {
4788 int cur;
4789
4790 if (CUR == '.') {
4791 if (ctxt->atom == NULL) {
4792 ctxt->atom = xmlRegNewAtom(ctxt, XML_REGEXP_ANYCHAR);
4793 } else if (ctxt->atom->type == XML_REGEXP_RANGES) {
4794 xmlRegAtomAddRange(ctxt, ctxt->atom, ctxt->neg,
4795 XML_REGEXP_ANYCHAR, 0, 0, NULL);
4796 }
4797 NEXT;
4798 return;
4799 }
4800 if (CUR != '\\') {
4801 ERROR("Escaped sequence: expecting \\");
4802 return;
4803 }
4804 NEXT;
4805 cur = CUR;
4806 if (cur == 'p') {
4807 NEXT;
4808 if (CUR != '{') {
4809 ERROR("Expecting '{'");
4810 return;
4811 }
4812 NEXT;
4813 xmlFAParseCharProp(ctxt);
4814 if (CUR != '}') {
4815 ERROR("Expecting '}'");
4816 return;
4817 }
4818 NEXT;
4819 } else if (cur == 'P') {
4820 NEXT;
4821 if (CUR != '{') {
4822 ERROR("Expecting '{'");
4823 return;
4824 }
4825 NEXT;
4826 xmlFAParseCharProp(ctxt);
4827 ctxt->atom->neg = 1;
4828 if (CUR != '}') {
4829 ERROR("Expecting '}'");
4830 return;
4831 }
4832 NEXT;
4833 } else if ((cur == 'n') || (cur == 'r') || (cur == 't') || (cur == '\\') ||
4834 (cur == '|') || (cur == '.') || (cur == '?') || (cur == '*') ||
4835 (cur == '+') || (cur == '(') || (cur == ')') || (cur == '{') ||
4836 (cur == '}') || (cur == 0x2D) || (cur == 0x5B) || (cur == 0x5D) ||
4837 (cur == 0x5E)) {
4838 if (ctxt->atom == NULL) {
4839 ctxt->atom = xmlRegNewAtom(ctxt, XML_REGEXP_CHARVAL);
Daniel Veillard99c394d2005-07-14 12:58:49 +00004840 if (ctxt->atom != NULL) {
4841 switch (cur) {
4842 case 'n':
4843 ctxt->atom->codepoint = '\n';
4844 break;
4845 case 'r':
4846 ctxt->atom->codepoint = '\r';
4847 break;
4848 case 't':
4849 ctxt->atom->codepoint = '\t';
4850 break;
4851 default:
4852 ctxt->atom->codepoint = cur;
4853 }
4854 }
Daniel Veillard4255d502002-04-16 15:50:10 +00004855 } else if (ctxt->atom->type == XML_REGEXP_RANGES) {
4856 xmlRegAtomAddRange(ctxt, ctxt->atom, ctxt->neg,
4857 XML_REGEXP_CHARVAL, cur, cur, NULL);
4858 }
4859 NEXT;
4860 } else if ((cur == 's') || (cur == 'S') || (cur == 'i') || (cur == 'I') ||
4861 (cur == 'c') || (cur == 'C') || (cur == 'd') || (cur == 'D') ||
4862 (cur == 'w') || (cur == 'W')) {
Daniel Veillardb509f152002-04-17 16:28:10 +00004863 xmlRegAtomType type = XML_REGEXP_ANYSPACE;
Daniel Veillard4255d502002-04-16 15:50:10 +00004864
4865 switch (cur) {
4866 case 's':
4867 type = XML_REGEXP_ANYSPACE;
4868 break;
4869 case 'S':
4870 type = XML_REGEXP_NOTSPACE;
4871 break;
4872 case 'i':
4873 type = XML_REGEXP_INITNAME;
4874 break;
4875 case 'I':
4876 type = XML_REGEXP_NOTINITNAME;
4877 break;
4878 case 'c':
4879 type = XML_REGEXP_NAMECHAR;
4880 break;
4881 case 'C':
4882 type = XML_REGEXP_NOTNAMECHAR;
4883 break;
4884 case 'd':
4885 type = XML_REGEXP_DECIMAL;
4886 break;
4887 case 'D':
4888 type = XML_REGEXP_NOTDECIMAL;
4889 break;
4890 case 'w':
4891 type = XML_REGEXP_REALCHAR;
4892 break;
4893 case 'W':
4894 type = XML_REGEXP_NOTREALCHAR;
4895 break;
4896 }
4897 NEXT;
4898 if (ctxt->atom == NULL) {
4899 ctxt->atom = xmlRegNewAtom(ctxt, type);
4900 } else if (ctxt->atom->type == XML_REGEXP_RANGES) {
4901 xmlRegAtomAddRange(ctxt, ctxt->atom, ctxt->neg,
4902 type, 0, 0, NULL);
4903 }
Daniel Veillardcb4284e2007-04-25 13:55:20 +00004904 } else {
4905 ERROR("Wrong escape sequence, misuse of character '\\'");
Daniel Veillard4255d502002-04-16 15:50:10 +00004906 }
4907}
4908
4909/**
4910 * xmlFAParseCharRef:
Daniel Veillard441bc322002-04-20 17:38:48 +00004911 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00004912 *
4913 * [19] XmlCharRef ::= ( '&#' [0-9]+ ';' ) | (' &#x' [0-9a-fA-F]+ ';' )
4914 */
4915static int
4916xmlFAParseCharRef(xmlRegParserCtxtPtr ctxt) {
4917 int ret = 0, cur;
4918
4919 if ((CUR != '&') || (NXT(1) != '#'))
4920 return(-1);
4921 NEXT;
4922 NEXT;
4923 cur = CUR;
4924 if (cur == 'x') {
4925 NEXT;
4926 cur = CUR;
4927 if (((cur >= '0') && (cur <= '9')) ||
4928 ((cur >= 'a') && (cur <= 'f')) ||
4929 ((cur >= 'A') && (cur <= 'F'))) {
4930 while (((cur >= '0') && (cur <= '9')) ||
Daniel Veillard11ce4002006-03-10 00:36:23 +00004931 ((cur >= 'a') && (cur <= 'f')) ||
Daniel Veillard4255d502002-04-16 15:50:10 +00004932 ((cur >= 'A') && (cur <= 'F'))) {
4933 if ((cur >= '0') && (cur <= '9'))
4934 ret = ret * 16 + cur - '0';
4935 else if ((cur >= 'a') && (cur <= 'f'))
4936 ret = ret * 16 + 10 + (cur - 'a');
4937 else
4938 ret = ret * 16 + 10 + (cur - 'A');
4939 NEXT;
4940 cur = CUR;
4941 }
4942 } else {
4943 ERROR("Char ref: expecting [0-9A-F]");
4944 return(-1);
4945 }
4946 } else {
4947 if ((cur >= '0') && (cur <= '9')) {
4948 while ((cur >= '0') && (cur <= '9')) {
4949 ret = ret * 10 + cur - '0';
4950 NEXT;
4951 cur = CUR;
4952 }
4953 } else {
4954 ERROR("Char ref: expecting [0-9]");
4955 return(-1);
4956 }
4957 }
4958 if (cur != ';') {
4959 ERROR("Char ref: expecting ';'");
4960 return(-1);
4961 } else {
4962 NEXT;
4963 }
4964 return(ret);
4965}
4966
4967/**
4968 * xmlFAParseCharRange:
Daniel Veillard441bc322002-04-20 17:38:48 +00004969 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00004970 *
4971 * [17] charRange ::= seRange | XmlCharRef | XmlCharIncDash
4972 * [18] seRange ::= charOrEsc '-' charOrEsc
4973 * [20] charOrEsc ::= XmlChar | SingleCharEsc
4974 * [21] XmlChar ::= [^\#x2D#x5B#x5D]
4975 * [22] XmlCharIncDash ::= [^\#x5B#x5D]
4976 */
4977static void
4978xmlFAParseCharRange(xmlRegParserCtxtPtr ctxt) {
William M. Brackdc99df92003-12-27 01:54:25 +00004979 int cur, len;
Daniel Veillard4255d502002-04-16 15:50:10 +00004980 int start = -1;
4981 int end = -1;
4982
Daniel Veillard777737e2006-10-17 21:23:17 +00004983 if (CUR == '\0') {
4984 ERROR("Expecting ']'");
4985 return;
4986 }
4987
Daniel Veillard4255d502002-04-16 15:50:10 +00004988 if ((CUR == '&') && (NXT(1) == '#')) {
4989 end = start = xmlFAParseCharRef(ctxt);
4990 xmlRegAtomAddRange(ctxt, ctxt->atom, ctxt->neg,
4991 XML_REGEXP_CHARVAL, start, end, NULL);
4992 return;
4993 }
4994 cur = CUR;
4995 if (cur == '\\') {
4996 NEXT;
4997 cur = CUR;
4998 switch (cur) {
4999 case 'n': start = 0xA; break;
5000 case 'r': start = 0xD; break;
5001 case 't': start = 0x9; break;
5002 case '\\': case '|': case '.': case '-': case '^': case '?':
5003 case '*': case '+': case '{': case '}': case '(': case ')':
5004 case '[': case ']':
5005 start = cur; break;
5006 default:
5007 ERROR("Invalid escape value");
5008 return;
5009 }
5010 end = start;
William M. Brackdc99df92003-12-27 01:54:25 +00005011 len = 1;
Daniel Veillard4255d502002-04-16 15:50:10 +00005012 } else if ((cur != 0x5B) && (cur != 0x5D)) {
William M. Brackdc99df92003-12-27 01:54:25 +00005013 end = start = CUR_SCHAR(ctxt->cur, len);
Daniel Veillard4255d502002-04-16 15:50:10 +00005014 } else {
5015 ERROR("Expecting a char range");
5016 return;
5017 }
William M. Bracka9cbf282007-03-21 13:16:33 +00005018 /*
5019 * Since we are "inside" a range, we can assume ctxt->cur is past
5020 * the start of ctxt->string, and PREV should be safe
5021 */
5022 if ((start == '-') && (NXT(1) != ']') && (PREV != '[') && (PREV != '^')) {
5023 NEXTL(len);
Daniel Veillard4255d502002-04-16 15:50:10 +00005024 return;
5025 }
William M. Bracka9cbf282007-03-21 13:16:33 +00005026 NEXTL(len);
Daniel Veillard4255d502002-04-16 15:50:10 +00005027 cur = CUR;
William M. Brack10f1ef42004-03-20 14:51:25 +00005028 if ((cur != '-') || (NXT(1) == ']')) {
Daniel Veillard4255d502002-04-16 15:50:10 +00005029 xmlRegAtomAddRange(ctxt, ctxt->atom, ctxt->neg,
5030 XML_REGEXP_CHARVAL, start, end, NULL);
5031 return;
5032 }
5033 NEXT;
5034 cur = CUR;
5035 if (cur == '\\') {
5036 NEXT;
5037 cur = CUR;
5038 switch (cur) {
5039 case 'n': end = 0xA; break;
5040 case 'r': end = 0xD; break;
5041 case 't': end = 0x9; break;
5042 case '\\': case '|': case '.': case '-': case '^': case '?':
5043 case '*': case '+': case '{': case '}': case '(': case ')':
5044 case '[': case ']':
5045 end = cur; break;
5046 default:
5047 ERROR("Invalid escape value");
5048 return;
5049 }
William M. Brackdc99df92003-12-27 01:54:25 +00005050 len = 1;
Daniel Veillard4255d502002-04-16 15:50:10 +00005051 } else if ((cur != 0x5B) && (cur != 0x5D)) {
William M. Brackdc99df92003-12-27 01:54:25 +00005052 end = CUR_SCHAR(ctxt->cur, len);
Daniel Veillard4255d502002-04-16 15:50:10 +00005053 } else {
5054 ERROR("Expecting the end of a char range");
5055 return;
5056 }
William M. Brackdc99df92003-12-27 01:54:25 +00005057 NEXTL(len);
Daniel Veillard4255d502002-04-16 15:50:10 +00005058 /* TODO check that the values are acceptable character ranges for XML */
5059 if (end < start) {
5060 ERROR("End of range is before start of range");
5061 } else {
5062 xmlRegAtomAddRange(ctxt, ctxt->atom, ctxt->neg,
5063 XML_REGEXP_CHARVAL, start, end, NULL);
5064 }
5065 return;
5066}
5067
5068/**
5069 * xmlFAParsePosCharGroup:
Daniel Veillard441bc322002-04-20 17:38:48 +00005070 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00005071 *
5072 * [14] posCharGroup ::= ( charRange | charClassEsc )+
5073 */
5074static void
5075xmlFAParsePosCharGroup(xmlRegParserCtxtPtr ctxt) {
5076 do {
Daniel Veillard041b6872008-02-08 10:37:18 +00005077 if (CUR == '\\') {
Daniel Veillard4255d502002-04-16 15:50:10 +00005078 xmlFAParseCharClassEsc(ctxt);
5079 } else {
5080 xmlFAParseCharRange(ctxt);
5081 }
5082 } while ((CUR != ']') && (CUR != '^') && (CUR != '-') &&
Daniel Veillard777737e2006-10-17 21:23:17 +00005083 (CUR != 0) && (ctxt->error == 0));
Daniel Veillard4255d502002-04-16 15:50:10 +00005084}
5085
5086/**
5087 * xmlFAParseCharGroup:
Daniel Veillard441bc322002-04-20 17:38:48 +00005088 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00005089 *
5090 * [13] charGroup ::= posCharGroup | negCharGroup | charClassSub
5091 * [15] negCharGroup ::= '^' posCharGroup
5092 * [16] charClassSub ::= ( posCharGroup | negCharGroup ) '-' charClassExpr
5093 * [12] charClassExpr ::= '[' charGroup ']'
5094 */
5095static void
5096xmlFAParseCharGroup(xmlRegParserCtxtPtr ctxt) {
5097 int n = ctxt->neg;
5098 while ((CUR != ']') && (ctxt->error == 0)) {
5099 if (CUR == '^') {
5100 int neg = ctxt->neg;
5101
5102 NEXT;
5103 ctxt->neg = !ctxt->neg;
5104 xmlFAParsePosCharGroup(ctxt);
5105 ctxt->neg = neg;
William M. Brack10f1ef42004-03-20 14:51:25 +00005106 } else if ((CUR == '-') && (NXT(1) == '[')) {
Daniel Veillardf8b9de32003-11-24 14:27:26 +00005107 int neg = ctxt->neg;
Daniel Veillardf8b9de32003-11-24 14:27:26 +00005108 ctxt->neg = 2;
William M. Brack10f1ef42004-03-20 14:51:25 +00005109 NEXT; /* eat the '-' */
5110 NEXT; /* eat the '[' */
Daniel Veillard4255d502002-04-16 15:50:10 +00005111 xmlFAParseCharGroup(ctxt);
5112 if (CUR == ']') {
5113 NEXT;
5114 } else {
5115 ERROR("charClassExpr: ']' expected");
5116 break;
5117 }
Daniel Veillardf8b9de32003-11-24 14:27:26 +00005118 ctxt->neg = neg;
Daniel Veillard4255d502002-04-16 15:50:10 +00005119 break;
5120 } else if (CUR != ']') {
5121 xmlFAParsePosCharGroup(ctxt);
5122 }
5123 }
5124 ctxt->neg = n;
5125}
5126
5127/**
5128 * xmlFAParseCharClass:
Daniel Veillard441bc322002-04-20 17:38:48 +00005129 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00005130 *
5131 * [11] charClass ::= charClassEsc | charClassExpr
5132 * [12] charClassExpr ::= '[' charGroup ']'
5133 */
5134static void
5135xmlFAParseCharClass(xmlRegParserCtxtPtr ctxt) {
5136 if (CUR == '[') {
5137 NEXT;
5138 ctxt->atom = xmlRegNewAtom(ctxt, XML_REGEXP_RANGES);
5139 if (ctxt->atom == NULL)
5140 return;
5141 xmlFAParseCharGroup(ctxt);
5142 if (CUR == ']') {
5143 NEXT;
5144 } else {
5145 ERROR("xmlFAParseCharClass: ']' expected");
5146 }
5147 } else {
5148 xmlFAParseCharClassEsc(ctxt);
5149 }
5150}
5151
5152/**
5153 * xmlFAParseQuantExact:
Daniel Veillard441bc322002-04-20 17:38:48 +00005154 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00005155 *
5156 * [8] QuantExact ::= [0-9]+
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00005157 *
5158 * Returns 0 if success or -1 in case of error
Daniel Veillard4255d502002-04-16 15:50:10 +00005159 */
5160static int
5161xmlFAParseQuantExact(xmlRegParserCtxtPtr ctxt) {
5162 int ret = 0;
5163 int ok = 0;
5164
5165 while ((CUR >= '0') && (CUR <= '9')) {
5166 ret = ret * 10 + (CUR - '0');
5167 ok = 1;
5168 NEXT;
5169 }
5170 if (ok != 1) {
5171 return(-1);
5172 }
5173 return(ret);
5174}
5175
5176/**
5177 * xmlFAParseQuantifier:
Daniel Veillard441bc322002-04-20 17:38:48 +00005178 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00005179 *
5180 * [4] quantifier ::= [?*+] | ( '{' quantity '}' )
5181 * [5] quantity ::= quantRange | quantMin | QuantExact
5182 * [6] quantRange ::= QuantExact ',' QuantExact
5183 * [7] quantMin ::= QuantExact ','
5184 * [8] QuantExact ::= [0-9]+
5185 */
5186static int
5187xmlFAParseQuantifier(xmlRegParserCtxtPtr ctxt) {
5188 int cur;
5189
5190 cur = CUR;
5191 if ((cur == '?') || (cur == '*') || (cur == '+')) {
5192 if (ctxt->atom != NULL) {
5193 if (cur == '?')
5194 ctxt->atom->quant = XML_REGEXP_QUANT_OPT;
5195 else if (cur == '*')
5196 ctxt->atom->quant = XML_REGEXP_QUANT_MULT;
5197 else if (cur == '+')
5198 ctxt->atom->quant = XML_REGEXP_QUANT_PLUS;
5199 }
5200 NEXT;
5201 return(1);
5202 }
5203 if (cur == '{') {
5204 int min = 0, max = 0;
5205
5206 NEXT;
5207 cur = xmlFAParseQuantExact(ctxt);
5208 if (cur >= 0)
5209 min = cur;
5210 if (CUR == ',') {
5211 NEXT;
Daniel Veillardebe48c62003-12-03 12:12:27 +00005212 if (CUR == '}')
5213 max = INT_MAX;
5214 else {
5215 cur = xmlFAParseQuantExact(ctxt);
5216 if (cur >= 0)
5217 max = cur;
5218 else {
5219 ERROR("Improper quantifier");
5220 }
5221 }
Daniel Veillard4255d502002-04-16 15:50:10 +00005222 }
5223 if (CUR == '}') {
5224 NEXT;
5225 } else {
5226 ERROR("Unterminated quantifier");
5227 }
5228 if (max == 0)
5229 max = min;
5230 if (ctxt->atom != NULL) {
5231 ctxt->atom->quant = XML_REGEXP_QUANT_RANGE;
5232 ctxt->atom->min = min;
5233 ctxt->atom->max = max;
5234 }
5235 return(1);
5236 }
5237 return(0);
5238}
5239
5240/**
5241 * xmlFAParseAtom:
Daniel Veillard441bc322002-04-20 17:38:48 +00005242 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00005243 *
5244 * [9] atom ::= Char | charClass | ( '(' regExp ')' )
5245 */
5246static int
5247xmlFAParseAtom(xmlRegParserCtxtPtr ctxt) {
5248 int codepoint, len;
5249
5250 codepoint = xmlFAIsChar(ctxt);
5251 if (codepoint > 0) {
5252 ctxt->atom = xmlRegNewAtom(ctxt, XML_REGEXP_CHARVAL);
5253 if (ctxt->atom == NULL)
5254 return(-1);
5255 codepoint = CUR_SCHAR(ctxt->cur, len);
5256 ctxt->atom->codepoint = codepoint;
5257 NEXTL(len);
5258 return(1);
5259 } else if (CUR == '|') {
5260 return(0);
5261 } else if (CUR == 0) {
5262 return(0);
5263 } else if (CUR == ')') {
5264 return(0);
5265 } else if (CUR == '(') {
Daniel Veillard76d59b62007-08-22 16:29:21 +00005266 xmlRegStatePtr start, oldend, start0;
Daniel Veillard4255d502002-04-16 15:50:10 +00005267
5268 NEXT;
Daniel Veillard76d59b62007-08-22 16:29:21 +00005269 /*
5270 * this extra Epsilon transition is needed if we count with 0 allowed
5271 * unfortunately this can't be known at that point
5272 */
5273 xmlFAGenerateEpsilonTransition(ctxt, ctxt->state, NULL);
5274 start0 = ctxt->state;
Daniel Veillard4255d502002-04-16 15:50:10 +00005275 xmlFAGenerateEpsilonTransition(ctxt, ctxt->state, NULL);
5276 start = ctxt->state;
5277 oldend = ctxt->end;
5278 ctxt->end = NULL;
5279 ctxt->atom = NULL;
5280 xmlFAParseRegExp(ctxt, 0);
5281 if (CUR == ')') {
5282 NEXT;
5283 } else {
5284 ERROR("xmlFAParseAtom: expecting ')'");
5285 }
5286 ctxt->atom = xmlRegNewAtom(ctxt, XML_REGEXP_SUBREG);
5287 if (ctxt->atom == NULL)
5288 return(-1);
5289 ctxt->atom->start = start;
Daniel Veillard76d59b62007-08-22 16:29:21 +00005290 ctxt->atom->start0 = start0;
Daniel Veillard4255d502002-04-16 15:50:10 +00005291 ctxt->atom->stop = ctxt->state;
5292 ctxt->end = oldend;
5293 return(1);
5294 } else if ((CUR == '[') || (CUR == '\\') || (CUR == '.')) {
5295 xmlFAParseCharClass(ctxt);
5296 return(1);
5297 }
5298 return(0);
5299}
5300
5301/**
5302 * xmlFAParsePiece:
Daniel Veillard441bc322002-04-20 17:38:48 +00005303 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00005304 *
5305 * [3] piece ::= atom quantifier?
5306 */
5307static int
5308xmlFAParsePiece(xmlRegParserCtxtPtr ctxt) {
5309 int ret;
5310
5311 ctxt->atom = NULL;
5312 ret = xmlFAParseAtom(ctxt);
5313 if (ret == 0)
5314 return(0);
5315 if (ctxt->atom == NULL) {
5316 ERROR("internal: no atom generated");
5317 }
5318 xmlFAParseQuantifier(ctxt);
5319 return(1);
5320}
5321
5322/**
5323 * xmlFAParseBranch:
Daniel Veillard441bc322002-04-20 17:38:48 +00005324 * @ctxt: a regexp parser context
Daniel Veillard54eb0242006-03-21 23:17:57 +00005325 * @to: optional target to the end of the branch
5326 *
5327 * @to is used to optimize by removing duplicate path in automata
5328 * in expressions like (a|b)(c|d)
Daniel Veillard4255d502002-04-16 15:50:10 +00005329 *
5330 * [2] branch ::= piece*
5331 */
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00005332static int
Daniel Veillard54eb0242006-03-21 23:17:57 +00005333xmlFAParseBranch(xmlRegParserCtxtPtr ctxt, xmlRegStatePtr to) {
Daniel Veillard4255d502002-04-16 15:50:10 +00005334 xmlRegStatePtr previous;
Daniel Veillard4255d502002-04-16 15:50:10 +00005335 int ret;
5336
5337 previous = ctxt->state;
5338 ret = xmlFAParsePiece(ctxt);
5339 if (ret != 0) {
Daniel Veillard54eb0242006-03-21 23:17:57 +00005340 if (xmlFAGenerateTransitions(ctxt, previous,
5341 (CUR=='|' || CUR==')') ? to : NULL, ctxt->atom) < 0)
Daniel Veillard2cbf5962004-03-31 15:50:43 +00005342 return(-1);
5343 previous = ctxt->state;
Daniel Veillard4255d502002-04-16 15:50:10 +00005344 ctxt->atom = NULL;
5345 }
5346 while ((ret != 0) && (ctxt->error == 0)) {
5347 ret = xmlFAParsePiece(ctxt);
5348 if (ret != 0) {
Daniel Veillard54eb0242006-03-21 23:17:57 +00005349 if (xmlFAGenerateTransitions(ctxt, previous,
5350 (CUR=='|' || CUR==')') ? to : NULL, ctxt->atom) < 0)
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00005351 return(-1);
Daniel Veillard4255d502002-04-16 15:50:10 +00005352 previous = ctxt->state;
5353 ctxt->atom = NULL;
5354 }
5355 }
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00005356 return(0);
Daniel Veillard4255d502002-04-16 15:50:10 +00005357}
5358
5359/**
5360 * xmlFAParseRegExp:
Daniel Veillard441bc322002-04-20 17:38:48 +00005361 * @ctxt: a regexp parser context
William M. Brackddf71d62004-05-06 04:17:26 +00005362 * @top: is this the top-level expression ?
Daniel Veillard4255d502002-04-16 15:50:10 +00005363 *
5364 * [1] regExp ::= branch ( '|' branch )*
5365 */
5366static void
5367xmlFAParseRegExp(xmlRegParserCtxtPtr ctxt, int top) {
Daniel Veillardc7e3cc42004-09-28 12:33:52 +00005368 xmlRegStatePtr start, end;
Daniel Veillard4255d502002-04-16 15:50:10 +00005369
Daniel Veillard2cbf5962004-03-31 15:50:43 +00005370 /* if not top start should have been generated by an epsilon trans */
Daniel Veillard4255d502002-04-16 15:50:10 +00005371 start = ctxt->state;
Daniel Veillard2cbf5962004-03-31 15:50:43 +00005372 ctxt->end = NULL;
Daniel Veillard54eb0242006-03-21 23:17:57 +00005373 xmlFAParseBranch(ctxt, NULL);
Daniel Veillard2cbf5962004-03-31 15:50:43 +00005374 if (top) {
5375#ifdef DEBUG_REGEXP_GRAPH
5376 printf("State %d is final\n", ctxt->state->no);
5377#endif
5378 ctxt->state->type = XML_REGEXP_FINAL_STATE;
5379 }
Daniel Veillard4255d502002-04-16 15:50:10 +00005380 if (CUR != '|') {
5381 ctxt->end = ctxt->state;
5382 return;
5383 }
5384 end = ctxt->state;
5385 while ((CUR == '|') && (ctxt->error == 0)) {
5386 NEXT;
5387 ctxt->state = start;
Daniel Veillard2cbf5962004-03-31 15:50:43 +00005388 ctxt->end = NULL;
Daniel Veillard54eb0242006-03-21 23:17:57 +00005389 xmlFAParseBranch(ctxt, end);
Daniel Veillard4255d502002-04-16 15:50:10 +00005390 }
Daniel Veillard2cbf5962004-03-31 15:50:43 +00005391 if (!top) {
5392 ctxt->state = end;
5393 ctxt->end = end;
5394 }
Daniel Veillard4255d502002-04-16 15:50:10 +00005395}
5396
5397/************************************************************************
5398 * *
5399 * The basic API *
5400 * *
5401 ************************************************************************/
5402
5403/**
5404 * xmlRegexpPrint:
5405 * @output: the file for the output debug
5406 * @regexp: the compiled regexp
5407 *
5408 * Print the content of the compiled regular expression
5409 */
5410void
5411xmlRegexpPrint(FILE *output, xmlRegexpPtr regexp) {
5412 int i;
5413
Daniel Veillarda82b1822004-11-08 16:24:57 +00005414 if (output == NULL)
5415 return;
Daniel Veillard4255d502002-04-16 15:50:10 +00005416 fprintf(output, " regexp: ");
5417 if (regexp == NULL) {
5418 fprintf(output, "NULL\n");
5419 return;
5420 }
5421 fprintf(output, "'%s' ", regexp->string);
5422 fprintf(output, "\n");
5423 fprintf(output, "%d atoms:\n", regexp->nbAtoms);
5424 for (i = 0;i < regexp->nbAtoms; i++) {
5425 fprintf(output, " %02d ", i);
5426 xmlRegPrintAtom(output, regexp->atoms[i]);
5427 }
5428 fprintf(output, "%d states:", regexp->nbStates);
5429 fprintf(output, "\n");
5430 for (i = 0;i < regexp->nbStates; i++) {
5431 xmlRegPrintState(output, regexp->states[i]);
5432 }
5433 fprintf(output, "%d counters:\n", regexp->nbCounters);
5434 for (i = 0;i < regexp->nbCounters; i++) {
5435 fprintf(output, " %d: min %d max %d\n", i, regexp->counters[i].min,
5436 regexp->counters[i].max);
5437 }
5438}
5439
5440/**
5441 * xmlRegexpCompile:
5442 * @regexp: a regular expression string
5443 *
5444 * Parses a regular expression conforming to XML Schemas Part 2 Datatype
William M. Brackddf71d62004-05-06 04:17:26 +00005445 * Appendix F and builds an automata suitable for testing strings against
Daniel Veillard4255d502002-04-16 15:50:10 +00005446 * that regular expression
5447 *
5448 * Returns the compiled expression or NULL in case of error
5449 */
5450xmlRegexpPtr
5451xmlRegexpCompile(const xmlChar *regexp) {
5452 xmlRegexpPtr ret;
5453 xmlRegParserCtxtPtr ctxt;
5454
5455 ctxt = xmlRegNewParserCtxt(regexp);
5456 if (ctxt == NULL)
5457 return(NULL);
5458
5459 /* initialize the parser */
5460 ctxt->end = NULL;
5461 ctxt->start = ctxt->state = xmlRegNewState(ctxt);
5462 xmlRegStatePush(ctxt, ctxt->start);
5463
5464 /* parse the expression building an automata */
5465 xmlFAParseRegExp(ctxt, 1);
5466 if (CUR != 0) {
5467 ERROR("xmlFAParseRegExp: extra characters");
5468 }
Daniel Veillardcb4284e2007-04-25 13:55:20 +00005469 if (ctxt->error != 0) {
5470 xmlRegFreeParserCtxt(ctxt);
5471 return(NULL);
5472 }
Daniel Veillard4255d502002-04-16 15:50:10 +00005473 ctxt->end = ctxt->state;
5474 ctxt->start->type = XML_REGEXP_START_STATE;
5475 ctxt->end->type = XML_REGEXP_FINAL_STATE;
5476
5477 /* remove the Epsilon except for counted transitions */
5478 xmlFAEliminateEpsilonTransitions(ctxt);
5479
5480
5481 if (ctxt->error != 0) {
5482 xmlRegFreeParserCtxt(ctxt);
5483 return(NULL);
5484 }
5485 ret = xmlRegEpxFromParse(ctxt);
5486 xmlRegFreeParserCtxt(ctxt);
5487 return(ret);
5488}
5489
5490/**
5491 * xmlRegexpExec:
5492 * @comp: the compiled regular expression
5493 * @content: the value to check against the regular expression
5494 *
William M. Brackddf71d62004-05-06 04:17:26 +00005495 * Check if the regular expression generates the value
Daniel Veillard4255d502002-04-16 15:50:10 +00005496 *
William M. Brackddf71d62004-05-06 04:17:26 +00005497 * Returns 1 if it matches, 0 if not and a negative value in case of error
Daniel Veillard4255d502002-04-16 15:50:10 +00005498 */
5499int
5500xmlRegexpExec(xmlRegexpPtr comp, const xmlChar *content) {
5501 if ((comp == NULL) || (content == NULL))
5502 return(-1);
5503 return(xmlFARegExec(comp, content));
5504}
5505
5506/**
Daniel Veillard23e73572002-09-19 19:56:43 +00005507 * xmlRegexpIsDeterminist:
5508 * @comp: the compiled regular expression
5509 *
5510 * Check if the regular expression is determinist
5511 *
William M. Brackddf71d62004-05-06 04:17:26 +00005512 * Returns 1 if it yes, 0 if not and a negative value in case of error
Daniel Veillard23e73572002-09-19 19:56:43 +00005513 */
5514int
5515xmlRegexpIsDeterminist(xmlRegexpPtr comp) {
5516 xmlAutomataPtr am;
5517 int ret;
5518
5519 if (comp == NULL)
5520 return(-1);
5521 if (comp->determinist != -1)
5522 return(comp->determinist);
5523
5524 am = xmlNewAutomata();
Daniel Veillardbd9afb52002-09-25 22:25:35 +00005525 if (am->states != NULL) {
5526 int i;
5527
5528 for (i = 0;i < am->nbStates;i++)
5529 xmlRegFreeState(am->states[i]);
5530 xmlFree(am->states);
5531 }
Daniel Veillard23e73572002-09-19 19:56:43 +00005532 am->nbAtoms = comp->nbAtoms;
5533 am->atoms = comp->atoms;
5534 am->nbStates = comp->nbStates;
5535 am->states = comp->states;
5536 am->determinist = -1;
5537 ret = xmlFAComputesDeterminism(am);
5538 am->atoms = NULL;
5539 am->states = NULL;
5540 xmlFreeAutomata(am);
5541 return(ret);
5542}
5543
5544/**
Daniel Veillard4255d502002-04-16 15:50:10 +00005545 * xmlRegFreeRegexp:
5546 * @regexp: the regexp
5547 *
5548 * Free a regexp
5549 */
5550void
5551xmlRegFreeRegexp(xmlRegexpPtr regexp) {
5552 int i;
5553 if (regexp == NULL)
5554 return;
5555
5556 if (regexp->string != NULL)
5557 xmlFree(regexp->string);
5558 if (regexp->states != NULL) {
5559 for (i = 0;i < regexp->nbStates;i++)
5560 xmlRegFreeState(regexp->states[i]);
5561 xmlFree(regexp->states);
5562 }
5563 if (regexp->atoms != NULL) {
5564 for (i = 0;i < regexp->nbAtoms;i++)
5565 xmlRegFreeAtom(regexp->atoms[i]);
5566 xmlFree(regexp->atoms);
5567 }
5568 if (regexp->counters != NULL)
5569 xmlFree(regexp->counters);
Daniel Veillard23e73572002-09-19 19:56:43 +00005570 if (regexp->compact != NULL)
5571 xmlFree(regexp->compact);
Daniel Veillard118aed72002-09-24 14:13:13 +00005572 if (regexp->transdata != NULL)
5573 xmlFree(regexp->transdata);
Daniel Veillard23e73572002-09-19 19:56:43 +00005574 if (regexp->stringMap != NULL) {
5575 for (i = 0; i < regexp->nbstrings;i++)
5576 xmlFree(regexp->stringMap[i]);
5577 xmlFree(regexp->stringMap);
5578 }
5579
Daniel Veillard4255d502002-04-16 15:50:10 +00005580 xmlFree(regexp);
5581}
5582
5583#ifdef LIBXML_AUTOMATA_ENABLED
5584/************************************************************************
5585 * *
5586 * The Automata interface *
5587 * *
5588 ************************************************************************/
5589
5590/**
5591 * xmlNewAutomata:
5592 *
5593 * Create a new automata
5594 *
5595 * Returns the new object or NULL in case of failure
5596 */
5597xmlAutomataPtr
5598xmlNewAutomata(void) {
5599 xmlAutomataPtr ctxt;
5600
5601 ctxt = xmlRegNewParserCtxt(NULL);
5602 if (ctxt == NULL)
5603 return(NULL);
5604
5605 /* initialize the parser */
5606 ctxt->end = NULL;
5607 ctxt->start = ctxt->state = xmlRegNewState(ctxt);
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00005608 if (ctxt->start == NULL) {
5609 xmlFreeAutomata(ctxt);
5610 return(NULL);
5611 }
Daniel Veillardd0271472006-01-02 10:22:02 +00005612 ctxt->start->type = XML_REGEXP_START_STATE;
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00005613 if (xmlRegStatePush(ctxt, ctxt->start) < 0) {
5614 xmlRegFreeState(ctxt->start);
5615 xmlFreeAutomata(ctxt);
5616 return(NULL);
5617 }
Daniel Veillard4255d502002-04-16 15:50:10 +00005618
5619 return(ctxt);
5620}
5621
5622/**
5623 * xmlFreeAutomata:
5624 * @am: an automata
5625 *
5626 * Free an automata
5627 */
5628void
5629xmlFreeAutomata(xmlAutomataPtr am) {
5630 if (am == NULL)
5631 return;
5632 xmlRegFreeParserCtxt(am);
5633}
5634
5635/**
5636 * xmlAutomataGetInitState:
5637 * @am: an automata
5638 *
Daniel Veillarda9b66d02002-12-11 14:23:49 +00005639 * Initial state lookup
5640 *
Daniel Veillard4255d502002-04-16 15:50:10 +00005641 * Returns the initial state of the automata
5642 */
5643xmlAutomataStatePtr
5644xmlAutomataGetInitState(xmlAutomataPtr am) {
5645 if (am == NULL)
5646 return(NULL);
5647 return(am->start);
5648}
5649
5650/**
5651 * xmlAutomataSetFinalState:
5652 * @am: an automata
5653 * @state: a state in this automata
5654 *
5655 * Makes that state a final state
5656 *
5657 * Returns 0 or -1 in case of error
5658 */
5659int
5660xmlAutomataSetFinalState(xmlAutomataPtr am, xmlAutomataStatePtr state) {
5661 if ((am == NULL) || (state == NULL))
5662 return(-1);
5663 state->type = XML_REGEXP_FINAL_STATE;
5664 return(0);
5665}
5666
5667/**
5668 * xmlAutomataNewTransition:
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 * @data: data passed to the callback function if the transition is activated
5674 *
William M. Brackddf71d62004-05-06 04:17:26 +00005675 * If @to is NULL, this creates first a new target state in the automata
Daniel Veillard4255d502002-04-16 15:50:10 +00005676 * and then adds a transition from the @from state to the target state
5677 * activated by the value of @token
5678 *
5679 * Returns the target state or NULL in case of error
5680 */
5681xmlAutomataStatePtr
5682xmlAutomataNewTransition(xmlAutomataPtr am, xmlAutomataStatePtr from,
5683 xmlAutomataStatePtr to, const xmlChar *token,
5684 void *data) {
5685 xmlRegAtomPtr atom;
5686
5687 if ((am == NULL) || (from == NULL) || (token == NULL))
5688 return(NULL);
5689 atom = xmlRegNewAtom(am, XML_REGEXP_STRING);
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00005690 if (atom == NULL)
5691 return(NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +00005692 atom->data = data;
5693 if (atom == NULL)
5694 return(NULL);
5695 atom->valuep = xmlStrdup(token);
5696
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00005697 if (xmlFAGenerateTransitions(am, from, to, atom) < 0) {
5698 xmlRegFreeAtom(atom);
5699 return(NULL);
5700 }
Daniel Veillard4255d502002-04-16 15:50:10 +00005701 if (to == NULL)
5702 return(am->state);
5703 return(to);
5704}
5705
5706/**
Daniel Veillard52b48c72003-04-13 19:53:42 +00005707 * xmlAutomataNewTransition2:
5708 * @am: an automata
5709 * @from: the starting point of the transition
5710 * @to: the target point of the transition or NULL
5711 * @token: the first input string associated to that transition
5712 * @token2: the second input string associated to that transition
5713 * @data: data passed to the callback function if the transition is activated
5714 *
William M. Brackddf71d62004-05-06 04:17:26 +00005715 * If @to is NULL, this creates first a new target state in the automata
Daniel Veillard52b48c72003-04-13 19:53:42 +00005716 * and then adds a transition from the @from state to the target state
5717 * activated by the value of @token
5718 *
5719 * Returns the target state or NULL in case of error
5720 */
5721xmlAutomataStatePtr
5722xmlAutomataNewTransition2(xmlAutomataPtr am, xmlAutomataStatePtr from,
5723 xmlAutomataStatePtr to, const xmlChar *token,
5724 const xmlChar *token2, void *data) {
5725 xmlRegAtomPtr atom;
5726
5727 if ((am == NULL) || (from == NULL) || (token == NULL))
5728 return(NULL);
5729 atom = xmlRegNewAtom(am, XML_REGEXP_STRING);
Daniel Veillard52b48c72003-04-13 19:53:42 +00005730 if (atom == NULL)
5731 return(NULL);
Daniel Veillard11ce4002006-03-10 00:36:23 +00005732 atom->data = data;
Daniel Veillard52b48c72003-04-13 19:53:42 +00005733 if ((token2 == NULL) || (*token2 == 0)) {
5734 atom->valuep = xmlStrdup(token);
5735 } else {
5736 int lenn, lenp;
5737 xmlChar *str;
5738
5739 lenn = strlen((char *) token2);
5740 lenp = strlen((char *) token);
5741
Daniel Veillard3c908dc2003-04-19 00:07:51 +00005742 str = (xmlChar *) xmlMallocAtomic(lenn + lenp + 2);
Daniel Veillard52b48c72003-04-13 19:53:42 +00005743 if (str == NULL) {
5744 xmlRegFreeAtom(atom);
5745 return(NULL);
5746 }
5747 memcpy(&str[0], token, lenp);
5748 str[lenp] = '|';
5749 memcpy(&str[lenp + 1], token2, lenn);
5750 str[lenn + lenp + 1] = 0;
5751
5752 atom->valuep = str;
5753 }
5754
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00005755 if (xmlFAGenerateTransitions(am, from, to, atom) < 0) {
5756 xmlRegFreeAtom(atom);
5757 return(NULL);
5758 }
Daniel Veillard52b48c72003-04-13 19:53:42 +00005759 if (to == NULL)
5760 return(am->state);
5761 return(to);
5762}
5763
5764/**
Daniel Veillard9efc4762005-07-19 14:33:55 +00005765 * xmlAutomataNewNegTrans:
5766 * @am: an automata
5767 * @from: the starting point of the transition
5768 * @to: the target point of the transition or NULL
5769 * @token: the first input string associated to that transition
5770 * @token2: the second input string associated to that transition
5771 * @data: data passed to the callback function if the transition is activated
5772 *
5773 * If @to is NULL, this creates first a new target state in the automata
5774 * and then adds a transition from the @from state to the target state
5775 * activated by any value except (@token,@token2)
Daniel Veillard6e65e152005-08-09 11:09:52 +00005776 * Note that if @token2 is not NULL, then (X, NULL) won't match to follow
5777 # the semantic of XSD ##other
Daniel Veillard9efc4762005-07-19 14:33:55 +00005778 *
5779 * Returns the target state or NULL in case of error
5780 */
5781xmlAutomataStatePtr
5782xmlAutomataNewNegTrans(xmlAutomataPtr am, xmlAutomataStatePtr from,
5783 xmlAutomataStatePtr to, const xmlChar *token,
5784 const xmlChar *token2, void *data) {
5785 xmlRegAtomPtr atom;
Daniel Veillard77005e62005-07-19 16:26:18 +00005786 xmlChar err_msg[200];
Daniel Veillard9efc4762005-07-19 14:33:55 +00005787
5788 if ((am == NULL) || (from == NULL) || (token == NULL))
5789 return(NULL);
5790 atom = xmlRegNewAtom(am, XML_REGEXP_STRING);
5791 if (atom == NULL)
5792 return(NULL);
5793 atom->data = data;
5794 atom->neg = 1;
5795 if ((token2 == NULL) || (*token2 == 0)) {
5796 atom->valuep = xmlStrdup(token);
5797 } else {
5798 int lenn, lenp;
5799 xmlChar *str;
5800
5801 lenn = strlen((char *) token2);
5802 lenp = strlen((char *) token);
5803
5804 str = (xmlChar *) xmlMallocAtomic(lenn + lenp + 2);
5805 if (str == NULL) {
5806 xmlRegFreeAtom(atom);
5807 return(NULL);
5808 }
5809 memcpy(&str[0], token, lenp);
5810 str[lenp] = '|';
5811 memcpy(&str[lenp + 1], token2, lenn);
5812 str[lenn + lenp + 1] = 0;
5813
5814 atom->valuep = str;
5815 }
Daniel Veillarddb68b742005-07-30 13:18:24 +00005816 snprintf((char *) err_msg, 199, "not %s", (const char *) atom->valuep);
Daniel Veillard77005e62005-07-19 16:26:18 +00005817 err_msg[199] = 0;
5818 atom->valuep2 = xmlStrdup(err_msg);
Daniel Veillard9efc4762005-07-19 14:33:55 +00005819
5820 if (xmlFAGenerateTransitions(am, from, to, atom) < 0) {
5821 xmlRegFreeAtom(atom);
5822 return(NULL);
5823 }
Daniel Veillard6e65e152005-08-09 11:09:52 +00005824 am->negs++;
Daniel Veillard9efc4762005-07-19 14:33:55 +00005825 if (to == NULL)
5826 return(am->state);
5827 return(to);
5828}
5829
5830/**
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00005831 * xmlAutomataNewCountTrans2:
5832 * @am: an automata
5833 * @from: the starting point of the transition
5834 * @to: the target point of the transition or NULL
5835 * @token: the input string associated to that transition
5836 * @token2: the second input string associated to that transition
5837 * @min: the minimum successive occurences of token
5838 * @max: the maximum successive occurences of token
5839 * @data: data associated to the transition
5840 *
5841 * If @to is NULL, this creates first a new target state in the automata
5842 * and then adds a transition from the @from state to the target state
5843 * activated by a succession of input of value @token and @token2 and
5844 * whose number is between @min and @max
5845 *
5846 * Returns the target state or NULL in case of error
5847 */
5848xmlAutomataStatePtr
5849xmlAutomataNewCountTrans2(xmlAutomataPtr am, xmlAutomataStatePtr from,
5850 xmlAutomataStatePtr to, const xmlChar *token,
5851 const xmlChar *token2,
5852 int min, int max, void *data) {
5853 xmlRegAtomPtr atom;
5854 int counter;
5855
5856 if ((am == NULL) || (from == NULL) || (token == NULL))
5857 return(NULL);
5858 if (min < 0)
5859 return(NULL);
5860 if ((max < min) || (max < 1))
5861 return(NULL);
5862 atom = xmlRegNewAtom(am, XML_REGEXP_STRING);
5863 if (atom == NULL)
5864 return(NULL);
5865 if ((token2 == NULL) || (*token2 == 0)) {
5866 atom->valuep = xmlStrdup(token);
5867 } else {
5868 int lenn, lenp;
5869 xmlChar *str;
5870
5871 lenn = strlen((char *) token2);
5872 lenp = strlen((char *) token);
5873
5874 str = (xmlChar *) xmlMallocAtomic(lenn + lenp + 2);
5875 if (str == NULL) {
5876 xmlRegFreeAtom(atom);
5877 return(NULL);
5878 }
5879 memcpy(&str[0], token, lenp);
5880 str[lenp] = '|';
5881 memcpy(&str[lenp + 1], token2, lenn);
5882 str[lenn + lenp + 1] = 0;
5883
5884 atom->valuep = str;
5885 }
5886 atom->data = data;
5887 if (min == 0)
5888 atom->min = 1;
5889 else
5890 atom->min = min;
5891 atom->max = max;
5892
5893 /*
5894 * associate a counter to the transition.
5895 */
5896 counter = xmlRegGetCounter(am);
5897 am->counters[counter].min = min;
5898 am->counters[counter].max = max;
5899
5900 /* xmlFAGenerateTransitions(am, from, to, atom); */
5901 if (to == NULL) {
5902 to = xmlRegNewState(am);
5903 xmlRegStatePush(am, to);
5904 }
Daniel Veillard5de09382005-09-26 17:18:17 +00005905 xmlRegStateAddTrans(am, from, atom, to, counter, -1);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00005906 xmlRegAtomPush(am, atom);
5907 am->state = to;
5908
5909 if (to == NULL)
5910 to = am->state;
5911 if (to == NULL)
5912 return(NULL);
5913 if (min == 0)
5914 xmlFAGenerateEpsilonTransition(am, from, to);
5915 return(to);
5916}
5917
5918/**
Daniel Veillard4255d502002-04-16 15:50:10 +00005919 * xmlAutomataNewCountTrans:
5920 * @am: an automata
5921 * @from: the starting point of the transition
5922 * @to: the target point of the transition or NULL
5923 * @token: the input string associated to that transition
5924 * @min: the minimum successive occurences of token
Daniel Veillarda9b66d02002-12-11 14:23:49 +00005925 * @max: the maximum successive occurences of token
5926 * @data: data associated to the transition
Daniel Veillard4255d502002-04-16 15:50:10 +00005927 *
William M. Brackddf71d62004-05-06 04:17:26 +00005928 * If @to is NULL, this creates first a new target state in the automata
Daniel Veillard4255d502002-04-16 15:50:10 +00005929 * and then adds a transition from the @from state to the target state
5930 * activated by a succession of input of value @token and whose number
5931 * is between @min and @max
5932 *
5933 * Returns the target state or NULL in case of error
5934 */
5935xmlAutomataStatePtr
5936xmlAutomataNewCountTrans(xmlAutomataPtr am, xmlAutomataStatePtr from,
5937 xmlAutomataStatePtr to, const xmlChar *token,
5938 int min, int max, void *data) {
5939 xmlRegAtomPtr atom;
Daniel Veillard0ddb21c2004-02-12 12:43:49 +00005940 int counter;
Daniel Veillard4255d502002-04-16 15:50:10 +00005941
5942 if ((am == NULL) || (from == NULL) || (token == NULL))
5943 return(NULL);
5944 if (min < 0)
5945 return(NULL);
5946 if ((max < min) || (max < 1))
5947 return(NULL);
5948 atom = xmlRegNewAtom(am, XML_REGEXP_STRING);
5949 if (atom == NULL)
5950 return(NULL);
5951 atom->valuep = xmlStrdup(token);
5952 atom->data = data;
5953 if (min == 0)
5954 atom->min = 1;
5955 else
5956 atom->min = min;
5957 atom->max = max;
5958
Daniel Veillard0ddb21c2004-02-12 12:43:49 +00005959 /*
5960 * associate a counter to the transition.
5961 */
5962 counter = xmlRegGetCounter(am);
5963 am->counters[counter].min = min;
5964 am->counters[counter].max = max;
5965
5966 /* xmlFAGenerateTransitions(am, from, to, atom); */
5967 if (to == NULL) {
5968 to = xmlRegNewState(am);
5969 xmlRegStatePush(am, to);
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00005970 }
Daniel Veillard5de09382005-09-26 17:18:17 +00005971 xmlRegStateAddTrans(am, from, atom, to, counter, -1);
Daniel Veillard0ddb21c2004-02-12 12:43:49 +00005972 xmlRegAtomPush(am, atom);
5973 am->state = to;
5974
Daniel Veillard4255d502002-04-16 15:50:10 +00005975 if (to == NULL)
5976 to = am->state;
5977 if (to == NULL)
5978 return(NULL);
5979 if (min == 0)
5980 xmlFAGenerateEpsilonTransition(am, from, to);
5981 return(to);
5982}
5983
5984/**
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00005985 * xmlAutomataNewOnceTrans2:
5986 * @am: an automata
5987 * @from: the starting point of the transition
5988 * @to: the target point of the transition or NULL
5989 * @token: the input string associated to that transition
5990 * @token2: the second input string associated to that transition
5991 * @min: the minimum successive occurences of token
5992 * @max: the maximum successive occurences of token
5993 * @data: data associated to the transition
5994 *
5995 * If @to is NULL, this creates first a new target state in the automata
5996 * and then adds a transition from the @from state to the target state
5997 * activated by a succession of input of value @token and @token2 and whose
5998 * number is between @min and @max, moreover that transition can only be
5999 * crossed once.
6000 *
6001 * Returns the target state or NULL in case of error
6002 */
6003xmlAutomataStatePtr
6004xmlAutomataNewOnceTrans2(xmlAutomataPtr am, xmlAutomataStatePtr from,
6005 xmlAutomataStatePtr to, const xmlChar *token,
6006 const xmlChar *token2,
6007 int min, int max, void *data) {
6008 xmlRegAtomPtr atom;
6009 int counter;
6010
6011 if ((am == NULL) || (from == NULL) || (token == NULL))
6012 return(NULL);
6013 if (min < 1)
6014 return(NULL);
6015 if ((max < min) || (max < 1))
6016 return(NULL);
6017 atom = xmlRegNewAtom(am, XML_REGEXP_STRING);
6018 if (atom == NULL)
6019 return(NULL);
6020 if ((token2 == NULL) || (*token2 == 0)) {
6021 atom->valuep = xmlStrdup(token);
6022 } else {
6023 int lenn, lenp;
6024 xmlChar *str;
6025
6026 lenn = strlen((char *) token2);
6027 lenp = strlen((char *) token);
6028
6029 str = (xmlChar *) xmlMallocAtomic(lenn + lenp + 2);
6030 if (str == NULL) {
6031 xmlRegFreeAtom(atom);
6032 return(NULL);
6033 }
6034 memcpy(&str[0], token, lenp);
6035 str[lenp] = '|';
6036 memcpy(&str[lenp + 1], token2, lenn);
6037 str[lenn + lenp + 1] = 0;
6038
6039 atom->valuep = str;
6040 }
6041 atom->data = data;
6042 atom->quant = XML_REGEXP_QUANT_ONCEONLY;
Daniel Veillard11ce4002006-03-10 00:36:23 +00006043 atom->min = min;
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00006044 atom->max = max;
6045 /*
6046 * associate a counter to the transition.
6047 */
6048 counter = xmlRegGetCounter(am);
6049 am->counters[counter].min = 1;
6050 am->counters[counter].max = 1;
6051
6052 /* xmlFAGenerateTransitions(am, from, to, atom); */
6053 if (to == NULL) {
6054 to = xmlRegNewState(am);
6055 xmlRegStatePush(am, to);
6056 }
Daniel Veillard5de09382005-09-26 17:18:17 +00006057 xmlRegStateAddTrans(am, from, atom, to, counter, -1);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00006058 xmlRegAtomPush(am, atom);
6059 am->state = to;
6060 return(to);
6061}
6062
6063
6064
6065/**
Daniel Veillard7646b182002-04-20 06:41:40 +00006066 * xmlAutomataNewOnceTrans:
6067 * @am: an automata
6068 * @from: the starting point of the transition
6069 * @to: the target point of the transition or NULL
6070 * @token: the input string associated to that transition
6071 * @min: the minimum successive occurences of token
Daniel Veillarda9b66d02002-12-11 14:23:49 +00006072 * @max: the maximum successive occurences of token
6073 * @data: data associated to the transition
Daniel Veillard7646b182002-04-20 06:41:40 +00006074 *
William M. Brackddf71d62004-05-06 04:17:26 +00006075 * If @to is NULL, this creates first a new target state in the automata
Daniel Veillard7646b182002-04-20 06:41:40 +00006076 * and then adds a transition from the @from state to the target state
6077 * activated by a succession of input of value @token and whose number
William M. Brackddf71d62004-05-06 04:17:26 +00006078 * is between @min and @max, moreover that transition can only be crossed
Daniel Veillard7646b182002-04-20 06:41:40 +00006079 * once.
6080 *
6081 * Returns the target state or NULL in case of error
6082 */
6083xmlAutomataStatePtr
6084xmlAutomataNewOnceTrans(xmlAutomataPtr am, xmlAutomataStatePtr from,
6085 xmlAutomataStatePtr to, const xmlChar *token,
6086 int min, int max, void *data) {
6087 xmlRegAtomPtr atom;
6088 int counter;
6089
6090 if ((am == NULL) || (from == NULL) || (token == NULL))
6091 return(NULL);
6092 if (min < 1)
6093 return(NULL);
6094 if ((max < min) || (max < 1))
6095 return(NULL);
6096 atom = xmlRegNewAtom(am, XML_REGEXP_STRING);
6097 if (atom == NULL)
6098 return(NULL);
6099 atom->valuep = xmlStrdup(token);
6100 atom->data = data;
6101 atom->quant = XML_REGEXP_QUANT_ONCEONLY;
Daniel Veillard11ce4002006-03-10 00:36:23 +00006102 atom->min = min;
Daniel Veillard7646b182002-04-20 06:41:40 +00006103 atom->max = max;
6104 /*
6105 * associate a counter to the transition.
6106 */
6107 counter = xmlRegGetCounter(am);
6108 am->counters[counter].min = 1;
6109 am->counters[counter].max = 1;
6110
6111 /* xmlFAGenerateTransitions(am, from, to, atom); */
6112 if (to == NULL) {
6113 to = xmlRegNewState(am);
6114 xmlRegStatePush(am, to);
6115 }
Daniel Veillard5de09382005-09-26 17:18:17 +00006116 xmlRegStateAddTrans(am, from, atom, to, counter, -1);
Daniel Veillard7646b182002-04-20 06:41:40 +00006117 xmlRegAtomPush(am, atom);
6118 am->state = to;
Daniel Veillard7646b182002-04-20 06:41:40 +00006119 return(to);
6120}
6121
6122/**
Daniel Veillard4255d502002-04-16 15:50:10 +00006123 * xmlAutomataNewState:
6124 * @am: an automata
6125 *
6126 * Create a new disconnected state in the automata
6127 *
6128 * Returns the new state or NULL in case of error
6129 */
6130xmlAutomataStatePtr
6131xmlAutomataNewState(xmlAutomataPtr am) {
6132 xmlAutomataStatePtr to;
6133
6134 if (am == NULL)
6135 return(NULL);
6136 to = xmlRegNewState(am);
6137 xmlRegStatePush(am, to);
6138 return(to);
6139}
6140
6141/**
Daniel Veillarda9b66d02002-12-11 14:23:49 +00006142 * xmlAutomataNewEpsilon:
Daniel Veillard4255d502002-04-16 15:50:10 +00006143 * @am: an automata
6144 * @from: the starting point of the transition
6145 * @to: the target point of the transition or NULL
6146 *
William M. Brackddf71d62004-05-06 04:17:26 +00006147 * If @to is NULL, this creates first a new target state in the automata
6148 * and then adds an epsilon transition from the @from state to the
Daniel Veillard4255d502002-04-16 15:50:10 +00006149 * target state
6150 *
6151 * Returns the target state or NULL in case of error
6152 */
6153xmlAutomataStatePtr
6154xmlAutomataNewEpsilon(xmlAutomataPtr am, xmlAutomataStatePtr from,
6155 xmlAutomataStatePtr to) {
6156 if ((am == NULL) || (from == NULL))
6157 return(NULL);
6158 xmlFAGenerateEpsilonTransition(am, from, to);
6159 if (to == NULL)
6160 return(am->state);
6161 return(to);
6162}
6163
Daniel Veillardb509f152002-04-17 16:28:10 +00006164/**
Daniel Veillard7646b182002-04-20 06:41:40 +00006165 * xmlAutomataNewAllTrans:
6166 * @am: an automata
6167 * @from: the starting point of the transition
6168 * @to: the target point of the transition or NULL
Daniel Veillarda9b66d02002-12-11 14:23:49 +00006169 * @lax: allow to transition if not all all transitions have been activated
Daniel Veillard7646b182002-04-20 06:41:40 +00006170 *
William M. Brackddf71d62004-05-06 04:17:26 +00006171 * If @to is NULL, this creates first a new target state in the automata
Daniel Veillard7646b182002-04-20 06:41:40 +00006172 * and then adds a an ALL transition from the @from state to the
6173 * target state. That transition is an epsilon transition allowed only when
6174 * all transitions from the @from node have been activated.
6175 *
6176 * Returns the target state or NULL in case of error
6177 */
6178xmlAutomataStatePtr
6179xmlAutomataNewAllTrans(xmlAutomataPtr am, xmlAutomataStatePtr from,
Daniel Veillard441bc322002-04-20 17:38:48 +00006180 xmlAutomataStatePtr to, int lax) {
Daniel Veillard7646b182002-04-20 06:41:40 +00006181 if ((am == NULL) || (from == NULL))
6182 return(NULL);
Daniel Veillard441bc322002-04-20 17:38:48 +00006183 xmlFAGenerateAllTransition(am, from, to, lax);
Daniel Veillard7646b182002-04-20 06:41:40 +00006184 if (to == NULL)
6185 return(am->state);
6186 return(to);
6187}
6188
6189/**
Daniel Veillardb509f152002-04-17 16:28:10 +00006190 * xmlAutomataNewCounter:
6191 * @am: an automata
6192 * @min: the minimal value on the counter
6193 * @max: the maximal value on the counter
6194 *
6195 * Create a new counter
6196 *
6197 * Returns the counter number or -1 in case of error
6198 */
6199int
6200xmlAutomataNewCounter(xmlAutomataPtr am, int min, int max) {
6201 int ret;
6202
6203 if (am == NULL)
6204 return(-1);
6205
6206 ret = xmlRegGetCounter(am);
6207 if (ret < 0)
6208 return(-1);
6209 am->counters[ret].min = min;
6210 am->counters[ret].max = max;
6211 return(ret);
6212}
6213
6214/**
6215 * xmlAutomataNewCountedTrans:
6216 * @am: an automata
6217 * @from: the starting point of the transition
6218 * @to: the target point of the transition or NULL
6219 * @counter: the counter associated to that transition
6220 *
William M. Brackddf71d62004-05-06 04:17:26 +00006221 * If @to is NULL, this creates first a new target state in the automata
Daniel Veillardb509f152002-04-17 16:28:10 +00006222 * and then adds an epsilon transition from the @from state to the target state
6223 * which will increment the counter provided
6224 *
6225 * Returns the target state or NULL in case of error
6226 */
6227xmlAutomataStatePtr
6228xmlAutomataNewCountedTrans(xmlAutomataPtr am, xmlAutomataStatePtr from,
6229 xmlAutomataStatePtr to, int counter) {
6230 if ((am == NULL) || (from == NULL) || (counter < 0))
6231 return(NULL);
6232 xmlFAGenerateCountedEpsilonTransition(am, from, to, counter);
6233 if (to == NULL)
6234 return(am->state);
6235 return(to);
6236}
6237
6238/**
6239 * xmlAutomataNewCounterTrans:
6240 * @am: an automata
6241 * @from: the starting point of the transition
6242 * @to: the target point of the transition or NULL
6243 * @counter: the counter associated to that transition
6244 *
William M. Brackddf71d62004-05-06 04:17:26 +00006245 * If @to is NULL, this creates first a new target state in the automata
Daniel Veillardb509f152002-04-17 16:28:10 +00006246 * and then adds an epsilon transition from the @from state to the target state
6247 * which will be allowed only if the counter is within the right range.
6248 *
6249 * Returns the target state or NULL in case of error
6250 */
6251xmlAutomataStatePtr
6252xmlAutomataNewCounterTrans(xmlAutomataPtr am, xmlAutomataStatePtr from,
6253 xmlAutomataStatePtr to, int counter) {
6254 if ((am == NULL) || (from == NULL) || (counter < 0))
6255 return(NULL);
6256 xmlFAGenerateCountedTransition(am, from, to, counter);
6257 if (to == NULL)
6258 return(am->state);
6259 return(to);
6260}
Daniel Veillard4255d502002-04-16 15:50:10 +00006261
6262/**
6263 * xmlAutomataCompile:
6264 * @am: an automata
6265 *
6266 * Compile the automata into a Reg Exp ready for being executed.
6267 * The automata should be free after this point.
6268 *
6269 * Returns the compiled regexp or NULL in case of error
6270 */
6271xmlRegexpPtr
6272xmlAutomataCompile(xmlAutomataPtr am) {
6273 xmlRegexpPtr ret;
6274
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00006275 if ((am == NULL) || (am->error != 0)) return(NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +00006276 xmlFAEliminateEpsilonTransitions(am);
Daniel Veillard23e73572002-09-19 19:56:43 +00006277 /* xmlFAComputesDeterminism(am); */
Daniel Veillard4255d502002-04-16 15:50:10 +00006278 ret = xmlRegEpxFromParse(am);
6279
6280 return(ret);
6281}
Daniel Veillarde19fc232002-04-22 16:01:24 +00006282
6283/**
6284 * xmlAutomataIsDeterminist:
6285 * @am: an automata
6286 *
6287 * Checks if an automata is determinist.
6288 *
6289 * Returns 1 if true, 0 if not, and -1 in case of error
6290 */
6291int
6292xmlAutomataIsDeterminist(xmlAutomataPtr am) {
6293 int ret;
6294
6295 if (am == NULL)
6296 return(-1);
6297
6298 ret = xmlFAComputesDeterminism(am);
6299 return(ret);
6300}
Daniel Veillard4255d502002-04-16 15:50:10 +00006301#endif /* LIBXML_AUTOMATA_ENABLED */
Daniel Veillard81a8ec62005-08-22 00:20:58 +00006302
6303#ifdef LIBXML_EXPR_ENABLED
6304/************************************************************************
6305 * *
6306 * Formal Expression handling code *
6307 * *
6308 ************************************************************************/
Daniel Veillard81a8ec62005-08-22 00:20:58 +00006309/************************************************************************
6310 * *
6311 * Expression handling context *
6312 * *
6313 ************************************************************************/
6314
6315struct _xmlExpCtxt {
6316 xmlDictPtr dict;
6317 xmlExpNodePtr *table;
6318 int size;
6319 int nbElems;
6320 int nb_nodes;
6321 const char *expr;
6322 const char *cur;
6323 int nb_cons;
Daniel Veillard81a8ec62005-08-22 00:20:58 +00006324 int tabSize;
6325};
6326
6327/**
6328 * xmlExpNewCtxt:
6329 * @maxNodes: the maximum number of nodes
6330 * @dict: optional dictionnary to use internally
6331 *
6332 * Creates a new context for manipulating expressions
6333 *
6334 * Returns the context or NULL in case of error
6335 */
6336xmlExpCtxtPtr
6337xmlExpNewCtxt(int maxNodes, xmlDictPtr dict) {
6338 xmlExpCtxtPtr ret;
6339 int size = 256;
6340
6341 if (maxNodes <= 4096)
6342 maxNodes = 4096;
6343
6344 ret = (xmlExpCtxtPtr) xmlMalloc(sizeof(xmlExpCtxt));
6345 if (ret == NULL)
6346 return(NULL);
6347 memset(ret, 0, sizeof(xmlExpCtxt));
6348 ret->size = size;
6349 ret->nbElems = 0;
6350 ret->table = xmlMalloc(size * sizeof(xmlExpNodePtr));
6351 if (ret->table == NULL) {
6352 xmlFree(ret);
6353 return(NULL);
6354 }
6355 memset(ret->table, 0, size * sizeof(xmlExpNodePtr));
6356 if (dict == NULL) {
6357 ret->dict = xmlDictCreate();
6358 if (ret->dict == NULL) {
6359 xmlFree(ret->table);
6360 xmlFree(ret);
6361 return(NULL);
6362 }
6363 } else {
6364 ret->dict = dict;
6365 xmlDictReference(ret->dict);
6366 }
6367 return(ret);
6368}
6369
6370/**
6371 * xmlExpFreeCtxt:
6372 * @ctxt: an expression context
6373 *
6374 * Free an expression context
6375 */
6376void
6377xmlExpFreeCtxt(xmlExpCtxtPtr ctxt) {
6378 if (ctxt == NULL)
6379 return;
6380 xmlDictFree(ctxt->dict);
6381 if (ctxt->table != NULL)
6382 xmlFree(ctxt->table);
6383 xmlFree(ctxt);
6384}
6385
6386/************************************************************************
6387 * *
6388 * Structure associated to an expression node *
6389 * *
6390 ************************************************************************/
Daniel Veillard465a0002005-08-22 12:07:04 +00006391#define MAX_NODES 10000
6392
6393/* #define DEBUG_DERIV */
6394
6395/*
6396 * TODO:
6397 * - Wildcards
6398 * - public API for creation
6399 *
6400 * Started
6401 * - regression testing
6402 *
6403 * Done
6404 * - split into module and test tool
6405 * - memleaks
6406 */
Daniel Veillard81a8ec62005-08-22 00:20:58 +00006407
6408typedef enum {
6409 XML_EXP_NILABLE = (1 << 0)
6410} xmlExpNodeInfo;
6411
6412#define IS_NILLABLE(node) ((node)->info & XML_EXP_NILABLE)
6413
6414struct _xmlExpNode {
6415 unsigned char type;/* xmlExpNodeType */
6416 unsigned char info;/* OR of xmlExpNodeInfo */
6417 unsigned short key; /* the hash key */
6418 unsigned int ref; /* The number of references */
6419 int c_max; /* the maximum length it can consume */
6420 xmlExpNodePtr exp_left;
6421 xmlExpNodePtr next;/* the next node in the hash table or free list */
6422 union {
6423 struct {
6424 int f_min;
6425 int f_max;
6426 } count;
6427 struct {
6428 xmlExpNodePtr f_right;
6429 } children;
6430 const xmlChar *f_str;
6431 } field;
6432};
6433
6434#define exp_min field.count.f_min
6435#define exp_max field.count.f_max
6436/* #define exp_left field.children.f_left */
6437#define exp_right field.children.f_right
6438#define exp_str field.f_str
6439
6440static xmlExpNodePtr xmlExpNewNode(xmlExpCtxtPtr ctxt, xmlExpNodeType type);
6441static xmlExpNode forbiddenExpNode = {
6442 XML_EXP_FORBID, 0, 0, 0, 0, NULL, NULL, {{ 0, 0}}
6443};
6444xmlExpNodePtr forbiddenExp = &forbiddenExpNode;
6445static xmlExpNode emptyExpNode = {
6446 XML_EXP_EMPTY, 1, 0, 0, 0, NULL, NULL, {{ 0, 0}}
6447};
6448xmlExpNodePtr emptyExp = &emptyExpNode;
6449
6450/************************************************************************
6451 * *
6452 * The custom hash table for unicity and canonicalization *
6453 * of sub-expressions pointers *
6454 * *
6455 ************************************************************************/
6456/*
6457 * xmlExpHashNameComputeKey:
6458 * Calculate the hash key for a token
6459 */
6460static unsigned short
6461xmlExpHashNameComputeKey(const xmlChar *name) {
6462 unsigned short value = 0L;
6463 char ch;
6464
6465 if (name != NULL) {
6466 value += 30 * (*name);
6467 while ((ch = *name++) != 0) {
6468 value = value ^ ((value << 5) + (value >> 3) + (unsigned long)ch);
6469 }
6470 }
6471 return (value);
6472}
6473
6474/*
6475 * xmlExpHashComputeKey:
6476 * Calculate the hash key for a compound expression
6477 */
6478static unsigned short
6479xmlExpHashComputeKey(xmlExpNodeType type, xmlExpNodePtr left,
6480 xmlExpNodePtr right) {
6481 unsigned long value;
6482 unsigned short ret;
6483
6484 switch (type) {
6485 case XML_EXP_SEQ:
6486 value = left->key;
6487 value += right->key;
6488 value *= 3;
6489 ret = (unsigned short) value;
6490 break;
6491 case XML_EXP_OR:
6492 value = left->key;
6493 value += right->key;
6494 value *= 7;
6495 ret = (unsigned short) value;
6496 break;
6497 case XML_EXP_COUNT:
6498 value = left->key;
6499 value += right->key;
6500 ret = (unsigned short) value;
6501 break;
6502 default:
6503 ret = 0;
6504 }
6505 return(ret);
6506}
6507
6508
6509static xmlExpNodePtr
6510xmlExpNewNode(xmlExpCtxtPtr ctxt, xmlExpNodeType type) {
6511 xmlExpNodePtr ret;
6512
6513 if (ctxt->nb_nodes >= MAX_NODES)
6514 return(NULL);
6515 ret = (xmlExpNodePtr) xmlMalloc(sizeof(xmlExpNode));
6516 if (ret == NULL)
6517 return(NULL);
6518 memset(ret, 0, sizeof(xmlExpNode));
6519 ret->type = type;
6520 ret->next = NULL;
6521 ctxt->nb_nodes++;
6522 ctxt->nb_cons++;
6523 return(ret);
6524}
6525
6526/**
6527 * xmlExpHashGetEntry:
6528 * @table: the hash table
6529 *
6530 * Get the unique entry from the hash table. The entry is created if
6531 * needed. @left and @right are consumed, i.e. their ref count will
6532 * be decremented by the operation.
6533 *
6534 * Returns the pointer or NULL in case of error
6535 */
6536static xmlExpNodePtr
6537xmlExpHashGetEntry(xmlExpCtxtPtr ctxt, xmlExpNodeType type,
6538 xmlExpNodePtr left, xmlExpNodePtr right,
6539 const xmlChar *name, int min, int max) {
6540 unsigned short kbase, key;
6541 xmlExpNodePtr entry;
6542 xmlExpNodePtr insert;
6543
6544 if (ctxt == NULL)
6545 return(NULL);
6546
6547 /*
6548 * Check for duplicate and insertion location.
6549 */
6550 if (type == XML_EXP_ATOM) {
6551 kbase = xmlExpHashNameComputeKey(name);
6552 } else if (type == XML_EXP_COUNT) {
6553 /* COUNT reduction rule 1 */
6554 /* a{1} -> a */
6555 if (min == max) {
6556 if (min == 1) {
6557 return(left);
6558 }
6559 if (min == 0) {
6560 xmlExpFree(ctxt, left);
6561 return(emptyExp);
6562 }
6563 }
6564 if (min < 0) {
6565 xmlExpFree(ctxt, left);
6566 return(forbiddenExp);
6567 }
6568 if (max == -1)
6569 kbase = min + 79;
6570 else
6571 kbase = max - min;
6572 kbase += left->key;
6573 } else if (type == XML_EXP_OR) {
6574 /* Forbid reduction rules */
6575 if (left->type == XML_EXP_FORBID) {
6576 xmlExpFree(ctxt, left);
6577 return(right);
6578 }
6579 if (right->type == XML_EXP_FORBID) {
6580 xmlExpFree(ctxt, right);
6581 return(left);
6582 }
6583
6584 /* OR reduction rule 1 */
6585 /* a | a reduced to a */
6586 if (left == right) {
6587 left->ref--;
6588 return(left);
6589 }
6590 /* OR canonicalization rule 1 */
6591 /* linearize (a | b) | c into a | (b | c) */
6592 if ((left->type == XML_EXP_OR) && (right->type != XML_EXP_OR)) {
6593 xmlExpNodePtr tmp = left;
6594 left = right;
6595 right = tmp;
6596 }
6597 /* OR reduction rule 2 */
6598 /* a | (a | b) and b | (a | b) are reduced to a | b */
6599 if (right->type == XML_EXP_OR) {
6600 if ((left == right->exp_left) ||
6601 (left == right->exp_right)) {
6602 xmlExpFree(ctxt, left);
6603 return(right);
6604 }
6605 }
6606 /* OR canonicalization rule 2 */
6607 /* linearize (a | b) | c into a | (b | c) */
6608 if (left->type == XML_EXP_OR) {
6609 xmlExpNodePtr tmp;
6610
6611 /* OR canonicalization rule 2 */
6612 if ((left->exp_right->type != XML_EXP_OR) &&
6613 (left->exp_right->key < left->exp_left->key)) {
6614 tmp = left->exp_right;
6615 left->exp_right = left->exp_left;
6616 left->exp_left = tmp;
6617 }
6618 left->exp_right->ref++;
6619 tmp = xmlExpHashGetEntry(ctxt, XML_EXP_OR, left->exp_right, right,
6620 NULL, 0, 0);
6621 left->exp_left->ref++;
6622 tmp = xmlExpHashGetEntry(ctxt, XML_EXP_OR, left->exp_left, tmp,
6623 NULL, 0, 0);
6624
6625 xmlExpFree(ctxt, left);
6626 return(tmp);
6627 }
6628 if (right->type == XML_EXP_OR) {
6629 /* Ordering in the tree */
6630 /* C | (A | B) -> A | (B | C) */
6631 if (left->key > right->exp_right->key) {
6632 xmlExpNodePtr tmp;
6633 right->exp_right->ref++;
6634 tmp = xmlExpHashGetEntry(ctxt, XML_EXP_OR, right->exp_right,
6635 left, NULL, 0, 0);
6636 right->exp_left->ref++;
6637 tmp = xmlExpHashGetEntry(ctxt, XML_EXP_OR, right->exp_left,
6638 tmp, NULL, 0, 0);
6639 xmlExpFree(ctxt, right);
6640 return(tmp);
6641 }
6642 /* Ordering in the tree */
6643 /* B | (A | C) -> A | (B | C) */
6644 if (left->key > right->exp_left->key) {
6645 xmlExpNodePtr tmp;
6646 right->exp_right->ref++;
6647 tmp = xmlExpHashGetEntry(ctxt, XML_EXP_OR, left,
6648 right->exp_right, NULL, 0, 0);
6649 right->exp_left->ref++;
6650 tmp = xmlExpHashGetEntry(ctxt, XML_EXP_OR, right->exp_left,
6651 tmp, NULL, 0, 0);
6652 xmlExpFree(ctxt, right);
6653 return(tmp);
6654 }
6655 }
6656 /* we know both types are != XML_EXP_OR here */
6657 else if (left->key > right->key) {
6658 xmlExpNodePtr tmp = left;
6659 left = right;
6660 right = tmp;
6661 }
6662 kbase = xmlExpHashComputeKey(type, left, right);
6663 } else if (type == XML_EXP_SEQ) {
6664 /* Forbid reduction rules */
6665 if (left->type == XML_EXP_FORBID) {
6666 xmlExpFree(ctxt, right);
6667 return(left);
6668 }
6669 if (right->type == XML_EXP_FORBID) {
6670 xmlExpFree(ctxt, left);
6671 return(right);
6672 }
6673 /* Empty reduction rules */
6674 if (right->type == XML_EXP_EMPTY) {
6675 return(left);
6676 }
6677 if (left->type == XML_EXP_EMPTY) {
6678 return(right);
6679 }
6680 kbase = xmlExpHashComputeKey(type, left, right);
6681 } else
6682 return(NULL);
6683
6684 key = kbase % ctxt->size;
6685 if (ctxt->table[key] != NULL) {
6686 for (insert = ctxt->table[key]; insert != NULL;
6687 insert = insert->next) {
6688 if ((insert->key == kbase) &&
6689 (insert->type == type)) {
6690 if (type == XML_EXP_ATOM) {
6691 if (name == insert->exp_str) {
6692 insert->ref++;
6693 return(insert);
6694 }
6695 } else if (type == XML_EXP_COUNT) {
6696 if ((insert->exp_min == min) && (insert->exp_max == max) &&
6697 (insert->exp_left == left)) {
6698 insert->ref++;
6699 left->ref--;
6700 return(insert);
6701 }
6702 } else if ((insert->exp_left == left) &&
6703 (insert->exp_right == right)) {
6704 insert->ref++;
6705 left->ref--;
6706 right->ref--;
6707 return(insert);
6708 }
6709 }
6710 }
6711 }
6712
6713 entry = xmlExpNewNode(ctxt, type);
6714 if (entry == NULL)
6715 return(NULL);
6716 entry->key = kbase;
6717 if (type == XML_EXP_ATOM) {
6718 entry->exp_str = name;
6719 entry->c_max = 1;
6720 } else if (type == XML_EXP_COUNT) {
6721 entry->exp_min = min;
6722 entry->exp_max = max;
6723 entry->exp_left = left;
6724 if ((min == 0) || (IS_NILLABLE(left)))
6725 entry->info |= XML_EXP_NILABLE;
6726 if (max < 0)
6727 entry->c_max = -1;
6728 else
6729 entry->c_max = max * entry->exp_left->c_max;
6730 } else {
6731 entry->exp_left = left;
6732 entry->exp_right = right;
6733 if (type == XML_EXP_OR) {
6734 if ((IS_NILLABLE(left)) || (IS_NILLABLE(right)))
6735 entry->info |= XML_EXP_NILABLE;
6736 if ((entry->exp_left->c_max == -1) ||
6737 (entry->exp_right->c_max == -1))
6738 entry->c_max = -1;
6739 else if (entry->exp_left->c_max > entry->exp_right->c_max)
6740 entry->c_max = entry->exp_left->c_max;
6741 else
6742 entry->c_max = entry->exp_right->c_max;
6743 } else {
6744 if ((IS_NILLABLE(left)) && (IS_NILLABLE(right)))
6745 entry->info |= XML_EXP_NILABLE;
6746 if ((entry->exp_left->c_max == -1) ||
6747 (entry->exp_right->c_max == -1))
6748 entry->c_max = -1;
6749 else
6750 entry->c_max = entry->exp_left->c_max + entry->exp_right->c_max;
6751 }
6752 }
6753 entry->ref = 1;
6754 if (ctxt->table[key] != NULL)
6755 entry->next = ctxt->table[key];
6756
6757 ctxt->table[key] = entry;
6758 ctxt->nbElems++;
6759
6760 return(entry);
6761}
6762
6763/**
6764 * xmlExpFree:
6765 * @ctxt: the expression context
6766 * @exp: the expression
6767 *
6768 * Dereference the expression
6769 */
6770void
6771xmlExpFree(xmlExpCtxtPtr ctxt, xmlExpNodePtr exp) {
6772 if ((exp == NULL) || (exp == forbiddenExp) || (exp == emptyExp))
6773 return;
6774 exp->ref--;
Daniel Veillard81a8ec62005-08-22 00:20:58 +00006775 if (exp->ref == 0) {
6776 unsigned short key;
6777
6778 /* Unlink it first from the hash table */
6779 key = exp->key % ctxt->size;
6780 if (ctxt->table[key] == exp) {
6781 ctxt->table[key] = exp->next;
6782 } else {
6783 xmlExpNodePtr tmp;
6784
6785 tmp = ctxt->table[key];
6786 while (tmp != NULL) {
6787 if (tmp->next == exp) {
6788 tmp->next = exp->next;
6789 break;
6790 }
6791 tmp = tmp->next;
6792 }
6793 }
6794
6795 if ((exp->type == XML_EXP_SEQ) || (exp->type == XML_EXP_OR)) {
6796 xmlExpFree(ctxt, exp->exp_left);
6797 xmlExpFree(ctxt, exp->exp_right);
6798 } else if (exp->type == XML_EXP_COUNT) {
6799 xmlExpFree(ctxt, exp->exp_left);
6800 }
6801 xmlFree(exp);
Daniel Veillard81a8ec62005-08-22 00:20:58 +00006802 ctxt->nb_nodes--;
6803 }
6804}
6805
6806/**
6807 * xmlExpRef:
6808 * @exp: the expression
6809 *
6810 * Increase the reference count of the expression
6811 */
6812void
6813xmlExpRef(xmlExpNodePtr exp) {
6814 if (exp != NULL)
6815 exp->ref++;
6816}
6817
Daniel Veillardccb4d412005-08-23 13:41:17 +00006818/**
6819 * xmlExpNewAtom:
6820 * @ctxt: the expression context
6821 * @name: the atom name
6822 * @len: the atom name lenght in byte (or -1);
6823 *
6824 * Get the atom associated to this name from that context
6825 *
6826 * Returns the node or NULL in case of error
6827 */
6828xmlExpNodePtr
6829xmlExpNewAtom(xmlExpCtxtPtr ctxt, const xmlChar *name, int len) {
6830 if ((ctxt == NULL) || (name == NULL))
6831 return(NULL);
6832 name = xmlDictLookup(ctxt->dict, name, len);
6833 if (name == NULL)
6834 return(NULL);
6835 return(xmlExpHashGetEntry(ctxt, XML_EXP_ATOM, NULL, NULL, name, 0, 0));
6836}
6837
6838/**
6839 * xmlExpNewOr:
6840 * @ctxt: the expression context
6841 * @left: left expression
6842 * @right: right expression
6843 *
6844 * Get the atom associated to the choice @left | @right
6845 * Note that @left and @right are consumed in the operation, to keep
6846 * an handle on them use xmlExpRef() and use xmlExpFree() to release them,
6847 * this is true even in case of failure (unless ctxt == NULL).
6848 *
6849 * Returns the node or NULL in case of error
6850 */
6851xmlExpNodePtr
6852xmlExpNewOr(xmlExpCtxtPtr ctxt, xmlExpNodePtr left, xmlExpNodePtr right) {
Daniel Veillard11ce4002006-03-10 00:36:23 +00006853 if (ctxt == NULL)
6854 return(NULL);
6855 if ((left == NULL) || (right == NULL)) {
Daniel Veillardccb4d412005-08-23 13:41:17 +00006856 xmlExpFree(ctxt, left);
6857 xmlExpFree(ctxt, right);
6858 return(NULL);
6859 }
6860 return(xmlExpHashGetEntry(ctxt, XML_EXP_OR, left, right, NULL, 0, 0));
6861}
6862
6863/**
6864 * xmlExpNewSeq:
6865 * @ctxt: the expression context
6866 * @left: left expression
6867 * @right: right expression
6868 *
6869 * Get the atom associated to the sequence @left , @right
6870 * Note that @left and @right are consumed in the operation, to keep
6871 * an handle on them use xmlExpRef() and use xmlExpFree() to release them,
6872 * this is true even in case of failure (unless ctxt == NULL).
6873 *
6874 * Returns the node or NULL in case of error
6875 */
6876xmlExpNodePtr
6877xmlExpNewSeq(xmlExpCtxtPtr ctxt, xmlExpNodePtr left, xmlExpNodePtr right) {
Daniel Veillard11ce4002006-03-10 00:36:23 +00006878 if (ctxt == NULL)
6879 return(NULL);
6880 if ((left == NULL) || (right == NULL)) {
Daniel Veillardccb4d412005-08-23 13:41:17 +00006881 xmlExpFree(ctxt, left);
6882 xmlExpFree(ctxt, right);
6883 return(NULL);
6884 }
6885 return(xmlExpHashGetEntry(ctxt, XML_EXP_SEQ, left, right, NULL, 0, 0));
6886}
6887
6888/**
6889 * xmlExpNewRange:
6890 * @ctxt: the expression context
6891 * @subset: the expression to be repeated
6892 * @min: the lower bound for the repetition
6893 * @max: the upper bound for the repetition, -1 means infinite
6894 *
6895 * Get the atom associated to the range (@subset){@min, @max}
6896 * Note that @subset is consumed in the operation, to keep
6897 * an handle on it use xmlExpRef() and use xmlExpFree() to release it,
6898 * this is true even in case of failure (unless ctxt == NULL).
6899 *
6900 * Returns the node or NULL in case of error
6901 */
6902xmlExpNodePtr
6903xmlExpNewRange(xmlExpCtxtPtr ctxt, xmlExpNodePtr subset, int min, int max) {
Daniel Veillard11ce4002006-03-10 00:36:23 +00006904 if (ctxt == NULL)
6905 return(NULL);
6906 if ((subset == NULL) || (min < 0) || (max < -1) ||
Daniel Veillardccb4d412005-08-23 13:41:17 +00006907 ((max >= 0) && (min > max))) {
6908 xmlExpFree(ctxt, subset);
6909 return(NULL);
6910 }
6911 return(xmlExpHashGetEntry(ctxt, XML_EXP_COUNT, subset,
6912 NULL, NULL, min, max));
6913}
6914
Daniel Veillard81a8ec62005-08-22 00:20:58 +00006915/************************************************************************
6916 * *
6917 * Public API for operations on expressions *
6918 * *
6919 ************************************************************************/
6920
6921static int
6922xmlExpGetLanguageInt(xmlExpCtxtPtr ctxt, xmlExpNodePtr exp,
6923 const xmlChar**list, int len, int nb) {
6924 int tmp, tmp2;
6925tail:
6926 switch (exp->type) {
6927 case XML_EXP_EMPTY:
6928 return(0);
6929 case XML_EXP_ATOM:
6930 for (tmp = 0;tmp < nb;tmp++)
6931 if (list[tmp] == exp->exp_str)
6932 return(0);
6933 if (nb >= len)
6934 return(-2);
6935 list[nb++] = exp->exp_str;
6936 return(1);
6937 case XML_EXP_COUNT:
6938 exp = exp->exp_left;
6939 goto tail;
6940 case XML_EXP_SEQ:
6941 case XML_EXP_OR:
6942 tmp = xmlExpGetLanguageInt(ctxt, exp->exp_left, list, len, nb);
6943 if (tmp < 0)
6944 return(tmp);
6945 tmp2 = xmlExpGetLanguageInt(ctxt, exp->exp_right, list, len,
6946 nb + tmp);
6947 if (tmp2 < 0)
6948 return(tmp2);
6949 return(tmp + tmp2);
6950 }
6951 return(-1);
6952}
6953
6954/**
6955 * xmlExpGetLanguage:
6956 * @ctxt: the expression context
6957 * @exp: the expression
Daniel Veillard7802ba52005-10-27 11:56:20 +00006958 * @langList: where to store the tokens
Daniel Veillard81a8ec62005-08-22 00:20:58 +00006959 * @len: the allocated lenght of @list
6960 *
6961 * Find all the strings used in @exp and store them in @list
6962 *
6963 * Returns the number of unique strings found, -1 in case of errors and
6964 * -2 if there is more than @len strings
6965 */
6966int
6967xmlExpGetLanguage(xmlExpCtxtPtr ctxt, xmlExpNodePtr exp,
Daniel Veillard7802ba52005-10-27 11:56:20 +00006968 const xmlChar**langList, int len) {
6969 if ((ctxt == NULL) || (exp == NULL) || (langList == NULL) || (len <= 0))
Daniel Veillard81a8ec62005-08-22 00:20:58 +00006970 return(-1);
Daniel Veillard7802ba52005-10-27 11:56:20 +00006971 return(xmlExpGetLanguageInt(ctxt, exp, langList, len, 0));
Daniel Veillard81a8ec62005-08-22 00:20:58 +00006972}
6973
6974static int
6975xmlExpGetStartInt(xmlExpCtxtPtr ctxt, xmlExpNodePtr exp,
6976 const xmlChar**list, int len, int nb) {
6977 int tmp, tmp2;
6978tail:
6979 switch (exp->type) {
6980 case XML_EXP_FORBID:
6981 return(0);
6982 case XML_EXP_EMPTY:
6983 return(0);
6984 case XML_EXP_ATOM:
6985 for (tmp = 0;tmp < nb;tmp++)
6986 if (list[tmp] == exp->exp_str)
6987 return(0);
6988 if (nb >= len)
6989 return(-2);
6990 list[nb++] = exp->exp_str;
6991 return(1);
6992 case XML_EXP_COUNT:
6993 exp = exp->exp_left;
6994 goto tail;
6995 case XML_EXP_SEQ:
6996 tmp = xmlExpGetStartInt(ctxt, exp->exp_left, list, len, nb);
6997 if (tmp < 0)
6998 return(tmp);
6999 if (IS_NILLABLE(exp->exp_left)) {
7000 tmp2 = xmlExpGetStartInt(ctxt, exp->exp_right, list, len,
7001 nb + tmp);
7002 if (tmp2 < 0)
7003 return(tmp2);
7004 tmp += tmp2;
7005 }
7006 return(tmp);
7007 case XML_EXP_OR:
7008 tmp = xmlExpGetStartInt(ctxt, exp->exp_left, list, len, nb);
7009 if (tmp < 0)
7010 return(tmp);
7011 tmp2 = xmlExpGetStartInt(ctxt, exp->exp_right, list, len,
7012 nb + tmp);
7013 if (tmp2 < 0)
7014 return(tmp2);
7015 return(tmp + tmp2);
7016 }
7017 return(-1);
7018}
7019
7020/**
7021 * xmlExpGetStart:
7022 * @ctxt: the expression context
7023 * @exp: the expression
Daniel Veillard7802ba52005-10-27 11:56:20 +00007024 * @tokList: where to store the tokens
Daniel Veillard81a8ec62005-08-22 00:20:58 +00007025 * @len: the allocated lenght of @list
7026 *
7027 * Find all the strings that appears at the start of the languages
7028 * accepted by @exp and store them in @list. E.g. for (a, b) | c
7029 * it will return the list [a, c]
7030 *
7031 * Returns the number of unique strings found, -1 in case of errors and
7032 * -2 if there is more than @len strings
7033 */
7034int
7035xmlExpGetStart(xmlExpCtxtPtr ctxt, xmlExpNodePtr exp,
Daniel Veillard7802ba52005-10-27 11:56:20 +00007036 const xmlChar**tokList, int len) {
7037 if ((ctxt == NULL) || (exp == NULL) || (tokList == NULL) || (len <= 0))
Daniel Veillard81a8ec62005-08-22 00:20:58 +00007038 return(-1);
Daniel Veillard7802ba52005-10-27 11:56:20 +00007039 return(xmlExpGetStartInt(ctxt, exp, tokList, len, 0));
Daniel Veillard81a8ec62005-08-22 00:20:58 +00007040}
7041
7042/**
7043 * xmlExpIsNillable:
7044 * @exp: the expression
7045 *
7046 * Finds if the expression is nillable, i.e. if it accepts the empty sequqnce
7047 *
7048 * Returns 1 if nillable, 0 if not and -1 in case of error
7049 */
7050int
7051xmlExpIsNillable(xmlExpNodePtr exp) {
7052 if (exp == NULL)
7053 return(-1);
7054 return(IS_NILLABLE(exp) != 0);
7055}
7056
7057static xmlExpNodePtr
7058xmlExpStringDeriveInt(xmlExpCtxtPtr ctxt, xmlExpNodePtr exp, const xmlChar *str)
7059{
7060 xmlExpNodePtr ret;
7061
7062 switch (exp->type) {
7063 case XML_EXP_EMPTY:
7064 return(forbiddenExp);
7065 case XML_EXP_FORBID:
7066 return(forbiddenExp);
7067 case XML_EXP_ATOM:
7068 if (exp->exp_str == str) {
7069#ifdef DEBUG_DERIV
7070 printf("deriv atom: equal => Empty\n");
7071#endif
7072 ret = emptyExp;
7073 } else {
7074#ifdef DEBUG_DERIV
7075 printf("deriv atom: mismatch => forbid\n");
7076#endif
7077 /* TODO wildcards here */
7078 ret = forbiddenExp;
7079 }
7080 return(ret);
7081 case XML_EXP_OR: {
7082 xmlExpNodePtr tmp;
7083
7084#ifdef DEBUG_DERIV
7085 printf("deriv or: => or(derivs)\n");
7086#endif
7087 tmp = xmlExpStringDeriveInt(ctxt, exp->exp_left, str);
7088 if (tmp == NULL) {
7089 return(NULL);
7090 }
7091 ret = xmlExpStringDeriveInt(ctxt, exp->exp_right, str);
7092 if (ret == NULL) {
7093 xmlExpFree(ctxt, tmp);
7094 return(NULL);
7095 }
7096 ret = xmlExpHashGetEntry(ctxt, XML_EXP_OR, tmp, ret,
7097 NULL, 0, 0);
7098 return(ret);
7099 }
7100 case XML_EXP_SEQ:
7101#ifdef DEBUG_DERIV
7102 printf("deriv seq: starting with left\n");
7103#endif
7104 ret = xmlExpStringDeriveInt(ctxt, exp->exp_left, str);
7105 if (ret == NULL) {
7106 return(NULL);
7107 } else if (ret == forbiddenExp) {
7108 if (IS_NILLABLE(exp->exp_left)) {
7109#ifdef DEBUG_DERIV
7110 printf("deriv seq: left failed but nillable\n");
7111#endif
7112 ret = xmlExpStringDeriveInt(ctxt, exp->exp_right, str);
7113 }
7114 } else {
7115#ifdef DEBUG_DERIV
7116 printf("deriv seq: left match => sequence\n");
7117#endif
7118 exp->exp_right->ref++;
7119 ret = xmlExpHashGetEntry(ctxt, XML_EXP_SEQ, ret, exp->exp_right,
7120 NULL, 0, 0);
7121 }
7122 return(ret);
7123 case XML_EXP_COUNT: {
7124 int min, max;
7125 xmlExpNodePtr tmp;
7126
7127 if (exp->exp_max == 0)
7128 return(forbiddenExp);
7129 ret = xmlExpStringDeriveInt(ctxt, exp->exp_left, str);
7130 if (ret == NULL)
7131 return(NULL);
7132 if (ret == forbiddenExp) {
7133#ifdef DEBUG_DERIV
7134 printf("deriv count: pattern mismatch => forbid\n");
7135#endif
7136 return(ret);
7137 }
7138 if (exp->exp_max == 1)
7139 return(ret);
7140 if (exp->exp_max < 0) /* unbounded */
7141 max = -1;
7142 else
7143 max = exp->exp_max - 1;
7144 if (exp->exp_min > 0)
7145 min = exp->exp_min - 1;
7146 else
7147 min = 0;
7148 exp->exp_left->ref++;
7149 tmp = xmlExpHashGetEntry(ctxt, XML_EXP_COUNT, exp->exp_left, NULL,
7150 NULL, min, max);
7151 if (ret == emptyExp) {
7152#ifdef DEBUG_DERIV
7153 printf("deriv count: match to empty => new count\n");
7154#endif
7155 return(tmp);
7156 }
7157#ifdef DEBUG_DERIV
7158 printf("deriv count: match => sequence with new count\n");
7159#endif
7160 return(xmlExpHashGetEntry(ctxt, XML_EXP_SEQ, ret, tmp,
7161 NULL, 0, 0));
7162 }
7163 }
7164 return(NULL);
7165}
7166
7167/**
7168 * xmlExpStringDerive:
7169 * @ctxt: the expression context
7170 * @exp: the expression
7171 * @str: the string
7172 * @len: the string len in bytes if available
7173 *
7174 * Do one step of Brzozowski derivation of the expression @exp with
7175 * respect to the input string
7176 *
7177 * Returns the resulting expression or NULL in case of internal error
7178 */
7179xmlExpNodePtr
7180xmlExpStringDerive(xmlExpCtxtPtr ctxt, xmlExpNodePtr exp,
7181 const xmlChar *str, int len) {
7182 const xmlChar *input;
7183
7184 if ((exp == NULL) || (ctxt == NULL) || (str == NULL)) {
7185 return(NULL);
7186 }
7187 /*
7188 * check the string is in the dictionnary, if yes use an interned
7189 * copy, otherwise we know it's not an acceptable input
7190 */
7191 input = xmlDictExists(ctxt->dict, str, len);
7192 if (input == NULL) {
7193 return(forbiddenExp);
7194 }
7195 return(xmlExpStringDeriveInt(ctxt, exp, input));
7196}
7197
7198static int
7199xmlExpCheckCard(xmlExpNodePtr exp, xmlExpNodePtr sub) {
7200 int ret = 1;
7201
7202 if (sub->c_max == -1) {
7203 if (exp->c_max != -1)
7204 ret = 0;
7205 } else if ((exp->c_max >= 0) && (exp->c_max < sub->c_max)) {
7206 ret = 0;
7207 }
7208#if 0
7209 if ((IS_NILLABLE(sub)) && (!IS_NILLABLE(exp)))
7210 ret = 0;
7211#endif
7212 return(ret);
7213}
7214
7215static xmlExpNodePtr xmlExpExpDeriveInt(xmlExpCtxtPtr ctxt, xmlExpNodePtr exp,
7216 xmlExpNodePtr sub);
7217/**
7218 * xmlExpDivide:
7219 * @ctxt: the expressions context
7220 * @exp: the englobing expression
7221 * @sub: the subexpression
7222 * @mult: the multiple expression
7223 * @remain: the remain from the derivation of the multiple
7224 *
7225 * Check if exp is a multiple of sub, i.e. if there is a finite number n
7226 * so that sub{n} subsume exp
7227 *
7228 * Returns the multiple value if successful, 0 if it is not a multiple
7229 * and -1 in case of internel error.
7230 */
7231
7232static int
7233xmlExpDivide(xmlExpCtxtPtr ctxt, xmlExpNodePtr exp, xmlExpNodePtr sub,
7234 xmlExpNodePtr *mult, xmlExpNodePtr *remain) {
7235 int i;
7236 xmlExpNodePtr tmp, tmp2;
7237
7238 if (mult != NULL) *mult = NULL;
7239 if (remain != NULL) *remain = NULL;
7240 if (exp->c_max == -1) return(0);
7241 if (IS_NILLABLE(exp) && (!IS_NILLABLE(sub))) return(0);
7242
7243 for (i = 1;i <= exp->c_max;i++) {
7244 sub->ref++;
7245 tmp = xmlExpHashGetEntry(ctxt, XML_EXP_COUNT,
7246 sub, NULL, NULL, i, i);
7247 if (tmp == NULL) {
7248 return(-1);
7249 }
7250 if (!xmlExpCheckCard(tmp, exp)) {
7251 xmlExpFree(ctxt, tmp);
7252 continue;
7253 }
7254 tmp2 = xmlExpExpDeriveInt(ctxt, tmp, exp);
7255 if (tmp2 == NULL) {
7256 xmlExpFree(ctxt, tmp);
7257 return(-1);
7258 }
7259 if ((tmp2 != forbiddenExp) && (IS_NILLABLE(tmp2))) {
7260 if (remain != NULL)
7261 *remain = tmp2;
7262 else
7263 xmlExpFree(ctxt, tmp2);
7264 if (mult != NULL)
7265 *mult = tmp;
7266 else
7267 xmlExpFree(ctxt, tmp);
7268#ifdef DEBUG_DERIV
7269 printf("Divide succeeded %d\n", i);
7270#endif
7271 return(i);
7272 }
7273 xmlExpFree(ctxt, tmp);
7274 xmlExpFree(ctxt, tmp2);
7275 }
7276#ifdef DEBUG_DERIV
7277 printf("Divide failed\n");
7278#endif
7279 return(0);
7280}
7281
7282/**
7283 * xmlExpExpDeriveInt:
7284 * @ctxt: the expressions context
7285 * @exp: the englobing expression
7286 * @sub: the subexpression
7287 *
7288 * Try to do a step of Brzozowski derivation but at a higher level
7289 * the input being a subexpression.
7290 *
7291 * Returns the resulting expression or NULL in case of internal error
7292 */
7293static xmlExpNodePtr
7294xmlExpExpDeriveInt(xmlExpCtxtPtr ctxt, xmlExpNodePtr exp, xmlExpNodePtr sub) {
7295 xmlExpNodePtr ret, tmp, tmp2, tmp3;
7296 const xmlChar **tab;
7297 int len, i;
7298
7299 /*
7300 * In case of equality and if the expression can only consume a finite
7301 * amount, then the derivation is empty
7302 */
7303 if ((exp == sub) && (exp->c_max >= 0)) {
7304#ifdef DEBUG_DERIV
7305 printf("Equal(exp, sub) and finite -> Empty\n");
7306#endif
7307 return(emptyExp);
7308 }
7309 /*
7310 * decompose sub sequence first
7311 */
7312 if (sub->type == XML_EXP_EMPTY) {
7313#ifdef DEBUG_DERIV
7314 printf("Empty(sub) -> Empty\n");
7315#endif
7316 exp->ref++;
7317 return(exp);
7318 }
7319 if (sub->type == XML_EXP_SEQ) {
7320#ifdef DEBUG_DERIV
7321 printf("Seq(sub) -> decompose\n");
7322#endif
7323 tmp = xmlExpExpDeriveInt(ctxt, exp, sub->exp_left);
7324 if (tmp == NULL)
7325 return(NULL);
7326 if (tmp == forbiddenExp)
7327 return(tmp);
7328 ret = xmlExpExpDeriveInt(ctxt, tmp, sub->exp_right);
7329 xmlExpFree(ctxt, tmp);
7330 return(ret);
7331 }
7332 if (sub->type == XML_EXP_OR) {
7333#ifdef DEBUG_DERIV
7334 printf("Or(sub) -> decompose\n");
7335#endif
7336 tmp = xmlExpExpDeriveInt(ctxt, exp, sub->exp_left);
7337 if (tmp == forbiddenExp)
7338 return(tmp);
7339 if (tmp == NULL)
7340 return(NULL);
7341 ret = xmlExpExpDeriveInt(ctxt, exp, sub->exp_right);
7342 if ((ret == NULL) || (ret == forbiddenExp)) {
7343 xmlExpFree(ctxt, tmp);
7344 return(ret);
7345 }
7346 return(xmlExpHashGetEntry(ctxt, XML_EXP_OR, tmp, ret, NULL, 0, 0));
7347 }
7348 if (!xmlExpCheckCard(exp, sub)) {
7349#ifdef DEBUG_DERIV
7350 printf("CheckCard(exp, sub) failed -> Forbid\n");
7351#endif
7352 return(forbiddenExp);
7353 }
7354 switch (exp->type) {
7355 case XML_EXP_EMPTY:
7356 if (sub == emptyExp)
7357 return(emptyExp);
7358#ifdef DEBUG_DERIV
7359 printf("Empty(exp) -> Forbid\n");
7360#endif
7361 return(forbiddenExp);
7362 case XML_EXP_FORBID:
7363#ifdef DEBUG_DERIV
7364 printf("Forbid(exp) -> Forbid\n");
7365#endif
7366 return(forbiddenExp);
7367 case XML_EXP_ATOM:
7368 if (sub->type == XML_EXP_ATOM) {
7369 /* TODO: handle wildcards */
7370 if (exp->exp_str == sub->exp_str) {
7371#ifdef DEBUG_DERIV
7372 printf("Atom match -> Empty\n");
7373#endif
7374 return(emptyExp);
7375 }
7376#ifdef DEBUG_DERIV
7377 printf("Atom mismatch -> Forbid\n");
7378#endif
7379 return(forbiddenExp);
7380 }
7381 if ((sub->type == XML_EXP_COUNT) &&
7382 (sub->exp_max == 1) &&
7383 (sub->exp_left->type == XML_EXP_ATOM)) {
7384 /* TODO: handle wildcards */
7385 if (exp->exp_str == sub->exp_left->exp_str) {
7386#ifdef DEBUG_DERIV
7387 printf("Atom match -> Empty\n");
7388#endif
7389 return(emptyExp);
7390 }
7391#ifdef DEBUG_DERIV
7392 printf("Atom mismatch -> Forbid\n");
7393#endif
7394 return(forbiddenExp);
7395 }
7396#ifdef DEBUG_DERIV
7397 printf("Compex exp vs Atom -> Forbid\n");
7398#endif
7399 return(forbiddenExp);
7400 case XML_EXP_SEQ:
7401 /* try to get the sequence consumed only if possible */
7402 if (xmlExpCheckCard(exp->exp_left, sub)) {
7403 /* See if the sequence can be consumed directly */
7404#ifdef DEBUG_DERIV
7405 printf("Seq trying left only\n");
7406#endif
7407 ret = xmlExpExpDeriveInt(ctxt, exp->exp_left, sub);
7408 if ((ret != forbiddenExp) && (ret != NULL)) {
7409#ifdef DEBUG_DERIV
7410 printf("Seq trying left only worked\n");
7411#endif
7412 /*
7413 * TODO: assumption here that we are determinist
7414 * i.e. we won't get to a nillable exp left
7415 * subset which could be matched by the right
7416 * part too.
7417 * e.g.: (a | b)+,(a | c) and 'a+,a'
7418 */
7419 exp->exp_right->ref++;
7420 return(xmlExpHashGetEntry(ctxt, XML_EXP_SEQ, ret,
7421 exp->exp_right, NULL, 0, 0));
7422 }
7423#ifdef DEBUG_DERIV
7424 } else {
7425 printf("Seq: left too short\n");
7426#endif
7427 }
7428 /* Try instead to decompose */
7429 if (sub->type == XML_EXP_COUNT) {
7430 int min, max;
7431
7432#ifdef DEBUG_DERIV
7433 printf("Seq: sub is a count\n");
7434#endif
7435 ret = xmlExpExpDeriveInt(ctxt, exp->exp_left, sub->exp_left);
7436 if (ret == NULL)
7437 return(NULL);
7438 if (ret != forbiddenExp) {
7439#ifdef DEBUG_DERIV
7440 printf("Seq , Count match on left\n");
7441#endif
7442 if (sub->exp_max < 0)
7443 max = -1;
7444 else
7445 max = sub->exp_max -1;
7446 if (sub->exp_min > 0)
7447 min = sub->exp_min -1;
7448 else
7449 min = 0;
7450 exp->exp_right->ref++;
7451 tmp = xmlExpHashGetEntry(ctxt, XML_EXP_SEQ, ret,
7452 exp->exp_right, NULL, 0, 0);
7453 if (tmp == NULL)
7454 return(NULL);
7455
7456 sub->exp_left->ref++;
7457 tmp2 = xmlExpHashGetEntry(ctxt, XML_EXP_COUNT,
7458 sub->exp_left, NULL, NULL, min, max);
7459 if (tmp2 == NULL) {
7460 xmlExpFree(ctxt, tmp);
7461 return(NULL);
7462 }
7463 ret = xmlExpExpDeriveInt(ctxt, tmp, tmp2);
7464 xmlExpFree(ctxt, tmp);
7465 xmlExpFree(ctxt, tmp2);
7466 return(ret);
7467 }
7468 }
7469 /* we made no progress on structured operations */
7470 break;
7471 case XML_EXP_OR:
7472#ifdef DEBUG_DERIV
7473 printf("Or , trying both side\n");
7474#endif
7475 ret = xmlExpExpDeriveInt(ctxt, exp->exp_left, sub);
7476 if (ret == NULL)
7477 return(NULL);
7478 tmp = xmlExpExpDeriveInt(ctxt, exp->exp_right, sub);
7479 if (tmp == NULL) {
7480 xmlExpFree(ctxt, ret);
7481 return(NULL);
7482 }
7483 return(xmlExpHashGetEntry(ctxt, XML_EXP_OR, ret, tmp, NULL, 0, 0));
7484 case XML_EXP_COUNT: {
7485 int min, max;
7486
7487 if (sub->type == XML_EXP_COUNT) {
7488 /*
7489 * Try to see if the loop is completely subsumed
7490 */
7491 tmp = xmlExpExpDeriveInt(ctxt, exp->exp_left, sub->exp_left);
7492 if (tmp == NULL)
7493 return(NULL);
7494 if (tmp == forbiddenExp) {
7495 int mult;
7496
7497#ifdef DEBUG_DERIV
7498 printf("Count, Count inner don't subsume\n");
7499#endif
7500 mult = xmlExpDivide(ctxt, sub->exp_left, exp->exp_left,
7501 NULL, &tmp);
7502 if (mult <= 0) {
7503#ifdef DEBUG_DERIV
7504 printf("Count, Count not multiple => forbidden\n");
7505#endif
7506 return(forbiddenExp);
7507 }
7508 if (sub->exp_max == -1) {
7509 max = -1;
7510 if (exp->exp_max == -1) {
7511 if (exp->exp_min <= sub->exp_min * mult)
7512 min = 0;
7513 else
7514 min = exp->exp_min - sub->exp_min * mult;
7515 } else {
7516#ifdef DEBUG_DERIV
7517 printf("Count, Count finite can't subsume infinite\n");
7518#endif
7519 xmlExpFree(ctxt, tmp);
7520 return(forbiddenExp);
7521 }
7522 } else {
7523 if (exp->exp_max == -1) {
7524#ifdef DEBUG_DERIV
7525 printf("Infinite loop consume mult finite loop\n");
7526#endif
7527 if (exp->exp_min > sub->exp_min * mult) {
7528 max = -1;
7529 min = exp->exp_min - sub->exp_min * mult;
7530 } else {
7531 max = -1;
7532 min = 0;
7533 }
7534 } else {
7535 if (exp->exp_max < sub->exp_max * mult) {
7536#ifdef DEBUG_DERIV
7537 printf("loops max mult mismatch => forbidden\n");
7538#endif
7539 xmlExpFree(ctxt, tmp);
7540 return(forbiddenExp);
7541 }
7542 if (sub->exp_max * mult > exp->exp_min)
7543 min = 0;
7544 else
7545 min = exp->exp_min - sub->exp_max * mult;
7546 max = exp->exp_max - sub->exp_max * mult;
7547 }
7548 }
7549 } else if (!IS_NILLABLE(tmp)) {
7550 /*
7551 * TODO: loop here to try to grow if working on finite
7552 * blocks.
7553 */
7554#ifdef DEBUG_DERIV
7555 printf("Count, Count remain not nillable => forbidden\n");
7556#endif
7557 xmlExpFree(ctxt, tmp);
7558 return(forbiddenExp);
7559 } else if (sub->exp_max == -1) {
7560 if (exp->exp_max == -1) {
7561 if (exp->exp_min <= sub->exp_min) {
7562#ifdef DEBUG_DERIV
7563 printf("Infinite loops Okay => COUNT(0,Inf)\n");
7564#endif
7565 max = -1;
7566 min = 0;
7567 } else {
7568#ifdef DEBUG_DERIV
7569 printf("Infinite loops min => Count(X,Inf)\n");
7570#endif
7571 max = -1;
7572 min = exp->exp_min - sub->exp_min;
7573 }
7574 } else if (exp->exp_min > sub->exp_min) {
7575#ifdef DEBUG_DERIV
7576 printf("loops min mismatch 1 => forbidden ???\n");
7577#endif
7578 xmlExpFree(ctxt, tmp);
7579 return(forbiddenExp);
7580 } else {
7581 max = -1;
7582 min = 0;
7583 }
7584 } else {
7585 if (exp->exp_max == -1) {
7586#ifdef DEBUG_DERIV
7587 printf("Infinite loop consume finite loop\n");
7588#endif
7589 if (exp->exp_min > sub->exp_min) {
7590 max = -1;
7591 min = exp->exp_min - sub->exp_min;
7592 } else {
7593 max = -1;
7594 min = 0;
7595 }
7596 } else {
7597 if (exp->exp_max < sub->exp_max) {
7598#ifdef DEBUG_DERIV
7599 printf("loops max mismatch => forbidden\n");
7600#endif
7601 xmlExpFree(ctxt, tmp);
7602 return(forbiddenExp);
7603 }
7604 if (sub->exp_max > exp->exp_min)
7605 min = 0;
7606 else
7607 min = exp->exp_min - sub->exp_max;
7608 max = exp->exp_max - sub->exp_max;
7609 }
7610 }
7611#ifdef DEBUG_DERIV
7612 printf("loops match => SEQ(COUNT())\n");
7613#endif
7614 exp->exp_left->ref++;
7615 tmp2 = xmlExpHashGetEntry(ctxt, XML_EXP_COUNT, exp->exp_left,
7616 NULL, NULL, min, max);
7617 if (tmp2 == NULL) {
7618 return(NULL);
7619 }
7620 ret = xmlExpHashGetEntry(ctxt, XML_EXP_SEQ, tmp, tmp2,
7621 NULL, 0, 0);
7622 return(ret);
7623 }
7624 tmp = xmlExpExpDeriveInt(ctxt, exp->exp_left, sub);
7625 if (tmp == NULL)
7626 return(NULL);
7627 if (tmp == forbiddenExp) {
7628#ifdef DEBUG_DERIV
7629 printf("loop mismatch => forbidden\n");
7630#endif
7631 return(forbiddenExp);
7632 }
7633 if (exp->exp_min > 0)
7634 min = exp->exp_min - 1;
7635 else
7636 min = 0;
7637 if (exp->exp_max < 0)
7638 max = -1;
7639 else
7640 max = exp->exp_max - 1;
7641
7642#ifdef DEBUG_DERIV
7643 printf("loop match => SEQ(COUNT())\n");
7644#endif
7645 exp->exp_left->ref++;
7646 tmp2 = xmlExpHashGetEntry(ctxt, XML_EXP_COUNT, exp->exp_left,
7647 NULL, NULL, min, max);
7648 if (tmp2 == NULL)
7649 return(NULL);
7650 ret = xmlExpHashGetEntry(ctxt, XML_EXP_SEQ, tmp, tmp2,
7651 NULL, 0, 0);
7652 return(ret);
7653 }
7654 }
7655
Daniel Veillardccb4d412005-08-23 13:41:17 +00007656#ifdef DEBUG_DERIV
7657 printf("Fallback to derivative\n");
7658#endif
7659 if (IS_NILLABLE(sub)) {
7660 if (!(IS_NILLABLE(exp)))
7661 return(forbiddenExp);
7662 else
7663 ret = emptyExp;
7664 } else
7665 ret = NULL;
Daniel Veillard81a8ec62005-08-22 00:20:58 +00007666 /*
7667 * here the structured derivation made no progress so
7668 * we use the default token based derivation to force one more step
7669 */
7670 if (ctxt->tabSize == 0)
7671 ctxt->tabSize = 40;
7672
7673 tab = (const xmlChar **) xmlMalloc(ctxt->tabSize *
7674 sizeof(const xmlChar *));
7675 if (tab == NULL) {
7676 return(NULL);
7677 }
7678
Daniel Veillard81a8ec62005-08-22 00:20:58 +00007679 /*
7680 * collect all the strings accepted by the subexpression on input
7681 */
7682 len = xmlExpGetStartInt(ctxt, sub, tab, ctxt->tabSize, 0);
7683 while (len < 0) {
7684 const xmlChar **temp;
Rob Richards54a8f672005-10-07 02:33:00 +00007685 temp = (const xmlChar **) xmlRealloc((xmlChar **) tab, ctxt->tabSize * 2 *
Daniel Veillard81a8ec62005-08-22 00:20:58 +00007686 sizeof(const xmlChar *));
7687 if (temp == NULL) {
Rob Richards54a8f672005-10-07 02:33:00 +00007688 xmlFree((xmlChar **) tab);
Daniel Veillard81a8ec62005-08-22 00:20:58 +00007689 return(NULL);
7690 }
7691 tab = temp;
7692 ctxt->tabSize *= 2;
7693 len = xmlExpGetStartInt(ctxt, sub, tab, ctxt->tabSize, 0);
7694 }
Daniel Veillard81a8ec62005-08-22 00:20:58 +00007695 for (i = 0;i < len;i++) {
7696 tmp = xmlExpStringDeriveInt(ctxt, exp, tab[i]);
7697 if ((tmp == NULL) || (tmp == forbiddenExp)) {
7698 xmlExpFree(ctxt, ret);
Rob Richards54a8f672005-10-07 02:33:00 +00007699 xmlFree((xmlChar **) tab);
Daniel Veillard81a8ec62005-08-22 00:20:58 +00007700 return(tmp);
7701 }
7702 tmp2 = xmlExpStringDeriveInt(ctxt, sub, tab[i]);
7703 if ((tmp2 == NULL) || (tmp2 == forbiddenExp)) {
7704 xmlExpFree(ctxt, tmp);
7705 xmlExpFree(ctxt, ret);
Rob Richards54a8f672005-10-07 02:33:00 +00007706 xmlFree((xmlChar **) tab);
Daniel Veillard81a8ec62005-08-22 00:20:58 +00007707 return(tmp);
7708 }
7709 tmp3 = xmlExpExpDeriveInt(ctxt, tmp, tmp2);
7710 xmlExpFree(ctxt, tmp);
7711 xmlExpFree(ctxt, tmp2);
7712
7713 if ((tmp3 == NULL) || (tmp3 == forbiddenExp)) {
7714 xmlExpFree(ctxt, ret);
Rob Richards54a8f672005-10-07 02:33:00 +00007715 xmlFree((xmlChar **) tab);
Daniel Veillard81a8ec62005-08-22 00:20:58 +00007716 return(tmp3);
7717 }
7718
7719 if (ret == NULL)
7720 ret = tmp3;
7721 else {
7722 ret = xmlExpHashGetEntry(ctxt, XML_EXP_OR, ret, tmp3, NULL, 0, 0);
7723 if (ret == NULL) {
Rob Richards54a8f672005-10-07 02:33:00 +00007724 xmlFree((xmlChar **) tab);
Daniel Veillard81a8ec62005-08-22 00:20:58 +00007725 return(NULL);
7726 }
7727 }
7728 }
Rob Richards54a8f672005-10-07 02:33:00 +00007729 xmlFree((xmlChar **) tab);
Daniel Veillard81a8ec62005-08-22 00:20:58 +00007730 return(ret);
7731}
7732
7733/**
Daniel Veillard0090bd52005-08-22 14:43:43 +00007734 * xmlExpExpDerive:
7735 * @ctxt: the expressions context
7736 * @exp: the englobing expression
7737 * @sub: the subexpression
7738 *
7739 * Evaluates the expression resulting from @exp consuming a sub expression @sub
7740 * Based on algebraic derivation and sometimes direct Brzozowski derivation
7741 * it usually tatkes less than linear time and can handle expressions generating
7742 * infinite languages.
7743 *
7744 * Returns the resulting expression or NULL in case of internal error, the
7745 * result must be freed
7746 */
7747xmlExpNodePtr
7748xmlExpExpDerive(xmlExpCtxtPtr ctxt, xmlExpNodePtr exp, xmlExpNodePtr sub) {
7749 if ((exp == NULL) || (ctxt == NULL) || (sub == NULL))
7750 return(NULL);
7751
7752 /*
7753 * O(1) speedups
7754 */
7755 if (IS_NILLABLE(sub) && (!IS_NILLABLE(exp))) {
7756#ifdef DEBUG_DERIV
7757 printf("Sub nillable and not exp : can't subsume\n");
7758#endif
7759 return(forbiddenExp);
7760 }
7761 if (xmlExpCheckCard(exp, sub) == 0) {
7762#ifdef DEBUG_DERIV
7763 printf("sub generate longuer sequances than exp : can't subsume\n");
7764#endif
7765 return(forbiddenExp);
7766 }
7767 return(xmlExpExpDeriveInt(ctxt, exp, sub));
7768}
7769
7770/**
Daniel Veillard81a8ec62005-08-22 00:20:58 +00007771 * xmlExpSubsume:
7772 * @ctxt: the expressions context
7773 * @exp: the englobing expression
7774 * @sub: the subexpression
7775 *
7776 * Check whether @exp accepts all the languages accexpted by @sub
7777 * the input being a subexpression.
7778 *
7779 * Returns 1 if true 0 if false and -1 in case of failure.
7780 */
7781int
7782xmlExpSubsume(xmlExpCtxtPtr ctxt, xmlExpNodePtr exp, xmlExpNodePtr sub) {
7783 xmlExpNodePtr tmp;
7784
7785 if ((exp == NULL) || (ctxt == NULL) || (sub == NULL))
7786 return(-1);
7787
7788 /*
7789 * TODO: speedup by checking the language of sub is a subset of the
7790 * language of exp
7791 */
7792 /*
7793 * O(1) speedups
7794 */
7795 if (IS_NILLABLE(sub) && (!IS_NILLABLE(exp))) {
7796#ifdef DEBUG_DERIV
7797 printf("Sub nillable and not exp : can't subsume\n");
7798#endif
7799 return(0);
7800 }
7801 if (xmlExpCheckCard(exp, sub) == 0) {
7802#ifdef DEBUG_DERIV
7803 printf("sub generate longuer sequances than exp : can't subsume\n");
7804#endif
7805 return(0);
7806 }
7807 tmp = xmlExpExpDeriveInt(ctxt, exp, sub);
7808#ifdef DEBUG_DERIV
7809 printf("Result derivation :\n");
7810 PRINT_EXP(tmp);
7811#endif
7812 if (tmp == NULL)
7813 return(-1);
7814 if (tmp == forbiddenExp)
7815 return(0);
7816 if (tmp == emptyExp)
7817 return(1);
7818 if ((tmp != NULL) && (IS_NILLABLE(tmp))) {
7819 xmlExpFree(ctxt, tmp);
7820 return(1);
7821 }
7822 xmlExpFree(ctxt, tmp);
7823 return(0);
7824}
Daniel Veillard465a0002005-08-22 12:07:04 +00007825
7826/************************************************************************
7827 * *
7828 * Parsing expression *
7829 * *
7830 ************************************************************************/
7831
7832static xmlExpNodePtr xmlExpParseExpr(xmlExpCtxtPtr ctxt);
7833
7834#undef CUR
7835#define CUR (*ctxt->cur)
7836#undef NEXT
7837#define NEXT ctxt->cur++;
7838#undef IS_BLANK
7839#define IS_BLANK(c) ((c == ' ') || (c == '\n') || (c == '\r') || (c == '\t'))
7840#define SKIP_BLANKS while (IS_BLANK(*ctxt->cur)) ctxt->cur++;
7841
7842static int
7843xmlExpParseNumber(xmlExpCtxtPtr ctxt) {
7844 int ret = 0;
7845
7846 SKIP_BLANKS
7847 if (CUR == '*') {
7848 NEXT
7849 return(-1);
7850 }
7851 if ((CUR < '0') || (CUR > '9'))
7852 return(-1);
7853 while ((CUR >= '0') && (CUR <= '9')) {
7854 ret = ret * 10 + (CUR - '0');
7855 NEXT
7856 }
7857 return(ret);
7858}
7859
7860static xmlExpNodePtr
7861xmlExpParseOr(xmlExpCtxtPtr ctxt) {
7862 const char *base;
7863 xmlExpNodePtr ret;
7864 const xmlChar *val;
7865
7866 SKIP_BLANKS
7867 base = ctxt->cur;
7868 if (*ctxt->cur == '(') {
7869 NEXT
7870 ret = xmlExpParseExpr(ctxt);
7871 SKIP_BLANKS
7872 if (*ctxt->cur != ')') {
7873 fprintf(stderr, "unbalanced '(' : %s\n", base);
7874 xmlExpFree(ctxt, ret);
7875 return(NULL);
7876 }
7877 NEXT;
7878 SKIP_BLANKS
7879 goto parse_quantifier;
7880 }
7881 while ((CUR != 0) && (!(IS_BLANK(CUR))) && (CUR != '(') &&
7882 (CUR != ')') && (CUR != '|') && (CUR != ',') && (CUR != '{') &&
7883 (CUR != '*') && (CUR != '+') && (CUR != '?') && (CUR != '}'))
7884 NEXT;
7885 val = xmlDictLookup(ctxt->dict, BAD_CAST base, ctxt->cur - base);
7886 if (val == NULL)
7887 return(NULL);
7888 ret = xmlExpHashGetEntry(ctxt, XML_EXP_ATOM, NULL, NULL, val, 0, 0);
7889 if (ret == NULL)
7890 return(NULL);
7891 SKIP_BLANKS
7892parse_quantifier:
7893 if (CUR == '{') {
7894 int min, max;
7895
7896 NEXT
7897 min = xmlExpParseNumber(ctxt);
7898 if (min < 0) {
7899 xmlExpFree(ctxt, ret);
7900 return(NULL);
7901 }
7902 SKIP_BLANKS
7903 if (CUR == ',') {
7904 NEXT
7905 max = xmlExpParseNumber(ctxt);
7906 SKIP_BLANKS
7907 } else
7908 max = min;
7909 if (CUR != '}') {
7910 xmlExpFree(ctxt, ret);
7911 return(NULL);
7912 }
7913 NEXT
7914 ret = xmlExpHashGetEntry(ctxt, XML_EXP_COUNT, ret, NULL, NULL,
7915 min, max);
7916 SKIP_BLANKS
7917 } else if (CUR == '?') {
7918 NEXT
7919 ret = xmlExpHashGetEntry(ctxt, XML_EXP_COUNT, ret, NULL, NULL,
7920 0, 1);
7921 SKIP_BLANKS
7922 } else if (CUR == '+') {
7923 NEXT
7924 ret = xmlExpHashGetEntry(ctxt, XML_EXP_COUNT, ret, NULL, NULL,
7925 1, -1);
7926 SKIP_BLANKS
7927 } else if (CUR == '*') {
7928 NEXT
7929 ret = xmlExpHashGetEntry(ctxt, XML_EXP_COUNT, ret, NULL, NULL,
7930 0, -1);
7931 SKIP_BLANKS
7932 }
7933 return(ret);
7934}
7935
7936
7937static xmlExpNodePtr
7938xmlExpParseSeq(xmlExpCtxtPtr ctxt) {
7939 xmlExpNodePtr ret, right;
7940
7941 ret = xmlExpParseOr(ctxt);
7942 SKIP_BLANKS
7943 while (CUR == '|') {
7944 NEXT
7945 right = xmlExpParseOr(ctxt);
7946 if (right == NULL) {
7947 xmlExpFree(ctxt, ret);
7948 return(NULL);
7949 }
7950 ret = xmlExpHashGetEntry(ctxt, XML_EXP_OR, ret, right, NULL, 0, 0);
7951 if (ret == NULL)
7952 return(NULL);
7953 }
7954 return(ret);
7955}
7956
7957static xmlExpNodePtr
7958xmlExpParseExpr(xmlExpCtxtPtr ctxt) {
7959 xmlExpNodePtr ret, right;
7960
7961 ret = xmlExpParseSeq(ctxt);
7962 SKIP_BLANKS
7963 while (CUR == ',') {
7964 NEXT
7965 right = xmlExpParseSeq(ctxt);
7966 if (right == NULL) {
7967 xmlExpFree(ctxt, ret);
7968 return(NULL);
7969 }
7970 ret = xmlExpHashGetEntry(ctxt, XML_EXP_SEQ, ret, right, NULL, 0, 0);
7971 if (ret == NULL)
7972 return(NULL);
7973 }
7974 return(ret);
7975}
7976
7977/**
7978 * xmlExpParse:
7979 * @ctxt: the expressions context
7980 * @expr: the 0 terminated string
7981 *
7982 * Minimal parser for regexps, it understand the following constructs
7983 * - string terminals
7984 * - choice operator |
7985 * - sequence operator ,
7986 * - subexpressions (...)
7987 * - usual cardinality operators + * and ?
7988 * - finite sequences { min, max }
7989 * - infinite sequences { min, * }
7990 * There is minimal checkings made especially no checking on strings values
7991 *
7992 * Returns a new expression or NULL in case of failure
7993 */
7994xmlExpNodePtr
7995xmlExpParse(xmlExpCtxtPtr ctxt, const char *expr) {
7996 xmlExpNodePtr ret;
7997
7998 ctxt->expr = expr;
7999 ctxt->cur = expr;
8000
8001 ret = xmlExpParseExpr(ctxt);
8002 SKIP_BLANKS
8003 if (*ctxt->cur != 0) {
8004 xmlExpFree(ctxt, ret);
8005 return(NULL);
8006 }
8007 return(ret);
8008}
8009
8010static void
8011xmlExpDumpInt(xmlBufferPtr buf, xmlExpNodePtr expr, int glob) {
8012 xmlExpNodePtr c;
8013
8014 if (expr == NULL) return;
8015 if (glob) xmlBufferWriteChar(buf, "(");
8016 switch (expr->type) {
8017 case XML_EXP_EMPTY:
8018 xmlBufferWriteChar(buf, "empty");
8019 break;
8020 case XML_EXP_FORBID:
8021 xmlBufferWriteChar(buf, "forbidden");
8022 break;
8023 case XML_EXP_ATOM:
8024 xmlBufferWriteCHAR(buf, expr->exp_str);
8025 break;
8026 case XML_EXP_SEQ:
8027 c = expr->exp_left;
8028 if ((c->type == XML_EXP_SEQ) || (c->type == XML_EXP_OR))
8029 xmlExpDumpInt(buf, c, 1);
8030 else
8031 xmlExpDumpInt(buf, c, 0);
8032 xmlBufferWriteChar(buf, " , ");
8033 c = expr->exp_right;
8034 if ((c->type == XML_EXP_SEQ) || (c->type == XML_EXP_OR))
8035 xmlExpDumpInt(buf, c, 1);
8036 else
8037 xmlExpDumpInt(buf, c, 0);
8038 break;
8039 case XML_EXP_OR:
8040 c = expr->exp_left;
8041 if ((c->type == XML_EXP_SEQ) || (c->type == XML_EXP_OR))
8042 xmlExpDumpInt(buf, c, 1);
8043 else
8044 xmlExpDumpInt(buf, c, 0);
8045 xmlBufferWriteChar(buf, " | ");
8046 c = expr->exp_right;
8047 if ((c->type == XML_EXP_SEQ) || (c->type == XML_EXP_OR))
8048 xmlExpDumpInt(buf, c, 1);
8049 else
8050 xmlExpDumpInt(buf, c, 0);
8051 break;
8052 case XML_EXP_COUNT: {
8053 char rep[40];
8054
8055 c = expr->exp_left;
8056 if ((c->type == XML_EXP_SEQ) || (c->type == XML_EXP_OR))
8057 xmlExpDumpInt(buf, c, 1);
8058 else
8059 xmlExpDumpInt(buf, c, 0);
8060 if ((expr->exp_min == 0) && (expr->exp_max == 1)) {
8061 rep[0] = '?';
8062 rep[1] = 0;
8063 } else if ((expr->exp_min == 0) && (expr->exp_max == -1)) {
8064 rep[0] = '*';
8065 rep[1] = 0;
8066 } else if ((expr->exp_min == 1) && (expr->exp_max == -1)) {
8067 rep[0] = '+';
8068 rep[1] = 0;
8069 } else if (expr->exp_max == expr->exp_min) {
8070 snprintf(rep, 39, "{%d}", expr->exp_min);
8071 } else if (expr->exp_max < 0) {
8072 snprintf(rep, 39, "{%d,inf}", expr->exp_min);
8073 } else {
8074 snprintf(rep, 39, "{%d,%d}", expr->exp_min, expr->exp_max);
8075 }
8076 rep[39] = 0;
8077 xmlBufferWriteChar(buf, rep);
8078 break;
8079 }
8080 default:
8081 fprintf(stderr, "Error in tree\n");
8082 }
8083 if (glob)
8084 xmlBufferWriteChar(buf, ")");
8085}
8086/**
8087 * xmlExpDump:
8088 * @buf: a buffer to receive the output
8089 * @expr: the compiled expression
8090 *
8091 * Serialize the expression as compiled to the buffer
8092 */
8093void
Daniel Veillard5eee7672005-08-22 21:22:27 +00008094xmlExpDump(xmlBufferPtr buf, xmlExpNodePtr expr) {
8095 if ((buf == NULL) || (expr == NULL))
Daniel Veillard465a0002005-08-22 12:07:04 +00008096 return;
Daniel Veillard5eee7672005-08-22 21:22:27 +00008097 xmlExpDumpInt(buf, expr, 0);
Daniel Veillard465a0002005-08-22 12:07:04 +00008098}
8099
8100/**
8101 * xmlExpMaxToken:
8102 * @expr: a compiled expression
8103 *
8104 * Indicate the maximum number of input a expression can accept
8105 *
8106 * Returns the maximum length or -1 in case of error
8107 */
8108int
8109xmlExpMaxToken(xmlExpNodePtr expr) {
8110 if (expr == NULL)
8111 return(-1);
8112 return(expr->c_max);
8113}
8114
8115/**
8116 * xmlExpCtxtNbNodes:
8117 * @ctxt: an expression context
8118 *
8119 * Debugging facility provides the number of allocated nodes at a that point
8120 *
8121 * Returns the number of nodes in use or -1 in case of error
8122 */
8123int
8124xmlExpCtxtNbNodes(xmlExpCtxtPtr ctxt) {
8125 if (ctxt == NULL)
8126 return(-1);
8127 return(ctxt->nb_nodes);
8128}
8129
8130/**
8131 * xmlExpCtxtNbCons:
8132 * @ctxt: an expression context
8133 *
8134 * Debugging facility provides the number of allocated nodes over lifetime
8135 *
8136 * Returns the number of nodes ever allocated or -1 in case of error
8137 */
8138int
8139xmlExpCtxtNbCons(xmlExpCtxtPtr ctxt) {
8140 if (ctxt == NULL)
8141 return(-1);
8142 return(ctxt->nb_cons);
8143}
8144
Daniel Veillard81a8ec62005-08-22 00:20:58 +00008145#endif /* LIBXML_EXPR_ENABLED */
Daniel Veillard5d4644e2005-04-01 13:11:58 +00008146#define bottom_xmlregexp
8147#include "elfgcchack.h"
Daniel Veillard4255d502002-04-16 15:50:10 +00008148#endif /* LIBXML_REGEXP_ENABLED */