blob: fbd0e9f3b1b2ed119a31a771691f543f25e1e199 [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
Daniel Veillardfc011b72006-02-12 19:14:15 +000074/*
75 * Note: the order of the enums below is significant, do not shuffle
76 */
Daniel Veillard4255d502002-04-16 15:50:10 +000077typedef enum {
78 XML_REGEXP_EPSILON = 1,
79 XML_REGEXP_CHARVAL,
80 XML_REGEXP_RANGES,
Daniel Veillard567a45b2005-10-18 19:11:55 +000081 XML_REGEXP_SUBREG, /* used for () sub regexps */
Daniel Veillard4255d502002-04-16 15:50:10 +000082 XML_REGEXP_STRING,
83 XML_REGEXP_ANYCHAR, /* . */
84 XML_REGEXP_ANYSPACE, /* \s */
85 XML_REGEXP_NOTSPACE, /* \S */
86 XML_REGEXP_INITNAME, /* \l */
Daniel Veillard567a45b2005-10-18 19:11:55 +000087 XML_REGEXP_NOTINITNAME, /* \L */
Daniel Veillard4255d502002-04-16 15:50:10 +000088 XML_REGEXP_NAMECHAR, /* \c */
89 XML_REGEXP_NOTNAMECHAR, /* \C */
90 XML_REGEXP_DECIMAL, /* \d */
Daniel Veillard567a45b2005-10-18 19:11:55 +000091 XML_REGEXP_NOTDECIMAL, /* \D */
Daniel Veillard4255d502002-04-16 15:50:10 +000092 XML_REGEXP_REALCHAR, /* \w */
Daniel Veillard567a45b2005-10-18 19:11:55 +000093 XML_REGEXP_NOTREALCHAR, /* \W */
94 XML_REGEXP_LETTER = 100,
Daniel Veillard4255d502002-04-16 15:50:10 +000095 XML_REGEXP_LETTER_UPPERCASE,
96 XML_REGEXP_LETTER_LOWERCASE,
97 XML_REGEXP_LETTER_TITLECASE,
98 XML_REGEXP_LETTER_MODIFIER,
99 XML_REGEXP_LETTER_OTHERS,
100 XML_REGEXP_MARK,
101 XML_REGEXP_MARK_NONSPACING,
102 XML_REGEXP_MARK_SPACECOMBINING,
103 XML_REGEXP_MARK_ENCLOSING,
104 XML_REGEXP_NUMBER,
105 XML_REGEXP_NUMBER_DECIMAL,
106 XML_REGEXP_NUMBER_LETTER,
107 XML_REGEXP_NUMBER_OTHERS,
108 XML_REGEXP_PUNCT,
109 XML_REGEXP_PUNCT_CONNECTOR,
110 XML_REGEXP_PUNCT_DASH,
111 XML_REGEXP_PUNCT_OPEN,
112 XML_REGEXP_PUNCT_CLOSE,
113 XML_REGEXP_PUNCT_INITQUOTE,
114 XML_REGEXP_PUNCT_FINQUOTE,
115 XML_REGEXP_PUNCT_OTHERS,
116 XML_REGEXP_SEPAR,
117 XML_REGEXP_SEPAR_SPACE,
118 XML_REGEXP_SEPAR_LINE,
119 XML_REGEXP_SEPAR_PARA,
120 XML_REGEXP_SYMBOL,
121 XML_REGEXP_SYMBOL_MATH,
122 XML_REGEXP_SYMBOL_CURRENCY,
123 XML_REGEXP_SYMBOL_MODIFIER,
124 XML_REGEXP_SYMBOL_OTHERS,
125 XML_REGEXP_OTHER,
126 XML_REGEXP_OTHER_CONTROL,
127 XML_REGEXP_OTHER_FORMAT,
128 XML_REGEXP_OTHER_PRIVATE,
129 XML_REGEXP_OTHER_NA,
130 XML_REGEXP_BLOCK_NAME
131} xmlRegAtomType;
132
133typedef enum {
134 XML_REGEXP_QUANT_EPSILON = 1,
135 XML_REGEXP_QUANT_ONCE,
136 XML_REGEXP_QUANT_OPT,
137 XML_REGEXP_QUANT_MULT,
138 XML_REGEXP_QUANT_PLUS,
Daniel Veillard7646b182002-04-20 06:41:40 +0000139 XML_REGEXP_QUANT_ONCEONLY,
140 XML_REGEXP_QUANT_ALL,
Daniel Veillard4255d502002-04-16 15:50:10 +0000141 XML_REGEXP_QUANT_RANGE
142} xmlRegQuantType;
143
144typedef enum {
145 XML_REGEXP_START_STATE = 1,
146 XML_REGEXP_FINAL_STATE,
Daniel Veillardcc026dc2005-01-12 13:21:17 +0000147 XML_REGEXP_TRANS_STATE,
Daniel Veillard0e05f4c2006-11-01 15:33:04 +0000148 XML_REGEXP_SINK_STATE,
149 XML_REGEXP_UNREACH_STATE
Daniel Veillard4255d502002-04-16 15:50:10 +0000150} xmlRegStateType;
151
152typedef enum {
153 XML_REGEXP_MARK_NORMAL = 0,
154 XML_REGEXP_MARK_START,
155 XML_REGEXP_MARK_VISITED
156} xmlRegMarkedType;
157
158typedef struct _xmlRegRange xmlRegRange;
159typedef xmlRegRange *xmlRegRangePtr;
160
161struct _xmlRegRange {
Daniel Veillardf8b9de32003-11-24 14:27:26 +0000162 int neg; /* 0 normal, 1 not, 2 exclude */
Daniel Veillard4255d502002-04-16 15:50:10 +0000163 xmlRegAtomType type;
164 int start;
165 int end;
166 xmlChar *blockName;
167};
168
169typedef struct _xmlRegAtom xmlRegAtom;
170typedef xmlRegAtom *xmlRegAtomPtr;
171
172typedef struct _xmlAutomataState xmlRegState;
173typedef xmlRegState *xmlRegStatePtr;
174
175struct _xmlRegAtom {
176 int no;
177 xmlRegAtomType type;
178 xmlRegQuantType quant;
179 int min;
180 int max;
181
182 void *valuep;
Daniel Veillarda646cfd2002-09-17 21:50:03 +0000183 void *valuep2;
Daniel Veillard4255d502002-04-16 15:50:10 +0000184 int neg;
185 int codepoint;
186 xmlRegStatePtr start;
187 xmlRegStatePtr stop;
188 int maxRanges;
189 int nbRanges;
190 xmlRegRangePtr *ranges;
191 void *data;
192};
193
194typedef struct _xmlRegCounter xmlRegCounter;
195typedef xmlRegCounter *xmlRegCounterPtr;
196
197struct _xmlRegCounter {
198 int min;
199 int max;
200};
201
202typedef struct _xmlRegTrans xmlRegTrans;
203typedef xmlRegTrans *xmlRegTransPtr;
204
205struct _xmlRegTrans {
206 xmlRegAtomPtr atom;
207 int to;
208 int counter;
209 int count;
Daniel Veillard567a45b2005-10-18 19:11:55 +0000210 int nd;
Daniel Veillard4255d502002-04-16 15:50:10 +0000211};
212
213struct _xmlAutomataState {
214 xmlRegStateType type;
215 xmlRegMarkedType mark;
Daniel Veillard23e73572002-09-19 19:56:43 +0000216 xmlRegMarkedType reached;
Daniel Veillard4255d502002-04-16 15:50:10 +0000217 int no;
Daniel Veillard4255d502002-04-16 15:50:10 +0000218 int maxTrans;
219 int nbTrans;
220 xmlRegTrans *trans;
Daniel Veillarddb68b742005-07-30 13:18:24 +0000221 /* knowing states ponting to us can speed things up */
222 int maxTransTo;
223 int nbTransTo;
224 int *transTo;
Daniel Veillard4255d502002-04-16 15:50:10 +0000225};
226
227typedef struct _xmlAutomata xmlRegParserCtxt;
228typedef xmlRegParserCtxt *xmlRegParserCtxtPtr;
229
230struct _xmlAutomata {
231 xmlChar *string;
232 xmlChar *cur;
233
234 int error;
235 int neg;
236
237 xmlRegStatePtr start;
238 xmlRegStatePtr end;
239 xmlRegStatePtr state;
240
241 xmlRegAtomPtr atom;
242
243 int maxAtoms;
244 int nbAtoms;
245 xmlRegAtomPtr *atoms;
246
247 int maxStates;
248 int nbStates;
249 xmlRegStatePtr *states;
250
251 int maxCounters;
252 int nbCounters;
253 xmlRegCounter *counters;
Daniel Veillarde19fc232002-04-22 16:01:24 +0000254
255 int determinist;
Daniel Veillard6e65e152005-08-09 11:09:52 +0000256 int negs;
Daniel Veillard4255d502002-04-16 15:50:10 +0000257};
258
259struct _xmlRegexp {
260 xmlChar *string;
261 int nbStates;
262 xmlRegStatePtr *states;
263 int nbAtoms;
264 xmlRegAtomPtr *atoms;
265 int nbCounters;
266 xmlRegCounter *counters;
Daniel Veillarde19fc232002-04-22 16:01:24 +0000267 int determinist;
Daniel Veillard23e73572002-09-19 19:56:43 +0000268 /*
269 * That's the compact form for determinists automatas
270 */
271 int nbstates;
272 int *compact;
Daniel Veillard118aed72002-09-24 14:13:13 +0000273 void **transdata;
Daniel Veillard23e73572002-09-19 19:56:43 +0000274 int nbstrings;
275 xmlChar **stringMap;
Daniel Veillard4255d502002-04-16 15:50:10 +0000276};
277
278typedef struct _xmlRegExecRollback xmlRegExecRollback;
279typedef xmlRegExecRollback *xmlRegExecRollbackPtr;
280
281struct _xmlRegExecRollback {
282 xmlRegStatePtr state;/* the current state */
283 int index; /* the index in the input stack */
284 int nextbranch; /* the next transition to explore in that state */
William M. Brackddf71d62004-05-06 04:17:26 +0000285 int *counts; /* save the automata state if it has some */
Daniel Veillard4255d502002-04-16 15:50:10 +0000286};
287
288typedef struct _xmlRegInputToken xmlRegInputToken;
289typedef xmlRegInputToken *xmlRegInputTokenPtr;
290
291struct _xmlRegInputToken {
292 xmlChar *value;
293 void *data;
294};
295
296struct _xmlRegExecCtxt {
297 int status; /* execution status != 0 indicate an error */
William M. Brackddf71d62004-05-06 04:17:26 +0000298 int determinist; /* did we find an indeterministic behaviour */
Daniel Veillard4255d502002-04-16 15:50:10 +0000299 xmlRegexpPtr comp; /* the compiled regexp */
300 xmlRegExecCallbacks callback;
301 void *data;
302
303 xmlRegStatePtr state;/* the current state */
304 int transno; /* the current transition on that state */
William M. Brackddf71d62004-05-06 04:17:26 +0000305 int transcount; /* the number of chars in char counted transitions */
Daniel Veillard4255d502002-04-16 15:50:10 +0000306
307 /*
308 * A stack of rollback states
309 */
310 int maxRollbacks;
311 int nbRollbacks;
312 xmlRegExecRollback *rollbacks;
313
314 /*
315 * The state of the automata if any
316 */
317 int *counts;
318
319 /*
320 * The input stack
321 */
322 int inputStackMax;
323 int inputStackNr;
324 int index;
325 int *charStack;
326 const xmlChar *inputString; /* when operating on characters */
327 xmlRegInputTokenPtr inputStack;/* when operating on strings */
328
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +0000329 /*
330 * error handling
331 */
332 int errStateNo; /* the error state number */
333 xmlRegStatePtr errState; /* the error state */
334 xmlChar *errString; /* the string raising the error */
335 int *errCounts; /* counters at the error state */
Daniel Veillard94cc1032005-09-15 13:09:00 +0000336 int nbPush;
Daniel Veillard4255d502002-04-16 15:50:10 +0000337};
338
Daniel Veillard441bc322002-04-20 17:38:48 +0000339#define REGEXP_ALL_COUNTER 0x123456
340#define REGEXP_ALL_LAX_COUNTER 0x123457
Daniel Veillard7646b182002-04-20 06:41:40 +0000341
Daniel Veillard4255d502002-04-16 15:50:10 +0000342static void xmlFAParseRegExp(xmlRegParserCtxtPtr ctxt, int top);
Daniel Veillard23e73572002-09-19 19:56:43 +0000343static void xmlRegFreeState(xmlRegStatePtr state);
344static void xmlRegFreeAtom(xmlRegAtomPtr atom);
Daniel Veillard9efc4762005-07-19 14:33:55 +0000345static int xmlRegStrEqualWildcard(const xmlChar *expStr, const xmlChar *valStr);
Daniel Veillard567a45b2005-10-18 19:11:55 +0000346static int xmlRegCheckCharacter(xmlRegAtomPtr atom, int codepoint);
347static int xmlRegCheckCharacterRange(xmlRegAtomType type, int codepoint,
348 int neg, int start, int end, const xmlChar *blockName);
Daniel Veillard4255d502002-04-16 15:50:10 +0000349
350/************************************************************************
Daniel Veillardff46a042003-10-08 08:53:17 +0000351 * *
352 * Regexp memory error handler *
353 * *
354 ************************************************************************/
355/**
356 * xmlRegexpErrMemory:
William M. Brackddf71d62004-05-06 04:17:26 +0000357 * @extra: extra information
Daniel Veillardff46a042003-10-08 08:53:17 +0000358 *
359 * Handle an out of memory condition
360 */
361static void
362xmlRegexpErrMemory(xmlRegParserCtxtPtr ctxt, const char *extra)
363{
364 const char *regexp = NULL;
365 if (ctxt != NULL) {
366 regexp = (const char *) ctxt->string;
367 ctxt->error = XML_ERR_NO_MEMORY;
368 }
Daniel Veillard659e71e2003-10-10 14:10:40 +0000369 __xmlRaiseError(NULL, NULL, NULL, NULL, NULL, XML_FROM_REGEXP,
Daniel Veillardff46a042003-10-08 08:53:17 +0000370 XML_ERR_NO_MEMORY, XML_ERR_FATAL, NULL, 0, extra,
371 regexp, NULL, 0, 0,
372 "Memory allocation failed : %s\n", extra);
373}
374
375/**
376 * xmlRegexpErrCompile:
William M. Brackddf71d62004-05-06 04:17:26 +0000377 * @extra: extra information
Daniel Veillardff46a042003-10-08 08:53:17 +0000378 *
William M. Brackddf71d62004-05-06 04:17:26 +0000379 * Handle a compilation failure
Daniel Veillardff46a042003-10-08 08:53:17 +0000380 */
381static void
382xmlRegexpErrCompile(xmlRegParserCtxtPtr ctxt, const char *extra)
383{
384 const char *regexp = NULL;
385 int idx = 0;
386
387 if (ctxt != NULL) {
388 regexp = (const char *) ctxt->string;
389 idx = ctxt->cur - ctxt->string;
390 ctxt->error = XML_REGEXP_COMPILE_ERROR;
391 }
Daniel Veillard659e71e2003-10-10 14:10:40 +0000392 __xmlRaiseError(NULL, NULL, NULL, NULL, NULL, XML_FROM_REGEXP,
Daniel Veillardff46a042003-10-08 08:53:17 +0000393 XML_REGEXP_COMPILE_ERROR, XML_ERR_FATAL, NULL, 0, extra,
394 regexp, NULL, idx, 0,
395 "failed to compile: %s\n", extra);
396}
397
398/************************************************************************
Daniel Veillard4255d502002-04-16 15:50:10 +0000399 * *
400 * Allocation/Deallocation *
401 * *
402 ************************************************************************/
403
Daniel Veillard23e73572002-09-19 19:56:43 +0000404static int xmlFAComputesDeterminism(xmlRegParserCtxtPtr ctxt);
Daniel Veillard4255d502002-04-16 15:50:10 +0000405/**
406 * xmlRegEpxFromParse:
407 * @ctxt: the parser context used to build it
408 *
William M. Brackddf71d62004-05-06 04:17:26 +0000409 * Allocate a new regexp and fill it with the result from the parser
Daniel Veillard4255d502002-04-16 15:50:10 +0000410 *
411 * Returns the new regexp or NULL in case of error
412 */
413static xmlRegexpPtr
414xmlRegEpxFromParse(xmlRegParserCtxtPtr ctxt) {
415 xmlRegexpPtr ret;
416
417 ret = (xmlRegexpPtr) xmlMalloc(sizeof(xmlRegexp));
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000418 if (ret == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +0000419 xmlRegexpErrMemory(ctxt, "compiling regexp");
Daniel Veillard4255d502002-04-16 15:50:10 +0000420 return(NULL);
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000421 }
Daniel Veillard4255d502002-04-16 15:50:10 +0000422 memset(ret, 0, sizeof(xmlRegexp));
423 ret->string = ctxt->string;
Daniel Veillard4255d502002-04-16 15:50:10 +0000424 ret->nbStates = ctxt->nbStates;
Daniel Veillard4255d502002-04-16 15:50:10 +0000425 ret->states = ctxt->states;
Daniel Veillard4255d502002-04-16 15:50:10 +0000426 ret->nbAtoms = ctxt->nbAtoms;
Daniel Veillard4255d502002-04-16 15:50:10 +0000427 ret->atoms = ctxt->atoms;
Daniel Veillard4255d502002-04-16 15:50:10 +0000428 ret->nbCounters = ctxt->nbCounters;
Daniel Veillard4255d502002-04-16 15:50:10 +0000429 ret->counters = ctxt->counters;
Daniel Veillarde19fc232002-04-22 16:01:24 +0000430 ret->determinist = ctxt->determinist;
Daniel Veillard567a45b2005-10-18 19:11:55 +0000431 if (ret->determinist == -1) {
432 xmlRegexpIsDeterminist(ret);
433 }
Daniel Veillard23e73572002-09-19 19:56:43 +0000434
435 if ((ret->determinist != 0) &&
436 (ret->nbCounters == 0) &&
Daniel Veillard6e65e152005-08-09 11:09:52 +0000437 (ctxt->negs == 0) &&
Daniel Veillard118aed72002-09-24 14:13:13 +0000438 (ret->atoms != NULL) &&
Daniel Veillard23e73572002-09-19 19:56:43 +0000439 (ret->atoms[0] != NULL) &&
440 (ret->atoms[0]->type == XML_REGEXP_STRING)) {
441 int i, j, nbstates = 0, nbatoms = 0;
442 int *stateRemap;
443 int *stringRemap;
444 int *transitions;
Daniel Veillard118aed72002-09-24 14:13:13 +0000445 void **transdata;
Daniel Veillard23e73572002-09-19 19:56:43 +0000446 xmlChar **stringMap;
447 xmlChar *value;
448
449 /*
450 * Switch to a compact representation
451 * 1/ counting the effective number of states left
William M. Brackddf71d62004-05-06 04:17:26 +0000452 * 2/ counting the unique number of atoms, and check that
Daniel Veillard23e73572002-09-19 19:56:43 +0000453 * they are all of the string type
454 * 3/ build a table state x atom for the transitions
455 */
456
457 stateRemap = xmlMalloc(ret->nbStates * sizeof(int));
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000458 if (stateRemap == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +0000459 xmlRegexpErrMemory(ctxt, "compiling regexp");
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000460 xmlFree(ret);
461 return(NULL);
462 }
Daniel Veillard23e73572002-09-19 19:56:43 +0000463 for (i = 0;i < ret->nbStates;i++) {
464 if (ret->states[i] != NULL) {
465 stateRemap[i] = nbstates;
466 nbstates++;
467 } else {
468 stateRemap[i] = -1;
469 }
470 }
471#ifdef DEBUG_COMPACTION
472 printf("Final: %d states\n", nbstates);
473#endif
474 stringMap = xmlMalloc(ret->nbAtoms * sizeof(char *));
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000475 if (stringMap == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +0000476 xmlRegexpErrMemory(ctxt, "compiling regexp");
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000477 xmlFree(stateRemap);
478 xmlFree(ret);
479 return(NULL);
480 }
Daniel Veillard23e73572002-09-19 19:56:43 +0000481 stringRemap = xmlMalloc(ret->nbAtoms * sizeof(int));
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000482 if (stringRemap == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +0000483 xmlRegexpErrMemory(ctxt, "compiling regexp");
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000484 xmlFree(stringMap);
485 xmlFree(stateRemap);
486 xmlFree(ret);
487 return(NULL);
488 }
Daniel Veillard23e73572002-09-19 19:56:43 +0000489 for (i = 0;i < ret->nbAtoms;i++) {
490 if ((ret->atoms[i]->type == XML_REGEXP_STRING) &&
491 (ret->atoms[i]->quant == XML_REGEXP_QUANT_ONCE)) {
492 value = ret->atoms[i]->valuep;
493 for (j = 0;j < nbatoms;j++) {
494 if (xmlStrEqual(stringMap[j], value)) {
495 stringRemap[i] = j;
496 break;
497 }
498 }
499 if (j >= nbatoms) {
500 stringRemap[i] = nbatoms;
501 stringMap[nbatoms] = xmlStrdup(value);
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000502 if (stringMap[nbatoms] == NULL) {
503 for (i = 0;i < nbatoms;i++)
504 xmlFree(stringMap[i]);
505 xmlFree(stringRemap);
506 xmlFree(stringMap);
507 xmlFree(stateRemap);
508 xmlFree(ret);
509 return(NULL);
510 }
Daniel Veillard23e73572002-09-19 19:56:43 +0000511 nbatoms++;
512 }
513 } else {
514 xmlFree(stateRemap);
515 xmlFree(stringRemap);
516 for (i = 0;i < nbatoms;i++)
517 xmlFree(stringMap[i]);
518 xmlFree(stringMap);
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000519 xmlFree(ret);
520 return(NULL);
Daniel Veillard23e73572002-09-19 19:56:43 +0000521 }
522 }
523#ifdef DEBUG_COMPACTION
524 printf("Final: %d atoms\n", nbatoms);
525#endif
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000526 transitions = (int *) xmlMalloc((nbstates + 1) *
527 (nbatoms + 1) * sizeof(int));
528 if (transitions == NULL) {
529 xmlFree(stateRemap);
530 xmlFree(stringRemap);
531 xmlFree(stringMap);
532 xmlFree(ret);
533 return(NULL);
534 }
535 memset(transitions, 0, (nbstates + 1) * (nbatoms + 1) * sizeof(int));
Daniel Veillard23e73572002-09-19 19:56:43 +0000536
537 /*
538 * Allocate the transition table. The first entry for each
William M. Brackddf71d62004-05-06 04:17:26 +0000539 * state corresponds to the state type.
Daniel Veillard23e73572002-09-19 19:56:43 +0000540 */
Daniel Veillard118aed72002-09-24 14:13:13 +0000541 transdata = NULL;
Daniel Veillard23e73572002-09-19 19:56:43 +0000542
543 for (i = 0;i < ret->nbStates;i++) {
544 int stateno, atomno, targetno, prev;
545 xmlRegStatePtr state;
546 xmlRegTransPtr trans;
547
548 stateno = stateRemap[i];
549 if (stateno == -1)
550 continue;
551 state = ret->states[i];
552
553 transitions[stateno * (nbatoms + 1)] = state->type;
554
555 for (j = 0;j < state->nbTrans;j++) {
556 trans = &(state->trans[j]);
557 if ((trans->to == -1) || (trans->atom == NULL))
558 continue;
559 atomno = stringRemap[trans->atom->no];
Daniel Veillard118aed72002-09-24 14:13:13 +0000560 if ((trans->atom->data != NULL) && (transdata == NULL)) {
561 transdata = (void **) xmlMalloc(nbstates * nbatoms *
562 sizeof(void *));
563 if (transdata != NULL)
564 memset(transdata, 0,
565 nbstates * nbatoms * sizeof(void *));
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000566 else {
Daniel Veillardff46a042003-10-08 08:53:17 +0000567 xmlRegexpErrMemory(ctxt, "compiling regexp");
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000568 break;
569 }
Daniel Veillard118aed72002-09-24 14:13:13 +0000570 }
Daniel Veillard23e73572002-09-19 19:56:43 +0000571 targetno = stateRemap[trans->to];
572 /*
William M. Brackddf71d62004-05-06 04:17:26 +0000573 * if the same atom can generate transitions to 2 different
Daniel Veillard23e73572002-09-19 19:56:43 +0000574 * states then it means the automata is not determinist and
575 * the compact form can't be used !
576 */
577 prev = transitions[stateno * (nbatoms + 1) + atomno + 1];
578 if (prev != 0) {
579 if (prev != targetno + 1) {
Daniel Veillard23e73572002-09-19 19:56:43 +0000580 ret->determinist = 0;
581#ifdef DEBUG_COMPACTION
582 printf("Indet: state %d trans %d, atom %d to %d : %d to %d\n",
583 i, j, trans->atom->no, trans->to, atomno, targetno);
584 printf(" previous to is %d\n", prev);
585#endif
Daniel Veillard118aed72002-09-24 14:13:13 +0000586 if (transdata != NULL)
587 xmlFree(transdata);
Daniel Veillard23e73572002-09-19 19:56:43 +0000588 xmlFree(transitions);
589 xmlFree(stateRemap);
590 xmlFree(stringRemap);
591 for (i = 0;i < nbatoms;i++)
592 xmlFree(stringMap[i]);
593 xmlFree(stringMap);
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000594 goto not_determ;
Daniel Veillard23e73572002-09-19 19:56:43 +0000595 }
596 } else {
597#if 0
598 printf("State %d trans %d: atom %d to %d : %d to %d\n",
599 i, j, trans->atom->no, trans->to, atomno, targetno);
600#endif
601 transitions[stateno * (nbatoms + 1) + atomno + 1] =
Daniel Veillard118aed72002-09-24 14:13:13 +0000602 targetno + 1; /* to avoid 0 */
603 if (transdata != NULL)
604 transdata[stateno * nbatoms + atomno] =
605 trans->atom->data;
Daniel Veillard23e73572002-09-19 19:56:43 +0000606 }
607 }
608 }
609 ret->determinist = 1;
610#ifdef DEBUG_COMPACTION
611 /*
612 * Debug
613 */
614 for (i = 0;i < nbstates;i++) {
615 for (j = 0;j < nbatoms + 1;j++) {
616 printf("%02d ", transitions[i * (nbatoms + 1) + j]);
617 }
618 printf("\n");
619 }
620 printf("\n");
621#endif
622 /*
623 * Cleanup of the old data
624 */
625 if (ret->states != NULL) {
626 for (i = 0;i < ret->nbStates;i++)
627 xmlRegFreeState(ret->states[i]);
628 xmlFree(ret->states);
629 }
630 ret->states = NULL;
631 ret->nbStates = 0;
632 if (ret->atoms != NULL) {
633 for (i = 0;i < ret->nbAtoms;i++)
634 xmlRegFreeAtom(ret->atoms[i]);
635 xmlFree(ret->atoms);
636 }
637 ret->atoms = NULL;
638 ret->nbAtoms = 0;
639
640 ret->compact = transitions;
Daniel Veillard118aed72002-09-24 14:13:13 +0000641 ret->transdata = transdata;
Daniel Veillard23e73572002-09-19 19:56:43 +0000642 ret->stringMap = stringMap;
643 ret->nbstrings = nbatoms;
644 ret->nbstates = nbstates;
645 xmlFree(stateRemap);
646 xmlFree(stringRemap);
647 }
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000648not_determ:
649 ctxt->string = NULL;
650 ctxt->nbStates = 0;
651 ctxt->states = NULL;
652 ctxt->nbAtoms = 0;
653 ctxt->atoms = NULL;
654 ctxt->nbCounters = 0;
655 ctxt->counters = NULL;
Daniel Veillard4255d502002-04-16 15:50:10 +0000656 return(ret);
657}
658
659/**
660 * xmlRegNewParserCtxt:
661 * @string: the string to parse
662 *
663 * Allocate a new regexp parser context
664 *
665 * Returns the new context or NULL in case of error
666 */
667static xmlRegParserCtxtPtr
668xmlRegNewParserCtxt(const xmlChar *string) {
669 xmlRegParserCtxtPtr ret;
670
671 ret = (xmlRegParserCtxtPtr) xmlMalloc(sizeof(xmlRegParserCtxt));
672 if (ret == NULL)
673 return(NULL);
674 memset(ret, 0, sizeof(xmlRegParserCtxt));
675 if (string != NULL)
676 ret->string = xmlStrdup(string);
677 ret->cur = ret->string;
678 ret->neg = 0;
Daniel Veillard6e65e152005-08-09 11:09:52 +0000679 ret->negs = 0;
Daniel Veillard4255d502002-04-16 15:50:10 +0000680 ret->error = 0;
Daniel Veillarde19fc232002-04-22 16:01:24 +0000681 ret->determinist = -1;
Daniel Veillard4255d502002-04-16 15:50:10 +0000682 return(ret);
683}
684
685/**
686 * xmlRegNewRange:
687 * @ctxt: the regexp parser context
688 * @neg: is that negative
689 * @type: the type of range
690 * @start: the start codepoint
691 * @end: the end codepoint
692 *
693 * Allocate a new regexp range
694 *
695 * Returns the new range or NULL in case of error
696 */
697static xmlRegRangePtr
698xmlRegNewRange(xmlRegParserCtxtPtr ctxt,
699 int neg, xmlRegAtomType type, int start, int end) {
700 xmlRegRangePtr ret;
701
702 ret = (xmlRegRangePtr) xmlMalloc(sizeof(xmlRegRange));
703 if (ret == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +0000704 xmlRegexpErrMemory(ctxt, "allocating range");
Daniel Veillard4255d502002-04-16 15:50:10 +0000705 return(NULL);
706 }
707 ret->neg = neg;
708 ret->type = type;
709 ret->start = start;
710 ret->end = end;
711 return(ret);
712}
713
714/**
715 * xmlRegFreeRange:
716 * @range: the regexp range
717 *
718 * Free a regexp range
719 */
720static void
721xmlRegFreeRange(xmlRegRangePtr range) {
722 if (range == NULL)
723 return;
724
725 if (range->blockName != NULL)
726 xmlFree(range->blockName);
727 xmlFree(range);
728}
729
730/**
731 * xmlRegNewAtom:
732 * @ctxt: the regexp parser context
733 * @type: the type of atom
734 *
735 * Allocate a new regexp range
736 *
737 * Returns the new atom or NULL in case of error
738 */
739static xmlRegAtomPtr
740xmlRegNewAtom(xmlRegParserCtxtPtr ctxt, xmlRegAtomType type) {
741 xmlRegAtomPtr ret;
742
743 ret = (xmlRegAtomPtr) xmlMalloc(sizeof(xmlRegAtom));
744 if (ret == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +0000745 xmlRegexpErrMemory(ctxt, "allocating atom");
Daniel Veillard4255d502002-04-16 15:50:10 +0000746 return(NULL);
747 }
748 memset(ret, 0, sizeof(xmlRegAtom));
749 ret->type = type;
750 ret->quant = XML_REGEXP_QUANT_ONCE;
751 ret->min = 0;
752 ret->max = 0;
753 return(ret);
754}
755
756/**
757 * xmlRegFreeAtom:
758 * @atom: the regexp atom
759 *
760 * Free a regexp atom
761 */
762static void
763xmlRegFreeAtom(xmlRegAtomPtr atom) {
764 int i;
765
766 if (atom == NULL)
767 return;
768
769 for (i = 0;i < atom->nbRanges;i++)
770 xmlRegFreeRange(atom->ranges[i]);
771 if (atom->ranges != NULL)
772 xmlFree(atom->ranges);
Daniel Veillardde0e4982005-07-03 14:35:44 +0000773 if ((atom->type == XML_REGEXP_STRING) && (atom->valuep != NULL))
774 xmlFree(atom->valuep);
Daniel Veillard77005e62005-07-19 16:26:18 +0000775 if ((atom->type == XML_REGEXP_STRING) && (atom->valuep2 != NULL))
776 xmlFree(atom->valuep2);
Daniel Veillardde0e4982005-07-03 14:35:44 +0000777 if ((atom->type == XML_REGEXP_BLOCK_NAME) && (atom->valuep != NULL))
Daniel Veillard4255d502002-04-16 15:50:10 +0000778 xmlFree(atom->valuep);
779 xmlFree(atom);
780}
781
782static xmlRegStatePtr
783xmlRegNewState(xmlRegParserCtxtPtr ctxt) {
784 xmlRegStatePtr ret;
785
786 ret = (xmlRegStatePtr) xmlMalloc(sizeof(xmlRegState));
787 if (ret == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +0000788 xmlRegexpErrMemory(ctxt, "allocating state");
Daniel Veillard4255d502002-04-16 15:50:10 +0000789 return(NULL);
790 }
791 memset(ret, 0, sizeof(xmlRegState));
792 ret->type = XML_REGEXP_TRANS_STATE;
793 ret->mark = XML_REGEXP_MARK_NORMAL;
794 return(ret);
795}
796
797/**
798 * xmlRegFreeState:
799 * @state: the regexp state
800 *
801 * Free a regexp state
802 */
803static void
804xmlRegFreeState(xmlRegStatePtr state) {
805 if (state == NULL)
806 return;
807
808 if (state->trans != NULL)
809 xmlFree(state->trans);
Daniel Veillarddb68b742005-07-30 13:18:24 +0000810 if (state->transTo != NULL)
811 xmlFree(state->transTo);
Daniel Veillard4255d502002-04-16 15:50:10 +0000812 xmlFree(state);
813}
814
815/**
816 * xmlRegFreeParserCtxt:
817 * @ctxt: the regexp parser context
818 *
819 * Free a regexp parser context
820 */
821static void
822xmlRegFreeParserCtxt(xmlRegParserCtxtPtr ctxt) {
823 int i;
824 if (ctxt == NULL)
825 return;
826
827 if (ctxt->string != NULL)
828 xmlFree(ctxt->string);
829 if (ctxt->states != NULL) {
830 for (i = 0;i < ctxt->nbStates;i++)
831 xmlRegFreeState(ctxt->states[i]);
832 xmlFree(ctxt->states);
833 }
834 if (ctxt->atoms != NULL) {
835 for (i = 0;i < ctxt->nbAtoms;i++)
836 xmlRegFreeAtom(ctxt->atoms[i]);
837 xmlFree(ctxt->atoms);
838 }
839 if (ctxt->counters != NULL)
840 xmlFree(ctxt->counters);
841 xmlFree(ctxt);
842}
843
844/************************************************************************
845 * *
846 * Display of Data structures *
847 * *
848 ************************************************************************/
849
850static void
851xmlRegPrintAtomType(FILE *output, xmlRegAtomType type) {
852 switch (type) {
853 case XML_REGEXP_EPSILON:
854 fprintf(output, "epsilon "); break;
855 case XML_REGEXP_CHARVAL:
856 fprintf(output, "charval "); break;
857 case XML_REGEXP_RANGES:
858 fprintf(output, "ranges "); break;
859 case XML_REGEXP_SUBREG:
860 fprintf(output, "subexpr "); break;
861 case XML_REGEXP_STRING:
862 fprintf(output, "string "); break;
863 case XML_REGEXP_ANYCHAR:
864 fprintf(output, "anychar "); break;
865 case XML_REGEXP_ANYSPACE:
866 fprintf(output, "anyspace "); break;
867 case XML_REGEXP_NOTSPACE:
868 fprintf(output, "notspace "); break;
869 case XML_REGEXP_INITNAME:
870 fprintf(output, "initname "); break;
871 case XML_REGEXP_NOTINITNAME:
872 fprintf(output, "notinitname "); break;
873 case XML_REGEXP_NAMECHAR:
874 fprintf(output, "namechar "); break;
875 case XML_REGEXP_NOTNAMECHAR:
876 fprintf(output, "notnamechar "); break;
877 case XML_REGEXP_DECIMAL:
878 fprintf(output, "decimal "); break;
879 case XML_REGEXP_NOTDECIMAL:
880 fprintf(output, "notdecimal "); break;
881 case XML_REGEXP_REALCHAR:
882 fprintf(output, "realchar "); break;
883 case XML_REGEXP_NOTREALCHAR:
884 fprintf(output, "notrealchar "); break;
885 case XML_REGEXP_LETTER:
886 fprintf(output, "LETTER "); break;
887 case XML_REGEXP_LETTER_UPPERCASE:
888 fprintf(output, "LETTER_UPPERCASE "); break;
889 case XML_REGEXP_LETTER_LOWERCASE:
890 fprintf(output, "LETTER_LOWERCASE "); break;
891 case XML_REGEXP_LETTER_TITLECASE:
892 fprintf(output, "LETTER_TITLECASE "); break;
893 case XML_REGEXP_LETTER_MODIFIER:
894 fprintf(output, "LETTER_MODIFIER "); break;
895 case XML_REGEXP_LETTER_OTHERS:
896 fprintf(output, "LETTER_OTHERS "); break;
897 case XML_REGEXP_MARK:
898 fprintf(output, "MARK "); break;
899 case XML_REGEXP_MARK_NONSPACING:
900 fprintf(output, "MARK_NONSPACING "); break;
901 case XML_REGEXP_MARK_SPACECOMBINING:
902 fprintf(output, "MARK_SPACECOMBINING "); break;
903 case XML_REGEXP_MARK_ENCLOSING:
904 fprintf(output, "MARK_ENCLOSING "); break;
905 case XML_REGEXP_NUMBER:
906 fprintf(output, "NUMBER "); break;
907 case XML_REGEXP_NUMBER_DECIMAL:
908 fprintf(output, "NUMBER_DECIMAL "); break;
909 case XML_REGEXP_NUMBER_LETTER:
910 fprintf(output, "NUMBER_LETTER "); break;
911 case XML_REGEXP_NUMBER_OTHERS:
912 fprintf(output, "NUMBER_OTHERS "); break;
913 case XML_REGEXP_PUNCT:
914 fprintf(output, "PUNCT "); break;
915 case XML_REGEXP_PUNCT_CONNECTOR:
916 fprintf(output, "PUNCT_CONNECTOR "); break;
917 case XML_REGEXP_PUNCT_DASH:
918 fprintf(output, "PUNCT_DASH "); break;
919 case XML_REGEXP_PUNCT_OPEN:
920 fprintf(output, "PUNCT_OPEN "); break;
921 case XML_REGEXP_PUNCT_CLOSE:
922 fprintf(output, "PUNCT_CLOSE "); break;
923 case XML_REGEXP_PUNCT_INITQUOTE:
924 fprintf(output, "PUNCT_INITQUOTE "); break;
925 case XML_REGEXP_PUNCT_FINQUOTE:
926 fprintf(output, "PUNCT_FINQUOTE "); break;
927 case XML_REGEXP_PUNCT_OTHERS:
928 fprintf(output, "PUNCT_OTHERS "); break;
929 case XML_REGEXP_SEPAR:
930 fprintf(output, "SEPAR "); break;
931 case XML_REGEXP_SEPAR_SPACE:
932 fprintf(output, "SEPAR_SPACE "); break;
933 case XML_REGEXP_SEPAR_LINE:
934 fprintf(output, "SEPAR_LINE "); break;
935 case XML_REGEXP_SEPAR_PARA:
936 fprintf(output, "SEPAR_PARA "); break;
937 case XML_REGEXP_SYMBOL:
938 fprintf(output, "SYMBOL "); break;
939 case XML_REGEXP_SYMBOL_MATH:
940 fprintf(output, "SYMBOL_MATH "); break;
941 case XML_REGEXP_SYMBOL_CURRENCY:
942 fprintf(output, "SYMBOL_CURRENCY "); break;
943 case XML_REGEXP_SYMBOL_MODIFIER:
944 fprintf(output, "SYMBOL_MODIFIER "); break;
945 case XML_REGEXP_SYMBOL_OTHERS:
946 fprintf(output, "SYMBOL_OTHERS "); break;
947 case XML_REGEXP_OTHER:
948 fprintf(output, "OTHER "); break;
949 case XML_REGEXP_OTHER_CONTROL:
950 fprintf(output, "OTHER_CONTROL "); break;
951 case XML_REGEXP_OTHER_FORMAT:
952 fprintf(output, "OTHER_FORMAT "); break;
953 case XML_REGEXP_OTHER_PRIVATE:
954 fprintf(output, "OTHER_PRIVATE "); break;
955 case XML_REGEXP_OTHER_NA:
956 fprintf(output, "OTHER_NA "); break;
957 case XML_REGEXP_BLOCK_NAME:
958 fprintf(output, "BLOCK "); break;
959 }
960}
961
962static void
963xmlRegPrintQuantType(FILE *output, xmlRegQuantType type) {
964 switch (type) {
965 case XML_REGEXP_QUANT_EPSILON:
966 fprintf(output, "epsilon "); break;
967 case XML_REGEXP_QUANT_ONCE:
968 fprintf(output, "once "); break;
969 case XML_REGEXP_QUANT_OPT:
970 fprintf(output, "? "); break;
971 case XML_REGEXP_QUANT_MULT:
972 fprintf(output, "* "); break;
973 case XML_REGEXP_QUANT_PLUS:
974 fprintf(output, "+ "); break;
975 case XML_REGEXP_QUANT_RANGE:
976 fprintf(output, "range "); break;
Daniel Veillard7646b182002-04-20 06:41:40 +0000977 case XML_REGEXP_QUANT_ONCEONLY:
978 fprintf(output, "onceonly "); break;
979 case XML_REGEXP_QUANT_ALL:
980 fprintf(output, "all "); break;
Daniel Veillard4255d502002-04-16 15:50:10 +0000981 }
982}
983static void
984xmlRegPrintRange(FILE *output, xmlRegRangePtr range) {
985 fprintf(output, " range: ");
986 if (range->neg)
987 fprintf(output, "negative ");
988 xmlRegPrintAtomType(output, range->type);
989 fprintf(output, "%c - %c\n", range->start, range->end);
990}
991
992static void
993xmlRegPrintAtom(FILE *output, xmlRegAtomPtr atom) {
994 fprintf(output, " atom: ");
995 if (atom == NULL) {
996 fprintf(output, "NULL\n");
997 return;
998 }
Daniel Veillard9efc4762005-07-19 14:33:55 +0000999 if (atom->neg)
1000 fprintf(output, "not ");
Daniel Veillard4255d502002-04-16 15:50:10 +00001001 xmlRegPrintAtomType(output, atom->type);
1002 xmlRegPrintQuantType(output, atom->quant);
1003 if (atom->quant == XML_REGEXP_QUANT_RANGE)
1004 fprintf(output, "%d-%d ", atom->min, atom->max);
1005 if (atom->type == XML_REGEXP_STRING)
1006 fprintf(output, "'%s' ", (char *) atom->valuep);
1007 if (atom->type == XML_REGEXP_CHARVAL)
1008 fprintf(output, "char %c\n", atom->codepoint);
1009 else if (atom->type == XML_REGEXP_RANGES) {
1010 int i;
1011 fprintf(output, "%d entries\n", atom->nbRanges);
1012 for (i = 0; i < atom->nbRanges;i++)
1013 xmlRegPrintRange(output, atom->ranges[i]);
1014 } else if (atom->type == XML_REGEXP_SUBREG) {
1015 fprintf(output, "start %d end %d\n", atom->start->no, atom->stop->no);
1016 } else {
1017 fprintf(output, "\n");
1018 }
1019}
1020
1021static void
1022xmlRegPrintTrans(FILE *output, xmlRegTransPtr trans) {
1023 fprintf(output, " trans: ");
1024 if (trans == NULL) {
1025 fprintf(output, "NULL\n");
1026 return;
1027 }
1028 if (trans->to < 0) {
1029 fprintf(output, "removed\n");
1030 return;
1031 }
Daniel Veillard567a45b2005-10-18 19:11:55 +00001032 if (trans->nd != 0) {
1033 if (trans->nd == 2)
1034 fprintf(output, "last not determinist, ");
1035 else
1036 fprintf(output, "not determinist, ");
1037 }
Daniel Veillard4255d502002-04-16 15:50:10 +00001038 if (trans->counter >= 0) {
1039 fprintf(output, "counted %d, ", trans->counter);
1040 }
Daniel Veillard8a001f62002-04-20 07:24:11 +00001041 if (trans->count == REGEXP_ALL_COUNTER) {
1042 fprintf(output, "all transition, ");
1043 } else if (trans->count >= 0) {
Daniel Veillard4255d502002-04-16 15:50:10 +00001044 fprintf(output, "count based %d, ", trans->count);
1045 }
1046 if (trans->atom == NULL) {
1047 fprintf(output, "epsilon to %d\n", trans->to);
1048 return;
1049 }
1050 if (trans->atom->type == XML_REGEXP_CHARVAL)
1051 fprintf(output, "char %c ", trans->atom->codepoint);
1052 fprintf(output, "atom %d, to %d\n", trans->atom->no, trans->to);
1053}
1054
1055static void
1056xmlRegPrintState(FILE *output, xmlRegStatePtr state) {
1057 int i;
1058
1059 fprintf(output, " state: ");
1060 if (state == NULL) {
1061 fprintf(output, "NULL\n");
1062 return;
1063 }
1064 if (state->type == XML_REGEXP_START_STATE)
1065 fprintf(output, "START ");
1066 if (state->type == XML_REGEXP_FINAL_STATE)
1067 fprintf(output, "FINAL ");
1068
1069 fprintf(output, "%d, %d transitions:\n", state->no, state->nbTrans);
1070 for (i = 0;i < state->nbTrans; i++) {
1071 xmlRegPrintTrans(output, &(state->trans[i]));
1072 }
1073}
1074
Daniel Veillard23e73572002-09-19 19:56:43 +00001075#ifdef DEBUG_REGEXP_GRAPH
Daniel Veillard4255d502002-04-16 15:50:10 +00001076static void
1077xmlRegPrintCtxt(FILE *output, xmlRegParserCtxtPtr ctxt) {
1078 int i;
1079
1080 fprintf(output, " ctxt: ");
1081 if (ctxt == NULL) {
1082 fprintf(output, "NULL\n");
1083 return;
1084 }
1085 fprintf(output, "'%s' ", ctxt->string);
1086 if (ctxt->error)
1087 fprintf(output, "error ");
1088 if (ctxt->neg)
1089 fprintf(output, "neg ");
1090 fprintf(output, "\n");
1091 fprintf(output, "%d atoms:\n", ctxt->nbAtoms);
1092 for (i = 0;i < ctxt->nbAtoms; i++) {
1093 fprintf(output, " %02d ", i);
1094 xmlRegPrintAtom(output, ctxt->atoms[i]);
1095 }
1096 if (ctxt->atom != NULL) {
1097 fprintf(output, "current atom:\n");
1098 xmlRegPrintAtom(output, ctxt->atom);
1099 }
1100 fprintf(output, "%d states:", ctxt->nbStates);
1101 if (ctxt->start != NULL)
1102 fprintf(output, " start: %d", ctxt->start->no);
1103 if (ctxt->end != NULL)
1104 fprintf(output, " end: %d", ctxt->end->no);
1105 fprintf(output, "\n");
1106 for (i = 0;i < ctxt->nbStates; i++) {
1107 xmlRegPrintState(output, ctxt->states[i]);
1108 }
1109 fprintf(output, "%d counters:\n", ctxt->nbCounters);
1110 for (i = 0;i < ctxt->nbCounters; i++) {
1111 fprintf(output, " %d: min %d max %d\n", i, ctxt->counters[i].min,
1112 ctxt->counters[i].max);
1113 }
1114}
Daniel Veillard23e73572002-09-19 19:56:43 +00001115#endif
Daniel Veillard4255d502002-04-16 15:50:10 +00001116
1117/************************************************************************
1118 * *
1119 * Finite Automata structures manipulations *
1120 * *
1121 ************************************************************************/
1122
1123static void
1124xmlRegAtomAddRange(xmlRegParserCtxtPtr ctxt, xmlRegAtomPtr atom,
1125 int neg, xmlRegAtomType type, int start, int end,
1126 xmlChar *blockName) {
1127 xmlRegRangePtr range;
1128
1129 if (atom == NULL) {
1130 ERROR("add range: atom is NULL");
1131 return;
1132 }
1133 if (atom->type != XML_REGEXP_RANGES) {
1134 ERROR("add range: atom is not ranges");
1135 return;
1136 }
1137 if (atom->maxRanges == 0) {
1138 atom->maxRanges = 4;
1139 atom->ranges = (xmlRegRangePtr *) xmlMalloc(atom->maxRanges *
1140 sizeof(xmlRegRangePtr));
1141 if (atom->ranges == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00001142 xmlRegexpErrMemory(ctxt, "adding ranges");
Daniel Veillard4255d502002-04-16 15:50:10 +00001143 atom->maxRanges = 0;
1144 return;
1145 }
1146 } else if (atom->nbRanges >= atom->maxRanges) {
1147 xmlRegRangePtr *tmp;
1148 atom->maxRanges *= 2;
1149 tmp = (xmlRegRangePtr *) xmlRealloc(atom->ranges, atom->maxRanges *
1150 sizeof(xmlRegRangePtr));
1151 if (tmp == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00001152 xmlRegexpErrMemory(ctxt, "adding ranges");
Daniel Veillard4255d502002-04-16 15:50:10 +00001153 atom->maxRanges /= 2;
1154 return;
1155 }
1156 atom->ranges = tmp;
1157 }
1158 range = xmlRegNewRange(ctxt, neg, type, start, end);
1159 if (range == NULL)
1160 return;
1161 range->blockName = blockName;
1162 atom->ranges[atom->nbRanges++] = range;
1163
1164}
1165
1166static int
1167xmlRegGetCounter(xmlRegParserCtxtPtr ctxt) {
1168 if (ctxt->maxCounters == 0) {
1169 ctxt->maxCounters = 4;
1170 ctxt->counters = (xmlRegCounter *) xmlMalloc(ctxt->maxCounters *
1171 sizeof(xmlRegCounter));
1172 if (ctxt->counters == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00001173 xmlRegexpErrMemory(ctxt, "allocating counter");
Daniel Veillard4255d502002-04-16 15:50:10 +00001174 ctxt->maxCounters = 0;
1175 return(-1);
1176 }
1177 } else if (ctxt->nbCounters >= ctxt->maxCounters) {
1178 xmlRegCounter *tmp;
1179 ctxt->maxCounters *= 2;
1180 tmp = (xmlRegCounter *) xmlRealloc(ctxt->counters, ctxt->maxCounters *
1181 sizeof(xmlRegCounter));
1182 if (tmp == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00001183 xmlRegexpErrMemory(ctxt, "allocating counter");
Daniel Veillard4255d502002-04-16 15:50:10 +00001184 ctxt->maxCounters /= 2;
1185 return(-1);
1186 }
1187 ctxt->counters = tmp;
1188 }
1189 ctxt->counters[ctxt->nbCounters].min = -1;
1190 ctxt->counters[ctxt->nbCounters].max = -1;
1191 return(ctxt->nbCounters++);
1192}
1193
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001194static int
Daniel Veillard4255d502002-04-16 15:50:10 +00001195xmlRegAtomPush(xmlRegParserCtxtPtr ctxt, xmlRegAtomPtr atom) {
1196 if (atom == NULL) {
1197 ERROR("atom push: atom is NULL");
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001198 return(-1);
Daniel Veillard4255d502002-04-16 15:50:10 +00001199 }
1200 if (ctxt->maxAtoms == 0) {
1201 ctxt->maxAtoms = 4;
1202 ctxt->atoms = (xmlRegAtomPtr *) xmlMalloc(ctxt->maxAtoms *
1203 sizeof(xmlRegAtomPtr));
1204 if (ctxt->atoms == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00001205 xmlRegexpErrMemory(ctxt, "pushing atom");
Daniel Veillard4255d502002-04-16 15:50:10 +00001206 ctxt->maxAtoms = 0;
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001207 return(-1);
Daniel Veillard4255d502002-04-16 15:50:10 +00001208 }
1209 } else if (ctxt->nbAtoms >= ctxt->maxAtoms) {
1210 xmlRegAtomPtr *tmp;
1211 ctxt->maxAtoms *= 2;
1212 tmp = (xmlRegAtomPtr *) xmlRealloc(ctxt->atoms, ctxt->maxAtoms *
1213 sizeof(xmlRegAtomPtr));
1214 if (tmp == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00001215 xmlRegexpErrMemory(ctxt, "allocating counter");
Daniel Veillard4255d502002-04-16 15:50:10 +00001216 ctxt->maxAtoms /= 2;
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001217 return(-1);
Daniel Veillard4255d502002-04-16 15:50:10 +00001218 }
1219 ctxt->atoms = tmp;
1220 }
1221 atom->no = ctxt->nbAtoms;
1222 ctxt->atoms[ctxt->nbAtoms++] = atom;
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001223 return(0);
Daniel Veillard4255d502002-04-16 15:50:10 +00001224}
1225
1226static void
Daniel Veillarddb68b742005-07-30 13:18:24 +00001227xmlRegStateAddTransTo(xmlRegParserCtxtPtr ctxt, xmlRegStatePtr target,
1228 int from) {
1229 if (target->maxTransTo == 0) {
1230 target->maxTransTo = 8;
1231 target->transTo = (int *) xmlMalloc(target->maxTransTo *
1232 sizeof(int));
1233 if (target->transTo == NULL) {
1234 xmlRegexpErrMemory(ctxt, "adding transition");
1235 target->maxTransTo = 0;
1236 return;
1237 }
1238 } else if (target->nbTransTo >= target->maxTransTo) {
1239 int *tmp;
1240 target->maxTransTo *= 2;
1241 tmp = (int *) xmlRealloc(target->transTo, target->maxTransTo *
1242 sizeof(int));
1243 if (tmp == NULL) {
1244 xmlRegexpErrMemory(ctxt, "adding transition");
1245 target->maxTransTo /= 2;
1246 return;
1247 }
1248 target->transTo = tmp;
1249 }
1250 target->transTo[target->nbTransTo] = from;
1251 target->nbTransTo++;
1252}
1253
1254static void
Daniel Veillard4255d502002-04-16 15:50:10 +00001255xmlRegStateAddTrans(xmlRegParserCtxtPtr ctxt, xmlRegStatePtr state,
1256 xmlRegAtomPtr atom, xmlRegStatePtr target,
Daniel Veillard5de09382005-09-26 17:18:17 +00001257 int counter, int count) {
William M. Brackf9b5fa22004-05-10 07:52:15 +00001258
1259 int nrtrans;
1260
Daniel Veillard4255d502002-04-16 15:50:10 +00001261 if (state == NULL) {
1262 ERROR("add state: state is NULL");
1263 return;
1264 }
1265 if (target == NULL) {
1266 ERROR("add state: target is NULL");
1267 return;
1268 }
William M. Brackf9b5fa22004-05-10 07:52:15 +00001269 /*
1270 * Other routines follow the philosophy 'When in doubt, add a transition'
1271 * so we check here whether such a transition is already present and, if
1272 * so, silently ignore this request.
1273 */
1274
Daniel Veillard5de09382005-09-26 17:18:17 +00001275 for (nrtrans = state->nbTrans - 1; nrtrans >= 0; nrtrans--) {
1276 xmlRegTransPtr trans = &(state->trans[nrtrans]);
1277 if ((trans->atom == atom) &&
1278 (trans->to == target->no) &&
1279 (trans->counter == counter) &&
1280 (trans->count == count)) {
William M. Brackf9b5fa22004-05-10 07:52:15 +00001281#ifdef DEBUG_REGEXP_GRAPH
Daniel Veillard5de09382005-09-26 17:18:17 +00001282 printf("Ignoring duplicate transition from %d to %d\n",
1283 state->no, target->no);
William M. Brackf9b5fa22004-05-10 07:52:15 +00001284#endif
Daniel Veillard5de09382005-09-26 17:18:17 +00001285 return;
Daniel Veillarddb68b742005-07-30 13:18:24 +00001286 }
William M. Brackf9b5fa22004-05-10 07:52:15 +00001287 }
1288
Daniel Veillard4255d502002-04-16 15:50:10 +00001289 if (state->maxTrans == 0) {
Daniel Veillarddb68b742005-07-30 13:18:24 +00001290 state->maxTrans = 8;
Daniel Veillard4255d502002-04-16 15:50:10 +00001291 state->trans = (xmlRegTrans *) xmlMalloc(state->maxTrans *
1292 sizeof(xmlRegTrans));
1293 if (state->trans == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00001294 xmlRegexpErrMemory(ctxt, "adding transition");
Daniel Veillard4255d502002-04-16 15:50:10 +00001295 state->maxTrans = 0;
1296 return;
1297 }
1298 } else if (state->nbTrans >= state->maxTrans) {
1299 xmlRegTrans *tmp;
1300 state->maxTrans *= 2;
1301 tmp = (xmlRegTrans *) xmlRealloc(state->trans, state->maxTrans *
1302 sizeof(xmlRegTrans));
1303 if (tmp == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00001304 xmlRegexpErrMemory(ctxt, "adding transition");
Daniel Veillard4255d502002-04-16 15:50:10 +00001305 state->maxTrans /= 2;
1306 return;
1307 }
1308 state->trans = tmp;
1309 }
1310#ifdef DEBUG_REGEXP_GRAPH
1311 printf("Add trans from %d to %d ", state->no, target->no);
Daniel Veillard8a001f62002-04-20 07:24:11 +00001312 if (count == REGEXP_ALL_COUNTER)
Daniel Veillard2cbf5962004-03-31 15:50:43 +00001313 printf("all transition\n");
Daniel Veillard4402ab42002-09-12 16:02:56 +00001314 else if (count >= 0)
Daniel Veillard2cbf5962004-03-31 15:50:43 +00001315 printf("count based %d\n", count);
Daniel Veillard4255d502002-04-16 15:50:10 +00001316 else if (counter >= 0)
Daniel Veillard2cbf5962004-03-31 15:50:43 +00001317 printf("counted %d\n", counter);
Daniel Veillard4255d502002-04-16 15:50:10 +00001318 else if (atom == NULL)
Daniel Veillard2cbf5962004-03-31 15:50:43 +00001319 printf("epsilon transition\n");
1320 else if (atom != NULL)
1321 xmlRegPrintAtom(stdout, atom);
Daniel Veillard4255d502002-04-16 15:50:10 +00001322#endif
1323
1324 state->trans[state->nbTrans].atom = atom;
1325 state->trans[state->nbTrans].to = target->no;
1326 state->trans[state->nbTrans].counter = counter;
1327 state->trans[state->nbTrans].count = count;
Daniel Veillard567a45b2005-10-18 19:11:55 +00001328 state->trans[state->nbTrans].nd = 0;
Daniel Veillard4255d502002-04-16 15:50:10 +00001329 state->nbTrans++;
Daniel Veillarddb68b742005-07-30 13:18:24 +00001330 xmlRegStateAddTransTo(ctxt, target, state->no);
Daniel Veillard4255d502002-04-16 15:50:10 +00001331}
1332
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001333static int
Daniel Veillard4255d502002-04-16 15:50:10 +00001334xmlRegStatePush(xmlRegParserCtxtPtr ctxt, xmlRegStatePtr state) {
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001335 if (state == NULL) return(-1);
Daniel Veillard4255d502002-04-16 15:50:10 +00001336 if (ctxt->maxStates == 0) {
1337 ctxt->maxStates = 4;
1338 ctxt->states = (xmlRegStatePtr *) xmlMalloc(ctxt->maxStates *
1339 sizeof(xmlRegStatePtr));
1340 if (ctxt->states == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00001341 xmlRegexpErrMemory(ctxt, "adding state");
Daniel Veillard4255d502002-04-16 15:50:10 +00001342 ctxt->maxStates = 0;
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001343 return(-1);
Daniel Veillard4255d502002-04-16 15:50:10 +00001344 }
1345 } else if (ctxt->nbStates >= ctxt->maxStates) {
1346 xmlRegStatePtr *tmp;
1347 ctxt->maxStates *= 2;
1348 tmp = (xmlRegStatePtr *) xmlRealloc(ctxt->states, ctxt->maxStates *
1349 sizeof(xmlRegStatePtr));
1350 if (tmp == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00001351 xmlRegexpErrMemory(ctxt, "adding state");
Daniel Veillard4255d502002-04-16 15:50:10 +00001352 ctxt->maxStates /= 2;
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001353 return(-1);
Daniel Veillard4255d502002-04-16 15:50:10 +00001354 }
1355 ctxt->states = tmp;
1356 }
1357 state->no = ctxt->nbStates;
1358 ctxt->states[ctxt->nbStates++] = state;
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001359 return(0);
Daniel Veillard4255d502002-04-16 15:50:10 +00001360}
1361
1362/**
Daniel Veillard7646b182002-04-20 06:41:40 +00001363 * xmlFAGenerateAllTransition:
Daniel Veillard441bc322002-04-20 17:38:48 +00001364 * @ctxt: a regexp parser context
1365 * @from: the from state
1366 * @to: the target state or NULL for building a new one
1367 * @lax:
Daniel Veillard7646b182002-04-20 06:41:40 +00001368 *
1369 */
1370static void
1371xmlFAGenerateAllTransition(xmlRegParserCtxtPtr ctxt,
Daniel Veillard441bc322002-04-20 17:38:48 +00001372 xmlRegStatePtr from, xmlRegStatePtr to,
1373 int lax) {
Daniel Veillard7646b182002-04-20 06:41:40 +00001374 if (to == NULL) {
1375 to = xmlRegNewState(ctxt);
1376 xmlRegStatePush(ctxt, to);
1377 ctxt->state = to;
1378 }
Daniel Veillard441bc322002-04-20 17:38:48 +00001379 if (lax)
Daniel Veillard5de09382005-09-26 17:18:17 +00001380 xmlRegStateAddTrans(ctxt, from, NULL, to, -1, REGEXP_ALL_LAX_COUNTER);
Daniel Veillard441bc322002-04-20 17:38:48 +00001381 else
Daniel Veillard5de09382005-09-26 17:18:17 +00001382 xmlRegStateAddTrans(ctxt, from, NULL, to, -1, REGEXP_ALL_COUNTER);
Daniel Veillard7646b182002-04-20 06:41:40 +00001383}
1384
1385/**
Daniel Veillard4255d502002-04-16 15:50:10 +00001386 * xmlFAGenerateEpsilonTransition:
Daniel Veillard441bc322002-04-20 17:38:48 +00001387 * @ctxt: a regexp parser context
1388 * @from: the from state
1389 * @to: the target state or NULL for building a new one
Daniel Veillard4255d502002-04-16 15:50:10 +00001390 *
1391 */
1392static void
1393xmlFAGenerateEpsilonTransition(xmlRegParserCtxtPtr ctxt,
1394 xmlRegStatePtr from, xmlRegStatePtr to) {
1395 if (to == NULL) {
1396 to = xmlRegNewState(ctxt);
1397 xmlRegStatePush(ctxt, to);
1398 ctxt->state = to;
1399 }
Daniel Veillard5de09382005-09-26 17:18:17 +00001400 xmlRegStateAddTrans(ctxt, from, NULL, to, -1, -1);
Daniel Veillard4255d502002-04-16 15:50:10 +00001401}
1402
1403/**
1404 * xmlFAGenerateCountedEpsilonTransition:
Daniel Veillard441bc322002-04-20 17:38:48 +00001405 * @ctxt: a regexp parser context
1406 * @from: the from state
1407 * @to: the target state or NULL for building a new one
Daniel Veillard4255d502002-04-16 15:50:10 +00001408 * counter: the counter for that transition
1409 *
1410 */
1411static void
1412xmlFAGenerateCountedEpsilonTransition(xmlRegParserCtxtPtr ctxt,
1413 xmlRegStatePtr from, xmlRegStatePtr to, int counter) {
1414 if (to == NULL) {
1415 to = xmlRegNewState(ctxt);
1416 xmlRegStatePush(ctxt, to);
1417 ctxt->state = to;
1418 }
Daniel Veillard5de09382005-09-26 17:18:17 +00001419 xmlRegStateAddTrans(ctxt, from, NULL, to, counter, -1);
Daniel Veillard4255d502002-04-16 15:50:10 +00001420}
1421
1422/**
1423 * xmlFAGenerateCountedTransition:
Daniel Veillard441bc322002-04-20 17:38:48 +00001424 * @ctxt: a regexp parser context
1425 * @from: the from state
1426 * @to: the target state or NULL for building a new one
Daniel Veillard4255d502002-04-16 15:50:10 +00001427 * counter: the counter for that transition
1428 *
1429 */
1430static void
1431xmlFAGenerateCountedTransition(xmlRegParserCtxtPtr ctxt,
1432 xmlRegStatePtr from, xmlRegStatePtr to, int counter) {
1433 if (to == NULL) {
1434 to = xmlRegNewState(ctxt);
1435 xmlRegStatePush(ctxt, to);
1436 ctxt->state = to;
1437 }
Daniel Veillard5de09382005-09-26 17:18:17 +00001438 xmlRegStateAddTrans(ctxt, from, NULL, to, -1, counter);
Daniel Veillard4255d502002-04-16 15:50:10 +00001439}
1440
1441/**
1442 * xmlFAGenerateTransitions:
Daniel Veillard441bc322002-04-20 17:38:48 +00001443 * @ctxt: a regexp parser context
1444 * @from: the from state
1445 * @to: the target state or NULL for building a new one
1446 * @atom: the atom generating the transition
Daniel Veillard4255d502002-04-16 15:50:10 +00001447 *
William M. Brackddf71d62004-05-06 04:17:26 +00001448 * Returns 0 if success and -1 in case of error.
Daniel Veillard4255d502002-04-16 15:50:10 +00001449 */
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001450static int
Daniel Veillard4255d502002-04-16 15:50:10 +00001451xmlFAGenerateTransitions(xmlRegParserCtxtPtr ctxt, xmlRegStatePtr from,
1452 xmlRegStatePtr to, xmlRegAtomPtr atom) {
1453 if (atom == NULL) {
1454 ERROR("genrate transition: atom == NULL");
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001455 return(-1);
Daniel Veillard4255d502002-04-16 15:50:10 +00001456 }
1457 if (atom->type == XML_REGEXP_SUBREG) {
1458 /*
1459 * this is a subexpression handling one should not need to
William M. Brackddf71d62004-05-06 04:17:26 +00001460 * create a new node except for XML_REGEXP_QUANT_RANGE.
Daniel Veillard4255d502002-04-16 15:50:10 +00001461 */
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001462 if (xmlRegAtomPush(ctxt, atom) < 0) {
1463 return(-1);
1464 }
Daniel Veillard4255d502002-04-16 15:50:10 +00001465 if ((to != NULL) && (atom->stop != to) &&
1466 (atom->quant != XML_REGEXP_QUANT_RANGE)) {
1467 /*
1468 * Generate an epsilon transition to link to the target
1469 */
1470 xmlFAGenerateEpsilonTransition(ctxt, atom->stop, to);
Daniel Veillardaa622012005-10-20 15:55:25 +00001471#ifdef DV
1472 } else if ((to == NULL) && (atom->quant != XML_REGEXP_QUANT_RANGE) &&
1473 (atom->quant != XML_REGEXP_QUANT_ONCE)) {
1474 to = xmlRegNewState(ctxt);
1475 xmlRegStatePush(ctxt, to);
1476 ctxt->state = to;
1477 xmlFAGenerateEpsilonTransition(ctxt, atom->stop, to);
1478#endif
Daniel Veillard4255d502002-04-16 15:50:10 +00001479 }
1480 switch (atom->quant) {
1481 case XML_REGEXP_QUANT_OPT:
1482 atom->quant = XML_REGEXP_QUANT_ONCE;
Daniel Veillard54eb0242006-03-21 23:17:57 +00001483 /*
1484 * transition done to the state after end of atom.
1485 * 1. set transition from atom start to new state
1486 * 2. set transition from atom end to this state.
1487 */
1488 xmlFAGenerateEpsilonTransition(ctxt, atom->start, 0);
1489 xmlFAGenerateEpsilonTransition(ctxt, atom->stop, ctxt->state);
Daniel Veillard4255d502002-04-16 15:50:10 +00001490 break;
1491 case XML_REGEXP_QUANT_MULT:
1492 atom->quant = XML_REGEXP_QUANT_ONCE;
1493 xmlFAGenerateEpsilonTransition(ctxt, atom->start, atom->stop);
1494 xmlFAGenerateEpsilonTransition(ctxt, atom->stop, atom->start);
1495 break;
1496 case XML_REGEXP_QUANT_PLUS:
1497 atom->quant = XML_REGEXP_QUANT_ONCE;
1498 xmlFAGenerateEpsilonTransition(ctxt, atom->stop, atom->start);
1499 break;
1500 case XML_REGEXP_QUANT_RANGE: {
1501 int counter;
1502 xmlRegStatePtr newstate;
1503
1504 /*
1505 * This one is nasty:
William M. Brackddf71d62004-05-06 04:17:26 +00001506 * 1/ if range has minOccurs == 0, create a new state
1507 * and create epsilon transitions from atom->start
1508 * to atom->stop, as well as atom->start to the new
1509 * state
1510 * 2/ register a new counter
1511 * 3/ register an epsilon transition associated to
Daniel Veillard4255d502002-04-16 15:50:10 +00001512 * this counter going from atom->stop to atom->start
William M. Brackddf71d62004-05-06 04:17:26 +00001513 * 4/ create a new state
1514 * 5/ generate a counted transition from atom->stop to
Daniel Veillard4255d502002-04-16 15:50:10 +00001515 * that state
1516 */
William M. Brackddf71d62004-05-06 04:17:26 +00001517 if (atom->min == 0) {
1518 xmlFAGenerateEpsilonTransition(ctxt, atom->start,
1519 atom->stop);
1520 newstate = xmlRegNewState(ctxt);
1521 xmlRegStatePush(ctxt, newstate);
1522 ctxt->state = newstate;
1523 xmlFAGenerateEpsilonTransition(ctxt, atom->start,
1524 newstate);
1525 }
Daniel Veillard4255d502002-04-16 15:50:10 +00001526 counter = xmlRegGetCounter(ctxt);
1527 ctxt->counters[counter].min = atom->min - 1;
1528 ctxt->counters[counter].max = atom->max - 1;
1529 atom->min = 0;
1530 atom->max = 0;
1531 atom->quant = XML_REGEXP_QUANT_ONCE;
Daniel Veillard4255d502002-04-16 15:50:10 +00001532 if (to != NULL) {
1533 newstate = to;
1534 } else {
1535 newstate = xmlRegNewState(ctxt);
1536 xmlRegStatePush(ctxt, newstate);
Daniel Veillard4255d502002-04-16 15:50:10 +00001537 }
Daniel Veillard9a00fd22005-11-09 08:56:26 +00001538 ctxt->state = newstate;
Daniel Veillard4255d502002-04-16 15:50:10 +00001539 xmlFAGenerateCountedTransition(ctxt, atom->stop,
1540 newstate, counter);
Daniel Veillard54eb0242006-03-21 23:17:57 +00001541
1542 /*
1543 * first check count and if OK jump forward;
1544 * if checking fail increment count and jump back
1545 */
1546 xmlFAGenerateCountedEpsilonTransition(ctxt, atom->stop,
1547 atom->start, counter);
Daniel Veillard4255d502002-04-16 15:50:10 +00001548 }
1549 default:
1550 break;
1551 }
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001552 return(0);
Daniel Veillard567a45b2005-10-18 19:11:55 +00001553 }
1554 if ((atom->min == 0) && (atom->max == 0) &&
Daniel Veillard99c394d2005-07-14 12:58:49 +00001555 (atom->quant == XML_REGEXP_QUANT_RANGE)) {
1556 /*
1557 * we can discard the atom and generate an epsilon transition instead
1558 */
1559 if (to == NULL) {
1560 to = xmlRegNewState(ctxt);
1561 if (to != NULL)
1562 xmlRegStatePush(ctxt, to);
1563 else {
1564 return(-1);
1565 }
1566 }
1567 xmlFAGenerateEpsilonTransition(ctxt, from, to);
1568 ctxt->state = to;
1569 xmlRegFreeAtom(atom);
1570 return(0);
Daniel Veillard567a45b2005-10-18 19:11:55 +00001571 }
1572 if (to == NULL) {
1573 to = xmlRegNewState(ctxt);
1574 if (to != NULL)
1575 xmlRegStatePush(ctxt, to);
1576 else {
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001577 return(-1);
Daniel Veillard4255d502002-04-16 15:50:10 +00001578 }
Daniel Veillard4255d502002-04-16 15:50:10 +00001579 }
Daniel Veillard567a45b2005-10-18 19:11:55 +00001580 if (xmlRegAtomPush(ctxt, atom) < 0) {
1581 return(-1);
1582 }
1583 xmlRegStateAddTrans(ctxt, from, atom, to, -1, -1);
1584 ctxt->state = to;
Daniel Veillard4255d502002-04-16 15:50:10 +00001585 switch (atom->quant) {
1586 case XML_REGEXP_QUANT_OPT:
1587 atom->quant = XML_REGEXP_QUANT_ONCE;
1588 xmlFAGenerateEpsilonTransition(ctxt, from, to);
1589 break;
1590 case XML_REGEXP_QUANT_MULT:
1591 atom->quant = XML_REGEXP_QUANT_ONCE;
1592 xmlFAGenerateEpsilonTransition(ctxt, from, to);
Daniel Veillard5de09382005-09-26 17:18:17 +00001593 xmlRegStateAddTrans(ctxt, to, atom, to, -1, -1);
Daniel Veillard4255d502002-04-16 15:50:10 +00001594 break;
1595 case XML_REGEXP_QUANT_PLUS:
1596 atom->quant = XML_REGEXP_QUANT_ONCE;
Daniel Veillard5de09382005-09-26 17:18:17 +00001597 xmlRegStateAddTrans(ctxt, to, atom, to, -1, -1);
Daniel Veillard4255d502002-04-16 15:50:10 +00001598 break;
1599 default:
1600 break;
1601 }
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001602 return(0);
Daniel Veillard4255d502002-04-16 15:50:10 +00001603}
1604
1605/**
1606 * xmlFAReduceEpsilonTransitions:
Daniel Veillard441bc322002-04-20 17:38:48 +00001607 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00001608 * @fromnr: the from state
1609 * @tonr: the to state
William M. Brackddf71d62004-05-06 04:17:26 +00001610 * @counter: should that transition be associated to a counted
Daniel Veillard4255d502002-04-16 15:50:10 +00001611 *
1612 */
1613static void
1614xmlFAReduceEpsilonTransitions(xmlRegParserCtxtPtr ctxt, int fromnr,
1615 int tonr, int counter) {
1616 int transnr;
1617 xmlRegStatePtr from;
1618 xmlRegStatePtr to;
1619
1620#ifdef DEBUG_REGEXP_GRAPH
1621 printf("xmlFAReduceEpsilonTransitions(%d, %d)\n", fromnr, tonr);
1622#endif
1623 from = ctxt->states[fromnr];
1624 if (from == NULL)
1625 return;
1626 to = ctxt->states[tonr];
1627 if (to == NULL)
1628 return;
1629 if ((to->mark == XML_REGEXP_MARK_START) ||
1630 (to->mark == XML_REGEXP_MARK_VISITED))
1631 return;
1632
1633 to->mark = XML_REGEXP_MARK_VISITED;
1634 if (to->type == XML_REGEXP_FINAL_STATE) {
1635#ifdef DEBUG_REGEXP_GRAPH
1636 printf("State %d is final, so %d becomes final\n", tonr, fromnr);
1637#endif
1638 from->type = XML_REGEXP_FINAL_STATE;
1639 }
1640 for (transnr = 0;transnr < to->nbTrans;transnr++) {
Daniel Veillarddb68b742005-07-30 13:18:24 +00001641 if (to->trans[transnr].to < 0)
1642 continue;
Daniel Veillard4255d502002-04-16 15:50:10 +00001643 if (to->trans[transnr].atom == NULL) {
1644 /*
1645 * Don't remove counted transitions
1646 * Don't loop either
1647 */
Daniel Veillardb509f152002-04-17 16:28:10 +00001648 if (to->trans[transnr].to != fromnr) {
1649 if (to->trans[transnr].count >= 0) {
1650 int newto = to->trans[transnr].to;
1651
1652 xmlRegStateAddTrans(ctxt, from, NULL,
1653 ctxt->states[newto],
Daniel Veillard5de09382005-09-26 17:18:17 +00001654 -1, to->trans[transnr].count);
Daniel Veillardb509f152002-04-17 16:28:10 +00001655 } else {
Daniel Veillard4255d502002-04-16 15:50:10 +00001656#ifdef DEBUG_REGEXP_GRAPH
Daniel Veillardb509f152002-04-17 16:28:10 +00001657 printf("Found epsilon trans %d from %d to %d\n",
1658 transnr, tonr, to->trans[transnr].to);
Daniel Veillard4255d502002-04-16 15:50:10 +00001659#endif
Daniel Veillardb509f152002-04-17 16:28:10 +00001660 if (to->trans[transnr].counter >= 0) {
1661 xmlFAReduceEpsilonTransitions(ctxt, fromnr,
1662 to->trans[transnr].to,
1663 to->trans[transnr].counter);
1664 } else {
1665 xmlFAReduceEpsilonTransitions(ctxt, fromnr,
1666 to->trans[transnr].to,
1667 counter);
1668 }
1669 }
Daniel Veillard4255d502002-04-16 15:50:10 +00001670 }
1671 } else {
1672 int newto = to->trans[transnr].to;
1673
Daniel Veillardb509f152002-04-17 16:28:10 +00001674 if (to->trans[transnr].counter >= 0) {
1675 xmlRegStateAddTrans(ctxt, from, to->trans[transnr].atom,
1676 ctxt->states[newto],
Daniel Veillard5de09382005-09-26 17:18:17 +00001677 to->trans[transnr].counter, -1);
Daniel Veillardb509f152002-04-17 16:28:10 +00001678 } else {
1679 xmlRegStateAddTrans(ctxt, from, to->trans[transnr].atom,
Daniel Veillard5de09382005-09-26 17:18:17 +00001680 ctxt->states[newto], counter, -1);
Daniel Veillardb509f152002-04-17 16:28:10 +00001681 }
Daniel Veillard4255d502002-04-16 15:50:10 +00001682 }
1683 }
1684 to->mark = XML_REGEXP_MARK_NORMAL;
1685}
1686
1687/**
Daniel Veillarddb68b742005-07-30 13:18:24 +00001688 * xmlFAEliminateSimpleEpsilonTransitions:
1689 * @ctxt: a regexp parser context
1690 *
1691 * Eliminating general epsilon transitions can get costly in the general
1692 * algorithm due to the large amount of generated new transitions and
1693 * associated comparisons. However for simple epsilon transition used just
1694 * to separate building blocks when generating the automata this can be
1695 * reduced to state elimination:
1696 * - if there exists an epsilon from X to Y
1697 * - if there is no other transition from X
1698 * then X and Y are semantically equivalent and X can be eliminated
1699 * If X is the start state then make Y the start state, else replace the
1700 * target of all transitions to X by transitions to Y.
1701 */
1702static void
1703xmlFAEliminateSimpleEpsilonTransitions(xmlRegParserCtxtPtr ctxt) {
1704 int statenr, i, j, newto;
1705 xmlRegStatePtr state, tmp;
1706
1707 for (statenr = 0;statenr < ctxt->nbStates;statenr++) {
1708 state = ctxt->states[statenr];
1709 if (state == NULL)
1710 continue;
1711 if (state->nbTrans != 1)
1712 continue;
Daniel Veillard0e05f4c2006-11-01 15:33:04 +00001713 if (state->type == XML_REGEXP_UNREACH_STATE)
1714 continue;
Daniel Veillarddb68b742005-07-30 13:18:24 +00001715 /* is the only transition out a basic transition */
1716 if ((state->trans[0].atom == NULL) &&
1717 (state->trans[0].to >= 0) &&
1718 (state->trans[0].to != statenr) &&
1719 (state->trans[0].counter < 0) &&
1720 (state->trans[0].count < 0)) {
1721 newto = state->trans[0].to;
1722
1723 if (state->type == XML_REGEXP_START_STATE) {
1724#ifdef DEBUG_REGEXP_GRAPH
1725 printf("Found simple epsilon trans from start %d to %d\n",
1726 statenr, newto);
1727#endif
1728 } else {
1729#ifdef DEBUG_REGEXP_GRAPH
1730 printf("Found simple epsilon trans from %d to %d\n",
1731 statenr, newto);
1732#endif
1733 for (i = 0;i < state->nbTransTo;i++) {
1734 tmp = ctxt->states[state->transTo[i]];
1735 for (j = 0;j < tmp->nbTrans;j++) {
1736 if (tmp->trans[j].to == statenr) {
Daniel Veillarddb68b742005-07-30 13:18:24 +00001737#ifdef DEBUG_REGEXP_GRAPH
1738 printf("Changed transition %d on %d to go to %d\n",
1739 j, tmp->no, newto);
1740#endif
Daniel Veillard0e05f4c2006-11-01 15:33:04 +00001741 tmp->trans[j].to = -1;
1742 xmlRegStateAddTrans(ctxt, tmp, tmp->trans[j].atom,
1743 ctxt->states[newto],
1744 tmp->trans[j].counter,
1745 tmp->trans[j].count);
Daniel Veillarddb68b742005-07-30 13:18:24 +00001746 }
1747 }
1748 }
Daniel Veillarddb68b742005-07-30 13:18:24 +00001749 if (state->type == XML_REGEXP_FINAL_STATE)
1750 ctxt->states[newto]->type = XML_REGEXP_FINAL_STATE;
1751 /* eliminate the transition completely */
1752 state->nbTrans = 0;
1753
Daniel Veillard0e05f4c2006-11-01 15:33:04 +00001754 state->type = XML_REGEXP_UNREACH_STATE;
Daniel Veillarddb68b742005-07-30 13:18:24 +00001755
1756 }
1757
1758 }
1759 }
1760}
1761/**
Daniel Veillard4255d502002-04-16 15:50:10 +00001762 * xmlFAEliminateEpsilonTransitions:
Daniel Veillard441bc322002-04-20 17:38:48 +00001763 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00001764 *
1765 */
1766static void
1767xmlFAEliminateEpsilonTransitions(xmlRegParserCtxtPtr ctxt) {
1768 int statenr, transnr;
1769 xmlRegStatePtr state;
Daniel Veillarddb68b742005-07-30 13:18:24 +00001770 int has_epsilon;
Daniel Veillard4255d502002-04-16 15:50:10 +00001771
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001772 if (ctxt->states == NULL) return;
1773
Daniel Veillard0e05f4c2006-11-01 15:33:04 +00001774 /*
1775 * Eliminate simple epsilon transition and the associated unreachable
1776 * states.
1777 */
Daniel Veillarddb68b742005-07-30 13:18:24 +00001778 xmlFAEliminateSimpleEpsilonTransitions(ctxt);
Daniel Veillard0e05f4c2006-11-01 15:33:04 +00001779 for (statenr = 0;statenr < ctxt->nbStates;statenr++) {
1780 state = ctxt->states[statenr];
1781 if ((state != NULL) && (state->type == XML_REGEXP_UNREACH_STATE)) {
1782#ifdef DEBUG_REGEXP_GRAPH
1783 printf("Removed unreachable state %d\n", statenr);
1784#endif
1785 xmlRegFreeState(state);
1786 ctxt->states[statenr] = NULL;
1787 }
1788 }
Daniel Veillarddb68b742005-07-30 13:18:24 +00001789
1790 has_epsilon = 0;
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001791
Daniel Veillard4255d502002-04-16 15:50:10 +00001792 /*
Daniel Veillardfcd18ff2006-11-02 10:28:04 +00001793 * Build the completed transitions bypassing the epsilons
Daniel Veillard4255d502002-04-16 15:50:10 +00001794 * Use a marking algorithm to avoid loops
Daniel Veillardfcd18ff2006-11-02 10:28:04 +00001795 * Mark sink states too.
1796 * Process from the latests states backward to the start when
1797 * there is long cascading epsilon chains this minimize the
1798 * recursions and transition compares when adding the new ones
Daniel Veillard4255d502002-04-16 15:50:10 +00001799 */
Daniel Veillardfcd18ff2006-11-02 10:28:04 +00001800 for (statenr = ctxt->nbStates - 1;statenr >= 0;statenr--) {
Daniel Veillard4255d502002-04-16 15:50:10 +00001801 state = ctxt->states[statenr];
1802 if (state == NULL)
1803 continue;
Daniel Veillardcc026dc2005-01-12 13:21:17 +00001804 if ((state->nbTrans == 0) &&
1805 (state->type != XML_REGEXP_FINAL_STATE)) {
1806 state->type = XML_REGEXP_SINK_STATE;
1807 }
Daniel Veillard4255d502002-04-16 15:50:10 +00001808 for (transnr = 0;transnr < state->nbTrans;transnr++) {
1809 if ((state->trans[transnr].atom == NULL) &&
1810 (state->trans[transnr].to >= 0)) {
1811 if (state->trans[transnr].to == statenr) {
1812 state->trans[transnr].to = -1;
1813#ifdef DEBUG_REGEXP_GRAPH
1814 printf("Removed loopback epsilon trans %d on %d\n",
1815 transnr, statenr);
1816#endif
1817 } else if (state->trans[transnr].count < 0) {
1818 int newto = state->trans[transnr].to;
1819
1820#ifdef DEBUG_REGEXP_GRAPH
1821 printf("Found epsilon trans %d from %d to %d\n",
1822 transnr, statenr, newto);
1823#endif
Daniel Veillarddb68b742005-07-30 13:18:24 +00001824 has_epsilon = 1;
Daniel Veillard0e05f4c2006-11-01 15:33:04 +00001825 state->trans[transnr].to = -2;
1826 state->mark = XML_REGEXP_MARK_START;
Daniel Veillard4255d502002-04-16 15:50:10 +00001827 xmlFAReduceEpsilonTransitions(ctxt, statenr,
1828 newto, state->trans[transnr].counter);
1829 state->mark = XML_REGEXP_MARK_NORMAL;
1830#ifdef DEBUG_REGEXP_GRAPH
1831 } else {
1832 printf("Found counted transition %d on %d\n",
1833 transnr, statenr);
1834#endif
1835 }
1836 }
1837 }
1838 }
1839 /*
1840 * Eliminate the epsilon transitions
1841 */
Daniel Veillarddb68b742005-07-30 13:18:24 +00001842 if (has_epsilon) {
1843 for (statenr = 0;statenr < ctxt->nbStates;statenr++) {
1844 state = ctxt->states[statenr];
1845 if (state == NULL)
1846 continue;
1847 for (transnr = 0;transnr < state->nbTrans;transnr++) {
1848 xmlRegTransPtr trans = &(state->trans[transnr]);
1849 if ((trans->atom == NULL) &&
1850 (trans->count < 0) &&
1851 (trans->to >= 0)) {
1852 trans->to = -1;
1853 }
Daniel Veillard4255d502002-04-16 15:50:10 +00001854 }
1855 }
1856 }
Daniel Veillard23e73572002-09-19 19:56:43 +00001857
1858 /*
1859 * Use this pass to detect unreachable states too
1860 */
1861 for (statenr = 0;statenr < ctxt->nbStates;statenr++) {
1862 state = ctxt->states[statenr];
1863 if (state != NULL)
William M. Brack779af002003-08-01 15:55:39 +00001864 state->reached = XML_REGEXP_MARK_NORMAL;
Daniel Veillard23e73572002-09-19 19:56:43 +00001865 }
1866 state = ctxt->states[0];
1867 if (state != NULL)
William M. Brack779af002003-08-01 15:55:39 +00001868 state->reached = XML_REGEXP_MARK_START;
Daniel Veillard23e73572002-09-19 19:56:43 +00001869 while (state != NULL) {
1870 xmlRegStatePtr target = NULL;
William M. Brack779af002003-08-01 15:55:39 +00001871 state->reached = XML_REGEXP_MARK_VISITED;
Daniel Veillard23e73572002-09-19 19:56:43 +00001872 /*
William M. Brackddf71d62004-05-06 04:17:26 +00001873 * Mark all states reachable from the current reachable state
Daniel Veillard23e73572002-09-19 19:56:43 +00001874 */
1875 for (transnr = 0;transnr < state->nbTrans;transnr++) {
1876 if ((state->trans[transnr].to >= 0) &&
1877 ((state->trans[transnr].atom != NULL) ||
1878 (state->trans[transnr].count >= 0))) {
1879 int newto = state->trans[transnr].to;
1880
1881 if (ctxt->states[newto] == NULL)
1882 continue;
William M. Brack779af002003-08-01 15:55:39 +00001883 if (ctxt->states[newto]->reached == XML_REGEXP_MARK_NORMAL) {
1884 ctxt->states[newto]->reached = XML_REGEXP_MARK_START;
Daniel Veillard23e73572002-09-19 19:56:43 +00001885 target = ctxt->states[newto];
1886 }
1887 }
1888 }
Daniel Veillardcc026dc2005-01-12 13:21:17 +00001889
Daniel Veillard23e73572002-09-19 19:56:43 +00001890 /*
1891 * find the next accessible state not explored
1892 */
1893 if (target == NULL) {
1894 for (statenr = 1;statenr < ctxt->nbStates;statenr++) {
1895 state = ctxt->states[statenr];
William M. Brack779af002003-08-01 15:55:39 +00001896 if ((state != NULL) && (state->reached ==
1897 XML_REGEXP_MARK_START)) {
Daniel Veillard23e73572002-09-19 19:56:43 +00001898 target = state;
1899 break;
1900 }
1901 }
1902 }
1903 state = target;
1904 }
1905 for (statenr = 0;statenr < ctxt->nbStates;statenr++) {
1906 state = ctxt->states[statenr];
William M. Brack779af002003-08-01 15:55:39 +00001907 if ((state != NULL) && (state->reached == XML_REGEXP_MARK_NORMAL)) {
Daniel Veillard23e73572002-09-19 19:56:43 +00001908#ifdef DEBUG_REGEXP_GRAPH
1909 printf("Removed unreachable state %d\n", statenr);
1910#endif
1911 xmlRegFreeState(state);
1912 ctxt->states[statenr] = NULL;
1913 }
1914 }
1915
Daniel Veillard4255d502002-04-16 15:50:10 +00001916}
1917
Daniel Veillard567a45b2005-10-18 19:11:55 +00001918static int
1919xmlFACompareRanges(xmlRegRangePtr range1, xmlRegRangePtr range2) {
1920 int ret = 0;
1921
1922 if ((range1->type == XML_REGEXP_RANGES) ||
1923 (range2->type == XML_REGEXP_RANGES) ||
1924 (range2->type == XML_REGEXP_SUBREG) ||
1925 (range1->type == XML_REGEXP_SUBREG) ||
1926 (range1->type == XML_REGEXP_STRING) ||
1927 (range2->type == XML_REGEXP_STRING))
1928 return(-1);
1929
1930 /* put them in order */
1931 if (range1->type > range2->type) {
1932 xmlRegRangePtr tmp;
1933
1934 tmp = range1;
1935 range1 = range2;
1936 range2 = tmp;
1937 }
1938 if ((range1->type == XML_REGEXP_ANYCHAR) ||
1939 (range2->type == XML_REGEXP_ANYCHAR)) {
1940 ret = 1;
1941 } else if ((range1->type == XML_REGEXP_EPSILON) ||
1942 (range2->type == XML_REGEXP_EPSILON)) {
1943 return(0);
1944 } else if (range1->type == range2->type) {
1945 if ((range1->type != XML_REGEXP_CHARVAL) ||
1946 (range1->end < range2->start) ||
1947 (range2->end < range1->start))
1948 ret = 1;
1949 else
1950 ret = 0;
1951 } else if (range1->type == XML_REGEXP_CHARVAL) {
1952 int codepoint;
1953 int neg = 0;
1954
1955 /*
1956 * just check all codepoints in the range for acceptance,
1957 * this is usually way cheaper since done only once at
1958 * compilation than testing over and over at runtime or
1959 * pushing too many states when evaluating.
1960 */
1961 if (((range1->neg == 0) && (range2->neg != 0)) ||
1962 ((range1->neg != 0) && (range2->neg == 0)))
1963 neg = 1;
1964
1965 for (codepoint = range1->start;codepoint <= range1->end ;codepoint++) {
1966 ret = xmlRegCheckCharacterRange(range2->type, codepoint,
1967 0, range2->start, range2->end,
1968 range2->blockName);
1969 if (ret < 0)
1970 return(-1);
1971 if (((neg == 1) && (ret == 0)) ||
1972 ((neg == 0) && (ret == 1)))
1973 return(1);
1974 }
1975 return(0);
1976 } else if ((range1->type == XML_REGEXP_BLOCK_NAME) ||
1977 (range2->type == XML_REGEXP_BLOCK_NAME)) {
1978 if (range1->type == range2->type) {
1979 ret = xmlStrEqual(range1->blockName, range2->blockName);
1980 } else {
1981 /*
1982 * comparing a block range with anything else is way
1983 * too costly, and maintining the table is like too much
1984 * memory too, so let's force the automata to save state
1985 * here.
1986 */
1987 return(1);
1988 }
1989 } else if ((range1->type < XML_REGEXP_LETTER) ||
1990 (range2->type < XML_REGEXP_LETTER)) {
1991 if ((range1->type == XML_REGEXP_ANYSPACE) &&
1992 (range2->type == XML_REGEXP_NOTSPACE))
1993 ret = 0;
1994 else if ((range1->type == XML_REGEXP_INITNAME) &&
1995 (range2->type == XML_REGEXP_NOTINITNAME))
1996 ret = 0;
1997 else if ((range1->type == XML_REGEXP_NAMECHAR) &&
1998 (range2->type == XML_REGEXP_NOTNAMECHAR))
1999 ret = 0;
2000 else if ((range1->type == XML_REGEXP_DECIMAL) &&
2001 (range2->type == XML_REGEXP_NOTDECIMAL))
2002 ret = 0;
2003 else if ((range1->type == XML_REGEXP_REALCHAR) &&
2004 (range2->type == XML_REGEXP_NOTREALCHAR))
2005 ret = 0;
2006 else {
2007 /* same thing to limit complexity */
2008 return(1);
2009 }
2010 } else {
2011 ret = 0;
2012 /* range1->type < range2->type here */
2013 switch (range1->type) {
2014 case XML_REGEXP_LETTER:
2015 /* all disjoint except in the subgroups */
2016 if ((range2->type == XML_REGEXP_LETTER_UPPERCASE) ||
2017 (range2->type == XML_REGEXP_LETTER_LOWERCASE) ||
2018 (range2->type == XML_REGEXP_LETTER_TITLECASE) ||
2019 (range2->type == XML_REGEXP_LETTER_MODIFIER) ||
2020 (range2->type == XML_REGEXP_LETTER_OTHERS))
2021 ret = 1;
2022 break;
2023 case XML_REGEXP_MARK:
2024 if ((range2->type == XML_REGEXP_MARK_NONSPACING) ||
2025 (range2->type == XML_REGEXP_MARK_SPACECOMBINING) ||
2026 (range2->type == XML_REGEXP_MARK_ENCLOSING))
2027 ret = 1;
2028 break;
2029 case XML_REGEXP_NUMBER:
2030 if ((range2->type == XML_REGEXP_NUMBER_DECIMAL) ||
2031 (range2->type == XML_REGEXP_NUMBER_LETTER) ||
2032 (range2->type == XML_REGEXP_NUMBER_OTHERS))
2033 ret = 1;
2034 break;
2035 case XML_REGEXP_PUNCT:
2036 if ((range2->type == XML_REGEXP_PUNCT_CONNECTOR) ||
2037 (range2->type == XML_REGEXP_PUNCT_DASH) ||
2038 (range2->type == XML_REGEXP_PUNCT_OPEN) ||
2039 (range2->type == XML_REGEXP_PUNCT_CLOSE) ||
2040 (range2->type == XML_REGEXP_PUNCT_INITQUOTE) ||
2041 (range2->type == XML_REGEXP_PUNCT_FINQUOTE) ||
2042 (range2->type == XML_REGEXP_PUNCT_OTHERS))
2043 ret = 1;
2044 break;
2045 case XML_REGEXP_SEPAR:
2046 if ((range2->type == XML_REGEXP_SEPAR_SPACE) ||
2047 (range2->type == XML_REGEXP_SEPAR_LINE) ||
2048 (range2->type == XML_REGEXP_SEPAR_PARA))
2049 ret = 1;
2050 break;
2051 case XML_REGEXP_SYMBOL:
2052 if ((range2->type == XML_REGEXP_SYMBOL_MATH) ||
2053 (range2->type == XML_REGEXP_SYMBOL_CURRENCY) ||
2054 (range2->type == XML_REGEXP_SYMBOL_MODIFIER) ||
2055 (range2->type == XML_REGEXP_SYMBOL_OTHERS))
2056 ret = 1;
2057 break;
2058 case XML_REGEXP_OTHER:
2059 if ((range2->type == XML_REGEXP_OTHER_CONTROL) ||
2060 (range2->type == XML_REGEXP_OTHER_FORMAT) ||
2061 (range2->type == XML_REGEXP_OTHER_PRIVATE))
2062 ret = 1;
2063 break;
2064 default:
2065 if ((range2->type >= XML_REGEXP_LETTER) &&
2066 (range2->type < XML_REGEXP_BLOCK_NAME))
2067 ret = 0;
2068 else {
2069 /* safety net ! */
2070 return(1);
2071 }
2072 }
2073 }
2074 if (((range1->neg == 0) && (range2->neg != 0)) ||
2075 ((range1->neg != 0) && (range2->neg == 0)))
2076 ret = !ret;
2077 return(1);
2078}
2079
Daniel Veillarde19fc232002-04-22 16:01:24 +00002080/**
Daniel Veillardfc011b72006-02-12 19:14:15 +00002081 * xmlFACompareAtomTypes:
2082 * @type1: an atom type
2083 * @type2: an atom type
2084 *
2085 * Compares two atoms type to check whether they intersect in some ways,
2086 * this is used by xmlFACompareAtoms only
2087 *
2088 * Returns 1 if they may intersect and 0 otherwise
2089 */
2090static int
2091xmlFACompareAtomTypes(xmlRegAtomType type1, xmlRegAtomType type2) {
2092 if ((type1 == XML_REGEXP_EPSILON) ||
2093 (type1 == XML_REGEXP_CHARVAL) ||
2094 (type1 == XML_REGEXP_RANGES) ||
2095 (type1 == XML_REGEXP_SUBREG) ||
2096 (type1 == XML_REGEXP_STRING) ||
2097 (type1 == XML_REGEXP_ANYCHAR))
2098 return(1);
2099 if ((type2 == XML_REGEXP_EPSILON) ||
2100 (type2 == XML_REGEXP_CHARVAL) ||
2101 (type2 == XML_REGEXP_RANGES) ||
2102 (type2 == XML_REGEXP_SUBREG) ||
2103 (type2 == XML_REGEXP_STRING) ||
2104 (type2 == XML_REGEXP_ANYCHAR))
2105 return(1);
2106
2107 if (type1 == type2) return(1);
2108
2109 /* simplify subsequent compares by making sure type1 < type2 */
2110 if (type1 > type2) {
2111 xmlRegAtomType tmp = type1;
2112 type1 = type2;
2113 type2 = tmp;
2114 }
2115 switch (type1) {
2116 case XML_REGEXP_ANYSPACE: /* \s */
2117 /* can't be a letter, number, mark, pontuation, symbol */
2118 if ((type2 == XML_REGEXP_NOTSPACE) ||
2119 ((type2 >= XML_REGEXP_LETTER) &&
2120 (type2 <= XML_REGEXP_LETTER_OTHERS)) ||
2121 ((type2 >= XML_REGEXP_NUMBER) &&
2122 (type2 <= XML_REGEXP_NUMBER_OTHERS)) ||
2123 ((type2 >= XML_REGEXP_MARK) &&
2124 (type2 <= XML_REGEXP_MARK_ENCLOSING)) ||
2125 ((type2 >= XML_REGEXP_PUNCT) &&
2126 (type2 <= XML_REGEXP_PUNCT_OTHERS)) ||
2127 ((type2 >= XML_REGEXP_SYMBOL) &&
2128 (type2 <= XML_REGEXP_SYMBOL_OTHERS))
2129 ) return(0);
2130 break;
2131 case XML_REGEXP_NOTSPACE: /* \S */
2132 break;
2133 case XML_REGEXP_INITNAME: /* \l */
2134 /* can't be a number, mark, separator, pontuation, symbol or other */
2135 if ((type2 == XML_REGEXP_NOTINITNAME) ||
2136 ((type2 >= XML_REGEXP_NUMBER) &&
2137 (type2 <= XML_REGEXP_NUMBER_OTHERS)) ||
2138 ((type2 >= XML_REGEXP_MARK) &&
2139 (type2 <= XML_REGEXP_MARK_ENCLOSING)) ||
2140 ((type2 >= XML_REGEXP_SEPAR) &&
2141 (type2 <= XML_REGEXP_SEPAR_PARA)) ||
2142 ((type2 >= XML_REGEXP_PUNCT) &&
2143 (type2 <= XML_REGEXP_PUNCT_OTHERS)) ||
2144 ((type2 >= XML_REGEXP_SYMBOL) &&
2145 (type2 <= XML_REGEXP_SYMBOL_OTHERS)) ||
2146 ((type2 >= XML_REGEXP_OTHER) &&
2147 (type2 <= XML_REGEXP_OTHER_NA))
2148 ) return(0);
2149 break;
2150 case XML_REGEXP_NOTINITNAME: /* \L */
2151 break;
2152 case XML_REGEXP_NAMECHAR: /* \c */
2153 /* can't be a mark, separator, pontuation, symbol or other */
2154 if ((type2 == XML_REGEXP_NOTNAMECHAR) ||
2155 ((type2 >= XML_REGEXP_MARK) &&
2156 (type2 <= XML_REGEXP_MARK_ENCLOSING)) ||
2157 ((type2 >= XML_REGEXP_PUNCT) &&
2158 (type2 <= XML_REGEXP_PUNCT_OTHERS)) ||
2159 ((type2 >= XML_REGEXP_SEPAR) &&
2160 (type2 <= XML_REGEXP_SEPAR_PARA)) ||
2161 ((type2 >= XML_REGEXP_SYMBOL) &&
2162 (type2 <= XML_REGEXP_SYMBOL_OTHERS)) ||
2163 ((type2 >= XML_REGEXP_OTHER) &&
2164 (type2 <= XML_REGEXP_OTHER_NA))
2165 ) return(0);
2166 break;
2167 case XML_REGEXP_NOTNAMECHAR: /* \C */
2168 break;
2169 case XML_REGEXP_DECIMAL: /* \d */
2170 /* can't be a letter, mark, separator, pontuation, symbol or other */
2171 if ((type2 == XML_REGEXP_NOTDECIMAL) ||
2172 (type2 == XML_REGEXP_REALCHAR) ||
2173 ((type2 >= XML_REGEXP_LETTER) &&
2174 (type2 <= XML_REGEXP_LETTER_OTHERS)) ||
2175 ((type2 >= XML_REGEXP_MARK) &&
2176 (type2 <= XML_REGEXP_MARK_ENCLOSING)) ||
2177 ((type2 >= XML_REGEXP_PUNCT) &&
2178 (type2 <= XML_REGEXP_PUNCT_OTHERS)) ||
2179 ((type2 >= XML_REGEXP_SEPAR) &&
2180 (type2 <= XML_REGEXP_SEPAR_PARA)) ||
2181 ((type2 >= XML_REGEXP_SYMBOL) &&
2182 (type2 <= XML_REGEXP_SYMBOL_OTHERS)) ||
2183 ((type2 >= XML_REGEXP_OTHER) &&
2184 (type2 <= XML_REGEXP_OTHER_NA))
2185 )return(0);
2186 break;
2187 case XML_REGEXP_NOTDECIMAL: /* \D */
2188 break;
2189 case XML_REGEXP_REALCHAR: /* \w */
2190 /* can't be a mark, separator, pontuation, symbol or other */
2191 if ((type2 == XML_REGEXP_NOTDECIMAL) ||
2192 ((type2 >= XML_REGEXP_MARK) &&
2193 (type2 <= XML_REGEXP_MARK_ENCLOSING)) ||
2194 ((type2 >= XML_REGEXP_PUNCT) &&
2195 (type2 <= XML_REGEXP_PUNCT_OTHERS)) ||
2196 ((type2 >= XML_REGEXP_SEPAR) &&
2197 (type2 <= XML_REGEXP_SEPAR_PARA)) ||
2198 ((type2 >= XML_REGEXP_SYMBOL) &&
2199 (type2 <= XML_REGEXP_SYMBOL_OTHERS)) ||
2200 ((type2 >= XML_REGEXP_OTHER) &&
2201 (type2 <= XML_REGEXP_OTHER_NA))
2202 )return(0);
2203 break;
2204 case XML_REGEXP_NOTREALCHAR: /* \W */
2205 break;
2206 /*
2207 * at that point we know both type 1 and type2 are from
2208 * character categories are ordered and are different,
2209 * it becomes simple because this is a partition
2210 */
2211 case XML_REGEXP_LETTER:
2212 if (type2 <= XML_REGEXP_LETTER_OTHERS)
2213 return(1);
2214 return(0);
2215 case XML_REGEXP_LETTER_UPPERCASE:
2216 case XML_REGEXP_LETTER_LOWERCASE:
2217 case XML_REGEXP_LETTER_TITLECASE:
2218 case XML_REGEXP_LETTER_MODIFIER:
2219 case XML_REGEXP_LETTER_OTHERS:
2220 return(0);
2221 case XML_REGEXP_MARK:
2222 if (type2 <= XML_REGEXP_MARK_ENCLOSING)
2223 return(1);
2224 return(0);
2225 case XML_REGEXP_MARK_NONSPACING:
2226 case XML_REGEXP_MARK_SPACECOMBINING:
2227 case XML_REGEXP_MARK_ENCLOSING:
2228 return(0);
2229 case XML_REGEXP_NUMBER:
2230 if (type2 <= XML_REGEXP_NUMBER_OTHERS)
2231 return(1);
2232 return(0);
2233 case XML_REGEXP_NUMBER_DECIMAL:
2234 case XML_REGEXP_NUMBER_LETTER:
2235 case XML_REGEXP_NUMBER_OTHERS:
2236 return(0);
2237 case XML_REGEXP_PUNCT:
2238 if (type2 <= XML_REGEXP_PUNCT_OTHERS)
2239 return(1);
2240 return(0);
2241 case XML_REGEXP_PUNCT_CONNECTOR:
2242 case XML_REGEXP_PUNCT_DASH:
2243 case XML_REGEXP_PUNCT_OPEN:
2244 case XML_REGEXP_PUNCT_CLOSE:
2245 case XML_REGEXP_PUNCT_INITQUOTE:
2246 case XML_REGEXP_PUNCT_FINQUOTE:
2247 case XML_REGEXP_PUNCT_OTHERS:
2248 return(0);
2249 case XML_REGEXP_SEPAR:
2250 if (type2 <= XML_REGEXP_SEPAR_PARA)
2251 return(1);
2252 return(0);
2253 case XML_REGEXP_SEPAR_SPACE:
2254 case XML_REGEXP_SEPAR_LINE:
2255 case XML_REGEXP_SEPAR_PARA:
2256 return(0);
2257 case XML_REGEXP_SYMBOL:
2258 if (type2 <= XML_REGEXP_SYMBOL_OTHERS)
2259 return(1);
2260 return(0);
2261 case XML_REGEXP_SYMBOL_MATH:
2262 case XML_REGEXP_SYMBOL_CURRENCY:
2263 case XML_REGEXP_SYMBOL_MODIFIER:
2264 case XML_REGEXP_SYMBOL_OTHERS:
2265 return(0);
2266 case XML_REGEXP_OTHER:
2267 if (type2 <= XML_REGEXP_OTHER_NA)
2268 return(1);
2269 return(0);
2270 case XML_REGEXP_OTHER_CONTROL:
2271 case XML_REGEXP_OTHER_FORMAT:
2272 case XML_REGEXP_OTHER_PRIVATE:
2273 case XML_REGEXP_OTHER_NA:
2274 return(0);
2275 default:
2276 break;
2277 }
2278 return(1);
2279}
2280
2281/**
2282 * xmlFAEqualAtoms:
Daniel Veillarde19fc232002-04-22 16:01:24 +00002283 * @atom1: an atom
2284 * @atom2: an atom
2285 *
Daniel Veillardfc011b72006-02-12 19:14:15 +00002286 * Compares two atoms to check whether they are the same exactly
2287 * this is used to remove equivalent transitions
Daniel Veillarde19fc232002-04-22 16:01:24 +00002288 *
Daniel Veillardfc011b72006-02-12 19:14:15 +00002289 * Returns 1 if same and 0 otherwise
Daniel Veillarde19fc232002-04-22 16:01:24 +00002290 */
2291static int
Daniel Veillardfc011b72006-02-12 19:14:15 +00002292xmlFAEqualAtoms(xmlRegAtomPtr atom1, xmlRegAtomPtr atom2) {
2293 int ret = 0;
Daniel Veillard9efc4762005-07-19 14:33:55 +00002294
Daniel Veillarde19fc232002-04-22 16:01:24 +00002295 if (atom1 == atom2)
2296 return(1);
2297 if ((atom1 == NULL) || (atom2 == NULL))
2298 return(0);
2299
Daniel Veillardfc011b72006-02-12 19:14:15 +00002300 if (atom1->type != atom2->type)
2301 return(0);
2302 switch (atom1->type) {
2303 case XML_REGEXP_EPSILON:
2304 ret = 0;
2305 break;
2306 case XML_REGEXP_STRING:
2307 ret = xmlStrEqual((xmlChar *)atom1->valuep,
2308 (xmlChar *)atom2->valuep);
2309 break;
2310 case XML_REGEXP_CHARVAL:
2311 ret = (atom1->codepoint == atom2->codepoint);
2312 break;
2313 case XML_REGEXP_RANGES:
2314 /* too hard to do in the general case */
2315 ret = 0;
2316 default:
2317 break;
2318 }
2319 return(ret);
2320}
2321
2322/**
2323 * xmlFACompareAtoms:
2324 * @atom1: an atom
2325 * @atom2: an atom
2326 *
2327 * Compares two atoms to check whether they intersect in some ways,
2328 * this is used by xmlFAComputesDeterminism and xmlFARecurseDeterminism only
2329 *
2330 * Returns 1 if yes and 0 otherwise
2331 */
2332static int
2333xmlFACompareAtoms(xmlRegAtomPtr atom1, xmlRegAtomPtr atom2) {
2334 int ret = 1;
2335
2336 if (atom1 == atom2)
2337 return(1);
2338 if ((atom1 == NULL) || (atom2 == NULL))
2339 return(0);
2340
2341 if ((atom1->type == XML_REGEXP_ANYCHAR) ||
2342 (atom2->type == XML_REGEXP_ANYCHAR))
2343 return(1);
2344
2345 if (atom1->type > atom2->type) {
Daniel Veillard567a45b2005-10-18 19:11:55 +00002346 xmlRegAtomPtr tmp;
2347 tmp = atom1;
2348 atom1 = atom2;
2349 atom2 = tmp;
Daniel Veillardfc011b72006-02-12 19:14:15 +00002350 }
2351 if (atom1->type != atom2->type) {
2352 ret = xmlFACompareAtomTypes(atom1->type, atom2->type);
2353 /* if they can't intersect at the type level break now */
2354 if (ret == 0)
2355 return(0);
Daniel Veillard567a45b2005-10-18 19:11:55 +00002356 }
Daniel Veillarde19fc232002-04-22 16:01:24 +00002357 switch (atom1->type) {
2358 case XML_REGEXP_STRING:
Daniel Veillard9efc4762005-07-19 14:33:55 +00002359 ret = xmlRegStrEqualWildcard((xmlChar *)atom1->valuep,
2360 (xmlChar *)atom2->valuep);
2361 break;
Daniel Veillarde19fc232002-04-22 16:01:24 +00002362 case XML_REGEXP_EPSILON:
Daniel Veillard567a45b2005-10-18 19:11:55 +00002363 goto not_determinist;
Daniel Veillarde19fc232002-04-22 16:01:24 +00002364 case XML_REGEXP_CHARVAL:
Daniel Veillardfc011b72006-02-12 19:14:15 +00002365 if (atom2->type == XML_REGEXP_CHARVAL) {
2366 ret = (atom1->codepoint == atom2->codepoint);
2367 } else {
2368 ret = xmlRegCheckCharacter(atom2, atom1->codepoint);
2369 if (ret < 0)
2370 ret = 1;
2371 }
Daniel Veillard9efc4762005-07-19 14:33:55 +00002372 break;
Daniel Veillarde19fc232002-04-22 16:01:24 +00002373 case XML_REGEXP_RANGES:
Daniel Veillardfc011b72006-02-12 19:14:15 +00002374 if (atom2->type == XML_REGEXP_RANGES) {
Daniel Veillard567a45b2005-10-18 19:11:55 +00002375 int i, j, res;
2376 xmlRegRangePtr r1, r2;
2377
2378 /*
2379 * need to check that none of the ranges eventually matches
2380 */
2381 for (i = 0;i < atom1->nbRanges;i++) {
2382 for (j = 0;j < atom2->nbRanges;j++) {
2383 r1 = atom1->ranges[i];
2384 r2 = atom2->ranges[j];
2385 res = xmlFACompareRanges(r1, r2);
2386 if (res == 1) {
2387 ret = 1;
2388 goto done;
2389 }
2390 }
2391 }
2392 ret = 0;
2393 }
2394 break;
Daniel Veillarde19fc232002-04-22 16:01:24 +00002395 default:
Daniel Veillard567a45b2005-10-18 19:11:55 +00002396 goto not_determinist;
Daniel Veillarde19fc232002-04-22 16:01:24 +00002397 }
Daniel Veillard567a45b2005-10-18 19:11:55 +00002398done:
Daniel Veillard6e65e152005-08-09 11:09:52 +00002399 if (atom1->neg != atom2->neg) {
Daniel Veillard9efc4762005-07-19 14:33:55 +00002400 ret = !ret;
Daniel Veillard6e65e152005-08-09 11:09:52 +00002401 }
Daniel Veillard567a45b2005-10-18 19:11:55 +00002402 if (ret == 0)
2403 return(0);
2404not_determinist:
2405 return(1);
Daniel Veillarde19fc232002-04-22 16:01:24 +00002406}
2407
2408/**
2409 * xmlFARecurseDeterminism:
2410 * @ctxt: a regexp parser context
2411 *
2412 * Check whether the associated regexp is determinist,
2413 * should be called after xmlFAEliminateEpsilonTransitions()
2414 *
2415 */
2416static int
2417xmlFARecurseDeterminism(xmlRegParserCtxtPtr ctxt, xmlRegStatePtr state,
2418 int to, xmlRegAtomPtr atom) {
2419 int ret = 1;
Daniel Veillard567a45b2005-10-18 19:11:55 +00002420 int res;
Daniel Veillard5de09382005-09-26 17:18:17 +00002421 int transnr, nbTrans;
Daniel Veillarde19fc232002-04-22 16:01:24 +00002422 xmlRegTransPtr t1;
2423
2424 if (state == NULL)
2425 return(ret);
Daniel Veillard5de09382005-09-26 17:18:17 +00002426 /*
2427 * don't recurse on transitions potentially added in the course of
2428 * the elimination.
2429 */
2430 nbTrans = state->nbTrans;
2431 for (transnr = 0;transnr < nbTrans;transnr++) {
Daniel Veillarde19fc232002-04-22 16:01:24 +00002432 t1 = &(state->trans[transnr]);
2433 /*
2434 * check transitions conflicting with the one looked at
2435 */
2436 if (t1->atom == NULL) {
Daniel Veillard0e05f4c2006-11-01 15:33:04 +00002437 if (t1->to < 0)
Daniel Veillarde19fc232002-04-22 16:01:24 +00002438 continue;
Daniel Veillard567a45b2005-10-18 19:11:55 +00002439 res = xmlFARecurseDeterminism(ctxt, ctxt->states[t1->to],
Daniel Veillarde19fc232002-04-22 16:01:24 +00002440 to, atom);
Daniel Veillard567a45b2005-10-18 19:11:55 +00002441 if (res == 0) {
2442 ret = 0;
Daniel Veillardaa622012005-10-20 15:55:25 +00002443 /* t1->nd = 1; */
Daniel Veillard567a45b2005-10-18 19:11:55 +00002444 }
Daniel Veillarde19fc232002-04-22 16:01:24 +00002445 continue;
2446 }
2447 if (t1->to != to)
2448 continue;
Daniel Veillard567a45b2005-10-18 19:11:55 +00002449 if (xmlFACompareAtoms(t1->atom, atom)) {
2450 ret = 0;
2451 /* mark the transition as non-deterministic */
2452 t1->nd = 1;
2453 }
Daniel Veillarde19fc232002-04-22 16:01:24 +00002454 }
2455 return(ret);
2456}
2457
2458/**
2459 * xmlFAComputesDeterminism:
2460 * @ctxt: a regexp parser context
2461 *
2462 * Check whether the associated regexp is determinist,
2463 * should be called after xmlFAEliminateEpsilonTransitions()
2464 *
2465 */
2466static int
2467xmlFAComputesDeterminism(xmlRegParserCtxtPtr ctxt) {
2468 int statenr, transnr;
2469 xmlRegStatePtr state;
Daniel Veillard567a45b2005-10-18 19:11:55 +00002470 xmlRegTransPtr t1, t2, last;
Daniel Veillarde19fc232002-04-22 16:01:24 +00002471 int i;
2472 int ret = 1;
2473
Daniel Veillard4402ab42002-09-12 16:02:56 +00002474#ifdef DEBUG_REGEXP_GRAPH
2475 printf("xmlFAComputesDeterminism\n");
2476 xmlRegPrintCtxt(stdout, ctxt);
2477#endif
Daniel Veillarde19fc232002-04-22 16:01:24 +00002478 if (ctxt->determinist != -1)
2479 return(ctxt->determinist);
2480
2481 /*
Daniel Veillard567a45b2005-10-18 19:11:55 +00002482 * First cleanup the automata removing cancelled transitions
Daniel Veillarde19fc232002-04-22 16:01:24 +00002483 */
2484 for (statenr = 0;statenr < ctxt->nbStates;statenr++) {
2485 state = ctxt->states[statenr];
2486 if (state == NULL)
2487 continue;
Daniel Veillard4f82c8a2005-08-09 21:40:08 +00002488 if (state->nbTrans < 2)
2489 continue;
Daniel Veillarde19fc232002-04-22 16:01:24 +00002490 for (transnr = 0;transnr < state->nbTrans;transnr++) {
2491 t1 = &(state->trans[transnr]);
2492 /*
2493 * Determinism checks in case of counted or all transitions
2494 * will have to be handled separately
2495 */
Daniel Veillard567a45b2005-10-18 19:11:55 +00002496 if (t1->atom == NULL) {
Daniel Veillardaa622012005-10-20 15:55:25 +00002497 /* t1->nd = 1; */
Daniel Veillarde19fc232002-04-22 16:01:24 +00002498 continue;
Daniel Veillard567a45b2005-10-18 19:11:55 +00002499 }
Daniel Veillarde19fc232002-04-22 16:01:24 +00002500 if (t1->to == -1) /* eliminated */
2501 continue;
2502 for (i = 0;i < transnr;i++) {
2503 t2 = &(state->trans[i]);
2504 if (t2->to == -1) /* eliminated */
2505 continue;
2506 if (t2->atom != NULL) {
2507 if (t1->to == t2->to) {
Daniel Veillardfc011b72006-02-12 19:14:15 +00002508 if (xmlFAEqualAtoms(t1->atom, t2->atom))
William M. Brackddf71d62004-05-06 04:17:26 +00002509 t2->to = -1; /* eliminated */
Daniel Veillard567a45b2005-10-18 19:11:55 +00002510 }
2511 }
2512 }
2513 }
2514 }
2515
2516 /*
2517 * Check for all states that there aren't 2 transitions
2518 * with the same atom and a different target.
2519 */
2520 for (statenr = 0;statenr < ctxt->nbStates;statenr++) {
2521 state = ctxt->states[statenr];
2522 if (state == NULL)
2523 continue;
2524 if (state->nbTrans < 2)
2525 continue;
2526 last = NULL;
2527 for (transnr = 0;transnr < state->nbTrans;transnr++) {
2528 t1 = &(state->trans[transnr]);
2529 /*
2530 * Determinism checks in case of counted or all transitions
2531 * will have to be handled separately
2532 */
2533 if (t1->atom == NULL) {
2534 continue;
2535 }
2536 if (t1->to == -1) /* eliminated */
2537 continue;
2538 for (i = 0;i < transnr;i++) {
2539 t2 = &(state->trans[i]);
2540 if (t2->to == -1) /* eliminated */
2541 continue;
2542 if (t2->atom != NULL) {
2543 /* not determinist ! */
2544 if (xmlFACompareAtoms(t1->atom, t2->atom)) {
2545 ret = 0;
2546 /* mark the transitions as non-deterministic ones */
2547 t1->nd = 1;
2548 t2->nd = 1;
2549 last = t1;
Daniel Veillarde19fc232002-04-22 16:01:24 +00002550 }
2551 } else if (t1->to != -1) {
2552 /*
2553 * do the closure in case of remaining specific
2554 * epsilon transitions like choices or all
2555 */
2556 ret = xmlFARecurseDeterminism(ctxt, ctxt->states[t1->to],
2557 t2->to, t2->atom);
Daniel Veillard567a45b2005-10-18 19:11:55 +00002558 /* don't shortcut the computation so all non deterministic
2559 transition get marked down
Daniel Veillarde19fc232002-04-22 16:01:24 +00002560 if (ret == 0)
Daniel Veillardaa622012005-10-20 15:55:25 +00002561 return(0);
2562 */
Daniel Veillard567a45b2005-10-18 19:11:55 +00002563 if (ret == 0) {
2564 t1->nd = 1;
Daniel Veillardaa622012005-10-20 15:55:25 +00002565 /* t2->nd = 1; */
Daniel Veillard567a45b2005-10-18 19:11:55 +00002566 last = t1;
2567 }
Daniel Veillarde19fc232002-04-22 16:01:24 +00002568 }
2569 }
Daniel Veillard567a45b2005-10-18 19:11:55 +00002570 /* don't shortcut the computation so all non deterministic
2571 transition get marked down
Daniel Veillarde19fc232002-04-22 16:01:24 +00002572 if (ret == 0)
Daniel Veillard567a45b2005-10-18 19:11:55 +00002573 break; */
Daniel Veillarde19fc232002-04-22 16:01:24 +00002574 }
Daniel Veillard567a45b2005-10-18 19:11:55 +00002575
2576 /*
2577 * mark specifically the last non-deterministic transition
2578 * from a state since there is no need to set-up rollback
2579 * from it
2580 */
2581 if (last != NULL) {
2582 last->nd = 2;
2583 }
2584
2585 /* don't shortcut the computation so all non deterministic
2586 transition get marked down
Daniel Veillarde19fc232002-04-22 16:01:24 +00002587 if (ret == 0)
Daniel Veillard567a45b2005-10-18 19:11:55 +00002588 break; */
Daniel Veillarde19fc232002-04-22 16:01:24 +00002589 }
Daniel Veillard567a45b2005-10-18 19:11:55 +00002590
Daniel Veillarde19fc232002-04-22 16:01:24 +00002591 ctxt->determinist = ret;
2592 return(ret);
2593}
2594
Daniel Veillard4255d502002-04-16 15:50:10 +00002595/************************************************************************
2596 * *
2597 * Routines to check input against transition atoms *
2598 * *
2599 ************************************************************************/
2600
2601static int
2602xmlRegCheckCharacterRange(xmlRegAtomType type, int codepoint, int neg,
2603 int start, int end, const xmlChar *blockName) {
2604 int ret = 0;
2605
2606 switch (type) {
2607 case XML_REGEXP_STRING:
2608 case XML_REGEXP_SUBREG:
2609 case XML_REGEXP_RANGES:
2610 case XML_REGEXP_EPSILON:
2611 return(-1);
2612 case XML_REGEXP_ANYCHAR:
2613 ret = ((codepoint != '\n') && (codepoint != '\r'));
2614 break;
2615 case XML_REGEXP_CHARVAL:
2616 ret = ((codepoint >= start) && (codepoint <= end));
2617 break;
2618 case XML_REGEXP_NOTSPACE:
2619 neg = !neg;
2620 case XML_REGEXP_ANYSPACE:
2621 ret = ((codepoint == '\n') || (codepoint == '\r') ||
2622 (codepoint == '\t') || (codepoint == ' '));
2623 break;
2624 case XML_REGEXP_NOTINITNAME:
2625 neg = !neg;
2626 case XML_REGEXP_INITNAME:
William M. Brack871611b2003-10-18 04:53:14 +00002627 ret = (IS_LETTER(codepoint) ||
Daniel Veillard4255d502002-04-16 15:50:10 +00002628 (codepoint == '_') || (codepoint == ':'));
2629 break;
2630 case XML_REGEXP_NOTNAMECHAR:
2631 neg = !neg;
2632 case XML_REGEXP_NAMECHAR:
William M. Brack871611b2003-10-18 04:53:14 +00002633 ret = (IS_LETTER(codepoint) || IS_DIGIT(codepoint) ||
Daniel Veillard4255d502002-04-16 15:50:10 +00002634 (codepoint == '.') || (codepoint == '-') ||
2635 (codepoint == '_') || (codepoint == ':') ||
William M. Brack871611b2003-10-18 04:53:14 +00002636 IS_COMBINING(codepoint) || IS_EXTENDER(codepoint));
Daniel Veillard4255d502002-04-16 15:50:10 +00002637 break;
2638 case XML_REGEXP_NOTDECIMAL:
2639 neg = !neg;
2640 case XML_REGEXP_DECIMAL:
2641 ret = xmlUCSIsCatNd(codepoint);
2642 break;
2643 case XML_REGEXP_REALCHAR:
2644 neg = !neg;
2645 case XML_REGEXP_NOTREALCHAR:
2646 ret = xmlUCSIsCatP(codepoint);
2647 if (ret == 0)
2648 ret = xmlUCSIsCatZ(codepoint);
2649 if (ret == 0)
2650 ret = xmlUCSIsCatC(codepoint);
2651 break;
2652 case XML_REGEXP_LETTER:
2653 ret = xmlUCSIsCatL(codepoint);
2654 break;
2655 case XML_REGEXP_LETTER_UPPERCASE:
2656 ret = xmlUCSIsCatLu(codepoint);
2657 break;
2658 case XML_REGEXP_LETTER_LOWERCASE:
2659 ret = xmlUCSIsCatLl(codepoint);
2660 break;
2661 case XML_REGEXP_LETTER_TITLECASE:
2662 ret = xmlUCSIsCatLt(codepoint);
2663 break;
2664 case XML_REGEXP_LETTER_MODIFIER:
2665 ret = xmlUCSIsCatLm(codepoint);
2666 break;
2667 case XML_REGEXP_LETTER_OTHERS:
2668 ret = xmlUCSIsCatLo(codepoint);
2669 break;
2670 case XML_REGEXP_MARK:
2671 ret = xmlUCSIsCatM(codepoint);
2672 break;
2673 case XML_REGEXP_MARK_NONSPACING:
2674 ret = xmlUCSIsCatMn(codepoint);
2675 break;
2676 case XML_REGEXP_MARK_SPACECOMBINING:
2677 ret = xmlUCSIsCatMc(codepoint);
2678 break;
2679 case XML_REGEXP_MARK_ENCLOSING:
2680 ret = xmlUCSIsCatMe(codepoint);
2681 break;
2682 case XML_REGEXP_NUMBER:
2683 ret = xmlUCSIsCatN(codepoint);
2684 break;
2685 case XML_REGEXP_NUMBER_DECIMAL:
2686 ret = xmlUCSIsCatNd(codepoint);
2687 break;
2688 case XML_REGEXP_NUMBER_LETTER:
2689 ret = xmlUCSIsCatNl(codepoint);
2690 break;
2691 case XML_REGEXP_NUMBER_OTHERS:
2692 ret = xmlUCSIsCatNo(codepoint);
2693 break;
2694 case XML_REGEXP_PUNCT:
2695 ret = xmlUCSIsCatP(codepoint);
2696 break;
2697 case XML_REGEXP_PUNCT_CONNECTOR:
2698 ret = xmlUCSIsCatPc(codepoint);
2699 break;
2700 case XML_REGEXP_PUNCT_DASH:
2701 ret = xmlUCSIsCatPd(codepoint);
2702 break;
2703 case XML_REGEXP_PUNCT_OPEN:
2704 ret = xmlUCSIsCatPs(codepoint);
2705 break;
2706 case XML_REGEXP_PUNCT_CLOSE:
2707 ret = xmlUCSIsCatPe(codepoint);
2708 break;
2709 case XML_REGEXP_PUNCT_INITQUOTE:
2710 ret = xmlUCSIsCatPi(codepoint);
2711 break;
2712 case XML_REGEXP_PUNCT_FINQUOTE:
2713 ret = xmlUCSIsCatPf(codepoint);
2714 break;
2715 case XML_REGEXP_PUNCT_OTHERS:
2716 ret = xmlUCSIsCatPo(codepoint);
2717 break;
2718 case XML_REGEXP_SEPAR:
2719 ret = xmlUCSIsCatZ(codepoint);
2720 break;
2721 case XML_REGEXP_SEPAR_SPACE:
2722 ret = xmlUCSIsCatZs(codepoint);
2723 break;
2724 case XML_REGEXP_SEPAR_LINE:
2725 ret = xmlUCSIsCatZl(codepoint);
2726 break;
2727 case XML_REGEXP_SEPAR_PARA:
2728 ret = xmlUCSIsCatZp(codepoint);
2729 break;
2730 case XML_REGEXP_SYMBOL:
2731 ret = xmlUCSIsCatS(codepoint);
2732 break;
2733 case XML_REGEXP_SYMBOL_MATH:
2734 ret = xmlUCSIsCatSm(codepoint);
2735 break;
2736 case XML_REGEXP_SYMBOL_CURRENCY:
2737 ret = xmlUCSIsCatSc(codepoint);
2738 break;
2739 case XML_REGEXP_SYMBOL_MODIFIER:
2740 ret = xmlUCSIsCatSk(codepoint);
2741 break;
2742 case XML_REGEXP_SYMBOL_OTHERS:
2743 ret = xmlUCSIsCatSo(codepoint);
2744 break;
2745 case XML_REGEXP_OTHER:
2746 ret = xmlUCSIsCatC(codepoint);
2747 break;
2748 case XML_REGEXP_OTHER_CONTROL:
2749 ret = xmlUCSIsCatCc(codepoint);
2750 break;
2751 case XML_REGEXP_OTHER_FORMAT:
2752 ret = xmlUCSIsCatCf(codepoint);
2753 break;
2754 case XML_REGEXP_OTHER_PRIVATE:
2755 ret = xmlUCSIsCatCo(codepoint);
2756 break;
2757 case XML_REGEXP_OTHER_NA:
2758 /* ret = xmlUCSIsCatCn(codepoint); */
2759 /* Seems it doesn't exist anymore in recent Unicode releases */
2760 ret = 0;
2761 break;
2762 case XML_REGEXP_BLOCK_NAME:
2763 ret = xmlUCSIsBlock(codepoint, (const char *) blockName);
2764 break;
2765 }
2766 if (neg)
2767 return(!ret);
2768 return(ret);
2769}
2770
2771static int
2772xmlRegCheckCharacter(xmlRegAtomPtr atom, int codepoint) {
2773 int i, ret = 0;
2774 xmlRegRangePtr range;
2775
William M. Brack871611b2003-10-18 04:53:14 +00002776 if ((atom == NULL) || (!IS_CHAR(codepoint)))
Daniel Veillard4255d502002-04-16 15:50:10 +00002777 return(-1);
2778
2779 switch (atom->type) {
2780 case XML_REGEXP_SUBREG:
2781 case XML_REGEXP_EPSILON:
2782 return(-1);
2783 case XML_REGEXP_CHARVAL:
2784 return(codepoint == atom->codepoint);
2785 case XML_REGEXP_RANGES: {
2786 int accept = 0;
Daniel Veillardf2a12832003-11-24 13:04:35 +00002787
Daniel Veillard4255d502002-04-16 15:50:10 +00002788 for (i = 0;i < atom->nbRanges;i++) {
2789 range = atom->ranges[i];
Daniel Veillardf8b9de32003-11-24 14:27:26 +00002790 if (range->neg == 2) {
Daniel Veillard4255d502002-04-16 15:50:10 +00002791 ret = xmlRegCheckCharacterRange(range->type, codepoint,
2792 0, range->start, range->end,
2793 range->blockName);
2794 if (ret != 0)
2795 return(0); /* excluded char */
Daniel Veillardf8b9de32003-11-24 14:27:26 +00002796 } else if (range->neg) {
2797 ret = xmlRegCheckCharacterRange(range->type, codepoint,
2798 0, range->start, range->end,
2799 range->blockName);
2800 if (ret == 0)
Daniel Veillardf2a12832003-11-24 13:04:35 +00002801 accept = 1;
Daniel Veillardf8b9de32003-11-24 14:27:26 +00002802 else
2803 return(0);
Daniel Veillard4255d502002-04-16 15:50:10 +00002804 } else {
2805 ret = xmlRegCheckCharacterRange(range->type, codepoint,
2806 0, range->start, range->end,
2807 range->blockName);
2808 if (ret != 0)
2809 accept = 1; /* might still be excluded */
2810 }
2811 }
2812 return(accept);
2813 }
2814 case XML_REGEXP_STRING:
2815 printf("TODO: XML_REGEXP_STRING\n");
2816 return(-1);
2817 case XML_REGEXP_ANYCHAR:
2818 case XML_REGEXP_ANYSPACE:
2819 case XML_REGEXP_NOTSPACE:
2820 case XML_REGEXP_INITNAME:
2821 case XML_REGEXP_NOTINITNAME:
2822 case XML_REGEXP_NAMECHAR:
2823 case XML_REGEXP_NOTNAMECHAR:
2824 case XML_REGEXP_DECIMAL:
2825 case XML_REGEXP_NOTDECIMAL:
2826 case XML_REGEXP_REALCHAR:
2827 case XML_REGEXP_NOTREALCHAR:
2828 case XML_REGEXP_LETTER:
2829 case XML_REGEXP_LETTER_UPPERCASE:
2830 case XML_REGEXP_LETTER_LOWERCASE:
2831 case XML_REGEXP_LETTER_TITLECASE:
2832 case XML_REGEXP_LETTER_MODIFIER:
2833 case XML_REGEXP_LETTER_OTHERS:
2834 case XML_REGEXP_MARK:
2835 case XML_REGEXP_MARK_NONSPACING:
2836 case XML_REGEXP_MARK_SPACECOMBINING:
2837 case XML_REGEXP_MARK_ENCLOSING:
2838 case XML_REGEXP_NUMBER:
2839 case XML_REGEXP_NUMBER_DECIMAL:
2840 case XML_REGEXP_NUMBER_LETTER:
2841 case XML_REGEXP_NUMBER_OTHERS:
2842 case XML_REGEXP_PUNCT:
2843 case XML_REGEXP_PUNCT_CONNECTOR:
2844 case XML_REGEXP_PUNCT_DASH:
2845 case XML_REGEXP_PUNCT_OPEN:
2846 case XML_REGEXP_PUNCT_CLOSE:
2847 case XML_REGEXP_PUNCT_INITQUOTE:
2848 case XML_REGEXP_PUNCT_FINQUOTE:
2849 case XML_REGEXP_PUNCT_OTHERS:
2850 case XML_REGEXP_SEPAR:
2851 case XML_REGEXP_SEPAR_SPACE:
2852 case XML_REGEXP_SEPAR_LINE:
2853 case XML_REGEXP_SEPAR_PARA:
2854 case XML_REGEXP_SYMBOL:
2855 case XML_REGEXP_SYMBOL_MATH:
2856 case XML_REGEXP_SYMBOL_CURRENCY:
2857 case XML_REGEXP_SYMBOL_MODIFIER:
2858 case XML_REGEXP_SYMBOL_OTHERS:
2859 case XML_REGEXP_OTHER:
2860 case XML_REGEXP_OTHER_CONTROL:
2861 case XML_REGEXP_OTHER_FORMAT:
2862 case XML_REGEXP_OTHER_PRIVATE:
2863 case XML_REGEXP_OTHER_NA:
2864 case XML_REGEXP_BLOCK_NAME:
2865 ret = xmlRegCheckCharacterRange(atom->type, codepoint, 0, 0, 0,
2866 (const xmlChar *)atom->valuep);
2867 if (atom->neg)
2868 ret = !ret;
2869 break;
2870 }
2871 return(ret);
2872}
2873
2874/************************************************************************
2875 * *
William M. Brackddf71d62004-05-06 04:17:26 +00002876 * Saving and restoring state of an execution context *
Daniel Veillard4255d502002-04-16 15:50:10 +00002877 * *
2878 ************************************************************************/
2879
2880#ifdef DEBUG_REGEXP_EXEC
2881static void
2882xmlFARegDebugExec(xmlRegExecCtxtPtr exec) {
2883 printf("state: %d:%d:idx %d", exec->state->no, exec->transno, exec->index);
2884 if (exec->inputStack != NULL) {
2885 int i;
2886 printf(": ");
2887 for (i = 0;(i < 3) && (i < exec->inputStackNr);i++)
Daniel Veillard0e05f4c2006-11-01 15:33:04 +00002888 printf("%s ", (const char *)
2889 exec->inputStack[exec->inputStackNr - (i + 1)].value);
Daniel Veillard4255d502002-04-16 15:50:10 +00002890 } else {
2891 printf(": %s", &(exec->inputString[exec->index]));
2892 }
2893 printf("\n");
2894}
2895#endif
2896
2897static void
2898xmlFARegExecSave(xmlRegExecCtxtPtr exec) {
2899#ifdef DEBUG_REGEXP_EXEC
2900 printf("saving ");
2901 exec->transno++;
2902 xmlFARegDebugExec(exec);
2903 exec->transno--;
2904#endif
Daniel Veillard94cc1032005-09-15 13:09:00 +00002905#ifdef MAX_PUSH
2906 if (exec->nbPush > MAX_PUSH) {
2907 return;
2908 }
2909 exec->nbPush++;
2910#endif
Daniel Veillard4255d502002-04-16 15:50:10 +00002911
2912 if (exec->maxRollbacks == 0) {
2913 exec->maxRollbacks = 4;
2914 exec->rollbacks = (xmlRegExecRollback *) xmlMalloc(exec->maxRollbacks *
2915 sizeof(xmlRegExecRollback));
2916 if (exec->rollbacks == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00002917 xmlRegexpErrMemory(NULL, "saving regexp");
Daniel Veillard4255d502002-04-16 15:50:10 +00002918 exec->maxRollbacks = 0;
2919 return;
2920 }
2921 memset(exec->rollbacks, 0,
2922 exec->maxRollbacks * sizeof(xmlRegExecRollback));
2923 } else if (exec->nbRollbacks >= exec->maxRollbacks) {
2924 xmlRegExecRollback *tmp;
2925 int len = exec->maxRollbacks;
2926
2927 exec->maxRollbacks *= 2;
2928 tmp = (xmlRegExecRollback *) xmlRealloc(exec->rollbacks,
2929 exec->maxRollbacks * sizeof(xmlRegExecRollback));
2930 if (tmp == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00002931 xmlRegexpErrMemory(NULL, "saving regexp");
Daniel Veillard4255d502002-04-16 15:50:10 +00002932 exec->maxRollbacks /= 2;
2933 return;
2934 }
2935 exec->rollbacks = tmp;
2936 tmp = &exec->rollbacks[len];
2937 memset(tmp, 0, (exec->maxRollbacks - len) * sizeof(xmlRegExecRollback));
2938 }
2939 exec->rollbacks[exec->nbRollbacks].state = exec->state;
2940 exec->rollbacks[exec->nbRollbacks].index = exec->index;
2941 exec->rollbacks[exec->nbRollbacks].nextbranch = exec->transno + 1;
2942 if (exec->comp->nbCounters > 0) {
2943 if (exec->rollbacks[exec->nbRollbacks].counts == NULL) {
2944 exec->rollbacks[exec->nbRollbacks].counts = (int *)
2945 xmlMalloc(exec->comp->nbCounters * sizeof(int));
2946 if (exec->rollbacks[exec->nbRollbacks].counts == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00002947 xmlRegexpErrMemory(NULL, "saving regexp");
Daniel Veillard4255d502002-04-16 15:50:10 +00002948 exec->status = -5;
2949 return;
2950 }
2951 }
2952 memcpy(exec->rollbacks[exec->nbRollbacks].counts, exec->counts,
2953 exec->comp->nbCounters * sizeof(int));
2954 }
2955 exec->nbRollbacks++;
2956}
2957
2958static void
2959xmlFARegExecRollBack(xmlRegExecCtxtPtr exec) {
2960 if (exec->nbRollbacks <= 0) {
2961 exec->status = -1;
2962#ifdef DEBUG_REGEXP_EXEC
2963 printf("rollback failed on empty stack\n");
2964#endif
2965 return;
2966 }
2967 exec->nbRollbacks--;
2968 exec->state = exec->rollbacks[exec->nbRollbacks].state;
2969 exec->index = exec->rollbacks[exec->nbRollbacks].index;
2970 exec->transno = exec->rollbacks[exec->nbRollbacks].nextbranch;
2971 if (exec->comp->nbCounters > 0) {
2972 if (exec->rollbacks[exec->nbRollbacks].counts == NULL) {
2973 fprintf(stderr, "exec save: allocation failed");
2974 exec->status = -6;
2975 return;
2976 }
2977 memcpy(exec->counts, exec->rollbacks[exec->nbRollbacks].counts,
2978 exec->comp->nbCounters * sizeof(int));
2979 }
2980
2981#ifdef DEBUG_REGEXP_EXEC
2982 printf("restored ");
2983 xmlFARegDebugExec(exec);
2984#endif
2985}
2986
2987/************************************************************************
2988 * *
William M. Brackddf71d62004-05-06 04:17:26 +00002989 * Verifier, running an input against a compiled regexp *
Daniel Veillard4255d502002-04-16 15:50:10 +00002990 * *
2991 ************************************************************************/
2992
2993static int
2994xmlFARegExec(xmlRegexpPtr comp, const xmlChar *content) {
2995 xmlRegExecCtxt execval;
2996 xmlRegExecCtxtPtr exec = &execval;
Daniel Veillard567a45b2005-10-18 19:11:55 +00002997 int ret, codepoint = 0, len, deter;
Daniel Veillard4255d502002-04-16 15:50:10 +00002998
2999 exec->inputString = content;
3000 exec->index = 0;
Daniel Veillard94cc1032005-09-15 13:09:00 +00003001 exec->nbPush = 0;
Daniel Veillard4255d502002-04-16 15:50:10 +00003002 exec->determinist = 1;
3003 exec->maxRollbacks = 0;
3004 exec->nbRollbacks = 0;
3005 exec->rollbacks = NULL;
3006 exec->status = 0;
3007 exec->comp = comp;
3008 exec->state = comp->states[0];
3009 exec->transno = 0;
3010 exec->transcount = 0;
Daniel Veillardf2a12832003-11-24 13:04:35 +00003011 exec->inputStack = NULL;
3012 exec->inputStackMax = 0;
Daniel Veillard4255d502002-04-16 15:50:10 +00003013 if (comp->nbCounters > 0) {
3014 exec->counts = (int *) xmlMalloc(comp->nbCounters * sizeof(int));
Daniel Veillardff46a042003-10-08 08:53:17 +00003015 if (exec->counts == NULL) {
3016 xmlRegexpErrMemory(NULL, "running regexp");
Daniel Veillard4255d502002-04-16 15:50:10 +00003017 return(-1);
Daniel Veillardff46a042003-10-08 08:53:17 +00003018 }
Daniel Veillard4255d502002-04-16 15:50:10 +00003019 memset(exec->counts, 0, comp->nbCounters * sizeof(int));
3020 } else
3021 exec->counts = NULL;
3022 while ((exec->status == 0) &&
3023 ((exec->inputString[exec->index] != 0) ||
3024 (exec->state->type != XML_REGEXP_FINAL_STATE))) {
3025 xmlRegTransPtr trans;
3026 xmlRegAtomPtr atom;
3027
3028 /*
William M. Brack0e00b282004-04-26 15:40:47 +00003029 * If end of input on non-terminal state, rollback, however we may
Daniel Veillard4255d502002-04-16 15:50:10 +00003030 * still have epsilon like transition for counted transitions
William M. Brack0e00b282004-04-26 15:40:47 +00003031 * on counters, in that case don't break too early. Additionally,
3032 * if we are working on a range like "AB{0,2}", where B is not present,
3033 * we don't want to break.
Daniel Veillard4255d502002-04-16 15:50:10 +00003034 */
Daniel Veillard11ce4002006-03-10 00:36:23 +00003035 len = 1;
William M. Brack0e00b282004-04-26 15:40:47 +00003036 if ((exec->inputString[exec->index] == 0) && (exec->counts == NULL)) {
William M. Brackddf71d62004-05-06 04:17:26 +00003037 /*
3038 * if there is a transition, we must check if
3039 * atom allows minOccurs of 0
3040 */
3041 if (exec->transno < exec->state->nbTrans) {
William M. Brack0e00b282004-04-26 15:40:47 +00003042 trans = &exec->state->trans[exec->transno];
3043 if (trans->to >=0) {
3044 atom = trans->atom;
3045 if (!((atom->min == 0) && (atom->max > 0)))
3046 goto rollback;
3047 }
3048 } else
3049 goto rollback;
3050 }
Daniel Veillard4255d502002-04-16 15:50:10 +00003051
3052 exec->transcount = 0;
3053 for (;exec->transno < exec->state->nbTrans;exec->transno++) {
3054 trans = &exec->state->trans[exec->transno];
3055 if (trans->to < 0)
3056 continue;
3057 atom = trans->atom;
3058 ret = 0;
Daniel Veillard567a45b2005-10-18 19:11:55 +00003059 deter = 1;
Daniel Veillard4255d502002-04-16 15:50:10 +00003060 if (trans->count >= 0) {
3061 int count;
3062 xmlRegCounterPtr counter;
3063
Daniel Veillard11ce4002006-03-10 00:36:23 +00003064 if (exec->counts == NULL) {
3065 exec->status = -1;
3066 goto error;
3067 }
Daniel Veillard4255d502002-04-16 15:50:10 +00003068 /*
3069 * A counted transition.
3070 */
3071
3072 count = exec->counts[trans->count];
3073 counter = &exec->comp->counters[trans->count];
3074#ifdef DEBUG_REGEXP_EXEC
3075 printf("testing count %d: val %d, min %d, max %d\n",
3076 trans->count, count, counter->min, counter->max);
3077#endif
3078 ret = ((count >= counter->min) && (count <= counter->max));
Daniel Veillard567a45b2005-10-18 19:11:55 +00003079 if ((ret) && (counter->min != counter->max))
3080 deter = 0;
Daniel Veillard4255d502002-04-16 15:50:10 +00003081 } else if (atom == NULL) {
3082 fprintf(stderr, "epsilon transition left at runtime\n");
3083 exec->status = -2;
3084 break;
3085 } else if (exec->inputString[exec->index] != 0) {
3086 codepoint = CUR_SCHAR(&(exec->inputString[exec->index]), len);
3087 ret = xmlRegCheckCharacter(atom, codepoint);
William M. Brack0e00b282004-04-26 15:40:47 +00003088 if ((ret == 1) && (atom->min >= 0) && (atom->max > 0)) {
Daniel Veillard4255d502002-04-16 15:50:10 +00003089 xmlRegStatePtr to = comp->states[trans->to];
3090
3091 /*
3092 * this is a multiple input sequence
Daniel Veillardfc6eca02005-11-01 15:24:02 +00003093 * If there is a counter associated increment it now.
3094 * before potentially saving and rollback
Daniel Veillard4255d502002-04-16 15:50:10 +00003095 */
Daniel Veillardfc6eca02005-11-01 15:24:02 +00003096 if (trans->counter >= 0) {
Daniel Veillard11ce4002006-03-10 00:36:23 +00003097 if (exec->counts == NULL) {
3098 exec->status = -1;
3099 goto error;
3100 }
Daniel Veillardfc6eca02005-11-01 15:24:02 +00003101#ifdef DEBUG_REGEXP_EXEC
3102 printf("Increasing count %d\n", trans->counter);
3103#endif
3104 exec->counts[trans->counter]++;
3105 }
Daniel Veillard4255d502002-04-16 15:50:10 +00003106 if (exec->state->nbTrans > exec->transno + 1) {
3107 xmlFARegExecSave(exec);
3108 }
3109 exec->transcount = 1;
3110 do {
3111 /*
3112 * Try to progress as much as possible on the input
3113 */
3114 if (exec->transcount == atom->max) {
3115 break;
3116 }
3117 exec->index += len;
3118 /*
3119 * End of input: stop here
3120 */
3121 if (exec->inputString[exec->index] == 0) {
3122 exec->index -= len;
3123 break;
3124 }
3125 if (exec->transcount >= atom->min) {
3126 int transno = exec->transno;
3127 xmlRegStatePtr state = exec->state;
3128
3129 /*
3130 * The transition is acceptable save it
3131 */
3132 exec->transno = -1; /* trick */
3133 exec->state = to;
3134 xmlFARegExecSave(exec);
3135 exec->transno = transno;
3136 exec->state = state;
3137 }
3138 codepoint = CUR_SCHAR(&(exec->inputString[exec->index]),
3139 len);
3140 ret = xmlRegCheckCharacter(atom, codepoint);
3141 exec->transcount++;
3142 } while (ret == 1);
3143 if (exec->transcount < atom->min)
3144 ret = 0;
3145
3146 /*
3147 * If the last check failed but one transition was found
3148 * possible, rollback
3149 */
3150 if (ret < 0)
3151 ret = 0;
3152 if (ret == 0) {
3153 goto rollback;
3154 }
Daniel Veillardfc6eca02005-11-01 15:24:02 +00003155 if (trans->counter >= 0) {
Daniel Veillard11ce4002006-03-10 00:36:23 +00003156 if (exec->counts == NULL) {
3157 exec->status = -1;
3158 goto error;
3159 }
Daniel Veillardfc6eca02005-11-01 15:24:02 +00003160#ifdef DEBUG_REGEXP_EXEC
3161 printf("Decreasing count %d\n", trans->counter);
3162#endif
3163 exec->counts[trans->counter]--;
3164 }
William M. Brack0e00b282004-04-26 15:40:47 +00003165 } else if ((ret == 0) && (atom->min == 0) && (atom->max > 0)) {
3166 /*
3167 * we don't match on the codepoint, but minOccurs of 0
3168 * says that's ok. Setting len to 0 inhibits stepping
3169 * over the codepoint.
3170 */
3171 exec->transcount = 1;
3172 len = 0;
3173 ret = 1;
Daniel Veillard4255d502002-04-16 15:50:10 +00003174 }
William M. Brack0e00b282004-04-26 15:40:47 +00003175 } else if ((atom->min == 0) && (atom->max > 0)) {
3176 /* another spot to match when minOccurs is 0 */
3177 exec->transcount = 1;
3178 len = 0;
3179 ret = 1;
Daniel Veillard4255d502002-04-16 15:50:10 +00003180 }
3181 if (ret == 1) {
Daniel Veillard567a45b2005-10-18 19:11:55 +00003182 if ((trans->nd == 1) ||
3183 ((trans->count >= 0) && (deter == 0) &&
3184 (exec->state->nbTrans > exec->transno + 1))) {
Daniel Veillardaa622012005-10-20 15:55:25 +00003185#ifdef DEBUG_REGEXP_EXEC
3186 if (trans->nd == 1)
3187 printf("Saving on nd transition atom %d for %c at %d\n",
3188 trans->atom->no, codepoint, exec->index);
3189 else
3190 printf("Saving on counted transition count %d for %c at %d\n",
3191 trans->count, codepoint, exec->index);
3192#endif
Daniel Veillard4255d502002-04-16 15:50:10 +00003193 xmlFARegExecSave(exec);
3194 }
3195 if (trans->counter >= 0) {
Daniel Veillard11ce4002006-03-10 00:36:23 +00003196 if (exec->counts == NULL) {
3197 exec->status = -1;
3198 goto error;
3199 }
Daniel Veillard4255d502002-04-16 15:50:10 +00003200#ifdef DEBUG_REGEXP_EXEC
3201 printf("Increasing count %d\n", trans->counter);
3202#endif
3203 exec->counts[trans->counter]++;
3204 }
Daniel Veillard10752282005-08-08 13:05:13 +00003205 if ((trans->count >= 0) &&
3206 (trans->count < REGEXP_ALL_COUNTER)) {
Daniel Veillard11ce4002006-03-10 00:36:23 +00003207 if (exec->counts == NULL) {
3208 exec->status = -1;
3209 goto error;
3210 }
Daniel Veillard10752282005-08-08 13:05:13 +00003211#ifdef DEBUG_REGEXP_EXEC
3212 printf("resetting count %d on transition\n",
3213 trans->count);
3214#endif
3215 exec->counts[trans->count] = 0;
3216 }
Daniel Veillard4255d502002-04-16 15:50:10 +00003217#ifdef DEBUG_REGEXP_EXEC
3218 printf("entering state %d\n", trans->to);
3219#endif
3220 exec->state = comp->states[trans->to];
3221 exec->transno = 0;
3222 if (trans->atom != NULL) {
3223 exec->index += len;
3224 }
3225 goto progress;
3226 } else if (ret < 0) {
3227 exec->status = -4;
3228 break;
3229 }
3230 }
3231 if ((exec->transno != 0) || (exec->state->nbTrans == 0)) {
3232rollback:
3233 /*
3234 * Failed to find a way out
3235 */
3236 exec->determinist = 0;
Daniel Veillardaa622012005-10-20 15:55:25 +00003237#ifdef DEBUG_REGEXP_EXEC
3238 printf("rollback from state %d on %d:%c\n", exec->state->no,
3239 codepoint,codepoint);
3240#endif
Daniel Veillard4255d502002-04-16 15:50:10 +00003241 xmlFARegExecRollBack(exec);
3242 }
3243progress:
3244 continue;
3245 }
Daniel Veillard11ce4002006-03-10 00:36:23 +00003246error:
Daniel Veillard4255d502002-04-16 15:50:10 +00003247 if (exec->rollbacks != NULL) {
3248 if (exec->counts != NULL) {
3249 int i;
3250
3251 for (i = 0;i < exec->maxRollbacks;i++)
3252 if (exec->rollbacks[i].counts != NULL)
3253 xmlFree(exec->rollbacks[i].counts);
3254 }
3255 xmlFree(exec->rollbacks);
3256 }
3257 if (exec->counts != NULL)
3258 xmlFree(exec->counts);
3259 if (exec->status == 0)
3260 return(1);
Daniel Veillard94cc1032005-09-15 13:09:00 +00003261 if (exec->status == -1) {
3262 if (exec->nbPush > MAX_PUSH)
3263 return(-1);
Daniel Veillard4255d502002-04-16 15:50:10 +00003264 return(0);
Daniel Veillard94cc1032005-09-15 13:09:00 +00003265 }
Daniel Veillard4255d502002-04-16 15:50:10 +00003266 return(exec->status);
3267}
3268
3269/************************************************************************
3270 * *
William M. Brackddf71d62004-05-06 04:17:26 +00003271 * Progressive interface to the verifier one atom at a time *
Daniel Veillard4255d502002-04-16 15:50:10 +00003272 * *
3273 ************************************************************************/
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003274#ifdef DEBUG_ERR
3275static void testerr(xmlRegExecCtxtPtr exec);
3276#endif
Daniel Veillard4255d502002-04-16 15:50:10 +00003277
3278/**
Daniel Veillard01c13b52002-12-10 15:19:08 +00003279 * xmlRegNewExecCtxt:
Daniel Veillard4255d502002-04-16 15:50:10 +00003280 * @comp: a precompiled regular expression
3281 * @callback: a callback function used for handling progresses in the
3282 * automata matching phase
3283 * @data: the context data associated to the callback in this context
3284 *
3285 * Build a context used for progressive evaluation of a regexp.
Daniel Veillard01c13b52002-12-10 15:19:08 +00003286 *
3287 * Returns the new context
Daniel Veillard4255d502002-04-16 15:50:10 +00003288 */
3289xmlRegExecCtxtPtr
3290xmlRegNewExecCtxt(xmlRegexpPtr comp, xmlRegExecCallbacks callback, void *data) {
3291 xmlRegExecCtxtPtr exec;
3292
3293 if (comp == NULL)
3294 return(NULL);
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00003295 if ((comp->compact == NULL) && (comp->states == NULL))
3296 return(NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +00003297 exec = (xmlRegExecCtxtPtr) xmlMalloc(sizeof(xmlRegExecCtxt));
3298 if (exec == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00003299 xmlRegexpErrMemory(NULL, "creating execution context");
Daniel Veillard4255d502002-04-16 15:50:10 +00003300 return(NULL);
3301 }
3302 memset(exec, 0, sizeof(xmlRegExecCtxt));
3303 exec->inputString = NULL;
3304 exec->index = 0;
3305 exec->determinist = 1;
3306 exec->maxRollbacks = 0;
3307 exec->nbRollbacks = 0;
3308 exec->rollbacks = NULL;
3309 exec->status = 0;
3310 exec->comp = comp;
Daniel Veillard23e73572002-09-19 19:56:43 +00003311 if (comp->compact == NULL)
3312 exec->state = comp->states[0];
Daniel Veillard4255d502002-04-16 15:50:10 +00003313 exec->transno = 0;
3314 exec->transcount = 0;
3315 exec->callback = callback;
3316 exec->data = data;
3317 if (comp->nbCounters > 0) {
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003318 /*
3319 * For error handling, exec->counts is allocated twice the size
3320 * the second half is used to store the data in case of rollback
3321 */
3322 exec->counts = (int *) xmlMalloc(comp->nbCounters * sizeof(int)
3323 * 2);
Daniel Veillard4255d502002-04-16 15:50:10 +00003324 if (exec->counts == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00003325 xmlRegexpErrMemory(NULL, "creating execution context");
Daniel Veillard4255d502002-04-16 15:50:10 +00003326 xmlFree(exec);
3327 return(NULL);
3328 }
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003329 memset(exec->counts, 0, comp->nbCounters * sizeof(int) * 2);
3330 exec->errCounts = &exec->counts[comp->nbCounters];
3331 } else {
Daniel Veillard4255d502002-04-16 15:50:10 +00003332 exec->counts = NULL;
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003333 exec->errCounts = NULL;
3334 }
Daniel Veillard4255d502002-04-16 15:50:10 +00003335 exec->inputStackMax = 0;
3336 exec->inputStackNr = 0;
3337 exec->inputStack = NULL;
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003338 exec->errStateNo = -1;
3339 exec->errString = NULL;
Daniel Veillard94cc1032005-09-15 13:09:00 +00003340 exec->nbPush = 0;
Daniel Veillard4255d502002-04-16 15:50:10 +00003341 return(exec);
3342}
3343
3344/**
3345 * xmlRegFreeExecCtxt:
3346 * @exec: a regular expression evaulation context
3347 *
3348 * Free the structures associated to a regular expression evaulation context.
3349 */
3350void
3351xmlRegFreeExecCtxt(xmlRegExecCtxtPtr exec) {
3352 if (exec == NULL)
3353 return;
3354
3355 if (exec->rollbacks != NULL) {
3356 if (exec->counts != NULL) {
3357 int i;
3358
3359 for (i = 0;i < exec->maxRollbacks;i++)
3360 if (exec->rollbacks[i].counts != NULL)
3361 xmlFree(exec->rollbacks[i].counts);
3362 }
3363 xmlFree(exec->rollbacks);
3364 }
3365 if (exec->counts != NULL)
3366 xmlFree(exec->counts);
3367 if (exec->inputStack != NULL) {
3368 int i;
3369
Daniel Veillard32370232002-10-16 14:08:14 +00003370 for (i = 0;i < exec->inputStackNr;i++) {
3371 if (exec->inputStack[i].value != NULL)
3372 xmlFree(exec->inputStack[i].value);
3373 }
Daniel Veillard4255d502002-04-16 15:50:10 +00003374 xmlFree(exec->inputStack);
3375 }
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003376 if (exec->errString != NULL)
3377 xmlFree(exec->errString);
Daniel Veillard4255d502002-04-16 15:50:10 +00003378 xmlFree(exec);
3379}
3380
3381static void
3382xmlFARegExecSaveInputString(xmlRegExecCtxtPtr exec, const xmlChar *value,
3383 void *data) {
3384#ifdef DEBUG_PUSH
3385 printf("saving value: %d:%s\n", exec->inputStackNr, value);
3386#endif
3387 if (exec->inputStackMax == 0) {
3388 exec->inputStackMax = 4;
3389 exec->inputStack = (xmlRegInputTokenPtr)
3390 xmlMalloc(exec->inputStackMax * sizeof(xmlRegInputToken));
3391 if (exec->inputStack == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00003392 xmlRegexpErrMemory(NULL, "pushing input string");
Daniel Veillard4255d502002-04-16 15:50:10 +00003393 exec->inputStackMax = 0;
3394 return;
3395 }
3396 } else if (exec->inputStackNr + 1 >= exec->inputStackMax) {
3397 xmlRegInputTokenPtr tmp;
3398
3399 exec->inputStackMax *= 2;
3400 tmp = (xmlRegInputTokenPtr) xmlRealloc(exec->inputStack,
3401 exec->inputStackMax * sizeof(xmlRegInputToken));
3402 if (tmp == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00003403 xmlRegexpErrMemory(NULL, "pushing input string");
Daniel Veillard4255d502002-04-16 15:50:10 +00003404 exec->inputStackMax /= 2;
3405 return;
3406 }
3407 exec->inputStack = tmp;
3408 }
3409 exec->inputStack[exec->inputStackNr].value = xmlStrdup(value);
3410 exec->inputStack[exec->inputStackNr].data = data;
3411 exec->inputStackNr++;
3412 exec->inputStack[exec->inputStackNr].value = NULL;
3413 exec->inputStack[exec->inputStackNr].data = NULL;
3414}
3415
Daniel Veillardc0826a72004-08-10 14:17:33 +00003416/**
3417 * xmlRegStrEqualWildcard:
3418 * @expStr: the string to be evaluated
3419 * @valStr: the validation string
3420 *
3421 * Checks if both strings are equal or have the same content. "*"
3422 * can be used as a wildcard in @valStr; "|" is used as a seperator of
3423 * substrings in both @expStr and @valStr.
3424 *
3425 * Returns 1 if the comparison is satisfied and the number of substrings
3426 * is equal, 0 otherwise.
3427 */
3428
3429static int
3430xmlRegStrEqualWildcard(const xmlChar *expStr, const xmlChar *valStr) {
3431 if (expStr == valStr) return(1);
3432 if (expStr == NULL) return(0);
3433 if (valStr == NULL) return(0);
3434 do {
3435 /*
3436 * Eval if we have a wildcard for the current item.
3437 */
3438 if (*expStr != *valStr) {
Daniel Veillard4f82c8a2005-08-09 21:40:08 +00003439 /* if one of them starts with a wildcard make valStr be it */
3440 if (*valStr == '*') {
3441 const xmlChar *tmp;
3442
3443 tmp = valStr;
3444 valStr = expStr;
3445 expStr = tmp;
3446 }
Daniel Veillardc0826a72004-08-10 14:17:33 +00003447 if ((*valStr != 0) && (*expStr != 0) && (*expStr++ == '*')) {
3448 do {
3449 if (*valStr == XML_REG_STRING_SEPARATOR)
3450 break;
Kasimier T. Buchcikc0e833f2005-04-19 15:02:20 +00003451 valStr++;
Daniel Veillardc0826a72004-08-10 14:17:33 +00003452 } while (*valStr != 0);
3453 continue;
3454 } else
3455 return(0);
3456 }
Kasimier T. Buchcikc0e833f2005-04-19 15:02:20 +00003457 expStr++;
3458 valStr++;
Daniel Veillardc0826a72004-08-10 14:17:33 +00003459 } while (*valStr != 0);
3460 if (*expStr != 0)
3461 return (0);
3462 else
3463 return (1);
3464}
Daniel Veillard4255d502002-04-16 15:50:10 +00003465
3466/**
Daniel Veillard23e73572002-09-19 19:56:43 +00003467 * xmlRegCompactPushString:
3468 * @exec: a regexp execution context
3469 * @comp: the precompiled exec with a compact table
3470 * @value: a string token input
3471 * @data: data associated to the token to reuse in callbacks
3472 *
3473 * Push one input token in the execution context
3474 *
3475 * Returns: 1 if the regexp reached a final state, 0 if non-final, and
3476 * a negative value in case of error.
3477 */
3478static int
3479xmlRegCompactPushString(xmlRegExecCtxtPtr exec,
3480 xmlRegexpPtr comp,
3481 const xmlChar *value,
3482 void *data) {
3483 int state = exec->index;
3484 int i, target;
3485
3486 if ((comp == NULL) || (comp->compact == NULL) || (comp->stringMap == NULL))
3487 return(-1);
3488
3489 if (value == NULL) {
3490 /*
3491 * are we at a final state ?
3492 */
3493 if (comp->compact[state * (comp->nbstrings + 1)] ==
3494 XML_REGEXP_FINAL_STATE)
3495 return(1);
3496 return(0);
3497 }
3498
3499#ifdef DEBUG_PUSH
3500 printf("value pushed: %s\n", value);
3501#endif
3502
3503 /*
William M. Brackddf71d62004-05-06 04:17:26 +00003504 * Examine all outside transitions from current state
Daniel Veillard23e73572002-09-19 19:56:43 +00003505 */
3506 for (i = 0;i < comp->nbstrings;i++) {
3507 target = comp->compact[state * (comp->nbstrings + 1) + i + 1];
3508 if ((target > 0) && (target <= comp->nbstates)) {
Daniel Veillardc0826a72004-08-10 14:17:33 +00003509 target--; /* to avoid 0 */
3510 if (xmlRegStrEqualWildcard(comp->stringMap[i], value)) {
3511 exec->index = target;
Daniel Veillard118aed72002-09-24 14:13:13 +00003512 if ((exec->callback != NULL) && (comp->transdata != NULL)) {
3513 exec->callback(exec->data, value,
3514 comp->transdata[state * comp->nbstrings + i], data);
3515 }
Daniel Veillard23e73572002-09-19 19:56:43 +00003516#ifdef DEBUG_PUSH
3517 printf("entering state %d\n", target);
3518#endif
3519 if (comp->compact[target * (comp->nbstrings + 1)] ==
Daniel Veillardcc026dc2005-01-12 13:21:17 +00003520 XML_REGEXP_SINK_STATE)
3521 goto error;
3522
3523 if (comp->compact[target * (comp->nbstrings + 1)] ==
Daniel Veillard23e73572002-09-19 19:56:43 +00003524 XML_REGEXP_FINAL_STATE)
3525 return(1);
3526 return(0);
3527 }
3528 }
3529 }
3530 /*
3531 * Failed to find an exit transition out from current state for the
3532 * current token
3533 */
3534#ifdef DEBUG_PUSH
3535 printf("failed to find a transition for %s on state %d\n", value, state);
3536#endif
Daniel Veillardcc026dc2005-01-12 13:21:17 +00003537error:
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003538 if (exec->errString != NULL)
3539 xmlFree(exec->errString);
3540 exec->errString = xmlStrdup(value);
3541 exec->errStateNo = state;
Daniel Veillard23e73572002-09-19 19:56:43 +00003542 exec->status = -1;
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003543#ifdef DEBUG_ERR
3544 testerr(exec);
3545#endif
Daniel Veillard23e73572002-09-19 19:56:43 +00003546 return(-1);
3547}
3548
3549/**
Daniel Veillard6e65e152005-08-09 11:09:52 +00003550 * xmlRegExecPushStringInternal:
Daniel Veillardea7751d2002-12-20 00:16:24 +00003551 * @exec: a regexp execution context or NULL to indicate the end
Daniel Veillard4255d502002-04-16 15:50:10 +00003552 * @value: a string token input
3553 * @data: data associated to the token to reuse in callbacks
Daniel Veillard6e65e152005-08-09 11:09:52 +00003554 * @compound: value was assembled from 2 strings
Daniel Veillard4255d502002-04-16 15:50:10 +00003555 *
3556 * Push one input token in the execution context
3557 *
3558 * Returns: 1 if the regexp reached a final state, 0 if non-final, and
3559 * a negative value in case of error.
3560 */
Daniel Veillard6e65e152005-08-09 11:09:52 +00003561static int
3562xmlRegExecPushStringInternal(xmlRegExecCtxtPtr exec, const xmlChar *value,
3563 void *data, int compound) {
Daniel Veillard4255d502002-04-16 15:50:10 +00003564 xmlRegTransPtr trans;
3565 xmlRegAtomPtr atom;
3566 int ret;
3567 int final = 0;
Daniel Veillard90700152005-01-08 22:05:09 +00003568 int progress = 1;
Daniel Veillard4255d502002-04-16 15:50:10 +00003569
3570 if (exec == NULL)
3571 return(-1);
Daniel Veillard23e73572002-09-19 19:56:43 +00003572 if (exec->comp == NULL)
3573 return(-1);
Daniel Veillard4255d502002-04-16 15:50:10 +00003574 if (exec->status != 0)
3575 return(exec->status);
3576
Daniel Veillard23e73572002-09-19 19:56:43 +00003577 if (exec->comp->compact != NULL)
3578 return(xmlRegCompactPushString(exec, exec->comp, value, data));
3579
Daniel Veillard4255d502002-04-16 15:50:10 +00003580 if (value == NULL) {
3581 if (exec->state->type == XML_REGEXP_FINAL_STATE)
3582 return(1);
3583 final = 1;
3584 }
3585
3586#ifdef DEBUG_PUSH
3587 printf("value pushed: %s\n", value);
3588#endif
3589 /*
3590 * If we have an active rollback stack push the new value there
3591 * and get back to where we were left
3592 */
3593 if ((value != NULL) && (exec->inputStackNr > 0)) {
3594 xmlFARegExecSaveInputString(exec, value, data);
3595 value = exec->inputStack[exec->index].value;
3596 data = exec->inputStack[exec->index].data;
3597#ifdef DEBUG_PUSH
3598 printf("value loaded: %s\n", value);
3599#endif
3600 }
3601
3602 while ((exec->status == 0) &&
3603 ((value != NULL) ||
3604 ((final == 1) &&
3605 (exec->state->type != XML_REGEXP_FINAL_STATE)))) {
3606
3607 /*
3608 * End of input on non-terminal state, rollback, however we may
3609 * still have epsilon like transition for counted transitions
3610 * on counters, in that case don't break too early.
3611 */
Daniel Veillardb509f152002-04-17 16:28:10 +00003612 if ((value == NULL) && (exec->counts == NULL))
Daniel Veillard4255d502002-04-16 15:50:10 +00003613 goto rollback;
3614
3615 exec->transcount = 0;
3616 for (;exec->transno < exec->state->nbTrans;exec->transno++) {
3617 trans = &exec->state->trans[exec->transno];
3618 if (trans->to < 0)
3619 continue;
3620 atom = trans->atom;
3621 ret = 0;
Daniel Veillard441bc322002-04-20 17:38:48 +00003622 if (trans->count == REGEXP_ALL_LAX_COUNTER) {
3623 int i;
3624 int count;
3625 xmlRegTransPtr t;
3626 xmlRegCounterPtr counter;
3627
3628 ret = 0;
3629
3630#ifdef DEBUG_PUSH
3631 printf("testing all lax %d\n", trans->count);
3632#endif
3633 /*
3634 * Check all counted transitions from the current state
3635 */
3636 if ((value == NULL) && (final)) {
3637 ret = 1;
3638 } else if (value != NULL) {
3639 for (i = 0;i < exec->state->nbTrans;i++) {
3640 t = &exec->state->trans[i];
3641 if ((t->counter < 0) || (t == trans))
3642 continue;
3643 counter = &exec->comp->counters[t->counter];
3644 count = exec->counts[t->counter];
3645 if ((count < counter->max) &&
3646 (t->atom != NULL) &&
3647 (xmlStrEqual(value, t->atom->valuep))) {
3648 ret = 0;
3649 break;
3650 }
3651 if ((count >= counter->min) &&
3652 (count < counter->max) &&
Daniel Veillard11ce4002006-03-10 00:36:23 +00003653 (t->atom != NULL) &&
Daniel Veillard441bc322002-04-20 17:38:48 +00003654 (xmlStrEqual(value, t->atom->valuep))) {
3655 ret = 1;
3656 break;
3657 }
3658 }
3659 }
3660 } else if (trans->count == REGEXP_ALL_COUNTER) {
Daniel Veillard8a001f62002-04-20 07:24:11 +00003661 int i;
3662 int count;
3663 xmlRegTransPtr t;
3664 xmlRegCounterPtr counter;
3665
3666 ret = 1;
3667
3668#ifdef DEBUG_PUSH
3669 printf("testing all %d\n", trans->count);
3670#endif
3671 /*
3672 * Check all counted transitions from the current state
3673 */
3674 for (i = 0;i < exec->state->nbTrans;i++) {
3675 t = &exec->state->trans[i];
3676 if ((t->counter < 0) || (t == trans))
3677 continue;
3678 counter = &exec->comp->counters[t->counter];
3679 count = exec->counts[t->counter];
3680 if ((count < counter->min) || (count > counter->max)) {
3681 ret = 0;
3682 break;
3683 }
3684 }
3685 } else if (trans->count >= 0) {
Daniel Veillard4255d502002-04-16 15:50:10 +00003686 int count;
3687 xmlRegCounterPtr counter;
3688
3689 /*
3690 * A counted transition.
3691 */
3692
3693 count = exec->counts[trans->count];
3694 counter = &exec->comp->counters[trans->count];
3695#ifdef DEBUG_PUSH
3696 printf("testing count %d: val %d, min %d, max %d\n",
3697 trans->count, count, counter->min, counter->max);
3698#endif
3699 ret = ((count >= counter->min) && (count <= counter->max));
3700 } else if (atom == NULL) {
3701 fprintf(stderr, "epsilon transition left at runtime\n");
3702 exec->status = -2;
3703 break;
3704 } else if (value != NULL) {
Daniel Veillardc0826a72004-08-10 14:17:33 +00003705 ret = xmlRegStrEqualWildcard(atom->valuep, value);
Daniel Veillard6e65e152005-08-09 11:09:52 +00003706 if (atom->neg) {
Daniel Veillard9efc4762005-07-19 14:33:55 +00003707 ret = !ret;
Daniel Veillard6e65e152005-08-09 11:09:52 +00003708 if (!compound)
3709 ret = 0;
3710 }
Daniel Veillard441bc322002-04-20 17:38:48 +00003711 if ((ret == 1) && (trans->counter >= 0)) {
3712 xmlRegCounterPtr counter;
3713 int count;
3714
3715 count = exec->counts[trans->counter];
3716 counter = &exec->comp->counters[trans->counter];
3717 if (count >= counter->max)
3718 ret = 0;
3719 }
3720
Daniel Veillard4255d502002-04-16 15:50:10 +00003721 if ((ret == 1) && (atom->min > 0) && (atom->max > 0)) {
3722 xmlRegStatePtr to = exec->comp->states[trans->to];
3723
3724 /*
3725 * this is a multiple input sequence
3726 */
3727 if (exec->state->nbTrans > exec->transno + 1) {
3728 if (exec->inputStackNr <= 0) {
3729 xmlFARegExecSaveInputString(exec, value, data);
3730 }
3731 xmlFARegExecSave(exec);
3732 }
3733 exec->transcount = 1;
3734 do {
3735 /*
3736 * Try to progress as much as possible on the input
3737 */
3738 if (exec->transcount == atom->max) {
3739 break;
3740 }
3741 exec->index++;
3742 value = exec->inputStack[exec->index].value;
3743 data = exec->inputStack[exec->index].data;
3744#ifdef DEBUG_PUSH
3745 printf("value loaded: %s\n", value);
3746#endif
3747
3748 /*
3749 * End of input: stop here
3750 */
3751 if (value == NULL) {
3752 exec->index --;
3753 break;
3754 }
3755 if (exec->transcount >= atom->min) {
3756 int transno = exec->transno;
3757 xmlRegStatePtr state = exec->state;
3758
3759 /*
3760 * The transition is acceptable save it
3761 */
3762 exec->transno = -1; /* trick */
3763 exec->state = to;
3764 if (exec->inputStackNr <= 0) {
3765 xmlFARegExecSaveInputString(exec, value, data);
3766 }
3767 xmlFARegExecSave(exec);
3768 exec->transno = transno;
3769 exec->state = state;
3770 }
3771 ret = xmlStrEqual(value, atom->valuep);
3772 exec->transcount++;
3773 } while (ret == 1);
3774 if (exec->transcount < atom->min)
3775 ret = 0;
3776
3777 /*
3778 * If the last check failed but one transition was found
3779 * possible, rollback
3780 */
3781 if (ret < 0)
3782 ret = 0;
3783 if (ret == 0) {
3784 goto rollback;
3785 }
3786 }
3787 }
3788 if (ret == 1) {
William M. Brack98873952003-12-26 06:03:14 +00003789 if ((exec->callback != NULL) && (atom != NULL) &&
3790 (data != NULL)) {
Daniel Veillard4255d502002-04-16 15:50:10 +00003791 exec->callback(exec->data, atom->valuep,
3792 atom->data, data);
3793 }
3794 if (exec->state->nbTrans > exec->transno + 1) {
3795 if (exec->inputStackNr <= 0) {
3796 xmlFARegExecSaveInputString(exec, value, data);
3797 }
3798 xmlFARegExecSave(exec);
3799 }
3800 if (trans->counter >= 0) {
3801#ifdef DEBUG_PUSH
3802 printf("Increasing count %d\n", trans->counter);
3803#endif
3804 exec->counts[trans->counter]++;
3805 }
Daniel Veillard10752282005-08-08 13:05:13 +00003806 if ((trans->count >= 0) &&
3807 (trans->count < REGEXP_ALL_COUNTER)) {
3808#ifdef DEBUG_REGEXP_EXEC
3809 printf("resetting count %d on transition\n",
3810 trans->count);
3811#endif
3812 exec->counts[trans->count] = 0;
3813 }
Daniel Veillard4255d502002-04-16 15:50:10 +00003814#ifdef DEBUG_PUSH
3815 printf("entering state %d\n", trans->to);
3816#endif
Daniel Veillardcc026dc2005-01-12 13:21:17 +00003817 if ((exec->comp->states[trans->to] != NULL) &&
3818 (exec->comp->states[trans->to]->type ==
3819 XML_REGEXP_SINK_STATE)) {
3820 /*
3821 * entering a sink state, save the current state as error
3822 * state.
3823 */
3824 if (exec->errString != NULL)
3825 xmlFree(exec->errString);
3826 exec->errString = xmlStrdup(value);
3827 exec->errState = exec->state;
3828 memcpy(exec->errCounts, exec->counts,
3829 exec->comp->nbCounters * sizeof(int));
3830 }
Daniel Veillard4255d502002-04-16 15:50:10 +00003831 exec->state = exec->comp->states[trans->to];
3832 exec->transno = 0;
3833 if (trans->atom != NULL) {
3834 if (exec->inputStack != NULL) {
3835 exec->index++;
3836 if (exec->index < exec->inputStackNr) {
3837 value = exec->inputStack[exec->index].value;
3838 data = exec->inputStack[exec->index].data;
3839#ifdef DEBUG_PUSH
3840 printf("value loaded: %s\n", value);
3841#endif
3842 } else {
3843 value = NULL;
3844 data = NULL;
3845#ifdef DEBUG_PUSH
3846 printf("end of input\n");
3847#endif
3848 }
3849 } else {
3850 value = NULL;
3851 data = NULL;
3852#ifdef DEBUG_PUSH
3853 printf("end of input\n");
3854#endif
3855 }
3856 }
3857 goto progress;
3858 } else if (ret < 0) {
3859 exec->status = -4;
3860 break;
3861 }
3862 }
3863 if ((exec->transno != 0) || (exec->state->nbTrans == 0)) {
3864rollback:
Daniel Veillard90700152005-01-08 22:05:09 +00003865 /*
Daniel Veillardcc026dc2005-01-12 13:21:17 +00003866 * if we didn't yet rollback on the current input
3867 * store the current state as the error state.
Daniel Veillard90700152005-01-08 22:05:09 +00003868 */
Daniel Veillardcc026dc2005-01-12 13:21:17 +00003869 if ((progress) && (exec->state != NULL) &&
3870 (exec->state->type != XML_REGEXP_SINK_STATE)) {
Daniel Veillard90700152005-01-08 22:05:09 +00003871 progress = 0;
3872 if (exec->errString != NULL)
3873 xmlFree(exec->errString);
3874 exec->errString = xmlStrdup(value);
3875 exec->errState = exec->state;
3876 memcpy(exec->errCounts, exec->counts,
3877 exec->comp->nbCounters * sizeof(int));
3878 }
3879
Daniel Veillard4255d502002-04-16 15:50:10 +00003880 /*
3881 * Failed to find a way out
3882 */
3883 exec->determinist = 0;
3884 xmlFARegExecRollBack(exec);
3885 if (exec->status == 0) {
3886 value = exec->inputStack[exec->index].value;
3887 data = exec->inputStack[exec->index].data;
3888#ifdef DEBUG_PUSH
3889 printf("value loaded: %s\n", value);
3890#endif
3891 }
3892 }
Daniel Veillard90700152005-01-08 22:05:09 +00003893 continue;
Daniel Veillard4255d502002-04-16 15:50:10 +00003894progress:
Daniel Veillard90700152005-01-08 22:05:09 +00003895 progress = 1;
Daniel Veillard4255d502002-04-16 15:50:10 +00003896 continue;
3897 }
3898 if (exec->status == 0) {
3899 return(exec->state->type == XML_REGEXP_FINAL_STATE);
3900 }
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003901#ifdef DEBUG_ERR
Daniel Veillard90700152005-01-08 22:05:09 +00003902 if (exec->status < 0) {
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003903 testerr(exec);
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003904 }
Daniel Veillard90700152005-01-08 22:05:09 +00003905#endif
Daniel Veillard4255d502002-04-16 15:50:10 +00003906 return(exec->status);
3907}
3908
Daniel Veillard52b48c72003-04-13 19:53:42 +00003909/**
Daniel Veillard6e65e152005-08-09 11:09:52 +00003910 * xmlRegExecPushString:
3911 * @exec: a regexp execution context or NULL to indicate the end
3912 * @value: a string token input
3913 * @data: data associated to the token to reuse in callbacks
3914 *
3915 * Push one input token in the execution context
3916 *
3917 * Returns: 1 if the regexp reached a final state, 0 if non-final, and
3918 * a negative value in case of error.
3919 */
3920int
3921xmlRegExecPushString(xmlRegExecCtxtPtr exec, const xmlChar *value,
3922 void *data) {
3923 return(xmlRegExecPushStringInternal(exec, value, data, 0));
3924}
3925
3926/**
Daniel Veillard52b48c72003-04-13 19:53:42 +00003927 * xmlRegExecPushString2:
3928 * @exec: a regexp execution context or NULL to indicate the end
3929 * @value: the first string token input
3930 * @value2: the second string token input
3931 * @data: data associated to the token to reuse in callbacks
3932 *
3933 * Push one input token in the execution context
3934 *
3935 * Returns: 1 if the regexp reached a final state, 0 if non-final, and
3936 * a negative value in case of error.
3937 */
3938int
3939xmlRegExecPushString2(xmlRegExecCtxtPtr exec, const xmlChar *value,
3940 const xmlChar *value2, void *data) {
3941 xmlChar buf[150];
3942 int lenn, lenp, ret;
3943 xmlChar *str;
3944
3945 if (exec == NULL)
3946 return(-1);
3947 if (exec->comp == NULL)
3948 return(-1);
3949 if (exec->status != 0)
3950 return(exec->status);
3951
3952 if (value2 == NULL)
3953 return(xmlRegExecPushString(exec, value, data));
3954
3955 lenn = strlen((char *) value2);
3956 lenp = strlen((char *) value);
3957
3958 if (150 < lenn + lenp + 2) {
Daniel Veillard3c908dc2003-04-19 00:07:51 +00003959 str = (xmlChar *) xmlMallocAtomic(lenn + lenp + 2);
Daniel Veillard52b48c72003-04-13 19:53:42 +00003960 if (str == NULL) {
3961 exec->status = -1;
3962 return(-1);
3963 }
3964 } else {
3965 str = buf;
3966 }
3967 memcpy(&str[0], value, lenp);
Daniel Veillardc0826a72004-08-10 14:17:33 +00003968 str[lenp] = XML_REG_STRING_SEPARATOR;
Daniel Veillard52b48c72003-04-13 19:53:42 +00003969 memcpy(&str[lenp + 1], value2, lenn);
3970 str[lenn + lenp + 1] = 0;
3971
3972 if (exec->comp->compact != NULL)
3973 ret = xmlRegCompactPushString(exec, exec->comp, str, data);
3974 else
Daniel Veillard6e65e152005-08-09 11:09:52 +00003975 ret = xmlRegExecPushStringInternal(exec, str, data, 1);
Daniel Veillard52b48c72003-04-13 19:53:42 +00003976
3977 if (str != buf)
Daniel Veillard0b1ff142005-12-28 21:13:33 +00003978 xmlFree(str);
Daniel Veillard52b48c72003-04-13 19:53:42 +00003979 return(ret);
3980}
3981
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003982/**
Daniel Veillard77005e62005-07-19 16:26:18 +00003983 * xmlRegExecGetValues:
Daniel Veillardfc0b6f62005-01-09 17:48:02 +00003984 * @exec: a regexp execution context
3985 * @err: error extraction or normal one
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003986 * @nbval: pointer to the number of accepted values IN/OUT
Daniel Veillardcc026dc2005-01-12 13:21:17 +00003987 * @nbneg: return number of negative transitions
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003988 * @values: pointer to the array of acceptable values
Daniel Veillardfc0b6f62005-01-09 17:48:02 +00003989 * @terminal: return value if this was a terminal state
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003990 *
Daniel Veillardfc0b6f62005-01-09 17:48:02 +00003991 * Extract informations from the regexp execution, internal routine to
3992 * implement xmlRegExecNextValues() and xmlRegExecErrInfo()
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003993 *
3994 * Returns: 0 in case of success or -1 in case of error.
3995 */
Daniel Veillardfc0b6f62005-01-09 17:48:02 +00003996static int
3997xmlRegExecGetValues(xmlRegExecCtxtPtr exec, int err,
Daniel Veillardcc026dc2005-01-12 13:21:17 +00003998 int *nbval, int *nbneg,
3999 xmlChar **values, int *terminal) {
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00004000 int maxval;
Daniel Veillardcc026dc2005-01-12 13:21:17 +00004001 int nb = 0;
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00004002
Daniel Veillardcc026dc2005-01-12 13:21:17 +00004003 if ((exec == NULL) || (nbval == NULL) || (nbneg == NULL) ||
4004 (values == NULL) || (*nbval <= 0))
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00004005 return(-1);
Daniel Veillardfc0b6f62005-01-09 17:48:02 +00004006
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00004007 maxval = *nbval;
4008 *nbval = 0;
Daniel Veillardcc026dc2005-01-12 13:21:17 +00004009 *nbneg = 0;
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00004010 if ((exec->comp != NULL) && (exec->comp->compact != NULL)) {
4011 xmlRegexpPtr comp;
4012 int target, i, state;
4013
4014 comp = exec->comp;
Daniel Veillardfc0b6f62005-01-09 17:48:02 +00004015
4016 if (err) {
4017 if (exec->errStateNo == -1) return(-1);
4018 state = exec->errStateNo;
4019 } else {
4020 state = exec->index;
4021 }
4022 if (terminal != NULL) {
4023 if (comp->compact[state * (comp->nbstrings + 1)] ==
4024 XML_REGEXP_FINAL_STATE)
4025 *terminal = 1;
4026 else
4027 *terminal = 0;
4028 }
Daniel Veillardcc026dc2005-01-12 13:21:17 +00004029 for (i = 0;(i < comp->nbstrings) && (nb < maxval);i++) {
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00004030 target = comp->compact[state * (comp->nbstrings + 1) + i + 1];
Daniel Veillardcc026dc2005-01-12 13:21:17 +00004031 if ((target > 0) && (target <= comp->nbstates) &&
4032 (comp->compact[(target - 1) * (comp->nbstrings + 1)] !=
4033 XML_REGEXP_SINK_STATE)) {
4034 values[nb++] = comp->stringMap[i];
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00004035 (*nbval)++;
4036 }
4037 }
Daniel Veillardcc026dc2005-01-12 13:21:17 +00004038 for (i = 0;(i < comp->nbstrings) && (nb < maxval);i++) {
4039 target = comp->compact[state * (comp->nbstrings + 1) + i + 1];
4040 if ((target > 0) && (target <= comp->nbstates) &&
4041 (comp->compact[(target - 1) * (comp->nbstrings + 1)] ==
4042 XML_REGEXP_SINK_STATE)) {
4043 values[nb++] = comp->stringMap[i];
4044 (*nbneg)++;
4045 }
4046 }
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00004047 } else {
4048 int transno;
4049 xmlRegTransPtr trans;
4050 xmlRegAtomPtr atom;
Daniel Veillardfc0b6f62005-01-09 17:48:02 +00004051 xmlRegStatePtr state;
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00004052
Daniel Veillardfc0b6f62005-01-09 17:48:02 +00004053 if (terminal != NULL) {
4054 if (exec->state->type == XML_REGEXP_FINAL_STATE)
4055 *terminal = 1;
4056 else
4057 *terminal = 0;
4058 }
4059
4060 if (err) {
4061 if (exec->errState == NULL) return(-1);
4062 state = exec->errState;
4063 } else {
4064 if (exec->state == NULL) return(-1);
4065 state = exec->state;
4066 }
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00004067 for (transno = 0;
Daniel Veillardcc026dc2005-01-12 13:21:17 +00004068 (transno < state->nbTrans) && (nb < maxval);
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00004069 transno++) {
Daniel Veillardfc0b6f62005-01-09 17:48:02 +00004070 trans = &state->trans[transno];
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00004071 if (trans->to < 0)
4072 continue;
4073 atom = trans->atom;
4074 if ((atom == NULL) || (atom->valuep == NULL))
4075 continue;
4076 if (trans->count == REGEXP_ALL_LAX_COUNTER) {
Daniel Veillardcc026dc2005-01-12 13:21:17 +00004077 /* this should not be reached but ... */
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00004078 TODO;
4079 } else if (trans->count == REGEXP_ALL_COUNTER) {
Daniel Veillardcc026dc2005-01-12 13:21:17 +00004080 /* this should not be reached but ... */
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00004081 TODO;
4082 } else if (trans->counter >= 0) {
Daniel Veillard11ce4002006-03-10 00:36:23 +00004083 xmlRegCounterPtr counter = NULL;
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00004084 int count;
4085
Daniel Veillardfc0b6f62005-01-09 17:48:02 +00004086 if (err)
4087 count = exec->errCounts[trans->counter];
4088 else
4089 count = exec->counts[trans->counter];
Daniel Veillard11ce4002006-03-10 00:36:23 +00004090 if (exec->comp != NULL)
4091 counter = &exec->comp->counters[trans->counter];
4092 if ((counter == NULL) || (count < counter->max)) {
Daniel Veillard77005e62005-07-19 16:26:18 +00004093 if (atom->neg)
4094 values[nb++] = (xmlChar *) atom->valuep2;
4095 else
4096 values[nb++] = (xmlChar *) atom->valuep;
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00004097 (*nbval)++;
4098 }
4099 } else {
Daniel Veillardcc026dc2005-01-12 13:21:17 +00004100 if ((exec->comp->states[trans->to] != NULL) &&
4101 (exec->comp->states[trans->to]->type !=
4102 XML_REGEXP_SINK_STATE)) {
Daniel Veillard77005e62005-07-19 16:26:18 +00004103 if (atom->neg)
4104 values[nb++] = (xmlChar *) atom->valuep2;
4105 else
4106 values[nb++] = (xmlChar *) atom->valuep;
Daniel Veillardcc026dc2005-01-12 13:21:17 +00004107 (*nbval)++;
4108 }
4109 }
4110 }
4111 for (transno = 0;
4112 (transno < state->nbTrans) && (nb < maxval);
4113 transno++) {
4114 trans = &state->trans[transno];
4115 if (trans->to < 0)
4116 continue;
4117 atom = trans->atom;
4118 if ((atom == NULL) || (atom->valuep == NULL))
4119 continue;
4120 if (trans->count == REGEXP_ALL_LAX_COUNTER) {
4121 continue;
4122 } else if (trans->count == REGEXP_ALL_COUNTER) {
4123 continue;
4124 } else if (trans->counter >= 0) {
4125 continue;
4126 } else {
4127 if ((exec->comp->states[trans->to] != NULL) &&
4128 (exec->comp->states[trans->to]->type ==
4129 XML_REGEXP_SINK_STATE)) {
Daniel Veillard77005e62005-07-19 16:26:18 +00004130 if (atom->neg)
4131 values[nb++] = (xmlChar *) atom->valuep2;
4132 else
4133 values[nb++] = (xmlChar *) atom->valuep;
Daniel Veillardcc026dc2005-01-12 13:21:17 +00004134 (*nbneg)++;
4135 }
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00004136 }
4137 }
4138 }
4139 return(0);
4140}
4141
Daniel Veillardfc0b6f62005-01-09 17:48:02 +00004142/**
4143 * xmlRegExecNextValues:
4144 * @exec: a regexp execution context
4145 * @nbval: pointer to the number of accepted values IN/OUT
Daniel Veillardcc026dc2005-01-12 13:21:17 +00004146 * @nbneg: return number of negative transitions
Daniel Veillardfc0b6f62005-01-09 17:48:02 +00004147 * @values: pointer to the array of acceptable values
4148 * @terminal: return value if this was a terminal state
4149 *
4150 * Extract informations from the regexp execution,
4151 * the parameter @values must point to an array of @nbval string pointers
4152 * on return nbval will contain the number of possible strings in that
4153 * state and the @values array will be updated with them. The string values
4154 * returned will be freed with the @exec context and don't need to be
4155 * deallocated.
4156 *
4157 * Returns: 0 in case of success or -1 in case of error.
4158 */
4159int
Daniel Veillardcc026dc2005-01-12 13:21:17 +00004160xmlRegExecNextValues(xmlRegExecCtxtPtr exec, int *nbval, int *nbneg,
4161 xmlChar **values, int *terminal) {
4162 return(xmlRegExecGetValues(exec, 0, nbval, nbneg, values, terminal));
Daniel Veillardfc0b6f62005-01-09 17:48:02 +00004163}
4164
4165/**
4166 * xmlRegExecErrInfo:
4167 * @exec: a regexp execution context generating an error
4168 * @string: return value for the error string
4169 * @nbval: pointer to the number of accepted values IN/OUT
Daniel Veillardcc026dc2005-01-12 13:21:17 +00004170 * @nbneg: return number of negative transitions
Daniel Veillardfc0b6f62005-01-09 17:48:02 +00004171 * @values: pointer to the array of acceptable values
4172 * @terminal: return value if this was a terminal state
4173 *
4174 * Extract error informations from the regexp execution, the parameter
4175 * @string will be updated with the value pushed and not accepted,
4176 * the parameter @values must point to an array of @nbval string pointers
4177 * on return nbval will contain the number of possible strings in that
4178 * state and the @values array will be updated with them. The string values
4179 * returned will be freed with the @exec context and don't need to be
4180 * deallocated.
4181 *
4182 * Returns: 0 in case of success or -1 in case of error.
4183 */
4184int
4185xmlRegExecErrInfo(xmlRegExecCtxtPtr exec, const xmlChar **string,
Daniel Veillardcc026dc2005-01-12 13:21:17 +00004186 int *nbval, int *nbneg, xmlChar **values, int *terminal) {
Daniel Veillardfc0b6f62005-01-09 17:48:02 +00004187 if (exec == NULL)
4188 return(-1);
4189 if (string != NULL) {
4190 if (exec->status != 0)
4191 *string = exec->errString;
4192 else
4193 *string = NULL;
4194 }
Daniel Veillardcc026dc2005-01-12 13:21:17 +00004195 return(xmlRegExecGetValues(exec, 1, nbval, nbneg, values, terminal));
Daniel Veillardfc0b6f62005-01-09 17:48:02 +00004196}
4197
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00004198#ifdef DEBUG_ERR
4199static void testerr(xmlRegExecCtxtPtr exec) {
4200 const xmlChar *string;
Daniel Veillardcee2b3a2005-01-25 00:22:52 +00004201 xmlChar *values[5];
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00004202 int nb = 5;
Daniel Veillardcc026dc2005-01-12 13:21:17 +00004203 int nbneg;
Daniel Veillardfc0b6f62005-01-09 17:48:02 +00004204 int terminal;
Daniel Veillardcc026dc2005-01-12 13:21:17 +00004205 xmlRegExecErrInfo(exec, &string, &nb, &nbneg, &values[0], &terminal);
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00004206}
4207#endif
4208
Daniel Veillard4255d502002-04-16 15:50:10 +00004209#if 0
4210static int
4211xmlRegExecPushChar(xmlRegExecCtxtPtr exec, int UCS) {
4212 xmlRegTransPtr trans;
4213 xmlRegAtomPtr atom;
4214 int ret;
4215 int codepoint, len;
4216
4217 if (exec == NULL)
4218 return(-1);
4219 if (exec->status != 0)
4220 return(exec->status);
4221
4222 while ((exec->status == 0) &&
4223 ((exec->inputString[exec->index] != 0) ||
4224 (exec->state->type != XML_REGEXP_FINAL_STATE))) {
4225
4226 /*
4227 * End of input on non-terminal state, rollback, however we may
4228 * still have epsilon like transition for counted transitions
4229 * on counters, in that case don't break too early.
4230 */
4231 if ((exec->inputString[exec->index] == 0) && (exec->counts == NULL))
4232 goto rollback;
4233
4234 exec->transcount = 0;
4235 for (;exec->transno < exec->state->nbTrans;exec->transno++) {
4236 trans = &exec->state->trans[exec->transno];
4237 if (trans->to < 0)
4238 continue;
4239 atom = trans->atom;
4240 ret = 0;
4241 if (trans->count >= 0) {
4242 int count;
4243 xmlRegCounterPtr counter;
4244
4245 /*
4246 * A counted transition.
4247 */
4248
4249 count = exec->counts[trans->count];
4250 counter = &exec->comp->counters[trans->count];
4251#ifdef DEBUG_REGEXP_EXEC
4252 printf("testing count %d: val %d, min %d, max %d\n",
4253 trans->count, count, counter->min, counter->max);
4254#endif
4255 ret = ((count >= counter->min) && (count <= counter->max));
4256 } else if (atom == NULL) {
4257 fprintf(stderr, "epsilon transition left at runtime\n");
4258 exec->status = -2;
4259 break;
4260 } else if (exec->inputString[exec->index] != 0) {
4261 codepoint = CUR_SCHAR(&(exec->inputString[exec->index]), len);
4262 ret = xmlRegCheckCharacter(atom, codepoint);
4263 if ((ret == 1) && (atom->min > 0) && (atom->max > 0)) {
4264 xmlRegStatePtr to = exec->comp->states[trans->to];
4265
4266 /*
4267 * this is a multiple input sequence
4268 */
4269 if (exec->state->nbTrans > exec->transno + 1) {
4270 xmlFARegExecSave(exec);
4271 }
4272 exec->transcount = 1;
4273 do {
4274 /*
4275 * Try to progress as much as possible on the input
4276 */
4277 if (exec->transcount == atom->max) {
4278 break;
4279 }
4280 exec->index += len;
4281 /*
4282 * End of input: stop here
4283 */
4284 if (exec->inputString[exec->index] == 0) {
4285 exec->index -= len;
4286 break;
4287 }
4288 if (exec->transcount >= atom->min) {
4289 int transno = exec->transno;
4290 xmlRegStatePtr state = exec->state;
4291
4292 /*
4293 * The transition is acceptable save it
4294 */
4295 exec->transno = -1; /* trick */
4296 exec->state = to;
4297 xmlFARegExecSave(exec);
4298 exec->transno = transno;
4299 exec->state = state;
4300 }
4301 codepoint = CUR_SCHAR(&(exec->inputString[exec->index]),
4302 len);
4303 ret = xmlRegCheckCharacter(atom, codepoint);
4304 exec->transcount++;
4305 } while (ret == 1);
4306 if (exec->transcount < atom->min)
4307 ret = 0;
4308
4309 /*
4310 * If the last check failed but one transition was found
4311 * possible, rollback
4312 */
4313 if (ret < 0)
4314 ret = 0;
4315 if (ret == 0) {
4316 goto rollback;
4317 }
4318 }
4319 }
4320 if (ret == 1) {
4321 if (exec->state->nbTrans > exec->transno + 1) {
4322 xmlFARegExecSave(exec);
4323 }
Daniel Veillard54eb0242006-03-21 23:17:57 +00004324 /*
4325 * restart count for expressions like this ((abc){2})*
4326 */
4327 if (trans->count >= 0) {
4328#ifdef DEBUG_REGEXP_EXEC
4329 printf("Reset count %d\n", trans->count);
4330#endif
4331 exec->counts[trans->count] = 0;
4332 }
Daniel Veillard4255d502002-04-16 15:50:10 +00004333 if (trans->counter >= 0) {
4334#ifdef DEBUG_REGEXP_EXEC
4335 printf("Increasing count %d\n", trans->counter);
4336#endif
4337 exec->counts[trans->counter]++;
4338 }
4339#ifdef DEBUG_REGEXP_EXEC
4340 printf("entering state %d\n", trans->to);
4341#endif
4342 exec->state = exec->comp->states[trans->to];
4343 exec->transno = 0;
4344 if (trans->atom != NULL) {
4345 exec->index += len;
4346 }
4347 goto progress;
4348 } else if (ret < 0) {
4349 exec->status = -4;
4350 break;
4351 }
4352 }
4353 if ((exec->transno != 0) || (exec->state->nbTrans == 0)) {
4354rollback:
4355 /*
4356 * Failed to find a way out
4357 */
4358 exec->determinist = 0;
4359 xmlFARegExecRollBack(exec);
4360 }
4361progress:
4362 continue;
4363 }
4364}
4365#endif
4366/************************************************************************
4367 * *
William M. Brackddf71d62004-05-06 04:17:26 +00004368 * Parser for the Schemas Datatype Regular Expressions *
Daniel Veillard4255d502002-04-16 15:50:10 +00004369 * http://www.w3.org/TR/2001/REC-xmlschema-2-20010502/#regexs *
4370 * *
4371 ************************************************************************/
4372
4373/**
4374 * xmlFAIsChar:
Daniel Veillard441bc322002-04-20 17:38:48 +00004375 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00004376 *
4377 * [10] Char ::= [^.\?*+()|#x5B#x5D]
4378 */
4379static int
4380xmlFAIsChar(xmlRegParserCtxtPtr ctxt) {
4381 int cur;
4382 int len;
4383
4384 cur = CUR_SCHAR(ctxt->cur, len);
4385 if ((cur == '.') || (cur == '\\') || (cur == '?') ||
4386 (cur == '*') || (cur == '+') || (cur == '(') ||
4387 (cur == ')') || (cur == '|') || (cur == 0x5B) ||
4388 (cur == 0x5D) || (cur == 0))
4389 return(-1);
4390 return(cur);
4391}
4392
4393/**
4394 * xmlFAParseCharProp:
Daniel Veillard441bc322002-04-20 17:38:48 +00004395 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00004396 *
4397 * [27] charProp ::= IsCategory | IsBlock
4398 * [28] IsCategory ::= Letters | Marks | Numbers | Punctuation |
4399 * Separators | Symbols | Others
4400 * [29] Letters ::= 'L' [ultmo]?
4401 * [30] Marks ::= 'M' [nce]?
4402 * [31] Numbers ::= 'N' [dlo]?
4403 * [32] Punctuation ::= 'P' [cdseifo]?
4404 * [33] Separators ::= 'Z' [slp]?
4405 * [34] Symbols ::= 'S' [mcko]?
4406 * [35] Others ::= 'C' [cfon]?
4407 * [36] IsBlock ::= 'Is' [a-zA-Z0-9#x2D]+
4408 */
4409static void
4410xmlFAParseCharProp(xmlRegParserCtxtPtr ctxt) {
4411 int cur;
William M. Brack779af002003-08-01 15:55:39 +00004412 xmlRegAtomType type = (xmlRegAtomType) 0;
Daniel Veillard4255d502002-04-16 15:50:10 +00004413 xmlChar *blockName = NULL;
4414
4415 cur = CUR;
4416 if (cur == 'L') {
4417 NEXT;
4418 cur = CUR;
4419 if (cur == 'u') {
4420 NEXT;
4421 type = XML_REGEXP_LETTER_UPPERCASE;
4422 } else if (cur == 'l') {
4423 NEXT;
4424 type = XML_REGEXP_LETTER_LOWERCASE;
4425 } else if (cur == 't') {
4426 NEXT;
4427 type = XML_REGEXP_LETTER_TITLECASE;
4428 } else if (cur == 'm') {
4429 NEXT;
4430 type = XML_REGEXP_LETTER_MODIFIER;
4431 } else if (cur == 'o') {
4432 NEXT;
4433 type = XML_REGEXP_LETTER_OTHERS;
4434 } else {
4435 type = XML_REGEXP_LETTER;
4436 }
4437 } else if (cur == 'M') {
4438 NEXT;
4439 cur = CUR;
4440 if (cur == 'n') {
4441 NEXT;
4442 /* nonspacing */
4443 type = XML_REGEXP_MARK_NONSPACING;
4444 } else if (cur == 'c') {
4445 NEXT;
4446 /* spacing combining */
4447 type = XML_REGEXP_MARK_SPACECOMBINING;
4448 } else if (cur == 'e') {
4449 NEXT;
4450 /* enclosing */
4451 type = XML_REGEXP_MARK_ENCLOSING;
4452 } else {
4453 /* all marks */
4454 type = XML_REGEXP_MARK;
4455 }
4456 } else if (cur == 'N') {
4457 NEXT;
4458 cur = CUR;
4459 if (cur == 'd') {
4460 NEXT;
4461 /* digital */
4462 type = XML_REGEXP_NUMBER_DECIMAL;
4463 } else if (cur == 'l') {
4464 NEXT;
4465 /* letter */
4466 type = XML_REGEXP_NUMBER_LETTER;
4467 } else if (cur == 'o') {
4468 NEXT;
4469 /* other */
4470 type = XML_REGEXP_NUMBER_OTHERS;
4471 } else {
4472 /* all numbers */
4473 type = XML_REGEXP_NUMBER;
4474 }
4475 } else if (cur == 'P') {
4476 NEXT;
4477 cur = CUR;
4478 if (cur == 'c') {
4479 NEXT;
4480 /* connector */
4481 type = XML_REGEXP_PUNCT_CONNECTOR;
4482 } else if (cur == 'd') {
4483 NEXT;
4484 /* dash */
4485 type = XML_REGEXP_PUNCT_DASH;
4486 } else if (cur == 's') {
4487 NEXT;
4488 /* open */
4489 type = XML_REGEXP_PUNCT_OPEN;
4490 } else if (cur == 'e') {
4491 NEXT;
4492 /* close */
4493 type = XML_REGEXP_PUNCT_CLOSE;
4494 } else if (cur == 'i') {
4495 NEXT;
4496 /* initial quote */
4497 type = XML_REGEXP_PUNCT_INITQUOTE;
4498 } else if (cur == 'f') {
4499 NEXT;
4500 /* final quote */
4501 type = XML_REGEXP_PUNCT_FINQUOTE;
4502 } else if (cur == 'o') {
4503 NEXT;
4504 /* other */
4505 type = XML_REGEXP_PUNCT_OTHERS;
4506 } else {
4507 /* all punctuation */
4508 type = XML_REGEXP_PUNCT;
4509 }
4510 } else if (cur == 'Z') {
4511 NEXT;
4512 cur = CUR;
4513 if (cur == 's') {
4514 NEXT;
4515 /* space */
4516 type = XML_REGEXP_SEPAR_SPACE;
4517 } else if (cur == 'l') {
4518 NEXT;
4519 /* line */
4520 type = XML_REGEXP_SEPAR_LINE;
4521 } else if (cur == 'p') {
4522 NEXT;
4523 /* paragraph */
4524 type = XML_REGEXP_SEPAR_PARA;
4525 } else {
4526 /* all separators */
4527 type = XML_REGEXP_SEPAR;
4528 }
4529 } else if (cur == 'S') {
4530 NEXT;
4531 cur = CUR;
4532 if (cur == 'm') {
4533 NEXT;
4534 type = XML_REGEXP_SYMBOL_MATH;
4535 /* math */
4536 } else if (cur == 'c') {
4537 NEXT;
4538 type = XML_REGEXP_SYMBOL_CURRENCY;
4539 /* currency */
4540 } else if (cur == 'k') {
4541 NEXT;
4542 type = XML_REGEXP_SYMBOL_MODIFIER;
4543 /* modifiers */
4544 } else if (cur == 'o') {
4545 NEXT;
4546 type = XML_REGEXP_SYMBOL_OTHERS;
4547 /* other */
4548 } else {
4549 /* all symbols */
4550 type = XML_REGEXP_SYMBOL;
4551 }
4552 } else if (cur == 'C') {
4553 NEXT;
4554 cur = CUR;
4555 if (cur == 'c') {
4556 NEXT;
4557 /* control */
4558 type = XML_REGEXP_OTHER_CONTROL;
4559 } else if (cur == 'f') {
4560 NEXT;
4561 /* format */
4562 type = XML_REGEXP_OTHER_FORMAT;
4563 } else if (cur == 'o') {
4564 NEXT;
4565 /* private use */
4566 type = XML_REGEXP_OTHER_PRIVATE;
4567 } else if (cur == 'n') {
4568 NEXT;
4569 /* not assigned */
4570 type = XML_REGEXP_OTHER_NA;
4571 } else {
4572 /* all others */
4573 type = XML_REGEXP_OTHER;
4574 }
4575 } else if (cur == 'I') {
4576 const xmlChar *start;
4577 NEXT;
4578 cur = CUR;
4579 if (cur != 's') {
4580 ERROR("IsXXXX expected");
4581 return;
4582 }
4583 NEXT;
4584 start = ctxt->cur;
4585 cur = CUR;
4586 if (((cur >= 'a') && (cur <= 'z')) ||
4587 ((cur >= 'A') && (cur <= 'Z')) ||
4588 ((cur >= '0') && (cur <= '9')) ||
4589 (cur == 0x2D)) {
4590 NEXT;
4591 cur = CUR;
4592 while (((cur >= 'a') && (cur <= 'z')) ||
4593 ((cur >= 'A') && (cur <= 'Z')) ||
4594 ((cur >= '0') && (cur <= '9')) ||
4595 (cur == 0x2D)) {
4596 NEXT;
4597 cur = CUR;
4598 }
4599 }
4600 type = XML_REGEXP_BLOCK_NAME;
4601 blockName = xmlStrndup(start, ctxt->cur - start);
4602 } else {
4603 ERROR("Unknown char property");
4604 return;
4605 }
4606 if (ctxt->atom == NULL) {
4607 ctxt->atom = xmlRegNewAtom(ctxt, type);
4608 if (ctxt->atom != NULL)
4609 ctxt->atom->valuep = blockName;
4610 } else if (ctxt->atom->type == XML_REGEXP_RANGES) {
4611 xmlRegAtomAddRange(ctxt, ctxt->atom, ctxt->neg,
4612 type, 0, 0, blockName);
4613 }
4614}
4615
4616/**
4617 * xmlFAParseCharClassEsc:
Daniel Veillard441bc322002-04-20 17:38:48 +00004618 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00004619 *
4620 * [23] charClassEsc ::= ( SingleCharEsc | MultiCharEsc | catEsc | complEsc )
4621 * [24] SingleCharEsc ::= '\' [nrt\|.?*+(){}#x2D#x5B#x5D#x5E]
4622 * [25] catEsc ::= '\p{' charProp '}'
4623 * [26] complEsc ::= '\P{' charProp '}'
4624 * [37] MultiCharEsc ::= '.' | ('\' [sSiIcCdDwW])
4625 */
4626static void
4627xmlFAParseCharClassEsc(xmlRegParserCtxtPtr ctxt) {
4628 int cur;
4629
4630 if (CUR == '.') {
4631 if (ctxt->atom == NULL) {
4632 ctxt->atom = xmlRegNewAtom(ctxt, XML_REGEXP_ANYCHAR);
4633 } else if (ctxt->atom->type == XML_REGEXP_RANGES) {
4634 xmlRegAtomAddRange(ctxt, ctxt->atom, ctxt->neg,
4635 XML_REGEXP_ANYCHAR, 0, 0, NULL);
4636 }
4637 NEXT;
4638 return;
4639 }
4640 if (CUR != '\\') {
4641 ERROR("Escaped sequence: expecting \\");
4642 return;
4643 }
4644 NEXT;
4645 cur = CUR;
4646 if (cur == 'p') {
4647 NEXT;
4648 if (CUR != '{') {
4649 ERROR("Expecting '{'");
4650 return;
4651 }
4652 NEXT;
4653 xmlFAParseCharProp(ctxt);
4654 if (CUR != '}') {
4655 ERROR("Expecting '}'");
4656 return;
4657 }
4658 NEXT;
4659 } else if (cur == 'P') {
4660 NEXT;
4661 if (CUR != '{') {
4662 ERROR("Expecting '{'");
4663 return;
4664 }
4665 NEXT;
4666 xmlFAParseCharProp(ctxt);
4667 ctxt->atom->neg = 1;
4668 if (CUR != '}') {
4669 ERROR("Expecting '}'");
4670 return;
4671 }
4672 NEXT;
4673 } else if ((cur == 'n') || (cur == 'r') || (cur == 't') || (cur == '\\') ||
4674 (cur == '|') || (cur == '.') || (cur == '?') || (cur == '*') ||
4675 (cur == '+') || (cur == '(') || (cur == ')') || (cur == '{') ||
4676 (cur == '}') || (cur == 0x2D) || (cur == 0x5B) || (cur == 0x5D) ||
4677 (cur == 0x5E)) {
4678 if (ctxt->atom == NULL) {
4679 ctxt->atom = xmlRegNewAtom(ctxt, XML_REGEXP_CHARVAL);
Daniel Veillard99c394d2005-07-14 12:58:49 +00004680 if (ctxt->atom != NULL) {
4681 switch (cur) {
4682 case 'n':
4683 ctxt->atom->codepoint = '\n';
4684 break;
4685 case 'r':
4686 ctxt->atom->codepoint = '\r';
4687 break;
4688 case 't':
4689 ctxt->atom->codepoint = '\t';
4690 break;
4691 default:
4692 ctxt->atom->codepoint = cur;
4693 }
4694 }
Daniel Veillard4255d502002-04-16 15:50:10 +00004695 } else if (ctxt->atom->type == XML_REGEXP_RANGES) {
4696 xmlRegAtomAddRange(ctxt, ctxt->atom, ctxt->neg,
4697 XML_REGEXP_CHARVAL, cur, cur, NULL);
4698 }
4699 NEXT;
4700 } else if ((cur == 's') || (cur == 'S') || (cur == 'i') || (cur == 'I') ||
4701 (cur == 'c') || (cur == 'C') || (cur == 'd') || (cur == 'D') ||
4702 (cur == 'w') || (cur == 'W')) {
Daniel Veillardb509f152002-04-17 16:28:10 +00004703 xmlRegAtomType type = XML_REGEXP_ANYSPACE;
Daniel Veillard4255d502002-04-16 15:50:10 +00004704
4705 switch (cur) {
4706 case 's':
4707 type = XML_REGEXP_ANYSPACE;
4708 break;
4709 case 'S':
4710 type = XML_REGEXP_NOTSPACE;
4711 break;
4712 case 'i':
4713 type = XML_REGEXP_INITNAME;
4714 break;
4715 case 'I':
4716 type = XML_REGEXP_NOTINITNAME;
4717 break;
4718 case 'c':
4719 type = XML_REGEXP_NAMECHAR;
4720 break;
4721 case 'C':
4722 type = XML_REGEXP_NOTNAMECHAR;
4723 break;
4724 case 'd':
4725 type = XML_REGEXP_DECIMAL;
4726 break;
4727 case 'D':
4728 type = XML_REGEXP_NOTDECIMAL;
4729 break;
4730 case 'w':
4731 type = XML_REGEXP_REALCHAR;
4732 break;
4733 case 'W':
4734 type = XML_REGEXP_NOTREALCHAR;
4735 break;
4736 }
4737 NEXT;
4738 if (ctxt->atom == NULL) {
4739 ctxt->atom = xmlRegNewAtom(ctxt, type);
4740 } else if (ctxt->atom->type == XML_REGEXP_RANGES) {
4741 xmlRegAtomAddRange(ctxt, ctxt->atom, ctxt->neg,
4742 type, 0, 0, NULL);
4743 }
4744 }
4745}
4746
4747/**
4748 * xmlFAParseCharRef:
Daniel Veillard441bc322002-04-20 17:38:48 +00004749 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00004750 *
4751 * [19] XmlCharRef ::= ( '&#' [0-9]+ ';' ) | (' &#x' [0-9a-fA-F]+ ';' )
4752 */
4753static int
4754xmlFAParseCharRef(xmlRegParserCtxtPtr ctxt) {
4755 int ret = 0, cur;
4756
4757 if ((CUR != '&') || (NXT(1) != '#'))
4758 return(-1);
4759 NEXT;
4760 NEXT;
4761 cur = CUR;
4762 if (cur == 'x') {
4763 NEXT;
4764 cur = CUR;
4765 if (((cur >= '0') && (cur <= '9')) ||
4766 ((cur >= 'a') && (cur <= 'f')) ||
4767 ((cur >= 'A') && (cur <= 'F'))) {
4768 while (((cur >= '0') && (cur <= '9')) ||
Daniel Veillard11ce4002006-03-10 00:36:23 +00004769 ((cur >= 'a') && (cur <= 'f')) ||
Daniel Veillard4255d502002-04-16 15:50:10 +00004770 ((cur >= 'A') && (cur <= 'F'))) {
4771 if ((cur >= '0') && (cur <= '9'))
4772 ret = ret * 16 + cur - '0';
4773 else if ((cur >= 'a') && (cur <= 'f'))
4774 ret = ret * 16 + 10 + (cur - 'a');
4775 else
4776 ret = ret * 16 + 10 + (cur - 'A');
4777 NEXT;
4778 cur = CUR;
4779 }
4780 } else {
4781 ERROR("Char ref: expecting [0-9A-F]");
4782 return(-1);
4783 }
4784 } else {
4785 if ((cur >= '0') && (cur <= '9')) {
4786 while ((cur >= '0') && (cur <= '9')) {
4787 ret = ret * 10 + cur - '0';
4788 NEXT;
4789 cur = CUR;
4790 }
4791 } else {
4792 ERROR("Char ref: expecting [0-9]");
4793 return(-1);
4794 }
4795 }
4796 if (cur != ';') {
4797 ERROR("Char ref: expecting ';'");
4798 return(-1);
4799 } else {
4800 NEXT;
4801 }
4802 return(ret);
4803}
4804
4805/**
4806 * xmlFAParseCharRange:
Daniel Veillard441bc322002-04-20 17:38:48 +00004807 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00004808 *
4809 * [17] charRange ::= seRange | XmlCharRef | XmlCharIncDash
4810 * [18] seRange ::= charOrEsc '-' charOrEsc
4811 * [20] charOrEsc ::= XmlChar | SingleCharEsc
4812 * [21] XmlChar ::= [^\#x2D#x5B#x5D]
4813 * [22] XmlCharIncDash ::= [^\#x5B#x5D]
4814 */
4815static void
4816xmlFAParseCharRange(xmlRegParserCtxtPtr ctxt) {
William M. Brackdc99df92003-12-27 01:54:25 +00004817 int cur, len;
Daniel Veillard4255d502002-04-16 15:50:10 +00004818 int start = -1;
4819 int end = -1;
4820
Daniel Veillard777737e2006-10-17 21:23:17 +00004821 if (CUR == '\0') {
4822 ERROR("Expecting ']'");
4823 return;
4824 }
4825
Daniel Veillard4255d502002-04-16 15:50:10 +00004826 if ((CUR == '&') && (NXT(1) == '#')) {
4827 end = start = xmlFAParseCharRef(ctxt);
4828 xmlRegAtomAddRange(ctxt, ctxt->atom, ctxt->neg,
4829 XML_REGEXP_CHARVAL, start, end, NULL);
4830 return;
4831 }
4832 cur = CUR;
4833 if (cur == '\\') {
4834 NEXT;
4835 cur = CUR;
4836 switch (cur) {
4837 case 'n': start = 0xA; break;
4838 case 'r': start = 0xD; break;
4839 case 't': start = 0x9; break;
4840 case '\\': case '|': case '.': case '-': case '^': case '?':
4841 case '*': case '+': case '{': case '}': case '(': case ')':
4842 case '[': case ']':
4843 start = cur; break;
4844 default:
4845 ERROR("Invalid escape value");
4846 return;
4847 }
4848 end = start;
William M. Brackdc99df92003-12-27 01:54:25 +00004849 len = 1;
Daniel Veillard4255d502002-04-16 15:50:10 +00004850 } else if ((cur != 0x5B) && (cur != 0x5D)) {
William M. Brackdc99df92003-12-27 01:54:25 +00004851 end = start = CUR_SCHAR(ctxt->cur, len);
Daniel Veillard4255d502002-04-16 15:50:10 +00004852 } else {
4853 ERROR("Expecting a char range");
4854 return;
4855 }
William M. Brackdc99df92003-12-27 01:54:25 +00004856 NEXTL(len);
Daniel Veillard4255d502002-04-16 15:50:10 +00004857 if (start == '-') {
4858 return;
4859 }
4860 cur = CUR;
William M. Brack10f1ef42004-03-20 14:51:25 +00004861 if ((cur != '-') || (NXT(1) == ']')) {
Daniel Veillard4255d502002-04-16 15:50:10 +00004862 xmlRegAtomAddRange(ctxt, ctxt->atom, ctxt->neg,
4863 XML_REGEXP_CHARVAL, start, end, NULL);
4864 return;
4865 }
4866 NEXT;
4867 cur = CUR;
4868 if (cur == '\\') {
4869 NEXT;
4870 cur = CUR;
4871 switch (cur) {
4872 case 'n': end = 0xA; break;
4873 case 'r': end = 0xD; break;
4874 case 't': end = 0x9; break;
4875 case '\\': case '|': case '.': case '-': case '^': case '?':
4876 case '*': case '+': case '{': case '}': case '(': case ')':
4877 case '[': case ']':
4878 end = cur; break;
4879 default:
4880 ERROR("Invalid escape value");
4881 return;
4882 }
William M. Brackdc99df92003-12-27 01:54:25 +00004883 len = 1;
Daniel Veillard4255d502002-04-16 15:50:10 +00004884 } else if ((cur != 0x5B) && (cur != 0x5D)) {
William M. Brackdc99df92003-12-27 01:54:25 +00004885 end = CUR_SCHAR(ctxt->cur, len);
Daniel Veillard4255d502002-04-16 15:50:10 +00004886 } else {
4887 ERROR("Expecting the end of a char range");
4888 return;
4889 }
William M. Brackdc99df92003-12-27 01:54:25 +00004890 NEXTL(len);
Daniel Veillard4255d502002-04-16 15:50:10 +00004891 /* TODO check that the values are acceptable character ranges for XML */
4892 if (end < start) {
4893 ERROR("End of range is before start of range");
4894 } else {
4895 xmlRegAtomAddRange(ctxt, ctxt->atom, ctxt->neg,
4896 XML_REGEXP_CHARVAL, start, end, NULL);
4897 }
4898 return;
4899}
4900
4901/**
4902 * xmlFAParsePosCharGroup:
Daniel Veillard441bc322002-04-20 17:38:48 +00004903 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00004904 *
4905 * [14] posCharGroup ::= ( charRange | charClassEsc )+
4906 */
4907static void
4908xmlFAParsePosCharGroup(xmlRegParserCtxtPtr ctxt) {
4909 do {
4910 if ((CUR == '\\') || (CUR == '.')) {
4911 xmlFAParseCharClassEsc(ctxt);
4912 } else {
4913 xmlFAParseCharRange(ctxt);
4914 }
4915 } while ((CUR != ']') && (CUR != '^') && (CUR != '-') &&
Daniel Veillard777737e2006-10-17 21:23:17 +00004916 (CUR != 0) && (ctxt->error == 0));
Daniel Veillard4255d502002-04-16 15:50:10 +00004917}
4918
4919/**
4920 * xmlFAParseCharGroup:
Daniel Veillard441bc322002-04-20 17:38:48 +00004921 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00004922 *
4923 * [13] charGroup ::= posCharGroup | negCharGroup | charClassSub
4924 * [15] negCharGroup ::= '^' posCharGroup
4925 * [16] charClassSub ::= ( posCharGroup | negCharGroup ) '-' charClassExpr
4926 * [12] charClassExpr ::= '[' charGroup ']'
4927 */
4928static void
4929xmlFAParseCharGroup(xmlRegParserCtxtPtr ctxt) {
4930 int n = ctxt->neg;
4931 while ((CUR != ']') && (ctxt->error == 0)) {
4932 if (CUR == '^') {
4933 int neg = ctxt->neg;
4934
4935 NEXT;
4936 ctxt->neg = !ctxt->neg;
4937 xmlFAParsePosCharGroup(ctxt);
4938 ctxt->neg = neg;
William M. Brack10f1ef42004-03-20 14:51:25 +00004939 } else if ((CUR == '-') && (NXT(1) == '[')) {
Daniel Veillardf8b9de32003-11-24 14:27:26 +00004940 int neg = ctxt->neg;
Daniel Veillardf8b9de32003-11-24 14:27:26 +00004941 ctxt->neg = 2;
William M. Brack10f1ef42004-03-20 14:51:25 +00004942 NEXT; /* eat the '-' */
4943 NEXT; /* eat the '[' */
Daniel Veillard4255d502002-04-16 15:50:10 +00004944 xmlFAParseCharGroup(ctxt);
4945 if (CUR == ']') {
4946 NEXT;
4947 } else {
4948 ERROR("charClassExpr: ']' expected");
4949 break;
4950 }
Daniel Veillardf8b9de32003-11-24 14:27:26 +00004951 ctxt->neg = neg;
Daniel Veillard4255d502002-04-16 15:50:10 +00004952 break;
4953 } else if (CUR != ']') {
4954 xmlFAParsePosCharGroup(ctxt);
4955 }
4956 }
4957 ctxt->neg = n;
4958}
4959
4960/**
4961 * xmlFAParseCharClass:
Daniel Veillard441bc322002-04-20 17:38:48 +00004962 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00004963 *
4964 * [11] charClass ::= charClassEsc | charClassExpr
4965 * [12] charClassExpr ::= '[' charGroup ']'
4966 */
4967static void
4968xmlFAParseCharClass(xmlRegParserCtxtPtr ctxt) {
4969 if (CUR == '[') {
4970 NEXT;
4971 ctxt->atom = xmlRegNewAtom(ctxt, XML_REGEXP_RANGES);
4972 if (ctxt->atom == NULL)
4973 return;
4974 xmlFAParseCharGroup(ctxt);
4975 if (CUR == ']') {
4976 NEXT;
4977 } else {
4978 ERROR("xmlFAParseCharClass: ']' expected");
4979 }
4980 } else {
4981 xmlFAParseCharClassEsc(ctxt);
4982 }
4983}
4984
4985/**
4986 * xmlFAParseQuantExact:
Daniel Veillard441bc322002-04-20 17:38:48 +00004987 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00004988 *
4989 * [8] QuantExact ::= [0-9]+
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00004990 *
4991 * Returns 0 if success or -1 in case of error
Daniel Veillard4255d502002-04-16 15:50:10 +00004992 */
4993static int
4994xmlFAParseQuantExact(xmlRegParserCtxtPtr ctxt) {
4995 int ret = 0;
4996 int ok = 0;
4997
4998 while ((CUR >= '0') && (CUR <= '9')) {
4999 ret = ret * 10 + (CUR - '0');
5000 ok = 1;
5001 NEXT;
5002 }
5003 if (ok != 1) {
5004 return(-1);
5005 }
5006 return(ret);
5007}
5008
5009/**
5010 * xmlFAParseQuantifier:
Daniel Veillard441bc322002-04-20 17:38:48 +00005011 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00005012 *
5013 * [4] quantifier ::= [?*+] | ( '{' quantity '}' )
5014 * [5] quantity ::= quantRange | quantMin | QuantExact
5015 * [6] quantRange ::= QuantExact ',' QuantExact
5016 * [7] quantMin ::= QuantExact ','
5017 * [8] QuantExact ::= [0-9]+
5018 */
5019static int
5020xmlFAParseQuantifier(xmlRegParserCtxtPtr ctxt) {
5021 int cur;
5022
5023 cur = CUR;
5024 if ((cur == '?') || (cur == '*') || (cur == '+')) {
5025 if (ctxt->atom != NULL) {
5026 if (cur == '?')
5027 ctxt->atom->quant = XML_REGEXP_QUANT_OPT;
5028 else if (cur == '*')
5029 ctxt->atom->quant = XML_REGEXP_QUANT_MULT;
5030 else if (cur == '+')
5031 ctxt->atom->quant = XML_REGEXP_QUANT_PLUS;
5032 }
5033 NEXT;
5034 return(1);
5035 }
5036 if (cur == '{') {
5037 int min = 0, max = 0;
5038
5039 NEXT;
5040 cur = xmlFAParseQuantExact(ctxt);
5041 if (cur >= 0)
5042 min = cur;
5043 if (CUR == ',') {
5044 NEXT;
Daniel Veillardebe48c62003-12-03 12:12:27 +00005045 if (CUR == '}')
5046 max = INT_MAX;
5047 else {
5048 cur = xmlFAParseQuantExact(ctxt);
5049 if (cur >= 0)
5050 max = cur;
5051 else {
5052 ERROR("Improper quantifier");
5053 }
5054 }
Daniel Veillard4255d502002-04-16 15:50:10 +00005055 }
5056 if (CUR == '}') {
5057 NEXT;
5058 } else {
5059 ERROR("Unterminated quantifier");
5060 }
5061 if (max == 0)
5062 max = min;
5063 if (ctxt->atom != NULL) {
5064 ctxt->atom->quant = XML_REGEXP_QUANT_RANGE;
5065 ctxt->atom->min = min;
5066 ctxt->atom->max = max;
5067 }
5068 return(1);
5069 }
5070 return(0);
5071}
5072
5073/**
5074 * xmlFAParseAtom:
Daniel Veillard441bc322002-04-20 17:38:48 +00005075 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00005076 *
5077 * [9] atom ::= Char | charClass | ( '(' regExp ')' )
5078 */
5079static int
5080xmlFAParseAtom(xmlRegParserCtxtPtr ctxt) {
5081 int codepoint, len;
5082
5083 codepoint = xmlFAIsChar(ctxt);
5084 if (codepoint > 0) {
5085 ctxt->atom = xmlRegNewAtom(ctxt, XML_REGEXP_CHARVAL);
5086 if (ctxt->atom == NULL)
5087 return(-1);
5088 codepoint = CUR_SCHAR(ctxt->cur, len);
5089 ctxt->atom->codepoint = codepoint;
5090 NEXTL(len);
5091 return(1);
5092 } else if (CUR == '|') {
5093 return(0);
5094 } else if (CUR == 0) {
5095 return(0);
5096 } else if (CUR == ')') {
5097 return(0);
5098 } else if (CUR == '(') {
5099 xmlRegStatePtr start, oldend;
5100
5101 NEXT;
5102 xmlFAGenerateEpsilonTransition(ctxt, ctxt->state, NULL);
5103 start = ctxt->state;
5104 oldend = ctxt->end;
5105 ctxt->end = NULL;
5106 ctxt->atom = NULL;
5107 xmlFAParseRegExp(ctxt, 0);
5108 if (CUR == ')') {
5109 NEXT;
5110 } else {
5111 ERROR("xmlFAParseAtom: expecting ')'");
5112 }
5113 ctxt->atom = xmlRegNewAtom(ctxt, XML_REGEXP_SUBREG);
5114 if (ctxt->atom == NULL)
5115 return(-1);
5116 ctxt->atom->start = start;
5117 ctxt->atom->stop = ctxt->state;
5118 ctxt->end = oldend;
5119 return(1);
5120 } else if ((CUR == '[') || (CUR == '\\') || (CUR == '.')) {
5121 xmlFAParseCharClass(ctxt);
5122 return(1);
5123 }
5124 return(0);
5125}
5126
5127/**
5128 * xmlFAParsePiece:
Daniel Veillard441bc322002-04-20 17:38:48 +00005129 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00005130 *
5131 * [3] piece ::= atom quantifier?
5132 */
5133static int
5134xmlFAParsePiece(xmlRegParserCtxtPtr ctxt) {
5135 int ret;
5136
5137 ctxt->atom = NULL;
5138 ret = xmlFAParseAtom(ctxt);
5139 if (ret == 0)
5140 return(0);
5141 if (ctxt->atom == NULL) {
5142 ERROR("internal: no atom generated");
5143 }
5144 xmlFAParseQuantifier(ctxt);
5145 return(1);
5146}
5147
5148/**
5149 * xmlFAParseBranch:
Daniel Veillard441bc322002-04-20 17:38:48 +00005150 * @ctxt: a regexp parser context
Daniel Veillard54eb0242006-03-21 23:17:57 +00005151 * @to: optional target to the end of the branch
5152 *
5153 * @to is used to optimize by removing duplicate path in automata
5154 * in expressions like (a|b)(c|d)
Daniel Veillard4255d502002-04-16 15:50:10 +00005155 *
5156 * [2] branch ::= piece*
5157 */
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00005158static int
Daniel Veillard54eb0242006-03-21 23:17:57 +00005159xmlFAParseBranch(xmlRegParserCtxtPtr ctxt, xmlRegStatePtr to) {
Daniel Veillard4255d502002-04-16 15:50:10 +00005160 xmlRegStatePtr previous;
Daniel Veillard4255d502002-04-16 15:50:10 +00005161 int ret;
5162
5163 previous = ctxt->state;
5164 ret = xmlFAParsePiece(ctxt);
5165 if (ret != 0) {
Daniel Veillard54eb0242006-03-21 23:17:57 +00005166 if (xmlFAGenerateTransitions(ctxt, previous,
5167 (CUR=='|' || CUR==')') ? to : NULL, ctxt->atom) < 0)
Daniel Veillard2cbf5962004-03-31 15:50:43 +00005168 return(-1);
5169 previous = ctxt->state;
Daniel Veillard4255d502002-04-16 15:50:10 +00005170 ctxt->atom = NULL;
5171 }
5172 while ((ret != 0) && (ctxt->error == 0)) {
5173 ret = xmlFAParsePiece(ctxt);
5174 if (ret != 0) {
Daniel Veillard54eb0242006-03-21 23:17:57 +00005175 if (xmlFAGenerateTransitions(ctxt, previous,
5176 (CUR=='|' || CUR==')') ? to : NULL, ctxt->atom) < 0)
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00005177 return(-1);
Daniel Veillard4255d502002-04-16 15:50:10 +00005178 previous = ctxt->state;
5179 ctxt->atom = NULL;
5180 }
5181 }
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00005182 return(0);
Daniel Veillard4255d502002-04-16 15:50:10 +00005183}
5184
5185/**
5186 * xmlFAParseRegExp:
Daniel Veillard441bc322002-04-20 17:38:48 +00005187 * @ctxt: a regexp parser context
William M. Brackddf71d62004-05-06 04:17:26 +00005188 * @top: is this the top-level expression ?
Daniel Veillard4255d502002-04-16 15:50:10 +00005189 *
5190 * [1] regExp ::= branch ( '|' branch )*
5191 */
5192static void
5193xmlFAParseRegExp(xmlRegParserCtxtPtr ctxt, int top) {
Daniel Veillardc7e3cc42004-09-28 12:33:52 +00005194 xmlRegStatePtr start, end;
Daniel Veillard4255d502002-04-16 15:50:10 +00005195
Daniel Veillard2cbf5962004-03-31 15:50:43 +00005196 /* if not top start should have been generated by an epsilon trans */
Daniel Veillard4255d502002-04-16 15:50:10 +00005197 start = ctxt->state;
Daniel Veillard2cbf5962004-03-31 15:50:43 +00005198 ctxt->end = NULL;
Daniel Veillard54eb0242006-03-21 23:17:57 +00005199 xmlFAParseBranch(ctxt, NULL);
Daniel Veillard2cbf5962004-03-31 15:50:43 +00005200 if (top) {
5201#ifdef DEBUG_REGEXP_GRAPH
5202 printf("State %d is final\n", ctxt->state->no);
5203#endif
5204 ctxt->state->type = XML_REGEXP_FINAL_STATE;
5205 }
Daniel Veillard4255d502002-04-16 15:50:10 +00005206 if (CUR != '|') {
5207 ctxt->end = ctxt->state;
5208 return;
5209 }
5210 end = ctxt->state;
5211 while ((CUR == '|') && (ctxt->error == 0)) {
5212 NEXT;
5213 ctxt->state = start;
Daniel Veillard2cbf5962004-03-31 15:50:43 +00005214 ctxt->end = NULL;
Daniel Veillard54eb0242006-03-21 23:17:57 +00005215 xmlFAParseBranch(ctxt, end);
Daniel Veillard4255d502002-04-16 15:50:10 +00005216 }
Daniel Veillard2cbf5962004-03-31 15:50:43 +00005217 if (!top) {
5218 ctxt->state = end;
5219 ctxt->end = end;
5220 }
Daniel Veillard4255d502002-04-16 15:50:10 +00005221}
5222
5223/************************************************************************
5224 * *
5225 * The basic API *
5226 * *
5227 ************************************************************************/
5228
5229/**
5230 * xmlRegexpPrint:
5231 * @output: the file for the output debug
5232 * @regexp: the compiled regexp
5233 *
5234 * Print the content of the compiled regular expression
5235 */
5236void
5237xmlRegexpPrint(FILE *output, xmlRegexpPtr regexp) {
5238 int i;
5239
Daniel Veillarda82b1822004-11-08 16:24:57 +00005240 if (output == NULL)
5241 return;
Daniel Veillard4255d502002-04-16 15:50:10 +00005242 fprintf(output, " regexp: ");
5243 if (regexp == NULL) {
5244 fprintf(output, "NULL\n");
5245 return;
5246 }
5247 fprintf(output, "'%s' ", regexp->string);
5248 fprintf(output, "\n");
5249 fprintf(output, "%d atoms:\n", regexp->nbAtoms);
5250 for (i = 0;i < regexp->nbAtoms; i++) {
5251 fprintf(output, " %02d ", i);
5252 xmlRegPrintAtom(output, regexp->atoms[i]);
5253 }
5254 fprintf(output, "%d states:", regexp->nbStates);
5255 fprintf(output, "\n");
5256 for (i = 0;i < regexp->nbStates; i++) {
5257 xmlRegPrintState(output, regexp->states[i]);
5258 }
5259 fprintf(output, "%d counters:\n", regexp->nbCounters);
5260 for (i = 0;i < regexp->nbCounters; i++) {
5261 fprintf(output, " %d: min %d max %d\n", i, regexp->counters[i].min,
5262 regexp->counters[i].max);
5263 }
5264}
5265
5266/**
5267 * xmlRegexpCompile:
5268 * @regexp: a regular expression string
5269 *
5270 * Parses a regular expression conforming to XML Schemas Part 2 Datatype
William M. Brackddf71d62004-05-06 04:17:26 +00005271 * Appendix F and builds an automata suitable for testing strings against
Daniel Veillard4255d502002-04-16 15:50:10 +00005272 * that regular expression
5273 *
5274 * Returns the compiled expression or NULL in case of error
5275 */
5276xmlRegexpPtr
5277xmlRegexpCompile(const xmlChar *regexp) {
5278 xmlRegexpPtr ret;
5279 xmlRegParserCtxtPtr ctxt;
5280
5281 ctxt = xmlRegNewParserCtxt(regexp);
5282 if (ctxt == NULL)
5283 return(NULL);
5284
5285 /* initialize the parser */
5286 ctxt->end = NULL;
5287 ctxt->start = ctxt->state = xmlRegNewState(ctxt);
5288 xmlRegStatePush(ctxt, ctxt->start);
5289
5290 /* parse the expression building an automata */
5291 xmlFAParseRegExp(ctxt, 1);
5292 if (CUR != 0) {
5293 ERROR("xmlFAParseRegExp: extra characters");
5294 }
5295 ctxt->end = ctxt->state;
5296 ctxt->start->type = XML_REGEXP_START_STATE;
5297 ctxt->end->type = XML_REGEXP_FINAL_STATE;
5298
5299 /* remove the Epsilon except for counted transitions */
5300 xmlFAEliminateEpsilonTransitions(ctxt);
5301
5302
5303 if (ctxt->error != 0) {
5304 xmlRegFreeParserCtxt(ctxt);
5305 return(NULL);
5306 }
5307 ret = xmlRegEpxFromParse(ctxt);
5308 xmlRegFreeParserCtxt(ctxt);
5309 return(ret);
5310}
5311
5312/**
5313 * xmlRegexpExec:
5314 * @comp: the compiled regular expression
5315 * @content: the value to check against the regular expression
5316 *
William M. Brackddf71d62004-05-06 04:17:26 +00005317 * Check if the regular expression generates the value
Daniel Veillard4255d502002-04-16 15:50:10 +00005318 *
William M. Brackddf71d62004-05-06 04:17:26 +00005319 * Returns 1 if it matches, 0 if not and a negative value in case of error
Daniel Veillard4255d502002-04-16 15:50:10 +00005320 */
5321int
5322xmlRegexpExec(xmlRegexpPtr comp, const xmlChar *content) {
5323 if ((comp == NULL) || (content == NULL))
5324 return(-1);
5325 return(xmlFARegExec(comp, content));
5326}
5327
5328/**
Daniel Veillard23e73572002-09-19 19:56:43 +00005329 * xmlRegexpIsDeterminist:
5330 * @comp: the compiled regular expression
5331 *
5332 * Check if the regular expression is determinist
5333 *
William M. Brackddf71d62004-05-06 04:17:26 +00005334 * Returns 1 if it yes, 0 if not and a negative value in case of error
Daniel Veillard23e73572002-09-19 19:56:43 +00005335 */
5336int
5337xmlRegexpIsDeterminist(xmlRegexpPtr comp) {
5338 xmlAutomataPtr am;
5339 int ret;
5340
5341 if (comp == NULL)
5342 return(-1);
5343 if (comp->determinist != -1)
5344 return(comp->determinist);
5345
5346 am = xmlNewAutomata();
Daniel Veillardbd9afb52002-09-25 22:25:35 +00005347 if (am->states != NULL) {
5348 int i;
5349
5350 for (i = 0;i < am->nbStates;i++)
5351 xmlRegFreeState(am->states[i]);
5352 xmlFree(am->states);
5353 }
Daniel Veillard23e73572002-09-19 19:56:43 +00005354 am->nbAtoms = comp->nbAtoms;
5355 am->atoms = comp->atoms;
5356 am->nbStates = comp->nbStates;
5357 am->states = comp->states;
5358 am->determinist = -1;
5359 ret = xmlFAComputesDeterminism(am);
5360 am->atoms = NULL;
5361 am->states = NULL;
5362 xmlFreeAutomata(am);
5363 return(ret);
5364}
5365
5366/**
Daniel Veillard4255d502002-04-16 15:50:10 +00005367 * xmlRegFreeRegexp:
5368 * @regexp: the regexp
5369 *
5370 * Free a regexp
5371 */
5372void
5373xmlRegFreeRegexp(xmlRegexpPtr regexp) {
5374 int i;
5375 if (regexp == NULL)
5376 return;
5377
5378 if (regexp->string != NULL)
5379 xmlFree(regexp->string);
5380 if (regexp->states != NULL) {
5381 for (i = 0;i < regexp->nbStates;i++)
5382 xmlRegFreeState(regexp->states[i]);
5383 xmlFree(regexp->states);
5384 }
5385 if (regexp->atoms != NULL) {
5386 for (i = 0;i < regexp->nbAtoms;i++)
5387 xmlRegFreeAtom(regexp->atoms[i]);
5388 xmlFree(regexp->atoms);
5389 }
5390 if (regexp->counters != NULL)
5391 xmlFree(regexp->counters);
Daniel Veillard23e73572002-09-19 19:56:43 +00005392 if (regexp->compact != NULL)
5393 xmlFree(regexp->compact);
Daniel Veillard118aed72002-09-24 14:13:13 +00005394 if (regexp->transdata != NULL)
5395 xmlFree(regexp->transdata);
Daniel Veillard23e73572002-09-19 19:56:43 +00005396 if (regexp->stringMap != NULL) {
5397 for (i = 0; i < regexp->nbstrings;i++)
5398 xmlFree(regexp->stringMap[i]);
5399 xmlFree(regexp->stringMap);
5400 }
5401
Daniel Veillard4255d502002-04-16 15:50:10 +00005402 xmlFree(regexp);
5403}
5404
5405#ifdef LIBXML_AUTOMATA_ENABLED
5406/************************************************************************
5407 * *
5408 * The Automata interface *
5409 * *
5410 ************************************************************************/
5411
5412/**
5413 * xmlNewAutomata:
5414 *
5415 * Create a new automata
5416 *
5417 * Returns the new object or NULL in case of failure
5418 */
5419xmlAutomataPtr
5420xmlNewAutomata(void) {
5421 xmlAutomataPtr ctxt;
5422
5423 ctxt = xmlRegNewParserCtxt(NULL);
5424 if (ctxt == NULL)
5425 return(NULL);
5426
5427 /* initialize the parser */
5428 ctxt->end = NULL;
5429 ctxt->start = ctxt->state = xmlRegNewState(ctxt);
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00005430 if (ctxt->start == NULL) {
5431 xmlFreeAutomata(ctxt);
5432 return(NULL);
5433 }
Daniel Veillardd0271472006-01-02 10:22:02 +00005434 ctxt->start->type = XML_REGEXP_START_STATE;
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00005435 if (xmlRegStatePush(ctxt, ctxt->start) < 0) {
5436 xmlRegFreeState(ctxt->start);
5437 xmlFreeAutomata(ctxt);
5438 return(NULL);
5439 }
Daniel Veillard4255d502002-04-16 15:50:10 +00005440
5441 return(ctxt);
5442}
5443
5444/**
5445 * xmlFreeAutomata:
5446 * @am: an automata
5447 *
5448 * Free an automata
5449 */
5450void
5451xmlFreeAutomata(xmlAutomataPtr am) {
5452 if (am == NULL)
5453 return;
5454 xmlRegFreeParserCtxt(am);
5455}
5456
5457/**
5458 * xmlAutomataGetInitState:
5459 * @am: an automata
5460 *
Daniel Veillarda9b66d02002-12-11 14:23:49 +00005461 * Initial state lookup
5462 *
Daniel Veillard4255d502002-04-16 15:50:10 +00005463 * Returns the initial state of the automata
5464 */
5465xmlAutomataStatePtr
5466xmlAutomataGetInitState(xmlAutomataPtr am) {
5467 if (am == NULL)
5468 return(NULL);
5469 return(am->start);
5470}
5471
5472/**
5473 * xmlAutomataSetFinalState:
5474 * @am: an automata
5475 * @state: a state in this automata
5476 *
5477 * Makes that state a final state
5478 *
5479 * Returns 0 or -1 in case of error
5480 */
5481int
5482xmlAutomataSetFinalState(xmlAutomataPtr am, xmlAutomataStatePtr state) {
5483 if ((am == NULL) || (state == NULL))
5484 return(-1);
5485 state->type = XML_REGEXP_FINAL_STATE;
5486 return(0);
5487}
5488
5489/**
5490 * xmlAutomataNewTransition:
5491 * @am: an automata
5492 * @from: the starting point of the transition
5493 * @to: the target point of the transition or NULL
5494 * @token: the input string associated to that transition
5495 * @data: data passed to the callback function if the transition is activated
5496 *
William M. Brackddf71d62004-05-06 04:17:26 +00005497 * If @to is NULL, this creates first a new target state in the automata
Daniel Veillard4255d502002-04-16 15:50:10 +00005498 * and then adds a transition from the @from state to the target state
5499 * activated by the value of @token
5500 *
5501 * Returns the target state or NULL in case of error
5502 */
5503xmlAutomataStatePtr
5504xmlAutomataNewTransition(xmlAutomataPtr am, xmlAutomataStatePtr from,
5505 xmlAutomataStatePtr to, const xmlChar *token,
5506 void *data) {
5507 xmlRegAtomPtr atom;
5508
5509 if ((am == NULL) || (from == NULL) || (token == NULL))
5510 return(NULL);
5511 atom = xmlRegNewAtom(am, XML_REGEXP_STRING);
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00005512 if (atom == NULL)
5513 return(NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +00005514 atom->data = data;
5515 if (atom == NULL)
5516 return(NULL);
5517 atom->valuep = xmlStrdup(token);
5518
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00005519 if (xmlFAGenerateTransitions(am, from, to, atom) < 0) {
5520 xmlRegFreeAtom(atom);
5521 return(NULL);
5522 }
Daniel Veillard4255d502002-04-16 15:50:10 +00005523 if (to == NULL)
5524 return(am->state);
5525 return(to);
5526}
5527
5528/**
Daniel Veillard52b48c72003-04-13 19:53:42 +00005529 * xmlAutomataNewTransition2:
5530 * @am: an automata
5531 * @from: the starting point of the transition
5532 * @to: the target point of the transition or NULL
5533 * @token: the first input string associated to that transition
5534 * @token2: the second input string associated to that transition
5535 * @data: data passed to the callback function if the transition is activated
5536 *
William M. Brackddf71d62004-05-06 04:17:26 +00005537 * If @to is NULL, this creates first a new target state in the automata
Daniel Veillard52b48c72003-04-13 19:53:42 +00005538 * and then adds a transition from the @from state to the target state
5539 * activated by the value of @token
5540 *
5541 * Returns the target state or NULL in case of error
5542 */
5543xmlAutomataStatePtr
5544xmlAutomataNewTransition2(xmlAutomataPtr am, xmlAutomataStatePtr from,
5545 xmlAutomataStatePtr to, const xmlChar *token,
5546 const xmlChar *token2, void *data) {
5547 xmlRegAtomPtr atom;
5548
5549 if ((am == NULL) || (from == NULL) || (token == NULL))
5550 return(NULL);
5551 atom = xmlRegNewAtom(am, XML_REGEXP_STRING);
Daniel Veillard52b48c72003-04-13 19:53:42 +00005552 if (atom == NULL)
5553 return(NULL);
Daniel Veillard11ce4002006-03-10 00:36:23 +00005554 atom->data = data;
Daniel Veillard52b48c72003-04-13 19:53:42 +00005555 if ((token2 == NULL) || (*token2 == 0)) {
5556 atom->valuep = xmlStrdup(token);
5557 } else {
5558 int lenn, lenp;
5559 xmlChar *str;
5560
5561 lenn = strlen((char *) token2);
5562 lenp = strlen((char *) token);
5563
Daniel Veillard3c908dc2003-04-19 00:07:51 +00005564 str = (xmlChar *) xmlMallocAtomic(lenn + lenp + 2);
Daniel Veillard52b48c72003-04-13 19:53:42 +00005565 if (str == NULL) {
5566 xmlRegFreeAtom(atom);
5567 return(NULL);
5568 }
5569 memcpy(&str[0], token, lenp);
5570 str[lenp] = '|';
5571 memcpy(&str[lenp + 1], token2, lenn);
5572 str[lenn + lenp + 1] = 0;
5573
5574 atom->valuep = str;
5575 }
5576
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00005577 if (xmlFAGenerateTransitions(am, from, to, atom) < 0) {
5578 xmlRegFreeAtom(atom);
5579 return(NULL);
5580 }
Daniel Veillard52b48c72003-04-13 19:53:42 +00005581 if (to == NULL)
5582 return(am->state);
5583 return(to);
5584}
5585
5586/**
Daniel Veillard9efc4762005-07-19 14:33:55 +00005587 * xmlAutomataNewNegTrans:
5588 * @am: an automata
5589 * @from: the starting point of the transition
5590 * @to: the target point of the transition or NULL
5591 * @token: the first input string associated to that transition
5592 * @token2: the second input string associated to that transition
5593 * @data: data passed to the callback function if the transition is activated
5594 *
5595 * If @to is NULL, this creates first a new target state in the automata
5596 * and then adds a transition from the @from state to the target state
5597 * activated by any value except (@token,@token2)
Daniel Veillard6e65e152005-08-09 11:09:52 +00005598 * Note that if @token2 is not NULL, then (X, NULL) won't match to follow
5599 # the semantic of XSD ##other
Daniel Veillard9efc4762005-07-19 14:33:55 +00005600 *
5601 * Returns the target state or NULL in case of error
5602 */
5603xmlAutomataStatePtr
5604xmlAutomataNewNegTrans(xmlAutomataPtr am, xmlAutomataStatePtr from,
5605 xmlAutomataStatePtr to, const xmlChar *token,
5606 const xmlChar *token2, void *data) {
5607 xmlRegAtomPtr atom;
Daniel Veillard77005e62005-07-19 16:26:18 +00005608 xmlChar err_msg[200];
Daniel Veillard9efc4762005-07-19 14:33:55 +00005609
5610 if ((am == NULL) || (from == NULL) || (token == NULL))
5611 return(NULL);
5612 atom = xmlRegNewAtom(am, XML_REGEXP_STRING);
5613 if (atom == NULL)
5614 return(NULL);
5615 atom->data = data;
5616 atom->neg = 1;
5617 if ((token2 == NULL) || (*token2 == 0)) {
5618 atom->valuep = xmlStrdup(token);
5619 } else {
5620 int lenn, lenp;
5621 xmlChar *str;
5622
5623 lenn = strlen((char *) token2);
5624 lenp = strlen((char *) token);
5625
5626 str = (xmlChar *) xmlMallocAtomic(lenn + lenp + 2);
5627 if (str == NULL) {
5628 xmlRegFreeAtom(atom);
5629 return(NULL);
5630 }
5631 memcpy(&str[0], token, lenp);
5632 str[lenp] = '|';
5633 memcpy(&str[lenp + 1], token2, lenn);
5634 str[lenn + lenp + 1] = 0;
5635
5636 atom->valuep = str;
5637 }
Daniel Veillarddb68b742005-07-30 13:18:24 +00005638 snprintf((char *) err_msg, 199, "not %s", (const char *) atom->valuep);
Daniel Veillard77005e62005-07-19 16:26:18 +00005639 err_msg[199] = 0;
5640 atom->valuep2 = xmlStrdup(err_msg);
Daniel Veillard9efc4762005-07-19 14:33:55 +00005641
5642 if (xmlFAGenerateTransitions(am, from, to, atom) < 0) {
5643 xmlRegFreeAtom(atom);
5644 return(NULL);
5645 }
Daniel Veillard6e65e152005-08-09 11:09:52 +00005646 am->negs++;
Daniel Veillard9efc4762005-07-19 14:33:55 +00005647 if (to == NULL)
5648 return(am->state);
5649 return(to);
5650}
5651
5652/**
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00005653 * xmlAutomataNewCountTrans2:
5654 * @am: an automata
5655 * @from: the starting point of the transition
5656 * @to: the target point of the transition or NULL
5657 * @token: the input string associated to that transition
5658 * @token2: the second input string associated to that transition
5659 * @min: the minimum successive occurences of token
5660 * @max: the maximum successive occurences of token
5661 * @data: data associated to the transition
5662 *
5663 * If @to is NULL, this creates first a new target state in the automata
5664 * and then adds a transition from the @from state to the target state
5665 * activated by a succession of input of value @token and @token2 and
5666 * whose number is between @min and @max
5667 *
5668 * Returns the target state or NULL in case of error
5669 */
5670xmlAutomataStatePtr
5671xmlAutomataNewCountTrans2(xmlAutomataPtr am, xmlAutomataStatePtr from,
5672 xmlAutomataStatePtr to, const xmlChar *token,
5673 const xmlChar *token2,
5674 int min, int max, void *data) {
5675 xmlRegAtomPtr atom;
5676 int counter;
5677
5678 if ((am == NULL) || (from == NULL) || (token == NULL))
5679 return(NULL);
5680 if (min < 0)
5681 return(NULL);
5682 if ((max < min) || (max < 1))
5683 return(NULL);
5684 atom = xmlRegNewAtom(am, XML_REGEXP_STRING);
5685 if (atom == NULL)
5686 return(NULL);
5687 if ((token2 == NULL) || (*token2 == 0)) {
5688 atom->valuep = xmlStrdup(token);
5689 } else {
5690 int lenn, lenp;
5691 xmlChar *str;
5692
5693 lenn = strlen((char *) token2);
5694 lenp = strlen((char *) token);
5695
5696 str = (xmlChar *) xmlMallocAtomic(lenn + lenp + 2);
5697 if (str == NULL) {
5698 xmlRegFreeAtom(atom);
5699 return(NULL);
5700 }
5701 memcpy(&str[0], token, lenp);
5702 str[lenp] = '|';
5703 memcpy(&str[lenp + 1], token2, lenn);
5704 str[lenn + lenp + 1] = 0;
5705
5706 atom->valuep = str;
5707 }
5708 atom->data = data;
5709 if (min == 0)
5710 atom->min = 1;
5711 else
5712 atom->min = min;
5713 atom->max = max;
5714
5715 /*
5716 * associate a counter to the transition.
5717 */
5718 counter = xmlRegGetCounter(am);
5719 am->counters[counter].min = min;
5720 am->counters[counter].max = max;
5721
5722 /* xmlFAGenerateTransitions(am, from, to, atom); */
5723 if (to == NULL) {
5724 to = xmlRegNewState(am);
5725 xmlRegStatePush(am, to);
5726 }
Daniel Veillard5de09382005-09-26 17:18:17 +00005727 xmlRegStateAddTrans(am, from, atom, to, counter, -1);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00005728 xmlRegAtomPush(am, atom);
5729 am->state = to;
5730
5731 if (to == NULL)
5732 to = am->state;
5733 if (to == NULL)
5734 return(NULL);
5735 if (min == 0)
5736 xmlFAGenerateEpsilonTransition(am, from, to);
5737 return(to);
5738}
5739
5740/**
Daniel Veillard4255d502002-04-16 15:50:10 +00005741 * xmlAutomataNewCountTrans:
5742 * @am: an automata
5743 * @from: the starting point of the transition
5744 * @to: the target point of the transition or NULL
5745 * @token: the input string associated to that transition
5746 * @min: the minimum successive occurences of token
Daniel Veillarda9b66d02002-12-11 14:23:49 +00005747 * @max: the maximum successive occurences of token
5748 * @data: data associated to the transition
Daniel Veillard4255d502002-04-16 15:50:10 +00005749 *
William M. Brackddf71d62004-05-06 04:17:26 +00005750 * If @to is NULL, this creates first a new target state in the automata
Daniel Veillard4255d502002-04-16 15:50:10 +00005751 * and then adds a transition from the @from state to the target state
5752 * activated by a succession of input of value @token and whose number
5753 * is between @min and @max
5754 *
5755 * Returns the target state or NULL in case of error
5756 */
5757xmlAutomataStatePtr
5758xmlAutomataNewCountTrans(xmlAutomataPtr am, xmlAutomataStatePtr from,
5759 xmlAutomataStatePtr to, const xmlChar *token,
5760 int min, int max, void *data) {
5761 xmlRegAtomPtr atom;
Daniel Veillard0ddb21c2004-02-12 12:43:49 +00005762 int counter;
Daniel Veillard4255d502002-04-16 15:50:10 +00005763
5764 if ((am == NULL) || (from == NULL) || (token == NULL))
5765 return(NULL);
5766 if (min < 0)
5767 return(NULL);
5768 if ((max < min) || (max < 1))
5769 return(NULL);
5770 atom = xmlRegNewAtom(am, XML_REGEXP_STRING);
5771 if (atom == NULL)
5772 return(NULL);
5773 atom->valuep = xmlStrdup(token);
5774 atom->data = data;
5775 if (min == 0)
5776 atom->min = 1;
5777 else
5778 atom->min = min;
5779 atom->max = max;
5780
Daniel Veillard0ddb21c2004-02-12 12:43:49 +00005781 /*
5782 * associate a counter to the transition.
5783 */
5784 counter = xmlRegGetCounter(am);
5785 am->counters[counter].min = min;
5786 am->counters[counter].max = max;
5787
5788 /* xmlFAGenerateTransitions(am, from, to, atom); */
5789 if (to == NULL) {
5790 to = xmlRegNewState(am);
5791 xmlRegStatePush(am, to);
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00005792 }
Daniel Veillard5de09382005-09-26 17:18:17 +00005793 xmlRegStateAddTrans(am, from, atom, to, counter, -1);
Daniel Veillard0ddb21c2004-02-12 12:43:49 +00005794 xmlRegAtomPush(am, atom);
5795 am->state = to;
5796
Daniel Veillard4255d502002-04-16 15:50:10 +00005797 if (to == NULL)
5798 to = am->state;
5799 if (to == NULL)
5800 return(NULL);
5801 if (min == 0)
5802 xmlFAGenerateEpsilonTransition(am, from, to);
5803 return(to);
5804}
5805
5806/**
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00005807 * xmlAutomataNewOnceTrans2:
5808 * @am: an automata
5809 * @from: the starting point of the transition
5810 * @to: the target point of the transition or NULL
5811 * @token: the input string associated to that transition
5812 * @token2: the second input string associated to that transition
5813 * @min: the minimum successive occurences of token
5814 * @max: the maximum successive occurences of token
5815 * @data: data associated to the transition
5816 *
5817 * If @to is NULL, this creates first a new target state in the automata
5818 * and then adds a transition from the @from state to the target state
5819 * activated by a succession of input of value @token and @token2 and whose
5820 * number is between @min and @max, moreover that transition can only be
5821 * crossed once.
5822 *
5823 * Returns the target state or NULL in case of error
5824 */
5825xmlAutomataStatePtr
5826xmlAutomataNewOnceTrans2(xmlAutomataPtr am, xmlAutomataStatePtr from,
5827 xmlAutomataStatePtr to, const xmlChar *token,
5828 const xmlChar *token2,
5829 int min, int max, void *data) {
5830 xmlRegAtomPtr atom;
5831 int counter;
5832
5833 if ((am == NULL) || (from == NULL) || (token == NULL))
5834 return(NULL);
5835 if (min < 1)
5836 return(NULL);
5837 if ((max < min) || (max < 1))
5838 return(NULL);
5839 atom = xmlRegNewAtom(am, XML_REGEXP_STRING);
5840 if (atom == NULL)
5841 return(NULL);
5842 if ((token2 == NULL) || (*token2 == 0)) {
5843 atom->valuep = xmlStrdup(token);
5844 } else {
5845 int lenn, lenp;
5846 xmlChar *str;
5847
5848 lenn = strlen((char *) token2);
5849 lenp = strlen((char *) token);
5850
5851 str = (xmlChar *) xmlMallocAtomic(lenn + lenp + 2);
5852 if (str == NULL) {
5853 xmlRegFreeAtom(atom);
5854 return(NULL);
5855 }
5856 memcpy(&str[0], token, lenp);
5857 str[lenp] = '|';
5858 memcpy(&str[lenp + 1], token2, lenn);
5859 str[lenn + lenp + 1] = 0;
5860
5861 atom->valuep = str;
5862 }
5863 atom->data = data;
5864 atom->quant = XML_REGEXP_QUANT_ONCEONLY;
Daniel Veillard11ce4002006-03-10 00:36:23 +00005865 atom->min = min;
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00005866 atom->max = max;
5867 /*
5868 * associate a counter to the transition.
5869 */
5870 counter = xmlRegGetCounter(am);
5871 am->counters[counter].min = 1;
5872 am->counters[counter].max = 1;
5873
5874 /* xmlFAGenerateTransitions(am, from, to, atom); */
5875 if (to == NULL) {
5876 to = xmlRegNewState(am);
5877 xmlRegStatePush(am, to);
5878 }
Daniel Veillard5de09382005-09-26 17:18:17 +00005879 xmlRegStateAddTrans(am, from, atom, to, counter, -1);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00005880 xmlRegAtomPush(am, atom);
5881 am->state = to;
5882 return(to);
5883}
5884
5885
5886
5887/**
Daniel Veillard7646b182002-04-20 06:41:40 +00005888 * xmlAutomataNewOnceTrans:
5889 * @am: an automata
5890 * @from: the starting point of the transition
5891 * @to: the target point of the transition or NULL
5892 * @token: the input string associated to that transition
5893 * @min: the minimum successive occurences of token
Daniel Veillarda9b66d02002-12-11 14:23:49 +00005894 * @max: the maximum successive occurences of token
5895 * @data: data associated to the transition
Daniel Veillard7646b182002-04-20 06:41:40 +00005896 *
William M. Brackddf71d62004-05-06 04:17:26 +00005897 * If @to is NULL, this creates first a new target state in the automata
Daniel Veillard7646b182002-04-20 06:41:40 +00005898 * and then adds a transition from the @from state to the target state
5899 * activated by a succession of input of value @token and whose number
William M. Brackddf71d62004-05-06 04:17:26 +00005900 * is between @min and @max, moreover that transition can only be crossed
Daniel Veillard7646b182002-04-20 06:41:40 +00005901 * once.
5902 *
5903 * Returns the target state or NULL in case of error
5904 */
5905xmlAutomataStatePtr
5906xmlAutomataNewOnceTrans(xmlAutomataPtr am, xmlAutomataStatePtr from,
5907 xmlAutomataStatePtr to, const xmlChar *token,
5908 int min, int max, void *data) {
5909 xmlRegAtomPtr atom;
5910 int counter;
5911
5912 if ((am == NULL) || (from == NULL) || (token == NULL))
5913 return(NULL);
5914 if (min < 1)
5915 return(NULL);
5916 if ((max < min) || (max < 1))
5917 return(NULL);
5918 atom = xmlRegNewAtom(am, XML_REGEXP_STRING);
5919 if (atom == NULL)
5920 return(NULL);
5921 atom->valuep = xmlStrdup(token);
5922 atom->data = data;
5923 atom->quant = XML_REGEXP_QUANT_ONCEONLY;
Daniel Veillard11ce4002006-03-10 00:36:23 +00005924 atom->min = min;
Daniel Veillard7646b182002-04-20 06:41:40 +00005925 atom->max = max;
5926 /*
5927 * associate a counter to the transition.
5928 */
5929 counter = xmlRegGetCounter(am);
5930 am->counters[counter].min = 1;
5931 am->counters[counter].max = 1;
5932
5933 /* xmlFAGenerateTransitions(am, from, to, atom); */
5934 if (to == NULL) {
5935 to = xmlRegNewState(am);
5936 xmlRegStatePush(am, to);
5937 }
Daniel Veillard5de09382005-09-26 17:18:17 +00005938 xmlRegStateAddTrans(am, from, atom, to, counter, -1);
Daniel Veillard7646b182002-04-20 06:41:40 +00005939 xmlRegAtomPush(am, atom);
5940 am->state = to;
Daniel Veillard7646b182002-04-20 06:41:40 +00005941 return(to);
5942}
5943
5944/**
Daniel Veillard4255d502002-04-16 15:50:10 +00005945 * xmlAutomataNewState:
5946 * @am: an automata
5947 *
5948 * Create a new disconnected state in the automata
5949 *
5950 * Returns the new state or NULL in case of error
5951 */
5952xmlAutomataStatePtr
5953xmlAutomataNewState(xmlAutomataPtr am) {
5954 xmlAutomataStatePtr to;
5955
5956 if (am == NULL)
5957 return(NULL);
5958 to = xmlRegNewState(am);
5959 xmlRegStatePush(am, to);
5960 return(to);
5961}
5962
5963/**
Daniel Veillarda9b66d02002-12-11 14:23:49 +00005964 * xmlAutomataNewEpsilon:
Daniel Veillard4255d502002-04-16 15:50:10 +00005965 * @am: an automata
5966 * @from: the starting point of the transition
5967 * @to: the target point of the transition or NULL
5968 *
William M. Brackddf71d62004-05-06 04:17:26 +00005969 * If @to is NULL, this creates first a new target state in the automata
5970 * and then adds an epsilon transition from the @from state to the
Daniel Veillard4255d502002-04-16 15:50:10 +00005971 * target state
5972 *
5973 * Returns the target state or NULL in case of error
5974 */
5975xmlAutomataStatePtr
5976xmlAutomataNewEpsilon(xmlAutomataPtr am, xmlAutomataStatePtr from,
5977 xmlAutomataStatePtr to) {
5978 if ((am == NULL) || (from == NULL))
5979 return(NULL);
5980 xmlFAGenerateEpsilonTransition(am, from, to);
5981 if (to == NULL)
5982 return(am->state);
5983 return(to);
5984}
5985
Daniel Veillardb509f152002-04-17 16:28:10 +00005986/**
Daniel Veillard7646b182002-04-20 06:41:40 +00005987 * xmlAutomataNewAllTrans:
5988 * @am: an automata
5989 * @from: the starting point of the transition
5990 * @to: the target point of the transition or NULL
Daniel Veillarda9b66d02002-12-11 14:23:49 +00005991 * @lax: allow to transition if not all all transitions have been activated
Daniel Veillard7646b182002-04-20 06:41:40 +00005992 *
William M. Brackddf71d62004-05-06 04:17:26 +00005993 * If @to is NULL, this creates first a new target state in the automata
Daniel Veillard7646b182002-04-20 06:41:40 +00005994 * and then adds a an ALL transition from the @from state to the
5995 * target state. That transition is an epsilon transition allowed only when
5996 * all transitions from the @from node have been activated.
5997 *
5998 * Returns the target state or NULL in case of error
5999 */
6000xmlAutomataStatePtr
6001xmlAutomataNewAllTrans(xmlAutomataPtr am, xmlAutomataStatePtr from,
Daniel Veillard441bc322002-04-20 17:38:48 +00006002 xmlAutomataStatePtr to, int lax) {
Daniel Veillard7646b182002-04-20 06:41:40 +00006003 if ((am == NULL) || (from == NULL))
6004 return(NULL);
Daniel Veillard441bc322002-04-20 17:38:48 +00006005 xmlFAGenerateAllTransition(am, from, to, lax);
Daniel Veillard7646b182002-04-20 06:41:40 +00006006 if (to == NULL)
6007 return(am->state);
6008 return(to);
6009}
6010
6011/**
Daniel Veillardb509f152002-04-17 16:28:10 +00006012 * xmlAutomataNewCounter:
6013 * @am: an automata
6014 * @min: the minimal value on the counter
6015 * @max: the maximal value on the counter
6016 *
6017 * Create a new counter
6018 *
6019 * Returns the counter number or -1 in case of error
6020 */
6021int
6022xmlAutomataNewCounter(xmlAutomataPtr am, int min, int max) {
6023 int ret;
6024
6025 if (am == NULL)
6026 return(-1);
6027
6028 ret = xmlRegGetCounter(am);
6029 if (ret < 0)
6030 return(-1);
6031 am->counters[ret].min = min;
6032 am->counters[ret].max = max;
6033 return(ret);
6034}
6035
6036/**
6037 * xmlAutomataNewCountedTrans:
6038 * @am: an automata
6039 * @from: the starting point of the transition
6040 * @to: the target point of the transition or NULL
6041 * @counter: the counter associated to that transition
6042 *
William M. Brackddf71d62004-05-06 04:17:26 +00006043 * If @to is NULL, this creates first a new target state in the automata
Daniel Veillardb509f152002-04-17 16:28:10 +00006044 * and then adds an epsilon transition from the @from state to the target state
6045 * which will increment the counter provided
6046 *
6047 * Returns the target state or NULL in case of error
6048 */
6049xmlAutomataStatePtr
6050xmlAutomataNewCountedTrans(xmlAutomataPtr am, xmlAutomataStatePtr from,
6051 xmlAutomataStatePtr to, int counter) {
6052 if ((am == NULL) || (from == NULL) || (counter < 0))
6053 return(NULL);
6054 xmlFAGenerateCountedEpsilonTransition(am, from, to, counter);
6055 if (to == NULL)
6056 return(am->state);
6057 return(to);
6058}
6059
6060/**
6061 * xmlAutomataNewCounterTrans:
6062 * @am: an automata
6063 * @from: the starting point of the transition
6064 * @to: the target point of the transition or NULL
6065 * @counter: the counter associated to that transition
6066 *
William M. Brackddf71d62004-05-06 04:17:26 +00006067 * If @to is NULL, this creates first a new target state in the automata
Daniel Veillardb509f152002-04-17 16:28:10 +00006068 * and then adds an epsilon transition from the @from state to the target state
6069 * which will be allowed only if the counter is within the right range.
6070 *
6071 * Returns the target state or NULL in case of error
6072 */
6073xmlAutomataStatePtr
6074xmlAutomataNewCounterTrans(xmlAutomataPtr am, xmlAutomataStatePtr from,
6075 xmlAutomataStatePtr to, int counter) {
6076 if ((am == NULL) || (from == NULL) || (counter < 0))
6077 return(NULL);
6078 xmlFAGenerateCountedTransition(am, from, to, counter);
6079 if (to == NULL)
6080 return(am->state);
6081 return(to);
6082}
Daniel Veillard4255d502002-04-16 15:50:10 +00006083
6084/**
6085 * xmlAutomataCompile:
6086 * @am: an automata
6087 *
6088 * Compile the automata into a Reg Exp ready for being executed.
6089 * The automata should be free after this point.
6090 *
6091 * Returns the compiled regexp or NULL in case of error
6092 */
6093xmlRegexpPtr
6094xmlAutomataCompile(xmlAutomataPtr am) {
6095 xmlRegexpPtr ret;
6096
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00006097 if ((am == NULL) || (am->error != 0)) return(NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +00006098 xmlFAEliminateEpsilonTransitions(am);
Daniel Veillard23e73572002-09-19 19:56:43 +00006099 /* xmlFAComputesDeterminism(am); */
Daniel Veillard4255d502002-04-16 15:50:10 +00006100 ret = xmlRegEpxFromParse(am);
6101
6102 return(ret);
6103}
Daniel Veillarde19fc232002-04-22 16:01:24 +00006104
6105/**
6106 * xmlAutomataIsDeterminist:
6107 * @am: an automata
6108 *
6109 * Checks if an automata is determinist.
6110 *
6111 * Returns 1 if true, 0 if not, and -1 in case of error
6112 */
6113int
6114xmlAutomataIsDeterminist(xmlAutomataPtr am) {
6115 int ret;
6116
6117 if (am == NULL)
6118 return(-1);
6119
6120 ret = xmlFAComputesDeterminism(am);
6121 return(ret);
6122}
Daniel Veillard4255d502002-04-16 15:50:10 +00006123#endif /* LIBXML_AUTOMATA_ENABLED */
Daniel Veillard81a8ec62005-08-22 00:20:58 +00006124
6125#ifdef LIBXML_EXPR_ENABLED
6126/************************************************************************
6127 * *
6128 * Formal Expression handling code *
6129 * *
6130 ************************************************************************/
Daniel Veillard81a8ec62005-08-22 00:20:58 +00006131/************************************************************************
6132 * *
6133 * Expression handling context *
6134 * *
6135 ************************************************************************/
6136
6137struct _xmlExpCtxt {
6138 xmlDictPtr dict;
6139 xmlExpNodePtr *table;
6140 int size;
6141 int nbElems;
6142 int nb_nodes;
6143 const char *expr;
6144 const char *cur;
6145 int nb_cons;
Daniel Veillard81a8ec62005-08-22 00:20:58 +00006146 int tabSize;
6147};
6148
6149/**
6150 * xmlExpNewCtxt:
6151 * @maxNodes: the maximum number of nodes
6152 * @dict: optional dictionnary to use internally
6153 *
6154 * Creates a new context for manipulating expressions
6155 *
6156 * Returns the context or NULL in case of error
6157 */
6158xmlExpCtxtPtr
6159xmlExpNewCtxt(int maxNodes, xmlDictPtr dict) {
6160 xmlExpCtxtPtr ret;
6161 int size = 256;
6162
6163 if (maxNodes <= 4096)
6164 maxNodes = 4096;
6165
6166 ret = (xmlExpCtxtPtr) xmlMalloc(sizeof(xmlExpCtxt));
6167 if (ret == NULL)
6168 return(NULL);
6169 memset(ret, 0, sizeof(xmlExpCtxt));
6170 ret->size = size;
6171 ret->nbElems = 0;
6172 ret->table = xmlMalloc(size * sizeof(xmlExpNodePtr));
6173 if (ret->table == NULL) {
6174 xmlFree(ret);
6175 return(NULL);
6176 }
6177 memset(ret->table, 0, size * sizeof(xmlExpNodePtr));
6178 if (dict == NULL) {
6179 ret->dict = xmlDictCreate();
6180 if (ret->dict == NULL) {
6181 xmlFree(ret->table);
6182 xmlFree(ret);
6183 return(NULL);
6184 }
6185 } else {
6186 ret->dict = dict;
6187 xmlDictReference(ret->dict);
6188 }
6189 return(ret);
6190}
6191
6192/**
6193 * xmlExpFreeCtxt:
6194 * @ctxt: an expression context
6195 *
6196 * Free an expression context
6197 */
6198void
6199xmlExpFreeCtxt(xmlExpCtxtPtr ctxt) {
6200 if (ctxt == NULL)
6201 return;
6202 xmlDictFree(ctxt->dict);
6203 if (ctxt->table != NULL)
6204 xmlFree(ctxt->table);
6205 xmlFree(ctxt);
6206}
6207
6208/************************************************************************
6209 * *
6210 * Structure associated to an expression node *
6211 * *
6212 ************************************************************************/
Daniel Veillard465a0002005-08-22 12:07:04 +00006213#define MAX_NODES 10000
6214
6215/* #define DEBUG_DERIV */
6216
6217/*
6218 * TODO:
6219 * - Wildcards
6220 * - public API for creation
6221 *
6222 * Started
6223 * - regression testing
6224 *
6225 * Done
6226 * - split into module and test tool
6227 * - memleaks
6228 */
Daniel Veillard81a8ec62005-08-22 00:20:58 +00006229
6230typedef enum {
6231 XML_EXP_NILABLE = (1 << 0)
6232} xmlExpNodeInfo;
6233
6234#define IS_NILLABLE(node) ((node)->info & XML_EXP_NILABLE)
6235
6236struct _xmlExpNode {
6237 unsigned char type;/* xmlExpNodeType */
6238 unsigned char info;/* OR of xmlExpNodeInfo */
6239 unsigned short key; /* the hash key */
6240 unsigned int ref; /* The number of references */
6241 int c_max; /* the maximum length it can consume */
6242 xmlExpNodePtr exp_left;
6243 xmlExpNodePtr next;/* the next node in the hash table or free list */
6244 union {
6245 struct {
6246 int f_min;
6247 int f_max;
6248 } count;
6249 struct {
6250 xmlExpNodePtr f_right;
6251 } children;
6252 const xmlChar *f_str;
6253 } field;
6254};
6255
6256#define exp_min field.count.f_min
6257#define exp_max field.count.f_max
6258/* #define exp_left field.children.f_left */
6259#define exp_right field.children.f_right
6260#define exp_str field.f_str
6261
6262static xmlExpNodePtr xmlExpNewNode(xmlExpCtxtPtr ctxt, xmlExpNodeType type);
6263static xmlExpNode forbiddenExpNode = {
6264 XML_EXP_FORBID, 0, 0, 0, 0, NULL, NULL, {{ 0, 0}}
6265};
6266xmlExpNodePtr forbiddenExp = &forbiddenExpNode;
6267static xmlExpNode emptyExpNode = {
6268 XML_EXP_EMPTY, 1, 0, 0, 0, NULL, NULL, {{ 0, 0}}
6269};
6270xmlExpNodePtr emptyExp = &emptyExpNode;
6271
6272/************************************************************************
6273 * *
6274 * The custom hash table for unicity and canonicalization *
6275 * of sub-expressions pointers *
6276 * *
6277 ************************************************************************/
6278/*
6279 * xmlExpHashNameComputeKey:
6280 * Calculate the hash key for a token
6281 */
6282static unsigned short
6283xmlExpHashNameComputeKey(const xmlChar *name) {
6284 unsigned short value = 0L;
6285 char ch;
6286
6287 if (name != NULL) {
6288 value += 30 * (*name);
6289 while ((ch = *name++) != 0) {
6290 value = value ^ ((value << 5) + (value >> 3) + (unsigned long)ch);
6291 }
6292 }
6293 return (value);
6294}
6295
6296/*
6297 * xmlExpHashComputeKey:
6298 * Calculate the hash key for a compound expression
6299 */
6300static unsigned short
6301xmlExpHashComputeKey(xmlExpNodeType type, xmlExpNodePtr left,
6302 xmlExpNodePtr right) {
6303 unsigned long value;
6304 unsigned short ret;
6305
6306 switch (type) {
6307 case XML_EXP_SEQ:
6308 value = left->key;
6309 value += right->key;
6310 value *= 3;
6311 ret = (unsigned short) value;
6312 break;
6313 case XML_EXP_OR:
6314 value = left->key;
6315 value += right->key;
6316 value *= 7;
6317 ret = (unsigned short) value;
6318 break;
6319 case XML_EXP_COUNT:
6320 value = left->key;
6321 value += right->key;
6322 ret = (unsigned short) value;
6323 break;
6324 default:
6325 ret = 0;
6326 }
6327 return(ret);
6328}
6329
6330
6331static xmlExpNodePtr
6332xmlExpNewNode(xmlExpCtxtPtr ctxt, xmlExpNodeType type) {
6333 xmlExpNodePtr ret;
6334
6335 if (ctxt->nb_nodes >= MAX_NODES)
6336 return(NULL);
6337 ret = (xmlExpNodePtr) xmlMalloc(sizeof(xmlExpNode));
6338 if (ret == NULL)
6339 return(NULL);
6340 memset(ret, 0, sizeof(xmlExpNode));
6341 ret->type = type;
6342 ret->next = NULL;
6343 ctxt->nb_nodes++;
6344 ctxt->nb_cons++;
6345 return(ret);
6346}
6347
6348/**
6349 * xmlExpHashGetEntry:
6350 * @table: the hash table
6351 *
6352 * Get the unique entry from the hash table. The entry is created if
6353 * needed. @left and @right are consumed, i.e. their ref count will
6354 * be decremented by the operation.
6355 *
6356 * Returns the pointer or NULL in case of error
6357 */
6358static xmlExpNodePtr
6359xmlExpHashGetEntry(xmlExpCtxtPtr ctxt, xmlExpNodeType type,
6360 xmlExpNodePtr left, xmlExpNodePtr right,
6361 const xmlChar *name, int min, int max) {
6362 unsigned short kbase, key;
6363 xmlExpNodePtr entry;
6364 xmlExpNodePtr insert;
6365
6366 if (ctxt == NULL)
6367 return(NULL);
6368
6369 /*
6370 * Check for duplicate and insertion location.
6371 */
6372 if (type == XML_EXP_ATOM) {
6373 kbase = xmlExpHashNameComputeKey(name);
6374 } else if (type == XML_EXP_COUNT) {
6375 /* COUNT reduction rule 1 */
6376 /* a{1} -> a */
6377 if (min == max) {
6378 if (min == 1) {
6379 return(left);
6380 }
6381 if (min == 0) {
6382 xmlExpFree(ctxt, left);
6383 return(emptyExp);
6384 }
6385 }
6386 if (min < 0) {
6387 xmlExpFree(ctxt, left);
6388 return(forbiddenExp);
6389 }
6390 if (max == -1)
6391 kbase = min + 79;
6392 else
6393 kbase = max - min;
6394 kbase += left->key;
6395 } else if (type == XML_EXP_OR) {
6396 /* Forbid reduction rules */
6397 if (left->type == XML_EXP_FORBID) {
6398 xmlExpFree(ctxt, left);
6399 return(right);
6400 }
6401 if (right->type == XML_EXP_FORBID) {
6402 xmlExpFree(ctxt, right);
6403 return(left);
6404 }
6405
6406 /* OR reduction rule 1 */
6407 /* a | a reduced to a */
6408 if (left == right) {
6409 left->ref--;
6410 return(left);
6411 }
6412 /* OR canonicalization rule 1 */
6413 /* linearize (a | b) | c into a | (b | c) */
6414 if ((left->type == XML_EXP_OR) && (right->type != XML_EXP_OR)) {
6415 xmlExpNodePtr tmp = left;
6416 left = right;
6417 right = tmp;
6418 }
6419 /* OR reduction rule 2 */
6420 /* a | (a | b) and b | (a | b) are reduced to a | b */
6421 if (right->type == XML_EXP_OR) {
6422 if ((left == right->exp_left) ||
6423 (left == right->exp_right)) {
6424 xmlExpFree(ctxt, left);
6425 return(right);
6426 }
6427 }
6428 /* OR canonicalization rule 2 */
6429 /* linearize (a | b) | c into a | (b | c) */
6430 if (left->type == XML_EXP_OR) {
6431 xmlExpNodePtr tmp;
6432
6433 /* OR canonicalization rule 2 */
6434 if ((left->exp_right->type != XML_EXP_OR) &&
6435 (left->exp_right->key < left->exp_left->key)) {
6436 tmp = left->exp_right;
6437 left->exp_right = left->exp_left;
6438 left->exp_left = tmp;
6439 }
6440 left->exp_right->ref++;
6441 tmp = xmlExpHashGetEntry(ctxt, XML_EXP_OR, left->exp_right, right,
6442 NULL, 0, 0);
6443 left->exp_left->ref++;
6444 tmp = xmlExpHashGetEntry(ctxt, XML_EXP_OR, left->exp_left, tmp,
6445 NULL, 0, 0);
6446
6447 xmlExpFree(ctxt, left);
6448 return(tmp);
6449 }
6450 if (right->type == XML_EXP_OR) {
6451 /* Ordering in the tree */
6452 /* C | (A | B) -> A | (B | C) */
6453 if (left->key > right->exp_right->key) {
6454 xmlExpNodePtr tmp;
6455 right->exp_right->ref++;
6456 tmp = xmlExpHashGetEntry(ctxt, XML_EXP_OR, right->exp_right,
6457 left, NULL, 0, 0);
6458 right->exp_left->ref++;
6459 tmp = xmlExpHashGetEntry(ctxt, XML_EXP_OR, right->exp_left,
6460 tmp, NULL, 0, 0);
6461 xmlExpFree(ctxt, right);
6462 return(tmp);
6463 }
6464 /* Ordering in the tree */
6465 /* B | (A | C) -> A | (B | C) */
6466 if (left->key > right->exp_left->key) {
6467 xmlExpNodePtr tmp;
6468 right->exp_right->ref++;
6469 tmp = xmlExpHashGetEntry(ctxt, XML_EXP_OR, left,
6470 right->exp_right, NULL, 0, 0);
6471 right->exp_left->ref++;
6472 tmp = xmlExpHashGetEntry(ctxt, XML_EXP_OR, right->exp_left,
6473 tmp, NULL, 0, 0);
6474 xmlExpFree(ctxt, right);
6475 return(tmp);
6476 }
6477 }
6478 /* we know both types are != XML_EXP_OR here */
6479 else if (left->key > right->key) {
6480 xmlExpNodePtr tmp = left;
6481 left = right;
6482 right = tmp;
6483 }
6484 kbase = xmlExpHashComputeKey(type, left, right);
6485 } else if (type == XML_EXP_SEQ) {
6486 /* Forbid reduction rules */
6487 if (left->type == XML_EXP_FORBID) {
6488 xmlExpFree(ctxt, right);
6489 return(left);
6490 }
6491 if (right->type == XML_EXP_FORBID) {
6492 xmlExpFree(ctxt, left);
6493 return(right);
6494 }
6495 /* Empty reduction rules */
6496 if (right->type == XML_EXP_EMPTY) {
6497 return(left);
6498 }
6499 if (left->type == XML_EXP_EMPTY) {
6500 return(right);
6501 }
6502 kbase = xmlExpHashComputeKey(type, left, right);
6503 } else
6504 return(NULL);
6505
6506 key = kbase % ctxt->size;
6507 if (ctxt->table[key] != NULL) {
6508 for (insert = ctxt->table[key]; insert != NULL;
6509 insert = insert->next) {
6510 if ((insert->key == kbase) &&
6511 (insert->type == type)) {
6512 if (type == XML_EXP_ATOM) {
6513 if (name == insert->exp_str) {
6514 insert->ref++;
6515 return(insert);
6516 }
6517 } else if (type == XML_EXP_COUNT) {
6518 if ((insert->exp_min == min) && (insert->exp_max == max) &&
6519 (insert->exp_left == left)) {
6520 insert->ref++;
6521 left->ref--;
6522 return(insert);
6523 }
6524 } else if ((insert->exp_left == left) &&
6525 (insert->exp_right == right)) {
6526 insert->ref++;
6527 left->ref--;
6528 right->ref--;
6529 return(insert);
6530 }
6531 }
6532 }
6533 }
6534
6535 entry = xmlExpNewNode(ctxt, type);
6536 if (entry == NULL)
6537 return(NULL);
6538 entry->key = kbase;
6539 if (type == XML_EXP_ATOM) {
6540 entry->exp_str = name;
6541 entry->c_max = 1;
6542 } else if (type == XML_EXP_COUNT) {
6543 entry->exp_min = min;
6544 entry->exp_max = max;
6545 entry->exp_left = left;
6546 if ((min == 0) || (IS_NILLABLE(left)))
6547 entry->info |= XML_EXP_NILABLE;
6548 if (max < 0)
6549 entry->c_max = -1;
6550 else
6551 entry->c_max = max * entry->exp_left->c_max;
6552 } else {
6553 entry->exp_left = left;
6554 entry->exp_right = right;
6555 if (type == XML_EXP_OR) {
6556 if ((IS_NILLABLE(left)) || (IS_NILLABLE(right)))
6557 entry->info |= XML_EXP_NILABLE;
6558 if ((entry->exp_left->c_max == -1) ||
6559 (entry->exp_right->c_max == -1))
6560 entry->c_max = -1;
6561 else if (entry->exp_left->c_max > entry->exp_right->c_max)
6562 entry->c_max = entry->exp_left->c_max;
6563 else
6564 entry->c_max = entry->exp_right->c_max;
6565 } else {
6566 if ((IS_NILLABLE(left)) && (IS_NILLABLE(right)))
6567 entry->info |= XML_EXP_NILABLE;
6568 if ((entry->exp_left->c_max == -1) ||
6569 (entry->exp_right->c_max == -1))
6570 entry->c_max = -1;
6571 else
6572 entry->c_max = entry->exp_left->c_max + entry->exp_right->c_max;
6573 }
6574 }
6575 entry->ref = 1;
6576 if (ctxt->table[key] != NULL)
6577 entry->next = ctxt->table[key];
6578
6579 ctxt->table[key] = entry;
6580 ctxt->nbElems++;
6581
6582 return(entry);
6583}
6584
6585/**
6586 * xmlExpFree:
6587 * @ctxt: the expression context
6588 * @exp: the expression
6589 *
6590 * Dereference the expression
6591 */
6592void
6593xmlExpFree(xmlExpCtxtPtr ctxt, xmlExpNodePtr exp) {
6594 if ((exp == NULL) || (exp == forbiddenExp) || (exp == emptyExp))
6595 return;
6596 exp->ref--;
Daniel Veillard81a8ec62005-08-22 00:20:58 +00006597 if (exp->ref == 0) {
6598 unsigned short key;
6599
6600 /* Unlink it first from the hash table */
6601 key = exp->key % ctxt->size;
6602 if (ctxt->table[key] == exp) {
6603 ctxt->table[key] = exp->next;
6604 } else {
6605 xmlExpNodePtr tmp;
6606
6607 tmp = ctxt->table[key];
6608 while (tmp != NULL) {
6609 if (tmp->next == exp) {
6610 tmp->next = exp->next;
6611 break;
6612 }
6613 tmp = tmp->next;
6614 }
6615 }
6616
6617 if ((exp->type == XML_EXP_SEQ) || (exp->type == XML_EXP_OR)) {
6618 xmlExpFree(ctxt, exp->exp_left);
6619 xmlExpFree(ctxt, exp->exp_right);
6620 } else if (exp->type == XML_EXP_COUNT) {
6621 xmlExpFree(ctxt, exp->exp_left);
6622 }
6623 xmlFree(exp);
Daniel Veillard81a8ec62005-08-22 00:20:58 +00006624 ctxt->nb_nodes--;
6625 }
6626}
6627
6628/**
6629 * xmlExpRef:
6630 * @exp: the expression
6631 *
6632 * Increase the reference count of the expression
6633 */
6634void
6635xmlExpRef(xmlExpNodePtr exp) {
6636 if (exp != NULL)
6637 exp->ref++;
6638}
6639
Daniel Veillardccb4d412005-08-23 13:41:17 +00006640/**
6641 * xmlExpNewAtom:
6642 * @ctxt: the expression context
6643 * @name: the atom name
6644 * @len: the atom name lenght in byte (or -1);
6645 *
6646 * Get the atom associated to this name from that context
6647 *
6648 * Returns the node or NULL in case of error
6649 */
6650xmlExpNodePtr
6651xmlExpNewAtom(xmlExpCtxtPtr ctxt, const xmlChar *name, int len) {
6652 if ((ctxt == NULL) || (name == NULL))
6653 return(NULL);
6654 name = xmlDictLookup(ctxt->dict, name, len);
6655 if (name == NULL)
6656 return(NULL);
6657 return(xmlExpHashGetEntry(ctxt, XML_EXP_ATOM, NULL, NULL, name, 0, 0));
6658}
6659
6660/**
6661 * xmlExpNewOr:
6662 * @ctxt: the expression context
6663 * @left: left expression
6664 * @right: right expression
6665 *
6666 * Get the atom associated to the choice @left | @right
6667 * Note that @left and @right are consumed in the operation, to keep
6668 * an handle on them use xmlExpRef() and use xmlExpFree() to release them,
6669 * this is true even in case of failure (unless ctxt == NULL).
6670 *
6671 * Returns the node or NULL in case of error
6672 */
6673xmlExpNodePtr
6674xmlExpNewOr(xmlExpCtxtPtr ctxt, xmlExpNodePtr left, xmlExpNodePtr right) {
Daniel Veillard11ce4002006-03-10 00:36:23 +00006675 if (ctxt == NULL)
6676 return(NULL);
6677 if ((left == NULL) || (right == NULL)) {
Daniel Veillardccb4d412005-08-23 13:41:17 +00006678 xmlExpFree(ctxt, left);
6679 xmlExpFree(ctxt, right);
6680 return(NULL);
6681 }
6682 return(xmlExpHashGetEntry(ctxt, XML_EXP_OR, left, right, NULL, 0, 0));
6683}
6684
6685/**
6686 * xmlExpNewSeq:
6687 * @ctxt: the expression context
6688 * @left: left expression
6689 * @right: right expression
6690 *
6691 * Get the atom associated to the sequence @left , @right
6692 * Note that @left and @right are consumed in the operation, to keep
6693 * an handle on them use xmlExpRef() and use xmlExpFree() to release them,
6694 * this is true even in case of failure (unless ctxt == NULL).
6695 *
6696 * Returns the node or NULL in case of error
6697 */
6698xmlExpNodePtr
6699xmlExpNewSeq(xmlExpCtxtPtr ctxt, xmlExpNodePtr left, xmlExpNodePtr right) {
Daniel Veillard11ce4002006-03-10 00:36:23 +00006700 if (ctxt == NULL)
6701 return(NULL);
6702 if ((left == NULL) || (right == NULL)) {
Daniel Veillardccb4d412005-08-23 13:41:17 +00006703 xmlExpFree(ctxt, left);
6704 xmlExpFree(ctxt, right);
6705 return(NULL);
6706 }
6707 return(xmlExpHashGetEntry(ctxt, XML_EXP_SEQ, left, right, NULL, 0, 0));
6708}
6709
6710/**
6711 * xmlExpNewRange:
6712 * @ctxt: the expression context
6713 * @subset: the expression to be repeated
6714 * @min: the lower bound for the repetition
6715 * @max: the upper bound for the repetition, -1 means infinite
6716 *
6717 * Get the atom associated to the range (@subset){@min, @max}
6718 * Note that @subset is consumed in the operation, to keep
6719 * an handle on it use xmlExpRef() and use xmlExpFree() to release it,
6720 * this is true even in case of failure (unless ctxt == NULL).
6721 *
6722 * Returns the node or NULL in case of error
6723 */
6724xmlExpNodePtr
6725xmlExpNewRange(xmlExpCtxtPtr ctxt, xmlExpNodePtr subset, int min, int max) {
Daniel Veillard11ce4002006-03-10 00:36:23 +00006726 if (ctxt == NULL)
6727 return(NULL);
6728 if ((subset == NULL) || (min < 0) || (max < -1) ||
Daniel Veillardccb4d412005-08-23 13:41:17 +00006729 ((max >= 0) && (min > max))) {
6730 xmlExpFree(ctxt, subset);
6731 return(NULL);
6732 }
6733 return(xmlExpHashGetEntry(ctxt, XML_EXP_COUNT, subset,
6734 NULL, NULL, min, max));
6735}
6736
Daniel Veillard81a8ec62005-08-22 00:20:58 +00006737/************************************************************************
6738 * *
6739 * Public API for operations on expressions *
6740 * *
6741 ************************************************************************/
6742
6743static int
6744xmlExpGetLanguageInt(xmlExpCtxtPtr ctxt, xmlExpNodePtr exp,
6745 const xmlChar**list, int len, int nb) {
6746 int tmp, tmp2;
6747tail:
6748 switch (exp->type) {
6749 case XML_EXP_EMPTY:
6750 return(0);
6751 case XML_EXP_ATOM:
6752 for (tmp = 0;tmp < nb;tmp++)
6753 if (list[tmp] == exp->exp_str)
6754 return(0);
6755 if (nb >= len)
6756 return(-2);
6757 list[nb++] = exp->exp_str;
6758 return(1);
6759 case XML_EXP_COUNT:
6760 exp = exp->exp_left;
6761 goto tail;
6762 case XML_EXP_SEQ:
6763 case XML_EXP_OR:
6764 tmp = xmlExpGetLanguageInt(ctxt, exp->exp_left, list, len, nb);
6765 if (tmp < 0)
6766 return(tmp);
6767 tmp2 = xmlExpGetLanguageInt(ctxt, exp->exp_right, list, len,
6768 nb + tmp);
6769 if (tmp2 < 0)
6770 return(tmp2);
6771 return(tmp + tmp2);
6772 }
6773 return(-1);
6774}
6775
6776/**
6777 * xmlExpGetLanguage:
6778 * @ctxt: the expression context
6779 * @exp: the expression
Daniel Veillard7802ba52005-10-27 11:56:20 +00006780 * @langList: where to store the tokens
Daniel Veillard81a8ec62005-08-22 00:20:58 +00006781 * @len: the allocated lenght of @list
6782 *
6783 * Find all the strings used in @exp and store them in @list
6784 *
6785 * Returns the number of unique strings found, -1 in case of errors and
6786 * -2 if there is more than @len strings
6787 */
6788int
6789xmlExpGetLanguage(xmlExpCtxtPtr ctxt, xmlExpNodePtr exp,
Daniel Veillard7802ba52005-10-27 11:56:20 +00006790 const xmlChar**langList, int len) {
6791 if ((ctxt == NULL) || (exp == NULL) || (langList == NULL) || (len <= 0))
Daniel Veillard81a8ec62005-08-22 00:20:58 +00006792 return(-1);
Daniel Veillard7802ba52005-10-27 11:56:20 +00006793 return(xmlExpGetLanguageInt(ctxt, exp, langList, len, 0));
Daniel Veillard81a8ec62005-08-22 00:20:58 +00006794}
6795
6796static int
6797xmlExpGetStartInt(xmlExpCtxtPtr ctxt, xmlExpNodePtr exp,
6798 const xmlChar**list, int len, int nb) {
6799 int tmp, tmp2;
6800tail:
6801 switch (exp->type) {
6802 case XML_EXP_FORBID:
6803 return(0);
6804 case XML_EXP_EMPTY:
6805 return(0);
6806 case XML_EXP_ATOM:
6807 for (tmp = 0;tmp < nb;tmp++)
6808 if (list[tmp] == exp->exp_str)
6809 return(0);
6810 if (nb >= len)
6811 return(-2);
6812 list[nb++] = exp->exp_str;
6813 return(1);
6814 case XML_EXP_COUNT:
6815 exp = exp->exp_left;
6816 goto tail;
6817 case XML_EXP_SEQ:
6818 tmp = xmlExpGetStartInt(ctxt, exp->exp_left, list, len, nb);
6819 if (tmp < 0)
6820 return(tmp);
6821 if (IS_NILLABLE(exp->exp_left)) {
6822 tmp2 = xmlExpGetStartInt(ctxt, exp->exp_right, list, len,
6823 nb + tmp);
6824 if (tmp2 < 0)
6825 return(tmp2);
6826 tmp += tmp2;
6827 }
6828 return(tmp);
6829 case XML_EXP_OR:
6830 tmp = xmlExpGetStartInt(ctxt, exp->exp_left, list, len, nb);
6831 if (tmp < 0)
6832 return(tmp);
6833 tmp2 = xmlExpGetStartInt(ctxt, exp->exp_right, list, len,
6834 nb + tmp);
6835 if (tmp2 < 0)
6836 return(tmp2);
6837 return(tmp + tmp2);
6838 }
6839 return(-1);
6840}
6841
6842/**
6843 * xmlExpGetStart:
6844 * @ctxt: the expression context
6845 * @exp: the expression
Daniel Veillard7802ba52005-10-27 11:56:20 +00006846 * @tokList: where to store the tokens
Daniel Veillard81a8ec62005-08-22 00:20:58 +00006847 * @len: the allocated lenght of @list
6848 *
6849 * Find all the strings that appears at the start of the languages
6850 * accepted by @exp and store them in @list. E.g. for (a, b) | c
6851 * it will return the list [a, c]
6852 *
6853 * Returns the number of unique strings found, -1 in case of errors and
6854 * -2 if there is more than @len strings
6855 */
6856int
6857xmlExpGetStart(xmlExpCtxtPtr ctxt, xmlExpNodePtr exp,
Daniel Veillard7802ba52005-10-27 11:56:20 +00006858 const xmlChar**tokList, int len) {
6859 if ((ctxt == NULL) || (exp == NULL) || (tokList == NULL) || (len <= 0))
Daniel Veillard81a8ec62005-08-22 00:20:58 +00006860 return(-1);
Daniel Veillard7802ba52005-10-27 11:56:20 +00006861 return(xmlExpGetStartInt(ctxt, exp, tokList, len, 0));
Daniel Veillard81a8ec62005-08-22 00:20:58 +00006862}
6863
6864/**
6865 * xmlExpIsNillable:
6866 * @exp: the expression
6867 *
6868 * Finds if the expression is nillable, i.e. if it accepts the empty sequqnce
6869 *
6870 * Returns 1 if nillable, 0 if not and -1 in case of error
6871 */
6872int
6873xmlExpIsNillable(xmlExpNodePtr exp) {
6874 if (exp == NULL)
6875 return(-1);
6876 return(IS_NILLABLE(exp) != 0);
6877}
6878
6879static xmlExpNodePtr
6880xmlExpStringDeriveInt(xmlExpCtxtPtr ctxt, xmlExpNodePtr exp, const xmlChar *str)
6881{
6882 xmlExpNodePtr ret;
6883
6884 switch (exp->type) {
6885 case XML_EXP_EMPTY:
6886 return(forbiddenExp);
6887 case XML_EXP_FORBID:
6888 return(forbiddenExp);
6889 case XML_EXP_ATOM:
6890 if (exp->exp_str == str) {
6891#ifdef DEBUG_DERIV
6892 printf("deriv atom: equal => Empty\n");
6893#endif
6894 ret = emptyExp;
6895 } else {
6896#ifdef DEBUG_DERIV
6897 printf("deriv atom: mismatch => forbid\n");
6898#endif
6899 /* TODO wildcards here */
6900 ret = forbiddenExp;
6901 }
6902 return(ret);
6903 case XML_EXP_OR: {
6904 xmlExpNodePtr tmp;
6905
6906#ifdef DEBUG_DERIV
6907 printf("deriv or: => or(derivs)\n");
6908#endif
6909 tmp = xmlExpStringDeriveInt(ctxt, exp->exp_left, str);
6910 if (tmp == NULL) {
6911 return(NULL);
6912 }
6913 ret = xmlExpStringDeriveInt(ctxt, exp->exp_right, str);
6914 if (ret == NULL) {
6915 xmlExpFree(ctxt, tmp);
6916 return(NULL);
6917 }
6918 ret = xmlExpHashGetEntry(ctxt, XML_EXP_OR, tmp, ret,
6919 NULL, 0, 0);
6920 return(ret);
6921 }
6922 case XML_EXP_SEQ:
6923#ifdef DEBUG_DERIV
6924 printf("deriv seq: starting with left\n");
6925#endif
6926 ret = xmlExpStringDeriveInt(ctxt, exp->exp_left, str);
6927 if (ret == NULL) {
6928 return(NULL);
6929 } else if (ret == forbiddenExp) {
6930 if (IS_NILLABLE(exp->exp_left)) {
6931#ifdef DEBUG_DERIV
6932 printf("deriv seq: left failed but nillable\n");
6933#endif
6934 ret = xmlExpStringDeriveInt(ctxt, exp->exp_right, str);
6935 }
6936 } else {
6937#ifdef DEBUG_DERIV
6938 printf("deriv seq: left match => sequence\n");
6939#endif
6940 exp->exp_right->ref++;
6941 ret = xmlExpHashGetEntry(ctxt, XML_EXP_SEQ, ret, exp->exp_right,
6942 NULL, 0, 0);
6943 }
6944 return(ret);
6945 case XML_EXP_COUNT: {
6946 int min, max;
6947 xmlExpNodePtr tmp;
6948
6949 if (exp->exp_max == 0)
6950 return(forbiddenExp);
6951 ret = xmlExpStringDeriveInt(ctxt, exp->exp_left, str);
6952 if (ret == NULL)
6953 return(NULL);
6954 if (ret == forbiddenExp) {
6955#ifdef DEBUG_DERIV
6956 printf("deriv count: pattern mismatch => forbid\n");
6957#endif
6958 return(ret);
6959 }
6960 if (exp->exp_max == 1)
6961 return(ret);
6962 if (exp->exp_max < 0) /* unbounded */
6963 max = -1;
6964 else
6965 max = exp->exp_max - 1;
6966 if (exp->exp_min > 0)
6967 min = exp->exp_min - 1;
6968 else
6969 min = 0;
6970 exp->exp_left->ref++;
6971 tmp = xmlExpHashGetEntry(ctxt, XML_EXP_COUNT, exp->exp_left, NULL,
6972 NULL, min, max);
6973 if (ret == emptyExp) {
6974#ifdef DEBUG_DERIV
6975 printf("deriv count: match to empty => new count\n");
6976#endif
6977 return(tmp);
6978 }
6979#ifdef DEBUG_DERIV
6980 printf("deriv count: match => sequence with new count\n");
6981#endif
6982 return(xmlExpHashGetEntry(ctxt, XML_EXP_SEQ, ret, tmp,
6983 NULL, 0, 0));
6984 }
6985 }
6986 return(NULL);
6987}
6988
6989/**
6990 * xmlExpStringDerive:
6991 * @ctxt: the expression context
6992 * @exp: the expression
6993 * @str: the string
6994 * @len: the string len in bytes if available
6995 *
6996 * Do one step of Brzozowski derivation of the expression @exp with
6997 * respect to the input string
6998 *
6999 * Returns the resulting expression or NULL in case of internal error
7000 */
7001xmlExpNodePtr
7002xmlExpStringDerive(xmlExpCtxtPtr ctxt, xmlExpNodePtr exp,
7003 const xmlChar *str, int len) {
7004 const xmlChar *input;
7005
7006 if ((exp == NULL) || (ctxt == NULL) || (str == NULL)) {
7007 return(NULL);
7008 }
7009 /*
7010 * check the string is in the dictionnary, if yes use an interned
7011 * copy, otherwise we know it's not an acceptable input
7012 */
7013 input = xmlDictExists(ctxt->dict, str, len);
7014 if (input == NULL) {
7015 return(forbiddenExp);
7016 }
7017 return(xmlExpStringDeriveInt(ctxt, exp, input));
7018}
7019
7020static int
7021xmlExpCheckCard(xmlExpNodePtr exp, xmlExpNodePtr sub) {
7022 int ret = 1;
7023
7024 if (sub->c_max == -1) {
7025 if (exp->c_max != -1)
7026 ret = 0;
7027 } else if ((exp->c_max >= 0) && (exp->c_max < sub->c_max)) {
7028 ret = 0;
7029 }
7030#if 0
7031 if ((IS_NILLABLE(sub)) && (!IS_NILLABLE(exp)))
7032 ret = 0;
7033#endif
7034 return(ret);
7035}
7036
7037static xmlExpNodePtr xmlExpExpDeriveInt(xmlExpCtxtPtr ctxt, xmlExpNodePtr exp,
7038 xmlExpNodePtr sub);
7039/**
7040 * xmlExpDivide:
7041 * @ctxt: the expressions context
7042 * @exp: the englobing expression
7043 * @sub: the subexpression
7044 * @mult: the multiple expression
7045 * @remain: the remain from the derivation of the multiple
7046 *
7047 * Check if exp is a multiple of sub, i.e. if there is a finite number n
7048 * so that sub{n} subsume exp
7049 *
7050 * Returns the multiple value if successful, 0 if it is not a multiple
7051 * and -1 in case of internel error.
7052 */
7053
7054static int
7055xmlExpDivide(xmlExpCtxtPtr ctxt, xmlExpNodePtr exp, xmlExpNodePtr sub,
7056 xmlExpNodePtr *mult, xmlExpNodePtr *remain) {
7057 int i;
7058 xmlExpNodePtr tmp, tmp2;
7059
7060 if (mult != NULL) *mult = NULL;
7061 if (remain != NULL) *remain = NULL;
7062 if (exp->c_max == -1) return(0);
7063 if (IS_NILLABLE(exp) && (!IS_NILLABLE(sub))) return(0);
7064
7065 for (i = 1;i <= exp->c_max;i++) {
7066 sub->ref++;
7067 tmp = xmlExpHashGetEntry(ctxt, XML_EXP_COUNT,
7068 sub, NULL, NULL, i, i);
7069 if (tmp == NULL) {
7070 return(-1);
7071 }
7072 if (!xmlExpCheckCard(tmp, exp)) {
7073 xmlExpFree(ctxt, tmp);
7074 continue;
7075 }
7076 tmp2 = xmlExpExpDeriveInt(ctxt, tmp, exp);
7077 if (tmp2 == NULL) {
7078 xmlExpFree(ctxt, tmp);
7079 return(-1);
7080 }
7081 if ((tmp2 != forbiddenExp) && (IS_NILLABLE(tmp2))) {
7082 if (remain != NULL)
7083 *remain = tmp2;
7084 else
7085 xmlExpFree(ctxt, tmp2);
7086 if (mult != NULL)
7087 *mult = tmp;
7088 else
7089 xmlExpFree(ctxt, tmp);
7090#ifdef DEBUG_DERIV
7091 printf("Divide succeeded %d\n", i);
7092#endif
7093 return(i);
7094 }
7095 xmlExpFree(ctxt, tmp);
7096 xmlExpFree(ctxt, tmp2);
7097 }
7098#ifdef DEBUG_DERIV
7099 printf("Divide failed\n");
7100#endif
7101 return(0);
7102}
7103
7104/**
7105 * xmlExpExpDeriveInt:
7106 * @ctxt: the expressions context
7107 * @exp: the englobing expression
7108 * @sub: the subexpression
7109 *
7110 * Try to do a step of Brzozowski derivation but at a higher level
7111 * the input being a subexpression.
7112 *
7113 * Returns the resulting expression or NULL in case of internal error
7114 */
7115static xmlExpNodePtr
7116xmlExpExpDeriveInt(xmlExpCtxtPtr ctxt, xmlExpNodePtr exp, xmlExpNodePtr sub) {
7117 xmlExpNodePtr ret, tmp, tmp2, tmp3;
7118 const xmlChar **tab;
7119 int len, i;
7120
7121 /*
7122 * In case of equality and if the expression can only consume a finite
7123 * amount, then the derivation is empty
7124 */
7125 if ((exp == sub) && (exp->c_max >= 0)) {
7126#ifdef DEBUG_DERIV
7127 printf("Equal(exp, sub) and finite -> Empty\n");
7128#endif
7129 return(emptyExp);
7130 }
7131 /*
7132 * decompose sub sequence first
7133 */
7134 if (sub->type == XML_EXP_EMPTY) {
7135#ifdef DEBUG_DERIV
7136 printf("Empty(sub) -> Empty\n");
7137#endif
7138 exp->ref++;
7139 return(exp);
7140 }
7141 if (sub->type == XML_EXP_SEQ) {
7142#ifdef DEBUG_DERIV
7143 printf("Seq(sub) -> decompose\n");
7144#endif
7145 tmp = xmlExpExpDeriveInt(ctxt, exp, sub->exp_left);
7146 if (tmp == NULL)
7147 return(NULL);
7148 if (tmp == forbiddenExp)
7149 return(tmp);
7150 ret = xmlExpExpDeriveInt(ctxt, tmp, sub->exp_right);
7151 xmlExpFree(ctxt, tmp);
7152 return(ret);
7153 }
7154 if (sub->type == XML_EXP_OR) {
7155#ifdef DEBUG_DERIV
7156 printf("Or(sub) -> decompose\n");
7157#endif
7158 tmp = xmlExpExpDeriveInt(ctxt, exp, sub->exp_left);
7159 if (tmp == forbiddenExp)
7160 return(tmp);
7161 if (tmp == NULL)
7162 return(NULL);
7163 ret = xmlExpExpDeriveInt(ctxt, exp, sub->exp_right);
7164 if ((ret == NULL) || (ret == forbiddenExp)) {
7165 xmlExpFree(ctxt, tmp);
7166 return(ret);
7167 }
7168 return(xmlExpHashGetEntry(ctxt, XML_EXP_OR, tmp, ret, NULL, 0, 0));
7169 }
7170 if (!xmlExpCheckCard(exp, sub)) {
7171#ifdef DEBUG_DERIV
7172 printf("CheckCard(exp, sub) failed -> Forbid\n");
7173#endif
7174 return(forbiddenExp);
7175 }
7176 switch (exp->type) {
7177 case XML_EXP_EMPTY:
7178 if (sub == emptyExp)
7179 return(emptyExp);
7180#ifdef DEBUG_DERIV
7181 printf("Empty(exp) -> Forbid\n");
7182#endif
7183 return(forbiddenExp);
7184 case XML_EXP_FORBID:
7185#ifdef DEBUG_DERIV
7186 printf("Forbid(exp) -> Forbid\n");
7187#endif
7188 return(forbiddenExp);
7189 case XML_EXP_ATOM:
7190 if (sub->type == XML_EXP_ATOM) {
7191 /* TODO: handle wildcards */
7192 if (exp->exp_str == sub->exp_str) {
7193#ifdef DEBUG_DERIV
7194 printf("Atom match -> Empty\n");
7195#endif
7196 return(emptyExp);
7197 }
7198#ifdef DEBUG_DERIV
7199 printf("Atom mismatch -> Forbid\n");
7200#endif
7201 return(forbiddenExp);
7202 }
7203 if ((sub->type == XML_EXP_COUNT) &&
7204 (sub->exp_max == 1) &&
7205 (sub->exp_left->type == XML_EXP_ATOM)) {
7206 /* TODO: handle wildcards */
7207 if (exp->exp_str == sub->exp_left->exp_str) {
7208#ifdef DEBUG_DERIV
7209 printf("Atom match -> Empty\n");
7210#endif
7211 return(emptyExp);
7212 }
7213#ifdef DEBUG_DERIV
7214 printf("Atom mismatch -> Forbid\n");
7215#endif
7216 return(forbiddenExp);
7217 }
7218#ifdef DEBUG_DERIV
7219 printf("Compex exp vs Atom -> Forbid\n");
7220#endif
7221 return(forbiddenExp);
7222 case XML_EXP_SEQ:
7223 /* try to get the sequence consumed only if possible */
7224 if (xmlExpCheckCard(exp->exp_left, sub)) {
7225 /* See if the sequence can be consumed directly */
7226#ifdef DEBUG_DERIV
7227 printf("Seq trying left only\n");
7228#endif
7229 ret = xmlExpExpDeriveInt(ctxt, exp->exp_left, sub);
7230 if ((ret != forbiddenExp) && (ret != NULL)) {
7231#ifdef DEBUG_DERIV
7232 printf("Seq trying left only worked\n");
7233#endif
7234 /*
7235 * TODO: assumption here that we are determinist
7236 * i.e. we won't get to a nillable exp left
7237 * subset which could be matched by the right
7238 * part too.
7239 * e.g.: (a | b)+,(a | c) and 'a+,a'
7240 */
7241 exp->exp_right->ref++;
7242 return(xmlExpHashGetEntry(ctxt, XML_EXP_SEQ, ret,
7243 exp->exp_right, NULL, 0, 0));
7244 }
7245#ifdef DEBUG_DERIV
7246 } else {
7247 printf("Seq: left too short\n");
7248#endif
7249 }
7250 /* Try instead to decompose */
7251 if (sub->type == XML_EXP_COUNT) {
7252 int min, max;
7253
7254#ifdef DEBUG_DERIV
7255 printf("Seq: sub is a count\n");
7256#endif
7257 ret = xmlExpExpDeriveInt(ctxt, exp->exp_left, sub->exp_left);
7258 if (ret == NULL)
7259 return(NULL);
7260 if (ret != forbiddenExp) {
7261#ifdef DEBUG_DERIV
7262 printf("Seq , Count match on left\n");
7263#endif
7264 if (sub->exp_max < 0)
7265 max = -1;
7266 else
7267 max = sub->exp_max -1;
7268 if (sub->exp_min > 0)
7269 min = sub->exp_min -1;
7270 else
7271 min = 0;
7272 exp->exp_right->ref++;
7273 tmp = xmlExpHashGetEntry(ctxt, XML_EXP_SEQ, ret,
7274 exp->exp_right, NULL, 0, 0);
7275 if (tmp == NULL)
7276 return(NULL);
7277
7278 sub->exp_left->ref++;
7279 tmp2 = xmlExpHashGetEntry(ctxt, XML_EXP_COUNT,
7280 sub->exp_left, NULL, NULL, min, max);
7281 if (tmp2 == NULL) {
7282 xmlExpFree(ctxt, tmp);
7283 return(NULL);
7284 }
7285 ret = xmlExpExpDeriveInt(ctxt, tmp, tmp2);
7286 xmlExpFree(ctxt, tmp);
7287 xmlExpFree(ctxt, tmp2);
7288 return(ret);
7289 }
7290 }
7291 /* we made no progress on structured operations */
7292 break;
7293 case XML_EXP_OR:
7294#ifdef DEBUG_DERIV
7295 printf("Or , trying both side\n");
7296#endif
7297 ret = xmlExpExpDeriveInt(ctxt, exp->exp_left, sub);
7298 if (ret == NULL)
7299 return(NULL);
7300 tmp = xmlExpExpDeriveInt(ctxt, exp->exp_right, sub);
7301 if (tmp == NULL) {
7302 xmlExpFree(ctxt, ret);
7303 return(NULL);
7304 }
7305 return(xmlExpHashGetEntry(ctxt, XML_EXP_OR, ret, tmp, NULL, 0, 0));
7306 case XML_EXP_COUNT: {
7307 int min, max;
7308
7309 if (sub->type == XML_EXP_COUNT) {
7310 /*
7311 * Try to see if the loop is completely subsumed
7312 */
7313 tmp = xmlExpExpDeriveInt(ctxt, exp->exp_left, sub->exp_left);
7314 if (tmp == NULL)
7315 return(NULL);
7316 if (tmp == forbiddenExp) {
7317 int mult;
7318
7319#ifdef DEBUG_DERIV
7320 printf("Count, Count inner don't subsume\n");
7321#endif
7322 mult = xmlExpDivide(ctxt, sub->exp_left, exp->exp_left,
7323 NULL, &tmp);
7324 if (mult <= 0) {
7325#ifdef DEBUG_DERIV
7326 printf("Count, Count not multiple => forbidden\n");
7327#endif
7328 return(forbiddenExp);
7329 }
7330 if (sub->exp_max == -1) {
7331 max = -1;
7332 if (exp->exp_max == -1) {
7333 if (exp->exp_min <= sub->exp_min * mult)
7334 min = 0;
7335 else
7336 min = exp->exp_min - sub->exp_min * mult;
7337 } else {
7338#ifdef DEBUG_DERIV
7339 printf("Count, Count finite can't subsume infinite\n");
7340#endif
7341 xmlExpFree(ctxt, tmp);
7342 return(forbiddenExp);
7343 }
7344 } else {
7345 if (exp->exp_max == -1) {
7346#ifdef DEBUG_DERIV
7347 printf("Infinite loop consume mult finite loop\n");
7348#endif
7349 if (exp->exp_min > sub->exp_min * mult) {
7350 max = -1;
7351 min = exp->exp_min - sub->exp_min * mult;
7352 } else {
7353 max = -1;
7354 min = 0;
7355 }
7356 } else {
7357 if (exp->exp_max < sub->exp_max * mult) {
7358#ifdef DEBUG_DERIV
7359 printf("loops max mult mismatch => forbidden\n");
7360#endif
7361 xmlExpFree(ctxt, tmp);
7362 return(forbiddenExp);
7363 }
7364 if (sub->exp_max * mult > exp->exp_min)
7365 min = 0;
7366 else
7367 min = exp->exp_min - sub->exp_max * mult;
7368 max = exp->exp_max - sub->exp_max * mult;
7369 }
7370 }
7371 } else if (!IS_NILLABLE(tmp)) {
7372 /*
7373 * TODO: loop here to try to grow if working on finite
7374 * blocks.
7375 */
7376#ifdef DEBUG_DERIV
7377 printf("Count, Count remain not nillable => forbidden\n");
7378#endif
7379 xmlExpFree(ctxt, tmp);
7380 return(forbiddenExp);
7381 } else if (sub->exp_max == -1) {
7382 if (exp->exp_max == -1) {
7383 if (exp->exp_min <= sub->exp_min) {
7384#ifdef DEBUG_DERIV
7385 printf("Infinite loops Okay => COUNT(0,Inf)\n");
7386#endif
7387 max = -1;
7388 min = 0;
7389 } else {
7390#ifdef DEBUG_DERIV
7391 printf("Infinite loops min => Count(X,Inf)\n");
7392#endif
7393 max = -1;
7394 min = exp->exp_min - sub->exp_min;
7395 }
7396 } else if (exp->exp_min > sub->exp_min) {
7397#ifdef DEBUG_DERIV
7398 printf("loops min mismatch 1 => forbidden ???\n");
7399#endif
7400 xmlExpFree(ctxt, tmp);
7401 return(forbiddenExp);
7402 } else {
7403 max = -1;
7404 min = 0;
7405 }
7406 } else {
7407 if (exp->exp_max == -1) {
7408#ifdef DEBUG_DERIV
7409 printf("Infinite loop consume finite loop\n");
7410#endif
7411 if (exp->exp_min > sub->exp_min) {
7412 max = -1;
7413 min = exp->exp_min - sub->exp_min;
7414 } else {
7415 max = -1;
7416 min = 0;
7417 }
7418 } else {
7419 if (exp->exp_max < sub->exp_max) {
7420#ifdef DEBUG_DERIV
7421 printf("loops max mismatch => forbidden\n");
7422#endif
7423 xmlExpFree(ctxt, tmp);
7424 return(forbiddenExp);
7425 }
7426 if (sub->exp_max > exp->exp_min)
7427 min = 0;
7428 else
7429 min = exp->exp_min - sub->exp_max;
7430 max = exp->exp_max - sub->exp_max;
7431 }
7432 }
7433#ifdef DEBUG_DERIV
7434 printf("loops match => SEQ(COUNT())\n");
7435#endif
7436 exp->exp_left->ref++;
7437 tmp2 = xmlExpHashGetEntry(ctxt, XML_EXP_COUNT, exp->exp_left,
7438 NULL, NULL, min, max);
7439 if (tmp2 == NULL) {
7440 return(NULL);
7441 }
7442 ret = xmlExpHashGetEntry(ctxt, XML_EXP_SEQ, tmp, tmp2,
7443 NULL, 0, 0);
7444 return(ret);
7445 }
7446 tmp = xmlExpExpDeriveInt(ctxt, exp->exp_left, sub);
7447 if (tmp == NULL)
7448 return(NULL);
7449 if (tmp == forbiddenExp) {
7450#ifdef DEBUG_DERIV
7451 printf("loop mismatch => forbidden\n");
7452#endif
7453 return(forbiddenExp);
7454 }
7455 if (exp->exp_min > 0)
7456 min = exp->exp_min - 1;
7457 else
7458 min = 0;
7459 if (exp->exp_max < 0)
7460 max = -1;
7461 else
7462 max = exp->exp_max - 1;
7463
7464#ifdef DEBUG_DERIV
7465 printf("loop match => SEQ(COUNT())\n");
7466#endif
7467 exp->exp_left->ref++;
7468 tmp2 = xmlExpHashGetEntry(ctxt, XML_EXP_COUNT, exp->exp_left,
7469 NULL, NULL, min, max);
7470 if (tmp2 == NULL)
7471 return(NULL);
7472 ret = xmlExpHashGetEntry(ctxt, XML_EXP_SEQ, tmp, tmp2,
7473 NULL, 0, 0);
7474 return(ret);
7475 }
7476 }
7477
Daniel Veillardccb4d412005-08-23 13:41:17 +00007478#ifdef DEBUG_DERIV
7479 printf("Fallback to derivative\n");
7480#endif
7481 if (IS_NILLABLE(sub)) {
7482 if (!(IS_NILLABLE(exp)))
7483 return(forbiddenExp);
7484 else
7485 ret = emptyExp;
7486 } else
7487 ret = NULL;
Daniel Veillard81a8ec62005-08-22 00:20:58 +00007488 /*
7489 * here the structured derivation made no progress so
7490 * we use the default token based derivation to force one more step
7491 */
7492 if (ctxt->tabSize == 0)
7493 ctxt->tabSize = 40;
7494
7495 tab = (const xmlChar **) xmlMalloc(ctxt->tabSize *
7496 sizeof(const xmlChar *));
7497 if (tab == NULL) {
7498 return(NULL);
7499 }
7500
Daniel Veillard81a8ec62005-08-22 00:20:58 +00007501 /*
7502 * collect all the strings accepted by the subexpression on input
7503 */
7504 len = xmlExpGetStartInt(ctxt, sub, tab, ctxt->tabSize, 0);
7505 while (len < 0) {
7506 const xmlChar **temp;
Rob Richards54a8f672005-10-07 02:33:00 +00007507 temp = (const xmlChar **) xmlRealloc((xmlChar **) tab, ctxt->tabSize * 2 *
Daniel Veillard81a8ec62005-08-22 00:20:58 +00007508 sizeof(const xmlChar *));
7509 if (temp == NULL) {
Rob Richards54a8f672005-10-07 02:33:00 +00007510 xmlFree((xmlChar **) tab);
Daniel Veillard81a8ec62005-08-22 00:20:58 +00007511 return(NULL);
7512 }
7513 tab = temp;
7514 ctxt->tabSize *= 2;
7515 len = xmlExpGetStartInt(ctxt, sub, tab, ctxt->tabSize, 0);
7516 }
Daniel Veillard81a8ec62005-08-22 00:20:58 +00007517 for (i = 0;i < len;i++) {
7518 tmp = xmlExpStringDeriveInt(ctxt, exp, tab[i]);
7519 if ((tmp == NULL) || (tmp == forbiddenExp)) {
7520 xmlExpFree(ctxt, ret);
Rob Richards54a8f672005-10-07 02:33:00 +00007521 xmlFree((xmlChar **) tab);
Daniel Veillard81a8ec62005-08-22 00:20:58 +00007522 return(tmp);
7523 }
7524 tmp2 = xmlExpStringDeriveInt(ctxt, sub, tab[i]);
7525 if ((tmp2 == NULL) || (tmp2 == forbiddenExp)) {
7526 xmlExpFree(ctxt, tmp);
7527 xmlExpFree(ctxt, ret);
Rob Richards54a8f672005-10-07 02:33:00 +00007528 xmlFree((xmlChar **) tab);
Daniel Veillard81a8ec62005-08-22 00:20:58 +00007529 return(tmp);
7530 }
7531 tmp3 = xmlExpExpDeriveInt(ctxt, tmp, tmp2);
7532 xmlExpFree(ctxt, tmp);
7533 xmlExpFree(ctxt, tmp2);
7534
7535 if ((tmp3 == NULL) || (tmp3 == forbiddenExp)) {
7536 xmlExpFree(ctxt, ret);
Rob Richards54a8f672005-10-07 02:33:00 +00007537 xmlFree((xmlChar **) tab);
Daniel Veillard81a8ec62005-08-22 00:20:58 +00007538 return(tmp3);
7539 }
7540
7541 if (ret == NULL)
7542 ret = tmp3;
7543 else {
7544 ret = xmlExpHashGetEntry(ctxt, XML_EXP_OR, ret, tmp3, NULL, 0, 0);
7545 if (ret == NULL) {
Rob Richards54a8f672005-10-07 02:33:00 +00007546 xmlFree((xmlChar **) tab);
Daniel Veillard81a8ec62005-08-22 00:20:58 +00007547 return(NULL);
7548 }
7549 }
7550 }
Rob Richards54a8f672005-10-07 02:33:00 +00007551 xmlFree((xmlChar **) tab);
Daniel Veillard81a8ec62005-08-22 00:20:58 +00007552 return(ret);
7553}
7554
7555/**
Daniel Veillard0090bd52005-08-22 14:43:43 +00007556 * xmlExpExpDerive:
7557 * @ctxt: the expressions context
7558 * @exp: the englobing expression
7559 * @sub: the subexpression
7560 *
7561 * Evaluates the expression resulting from @exp consuming a sub expression @sub
7562 * Based on algebraic derivation and sometimes direct Brzozowski derivation
7563 * it usually tatkes less than linear time and can handle expressions generating
7564 * infinite languages.
7565 *
7566 * Returns the resulting expression or NULL in case of internal error, the
7567 * result must be freed
7568 */
7569xmlExpNodePtr
7570xmlExpExpDerive(xmlExpCtxtPtr ctxt, xmlExpNodePtr exp, xmlExpNodePtr sub) {
7571 if ((exp == NULL) || (ctxt == NULL) || (sub == NULL))
7572 return(NULL);
7573
7574 /*
7575 * O(1) speedups
7576 */
7577 if (IS_NILLABLE(sub) && (!IS_NILLABLE(exp))) {
7578#ifdef DEBUG_DERIV
7579 printf("Sub nillable and not exp : can't subsume\n");
7580#endif
7581 return(forbiddenExp);
7582 }
7583 if (xmlExpCheckCard(exp, sub) == 0) {
7584#ifdef DEBUG_DERIV
7585 printf("sub generate longuer sequances than exp : can't subsume\n");
7586#endif
7587 return(forbiddenExp);
7588 }
7589 return(xmlExpExpDeriveInt(ctxt, exp, sub));
7590}
7591
7592/**
Daniel Veillard81a8ec62005-08-22 00:20:58 +00007593 * xmlExpSubsume:
7594 * @ctxt: the expressions context
7595 * @exp: the englobing expression
7596 * @sub: the subexpression
7597 *
7598 * Check whether @exp accepts all the languages accexpted by @sub
7599 * the input being a subexpression.
7600 *
7601 * Returns 1 if true 0 if false and -1 in case of failure.
7602 */
7603int
7604xmlExpSubsume(xmlExpCtxtPtr ctxt, xmlExpNodePtr exp, xmlExpNodePtr sub) {
7605 xmlExpNodePtr tmp;
7606
7607 if ((exp == NULL) || (ctxt == NULL) || (sub == NULL))
7608 return(-1);
7609
7610 /*
7611 * TODO: speedup by checking the language of sub is a subset of the
7612 * language of exp
7613 */
7614 /*
7615 * O(1) speedups
7616 */
7617 if (IS_NILLABLE(sub) && (!IS_NILLABLE(exp))) {
7618#ifdef DEBUG_DERIV
7619 printf("Sub nillable and not exp : can't subsume\n");
7620#endif
7621 return(0);
7622 }
7623 if (xmlExpCheckCard(exp, sub) == 0) {
7624#ifdef DEBUG_DERIV
7625 printf("sub generate longuer sequances than exp : can't subsume\n");
7626#endif
7627 return(0);
7628 }
7629 tmp = xmlExpExpDeriveInt(ctxt, exp, sub);
7630#ifdef DEBUG_DERIV
7631 printf("Result derivation :\n");
7632 PRINT_EXP(tmp);
7633#endif
7634 if (tmp == NULL)
7635 return(-1);
7636 if (tmp == forbiddenExp)
7637 return(0);
7638 if (tmp == emptyExp)
7639 return(1);
7640 if ((tmp != NULL) && (IS_NILLABLE(tmp))) {
7641 xmlExpFree(ctxt, tmp);
7642 return(1);
7643 }
7644 xmlExpFree(ctxt, tmp);
7645 return(0);
7646}
Daniel Veillard465a0002005-08-22 12:07:04 +00007647
7648/************************************************************************
7649 * *
7650 * Parsing expression *
7651 * *
7652 ************************************************************************/
7653
7654static xmlExpNodePtr xmlExpParseExpr(xmlExpCtxtPtr ctxt);
7655
7656#undef CUR
7657#define CUR (*ctxt->cur)
7658#undef NEXT
7659#define NEXT ctxt->cur++;
7660#undef IS_BLANK
7661#define IS_BLANK(c) ((c == ' ') || (c == '\n') || (c == '\r') || (c == '\t'))
7662#define SKIP_BLANKS while (IS_BLANK(*ctxt->cur)) ctxt->cur++;
7663
7664static int
7665xmlExpParseNumber(xmlExpCtxtPtr ctxt) {
7666 int ret = 0;
7667
7668 SKIP_BLANKS
7669 if (CUR == '*') {
7670 NEXT
7671 return(-1);
7672 }
7673 if ((CUR < '0') || (CUR > '9'))
7674 return(-1);
7675 while ((CUR >= '0') && (CUR <= '9')) {
7676 ret = ret * 10 + (CUR - '0');
7677 NEXT
7678 }
7679 return(ret);
7680}
7681
7682static xmlExpNodePtr
7683xmlExpParseOr(xmlExpCtxtPtr ctxt) {
7684 const char *base;
7685 xmlExpNodePtr ret;
7686 const xmlChar *val;
7687
7688 SKIP_BLANKS
7689 base = ctxt->cur;
7690 if (*ctxt->cur == '(') {
7691 NEXT
7692 ret = xmlExpParseExpr(ctxt);
7693 SKIP_BLANKS
7694 if (*ctxt->cur != ')') {
7695 fprintf(stderr, "unbalanced '(' : %s\n", base);
7696 xmlExpFree(ctxt, ret);
7697 return(NULL);
7698 }
7699 NEXT;
7700 SKIP_BLANKS
7701 goto parse_quantifier;
7702 }
7703 while ((CUR != 0) && (!(IS_BLANK(CUR))) && (CUR != '(') &&
7704 (CUR != ')') && (CUR != '|') && (CUR != ',') && (CUR != '{') &&
7705 (CUR != '*') && (CUR != '+') && (CUR != '?') && (CUR != '}'))
7706 NEXT;
7707 val = xmlDictLookup(ctxt->dict, BAD_CAST base, ctxt->cur - base);
7708 if (val == NULL)
7709 return(NULL);
7710 ret = xmlExpHashGetEntry(ctxt, XML_EXP_ATOM, NULL, NULL, val, 0, 0);
7711 if (ret == NULL)
7712 return(NULL);
7713 SKIP_BLANKS
7714parse_quantifier:
7715 if (CUR == '{') {
7716 int min, max;
7717
7718 NEXT
7719 min = xmlExpParseNumber(ctxt);
7720 if (min < 0) {
7721 xmlExpFree(ctxt, ret);
7722 return(NULL);
7723 }
7724 SKIP_BLANKS
7725 if (CUR == ',') {
7726 NEXT
7727 max = xmlExpParseNumber(ctxt);
7728 SKIP_BLANKS
7729 } else
7730 max = min;
7731 if (CUR != '}') {
7732 xmlExpFree(ctxt, ret);
7733 return(NULL);
7734 }
7735 NEXT
7736 ret = xmlExpHashGetEntry(ctxt, XML_EXP_COUNT, ret, NULL, NULL,
7737 min, max);
7738 SKIP_BLANKS
7739 } else if (CUR == '?') {
7740 NEXT
7741 ret = xmlExpHashGetEntry(ctxt, XML_EXP_COUNT, ret, NULL, NULL,
7742 0, 1);
7743 SKIP_BLANKS
7744 } else if (CUR == '+') {
7745 NEXT
7746 ret = xmlExpHashGetEntry(ctxt, XML_EXP_COUNT, ret, NULL, NULL,
7747 1, -1);
7748 SKIP_BLANKS
7749 } else if (CUR == '*') {
7750 NEXT
7751 ret = xmlExpHashGetEntry(ctxt, XML_EXP_COUNT, ret, NULL, NULL,
7752 0, -1);
7753 SKIP_BLANKS
7754 }
7755 return(ret);
7756}
7757
7758
7759static xmlExpNodePtr
7760xmlExpParseSeq(xmlExpCtxtPtr ctxt) {
7761 xmlExpNodePtr ret, right;
7762
7763 ret = xmlExpParseOr(ctxt);
7764 SKIP_BLANKS
7765 while (CUR == '|') {
7766 NEXT
7767 right = xmlExpParseOr(ctxt);
7768 if (right == NULL) {
7769 xmlExpFree(ctxt, ret);
7770 return(NULL);
7771 }
7772 ret = xmlExpHashGetEntry(ctxt, XML_EXP_OR, ret, right, NULL, 0, 0);
7773 if (ret == NULL)
7774 return(NULL);
7775 }
7776 return(ret);
7777}
7778
7779static xmlExpNodePtr
7780xmlExpParseExpr(xmlExpCtxtPtr ctxt) {
7781 xmlExpNodePtr ret, right;
7782
7783 ret = xmlExpParseSeq(ctxt);
7784 SKIP_BLANKS
7785 while (CUR == ',') {
7786 NEXT
7787 right = xmlExpParseSeq(ctxt);
7788 if (right == NULL) {
7789 xmlExpFree(ctxt, ret);
7790 return(NULL);
7791 }
7792 ret = xmlExpHashGetEntry(ctxt, XML_EXP_SEQ, ret, right, NULL, 0, 0);
7793 if (ret == NULL)
7794 return(NULL);
7795 }
7796 return(ret);
7797}
7798
7799/**
7800 * xmlExpParse:
7801 * @ctxt: the expressions context
7802 * @expr: the 0 terminated string
7803 *
7804 * Minimal parser for regexps, it understand the following constructs
7805 * - string terminals
7806 * - choice operator |
7807 * - sequence operator ,
7808 * - subexpressions (...)
7809 * - usual cardinality operators + * and ?
7810 * - finite sequences { min, max }
7811 * - infinite sequences { min, * }
7812 * There is minimal checkings made especially no checking on strings values
7813 *
7814 * Returns a new expression or NULL in case of failure
7815 */
7816xmlExpNodePtr
7817xmlExpParse(xmlExpCtxtPtr ctxt, const char *expr) {
7818 xmlExpNodePtr ret;
7819
7820 ctxt->expr = expr;
7821 ctxt->cur = expr;
7822
7823 ret = xmlExpParseExpr(ctxt);
7824 SKIP_BLANKS
7825 if (*ctxt->cur != 0) {
7826 xmlExpFree(ctxt, ret);
7827 return(NULL);
7828 }
7829 return(ret);
7830}
7831
7832static void
7833xmlExpDumpInt(xmlBufferPtr buf, xmlExpNodePtr expr, int glob) {
7834 xmlExpNodePtr c;
7835
7836 if (expr == NULL) return;
7837 if (glob) xmlBufferWriteChar(buf, "(");
7838 switch (expr->type) {
7839 case XML_EXP_EMPTY:
7840 xmlBufferWriteChar(buf, "empty");
7841 break;
7842 case XML_EXP_FORBID:
7843 xmlBufferWriteChar(buf, "forbidden");
7844 break;
7845 case XML_EXP_ATOM:
7846 xmlBufferWriteCHAR(buf, expr->exp_str);
7847 break;
7848 case XML_EXP_SEQ:
7849 c = expr->exp_left;
7850 if ((c->type == XML_EXP_SEQ) || (c->type == XML_EXP_OR))
7851 xmlExpDumpInt(buf, c, 1);
7852 else
7853 xmlExpDumpInt(buf, c, 0);
7854 xmlBufferWriteChar(buf, " , ");
7855 c = expr->exp_right;
7856 if ((c->type == XML_EXP_SEQ) || (c->type == XML_EXP_OR))
7857 xmlExpDumpInt(buf, c, 1);
7858 else
7859 xmlExpDumpInt(buf, c, 0);
7860 break;
7861 case XML_EXP_OR:
7862 c = expr->exp_left;
7863 if ((c->type == XML_EXP_SEQ) || (c->type == XML_EXP_OR))
7864 xmlExpDumpInt(buf, c, 1);
7865 else
7866 xmlExpDumpInt(buf, c, 0);
7867 xmlBufferWriteChar(buf, " | ");
7868 c = expr->exp_right;
7869 if ((c->type == XML_EXP_SEQ) || (c->type == XML_EXP_OR))
7870 xmlExpDumpInt(buf, c, 1);
7871 else
7872 xmlExpDumpInt(buf, c, 0);
7873 break;
7874 case XML_EXP_COUNT: {
7875 char rep[40];
7876
7877 c = expr->exp_left;
7878 if ((c->type == XML_EXP_SEQ) || (c->type == XML_EXP_OR))
7879 xmlExpDumpInt(buf, c, 1);
7880 else
7881 xmlExpDumpInt(buf, c, 0);
7882 if ((expr->exp_min == 0) && (expr->exp_max == 1)) {
7883 rep[0] = '?';
7884 rep[1] = 0;
7885 } else if ((expr->exp_min == 0) && (expr->exp_max == -1)) {
7886 rep[0] = '*';
7887 rep[1] = 0;
7888 } else if ((expr->exp_min == 1) && (expr->exp_max == -1)) {
7889 rep[0] = '+';
7890 rep[1] = 0;
7891 } else if (expr->exp_max == expr->exp_min) {
7892 snprintf(rep, 39, "{%d}", expr->exp_min);
7893 } else if (expr->exp_max < 0) {
7894 snprintf(rep, 39, "{%d,inf}", expr->exp_min);
7895 } else {
7896 snprintf(rep, 39, "{%d,%d}", expr->exp_min, expr->exp_max);
7897 }
7898 rep[39] = 0;
7899 xmlBufferWriteChar(buf, rep);
7900 break;
7901 }
7902 default:
7903 fprintf(stderr, "Error in tree\n");
7904 }
7905 if (glob)
7906 xmlBufferWriteChar(buf, ")");
7907}
7908/**
7909 * xmlExpDump:
7910 * @buf: a buffer to receive the output
7911 * @expr: the compiled expression
7912 *
7913 * Serialize the expression as compiled to the buffer
7914 */
7915void
Daniel Veillard5eee7672005-08-22 21:22:27 +00007916xmlExpDump(xmlBufferPtr buf, xmlExpNodePtr expr) {
7917 if ((buf == NULL) || (expr == NULL))
Daniel Veillard465a0002005-08-22 12:07:04 +00007918 return;
Daniel Veillard5eee7672005-08-22 21:22:27 +00007919 xmlExpDumpInt(buf, expr, 0);
Daniel Veillard465a0002005-08-22 12:07:04 +00007920}
7921
7922/**
7923 * xmlExpMaxToken:
7924 * @expr: a compiled expression
7925 *
7926 * Indicate the maximum number of input a expression can accept
7927 *
7928 * Returns the maximum length or -1 in case of error
7929 */
7930int
7931xmlExpMaxToken(xmlExpNodePtr expr) {
7932 if (expr == NULL)
7933 return(-1);
7934 return(expr->c_max);
7935}
7936
7937/**
7938 * xmlExpCtxtNbNodes:
7939 * @ctxt: an expression context
7940 *
7941 * Debugging facility provides the number of allocated nodes at a that point
7942 *
7943 * Returns the number of nodes in use or -1 in case of error
7944 */
7945int
7946xmlExpCtxtNbNodes(xmlExpCtxtPtr ctxt) {
7947 if (ctxt == NULL)
7948 return(-1);
7949 return(ctxt->nb_nodes);
7950}
7951
7952/**
7953 * xmlExpCtxtNbCons:
7954 * @ctxt: an expression context
7955 *
7956 * Debugging facility provides the number of allocated nodes over lifetime
7957 *
7958 * Returns the number of nodes ever allocated or -1 in case of error
7959 */
7960int
7961xmlExpCtxtNbCons(xmlExpCtxtPtr ctxt) {
7962 if (ctxt == NULL)
7963 return(-1);
7964 return(ctxt->nb_cons);
7965}
7966
Daniel Veillard81a8ec62005-08-22 00:20:58 +00007967#endif /* LIBXML_EXPR_ENABLED */
Daniel Veillard5d4644e2005-04-01 13:11:58 +00007968#define bottom_xmlregexp
7969#include "elfgcchack.h"
Daniel Veillard4255d502002-04-16 15:50:10 +00007970#endif /* LIBXML_REGEXP_ENABLED */