blob: 55b09544244350ce0d46c941569ff0ba30c7c66a [file] [log] [blame]
Daniel Veillard4255d502002-04-16 15:50:10 +00001/*
2 * regexp.c: generic and extensible Regular Expression engine
3 *
4 * Basically designed with the purpose of compiling regexps for
5 * the variety of validation/shemas mechanisms now available in
William M. Brackddf71d62004-05-06 04:17:26 +00006 * XML related specifications these include:
Daniel Veillard4255d502002-04-16 15:50:10 +00007 * - XML-1.0 DTD validation
8 * - XML Schemas structure part 1
9 * - XML Schemas Datatypes part 2 especially Appendix F
10 * - RELAX-NG/TREX i.e. the counter proposal
11 *
12 * See Copyright for the status of this software.
13 *
14 * Daniel Veillard <veillard@redhat.com>
15 */
16
17#define IN_LIBXML
18#include "libxml.h"
19
20#ifdef LIBXML_REGEXP_ENABLED
21
Daniel Veillardcee2b3a2005-01-25 00:22:52 +000022/* #define DEBUG_ERR */
Daniel Veillardfc0b6f62005-01-09 17:48:02 +000023
Daniel Veillard4255d502002-04-16 15:50:10 +000024#include <stdio.h>
25#include <string.h>
Daniel Veillardebe48c62003-12-03 12:12:27 +000026#ifdef HAVE_LIMITS_H
27#include <limits.h>
28#endif
29
Daniel Veillard4255d502002-04-16 15:50:10 +000030#include <libxml/tree.h>
31#include <libxml/parserInternals.h>
32#include <libxml/xmlregexp.h>
33#include <libxml/xmlautomata.h>
34#include <libxml/xmlunicode.h>
35
Daniel Veillardebe48c62003-12-03 12:12:27 +000036#ifndef INT_MAX
37#define INT_MAX 123456789 /* easy to flag and big enough for our needs */
38#endif
39
Daniel Veillardc0826a72004-08-10 14:17:33 +000040/* #define DEBUG_REGEXP_GRAPH */
Daniel Veillard10752282005-08-08 13:05:13 +000041/* #define DEBUG_REGEXP_EXEC */
Daniel Veillard4255d502002-04-16 15:50:10 +000042/* #define DEBUG_PUSH */
Daniel Veillard23e73572002-09-19 19:56:43 +000043/* #define DEBUG_COMPACTION */
Daniel Veillard4255d502002-04-16 15:50:10 +000044
Daniel Veillard567a45b2005-10-18 19:11:55 +000045#define MAX_PUSH 10000000
Daniel Veillard94cc1032005-09-15 13:09:00 +000046
Daniel Veillardff46a042003-10-08 08:53:17 +000047#define ERROR(str) \
48 ctxt->error = XML_REGEXP_COMPILE_ERROR; \
49 xmlRegexpErrCompile(ctxt, str);
Daniel Veillard4255d502002-04-16 15:50:10 +000050#define NEXT ctxt->cur++
51#define CUR (*(ctxt->cur))
52#define NXT(index) (ctxt->cur[index])
53
54#define CUR_SCHAR(s, l) xmlStringCurrentChar(NULL, s, &l)
55#define NEXTL(l) ctxt->cur += l;
Daniel Veillardc0826a72004-08-10 14:17:33 +000056#define XML_REG_STRING_SEPARATOR '|'
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,
Daniel Veillard567a45b2005-10-18 19:11:55 +000078 XML_REGEXP_SUBREG, /* used for () sub regexps */
Daniel Veillard4255d502002-04-16 15:50:10 +000079 XML_REGEXP_STRING,
80 XML_REGEXP_ANYCHAR, /* . */
81 XML_REGEXP_ANYSPACE, /* \s */
82 XML_REGEXP_NOTSPACE, /* \S */
83 XML_REGEXP_INITNAME, /* \l */
Daniel Veillard567a45b2005-10-18 19:11:55 +000084 XML_REGEXP_NOTINITNAME, /* \L */
Daniel Veillard4255d502002-04-16 15:50:10 +000085 XML_REGEXP_NAMECHAR, /* \c */
86 XML_REGEXP_NOTNAMECHAR, /* \C */
87 XML_REGEXP_DECIMAL, /* \d */
Daniel Veillard567a45b2005-10-18 19:11:55 +000088 XML_REGEXP_NOTDECIMAL, /* \D */
Daniel Veillard4255d502002-04-16 15:50:10 +000089 XML_REGEXP_REALCHAR, /* \w */
Daniel Veillard567a45b2005-10-18 19:11:55 +000090 XML_REGEXP_NOTREALCHAR, /* \W */
91 XML_REGEXP_LETTER = 100,
Daniel Veillard4255d502002-04-16 15:50:10 +000092 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;
Daniel Veillard567a45b2005-10-18 19:11:55 +0000206 int nd;
Daniel Veillard4255d502002-04-16 15:50:10 +0000207};
208
209struct _xmlAutomataState {
210 xmlRegStateType type;
211 xmlRegMarkedType mark;
Daniel Veillard23e73572002-09-19 19:56:43 +0000212 xmlRegMarkedType reached;
Daniel Veillard4255d502002-04-16 15:50:10 +0000213 int no;
Daniel Veillard4255d502002-04-16 15:50:10 +0000214 int maxTrans;
215 int nbTrans;
216 xmlRegTrans *trans;
Daniel Veillarddb68b742005-07-30 13:18:24 +0000217 /* knowing states ponting to us can speed things up */
218 int maxTransTo;
219 int nbTransTo;
220 int *transTo;
Daniel Veillard4255d502002-04-16 15:50:10 +0000221};
222
223typedef struct _xmlAutomata xmlRegParserCtxt;
224typedef xmlRegParserCtxt *xmlRegParserCtxtPtr;
225
226struct _xmlAutomata {
227 xmlChar *string;
228 xmlChar *cur;
229
230 int error;
231 int neg;
232
233 xmlRegStatePtr start;
234 xmlRegStatePtr end;
235 xmlRegStatePtr state;
236
237 xmlRegAtomPtr atom;
238
239 int maxAtoms;
240 int nbAtoms;
241 xmlRegAtomPtr *atoms;
242
243 int maxStates;
244 int nbStates;
245 xmlRegStatePtr *states;
246
247 int maxCounters;
248 int nbCounters;
249 xmlRegCounter *counters;
Daniel Veillarde19fc232002-04-22 16:01:24 +0000250
251 int determinist;
Daniel Veillard6e65e152005-08-09 11:09:52 +0000252 int negs;
Daniel Veillard4255d502002-04-16 15:50:10 +0000253};
254
255struct _xmlRegexp {
256 xmlChar *string;
257 int nbStates;
258 xmlRegStatePtr *states;
259 int nbAtoms;
260 xmlRegAtomPtr *atoms;
261 int nbCounters;
262 xmlRegCounter *counters;
Daniel Veillarde19fc232002-04-22 16:01:24 +0000263 int determinist;
Daniel Veillard23e73572002-09-19 19:56:43 +0000264 /*
265 * That's the compact form for determinists automatas
266 */
267 int nbstates;
268 int *compact;
Daniel Veillard118aed72002-09-24 14:13:13 +0000269 void **transdata;
Daniel Veillard23e73572002-09-19 19:56:43 +0000270 int nbstrings;
271 xmlChar **stringMap;
Daniel Veillard4255d502002-04-16 15:50:10 +0000272};
273
274typedef struct _xmlRegExecRollback xmlRegExecRollback;
275typedef xmlRegExecRollback *xmlRegExecRollbackPtr;
276
277struct _xmlRegExecRollback {
278 xmlRegStatePtr state;/* the current state */
279 int index; /* the index in the input stack */
280 int nextbranch; /* the next transition to explore in that state */
William M. Brackddf71d62004-05-06 04:17:26 +0000281 int *counts; /* save the automata state if it has some */
Daniel Veillard4255d502002-04-16 15:50:10 +0000282};
283
284typedef struct _xmlRegInputToken xmlRegInputToken;
285typedef xmlRegInputToken *xmlRegInputTokenPtr;
286
287struct _xmlRegInputToken {
288 xmlChar *value;
289 void *data;
290};
291
292struct _xmlRegExecCtxt {
293 int status; /* execution status != 0 indicate an error */
William M. Brackddf71d62004-05-06 04:17:26 +0000294 int determinist; /* did we find an indeterministic behaviour */
Daniel Veillard4255d502002-04-16 15:50:10 +0000295 xmlRegexpPtr comp; /* the compiled regexp */
296 xmlRegExecCallbacks callback;
297 void *data;
298
299 xmlRegStatePtr state;/* the current state */
300 int transno; /* the current transition on that state */
William M. Brackddf71d62004-05-06 04:17:26 +0000301 int transcount; /* the number of chars in char counted transitions */
Daniel Veillard4255d502002-04-16 15:50:10 +0000302
303 /*
304 * A stack of rollback states
305 */
306 int maxRollbacks;
307 int nbRollbacks;
308 xmlRegExecRollback *rollbacks;
309
310 /*
311 * The state of the automata if any
312 */
313 int *counts;
314
315 /*
316 * The input stack
317 */
318 int inputStackMax;
319 int inputStackNr;
320 int index;
321 int *charStack;
322 const xmlChar *inputString; /* when operating on characters */
323 xmlRegInputTokenPtr inputStack;/* when operating on strings */
324
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +0000325 /*
326 * error handling
327 */
328 int errStateNo; /* the error state number */
329 xmlRegStatePtr errState; /* the error state */
330 xmlChar *errString; /* the string raising the error */
331 int *errCounts; /* counters at the error state */
Daniel Veillard94cc1032005-09-15 13:09:00 +0000332 int nbPush;
Daniel Veillard4255d502002-04-16 15:50:10 +0000333};
334
Daniel Veillard441bc322002-04-20 17:38:48 +0000335#define REGEXP_ALL_COUNTER 0x123456
336#define REGEXP_ALL_LAX_COUNTER 0x123457
Daniel Veillard7646b182002-04-20 06:41:40 +0000337
Daniel Veillard4255d502002-04-16 15:50:10 +0000338static void xmlFAParseRegExp(xmlRegParserCtxtPtr ctxt, int top);
Daniel Veillard23e73572002-09-19 19:56:43 +0000339static void xmlRegFreeState(xmlRegStatePtr state);
340static void xmlRegFreeAtom(xmlRegAtomPtr atom);
Daniel Veillard9efc4762005-07-19 14:33:55 +0000341static int xmlRegStrEqualWildcard(const xmlChar *expStr, const xmlChar *valStr);
Daniel Veillard567a45b2005-10-18 19:11:55 +0000342static int xmlRegCheckCharacter(xmlRegAtomPtr atom, int codepoint);
343static int xmlRegCheckCharacterRange(xmlRegAtomType type, int codepoint,
344 int neg, int start, int end, const xmlChar *blockName);
Daniel Veillard4255d502002-04-16 15:50:10 +0000345
346/************************************************************************
Daniel Veillardff46a042003-10-08 08:53:17 +0000347 * *
348 * Regexp memory error handler *
349 * *
350 ************************************************************************/
351/**
352 * xmlRegexpErrMemory:
William M. Brackddf71d62004-05-06 04:17:26 +0000353 * @extra: extra information
Daniel Veillardff46a042003-10-08 08:53:17 +0000354 *
355 * Handle an out of memory condition
356 */
357static void
358xmlRegexpErrMemory(xmlRegParserCtxtPtr ctxt, const char *extra)
359{
360 const char *regexp = NULL;
361 if (ctxt != NULL) {
362 regexp = (const char *) ctxt->string;
363 ctxt->error = XML_ERR_NO_MEMORY;
364 }
Daniel Veillard659e71e2003-10-10 14:10:40 +0000365 __xmlRaiseError(NULL, NULL, NULL, NULL, NULL, XML_FROM_REGEXP,
Daniel Veillardff46a042003-10-08 08:53:17 +0000366 XML_ERR_NO_MEMORY, XML_ERR_FATAL, NULL, 0, extra,
367 regexp, NULL, 0, 0,
368 "Memory allocation failed : %s\n", extra);
369}
370
371/**
372 * xmlRegexpErrCompile:
William M. Brackddf71d62004-05-06 04:17:26 +0000373 * @extra: extra information
Daniel Veillardff46a042003-10-08 08:53:17 +0000374 *
William M. Brackddf71d62004-05-06 04:17:26 +0000375 * Handle a compilation failure
Daniel Veillardff46a042003-10-08 08:53:17 +0000376 */
377static void
378xmlRegexpErrCompile(xmlRegParserCtxtPtr ctxt, const char *extra)
379{
380 const char *regexp = NULL;
381 int idx = 0;
382
383 if (ctxt != NULL) {
384 regexp = (const char *) ctxt->string;
385 idx = ctxt->cur - ctxt->string;
386 ctxt->error = XML_REGEXP_COMPILE_ERROR;
387 }
Daniel Veillard659e71e2003-10-10 14:10:40 +0000388 __xmlRaiseError(NULL, NULL, NULL, NULL, NULL, XML_FROM_REGEXP,
Daniel Veillardff46a042003-10-08 08:53:17 +0000389 XML_REGEXP_COMPILE_ERROR, XML_ERR_FATAL, NULL, 0, extra,
390 regexp, NULL, idx, 0,
391 "failed to compile: %s\n", extra);
392}
393
394/************************************************************************
Daniel Veillard4255d502002-04-16 15:50:10 +0000395 * *
396 * Allocation/Deallocation *
397 * *
398 ************************************************************************/
399
Daniel Veillard23e73572002-09-19 19:56:43 +0000400static int xmlFAComputesDeterminism(xmlRegParserCtxtPtr ctxt);
Daniel Veillard4255d502002-04-16 15:50:10 +0000401/**
402 * xmlRegEpxFromParse:
403 * @ctxt: the parser context used to build it
404 *
William M. Brackddf71d62004-05-06 04:17:26 +0000405 * Allocate a new regexp and fill it with the result from the parser
Daniel Veillard4255d502002-04-16 15:50:10 +0000406 *
407 * Returns the new regexp or NULL in case of error
408 */
409static xmlRegexpPtr
410xmlRegEpxFromParse(xmlRegParserCtxtPtr ctxt) {
411 xmlRegexpPtr ret;
412
413 ret = (xmlRegexpPtr) xmlMalloc(sizeof(xmlRegexp));
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000414 if (ret == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +0000415 xmlRegexpErrMemory(ctxt, "compiling regexp");
Daniel Veillard4255d502002-04-16 15:50:10 +0000416 return(NULL);
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000417 }
Daniel Veillard4255d502002-04-16 15:50:10 +0000418 memset(ret, 0, sizeof(xmlRegexp));
419 ret->string = ctxt->string;
Daniel Veillard4255d502002-04-16 15:50:10 +0000420 ret->nbStates = ctxt->nbStates;
Daniel Veillard4255d502002-04-16 15:50:10 +0000421 ret->states = ctxt->states;
Daniel Veillard4255d502002-04-16 15:50:10 +0000422 ret->nbAtoms = ctxt->nbAtoms;
Daniel Veillard4255d502002-04-16 15:50:10 +0000423 ret->atoms = ctxt->atoms;
Daniel Veillard4255d502002-04-16 15:50:10 +0000424 ret->nbCounters = ctxt->nbCounters;
Daniel Veillard4255d502002-04-16 15:50:10 +0000425 ret->counters = ctxt->counters;
Daniel Veillarde19fc232002-04-22 16:01:24 +0000426 ret->determinist = ctxt->determinist;
Daniel Veillard567a45b2005-10-18 19:11:55 +0000427 if (ret->determinist == -1) {
428 xmlRegexpIsDeterminist(ret);
429 }
Daniel Veillard23e73572002-09-19 19:56:43 +0000430
431 if ((ret->determinist != 0) &&
432 (ret->nbCounters == 0) &&
Daniel Veillard6e65e152005-08-09 11:09:52 +0000433 (ctxt->negs == 0) &&
Daniel Veillard118aed72002-09-24 14:13:13 +0000434 (ret->atoms != NULL) &&
Daniel Veillard23e73572002-09-19 19:56:43 +0000435 (ret->atoms[0] != NULL) &&
436 (ret->atoms[0]->type == XML_REGEXP_STRING)) {
437 int i, j, nbstates = 0, nbatoms = 0;
438 int *stateRemap;
439 int *stringRemap;
440 int *transitions;
Daniel Veillard118aed72002-09-24 14:13:13 +0000441 void **transdata;
Daniel Veillard23e73572002-09-19 19:56:43 +0000442 xmlChar **stringMap;
443 xmlChar *value;
444
445 /*
446 * Switch to a compact representation
447 * 1/ counting the effective number of states left
William M. Brackddf71d62004-05-06 04:17:26 +0000448 * 2/ counting the unique number of atoms, and check that
Daniel Veillard23e73572002-09-19 19:56:43 +0000449 * they are all of the string type
450 * 3/ build a table state x atom for the transitions
451 */
452
453 stateRemap = xmlMalloc(ret->nbStates * sizeof(int));
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000454 if (stateRemap == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +0000455 xmlRegexpErrMemory(ctxt, "compiling regexp");
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000456 xmlFree(ret);
457 return(NULL);
458 }
Daniel Veillard23e73572002-09-19 19:56:43 +0000459 for (i = 0;i < ret->nbStates;i++) {
460 if (ret->states[i] != NULL) {
461 stateRemap[i] = nbstates;
462 nbstates++;
463 } else {
464 stateRemap[i] = -1;
465 }
466 }
467#ifdef DEBUG_COMPACTION
468 printf("Final: %d states\n", nbstates);
469#endif
470 stringMap = xmlMalloc(ret->nbAtoms * sizeof(char *));
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000471 if (stringMap == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +0000472 xmlRegexpErrMemory(ctxt, "compiling regexp");
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000473 xmlFree(stateRemap);
474 xmlFree(ret);
475 return(NULL);
476 }
Daniel Veillard23e73572002-09-19 19:56:43 +0000477 stringRemap = xmlMalloc(ret->nbAtoms * sizeof(int));
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000478 if (stringRemap == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +0000479 xmlRegexpErrMemory(ctxt, "compiling regexp");
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000480 xmlFree(stringMap);
481 xmlFree(stateRemap);
482 xmlFree(ret);
483 return(NULL);
484 }
Daniel Veillard23e73572002-09-19 19:56:43 +0000485 for (i = 0;i < ret->nbAtoms;i++) {
486 if ((ret->atoms[i]->type == XML_REGEXP_STRING) &&
487 (ret->atoms[i]->quant == XML_REGEXP_QUANT_ONCE)) {
488 value = ret->atoms[i]->valuep;
489 for (j = 0;j < nbatoms;j++) {
490 if (xmlStrEqual(stringMap[j], value)) {
491 stringRemap[i] = j;
492 break;
493 }
494 }
495 if (j >= nbatoms) {
496 stringRemap[i] = nbatoms;
497 stringMap[nbatoms] = xmlStrdup(value);
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000498 if (stringMap[nbatoms] == NULL) {
499 for (i = 0;i < nbatoms;i++)
500 xmlFree(stringMap[i]);
501 xmlFree(stringRemap);
502 xmlFree(stringMap);
503 xmlFree(stateRemap);
504 xmlFree(ret);
505 return(NULL);
506 }
Daniel Veillard23e73572002-09-19 19:56:43 +0000507 nbatoms++;
508 }
509 } else {
510 xmlFree(stateRemap);
511 xmlFree(stringRemap);
512 for (i = 0;i < nbatoms;i++)
513 xmlFree(stringMap[i]);
514 xmlFree(stringMap);
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000515 xmlFree(ret);
516 return(NULL);
Daniel Veillard23e73572002-09-19 19:56:43 +0000517 }
518 }
519#ifdef DEBUG_COMPACTION
520 printf("Final: %d atoms\n", nbatoms);
521#endif
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000522 transitions = (int *) xmlMalloc((nbstates + 1) *
523 (nbatoms + 1) * sizeof(int));
524 if (transitions == NULL) {
525 xmlFree(stateRemap);
526 xmlFree(stringRemap);
527 xmlFree(stringMap);
528 xmlFree(ret);
529 return(NULL);
530 }
531 memset(transitions, 0, (nbstates + 1) * (nbatoms + 1) * sizeof(int));
Daniel Veillard23e73572002-09-19 19:56:43 +0000532
533 /*
534 * Allocate the transition table. The first entry for each
William M. Brackddf71d62004-05-06 04:17:26 +0000535 * state corresponds to the state type.
Daniel Veillard23e73572002-09-19 19:56:43 +0000536 */
Daniel Veillard118aed72002-09-24 14:13:13 +0000537 transdata = NULL;
Daniel Veillard23e73572002-09-19 19:56:43 +0000538
539 for (i = 0;i < ret->nbStates;i++) {
540 int stateno, atomno, targetno, prev;
541 xmlRegStatePtr state;
542 xmlRegTransPtr trans;
543
544 stateno = stateRemap[i];
545 if (stateno == -1)
546 continue;
547 state = ret->states[i];
548
549 transitions[stateno * (nbatoms + 1)] = state->type;
550
551 for (j = 0;j < state->nbTrans;j++) {
552 trans = &(state->trans[j]);
553 if ((trans->to == -1) || (trans->atom == NULL))
554 continue;
555 atomno = stringRemap[trans->atom->no];
Daniel Veillard118aed72002-09-24 14:13:13 +0000556 if ((trans->atom->data != NULL) && (transdata == NULL)) {
557 transdata = (void **) xmlMalloc(nbstates * nbatoms *
558 sizeof(void *));
559 if (transdata != NULL)
560 memset(transdata, 0,
561 nbstates * nbatoms * sizeof(void *));
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000562 else {
Daniel Veillardff46a042003-10-08 08:53:17 +0000563 xmlRegexpErrMemory(ctxt, "compiling regexp");
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000564 break;
565 }
Daniel Veillard118aed72002-09-24 14:13:13 +0000566 }
Daniel Veillard23e73572002-09-19 19:56:43 +0000567 targetno = stateRemap[trans->to];
568 /*
William M. Brackddf71d62004-05-06 04:17:26 +0000569 * if the same atom can generate transitions to 2 different
Daniel Veillard23e73572002-09-19 19:56:43 +0000570 * states then it means the automata is not determinist and
571 * the compact form can't be used !
572 */
573 prev = transitions[stateno * (nbatoms + 1) + atomno + 1];
574 if (prev != 0) {
575 if (prev != targetno + 1) {
Daniel Veillard23e73572002-09-19 19:56:43 +0000576 ret->determinist = 0;
577#ifdef DEBUG_COMPACTION
578 printf("Indet: state %d trans %d, atom %d to %d : %d to %d\n",
579 i, j, trans->atom->no, trans->to, atomno, targetno);
580 printf(" previous to is %d\n", prev);
581#endif
Daniel Veillard118aed72002-09-24 14:13:13 +0000582 if (transdata != NULL)
583 xmlFree(transdata);
Daniel Veillard23e73572002-09-19 19:56:43 +0000584 xmlFree(transitions);
585 xmlFree(stateRemap);
586 xmlFree(stringRemap);
587 for (i = 0;i < nbatoms;i++)
588 xmlFree(stringMap[i]);
589 xmlFree(stringMap);
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000590 goto not_determ;
Daniel Veillard23e73572002-09-19 19:56:43 +0000591 }
592 } else {
593#if 0
594 printf("State %d trans %d: atom %d to %d : %d to %d\n",
595 i, j, trans->atom->no, trans->to, atomno, targetno);
596#endif
597 transitions[stateno * (nbatoms + 1) + atomno + 1] =
Daniel Veillard118aed72002-09-24 14:13:13 +0000598 targetno + 1; /* to avoid 0 */
599 if (transdata != NULL)
600 transdata[stateno * nbatoms + atomno] =
601 trans->atom->data;
Daniel Veillard23e73572002-09-19 19:56:43 +0000602 }
603 }
604 }
605 ret->determinist = 1;
606#ifdef DEBUG_COMPACTION
607 /*
608 * Debug
609 */
610 for (i = 0;i < nbstates;i++) {
611 for (j = 0;j < nbatoms + 1;j++) {
612 printf("%02d ", transitions[i * (nbatoms + 1) + j]);
613 }
614 printf("\n");
615 }
616 printf("\n");
617#endif
618 /*
619 * Cleanup of the old data
620 */
621 if (ret->states != NULL) {
622 for (i = 0;i < ret->nbStates;i++)
623 xmlRegFreeState(ret->states[i]);
624 xmlFree(ret->states);
625 }
626 ret->states = NULL;
627 ret->nbStates = 0;
628 if (ret->atoms != NULL) {
629 for (i = 0;i < ret->nbAtoms;i++)
630 xmlRegFreeAtom(ret->atoms[i]);
631 xmlFree(ret->atoms);
632 }
633 ret->atoms = NULL;
634 ret->nbAtoms = 0;
635
636 ret->compact = transitions;
Daniel Veillard118aed72002-09-24 14:13:13 +0000637 ret->transdata = transdata;
Daniel Veillard23e73572002-09-19 19:56:43 +0000638 ret->stringMap = stringMap;
639 ret->nbstrings = nbatoms;
640 ret->nbstates = nbstates;
641 xmlFree(stateRemap);
642 xmlFree(stringRemap);
643 }
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000644not_determ:
645 ctxt->string = NULL;
646 ctxt->nbStates = 0;
647 ctxt->states = NULL;
648 ctxt->nbAtoms = 0;
649 ctxt->atoms = NULL;
650 ctxt->nbCounters = 0;
651 ctxt->counters = NULL;
Daniel Veillard4255d502002-04-16 15:50:10 +0000652 return(ret);
653}
654
655/**
656 * xmlRegNewParserCtxt:
657 * @string: the string to parse
658 *
659 * Allocate a new regexp parser context
660 *
661 * Returns the new context or NULL in case of error
662 */
663static xmlRegParserCtxtPtr
664xmlRegNewParserCtxt(const xmlChar *string) {
665 xmlRegParserCtxtPtr ret;
666
667 ret = (xmlRegParserCtxtPtr) xmlMalloc(sizeof(xmlRegParserCtxt));
668 if (ret == NULL)
669 return(NULL);
670 memset(ret, 0, sizeof(xmlRegParserCtxt));
671 if (string != NULL)
672 ret->string = xmlStrdup(string);
673 ret->cur = ret->string;
674 ret->neg = 0;
Daniel Veillard6e65e152005-08-09 11:09:52 +0000675 ret->negs = 0;
Daniel Veillard4255d502002-04-16 15:50:10 +0000676 ret->error = 0;
Daniel Veillarde19fc232002-04-22 16:01:24 +0000677 ret->determinist = -1;
Daniel Veillard4255d502002-04-16 15:50:10 +0000678 return(ret);
679}
680
681/**
682 * xmlRegNewRange:
683 * @ctxt: the regexp parser context
684 * @neg: is that negative
685 * @type: the type of range
686 * @start: the start codepoint
687 * @end: the end codepoint
688 *
689 * Allocate a new regexp range
690 *
691 * Returns the new range or NULL in case of error
692 */
693static xmlRegRangePtr
694xmlRegNewRange(xmlRegParserCtxtPtr ctxt,
695 int neg, xmlRegAtomType type, int start, int end) {
696 xmlRegRangePtr ret;
697
698 ret = (xmlRegRangePtr) xmlMalloc(sizeof(xmlRegRange));
699 if (ret == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +0000700 xmlRegexpErrMemory(ctxt, "allocating range");
Daniel Veillard4255d502002-04-16 15:50:10 +0000701 return(NULL);
702 }
703 ret->neg = neg;
704 ret->type = type;
705 ret->start = start;
706 ret->end = end;
707 return(ret);
708}
709
710/**
711 * xmlRegFreeRange:
712 * @range: the regexp range
713 *
714 * Free a regexp range
715 */
716static void
717xmlRegFreeRange(xmlRegRangePtr range) {
718 if (range == NULL)
719 return;
720
721 if (range->blockName != NULL)
722 xmlFree(range->blockName);
723 xmlFree(range);
724}
725
726/**
727 * xmlRegNewAtom:
728 * @ctxt: the regexp parser context
729 * @type: the type of atom
730 *
731 * Allocate a new regexp range
732 *
733 * Returns the new atom or NULL in case of error
734 */
735static xmlRegAtomPtr
736xmlRegNewAtom(xmlRegParserCtxtPtr ctxt, xmlRegAtomType type) {
737 xmlRegAtomPtr ret;
738
739 ret = (xmlRegAtomPtr) xmlMalloc(sizeof(xmlRegAtom));
740 if (ret == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +0000741 xmlRegexpErrMemory(ctxt, "allocating atom");
Daniel Veillard4255d502002-04-16 15:50:10 +0000742 return(NULL);
743 }
744 memset(ret, 0, sizeof(xmlRegAtom));
745 ret->type = type;
746 ret->quant = XML_REGEXP_QUANT_ONCE;
747 ret->min = 0;
748 ret->max = 0;
749 return(ret);
750}
751
752/**
753 * xmlRegFreeAtom:
754 * @atom: the regexp atom
755 *
756 * Free a regexp atom
757 */
758static void
759xmlRegFreeAtom(xmlRegAtomPtr atom) {
760 int i;
761
762 if (atom == NULL)
763 return;
764
765 for (i = 0;i < atom->nbRanges;i++)
766 xmlRegFreeRange(atom->ranges[i]);
767 if (atom->ranges != NULL)
768 xmlFree(atom->ranges);
Daniel Veillardde0e4982005-07-03 14:35:44 +0000769 if ((atom->type == XML_REGEXP_STRING) && (atom->valuep != NULL))
770 xmlFree(atom->valuep);
Daniel Veillard77005e62005-07-19 16:26:18 +0000771 if ((atom->type == XML_REGEXP_STRING) && (atom->valuep2 != NULL))
772 xmlFree(atom->valuep2);
Daniel Veillardde0e4982005-07-03 14:35:44 +0000773 if ((atom->type == XML_REGEXP_BLOCK_NAME) && (atom->valuep != NULL))
Daniel Veillard4255d502002-04-16 15:50:10 +0000774 xmlFree(atom->valuep);
775 xmlFree(atom);
776}
777
778static xmlRegStatePtr
779xmlRegNewState(xmlRegParserCtxtPtr ctxt) {
780 xmlRegStatePtr ret;
781
782 ret = (xmlRegStatePtr) xmlMalloc(sizeof(xmlRegState));
783 if (ret == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +0000784 xmlRegexpErrMemory(ctxt, "allocating state");
Daniel Veillard4255d502002-04-16 15:50:10 +0000785 return(NULL);
786 }
787 memset(ret, 0, sizeof(xmlRegState));
788 ret->type = XML_REGEXP_TRANS_STATE;
789 ret->mark = XML_REGEXP_MARK_NORMAL;
790 return(ret);
791}
792
793/**
794 * xmlRegFreeState:
795 * @state: the regexp state
796 *
797 * Free a regexp state
798 */
799static void
800xmlRegFreeState(xmlRegStatePtr state) {
801 if (state == NULL)
802 return;
803
804 if (state->trans != NULL)
805 xmlFree(state->trans);
Daniel Veillarddb68b742005-07-30 13:18:24 +0000806 if (state->transTo != NULL)
807 xmlFree(state->transTo);
Daniel Veillard4255d502002-04-16 15:50:10 +0000808 xmlFree(state);
809}
810
811/**
812 * xmlRegFreeParserCtxt:
813 * @ctxt: the regexp parser context
814 *
815 * Free a regexp parser context
816 */
817static void
818xmlRegFreeParserCtxt(xmlRegParserCtxtPtr ctxt) {
819 int i;
820 if (ctxt == NULL)
821 return;
822
823 if (ctxt->string != NULL)
824 xmlFree(ctxt->string);
825 if (ctxt->states != NULL) {
826 for (i = 0;i < ctxt->nbStates;i++)
827 xmlRegFreeState(ctxt->states[i]);
828 xmlFree(ctxt->states);
829 }
830 if (ctxt->atoms != NULL) {
831 for (i = 0;i < ctxt->nbAtoms;i++)
832 xmlRegFreeAtom(ctxt->atoms[i]);
833 xmlFree(ctxt->atoms);
834 }
835 if (ctxt->counters != NULL)
836 xmlFree(ctxt->counters);
837 xmlFree(ctxt);
838}
839
840/************************************************************************
841 * *
842 * Display of Data structures *
843 * *
844 ************************************************************************/
845
846static void
847xmlRegPrintAtomType(FILE *output, xmlRegAtomType type) {
848 switch (type) {
849 case XML_REGEXP_EPSILON:
850 fprintf(output, "epsilon "); break;
851 case XML_REGEXP_CHARVAL:
852 fprintf(output, "charval "); break;
853 case XML_REGEXP_RANGES:
854 fprintf(output, "ranges "); break;
855 case XML_REGEXP_SUBREG:
856 fprintf(output, "subexpr "); break;
857 case XML_REGEXP_STRING:
858 fprintf(output, "string "); break;
859 case XML_REGEXP_ANYCHAR:
860 fprintf(output, "anychar "); break;
861 case XML_REGEXP_ANYSPACE:
862 fprintf(output, "anyspace "); break;
863 case XML_REGEXP_NOTSPACE:
864 fprintf(output, "notspace "); break;
865 case XML_REGEXP_INITNAME:
866 fprintf(output, "initname "); break;
867 case XML_REGEXP_NOTINITNAME:
868 fprintf(output, "notinitname "); break;
869 case XML_REGEXP_NAMECHAR:
870 fprintf(output, "namechar "); break;
871 case XML_REGEXP_NOTNAMECHAR:
872 fprintf(output, "notnamechar "); break;
873 case XML_REGEXP_DECIMAL:
874 fprintf(output, "decimal "); break;
875 case XML_REGEXP_NOTDECIMAL:
876 fprintf(output, "notdecimal "); break;
877 case XML_REGEXP_REALCHAR:
878 fprintf(output, "realchar "); break;
879 case XML_REGEXP_NOTREALCHAR:
880 fprintf(output, "notrealchar "); break;
881 case XML_REGEXP_LETTER:
882 fprintf(output, "LETTER "); break;
883 case XML_REGEXP_LETTER_UPPERCASE:
884 fprintf(output, "LETTER_UPPERCASE "); break;
885 case XML_REGEXP_LETTER_LOWERCASE:
886 fprintf(output, "LETTER_LOWERCASE "); break;
887 case XML_REGEXP_LETTER_TITLECASE:
888 fprintf(output, "LETTER_TITLECASE "); break;
889 case XML_REGEXP_LETTER_MODIFIER:
890 fprintf(output, "LETTER_MODIFIER "); break;
891 case XML_REGEXP_LETTER_OTHERS:
892 fprintf(output, "LETTER_OTHERS "); break;
893 case XML_REGEXP_MARK:
894 fprintf(output, "MARK "); break;
895 case XML_REGEXP_MARK_NONSPACING:
896 fprintf(output, "MARK_NONSPACING "); break;
897 case XML_REGEXP_MARK_SPACECOMBINING:
898 fprintf(output, "MARK_SPACECOMBINING "); break;
899 case XML_REGEXP_MARK_ENCLOSING:
900 fprintf(output, "MARK_ENCLOSING "); break;
901 case XML_REGEXP_NUMBER:
902 fprintf(output, "NUMBER "); break;
903 case XML_REGEXP_NUMBER_DECIMAL:
904 fprintf(output, "NUMBER_DECIMAL "); break;
905 case XML_REGEXP_NUMBER_LETTER:
906 fprintf(output, "NUMBER_LETTER "); break;
907 case XML_REGEXP_NUMBER_OTHERS:
908 fprintf(output, "NUMBER_OTHERS "); break;
909 case XML_REGEXP_PUNCT:
910 fprintf(output, "PUNCT "); break;
911 case XML_REGEXP_PUNCT_CONNECTOR:
912 fprintf(output, "PUNCT_CONNECTOR "); break;
913 case XML_REGEXP_PUNCT_DASH:
914 fprintf(output, "PUNCT_DASH "); break;
915 case XML_REGEXP_PUNCT_OPEN:
916 fprintf(output, "PUNCT_OPEN "); break;
917 case XML_REGEXP_PUNCT_CLOSE:
918 fprintf(output, "PUNCT_CLOSE "); break;
919 case XML_REGEXP_PUNCT_INITQUOTE:
920 fprintf(output, "PUNCT_INITQUOTE "); break;
921 case XML_REGEXP_PUNCT_FINQUOTE:
922 fprintf(output, "PUNCT_FINQUOTE "); break;
923 case XML_REGEXP_PUNCT_OTHERS:
924 fprintf(output, "PUNCT_OTHERS "); break;
925 case XML_REGEXP_SEPAR:
926 fprintf(output, "SEPAR "); break;
927 case XML_REGEXP_SEPAR_SPACE:
928 fprintf(output, "SEPAR_SPACE "); break;
929 case XML_REGEXP_SEPAR_LINE:
930 fprintf(output, "SEPAR_LINE "); break;
931 case XML_REGEXP_SEPAR_PARA:
932 fprintf(output, "SEPAR_PARA "); break;
933 case XML_REGEXP_SYMBOL:
934 fprintf(output, "SYMBOL "); break;
935 case XML_REGEXP_SYMBOL_MATH:
936 fprintf(output, "SYMBOL_MATH "); break;
937 case XML_REGEXP_SYMBOL_CURRENCY:
938 fprintf(output, "SYMBOL_CURRENCY "); break;
939 case XML_REGEXP_SYMBOL_MODIFIER:
940 fprintf(output, "SYMBOL_MODIFIER "); break;
941 case XML_REGEXP_SYMBOL_OTHERS:
942 fprintf(output, "SYMBOL_OTHERS "); break;
943 case XML_REGEXP_OTHER:
944 fprintf(output, "OTHER "); break;
945 case XML_REGEXP_OTHER_CONTROL:
946 fprintf(output, "OTHER_CONTROL "); break;
947 case XML_REGEXP_OTHER_FORMAT:
948 fprintf(output, "OTHER_FORMAT "); break;
949 case XML_REGEXP_OTHER_PRIVATE:
950 fprintf(output, "OTHER_PRIVATE "); break;
951 case XML_REGEXP_OTHER_NA:
952 fprintf(output, "OTHER_NA "); break;
953 case XML_REGEXP_BLOCK_NAME:
954 fprintf(output, "BLOCK "); break;
955 }
956}
957
958static void
959xmlRegPrintQuantType(FILE *output, xmlRegQuantType type) {
960 switch (type) {
961 case XML_REGEXP_QUANT_EPSILON:
962 fprintf(output, "epsilon "); break;
963 case XML_REGEXP_QUANT_ONCE:
964 fprintf(output, "once "); break;
965 case XML_REGEXP_QUANT_OPT:
966 fprintf(output, "? "); break;
967 case XML_REGEXP_QUANT_MULT:
968 fprintf(output, "* "); break;
969 case XML_REGEXP_QUANT_PLUS:
970 fprintf(output, "+ "); break;
971 case XML_REGEXP_QUANT_RANGE:
972 fprintf(output, "range "); break;
Daniel Veillard7646b182002-04-20 06:41:40 +0000973 case XML_REGEXP_QUANT_ONCEONLY:
974 fprintf(output, "onceonly "); break;
975 case XML_REGEXP_QUANT_ALL:
976 fprintf(output, "all "); break;
Daniel Veillard4255d502002-04-16 15:50:10 +0000977 }
978}
979static void
980xmlRegPrintRange(FILE *output, xmlRegRangePtr range) {
981 fprintf(output, " range: ");
982 if (range->neg)
983 fprintf(output, "negative ");
984 xmlRegPrintAtomType(output, range->type);
985 fprintf(output, "%c - %c\n", range->start, range->end);
986}
987
988static void
989xmlRegPrintAtom(FILE *output, xmlRegAtomPtr atom) {
990 fprintf(output, " atom: ");
991 if (atom == NULL) {
992 fprintf(output, "NULL\n");
993 return;
994 }
Daniel Veillard9efc4762005-07-19 14:33:55 +0000995 if (atom->neg)
996 fprintf(output, "not ");
Daniel Veillard4255d502002-04-16 15:50:10 +0000997 xmlRegPrintAtomType(output, atom->type);
998 xmlRegPrintQuantType(output, atom->quant);
999 if (atom->quant == XML_REGEXP_QUANT_RANGE)
1000 fprintf(output, "%d-%d ", atom->min, atom->max);
1001 if (atom->type == XML_REGEXP_STRING)
1002 fprintf(output, "'%s' ", (char *) atom->valuep);
1003 if (atom->type == XML_REGEXP_CHARVAL)
1004 fprintf(output, "char %c\n", atom->codepoint);
1005 else if (atom->type == XML_REGEXP_RANGES) {
1006 int i;
1007 fprintf(output, "%d entries\n", atom->nbRanges);
1008 for (i = 0; i < atom->nbRanges;i++)
1009 xmlRegPrintRange(output, atom->ranges[i]);
1010 } else if (atom->type == XML_REGEXP_SUBREG) {
1011 fprintf(output, "start %d end %d\n", atom->start->no, atom->stop->no);
1012 } else {
1013 fprintf(output, "\n");
1014 }
1015}
1016
1017static void
1018xmlRegPrintTrans(FILE *output, xmlRegTransPtr trans) {
1019 fprintf(output, " trans: ");
1020 if (trans == NULL) {
1021 fprintf(output, "NULL\n");
1022 return;
1023 }
1024 if (trans->to < 0) {
1025 fprintf(output, "removed\n");
1026 return;
1027 }
Daniel Veillard567a45b2005-10-18 19:11:55 +00001028 if (trans->nd != 0) {
1029 if (trans->nd == 2)
1030 fprintf(output, "last not determinist, ");
1031 else
1032 fprintf(output, "not determinist, ");
1033 }
Daniel Veillard4255d502002-04-16 15:50:10 +00001034 if (trans->counter >= 0) {
1035 fprintf(output, "counted %d, ", trans->counter);
1036 }
Daniel Veillard8a001f62002-04-20 07:24:11 +00001037 if (trans->count == REGEXP_ALL_COUNTER) {
1038 fprintf(output, "all transition, ");
1039 } else if (trans->count >= 0) {
Daniel Veillard4255d502002-04-16 15:50:10 +00001040 fprintf(output, "count based %d, ", trans->count);
1041 }
1042 if (trans->atom == NULL) {
1043 fprintf(output, "epsilon to %d\n", trans->to);
1044 return;
1045 }
1046 if (trans->atom->type == XML_REGEXP_CHARVAL)
1047 fprintf(output, "char %c ", trans->atom->codepoint);
1048 fprintf(output, "atom %d, to %d\n", trans->atom->no, trans->to);
1049}
1050
1051static void
1052xmlRegPrintState(FILE *output, xmlRegStatePtr state) {
1053 int i;
1054
1055 fprintf(output, " state: ");
1056 if (state == NULL) {
1057 fprintf(output, "NULL\n");
1058 return;
1059 }
1060 if (state->type == XML_REGEXP_START_STATE)
1061 fprintf(output, "START ");
1062 if (state->type == XML_REGEXP_FINAL_STATE)
1063 fprintf(output, "FINAL ");
1064
1065 fprintf(output, "%d, %d transitions:\n", state->no, state->nbTrans);
1066 for (i = 0;i < state->nbTrans; i++) {
1067 xmlRegPrintTrans(output, &(state->trans[i]));
1068 }
1069}
1070
Daniel Veillard23e73572002-09-19 19:56:43 +00001071#ifdef DEBUG_REGEXP_GRAPH
Daniel Veillard4255d502002-04-16 15:50:10 +00001072static void
1073xmlRegPrintCtxt(FILE *output, xmlRegParserCtxtPtr ctxt) {
1074 int i;
1075
1076 fprintf(output, " ctxt: ");
1077 if (ctxt == NULL) {
1078 fprintf(output, "NULL\n");
1079 return;
1080 }
1081 fprintf(output, "'%s' ", ctxt->string);
1082 if (ctxt->error)
1083 fprintf(output, "error ");
1084 if (ctxt->neg)
1085 fprintf(output, "neg ");
1086 fprintf(output, "\n");
1087 fprintf(output, "%d atoms:\n", ctxt->nbAtoms);
1088 for (i = 0;i < ctxt->nbAtoms; i++) {
1089 fprintf(output, " %02d ", i);
1090 xmlRegPrintAtom(output, ctxt->atoms[i]);
1091 }
1092 if (ctxt->atom != NULL) {
1093 fprintf(output, "current atom:\n");
1094 xmlRegPrintAtom(output, ctxt->atom);
1095 }
1096 fprintf(output, "%d states:", ctxt->nbStates);
1097 if (ctxt->start != NULL)
1098 fprintf(output, " start: %d", ctxt->start->no);
1099 if (ctxt->end != NULL)
1100 fprintf(output, " end: %d", ctxt->end->no);
1101 fprintf(output, "\n");
1102 for (i = 0;i < ctxt->nbStates; i++) {
1103 xmlRegPrintState(output, ctxt->states[i]);
1104 }
1105 fprintf(output, "%d counters:\n", ctxt->nbCounters);
1106 for (i = 0;i < ctxt->nbCounters; i++) {
1107 fprintf(output, " %d: min %d max %d\n", i, ctxt->counters[i].min,
1108 ctxt->counters[i].max);
1109 }
1110}
Daniel Veillard23e73572002-09-19 19:56:43 +00001111#endif
Daniel Veillard4255d502002-04-16 15:50:10 +00001112
1113/************************************************************************
1114 * *
1115 * Finite Automata structures manipulations *
1116 * *
1117 ************************************************************************/
1118
1119static void
1120xmlRegAtomAddRange(xmlRegParserCtxtPtr ctxt, xmlRegAtomPtr atom,
1121 int neg, xmlRegAtomType type, int start, int end,
1122 xmlChar *blockName) {
1123 xmlRegRangePtr range;
1124
1125 if (atom == NULL) {
1126 ERROR("add range: atom is NULL");
1127 return;
1128 }
1129 if (atom->type != XML_REGEXP_RANGES) {
1130 ERROR("add range: atom is not ranges");
1131 return;
1132 }
1133 if (atom->maxRanges == 0) {
1134 atom->maxRanges = 4;
1135 atom->ranges = (xmlRegRangePtr *) xmlMalloc(atom->maxRanges *
1136 sizeof(xmlRegRangePtr));
1137 if (atom->ranges == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00001138 xmlRegexpErrMemory(ctxt, "adding ranges");
Daniel Veillard4255d502002-04-16 15:50:10 +00001139 atom->maxRanges = 0;
1140 return;
1141 }
1142 } else if (atom->nbRanges >= atom->maxRanges) {
1143 xmlRegRangePtr *tmp;
1144 atom->maxRanges *= 2;
1145 tmp = (xmlRegRangePtr *) xmlRealloc(atom->ranges, atom->maxRanges *
1146 sizeof(xmlRegRangePtr));
1147 if (tmp == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00001148 xmlRegexpErrMemory(ctxt, "adding ranges");
Daniel Veillard4255d502002-04-16 15:50:10 +00001149 atom->maxRanges /= 2;
1150 return;
1151 }
1152 atom->ranges = tmp;
1153 }
1154 range = xmlRegNewRange(ctxt, neg, type, start, end);
1155 if (range == NULL)
1156 return;
1157 range->blockName = blockName;
1158 atom->ranges[atom->nbRanges++] = range;
1159
1160}
1161
1162static int
1163xmlRegGetCounter(xmlRegParserCtxtPtr ctxt) {
1164 if (ctxt->maxCounters == 0) {
1165 ctxt->maxCounters = 4;
1166 ctxt->counters = (xmlRegCounter *) xmlMalloc(ctxt->maxCounters *
1167 sizeof(xmlRegCounter));
1168 if (ctxt->counters == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00001169 xmlRegexpErrMemory(ctxt, "allocating counter");
Daniel Veillard4255d502002-04-16 15:50:10 +00001170 ctxt->maxCounters = 0;
1171 return(-1);
1172 }
1173 } else if (ctxt->nbCounters >= ctxt->maxCounters) {
1174 xmlRegCounter *tmp;
1175 ctxt->maxCounters *= 2;
1176 tmp = (xmlRegCounter *) xmlRealloc(ctxt->counters, ctxt->maxCounters *
1177 sizeof(xmlRegCounter));
1178 if (tmp == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00001179 xmlRegexpErrMemory(ctxt, "allocating counter");
Daniel Veillard4255d502002-04-16 15:50:10 +00001180 ctxt->maxCounters /= 2;
1181 return(-1);
1182 }
1183 ctxt->counters = tmp;
1184 }
1185 ctxt->counters[ctxt->nbCounters].min = -1;
1186 ctxt->counters[ctxt->nbCounters].max = -1;
1187 return(ctxt->nbCounters++);
1188}
1189
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001190static int
Daniel Veillard4255d502002-04-16 15:50:10 +00001191xmlRegAtomPush(xmlRegParserCtxtPtr ctxt, xmlRegAtomPtr atom) {
1192 if (atom == NULL) {
1193 ERROR("atom push: atom is NULL");
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001194 return(-1);
Daniel Veillard4255d502002-04-16 15:50:10 +00001195 }
1196 if (ctxt->maxAtoms == 0) {
1197 ctxt->maxAtoms = 4;
1198 ctxt->atoms = (xmlRegAtomPtr *) xmlMalloc(ctxt->maxAtoms *
1199 sizeof(xmlRegAtomPtr));
1200 if (ctxt->atoms == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00001201 xmlRegexpErrMemory(ctxt, "pushing atom");
Daniel Veillard4255d502002-04-16 15:50:10 +00001202 ctxt->maxAtoms = 0;
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001203 return(-1);
Daniel Veillard4255d502002-04-16 15:50:10 +00001204 }
1205 } else if (ctxt->nbAtoms >= ctxt->maxAtoms) {
1206 xmlRegAtomPtr *tmp;
1207 ctxt->maxAtoms *= 2;
1208 tmp = (xmlRegAtomPtr *) xmlRealloc(ctxt->atoms, ctxt->maxAtoms *
1209 sizeof(xmlRegAtomPtr));
1210 if (tmp == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00001211 xmlRegexpErrMemory(ctxt, "allocating counter");
Daniel Veillard4255d502002-04-16 15:50:10 +00001212 ctxt->maxAtoms /= 2;
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001213 return(-1);
Daniel Veillard4255d502002-04-16 15:50:10 +00001214 }
1215 ctxt->atoms = tmp;
1216 }
1217 atom->no = ctxt->nbAtoms;
1218 ctxt->atoms[ctxt->nbAtoms++] = atom;
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001219 return(0);
Daniel Veillard4255d502002-04-16 15:50:10 +00001220}
1221
1222static void
Daniel Veillarddb68b742005-07-30 13:18:24 +00001223xmlRegStateAddTransTo(xmlRegParserCtxtPtr ctxt, xmlRegStatePtr target,
1224 int from) {
1225 if (target->maxTransTo == 0) {
1226 target->maxTransTo = 8;
1227 target->transTo = (int *) xmlMalloc(target->maxTransTo *
1228 sizeof(int));
1229 if (target->transTo == NULL) {
1230 xmlRegexpErrMemory(ctxt, "adding transition");
1231 target->maxTransTo = 0;
1232 return;
1233 }
1234 } else if (target->nbTransTo >= target->maxTransTo) {
1235 int *tmp;
1236 target->maxTransTo *= 2;
1237 tmp = (int *) xmlRealloc(target->transTo, target->maxTransTo *
1238 sizeof(int));
1239 if (tmp == NULL) {
1240 xmlRegexpErrMemory(ctxt, "adding transition");
1241 target->maxTransTo /= 2;
1242 return;
1243 }
1244 target->transTo = tmp;
1245 }
1246 target->transTo[target->nbTransTo] = from;
1247 target->nbTransTo++;
1248}
1249
1250static void
Daniel Veillard4255d502002-04-16 15:50:10 +00001251xmlRegStateAddTrans(xmlRegParserCtxtPtr ctxt, xmlRegStatePtr state,
1252 xmlRegAtomPtr atom, xmlRegStatePtr target,
Daniel Veillard5de09382005-09-26 17:18:17 +00001253 int counter, int count) {
William M. Brackf9b5fa22004-05-10 07:52:15 +00001254
1255 int nrtrans;
1256
Daniel Veillard4255d502002-04-16 15:50:10 +00001257 if (state == NULL) {
1258 ERROR("add state: state is NULL");
1259 return;
1260 }
1261 if (target == NULL) {
1262 ERROR("add state: target is NULL");
1263 return;
1264 }
William M. Brackf9b5fa22004-05-10 07:52:15 +00001265 /*
1266 * Other routines follow the philosophy 'When in doubt, add a transition'
1267 * so we check here whether such a transition is already present and, if
1268 * so, silently ignore this request.
1269 */
1270
Daniel Veillard5de09382005-09-26 17:18:17 +00001271 for (nrtrans = state->nbTrans - 1; nrtrans >= 0; nrtrans--) {
1272 xmlRegTransPtr trans = &(state->trans[nrtrans]);
1273 if ((trans->atom == atom) &&
1274 (trans->to == target->no) &&
1275 (trans->counter == counter) &&
1276 (trans->count == count)) {
William M. Brackf9b5fa22004-05-10 07:52:15 +00001277#ifdef DEBUG_REGEXP_GRAPH
Daniel Veillard5de09382005-09-26 17:18:17 +00001278 printf("Ignoring duplicate transition from %d to %d\n",
1279 state->no, target->no);
William M. Brackf9b5fa22004-05-10 07:52:15 +00001280#endif
Daniel Veillard5de09382005-09-26 17:18:17 +00001281 return;
Daniel Veillarddb68b742005-07-30 13:18:24 +00001282 }
William M. Brackf9b5fa22004-05-10 07:52:15 +00001283 }
1284
Daniel Veillard4255d502002-04-16 15:50:10 +00001285 if (state->maxTrans == 0) {
Daniel Veillarddb68b742005-07-30 13:18:24 +00001286 state->maxTrans = 8;
Daniel Veillard4255d502002-04-16 15:50:10 +00001287 state->trans = (xmlRegTrans *) xmlMalloc(state->maxTrans *
1288 sizeof(xmlRegTrans));
1289 if (state->trans == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00001290 xmlRegexpErrMemory(ctxt, "adding transition");
Daniel Veillard4255d502002-04-16 15:50:10 +00001291 state->maxTrans = 0;
1292 return;
1293 }
1294 } else if (state->nbTrans >= state->maxTrans) {
1295 xmlRegTrans *tmp;
1296 state->maxTrans *= 2;
1297 tmp = (xmlRegTrans *) xmlRealloc(state->trans, state->maxTrans *
1298 sizeof(xmlRegTrans));
1299 if (tmp == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00001300 xmlRegexpErrMemory(ctxt, "adding transition");
Daniel Veillard4255d502002-04-16 15:50:10 +00001301 state->maxTrans /= 2;
1302 return;
1303 }
1304 state->trans = tmp;
1305 }
1306#ifdef DEBUG_REGEXP_GRAPH
1307 printf("Add trans from %d to %d ", state->no, target->no);
Daniel Veillard8a001f62002-04-20 07:24:11 +00001308 if (count == REGEXP_ALL_COUNTER)
Daniel Veillard2cbf5962004-03-31 15:50:43 +00001309 printf("all transition\n");
Daniel Veillard4402ab42002-09-12 16:02:56 +00001310 else if (count >= 0)
Daniel Veillard2cbf5962004-03-31 15:50:43 +00001311 printf("count based %d\n", count);
Daniel Veillard4255d502002-04-16 15:50:10 +00001312 else if (counter >= 0)
Daniel Veillard2cbf5962004-03-31 15:50:43 +00001313 printf("counted %d\n", counter);
Daniel Veillard4255d502002-04-16 15:50:10 +00001314 else if (atom == NULL)
Daniel Veillard2cbf5962004-03-31 15:50:43 +00001315 printf("epsilon transition\n");
1316 else if (atom != NULL)
1317 xmlRegPrintAtom(stdout, atom);
Daniel Veillard4255d502002-04-16 15:50:10 +00001318#endif
1319
1320 state->trans[state->nbTrans].atom = atom;
1321 state->trans[state->nbTrans].to = target->no;
1322 state->trans[state->nbTrans].counter = counter;
1323 state->trans[state->nbTrans].count = count;
Daniel Veillard567a45b2005-10-18 19:11:55 +00001324 state->trans[state->nbTrans].nd = 0;
Daniel Veillard4255d502002-04-16 15:50:10 +00001325 state->nbTrans++;
Daniel Veillarddb68b742005-07-30 13:18:24 +00001326 xmlRegStateAddTransTo(ctxt, target, state->no);
Daniel Veillard4255d502002-04-16 15:50:10 +00001327}
1328
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001329static int
Daniel Veillard4255d502002-04-16 15:50:10 +00001330xmlRegStatePush(xmlRegParserCtxtPtr ctxt, xmlRegStatePtr state) {
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001331 if (state == NULL) return(-1);
Daniel Veillard4255d502002-04-16 15:50:10 +00001332 if (ctxt->maxStates == 0) {
1333 ctxt->maxStates = 4;
1334 ctxt->states = (xmlRegStatePtr *) xmlMalloc(ctxt->maxStates *
1335 sizeof(xmlRegStatePtr));
1336 if (ctxt->states == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00001337 xmlRegexpErrMemory(ctxt, "adding state");
Daniel Veillard4255d502002-04-16 15:50:10 +00001338 ctxt->maxStates = 0;
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001339 return(-1);
Daniel Veillard4255d502002-04-16 15:50:10 +00001340 }
1341 } else if (ctxt->nbStates >= ctxt->maxStates) {
1342 xmlRegStatePtr *tmp;
1343 ctxt->maxStates *= 2;
1344 tmp = (xmlRegStatePtr *) xmlRealloc(ctxt->states, ctxt->maxStates *
1345 sizeof(xmlRegStatePtr));
1346 if (tmp == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00001347 xmlRegexpErrMemory(ctxt, "adding state");
Daniel Veillard4255d502002-04-16 15:50:10 +00001348 ctxt->maxStates /= 2;
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001349 return(-1);
Daniel Veillard4255d502002-04-16 15:50:10 +00001350 }
1351 ctxt->states = tmp;
1352 }
1353 state->no = ctxt->nbStates;
1354 ctxt->states[ctxt->nbStates++] = state;
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001355 return(0);
Daniel Veillard4255d502002-04-16 15:50:10 +00001356}
1357
1358/**
Daniel Veillard7646b182002-04-20 06:41:40 +00001359 * xmlFAGenerateAllTransition:
Daniel Veillard441bc322002-04-20 17:38:48 +00001360 * @ctxt: a regexp parser context
1361 * @from: the from state
1362 * @to: the target state or NULL for building a new one
1363 * @lax:
Daniel Veillard7646b182002-04-20 06:41:40 +00001364 *
1365 */
1366static void
1367xmlFAGenerateAllTransition(xmlRegParserCtxtPtr ctxt,
Daniel Veillard441bc322002-04-20 17:38:48 +00001368 xmlRegStatePtr from, xmlRegStatePtr to,
1369 int lax) {
Daniel Veillard7646b182002-04-20 06:41:40 +00001370 if (to == NULL) {
1371 to = xmlRegNewState(ctxt);
1372 xmlRegStatePush(ctxt, to);
1373 ctxt->state = to;
1374 }
Daniel Veillard441bc322002-04-20 17:38:48 +00001375 if (lax)
Daniel Veillard5de09382005-09-26 17:18:17 +00001376 xmlRegStateAddTrans(ctxt, from, NULL, to, -1, REGEXP_ALL_LAX_COUNTER);
Daniel Veillard441bc322002-04-20 17:38:48 +00001377 else
Daniel Veillard5de09382005-09-26 17:18:17 +00001378 xmlRegStateAddTrans(ctxt, from, NULL, to, -1, REGEXP_ALL_COUNTER);
Daniel Veillard7646b182002-04-20 06:41:40 +00001379}
1380
1381/**
Daniel Veillard4255d502002-04-16 15:50:10 +00001382 * xmlFAGenerateEpsilonTransition:
Daniel Veillard441bc322002-04-20 17:38:48 +00001383 * @ctxt: a regexp parser context
1384 * @from: the from state
1385 * @to: the target state or NULL for building a new one
Daniel Veillard4255d502002-04-16 15:50:10 +00001386 *
1387 */
1388static void
1389xmlFAGenerateEpsilonTransition(xmlRegParserCtxtPtr ctxt,
1390 xmlRegStatePtr from, xmlRegStatePtr to) {
1391 if (to == NULL) {
1392 to = xmlRegNewState(ctxt);
1393 xmlRegStatePush(ctxt, to);
1394 ctxt->state = to;
1395 }
Daniel Veillard5de09382005-09-26 17:18:17 +00001396 xmlRegStateAddTrans(ctxt, from, NULL, to, -1, -1);
Daniel Veillard4255d502002-04-16 15:50:10 +00001397}
1398
1399/**
1400 * xmlFAGenerateCountedEpsilonTransition:
Daniel Veillard441bc322002-04-20 17:38:48 +00001401 * @ctxt: a regexp parser context
1402 * @from: the from state
1403 * @to: the target state or NULL for building a new one
Daniel Veillard4255d502002-04-16 15:50:10 +00001404 * counter: the counter for that transition
1405 *
1406 */
1407static void
1408xmlFAGenerateCountedEpsilonTransition(xmlRegParserCtxtPtr ctxt,
1409 xmlRegStatePtr from, xmlRegStatePtr to, int counter) {
1410 if (to == NULL) {
1411 to = xmlRegNewState(ctxt);
1412 xmlRegStatePush(ctxt, to);
1413 ctxt->state = to;
1414 }
Daniel Veillard5de09382005-09-26 17:18:17 +00001415 xmlRegStateAddTrans(ctxt, from, NULL, to, counter, -1);
Daniel Veillard4255d502002-04-16 15:50:10 +00001416}
1417
1418/**
1419 * xmlFAGenerateCountedTransition:
Daniel Veillard441bc322002-04-20 17:38:48 +00001420 * @ctxt: a regexp parser context
1421 * @from: the from state
1422 * @to: the target state or NULL for building a new one
Daniel Veillard4255d502002-04-16 15:50:10 +00001423 * counter: the counter for that transition
1424 *
1425 */
1426static void
1427xmlFAGenerateCountedTransition(xmlRegParserCtxtPtr ctxt,
1428 xmlRegStatePtr from, xmlRegStatePtr to, int counter) {
1429 if (to == NULL) {
1430 to = xmlRegNewState(ctxt);
1431 xmlRegStatePush(ctxt, to);
1432 ctxt->state = to;
1433 }
Daniel Veillard5de09382005-09-26 17:18:17 +00001434 xmlRegStateAddTrans(ctxt, from, NULL, to, -1, counter);
Daniel Veillard4255d502002-04-16 15:50:10 +00001435}
1436
1437/**
1438 * xmlFAGenerateTransitions:
Daniel Veillard441bc322002-04-20 17:38:48 +00001439 * @ctxt: a regexp parser context
1440 * @from: the from state
1441 * @to: the target state or NULL for building a new one
1442 * @atom: the atom generating the transition
Daniel Veillard4255d502002-04-16 15:50:10 +00001443 *
William M. Brackddf71d62004-05-06 04:17:26 +00001444 * Returns 0 if success and -1 in case of error.
Daniel Veillard4255d502002-04-16 15:50:10 +00001445 */
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001446static int
Daniel Veillard4255d502002-04-16 15:50:10 +00001447xmlFAGenerateTransitions(xmlRegParserCtxtPtr ctxt, xmlRegStatePtr from,
1448 xmlRegStatePtr to, xmlRegAtomPtr atom) {
1449 if (atom == NULL) {
1450 ERROR("genrate transition: atom == NULL");
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001451 return(-1);
Daniel Veillard4255d502002-04-16 15:50:10 +00001452 }
1453 if (atom->type == XML_REGEXP_SUBREG) {
1454 /*
1455 * this is a subexpression handling one should not need to
William M. Brackddf71d62004-05-06 04:17:26 +00001456 * create a new node except for XML_REGEXP_QUANT_RANGE.
Daniel Veillard4255d502002-04-16 15:50:10 +00001457 */
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001458 if (xmlRegAtomPush(ctxt, atom) < 0) {
1459 return(-1);
1460 }
Daniel Veillard4255d502002-04-16 15:50:10 +00001461 if ((to != NULL) && (atom->stop != to) &&
1462 (atom->quant != XML_REGEXP_QUANT_RANGE)) {
1463 /*
1464 * Generate an epsilon transition to link to the target
1465 */
1466 xmlFAGenerateEpsilonTransition(ctxt, atom->stop, to);
Daniel Veillardaa622012005-10-20 15:55:25 +00001467#ifdef DV
1468 } else if ((to == NULL) && (atom->quant != XML_REGEXP_QUANT_RANGE) &&
1469 (atom->quant != XML_REGEXP_QUANT_ONCE)) {
1470 to = xmlRegNewState(ctxt);
1471 xmlRegStatePush(ctxt, to);
1472 ctxt->state = to;
1473 xmlFAGenerateEpsilonTransition(ctxt, atom->stop, to);
1474#endif
Daniel Veillard4255d502002-04-16 15:50:10 +00001475 }
1476 switch (atom->quant) {
1477 case XML_REGEXP_QUANT_OPT:
1478 atom->quant = XML_REGEXP_QUANT_ONCE;
1479 xmlFAGenerateEpsilonTransition(ctxt, atom->start, atom->stop);
1480 break;
1481 case XML_REGEXP_QUANT_MULT:
1482 atom->quant = XML_REGEXP_QUANT_ONCE;
1483 xmlFAGenerateEpsilonTransition(ctxt, atom->start, atom->stop);
1484 xmlFAGenerateEpsilonTransition(ctxt, atom->stop, atom->start);
1485 break;
1486 case XML_REGEXP_QUANT_PLUS:
1487 atom->quant = XML_REGEXP_QUANT_ONCE;
1488 xmlFAGenerateEpsilonTransition(ctxt, atom->stop, atom->start);
1489 break;
1490 case XML_REGEXP_QUANT_RANGE: {
1491 int counter;
1492 xmlRegStatePtr newstate;
1493
1494 /*
1495 * This one is nasty:
William M. Brackddf71d62004-05-06 04:17:26 +00001496 * 1/ if range has minOccurs == 0, create a new state
1497 * and create epsilon transitions from atom->start
1498 * to atom->stop, as well as atom->start to the new
1499 * state
1500 * 2/ register a new counter
1501 * 3/ register an epsilon transition associated to
Daniel Veillard4255d502002-04-16 15:50:10 +00001502 * this counter going from atom->stop to atom->start
William M. Brackddf71d62004-05-06 04:17:26 +00001503 * 4/ create a new state
1504 * 5/ generate a counted transition from atom->stop to
Daniel Veillard4255d502002-04-16 15:50:10 +00001505 * that state
1506 */
William M. Brackddf71d62004-05-06 04:17:26 +00001507 if (atom->min == 0) {
1508 xmlFAGenerateEpsilonTransition(ctxt, atom->start,
1509 atom->stop);
1510 newstate = xmlRegNewState(ctxt);
1511 xmlRegStatePush(ctxt, newstate);
1512 ctxt->state = newstate;
1513 xmlFAGenerateEpsilonTransition(ctxt, atom->start,
1514 newstate);
1515 }
Daniel Veillard4255d502002-04-16 15:50:10 +00001516 counter = xmlRegGetCounter(ctxt);
1517 ctxt->counters[counter].min = atom->min - 1;
1518 ctxt->counters[counter].max = atom->max - 1;
1519 atom->min = 0;
1520 atom->max = 0;
1521 atom->quant = XML_REGEXP_QUANT_ONCE;
1522 xmlFAGenerateCountedEpsilonTransition(ctxt, atom->stop,
1523 atom->start, counter);
1524 if (to != NULL) {
1525 newstate = to;
1526 } else {
1527 newstate = xmlRegNewState(ctxt);
1528 xmlRegStatePush(ctxt, newstate);
Daniel Veillard4255d502002-04-16 15:50:10 +00001529 }
Daniel Veillard9a00fd22005-11-09 08:56:26 +00001530 ctxt->state = newstate;
Daniel Veillard4255d502002-04-16 15:50:10 +00001531 xmlFAGenerateCountedTransition(ctxt, atom->stop,
1532 newstate, counter);
1533 }
1534 default:
1535 break;
1536 }
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001537 return(0);
Daniel Veillard567a45b2005-10-18 19:11:55 +00001538 }
1539 if ((atom->min == 0) && (atom->max == 0) &&
Daniel Veillard99c394d2005-07-14 12:58:49 +00001540 (atom->quant == XML_REGEXP_QUANT_RANGE)) {
1541 /*
1542 * we can discard the atom and generate an epsilon transition instead
1543 */
1544 if (to == NULL) {
1545 to = xmlRegNewState(ctxt);
1546 if (to != NULL)
1547 xmlRegStatePush(ctxt, to);
1548 else {
1549 return(-1);
1550 }
1551 }
1552 xmlFAGenerateEpsilonTransition(ctxt, from, to);
1553 ctxt->state = to;
1554 xmlRegFreeAtom(atom);
1555 return(0);
Daniel Veillard567a45b2005-10-18 19:11:55 +00001556 }
1557 if (to == NULL) {
1558 to = xmlRegNewState(ctxt);
1559 if (to != NULL)
1560 xmlRegStatePush(ctxt, to);
1561 else {
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001562 return(-1);
Daniel Veillard4255d502002-04-16 15:50:10 +00001563 }
Daniel Veillard4255d502002-04-16 15:50:10 +00001564 }
Daniel Veillard567a45b2005-10-18 19:11:55 +00001565 if (xmlRegAtomPush(ctxt, atom) < 0) {
1566 return(-1);
1567 }
1568 xmlRegStateAddTrans(ctxt, from, atom, to, -1, -1);
1569 ctxt->state = to;
Daniel Veillard4255d502002-04-16 15:50:10 +00001570 switch (atom->quant) {
1571 case XML_REGEXP_QUANT_OPT:
1572 atom->quant = XML_REGEXP_QUANT_ONCE;
1573 xmlFAGenerateEpsilonTransition(ctxt, from, to);
1574 break;
1575 case XML_REGEXP_QUANT_MULT:
1576 atom->quant = XML_REGEXP_QUANT_ONCE;
1577 xmlFAGenerateEpsilonTransition(ctxt, from, to);
Daniel Veillard5de09382005-09-26 17:18:17 +00001578 xmlRegStateAddTrans(ctxt, to, atom, to, -1, -1);
Daniel Veillard4255d502002-04-16 15:50:10 +00001579 break;
1580 case XML_REGEXP_QUANT_PLUS:
1581 atom->quant = XML_REGEXP_QUANT_ONCE;
Daniel Veillard5de09382005-09-26 17:18:17 +00001582 xmlRegStateAddTrans(ctxt, to, atom, to, -1, -1);
Daniel Veillard4255d502002-04-16 15:50:10 +00001583 break;
1584 default:
1585 break;
1586 }
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001587 return(0);
Daniel Veillard4255d502002-04-16 15:50:10 +00001588}
1589
1590/**
1591 * xmlFAReduceEpsilonTransitions:
Daniel Veillard441bc322002-04-20 17:38:48 +00001592 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00001593 * @fromnr: the from state
1594 * @tonr: the to state
William M. Brackddf71d62004-05-06 04:17:26 +00001595 * @counter: should that transition be associated to a counted
Daniel Veillard4255d502002-04-16 15:50:10 +00001596 *
1597 */
1598static void
1599xmlFAReduceEpsilonTransitions(xmlRegParserCtxtPtr ctxt, int fromnr,
1600 int tonr, int counter) {
1601 int transnr;
1602 xmlRegStatePtr from;
1603 xmlRegStatePtr to;
1604
1605#ifdef DEBUG_REGEXP_GRAPH
1606 printf("xmlFAReduceEpsilonTransitions(%d, %d)\n", fromnr, tonr);
1607#endif
1608 from = ctxt->states[fromnr];
1609 if (from == NULL)
1610 return;
1611 to = ctxt->states[tonr];
1612 if (to == NULL)
1613 return;
1614 if ((to->mark == XML_REGEXP_MARK_START) ||
1615 (to->mark == XML_REGEXP_MARK_VISITED))
1616 return;
1617
1618 to->mark = XML_REGEXP_MARK_VISITED;
1619 if (to->type == XML_REGEXP_FINAL_STATE) {
1620#ifdef DEBUG_REGEXP_GRAPH
1621 printf("State %d is final, so %d becomes final\n", tonr, fromnr);
1622#endif
1623 from->type = XML_REGEXP_FINAL_STATE;
1624 }
1625 for (transnr = 0;transnr < to->nbTrans;transnr++) {
Daniel Veillarddb68b742005-07-30 13:18:24 +00001626 if (to->trans[transnr].to < 0)
1627 continue;
Daniel Veillard4255d502002-04-16 15:50:10 +00001628 if (to->trans[transnr].atom == NULL) {
1629 /*
1630 * Don't remove counted transitions
1631 * Don't loop either
1632 */
Daniel Veillardb509f152002-04-17 16:28:10 +00001633 if (to->trans[transnr].to != fromnr) {
1634 if (to->trans[transnr].count >= 0) {
1635 int newto = to->trans[transnr].to;
1636
1637 xmlRegStateAddTrans(ctxt, from, NULL,
1638 ctxt->states[newto],
Daniel Veillard5de09382005-09-26 17:18:17 +00001639 -1, to->trans[transnr].count);
Daniel Veillardb509f152002-04-17 16:28:10 +00001640 } else {
Daniel Veillard4255d502002-04-16 15:50:10 +00001641#ifdef DEBUG_REGEXP_GRAPH
Daniel Veillardb509f152002-04-17 16:28:10 +00001642 printf("Found epsilon trans %d from %d to %d\n",
1643 transnr, tonr, to->trans[transnr].to);
Daniel Veillard4255d502002-04-16 15:50:10 +00001644#endif
Daniel Veillardb509f152002-04-17 16:28:10 +00001645 if (to->trans[transnr].counter >= 0) {
1646 xmlFAReduceEpsilonTransitions(ctxt, fromnr,
1647 to->trans[transnr].to,
1648 to->trans[transnr].counter);
1649 } else {
1650 xmlFAReduceEpsilonTransitions(ctxt, fromnr,
1651 to->trans[transnr].to,
1652 counter);
1653 }
1654 }
Daniel Veillard4255d502002-04-16 15:50:10 +00001655 }
1656 } else {
1657 int newto = to->trans[transnr].to;
1658
Daniel Veillardb509f152002-04-17 16:28:10 +00001659 if (to->trans[transnr].counter >= 0) {
1660 xmlRegStateAddTrans(ctxt, from, to->trans[transnr].atom,
1661 ctxt->states[newto],
Daniel Veillard5de09382005-09-26 17:18:17 +00001662 to->trans[transnr].counter, -1);
Daniel Veillardb509f152002-04-17 16:28:10 +00001663 } else {
1664 xmlRegStateAddTrans(ctxt, from, to->trans[transnr].atom,
Daniel Veillard5de09382005-09-26 17:18:17 +00001665 ctxt->states[newto], counter, -1);
Daniel Veillardb509f152002-04-17 16:28:10 +00001666 }
Daniel Veillard4255d502002-04-16 15:50:10 +00001667 }
1668 }
1669 to->mark = XML_REGEXP_MARK_NORMAL;
1670}
1671
1672/**
Daniel Veillarddb68b742005-07-30 13:18:24 +00001673 * xmlFAEliminateSimpleEpsilonTransitions:
1674 * @ctxt: a regexp parser context
1675 *
1676 * Eliminating general epsilon transitions can get costly in the general
1677 * algorithm due to the large amount of generated new transitions and
1678 * associated comparisons. However for simple epsilon transition used just
1679 * to separate building blocks when generating the automata this can be
1680 * reduced to state elimination:
1681 * - if there exists an epsilon from X to Y
1682 * - if there is no other transition from X
1683 * then X and Y are semantically equivalent and X can be eliminated
1684 * If X is the start state then make Y the start state, else replace the
1685 * target of all transitions to X by transitions to Y.
1686 */
1687static void
1688xmlFAEliminateSimpleEpsilonTransitions(xmlRegParserCtxtPtr ctxt) {
1689 int statenr, i, j, newto;
1690 xmlRegStatePtr state, tmp;
1691
1692 for (statenr = 0;statenr < ctxt->nbStates;statenr++) {
1693 state = ctxt->states[statenr];
1694 if (state == NULL)
1695 continue;
1696 if (state->nbTrans != 1)
1697 continue;
1698 /* is the only transition out a basic transition */
1699 if ((state->trans[0].atom == NULL) &&
1700 (state->trans[0].to >= 0) &&
1701 (state->trans[0].to != statenr) &&
1702 (state->trans[0].counter < 0) &&
1703 (state->trans[0].count < 0)) {
1704 newto = state->trans[0].to;
1705
1706 if (state->type == XML_REGEXP_START_STATE) {
1707#ifdef DEBUG_REGEXP_GRAPH
1708 printf("Found simple epsilon trans from start %d to %d\n",
1709 statenr, newto);
1710#endif
1711 } else {
1712#ifdef DEBUG_REGEXP_GRAPH
1713 printf("Found simple epsilon trans from %d to %d\n",
1714 statenr, newto);
1715#endif
1716 for (i = 0;i < state->nbTransTo;i++) {
1717 tmp = ctxt->states[state->transTo[i]];
1718 for (j = 0;j < tmp->nbTrans;j++) {
1719 if (tmp->trans[j].to == statenr) {
1720 tmp->trans[j].to = newto;
1721#ifdef DEBUG_REGEXP_GRAPH
1722 printf("Changed transition %d on %d to go to %d\n",
1723 j, tmp->no, newto);
1724#endif
1725 xmlRegStateAddTransTo(ctxt, ctxt->states[newto],
1726 tmp->no);
1727 }
1728 }
1729 }
1730#if 0
1731 for (i = 0;i < ctxt->nbStates;i++) {
1732 tmp = ctxt->states[i];
1733 for (j = 0;j < tmp->nbTrans;j++) {
1734 if (tmp->trans[j].to == statenr) {
1735 tmp->trans[j].to = newto;
1736#ifdef DEBUG_REGEXP_GRAPH
1737 printf("Changed transition %d on %d to go to %d\n",
1738 j, tmp->no, newto);
1739#endif
1740 }
1741 }
1742 }
1743#endif
1744 if (state->type == XML_REGEXP_FINAL_STATE)
1745 ctxt->states[newto]->type = XML_REGEXP_FINAL_STATE;
1746 /* eliminate the transition completely */
1747 state->nbTrans = 0;
1748
1749
1750 }
1751
1752 }
1753 }
1754}
1755/**
Daniel Veillard4255d502002-04-16 15:50:10 +00001756 * xmlFAEliminateEpsilonTransitions:
Daniel Veillard441bc322002-04-20 17:38:48 +00001757 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00001758 *
1759 */
1760static void
1761xmlFAEliminateEpsilonTransitions(xmlRegParserCtxtPtr ctxt) {
1762 int statenr, transnr;
1763 xmlRegStatePtr state;
Daniel Veillarddb68b742005-07-30 13:18:24 +00001764 int has_epsilon;
Daniel Veillard4255d502002-04-16 15:50:10 +00001765
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001766 if (ctxt->states == NULL) return;
1767
Daniel Veillarddb68b742005-07-30 13:18:24 +00001768 xmlFAEliminateSimpleEpsilonTransitions(ctxt);
1769
1770 has_epsilon = 0;
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001771
Daniel Veillard4255d502002-04-16 15:50:10 +00001772 /*
1773 * build the completed transitions bypassing the epsilons
1774 * Use a marking algorithm to avoid loops
Daniel Veillardcc026dc2005-01-12 13:21:17 +00001775 * mark sink states too.
Daniel Veillard4255d502002-04-16 15:50:10 +00001776 */
1777 for (statenr = 0;statenr < ctxt->nbStates;statenr++) {
1778 state = ctxt->states[statenr];
1779 if (state == NULL)
1780 continue;
Daniel Veillardcc026dc2005-01-12 13:21:17 +00001781 if ((state->nbTrans == 0) &&
1782 (state->type != XML_REGEXP_FINAL_STATE)) {
1783 state->type = XML_REGEXP_SINK_STATE;
1784 }
Daniel Veillard4255d502002-04-16 15:50:10 +00001785 for (transnr = 0;transnr < state->nbTrans;transnr++) {
1786 if ((state->trans[transnr].atom == NULL) &&
1787 (state->trans[transnr].to >= 0)) {
1788 if (state->trans[transnr].to == statenr) {
1789 state->trans[transnr].to = -1;
1790#ifdef DEBUG_REGEXP_GRAPH
1791 printf("Removed loopback epsilon trans %d on %d\n",
1792 transnr, statenr);
1793#endif
1794 } else if (state->trans[transnr].count < 0) {
1795 int newto = state->trans[transnr].to;
1796
1797#ifdef DEBUG_REGEXP_GRAPH
1798 printf("Found epsilon trans %d from %d to %d\n",
1799 transnr, statenr, newto);
1800#endif
1801 state->mark = XML_REGEXP_MARK_START;
Daniel Veillarddb68b742005-07-30 13:18:24 +00001802 has_epsilon = 1;
Daniel Veillard4255d502002-04-16 15:50:10 +00001803 xmlFAReduceEpsilonTransitions(ctxt, statenr,
1804 newto, state->trans[transnr].counter);
1805 state->mark = XML_REGEXP_MARK_NORMAL;
1806#ifdef DEBUG_REGEXP_GRAPH
1807 } else {
1808 printf("Found counted transition %d on %d\n",
1809 transnr, statenr);
1810#endif
1811 }
1812 }
1813 }
1814 }
1815 /*
1816 * Eliminate the epsilon transitions
1817 */
Daniel Veillarddb68b742005-07-30 13:18:24 +00001818 if (has_epsilon) {
1819 for (statenr = 0;statenr < ctxt->nbStates;statenr++) {
1820 state = ctxt->states[statenr];
1821 if (state == NULL)
1822 continue;
1823 for (transnr = 0;transnr < state->nbTrans;transnr++) {
1824 xmlRegTransPtr trans = &(state->trans[transnr]);
1825 if ((trans->atom == NULL) &&
1826 (trans->count < 0) &&
1827 (trans->to >= 0)) {
1828 trans->to = -1;
1829 }
Daniel Veillard4255d502002-04-16 15:50:10 +00001830 }
1831 }
1832 }
Daniel Veillard23e73572002-09-19 19:56:43 +00001833
1834 /*
1835 * Use this pass to detect unreachable states too
1836 */
1837 for (statenr = 0;statenr < ctxt->nbStates;statenr++) {
1838 state = ctxt->states[statenr];
1839 if (state != NULL)
William M. Brack779af002003-08-01 15:55:39 +00001840 state->reached = XML_REGEXP_MARK_NORMAL;
Daniel Veillard23e73572002-09-19 19:56:43 +00001841 }
1842 state = ctxt->states[0];
1843 if (state != NULL)
William M. Brack779af002003-08-01 15:55:39 +00001844 state->reached = XML_REGEXP_MARK_START;
Daniel Veillard23e73572002-09-19 19:56:43 +00001845 while (state != NULL) {
1846 xmlRegStatePtr target = NULL;
William M. Brack779af002003-08-01 15:55:39 +00001847 state->reached = XML_REGEXP_MARK_VISITED;
Daniel Veillard23e73572002-09-19 19:56:43 +00001848 /*
William M. Brackddf71d62004-05-06 04:17:26 +00001849 * Mark all states reachable from the current reachable state
Daniel Veillard23e73572002-09-19 19:56:43 +00001850 */
1851 for (transnr = 0;transnr < state->nbTrans;transnr++) {
1852 if ((state->trans[transnr].to >= 0) &&
1853 ((state->trans[transnr].atom != NULL) ||
1854 (state->trans[transnr].count >= 0))) {
1855 int newto = state->trans[transnr].to;
1856
1857 if (ctxt->states[newto] == NULL)
1858 continue;
William M. Brack779af002003-08-01 15:55:39 +00001859 if (ctxt->states[newto]->reached == XML_REGEXP_MARK_NORMAL) {
1860 ctxt->states[newto]->reached = XML_REGEXP_MARK_START;
Daniel Veillard23e73572002-09-19 19:56:43 +00001861 target = ctxt->states[newto];
1862 }
1863 }
1864 }
Daniel Veillardcc026dc2005-01-12 13:21:17 +00001865
Daniel Veillard23e73572002-09-19 19:56:43 +00001866 /*
1867 * find the next accessible state not explored
1868 */
1869 if (target == NULL) {
1870 for (statenr = 1;statenr < ctxt->nbStates;statenr++) {
1871 state = ctxt->states[statenr];
William M. Brack779af002003-08-01 15:55:39 +00001872 if ((state != NULL) && (state->reached ==
1873 XML_REGEXP_MARK_START)) {
Daniel Veillard23e73572002-09-19 19:56:43 +00001874 target = state;
1875 break;
1876 }
1877 }
1878 }
1879 state = target;
1880 }
1881 for (statenr = 0;statenr < ctxt->nbStates;statenr++) {
1882 state = ctxt->states[statenr];
William M. Brack779af002003-08-01 15:55:39 +00001883 if ((state != NULL) && (state->reached == XML_REGEXP_MARK_NORMAL)) {
Daniel Veillard23e73572002-09-19 19:56:43 +00001884#ifdef DEBUG_REGEXP_GRAPH
1885 printf("Removed unreachable state %d\n", statenr);
1886#endif
1887 xmlRegFreeState(state);
1888 ctxt->states[statenr] = NULL;
1889 }
1890 }
1891
Daniel Veillard4255d502002-04-16 15:50:10 +00001892}
1893
Daniel Veillard567a45b2005-10-18 19:11:55 +00001894static int
1895xmlFACompareRanges(xmlRegRangePtr range1, xmlRegRangePtr range2) {
1896 int ret = 0;
1897
1898 if ((range1->type == XML_REGEXP_RANGES) ||
1899 (range2->type == XML_REGEXP_RANGES) ||
1900 (range2->type == XML_REGEXP_SUBREG) ||
1901 (range1->type == XML_REGEXP_SUBREG) ||
1902 (range1->type == XML_REGEXP_STRING) ||
1903 (range2->type == XML_REGEXP_STRING))
1904 return(-1);
1905
1906 /* put them in order */
1907 if (range1->type > range2->type) {
1908 xmlRegRangePtr tmp;
1909
1910 tmp = range1;
1911 range1 = range2;
1912 range2 = tmp;
1913 }
1914 if ((range1->type == XML_REGEXP_ANYCHAR) ||
1915 (range2->type == XML_REGEXP_ANYCHAR)) {
1916 ret = 1;
1917 } else if ((range1->type == XML_REGEXP_EPSILON) ||
1918 (range2->type == XML_REGEXP_EPSILON)) {
1919 return(0);
1920 } else if (range1->type == range2->type) {
1921 if ((range1->type != XML_REGEXP_CHARVAL) ||
1922 (range1->end < range2->start) ||
1923 (range2->end < range1->start))
1924 ret = 1;
1925 else
1926 ret = 0;
1927 } else if (range1->type == XML_REGEXP_CHARVAL) {
1928 int codepoint;
1929 int neg = 0;
1930
1931 /*
1932 * just check all codepoints in the range for acceptance,
1933 * this is usually way cheaper since done only once at
1934 * compilation than testing over and over at runtime or
1935 * pushing too many states when evaluating.
1936 */
1937 if (((range1->neg == 0) && (range2->neg != 0)) ||
1938 ((range1->neg != 0) && (range2->neg == 0)))
1939 neg = 1;
1940
1941 for (codepoint = range1->start;codepoint <= range1->end ;codepoint++) {
1942 ret = xmlRegCheckCharacterRange(range2->type, codepoint,
1943 0, range2->start, range2->end,
1944 range2->blockName);
1945 if (ret < 0)
1946 return(-1);
1947 if (((neg == 1) && (ret == 0)) ||
1948 ((neg == 0) && (ret == 1)))
1949 return(1);
1950 }
1951 return(0);
1952 } else if ((range1->type == XML_REGEXP_BLOCK_NAME) ||
1953 (range2->type == XML_REGEXP_BLOCK_NAME)) {
1954 if (range1->type == range2->type) {
1955 ret = xmlStrEqual(range1->blockName, range2->blockName);
1956 } else {
1957 /*
1958 * comparing a block range with anything else is way
1959 * too costly, and maintining the table is like too much
1960 * memory too, so let's force the automata to save state
1961 * here.
1962 */
1963 return(1);
1964 }
1965 } else if ((range1->type < XML_REGEXP_LETTER) ||
1966 (range2->type < XML_REGEXP_LETTER)) {
1967 if ((range1->type == XML_REGEXP_ANYSPACE) &&
1968 (range2->type == XML_REGEXP_NOTSPACE))
1969 ret = 0;
1970 else if ((range1->type == XML_REGEXP_INITNAME) &&
1971 (range2->type == XML_REGEXP_NOTINITNAME))
1972 ret = 0;
1973 else if ((range1->type == XML_REGEXP_NAMECHAR) &&
1974 (range2->type == XML_REGEXP_NOTNAMECHAR))
1975 ret = 0;
1976 else if ((range1->type == XML_REGEXP_DECIMAL) &&
1977 (range2->type == XML_REGEXP_NOTDECIMAL))
1978 ret = 0;
1979 else if ((range1->type == XML_REGEXP_REALCHAR) &&
1980 (range2->type == XML_REGEXP_NOTREALCHAR))
1981 ret = 0;
1982 else {
1983 /* same thing to limit complexity */
1984 return(1);
1985 }
1986 } else {
1987 ret = 0;
1988 /* range1->type < range2->type here */
1989 switch (range1->type) {
1990 case XML_REGEXP_LETTER:
1991 /* all disjoint except in the subgroups */
1992 if ((range2->type == XML_REGEXP_LETTER_UPPERCASE) ||
1993 (range2->type == XML_REGEXP_LETTER_LOWERCASE) ||
1994 (range2->type == XML_REGEXP_LETTER_TITLECASE) ||
1995 (range2->type == XML_REGEXP_LETTER_MODIFIER) ||
1996 (range2->type == XML_REGEXP_LETTER_OTHERS))
1997 ret = 1;
1998 break;
1999 case XML_REGEXP_MARK:
2000 if ((range2->type == XML_REGEXP_MARK_NONSPACING) ||
2001 (range2->type == XML_REGEXP_MARK_SPACECOMBINING) ||
2002 (range2->type == XML_REGEXP_MARK_ENCLOSING))
2003 ret = 1;
2004 break;
2005 case XML_REGEXP_NUMBER:
2006 if ((range2->type == XML_REGEXP_NUMBER_DECIMAL) ||
2007 (range2->type == XML_REGEXP_NUMBER_LETTER) ||
2008 (range2->type == XML_REGEXP_NUMBER_OTHERS))
2009 ret = 1;
2010 break;
2011 case XML_REGEXP_PUNCT:
2012 if ((range2->type == XML_REGEXP_PUNCT_CONNECTOR) ||
2013 (range2->type == XML_REGEXP_PUNCT_DASH) ||
2014 (range2->type == XML_REGEXP_PUNCT_OPEN) ||
2015 (range2->type == XML_REGEXP_PUNCT_CLOSE) ||
2016 (range2->type == XML_REGEXP_PUNCT_INITQUOTE) ||
2017 (range2->type == XML_REGEXP_PUNCT_FINQUOTE) ||
2018 (range2->type == XML_REGEXP_PUNCT_OTHERS))
2019 ret = 1;
2020 break;
2021 case XML_REGEXP_SEPAR:
2022 if ((range2->type == XML_REGEXP_SEPAR_SPACE) ||
2023 (range2->type == XML_REGEXP_SEPAR_LINE) ||
2024 (range2->type == XML_REGEXP_SEPAR_PARA))
2025 ret = 1;
2026 break;
2027 case XML_REGEXP_SYMBOL:
2028 if ((range2->type == XML_REGEXP_SYMBOL_MATH) ||
2029 (range2->type == XML_REGEXP_SYMBOL_CURRENCY) ||
2030 (range2->type == XML_REGEXP_SYMBOL_MODIFIER) ||
2031 (range2->type == XML_REGEXP_SYMBOL_OTHERS))
2032 ret = 1;
2033 break;
2034 case XML_REGEXP_OTHER:
2035 if ((range2->type == XML_REGEXP_OTHER_CONTROL) ||
2036 (range2->type == XML_REGEXP_OTHER_FORMAT) ||
2037 (range2->type == XML_REGEXP_OTHER_PRIVATE))
2038 ret = 1;
2039 break;
2040 default:
2041 if ((range2->type >= XML_REGEXP_LETTER) &&
2042 (range2->type < XML_REGEXP_BLOCK_NAME))
2043 ret = 0;
2044 else {
2045 /* safety net ! */
2046 return(1);
2047 }
2048 }
2049 }
2050 if (((range1->neg == 0) && (range2->neg != 0)) ||
2051 ((range1->neg != 0) && (range2->neg == 0)))
2052 ret = !ret;
2053 return(1);
2054}
2055
Daniel Veillarde19fc232002-04-22 16:01:24 +00002056/**
2057 * xmlFACompareAtoms:
2058 * @atom1: an atom
2059 * @atom2: an atom
2060 *
Daniel Veillard567a45b2005-10-18 19:11:55 +00002061 * Compares two atoms to check whether they intersect in some ways,
2062 * this is used by xmlFAComputesDeterminism only
Daniel Veillarde19fc232002-04-22 16:01:24 +00002063 *
2064 * Returns 1 if yes and 0 otherwise
2065 */
2066static int
2067xmlFACompareAtoms(xmlRegAtomPtr atom1, xmlRegAtomPtr atom2) {
Daniel Veillard9efc4762005-07-19 14:33:55 +00002068 int ret;
2069
Daniel Veillarde19fc232002-04-22 16:01:24 +00002070 if (atom1 == atom2)
2071 return(1);
2072 if ((atom1 == NULL) || (atom2 == NULL))
2073 return(0);
2074
Daniel Veillard567a45b2005-10-18 19:11:55 +00002075 if ((atom1->type == XML_REGEXP_RANGES) &&
2076 (atom2->type == XML_REGEXP_CHARVAL)) {
2077 } else if ((atom1->type == XML_REGEXP_CHARVAL) &&
2078 (atom2->type == XML_REGEXP_RANGES)) {
2079 xmlRegAtomPtr tmp;
2080 tmp = atom1;
2081 atom1 = atom2;
2082 atom2 = tmp;
2083 } else if (atom1->type != atom2->type) {
Daniel Veillarde19fc232002-04-22 16:01:24 +00002084 return(0);
Daniel Veillard567a45b2005-10-18 19:11:55 +00002085 }
Daniel Veillarde19fc232002-04-22 16:01:24 +00002086 switch (atom1->type) {
2087 case XML_REGEXP_STRING:
Daniel Veillard9efc4762005-07-19 14:33:55 +00002088 ret = xmlRegStrEqualWildcard((xmlChar *)atom1->valuep,
2089 (xmlChar *)atom2->valuep);
2090 break;
Daniel Veillarde19fc232002-04-22 16:01:24 +00002091 case XML_REGEXP_EPSILON:
Daniel Veillard567a45b2005-10-18 19:11:55 +00002092 goto not_determinist;
Daniel Veillarde19fc232002-04-22 16:01:24 +00002093 case XML_REGEXP_CHARVAL:
Daniel Veillard567a45b2005-10-18 19:11:55 +00002094 ret = (atom1->codepoint == atom2->codepoint);
Daniel Veillard9efc4762005-07-19 14:33:55 +00002095 break;
Daniel Veillarde19fc232002-04-22 16:01:24 +00002096 case XML_REGEXP_RANGES:
Daniel Veillard567a45b2005-10-18 19:11:55 +00002097 if (atom2->type == XML_REGEXP_CHARVAL) {
2098 ret = xmlRegCheckCharacter(atom1, atom2->codepoint);
2099 if (ret < 0)
2100 return(-1);
2101 break;
2102 } else {
2103 int i, j, res;
2104 xmlRegRangePtr r1, r2;
2105
2106 /*
2107 * need to check that none of the ranges eventually matches
2108 */
2109 for (i = 0;i < atom1->nbRanges;i++) {
2110 for (j = 0;j < atom2->nbRanges;j++) {
2111 r1 = atom1->ranges[i];
2112 r2 = atom2->ranges[j];
2113 res = xmlFACompareRanges(r1, r2);
2114 if (res == 1) {
2115 ret = 1;
2116 goto done;
2117 }
2118 }
2119 }
2120 ret = 0;
2121 }
2122 break;
Daniel Veillarde19fc232002-04-22 16:01:24 +00002123 default:
Daniel Veillard567a45b2005-10-18 19:11:55 +00002124 goto not_determinist;
Daniel Veillarde19fc232002-04-22 16:01:24 +00002125 }
Daniel Veillard567a45b2005-10-18 19:11:55 +00002126done:
Daniel Veillard6e65e152005-08-09 11:09:52 +00002127 if (atom1->neg != atom2->neg) {
Daniel Veillard9efc4762005-07-19 14:33:55 +00002128 ret = !ret;
Daniel Veillard6e65e152005-08-09 11:09:52 +00002129 }
Daniel Veillard567a45b2005-10-18 19:11:55 +00002130 if (ret == 0)
2131 return(0);
2132not_determinist:
2133 return(1);
Daniel Veillarde19fc232002-04-22 16:01:24 +00002134}
2135
2136/**
2137 * xmlFARecurseDeterminism:
2138 * @ctxt: a regexp parser context
2139 *
2140 * Check whether the associated regexp is determinist,
2141 * should be called after xmlFAEliminateEpsilonTransitions()
2142 *
2143 */
2144static int
2145xmlFARecurseDeterminism(xmlRegParserCtxtPtr ctxt, xmlRegStatePtr state,
2146 int to, xmlRegAtomPtr atom) {
2147 int ret = 1;
Daniel Veillard567a45b2005-10-18 19:11:55 +00002148 int res;
Daniel Veillard5de09382005-09-26 17:18:17 +00002149 int transnr, nbTrans;
Daniel Veillarde19fc232002-04-22 16:01:24 +00002150 xmlRegTransPtr t1;
2151
2152 if (state == NULL)
2153 return(ret);
Daniel Veillard5de09382005-09-26 17:18:17 +00002154 /*
2155 * don't recurse on transitions potentially added in the course of
2156 * the elimination.
2157 */
2158 nbTrans = state->nbTrans;
2159 for (transnr = 0;transnr < nbTrans;transnr++) {
Daniel Veillarde19fc232002-04-22 16:01:24 +00002160 t1 = &(state->trans[transnr]);
2161 /*
2162 * check transitions conflicting with the one looked at
2163 */
2164 if (t1->atom == NULL) {
2165 if (t1->to == -1)
2166 continue;
Daniel Veillard567a45b2005-10-18 19:11:55 +00002167 res = xmlFARecurseDeterminism(ctxt, ctxt->states[t1->to],
Daniel Veillarde19fc232002-04-22 16:01:24 +00002168 to, atom);
Daniel Veillard567a45b2005-10-18 19:11:55 +00002169 if (res == 0) {
2170 ret = 0;
Daniel Veillardaa622012005-10-20 15:55:25 +00002171 /* t1->nd = 1; */
Daniel Veillard567a45b2005-10-18 19:11:55 +00002172 }
Daniel Veillarde19fc232002-04-22 16:01:24 +00002173 continue;
2174 }
2175 if (t1->to != to)
2176 continue;
Daniel Veillard567a45b2005-10-18 19:11:55 +00002177 if (xmlFACompareAtoms(t1->atom, atom)) {
2178 ret = 0;
2179 /* mark the transition as non-deterministic */
2180 t1->nd = 1;
2181 }
Daniel Veillarde19fc232002-04-22 16:01:24 +00002182 }
2183 return(ret);
2184}
2185
2186/**
2187 * xmlFAComputesDeterminism:
2188 * @ctxt: a regexp parser context
2189 *
2190 * Check whether the associated regexp is determinist,
2191 * should be called after xmlFAEliminateEpsilonTransitions()
2192 *
2193 */
2194static int
2195xmlFAComputesDeterminism(xmlRegParserCtxtPtr ctxt) {
2196 int statenr, transnr;
2197 xmlRegStatePtr state;
Daniel Veillard567a45b2005-10-18 19:11:55 +00002198 xmlRegTransPtr t1, t2, last;
Daniel Veillarde19fc232002-04-22 16:01:24 +00002199 int i;
2200 int ret = 1;
2201
Daniel Veillard4402ab42002-09-12 16:02:56 +00002202#ifdef DEBUG_REGEXP_GRAPH
2203 printf("xmlFAComputesDeterminism\n");
2204 xmlRegPrintCtxt(stdout, ctxt);
2205#endif
Daniel Veillarde19fc232002-04-22 16:01:24 +00002206 if (ctxt->determinist != -1)
2207 return(ctxt->determinist);
2208
2209 /*
Daniel Veillard567a45b2005-10-18 19:11:55 +00002210 * First cleanup the automata removing cancelled transitions
Daniel Veillarde19fc232002-04-22 16:01:24 +00002211 */
2212 for (statenr = 0;statenr < ctxt->nbStates;statenr++) {
2213 state = ctxt->states[statenr];
2214 if (state == NULL)
2215 continue;
Daniel Veillard4f82c8a2005-08-09 21:40:08 +00002216 if (state->nbTrans < 2)
2217 continue;
Daniel Veillarde19fc232002-04-22 16:01:24 +00002218 for (transnr = 0;transnr < state->nbTrans;transnr++) {
2219 t1 = &(state->trans[transnr]);
2220 /*
2221 * Determinism checks in case of counted or all transitions
2222 * will have to be handled separately
2223 */
Daniel Veillard567a45b2005-10-18 19:11:55 +00002224 if (t1->atom == NULL) {
Daniel Veillardaa622012005-10-20 15:55:25 +00002225 /* t1->nd = 1; */
Daniel Veillarde19fc232002-04-22 16:01:24 +00002226 continue;
Daniel Veillard567a45b2005-10-18 19:11:55 +00002227 }
Daniel Veillarde19fc232002-04-22 16:01:24 +00002228 if (t1->to == -1) /* eliminated */
2229 continue;
2230 for (i = 0;i < transnr;i++) {
2231 t2 = &(state->trans[i]);
2232 if (t2->to == -1) /* eliminated */
2233 continue;
2234 if (t2->atom != NULL) {
2235 if (t1->to == t2->to) {
2236 if (xmlFACompareAtoms(t1->atom, t2->atom))
William M. Brackddf71d62004-05-06 04:17:26 +00002237 t2->to = -1; /* eliminated */
Daniel Veillard567a45b2005-10-18 19:11:55 +00002238 }
2239 }
2240 }
2241 }
2242 }
2243
2244 /*
2245 * Check for all states that there aren't 2 transitions
2246 * with the same atom and a different target.
2247 */
2248 for (statenr = 0;statenr < ctxt->nbStates;statenr++) {
2249 state = ctxt->states[statenr];
2250 if (state == NULL)
2251 continue;
2252 if (state->nbTrans < 2)
2253 continue;
2254 last = NULL;
2255 for (transnr = 0;transnr < state->nbTrans;transnr++) {
2256 t1 = &(state->trans[transnr]);
2257 /*
2258 * Determinism checks in case of counted or all transitions
2259 * will have to be handled separately
2260 */
2261 if (t1->atom == NULL) {
2262 continue;
2263 }
2264 if (t1->to == -1) /* eliminated */
2265 continue;
2266 for (i = 0;i < transnr;i++) {
2267 t2 = &(state->trans[i]);
2268 if (t2->to == -1) /* eliminated */
2269 continue;
2270 if (t2->atom != NULL) {
2271 /* not determinist ! */
2272 if (xmlFACompareAtoms(t1->atom, t2->atom)) {
2273 ret = 0;
2274 /* mark the transitions as non-deterministic ones */
2275 t1->nd = 1;
2276 t2->nd = 1;
2277 last = t1;
Daniel Veillarde19fc232002-04-22 16:01:24 +00002278 }
2279 } else if (t1->to != -1) {
2280 /*
2281 * do the closure in case of remaining specific
2282 * epsilon transitions like choices or all
2283 */
2284 ret = xmlFARecurseDeterminism(ctxt, ctxt->states[t1->to],
2285 t2->to, t2->atom);
Daniel Veillard567a45b2005-10-18 19:11:55 +00002286 /* don't shortcut the computation so all non deterministic
2287 transition get marked down
Daniel Veillarde19fc232002-04-22 16:01:24 +00002288 if (ret == 0)
Daniel Veillardaa622012005-10-20 15:55:25 +00002289 return(0);
2290 */
Daniel Veillard567a45b2005-10-18 19:11:55 +00002291 if (ret == 0) {
2292 t1->nd = 1;
Daniel Veillardaa622012005-10-20 15:55:25 +00002293 /* t2->nd = 1; */
Daniel Veillard567a45b2005-10-18 19:11:55 +00002294 last = t1;
2295 }
Daniel Veillarde19fc232002-04-22 16:01:24 +00002296 }
2297 }
Daniel Veillard567a45b2005-10-18 19:11:55 +00002298 /* don't shortcut the computation so all non deterministic
2299 transition get marked down
Daniel Veillarde19fc232002-04-22 16:01:24 +00002300 if (ret == 0)
Daniel Veillard567a45b2005-10-18 19:11:55 +00002301 break; */
Daniel Veillarde19fc232002-04-22 16:01:24 +00002302 }
Daniel Veillard567a45b2005-10-18 19:11:55 +00002303
2304 /*
2305 * mark specifically the last non-deterministic transition
2306 * from a state since there is no need to set-up rollback
2307 * from it
2308 */
2309 if (last != NULL) {
2310 last->nd = 2;
2311 }
2312
2313 /* don't shortcut the computation so all non deterministic
2314 transition get marked down
Daniel Veillarde19fc232002-04-22 16:01:24 +00002315 if (ret == 0)
Daniel Veillard567a45b2005-10-18 19:11:55 +00002316 break; */
Daniel Veillarde19fc232002-04-22 16:01:24 +00002317 }
Daniel Veillard567a45b2005-10-18 19:11:55 +00002318
Daniel Veillarde19fc232002-04-22 16:01:24 +00002319 ctxt->determinist = ret;
2320 return(ret);
2321}
2322
Daniel Veillard4255d502002-04-16 15:50:10 +00002323/************************************************************************
2324 * *
2325 * Routines to check input against transition atoms *
2326 * *
2327 ************************************************************************/
2328
2329static int
2330xmlRegCheckCharacterRange(xmlRegAtomType type, int codepoint, int neg,
2331 int start, int end, const xmlChar *blockName) {
2332 int ret = 0;
2333
2334 switch (type) {
2335 case XML_REGEXP_STRING:
2336 case XML_REGEXP_SUBREG:
2337 case XML_REGEXP_RANGES:
2338 case XML_REGEXP_EPSILON:
2339 return(-1);
2340 case XML_REGEXP_ANYCHAR:
2341 ret = ((codepoint != '\n') && (codepoint != '\r'));
2342 break;
2343 case XML_REGEXP_CHARVAL:
2344 ret = ((codepoint >= start) && (codepoint <= end));
2345 break;
2346 case XML_REGEXP_NOTSPACE:
2347 neg = !neg;
2348 case XML_REGEXP_ANYSPACE:
2349 ret = ((codepoint == '\n') || (codepoint == '\r') ||
2350 (codepoint == '\t') || (codepoint == ' '));
2351 break;
2352 case XML_REGEXP_NOTINITNAME:
2353 neg = !neg;
2354 case XML_REGEXP_INITNAME:
William M. Brack871611b2003-10-18 04:53:14 +00002355 ret = (IS_LETTER(codepoint) ||
Daniel Veillard4255d502002-04-16 15:50:10 +00002356 (codepoint == '_') || (codepoint == ':'));
2357 break;
2358 case XML_REGEXP_NOTNAMECHAR:
2359 neg = !neg;
2360 case XML_REGEXP_NAMECHAR:
William M. Brack871611b2003-10-18 04:53:14 +00002361 ret = (IS_LETTER(codepoint) || IS_DIGIT(codepoint) ||
Daniel Veillard4255d502002-04-16 15:50:10 +00002362 (codepoint == '.') || (codepoint == '-') ||
2363 (codepoint == '_') || (codepoint == ':') ||
William M. Brack871611b2003-10-18 04:53:14 +00002364 IS_COMBINING(codepoint) || IS_EXTENDER(codepoint));
Daniel Veillard4255d502002-04-16 15:50:10 +00002365 break;
2366 case XML_REGEXP_NOTDECIMAL:
2367 neg = !neg;
2368 case XML_REGEXP_DECIMAL:
2369 ret = xmlUCSIsCatNd(codepoint);
2370 break;
2371 case XML_REGEXP_REALCHAR:
2372 neg = !neg;
2373 case XML_REGEXP_NOTREALCHAR:
2374 ret = xmlUCSIsCatP(codepoint);
2375 if (ret == 0)
2376 ret = xmlUCSIsCatZ(codepoint);
2377 if (ret == 0)
2378 ret = xmlUCSIsCatC(codepoint);
2379 break;
2380 case XML_REGEXP_LETTER:
2381 ret = xmlUCSIsCatL(codepoint);
2382 break;
2383 case XML_REGEXP_LETTER_UPPERCASE:
2384 ret = xmlUCSIsCatLu(codepoint);
2385 break;
2386 case XML_REGEXP_LETTER_LOWERCASE:
2387 ret = xmlUCSIsCatLl(codepoint);
2388 break;
2389 case XML_REGEXP_LETTER_TITLECASE:
2390 ret = xmlUCSIsCatLt(codepoint);
2391 break;
2392 case XML_REGEXP_LETTER_MODIFIER:
2393 ret = xmlUCSIsCatLm(codepoint);
2394 break;
2395 case XML_REGEXP_LETTER_OTHERS:
2396 ret = xmlUCSIsCatLo(codepoint);
2397 break;
2398 case XML_REGEXP_MARK:
2399 ret = xmlUCSIsCatM(codepoint);
2400 break;
2401 case XML_REGEXP_MARK_NONSPACING:
2402 ret = xmlUCSIsCatMn(codepoint);
2403 break;
2404 case XML_REGEXP_MARK_SPACECOMBINING:
2405 ret = xmlUCSIsCatMc(codepoint);
2406 break;
2407 case XML_REGEXP_MARK_ENCLOSING:
2408 ret = xmlUCSIsCatMe(codepoint);
2409 break;
2410 case XML_REGEXP_NUMBER:
2411 ret = xmlUCSIsCatN(codepoint);
2412 break;
2413 case XML_REGEXP_NUMBER_DECIMAL:
2414 ret = xmlUCSIsCatNd(codepoint);
2415 break;
2416 case XML_REGEXP_NUMBER_LETTER:
2417 ret = xmlUCSIsCatNl(codepoint);
2418 break;
2419 case XML_REGEXP_NUMBER_OTHERS:
2420 ret = xmlUCSIsCatNo(codepoint);
2421 break;
2422 case XML_REGEXP_PUNCT:
2423 ret = xmlUCSIsCatP(codepoint);
2424 break;
2425 case XML_REGEXP_PUNCT_CONNECTOR:
2426 ret = xmlUCSIsCatPc(codepoint);
2427 break;
2428 case XML_REGEXP_PUNCT_DASH:
2429 ret = xmlUCSIsCatPd(codepoint);
2430 break;
2431 case XML_REGEXP_PUNCT_OPEN:
2432 ret = xmlUCSIsCatPs(codepoint);
2433 break;
2434 case XML_REGEXP_PUNCT_CLOSE:
2435 ret = xmlUCSIsCatPe(codepoint);
2436 break;
2437 case XML_REGEXP_PUNCT_INITQUOTE:
2438 ret = xmlUCSIsCatPi(codepoint);
2439 break;
2440 case XML_REGEXP_PUNCT_FINQUOTE:
2441 ret = xmlUCSIsCatPf(codepoint);
2442 break;
2443 case XML_REGEXP_PUNCT_OTHERS:
2444 ret = xmlUCSIsCatPo(codepoint);
2445 break;
2446 case XML_REGEXP_SEPAR:
2447 ret = xmlUCSIsCatZ(codepoint);
2448 break;
2449 case XML_REGEXP_SEPAR_SPACE:
2450 ret = xmlUCSIsCatZs(codepoint);
2451 break;
2452 case XML_REGEXP_SEPAR_LINE:
2453 ret = xmlUCSIsCatZl(codepoint);
2454 break;
2455 case XML_REGEXP_SEPAR_PARA:
2456 ret = xmlUCSIsCatZp(codepoint);
2457 break;
2458 case XML_REGEXP_SYMBOL:
2459 ret = xmlUCSIsCatS(codepoint);
2460 break;
2461 case XML_REGEXP_SYMBOL_MATH:
2462 ret = xmlUCSIsCatSm(codepoint);
2463 break;
2464 case XML_REGEXP_SYMBOL_CURRENCY:
2465 ret = xmlUCSIsCatSc(codepoint);
2466 break;
2467 case XML_REGEXP_SYMBOL_MODIFIER:
2468 ret = xmlUCSIsCatSk(codepoint);
2469 break;
2470 case XML_REGEXP_SYMBOL_OTHERS:
2471 ret = xmlUCSIsCatSo(codepoint);
2472 break;
2473 case XML_REGEXP_OTHER:
2474 ret = xmlUCSIsCatC(codepoint);
2475 break;
2476 case XML_REGEXP_OTHER_CONTROL:
2477 ret = xmlUCSIsCatCc(codepoint);
2478 break;
2479 case XML_REGEXP_OTHER_FORMAT:
2480 ret = xmlUCSIsCatCf(codepoint);
2481 break;
2482 case XML_REGEXP_OTHER_PRIVATE:
2483 ret = xmlUCSIsCatCo(codepoint);
2484 break;
2485 case XML_REGEXP_OTHER_NA:
2486 /* ret = xmlUCSIsCatCn(codepoint); */
2487 /* Seems it doesn't exist anymore in recent Unicode releases */
2488 ret = 0;
2489 break;
2490 case XML_REGEXP_BLOCK_NAME:
2491 ret = xmlUCSIsBlock(codepoint, (const char *) blockName);
2492 break;
2493 }
2494 if (neg)
2495 return(!ret);
2496 return(ret);
2497}
2498
2499static int
2500xmlRegCheckCharacter(xmlRegAtomPtr atom, int codepoint) {
2501 int i, ret = 0;
2502 xmlRegRangePtr range;
2503
William M. Brack871611b2003-10-18 04:53:14 +00002504 if ((atom == NULL) || (!IS_CHAR(codepoint)))
Daniel Veillard4255d502002-04-16 15:50:10 +00002505 return(-1);
2506
2507 switch (atom->type) {
2508 case XML_REGEXP_SUBREG:
2509 case XML_REGEXP_EPSILON:
2510 return(-1);
2511 case XML_REGEXP_CHARVAL:
2512 return(codepoint == atom->codepoint);
2513 case XML_REGEXP_RANGES: {
2514 int accept = 0;
Daniel Veillardf2a12832003-11-24 13:04:35 +00002515
Daniel Veillard4255d502002-04-16 15:50:10 +00002516 for (i = 0;i < atom->nbRanges;i++) {
2517 range = atom->ranges[i];
Daniel Veillardf8b9de32003-11-24 14:27:26 +00002518 if (range->neg == 2) {
Daniel Veillard4255d502002-04-16 15:50:10 +00002519 ret = xmlRegCheckCharacterRange(range->type, codepoint,
2520 0, range->start, range->end,
2521 range->blockName);
2522 if (ret != 0)
2523 return(0); /* excluded char */
Daniel Veillardf8b9de32003-11-24 14:27:26 +00002524 } else if (range->neg) {
2525 ret = xmlRegCheckCharacterRange(range->type, codepoint,
2526 0, range->start, range->end,
2527 range->blockName);
2528 if (ret == 0)
Daniel Veillardf2a12832003-11-24 13:04:35 +00002529 accept = 1;
Daniel Veillardf8b9de32003-11-24 14:27:26 +00002530 else
2531 return(0);
Daniel Veillard4255d502002-04-16 15:50:10 +00002532 } else {
2533 ret = xmlRegCheckCharacterRange(range->type, codepoint,
2534 0, range->start, range->end,
2535 range->blockName);
2536 if (ret != 0)
2537 accept = 1; /* might still be excluded */
2538 }
2539 }
2540 return(accept);
2541 }
2542 case XML_REGEXP_STRING:
2543 printf("TODO: XML_REGEXP_STRING\n");
2544 return(-1);
2545 case XML_REGEXP_ANYCHAR:
2546 case XML_REGEXP_ANYSPACE:
2547 case XML_REGEXP_NOTSPACE:
2548 case XML_REGEXP_INITNAME:
2549 case XML_REGEXP_NOTINITNAME:
2550 case XML_REGEXP_NAMECHAR:
2551 case XML_REGEXP_NOTNAMECHAR:
2552 case XML_REGEXP_DECIMAL:
2553 case XML_REGEXP_NOTDECIMAL:
2554 case XML_REGEXP_REALCHAR:
2555 case XML_REGEXP_NOTREALCHAR:
2556 case XML_REGEXP_LETTER:
2557 case XML_REGEXP_LETTER_UPPERCASE:
2558 case XML_REGEXP_LETTER_LOWERCASE:
2559 case XML_REGEXP_LETTER_TITLECASE:
2560 case XML_REGEXP_LETTER_MODIFIER:
2561 case XML_REGEXP_LETTER_OTHERS:
2562 case XML_REGEXP_MARK:
2563 case XML_REGEXP_MARK_NONSPACING:
2564 case XML_REGEXP_MARK_SPACECOMBINING:
2565 case XML_REGEXP_MARK_ENCLOSING:
2566 case XML_REGEXP_NUMBER:
2567 case XML_REGEXP_NUMBER_DECIMAL:
2568 case XML_REGEXP_NUMBER_LETTER:
2569 case XML_REGEXP_NUMBER_OTHERS:
2570 case XML_REGEXP_PUNCT:
2571 case XML_REGEXP_PUNCT_CONNECTOR:
2572 case XML_REGEXP_PUNCT_DASH:
2573 case XML_REGEXP_PUNCT_OPEN:
2574 case XML_REGEXP_PUNCT_CLOSE:
2575 case XML_REGEXP_PUNCT_INITQUOTE:
2576 case XML_REGEXP_PUNCT_FINQUOTE:
2577 case XML_REGEXP_PUNCT_OTHERS:
2578 case XML_REGEXP_SEPAR:
2579 case XML_REGEXP_SEPAR_SPACE:
2580 case XML_REGEXP_SEPAR_LINE:
2581 case XML_REGEXP_SEPAR_PARA:
2582 case XML_REGEXP_SYMBOL:
2583 case XML_REGEXP_SYMBOL_MATH:
2584 case XML_REGEXP_SYMBOL_CURRENCY:
2585 case XML_REGEXP_SYMBOL_MODIFIER:
2586 case XML_REGEXP_SYMBOL_OTHERS:
2587 case XML_REGEXP_OTHER:
2588 case XML_REGEXP_OTHER_CONTROL:
2589 case XML_REGEXP_OTHER_FORMAT:
2590 case XML_REGEXP_OTHER_PRIVATE:
2591 case XML_REGEXP_OTHER_NA:
2592 case XML_REGEXP_BLOCK_NAME:
2593 ret = xmlRegCheckCharacterRange(atom->type, codepoint, 0, 0, 0,
2594 (const xmlChar *)atom->valuep);
2595 if (atom->neg)
2596 ret = !ret;
2597 break;
2598 }
2599 return(ret);
2600}
2601
2602/************************************************************************
2603 * *
William M. Brackddf71d62004-05-06 04:17:26 +00002604 * Saving and restoring state of an execution context *
Daniel Veillard4255d502002-04-16 15:50:10 +00002605 * *
2606 ************************************************************************/
2607
2608#ifdef DEBUG_REGEXP_EXEC
2609static void
2610xmlFARegDebugExec(xmlRegExecCtxtPtr exec) {
2611 printf("state: %d:%d:idx %d", exec->state->no, exec->transno, exec->index);
2612 if (exec->inputStack != NULL) {
2613 int i;
2614 printf(": ");
2615 for (i = 0;(i < 3) && (i < exec->inputStackNr);i++)
2616 printf("%s ", exec->inputStack[exec->inputStackNr - (i + 1)]);
2617 } else {
2618 printf(": %s", &(exec->inputString[exec->index]));
2619 }
2620 printf("\n");
2621}
2622#endif
2623
2624static void
2625xmlFARegExecSave(xmlRegExecCtxtPtr exec) {
2626#ifdef DEBUG_REGEXP_EXEC
2627 printf("saving ");
2628 exec->transno++;
2629 xmlFARegDebugExec(exec);
2630 exec->transno--;
2631#endif
Daniel Veillard94cc1032005-09-15 13:09:00 +00002632#ifdef MAX_PUSH
2633 if (exec->nbPush > MAX_PUSH) {
2634 return;
2635 }
2636 exec->nbPush++;
2637#endif
Daniel Veillard4255d502002-04-16 15:50:10 +00002638
2639 if (exec->maxRollbacks == 0) {
2640 exec->maxRollbacks = 4;
2641 exec->rollbacks = (xmlRegExecRollback *) xmlMalloc(exec->maxRollbacks *
2642 sizeof(xmlRegExecRollback));
2643 if (exec->rollbacks == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00002644 xmlRegexpErrMemory(NULL, "saving regexp");
Daniel Veillard4255d502002-04-16 15:50:10 +00002645 exec->maxRollbacks = 0;
2646 return;
2647 }
2648 memset(exec->rollbacks, 0,
2649 exec->maxRollbacks * sizeof(xmlRegExecRollback));
2650 } else if (exec->nbRollbacks >= exec->maxRollbacks) {
2651 xmlRegExecRollback *tmp;
2652 int len = exec->maxRollbacks;
2653
2654 exec->maxRollbacks *= 2;
2655 tmp = (xmlRegExecRollback *) xmlRealloc(exec->rollbacks,
2656 exec->maxRollbacks * sizeof(xmlRegExecRollback));
2657 if (tmp == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00002658 xmlRegexpErrMemory(NULL, "saving regexp");
Daniel Veillard4255d502002-04-16 15:50:10 +00002659 exec->maxRollbacks /= 2;
2660 return;
2661 }
2662 exec->rollbacks = tmp;
2663 tmp = &exec->rollbacks[len];
2664 memset(tmp, 0, (exec->maxRollbacks - len) * sizeof(xmlRegExecRollback));
2665 }
2666 exec->rollbacks[exec->nbRollbacks].state = exec->state;
2667 exec->rollbacks[exec->nbRollbacks].index = exec->index;
2668 exec->rollbacks[exec->nbRollbacks].nextbranch = exec->transno + 1;
2669 if (exec->comp->nbCounters > 0) {
2670 if (exec->rollbacks[exec->nbRollbacks].counts == NULL) {
2671 exec->rollbacks[exec->nbRollbacks].counts = (int *)
2672 xmlMalloc(exec->comp->nbCounters * sizeof(int));
2673 if (exec->rollbacks[exec->nbRollbacks].counts == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00002674 xmlRegexpErrMemory(NULL, "saving regexp");
Daniel Veillard4255d502002-04-16 15:50:10 +00002675 exec->status = -5;
2676 return;
2677 }
2678 }
2679 memcpy(exec->rollbacks[exec->nbRollbacks].counts, exec->counts,
2680 exec->comp->nbCounters * sizeof(int));
2681 }
2682 exec->nbRollbacks++;
2683}
2684
2685static void
2686xmlFARegExecRollBack(xmlRegExecCtxtPtr exec) {
2687 if (exec->nbRollbacks <= 0) {
2688 exec->status = -1;
2689#ifdef DEBUG_REGEXP_EXEC
2690 printf("rollback failed on empty stack\n");
2691#endif
2692 return;
2693 }
2694 exec->nbRollbacks--;
2695 exec->state = exec->rollbacks[exec->nbRollbacks].state;
2696 exec->index = exec->rollbacks[exec->nbRollbacks].index;
2697 exec->transno = exec->rollbacks[exec->nbRollbacks].nextbranch;
2698 if (exec->comp->nbCounters > 0) {
2699 if (exec->rollbacks[exec->nbRollbacks].counts == NULL) {
2700 fprintf(stderr, "exec save: allocation failed");
2701 exec->status = -6;
2702 return;
2703 }
2704 memcpy(exec->counts, exec->rollbacks[exec->nbRollbacks].counts,
2705 exec->comp->nbCounters * sizeof(int));
2706 }
2707
2708#ifdef DEBUG_REGEXP_EXEC
2709 printf("restored ");
2710 xmlFARegDebugExec(exec);
2711#endif
2712}
2713
2714/************************************************************************
2715 * *
William M. Brackddf71d62004-05-06 04:17:26 +00002716 * Verifier, running an input against a compiled regexp *
Daniel Veillard4255d502002-04-16 15:50:10 +00002717 * *
2718 ************************************************************************/
2719
2720static int
2721xmlFARegExec(xmlRegexpPtr comp, const xmlChar *content) {
2722 xmlRegExecCtxt execval;
2723 xmlRegExecCtxtPtr exec = &execval;
Daniel Veillard567a45b2005-10-18 19:11:55 +00002724 int ret, codepoint = 0, len, deter;
Daniel Veillard4255d502002-04-16 15:50:10 +00002725
2726 exec->inputString = content;
2727 exec->index = 0;
Daniel Veillard94cc1032005-09-15 13:09:00 +00002728 exec->nbPush = 0;
Daniel Veillard4255d502002-04-16 15:50:10 +00002729 exec->determinist = 1;
2730 exec->maxRollbacks = 0;
2731 exec->nbRollbacks = 0;
2732 exec->rollbacks = NULL;
2733 exec->status = 0;
2734 exec->comp = comp;
2735 exec->state = comp->states[0];
2736 exec->transno = 0;
2737 exec->transcount = 0;
Daniel Veillardf2a12832003-11-24 13:04:35 +00002738 exec->inputStack = NULL;
2739 exec->inputStackMax = 0;
Daniel Veillard4255d502002-04-16 15:50:10 +00002740 if (comp->nbCounters > 0) {
2741 exec->counts = (int *) xmlMalloc(comp->nbCounters * sizeof(int));
Daniel Veillardff46a042003-10-08 08:53:17 +00002742 if (exec->counts == NULL) {
2743 xmlRegexpErrMemory(NULL, "running regexp");
Daniel Veillard4255d502002-04-16 15:50:10 +00002744 return(-1);
Daniel Veillardff46a042003-10-08 08:53:17 +00002745 }
Daniel Veillard4255d502002-04-16 15:50:10 +00002746 memset(exec->counts, 0, comp->nbCounters * sizeof(int));
2747 } else
2748 exec->counts = NULL;
2749 while ((exec->status == 0) &&
2750 ((exec->inputString[exec->index] != 0) ||
2751 (exec->state->type != XML_REGEXP_FINAL_STATE))) {
2752 xmlRegTransPtr trans;
2753 xmlRegAtomPtr atom;
2754
2755 /*
William M. Brack0e00b282004-04-26 15:40:47 +00002756 * If end of input on non-terminal state, rollback, however we may
Daniel Veillard4255d502002-04-16 15:50:10 +00002757 * still have epsilon like transition for counted transitions
William M. Brack0e00b282004-04-26 15:40:47 +00002758 * on counters, in that case don't break too early. Additionally,
2759 * if we are working on a range like "AB{0,2}", where B is not present,
2760 * we don't want to break.
Daniel Veillard4255d502002-04-16 15:50:10 +00002761 */
William M. Brack0e00b282004-04-26 15:40:47 +00002762 if ((exec->inputString[exec->index] == 0) && (exec->counts == NULL)) {
William M. Brackddf71d62004-05-06 04:17:26 +00002763 /*
2764 * if there is a transition, we must check if
2765 * atom allows minOccurs of 0
2766 */
2767 if (exec->transno < exec->state->nbTrans) {
William M. Brack0e00b282004-04-26 15:40:47 +00002768 trans = &exec->state->trans[exec->transno];
2769 if (trans->to >=0) {
2770 atom = trans->atom;
2771 if (!((atom->min == 0) && (atom->max > 0)))
2772 goto rollback;
2773 }
2774 } else
2775 goto rollback;
2776 }
Daniel Veillard4255d502002-04-16 15:50:10 +00002777
2778 exec->transcount = 0;
2779 for (;exec->transno < exec->state->nbTrans;exec->transno++) {
2780 trans = &exec->state->trans[exec->transno];
2781 if (trans->to < 0)
2782 continue;
2783 atom = trans->atom;
2784 ret = 0;
Daniel Veillard567a45b2005-10-18 19:11:55 +00002785 deter = 1;
Daniel Veillard4255d502002-04-16 15:50:10 +00002786 if (trans->count >= 0) {
2787 int count;
2788 xmlRegCounterPtr counter;
2789
2790 /*
2791 * A counted transition.
2792 */
2793
2794 count = exec->counts[trans->count];
2795 counter = &exec->comp->counters[trans->count];
2796#ifdef DEBUG_REGEXP_EXEC
2797 printf("testing count %d: val %d, min %d, max %d\n",
2798 trans->count, count, counter->min, counter->max);
2799#endif
2800 ret = ((count >= counter->min) && (count <= counter->max));
Daniel Veillard567a45b2005-10-18 19:11:55 +00002801 if ((ret) && (counter->min != counter->max))
2802 deter = 0;
Daniel Veillard4255d502002-04-16 15:50:10 +00002803 } else if (atom == NULL) {
2804 fprintf(stderr, "epsilon transition left at runtime\n");
2805 exec->status = -2;
2806 break;
2807 } else if (exec->inputString[exec->index] != 0) {
2808 codepoint = CUR_SCHAR(&(exec->inputString[exec->index]), len);
2809 ret = xmlRegCheckCharacter(atom, codepoint);
William M. Brack0e00b282004-04-26 15:40:47 +00002810 if ((ret == 1) && (atom->min >= 0) && (atom->max > 0)) {
Daniel Veillard4255d502002-04-16 15:50:10 +00002811 xmlRegStatePtr to = comp->states[trans->to];
2812
2813 /*
2814 * this is a multiple input sequence
Daniel Veillardfc6eca02005-11-01 15:24:02 +00002815 * If there is a counter associated increment it now.
2816 * before potentially saving and rollback
Daniel Veillard4255d502002-04-16 15:50:10 +00002817 */
Daniel Veillardfc6eca02005-11-01 15:24:02 +00002818 if (trans->counter >= 0) {
2819#ifdef DEBUG_REGEXP_EXEC
2820 printf("Increasing count %d\n", trans->counter);
2821#endif
2822 exec->counts[trans->counter]++;
2823 }
Daniel Veillard4255d502002-04-16 15:50:10 +00002824 if (exec->state->nbTrans > exec->transno + 1) {
2825 xmlFARegExecSave(exec);
2826 }
2827 exec->transcount = 1;
2828 do {
2829 /*
2830 * Try to progress as much as possible on the input
2831 */
2832 if (exec->transcount == atom->max) {
2833 break;
2834 }
2835 exec->index += len;
2836 /*
2837 * End of input: stop here
2838 */
2839 if (exec->inputString[exec->index] == 0) {
2840 exec->index -= len;
2841 break;
2842 }
2843 if (exec->transcount >= atom->min) {
2844 int transno = exec->transno;
2845 xmlRegStatePtr state = exec->state;
2846
2847 /*
2848 * The transition is acceptable save it
2849 */
2850 exec->transno = -1; /* trick */
2851 exec->state = to;
2852 xmlFARegExecSave(exec);
2853 exec->transno = transno;
2854 exec->state = state;
2855 }
2856 codepoint = CUR_SCHAR(&(exec->inputString[exec->index]),
2857 len);
2858 ret = xmlRegCheckCharacter(atom, codepoint);
2859 exec->transcount++;
2860 } while (ret == 1);
2861 if (exec->transcount < atom->min)
2862 ret = 0;
2863
2864 /*
2865 * If the last check failed but one transition was found
2866 * possible, rollback
2867 */
2868 if (ret < 0)
2869 ret = 0;
2870 if (ret == 0) {
2871 goto rollback;
2872 }
Daniel Veillardfc6eca02005-11-01 15:24:02 +00002873 if (trans->counter >= 0) {
2874#ifdef DEBUG_REGEXP_EXEC
2875 printf("Decreasing count %d\n", trans->counter);
2876#endif
2877 exec->counts[trans->counter]--;
2878 }
William M. Brack0e00b282004-04-26 15:40:47 +00002879 } else if ((ret == 0) && (atom->min == 0) && (atom->max > 0)) {
2880 /*
2881 * we don't match on the codepoint, but minOccurs of 0
2882 * says that's ok. Setting len to 0 inhibits stepping
2883 * over the codepoint.
2884 */
2885 exec->transcount = 1;
2886 len = 0;
2887 ret = 1;
Daniel Veillard4255d502002-04-16 15:50:10 +00002888 }
William M. Brack0e00b282004-04-26 15:40:47 +00002889 } else if ((atom->min == 0) && (atom->max > 0)) {
2890 /* another spot to match when minOccurs is 0 */
2891 exec->transcount = 1;
2892 len = 0;
2893 ret = 1;
Daniel Veillard4255d502002-04-16 15:50:10 +00002894 }
2895 if (ret == 1) {
Daniel Veillard567a45b2005-10-18 19:11:55 +00002896 if ((trans->nd == 1) ||
2897 ((trans->count >= 0) && (deter == 0) &&
2898 (exec->state->nbTrans > exec->transno + 1))) {
Daniel Veillardaa622012005-10-20 15:55:25 +00002899#ifdef DEBUG_REGEXP_EXEC
2900 if (trans->nd == 1)
2901 printf("Saving on nd transition atom %d for %c at %d\n",
2902 trans->atom->no, codepoint, exec->index);
2903 else
2904 printf("Saving on counted transition count %d for %c at %d\n",
2905 trans->count, codepoint, exec->index);
2906#endif
Daniel Veillard4255d502002-04-16 15:50:10 +00002907 xmlFARegExecSave(exec);
2908 }
2909 if (trans->counter >= 0) {
2910#ifdef DEBUG_REGEXP_EXEC
2911 printf("Increasing count %d\n", trans->counter);
2912#endif
2913 exec->counts[trans->counter]++;
2914 }
Daniel Veillard10752282005-08-08 13:05:13 +00002915 if ((trans->count >= 0) &&
2916 (trans->count < REGEXP_ALL_COUNTER)) {
2917#ifdef DEBUG_REGEXP_EXEC
2918 printf("resetting count %d on transition\n",
2919 trans->count);
2920#endif
2921 exec->counts[trans->count] = 0;
2922 }
Daniel Veillard4255d502002-04-16 15:50:10 +00002923#ifdef DEBUG_REGEXP_EXEC
2924 printf("entering state %d\n", trans->to);
2925#endif
2926 exec->state = comp->states[trans->to];
2927 exec->transno = 0;
2928 if (trans->atom != NULL) {
2929 exec->index += len;
2930 }
2931 goto progress;
2932 } else if (ret < 0) {
2933 exec->status = -4;
2934 break;
2935 }
2936 }
2937 if ((exec->transno != 0) || (exec->state->nbTrans == 0)) {
2938rollback:
2939 /*
2940 * Failed to find a way out
2941 */
2942 exec->determinist = 0;
Daniel Veillardaa622012005-10-20 15:55:25 +00002943#ifdef DEBUG_REGEXP_EXEC
2944 printf("rollback from state %d on %d:%c\n", exec->state->no,
2945 codepoint,codepoint);
2946#endif
Daniel Veillard4255d502002-04-16 15:50:10 +00002947 xmlFARegExecRollBack(exec);
2948 }
2949progress:
2950 continue;
2951 }
2952 if (exec->rollbacks != NULL) {
2953 if (exec->counts != NULL) {
2954 int i;
2955
2956 for (i = 0;i < exec->maxRollbacks;i++)
2957 if (exec->rollbacks[i].counts != NULL)
2958 xmlFree(exec->rollbacks[i].counts);
2959 }
2960 xmlFree(exec->rollbacks);
2961 }
2962 if (exec->counts != NULL)
2963 xmlFree(exec->counts);
2964 if (exec->status == 0)
2965 return(1);
Daniel Veillard94cc1032005-09-15 13:09:00 +00002966 if (exec->status == -1) {
2967 if (exec->nbPush > MAX_PUSH)
2968 return(-1);
Daniel Veillard4255d502002-04-16 15:50:10 +00002969 return(0);
Daniel Veillard94cc1032005-09-15 13:09:00 +00002970 }
Daniel Veillard4255d502002-04-16 15:50:10 +00002971 return(exec->status);
2972}
2973
2974/************************************************************************
2975 * *
William M. Brackddf71d62004-05-06 04:17:26 +00002976 * Progressive interface to the verifier one atom at a time *
Daniel Veillard4255d502002-04-16 15:50:10 +00002977 * *
2978 ************************************************************************/
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00002979#ifdef DEBUG_ERR
2980static void testerr(xmlRegExecCtxtPtr exec);
2981#endif
Daniel Veillard4255d502002-04-16 15:50:10 +00002982
2983/**
Daniel Veillard01c13b52002-12-10 15:19:08 +00002984 * xmlRegNewExecCtxt:
Daniel Veillard4255d502002-04-16 15:50:10 +00002985 * @comp: a precompiled regular expression
2986 * @callback: a callback function used for handling progresses in the
2987 * automata matching phase
2988 * @data: the context data associated to the callback in this context
2989 *
2990 * Build a context used for progressive evaluation of a regexp.
Daniel Veillard01c13b52002-12-10 15:19:08 +00002991 *
2992 * Returns the new context
Daniel Veillard4255d502002-04-16 15:50:10 +00002993 */
2994xmlRegExecCtxtPtr
2995xmlRegNewExecCtxt(xmlRegexpPtr comp, xmlRegExecCallbacks callback, void *data) {
2996 xmlRegExecCtxtPtr exec;
2997
2998 if (comp == NULL)
2999 return(NULL);
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00003000 if ((comp->compact == NULL) && (comp->states == NULL))
3001 return(NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +00003002 exec = (xmlRegExecCtxtPtr) xmlMalloc(sizeof(xmlRegExecCtxt));
3003 if (exec == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00003004 xmlRegexpErrMemory(NULL, "creating execution context");
Daniel Veillard4255d502002-04-16 15:50:10 +00003005 return(NULL);
3006 }
3007 memset(exec, 0, sizeof(xmlRegExecCtxt));
3008 exec->inputString = NULL;
3009 exec->index = 0;
3010 exec->determinist = 1;
3011 exec->maxRollbacks = 0;
3012 exec->nbRollbacks = 0;
3013 exec->rollbacks = NULL;
3014 exec->status = 0;
3015 exec->comp = comp;
Daniel Veillard23e73572002-09-19 19:56:43 +00003016 if (comp->compact == NULL)
3017 exec->state = comp->states[0];
Daniel Veillard4255d502002-04-16 15:50:10 +00003018 exec->transno = 0;
3019 exec->transcount = 0;
3020 exec->callback = callback;
3021 exec->data = data;
3022 if (comp->nbCounters > 0) {
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003023 /*
3024 * For error handling, exec->counts is allocated twice the size
3025 * the second half is used to store the data in case of rollback
3026 */
3027 exec->counts = (int *) xmlMalloc(comp->nbCounters * sizeof(int)
3028 * 2);
Daniel Veillard4255d502002-04-16 15:50:10 +00003029 if (exec->counts == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00003030 xmlRegexpErrMemory(NULL, "creating execution context");
Daniel Veillard4255d502002-04-16 15:50:10 +00003031 xmlFree(exec);
3032 return(NULL);
3033 }
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003034 memset(exec->counts, 0, comp->nbCounters * sizeof(int) * 2);
3035 exec->errCounts = &exec->counts[comp->nbCounters];
3036 } else {
Daniel Veillard4255d502002-04-16 15:50:10 +00003037 exec->counts = NULL;
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003038 exec->errCounts = NULL;
3039 }
Daniel Veillard4255d502002-04-16 15:50:10 +00003040 exec->inputStackMax = 0;
3041 exec->inputStackNr = 0;
3042 exec->inputStack = NULL;
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003043 exec->errStateNo = -1;
3044 exec->errString = NULL;
Daniel Veillard94cc1032005-09-15 13:09:00 +00003045 exec->nbPush = 0;
Daniel Veillard4255d502002-04-16 15:50:10 +00003046 return(exec);
3047}
3048
3049/**
3050 * xmlRegFreeExecCtxt:
3051 * @exec: a regular expression evaulation context
3052 *
3053 * Free the structures associated to a regular expression evaulation context.
3054 */
3055void
3056xmlRegFreeExecCtxt(xmlRegExecCtxtPtr exec) {
3057 if (exec == NULL)
3058 return;
3059
3060 if (exec->rollbacks != NULL) {
3061 if (exec->counts != NULL) {
3062 int i;
3063
3064 for (i = 0;i < exec->maxRollbacks;i++)
3065 if (exec->rollbacks[i].counts != NULL)
3066 xmlFree(exec->rollbacks[i].counts);
3067 }
3068 xmlFree(exec->rollbacks);
3069 }
3070 if (exec->counts != NULL)
3071 xmlFree(exec->counts);
3072 if (exec->inputStack != NULL) {
3073 int i;
3074
Daniel Veillard32370232002-10-16 14:08:14 +00003075 for (i = 0;i < exec->inputStackNr;i++) {
3076 if (exec->inputStack[i].value != NULL)
3077 xmlFree(exec->inputStack[i].value);
3078 }
Daniel Veillard4255d502002-04-16 15:50:10 +00003079 xmlFree(exec->inputStack);
3080 }
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003081 if (exec->errString != NULL)
3082 xmlFree(exec->errString);
Daniel Veillard4255d502002-04-16 15:50:10 +00003083 xmlFree(exec);
3084}
3085
3086static void
3087xmlFARegExecSaveInputString(xmlRegExecCtxtPtr exec, const xmlChar *value,
3088 void *data) {
3089#ifdef DEBUG_PUSH
3090 printf("saving value: %d:%s\n", exec->inputStackNr, value);
3091#endif
3092 if (exec->inputStackMax == 0) {
3093 exec->inputStackMax = 4;
3094 exec->inputStack = (xmlRegInputTokenPtr)
3095 xmlMalloc(exec->inputStackMax * sizeof(xmlRegInputToken));
3096 if (exec->inputStack == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00003097 xmlRegexpErrMemory(NULL, "pushing input string");
Daniel Veillard4255d502002-04-16 15:50:10 +00003098 exec->inputStackMax = 0;
3099 return;
3100 }
3101 } else if (exec->inputStackNr + 1 >= exec->inputStackMax) {
3102 xmlRegInputTokenPtr tmp;
3103
3104 exec->inputStackMax *= 2;
3105 tmp = (xmlRegInputTokenPtr) xmlRealloc(exec->inputStack,
3106 exec->inputStackMax * sizeof(xmlRegInputToken));
3107 if (tmp == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00003108 xmlRegexpErrMemory(NULL, "pushing input string");
Daniel Veillard4255d502002-04-16 15:50:10 +00003109 exec->inputStackMax /= 2;
3110 return;
3111 }
3112 exec->inputStack = tmp;
3113 }
3114 exec->inputStack[exec->inputStackNr].value = xmlStrdup(value);
3115 exec->inputStack[exec->inputStackNr].data = data;
3116 exec->inputStackNr++;
3117 exec->inputStack[exec->inputStackNr].value = NULL;
3118 exec->inputStack[exec->inputStackNr].data = NULL;
3119}
3120
Daniel Veillardc0826a72004-08-10 14:17:33 +00003121/**
3122 * xmlRegStrEqualWildcard:
3123 * @expStr: the string to be evaluated
3124 * @valStr: the validation string
3125 *
3126 * Checks if both strings are equal or have the same content. "*"
3127 * can be used as a wildcard in @valStr; "|" is used as a seperator of
3128 * substrings in both @expStr and @valStr.
3129 *
3130 * Returns 1 if the comparison is satisfied and the number of substrings
3131 * is equal, 0 otherwise.
3132 */
3133
3134static int
3135xmlRegStrEqualWildcard(const xmlChar *expStr, const xmlChar *valStr) {
3136 if (expStr == valStr) return(1);
3137 if (expStr == NULL) return(0);
3138 if (valStr == NULL) return(0);
3139 do {
3140 /*
3141 * Eval if we have a wildcard for the current item.
3142 */
3143 if (*expStr != *valStr) {
Daniel Veillard4f82c8a2005-08-09 21:40:08 +00003144 /* if one of them starts with a wildcard make valStr be it */
3145 if (*valStr == '*') {
3146 const xmlChar *tmp;
3147
3148 tmp = valStr;
3149 valStr = expStr;
3150 expStr = tmp;
3151 }
Daniel Veillardc0826a72004-08-10 14:17:33 +00003152 if ((*valStr != 0) && (*expStr != 0) && (*expStr++ == '*')) {
3153 do {
3154 if (*valStr == XML_REG_STRING_SEPARATOR)
3155 break;
Kasimier T. Buchcikc0e833f2005-04-19 15:02:20 +00003156 valStr++;
Daniel Veillardc0826a72004-08-10 14:17:33 +00003157 } while (*valStr != 0);
3158 continue;
3159 } else
3160 return(0);
3161 }
Kasimier T. Buchcikc0e833f2005-04-19 15:02:20 +00003162 expStr++;
3163 valStr++;
Daniel Veillardc0826a72004-08-10 14:17:33 +00003164 } while (*valStr != 0);
3165 if (*expStr != 0)
3166 return (0);
3167 else
3168 return (1);
3169}
Daniel Veillard4255d502002-04-16 15:50:10 +00003170
3171/**
Daniel Veillard23e73572002-09-19 19:56:43 +00003172 * xmlRegCompactPushString:
3173 * @exec: a regexp execution context
3174 * @comp: the precompiled exec with a compact table
3175 * @value: a string token input
3176 * @data: data associated to the token to reuse in callbacks
3177 *
3178 * Push one input token in the execution context
3179 *
3180 * Returns: 1 if the regexp reached a final state, 0 if non-final, and
3181 * a negative value in case of error.
3182 */
3183static int
3184xmlRegCompactPushString(xmlRegExecCtxtPtr exec,
3185 xmlRegexpPtr comp,
3186 const xmlChar *value,
3187 void *data) {
3188 int state = exec->index;
3189 int i, target;
3190
3191 if ((comp == NULL) || (comp->compact == NULL) || (comp->stringMap == NULL))
3192 return(-1);
3193
3194 if (value == NULL) {
3195 /*
3196 * are we at a final state ?
3197 */
3198 if (comp->compact[state * (comp->nbstrings + 1)] ==
3199 XML_REGEXP_FINAL_STATE)
3200 return(1);
3201 return(0);
3202 }
3203
3204#ifdef DEBUG_PUSH
3205 printf("value pushed: %s\n", value);
3206#endif
3207
3208 /*
William M. Brackddf71d62004-05-06 04:17:26 +00003209 * Examine all outside transitions from current state
Daniel Veillard23e73572002-09-19 19:56:43 +00003210 */
3211 for (i = 0;i < comp->nbstrings;i++) {
3212 target = comp->compact[state * (comp->nbstrings + 1) + i + 1];
3213 if ((target > 0) && (target <= comp->nbstates)) {
Daniel Veillardc0826a72004-08-10 14:17:33 +00003214 target--; /* to avoid 0 */
3215 if (xmlRegStrEqualWildcard(comp->stringMap[i], value)) {
3216 exec->index = target;
Daniel Veillard118aed72002-09-24 14:13:13 +00003217 if ((exec->callback != NULL) && (comp->transdata != NULL)) {
3218 exec->callback(exec->data, value,
3219 comp->transdata[state * comp->nbstrings + i], data);
3220 }
Daniel Veillard23e73572002-09-19 19:56:43 +00003221#ifdef DEBUG_PUSH
3222 printf("entering state %d\n", target);
3223#endif
3224 if (comp->compact[target * (comp->nbstrings + 1)] ==
Daniel Veillardcc026dc2005-01-12 13:21:17 +00003225 XML_REGEXP_SINK_STATE)
3226 goto error;
3227
3228 if (comp->compact[target * (comp->nbstrings + 1)] ==
Daniel Veillard23e73572002-09-19 19:56:43 +00003229 XML_REGEXP_FINAL_STATE)
3230 return(1);
3231 return(0);
3232 }
3233 }
3234 }
3235 /*
3236 * Failed to find an exit transition out from current state for the
3237 * current token
3238 */
3239#ifdef DEBUG_PUSH
3240 printf("failed to find a transition for %s on state %d\n", value, state);
3241#endif
Daniel Veillardcc026dc2005-01-12 13:21:17 +00003242error:
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003243 if (exec->errString != NULL)
3244 xmlFree(exec->errString);
3245 exec->errString = xmlStrdup(value);
3246 exec->errStateNo = state;
Daniel Veillard23e73572002-09-19 19:56:43 +00003247 exec->status = -1;
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003248#ifdef DEBUG_ERR
3249 testerr(exec);
3250#endif
Daniel Veillard23e73572002-09-19 19:56:43 +00003251 return(-1);
3252}
3253
3254/**
Daniel Veillard6e65e152005-08-09 11:09:52 +00003255 * xmlRegExecPushStringInternal:
Daniel Veillardea7751d2002-12-20 00:16:24 +00003256 * @exec: a regexp execution context or NULL to indicate the end
Daniel Veillard4255d502002-04-16 15:50:10 +00003257 * @value: a string token input
3258 * @data: data associated to the token to reuse in callbacks
Daniel Veillard6e65e152005-08-09 11:09:52 +00003259 * @compound: value was assembled from 2 strings
Daniel Veillard4255d502002-04-16 15:50:10 +00003260 *
3261 * Push one input token in the execution context
3262 *
3263 * Returns: 1 if the regexp reached a final state, 0 if non-final, and
3264 * a negative value in case of error.
3265 */
Daniel Veillard6e65e152005-08-09 11:09:52 +00003266static int
3267xmlRegExecPushStringInternal(xmlRegExecCtxtPtr exec, const xmlChar *value,
3268 void *data, int compound) {
Daniel Veillard4255d502002-04-16 15:50:10 +00003269 xmlRegTransPtr trans;
3270 xmlRegAtomPtr atom;
3271 int ret;
3272 int final = 0;
Daniel Veillard90700152005-01-08 22:05:09 +00003273 int progress = 1;
Daniel Veillard4255d502002-04-16 15:50:10 +00003274
3275 if (exec == NULL)
3276 return(-1);
Daniel Veillard23e73572002-09-19 19:56:43 +00003277 if (exec->comp == NULL)
3278 return(-1);
Daniel Veillard4255d502002-04-16 15:50:10 +00003279 if (exec->status != 0)
3280 return(exec->status);
3281
Daniel Veillard23e73572002-09-19 19:56:43 +00003282 if (exec->comp->compact != NULL)
3283 return(xmlRegCompactPushString(exec, exec->comp, value, data));
3284
Daniel Veillard4255d502002-04-16 15:50:10 +00003285 if (value == NULL) {
3286 if (exec->state->type == XML_REGEXP_FINAL_STATE)
3287 return(1);
3288 final = 1;
3289 }
3290
3291#ifdef DEBUG_PUSH
3292 printf("value pushed: %s\n", value);
3293#endif
3294 /*
3295 * If we have an active rollback stack push the new value there
3296 * and get back to where we were left
3297 */
3298 if ((value != NULL) && (exec->inputStackNr > 0)) {
3299 xmlFARegExecSaveInputString(exec, value, data);
3300 value = exec->inputStack[exec->index].value;
3301 data = exec->inputStack[exec->index].data;
3302#ifdef DEBUG_PUSH
3303 printf("value loaded: %s\n", value);
3304#endif
3305 }
3306
3307 while ((exec->status == 0) &&
3308 ((value != NULL) ||
3309 ((final == 1) &&
3310 (exec->state->type != XML_REGEXP_FINAL_STATE)))) {
3311
3312 /*
3313 * End of input on non-terminal state, rollback, however we may
3314 * still have epsilon like transition for counted transitions
3315 * on counters, in that case don't break too early.
3316 */
Daniel Veillardb509f152002-04-17 16:28:10 +00003317 if ((value == NULL) && (exec->counts == NULL))
Daniel Veillard4255d502002-04-16 15:50:10 +00003318 goto rollback;
3319
3320 exec->transcount = 0;
3321 for (;exec->transno < exec->state->nbTrans;exec->transno++) {
3322 trans = &exec->state->trans[exec->transno];
3323 if (trans->to < 0)
3324 continue;
3325 atom = trans->atom;
3326 ret = 0;
Daniel Veillard441bc322002-04-20 17:38:48 +00003327 if (trans->count == REGEXP_ALL_LAX_COUNTER) {
3328 int i;
3329 int count;
3330 xmlRegTransPtr t;
3331 xmlRegCounterPtr counter;
3332
3333 ret = 0;
3334
3335#ifdef DEBUG_PUSH
3336 printf("testing all lax %d\n", trans->count);
3337#endif
3338 /*
3339 * Check all counted transitions from the current state
3340 */
3341 if ((value == NULL) && (final)) {
3342 ret = 1;
3343 } else if (value != NULL) {
3344 for (i = 0;i < exec->state->nbTrans;i++) {
3345 t = &exec->state->trans[i];
3346 if ((t->counter < 0) || (t == trans))
3347 continue;
3348 counter = &exec->comp->counters[t->counter];
3349 count = exec->counts[t->counter];
3350 if ((count < counter->max) &&
3351 (t->atom != NULL) &&
3352 (xmlStrEqual(value, t->atom->valuep))) {
3353 ret = 0;
3354 break;
3355 }
3356 if ((count >= counter->min) &&
3357 (count < counter->max) &&
3358 (xmlStrEqual(value, t->atom->valuep))) {
3359 ret = 1;
3360 break;
3361 }
3362 }
3363 }
3364 } else if (trans->count == REGEXP_ALL_COUNTER) {
Daniel Veillard8a001f62002-04-20 07:24:11 +00003365 int i;
3366 int count;
3367 xmlRegTransPtr t;
3368 xmlRegCounterPtr counter;
3369
3370 ret = 1;
3371
3372#ifdef DEBUG_PUSH
3373 printf("testing all %d\n", trans->count);
3374#endif
3375 /*
3376 * Check all counted transitions from the current state
3377 */
3378 for (i = 0;i < exec->state->nbTrans;i++) {
3379 t = &exec->state->trans[i];
3380 if ((t->counter < 0) || (t == trans))
3381 continue;
3382 counter = &exec->comp->counters[t->counter];
3383 count = exec->counts[t->counter];
3384 if ((count < counter->min) || (count > counter->max)) {
3385 ret = 0;
3386 break;
3387 }
3388 }
3389 } else if (trans->count >= 0) {
Daniel Veillard4255d502002-04-16 15:50:10 +00003390 int count;
3391 xmlRegCounterPtr counter;
3392
3393 /*
3394 * A counted transition.
3395 */
3396
3397 count = exec->counts[trans->count];
3398 counter = &exec->comp->counters[trans->count];
3399#ifdef DEBUG_PUSH
3400 printf("testing count %d: val %d, min %d, max %d\n",
3401 trans->count, count, counter->min, counter->max);
3402#endif
3403 ret = ((count >= counter->min) && (count <= counter->max));
3404 } else if (atom == NULL) {
3405 fprintf(stderr, "epsilon transition left at runtime\n");
3406 exec->status = -2;
3407 break;
3408 } else if (value != NULL) {
Daniel Veillardc0826a72004-08-10 14:17:33 +00003409 ret = xmlRegStrEqualWildcard(atom->valuep, value);
Daniel Veillard6e65e152005-08-09 11:09:52 +00003410 if (atom->neg) {
Daniel Veillard9efc4762005-07-19 14:33:55 +00003411 ret = !ret;
Daniel Veillard6e65e152005-08-09 11:09:52 +00003412 if (!compound)
3413 ret = 0;
3414 }
Daniel Veillard441bc322002-04-20 17:38:48 +00003415 if ((ret == 1) && (trans->counter >= 0)) {
3416 xmlRegCounterPtr counter;
3417 int count;
3418
3419 count = exec->counts[trans->counter];
3420 counter = &exec->comp->counters[trans->counter];
3421 if (count >= counter->max)
3422 ret = 0;
3423 }
3424
Daniel Veillard4255d502002-04-16 15:50:10 +00003425 if ((ret == 1) && (atom->min > 0) && (atom->max > 0)) {
3426 xmlRegStatePtr to = exec->comp->states[trans->to];
3427
3428 /*
3429 * this is a multiple input sequence
3430 */
3431 if (exec->state->nbTrans > exec->transno + 1) {
3432 if (exec->inputStackNr <= 0) {
3433 xmlFARegExecSaveInputString(exec, value, data);
3434 }
3435 xmlFARegExecSave(exec);
3436 }
3437 exec->transcount = 1;
3438 do {
3439 /*
3440 * Try to progress as much as possible on the input
3441 */
3442 if (exec->transcount == atom->max) {
3443 break;
3444 }
3445 exec->index++;
3446 value = exec->inputStack[exec->index].value;
3447 data = exec->inputStack[exec->index].data;
3448#ifdef DEBUG_PUSH
3449 printf("value loaded: %s\n", value);
3450#endif
3451
3452 /*
3453 * End of input: stop here
3454 */
3455 if (value == NULL) {
3456 exec->index --;
3457 break;
3458 }
3459 if (exec->transcount >= atom->min) {
3460 int transno = exec->transno;
3461 xmlRegStatePtr state = exec->state;
3462
3463 /*
3464 * The transition is acceptable save it
3465 */
3466 exec->transno = -1; /* trick */
3467 exec->state = to;
3468 if (exec->inputStackNr <= 0) {
3469 xmlFARegExecSaveInputString(exec, value, data);
3470 }
3471 xmlFARegExecSave(exec);
3472 exec->transno = transno;
3473 exec->state = state;
3474 }
3475 ret = xmlStrEqual(value, atom->valuep);
3476 exec->transcount++;
3477 } while (ret == 1);
3478 if (exec->transcount < atom->min)
3479 ret = 0;
3480
3481 /*
3482 * If the last check failed but one transition was found
3483 * possible, rollback
3484 */
3485 if (ret < 0)
3486 ret = 0;
3487 if (ret == 0) {
3488 goto rollback;
3489 }
3490 }
3491 }
3492 if (ret == 1) {
William M. Brack98873952003-12-26 06:03:14 +00003493 if ((exec->callback != NULL) && (atom != NULL) &&
3494 (data != NULL)) {
Daniel Veillard4255d502002-04-16 15:50:10 +00003495 exec->callback(exec->data, atom->valuep,
3496 atom->data, data);
3497 }
3498 if (exec->state->nbTrans > exec->transno + 1) {
3499 if (exec->inputStackNr <= 0) {
3500 xmlFARegExecSaveInputString(exec, value, data);
3501 }
3502 xmlFARegExecSave(exec);
3503 }
3504 if (trans->counter >= 0) {
3505#ifdef DEBUG_PUSH
3506 printf("Increasing count %d\n", trans->counter);
3507#endif
3508 exec->counts[trans->counter]++;
3509 }
Daniel Veillard10752282005-08-08 13:05:13 +00003510 if ((trans->count >= 0) &&
3511 (trans->count < REGEXP_ALL_COUNTER)) {
3512#ifdef DEBUG_REGEXP_EXEC
3513 printf("resetting count %d on transition\n",
3514 trans->count);
3515#endif
3516 exec->counts[trans->count] = 0;
3517 }
Daniel Veillard4255d502002-04-16 15:50:10 +00003518#ifdef DEBUG_PUSH
3519 printf("entering state %d\n", trans->to);
3520#endif
Daniel Veillardcc026dc2005-01-12 13:21:17 +00003521 if ((exec->comp->states[trans->to] != NULL) &&
3522 (exec->comp->states[trans->to]->type ==
3523 XML_REGEXP_SINK_STATE)) {
3524 /*
3525 * entering a sink state, save the current state as error
3526 * state.
3527 */
3528 if (exec->errString != NULL)
3529 xmlFree(exec->errString);
3530 exec->errString = xmlStrdup(value);
3531 exec->errState = exec->state;
3532 memcpy(exec->errCounts, exec->counts,
3533 exec->comp->nbCounters * sizeof(int));
3534 }
Daniel Veillard4255d502002-04-16 15:50:10 +00003535 exec->state = exec->comp->states[trans->to];
3536 exec->transno = 0;
3537 if (trans->atom != NULL) {
3538 if (exec->inputStack != NULL) {
3539 exec->index++;
3540 if (exec->index < exec->inputStackNr) {
3541 value = exec->inputStack[exec->index].value;
3542 data = exec->inputStack[exec->index].data;
3543#ifdef DEBUG_PUSH
3544 printf("value loaded: %s\n", value);
3545#endif
3546 } else {
3547 value = NULL;
3548 data = NULL;
3549#ifdef DEBUG_PUSH
3550 printf("end of input\n");
3551#endif
3552 }
3553 } else {
3554 value = NULL;
3555 data = NULL;
3556#ifdef DEBUG_PUSH
3557 printf("end of input\n");
3558#endif
3559 }
3560 }
3561 goto progress;
3562 } else if (ret < 0) {
3563 exec->status = -4;
3564 break;
3565 }
3566 }
3567 if ((exec->transno != 0) || (exec->state->nbTrans == 0)) {
3568rollback:
Daniel Veillard90700152005-01-08 22:05:09 +00003569 /*
Daniel Veillardcc026dc2005-01-12 13:21:17 +00003570 * if we didn't yet rollback on the current input
3571 * store the current state as the error state.
Daniel Veillard90700152005-01-08 22:05:09 +00003572 */
Daniel Veillardcc026dc2005-01-12 13:21:17 +00003573 if ((progress) && (exec->state != NULL) &&
3574 (exec->state->type != XML_REGEXP_SINK_STATE)) {
Daniel Veillard90700152005-01-08 22:05:09 +00003575 progress = 0;
3576 if (exec->errString != NULL)
3577 xmlFree(exec->errString);
3578 exec->errString = xmlStrdup(value);
3579 exec->errState = exec->state;
3580 memcpy(exec->errCounts, exec->counts,
3581 exec->comp->nbCounters * sizeof(int));
3582 }
3583
Daniel Veillard4255d502002-04-16 15:50:10 +00003584 /*
3585 * Failed to find a way out
3586 */
3587 exec->determinist = 0;
3588 xmlFARegExecRollBack(exec);
3589 if (exec->status == 0) {
3590 value = exec->inputStack[exec->index].value;
3591 data = exec->inputStack[exec->index].data;
3592#ifdef DEBUG_PUSH
3593 printf("value loaded: %s\n", value);
3594#endif
3595 }
3596 }
Daniel Veillard90700152005-01-08 22:05:09 +00003597 continue;
Daniel Veillard4255d502002-04-16 15:50:10 +00003598progress:
Daniel Veillard90700152005-01-08 22:05:09 +00003599 progress = 1;
Daniel Veillard4255d502002-04-16 15:50:10 +00003600 continue;
3601 }
3602 if (exec->status == 0) {
3603 return(exec->state->type == XML_REGEXP_FINAL_STATE);
3604 }
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003605#ifdef DEBUG_ERR
Daniel Veillard90700152005-01-08 22:05:09 +00003606 if (exec->status < 0) {
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003607 testerr(exec);
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003608 }
Daniel Veillard90700152005-01-08 22:05:09 +00003609#endif
Daniel Veillard4255d502002-04-16 15:50:10 +00003610 return(exec->status);
3611}
3612
Daniel Veillard52b48c72003-04-13 19:53:42 +00003613/**
Daniel Veillard6e65e152005-08-09 11:09:52 +00003614 * xmlRegExecPushString:
3615 * @exec: a regexp execution context or NULL to indicate the end
3616 * @value: a string token input
3617 * @data: data associated to the token to reuse in callbacks
3618 *
3619 * Push one input token in the execution context
3620 *
3621 * Returns: 1 if the regexp reached a final state, 0 if non-final, and
3622 * a negative value in case of error.
3623 */
3624int
3625xmlRegExecPushString(xmlRegExecCtxtPtr exec, const xmlChar *value,
3626 void *data) {
3627 return(xmlRegExecPushStringInternal(exec, value, data, 0));
3628}
3629
3630/**
Daniel Veillard52b48c72003-04-13 19:53:42 +00003631 * xmlRegExecPushString2:
3632 * @exec: a regexp execution context or NULL to indicate the end
3633 * @value: the first string token input
3634 * @value2: the second string token input
3635 * @data: data associated to the token to reuse in callbacks
3636 *
3637 * Push one input token in the execution context
3638 *
3639 * Returns: 1 if the regexp reached a final state, 0 if non-final, and
3640 * a negative value in case of error.
3641 */
3642int
3643xmlRegExecPushString2(xmlRegExecCtxtPtr exec, const xmlChar *value,
3644 const xmlChar *value2, void *data) {
3645 xmlChar buf[150];
3646 int lenn, lenp, ret;
3647 xmlChar *str;
3648
3649 if (exec == NULL)
3650 return(-1);
3651 if (exec->comp == NULL)
3652 return(-1);
3653 if (exec->status != 0)
3654 return(exec->status);
3655
3656 if (value2 == NULL)
3657 return(xmlRegExecPushString(exec, value, data));
3658
3659 lenn = strlen((char *) value2);
3660 lenp = strlen((char *) value);
3661
3662 if (150 < lenn + lenp + 2) {
Daniel Veillard3c908dc2003-04-19 00:07:51 +00003663 str = (xmlChar *) xmlMallocAtomic(lenn + lenp + 2);
Daniel Veillard52b48c72003-04-13 19:53:42 +00003664 if (str == NULL) {
3665 exec->status = -1;
3666 return(-1);
3667 }
3668 } else {
3669 str = buf;
3670 }
3671 memcpy(&str[0], value, lenp);
Daniel Veillardc0826a72004-08-10 14:17:33 +00003672 str[lenp] = XML_REG_STRING_SEPARATOR;
Daniel Veillard52b48c72003-04-13 19:53:42 +00003673 memcpy(&str[lenp + 1], value2, lenn);
3674 str[lenn + lenp + 1] = 0;
3675
3676 if (exec->comp->compact != NULL)
3677 ret = xmlRegCompactPushString(exec, exec->comp, str, data);
3678 else
Daniel Veillard6e65e152005-08-09 11:09:52 +00003679 ret = xmlRegExecPushStringInternal(exec, str, data, 1);
Daniel Veillard52b48c72003-04-13 19:53:42 +00003680
3681 if (str != buf)
Daniel Veillard0b1ff142005-12-28 21:13:33 +00003682 xmlFree(str);
Daniel Veillard52b48c72003-04-13 19:53:42 +00003683 return(ret);
3684}
3685
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003686/**
Daniel Veillard77005e62005-07-19 16:26:18 +00003687 * xmlRegExecGetValues:
Daniel Veillardfc0b6f62005-01-09 17:48:02 +00003688 * @exec: a regexp execution context
3689 * @err: error extraction or normal one
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003690 * @nbval: pointer to the number of accepted values IN/OUT
Daniel Veillardcc026dc2005-01-12 13:21:17 +00003691 * @nbneg: return number of negative transitions
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003692 * @values: pointer to the array of acceptable values
Daniel Veillardfc0b6f62005-01-09 17:48:02 +00003693 * @terminal: return value if this was a terminal state
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003694 *
Daniel Veillardfc0b6f62005-01-09 17:48:02 +00003695 * Extract informations from the regexp execution, internal routine to
3696 * implement xmlRegExecNextValues() and xmlRegExecErrInfo()
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003697 *
3698 * Returns: 0 in case of success or -1 in case of error.
3699 */
Daniel Veillardfc0b6f62005-01-09 17:48:02 +00003700static int
3701xmlRegExecGetValues(xmlRegExecCtxtPtr exec, int err,
Daniel Veillardcc026dc2005-01-12 13:21:17 +00003702 int *nbval, int *nbneg,
3703 xmlChar **values, int *terminal) {
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003704 int maxval;
Daniel Veillardcc026dc2005-01-12 13:21:17 +00003705 int nb = 0;
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003706
Daniel Veillardcc026dc2005-01-12 13:21:17 +00003707 if ((exec == NULL) || (nbval == NULL) || (nbneg == NULL) ||
3708 (values == NULL) || (*nbval <= 0))
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003709 return(-1);
Daniel Veillardfc0b6f62005-01-09 17:48:02 +00003710
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003711 maxval = *nbval;
3712 *nbval = 0;
Daniel Veillardcc026dc2005-01-12 13:21:17 +00003713 *nbneg = 0;
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003714 if ((exec->comp != NULL) && (exec->comp->compact != NULL)) {
3715 xmlRegexpPtr comp;
3716 int target, i, state;
3717
3718 comp = exec->comp;
Daniel Veillardfc0b6f62005-01-09 17:48:02 +00003719
3720 if (err) {
3721 if (exec->errStateNo == -1) return(-1);
3722 state = exec->errStateNo;
3723 } else {
3724 state = exec->index;
3725 }
3726 if (terminal != NULL) {
3727 if (comp->compact[state * (comp->nbstrings + 1)] ==
3728 XML_REGEXP_FINAL_STATE)
3729 *terminal = 1;
3730 else
3731 *terminal = 0;
3732 }
Daniel Veillardcc026dc2005-01-12 13:21:17 +00003733 for (i = 0;(i < comp->nbstrings) && (nb < maxval);i++) {
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003734 target = comp->compact[state * (comp->nbstrings + 1) + i + 1];
Daniel Veillardcc026dc2005-01-12 13:21:17 +00003735 if ((target > 0) && (target <= comp->nbstates) &&
3736 (comp->compact[(target - 1) * (comp->nbstrings + 1)] !=
3737 XML_REGEXP_SINK_STATE)) {
3738 values[nb++] = comp->stringMap[i];
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003739 (*nbval)++;
3740 }
3741 }
Daniel Veillardcc026dc2005-01-12 13:21:17 +00003742 for (i = 0;(i < comp->nbstrings) && (nb < maxval);i++) {
3743 target = comp->compact[state * (comp->nbstrings + 1) + i + 1];
3744 if ((target > 0) && (target <= comp->nbstates) &&
3745 (comp->compact[(target - 1) * (comp->nbstrings + 1)] ==
3746 XML_REGEXP_SINK_STATE)) {
3747 values[nb++] = comp->stringMap[i];
3748 (*nbneg)++;
3749 }
3750 }
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003751 } else {
3752 int transno;
3753 xmlRegTransPtr trans;
3754 xmlRegAtomPtr atom;
Daniel Veillardfc0b6f62005-01-09 17:48:02 +00003755 xmlRegStatePtr state;
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003756
Daniel Veillardfc0b6f62005-01-09 17:48:02 +00003757 if (terminal != NULL) {
3758 if (exec->state->type == XML_REGEXP_FINAL_STATE)
3759 *terminal = 1;
3760 else
3761 *terminal = 0;
3762 }
3763
3764 if (err) {
3765 if (exec->errState == NULL) return(-1);
3766 state = exec->errState;
3767 } else {
3768 if (exec->state == NULL) return(-1);
3769 state = exec->state;
3770 }
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003771 for (transno = 0;
Daniel Veillardcc026dc2005-01-12 13:21:17 +00003772 (transno < state->nbTrans) && (nb < maxval);
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003773 transno++) {
Daniel Veillardfc0b6f62005-01-09 17:48:02 +00003774 trans = &state->trans[transno];
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003775 if (trans->to < 0)
3776 continue;
3777 atom = trans->atom;
3778 if ((atom == NULL) || (atom->valuep == NULL))
3779 continue;
3780 if (trans->count == REGEXP_ALL_LAX_COUNTER) {
Daniel Veillardcc026dc2005-01-12 13:21:17 +00003781 /* this should not be reached but ... */
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003782 TODO;
3783 } else if (trans->count == REGEXP_ALL_COUNTER) {
Daniel Veillardcc026dc2005-01-12 13:21:17 +00003784 /* this should not be reached but ... */
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003785 TODO;
3786 } else if (trans->counter >= 0) {
3787 xmlRegCounterPtr counter;
3788 int count;
3789
Daniel Veillardfc0b6f62005-01-09 17:48:02 +00003790 if (err)
3791 count = exec->errCounts[trans->counter];
3792 else
3793 count = exec->counts[trans->counter];
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003794 counter = &exec->comp->counters[trans->counter];
3795 if (count < counter->max) {
Daniel Veillard77005e62005-07-19 16:26:18 +00003796 if (atom->neg)
3797 values[nb++] = (xmlChar *) atom->valuep2;
3798 else
3799 values[nb++] = (xmlChar *) atom->valuep;
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003800 (*nbval)++;
3801 }
3802 } else {
Daniel Veillardcc026dc2005-01-12 13:21:17 +00003803 if ((exec->comp->states[trans->to] != NULL) &&
3804 (exec->comp->states[trans->to]->type !=
3805 XML_REGEXP_SINK_STATE)) {
Daniel Veillard77005e62005-07-19 16:26:18 +00003806 if (atom->neg)
3807 values[nb++] = (xmlChar *) atom->valuep2;
3808 else
3809 values[nb++] = (xmlChar *) atom->valuep;
Daniel Veillardcc026dc2005-01-12 13:21:17 +00003810 (*nbval)++;
3811 }
3812 }
3813 }
3814 for (transno = 0;
3815 (transno < state->nbTrans) && (nb < maxval);
3816 transno++) {
3817 trans = &state->trans[transno];
3818 if (trans->to < 0)
3819 continue;
3820 atom = trans->atom;
3821 if ((atom == NULL) || (atom->valuep == NULL))
3822 continue;
3823 if (trans->count == REGEXP_ALL_LAX_COUNTER) {
3824 continue;
3825 } else if (trans->count == REGEXP_ALL_COUNTER) {
3826 continue;
3827 } else if (trans->counter >= 0) {
3828 continue;
3829 } else {
3830 if ((exec->comp->states[trans->to] != NULL) &&
3831 (exec->comp->states[trans->to]->type ==
3832 XML_REGEXP_SINK_STATE)) {
Daniel Veillard77005e62005-07-19 16:26:18 +00003833 if (atom->neg)
3834 values[nb++] = (xmlChar *) atom->valuep2;
3835 else
3836 values[nb++] = (xmlChar *) atom->valuep;
Daniel Veillardcc026dc2005-01-12 13:21:17 +00003837 (*nbneg)++;
3838 }
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003839 }
3840 }
3841 }
3842 return(0);
3843}
3844
Daniel Veillardfc0b6f62005-01-09 17:48:02 +00003845/**
3846 * xmlRegExecNextValues:
3847 * @exec: a regexp execution context
3848 * @nbval: pointer to the number of accepted values IN/OUT
Daniel Veillardcc026dc2005-01-12 13:21:17 +00003849 * @nbneg: return number of negative transitions
Daniel Veillardfc0b6f62005-01-09 17:48:02 +00003850 * @values: pointer to the array of acceptable values
3851 * @terminal: return value if this was a terminal state
3852 *
3853 * Extract informations from the regexp execution,
3854 * the parameter @values must point to an array of @nbval string pointers
3855 * on return nbval will contain the number of possible strings in that
3856 * state and the @values array will be updated with them. The string values
3857 * returned will be freed with the @exec context and don't need to be
3858 * deallocated.
3859 *
3860 * Returns: 0 in case of success or -1 in case of error.
3861 */
3862int
Daniel Veillardcc026dc2005-01-12 13:21:17 +00003863xmlRegExecNextValues(xmlRegExecCtxtPtr exec, int *nbval, int *nbneg,
3864 xmlChar **values, int *terminal) {
3865 return(xmlRegExecGetValues(exec, 0, nbval, nbneg, values, terminal));
Daniel Veillardfc0b6f62005-01-09 17:48:02 +00003866}
3867
3868/**
3869 * xmlRegExecErrInfo:
3870 * @exec: a regexp execution context generating an error
3871 * @string: return value for the error string
3872 * @nbval: pointer to the number of accepted values IN/OUT
Daniel Veillardcc026dc2005-01-12 13:21:17 +00003873 * @nbneg: return number of negative transitions
Daniel Veillardfc0b6f62005-01-09 17:48:02 +00003874 * @values: pointer to the array of acceptable values
3875 * @terminal: return value if this was a terminal state
3876 *
3877 * Extract error informations from the regexp execution, the parameter
3878 * @string will be updated with the value pushed and not accepted,
3879 * the parameter @values must point to an array of @nbval string pointers
3880 * on return nbval will contain the number of possible strings in that
3881 * state and the @values array will be updated with them. The string values
3882 * returned will be freed with the @exec context and don't need to be
3883 * deallocated.
3884 *
3885 * Returns: 0 in case of success or -1 in case of error.
3886 */
3887int
3888xmlRegExecErrInfo(xmlRegExecCtxtPtr exec, const xmlChar **string,
Daniel Veillardcc026dc2005-01-12 13:21:17 +00003889 int *nbval, int *nbneg, xmlChar **values, int *terminal) {
Daniel Veillardfc0b6f62005-01-09 17:48:02 +00003890 if (exec == NULL)
3891 return(-1);
3892 if (string != NULL) {
3893 if (exec->status != 0)
3894 *string = exec->errString;
3895 else
3896 *string = NULL;
3897 }
Daniel Veillardcc026dc2005-01-12 13:21:17 +00003898 return(xmlRegExecGetValues(exec, 1, nbval, nbneg, values, terminal));
Daniel Veillardfc0b6f62005-01-09 17:48:02 +00003899}
3900
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003901#ifdef DEBUG_ERR
3902static void testerr(xmlRegExecCtxtPtr exec) {
3903 const xmlChar *string;
Daniel Veillardcee2b3a2005-01-25 00:22:52 +00003904 xmlChar *values[5];
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003905 int nb = 5;
Daniel Veillardcc026dc2005-01-12 13:21:17 +00003906 int nbneg;
Daniel Veillardfc0b6f62005-01-09 17:48:02 +00003907 int terminal;
Daniel Veillardcc026dc2005-01-12 13:21:17 +00003908 xmlRegExecErrInfo(exec, &string, &nb, &nbneg, &values[0], &terminal);
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003909}
3910#endif
3911
Daniel Veillard4255d502002-04-16 15:50:10 +00003912#if 0
3913static int
3914xmlRegExecPushChar(xmlRegExecCtxtPtr exec, int UCS) {
3915 xmlRegTransPtr trans;
3916 xmlRegAtomPtr atom;
3917 int ret;
3918 int codepoint, len;
3919
3920 if (exec == NULL)
3921 return(-1);
3922 if (exec->status != 0)
3923 return(exec->status);
3924
3925 while ((exec->status == 0) &&
3926 ((exec->inputString[exec->index] != 0) ||
3927 (exec->state->type != XML_REGEXP_FINAL_STATE))) {
3928
3929 /*
3930 * End of input on non-terminal state, rollback, however we may
3931 * still have epsilon like transition for counted transitions
3932 * on counters, in that case don't break too early.
3933 */
3934 if ((exec->inputString[exec->index] == 0) && (exec->counts == NULL))
3935 goto rollback;
3936
3937 exec->transcount = 0;
3938 for (;exec->transno < exec->state->nbTrans;exec->transno++) {
3939 trans = &exec->state->trans[exec->transno];
3940 if (trans->to < 0)
3941 continue;
3942 atom = trans->atom;
3943 ret = 0;
3944 if (trans->count >= 0) {
3945 int count;
3946 xmlRegCounterPtr counter;
3947
3948 /*
3949 * A counted transition.
3950 */
3951
3952 count = exec->counts[trans->count];
3953 counter = &exec->comp->counters[trans->count];
3954#ifdef DEBUG_REGEXP_EXEC
3955 printf("testing count %d: val %d, min %d, max %d\n",
3956 trans->count, count, counter->min, counter->max);
3957#endif
3958 ret = ((count >= counter->min) && (count <= counter->max));
3959 } else if (atom == NULL) {
3960 fprintf(stderr, "epsilon transition left at runtime\n");
3961 exec->status = -2;
3962 break;
3963 } else if (exec->inputString[exec->index] != 0) {
3964 codepoint = CUR_SCHAR(&(exec->inputString[exec->index]), len);
3965 ret = xmlRegCheckCharacter(atom, codepoint);
3966 if ((ret == 1) && (atom->min > 0) && (atom->max > 0)) {
3967 xmlRegStatePtr to = exec->comp->states[trans->to];
3968
3969 /*
3970 * this is a multiple input sequence
3971 */
3972 if (exec->state->nbTrans > exec->transno + 1) {
3973 xmlFARegExecSave(exec);
3974 }
3975 exec->transcount = 1;
3976 do {
3977 /*
3978 * Try to progress as much as possible on the input
3979 */
3980 if (exec->transcount == atom->max) {
3981 break;
3982 }
3983 exec->index += len;
3984 /*
3985 * End of input: stop here
3986 */
3987 if (exec->inputString[exec->index] == 0) {
3988 exec->index -= len;
3989 break;
3990 }
3991 if (exec->transcount >= atom->min) {
3992 int transno = exec->transno;
3993 xmlRegStatePtr state = exec->state;
3994
3995 /*
3996 * The transition is acceptable save it
3997 */
3998 exec->transno = -1; /* trick */
3999 exec->state = to;
4000 xmlFARegExecSave(exec);
4001 exec->transno = transno;
4002 exec->state = state;
4003 }
4004 codepoint = CUR_SCHAR(&(exec->inputString[exec->index]),
4005 len);
4006 ret = xmlRegCheckCharacter(atom, codepoint);
4007 exec->transcount++;
4008 } while (ret == 1);
4009 if (exec->transcount < atom->min)
4010 ret = 0;
4011
4012 /*
4013 * If the last check failed but one transition was found
4014 * possible, rollback
4015 */
4016 if (ret < 0)
4017 ret = 0;
4018 if (ret == 0) {
4019 goto rollback;
4020 }
4021 }
4022 }
4023 if (ret == 1) {
4024 if (exec->state->nbTrans > exec->transno + 1) {
4025 xmlFARegExecSave(exec);
4026 }
4027 if (trans->counter >= 0) {
4028#ifdef DEBUG_REGEXP_EXEC
4029 printf("Increasing count %d\n", trans->counter);
4030#endif
4031 exec->counts[trans->counter]++;
4032 }
4033#ifdef DEBUG_REGEXP_EXEC
4034 printf("entering state %d\n", trans->to);
4035#endif
4036 exec->state = exec->comp->states[trans->to];
4037 exec->transno = 0;
4038 if (trans->atom != NULL) {
4039 exec->index += len;
4040 }
4041 goto progress;
4042 } else if (ret < 0) {
4043 exec->status = -4;
4044 break;
4045 }
4046 }
4047 if ((exec->transno != 0) || (exec->state->nbTrans == 0)) {
4048rollback:
4049 /*
4050 * Failed to find a way out
4051 */
4052 exec->determinist = 0;
4053 xmlFARegExecRollBack(exec);
4054 }
4055progress:
4056 continue;
4057 }
4058}
4059#endif
4060/************************************************************************
4061 * *
William M. Brackddf71d62004-05-06 04:17:26 +00004062 * Parser for the Schemas Datatype Regular Expressions *
Daniel Veillard4255d502002-04-16 15:50:10 +00004063 * http://www.w3.org/TR/2001/REC-xmlschema-2-20010502/#regexs *
4064 * *
4065 ************************************************************************/
4066
4067/**
4068 * xmlFAIsChar:
Daniel Veillard441bc322002-04-20 17:38:48 +00004069 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00004070 *
4071 * [10] Char ::= [^.\?*+()|#x5B#x5D]
4072 */
4073static int
4074xmlFAIsChar(xmlRegParserCtxtPtr ctxt) {
4075 int cur;
4076 int len;
4077
4078 cur = CUR_SCHAR(ctxt->cur, len);
4079 if ((cur == '.') || (cur == '\\') || (cur == '?') ||
4080 (cur == '*') || (cur == '+') || (cur == '(') ||
4081 (cur == ')') || (cur == '|') || (cur == 0x5B) ||
4082 (cur == 0x5D) || (cur == 0))
4083 return(-1);
4084 return(cur);
4085}
4086
4087/**
4088 * xmlFAParseCharProp:
Daniel Veillard441bc322002-04-20 17:38:48 +00004089 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00004090 *
4091 * [27] charProp ::= IsCategory | IsBlock
4092 * [28] IsCategory ::= Letters | Marks | Numbers | Punctuation |
4093 * Separators | Symbols | Others
4094 * [29] Letters ::= 'L' [ultmo]?
4095 * [30] Marks ::= 'M' [nce]?
4096 * [31] Numbers ::= 'N' [dlo]?
4097 * [32] Punctuation ::= 'P' [cdseifo]?
4098 * [33] Separators ::= 'Z' [slp]?
4099 * [34] Symbols ::= 'S' [mcko]?
4100 * [35] Others ::= 'C' [cfon]?
4101 * [36] IsBlock ::= 'Is' [a-zA-Z0-9#x2D]+
4102 */
4103static void
4104xmlFAParseCharProp(xmlRegParserCtxtPtr ctxt) {
4105 int cur;
William M. Brack779af002003-08-01 15:55:39 +00004106 xmlRegAtomType type = (xmlRegAtomType) 0;
Daniel Veillard4255d502002-04-16 15:50:10 +00004107 xmlChar *blockName = NULL;
4108
4109 cur = CUR;
4110 if (cur == 'L') {
4111 NEXT;
4112 cur = CUR;
4113 if (cur == 'u') {
4114 NEXT;
4115 type = XML_REGEXP_LETTER_UPPERCASE;
4116 } else if (cur == 'l') {
4117 NEXT;
4118 type = XML_REGEXP_LETTER_LOWERCASE;
4119 } else if (cur == 't') {
4120 NEXT;
4121 type = XML_REGEXP_LETTER_TITLECASE;
4122 } else if (cur == 'm') {
4123 NEXT;
4124 type = XML_REGEXP_LETTER_MODIFIER;
4125 } else if (cur == 'o') {
4126 NEXT;
4127 type = XML_REGEXP_LETTER_OTHERS;
4128 } else {
4129 type = XML_REGEXP_LETTER;
4130 }
4131 } else if (cur == 'M') {
4132 NEXT;
4133 cur = CUR;
4134 if (cur == 'n') {
4135 NEXT;
4136 /* nonspacing */
4137 type = XML_REGEXP_MARK_NONSPACING;
4138 } else if (cur == 'c') {
4139 NEXT;
4140 /* spacing combining */
4141 type = XML_REGEXP_MARK_SPACECOMBINING;
4142 } else if (cur == 'e') {
4143 NEXT;
4144 /* enclosing */
4145 type = XML_REGEXP_MARK_ENCLOSING;
4146 } else {
4147 /* all marks */
4148 type = XML_REGEXP_MARK;
4149 }
4150 } else if (cur == 'N') {
4151 NEXT;
4152 cur = CUR;
4153 if (cur == 'd') {
4154 NEXT;
4155 /* digital */
4156 type = XML_REGEXP_NUMBER_DECIMAL;
4157 } else if (cur == 'l') {
4158 NEXT;
4159 /* letter */
4160 type = XML_REGEXP_NUMBER_LETTER;
4161 } else if (cur == 'o') {
4162 NEXT;
4163 /* other */
4164 type = XML_REGEXP_NUMBER_OTHERS;
4165 } else {
4166 /* all numbers */
4167 type = XML_REGEXP_NUMBER;
4168 }
4169 } else if (cur == 'P') {
4170 NEXT;
4171 cur = CUR;
4172 if (cur == 'c') {
4173 NEXT;
4174 /* connector */
4175 type = XML_REGEXP_PUNCT_CONNECTOR;
4176 } else if (cur == 'd') {
4177 NEXT;
4178 /* dash */
4179 type = XML_REGEXP_PUNCT_DASH;
4180 } else if (cur == 's') {
4181 NEXT;
4182 /* open */
4183 type = XML_REGEXP_PUNCT_OPEN;
4184 } else if (cur == 'e') {
4185 NEXT;
4186 /* close */
4187 type = XML_REGEXP_PUNCT_CLOSE;
4188 } else if (cur == 'i') {
4189 NEXT;
4190 /* initial quote */
4191 type = XML_REGEXP_PUNCT_INITQUOTE;
4192 } else if (cur == 'f') {
4193 NEXT;
4194 /* final quote */
4195 type = XML_REGEXP_PUNCT_FINQUOTE;
4196 } else if (cur == 'o') {
4197 NEXT;
4198 /* other */
4199 type = XML_REGEXP_PUNCT_OTHERS;
4200 } else {
4201 /* all punctuation */
4202 type = XML_REGEXP_PUNCT;
4203 }
4204 } else if (cur == 'Z') {
4205 NEXT;
4206 cur = CUR;
4207 if (cur == 's') {
4208 NEXT;
4209 /* space */
4210 type = XML_REGEXP_SEPAR_SPACE;
4211 } else if (cur == 'l') {
4212 NEXT;
4213 /* line */
4214 type = XML_REGEXP_SEPAR_LINE;
4215 } else if (cur == 'p') {
4216 NEXT;
4217 /* paragraph */
4218 type = XML_REGEXP_SEPAR_PARA;
4219 } else {
4220 /* all separators */
4221 type = XML_REGEXP_SEPAR;
4222 }
4223 } else if (cur == 'S') {
4224 NEXT;
4225 cur = CUR;
4226 if (cur == 'm') {
4227 NEXT;
4228 type = XML_REGEXP_SYMBOL_MATH;
4229 /* math */
4230 } else if (cur == 'c') {
4231 NEXT;
4232 type = XML_REGEXP_SYMBOL_CURRENCY;
4233 /* currency */
4234 } else if (cur == 'k') {
4235 NEXT;
4236 type = XML_REGEXP_SYMBOL_MODIFIER;
4237 /* modifiers */
4238 } else if (cur == 'o') {
4239 NEXT;
4240 type = XML_REGEXP_SYMBOL_OTHERS;
4241 /* other */
4242 } else {
4243 /* all symbols */
4244 type = XML_REGEXP_SYMBOL;
4245 }
4246 } else if (cur == 'C') {
4247 NEXT;
4248 cur = CUR;
4249 if (cur == 'c') {
4250 NEXT;
4251 /* control */
4252 type = XML_REGEXP_OTHER_CONTROL;
4253 } else if (cur == 'f') {
4254 NEXT;
4255 /* format */
4256 type = XML_REGEXP_OTHER_FORMAT;
4257 } else if (cur == 'o') {
4258 NEXT;
4259 /* private use */
4260 type = XML_REGEXP_OTHER_PRIVATE;
4261 } else if (cur == 'n') {
4262 NEXT;
4263 /* not assigned */
4264 type = XML_REGEXP_OTHER_NA;
4265 } else {
4266 /* all others */
4267 type = XML_REGEXP_OTHER;
4268 }
4269 } else if (cur == 'I') {
4270 const xmlChar *start;
4271 NEXT;
4272 cur = CUR;
4273 if (cur != 's') {
4274 ERROR("IsXXXX expected");
4275 return;
4276 }
4277 NEXT;
4278 start = ctxt->cur;
4279 cur = CUR;
4280 if (((cur >= 'a') && (cur <= 'z')) ||
4281 ((cur >= 'A') && (cur <= 'Z')) ||
4282 ((cur >= '0') && (cur <= '9')) ||
4283 (cur == 0x2D)) {
4284 NEXT;
4285 cur = CUR;
4286 while (((cur >= 'a') && (cur <= 'z')) ||
4287 ((cur >= 'A') && (cur <= 'Z')) ||
4288 ((cur >= '0') && (cur <= '9')) ||
4289 (cur == 0x2D)) {
4290 NEXT;
4291 cur = CUR;
4292 }
4293 }
4294 type = XML_REGEXP_BLOCK_NAME;
4295 blockName = xmlStrndup(start, ctxt->cur - start);
4296 } else {
4297 ERROR("Unknown char property");
4298 return;
4299 }
4300 if (ctxt->atom == NULL) {
4301 ctxt->atom = xmlRegNewAtom(ctxt, type);
4302 if (ctxt->atom != NULL)
4303 ctxt->atom->valuep = blockName;
4304 } else if (ctxt->atom->type == XML_REGEXP_RANGES) {
4305 xmlRegAtomAddRange(ctxt, ctxt->atom, ctxt->neg,
4306 type, 0, 0, blockName);
4307 }
4308}
4309
4310/**
4311 * xmlFAParseCharClassEsc:
Daniel Veillard441bc322002-04-20 17:38:48 +00004312 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00004313 *
4314 * [23] charClassEsc ::= ( SingleCharEsc | MultiCharEsc | catEsc | complEsc )
4315 * [24] SingleCharEsc ::= '\' [nrt\|.?*+(){}#x2D#x5B#x5D#x5E]
4316 * [25] catEsc ::= '\p{' charProp '}'
4317 * [26] complEsc ::= '\P{' charProp '}'
4318 * [37] MultiCharEsc ::= '.' | ('\' [sSiIcCdDwW])
4319 */
4320static void
4321xmlFAParseCharClassEsc(xmlRegParserCtxtPtr ctxt) {
4322 int cur;
4323
4324 if (CUR == '.') {
4325 if (ctxt->atom == NULL) {
4326 ctxt->atom = xmlRegNewAtom(ctxt, XML_REGEXP_ANYCHAR);
4327 } else if (ctxt->atom->type == XML_REGEXP_RANGES) {
4328 xmlRegAtomAddRange(ctxt, ctxt->atom, ctxt->neg,
4329 XML_REGEXP_ANYCHAR, 0, 0, NULL);
4330 }
4331 NEXT;
4332 return;
4333 }
4334 if (CUR != '\\') {
4335 ERROR("Escaped sequence: expecting \\");
4336 return;
4337 }
4338 NEXT;
4339 cur = CUR;
4340 if (cur == 'p') {
4341 NEXT;
4342 if (CUR != '{') {
4343 ERROR("Expecting '{'");
4344 return;
4345 }
4346 NEXT;
4347 xmlFAParseCharProp(ctxt);
4348 if (CUR != '}') {
4349 ERROR("Expecting '}'");
4350 return;
4351 }
4352 NEXT;
4353 } else if (cur == 'P') {
4354 NEXT;
4355 if (CUR != '{') {
4356 ERROR("Expecting '{'");
4357 return;
4358 }
4359 NEXT;
4360 xmlFAParseCharProp(ctxt);
4361 ctxt->atom->neg = 1;
4362 if (CUR != '}') {
4363 ERROR("Expecting '}'");
4364 return;
4365 }
4366 NEXT;
4367 } else if ((cur == 'n') || (cur == 'r') || (cur == 't') || (cur == '\\') ||
4368 (cur == '|') || (cur == '.') || (cur == '?') || (cur == '*') ||
4369 (cur == '+') || (cur == '(') || (cur == ')') || (cur == '{') ||
4370 (cur == '}') || (cur == 0x2D) || (cur == 0x5B) || (cur == 0x5D) ||
4371 (cur == 0x5E)) {
4372 if (ctxt->atom == NULL) {
4373 ctxt->atom = xmlRegNewAtom(ctxt, XML_REGEXP_CHARVAL);
Daniel Veillard99c394d2005-07-14 12:58:49 +00004374 if (ctxt->atom != NULL) {
4375 switch (cur) {
4376 case 'n':
4377 ctxt->atom->codepoint = '\n';
4378 break;
4379 case 'r':
4380 ctxt->atom->codepoint = '\r';
4381 break;
4382 case 't':
4383 ctxt->atom->codepoint = '\t';
4384 break;
4385 default:
4386 ctxt->atom->codepoint = cur;
4387 }
4388 }
Daniel Veillard4255d502002-04-16 15:50:10 +00004389 } else if (ctxt->atom->type == XML_REGEXP_RANGES) {
4390 xmlRegAtomAddRange(ctxt, ctxt->atom, ctxt->neg,
4391 XML_REGEXP_CHARVAL, cur, cur, NULL);
4392 }
4393 NEXT;
4394 } else if ((cur == 's') || (cur == 'S') || (cur == 'i') || (cur == 'I') ||
4395 (cur == 'c') || (cur == 'C') || (cur == 'd') || (cur == 'D') ||
4396 (cur == 'w') || (cur == 'W')) {
Daniel Veillardb509f152002-04-17 16:28:10 +00004397 xmlRegAtomType type = XML_REGEXP_ANYSPACE;
Daniel Veillard4255d502002-04-16 15:50:10 +00004398
4399 switch (cur) {
4400 case 's':
4401 type = XML_REGEXP_ANYSPACE;
4402 break;
4403 case 'S':
4404 type = XML_REGEXP_NOTSPACE;
4405 break;
4406 case 'i':
4407 type = XML_REGEXP_INITNAME;
4408 break;
4409 case 'I':
4410 type = XML_REGEXP_NOTINITNAME;
4411 break;
4412 case 'c':
4413 type = XML_REGEXP_NAMECHAR;
4414 break;
4415 case 'C':
4416 type = XML_REGEXP_NOTNAMECHAR;
4417 break;
4418 case 'd':
4419 type = XML_REGEXP_DECIMAL;
4420 break;
4421 case 'D':
4422 type = XML_REGEXP_NOTDECIMAL;
4423 break;
4424 case 'w':
4425 type = XML_REGEXP_REALCHAR;
4426 break;
4427 case 'W':
4428 type = XML_REGEXP_NOTREALCHAR;
4429 break;
4430 }
4431 NEXT;
4432 if (ctxt->atom == NULL) {
4433 ctxt->atom = xmlRegNewAtom(ctxt, type);
4434 } else if (ctxt->atom->type == XML_REGEXP_RANGES) {
4435 xmlRegAtomAddRange(ctxt, ctxt->atom, ctxt->neg,
4436 type, 0, 0, NULL);
4437 }
4438 }
4439}
4440
4441/**
4442 * xmlFAParseCharRef:
Daniel Veillard441bc322002-04-20 17:38:48 +00004443 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00004444 *
4445 * [19] XmlCharRef ::= ( '&#' [0-9]+ ';' ) | (' &#x' [0-9a-fA-F]+ ';' )
4446 */
4447static int
4448xmlFAParseCharRef(xmlRegParserCtxtPtr ctxt) {
4449 int ret = 0, cur;
4450
4451 if ((CUR != '&') || (NXT(1) != '#'))
4452 return(-1);
4453 NEXT;
4454 NEXT;
4455 cur = CUR;
4456 if (cur == 'x') {
4457 NEXT;
4458 cur = CUR;
4459 if (((cur >= '0') && (cur <= '9')) ||
4460 ((cur >= 'a') && (cur <= 'f')) ||
4461 ((cur >= 'A') && (cur <= 'F'))) {
4462 while (((cur >= '0') && (cur <= '9')) ||
4463 ((cur >= 'A') && (cur <= 'F'))) {
4464 if ((cur >= '0') && (cur <= '9'))
4465 ret = ret * 16 + cur - '0';
4466 else if ((cur >= 'a') && (cur <= 'f'))
4467 ret = ret * 16 + 10 + (cur - 'a');
4468 else
4469 ret = ret * 16 + 10 + (cur - 'A');
4470 NEXT;
4471 cur = CUR;
4472 }
4473 } else {
4474 ERROR("Char ref: expecting [0-9A-F]");
4475 return(-1);
4476 }
4477 } else {
4478 if ((cur >= '0') && (cur <= '9')) {
4479 while ((cur >= '0') && (cur <= '9')) {
4480 ret = ret * 10 + cur - '0';
4481 NEXT;
4482 cur = CUR;
4483 }
4484 } else {
4485 ERROR("Char ref: expecting [0-9]");
4486 return(-1);
4487 }
4488 }
4489 if (cur != ';') {
4490 ERROR("Char ref: expecting ';'");
4491 return(-1);
4492 } else {
4493 NEXT;
4494 }
4495 return(ret);
4496}
4497
4498/**
4499 * xmlFAParseCharRange:
Daniel Veillard441bc322002-04-20 17:38:48 +00004500 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00004501 *
4502 * [17] charRange ::= seRange | XmlCharRef | XmlCharIncDash
4503 * [18] seRange ::= charOrEsc '-' charOrEsc
4504 * [20] charOrEsc ::= XmlChar | SingleCharEsc
4505 * [21] XmlChar ::= [^\#x2D#x5B#x5D]
4506 * [22] XmlCharIncDash ::= [^\#x5B#x5D]
4507 */
4508static void
4509xmlFAParseCharRange(xmlRegParserCtxtPtr ctxt) {
William M. Brackdc99df92003-12-27 01:54:25 +00004510 int cur, len;
Daniel Veillard4255d502002-04-16 15:50:10 +00004511 int start = -1;
4512 int end = -1;
4513
4514 if ((CUR == '&') && (NXT(1) == '#')) {
4515 end = start = xmlFAParseCharRef(ctxt);
4516 xmlRegAtomAddRange(ctxt, ctxt->atom, ctxt->neg,
4517 XML_REGEXP_CHARVAL, start, end, NULL);
4518 return;
4519 }
4520 cur = CUR;
4521 if (cur == '\\') {
4522 NEXT;
4523 cur = CUR;
4524 switch (cur) {
4525 case 'n': start = 0xA; break;
4526 case 'r': start = 0xD; break;
4527 case 't': start = 0x9; break;
4528 case '\\': case '|': case '.': case '-': case '^': case '?':
4529 case '*': case '+': case '{': case '}': case '(': case ')':
4530 case '[': case ']':
4531 start = cur; break;
4532 default:
4533 ERROR("Invalid escape value");
4534 return;
4535 }
4536 end = start;
William M. Brackdc99df92003-12-27 01:54:25 +00004537 len = 1;
Daniel Veillard4255d502002-04-16 15:50:10 +00004538 } else if ((cur != 0x5B) && (cur != 0x5D)) {
William M. Brackdc99df92003-12-27 01:54:25 +00004539 end = start = CUR_SCHAR(ctxt->cur, len);
Daniel Veillard4255d502002-04-16 15:50:10 +00004540 } else {
4541 ERROR("Expecting a char range");
4542 return;
4543 }
William M. Brackdc99df92003-12-27 01:54:25 +00004544 NEXTL(len);
Daniel Veillard4255d502002-04-16 15:50:10 +00004545 if (start == '-') {
4546 return;
4547 }
4548 cur = CUR;
William M. Brack10f1ef42004-03-20 14:51:25 +00004549 if ((cur != '-') || (NXT(1) == ']')) {
Daniel Veillard4255d502002-04-16 15:50:10 +00004550 xmlRegAtomAddRange(ctxt, ctxt->atom, ctxt->neg,
4551 XML_REGEXP_CHARVAL, start, end, NULL);
4552 return;
4553 }
4554 NEXT;
4555 cur = CUR;
4556 if (cur == '\\') {
4557 NEXT;
4558 cur = CUR;
4559 switch (cur) {
4560 case 'n': end = 0xA; break;
4561 case 'r': end = 0xD; break;
4562 case 't': end = 0x9; break;
4563 case '\\': case '|': case '.': case '-': case '^': case '?':
4564 case '*': case '+': case '{': case '}': case '(': case ')':
4565 case '[': case ']':
4566 end = cur; break;
4567 default:
4568 ERROR("Invalid escape value");
4569 return;
4570 }
William M. Brackdc99df92003-12-27 01:54:25 +00004571 len = 1;
Daniel Veillard4255d502002-04-16 15:50:10 +00004572 } else if ((cur != 0x5B) && (cur != 0x5D)) {
William M. Brackdc99df92003-12-27 01:54:25 +00004573 end = CUR_SCHAR(ctxt->cur, len);
Daniel Veillard4255d502002-04-16 15:50:10 +00004574 } else {
4575 ERROR("Expecting the end of a char range");
4576 return;
4577 }
William M. Brackdc99df92003-12-27 01:54:25 +00004578 NEXTL(len);
Daniel Veillard4255d502002-04-16 15:50:10 +00004579 /* TODO check that the values are acceptable character ranges for XML */
4580 if (end < start) {
4581 ERROR("End of range is before start of range");
4582 } else {
4583 xmlRegAtomAddRange(ctxt, ctxt->atom, ctxt->neg,
4584 XML_REGEXP_CHARVAL, start, end, NULL);
4585 }
4586 return;
4587}
4588
4589/**
4590 * xmlFAParsePosCharGroup:
Daniel Veillard441bc322002-04-20 17:38:48 +00004591 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00004592 *
4593 * [14] posCharGroup ::= ( charRange | charClassEsc )+
4594 */
4595static void
4596xmlFAParsePosCharGroup(xmlRegParserCtxtPtr ctxt) {
4597 do {
4598 if ((CUR == '\\') || (CUR == '.')) {
4599 xmlFAParseCharClassEsc(ctxt);
4600 } else {
4601 xmlFAParseCharRange(ctxt);
4602 }
4603 } while ((CUR != ']') && (CUR != '^') && (CUR != '-') &&
4604 (ctxt->error == 0));
4605}
4606
4607/**
4608 * xmlFAParseCharGroup:
Daniel Veillard441bc322002-04-20 17:38:48 +00004609 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00004610 *
4611 * [13] charGroup ::= posCharGroup | negCharGroup | charClassSub
4612 * [15] negCharGroup ::= '^' posCharGroup
4613 * [16] charClassSub ::= ( posCharGroup | negCharGroup ) '-' charClassExpr
4614 * [12] charClassExpr ::= '[' charGroup ']'
4615 */
4616static void
4617xmlFAParseCharGroup(xmlRegParserCtxtPtr ctxt) {
4618 int n = ctxt->neg;
4619 while ((CUR != ']') && (ctxt->error == 0)) {
4620 if (CUR == '^') {
4621 int neg = ctxt->neg;
4622
4623 NEXT;
4624 ctxt->neg = !ctxt->neg;
4625 xmlFAParsePosCharGroup(ctxt);
4626 ctxt->neg = neg;
William M. Brack10f1ef42004-03-20 14:51:25 +00004627 } else if ((CUR == '-') && (NXT(1) == '[')) {
Daniel Veillardf8b9de32003-11-24 14:27:26 +00004628 int neg = ctxt->neg;
Daniel Veillardf8b9de32003-11-24 14:27:26 +00004629 ctxt->neg = 2;
William M. Brack10f1ef42004-03-20 14:51:25 +00004630 NEXT; /* eat the '-' */
4631 NEXT; /* eat the '[' */
Daniel Veillard4255d502002-04-16 15:50:10 +00004632 xmlFAParseCharGroup(ctxt);
4633 if (CUR == ']') {
4634 NEXT;
4635 } else {
4636 ERROR("charClassExpr: ']' expected");
4637 break;
4638 }
Daniel Veillardf8b9de32003-11-24 14:27:26 +00004639 ctxt->neg = neg;
Daniel Veillard4255d502002-04-16 15:50:10 +00004640 break;
4641 } else if (CUR != ']') {
4642 xmlFAParsePosCharGroup(ctxt);
4643 }
4644 }
4645 ctxt->neg = n;
4646}
4647
4648/**
4649 * xmlFAParseCharClass:
Daniel Veillard441bc322002-04-20 17:38:48 +00004650 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00004651 *
4652 * [11] charClass ::= charClassEsc | charClassExpr
4653 * [12] charClassExpr ::= '[' charGroup ']'
4654 */
4655static void
4656xmlFAParseCharClass(xmlRegParserCtxtPtr ctxt) {
4657 if (CUR == '[') {
4658 NEXT;
4659 ctxt->atom = xmlRegNewAtom(ctxt, XML_REGEXP_RANGES);
4660 if (ctxt->atom == NULL)
4661 return;
4662 xmlFAParseCharGroup(ctxt);
4663 if (CUR == ']') {
4664 NEXT;
4665 } else {
4666 ERROR("xmlFAParseCharClass: ']' expected");
4667 }
4668 } else {
4669 xmlFAParseCharClassEsc(ctxt);
4670 }
4671}
4672
4673/**
4674 * xmlFAParseQuantExact:
Daniel Veillard441bc322002-04-20 17:38:48 +00004675 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00004676 *
4677 * [8] QuantExact ::= [0-9]+
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00004678 *
4679 * Returns 0 if success or -1 in case of error
Daniel Veillard4255d502002-04-16 15:50:10 +00004680 */
4681static int
4682xmlFAParseQuantExact(xmlRegParserCtxtPtr ctxt) {
4683 int ret = 0;
4684 int ok = 0;
4685
4686 while ((CUR >= '0') && (CUR <= '9')) {
4687 ret = ret * 10 + (CUR - '0');
4688 ok = 1;
4689 NEXT;
4690 }
4691 if (ok != 1) {
4692 return(-1);
4693 }
4694 return(ret);
4695}
4696
4697/**
4698 * xmlFAParseQuantifier:
Daniel Veillard441bc322002-04-20 17:38:48 +00004699 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00004700 *
4701 * [4] quantifier ::= [?*+] | ( '{' quantity '}' )
4702 * [5] quantity ::= quantRange | quantMin | QuantExact
4703 * [6] quantRange ::= QuantExact ',' QuantExact
4704 * [7] quantMin ::= QuantExact ','
4705 * [8] QuantExact ::= [0-9]+
4706 */
4707static int
4708xmlFAParseQuantifier(xmlRegParserCtxtPtr ctxt) {
4709 int cur;
4710
4711 cur = CUR;
4712 if ((cur == '?') || (cur == '*') || (cur == '+')) {
4713 if (ctxt->atom != NULL) {
4714 if (cur == '?')
4715 ctxt->atom->quant = XML_REGEXP_QUANT_OPT;
4716 else if (cur == '*')
4717 ctxt->atom->quant = XML_REGEXP_QUANT_MULT;
4718 else if (cur == '+')
4719 ctxt->atom->quant = XML_REGEXP_QUANT_PLUS;
4720 }
4721 NEXT;
4722 return(1);
4723 }
4724 if (cur == '{') {
4725 int min = 0, max = 0;
4726
4727 NEXT;
4728 cur = xmlFAParseQuantExact(ctxt);
4729 if (cur >= 0)
4730 min = cur;
4731 if (CUR == ',') {
4732 NEXT;
Daniel Veillardebe48c62003-12-03 12:12:27 +00004733 if (CUR == '}')
4734 max = INT_MAX;
4735 else {
4736 cur = xmlFAParseQuantExact(ctxt);
4737 if (cur >= 0)
4738 max = cur;
4739 else {
4740 ERROR("Improper quantifier");
4741 }
4742 }
Daniel Veillard4255d502002-04-16 15:50:10 +00004743 }
4744 if (CUR == '}') {
4745 NEXT;
4746 } else {
4747 ERROR("Unterminated quantifier");
4748 }
4749 if (max == 0)
4750 max = min;
4751 if (ctxt->atom != NULL) {
4752 ctxt->atom->quant = XML_REGEXP_QUANT_RANGE;
4753 ctxt->atom->min = min;
4754 ctxt->atom->max = max;
4755 }
4756 return(1);
4757 }
4758 return(0);
4759}
4760
4761/**
4762 * xmlFAParseAtom:
Daniel Veillard441bc322002-04-20 17:38:48 +00004763 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00004764 *
4765 * [9] atom ::= Char | charClass | ( '(' regExp ')' )
4766 */
4767static int
4768xmlFAParseAtom(xmlRegParserCtxtPtr ctxt) {
4769 int codepoint, len;
4770
4771 codepoint = xmlFAIsChar(ctxt);
4772 if (codepoint > 0) {
4773 ctxt->atom = xmlRegNewAtom(ctxt, XML_REGEXP_CHARVAL);
4774 if (ctxt->atom == NULL)
4775 return(-1);
4776 codepoint = CUR_SCHAR(ctxt->cur, len);
4777 ctxt->atom->codepoint = codepoint;
4778 NEXTL(len);
4779 return(1);
4780 } else if (CUR == '|') {
4781 return(0);
4782 } else if (CUR == 0) {
4783 return(0);
4784 } else if (CUR == ')') {
4785 return(0);
4786 } else if (CUR == '(') {
4787 xmlRegStatePtr start, oldend;
4788
4789 NEXT;
4790 xmlFAGenerateEpsilonTransition(ctxt, ctxt->state, NULL);
4791 start = ctxt->state;
4792 oldend = ctxt->end;
4793 ctxt->end = NULL;
4794 ctxt->atom = NULL;
4795 xmlFAParseRegExp(ctxt, 0);
4796 if (CUR == ')') {
4797 NEXT;
4798 } else {
4799 ERROR("xmlFAParseAtom: expecting ')'");
4800 }
4801 ctxt->atom = xmlRegNewAtom(ctxt, XML_REGEXP_SUBREG);
4802 if (ctxt->atom == NULL)
4803 return(-1);
4804 ctxt->atom->start = start;
4805 ctxt->atom->stop = ctxt->state;
4806 ctxt->end = oldend;
4807 return(1);
4808 } else if ((CUR == '[') || (CUR == '\\') || (CUR == '.')) {
4809 xmlFAParseCharClass(ctxt);
4810 return(1);
4811 }
4812 return(0);
4813}
4814
4815/**
4816 * xmlFAParsePiece:
Daniel Veillard441bc322002-04-20 17:38:48 +00004817 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00004818 *
4819 * [3] piece ::= atom quantifier?
4820 */
4821static int
4822xmlFAParsePiece(xmlRegParserCtxtPtr ctxt) {
4823 int ret;
4824
4825 ctxt->atom = NULL;
4826 ret = xmlFAParseAtom(ctxt);
4827 if (ret == 0)
4828 return(0);
4829 if (ctxt->atom == NULL) {
4830 ERROR("internal: no atom generated");
4831 }
4832 xmlFAParseQuantifier(ctxt);
4833 return(1);
4834}
4835
4836/**
4837 * xmlFAParseBranch:
Daniel Veillard441bc322002-04-20 17:38:48 +00004838 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00004839 *
4840 * [2] branch ::= piece*
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00004841 8
Daniel Veillard4255d502002-04-16 15:50:10 +00004842 */
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00004843static int
Daniel Veillard2cbf5962004-03-31 15:50:43 +00004844xmlFAParseBranch(xmlRegParserCtxtPtr ctxt) {
Daniel Veillard4255d502002-04-16 15:50:10 +00004845 xmlRegStatePtr previous;
Daniel Veillard4255d502002-04-16 15:50:10 +00004846 int ret;
4847
4848 previous = ctxt->state;
4849 ret = xmlFAParsePiece(ctxt);
4850 if (ret != 0) {
Daniel Veillard2cbf5962004-03-31 15:50:43 +00004851 if (xmlFAGenerateTransitions(ctxt, previous, NULL, ctxt->atom) < 0)
4852 return(-1);
4853 previous = ctxt->state;
Daniel Veillard4255d502002-04-16 15:50:10 +00004854 ctxt->atom = NULL;
4855 }
4856 while ((ret != 0) && (ctxt->error == 0)) {
4857 ret = xmlFAParsePiece(ctxt);
4858 if (ret != 0) {
Daniel Veillard2cbf5962004-03-31 15:50:43 +00004859 if (xmlFAGenerateTransitions(ctxt, previous, NULL,
4860 ctxt->atom) < 0)
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00004861 return(-1);
Daniel Veillard4255d502002-04-16 15:50:10 +00004862 previous = ctxt->state;
4863 ctxt->atom = NULL;
4864 }
4865 }
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00004866 return(0);
Daniel Veillard4255d502002-04-16 15:50:10 +00004867}
4868
4869/**
4870 * xmlFAParseRegExp:
Daniel Veillard441bc322002-04-20 17:38:48 +00004871 * @ctxt: a regexp parser context
William M. Brackddf71d62004-05-06 04:17:26 +00004872 * @top: is this the top-level expression ?
Daniel Veillard4255d502002-04-16 15:50:10 +00004873 *
4874 * [1] regExp ::= branch ( '|' branch )*
4875 */
4876static void
4877xmlFAParseRegExp(xmlRegParserCtxtPtr ctxt, int top) {
Daniel Veillardc7e3cc42004-09-28 12:33:52 +00004878 xmlRegStatePtr start, end;
Daniel Veillard4255d502002-04-16 15:50:10 +00004879
Daniel Veillard2cbf5962004-03-31 15:50:43 +00004880 /* if not top start should have been generated by an epsilon trans */
Daniel Veillard4255d502002-04-16 15:50:10 +00004881 start = ctxt->state;
Daniel Veillard2cbf5962004-03-31 15:50:43 +00004882 ctxt->end = NULL;
4883 xmlFAParseBranch(ctxt);
4884 if (top) {
4885#ifdef DEBUG_REGEXP_GRAPH
4886 printf("State %d is final\n", ctxt->state->no);
4887#endif
4888 ctxt->state->type = XML_REGEXP_FINAL_STATE;
4889 }
Daniel Veillard4255d502002-04-16 15:50:10 +00004890 if (CUR != '|') {
4891 ctxt->end = ctxt->state;
4892 return;
4893 }
4894 end = ctxt->state;
4895 while ((CUR == '|') && (ctxt->error == 0)) {
4896 NEXT;
4897 ctxt->state = start;
Daniel Veillard2cbf5962004-03-31 15:50:43 +00004898 ctxt->end = NULL;
4899 xmlFAParseBranch(ctxt);
4900 if (top) {
4901 ctxt->state->type = XML_REGEXP_FINAL_STATE;
4902#ifdef DEBUG_REGEXP_GRAPH
4903 printf("State %d is final\n", ctxt->state->no);
4904#endif
4905 } else {
4906 xmlFAGenerateEpsilonTransition(ctxt, ctxt->state, end);
4907 }
Daniel Veillard4255d502002-04-16 15:50:10 +00004908 }
Daniel Veillard2cbf5962004-03-31 15:50:43 +00004909 if (!top) {
4910 ctxt->state = end;
4911 ctxt->end = end;
4912 }
Daniel Veillard4255d502002-04-16 15:50:10 +00004913}
4914
4915/************************************************************************
4916 * *
4917 * The basic API *
4918 * *
4919 ************************************************************************/
4920
4921/**
4922 * xmlRegexpPrint:
4923 * @output: the file for the output debug
4924 * @regexp: the compiled regexp
4925 *
4926 * Print the content of the compiled regular expression
4927 */
4928void
4929xmlRegexpPrint(FILE *output, xmlRegexpPtr regexp) {
4930 int i;
4931
Daniel Veillarda82b1822004-11-08 16:24:57 +00004932 if (output == NULL)
4933 return;
Daniel Veillard4255d502002-04-16 15:50:10 +00004934 fprintf(output, " regexp: ");
4935 if (regexp == NULL) {
4936 fprintf(output, "NULL\n");
4937 return;
4938 }
4939 fprintf(output, "'%s' ", regexp->string);
4940 fprintf(output, "\n");
4941 fprintf(output, "%d atoms:\n", regexp->nbAtoms);
4942 for (i = 0;i < regexp->nbAtoms; i++) {
4943 fprintf(output, " %02d ", i);
4944 xmlRegPrintAtom(output, regexp->atoms[i]);
4945 }
4946 fprintf(output, "%d states:", regexp->nbStates);
4947 fprintf(output, "\n");
4948 for (i = 0;i < regexp->nbStates; i++) {
4949 xmlRegPrintState(output, regexp->states[i]);
4950 }
4951 fprintf(output, "%d counters:\n", regexp->nbCounters);
4952 for (i = 0;i < regexp->nbCounters; i++) {
4953 fprintf(output, " %d: min %d max %d\n", i, regexp->counters[i].min,
4954 regexp->counters[i].max);
4955 }
4956}
4957
4958/**
4959 * xmlRegexpCompile:
4960 * @regexp: a regular expression string
4961 *
4962 * Parses a regular expression conforming to XML Schemas Part 2 Datatype
William M. Brackddf71d62004-05-06 04:17:26 +00004963 * Appendix F and builds an automata suitable for testing strings against
Daniel Veillard4255d502002-04-16 15:50:10 +00004964 * that regular expression
4965 *
4966 * Returns the compiled expression or NULL in case of error
4967 */
4968xmlRegexpPtr
4969xmlRegexpCompile(const xmlChar *regexp) {
4970 xmlRegexpPtr ret;
4971 xmlRegParserCtxtPtr ctxt;
4972
4973 ctxt = xmlRegNewParserCtxt(regexp);
4974 if (ctxt == NULL)
4975 return(NULL);
4976
4977 /* initialize the parser */
4978 ctxt->end = NULL;
4979 ctxt->start = ctxt->state = xmlRegNewState(ctxt);
4980 xmlRegStatePush(ctxt, ctxt->start);
4981
4982 /* parse the expression building an automata */
4983 xmlFAParseRegExp(ctxt, 1);
4984 if (CUR != 0) {
4985 ERROR("xmlFAParseRegExp: extra characters");
4986 }
4987 ctxt->end = ctxt->state;
4988 ctxt->start->type = XML_REGEXP_START_STATE;
4989 ctxt->end->type = XML_REGEXP_FINAL_STATE;
4990
4991 /* remove the Epsilon except for counted transitions */
4992 xmlFAEliminateEpsilonTransitions(ctxt);
4993
4994
4995 if (ctxt->error != 0) {
4996 xmlRegFreeParserCtxt(ctxt);
4997 return(NULL);
4998 }
4999 ret = xmlRegEpxFromParse(ctxt);
5000 xmlRegFreeParserCtxt(ctxt);
5001 return(ret);
5002}
5003
5004/**
5005 * xmlRegexpExec:
5006 * @comp: the compiled regular expression
5007 * @content: the value to check against the regular expression
5008 *
William M. Brackddf71d62004-05-06 04:17:26 +00005009 * Check if the regular expression generates the value
Daniel Veillard4255d502002-04-16 15:50:10 +00005010 *
William M. Brackddf71d62004-05-06 04:17:26 +00005011 * Returns 1 if it matches, 0 if not and a negative value in case of error
Daniel Veillard4255d502002-04-16 15:50:10 +00005012 */
5013int
5014xmlRegexpExec(xmlRegexpPtr comp, const xmlChar *content) {
5015 if ((comp == NULL) || (content == NULL))
5016 return(-1);
5017 return(xmlFARegExec(comp, content));
5018}
5019
5020/**
Daniel Veillard23e73572002-09-19 19:56:43 +00005021 * xmlRegexpIsDeterminist:
5022 * @comp: the compiled regular expression
5023 *
5024 * Check if the regular expression is determinist
5025 *
William M. Brackddf71d62004-05-06 04:17:26 +00005026 * Returns 1 if it yes, 0 if not and a negative value in case of error
Daniel Veillard23e73572002-09-19 19:56:43 +00005027 */
5028int
5029xmlRegexpIsDeterminist(xmlRegexpPtr comp) {
5030 xmlAutomataPtr am;
5031 int ret;
5032
5033 if (comp == NULL)
5034 return(-1);
5035 if (comp->determinist != -1)
5036 return(comp->determinist);
5037
5038 am = xmlNewAutomata();
Daniel Veillardbd9afb52002-09-25 22:25:35 +00005039 if (am->states != NULL) {
5040 int i;
5041
5042 for (i = 0;i < am->nbStates;i++)
5043 xmlRegFreeState(am->states[i]);
5044 xmlFree(am->states);
5045 }
Daniel Veillard23e73572002-09-19 19:56:43 +00005046 am->nbAtoms = comp->nbAtoms;
5047 am->atoms = comp->atoms;
5048 am->nbStates = comp->nbStates;
5049 am->states = comp->states;
5050 am->determinist = -1;
5051 ret = xmlFAComputesDeterminism(am);
5052 am->atoms = NULL;
5053 am->states = NULL;
5054 xmlFreeAutomata(am);
5055 return(ret);
5056}
5057
5058/**
Daniel Veillard4255d502002-04-16 15:50:10 +00005059 * xmlRegFreeRegexp:
5060 * @regexp: the regexp
5061 *
5062 * Free a regexp
5063 */
5064void
5065xmlRegFreeRegexp(xmlRegexpPtr regexp) {
5066 int i;
5067 if (regexp == NULL)
5068 return;
5069
5070 if (regexp->string != NULL)
5071 xmlFree(regexp->string);
5072 if (regexp->states != NULL) {
5073 for (i = 0;i < regexp->nbStates;i++)
5074 xmlRegFreeState(regexp->states[i]);
5075 xmlFree(regexp->states);
5076 }
5077 if (regexp->atoms != NULL) {
5078 for (i = 0;i < regexp->nbAtoms;i++)
5079 xmlRegFreeAtom(regexp->atoms[i]);
5080 xmlFree(regexp->atoms);
5081 }
5082 if (regexp->counters != NULL)
5083 xmlFree(regexp->counters);
Daniel Veillard23e73572002-09-19 19:56:43 +00005084 if (regexp->compact != NULL)
5085 xmlFree(regexp->compact);
Daniel Veillard118aed72002-09-24 14:13:13 +00005086 if (regexp->transdata != NULL)
5087 xmlFree(regexp->transdata);
Daniel Veillard23e73572002-09-19 19:56:43 +00005088 if (regexp->stringMap != NULL) {
5089 for (i = 0; i < regexp->nbstrings;i++)
5090 xmlFree(regexp->stringMap[i]);
5091 xmlFree(regexp->stringMap);
5092 }
5093
Daniel Veillard4255d502002-04-16 15:50:10 +00005094 xmlFree(regexp);
5095}
5096
5097#ifdef LIBXML_AUTOMATA_ENABLED
5098/************************************************************************
5099 * *
5100 * The Automata interface *
5101 * *
5102 ************************************************************************/
5103
5104/**
5105 * xmlNewAutomata:
5106 *
5107 * Create a new automata
5108 *
5109 * Returns the new object or NULL in case of failure
5110 */
5111xmlAutomataPtr
5112xmlNewAutomata(void) {
5113 xmlAutomataPtr ctxt;
5114
5115 ctxt = xmlRegNewParserCtxt(NULL);
5116 if (ctxt == NULL)
5117 return(NULL);
5118
5119 /* initialize the parser */
5120 ctxt->end = NULL;
5121 ctxt->start = ctxt->state = xmlRegNewState(ctxt);
Daniel Veillarddb68b742005-07-30 13:18:24 +00005122 ctxt->start->type = XML_REGEXP_START_STATE;
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00005123 if (ctxt->start == NULL) {
5124 xmlFreeAutomata(ctxt);
5125 return(NULL);
5126 }
5127 if (xmlRegStatePush(ctxt, ctxt->start) < 0) {
5128 xmlRegFreeState(ctxt->start);
5129 xmlFreeAutomata(ctxt);
5130 return(NULL);
5131 }
Daniel Veillard4255d502002-04-16 15:50:10 +00005132
5133 return(ctxt);
5134}
5135
5136/**
5137 * xmlFreeAutomata:
5138 * @am: an automata
5139 *
5140 * Free an automata
5141 */
5142void
5143xmlFreeAutomata(xmlAutomataPtr am) {
5144 if (am == NULL)
5145 return;
5146 xmlRegFreeParserCtxt(am);
5147}
5148
5149/**
5150 * xmlAutomataGetInitState:
5151 * @am: an automata
5152 *
Daniel Veillarda9b66d02002-12-11 14:23:49 +00005153 * Initial state lookup
5154 *
Daniel Veillard4255d502002-04-16 15:50:10 +00005155 * Returns the initial state of the automata
5156 */
5157xmlAutomataStatePtr
5158xmlAutomataGetInitState(xmlAutomataPtr am) {
5159 if (am == NULL)
5160 return(NULL);
5161 return(am->start);
5162}
5163
5164/**
5165 * xmlAutomataSetFinalState:
5166 * @am: an automata
5167 * @state: a state in this automata
5168 *
5169 * Makes that state a final state
5170 *
5171 * Returns 0 or -1 in case of error
5172 */
5173int
5174xmlAutomataSetFinalState(xmlAutomataPtr am, xmlAutomataStatePtr state) {
5175 if ((am == NULL) || (state == NULL))
5176 return(-1);
5177 state->type = XML_REGEXP_FINAL_STATE;
5178 return(0);
5179}
5180
5181/**
5182 * xmlAutomataNewTransition:
5183 * @am: an automata
5184 * @from: the starting point of the transition
5185 * @to: the target point of the transition or NULL
5186 * @token: the input string associated to that transition
5187 * @data: data passed to the callback function if the transition is activated
5188 *
William M. Brackddf71d62004-05-06 04:17:26 +00005189 * If @to is NULL, this creates first a new target state in the automata
Daniel Veillard4255d502002-04-16 15:50:10 +00005190 * and then adds a transition from the @from state to the target state
5191 * activated by the value of @token
5192 *
5193 * Returns the target state or NULL in case of error
5194 */
5195xmlAutomataStatePtr
5196xmlAutomataNewTransition(xmlAutomataPtr am, xmlAutomataStatePtr from,
5197 xmlAutomataStatePtr to, const xmlChar *token,
5198 void *data) {
5199 xmlRegAtomPtr atom;
5200
5201 if ((am == NULL) || (from == NULL) || (token == NULL))
5202 return(NULL);
5203 atom = xmlRegNewAtom(am, XML_REGEXP_STRING);
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00005204 if (atom == NULL)
5205 return(NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +00005206 atom->data = data;
5207 if (atom == NULL)
5208 return(NULL);
5209 atom->valuep = xmlStrdup(token);
5210
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00005211 if (xmlFAGenerateTransitions(am, from, to, atom) < 0) {
5212 xmlRegFreeAtom(atom);
5213 return(NULL);
5214 }
Daniel Veillard4255d502002-04-16 15:50:10 +00005215 if (to == NULL)
5216 return(am->state);
5217 return(to);
5218}
5219
5220/**
Daniel Veillard52b48c72003-04-13 19:53:42 +00005221 * xmlAutomataNewTransition2:
5222 * @am: an automata
5223 * @from: the starting point of the transition
5224 * @to: the target point of the transition or NULL
5225 * @token: the first input string associated to that transition
5226 * @token2: the second input string associated to that transition
5227 * @data: data passed to the callback function if the transition is activated
5228 *
William M. Brackddf71d62004-05-06 04:17:26 +00005229 * If @to is NULL, this creates first a new target state in the automata
Daniel Veillard52b48c72003-04-13 19:53:42 +00005230 * and then adds a transition from the @from state to the target state
5231 * activated by the value of @token
5232 *
5233 * Returns the target state or NULL in case of error
5234 */
5235xmlAutomataStatePtr
5236xmlAutomataNewTransition2(xmlAutomataPtr am, xmlAutomataStatePtr from,
5237 xmlAutomataStatePtr to, const xmlChar *token,
5238 const xmlChar *token2, void *data) {
5239 xmlRegAtomPtr atom;
5240
5241 if ((am == NULL) || (from == NULL) || (token == NULL))
5242 return(NULL);
5243 atom = xmlRegNewAtom(am, XML_REGEXP_STRING);
5244 atom->data = data;
5245 if (atom == NULL)
5246 return(NULL);
5247 if ((token2 == NULL) || (*token2 == 0)) {
5248 atom->valuep = xmlStrdup(token);
5249 } else {
5250 int lenn, lenp;
5251 xmlChar *str;
5252
5253 lenn = strlen((char *) token2);
5254 lenp = strlen((char *) token);
5255
Daniel Veillard3c908dc2003-04-19 00:07:51 +00005256 str = (xmlChar *) xmlMallocAtomic(lenn + lenp + 2);
Daniel Veillard52b48c72003-04-13 19:53:42 +00005257 if (str == NULL) {
5258 xmlRegFreeAtom(atom);
5259 return(NULL);
5260 }
5261 memcpy(&str[0], token, lenp);
5262 str[lenp] = '|';
5263 memcpy(&str[lenp + 1], token2, lenn);
5264 str[lenn + lenp + 1] = 0;
5265
5266 atom->valuep = str;
5267 }
5268
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00005269 if (xmlFAGenerateTransitions(am, from, to, atom) < 0) {
5270 xmlRegFreeAtom(atom);
5271 return(NULL);
5272 }
Daniel Veillard52b48c72003-04-13 19:53:42 +00005273 if (to == NULL)
5274 return(am->state);
5275 return(to);
5276}
5277
5278/**
Daniel Veillard9efc4762005-07-19 14:33:55 +00005279 * xmlAutomataNewNegTrans:
5280 * @am: an automata
5281 * @from: the starting point of the transition
5282 * @to: the target point of the transition or NULL
5283 * @token: the first input string associated to that transition
5284 * @token2: the second input string associated to that transition
5285 * @data: data passed to the callback function if the transition is activated
5286 *
5287 * If @to is NULL, this creates first a new target state in the automata
5288 * and then adds a transition from the @from state to the target state
5289 * activated by any value except (@token,@token2)
Daniel Veillard6e65e152005-08-09 11:09:52 +00005290 * Note that if @token2 is not NULL, then (X, NULL) won't match to follow
5291 # the semantic of XSD ##other
Daniel Veillard9efc4762005-07-19 14:33:55 +00005292 *
5293 * Returns the target state or NULL in case of error
5294 */
5295xmlAutomataStatePtr
5296xmlAutomataNewNegTrans(xmlAutomataPtr am, xmlAutomataStatePtr from,
5297 xmlAutomataStatePtr to, const xmlChar *token,
5298 const xmlChar *token2, void *data) {
5299 xmlRegAtomPtr atom;
Daniel Veillard77005e62005-07-19 16:26:18 +00005300 xmlChar err_msg[200];
Daniel Veillard9efc4762005-07-19 14:33:55 +00005301
5302 if ((am == NULL) || (from == NULL) || (token == NULL))
5303 return(NULL);
5304 atom = xmlRegNewAtom(am, XML_REGEXP_STRING);
5305 if (atom == NULL)
5306 return(NULL);
5307 atom->data = data;
5308 atom->neg = 1;
5309 if ((token2 == NULL) || (*token2 == 0)) {
5310 atom->valuep = xmlStrdup(token);
5311 } else {
5312 int lenn, lenp;
5313 xmlChar *str;
5314
5315 lenn = strlen((char *) token2);
5316 lenp = strlen((char *) token);
5317
5318 str = (xmlChar *) xmlMallocAtomic(lenn + lenp + 2);
5319 if (str == NULL) {
5320 xmlRegFreeAtom(atom);
5321 return(NULL);
5322 }
5323 memcpy(&str[0], token, lenp);
5324 str[lenp] = '|';
5325 memcpy(&str[lenp + 1], token2, lenn);
5326 str[lenn + lenp + 1] = 0;
5327
5328 atom->valuep = str;
5329 }
Daniel Veillarddb68b742005-07-30 13:18:24 +00005330 snprintf((char *) err_msg, 199, "not %s", (const char *) atom->valuep);
Daniel Veillard77005e62005-07-19 16:26:18 +00005331 err_msg[199] = 0;
5332 atom->valuep2 = xmlStrdup(err_msg);
Daniel Veillard9efc4762005-07-19 14:33:55 +00005333
5334 if (xmlFAGenerateTransitions(am, from, to, atom) < 0) {
5335 xmlRegFreeAtom(atom);
5336 return(NULL);
5337 }
Daniel Veillard6e65e152005-08-09 11:09:52 +00005338 am->negs++;
Daniel Veillard9efc4762005-07-19 14:33:55 +00005339 if (to == NULL)
5340 return(am->state);
5341 return(to);
5342}
5343
5344/**
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00005345 * xmlAutomataNewCountTrans2:
5346 * @am: an automata
5347 * @from: the starting point of the transition
5348 * @to: the target point of the transition or NULL
5349 * @token: the input string associated to that transition
5350 * @token2: the second input string associated to that transition
5351 * @min: the minimum successive occurences of token
5352 * @max: the maximum successive occurences of token
5353 * @data: data associated to the transition
5354 *
5355 * If @to is NULL, this creates first a new target state in the automata
5356 * and then adds a transition from the @from state to the target state
5357 * activated by a succession of input of value @token and @token2 and
5358 * whose number is between @min and @max
5359 *
5360 * Returns the target state or NULL in case of error
5361 */
5362xmlAutomataStatePtr
5363xmlAutomataNewCountTrans2(xmlAutomataPtr am, xmlAutomataStatePtr from,
5364 xmlAutomataStatePtr to, const xmlChar *token,
5365 const xmlChar *token2,
5366 int min, int max, void *data) {
5367 xmlRegAtomPtr atom;
5368 int counter;
5369
5370 if ((am == NULL) || (from == NULL) || (token == NULL))
5371 return(NULL);
5372 if (min < 0)
5373 return(NULL);
5374 if ((max < min) || (max < 1))
5375 return(NULL);
5376 atom = xmlRegNewAtom(am, XML_REGEXP_STRING);
5377 if (atom == NULL)
5378 return(NULL);
5379 if ((token2 == NULL) || (*token2 == 0)) {
5380 atom->valuep = xmlStrdup(token);
5381 } else {
5382 int lenn, lenp;
5383 xmlChar *str;
5384
5385 lenn = strlen((char *) token2);
5386 lenp = strlen((char *) token);
5387
5388 str = (xmlChar *) xmlMallocAtomic(lenn + lenp + 2);
5389 if (str == NULL) {
5390 xmlRegFreeAtom(atom);
5391 return(NULL);
5392 }
5393 memcpy(&str[0], token, lenp);
5394 str[lenp] = '|';
5395 memcpy(&str[lenp + 1], token2, lenn);
5396 str[lenn + lenp + 1] = 0;
5397
5398 atom->valuep = str;
5399 }
5400 atom->data = data;
5401 if (min == 0)
5402 atom->min = 1;
5403 else
5404 atom->min = min;
5405 atom->max = max;
5406
5407 /*
5408 * associate a counter to the transition.
5409 */
5410 counter = xmlRegGetCounter(am);
5411 am->counters[counter].min = min;
5412 am->counters[counter].max = max;
5413
5414 /* xmlFAGenerateTransitions(am, from, to, atom); */
5415 if (to == NULL) {
5416 to = xmlRegNewState(am);
5417 xmlRegStatePush(am, to);
5418 }
Daniel Veillard5de09382005-09-26 17:18:17 +00005419 xmlRegStateAddTrans(am, from, atom, to, counter, -1);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00005420 xmlRegAtomPush(am, atom);
5421 am->state = to;
5422
5423 if (to == NULL)
5424 to = am->state;
5425 if (to == NULL)
5426 return(NULL);
5427 if (min == 0)
5428 xmlFAGenerateEpsilonTransition(am, from, to);
5429 return(to);
5430}
5431
5432/**
Daniel Veillard4255d502002-04-16 15:50:10 +00005433 * xmlAutomataNewCountTrans:
5434 * @am: an automata
5435 * @from: the starting point of the transition
5436 * @to: the target point of the transition or NULL
5437 * @token: the input string associated to that transition
5438 * @min: the minimum successive occurences of token
Daniel Veillarda9b66d02002-12-11 14:23:49 +00005439 * @max: the maximum successive occurences of token
5440 * @data: data associated to the transition
Daniel Veillard4255d502002-04-16 15:50:10 +00005441 *
William M. Brackddf71d62004-05-06 04:17:26 +00005442 * If @to is NULL, this creates first a new target state in the automata
Daniel Veillard4255d502002-04-16 15:50:10 +00005443 * and then adds a transition from the @from state to the target state
5444 * activated by a succession of input of value @token and whose number
5445 * is between @min and @max
5446 *
5447 * Returns the target state or NULL in case of error
5448 */
5449xmlAutomataStatePtr
5450xmlAutomataNewCountTrans(xmlAutomataPtr am, xmlAutomataStatePtr from,
5451 xmlAutomataStatePtr to, const xmlChar *token,
5452 int min, int max, void *data) {
5453 xmlRegAtomPtr atom;
Daniel Veillard0ddb21c2004-02-12 12:43:49 +00005454 int counter;
Daniel Veillard4255d502002-04-16 15:50:10 +00005455
5456 if ((am == NULL) || (from == NULL) || (token == NULL))
5457 return(NULL);
5458 if (min < 0)
5459 return(NULL);
5460 if ((max < min) || (max < 1))
5461 return(NULL);
5462 atom = xmlRegNewAtom(am, XML_REGEXP_STRING);
5463 if (atom == NULL)
5464 return(NULL);
5465 atom->valuep = xmlStrdup(token);
5466 atom->data = data;
5467 if (min == 0)
5468 atom->min = 1;
5469 else
5470 atom->min = min;
5471 atom->max = max;
5472
Daniel Veillard0ddb21c2004-02-12 12:43:49 +00005473 /*
5474 * associate a counter to the transition.
5475 */
5476 counter = xmlRegGetCounter(am);
5477 am->counters[counter].min = min;
5478 am->counters[counter].max = max;
5479
5480 /* xmlFAGenerateTransitions(am, from, to, atom); */
5481 if (to == NULL) {
5482 to = xmlRegNewState(am);
5483 xmlRegStatePush(am, to);
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00005484 }
Daniel Veillard5de09382005-09-26 17:18:17 +00005485 xmlRegStateAddTrans(am, from, atom, to, counter, -1);
Daniel Veillard0ddb21c2004-02-12 12:43:49 +00005486 xmlRegAtomPush(am, atom);
5487 am->state = to;
5488
Daniel Veillard4255d502002-04-16 15:50:10 +00005489 if (to == NULL)
5490 to = am->state;
5491 if (to == NULL)
5492 return(NULL);
5493 if (min == 0)
5494 xmlFAGenerateEpsilonTransition(am, from, to);
5495 return(to);
5496}
5497
5498/**
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00005499 * xmlAutomataNewOnceTrans2:
5500 * @am: an automata
5501 * @from: the starting point of the transition
5502 * @to: the target point of the transition or NULL
5503 * @token: the input string associated to that transition
5504 * @token2: the second input string associated to that transition
5505 * @min: the minimum successive occurences of token
5506 * @max: the maximum successive occurences of token
5507 * @data: data associated to the transition
5508 *
5509 * If @to is NULL, this creates first a new target state in the automata
5510 * and then adds a transition from the @from state to the target state
5511 * activated by a succession of input of value @token and @token2 and whose
5512 * number is between @min and @max, moreover that transition can only be
5513 * crossed once.
5514 *
5515 * Returns the target state or NULL in case of error
5516 */
5517xmlAutomataStatePtr
5518xmlAutomataNewOnceTrans2(xmlAutomataPtr am, xmlAutomataStatePtr from,
5519 xmlAutomataStatePtr to, const xmlChar *token,
5520 const xmlChar *token2,
5521 int min, int max, void *data) {
5522 xmlRegAtomPtr atom;
5523 int counter;
5524
5525 if ((am == NULL) || (from == NULL) || (token == NULL))
5526 return(NULL);
5527 if (min < 1)
5528 return(NULL);
5529 if ((max < min) || (max < 1))
5530 return(NULL);
5531 atom = xmlRegNewAtom(am, XML_REGEXP_STRING);
5532 if (atom == NULL)
5533 return(NULL);
5534 if ((token2 == NULL) || (*token2 == 0)) {
5535 atom->valuep = xmlStrdup(token);
5536 } else {
5537 int lenn, lenp;
5538 xmlChar *str;
5539
5540 lenn = strlen((char *) token2);
5541 lenp = strlen((char *) token);
5542
5543 str = (xmlChar *) xmlMallocAtomic(lenn + lenp + 2);
5544 if (str == NULL) {
5545 xmlRegFreeAtom(atom);
5546 return(NULL);
5547 }
5548 memcpy(&str[0], token, lenp);
5549 str[lenp] = '|';
5550 memcpy(&str[lenp + 1], token2, lenn);
5551 str[lenn + lenp + 1] = 0;
5552
5553 atom->valuep = str;
5554 }
5555 atom->data = data;
5556 atom->quant = XML_REGEXP_QUANT_ONCEONLY;
5557 if (min == 0)
5558 atom->min = 1;
5559 else
5560 atom->min = min;
5561 atom->max = max;
5562 /*
5563 * associate a counter to the transition.
5564 */
5565 counter = xmlRegGetCounter(am);
5566 am->counters[counter].min = 1;
5567 am->counters[counter].max = 1;
5568
5569 /* xmlFAGenerateTransitions(am, from, to, atom); */
5570 if (to == NULL) {
5571 to = xmlRegNewState(am);
5572 xmlRegStatePush(am, to);
5573 }
Daniel Veillard5de09382005-09-26 17:18:17 +00005574 xmlRegStateAddTrans(am, from, atom, to, counter, -1);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00005575 xmlRegAtomPush(am, atom);
5576 am->state = to;
5577 return(to);
5578}
5579
5580
5581
5582/**
Daniel Veillard7646b182002-04-20 06:41:40 +00005583 * xmlAutomataNewOnceTrans:
5584 * @am: an automata
5585 * @from: the starting point of the transition
5586 * @to: the target point of the transition or NULL
5587 * @token: the input string associated to that transition
5588 * @min: the minimum successive occurences of token
Daniel Veillarda9b66d02002-12-11 14:23:49 +00005589 * @max: the maximum successive occurences of token
5590 * @data: data associated to the transition
Daniel Veillard7646b182002-04-20 06:41:40 +00005591 *
William M. Brackddf71d62004-05-06 04:17:26 +00005592 * If @to is NULL, this creates first a new target state in the automata
Daniel Veillard7646b182002-04-20 06:41:40 +00005593 * and then adds a transition from the @from state to the target state
5594 * activated by a succession of input of value @token and whose number
William M. Brackddf71d62004-05-06 04:17:26 +00005595 * is between @min and @max, moreover that transition can only be crossed
Daniel Veillard7646b182002-04-20 06:41:40 +00005596 * once.
5597 *
5598 * Returns the target state or NULL in case of error
5599 */
5600xmlAutomataStatePtr
5601xmlAutomataNewOnceTrans(xmlAutomataPtr am, xmlAutomataStatePtr from,
5602 xmlAutomataStatePtr to, const xmlChar *token,
5603 int min, int max, void *data) {
5604 xmlRegAtomPtr atom;
5605 int counter;
5606
5607 if ((am == NULL) || (from == NULL) || (token == NULL))
5608 return(NULL);
5609 if (min < 1)
5610 return(NULL);
5611 if ((max < min) || (max < 1))
5612 return(NULL);
5613 atom = xmlRegNewAtom(am, XML_REGEXP_STRING);
5614 if (atom == NULL)
5615 return(NULL);
5616 atom->valuep = xmlStrdup(token);
5617 atom->data = data;
5618 atom->quant = XML_REGEXP_QUANT_ONCEONLY;
5619 if (min == 0)
5620 atom->min = 1;
5621 else
5622 atom->min = min;
5623 atom->max = max;
5624 /*
5625 * associate a counter to the transition.
5626 */
5627 counter = xmlRegGetCounter(am);
5628 am->counters[counter].min = 1;
5629 am->counters[counter].max = 1;
5630
5631 /* xmlFAGenerateTransitions(am, from, to, atom); */
5632 if (to == NULL) {
5633 to = xmlRegNewState(am);
5634 xmlRegStatePush(am, to);
5635 }
Daniel Veillard5de09382005-09-26 17:18:17 +00005636 xmlRegStateAddTrans(am, from, atom, to, counter, -1);
Daniel Veillard7646b182002-04-20 06:41:40 +00005637 xmlRegAtomPush(am, atom);
5638 am->state = to;
Daniel Veillard7646b182002-04-20 06:41:40 +00005639 return(to);
5640}
5641
5642/**
Daniel Veillard4255d502002-04-16 15:50:10 +00005643 * xmlAutomataNewState:
5644 * @am: an automata
5645 *
5646 * Create a new disconnected state in the automata
5647 *
5648 * Returns the new state or NULL in case of error
5649 */
5650xmlAutomataStatePtr
5651xmlAutomataNewState(xmlAutomataPtr am) {
5652 xmlAutomataStatePtr to;
5653
5654 if (am == NULL)
5655 return(NULL);
5656 to = xmlRegNewState(am);
5657 xmlRegStatePush(am, to);
5658 return(to);
5659}
5660
5661/**
Daniel Veillarda9b66d02002-12-11 14:23:49 +00005662 * xmlAutomataNewEpsilon:
Daniel Veillard4255d502002-04-16 15:50:10 +00005663 * @am: an automata
5664 * @from: the starting point of the transition
5665 * @to: the target point of the transition or NULL
5666 *
William M. Brackddf71d62004-05-06 04:17:26 +00005667 * If @to is NULL, this creates first a new target state in the automata
5668 * and then adds an epsilon transition from the @from state to the
Daniel Veillard4255d502002-04-16 15:50:10 +00005669 * target state
5670 *
5671 * Returns the target state or NULL in case of error
5672 */
5673xmlAutomataStatePtr
5674xmlAutomataNewEpsilon(xmlAutomataPtr am, xmlAutomataStatePtr from,
5675 xmlAutomataStatePtr to) {
5676 if ((am == NULL) || (from == NULL))
5677 return(NULL);
5678 xmlFAGenerateEpsilonTransition(am, from, to);
5679 if (to == NULL)
5680 return(am->state);
5681 return(to);
5682}
5683
Daniel Veillardb509f152002-04-17 16:28:10 +00005684/**
Daniel Veillard7646b182002-04-20 06:41:40 +00005685 * xmlAutomataNewAllTrans:
5686 * @am: an automata
5687 * @from: the starting point of the transition
5688 * @to: the target point of the transition or NULL
Daniel Veillarda9b66d02002-12-11 14:23:49 +00005689 * @lax: allow to transition if not all all transitions have been activated
Daniel Veillard7646b182002-04-20 06:41:40 +00005690 *
William M. Brackddf71d62004-05-06 04:17:26 +00005691 * If @to is NULL, this creates first a new target state in the automata
Daniel Veillard7646b182002-04-20 06:41:40 +00005692 * and then adds a an ALL transition from the @from state to the
5693 * target state. That transition is an epsilon transition allowed only when
5694 * all transitions from the @from node have been activated.
5695 *
5696 * Returns the target state or NULL in case of error
5697 */
5698xmlAutomataStatePtr
5699xmlAutomataNewAllTrans(xmlAutomataPtr am, xmlAutomataStatePtr from,
Daniel Veillard441bc322002-04-20 17:38:48 +00005700 xmlAutomataStatePtr to, int lax) {
Daniel Veillard7646b182002-04-20 06:41:40 +00005701 if ((am == NULL) || (from == NULL))
5702 return(NULL);
Daniel Veillard441bc322002-04-20 17:38:48 +00005703 xmlFAGenerateAllTransition(am, from, to, lax);
Daniel Veillard7646b182002-04-20 06:41:40 +00005704 if (to == NULL)
5705 return(am->state);
5706 return(to);
5707}
5708
5709/**
Daniel Veillardb509f152002-04-17 16:28:10 +00005710 * xmlAutomataNewCounter:
5711 * @am: an automata
5712 * @min: the minimal value on the counter
5713 * @max: the maximal value on the counter
5714 *
5715 * Create a new counter
5716 *
5717 * Returns the counter number or -1 in case of error
5718 */
5719int
5720xmlAutomataNewCounter(xmlAutomataPtr am, int min, int max) {
5721 int ret;
5722
5723 if (am == NULL)
5724 return(-1);
5725
5726 ret = xmlRegGetCounter(am);
5727 if (ret < 0)
5728 return(-1);
5729 am->counters[ret].min = min;
5730 am->counters[ret].max = max;
5731 return(ret);
5732}
5733
5734/**
5735 * xmlAutomataNewCountedTrans:
5736 * @am: an automata
5737 * @from: the starting point of the transition
5738 * @to: the target point of the transition or NULL
5739 * @counter: the counter associated to that transition
5740 *
William M. Brackddf71d62004-05-06 04:17:26 +00005741 * If @to is NULL, this creates first a new target state in the automata
Daniel Veillardb509f152002-04-17 16:28:10 +00005742 * and then adds an epsilon transition from the @from state to the target state
5743 * which will increment the counter provided
5744 *
5745 * Returns the target state or NULL in case of error
5746 */
5747xmlAutomataStatePtr
5748xmlAutomataNewCountedTrans(xmlAutomataPtr am, xmlAutomataStatePtr from,
5749 xmlAutomataStatePtr to, int counter) {
5750 if ((am == NULL) || (from == NULL) || (counter < 0))
5751 return(NULL);
5752 xmlFAGenerateCountedEpsilonTransition(am, from, to, counter);
5753 if (to == NULL)
5754 return(am->state);
5755 return(to);
5756}
5757
5758/**
5759 * xmlAutomataNewCounterTrans:
5760 * @am: an automata
5761 * @from: the starting point of the transition
5762 * @to: the target point of the transition or NULL
5763 * @counter: the counter associated to that transition
5764 *
William M. Brackddf71d62004-05-06 04:17:26 +00005765 * If @to is NULL, this creates first a new target state in the automata
Daniel Veillardb509f152002-04-17 16:28:10 +00005766 * and then adds an epsilon transition from the @from state to the target state
5767 * which will be allowed only if the counter is within the right range.
5768 *
5769 * Returns the target state or NULL in case of error
5770 */
5771xmlAutomataStatePtr
5772xmlAutomataNewCounterTrans(xmlAutomataPtr am, xmlAutomataStatePtr from,
5773 xmlAutomataStatePtr to, int counter) {
5774 if ((am == NULL) || (from == NULL) || (counter < 0))
5775 return(NULL);
5776 xmlFAGenerateCountedTransition(am, from, to, counter);
5777 if (to == NULL)
5778 return(am->state);
5779 return(to);
5780}
Daniel Veillard4255d502002-04-16 15:50:10 +00005781
5782/**
5783 * xmlAutomataCompile:
5784 * @am: an automata
5785 *
5786 * Compile the automata into a Reg Exp ready for being executed.
5787 * The automata should be free after this point.
5788 *
5789 * Returns the compiled regexp or NULL in case of error
5790 */
5791xmlRegexpPtr
5792xmlAutomataCompile(xmlAutomataPtr am) {
5793 xmlRegexpPtr ret;
5794
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00005795 if ((am == NULL) || (am->error != 0)) return(NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +00005796 xmlFAEliminateEpsilonTransitions(am);
Daniel Veillard23e73572002-09-19 19:56:43 +00005797 /* xmlFAComputesDeterminism(am); */
Daniel Veillard4255d502002-04-16 15:50:10 +00005798 ret = xmlRegEpxFromParse(am);
5799
5800 return(ret);
5801}
Daniel Veillarde19fc232002-04-22 16:01:24 +00005802
5803/**
5804 * xmlAutomataIsDeterminist:
5805 * @am: an automata
5806 *
5807 * Checks if an automata is determinist.
5808 *
5809 * Returns 1 if true, 0 if not, and -1 in case of error
5810 */
5811int
5812xmlAutomataIsDeterminist(xmlAutomataPtr am) {
5813 int ret;
5814
5815 if (am == NULL)
5816 return(-1);
5817
5818 ret = xmlFAComputesDeterminism(am);
5819 return(ret);
5820}
Daniel Veillard4255d502002-04-16 15:50:10 +00005821#endif /* LIBXML_AUTOMATA_ENABLED */
Daniel Veillard81a8ec62005-08-22 00:20:58 +00005822
5823#ifdef LIBXML_EXPR_ENABLED
5824/************************************************************************
5825 * *
5826 * Formal Expression handling code *
5827 * *
5828 ************************************************************************/
Daniel Veillard81a8ec62005-08-22 00:20:58 +00005829/************************************************************************
5830 * *
5831 * Expression handling context *
5832 * *
5833 ************************************************************************/
5834
5835struct _xmlExpCtxt {
5836 xmlDictPtr dict;
5837 xmlExpNodePtr *table;
5838 int size;
5839 int nbElems;
5840 int nb_nodes;
5841 const char *expr;
5842 const char *cur;
5843 int nb_cons;
Daniel Veillard81a8ec62005-08-22 00:20:58 +00005844 int tabSize;
5845};
5846
5847/**
5848 * xmlExpNewCtxt:
5849 * @maxNodes: the maximum number of nodes
5850 * @dict: optional dictionnary to use internally
5851 *
5852 * Creates a new context for manipulating expressions
5853 *
5854 * Returns the context or NULL in case of error
5855 */
5856xmlExpCtxtPtr
5857xmlExpNewCtxt(int maxNodes, xmlDictPtr dict) {
5858 xmlExpCtxtPtr ret;
5859 int size = 256;
5860
5861 if (maxNodes <= 4096)
5862 maxNodes = 4096;
5863
5864 ret = (xmlExpCtxtPtr) xmlMalloc(sizeof(xmlExpCtxt));
5865 if (ret == NULL)
5866 return(NULL);
5867 memset(ret, 0, sizeof(xmlExpCtxt));
5868 ret->size = size;
5869 ret->nbElems = 0;
5870 ret->table = xmlMalloc(size * sizeof(xmlExpNodePtr));
5871 if (ret->table == NULL) {
5872 xmlFree(ret);
5873 return(NULL);
5874 }
5875 memset(ret->table, 0, size * sizeof(xmlExpNodePtr));
5876 if (dict == NULL) {
5877 ret->dict = xmlDictCreate();
5878 if (ret->dict == NULL) {
5879 xmlFree(ret->table);
5880 xmlFree(ret);
5881 return(NULL);
5882 }
5883 } else {
5884 ret->dict = dict;
5885 xmlDictReference(ret->dict);
5886 }
5887 return(ret);
5888}
5889
5890/**
5891 * xmlExpFreeCtxt:
5892 * @ctxt: an expression context
5893 *
5894 * Free an expression context
5895 */
5896void
5897xmlExpFreeCtxt(xmlExpCtxtPtr ctxt) {
5898 if (ctxt == NULL)
5899 return;
5900 xmlDictFree(ctxt->dict);
5901 if (ctxt->table != NULL)
5902 xmlFree(ctxt->table);
5903 xmlFree(ctxt);
5904}
5905
5906/************************************************************************
5907 * *
5908 * Structure associated to an expression node *
5909 * *
5910 ************************************************************************/
Daniel Veillard465a0002005-08-22 12:07:04 +00005911#define MAX_NODES 10000
5912
5913/* #define DEBUG_DERIV */
5914
5915/*
5916 * TODO:
5917 * - Wildcards
5918 * - public API for creation
5919 *
5920 * Started
5921 * - regression testing
5922 *
5923 * Done
5924 * - split into module and test tool
5925 * - memleaks
5926 */
Daniel Veillard81a8ec62005-08-22 00:20:58 +00005927
5928typedef enum {
5929 XML_EXP_NILABLE = (1 << 0)
5930} xmlExpNodeInfo;
5931
5932#define IS_NILLABLE(node) ((node)->info & XML_EXP_NILABLE)
5933
5934struct _xmlExpNode {
5935 unsigned char type;/* xmlExpNodeType */
5936 unsigned char info;/* OR of xmlExpNodeInfo */
5937 unsigned short key; /* the hash key */
5938 unsigned int ref; /* The number of references */
5939 int c_max; /* the maximum length it can consume */
5940 xmlExpNodePtr exp_left;
5941 xmlExpNodePtr next;/* the next node in the hash table or free list */
5942 union {
5943 struct {
5944 int f_min;
5945 int f_max;
5946 } count;
5947 struct {
5948 xmlExpNodePtr f_right;
5949 } children;
5950 const xmlChar *f_str;
5951 } field;
5952};
5953
5954#define exp_min field.count.f_min
5955#define exp_max field.count.f_max
5956/* #define exp_left field.children.f_left */
5957#define exp_right field.children.f_right
5958#define exp_str field.f_str
5959
5960static xmlExpNodePtr xmlExpNewNode(xmlExpCtxtPtr ctxt, xmlExpNodeType type);
5961static xmlExpNode forbiddenExpNode = {
5962 XML_EXP_FORBID, 0, 0, 0, 0, NULL, NULL, {{ 0, 0}}
5963};
5964xmlExpNodePtr forbiddenExp = &forbiddenExpNode;
5965static xmlExpNode emptyExpNode = {
5966 XML_EXP_EMPTY, 1, 0, 0, 0, NULL, NULL, {{ 0, 0}}
5967};
5968xmlExpNodePtr emptyExp = &emptyExpNode;
5969
5970/************************************************************************
5971 * *
5972 * The custom hash table for unicity and canonicalization *
5973 * of sub-expressions pointers *
5974 * *
5975 ************************************************************************/
5976/*
5977 * xmlExpHashNameComputeKey:
5978 * Calculate the hash key for a token
5979 */
5980static unsigned short
5981xmlExpHashNameComputeKey(const xmlChar *name) {
5982 unsigned short value = 0L;
5983 char ch;
5984
5985 if (name != NULL) {
5986 value += 30 * (*name);
5987 while ((ch = *name++) != 0) {
5988 value = value ^ ((value << 5) + (value >> 3) + (unsigned long)ch);
5989 }
5990 }
5991 return (value);
5992}
5993
5994/*
5995 * xmlExpHashComputeKey:
5996 * Calculate the hash key for a compound expression
5997 */
5998static unsigned short
5999xmlExpHashComputeKey(xmlExpNodeType type, xmlExpNodePtr left,
6000 xmlExpNodePtr right) {
6001 unsigned long value;
6002 unsigned short ret;
6003
6004 switch (type) {
6005 case XML_EXP_SEQ:
6006 value = left->key;
6007 value += right->key;
6008 value *= 3;
6009 ret = (unsigned short) value;
6010 break;
6011 case XML_EXP_OR:
6012 value = left->key;
6013 value += right->key;
6014 value *= 7;
6015 ret = (unsigned short) value;
6016 break;
6017 case XML_EXP_COUNT:
6018 value = left->key;
6019 value += right->key;
6020 ret = (unsigned short) value;
6021 break;
6022 default:
6023 ret = 0;
6024 }
6025 return(ret);
6026}
6027
6028
6029static xmlExpNodePtr
6030xmlExpNewNode(xmlExpCtxtPtr ctxt, xmlExpNodeType type) {
6031 xmlExpNodePtr ret;
6032
6033 if (ctxt->nb_nodes >= MAX_NODES)
6034 return(NULL);
6035 ret = (xmlExpNodePtr) xmlMalloc(sizeof(xmlExpNode));
6036 if (ret == NULL)
6037 return(NULL);
6038 memset(ret, 0, sizeof(xmlExpNode));
6039 ret->type = type;
6040 ret->next = NULL;
6041 ctxt->nb_nodes++;
6042 ctxt->nb_cons++;
6043 return(ret);
6044}
6045
6046/**
6047 * xmlExpHashGetEntry:
6048 * @table: the hash table
6049 *
6050 * Get the unique entry from the hash table. The entry is created if
6051 * needed. @left and @right are consumed, i.e. their ref count will
6052 * be decremented by the operation.
6053 *
6054 * Returns the pointer or NULL in case of error
6055 */
6056static xmlExpNodePtr
6057xmlExpHashGetEntry(xmlExpCtxtPtr ctxt, xmlExpNodeType type,
6058 xmlExpNodePtr left, xmlExpNodePtr right,
6059 const xmlChar *name, int min, int max) {
6060 unsigned short kbase, key;
6061 xmlExpNodePtr entry;
6062 xmlExpNodePtr insert;
6063
6064 if (ctxt == NULL)
6065 return(NULL);
6066
6067 /*
6068 * Check for duplicate and insertion location.
6069 */
6070 if (type == XML_EXP_ATOM) {
6071 kbase = xmlExpHashNameComputeKey(name);
6072 } else if (type == XML_EXP_COUNT) {
6073 /* COUNT reduction rule 1 */
6074 /* a{1} -> a */
6075 if (min == max) {
6076 if (min == 1) {
6077 return(left);
6078 }
6079 if (min == 0) {
6080 xmlExpFree(ctxt, left);
6081 return(emptyExp);
6082 }
6083 }
6084 if (min < 0) {
6085 xmlExpFree(ctxt, left);
6086 return(forbiddenExp);
6087 }
6088 if (max == -1)
6089 kbase = min + 79;
6090 else
6091 kbase = max - min;
6092 kbase += left->key;
6093 } else if (type == XML_EXP_OR) {
6094 /* Forbid reduction rules */
6095 if (left->type == XML_EXP_FORBID) {
6096 xmlExpFree(ctxt, left);
6097 return(right);
6098 }
6099 if (right->type == XML_EXP_FORBID) {
6100 xmlExpFree(ctxt, right);
6101 return(left);
6102 }
6103
6104 /* OR reduction rule 1 */
6105 /* a | a reduced to a */
6106 if (left == right) {
6107 left->ref--;
6108 return(left);
6109 }
6110 /* OR canonicalization rule 1 */
6111 /* linearize (a | b) | c into a | (b | c) */
6112 if ((left->type == XML_EXP_OR) && (right->type != XML_EXP_OR)) {
6113 xmlExpNodePtr tmp = left;
6114 left = right;
6115 right = tmp;
6116 }
6117 /* OR reduction rule 2 */
6118 /* a | (a | b) and b | (a | b) are reduced to a | b */
6119 if (right->type == XML_EXP_OR) {
6120 if ((left == right->exp_left) ||
6121 (left == right->exp_right)) {
6122 xmlExpFree(ctxt, left);
6123 return(right);
6124 }
6125 }
6126 /* OR canonicalization rule 2 */
6127 /* linearize (a | b) | c into a | (b | c) */
6128 if (left->type == XML_EXP_OR) {
6129 xmlExpNodePtr tmp;
6130
6131 /* OR canonicalization rule 2 */
6132 if ((left->exp_right->type != XML_EXP_OR) &&
6133 (left->exp_right->key < left->exp_left->key)) {
6134 tmp = left->exp_right;
6135 left->exp_right = left->exp_left;
6136 left->exp_left = tmp;
6137 }
6138 left->exp_right->ref++;
6139 tmp = xmlExpHashGetEntry(ctxt, XML_EXP_OR, left->exp_right, right,
6140 NULL, 0, 0);
6141 left->exp_left->ref++;
6142 tmp = xmlExpHashGetEntry(ctxt, XML_EXP_OR, left->exp_left, tmp,
6143 NULL, 0, 0);
6144
6145 xmlExpFree(ctxt, left);
6146 return(tmp);
6147 }
6148 if (right->type == XML_EXP_OR) {
6149 /* Ordering in the tree */
6150 /* C | (A | B) -> A | (B | C) */
6151 if (left->key > right->exp_right->key) {
6152 xmlExpNodePtr tmp;
6153 right->exp_right->ref++;
6154 tmp = xmlExpHashGetEntry(ctxt, XML_EXP_OR, right->exp_right,
6155 left, NULL, 0, 0);
6156 right->exp_left->ref++;
6157 tmp = xmlExpHashGetEntry(ctxt, XML_EXP_OR, right->exp_left,
6158 tmp, NULL, 0, 0);
6159 xmlExpFree(ctxt, right);
6160 return(tmp);
6161 }
6162 /* Ordering in the tree */
6163 /* B | (A | C) -> A | (B | C) */
6164 if (left->key > right->exp_left->key) {
6165 xmlExpNodePtr tmp;
6166 right->exp_right->ref++;
6167 tmp = xmlExpHashGetEntry(ctxt, XML_EXP_OR, left,
6168 right->exp_right, NULL, 0, 0);
6169 right->exp_left->ref++;
6170 tmp = xmlExpHashGetEntry(ctxt, XML_EXP_OR, right->exp_left,
6171 tmp, NULL, 0, 0);
6172 xmlExpFree(ctxt, right);
6173 return(tmp);
6174 }
6175 }
6176 /* we know both types are != XML_EXP_OR here */
6177 else if (left->key > right->key) {
6178 xmlExpNodePtr tmp = left;
6179 left = right;
6180 right = tmp;
6181 }
6182 kbase = xmlExpHashComputeKey(type, left, right);
6183 } else if (type == XML_EXP_SEQ) {
6184 /* Forbid reduction rules */
6185 if (left->type == XML_EXP_FORBID) {
6186 xmlExpFree(ctxt, right);
6187 return(left);
6188 }
6189 if (right->type == XML_EXP_FORBID) {
6190 xmlExpFree(ctxt, left);
6191 return(right);
6192 }
6193 /* Empty reduction rules */
6194 if (right->type == XML_EXP_EMPTY) {
6195 return(left);
6196 }
6197 if (left->type == XML_EXP_EMPTY) {
6198 return(right);
6199 }
6200 kbase = xmlExpHashComputeKey(type, left, right);
6201 } else
6202 return(NULL);
6203
6204 key = kbase % ctxt->size;
6205 if (ctxt->table[key] != NULL) {
6206 for (insert = ctxt->table[key]; insert != NULL;
6207 insert = insert->next) {
6208 if ((insert->key == kbase) &&
6209 (insert->type == type)) {
6210 if (type == XML_EXP_ATOM) {
6211 if (name == insert->exp_str) {
6212 insert->ref++;
6213 return(insert);
6214 }
6215 } else if (type == XML_EXP_COUNT) {
6216 if ((insert->exp_min == min) && (insert->exp_max == max) &&
6217 (insert->exp_left == left)) {
6218 insert->ref++;
6219 left->ref--;
6220 return(insert);
6221 }
6222 } else if ((insert->exp_left == left) &&
6223 (insert->exp_right == right)) {
6224 insert->ref++;
6225 left->ref--;
6226 right->ref--;
6227 return(insert);
6228 }
6229 }
6230 }
6231 }
6232
6233 entry = xmlExpNewNode(ctxt, type);
6234 if (entry == NULL)
6235 return(NULL);
6236 entry->key = kbase;
6237 if (type == XML_EXP_ATOM) {
6238 entry->exp_str = name;
6239 entry->c_max = 1;
6240 } else if (type == XML_EXP_COUNT) {
6241 entry->exp_min = min;
6242 entry->exp_max = max;
6243 entry->exp_left = left;
6244 if ((min == 0) || (IS_NILLABLE(left)))
6245 entry->info |= XML_EXP_NILABLE;
6246 if (max < 0)
6247 entry->c_max = -1;
6248 else
6249 entry->c_max = max * entry->exp_left->c_max;
6250 } else {
6251 entry->exp_left = left;
6252 entry->exp_right = right;
6253 if (type == XML_EXP_OR) {
6254 if ((IS_NILLABLE(left)) || (IS_NILLABLE(right)))
6255 entry->info |= XML_EXP_NILABLE;
6256 if ((entry->exp_left->c_max == -1) ||
6257 (entry->exp_right->c_max == -1))
6258 entry->c_max = -1;
6259 else if (entry->exp_left->c_max > entry->exp_right->c_max)
6260 entry->c_max = entry->exp_left->c_max;
6261 else
6262 entry->c_max = entry->exp_right->c_max;
6263 } else {
6264 if ((IS_NILLABLE(left)) && (IS_NILLABLE(right)))
6265 entry->info |= XML_EXP_NILABLE;
6266 if ((entry->exp_left->c_max == -1) ||
6267 (entry->exp_right->c_max == -1))
6268 entry->c_max = -1;
6269 else
6270 entry->c_max = entry->exp_left->c_max + entry->exp_right->c_max;
6271 }
6272 }
6273 entry->ref = 1;
6274 if (ctxt->table[key] != NULL)
6275 entry->next = ctxt->table[key];
6276
6277 ctxt->table[key] = entry;
6278 ctxt->nbElems++;
6279
6280 return(entry);
6281}
6282
6283/**
6284 * xmlExpFree:
6285 * @ctxt: the expression context
6286 * @exp: the expression
6287 *
6288 * Dereference the expression
6289 */
6290void
6291xmlExpFree(xmlExpCtxtPtr ctxt, xmlExpNodePtr exp) {
6292 if ((exp == NULL) || (exp == forbiddenExp) || (exp == emptyExp))
6293 return;
6294 exp->ref--;
Daniel Veillard81a8ec62005-08-22 00:20:58 +00006295 if (exp->ref == 0) {
6296 unsigned short key;
6297
6298 /* Unlink it first from the hash table */
6299 key = exp->key % ctxt->size;
6300 if (ctxt->table[key] == exp) {
6301 ctxt->table[key] = exp->next;
6302 } else {
6303 xmlExpNodePtr tmp;
6304
6305 tmp = ctxt->table[key];
6306 while (tmp != NULL) {
6307 if (tmp->next == exp) {
6308 tmp->next = exp->next;
6309 break;
6310 }
6311 tmp = tmp->next;
6312 }
6313 }
6314
6315 if ((exp->type == XML_EXP_SEQ) || (exp->type == XML_EXP_OR)) {
6316 xmlExpFree(ctxt, exp->exp_left);
6317 xmlExpFree(ctxt, exp->exp_right);
6318 } else if (exp->type == XML_EXP_COUNT) {
6319 xmlExpFree(ctxt, exp->exp_left);
6320 }
6321 xmlFree(exp);
Daniel Veillard81a8ec62005-08-22 00:20:58 +00006322 ctxt->nb_nodes--;
6323 }
6324}
6325
6326/**
6327 * xmlExpRef:
6328 * @exp: the expression
6329 *
6330 * Increase the reference count of the expression
6331 */
6332void
6333xmlExpRef(xmlExpNodePtr exp) {
6334 if (exp != NULL)
6335 exp->ref++;
6336}
6337
Daniel Veillardccb4d412005-08-23 13:41:17 +00006338/**
6339 * xmlExpNewAtom:
6340 * @ctxt: the expression context
6341 * @name: the atom name
6342 * @len: the atom name lenght in byte (or -1);
6343 *
6344 * Get the atom associated to this name from that context
6345 *
6346 * Returns the node or NULL in case of error
6347 */
6348xmlExpNodePtr
6349xmlExpNewAtom(xmlExpCtxtPtr ctxt, const xmlChar *name, int len) {
6350 if ((ctxt == NULL) || (name == NULL))
6351 return(NULL);
6352 name = xmlDictLookup(ctxt->dict, name, len);
6353 if (name == NULL)
6354 return(NULL);
6355 return(xmlExpHashGetEntry(ctxt, XML_EXP_ATOM, NULL, NULL, name, 0, 0));
6356}
6357
6358/**
6359 * xmlExpNewOr:
6360 * @ctxt: the expression context
6361 * @left: left expression
6362 * @right: right expression
6363 *
6364 * Get the atom associated to the choice @left | @right
6365 * Note that @left and @right are consumed in the operation, to keep
6366 * an handle on them use xmlExpRef() and use xmlExpFree() to release them,
6367 * this is true even in case of failure (unless ctxt == NULL).
6368 *
6369 * Returns the node or NULL in case of error
6370 */
6371xmlExpNodePtr
6372xmlExpNewOr(xmlExpCtxtPtr ctxt, xmlExpNodePtr left, xmlExpNodePtr right) {
6373 if ((ctxt == NULL) || (left == NULL) || (right == NULL)) {
6374 xmlExpFree(ctxt, left);
6375 xmlExpFree(ctxt, right);
6376 return(NULL);
6377 }
6378 return(xmlExpHashGetEntry(ctxt, XML_EXP_OR, left, right, NULL, 0, 0));
6379}
6380
6381/**
6382 * xmlExpNewSeq:
6383 * @ctxt: the expression context
6384 * @left: left expression
6385 * @right: right expression
6386 *
6387 * Get the atom associated to the sequence @left , @right
6388 * Note that @left and @right are consumed in the operation, to keep
6389 * an handle on them use xmlExpRef() and use xmlExpFree() to release them,
6390 * this is true even in case of failure (unless ctxt == NULL).
6391 *
6392 * Returns the node or NULL in case of error
6393 */
6394xmlExpNodePtr
6395xmlExpNewSeq(xmlExpCtxtPtr ctxt, xmlExpNodePtr left, xmlExpNodePtr right) {
6396 if ((ctxt == NULL) || (left == NULL) || (right == NULL)) {
6397 xmlExpFree(ctxt, left);
6398 xmlExpFree(ctxt, right);
6399 return(NULL);
6400 }
6401 return(xmlExpHashGetEntry(ctxt, XML_EXP_SEQ, left, right, NULL, 0, 0));
6402}
6403
6404/**
6405 * xmlExpNewRange:
6406 * @ctxt: the expression context
6407 * @subset: the expression to be repeated
6408 * @min: the lower bound for the repetition
6409 * @max: the upper bound for the repetition, -1 means infinite
6410 *
6411 * Get the atom associated to the range (@subset){@min, @max}
6412 * Note that @subset is consumed in the operation, to keep
6413 * an handle on it use xmlExpRef() and use xmlExpFree() to release it,
6414 * this is true even in case of failure (unless ctxt == NULL).
6415 *
6416 * Returns the node or NULL in case of error
6417 */
6418xmlExpNodePtr
6419xmlExpNewRange(xmlExpCtxtPtr ctxt, xmlExpNodePtr subset, int min, int max) {
6420 if ((ctxt == NULL) || (subset == NULL) || (min < 0) || (max < -1) ||
6421 ((max >= 0) && (min > max))) {
6422 xmlExpFree(ctxt, subset);
6423 return(NULL);
6424 }
6425 return(xmlExpHashGetEntry(ctxt, XML_EXP_COUNT, subset,
6426 NULL, NULL, min, max));
6427}
6428
Daniel Veillard81a8ec62005-08-22 00:20:58 +00006429/************************************************************************
6430 * *
6431 * Public API for operations on expressions *
6432 * *
6433 ************************************************************************/
6434
6435static int
6436xmlExpGetLanguageInt(xmlExpCtxtPtr ctxt, xmlExpNodePtr exp,
6437 const xmlChar**list, int len, int nb) {
6438 int tmp, tmp2;
6439tail:
6440 switch (exp->type) {
6441 case XML_EXP_EMPTY:
6442 return(0);
6443 case XML_EXP_ATOM:
6444 for (tmp = 0;tmp < nb;tmp++)
6445 if (list[tmp] == exp->exp_str)
6446 return(0);
6447 if (nb >= len)
6448 return(-2);
6449 list[nb++] = exp->exp_str;
6450 return(1);
6451 case XML_EXP_COUNT:
6452 exp = exp->exp_left;
6453 goto tail;
6454 case XML_EXP_SEQ:
6455 case XML_EXP_OR:
6456 tmp = xmlExpGetLanguageInt(ctxt, exp->exp_left, list, len, nb);
6457 if (tmp < 0)
6458 return(tmp);
6459 tmp2 = xmlExpGetLanguageInt(ctxt, exp->exp_right, list, len,
6460 nb + tmp);
6461 if (tmp2 < 0)
6462 return(tmp2);
6463 return(tmp + tmp2);
6464 }
6465 return(-1);
6466}
6467
6468/**
6469 * xmlExpGetLanguage:
6470 * @ctxt: the expression context
6471 * @exp: the expression
Daniel Veillard7802ba52005-10-27 11:56:20 +00006472 * @langList: where to store the tokens
Daniel Veillard81a8ec62005-08-22 00:20:58 +00006473 * @len: the allocated lenght of @list
6474 *
6475 * Find all the strings used in @exp and store them in @list
6476 *
6477 * Returns the number of unique strings found, -1 in case of errors and
6478 * -2 if there is more than @len strings
6479 */
6480int
6481xmlExpGetLanguage(xmlExpCtxtPtr ctxt, xmlExpNodePtr exp,
Daniel Veillard7802ba52005-10-27 11:56:20 +00006482 const xmlChar**langList, int len) {
6483 if ((ctxt == NULL) || (exp == NULL) || (langList == NULL) || (len <= 0))
Daniel Veillard81a8ec62005-08-22 00:20:58 +00006484 return(-1);
Daniel Veillard7802ba52005-10-27 11:56:20 +00006485 return(xmlExpGetLanguageInt(ctxt, exp, langList, len, 0));
Daniel Veillard81a8ec62005-08-22 00:20:58 +00006486}
6487
6488static int
6489xmlExpGetStartInt(xmlExpCtxtPtr ctxt, xmlExpNodePtr exp,
6490 const xmlChar**list, int len, int nb) {
6491 int tmp, tmp2;
6492tail:
6493 switch (exp->type) {
6494 case XML_EXP_FORBID:
6495 return(0);
6496 case XML_EXP_EMPTY:
6497 return(0);
6498 case XML_EXP_ATOM:
6499 for (tmp = 0;tmp < nb;tmp++)
6500 if (list[tmp] == exp->exp_str)
6501 return(0);
6502 if (nb >= len)
6503 return(-2);
6504 list[nb++] = exp->exp_str;
6505 return(1);
6506 case XML_EXP_COUNT:
6507 exp = exp->exp_left;
6508 goto tail;
6509 case XML_EXP_SEQ:
6510 tmp = xmlExpGetStartInt(ctxt, exp->exp_left, list, len, nb);
6511 if (tmp < 0)
6512 return(tmp);
6513 if (IS_NILLABLE(exp->exp_left)) {
6514 tmp2 = xmlExpGetStartInt(ctxt, exp->exp_right, list, len,
6515 nb + tmp);
6516 if (tmp2 < 0)
6517 return(tmp2);
6518 tmp += tmp2;
6519 }
6520 return(tmp);
6521 case XML_EXP_OR:
6522 tmp = xmlExpGetStartInt(ctxt, exp->exp_left, list, len, nb);
6523 if (tmp < 0)
6524 return(tmp);
6525 tmp2 = xmlExpGetStartInt(ctxt, exp->exp_right, list, len,
6526 nb + tmp);
6527 if (tmp2 < 0)
6528 return(tmp2);
6529 return(tmp + tmp2);
6530 }
6531 return(-1);
6532}
6533
6534/**
6535 * xmlExpGetStart:
6536 * @ctxt: the expression context
6537 * @exp: the expression
Daniel Veillard7802ba52005-10-27 11:56:20 +00006538 * @tokList: where to store the tokens
Daniel Veillard81a8ec62005-08-22 00:20:58 +00006539 * @len: the allocated lenght of @list
6540 *
6541 * Find all the strings that appears at the start of the languages
6542 * accepted by @exp and store them in @list. E.g. for (a, b) | c
6543 * it will return the list [a, c]
6544 *
6545 * Returns the number of unique strings found, -1 in case of errors and
6546 * -2 if there is more than @len strings
6547 */
6548int
6549xmlExpGetStart(xmlExpCtxtPtr ctxt, xmlExpNodePtr exp,
Daniel Veillard7802ba52005-10-27 11:56:20 +00006550 const xmlChar**tokList, int len) {
6551 if ((ctxt == NULL) || (exp == NULL) || (tokList == NULL) || (len <= 0))
Daniel Veillard81a8ec62005-08-22 00:20:58 +00006552 return(-1);
Daniel Veillard7802ba52005-10-27 11:56:20 +00006553 return(xmlExpGetStartInt(ctxt, exp, tokList, len, 0));
Daniel Veillard81a8ec62005-08-22 00:20:58 +00006554}
6555
6556/**
6557 * xmlExpIsNillable:
6558 * @exp: the expression
6559 *
6560 * Finds if the expression is nillable, i.e. if it accepts the empty sequqnce
6561 *
6562 * Returns 1 if nillable, 0 if not and -1 in case of error
6563 */
6564int
6565xmlExpIsNillable(xmlExpNodePtr exp) {
6566 if (exp == NULL)
6567 return(-1);
6568 return(IS_NILLABLE(exp) != 0);
6569}
6570
6571static xmlExpNodePtr
6572xmlExpStringDeriveInt(xmlExpCtxtPtr ctxt, xmlExpNodePtr exp, const xmlChar *str)
6573{
6574 xmlExpNodePtr ret;
6575
6576 switch (exp->type) {
6577 case XML_EXP_EMPTY:
6578 return(forbiddenExp);
6579 case XML_EXP_FORBID:
6580 return(forbiddenExp);
6581 case XML_EXP_ATOM:
6582 if (exp->exp_str == str) {
6583#ifdef DEBUG_DERIV
6584 printf("deriv atom: equal => Empty\n");
6585#endif
6586 ret = emptyExp;
6587 } else {
6588#ifdef DEBUG_DERIV
6589 printf("deriv atom: mismatch => forbid\n");
6590#endif
6591 /* TODO wildcards here */
6592 ret = forbiddenExp;
6593 }
6594 return(ret);
6595 case XML_EXP_OR: {
6596 xmlExpNodePtr tmp;
6597
6598#ifdef DEBUG_DERIV
6599 printf("deriv or: => or(derivs)\n");
6600#endif
6601 tmp = xmlExpStringDeriveInt(ctxt, exp->exp_left, str);
6602 if (tmp == NULL) {
6603 return(NULL);
6604 }
6605 ret = xmlExpStringDeriveInt(ctxt, exp->exp_right, str);
6606 if (ret == NULL) {
6607 xmlExpFree(ctxt, tmp);
6608 return(NULL);
6609 }
6610 ret = xmlExpHashGetEntry(ctxt, XML_EXP_OR, tmp, ret,
6611 NULL, 0, 0);
6612 return(ret);
6613 }
6614 case XML_EXP_SEQ:
6615#ifdef DEBUG_DERIV
6616 printf("deriv seq: starting with left\n");
6617#endif
6618 ret = xmlExpStringDeriveInt(ctxt, exp->exp_left, str);
6619 if (ret == NULL) {
6620 return(NULL);
6621 } else if (ret == forbiddenExp) {
6622 if (IS_NILLABLE(exp->exp_left)) {
6623#ifdef DEBUG_DERIV
6624 printf("deriv seq: left failed but nillable\n");
6625#endif
6626 ret = xmlExpStringDeriveInt(ctxt, exp->exp_right, str);
6627 }
6628 } else {
6629#ifdef DEBUG_DERIV
6630 printf("deriv seq: left match => sequence\n");
6631#endif
6632 exp->exp_right->ref++;
6633 ret = xmlExpHashGetEntry(ctxt, XML_EXP_SEQ, ret, exp->exp_right,
6634 NULL, 0, 0);
6635 }
6636 return(ret);
6637 case XML_EXP_COUNT: {
6638 int min, max;
6639 xmlExpNodePtr tmp;
6640
6641 if (exp->exp_max == 0)
6642 return(forbiddenExp);
6643 ret = xmlExpStringDeriveInt(ctxt, exp->exp_left, str);
6644 if (ret == NULL)
6645 return(NULL);
6646 if (ret == forbiddenExp) {
6647#ifdef DEBUG_DERIV
6648 printf("deriv count: pattern mismatch => forbid\n");
6649#endif
6650 return(ret);
6651 }
6652 if (exp->exp_max == 1)
6653 return(ret);
6654 if (exp->exp_max < 0) /* unbounded */
6655 max = -1;
6656 else
6657 max = exp->exp_max - 1;
6658 if (exp->exp_min > 0)
6659 min = exp->exp_min - 1;
6660 else
6661 min = 0;
6662 exp->exp_left->ref++;
6663 tmp = xmlExpHashGetEntry(ctxt, XML_EXP_COUNT, exp->exp_left, NULL,
6664 NULL, min, max);
6665 if (ret == emptyExp) {
6666#ifdef DEBUG_DERIV
6667 printf("deriv count: match to empty => new count\n");
6668#endif
6669 return(tmp);
6670 }
6671#ifdef DEBUG_DERIV
6672 printf("deriv count: match => sequence with new count\n");
6673#endif
6674 return(xmlExpHashGetEntry(ctxt, XML_EXP_SEQ, ret, tmp,
6675 NULL, 0, 0));
6676 }
6677 }
6678 return(NULL);
6679}
6680
6681/**
6682 * xmlExpStringDerive:
6683 * @ctxt: the expression context
6684 * @exp: the expression
6685 * @str: the string
6686 * @len: the string len in bytes if available
6687 *
6688 * Do one step of Brzozowski derivation of the expression @exp with
6689 * respect to the input string
6690 *
6691 * Returns the resulting expression or NULL in case of internal error
6692 */
6693xmlExpNodePtr
6694xmlExpStringDerive(xmlExpCtxtPtr ctxt, xmlExpNodePtr exp,
6695 const xmlChar *str, int len) {
6696 const xmlChar *input;
6697
6698 if ((exp == NULL) || (ctxt == NULL) || (str == NULL)) {
6699 return(NULL);
6700 }
6701 /*
6702 * check the string is in the dictionnary, if yes use an interned
6703 * copy, otherwise we know it's not an acceptable input
6704 */
6705 input = xmlDictExists(ctxt->dict, str, len);
6706 if (input == NULL) {
6707 return(forbiddenExp);
6708 }
6709 return(xmlExpStringDeriveInt(ctxt, exp, input));
6710}
6711
6712static int
6713xmlExpCheckCard(xmlExpNodePtr exp, xmlExpNodePtr sub) {
6714 int ret = 1;
6715
6716 if (sub->c_max == -1) {
6717 if (exp->c_max != -1)
6718 ret = 0;
6719 } else if ((exp->c_max >= 0) && (exp->c_max < sub->c_max)) {
6720 ret = 0;
6721 }
6722#if 0
6723 if ((IS_NILLABLE(sub)) && (!IS_NILLABLE(exp)))
6724 ret = 0;
6725#endif
6726 return(ret);
6727}
6728
6729static xmlExpNodePtr xmlExpExpDeriveInt(xmlExpCtxtPtr ctxt, xmlExpNodePtr exp,
6730 xmlExpNodePtr sub);
6731/**
6732 * xmlExpDivide:
6733 * @ctxt: the expressions context
6734 * @exp: the englobing expression
6735 * @sub: the subexpression
6736 * @mult: the multiple expression
6737 * @remain: the remain from the derivation of the multiple
6738 *
6739 * Check if exp is a multiple of sub, i.e. if there is a finite number n
6740 * so that sub{n} subsume exp
6741 *
6742 * Returns the multiple value if successful, 0 if it is not a multiple
6743 * and -1 in case of internel error.
6744 */
6745
6746static int
6747xmlExpDivide(xmlExpCtxtPtr ctxt, xmlExpNodePtr exp, xmlExpNodePtr sub,
6748 xmlExpNodePtr *mult, xmlExpNodePtr *remain) {
6749 int i;
6750 xmlExpNodePtr tmp, tmp2;
6751
6752 if (mult != NULL) *mult = NULL;
6753 if (remain != NULL) *remain = NULL;
6754 if (exp->c_max == -1) return(0);
6755 if (IS_NILLABLE(exp) && (!IS_NILLABLE(sub))) return(0);
6756
6757 for (i = 1;i <= exp->c_max;i++) {
6758 sub->ref++;
6759 tmp = xmlExpHashGetEntry(ctxt, XML_EXP_COUNT,
6760 sub, NULL, NULL, i, i);
6761 if (tmp == NULL) {
6762 return(-1);
6763 }
6764 if (!xmlExpCheckCard(tmp, exp)) {
6765 xmlExpFree(ctxt, tmp);
6766 continue;
6767 }
6768 tmp2 = xmlExpExpDeriveInt(ctxt, tmp, exp);
6769 if (tmp2 == NULL) {
6770 xmlExpFree(ctxt, tmp);
6771 return(-1);
6772 }
6773 if ((tmp2 != forbiddenExp) && (IS_NILLABLE(tmp2))) {
6774 if (remain != NULL)
6775 *remain = tmp2;
6776 else
6777 xmlExpFree(ctxt, tmp2);
6778 if (mult != NULL)
6779 *mult = tmp;
6780 else
6781 xmlExpFree(ctxt, tmp);
6782#ifdef DEBUG_DERIV
6783 printf("Divide succeeded %d\n", i);
6784#endif
6785 return(i);
6786 }
6787 xmlExpFree(ctxt, tmp);
6788 xmlExpFree(ctxt, tmp2);
6789 }
6790#ifdef DEBUG_DERIV
6791 printf("Divide failed\n");
6792#endif
6793 return(0);
6794}
6795
6796/**
6797 * xmlExpExpDeriveInt:
6798 * @ctxt: the expressions context
6799 * @exp: the englobing expression
6800 * @sub: the subexpression
6801 *
6802 * Try to do a step of Brzozowski derivation but at a higher level
6803 * the input being a subexpression.
6804 *
6805 * Returns the resulting expression or NULL in case of internal error
6806 */
6807static xmlExpNodePtr
6808xmlExpExpDeriveInt(xmlExpCtxtPtr ctxt, xmlExpNodePtr exp, xmlExpNodePtr sub) {
6809 xmlExpNodePtr ret, tmp, tmp2, tmp3;
6810 const xmlChar **tab;
6811 int len, i;
6812
6813 /*
6814 * In case of equality and if the expression can only consume a finite
6815 * amount, then the derivation is empty
6816 */
6817 if ((exp == sub) && (exp->c_max >= 0)) {
6818#ifdef DEBUG_DERIV
6819 printf("Equal(exp, sub) and finite -> Empty\n");
6820#endif
6821 return(emptyExp);
6822 }
6823 /*
6824 * decompose sub sequence first
6825 */
6826 if (sub->type == XML_EXP_EMPTY) {
6827#ifdef DEBUG_DERIV
6828 printf("Empty(sub) -> Empty\n");
6829#endif
6830 exp->ref++;
6831 return(exp);
6832 }
6833 if (sub->type == XML_EXP_SEQ) {
6834#ifdef DEBUG_DERIV
6835 printf("Seq(sub) -> decompose\n");
6836#endif
6837 tmp = xmlExpExpDeriveInt(ctxt, exp, sub->exp_left);
6838 if (tmp == NULL)
6839 return(NULL);
6840 if (tmp == forbiddenExp)
6841 return(tmp);
6842 ret = xmlExpExpDeriveInt(ctxt, tmp, sub->exp_right);
6843 xmlExpFree(ctxt, tmp);
6844 return(ret);
6845 }
6846 if (sub->type == XML_EXP_OR) {
6847#ifdef DEBUG_DERIV
6848 printf("Or(sub) -> decompose\n");
6849#endif
6850 tmp = xmlExpExpDeriveInt(ctxt, exp, sub->exp_left);
6851 if (tmp == forbiddenExp)
6852 return(tmp);
6853 if (tmp == NULL)
6854 return(NULL);
6855 ret = xmlExpExpDeriveInt(ctxt, exp, sub->exp_right);
6856 if ((ret == NULL) || (ret == forbiddenExp)) {
6857 xmlExpFree(ctxt, tmp);
6858 return(ret);
6859 }
6860 return(xmlExpHashGetEntry(ctxt, XML_EXP_OR, tmp, ret, NULL, 0, 0));
6861 }
6862 if (!xmlExpCheckCard(exp, sub)) {
6863#ifdef DEBUG_DERIV
6864 printf("CheckCard(exp, sub) failed -> Forbid\n");
6865#endif
6866 return(forbiddenExp);
6867 }
6868 switch (exp->type) {
6869 case XML_EXP_EMPTY:
6870 if (sub == emptyExp)
6871 return(emptyExp);
6872#ifdef DEBUG_DERIV
6873 printf("Empty(exp) -> Forbid\n");
6874#endif
6875 return(forbiddenExp);
6876 case XML_EXP_FORBID:
6877#ifdef DEBUG_DERIV
6878 printf("Forbid(exp) -> Forbid\n");
6879#endif
6880 return(forbiddenExp);
6881 case XML_EXP_ATOM:
6882 if (sub->type == XML_EXP_ATOM) {
6883 /* TODO: handle wildcards */
6884 if (exp->exp_str == sub->exp_str) {
6885#ifdef DEBUG_DERIV
6886 printf("Atom match -> Empty\n");
6887#endif
6888 return(emptyExp);
6889 }
6890#ifdef DEBUG_DERIV
6891 printf("Atom mismatch -> Forbid\n");
6892#endif
6893 return(forbiddenExp);
6894 }
6895 if ((sub->type == XML_EXP_COUNT) &&
6896 (sub->exp_max == 1) &&
6897 (sub->exp_left->type == XML_EXP_ATOM)) {
6898 /* TODO: handle wildcards */
6899 if (exp->exp_str == sub->exp_left->exp_str) {
6900#ifdef DEBUG_DERIV
6901 printf("Atom match -> Empty\n");
6902#endif
6903 return(emptyExp);
6904 }
6905#ifdef DEBUG_DERIV
6906 printf("Atom mismatch -> Forbid\n");
6907#endif
6908 return(forbiddenExp);
6909 }
6910#ifdef DEBUG_DERIV
6911 printf("Compex exp vs Atom -> Forbid\n");
6912#endif
6913 return(forbiddenExp);
6914 case XML_EXP_SEQ:
6915 /* try to get the sequence consumed only if possible */
6916 if (xmlExpCheckCard(exp->exp_left, sub)) {
6917 /* See if the sequence can be consumed directly */
6918#ifdef DEBUG_DERIV
6919 printf("Seq trying left only\n");
6920#endif
6921 ret = xmlExpExpDeriveInt(ctxt, exp->exp_left, sub);
6922 if ((ret != forbiddenExp) && (ret != NULL)) {
6923#ifdef DEBUG_DERIV
6924 printf("Seq trying left only worked\n");
6925#endif
6926 /*
6927 * TODO: assumption here that we are determinist
6928 * i.e. we won't get to a nillable exp left
6929 * subset which could be matched by the right
6930 * part too.
6931 * e.g.: (a | b)+,(a | c) and 'a+,a'
6932 */
6933 exp->exp_right->ref++;
6934 return(xmlExpHashGetEntry(ctxt, XML_EXP_SEQ, ret,
6935 exp->exp_right, NULL, 0, 0));
6936 }
6937#ifdef DEBUG_DERIV
6938 } else {
6939 printf("Seq: left too short\n");
6940#endif
6941 }
6942 /* Try instead to decompose */
6943 if (sub->type == XML_EXP_COUNT) {
6944 int min, max;
6945
6946#ifdef DEBUG_DERIV
6947 printf("Seq: sub is a count\n");
6948#endif
6949 ret = xmlExpExpDeriveInt(ctxt, exp->exp_left, sub->exp_left);
6950 if (ret == NULL)
6951 return(NULL);
6952 if (ret != forbiddenExp) {
6953#ifdef DEBUG_DERIV
6954 printf("Seq , Count match on left\n");
6955#endif
6956 if (sub->exp_max < 0)
6957 max = -1;
6958 else
6959 max = sub->exp_max -1;
6960 if (sub->exp_min > 0)
6961 min = sub->exp_min -1;
6962 else
6963 min = 0;
6964 exp->exp_right->ref++;
6965 tmp = xmlExpHashGetEntry(ctxt, XML_EXP_SEQ, ret,
6966 exp->exp_right, NULL, 0, 0);
6967 if (tmp == NULL)
6968 return(NULL);
6969
6970 sub->exp_left->ref++;
6971 tmp2 = xmlExpHashGetEntry(ctxt, XML_EXP_COUNT,
6972 sub->exp_left, NULL, NULL, min, max);
6973 if (tmp2 == NULL) {
6974 xmlExpFree(ctxt, tmp);
6975 return(NULL);
6976 }
6977 ret = xmlExpExpDeriveInt(ctxt, tmp, tmp2);
6978 xmlExpFree(ctxt, tmp);
6979 xmlExpFree(ctxt, tmp2);
6980 return(ret);
6981 }
6982 }
6983 /* we made no progress on structured operations */
6984 break;
6985 case XML_EXP_OR:
6986#ifdef DEBUG_DERIV
6987 printf("Or , trying both side\n");
6988#endif
6989 ret = xmlExpExpDeriveInt(ctxt, exp->exp_left, sub);
6990 if (ret == NULL)
6991 return(NULL);
6992 tmp = xmlExpExpDeriveInt(ctxt, exp->exp_right, sub);
6993 if (tmp == NULL) {
6994 xmlExpFree(ctxt, ret);
6995 return(NULL);
6996 }
6997 return(xmlExpHashGetEntry(ctxt, XML_EXP_OR, ret, tmp, NULL, 0, 0));
6998 case XML_EXP_COUNT: {
6999 int min, max;
7000
7001 if (sub->type == XML_EXP_COUNT) {
7002 /*
7003 * Try to see if the loop is completely subsumed
7004 */
7005 tmp = xmlExpExpDeriveInt(ctxt, exp->exp_left, sub->exp_left);
7006 if (tmp == NULL)
7007 return(NULL);
7008 if (tmp == forbiddenExp) {
7009 int mult;
7010
7011#ifdef DEBUG_DERIV
7012 printf("Count, Count inner don't subsume\n");
7013#endif
7014 mult = xmlExpDivide(ctxt, sub->exp_left, exp->exp_left,
7015 NULL, &tmp);
7016 if (mult <= 0) {
7017#ifdef DEBUG_DERIV
7018 printf("Count, Count not multiple => forbidden\n");
7019#endif
7020 return(forbiddenExp);
7021 }
7022 if (sub->exp_max == -1) {
7023 max = -1;
7024 if (exp->exp_max == -1) {
7025 if (exp->exp_min <= sub->exp_min * mult)
7026 min = 0;
7027 else
7028 min = exp->exp_min - sub->exp_min * mult;
7029 } else {
7030#ifdef DEBUG_DERIV
7031 printf("Count, Count finite can't subsume infinite\n");
7032#endif
7033 xmlExpFree(ctxt, tmp);
7034 return(forbiddenExp);
7035 }
7036 } else {
7037 if (exp->exp_max == -1) {
7038#ifdef DEBUG_DERIV
7039 printf("Infinite loop consume mult finite loop\n");
7040#endif
7041 if (exp->exp_min > sub->exp_min * mult) {
7042 max = -1;
7043 min = exp->exp_min - sub->exp_min * mult;
7044 } else {
7045 max = -1;
7046 min = 0;
7047 }
7048 } else {
7049 if (exp->exp_max < sub->exp_max * mult) {
7050#ifdef DEBUG_DERIV
7051 printf("loops max mult mismatch => forbidden\n");
7052#endif
7053 xmlExpFree(ctxt, tmp);
7054 return(forbiddenExp);
7055 }
7056 if (sub->exp_max * mult > exp->exp_min)
7057 min = 0;
7058 else
7059 min = exp->exp_min - sub->exp_max * mult;
7060 max = exp->exp_max - sub->exp_max * mult;
7061 }
7062 }
7063 } else if (!IS_NILLABLE(tmp)) {
7064 /*
7065 * TODO: loop here to try to grow if working on finite
7066 * blocks.
7067 */
7068#ifdef DEBUG_DERIV
7069 printf("Count, Count remain not nillable => forbidden\n");
7070#endif
7071 xmlExpFree(ctxt, tmp);
7072 return(forbiddenExp);
7073 } else if (sub->exp_max == -1) {
7074 if (exp->exp_max == -1) {
7075 if (exp->exp_min <= sub->exp_min) {
7076#ifdef DEBUG_DERIV
7077 printf("Infinite loops Okay => COUNT(0,Inf)\n");
7078#endif
7079 max = -1;
7080 min = 0;
7081 } else {
7082#ifdef DEBUG_DERIV
7083 printf("Infinite loops min => Count(X,Inf)\n");
7084#endif
7085 max = -1;
7086 min = exp->exp_min - sub->exp_min;
7087 }
7088 } else if (exp->exp_min > sub->exp_min) {
7089#ifdef DEBUG_DERIV
7090 printf("loops min mismatch 1 => forbidden ???\n");
7091#endif
7092 xmlExpFree(ctxt, tmp);
7093 return(forbiddenExp);
7094 } else {
7095 max = -1;
7096 min = 0;
7097 }
7098 } else {
7099 if (exp->exp_max == -1) {
7100#ifdef DEBUG_DERIV
7101 printf("Infinite loop consume finite loop\n");
7102#endif
7103 if (exp->exp_min > sub->exp_min) {
7104 max = -1;
7105 min = exp->exp_min - sub->exp_min;
7106 } else {
7107 max = -1;
7108 min = 0;
7109 }
7110 } else {
7111 if (exp->exp_max < sub->exp_max) {
7112#ifdef DEBUG_DERIV
7113 printf("loops max mismatch => forbidden\n");
7114#endif
7115 xmlExpFree(ctxt, tmp);
7116 return(forbiddenExp);
7117 }
7118 if (sub->exp_max > exp->exp_min)
7119 min = 0;
7120 else
7121 min = exp->exp_min - sub->exp_max;
7122 max = exp->exp_max - sub->exp_max;
7123 }
7124 }
7125#ifdef DEBUG_DERIV
7126 printf("loops match => SEQ(COUNT())\n");
7127#endif
7128 exp->exp_left->ref++;
7129 tmp2 = xmlExpHashGetEntry(ctxt, XML_EXP_COUNT, exp->exp_left,
7130 NULL, NULL, min, max);
7131 if (tmp2 == NULL) {
7132 return(NULL);
7133 }
7134 ret = xmlExpHashGetEntry(ctxt, XML_EXP_SEQ, tmp, tmp2,
7135 NULL, 0, 0);
7136 return(ret);
7137 }
7138 tmp = xmlExpExpDeriveInt(ctxt, exp->exp_left, sub);
7139 if (tmp == NULL)
7140 return(NULL);
7141 if (tmp == forbiddenExp) {
7142#ifdef DEBUG_DERIV
7143 printf("loop mismatch => forbidden\n");
7144#endif
7145 return(forbiddenExp);
7146 }
7147 if (exp->exp_min > 0)
7148 min = exp->exp_min - 1;
7149 else
7150 min = 0;
7151 if (exp->exp_max < 0)
7152 max = -1;
7153 else
7154 max = exp->exp_max - 1;
7155
7156#ifdef DEBUG_DERIV
7157 printf("loop match => SEQ(COUNT())\n");
7158#endif
7159 exp->exp_left->ref++;
7160 tmp2 = xmlExpHashGetEntry(ctxt, XML_EXP_COUNT, exp->exp_left,
7161 NULL, NULL, min, max);
7162 if (tmp2 == NULL)
7163 return(NULL);
7164 ret = xmlExpHashGetEntry(ctxt, XML_EXP_SEQ, tmp, tmp2,
7165 NULL, 0, 0);
7166 return(ret);
7167 }
7168 }
7169
Daniel Veillardccb4d412005-08-23 13:41:17 +00007170#ifdef DEBUG_DERIV
7171 printf("Fallback to derivative\n");
7172#endif
7173 if (IS_NILLABLE(sub)) {
7174 if (!(IS_NILLABLE(exp)))
7175 return(forbiddenExp);
7176 else
7177 ret = emptyExp;
7178 } else
7179 ret = NULL;
Daniel Veillard81a8ec62005-08-22 00:20:58 +00007180 /*
7181 * here the structured derivation made no progress so
7182 * we use the default token based derivation to force one more step
7183 */
7184 if (ctxt->tabSize == 0)
7185 ctxt->tabSize = 40;
7186
7187 tab = (const xmlChar **) xmlMalloc(ctxt->tabSize *
7188 sizeof(const xmlChar *));
7189 if (tab == NULL) {
7190 return(NULL);
7191 }
7192
Daniel Veillard81a8ec62005-08-22 00:20:58 +00007193 /*
7194 * collect all the strings accepted by the subexpression on input
7195 */
7196 len = xmlExpGetStartInt(ctxt, sub, tab, ctxt->tabSize, 0);
7197 while (len < 0) {
7198 const xmlChar **temp;
Rob Richards54a8f672005-10-07 02:33:00 +00007199 temp = (const xmlChar **) xmlRealloc((xmlChar **) tab, ctxt->tabSize * 2 *
Daniel Veillard81a8ec62005-08-22 00:20:58 +00007200 sizeof(const xmlChar *));
7201 if (temp == NULL) {
Rob Richards54a8f672005-10-07 02:33:00 +00007202 xmlFree((xmlChar **) tab);
Daniel Veillard81a8ec62005-08-22 00:20:58 +00007203 return(NULL);
7204 }
7205 tab = temp;
7206 ctxt->tabSize *= 2;
7207 len = xmlExpGetStartInt(ctxt, sub, tab, ctxt->tabSize, 0);
7208 }
Daniel Veillard81a8ec62005-08-22 00:20:58 +00007209 for (i = 0;i < len;i++) {
7210 tmp = xmlExpStringDeriveInt(ctxt, exp, tab[i]);
7211 if ((tmp == NULL) || (tmp == forbiddenExp)) {
7212 xmlExpFree(ctxt, ret);
Rob Richards54a8f672005-10-07 02:33:00 +00007213 xmlFree((xmlChar **) tab);
Daniel Veillard81a8ec62005-08-22 00:20:58 +00007214 return(tmp);
7215 }
7216 tmp2 = xmlExpStringDeriveInt(ctxt, sub, tab[i]);
7217 if ((tmp2 == NULL) || (tmp2 == forbiddenExp)) {
7218 xmlExpFree(ctxt, tmp);
7219 xmlExpFree(ctxt, ret);
Rob Richards54a8f672005-10-07 02:33:00 +00007220 xmlFree((xmlChar **) tab);
Daniel Veillard81a8ec62005-08-22 00:20:58 +00007221 return(tmp);
7222 }
7223 tmp3 = xmlExpExpDeriveInt(ctxt, tmp, tmp2);
7224 xmlExpFree(ctxt, tmp);
7225 xmlExpFree(ctxt, tmp2);
7226
7227 if ((tmp3 == NULL) || (tmp3 == forbiddenExp)) {
7228 xmlExpFree(ctxt, ret);
Rob Richards54a8f672005-10-07 02:33:00 +00007229 xmlFree((xmlChar **) tab);
Daniel Veillard81a8ec62005-08-22 00:20:58 +00007230 return(tmp3);
7231 }
7232
7233 if (ret == NULL)
7234 ret = tmp3;
7235 else {
7236 ret = xmlExpHashGetEntry(ctxt, XML_EXP_OR, ret, tmp3, NULL, 0, 0);
7237 if (ret == NULL) {
Rob Richards54a8f672005-10-07 02:33:00 +00007238 xmlFree((xmlChar **) tab);
Daniel Veillard81a8ec62005-08-22 00:20:58 +00007239 return(NULL);
7240 }
7241 }
7242 }
Rob Richards54a8f672005-10-07 02:33:00 +00007243 xmlFree((xmlChar **) tab);
Daniel Veillard81a8ec62005-08-22 00:20:58 +00007244 return(ret);
7245}
7246
7247/**
Daniel Veillard0090bd52005-08-22 14:43:43 +00007248 * xmlExpExpDerive:
7249 * @ctxt: the expressions context
7250 * @exp: the englobing expression
7251 * @sub: the subexpression
7252 *
7253 * Evaluates the expression resulting from @exp consuming a sub expression @sub
7254 * Based on algebraic derivation and sometimes direct Brzozowski derivation
7255 * it usually tatkes less than linear time and can handle expressions generating
7256 * infinite languages.
7257 *
7258 * Returns the resulting expression or NULL in case of internal error, the
7259 * result must be freed
7260 */
7261xmlExpNodePtr
7262xmlExpExpDerive(xmlExpCtxtPtr ctxt, xmlExpNodePtr exp, xmlExpNodePtr sub) {
7263 if ((exp == NULL) || (ctxt == NULL) || (sub == NULL))
7264 return(NULL);
7265
7266 /*
7267 * O(1) speedups
7268 */
7269 if (IS_NILLABLE(sub) && (!IS_NILLABLE(exp))) {
7270#ifdef DEBUG_DERIV
7271 printf("Sub nillable and not exp : can't subsume\n");
7272#endif
7273 return(forbiddenExp);
7274 }
7275 if (xmlExpCheckCard(exp, sub) == 0) {
7276#ifdef DEBUG_DERIV
7277 printf("sub generate longuer sequances than exp : can't subsume\n");
7278#endif
7279 return(forbiddenExp);
7280 }
7281 return(xmlExpExpDeriveInt(ctxt, exp, sub));
7282}
7283
7284/**
Daniel Veillard81a8ec62005-08-22 00:20:58 +00007285 * xmlExpSubsume:
7286 * @ctxt: the expressions context
7287 * @exp: the englobing expression
7288 * @sub: the subexpression
7289 *
7290 * Check whether @exp accepts all the languages accexpted by @sub
7291 * the input being a subexpression.
7292 *
7293 * Returns 1 if true 0 if false and -1 in case of failure.
7294 */
7295int
7296xmlExpSubsume(xmlExpCtxtPtr ctxt, xmlExpNodePtr exp, xmlExpNodePtr sub) {
7297 xmlExpNodePtr tmp;
7298
7299 if ((exp == NULL) || (ctxt == NULL) || (sub == NULL))
7300 return(-1);
7301
7302 /*
7303 * TODO: speedup by checking the language of sub is a subset of the
7304 * language of exp
7305 */
7306 /*
7307 * O(1) speedups
7308 */
7309 if (IS_NILLABLE(sub) && (!IS_NILLABLE(exp))) {
7310#ifdef DEBUG_DERIV
7311 printf("Sub nillable and not exp : can't subsume\n");
7312#endif
7313 return(0);
7314 }
7315 if (xmlExpCheckCard(exp, sub) == 0) {
7316#ifdef DEBUG_DERIV
7317 printf("sub generate longuer sequances than exp : can't subsume\n");
7318#endif
7319 return(0);
7320 }
7321 tmp = xmlExpExpDeriveInt(ctxt, exp, sub);
7322#ifdef DEBUG_DERIV
7323 printf("Result derivation :\n");
7324 PRINT_EXP(tmp);
7325#endif
7326 if (tmp == NULL)
7327 return(-1);
7328 if (tmp == forbiddenExp)
7329 return(0);
7330 if (tmp == emptyExp)
7331 return(1);
7332 if ((tmp != NULL) && (IS_NILLABLE(tmp))) {
7333 xmlExpFree(ctxt, tmp);
7334 return(1);
7335 }
7336 xmlExpFree(ctxt, tmp);
7337 return(0);
7338}
Daniel Veillard465a0002005-08-22 12:07:04 +00007339
7340/************************************************************************
7341 * *
7342 * Parsing expression *
7343 * *
7344 ************************************************************************/
7345
7346static xmlExpNodePtr xmlExpParseExpr(xmlExpCtxtPtr ctxt);
7347
7348#undef CUR
7349#define CUR (*ctxt->cur)
7350#undef NEXT
7351#define NEXT ctxt->cur++;
7352#undef IS_BLANK
7353#define IS_BLANK(c) ((c == ' ') || (c == '\n') || (c == '\r') || (c == '\t'))
7354#define SKIP_BLANKS while (IS_BLANK(*ctxt->cur)) ctxt->cur++;
7355
7356static int
7357xmlExpParseNumber(xmlExpCtxtPtr ctxt) {
7358 int ret = 0;
7359
7360 SKIP_BLANKS
7361 if (CUR == '*') {
7362 NEXT
7363 return(-1);
7364 }
7365 if ((CUR < '0') || (CUR > '9'))
7366 return(-1);
7367 while ((CUR >= '0') && (CUR <= '9')) {
7368 ret = ret * 10 + (CUR - '0');
7369 NEXT
7370 }
7371 return(ret);
7372}
7373
7374static xmlExpNodePtr
7375xmlExpParseOr(xmlExpCtxtPtr ctxt) {
7376 const char *base;
7377 xmlExpNodePtr ret;
7378 const xmlChar *val;
7379
7380 SKIP_BLANKS
7381 base = ctxt->cur;
7382 if (*ctxt->cur == '(') {
7383 NEXT
7384 ret = xmlExpParseExpr(ctxt);
7385 SKIP_BLANKS
7386 if (*ctxt->cur != ')') {
7387 fprintf(stderr, "unbalanced '(' : %s\n", base);
7388 xmlExpFree(ctxt, ret);
7389 return(NULL);
7390 }
7391 NEXT;
7392 SKIP_BLANKS
7393 goto parse_quantifier;
7394 }
7395 while ((CUR != 0) && (!(IS_BLANK(CUR))) && (CUR != '(') &&
7396 (CUR != ')') && (CUR != '|') && (CUR != ',') && (CUR != '{') &&
7397 (CUR != '*') && (CUR != '+') && (CUR != '?') && (CUR != '}'))
7398 NEXT;
7399 val = xmlDictLookup(ctxt->dict, BAD_CAST base, ctxt->cur - base);
7400 if (val == NULL)
7401 return(NULL);
7402 ret = xmlExpHashGetEntry(ctxt, XML_EXP_ATOM, NULL, NULL, val, 0, 0);
7403 if (ret == NULL)
7404 return(NULL);
7405 SKIP_BLANKS
7406parse_quantifier:
7407 if (CUR == '{') {
7408 int min, max;
7409
7410 NEXT
7411 min = xmlExpParseNumber(ctxt);
7412 if (min < 0) {
7413 xmlExpFree(ctxt, ret);
7414 return(NULL);
7415 }
7416 SKIP_BLANKS
7417 if (CUR == ',') {
7418 NEXT
7419 max = xmlExpParseNumber(ctxt);
7420 SKIP_BLANKS
7421 } else
7422 max = min;
7423 if (CUR != '}') {
7424 xmlExpFree(ctxt, ret);
7425 return(NULL);
7426 }
7427 NEXT
7428 ret = xmlExpHashGetEntry(ctxt, XML_EXP_COUNT, ret, NULL, NULL,
7429 min, max);
7430 SKIP_BLANKS
7431 } else if (CUR == '?') {
7432 NEXT
7433 ret = xmlExpHashGetEntry(ctxt, XML_EXP_COUNT, ret, NULL, NULL,
7434 0, 1);
7435 SKIP_BLANKS
7436 } else if (CUR == '+') {
7437 NEXT
7438 ret = xmlExpHashGetEntry(ctxt, XML_EXP_COUNT, ret, NULL, NULL,
7439 1, -1);
7440 SKIP_BLANKS
7441 } else if (CUR == '*') {
7442 NEXT
7443 ret = xmlExpHashGetEntry(ctxt, XML_EXP_COUNT, ret, NULL, NULL,
7444 0, -1);
7445 SKIP_BLANKS
7446 }
7447 return(ret);
7448}
7449
7450
7451static xmlExpNodePtr
7452xmlExpParseSeq(xmlExpCtxtPtr ctxt) {
7453 xmlExpNodePtr ret, right;
7454
7455 ret = xmlExpParseOr(ctxt);
7456 SKIP_BLANKS
7457 while (CUR == '|') {
7458 NEXT
7459 right = xmlExpParseOr(ctxt);
7460 if (right == NULL) {
7461 xmlExpFree(ctxt, ret);
7462 return(NULL);
7463 }
7464 ret = xmlExpHashGetEntry(ctxt, XML_EXP_OR, ret, right, NULL, 0, 0);
7465 if (ret == NULL)
7466 return(NULL);
7467 }
7468 return(ret);
7469}
7470
7471static xmlExpNodePtr
7472xmlExpParseExpr(xmlExpCtxtPtr ctxt) {
7473 xmlExpNodePtr ret, right;
7474
7475 ret = xmlExpParseSeq(ctxt);
7476 SKIP_BLANKS
7477 while (CUR == ',') {
7478 NEXT
7479 right = xmlExpParseSeq(ctxt);
7480 if (right == NULL) {
7481 xmlExpFree(ctxt, ret);
7482 return(NULL);
7483 }
7484 ret = xmlExpHashGetEntry(ctxt, XML_EXP_SEQ, ret, right, NULL, 0, 0);
7485 if (ret == NULL)
7486 return(NULL);
7487 }
7488 return(ret);
7489}
7490
7491/**
7492 * xmlExpParse:
7493 * @ctxt: the expressions context
7494 * @expr: the 0 terminated string
7495 *
7496 * Minimal parser for regexps, it understand the following constructs
7497 * - string terminals
7498 * - choice operator |
7499 * - sequence operator ,
7500 * - subexpressions (...)
7501 * - usual cardinality operators + * and ?
7502 * - finite sequences { min, max }
7503 * - infinite sequences { min, * }
7504 * There is minimal checkings made especially no checking on strings values
7505 *
7506 * Returns a new expression or NULL in case of failure
7507 */
7508xmlExpNodePtr
7509xmlExpParse(xmlExpCtxtPtr ctxt, const char *expr) {
7510 xmlExpNodePtr ret;
7511
7512 ctxt->expr = expr;
7513 ctxt->cur = expr;
7514
7515 ret = xmlExpParseExpr(ctxt);
7516 SKIP_BLANKS
7517 if (*ctxt->cur != 0) {
7518 xmlExpFree(ctxt, ret);
7519 return(NULL);
7520 }
7521 return(ret);
7522}
7523
7524static void
7525xmlExpDumpInt(xmlBufferPtr buf, xmlExpNodePtr expr, int glob) {
7526 xmlExpNodePtr c;
7527
7528 if (expr == NULL) return;
7529 if (glob) xmlBufferWriteChar(buf, "(");
7530 switch (expr->type) {
7531 case XML_EXP_EMPTY:
7532 xmlBufferWriteChar(buf, "empty");
7533 break;
7534 case XML_EXP_FORBID:
7535 xmlBufferWriteChar(buf, "forbidden");
7536 break;
7537 case XML_EXP_ATOM:
7538 xmlBufferWriteCHAR(buf, expr->exp_str);
7539 break;
7540 case XML_EXP_SEQ:
7541 c = expr->exp_left;
7542 if ((c->type == XML_EXP_SEQ) || (c->type == XML_EXP_OR))
7543 xmlExpDumpInt(buf, c, 1);
7544 else
7545 xmlExpDumpInt(buf, c, 0);
7546 xmlBufferWriteChar(buf, " , ");
7547 c = expr->exp_right;
7548 if ((c->type == XML_EXP_SEQ) || (c->type == XML_EXP_OR))
7549 xmlExpDumpInt(buf, c, 1);
7550 else
7551 xmlExpDumpInt(buf, c, 0);
7552 break;
7553 case XML_EXP_OR:
7554 c = expr->exp_left;
7555 if ((c->type == XML_EXP_SEQ) || (c->type == XML_EXP_OR))
7556 xmlExpDumpInt(buf, c, 1);
7557 else
7558 xmlExpDumpInt(buf, c, 0);
7559 xmlBufferWriteChar(buf, " | ");
7560 c = expr->exp_right;
7561 if ((c->type == XML_EXP_SEQ) || (c->type == XML_EXP_OR))
7562 xmlExpDumpInt(buf, c, 1);
7563 else
7564 xmlExpDumpInt(buf, c, 0);
7565 break;
7566 case XML_EXP_COUNT: {
7567 char rep[40];
7568
7569 c = expr->exp_left;
7570 if ((c->type == XML_EXP_SEQ) || (c->type == XML_EXP_OR))
7571 xmlExpDumpInt(buf, c, 1);
7572 else
7573 xmlExpDumpInt(buf, c, 0);
7574 if ((expr->exp_min == 0) && (expr->exp_max == 1)) {
7575 rep[0] = '?';
7576 rep[1] = 0;
7577 } else if ((expr->exp_min == 0) && (expr->exp_max == -1)) {
7578 rep[0] = '*';
7579 rep[1] = 0;
7580 } else if ((expr->exp_min == 1) && (expr->exp_max == -1)) {
7581 rep[0] = '+';
7582 rep[1] = 0;
7583 } else if (expr->exp_max == expr->exp_min) {
7584 snprintf(rep, 39, "{%d}", expr->exp_min);
7585 } else if (expr->exp_max < 0) {
7586 snprintf(rep, 39, "{%d,inf}", expr->exp_min);
7587 } else {
7588 snprintf(rep, 39, "{%d,%d}", expr->exp_min, expr->exp_max);
7589 }
7590 rep[39] = 0;
7591 xmlBufferWriteChar(buf, rep);
7592 break;
7593 }
7594 default:
7595 fprintf(stderr, "Error in tree\n");
7596 }
7597 if (glob)
7598 xmlBufferWriteChar(buf, ")");
7599}
7600/**
7601 * xmlExpDump:
7602 * @buf: a buffer to receive the output
7603 * @expr: the compiled expression
7604 *
7605 * Serialize the expression as compiled to the buffer
7606 */
7607void
Daniel Veillard5eee7672005-08-22 21:22:27 +00007608xmlExpDump(xmlBufferPtr buf, xmlExpNodePtr expr) {
7609 if ((buf == NULL) || (expr == NULL))
Daniel Veillard465a0002005-08-22 12:07:04 +00007610 return;
Daniel Veillard5eee7672005-08-22 21:22:27 +00007611 xmlExpDumpInt(buf, expr, 0);
Daniel Veillard465a0002005-08-22 12:07:04 +00007612}
7613
7614/**
7615 * xmlExpMaxToken:
7616 * @expr: a compiled expression
7617 *
7618 * Indicate the maximum number of input a expression can accept
7619 *
7620 * Returns the maximum length or -1 in case of error
7621 */
7622int
7623xmlExpMaxToken(xmlExpNodePtr expr) {
7624 if (expr == NULL)
7625 return(-1);
7626 return(expr->c_max);
7627}
7628
7629/**
7630 * xmlExpCtxtNbNodes:
7631 * @ctxt: an expression context
7632 *
7633 * Debugging facility provides the number of allocated nodes at a that point
7634 *
7635 * Returns the number of nodes in use or -1 in case of error
7636 */
7637int
7638xmlExpCtxtNbNodes(xmlExpCtxtPtr ctxt) {
7639 if (ctxt == NULL)
7640 return(-1);
7641 return(ctxt->nb_nodes);
7642}
7643
7644/**
7645 * xmlExpCtxtNbCons:
7646 * @ctxt: an expression context
7647 *
7648 * Debugging facility provides the number of allocated nodes over lifetime
7649 *
7650 * Returns the number of nodes ever allocated or -1 in case of error
7651 */
7652int
7653xmlExpCtxtNbCons(xmlExpCtxtPtr ctxt) {
7654 if (ctxt == NULL)
7655 return(-1);
7656 return(ctxt->nb_cons);
7657}
7658
Daniel Veillard81a8ec62005-08-22 00:20:58 +00007659#endif /* LIBXML_EXPR_ENABLED */
Daniel Veillard5d4644e2005-04-01 13:11:58 +00007660#define bottom_xmlregexp
7661#include "elfgcchack.h"
Daniel Veillard4255d502002-04-16 15:50:10 +00007662#endif /* LIBXML_REGEXP_ENABLED */