blob: 9d479217e00678db6e0cbb181fa5efa567f6b60c [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 Veillarddb68b742005-07-30 13:18:24 +00001241 int counter, int count, int nchk) {
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 Veillarddb68b742005-07-30 13:18:24 +00001259 if (nchk == 0) {
1260 for (nrtrans = state->nbTrans - 1; nrtrans >= 0; nrtrans--) {
1261 xmlRegTransPtr trans = &(state->trans[nrtrans]);
1262 if ((trans->atom == atom) &&
1263 (trans->to == target->no) &&
1264 (trans->counter == counter) &&
1265 (trans->count == count)) {
William M. Brackf9b5fa22004-05-10 07:52:15 +00001266#ifdef DEBUG_REGEXP_GRAPH
Daniel Veillarddb68b742005-07-30 13:18:24 +00001267 printf("Ignoring duplicate transition from %d to %d\n",
1268 state->no, target->no);
William M. Brackf9b5fa22004-05-10 07:52:15 +00001269#endif
Daniel Veillarddb68b742005-07-30 13:18:24 +00001270 return;
1271 }
1272 }
William M. Brackf9b5fa22004-05-10 07:52:15 +00001273 }
1274
Daniel Veillard4255d502002-04-16 15:50:10 +00001275 if (state->maxTrans == 0) {
Daniel Veillarddb68b742005-07-30 13:18:24 +00001276 state->maxTrans = 8;
Daniel Veillard4255d502002-04-16 15:50:10 +00001277 state->trans = (xmlRegTrans *) xmlMalloc(state->maxTrans *
1278 sizeof(xmlRegTrans));
1279 if (state->trans == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00001280 xmlRegexpErrMemory(ctxt, "adding transition");
Daniel Veillard4255d502002-04-16 15:50:10 +00001281 state->maxTrans = 0;
1282 return;
1283 }
1284 } else if (state->nbTrans >= state->maxTrans) {
1285 xmlRegTrans *tmp;
1286 state->maxTrans *= 2;
1287 tmp = (xmlRegTrans *) xmlRealloc(state->trans, state->maxTrans *
1288 sizeof(xmlRegTrans));
1289 if (tmp == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00001290 xmlRegexpErrMemory(ctxt, "adding transition");
Daniel Veillard4255d502002-04-16 15:50:10 +00001291 state->maxTrans /= 2;
1292 return;
1293 }
1294 state->trans = tmp;
1295 }
1296#ifdef DEBUG_REGEXP_GRAPH
1297 printf("Add trans from %d to %d ", state->no, target->no);
Daniel Veillard8a001f62002-04-20 07:24:11 +00001298 if (count == REGEXP_ALL_COUNTER)
Daniel Veillard2cbf5962004-03-31 15:50:43 +00001299 printf("all transition\n");
Daniel Veillard4402ab42002-09-12 16:02:56 +00001300 else if (count >= 0)
Daniel Veillard2cbf5962004-03-31 15:50:43 +00001301 printf("count based %d\n", count);
Daniel Veillard4255d502002-04-16 15:50:10 +00001302 else if (counter >= 0)
Daniel Veillard2cbf5962004-03-31 15:50:43 +00001303 printf("counted %d\n", counter);
Daniel Veillard4255d502002-04-16 15:50:10 +00001304 else if (atom == NULL)
Daniel Veillard2cbf5962004-03-31 15:50:43 +00001305 printf("epsilon transition\n");
1306 else if (atom != NULL)
1307 xmlRegPrintAtom(stdout, atom);
Daniel Veillard4255d502002-04-16 15:50:10 +00001308#endif
1309
1310 state->trans[state->nbTrans].atom = atom;
1311 state->trans[state->nbTrans].to = target->no;
1312 state->trans[state->nbTrans].counter = counter;
1313 state->trans[state->nbTrans].count = count;
1314 state->nbTrans++;
Daniel Veillarddb68b742005-07-30 13:18:24 +00001315 xmlRegStateAddTransTo(ctxt, target, state->no);
Daniel Veillard4255d502002-04-16 15:50:10 +00001316}
1317
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001318static int
Daniel Veillard4255d502002-04-16 15:50:10 +00001319xmlRegStatePush(xmlRegParserCtxtPtr ctxt, xmlRegStatePtr state) {
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001320 if (state == NULL) return(-1);
Daniel Veillard4255d502002-04-16 15:50:10 +00001321 if (ctxt->maxStates == 0) {
1322 ctxt->maxStates = 4;
1323 ctxt->states = (xmlRegStatePtr *) xmlMalloc(ctxt->maxStates *
1324 sizeof(xmlRegStatePtr));
1325 if (ctxt->states == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00001326 xmlRegexpErrMemory(ctxt, "adding state");
Daniel Veillard4255d502002-04-16 15:50:10 +00001327 ctxt->maxStates = 0;
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001328 return(-1);
Daniel Veillard4255d502002-04-16 15:50:10 +00001329 }
1330 } else if (ctxt->nbStates >= ctxt->maxStates) {
1331 xmlRegStatePtr *tmp;
1332 ctxt->maxStates *= 2;
1333 tmp = (xmlRegStatePtr *) xmlRealloc(ctxt->states, ctxt->maxStates *
1334 sizeof(xmlRegStatePtr));
1335 if (tmp == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00001336 xmlRegexpErrMemory(ctxt, "adding state");
Daniel Veillard4255d502002-04-16 15:50:10 +00001337 ctxt->maxStates /= 2;
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001338 return(-1);
Daniel Veillard4255d502002-04-16 15:50:10 +00001339 }
1340 ctxt->states = tmp;
1341 }
1342 state->no = ctxt->nbStates;
1343 ctxt->states[ctxt->nbStates++] = state;
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001344 return(0);
Daniel Veillard4255d502002-04-16 15:50:10 +00001345}
1346
1347/**
Daniel Veillard7646b182002-04-20 06:41:40 +00001348 * xmlFAGenerateAllTransition:
Daniel Veillard441bc322002-04-20 17:38:48 +00001349 * @ctxt: a regexp parser context
1350 * @from: the from state
1351 * @to: the target state or NULL for building a new one
1352 * @lax:
Daniel Veillard7646b182002-04-20 06:41:40 +00001353 *
1354 */
1355static void
1356xmlFAGenerateAllTransition(xmlRegParserCtxtPtr ctxt,
Daniel Veillard441bc322002-04-20 17:38:48 +00001357 xmlRegStatePtr from, xmlRegStatePtr to,
1358 int lax) {
Daniel Veillard7646b182002-04-20 06:41:40 +00001359 if (to == NULL) {
1360 to = xmlRegNewState(ctxt);
1361 xmlRegStatePush(ctxt, to);
1362 ctxt->state = to;
1363 }
Daniel Veillard441bc322002-04-20 17:38:48 +00001364 if (lax)
Daniel Veillarddb68b742005-07-30 13:18:24 +00001365 xmlRegStateAddTrans(ctxt, from, NULL, to, -1, REGEXP_ALL_LAX_COUNTER, 0);
Daniel Veillard441bc322002-04-20 17:38:48 +00001366 else
Daniel Veillarddb68b742005-07-30 13:18:24 +00001367 xmlRegStateAddTrans(ctxt, from, NULL, to, -1, REGEXP_ALL_COUNTER, 0);
Daniel Veillard7646b182002-04-20 06:41:40 +00001368}
1369
1370/**
Daniel Veillard4255d502002-04-16 15:50:10 +00001371 * xmlFAGenerateEpsilonTransition:
Daniel Veillard441bc322002-04-20 17:38:48 +00001372 * @ctxt: a regexp parser context
1373 * @from: the from state
1374 * @to: the target state or NULL for building a new one
Daniel Veillard4255d502002-04-16 15:50:10 +00001375 *
1376 */
1377static void
1378xmlFAGenerateEpsilonTransition(xmlRegParserCtxtPtr ctxt,
1379 xmlRegStatePtr from, xmlRegStatePtr to) {
1380 if (to == NULL) {
1381 to = xmlRegNewState(ctxt);
1382 xmlRegStatePush(ctxt, to);
1383 ctxt->state = to;
1384 }
Daniel Veillarddb68b742005-07-30 13:18:24 +00001385 xmlRegStateAddTrans(ctxt, from, NULL, to, -1, -1, 0);
Daniel Veillard4255d502002-04-16 15:50:10 +00001386}
1387
1388/**
1389 * xmlFAGenerateCountedEpsilonTransition:
Daniel Veillard441bc322002-04-20 17:38:48 +00001390 * @ctxt: a regexp parser context
1391 * @from: the from state
1392 * @to: the target state or NULL for building a new one
Daniel Veillard4255d502002-04-16 15:50:10 +00001393 * counter: the counter for that transition
1394 *
1395 */
1396static void
1397xmlFAGenerateCountedEpsilonTransition(xmlRegParserCtxtPtr ctxt,
1398 xmlRegStatePtr from, xmlRegStatePtr to, int counter) {
1399 if (to == NULL) {
1400 to = xmlRegNewState(ctxt);
1401 xmlRegStatePush(ctxt, to);
1402 ctxt->state = to;
1403 }
Daniel Veillarddb68b742005-07-30 13:18:24 +00001404 xmlRegStateAddTrans(ctxt, from, NULL, to, counter, -1, 0);
Daniel Veillard4255d502002-04-16 15:50:10 +00001405}
1406
1407/**
1408 * xmlFAGenerateCountedTransition:
Daniel Veillard441bc322002-04-20 17:38:48 +00001409 * @ctxt: a regexp parser context
1410 * @from: the from state
1411 * @to: the target state or NULL for building a new one
Daniel Veillard4255d502002-04-16 15:50:10 +00001412 * counter: the counter for that transition
1413 *
1414 */
1415static void
1416xmlFAGenerateCountedTransition(xmlRegParserCtxtPtr ctxt,
1417 xmlRegStatePtr from, xmlRegStatePtr to, int counter) {
1418 if (to == NULL) {
1419 to = xmlRegNewState(ctxt);
1420 xmlRegStatePush(ctxt, to);
1421 ctxt->state = to;
1422 }
Daniel Veillarddb68b742005-07-30 13:18:24 +00001423 xmlRegStateAddTrans(ctxt, from, NULL, to, -1, counter, 0);
Daniel Veillard4255d502002-04-16 15:50:10 +00001424}
1425
1426/**
1427 * xmlFAGenerateTransitions:
Daniel Veillard441bc322002-04-20 17:38:48 +00001428 * @ctxt: a regexp parser context
1429 * @from: the from state
1430 * @to: the target state or NULL for building a new one
1431 * @atom: the atom generating the transition
Daniel Veillard4255d502002-04-16 15:50:10 +00001432 *
William M. Brackddf71d62004-05-06 04:17:26 +00001433 * Returns 0 if success and -1 in case of error.
Daniel Veillard4255d502002-04-16 15:50:10 +00001434 */
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001435static int
Daniel Veillard4255d502002-04-16 15:50:10 +00001436xmlFAGenerateTransitions(xmlRegParserCtxtPtr ctxt, xmlRegStatePtr from,
1437 xmlRegStatePtr to, xmlRegAtomPtr atom) {
1438 if (atom == NULL) {
1439 ERROR("genrate transition: atom == NULL");
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001440 return(-1);
Daniel Veillard4255d502002-04-16 15:50:10 +00001441 }
1442 if (atom->type == XML_REGEXP_SUBREG) {
1443 /*
1444 * this is a subexpression handling one should not need to
William M. Brackddf71d62004-05-06 04:17:26 +00001445 * create a new node except for XML_REGEXP_QUANT_RANGE.
Daniel Veillard4255d502002-04-16 15:50:10 +00001446 */
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001447 if (xmlRegAtomPush(ctxt, atom) < 0) {
1448 return(-1);
1449 }
Daniel Veillard4255d502002-04-16 15:50:10 +00001450 if ((to != NULL) && (atom->stop != to) &&
1451 (atom->quant != XML_REGEXP_QUANT_RANGE)) {
1452 /*
1453 * Generate an epsilon transition to link to the target
1454 */
1455 xmlFAGenerateEpsilonTransition(ctxt, atom->stop, to);
1456 }
1457 switch (atom->quant) {
1458 case XML_REGEXP_QUANT_OPT:
1459 atom->quant = XML_REGEXP_QUANT_ONCE;
1460 xmlFAGenerateEpsilonTransition(ctxt, atom->start, atom->stop);
1461 break;
1462 case XML_REGEXP_QUANT_MULT:
1463 atom->quant = XML_REGEXP_QUANT_ONCE;
1464 xmlFAGenerateEpsilonTransition(ctxt, atom->start, atom->stop);
1465 xmlFAGenerateEpsilonTransition(ctxt, atom->stop, atom->start);
1466 break;
1467 case XML_REGEXP_QUANT_PLUS:
1468 atom->quant = XML_REGEXP_QUANT_ONCE;
1469 xmlFAGenerateEpsilonTransition(ctxt, atom->stop, atom->start);
1470 break;
1471 case XML_REGEXP_QUANT_RANGE: {
1472 int counter;
1473 xmlRegStatePtr newstate;
1474
1475 /*
1476 * This one is nasty:
William M. Brackddf71d62004-05-06 04:17:26 +00001477 * 1/ if range has minOccurs == 0, create a new state
1478 * and create epsilon transitions from atom->start
1479 * to atom->stop, as well as atom->start to the new
1480 * state
1481 * 2/ register a new counter
1482 * 3/ register an epsilon transition associated to
Daniel Veillard4255d502002-04-16 15:50:10 +00001483 * this counter going from atom->stop to atom->start
William M. Brackddf71d62004-05-06 04:17:26 +00001484 * 4/ create a new state
1485 * 5/ generate a counted transition from atom->stop to
Daniel Veillard4255d502002-04-16 15:50:10 +00001486 * that state
1487 */
William M. Brackddf71d62004-05-06 04:17:26 +00001488 if (atom->min == 0) {
1489 xmlFAGenerateEpsilonTransition(ctxt, atom->start,
1490 atom->stop);
1491 newstate = xmlRegNewState(ctxt);
1492 xmlRegStatePush(ctxt, newstate);
1493 ctxt->state = newstate;
1494 xmlFAGenerateEpsilonTransition(ctxt, atom->start,
1495 newstate);
1496 }
Daniel Veillard4255d502002-04-16 15:50:10 +00001497 counter = xmlRegGetCounter(ctxt);
1498 ctxt->counters[counter].min = atom->min - 1;
1499 ctxt->counters[counter].max = atom->max - 1;
1500 atom->min = 0;
1501 atom->max = 0;
1502 atom->quant = XML_REGEXP_QUANT_ONCE;
1503 xmlFAGenerateCountedEpsilonTransition(ctxt, atom->stop,
1504 atom->start, counter);
1505 if (to != NULL) {
1506 newstate = to;
1507 } else {
1508 newstate = xmlRegNewState(ctxt);
1509 xmlRegStatePush(ctxt, newstate);
1510 ctxt->state = newstate;
1511 }
1512 xmlFAGenerateCountedTransition(ctxt, atom->stop,
1513 newstate, counter);
1514 }
1515 default:
1516 break;
1517 }
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001518 return(0);
Daniel Veillard99c394d2005-07-14 12:58:49 +00001519 } else if ((atom->min == 0) && (atom->max == 0) &&
1520 (atom->quant == XML_REGEXP_QUANT_RANGE)) {
1521 /*
1522 * we can discard the atom and generate an epsilon transition instead
1523 */
1524 if (to == NULL) {
1525 to = xmlRegNewState(ctxt);
1526 if (to != NULL)
1527 xmlRegStatePush(ctxt, to);
1528 else {
1529 return(-1);
1530 }
1531 }
1532 xmlFAGenerateEpsilonTransition(ctxt, from, to);
1533 ctxt->state = to;
1534 xmlRegFreeAtom(atom);
1535 return(0);
Daniel Veillard4255d502002-04-16 15:50:10 +00001536 } else {
1537 if (to == NULL) {
1538 to = xmlRegNewState(ctxt);
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001539 if (to != NULL)
1540 xmlRegStatePush(ctxt, to);
1541 else {
1542 return(-1);
1543 }
1544 }
1545 if (xmlRegAtomPush(ctxt, atom) < 0) {
1546 return(-1);
Daniel Veillard4255d502002-04-16 15:50:10 +00001547 }
Daniel Veillarddb68b742005-07-30 13:18:24 +00001548 xmlRegStateAddTrans(ctxt, from, atom, to, -1, -1, 0);
Daniel Veillard4255d502002-04-16 15:50:10 +00001549 ctxt->state = to;
1550 }
1551 switch (atom->quant) {
1552 case XML_REGEXP_QUANT_OPT:
1553 atom->quant = XML_REGEXP_QUANT_ONCE;
1554 xmlFAGenerateEpsilonTransition(ctxt, from, to);
1555 break;
1556 case XML_REGEXP_QUANT_MULT:
1557 atom->quant = XML_REGEXP_QUANT_ONCE;
1558 xmlFAGenerateEpsilonTransition(ctxt, from, to);
Daniel Veillarddb68b742005-07-30 13:18:24 +00001559 xmlRegStateAddTrans(ctxt, to, atom, to, -1, -1, 0);
Daniel Veillard4255d502002-04-16 15:50:10 +00001560 break;
1561 case XML_REGEXP_QUANT_PLUS:
1562 atom->quant = XML_REGEXP_QUANT_ONCE;
Daniel Veillarddb68b742005-07-30 13:18:24 +00001563 xmlRegStateAddTrans(ctxt, to, atom, to, -1, -1, 0);
Daniel Veillard4255d502002-04-16 15:50:10 +00001564 break;
1565 default:
1566 break;
1567 }
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001568 return(0);
Daniel Veillard4255d502002-04-16 15:50:10 +00001569}
1570
1571/**
1572 * xmlFAReduceEpsilonTransitions:
Daniel Veillard441bc322002-04-20 17:38:48 +00001573 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00001574 * @fromnr: the from state
1575 * @tonr: the to state
William M. Brackddf71d62004-05-06 04:17:26 +00001576 * @counter: should that transition be associated to a counted
Daniel Veillard4255d502002-04-16 15:50:10 +00001577 *
1578 */
1579static void
1580xmlFAReduceEpsilonTransitions(xmlRegParserCtxtPtr ctxt, int fromnr,
1581 int tonr, int counter) {
1582 int transnr;
1583 xmlRegStatePtr from;
1584 xmlRegStatePtr to;
1585
1586#ifdef DEBUG_REGEXP_GRAPH
1587 printf("xmlFAReduceEpsilonTransitions(%d, %d)\n", fromnr, tonr);
1588#endif
1589 from = ctxt->states[fromnr];
1590 if (from == NULL)
1591 return;
1592 to = ctxt->states[tonr];
1593 if (to == NULL)
1594 return;
1595 if ((to->mark == XML_REGEXP_MARK_START) ||
1596 (to->mark == XML_REGEXP_MARK_VISITED))
1597 return;
1598
1599 to->mark = XML_REGEXP_MARK_VISITED;
1600 if (to->type == XML_REGEXP_FINAL_STATE) {
1601#ifdef DEBUG_REGEXP_GRAPH
1602 printf("State %d is final, so %d becomes final\n", tonr, fromnr);
1603#endif
1604 from->type = XML_REGEXP_FINAL_STATE;
1605 }
1606 for (transnr = 0;transnr < to->nbTrans;transnr++) {
Daniel Veillarddb68b742005-07-30 13:18:24 +00001607 if (to->trans[transnr].to < 0)
1608 continue;
Daniel Veillard4255d502002-04-16 15:50:10 +00001609 if (to->trans[transnr].atom == NULL) {
1610 /*
1611 * Don't remove counted transitions
1612 * Don't loop either
1613 */
Daniel Veillardb509f152002-04-17 16:28:10 +00001614 if (to->trans[transnr].to != fromnr) {
1615 if (to->trans[transnr].count >= 0) {
1616 int newto = to->trans[transnr].to;
1617
1618 xmlRegStateAddTrans(ctxt, from, NULL,
1619 ctxt->states[newto],
Daniel Veillarddb68b742005-07-30 13:18:24 +00001620 -1, to->trans[transnr].count, 0);
Daniel Veillardb509f152002-04-17 16:28:10 +00001621 } else {
Daniel Veillard4255d502002-04-16 15:50:10 +00001622#ifdef DEBUG_REGEXP_GRAPH
Daniel Veillardb509f152002-04-17 16:28:10 +00001623 printf("Found epsilon trans %d from %d to %d\n",
1624 transnr, tonr, to->trans[transnr].to);
Daniel Veillard4255d502002-04-16 15:50:10 +00001625#endif
Daniel Veillardb509f152002-04-17 16:28:10 +00001626 if (to->trans[transnr].counter >= 0) {
1627 xmlFAReduceEpsilonTransitions(ctxt, fromnr,
1628 to->trans[transnr].to,
1629 to->trans[transnr].counter);
1630 } else {
1631 xmlFAReduceEpsilonTransitions(ctxt, fromnr,
1632 to->trans[transnr].to,
1633 counter);
1634 }
1635 }
Daniel Veillard4255d502002-04-16 15:50:10 +00001636 }
1637 } else {
1638 int newto = to->trans[transnr].to;
1639
Daniel Veillardb509f152002-04-17 16:28:10 +00001640 if (to->trans[transnr].counter >= 0) {
1641 xmlRegStateAddTrans(ctxt, from, to->trans[transnr].atom,
1642 ctxt->states[newto],
Daniel Veillarddb68b742005-07-30 13:18:24 +00001643 to->trans[transnr].counter, -1, 1);
Daniel Veillardb509f152002-04-17 16:28:10 +00001644 } else {
1645 xmlRegStateAddTrans(ctxt, from, to->trans[transnr].atom,
Daniel Veillarddb68b742005-07-30 13:18:24 +00001646 ctxt->states[newto], counter, -1, 1);
Daniel Veillardb509f152002-04-17 16:28:10 +00001647 }
Daniel Veillard4255d502002-04-16 15:50:10 +00001648 }
1649 }
1650 to->mark = XML_REGEXP_MARK_NORMAL;
1651}
1652
1653/**
Daniel Veillarddb68b742005-07-30 13:18:24 +00001654 * xmlFAEliminateSimpleEpsilonTransitions:
1655 * @ctxt: a regexp parser context
1656 *
1657 * Eliminating general epsilon transitions can get costly in the general
1658 * algorithm due to the large amount of generated new transitions and
1659 * associated comparisons. However for simple epsilon transition used just
1660 * to separate building blocks when generating the automata this can be
1661 * reduced to state elimination:
1662 * - if there exists an epsilon from X to Y
1663 * - if there is no other transition from X
1664 * then X and Y are semantically equivalent and X can be eliminated
1665 * If X is the start state then make Y the start state, else replace the
1666 * target of all transitions to X by transitions to Y.
1667 */
1668static void
1669xmlFAEliminateSimpleEpsilonTransitions(xmlRegParserCtxtPtr ctxt) {
1670 int statenr, i, j, newto;
1671 xmlRegStatePtr state, tmp;
1672
1673 for (statenr = 0;statenr < ctxt->nbStates;statenr++) {
1674 state = ctxt->states[statenr];
1675 if (state == NULL)
1676 continue;
1677 if (state->nbTrans != 1)
1678 continue;
1679 /* is the only transition out a basic transition */
1680 if ((state->trans[0].atom == NULL) &&
1681 (state->trans[0].to >= 0) &&
1682 (state->trans[0].to != statenr) &&
1683 (state->trans[0].counter < 0) &&
1684 (state->trans[0].count < 0)) {
1685 newto = state->trans[0].to;
1686
1687 if (state->type == XML_REGEXP_START_STATE) {
1688#ifdef DEBUG_REGEXP_GRAPH
1689 printf("Found simple epsilon trans from start %d to %d\n",
1690 statenr, newto);
1691#endif
1692 } else {
1693#ifdef DEBUG_REGEXP_GRAPH
1694 printf("Found simple epsilon trans from %d to %d\n",
1695 statenr, newto);
1696#endif
1697 for (i = 0;i < state->nbTransTo;i++) {
1698 tmp = ctxt->states[state->transTo[i]];
1699 for (j = 0;j < tmp->nbTrans;j++) {
1700 if (tmp->trans[j].to == statenr) {
1701 tmp->trans[j].to = newto;
1702#ifdef DEBUG_REGEXP_GRAPH
1703 printf("Changed transition %d on %d to go to %d\n",
1704 j, tmp->no, newto);
1705#endif
1706 xmlRegStateAddTransTo(ctxt, ctxt->states[newto],
1707 tmp->no);
1708 }
1709 }
1710 }
1711#if 0
1712 for (i = 0;i < ctxt->nbStates;i++) {
1713 tmp = ctxt->states[i];
1714 for (j = 0;j < tmp->nbTrans;j++) {
1715 if (tmp->trans[j].to == statenr) {
1716 tmp->trans[j].to = newto;
1717#ifdef DEBUG_REGEXP_GRAPH
1718 printf("Changed transition %d on %d to go to %d\n",
1719 j, tmp->no, newto);
1720#endif
1721 }
1722 }
1723 }
1724#endif
1725 if (state->type == XML_REGEXP_FINAL_STATE)
1726 ctxt->states[newto]->type = XML_REGEXP_FINAL_STATE;
1727 /* eliminate the transition completely */
1728 state->nbTrans = 0;
1729
1730
1731 }
1732
1733 }
1734 }
1735}
1736/**
Daniel Veillard4255d502002-04-16 15:50:10 +00001737 * xmlFAEliminateEpsilonTransitions:
Daniel Veillard441bc322002-04-20 17:38:48 +00001738 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00001739 *
1740 */
1741static void
1742xmlFAEliminateEpsilonTransitions(xmlRegParserCtxtPtr ctxt) {
1743 int statenr, transnr;
1744 xmlRegStatePtr state;
Daniel Veillarddb68b742005-07-30 13:18:24 +00001745 int has_epsilon;
Daniel Veillard4255d502002-04-16 15:50:10 +00001746
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001747 if (ctxt->states == NULL) return;
1748
Daniel Veillarddb68b742005-07-30 13:18:24 +00001749 xmlFAEliminateSimpleEpsilonTransitions(ctxt);
1750
1751 has_epsilon = 0;
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001752
Daniel Veillard4255d502002-04-16 15:50:10 +00001753 /*
1754 * build the completed transitions bypassing the epsilons
1755 * Use a marking algorithm to avoid loops
Daniel Veillardcc026dc2005-01-12 13:21:17 +00001756 * mark sink states too.
Daniel Veillard4255d502002-04-16 15:50:10 +00001757 */
1758 for (statenr = 0;statenr < ctxt->nbStates;statenr++) {
1759 state = ctxt->states[statenr];
1760 if (state == NULL)
1761 continue;
Daniel Veillardcc026dc2005-01-12 13:21:17 +00001762 if ((state->nbTrans == 0) &&
1763 (state->type != XML_REGEXP_FINAL_STATE)) {
1764 state->type = XML_REGEXP_SINK_STATE;
1765 }
Daniel Veillard4255d502002-04-16 15:50:10 +00001766 for (transnr = 0;transnr < state->nbTrans;transnr++) {
1767 if ((state->trans[transnr].atom == NULL) &&
1768 (state->trans[transnr].to >= 0)) {
1769 if (state->trans[transnr].to == statenr) {
1770 state->trans[transnr].to = -1;
1771#ifdef DEBUG_REGEXP_GRAPH
1772 printf("Removed loopback epsilon trans %d on %d\n",
1773 transnr, statenr);
1774#endif
1775 } else if (state->trans[transnr].count < 0) {
1776 int newto = state->trans[transnr].to;
1777
1778#ifdef DEBUG_REGEXP_GRAPH
1779 printf("Found epsilon trans %d from %d to %d\n",
1780 transnr, statenr, newto);
1781#endif
1782 state->mark = XML_REGEXP_MARK_START;
Daniel Veillarddb68b742005-07-30 13:18:24 +00001783 has_epsilon = 1;
Daniel Veillard4255d502002-04-16 15:50:10 +00001784 xmlFAReduceEpsilonTransitions(ctxt, statenr,
1785 newto, state->trans[transnr].counter);
1786 state->mark = XML_REGEXP_MARK_NORMAL;
1787#ifdef DEBUG_REGEXP_GRAPH
1788 } else {
1789 printf("Found counted transition %d on %d\n",
1790 transnr, statenr);
1791#endif
1792 }
1793 }
1794 }
1795 }
1796 /*
1797 * Eliminate the epsilon transitions
1798 */
Daniel Veillarddb68b742005-07-30 13:18:24 +00001799 if (has_epsilon) {
1800 for (statenr = 0;statenr < ctxt->nbStates;statenr++) {
1801 state = ctxt->states[statenr];
1802 if (state == NULL)
1803 continue;
1804 for (transnr = 0;transnr < state->nbTrans;transnr++) {
1805 xmlRegTransPtr trans = &(state->trans[transnr]);
1806 if ((trans->atom == NULL) &&
1807 (trans->count < 0) &&
1808 (trans->to >= 0)) {
1809 trans->to = -1;
1810 }
Daniel Veillard4255d502002-04-16 15:50:10 +00001811 }
1812 }
1813 }
Daniel Veillard23e73572002-09-19 19:56:43 +00001814
1815 /*
1816 * Use this pass to detect unreachable states too
1817 */
1818 for (statenr = 0;statenr < ctxt->nbStates;statenr++) {
1819 state = ctxt->states[statenr];
1820 if (state != NULL)
William M. Brack779af002003-08-01 15:55:39 +00001821 state->reached = XML_REGEXP_MARK_NORMAL;
Daniel Veillard23e73572002-09-19 19:56:43 +00001822 }
1823 state = ctxt->states[0];
1824 if (state != NULL)
William M. Brack779af002003-08-01 15:55:39 +00001825 state->reached = XML_REGEXP_MARK_START;
Daniel Veillard23e73572002-09-19 19:56:43 +00001826 while (state != NULL) {
1827 xmlRegStatePtr target = NULL;
William M. Brack779af002003-08-01 15:55:39 +00001828 state->reached = XML_REGEXP_MARK_VISITED;
Daniel Veillard23e73572002-09-19 19:56:43 +00001829 /*
William M. Brackddf71d62004-05-06 04:17:26 +00001830 * Mark all states reachable from the current reachable state
Daniel Veillard23e73572002-09-19 19:56:43 +00001831 */
1832 for (transnr = 0;transnr < state->nbTrans;transnr++) {
1833 if ((state->trans[transnr].to >= 0) &&
1834 ((state->trans[transnr].atom != NULL) ||
1835 (state->trans[transnr].count >= 0))) {
1836 int newto = state->trans[transnr].to;
1837
1838 if (ctxt->states[newto] == NULL)
1839 continue;
William M. Brack779af002003-08-01 15:55:39 +00001840 if (ctxt->states[newto]->reached == XML_REGEXP_MARK_NORMAL) {
1841 ctxt->states[newto]->reached = XML_REGEXP_MARK_START;
Daniel Veillard23e73572002-09-19 19:56:43 +00001842 target = ctxt->states[newto];
1843 }
1844 }
1845 }
Daniel Veillardcc026dc2005-01-12 13:21:17 +00001846
Daniel Veillard23e73572002-09-19 19:56:43 +00001847 /*
1848 * find the next accessible state not explored
1849 */
1850 if (target == NULL) {
1851 for (statenr = 1;statenr < ctxt->nbStates;statenr++) {
1852 state = ctxt->states[statenr];
William M. Brack779af002003-08-01 15:55:39 +00001853 if ((state != NULL) && (state->reached ==
1854 XML_REGEXP_MARK_START)) {
Daniel Veillard23e73572002-09-19 19:56:43 +00001855 target = state;
1856 break;
1857 }
1858 }
1859 }
1860 state = target;
1861 }
1862 for (statenr = 0;statenr < ctxt->nbStates;statenr++) {
1863 state = ctxt->states[statenr];
William M. Brack779af002003-08-01 15:55:39 +00001864 if ((state != NULL) && (state->reached == XML_REGEXP_MARK_NORMAL)) {
Daniel Veillard23e73572002-09-19 19:56:43 +00001865#ifdef DEBUG_REGEXP_GRAPH
1866 printf("Removed unreachable state %d\n", statenr);
1867#endif
1868 xmlRegFreeState(state);
1869 ctxt->states[statenr] = NULL;
1870 }
1871 }
1872
Daniel Veillard4255d502002-04-16 15:50:10 +00001873}
1874
Daniel Veillarde19fc232002-04-22 16:01:24 +00001875/**
1876 * xmlFACompareAtoms:
1877 * @atom1: an atom
1878 * @atom2: an atom
1879 *
William M. Brackddf71d62004-05-06 04:17:26 +00001880 * Compares two atoms to check whether they are equivalents
Daniel Veillarde19fc232002-04-22 16:01:24 +00001881 *
1882 * Returns 1 if yes and 0 otherwise
1883 */
1884static int
1885xmlFACompareAtoms(xmlRegAtomPtr atom1, xmlRegAtomPtr atom2) {
Daniel Veillard9efc4762005-07-19 14:33:55 +00001886 int ret;
1887
Daniel Veillarde19fc232002-04-22 16:01:24 +00001888 if (atom1 == atom2)
1889 return(1);
1890 if ((atom1 == NULL) || (atom2 == NULL))
1891 return(0);
1892
1893 if (atom1->type != atom2->type)
1894 return(0);
1895 switch (atom1->type) {
1896 case XML_REGEXP_STRING:
Daniel Veillard9efc4762005-07-19 14:33:55 +00001897 ret = xmlRegStrEqualWildcard((xmlChar *)atom1->valuep,
1898 (xmlChar *)atom2->valuep);
1899 break;
Daniel Veillarde19fc232002-04-22 16:01:24 +00001900 case XML_REGEXP_EPSILON:
1901 return(1);
1902 case XML_REGEXP_CHARVAL:
Daniel Veillard9efc4762005-07-19 14:33:55 +00001903 ret = atom1->codepoint == atom2->codepoint;
1904 break;
Daniel Veillarde19fc232002-04-22 16:01:24 +00001905 case XML_REGEXP_RANGES:
1906 TODO;
1907 return(0);
1908 default:
Daniel Veillard9efc4762005-07-19 14:33:55 +00001909 return(1);
Daniel Veillarde19fc232002-04-22 16:01:24 +00001910 }
Daniel Veillard6e65e152005-08-09 11:09:52 +00001911 if (atom1->neg != atom2->neg) {
Daniel Veillard9efc4762005-07-19 14:33:55 +00001912 ret = !ret;
Daniel Veillard6e65e152005-08-09 11:09:52 +00001913 }
Daniel Veillard9efc4762005-07-19 14:33:55 +00001914 return(ret);
Daniel Veillarde19fc232002-04-22 16:01:24 +00001915}
1916
1917/**
1918 * xmlFARecurseDeterminism:
1919 * @ctxt: a regexp parser context
1920 *
1921 * Check whether the associated regexp is determinist,
1922 * should be called after xmlFAEliminateEpsilonTransitions()
1923 *
1924 */
1925static int
1926xmlFARecurseDeterminism(xmlRegParserCtxtPtr ctxt, xmlRegStatePtr state,
1927 int to, xmlRegAtomPtr atom) {
1928 int ret = 1;
1929 int transnr;
1930 xmlRegTransPtr t1;
1931
1932 if (state == NULL)
1933 return(ret);
1934 for (transnr = 0;transnr < state->nbTrans;transnr++) {
1935 t1 = &(state->trans[transnr]);
1936 /*
1937 * check transitions conflicting with the one looked at
1938 */
1939 if (t1->atom == NULL) {
1940 if (t1->to == -1)
1941 continue;
1942 ret = xmlFARecurseDeterminism(ctxt, ctxt->states[t1->to],
1943 to, atom);
1944 if (ret == 0)
1945 return(0);
1946 continue;
1947 }
1948 if (t1->to != to)
1949 continue;
1950 if (xmlFACompareAtoms(t1->atom, atom))
1951 return(0);
1952 }
1953 return(ret);
1954}
1955
1956/**
1957 * xmlFAComputesDeterminism:
1958 * @ctxt: a regexp parser context
1959 *
1960 * Check whether the associated regexp is determinist,
1961 * should be called after xmlFAEliminateEpsilonTransitions()
1962 *
1963 */
1964static int
1965xmlFAComputesDeterminism(xmlRegParserCtxtPtr ctxt) {
1966 int statenr, transnr;
1967 xmlRegStatePtr state;
1968 xmlRegTransPtr t1, t2;
1969 int i;
1970 int ret = 1;
1971
Daniel Veillard4402ab42002-09-12 16:02:56 +00001972#ifdef DEBUG_REGEXP_GRAPH
1973 printf("xmlFAComputesDeterminism\n");
1974 xmlRegPrintCtxt(stdout, ctxt);
1975#endif
Daniel Veillarde19fc232002-04-22 16:01:24 +00001976 if (ctxt->determinist != -1)
1977 return(ctxt->determinist);
1978
1979 /*
William M. Brackddf71d62004-05-06 04:17:26 +00001980 * Check for all states that there aren't 2 transitions
Daniel Veillarde19fc232002-04-22 16:01:24 +00001981 * with the same atom and a different target.
1982 */
1983 for (statenr = 0;statenr < ctxt->nbStates;statenr++) {
1984 state = ctxt->states[statenr];
1985 if (state == NULL)
1986 continue;
Daniel Veillard4f82c8a2005-08-09 21:40:08 +00001987 if (state->nbTrans < 2)
1988 continue;
Daniel Veillarde19fc232002-04-22 16:01:24 +00001989 for (transnr = 0;transnr < state->nbTrans;transnr++) {
1990 t1 = &(state->trans[transnr]);
1991 /*
1992 * Determinism checks in case of counted or all transitions
1993 * will have to be handled separately
1994 */
1995 if (t1->atom == NULL)
1996 continue;
1997 if (t1->to == -1) /* eliminated */
1998 continue;
1999 for (i = 0;i < transnr;i++) {
2000 t2 = &(state->trans[i]);
2001 if (t2->to == -1) /* eliminated */
2002 continue;
2003 if (t2->atom != NULL) {
2004 if (t1->to == t2->to) {
2005 if (xmlFACompareAtoms(t1->atom, t2->atom))
William M. Brackddf71d62004-05-06 04:17:26 +00002006 t2->to = -1; /* eliminated */
Daniel Veillarde19fc232002-04-22 16:01:24 +00002007 } else {
2008 /* not determinist ! */
2009 if (xmlFACompareAtoms(t1->atom, t2->atom))
2010 ret = 0;
2011 }
2012 } else if (t1->to != -1) {
2013 /*
2014 * do the closure in case of remaining specific
2015 * epsilon transitions like choices or all
2016 */
2017 ret = xmlFARecurseDeterminism(ctxt, ctxt->states[t1->to],
2018 t2->to, t2->atom);
2019 if (ret == 0)
2020 return(0);
2021 }
2022 }
2023 if (ret == 0)
2024 break;
2025 }
2026 if (ret == 0)
2027 break;
2028 }
2029 ctxt->determinist = ret;
2030 return(ret);
2031}
2032
Daniel Veillard4255d502002-04-16 15:50:10 +00002033/************************************************************************
2034 * *
2035 * Routines to check input against transition atoms *
2036 * *
2037 ************************************************************************/
2038
2039static int
2040xmlRegCheckCharacterRange(xmlRegAtomType type, int codepoint, int neg,
2041 int start, int end, const xmlChar *blockName) {
2042 int ret = 0;
2043
2044 switch (type) {
2045 case XML_REGEXP_STRING:
2046 case XML_REGEXP_SUBREG:
2047 case XML_REGEXP_RANGES:
2048 case XML_REGEXP_EPSILON:
2049 return(-1);
2050 case XML_REGEXP_ANYCHAR:
2051 ret = ((codepoint != '\n') && (codepoint != '\r'));
2052 break;
2053 case XML_REGEXP_CHARVAL:
2054 ret = ((codepoint >= start) && (codepoint <= end));
2055 break;
2056 case XML_REGEXP_NOTSPACE:
2057 neg = !neg;
2058 case XML_REGEXP_ANYSPACE:
2059 ret = ((codepoint == '\n') || (codepoint == '\r') ||
2060 (codepoint == '\t') || (codepoint == ' '));
2061 break;
2062 case XML_REGEXP_NOTINITNAME:
2063 neg = !neg;
2064 case XML_REGEXP_INITNAME:
William M. Brack871611b2003-10-18 04:53:14 +00002065 ret = (IS_LETTER(codepoint) ||
Daniel Veillard4255d502002-04-16 15:50:10 +00002066 (codepoint == '_') || (codepoint == ':'));
2067 break;
2068 case XML_REGEXP_NOTNAMECHAR:
2069 neg = !neg;
2070 case XML_REGEXP_NAMECHAR:
William M. Brack871611b2003-10-18 04:53:14 +00002071 ret = (IS_LETTER(codepoint) || IS_DIGIT(codepoint) ||
Daniel Veillard4255d502002-04-16 15:50:10 +00002072 (codepoint == '.') || (codepoint == '-') ||
2073 (codepoint == '_') || (codepoint == ':') ||
William M. Brack871611b2003-10-18 04:53:14 +00002074 IS_COMBINING(codepoint) || IS_EXTENDER(codepoint));
Daniel Veillard4255d502002-04-16 15:50:10 +00002075 break;
2076 case XML_REGEXP_NOTDECIMAL:
2077 neg = !neg;
2078 case XML_REGEXP_DECIMAL:
2079 ret = xmlUCSIsCatNd(codepoint);
2080 break;
2081 case XML_REGEXP_REALCHAR:
2082 neg = !neg;
2083 case XML_REGEXP_NOTREALCHAR:
2084 ret = xmlUCSIsCatP(codepoint);
2085 if (ret == 0)
2086 ret = xmlUCSIsCatZ(codepoint);
2087 if (ret == 0)
2088 ret = xmlUCSIsCatC(codepoint);
2089 break;
2090 case XML_REGEXP_LETTER:
2091 ret = xmlUCSIsCatL(codepoint);
2092 break;
2093 case XML_REGEXP_LETTER_UPPERCASE:
2094 ret = xmlUCSIsCatLu(codepoint);
2095 break;
2096 case XML_REGEXP_LETTER_LOWERCASE:
2097 ret = xmlUCSIsCatLl(codepoint);
2098 break;
2099 case XML_REGEXP_LETTER_TITLECASE:
2100 ret = xmlUCSIsCatLt(codepoint);
2101 break;
2102 case XML_REGEXP_LETTER_MODIFIER:
2103 ret = xmlUCSIsCatLm(codepoint);
2104 break;
2105 case XML_REGEXP_LETTER_OTHERS:
2106 ret = xmlUCSIsCatLo(codepoint);
2107 break;
2108 case XML_REGEXP_MARK:
2109 ret = xmlUCSIsCatM(codepoint);
2110 break;
2111 case XML_REGEXP_MARK_NONSPACING:
2112 ret = xmlUCSIsCatMn(codepoint);
2113 break;
2114 case XML_REGEXP_MARK_SPACECOMBINING:
2115 ret = xmlUCSIsCatMc(codepoint);
2116 break;
2117 case XML_REGEXP_MARK_ENCLOSING:
2118 ret = xmlUCSIsCatMe(codepoint);
2119 break;
2120 case XML_REGEXP_NUMBER:
2121 ret = xmlUCSIsCatN(codepoint);
2122 break;
2123 case XML_REGEXP_NUMBER_DECIMAL:
2124 ret = xmlUCSIsCatNd(codepoint);
2125 break;
2126 case XML_REGEXP_NUMBER_LETTER:
2127 ret = xmlUCSIsCatNl(codepoint);
2128 break;
2129 case XML_REGEXP_NUMBER_OTHERS:
2130 ret = xmlUCSIsCatNo(codepoint);
2131 break;
2132 case XML_REGEXP_PUNCT:
2133 ret = xmlUCSIsCatP(codepoint);
2134 break;
2135 case XML_REGEXP_PUNCT_CONNECTOR:
2136 ret = xmlUCSIsCatPc(codepoint);
2137 break;
2138 case XML_REGEXP_PUNCT_DASH:
2139 ret = xmlUCSIsCatPd(codepoint);
2140 break;
2141 case XML_REGEXP_PUNCT_OPEN:
2142 ret = xmlUCSIsCatPs(codepoint);
2143 break;
2144 case XML_REGEXP_PUNCT_CLOSE:
2145 ret = xmlUCSIsCatPe(codepoint);
2146 break;
2147 case XML_REGEXP_PUNCT_INITQUOTE:
2148 ret = xmlUCSIsCatPi(codepoint);
2149 break;
2150 case XML_REGEXP_PUNCT_FINQUOTE:
2151 ret = xmlUCSIsCatPf(codepoint);
2152 break;
2153 case XML_REGEXP_PUNCT_OTHERS:
2154 ret = xmlUCSIsCatPo(codepoint);
2155 break;
2156 case XML_REGEXP_SEPAR:
2157 ret = xmlUCSIsCatZ(codepoint);
2158 break;
2159 case XML_REGEXP_SEPAR_SPACE:
2160 ret = xmlUCSIsCatZs(codepoint);
2161 break;
2162 case XML_REGEXP_SEPAR_LINE:
2163 ret = xmlUCSIsCatZl(codepoint);
2164 break;
2165 case XML_REGEXP_SEPAR_PARA:
2166 ret = xmlUCSIsCatZp(codepoint);
2167 break;
2168 case XML_REGEXP_SYMBOL:
2169 ret = xmlUCSIsCatS(codepoint);
2170 break;
2171 case XML_REGEXP_SYMBOL_MATH:
2172 ret = xmlUCSIsCatSm(codepoint);
2173 break;
2174 case XML_REGEXP_SYMBOL_CURRENCY:
2175 ret = xmlUCSIsCatSc(codepoint);
2176 break;
2177 case XML_REGEXP_SYMBOL_MODIFIER:
2178 ret = xmlUCSIsCatSk(codepoint);
2179 break;
2180 case XML_REGEXP_SYMBOL_OTHERS:
2181 ret = xmlUCSIsCatSo(codepoint);
2182 break;
2183 case XML_REGEXP_OTHER:
2184 ret = xmlUCSIsCatC(codepoint);
2185 break;
2186 case XML_REGEXP_OTHER_CONTROL:
2187 ret = xmlUCSIsCatCc(codepoint);
2188 break;
2189 case XML_REGEXP_OTHER_FORMAT:
2190 ret = xmlUCSIsCatCf(codepoint);
2191 break;
2192 case XML_REGEXP_OTHER_PRIVATE:
2193 ret = xmlUCSIsCatCo(codepoint);
2194 break;
2195 case XML_REGEXP_OTHER_NA:
2196 /* ret = xmlUCSIsCatCn(codepoint); */
2197 /* Seems it doesn't exist anymore in recent Unicode releases */
2198 ret = 0;
2199 break;
2200 case XML_REGEXP_BLOCK_NAME:
2201 ret = xmlUCSIsBlock(codepoint, (const char *) blockName);
2202 break;
2203 }
2204 if (neg)
2205 return(!ret);
2206 return(ret);
2207}
2208
2209static int
2210xmlRegCheckCharacter(xmlRegAtomPtr atom, int codepoint) {
2211 int i, ret = 0;
2212 xmlRegRangePtr range;
2213
William M. Brack871611b2003-10-18 04:53:14 +00002214 if ((atom == NULL) || (!IS_CHAR(codepoint)))
Daniel Veillard4255d502002-04-16 15:50:10 +00002215 return(-1);
2216
2217 switch (atom->type) {
2218 case XML_REGEXP_SUBREG:
2219 case XML_REGEXP_EPSILON:
2220 return(-1);
2221 case XML_REGEXP_CHARVAL:
2222 return(codepoint == atom->codepoint);
2223 case XML_REGEXP_RANGES: {
2224 int accept = 0;
Daniel Veillardf2a12832003-11-24 13:04:35 +00002225
Daniel Veillard4255d502002-04-16 15:50:10 +00002226 for (i = 0;i < atom->nbRanges;i++) {
2227 range = atom->ranges[i];
Daniel Veillardf8b9de32003-11-24 14:27:26 +00002228 if (range->neg == 2) {
Daniel Veillard4255d502002-04-16 15:50:10 +00002229 ret = xmlRegCheckCharacterRange(range->type, codepoint,
2230 0, range->start, range->end,
2231 range->blockName);
2232 if (ret != 0)
2233 return(0); /* excluded char */
Daniel Veillardf8b9de32003-11-24 14:27:26 +00002234 } else if (range->neg) {
2235 ret = xmlRegCheckCharacterRange(range->type, codepoint,
2236 0, range->start, range->end,
2237 range->blockName);
2238 if (ret == 0)
Daniel Veillardf2a12832003-11-24 13:04:35 +00002239 accept = 1;
Daniel Veillardf8b9de32003-11-24 14:27:26 +00002240 else
2241 return(0);
Daniel Veillard4255d502002-04-16 15:50:10 +00002242 } else {
2243 ret = xmlRegCheckCharacterRange(range->type, codepoint,
2244 0, range->start, range->end,
2245 range->blockName);
2246 if (ret != 0)
2247 accept = 1; /* might still be excluded */
2248 }
2249 }
2250 return(accept);
2251 }
2252 case XML_REGEXP_STRING:
2253 printf("TODO: XML_REGEXP_STRING\n");
2254 return(-1);
2255 case XML_REGEXP_ANYCHAR:
2256 case XML_REGEXP_ANYSPACE:
2257 case XML_REGEXP_NOTSPACE:
2258 case XML_REGEXP_INITNAME:
2259 case XML_REGEXP_NOTINITNAME:
2260 case XML_REGEXP_NAMECHAR:
2261 case XML_REGEXP_NOTNAMECHAR:
2262 case XML_REGEXP_DECIMAL:
2263 case XML_REGEXP_NOTDECIMAL:
2264 case XML_REGEXP_REALCHAR:
2265 case XML_REGEXP_NOTREALCHAR:
2266 case XML_REGEXP_LETTER:
2267 case XML_REGEXP_LETTER_UPPERCASE:
2268 case XML_REGEXP_LETTER_LOWERCASE:
2269 case XML_REGEXP_LETTER_TITLECASE:
2270 case XML_REGEXP_LETTER_MODIFIER:
2271 case XML_REGEXP_LETTER_OTHERS:
2272 case XML_REGEXP_MARK:
2273 case XML_REGEXP_MARK_NONSPACING:
2274 case XML_REGEXP_MARK_SPACECOMBINING:
2275 case XML_REGEXP_MARK_ENCLOSING:
2276 case XML_REGEXP_NUMBER:
2277 case XML_REGEXP_NUMBER_DECIMAL:
2278 case XML_REGEXP_NUMBER_LETTER:
2279 case XML_REGEXP_NUMBER_OTHERS:
2280 case XML_REGEXP_PUNCT:
2281 case XML_REGEXP_PUNCT_CONNECTOR:
2282 case XML_REGEXP_PUNCT_DASH:
2283 case XML_REGEXP_PUNCT_OPEN:
2284 case XML_REGEXP_PUNCT_CLOSE:
2285 case XML_REGEXP_PUNCT_INITQUOTE:
2286 case XML_REGEXP_PUNCT_FINQUOTE:
2287 case XML_REGEXP_PUNCT_OTHERS:
2288 case XML_REGEXP_SEPAR:
2289 case XML_REGEXP_SEPAR_SPACE:
2290 case XML_REGEXP_SEPAR_LINE:
2291 case XML_REGEXP_SEPAR_PARA:
2292 case XML_REGEXP_SYMBOL:
2293 case XML_REGEXP_SYMBOL_MATH:
2294 case XML_REGEXP_SYMBOL_CURRENCY:
2295 case XML_REGEXP_SYMBOL_MODIFIER:
2296 case XML_REGEXP_SYMBOL_OTHERS:
2297 case XML_REGEXP_OTHER:
2298 case XML_REGEXP_OTHER_CONTROL:
2299 case XML_REGEXP_OTHER_FORMAT:
2300 case XML_REGEXP_OTHER_PRIVATE:
2301 case XML_REGEXP_OTHER_NA:
2302 case XML_REGEXP_BLOCK_NAME:
2303 ret = xmlRegCheckCharacterRange(atom->type, codepoint, 0, 0, 0,
2304 (const xmlChar *)atom->valuep);
2305 if (atom->neg)
2306 ret = !ret;
2307 break;
2308 }
2309 return(ret);
2310}
2311
2312/************************************************************************
2313 * *
William M. Brackddf71d62004-05-06 04:17:26 +00002314 * Saving and restoring state of an execution context *
Daniel Veillard4255d502002-04-16 15:50:10 +00002315 * *
2316 ************************************************************************/
2317
2318#ifdef DEBUG_REGEXP_EXEC
2319static void
2320xmlFARegDebugExec(xmlRegExecCtxtPtr exec) {
2321 printf("state: %d:%d:idx %d", exec->state->no, exec->transno, exec->index);
2322 if (exec->inputStack != NULL) {
2323 int i;
2324 printf(": ");
2325 for (i = 0;(i < 3) && (i < exec->inputStackNr);i++)
2326 printf("%s ", exec->inputStack[exec->inputStackNr - (i + 1)]);
2327 } else {
2328 printf(": %s", &(exec->inputString[exec->index]));
2329 }
2330 printf("\n");
2331}
2332#endif
2333
2334static void
2335xmlFARegExecSave(xmlRegExecCtxtPtr exec) {
2336#ifdef DEBUG_REGEXP_EXEC
2337 printf("saving ");
2338 exec->transno++;
2339 xmlFARegDebugExec(exec);
2340 exec->transno--;
2341#endif
Daniel Veillard94cc1032005-09-15 13:09:00 +00002342#ifdef MAX_PUSH
2343 if (exec->nbPush > MAX_PUSH) {
2344 return;
2345 }
2346 exec->nbPush++;
2347#endif
Daniel Veillard4255d502002-04-16 15:50:10 +00002348
2349 if (exec->maxRollbacks == 0) {
2350 exec->maxRollbacks = 4;
2351 exec->rollbacks = (xmlRegExecRollback *) xmlMalloc(exec->maxRollbacks *
2352 sizeof(xmlRegExecRollback));
2353 if (exec->rollbacks == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00002354 xmlRegexpErrMemory(NULL, "saving regexp");
Daniel Veillard4255d502002-04-16 15:50:10 +00002355 exec->maxRollbacks = 0;
2356 return;
2357 }
2358 memset(exec->rollbacks, 0,
2359 exec->maxRollbacks * sizeof(xmlRegExecRollback));
2360 } else if (exec->nbRollbacks >= exec->maxRollbacks) {
2361 xmlRegExecRollback *tmp;
2362 int len = exec->maxRollbacks;
2363
2364 exec->maxRollbacks *= 2;
2365 tmp = (xmlRegExecRollback *) xmlRealloc(exec->rollbacks,
2366 exec->maxRollbacks * sizeof(xmlRegExecRollback));
2367 if (tmp == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00002368 xmlRegexpErrMemory(NULL, "saving regexp");
Daniel Veillard4255d502002-04-16 15:50:10 +00002369 exec->maxRollbacks /= 2;
2370 return;
2371 }
2372 exec->rollbacks = tmp;
2373 tmp = &exec->rollbacks[len];
2374 memset(tmp, 0, (exec->maxRollbacks - len) * sizeof(xmlRegExecRollback));
2375 }
2376 exec->rollbacks[exec->nbRollbacks].state = exec->state;
2377 exec->rollbacks[exec->nbRollbacks].index = exec->index;
2378 exec->rollbacks[exec->nbRollbacks].nextbranch = exec->transno + 1;
2379 if (exec->comp->nbCounters > 0) {
2380 if (exec->rollbacks[exec->nbRollbacks].counts == NULL) {
2381 exec->rollbacks[exec->nbRollbacks].counts = (int *)
2382 xmlMalloc(exec->comp->nbCounters * sizeof(int));
2383 if (exec->rollbacks[exec->nbRollbacks].counts == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00002384 xmlRegexpErrMemory(NULL, "saving regexp");
Daniel Veillard4255d502002-04-16 15:50:10 +00002385 exec->status = -5;
2386 return;
2387 }
2388 }
2389 memcpy(exec->rollbacks[exec->nbRollbacks].counts, exec->counts,
2390 exec->comp->nbCounters * sizeof(int));
2391 }
2392 exec->nbRollbacks++;
2393}
2394
2395static void
2396xmlFARegExecRollBack(xmlRegExecCtxtPtr exec) {
2397 if (exec->nbRollbacks <= 0) {
2398 exec->status = -1;
2399#ifdef DEBUG_REGEXP_EXEC
2400 printf("rollback failed on empty stack\n");
2401#endif
2402 return;
2403 }
2404 exec->nbRollbacks--;
2405 exec->state = exec->rollbacks[exec->nbRollbacks].state;
2406 exec->index = exec->rollbacks[exec->nbRollbacks].index;
2407 exec->transno = exec->rollbacks[exec->nbRollbacks].nextbranch;
2408 if (exec->comp->nbCounters > 0) {
2409 if (exec->rollbacks[exec->nbRollbacks].counts == NULL) {
2410 fprintf(stderr, "exec save: allocation failed");
2411 exec->status = -6;
2412 return;
2413 }
2414 memcpy(exec->counts, exec->rollbacks[exec->nbRollbacks].counts,
2415 exec->comp->nbCounters * sizeof(int));
2416 }
2417
2418#ifdef DEBUG_REGEXP_EXEC
2419 printf("restored ");
2420 xmlFARegDebugExec(exec);
2421#endif
2422}
2423
2424/************************************************************************
2425 * *
William M. Brackddf71d62004-05-06 04:17:26 +00002426 * Verifier, running an input against a compiled regexp *
Daniel Veillard4255d502002-04-16 15:50:10 +00002427 * *
2428 ************************************************************************/
2429
2430static int
2431xmlFARegExec(xmlRegexpPtr comp, const xmlChar *content) {
2432 xmlRegExecCtxt execval;
2433 xmlRegExecCtxtPtr exec = &execval;
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00002434 int ret, codepoint = 0, len;
Daniel Veillard4255d502002-04-16 15:50:10 +00002435
2436 exec->inputString = content;
2437 exec->index = 0;
Daniel Veillard94cc1032005-09-15 13:09:00 +00002438 exec->nbPush = 0;
Daniel Veillard4255d502002-04-16 15:50:10 +00002439 exec->determinist = 1;
2440 exec->maxRollbacks = 0;
2441 exec->nbRollbacks = 0;
2442 exec->rollbacks = NULL;
2443 exec->status = 0;
2444 exec->comp = comp;
2445 exec->state = comp->states[0];
2446 exec->transno = 0;
2447 exec->transcount = 0;
Daniel Veillardf2a12832003-11-24 13:04:35 +00002448 exec->inputStack = NULL;
2449 exec->inputStackMax = 0;
Daniel Veillard4255d502002-04-16 15:50:10 +00002450 if (comp->nbCounters > 0) {
2451 exec->counts = (int *) xmlMalloc(comp->nbCounters * sizeof(int));
Daniel Veillardff46a042003-10-08 08:53:17 +00002452 if (exec->counts == NULL) {
2453 xmlRegexpErrMemory(NULL, "running regexp");
Daniel Veillard4255d502002-04-16 15:50:10 +00002454 return(-1);
Daniel Veillardff46a042003-10-08 08:53:17 +00002455 }
Daniel Veillard4255d502002-04-16 15:50:10 +00002456 memset(exec->counts, 0, comp->nbCounters * sizeof(int));
2457 } else
2458 exec->counts = NULL;
2459 while ((exec->status == 0) &&
2460 ((exec->inputString[exec->index] != 0) ||
2461 (exec->state->type != XML_REGEXP_FINAL_STATE))) {
2462 xmlRegTransPtr trans;
2463 xmlRegAtomPtr atom;
2464
2465 /*
William M. Brack0e00b282004-04-26 15:40:47 +00002466 * If end of input on non-terminal state, rollback, however we may
Daniel Veillard4255d502002-04-16 15:50:10 +00002467 * still have epsilon like transition for counted transitions
William M. Brack0e00b282004-04-26 15:40:47 +00002468 * on counters, in that case don't break too early. Additionally,
2469 * if we are working on a range like "AB{0,2}", where B is not present,
2470 * we don't want to break.
Daniel Veillard4255d502002-04-16 15:50:10 +00002471 */
William M. Brack0e00b282004-04-26 15:40:47 +00002472 if ((exec->inputString[exec->index] == 0) && (exec->counts == NULL)) {
William M. Brackddf71d62004-05-06 04:17:26 +00002473 /*
2474 * if there is a transition, we must check if
2475 * atom allows minOccurs of 0
2476 */
2477 if (exec->transno < exec->state->nbTrans) {
William M. Brack0e00b282004-04-26 15:40:47 +00002478 trans = &exec->state->trans[exec->transno];
2479 if (trans->to >=0) {
2480 atom = trans->atom;
2481 if (!((atom->min == 0) && (atom->max > 0)))
2482 goto rollback;
2483 }
2484 } else
2485 goto rollback;
2486 }
Daniel Veillard4255d502002-04-16 15:50:10 +00002487
2488 exec->transcount = 0;
2489 for (;exec->transno < exec->state->nbTrans;exec->transno++) {
2490 trans = &exec->state->trans[exec->transno];
2491 if (trans->to < 0)
2492 continue;
2493 atom = trans->atom;
2494 ret = 0;
2495 if (trans->count >= 0) {
2496 int count;
2497 xmlRegCounterPtr counter;
2498
2499 /*
2500 * A counted transition.
2501 */
2502
2503 count = exec->counts[trans->count];
2504 counter = &exec->comp->counters[trans->count];
2505#ifdef DEBUG_REGEXP_EXEC
2506 printf("testing count %d: val %d, min %d, max %d\n",
2507 trans->count, count, counter->min, counter->max);
2508#endif
2509 ret = ((count >= counter->min) && (count <= counter->max));
2510 } else if (atom == NULL) {
2511 fprintf(stderr, "epsilon transition left at runtime\n");
2512 exec->status = -2;
2513 break;
2514 } else if (exec->inputString[exec->index] != 0) {
2515 codepoint = CUR_SCHAR(&(exec->inputString[exec->index]), len);
2516 ret = xmlRegCheckCharacter(atom, codepoint);
William M. Brack0e00b282004-04-26 15:40:47 +00002517 if ((ret == 1) && (atom->min >= 0) && (atom->max > 0)) {
Daniel Veillard4255d502002-04-16 15:50:10 +00002518 xmlRegStatePtr to = comp->states[trans->to];
2519
2520 /*
2521 * this is a multiple input sequence
2522 */
2523 if (exec->state->nbTrans > exec->transno + 1) {
2524 xmlFARegExecSave(exec);
2525 }
2526 exec->transcount = 1;
2527 do {
2528 /*
2529 * Try to progress as much as possible on the input
2530 */
2531 if (exec->transcount == atom->max) {
2532 break;
2533 }
2534 exec->index += len;
2535 /*
2536 * End of input: stop here
2537 */
2538 if (exec->inputString[exec->index] == 0) {
2539 exec->index -= len;
2540 break;
2541 }
2542 if (exec->transcount >= atom->min) {
2543 int transno = exec->transno;
2544 xmlRegStatePtr state = exec->state;
2545
2546 /*
2547 * The transition is acceptable save it
2548 */
2549 exec->transno = -1; /* trick */
2550 exec->state = to;
2551 xmlFARegExecSave(exec);
2552 exec->transno = transno;
2553 exec->state = state;
2554 }
2555 codepoint = CUR_SCHAR(&(exec->inputString[exec->index]),
2556 len);
2557 ret = xmlRegCheckCharacter(atom, codepoint);
2558 exec->transcount++;
2559 } while (ret == 1);
2560 if (exec->transcount < atom->min)
2561 ret = 0;
2562
2563 /*
2564 * If the last check failed but one transition was found
2565 * possible, rollback
2566 */
2567 if (ret < 0)
2568 ret = 0;
2569 if (ret == 0) {
2570 goto rollback;
2571 }
William M. Brack0e00b282004-04-26 15:40:47 +00002572 } else if ((ret == 0) && (atom->min == 0) && (atom->max > 0)) {
2573 /*
2574 * we don't match on the codepoint, but minOccurs of 0
2575 * says that's ok. Setting len to 0 inhibits stepping
2576 * over the codepoint.
2577 */
2578 exec->transcount = 1;
2579 len = 0;
2580 ret = 1;
Daniel Veillard4255d502002-04-16 15:50:10 +00002581 }
William M. Brack0e00b282004-04-26 15:40:47 +00002582 } else if ((atom->min == 0) && (atom->max > 0)) {
2583 /* another spot to match when minOccurs is 0 */
2584 exec->transcount = 1;
2585 len = 0;
2586 ret = 1;
Daniel Veillard4255d502002-04-16 15:50:10 +00002587 }
2588 if (ret == 1) {
2589 if (exec->state->nbTrans > exec->transno + 1) {
2590 xmlFARegExecSave(exec);
2591 }
2592 if (trans->counter >= 0) {
2593#ifdef DEBUG_REGEXP_EXEC
2594 printf("Increasing count %d\n", trans->counter);
2595#endif
2596 exec->counts[trans->counter]++;
2597 }
Daniel Veillard10752282005-08-08 13:05:13 +00002598 if ((trans->count >= 0) &&
2599 (trans->count < REGEXP_ALL_COUNTER)) {
2600#ifdef DEBUG_REGEXP_EXEC
2601 printf("resetting count %d on transition\n",
2602 trans->count);
2603#endif
2604 exec->counts[trans->count] = 0;
2605 }
Daniel Veillard4255d502002-04-16 15:50:10 +00002606#ifdef DEBUG_REGEXP_EXEC
2607 printf("entering state %d\n", trans->to);
2608#endif
2609 exec->state = comp->states[trans->to];
2610 exec->transno = 0;
2611 if (trans->atom != NULL) {
2612 exec->index += len;
2613 }
2614 goto progress;
2615 } else if (ret < 0) {
2616 exec->status = -4;
2617 break;
2618 }
2619 }
2620 if ((exec->transno != 0) || (exec->state->nbTrans == 0)) {
2621rollback:
2622 /*
2623 * Failed to find a way out
2624 */
2625 exec->determinist = 0;
2626 xmlFARegExecRollBack(exec);
2627 }
2628progress:
2629 continue;
2630 }
2631 if (exec->rollbacks != NULL) {
2632 if (exec->counts != NULL) {
2633 int i;
2634
2635 for (i = 0;i < exec->maxRollbacks;i++)
2636 if (exec->rollbacks[i].counts != NULL)
2637 xmlFree(exec->rollbacks[i].counts);
2638 }
2639 xmlFree(exec->rollbacks);
2640 }
2641 if (exec->counts != NULL)
2642 xmlFree(exec->counts);
2643 if (exec->status == 0)
2644 return(1);
Daniel Veillard94cc1032005-09-15 13:09:00 +00002645 if (exec->status == -1) {
2646 if (exec->nbPush > MAX_PUSH)
2647 return(-1);
Daniel Veillard4255d502002-04-16 15:50:10 +00002648 return(0);
Daniel Veillard94cc1032005-09-15 13:09:00 +00002649 }
Daniel Veillard4255d502002-04-16 15:50:10 +00002650 return(exec->status);
2651}
2652
2653/************************************************************************
2654 * *
William M. Brackddf71d62004-05-06 04:17:26 +00002655 * Progressive interface to the verifier one atom at a time *
Daniel Veillard4255d502002-04-16 15:50:10 +00002656 * *
2657 ************************************************************************/
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00002658#ifdef DEBUG_ERR
2659static void testerr(xmlRegExecCtxtPtr exec);
2660#endif
Daniel Veillard4255d502002-04-16 15:50:10 +00002661
2662/**
Daniel Veillard01c13b52002-12-10 15:19:08 +00002663 * xmlRegNewExecCtxt:
Daniel Veillard4255d502002-04-16 15:50:10 +00002664 * @comp: a precompiled regular expression
2665 * @callback: a callback function used for handling progresses in the
2666 * automata matching phase
2667 * @data: the context data associated to the callback in this context
2668 *
2669 * Build a context used for progressive evaluation of a regexp.
Daniel Veillard01c13b52002-12-10 15:19:08 +00002670 *
2671 * Returns the new context
Daniel Veillard4255d502002-04-16 15:50:10 +00002672 */
2673xmlRegExecCtxtPtr
2674xmlRegNewExecCtxt(xmlRegexpPtr comp, xmlRegExecCallbacks callback, void *data) {
2675 xmlRegExecCtxtPtr exec;
2676
2677 if (comp == NULL)
2678 return(NULL);
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00002679 if ((comp->compact == NULL) && (comp->states == NULL))
2680 return(NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +00002681 exec = (xmlRegExecCtxtPtr) xmlMalloc(sizeof(xmlRegExecCtxt));
2682 if (exec == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00002683 xmlRegexpErrMemory(NULL, "creating execution context");
Daniel Veillard4255d502002-04-16 15:50:10 +00002684 return(NULL);
2685 }
2686 memset(exec, 0, sizeof(xmlRegExecCtxt));
2687 exec->inputString = NULL;
2688 exec->index = 0;
2689 exec->determinist = 1;
2690 exec->maxRollbacks = 0;
2691 exec->nbRollbacks = 0;
2692 exec->rollbacks = NULL;
2693 exec->status = 0;
2694 exec->comp = comp;
Daniel Veillard23e73572002-09-19 19:56:43 +00002695 if (comp->compact == NULL)
2696 exec->state = comp->states[0];
Daniel Veillard4255d502002-04-16 15:50:10 +00002697 exec->transno = 0;
2698 exec->transcount = 0;
2699 exec->callback = callback;
2700 exec->data = data;
2701 if (comp->nbCounters > 0) {
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00002702 /*
2703 * For error handling, exec->counts is allocated twice the size
2704 * the second half is used to store the data in case of rollback
2705 */
2706 exec->counts = (int *) xmlMalloc(comp->nbCounters * sizeof(int)
2707 * 2);
Daniel Veillard4255d502002-04-16 15:50:10 +00002708 if (exec->counts == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00002709 xmlRegexpErrMemory(NULL, "creating execution context");
Daniel Veillard4255d502002-04-16 15:50:10 +00002710 xmlFree(exec);
2711 return(NULL);
2712 }
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00002713 memset(exec->counts, 0, comp->nbCounters * sizeof(int) * 2);
2714 exec->errCounts = &exec->counts[comp->nbCounters];
2715 } else {
Daniel Veillard4255d502002-04-16 15:50:10 +00002716 exec->counts = NULL;
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00002717 exec->errCounts = NULL;
2718 }
Daniel Veillard4255d502002-04-16 15:50:10 +00002719 exec->inputStackMax = 0;
2720 exec->inputStackNr = 0;
2721 exec->inputStack = NULL;
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00002722 exec->errStateNo = -1;
2723 exec->errString = NULL;
Daniel Veillard94cc1032005-09-15 13:09:00 +00002724 exec->nbPush = 0;
Daniel Veillard4255d502002-04-16 15:50:10 +00002725 return(exec);
2726}
2727
2728/**
2729 * xmlRegFreeExecCtxt:
2730 * @exec: a regular expression evaulation context
2731 *
2732 * Free the structures associated to a regular expression evaulation context.
2733 */
2734void
2735xmlRegFreeExecCtxt(xmlRegExecCtxtPtr exec) {
2736 if (exec == NULL)
2737 return;
2738
2739 if (exec->rollbacks != NULL) {
2740 if (exec->counts != NULL) {
2741 int i;
2742
2743 for (i = 0;i < exec->maxRollbacks;i++)
2744 if (exec->rollbacks[i].counts != NULL)
2745 xmlFree(exec->rollbacks[i].counts);
2746 }
2747 xmlFree(exec->rollbacks);
2748 }
2749 if (exec->counts != NULL)
2750 xmlFree(exec->counts);
2751 if (exec->inputStack != NULL) {
2752 int i;
2753
Daniel Veillard32370232002-10-16 14:08:14 +00002754 for (i = 0;i < exec->inputStackNr;i++) {
2755 if (exec->inputStack[i].value != NULL)
2756 xmlFree(exec->inputStack[i].value);
2757 }
Daniel Veillard4255d502002-04-16 15:50:10 +00002758 xmlFree(exec->inputStack);
2759 }
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00002760 if (exec->errString != NULL)
2761 xmlFree(exec->errString);
Daniel Veillard4255d502002-04-16 15:50:10 +00002762 xmlFree(exec);
2763}
2764
2765static void
2766xmlFARegExecSaveInputString(xmlRegExecCtxtPtr exec, const xmlChar *value,
2767 void *data) {
2768#ifdef DEBUG_PUSH
2769 printf("saving value: %d:%s\n", exec->inputStackNr, value);
2770#endif
2771 if (exec->inputStackMax == 0) {
2772 exec->inputStackMax = 4;
2773 exec->inputStack = (xmlRegInputTokenPtr)
2774 xmlMalloc(exec->inputStackMax * sizeof(xmlRegInputToken));
2775 if (exec->inputStack == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00002776 xmlRegexpErrMemory(NULL, "pushing input string");
Daniel Veillard4255d502002-04-16 15:50:10 +00002777 exec->inputStackMax = 0;
2778 return;
2779 }
2780 } else if (exec->inputStackNr + 1 >= exec->inputStackMax) {
2781 xmlRegInputTokenPtr tmp;
2782
2783 exec->inputStackMax *= 2;
2784 tmp = (xmlRegInputTokenPtr) xmlRealloc(exec->inputStack,
2785 exec->inputStackMax * sizeof(xmlRegInputToken));
2786 if (tmp == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00002787 xmlRegexpErrMemory(NULL, "pushing input string");
Daniel Veillard4255d502002-04-16 15:50:10 +00002788 exec->inputStackMax /= 2;
2789 return;
2790 }
2791 exec->inputStack = tmp;
2792 }
2793 exec->inputStack[exec->inputStackNr].value = xmlStrdup(value);
2794 exec->inputStack[exec->inputStackNr].data = data;
2795 exec->inputStackNr++;
2796 exec->inputStack[exec->inputStackNr].value = NULL;
2797 exec->inputStack[exec->inputStackNr].data = NULL;
2798}
2799
Daniel Veillardc0826a72004-08-10 14:17:33 +00002800/**
2801 * xmlRegStrEqualWildcard:
2802 * @expStr: the string to be evaluated
2803 * @valStr: the validation string
2804 *
2805 * Checks if both strings are equal or have the same content. "*"
2806 * can be used as a wildcard in @valStr; "|" is used as a seperator of
2807 * substrings in both @expStr and @valStr.
2808 *
2809 * Returns 1 if the comparison is satisfied and the number of substrings
2810 * is equal, 0 otherwise.
2811 */
2812
2813static int
2814xmlRegStrEqualWildcard(const xmlChar *expStr, const xmlChar *valStr) {
2815 if (expStr == valStr) return(1);
2816 if (expStr == NULL) return(0);
2817 if (valStr == NULL) return(0);
2818 do {
2819 /*
2820 * Eval if we have a wildcard for the current item.
2821 */
2822 if (*expStr != *valStr) {
Daniel Veillard4f82c8a2005-08-09 21:40:08 +00002823 /* if one of them starts with a wildcard make valStr be it */
2824 if (*valStr == '*') {
2825 const xmlChar *tmp;
2826
2827 tmp = valStr;
2828 valStr = expStr;
2829 expStr = tmp;
2830 }
Daniel Veillardc0826a72004-08-10 14:17:33 +00002831 if ((*valStr != 0) && (*expStr != 0) && (*expStr++ == '*')) {
2832 do {
2833 if (*valStr == XML_REG_STRING_SEPARATOR)
2834 break;
Kasimier T. Buchcikc0e833f2005-04-19 15:02:20 +00002835 valStr++;
Daniel Veillardc0826a72004-08-10 14:17:33 +00002836 } while (*valStr != 0);
2837 continue;
2838 } else
2839 return(0);
2840 }
Kasimier T. Buchcikc0e833f2005-04-19 15:02:20 +00002841 expStr++;
2842 valStr++;
Daniel Veillardc0826a72004-08-10 14:17:33 +00002843 } while (*valStr != 0);
2844 if (*expStr != 0)
2845 return (0);
2846 else
2847 return (1);
2848}
Daniel Veillard4255d502002-04-16 15:50:10 +00002849
2850/**
Daniel Veillard23e73572002-09-19 19:56:43 +00002851 * xmlRegCompactPushString:
2852 * @exec: a regexp execution context
2853 * @comp: the precompiled exec with a compact table
2854 * @value: a string token input
2855 * @data: data associated to the token to reuse in callbacks
2856 *
2857 * Push one input token in the execution context
2858 *
2859 * Returns: 1 if the regexp reached a final state, 0 if non-final, and
2860 * a negative value in case of error.
2861 */
2862static int
2863xmlRegCompactPushString(xmlRegExecCtxtPtr exec,
2864 xmlRegexpPtr comp,
2865 const xmlChar *value,
2866 void *data) {
2867 int state = exec->index;
2868 int i, target;
2869
2870 if ((comp == NULL) || (comp->compact == NULL) || (comp->stringMap == NULL))
2871 return(-1);
2872
2873 if (value == NULL) {
2874 /*
2875 * are we at a final state ?
2876 */
2877 if (comp->compact[state * (comp->nbstrings + 1)] ==
2878 XML_REGEXP_FINAL_STATE)
2879 return(1);
2880 return(0);
2881 }
2882
2883#ifdef DEBUG_PUSH
2884 printf("value pushed: %s\n", value);
2885#endif
2886
2887 /*
William M. Brackddf71d62004-05-06 04:17:26 +00002888 * Examine all outside transitions from current state
Daniel Veillard23e73572002-09-19 19:56:43 +00002889 */
2890 for (i = 0;i < comp->nbstrings;i++) {
2891 target = comp->compact[state * (comp->nbstrings + 1) + i + 1];
2892 if ((target > 0) && (target <= comp->nbstates)) {
Daniel Veillardc0826a72004-08-10 14:17:33 +00002893 target--; /* to avoid 0 */
2894 if (xmlRegStrEqualWildcard(comp->stringMap[i], value)) {
2895 exec->index = target;
Daniel Veillard118aed72002-09-24 14:13:13 +00002896 if ((exec->callback != NULL) && (comp->transdata != NULL)) {
2897 exec->callback(exec->data, value,
2898 comp->transdata[state * comp->nbstrings + i], data);
2899 }
Daniel Veillard23e73572002-09-19 19:56:43 +00002900#ifdef DEBUG_PUSH
2901 printf("entering state %d\n", target);
2902#endif
2903 if (comp->compact[target * (comp->nbstrings + 1)] ==
Daniel Veillardcc026dc2005-01-12 13:21:17 +00002904 XML_REGEXP_SINK_STATE)
2905 goto error;
2906
2907 if (comp->compact[target * (comp->nbstrings + 1)] ==
Daniel Veillard23e73572002-09-19 19:56:43 +00002908 XML_REGEXP_FINAL_STATE)
2909 return(1);
2910 return(0);
2911 }
2912 }
2913 }
2914 /*
2915 * Failed to find an exit transition out from current state for the
2916 * current token
2917 */
2918#ifdef DEBUG_PUSH
2919 printf("failed to find a transition for %s on state %d\n", value, state);
2920#endif
Daniel Veillardcc026dc2005-01-12 13:21:17 +00002921error:
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00002922 if (exec->errString != NULL)
2923 xmlFree(exec->errString);
2924 exec->errString = xmlStrdup(value);
2925 exec->errStateNo = state;
Daniel Veillard23e73572002-09-19 19:56:43 +00002926 exec->status = -1;
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00002927#ifdef DEBUG_ERR
2928 testerr(exec);
2929#endif
Daniel Veillard23e73572002-09-19 19:56:43 +00002930 return(-1);
2931}
2932
2933/**
Daniel Veillard6e65e152005-08-09 11:09:52 +00002934 * xmlRegExecPushStringInternal:
Daniel Veillardea7751d2002-12-20 00:16:24 +00002935 * @exec: a regexp execution context or NULL to indicate the end
Daniel Veillard4255d502002-04-16 15:50:10 +00002936 * @value: a string token input
2937 * @data: data associated to the token to reuse in callbacks
Daniel Veillard6e65e152005-08-09 11:09:52 +00002938 * @compound: value was assembled from 2 strings
Daniel Veillard4255d502002-04-16 15:50:10 +00002939 *
2940 * Push one input token in the execution context
2941 *
2942 * Returns: 1 if the regexp reached a final state, 0 if non-final, and
2943 * a negative value in case of error.
2944 */
Daniel Veillard6e65e152005-08-09 11:09:52 +00002945static int
2946xmlRegExecPushStringInternal(xmlRegExecCtxtPtr exec, const xmlChar *value,
2947 void *data, int compound) {
Daniel Veillard4255d502002-04-16 15:50:10 +00002948 xmlRegTransPtr trans;
2949 xmlRegAtomPtr atom;
2950 int ret;
2951 int final = 0;
Daniel Veillard90700152005-01-08 22:05:09 +00002952 int progress = 1;
Daniel Veillard4255d502002-04-16 15:50:10 +00002953
2954 if (exec == NULL)
2955 return(-1);
Daniel Veillard23e73572002-09-19 19:56:43 +00002956 if (exec->comp == NULL)
2957 return(-1);
Daniel Veillard4255d502002-04-16 15:50:10 +00002958 if (exec->status != 0)
2959 return(exec->status);
2960
Daniel Veillard23e73572002-09-19 19:56:43 +00002961 if (exec->comp->compact != NULL)
2962 return(xmlRegCompactPushString(exec, exec->comp, value, data));
2963
Daniel Veillard4255d502002-04-16 15:50:10 +00002964 if (value == NULL) {
2965 if (exec->state->type == XML_REGEXP_FINAL_STATE)
2966 return(1);
2967 final = 1;
2968 }
2969
2970#ifdef DEBUG_PUSH
2971 printf("value pushed: %s\n", value);
2972#endif
2973 /*
2974 * If we have an active rollback stack push the new value there
2975 * and get back to where we were left
2976 */
2977 if ((value != NULL) && (exec->inputStackNr > 0)) {
2978 xmlFARegExecSaveInputString(exec, value, data);
2979 value = exec->inputStack[exec->index].value;
2980 data = exec->inputStack[exec->index].data;
2981#ifdef DEBUG_PUSH
2982 printf("value loaded: %s\n", value);
2983#endif
2984 }
2985
2986 while ((exec->status == 0) &&
2987 ((value != NULL) ||
2988 ((final == 1) &&
2989 (exec->state->type != XML_REGEXP_FINAL_STATE)))) {
2990
2991 /*
2992 * End of input on non-terminal state, rollback, however we may
2993 * still have epsilon like transition for counted transitions
2994 * on counters, in that case don't break too early.
2995 */
Daniel Veillardb509f152002-04-17 16:28:10 +00002996 if ((value == NULL) && (exec->counts == NULL))
Daniel Veillard4255d502002-04-16 15:50:10 +00002997 goto rollback;
2998
2999 exec->transcount = 0;
3000 for (;exec->transno < exec->state->nbTrans;exec->transno++) {
3001 trans = &exec->state->trans[exec->transno];
3002 if (trans->to < 0)
3003 continue;
3004 atom = trans->atom;
3005 ret = 0;
Daniel Veillard441bc322002-04-20 17:38:48 +00003006 if (trans->count == REGEXP_ALL_LAX_COUNTER) {
3007 int i;
3008 int count;
3009 xmlRegTransPtr t;
3010 xmlRegCounterPtr counter;
3011
3012 ret = 0;
3013
3014#ifdef DEBUG_PUSH
3015 printf("testing all lax %d\n", trans->count);
3016#endif
3017 /*
3018 * Check all counted transitions from the current state
3019 */
3020 if ((value == NULL) && (final)) {
3021 ret = 1;
3022 } else if (value != NULL) {
3023 for (i = 0;i < exec->state->nbTrans;i++) {
3024 t = &exec->state->trans[i];
3025 if ((t->counter < 0) || (t == trans))
3026 continue;
3027 counter = &exec->comp->counters[t->counter];
3028 count = exec->counts[t->counter];
3029 if ((count < counter->max) &&
3030 (t->atom != NULL) &&
3031 (xmlStrEqual(value, t->atom->valuep))) {
3032 ret = 0;
3033 break;
3034 }
3035 if ((count >= counter->min) &&
3036 (count < counter->max) &&
3037 (xmlStrEqual(value, t->atom->valuep))) {
3038 ret = 1;
3039 break;
3040 }
3041 }
3042 }
3043 } else if (trans->count == REGEXP_ALL_COUNTER) {
Daniel Veillard8a001f62002-04-20 07:24:11 +00003044 int i;
3045 int count;
3046 xmlRegTransPtr t;
3047 xmlRegCounterPtr counter;
3048
3049 ret = 1;
3050
3051#ifdef DEBUG_PUSH
3052 printf("testing all %d\n", trans->count);
3053#endif
3054 /*
3055 * Check all counted transitions from the current state
3056 */
3057 for (i = 0;i < exec->state->nbTrans;i++) {
3058 t = &exec->state->trans[i];
3059 if ((t->counter < 0) || (t == trans))
3060 continue;
3061 counter = &exec->comp->counters[t->counter];
3062 count = exec->counts[t->counter];
3063 if ((count < counter->min) || (count > counter->max)) {
3064 ret = 0;
3065 break;
3066 }
3067 }
3068 } else if (trans->count >= 0) {
Daniel Veillard4255d502002-04-16 15:50:10 +00003069 int count;
3070 xmlRegCounterPtr counter;
3071
3072 /*
3073 * A counted transition.
3074 */
3075
3076 count = exec->counts[trans->count];
3077 counter = &exec->comp->counters[trans->count];
3078#ifdef DEBUG_PUSH
3079 printf("testing count %d: val %d, min %d, max %d\n",
3080 trans->count, count, counter->min, counter->max);
3081#endif
3082 ret = ((count >= counter->min) && (count <= counter->max));
3083 } else if (atom == NULL) {
3084 fprintf(stderr, "epsilon transition left at runtime\n");
3085 exec->status = -2;
3086 break;
3087 } else if (value != NULL) {
Daniel Veillardc0826a72004-08-10 14:17:33 +00003088 ret = xmlRegStrEqualWildcard(atom->valuep, value);
Daniel Veillard6e65e152005-08-09 11:09:52 +00003089 if (atom->neg) {
Daniel Veillard9efc4762005-07-19 14:33:55 +00003090 ret = !ret;
Daniel Veillard6e65e152005-08-09 11:09:52 +00003091 if (!compound)
3092 ret = 0;
3093 }
Daniel Veillard441bc322002-04-20 17:38:48 +00003094 if ((ret == 1) && (trans->counter >= 0)) {
3095 xmlRegCounterPtr counter;
3096 int count;
3097
3098 count = exec->counts[trans->counter];
3099 counter = &exec->comp->counters[trans->counter];
3100 if (count >= counter->max)
3101 ret = 0;
3102 }
3103
Daniel Veillard4255d502002-04-16 15:50:10 +00003104 if ((ret == 1) && (atom->min > 0) && (atom->max > 0)) {
3105 xmlRegStatePtr to = exec->comp->states[trans->to];
3106
3107 /*
3108 * this is a multiple input sequence
3109 */
3110 if (exec->state->nbTrans > exec->transno + 1) {
3111 if (exec->inputStackNr <= 0) {
3112 xmlFARegExecSaveInputString(exec, value, data);
3113 }
3114 xmlFARegExecSave(exec);
3115 }
3116 exec->transcount = 1;
3117 do {
3118 /*
3119 * Try to progress as much as possible on the input
3120 */
3121 if (exec->transcount == atom->max) {
3122 break;
3123 }
3124 exec->index++;
3125 value = exec->inputStack[exec->index].value;
3126 data = exec->inputStack[exec->index].data;
3127#ifdef DEBUG_PUSH
3128 printf("value loaded: %s\n", value);
3129#endif
3130
3131 /*
3132 * End of input: stop here
3133 */
3134 if (value == NULL) {
3135 exec->index --;
3136 break;
3137 }
3138 if (exec->transcount >= atom->min) {
3139 int transno = exec->transno;
3140 xmlRegStatePtr state = exec->state;
3141
3142 /*
3143 * The transition is acceptable save it
3144 */
3145 exec->transno = -1; /* trick */
3146 exec->state = to;
3147 if (exec->inputStackNr <= 0) {
3148 xmlFARegExecSaveInputString(exec, value, data);
3149 }
3150 xmlFARegExecSave(exec);
3151 exec->transno = transno;
3152 exec->state = state;
3153 }
3154 ret = xmlStrEqual(value, atom->valuep);
3155 exec->transcount++;
3156 } while (ret == 1);
3157 if (exec->transcount < atom->min)
3158 ret = 0;
3159
3160 /*
3161 * If the last check failed but one transition was found
3162 * possible, rollback
3163 */
3164 if (ret < 0)
3165 ret = 0;
3166 if (ret == 0) {
3167 goto rollback;
3168 }
3169 }
3170 }
3171 if (ret == 1) {
William M. Brack98873952003-12-26 06:03:14 +00003172 if ((exec->callback != NULL) && (atom != NULL) &&
3173 (data != NULL)) {
Daniel Veillard4255d502002-04-16 15:50:10 +00003174 exec->callback(exec->data, atom->valuep,
3175 atom->data, data);
3176 }
3177 if (exec->state->nbTrans > exec->transno + 1) {
3178 if (exec->inputStackNr <= 0) {
3179 xmlFARegExecSaveInputString(exec, value, data);
3180 }
3181 xmlFARegExecSave(exec);
3182 }
3183 if (trans->counter >= 0) {
3184#ifdef DEBUG_PUSH
3185 printf("Increasing count %d\n", trans->counter);
3186#endif
3187 exec->counts[trans->counter]++;
3188 }
Daniel Veillard10752282005-08-08 13:05:13 +00003189 if ((trans->count >= 0) &&
3190 (trans->count < REGEXP_ALL_COUNTER)) {
3191#ifdef DEBUG_REGEXP_EXEC
3192 printf("resetting count %d on transition\n",
3193 trans->count);
3194#endif
3195 exec->counts[trans->count] = 0;
3196 }
Daniel Veillard4255d502002-04-16 15:50:10 +00003197#ifdef DEBUG_PUSH
3198 printf("entering state %d\n", trans->to);
3199#endif
Daniel Veillardcc026dc2005-01-12 13:21:17 +00003200 if ((exec->comp->states[trans->to] != NULL) &&
3201 (exec->comp->states[trans->to]->type ==
3202 XML_REGEXP_SINK_STATE)) {
3203 /*
3204 * entering a sink state, save the current state as error
3205 * state.
3206 */
3207 if (exec->errString != NULL)
3208 xmlFree(exec->errString);
3209 exec->errString = xmlStrdup(value);
3210 exec->errState = exec->state;
3211 memcpy(exec->errCounts, exec->counts,
3212 exec->comp->nbCounters * sizeof(int));
3213 }
Daniel Veillard4255d502002-04-16 15:50:10 +00003214 exec->state = exec->comp->states[trans->to];
3215 exec->transno = 0;
3216 if (trans->atom != NULL) {
3217 if (exec->inputStack != NULL) {
3218 exec->index++;
3219 if (exec->index < exec->inputStackNr) {
3220 value = exec->inputStack[exec->index].value;
3221 data = exec->inputStack[exec->index].data;
3222#ifdef DEBUG_PUSH
3223 printf("value loaded: %s\n", value);
3224#endif
3225 } else {
3226 value = NULL;
3227 data = NULL;
3228#ifdef DEBUG_PUSH
3229 printf("end of input\n");
3230#endif
3231 }
3232 } else {
3233 value = NULL;
3234 data = NULL;
3235#ifdef DEBUG_PUSH
3236 printf("end of input\n");
3237#endif
3238 }
3239 }
3240 goto progress;
3241 } else if (ret < 0) {
3242 exec->status = -4;
3243 break;
3244 }
3245 }
3246 if ((exec->transno != 0) || (exec->state->nbTrans == 0)) {
3247rollback:
Daniel Veillard90700152005-01-08 22:05:09 +00003248 /*
Daniel Veillardcc026dc2005-01-12 13:21:17 +00003249 * if we didn't yet rollback on the current input
3250 * store the current state as the error state.
Daniel Veillard90700152005-01-08 22:05:09 +00003251 */
Daniel Veillardcc026dc2005-01-12 13:21:17 +00003252 if ((progress) && (exec->state != NULL) &&
3253 (exec->state->type != XML_REGEXP_SINK_STATE)) {
Daniel Veillard90700152005-01-08 22:05:09 +00003254 progress = 0;
3255 if (exec->errString != NULL)
3256 xmlFree(exec->errString);
3257 exec->errString = xmlStrdup(value);
3258 exec->errState = exec->state;
3259 memcpy(exec->errCounts, exec->counts,
3260 exec->comp->nbCounters * sizeof(int));
3261 }
3262
Daniel Veillard4255d502002-04-16 15:50:10 +00003263 /*
3264 * Failed to find a way out
3265 */
3266 exec->determinist = 0;
3267 xmlFARegExecRollBack(exec);
3268 if (exec->status == 0) {
3269 value = exec->inputStack[exec->index].value;
3270 data = exec->inputStack[exec->index].data;
3271#ifdef DEBUG_PUSH
3272 printf("value loaded: %s\n", value);
3273#endif
3274 }
3275 }
Daniel Veillard90700152005-01-08 22:05:09 +00003276 continue;
Daniel Veillard4255d502002-04-16 15:50:10 +00003277progress:
Daniel Veillard90700152005-01-08 22:05:09 +00003278 progress = 1;
Daniel Veillard4255d502002-04-16 15:50:10 +00003279 continue;
3280 }
3281 if (exec->status == 0) {
3282 return(exec->state->type == XML_REGEXP_FINAL_STATE);
3283 }
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003284#ifdef DEBUG_ERR
Daniel Veillard90700152005-01-08 22:05:09 +00003285 if (exec->status < 0) {
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003286 testerr(exec);
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003287 }
Daniel Veillard90700152005-01-08 22:05:09 +00003288#endif
Daniel Veillard4255d502002-04-16 15:50:10 +00003289 return(exec->status);
3290}
3291
Daniel Veillard52b48c72003-04-13 19:53:42 +00003292/**
Daniel Veillard6e65e152005-08-09 11:09:52 +00003293 * xmlRegExecPushString:
3294 * @exec: a regexp execution context or NULL to indicate the end
3295 * @value: a string token input
3296 * @data: data associated to the token to reuse in callbacks
3297 *
3298 * Push one input token in the execution context
3299 *
3300 * Returns: 1 if the regexp reached a final state, 0 if non-final, and
3301 * a negative value in case of error.
3302 */
3303int
3304xmlRegExecPushString(xmlRegExecCtxtPtr exec, const xmlChar *value,
3305 void *data) {
3306 return(xmlRegExecPushStringInternal(exec, value, data, 0));
3307}
3308
3309/**
Daniel Veillard52b48c72003-04-13 19:53:42 +00003310 * xmlRegExecPushString2:
3311 * @exec: a regexp execution context or NULL to indicate the end
3312 * @value: the first string token input
3313 * @value2: the second string token input
3314 * @data: data associated to the token to reuse in callbacks
3315 *
3316 * Push one input token in the execution context
3317 *
3318 * Returns: 1 if the regexp reached a final state, 0 if non-final, and
3319 * a negative value in case of error.
3320 */
3321int
3322xmlRegExecPushString2(xmlRegExecCtxtPtr exec, const xmlChar *value,
3323 const xmlChar *value2, void *data) {
3324 xmlChar buf[150];
3325 int lenn, lenp, ret;
3326 xmlChar *str;
3327
3328 if (exec == NULL)
3329 return(-1);
3330 if (exec->comp == NULL)
3331 return(-1);
3332 if (exec->status != 0)
3333 return(exec->status);
3334
3335 if (value2 == NULL)
3336 return(xmlRegExecPushString(exec, value, data));
3337
3338 lenn = strlen((char *) value2);
3339 lenp = strlen((char *) value);
3340
3341 if (150 < lenn + lenp + 2) {
Daniel Veillard3c908dc2003-04-19 00:07:51 +00003342 str = (xmlChar *) xmlMallocAtomic(lenn + lenp + 2);
Daniel Veillard52b48c72003-04-13 19:53:42 +00003343 if (str == NULL) {
3344 exec->status = -1;
3345 return(-1);
3346 }
3347 } else {
3348 str = buf;
3349 }
3350 memcpy(&str[0], value, lenp);
Daniel Veillardc0826a72004-08-10 14:17:33 +00003351 str[lenp] = XML_REG_STRING_SEPARATOR;
Daniel Veillard52b48c72003-04-13 19:53:42 +00003352 memcpy(&str[lenp + 1], value2, lenn);
3353 str[lenn + lenp + 1] = 0;
3354
3355 if (exec->comp->compact != NULL)
3356 ret = xmlRegCompactPushString(exec, exec->comp, str, data);
3357 else
Daniel Veillard6e65e152005-08-09 11:09:52 +00003358 ret = xmlRegExecPushStringInternal(exec, str, data, 1);
Daniel Veillard52b48c72003-04-13 19:53:42 +00003359
3360 if (str != buf)
3361 xmlFree(buf);
3362 return(ret);
3363}
3364
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003365/**
Daniel Veillard77005e62005-07-19 16:26:18 +00003366 * xmlRegExecGetValues:
Daniel Veillardfc0b6f62005-01-09 17:48:02 +00003367 * @exec: a regexp execution context
3368 * @err: error extraction or normal one
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003369 * @nbval: pointer to the number of accepted values IN/OUT
Daniel Veillardcc026dc2005-01-12 13:21:17 +00003370 * @nbneg: return number of negative transitions
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003371 * @values: pointer to the array of acceptable values
Daniel Veillardfc0b6f62005-01-09 17:48:02 +00003372 * @terminal: return value if this was a terminal state
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003373 *
Daniel Veillardfc0b6f62005-01-09 17:48:02 +00003374 * Extract informations from the regexp execution, internal routine to
3375 * implement xmlRegExecNextValues() and xmlRegExecErrInfo()
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003376 *
3377 * Returns: 0 in case of success or -1 in case of error.
3378 */
Daniel Veillardfc0b6f62005-01-09 17:48:02 +00003379static int
3380xmlRegExecGetValues(xmlRegExecCtxtPtr exec, int err,
Daniel Veillardcc026dc2005-01-12 13:21:17 +00003381 int *nbval, int *nbneg,
3382 xmlChar **values, int *terminal) {
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003383 int maxval;
Daniel Veillardcc026dc2005-01-12 13:21:17 +00003384 int nb = 0;
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003385
Daniel Veillardcc026dc2005-01-12 13:21:17 +00003386 if ((exec == NULL) || (nbval == NULL) || (nbneg == NULL) ||
3387 (values == NULL) || (*nbval <= 0))
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003388 return(-1);
Daniel Veillardfc0b6f62005-01-09 17:48:02 +00003389
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003390 maxval = *nbval;
3391 *nbval = 0;
Daniel Veillardcc026dc2005-01-12 13:21:17 +00003392 *nbneg = 0;
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003393 if ((exec->comp != NULL) && (exec->comp->compact != NULL)) {
3394 xmlRegexpPtr comp;
3395 int target, i, state;
3396
3397 comp = exec->comp;
Daniel Veillardfc0b6f62005-01-09 17:48:02 +00003398
3399 if (err) {
3400 if (exec->errStateNo == -1) return(-1);
3401 state = exec->errStateNo;
3402 } else {
3403 state = exec->index;
3404 }
3405 if (terminal != NULL) {
3406 if (comp->compact[state * (comp->nbstrings + 1)] ==
3407 XML_REGEXP_FINAL_STATE)
3408 *terminal = 1;
3409 else
3410 *terminal = 0;
3411 }
Daniel Veillardcc026dc2005-01-12 13:21:17 +00003412 for (i = 0;(i < comp->nbstrings) && (nb < maxval);i++) {
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003413 target = comp->compact[state * (comp->nbstrings + 1) + i + 1];
Daniel Veillardcc026dc2005-01-12 13:21:17 +00003414 if ((target > 0) && (target <= comp->nbstates) &&
3415 (comp->compact[(target - 1) * (comp->nbstrings + 1)] !=
3416 XML_REGEXP_SINK_STATE)) {
3417 values[nb++] = comp->stringMap[i];
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003418 (*nbval)++;
3419 }
3420 }
Daniel Veillardcc026dc2005-01-12 13:21:17 +00003421 for (i = 0;(i < comp->nbstrings) && (nb < maxval);i++) {
3422 target = comp->compact[state * (comp->nbstrings + 1) + i + 1];
3423 if ((target > 0) && (target <= comp->nbstates) &&
3424 (comp->compact[(target - 1) * (comp->nbstrings + 1)] ==
3425 XML_REGEXP_SINK_STATE)) {
3426 values[nb++] = comp->stringMap[i];
3427 (*nbneg)++;
3428 }
3429 }
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003430 } else {
3431 int transno;
3432 xmlRegTransPtr trans;
3433 xmlRegAtomPtr atom;
Daniel Veillardfc0b6f62005-01-09 17:48:02 +00003434 xmlRegStatePtr state;
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003435
Daniel Veillardfc0b6f62005-01-09 17:48:02 +00003436 if (terminal != NULL) {
3437 if (exec->state->type == XML_REGEXP_FINAL_STATE)
3438 *terminal = 1;
3439 else
3440 *terminal = 0;
3441 }
3442
3443 if (err) {
3444 if (exec->errState == NULL) return(-1);
3445 state = exec->errState;
3446 } else {
3447 if (exec->state == NULL) return(-1);
3448 state = exec->state;
3449 }
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003450 for (transno = 0;
Daniel Veillardcc026dc2005-01-12 13:21:17 +00003451 (transno < state->nbTrans) && (nb < maxval);
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003452 transno++) {
Daniel Veillardfc0b6f62005-01-09 17:48:02 +00003453 trans = &state->trans[transno];
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003454 if (trans->to < 0)
3455 continue;
3456 atom = trans->atom;
3457 if ((atom == NULL) || (atom->valuep == NULL))
3458 continue;
3459 if (trans->count == REGEXP_ALL_LAX_COUNTER) {
Daniel Veillardcc026dc2005-01-12 13:21:17 +00003460 /* this should not be reached but ... */
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003461 TODO;
3462 } else if (trans->count == REGEXP_ALL_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->counter >= 0) {
3466 xmlRegCounterPtr counter;
3467 int count;
3468
Daniel Veillardfc0b6f62005-01-09 17:48:02 +00003469 if (err)
3470 count = exec->errCounts[trans->counter];
3471 else
3472 count = exec->counts[trans->counter];
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003473 counter = &exec->comp->counters[trans->counter];
3474 if (count < counter->max) {
Daniel Veillard77005e62005-07-19 16:26:18 +00003475 if (atom->neg)
3476 values[nb++] = (xmlChar *) atom->valuep2;
3477 else
3478 values[nb++] = (xmlChar *) atom->valuep;
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003479 (*nbval)++;
3480 }
3481 } else {
Daniel Veillardcc026dc2005-01-12 13:21:17 +00003482 if ((exec->comp->states[trans->to] != NULL) &&
3483 (exec->comp->states[trans->to]->type !=
3484 XML_REGEXP_SINK_STATE)) {
Daniel Veillard77005e62005-07-19 16:26:18 +00003485 if (atom->neg)
3486 values[nb++] = (xmlChar *) atom->valuep2;
3487 else
3488 values[nb++] = (xmlChar *) atom->valuep;
Daniel Veillardcc026dc2005-01-12 13:21:17 +00003489 (*nbval)++;
3490 }
3491 }
3492 }
3493 for (transno = 0;
3494 (transno < state->nbTrans) && (nb < maxval);
3495 transno++) {
3496 trans = &state->trans[transno];
3497 if (trans->to < 0)
3498 continue;
3499 atom = trans->atom;
3500 if ((atom == NULL) || (atom->valuep == NULL))
3501 continue;
3502 if (trans->count == REGEXP_ALL_LAX_COUNTER) {
3503 continue;
3504 } else if (trans->count == REGEXP_ALL_COUNTER) {
3505 continue;
3506 } else if (trans->counter >= 0) {
3507 continue;
3508 } else {
3509 if ((exec->comp->states[trans->to] != NULL) &&
3510 (exec->comp->states[trans->to]->type ==
3511 XML_REGEXP_SINK_STATE)) {
Daniel Veillard77005e62005-07-19 16:26:18 +00003512 if (atom->neg)
3513 values[nb++] = (xmlChar *) atom->valuep2;
3514 else
3515 values[nb++] = (xmlChar *) atom->valuep;
Daniel Veillardcc026dc2005-01-12 13:21:17 +00003516 (*nbneg)++;
3517 }
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003518 }
3519 }
3520 }
3521 return(0);
3522}
3523
Daniel Veillardfc0b6f62005-01-09 17:48:02 +00003524/**
3525 * xmlRegExecNextValues:
3526 * @exec: a regexp execution context
3527 * @nbval: pointer to the number of accepted values IN/OUT
Daniel Veillardcc026dc2005-01-12 13:21:17 +00003528 * @nbneg: return number of negative transitions
Daniel Veillardfc0b6f62005-01-09 17:48:02 +00003529 * @values: pointer to the array of acceptable values
3530 * @terminal: return value if this was a terminal state
3531 *
3532 * Extract informations from the regexp execution,
3533 * the parameter @values must point to an array of @nbval string pointers
3534 * on return nbval will contain the number of possible strings in that
3535 * state and the @values array will be updated with them. The string values
3536 * returned will be freed with the @exec context and don't need to be
3537 * deallocated.
3538 *
3539 * Returns: 0 in case of success or -1 in case of error.
3540 */
3541int
Daniel Veillardcc026dc2005-01-12 13:21:17 +00003542xmlRegExecNextValues(xmlRegExecCtxtPtr exec, int *nbval, int *nbneg,
3543 xmlChar **values, int *terminal) {
3544 return(xmlRegExecGetValues(exec, 0, nbval, nbneg, values, terminal));
Daniel Veillardfc0b6f62005-01-09 17:48:02 +00003545}
3546
3547/**
3548 * xmlRegExecErrInfo:
3549 * @exec: a regexp execution context generating an error
3550 * @string: return value for the error string
3551 * @nbval: pointer to the number of accepted values IN/OUT
Daniel Veillardcc026dc2005-01-12 13:21:17 +00003552 * @nbneg: return number of negative transitions
Daniel Veillardfc0b6f62005-01-09 17:48:02 +00003553 * @values: pointer to the array of acceptable values
3554 * @terminal: return value if this was a terminal state
3555 *
3556 * Extract error informations from the regexp execution, the parameter
3557 * @string will be updated with the value pushed and not accepted,
3558 * the parameter @values must point to an array of @nbval string pointers
3559 * on return nbval will contain the number of possible strings in that
3560 * state and the @values array will be updated with them. The string values
3561 * returned will be freed with the @exec context and don't need to be
3562 * deallocated.
3563 *
3564 * Returns: 0 in case of success or -1 in case of error.
3565 */
3566int
3567xmlRegExecErrInfo(xmlRegExecCtxtPtr exec, const xmlChar **string,
Daniel Veillardcc026dc2005-01-12 13:21:17 +00003568 int *nbval, int *nbneg, xmlChar **values, int *terminal) {
Daniel Veillardfc0b6f62005-01-09 17:48:02 +00003569 if (exec == NULL)
3570 return(-1);
3571 if (string != NULL) {
3572 if (exec->status != 0)
3573 *string = exec->errString;
3574 else
3575 *string = NULL;
3576 }
Daniel Veillardcc026dc2005-01-12 13:21:17 +00003577 return(xmlRegExecGetValues(exec, 1, nbval, nbneg, values, terminal));
Daniel Veillardfc0b6f62005-01-09 17:48:02 +00003578}
3579
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003580#ifdef DEBUG_ERR
3581static void testerr(xmlRegExecCtxtPtr exec) {
3582 const xmlChar *string;
Daniel Veillardcee2b3a2005-01-25 00:22:52 +00003583 xmlChar *values[5];
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003584 int nb = 5;
Daniel Veillardcc026dc2005-01-12 13:21:17 +00003585 int nbneg;
Daniel Veillardfc0b6f62005-01-09 17:48:02 +00003586 int terminal;
Daniel Veillardcc026dc2005-01-12 13:21:17 +00003587 xmlRegExecErrInfo(exec, &string, &nb, &nbneg, &values[0], &terminal);
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003588}
3589#endif
3590
Daniel Veillard4255d502002-04-16 15:50:10 +00003591#if 0
3592static int
3593xmlRegExecPushChar(xmlRegExecCtxtPtr exec, int UCS) {
3594 xmlRegTransPtr trans;
3595 xmlRegAtomPtr atom;
3596 int ret;
3597 int codepoint, len;
3598
3599 if (exec == NULL)
3600 return(-1);
3601 if (exec->status != 0)
3602 return(exec->status);
3603
3604 while ((exec->status == 0) &&
3605 ((exec->inputString[exec->index] != 0) ||
3606 (exec->state->type != XML_REGEXP_FINAL_STATE))) {
3607
3608 /*
3609 * End of input on non-terminal state, rollback, however we may
3610 * still have epsilon like transition for counted transitions
3611 * on counters, in that case don't break too early.
3612 */
3613 if ((exec->inputString[exec->index] == 0) && (exec->counts == NULL))
3614 goto rollback;
3615
3616 exec->transcount = 0;
3617 for (;exec->transno < exec->state->nbTrans;exec->transno++) {
3618 trans = &exec->state->trans[exec->transno];
3619 if (trans->to < 0)
3620 continue;
3621 atom = trans->atom;
3622 ret = 0;
3623 if (trans->count >= 0) {
3624 int count;
3625 xmlRegCounterPtr counter;
3626
3627 /*
3628 * A counted transition.
3629 */
3630
3631 count = exec->counts[trans->count];
3632 counter = &exec->comp->counters[trans->count];
3633#ifdef DEBUG_REGEXP_EXEC
3634 printf("testing count %d: val %d, min %d, max %d\n",
3635 trans->count, count, counter->min, counter->max);
3636#endif
3637 ret = ((count >= counter->min) && (count <= counter->max));
3638 } else if (atom == NULL) {
3639 fprintf(stderr, "epsilon transition left at runtime\n");
3640 exec->status = -2;
3641 break;
3642 } else if (exec->inputString[exec->index] != 0) {
3643 codepoint = CUR_SCHAR(&(exec->inputString[exec->index]), len);
3644 ret = xmlRegCheckCharacter(atom, codepoint);
3645 if ((ret == 1) && (atom->min > 0) && (atom->max > 0)) {
3646 xmlRegStatePtr to = exec->comp->states[trans->to];
3647
3648 /*
3649 * this is a multiple input sequence
3650 */
3651 if (exec->state->nbTrans > exec->transno + 1) {
3652 xmlFARegExecSave(exec);
3653 }
3654 exec->transcount = 1;
3655 do {
3656 /*
3657 * Try to progress as much as possible on the input
3658 */
3659 if (exec->transcount == atom->max) {
3660 break;
3661 }
3662 exec->index += len;
3663 /*
3664 * End of input: stop here
3665 */
3666 if (exec->inputString[exec->index] == 0) {
3667 exec->index -= len;
3668 break;
3669 }
3670 if (exec->transcount >= atom->min) {
3671 int transno = exec->transno;
3672 xmlRegStatePtr state = exec->state;
3673
3674 /*
3675 * The transition is acceptable save it
3676 */
3677 exec->transno = -1; /* trick */
3678 exec->state = to;
3679 xmlFARegExecSave(exec);
3680 exec->transno = transno;
3681 exec->state = state;
3682 }
3683 codepoint = CUR_SCHAR(&(exec->inputString[exec->index]),
3684 len);
3685 ret = xmlRegCheckCharacter(atom, codepoint);
3686 exec->transcount++;
3687 } while (ret == 1);
3688 if (exec->transcount < atom->min)
3689 ret = 0;
3690
3691 /*
3692 * If the last check failed but one transition was found
3693 * possible, rollback
3694 */
3695 if (ret < 0)
3696 ret = 0;
3697 if (ret == 0) {
3698 goto rollback;
3699 }
3700 }
3701 }
3702 if (ret == 1) {
3703 if (exec->state->nbTrans > exec->transno + 1) {
3704 xmlFARegExecSave(exec);
3705 }
3706 if (trans->counter >= 0) {
3707#ifdef DEBUG_REGEXP_EXEC
3708 printf("Increasing count %d\n", trans->counter);
3709#endif
3710 exec->counts[trans->counter]++;
3711 }
3712#ifdef DEBUG_REGEXP_EXEC
3713 printf("entering state %d\n", trans->to);
3714#endif
3715 exec->state = exec->comp->states[trans->to];
3716 exec->transno = 0;
3717 if (trans->atom != NULL) {
3718 exec->index += len;
3719 }
3720 goto progress;
3721 } else if (ret < 0) {
3722 exec->status = -4;
3723 break;
3724 }
3725 }
3726 if ((exec->transno != 0) || (exec->state->nbTrans == 0)) {
3727rollback:
3728 /*
3729 * Failed to find a way out
3730 */
3731 exec->determinist = 0;
3732 xmlFARegExecRollBack(exec);
3733 }
3734progress:
3735 continue;
3736 }
3737}
3738#endif
3739/************************************************************************
3740 * *
William M. Brackddf71d62004-05-06 04:17:26 +00003741 * Parser for the Schemas Datatype Regular Expressions *
Daniel Veillard4255d502002-04-16 15:50:10 +00003742 * http://www.w3.org/TR/2001/REC-xmlschema-2-20010502/#regexs *
3743 * *
3744 ************************************************************************/
3745
3746/**
3747 * xmlFAIsChar:
Daniel Veillard441bc322002-04-20 17:38:48 +00003748 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00003749 *
3750 * [10] Char ::= [^.\?*+()|#x5B#x5D]
3751 */
3752static int
3753xmlFAIsChar(xmlRegParserCtxtPtr ctxt) {
3754 int cur;
3755 int len;
3756
3757 cur = CUR_SCHAR(ctxt->cur, len);
3758 if ((cur == '.') || (cur == '\\') || (cur == '?') ||
3759 (cur == '*') || (cur == '+') || (cur == '(') ||
3760 (cur == ')') || (cur == '|') || (cur == 0x5B) ||
3761 (cur == 0x5D) || (cur == 0))
3762 return(-1);
3763 return(cur);
3764}
3765
3766/**
3767 * xmlFAParseCharProp:
Daniel Veillard441bc322002-04-20 17:38:48 +00003768 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00003769 *
3770 * [27] charProp ::= IsCategory | IsBlock
3771 * [28] IsCategory ::= Letters | Marks | Numbers | Punctuation |
3772 * Separators | Symbols | Others
3773 * [29] Letters ::= 'L' [ultmo]?
3774 * [30] Marks ::= 'M' [nce]?
3775 * [31] Numbers ::= 'N' [dlo]?
3776 * [32] Punctuation ::= 'P' [cdseifo]?
3777 * [33] Separators ::= 'Z' [slp]?
3778 * [34] Symbols ::= 'S' [mcko]?
3779 * [35] Others ::= 'C' [cfon]?
3780 * [36] IsBlock ::= 'Is' [a-zA-Z0-9#x2D]+
3781 */
3782static void
3783xmlFAParseCharProp(xmlRegParserCtxtPtr ctxt) {
3784 int cur;
William M. Brack779af002003-08-01 15:55:39 +00003785 xmlRegAtomType type = (xmlRegAtomType) 0;
Daniel Veillard4255d502002-04-16 15:50:10 +00003786 xmlChar *blockName = NULL;
3787
3788 cur = CUR;
3789 if (cur == 'L') {
3790 NEXT;
3791 cur = CUR;
3792 if (cur == 'u') {
3793 NEXT;
3794 type = XML_REGEXP_LETTER_UPPERCASE;
3795 } else if (cur == 'l') {
3796 NEXT;
3797 type = XML_REGEXP_LETTER_LOWERCASE;
3798 } else if (cur == 't') {
3799 NEXT;
3800 type = XML_REGEXP_LETTER_TITLECASE;
3801 } else if (cur == 'm') {
3802 NEXT;
3803 type = XML_REGEXP_LETTER_MODIFIER;
3804 } else if (cur == 'o') {
3805 NEXT;
3806 type = XML_REGEXP_LETTER_OTHERS;
3807 } else {
3808 type = XML_REGEXP_LETTER;
3809 }
3810 } else if (cur == 'M') {
3811 NEXT;
3812 cur = CUR;
3813 if (cur == 'n') {
3814 NEXT;
3815 /* nonspacing */
3816 type = XML_REGEXP_MARK_NONSPACING;
3817 } else if (cur == 'c') {
3818 NEXT;
3819 /* spacing combining */
3820 type = XML_REGEXP_MARK_SPACECOMBINING;
3821 } else if (cur == 'e') {
3822 NEXT;
3823 /* enclosing */
3824 type = XML_REGEXP_MARK_ENCLOSING;
3825 } else {
3826 /* all marks */
3827 type = XML_REGEXP_MARK;
3828 }
3829 } else if (cur == 'N') {
3830 NEXT;
3831 cur = CUR;
3832 if (cur == 'd') {
3833 NEXT;
3834 /* digital */
3835 type = XML_REGEXP_NUMBER_DECIMAL;
3836 } else if (cur == 'l') {
3837 NEXT;
3838 /* letter */
3839 type = XML_REGEXP_NUMBER_LETTER;
3840 } else if (cur == 'o') {
3841 NEXT;
3842 /* other */
3843 type = XML_REGEXP_NUMBER_OTHERS;
3844 } else {
3845 /* all numbers */
3846 type = XML_REGEXP_NUMBER;
3847 }
3848 } else if (cur == 'P') {
3849 NEXT;
3850 cur = CUR;
3851 if (cur == 'c') {
3852 NEXT;
3853 /* connector */
3854 type = XML_REGEXP_PUNCT_CONNECTOR;
3855 } else if (cur == 'd') {
3856 NEXT;
3857 /* dash */
3858 type = XML_REGEXP_PUNCT_DASH;
3859 } else if (cur == 's') {
3860 NEXT;
3861 /* open */
3862 type = XML_REGEXP_PUNCT_OPEN;
3863 } else if (cur == 'e') {
3864 NEXT;
3865 /* close */
3866 type = XML_REGEXP_PUNCT_CLOSE;
3867 } else if (cur == 'i') {
3868 NEXT;
3869 /* initial quote */
3870 type = XML_REGEXP_PUNCT_INITQUOTE;
3871 } else if (cur == 'f') {
3872 NEXT;
3873 /* final quote */
3874 type = XML_REGEXP_PUNCT_FINQUOTE;
3875 } else if (cur == 'o') {
3876 NEXT;
3877 /* other */
3878 type = XML_REGEXP_PUNCT_OTHERS;
3879 } else {
3880 /* all punctuation */
3881 type = XML_REGEXP_PUNCT;
3882 }
3883 } else if (cur == 'Z') {
3884 NEXT;
3885 cur = CUR;
3886 if (cur == 's') {
3887 NEXT;
3888 /* space */
3889 type = XML_REGEXP_SEPAR_SPACE;
3890 } else if (cur == 'l') {
3891 NEXT;
3892 /* line */
3893 type = XML_REGEXP_SEPAR_LINE;
3894 } else if (cur == 'p') {
3895 NEXT;
3896 /* paragraph */
3897 type = XML_REGEXP_SEPAR_PARA;
3898 } else {
3899 /* all separators */
3900 type = XML_REGEXP_SEPAR;
3901 }
3902 } else if (cur == 'S') {
3903 NEXT;
3904 cur = CUR;
3905 if (cur == 'm') {
3906 NEXT;
3907 type = XML_REGEXP_SYMBOL_MATH;
3908 /* math */
3909 } else if (cur == 'c') {
3910 NEXT;
3911 type = XML_REGEXP_SYMBOL_CURRENCY;
3912 /* currency */
3913 } else if (cur == 'k') {
3914 NEXT;
3915 type = XML_REGEXP_SYMBOL_MODIFIER;
3916 /* modifiers */
3917 } else if (cur == 'o') {
3918 NEXT;
3919 type = XML_REGEXP_SYMBOL_OTHERS;
3920 /* other */
3921 } else {
3922 /* all symbols */
3923 type = XML_REGEXP_SYMBOL;
3924 }
3925 } else if (cur == 'C') {
3926 NEXT;
3927 cur = CUR;
3928 if (cur == 'c') {
3929 NEXT;
3930 /* control */
3931 type = XML_REGEXP_OTHER_CONTROL;
3932 } else if (cur == 'f') {
3933 NEXT;
3934 /* format */
3935 type = XML_REGEXP_OTHER_FORMAT;
3936 } else if (cur == 'o') {
3937 NEXT;
3938 /* private use */
3939 type = XML_REGEXP_OTHER_PRIVATE;
3940 } else if (cur == 'n') {
3941 NEXT;
3942 /* not assigned */
3943 type = XML_REGEXP_OTHER_NA;
3944 } else {
3945 /* all others */
3946 type = XML_REGEXP_OTHER;
3947 }
3948 } else if (cur == 'I') {
3949 const xmlChar *start;
3950 NEXT;
3951 cur = CUR;
3952 if (cur != 's') {
3953 ERROR("IsXXXX expected");
3954 return;
3955 }
3956 NEXT;
3957 start = ctxt->cur;
3958 cur = CUR;
3959 if (((cur >= 'a') && (cur <= 'z')) ||
3960 ((cur >= 'A') && (cur <= 'Z')) ||
3961 ((cur >= '0') && (cur <= '9')) ||
3962 (cur == 0x2D)) {
3963 NEXT;
3964 cur = CUR;
3965 while (((cur >= 'a') && (cur <= 'z')) ||
3966 ((cur >= 'A') && (cur <= 'Z')) ||
3967 ((cur >= '0') && (cur <= '9')) ||
3968 (cur == 0x2D)) {
3969 NEXT;
3970 cur = CUR;
3971 }
3972 }
3973 type = XML_REGEXP_BLOCK_NAME;
3974 blockName = xmlStrndup(start, ctxt->cur - start);
3975 } else {
3976 ERROR("Unknown char property");
3977 return;
3978 }
3979 if (ctxt->atom == NULL) {
3980 ctxt->atom = xmlRegNewAtom(ctxt, type);
3981 if (ctxt->atom != NULL)
3982 ctxt->atom->valuep = blockName;
3983 } else if (ctxt->atom->type == XML_REGEXP_RANGES) {
3984 xmlRegAtomAddRange(ctxt, ctxt->atom, ctxt->neg,
3985 type, 0, 0, blockName);
3986 }
3987}
3988
3989/**
3990 * xmlFAParseCharClassEsc:
Daniel Veillard441bc322002-04-20 17:38:48 +00003991 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00003992 *
3993 * [23] charClassEsc ::= ( SingleCharEsc | MultiCharEsc | catEsc | complEsc )
3994 * [24] SingleCharEsc ::= '\' [nrt\|.?*+(){}#x2D#x5B#x5D#x5E]
3995 * [25] catEsc ::= '\p{' charProp '}'
3996 * [26] complEsc ::= '\P{' charProp '}'
3997 * [37] MultiCharEsc ::= '.' | ('\' [sSiIcCdDwW])
3998 */
3999static void
4000xmlFAParseCharClassEsc(xmlRegParserCtxtPtr ctxt) {
4001 int cur;
4002
4003 if (CUR == '.') {
4004 if (ctxt->atom == NULL) {
4005 ctxt->atom = xmlRegNewAtom(ctxt, XML_REGEXP_ANYCHAR);
4006 } else if (ctxt->atom->type == XML_REGEXP_RANGES) {
4007 xmlRegAtomAddRange(ctxt, ctxt->atom, ctxt->neg,
4008 XML_REGEXP_ANYCHAR, 0, 0, NULL);
4009 }
4010 NEXT;
4011 return;
4012 }
4013 if (CUR != '\\') {
4014 ERROR("Escaped sequence: expecting \\");
4015 return;
4016 }
4017 NEXT;
4018 cur = CUR;
4019 if (cur == 'p') {
4020 NEXT;
4021 if (CUR != '{') {
4022 ERROR("Expecting '{'");
4023 return;
4024 }
4025 NEXT;
4026 xmlFAParseCharProp(ctxt);
4027 if (CUR != '}') {
4028 ERROR("Expecting '}'");
4029 return;
4030 }
4031 NEXT;
4032 } else if (cur == 'P') {
4033 NEXT;
4034 if (CUR != '{') {
4035 ERROR("Expecting '{'");
4036 return;
4037 }
4038 NEXT;
4039 xmlFAParseCharProp(ctxt);
4040 ctxt->atom->neg = 1;
4041 if (CUR != '}') {
4042 ERROR("Expecting '}'");
4043 return;
4044 }
4045 NEXT;
4046 } else if ((cur == 'n') || (cur == 'r') || (cur == 't') || (cur == '\\') ||
4047 (cur == '|') || (cur == '.') || (cur == '?') || (cur == '*') ||
4048 (cur == '+') || (cur == '(') || (cur == ')') || (cur == '{') ||
4049 (cur == '}') || (cur == 0x2D) || (cur == 0x5B) || (cur == 0x5D) ||
4050 (cur == 0x5E)) {
4051 if (ctxt->atom == NULL) {
4052 ctxt->atom = xmlRegNewAtom(ctxt, XML_REGEXP_CHARVAL);
Daniel Veillard99c394d2005-07-14 12:58:49 +00004053 if (ctxt->atom != NULL) {
4054 switch (cur) {
4055 case 'n':
4056 ctxt->atom->codepoint = '\n';
4057 break;
4058 case 'r':
4059 ctxt->atom->codepoint = '\r';
4060 break;
4061 case 't':
4062 ctxt->atom->codepoint = '\t';
4063 break;
4064 default:
4065 ctxt->atom->codepoint = cur;
4066 }
4067 }
Daniel Veillard4255d502002-04-16 15:50:10 +00004068 } else if (ctxt->atom->type == XML_REGEXP_RANGES) {
4069 xmlRegAtomAddRange(ctxt, ctxt->atom, ctxt->neg,
4070 XML_REGEXP_CHARVAL, cur, cur, NULL);
4071 }
4072 NEXT;
4073 } else if ((cur == 's') || (cur == 'S') || (cur == 'i') || (cur == 'I') ||
4074 (cur == 'c') || (cur == 'C') || (cur == 'd') || (cur == 'D') ||
4075 (cur == 'w') || (cur == 'W')) {
Daniel Veillardb509f152002-04-17 16:28:10 +00004076 xmlRegAtomType type = XML_REGEXP_ANYSPACE;
Daniel Veillard4255d502002-04-16 15:50:10 +00004077
4078 switch (cur) {
4079 case 's':
4080 type = XML_REGEXP_ANYSPACE;
4081 break;
4082 case 'S':
4083 type = XML_REGEXP_NOTSPACE;
4084 break;
4085 case 'i':
4086 type = XML_REGEXP_INITNAME;
4087 break;
4088 case 'I':
4089 type = XML_REGEXP_NOTINITNAME;
4090 break;
4091 case 'c':
4092 type = XML_REGEXP_NAMECHAR;
4093 break;
4094 case 'C':
4095 type = XML_REGEXP_NOTNAMECHAR;
4096 break;
4097 case 'd':
4098 type = XML_REGEXP_DECIMAL;
4099 break;
4100 case 'D':
4101 type = XML_REGEXP_NOTDECIMAL;
4102 break;
4103 case 'w':
4104 type = XML_REGEXP_REALCHAR;
4105 break;
4106 case 'W':
4107 type = XML_REGEXP_NOTREALCHAR;
4108 break;
4109 }
4110 NEXT;
4111 if (ctxt->atom == NULL) {
4112 ctxt->atom = xmlRegNewAtom(ctxt, type);
4113 } else if (ctxt->atom->type == XML_REGEXP_RANGES) {
4114 xmlRegAtomAddRange(ctxt, ctxt->atom, ctxt->neg,
4115 type, 0, 0, NULL);
4116 }
4117 }
4118}
4119
4120/**
4121 * xmlFAParseCharRef:
Daniel Veillard441bc322002-04-20 17:38:48 +00004122 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00004123 *
4124 * [19] XmlCharRef ::= ( '&#' [0-9]+ ';' ) | (' &#x' [0-9a-fA-F]+ ';' )
4125 */
4126static int
4127xmlFAParseCharRef(xmlRegParserCtxtPtr ctxt) {
4128 int ret = 0, cur;
4129
4130 if ((CUR != '&') || (NXT(1) != '#'))
4131 return(-1);
4132 NEXT;
4133 NEXT;
4134 cur = CUR;
4135 if (cur == 'x') {
4136 NEXT;
4137 cur = CUR;
4138 if (((cur >= '0') && (cur <= '9')) ||
4139 ((cur >= 'a') && (cur <= 'f')) ||
4140 ((cur >= 'A') && (cur <= 'F'))) {
4141 while (((cur >= '0') && (cur <= '9')) ||
4142 ((cur >= 'A') && (cur <= 'F'))) {
4143 if ((cur >= '0') && (cur <= '9'))
4144 ret = ret * 16 + cur - '0';
4145 else if ((cur >= 'a') && (cur <= 'f'))
4146 ret = ret * 16 + 10 + (cur - 'a');
4147 else
4148 ret = ret * 16 + 10 + (cur - 'A');
4149 NEXT;
4150 cur = CUR;
4151 }
4152 } else {
4153 ERROR("Char ref: expecting [0-9A-F]");
4154 return(-1);
4155 }
4156 } else {
4157 if ((cur >= '0') && (cur <= '9')) {
4158 while ((cur >= '0') && (cur <= '9')) {
4159 ret = ret * 10 + cur - '0';
4160 NEXT;
4161 cur = CUR;
4162 }
4163 } else {
4164 ERROR("Char ref: expecting [0-9]");
4165 return(-1);
4166 }
4167 }
4168 if (cur != ';') {
4169 ERROR("Char ref: expecting ';'");
4170 return(-1);
4171 } else {
4172 NEXT;
4173 }
4174 return(ret);
4175}
4176
4177/**
4178 * xmlFAParseCharRange:
Daniel Veillard441bc322002-04-20 17:38:48 +00004179 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00004180 *
4181 * [17] charRange ::= seRange | XmlCharRef | XmlCharIncDash
4182 * [18] seRange ::= charOrEsc '-' charOrEsc
4183 * [20] charOrEsc ::= XmlChar | SingleCharEsc
4184 * [21] XmlChar ::= [^\#x2D#x5B#x5D]
4185 * [22] XmlCharIncDash ::= [^\#x5B#x5D]
4186 */
4187static void
4188xmlFAParseCharRange(xmlRegParserCtxtPtr ctxt) {
William M. Brackdc99df92003-12-27 01:54:25 +00004189 int cur, len;
Daniel Veillard4255d502002-04-16 15:50:10 +00004190 int start = -1;
4191 int end = -1;
4192
4193 if ((CUR == '&') && (NXT(1) == '#')) {
4194 end = start = xmlFAParseCharRef(ctxt);
4195 xmlRegAtomAddRange(ctxt, ctxt->atom, ctxt->neg,
4196 XML_REGEXP_CHARVAL, start, end, NULL);
4197 return;
4198 }
4199 cur = CUR;
4200 if (cur == '\\') {
4201 NEXT;
4202 cur = CUR;
4203 switch (cur) {
4204 case 'n': start = 0xA; break;
4205 case 'r': start = 0xD; break;
4206 case 't': start = 0x9; break;
4207 case '\\': case '|': case '.': case '-': case '^': case '?':
4208 case '*': case '+': case '{': case '}': case '(': case ')':
4209 case '[': case ']':
4210 start = cur; break;
4211 default:
4212 ERROR("Invalid escape value");
4213 return;
4214 }
4215 end = start;
William M. Brackdc99df92003-12-27 01:54:25 +00004216 len = 1;
Daniel Veillard4255d502002-04-16 15:50:10 +00004217 } else if ((cur != 0x5B) && (cur != 0x5D)) {
William M. Brackdc99df92003-12-27 01:54:25 +00004218 end = start = CUR_SCHAR(ctxt->cur, len);
Daniel Veillard4255d502002-04-16 15:50:10 +00004219 } else {
4220 ERROR("Expecting a char range");
4221 return;
4222 }
William M. Brackdc99df92003-12-27 01:54:25 +00004223 NEXTL(len);
Daniel Veillard4255d502002-04-16 15:50:10 +00004224 if (start == '-') {
4225 return;
4226 }
4227 cur = CUR;
William M. Brack10f1ef42004-03-20 14:51:25 +00004228 if ((cur != '-') || (NXT(1) == ']')) {
Daniel Veillard4255d502002-04-16 15:50:10 +00004229 xmlRegAtomAddRange(ctxt, ctxt->atom, ctxt->neg,
4230 XML_REGEXP_CHARVAL, start, end, NULL);
4231 return;
4232 }
4233 NEXT;
4234 cur = CUR;
4235 if (cur == '\\') {
4236 NEXT;
4237 cur = CUR;
4238 switch (cur) {
4239 case 'n': end = 0xA; break;
4240 case 'r': end = 0xD; break;
4241 case 't': end = 0x9; break;
4242 case '\\': case '|': case '.': case '-': case '^': case '?':
4243 case '*': case '+': case '{': case '}': case '(': case ')':
4244 case '[': case ']':
4245 end = cur; break;
4246 default:
4247 ERROR("Invalid escape value");
4248 return;
4249 }
William M. Brackdc99df92003-12-27 01:54:25 +00004250 len = 1;
Daniel Veillard4255d502002-04-16 15:50:10 +00004251 } else if ((cur != 0x5B) && (cur != 0x5D)) {
William M. Brackdc99df92003-12-27 01:54:25 +00004252 end = CUR_SCHAR(ctxt->cur, len);
Daniel Veillard4255d502002-04-16 15:50:10 +00004253 } else {
4254 ERROR("Expecting the end of a char range");
4255 return;
4256 }
William M. Brackdc99df92003-12-27 01:54:25 +00004257 NEXTL(len);
Daniel Veillard4255d502002-04-16 15:50:10 +00004258 /* TODO check that the values are acceptable character ranges for XML */
4259 if (end < start) {
4260 ERROR("End of range is before start of range");
4261 } else {
4262 xmlRegAtomAddRange(ctxt, ctxt->atom, ctxt->neg,
4263 XML_REGEXP_CHARVAL, start, end, NULL);
4264 }
4265 return;
4266}
4267
4268/**
4269 * xmlFAParsePosCharGroup:
Daniel Veillard441bc322002-04-20 17:38:48 +00004270 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00004271 *
4272 * [14] posCharGroup ::= ( charRange | charClassEsc )+
4273 */
4274static void
4275xmlFAParsePosCharGroup(xmlRegParserCtxtPtr ctxt) {
4276 do {
4277 if ((CUR == '\\') || (CUR == '.')) {
4278 xmlFAParseCharClassEsc(ctxt);
4279 } else {
4280 xmlFAParseCharRange(ctxt);
4281 }
4282 } while ((CUR != ']') && (CUR != '^') && (CUR != '-') &&
4283 (ctxt->error == 0));
4284}
4285
4286/**
4287 * xmlFAParseCharGroup:
Daniel Veillard441bc322002-04-20 17:38:48 +00004288 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00004289 *
4290 * [13] charGroup ::= posCharGroup | negCharGroup | charClassSub
4291 * [15] negCharGroup ::= '^' posCharGroup
4292 * [16] charClassSub ::= ( posCharGroup | negCharGroup ) '-' charClassExpr
4293 * [12] charClassExpr ::= '[' charGroup ']'
4294 */
4295static void
4296xmlFAParseCharGroup(xmlRegParserCtxtPtr ctxt) {
4297 int n = ctxt->neg;
4298 while ((CUR != ']') && (ctxt->error == 0)) {
4299 if (CUR == '^') {
4300 int neg = ctxt->neg;
4301
4302 NEXT;
4303 ctxt->neg = !ctxt->neg;
4304 xmlFAParsePosCharGroup(ctxt);
4305 ctxt->neg = neg;
William M. Brack10f1ef42004-03-20 14:51:25 +00004306 } else if ((CUR == '-') && (NXT(1) == '[')) {
Daniel Veillardf8b9de32003-11-24 14:27:26 +00004307 int neg = ctxt->neg;
Daniel Veillardf8b9de32003-11-24 14:27:26 +00004308 ctxt->neg = 2;
William M. Brack10f1ef42004-03-20 14:51:25 +00004309 NEXT; /* eat the '-' */
4310 NEXT; /* eat the '[' */
Daniel Veillard4255d502002-04-16 15:50:10 +00004311 xmlFAParseCharGroup(ctxt);
4312 if (CUR == ']') {
4313 NEXT;
4314 } else {
4315 ERROR("charClassExpr: ']' expected");
4316 break;
4317 }
Daniel Veillardf8b9de32003-11-24 14:27:26 +00004318 ctxt->neg = neg;
Daniel Veillard4255d502002-04-16 15:50:10 +00004319 break;
4320 } else if (CUR != ']') {
4321 xmlFAParsePosCharGroup(ctxt);
4322 }
4323 }
4324 ctxt->neg = n;
4325}
4326
4327/**
4328 * xmlFAParseCharClass:
Daniel Veillard441bc322002-04-20 17:38:48 +00004329 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00004330 *
4331 * [11] charClass ::= charClassEsc | charClassExpr
4332 * [12] charClassExpr ::= '[' charGroup ']'
4333 */
4334static void
4335xmlFAParseCharClass(xmlRegParserCtxtPtr ctxt) {
4336 if (CUR == '[') {
4337 NEXT;
4338 ctxt->atom = xmlRegNewAtom(ctxt, XML_REGEXP_RANGES);
4339 if (ctxt->atom == NULL)
4340 return;
4341 xmlFAParseCharGroup(ctxt);
4342 if (CUR == ']') {
4343 NEXT;
4344 } else {
4345 ERROR("xmlFAParseCharClass: ']' expected");
4346 }
4347 } else {
4348 xmlFAParseCharClassEsc(ctxt);
4349 }
4350}
4351
4352/**
4353 * xmlFAParseQuantExact:
Daniel Veillard441bc322002-04-20 17:38:48 +00004354 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00004355 *
4356 * [8] QuantExact ::= [0-9]+
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00004357 *
4358 * Returns 0 if success or -1 in case of error
Daniel Veillard4255d502002-04-16 15:50:10 +00004359 */
4360static int
4361xmlFAParseQuantExact(xmlRegParserCtxtPtr ctxt) {
4362 int ret = 0;
4363 int ok = 0;
4364
4365 while ((CUR >= '0') && (CUR <= '9')) {
4366 ret = ret * 10 + (CUR - '0');
4367 ok = 1;
4368 NEXT;
4369 }
4370 if (ok != 1) {
4371 return(-1);
4372 }
4373 return(ret);
4374}
4375
4376/**
4377 * xmlFAParseQuantifier:
Daniel Veillard441bc322002-04-20 17:38:48 +00004378 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00004379 *
4380 * [4] quantifier ::= [?*+] | ( '{' quantity '}' )
4381 * [5] quantity ::= quantRange | quantMin | QuantExact
4382 * [6] quantRange ::= QuantExact ',' QuantExact
4383 * [7] quantMin ::= QuantExact ','
4384 * [8] QuantExact ::= [0-9]+
4385 */
4386static int
4387xmlFAParseQuantifier(xmlRegParserCtxtPtr ctxt) {
4388 int cur;
4389
4390 cur = CUR;
4391 if ((cur == '?') || (cur == '*') || (cur == '+')) {
4392 if (ctxt->atom != NULL) {
4393 if (cur == '?')
4394 ctxt->atom->quant = XML_REGEXP_QUANT_OPT;
4395 else if (cur == '*')
4396 ctxt->atom->quant = XML_REGEXP_QUANT_MULT;
4397 else if (cur == '+')
4398 ctxt->atom->quant = XML_REGEXP_QUANT_PLUS;
4399 }
4400 NEXT;
4401 return(1);
4402 }
4403 if (cur == '{') {
4404 int min = 0, max = 0;
4405
4406 NEXT;
4407 cur = xmlFAParseQuantExact(ctxt);
4408 if (cur >= 0)
4409 min = cur;
4410 if (CUR == ',') {
4411 NEXT;
Daniel Veillardebe48c62003-12-03 12:12:27 +00004412 if (CUR == '}')
4413 max = INT_MAX;
4414 else {
4415 cur = xmlFAParseQuantExact(ctxt);
4416 if (cur >= 0)
4417 max = cur;
4418 else {
4419 ERROR("Improper quantifier");
4420 }
4421 }
Daniel Veillard4255d502002-04-16 15:50:10 +00004422 }
4423 if (CUR == '}') {
4424 NEXT;
4425 } else {
4426 ERROR("Unterminated quantifier");
4427 }
4428 if (max == 0)
4429 max = min;
4430 if (ctxt->atom != NULL) {
4431 ctxt->atom->quant = XML_REGEXP_QUANT_RANGE;
4432 ctxt->atom->min = min;
4433 ctxt->atom->max = max;
4434 }
4435 return(1);
4436 }
4437 return(0);
4438}
4439
4440/**
4441 * xmlFAParseAtom:
Daniel Veillard441bc322002-04-20 17:38:48 +00004442 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00004443 *
4444 * [9] atom ::= Char | charClass | ( '(' regExp ')' )
4445 */
4446static int
4447xmlFAParseAtom(xmlRegParserCtxtPtr ctxt) {
4448 int codepoint, len;
4449
4450 codepoint = xmlFAIsChar(ctxt);
4451 if (codepoint > 0) {
4452 ctxt->atom = xmlRegNewAtom(ctxt, XML_REGEXP_CHARVAL);
4453 if (ctxt->atom == NULL)
4454 return(-1);
4455 codepoint = CUR_SCHAR(ctxt->cur, len);
4456 ctxt->atom->codepoint = codepoint;
4457 NEXTL(len);
4458 return(1);
4459 } else if (CUR == '|') {
4460 return(0);
4461 } else if (CUR == 0) {
4462 return(0);
4463 } else if (CUR == ')') {
4464 return(0);
4465 } else if (CUR == '(') {
4466 xmlRegStatePtr start, oldend;
4467
4468 NEXT;
4469 xmlFAGenerateEpsilonTransition(ctxt, ctxt->state, NULL);
4470 start = ctxt->state;
4471 oldend = ctxt->end;
4472 ctxt->end = NULL;
4473 ctxt->atom = NULL;
4474 xmlFAParseRegExp(ctxt, 0);
4475 if (CUR == ')') {
4476 NEXT;
4477 } else {
4478 ERROR("xmlFAParseAtom: expecting ')'");
4479 }
4480 ctxt->atom = xmlRegNewAtom(ctxt, XML_REGEXP_SUBREG);
4481 if (ctxt->atom == NULL)
4482 return(-1);
4483 ctxt->atom->start = start;
4484 ctxt->atom->stop = ctxt->state;
4485 ctxt->end = oldend;
4486 return(1);
4487 } else if ((CUR == '[') || (CUR == '\\') || (CUR == '.')) {
4488 xmlFAParseCharClass(ctxt);
4489 return(1);
4490 }
4491 return(0);
4492}
4493
4494/**
4495 * xmlFAParsePiece:
Daniel Veillard441bc322002-04-20 17:38:48 +00004496 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00004497 *
4498 * [3] piece ::= atom quantifier?
4499 */
4500static int
4501xmlFAParsePiece(xmlRegParserCtxtPtr ctxt) {
4502 int ret;
4503
4504 ctxt->atom = NULL;
4505 ret = xmlFAParseAtom(ctxt);
4506 if (ret == 0)
4507 return(0);
4508 if (ctxt->atom == NULL) {
4509 ERROR("internal: no atom generated");
4510 }
4511 xmlFAParseQuantifier(ctxt);
4512 return(1);
4513}
4514
4515/**
4516 * xmlFAParseBranch:
Daniel Veillard441bc322002-04-20 17:38:48 +00004517 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00004518 *
4519 * [2] branch ::= piece*
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00004520 8
Daniel Veillard4255d502002-04-16 15:50:10 +00004521 */
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00004522static int
Daniel Veillard2cbf5962004-03-31 15:50:43 +00004523xmlFAParseBranch(xmlRegParserCtxtPtr ctxt) {
Daniel Veillard4255d502002-04-16 15:50:10 +00004524 xmlRegStatePtr previous;
Daniel Veillard4255d502002-04-16 15:50:10 +00004525 int ret;
4526
4527 previous = ctxt->state;
4528 ret = xmlFAParsePiece(ctxt);
4529 if (ret != 0) {
Daniel Veillard2cbf5962004-03-31 15:50:43 +00004530 if (xmlFAGenerateTransitions(ctxt, previous, NULL, ctxt->atom) < 0)
4531 return(-1);
4532 previous = ctxt->state;
Daniel Veillard4255d502002-04-16 15:50:10 +00004533 ctxt->atom = NULL;
4534 }
4535 while ((ret != 0) && (ctxt->error == 0)) {
4536 ret = xmlFAParsePiece(ctxt);
4537 if (ret != 0) {
Daniel Veillard2cbf5962004-03-31 15:50:43 +00004538 if (xmlFAGenerateTransitions(ctxt, previous, NULL,
4539 ctxt->atom) < 0)
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00004540 return(-1);
Daniel Veillard4255d502002-04-16 15:50:10 +00004541 previous = ctxt->state;
4542 ctxt->atom = NULL;
4543 }
4544 }
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00004545 return(0);
Daniel Veillard4255d502002-04-16 15:50:10 +00004546}
4547
4548/**
4549 * xmlFAParseRegExp:
Daniel Veillard441bc322002-04-20 17:38:48 +00004550 * @ctxt: a regexp parser context
William M. Brackddf71d62004-05-06 04:17:26 +00004551 * @top: is this the top-level expression ?
Daniel Veillard4255d502002-04-16 15:50:10 +00004552 *
4553 * [1] regExp ::= branch ( '|' branch )*
4554 */
4555static void
4556xmlFAParseRegExp(xmlRegParserCtxtPtr ctxt, int top) {
Daniel Veillardc7e3cc42004-09-28 12:33:52 +00004557 xmlRegStatePtr start, end;
Daniel Veillard4255d502002-04-16 15:50:10 +00004558
Daniel Veillard2cbf5962004-03-31 15:50:43 +00004559 /* if not top start should have been generated by an epsilon trans */
Daniel Veillard4255d502002-04-16 15:50:10 +00004560 start = ctxt->state;
Daniel Veillard2cbf5962004-03-31 15:50:43 +00004561 ctxt->end = NULL;
4562 xmlFAParseBranch(ctxt);
4563 if (top) {
4564#ifdef DEBUG_REGEXP_GRAPH
4565 printf("State %d is final\n", ctxt->state->no);
4566#endif
4567 ctxt->state->type = XML_REGEXP_FINAL_STATE;
4568 }
Daniel Veillard4255d502002-04-16 15:50:10 +00004569 if (CUR != '|') {
4570 ctxt->end = ctxt->state;
4571 return;
4572 }
4573 end = ctxt->state;
4574 while ((CUR == '|') && (ctxt->error == 0)) {
4575 NEXT;
4576 ctxt->state = start;
Daniel Veillard2cbf5962004-03-31 15:50:43 +00004577 ctxt->end = NULL;
4578 xmlFAParseBranch(ctxt);
4579 if (top) {
4580 ctxt->state->type = XML_REGEXP_FINAL_STATE;
4581#ifdef DEBUG_REGEXP_GRAPH
4582 printf("State %d is final\n", ctxt->state->no);
4583#endif
4584 } else {
4585 xmlFAGenerateEpsilonTransition(ctxt, ctxt->state, end);
4586 }
Daniel Veillard4255d502002-04-16 15:50:10 +00004587 }
Daniel Veillard2cbf5962004-03-31 15:50:43 +00004588 if (!top) {
4589 ctxt->state = end;
4590 ctxt->end = end;
4591 }
Daniel Veillard4255d502002-04-16 15:50:10 +00004592}
4593
4594/************************************************************************
4595 * *
4596 * The basic API *
4597 * *
4598 ************************************************************************/
4599
4600/**
4601 * xmlRegexpPrint:
4602 * @output: the file for the output debug
4603 * @regexp: the compiled regexp
4604 *
4605 * Print the content of the compiled regular expression
4606 */
4607void
4608xmlRegexpPrint(FILE *output, xmlRegexpPtr regexp) {
4609 int i;
4610
Daniel Veillarda82b1822004-11-08 16:24:57 +00004611 if (output == NULL)
4612 return;
Daniel Veillard4255d502002-04-16 15:50:10 +00004613 fprintf(output, " regexp: ");
4614 if (regexp == NULL) {
4615 fprintf(output, "NULL\n");
4616 return;
4617 }
4618 fprintf(output, "'%s' ", regexp->string);
4619 fprintf(output, "\n");
4620 fprintf(output, "%d atoms:\n", regexp->nbAtoms);
4621 for (i = 0;i < regexp->nbAtoms; i++) {
4622 fprintf(output, " %02d ", i);
4623 xmlRegPrintAtom(output, regexp->atoms[i]);
4624 }
4625 fprintf(output, "%d states:", regexp->nbStates);
4626 fprintf(output, "\n");
4627 for (i = 0;i < regexp->nbStates; i++) {
4628 xmlRegPrintState(output, regexp->states[i]);
4629 }
4630 fprintf(output, "%d counters:\n", regexp->nbCounters);
4631 for (i = 0;i < regexp->nbCounters; i++) {
4632 fprintf(output, " %d: min %d max %d\n", i, regexp->counters[i].min,
4633 regexp->counters[i].max);
4634 }
4635}
4636
4637/**
4638 * xmlRegexpCompile:
4639 * @regexp: a regular expression string
4640 *
4641 * Parses a regular expression conforming to XML Schemas Part 2 Datatype
William M. Brackddf71d62004-05-06 04:17:26 +00004642 * Appendix F and builds an automata suitable for testing strings against
Daniel Veillard4255d502002-04-16 15:50:10 +00004643 * that regular expression
4644 *
4645 * Returns the compiled expression or NULL in case of error
4646 */
4647xmlRegexpPtr
4648xmlRegexpCompile(const xmlChar *regexp) {
4649 xmlRegexpPtr ret;
4650 xmlRegParserCtxtPtr ctxt;
4651
4652 ctxt = xmlRegNewParserCtxt(regexp);
4653 if (ctxt == NULL)
4654 return(NULL);
4655
4656 /* initialize the parser */
4657 ctxt->end = NULL;
4658 ctxt->start = ctxt->state = xmlRegNewState(ctxt);
4659 xmlRegStatePush(ctxt, ctxt->start);
4660
4661 /* parse the expression building an automata */
4662 xmlFAParseRegExp(ctxt, 1);
4663 if (CUR != 0) {
4664 ERROR("xmlFAParseRegExp: extra characters");
4665 }
4666 ctxt->end = ctxt->state;
4667 ctxt->start->type = XML_REGEXP_START_STATE;
4668 ctxt->end->type = XML_REGEXP_FINAL_STATE;
4669
4670 /* remove the Epsilon except for counted transitions */
4671 xmlFAEliminateEpsilonTransitions(ctxt);
4672
4673
4674 if (ctxt->error != 0) {
4675 xmlRegFreeParserCtxt(ctxt);
4676 return(NULL);
4677 }
4678 ret = xmlRegEpxFromParse(ctxt);
4679 xmlRegFreeParserCtxt(ctxt);
4680 return(ret);
4681}
4682
4683/**
4684 * xmlRegexpExec:
4685 * @comp: the compiled regular expression
4686 * @content: the value to check against the regular expression
4687 *
William M. Brackddf71d62004-05-06 04:17:26 +00004688 * Check if the regular expression generates the value
Daniel Veillard4255d502002-04-16 15:50:10 +00004689 *
William M. Brackddf71d62004-05-06 04:17:26 +00004690 * Returns 1 if it matches, 0 if not and a negative value in case of error
Daniel Veillard4255d502002-04-16 15:50:10 +00004691 */
4692int
4693xmlRegexpExec(xmlRegexpPtr comp, const xmlChar *content) {
4694 if ((comp == NULL) || (content == NULL))
4695 return(-1);
4696 return(xmlFARegExec(comp, content));
4697}
4698
4699/**
Daniel Veillard23e73572002-09-19 19:56:43 +00004700 * xmlRegexpIsDeterminist:
4701 * @comp: the compiled regular expression
4702 *
4703 * Check if the regular expression is determinist
4704 *
William M. Brackddf71d62004-05-06 04:17:26 +00004705 * Returns 1 if it yes, 0 if not and a negative value in case of error
Daniel Veillard23e73572002-09-19 19:56:43 +00004706 */
4707int
4708xmlRegexpIsDeterminist(xmlRegexpPtr comp) {
4709 xmlAutomataPtr am;
4710 int ret;
4711
4712 if (comp == NULL)
4713 return(-1);
4714 if (comp->determinist != -1)
4715 return(comp->determinist);
4716
4717 am = xmlNewAutomata();
Daniel Veillardbd9afb52002-09-25 22:25:35 +00004718 if (am->states != NULL) {
4719 int i;
4720
4721 for (i = 0;i < am->nbStates;i++)
4722 xmlRegFreeState(am->states[i]);
4723 xmlFree(am->states);
4724 }
Daniel Veillard23e73572002-09-19 19:56:43 +00004725 am->nbAtoms = comp->nbAtoms;
4726 am->atoms = comp->atoms;
4727 am->nbStates = comp->nbStates;
4728 am->states = comp->states;
4729 am->determinist = -1;
4730 ret = xmlFAComputesDeterminism(am);
4731 am->atoms = NULL;
4732 am->states = NULL;
4733 xmlFreeAutomata(am);
4734 return(ret);
4735}
4736
4737/**
Daniel Veillard4255d502002-04-16 15:50:10 +00004738 * xmlRegFreeRegexp:
4739 * @regexp: the regexp
4740 *
4741 * Free a regexp
4742 */
4743void
4744xmlRegFreeRegexp(xmlRegexpPtr regexp) {
4745 int i;
4746 if (regexp == NULL)
4747 return;
4748
4749 if (regexp->string != NULL)
4750 xmlFree(regexp->string);
4751 if (regexp->states != NULL) {
4752 for (i = 0;i < regexp->nbStates;i++)
4753 xmlRegFreeState(regexp->states[i]);
4754 xmlFree(regexp->states);
4755 }
4756 if (regexp->atoms != NULL) {
4757 for (i = 0;i < regexp->nbAtoms;i++)
4758 xmlRegFreeAtom(regexp->atoms[i]);
4759 xmlFree(regexp->atoms);
4760 }
4761 if (regexp->counters != NULL)
4762 xmlFree(regexp->counters);
Daniel Veillard23e73572002-09-19 19:56:43 +00004763 if (regexp->compact != NULL)
4764 xmlFree(regexp->compact);
Daniel Veillard118aed72002-09-24 14:13:13 +00004765 if (regexp->transdata != NULL)
4766 xmlFree(regexp->transdata);
Daniel Veillard23e73572002-09-19 19:56:43 +00004767 if (regexp->stringMap != NULL) {
4768 for (i = 0; i < regexp->nbstrings;i++)
4769 xmlFree(regexp->stringMap[i]);
4770 xmlFree(regexp->stringMap);
4771 }
4772
Daniel Veillard4255d502002-04-16 15:50:10 +00004773 xmlFree(regexp);
4774}
4775
4776#ifdef LIBXML_AUTOMATA_ENABLED
4777/************************************************************************
4778 * *
4779 * The Automata interface *
4780 * *
4781 ************************************************************************/
4782
4783/**
4784 * xmlNewAutomata:
4785 *
4786 * Create a new automata
4787 *
4788 * Returns the new object or NULL in case of failure
4789 */
4790xmlAutomataPtr
4791xmlNewAutomata(void) {
4792 xmlAutomataPtr ctxt;
4793
4794 ctxt = xmlRegNewParserCtxt(NULL);
4795 if (ctxt == NULL)
4796 return(NULL);
4797
4798 /* initialize the parser */
4799 ctxt->end = NULL;
4800 ctxt->start = ctxt->state = xmlRegNewState(ctxt);
Daniel Veillarddb68b742005-07-30 13:18:24 +00004801 ctxt->start->type = XML_REGEXP_START_STATE;
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00004802 if (ctxt->start == NULL) {
4803 xmlFreeAutomata(ctxt);
4804 return(NULL);
4805 }
4806 if (xmlRegStatePush(ctxt, ctxt->start) < 0) {
4807 xmlRegFreeState(ctxt->start);
4808 xmlFreeAutomata(ctxt);
4809 return(NULL);
4810 }
Daniel Veillard4255d502002-04-16 15:50:10 +00004811
4812 return(ctxt);
4813}
4814
4815/**
4816 * xmlFreeAutomata:
4817 * @am: an automata
4818 *
4819 * Free an automata
4820 */
4821void
4822xmlFreeAutomata(xmlAutomataPtr am) {
4823 if (am == NULL)
4824 return;
4825 xmlRegFreeParserCtxt(am);
4826}
4827
4828/**
4829 * xmlAutomataGetInitState:
4830 * @am: an automata
4831 *
Daniel Veillarda9b66d02002-12-11 14:23:49 +00004832 * Initial state lookup
4833 *
Daniel Veillard4255d502002-04-16 15:50:10 +00004834 * Returns the initial state of the automata
4835 */
4836xmlAutomataStatePtr
4837xmlAutomataGetInitState(xmlAutomataPtr am) {
4838 if (am == NULL)
4839 return(NULL);
4840 return(am->start);
4841}
4842
4843/**
4844 * xmlAutomataSetFinalState:
4845 * @am: an automata
4846 * @state: a state in this automata
4847 *
4848 * Makes that state a final state
4849 *
4850 * Returns 0 or -1 in case of error
4851 */
4852int
4853xmlAutomataSetFinalState(xmlAutomataPtr am, xmlAutomataStatePtr state) {
4854 if ((am == NULL) || (state == NULL))
4855 return(-1);
4856 state->type = XML_REGEXP_FINAL_STATE;
4857 return(0);
4858}
4859
4860/**
4861 * xmlAutomataNewTransition:
4862 * @am: an automata
4863 * @from: the starting point of the transition
4864 * @to: the target point of the transition or NULL
4865 * @token: the input string associated to that transition
4866 * @data: data passed to the callback function if the transition is activated
4867 *
William M. Brackddf71d62004-05-06 04:17:26 +00004868 * If @to is NULL, this creates first a new target state in the automata
Daniel Veillard4255d502002-04-16 15:50:10 +00004869 * and then adds a transition from the @from state to the target state
4870 * activated by the value of @token
4871 *
4872 * Returns the target state or NULL in case of error
4873 */
4874xmlAutomataStatePtr
4875xmlAutomataNewTransition(xmlAutomataPtr am, xmlAutomataStatePtr from,
4876 xmlAutomataStatePtr to, const xmlChar *token,
4877 void *data) {
4878 xmlRegAtomPtr atom;
4879
4880 if ((am == NULL) || (from == NULL) || (token == NULL))
4881 return(NULL);
4882 atom = xmlRegNewAtom(am, XML_REGEXP_STRING);
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00004883 if (atom == NULL)
4884 return(NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +00004885 atom->data = data;
4886 if (atom == NULL)
4887 return(NULL);
4888 atom->valuep = xmlStrdup(token);
4889
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00004890 if (xmlFAGenerateTransitions(am, from, to, atom) < 0) {
4891 xmlRegFreeAtom(atom);
4892 return(NULL);
4893 }
Daniel Veillard4255d502002-04-16 15:50:10 +00004894 if (to == NULL)
4895 return(am->state);
4896 return(to);
4897}
4898
4899/**
Daniel Veillard52b48c72003-04-13 19:53:42 +00004900 * xmlAutomataNewTransition2:
4901 * @am: an automata
4902 * @from: the starting point of the transition
4903 * @to: the target point of the transition or NULL
4904 * @token: the first input string associated to that transition
4905 * @token2: the second input string associated to that transition
4906 * @data: data passed to the callback function if the transition is activated
4907 *
William M. Brackddf71d62004-05-06 04:17:26 +00004908 * If @to is NULL, this creates first a new target state in the automata
Daniel Veillard52b48c72003-04-13 19:53:42 +00004909 * and then adds a transition from the @from state to the target state
4910 * activated by the value of @token
4911 *
4912 * Returns the target state or NULL in case of error
4913 */
4914xmlAutomataStatePtr
4915xmlAutomataNewTransition2(xmlAutomataPtr am, xmlAutomataStatePtr from,
4916 xmlAutomataStatePtr to, const xmlChar *token,
4917 const xmlChar *token2, void *data) {
4918 xmlRegAtomPtr atom;
4919
4920 if ((am == NULL) || (from == NULL) || (token == NULL))
4921 return(NULL);
4922 atom = xmlRegNewAtom(am, XML_REGEXP_STRING);
4923 atom->data = data;
4924 if (atom == NULL)
4925 return(NULL);
4926 if ((token2 == NULL) || (*token2 == 0)) {
4927 atom->valuep = xmlStrdup(token);
4928 } else {
4929 int lenn, lenp;
4930 xmlChar *str;
4931
4932 lenn = strlen((char *) token2);
4933 lenp = strlen((char *) token);
4934
Daniel Veillard3c908dc2003-04-19 00:07:51 +00004935 str = (xmlChar *) xmlMallocAtomic(lenn + lenp + 2);
Daniel Veillard52b48c72003-04-13 19:53:42 +00004936 if (str == NULL) {
4937 xmlRegFreeAtom(atom);
4938 return(NULL);
4939 }
4940 memcpy(&str[0], token, lenp);
4941 str[lenp] = '|';
4942 memcpy(&str[lenp + 1], token2, lenn);
4943 str[lenn + lenp + 1] = 0;
4944
4945 atom->valuep = str;
4946 }
4947
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00004948 if (xmlFAGenerateTransitions(am, from, to, atom) < 0) {
4949 xmlRegFreeAtom(atom);
4950 return(NULL);
4951 }
Daniel Veillard52b48c72003-04-13 19:53:42 +00004952 if (to == NULL)
4953 return(am->state);
4954 return(to);
4955}
4956
4957/**
Daniel Veillard9efc4762005-07-19 14:33:55 +00004958 * xmlAutomataNewNegTrans:
4959 * @am: an automata
4960 * @from: the starting point of the transition
4961 * @to: the target point of the transition or NULL
4962 * @token: the first input string associated to that transition
4963 * @token2: the second input string associated to that transition
4964 * @data: data passed to the callback function if the transition is activated
4965 *
4966 * If @to is NULL, this creates first a new target state in the automata
4967 * and then adds a transition from the @from state to the target state
4968 * activated by any value except (@token,@token2)
Daniel Veillard6e65e152005-08-09 11:09:52 +00004969 * Note that if @token2 is not NULL, then (X, NULL) won't match to follow
4970 # the semantic of XSD ##other
Daniel Veillard9efc4762005-07-19 14:33:55 +00004971 *
4972 * Returns the target state or NULL in case of error
4973 */
4974xmlAutomataStatePtr
4975xmlAutomataNewNegTrans(xmlAutomataPtr am, xmlAutomataStatePtr from,
4976 xmlAutomataStatePtr to, const xmlChar *token,
4977 const xmlChar *token2, void *data) {
4978 xmlRegAtomPtr atom;
Daniel Veillard77005e62005-07-19 16:26:18 +00004979 xmlChar err_msg[200];
Daniel Veillard9efc4762005-07-19 14:33:55 +00004980
4981 if ((am == NULL) || (from == NULL) || (token == NULL))
4982 return(NULL);
4983 atom = xmlRegNewAtom(am, XML_REGEXP_STRING);
4984 if (atom == NULL)
4985 return(NULL);
4986 atom->data = data;
4987 atom->neg = 1;
4988 if ((token2 == NULL) || (*token2 == 0)) {
4989 atom->valuep = xmlStrdup(token);
4990 } else {
4991 int lenn, lenp;
4992 xmlChar *str;
4993
4994 lenn = strlen((char *) token2);
4995 lenp = strlen((char *) token);
4996
4997 str = (xmlChar *) xmlMallocAtomic(lenn + lenp + 2);
4998 if (str == NULL) {
4999 xmlRegFreeAtom(atom);
5000 return(NULL);
5001 }
5002 memcpy(&str[0], token, lenp);
5003 str[lenp] = '|';
5004 memcpy(&str[lenp + 1], token2, lenn);
5005 str[lenn + lenp + 1] = 0;
5006
5007 atom->valuep = str;
5008 }
Daniel Veillarddb68b742005-07-30 13:18:24 +00005009 snprintf((char *) err_msg, 199, "not %s", (const char *) atom->valuep);
Daniel Veillard77005e62005-07-19 16:26:18 +00005010 err_msg[199] = 0;
5011 atom->valuep2 = xmlStrdup(err_msg);
Daniel Veillard9efc4762005-07-19 14:33:55 +00005012
5013 if (xmlFAGenerateTransitions(am, from, to, atom) < 0) {
5014 xmlRegFreeAtom(atom);
5015 return(NULL);
5016 }
Daniel Veillard6e65e152005-08-09 11:09:52 +00005017 am->negs++;
Daniel Veillard9efc4762005-07-19 14:33:55 +00005018 if (to == NULL)
5019 return(am->state);
5020 return(to);
5021}
5022
5023/**
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00005024 * xmlAutomataNewCountTrans2:
5025 * @am: an automata
5026 * @from: the starting point of the transition
5027 * @to: the target point of the transition or NULL
5028 * @token: the input string associated to that transition
5029 * @token2: the second input string associated to that transition
5030 * @min: the minimum successive occurences of token
5031 * @max: the maximum successive occurences of token
5032 * @data: data associated to the transition
5033 *
5034 * If @to is NULL, this creates first a new target state in the automata
5035 * and then adds a transition from the @from state to the target state
5036 * activated by a succession of input of value @token and @token2 and
5037 * whose number is between @min and @max
5038 *
5039 * Returns the target state or NULL in case of error
5040 */
5041xmlAutomataStatePtr
5042xmlAutomataNewCountTrans2(xmlAutomataPtr am, xmlAutomataStatePtr from,
5043 xmlAutomataStatePtr to, const xmlChar *token,
5044 const xmlChar *token2,
5045 int min, int max, void *data) {
5046 xmlRegAtomPtr atom;
5047 int counter;
5048
5049 if ((am == NULL) || (from == NULL) || (token == NULL))
5050 return(NULL);
5051 if (min < 0)
5052 return(NULL);
5053 if ((max < min) || (max < 1))
5054 return(NULL);
5055 atom = xmlRegNewAtom(am, XML_REGEXP_STRING);
5056 if (atom == NULL)
5057 return(NULL);
5058 if ((token2 == NULL) || (*token2 == 0)) {
5059 atom->valuep = xmlStrdup(token);
5060 } else {
5061 int lenn, lenp;
5062 xmlChar *str;
5063
5064 lenn = strlen((char *) token2);
5065 lenp = strlen((char *) token);
5066
5067 str = (xmlChar *) xmlMallocAtomic(lenn + lenp + 2);
5068 if (str == NULL) {
5069 xmlRegFreeAtom(atom);
5070 return(NULL);
5071 }
5072 memcpy(&str[0], token, lenp);
5073 str[lenp] = '|';
5074 memcpy(&str[lenp + 1], token2, lenn);
5075 str[lenn + lenp + 1] = 0;
5076
5077 atom->valuep = str;
5078 }
5079 atom->data = data;
5080 if (min == 0)
5081 atom->min = 1;
5082 else
5083 atom->min = min;
5084 atom->max = max;
5085
5086 /*
5087 * associate a counter to the transition.
5088 */
5089 counter = xmlRegGetCounter(am);
5090 am->counters[counter].min = min;
5091 am->counters[counter].max = max;
5092
5093 /* xmlFAGenerateTransitions(am, from, to, atom); */
5094 if (to == NULL) {
5095 to = xmlRegNewState(am);
5096 xmlRegStatePush(am, to);
5097 }
Daniel Veillarddb68b742005-07-30 13:18:24 +00005098 xmlRegStateAddTrans(am, from, atom, to, counter, -1, 0);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00005099 xmlRegAtomPush(am, atom);
5100 am->state = to;
5101
5102 if (to == NULL)
5103 to = am->state;
5104 if (to == NULL)
5105 return(NULL);
5106 if (min == 0)
5107 xmlFAGenerateEpsilonTransition(am, from, to);
5108 return(to);
5109}
5110
5111/**
Daniel Veillard4255d502002-04-16 15:50:10 +00005112 * xmlAutomataNewCountTrans:
5113 * @am: an automata
5114 * @from: the starting point of the transition
5115 * @to: the target point of the transition or NULL
5116 * @token: the input string associated to that transition
5117 * @min: the minimum successive occurences of token
Daniel Veillarda9b66d02002-12-11 14:23:49 +00005118 * @max: the maximum successive occurences of token
5119 * @data: data associated to the transition
Daniel Veillard4255d502002-04-16 15:50:10 +00005120 *
William M. Brackddf71d62004-05-06 04:17:26 +00005121 * If @to is NULL, this creates first a new target state in the automata
Daniel Veillard4255d502002-04-16 15:50:10 +00005122 * and then adds a transition from the @from state to the target state
5123 * activated by a succession of input of value @token and whose number
5124 * is between @min and @max
5125 *
5126 * Returns the target state or NULL in case of error
5127 */
5128xmlAutomataStatePtr
5129xmlAutomataNewCountTrans(xmlAutomataPtr am, xmlAutomataStatePtr from,
5130 xmlAutomataStatePtr to, const xmlChar *token,
5131 int min, int max, void *data) {
5132 xmlRegAtomPtr atom;
Daniel Veillard0ddb21c2004-02-12 12:43:49 +00005133 int counter;
Daniel Veillard4255d502002-04-16 15:50:10 +00005134
5135 if ((am == NULL) || (from == NULL) || (token == NULL))
5136 return(NULL);
5137 if (min < 0)
5138 return(NULL);
5139 if ((max < min) || (max < 1))
5140 return(NULL);
5141 atom = xmlRegNewAtom(am, XML_REGEXP_STRING);
5142 if (atom == NULL)
5143 return(NULL);
5144 atom->valuep = xmlStrdup(token);
5145 atom->data = data;
5146 if (min == 0)
5147 atom->min = 1;
5148 else
5149 atom->min = min;
5150 atom->max = max;
5151
Daniel Veillard0ddb21c2004-02-12 12:43:49 +00005152 /*
5153 * associate a counter to the transition.
5154 */
5155 counter = xmlRegGetCounter(am);
5156 am->counters[counter].min = min;
5157 am->counters[counter].max = max;
5158
5159 /* xmlFAGenerateTransitions(am, from, to, atom); */
5160 if (to == NULL) {
5161 to = xmlRegNewState(am);
5162 xmlRegStatePush(am, to);
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00005163 }
Daniel Veillarddb68b742005-07-30 13:18:24 +00005164 xmlRegStateAddTrans(am, from, atom, to, counter, -1, 0);
Daniel Veillard0ddb21c2004-02-12 12:43:49 +00005165 xmlRegAtomPush(am, atom);
5166 am->state = to;
5167
Daniel Veillard4255d502002-04-16 15:50:10 +00005168 if (to == NULL)
5169 to = am->state;
5170 if (to == NULL)
5171 return(NULL);
5172 if (min == 0)
5173 xmlFAGenerateEpsilonTransition(am, from, to);
5174 return(to);
5175}
5176
5177/**
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00005178 * xmlAutomataNewOnceTrans2:
5179 * @am: an automata
5180 * @from: the starting point of the transition
5181 * @to: the target point of the transition or NULL
5182 * @token: the input string associated to that transition
5183 * @token2: the second input string associated to that transition
5184 * @min: the minimum successive occurences of token
5185 * @max: the maximum successive occurences of token
5186 * @data: data associated to the transition
5187 *
5188 * If @to is NULL, this creates first a new target state in the automata
5189 * and then adds a transition from the @from state to the target state
5190 * activated by a succession of input of value @token and @token2 and whose
5191 * number is between @min and @max, moreover that transition can only be
5192 * crossed once.
5193 *
5194 * Returns the target state or NULL in case of error
5195 */
5196xmlAutomataStatePtr
5197xmlAutomataNewOnceTrans2(xmlAutomataPtr am, xmlAutomataStatePtr from,
5198 xmlAutomataStatePtr to, const xmlChar *token,
5199 const xmlChar *token2,
5200 int min, int max, void *data) {
5201 xmlRegAtomPtr atom;
5202 int counter;
5203
5204 if ((am == NULL) || (from == NULL) || (token == NULL))
5205 return(NULL);
5206 if (min < 1)
5207 return(NULL);
5208 if ((max < min) || (max < 1))
5209 return(NULL);
5210 atom = xmlRegNewAtom(am, XML_REGEXP_STRING);
5211 if (atom == NULL)
5212 return(NULL);
5213 if ((token2 == NULL) || (*token2 == 0)) {
5214 atom->valuep = xmlStrdup(token);
5215 } else {
5216 int lenn, lenp;
5217 xmlChar *str;
5218
5219 lenn = strlen((char *) token2);
5220 lenp = strlen((char *) token);
5221
5222 str = (xmlChar *) xmlMallocAtomic(lenn + lenp + 2);
5223 if (str == NULL) {
5224 xmlRegFreeAtom(atom);
5225 return(NULL);
5226 }
5227 memcpy(&str[0], token, lenp);
5228 str[lenp] = '|';
5229 memcpy(&str[lenp + 1], token2, lenn);
5230 str[lenn + lenp + 1] = 0;
5231
5232 atom->valuep = str;
5233 }
5234 atom->data = data;
5235 atom->quant = XML_REGEXP_QUANT_ONCEONLY;
5236 if (min == 0)
5237 atom->min = 1;
5238 else
5239 atom->min = min;
5240 atom->max = max;
5241 /*
5242 * associate a counter to the transition.
5243 */
5244 counter = xmlRegGetCounter(am);
5245 am->counters[counter].min = 1;
5246 am->counters[counter].max = 1;
5247
5248 /* xmlFAGenerateTransitions(am, from, to, atom); */
5249 if (to == NULL) {
5250 to = xmlRegNewState(am);
5251 xmlRegStatePush(am, to);
5252 }
Daniel Veillarddb68b742005-07-30 13:18:24 +00005253 xmlRegStateAddTrans(am, from, atom, to, counter, -1, 0);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00005254 xmlRegAtomPush(am, atom);
5255 am->state = to;
5256 return(to);
5257}
5258
5259
5260
5261/**
Daniel Veillard7646b182002-04-20 06:41:40 +00005262 * xmlAutomataNewOnceTrans:
5263 * @am: an automata
5264 * @from: the starting point of the transition
5265 * @to: the target point of the transition or NULL
5266 * @token: the input string associated to that transition
5267 * @min: the minimum successive occurences of token
Daniel Veillarda9b66d02002-12-11 14:23:49 +00005268 * @max: the maximum successive occurences of token
5269 * @data: data associated to the transition
Daniel Veillard7646b182002-04-20 06:41:40 +00005270 *
William M. Brackddf71d62004-05-06 04:17:26 +00005271 * If @to is NULL, this creates first a new target state in the automata
Daniel Veillard7646b182002-04-20 06:41:40 +00005272 * and then adds a transition from the @from state to the target state
5273 * activated by a succession of input of value @token and whose number
William M. Brackddf71d62004-05-06 04:17:26 +00005274 * is between @min and @max, moreover that transition can only be crossed
Daniel Veillard7646b182002-04-20 06:41:40 +00005275 * once.
5276 *
5277 * Returns the target state or NULL in case of error
5278 */
5279xmlAutomataStatePtr
5280xmlAutomataNewOnceTrans(xmlAutomataPtr am, xmlAutomataStatePtr from,
5281 xmlAutomataStatePtr to, const xmlChar *token,
5282 int min, int max, void *data) {
5283 xmlRegAtomPtr atom;
5284 int counter;
5285
5286 if ((am == NULL) || (from == NULL) || (token == NULL))
5287 return(NULL);
5288 if (min < 1)
5289 return(NULL);
5290 if ((max < min) || (max < 1))
5291 return(NULL);
5292 atom = xmlRegNewAtom(am, XML_REGEXP_STRING);
5293 if (atom == NULL)
5294 return(NULL);
5295 atom->valuep = xmlStrdup(token);
5296 atom->data = data;
5297 atom->quant = XML_REGEXP_QUANT_ONCEONLY;
5298 if (min == 0)
5299 atom->min = 1;
5300 else
5301 atom->min = min;
5302 atom->max = max;
5303 /*
5304 * associate a counter to the transition.
5305 */
5306 counter = xmlRegGetCounter(am);
5307 am->counters[counter].min = 1;
5308 am->counters[counter].max = 1;
5309
5310 /* xmlFAGenerateTransitions(am, from, to, atom); */
5311 if (to == NULL) {
5312 to = xmlRegNewState(am);
5313 xmlRegStatePush(am, to);
5314 }
Daniel Veillarddb68b742005-07-30 13:18:24 +00005315 xmlRegStateAddTrans(am, from, atom, to, counter, -1, 0);
Daniel Veillard7646b182002-04-20 06:41:40 +00005316 xmlRegAtomPush(am, atom);
5317 am->state = to;
Daniel Veillard7646b182002-04-20 06:41:40 +00005318 return(to);
5319}
5320
5321/**
Daniel Veillard4255d502002-04-16 15:50:10 +00005322 * xmlAutomataNewState:
5323 * @am: an automata
5324 *
5325 * Create a new disconnected state in the automata
5326 *
5327 * Returns the new state or NULL in case of error
5328 */
5329xmlAutomataStatePtr
5330xmlAutomataNewState(xmlAutomataPtr am) {
5331 xmlAutomataStatePtr to;
5332
5333 if (am == NULL)
5334 return(NULL);
5335 to = xmlRegNewState(am);
5336 xmlRegStatePush(am, to);
5337 return(to);
5338}
5339
5340/**
Daniel Veillarda9b66d02002-12-11 14:23:49 +00005341 * xmlAutomataNewEpsilon:
Daniel Veillard4255d502002-04-16 15:50:10 +00005342 * @am: an automata
5343 * @from: the starting point of the transition
5344 * @to: the target point of the transition or NULL
5345 *
William M. Brackddf71d62004-05-06 04:17:26 +00005346 * If @to is NULL, this creates first a new target state in the automata
5347 * and then adds an epsilon transition from the @from state to the
Daniel Veillard4255d502002-04-16 15:50:10 +00005348 * target state
5349 *
5350 * Returns the target state or NULL in case of error
5351 */
5352xmlAutomataStatePtr
5353xmlAutomataNewEpsilon(xmlAutomataPtr am, xmlAutomataStatePtr from,
5354 xmlAutomataStatePtr to) {
5355 if ((am == NULL) || (from == NULL))
5356 return(NULL);
5357 xmlFAGenerateEpsilonTransition(am, from, to);
5358 if (to == NULL)
5359 return(am->state);
5360 return(to);
5361}
5362
Daniel Veillardb509f152002-04-17 16:28:10 +00005363/**
Daniel Veillard7646b182002-04-20 06:41:40 +00005364 * xmlAutomataNewAllTrans:
5365 * @am: an automata
5366 * @from: the starting point of the transition
5367 * @to: the target point of the transition or NULL
Daniel Veillarda9b66d02002-12-11 14:23:49 +00005368 * @lax: allow to transition if not all all transitions have been activated
Daniel Veillard7646b182002-04-20 06:41:40 +00005369 *
William M. Brackddf71d62004-05-06 04:17:26 +00005370 * If @to is NULL, this creates first a new target state in the automata
Daniel Veillard7646b182002-04-20 06:41:40 +00005371 * and then adds a an ALL transition from the @from state to the
5372 * target state. That transition is an epsilon transition allowed only when
5373 * all transitions from the @from node have been activated.
5374 *
5375 * Returns the target state or NULL in case of error
5376 */
5377xmlAutomataStatePtr
5378xmlAutomataNewAllTrans(xmlAutomataPtr am, xmlAutomataStatePtr from,
Daniel Veillard441bc322002-04-20 17:38:48 +00005379 xmlAutomataStatePtr to, int lax) {
Daniel Veillard7646b182002-04-20 06:41:40 +00005380 if ((am == NULL) || (from == NULL))
5381 return(NULL);
Daniel Veillard441bc322002-04-20 17:38:48 +00005382 xmlFAGenerateAllTransition(am, from, to, lax);
Daniel Veillard7646b182002-04-20 06:41:40 +00005383 if (to == NULL)
5384 return(am->state);
5385 return(to);
5386}
5387
5388/**
Daniel Veillardb509f152002-04-17 16:28:10 +00005389 * xmlAutomataNewCounter:
5390 * @am: an automata
5391 * @min: the minimal value on the counter
5392 * @max: the maximal value on the counter
5393 *
5394 * Create a new counter
5395 *
5396 * Returns the counter number or -1 in case of error
5397 */
5398int
5399xmlAutomataNewCounter(xmlAutomataPtr am, int min, int max) {
5400 int ret;
5401
5402 if (am == NULL)
5403 return(-1);
5404
5405 ret = xmlRegGetCounter(am);
5406 if (ret < 0)
5407 return(-1);
5408 am->counters[ret].min = min;
5409 am->counters[ret].max = max;
5410 return(ret);
5411}
5412
5413/**
5414 * xmlAutomataNewCountedTrans:
5415 * @am: an automata
5416 * @from: the starting point of the transition
5417 * @to: the target point of the transition or NULL
5418 * @counter: the counter associated to that transition
5419 *
William M. Brackddf71d62004-05-06 04:17:26 +00005420 * If @to is NULL, this creates first a new target state in the automata
Daniel Veillardb509f152002-04-17 16:28:10 +00005421 * and then adds an epsilon transition from the @from state to the target state
5422 * which will increment the counter provided
5423 *
5424 * Returns the target state or NULL in case of error
5425 */
5426xmlAutomataStatePtr
5427xmlAutomataNewCountedTrans(xmlAutomataPtr am, xmlAutomataStatePtr from,
5428 xmlAutomataStatePtr to, int counter) {
5429 if ((am == NULL) || (from == NULL) || (counter < 0))
5430 return(NULL);
5431 xmlFAGenerateCountedEpsilonTransition(am, from, to, counter);
5432 if (to == NULL)
5433 return(am->state);
5434 return(to);
5435}
5436
5437/**
5438 * xmlAutomataNewCounterTrans:
5439 * @am: an automata
5440 * @from: the starting point of the transition
5441 * @to: the target point of the transition or NULL
5442 * @counter: the counter associated to that transition
5443 *
William M. Brackddf71d62004-05-06 04:17:26 +00005444 * If @to is NULL, this creates first a new target state in the automata
Daniel Veillardb509f152002-04-17 16:28:10 +00005445 * and then adds an epsilon transition from the @from state to the target state
5446 * which will be allowed only if the counter is within the right range.
5447 *
5448 * Returns the target state or NULL in case of error
5449 */
5450xmlAutomataStatePtr
5451xmlAutomataNewCounterTrans(xmlAutomataPtr am, xmlAutomataStatePtr from,
5452 xmlAutomataStatePtr to, int counter) {
5453 if ((am == NULL) || (from == NULL) || (counter < 0))
5454 return(NULL);
5455 xmlFAGenerateCountedTransition(am, from, to, counter);
5456 if (to == NULL)
5457 return(am->state);
5458 return(to);
5459}
Daniel Veillard4255d502002-04-16 15:50:10 +00005460
5461/**
5462 * xmlAutomataCompile:
5463 * @am: an automata
5464 *
5465 * Compile the automata into a Reg Exp ready for being executed.
5466 * The automata should be free after this point.
5467 *
5468 * Returns the compiled regexp or NULL in case of error
5469 */
5470xmlRegexpPtr
5471xmlAutomataCompile(xmlAutomataPtr am) {
5472 xmlRegexpPtr ret;
5473
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00005474 if ((am == NULL) || (am->error != 0)) return(NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +00005475 xmlFAEliminateEpsilonTransitions(am);
Daniel Veillard23e73572002-09-19 19:56:43 +00005476 /* xmlFAComputesDeterminism(am); */
Daniel Veillard4255d502002-04-16 15:50:10 +00005477 ret = xmlRegEpxFromParse(am);
5478
5479 return(ret);
5480}
Daniel Veillarde19fc232002-04-22 16:01:24 +00005481
5482/**
5483 * xmlAutomataIsDeterminist:
5484 * @am: an automata
5485 *
5486 * Checks if an automata is determinist.
5487 *
5488 * Returns 1 if true, 0 if not, and -1 in case of error
5489 */
5490int
5491xmlAutomataIsDeterminist(xmlAutomataPtr am) {
5492 int ret;
5493
5494 if (am == NULL)
5495 return(-1);
5496
5497 ret = xmlFAComputesDeterminism(am);
5498 return(ret);
5499}
Daniel Veillard4255d502002-04-16 15:50:10 +00005500#endif /* LIBXML_AUTOMATA_ENABLED */
Daniel Veillard81a8ec62005-08-22 00:20:58 +00005501
5502#ifdef LIBXML_EXPR_ENABLED
5503/************************************************************************
5504 * *
5505 * Formal Expression handling code *
5506 * *
5507 ************************************************************************/
Daniel Veillard81a8ec62005-08-22 00:20:58 +00005508/************************************************************************
5509 * *
5510 * Expression handling context *
5511 * *
5512 ************************************************************************/
5513
5514struct _xmlExpCtxt {
5515 xmlDictPtr dict;
5516 xmlExpNodePtr *table;
5517 int size;
5518 int nbElems;
5519 int nb_nodes;
5520 const char *expr;
5521 const char *cur;
5522 int nb_cons;
Daniel Veillard81a8ec62005-08-22 00:20:58 +00005523 int tabSize;
5524};
5525
5526/**
5527 * xmlExpNewCtxt:
5528 * @maxNodes: the maximum number of nodes
5529 * @dict: optional dictionnary to use internally
5530 *
5531 * Creates a new context for manipulating expressions
5532 *
5533 * Returns the context or NULL in case of error
5534 */
5535xmlExpCtxtPtr
5536xmlExpNewCtxt(int maxNodes, xmlDictPtr dict) {
5537 xmlExpCtxtPtr ret;
5538 int size = 256;
5539
5540 if (maxNodes <= 4096)
5541 maxNodes = 4096;
5542
5543 ret = (xmlExpCtxtPtr) xmlMalloc(sizeof(xmlExpCtxt));
5544 if (ret == NULL)
5545 return(NULL);
5546 memset(ret, 0, sizeof(xmlExpCtxt));
5547 ret->size = size;
5548 ret->nbElems = 0;
5549 ret->table = xmlMalloc(size * sizeof(xmlExpNodePtr));
5550 if (ret->table == NULL) {
5551 xmlFree(ret);
5552 return(NULL);
5553 }
5554 memset(ret->table, 0, size * sizeof(xmlExpNodePtr));
5555 if (dict == NULL) {
5556 ret->dict = xmlDictCreate();
5557 if (ret->dict == NULL) {
5558 xmlFree(ret->table);
5559 xmlFree(ret);
5560 return(NULL);
5561 }
5562 } else {
5563 ret->dict = dict;
5564 xmlDictReference(ret->dict);
5565 }
5566 return(ret);
5567}
5568
5569/**
5570 * xmlExpFreeCtxt:
5571 * @ctxt: an expression context
5572 *
5573 * Free an expression context
5574 */
5575void
5576xmlExpFreeCtxt(xmlExpCtxtPtr ctxt) {
5577 if (ctxt == NULL)
5578 return;
5579 xmlDictFree(ctxt->dict);
5580 if (ctxt->table != NULL)
5581 xmlFree(ctxt->table);
5582 xmlFree(ctxt);
5583}
5584
5585/************************************************************************
5586 * *
5587 * Structure associated to an expression node *
5588 * *
5589 ************************************************************************/
Daniel Veillard465a0002005-08-22 12:07:04 +00005590#define MAX_NODES 10000
5591
5592/* #define DEBUG_DERIV */
5593
5594/*
5595 * TODO:
5596 * - Wildcards
5597 * - public API for creation
5598 *
5599 * Started
5600 * - regression testing
5601 *
5602 * Done
5603 * - split into module and test tool
5604 * - memleaks
5605 */
Daniel Veillard81a8ec62005-08-22 00:20:58 +00005606
5607typedef enum {
5608 XML_EXP_NILABLE = (1 << 0)
5609} xmlExpNodeInfo;
5610
5611#define IS_NILLABLE(node) ((node)->info & XML_EXP_NILABLE)
5612
5613struct _xmlExpNode {
5614 unsigned char type;/* xmlExpNodeType */
5615 unsigned char info;/* OR of xmlExpNodeInfo */
5616 unsigned short key; /* the hash key */
5617 unsigned int ref; /* The number of references */
5618 int c_max; /* the maximum length it can consume */
5619 xmlExpNodePtr exp_left;
5620 xmlExpNodePtr next;/* the next node in the hash table or free list */
5621 union {
5622 struct {
5623 int f_min;
5624 int f_max;
5625 } count;
5626 struct {
5627 xmlExpNodePtr f_right;
5628 } children;
5629 const xmlChar *f_str;
5630 } field;
5631};
5632
5633#define exp_min field.count.f_min
5634#define exp_max field.count.f_max
5635/* #define exp_left field.children.f_left */
5636#define exp_right field.children.f_right
5637#define exp_str field.f_str
5638
5639static xmlExpNodePtr xmlExpNewNode(xmlExpCtxtPtr ctxt, xmlExpNodeType type);
5640static xmlExpNode forbiddenExpNode = {
5641 XML_EXP_FORBID, 0, 0, 0, 0, NULL, NULL, {{ 0, 0}}
5642};
5643xmlExpNodePtr forbiddenExp = &forbiddenExpNode;
5644static xmlExpNode emptyExpNode = {
5645 XML_EXP_EMPTY, 1, 0, 0, 0, NULL, NULL, {{ 0, 0}}
5646};
5647xmlExpNodePtr emptyExp = &emptyExpNode;
5648
5649/************************************************************************
5650 * *
5651 * The custom hash table for unicity and canonicalization *
5652 * of sub-expressions pointers *
5653 * *
5654 ************************************************************************/
5655/*
5656 * xmlExpHashNameComputeKey:
5657 * Calculate the hash key for a token
5658 */
5659static unsigned short
5660xmlExpHashNameComputeKey(const xmlChar *name) {
5661 unsigned short value = 0L;
5662 char ch;
5663
5664 if (name != NULL) {
5665 value += 30 * (*name);
5666 while ((ch = *name++) != 0) {
5667 value = value ^ ((value << 5) + (value >> 3) + (unsigned long)ch);
5668 }
5669 }
5670 return (value);
5671}
5672
5673/*
5674 * xmlExpHashComputeKey:
5675 * Calculate the hash key for a compound expression
5676 */
5677static unsigned short
5678xmlExpHashComputeKey(xmlExpNodeType type, xmlExpNodePtr left,
5679 xmlExpNodePtr right) {
5680 unsigned long value;
5681 unsigned short ret;
5682
5683 switch (type) {
5684 case XML_EXP_SEQ:
5685 value = left->key;
5686 value += right->key;
5687 value *= 3;
5688 ret = (unsigned short) value;
5689 break;
5690 case XML_EXP_OR:
5691 value = left->key;
5692 value += right->key;
5693 value *= 7;
5694 ret = (unsigned short) value;
5695 break;
5696 case XML_EXP_COUNT:
5697 value = left->key;
5698 value += right->key;
5699 ret = (unsigned short) value;
5700 break;
5701 default:
5702 ret = 0;
5703 }
5704 return(ret);
5705}
5706
5707
5708static xmlExpNodePtr
5709xmlExpNewNode(xmlExpCtxtPtr ctxt, xmlExpNodeType type) {
5710 xmlExpNodePtr ret;
5711
5712 if (ctxt->nb_nodes >= MAX_NODES)
5713 return(NULL);
5714 ret = (xmlExpNodePtr) xmlMalloc(sizeof(xmlExpNode));
5715 if (ret == NULL)
5716 return(NULL);
5717 memset(ret, 0, sizeof(xmlExpNode));
5718 ret->type = type;
5719 ret->next = NULL;
5720 ctxt->nb_nodes++;
5721 ctxt->nb_cons++;
5722 return(ret);
5723}
5724
5725/**
5726 * xmlExpHashGetEntry:
5727 * @table: the hash table
5728 *
5729 * Get the unique entry from the hash table. The entry is created if
5730 * needed. @left and @right are consumed, i.e. their ref count will
5731 * be decremented by the operation.
5732 *
5733 * Returns the pointer or NULL in case of error
5734 */
5735static xmlExpNodePtr
5736xmlExpHashGetEntry(xmlExpCtxtPtr ctxt, xmlExpNodeType type,
5737 xmlExpNodePtr left, xmlExpNodePtr right,
5738 const xmlChar *name, int min, int max) {
5739 unsigned short kbase, key;
5740 xmlExpNodePtr entry;
5741 xmlExpNodePtr insert;
5742
5743 if (ctxt == NULL)
5744 return(NULL);
5745
5746 /*
5747 * Check for duplicate and insertion location.
5748 */
5749 if (type == XML_EXP_ATOM) {
5750 kbase = xmlExpHashNameComputeKey(name);
5751 } else if (type == XML_EXP_COUNT) {
5752 /* COUNT reduction rule 1 */
5753 /* a{1} -> a */
5754 if (min == max) {
5755 if (min == 1) {
5756 return(left);
5757 }
5758 if (min == 0) {
5759 xmlExpFree(ctxt, left);
5760 return(emptyExp);
5761 }
5762 }
5763 if (min < 0) {
5764 xmlExpFree(ctxt, left);
5765 return(forbiddenExp);
5766 }
5767 if (max == -1)
5768 kbase = min + 79;
5769 else
5770 kbase = max - min;
5771 kbase += left->key;
5772 } else if (type == XML_EXP_OR) {
5773 /* Forbid reduction rules */
5774 if (left->type == XML_EXP_FORBID) {
5775 xmlExpFree(ctxt, left);
5776 return(right);
5777 }
5778 if (right->type == XML_EXP_FORBID) {
5779 xmlExpFree(ctxt, right);
5780 return(left);
5781 }
5782
5783 /* OR reduction rule 1 */
5784 /* a | a reduced to a */
5785 if (left == right) {
5786 left->ref--;
5787 return(left);
5788 }
5789 /* OR canonicalization rule 1 */
5790 /* linearize (a | b) | c into a | (b | c) */
5791 if ((left->type == XML_EXP_OR) && (right->type != XML_EXP_OR)) {
5792 xmlExpNodePtr tmp = left;
5793 left = right;
5794 right = tmp;
5795 }
5796 /* OR reduction rule 2 */
5797 /* a | (a | b) and b | (a | b) are reduced to a | b */
5798 if (right->type == XML_EXP_OR) {
5799 if ((left == right->exp_left) ||
5800 (left == right->exp_right)) {
5801 xmlExpFree(ctxt, left);
5802 return(right);
5803 }
5804 }
5805 /* OR canonicalization rule 2 */
5806 /* linearize (a | b) | c into a | (b | c) */
5807 if (left->type == XML_EXP_OR) {
5808 xmlExpNodePtr tmp;
5809
5810 /* OR canonicalization rule 2 */
5811 if ((left->exp_right->type != XML_EXP_OR) &&
5812 (left->exp_right->key < left->exp_left->key)) {
5813 tmp = left->exp_right;
5814 left->exp_right = left->exp_left;
5815 left->exp_left = tmp;
5816 }
5817 left->exp_right->ref++;
5818 tmp = xmlExpHashGetEntry(ctxt, XML_EXP_OR, left->exp_right, right,
5819 NULL, 0, 0);
5820 left->exp_left->ref++;
5821 tmp = xmlExpHashGetEntry(ctxt, XML_EXP_OR, left->exp_left, tmp,
5822 NULL, 0, 0);
5823
5824 xmlExpFree(ctxt, left);
5825 return(tmp);
5826 }
5827 if (right->type == XML_EXP_OR) {
5828 /* Ordering in the tree */
5829 /* C | (A | B) -> A | (B | C) */
5830 if (left->key > right->exp_right->key) {
5831 xmlExpNodePtr tmp;
5832 right->exp_right->ref++;
5833 tmp = xmlExpHashGetEntry(ctxt, XML_EXP_OR, right->exp_right,
5834 left, NULL, 0, 0);
5835 right->exp_left->ref++;
5836 tmp = xmlExpHashGetEntry(ctxt, XML_EXP_OR, right->exp_left,
5837 tmp, NULL, 0, 0);
5838 xmlExpFree(ctxt, right);
5839 return(tmp);
5840 }
5841 /* Ordering in the tree */
5842 /* B | (A | C) -> A | (B | C) */
5843 if (left->key > right->exp_left->key) {
5844 xmlExpNodePtr tmp;
5845 right->exp_right->ref++;
5846 tmp = xmlExpHashGetEntry(ctxt, XML_EXP_OR, left,
5847 right->exp_right, NULL, 0, 0);
5848 right->exp_left->ref++;
5849 tmp = xmlExpHashGetEntry(ctxt, XML_EXP_OR, right->exp_left,
5850 tmp, NULL, 0, 0);
5851 xmlExpFree(ctxt, right);
5852 return(tmp);
5853 }
5854 }
5855 /* we know both types are != XML_EXP_OR here */
5856 else if (left->key > right->key) {
5857 xmlExpNodePtr tmp = left;
5858 left = right;
5859 right = tmp;
5860 }
5861 kbase = xmlExpHashComputeKey(type, left, right);
5862 } else if (type == XML_EXP_SEQ) {
5863 /* Forbid reduction rules */
5864 if (left->type == XML_EXP_FORBID) {
5865 xmlExpFree(ctxt, right);
5866 return(left);
5867 }
5868 if (right->type == XML_EXP_FORBID) {
5869 xmlExpFree(ctxt, left);
5870 return(right);
5871 }
5872 /* Empty reduction rules */
5873 if (right->type == XML_EXP_EMPTY) {
5874 return(left);
5875 }
5876 if (left->type == XML_EXP_EMPTY) {
5877 return(right);
5878 }
5879 kbase = xmlExpHashComputeKey(type, left, right);
5880 } else
5881 return(NULL);
5882
5883 key = kbase % ctxt->size;
5884 if (ctxt->table[key] != NULL) {
5885 for (insert = ctxt->table[key]; insert != NULL;
5886 insert = insert->next) {
5887 if ((insert->key == kbase) &&
5888 (insert->type == type)) {
5889 if (type == XML_EXP_ATOM) {
5890 if (name == insert->exp_str) {
5891 insert->ref++;
5892 return(insert);
5893 }
5894 } else if (type == XML_EXP_COUNT) {
5895 if ((insert->exp_min == min) && (insert->exp_max == max) &&
5896 (insert->exp_left == left)) {
5897 insert->ref++;
5898 left->ref--;
5899 return(insert);
5900 }
5901 } else if ((insert->exp_left == left) &&
5902 (insert->exp_right == right)) {
5903 insert->ref++;
5904 left->ref--;
5905 right->ref--;
5906 return(insert);
5907 }
5908 }
5909 }
5910 }
5911
5912 entry = xmlExpNewNode(ctxt, type);
5913 if (entry == NULL)
5914 return(NULL);
5915 entry->key = kbase;
5916 if (type == XML_EXP_ATOM) {
5917 entry->exp_str = name;
5918 entry->c_max = 1;
5919 } else if (type == XML_EXP_COUNT) {
5920 entry->exp_min = min;
5921 entry->exp_max = max;
5922 entry->exp_left = left;
5923 if ((min == 0) || (IS_NILLABLE(left)))
5924 entry->info |= XML_EXP_NILABLE;
5925 if (max < 0)
5926 entry->c_max = -1;
5927 else
5928 entry->c_max = max * entry->exp_left->c_max;
5929 } else {
5930 entry->exp_left = left;
5931 entry->exp_right = right;
5932 if (type == XML_EXP_OR) {
5933 if ((IS_NILLABLE(left)) || (IS_NILLABLE(right)))
5934 entry->info |= XML_EXP_NILABLE;
5935 if ((entry->exp_left->c_max == -1) ||
5936 (entry->exp_right->c_max == -1))
5937 entry->c_max = -1;
5938 else if (entry->exp_left->c_max > entry->exp_right->c_max)
5939 entry->c_max = entry->exp_left->c_max;
5940 else
5941 entry->c_max = entry->exp_right->c_max;
5942 } else {
5943 if ((IS_NILLABLE(left)) && (IS_NILLABLE(right)))
5944 entry->info |= XML_EXP_NILABLE;
5945 if ((entry->exp_left->c_max == -1) ||
5946 (entry->exp_right->c_max == -1))
5947 entry->c_max = -1;
5948 else
5949 entry->c_max = entry->exp_left->c_max + entry->exp_right->c_max;
5950 }
5951 }
5952 entry->ref = 1;
5953 if (ctxt->table[key] != NULL)
5954 entry->next = ctxt->table[key];
5955
5956 ctxt->table[key] = entry;
5957 ctxt->nbElems++;
5958
5959 return(entry);
5960}
5961
5962/**
5963 * xmlExpFree:
5964 * @ctxt: the expression context
5965 * @exp: the expression
5966 *
5967 * Dereference the expression
5968 */
5969void
5970xmlExpFree(xmlExpCtxtPtr ctxt, xmlExpNodePtr exp) {
5971 if ((exp == NULL) || (exp == forbiddenExp) || (exp == emptyExp))
5972 return;
5973 exp->ref--;
Daniel Veillard81a8ec62005-08-22 00:20:58 +00005974 if (exp->ref == 0) {
5975 unsigned short key;
5976
5977 /* Unlink it first from the hash table */
5978 key = exp->key % ctxt->size;
5979 if (ctxt->table[key] == exp) {
5980 ctxt->table[key] = exp->next;
5981 } else {
5982 xmlExpNodePtr tmp;
5983
5984 tmp = ctxt->table[key];
5985 while (tmp != NULL) {
5986 if (tmp->next == exp) {
5987 tmp->next = exp->next;
5988 break;
5989 }
5990 tmp = tmp->next;
5991 }
5992 }
5993
5994 if ((exp->type == XML_EXP_SEQ) || (exp->type == XML_EXP_OR)) {
5995 xmlExpFree(ctxt, exp->exp_left);
5996 xmlExpFree(ctxt, exp->exp_right);
5997 } else if (exp->type == XML_EXP_COUNT) {
5998 xmlExpFree(ctxt, exp->exp_left);
5999 }
6000 xmlFree(exp);
Daniel Veillard81a8ec62005-08-22 00:20:58 +00006001 ctxt->nb_nodes--;
6002 }
6003}
6004
6005/**
6006 * xmlExpRef:
6007 * @exp: the expression
6008 *
6009 * Increase the reference count of the expression
6010 */
6011void
6012xmlExpRef(xmlExpNodePtr exp) {
6013 if (exp != NULL)
6014 exp->ref++;
6015}
6016
Daniel Veillardccb4d412005-08-23 13:41:17 +00006017/**
6018 * xmlExpNewAtom:
6019 * @ctxt: the expression context
6020 * @name: the atom name
6021 * @len: the atom name lenght in byte (or -1);
6022 *
6023 * Get the atom associated to this name from that context
6024 *
6025 * Returns the node or NULL in case of error
6026 */
6027xmlExpNodePtr
6028xmlExpNewAtom(xmlExpCtxtPtr ctxt, const xmlChar *name, int len) {
6029 if ((ctxt == NULL) || (name == NULL))
6030 return(NULL);
6031 name = xmlDictLookup(ctxt->dict, name, len);
6032 if (name == NULL)
6033 return(NULL);
6034 return(xmlExpHashGetEntry(ctxt, XML_EXP_ATOM, NULL, NULL, name, 0, 0));
6035}
6036
6037/**
6038 * xmlExpNewOr:
6039 * @ctxt: the expression context
6040 * @left: left expression
6041 * @right: right expression
6042 *
6043 * Get the atom associated to the choice @left | @right
6044 * Note that @left and @right are consumed in the operation, to keep
6045 * an handle on them use xmlExpRef() and use xmlExpFree() to release them,
6046 * this is true even in case of failure (unless ctxt == NULL).
6047 *
6048 * Returns the node or NULL in case of error
6049 */
6050xmlExpNodePtr
6051xmlExpNewOr(xmlExpCtxtPtr ctxt, xmlExpNodePtr left, xmlExpNodePtr right) {
6052 if ((ctxt == NULL) || (left == NULL) || (right == NULL)) {
6053 xmlExpFree(ctxt, left);
6054 xmlExpFree(ctxt, right);
6055 return(NULL);
6056 }
6057 return(xmlExpHashGetEntry(ctxt, XML_EXP_OR, left, right, NULL, 0, 0));
6058}
6059
6060/**
6061 * xmlExpNewSeq:
6062 * @ctxt: the expression context
6063 * @left: left expression
6064 * @right: right expression
6065 *
6066 * Get the atom associated to the sequence @left , @right
6067 * Note that @left and @right are consumed in the operation, to keep
6068 * an handle on them use xmlExpRef() and use xmlExpFree() to release them,
6069 * this is true even in case of failure (unless ctxt == NULL).
6070 *
6071 * Returns the node or NULL in case of error
6072 */
6073xmlExpNodePtr
6074xmlExpNewSeq(xmlExpCtxtPtr ctxt, xmlExpNodePtr left, xmlExpNodePtr right) {
6075 if ((ctxt == NULL) || (left == NULL) || (right == NULL)) {
6076 xmlExpFree(ctxt, left);
6077 xmlExpFree(ctxt, right);
6078 return(NULL);
6079 }
6080 return(xmlExpHashGetEntry(ctxt, XML_EXP_SEQ, left, right, NULL, 0, 0));
6081}
6082
6083/**
6084 * xmlExpNewRange:
6085 * @ctxt: the expression context
6086 * @subset: the expression to be repeated
6087 * @min: the lower bound for the repetition
6088 * @max: the upper bound for the repetition, -1 means infinite
6089 *
6090 * Get the atom associated to the range (@subset){@min, @max}
6091 * Note that @subset is consumed in the operation, to keep
6092 * an handle on it use xmlExpRef() and use xmlExpFree() to release it,
6093 * this is true even in case of failure (unless ctxt == NULL).
6094 *
6095 * Returns the node or NULL in case of error
6096 */
6097xmlExpNodePtr
6098xmlExpNewRange(xmlExpCtxtPtr ctxt, xmlExpNodePtr subset, int min, int max) {
6099 if ((ctxt == NULL) || (subset == NULL) || (min < 0) || (max < -1) ||
6100 ((max >= 0) && (min > max))) {
6101 xmlExpFree(ctxt, subset);
6102 return(NULL);
6103 }
6104 return(xmlExpHashGetEntry(ctxt, XML_EXP_COUNT, subset,
6105 NULL, NULL, min, max));
6106}
6107
Daniel Veillard81a8ec62005-08-22 00:20:58 +00006108/************************************************************************
6109 * *
6110 * Public API for operations on expressions *
6111 * *
6112 ************************************************************************/
6113
6114static int
6115xmlExpGetLanguageInt(xmlExpCtxtPtr ctxt, xmlExpNodePtr exp,
6116 const xmlChar**list, int len, int nb) {
6117 int tmp, tmp2;
6118tail:
6119 switch (exp->type) {
6120 case XML_EXP_EMPTY:
6121 return(0);
6122 case XML_EXP_ATOM:
6123 for (tmp = 0;tmp < nb;tmp++)
6124 if (list[tmp] == exp->exp_str)
6125 return(0);
6126 if (nb >= len)
6127 return(-2);
6128 list[nb++] = exp->exp_str;
6129 return(1);
6130 case XML_EXP_COUNT:
6131 exp = exp->exp_left;
6132 goto tail;
6133 case XML_EXP_SEQ:
6134 case XML_EXP_OR:
6135 tmp = xmlExpGetLanguageInt(ctxt, exp->exp_left, list, len, nb);
6136 if (tmp < 0)
6137 return(tmp);
6138 tmp2 = xmlExpGetLanguageInt(ctxt, exp->exp_right, list, len,
6139 nb + tmp);
6140 if (tmp2 < 0)
6141 return(tmp2);
6142 return(tmp + tmp2);
6143 }
6144 return(-1);
6145}
6146
6147/**
6148 * xmlExpGetLanguage:
6149 * @ctxt: the expression context
6150 * @exp: the expression
6151 * @list: where to store the tokens
6152 * @len: the allocated lenght of @list
6153 *
6154 * Find all the strings used in @exp and store them in @list
6155 *
6156 * Returns the number of unique strings found, -1 in case of errors and
6157 * -2 if there is more than @len strings
6158 */
6159int
6160xmlExpGetLanguage(xmlExpCtxtPtr ctxt, xmlExpNodePtr exp,
6161 const xmlChar**list, int len) {
6162 if ((ctxt == NULL) || (exp == NULL) || (list == NULL) || (len <= 0))
6163 return(-1);
6164 return(xmlExpGetLanguageInt(ctxt, exp, list, len, 0));
6165}
6166
6167static int
6168xmlExpGetStartInt(xmlExpCtxtPtr ctxt, xmlExpNodePtr exp,
6169 const xmlChar**list, int len, int nb) {
6170 int tmp, tmp2;
6171tail:
6172 switch (exp->type) {
6173 case XML_EXP_FORBID:
6174 return(0);
6175 case XML_EXP_EMPTY:
6176 return(0);
6177 case XML_EXP_ATOM:
6178 for (tmp = 0;tmp < nb;tmp++)
6179 if (list[tmp] == exp->exp_str)
6180 return(0);
6181 if (nb >= len)
6182 return(-2);
6183 list[nb++] = exp->exp_str;
6184 return(1);
6185 case XML_EXP_COUNT:
6186 exp = exp->exp_left;
6187 goto tail;
6188 case XML_EXP_SEQ:
6189 tmp = xmlExpGetStartInt(ctxt, exp->exp_left, list, len, nb);
6190 if (tmp < 0)
6191 return(tmp);
6192 if (IS_NILLABLE(exp->exp_left)) {
6193 tmp2 = xmlExpGetStartInt(ctxt, exp->exp_right, list, len,
6194 nb + tmp);
6195 if (tmp2 < 0)
6196 return(tmp2);
6197 tmp += tmp2;
6198 }
6199 return(tmp);
6200 case XML_EXP_OR:
6201 tmp = xmlExpGetStartInt(ctxt, exp->exp_left, list, len, nb);
6202 if (tmp < 0)
6203 return(tmp);
6204 tmp2 = xmlExpGetStartInt(ctxt, exp->exp_right, list, len,
6205 nb + tmp);
6206 if (tmp2 < 0)
6207 return(tmp2);
6208 return(tmp + tmp2);
6209 }
6210 return(-1);
6211}
6212
6213/**
6214 * xmlExpGetStart:
6215 * @ctxt: the expression context
6216 * @exp: the expression
6217 * @list: where to store the tokens
6218 * @len: the allocated lenght of @list
6219 *
6220 * Find all the strings that appears at the start of the languages
6221 * accepted by @exp and store them in @list. E.g. for (a, b) | c
6222 * it will return the list [a, c]
6223 *
6224 * Returns the number of unique strings found, -1 in case of errors and
6225 * -2 if there is more than @len strings
6226 */
6227int
6228xmlExpGetStart(xmlExpCtxtPtr ctxt, xmlExpNodePtr exp,
6229 const xmlChar**list, int len) {
6230 if ((ctxt == NULL) || (exp == NULL) || (list == NULL) || (len <= 0))
6231 return(-1);
6232 return(xmlExpGetStartInt(ctxt, exp, list, len, 0));
6233}
6234
6235/**
6236 * xmlExpIsNillable:
6237 * @exp: the expression
6238 *
6239 * Finds if the expression is nillable, i.e. if it accepts the empty sequqnce
6240 *
6241 * Returns 1 if nillable, 0 if not and -1 in case of error
6242 */
6243int
6244xmlExpIsNillable(xmlExpNodePtr exp) {
6245 if (exp == NULL)
6246 return(-1);
6247 return(IS_NILLABLE(exp) != 0);
6248}
6249
6250static xmlExpNodePtr
6251xmlExpStringDeriveInt(xmlExpCtxtPtr ctxt, xmlExpNodePtr exp, const xmlChar *str)
6252{
6253 xmlExpNodePtr ret;
6254
6255 switch (exp->type) {
6256 case XML_EXP_EMPTY:
6257 return(forbiddenExp);
6258 case XML_EXP_FORBID:
6259 return(forbiddenExp);
6260 case XML_EXP_ATOM:
6261 if (exp->exp_str == str) {
6262#ifdef DEBUG_DERIV
6263 printf("deriv atom: equal => Empty\n");
6264#endif
6265 ret = emptyExp;
6266 } else {
6267#ifdef DEBUG_DERIV
6268 printf("deriv atom: mismatch => forbid\n");
6269#endif
6270 /* TODO wildcards here */
6271 ret = forbiddenExp;
6272 }
6273 return(ret);
6274 case XML_EXP_OR: {
6275 xmlExpNodePtr tmp;
6276
6277#ifdef DEBUG_DERIV
6278 printf("deriv or: => or(derivs)\n");
6279#endif
6280 tmp = xmlExpStringDeriveInt(ctxt, exp->exp_left, str);
6281 if (tmp == NULL) {
6282 return(NULL);
6283 }
6284 ret = xmlExpStringDeriveInt(ctxt, exp->exp_right, str);
6285 if (ret == NULL) {
6286 xmlExpFree(ctxt, tmp);
6287 return(NULL);
6288 }
6289 ret = xmlExpHashGetEntry(ctxt, XML_EXP_OR, tmp, ret,
6290 NULL, 0, 0);
6291 return(ret);
6292 }
6293 case XML_EXP_SEQ:
6294#ifdef DEBUG_DERIV
6295 printf("deriv seq: starting with left\n");
6296#endif
6297 ret = xmlExpStringDeriveInt(ctxt, exp->exp_left, str);
6298 if (ret == NULL) {
6299 return(NULL);
6300 } else if (ret == forbiddenExp) {
6301 if (IS_NILLABLE(exp->exp_left)) {
6302#ifdef DEBUG_DERIV
6303 printf("deriv seq: left failed but nillable\n");
6304#endif
6305 ret = xmlExpStringDeriveInt(ctxt, exp->exp_right, str);
6306 }
6307 } else {
6308#ifdef DEBUG_DERIV
6309 printf("deriv seq: left match => sequence\n");
6310#endif
6311 exp->exp_right->ref++;
6312 ret = xmlExpHashGetEntry(ctxt, XML_EXP_SEQ, ret, exp->exp_right,
6313 NULL, 0, 0);
6314 }
6315 return(ret);
6316 case XML_EXP_COUNT: {
6317 int min, max;
6318 xmlExpNodePtr tmp;
6319
6320 if (exp->exp_max == 0)
6321 return(forbiddenExp);
6322 ret = xmlExpStringDeriveInt(ctxt, exp->exp_left, str);
6323 if (ret == NULL)
6324 return(NULL);
6325 if (ret == forbiddenExp) {
6326#ifdef DEBUG_DERIV
6327 printf("deriv count: pattern mismatch => forbid\n");
6328#endif
6329 return(ret);
6330 }
6331 if (exp->exp_max == 1)
6332 return(ret);
6333 if (exp->exp_max < 0) /* unbounded */
6334 max = -1;
6335 else
6336 max = exp->exp_max - 1;
6337 if (exp->exp_min > 0)
6338 min = exp->exp_min - 1;
6339 else
6340 min = 0;
6341 exp->exp_left->ref++;
6342 tmp = xmlExpHashGetEntry(ctxt, XML_EXP_COUNT, exp->exp_left, NULL,
6343 NULL, min, max);
6344 if (ret == emptyExp) {
6345#ifdef DEBUG_DERIV
6346 printf("deriv count: match to empty => new count\n");
6347#endif
6348 return(tmp);
6349 }
6350#ifdef DEBUG_DERIV
6351 printf("deriv count: match => sequence with new count\n");
6352#endif
6353 return(xmlExpHashGetEntry(ctxt, XML_EXP_SEQ, ret, tmp,
6354 NULL, 0, 0));
6355 }
6356 }
6357 return(NULL);
6358}
6359
6360/**
6361 * xmlExpStringDerive:
6362 * @ctxt: the expression context
6363 * @exp: the expression
6364 * @str: the string
6365 * @len: the string len in bytes if available
6366 *
6367 * Do one step of Brzozowski derivation of the expression @exp with
6368 * respect to the input string
6369 *
6370 * Returns the resulting expression or NULL in case of internal error
6371 */
6372xmlExpNodePtr
6373xmlExpStringDerive(xmlExpCtxtPtr ctxt, xmlExpNodePtr exp,
6374 const xmlChar *str, int len) {
6375 const xmlChar *input;
6376
6377 if ((exp == NULL) || (ctxt == NULL) || (str == NULL)) {
6378 return(NULL);
6379 }
6380 /*
6381 * check the string is in the dictionnary, if yes use an interned
6382 * copy, otherwise we know it's not an acceptable input
6383 */
6384 input = xmlDictExists(ctxt->dict, str, len);
6385 if (input == NULL) {
6386 return(forbiddenExp);
6387 }
6388 return(xmlExpStringDeriveInt(ctxt, exp, input));
6389}
6390
6391static int
6392xmlExpCheckCard(xmlExpNodePtr exp, xmlExpNodePtr sub) {
6393 int ret = 1;
6394
6395 if (sub->c_max == -1) {
6396 if (exp->c_max != -1)
6397 ret = 0;
6398 } else if ((exp->c_max >= 0) && (exp->c_max < sub->c_max)) {
6399 ret = 0;
6400 }
6401#if 0
6402 if ((IS_NILLABLE(sub)) && (!IS_NILLABLE(exp)))
6403 ret = 0;
6404#endif
6405 return(ret);
6406}
6407
6408static xmlExpNodePtr xmlExpExpDeriveInt(xmlExpCtxtPtr ctxt, xmlExpNodePtr exp,
6409 xmlExpNodePtr sub);
6410/**
6411 * xmlExpDivide:
6412 * @ctxt: the expressions context
6413 * @exp: the englobing expression
6414 * @sub: the subexpression
6415 * @mult: the multiple expression
6416 * @remain: the remain from the derivation of the multiple
6417 *
6418 * Check if exp is a multiple of sub, i.e. if there is a finite number n
6419 * so that sub{n} subsume exp
6420 *
6421 * Returns the multiple value if successful, 0 if it is not a multiple
6422 * and -1 in case of internel error.
6423 */
6424
6425static int
6426xmlExpDivide(xmlExpCtxtPtr ctxt, xmlExpNodePtr exp, xmlExpNodePtr sub,
6427 xmlExpNodePtr *mult, xmlExpNodePtr *remain) {
6428 int i;
6429 xmlExpNodePtr tmp, tmp2;
6430
6431 if (mult != NULL) *mult = NULL;
6432 if (remain != NULL) *remain = NULL;
6433 if (exp->c_max == -1) return(0);
6434 if (IS_NILLABLE(exp) && (!IS_NILLABLE(sub))) return(0);
6435
6436 for (i = 1;i <= exp->c_max;i++) {
6437 sub->ref++;
6438 tmp = xmlExpHashGetEntry(ctxt, XML_EXP_COUNT,
6439 sub, NULL, NULL, i, i);
6440 if (tmp == NULL) {
6441 return(-1);
6442 }
6443 if (!xmlExpCheckCard(tmp, exp)) {
6444 xmlExpFree(ctxt, tmp);
6445 continue;
6446 }
6447 tmp2 = xmlExpExpDeriveInt(ctxt, tmp, exp);
6448 if (tmp2 == NULL) {
6449 xmlExpFree(ctxt, tmp);
6450 return(-1);
6451 }
6452 if ((tmp2 != forbiddenExp) && (IS_NILLABLE(tmp2))) {
6453 if (remain != NULL)
6454 *remain = tmp2;
6455 else
6456 xmlExpFree(ctxt, tmp2);
6457 if (mult != NULL)
6458 *mult = tmp;
6459 else
6460 xmlExpFree(ctxt, tmp);
6461#ifdef DEBUG_DERIV
6462 printf("Divide succeeded %d\n", i);
6463#endif
6464 return(i);
6465 }
6466 xmlExpFree(ctxt, tmp);
6467 xmlExpFree(ctxt, tmp2);
6468 }
6469#ifdef DEBUG_DERIV
6470 printf("Divide failed\n");
6471#endif
6472 return(0);
6473}
6474
6475/**
6476 * xmlExpExpDeriveInt:
6477 * @ctxt: the expressions context
6478 * @exp: the englobing expression
6479 * @sub: the subexpression
6480 *
6481 * Try to do a step of Brzozowski derivation but at a higher level
6482 * the input being a subexpression.
6483 *
6484 * Returns the resulting expression or NULL in case of internal error
6485 */
6486static xmlExpNodePtr
6487xmlExpExpDeriveInt(xmlExpCtxtPtr ctxt, xmlExpNodePtr exp, xmlExpNodePtr sub) {
6488 xmlExpNodePtr ret, tmp, tmp2, tmp3;
6489 const xmlChar **tab;
6490 int len, i;
6491
6492 /*
6493 * In case of equality and if the expression can only consume a finite
6494 * amount, then the derivation is empty
6495 */
6496 if ((exp == sub) && (exp->c_max >= 0)) {
6497#ifdef DEBUG_DERIV
6498 printf("Equal(exp, sub) and finite -> Empty\n");
6499#endif
6500 return(emptyExp);
6501 }
6502 /*
6503 * decompose sub sequence first
6504 */
6505 if (sub->type == XML_EXP_EMPTY) {
6506#ifdef DEBUG_DERIV
6507 printf("Empty(sub) -> Empty\n");
6508#endif
6509 exp->ref++;
6510 return(exp);
6511 }
6512 if (sub->type == XML_EXP_SEQ) {
6513#ifdef DEBUG_DERIV
6514 printf("Seq(sub) -> decompose\n");
6515#endif
6516 tmp = xmlExpExpDeriveInt(ctxt, exp, sub->exp_left);
6517 if (tmp == NULL)
6518 return(NULL);
6519 if (tmp == forbiddenExp)
6520 return(tmp);
6521 ret = xmlExpExpDeriveInt(ctxt, tmp, sub->exp_right);
6522 xmlExpFree(ctxt, tmp);
6523 return(ret);
6524 }
6525 if (sub->type == XML_EXP_OR) {
6526#ifdef DEBUG_DERIV
6527 printf("Or(sub) -> decompose\n");
6528#endif
6529 tmp = xmlExpExpDeriveInt(ctxt, exp, sub->exp_left);
6530 if (tmp == forbiddenExp)
6531 return(tmp);
6532 if (tmp == NULL)
6533 return(NULL);
6534 ret = xmlExpExpDeriveInt(ctxt, exp, sub->exp_right);
6535 if ((ret == NULL) || (ret == forbiddenExp)) {
6536 xmlExpFree(ctxt, tmp);
6537 return(ret);
6538 }
6539 return(xmlExpHashGetEntry(ctxt, XML_EXP_OR, tmp, ret, NULL, 0, 0));
6540 }
6541 if (!xmlExpCheckCard(exp, sub)) {
6542#ifdef DEBUG_DERIV
6543 printf("CheckCard(exp, sub) failed -> Forbid\n");
6544#endif
6545 return(forbiddenExp);
6546 }
6547 switch (exp->type) {
6548 case XML_EXP_EMPTY:
6549 if (sub == emptyExp)
6550 return(emptyExp);
6551#ifdef DEBUG_DERIV
6552 printf("Empty(exp) -> Forbid\n");
6553#endif
6554 return(forbiddenExp);
6555 case XML_EXP_FORBID:
6556#ifdef DEBUG_DERIV
6557 printf("Forbid(exp) -> Forbid\n");
6558#endif
6559 return(forbiddenExp);
6560 case XML_EXP_ATOM:
6561 if (sub->type == XML_EXP_ATOM) {
6562 /* TODO: handle wildcards */
6563 if (exp->exp_str == sub->exp_str) {
6564#ifdef DEBUG_DERIV
6565 printf("Atom match -> Empty\n");
6566#endif
6567 return(emptyExp);
6568 }
6569#ifdef DEBUG_DERIV
6570 printf("Atom mismatch -> Forbid\n");
6571#endif
6572 return(forbiddenExp);
6573 }
6574 if ((sub->type == XML_EXP_COUNT) &&
6575 (sub->exp_max == 1) &&
6576 (sub->exp_left->type == XML_EXP_ATOM)) {
6577 /* TODO: handle wildcards */
6578 if (exp->exp_str == sub->exp_left->exp_str) {
6579#ifdef DEBUG_DERIV
6580 printf("Atom match -> Empty\n");
6581#endif
6582 return(emptyExp);
6583 }
6584#ifdef DEBUG_DERIV
6585 printf("Atom mismatch -> Forbid\n");
6586#endif
6587 return(forbiddenExp);
6588 }
6589#ifdef DEBUG_DERIV
6590 printf("Compex exp vs Atom -> Forbid\n");
6591#endif
6592 return(forbiddenExp);
6593 case XML_EXP_SEQ:
6594 /* try to get the sequence consumed only if possible */
6595 if (xmlExpCheckCard(exp->exp_left, sub)) {
6596 /* See if the sequence can be consumed directly */
6597#ifdef DEBUG_DERIV
6598 printf("Seq trying left only\n");
6599#endif
6600 ret = xmlExpExpDeriveInt(ctxt, exp->exp_left, sub);
6601 if ((ret != forbiddenExp) && (ret != NULL)) {
6602#ifdef DEBUG_DERIV
6603 printf("Seq trying left only worked\n");
6604#endif
6605 /*
6606 * TODO: assumption here that we are determinist
6607 * i.e. we won't get to a nillable exp left
6608 * subset which could be matched by the right
6609 * part too.
6610 * e.g.: (a | b)+,(a | c) and 'a+,a'
6611 */
6612 exp->exp_right->ref++;
6613 return(xmlExpHashGetEntry(ctxt, XML_EXP_SEQ, ret,
6614 exp->exp_right, NULL, 0, 0));
6615 }
6616#ifdef DEBUG_DERIV
6617 } else {
6618 printf("Seq: left too short\n");
6619#endif
6620 }
6621 /* Try instead to decompose */
6622 if (sub->type == XML_EXP_COUNT) {
6623 int min, max;
6624
6625#ifdef DEBUG_DERIV
6626 printf("Seq: sub is a count\n");
6627#endif
6628 ret = xmlExpExpDeriveInt(ctxt, exp->exp_left, sub->exp_left);
6629 if (ret == NULL)
6630 return(NULL);
6631 if (ret != forbiddenExp) {
6632#ifdef DEBUG_DERIV
6633 printf("Seq , Count match on left\n");
6634#endif
6635 if (sub->exp_max < 0)
6636 max = -1;
6637 else
6638 max = sub->exp_max -1;
6639 if (sub->exp_min > 0)
6640 min = sub->exp_min -1;
6641 else
6642 min = 0;
6643 exp->exp_right->ref++;
6644 tmp = xmlExpHashGetEntry(ctxt, XML_EXP_SEQ, ret,
6645 exp->exp_right, NULL, 0, 0);
6646 if (tmp == NULL)
6647 return(NULL);
6648
6649 sub->exp_left->ref++;
6650 tmp2 = xmlExpHashGetEntry(ctxt, XML_EXP_COUNT,
6651 sub->exp_left, NULL, NULL, min, max);
6652 if (tmp2 == NULL) {
6653 xmlExpFree(ctxt, tmp);
6654 return(NULL);
6655 }
6656 ret = xmlExpExpDeriveInt(ctxt, tmp, tmp2);
6657 xmlExpFree(ctxt, tmp);
6658 xmlExpFree(ctxt, tmp2);
6659 return(ret);
6660 }
6661 }
6662 /* we made no progress on structured operations */
6663 break;
6664 case XML_EXP_OR:
6665#ifdef DEBUG_DERIV
6666 printf("Or , trying both side\n");
6667#endif
6668 ret = xmlExpExpDeriveInt(ctxt, exp->exp_left, sub);
6669 if (ret == NULL)
6670 return(NULL);
6671 tmp = xmlExpExpDeriveInt(ctxt, exp->exp_right, sub);
6672 if (tmp == NULL) {
6673 xmlExpFree(ctxt, ret);
6674 return(NULL);
6675 }
6676 return(xmlExpHashGetEntry(ctxt, XML_EXP_OR, ret, tmp, NULL, 0, 0));
6677 case XML_EXP_COUNT: {
6678 int min, max;
6679
6680 if (sub->type == XML_EXP_COUNT) {
6681 /*
6682 * Try to see if the loop is completely subsumed
6683 */
6684 tmp = xmlExpExpDeriveInt(ctxt, exp->exp_left, sub->exp_left);
6685 if (tmp == NULL)
6686 return(NULL);
6687 if (tmp == forbiddenExp) {
6688 int mult;
6689
6690#ifdef DEBUG_DERIV
6691 printf("Count, Count inner don't subsume\n");
6692#endif
6693 mult = xmlExpDivide(ctxt, sub->exp_left, exp->exp_left,
6694 NULL, &tmp);
6695 if (mult <= 0) {
6696#ifdef DEBUG_DERIV
6697 printf("Count, Count not multiple => forbidden\n");
6698#endif
6699 return(forbiddenExp);
6700 }
6701 if (sub->exp_max == -1) {
6702 max = -1;
6703 if (exp->exp_max == -1) {
6704 if (exp->exp_min <= sub->exp_min * mult)
6705 min = 0;
6706 else
6707 min = exp->exp_min - sub->exp_min * mult;
6708 } else {
6709#ifdef DEBUG_DERIV
6710 printf("Count, Count finite can't subsume infinite\n");
6711#endif
6712 xmlExpFree(ctxt, tmp);
6713 return(forbiddenExp);
6714 }
6715 } else {
6716 if (exp->exp_max == -1) {
6717#ifdef DEBUG_DERIV
6718 printf("Infinite loop consume mult finite loop\n");
6719#endif
6720 if (exp->exp_min > sub->exp_min * mult) {
6721 max = -1;
6722 min = exp->exp_min - sub->exp_min * mult;
6723 } else {
6724 max = -1;
6725 min = 0;
6726 }
6727 } else {
6728 if (exp->exp_max < sub->exp_max * mult) {
6729#ifdef DEBUG_DERIV
6730 printf("loops max mult mismatch => forbidden\n");
6731#endif
6732 xmlExpFree(ctxt, tmp);
6733 return(forbiddenExp);
6734 }
6735 if (sub->exp_max * mult > exp->exp_min)
6736 min = 0;
6737 else
6738 min = exp->exp_min - sub->exp_max * mult;
6739 max = exp->exp_max - sub->exp_max * mult;
6740 }
6741 }
6742 } else if (!IS_NILLABLE(tmp)) {
6743 /*
6744 * TODO: loop here to try to grow if working on finite
6745 * blocks.
6746 */
6747#ifdef DEBUG_DERIV
6748 printf("Count, Count remain not nillable => forbidden\n");
6749#endif
6750 xmlExpFree(ctxt, tmp);
6751 return(forbiddenExp);
6752 } else if (sub->exp_max == -1) {
6753 if (exp->exp_max == -1) {
6754 if (exp->exp_min <= sub->exp_min) {
6755#ifdef DEBUG_DERIV
6756 printf("Infinite loops Okay => COUNT(0,Inf)\n");
6757#endif
6758 max = -1;
6759 min = 0;
6760 } else {
6761#ifdef DEBUG_DERIV
6762 printf("Infinite loops min => Count(X,Inf)\n");
6763#endif
6764 max = -1;
6765 min = exp->exp_min - sub->exp_min;
6766 }
6767 } else if (exp->exp_min > sub->exp_min) {
6768#ifdef DEBUG_DERIV
6769 printf("loops min mismatch 1 => forbidden ???\n");
6770#endif
6771 xmlExpFree(ctxt, tmp);
6772 return(forbiddenExp);
6773 } else {
6774 max = -1;
6775 min = 0;
6776 }
6777 } else {
6778 if (exp->exp_max == -1) {
6779#ifdef DEBUG_DERIV
6780 printf("Infinite loop consume finite loop\n");
6781#endif
6782 if (exp->exp_min > sub->exp_min) {
6783 max = -1;
6784 min = exp->exp_min - sub->exp_min;
6785 } else {
6786 max = -1;
6787 min = 0;
6788 }
6789 } else {
6790 if (exp->exp_max < sub->exp_max) {
6791#ifdef DEBUG_DERIV
6792 printf("loops max mismatch => forbidden\n");
6793#endif
6794 xmlExpFree(ctxt, tmp);
6795 return(forbiddenExp);
6796 }
6797 if (sub->exp_max > exp->exp_min)
6798 min = 0;
6799 else
6800 min = exp->exp_min - sub->exp_max;
6801 max = exp->exp_max - sub->exp_max;
6802 }
6803 }
6804#ifdef DEBUG_DERIV
6805 printf("loops match => SEQ(COUNT())\n");
6806#endif
6807 exp->exp_left->ref++;
6808 tmp2 = xmlExpHashGetEntry(ctxt, XML_EXP_COUNT, exp->exp_left,
6809 NULL, NULL, min, max);
6810 if (tmp2 == NULL) {
6811 return(NULL);
6812 }
6813 ret = xmlExpHashGetEntry(ctxt, XML_EXP_SEQ, tmp, tmp2,
6814 NULL, 0, 0);
6815 return(ret);
6816 }
6817 tmp = xmlExpExpDeriveInt(ctxt, exp->exp_left, sub);
6818 if (tmp == NULL)
6819 return(NULL);
6820 if (tmp == forbiddenExp) {
6821#ifdef DEBUG_DERIV
6822 printf("loop mismatch => forbidden\n");
6823#endif
6824 return(forbiddenExp);
6825 }
6826 if (exp->exp_min > 0)
6827 min = exp->exp_min - 1;
6828 else
6829 min = 0;
6830 if (exp->exp_max < 0)
6831 max = -1;
6832 else
6833 max = exp->exp_max - 1;
6834
6835#ifdef DEBUG_DERIV
6836 printf("loop match => SEQ(COUNT())\n");
6837#endif
6838 exp->exp_left->ref++;
6839 tmp2 = xmlExpHashGetEntry(ctxt, XML_EXP_COUNT, exp->exp_left,
6840 NULL, NULL, min, max);
6841 if (tmp2 == NULL)
6842 return(NULL);
6843 ret = xmlExpHashGetEntry(ctxt, XML_EXP_SEQ, tmp, tmp2,
6844 NULL, 0, 0);
6845 return(ret);
6846 }
6847 }
6848
Daniel Veillardccb4d412005-08-23 13:41:17 +00006849#ifdef DEBUG_DERIV
6850 printf("Fallback to derivative\n");
6851#endif
6852 if (IS_NILLABLE(sub)) {
6853 if (!(IS_NILLABLE(exp)))
6854 return(forbiddenExp);
6855 else
6856 ret = emptyExp;
6857 } else
6858 ret = NULL;
Daniel Veillard81a8ec62005-08-22 00:20:58 +00006859 /*
6860 * here the structured derivation made no progress so
6861 * we use the default token based derivation to force one more step
6862 */
6863 if (ctxt->tabSize == 0)
6864 ctxt->tabSize = 40;
6865
6866 tab = (const xmlChar **) xmlMalloc(ctxt->tabSize *
6867 sizeof(const xmlChar *));
6868 if (tab == NULL) {
6869 return(NULL);
6870 }
6871
Daniel Veillard81a8ec62005-08-22 00:20:58 +00006872 /*
6873 * collect all the strings accepted by the subexpression on input
6874 */
6875 len = xmlExpGetStartInt(ctxt, sub, tab, ctxt->tabSize, 0);
6876 while (len < 0) {
6877 const xmlChar **temp;
6878 temp = (const xmlChar **) xmlRealloc(tab, ctxt->tabSize * 2 *
6879 sizeof(const xmlChar *));
6880 if (temp == NULL) {
6881 xmlFree(tab);
6882 return(NULL);
6883 }
6884 tab = temp;
6885 ctxt->tabSize *= 2;
6886 len = xmlExpGetStartInt(ctxt, sub, tab, ctxt->tabSize, 0);
6887 }
Daniel Veillard81a8ec62005-08-22 00:20:58 +00006888 for (i = 0;i < len;i++) {
6889 tmp = xmlExpStringDeriveInt(ctxt, exp, tab[i]);
6890 if ((tmp == NULL) || (tmp == forbiddenExp)) {
6891 xmlExpFree(ctxt, ret);
6892 xmlFree(tab);
6893 return(tmp);
6894 }
6895 tmp2 = xmlExpStringDeriveInt(ctxt, sub, tab[i]);
6896 if ((tmp2 == NULL) || (tmp2 == forbiddenExp)) {
6897 xmlExpFree(ctxt, tmp);
6898 xmlExpFree(ctxt, ret);
6899 xmlFree(tab);
6900 return(tmp);
6901 }
6902 tmp3 = xmlExpExpDeriveInt(ctxt, tmp, tmp2);
6903 xmlExpFree(ctxt, tmp);
6904 xmlExpFree(ctxt, tmp2);
6905
6906 if ((tmp3 == NULL) || (tmp3 == forbiddenExp)) {
6907 xmlExpFree(ctxt, ret);
6908 xmlFree(tab);
6909 return(tmp3);
6910 }
6911
6912 if (ret == NULL)
6913 ret = tmp3;
6914 else {
6915 ret = xmlExpHashGetEntry(ctxt, XML_EXP_OR, ret, tmp3, NULL, 0, 0);
6916 if (ret == NULL) {
6917 xmlFree(tab);
6918 return(NULL);
6919 }
6920 }
6921 }
6922 xmlFree(tab);
6923 return(ret);
6924}
6925
6926/**
Daniel Veillard0090bd52005-08-22 14:43:43 +00006927 * xmlExpExpDerive:
6928 * @ctxt: the expressions context
6929 * @exp: the englobing expression
6930 * @sub: the subexpression
6931 *
6932 * Evaluates the expression resulting from @exp consuming a sub expression @sub
6933 * Based on algebraic derivation and sometimes direct Brzozowski derivation
6934 * it usually tatkes less than linear time and can handle expressions generating
6935 * infinite languages.
6936 *
6937 * Returns the resulting expression or NULL in case of internal error, the
6938 * result must be freed
6939 */
6940xmlExpNodePtr
6941xmlExpExpDerive(xmlExpCtxtPtr ctxt, xmlExpNodePtr exp, xmlExpNodePtr sub) {
6942 if ((exp == NULL) || (ctxt == NULL) || (sub == NULL))
6943 return(NULL);
6944
6945 /*
6946 * O(1) speedups
6947 */
6948 if (IS_NILLABLE(sub) && (!IS_NILLABLE(exp))) {
6949#ifdef DEBUG_DERIV
6950 printf("Sub nillable and not exp : can't subsume\n");
6951#endif
6952 return(forbiddenExp);
6953 }
6954 if (xmlExpCheckCard(exp, sub) == 0) {
6955#ifdef DEBUG_DERIV
6956 printf("sub generate longuer sequances than exp : can't subsume\n");
6957#endif
6958 return(forbiddenExp);
6959 }
6960 return(xmlExpExpDeriveInt(ctxt, exp, sub));
6961}
6962
6963/**
Daniel Veillard81a8ec62005-08-22 00:20:58 +00006964 * xmlExpSubsume:
6965 * @ctxt: the expressions context
6966 * @exp: the englobing expression
6967 * @sub: the subexpression
6968 *
6969 * Check whether @exp accepts all the languages accexpted by @sub
6970 * the input being a subexpression.
6971 *
6972 * Returns 1 if true 0 if false and -1 in case of failure.
6973 */
6974int
6975xmlExpSubsume(xmlExpCtxtPtr ctxt, xmlExpNodePtr exp, xmlExpNodePtr sub) {
6976 xmlExpNodePtr tmp;
6977
6978 if ((exp == NULL) || (ctxt == NULL) || (sub == NULL))
6979 return(-1);
6980
6981 /*
6982 * TODO: speedup by checking the language of sub is a subset of the
6983 * language of exp
6984 */
6985 /*
6986 * O(1) speedups
6987 */
6988 if (IS_NILLABLE(sub) && (!IS_NILLABLE(exp))) {
6989#ifdef DEBUG_DERIV
6990 printf("Sub nillable and not exp : can't subsume\n");
6991#endif
6992 return(0);
6993 }
6994 if (xmlExpCheckCard(exp, sub) == 0) {
6995#ifdef DEBUG_DERIV
6996 printf("sub generate longuer sequances than exp : can't subsume\n");
6997#endif
6998 return(0);
6999 }
7000 tmp = xmlExpExpDeriveInt(ctxt, exp, sub);
7001#ifdef DEBUG_DERIV
7002 printf("Result derivation :\n");
7003 PRINT_EXP(tmp);
7004#endif
7005 if (tmp == NULL)
7006 return(-1);
7007 if (tmp == forbiddenExp)
7008 return(0);
7009 if (tmp == emptyExp)
7010 return(1);
7011 if ((tmp != NULL) && (IS_NILLABLE(tmp))) {
7012 xmlExpFree(ctxt, tmp);
7013 return(1);
7014 }
7015 xmlExpFree(ctxt, tmp);
7016 return(0);
7017}
Daniel Veillard465a0002005-08-22 12:07:04 +00007018
7019/************************************************************************
7020 * *
7021 * Parsing expression *
7022 * *
7023 ************************************************************************/
7024
7025static xmlExpNodePtr xmlExpParseExpr(xmlExpCtxtPtr ctxt);
7026
7027#undef CUR
7028#define CUR (*ctxt->cur)
7029#undef NEXT
7030#define NEXT ctxt->cur++;
7031#undef IS_BLANK
7032#define IS_BLANK(c) ((c == ' ') || (c == '\n') || (c == '\r') || (c == '\t'))
7033#define SKIP_BLANKS while (IS_BLANK(*ctxt->cur)) ctxt->cur++;
7034
7035static int
7036xmlExpParseNumber(xmlExpCtxtPtr ctxt) {
7037 int ret = 0;
7038
7039 SKIP_BLANKS
7040 if (CUR == '*') {
7041 NEXT
7042 return(-1);
7043 }
7044 if ((CUR < '0') || (CUR > '9'))
7045 return(-1);
7046 while ((CUR >= '0') && (CUR <= '9')) {
7047 ret = ret * 10 + (CUR - '0');
7048 NEXT
7049 }
7050 return(ret);
7051}
7052
7053static xmlExpNodePtr
7054xmlExpParseOr(xmlExpCtxtPtr ctxt) {
7055 const char *base;
7056 xmlExpNodePtr ret;
7057 const xmlChar *val;
7058
7059 SKIP_BLANKS
7060 base = ctxt->cur;
7061 if (*ctxt->cur == '(') {
7062 NEXT
7063 ret = xmlExpParseExpr(ctxt);
7064 SKIP_BLANKS
7065 if (*ctxt->cur != ')') {
7066 fprintf(stderr, "unbalanced '(' : %s\n", base);
7067 xmlExpFree(ctxt, ret);
7068 return(NULL);
7069 }
7070 NEXT;
7071 SKIP_BLANKS
7072 goto parse_quantifier;
7073 }
7074 while ((CUR != 0) && (!(IS_BLANK(CUR))) && (CUR != '(') &&
7075 (CUR != ')') && (CUR != '|') && (CUR != ',') && (CUR != '{') &&
7076 (CUR != '*') && (CUR != '+') && (CUR != '?') && (CUR != '}'))
7077 NEXT;
7078 val = xmlDictLookup(ctxt->dict, BAD_CAST base, ctxt->cur - base);
7079 if (val == NULL)
7080 return(NULL);
7081 ret = xmlExpHashGetEntry(ctxt, XML_EXP_ATOM, NULL, NULL, val, 0, 0);
7082 if (ret == NULL)
7083 return(NULL);
7084 SKIP_BLANKS
7085parse_quantifier:
7086 if (CUR == '{') {
7087 int min, max;
7088
7089 NEXT
7090 min = xmlExpParseNumber(ctxt);
7091 if (min < 0) {
7092 xmlExpFree(ctxt, ret);
7093 return(NULL);
7094 }
7095 SKIP_BLANKS
7096 if (CUR == ',') {
7097 NEXT
7098 max = xmlExpParseNumber(ctxt);
7099 SKIP_BLANKS
7100 } else
7101 max = min;
7102 if (CUR != '}') {
7103 xmlExpFree(ctxt, ret);
7104 return(NULL);
7105 }
7106 NEXT
7107 ret = xmlExpHashGetEntry(ctxt, XML_EXP_COUNT, ret, NULL, NULL,
7108 min, max);
7109 SKIP_BLANKS
7110 } else if (CUR == '?') {
7111 NEXT
7112 ret = xmlExpHashGetEntry(ctxt, XML_EXP_COUNT, ret, NULL, NULL,
7113 0, 1);
7114 SKIP_BLANKS
7115 } else if (CUR == '+') {
7116 NEXT
7117 ret = xmlExpHashGetEntry(ctxt, XML_EXP_COUNT, ret, NULL, NULL,
7118 1, -1);
7119 SKIP_BLANKS
7120 } else if (CUR == '*') {
7121 NEXT
7122 ret = xmlExpHashGetEntry(ctxt, XML_EXP_COUNT, ret, NULL, NULL,
7123 0, -1);
7124 SKIP_BLANKS
7125 }
7126 return(ret);
7127}
7128
7129
7130static xmlExpNodePtr
7131xmlExpParseSeq(xmlExpCtxtPtr ctxt) {
7132 xmlExpNodePtr ret, right;
7133
7134 ret = xmlExpParseOr(ctxt);
7135 SKIP_BLANKS
7136 while (CUR == '|') {
7137 NEXT
7138 right = xmlExpParseOr(ctxt);
7139 if (right == NULL) {
7140 xmlExpFree(ctxt, ret);
7141 return(NULL);
7142 }
7143 ret = xmlExpHashGetEntry(ctxt, XML_EXP_OR, ret, right, NULL, 0, 0);
7144 if (ret == NULL)
7145 return(NULL);
7146 }
7147 return(ret);
7148}
7149
7150static xmlExpNodePtr
7151xmlExpParseExpr(xmlExpCtxtPtr ctxt) {
7152 xmlExpNodePtr ret, right;
7153
7154 ret = xmlExpParseSeq(ctxt);
7155 SKIP_BLANKS
7156 while (CUR == ',') {
7157 NEXT
7158 right = xmlExpParseSeq(ctxt);
7159 if (right == NULL) {
7160 xmlExpFree(ctxt, ret);
7161 return(NULL);
7162 }
7163 ret = xmlExpHashGetEntry(ctxt, XML_EXP_SEQ, ret, right, NULL, 0, 0);
7164 if (ret == NULL)
7165 return(NULL);
7166 }
7167 return(ret);
7168}
7169
7170/**
7171 * xmlExpParse:
7172 * @ctxt: the expressions context
7173 * @expr: the 0 terminated string
7174 *
7175 * Minimal parser for regexps, it understand the following constructs
7176 * - string terminals
7177 * - choice operator |
7178 * - sequence operator ,
7179 * - subexpressions (...)
7180 * - usual cardinality operators + * and ?
7181 * - finite sequences { min, max }
7182 * - infinite sequences { min, * }
7183 * There is minimal checkings made especially no checking on strings values
7184 *
7185 * Returns a new expression or NULL in case of failure
7186 */
7187xmlExpNodePtr
7188xmlExpParse(xmlExpCtxtPtr ctxt, const char *expr) {
7189 xmlExpNodePtr ret;
7190
7191 ctxt->expr = expr;
7192 ctxt->cur = expr;
7193
7194 ret = xmlExpParseExpr(ctxt);
7195 SKIP_BLANKS
7196 if (*ctxt->cur != 0) {
7197 xmlExpFree(ctxt, ret);
7198 return(NULL);
7199 }
7200 return(ret);
7201}
7202
7203static void
7204xmlExpDumpInt(xmlBufferPtr buf, xmlExpNodePtr expr, int glob) {
7205 xmlExpNodePtr c;
7206
7207 if (expr == NULL) return;
7208 if (glob) xmlBufferWriteChar(buf, "(");
7209 switch (expr->type) {
7210 case XML_EXP_EMPTY:
7211 xmlBufferWriteChar(buf, "empty");
7212 break;
7213 case XML_EXP_FORBID:
7214 xmlBufferWriteChar(buf, "forbidden");
7215 break;
7216 case XML_EXP_ATOM:
7217 xmlBufferWriteCHAR(buf, expr->exp_str);
7218 break;
7219 case XML_EXP_SEQ:
7220 c = expr->exp_left;
7221 if ((c->type == XML_EXP_SEQ) || (c->type == XML_EXP_OR))
7222 xmlExpDumpInt(buf, c, 1);
7223 else
7224 xmlExpDumpInt(buf, c, 0);
7225 xmlBufferWriteChar(buf, " , ");
7226 c = expr->exp_right;
7227 if ((c->type == XML_EXP_SEQ) || (c->type == XML_EXP_OR))
7228 xmlExpDumpInt(buf, c, 1);
7229 else
7230 xmlExpDumpInt(buf, c, 0);
7231 break;
7232 case XML_EXP_OR:
7233 c = expr->exp_left;
7234 if ((c->type == XML_EXP_SEQ) || (c->type == XML_EXP_OR))
7235 xmlExpDumpInt(buf, c, 1);
7236 else
7237 xmlExpDumpInt(buf, c, 0);
7238 xmlBufferWriteChar(buf, " | ");
7239 c = expr->exp_right;
7240 if ((c->type == XML_EXP_SEQ) || (c->type == XML_EXP_OR))
7241 xmlExpDumpInt(buf, c, 1);
7242 else
7243 xmlExpDumpInt(buf, c, 0);
7244 break;
7245 case XML_EXP_COUNT: {
7246 char rep[40];
7247
7248 c = expr->exp_left;
7249 if ((c->type == XML_EXP_SEQ) || (c->type == XML_EXP_OR))
7250 xmlExpDumpInt(buf, c, 1);
7251 else
7252 xmlExpDumpInt(buf, c, 0);
7253 if ((expr->exp_min == 0) && (expr->exp_max == 1)) {
7254 rep[0] = '?';
7255 rep[1] = 0;
7256 } else if ((expr->exp_min == 0) && (expr->exp_max == -1)) {
7257 rep[0] = '*';
7258 rep[1] = 0;
7259 } else if ((expr->exp_min == 1) && (expr->exp_max == -1)) {
7260 rep[0] = '+';
7261 rep[1] = 0;
7262 } else if (expr->exp_max == expr->exp_min) {
7263 snprintf(rep, 39, "{%d}", expr->exp_min);
7264 } else if (expr->exp_max < 0) {
7265 snprintf(rep, 39, "{%d,inf}", expr->exp_min);
7266 } else {
7267 snprintf(rep, 39, "{%d,%d}", expr->exp_min, expr->exp_max);
7268 }
7269 rep[39] = 0;
7270 xmlBufferWriteChar(buf, rep);
7271 break;
7272 }
7273 default:
7274 fprintf(stderr, "Error in tree\n");
7275 }
7276 if (glob)
7277 xmlBufferWriteChar(buf, ")");
7278}
7279/**
7280 * xmlExpDump:
7281 * @buf: a buffer to receive the output
7282 * @expr: the compiled expression
7283 *
7284 * Serialize the expression as compiled to the buffer
7285 */
7286void
Daniel Veillard5eee7672005-08-22 21:22:27 +00007287xmlExpDump(xmlBufferPtr buf, xmlExpNodePtr expr) {
7288 if ((buf == NULL) || (expr == NULL))
Daniel Veillard465a0002005-08-22 12:07:04 +00007289 return;
Daniel Veillard5eee7672005-08-22 21:22:27 +00007290 xmlExpDumpInt(buf, expr, 0);
Daniel Veillard465a0002005-08-22 12:07:04 +00007291}
7292
7293/**
7294 * xmlExpMaxToken:
7295 * @expr: a compiled expression
7296 *
7297 * Indicate the maximum number of input a expression can accept
7298 *
7299 * Returns the maximum length or -1 in case of error
7300 */
7301int
7302xmlExpMaxToken(xmlExpNodePtr expr) {
7303 if (expr == NULL)
7304 return(-1);
7305 return(expr->c_max);
7306}
7307
7308/**
7309 * xmlExpCtxtNbNodes:
7310 * @ctxt: an expression context
7311 *
7312 * Debugging facility provides the number of allocated nodes at a that point
7313 *
7314 * Returns the number of nodes in use or -1 in case of error
7315 */
7316int
7317xmlExpCtxtNbNodes(xmlExpCtxtPtr ctxt) {
7318 if (ctxt == NULL)
7319 return(-1);
7320 return(ctxt->nb_nodes);
7321}
7322
7323/**
7324 * xmlExpCtxtNbCons:
7325 * @ctxt: an expression context
7326 *
7327 * Debugging facility provides the number of allocated nodes over lifetime
7328 *
7329 * Returns the number of nodes ever allocated or -1 in case of error
7330 */
7331int
7332xmlExpCtxtNbCons(xmlExpCtxtPtr ctxt) {
7333 if (ctxt == NULL)
7334 return(-1);
7335 return(ctxt->nb_cons);
7336}
7337
Daniel Veillard81a8ec62005-08-22 00:20:58 +00007338#endif /* LIBXML_EXPR_ENABLED */
Daniel Veillard5d4644e2005-04-01 13:11:58 +00007339#define bottom_xmlregexp
7340#include "elfgcchack.h"
Daniel Veillard4255d502002-04-16 15:50:10 +00007341#endif /* LIBXML_REGEXP_ENABLED */