blob: befd91367a54657dda3f58029f678fc4addc8286 [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 Veillard94cc1032005-09-15 13:09:00 +000045#define MAX_PUSH 100000
46
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 '|'
Daniel Veillard4255d502002-04-16 15:50:10 +000057
Daniel Veillarde19fc232002-04-22 16:01:24 +000058/**
59 * TODO:
60 *
61 * macro to flag unimplemented blocks
62 */
63#define TODO \
64 xmlGenericError(xmlGenericErrorContext, \
65 "Unimplemented block at %s:%d\n", \
66 __FILE__, __LINE__);
67
Daniel Veillard4255d502002-04-16 15:50:10 +000068/************************************************************************
69 * *
70 * Datatypes and structures *
71 * *
72 ************************************************************************/
73
74typedef enum {
75 XML_REGEXP_EPSILON = 1,
76 XML_REGEXP_CHARVAL,
77 XML_REGEXP_RANGES,
78 XML_REGEXP_SUBREG,
79 XML_REGEXP_STRING,
80 XML_REGEXP_ANYCHAR, /* . */
81 XML_REGEXP_ANYSPACE, /* \s */
82 XML_REGEXP_NOTSPACE, /* \S */
83 XML_REGEXP_INITNAME, /* \l */
84 XML_REGEXP_NOTINITNAME, /* \l */
85 XML_REGEXP_NAMECHAR, /* \c */
86 XML_REGEXP_NOTNAMECHAR, /* \C */
87 XML_REGEXP_DECIMAL, /* \d */
88 XML_REGEXP_NOTDECIMAL, /* \d */
89 XML_REGEXP_REALCHAR, /* \w */
90 XML_REGEXP_NOTREALCHAR, /* \w */
91 XML_REGEXP_LETTER,
92 XML_REGEXP_LETTER_UPPERCASE,
93 XML_REGEXP_LETTER_LOWERCASE,
94 XML_REGEXP_LETTER_TITLECASE,
95 XML_REGEXP_LETTER_MODIFIER,
96 XML_REGEXP_LETTER_OTHERS,
97 XML_REGEXP_MARK,
98 XML_REGEXP_MARK_NONSPACING,
99 XML_REGEXP_MARK_SPACECOMBINING,
100 XML_REGEXP_MARK_ENCLOSING,
101 XML_REGEXP_NUMBER,
102 XML_REGEXP_NUMBER_DECIMAL,
103 XML_REGEXP_NUMBER_LETTER,
104 XML_REGEXP_NUMBER_OTHERS,
105 XML_REGEXP_PUNCT,
106 XML_REGEXP_PUNCT_CONNECTOR,
107 XML_REGEXP_PUNCT_DASH,
108 XML_REGEXP_PUNCT_OPEN,
109 XML_REGEXP_PUNCT_CLOSE,
110 XML_REGEXP_PUNCT_INITQUOTE,
111 XML_REGEXP_PUNCT_FINQUOTE,
112 XML_REGEXP_PUNCT_OTHERS,
113 XML_REGEXP_SEPAR,
114 XML_REGEXP_SEPAR_SPACE,
115 XML_REGEXP_SEPAR_LINE,
116 XML_REGEXP_SEPAR_PARA,
117 XML_REGEXP_SYMBOL,
118 XML_REGEXP_SYMBOL_MATH,
119 XML_REGEXP_SYMBOL_CURRENCY,
120 XML_REGEXP_SYMBOL_MODIFIER,
121 XML_REGEXP_SYMBOL_OTHERS,
122 XML_REGEXP_OTHER,
123 XML_REGEXP_OTHER_CONTROL,
124 XML_REGEXP_OTHER_FORMAT,
125 XML_REGEXP_OTHER_PRIVATE,
126 XML_REGEXP_OTHER_NA,
127 XML_REGEXP_BLOCK_NAME
128} xmlRegAtomType;
129
130typedef enum {
131 XML_REGEXP_QUANT_EPSILON = 1,
132 XML_REGEXP_QUANT_ONCE,
133 XML_REGEXP_QUANT_OPT,
134 XML_REGEXP_QUANT_MULT,
135 XML_REGEXP_QUANT_PLUS,
Daniel Veillard7646b182002-04-20 06:41:40 +0000136 XML_REGEXP_QUANT_ONCEONLY,
137 XML_REGEXP_QUANT_ALL,
Daniel Veillard4255d502002-04-16 15:50:10 +0000138 XML_REGEXP_QUANT_RANGE
139} xmlRegQuantType;
140
141typedef enum {
142 XML_REGEXP_START_STATE = 1,
143 XML_REGEXP_FINAL_STATE,
Daniel Veillardcc026dc2005-01-12 13:21:17 +0000144 XML_REGEXP_TRANS_STATE,
145 XML_REGEXP_SINK_STATE
Daniel Veillard4255d502002-04-16 15:50:10 +0000146} xmlRegStateType;
147
148typedef enum {
149 XML_REGEXP_MARK_NORMAL = 0,
150 XML_REGEXP_MARK_START,
151 XML_REGEXP_MARK_VISITED
152} xmlRegMarkedType;
153
154typedef struct _xmlRegRange xmlRegRange;
155typedef xmlRegRange *xmlRegRangePtr;
156
157struct _xmlRegRange {
Daniel Veillardf8b9de32003-11-24 14:27:26 +0000158 int neg; /* 0 normal, 1 not, 2 exclude */
Daniel Veillard4255d502002-04-16 15:50:10 +0000159 xmlRegAtomType type;
160 int start;
161 int end;
162 xmlChar *blockName;
163};
164
165typedef struct _xmlRegAtom xmlRegAtom;
166typedef xmlRegAtom *xmlRegAtomPtr;
167
168typedef struct _xmlAutomataState xmlRegState;
169typedef xmlRegState *xmlRegStatePtr;
170
171struct _xmlRegAtom {
172 int no;
173 xmlRegAtomType type;
174 xmlRegQuantType quant;
175 int min;
176 int max;
177
178 void *valuep;
Daniel Veillarda646cfd2002-09-17 21:50:03 +0000179 void *valuep2;
Daniel Veillard4255d502002-04-16 15:50:10 +0000180 int neg;
181 int codepoint;
182 xmlRegStatePtr start;
183 xmlRegStatePtr stop;
184 int maxRanges;
185 int nbRanges;
186 xmlRegRangePtr *ranges;
187 void *data;
188};
189
190typedef struct _xmlRegCounter xmlRegCounter;
191typedef xmlRegCounter *xmlRegCounterPtr;
192
193struct _xmlRegCounter {
194 int min;
195 int max;
196};
197
198typedef struct _xmlRegTrans xmlRegTrans;
199typedef xmlRegTrans *xmlRegTransPtr;
200
201struct _xmlRegTrans {
202 xmlRegAtomPtr atom;
203 int to;
204 int counter;
205 int count;
206};
207
208struct _xmlAutomataState {
209 xmlRegStateType type;
210 xmlRegMarkedType mark;
Daniel Veillard23e73572002-09-19 19:56:43 +0000211 xmlRegMarkedType reached;
Daniel Veillard4255d502002-04-16 15:50:10 +0000212 int no;
Daniel Veillard4255d502002-04-16 15:50:10 +0000213 int maxTrans;
214 int nbTrans;
215 xmlRegTrans *trans;
Daniel Veillarddb68b742005-07-30 13:18:24 +0000216 /* knowing states ponting to us can speed things up */
217 int maxTransTo;
218 int nbTransTo;
219 int *transTo;
Daniel Veillard4255d502002-04-16 15:50:10 +0000220};
221
222typedef struct _xmlAutomata xmlRegParserCtxt;
223typedef xmlRegParserCtxt *xmlRegParserCtxtPtr;
224
225struct _xmlAutomata {
226 xmlChar *string;
227 xmlChar *cur;
228
229 int error;
230 int neg;
231
232 xmlRegStatePtr start;
233 xmlRegStatePtr end;
234 xmlRegStatePtr state;
235
236 xmlRegAtomPtr atom;
237
238 int maxAtoms;
239 int nbAtoms;
240 xmlRegAtomPtr *atoms;
241
242 int maxStates;
243 int nbStates;
244 xmlRegStatePtr *states;
245
246 int maxCounters;
247 int nbCounters;
248 xmlRegCounter *counters;
Daniel Veillarde19fc232002-04-22 16:01:24 +0000249
250 int determinist;
Daniel Veillard6e65e152005-08-09 11:09:52 +0000251 int negs;
Daniel Veillard4255d502002-04-16 15:50:10 +0000252};
253
254struct _xmlRegexp {
255 xmlChar *string;
256 int nbStates;
257 xmlRegStatePtr *states;
258 int nbAtoms;
259 xmlRegAtomPtr *atoms;
260 int nbCounters;
261 xmlRegCounter *counters;
Daniel Veillarde19fc232002-04-22 16:01:24 +0000262 int determinist;
Daniel Veillard23e73572002-09-19 19:56:43 +0000263 /*
264 * That's the compact form for determinists automatas
265 */
266 int nbstates;
267 int *compact;
Daniel Veillard118aed72002-09-24 14:13:13 +0000268 void **transdata;
Daniel Veillard23e73572002-09-19 19:56:43 +0000269 int nbstrings;
270 xmlChar **stringMap;
Daniel Veillard4255d502002-04-16 15:50:10 +0000271};
272
273typedef struct _xmlRegExecRollback xmlRegExecRollback;
274typedef xmlRegExecRollback *xmlRegExecRollbackPtr;
275
276struct _xmlRegExecRollback {
277 xmlRegStatePtr state;/* the current state */
278 int index; /* the index in the input stack */
279 int nextbranch; /* the next transition to explore in that state */
William M. Brackddf71d62004-05-06 04:17:26 +0000280 int *counts; /* save the automata state if it has some */
Daniel Veillard4255d502002-04-16 15:50:10 +0000281};
282
283typedef struct _xmlRegInputToken xmlRegInputToken;
284typedef xmlRegInputToken *xmlRegInputTokenPtr;
285
286struct _xmlRegInputToken {
287 xmlChar *value;
288 void *data;
289};
290
291struct _xmlRegExecCtxt {
292 int status; /* execution status != 0 indicate an error */
William M. Brackddf71d62004-05-06 04:17:26 +0000293 int determinist; /* did we find an indeterministic behaviour */
Daniel Veillard4255d502002-04-16 15:50:10 +0000294 xmlRegexpPtr comp; /* the compiled regexp */
295 xmlRegExecCallbacks callback;
296 void *data;
297
298 xmlRegStatePtr state;/* the current state */
299 int transno; /* the current transition on that state */
William M. Brackddf71d62004-05-06 04:17:26 +0000300 int transcount; /* the number of chars in char counted transitions */
Daniel Veillard4255d502002-04-16 15:50:10 +0000301
302 /*
303 * A stack of rollback states
304 */
305 int maxRollbacks;
306 int nbRollbacks;
307 xmlRegExecRollback *rollbacks;
308
309 /*
310 * The state of the automata if any
311 */
312 int *counts;
313
314 /*
315 * The input stack
316 */
317 int inputStackMax;
318 int inputStackNr;
319 int index;
320 int *charStack;
321 const xmlChar *inputString; /* when operating on characters */
322 xmlRegInputTokenPtr inputStack;/* when operating on strings */
323
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +0000324 /*
325 * error handling
326 */
327 int errStateNo; /* the error state number */
328 xmlRegStatePtr errState; /* the error state */
329 xmlChar *errString; /* the string raising the error */
330 int *errCounts; /* counters at the error state */
Daniel Veillard94cc1032005-09-15 13:09:00 +0000331 int nbPush;
Daniel Veillard4255d502002-04-16 15:50:10 +0000332};
333
Daniel Veillard441bc322002-04-20 17:38:48 +0000334#define REGEXP_ALL_COUNTER 0x123456
335#define REGEXP_ALL_LAX_COUNTER 0x123457
Daniel Veillard7646b182002-04-20 06:41:40 +0000336
Daniel Veillard4255d502002-04-16 15:50:10 +0000337static void xmlFAParseRegExp(xmlRegParserCtxtPtr ctxt, int top);
Daniel Veillard23e73572002-09-19 19:56:43 +0000338static void xmlRegFreeState(xmlRegStatePtr state);
339static void xmlRegFreeAtom(xmlRegAtomPtr atom);
Daniel Veillard9efc4762005-07-19 14:33:55 +0000340static int xmlRegStrEqualWildcard(const xmlChar *expStr, const xmlChar *valStr);
Daniel Veillard4255d502002-04-16 15:50:10 +0000341
342/************************************************************************
Daniel Veillardff46a042003-10-08 08:53:17 +0000343 * *
344 * Regexp memory error handler *
345 * *
346 ************************************************************************/
347/**
348 * xmlRegexpErrMemory:
William M. Brackddf71d62004-05-06 04:17:26 +0000349 * @extra: extra information
Daniel Veillardff46a042003-10-08 08:53:17 +0000350 *
351 * Handle an out of memory condition
352 */
353static void
354xmlRegexpErrMemory(xmlRegParserCtxtPtr ctxt, const char *extra)
355{
356 const char *regexp = NULL;
357 if (ctxt != NULL) {
358 regexp = (const char *) ctxt->string;
359 ctxt->error = XML_ERR_NO_MEMORY;
360 }
Daniel Veillard659e71e2003-10-10 14:10:40 +0000361 __xmlRaiseError(NULL, NULL, NULL, NULL, NULL, XML_FROM_REGEXP,
Daniel Veillardff46a042003-10-08 08:53:17 +0000362 XML_ERR_NO_MEMORY, XML_ERR_FATAL, NULL, 0, extra,
363 regexp, NULL, 0, 0,
364 "Memory allocation failed : %s\n", extra);
365}
366
367/**
368 * xmlRegexpErrCompile:
William M. Brackddf71d62004-05-06 04:17:26 +0000369 * @extra: extra information
Daniel Veillardff46a042003-10-08 08:53:17 +0000370 *
William M. Brackddf71d62004-05-06 04:17:26 +0000371 * Handle a compilation failure
Daniel Veillardff46a042003-10-08 08:53:17 +0000372 */
373static void
374xmlRegexpErrCompile(xmlRegParserCtxtPtr ctxt, const char *extra)
375{
376 const char *regexp = NULL;
377 int idx = 0;
378
379 if (ctxt != NULL) {
380 regexp = (const char *) ctxt->string;
381 idx = ctxt->cur - ctxt->string;
382 ctxt->error = XML_REGEXP_COMPILE_ERROR;
383 }
Daniel Veillard659e71e2003-10-10 14:10:40 +0000384 __xmlRaiseError(NULL, NULL, NULL, NULL, NULL, XML_FROM_REGEXP,
Daniel Veillardff46a042003-10-08 08:53:17 +0000385 XML_REGEXP_COMPILE_ERROR, XML_ERR_FATAL, NULL, 0, extra,
386 regexp, NULL, idx, 0,
387 "failed to compile: %s\n", extra);
388}
389
390/************************************************************************
Daniel Veillard4255d502002-04-16 15:50:10 +0000391 * *
392 * Allocation/Deallocation *
393 * *
394 ************************************************************************/
395
Daniel Veillard23e73572002-09-19 19:56:43 +0000396static int xmlFAComputesDeterminism(xmlRegParserCtxtPtr ctxt);
Daniel Veillard4255d502002-04-16 15:50:10 +0000397/**
398 * xmlRegEpxFromParse:
399 * @ctxt: the parser context used to build it
400 *
William M. Brackddf71d62004-05-06 04:17:26 +0000401 * Allocate a new regexp and fill it with the result from the parser
Daniel Veillard4255d502002-04-16 15:50:10 +0000402 *
403 * Returns the new regexp or NULL in case of error
404 */
405static xmlRegexpPtr
406xmlRegEpxFromParse(xmlRegParserCtxtPtr ctxt) {
407 xmlRegexpPtr ret;
408
409 ret = (xmlRegexpPtr) xmlMalloc(sizeof(xmlRegexp));
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000410 if (ret == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +0000411 xmlRegexpErrMemory(ctxt, "compiling regexp");
Daniel Veillard4255d502002-04-16 15:50:10 +0000412 return(NULL);
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000413 }
Daniel Veillard4255d502002-04-16 15:50:10 +0000414 memset(ret, 0, sizeof(xmlRegexp));
415 ret->string = ctxt->string;
Daniel Veillard4255d502002-04-16 15:50:10 +0000416 ret->nbStates = ctxt->nbStates;
Daniel Veillard4255d502002-04-16 15:50:10 +0000417 ret->states = ctxt->states;
Daniel Veillard4255d502002-04-16 15:50:10 +0000418 ret->nbAtoms = ctxt->nbAtoms;
Daniel Veillard4255d502002-04-16 15:50:10 +0000419 ret->atoms = ctxt->atoms;
Daniel Veillard4255d502002-04-16 15:50:10 +0000420 ret->nbCounters = ctxt->nbCounters;
Daniel Veillard4255d502002-04-16 15:50:10 +0000421 ret->counters = ctxt->counters;
Daniel Veillarde19fc232002-04-22 16:01:24 +0000422 ret->determinist = ctxt->determinist;
Daniel Veillard23e73572002-09-19 19:56:43 +0000423
424 if ((ret->determinist != 0) &&
425 (ret->nbCounters == 0) &&
Daniel Veillard6e65e152005-08-09 11:09:52 +0000426 (ctxt->negs == 0) &&
Daniel Veillard118aed72002-09-24 14:13:13 +0000427 (ret->atoms != NULL) &&
Daniel Veillard23e73572002-09-19 19:56:43 +0000428 (ret->atoms[0] != NULL) &&
429 (ret->atoms[0]->type == XML_REGEXP_STRING)) {
430 int i, j, nbstates = 0, nbatoms = 0;
431 int *stateRemap;
432 int *stringRemap;
433 int *transitions;
Daniel Veillard118aed72002-09-24 14:13:13 +0000434 void **transdata;
Daniel Veillard23e73572002-09-19 19:56:43 +0000435 xmlChar **stringMap;
436 xmlChar *value;
437
438 /*
439 * Switch to a compact representation
440 * 1/ counting the effective number of states left
William M. Brackddf71d62004-05-06 04:17:26 +0000441 * 2/ counting the unique number of atoms, and check that
Daniel Veillard23e73572002-09-19 19:56:43 +0000442 * they are all of the string type
443 * 3/ build a table state x atom for the transitions
444 */
445
446 stateRemap = xmlMalloc(ret->nbStates * sizeof(int));
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000447 if (stateRemap == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +0000448 xmlRegexpErrMemory(ctxt, "compiling regexp");
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000449 xmlFree(ret);
450 return(NULL);
451 }
Daniel Veillard23e73572002-09-19 19:56:43 +0000452 for (i = 0;i < ret->nbStates;i++) {
453 if (ret->states[i] != NULL) {
454 stateRemap[i] = nbstates;
455 nbstates++;
456 } else {
457 stateRemap[i] = -1;
458 }
459 }
460#ifdef DEBUG_COMPACTION
461 printf("Final: %d states\n", nbstates);
462#endif
463 stringMap = xmlMalloc(ret->nbAtoms * sizeof(char *));
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000464 if (stringMap == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +0000465 xmlRegexpErrMemory(ctxt, "compiling regexp");
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000466 xmlFree(stateRemap);
467 xmlFree(ret);
468 return(NULL);
469 }
Daniel Veillard23e73572002-09-19 19:56:43 +0000470 stringRemap = xmlMalloc(ret->nbAtoms * sizeof(int));
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000471 if (stringRemap == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +0000472 xmlRegexpErrMemory(ctxt, "compiling regexp");
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000473 xmlFree(stringMap);
474 xmlFree(stateRemap);
475 xmlFree(ret);
476 return(NULL);
477 }
Daniel Veillard23e73572002-09-19 19:56:43 +0000478 for (i = 0;i < ret->nbAtoms;i++) {
479 if ((ret->atoms[i]->type == XML_REGEXP_STRING) &&
480 (ret->atoms[i]->quant == XML_REGEXP_QUANT_ONCE)) {
481 value = ret->atoms[i]->valuep;
482 for (j = 0;j < nbatoms;j++) {
483 if (xmlStrEqual(stringMap[j], value)) {
484 stringRemap[i] = j;
485 break;
486 }
487 }
488 if (j >= nbatoms) {
489 stringRemap[i] = nbatoms;
490 stringMap[nbatoms] = xmlStrdup(value);
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000491 if (stringMap[nbatoms] == NULL) {
492 for (i = 0;i < nbatoms;i++)
493 xmlFree(stringMap[i]);
494 xmlFree(stringRemap);
495 xmlFree(stringMap);
496 xmlFree(stateRemap);
497 xmlFree(ret);
498 return(NULL);
499 }
Daniel Veillard23e73572002-09-19 19:56:43 +0000500 nbatoms++;
501 }
502 } else {
503 xmlFree(stateRemap);
504 xmlFree(stringRemap);
505 for (i = 0;i < nbatoms;i++)
506 xmlFree(stringMap[i]);
507 xmlFree(stringMap);
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000508 xmlFree(ret);
509 return(NULL);
Daniel Veillard23e73572002-09-19 19:56:43 +0000510 }
511 }
512#ifdef DEBUG_COMPACTION
513 printf("Final: %d atoms\n", nbatoms);
514#endif
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000515 transitions = (int *) xmlMalloc((nbstates + 1) *
516 (nbatoms + 1) * sizeof(int));
517 if (transitions == NULL) {
518 xmlFree(stateRemap);
519 xmlFree(stringRemap);
520 xmlFree(stringMap);
521 xmlFree(ret);
522 return(NULL);
523 }
524 memset(transitions, 0, (nbstates + 1) * (nbatoms + 1) * sizeof(int));
Daniel Veillard23e73572002-09-19 19:56:43 +0000525
526 /*
527 * Allocate the transition table. The first entry for each
William M. Brackddf71d62004-05-06 04:17:26 +0000528 * state corresponds to the state type.
Daniel Veillard23e73572002-09-19 19:56:43 +0000529 */
Daniel Veillard118aed72002-09-24 14:13:13 +0000530 transdata = NULL;
Daniel Veillard23e73572002-09-19 19:56:43 +0000531
532 for (i = 0;i < ret->nbStates;i++) {
533 int stateno, atomno, targetno, prev;
534 xmlRegStatePtr state;
535 xmlRegTransPtr trans;
536
537 stateno = stateRemap[i];
538 if (stateno == -1)
539 continue;
540 state = ret->states[i];
541
542 transitions[stateno * (nbatoms + 1)] = state->type;
543
544 for (j = 0;j < state->nbTrans;j++) {
545 trans = &(state->trans[j]);
546 if ((trans->to == -1) || (trans->atom == NULL))
547 continue;
548 atomno = stringRemap[trans->atom->no];
Daniel Veillard118aed72002-09-24 14:13:13 +0000549 if ((trans->atom->data != NULL) && (transdata == NULL)) {
550 transdata = (void **) xmlMalloc(nbstates * nbatoms *
551 sizeof(void *));
552 if (transdata != NULL)
553 memset(transdata, 0,
554 nbstates * nbatoms * sizeof(void *));
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000555 else {
Daniel Veillardff46a042003-10-08 08:53:17 +0000556 xmlRegexpErrMemory(ctxt, "compiling regexp");
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000557 break;
558 }
Daniel Veillard118aed72002-09-24 14:13:13 +0000559 }
Daniel Veillard23e73572002-09-19 19:56:43 +0000560 targetno = stateRemap[trans->to];
561 /*
William M. Brackddf71d62004-05-06 04:17:26 +0000562 * if the same atom can generate transitions to 2 different
Daniel Veillard23e73572002-09-19 19:56:43 +0000563 * states then it means the automata is not determinist and
564 * the compact form can't be used !
565 */
566 prev = transitions[stateno * (nbatoms + 1) + atomno + 1];
567 if (prev != 0) {
568 if (prev != targetno + 1) {
Daniel Veillard23e73572002-09-19 19:56:43 +0000569 ret->determinist = 0;
570#ifdef DEBUG_COMPACTION
571 printf("Indet: state %d trans %d, atom %d to %d : %d to %d\n",
572 i, j, trans->atom->no, trans->to, atomno, targetno);
573 printf(" previous to is %d\n", prev);
574#endif
575 ret->determinist = 0;
Daniel Veillard118aed72002-09-24 14:13:13 +0000576 if (transdata != NULL)
577 xmlFree(transdata);
Daniel Veillard23e73572002-09-19 19:56:43 +0000578 xmlFree(transitions);
579 xmlFree(stateRemap);
580 xmlFree(stringRemap);
581 for (i = 0;i < nbatoms;i++)
582 xmlFree(stringMap[i]);
583 xmlFree(stringMap);
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000584 goto not_determ;
Daniel Veillard23e73572002-09-19 19:56:43 +0000585 }
586 } else {
587#if 0
588 printf("State %d trans %d: atom %d to %d : %d to %d\n",
589 i, j, trans->atom->no, trans->to, atomno, targetno);
590#endif
591 transitions[stateno * (nbatoms + 1) + atomno + 1] =
Daniel Veillard118aed72002-09-24 14:13:13 +0000592 targetno + 1; /* to avoid 0 */
593 if (transdata != NULL)
594 transdata[stateno * nbatoms + atomno] =
595 trans->atom->data;
Daniel Veillard23e73572002-09-19 19:56:43 +0000596 }
597 }
598 }
599 ret->determinist = 1;
600#ifdef DEBUG_COMPACTION
601 /*
602 * Debug
603 */
604 for (i = 0;i < nbstates;i++) {
605 for (j = 0;j < nbatoms + 1;j++) {
606 printf("%02d ", transitions[i * (nbatoms + 1) + j]);
607 }
608 printf("\n");
609 }
610 printf("\n");
611#endif
612 /*
613 * Cleanup of the old data
614 */
615 if (ret->states != NULL) {
616 for (i = 0;i < ret->nbStates;i++)
617 xmlRegFreeState(ret->states[i]);
618 xmlFree(ret->states);
619 }
620 ret->states = NULL;
621 ret->nbStates = 0;
622 if (ret->atoms != NULL) {
623 for (i = 0;i < ret->nbAtoms;i++)
624 xmlRegFreeAtom(ret->atoms[i]);
625 xmlFree(ret->atoms);
626 }
627 ret->atoms = NULL;
628 ret->nbAtoms = 0;
629
630 ret->compact = transitions;
Daniel Veillard118aed72002-09-24 14:13:13 +0000631 ret->transdata = transdata;
Daniel Veillard23e73572002-09-19 19:56:43 +0000632 ret->stringMap = stringMap;
633 ret->nbstrings = nbatoms;
634 ret->nbstates = nbstates;
635 xmlFree(stateRemap);
636 xmlFree(stringRemap);
637 }
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000638not_determ:
639 ctxt->string = NULL;
640 ctxt->nbStates = 0;
641 ctxt->states = NULL;
642 ctxt->nbAtoms = 0;
643 ctxt->atoms = NULL;
644 ctxt->nbCounters = 0;
645 ctxt->counters = NULL;
Daniel Veillard4255d502002-04-16 15:50:10 +0000646 return(ret);
647}
648
649/**
650 * xmlRegNewParserCtxt:
651 * @string: the string to parse
652 *
653 * Allocate a new regexp parser context
654 *
655 * Returns the new context or NULL in case of error
656 */
657static xmlRegParserCtxtPtr
658xmlRegNewParserCtxt(const xmlChar *string) {
659 xmlRegParserCtxtPtr ret;
660
661 ret = (xmlRegParserCtxtPtr) xmlMalloc(sizeof(xmlRegParserCtxt));
662 if (ret == NULL)
663 return(NULL);
664 memset(ret, 0, sizeof(xmlRegParserCtxt));
665 if (string != NULL)
666 ret->string = xmlStrdup(string);
667 ret->cur = ret->string;
668 ret->neg = 0;
Daniel Veillard6e65e152005-08-09 11:09:52 +0000669 ret->negs = 0;
Daniel Veillard4255d502002-04-16 15:50:10 +0000670 ret->error = 0;
Daniel Veillarde19fc232002-04-22 16:01:24 +0000671 ret->determinist = -1;
Daniel Veillard4255d502002-04-16 15:50:10 +0000672 return(ret);
673}
674
675/**
676 * xmlRegNewRange:
677 * @ctxt: the regexp parser context
678 * @neg: is that negative
679 * @type: the type of range
680 * @start: the start codepoint
681 * @end: the end codepoint
682 *
683 * Allocate a new regexp range
684 *
685 * Returns the new range or NULL in case of error
686 */
687static xmlRegRangePtr
688xmlRegNewRange(xmlRegParserCtxtPtr ctxt,
689 int neg, xmlRegAtomType type, int start, int end) {
690 xmlRegRangePtr ret;
691
692 ret = (xmlRegRangePtr) xmlMalloc(sizeof(xmlRegRange));
693 if (ret == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +0000694 xmlRegexpErrMemory(ctxt, "allocating range");
Daniel Veillard4255d502002-04-16 15:50:10 +0000695 return(NULL);
696 }
697 ret->neg = neg;
698 ret->type = type;
699 ret->start = start;
700 ret->end = end;
701 return(ret);
702}
703
704/**
705 * xmlRegFreeRange:
706 * @range: the regexp range
707 *
708 * Free a regexp range
709 */
710static void
711xmlRegFreeRange(xmlRegRangePtr range) {
712 if (range == NULL)
713 return;
714
715 if (range->blockName != NULL)
716 xmlFree(range->blockName);
717 xmlFree(range);
718}
719
720/**
721 * xmlRegNewAtom:
722 * @ctxt: the regexp parser context
723 * @type: the type of atom
724 *
725 * Allocate a new regexp range
726 *
727 * Returns the new atom or NULL in case of error
728 */
729static xmlRegAtomPtr
730xmlRegNewAtom(xmlRegParserCtxtPtr ctxt, xmlRegAtomType type) {
731 xmlRegAtomPtr ret;
732
733 ret = (xmlRegAtomPtr) xmlMalloc(sizeof(xmlRegAtom));
734 if (ret == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +0000735 xmlRegexpErrMemory(ctxt, "allocating atom");
Daniel Veillard4255d502002-04-16 15:50:10 +0000736 return(NULL);
737 }
738 memset(ret, 0, sizeof(xmlRegAtom));
739 ret->type = type;
740 ret->quant = XML_REGEXP_QUANT_ONCE;
741 ret->min = 0;
742 ret->max = 0;
743 return(ret);
744}
745
746/**
747 * xmlRegFreeAtom:
748 * @atom: the regexp atom
749 *
750 * Free a regexp atom
751 */
752static void
753xmlRegFreeAtom(xmlRegAtomPtr atom) {
754 int i;
755
756 if (atom == NULL)
757 return;
758
759 for (i = 0;i < atom->nbRanges;i++)
760 xmlRegFreeRange(atom->ranges[i]);
761 if (atom->ranges != NULL)
762 xmlFree(atom->ranges);
Daniel Veillardde0e4982005-07-03 14:35:44 +0000763 if ((atom->type == XML_REGEXP_STRING) && (atom->valuep != NULL))
764 xmlFree(atom->valuep);
Daniel Veillard77005e62005-07-19 16:26:18 +0000765 if ((atom->type == XML_REGEXP_STRING) && (atom->valuep2 != NULL))
766 xmlFree(atom->valuep2);
Daniel Veillardde0e4982005-07-03 14:35:44 +0000767 if ((atom->type == XML_REGEXP_BLOCK_NAME) && (atom->valuep != NULL))
Daniel Veillard4255d502002-04-16 15:50:10 +0000768 xmlFree(atom->valuep);
769 xmlFree(atom);
770}
771
772static xmlRegStatePtr
773xmlRegNewState(xmlRegParserCtxtPtr ctxt) {
774 xmlRegStatePtr ret;
775
776 ret = (xmlRegStatePtr) xmlMalloc(sizeof(xmlRegState));
777 if (ret == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +0000778 xmlRegexpErrMemory(ctxt, "allocating state");
Daniel Veillard4255d502002-04-16 15:50:10 +0000779 return(NULL);
780 }
781 memset(ret, 0, sizeof(xmlRegState));
782 ret->type = XML_REGEXP_TRANS_STATE;
783 ret->mark = XML_REGEXP_MARK_NORMAL;
784 return(ret);
785}
786
787/**
788 * xmlRegFreeState:
789 * @state: the regexp state
790 *
791 * Free a regexp state
792 */
793static void
794xmlRegFreeState(xmlRegStatePtr state) {
795 if (state == NULL)
796 return;
797
798 if (state->trans != NULL)
799 xmlFree(state->trans);
Daniel Veillarddb68b742005-07-30 13:18:24 +0000800 if (state->transTo != NULL)
801 xmlFree(state->transTo);
Daniel Veillard4255d502002-04-16 15:50:10 +0000802 xmlFree(state);
803}
804
805/**
806 * xmlRegFreeParserCtxt:
807 * @ctxt: the regexp parser context
808 *
809 * Free a regexp parser context
810 */
811static void
812xmlRegFreeParserCtxt(xmlRegParserCtxtPtr ctxt) {
813 int i;
814 if (ctxt == NULL)
815 return;
816
817 if (ctxt->string != NULL)
818 xmlFree(ctxt->string);
819 if (ctxt->states != NULL) {
820 for (i = 0;i < ctxt->nbStates;i++)
821 xmlRegFreeState(ctxt->states[i]);
822 xmlFree(ctxt->states);
823 }
824 if (ctxt->atoms != NULL) {
825 for (i = 0;i < ctxt->nbAtoms;i++)
826 xmlRegFreeAtom(ctxt->atoms[i]);
827 xmlFree(ctxt->atoms);
828 }
829 if (ctxt->counters != NULL)
830 xmlFree(ctxt->counters);
831 xmlFree(ctxt);
832}
833
834/************************************************************************
835 * *
836 * Display of Data structures *
837 * *
838 ************************************************************************/
839
840static void
841xmlRegPrintAtomType(FILE *output, xmlRegAtomType type) {
842 switch (type) {
843 case XML_REGEXP_EPSILON:
844 fprintf(output, "epsilon "); break;
845 case XML_REGEXP_CHARVAL:
846 fprintf(output, "charval "); break;
847 case XML_REGEXP_RANGES:
848 fprintf(output, "ranges "); break;
849 case XML_REGEXP_SUBREG:
850 fprintf(output, "subexpr "); break;
851 case XML_REGEXP_STRING:
852 fprintf(output, "string "); break;
853 case XML_REGEXP_ANYCHAR:
854 fprintf(output, "anychar "); break;
855 case XML_REGEXP_ANYSPACE:
856 fprintf(output, "anyspace "); break;
857 case XML_REGEXP_NOTSPACE:
858 fprintf(output, "notspace "); break;
859 case XML_REGEXP_INITNAME:
860 fprintf(output, "initname "); break;
861 case XML_REGEXP_NOTINITNAME:
862 fprintf(output, "notinitname "); break;
863 case XML_REGEXP_NAMECHAR:
864 fprintf(output, "namechar "); break;
865 case XML_REGEXP_NOTNAMECHAR:
866 fprintf(output, "notnamechar "); break;
867 case XML_REGEXP_DECIMAL:
868 fprintf(output, "decimal "); break;
869 case XML_REGEXP_NOTDECIMAL:
870 fprintf(output, "notdecimal "); break;
871 case XML_REGEXP_REALCHAR:
872 fprintf(output, "realchar "); break;
873 case XML_REGEXP_NOTREALCHAR:
874 fprintf(output, "notrealchar "); break;
875 case XML_REGEXP_LETTER:
876 fprintf(output, "LETTER "); break;
877 case XML_REGEXP_LETTER_UPPERCASE:
878 fprintf(output, "LETTER_UPPERCASE "); break;
879 case XML_REGEXP_LETTER_LOWERCASE:
880 fprintf(output, "LETTER_LOWERCASE "); break;
881 case XML_REGEXP_LETTER_TITLECASE:
882 fprintf(output, "LETTER_TITLECASE "); break;
883 case XML_REGEXP_LETTER_MODIFIER:
884 fprintf(output, "LETTER_MODIFIER "); break;
885 case XML_REGEXP_LETTER_OTHERS:
886 fprintf(output, "LETTER_OTHERS "); break;
887 case XML_REGEXP_MARK:
888 fprintf(output, "MARK "); break;
889 case XML_REGEXP_MARK_NONSPACING:
890 fprintf(output, "MARK_NONSPACING "); break;
891 case XML_REGEXP_MARK_SPACECOMBINING:
892 fprintf(output, "MARK_SPACECOMBINING "); break;
893 case XML_REGEXP_MARK_ENCLOSING:
894 fprintf(output, "MARK_ENCLOSING "); break;
895 case XML_REGEXP_NUMBER:
896 fprintf(output, "NUMBER "); break;
897 case XML_REGEXP_NUMBER_DECIMAL:
898 fprintf(output, "NUMBER_DECIMAL "); break;
899 case XML_REGEXP_NUMBER_LETTER:
900 fprintf(output, "NUMBER_LETTER "); break;
901 case XML_REGEXP_NUMBER_OTHERS:
902 fprintf(output, "NUMBER_OTHERS "); break;
903 case XML_REGEXP_PUNCT:
904 fprintf(output, "PUNCT "); break;
905 case XML_REGEXP_PUNCT_CONNECTOR:
906 fprintf(output, "PUNCT_CONNECTOR "); break;
907 case XML_REGEXP_PUNCT_DASH:
908 fprintf(output, "PUNCT_DASH "); break;
909 case XML_REGEXP_PUNCT_OPEN:
910 fprintf(output, "PUNCT_OPEN "); break;
911 case XML_REGEXP_PUNCT_CLOSE:
912 fprintf(output, "PUNCT_CLOSE "); break;
913 case XML_REGEXP_PUNCT_INITQUOTE:
914 fprintf(output, "PUNCT_INITQUOTE "); break;
915 case XML_REGEXP_PUNCT_FINQUOTE:
916 fprintf(output, "PUNCT_FINQUOTE "); break;
917 case XML_REGEXP_PUNCT_OTHERS:
918 fprintf(output, "PUNCT_OTHERS "); break;
919 case XML_REGEXP_SEPAR:
920 fprintf(output, "SEPAR "); break;
921 case XML_REGEXP_SEPAR_SPACE:
922 fprintf(output, "SEPAR_SPACE "); break;
923 case XML_REGEXP_SEPAR_LINE:
924 fprintf(output, "SEPAR_LINE "); break;
925 case XML_REGEXP_SEPAR_PARA:
926 fprintf(output, "SEPAR_PARA "); break;
927 case XML_REGEXP_SYMBOL:
928 fprintf(output, "SYMBOL "); break;
929 case XML_REGEXP_SYMBOL_MATH:
930 fprintf(output, "SYMBOL_MATH "); break;
931 case XML_REGEXP_SYMBOL_CURRENCY:
932 fprintf(output, "SYMBOL_CURRENCY "); break;
933 case XML_REGEXP_SYMBOL_MODIFIER:
934 fprintf(output, "SYMBOL_MODIFIER "); break;
935 case XML_REGEXP_SYMBOL_OTHERS:
936 fprintf(output, "SYMBOL_OTHERS "); break;
937 case XML_REGEXP_OTHER:
938 fprintf(output, "OTHER "); break;
939 case XML_REGEXP_OTHER_CONTROL:
940 fprintf(output, "OTHER_CONTROL "); break;
941 case XML_REGEXP_OTHER_FORMAT:
942 fprintf(output, "OTHER_FORMAT "); break;
943 case XML_REGEXP_OTHER_PRIVATE:
944 fprintf(output, "OTHER_PRIVATE "); break;
945 case XML_REGEXP_OTHER_NA:
946 fprintf(output, "OTHER_NA "); break;
947 case XML_REGEXP_BLOCK_NAME:
948 fprintf(output, "BLOCK "); break;
949 }
950}
951
952static void
953xmlRegPrintQuantType(FILE *output, xmlRegQuantType type) {
954 switch (type) {
955 case XML_REGEXP_QUANT_EPSILON:
956 fprintf(output, "epsilon "); break;
957 case XML_REGEXP_QUANT_ONCE:
958 fprintf(output, "once "); break;
959 case XML_REGEXP_QUANT_OPT:
960 fprintf(output, "? "); break;
961 case XML_REGEXP_QUANT_MULT:
962 fprintf(output, "* "); break;
963 case XML_REGEXP_QUANT_PLUS:
964 fprintf(output, "+ "); break;
965 case XML_REGEXP_QUANT_RANGE:
966 fprintf(output, "range "); break;
Daniel Veillard7646b182002-04-20 06:41:40 +0000967 case XML_REGEXP_QUANT_ONCEONLY:
968 fprintf(output, "onceonly "); break;
969 case XML_REGEXP_QUANT_ALL:
970 fprintf(output, "all "); break;
Daniel Veillard4255d502002-04-16 15:50:10 +0000971 }
972}
973static void
974xmlRegPrintRange(FILE *output, xmlRegRangePtr range) {
975 fprintf(output, " range: ");
976 if (range->neg)
977 fprintf(output, "negative ");
978 xmlRegPrintAtomType(output, range->type);
979 fprintf(output, "%c - %c\n", range->start, range->end);
980}
981
982static void
983xmlRegPrintAtom(FILE *output, xmlRegAtomPtr atom) {
984 fprintf(output, " atom: ");
985 if (atom == NULL) {
986 fprintf(output, "NULL\n");
987 return;
988 }
Daniel Veillard9efc4762005-07-19 14:33:55 +0000989 if (atom->neg)
990 fprintf(output, "not ");
Daniel Veillard4255d502002-04-16 15:50:10 +0000991 xmlRegPrintAtomType(output, atom->type);
992 xmlRegPrintQuantType(output, atom->quant);
993 if (atom->quant == XML_REGEXP_QUANT_RANGE)
994 fprintf(output, "%d-%d ", atom->min, atom->max);
995 if (atom->type == XML_REGEXP_STRING)
996 fprintf(output, "'%s' ", (char *) atom->valuep);
997 if (atom->type == XML_REGEXP_CHARVAL)
998 fprintf(output, "char %c\n", atom->codepoint);
999 else if (atom->type == XML_REGEXP_RANGES) {
1000 int i;
1001 fprintf(output, "%d entries\n", atom->nbRanges);
1002 for (i = 0; i < atom->nbRanges;i++)
1003 xmlRegPrintRange(output, atom->ranges[i]);
1004 } else if (atom->type == XML_REGEXP_SUBREG) {
1005 fprintf(output, "start %d end %d\n", atom->start->no, atom->stop->no);
1006 } else {
1007 fprintf(output, "\n");
1008 }
1009}
1010
1011static void
1012xmlRegPrintTrans(FILE *output, xmlRegTransPtr trans) {
1013 fprintf(output, " trans: ");
1014 if (trans == NULL) {
1015 fprintf(output, "NULL\n");
1016 return;
1017 }
1018 if (trans->to < 0) {
1019 fprintf(output, "removed\n");
1020 return;
1021 }
1022 if (trans->counter >= 0) {
1023 fprintf(output, "counted %d, ", trans->counter);
1024 }
Daniel Veillard8a001f62002-04-20 07:24:11 +00001025 if (trans->count == REGEXP_ALL_COUNTER) {
1026 fprintf(output, "all transition, ");
1027 } else if (trans->count >= 0) {
Daniel Veillard4255d502002-04-16 15:50:10 +00001028 fprintf(output, "count based %d, ", trans->count);
1029 }
1030 if (trans->atom == NULL) {
1031 fprintf(output, "epsilon to %d\n", trans->to);
1032 return;
1033 }
1034 if (trans->atom->type == XML_REGEXP_CHARVAL)
1035 fprintf(output, "char %c ", trans->atom->codepoint);
1036 fprintf(output, "atom %d, to %d\n", trans->atom->no, trans->to);
1037}
1038
1039static void
1040xmlRegPrintState(FILE *output, xmlRegStatePtr state) {
1041 int i;
1042
1043 fprintf(output, " state: ");
1044 if (state == NULL) {
1045 fprintf(output, "NULL\n");
1046 return;
1047 }
1048 if (state->type == XML_REGEXP_START_STATE)
1049 fprintf(output, "START ");
1050 if (state->type == XML_REGEXP_FINAL_STATE)
1051 fprintf(output, "FINAL ");
1052
1053 fprintf(output, "%d, %d transitions:\n", state->no, state->nbTrans);
1054 for (i = 0;i < state->nbTrans; i++) {
1055 xmlRegPrintTrans(output, &(state->trans[i]));
1056 }
1057}
1058
Daniel Veillard23e73572002-09-19 19:56:43 +00001059#ifdef DEBUG_REGEXP_GRAPH
Daniel Veillard4255d502002-04-16 15:50:10 +00001060static void
1061xmlRegPrintCtxt(FILE *output, xmlRegParserCtxtPtr ctxt) {
1062 int i;
1063
1064 fprintf(output, " ctxt: ");
1065 if (ctxt == NULL) {
1066 fprintf(output, "NULL\n");
1067 return;
1068 }
1069 fprintf(output, "'%s' ", ctxt->string);
1070 if (ctxt->error)
1071 fprintf(output, "error ");
1072 if (ctxt->neg)
1073 fprintf(output, "neg ");
1074 fprintf(output, "\n");
1075 fprintf(output, "%d atoms:\n", ctxt->nbAtoms);
1076 for (i = 0;i < ctxt->nbAtoms; i++) {
1077 fprintf(output, " %02d ", i);
1078 xmlRegPrintAtom(output, ctxt->atoms[i]);
1079 }
1080 if (ctxt->atom != NULL) {
1081 fprintf(output, "current atom:\n");
1082 xmlRegPrintAtom(output, ctxt->atom);
1083 }
1084 fprintf(output, "%d states:", ctxt->nbStates);
1085 if (ctxt->start != NULL)
1086 fprintf(output, " start: %d", ctxt->start->no);
1087 if (ctxt->end != NULL)
1088 fprintf(output, " end: %d", ctxt->end->no);
1089 fprintf(output, "\n");
1090 for (i = 0;i < ctxt->nbStates; i++) {
1091 xmlRegPrintState(output, ctxt->states[i]);
1092 }
1093 fprintf(output, "%d counters:\n", ctxt->nbCounters);
1094 for (i = 0;i < ctxt->nbCounters; i++) {
1095 fprintf(output, " %d: min %d max %d\n", i, ctxt->counters[i].min,
1096 ctxt->counters[i].max);
1097 }
1098}
Daniel Veillard23e73572002-09-19 19:56:43 +00001099#endif
Daniel Veillard4255d502002-04-16 15:50:10 +00001100
1101/************************************************************************
1102 * *
1103 * Finite Automata structures manipulations *
1104 * *
1105 ************************************************************************/
1106
1107static void
1108xmlRegAtomAddRange(xmlRegParserCtxtPtr ctxt, xmlRegAtomPtr atom,
1109 int neg, xmlRegAtomType type, int start, int end,
1110 xmlChar *blockName) {
1111 xmlRegRangePtr range;
1112
1113 if (atom == NULL) {
1114 ERROR("add range: atom is NULL");
1115 return;
1116 }
1117 if (atom->type != XML_REGEXP_RANGES) {
1118 ERROR("add range: atom is not ranges");
1119 return;
1120 }
1121 if (atom->maxRanges == 0) {
1122 atom->maxRanges = 4;
1123 atom->ranges = (xmlRegRangePtr *) xmlMalloc(atom->maxRanges *
1124 sizeof(xmlRegRangePtr));
1125 if (atom->ranges == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00001126 xmlRegexpErrMemory(ctxt, "adding ranges");
Daniel Veillard4255d502002-04-16 15:50:10 +00001127 atom->maxRanges = 0;
1128 return;
1129 }
1130 } else if (atom->nbRanges >= atom->maxRanges) {
1131 xmlRegRangePtr *tmp;
1132 atom->maxRanges *= 2;
1133 tmp = (xmlRegRangePtr *) xmlRealloc(atom->ranges, atom->maxRanges *
1134 sizeof(xmlRegRangePtr));
1135 if (tmp == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00001136 xmlRegexpErrMemory(ctxt, "adding ranges");
Daniel Veillard4255d502002-04-16 15:50:10 +00001137 atom->maxRanges /= 2;
1138 return;
1139 }
1140 atom->ranges = tmp;
1141 }
1142 range = xmlRegNewRange(ctxt, neg, type, start, end);
1143 if (range == NULL)
1144 return;
1145 range->blockName = blockName;
1146 atom->ranges[atom->nbRanges++] = range;
1147
1148}
1149
1150static int
1151xmlRegGetCounter(xmlRegParserCtxtPtr ctxt) {
1152 if (ctxt->maxCounters == 0) {
1153 ctxt->maxCounters = 4;
1154 ctxt->counters = (xmlRegCounter *) xmlMalloc(ctxt->maxCounters *
1155 sizeof(xmlRegCounter));
1156 if (ctxt->counters == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00001157 xmlRegexpErrMemory(ctxt, "allocating counter");
Daniel Veillard4255d502002-04-16 15:50:10 +00001158 ctxt->maxCounters = 0;
1159 return(-1);
1160 }
1161 } else if (ctxt->nbCounters >= ctxt->maxCounters) {
1162 xmlRegCounter *tmp;
1163 ctxt->maxCounters *= 2;
1164 tmp = (xmlRegCounter *) xmlRealloc(ctxt->counters, ctxt->maxCounters *
1165 sizeof(xmlRegCounter));
1166 if (tmp == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00001167 xmlRegexpErrMemory(ctxt, "allocating counter");
Daniel Veillard4255d502002-04-16 15:50:10 +00001168 ctxt->maxCounters /= 2;
1169 return(-1);
1170 }
1171 ctxt->counters = tmp;
1172 }
1173 ctxt->counters[ctxt->nbCounters].min = -1;
1174 ctxt->counters[ctxt->nbCounters].max = -1;
1175 return(ctxt->nbCounters++);
1176}
1177
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001178static int
Daniel Veillard4255d502002-04-16 15:50:10 +00001179xmlRegAtomPush(xmlRegParserCtxtPtr ctxt, xmlRegAtomPtr atom) {
1180 if (atom == NULL) {
1181 ERROR("atom push: atom is NULL");
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001182 return(-1);
Daniel Veillard4255d502002-04-16 15:50:10 +00001183 }
1184 if (ctxt->maxAtoms == 0) {
1185 ctxt->maxAtoms = 4;
1186 ctxt->atoms = (xmlRegAtomPtr *) xmlMalloc(ctxt->maxAtoms *
1187 sizeof(xmlRegAtomPtr));
1188 if (ctxt->atoms == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00001189 xmlRegexpErrMemory(ctxt, "pushing atom");
Daniel Veillard4255d502002-04-16 15:50:10 +00001190 ctxt->maxAtoms = 0;
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001191 return(-1);
Daniel Veillard4255d502002-04-16 15:50:10 +00001192 }
1193 } else if (ctxt->nbAtoms >= ctxt->maxAtoms) {
1194 xmlRegAtomPtr *tmp;
1195 ctxt->maxAtoms *= 2;
1196 tmp = (xmlRegAtomPtr *) xmlRealloc(ctxt->atoms, ctxt->maxAtoms *
1197 sizeof(xmlRegAtomPtr));
1198 if (tmp == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00001199 xmlRegexpErrMemory(ctxt, "allocating counter");
Daniel Veillard4255d502002-04-16 15:50:10 +00001200 ctxt->maxAtoms /= 2;
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001201 return(-1);
Daniel Veillard4255d502002-04-16 15:50:10 +00001202 }
1203 ctxt->atoms = tmp;
1204 }
1205 atom->no = ctxt->nbAtoms;
1206 ctxt->atoms[ctxt->nbAtoms++] = atom;
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001207 return(0);
Daniel Veillard4255d502002-04-16 15:50:10 +00001208}
1209
1210static void
Daniel Veillarddb68b742005-07-30 13:18:24 +00001211xmlRegStateAddTransTo(xmlRegParserCtxtPtr ctxt, xmlRegStatePtr target,
1212 int from) {
1213 if (target->maxTransTo == 0) {
1214 target->maxTransTo = 8;
1215 target->transTo = (int *) xmlMalloc(target->maxTransTo *
1216 sizeof(int));
1217 if (target->transTo == NULL) {
1218 xmlRegexpErrMemory(ctxt, "adding transition");
1219 target->maxTransTo = 0;
1220 return;
1221 }
1222 } else if (target->nbTransTo >= target->maxTransTo) {
1223 int *tmp;
1224 target->maxTransTo *= 2;
1225 tmp = (int *) xmlRealloc(target->transTo, target->maxTransTo *
1226 sizeof(int));
1227 if (tmp == NULL) {
1228 xmlRegexpErrMemory(ctxt, "adding transition");
1229 target->maxTransTo /= 2;
1230 return;
1231 }
1232 target->transTo = tmp;
1233 }
1234 target->transTo[target->nbTransTo] = from;
1235 target->nbTransTo++;
1236}
1237
1238static void
Daniel Veillard4255d502002-04-16 15:50:10 +00001239xmlRegStateAddTrans(xmlRegParserCtxtPtr ctxt, xmlRegStatePtr state,
1240 xmlRegAtomPtr atom, xmlRegStatePtr target,
Daniel Veillard5de09382005-09-26 17:18:17 +00001241 int counter, int count) {
William M. Brackf9b5fa22004-05-10 07:52:15 +00001242
1243 int nrtrans;
1244
Daniel Veillard4255d502002-04-16 15:50:10 +00001245 if (state == NULL) {
1246 ERROR("add state: state is NULL");
1247 return;
1248 }
1249 if (target == NULL) {
1250 ERROR("add state: target is NULL");
1251 return;
1252 }
William M. Brackf9b5fa22004-05-10 07:52:15 +00001253 /*
1254 * Other routines follow the philosophy 'When in doubt, add a transition'
1255 * so we check here whether such a transition is already present and, if
1256 * so, silently ignore this request.
1257 */
1258
Daniel Veillard5de09382005-09-26 17:18:17 +00001259 for (nrtrans = state->nbTrans - 1; nrtrans >= 0; nrtrans--) {
1260 xmlRegTransPtr trans = &(state->trans[nrtrans]);
1261 if ((trans->atom == atom) &&
1262 (trans->to == target->no) &&
1263 (trans->counter == counter) &&
1264 (trans->count == count)) {
William M. Brackf9b5fa22004-05-10 07:52:15 +00001265#ifdef DEBUG_REGEXP_GRAPH
Daniel Veillard5de09382005-09-26 17:18:17 +00001266 printf("Ignoring duplicate transition from %d to %d\n",
1267 state->no, target->no);
William M. Brackf9b5fa22004-05-10 07:52:15 +00001268#endif
Daniel Veillard5de09382005-09-26 17:18:17 +00001269 return;
Daniel Veillarddb68b742005-07-30 13:18:24 +00001270 }
William M. Brackf9b5fa22004-05-10 07:52:15 +00001271 }
1272
Daniel Veillard4255d502002-04-16 15:50:10 +00001273 if (state->maxTrans == 0) {
Daniel Veillarddb68b742005-07-30 13:18:24 +00001274 state->maxTrans = 8;
Daniel Veillard4255d502002-04-16 15:50:10 +00001275 state->trans = (xmlRegTrans *) xmlMalloc(state->maxTrans *
1276 sizeof(xmlRegTrans));
1277 if (state->trans == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00001278 xmlRegexpErrMemory(ctxt, "adding transition");
Daniel Veillard4255d502002-04-16 15:50:10 +00001279 state->maxTrans = 0;
1280 return;
1281 }
1282 } else if (state->nbTrans >= state->maxTrans) {
1283 xmlRegTrans *tmp;
1284 state->maxTrans *= 2;
1285 tmp = (xmlRegTrans *) xmlRealloc(state->trans, state->maxTrans *
1286 sizeof(xmlRegTrans));
1287 if (tmp == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00001288 xmlRegexpErrMemory(ctxt, "adding transition");
Daniel Veillard4255d502002-04-16 15:50:10 +00001289 state->maxTrans /= 2;
1290 return;
1291 }
1292 state->trans = tmp;
1293 }
1294#ifdef DEBUG_REGEXP_GRAPH
1295 printf("Add trans from %d to %d ", state->no, target->no);
Daniel Veillard8a001f62002-04-20 07:24:11 +00001296 if (count == REGEXP_ALL_COUNTER)
Daniel Veillard2cbf5962004-03-31 15:50:43 +00001297 printf("all transition\n");
Daniel Veillard4402ab42002-09-12 16:02:56 +00001298 else if (count >= 0)
Daniel Veillard2cbf5962004-03-31 15:50:43 +00001299 printf("count based %d\n", count);
Daniel Veillard4255d502002-04-16 15:50:10 +00001300 else if (counter >= 0)
Daniel Veillard2cbf5962004-03-31 15:50:43 +00001301 printf("counted %d\n", counter);
Daniel Veillard4255d502002-04-16 15:50:10 +00001302 else if (atom == NULL)
Daniel Veillard2cbf5962004-03-31 15:50:43 +00001303 printf("epsilon transition\n");
1304 else if (atom != NULL)
1305 xmlRegPrintAtom(stdout, atom);
Daniel Veillard4255d502002-04-16 15:50:10 +00001306#endif
1307
1308 state->trans[state->nbTrans].atom = atom;
1309 state->trans[state->nbTrans].to = target->no;
1310 state->trans[state->nbTrans].counter = counter;
1311 state->trans[state->nbTrans].count = count;
1312 state->nbTrans++;
Daniel Veillarddb68b742005-07-30 13:18:24 +00001313 xmlRegStateAddTransTo(ctxt, target, state->no);
Daniel Veillard4255d502002-04-16 15:50:10 +00001314}
1315
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001316static int
Daniel Veillard4255d502002-04-16 15:50:10 +00001317xmlRegStatePush(xmlRegParserCtxtPtr ctxt, xmlRegStatePtr state) {
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001318 if (state == NULL) return(-1);
Daniel Veillard4255d502002-04-16 15:50:10 +00001319 if (ctxt->maxStates == 0) {
1320 ctxt->maxStates = 4;
1321 ctxt->states = (xmlRegStatePtr *) xmlMalloc(ctxt->maxStates *
1322 sizeof(xmlRegStatePtr));
1323 if (ctxt->states == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00001324 xmlRegexpErrMemory(ctxt, "adding state");
Daniel Veillard4255d502002-04-16 15:50:10 +00001325 ctxt->maxStates = 0;
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001326 return(-1);
Daniel Veillard4255d502002-04-16 15:50:10 +00001327 }
1328 } else if (ctxt->nbStates >= ctxt->maxStates) {
1329 xmlRegStatePtr *tmp;
1330 ctxt->maxStates *= 2;
1331 tmp = (xmlRegStatePtr *) xmlRealloc(ctxt->states, ctxt->maxStates *
1332 sizeof(xmlRegStatePtr));
1333 if (tmp == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00001334 xmlRegexpErrMemory(ctxt, "adding state");
Daniel Veillard4255d502002-04-16 15:50:10 +00001335 ctxt->maxStates /= 2;
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001336 return(-1);
Daniel Veillard4255d502002-04-16 15:50:10 +00001337 }
1338 ctxt->states = tmp;
1339 }
1340 state->no = ctxt->nbStates;
1341 ctxt->states[ctxt->nbStates++] = state;
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001342 return(0);
Daniel Veillard4255d502002-04-16 15:50:10 +00001343}
1344
1345/**
Daniel Veillard7646b182002-04-20 06:41:40 +00001346 * xmlFAGenerateAllTransition:
Daniel Veillard441bc322002-04-20 17:38:48 +00001347 * @ctxt: a regexp parser context
1348 * @from: the from state
1349 * @to: the target state or NULL for building a new one
1350 * @lax:
Daniel Veillard7646b182002-04-20 06:41:40 +00001351 *
1352 */
1353static void
1354xmlFAGenerateAllTransition(xmlRegParserCtxtPtr ctxt,
Daniel Veillard441bc322002-04-20 17:38:48 +00001355 xmlRegStatePtr from, xmlRegStatePtr to,
1356 int lax) {
Daniel Veillard7646b182002-04-20 06:41:40 +00001357 if (to == NULL) {
1358 to = xmlRegNewState(ctxt);
1359 xmlRegStatePush(ctxt, to);
1360 ctxt->state = to;
1361 }
Daniel Veillard441bc322002-04-20 17:38:48 +00001362 if (lax)
Daniel Veillard5de09382005-09-26 17:18:17 +00001363 xmlRegStateAddTrans(ctxt, from, NULL, to, -1, REGEXP_ALL_LAX_COUNTER);
Daniel Veillard441bc322002-04-20 17:38:48 +00001364 else
Daniel Veillard5de09382005-09-26 17:18:17 +00001365 xmlRegStateAddTrans(ctxt, from, NULL, to, -1, REGEXP_ALL_COUNTER);
Daniel Veillard7646b182002-04-20 06:41:40 +00001366}
1367
1368/**
Daniel Veillard4255d502002-04-16 15:50:10 +00001369 * xmlFAGenerateEpsilonTransition:
Daniel Veillard441bc322002-04-20 17:38:48 +00001370 * @ctxt: a regexp parser context
1371 * @from: the from state
1372 * @to: the target state or NULL for building a new one
Daniel Veillard4255d502002-04-16 15:50:10 +00001373 *
1374 */
1375static void
1376xmlFAGenerateEpsilonTransition(xmlRegParserCtxtPtr ctxt,
1377 xmlRegStatePtr from, xmlRegStatePtr to) {
1378 if (to == NULL) {
1379 to = xmlRegNewState(ctxt);
1380 xmlRegStatePush(ctxt, to);
1381 ctxt->state = to;
1382 }
Daniel Veillard5de09382005-09-26 17:18:17 +00001383 xmlRegStateAddTrans(ctxt, from, NULL, to, -1, -1);
Daniel Veillard4255d502002-04-16 15:50:10 +00001384}
1385
1386/**
1387 * xmlFAGenerateCountedEpsilonTransition:
Daniel Veillard441bc322002-04-20 17:38:48 +00001388 * @ctxt: a regexp parser context
1389 * @from: the from state
1390 * @to: the target state or NULL for building a new one
Daniel Veillard4255d502002-04-16 15:50:10 +00001391 * counter: the counter for that transition
1392 *
1393 */
1394static void
1395xmlFAGenerateCountedEpsilonTransition(xmlRegParserCtxtPtr ctxt,
1396 xmlRegStatePtr from, xmlRegStatePtr to, int counter) {
1397 if (to == NULL) {
1398 to = xmlRegNewState(ctxt);
1399 xmlRegStatePush(ctxt, to);
1400 ctxt->state = to;
1401 }
Daniel Veillard5de09382005-09-26 17:18:17 +00001402 xmlRegStateAddTrans(ctxt, from, NULL, to, counter, -1);
Daniel Veillard4255d502002-04-16 15:50:10 +00001403}
1404
1405/**
1406 * xmlFAGenerateCountedTransition:
Daniel Veillard441bc322002-04-20 17:38:48 +00001407 * @ctxt: a regexp parser context
1408 * @from: the from state
1409 * @to: the target state or NULL for building a new one
Daniel Veillard4255d502002-04-16 15:50:10 +00001410 * counter: the counter for that transition
1411 *
1412 */
1413static void
1414xmlFAGenerateCountedTransition(xmlRegParserCtxtPtr ctxt,
1415 xmlRegStatePtr from, xmlRegStatePtr to, int counter) {
1416 if (to == NULL) {
1417 to = xmlRegNewState(ctxt);
1418 xmlRegStatePush(ctxt, to);
1419 ctxt->state = to;
1420 }
Daniel Veillard5de09382005-09-26 17:18:17 +00001421 xmlRegStateAddTrans(ctxt, from, NULL, to, -1, counter);
Daniel Veillard4255d502002-04-16 15:50:10 +00001422}
1423
1424/**
1425 * xmlFAGenerateTransitions:
Daniel Veillard441bc322002-04-20 17:38:48 +00001426 * @ctxt: a regexp parser context
1427 * @from: the from state
1428 * @to: the target state or NULL for building a new one
1429 * @atom: the atom generating the transition
Daniel Veillard4255d502002-04-16 15:50:10 +00001430 *
William M. Brackddf71d62004-05-06 04:17:26 +00001431 * Returns 0 if success and -1 in case of error.
Daniel Veillard4255d502002-04-16 15:50:10 +00001432 */
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001433static int
Daniel Veillard4255d502002-04-16 15:50:10 +00001434xmlFAGenerateTransitions(xmlRegParserCtxtPtr ctxt, xmlRegStatePtr from,
1435 xmlRegStatePtr to, xmlRegAtomPtr atom) {
1436 if (atom == NULL) {
1437 ERROR("genrate transition: atom == NULL");
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001438 return(-1);
Daniel Veillard4255d502002-04-16 15:50:10 +00001439 }
1440 if (atom->type == XML_REGEXP_SUBREG) {
1441 /*
1442 * this is a subexpression handling one should not need to
William M. Brackddf71d62004-05-06 04:17:26 +00001443 * create a new node except for XML_REGEXP_QUANT_RANGE.
Daniel Veillard4255d502002-04-16 15:50:10 +00001444 */
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001445 if (xmlRegAtomPush(ctxt, atom) < 0) {
1446 return(-1);
1447 }
Daniel Veillard4255d502002-04-16 15:50:10 +00001448 if ((to != NULL) && (atom->stop != to) &&
1449 (atom->quant != XML_REGEXP_QUANT_RANGE)) {
1450 /*
1451 * Generate an epsilon transition to link to the target
1452 */
1453 xmlFAGenerateEpsilonTransition(ctxt, atom->stop, to);
1454 }
1455 switch (atom->quant) {
1456 case XML_REGEXP_QUANT_OPT:
1457 atom->quant = XML_REGEXP_QUANT_ONCE;
1458 xmlFAGenerateEpsilonTransition(ctxt, atom->start, atom->stop);
1459 break;
1460 case XML_REGEXP_QUANT_MULT:
1461 atom->quant = XML_REGEXP_QUANT_ONCE;
1462 xmlFAGenerateEpsilonTransition(ctxt, atom->start, atom->stop);
1463 xmlFAGenerateEpsilonTransition(ctxt, atom->stop, atom->start);
1464 break;
1465 case XML_REGEXP_QUANT_PLUS:
1466 atom->quant = XML_REGEXP_QUANT_ONCE;
1467 xmlFAGenerateEpsilonTransition(ctxt, atom->stop, atom->start);
1468 break;
1469 case XML_REGEXP_QUANT_RANGE: {
1470 int counter;
1471 xmlRegStatePtr newstate;
1472
1473 /*
1474 * This one is nasty:
William M. Brackddf71d62004-05-06 04:17:26 +00001475 * 1/ if range has minOccurs == 0, create a new state
1476 * and create epsilon transitions from atom->start
1477 * to atom->stop, as well as atom->start to the new
1478 * state
1479 * 2/ register a new counter
1480 * 3/ register an epsilon transition associated to
Daniel Veillard4255d502002-04-16 15:50:10 +00001481 * this counter going from atom->stop to atom->start
William M. Brackddf71d62004-05-06 04:17:26 +00001482 * 4/ create a new state
1483 * 5/ generate a counted transition from atom->stop to
Daniel Veillard4255d502002-04-16 15:50:10 +00001484 * that state
1485 */
William M. Brackddf71d62004-05-06 04:17:26 +00001486 if (atom->min == 0) {
1487 xmlFAGenerateEpsilonTransition(ctxt, atom->start,
1488 atom->stop);
1489 newstate = xmlRegNewState(ctxt);
1490 xmlRegStatePush(ctxt, newstate);
1491 ctxt->state = newstate;
1492 xmlFAGenerateEpsilonTransition(ctxt, atom->start,
1493 newstate);
1494 }
Daniel Veillard4255d502002-04-16 15:50:10 +00001495 counter = xmlRegGetCounter(ctxt);
1496 ctxt->counters[counter].min = atom->min - 1;
1497 ctxt->counters[counter].max = atom->max - 1;
1498 atom->min = 0;
1499 atom->max = 0;
1500 atom->quant = XML_REGEXP_QUANT_ONCE;
1501 xmlFAGenerateCountedEpsilonTransition(ctxt, atom->stop,
1502 atom->start, counter);
1503 if (to != NULL) {
1504 newstate = to;
1505 } else {
1506 newstate = xmlRegNewState(ctxt);
1507 xmlRegStatePush(ctxt, newstate);
1508 ctxt->state = newstate;
1509 }
1510 xmlFAGenerateCountedTransition(ctxt, atom->stop,
1511 newstate, counter);
1512 }
1513 default:
1514 break;
1515 }
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001516 return(0);
Daniel Veillard99c394d2005-07-14 12:58:49 +00001517 } else if ((atom->min == 0) && (atom->max == 0) &&
1518 (atom->quant == XML_REGEXP_QUANT_RANGE)) {
1519 /*
1520 * we can discard the atom and generate an epsilon transition instead
1521 */
1522 if (to == NULL) {
1523 to = xmlRegNewState(ctxt);
1524 if (to != NULL)
1525 xmlRegStatePush(ctxt, to);
1526 else {
1527 return(-1);
1528 }
1529 }
1530 xmlFAGenerateEpsilonTransition(ctxt, from, to);
1531 ctxt->state = to;
1532 xmlRegFreeAtom(atom);
1533 return(0);
Daniel Veillard4255d502002-04-16 15:50:10 +00001534 } else {
1535 if (to == NULL) {
1536 to = xmlRegNewState(ctxt);
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001537 if (to != NULL)
1538 xmlRegStatePush(ctxt, to);
1539 else {
1540 return(-1);
1541 }
1542 }
1543 if (xmlRegAtomPush(ctxt, atom) < 0) {
1544 return(-1);
Daniel Veillard4255d502002-04-16 15:50:10 +00001545 }
Daniel Veillard5de09382005-09-26 17:18:17 +00001546 xmlRegStateAddTrans(ctxt, from, atom, to, -1, -1);
Daniel Veillard4255d502002-04-16 15:50:10 +00001547 ctxt->state = to;
1548 }
1549 switch (atom->quant) {
1550 case XML_REGEXP_QUANT_OPT:
1551 atom->quant = XML_REGEXP_QUANT_ONCE;
1552 xmlFAGenerateEpsilonTransition(ctxt, from, to);
1553 break;
1554 case XML_REGEXP_QUANT_MULT:
1555 atom->quant = XML_REGEXP_QUANT_ONCE;
1556 xmlFAGenerateEpsilonTransition(ctxt, from, to);
Daniel Veillard5de09382005-09-26 17:18:17 +00001557 xmlRegStateAddTrans(ctxt, to, atom, to, -1, -1);
Daniel Veillard4255d502002-04-16 15:50:10 +00001558 break;
1559 case XML_REGEXP_QUANT_PLUS:
1560 atom->quant = XML_REGEXP_QUANT_ONCE;
Daniel Veillard5de09382005-09-26 17:18:17 +00001561 xmlRegStateAddTrans(ctxt, to, atom, to, -1, -1);
Daniel Veillard4255d502002-04-16 15:50:10 +00001562 break;
1563 default:
1564 break;
1565 }
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001566 return(0);
Daniel Veillard4255d502002-04-16 15:50:10 +00001567}
1568
1569/**
1570 * xmlFAReduceEpsilonTransitions:
Daniel Veillard441bc322002-04-20 17:38:48 +00001571 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00001572 * @fromnr: the from state
1573 * @tonr: the to state
William M. Brackddf71d62004-05-06 04:17:26 +00001574 * @counter: should that transition be associated to a counted
Daniel Veillard4255d502002-04-16 15:50:10 +00001575 *
1576 */
1577static void
1578xmlFAReduceEpsilonTransitions(xmlRegParserCtxtPtr ctxt, int fromnr,
1579 int tonr, int counter) {
1580 int transnr;
1581 xmlRegStatePtr from;
1582 xmlRegStatePtr to;
1583
1584#ifdef DEBUG_REGEXP_GRAPH
1585 printf("xmlFAReduceEpsilonTransitions(%d, %d)\n", fromnr, tonr);
1586#endif
1587 from = ctxt->states[fromnr];
1588 if (from == NULL)
1589 return;
1590 to = ctxt->states[tonr];
1591 if (to == NULL)
1592 return;
1593 if ((to->mark == XML_REGEXP_MARK_START) ||
1594 (to->mark == XML_REGEXP_MARK_VISITED))
1595 return;
1596
1597 to->mark = XML_REGEXP_MARK_VISITED;
1598 if (to->type == XML_REGEXP_FINAL_STATE) {
1599#ifdef DEBUG_REGEXP_GRAPH
1600 printf("State %d is final, so %d becomes final\n", tonr, fromnr);
1601#endif
1602 from->type = XML_REGEXP_FINAL_STATE;
1603 }
1604 for (transnr = 0;transnr < to->nbTrans;transnr++) {
Daniel Veillarddb68b742005-07-30 13:18:24 +00001605 if (to->trans[transnr].to < 0)
1606 continue;
Daniel Veillard4255d502002-04-16 15:50:10 +00001607 if (to->trans[transnr].atom == NULL) {
1608 /*
1609 * Don't remove counted transitions
1610 * Don't loop either
1611 */
Daniel Veillardb509f152002-04-17 16:28:10 +00001612 if (to->trans[transnr].to != fromnr) {
1613 if (to->trans[transnr].count >= 0) {
1614 int newto = to->trans[transnr].to;
1615
1616 xmlRegStateAddTrans(ctxt, from, NULL,
1617 ctxt->states[newto],
Daniel Veillard5de09382005-09-26 17:18:17 +00001618 -1, to->trans[transnr].count);
Daniel Veillardb509f152002-04-17 16:28:10 +00001619 } else {
Daniel Veillard4255d502002-04-16 15:50:10 +00001620#ifdef DEBUG_REGEXP_GRAPH
Daniel Veillardb509f152002-04-17 16:28:10 +00001621 printf("Found epsilon trans %d from %d to %d\n",
1622 transnr, tonr, to->trans[transnr].to);
Daniel Veillard4255d502002-04-16 15:50:10 +00001623#endif
Daniel Veillardb509f152002-04-17 16:28:10 +00001624 if (to->trans[transnr].counter >= 0) {
1625 xmlFAReduceEpsilonTransitions(ctxt, fromnr,
1626 to->trans[transnr].to,
1627 to->trans[transnr].counter);
1628 } else {
1629 xmlFAReduceEpsilonTransitions(ctxt, fromnr,
1630 to->trans[transnr].to,
1631 counter);
1632 }
1633 }
Daniel Veillard4255d502002-04-16 15:50:10 +00001634 }
1635 } else {
1636 int newto = to->trans[transnr].to;
1637
Daniel Veillardb509f152002-04-17 16:28:10 +00001638 if (to->trans[transnr].counter >= 0) {
1639 xmlRegStateAddTrans(ctxt, from, to->trans[transnr].atom,
1640 ctxt->states[newto],
Daniel Veillard5de09382005-09-26 17:18:17 +00001641 to->trans[transnr].counter, -1);
Daniel Veillardb509f152002-04-17 16:28:10 +00001642 } else {
1643 xmlRegStateAddTrans(ctxt, from, to->trans[transnr].atom,
Daniel Veillard5de09382005-09-26 17:18:17 +00001644 ctxt->states[newto], counter, -1);
Daniel Veillardb509f152002-04-17 16:28:10 +00001645 }
Daniel Veillard4255d502002-04-16 15:50:10 +00001646 }
1647 }
1648 to->mark = XML_REGEXP_MARK_NORMAL;
1649}
1650
1651/**
Daniel Veillarddb68b742005-07-30 13:18:24 +00001652 * xmlFAEliminateSimpleEpsilonTransitions:
1653 * @ctxt: a regexp parser context
1654 *
1655 * Eliminating general epsilon transitions can get costly in the general
1656 * algorithm due to the large amount of generated new transitions and
1657 * associated comparisons. However for simple epsilon transition used just
1658 * to separate building blocks when generating the automata this can be
1659 * reduced to state elimination:
1660 * - if there exists an epsilon from X to Y
1661 * - if there is no other transition from X
1662 * then X and Y are semantically equivalent and X can be eliminated
1663 * If X is the start state then make Y the start state, else replace the
1664 * target of all transitions to X by transitions to Y.
1665 */
1666static void
1667xmlFAEliminateSimpleEpsilonTransitions(xmlRegParserCtxtPtr ctxt) {
1668 int statenr, i, j, newto;
1669 xmlRegStatePtr state, tmp;
1670
1671 for (statenr = 0;statenr < ctxt->nbStates;statenr++) {
1672 state = ctxt->states[statenr];
1673 if (state == NULL)
1674 continue;
1675 if (state->nbTrans != 1)
1676 continue;
1677 /* is the only transition out a basic transition */
1678 if ((state->trans[0].atom == NULL) &&
1679 (state->trans[0].to >= 0) &&
1680 (state->trans[0].to != statenr) &&
1681 (state->trans[0].counter < 0) &&
1682 (state->trans[0].count < 0)) {
1683 newto = state->trans[0].to;
1684
1685 if (state->type == XML_REGEXP_START_STATE) {
1686#ifdef DEBUG_REGEXP_GRAPH
1687 printf("Found simple epsilon trans from start %d to %d\n",
1688 statenr, newto);
1689#endif
1690 } else {
1691#ifdef DEBUG_REGEXP_GRAPH
1692 printf("Found simple epsilon trans from %d to %d\n",
1693 statenr, newto);
1694#endif
1695 for (i = 0;i < state->nbTransTo;i++) {
1696 tmp = ctxt->states[state->transTo[i]];
1697 for (j = 0;j < tmp->nbTrans;j++) {
1698 if (tmp->trans[j].to == statenr) {
1699 tmp->trans[j].to = newto;
1700#ifdef DEBUG_REGEXP_GRAPH
1701 printf("Changed transition %d on %d to go to %d\n",
1702 j, tmp->no, newto);
1703#endif
1704 xmlRegStateAddTransTo(ctxt, ctxt->states[newto],
1705 tmp->no);
1706 }
1707 }
1708 }
1709#if 0
1710 for (i = 0;i < ctxt->nbStates;i++) {
1711 tmp = ctxt->states[i];
1712 for (j = 0;j < tmp->nbTrans;j++) {
1713 if (tmp->trans[j].to == statenr) {
1714 tmp->trans[j].to = newto;
1715#ifdef DEBUG_REGEXP_GRAPH
1716 printf("Changed transition %d on %d to go to %d\n",
1717 j, tmp->no, newto);
1718#endif
1719 }
1720 }
1721 }
1722#endif
1723 if (state->type == XML_REGEXP_FINAL_STATE)
1724 ctxt->states[newto]->type = XML_REGEXP_FINAL_STATE;
1725 /* eliminate the transition completely */
1726 state->nbTrans = 0;
1727
1728
1729 }
1730
1731 }
1732 }
1733}
1734/**
Daniel Veillard4255d502002-04-16 15:50:10 +00001735 * xmlFAEliminateEpsilonTransitions:
Daniel Veillard441bc322002-04-20 17:38:48 +00001736 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00001737 *
1738 */
1739static void
1740xmlFAEliminateEpsilonTransitions(xmlRegParserCtxtPtr ctxt) {
1741 int statenr, transnr;
1742 xmlRegStatePtr state;
Daniel Veillarddb68b742005-07-30 13:18:24 +00001743 int has_epsilon;
Daniel Veillard4255d502002-04-16 15:50:10 +00001744
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001745 if (ctxt->states == NULL) return;
1746
Daniel Veillarddb68b742005-07-30 13:18:24 +00001747 xmlFAEliminateSimpleEpsilonTransitions(ctxt);
1748
1749 has_epsilon = 0;
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001750
Daniel Veillard4255d502002-04-16 15:50:10 +00001751 /*
1752 * build the completed transitions bypassing the epsilons
1753 * Use a marking algorithm to avoid loops
Daniel Veillardcc026dc2005-01-12 13:21:17 +00001754 * mark sink states too.
Daniel Veillard4255d502002-04-16 15:50:10 +00001755 */
1756 for (statenr = 0;statenr < ctxt->nbStates;statenr++) {
1757 state = ctxt->states[statenr];
1758 if (state == NULL)
1759 continue;
Daniel Veillardcc026dc2005-01-12 13:21:17 +00001760 if ((state->nbTrans == 0) &&
1761 (state->type != XML_REGEXP_FINAL_STATE)) {
1762 state->type = XML_REGEXP_SINK_STATE;
1763 }
Daniel Veillard4255d502002-04-16 15:50:10 +00001764 for (transnr = 0;transnr < state->nbTrans;transnr++) {
1765 if ((state->trans[transnr].atom == NULL) &&
1766 (state->trans[transnr].to >= 0)) {
1767 if (state->trans[transnr].to == statenr) {
1768 state->trans[transnr].to = -1;
1769#ifdef DEBUG_REGEXP_GRAPH
1770 printf("Removed loopback epsilon trans %d on %d\n",
1771 transnr, statenr);
1772#endif
1773 } else if (state->trans[transnr].count < 0) {
1774 int newto = state->trans[transnr].to;
1775
1776#ifdef DEBUG_REGEXP_GRAPH
1777 printf("Found epsilon trans %d from %d to %d\n",
1778 transnr, statenr, newto);
1779#endif
1780 state->mark = XML_REGEXP_MARK_START;
Daniel Veillarddb68b742005-07-30 13:18:24 +00001781 has_epsilon = 1;
Daniel Veillard4255d502002-04-16 15:50:10 +00001782 xmlFAReduceEpsilonTransitions(ctxt, statenr,
1783 newto, state->trans[transnr].counter);
1784 state->mark = XML_REGEXP_MARK_NORMAL;
1785#ifdef DEBUG_REGEXP_GRAPH
1786 } else {
1787 printf("Found counted transition %d on %d\n",
1788 transnr, statenr);
1789#endif
1790 }
1791 }
1792 }
1793 }
1794 /*
1795 * Eliminate the epsilon transitions
1796 */
Daniel Veillarddb68b742005-07-30 13:18:24 +00001797 if (has_epsilon) {
1798 for (statenr = 0;statenr < ctxt->nbStates;statenr++) {
1799 state = ctxt->states[statenr];
1800 if (state == NULL)
1801 continue;
1802 for (transnr = 0;transnr < state->nbTrans;transnr++) {
1803 xmlRegTransPtr trans = &(state->trans[transnr]);
1804 if ((trans->atom == NULL) &&
1805 (trans->count < 0) &&
1806 (trans->to >= 0)) {
1807 trans->to = -1;
1808 }
Daniel Veillard4255d502002-04-16 15:50:10 +00001809 }
1810 }
1811 }
Daniel Veillard23e73572002-09-19 19:56:43 +00001812
1813 /*
1814 * Use this pass to detect unreachable states too
1815 */
1816 for (statenr = 0;statenr < ctxt->nbStates;statenr++) {
1817 state = ctxt->states[statenr];
1818 if (state != NULL)
William M. Brack779af002003-08-01 15:55:39 +00001819 state->reached = XML_REGEXP_MARK_NORMAL;
Daniel Veillard23e73572002-09-19 19:56:43 +00001820 }
1821 state = ctxt->states[0];
1822 if (state != NULL)
William M. Brack779af002003-08-01 15:55:39 +00001823 state->reached = XML_REGEXP_MARK_START;
Daniel Veillard23e73572002-09-19 19:56:43 +00001824 while (state != NULL) {
1825 xmlRegStatePtr target = NULL;
William M. Brack779af002003-08-01 15:55:39 +00001826 state->reached = XML_REGEXP_MARK_VISITED;
Daniel Veillard23e73572002-09-19 19:56:43 +00001827 /*
William M. Brackddf71d62004-05-06 04:17:26 +00001828 * Mark all states reachable from the current reachable state
Daniel Veillard23e73572002-09-19 19:56:43 +00001829 */
1830 for (transnr = 0;transnr < state->nbTrans;transnr++) {
1831 if ((state->trans[transnr].to >= 0) &&
1832 ((state->trans[transnr].atom != NULL) ||
1833 (state->trans[transnr].count >= 0))) {
1834 int newto = state->trans[transnr].to;
1835
1836 if (ctxt->states[newto] == NULL)
1837 continue;
William M. Brack779af002003-08-01 15:55:39 +00001838 if (ctxt->states[newto]->reached == XML_REGEXP_MARK_NORMAL) {
1839 ctxt->states[newto]->reached = XML_REGEXP_MARK_START;
Daniel Veillard23e73572002-09-19 19:56:43 +00001840 target = ctxt->states[newto];
1841 }
1842 }
1843 }
Daniel Veillardcc026dc2005-01-12 13:21:17 +00001844
Daniel Veillard23e73572002-09-19 19:56:43 +00001845 /*
1846 * find the next accessible state not explored
1847 */
1848 if (target == NULL) {
1849 for (statenr = 1;statenr < ctxt->nbStates;statenr++) {
1850 state = ctxt->states[statenr];
William M. Brack779af002003-08-01 15:55:39 +00001851 if ((state != NULL) && (state->reached ==
1852 XML_REGEXP_MARK_START)) {
Daniel Veillard23e73572002-09-19 19:56:43 +00001853 target = state;
1854 break;
1855 }
1856 }
1857 }
1858 state = target;
1859 }
1860 for (statenr = 0;statenr < ctxt->nbStates;statenr++) {
1861 state = ctxt->states[statenr];
William M. Brack779af002003-08-01 15:55:39 +00001862 if ((state != NULL) && (state->reached == XML_REGEXP_MARK_NORMAL)) {
Daniel Veillard23e73572002-09-19 19:56:43 +00001863#ifdef DEBUG_REGEXP_GRAPH
1864 printf("Removed unreachable state %d\n", statenr);
1865#endif
1866 xmlRegFreeState(state);
1867 ctxt->states[statenr] = NULL;
1868 }
1869 }
1870
Daniel Veillard4255d502002-04-16 15:50:10 +00001871}
1872
Daniel Veillarde19fc232002-04-22 16:01:24 +00001873/**
1874 * xmlFACompareAtoms:
1875 * @atom1: an atom
1876 * @atom2: an atom
1877 *
William M. Brackddf71d62004-05-06 04:17:26 +00001878 * Compares two atoms to check whether they are equivalents
Daniel Veillarde19fc232002-04-22 16:01:24 +00001879 *
1880 * Returns 1 if yes and 0 otherwise
1881 */
1882static int
1883xmlFACompareAtoms(xmlRegAtomPtr atom1, xmlRegAtomPtr atom2) {
Daniel Veillard9efc4762005-07-19 14:33:55 +00001884 int ret;
1885
Daniel Veillarde19fc232002-04-22 16:01:24 +00001886 if (atom1 == atom2)
1887 return(1);
1888 if ((atom1 == NULL) || (atom2 == NULL))
1889 return(0);
1890
1891 if (atom1->type != atom2->type)
1892 return(0);
1893 switch (atom1->type) {
1894 case XML_REGEXP_STRING:
Daniel Veillard9efc4762005-07-19 14:33:55 +00001895 ret = xmlRegStrEqualWildcard((xmlChar *)atom1->valuep,
1896 (xmlChar *)atom2->valuep);
1897 break;
Daniel Veillarde19fc232002-04-22 16:01:24 +00001898 case XML_REGEXP_EPSILON:
1899 return(1);
1900 case XML_REGEXP_CHARVAL:
Daniel Veillard9efc4762005-07-19 14:33:55 +00001901 ret = atom1->codepoint == atom2->codepoint;
1902 break;
Daniel Veillarde19fc232002-04-22 16:01:24 +00001903 case XML_REGEXP_RANGES:
1904 TODO;
1905 return(0);
1906 default:
Daniel Veillard9efc4762005-07-19 14:33:55 +00001907 return(1);
Daniel Veillarde19fc232002-04-22 16:01:24 +00001908 }
Daniel Veillard6e65e152005-08-09 11:09:52 +00001909 if (atom1->neg != atom2->neg) {
Daniel Veillard9efc4762005-07-19 14:33:55 +00001910 ret = !ret;
Daniel Veillard6e65e152005-08-09 11:09:52 +00001911 }
Daniel Veillard9efc4762005-07-19 14:33:55 +00001912 return(ret);
Daniel Veillarde19fc232002-04-22 16:01:24 +00001913}
1914
1915/**
1916 * xmlFARecurseDeterminism:
1917 * @ctxt: a regexp parser context
1918 *
1919 * Check whether the associated regexp is determinist,
1920 * should be called after xmlFAEliminateEpsilonTransitions()
1921 *
1922 */
1923static int
1924xmlFARecurseDeterminism(xmlRegParserCtxtPtr ctxt, xmlRegStatePtr state,
1925 int to, xmlRegAtomPtr atom) {
1926 int ret = 1;
Daniel Veillard5de09382005-09-26 17:18:17 +00001927 int transnr, nbTrans;
Daniel Veillarde19fc232002-04-22 16:01:24 +00001928 xmlRegTransPtr t1;
1929
1930 if (state == NULL)
1931 return(ret);
Daniel Veillard5de09382005-09-26 17:18:17 +00001932 /*
1933 * don't recurse on transitions potentially added in the course of
1934 * the elimination.
1935 */
1936 nbTrans = state->nbTrans;
1937 for (transnr = 0;transnr < nbTrans;transnr++) {
Daniel Veillarde19fc232002-04-22 16:01:24 +00001938 t1 = &(state->trans[transnr]);
1939 /*
1940 * check transitions conflicting with the one looked at
1941 */
1942 if (t1->atom == NULL) {
1943 if (t1->to == -1)
1944 continue;
1945 ret = xmlFARecurseDeterminism(ctxt, ctxt->states[t1->to],
1946 to, atom);
1947 if (ret == 0)
1948 return(0);
1949 continue;
1950 }
1951 if (t1->to != to)
1952 continue;
1953 if (xmlFACompareAtoms(t1->atom, atom))
1954 return(0);
1955 }
1956 return(ret);
1957}
1958
1959/**
1960 * xmlFAComputesDeterminism:
1961 * @ctxt: a regexp parser context
1962 *
1963 * Check whether the associated regexp is determinist,
1964 * should be called after xmlFAEliminateEpsilonTransitions()
1965 *
1966 */
1967static int
1968xmlFAComputesDeterminism(xmlRegParserCtxtPtr ctxt) {
1969 int statenr, transnr;
1970 xmlRegStatePtr state;
1971 xmlRegTransPtr t1, t2;
1972 int i;
1973 int ret = 1;
1974
Daniel Veillard4402ab42002-09-12 16:02:56 +00001975#ifdef DEBUG_REGEXP_GRAPH
1976 printf("xmlFAComputesDeterminism\n");
1977 xmlRegPrintCtxt(stdout, ctxt);
1978#endif
Daniel Veillarde19fc232002-04-22 16:01:24 +00001979 if (ctxt->determinist != -1)
1980 return(ctxt->determinist);
1981
1982 /*
William M. Brackddf71d62004-05-06 04:17:26 +00001983 * Check for all states that there aren't 2 transitions
Daniel Veillarde19fc232002-04-22 16:01:24 +00001984 * with the same atom and a different target.
1985 */
1986 for (statenr = 0;statenr < ctxt->nbStates;statenr++) {
1987 state = ctxt->states[statenr];
1988 if (state == NULL)
1989 continue;
Daniel Veillard4f82c8a2005-08-09 21:40:08 +00001990 if (state->nbTrans < 2)
1991 continue;
Daniel Veillarde19fc232002-04-22 16:01:24 +00001992 for (transnr = 0;transnr < state->nbTrans;transnr++) {
1993 t1 = &(state->trans[transnr]);
1994 /*
1995 * Determinism checks in case of counted or all transitions
1996 * will have to be handled separately
1997 */
1998 if (t1->atom == NULL)
1999 continue;
2000 if (t1->to == -1) /* eliminated */
2001 continue;
2002 for (i = 0;i < transnr;i++) {
2003 t2 = &(state->trans[i]);
2004 if (t2->to == -1) /* eliminated */
2005 continue;
2006 if (t2->atom != NULL) {
2007 if (t1->to == t2->to) {
2008 if (xmlFACompareAtoms(t1->atom, t2->atom))
William M. Brackddf71d62004-05-06 04:17:26 +00002009 t2->to = -1; /* eliminated */
Daniel Veillarde19fc232002-04-22 16:01:24 +00002010 } else {
2011 /* not determinist ! */
2012 if (xmlFACompareAtoms(t1->atom, t2->atom))
2013 ret = 0;
2014 }
2015 } else if (t1->to != -1) {
2016 /*
2017 * do the closure in case of remaining specific
2018 * epsilon transitions like choices or all
2019 */
2020 ret = xmlFARecurseDeterminism(ctxt, ctxt->states[t1->to],
2021 t2->to, t2->atom);
2022 if (ret == 0)
2023 return(0);
2024 }
2025 }
2026 if (ret == 0)
2027 break;
2028 }
2029 if (ret == 0)
2030 break;
2031 }
2032 ctxt->determinist = ret;
2033 return(ret);
2034}
2035
Daniel Veillard4255d502002-04-16 15:50:10 +00002036/************************************************************************
2037 * *
2038 * Routines to check input against transition atoms *
2039 * *
2040 ************************************************************************/
2041
2042static int
2043xmlRegCheckCharacterRange(xmlRegAtomType type, int codepoint, int neg,
2044 int start, int end, const xmlChar *blockName) {
2045 int ret = 0;
2046
2047 switch (type) {
2048 case XML_REGEXP_STRING:
2049 case XML_REGEXP_SUBREG:
2050 case XML_REGEXP_RANGES:
2051 case XML_REGEXP_EPSILON:
2052 return(-1);
2053 case XML_REGEXP_ANYCHAR:
2054 ret = ((codepoint != '\n') && (codepoint != '\r'));
2055 break;
2056 case XML_REGEXP_CHARVAL:
2057 ret = ((codepoint >= start) && (codepoint <= end));
2058 break;
2059 case XML_REGEXP_NOTSPACE:
2060 neg = !neg;
2061 case XML_REGEXP_ANYSPACE:
2062 ret = ((codepoint == '\n') || (codepoint == '\r') ||
2063 (codepoint == '\t') || (codepoint == ' '));
2064 break;
2065 case XML_REGEXP_NOTINITNAME:
2066 neg = !neg;
2067 case XML_REGEXP_INITNAME:
William M. Brack871611b2003-10-18 04:53:14 +00002068 ret = (IS_LETTER(codepoint) ||
Daniel Veillard4255d502002-04-16 15:50:10 +00002069 (codepoint == '_') || (codepoint == ':'));
2070 break;
2071 case XML_REGEXP_NOTNAMECHAR:
2072 neg = !neg;
2073 case XML_REGEXP_NAMECHAR:
William M. Brack871611b2003-10-18 04:53:14 +00002074 ret = (IS_LETTER(codepoint) || IS_DIGIT(codepoint) ||
Daniel Veillard4255d502002-04-16 15:50:10 +00002075 (codepoint == '.') || (codepoint == '-') ||
2076 (codepoint == '_') || (codepoint == ':') ||
William M. Brack871611b2003-10-18 04:53:14 +00002077 IS_COMBINING(codepoint) || IS_EXTENDER(codepoint));
Daniel Veillard4255d502002-04-16 15:50:10 +00002078 break;
2079 case XML_REGEXP_NOTDECIMAL:
2080 neg = !neg;
2081 case XML_REGEXP_DECIMAL:
2082 ret = xmlUCSIsCatNd(codepoint);
2083 break;
2084 case XML_REGEXP_REALCHAR:
2085 neg = !neg;
2086 case XML_REGEXP_NOTREALCHAR:
2087 ret = xmlUCSIsCatP(codepoint);
2088 if (ret == 0)
2089 ret = xmlUCSIsCatZ(codepoint);
2090 if (ret == 0)
2091 ret = xmlUCSIsCatC(codepoint);
2092 break;
2093 case XML_REGEXP_LETTER:
2094 ret = xmlUCSIsCatL(codepoint);
2095 break;
2096 case XML_REGEXP_LETTER_UPPERCASE:
2097 ret = xmlUCSIsCatLu(codepoint);
2098 break;
2099 case XML_REGEXP_LETTER_LOWERCASE:
2100 ret = xmlUCSIsCatLl(codepoint);
2101 break;
2102 case XML_REGEXP_LETTER_TITLECASE:
2103 ret = xmlUCSIsCatLt(codepoint);
2104 break;
2105 case XML_REGEXP_LETTER_MODIFIER:
2106 ret = xmlUCSIsCatLm(codepoint);
2107 break;
2108 case XML_REGEXP_LETTER_OTHERS:
2109 ret = xmlUCSIsCatLo(codepoint);
2110 break;
2111 case XML_REGEXP_MARK:
2112 ret = xmlUCSIsCatM(codepoint);
2113 break;
2114 case XML_REGEXP_MARK_NONSPACING:
2115 ret = xmlUCSIsCatMn(codepoint);
2116 break;
2117 case XML_REGEXP_MARK_SPACECOMBINING:
2118 ret = xmlUCSIsCatMc(codepoint);
2119 break;
2120 case XML_REGEXP_MARK_ENCLOSING:
2121 ret = xmlUCSIsCatMe(codepoint);
2122 break;
2123 case XML_REGEXP_NUMBER:
2124 ret = xmlUCSIsCatN(codepoint);
2125 break;
2126 case XML_REGEXP_NUMBER_DECIMAL:
2127 ret = xmlUCSIsCatNd(codepoint);
2128 break;
2129 case XML_REGEXP_NUMBER_LETTER:
2130 ret = xmlUCSIsCatNl(codepoint);
2131 break;
2132 case XML_REGEXP_NUMBER_OTHERS:
2133 ret = xmlUCSIsCatNo(codepoint);
2134 break;
2135 case XML_REGEXP_PUNCT:
2136 ret = xmlUCSIsCatP(codepoint);
2137 break;
2138 case XML_REGEXP_PUNCT_CONNECTOR:
2139 ret = xmlUCSIsCatPc(codepoint);
2140 break;
2141 case XML_REGEXP_PUNCT_DASH:
2142 ret = xmlUCSIsCatPd(codepoint);
2143 break;
2144 case XML_REGEXP_PUNCT_OPEN:
2145 ret = xmlUCSIsCatPs(codepoint);
2146 break;
2147 case XML_REGEXP_PUNCT_CLOSE:
2148 ret = xmlUCSIsCatPe(codepoint);
2149 break;
2150 case XML_REGEXP_PUNCT_INITQUOTE:
2151 ret = xmlUCSIsCatPi(codepoint);
2152 break;
2153 case XML_REGEXP_PUNCT_FINQUOTE:
2154 ret = xmlUCSIsCatPf(codepoint);
2155 break;
2156 case XML_REGEXP_PUNCT_OTHERS:
2157 ret = xmlUCSIsCatPo(codepoint);
2158 break;
2159 case XML_REGEXP_SEPAR:
2160 ret = xmlUCSIsCatZ(codepoint);
2161 break;
2162 case XML_REGEXP_SEPAR_SPACE:
2163 ret = xmlUCSIsCatZs(codepoint);
2164 break;
2165 case XML_REGEXP_SEPAR_LINE:
2166 ret = xmlUCSIsCatZl(codepoint);
2167 break;
2168 case XML_REGEXP_SEPAR_PARA:
2169 ret = xmlUCSIsCatZp(codepoint);
2170 break;
2171 case XML_REGEXP_SYMBOL:
2172 ret = xmlUCSIsCatS(codepoint);
2173 break;
2174 case XML_REGEXP_SYMBOL_MATH:
2175 ret = xmlUCSIsCatSm(codepoint);
2176 break;
2177 case XML_REGEXP_SYMBOL_CURRENCY:
2178 ret = xmlUCSIsCatSc(codepoint);
2179 break;
2180 case XML_REGEXP_SYMBOL_MODIFIER:
2181 ret = xmlUCSIsCatSk(codepoint);
2182 break;
2183 case XML_REGEXP_SYMBOL_OTHERS:
2184 ret = xmlUCSIsCatSo(codepoint);
2185 break;
2186 case XML_REGEXP_OTHER:
2187 ret = xmlUCSIsCatC(codepoint);
2188 break;
2189 case XML_REGEXP_OTHER_CONTROL:
2190 ret = xmlUCSIsCatCc(codepoint);
2191 break;
2192 case XML_REGEXP_OTHER_FORMAT:
2193 ret = xmlUCSIsCatCf(codepoint);
2194 break;
2195 case XML_REGEXP_OTHER_PRIVATE:
2196 ret = xmlUCSIsCatCo(codepoint);
2197 break;
2198 case XML_REGEXP_OTHER_NA:
2199 /* ret = xmlUCSIsCatCn(codepoint); */
2200 /* Seems it doesn't exist anymore in recent Unicode releases */
2201 ret = 0;
2202 break;
2203 case XML_REGEXP_BLOCK_NAME:
2204 ret = xmlUCSIsBlock(codepoint, (const char *) blockName);
2205 break;
2206 }
2207 if (neg)
2208 return(!ret);
2209 return(ret);
2210}
2211
2212static int
2213xmlRegCheckCharacter(xmlRegAtomPtr atom, int codepoint) {
2214 int i, ret = 0;
2215 xmlRegRangePtr range;
2216
William M. Brack871611b2003-10-18 04:53:14 +00002217 if ((atom == NULL) || (!IS_CHAR(codepoint)))
Daniel Veillard4255d502002-04-16 15:50:10 +00002218 return(-1);
2219
2220 switch (atom->type) {
2221 case XML_REGEXP_SUBREG:
2222 case XML_REGEXP_EPSILON:
2223 return(-1);
2224 case XML_REGEXP_CHARVAL:
2225 return(codepoint == atom->codepoint);
2226 case XML_REGEXP_RANGES: {
2227 int accept = 0;
Daniel Veillardf2a12832003-11-24 13:04:35 +00002228
Daniel Veillard4255d502002-04-16 15:50:10 +00002229 for (i = 0;i < atom->nbRanges;i++) {
2230 range = atom->ranges[i];
Daniel Veillardf8b9de32003-11-24 14:27:26 +00002231 if (range->neg == 2) {
Daniel Veillard4255d502002-04-16 15:50:10 +00002232 ret = xmlRegCheckCharacterRange(range->type, codepoint,
2233 0, range->start, range->end,
2234 range->blockName);
2235 if (ret != 0)
2236 return(0); /* excluded char */
Daniel Veillardf8b9de32003-11-24 14:27:26 +00002237 } else if (range->neg) {
2238 ret = xmlRegCheckCharacterRange(range->type, codepoint,
2239 0, range->start, range->end,
2240 range->blockName);
2241 if (ret == 0)
Daniel Veillardf2a12832003-11-24 13:04:35 +00002242 accept = 1;
Daniel Veillardf8b9de32003-11-24 14:27:26 +00002243 else
2244 return(0);
Daniel Veillard4255d502002-04-16 15:50:10 +00002245 } else {
2246 ret = xmlRegCheckCharacterRange(range->type, codepoint,
2247 0, range->start, range->end,
2248 range->blockName);
2249 if (ret != 0)
2250 accept = 1; /* might still be excluded */
2251 }
2252 }
2253 return(accept);
2254 }
2255 case XML_REGEXP_STRING:
2256 printf("TODO: XML_REGEXP_STRING\n");
2257 return(-1);
2258 case XML_REGEXP_ANYCHAR:
2259 case XML_REGEXP_ANYSPACE:
2260 case XML_REGEXP_NOTSPACE:
2261 case XML_REGEXP_INITNAME:
2262 case XML_REGEXP_NOTINITNAME:
2263 case XML_REGEXP_NAMECHAR:
2264 case XML_REGEXP_NOTNAMECHAR:
2265 case XML_REGEXP_DECIMAL:
2266 case XML_REGEXP_NOTDECIMAL:
2267 case XML_REGEXP_REALCHAR:
2268 case XML_REGEXP_NOTREALCHAR:
2269 case XML_REGEXP_LETTER:
2270 case XML_REGEXP_LETTER_UPPERCASE:
2271 case XML_REGEXP_LETTER_LOWERCASE:
2272 case XML_REGEXP_LETTER_TITLECASE:
2273 case XML_REGEXP_LETTER_MODIFIER:
2274 case XML_REGEXP_LETTER_OTHERS:
2275 case XML_REGEXP_MARK:
2276 case XML_REGEXP_MARK_NONSPACING:
2277 case XML_REGEXP_MARK_SPACECOMBINING:
2278 case XML_REGEXP_MARK_ENCLOSING:
2279 case XML_REGEXP_NUMBER:
2280 case XML_REGEXP_NUMBER_DECIMAL:
2281 case XML_REGEXP_NUMBER_LETTER:
2282 case XML_REGEXP_NUMBER_OTHERS:
2283 case XML_REGEXP_PUNCT:
2284 case XML_REGEXP_PUNCT_CONNECTOR:
2285 case XML_REGEXP_PUNCT_DASH:
2286 case XML_REGEXP_PUNCT_OPEN:
2287 case XML_REGEXP_PUNCT_CLOSE:
2288 case XML_REGEXP_PUNCT_INITQUOTE:
2289 case XML_REGEXP_PUNCT_FINQUOTE:
2290 case XML_REGEXP_PUNCT_OTHERS:
2291 case XML_REGEXP_SEPAR:
2292 case XML_REGEXP_SEPAR_SPACE:
2293 case XML_REGEXP_SEPAR_LINE:
2294 case XML_REGEXP_SEPAR_PARA:
2295 case XML_REGEXP_SYMBOL:
2296 case XML_REGEXP_SYMBOL_MATH:
2297 case XML_REGEXP_SYMBOL_CURRENCY:
2298 case XML_REGEXP_SYMBOL_MODIFIER:
2299 case XML_REGEXP_SYMBOL_OTHERS:
2300 case XML_REGEXP_OTHER:
2301 case XML_REGEXP_OTHER_CONTROL:
2302 case XML_REGEXP_OTHER_FORMAT:
2303 case XML_REGEXP_OTHER_PRIVATE:
2304 case XML_REGEXP_OTHER_NA:
2305 case XML_REGEXP_BLOCK_NAME:
2306 ret = xmlRegCheckCharacterRange(atom->type, codepoint, 0, 0, 0,
2307 (const xmlChar *)atom->valuep);
2308 if (atom->neg)
2309 ret = !ret;
2310 break;
2311 }
2312 return(ret);
2313}
2314
2315/************************************************************************
2316 * *
William M. Brackddf71d62004-05-06 04:17:26 +00002317 * Saving and restoring state of an execution context *
Daniel Veillard4255d502002-04-16 15:50:10 +00002318 * *
2319 ************************************************************************/
2320
2321#ifdef DEBUG_REGEXP_EXEC
2322static void
2323xmlFARegDebugExec(xmlRegExecCtxtPtr exec) {
2324 printf("state: %d:%d:idx %d", exec->state->no, exec->transno, exec->index);
2325 if (exec->inputStack != NULL) {
2326 int i;
2327 printf(": ");
2328 for (i = 0;(i < 3) && (i < exec->inputStackNr);i++)
2329 printf("%s ", exec->inputStack[exec->inputStackNr - (i + 1)]);
2330 } else {
2331 printf(": %s", &(exec->inputString[exec->index]));
2332 }
2333 printf("\n");
2334}
2335#endif
2336
2337static void
2338xmlFARegExecSave(xmlRegExecCtxtPtr exec) {
2339#ifdef DEBUG_REGEXP_EXEC
2340 printf("saving ");
2341 exec->transno++;
2342 xmlFARegDebugExec(exec);
2343 exec->transno--;
2344#endif
Daniel Veillard94cc1032005-09-15 13:09:00 +00002345#ifdef MAX_PUSH
2346 if (exec->nbPush > MAX_PUSH) {
2347 return;
2348 }
2349 exec->nbPush++;
2350#endif
Daniel Veillard4255d502002-04-16 15:50:10 +00002351
2352 if (exec->maxRollbacks == 0) {
2353 exec->maxRollbacks = 4;
2354 exec->rollbacks = (xmlRegExecRollback *) xmlMalloc(exec->maxRollbacks *
2355 sizeof(xmlRegExecRollback));
2356 if (exec->rollbacks == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00002357 xmlRegexpErrMemory(NULL, "saving regexp");
Daniel Veillard4255d502002-04-16 15:50:10 +00002358 exec->maxRollbacks = 0;
2359 return;
2360 }
2361 memset(exec->rollbacks, 0,
2362 exec->maxRollbacks * sizeof(xmlRegExecRollback));
2363 } else if (exec->nbRollbacks >= exec->maxRollbacks) {
2364 xmlRegExecRollback *tmp;
2365 int len = exec->maxRollbacks;
2366
2367 exec->maxRollbacks *= 2;
2368 tmp = (xmlRegExecRollback *) xmlRealloc(exec->rollbacks,
2369 exec->maxRollbacks * sizeof(xmlRegExecRollback));
2370 if (tmp == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00002371 xmlRegexpErrMemory(NULL, "saving regexp");
Daniel Veillard4255d502002-04-16 15:50:10 +00002372 exec->maxRollbacks /= 2;
2373 return;
2374 }
2375 exec->rollbacks = tmp;
2376 tmp = &exec->rollbacks[len];
2377 memset(tmp, 0, (exec->maxRollbacks - len) * sizeof(xmlRegExecRollback));
2378 }
2379 exec->rollbacks[exec->nbRollbacks].state = exec->state;
2380 exec->rollbacks[exec->nbRollbacks].index = exec->index;
2381 exec->rollbacks[exec->nbRollbacks].nextbranch = exec->transno + 1;
2382 if (exec->comp->nbCounters > 0) {
2383 if (exec->rollbacks[exec->nbRollbacks].counts == NULL) {
2384 exec->rollbacks[exec->nbRollbacks].counts = (int *)
2385 xmlMalloc(exec->comp->nbCounters * sizeof(int));
2386 if (exec->rollbacks[exec->nbRollbacks].counts == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00002387 xmlRegexpErrMemory(NULL, "saving regexp");
Daniel Veillard4255d502002-04-16 15:50:10 +00002388 exec->status = -5;
2389 return;
2390 }
2391 }
2392 memcpy(exec->rollbacks[exec->nbRollbacks].counts, exec->counts,
2393 exec->comp->nbCounters * sizeof(int));
2394 }
2395 exec->nbRollbacks++;
2396}
2397
2398static void
2399xmlFARegExecRollBack(xmlRegExecCtxtPtr exec) {
2400 if (exec->nbRollbacks <= 0) {
2401 exec->status = -1;
2402#ifdef DEBUG_REGEXP_EXEC
2403 printf("rollback failed on empty stack\n");
2404#endif
2405 return;
2406 }
2407 exec->nbRollbacks--;
2408 exec->state = exec->rollbacks[exec->nbRollbacks].state;
2409 exec->index = exec->rollbacks[exec->nbRollbacks].index;
2410 exec->transno = exec->rollbacks[exec->nbRollbacks].nextbranch;
2411 if (exec->comp->nbCounters > 0) {
2412 if (exec->rollbacks[exec->nbRollbacks].counts == NULL) {
2413 fprintf(stderr, "exec save: allocation failed");
2414 exec->status = -6;
2415 return;
2416 }
2417 memcpy(exec->counts, exec->rollbacks[exec->nbRollbacks].counts,
2418 exec->comp->nbCounters * sizeof(int));
2419 }
2420
2421#ifdef DEBUG_REGEXP_EXEC
2422 printf("restored ");
2423 xmlFARegDebugExec(exec);
2424#endif
2425}
2426
2427/************************************************************************
2428 * *
William M. Brackddf71d62004-05-06 04:17:26 +00002429 * Verifier, running an input against a compiled regexp *
Daniel Veillard4255d502002-04-16 15:50:10 +00002430 * *
2431 ************************************************************************/
2432
2433static int
2434xmlFARegExec(xmlRegexpPtr comp, const xmlChar *content) {
2435 xmlRegExecCtxt execval;
2436 xmlRegExecCtxtPtr exec = &execval;
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00002437 int ret, codepoint = 0, len;
Daniel Veillard4255d502002-04-16 15:50:10 +00002438
2439 exec->inputString = content;
2440 exec->index = 0;
Daniel Veillard94cc1032005-09-15 13:09:00 +00002441 exec->nbPush = 0;
Daniel Veillard4255d502002-04-16 15:50:10 +00002442 exec->determinist = 1;
2443 exec->maxRollbacks = 0;
2444 exec->nbRollbacks = 0;
2445 exec->rollbacks = NULL;
2446 exec->status = 0;
2447 exec->comp = comp;
2448 exec->state = comp->states[0];
2449 exec->transno = 0;
2450 exec->transcount = 0;
Daniel Veillardf2a12832003-11-24 13:04:35 +00002451 exec->inputStack = NULL;
2452 exec->inputStackMax = 0;
Daniel Veillard4255d502002-04-16 15:50:10 +00002453 if (comp->nbCounters > 0) {
2454 exec->counts = (int *) xmlMalloc(comp->nbCounters * sizeof(int));
Daniel Veillardff46a042003-10-08 08:53:17 +00002455 if (exec->counts == NULL) {
2456 xmlRegexpErrMemory(NULL, "running regexp");
Daniel Veillard4255d502002-04-16 15:50:10 +00002457 return(-1);
Daniel Veillardff46a042003-10-08 08:53:17 +00002458 }
Daniel Veillard4255d502002-04-16 15:50:10 +00002459 memset(exec->counts, 0, comp->nbCounters * sizeof(int));
2460 } else
2461 exec->counts = NULL;
2462 while ((exec->status == 0) &&
2463 ((exec->inputString[exec->index] != 0) ||
2464 (exec->state->type != XML_REGEXP_FINAL_STATE))) {
2465 xmlRegTransPtr trans;
2466 xmlRegAtomPtr atom;
2467
2468 /*
William M. Brack0e00b282004-04-26 15:40:47 +00002469 * If end of input on non-terminal state, rollback, however we may
Daniel Veillard4255d502002-04-16 15:50:10 +00002470 * still have epsilon like transition for counted transitions
William M. Brack0e00b282004-04-26 15:40:47 +00002471 * on counters, in that case don't break too early. Additionally,
2472 * if we are working on a range like "AB{0,2}", where B is not present,
2473 * we don't want to break.
Daniel Veillard4255d502002-04-16 15:50:10 +00002474 */
William M. Brack0e00b282004-04-26 15:40:47 +00002475 if ((exec->inputString[exec->index] == 0) && (exec->counts == NULL)) {
William M. Brackddf71d62004-05-06 04:17:26 +00002476 /*
2477 * if there is a transition, we must check if
2478 * atom allows minOccurs of 0
2479 */
2480 if (exec->transno < exec->state->nbTrans) {
William M. Brack0e00b282004-04-26 15:40:47 +00002481 trans = &exec->state->trans[exec->transno];
2482 if (trans->to >=0) {
2483 atom = trans->atom;
2484 if (!((atom->min == 0) && (atom->max > 0)))
2485 goto rollback;
2486 }
2487 } else
2488 goto rollback;
2489 }
Daniel Veillard4255d502002-04-16 15:50:10 +00002490
2491 exec->transcount = 0;
2492 for (;exec->transno < exec->state->nbTrans;exec->transno++) {
2493 trans = &exec->state->trans[exec->transno];
2494 if (trans->to < 0)
2495 continue;
2496 atom = trans->atom;
2497 ret = 0;
2498 if (trans->count >= 0) {
2499 int count;
2500 xmlRegCounterPtr counter;
2501
2502 /*
2503 * A counted transition.
2504 */
2505
2506 count = exec->counts[trans->count];
2507 counter = &exec->comp->counters[trans->count];
2508#ifdef DEBUG_REGEXP_EXEC
2509 printf("testing count %d: val %d, min %d, max %d\n",
2510 trans->count, count, counter->min, counter->max);
2511#endif
2512 ret = ((count >= counter->min) && (count <= counter->max));
2513 } else if (atom == NULL) {
2514 fprintf(stderr, "epsilon transition left at runtime\n");
2515 exec->status = -2;
2516 break;
2517 } else if (exec->inputString[exec->index] != 0) {
2518 codepoint = CUR_SCHAR(&(exec->inputString[exec->index]), len);
2519 ret = xmlRegCheckCharacter(atom, codepoint);
William M. Brack0e00b282004-04-26 15:40:47 +00002520 if ((ret == 1) && (atom->min >= 0) && (atom->max > 0)) {
Daniel Veillard4255d502002-04-16 15:50:10 +00002521 xmlRegStatePtr to = comp->states[trans->to];
2522
2523 /*
2524 * this is a multiple input sequence
2525 */
2526 if (exec->state->nbTrans > exec->transno + 1) {
2527 xmlFARegExecSave(exec);
2528 }
2529 exec->transcount = 1;
2530 do {
2531 /*
2532 * Try to progress as much as possible on the input
2533 */
2534 if (exec->transcount == atom->max) {
2535 break;
2536 }
2537 exec->index += len;
2538 /*
2539 * End of input: stop here
2540 */
2541 if (exec->inputString[exec->index] == 0) {
2542 exec->index -= len;
2543 break;
2544 }
2545 if (exec->transcount >= atom->min) {
2546 int transno = exec->transno;
2547 xmlRegStatePtr state = exec->state;
2548
2549 /*
2550 * The transition is acceptable save it
2551 */
2552 exec->transno = -1; /* trick */
2553 exec->state = to;
2554 xmlFARegExecSave(exec);
2555 exec->transno = transno;
2556 exec->state = state;
2557 }
2558 codepoint = CUR_SCHAR(&(exec->inputString[exec->index]),
2559 len);
2560 ret = xmlRegCheckCharacter(atom, codepoint);
2561 exec->transcount++;
2562 } while (ret == 1);
2563 if (exec->transcount < atom->min)
2564 ret = 0;
2565
2566 /*
2567 * If the last check failed but one transition was found
2568 * possible, rollback
2569 */
2570 if (ret < 0)
2571 ret = 0;
2572 if (ret == 0) {
2573 goto rollback;
2574 }
William M. Brack0e00b282004-04-26 15:40:47 +00002575 } else if ((ret == 0) && (atom->min == 0) && (atom->max > 0)) {
2576 /*
2577 * we don't match on the codepoint, but minOccurs of 0
2578 * says that's ok. Setting len to 0 inhibits stepping
2579 * over the codepoint.
2580 */
2581 exec->transcount = 1;
2582 len = 0;
2583 ret = 1;
Daniel Veillard4255d502002-04-16 15:50:10 +00002584 }
William M. Brack0e00b282004-04-26 15:40:47 +00002585 } else if ((atom->min == 0) && (atom->max > 0)) {
2586 /* another spot to match when minOccurs is 0 */
2587 exec->transcount = 1;
2588 len = 0;
2589 ret = 1;
Daniel Veillard4255d502002-04-16 15:50:10 +00002590 }
2591 if (ret == 1) {
2592 if (exec->state->nbTrans > exec->transno + 1) {
2593 xmlFARegExecSave(exec);
2594 }
2595 if (trans->counter >= 0) {
2596#ifdef DEBUG_REGEXP_EXEC
2597 printf("Increasing count %d\n", trans->counter);
2598#endif
2599 exec->counts[trans->counter]++;
2600 }
Daniel Veillard10752282005-08-08 13:05:13 +00002601 if ((trans->count >= 0) &&
2602 (trans->count < REGEXP_ALL_COUNTER)) {
2603#ifdef DEBUG_REGEXP_EXEC
2604 printf("resetting count %d on transition\n",
2605 trans->count);
2606#endif
2607 exec->counts[trans->count] = 0;
2608 }
Daniel Veillard4255d502002-04-16 15:50:10 +00002609#ifdef DEBUG_REGEXP_EXEC
2610 printf("entering state %d\n", trans->to);
2611#endif
2612 exec->state = comp->states[trans->to];
2613 exec->transno = 0;
2614 if (trans->atom != NULL) {
2615 exec->index += len;
2616 }
2617 goto progress;
2618 } else if (ret < 0) {
2619 exec->status = -4;
2620 break;
2621 }
2622 }
2623 if ((exec->transno != 0) || (exec->state->nbTrans == 0)) {
2624rollback:
2625 /*
2626 * Failed to find a way out
2627 */
2628 exec->determinist = 0;
2629 xmlFARegExecRollBack(exec);
2630 }
2631progress:
2632 continue;
2633 }
2634 if (exec->rollbacks != NULL) {
2635 if (exec->counts != NULL) {
2636 int i;
2637
2638 for (i = 0;i < exec->maxRollbacks;i++)
2639 if (exec->rollbacks[i].counts != NULL)
2640 xmlFree(exec->rollbacks[i].counts);
2641 }
2642 xmlFree(exec->rollbacks);
2643 }
2644 if (exec->counts != NULL)
2645 xmlFree(exec->counts);
2646 if (exec->status == 0)
2647 return(1);
Daniel Veillard94cc1032005-09-15 13:09:00 +00002648 if (exec->status == -1) {
2649 if (exec->nbPush > MAX_PUSH)
2650 return(-1);
Daniel Veillard4255d502002-04-16 15:50:10 +00002651 return(0);
Daniel Veillard94cc1032005-09-15 13:09:00 +00002652 }
Daniel Veillard4255d502002-04-16 15:50:10 +00002653 return(exec->status);
2654}
2655
2656/************************************************************************
2657 * *
William M. Brackddf71d62004-05-06 04:17:26 +00002658 * Progressive interface to the verifier one atom at a time *
Daniel Veillard4255d502002-04-16 15:50:10 +00002659 * *
2660 ************************************************************************/
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00002661#ifdef DEBUG_ERR
2662static void testerr(xmlRegExecCtxtPtr exec);
2663#endif
Daniel Veillard4255d502002-04-16 15:50:10 +00002664
2665/**
Daniel Veillard01c13b52002-12-10 15:19:08 +00002666 * xmlRegNewExecCtxt:
Daniel Veillard4255d502002-04-16 15:50:10 +00002667 * @comp: a precompiled regular expression
2668 * @callback: a callback function used for handling progresses in the
2669 * automata matching phase
2670 * @data: the context data associated to the callback in this context
2671 *
2672 * Build a context used for progressive evaluation of a regexp.
Daniel Veillard01c13b52002-12-10 15:19:08 +00002673 *
2674 * Returns the new context
Daniel Veillard4255d502002-04-16 15:50:10 +00002675 */
2676xmlRegExecCtxtPtr
2677xmlRegNewExecCtxt(xmlRegexpPtr comp, xmlRegExecCallbacks callback, void *data) {
2678 xmlRegExecCtxtPtr exec;
2679
2680 if (comp == NULL)
2681 return(NULL);
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00002682 if ((comp->compact == NULL) && (comp->states == NULL))
2683 return(NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +00002684 exec = (xmlRegExecCtxtPtr) xmlMalloc(sizeof(xmlRegExecCtxt));
2685 if (exec == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00002686 xmlRegexpErrMemory(NULL, "creating execution context");
Daniel Veillard4255d502002-04-16 15:50:10 +00002687 return(NULL);
2688 }
2689 memset(exec, 0, sizeof(xmlRegExecCtxt));
2690 exec->inputString = NULL;
2691 exec->index = 0;
2692 exec->determinist = 1;
2693 exec->maxRollbacks = 0;
2694 exec->nbRollbacks = 0;
2695 exec->rollbacks = NULL;
2696 exec->status = 0;
2697 exec->comp = comp;
Daniel Veillard23e73572002-09-19 19:56:43 +00002698 if (comp->compact == NULL)
2699 exec->state = comp->states[0];
Daniel Veillard4255d502002-04-16 15:50:10 +00002700 exec->transno = 0;
2701 exec->transcount = 0;
2702 exec->callback = callback;
2703 exec->data = data;
2704 if (comp->nbCounters > 0) {
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00002705 /*
2706 * For error handling, exec->counts is allocated twice the size
2707 * the second half is used to store the data in case of rollback
2708 */
2709 exec->counts = (int *) xmlMalloc(comp->nbCounters * sizeof(int)
2710 * 2);
Daniel Veillard4255d502002-04-16 15:50:10 +00002711 if (exec->counts == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00002712 xmlRegexpErrMemory(NULL, "creating execution context");
Daniel Veillard4255d502002-04-16 15:50:10 +00002713 xmlFree(exec);
2714 return(NULL);
2715 }
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00002716 memset(exec->counts, 0, comp->nbCounters * sizeof(int) * 2);
2717 exec->errCounts = &exec->counts[comp->nbCounters];
2718 } else {
Daniel Veillard4255d502002-04-16 15:50:10 +00002719 exec->counts = NULL;
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00002720 exec->errCounts = NULL;
2721 }
Daniel Veillard4255d502002-04-16 15:50:10 +00002722 exec->inputStackMax = 0;
2723 exec->inputStackNr = 0;
2724 exec->inputStack = NULL;
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00002725 exec->errStateNo = -1;
2726 exec->errString = NULL;
Daniel Veillard94cc1032005-09-15 13:09:00 +00002727 exec->nbPush = 0;
Daniel Veillard4255d502002-04-16 15:50:10 +00002728 return(exec);
2729}
2730
2731/**
2732 * xmlRegFreeExecCtxt:
2733 * @exec: a regular expression evaulation context
2734 *
2735 * Free the structures associated to a regular expression evaulation context.
2736 */
2737void
2738xmlRegFreeExecCtxt(xmlRegExecCtxtPtr exec) {
2739 if (exec == NULL)
2740 return;
2741
2742 if (exec->rollbacks != NULL) {
2743 if (exec->counts != NULL) {
2744 int i;
2745
2746 for (i = 0;i < exec->maxRollbacks;i++)
2747 if (exec->rollbacks[i].counts != NULL)
2748 xmlFree(exec->rollbacks[i].counts);
2749 }
2750 xmlFree(exec->rollbacks);
2751 }
2752 if (exec->counts != NULL)
2753 xmlFree(exec->counts);
2754 if (exec->inputStack != NULL) {
2755 int i;
2756
Daniel Veillard32370232002-10-16 14:08:14 +00002757 for (i = 0;i < exec->inputStackNr;i++) {
2758 if (exec->inputStack[i].value != NULL)
2759 xmlFree(exec->inputStack[i].value);
2760 }
Daniel Veillard4255d502002-04-16 15:50:10 +00002761 xmlFree(exec->inputStack);
2762 }
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00002763 if (exec->errString != NULL)
2764 xmlFree(exec->errString);
Daniel Veillard4255d502002-04-16 15:50:10 +00002765 xmlFree(exec);
2766}
2767
2768static void
2769xmlFARegExecSaveInputString(xmlRegExecCtxtPtr exec, const xmlChar *value,
2770 void *data) {
2771#ifdef DEBUG_PUSH
2772 printf("saving value: %d:%s\n", exec->inputStackNr, value);
2773#endif
2774 if (exec->inputStackMax == 0) {
2775 exec->inputStackMax = 4;
2776 exec->inputStack = (xmlRegInputTokenPtr)
2777 xmlMalloc(exec->inputStackMax * sizeof(xmlRegInputToken));
2778 if (exec->inputStack == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00002779 xmlRegexpErrMemory(NULL, "pushing input string");
Daniel Veillard4255d502002-04-16 15:50:10 +00002780 exec->inputStackMax = 0;
2781 return;
2782 }
2783 } else if (exec->inputStackNr + 1 >= exec->inputStackMax) {
2784 xmlRegInputTokenPtr tmp;
2785
2786 exec->inputStackMax *= 2;
2787 tmp = (xmlRegInputTokenPtr) xmlRealloc(exec->inputStack,
2788 exec->inputStackMax * sizeof(xmlRegInputToken));
2789 if (tmp == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00002790 xmlRegexpErrMemory(NULL, "pushing input string");
Daniel Veillard4255d502002-04-16 15:50:10 +00002791 exec->inputStackMax /= 2;
2792 return;
2793 }
2794 exec->inputStack = tmp;
2795 }
2796 exec->inputStack[exec->inputStackNr].value = xmlStrdup(value);
2797 exec->inputStack[exec->inputStackNr].data = data;
2798 exec->inputStackNr++;
2799 exec->inputStack[exec->inputStackNr].value = NULL;
2800 exec->inputStack[exec->inputStackNr].data = NULL;
2801}
2802
Daniel Veillardc0826a72004-08-10 14:17:33 +00002803/**
2804 * xmlRegStrEqualWildcard:
2805 * @expStr: the string to be evaluated
2806 * @valStr: the validation string
2807 *
2808 * Checks if both strings are equal or have the same content. "*"
2809 * can be used as a wildcard in @valStr; "|" is used as a seperator of
2810 * substrings in both @expStr and @valStr.
2811 *
2812 * Returns 1 if the comparison is satisfied and the number of substrings
2813 * is equal, 0 otherwise.
2814 */
2815
2816static int
2817xmlRegStrEqualWildcard(const xmlChar *expStr, const xmlChar *valStr) {
2818 if (expStr == valStr) return(1);
2819 if (expStr == NULL) return(0);
2820 if (valStr == NULL) return(0);
2821 do {
2822 /*
2823 * Eval if we have a wildcard for the current item.
2824 */
2825 if (*expStr != *valStr) {
Daniel Veillard4f82c8a2005-08-09 21:40:08 +00002826 /* if one of them starts with a wildcard make valStr be it */
2827 if (*valStr == '*') {
2828 const xmlChar *tmp;
2829
2830 tmp = valStr;
2831 valStr = expStr;
2832 expStr = tmp;
2833 }
Daniel Veillardc0826a72004-08-10 14:17:33 +00002834 if ((*valStr != 0) && (*expStr != 0) && (*expStr++ == '*')) {
2835 do {
2836 if (*valStr == XML_REG_STRING_SEPARATOR)
2837 break;
Kasimier T. Buchcikc0e833f2005-04-19 15:02:20 +00002838 valStr++;
Daniel Veillardc0826a72004-08-10 14:17:33 +00002839 } while (*valStr != 0);
2840 continue;
2841 } else
2842 return(0);
2843 }
Kasimier T. Buchcikc0e833f2005-04-19 15:02:20 +00002844 expStr++;
2845 valStr++;
Daniel Veillardc0826a72004-08-10 14:17:33 +00002846 } while (*valStr != 0);
2847 if (*expStr != 0)
2848 return (0);
2849 else
2850 return (1);
2851}
Daniel Veillard4255d502002-04-16 15:50:10 +00002852
2853/**
Daniel Veillard23e73572002-09-19 19:56:43 +00002854 * xmlRegCompactPushString:
2855 * @exec: a regexp execution context
2856 * @comp: the precompiled exec with a compact table
2857 * @value: a string token input
2858 * @data: data associated to the token to reuse in callbacks
2859 *
2860 * Push one input token in the execution context
2861 *
2862 * Returns: 1 if the regexp reached a final state, 0 if non-final, and
2863 * a negative value in case of error.
2864 */
2865static int
2866xmlRegCompactPushString(xmlRegExecCtxtPtr exec,
2867 xmlRegexpPtr comp,
2868 const xmlChar *value,
2869 void *data) {
2870 int state = exec->index;
2871 int i, target;
2872
2873 if ((comp == NULL) || (comp->compact == NULL) || (comp->stringMap == NULL))
2874 return(-1);
2875
2876 if (value == NULL) {
2877 /*
2878 * are we at a final state ?
2879 */
2880 if (comp->compact[state * (comp->nbstrings + 1)] ==
2881 XML_REGEXP_FINAL_STATE)
2882 return(1);
2883 return(0);
2884 }
2885
2886#ifdef DEBUG_PUSH
2887 printf("value pushed: %s\n", value);
2888#endif
2889
2890 /*
William M. Brackddf71d62004-05-06 04:17:26 +00002891 * Examine all outside transitions from current state
Daniel Veillard23e73572002-09-19 19:56:43 +00002892 */
2893 for (i = 0;i < comp->nbstrings;i++) {
2894 target = comp->compact[state * (comp->nbstrings + 1) + i + 1];
2895 if ((target > 0) && (target <= comp->nbstates)) {
Daniel Veillardc0826a72004-08-10 14:17:33 +00002896 target--; /* to avoid 0 */
2897 if (xmlRegStrEqualWildcard(comp->stringMap[i], value)) {
2898 exec->index = target;
Daniel Veillard118aed72002-09-24 14:13:13 +00002899 if ((exec->callback != NULL) && (comp->transdata != NULL)) {
2900 exec->callback(exec->data, value,
2901 comp->transdata[state * comp->nbstrings + i], data);
2902 }
Daniel Veillard23e73572002-09-19 19:56:43 +00002903#ifdef DEBUG_PUSH
2904 printf("entering state %d\n", target);
2905#endif
2906 if (comp->compact[target * (comp->nbstrings + 1)] ==
Daniel Veillardcc026dc2005-01-12 13:21:17 +00002907 XML_REGEXP_SINK_STATE)
2908 goto error;
2909
2910 if (comp->compact[target * (comp->nbstrings + 1)] ==
Daniel Veillard23e73572002-09-19 19:56:43 +00002911 XML_REGEXP_FINAL_STATE)
2912 return(1);
2913 return(0);
2914 }
2915 }
2916 }
2917 /*
2918 * Failed to find an exit transition out from current state for the
2919 * current token
2920 */
2921#ifdef DEBUG_PUSH
2922 printf("failed to find a transition for %s on state %d\n", value, state);
2923#endif
Daniel Veillardcc026dc2005-01-12 13:21:17 +00002924error:
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00002925 if (exec->errString != NULL)
2926 xmlFree(exec->errString);
2927 exec->errString = xmlStrdup(value);
2928 exec->errStateNo = state;
Daniel Veillard23e73572002-09-19 19:56:43 +00002929 exec->status = -1;
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00002930#ifdef DEBUG_ERR
2931 testerr(exec);
2932#endif
Daniel Veillard23e73572002-09-19 19:56:43 +00002933 return(-1);
2934}
2935
2936/**
Daniel Veillard6e65e152005-08-09 11:09:52 +00002937 * xmlRegExecPushStringInternal:
Daniel Veillardea7751d2002-12-20 00:16:24 +00002938 * @exec: a regexp execution context or NULL to indicate the end
Daniel Veillard4255d502002-04-16 15:50:10 +00002939 * @value: a string token input
2940 * @data: data associated to the token to reuse in callbacks
Daniel Veillard6e65e152005-08-09 11:09:52 +00002941 * @compound: value was assembled from 2 strings
Daniel Veillard4255d502002-04-16 15:50:10 +00002942 *
2943 * Push one input token in the execution context
2944 *
2945 * Returns: 1 if the regexp reached a final state, 0 if non-final, and
2946 * a negative value in case of error.
2947 */
Daniel Veillard6e65e152005-08-09 11:09:52 +00002948static int
2949xmlRegExecPushStringInternal(xmlRegExecCtxtPtr exec, const xmlChar *value,
2950 void *data, int compound) {
Daniel Veillard4255d502002-04-16 15:50:10 +00002951 xmlRegTransPtr trans;
2952 xmlRegAtomPtr atom;
2953 int ret;
2954 int final = 0;
Daniel Veillard90700152005-01-08 22:05:09 +00002955 int progress = 1;
Daniel Veillard4255d502002-04-16 15:50:10 +00002956
2957 if (exec == NULL)
2958 return(-1);
Daniel Veillard23e73572002-09-19 19:56:43 +00002959 if (exec->comp == NULL)
2960 return(-1);
Daniel Veillard4255d502002-04-16 15:50:10 +00002961 if (exec->status != 0)
2962 return(exec->status);
2963
Daniel Veillard23e73572002-09-19 19:56:43 +00002964 if (exec->comp->compact != NULL)
2965 return(xmlRegCompactPushString(exec, exec->comp, value, data));
2966
Daniel Veillard4255d502002-04-16 15:50:10 +00002967 if (value == NULL) {
2968 if (exec->state->type == XML_REGEXP_FINAL_STATE)
2969 return(1);
2970 final = 1;
2971 }
2972
2973#ifdef DEBUG_PUSH
2974 printf("value pushed: %s\n", value);
2975#endif
2976 /*
2977 * If we have an active rollback stack push the new value there
2978 * and get back to where we were left
2979 */
2980 if ((value != NULL) && (exec->inputStackNr > 0)) {
2981 xmlFARegExecSaveInputString(exec, value, data);
2982 value = exec->inputStack[exec->index].value;
2983 data = exec->inputStack[exec->index].data;
2984#ifdef DEBUG_PUSH
2985 printf("value loaded: %s\n", value);
2986#endif
2987 }
2988
2989 while ((exec->status == 0) &&
2990 ((value != NULL) ||
2991 ((final == 1) &&
2992 (exec->state->type != XML_REGEXP_FINAL_STATE)))) {
2993
2994 /*
2995 * End of input on non-terminal state, rollback, however we may
2996 * still have epsilon like transition for counted transitions
2997 * on counters, in that case don't break too early.
2998 */
Daniel Veillardb509f152002-04-17 16:28:10 +00002999 if ((value == NULL) && (exec->counts == NULL))
Daniel Veillard4255d502002-04-16 15:50:10 +00003000 goto rollback;
3001
3002 exec->transcount = 0;
3003 for (;exec->transno < exec->state->nbTrans;exec->transno++) {
3004 trans = &exec->state->trans[exec->transno];
3005 if (trans->to < 0)
3006 continue;
3007 atom = trans->atom;
3008 ret = 0;
Daniel Veillard441bc322002-04-20 17:38:48 +00003009 if (trans->count == REGEXP_ALL_LAX_COUNTER) {
3010 int i;
3011 int count;
3012 xmlRegTransPtr t;
3013 xmlRegCounterPtr counter;
3014
3015 ret = 0;
3016
3017#ifdef DEBUG_PUSH
3018 printf("testing all lax %d\n", trans->count);
3019#endif
3020 /*
3021 * Check all counted transitions from the current state
3022 */
3023 if ((value == NULL) && (final)) {
3024 ret = 1;
3025 } else if (value != NULL) {
3026 for (i = 0;i < exec->state->nbTrans;i++) {
3027 t = &exec->state->trans[i];
3028 if ((t->counter < 0) || (t == trans))
3029 continue;
3030 counter = &exec->comp->counters[t->counter];
3031 count = exec->counts[t->counter];
3032 if ((count < counter->max) &&
3033 (t->atom != NULL) &&
3034 (xmlStrEqual(value, t->atom->valuep))) {
3035 ret = 0;
3036 break;
3037 }
3038 if ((count >= counter->min) &&
3039 (count < counter->max) &&
3040 (xmlStrEqual(value, t->atom->valuep))) {
3041 ret = 1;
3042 break;
3043 }
3044 }
3045 }
3046 } else if (trans->count == REGEXP_ALL_COUNTER) {
Daniel Veillard8a001f62002-04-20 07:24:11 +00003047 int i;
3048 int count;
3049 xmlRegTransPtr t;
3050 xmlRegCounterPtr counter;
3051
3052 ret = 1;
3053
3054#ifdef DEBUG_PUSH
3055 printf("testing all %d\n", trans->count);
3056#endif
3057 /*
3058 * Check all counted transitions from the current state
3059 */
3060 for (i = 0;i < exec->state->nbTrans;i++) {
3061 t = &exec->state->trans[i];
3062 if ((t->counter < 0) || (t == trans))
3063 continue;
3064 counter = &exec->comp->counters[t->counter];
3065 count = exec->counts[t->counter];
3066 if ((count < counter->min) || (count > counter->max)) {
3067 ret = 0;
3068 break;
3069 }
3070 }
3071 } else if (trans->count >= 0) {
Daniel Veillard4255d502002-04-16 15:50:10 +00003072 int count;
3073 xmlRegCounterPtr counter;
3074
3075 /*
3076 * A counted transition.
3077 */
3078
3079 count = exec->counts[trans->count];
3080 counter = &exec->comp->counters[trans->count];
3081#ifdef DEBUG_PUSH
3082 printf("testing count %d: val %d, min %d, max %d\n",
3083 trans->count, count, counter->min, counter->max);
3084#endif
3085 ret = ((count >= counter->min) && (count <= counter->max));
3086 } else if (atom == NULL) {
3087 fprintf(stderr, "epsilon transition left at runtime\n");
3088 exec->status = -2;
3089 break;
3090 } else if (value != NULL) {
Daniel Veillardc0826a72004-08-10 14:17:33 +00003091 ret = xmlRegStrEqualWildcard(atom->valuep, value);
Daniel Veillard6e65e152005-08-09 11:09:52 +00003092 if (atom->neg) {
Daniel Veillard9efc4762005-07-19 14:33:55 +00003093 ret = !ret;
Daniel Veillard6e65e152005-08-09 11:09:52 +00003094 if (!compound)
3095 ret = 0;
3096 }
Daniel Veillard441bc322002-04-20 17:38:48 +00003097 if ((ret == 1) && (trans->counter >= 0)) {
3098 xmlRegCounterPtr counter;
3099 int count;
3100
3101 count = exec->counts[trans->counter];
3102 counter = &exec->comp->counters[trans->counter];
3103 if (count >= counter->max)
3104 ret = 0;
3105 }
3106
Daniel Veillard4255d502002-04-16 15:50:10 +00003107 if ((ret == 1) && (atom->min > 0) && (atom->max > 0)) {
3108 xmlRegStatePtr to = exec->comp->states[trans->to];
3109
3110 /*
3111 * this is a multiple input sequence
3112 */
3113 if (exec->state->nbTrans > exec->transno + 1) {
3114 if (exec->inputStackNr <= 0) {
3115 xmlFARegExecSaveInputString(exec, value, data);
3116 }
3117 xmlFARegExecSave(exec);
3118 }
3119 exec->transcount = 1;
3120 do {
3121 /*
3122 * Try to progress as much as possible on the input
3123 */
3124 if (exec->transcount == atom->max) {
3125 break;
3126 }
3127 exec->index++;
3128 value = exec->inputStack[exec->index].value;
3129 data = exec->inputStack[exec->index].data;
3130#ifdef DEBUG_PUSH
3131 printf("value loaded: %s\n", value);
3132#endif
3133
3134 /*
3135 * End of input: stop here
3136 */
3137 if (value == NULL) {
3138 exec->index --;
3139 break;
3140 }
3141 if (exec->transcount >= atom->min) {
3142 int transno = exec->transno;
3143 xmlRegStatePtr state = exec->state;
3144
3145 /*
3146 * The transition is acceptable save it
3147 */
3148 exec->transno = -1; /* trick */
3149 exec->state = to;
3150 if (exec->inputStackNr <= 0) {
3151 xmlFARegExecSaveInputString(exec, value, data);
3152 }
3153 xmlFARegExecSave(exec);
3154 exec->transno = transno;
3155 exec->state = state;
3156 }
3157 ret = xmlStrEqual(value, atom->valuep);
3158 exec->transcount++;
3159 } while (ret == 1);
3160 if (exec->transcount < atom->min)
3161 ret = 0;
3162
3163 /*
3164 * If the last check failed but one transition was found
3165 * possible, rollback
3166 */
3167 if (ret < 0)
3168 ret = 0;
3169 if (ret == 0) {
3170 goto rollback;
3171 }
3172 }
3173 }
3174 if (ret == 1) {
William M. Brack98873952003-12-26 06:03:14 +00003175 if ((exec->callback != NULL) && (atom != NULL) &&
3176 (data != NULL)) {
Daniel Veillard4255d502002-04-16 15:50:10 +00003177 exec->callback(exec->data, atom->valuep,
3178 atom->data, data);
3179 }
3180 if (exec->state->nbTrans > exec->transno + 1) {
3181 if (exec->inputStackNr <= 0) {
3182 xmlFARegExecSaveInputString(exec, value, data);
3183 }
3184 xmlFARegExecSave(exec);
3185 }
3186 if (trans->counter >= 0) {
3187#ifdef DEBUG_PUSH
3188 printf("Increasing count %d\n", trans->counter);
3189#endif
3190 exec->counts[trans->counter]++;
3191 }
Daniel Veillard10752282005-08-08 13:05:13 +00003192 if ((trans->count >= 0) &&
3193 (trans->count < REGEXP_ALL_COUNTER)) {
3194#ifdef DEBUG_REGEXP_EXEC
3195 printf("resetting count %d on transition\n",
3196 trans->count);
3197#endif
3198 exec->counts[trans->count] = 0;
3199 }
Daniel Veillard4255d502002-04-16 15:50:10 +00003200#ifdef DEBUG_PUSH
3201 printf("entering state %d\n", trans->to);
3202#endif
Daniel Veillardcc026dc2005-01-12 13:21:17 +00003203 if ((exec->comp->states[trans->to] != NULL) &&
3204 (exec->comp->states[trans->to]->type ==
3205 XML_REGEXP_SINK_STATE)) {
3206 /*
3207 * entering a sink state, save the current state as error
3208 * state.
3209 */
3210 if (exec->errString != NULL)
3211 xmlFree(exec->errString);
3212 exec->errString = xmlStrdup(value);
3213 exec->errState = exec->state;
3214 memcpy(exec->errCounts, exec->counts,
3215 exec->comp->nbCounters * sizeof(int));
3216 }
Daniel Veillard4255d502002-04-16 15:50:10 +00003217 exec->state = exec->comp->states[trans->to];
3218 exec->transno = 0;
3219 if (trans->atom != NULL) {
3220 if (exec->inputStack != NULL) {
3221 exec->index++;
3222 if (exec->index < exec->inputStackNr) {
3223 value = exec->inputStack[exec->index].value;
3224 data = exec->inputStack[exec->index].data;
3225#ifdef DEBUG_PUSH
3226 printf("value loaded: %s\n", value);
3227#endif
3228 } else {
3229 value = NULL;
3230 data = NULL;
3231#ifdef DEBUG_PUSH
3232 printf("end of input\n");
3233#endif
3234 }
3235 } else {
3236 value = NULL;
3237 data = NULL;
3238#ifdef DEBUG_PUSH
3239 printf("end of input\n");
3240#endif
3241 }
3242 }
3243 goto progress;
3244 } else if (ret < 0) {
3245 exec->status = -4;
3246 break;
3247 }
3248 }
3249 if ((exec->transno != 0) || (exec->state->nbTrans == 0)) {
3250rollback:
Daniel Veillard90700152005-01-08 22:05:09 +00003251 /*
Daniel Veillardcc026dc2005-01-12 13:21:17 +00003252 * if we didn't yet rollback on the current input
3253 * store the current state as the error state.
Daniel Veillard90700152005-01-08 22:05:09 +00003254 */
Daniel Veillardcc026dc2005-01-12 13:21:17 +00003255 if ((progress) && (exec->state != NULL) &&
3256 (exec->state->type != XML_REGEXP_SINK_STATE)) {
Daniel Veillard90700152005-01-08 22:05:09 +00003257 progress = 0;
3258 if (exec->errString != NULL)
3259 xmlFree(exec->errString);
3260 exec->errString = xmlStrdup(value);
3261 exec->errState = exec->state;
3262 memcpy(exec->errCounts, exec->counts,
3263 exec->comp->nbCounters * sizeof(int));
3264 }
3265
Daniel Veillard4255d502002-04-16 15:50:10 +00003266 /*
3267 * Failed to find a way out
3268 */
3269 exec->determinist = 0;
3270 xmlFARegExecRollBack(exec);
3271 if (exec->status == 0) {
3272 value = exec->inputStack[exec->index].value;
3273 data = exec->inputStack[exec->index].data;
3274#ifdef DEBUG_PUSH
3275 printf("value loaded: %s\n", value);
3276#endif
3277 }
3278 }
Daniel Veillard90700152005-01-08 22:05:09 +00003279 continue;
Daniel Veillard4255d502002-04-16 15:50:10 +00003280progress:
Daniel Veillard90700152005-01-08 22:05:09 +00003281 progress = 1;
Daniel Veillard4255d502002-04-16 15:50:10 +00003282 continue;
3283 }
3284 if (exec->status == 0) {
3285 return(exec->state->type == XML_REGEXP_FINAL_STATE);
3286 }
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003287#ifdef DEBUG_ERR
Daniel Veillard90700152005-01-08 22:05:09 +00003288 if (exec->status < 0) {
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003289 testerr(exec);
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003290 }
Daniel Veillard90700152005-01-08 22:05:09 +00003291#endif
Daniel Veillard4255d502002-04-16 15:50:10 +00003292 return(exec->status);
3293}
3294
Daniel Veillard52b48c72003-04-13 19:53:42 +00003295/**
Daniel Veillard6e65e152005-08-09 11:09:52 +00003296 * xmlRegExecPushString:
3297 * @exec: a regexp execution context or NULL to indicate the end
3298 * @value: a string token input
3299 * @data: data associated to the token to reuse in callbacks
3300 *
3301 * Push one input token in the execution context
3302 *
3303 * Returns: 1 if the regexp reached a final state, 0 if non-final, and
3304 * a negative value in case of error.
3305 */
3306int
3307xmlRegExecPushString(xmlRegExecCtxtPtr exec, const xmlChar *value,
3308 void *data) {
3309 return(xmlRegExecPushStringInternal(exec, value, data, 0));
3310}
3311
3312/**
Daniel Veillard52b48c72003-04-13 19:53:42 +00003313 * xmlRegExecPushString2:
3314 * @exec: a regexp execution context or NULL to indicate the end
3315 * @value: the first string token input
3316 * @value2: the second string token input
3317 * @data: data associated to the token to reuse in callbacks
3318 *
3319 * Push one input token in the execution context
3320 *
3321 * Returns: 1 if the regexp reached a final state, 0 if non-final, and
3322 * a negative value in case of error.
3323 */
3324int
3325xmlRegExecPushString2(xmlRegExecCtxtPtr exec, const xmlChar *value,
3326 const xmlChar *value2, void *data) {
3327 xmlChar buf[150];
3328 int lenn, lenp, ret;
3329 xmlChar *str;
3330
3331 if (exec == NULL)
3332 return(-1);
3333 if (exec->comp == NULL)
3334 return(-1);
3335 if (exec->status != 0)
3336 return(exec->status);
3337
3338 if (value2 == NULL)
3339 return(xmlRegExecPushString(exec, value, data));
3340
3341 lenn = strlen((char *) value2);
3342 lenp = strlen((char *) value);
3343
3344 if (150 < lenn + lenp + 2) {
Daniel Veillard3c908dc2003-04-19 00:07:51 +00003345 str = (xmlChar *) xmlMallocAtomic(lenn + lenp + 2);
Daniel Veillard52b48c72003-04-13 19:53:42 +00003346 if (str == NULL) {
3347 exec->status = -1;
3348 return(-1);
3349 }
3350 } else {
3351 str = buf;
3352 }
3353 memcpy(&str[0], value, lenp);
Daniel Veillardc0826a72004-08-10 14:17:33 +00003354 str[lenp] = XML_REG_STRING_SEPARATOR;
Daniel Veillard52b48c72003-04-13 19:53:42 +00003355 memcpy(&str[lenp + 1], value2, lenn);
3356 str[lenn + lenp + 1] = 0;
3357
3358 if (exec->comp->compact != NULL)
3359 ret = xmlRegCompactPushString(exec, exec->comp, str, data);
3360 else
Daniel Veillard6e65e152005-08-09 11:09:52 +00003361 ret = xmlRegExecPushStringInternal(exec, str, data, 1);
Daniel Veillard52b48c72003-04-13 19:53:42 +00003362
3363 if (str != buf)
3364 xmlFree(buf);
3365 return(ret);
3366}
3367
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003368/**
Daniel Veillard77005e62005-07-19 16:26:18 +00003369 * xmlRegExecGetValues:
Daniel Veillardfc0b6f62005-01-09 17:48:02 +00003370 * @exec: a regexp execution context
3371 * @err: error extraction or normal one
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003372 * @nbval: pointer to the number of accepted values IN/OUT
Daniel Veillardcc026dc2005-01-12 13:21:17 +00003373 * @nbneg: return number of negative transitions
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003374 * @values: pointer to the array of acceptable values
Daniel Veillardfc0b6f62005-01-09 17:48:02 +00003375 * @terminal: return value if this was a terminal state
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003376 *
Daniel Veillardfc0b6f62005-01-09 17:48:02 +00003377 * Extract informations from the regexp execution, internal routine to
3378 * implement xmlRegExecNextValues() and xmlRegExecErrInfo()
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003379 *
3380 * Returns: 0 in case of success or -1 in case of error.
3381 */
Daniel Veillardfc0b6f62005-01-09 17:48:02 +00003382static int
3383xmlRegExecGetValues(xmlRegExecCtxtPtr exec, int err,
Daniel Veillardcc026dc2005-01-12 13:21:17 +00003384 int *nbval, int *nbneg,
3385 xmlChar **values, int *terminal) {
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003386 int maxval;
Daniel Veillardcc026dc2005-01-12 13:21:17 +00003387 int nb = 0;
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003388
Daniel Veillardcc026dc2005-01-12 13:21:17 +00003389 if ((exec == NULL) || (nbval == NULL) || (nbneg == NULL) ||
3390 (values == NULL) || (*nbval <= 0))
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003391 return(-1);
Daniel Veillardfc0b6f62005-01-09 17:48:02 +00003392
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003393 maxval = *nbval;
3394 *nbval = 0;
Daniel Veillardcc026dc2005-01-12 13:21:17 +00003395 *nbneg = 0;
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003396 if ((exec->comp != NULL) && (exec->comp->compact != NULL)) {
3397 xmlRegexpPtr comp;
3398 int target, i, state;
3399
3400 comp = exec->comp;
Daniel Veillardfc0b6f62005-01-09 17:48:02 +00003401
3402 if (err) {
3403 if (exec->errStateNo == -1) return(-1);
3404 state = exec->errStateNo;
3405 } else {
3406 state = exec->index;
3407 }
3408 if (terminal != NULL) {
3409 if (comp->compact[state * (comp->nbstrings + 1)] ==
3410 XML_REGEXP_FINAL_STATE)
3411 *terminal = 1;
3412 else
3413 *terminal = 0;
3414 }
Daniel Veillardcc026dc2005-01-12 13:21:17 +00003415 for (i = 0;(i < comp->nbstrings) && (nb < maxval);i++) {
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003416 target = comp->compact[state * (comp->nbstrings + 1) + i + 1];
Daniel Veillardcc026dc2005-01-12 13:21:17 +00003417 if ((target > 0) && (target <= comp->nbstates) &&
3418 (comp->compact[(target - 1) * (comp->nbstrings + 1)] !=
3419 XML_REGEXP_SINK_STATE)) {
3420 values[nb++] = comp->stringMap[i];
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003421 (*nbval)++;
3422 }
3423 }
Daniel Veillardcc026dc2005-01-12 13:21:17 +00003424 for (i = 0;(i < comp->nbstrings) && (nb < maxval);i++) {
3425 target = comp->compact[state * (comp->nbstrings + 1) + i + 1];
3426 if ((target > 0) && (target <= comp->nbstates) &&
3427 (comp->compact[(target - 1) * (comp->nbstrings + 1)] ==
3428 XML_REGEXP_SINK_STATE)) {
3429 values[nb++] = comp->stringMap[i];
3430 (*nbneg)++;
3431 }
3432 }
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003433 } else {
3434 int transno;
3435 xmlRegTransPtr trans;
3436 xmlRegAtomPtr atom;
Daniel Veillardfc0b6f62005-01-09 17:48:02 +00003437 xmlRegStatePtr state;
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003438
Daniel Veillardfc0b6f62005-01-09 17:48:02 +00003439 if (terminal != NULL) {
3440 if (exec->state->type == XML_REGEXP_FINAL_STATE)
3441 *terminal = 1;
3442 else
3443 *terminal = 0;
3444 }
3445
3446 if (err) {
3447 if (exec->errState == NULL) return(-1);
3448 state = exec->errState;
3449 } else {
3450 if (exec->state == NULL) return(-1);
3451 state = exec->state;
3452 }
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003453 for (transno = 0;
Daniel Veillardcc026dc2005-01-12 13:21:17 +00003454 (transno < state->nbTrans) && (nb < maxval);
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003455 transno++) {
Daniel Veillardfc0b6f62005-01-09 17:48:02 +00003456 trans = &state->trans[transno];
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003457 if (trans->to < 0)
3458 continue;
3459 atom = trans->atom;
3460 if ((atom == NULL) || (atom->valuep == NULL))
3461 continue;
3462 if (trans->count == REGEXP_ALL_LAX_COUNTER) {
Daniel Veillardcc026dc2005-01-12 13:21:17 +00003463 /* this should not be reached but ... */
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003464 TODO;
3465 } else if (trans->count == REGEXP_ALL_COUNTER) {
Daniel Veillardcc026dc2005-01-12 13:21:17 +00003466 /* this should not be reached but ... */
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003467 TODO;
3468 } else if (trans->counter >= 0) {
3469 xmlRegCounterPtr counter;
3470 int count;
3471
Daniel Veillardfc0b6f62005-01-09 17:48:02 +00003472 if (err)
3473 count = exec->errCounts[trans->counter];
3474 else
3475 count = exec->counts[trans->counter];
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003476 counter = &exec->comp->counters[trans->counter];
3477 if (count < counter->max) {
Daniel Veillard77005e62005-07-19 16:26:18 +00003478 if (atom->neg)
3479 values[nb++] = (xmlChar *) atom->valuep2;
3480 else
3481 values[nb++] = (xmlChar *) atom->valuep;
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003482 (*nbval)++;
3483 }
3484 } else {
Daniel Veillardcc026dc2005-01-12 13:21:17 +00003485 if ((exec->comp->states[trans->to] != NULL) &&
3486 (exec->comp->states[trans->to]->type !=
3487 XML_REGEXP_SINK_STATE)) {
Daniel Veillard77005e62005-07-19 16:26:18 +00003488 if (atom->neg)
3489 values[nb++] = (xmlChar *) atom->valuep2;
3490 else
3491 values[nb++] = (xmlChar *) atom->valuep;
Daniel Veillardcc026dc2005-01-12 13:21:17 +00003492 (*nbval)++;
3493 }
3494 }
3495 }
3496 for (transno = 0;
3497 (transno < state->nbTrans) && (nb < maxval);
3498 transno++) {
3499 trans = &state->trans[transno];
3500 if (trans->to < 0)
3501 continue;
3502 atom = trans->atom;
3503 if ((atom == NULL) || (atom->valuep == NULL))
3504 continue;
3505 if (trans->count == REGEXP_ALL_LAX_COUNTER) {
3506 continue;
3507 } else if (trans->count == REGEXP_ALL_COUNTER) {
3508 continue;
3509 } else if (trans->counter >= 0) {
3510 continue;
3511 } else {
3512 if ((exec->comp->states[trans->to] != NULL) &&
3513 (exec->comp->states[trans->to]->type ==
3514 XML_REGEXP_SINK_STATE)) {
Daniel Veillard77005e62005-07-19 16:26:18 +00003515 if (atom->neg)
3516 values[nb++] = (xmlChar *) atom->valuep2;
3517 else
3518 values[nb++] = (xmlChar *) atom->valuep;
Daniel Veillardcc026dc2005-01-12 13:21:17 +00003519 (*nbneg)++;
3520 }
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003521 }
3522 }
3523 }
3524 return(0);
3525}
3526
Daniel Veillardfc0b6f62005-01-09 17:48:02 +00003527/**
3528 * xmlRegExecNextValues:
3529 * @exec: a regexp execution context
3530 * @nbval: pointer to the number of accepted values IN/OUT
Daniel Veillardcc026dc2005-01-12 13:21:17 +00003531 * @nbneg: return number of negative transitions
Daniel Veillardfc0b6f62005-01-09 17:48:02 +00003532 * @values: pointer to the array of acceptable values
3533 * @terminal: return value if this was a terminal state
3534 *
3535 * Extract informations from the regexp execution,
3536 * the parameter @values must point to an array of @nbval string pointers
3537 * on return nbval will contain the number of possible strings in that
3538 * state and the @values array will be updated with them. The string values
3539 * returned will be freed with the @exec context and don't need to be
3540 * deallocated.
3541 *
3542 * Returns: 0 in case of success or -1 in case of error.
3543 */
3544int
Daniel Veillardcc026dc2005-01-12 13:21:17 +00003545xmlRegExecNextValues(xmlRegExecCtxtPtr exec, int *nbval, int *nbneg,
3546 xmlChar **values, int *terminal) {
3547 return(xmlRegExecGetValues(exec, 0, nbval, nbneg, values, terminal));
Daniel Veillardfc0b6f62005-01-09 17:48:02 +00003548}
3549
3550/**
3551 * xmlRegExecErrInfo:
3552 * @exec: a regexp execution context generating an error
3553 * @string: return value for the error string
3554 * @nbval: pointer to the number of accepted values IN/OUT
Daniel Veillardcc026dc2005-01-12 13:21:17 +00003555 * @nbneg: return number of negative transitions
Daniel Veillardfc0b6f62005-01-09 17:48:02 +00003556 * @values: pointer to the array of acceptable values
3557 * @terminal: return value if this was a terminal state
3558 *
3559 * Extract error informations from the regexp execution, the parameter
3560 * @string will be updated with the value pushed and not accepted,
3561 * the parameter @values must point to an array of @nbval string pointers
3562 * on return nbval will contain the number of possible strings in that
3563 * state and the @values array will be updated with them. The string values
3564 * returned will be freed with the @exec context and don't need to be
3565 * deallocated.
3566 *
3567 * Returns: 0 in case of success or -1 in case of error.
3568 */
3569int
3570xmlRegExecErrInfo(xmlRegExecCtxtPtr exec, const xmlChar **string,
Daniel Veillardcc026dc2005-01-12 13:21:17 +00003571 int *nbval, int *nbneg, xmlChar **values, int *terminal) {
Daniel Veillardfc0b6f62005-01-09 17:48:02 +00003572 if (exec == NULL)
3573 return(-1);
3574 if (string != NULL) {
3575 if (exec->status != 0)
3576 *string = exec->errString;
3577 else
3578 *string = NULL;
3579 }
Daniel Veillardcc026dc2005-01-12 13:21:17 +00003580 return(xmlRegExecGetValues(exec, 1, nbval, nbneg, values, terminal));
Daniel Veillardfc0b6f62005-01-09 17:48:02 +00003581}
3582
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003583#ifdef DEBUG_ERR
3584static void testerr(xmlRegExecCtxtPtr exec) {
3585 const xmlChar *string;
Daniel Veillardcee2b3a2005-01-25 00:22:52 +00003586 xmlChar *values[5];
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003587 int nb = 5;
Daniel Veillardcc026dc2005-01-12 13:21:17 +00003588 int nbneg;
Daniel Veillardfc0b6f62005-01-09 17:48:02 +00003589 int terminal;
Daniel Veillardcc026dc2005-01-12 13:21:17 +00003590 xmlRegExecErrInfo(exec, &string, &nb, &nbneg, &values[0], &terminal);
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003591}
3592#endif
3593
Daniel Veillard4255d502002-04-16 15:50:10 +00003594#if 0
3595static int
3596xmlRegExecPushChar(xmlRegExecCtxtPtr exec, int UCS) {
3597 xmlRegTransPtr trans;
3598 xmlRegAtomPtr atom;
3599 int ret;
3600 int codepoint, len;
3601
3602 if (exec == NULL)
3603 return(-1);
3604 if (exec->status != 0)
3605 return(exec->status);
3606
3607 while ((exec->status == 0) &&
3608 ((exec->inputString[exec->index] != 0) ||
3609 (exec->state->type != XML_REGEXP_FINAL_STATE))) {
3610
3611 /*
3612 * End of input on non-terminal state, rollback, however we may
3613 * still have epsilon like transition for counted transitions
3614 * on counters, in that case don't break too early.
3615 */
3616 if ((exec->inputString[exec->index] == 0) && (exec->counts == NULL))
3617 goto rollback;
3618
3619 exec->transcount = 0;
3620 for (;exec->transno < exec->state->nbTrans;exec->transno++) {
3621 trans = &exec->state->trans[exec->transno];
3622 if (trans->to < 0)
3623 continue;
3624 atom = trans->atom;
3625 ret = 0;
3626 if (trans->count >= 0) {
3627 int count;
3628 xmlRegCounterPtr counter;
3629
3630 /*
3631 * A counted transition.
3632 */
3633
3634 count = exec->counts[trans->count];
3635 counter = &exec->comp->counters[trans->count];
3636#ifdef DEBUG_REGEXP_EXEC
3637 printf("testing count %d: val %d, min %d, max %d\n",
3638 trans->count, count, counter->min, counter->max);
3639#endif
3640 ret = ((count >= counter->min) && (count <= counter->max));
3641 } else if (atom == NULL) {
3642 fprintf(stderr, "epsilon transition left at runtime\n");
3643 exec->status = -2;
3644 break;
3645 } else if (exec->inputString[exec->index] != 0) {
3646 codepoint = CUR_SCHAR(&(exec->inputString[exec->index]), len);
3647 ret = xmlRegCheckCharacter(atom, codepoint);
3648 if ((ret == 1) && (atom->min > 0) && (atom->max > 0)) {
3649 xmlRegStatePtr to = exec->comp->states[trans->to];
3650
3651 /*
3652 * this is a multiple input sequence
3653 */
3654 if (exec->state->nbTrans > exec->transno + 1) {
3655 xmlFARegExecSave(exec);
3656 }
3657 exec->transcount = 1;
3658 do {
3659 /*
3660 * Try to progress as much as possible on the input
3661 */
3662 if (exec->transcount == atom->max) {
3663 break;
3664 }
3665 exec->index += len;
3666 /*
3667 * End of input: stop here
3668 */
3669 if (exec->inputString[exec->index] == 0) {
3670 exec->index -= len;
3671 break;
3672 }
3673 if (exec->transcount >= atom->min) {
3674 int transno = exec->transno;
3675 xmlRegStatePtr state = exec->state;
3676
3677 /*
3678 * The transition is acceptable save it
3679 */
3680 exec->transno = -1; /* trick */
3681 exec->state = to;
3682 xmlFARegExecSave(exec);
3683 exec->transno = transno;
3684 exec->state = state;
3685 }
3686 codepoint = CUR_SCHAR(&(exec->inputString[exec->index]),
3687 len);
3688 ret = xmlRegCheckCharacter(atom, codepoint);
3689 exec->transcount++;
3690 } while (ret == 1);
3691 if (exec->transcount < atom->min)
3692 ret = 0;
3693
3694 /*
3695 * If the last check failed but one transition was found
3696 * possible, rollback
3697 */
3698 if (ret < 0)
3699 ret = 0;
3700 if (ret == 0) {
3701 goto rollback;
3702 }
3703 }
3704 }
3705 if (ret == 1) {
3706 if (exec->state->nbTrans > exec->transno + 1) {
3707 xmlFARegExecSave(exec);
3708 }
3709 if (trans->counter >= 0) {
3710#ifdef DEBUG_REGEXP_EXEC
3711 printf("Increasing count %d\n", trans->counter);
3712#endif
3713 exec->counts[trans->counter]++;
3714 }
3715#ifdef DEBUG_REGEXP_EXEC
3716 printf("entering state %d\n", trans->to);
3717#endif
3718 exec->state = exec->comp->states[trans->to];
3719 exec->transno = 0;
3720 if (trans->atom != NULL) {
3721 exec->index += len;
3722 }
3723 goto progress;
3724 } else if (ret < 0) {
3725 exec->status = -4;
3726 break;
3727 }
3728 }
3729 if ((exec->transno != 0) || (exec->state->nbTrans == 0)) {
3730rollback:
3731 /*
3732 * Failed to find a way out
3733 */
3734 exec->determinist = 0;
3735 xmlFARegExecRollBack(exec);
3736 }
3737progress:
3738 continue;
3739 }
3740}
3741#endif
3742/************************************************************************
3743 * *
William M. Brackddf71d62004-05-06 04:17:26 +00003744 * Parser for the Schemas Datatype Regular Expressions *
Daniel Veillard4255d502002-04-16 15:50:10 +00003745 * http://www.w3.org/TR/2001/REC-xmlschema-2-20010502/#regexs *
3746 * *
3747 ************************************************************************/
3748
3749/**
3750 * xmlFAIsChar:
Daniel Veillard441bc322002-04-20 17:38:48 +00003751 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00003752 *
3753 * [10] Char ::= [^.\?*+()|#x5B#x5D]
3754 */
3755static int
3756xmlFAIsChar(xmlRegParserCtxtPtr ctxt) {
3757 int cur;
3758 int len;
3759
3760 cur = CUR_SCHAR(ctxt->cur, len);
3761 if ((cur == '.') || (cur == '\\') || (cur == '?') ||
3762 (cur == '*') || (cur == '+') || (cur == '(') ||
3763 (cur == ')') || (cur == '|') || (cur == 0x5B) ||
3764 (cur == 0x5D) || (cur == 0))
3765 return(-1);
3766 return(cur);
3767}
3768
3769/**
3770 * xmlFAParseCharProp:
Daniel Veillard441bc322002-04-20 17:38:48 +00003771 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00003772 *
3773 * [27] charProp ::= IsCategory | IsBlock
3774 * [28] IsCategory ::= Letters | Marks | Numbers | Punctuation |
3775 * Separators | Symbols | Others
3776 * [29] Letters ::= 'L' [ultmo]?
3777 * [30] Marks ::= 'M' [nce]?
3778 * [31] Numbers ::= 'N' [dlo]?
3779 * [32] Punctuation ::= 'P' [cdseifo]?
3780 * [33] Separators ::= 'Z' [slp]?
3781 * [34] Symbols ::= 'S' [mcko]?
3782 * [35] Others ::= 'C' [cfon]?
3783 * [36] IsBlock ::= 'Is' [a-zA-Z0-9#x2D]+
3784 */
3785static void
3786xmlFAParseCharProp(xmlRegParserCtxtPtr ctxt) {
3787 int cur;
William M. Brack779af002003-08-01 15:55:39 +00003788 xmlRegAtomType type = (xmlRegAtomType) 0;
Daniel Veillard4255d502002-04-16 15:50:10 +00003789 xmlChar *blockName = NULL;
3790
3791 cur = CUR;
3792 if (cur == 'L') {
3793 NEXT;
3794 cur = CUR;
3795 if (cur == 'u') {
3796 NEXT;
3797 type = XML_REGEXP_LETTER_UPPERCASE;
3798 } else if (cur == 'l') {
3799 NEXT;
3800 type = XML_REGEXP_LETTER_LOWERCASE;
3801 } else if (cur == 't') {
3802 NEXT;
3803 type = XML_REGEXP_LETTER_TITLECASE;
3804 } else if (cur == 'm') {
3805 NEXT;
3806 type = XML_REGEXP_LETTER_MODIFIER;
3807 } else if (cur == 'o') {
3808 NEXT;
3809 type = XML_REGEXP_LETTER_OTHERS;
3810 } else {
3811 type = XML_REGEXP_LETTER;
3812 }
3813 } else if (cur == 'M') {
3814 NEXT;
3815 cur = CUR;
3816 if (cur == 'n') {
3817 NEXT;
3818 /* nonspacing */
3819 type = XML_REGEXP_MARK_NONSPACING;
3820 } else if (cur == 'c') {
3821 NEXT;
3822 /* spacing combining */
3823 type = XML_REGEXP_MARK_SPACECOMBINING;
3824 } else if (cur == 'e') {
3825 NEXT;
3826 /* enclosing */
3827 type = XML_REGEXP_MARK_ENCLOSING;
3828 } else {
3829 /* all marks */
3830 type = XML_REGEXP_MARK;
3831 }
3832 } else if (cur == 'N') {
3833 NEXT;
3834 cur = CUR;
3835 if (cur == 'd') {
3836 NEXT;
3837 /* digital */
3838 type = XML_REGEXP_NUMBER_DECIMAL;
3839 } else if (cur == 'l') {
3840 NEXT;
3841 /* letter */
3842 type = XML_REGEXP_NUMBER_LETTER;
3843 } else if (cur == 'o') {
3844 NEXT;
3845 /* other */
3846 type = XML_REGEXP_NUMBER_OTHERS;
3847 } else {
3848 /* all numbers */
3849 type = XML_REGEXP_NUMBER;
3850 }
3851 } else if (cur == 'P') {
3852 NEXT;
3853 cur = CUR;
3854 if (cur == 'c') {
3855 NEXT;
3856 /* connector */
3857 type = XML_REGEXP_PUNCT_CONNECTOR;
3858 } else if (cur == 'd') {
3859 NEXT;
3860 /* dash */
3861 type = XML_REGEXP_PUNCT_DASH;
3862 } else if (cur == 's') {
3863 NEXT;
3864 /* open */
3865 type = XML_REGEXP_PUNCT_OPEN;
3866 } else if (cur == 'e') {
3867 NEXT;
3868 /* close */
3869 type = XML_REGEXP_PUNCT_CLOSE;
3870 } else if (cur == 'i') {
3871 NEXT;
3872 /* initial quote */
3873 type = XML_REGEXP_PUNCT_INITQUOTE;
3874 } else if (cur == 'f') {
3875 NEXT;
3876 /* final quote */
3877 type = XML_REGEXP_PUNCT_FINQUOTE;
3878 } else if (cur == 'o') {
3879 NEXT;
3880 /* other */
3881 type = XML_REGEXP_PUNCT_OTHERS;
3882 } else {
3883 /* all punctuation */
3884 type = XML_REGEXP_PUNCT;
3885 }
3886 } else if (cur == 'Z') {
3887 NEXT;
3888 cur = CUR;
3889 if (cur == 's') {
3890 NEXT;
3891 /* space */
3892 type = XML_REGEXP_SEPAR_SPACE;
3893 } else if (cur == 'l') {
3894 NEXT;
3895 /* line */
3896 type = XML_REGEXP_SEPAR_LINE;
3897 } else if (cur == 'p') {
3898 NEXT;
3899 /* paragraph */
3900 type = XML_REGEXP_SEPAR_PARA;
3901 } else {
3902 /* all separators */
3903 type = XML_REGEXP_SEPAR;
3904 }
3905 } else if (cur == 'S') {
3906 NEXT;
3907 cur = CUR;
3908 if (cur == 'm') {
3909 NEXT;
3910 type = XML_REGEXP_SYMBOL_MATH;
3911 /* math */
3912 } else if (cur == 'c') {
3913 NEXT;
3914 type = XML_REGEXP_SYMBOL_CURRENCY;
3915 /* currency */
3916 } else if (cur == 'k') {
3917 NEXT;
3918 type = XML_REGEXP_SYMBOL_MODIFIER;
3919 /* modifiers */
3920 } else if (cur == 'o') {
3921 NEXT;
3922 type = XML_REGEXP_SYMBOL_OTHERS;
3923 /* other */
3924 } else {
3925 /* all symbols */
3926 type = XML_REGEXP_SYMBOL;
3927 }
3928 } else if (cur == 'C') {
3929 NEXT;
3930 cur = CUR;
3931 if (cur == 'c') {
3932 NEXT;
3933 /* control */
3934 type = XML_REGEXP_OTHER_CONTROL;
3935 } else if (cur == 'f') {
3936 NEXT;
3937 /* format */
3938 type = XML_REGEXP_OTHER_FORMAT;
3939 } else if (cur == 'o') {
3940 NEXT;
3941 /* private use */
3942 type = XML_REGEXP_OTHER_PRIVATE;
3943 } else if (cur == 'n') {
3944 NEXT;
3945 /* not assigned */
3946 type = XML_REGEXP_OTHER_NA;
3947 } else {
3948 /* all others */
3949 type = XML_REGEXP_OTHER;
3950 }
3951 } else if (cur == 'I') {
3952 const xmlChar *start;
3953 NEXT;
3954 cur = CUR;
3955 if (cur != 's') {
3956 ERROR("IsXXXX expected");
3957 return;
3958 }
3959 NEXT;
3960 start = ctxt->cur;
3961 cur = CUR;
3962 if (((cur >= 'a') && (cur <= 'z')) ||
3963 ((cur >= 'A') && (cur <= 'Z')) ||
3964 ((cur >= '0') && (cur <= '9')) ||
3965 (cur == 0x2D)) {
3966 NEXT;
3967 cur = CUR;
3968 while (((cur >= 'a') && (cur <= 'z')) ||
3969 ((cur >= 'A') && (cur <= 'Z')) ||
3970 ((cur >= '0') && (cur <= '9')) ||
3971 (cur == 0x2D)) {
3972 NEXT;
3973 cur = CUR;
3974 }
3975 }
3976 type = XML_REGEXP_BLOCK_NAME;
3977 blockName = xmlStrndup(start, ctxt->cur - start);
3978 } else {
3979 ERROR("Unknown char property");
3980 return;
3981 }
3982 if (ctxt->atom == NULL) {
3983 ctxt->atom = xmlRegNewAtom(ctxt, type);
3984 if (ctxt->atom != NULL)
3985 ctxt->atom->valuep = blockName;
3986 } else if (ctxt->atom->type == XML_REGEXP_RANGES) {
3987 xmlRegAtomAddRange(ctxt, ctxt->atom, ctxt->neg,
3988 type, 0, 0, blockName);
3989 }
3990}
3991
3992/**
3993 * xmlFAParseCharClassEsc:
Daniel Veillard441bc322002-04-20 17:38:48 +00003994 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00003995 *
3996 * [23] charClassEsc ::= ( SingleCharEsc | MultiCharEsc | catEsc | complEsc )
3997 * [24] SingleCharEsc ::= '\' [nrt\|.?*+(){}#x2D#x5B#x5D#x5E]
3998 * [25] catEsc ::= '\p{' charProp '}'
3999 * [26] complEsc ::= '\P{' charProp '}'
4000 * [37] MultiCharEsc ::= '.' | ('\' [sSiIcCdDwW])
4001 */
4002static void
4003xmlFAParseCharClassEsc(xmlRegParserCtxtPtr ctxt) {
4004 int cur;
4005
4006 if (CUR == '.') {
4007 if (ctxt->atom == NULL) {
4008 ctxt->atom = xmlRegNewAtom(ctxt, XML_REGEXP_ANYCHAR);
4009 } else if (ctxt->atom->type == XML_REGEXP_RANGES) {
4010 xmlRegAtomAddRange(ctxt, ctxt->atom, ctxt->neg,
4011 XML_REGEXP_ANYCHAR, 0, 0, NULL);
4012 }
4013 NEXT;
4014 return;
4015 }
4016 if (CUR != '\\') {
4017 ERROR("Escaped sequence: expecting \\");
4018 return;
4019 }
4020 NEXT;
4021 cur = CUR;
4022 if (cur == 'p') {
4023 NEXT;
4024 if (CUR != '{') {
4025 ERROR("Expecting '{'");
4026 return;
4027 }
4028 NEXT;
4029 xmlFAParseCharProp(ctxt);
4030 if (CUR != '}') {
4031 ERROR("Expecting '}'");
4032 return;
4033 }
4034 NEXT;
4035 } else if (cur == 'P') {
4036 NEXT;
4037 if (CUR != '{') {
4038 ERROR("Expecting '{'");
4039 return;
4040 }
4041 NEXT;
4042 xmlFAParseCharProp(ctxt);
4043 ctxt->atom->neg = 1;
4044 if (CUR != '}') {
4045 ERROR("Expecting '}'");
4046 return;
4047 }
4048 NEXT;
4049 } else if ((cur == 'n') || (cur == 'r') || (cur == 't') || (cur == '\\') ||
4050 (cur == '|') || (cur == '.') || (cur == '?') || (cur == '*') ||
4051 (cur == '+') || (cur == '(') || (cur == ')') || (cur == '{') ||
4052 (cur == '}') || (cur == 0x2D) || (cur == 0x5B) || (cur == 0x5D) ||
4053 (cur == 0x5E)) {
4054 if (ctxt->atom == NULL) {
4055 ctxt->atom = xmlRegNewAtom(ctxt, XML_REGEXP_CHARVAL);
Daniel Veillard99c394d2005-07-14 12:58:49 +00004056 if (ctxt->atom != NULL) {
4057 switch (cur) {
4058 case 'n':
4059 ctxt->atom->codepoint = '\n';
4060 break;
4061 case 'r':
4062 ctxt->atom->codepoint = '\r';
4063 break;
4064 case 't':
4065 ctxt->atom->codepoint = '\t';
4066 break;
4067 default:
4068 ctxt->atom->codepoint = cur;
4069 }
4070 }
Daniel Veillard4255d502002-04-16 15:50:10 +00004071 } else if (ctxt->atom->type == XML_REGEXP_RANGES) {
4072 xmlRegAtomAddRange(ctxt, ctxt->atom, ctxt->neg,
4073 XML_REGEXP_CHARVAL, cur, cur, NULL);
4074 }
4075 NEXT;
4076 } else if ((cur == 's') || (cur == 'S') || (cur == 'i') || (cur == 'I') ||
4077 (cur == 'c') || (cur == 'C') || (cur == 'd') || (cur == 'D') ||
4078 (cur == 'w') || (cur == 'W')) {
Daniel Veillardb509f152002-04-17 16:28:10 +00004079 xmlRegAtomType type = XML_REGEXP_ANYSPACE;
Daniel Veillard4255d502002-04-16 15:50:10 +00004080
4081 switch (cur) {
4082 case 's':
4083 type = XML_REGEXP_ANYSPACE;
4084 break;
4085 case 'S':
4086 type = XML_REGEXP_NOTSPACE;
4087 break;
4088 case 'i':
4089 type = XML_REGEXP_INITNAME;
4090 break;
4091 case 'I':
4092 type = XML_REGEXP_NOTINITNAME;
4093 break;
4094 case 'c':
4095 type = XML_REGEXP_NAMECHAR;
4096 break;
4097 case 'C':
4098 type = XML_REGEXP_NOTNAMECHAR;
4099 break;
4100 case 'd':
4101 type = XML_REGEXP_DECIMAL;
4102 break;
4103 case 'D':
4104 type = XML_REGEXP_NOTDECIMAL;
4105 break;
4106 case 'w':
4107 type = XML_REGEXP_REALCHAR;
4108 break;
4109 case 'W':
4110 type = XML_REGEXP_NOTREALCHAR;
4111 break;
4112 }
4113 NEXT;
4114 if (ctxt->atom == NULL) {
4115 ctxt->atom = xmlRegNewAtom(ctxt, type);
4116 } else if (ctxt->atom->type == XML_REGEXP_RANGES) {
4117 xmlRegAtomAddRange(ctxt, ctxt->atom, ctxt->neg,
4118 type, 0, 0, NULL);
4119 }
4120 }
4121}
4122
4123/**
4124 * xmlFAParseCharRef:
Daniel Veillard441bc322002-04-20 17:38:48 +00004125 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00004126 *
4127 * [19] XmlCharRef ::= ( '&#' [0-9]+ ';' ) | (' &#x' [0-9a-fA-F]+ ';' )
4128 */
4129static int
4130xmlFAParseCharRef(xmlRegParserCtxtPtr ctxt) {
4131 int ret = 0, cur;
4132
4133 if ((CUR != '&') || (NXT(1) != '#'))
4134 return(-1);
4135 NEXT;
4136 NEXT;
4137 cur = CUR;
4138 if (cur == 'x') {
4139 NEXT;
4140 cur = CUR;
4141 if (((cur >= '0') && (cur <= '9')) ||
4142 ((cur >= 'a') && (cur <= 'f')) ||
4143 ((cur >= 'A') && (cur <= 'F'))) {
4144 while (((cur >= '0') && (cur <= '9')) ||
4145 ((cur >= 'A') && (cur <= 'F'))) {
4146 if ((cur >= '0') && (cur <= '9'))
4147 ret = ret * 16 + cur - '0';
4148 else if ((cur >= 'a') && (cur <= 'f'))
4149 ret = ret * 16 + 10 + (cur - 'a');
4150 else
4151 ret = ret * 16 + 10 + (cur - 'A');
4152 NEXT;
4153 cur = CUR;
4154 }
4155 } else {
4156 ERROR("Char ref: expecting [0-9A-F]");
4157 return(-1);
4158 }
4159 } else {
4160 if ((cur >= '0') && (cur <= '9')) {
4161 while ((cur >= '0') && (cur <= '9')) {
4162 ret = ret * 10 + cur - '0';
4163 NEXT;
4164 cur = CUR;
4165 }
4166 } else {
4167 ERROR("Char ref: expecting [0-9]");
4168 return(-1);
4169 }
4170 }
4171 if (cur != ';') {
4172 ERROR("Char ref: expecting ';'");
4173 return(-1);
4174 } else {
4175 NEXT;
4176 }
4177 return(ret);
4178}
4179
4180/**
4181 * xmlFAParseCharRange:
Daniel Veillard441bc322002-04-20 17:38:48 +00004182 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00004183 *
4184 * [17] charRange ::= seRange | XmlCharRef | XmlCharIncDash
4185 * [18] seRange ::= charOrEsc '-' charOrEsc
4186 * [20] charOrEsc ::= XmlChar | SingleCharEsc
4187 * [21] XmlChar ::= [^\#x2D#x5B#x5D]
4188 * [22] XmlCharIncDash ::= [^\#x5B#x5D]
4189 */
4190static void
4191xmlFAParseCharRange(xmlRegParserCtxtPtr ctxt) {
William M. Brackdc99df92003-12-27 01:54:25 +00004192 int cur, len;
Daniel Veillard4255d502002-04-16 15:50:10 +00004193 int start = -1;
4194 int end = -1;
4195
4196 if ((CUR == '&') && (NXT(1) == '#')) {
4197 end = start = xmlFAParseCharRef(ctxt);
4198 xmlRegAtomAddRange(ctxt, ctxt->atom, ctxt->neg,
4199 XML_REGEXP_CHARVAL, start, end, NULL);
4200 return;
4201 }
4202 cur = CUR;
4203 if (cur == '\\') {
4204 NEXT;
4205 cur = CUR;
4206 switch (cur) {
4207 case 'n': start = 0xA; break;
4208 case 'r': start = 0xD; break;
4209 case 't': start = 0x9; break;
4210 case '\\': case '|': case '.': case '-': case '^': case '?':
4211 case '*': case '+': case '{': case '}': case '(': case ')':
4212 case '[': case ']':
4213 start = cur; break;
4214 default:
4215 ERROR("Invalid escape value");
4216 return;
4217 }
4218 end = start;
William M. Brackdc99df92003-12-27 01:54:25 +00004219 len = 1;
Daniel Veillard4255d502002-04-16 15:50:10 +00004220 } else if ((cur != 0x5B) && (cur != 0x5D)) {
William M. Brackdc99df92003-12-27 01:54:25 +00004221 end = start = CUR_SCHAR(ctxt->cur, len);
Daniel Veillard4255d502002-04-16 15:50:10 +00004222 } else {
4223 ERROR("Expecting a char range");
4224 return;
4225 }
William M. Brackdc99df92003-12-27 01:54:25 +00004226 NEXTL(len);
Daniel Veillard4255d502002-04-16 15:50:10 +00004227 if (start == '-') {
4228 return;
4229 }
4230 cur = CUR;
William M. Brack10f1ef42004-03-20 14:51:25 +00004231 if ((cur != '-') || (NXT(1) == ']')) {
Daniel Veillard4255d502002-04-16 15:50:10 +00004232 xmlRegAtomAddRange(ctxt, ctxt->atom, ctxt->neg,
4233 XML_REGEXP_CHARVAL, start, end, NULL);
4234 return;
4235 }
4236 NEXT;
4237 cur = CUR;
4238 if (cur == '\\') {
4239 NEXT;
4240 cur = CUR;
4241 switch (cur) {
4242 case 'n': end = 0xA; break;
4243 case 'r': end = 0xD; break;
4244 case 't': end = 0x9; break;
4245 case '\\': case '|': case '.': case '-': case '^': case '?':
4246 case '*': case '+': case '{': case '}': case '(': case ')':
4247 case '[': case ']':
4248 end = cur; break;
4249 default:
4250 ERROR("Invalid escape value");
4251 return;
4252 }
William M. Brackdc99df92003-12-27 01:54:25 +00004253 len = 1;
Daniel Veillard4255d502002-04-16 15:50:10 +00004254 } else if ((cur != 0x5B) && (cur != 0x5D)) {
William M. Brackdc99df92003-12-27 01:54:25 +00004255 end = CUR_SCHAR(ctxt->cur, len);
Daniel Veillard4255d502002-04-16 15:50:10 +00004256 } else {
4257 ERROR("Expecting the end of a char range");
4258 return;
4259 }
William M. Brackdc99df92003-12-27 01:54:25 +00004260 NEXTL(len);
Daniel Veillard4255d502002-04-16 15:50:10 +00004261 /* TODO check that the values are acceptable character ranges for XML */
4262 if (end < start) {
4263 ERROR("End of range is before start of range");
4264 } else {
4265 xmlRegAtomAddRange(ctxt, ctxt->atom, ctxt->neg,
4266 XML_REGEXP_CHARVAL, start, end, NULL);
4267 }
4268 return;
4269}
4270
4271/**
4272 * xmlFAParsePosCharGroup:
Daniel Veillard441bc322002-04-20 17:38:48 +00004273 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00004274 *
4275 * [14] posCharGroup ::= ( charRange | charClassEsc )+
4276 */
4277static void
4278xmlFAParsePosCharGroup(xmlRegParserCtxtPtr ctxt) {
4279 do {
4280 if ((CUR == '\\') || (CUR == '.')) {
4281 xmlFAParseCharClassEsc(ctxt);
4282 } else {
4283 xmlFAParseCharRange(ctxt);
4284 }
4285 } while ((CUR != ']') && (CUR != '^') && (CUR != '-') &&
4286 (ctxt->error == 0));
4287}
4288
4289/**
4290 * xmlFAParseCharGroup:
Daniel Veillard441bc322002-04-20 17:38:48 +00004291 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00004292 *
4293 * [13] charGroup ::= posCharGroup | negCharGroup | charClassSub
4294 * [15] negCharGroup ::= '^' posCharGroup
4295 * [16] charClassSub ::= ( posCharGroup | negCharGroup ) '-' charClassExpr
4296 * [12] charClassExpr ::= '[' charGroup ']'
4297 */
4298static void
4299xmlFAParseCharGroup(xmlRegParserCtxtPtr ctxt) {
4300 int n = ctxt->neg;
4301 while ((CUR != ']') && (ctxt->error == 0)) {
4302 if (CUR == '^') {
4303 int neg = ctxt->neg;
4304
4305 NEXT;
4306 ctxt->neg = !ctxt->neg;
4307 xmlFAParsePosCharGroup(ctxt);
4308 ctxt->neg = neg;
William M. Brack10f1ef42004-03-20 14:51:25 +00004309 } else if ((CUR == '-') && (NXT(1) == '[')) {
Daniel Veillardf8b9de32003-11-24 14:27:26 +00004310 int neg = ctxt->neg;
Daniel Veillardf8b9de32003-11-24 14:27:26 +00004311 ctxt->neg = 2;
William M. Brack10f1ef42004-03-20 14:51:25 +00004312 NEXT; /* eat the '-' */
4313 NEXT; /* eat the '[' */
Daniel Veillard4255d502002-04-16 15:50:10 +00004314 xmlFAParseCharGroup(ctxt);
4315 if (CUR == ']') {
4316 NEXT;
4317 } else {
4318 ERROR("charClassExpr: ']' expected");
4319 break;
4320 }
Daniel Veillardf8b9de32003-11-24 14:27:26 +00004321 ctxt->neg = neg;
Daniel Veillard4255d502002-04-16 15:50:10 +00004322 break;
4323 } else if (CUR != ']') {
4324 xmlFAParsePosCharGroup(ctxt);
4325 }
4326 }
4327 ctxt->neg = n;
4328}
4329
4330/**
4331 * xmlFAParseCharClass:
Daniel Veillard441bc322002-04-20 17:38:48 +00004332 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00004333 *
4334 * [11] charClass ::= charClassEsc | charClassExpr
4335 * [12] charClassExpr ::= '[' charGroup ']'
4336 */
4337static void
4338xmlFAParseCharClass(xmlRegParserCtxtPtr ctxt) {
4339 if (CUR == '[') {
4340 NEXT;
4341 ctxt->atom = xmlRegNewAtom(ctxt, XML_REGEXP_RANGES);
4342 if (ctxt->atom == NULL)
4343 return;
4344 xmlFAParseCharGroup(ctxt);
4345 if (CUR == ']') {
4346 NEXT;
4347 } else {
4348 ERROR("xmlFAParseCharClass: ']' expected");
4349 }
4350 } else {
4351 xmlFAParseCharClassEsc(ctxt);
4352 }
4353}
4354
4355/**
4356 * xmlFAParseQuantExact:
Daniel Veillard441bc322002-04-20 17:38:48 +00004357 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00004358 *
4359 * [8] QuantExact ::= [0-9]+
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00004360 *
4361 * Returns 0 if success or -1 in case of error
Daniel Veillard4255d502002-04-16 15:50:10 +00004362 */
4363static int
4364xmlFAParseQuantExact(xmlRegParserCtxtPtr ctxt) {
4365 int ret = 0;
4366 int ok = 0;
4367
4368 while ((CUR >= '0') && (CUR <= '9')) {
4369 ret = ret * 10 + (CUR - '0');
4370 ok = 1;
4371 NEXT;
4372 }
4373 if (ok != 1) {
4374 return(-1);
4375 }
4376 return(ret);
4377}
4378
4379/**
4380 * xmlFAParseQuantifier:
Daniel Veillard441bc322002-04-20 17:38:48 +00004381 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00004382 *
4383 * [4] quantifier ::= [?*+] | ( '{' quantity '}' )
4384 * [5] quantity ::= quantRange | quantMin | QuantExact
4385 * [6] quantRange ::= QuantExact ',' QuantExact
4386 * [7] quantMin ::= QuantExact ','
4387 * [8] QuantExact ::= [0-9]+
4388 */
4389static int
4390xmlFAParseQuantifier(xmlRegParserCtxtPtr ctxt) {
4391 int cur;
4392
4393 cur = CUR;
4394 if ((cur == '?') || (cur == '*') || (cur == '+')) {
4395 if (ctxt->atom != NULL) {
4396 if (cur == '?')
4397 ctxt->atom->quant = XML_REGEXP_QUANT_OPT;
4398 else if (cur == '*')
4399 ctxt->atom->quant = XML_REGEXP_QUANT_MULT;
4400 else if (cur == '+')
4401 ctxt->atom->quant = XML_REGEXP_QUANT_PLUS;
4402 }
4403 NEXT;
4404 return(1);
4405 }
4406 if (cur == '{') {
4407 int min = 0, max = 0;
4408
4409 NEXT;
4410 cur = xmlFAParseQuantExact(ctxt);
4411 if (cur >= 0)
4412 min = cur;
4413 if (CUR == ',') {
4414 NEXT;
Daniel Veillardebe48c62003-12-03 12:12:27 +00004415 if (CUR == '}')
4416 max = INT_MAX;
4417 else {
4418 cur = xmlFAParseQuantExact(ctxt);
4419 if (cur >= 0)
4420 max = cur;
4421 else {
4422 ERROR("Improper quantifier");
4423 }
4424 }
Daniel Veillard4255d502002-04-16 15:50:10 +00004425 }
4426 if (CUR == '}') {
4427 NEXT;
4428 } else {
4429 ERROR("Unterminated quantifier");
4430 }
4431 if (max == 0)
4432 max = min;
4433 if (ctxt->atom != NULL) {
4434 ctxt->atom->quant = XML_REGEXP_QUANT_RANGE;
4435 ctxt->atom->min = min;
4436 ctxt->atom->max = max;
4437 }
4438 return(1);
4439 }
4440 return(0);
4441}
4442
4443/**
4444 * xmlFAParseAtom:
Daniel Veillard441bc322002-04-20 17:38:48 +00004445 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00004446 *
4447 * [9] atom ::= Char | charClass | ( '(' regExp ')' )
4448 */
4449static int
4450xmlFAParseAtom(xmlRegParserCtxtPtr ctxt) {
4451 int codepoint, len;
4452
4453 codepoint = xmlFAIsChar(ctxt);
4454 if (codepoint > 0) {
4455 ctxt->atom = xmlRegNewAtom(ctxt, XML_REGEXP_CHARVAL);
4456 if (ctxt->atom == NULL)
4457 return(-1);
4458 codepoint = CUR_SCHAR(ctxt->cur, len);
4459 ctxt->atom->codepoint = codepoint;
4460 NEXTL(len);
4461 return(1);
4462 } else if (CUR == '|') {
4463 return(0);
4464 } else if (CUR == 0) {
4465 return(0);
4466 } else if (CUR == ')') {
4467 return(0);
4468 } else if (CUR == '(') {
4469 xmlRegStatePtr start, oldend;
4470
4471 NEXT;
4472 xmlFAGenerateEpsilonTransition(ctxt, ctxt->state, NULL);
4473 start = ctxt->state;
4474 oldend = ctxt->end;
4475 ctxt->end = NULL;
4476 ctxt->atom = NULL;
4477 xmlFAParseRegExp(ctxt, 0);
4478 if (CUR == ')') {
4479 NEXT;
4480 } else {
4481 ERROR("xmlFAParseAtom: expecting ')'");
4482 }
4483 ctxt->atom = xmlRegNewAtom(ctxt, XML_REGEXP_SUBREG);
4484 if (ctxt->atom == NULL)
4485 return(-1);
4486 ctxt->atom->start = start;
4487 ctxt->atom->stop = ctxt->state;
4488 ctxt->end = oldend;
4489 return(1);
4490 } else if ((CUR == '[') || (CUR == '\\') || (CUR == '.')) {
4491 xmlFAParseCharClass(ctxt);
4492 return(1);
4493 }
4494 return(0);
4495}
4496
4497/**
4498 * xmlFAParsePiece:
Daniel Veillard441bc322002-04-20 17:38:48 +00004499 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00004500 *
4501 * [3] piece ::= atom quantifier?
4502 */
4503static int
4504xmlFAParsePiece(xmlRegParserCtxtPtr ctxt) {
4505 int ret;
4506
4507 ctxt->atom = NULL;
4508 ret = xmlFAParseAtom(ctxt);
4509 if (ret == 0)
4510 return(0);
4511 if (ctxt->atom == NULL) {
4512 ERROR("internal: no atom generated");
4513 }
4514 xmlFAParseQuantifier(ctxt);
4515 return(1);
4516}
4517
4518/**
4519 * xmlFAParseBranch:
Daniel Veillard441bc322002-04-20 17:38:48 +00004520 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00004521 *
4522 * [2] branch ::= piece*
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00004523 8
Daniel Veillard4255d502002-04-16 15:50:10 +00004524 */
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00004525static int
Daniel Veillard2cbf5962004-03-31 15:50:43 +00004526xmlFAParseBranch(xmlRegParserCtxtPtr ctxt) {
Daniel Veillard4255d502002-04-16 15:50:10 +00004527 xmlRegStatePtr previous;
Daniel Veillard4255d502002-04-16 15:50:10 +00004528 int ret;
4529
4530 previous = ctxt->state;
4531 ret = xmlFAParsePiece(ctxt);
4532 if (ret != 0) {
Daniel Veillard2cbf5962004-03-31 15:50:43 +00004533 if (xmlFAGenerateTransitions(ctxt, previous, NULL, ctxt->atom) < 0)
4534 return(-1);
4535 previous = ctxt->state;
Daniel Veillard4255d502002-04-16 15:50:10 +00004536 ctxt->atom = NULL;
4537 }
4538 while ((ret != 0) && (ctxt->error == 0)) {
4539 ret = xmlFAParsePiece(ctxt);
4540 if (ret != 0) {
Daniel Veillard2cbf5962004-03-31 15:50:43 +00004541 if (xmlFAGenerateTransitions(ctxt, previous, NULL,
4542 ctxt->atom) < 0)
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00004543 return(-1);
Daniel Veillard4255d502002-04-16 15:50:10 +00004544 previous = ctxt->state;
4545 ctxt->atom = NULL;
4546 }
4547 }
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00004548 return(0);
Daniel Veillard4255d502002-04-16 15:50:10 +00004549}
4550
4551/**
4552 * xmlFAParseRegExp:
Daniel Veillard441bc322002-04-20 17:38:48 +00004553 * @ctxt: a regexp parser context
William M. Brackddf71d62004-05-06 04:17:26 +00004554 * @top: is this the top-level expression ?
Daniel Veillard4255d502002-04-16 15:50:10 +00004555 *
4556 * [1] regExp ::= branch ( '|' branch )*
4557 */
4558static void
4559xmlFAParseRegExp(xmlRegParserCtxtPtr ctxt, int top) {
Daniel Veillardc7e3cc42004-09-28 12:33:52 +00004560 xmlRegStatePtr start, end;
Daniel Veillard4255d502002-04-16 15:50:10 +00004561
Daniel Veillard2cbf5962004-03-31 15:50:43 +00004562 /* if not top start should have been generated by an epsilon trans */
Daniel Veillard4255d502002-04-16 15:50:10 +00004563 start = ctxt->state;
Daniel Veillard2cbf5962004-03-31 15:50:43 +00004564 ctxt->end = NULL;
4565 xmlFAParseBranch(ctxt);
4566 if (top) {
4567#ifdef DEBUG_REGEXP_GRAPH
4568 printf("State %d is final\n", ctxt->state->no);
4569#endif
4570 ctxt->state->type = XML_REGEXP_FINAL_STATE;
4571 }
Daniel Veillard4255d502002-04-16 15:50:10 +00004572 if (CUR != '|') {
4573 ctxt->end = ctxt->state;
4574 return;
4575 }
4576 end = ctxt->state;
4577 while ((CUR == '|') && (ctxt->error == 0)) {
4578 NEXT;
4579 ctxt->state = start;
Daniel Veillard2cbf5962004-03-31 15:50:43 +00004580 ctxt->end = NULL;
4581 xmlFAParseBranch(ctxt);
4582 if (top) {
4583 ctxt->state->type = XML_REGEXP_FINAL_STATE;
4584#ifdef DEBUG_REGEXP_GRAPH
4585 printf("State %d is final\n", ctxt->state->no);
4586#endif
4587 } else {
4588 xmlFAGenerateEpsilonTransition(ctxt, ctxt->state, end);
4589 }
Daniel Veillard4255d502002-04-16 15:50:10 +00004590 }
Daniel Veillard2cbf5962004-03-31 15:50:43 +00004591 if (!top) {
4592 ctxt->state = end;
4593 ctxt->end = end;
4594 }
Daniel Veillard4255d502002-04-16 15:50:10 +00004595}
4596
4597/************************************************************************
4598 * *
4599 * The basic API *
4600 * *
4601 ************************************************************************/
4602
4603/**
4604 * xmlRegexpPrint:
4605 * @output: the file for the output debug
4606 * @regexp: the compiled regexp
4607 *
4608 * Print the content of the compiled regular expression
4609 */
4610void
4611xmlRegexpPrint(FILE *output, xmlRegexpPtr regexp) {
4612 int i;
4613
Daniel Veillarda82b1822004-11-08 16:24:57 +00004614 if (output == NULL)
4615 return;
Daniel Veillard4255d502002-04-16 15:50:10 +00004616 fprintf(output, " regexp: ");
4617 if (regexp == NULL) {
4618 fprintf(output, "NULL\n");
4619 return;
4620 }
4621 fprintf(output, "'%s' ", regexp->string);
4622 fprintf(output, "\n");
4623 fprintf(output, "%d atoms:\n", regexp->nbAtoms);
4624 for (i = 0;i < regexp->nbAtoms; i++) {
4625 fprintf(output, " %02d ", i);
4626 xmlRegPrintAtom(output, regexp->atoms[i]);
4627 }
4628 fprintf(output, "%d states:", regexp->nbStates);
4629 fprintf(output, "\n");
4630 for (i = 0;i < regexp->nbStates; i++) {
4631 xmlRegPrintState(output, regexp->states[i]);
4632 }
4633 fprintf(output, "%d counters:\n", regexp->nbCounters);
4634 for (i = 0;i < regexp->nbCounters; i++) {
4635 fprintf(output, " %d: min %d max %d\n", i, regexp->counters[i].min,
4636 regexp->counters[i].max);
4637 }
4638}
4639
4640/**
4641 * xmlRegexpCompile:
4642 * @regexp: a regular expression string
4643 *
4644 * Parses a regular expression conforming to XML Schemas Part 2 Datatype
William M. Brackddf71d62004-05-06 04:17:26 +00004645 * Appendix F and builds an automata suitable for testing strings against
Daniel Veillard4255d502002-04-16 15:50:10 +00004646 * that regular expression
4647 *
4648 * Returns the compiled expression or NULL in case of error
4649 */
4650xmlRegexpPtr
4651xmlRegexpCompile(const xmlChar *regexp) {
4652 xmlRegexpPtr ret;
4653 xmlRegParserCtxtPtr ctxt;
4654
4655 ctxt = xmlRegNewParserCtxt(regexp);
4656 if (ctxt == NULL)
4657 return(NULL);
4658
4659 /* initialize the parser */
4660 ctxt->end = NULL;
4661 ctxt->start = ctxt->state = xmlRegNewState(ctxt);
4662 xmlRegStatePush(ctxt, ctxt->start);
4663
4664 /* parse the expression building an automata */
4665 xmlFAParseRegExp(ctxt, 1);
4666 if (CUR != 0) {
4667 ERROR("xmlFAParseRegExp: extra characters");
4668 }
4669 ctxt->end = ctxt->state;
4670 ctxt->start->type = XML_REGEXP_START_STATE;
4671 ctxt->end->type = XML_REGEXP_FINAL_STATE;
4672
4673 /* remove the Epsilon except for counted transitions */
4674 xmlFAEliminateEpsilonTransitions(ctxt);
4675
4676
4677 if (ctxt->error != 0) {
4678 xmlRegFreeParserCtxt(ctxt);
4679 return(NULL);
4680 }
4681 ret = xmlRegEpxFromParse(ctxt);
4682 xmlRegFreeParserCtxt(ctxt);
4683 return(ret);
4684}
4685
4686/**
4687 * xmlRegexpExec:
4688 * @comp: the compiled regular expression
4689 * @content: the value to check against the regular expression
4690 *
William M. Brackddf71d62004-05-06 04:17:26 +00004691 * Check if the regular expression generates the value
Daniel Veillard4255d502002-04-16 15:50:10 +00004692 *
William M. Brackddf71d62004-05-06 04:17:26 +00004693 * Returns 1 if it matches, 0 if not and a negative value in case of error
Daniel Veillard4255d502002-04-16 15:50:10 +00004694 */
4695int
4696xmlRegexpExec(xmlRegexpPtr comp, const xmlChar *content) {
4697 if ((comp == NULL) || (content == NULL))
4698 return(-1);
4699 return(xmlFARegExec(comp, content));
4700}
4701
4702/**
Daniel Veillard23e73572002-09-19 19:56:43 +00004703 * xmlRegexpIsDeterminist:
4704 * @comp: the compiled regular expression
4705 *
4706 * Check if the regular expression is determinist
4707 *
William M. Brackddf71d62004-05-06 04:17:26 +00004708 * Returns 1 if it yes, 0 if not and a negative value in case of error
Daniel Veillard23e73572002-09-19 19:56:43 +00004709 */
4710int
4711xmlRegexpIsDeterminist(xmlRegexpPtr comp) {
4712 xmlAutomataPtr am;
4713 int ret;
4714
4715 if (comp == NULL)
4716 return(-1);
4717 if (comp->determinist != -1)
4718 return(comp->determinist);
4719
4720 am = xmlNewAutomata();
Daniel Veillardbd9afb52002-09-25 22:25:35 +00004721 if (am->states != NULL) {
4722 int i;
4723
4724 for (i = 0;i < am->nbStates;i++)
4725 xmlRegFreeState(am->states[i]);
4726 xmlFree(am->states);
4727 }
Daniel Veillard23e73572002-09-19 19:56:43 +00004728 am->nbAtoms = comp->nbAtoms;
4729 am->atoms = comp->atoms;
4730 am->nbStates = comp->nbStates;
4731 am->states = comp->states;
4732 am->determinist = -1;
4733 ret = xmlFAComputesDeterminism(am);
4734 am->atoms = NULL;
4735 am->states = NULL;
4736 xmlFreeAutomata(am);
4737 return(ret);
4738}
4739
4740/**
Daniel Veillard4255d502002-04-16 15:50:10 +00004741 * xmlRegFreeRegexp:
4742 * @regexp: the regexp
4743 *
4744 * Free a regexp
4745 */
4746void
4747xmlRegFreeRegexp(xmlRegexpPtr regexp) {
4748 int i;
4749 if (regexp == NULL)
4750 return;
4751
4752 if (regexp->string != NULL)
4753 xmlFree(regexp->string);
4754 if (regexp->states != NULL) {
4755 for (i = 0;i < regexp->nbStates;i++)
4756 xmlRegFreeState(regexp->states[i]);
4757 xmlFree(regexp->states);
4758 }
4759 if (regexp->atoms != NULL) {
4760 for (i = 0;i < regexp->nbAtoms;i++)
4761 xmlRegFreeAtom(regexp->atoms[i]);
4762 xmlFree(regexp->atoms);
4763 }
4764 if (regexp->counters != NULL)
4765 xmlFree(regexp->counters);
Daniel Veillard23e73572002-09-19 19:56:43 +00004766 if (regexp->compact != NULL)
4767 xmlFree(regexp->compact);
Daniel Veillard118aed72002-09-24 14:13:13 +00004768 if (regexp->transdata != NULL)
4769 xmlFree(regexp->transdata);
Daniel Veillard23e73572002-09-19 19:56:43 +00004770 if (regexp->stringMap != NULL) {
4771 for (i = 0; i < regexp->nbstrings;i++)
4772 xmlFree(regexp->stringMap[i]);
4773 xmlFree(regexp->stringMap);
4774 }
4775
Daniel Veillard4255d502002-04-16 15:50:10 +00004776 xmlFree(regexp);
4777}
4778
4779#ifdef LIBXML_AUTOMATA_ENABLED
4780/************************************************************************
4781 * *
4782 * The Automata interface *
4783 * *
4784 ************************************************************************/
4785
4786/**
4787 * xmlNewAutomata:
4788 *
4789 * Create a new automata
4790 *
4791 * Returns the new object or NULL in case of failure
4792 */
4793xmlAutomataPtr
4794xmlNewAutomata(void) {
4795 xmlAutomataPtr ctxt;
4796
4797 ctxt = xmlRegNewParserCtxt(NULL);
4798 if (ctxt == NULL)
4799 return(NULL);
4800
4801 /* initialize the parser */
4802 ctxt->end = NULL;
4803 ctxt->start = ctxt->state = xmlRegNewState(ctxt);
Daniel Veillarddb68b742005-07-30 13:18:24 +00004804 ctxt->start->type = XML_REGEXP_START_STATE;
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00004805 if (ctxt->start == NULL) {
4806 xmlFreeAutomata(ctxt);
4807 return(NULL);
4808 }
4809 if (xmlRegStatePush(ctxt, ctxt->start) < 0) {
4810 xmlRegFreeState(ctxt->start);
4811 xmlFreeAutomata(ctxt);
4812 return(NULL);
4813 }
Daniel Veillard4255d502002-04-16 15:50:10 +00004814
4815 return(ctxt);
4816}
4817
4818/**
4819 * xmlFreeAutomata:
4820 * @am: an automata
4821 *
4822 * Free an automata
4823 */
4824void
4825xmlFreeAutomata(xmlAutomataPtr am) {
4826 if (am == NULL)
4827 return;
4828 xmlRegFreeParserCtxt(am);
4829}
4830
4831/**
4832 * xmlAutomataGetInitState:
4833 * @am: an automata
4834 *
Daniel Veillarda9b66d02002-12-11 14:23:49 +00004835 * Initial state lookup
4836 *
Daniel Veillard4255d502002-04-16 15:50:10 +00004837 * Returns the initial state of the automata
4838 */
4839xmlAutomataStatePtr
4840xmlAutomataGetInitState(xmlAutomataPtr am) {
4841 if (am == NULL)
4842 return(NULL);
4843 return(am->start);
4844}
4845
4846/**
4847 * xmlAutomataSetFinalState:
4848 * @am: an automata
4849 * @state: a state in this automata
4850 *
4851 * Makes that state a final state
4852 *
4853 * Returns 0 or -1 in case of error
4854 */
4855int
4856xmlAutomataSetFinalState(xmlAutomataPtr am, xmlAutomataStatePtr state) {
4857 if ((am == NULL) || (state == NULL))
4858 return(-1);
4859 state->type = XML_REGEXP_FINAL_STATE;
4860 return(0);
4861}
4862
4863/**
4864 * xmlAutomataNewTransition:
4865 * @am: an automata
4866 * @from: the starting point of the transition
4867 * @to: the target point of the transition or NULL
4868 * @token: the input string associated to that transition
4869 * @data: data passed to the callback function if the transition is activated
4870 *
William M. Brackddf71d62004-05-06 04:17:26 +00004871 * If @to is NULL, this creates first a new target state in the automata
Daniel Veillard4255d502002-04-16 15:50:10 +00004872 * and then adds a transition from the @from state to the target state
4873 * activated by the value of @token
4874 *
4875 * Returns the target state or NULL in case of error
4876 */
4877xmlAutomataStatePtr
4878xmlAutomataNewTransition(xmlAutomataPtr am, xmlAutomataStatePtr from,
4879 xmlAutomataStatePtr to, const xmlChar *token,
4880 void *data) {
4881 xmlRegAtomPtr atom;
4882
4883 if ((am == NULL) || (from == NULL) || (token == NULL))
4884 return(NULL);
4885 atom = xmlRegNewAtom(am, XML_REGEXP_STRING);
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00004886 if (atom == NULL)
4887 return(NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +00004888 atom->data = data;
4889 if (atom == NULL)
4890 return(NULL);
4891 atom->valuep = xmlStrdup(token);
4892
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00004893 if (xmlFAGenerateTransitions(am, from, to, atom) < 0) {
4894 xmlRegFreeAtom(atom);
4895 return(NULL);
4896 }
Daniel Veillard4255d502002-04-16 15:50:10 +00004897 if (to == NULL)
4898 return(am->state);
4899 return(to);
4900}
4901
4902/**
Daniel Veillard52b48c72003-04-13 19:53:42 +00004903 * xmlAutomataNewTransition2:
4904 * @am: an automata
4905 * @from: the starting point of the transition
4906 * @to: the target point of the transition or NULL
4907 * @token: the first input string associated to that transition
4908 * @token2: the second input string associated to that transition
4909 * @data: data passed to the callback function if the transition is activated
4910 *
William M. Brackddf71d62004-05-06 04:17:26 +00004911 * If @to is NULL, this creates first a new target state in the automata
Daniel Veillard52b48c72003-04-13 19:53:42 +00004912 * and then adds a transition from the @from state to the target state
4913 * activated by the value of @token
4914 *
4915 * Returns the target state or NULL in case of error
4916 */
4917xmlAutomataStatePtr
4918xmlAutomataNewTransition2(xmlAutomataPtr am, xmlAutomataStatePtr from,
4919 xmlAutomataStatePtr to, const xmlChar *token,
4920 const xmlChar *token2, void *data) {
4921 xmlRegAtomPtr atom;
4922
4923 if ((am == NULL) || (from == NULL) || (token == NULL))
4924 return(NULL);
4925 atom = xmlRegNewAtom(am, XML_REGEXP_STRING);
4926 atom->data = data;
4927 if (atom == NULL)
4928 return(NULL);
4929 if ((token2 == NULL) || (*token2 == 0)) {
4930 atom->valuep = xmlStrdup(token);
4931 } else {
4932 int lenn, lenp;
4933 xmlChar *str;
4934
4935 lenn = strlen((char *) token2);
4936 lenp = strlen((char *) token);
4937
Daniel Veillard3c908dc2003-04-19 00:07:51 +00004938 str = (xmlChar *) xmlMallocAtomic(lenn + lenp + 2);
Daniel Veillard52b48c72003-04-13 19:53:42 +00004939 if (str == NULL) {
4940 xmlRegFreeAtom(atom);
4941 return(NULL);
4942 }
4943 memcpy(&str[0], token, lenp);
4944 str[lenp] = '|';
4945 memcpy(&str[lenp + 1], token2, lenn);
4946 str[lenn + lenp + 1] = 0;
4947
4948 atom->valuep = str;
4949 }
4950
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00004951 if (xmlFAGenerateTransitions(am, from, to, atom) < 0) {
4952 xmlRegFreeAtom(atom);
4953 return(NULL);
4954 }
Daniel Veillard52b48c72003-04-13 19:53:42 +00004955 if (to == NULL)
4956 return(am->state);
4957 return(to);
4958}
4959
4960/**
Daniel Veillard9efc4762005-07-19 14:33:55 +00004961 * xmlAutomataNewNegTrans:
4962 * @am: an automata
4963 * @from: the starting point of the transition
4964 * @to: the target point of the transition or NULL
4965 * @token: the first input string associated to that transition
4966 * @token2: the second input string associated to that transition
4967 * @data: data passed to the callback function if the transition is activated
4968 *
4969 * If @to is NULL, this creates first a new target state in the automata
4970 * and then adds a transition from the @from state to the target state
4971 * activated by any value except (@token,@token2)
Daniel Veillard6e65e152005-08-09 11:09:52 +00004972 * Note that if @token2 is not NULL, then (X, NULL) won't match to follow
4973 # the semantic of XSD ##other
Daniel Veillard9efc4762005-07-19 14:33:55 +00004974 *
4975 * Returns the target state or NULL in case of error
4976 */
4977xmlAutomataStatePtr
4978xmlAutomataNewNegTrans(xmlAutomataPtr am, xmlAutomataStatePtr from,
4979 xmlAutomataStatePtr to, const xmlChar *token,
4980 const xmlChar *token2, void *data) {
4981 xmlRegAtomPtr atom;
Daniel Veillard77005e62005-07-19 16:26:18 +00004982 xmlChar err_msg[200];
Daniel Veillard9efc4762005-07-19 14:33:55 +00004983
4984 if ((am == NULL) || (from == NULL) || (token == NULL))
4985 return(NULL);
4986 atom = xmlRegNewAtom(am, XML_REGEXP_STRING);
4987 if (atom == NULL)
4988 return(NULL);
4989 atom->data = data;
4990 atom->neg = 1;
4991 if ((token2 == NULL) || (*token2 == 0)) {
4992 atom->valuep = xmlStrdup(token);
4993 } else {
4994 int lenn, lenp;
4995 xmlChar *str;
4996
4997 lenn = strlen((char *) token2);
4998 lenp = strlen((char *) token);
4999
5000 str = (xmlChar *) xmlMallocAtomic(lenn + lenp + 2);
5001 if (str == NULL) {
5002 xmlRegFreeAtom(atom);
5003 return(NULL);
5004 }
5005 memcpy(&str[0], token, lenp);
5006 str[lenp] = '|';
5007 memcpy(&str[lenp + 1], token2, lenn);
5008 str[lenn + lenp + 1] = 0;
5009
5010 atom->valuep = str;
5011 }
Daniel Veillarddb68b742005-07-30 13:18:24 +00005012 snprintf((char *) err_msg, 199, "not %s", (const char *) atom->valuep);
Daniel Veillard77005e62005-07-19 16:26:18 +00005013 err_msg[199] = 0;
5014 atom->valuep2 = xmlStrdup(err_msg);
Daniel Veillard9efc4762005-07-19 14:33:55 +00005015
5016 if (xmlFAGenerateTransitions(am, from, to, atom) < 0) {
5017 xmlRegFreeAtom(atom);
5018 return(NULL);
5019 }
Daniel Veillard6e65e152005-08-09 11:09:52 +00005020 am->negs++;
Daniel Veillard9efc4762005-07-19 14:33:55 +00005021 if (to == NULL)
5022 return(am->state);
5023 return(to);
5024}
5025
5026/**
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00005027 * xmlAutomataNewCountTrans2:
5028 * @am: an automata
5029 * @from: the starting point of the transition
5030 * @to: the target point of the transition or NULL
5031 * @token: the input string associated to that transition
5032 * @token2: the second input string associated to that transition
5033 * @min: the minimum successive occurences of token
5034 * @max: the maximum successive occurences of token
5035 * @data: data associated to the transition
5036 *
5037 * If @to is NULL, this creates first a new target state in the automata
5038 * and then adds a transition from the @from state to the target state
5039 * activated by a succession of input of value @token and @token2 and
5040 * whose number is between @min and @max
5041 *
5042 * Returns the target state or NULL in case of error
5043 */
5044xmlAutomataStatePtr
5045xmlAutomataNewCountTrans2(xmlAutomataPtr am, xmlAutomataStatePtr from,
5046 xmlAutomataStatePtr to, const xmlChar *token,
5047 const xmlChar *token2,
5048 int min, int max, void *data) {
5049 xmlRegAtomPtr atom;
5050 int counter;
5051
5052 if ((am == NULL) || (from == NULL) || (token == NULL))
5053 return(NULL);
5054 if (min < 0)
5055 return(NULL);
5056 if ((max < min) || (max < 1))
5057 return(NULL);
5058 atom = xmlRegNewAtom(am, XML_REGEXP_STRING);
5059 if (atom == NULL)
5060 return(NULL);
5061 if ((token2 == NULL) || (*token2 == 0)) {
5062 atom->valuep = xmlStrdup(token);
5063 } else {
5064 int lenn, lenp;
5065 xmlChar *str;
5066
5067 lenn = strlen((char *) token2);
5068 lenp = strlen((char *) token);
5069
5070 str = (xmlChar *) xmlMallocAtomic(lenn + lenp + 2);
5071 if (str == NULL) {
5072 xmlRegFreeAtom(atom);
5073 return(NULL);
5074 }
5075 memcpy(&str[0], token, lenp);
5076 str[lenp] = '|';
5077 memcpy(&str[lenp + 1], token2, lenn);
5078 str[lenn + lenp + 1] = 0;
5079
5080 atom->valuep = str;
5081 }
5082 atom->data = data;
5083 if (min == 0)
5084 atom->min = 1;
5085 else
5086 atom->min = min;
5087 atom->max = max;
5088
5089 /*
5090 * associate a counter to the transition.
5091 */
5092 counter = xmlRegGetCounter(am);
5093 am->counters[counter].min = min;
5094 am->counters[counter].max = max;
5095
5096 /* xmlFAGenerateTransitions(am, from, to, atom); */
5097 if (to == NULL) {
5098 to = xmlRegNewState(am);
5099 xmlRegStatePush(am, to);
5100 }
Daniel Veillard5de09382005-09-26 17:18:17 +00005101 xmlRegStateAddTrans(am, from, atom, to, counter, -1);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00005102 xmlRegAtomPush(am, atom);
5103 am->state = to;
5104
5105 if (to == NULL)
5106 to = am->state;
5107 if (to == NULL)
5108 return(NULL);
5109 if (min == 0)
5110 xmlFAGenerateEpsilonTransition(am, from, to);
5111 return(to);
5112}
5113
5114/**
Daniel Veillard4255d502002-04-16 15:50:10 +00005115 * xmlAutomataNewCountTrans:
5116 * @am: an automata
5117 * @from: the starting point of the transition
5118 * @to: the target point of the transition or NULL
5119 * @token: the input string associated to that transition
5120 * @min: the minimum successive occurences of token
Daniel Veillarda9b66d02002-12-11 14:23:49 +00005121 * @max: the maximum successive occurences of token
5122 * @data: data associated to the transition
Daniel Veillard4255d502002-04-16 15:50:10 +00005123 *
William M. Brackddf71d62004-05-06 04:17:26 +00005124 * If @to is NULL, this creates first a new target state in the automata
Daniel Veillard4255d502002-04-16 15:50:10 +00005125 * and then adds a transition from the @from state to the target state
5126 * activated by a succession of input of value @token and whose number
5127 * is between @min and @max
5128 *
5129 * Returns the target state or NULL in case of error
5130 */
5131xmlAutomataStatePtr
5132xmlAutomataNewCountTrans(xmlAutomataPtr am, xmlAutomataStatePtr from,
5133 xmlAutomataStatePtr to, const xmlChar *token,
5134 int min, int max, void *data) {
5135 xmlRegAtomPtr atom;
Daniel Veillard0ddb21c2004-02-12 12:43:49 +00005136 int counter;
Daniel Veillard4255d502002-04-16 15:50:10 +00005137
5138 if ((am == NULL) || (from == NULL) || (token == NULL))
5139 return(NULL);
5140 if (min < 0)
5141 return(NULL);
5142 if ((max < min) || (max < 1))
5143 return(NULL);
5144 atom = xmlRegNewAtom(am, XML_REGEXP_STRING);
5145 if (atom == NULL)
5146 return(NULL);
5147 atom->valuep = xmlStrdup(token);
5148 atom->data = data;
5149 if (min == 0)
5150 atom->min = 1;
5151 else
5152 atom->min = min;
5153 atom->max = max;
5154
Daniel Veillard0ddb21c2004-02-12 12:43:49 +00005155 /*
5156 * associate a counter to the transition.
5157 */
5158 counter = xmlRegGetCounter(am);
5159 am->counters[counter].min = min;
5160 am->counters[counter].max = max;
5161
5162 /* xmlFAGenerateTransitions(am, from, to, atom); */
5163 if (to == NULL) {
5164 to = xmlRegNewState(am);
5165 xmlRegStatePush(am, to);
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00005166 }
Daniel Veillard5de09382005-09-26 17:18:17 +00005167 xmlRegStateAddTrans(am, from, atom, to, counter, -1);
Daniel Veillard0ddb21c2004-02-12 12:43:49 +00005168 xmlRegAtomPush(am, atom);
5169 am->state = to;
5170
Daniel Veillard4255d502002-04-16 15:50:10 +00005171 if (to == NULL)
5172 to = am->state;
5173 if (to == NULL)
5174 return(NULL);
5175 if (min == 0)
5176 xmlFAGenerateEpsilonTransition(am, from, to);
5177 return(to);
5178}
5179
5180/**
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00005181 * xmlAutomataNewOnceTrans2:
5182 * @am: an automata
5183 * @from: the starting point of the transition
5184 * @to: the target point of the transition or NULL
5185 * @token: the input string associated to that transition
5186 * @token2: the second input string associated to that transition
5187 * @min: the minimum successive occurences of token
5188 * @max: the maximum successive occurences of token
5189 * @data: data associated to the transition
5190 *
5191 * If @to is NULL, this creates first a new target state in the automata
5192 * and then adds a transition from the @from state to the target state
5193 * activated by a succession of input of value @token and @token2 and whose
5194 * number is between @min and @max, moreover that transition can only be
5195 * crossed once.
5196 *
5197 * Returns the target state or NULL in case of error
5198 */
5199xmlAutomataStatePtr
5200xmlAutomataNewOnceTrans2(xmlAutomataPtr am, xmlAutomataStatePtr from,
5201 xmlAutomataStatePtr to, const xmlChar *token,
5202 const xmlChar *token2,
5203 int min, int max, void *data) {
5204 xmlRegAtomPtr atom;
5205 int counter;
5206
5207 if ((am == NULL) || (from == NULL) || (token == NULL))
5208 return(NULL);
5209 if (min < 1)
5210 return(NULL);
5211 if ((max < min) || (max < 1))
5212 return(NULL);
5213 atom = xmlRegNewAtom(am, XML_REGEXP_STRING);
5214 if (atom == NULL)
5215 return(NULL);
5216 if ((token2 == NULL) || (*token2 == 0)) {
5217 atom->valuep = xmlStrdup(token);
5218 } else {
5219 int lenn, lenp;
5220 xmlChar *str;
5221
5222 lenn = strlen((char *) token2);
5223 lenp = strlen((char *) token);
5224
5225 str = (xmlChar *) xmlMallocAtomic(lenn + lenp + 2);
5226 if (str == NULL) {
5227 xmlRegFreeAtom(atom);
5228 return(NULL);
5229 }
5230 memcpy(&str[0], token, lenp);
5231 str[lenp] = '|';
5232 memcpy(&str[lenp + 1], token2, lenn);
5233 str[lenn + lenp + 1] = 0;
5234
5235 atom->valuep = str;
5236 }
5237 atom->data = data;
5238 atom->quant = XML_REGEXP_QUANT_ONCEONLY;
5239 if (min == 0)
5240 atom->min = 1;
5241 else
5242 atom->min = min;
5243 atom->max = max;
5244 /*
5245 * associate a counter to the transition.
5246 */
5247 counter = xmlRegGetCounter(am);
5248 am->counters[counter].min = 1;
5249 am->counters[counter].max = 1;
5250
5251 /* xmlFAGenerateTransitions(am, from, to, atom); */
5252 if (to == NULL) {
5253 to = xmlRegNewState(am);
5254 xmlRegStatePush(am, to);
5255 }
Daniel Veillard5de09382005-09-26 17:18:17 +00005256 xmlRegStateAddTrans(am, from, atom, to, counter, -1);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00005257 xmlRegAtomPush(am, atom);
5258 am->state = to;
5259 return(to);
5260}
5261
5262
5263
5264/**
Daniel Veillard7646b182002-04-20 06:41:40 +00005265 * xmlAutomataNewOnceTrans:
5266 * @am: an automata
5267 * @from: the starting point of the transition
5268 * @to: the target point of the transition or NULL
5269 * @token: the input string associated to that transition
5270 * @min: the minimum successive occurences of token
Daniel Veillarda9b66d02002-12-11 14:23:49 +00005271 * @max: the maximum successive occurences of token
5272 * @data: data associated to the transition
Daniel Veillard7646b182002-04-20 06:41:40 +00005273 *
William M. Brackddf71d62004-05-06 04:17:26 +00005274 * If @to is NULL, this creates first a new target state in the automata
Daniel Veillard7646b182002-04-20 06:41:40 +00005275 * and then adds a transition from the @from state to the target state
5276 * activated by a succession of input of value @token and whose number
William M. Brackddf71d62004-05-06 04:17:26 +00005277 * is between @min and @max, moreover that transition can only be crossed
Daniel Veillard7646b182002-04-20 06:41:40 +00005278 * once.
5279 *
5280 * Returns the target state or NULL in case of error
5281 */
5282xmlAutomataStatePtr
5283xmlAutomataNewOnceTrans(xmlAutomataPtr am, xmlAutomataStatePtr from,
5284 xmlAutomataStatePtr to, const xmlChar *token,
5285 int min, int max, void *data) {
5286 xmlRegAtomPtr atom;
5287 int counter;
5288
5289 if ((am == NULL) || (from == NULL) || (token == NULL))
5290 return(NULL);
5291 if (min < 1)
5292 return(NULL);
5293 if ((max < min) || (max < 1))
5294 return(NULL);
5295 atom = xmlRegNewAtom(am, XML_REGEXP_STRING);
5296 if (atom == NULL)
5297 return(NULL);
5298 atom->valuep = xmlStrdup(token);
5299 atom->data = data;
5300 atom->quant = XML_REGEXP_QUANT_ONCEONLY;
5301 if (min == 0)
5302 atom->min = 1;
5303 else
5304 atom->min = min;
5305 atom->max = max;
5306 /*
5307 * associate a counter to the transition.
5308 */
5309 counter = xmlRegGetCounter(am);
5310 am->counters[counter].min = 1;
5311 am->counters[counter].max = 1;
5312
5313 /* xmlFAGenerateTransitions(am, from, to, atom); */
5314 if (to == NULL) {
5315 to = xmlRegNewState(am);
5316 xmlRegStatePush(am, to);
5317 }
Daniel Veillard5de09382005-09-26 17:18:17 +00005318 xmlRegStateAddTrans(am, from, atom, to, counter, -1);
Daniel Veillard7646b182002-04-20 06:41:40 +00005319 xmlRegAtomPush(am, atom);
5320 am->state = to;
Daniel Veillard7646b182002-04-20 06:41:40 +00005321 return(to);
5322}
5323
5324/**
Daniel Veillard4255d502002-04-16 15:50:10 +00005325 * xmlAutomataNewState:
5326 * @am: an automata
5327 *
5328 * Create a new disconnected state in the automata
5329 *
5330 * Returns the new state or NULL in case of error
5331 */
5332xmlAutomataStatePtr
5333xmlAutomataNewState(xmlAutomataPtr am) {
5334 xmlAutomataStatePtr to;
5335
5336 if (am == NULL)
5337 return(NULL);
5338 to = xmlRegNewState(am);
5339 xmlRegStatePush(am, to);
5340 return(to);
5341}
5342
5343/**
Daniel Veillarda9b66d02002-12-11 14:23:49 +00005344 * xmlAutomataNewEpsilon:
Daniel Veillard4255d502002-04-16 15:50:10 +00005345 * @am: an automata
5346 * @from: the starting point of the transition
5347 * @to: the target point of the transition or NULL
5348 *
William M. Brackddf71d62004-05-06 04:17:26 +00005349 * If @to is NULL, this creates first a new target state in the automata
5350 * and then adds an epsilon transition from the @from state to the
Daniel Veillard4255d502002-04-16 15:50:10 +00005351 * target state
5352 *
5353 * Returns the target state or NULL in case of error
5354 */
5355xmlAutomataStatePtr
5356xmlAutomataNewEpsilon(xmlAutomataPtr am, xmlAutomataStatePtr from,
5357 xmlAutomataStatePtr to) {
5358 if ((am == NULL) || (from == NULL))
5359 return(NULL);
5360 xmlFAGenerateEpsilonTransition(am, from, to);
5361 if (to == NULL)
5362 return(am->state);
5363 return(to);
5364}
5365
Daniel Veillardb509f152002-04-17 16:28:10 +00005366/**
Daniel Veillard7646b182002-04-20 06:41:40 +00005367 * xmlAutomataNewAllTrans:
5368 * @am: an automata
5369 * @from: the starting point of the transition
5370 * @to: the target point of the transition or NULL
Daniel Veillarda9b66d02002-12-11 14:23:49 +00005371 * @lax: allow to transition if not all all transitions have been activated
Daniel Veillard7646b182002-04-20 06:41:40 +00005372 *
William M. Brackddf71d62004-05-06 04:17:26 +00005373 * If @to is NULL, this creates first a new target state in the automata
Daniel Veillard7646b182002-04-20 06:41:40 +00005374 * and then adds a an ALL transition from the @from state to the
5375 * target state. That transition is an epsilon transition allowed only when
5376 * all transitions from the @from node have been activated.
5377 *
5378 * Returns the target state or NULL in case of error
5379 */
5380xmlAutomataStatePtr
5381xmlAutomataNewAllTrans(xmlAutomataPtr am, xmlAutomataStatePtr from,
Daniel Veillard441bc322002-04-20 17:38:48 +00005382 xmlAutomataStatePtr to, int lax) {
Daniel Veillard7646b182002-04-20 06:41:40 +00005383 if ((am == NULL) || (from == NULL))
5384 return(NULL);
Daniel Veillard441bc322002-04-20 17:38:48 +00005385 xmlFAGenerateAllTransition(am, from, to, lax);
Daniel Veillard7646b182002-04-20 06:41:40 +00005386 if (to == NULL)
5387 return(am->state);
5388 return(to);
5389}
5390
5391/**
Daniel Veillardb509f152002-04-17 16:28:10 +00005392 * xmlAutomataNewCounter:
5393 * @am: an automata
5394 * @min: the minimal value on the counter
5395 * @max: the maximal value on the counter
5396 *
5397 * Create a new counter
5398 *
5399 * Returns the counter number or -1 in case of error
5400 */
5401int
5402xmlAutomataNewCounter(xmlAutomataPtr am, int min, int max) {
5403 int ret;
5404
5405 if (am == NULL)
5406 return(-1);
5407
5408 ret = xmlRegGetCounter(am);
5409 if (ret < 0)
5410 return(-1);
5411 am->counters[ret].min = min;
5412 am->counters[ret].max = max;
5413 return(ret);
5414}
5415
5416/**
5417 * xmlAutomataNewCountedTrans:
5418 * @am: an automata
5419 * @from: the starting point of the transition
5420 * @to: the target point of the transition or NULL
5421 * @counter: the counter associated to that transition
5422 *
William M. Brackddf71d62004-05-06 04:17:26 +00005423 * If @to is NULL, this creates first a new target state in the automata
Daniel Veillardb509f152002-04-17 16:28:10 +00005424 * and then adds an epsilon transition from the @from state to the target state
5425 * which will increment the counter provided
5426 *
5427 * Returns the target state or NULL in case of error
5428 */
5429xmlAutomataStatePtr
5430xmlAutomataNewCountedTrans(xmlAutomataPtr am, xmlAutomataStatePtr from,
5431 xmlAutomataStatePtr to, int counter) {
5432 if ((am == NULL) || (from == NULL) || (counter < 0))
5433 return(NULL);
5434 xmlFAGenerateCountedEpsilonTransition(am, from, to, counter);
5435 if (to == NULL)
5436 return(am->state);
5437 return(to);
5438}
5439
5440/**
5441 * xmlAutomataNewCounterTrans:
5442 * @am: an automata
5443 * @from: the starting point of the transition
5444 * @to: the target point of the transition or NULL
5445 * @counter: the counter associated to that transition
5446 *
William M. Brackddf71d62004-05-06 04:17:26 +00005447 * If @to is NULL, this creates first a new target state in the automata
Daniel Veillardb509f152002-04-17 16:28:10 +00005448 * and then adds an epsilon transition from the @from state to the target state
5449 * which will be allowed only if the counter is within the right range.
5450 *
5451 * Returns the target state or NULL in case of error
5452 */
5453xmlAutomataStatePtr
5454xmlAutomataNewCounterTrans(xmlAutomataPtr am, xmlAutomataStatePtr from,
5455 xmlAutomataStatePtr to, int counter) {
5456 if ((am == NULL) || (from == NULL) || (counter < 0))
5457 return(NULL);
5458 xmlFAGenerateCountedTransition(am, from, to, counter);
5459 if (to == NULL)
5460 return(am->state);
5461 return(to);
5462}
Daniel Veillard4255d502002-04-16 15:50:10 +00005463
5464/**
5465 * xmlAutomataCompile:
5466 * @am: an automata
5467 *
5468 * Compile the automata into a Reg Exp ready for being executed.
5469 * The automata should be free after this point.
5470 *
5471 * Returns the compiled regexp or NULL in case of error
5472 */
5473xmlRegexpPtr
5474xmlAutomataCompile(xmlAutomataPtr am) {
5475 xmlRegexpPtr ret;
5476
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00005477 if ((am == NULL) || (am->error != 0)) return(NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +00005478 xmlFAEliminateEpsilonTransitions(am);
Daniel Veillard23e73572002-09-19 19:56:43 +00005479 /* xmlFAComputesDeterminism(am); */
Daniel Veillard4255d502002-04-16 15:50:10 +00005480 ret = xmlRegEpxFromParse(am);
5481
5482 return(ret);
5483}
Daniel Veillarde19fc232002-04-22 16:01:24 +00005484
5485/**
5486 * xmlAutomataIsDeterminist:
5487 * @am: an automata
5488 *
5489 * Checks if an automata is determinist.
5490 *
5491 * Returns 1 if true, 0 if not, and -1 in case of error
5492 */
5493int
5494xmlAutomataIsDeterminist(xmlAutomataPtr am) {
5495 int ret;
5496
5497 if (am == NULL)
5498 return(-1);
5499
5500 ret = xmlFAComputesDeterminism(am);
5501 return(ret);
5502}
Daniel Veillard4255d502002-04-16 15:50:10 +00005503#endif /* LIBXML_AUTOMATA_ENABLED */
Daniel Veillard81a8ec62005-08-22 00:20:58 +00005504
5505#ifdef LIBXML_EXPR_ENABLED
5506/************************************************************************
5507 * *
5508 * Formal Expression handling code *
5509 * *
5510 ************************************************************************/
Daniel Veillard81a8ec62005-08-22 00:20:58 +00005511/************************************************************************
5512 * *
5513 * Expression handling context *
5514 * *
5515 ************************************************************************/
5516
5517struct _xmlExpCtxt {
5518 xmlDictPtr dict;
5519 xmlExpNodePtr *table;
5520 int size;
5521 int nbElems;
5522 int nb_nodes;
5523 const char *expr;
5524 const char *cur;
5525 int nb_cons;
Daniel Veillard81a8ec62005-08-22 00:20:58 +00005526 int tabSize;
5527};
5528
5529/**
5530 * xmlExpNewCtxt:
5531 * @maxNodes: the maximum number of nodes
5532 * @dict: optional dictionnary to use internally
5533 *
5534 * Creates a new context for manipulating expressions
5535 *
5536 * Returns the context or NULL in case of error
5537 */
5538xmlExpCtxtPtr
5539xmlExpNewCtxt(int maxNodes, xmlDictPtr dict) {
5540 xmlExpCtxtPtr ret;
5541 int size = 256;
5542
5543 if (maxNodes <= 4096)
5544 maxNodes = 4096;
5545
5546 ret = (xmlExpCtxtPtr) xmlMalloc(sizeof(xmlExpCtxt));
5547 if (ret == NULL)
5548 return(NULL);
5549 memset(ret, 0, sizeof(xmlExpCtxt));
5550 ret->size = size;
5551 ret->nbElems = 0;
5552 ret->table = xmlMalloc(size * sizeof(xmlExpNodePtr));
5553 if (ret->table == NULL) {
5554 xmlFree(ret);
5555 return(NULL);
5556 }
5557 memset(ret->table, 0, size * sizeof(xmlExpNodePtr));
5558 if (dict == NULL) {
5559 ret->dict = xmlDictCreate();
5560 if (ret->dict == NULL) {
5561 xmlFree(ret->table);
5562 xmlFree(ret);
5563 return(NULL);
5564 }
5565 } else {
5566 ret->dict = dict;
5567 xmlDictReference(ret->dict);
5568 }
5569 return(ret);
5570}
5571
5572/**
5573 * xmlExpFreeCtxt:
5574 * @ctxt: an expression context
5575 *
5576 * Free an expression context
5577 */
5578void
5579xmlExpFreeCtxt(xmlExpCtxtPtr ctxt) {
5580 if (ctxt == NULL)
5581 return;
5582 xmlDictFree(ctxt->dict);
5583 if (ctxt->table != NULL)
5584 xmlFree(ctxt->table);
5585 xmlFree(ctxt);
5586}
5587
5588/************************************************************************
5589 * *
5590 * Structure associated to an expression node *
5591 * *
5592 ************************************************************************/
Daniel Veillard465a0002005-08-22 12:07:04 +00005593#define MAX_NODES 10000
5594
5595/* #define DEBUG_DERIV */
5596
5597/*
5598 * TODO:
5599 * - Wildcards
5600 * - public API for creation
5601 *
5602 * Started
5603 * - regression testing
5604 *
5605 * Done
5606 * - split into module and test tool
5607 * - memleaks
5608 */
Daniel Veillard81a8ec62005-08-22 00:20:58 +00005609
5610typedef enum {
5611 XML_EXP_NILABLE = (1 << 0)
5612} xmlExpNodeInfo;
5613
5614#define IS_NILLABLE(node) ((node)->info & XML_EXP_NILABLE)
5615
5616struct _xmlExpNode {
5617 unsigned char type;/* xmlExpNodeType */
5618 unsigned char info;/* OR of xmlExpNodeInfo */
5619 unsigned short key; /* the hash key */
5620 unsigned int ref; /* The number of references */
5621 int c_max; /* the maximum length it can consume */
5622 xmlExpNodePtr exp_left;
5623 xmlExpNodePtr next;/* the next node in the hash table or free list */
5624 union {
5625 struct {
5626 int f_min;
5627 int f_max;
5628 } count;
5629 struct {
5630 xmlExpNodePtr f_right;
5631 } children;
5632 const xmlChar *f_str;
5633 } field;
5634};
5635
5636#define exp_min field.count.f_min
5637#define exp_max field.count.f_max
5638/* #define exp_left field.children.f_left */
5639#define exp_right field.children.f_right
5640#define exp_str field.f_str
5641
5642static xmlExpNodePtr xmlExpNewNode(xmlExpCtxtPtr ctxt, xmlExpNodeType type);
5643static xmlExpNode forbiddenExpNode = {
5644 XML_EXP_FORBID, 0, 0, 0, 0, NULL, NULL, {{ 0, 0}}
5645};
5646xmlExpNodePtr forbiddenExp = &forbiddenExpNode;
5647static xmlExpNode emptyExpNode = {
5648 XML_EXP_EMPTY, 1, 0, 0, 0, NULL, NULL, {{ 0, 0}}
5649};
5650xmlExpNodePtr emptyExp = &emptyExpNode;
5651
5652/************************************************************************
5653 * *
5654 * The custom hash table for unicity and canonicalization *
5655 * of sub-expressions pointers *
5656 * *
5657 ************************************************************************/
5658/*
5659 * xmlExpHashNameComputeKey:
5660 * Calculate the hash key for a token
5661 */
5662static unsigned short
5663xmlExpHashNameComputeKey(const xmlChar *name) {
5664 unsigned short value = 0L;
5665 char ch;
5666
5667 if (name != NULL) {
5668 value += 30 * (*name);
5669 while ((ch = *name++) != 0) {
5670 value = value ^ ((value << 5) + (value >> 3) + (unsigned long)ch);
5671 }
5672 }
5673 return (value);
5674}
5675
5676/*
5677 * xmlExpHashComputeKey:
5678 * Calculate the hash key for a compound expression
5679 */
5680static unsigned short
5681xmlExpHashComputeKey(xmlExpNodeType type, xmlExpNodePtr left,
5682 xmlExpNodePtr right) {
5683 unsigned long value;
5684 unsigned short ret;
5685
5686 switch (type) {
5687 case XML_EXP_SEQ:
5688 value = left->key;
5689 value += right->key;
5690 value *= 3;
5691 ret = (unsigned short) value;
5692 break;
5693 case XML_EXP_OR:
5694 value = left->key;
5695 value += right->key;
5696 value *= 7;
5697 ret = (unsigned short) value;
5698 break;
5699 case XML_EXP_COUNT:
5700 value = left->key;
5701 value += right->key;
5702 ret = (unsigned short) value;
5703 break;
5704 default:
5705 ret = 0;
5706 }
5707 return(ret);
5708}
5709
5710
5711static xmlExpNodePtr
5712xmlExpNewNode(xmlExpCtxtPtr ctxt, xmlExpNodeType type) {
5713 xmlExpNodePtr ret;
5714
5715 if (ctxt->nb_nodes >= MAX_NODES)
5716 return(NULL);
5717 ret = (xmlExpNodePtr) xmlMalloc(sizeof(xmlExpNode));
5718 if (ret == NULL)
5719 return(NULL);
5720 memset(ret, 0, sizeof(xmlExpNode));
5721 ret->type = type;
5722 ret->next = NULL;
5723 ctxt->nb_nodes++;
5724 ctxt->nb_cons++;
5725 return(ret);
5726}
5727
5728/**
5729 * xmlExpHashGetEntry:
5730 * @table: the hash table
5731 *
5732 * Get the unique entry from the hash table. The entry is created if
5733 * needed. @left and @right are consumed, i.e. their ref count will
5734 * be decremented by the operation.
5735 *
5736 * Returns the pointer or NULL in case of error
5737 */
5738static xmlExpNodePtr
5739xmlExpHashGetEntry(xmlExpCtxtPtr ctxt, xmlExpNodeType type,
5740 xmlExpNodePtr left, xmlExpNodePtr right,
5741 const xmlChar *name, int min, int max) {
5742 unsigned short kbase, key;
5743 xmlExpNodePtr entry;
5744 xmlExpNodePtr insert;
5745
5746 if (ctxt == NULL)
5747 return(NULL);
5748
5749 /*
5750 * Check for duplicate and insertion location.
5751 */
5752 if (type == XML_EXP_ATOM) {
5753 kbase = xmlExpHashNameComputeKey(name);
5754 } else if (type == XML_EXP_COUNT) {
5755 /* COUNT reduction rule 1 */
5756 /* a{1} -> a */
5757 if (min == max) {
5758 if (min == 1) {
5759 return(left);
5760 }
5761 if (min == 0) {
5762 xmlExpFree(ctxt, left);
5763 return(emptyExp);
5764 }
5765 }
5766 if (min < 0) {
5767 xmlExpFree(ctxt, left);
5768 return(forbiddenExp);
5769 }
5770 if (max == -1)
5771 kbase = min + 79;
5772 else
5773 kbase = max - min;
5774 kbase += left->key;
5775 } else if (type == XML_EXP_OR) {
5776 /* Forbid reduction rules */
5777 if (left->type == XML_EXP_FORBID) {
5778 xmlExpFree(ctxt, left);
5779 return(right);
5780 }
5781 if (right->type == XML_EXP_FORBID) {
5782 xmlExpFree(ctxt, right);
5783 return(left);
5784 }
5785
5786 /* OR reduction rule 1 */
5787 /* a | a reduced to a */
5788 if (left == right) {
5789 left->ref--;
5790 return(left);
5791 }
5792 /* OR canonicalization rule 1 */
5793 /* linearize (a | b) | c into a | (b | c) */
5794 if ((left->type == XML_EXP_OR) && (right->type != XML_EXP_OR)) {
5795 xmlExpNodePtr tmp = left;
5796 left = right;
5797 right = tmp;
5798 }
5799 /* OR reduction rule 2 */
5800 /* a | (a | b) and b | (a | b) are reduced to a | b */
5801 if (right->type == XML_EXP_OR) {
5802 if ((left == right->exp_left) ||
5803 (left == right->exp_right)) {
5804 xmlExpFree(ctxt, left);
5805 return(right);
5806 }
5807 }
5808 /* OR canonicalization rule 2 */
5809 /* linearize (a | b) | c into a | (b | c) */
5810 if (left->type == XML_EXP_OR) {
5811 xmlExpNodePtr tmp;
5812
5813 /* OR canonicalization rule 2 */
5814 if ((left->exp_right->type != XML_EXP_OR) &&
5815 (left->exp_right->key < left->exp_left->key)) {
5816 tmp = left->exp_right;
5817 left->exp_right = left->exp_left;
5818 left->exp_left = tmp;
5819 }
5820 left->exp_right->ref++;
5821 tmp = xmlExpHashGetEntry(ctxt, XML_EXP_OR, left->exp_right, right,
5822 NULL, 0, 0);
5823 left->exp_left->ref++;
5824 tmp = xmlExpHashGetEntry(ctxt, XML_EXP_OR, left->exp_left, tmp,
5825 NULL, 0, 0);
5826
5827 xmlExpFree(ctxt, left);
5828 return(tmp);
5829 }
5830 if (right->type == XML_EXP_OR) {
5831 /* Ordering in the tree */
5832 /* C | (A | B) -> A | (B | C) */
5833 if (left->key > right->exp_right->key) {
5834 xmlExpNodePtr tmp;
5835 right->exp_right->ref++;
5836 tmp = xmlExpHashGetEntry(ctxt, XML_EXP_OR, right->exp_right,
5837 left, NULL, 0, 0);
5838 right->exp_left->ref++;
5839 tmp = xmlExpHashGetEntry(ctxt, XML_EXP_OR, right->exp_left,
5840 tmp, NULL, 0, 0);
5841 xmlExpFree(ctxt, right);
5842 return(tmp);
5843 }
5844 /* Ordering in the tree */
5845 /* B | (A | C) -> A | (B | C) */
5846 if (left->key > right->exp_left->key) {
5847 xmlExpNodePtr tmp;
5848 right->exp_right->ref++;
5849 tmp = xmlExpHashGetEntry(ctxt, XML_EXP_OR, left,
5850 right->exp_right, NULL, 0, 0);
5851 right->exp_left->ref++;
5852 tmp = xmlExpHashGetEntry(ctxt, XML_EXP_OR, right->exp_left,
5853 tmp, NULL, 0, 0);
5854 xmlExpFree(ctxt, right);
5855 return(tmp);
5856 }
5857 }
5858 /* we know both types are != XML_EXP_OR here */
5859 else if (left->key > right->key) {
5860 xmlExpNodePtr tmp = left;
5861 left = right;
5862 right = tmp;
5863 }
5864 kbase = xmlExpHashComputeKey(type, left, right);
5865 } else if (type == XML_EXP_SEQ) {
5866 /* Forbid reduction rules */
5867 if (left->type == XML_EXP_FORBID) {
5868 xmlExpFree(ctxt, right);
5869 return(left);
5870 }
5871 if (right->type == XML_EXP_FORBID) {
5872 xmlExpFree(ctxt, left);
5873 return(right);
5874 }
5875 /* Empty reduction rules */
5876 if (right->type == XML_EXP_EMPTY) {
5877 return(left);
5878 }
5879 if (left->type == XML_EXP_EMPTY) {
5880 return(right);
5881 }
5882 kbase = xmlExpHashComputeKey(type, left, right);
5883 } else
5884 return(NULL);
5885
5886 key = kbase % ctxt->size;
5887 if (ctxt->table[key] != NULL) {
5888 for (insert = ctxt->table[key]; insert != NULL;
5889 insert = insert->next) {
5890 if ((insert->key == kbase) &&
5891 (insert->type == type)) {
5892 if (type == XML_EXP_ATOM) {
5893 if (name == insert->exp_str) {
5894 insert->ref++;
5895 return(insert);
5896 }
5897 } else if (type == XML_EXP_COUNT) {
5898 if ((insert->exp_min == min) && (insert->exp_max == max) &&
5899 (insert->exp_left == left)) {
5900 insert->ref++;
5901 left->ref--;
5902 return(insert);
5903 }
5904 } else if ((insert->exp_left == left) &&
5905 (insert->exp_right == right)) {
5906 insert->ref++;
5907 left->ref--;
5908 right->ref--;
5909 return(insert);
5910 }
5911 }
5912 }
5913 }
5914
5915 entry = xmlExpNewNode(ctxt, type);
5916 if (entry == NULL)
5917 return(NULL);
5918 entry->key = kbase;
5919 if (type == XML_EXP_ATOM) {
5920 entry->exp_str = name;
5921 entry->c_max = 1;
5922 } else if (type == XML_EXP_COUNT) {
5923 entry->exp_min = min;
5924 entry->exp_max = max;
5925 entry->exp_left = left;
5926 if ((min == 0) || (IS_NILLABLE(left)))
5927 entry->info |= XML_EXP_NILABLE;
5928 if (max < 0)
5929 entry->c_max = -1;
5930 else
5931 entry->c_max = max * entry->exp_left->c_max;
5932 } else {
5933 entry->exp_left = left;
5934 entry->exp_right = right;
5935 if (type == XML_EXP_OR) {
5936 if ((IS_NILLABLE(left)) || (IS_NILLABLE(right)))
5937 entry->info |= XML_EXP_NILABLE;
5938 if ((entry->exp_left->c_max == -1) ||
5939 (entry->exp_right->c_max == -1))
5940 entry->c_max = -1;
5941 else if (entry->exp_left->c_max > entry->exp_right->c_max)
5942 entry->c_max = entry->exp_left->c_max;
5943 else
5944 entry->c_max = entry->exp_right->c_max;
5945 } else {
5946 if ((IS_NILLABLE(left)) && (IS_NILLABLE(right)))
5947 entry->info |= XML_EXP_NILABLE;
5948 if ((entry->exp_left->c_max == -1) ||
5949 (entry->exp_right->c_max == -1))
5950 entry->c_max = -1;
5951 else
5952 entry->c_max = entry->exp_left->c_max + entry->exp_right->c_max;
5953 }
5954 }
5955 entry->ref = 1;
5956 if (ctxt->table[key] != NULL)
5957 entry->next = ctxt->table[key];
5958
5959 ctxt->table[key] = entry;
5960 ctxt->nbElems++;
5961
5962 return(entry);
5963}
5964
5965/**
5966 * xmlExpFree:
5967 * @ctxt: the expression context
5968 * @exp: the expression
5969 *
5970 * Dereference the expression
5971 */
5972void
5973xmlExpFree(xmlExpCtxtPtr ctxt, xmlExpNodePtr exp) {
5974 if ((exp == NULL) || (exp == forbiddenExp) || (exp == emptyExp))
5975 return;
5976 exp->ref--;
Daniel Veillard81a8ec62005-08-22 00:20:58 +00005977 if (exp->ref == 0) {
5978 unsigned short key;
5979
5980 /* Unlink it first from the hash table */
5981 key = exp->key % ctxt->size;
5982 if (ctxt->table[key] == exp) {
5983 ctxt->table[key] = exp->next;
5984 } else {
5985 xmlExpNodePtr tmp;
5986
5987 tmp = ctxt->table[key];
5988 while (tmp != NULL) {
5989 if (tmp->next == exp) {
5990 tmp->next = exp->next;
5991 break;
5992 }
5993 tmp = tmp->next;
5994 }
5995 }
5996
5997 if ((exp->type == XML_EXP_SEQ) || (exp->type == XML_EXP_OR)) {
5998 xmlExpFree(ctxt, exp->exp_left);
5999 xmlExpFree(ctxt, exp->exp_right);
6000 } else if (exp->type == XML_EXP_COUNT) {
6001 xmlExpFree(ctxt, exp->exp_left);
6002 }
6003 xmlFree(exp);
Daniel Veillard81a8ec62005-08-22 00:20:58 +00006004 ctxt->nb_nodes--;
6005 }
6006}
6007
6008/**
6009 * xmlExpRef:
6010 * @exp: the expression
6011 *
6012 * Increase the reference count of the expression
6013 */
6014void
6015xmlExpRef(xmlExpNodePtr exp) {
6016 if (exp != NULL)
6017 exp->ref++;
6018}
6019
Daniel Veillardccb4d412005-08-23 13:41:17 +00006020/**
6021 * xmlExpNewAtom:
6022 * @ctxt: the expression context
6023 * @name: the atom name
6024 * @len: the atom name lenght in byte (or -1);
6025 *
6026 * Get the atom associated to this name from that context
6027 *
6028 * Returns the node or NULL in case of error
6029 */
6030xmlExpNodePtr
6031xmlExpNewAtom(xmlExpCtxtPtr ctxt, const xmlChar *name, int len) {
6032 if ((ctxt == NULL) || (name == NULL))
6033 return(NULL);
6034 name = xmlDictLookup(ctxt->dict, name, len);
6035 if (name == NULL)
6036 return(NULL);
6037 return(xmlExpHashGetEntry(ctxt, XML_EXP_ATOM, NULL, NULL, name, 0, 0));
6038}
6039
6040/**
6041 * xmlExpNewOr:
6042 * @ctxt: the expression context
6043 * @left: left expression
6044 * @right: right expression
6045 *
6046 * Get the atom associated to the choice @left | @right
6047 * Note that @left and @right are consumed in the operation, to keep
6048 * an handle on them use xmlExpRef() and use xmlExpFree() to release them,
6049 * this is true even in case of failure (unless ctxt == NULL).
6050 *
6051 * Returns the node or NULL in case of error
6052 */
6053xmlExpNodePtr
6054xmlExpNewOr(xmlExpCtxtPtr ctxt, xmlExpNodePtr left, xmlExpNodePtr right) {
6055 if ((ctxt == NULL) || (left == NULL) || (right == NULL)) {
6056 xmlExpFree(ctxt, left);
6057 xmlExpFree(ctxt, right);
6058 return(NULL);
6059 }
6060 return(xmlExpHashGetEntry(ctxt, XML_EXP_OR, left, right, NULL, 0, 0));
6061}
6062
6063/**
6064 * xmlExpNewSeq:
6065 * @ctxt: the expression context
6066 * @left: left expression
6067 * @right: right expression
6068 *
6069 * Get the atom associated to the sequence @left , @right
6070 * Note that @left and @right are consumed in the operation, to keep
6071 * an handle on them use xmlExpRef() and use xmlExpFree() to release them,
6072 * this is true even in case of failure (unless ctxt == NULL).
6073 *
6074 * Returns the node or NULL in case of error
6075 */
6076xmlExpNodePtr
6077xmlExpNewSeq(xmlExpCtxtPtr ctxt, xmlExpNodePtr left, xmlExpNodePtr right) {
6078 if ((ctxt == NULL) || (left == NULL) || (right == NULL)) {
6079 xmlExpFree(ctxt, left);
6080 xmlExpFree(ctxt, right);
6081 return(NULL);
6082 }
6083 return(xmlExpHashGetEntry(ctxt, XML_EXP_SEQ, left, right, NULL, 0, 0));
6084}
6085
6086/**
6087 * xmlExpNewRange:
6088 * @ctxt: the expression context
6089 * @subset: the expression to be repeated
6090 * @min: the lower bound for the repetition
6091 * @max: the upper bound for the repetition, -1 means infinite
6092 *
6093 * Get the atom associated to the range (@subset){@min, @max}
6094 * Note that @subset is consumed in the operation, to keep
6095 * an handle on it use xmlExpRef() and use xmlExpFree() to release it,
6096 * this is true even in case of failure (unless ctxt == NULL).
6097 *
6098 * Returns the node or NULL in case of error
6099 */
6100xmlExpNodePtr
6101xmlExpNewRange(xmlExpCtxtPtr ctxt, xmlExpNodePtr subset, int min, int max) {
6102 if ((ctxt == NULL) || (subset == NULL) || (min < 0) || (max < -1) ||
6103 ((max >= 0) && (min > max))) {
6104 xmlExpFree(ctxt, subset);
6105 return(NULL);
6106 }
6107 return(xmlExpHashGetEntry(ctxt, XML_EXP_COUNT, subset,
6108 NULL, NULL, min, max));
6109}
6110
Daniel Veillard81a8ec62005-08-22 00:20:58 +00006111/************************************************************************
6112 * *
6113 * Public API for operations on expressions *
6114 * *
6115 ************************************************************************/
6116
6117static int
6118xmlExpGetLanguageInt(xmlExpCtxtPtr ctxt, xmlExpNodePtr exp,
6119 const xmlChar**list, int len, int nb) {
6120 int tmp, tmp2;
6121tail:
6122 switch (exp->type) {
6123 case XML_EXP_EMPTY:
6124 return(0);
6125 case XML_EXP_ATOM:
6126 for (tmp = 0;tmp < nb;tmp++)
6127 if (list[tmp] == exp->exp_str)
6128 return(0);
6129 if (nb >= len)
6130 return(-2);
6131 list[nb++] = exp->exp_str;
6132 return(1);
6133 case XML_EXP_COUNT:
6134 exp = exp->exp_left;
6135 goto tail;
6136 case XML_EXP_SEQ:
6137 case XML_EXP_OR:
6138 tmp = xmlExpGetLanguageInt(ctxt, exp->exp_left, list, len, nb);
6139 if (tmp < 0)
6140 return(tmp);
6141 tmp2 = xmlExpGetLanguageInt(ctxt, exp->exp_right, list, len,
6142 nb + tmp);
6143 if (tmp2 < 0)
6144 return(tmp2);
6145 return(tmp + tmp2);
6146 }
6147 return(-1);
6148}
6149
6150/**
6151 * xmlExpGetLanguage:
6152 * @ctxt: the expression context
6153 * @exp: the expression
6154 * @list: where to store the tokens
6155 * @len: the allocated lenght of @list
6156 *
6157 * Find all the strings used in @exp and store them in @list
6158 *
6159 * Returns the number of unique strings found, -1 in case of errors and
6160 * -2 if there is more than @len strings
6161 */
6162int
6163xmlExpGetLanguage(xmlExpCtxtPtr ctxt, xmlExpNodePtr exp,
6164 const xmlChar**list, int len) {
6165 if ((ctxt == NULL) || (exp == NULL) || (list == NULL) || (len <= 0))
6166 return(-1);
6167 return(xmlExpGetLanguageInt(ctxt, exp, list, len, 0));
6168}
6169
6170static int
6171xmlExpGetStartInt(xmlExpCtxtPtr ctxt, xmlExpNodePtr exp,
6172 const xmlChar**list, int len, int nb) {
6173 int tmp, tmp2;
6174tail:
6175 switch (exp->type) {
6176 case XML_EXP_FORBID:
6177 return(0);
6178 case XML_EXP_EMPTY:
6179 return(0);
6180 case XML_EXP_ATOM:
6181 for (tmp = 0;tmp < nb;tmp++)
6182 if (list[tmp] == exp->exp_str)
6183 return(0);
6184 if (nb >= len)
6185 return(-2);
6186 list[nb++] = exp->exp_str;
6187 return(1);
6188 case XML_EXP_COUNT:
6189 exp = exp->exp_left;
6190 goto tail;
6191 case XML_EXP_SEQ:
6192 tmp = xmlExpGetStartInt(ctxt, exp->exp_left, list, len, nb);
6193 if (tmp < 0)
6194 return(tmp);
6195 if (IS_NILLABLE(exp->exp_left)) {
6196 tmp2 = xmlExpGetStartInt(ctxt, exp->exp_right, list, len,
6197 nb + tmp);
6198 if (tmp2 < 0)
6199 return(tmp2);
6200 tmp += tmp2;
6201 }
6202 return(tmp);
6203 case XML_EXP_OR:
6204 tmp = xmlExpGetStartInt(ctxt, exp->exp_left, list, len, nb);
6205 if (tmp < 0)
6206 return(tmp);
6207 tmp2 = xmlExpGetStartInt(ctxt, exp->exp_right, list, len,
6208 nb + tmp);
6209 if (tmp2 < 0)
6210 return(tmp2);
6211 return(tmp + tmp2);
6212 }
6213 return(-1);
6214}
6215
6216/**
6217 * xmlExpGetStart:
6218 * @ctxt: the expression context
6219 * @exp: the expression
6220 * @list: where to store the tokens
6221 * @len: the allocated lenght of @list
6222 *
6223 * Find all the strings that appears at the start of the languages
6224 * accepted by @exp and store them in @list. E.g. for (a, b) | c
6225 * it will return the list [a, c]
6226 *
6227 * Returns the number of unique strings found, -1 in case of errors and
6228 * -2 if there is more than @len strings
6229 */
6230int
6231xmlExpGetStart(xmlExpCtxtPtr ctxt, xmlExpNodePtr exp,
6232 const xmlChar**list, int len) {
6233 if ((ctxt == NULL) || (exp == NULL) || (list == NULL) || (len <= 0))
6234 return(-1);
6235 return(xmlExpGetStartInt(ctxt, exp, list, len, 0));
6236}
6237
6238/**
6239 * xmlExpIsNillable:
6240 * @exp: the expression
6241 *
6242 * Finds if the expression is nillable, i.e. if it accepts the empty sequqnce
6243 *
6244 * Returns 1 if nillable, 0 if not and -1 in case of error
6245 */
6246int
6247xmlExpIsNillable(xmlExpNodePtr exp) {
6248 if (exp == NULL)
6249 return(-1);
6250 return(IS_NILLABLE(exp) != 0);
6251}
6252
6253static xmlExpNodePtr
6254xmlExpStringDeriveInt(xmlExpCtxtPtr ctxt, xmlExpNodePtr exp, const xmlChar *str)
6255{
6256 xmlExpNodePtr ret;
6257
6258 switch (exp->type) {
6259 case XML_EXP_EMPTY:
6260 return(forbiddenExp);
6261 case XML_EXP_FORBID:
6262 return(forbiddenExp);
6263 case XML_EXP_ATOM:
6264 if (exp->exp_str == str) {
6265#ifdef DEBUG_DERIV
6266 printf("deriv atom: equal => Empty\n");
6267#endif
6268 ret = emptyExp;
6269 } else {
6270#ifdef DEBUG_DERIV
6271 printf("deriv atom: mismatch => forbid\n");
6272#endif
6273 /* TODO wildcards here */
6274 ret = forbiddenExp;
6275 }
6276 return(ret);
6277 case XML_EXP_OR: {
6278 xmlExpNodePtr tmp;
6279
6280#ifdef DEBUG_DERIV
6281 printf("deriv or: => or(derivs)\n");
6282#endif
6283 tmp = xmlExpStringDeriveInt(ctxt, exp->exp_left, str);
6284 if (tmp == NULL) {
6285 return(NULL);
6286 }
6287 ret = xmlExpStringDeriveInt(ctxt, exp->exp_right, str);
6288 if (ret == NULL) {
6289 xmlExpFree(ctxt, tmp);
6290 return(NULL);
6291 }
6292 ret = xmlExpHashGetEntry(ctxt, XML_EXP_OR, tmp, ret,
6293 NULL, 0, 0);
6294 return(ret);
6295 }
6296 case XML_EXP_SEQ:
6297#ifdef DEBUG_DERIV
6298 printf("deriv seq: starting with left\n");
6299#endif
6300 ret = xmlExpStringDeriveInt(ctxt, exp->exp_left, str);
6301 if (ret == NULL) {
6302 return(NULL);
6303 } else if (ret == forbiddenExp) {
6304 if (IS_NILLABLE(exp->exp_left)) {
6305#ifdef DEBUG_DERIV
6306 printf("deriv seq: left failed but nillable\n");
6307#endif
6308 ret = xmlExpStringDeriveInt(ctxt, exp->exp_right, str);
6309 }
6310 } else {
6311#ifdef DEBUG_DERIV
6312 printf("deriv seq: left match => sequence\n");
6313#endif
6314 exp->exp_right->ref++;
6315 ret = xmlExpHashGetEntry(ctxt, XML_EXP_SEQ, ret, exp->exp_right,
6316 NULL, 0, 0);
6317 }
6318 return(ret);
6319 case XML_EXP_COUNT: {
6320 int min, max;
6321 xmlExpNodePtr tmp;
6322
6323 if (exp->exp_max == 0)
6324 return(forbiddenExp);
6325 ret = xmlExpStringDeriveInt(ctxt, exp->exp_left, str);
6326 if (ret == NULL)
6327 return(NULL);
6328 if (ret == forbiddenExp) {
6329#ifdef DEBUG_DERIV
6330 printf("deriv count: pattern mismatch => forbid\n");
6331#endif
6332 return(ret);
6333 }
6334 if (exp->exp_max == 1)
6335 return(ret);
6336 if (exp->exp_max < 0) /* unbounded */
6337 max = -1;
6338 else
6339 max = exp->exp_max - 1;
6340 if (exp->exp_min > 0)
6341 min = exp->exp_min - 1;
6342 else
6343 min = 0;
6344 exp->exp_left->ref++;
6345 tmp = xmlExpHashGetEntry(ctxt, XML_EXP_COUNT, exp->exp_left, NULL,
6346 NULL, min, max);
6347 if (ret == emptyExp) {
6348#ifdef DEBUG_DERIV
6349 printf("deriv count: match to empty => new count\n");
6350#endif
6351 return(tmp);
6352 }
6353#ifdef DEBUG_DERIV
6354 printf("deriv count: match => sequence with new count\n");
6355#endif
6356 return(xmlExpHashGetEntry(ctxt, XML_EXP_SEQ, ret, tmp,
6357 NULL, 0, 0));
6358 }
6359 }
6360 return(NULL);
6361}
6362
6363/**
6364 * xmlExpStringDerive:
6365 * @ctxt: the expression context
6366 * @exp: the expression
6367 * @str: the string
6368 * @len: the string len in bytes if available
6369 *
6370 * Do one step of Brzozowski derivation of the expression @exp with
6371 * respect to the input string
6372 *
6373 * Returns the resulting expression or NULL in case of internal error
6374 */
6375xmlExpNodePtr
6376xmlExpStringDerive(xmlExpCtxtPtr ctxt, xmlExpNodePtr exp,
6377 const xmlChar *str, int len) {
6378 const xmlChar *input;
6379
6380 if ((exp == NULL) || (ctxt == NULL) || (str == NULL)) {
6381 return(NULL);
6382 }
6383 /*
6384 * check the string is in the dictionnary, if yes use an interned
6385 * copy, otherwise we know it's not an acceptable input
6386 */
6387 input = xmlDictExists(ctxt->dict, str, len);
6388 if (input == NULL) {
6389 return(forbiddenExp);
6390 }
6391 return(xmlExpStringDeriveInt(ctxt, exp, input));
6392}
6393
6394static int
6395xmlExpCheckCard(xmlExpNodePtr exp, xmlExpNodePtr sub) {
6396 int ret = 1;
6397
6398 if (sub->c_max == -1) {
6399 if (exp->c_max != -1)
6400 ret = 0;
6401 } else if ((exp->c_max >= 0) && (exp->c_max < sub->c_max)) {
6402 ret = 0;
6403 }
6404#if 0
6405 if ((IS_NILLABLE(sub)) && (!IS_NILLABLE(exp)))
6406 ret = 0;
6407#endif
6408 return(ret);
6409}
6410
6411static xmlExpNodePtr xmlExpExpDeriveInt(xmlExpCtxtPtr ctxt, xmlExpNodePtr exp,
6412 xmlExpNodePtr sub);
6413/**
6414 * xmlExpDivide:
6415 * @ctxt: the expressions context
6416 * @exp: the englobing expression
6417 * @sub: the subexpression
6418 * @mult: the multiple expression
6419 * @remain: the remain from the derivation of the multiple
6420 *
6421 * Check if exp is a multiple of sub, i.e. if there is a finite number n
6422 * so that sub{n} subsume exp
6423 *
6424 * Returns the multiple value if successful, 0 if it is not a multiple
6425 * and -1 in case of internel error.
6426 */
6427
6428static int
6429xmlExpDivide(xmlExpCtxtPtr ctxt, xmlExpNodePtr exp, xmlExpNodePtr sub,
6430 xmlExpNodePtr *mult, xmlExpNodePtr *remain) {
6431 int i;
6432 xmlExpNodePtr tmp, tmp2;
6433
6434 if (mult != NULL) *mult = NULL;
6435 if (remain != NULL) *remain = NULL;
6436 if (exp->c_max == -1) return(0);
6437 if (IS_NILLABLE(exp) && (!IS_NILLABLE(sub))) return(0);
6438
6439 for (i = 1;i <= exp->c_max;i++) {
6440 sub->ref++;
6441 tmp = xmlExpHashGetEntry(ctxt, XML_EXP_COUNT,
6442 sub, NULL, NULL, i, i);
6443 if (tmp == NULL) {
6444 return(-1);
6445 }
6446 if (!xmlExpCheckCard(tmp, exp)) {
6447 xmlExpFree(ctxt, tmp);
6448 continue;
6449 }
6450 tmp2 = xmlExpExpDeriveInt(ctxt, tmp, exp);
6451 if (tmp2 == NULL) {
6452 xmlExpFree(ctxt, tmp);
6453 return(-1);
6454 }
6455 if ((tmp2 != forbiddenExp) && (IS_NILLABLE(tmp2))) {
6456 if (remain != NULL)
6457 *remain = tmp2;
6458 else
6459 xmlExpFree(ctxt, tmp2);
6460 if (mult != NULL)
6461 *mult = tmp;
6462 else
6463 xmlExpFree(ctxt, tmp);
6464#ifdef DEBUG_DERIV
6465 printf("Divide succeeded %d\n", i);
6466#endif
6467 return(i);
6468 }
6469 xmlExpFree(ctxt, tmp);
6470 xmlExpFree(ctxt, tmp2);
6471 }
6472#ifdef DEBUG_DERIV
6473 printf("Divide failed\n");
6474#endif
6475 return(0);
6476}
6477
6478/**
6479 * xmlExpExpDeriveInt:
6480 * @ctxt: the expressions context
6481 * @exp: the englobing expression
6482 * @sub: the subexpression
6483 *
6484 * Try to do a step of Brzozowski derivation but at a higher level
6485 * the input being a subexpression.
6486 *
6487 * Returns the resulting expression or NULL in case of internal error
6488 */
6489static xmlExpNodePtr
6490xmlExpExpDeriveInt(xmlExpCtxtPtr ctxt, xmlExpNodePtr exp, xmlExpNodePtr sub) {
6491 xmlExpNodePtr ret, tmp, tmp2, tmp3;
6492 const xmlChar **tab;
6493 int len, i;
6494
6495 /*
6496 * In case of equality and if the expression can only consume a finite
6497 * amount, then the derivation is empty
6498 */
6499 if ((exp == sub) && (exp->c_max >= 0)) {
6500#ifdef DEBUG_DERIV
6501 printf("Equal(exp, sub) and finite -> Empty\n");
6502#endif
6503 return(emptyExp);
6504 }
6505 /*
6506 * decompose sub sequence first
6507 */
6508 if (sub->type == XML_EXP_EMPTY) {
6509#ifdef DEBUG_DERIV
6510 printf("Empty(sub) -> Empty\n");
6511#endif
6512 exp->ref++;
6513 return(exp);
6514 }
6515 if (sub->type == XML_EXP_SEQ) {
6516#ifdef DEBUG_DERIV
6517 printf("Seq(sub) -> decompose\n");
6518#endif
6519 tmp = xmlExpExpDeriveInt(ctxt, exp, sub->exp_left);
6520 if (tmp == NULL)
6521 return(NULL);
6522 if (tmp == forbiddenExp)
6523 return(tmp);
6524 ret = xmlExpExpDeriveInt(ctxt, tmp, sub->exp_right);
6525 xmlExpFree(ctxt, tmp);
6526 return(ret);
6527 }
6528 if (sub->type == XML_EXP_OR) {
6529#ifdef DEBUG_DERIV
6530 printf("Or(sub) -> decompose\n");
6531#endif
6532 tmp = xmlExpExpDeriveInt(ctxt, exp, sub->exp_left);
6533 if (tmp == forbiddenExp)
6534 return(tmp);
6535 if (tmp == NULL)
6536 return(NULL);
6537 ret = xmlExpExpDeriveInt(ctxt, exp, sub->exp_right);
6538 if ((ret == NULL) || (ret == forbiddenExp)) {
6539 xmlExpFree(ctxt, tmp);
6540 return(ret);
6541 }
6542 return(xmlExpHashGetEntry(ctxt, XML_EXP_OR, tmp, ret, NULL, 0, 0));
6543 }
6544 if (!xmlExpCheckCard(exp, sub)) {
6545#ifdef DEBUG_DERIV
6546 printf("CheckCard(exp, sub) failed -> Forbid\n");
6547#endif
6548 return(forbiddenExp);
6549 }
6550 switch (exp->type) {
6551 case XML_EXP_EMPTY:
6552 if (sub == emptyExp)
6553 return(emptyExp);
6554#ifdef DEBUG_DERIV
6555 printf("Empty(exp) -> Forbid\n");
6556#endif
6557 return(forbiddenExp);
6558 case XML_EXP_FORBID:
6559#ifdef DEBUG_DERIV
6560 printf("Forbid(exp) -> Forbid\n");
6561#endif
6562 return(forbiddenExp);
6563 case XML_EXP_ATOM:
6564 if (sub->type == XML_EXP_ATOM) {
6565 /* TODO: handle wildcards */
6566 if (exp->exp_str == sub->exp_str) {
6567#ifdef DEBUG_DERIV
6568 printf("Atom match -> Empty\n");
6569#endif
6570 return(emptyExp);
6571 }
6572#ifdef DEBUG_DERIV
6573 printf("Atom mismatch -> Forbid\n");
6574#endif
6575 return(forbiddenExp);
6576 }
6577 if ((sub->type == XML_EXP_COUNT) &&
6578 (sub->exp_max == 1) &&
6579 (sub->exp_left->type == XML_EXP_ATOM)) {
6580 /* TODO: handle wildcards */
6581 if (exp->exp_str == sub->exp_left->exp_str) {
6582#ifdef DEBUG_DERIV
6583 printf("Atom match -> Empty\n");
6584#endif
6585 return(emptyExp);
6586 }
6587#ifdef DEBUG_DERIV
6588 printf("Atom mismatch -> Forbid\n");
6589#endif
6590 return(forbiddenExp);
6591 }
6592#ifdef DEBUG_DERIV
6593 printf("Compex exp vs Atom -> Forbid\n");
6594#endif
6595 return(forbiddenExp);
6596 case XML_EXP_SEQ:
6597 /* try to get the sequence consumed only if possible */
6598 if (xmlExpCheckCard(exp->exp_left, sub)) {
6599 /* See if the sequence can be consumed directly */
6600#ifdef DEBUG_DERIV
6601 printf("Seq trying left only\n");
6602#endif
6603 ret = xmlExpExpDeriveInt(ctxt, exp->exp_left, sub);
6604 if ((ret != forbiddenExp) && (ret != NULL)) {
6605#ifdef DEBUG_DERIV
6606 printf("Seq trying left only worked\n");
6607#endif
6608 /*
6609 * TODO: assumption here that we are determinist
6610 * i.e. we won't get to a nillable exp left
6611 * subset which could be matched by the right
6612 * part too.
6613 * e.g.: (a | b)+,(a | c) and 'a+,a'
6614 */
6615 exp->exp_right->ref++;
6616 return(xmlExpHashGetEntry(ctxt, XML_EXP_SEQ, ret,
6617 exp->exp_right, NULL, 0, 0));
6618 }
6619#ifdef DEBUG_DERIV
6620 } else {
6621 printf("Seq: left too short\n");
6622#endif
6623 }
6624 /* Try instead to decompose */
6625 if (sub->type == XML_EXP_COUNT) {
6626 int min, max;
6627
6628#ifdef DEBUG_DERIV
6629 printf("Seq: sub is a count\n");
6630#endif
6631 ret = xmlExpExpDeriveInt(ctxt, exp->exp_left, sub->exp_left);
6632 if (ret == NULL)
6633 return(NULL);
6634 if (ret != forbiddenExp) {
6635#ifdef DEBUG_DERIV
6636 printf("Seq , Count match on left\n");
6637#endif
6638 if (sub->exp_max < 0)
6639 max = -1;
6640 else
6641 max = sub->exp_max -1;
6642 if (sub->exp_min > 0)
6643 min = sub->exp_min -1;
6644 else
6645 min = 0;
6646 exp->exp_right->ref++;
6647 tmp = xmlExpHashGetEntry(ctxt, XML_EXP_SEQ, ret,
6648 exp->exp_right, NULL, 0, 0);
6649 if (tmp == NULL)
6650 return(NULL);
6651
6652 sub->exp_left->ref++;
6653 tmp2 = xmlExpHashGetEntry(ctxt, XML_EXP_COUNT,
6654 sub->exp_left, NULL, NULL, min, max);
6655 if (tmp2 == NULL) {
6656 xmlExpFree(ctxt, tmp);
6657 return(NULL);
6658 }
6659 ret = xmlExpExpDeriveInt(ctxt, tmp, tmp2);
6660 xmlExpFree(ctxt, tmp);
6661 xmlExpFree(ctxt, tmp2);
6662 return(ret);
6663 }
6664 }
6665 /* we made no progress on structured operations */
6666 break;
6667 case XML_EXP_OR:
6668#ifdef DEBUG_DERIV
6669 printf("Or , trying both side\n");
6670#endif
6671 ret = xmlExpExpDeriveInt(ctxt, exp->exp_left, sub);
6672 if (ret == NULL)
6673 return(NULL);
6674 tmp = xmlExpExpDeriveInt(ctxt, exp->exp_right, sub);
6675 if (tmp == NULL) {
6676 xmlExpFree(ctxt, ret);
6677 return(NULL);
6678 }
6679 return(xmlExpHashGetEntry(ctxt, XML_EXP_OR, ret, tmp, NULL, 0, 0));
6680 case XML_EXP_COUNT: {
6681 int min, max;
6682
6683 if (sub->type == XML_EXP_COUNT) {
6684 /*
6685 * Try to see if the loop is completely subsumed
6686 */
6687 tmp = xmlExpExpDeriveInt(ctxt, exp->exp_left, sub->exp_left);
6688 if (tmp == NULL)
6689 return(NULL);
6690 if (tmp == forbiddenExp) {
6691 int mult;
6692
6693#ifdef DEBUG_DERIV
6694 printf("Count, Count inner don't subsume\n");
6695#endif
6696 mult = xmlExpDivide(ctxt, sub->exp_left, exp->exp_left,
6697 NULL, &tmp);
6698 if (mult <= 0) {
6699#ifdef DEBUG_DERIV
6700 printf("Count, Count not multiple => forbidden\n");
6701#endif
6702 return(forbiddenExp);
6703 }
6704 if (sub->exp_max == -1) {
6705 max = -1;
6706 if (exp->exp_max == -1) {
6707 if (exp->exp_min <= sub->exp_min * mult)
6708 min = 0;
6709 else
6710 min = exp->exp_min - sub->exp_min * mult;
6711 } else {
6712#ifdef DEBUG_DERIV
6713 printf("Count, Count finite can't subsume infinite\n");
6714#endif
6715 xmlExpFree(ctxt, tmp);
6716 return(forbiddenExp);
6717 }
6718 } else {
6719 if (exp->exp_max == -1) {
6720#ifdef DEBUG_DERIV
6721 printf("Infinite loop consume mult finite loop\n");
6722#endif
6723 if (exp->exp_min > sub->exp_min * mult) {
6724 max = -1;
6725 min = exp->exp_min - sub->exp_min * mult;
6726 } else {
6727 max = -1;
6728 min = 0;
6729 }
6730 } else {
6731 if (exp->exp_max < sub->exp_max * mult) {
6732#ifdef DEBUG_DERIV
6733 printf("loops max mult mismatch => forbidden\n");
6734#endif
6735 xmlExpFree(ctxt, tmp);
6736 return(forbiddenExp);
6737 }
6738 if (sub->exp_max * mult > exp->exp_min)
6739 min = 0;
6740 else
6741 min = exp->exp_min - sub->exp_max * mult;
6742 max = exp->exp_max - sub->exp_max * mult;
6743 }
6744 }
6745 } else if (!IS_NILLABLE(tmp)) {
6746 /*
6747 * TODO: loop here to try to grow if working on finite
6748 * blocks.
6749 */
6750#ifdef DEBUG_DERIV
6751 printf("Count, Count remain not nillable => forbidden\n");
6752#endif
6753 xmlExpFree(ctxt, tmp);
6754 return(forbiddenExp);
6755 } else if (sub->exp_max == -1) {
6756 if (exp->exp_max == -1) {
6757 if (exp->exp_min <= sub->exp_min) {
6758#ifdef DEBUG_DERIV
6759 printf("Infinite loops Okay => COUNT(0,Inf)\n");
6760#endif
6761 max = -1;
6762 min = 0;
6763 } else {
6764#ifdef DEBUG_DERIV
6765 printf("Infinite loops min => Count(X,Inf)\n");
6766#endif
6767 max = -1;
6768 min = exp->exp_min - sub->exp_min;
6769 }
6770 } else if (exp->exp_min > sub->exp_min) {
6771#ifdef DEBUG_DERIV
6772 printf("loops min mismatch 1 => forbidden ???\n");
6773#endif
6774 xmlExpFree(ctxt, tmp);
6775 return(forbiddenExp);
6776 } else {
6777 max = -1;
6778 min = 0;
6779 }
6780 } else {
6781 if (exp->exp_max == -1) {
6782#ifdef DEBUG_DERIV
6783 printf("Infinite loop consume finite loop\n");
6784#endif
6785 if (exp->exp_min > sub->exp_min) {
6786 max = -1;
6787 min = exp->exp_min - sub->exp_min;
6788 } else {
6789 max = -1;
6790 min = 0;
6791 }
6792 } else {
6793 if (exp->exp_max < sub->exp_max) {
6794#ifdef DEBUG_DERIV
6795 printf("loops max mismatch => forbidden\n");
6796#endif
6797 xmlExpFree(ctxt, tmp);
6798 return(forbiddenExp);
6799 }
6800 if (sub->exp_max > exp->exp_min)
6801 min = 0;
6802 else
6803 min = exp->exp_min - sub->exp_max;
6804 max = exp->exp_max - sub->exp_max;
6805 }
6806 }
6807#ifdef DEBUG_DERIV
6808 printf("loops match => SEQ(COUNT())\n");
6809#endif
6810 exp->exp_left->ref++;
6811 tmp2 = xmlExpHashGetEntry(ctxt, XML_EXP_COUNT, exp->exp_left,
6812 NULL, NULL, min, max);
6813 if (tmp2 == NULL) {
6814 return(NULL);
6815 }
6816 ret = xmlExpHashGetEntry(ctxt, XML_EXP_SEQ, tmp, tmp2,
6817 NULL, 0, 0);
6818 return(ret);
6819 }
6820 tmp = xmlExpExpDeriveInt(ctxt, exp->exp_left, sub);
6821 if (tmp == NULL)
6822 return(NULL);
6823 if (tmp == forbiddenExp) {
6824#ifdef DEBUG_DERIV
6825 printf("loop mismatch => forbidden\n");
6826#endif
6827 return(forbiddenExp);
6828 }
6829 if (exp->exp_min > 0)
6830 min = exp->exp_min - 1;
6831 else
6832 min = 0;
6833 if (exp->exp_max < 0)
6834 max = -1;
6835 else
6836 max = exp->exp_max - 1;
6837
6838#ifdef DEBUG_DERIV
6839 printf("loop match => SEQ(COUNT())\n");
6840#endif
6841 exp->exp_left->ref++;
6842 tmp2 = xmlExpHashGetEntry(ctxt, XML_EXP_COUNT, exp->exp_left,
6843 NULL, NULL, min, max);
6844 if (tmp2 == NULL)
6845 return(NULL);
6846 ret = xmlExpHashGetEntry(ctxt, XML_EXP_SEQ, tmp, tmp2,
6847 NULL, 0, 0);
6848 return(ret);
6849 }
6850 }
6851
Daniel Veillardccb4d412005-08-23 13:41:17 +00006852#ifdef DEBUG_DERIV
6853 printf("Fallback to derivative\n");
6854#endif
6855 if (IS_NILLABLE(sub)) {
6856 if (!(IS_NILLABLE(exp)))
6857 return(forbiddenExp);
6858 else
6859 ret = emptyExp;
6860 } else
6861 ret = NULL;
Daniel Veillard81a8ec62005-08-22 00:20:58 +00006862 /*
6863 * here the structured derivation made no progress so
6864 * we use the default token based derivation to force one more step
6865 */
6866 if (ctxt->tabSize == 0)
6867 ctxt->tabSize = 40;
6868
6869 tab = (const xmlChar **) xmlMalloc(ctxt->tabSize *
6870 sizeof(const xmlChar *));
6871 if (tab == NULL) {
6872 return(NULL);
6873 }
6874
Daniel Veillard81a8ec62005-08-22 00:20:58 +00006875 /*
6876 * collect all the strings accepted by the subexpression on input
6877 */
6878 len = xmlExpGetStartInt(ctxt, sub, tab, ctxt->tabSize, 0);
6879 while (len < 0) {
6880 const xmlChar **temp;
6881 temp = (const xmlChar **) xmlRealloc(tab, ctxt->tabSize * 2 *
6882 sizeof(const xmlChar *));
6883 if (temp == NULL) {
6884 xmlFree(tab);
6885 return(NULL);
6886 }
6887 tab = temp;
6888 ctxt->tabSize *= 2;
6889 len = xmlExpGetStartInt(ctxt, sub, tab, ctxt->tabSize, 0);
6890 }
Daniel Veillard81a8ec62005-08-22 00:20:58 +00006891 for (i = 0;i < len;i++) {
6892 tmp = xmlExpStringDeriveInt(ctxt, exp, tab[i]);
6893 if ((tmp == NULL) || (tmp == forbiddenExp)) {
6894 xmlExpFree(ctxt, ret);
6895 xmlFree(tab);
6896 return(tmp);
6897 }
6898 tmp2 = xmlExpStringDeriveInt(ctxt, sub, tab[i]);
6899 if ((tmp2 == NULL) || (tmp2 == forbiddenExp)) {
6900 xmlExpFree(ctxt, tmp);
6901 xmlExpFree(ctxt, ret);
6902 xmlFree(tab);
6903 return(tmp);
6904 }
6905 tmp3 = xmlExpExpDeriveInt(ctxt, tmp, tmp2);
6906 xmlExpFree(ctxt, tmp);
6907 xmlExpFree(ctxt, tmp2);
6908
6909 if ((tmp3 == NULL) || (tmp3 == forbiddenExp)) {
6910 xmlExpFree(ctxt, ret);
6911 xmlFree(tab);
6912 return(tmp3);
6913 }
6914
6915 if (ret == NULL)
6916 ret = tmp3;
6917 else {
6918 ret = xmlExpHashGetEntry(ctxt, XML_EXP_OR, ret, tmp3, NULL, 0, 0);
6919 if (ret == NULL) {
6920 xmlFree(tab);
6921 return(NULL);
6922 }
6923 }
6924 }
6925 xmlFree(tab);
6926 return(ret);
6927}
6928
6929/**
Daniel Veillard0090bd52005-08-22 14:43:43 +00006930 * xmlExpExpDerive:
6931 * @ctxt: the expressions context
6932 * @exp: the englobing expression
6933 * @sub: the subexpression
6934 *
6935 * Evaluates the expression resulting from @exp consuming a sub expression @sub
6936 * Based on algebraic derivation and sometimes direct Brzozowski derivation
6937 * it usually tatkes less than linear time and can handle expressions generating
6938 * infinite languages.
6939 *
6940 * Returns the resulting expression or NULL in case of internal error, the
6941 * result must be freed
6942 */
6943xmlExpNodePtr
6944xmlExpExpDerive(xmlExpCtxtPtr ctxt, xmlExpNodePtr exp, xmlExpNodePtr sub) {
6945 if ((exp == NULL) || (ctxt == NULL) || (sub == NULL))
6946 return(NULL);
6947
6948 /*
6949 * O(1) speedups
6950 */
6951 if (IS_NILLABLE(sub) && (!IS_NILLABLE(exp))) {
6952#ifdef DEBUG_DERIV
6953 printf("Sub nillable and not exp : can't subsume\n");
6954#endif
6955 return(forbiddenExp);
6956 }
6957 if (xmlExpCheckCard(exp, sub) == 0) {
6958#ifdef DEBUG_DERIV
6959 printf("sub generate longuer sequances than exp : can't subsume\n");
6960#endif
6961 return(forbiddenExp);
6962 }
6963 return(xmlExpExpDeriveInt(ctxt, exp, sub));
6964}
6965
6966/**
Daniel Veillard81a8ec62005-08-22 00:20:58 +00006967 * xmlExpSubsume:
6968 * @ctxt: the expressions context
6969 * @exp: the englobing expression
6970 * @sub: the subexpression
6971 *
6972 * Check whether @exp accepts all the languages accexpted by @sub
6973 * the input being a subexpression.
6974 *
6975 * Returns 1 if true 0 if false and -1 in case of failure.
6976 */
6977int
6978xmlExpSubsume(xmlExpCtxtPtr ctxt, xmlExpNodePtr exp, xmlExpNodePtr sub) {
6979 xmlExpNodePtr tmp;
6980
6981 if ((exp == NULL) || (ctxt == NULL) || (sub == NULL))
6982 return(-1);
6983
6984 /*
6985 * TODO: speedup by checking the language of sub is a subset of the
6986 * language of exp
6987 */
6988 /*
6989 * O(1) speedups
6990 */
6991 if (IS_NILLABLE(sub) && (!IS_NILLABLE(exp))) {
6992#ifdef DEBUG_DERIV
6993 printf("Sub nillable and not exp : can't subsume\n");
6994#endif
6995 return(0);
6996 }
6997 if (xmlExpCheckCard(exp, sub) == 0) {
6998#ifdef DEBUG_DERIV
6999 printf("sub generate longuer sequances than exp : can't subsume\n");
7000#endif
7001 return(0);
7002 }
7003 tmp = xmlExpExpDeriveInt(ctxt, exp, sub);
7004#ifdef DEBUG_DERIV
7005 printf("Result derivation :\n");
7006 PRINT_EXP(tmp);
7007#endif
7008 if (tmp == NULL)
7009 return(-1);
7010 if (tmp == forbiddenExp)
7011 return(0);
7012 if (tmp == emptyExp)
7013 return(1);
7014 if ((tmp != NULL) && (IS_NILLABLE(tmp))) {
7015 xmlExpFree(ctxt, tmp);
7016 return(1);
7017 }
7018 xmlExpFree(ctxt, tmp);
7019 return(0);
7020}
Daniel Veillard465a0002005-08-22 12:07:04 +00007021
7022/************************************************************************
7023 * *
7024 * Parsing expression *
7025 * *
7026 ************************************************************************/
7027
7028static xmlExpNodePtr xmlExpParseExpr(xmlExpCtxtPtr ctxt);
7029
7030#undef CUR
7031#define CUR (*ctxt->cur)
7032#undef NEXT
7033#define NEXT ctxt->cur++;
7034#undef IS_BLANK
7035#define IS_BLANK(c) ((c == ' ') || (c == '\n') || (c == '\r') || (c == '\t'))
7036#define SKIP_BLANKS while (IS_BLANK(*ctxt->cur)) ctxt->cur++;
7037
7038static int
7039xmlExpParseNumber(xmlExpCtxtPtr ctxt) {
7040 int ret = 0;
7041
7042 SKIP_BLANKS
7043 if (CUR == '*') {
7044 NEXT
7045 return(-1);
7046 }
7047 if ((CUR < '0') || (CUR > '9'))
7048 return(-1);
7049 while ((CUR >= '0') && (CUR <= '9')) {
7050 ret = ret * 10 + (CUR - '0');
7051 NEXT
7052 }
7053 return(ret);
7054}
7055
7056static xmlExpNodePtr
7057xmlExpParseOr(xmlExpCtxtPtr ctxt) {
7058 const char *base;
7059 xmlExpNodePtr ret;
7060 const xmlChar *val;
7061
7062 SKIP_BLANKS
7063 base = ctxt->cur;
7064 if (*ctxt->cur == '(') {
7065 NEXT
7066 ret = xmlExpParseExpr(ctxt);
7067 SKIP_BLANKS
7068 if (*ctxt->cur != ')') {
7069 fprintf(stderr, "unbalanced '(' : %s\n", base);
7070 xmlExpFree(ctxt, ret);
7071 return(NULL);
7072 }
7073 NEXT;
7074 SKIP_BLANKS
7075 goto parse_quantifier;
7076 }
7077 while ((CUR != 0) && (!(IS_BLANK(CUR))) && (CUR != '(') &&
7078 (CUR != ')') && (CUR != '|') && (CUR != ',') && (CUR != '{') &&
7079 (CUR != '*') && (CUR != '+') && (CUR != '?') && (CUR != '}'))
7080 NEXT;
7081 val = xmlDictLookup(ctxt->dict, BAD_CAST base, ctxt->cur - base);
7082 if (val == NULL)
7083 return(NULL);
7084 ret = xmlExpHashGetEntry(ctxt, XML_EXP_ATOM, NULL, NULL, val, 0, 0);
7085 if (ret == NULL)
7086 return(NULL);
7087 SKIP_BLANKS
7088parse_quantifier:
7089 if (CUR == '{') {
7090 int min, max;
7091
7092 NEXT
7093 min = xmlExpParseNumber(ctxt);
7094 if (min < 0) {
7095 xmlExpFree(ctxt, ret);
7096 return(NULL);
7097 }
7098 SKIP_BLANKS
7099 if (CUR == ',') {
7100 NEXT
7101 max = xmlExpParseNumber(ctxt);
7102 SKIP_BLANKS
7103 } else
7104 max = min;
7105 if (CUR != '}') {
7106 xmlExpFree(ctxt, ret);
7107 return(NULL);
7108 }
7109 NEXT
7110 ret = xmlExpHashGetEntry(ctxt, XML_EXP_COUNT, ret, NULL, NULL,
7111 min, max);
7112 SKIP_BLANKS
7113 } else if (CUR == '?') {
7114 NEXT
7115 ret = xmlExpHashGetEntry(ctxt, XML_EXP_COUNT, ret, NULL, NULL,
7116 0, 1);
7117 SKIP_BLANKS
7118 } else if (CUR == '+') {
7119 NEXT
7120 ret = xmlExpHashGetEntry(ctxt, XML_EXP_COUNT, ret, NULL, NULL,
7121 1, -1);
7122 SKIP_BLANKS
7123 } else if (CUR == '*') {
7124 NEXT
7125 ret = xmlExpHashGetEntry(ctxt, XML_EXP_COUNT, ret, NULL, NULL,
7126 0, -1);
7127 SKIP_BLANKS
7128 }
7129 return(ret);
7130}
7131
7132
7133static xmlExpNodePtr
7134xmlExpParseSeq(xmlExpCtxtPtr ctxt) {
7135 xmlExpNodePtr ret, right;
7136
7137 ret = xmlExpParseOr(ctxt);
7138 SKIP_BLANKS
7139 while (CUR == '|') {
7140 NEXT
7141 right = xmlExpParseOr(ctxt);
7142 if (right == NULL) {
7143 xmlExpFree(ctxt, ret);
7144 return(NULL);
7145 }
7146 ret = xmlExpHashGetEntry(ctxt, XML_EXP_OR, ret, right, NULL, 0, 0);
7147 if (ret == NULL)
7148 return(NULL);
7149 }
7150 return(ret);
7151}
7152
7153static xmlExpNodePtr
7154xmlExpParseExpr(xmlExpCtxtPtr ctxt) {
7155 xmlExpNodePtr ret, right;
7156
7157 ret = xmlExpParseSeq(ctxt);
7158 SKIP_BLANKS
7159 while (CUR == ',') {
7160 NEXT
7161 right = xmlExpParseSeq(ctxt);
7162 if (right == NULL) {
7163 xmlExpFree(ctxt, ret);
7164 return(NULL);
7165 }
7166 ret = xmlExpHashGetEntry(ctxt, XML_EXP_SEQ, ret, right, NULL, 0, 0);
7167 if (ret == NULL)
7168 return(NULL);
7169 }
7170 return(ret);
7171}
7172
7173/**
7174 * xmlExpParse:
7175 * @ctxt: the expressions context
7176 * @expr: the 0 terminated string
7177 *
7178 * Minimal parser for regexps, it understand the following constructs
7179 * - string terminals
7180 * - choice operator |
7181 * - sequence operator ,
7182 * - subexpressions (...)
7183 * - usual cardinality operators + * and ?
7184 * - finite sequences { min, max }
7185 * - infinite sequences { min, * }
7186 * There is minimal checkings made especially no checking on strings values
7187 *
7188 * Returns a new expression or NULL in case of failure
7189 */
7190xmlExpNodePtr
7191xmlExpParse(xmlExpCtxtPtr ctxt, const char *expr) {
7192 xmlExpNodePtr ret;
7193
7194 ctxt->expr = expr;
7195 ctxt->cur = expr;
7196
7197 ret = xmlExpParseExpr(ctxt);
7198 SKIP_BLANKS
7199 if (*ctxt->cur != 0) {
7200 xmlExpFree(ctxt, ret);
7201 return(NULL);
7202 }
7203 return(ret);
7204}
7205
7206static void
7207xmlExpDumpInt(xmlBufferPtr buf, xmlExpNodePtr expr, int glob) {
7208 xmlExpNodePtr c;
7209
7210 if (expr == NULL) return;
7211 if (glob) xmlBufferWriteChar(buf, "(");
7212 switch (expr->type) {
7213 case XML_EXP_EMPTY:
7214 xmlBufferWriteChar(buf, "empty");
7215 break;
7216 case XML_EXP_FORBID:
7217 xmlBufferWriteChar(buf, "forbidden");
7218 break;
7219 case XML_EXP_ATOM:
7220 xmlBufferWriteCHAR(buf, expr->exp_str);
7221 break;
7222 case XML_EXP_SEQ:
7223 c = expr->exp_left;
7224 if ((c->type == XML_EXP_SEQ) || (c->type == XML_EXP_OR))
7225 xmlExpDumpInt(buf, c, 1);
7226 else
7227 xmlExpDumpInt(buf, c, 0);
7228 xmlBufferWriteChar(buf, " , ");
7229 c = expr->exp_right;
7230 if ((c->type == XML_EXP_SEQ) || (c->type == XML_EXP_OR))
7231 xmlExpDumpInt(buf, c, 1);
7232 else
7233 xmlExpDumpInt(buf, c, 0);
7234 break;
7235 case XML_EXP_OR:
7236 c = expr->exp_left;
7237 if ((c->type == XML_EXP_SEQ) || (c->type == XML_EXP_OR))
7238 xmlExpDumpInt(buf, c, 1);
7239 else
7240 xmlExpDumpInt(buf, c, 0);
7241 xmlBufferWriteChar(buf, " | ");
7242 c = expr->exp_right;
7243 if ((c->type == XML_EXP_SEQ) || (c->type == XML_EXP_OR))
7244 xmlExpDumpInt(buf, c, 1);
7245 else
7246 xmlExpDumpInt(buf, c, 0);
7247 break;
7248 case XML_EXP_COUNT: {
7249 char rep[40];
7250
7251 c = expr->exp_left;
7252 if ((c->type == XML_EXP_SEQ) || (c->type == XML_EXP_OR))
7253 xmlExpDumpInt(buf, c, 1);
7254 else
7255 xmlExpDumpInt(buf, c, 0);
7256 if ((expr->exp_min == 0) && (expr->exp_max == 1)) {
7257 rep[0] = '?';
7258 rep[1] = 0;
7259 } else if ((expr->exp_min == 0) && (expr->exp_max == -1)) {
7260 rep[0] = '*';
7261 rep[1] = 0;
7262 } else if ((expr->exp_min == 1) && (expr->exp_max == -1)) {
7263 rep[0] = '+';
7264 rep[1] = 0;
7265 } else if (expr->exp_max == expr->exp_min) {
7266 snprintf(rep, 39, "{%d}", expr->exp_min);
7267 } else if (expr->exp_max < 0) {
7268 snprintf(rep, 39, "{%d,inf}", expr->exp_min);
7269 } else {
7270 snprintf(rep, 39, "{%d,%d}", expr->exp_min, expr->exp_max);
7271 }
7272 rep[39] = 0;
7273 xmlBufferWriteChar(buf, rep);
7274 break;
7275 }
7276 default:
7277 fprintf(stderr, "Error in tree\n");
7278 }
7279 if (glob)
7280 xmlBufferWriteChar(buf, ")");
7281}
7282/**
7283 * xmlExpDump:
7284 * @buf: a buffer to receive the output
7285 * @expr: the compiled expression
7286 *
7287 * Serialize the expression as compiled to the buffer
7288 */
7289void
Daniel Veillard5eee7672005-08-22 21:22:27 +00007290xmlExpDump(xmlBufferPtr buf, xmlExpNodePtr expr) {
7291 if ((buf == NULL) || (expr == NULL))
Daniel Veillard465a0002005-08-22 12:07:04 +00007292 return;
Daniel Veillard5eee7672005-08-22 21:22:27 +00007293 xmlExpDumpInt(buf, expr, 0);
Daniel Veillard465a0002005-08-22 12:07:04 +00007294}
7295
7296/**
7297 * xmlExpMaxToken:
7298 * @expr: a compiled expression
7299 *
7300 * Indicate the maximum number of input a expression can accept
7301 *
7302 * Returns the maximum length or -1 in case of error
7303 */
7304int
7305xmlExpMaxToken(xmlExpNodePtr expr) {
7306 if (expr == NULL)
7307 return(-1);
7308 return(expr->c_max);
7309}
7310
7311/**
7312 * xmlExpCtxtNbNodes:
7313 * @ctxt: an expression context
7314 *
7315 * Debugging facility provides the number of allocated nodes at a that point
7316 *
7317 * Returns the number of nodes in use or -1 in case of error
7318 */
7319int
7320xmlExpCtxtNbNodes(xmlExpCtxtPtr ctxt) {
7321 if (ctxt == NULL)
7322 return(-1);
7323 return(ctxt->nb_nodes);
7324}
7325
7326/**
7327 * xmlExpCtxtNbCons:
7328 * @ctxt: an expression context
7329 *
7330 * Debugging facility provides the number of allocated nodes over lifetime
7331 *
7332 * Returns the number of nodes ever allocated or -1 in case of error
7333 */
7334int
7335xmlExpCtxtNbCons(xmlExpCtxtPtr ctxt) {
7336 if (ctxt == NULL)
7337 return(-1);
7338 return(ctxt->nb_cons);
7339}
7340
Daniel Veillard81a8ec62005-08-22 00:20:58 +00007341#endif /* LIBXML_EXPR_ENABLED */
Daniel Veillard5d4644e2005-04-01 13:11:58 +00007342#define bottom_xmlregexp
7343#include "elfgcchack.h"
Daniel Veillard4255d502002-04-16 15:50:10 +00007344#endif /* LIBXML_REGEXP_ENABLED */