blob: 0644d0bf7161a405cd9b3b9aec39ebc9480b6d86 [file] [log] [blame]
Daniel Veillard4255d502002-04-16 15:50:10 +00001/*
2 * regexp.c: generic and extensible Regular Expression engine
3 *
4 * Basically designed with the purpose of compiling regexps for
5 * the variety of validation/shemas mechanisms now available in
William M. Brackddf71d62004-05-06 04:17:26 +00006 * XML related specifications these include:
Daniel Veillard4255d502002-04-16 15:50:10 +00007 * - XML-1.0 DTD validation
8 * - XML Schemas structure part 1
9 * - XML Schemas Datatypes part 2 especially Appendix F
10 * - RELAX-NG/TREX i.e. the counter proposal
11 *
12 * See Copyright for the status of this software.
13 *
14 * Daniel Veillard <veillard@redhat.com>
15 */
16
17#define IN_LIBXML
18#include "libxml.h"
19
20#ifdef LIBXML_REGEXP_ENABLED
21
Daniel Veillardcee2b3a2005-01-25 00:22:52 +000022/* #define DEBUG_ERR */
Daniel Veillardfc0b6f62005-01-09 17:48:02 +000023
Daniel Veillard4255d502002-04-16 15:50:10 +000024#include <stdio.h>
25#include <string.h>
Daniel Veillardebe48c62003-12-03 12:12:27 +000026#ifdef HAVE_LIMITS_H
27#include <limits.h>
28#endif
29
Daniel Veillard4255d502002-04-16 15:50:10 +000030#include <libxml/tree.h>
31#include <libxml/parserInternals.h>
32#include <libxml/xmlregexp.h>
33#include <libxml/xmlautomata.h>
34#include <libxml/xmlunicode.h>
35
Daniel Veillardebe48c62003-12-03 12:12:27 +000036#ifndef INT_MAX
37#define INT_MAX 123456789 /* easy to flag and big enough for our needs */
38#endif
39
Daniel Veillardc0826a72004-08-10 14:17:33 +000040/* #define DEBUG_REGEXP_GRAPH */
Daniel Veillard10752282005-08-08 13:05:13 +000041/* #define DEBUG_REGEXP_EXEC */
Daniel Veillard4255d502002-04-16 15:50:10 +000042/* #define DEBUG_PUSH */
Daniel Veillard23e73572002-09-19 19:56:43 +000043/* #define DEBUG_COMPACTION */
Daniel Veillard4255d502002-04-16 15:50:10 +000044
Daniel Veillard567a45b2005-10-18 19:11:55 +000045#define MAX_PUSH 10000000
Daniel Veillard94cc1032005-09-15 13:09:00 +000046
Daniel Veillardff46a042003-10-08 08:53:17 +000047#define ERROR(str) \
48 ctxt->error = XML_REGEXP_COMPILE_ERROR; \
49 xmlRegexpErrCompile(ctxt, str);
Daniel Veillard4255d502002-04-16 15:50:10 +000050#define NEXT ctxt->cur++
51#define CUR (*(ctxt->cur))
52#define NXT(index) (ctxt->cur[index])
53
54#define CUR_SCHAR(s, l) xmlStringCurrentChar(NULL, s, &l)
55#define NEXTL(l) ctxt->cur += l;
Daniel Veillardc0826a72004-08-10 14:17:33 +000056#define XML_REG_STRING_SEPARATOR '|'
William M. Bracka9cbf282007-03-21 13:16:33 +000057/*
58 * Need PREV to check on a '-' within a Character Group. May only be used
59 * when it's guaranteed that cur is not at the beginning of ctxt->string!
60 */
61#define PREV (ctxt->cur[-1])
Daniel Veillard4255d502002-04-16 15:50:10 +000062
Daniel Veillarde19fc232002-04-22 16:01:24 +000063/**
64 * TODO:
65 *
66 * macro to flag unimplemented blocks
67 */
68#define TODO \
69 xmlGenericError(xmlGenericErrorContext, \
70 "Unimplemented block at %s:%d\n", \
71 __FILE__, __LINE__);
72
Daniel Veillard4255d502002-04-16 15:50:10 +000073/************************************************************************
74 * *
75 * Datatypes and structures *
76 * *
77 ************************************************************************/
78
Daniel Veillardfc011b72006-02-12 19:14:15 +000079/*
80 * Note: the order of the enums below is significant, do not shuffle
81 */
Daniel Veillard4255d502002-04-16 15:50:10 +000082typedef enum {
83 XML_REGEXP_EPSILON = 1,
84 XML_REGEXP_CHARVAL,
85 XML_REGEXP_RANGES,
Daniel Veillard567a45b2005-10-18 19:11:55 +000086 XML_REGEXP_SUBREG, /* used for () sub regexps */
Daniel Veillard4255d502002-04-16 15:50:10 +000087 XML_REGEXP_STRING,
88 XML_REGEXP_ANYCHAR, /* . */
89 XML_REGEXP_ANYSPACE, /* \s */
90 XML_REGEXP_NOTSPACE, /* \S */
91 XML_REGEXP_INITNAME, /* \l */
Daniel Veillard567a45b2005-10-18 19:11:55 +000092 XML_REGEXP_NOTINITNAME, /* \L */
Daniel Veillard4255d502002-04-16 15:50:10 +000093 XML_REGEXP_NAMECHAR, /* \c */
94 XML_REGEXP_NOTNAMECHAR, /* \C */
95 XML_REGEXP_DECIMAL, /* \d */
Daniel Veillard567a45b2005-10-18 19:11:55 +000096 XML_REGEXP_NOTDECIMAL, /* \D */
Daniel Veillard4255d502002-04-16 15:50:10 +000097 XML_REGEXP_REALCHAR, /* \w */
Daniel Veillard567a45b2005-10-18 19:11:55 +000098 XML_REGEXP_NOTREALCHAR, /* \W */
99 XML_REGEXP_LETTER = 100,
Daniel Veillard4255d502002-04-16 15:50:10 +0000100 XML_REGEXP_LETTER_UPPERCASE,
101 XML_REGEXP_LETTER_LOWERCASE,
102 XML_REGEXP_LETTER_TITLECASE,
103 XML_REGEXP_LETTER_MODIFIER,
104 XML_REGEXP_LETTER_OTHERS,
105 XML_REGEXP_MARK,
106 XML_REGEXP_MARK_NONSPACING,
107 XML_REGEXP_MARK_SPACECOMBINING,
108 XML_REGEXP_MARK_ENCLOSING,
109 XML_REGEXP_NUMBER,
110 XML_REGEXP_NUMBER_DECIMAL,
111 XML_REGEXP_NUMBER_LETTER,
112 XML_REGEXP_NUMBER_OTHERS,
113 XML_REGEXP_PUNCT,
114 XML_REGEXP_PUNCT_CONNECTOR,
115 XML_REGEXP_PUNCT_DASH,
116 XML_REGEXP_PUNCT_OPEN,
117 XML_REGEXP_PUNCT_CLOSE,
118 XML_REGEXP_PUNCT_INITQUOTE,
119 XML_REGEXP_PUNCT_FINQUOTE,
120 XML_REGEXP_PUNCT_OTHERS,
121 XML_REGEXP_SEPAR,
122 XML_REGEXP_SEPAR_SPACE,
123 XML_REGEXP_SEPAR_LINE,
124 XML_REGEXP_SEPAR_PARA,
125 XML_REGEXP_SYMBOL,
126 XML_REGEXP_SYMBOL_MATH,
127 XML_REGEXP_SYMBOL_CURRENCY,
128 XML_REGEXP_SYMBOL_MODIFIER,
129 XML_REGEXP_SYMBOL_OTHERS,
130 XML_REGEXP_OTHER,
131 XML_REGEXP_OTHER_CONTROL,
132 XML_REGEXP_OTHER_FORMAT,
133 XML_REGEXP_OTHER_PRIVATE,
134 XML_REGEXP_OTHER_NA,
135 XML_REGEXP_BLOCK_NAME
136} xmlRegAtomType;
137
138typedef enum {
139 XML_REGEXP_QUANT_EPSILON = 1,
140 XML_REGEXP_QUANT_ONCE,
141 XML_REGEXP_QUANT_OPT,
142 XML_REGEXP_QUANT_MULT,
143 XML_REGEXP_QUANT_PLUS,
Daniel Veillard7646b182002-04-20 06:41:40 +0000144 XML_REGEXP_QUANT_ONCEONLY,
145 XML_REGEXP_QUANT_ALL,
Daniel Veillard4255d502002-04-16 15:50:10 +0000146 XML_REGEXP_QUANT_RANGE
147} xmlRegQuantType;
148
149typedef enum {
150 XML_REGEXP_START_STATE = 1,
151 XML_REGEXP_FINAL_STATE,
Daniel Veillardcc026dc2005-01-12 13:21:17 +0000152 XML_REGEXP_TRANS_STATE,
Daniel Veillard0e05f4c2006-11-01 15:33:04 +0000153 XML_REGEXP_SINK_STATE,
154 XML_REGEXP_UNREACH_STATE
Daniel Veillard4255d502002-04-16 15:50:10 +0000155} xmlRegStateType;
156
157typedef enum {
158 XML_REGEXP_MARK_NORMAL = 0,
159 XML_REGEXP_MARK_START,
160 XML_REGEXP_MARK_VISITED
161} xmlRegMarkedType;
162
163typedef struct _xmlRegRange xmlRegRange;
164typedef xmlRegRange *xmlRegRangePtr;
165
166struct _xmlRegRange {
Daniel Veillardf8b9de32003-11-24 14:27:26 +0000167 int neg; /* 0 normal, 1 not, 2 exclude */
Daniel Veillard4255d502002-04-16 15:50:10 +0000168 xmlRegAtomType type;
169 int start;
170 int end;
171 xmlChar *blockName;
172};
173
174typedef struct _xmlRegAtom xmlRegAtom;
175typedef xmlRegAtom *xmlRegAtomPtr;
176
177typedef struct _xmlAutomataState xmlRegState;
178typedef xmlRegState *xmlRegStatePtr;
179
180struct _xmlRegAtom {
181 int no;
182 xmlRegAtomType type;
183 xmlRegQuantType quant;
184 int min;
185 int max;
186
187 void *valuep;
Daniel Veillarda646cfd2002-09-17 21:50:03 +0000188 void *valuep2;
Daniel Veillard4255d502002-04-16 15:50:10 +0000189 int neg;
190 int codepoint;
191 xmlRegStatePtr start;
Daniel Veillard76d59b62007-08-22 16:29:21 +0000192 xmlRegStatePtr start0;
Daniel Veillard4255d502002-04-16 15:50:10 +0000193 xmlRegStatePtr stop;
194 int maxRanges;
195 int nbRanges;
196 xmlRegRangePtr *ranges;
197 void *data;
198};
199
200typedef struct _xmlRegCounter xmlRegCounter;
201typedef xmlRegCounter *xmlRegCounterPtr;
202
203struct _xmlRegCounter {
204 int min;
205 int max;
206};
207
208typedef struct _xmlRegTrans xmlRegTrans;
209typedef xmlRegTrans *xmlRegTransPtr;
210
211struct _xmlRegTrans {
212 xmlRegAtomPtr atom;
213 int to;
214 int counter;
215 int count;
Daniel Veillard567a45b2005-10-18 19:11:55 +0000216 int nd;
Daniel Veillard4255d502002-04-16 15:50:10 +0000217};
218
219struct _xmlAutomataState {
220 xmlRegStateType type;
221 xmlRegMarkedType mark;
Daniel Veillard23e73572002-09-19 19:56:43 +0000222 xmlRegMarkedType reached;
Daniel Veillard4255d502002-04-16 15:50:10 +0000223 int no;
Daniel Veillard4255d502002-04-16 15:50:10 +0000224 int maxTrans;
225 int nbTrans;
226 xmlRegTrans *trans;
Daniel Veillarddb68b742005-07-30 13:18:24 +0000227 /* knowing states ponting to us can speed things up */
228 int maxTransTo;
229 int nbTransTo;
230 int *transTo;
Daniel Veillard4255d502002-04-16 15:50:10 +0000231};
232
233typedef struct _xmlAutomata xmlRegParserCtxt;
234typedef xmlRegParserCtxt *xmlRegParserCtxtPtr;
235
236struct _xmlAutomata {
237 xmlChar *string;
238 xmlChar *cur;
239
240 int error;
241 int neg;
242
243 xmlRegStatePtr start;
244 xmlRegStatePtr end;
245 xmlRegStatePtr state;
246
247 xmlRegAtomPtr atom;
248
249 int maxAtoms;
250 int nbAtoms;
251 xmlRegAtomPtr *atoms;
252
253 int maxStates;
254 int nbStates;
255 xmlRegStatePtr *states;
256
257 int maxCounters;
258 int nbCounters;
259 xmlRegCounter *counters;
Daniel Veillarde19fc232002-04-22 16:01:24 +0000260
261 int determinist;
Daniel Veillard6e65e152005-08-09 11:09:52 +0000262 int negs;
Daniel Veillard4255d502002-04-16 15:50:10 +0000263};
264
265struct _xmlRegexp {
266 xmlChar *string;
267 int nbStates;
268 xmlRegStatePtr *states;
269 int nbAtoms;
270 xmlRegAtomPtr *atoms;
271 int nbCounters;
272 xmlRegCounter *counters;
Daniel Veillarde19fc232002-04-22 16:01:24 +0000273 int determinist;
Daniel Veillard23e73572002-09-19 19:56:43 +0000274 /*
275 * That's the compact form for determinists automatas
276 */
277 int nbstates;
278 int *compact;
Daniel Veillard118aed72002-09-24 14:13:13 +0000279 void **transdata;
Daniel Veillard23e73572002-09-19 19:56:43 +0000280 int nbstrings;
281 xmlChar **stringMap;
Daniel Veillard4255d502002-04-16 15:50:10 +0000282};
283
284typedef struct _xmlRegExecRollback xmlRegExecRollback;
285typedef xmlRegExecRollback *xmlRegExecRollbackPtr;
286
287struct _xmlRegExecRollback {
288 xmlRegStatePtr state;/* the current state */
289 int index; /* the index in the input stack */
290 int nextbranch; /* the next transition to explore in that state */
William M. Brackddf71d62004-05-06 04:17:26 +0000291 int *counts; /* save the automata state if it has some */
Daniel Veillard4255d502002-04-16 15:50:10 +0000292};
293
294typedef struct _xmlRegInputToken xmlRegInputToken;
295typedef xmlRegInputToken *xmlRegInputTokenPtr;
296
297struct _xmlRegInputToken {
298 xmlChar *value;
299 void *data;
300};
301
302struct _xmlRegExecCtxt {
303 int status; /* execution status != 0 indicate an error */
William M. Brackddf71d62004-05-06 04:17:26 +0000304 int determinist; /* did we find an indeterministic behaviour */
Daniel Veillard4255d502002-04-16 15:50:10 +0000305 xmlRegexpPtr comp; /* the compiled regexp */
306 xmlRegExecCallbacks callback;
307 void *data;
308
309 xmlRegStatePtr state;/* the current state */
310 int transno; /* the current transition on that state */
William M. Brackddf71d62004-05-06 04:17:26 +0000311 int transcount; /* the number of chars in char counted transitions */
Daniel Veillard4255d502002-04-16 15:50:10 +0000312
313 /*
314 * A stack of rollback states
315 */
316 int maxRollbacks;
317 int nbRollbacks;
318 xmlRegExecRollback *rollbacks;
319
320 /*
321 * The state of the automata if any
322 */
323 int *counts;
324
325 /*
326 * The input stack
327 */
328 int inputStackMax;
329 int inputStackNr;
330 int index;
331 int *charStack;
332 const xmlChar *inputString; /* when operating on characters */
333 xmlRegInputTokenPtr inputStack;/* when operating on strings */
334
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +0000335 /*
336 * error handling
337 */
338 int errStateNo; /* the error state number */
339 xmlRegStatePtr errState; /* the error state */
340 xmlChar *errString; /* the string raising the error */
341 int *errCounts; /* counters at the error state */
Daniel Veillard94cc1032005-09-15 13:09:00 +0000342 int nbPush;
Daniel Veillard4255d502002-04-16 15:50:10 +0000343};
344
Daniel Veillard441bc322002-04-20 17:38:48 +0000345#define REGEXP_ALL_COUNTER 0x123456
346#define REGEXP_ALL_LAX_COUNTER 0x123457
Daniel Veillard7646b182002-04-20 06:41:40 +0000347
Daniel Veillard4255d502002-04-16 15:50:10 +0000348static void xmlFAParseRegExp(xmlRegParserCtxtPtr ctxt, int top);
Daniel Veillard23e73572002-09-19 19:56:43 +0000349static void xmlRegFreeState(xmlRegStatePtr state);
350static void xmlRegFreeAtom(xmlRegAtomPtr atom);
Daniel Veillard9efc4762005-07-19 14:33:55 +0000351static int xmlRegStrEqualWildcard(const xmlChar *expStr, const xmlChar *valStr);
Daniel Veillard567a45b2005-10-18 19:11:55 +0000352static int xmlRegCheckCharacter(xmlRegAtomPtr atom, int codepoint);
353static int xmlRegCheckCharacterRange(xmlRegAtomType type, int codepoint,
354 int neg, int start, int end, const xmlChar *blockName);
Daniel Veillard4255d502002-04-16 15:50:10 +0000355
356/************************************************************************
Daniel Veillardff46a042003-10-08 08:53:17 +0000357 * *
358 * Regexp memory error handler *
359 * *
360 ************************************************************************/
361/**
362 * xmlRegexpErrMemory:
William M. Brackddf71d62004-05-06 04:17:26 +0000363 * @extra: extra information
Daniel Veillardff46a042003-10-08 08:53:17 +0000364 *
365 * Handle an out of memory condition
366 */
367static void
368xmlRegexpErrMemory(xmlRegParserCtxtPtr ctxt, const char *extra)
369{
370 const char *regexp = NULL;
371 if (ctxt != NULL) {
372 regexp = (const char *) ctxt->string;
373 ctxt->error = XML_ERR_NO_MEMORY;
374 }
Daniel Veillard659e71e2003-10-10 14:10:40 +0000375 __xmlRaiseError(NULL, NULL, NULL, NULL, NULL, XML_FROM_REGEXP,
Daniel Veillardff46a042003-10-08 08:53:17 +0000376 XML_ERR_NO_MEMORY, XML_ERR_FATAL, NULL, 0, extra,
377 regexp, NULL, 0, 0,
378 "Memory allocation failed : %s\n", extra);
379}
380
381/**
382 * xmlRegexpErrCompile:
William M. Brackddf71d62004-05-06 04:17:26 +0000383 * @extra: extra information
Daniel Veillardff46a042003-10-08 08:53:17 +0000384 *
William M. Brackddf71d62004-05-06 04:17:26 +0000385 * Handle a compilation failure
Daniel Veillardff46a042003-10-08 08:53:17 +0000386 */
387static void
388xmlRegexpErrCompile(xmlRegParserCtxtPtr ctxt, const char *extra)
389{
390 const char *regexp = NULL;
391 int idx = 0;
392
393 if (ctxt != NULL) {
394 regexp = (const char *) ctxt->string;
395 idx = ctxt->cur - ctxt->string;
396 ctxt->error = XML_REGEXP_COMPILE_ERROR;
397 }
Daniel Veillard659e71e2003-10-10 14:10:40 +0000398 __xmlRaiseError(NULL, NULL, NULL, NULL, NULL, XML_FROM_REGEXP,
Daniel Veillardff46a042003-10-08 08:53:17 +0000399 XML_REGEXP_COMPILE_ERROR, XML_ERR_FATAL, NULL, 0, extra,
400 regexp, NULL, idx, 0,
401 "failed to compile: %s\n", extra);
402}
403
404/************************************************************************
Daniel Veillard4255d502002-04-16 15:50:10 +0000405 * *
406 * Allocation/Deallocation *
407 * *
408 ************************************************************************/
409
Daniel Veillard23e73572002-09-19 19:56:43 +0000410static int xmlFAComputesDeterminism(xmlRegParserCtxtPtr ctxt);
Daniel Veillard4255d502002-04-16 15:50:10 +0000411/**
412 * xmlRegEpxFromParse:
413 * @ctxt: the parser context used to build it
414 *
William M. Brackddf71d62004-05-06 04:17:26 +0000415 * Allocate a new regexp and fill it with the result from the parser
Daniel Veillard4255d502002-04-16 15:50:10 +0000416 *
417 * Returns the new regexp or NULL in case of error
418 */
419static xmlRegexpPtr
420xmlRegEpxFromParse(xmlRegParserCtxtPtr ctxt) {
421 xmlRegexpPtr ret;
422
423 ret = (xmlRegexpPtr) xmlMalloc(sizeof(xmlRegexp));
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000424 if (ret == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +0000425 xmlRegexpErrMemory(ctxt, "compiling regexp");
Daniel Veillard4255d502002-04-16 15:50:10 +0000426 return(NULL);
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000427 }
Daniel Veillard4255d502002-04-16 15:50:10 +0000428 memset(ret, 0, sizeof(xmlRegexp));
429 ret->string = ctxt->string;
Daniel Veillard4255d502002-04-16 15:50:10 +0000430 ret->nbStates = ctxt->nbStates;
Daniel Veillard4255d502002-04-16 15:50:10 +0000431 ret->states = ctxt->states;
Daniel Veillard4255d502002-04-16 15:50:10 +0000432 ret->nbAtoms = ctxt->nbAtoms;
Daniel Veillard4255d502002-04-16 15:50:10 +0000433 ret->atoms = ctxt->atoms;
Daniel Veillard4255d502002-04-16 15:50:10 +0000434 ret->nbCounters = ctxt->nbCounters;
Daniel Veillard4255d502002-04-16 15:50:10 +0000435 ret->counters = ctxt->counters;
Daniel Veillarde19fc232002-04-22 16:01:24 +0000436 ret->determinist = ctxt->determinist;
Daniel Veillard567a45b2005-10-18 19:11:55 +0000437 if (ret->determinist == -1) {
438 xmlRegexpIsDeterminist(ret);
439 }
Daniel Veillard23e73572002-09-19 19:56:43 +0000440
441 if ((ret->determinist != 0) &&
442 (ret->nbCounters == 0) &&
Daniel Veillard6e65e152005-08-09 11:09:52 +0000443 (ctxt->negs == 0) &&
Daniel Veillard118aed72002-09-24 14:13:13 +0000444 (ret->atoms != NULL) &&
Daniel Veillard23e73572002-09-19 19:56:43 +0000445 (ret->atoms[0] != NULL) &&
446 (ret->atoms[0]->type == XML_REGEXP_STRING)) {
447 int i, j, nbstates = 0, nbatoms = 0;
448 int *stateRemap;
449 int *stringRemap;
450 int *transitions;
Daniel Veillard118aed72002-09-24 14:13:13 +0000451 void **transdata;
Daniel Veillard23e73572002-09-19 19:56:43 +0000452 xmlChar **stringMap;
453 xmlChar *value;
454
455 /*
456 * Switch to a compact representation
457 * 1/ counting the effective number of states left
William M. Brackddf71d62004-05-06 04:17:26 +0000458 * 2/ counting the unique number of atoms, and check that
Daniel Veillard23e73572002-09-19 19:56:43 +0000459 * they are all of the string type
460 * 3/ build a table state x atom for the transitions
461 */
462
463 stateRemap = xmlMalloc(ret->nbStates * sizeof(int));
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000464 if (stateRemap == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +0000465 xmlRegexpErrMemory(ctxt, "compiling regexp");
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000466 xmlFree(ret);
467 return(NULL);
468 }
Daniel Veillard23e73572002-09-19 19:56:43 +0000469 for (i = 0;i < ret->nbStates;i++) {
470 if (ret->states[i] != NULL) {
471 stateRemap[i] = nbstates;
472 nbstates++;
473 } else {
474 stateRemap[i] = -1;
475 }
476 }
477#ifdef DEBUG_COMPACTION
478 printf("Final: %d states\n", nbstates);
479#endif
480 stringMap = xmlMalloc(ret->nbAtoms * sizeof(char *));
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000481 if (stringMap == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +0000482 xmlRegexpErrMemory(ctxt, "compiling regexp");
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000483 xmlFree(stateRemap);
484 xmlFree(ret);
485 return(NULL);
486 }
Daniel Veillard23e73572002-09-19 19:56:43 +0000487 stringRemap = xmlMalloc(ret->nbAtoms * sizeof(int));
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000488 if (stringRemap == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +0000489 xmlRegexpErrMemory(ctxt, "compiling regexp");
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000490 xmlFree(stringMap);
491 xmlFree(stateRemap);
492 xmlFree(ret);
493 return(NULL);
494 }
Daniel Veillard23e73572002-09-19 19:56:43 +0000495 for (i = 0;i < ret->nbAtoms;i++) {
496 if ((ret->atoms[i]->type == XML_REGEXP_STRING) &&
497 (ret->atoms[i]->quant == XML_REGEXP_QUANT_ONCE)) {
498 value = ret->atoms[i]->valuep;
499 for (j = 0;j < nbatoms;j++) {
500 if (xmlStrEqual(stringMap[j], value)) {
501 stringRemap[i] = j;
502 break;
503 }
504 }
505 if (j >= nbatoms) {
506 stringRemap[i] = nbatoms;
507 stringMap[nbatoms] = xmlStrdup(value);
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000508 if (stringMap[nbatoms] == NULL) {
509 for (i = 0;i < nbatoms;i++)
510 xmlFree(stringMap[i]);
511 xmlFree(stringRemap);
512 xmlFree(stringMap);
513 xmlFree(stateRemap);
514 xmlFree(ret);
515 return(NULL);
516 }
Daniel Veillard23e73572002-09-19 19:56:43 +0000517 nbatoms++;
518 }
519 } else {
520 xmlFree(stateRemap);
521 xmlFree(stringRemap);
522 for (i = 0;i < nbatoms;i++)
523 xmlFree(stringMap[i]);
524 xmlFree(stringMap);
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000525 xmlFree(ret);
526 return(NULL);
Daniel Veillard23e73572002-09-19 19:56:43 +0000527 }
528 }
529#ifdef DEBUG_COMPACTION
530 printf("Final: %d atoms\n", nbatoms);
531#endif
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000532 transitions = (int *) xmlMalloc((nbstates + 1) *
533 (nbatoms + 1) * sizeof(int));
534 if (transitions == NULL) {
535 xmlFree(stateRemap);
536 xmlFree(stringRemap);
537 xmlFree(stringMap);
538 xmlFree(ret);
539 return(NULL);
540 }
541 memset(transitions, 0, (nbstates + 1) * (nbatoms + 1) * sizeof(int));
Daniel Veillard23e73572002-09-19 19:56:43 +0000542
543 /*
544 * Allocate the transition table. The first entry for each
William M. Brackddf71d62004-05-06 04:17:26 +0000545 * state corresponds to the state type.
Daniel Veillard23e73572002-09-19 19:56:43 +0000546 */
Daniel Veillard118aed72002-09-24 14:13:13 +0000547 transdata = NULL;
Daniel Veillard23e73572002-09-19 19:56:43 +0000548
549 for (i = 0;i < ret->nbStates;i++) {
550 int stateno, atomno, targetno, prev;
551 xmlRegStatePtr state;
552 xmlRegTransPtr trans;
553
554 stateno = stateRemap[i];
555 if (stateno == -1)
556 continue;
557 state = ret->states[i];
558
559 transitions[stateno * (nbatoms + 1)] = state->type;
560
561 for (j = 0;j < state->nbTrans;j++) {
562 trans = &(state->trans[j]);
563 if ((trans->to == -1) || (trans->atom == NULL))
564 continue;
565 atomno = stringRemap[trans->atom->no];
Daniel Veillard118aed72002-09-24 14:13:13 +0000566 if ((trans->atom->data != NULL) && (transdata == NULL)) {
567 transdata = (void **) xmlMalloc(nbstates * nbatoms *
568 sizeof(void *));
569 if (transdata != NULL)
570 memset(transdata, 0,
571 nbstates * nbatoms * sizeof(void *));
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000572 else {
Daniel Veillardff46a042003-10-08 08:53:17 +0000573 xmlRegexpErrMemory(ctxt, "compiling regexp");
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000574 break;
575 }
Daniel Veillard118aed72002-09-24 14:13:13 +0000576 }
Daniel Veillard23e73572002-09-19 19:56:43 +0000577 targetno = stateRemap[trans->to];
578 /*
William M. Brackddf71d62004-05-06 04:17:26 +0000579 * if the same atom can generate transitions to 2 different
Daniel Veillard23e73572002-09-19 19:56:43 +0000580 * states then it means the automata is not determinist and
581 * the compact form can't be used !
582 */
583 prev = transitions[stateno * (nbatoms + 1) + atomno + 1];
584 if (prev != 0) {
585 if (prev != targetno + 1) {
Daniel Veillard23e73572002-09-19 19:56:43 +0000586 ret->determinist = 0;
587#ifdef DEBUG_COMPACTION
588 printf("Indet: state %d trans %d, atom %d to %d : %d to %d\n",
589 i, j, trans->atom->no, trans->to, atomno, targetno);
590 printf(" previous to is %d\n", prev);
591#endif
Daniel Veillard118aed72002-09-24 14:13:13 +0000592 if (transdata != NULL)
593 xmlFree(transdata);
Daniel Veillard23e73572002-09-19 19:56:43 +0000594 xmlFree(transitions);
595 xmlFree(stateRemap);
596 xmlFree(stringRemap);
597 for (i = 0;i < nbatoms;i++)
598 xmlFree(stringMap[i]);
599 xmlFree(stringMap);
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000600 goto not_determ;
Daniel Veillard23e73572002-09-19 19:56:43 +0000601 }
602 } else {
603#if 0
604 printf("State %d trans %d: atom %d to %d : %d to %d\n",
605 i, j, trans->atom->no, trans->to, atomno, targetno);
606#endif
607 transitions[stateno * (nbatoms + 1) + atomno + 1] =
Daniel Veillard118aed72002-09-24 14:13:13 +0000608 targetno + 1; /* to avoid 0 */
609 if (transdata != NULL)
610 transdata[stateno * nbatoms + atomno] =
611 trans->atom->data;
Daniel Veillard23e73572002-09-19 19:56:43 +0000612 }
613 }
614 }
615 ret->determinist = 1;
616#ifdef DEBUG_COMPACTION
617 /*
618 * Debug
619 */
620 for (i = 0;i < nbstates;i++) {
621 for (j = 0;j < nbatoms + 1;j++) {
622 printf("%02d ", transitions[i * (nbatoms + 1) + j]);
623 }
624 printf("\n");
625 }
626 printf("\n");
627#endif
628 /*
629 * Cleanup of the old data
630 */
631 if (ret->states != NULL) {
632 for (i = 0;i < ret->nbStates;i++)
633 xmlRegFreeState(ret->states[i]);
634 xmlFree(ret->states);
635 }
636 ret->states = NULL;
637 ret->nbStates = 0;
638 if (ret->atoms != NULL) {
639 for (i = 0;i < ret->nbAtoms;i++)
640 xmlRegFreeAtom(ret->atoms[i]);
641 xmlFree(ret->atoms);
642 }
643 ret->atoms = NULL;
644 ret->nbAtoms = 0;
645
646 ret->compact = transitions;
Daniel Veillard118aed72002-09-24 14:13:13 +0000647 ret->transdata = transdata;
Daniel Veillard23e73572002-09-19 19:56:43 +0000648 ret->stringMap = stringMap;
649 ret->nbstrings = nbatoms;
650 ret->nbstates = nbstates;
651 xmlFree(stateRemap);
652 xmlFree(stringRemap);
653 }
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000654not_determ:
655 ctxt->string = NULL;
656 ctxt->nbStates = 0;
657 ctxt->states = NULL;
658 ctxt->nbAtoms = 0;
659 ctxt->atoms = NULL;
660 ctxt->nbCounters = 0;
661 ctxt->counters = NULL;
Daniel Veillard4255d502002-04-16 15:50:10 +0000662 return(ret);
663}
664
665/**
666 * xmlRegNewParserCtxt:
667 * @string: the string to parse
668 *
669 * Allocate a new regexp parser context
670 *
671 * Returns the new context or NULL in case of error
672 */
673static xmlRegParserCtxtPtr
674xmlRegNewParserCtxt(const xmlChar *string) {
675 xmlRegParserCtxtPtr ret;
676
677 ret = (xmlRegParserCtxtPtr) xmlMalloc(sizeof(xmlRegParserCtxt));
678 if (ret == NULL)
679 return(NULL);
680 memset(ret, 0, sizeof(xmlRegParserCtxt));
681 if (string != NULL)
682 ret->string = xmlStrdup(string);
683 ret->cur = ret->string;
684 ret->neg = 0;
Daniel Veillard6e65e152005-08-09 11:09:52 +0000685 ret->negs = 0;
Daniel Veillard4255d502002-04-16 15:50:10 +0000686 ret->error = 0;
Daniel Veillarde19fc232002-04-22 16:01:24 +0000687 ret->determinist = -1;
Daniel Veillard4255d502002-04-16 15:50:10 +0000688 return(ret);
689}
690
691/**
692 * xmlRegNewRange:
693 * @ctxt: the regexp parser context
694 * @neg: is that negative
695 * @type: the type of range
696 * @start: the start codepoint
697 * @end: the end codepoint
698 *
699 * Allocate a new regexp range
700 *
701 * Returns the new range or NULL in case of error
702 */
703static xmlRegRangePtr
704xmlRegNewRange(xmlRegParserCtxtPtr ctxt,
705 int neg, xmlRegAtomType type, int start, int end) {
706 xmlRegRangePtr ret;
707
708 ret = (xmlRegRangePtr) xmlMalloc(sizeof(xmlRegRange));
709 if (ret == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +0000710 xmlRegexpErrMemory(ctxt, "allocating range");
Daniel Veillard4255d502002-04-16 15:50:10 +0000711 return(NULL);
712 }
713 ret->neg = neg;
714 ret->type = type;
715 ret->start = start;
716 ret->end = end;
717 return(ret);
718}
719
720/**
721 * xmlRegFreeRange:
722 * @range: the regexp range
723 *
724 * Free a regexp range
725 */
726static void
727xmlRegFreeRange(xmlRegRangePtr range) {
728 if (range == NULL)
729 return;
730
731 if (range->blockName != NULL)
732 xmlFree(range->blockName);
733 xmlFree(range);
734}
735
736/**
Daniel Veillard76d59b62007-08-22 16:29:21 +0000737 * xmlRegCopyRange:
738 * @range: the regexp range
739 *
740 * Copy a regexp range
741 *
742 * Returns the new copy or NULL in case of error.
743 */
744static xmlRegRangePtr
745xmlRegCopyRange(xmlRegParserCtxtPtr ctxt, xmlRegRangePtr range) {
746 xmlRegRangePtr ret;
747
748 if (range == NULL)
749 return(NULL);
750
751 ret = xmlRegNewRange(ctxt, range->neg, range->type, range->start,
752 range->end);
753 if (ret == NULL)
754 return(NULL);
755 if (range->blockName != NULL) {
756 ret->blockName = xmlStrdup(range->blockName);
757 if (ret->blockName == NULL) {
758 xmlRegexpErrMemory(ctxt, "allocating range");
759 xmlRegFreeRange(ret);
760 return(NULL);
761 }
762 }
763 return(ret);
764}
765
766/**
Daniel Veillard4255d502002-04-16 15:50:10 +0000767 * xmlRegNewAtom:
768 * @ctxt: the regexp parser context
769 * @type: the type of atom
770 *
Daniel Veillard76d59b62007-08-22 16:29:21 +0000771 * Allocate a new atom
Daniel Veillard4255d502002-04-16 15:50:10 +0000772 *
773 * Returns the new atom or NULL in case of error
774 */
775static xmlRegAtomPtr
776xmlRegNewAtom(xmlRegParserCtxtPtr ctxt, xmlRegAtomType type) {
777 xmlRegAtomPtr ret;
778
779 ret = (xmlRegAtomPtr) xmlMalloc(sizeof(xmlRegAtom));
780 if (ret == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +0000781 xmlRegexpErrMemory(ctxt, "allocating atom");
Daniel Veillard4255d502002-04-16 15:50:10 +0000782 return(NULL);
783 }
784 memset(ret, 0, sizeof(xmlRegAtom));
785 ret->type = type;
786 ret->quant = XML_REGEXP_QUANT_ONCE;
787 ret->min = 0;
788 ret->max = 0;
789 return(ret);
790}
791
792/**
793 * xmlRegFreeAtom:
794 * @atom: the regexp atom
795 *
796 * Free a regexp atom
797 */
798static void
799xmlRegFreeAtom(xmlRegAtomPtr atom) {
800 int i;
801
802 if (atom == NULL)
803 return;
804
805 for (i = 0;i < atom->nbRanges;i++)
806 xmlRegFreeRange(atom->ranges[i]);
807 if (atom->ranges != NULL)
808 xmlFree(atom->ranges);
Daniel Veillardde0e4982005-07-03 14:35:44 +0000809 if ((atom->type == XML_REGEXP_STRING) && (atom->valuep != NULL))
810 xmlFree(atom->valuep);
Daniel Veillard77005e62005-07-19 16:26:18 +0000811 if ((atom->type == XML_REGEXP_STRING) && (atom->valuep2 != NULL))
812 xmlFree(atom->valuep2);
Daniel Veillardde0e4982005-07-03 14:35:44 +0000813 if ((atom->type == XML_REGEXP_BLOCK_NAME) && (atom->valuep != NULL))
Daniel Veillard4255d502002-04-16 15:50:10 +0000814 xmlFree(atom->valuep);
815 xmlFree(atom);
816}
817
Daniel Veillard76d59b62007-08-22 16:29:21 +0000818/**
819 * xmlRegCopyAtom:
820 * @ctxt: the regexp parser context
821 * @atom: the oiginal atom
822 *
823 * Allocate a new regexp range
824 *
825 * Returns the new atom or NULL in case of error
826 */
827static xmlRegAtomPtr
828xmlRegCopyAtom(xmlRegParserCtxtPtr ctxt, xmlRegAtomPtr atom) {
829 xmlRegAtomPtr ret;
830
831 ret = (xmlRegAtomPtr) xmlMalloc(sizeof(xmlRegAtom));
832 if (ret == NULL) {
833 xmlRegexpErrMemory(ctxt, "copying atom");
834 return(NULL);
835 }
836 memset(ret, 0, sizeof(xmlRegAtom));
837 ret->type = atom->type;
838 ret->quant = atom->quant;
839 ret->min = atom->min;
840 ret->max = atom->max;
841 if (atom->nbRanges > 0) {
842 int i;
843
844 ret->ranges = (xmlRegRangePtr *) xmlMalloc(sizeof(xmlRegRangePtr) *
845 atom->nbRanges);
846 if (ret->ranges == NULL) {
847 xmlRegexpErrMemory(ctxt, "copying atom");
848 goto error;
849 }
850 for (i = 0;i < atom->nbRanges;i++) {
851 ret->ranges[i] = xmlRegCopyRange(ctxt, atom->ranges[i]);
852 if (ret->ranges[i] == NULL)
853 goto error;
854 ret->nbRanges = i + 1;
855 }
856 }
857 return(ret);
858
859error:
860 xmlRegFreeAtom(ret);
861 return(NULL);
862}
863
Daniel Veillard4255d502002-04-16 15:50:10 +0000864static xmlRegStatePtr
865xmlRegNewState(xmlRegParserCtxtPtr ctxt) {
866 xmlRegStatePtr ret;
867
868 ret = (xmlRegStatePtr) xmlMalloc(sizeof(xmlRegState));
869 if (ret == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +0000870 xmlRegexpErrMemory(ctxt, "allocating state");
Daniel Veillard4255d502002-04-16 15:50:10 +0000871 return(NULL);
872 }
873 memset(ret, 0, sizeof(xmlRegState));
874 ret->type = XML_REGEXP_TRANS_STATE;
875 ret->mark = XML_REGEXP_MARK_NORMAL;
876 return(ret);
877}
878
879/**
880 * xmlRegFreeState:
881 * @state: the regexp state
882 *
883 * Free a regexp state
884 */
885static void
886xmlRegFreeState(xmlRegStatePtr state) {
887 if (state == NULL)
888 return;
889
890 if (state->trans != NULL)
891 xmlFree(state->trans);
Daniel Veillarddb68b742005-07-30 13:18:24 +0000892 if (state->transTo != NULL)
893 xmlFree(state->transTo);
Daniel Veillard4255d502002-04-16 15:50:10 +0000894 xmlFree(state);
895}
896
897/**
898 * xmlRegFreeParserCtxt:
899 * @ctxt: the regexp parser context
900 *
901 * Free a regexp parser context
902 */
903static void
904xmlRegFreeParserCtxt(xmlRegParserCtxtPtr ctxt) {
905 int i;
906 if (ctxt == NULL)
907 return;
908
909 if (ctxt->string != NULL)
910 xmlFree(ctxt->string);
911 if (ctxt->states != NULL) {
912 for (i = 0;i < ctxt->nbStates;i++)
913 xmlRegFreeState(ctxt->states[i]);
914 xmlFree(ctxt->states);
915 }
916 if (ctxt->atoms != NULL) {
917 for (i = 0;i < ctxt->nbAtoms;i++)
918 xmlRegFreeAtom(ctxt->atoms[i]);
919 xmlFree(ctxt->atoms);
920 }
921 if (ctxt->counters != NULL)
922 xmlFree(ctxt->counters);
923 xmlFree(ctxt);
924}
925
926/************************************************************************
927 * *
928 * Display of Data structures *
929 * *
930 ************************************************************************/
931
932static void
933xmlRegPrintAtomType(FILE *output, xmlRegAtomType type) {
934 switch (type) {
935 case XML_REGEXP_EPSILON:
936 fprintf(output, "epsilon "); break;
937 case XML_REGEXP_CHARVAL:
938 fprintf(output, "charval "); break;
939 case XML_REGEXP_RANGES:
940 fprintf(output, "ranges "); break;
941 case XML_REGEXP_SUBREG:
942 fprintf(output, "subexpr "); break;
943 case XML_REGEXP_STRING:
944 fprintf(output, "string "); break;
945 case XML_REGEXP_ANYCHAR:
946 fprintf(output, "anychar "); break;
947 case XML_REGEXP_ANYSPACE:
948 fprintf(output, "anyspace "); break;
949 case XML_REGEXP_NOTSPACE:
950 fprintf(output, "notspace "); break;
951 case XML_REGEXP_INITNAME:
952 fprintf(output, "initname "); break;
953 case XML_REGEXP_NOTINITNAME:
954 fprintf(output, "notinitname "); break;
955 case XML_REGEXP_NAMECHAR:
956 fprintf(output, "namechar "); break;
957 case XML_REGEXP_NOTNAMECHAR:
958 fprintf(output, "notnamechar "); break;
959 case XML_REGEXP_DECIMAL:
960 fprintf(output, "decimal "); break;
961 case XML_REGEXP_NOTDECIMAL:
962 fprintf(output, "notdecimal "); break;
963 case XML_REGEXP_REALCHAR:
964 fprintf(output, "realchar "); break;
965 case XML_REGEXP_NOTREALCHAR:
966 fprintf(output, "notrealchar "); break;
967 case XML_REGEXP_LETTER:
968 fprintf(output, "LETTER "); break;
969 case XML_REGEXP_LETTER_UPPERCASE:
970 fprintf(output, "LETTER_UPPERCASE "); break;
971 case XML_REGEXP_LETTER_LOWERCASE:
972 fprintf(output, "LETTER_LOWERCASE "); break;
973 case XML_REGEXP_LETTER_TITLECASE:
974 fprintf(output, "LETTER_TITLECASE "); break;
975 case XML_REGEXP_LETTER_MODIFIER:
976 fprintf(output, "LETTER_MODIFIER "); break;
977 case XML_REGEXP_LETTER_OTHERS:
978 fprintf(output, "LETTER_OTHERS "); break;
979 case XML_REGEXP_MARK:
980 fprintf(output, "MARK "); break;
981 case XML_REGEXP_MARK_NONSPACING:
982 fprintf(output, "MARK_NONSPACING "); break;
983 case XML_REGEXP_MARK_SPACECOMBINING:
984 fprintf(output, "MARK_SPACECOMBINING "); break;
985 case XML_REGEXP_MARK_ENCLOSING:
986 fprintf(output, "MARK_ENCLOSING "); break;
987 case XML_REGEXP_NUMBER:
988 fprintf(output, "NUMBER "); break;
989 case XML_REGEXP_NUMBER_DECIMAL:
990 fprintf(output, "NUMBER_DECIMAL "); break;
991 case XML_REGEXP_NUMBER_LETTER:
992 fprintf(output, "NUMBER_LETTER "); break;
993 case XML_REGEXP_NUMBER_OTHERS:
994 fprintf(output, "NUMBER_OTHERS "); break;
995 case XML_REGEXP_PUNCT:
996 fprintf(output, "PUNCT "); break;
997 case XML_REGEXP_PUNCT_CONNECTOR:
998 fprintf(output, "PUNCT_CONNECTOR "); break;
999 case XML_REGEXP_PUNCT_DASH:
1000 fprintf(output, "PUNCT_DASH "); break;
1001 case XML_REGEXP_PUNCT_OPEN:
1002 fprintf(output, "PUNCT_OPEN "); break;
1003 case XML_REGEXP_PUNCT_CLOSE:
1004 fprintf(output, "PUNCT_CLOSE "); break;
1005 case XML_REGEXP_PUNCT_INITQUOTE:
1006 fprintf(output, "PUNCT_INITQUOTE "); break;
1007 case XML_REGEXP_PUNCT_FINQUOTE:
1008 fprintf(output, "PUNCT_FINQUOTE "); break;
1009 case XML_REGEXP_PUNCT_OTHERS:
1010 fprintf(output, "PUNCT_OTHERS "); break;
1011 case XML_REGEXP_SEPAR:
1012 fprintf(output, "SEPAR "); break;
1013 case XML_REGEXP_SEPAR_SPACE:
1014 fprintf(output, "SEPAR_SPACE "); break;
1015 case XML_REGEXP_SEPAR_LINE:
1016 fprintf(output, "SEPAR_LINE "); break;
1017 case XML_REGEXP_SEPAR_PARA:
1018 fprintf(output, "SEPAR_PARA "); break;
1019 case XML_REGEXP_SYMBOL:
1020 fprintf(output, "SYMBOL "); break;
1021 case XML_REGEXP_SYMBOL_MATH:
1022 fprintf(output, "SYMBOL_MATH "); break;
1023 case XML_REGEXP_SYMBOL_CURRENCY:
1024 fprintf(output, "SYMBOL_CURRENCY "); break;
1025 case XML_REGEXP_SYMBOL_MODIFIER:
1026 fprintf(output, "SYMBOL_MODIFIER "); break;
1027 case XML_REGEXP_SYMBOL_OTHERS:
1028 fprintf(output, "SYMBOL_OTHERS "); break;
1029 case XML_REGEXP_OTHER:
1030 fprintf(output, "OTHER "); break;
1031 case XML_REGEXP_OTHER_CONTROL:
1032 fprintf(output, "OTHER_CONTROL "); break;
1033 case XML_REGEXP_OTHER_FORMAT:
1034 fprintf(output, "OTHER_FORMAT "); break;
1035 case XML_REGEXP_OTHER_PRIVATE:
1036 fprintf(output, "OTHER_PRIVATE "); break;
1037 case XML_REGEXP_OTHER_NA:
1038 fprintf(output, "OTHER_NA "); break;
1039 case XML_REGEXP_BLOCK_NAME:
1040 fprintf(output, "BLOCK "); break;
1041 }
1042}
1043
1044static void
1045xmlRegPrintQuantType(FILE *output, xmlRegQuantType type) {
1046 switch (type) {
1047 case XML_REGEXP_QUANT_EPSILON:
1048 fprintf(output, "epsilon "); break;
1049 case XML_REGEXP_QUANT_ONCE:
1050 fprintf(output, "once "); break;
1051 case XML_REGEXP_QUANT_OPT:
1052 fprintf(output, "? "); break;
1053 case XML_REGEXP_QUANT_MULT:
1054 fprintf(output, "* "); break;
1055 case XML_REGEXP_QUANT_PLUS:
1056 fprintf(output, "+ "); break;
1057 case XML_REGEXP_QUANT_RANGE:
1058 fprintf(output, "range "); break;
Daniel Veillard7646b182002-04-20 06:41:40 +00001059 case XML_REGEXP_QUANT_ONCEONLY:
1060 fprintf(output, "onceonly "); break;
1061 case XML_REGEXP_QUANT_ALL:
1062 fprintf(output, "all "); break;
Daniel Veillard4255d502002-04-16 15:50:10 +00001063 }
1064}
1065static void
1066xmlRegPrintRange(FILE *output, xmlRegRangePtr range) {
1067 fprintf(output, " range: ");
1068 if (range->neg)
1069 fprintf(output, "negative ");
1070 xmlRegPrintAtomType(output, range->type);
1071 fprintf(output, "%c - %c\n", range->start, range->end);
1072}
1073
1074static void
1075xmlRegPrintAtom(FILE *output, xmlRegAtomPtr atom) {
1076 fprintf(output, " atom: ");
1077 if (atom == NULL) {
1078 fprintf(output, "NULL\n");
1079 return;
1080 }
Daniel Veillard9efc4762005-07-19 14:33:55 +00001081 if (atom->neg)
1082 fprintf(output, "not ");
Daniel Veillard4255d502002-04-16 15:50:10 +00001083 xmlRegPrintAtomType(output, atom->type);
1084 xmlRegPrintQuantType(output, atom->quant);
1085 if (atom->quant == XML_REGEXP_QUANT_RANGE)
1086 fprintf(output, "%d-%d ", atom->min, atom->max);
1087 if (atom->type == XML_REGEXP_STRING)
1088 fprintf(output, "'%s' ", (char *) atom->valuep);
1089 if (atom->type == XML_REGEXP_CHARVAL)
1090 fprintf(output, "char %c\n", atom->codepoint);
1091 else if (atom->type == XML_REGEXP_RANGES) {
1092 int i;
1093 fprintf(output, "%d entries\n", atom->nbRanges);
1094 for (i = 0; i < atom->nbRanges;i++)
1095 xmlRegPrintRange(output, atom->ranges[i]);
1096 } else if (atom->type == XML_REGEXP_SUBREG) {
1097 fprintf(output, "start %d end %d\n", atom->start->no, atom->stop->no);
1098 } else {
1099 fprintf(output, "\n");
1100 }
1101}
1102
1103static void
1104xmlRegPrintTrans(FILE *output, xmlRegTransPtr trans) {
1105 fprintf(output, " trans: ");
1106 if (trans == NULL) {
1107 fprintf(output, "NULL\n");
1108 return;
1109 }
1110 if (trans->to < 0) {
1111 fprintf(output, "removed\n");
1112 return;
1113 }
Daniel Veillard567a45b2005-10-18 19:11:55 +00001114 if (trans->nd != 0) {
1115 if (trans->nd == 2)
1116 fprintf(output, "last not determinist, ");
1117 else
1118 fprintf(output, "not determinist, ");
1119 }
Daniel Veillard4255d502002-04-16 15:50:10 +00001120 if (trans->counter >= 0) {
1121 fprintf(output, "counted %d, ", trans->counter);
1122 }
Daniel Veillard8a001f62002-04-20 07:24:11 +00001123 if (trans->count == REGEXP_ALL_COUNTER) {
1124 fprintf(output, "all transition, ");
1125 } else if (trans->count >= 0) {
Daniel Veillard4255d502002-04-16 15:50:10 +00001126 fprintf(output, "count based %d, ", trans->count);
1127 }
1128 if (trans->atom == NULL) {
1129 fprintf(output, "epsilon to %d\n", trans->to);
1130 return;
1131 }
1132 if (trans->atom->type == XML_REGEXP_CHARVAL)
1133 fprintf(output, "char %c ", trans->atom->codepoint);
1134 fprintf(output, "atom %d, to %d\n", trans->atom->no, trans->to);
1135}
1136
1137static void
1138xmlRegPrintState(FILE *output, xmlRegStatePtr state) {
1139 int i;
1140
1141 fprintf(output, " state: ");
1142 if (state == NULL) {
1143 fprintf(output, "NULL\n");
1144 return;
1145 }
1146 if (state->type == XML_REGEXP_START_STATE)
1147 fprintf(output, "START ");
1148 if (state->type == XML_REGEXP_FINAL_STATE)
1149 fprintf(output, "FINAL ");
1150
1151 fprintf(output, "%d, %d transitions:\n", state->no, state->nbTrans);
1152 for (i = 0;i < state->nbTrans; i++) {
1153 xmlRegPrintTrans(output, &(state->trans[i]));
1154 }
1155}
1156
Daniel Veillard23e73572002-09-19 19:56:43 +00001157#ifdef DEBUG_REGEXP_GRAPH
Daniel Veillard4255d502002-04-16 15:50:10 +00001158static void
1159xmlRegPrintCtxt(FILE *output, xmlRegParserCtxtPtr ctxt) {
1160 int i;
1161
1162 fprintf(output, " ctxt: ");
1163 if (ctxt == NULL) {
1164 fprintf(output, "NULL\n");
1165 return;
1166 }
1167 fprintf(output, "'%s' ", ctxt->string);
1168 if (ctxt->error)
1169 fprintf(output, "error ");
1170 if (ctxt->neg)
1171 fprintf(output, "neg ");
1172 fprintf(output, "\n");
1173 fprintf(output, "%d atoms:\n", ctxt->nbAtoms);
1174 for (i = 0;i < ctxt->nbAtoms; i++) {
1175 fprintf(output, " %02d ", i);
1176 xmlRegPrintAtom(output, ctxt->atoms[i]);
1177 }
1178 if (ctxt->atom != NULL) {
1179 fprintf(output, "current atom:\n");
1180 xmlRegPrintAtom(output, ctxt->atom);
1181 }
1182 fprintf(output, "%d states:", ctxt->nbStates);
1183 if (ctxt->start != NULL)
1184 fprintf(output, " start: %d", ctxt->start->no);
1185 if (ctxt->end != NULL)
1186 fprintf(output, " end: %d", ctxt->end->no);
1187 fprintf(output, "\n");
1188 for (i = 0;i < ctxt->nbStates; i++) {
1189 xmlRegPrintState(output, ctxt->states[i]);
1190 }
1191 fprintf(output, "%d counters:\n", ctxt->nbCounters);
1192 for (i = 0;i < ctxt->nbCounters; i++) {
1193 fprintf(output, " %d: min %d max %d\n", i, ctxt->counters[i].min,
1194 ctxt->counters[i].max);
1195 }
1196}
Daniel Veillard23e73572002-09-19 19:56:43 +00001197#endif
Daniel Veillard4255d502002-04-16 15:50:10 +00001198
1199/************************************************************************
1200 * *
1201 * Finite Automata structures manipulations *
1202 * *
1203 ************************************************************************/
1204
1205static void
1206xmlRegAtomAddRange(xmlRegParserCtxtPtr ctxt, xmlRegAtomPtr atom,
1207 int neg, xmlRegAtomType type, int start, int end,
1208 xmlChar *blockName) {
1209 xmlRegRangePtr range;
1210
1211 if (atom == NULL) {
1212 ERROR("add range: atom is NULL");
1213 return;
1214 }
1215 if (atom->type != XML_REGEXP_RANGES) {
1216 ERROR("add range: atom is not ranges");
1217 return;
1218 }
1219 if (atom->maxRanges == 0) {
1220 atom->maxRanges = 4;
1221 atom->ranges = (xmlRegRangePtr *) xmlMalloc(atom->maxRanges *
1222 sizeof(xmlRegRangePtr));
1223 if (atom->ranges == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00001224 xmlRegexpErrMemory(ctxt, "adding ranges");
Daniel Veillard4255d502002-04-16 15:50:10 +00001225 atom->maxRanges = 0;
1226 return;
1227 }
1228 } else if (atom->nbRanges >= atom->maxRanges) {
1229 xmlRegRangePtr *tmp;
1230 atom->maxRanges *= 2;
1231 tmp = (xmlRegRangePtr *) xmlRealloc(atom->ranges, atom->maxRanges *
1232 sizeof(xmlRegRangePtr));
1233 if (tmp == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00001234 xmlRegexpErrMemory(ctxt, "adding ranges");
Daniel Veillard4255d502002-04-16 15:50:10 +00001235 atom->maxRanges /= 2;
1236 return;
1237 }
1238 atom->ranges = tmp;
1239 }
1240 range = xmlRegNewRange(ctxt, neg, type, start, end);
1241 if (range == NULL)
1242 return;
1243 range->blockName = blockName;
1244 atom->ranges[atom->nbRanges++] = range;
1245
1246}
1247
1248static int
1249xmlRegGetCounter(xmlRegParserCtxtPtr ctxt) {
1250 if (ctxt->maxCounters == 0) {
1251 ctxt->maxCounters = 4;
1252 ctxt->counters = (xmlRegCounter *) xmlMalloc(ctxt->maxCounters *
1253 sizeof(xmlRegCounter));
1254 if (ctxt->counters == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00001255 xmlRegexpErrMemory(ctxt, "allocating counter");
Daniel Veillard4255d502002-04-16 15:50:10 +00001256 ctxt->maxCounters = 0;
1257 return(-1);
1258 }
1259 } else if (ctxt->nbCounters >= ctxt->maxCounters) {
1260 xmlRegCounter *tmp;
1261 ctxt->maxCounters *= 2;
1262 tmp = (xmlRegCounter *) xmlRealloc(ctxt->counters, ctxt->maxCounters *
1263 sizeof(xmlRegCounter));
1264 if (tmp == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00001265 xmlRegexpErrMemory(ctxt, "allocating counter");
Daniel Veillard4255d502002-04-16 15:50:10 +00001266 ctxt->maxCounters /= 2;
1267 return(-1);
1268 }
1269 ctxt->counters = tmp;
1270 }
1271 ctxt->counters[ctxt->nbCounters].min = -1;
1272 ctxt->counters[ctxt->nbCounters].max = -1;
1273 return(ctxt->nbCounters++);
1274}
1275
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001276static int
Daniel Veillard4255d502002-04-16 15:50:10 +00001277xmlRegAtomPush(xmlRegParserCtxtPtr ctxt, xmlRegAtomPtr atom) {
1278 if (atom == NULL) {
1279 ERROR("atom push: atom is NULL");
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001280 return(-1);
Daniel Veillard4255d502002-04-16 15:50:10 +00001281 }
1282 if (ctxt->maxAtoms == 0) {
1283 ctxt->maxAtoms = 4;
1284 ctxt->atoms = (xmlRegAtomPtr *) xmlMalloc(ctxt->maxAtoms *
1285 sizeof(xmlRegAtomPtr));
1286 if (ctxt->atoms == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00001287 xmlRegexpErrMemory(ctxt, "pushing atom");
Daniel Veillard4255d502002-04-16 15:50:10 +00001288 ctxt->maxAtoms = 0;
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001289 return(-1);
Daniel Veillard4255d502002-04-16 15:50:10 +00001290 }
1291 } else if (ctxt->nbAtoms >= ctxt->maxAtoms) {
1292 xmlRegAtomPtr *tmp;
1293 ctxt->maxAtoms *= 2;
1294 tmp = (xmlRegAtomPtr *) xmlRealloc(ctxt->atoms, ctxt->maxAtoms *
1295 sizeof(xmlRegAtomPtr));
1296 if (tmp == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00001297 xmlRegexpErrMemory(ctxt, "allocating counter");
Daniel Veillard4255d502002-04-16 15:50:10 +00001298 ctxt->maxAtoms /= 2;
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001299 return(-1);
Daniel Veillard4255d502002-04-16 15:50:10 +00001300 }
1301 ctxt->atoms = tmp;
1302 }
1303 atom->no = ctxt->nbAtoms;
1304 ctxt->atoms[ctxt->nbAtoms++] = atom;
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001305 return(0);
Daniel Veillard4255d502002-04-16 15:50:10 +00001306}
1307
1308static void
Daniel Veillarddb68b742005-07-30 13:18:24 +00001309xmlRegStateAddTransTo(xmlRegParserCtxtPtr ctxt, xmlRegStatePtr target,
1310 int from) {
1311 if (target->maxTransTo == 0) {
1312 target->maxTransTo = 8;
1313 target->transTo = (int *) xmlMalloc(target->maxTransTo *
1314 sizeof(int));
1315 if (target->transTo == NULL) {
1316 xmlRegexpErrMemory(ctxt, "adding transition");
1317 target->maxTransTo = 0;
1318 return;
1319 }
1320 } else if (target->nbTransTo >= target->maxTransTo) {
1321 int *tmp;
1322 target->maxTransTo *= 2;
1323 tmp = (int *) xmlRealloc(target->transTo, target->maxTransTo *
1324 sizeof(int));
1325 if (tmp == NULL) {
1326 xmlRegexpErrMemory(ctxt, "adding transition");
1327 target->maxTransTo /= 2;
1328 return;
1329 }
1330 target->transTo = tmp;
1331 }
1332 target->transTo[target->nbTransTo] = from;
1333 target->nbTransTo++;
1334}
1335
1336static void
Daniel Veillard4255d502002-04-16 15:50:10 +00001337xmlRegStateAddTrans(xmlRegParserCtxtPtr ctxt, xmlRegStatePtr state,
1338 xmlRegAtomPtr atom, xmlRegStatePtr target,
Daniel Veillard5de09382005-09-26 17:18:17 +00001339 int counter, int count) {
William M. Brackf9b5fa22004-05-10 07:52:15 +00001340
1341 int nrtrans;
1342
Daniel Veillard4255d502002-04-16 15:50:10 +00001343 if (state == NULL) {
1344 ERROR("add state: state is NULL");
1345 return;
1346 }
1347 if (target == NULL) {
1348 ERROR("add state: target is NULL");
1349 return;
1350 }
William M. Brackf9b5fa22004-05-10 07:52:15 +00001351 /*
1352 * Other routines follow the philosophy 'When in doubt, add a transition'
1353 * so we check here whether such a transition is already present and, if
1354 * so, silently ignore this request.
1355 */
1356
Daniel Veillard5de09382005-09-26 17:18:17 +00001357 for (nrtrans = state->nbTrans - 1; nrtrans >= 0; nrtrans--) {
1358 xmlRegTransPtr trans = &(state->trans[nrtrans]);
1359 if ((trans->atom == atom) &&
1360 (trans->to == target->no) &&
1361 (trans->counter == counter) &&
1362 (trans->count == count)) {
William M. Brackf9b5fa22004-05-10 07:52:15 +00001363#ifdef DEBUG_REGEXP_GRAPH
Daniel Veillard5de09382005-09-26 17:18:17 +00001364 printf("Ignoring duplicate transition from %d to %d\n",
1365 state->no, target->no);
William M. Brackf9b5fa22004-05-10 07:52:15 +00001366#endif
Daniel Veillard5de09382005-09-26 17:18:17 +00001367 return;
Daniel Veillarddb68b742005-07-30 13:18:24 +00001368 }
William M. Brackf9b5fa22004-05-10 07:52:15 +00001369 }
1370
Daniel Veillard4255d502002-04-16 15:50:10 +00001371 if (state->maxTrans == 0) {
Daniel Veillarddb68b742005-07-30 13:18:24 +00001372 state->maxTrans = 8;
Daniel Veillard4255d502002-04-16 15:50:10 +00001373 state->trans = (xmlRegTrans *) xmlMalloc(state->maxTrans *
1374 sizeof(xmlRegTrans));
1375 if (state->trans == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00001376 xmlRegexpErrMemory(ctxt, "adding transition");
Daniel Veillard4255d502002-04-16 15:50:10 +00001377 state->maxTrans = 0;
1378 return;
1379 }
1380 } else if (state->nbTrans >= state->maxTrans) {
1381 xmlRegTrans *tmp;
1382 state->maxTrans *= 2;
1383 tmp = (xmlRegTrans *) xmlRealloc(state->trans, state->maxTrans *
1384 sizeof(xmlRegTrans));
1385 if (tmp == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00001386 xmlRegexpErrMemory(ctxt, "adding transition");
Daniel Veillard4255d502002-04-16 15:50:10 +00001387 state->maxTrans /= 2;
1388 return;
1389 }
1390 state->trans = tmp;
1391 }
1392#ifdef DEBUG_REGEXP_GRAPH
1393 printf("Add trans from %d to %d ", state->no, target->no);
Daniel Veillard8a001f62002-04-20 07:24:11 +00001394 if (count == REGEXP_ALL_COUNTER)
Daniel Veillard2cbf5962004-03-31 15:50:43 +00001395 printf("all transition\n");
Daniel Veillard4402ab42002-09-12 16:02:56 +00001396 else if (count >= 0)
Daniel Veillard2cbf5962004-03-31 15:50:43 +00001397 printf("count based %d\n", count);
Daniel Veillard4255d502002-04-16 15:50:10 +00001398 else if (counter >= 0)
Daniel Veillard2cbf5962004-03-31 15:50:43 +00001399 printf("counted %d\n", counter);
Daniel Veillard4255d502002-04-16 15:50:10 +00001400 else if (atom == NULL)
Daniel Veillard2cbf5962004-03-31 15:50:43 +00001401 printf("epsilon transition\n");
1402 else if (atom != NULL)
1403 xmlRegPrintAtom(stdout, atom);
Daniel Veillard4255d502002-04-16 15:50:10 +00001404#endif
1405
1406 state->trans[state->nbTrans].atom = atom;
1407 state->trans[state->nbTrans].to = target->no;
1408 state->trans[state->nbTrans].counter = counter;
1409 state->trans[state->nbTrans].count = count;
Daniel Veillard567a45b2005-10-18 19:11:55 +00001410 state->trans[state->nbTrans].nd = 0;
Daniel Veillard4255d502002-04-16 15:50:10 +00001411 state->nbTrans++;
Daniel Veillarddb68b742005-07-30 13:18:24 +00001412 xmlRegStateAddTransTo(ctxt, target, state->no);
Daniel Veillard4255d502002-04-16 15:50:10 +00001413}
1414
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001415static int
Daniel Veillard4255d502002-04-16 15:50:10 +00001416xmlRegStatePush(xmlRegParserCtxtPtr ctxt, xmlRegStatePtr state) {
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001417 if (state == NULL) return(-1);
Daniel Veillard4255d502002-04-16 15:50:10 +00001418 if (ctxt->maxStates == 0) {
1419 ctxt->maxStates = 4;
1420 ctxt->states = (xmlRegStatePtr *) xmlMalloc(ctxt->maxStates *
1421 sizeof(xmlRegStatePtr));
1422 if (ctxt->states == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00001423 xmlRegexpErrMemory(ctxt, "adding state");
Daniel Veillard4255d502002-04-16 15:50:10 +00001424 ctxt->maxStates = 0;
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001425 return(-1);
Daniel Veillard4255d502002-04-16 15:50:10 +00001426 }
1427 } else if (ctxt->nbStates >= ctxt->maxStates) {
1428 xmlRegStatePtr *tmp;
1429 ctxt->maxStates *= 2;
1430 tmp = (xmlRegStatePtr *) xmlRealloc(ctxt->states, ctxt->maxStates *
1431 sizeof(xmlRegStatePtr));
1432 if (tmp == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00001433 xmlRegexpErrMemory(ctxt, "adding state");
Daniel Veillard4255d502002-04-16 15:50:10 +00001434 ctxt->maxStates /= 2;
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001435 return(-1);
Daniel Veillard4255d502002-04-16 15:50:10 +00001436 }
1437 ctxt->states = tmp;
1438 }
1439 state->no = ctxt->nbStates;
1440 ctxt->states[ctxt->nbStates++] = state;
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001441 return(0);
Daniel Veillard4255d502002-04-16 15:50:10 +00001442}
1443
1444/**
Daniel Veillard7646b182002-04-20 06:41:40 +00001445 * xmlFAGenerateAllTransition:
Daniel Veillard441bc322002-04-20 17:38:48 +00001446 * @ctxt: a regexp parser context
1447 * @from: the from state
1448 * @to: the target state or NULL for building a new one
1449 * @lax:
Daniel Veillard7646b182002-04-20 06:41:40 +00001450 *
1451 */
1452static void
1453xmlFAGenerateAllTransition(xmlRegParserCtxtPtr ctxt,
Daniel Veillard441bc322002-04-20 17:38:48 +00001454 xmlRegStatePtr from, xmlRegStatePtr to,
1455 int lax) {
Daniel Veillard7646b182002-04-20 06:41:40 +00001456 if (to == NULL) {
1457 to = xmlRegNewState(ctxt);
1458 xmlRegStatePush(ctxt, to);
1459 ctxt->state = to;
1460 }
Daniel Veillard441bc322002-04-20 17:38:48 +00001461 if (lax)
Daniel Veillard5de09382005-09-26 17:18:17 +00001462 xmlRegStateAddTrans(ctxt, from, NULL, to, -1, REGEXP_ALL_LAX_COUNTER);
Daniel Veillard441bc322002-04-20 17:38:48 +00001463 else
Daniel Veillard5de09382005-09-26 17:18:17 +00001464 xmlRegStateAddTrans(ctxt, from, NULL, to, -1, REGEXP_ALL_COUNTER);
Daniel Veillard7646b182002-04-20 06:41:40 +00001465}
1466
1467/**
Daniel Veillard4255d502002-04-16 15:50:10 +00001468 * xmlFAGenerateEpsilonTransition:
Daniel Veillard441bc322002-04-20 17:38:48 +00001469 * @ctxt: a regexp parser context
1470 * @from: the from state
1471 * @to: the target state or NULL for building a new one
Daniel Veillard4255d502002-04-16 15:50:10 +00001472 *
1473 */
1474static void
1475xmlFAGenerateEpsilonTransition(xmlRegParserCtxtPtr ctxt,
1476 xmlRegStatePtr from, xmlRegStatePtr to) {
1477 if (to == NULL) {
1478 to = xmlRegNewState(ctxt);
1479 xmlRegStatePush(ctxt, to);
1480 ctxt->state = to;
1481 }
Daniel Veillard5de09382005-09-26 17:18:17 +00001482 xmlRegStateAddTrans(ctxt, from, NULL, to, -1, -1);
Daniel Veillard4255d502002-04-16 15:50:10 +00001483}
1484
1485/**
1486 * xmlFAGenerateCountedEpsilonTransition:
Daniel Veillard441bc322002-04-20 17:38:48 +00001487 * @ctxt: a regexp parser context
1488 * @from: the from state
1489 * @to: the target state or NULL for building a new one
Daniel Veillard4255d502002-04-16 15:50:10 +00001490 * counter: the counter for that transition
1491 *
1492 */
1493static void
1494xmlFAGenerateCountedEpsilonTransition(xmlRegParserCtxtPtr ctxt,
1495 xmlRegStatePtr from, xmlRegStatePtr to, int counter) {
1496 if (to == NULL) {
1497 to = xmlRegNewState(ctxt);
1498 xmlRegStatePush(ctxt, to);
1499 ctxt->state = to;
1500 }
Daniel Veillard5de09382005-09-26 17:18:17 +00001501 xmlRegStateAddTrans(ctxt, from, NULL, to, counter, -1);
Daniel Veillard4255d502002-04-16 15:50:10 +00001502}
1503
1504/**
1505 * xmlFAGenerateCountedTransition:
Daniel Veillard441bc322002-04-20 17:38:48 +00001506 * @ctxt: a regexp parser context
1507 * @from: the from state
1508 * @to: the target state or NULL for building a new one
Daniel Veillard4255d502002-04-16 15:50:10 +00001509 * counter: the counter for that transition
1510 *
1511 */
1512static void
1513xmlFAGenerateCountedTransition(xmlRegParserCtxtPtr ctxt,
1514 xmlRegStatePtr from, xmlRegStatePtr to, int counter) {
1515 if (to == NULL) {
1516 to = xmlRegNewState(ctxt);
1517 xmlRegStatePush(ctxt, to);
1518 ctxt->state = to;
1519 }
Daniel Veillard5de09382005-09-26 17:18:17 +00001520 xmlRegStateAddTrans(ctxt, from, NULL, to, -1, counter);
Daniel Veillard4255d502002-04-16 15:50:10 +00001521}
1522
1523/**
1524 * xmlFAGenerateTransitions:
Daniel Veillard441bc322002-04-20 17:38:48 +00001525 * @ctxt: a regexp parser context
1526 * @from: the from state
1527 * @to: the target state or NULL for building a new one
1528 * @atom: the atom generating the transition
Daniel Veillard4255d502002-04-16 15:50:10 +00001529 *
William M. Brackddf71d62004-05-06 04:17:26 +00001530 * Returns 0 if success and -1 in case of error.
Daniel Veillard4255d502002-04-16 15:50:10 +00001531 */
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001532static int
Daniel Veillard4255d502002-04-16 15:50:10 +00001533xmlFAGenerateTransitions(xmlRegParserCtxtPtr ctxt, xmlRegStatePtr from,
1534 xmlRegStatePtr to, xmlRegAtomPtr atom) {
Daniel Veillard10bda622008-03-13 07:27:24 +00001535 xmlRegStatePtr end;
1536
Daniel Veillard4255d502002-04-16 15:50:10 +00001537 if (atom == NULL) {
1538 ERROR("genrate transition: atom == NULL");
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001539 return(-1);
Daniel Veillard4255d502002-04-16 15:50:10 +00001540 }
1541 if (atom->type == XML_REGEXP_SUBREG) {
1542 /*
1543 * this is a subexpression handling one should not need to
William M. Brackddf71d62004-05-06 04:17:26 +00001544 * create a new node except for XML_REGEXP_QUANT_RANGE.
Daniel Veillard4255d502002-04-16 15:50:10 +00001545 */
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001546 if (xmlRegAtomPush(ctxt, atom) < 0) {
1547 return(-1);
1548 }
Daniel Veillard4255d502002-04-16 15:50:10 +00001549 if ((to != NULL) && (atom->stop != to) &&
1550 (atom->quant != XML_REGEXP_QUANT_RANGE)) {
1551 /*
1552 * Generate an epsilon transition to link to the target
1553 */
1554 xmlFAGenerateEpsilonTransition(ctxt, atom->stop, to);
Daniel Veillardaa622012005-10-20 15:55:25 +00001555#ifdef DV
1556 } else if ((to == NULL) && (atom->quant != XML_REGEXP_QUANT_RANGE) &&
1557 (atom->quant != XML_REGEXP_QUANT_ONCE)) {
1558 to = xmlRegNewState(ctxt);
1559 xmlRegStatePush(ctxt, to);
1560 ctxt->state = to;
1561 xmlFAGenerateEpsilonTransition(ctxt, atom->stop, to);
1562#endif
Daniel Veillard4255d502002-04-16 15:50:10 +00001563 }
1564 switch (atom->quant) {
1565 case XML_REGEXP_QUANT_OPT:
1566 atom->quant = XML_REGEXP_QUANT_ONCE;
Daniel Veillard54eb0242006-03-21 23:17:57 +00001567 /*
1568 * transition done to the state after end of atom.
1569 * 1. set transition from atom start to new state
1570 * 2. set transition from atom end to this state.
1571 */
Daniel Veillardd80d0722009-08-22 18:56:01 +02001572 if (to == NULL) {
1573 xmlFAGenerateEpsilonTransition(ctxt, atom->start, 0);
1574 xmlFAGenerateEpsilonTransition(ctxt, atom->stop,
1575 ctxt->state);
1576 } else {
1577 xmlFAGenerateEpsilonTransition(ctxt, atom->start, to);
1578 }
Daniel Veillard4255d502002-04-16 15:50:10 +00001579 break;
1580 case XML_REGEXP_QUANT_MULT:
1581 atom->quant = XML_REGEXP_QUANT_ONCE;
1582 xmlFAGenerateEpsilonTransition(ctxt, atom->start, atom->stop);
1583 xmlFAGenerateEpsilonTransition(ctxt, atom->stop, atom->start);
1584 break;
1585 case XML_REGEXP_QUANT_PLUS:
1586 atom->quant = XML_REGEXP_QUANT_ONCE;
1587 xmlFAGenerateEpsilonTransition(ctxt, atom->stop, atom->start);
1588 break;
1589 case XML_REGEXP_QUANT_RANGE: {
1590 int counter;
Daniel Veillard76d59b62007-08-22 16:29:21 +00001591 xmlRegStatePtr inter, newstate;
Daniel Veillard4255d502002-04-16 15:50:10 +00001592
1593 /*
Daniel Veillard76d59b62007-08-22 16:29:21 +00001594 * create the final state now if needed
Daniel Veillard4255d502002-04-16 15:50:10 +00001595 */
Daniel Veillard4255d502002-04-16 15:50:10 +00001596 if (to != NULL) {
1597 newstate = to;
1598 } else {
1599 newstate = xmlRegNewState(ctxt);
1600 xmlRegStatePush(ctxt, newstate);
Daniel Veillard4255d502002-04-16 15:50:10 +00001601 }
Daniel Veillard76d59b62007-08-22 16:29:21 +00001602
1603 /*
1604 * The principle here is to use counted transition
1605 * to avoid explosion in the number of states in the
1606 * graph. This is clearly more complex but should not
1607 * be exploitable at runtime.
Daniel Veillard54eb0242006-03-21 23:17:57 +00001608 */
Daniel Veillard76d59b62007-08-22 16:29:21 +00001609 if ((atom->min == 0) && (atom->start0 == NULL)) {
1610 xmlRegAtomPtr copy;
1611 /*
1612 * duplicate a transition based on atom to count next
1613 * occurences after 1. We cannot loop to atom->start
1614 * directly because we need an epsilon transition to
1615 * newstate.
1616 */
1617 /* ???? For some reason it seems we never reach that
1618 case, I suppose this got optimized out before when
1619 building the automata */
Daniel Veillardc821e032007-08-28 17:33:45 +00001620 copy = xmlRegCopyAtom(ctxt, atom);
Daniel Veillard76d59b62007-08-22 16:29:21 +00001621 if (copy == NULL)
1622 return(-1);
Daniel Veillard76d59b62007-08-22 16:29:21 +00001623 copy->quant = XML_REGEXP_QUANT_ONCE;
1624 copy->min = 0;
1625 copy->max = 0;
1626
1627 if (xmlFAGenerateTransitions(ctxt, atom->start, NULL, copy)
1628 < 0)
1629 return(-1);
1630 inter = ctxt->state;
1631 counter = xmlRegGetCounter(ctxt);
1632 ctxt->counters[counter].min = atom->min - 1;
1633 ctxt->counters[counter].max = atom->max - 1;
1634 /* count the number of times we see it again */
1635 xmlFAGenerateCountedEpsilonTransition(ctxt, inter,
1636 atom->stop, counter);
1637 /* allow a way out based on the count */
1638 xmlFAGenerateCountedTransition(ctxt, inter,
1639 newstate, counter);
1640 /* and also allow a direct exit for 0 */
1641 xmlFAGenerateEpsilonTransition(ctxt, atom->start,
1642 newstate);
1643 } else {
1644 /*
1645 * either we need the atom at least once or there
1646 * is an atom->start0 allowing to easilly plug the
1647 * epsilon transition.
1648 */
1649 counter = xmlRegGetCounter(ctxt);
1650 ctxt->counters[counter].min = atom->min - 1;
1651 ctxt->counters[counter].max = atom->max - 1;
1652 /* count the number of times we see it again */
1653 xmlFAGenerateCountedEpsilonTransition(ctxt, atom->stop,
1654 atom->start, counter);
1655 /* allow a way out based on the count */
1656 xmlFAGenerateCountedTransition(ctxt, atom->stop,
1657 newstate, counter);
1658 /* and if needed allow a direct exit for 0 */
1659 if (atom->min == 0)
1660 xmlFAGenerateEpsilonTransition(ctxt, atom->start0,
1661 newstate);
1662
1663 }
1664 atom->min = 0;
1665 atom->max = 0;
1666 atom->quant = XML_REGEXP_QUANT_ONCE;
1667 ctxt->state = newstate;
Daniel Veillard4255d502002-04-16 15:50:10 +00001668 }
1669 default:
1670 break;
1671 }
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001672 return(0);
Daniel Veillard567a45b2005-10-18 19:11:55 +00001673 }
1674 if ((atom->min == 0) && (atom->max == 0) &&
Daniel Veillard99c394d2005-07-14 12:58:49 +00001675 (atom->quant == XML_REGEXP_QUANT_RANGE)) {
1676 /*
1677 * we can discard the atom and generate an epsilon transition instead
1678 */
1679 if (to == NULL) {
1680 to = xmlRegNewState(ctxt);
1681 if (to != NULL)
1682 xmlRegStatePush(ctxt, to);
1683 else {
1684 return(-1);
1685 }
1686 }
1687 xmlFAGenerateEpsilonTransition(ctxt, from, to);
1688 ctxt->state = to;
1689 xmlRegFreeAtom(atom);
1690 return(0);
Daniel Veillard567a45b2005-10-18 19:11:55 +00001691 }
1692 if (to == NULL) {
1693 to = xmlRegNewState(ctxt);
1694 if (to != NULL)
1695 xmlRegStatePush(ctxt, to);
1696 else {
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001697 return(-1);
Daniel Veillard4255d502002-04-16 15:50:10 +00001698 }
Daniel Veillard10bda622008-03-13 07:27:24 +00001699 }
1700 end = to;
1701 if ((atom->quant == XML_REGEXP_QUANT_MULT) ||
1702 (atom->quant == XML_REGEXP_QUANT_PLUS)) {
1703 /*
1704 * Do not pollute the target state by adding transitions from
1705 * it as it is likely to be the shared target of multiple branches.
1706 * So isolate with an epsilon transition.
1707 */
1708 xmlRegStatePtr tmp;
1709
1710 tmp = xmlRegNewState(ctxt);
1711 if (tmp != NULL)
1712 xmlRegStatePush(ctxt, tmp);
1713 else {
1714 return(-1);
1715 }
1716 xmlFAGenerateEpsilonTransition(ctxt, tmp, to);
1717 to = tmp;
Daniel Veillard4255d502002-04-16 15:50:10 +00001718 }
Daniel Veillard567a45b2005-10-18 19:11:55 +00001719 if (xmlRegAtomPush(ctxt, atom) < 0) {
1720 return(-1);
1721 }
1722 xmlRegStateAddTrans(ctxt, from, atom, to, -1, -1);
Daniel Veillard10bda622008-03-13 07:27:24 +00001723 ctxt->state = end;
Daniel Veillard4255d502002-04-16 15:50:10 +00001724 switch (atom->quant) {
1725 case XML_REGEXP_QUANT_OPT:
1726 atom->quant = XML_REGEXP_QUANT_ONCE;
1727 xmlFAGenerateEpsilonTransition(ctxt, from, to);
1728 break;
1729 case XML_REGEXP_QUANT_MULT:
1730 atom->quant = XML_REGEXP_QUANT_ONCE;
1731 xmlFAGenerateEpsilonTransition(ctxt, from, to);
Daniel Veillard5de09382005-09-26 17:18:17 +00001732 xmlRegStateAddTrans(ctxt, to, atom, to, -1, -1);
Daniel Veillard4255d502002-04-16 15:50:10 +00001733 break;
1734 case XML_REGEXP_QUANT_PLUS:
1735 atom->quant = XML_REGEXP_QUANT_ONCE;
Daniel Veillard5de09382005-09-26 17:18:17 +00001736 xmlRegStateAddTrans(ctxt, to, atom, to, -1, -1);
Daniel Veillard4255d502002-04-16 15:50:10 +00001737 break;
William M. Brack56578372007-04-11 14:33:46 +00001738 case XML_REGEXP_QUANT_RANGE:
Daniel Veillardc821e032007-08-28 17:33:45 +00001739#if DV_test
William M. Brack56578372007-04-11 14:33:46 +00001740 if (atom->min == 0) {
1741 xmlFAGenerateEpsilonTransition(ctxt, from, to);
1742 }
Daniel Veillardc821e032007-08-28 17:33:45 +00001743#endif
William M. Brack56578372007-04-11 14:33:46 +00001744 break;
Daniel Veillard4255d502002-04-16 15:50:10 +00001745 default:
1746 break;
1747 }
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001748 return(0);
Daniel Veillard4255d502002-04-16 15:50:10 +00001749}
1750
1751/**
1752 * xmlFAReduceEpsilonTransitions:
Daniel Veillard441bc322002-04-20 17:38:48 +00001753 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00001754 * @fromnr: the from state
1755 * @tonr: the to state
William M. Brackddf71d62004-05-06 04:17:26 +00001756 * @counter: should that transition be associated to a counted
Daniel Veillard4255d502002-04-16 15:50:10 +00001757 *
1758 */
1759static void
1760xmlFAReduceEpsilonTransitions(xmlRegParserCtxtPtr ctxt, int fromnr,
1761 int tonr, int counter) {
1762 int transnr;
1763 xmlRegStatePtr from;
1764 xmlRegStatePtr to;
1765
1766#ifdef DEBUG_REGEXP_GRAPH
1767 printf("xmlFAReduceEpsilonTransitions(%d, %d)\n", fromnr, tonr);
1768#endif
1769 from = ctxt->states[fromnr];
1770 if (from == NULL)
1771 return;
1772 to = ctxt->states[tonr];
1773 if (to == NULL)
1774 return;
1775 if ((to->mark == XML_REGEXP_MARK_START) ||
1776 (to->mark == XML_REGEXP_MARK_VISITED))
1777 return;
1778
1779 to->mark = XML_REGEXP_MARK_VISITED;
1780 if (to->type == XML_REGEXP_FINAL_STATE) {
1781#ifdef DEBUG_REGEXP_GRAPH
1782 printf("State %d is final, so %d becomes final\n", tonr, fromnr);
1783#endif
1784 from->type = XML_REGEXP_FINAL_STATE;
1785 }
1786 for (transnr = 0;transnr < to->nbTrans;transnr++) {
Daniel Veillarddb68b742005-07-30 13:18:24 +00001787 if (to->trans[transnr].to < 0)
1788 continue;
Daniel Veillard4255d502002-04-16 15:50:10 +00001789 if (to->trans[transnr].atom == NULL) {
1790 /*
1791 * Don't remove counted transitions
1792 * Don't loop either
1793 */
Daniel Veillardb509f152002-04-17 16:28:10 +00001794 if (to->trans[transnr].to != fromnr) {
1795 if (to->trans[transnr].count >= 0) {
1796 int newto = to->trans[transnr].to;
1797
1798 xmlRegStateAddTrans(ctxt, from, NULL,
1799 ctxt->states[newto],
Daniel Veillard5de09382005-09-26 17:18:17 +00001800 -1, to->trans[transnr].count);
Daniel Veillardb509f152002-04-17 16:28:10 +00001801 } else {
Daniel Veillard4255d502002-04-16 15:50:10 +00001802#ifdef DEBUG_REGEXP_GRAPH
Daniel Veillardb509f152002-04-17 16:28:10 +00001803 printf("Found epsilon trans %d from %d to %d\n",
1804 transnr, tonr, to->trans[transnr].to);
Daniel Veillard4255d502002-04-16 15:50:10 +00001805#endif
Daniel Veillardb509f152002-04-17 16:28:10 +00001806 if (to->trans[transnr].counter >= 0) {
1807 xmlFAReduceEpsilonTransitions(ctxt, fromnr,
1808 to->trans[transnr].to,
1809 to->trans[transnr].counter);
1810 } else {
1811 xmlFAReduceEpsilonTransitions(ctxt, fromnr,
1812 to->trans[transnr].to,
1813 counter);
1814 }
1815 }
Daniel Veillard4255d502002-04-16 15:50:10 +00001816 }
1817 } else {
1818 int newto = to->trans[transnr].to;
1819
Daniel Veillardb509f152002-04-17 16:28:10 +00001820 if (to->trans[transnr].counter >= 0) {
1821 xmlRegStateAddTrans(ctxt, from, to->trans[transnr].atom,
1822 ctxt->states[newto],
Daniel Veillard5de09382005-09-26 17:18:17 +00001823 to->trans[transnr].counter, -1);
Daniel Veillardb509f152002-04-17 16:28:10 +00001824 } else {
1825 xmlRegStateAddTrans(ctxt, from, to->trans[transnr].atom,
Daniel Veillard5de09382005-09-26 17:18:17 +00001826 ctxt->states[newto], counter, -1);
Daniel Veillardb509f152002-04-17 16:28:10 +00001827 }
Daniel Veillard4255d502002-04-16 15:50:10 +00001828 }
1829 }
1830 to->mark = XML_REGEXP_MARK_NORMAL;
1831}
1832
1833/**
Daniel Veillarddb68b742005-07-30 13:18:24 +00001834 * xmlFAEliminateSimpleEpsilonTransitions:
1835 * @ctxt: a regexp parser context
1836 *
1837 * Eliminating general epsilon transitions can get costly in the general
1838 * algorithm due to the large amount of generated new transitions and
1839 * associated comparisons. However for simple epsilon transition used just
1840 * to separate building blocks when generating the automata this can be
1841 * reduced to state elimination:
1842 * - if there exists an epsilon from X to Y
1843 * - if there is no other transition from X
1844 * then X and Y are semantically equivalent and X can be eliminated
1845 * If X is the start state then make Y the start state, else replace the
1846 * target of all transitions to X by transitions to Y.
1847 */
1848static void
1849xmlFAEliminateSimpleEpsilonTransitions(xmlRegParserCtxtPtr ctxt) {
1850 int statenr, i, j, newto;
1851 xmlRegStatePtr state, tmp;
1852
1853 for (statenr = 0;statenr < ctxt->nbStates;statenr++) {
1854 state = ctxt->states[statenr];
1855 if (state == NULL)
1856 continue;
1857 if (state->nbTrans != 1)
1858 continue;
Daniel Veillard0e05f4c2006-11-01 15:33:04 +00001859 if (state->type == XML_REGEXP_UNREACH_STATE)
1860 continue;
Daniel Veillarddb68b742005-07-30 13:18:24 +00001861 /* is the only transition out a basic transition */
1862 if ((state->trans[0].atom == NULL) &&
1863 (state->trans[0].to >= 0) &&
1864 (state->trans[0].to != statenr) &&
1865 (state->trans[0].counter < 0) &&
1866 (state->trans[0].count < 0)) {
1867 newto = state->trans[0].to;
1868
1869 if (state->type == XML_REGEXP_START_STATE) {
1870#ifdef DEBUG_REGEXP_GRAPH
1871 printf("Found simple epsilon trans from start %d to %d\n",
1872 statenr, newto);
1873#endif
1874 } else {
1875#ifdef DEBUG_REGEXP_GRAPH
1876 printf("Found simple epsilon trans from %d to %d\n",
1877 statenr, newto);
1878#endif
1879 for (i = 0;i < state->nbTransTo;i++) {
1880 tmp = ctxt->states[state->transTo[i]];
1881 for (j = 0;j < tmp->nbTrans;j++) {
1882 if (tmp->trans[j].to == statenr) {
Daniel Veillarddb68b742005-07-30 13:18:24 +00001883#ifdef DEBUG_REGEXP_GRAPH
1884 printf("Changed transition %d on %d to go to %d\n",
1885 j, tmp->no, newto);
1886#endif
Daniel Veillard0e05f4c2006-11-01 15:33:04 +00001887 tmp->trans[j].to = -1;
1888 xmlRegStateAddTrans(ctxt, tmp, tmp->trans[j].atom,
1889 ctxt->states[newto],
1890 tmp->trans[j].counter,
1891 tmp->trans[j].count);
Daniel Veillarddb68b742005-07-30 13:18:24 +00001892 }
1893 }
1894 }
Daniel Veillarddb68b742005-07-30 13:18:24 +00001895 if (state->type == XML_REGEXP_FINAL_STATE)
1896 ctxt->states[newto]->type = XML_REGEXP_FINAL_STATE;
1897 /* eliminate the transition completely */
1898 state->nbTrans = 0;
1899
Daniel Veillard0e05f4c2006-11-01 15:33:04 +00001900 state->type = XML_REGEXP_UNREACH_STATE;
Daniel Veillarddb68b742005-07-30 13:18:24 +00001901
1902 }
1903
1904 }
1905 }
1906}
1907/**
Daniel Veillard4255d502002-04-16 15:50:10 +00001908 * xmlFAEliminateEpsilonTransitions:
Daniel Veillard441bc322002-04-20 17:38:48 +00001909 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00001910 *
1911 */
1912static void
1913xmlFAEliminateEpsilonTransitions(xmlRegParserCtxtPtr ctxt) {
1914 int statenr, transnr;
1915 xmlRegStatePtr state;
Daniel Veillarddb68b742005-07-30 13:18:24 +00001916 int has_epsilon;
Daniel Veillard4255d502002-04-16 15:50:10 +00001917
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001918 if (ctxt->states == NULL) return;
1919
Daniel Veillard0e05f4c2006-11-01 15:33:04 +00001920 /*
1921 * Eliminate simple epsilon transition and the associated unreachable
1922 * states.
1923 */
Daniel Veillarddb68b742005-07-30 13:18:24 +00001924 xmlFAEliminateSimpleEpsilonTransitions(ctxt);
Daniel Veillard0e05f4c2006-11-01 15:33:04 +00001925 for (statenr = 0;statenr < ctxt->nbStates;statenr++) {
1926 state = ctxt->states[statenr];
1927 if ((state != NULL) && (state->type == XML_REGEXP_UNREACH_STATE)) {
1928#ifdef DEBUG_REGEXP_GRAPH
1929 printf("Removed unreachable state %d\n", statenr);
1930#endif
1931 xmlRegFreeState(state);
1932 ctxt->states[statenr] = NULL;
1933 }
1934 }
Daniel Veillarddb68b742005-07-30 13:18:24 +00001935
1936 has_epsilon = 0;
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001937
Daniel Veillard4255d502002-04-16 15:50:10 +00001938 /*
Daniel Veillardfcd18ff2006-11-02 10:28:04 +00001939 * Build the completed transitions bypassing the epsilons
Daniel Veillard4255d502002-04-16 15:50:10 +00001940 * Use a marking algorithm to avoid loops
Daniel Veillardfcd18ff2006-11-02 10:28:04 +00001941 * Mark sink states too.
1942 * Process from the latests states backward to the start when
1943 * there is long cascading epsilon chains this minimize the
1944 * recursions and transition compares when adding the new ones
Daniel Veillard4255d502002-04-16 15:50:10 +00001945 */
Daniel Veillardfcd18ff2006-11-02 10:28:04 +00001946 for (statenr = ctxt->nbStates - 1;statenr >= 0;statenr--) {
Daniel Veillard4255d502002-04-16 15:50:10 +00001947 state = ctxt->states[statenr];
1948 if (state == NULL)
1949 continue;
Daniel Veillardcc026dc2005-01-12 13:21:17 +00001950 if ((state->nbTrans == 0) &&
1951 (state->type != XML_REGEXP_FINAL_STATE)) {
1952 state->type = XML_REGEXP_SINK_STATE;
1953 }
Daniel Veillard4255d502002-04-16 15:50:10 +00001954 for (transnr = 0;transnr < state->nbTrans;transnr++) {
1955 if ((state->trans[transnr].atom == NULL) &&
1956 (state->trans[transnr].to >= 0)) {
1957 if (state->trans[transnr].to == statenr) {
1958 state->trans[transnr].to = -1;
1959#ifdef DEBUG_REGEXP_GRAPH
1960 printf("Removed loopback epsilon trans %d on %d\n",
1961 transnr, statenr);
1962#endif
1963 } else if (state->trans[transnr].count < 0) {
1964 int newto = state->trans[transnr].to;
1965
1966#ifdef DEBUG_REGEXP_GRAPH
1967 printf("Found epsilon trans %d from %d to %d\n",
1968 transnr, statenr, newto);
1969#endif
Daniel Veillarddb68b742005-07-30 13:18:24 +00001970 has_epsilon = 1;
Daniel Veillard0e05f4c2006-11-01 15:33:04 +00001971 state->trans[transnr].to = -2;
1972 state->mark = XML_REGEXP_MARK_START;
Daniel Veillard4255d502002-04-16 15:50:10 +00001973 xmlFAReduceEpsilonTransitions(ctxt, statenr,
1974 newto, state->trans[transnr].counter);
1975 state->mark = XML_REGEXP_MARK_NORMAL;
1976#ifdef DEBUG_REGEXP_GRAPH
1977 } else {
1978 printf("Found counted transition %d on %d\n",
1979 transnr, statenr);
1980#endif
1981 }
1982 }
1983 }
1984 }
1985 /*
1986 * Eliminate the epsilon transitions
1987 */
Daniel Veillarddb68b742005-07-30 13:18:24 +00001988 if (has_epsilon) {
1989 for (statenr = 0;statenr < ctxt->nbStates;statenr++) {
1990 state = ctxt->states[statenr];
1991 if (state == NULL)
1992 continue;
1993 for (transnr = 0;transnr < state->nbTrans;transnr++) {
1994 xmlRegTransPtr trans = &(state->trans[transnr]);
1995 if ((trans->atom == NULL) &&
1996 (trans->count < 0) &&
1997 (trans->to >= 0)) {
1998 trans->to = -1;
1999 }
Daniel Veillard4255d502002-04-16 15:50:10 +00002000 }
2001 }
2002 }
Daniel Veillard23e73572002-09-19 19:56:43 +00002003
2004 /*
2005 * Use this pass to detect unreachable states too
2006 */
2007 for (statenr = 0;statenr < ctxt->nbStates;statenr++) {
2008 state = ctxt->states[statenr];
2009 if (state != NULL)
William M. Brack779af002003-08-01 15:55:39 +00002010 state->reached = XML_REGEXP_MARK_NORMAL;
Daniel Veillard23e73572002-09-19 19:56:43 +00002011 }
2012 state = ctxt->states[0];
2013 if (state != NULL)
William M. Brack779af002003-08-01 15:55:39 +00002014 state->reached = XML_REGEXP_MARK_START;
Daniel Veillard23e73572002-09-19 19:56:43 +00002015 while (state != NULL) {
2016 xmlRegStatePtr target = NULL;
William M. Brack779af002003-08-01 15:55:39 +00002017 state->reached = XML_REGEXP_MARK_VISITED;
Daniel Veillard23e73572002-09-19 19:56:43 +00002018 /*
William M. Brackddf71d62004-05-06 04:17:26 +00002019 * Mark all states reachable from the current reachable state
Daniel Veillard23e73572002-09-19 19:56:43 +00002020 */
2021 for (transnr = 0;transnr < state->nbTrans;transnr++) {
2022 if ((state->trans[transnr].to >= 0) &&
2023 ((state->trans[transnr].atom != NULL) ||
2024 (state->trans[transnr].count >= 0))) {
2025 int newto = state->trans[transnr].to;
2026
2027 if (ctxt->states[newto] == NULL)
2028 continue;
William M. Brack779af002003-08-01 15:55:39 +00002029 if (ctxt->states[newto]->reached == XML_REGEXP_MARK_NORMAL) {
2030 ctxt->states[newto]->reached = XML_REGEXP_MARK_START;
Daniel Veillard23e73572002-09-19 19:56:43 +00002031 target = ctxt->states[newto];
2032 }
2033 }
2034 }
Daniel Veillardcc026dc2005-01-12 13:21:17 +00002035
Daniel Veillard23e73572002-09-19 19:56:43 +00002036 /*
2037 * find the next accessible state not explored
2038 */
2039 if (target == NULL) {
2040 for (statenr = 1;statenr < ctxt->nbStates;statenr++) {
2041 state = ctxt->states[statenr];
William M. Brack779af002003-08-01 15:55:39 +00002042 if ((state != NULL) && (state->reached ==
2043 XML_REGEXP_MARK_START)) {
Daniel Veillard23e73572002-09-19 19:56:43 +00002044 target = state;
2045 break;
2046 }
2047 }
2048 }
2049 state = target;
2050 }
2051 for (statenr = 0;statenr < ctxt->nbStates;statenr++) {
2052 state = ctxt->states[statenr];
William M. Brack779af002003-08-01 15:55:39 +00002053 if ((state != NULL) && (state->reached == XML_REGEXP_MARK_NORMAL)) {
Daniel Veillard23e73572002-09-19 19:56:43 +00002054#ifdef DEBUG_REGEXP_GRAPH
2055 printf("Removed unreachable state %d\n", statenr);
2056#endif
2057 xmlRegFreeState(state);
2058 ctxt->states[statenr] = NULL;
2059 }
2060 }
2061
Daniel Veillard4255d502002-04-16 15:50:10 +00002062}
2063
Daniel Veillard567a45b2005-10-18 19:11:55 +00002064static int
2065xmlFACompareRanges(xmlRegRangePtr range1, xmlRegRangePtr range2) {
2066 int ret = 0;
2067
2068 if ((range1->type == XML_REGEXP_RANGES) ||
2069 (range2->type == XML_REGEXP_RANGES) ||
2070 (range2->type == XML_REGEXP_SUBREG) ||
2071 (range1->type == XML_REGEXP_SUBREG) ||
2072 (range1->type == XML_REGEXP_STRING) ||
2073 (range2->type == XML_REGEXP_STRING))
2074 return(-1);
2075
2076 /* put them in order */
2077 if (range1->type > range2->type) {
2078 xmlRegRangePtr tmp;
2079
2080 tmp = range1;
2081 range1 = range2;
2082 range2 = tmp;
2083 }
2084 if ((range1->type == XML_REGEXP_ANYCHAR) ||
2085 (range2->type == XML_REGEXP_ANYCHAR)) {
2086 ret = 1;
2087 } else if ((range1->type == XML_REGEXP_EPSILON) ||
2088 (range2->type == XML_REGEXP_EPSILON)) {
2089 return(0);
2090 } else if (range1->type == range2->type) {
2091 if ((range1->type != XML_REGEXP_CHARVAL) ||
2092 (range1->end < range2->start) ||
2093 (range2->end < range1->start))
2094 ret = 1;
2095 else
2096 ret = 0;
2097 } else if (range1->type == XML_REGEXP_CHARVAL) {
2098 int codepoint;
2099 int neg = 0;
2100
2101 /*
2102 * just check all codepoints in the range for acceptance,
2103 * this is usually way cheaper since done only once at
2104 * compilation than testing over and over at runtime or
2105 * pushing too many states when evaluating.
2106 */
2107 if (((range1->neg == 0) && (range2->neg != 0)) ||
2108 ((range1->neg != 0) && (range2->neg == 0)))
2109 neg = 1;
2110
2111 for (codepoint = range1->start;codepoint <= range1->end ;codepoint++) {
2112 ret = xmlRegCheckCharacterRange(range2->type, codepoint,
2113 0, range2->start, range2->end,
2114 range2->blockName);
2115 if (ret < 0)
2116 return(-1);
2117 if (((neg == 1) && (ret == 0)) ||
2118 ((neg == 0) && (ret == 1)))
2119 return(1);
2120 }
2121 return(0);
2122 } else if ((range1->type == XML_REGEXP_BLOCK_NAME) ||
2123 (range2->type == XML_REGEXP_BLOCK_NAME)) {
2124 if (range1->type == range2->type) {
2125 ret = xmlStrEqual(range1->blockName, range2->blockName);
2126 } else {
2127 /*
2128 * comparing a block range with anything else is way
2129 * too costly, and maintining the table is like too much
2130 * memory too, so let's force the automata to save state
2131 * here.
2132 */
2133 return(1);
2134 }
2135 } else if ((range1->type < XML_REGEXP_LETTER) ||
2136 (range2->type < XML_REGEXP_LETTER)) {
2137 if ((range1->type == XML_REGEXP_ANYSPACE) &&
2138 (range2->type == XML_REGEXP_NOTSPACE))
2139 ret = 0;
2140 else if ((range1->type == XML_REGEXP_INITNAME) &&
2141 (range2->type == XML_REGEXP_NOTINITNAME))
2142 ret = 0;
2143 else if ((range1->type == XML_REGEXP_NAMECHAR) &&
2144 (range2->type == XML_REGEXP_NOTNAMECHAR))
2145 ret = 0;
2146 else if ((range1->type == XML_REGEXP_DECIMAL) &&
2147 (range2->type == XML_REGEXP_NOTDECIMAL))
2148 ret = 0;
2149 else if ((range1->type == XML_REGEXP_REALCHAR) &&
2150 (range2->type == XML_REGEXP_NOTREALCHAR))
2151 ret = 0;
2152 else {
2153 /* same thing to limit complexity */
2154 return(1);
2155 }
2156 } else {
2157 ret = 0;
2158 /* range1->type < range2->type here */
2159 switch (range1->type) {
2160 case XML_REGEXP_LETTER:
2161 /* all disjoint except in the subgroups */
2162 if ((range2->type == XML_REGEXP_LETTER_UPPERCASE) ||
2163 (range2->type == XML_REGEXP_LETTER_LOWERCASE) ||
2164 (range2->type == XML_REGEXP_LETTER_TITLECASE) ||
2165 (range2->type == XML_REGEXP_LETTER_MODIFIER) ||
2166 (range2->type == XML_REGEXP_LETTER_OTHERS))
2167 ret = 1;
2168 break;
2169 case XML_REGEXP_MARK:
2170 if ((range2->type == XML_REGEXP_MARK_NONSPACING) ||
2171 (range2->type == XML_REGEXP_MARK_SPACECOMBINING) ||
2172 (range2->type == XML_REGEXP_MARK_ENCLOSING))
2173 ret = 1;
2174 break;
2175 case XML_REGEXP_NUMBER:
2176 if ((range2->type == XML_REGEXP_NUMBER_DECIMAL) ||
2177 (range2->type == XML_REGEXP_NUMBER_LETTER) ||
2178 (range2->type == XML_REGEXP_NUMBER_OTHERS))
2179 ret = 1;
2180 break;
2181 case XML_REGEXP_PUNCT:
2182 if ((range2->type == XML_REGEXP_PUNCT_CONNECTOR) ||
2183 (range2->type == XML_REGEXP_PUNCT_DASH) ||
2184 (range2->type == XML_REGEXP_PUNCT_OPEN) ||
2185 (range2->type == XML_REGEXP_PUNCT_CLOSE) ||
2186 (range2->type == XML_REGEXP_PUNCT_INITQUOTE) ||
2187 (range2->type == XML_REGEXP_PUNCT_FINQUOTE) ||
2188 (range2->type == XML_REGEXP_PUNCT_OTHERS))
2189 ret = 1;
2190 break;
2191 case XML_REGEXP_SEPAR:
2192 if ((range2->type == XML_REGEXP_SEPAR_SPACE) ||
2193 (range2->type == XML_REGEXP_SEPAR_LINE) ||
2194 (range2->type == XML_REGEXP_SEPAR_PARA))
2195 ret = 1;
2196 break;
2197 case XML_REGEXP_SYMBOL:
2198 if ((range2->type == XML_REGEXP_SYMBOL_MATH) ||
2199 (range2->type == XML_REGEXP_SYMBOL_CURRENCY) ||
2200 (range2->type == XML_REGEXP_SYMBOL_MODIFIER) ||
2201 (range2->type == XML_REGEXP_SYMBOL_OTHERS))
2202 ret = 1;
2203 break;
2204 case XML_REGEXP_OTHER:
2205 if ((range2->type == XML_REGEXP_OTHER_CONTROL) ||
2206 (range2->type == XML_REGEXP_OTHER_FORMAT) ||
2207 (range2->type == XML_REGEXP_OTHER_PRIVATE))
2208 ret = 1;
2209 break;
2210 default:
2211 if ((range2->type >= XML_REGEXP_LETTER) &&
2212 (range2->type < XML_REGEXP_BLOCK_NAME))
2213 ret = 0;
2214 else {
2215 /* safety net ! */
2216 return(1);
2217 }
2218 }
2219 }
2220 if (((range1->neg == 0) && (range2->neg != 0)) ||
2221 ((range1->neg != 0) && (range2->neg == 0)))
2222 ret = !ret;
2223 return(1);
2224}
2225
Daniel Veillarde19fc232002-04-22 16:01:24 +00002226/**
Daniel Veillardfc011b72006-02-12 19:14:15 +00002227 * xmlFACompareAtomTypes:
2228 * @type1: an atom type
2229 * @type2: an atom type
2230 *
2231 * Compares two atoms type to check whether they intersect in some ways,
2232 * this is used by xmlFACompareAtoms only
2233 *
2234 * Returns 1 if they may intersect and 0 otherwise
2235 */
2236static int
2237xmlFACompareAtomTypes(xmlRegAtomType type1, xmlRegAtomType type2) {
2238 if ((type1 == XML_REGEXP_EPSILON) ||
2239 (type1 == XML_REGEXP_CHARVAL) ||
2240 (type1 == XML_REGEXP_RANGES) ||
2241 (type1 == XML_REGEXP_SUBREG) ||
2242 (type1 == XML_REGEXP_STRING) ||
2243 (type1 == XML_REGEXP_ANYCHAR))
2244 return(1);
2245 if ((type2 == XML_REGEXP_EPSILON) ||
2246 (type2 == XML_REGEXP_CHARVAL) ||
2247 (type2 == XML_REGEXP_RANGES) ||
2248 (type2 == XML_REGEXP_SUBREG) ||
2249 (type2 == XML_REGEXP_STRING) ||
2250 (type2 == XML_REGEXP_ANYCHAR))
2251 return(1);
2252
2253 if (type1 == type2) return(1);
2254
2255 /* simplify subsequent compares by making sure type1 < type2 */
2256 if (type1 > type2) {
2257 xmlRegAtomType tmp = type1;
2258 type1 = type2;
2259 type2 = tmp;
2260 }
2261 switch (type1) {
2262 case XML_REGEXP_ANYSPACE: /* \s */
2263 /* can't be a letter, number, mark, pontuation, symbol */
2264 if ((type2 == XML_REGEXP_NOTSPACE) ||
2265 ((type2 >= XML_REGEXP_LETTER) &&
2266 (type2 <= XML_REGEXP_LETTER_OTHERS)) ||
2267 ((type2 >= XML_REGEXP_NUMBER) &&
2268 (type2 <= XML_REGEXP_NUMBER_OTHERS)) ||
2269 ((type2 >= XML_REGEXP_MARK) &&
2270 (type2 <= XML_REGEXP_MARK_ENCLOSING)) ||
2271 ((type2 >= XML_REGEXP_PUNCT) &&
2272 (type2 <= XML_REGEXP_PUNCT_OTHERS)) ||
2273 ((type2 >= XML_REGEXP_SYMBOL) &&
2274 (type2 <= XML_REGEXP_SYMBOL_OTHERS))
2275 ) return(0);
2276 break;
2277 case XML_REGEXP_NOTSPACE: /* \S */
2278 break;
2279 case XML_REGEXP_INITNAME: /* \l */
2280 /* can't be a number, mark, separator, pontuation, symbol or other */
2281 if ((type2 == XML_REGEXP_NOTINITNAME) ||
2282 ((type2 >= XML_REGEXP_NUMBER) &&
2283 (type2 <= XML_REGEXP_NUMBER_OTHERS)) ||
2284 ((type2 >= XML_REGEXP_MARK) &&
2285 (type2 <= XML_REGEXP_MARK_ENCLOSING)) ||
2286 ((type2 >= XML_REGEXP_SEPAR) &&
2287 (type2 <= XML_REGEXP_SEPAR_PARA)) ||
2288 ((type2 >= XML_REGEXP_PUNCT) &&
2289 (type2 <= XML_REGEXP_PUNCT_OTHERS)) ||
2290 ((type2 >= XML_REGEXP_SYMBOL) &&
2291 (type2 <= XML_REGEXP_SYMBOL_OTHERS)) ||
2292 ((type2 >= XML_REGEXP_OTHER) &&
2293 (type2 <= XML_REGEXP_OTHER_NA))
2294 ) return(0);
2295 break;
2296 case XML_REGEXP_NOTINITNAME: /* \L */
2297 break;
2298 case XML_REGEXP_NAMECHAR: /* \c */
2299 /* can't be a mark, separator, pontuation, symbol or other */
2300 if ((type2 == XML_REGEXP_NOTNAMECHAR) ||
2301 ((type2 >= XML_REGEXP_MARK) &&
2302 (type2 <= XML_REGEXP_MARK_ENCLOSING)) ||
2303 ((type2 >= XML_REGEXP_PUNCT) &&
2304 (type2 <= XML_REGEXP_PUNCT_OTHERS)) ||
2305 ((type2 >= XML_REGEXP_SEPAR) &&
2306 (type2 <= XML_REGEXP_SEPAR_PARA)) ||
2307 ((type2 >= XML_REGEXP_SYMBOL) &&
2308 (type2 <= XML_REGEXP_SYMBOL_OTHERS)) ||
2309 ((type2 >= XML_REGEXP_OTHER) &&
2310 (type2 <= XML_REGEXP_OTHER_NA))
2311 ) return(0);
2312 break;
2313 case XML_REGEXP_NOTNAMECHAR: /* \C */
2314 break;
2315 case XML_REGEXP_DECIMAL: /* \d */
2316 /* can't be a letter, mark, separator, pontuation, symbol or other */
2317 if ((type2 == XML_REGEXP_NOTDECIMAL) ||
2318 (type2 == XML_REGEXP_REALCHAR) ||
2319 ((type2 >= XML_REGEXP_LETTER) &&
2320 (type2 <= XML_REGEXP_LETTER_OTHERS)) ||
2321 ((type2 >= XML_REGEXP_MARK) &&
2322 (type2 <= XML_REGEXP_MARK_ENCLOSING)) ||
2323 ((type2 >= XML_REGEXP_PUNCT) &&
2324 (type2 <= XML_REGEXP_PUNCT_OTHERS)) ||
2325 ((type2 >= XML_REGEXP_SEPAR) &&
2326 (type2 <= XML_REGEXP_SEPAR_PARA)) ||
2327 ((type2 >= XML_REGEXP_SYMBOL) &&
2328 (type2 <= XML_REGEXP_SYMBOL_OTHERS)) ||
2329 ((type2 >= XML_REGEXP_OTHER) &&
2330 (type2 <= XML_REGEXP_OTHER_NA))
2331 )return(0);
2332 break;
2333 case XML_REGEXP_NOTDECIMAL: /* \D */
2334 break;
2335 case XML_REGEXP_REALCHAR: /* \w */
2336 /* can't be a mark, separator, pontuation, symbol or other */
2337 if ((type2 == XML_REGEXP_NOTDECIMAL) ||
2338 ((type2 >= XML_REGEXP_MARK) &&
2339 (type2 <= XML_REGEXP_MARK_ENCLOSING)) ||
2340 ((type2 >= XML_REGEXP_PUNCT) &&
2341 (type2 <= XML_REGEXP_PUNCT_OTHERS)) ||
2342 ((type2 >= XML_REGEXP_SEPAR) &&
2343 (type2 <= XML_REGEXP_SEPAR_PARA)) ||
2344 ((type2 >= XML_REGEXP_SYMBOL) &&
2345 (type2 <= XML_REGEXP_SYMBOL_OTHERS)) ||
2346 ((type2 >= XML_REGEXP_OTHER) &&
2347 (type2 <= XML_REGEXP_OTHER_NA))
2348 )return(0);
2349 break;
2350 case XML_REGEXP_NOTREALCHAR: /* \W */
2351 break;
2352 /*
2353 * at that point we know both type 1 and type2 are from
2354 * character categories are ordered and are different,
2355 * it becomes simple because this is a partition
2356 */
2357 case XML_REGEXP_LETTER:
2358 if (type2 <= XML_REGEXP_LETTER_OTHERS)
2359 return(1);
2360 return(0);
2361 case XML_REGEXP_LETTER_UPPERCASE:
2362 case XML_REGEXP_LETTER_LOWERCASE:
2363 case XML_REGEXP_LETTER_TITLECASE:
2364 case XML_REGEXP_LETTER_MODIFIER:
2365 case XML_REGEXP_LETTER_OTHERS:
2366 return(0);
2367 case XML_REGEXP_MARK:
2368 if (type2 <= XML_REGEXP_MARK_ENCLOSING)
2369 return(1);
2370 return(0);
2371 case XML_REGEXP_MARK_NONSPACING:
2372 case XML_REGEXP_MARK_SPACECOMBINING:
2373 case XML_REGEXP_MARK_ENCLOSING:
2374 return(0);
2375 case XML_REGEXP_NUMBER:
2376 if (type2 <= XML_REGEXP_NUMBER_OTHERS)
2377 return(1);
2378 return(0);
2379 case XML_REGEXP_NUMBER_DECIMAL:
2380 case XML_REGEXP_NUMBER_LETTER:
2381 case XML_REGEXP_NUMBER_OTHERS:
2382 return(0);
2383 case XML_REGEXP_PUNCT:
2384 if (type2 <= XML_REGEXP_PUNCT_OTHERS)
2385 return(1);
2386 return(0);
2387 case XML_REGEXP_PUNCT_CONNECTOR:
2388 case XML_REGEXP_PUNCT_DASH:
2389 case XML_REGEXP_PUNCT_OPEN:
2390 case XML_REGEXP_PUNCT_CLOSE:
2391 case XML_REGEXP_PUNCT_INITQUOTE:
2392 case XML_REGEXP_PUNCT_FINQUOTE:
2393 case XML_REGEXP_PUNCT_OTHERS:
2394 return(0);
2395 case XML_REGEXP_SEPAR:
2396 if (type2 <= XML_REGEXP_SEPAR_PARA)
2397 return(1);
2398 return(0);
2399 case XML_REGEXP_SEPAR_SPACE:
2400 case XML_REGEXP_SEPAR_LINE:
2401 case XML_REGEXP_SEPAR_PARA:
2402 return(0);
2403 case XML_REGEXP_SYMBOL:
2404 if (type2 <= XML_REGEXP_SYMBOL_OTHERS)
2405 return(1);
2406 return(0);
2407 case XML_REGEXP_SYMBOL_MATH:
2408 case XML_REGEXP_SYMBOL_CURRENCY:
2409 case XML_REGEXP_SYMBOL_MODIFIER:
2410 case XML_REGEXP_SYMBOL_OTHERS:
2411 return(0);
2412 case XML_REGEXP_OTHER:
2413 if (type2 <= XML_REGEXP_OTHER_NA)
2414 return(1);
2415 return(0);
2416 case XML_REGEXP_OTHER_CONTROL:
2417 case XML_REGEXP_OTHER_FORMAT:
2418 case XML_REGEXP_OTHER_PRIVATE:
2419 case XML_REGEXP_OTHER_NA:
2420 return(0);
2421 default:
2422 break;
2423 }
2424 return(1);
2425}
2426
2427/**
2428 * xmlFAEqualAtoms:
Daniel Veillarde19fc232002-04-22 16:01:24 +00002429 * @atom1: an atom
2430 * @atom2: an atom
2431 *
Daniel Veillardfc011b72006-02-12 19:14:15 +00002432 * Compares two atoms to check whether they are the same exactly
2433 * this is used to remove equivalent transitions
Daniel Veillarde19fc232002-04-22 16:01:24 +00002434 *
Daniel Veillardfc011b72006-02-12 19:14:15 +00002435 * Returns 1 if same and 0 otherwise
Daniel Veillarde19fc232002-04-22 16:01:24 +00002436 */
2437static int
Daniel Veillardfc011b72006-02-12 19:14:15 +00002438xmlFAEqualAtoms(xmlRegAtomPtr atom1, xmlRegAtomPtr atom2) {
2439 int ret = 0;
Daniel Veillard9efc4762005-07-19 14:33:55 +00002440
Daniel Veillarde19fc232002-04-22 16:01:24 +00002441 if (atom1 == atom2)
2442 return(1);
2443 if ((atom1 == NULL) || (atom2 == NULL))
2444 return(0);
2445
Daniel Veillardfc011b72006-02-12 19:14:15 +00002446 if (atom1->type != atom2->type)
2447 return(0);
2448 switch (atom1->type) {
2449 case XML_REGEXP_EPSILON:
2450 ret = 0;
2451 break;
2452 case XML_REGEXP_STRING:
2453 ret = xmlStrEqual((xmlChar *)atom1->valuep,
2454 (xmlChar *)atom2->valuep);
2455 break;
2456 case XML_REGEXP_CHARVAL:
2457 ret = (atom1->codepoint == atom2->codepoint);
2458 break;
2459 case XML_REGEXP_RANGES:
2460 /* too hard to do in the general case */
2461 ret = 0;
2462 default:
2463 break;
2464 }
2465 return(ret);
2466}
2467
2468/**
2469 * xmlFACompareAtoms:
2470 * @atom1: an atom
2471 * @atom2: an atom
2472 *
2473 * Compares two atoms to check whether they intersect in some ways,
2474 * this is used by xmlFAComputesDeterminism and xmlFARecurseDeterminism only
2475 *
2476 * Returns 1 if yes and 0 otherwise
2477 */
2478static int
2479xmlFACompareAtoms(xmlRegAtomPtr atom1, xmlRegAtomPtr atom2) {
2480 int ret = 1;
2481
2482 if (atom1 == atom2)
2483 return(1);
2484 if ((atom1 == NULL) || (atom2 == NULL))
2485 return(0);
2486
2487 if ((atom1->type == XML_REGEXP_ANYCHAR) ||
2488 (atom2->type == XML_REGEXP_ANYCHAR))
2489 return(1);
2490
2491 if (atom1->type > atom2->type) {
Daniel Veillard567a45b2005-10-18 19:11:55 +00002492 xmlRegAtomPtr tmp;
2493 tmp = atom1;
2494 atom1 = atom2;
2495 atom2 = tmp;
Daniel Veillardfc011b72006-02-12 19:14:15 +00002496 }
2497 if (atom1->type != atom2->type) {
2498 ret = xmlFACompareAtomTypes(atom1->type, atom2->type);
2499 /* if they can't intersect at the type level break now */
2500 if (ret == 0)
2501 return(0);
Daniel Veillard567a45b2005-10-18 19:11:55 +00002502 }
Daniel Veillarde19fc232002-04-22 16:01:24 +00002503 switch (atom1->type) {
2504 case XML_REGEXP_STRING:
Daniel Veillard9efc4762005-07-19 14:33:55 +00002505 ret = xmlRegStrEqualWildcard((xmlChar *)atom1->valuep,
2506 (xmlChar *)atom2->valuep);
2507 break;
Daniel Veillarde19fc232002-04-22 16:01:24 +00002508 case XML_REGEXP_EPSILON:
Daniel Veillard567a45b2005-10-18 19:11:55 +00002509 goto not_determinist;
Daniel Veillarde19fc232002-04-22 16:01:24 +00002510 case XML_REGEXP_CHARVAL:
Daniel Veillardfc011b72006-02-12 19:14:15 +00002511 if (atom2->type == XML_REGEXP_CHARVAL) {
2512 ret = (atom1->codepoint == atom2->codepoint);
2513 } else {
2514 ret = xmlRegCheckCharacter(atom2, atom1->codepoint);
2515 if (ret < 0)
2516 ret = 1;
2517 }
Daniel Veillard9efc4762005-07-19 14:33:55 +00002518 break;
Daniel Veillarde19fc232002-04-22 16:01:24 +00002519 case XML_REGEXP_RANGES:
Daniel Veillardfc011b72006-02-12 19:14:15 +00002520 if (atom2->type == XML_REGEXP_RANGES) {
Daniel Veillard567a45b2005-10-18 19:11:55 +00002521 int i, j, res;
2522 xmlRegRangePtr r1, r2;
2523
2524 /*
2525 * need to check that none of the ranges eventually matches
2526 */
2527 for (i = 0;i < atom1->nbRanges;i++) {
2528 for (j = 0;j < atom2->nbRanges;j++) {
2529 r1 = atom1->ranges[i];
2530 r2 = atom2->ranges[j];
2531 res = xmlFACompareRanges(r1, r2);
2532 if (res == 1) {
2533 ret = 1;
2534 goto done;
2535 }
2536 }
2537 }
2538 ret = 0;
2539 }
2540 break;
Daniel Veillarde19fc232002-04-22 16:01:24 +00002541 default:
Daniel Veillard567a45b2005-10-18 19:11:55 +00002542 goto not_determinist;
Daniel Veillarde19fc232002-04-22 16:01:24 +00002543 }
Daniel Veillard567a45b2005-10-18 19:11:55 +00002544done:
Daniel Veillard6e65e152005-08-09 11:09:52 +00002545 if (atom1->neg != atom2->neg) {
Daniel Veillard9efc4762005-07-19 14:33:55 +00002546 ret = !ret;
Daniel Veillard6e65e152005-08-09 11:09:52 +00002547 }
Daniel Veillard567a45b2005-10-18 19:11:55 +00002548 if (ret == 0)
2549 return(0);
2550not_determinist:
2551 return(1);
Daniel Veillarde19fc232002-04-22 16:01:24 +00002552}
2553
2554/**
2555 * xmlFARecurseDeterminism:
2556 * @ctxt: a regexp parser context
2557 *
2558 * Check whether the associated regexp is determinist,
2559 * should be called after xmlFAEliminateEpsilonTransitions()
2560 *
2561 */
2562static int
2563xmlFARecurseDeterminism(xmlRegParserCtxtPtr ctxt, xmlRegStatePtr state,
2564 int to, xmlRegAtomPtr atom) {
2565 int ret = 1;
Daniel Veillard567a45b2005-10-18 19:11:55 +00002566 int res;
Daniel Veillard5de09382005-09-26 17:18:17 +00002567 int transnr, nbTrans;
Daniel Veillarde19fc232002-04-22 16:01:24 +00002568 xmlRegTransPtr t1;
2569
2570 if (state == NULL)
2571 return(ret);
Daniel Veillard5de09382005-09-26 17:18:17 +00002572 /*
2573 * don't recurse on transitions potentially added in the course of
2574 * the elimination.
2575 */
2576 nbTrans = state->nbTrans;
2577 for (transnr = 0;transnr < nbTrans;transnr++) {
Daniel Veillarde19fc232002-04-22 16:01:24 +00002578 t1 = &(state->trans[transnr]);
2579 /*
2580 * check transitions conflicting with the one looked at
2581 */
2582 if (t1->atom == NULL) {
Daniel Veillard0e05f4c2006-11-01 15:33:04 +00002583 if (t1->to < 0)
Daniel Veillarde19fc232002-04-22 16:01:24 +00002584 continue;
Daniel Veillard567a45b2005-10-18 19:11:55 +00002585 res = xmlFARecurseDeterminism(ctxt, ctxt->states[t1->to],
Daniel Veillarde19fc232002-04-22 16:01:24 +00002586 to, atom);
Daniel Veillard567a45b2005-10-18 19:11:55 +00002587 if (res == 0) {
2588 ret = 0;
Daniel Veillardaa622012005-10-20 15:55:25 +00002589 /* t1->nd = 1; */
Daniel Veillard567a45b2005-10-18 19:11:55 +00002590 }
Daniel Veillarde19fc232002-04-22 16:01:24 +00002591 continue;
2592 }
2593 if (t1->to != to)
2594 continue;
Daniel Veillard567a45b2005-10-18 19:11:55 +00002595 if (xmlFACompareAtoms(t1->atom, atom)) {
2596 ret = 0;
2597 /* mark the transition as non-deterministic */
2598 t1->nd = 1;
2599 }
Daniel Veillarde19fc232002-04-22 16:01:24 +00002600 }
2601 return(ret);
2602}
2603
2604/**
2605 * xmlFAComputesDeterminism:
2606 * @ctxt: a regexp parser context
2607 *
2608 * Check whether the associated regexp is determinist,
2609 * should be called after xmlFAEliminateEpsilonTransitions()
2610 *
2611 */
2612static int
2613xmlFAComputesDeterminism(xmlRegParserCtxtPtr ctxt) {
2614 int statenr, transnr;
2615 xmlRegStatePtr state;
Daniel Veillard567a45b2005-10-18 19:11:55 +00002616 xmlRegTransPtr t1, t2, last;
Daniel Veillarde19fc232002-04-22 16:01:24 +00002617 int i;
2618 int ret = 1;
2619
Daniel Veillard4402ab42002-09-12 16:02:56 +00002620#ifdef DEBUG_REGEXP_GRAPH
2621 printf("xmlFAComputesDeterminism\n");
2622 xmlRegPrintCtxt(stdout, ctxt);
2623#endif
Daniel Veillarde19fc232002-04-22 16:01:24 +00002624 if (ctxt->determinist != -1)
2625 return(ctxt->determinist);
2626
2627 /*
Daniel Veillard567a45b2005-10-18 19:11:55 +00002628 * First cleanup the automata removing cancelled transitions
Daniel Veillarde19fc232002-04-22 16:01:24 +00002629 */
2630 for (statenr = 0;statenr < ctxt->nbStates;statenr++) {
2631 state = ctxt->states[statenr];
2632 if (state == NULL)
2633 continue;
Daniel Veillard4f82c8a2005-08-09 21:40:08 +00002634 if (state->nbTrans < 2)
2635 continue;
Daniel Veillarde19fc232002-04-22 16:01:24 +00002636 for (transnr = 0;transnr < state->nbTrans;transnr++) {
2637 t1 = &(state->trans[transnr]);
2638 /*
2639 * Determinism checks in case of counted or all transitions
2640 * will have to be handled separately
2641 */
Daniel Veillard567a45b2005-10-18 19:11:55 +00002642 if (t1->atom == NULL) {
Daniel Veillardaa622012005-10-20 15:55:25 +00002643 /* t1->nd = 1; */
Daniel Veillarde19fc232002-04-22 16:01:24 +00002644 continue;
Daniel Veillard567a45b2005-10-18 19:11:55 +00002645 }
Daniel Veillarde19fc232002-04-22 16:01:24 +00002646 if (t1->to == -1) /* eliminated */
2647 continue;
2648 for (i = 0;i < transnr;i++) {
2649 t2 = &(state->trans[i]);
2650 if (t2->to == -1) /* eliminated */
2651 continue;
2652 if (t2->atom != NULL) {
2653 if (t1->to == t2->to) {
Daniel Veillard11e28e42009-08-12 12:21:42 +02002654 if (xmlFAEqualAtoms(t1->atom, t2->atom) &&
2655 (t1->counter == t2->counter) &&
2656 (t1->count == t2->count))
William M. Brackddf71d62004-05-06 04:17:26 +00002657 t2->to = -1; /* eliminated */
Daniel Veillard567a45b2005-10-18 19:11:55 +00002658 }
2659 }
2660 }
2661 }
2662 }
2663
2664 /*
2665 * Check for all states that there aren't 2 transitions
2666 * with the same atom and a different target.
2667 */
2668 for (statenr = 0;statenr < ctxt->nbStates;statenr++) {
2669 state = ctxt->states[statenr];
2670 if (state == NULL)
2671 continue;
2672 if (state->nbTrans < 2)
2673 continue;
2674 last = NULL;
2675 for (transnr = 0;transnr < state->nbTrans;transnr++) {
2676 t1 = &(state->trans[transnr]);
2677 /*
2678 * Determinism checks in case of counted or all transitions
2679 * will have to be handled separately
2680 */
2681 if (t1->atom == NULL) {
2682 continue;
2683 }
2684 if (t1->to == -1) /* eliminated */
2685 continue;
2686 for (i = 0;i < transnr;i++) {
2687 t2 = &(state->trans[i]);
2688 if (t2->to == -1) /* eliminated */
2689 continue;
2690 if (t2->atom != NULL) {
2691 /* not determinist ! */
2692 if (xmlFACompareAtoms(t1->atom, t2->atom)) {
2693 ret = 0;
2694 /* mark the transitions as non-deterministic ones */
2695 t1->nd = 1;
2696 t2->nd = 1;
2697 last = t1;
Daniel Veillarde19fc232002-04-22 16:01:24 +00002698 }
2699 } else if (t1->to != -1) {
2700 /*
2701 * do the closure in case of remaining specific
2702 * epsilon transitions like choices or all
2703 */
2704 ret = xmlFARecurseDeterminism(ctxt, ctxt->states[t1->to],
2705 t2->to, t2->atom);
Daniel Veillard567a45b2005-10-18 19:11:55 +00002706 /* don't shortcut the computation so all non deterministic
2707 transition get marked down
Daniel Veillarde19fc232002-04-22 16:01:24 +00002708 if (ret == 0)
Daniel Veillardaa622012005-10-20 15:55:25 +00002709 return(0);
2710 */
Daniel Veillard567a45b2005-10-18 19:11:55 +00002711 if (ret == 0) {
2712 t1->nd = 1;
Daniel Veillardaa622012005-10-20 15:55:25 +00002713 /* t2->nd = 1; */
Daniel Veillard567a45b2005-10-18 19:11:55 +00002714 last = t1;
2715 }
Daniel Veillarde19fc232002-04-22 16:01:24 +00002716 }
2717 }
Daniel Veillard567a45b2005-10-18 19:11:55 +00002718 /* don't shortcut the computation so all non deterministic
2719 transition get marked down
Daniel Veillarde19fc232002-04-22 16:01:24 +00002720 if (ret == 0)
Daniel Veillard567a45b2005-10-18 19:11:55 +00002721 break; */
Daniel Veillarde19fc232002-04-22 16:01:24 +00002722 }
Daniel Veillard567a45b2005-10-18 19:11:55 +00002723
2724 /*
2725 * mark specifically the last non-deterministic transition
2726 * from a state since there is no need to set-up rollback
2727 * from it
2728 */
2729 if (last != NULL) {
2730 last->nd = 2;
2731 }
2732
2733 /* don't shortcut the computation so all non deterministic
2734 transition get marked down
Daniel Veillarde19fc232002-04-22 16:01:24 +00002735 if (ret == 0)
Daniel Veillard567a45b2005-10-18 19:11:55 +00002736 break; */
Daniel Veillarde19fc232002-04-22 16:01:24 +00002737 }
Daniel Veillard567a45b2005-10-18 19:11:55 +00002738
Daniel Veillarde19fc232002-04-22 16:01:24 +00002739 ctxt->determinist = ret;
2740 return(ret);
2741}
2742
Daniel Veillard4255d502002-04-16 15:50:10 +00002743/************************************************************************
2744 * *
2745 * Routines to check input against transition atoms *
2746 * *
2747 ************************************************************************/
2748
2749static int
2750xmlRegCheckCharacterRange(xmlRegAtomType type, int codepoint, int neg,
2751 int start, int end, const xmlChar *blockName) {
2752 int ret = 0;
2753
2754 switch (type) {
2755 case XML_REGEXP_STRING:
2756 case XML_REGEXP_SUBREG:
2757 case XML_REGEXP_RANGES:
2758 case XML_REGEXP_EPSILON:
2759 return(-1);
2760 case XML_REGEXP_ANYCHAR:
2761 ret = ((codepoint != '\n') && (codepoint != '\r'));
2762 break;
2763 case XML_REGEXP_CHARVAL:
2764 ret = ((codepoint >= start) && (codepoint <= end));
2765 break;
2766 case XML_REGEXP_NOTSPACE:
2767 neg = !neg;
2768 case XML_REGEXP_ANYSPACE:
2769 ret = ((codepoint == '\n') || (codepoint == '\r') ||
2770 (codepoint == '\t') || (codepoint == ' '));
2771 break;
2772 case XML_REGEXP_NOTINITNAME:
2773 neg = !neg;
2774 case XML_REGEXP_INITNAME:
William M. Brack871611b2003-10-18 04:53:14 +00002775 ret = (IS_LETTER(codepoint) ||
Daniel Veillard4255d502002-04-16 15:50:10 +00002776 (codepoint == '_') || (codepoint == ':'));
2777 break;
2778 case XML_REGEXP_NOTNAMECHAR:
2779 neg = !neg;
2780 case XML_REGEXP_NAMECHAR:
William M. Brack871611b2003-10-18 04:53:14 +00002781 ret = (IS_LETTER(codepoint) || IS_DIGIT(codepoint) ||
Daniel Veillard4255d502002-04-16 15:50:10 +00002782 (codepoint == '.') || (codepoint == '-') ||
2783 (codepoint == '_') || (codepoint == ':') ||
William M. Brack871611b2003-10-18 04:53:14 +00002784 IS_COMBINING(codepoint) || IS_EXTENDER(codepoint));
Daniel Veillard4255d502002-04-16 15:50:10 +00002785 break;
2786 case XML_REGEXP_NOTDECIMAL:
2787 neg = !neg;
2788 case XML_REGEXP_DECIMAL:
2789 ret = xmlUCSIsCatNd(codepoint);
2790 break;
2791 case XML_REGEXP_REALCHAR:
2792 neg = !neg;
2793 case XML_REGEXP_NOTREALCHAR:
2794 ret = xmlUCSIsCatP(codepoint);
2795 if (ret == 0)
2796 ret = xmlUCSIsCatZ(codepoint);
2797 if (ret == 0)
2798 ret = xmlUCSIsCatC(codepoint);
2799 break;
2800 case XML_REGEXP_LETTER:
2801 ret = xmlUCSIsCatL(codepoint);
2802 break;
2803 case XML_REGEXP_LETTER_UPPERCASE:
2804 ret = xmlUCSIsCatLu(codepoint);
2805 break;
2806 case XML_REGEXP_LETTER_LOWERCASE:
2807 ret = xmlUCSIsCatLl(codepoint);
2808 break;
2809 case XML_REGEXP_LETTER_TITLECASE:
2810 ret = xmlUCSIsCatLt(codepoint);
2811 break;
2812 case XML_REGEXP_LETTER_MODIFIER:
2813 ret = xmlUCSIsCatLm(codepoint);
2814 break;
2815 case XML_REGEXP_LETTER_OTHERS:
2816 ret = xmlUCSIsCatLo(codepoint);
2817 break;
2818 case XML_REGEXP_MARK:
2819 ret = xmlUCSIsCatM(codepoint);
2820 break;
2821 case XML_REGEXP_MARK_NONSPACING:
2822 ret = xmlUCSIsCatMn(codepoint);
2823 break;
2824 case XML_REGEXP_MARK_SPACECOMBINING:
2825 ret = xmlUCSIsCatMc(codepoint);
2826 break;
2827 case XML_REGEXP_MARK_ENCLOSING:
2828 ret = xmlUCSIsCatMe(codepoint);
2829 break;
2830 case XML_REGEXP_NUMBER:
2831 ret = xmlUCSIsCatN(codepoint);
2832 break;
2833 case XML_REGEXP_NUMBER_DECIMAL:
2834 ret = xmlUCSIsCatNd(codepoint);
2835 break;
2836 case XML_REGEXP_NUMBER_LETTER:
2837 ret = xmlUCSIsCatNl(codepoint);
2838 break;
2839 case XML_REGEXP_NUMBER_OTHERS:
2840 ret = xmlUCSIsCatNo(codepoint);
2841 break;
2842 case XML_REGEXP_PUNCT:
2843 ret = xmlUCSIsCatP(codepoint);
2844 break;
2845 case XML_REGEXP_PUNCT_CONNECTOR:
2846 ret = xmlUCSIsCatPc(codepoint);
2847 break;
2848 case XML_REGEXP_PUNCT_DASH:
2849 ret = xmlUCSIsCatPd(codepoint);
2850 break;
2851 case XML_REGEXP_PUNCT_OPEN:
2852 ret = xmlUCSIsCatPs(codepoint);
2853 break;
2854 case XML_REGEXP_PUNCT_CLOSE:
2855 ret = xmlUCSIsCatPe(codepoint);
2856 break;
2857 case XML_REGEXP_PUNCT_INITQUOTE:
2858 ret = xmlUCSIsCatPi(codepoint);
2859 break;
2860 case XML_REGEXP_PUNCT_FINQUOTE:
2861 ret = xmlUCSIsCatPf(codepoint);
2862 break;
2863 case XML_REGEXP_PUNCT_OTHERS:
2864 ret = xmlUCSIsCatPo(codepoint);
2865 break;
2866 case XML_REGEXP_SEPAR:
2867 ret = xmlUCSIsCatZ(codepoint);
2868 break;
2869 case XML_REGEXP_SEPAR_SPACE:
2870 ret = xmlUCSIsCatZs(codepoint);
2871 break;
2872 case XML_REGEXP_SEPAR_LINE:
2873 ret = xmlUCSIsCatZl(codepoint);
2874 break;
2875 case XML_REGEXP_SEPAR_PARA:
2876 ret = xmlUCSIsCatZp(codepoint);
2877 break;
2878 case XML_REGEXP_SYMBOL:
2879 ret = xmlUCSIsCatS(codepoint);
2880 break;
2881 case XML_REGEXP_SYMBOL_MATH:
2882 ret = xmlUCSIsCatSm(codepoint);
2883 break;
2884 case XML_REGEXP_SYMBOL_CURRENCY:
2885 ret = xmlUCSIsCatSc(codepoint);
2886 break;
2887 case XML_REGEXP_SYMBOL_MODIFIER:
2888 ret = xmlUCSIsCatSk(codepoint);
2889 break;
2890 case XML_REGEXP_SYMBOL_OTHERS:
2891 ret = xmlUCSIsCatSo(codepoint);
2892 break;
2893 case XML_REGEXP_OTHER:
2894 ret = xmlUCSIsCatC(codepoint);
2895 break;
2896 case XML_REGEXP_OTHER_CONTROL:
2897 ret = xmlUCSIsCatCc(codepoint);
2898 break;
2899 case XML_REGEXP_OTHER_FORMAT:
2900 ret = xmlUCSIsCatCf(codepoint);
2901 break;
2902 case XML_REGEXP_OTHER_PRIVATE:
2903 ret = xmlUCSIsCatCo(codepoint);
2904 break;
2905 case XML_REGEXP_OTHER_NA:
2906 /* ret = xmlUCSIsCatCn(codepoint); */
2907 /* Seems it doesn't exist anymore in recent Unicode releases */
2908 ret = 0;
2909 break;
2910 case XML_REGEXP_BLOCK_NAME:
2911 ret = xmlUCSIsBlock(codepoint, (const char *) blockName);
2912 break;
2913 }
2914 if (neg)
2915 return(!ret);
2916 return(ret);
2917}
2918
2919static int
2920xmlRegCheckCharacter(xmlRegAtomPtr atom, int codepoint) {
2921 int i, ret = 0;
2922 xmlRegRangePtr range;
2923
William M. Brack871611b2003-10-18 04:53:14 +00002924 if ((atom == NULL) || (!IS_CHAR(codepoint)))
Daniel Veillard4255d502002-04-16 15:50:10 +00002925 return(-1);
2926
2927 switch (atom->type) {
2928 case XML_REGEXP_SUBREG:
2929 case XML_REGEXP_EPSILON:
2930 return(-1);
2931 case XML_REGEXP_CHARVAL:
2932 return(codepoint == atom->codepoint);
2933 case XML_REGEXP_RANGES: {
2934 int accept = 0;
Daniel Veillardf2a12832003-11-24 13:04:35 +00002935
Daniel Veillard4255d502002-04-16 15:50:10 +00002936 for (i = 0;i < atom->nbRanges;i++) {
2937 range = atom->ranges[i];
Daniel Veillardf8b9de32003-11-24 14:27:26 +00002938 if (range->neg == 2) {
Daniel Veillard4255d502002-04-16 15:50:10 +00002939 ret = xmlRegCheckCharacterRange(range->type, codepoint,
2940 0, range->start, range->end,
2941 range->blockName);
2942 if (ret != 0)
2943 return(0); /* excluded char */
Daniel Veillardf8b9de32003-11-24 14:27:26 +00002944 } else if (range->neg) {
2945 ret = xmlRegCheckCharacterRange(range->type, codepoint,
2946 0, range->start, range->end,
2947 range->blockName);
2948 if (ret == 0)
Daniel Veillardf2a12832003-11-24 13:04:35 +00002949 accept = 1;
Daniel Veillardf8b9de32003-11-24 14:27:26 +00002950 else
2951 return(0);
Daniel Veillard4255d502002-04-16 15:50:10 +00002952 } else {
2953 ret = xmlRegCheckCharacterRange(range->type, codepoint,
2954 0, range->start, range->end,
2955 range->blockName);
2956 if (ret != 0)
2957 accept = 1; /* might still be excluded */
2958 }
2959 }
2960 return(accept);
2961 }
2962 case XML_REGEXP_STRING:
2963 printf("TODO: XML_REGEXP_STRING\n");
2964 return(-1);
2965 case XML_REGEXP_ANYCHAR:
2966 case XML_REGEXP_ANYSPACE:
2967 case XML_REGEXP_NOTSPACE:
2968 case XML_REGEXP_INITNAME:
2969 case XML_REGEXP_NOTINITNAME:
2970 case XML_REGEXP_NAMECHAR:
2971 case XML_REGEXP_NOTNAMECHAR:
2972 case XML_REGEXP_DECIMAL:
2973 case XML_REGEXP_NOTDECIMAL:
2974 case XML_REGEXP_REALCHAR:
2975 case XML_REGEXP_NOTREALCHAR:
2976 case XML_REGEXP_LETTER:
2977 case XML_REGEXP_LETTER_UPPERCASE:
2978 case XML_REGEXP_LETTER_LOWERCASE:
2979 case XML_REGEXP_LETTER_TITLECASE:
2980 case XML_REGEXP_LETTER_MODIFIER:
2981 case XML_REGEXP_LETTER_OTHERS:
2982 case XML_REGEXP_MARK:
2983 case XML_REGEXP_MARK_NONSPACING:
2984 case XML_REGEXP_MARK_SPACECOMBINING:
2985 case XML_REGEXP_MARK_ENCLOSING:
2986 case XML_REGEXP_NUMBER:
2987 case XML_REGEXP_NUMBER_DECIMAL:
2988 case XML_REGEXP_NUMBER_LETTER:
2989 case XML_REGEXP_NUMBER_OTHERS:
2990 case XML_REGEXP_PUNCT:
2991 case XML_REGEXP_PUNCT_CONNECTOR:
2992 case XML_REGEXP_PUNCT_DASH:
2993 case XML_REGEXP_PUNCT_OPEN:
2994 case XML_REGEXP_PUNCT_CLOSE:
2995 case XML_REGEXP_PUNCT_INITQUOTE:
2996 case XML_REGEXP_PUNCT_FINQUOTE:
2997 case XML_REGEXP_PUNCT_OTHERS:
2998 case XML_REGEXP_SEPAR:
2999 case XML_REGEXP_SEPAR_SPACE:
3000 case XML_REGEXP_SEPAR_LINE:
3001 case XML_REGEXP_SEPAR_PARA:
3002 case XML_REGEXP_SYMBOL:
3003 case XML_REGEXP_SYMBOL_MATH:
3004 case XML_REGEXP_SYMBOL_CURRENCY:
3005 case XML_REGEXP_SYMBOL_MODIFIER:
3006 case XML_REGEXP_SYMBOL_OTHERS:
3007 case XML_REGEXP_OTHER:
3008 case XML_REGEXP_OTHER_CONTROL:
3009 case XML_REGEXP_OTHER_FORMAT:
3010 case XML_REGEXP_OTHER_PRIVATE:
3011 case XML_REGEXP_OTHER_NA:
3012 case XML_REGEXP_BLOCK_NAME:
3013 ret = xmlRegCheckCharacterRange(atom->type, codepoint, 0, 0, 0,
3014 (const xmlChar *)atom->valuep);
3015 if (atom->neg)
3016 ret = !ret;
3017 break;
3018 }
3019 return(ret);
3020}
3021
3022/************************************************************************
3023 * *
William M. Brackddf71d62004-05-06 04:17:26 +00003024 * Saving and restoring state of an execution context *
Daniel Veillard4255d502002-04-16 15:50:10 +00003025 * *
3026 ************************************************************************/
3027
3028#ifdef DEBUG_REGEXP_EXEC
3029static void
3030xmlFARegDebugExec(xmlRegExecCtxtPtr exec) {
3031 printf("state: %d:%d:idx %d", exec->state->no, exec->transno, exec->index);
3032 if (exec->inputStack != NULL) {
3033 int i;
3034 printf(": ");
3035 for (i = 0;(i < 3) && (i < exec->inputStackNr);i++)
Daniel Veillard0e05f4c2006-11-01 15:33:04 +00003036 printf("%s ", (const char *)
3037 exec->inputStack[exec->inputStackNr - (i + 1)].value);
Daniel Veillard4255d502002-04-16 15:50:10 +00003038 } else {
3039 printf(": %s", &(exec->inputString[exec->index]));
3040 }
3041 printf("\n");
3042}
3043#endif
3044
3045static void
3046xmlFARegExecSave(xmlRegExecCtxtPtr exec) {
3047#ifdef DEBUG_REGEXP_EXEC
3048 printf("saving ");
3049 exec->transno++;
3050 xmlFARegDebugExec(exec);
3051 exec->transno--;
3052#endif
Daniel Veillard94cc1032005-09-15 13:09:00 +00003053#ifdef MAX_PUSH
3054 if (exec->nbPush > MAX_PUSH) {
3055 return;
3056 }
3057 exec->nbPush++;
3058#endif
Daniel Veillard4255d502002-04-16 15:50:10 +00003059
3060 if (exec->maxRollbacks == 0) {
3061 exec->maxRollbacks = 4;
3062 exec->rollbacks = (xmlRegExecRollback *) xmlMalloc(exec->maxRollbacks *
3063 sizeof(xmlRegExecRollback));
3064 if (exec->rollbacks == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00003065 xmlRegexpErrMemory(NULL, "saving regexp");
Daniel Veillard4255d502002-04-16 15:50:10 +00003066 exec->maxRollbacks = 0;
3067 return;
3068 }
3069 memset(exec->rollbacks, 0,
3070 exec->maxRollbacks * sizeof(xmlRegExecRollback));
3071 } else if (exec->nbRollbacks >= exec->maxRollbacks) {
3072 xmlRegExecRollback *tmp;
3073 int len = exec->maxRollbacks;
3074
3075 exec->maxRollbacks *= 2;
3076 tmp = (xmlRegExecRollback *) xmlRealloc(exec->rollbacks,
3077 exec->maxRollbacks * sizeof(xmlRegExecRollback));
3078 if (tmp == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00003079 xmlRegexpErrMemory(NULL, "saving regexp");
Daniel Veillard4255d502002-04-16 15:50:10 +00003080 exec->maxRollbacks /= 2;
3081 return;
3082 }
3083 exec->rollbacks = tmp;
3084 tmp = &exec->rollbacks[len];
3085 memset(tmp, 0, (exec->maxRollbacks - len) * sizeof(xmlRegExecRollback));
3086 }
3087 exec->rollbacks[exec->nbRollbacks].state = exec->state;
3088 exec->rollbacks[exec->nbRollbacks].index = exec->index;
3089 exec->rollbacks[exec->nbRollbacks].nextbranch = exec->transno + 1;
3090 if (exec->comp->nbCounters > 0) {
3091 if (exec->rollbacks[exec->nbRollbacks].counts == NULL) {
3092 exec->rollbacks[exec->nbRollbacks].counts = (int *)
3093 xmlMalloc(exec->comp->nbCounters * sizeof(int));
3094 if (exec->rollbacks[exec->nbRollbacks].counts == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00003095 xmlRegexpErrMemory(NULL, "saving regexp");
Daniel Veillard4255d502002-04-16 15:50:10 +00003096 exec->status = -5;
3097 return;
3098 }
3099 }
3100 memcpy(exec->rollbacks[exec->nbRollbacks].counts, exec->counts,
3101 exec->comp->nbCounters * sizeof(int));
3102 }
3103 exec->nbRollbacks++;
3104}
3105
3106static void
3107xmlFARegExecRollBack(xmlRegExecCtxtPtr exec) {
3108 if (exec->nbRollbacks <= 0) {
3109 exec->status = -1;
3110#ifdef DEBUG_REGEXP_EXEC
3111 printf("rollback failed on empty stack\n");
3112#endif
3113 return;
3114 }
3115 exec->nbRollbacks--;
3116 exec->state = exec->rollbacks[exec->nbRollbacks].state;
3117 exec->index = exec->rollbacks[exec->nbRollbacks].index;
3118 exec->transno = exec->rollbacks[exec->nbRollbacks].nextbranch;
3119 if (exec->comp->nbCounters > 0) {
3120 if (exec->rollbacks[exec->nbRollbacks].counts == NULL) {
3121 fprintf(stderr, "exec save: allocation failed");
3122 exec->status = -6;
3123 return;
3124 }
3125 memcpy(exec->counts, exec->rollbacks[exec->nbRollbacks].counts,
3126 exec->comp->nbCounters * sizeof(int));
3127 }
3128
3129#ifdef DEBUG_REGEXP_EXEC
3130 printf("restored ");
3131 xmlFARegDebugExec(exec);
3132#endif
3133}
3134
3135/************************************************************************
3136 * *
William M. Brackddf71d62004-05-06 04:17:26 +00003137 * Verifier, running an input against a compiled regexp *
Daniel Veillard4255d502002-04-16 15:50:10 +00003138 * *
3139 ************************************************************************/
3140
3141static int
3142xmlFARegExec(xmlRegexpPtr comp, const xmlChar *content) {
3143 xmlRegExecCtxt execval;
3144 xmlRegExecCtxtPtr exec = &execval;
Daniel Veillard567a45b2005-10-18 19:11:55 +00003145 int ret, codepoint = 0, len, deter;
Daniel Veillard4255d502002-04-16 15:50:10 +00003146
3147 exec->inputString = content;
3148 exec->index = 0;
Daniel Veillard94cc1032005-09-15 13:09:00 +00003149 exec->nbPush = 0;
Daniel Veillard4255d502002-04-16 15:50:10 +00003150 exec->determinist = 1;
3151 exec->maxRollbacks = 0;
3152 exec->nbRollbacks = 0;
3153 exec->rollbacks = NULL;
3154 exec->status = 0;
3155 exec->comp = comp;
3156 exec->state = comp->states[0];
3157 exec->transno = 0;
3158 exec->transcount = 0;
Daniel Veillardf2a12832003-11-24 13:04:35 +00003159 exec->inputStack = NULL;
3160 exec->inputStackMax = 0;
Daniel Veillard4255d502002-04-16 15:50:10 +00003161 if (comp->nbCounters > 0) {
3162 exec->counts = (int *) xmlMalloc(comp->nbCounters * sizeof(int));
Daniel Veillardff46a042003-10-08 08:53:17 +00003163 if (exec->counts == NULL) {
3164 xmlRegexpErrMemory(NULL, "running regexp");
Daniel Veillard4255d502002-04-16 15:50:10 +00003165 return(-1);
Daniel Veillardff46a042003-10-08 08:53:17 +00003166 }
Daniel Veillard4255d502002-04-16 15:50:10 +00003167 memset(exec->counts, 0, comp->nbCounters * sizeof(int));
3168 } else
3169 exec->counts = NULL;
3170 while ((exec->status == 0) &&
3171 ((exec->inputString[exec->index] != 0) ||
Daniel Veillardad559982008-05-12 13:15:35 +00003172 ((exec->state != NULL) &&
3173 (exec->state->type != XML_REGEXP_FINAL_STATE)))) {
Daniel Veillard4255d502002-04-16 15:50:10 +00003174 xmlRegTransPtr trans;
3175 xmlRegAtomPtr atom;
3176
3177 /*
William M. Brack0e00b282004-04-26 15:40:47 +00003178 * If end of input on non-terminal state, rollback, however we may
Daniel Veillard4255d502002-04-16 15:50:10 +00003179 * still have epsilon like transition for counted transitions
William M. Brack0e00b282004-04-26 15:40:47 +00003180 * on counters, in that case don't break too early. Additionally,
3181 * if we are working on a range like "AB{0,2}", where B is not present,
3182 * we don't want to break.
Daniel Veillard4255d502002-04-16 15:50:10 +00003183 */
Daniel Veillard11ce4002006-03-10 00:36:23 +00003184 len = 1;
William M. Brack0e00b282004-04-26 15:40:47 +00003185 if ((exec->inputString[exec->index] == 0) && (exec->counts == NULL)) {
William M. Brackddf71d62004-05-06 04:17:26 +00003186 /*
3187 * if there is a transition, we must check if
3188 * atom allows minOccurs of 0
3189 */
3190 if (exec->transno < exec->state->nbTrans) {
William M. Brack0e00b282004-04-26 15:40:47 +00003191 trans = &exec->state->trans[exec->transno];
3192 if (trans->to >=0) {
3193 atom = trans->atom;
3194 if (!((atom->min == 0) && (atom->max > 0)))
3195 goto rollback;
3196 }
3197 } else
3198 goto rollback;
3199 }
Daniel Veillard4255d502002-04-16 15:50:10 +00003200
3201 exec->transcount = 0;
3202 for (;exec->transno < exec->state->nbTrans;exec->transno++) {
3203 trans = &exec->state->trans[exec->transno];
3204 if (trans->to < 0)
3205 continue;
3206 atom = trans->atom;
3207 ret = 0;
Daniel Veillard567a45b2005-10-18 19:11:55 +00003208 deter = 1;
Daniel Veillard4255d502002-04-16 15:50:10 +00003209 if (trans->count >= 0) {
3210 int count;
3211 xmlRegCounterPtr counter;
3212
Daniel Veillard11ce4002006-03-10 00:36:23 +00003213 if (exec->counts == NULL) {
3214 exec->status = -1;
3215 goto error;
3216 }
Daniel Veillard4255d502002-04-16 15:50:10 +00003217 /*
3218 * A counted transition.
3219 */
3220
3221 count = exec->counts[trans->count];
3222 counter = &exec->comp->counters[trans->count];
3223#ifdef DEBUG_REGEXP_EXEC
3224 printf("testing count %d: val %d, min %d, max %d\n",
3225 trans->count, count, counter->min, counter->max);
3226#endif
3227 ret = ((count >= counter->min) && (count <= counter->max));
Daniel Veillard567a45b2005-10-18 19:11:55 +00003228 if ((ret) && (counter->min != counter->max))
3229 deter = 0;
Daniel Veillard4255d502002-04-16 15:50:10 +00003230 } else if (atom == NULL) {
3231 fprintf(stderr, "epsilon transition left at runtime\n");
3232 exec->status = -2;
3233 break;
3234 } else if (exec->inputString[exec->index] != 0) {
3235 codepoint = CUR_SCHAR(&(exec->inputString[exec->index]), len);
3236 ret = xmlRegCheckCharacter(atom, codepoint);
William M. Brack0e00b282004-04-26 15:40:47 +00003237 if ((ret == 1) && (atom->min >= 0) && (atom->max > 0)) {
Daniel Veillard4255d502002-04-16 15:50:10 +00003238 xmlRegStatePtr to = comp->states[trans->to];
3239
3240 /*
3241 * this is a multiple input sequence
Daniel Veillardfc6eca02005-11-01 15:24:02 +00003242 * If there is a counter associated increment it now.
3243 * before potentially saving and rollback
Daniel Veillardc821e032007-08-28 17:33:45 +00003244 * do not increment if the counter is already over the
3245 * maximum limit in which case get to next transition
Daniel Veillard4255d502002-04-16 15:50:10 +00003246 */
Daniel Veillardfc6eca02005-11-01 15:24:02 +00003247 if (trans->counter >= 0) {
Daniel Veillardc821e032007-08-28 17:33:45 +00003248 xmlRegCounterPtr counter;
3249
3250 if ((exec->counts == NULL) ||
3251 (exec->comp == NULL) ||
3252 (exec->comp->counters == NULL)) {
Daniel Veillard11ce4002006-03-10 00:36:23 +00003253 exec->status = -1;
3254 goto error;
3255 }
Daniel Veillardc821e032007-08-28 17:33:45 +00003256 counter = &exec->comp->counters[trans->counter];
3257 if (exec->counts[trans->counter] >= counter->max)
3258 continue; /* for loop on transitions */
3259
Daniel Veillardfc6eca02005-11-01 15:24:02 +00003260#ifdef DEBUG_REGEXP_EXEC
3261 printf("Increasing count %d\n", trans->counter);
3262#endif
3263 exec->counts[trans->counter]++;
3264 }
Daniel Veillard4255d502002-04-16 15:50:10 +00003265 if (exec->state->nbTrans > exec->transno + 1) {
3266 xmlFARegExecSave(exec);
3267 }
3268 exec->transcount = 1;
3269 do {
3270 /*
3271 * Try to progress as much as possible on the input
3272 */
3273 if (exec->transcount == atom->max) {
3274 break;
3275 }
3276 exec->index += len;
3277 /*
3278 * End of input: stop here
3279 */
3280 if (exec->inputString[exec->index] == 0) {
3281 exec->index -= len;
3282 break;
3283 }
3284 if (exec->transcount >= atom->min) {
3285 int transno = exec->transno;
3286 xmlRegStatePtr state = exec->state;
3287
3288 /*
3289 * The transition is acceptable save it
3290 */
3291 exec->transno = -1; /* trick */
3292 exec->state = to;
3293 xmlFARegExecSave(exec);
3294 exec->transno = transno;
3295 exec->state = state;
3296 }
3297 codepoint = CUR_SCHAR(&(exec->inputString[exec->index]),
3298 len);
3299 ret = xmlRegCheckCharacter(atom, codepoint);
3300 exec->transcount++;
3301 } while (ret == 1);
3302 if (exec->transcount < atom->min)
3303 ret = 0;
3304
3305 /*
3306 * If the last check failed but one transition was found
3307 * possible, rollback
3308 */
3309 if (ret < 0)
3310 ret = 0;
3311 if (ret == 0) {
3312 goto rollback;
3313 }
Daniel Veillardfc6eca02005-11-01 15:24:02 +00003314 if (trans->counter >= 0) {
Daniel Veillard11ce4002006-03-10 00:36:23 +00003315 if (exec->counts == NULL) {
3316 exec->status = -1;
3317 goto error;
3318 }
Daniel Veillardfc6eca02005-11-01 15:24:02 +00003319#ifdef DEBUG_REGEXP_EXEC
3320 printf("Decreasing count %d\n", trans->counter);
3321#endif
3322 exec->counts[trans->counter]--;
3323 }
William M. Brack0e00b282004-04-26 15:40:47 +00003324 } else if ((ret == 0) && (atom->min == 0) && (atom->max > 0)) {
3325 /*
3326 * we don't match on the codepoint, but minOccurs of 0
3327 * says that's ok. Setting len to 0 inhibits stepping
3328 * over the codepoint.
3329 */
3330 exec->transcount = 1;
3331 len = 0;
3332 ret = 1;
Daniel Veillard4255d502002-04-16 15:50:10 +00003333 }
William M. Brack0e00b282004-04-26 15:40:47 +00003334 } else if ((atom->min == 0) && (atom->max > 0)) {
3335 /* another spot to match when minOccurs is 0 */
3336 exec->transcount = 1;
3337 len = 0;
3338 ret = 1;
Daniel Veillard4255d502002-04-16 15:50:10 +00003339 }
3340 if (ret == 1) {
Daniel Veillard567a45b2005-10-18 19:11:55 +00003341 if ((trans->nd == 1) ||
3342 ((trans->count >= 0) && (deter == 0) &&
3343 (exec->state->nbTrans > exec->transno + 1))) {
Daniel Veillardaa622012005-10-20 15:55:25 +00003344#ifdef DEBUG_REGEXP_EXEC
3345 if (trans->nd == 1)
3346 printf("Saving on nd transition atom %d for %c at %d\n",
3347 trans->atom->no, codepoint, exec->index);
3348 else
3349 printf("Saving on counted transition count %d for %c at %d\n",
3350 trans->count, codepoint, exec->index);
3351#endif
Daniel Veillard4255d502002-04-16 15:50:10 +00003352 xmlFARegExecSave(exec);
3353 }
3354 if (trans->counter >= 0) {
Daniel Veillardc821e032007-08-28 17:33:45 +00003355 xmlRegCounterPtr counter;
3356
3357 /* make sure we don't go over the counter maximum value */
3358 if ((exec->counts == NULL) ||
3359 (exec->comp == NULL) ||
3360 (exec->comp->counters == NULL)) {
3361 exec->status = -1;
Daniel Veillard11ce4002006-03-10 00:36:23 +00003362 goto error;
3363 }
Daniel Veillardc821e032007-08-28 17:33:45 +00003364 counter = &exec->comp->counters[trans->counter];
3365 if (exec->counts[trans->counter] >= counter->max)
3366 continue; /* for loop on transitions */
Daniel Veillard4255d502002-04-16 15:50:10 +00003367#ifdef DEBUG_REGEXP_EXEC
3368 printf("Increasing count %d\n", trans->counter);
3369#endif
3370 exec->counts[trans->counter]++;
3371 }
Daniel Veillard10752282005-08-08 13:05:13 +00003372 if ((trans->count >= 0) &&
3373 (trans->count < REGEXP_ALL_COUNTER)) {
Daniel Veillard11ce4002006-03-10 00:36:23 +00003374 if (exec->counts == NULL) {
3375 exec->status = -1;
3376 goto error;
3377 }
Daniel Veillard10752282005-08-08 13:05:13 +00003378#ifdef DEBUG_REGEXP_EXEC
3379 printf("resetting count %d on transition\n",
3380 trans->count);
3381#endif
3382 exec->counts[trans->count] = 0;
3383 }
Daniel Veillard4255d502002-04-16 15:50:10 +00003384#ifdef DEBUG_REGEXP_EXEC
3385 printf("entering state %d\n", trans->to);
3386#endif
3387 exec->state = comp->states[trans->to];
3388 exec->transno = 0;
3389 if (trans->atom != NULL) {
3390 exec->index += len;
3391 }
3392 goto progress;
3393 } else if (ret < 0) {
3394 exec->status = -4;
3395 break;
3396 }
3397 }
3398 if ((exec->transno != 0) || (exec->state->nbTrans == 0)) {
3399rollback:
3400 /*
3401 * Failed to find a way out
3402 */
3403 exec->determinist = 0;
Daniel Veillardaa622012005-10-20 15:55:25 +00003404#ifdef DEBUG_REGEXP_EXEC
3405 printf("rollback from state %d on %d:%c\n", exec->state->no,
3406 codepoint,codepoint);
3407#endif
Daniel Veillard4255d502002-04-16 15:50:10 +00003408 xmlFARegExecRollBack(exec);
3409 }
3410progress:
3411 continue;
3412 }
Daniel Veillard11ce4002006-03-10 00:36:23 +00003413error:
Daniel Veillard4255d502002-04-16 15:50:10 +00003414 if (exec->rollbacks != NULL) {
3415 if (exec->counts != NULL) {
3416 int i;
3417
3418 for (i = 0;i < exec->maxRollbacks;i++)
3419 if (exec->rollbacks[i].counts != NULL)
3420 xmlFree(exec->rollbacks[i].counts);
3421 }
3422 xmlFree(exec->rollbacks);
3423 }
3424 if (exec->counts != NULL)
3425 xmlFree(exec->counts);
3426 if (exec->status == 0)
3427 return(1);
Daniel Veillard94cc1032005-09-15 13:09:00 +00003428 if (exec->status == -1) {
3429 if (exec->nbPush > MAX_PUSH)
3430 return(-1);
Daniel Veillard4255d502002-04-16 15:50:10 +00003431 return(0);
Daniel Veillard94cc1032005-09-15 13:09:00 +00003432 }
Daniel Veillard4255d502002-04-16 15:50:10 +00003433 return(exec->status);
3434}
3435
3436/************************************************************************
3437 * *
William M. Brackddf71d62004-05-06 04:17:26 +00003438 * Progressive interface to the verifier one atom at a time *
Daniel Veillard4255d502002-04-16 15:50:10 +00003439 * *
3440 ************************************************************************/
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003441#ifdef DEBUG_ERR
3442static void testerr(xmlRegExecCtxtPtr exec);
3443#endif
Daniel Veillard4255d502002-04-16 15:50:10 +00003444
3445/**
Daniel Veillard01c13b52002-12-10 15:19:08 +00003446 * xmlRegNewExecCtxt:
Daniel Veillard4255d502002-04-16 15:50:10 +00003447 * @comp: a precompiled regular expression
3448 * @callback: a callback function used for handling progresses in the
3449 * automata matching phase
3450 * @data: the context data associated to the callback in this context
3451 *
3452 * Build a context used for progressive evaluation of a regexp.
Daniel Veillard01c13b52002-12-10 15:19:08 +00003453 *
3454 * Returns the new context
Daniel Veillard4255d502002-04-16 15:50:10 +00003455 */
3456xmlRegExecCtxtPtr
3457xmlRegNewExecCtxt(xmlRegexpPtr comp, xmlRegExecCallbacks callback, void *data) {
3458 xmlRegExecCtxtPtr exec;
3459
3460 if (comp == NULL)
3461 return(NULL);
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00003462 if ((comp->compact == NULL) && (comp->states == NULL))
3463 return(NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +00003464 exec = (xmlRegExecCtxtPtr) xmlMalloc(sizeof(xmlRegExecCtxt));
3465 if (exec == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00003466 xmlRegexpErrMemory(NULL, "creating execution context");
Daniel Veillard4255d502002-04-16 15:50:10 +00003467 return(NULL);
3468 }
3469 memset(exec, 0, sizeof(xmlRegExecCtxt));
3470 exec->inputString = NULL;
3471 exec->index = 0;
3472 exec->determinist = 1;
3473 exec->maxRollbacks = 0;
3474 exec->nbRollbacks = 0;
3475 exec->rollbacks = NULL;
3476 exec->status = 0;
3477 exec->comp = comp;
Daniel Veillard23e73572002-09-19 19:56:43 +00003478 if (comp->compact == NULL)
3479 exec->state = comp->states[0];
Daniel Veillard4255d502002-04-16 15:50:10 +00003480 exec->transno = 0;
3481 exec->transcount = 0;
3482 exec->callback = callback;
3483 exec->data = data;
3484 if (comp->nbCounters > 0) {
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003485 /*
3486 * For error handling, exec->counts is allocated twice the size
3487 * the second half is used to store the data in case of rollback
3488 */
3489 exec->counts = (int *) xmlMalloc(comp->nbCounters * sizeof(int)
3490 * 2);
Daniel Veillard4255d502002-04-16 15:50:10 +00003491 if (exec->counts == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00003492 xmlRegexpErrMemory(NULL, "creating execution context");
Daniel Veillard4255d502002-04-16 15:50:10 +00003493 xmlFree(exec);
3494 return(NULL);
3495 }
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003496 memset(exec->counts, 0, comp->nbCounters * sizeof(int) * 2);
3497 exec->errCounts = &exec->counts[comp->nbCounters];
3498 } else {
Daniel Veillard4255d502002-04-16 15:50:10 +00003499 exec->counts = NULL;
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003500 exec->errCounts = NULL;
3501 }
Daniel Veillard4255d502002-04-16 15:50:10 +00003502 exec->inputStackMax = 0;
3503 exec->inputStackNr = 0;
3504 exec->inputStack = NULL;
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003505 exec->errStateNo = -1;
3506 exec->errString = NULL;
Daniel Veillard94cc1032005-09-15 13:09:00 +00003507 exec->nbPush = 0;
Daniel Veillard4255d502002-04-16 15:50:10 +00003508 return(exec);
3509}
3510
3511/**
3512 * xmlRegFreeExecCtxt:
3513 * @exec: a regular expression evaulation context
3514 *
3515 * Free the structures associated to a regular expression evaulation context.
3516 */
3517void
3518xmlRegFreeExecCtxt(xmlRegExecCtxtPtr exec) {
3519 if (exec == NULL)
3520 return;
3521
3522 if (exec->rollbacks != NULL) {
3523 if (exec->counts != NULL) {
3524 int i;
3525
3526 for (i = 0;i < exec->maxRollbacks;i++)
3527 if (exec->rollbacks[i].counts != NULL)
3528 xmlFree(exec->rollbacks[i].counts);
3529 }
3530 xmlFree(exec->rollbacks);
3531 }
3532 if (exec->counts != NULL)
3533 xmlFree(exec->counts);
3534 if (exec->inputStack != NULL) {
3535 int i;
3536
Daniel Veillard32370232002-10-16 14:08:14 +00003537 for (i = 0;i < exec->inputStackNr;i++) {
3538 if (exec->inputStack[i].value != NULL)
3539 xmlFree(exec->inputStack[i].value);
3540 }
Daniel Veillard4255d502002-04-16 15:50:10 +00003541 xmlFree(exec->inputStack);
3542 }
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003543 if (exec->errString != NULL)
3544 xmlFree(exec->errString);
Daniel Veillard4255d502002-04-16 15:50:10 +00003545 xmlFree(exec);
3546}
3547
3548static void
3549xmlFARegExecSaveInputString(xmlRegExecCtxtPtr exec, const xmlChar *value,
3550 void *data) {
3551#ifdef DEBUG_PUSH
3552 printf("saving value: %d:%s\n", exec->inputStackNr, value);
3553#endif
3554 if (exec->inputStackMax == 0) {
3555 exec->inputStackMax = 4;
3556 exec->inputStack = (xmlRegInputTokenPtr)
3557 xmlMalloc(exec->inputStackMax * sizeof(xmlRegInputToken));
3558 if (exec->inputStack == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00003559 xmlRegexpErrMemory(NULL, "pushing input string");
Daniel Veillard4255d502002-04-16 15:50:10 +00003560 exec->inputStackMax = 0;
3561 return;
3562 }
3563 } else if (exec->inputStackNr + 1 >= exec->inputStackMax) {
3564 xmlRegInputTokenPtr tmp;
3565
3566 exec->inputStackMax *= 2;
3567 tmp = (xmlRegInputTokenPtr) xmlRealloc(exec->inputStack,
3568 exec->inputStackMax * sizeof(xmlRegInputToken));
3569 if (tmp == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00003570 xmlRegexpErrMemory(NULL, "pushing input string");
Daniel Veillard4255d502002-04-16 15:50:10 +00003571 exec->inputStackMax /= 2;
3572 return;
3573 }
3574 exec->inputStack = tmp;
3575 }
3576 exec->inputStack[exec->inputStackNr].value = xmlStrdup(value);
3577 exec->inputStack[exec->inputStackNr].data = data;
3578 exec->inputStackNr++;
3579 exec->inputStack[exec->inputStackNr].value = NULL;
3580 exec->inputStack[exec->inputStackNr].data = NULL;
3581}
3582
Daniel Veillardc0826a72004-08-10 14:17:33 +00003583/**
3584 * xmlRegStrEqualWildcard:
3585 * @expStr: the string to be evaluated
3586 * @valStr: the validation string
3587 *
3588 * Checks if both strings are equal or have the same content. "*"
3589 * can be used as a wildcard in @valStr; "|" is used as a seperator of
3590 * substrings in both @expStr and @valStr.
3591 *
3592 * Returns 1 if the comparison is satisfied and the number of substrings
3593 * is equal, 0 otherwise.
3594 */
3595
3596static int
3597xmlRegStrEqualWildcard(const xmlChar *expStr, const xmlChar *valStr) {
3598 if (expStr == valStr) return(1);
3599 if (expStr == NULL) return(0);
3600 if (valStr == NULL) return(0);
3601 do {
3602 /*
3603 * Eval if we have a wildcard for the current item.
3604 */
3605 if (*expStr != *valStr) {
Daniel Veillard4f82c8a2005-08-09 21:40:08 +00003606 /* if one of them starts with a wildcard make valStr be it */
3607 if (*valStr == '*') {
3608 const xmlChar *tmp;
3609
3610 tmp = valStr;
3611 valStr = expStr;
3612 expStr = tmp;
3613 }
Daniel Veillardc0826a72004-08-10 14:17:33 +00003614 if ((*valStr != 0) && (*expStr != 0) && (*expStr++ == '*')) {
3615 do {
3616 if (*valStr == XML_REG_STRING_SEPARATOR)
3617 break;
Kasimier T. Buchcikc0e833f2005-04-19 15:02:20 +00003618 valStr++;
Daniel Veillardc0826a72004-08-10 14:17:33 +00003619 } while (*valStr != 0);
3620 continue;
3621 } else
3622 return(0);
3623 }
Kasimier T. Buchcikc0e833f2005-04-19 15:02:20 +00003624 expStr++;
3625 valStr++;
Daniel Veillardc0826a72004-08-10 14:17:33 +00003626 } while (*valStr != 0);
3627 if (*expStr != 0)
3628 return (0);
3629 else
3630 return (1);
3631}
Daniel Veillard4255d502002-04-16 15:50:10 +00003632
3633/**
Daniel Veillard23e73572002-09-19 19:56:43 +00003634 * xmlRegCompactPushString:
3635 * @exec: a regexp execution context
3636 * @comp: the precompiled exec with a compact table
3637 * @value: a string token input
3638 * @data: data associated to the token to reuse in callbacks
3639 *
3640 * Push one input token in the execution context
3641 *
3642 * Returns: 1 if the regexp reached a final state, 0 if non-final, and
3643 * a negative value in case of error.
3644 */
3645static int
3646xmlRegCompactPushString(xmlRegExecCtxtPtr exec,
3647 xmlRegexpPtr comp,
3648 const xmlChar *value,
3649 void *data) {
3650 int state = exec->index;
3651 int i, target;
3652
3653 if ((comp == NULL) || (comp->compact == NULL) || (comp->stringMap == NULL))
3654 return(-1);
3655
3656 if (value == NULL) {
3657 /*
3658 * are we at a final state ?
3659 */
3660 if (comp->compact[state * (comp->nbstrings + 1)] ==
3661 XML_REGEXP_FINAL_STATE)
3662 return(1);
3663 return(0);
3664 }
3665
3666#ifdef DEBUG_PUSH
3667 printf("value pushed: %s\n", value);
3668#endif
3669
3670 /*
William M. Brackddf71d62004-05-06 04:17:26 +00003671 * Examine all outside transitions from current state
Daniel Veillard23e73572002-09-19 19:56:43 +00003672 */
3673 for (i = 0;i < comp->nbstrings;i++) {
3674 target = comp->compact[state * (comp->nbstrings + 1) + i + 1];
3675 if ((target > 0) && (target <= comp->nbstates)) {
Daniel Veillardc0826a72004-08-10 14:17:33 +00003676 target--; /* to avoid 0 */
3677 if (xmlRegStrEqualWildcard(comp->stringMap[i], value)) {
3678 exec->index = target;
Daniel Veillard118aed72002-09-24 14:13:13 +00003679 if ((exec->callback != NULL) && (comp->transdata != NULL)) {
3680 exec->callback(exec->data, value,
3681 comp->transdata[state * comp->nbstrings + i], data);
3682 }
Daniel Veillard23e73572002-09-19 19:56:43 +00003683#ifdef DEBUG_PUSH
3684 printf("entering state %d\n", target);
3685#endif
3686 if (comp->compact[target * (comp->nbstrings + 1)] ==
Daniel Veillardcc026dc2005-01-12 13:21:17 +00003687 XML_REGEXP_SINK_STATE)
3688 goto error;
3689
3690 if (comp->compact[target * (comp->nbstrings + 1)] ==
Daniel Veillard23e73572002-09-19 19:56:43 +00003691 XML_REGEXP_FINAL_STATE)
3692 return(1);
3693 return(0);
3694 }
3695 }
3696 }
3697 /*
3698 * Failed to find an exit transition out from current state for the
3699 * current token
3700 */
3701#ifdef DEBUG_PUSH
3702 printf("failed to find a transition for %s on state %d\n", value, state);
3703#endif
Daniel Veillardcc026dc2005-01-12 13:21:17 +00003704error:
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003705 if (exec->errString != NULL)
3706 xmlFree(exec->errString);
3707 exec->errString = xmlStrdup(value);
3708 exec->errStateNo = state;
Daniel Veillard23e73572002-09-19 19:56:43 +00003709 exec->status = -1;
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003710#ifdef DEBUG_ERR
3711 testerr(exec);
3712#endif
Daniel Veillard23e73572002-09-19 19:56:43 +00003713 return(-1);
3714}
3715
3716/**
Daniel Veillard6e65e152005-08-09 11:09:52 +00003717 * xmlRegExecPushStringInternal:
Daniel Veillardea7751d2002-12-20 00:16:24 +00003718 * @exec: a regexp execution context or NULL to indicate the end
Daniel Veillard4255d502002-04-16 15:50:10 +00003719 * @value: a string token input
3720 * @data: data associated to the token to reuse in callbacks
Daniel Veillard6e65e152005-08-09 11:09:52 +00003721 * @compound: value was assembled from 2 strings
Daniel Veillard4255d502002-04-16 15:50:10 +00003722 *
3723 * Push one input token in the execution context
3724 *
3725 * Returns: 1 if the regexp reached a final state, 0 if non-final, and
3726 * a negative value in case of error.
3727 */
Daniel Veillard6e65e152005-08-09 11:09:52 +00003728static int
3729xmlRegExecPushStringInternal(xmlRegExecCtxtPtr exec, const xmlChar *value,
3730 void *data, int compound) {
Daniel Veillard4255d502002-04-16 15:50:10 +00003731 xmlRegTransPtr trans;
3732 xmlRegAtomPtr atom;
3733 int ret;
3734 int final = 0;
Daniel Veillard90700152005-01-08 22:05:09 +00003735 int progress = 1;
Daniel Veillard4255d502002-04-16 15:50:10 +00003736
3737 if (exec == NULL)
3738 return(-1);
Daniel Veillard23e73572002-09-19 19:56:43 +00003739 if (exec->comp == NULL)
3740 return(-1);
Daniel Veillard4255d502002-04-16 15:50:10 +00003741 if (exec->status != 0)
3742 return(exec->status);
3743
Daniel Veillard23e73572002-09-19 19:56:43 +00003744 if (exec->comp->compact != NULL)
3745 return(xmlRegCompactPushString(exec, exec->comp, value, data));
3746
Daniel Veillard4255d502002-04-16 15:50:10 +00003747 if (value == NULL) {
3748 if (exec->state->type == XML_REGEXP_FINAL_STATE)
3749 return(1);
3750 final = 1;
3751 }
3752
3753#ifdef DEBUG_PUSH
3754 printf("value pushed: %s\n", value);
3755#endif
3756 /*
3757 * If we have an active rollback stack push the new value there
3758 * and get back to where we were left
3759 */
3760 if ((value != NULL) && (exec->inputStackNr > 0)) {
3761 xmlFARegExecSaveInputString(exec, value, data);
3762 value = exec->inputStack[exec->index].value;
3763 data = exec->inputStack[exec->index].data;
3764#ifdef DEBUG_PUSH
3765 printf("value loaded: %s\n", value);
3766#endif
3767 }
3768
3769 while ((exec->status == 0) &&
3770 ((value != NULL) ||
3771 ((final == 1) &&
3772 (exec->state->type != XML_REGEXP_FINAL_STATE)))) {
3773
3774 /*
3775 * End of input on non-terminal state, rollback, however we may
3776 * still have epsilon like transition for counted transitions
3777 * on counters, in that case don't break too early.
3778 */
Daniel Veillardb509f152002-04-17 16:28:10 +00003779 if ((value == NULL) && (exec->counts == NULL))
Daniel Veillard4255d502002-04-16 15:50:10 +00003780 goto rollback;
3781
3782 exec->transcount = 0;
3783 for (;exec->transno < exec->state->nbTrans;exec->transno++) {
3784 trans = &exec->state->trans[exec->transno];
3785 if (trans->to < 0)
3786 continue;
3787 atom = trans->atom;
3788 ret = 0;
Daniel Veillard441bc322002-04-20 17:38:48 +00003789 if (trans->count == REGEXP_ALL_LAX_COUNTER) {
3790 int i;
3791 int count;
3792 xmlRegTransPtr t;
3793 xmlRegCounterPtr counter;
3794
3795 ret = 0;
3796
3797#ifdef DEBUG_PUSH
3798 printf("testing all lax %d\n", trans->count);
3799#endif
3800 /*
3801 * Check all counted transitions from the current state
3802 */
3803 if ((value == NULL) && (final)) {
3804 ret = 1;
3805 } else if (value != NULL) {
3806 for (i = 0;i < exec->state->nbTrans;i++) {
3807 t = &exec->state->trans[i];
3808 if ((t->counter < 0) || (t == trans))
3809 continue;
3810 counter = &exec->comp->counters[t->counter];
3811 count = exec->counts[t->counter];
3812 if ((count < counter->max) &&
3813 (t->atom != NULL) &&
3814 (xmlStrEqual(value, t->atom->valuep))) {
3815 ret = 0;
3816 break;
3817 }
3818 if ((count >= counter->min) &&
3819 (count < counter->max) &&
Daniel Veillard11ce4002006-03-10 00:36:23 +00003820 (t->atom != NULL) &&
Daniel Veillard441bc322002-04-20 17:38:48 +00003821 (xmlStrEqual(value, t->atom->valuep))) {
3822 ret = 1;
3823 break;
3824 }
3825 }
3826 }
3827 } else if (trans->count == REGEXP_ALL_COUNTER) {
Daniel Veillard8a001f62002-04-20 07:24:11 +00003828 int i;
3829 int count;
3830 xmlRegTransPtr t;
3831 xmlRegCounterPtr counter;
3832
3833 ret = 1;
3834
3835#ifdef DEBUG_PUSH
3836 printf("testing all %d\n", trans->count);
3837#endif
3838 /*
3839 * Check all counted transitions from the current state
3840 */
3841 for (i = 0;i < exec->state->nbTrans;i++) {
3842 t = &exec->state->trans[i];
3843 if ((t->counter < 0) || (t == trans))
3844 continue;
3845 counter = &exec->comp->counters[t->counter];
3846 count = exec->counts[t->counter];
3847 if ((count < counter->min) || (count > counter->max)) {
3848 ret = 0;
3849 break;
3850 }
3851 }
3852 } else if (trans->count >= 0) {
Daniel Veillard4255d502002-04-16 15:50:10 +00003853 int count;
3854 xmlRegCounterPtr counter;
3855
3856 /*
3857 * A counted transition.
3858 */
3859
3860 count = exec->counts[trans->count];
3861 counter = &exec->comp->counters[trans->count];
3862#ifdef DEBUG_PUSH
3863 printf("testing count %d: val %d, min %d, max %d\n",
3864 trans->count, count, counter->min, counter->max);
3865#endif
3866 ret = ((count >= counter->min) && (count <= counter->max));
3867 } else if (atom == NULL) {
3868 fprintf(stderr, "epsilon transition left at runtime\n");
3869 exec->status = -2;
3870 break;
3871 } else if (value != NULL) {
Daniel Veillardc0826a72004-08-10 14:17:33 +00003872 ret = xmlRegStrEqualWildcard(atom->valuep, value);
Daniel Veillard6e65e152005-08-09 11:09:52 +00003873 if (atom->neg) {
Daniel Veillard9efc4762005-07-19 14:33:55 +00003874 ret = !ret;
Daniel Veillard6e65e152005-08-09 11:09:52 +00003875 if (!compound)
3876 ret = 0;
3877 }
Daniel Veillard441bc322002-04-20 17:38:48 +00003878 if ((ret == 1) && (trans->counter >= 0)) {
3879 xmlRegCounterPtr counter;
3880 int count;
3881
3882 count = exec->counts[trans->counter];
3883 counter = &exec->comp->counters[trans->counter];
3884 if (count >= counter->max)
3885 ret = 0;
3886 }
3887
Daniel Veillard4255d502002-04-16 15:50:10 +00003888 if ((ret == 1) && (atom->min > 0) && (atom->max > 0)) {
3889 xmlRegStatePtr to = exec->comp->states[trans->to];
3890
3891 /*
3892 * this is a multiple input sequence
3893 */
3894 if (exec->state->nbTrans > exec->transno + 1) {
3895 if (exec->inputStackNr <= 0) {
3896 xmlFARegExecSaveInputString(exec, value, data);
3897 }
3898 xmlFARegExecSave(exec);
3899 }
3900 exec->transcount = 1;
3901 do {
3902 /*
3903 * Try to progress as much as possible on the input
3904 */
3905 if (exec->transcount == atom->max) {
3906 break;
3907 }
3908 exec->index++;
3909 value = exec->inputStack[exec->index].value;
3910 data = exec->inputStack[exec->index].data;
3911#ifdef DEBUG_PUSH
3912 printf("value loaded: %s\n", value);
3913#endif
3914
3915 /*
3916 * End of input: stop here
3917 */
3918 if (value == NULL) {
3919 exec->index --;
3920 break;
3921 }
3922 if (exec->transcount >= atom->min) {
3923 int transno = exec->transno;
3924 xmlRegStatePtr state = exec->state;
3925
3926 /*
3927 * The transition is acceptable save it
3928 */
3929 exec->transno = -1; /* trick */
3930 exec->state = to;
3931 if (exec->inputStackNr <= 0) {
3932 xmlFARegExecSaveInputString(exec, value, data);
3933 }
3934 xmlFARegExecSave(exec);
3935 exec->transno = transno;
3936 exec->state = state;
3937 }
3938 ret = xmlStrEqual(value, atom->valuep);
3939 exec->transcount++;
3940 } while (ret == 1);
3941 if (exec->transcount < atom->min)
3942 ret = 0;
3943
3944 /*
3945 * If the last check failed but one transition was found
3946 * possible, rollback
3947 */
3948 if (ret < 0)
3949 ret = 0;
3950 if (ret == 0) {
3951 goto rollback;
3952 }
3953 }
3954 }
3955 if (ret == 1) {
William M. Brack98873952003-12-26 06:03:14 +00003956 if ((exec->callback != NULL) && (atom != NULL) &&
3957 (data != NULL)) {
Daniel Veillard4255d502002-04-16 15:50:10 +00003958 exec->callback(exec->data, atom->valuep,
3959 atom->data, data);
3960 }
3961 if (exec->state->nbTrans > exec->transno + 1) {
3962 if (exec->inputStackNr <= 0) {
3963 xmlFARegExecSaveInputString(exec, value, data);
3964 }
3965 xmlFARegExecSave(exec);
3966 }
3967 if (trans->counter >= 0) {
3968#ifdef DEBUG_PUSH
3969 printf("Increasing count %d\n", trans->counter);
3970#endif
3971 exec->counts[trans->counter]++;
3972 }
Daniel Veillard10752282005-08-08 13:05:13 +00003973 if ((trans->count >= 0) &&
3974 (trans->count < REGEXP_ALL_COUNTER)) {
3975#ifdef DEBUG_REGEXP_EXEC
3976 printf("resetting count %d on transition\n",
3977 trans->count);
3978#endif
3979 exec->counts[trans->count] = 0;
3980 }
Daniel Veillard4255d502002-04-16 15:50:10 +00003981#ifdef DEBUG_PUSH
3982 printf("entering state %d\n", trans->to);
3983#endif
Daniel Veillardcc026dc2005-01-12 13:21:17 +00003984 if ((exec->comp->states[trans->to] != NULL) &&
3985 (exec->comp->states[trans->to]->type ==
3986 XML_REGEXP_SINK_STATE)) {
3987 /*
3988 * entering a sink state, save the current state as error
3989 * state.
3990 */
3991 if (exec->errString != NULL)
3992 xmlFree(exec->errString);
3993 exec->errString = xmlStrdup(value);
3994 exec->errState = exec->state;
3995 memcpy(exec->errCounts, exec->counts,
3996 exec->comp->nbCounters * sizeof(int));
3997 }
Daniel Veillard4255d502002-04-16 15:50:10 +00003998 exec->state = exec->comp->states[trans->to];
3999 exec->transno = 0;
4000 if (trans->atom != NULL) {
4001 if (exec->inputStack != NULL) {
4002 exec->index++;
4003 if (exec->index < exec->inputStackNr) {
4004 value = exec->inputStack[exec->index].value;
4005 data = exec->inputStack[exec->index].data;
4006#ifdef DEBUG_PUSH
4007 printf("value loaded: %s\n", value);
4008#endif
4009 } else {
4010 value = NULL;
4011 data = NULL;
4012#ifdef DEBUG_PUSH
4013 printf("end of input\n");
4014#endif
4015 }
4016 } else {
4017 value = NULL;
4018 data = NULL;
4019#ifdef DEBUG_PUSH
4020 printf("end of input\n");
4021#endif
4022 }
4023 }
4024 goto progress;
4025 } else if (ret < 0) {
4026 exec->status = -4;
4027 break;
4028 }
4029 }
4030 if ((exec->transno != 0) || (exec->state->nbTrans == 0)) {
4031rollback:
Daniel Veillard90700152005-01-08 22:05:09 +00004032 /*
Daniel Veillardcc026dc2005-01-12 13:21:17 +00004033 * if we didn't yet rollback on the current input
4034 * store the current state as the error state.
Daniel Veillard90700152005-01-08 22:05:09 +00004035 */
Daniel Veillardcc026dc2005-01-12 13:21:17 +00004036 if ((progress) && (exec->state != NULL) &&
4037 (exec->state->type != XML_REGEXP_SINK_STATE)) {
Daniel Veillard90700152005-01-08 22:05:09 +00004038 progress = 0;
4039 if (exec->errString != NULL)
4040 xmlFree(exec->errString);
4041 exec->errString = xmlStrdup(value);
4042 exec->errState = exec->state;
4043 memcpy(exec->errCounts, exec->counts,
4044 exec->comp->nbCounters * sizeof(int));
4045 }
4046
Daniel Veillard4255d502002-04-16 15:50:10 +00004047 /*
4048 * Failed to find a way out
4049 */
4050 exec->determinist = 0;
4051 xmlFARegExecRollBack(exec);
4052 if (exec->status == 0) {
4053 value = exec->inputStack[exec->index].value;
4054 data = exec->inputStack[exec->index].data;
4055#ifdef DEBUG_PUSH
4056 printf("value loaded: %s\n", value);
4057#endif
4058 }
4059 }
Daniel Veillard90700152005-01-08 22:05:09 +00004060 continue;
Daniel Veillard4255d502002-04-16 15:50:10 +00004061progress:
Daniel Veillard90700152005-01-08 22:05:09 +00004062 progress = 1;
Daniel Veillard4255d502002-04-16 15:50:10 +00004063 continue;
4064 }
4065 if (exec->status == 0) {
4066 return(exec->state->type == XML_REGEXP_FINAL_STATE);
4067 }
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00004068#ifdef DEBUG_ERR
Daniel Veillard90700152005-01-08 22:05:09 +00004069 if (exec->status < 0) {
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00004070 testerr(exec);
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00004071 }
Daniel Veillard90700152005-01-08 22:05:09 +00004072#endif
Daniel Veillard4255d502002-04-16 15:50:10 +00004073 return(exec->status);
4074}
4075
Daniel Veillard52b48c72003-04-13 19:53:42 +00004076/**
Daniel Veillard6e65e152005-08-09 11:09:52 +00004077 * xmlRegExecPushString:
4078 * @exec: a regexp execution context or NULL to indicate the end
4079 * @value: a string token input
4080 * @data: data associated to the token to reuse in callbacks
4081 *
4082 * Push one input token in the execution context
4083 *
4084 * Returns: 1 if the regexp reached a final state, 0 if non-final, and
4085 * a negative value in case of error.
4086 */
4087int
4088xmlRegExecPushString(xmlRegExecCtxtPtr exec, const xmlChar *value,
4089 void *data) {
4090 return(xmlRegExecPushStringInternal(exec, value, data, 0));
4091}
4092
4093/**
Daniel Veillard52b48c72003-04-13 19:53:42 +00004094 * xmlRegExecPushString2:
4095 * @exec: a regexp execution context or NULL to indicate the end
4096 * @value: the first string token input
4097 * @value2: the second string token input
4098 * @data: data associated to the token to reuse in callbacks
4099 *
4100 * Push one input token in the execution context
4101 *
4102 * Returns: 1 if the regexp reached a final state, 0 if non-final, and
4103 * a negative value in case of error.
4104 */
4105int
4106xmlRegExecPushString2(xmlRegExecCtxtPtr exec, const xmlChar *value,
4107 const xmlChar *value2, void *data) {
4108 xmlChar buf[150];
4109 int lenn, lenp, ret;
4110 xmlChar *str;
4111
4112 if (exec == NULL)
4113 return(-1);
4114 if (exec->comp == NULL)
4115 return(-1);
4116 if (exec->status != 0)
4117 return(exec->status);
4118
4119 if (value2 == NULL)
4120 return(xmlRegExecPushString(exec, value, data));
4121
4122 lenn = strlen((char *) value2);
4123 lenp = strlen((char *) value);
4124
4125 if (150 < lenn + lenp + 2) {
Daniel Veillard3c908dc2003-04-19 00:07:51 +00004126 str = (xmlChar *) xmlMallocAtomic(lenn + lenp + 2);
Daniel Veillard52b48c72003-04-13 19:53:42 +00004127 if (str == NULL) {
4128 exec->status = -1;
4129 return(-1);
4130 }
4131 } else {
4132 str = buf;
4133 }
4134 memcpy(&str[0], value, lenp);
Daniel Veillardc0826a72004-08-10 14:17:33 +00004135 str[lenp] = XML_REG_STRING_SEPARATOR;
Daniel Veillard52b48c72003-04-13 19:53:42 +00004136 memcpy(&str[lenp + 1], value2, lenn);
4137 str[lenn + lenp + 1] = 0;
4138
4139 if (exec->comp->compact != NULL)
4140 ret = xmlRegCompactPushString(exec, exec->comp, str, data);
4141 else
Daniel Veillard6e65e152005-08-09 11:09:52 +00004142 ret = xmlRegExecPushStringInternal(exec, str, data, 1);
Daniel Veillard52b48c72003-04-13 19:53:42 +00004143
4144 if (str != buf)
Daniel Veillard0b1ff142005-12-28 21:13:33 +00004145 xmlFree(str);
Daniel Veillard52b48c72003-04-13 19:53:42 +00004146 return(ret);
4147}
4148
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00004149/**
Daniel Veillard77005e62005-07-19 16:26:18 +00004150 * xmlRegExecGetValues:
Daniel Veillardfc0b6f62005-01-09 17:48:02 +00004151 * @exec: a regexp execution context
4152 * @err: error extraction or normal one
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00004153 * @nbval: pointer to the number of accepted values IN/OUT
Daniel Veillardcc026dc2005-01-12 13:21:17 +00004154 * @nbneg: return number of negative transitions
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00004155 * @values: pointer to the array of acceptable values
Daniel Veillardfc0b6f62005-01-09 17:48:02 +00004156 * @terminal: return value if this was a terminal state
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00004157 *
Daniel Veillardfc0b6f62005-01-09 17:48:02 +00004158 * Extract informations from the regexp execution, internal routine to
4159 * implement xmlRegExecNextValues() and xmlRegExecErrInfo()
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00004160 *
4161 * Returns: 0 in case of success or -1 in case of error.
4162 */
Daniel Veillardfc0b6f62005-01-09 17:48:02 +00004163static int
4164xmlRegExecGetValues(xmlRegExecCtxtPtr exec, int err,
Daniel Veillardcc026dc2005-01-12 13:21:17 +00004165 int *nbval, int *nbneg,
4166 xmlChar **values, int *terminal) {
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00004167 int maxval;
Daniel Veillardcc026dc2005-01-12 13:21:17 +00004168 int nb = 0;
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00004169
Daniel Veillardcc026dc2005-01-12 13:21:17 +00004170 if ((exec == NULL) || (nbval == NULL) || (nbneg == NULL) ||
4171 (values == NULL) || (*nbval <= 0))
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00004172 return(-1);
Daniel Veillardfc0b6f62005-01-09 17:48:02 +00004173
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00004174 maxval = *nbval;
4175 *nbval = 0;
Daniel Veillardcc026dc2005-01-12 13:21:17 +00004176 *nbneg = 0;
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00004177 if ((exec->comp != NULL) && (exec->comp->compact != NULL)) {
4178 xmlRegexpPtr comp;
4179 int target, i, state;
4180
4181 comp = exec->comp;
Daniel Veillardfc0b6f62005-01-09 17:48:02 +00004182
4183 if (err) {
4184 if (exec->errStateNo == -1) return(-1);
4185 state = exec->errStateNo;
4186 } else {
4187 state = exec->index;
4188 }
4189 if (terminal != NULL) {
4190 if (comp->compact[state * (comp->nbstrings + 1)] ==
4191 XML_REGEXP_FINAL_STATE)
4192 *terminal = 1;
4193 else
4194 *terminal = 0;
4195 }
Daniel Veillardcc026dc2005-01-12 13:21:17 +00004196 for (i = 0;(i < comp->nbstrings) && (nb < maxval);i++) {
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00004197 target = comp->compact[state * (comp->nbstrings + 1) + i + 1];
Daniel Veillardcc026dc2005-01-12 13:21:17 +00004198 if ((target > 0) && (target <= comp->nbstates) &&
4199 (comp->compact[(target - 1) * (comp->nbstrings + 1)] !=
4200 XML_REGEXP_SINK_STATE)) {
4201 values[nb++] = comp->stringMap[i];
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00004202 (*nbval)++;
4203 }
4204 }
Daniel Veillardcc026dc2005-01-12 13:21:17 +00004205 for (i = 0;(i < comp->nbstrings) && (nb < maxval);i++) {
4206 target = comp->compact[state * (comp->nbstrings + 1) + i + 1];
4207 if ((target > 0) && (target <= comp->nbstates) &&
4208 (comp->compact[(target - 1) * (comp->nbstrings + 1)] ==
4209 XML_REGEXP_SINK_STATE)) {
4210 values[nb++] = comp->stringMap[i];
4211 (*nbneg)++;
4212 }
4213 }
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00004214 } else {
4215 int transno;
4216 xmlRegTransPtr trans;
4217 xmlRegAtomPtr atom;
Daniel Veillardfc0b6f62005-01-09 17:48:02 +00004218 xmlRegStatePtr state;
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00004219
Daniel Veillardfc0b6f62005-01-09 17:48:02 +00004220 if (terminal != NULL) {
4221 if (exec->state->type == XML_REGEXP_FINAL_STATE)
4222 *terminal = 1;
4223 else
4224 *terminal = 0;
4225 }
4226
4227 if (err) {
4228 if (exec->errState == NULL) return(-1);
4229 state = exec->errState;
4230 } else {
4231 if (exec->state == NULL) return(-1);
4232 state = exec->state;
4233 }
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00004234 for (transno = 0;
Daniel Veillardcc026dc2005-01-12 13:21:17 +00004235 (transno < state->nbTrans) && (nb < maxval);
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00004236 transno++) {
Daniel Veillardfc0b6f62005-01-09 17:48:02 +00004237 trans = &state->trans[transno];
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00004238 if (trans->to < 0)
4239 continue;
4240 atom = trans->atom;
4241 if ((atom == NULL) || (atom->valuep == NULL))
4242 continue;
4243 if (trans->count == REGEXP_ALL_LAX_COUNTER) {
Daniel Veillardcc026dc2005-01-12 13:21:17 +00004244 /* this should not be reached but ... */
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00004245 TODO;
4246 } else if (trans->count == REGEXP_ALL_COUNTER) {
Daniel Veillardcc026dc2005-01-12 13:21:17 +00004247 /* this should not be reached but ... */
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00004248 TODO;
4249 } else if (trans->counter >= 0) {
Daniel Veillard11ce4002006-03-10 00:36:23 +00004250 xmlRegCounterPtr counter = NULL;
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00004251 int count;
4252
Daniel Veillardfc0b6f62005-01-09 17:48:02 +00004253 if (err)
4254 count = exec->errCounts[trans->counter];
4255 else
4256 count = exec->counts[trans->counter];
Daniel Veillard11ce4002006-03-10 00:36:23 +00004257 if (exec->comp != NULL)
4258 counter = &exec->comp->counters[trans->counter];
4259 if ((counter == NULL) || (count < counter->max)) {
Daniel Veillard77005e62005-07-19 16:26:18 +00004260 if (atom->neg)
4261 values[nb++] = (xmlChar *) atom->valuep2;
4262 else
4263 values[nb++] = (xmlChar *) atom->valuep;
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00004264 (*nbval)++;
4265 }
4266 } else {
Daniel Veillardcc026dc2005-01-12 13:21:17 +00004267 if ((exec->comp->states[trans->to] != NULL) &&
4268 (exec->comp->states[trans->to]->type !=
4269 XML_REGEXP_SINK_STATE)) {
Daniel Veillard77005e62005-07-19 16:26:18 +00004270 if (atom->neg)
4271 values[nb++] = (xmlChar *) atom->valuep2;
4272 else
4273 values[nb++] = (xmlChar *) atom->valuep;
Daniel Veillardcc026dc2005-01-12 13:21:17 +00004274 (*nbval)++;
4275 }
4276 }
4277 }
4278 for (transno = 0;
4279 (transno < state->nbTrans) && (nb < maxval);
4280 transno++) {
4281 trans = &state->trans[transno];
4282 if (trans->to < 0)
4283 continue;
4284 atom = trans->atom;
4285 if ((atom == NULL) || (atom->valuep == NULL))
4286 continue;
4287 if (trans->count == REGEXP_ALL_LAX_COUNTER) {
4288 continue;
4289 } else if (trans->count == REGEXP_ALL_COUNTER) {
4290 continue;
4291 } else if (trans->counter >= 0) {
4292 continue;
4293 } else {
4294 if ((exec->comp->states[trans->to] != NULL) &&
4295 (exec->comp->states[trans->to]->type ==
4296 XML_REGEXP_SINK_STATE)) {
Daniel Veillard77005e62005-07-19 16:26:18 +00004297 if (atom->neg)
4298 values[nb++] = (xmlChar *) atom->valuep2;
4299 else
4300 values[nb++] = (xmlChar *) atom->valuep;
Daniel Veillardcc026dc2005-01-12 13:21:17 +00004301 (*nbneg)++;
4302 }
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00004303 }
4304 }
4305 }
4306 return(0);
4307}
4308
Daniel Veillardfc0b6f62005-01-09 17:48:02 +00004309/**
4310 * xmlRegExecNextValues:
4311 * @exec: a regexp execution context
4312 * @nbval: pointer to the number of accepted values IN/OUT
Daniel Veillardcc026dc2005-01-12 13:21:17 +00004313 * @nbneg: return number of negative transitions
Daniel Veillardfc0b6f62005-01-09 17:48:02 +00004314 * @values: pointer to the array of acceptable values
4315 * @terminal: return value if this was a terminal state
4316 *
4317 * Extract informations from the regexp execution,
4318 * the parameter @values must point to an array of @nbval string pointers
4319 * on return nbval will contain the number of possible strings in that
4320 * state and the @values array will be updated with them. The string values
4321 * returned will be freed with the @exec context and don't need to be
4322 * deallocated.
4323 *
4324 * Returns: 0 in case of success or -1 in case of error.
4325 */
4326int
Daniel Veillardcc026dc2005-01-12 13:21:17 +00004327xmlRegExecNextValues(xmlRegExecCtxtPtr exec, int *nbval, int *nbneg,
4328 xmlChar **values, int *terminal) {
4329 return(xmlRegExecGetValues(exec, 0, nbval, nbneg, values, terminal));
Daniel Veillardfc0b6f62005-01-09 17:48:02 +00004330}
4331
4332/**
4333 * xmlRegExecErrInfo:
4334 * @exec: a regexp execution context generating an error
4335 * @string: return value for the error string
4336 * @nbval: pointer to the number of accepted values IN/OUT
Daniel Veillardcc026dc2005-01-12 13:21:17 +00004337 * @nbneg: return number of negative transitions
Daniel Veillardfc0b6f62005-01-09 17:48:02 +00004338 * @values: pointer to the array of acceptable values
4339 * @terminal: return value if this was a terminal state
4340 *
4341 * Extract error informations from the regexp execution, the parameter
4342 * @string will be updated with the value pushed and not accepted,
4343 * the parameter @values must point to an array of @nbval string pointers
4344 * on return nbval will contain the number of possible strings in that
4345 * state and the @values array will be updated with them. The string values
4346 * returned will be freed with the @exec context and don't need to be
4347 * deallocated.
4348 *
4349 * Returns: 0 in case of success or -1 in case of error.
4350 */
4351int
4352xmlRegExecErrInfo(xmlRegExecCtxtPtr exec, const xmlChar **string,
Daniel Veillardcc026dc2005-01-12 13:21:17 +00004353 int *nbval, int *nbneg, xmlChar **values, int *terminal) {
Daniel Veillardfc0b6f62005-01-09 17:48:02 +00004354 if (exec == NULL)
4355 return(-1);
4356 if (string != NULL) {
4357 if (exec->status != 0)
4358 *string = exec->errString;
4359 else
4360 *string = NULL;
4361 }
Daniel Veillardcc026dc2005-01-12 13:21:17 +00004362 return(xmlRegExecGetValues(exec, 1, nbval, nbneg, values, terminal));
Daniel Veillardfc0b6f62005-01-09 17:48:02 +00004363}
4364
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00004365#ifdef DEBUG_ERR
4366static void testerr(xmlRegExecCtxtPtr exec) {
4367 const xmlChar *string;
Daniel Veillardcee2b3a2005-01-25 00:22:52 +00004368 xmlChar *values[5];
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00004369 int nb = 5;
Daniel Veillardcc026dc2005-01-12 13:21:17 +00004370 int nbneg;
Daniel Veillardfc0b6f62005-01-09 17:48:02 +00004371 int terminal;
Daniel Veillardcc026dc2005-01-12 13:21:17 +00004372 xmlRegExecErrInfo(exec, &string, &nb, &nbneg, &values[0], &terminal);
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00004373}
4374#endif
4375
Daniel Veillard4255d502002-04-16 15:50:10 +00004376#if 0
4377static int
4378xmlRegExecPushChar(xmlRegExecCtxtPtr exec, int UCS) {
4379 xmlRegTransPtr trans;
4380 xmlRegAtomPtr atom;
4381 int ret;
4382 int codepoint, len;
4383
4384 if (exec == NULL)
4385 return(-1);
4386 if (exec->status != 0)
4387 return(exec->status);
4388
4389 while ((exec->status == 0) &&
4390 ((exec->inputString[exec->index] != 0) ||
4391 (exec->state->type != XML_REGEXP_FINAL_STATE))) {
4392
4393 /*
4394 * End of input on non-terminal state, rollback, however we may
4395 * still have epsilon like transition for counted transitions
4396 * on counters, in that case don't break too early.
4397 */
4398 if ((exec->inputString[exec->index] == 0) && (exec->counts == NULL))
4399 goto rollback;
4400
4401 exec->transcount = 0;
4402 for (;exec->transno < exec->state->nbTrans;exec->transno++) {
4403 trans = &exec->state->trans[exec->transno];
4404 if (trans->to < 0)
4405 continue;
4406 atom = trans->atom;
4407 ret = 0;
4408 if (trans->count >= 0) {
4409 int count;
4410 xmlRegCounterPtr counter;
4411
4412 /*
4413 * A counted transition.
4414 */
4415
4416 count = exec->counts[trans->count];
4417 counter = &exec->comp->counters[trans->count];
4418#ifdef DEBUG_REGEXP_EXEC
4419 printf("testing count %d: val %d, min %d, max %d\n",
4420 trans->count, count, counter->min, counter->max);
4421#endif
4422 ret = ((count >= counter->min) && (count <= counter->max));
4423 } else if (atom == NULL) {
4424 fprintf(stderr, "epsilon transition left at runtime\n");
4425 exec->status = -2;
4426 break;
4427 } else if (exec->inputString[exec->index] != 0) {
4428 codepoint = CUR_SCHAR(&(exec->inputString[exec->index]), len);
4429 ret = xmlRegCheckCharacter(atom, codepoint);
4430 if ((ret == 1) && (atom->min > 0) && (atom->max > 0)) {
4431 xmlRegStatePtr to = exec->comp->states[trans->to];
4432
4433 /*
4434 * this is a multiple input sequence
4435 */
4436 if (exec->state->nbTrans > exec->transno + 1) {
4437 xmlFARegExecSave(exec);
4438 }
4439 exec->transcount = 1;
4440 do {
4441 /*
4442 * Try to progress as much as possible on the input
4443 */
4444 if (exec->transcount == atom->max) {
4445 break;
4446 }
4447 exec->index += len;
4448 /*
4449 * End of input: stop here
4450 */
4451 if (exec->inputString[exec->index] == 0) {
4452 exec->index -= len;
4453 break;
4454 }
4455 if (exec->transcount >= atom->min) {
4456 int transno = exec->transno;
4457 xmlRegStatePtr state = exec->state;
4458
4459 /*
4460 * The transition is acceptable save it
4461 */
4462 exec->transno = -1; /* trick */
4463 exec->state = to;
4464 xmlFARegExecSave(exec);
4465 exec->transno = transno;
4466 exec->state = state;
4467 }
4468 codepoint = CUR_SCHAR(&(exec->inputString[exec->index]),
4469 len);
4470 ret = xmlRegCheckCharacter(atom, codepoint);
4471 exec->transcount++;
4472 } while (ret == 1);
4473 if (exec->transcount < atom->min)
4474 ret = 0;
4475
4476 /*
4477 * If the last check failed but one transition was found
4478 * possible, rollback
4479 */
4480 if (ret < 0)
4481 ret = 0;
4482 if (ret == 0) {
4483 goto rollback;
4484 }
4485 }
4486 }
4487 if (ret == 1) {
4488 if (exec->state->nbTrans > exec->transno + 1) {
4489 xmlFARegExecSave(exec);
4490 }
Daniel Veillard54eb0242006-03-21 23:17:57 +00004491 /*
4492 * restart count for expressions like this ((abc){2})*
4493 */
4494 if (trans->count >= 0) {
4495#ifdef DEBUG_REGEXP_EXEC
4496 printf("Reset count %d\n", trans->count);
4497#endif
4498 exec->counts[trans->count] = 0;
4499 }
Daniel Veillard4255d502002-04-16 15:50:10 +00004500 if (trans->counter >= 0) {
4501#ifdef DEBUG_REGEXP_EXEC
4502 printf("Increasing count %d\n", trans->counter);
4503#endif
4504 exec->counts[trans->counter]++;
4505 }
4506#ifdef DEBUG_REGEXP_EXEC
4507 printf("entering state %d\n", trans->to);
4508#endif
4509 exec->state = exec->comp->states[trans->to];
4510 exec->transno = 0;
4511 if (trans->atom != NULL) {
4512 exec->index += len;
4513 }
4514 goto progress;
4515 } else if (ret < 0) {
4516 exec->status = -4;
4517 break;
4518 }
4519 }
4520 if ((exec->transno != 0) || (exec->state->nbTrans == 0)) {
4521rollback:
4522 /*
4523 * Failed to find a way out
4524 */
4525 exec->determinist = 0;
4526 xmlFARegExecRollBack(exec);
4527 }
4528progress:
4529 continue;
4530 }
4531}
4532#endif
4533/************************************************************************
4534 * *
William M. Brackddf71d62004-05-06 04:17:26 +00004535 * Parser for the Schemas Datatype Regular Expressions *
Daniel Veillard4255d502002-04-16 15:50:10 +00004536 * http://www.w3.org/TR/2001/REC-xmlschema-2-20010502/#regexs *
4537 * *
4538 ************************************************************************/
4539
4540/**
4541 * xmlFAIsChar:
Daniel Veillard441bc322002-04-20 17:38:48 +00004542 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00004543 *
4544 * [10] Char ::= [^.\?*+()|#x5B#x5D]
4545 */
4546static int
4547xmlFAIsChar(xmlRegParserCtxtPtr ctxt) {
4548 int cur;
4549 int len;
4550
4551 cur = CUR_SCHAR(ctxt->cur, len);
4552 if ((cur == '.') || (cur == '\\') || (cur == '?') ||
4553 (cur == '*') || (cur == '+') || (cur == '(') ||
4554 (cur == ')') || (cur == '|') || (cur == 0x5B) ||
4555 (cur == 0x5D) || (cur == 0))
4556 return(-1);
4557 return(cur);
4558}
4559
4560/**
4561 * xmlFAParseCharProp:
Daniel Veillard441bc322002-04-20 17:38:48 +00004562 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00004563 *
4564 * [27] charProp ::= IsCategory | IsBlock
4565 * [28] IsCategory ::= Letters | Marks | Numbers | Punctuation |
4566 * Separators | Symbols | Others
4567 * [29] Letters ::= 'L' [ultmo]?
4568 * [30] Marks ::= 'M' [nce]?
4569 * [31] Numbers ::= 'N' [dlo]?
4570 * [32] Punctuation ::= 'P' [cdseifo]?
4571 * [33] Separators ::= 'Z' [slp]?
4572 * [34] Symbols ::= 'S' [mcko]?
4573 * [35] Others ::= 'C' [cfon]?
4574 * [36] IsBlock ::= 'Is' [a-zA-Z0-9#x2D]+
4575 */
4576static void
4577xmlFAParseCharProp(xmlRegParserCtxtPtr ctxt) {
4578 int cur;
William M. Brack779af002003-08-01 15:55:39 +00004579 xmlRegAtomType type = (xmlRegAtomType) 0;
Daniel Veillard4255d502002-04-16 15:50:10 +00004580 xmlChar *blockName = NULL;
4581
4582 cur = CUR;
4583 if (cur == 'L') {
4584 NEXT;
4585 cur = CUR;
4586 if (cur == 'u') {
4587 NEXT;
4588 type = XML_REGEXP_LETTER_UPPERCASE;
4589 } else if (cur == 'l') {
4590 NEXT;
4591 type = XML_REGEXP_LETTER_LOWERCASE;
4592 } else if (cur == 't') {
4593 NEXT;
4594 type = XML_REGEXP_LETTER_TITLECASE;
4595 } else if (cur == 'm') {
4596 NEXT;
4597 type = XML_REGEXP_LETTER_MODIFIER;
4598 } else if (cur == 'o') {
4599 NEXT;
4600 type = XML_REGEXP_LETTER_OTHERS;
4601 } else {
4602 type = XML_REGEXP_LETTER;
4603 }
4604 } else if (cur == 'M') {
4605 NEXT;
4606 cur = CUR;
4607 if (cur == 'n') {
4608 NEXT;
4609 /* nonspacing */
4610 type = XML_REGEXP_MARK_NONSPACING;
4611 } else if (cur == 'c') {
4612 NEXT;
4613 /* spacing combining */
4614 type = XML_REGEXP_MARK_SPACECOMBINING;
4615 } else if (cur == 'e') {
4616 NEXT;
4617 /* enclosing */
4618 type = XML_REGEXP_MARK_ENCLOSING;
4619 } else {
4620 /* all marks */
4621 type = XML_REGEXP_MARK;
4622 }
4623 } else if (cur == 'N') {
4624 NEXT;
4625 cur = CUR;
4626 if (cur == 'd') {
4627 NEXT;
4628 /* digital */
4629 type = XML_REGEXP_NUMBER_DECIMAL;
4630 } else if (cur == 'l') {
4631 NEXT;
4632 /* letter */
4633 type = XML_REGEXP_NUMBER_LETTER;
4634 } else if (cur == 'o') {
4635 NEXT;
4636 /* other */
4637 type = XML_REGEXP_NUMBER_OTHERS;
4638 } else {
4639 /* all numbers */
4640 type = XML_REGEXP_NUMBER;
4641 }
4642 } else if (cur == 'P') {
4643 NEXT;
4644 cur = CUR;
4645 if (cur == 'c') {
4646 NEXT;
4647 /* connector */
4648 type = XML_REGEXP_PUNCT_CONNECTOR;
4649 } else if (cur == 'd') {
4650 NEXT;
4651 /* dash */
4652 type = XML_REGEXP_PUNCT_DASH;
4653 } else if (cur == 's') {
4654 NEXT;
4655 /* open */
4656 type = XML_REGEXP_PUNCT_OPEN;
4657 } else if (cur == 'e') {
4658 NEXT;
4659 /* close */
4660 type = XML_REGEXP_PUNCT_CLOSE;
4661 } else if (cur == 'i') {
4662 NEXT;
4663 /* initial quote */
4664 type = XML_REGEXP_PUNCT_INITQUOTE;
4665 } else if (cur == 'f') {
4666 NEXT;
4667 /* final quote */
4668 type = XML_REGEXP_PUNCT_FINQUOTE;
4669 } else if (cur == 'o') {
4670 NEXT;
4671 /* other */
4672 type = XML_REGEXP_PUNCT_OTHERS;
4673 } else {
4674 /* all punctuation */
4675 type = XML_REGEXP_PUNCT;
4676 }
4677 } else if (cur == 'Z') {
4678 NEXT;
4679 cur = CUR;
4680 if (cur == 's') {
4681 NEXT;
4682 /* space */
4683 type = XML_REGEXP_SEPAR_SPACE;
4684 } else if (cur == 'l') {
4685 NEXT;
4686 /* line */
4687 type = XML_REGEXP_SEPAR_LINE;
4688 } else if (cur == 'p') {
4689 NEXT;
4690 /* paragraph */
4691 type = XML_REGEXP_SEPAR_PARA;
4692 } else {
4693 /* all separators */
4694 type = XML_REGEXP_SEPAR;
4695 }
4696 } else if (cur == 'S') {
4697 NEXT;
4698 cur = CUR;
4699 if (cur == 'm') {
4700 NEXT;
4701 type = XML_REGEXP_SYMBOL_MATH;
4702 /* math */
4703 } else if (cur == 'c') {
4704 NEXT;
4705 type = XML_REGEXP_SYMBOL_CURRENCY;
4706 /* currency */
4707 } else if (cur == 'k') {
4708 NEXT;
4709 type = XML_REGEXP_SYMBOL_MODIFIER;
4710 /* modifiers */
4711 } else if (cur == 'o') {
4712 NEXT;
4713 type = XML_REGEXP_SYMBOL_OTHERS;
4714 /* other */
4715 } else {
4716 /* all symbols */
4717 type = XML_REGEXP_SYMBOL;
4718 }
4719 } else if (cur == 'C') {
4720 NEXT;
4721 cur = CUR;
4722 if (cur == 'c') {
4723 NEXT;
4724 /* control */
4725 type = XML_REGEXP_OTHER_CONTROL;
4726 } else if (cur == 'f') {
4727 NEXT;
4728 /* format */
4729 type = XML_REGEXP_OTHER_FORMAT;
4730 } else if (cur == 'o') {
4731 NEXT;
4732 /* private use */
4733 type = XML_REGEXP_OTHER_PRIVATE;
4734 } else if (cur == 'n') {
4735 NEXT;
4736 /* not assigned */
4737 type = XML_REGEXP_OTHER_NA;
4738 } else {
4739 /* all others */
4740 type = XML_REGEXP_OTHER;
4741 }
4742 } else if (cur == 'I') {
4743 const xmlChar *start;
4744 NEXT;
4745 cur = CUR;
4746 if (cur != 's') {
4747 ERROR("IsXXXX expected");
4748 return;
4749 }
4750 NEXT;
4751 start = ctxt->cur;
4752 cur = CUR;
4753 if (((cur >= 'a') && (cur <= 'z')) ||
4754 ((cur >= 'A') && (cur <= 'Z')) ||
4755 ((cur >= '0') && (cur <= '9')) ||
4756 (cur == 0x2D)) {
4757 NEXT;
4758 cur = CUR;
4759 while (((cur >= 'a') && (cur <= 'z')) ||
4760 ((cur >= 'A') && (cur <= 'Z')) ||
4761 ((cur >= '0') && (cur <= '9')) ||
4762 (cur == 0x2D)) {
4763 NEXT;
4764 cur = CUR;
4765 }
4766 }
4767 type = XML_REGEXP_BLOCK_NAME;
4768 blockName = xmlStrndup(start, ctxt->cur - start);
4769 } else {
4770 ERROR("Unknown char property");
4771 return;
4772 }
4773 if (ctxt->atom == NULL) {
4774 ctxt->atom = xmlRegNewAtom(ctxt, type);
4775 if (ctxt->atom != NULL)
4776 ctxt->atom->valuep = blockName;
4777 } else if (ctxt->atom->type == XML_REGEXP_RANGES) {
4778 xmlRegAtomAddRange(ctxt, ctxt->atom, ctxt->neg,
4779 type, 0, 0, blockName);
4780 }
4781}
4782
4783/**
4784 * xmlFAParseCharClassEsc:
Daniel Veillard441bc322002-04-20 17:38:48 +00004785 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00004786 *
4787 * [23] charClassEsc ::= ( SingleCharEsc | MultiCharEsc | catEsc | complEsc )
4788 * [24] SingleCharEsc ::= '\' [nrt\|.?*+(){}#x2D#x5B#x5D#x5E]
4789 * [25] catEsc ::= '\p{' charProp '}'
4790 * [26] complEsc ::= '\P{' charProp '}'
4791 * [37] MultiCharEsc ::= '.' | ('\' [sSiIcCdDwW])
4792 */
4793static void
4794xmlFAParseCharClassEsc(xmlRegParserCtxtPtr ctxt) {
4795 int cur;
4796
4797 if (CUR == '.') {
4798 if (ctxt->atom == NULL) {
4799 ctxt->atom = xmlRegNewAtom(ctxt, XML_REGEXP_ANYCHAR);
4800 } else if (ctxt->atom->type == XML_REGEXP_RANGES) {
4801 xmlRegAtomAddRange(ctxt, ctxt->atom, ctxt->neg,
4802 XML_REGEXP_ANYCHAR, 0, 0, NULL);
4803 }
4804 NEXT;
4805 return;
4806 }
4807 if (CUR != '\\') {
4808 ERROR("Escaped sequence: expecting \\");
4809 return;
4810 }
4811 NEXT;
4812 cur = CUR;
4813 if (cur == 'p') {
4814 NEXT;
4815 if (CUR != '{') {
4816 ERROR("Expecting '{'");
4817 return;
4818 }
4819 NEXT;
4820 xmlFAParseCharProp(ctxt);
4821 if (CUR != '}') {
4822 ERROR("Expecting '}'");
4823 return;
4824 }
4825 NEXT;
4826 } else if (cur == 'P') {
4827 NEXT;
4828 if (CUR != '{') {
4829 ERROR("Expecting '{'");
4830 return;
4831 }
4832 NEXT;
4833 xmlFAParseCharProp(ctxt);
4834 ctxt->atom->neg = 1;
4835 if (CUR != '}') {
4836 ERROR("Expecting '}'");
4837 return;
4838 }
4839 NEXT;
4840 } else if ((cur == 'n') || (cur == 'r') || (cur == 't') || (cur == '\\') ||
4841 (cur == '|') || (cur == '.') || (cur == '?') || (cur == '*') ||
4842 (cur == '+') || (cur == '(') || (cur == ')') || (cur == '{') ||
4843 (cur == '}') || (cur == 0x2D) || (cur == 0x5B) || (cur == 0x5D) ||
4844 (cur == 0x5E)) {
4845 if (ctxt->atom == NULL) {
4846 ctxt->atom = xmlRegNewAtom(ctxt, XML_REGEXP_CHARVAL);
Daniel Veillard99c394d2005-07-14 12:58:49 +00004847 if (ctxt->atom != NULL) {
4848 switch (cur) {
4849 case 'n':
4850 ctxt->atom->codepoint = '\n';
4851 break;
4852 case 'r':
4853 ctxt->atom->codepoint = '\r';
4854 break;
4855 case 't':
4856 ctxt->atom->codepoint = '\t';
4857 break;
4858 default:
4859 ctxt->atom->codepoint = cur;
4860 }
4861 }
Daniel Veillard4255d502002-04-16 15:50:10 +00004862 } else if (ctxt->atom->type == XML_REGEXP_RANGES) {
4863 xmlRegAtomAddRange(ctxt, ctxt->atom, ctxt->neg,
4864 XML_REGEXP_CHARVAL, cur, cur, NULL);
4865 }
4866 NEXT;
4867 } else if ((cur == 's') || (cur == 'S') || (cur == 'i') || (cur == 'I') ||
4868 (cur == 'c') || (cur == 'C') || (cur == 'd') || (cur == 'D') ||
4869 (cur == 'w') || (cur == 'W')) {
Daniel Veillardb509f152002-04-17 16:28:10 +00004870 xmlRegAtomType type = XML_REGEXP_ANYSPACE;
Daniel Veillard4255d502002-04-16 15:50:10 +00004871
4872 switch (cur) {
4873 case 's':
4874 type = XML_REGEXP_ANYSPACE;
4875 break;
4876 case 'S':
4877 type = XML_REGEXP_NOTSPACE;
4878 break;
4879 case 'i':
4880 type = XML_REGEXP_INITNAME;
4881 break;
4882 case 'I':
4883 type = XML_REGEXP_NOTINITNAME;
4884 break;
4885 case 'c':
4886 type = XML_REGEXP_NAMECHAR;
4887 break;
4888 case 'C':
4889 type = XML_REGEXP_NOTNAMECHAR;
4890 break;
4891 case 'd':
4892 type = XML_REGEXP_DECIMAL;
4893 break;
4894 case 'D':
4895 type = XML_REGEXP_NOTDECIMAL;
4896 break;
4897 case 'w':
4898 type = XML_REGEXP_REALCHAR;
4899 break;
4900 case 'W':
4901 type = XML_REGEXP_NOTREALCHAR;
4902 break;
4903 }
4904 NEXT;
4905 if (ctxt->atom == NULL) {
4906 ctxt->atom = xmlRegNewAtom(ctxt, type);
4907 } else if (ctxt->atom->type == XML_REGEXP_RANGES) {
4908 xmlRegAtomAddRange(ctxt, ctxt->atom, ctxt->neg,
4909 type, 0, 0, NULL);
4910 }
Daniel Veillardcb4284e2007-04-25 13:55:20 +00004911 } else {
4912 ERROR("Wrong escape sequence, misuse of character '\\'");
Daniel Veillard4255d502002-04-16 15:50:10 +00004913 }
4914}
4915
4916/**
Daniel Veillard4255d502002-04-16 15:50:10 +00004917 * xmlFAParseCharRange:
Daniel Veillard441bc322002-04-20 17:38:48 +00004918 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00004919 *
4920 * [17] charRange ::= seRange | XmlCharRef | XmlCharIncDash
4921 * [18] seRange ::= charOrEsc '-' charOrEsc
4922 * [20] charOrEsc ::= XmlChar | SingleCharEsc
4923 * [21] XmlChar ::= [^\#x2D#x5B#x5D]
4924 * [22] XmlCharIncDash ::= [^\#x5B#x5D]
4925 */
4926static void
4927xmlFAParseCharRange(xmlRegParserCtxtPtr ctxt) {
William M. Brackdc99df92003-12-27 01:54:25 +00004928 int cur, len;
Daniel Veillard4255d502002-04-16 15:50:10 +00004929 int start = -1;
4930 int end = -1;
4931
Daniel Veillard777737e2006-10-17 21:23:17 +00004932 if (CUR == '\0') {
4933 ERROR("Expecting ']'");
4934 return;
4935 }
4936
Daniel Veillard4255d502002-04-16 15:50:10 +00004937 cur = CUR;
4938 if (cur == '\\') {
4939 NEXT;
4940 cur = CUR;
4941 switch (cur) {
4942 case 'n': start = 0xA; break;
4943 case 'r': start = 0xD; break;
4944 case 't': start = 0x9; break;
4945 case '\\': case '|': case '.': case '-': case '^': case '?':
4946 case '*': case '+': case '{': case '}': case '(': case ')':
4947 case '[': case ']':
4948 start = cur; break;
4949 default:
4950 ERROR("Invalid escape value");
4951 return;
4952 }
4953 end = start;
William M. Brackdc99df92003-12-27 01:54:25 +00004954 len = 1;
Daniel Veillard4255d502002-04-16 15:50:10 +00004955 } else if ((cur != 0x5B) && (cur != 0x5D)) {
William M. Brackdc99df92003-12-27 01:54:25 +00004956 end = start = CUR_SCHAR(ctxt->cur, len);
Daniel Veillard4255d502002-04-16 15:50:10 +00004957 } else {
4958 ERROR("Expecting a char range");
4959 return;
4960 }
William M. Bracka9cbf282007-03-21 13:16:33 +00004961 /*
4962 * Since we are "inside" a range, we can assume ctxt->cur is past
4963 * the start of ctxt->string, and PREV should be safe
4964 */
4965 if ((start == '-') && (NXT(1) != ']') && (PREV != '[') && (PREV != '^')) {
4966 NEXTL(len);
Daniel Veillard4255d502002-04-16 15:50:10 +00004967 return;
4968 }
William M. Bracka9cbf282007-03-21 13:16:33 +00004969 NEXTL(len);
Daniel Veillard4255d502002-04-16 15:50:10 +00004970 cur = CUR;
William M. Brack10f1ef42004-03-20 14:51:25 +00004971 if ((cur != '-') || (NXT(1) == ']')) {
Daniel Veillard4255d502002-04-16 15:50:10 +00004972 xmlRegAtomAddRange(ctxt, ctxt->atom, ctxt->neg,
4973 XML_REGEXP_CHARVAL, start, end, NULL);
4974 return;
4975 }
4976 NEXT;
4977 cur = CUR;
4978 if (cur == '\\') {
4979 NEXT;
4980 cur = CUR;
4981 switch (cur) {
4982 case 'n': end = 0xA; break;
4983 case 'r': end = 0xD; break;
4984 case 't': end = 0x9; break;
4985 case '\\': case '|': case '.': case '-': case '^': case '?':
4986 case '*': case '+': case '{': case '}': case '(': case ')':
4987 case '[': case ']':
4988 end = cur; break;
4989 default:
4990 ERROR("Invalid escape value");
4991 return;
4992 }
William M. Brackdc99df92003-12-27 01:54:25 +00004993 len = 1;
Daniel Veillard4255d502002-04-16 15:50:10 +00004994 } else if ((cur != 0x5B) && (cur != 0x5D)) {
William M. Brackdc99df92003-12-27 01:54:25 +00004995 end = CUR_SCHAR(ctxt->cur, len);
Daniel Veillard4255d502002-04-16 15:50:10 +00004996 } else {
4997 ERROR("Expecting the end of a char range");
4998 return;
4999 }
William M. Brackdc99df92003-12-27 01:54:25 +00005000 NEXTL(len);
Daniel Veillard4255d502002-04-16 15:50:10 +00005001 /* TODO check that the values are acceptable character ranges for XML */
5002 if (end < start) {
5003 ERROR("End of range is before start of range");
5004 } else {
5005 xmlRegAtomAddRange(ctxt, ctxt->atom, ctxt->neg,
5006 XML_REGEXP_CHARVAL, start, end, NULL);
5007 }
5008 return;
5009}
5010
5011/**
5012 * xmlFAParsePosCharGroup:
Daniel Veillard441bc322002-04-20 17:38:48 +00005013 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00005014 *
5015 * [14] posCharGroup ::= ( charRange | charClassEsc )+
5016 */
5017static void
5018xmlFAParsePosCharGroup(xmlRegParserCtxtPtr ctxt) {
5019 do {
Daniel Veillard041b6872008-02-08 10:37:18 +00005020 if (CUR == '\\') {
Daniel Veillard4255d502002-04-16 15:50:10 +00005021 xmlFAParseCharClassEsc(ctxt);
5022 } else {
5023 xmlFAParseCharRange(ctxt);
5024 }
5025 } while ((CUR != ']') && (CUR != '^') && (CUR != '-') &&
Daniel Veillard777737e2006-10-17 21:23:17 +00005026 (CUR != 0) && (ctxt->error == 0));
Daniel Veillard4255d502002-04-16 15:50:10 +00005027}
5028
5029/**
5030 * xmlFAParseCharGroup:
Daniel Veillard441bc322002-04-20 17:38:48 +00005031 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00005032 *
5033 * [13] charGroup ::= posCharGroup | negCharGroup | charClassSub
5034 * [15] negCharGroup ::= '^' posCharGroup
5035 * [16] charClassSub ::= ( posCharGroup | negCharGroup ) '-' charClassExpr
5036 * [12] charClassExpr ::= '[' charGroup ']'
5037 */
5038static void
5039xmlFAParseCharGroup(xmlRegParserCtxtPtr ctxt) {
5040 int n = ctxt->neg;
5041 while ((CUR != ']') && (ctxt->error == 0)) {
5042 if (CUR == '^') {
5043 int neg = ctxt->neg;
5044
5045 NEXT;
5046 ctxt->neg = !ctxt->neg;
5047 xmlFAParsePosCharGroup(ctxt);
5048 ctxt->neg = neg;
William M. Brack10f1ef42004-03-20 14:51:25 +00005049 } else if ((CUR == '-') && (NXT(1) == '[')) {
Daniel Veillardf8b9de32003-11-24 14:27:26 +00005050 int neg = ctxt->neg;
Daniel Veillardf8b9de32003-11-24 14:27:26 +00005051 ctxt->neg = 2;
William M. Brack10f1ef42004-03-20 14:51:25 +00005052 NEXT; /* eat the '-' */
5053 NEXT; /* eat the '[' */
Daniel Veillard4255d502002-04-16 15:50:10 +00005054 xmlFAParseCharGroup(ctxt);
5055 if (CUR == ']') {
5056 NEXT;
5057 } else {
5058 ERROR("charClassExpr: ']' expected");
5059 break;
5060 }
Daniel Veillardf8b9de32003-11-24 14:27:26 +00005061 ctxt->neg = neg;
Daniel Veillard4255d502002-04-16 15:50:10 +00005062 break;
5063 } else if (CUR != ']') {
5064 xmlFAParsePosCharGroup(ctxt);
5065 }
5066 }
5067 ctxt->neg = n;
5068}
5069
5070/**
5071 * xmlFAParseCharClass:
Daniel Veillard441bc322002-04-20 17:38:48 +00005072 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00005073 *
5074 * [11] charClass ::= charClassEsc | charClassExpr
5075 * [12] charClassExpr ::= '[' charGroup ']'
5076 */
5077static void
5078xmlFAParseCharClass(xmlRegParserCtxtPtr ctxt) {
5079 if (CUR == '[') {
5080 NEXT;
5081 ctxt->atom = xmlRegNewAtom(ctxt, XML_REGEXP_RANGES);
5082 if (ctxt->atom == NULL)
5083 return;
5084 xmlFAParseCharGroup(ctxt);
5085 if (CUR == ']') {
5086 NEXT;
5087 } else {
5088 ERROR("xmlFAParseCharClass: ']' expected");
5089 }
5090 } else {
5091 xmlFAParseCharClassEsc(ctxt);
5092 }
5093}
5094
5095/**
5096 * xmlFAParseQuantExact:
Daniel Veillard441bc322002-04-20 17:38:48 +00005097 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00005098 *
5099 * [8] QuantExact ::= [0-9]+
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00005100 *
5101 * Returns 0 if success or -1 in case of error
Daniel Veillard4255d502002-04-16 15:50:10 +00005102 */
5103static int
5104xmlFAParseQuantExact(xmlRegParserCtxtPtr ctxt) {
5105 int ret = 0;
5106 int ok = 0;
5107
5108 while ((CUR >= '0') && (CUR <= '9')) {
5109 ret = ret * 10 + (CUR - '0');
5110 ok = 1;
5111 NEXT;
5112 }
5113 if (ok != 1) {
5114 return(-1);
5115 }
5116 return(ret);
5117}
5118
5119/**
5120 * xmlFAParseQuantifier:
Daniel Veillard441bc322002-04-20 17:38:48 +00005121 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00005122 *
5123 * [4] quantifier ::= [?*+] | ( '{' quantity '}' )
5124 * [5] quantity ::= quantRange | quantMin | QuantExact
5125 * [6] quantRange ::= QuantExact ',' QuantExact
5126 * [7] quantMin ::= QuantExact ','
5127 * [8] QuantExact ::= [0-9]+
5128 */
5129static int
5130xmlFAParseQuantifier(xmlRegParserCtxtPtr ctxt) {
5131 int cur;
5132
5133 cur = CUR;
5134 if ((cur == '?') || (cur == '*') || (cur == '+')) {
5135 if (ctxt->atom != NULL) {
5136 if (cur == '?')
5137 ctxt->atom->quant = XML_REGEXP_QUANT_OPT;
5138 else if (cur == '*')
5139 ctxt->atom->quant = XML_REGEXP_QUANT_MULT;
5140 else if (cur == '+')
5141 ctxt->atom->quant = XML_REGEXP_QUANT_PLUS;
5142 }
5143 NEXT;
5144 return(1);
5145 }
5146 if (cur == '{') {
5147 int min = 0, max = 0;
5148
5149 NEXT;
5150 cur = xmlFAParseQuantExact(ctxt);
5151 if (cur >= 0)
5152 min = cur;
5153 if (CUR == ',') {
5154 NEXT;
Daniel Veillardebe48c62003-12-03 12:12:27 +00005155 if (CUR == '}')
5156 max = INT_MAX;
5157 else {
5158 cur = xmlFAParseQuantExact(ctxt);
5159 if (cur >= 0)
5160 max = cur;
5161 else {
5162 ERROR("Improper quantifier");
5163 }
5164 }
Daniel Veillard4255d502002-04-16 15:50:10 +00005165 }
5166 if (CUR == '}') {
5167 NEXT;
5168 } else {
5169 ERROR("Unterminated quantifier");
5170 }
5171 if (max == 0)
5172 max = min;
5173 if (ctxt->atom != NULL) {
5174 ctxt->atom->quant = XML_REGEXP_QUANT_RANGE;
5175 ctxt->atom->min = min;
5176 ctxt->atom->max = max;
5177 }
5178 return(1);
5179 }
5180 return(0);
5181}
5182
5183/**
5184 * xmlFAParseAtom:
Daniel Veillard441bc322002-04-20 17:38:48 +00005185 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00005186 *
5187 * [9] atom ::= Char | charClass | ( '(' regExp ')' )
5188 */
5189static int
5190xmlFAParseAtom(xmlRegParserCtxtPtr ctxt) {
5191 int codepoint, len;
5192
5193 codepoint = xmlFAIsChar(ctxt);
5194 if (codepoint > 0) {
5195 ctxt->atom = xmlRegNewAtom(ctxt, XML_REGEXP_CHARVAL);
5196 if (ctxt->atom == NULL)
5197 return(-1);
5198 codepoint = CUR_SCHAR(ctxt->cur, len);
5199 ctxt->atom->codepoint = codepoint;
5200 NEXTL(len);
5201 return(1);
5202 } else if (CUR == '|') {
5203 return(0);
5204 } else if (CUR == 0) {
5205 return(0);
5206 } else if (CUR == ')') {
5207 return(0);
5208 } else if (CUR == '(') {
Daniel Veillard76d59b62007-08-22 16:29:21 +00005209 xmlRegStatePtr start, oldend, start0;
Daniel Veillard4255d502002-04-16 15:50:10 +00005210
5211 NEXT;
Daniel Veillard76d59b62007-08-22 16:29:21 +00005212 /*
5213 * this extra Epsilon transition is needed if we count with 0 allowed
5214 * unfortunately this can't be known at that point
5215 */
5216 xmlFAGenerateEpsilonTransition(ctxt, ctxt->state, NULL);
5217 start0 = ctxt->state;
Daniel Veillard4255d502002-04-16 15:50:10 +00005218 xmlFAGenerateEpsilonTransition(ctxt, ctxt->state, NULL);
5219 start = ctxt->state;
5220 oldend = ctxt->end;
5221 ctxt->end = NULL;
5222 ctxt->atom = NULL;
5223 xmlFAParseRegExp(ctxt, 0);
5224 if (CUR == ')') {
5225 NEXT;
5226 } else {
5227 ERROR("xmlFAParseAtom: expecting ')'");
5228 }
5229 ctxt->atom = xmlRegNewAtom(ctxt, XML_REGEXP_SUBREG);
5230 if (ctxt->atom == NULL)
5231 return(-1);
5232 ctxt->atom->start = start;
Daniel Veillard76d59b62007-08-22 16:29:21 +00005233 ctxt->atom->start0 = start0;
Daniel Veillard4255d502002-04-16 15:50:10 +00005234 ctxt->atom->stop = ctxt->state;
5235 ctxt->end = oldend;
5236 return(1);
5237 } else if ((CUR == '[') || (CUR == '\\') || (CUR == '.')) {
5238 xmlFAParseCharClass(ctxt);
5239 return(1);
5240 }
5241 return(0);
5242}
5243
5244/**
5245 * xmlFAParsePiece:
Daniel Veillard441bc322002-04-20 17:38:48 +00005246 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00005247 *
5248 * [3] piece ::= atom quantifier?
5249 */
5250static int
5251xmlFAParsePiece(xmlRegParserCtxtPtr ctxt) {
5252 int ret;
5253
5254 ctxt->atom = NULL;
5255 ret = xmlFAParseAtom(ctxt);
5256 if (ret == 0)
5257 return(0);
5258 if (ctxt->atom == NULL) {
5259 ERROR("internal: no atom generated");
5260 }
5261 xmlFAParseQuantifier(ctxt);
5262 return(1);
5263}
5264
5265/**
5266 * xmlFAParseBranch:
Daniel Veillard441bc322002-04-20 17:38:48 +00005267 * @ctxt: a regexp parser context
Daniel Veillard54eb0242006-03-21 23:17:57 +00005268 * @to: optional target to the end of the branch
5269 *
5270 * @to is used to optimize by removing duplicate path in automata
5271 * in expressions like (a|b)(c|d)
Daniel Veillard4255d502002-04-16 15:50:10 +00005272 *
5273 * [2] branch ::= piece*
5274 */
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00005275static int
Daniel Veillard54eb0242006-03-21 23:17:57 +00005276xmlFAParseBranch(xmlRegParserCtxtPtr ctxt, xmlRegStatePtr to) {
Daniel Veillard4255d502002-04-16 15:50:10 +00005277 xmlRegStatePtr previous;
Daniel Veillard4255d502002-04-16 15:50:10 +00005278 int ret;
5279
5280 previous = ctxt->state;
5281 ret = xmlFAParsePiece(ctxt);
5282 if (ret != 0) {
Daniel Veillard54eb0242006-03-21 23:17:57 +00005283 if (xmlFAGenerateTransitions(ctxt, previous,
5284 (CUR=='|' || CUR==')') ? to : NULL, ctxt->atom) < 0)
Daniel Veillard2cbf5962004-03-31 15:50:43 +00005285 return(-1);
5286 previous = ctxt->state;
Daniel Veillard4255d502002-04-16 15:50:10 +00005287 ctxt->atom = NULL;
5288 }
5289 while ((ret != 0) && (ctxt->error == 0)) {
5290 ret = xmlFAParsePiece(ctxt);
5291 if (ret != 0) {
Daniel Veillard54eb0242006-03-21 23:17:57 +00005292 if (xmlFAGenerateTransitions(ctxt, previous,
5293 (CUR=='|' || CUR==')') ? to : NULL, ctxt->atom) < 0)
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00005294 return(-1);
Daniel Veillard4255d502002-04-16 15:50:10 +00005295 previous = ctxt->state;
5296 ctxt->atom = NULL;
5297 }
5298 }
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00005299 return(0);
Daniel Veillard4255d502002-04-16 15:50:10 +00005300}
5301
5302/**
5303 * xmlFAParseRegExp:
Daniel Veillard441bc322002-04-20 17:38:48 +00005304 * @ctxt: a regexp parser context
William M. Brackddf71d62004-05-06 04:17:26 +00005305 * @top: is this the top-level expression ?
Daniel Veillard4255d502002-04-16 15:50:10 +00005306 *
5307 * [1] regExp ::= branch ( '|' branch )*
5308 */
5309static void
5310xmlFAParseRegExp(xmlRegParserCtxtPtr ctxt, int top) {
Daniel Veillardc7e3cc42004-09-28 12:33:52 +00005311 xmlRegStatePtr start, end;
Daniel Veillard4255d502002-04-16 15:50:10 +00005312
Daniel Veillard2cbf5962004-03-31 15:50:43 +00005313 /* if not top start should have been generated by an epsilon trans */
Daniel Veillard4255d502002-04-16 15:50:10 +00005314 start = ctxt->state;
Daniel Veillard2cbf5962004-03-31 15:50:43 +00005315 ctxt->end = NULL;
Daniel Veillard54eb0242006-03-21 23:17:57 +00005316 xmlFAParseBranch(ctxt, NULL);
Daniel Veillard2cbf5962004-03-31 15:50:43 +00005317 if (top) {
5318#ifdef DEBUG_REGEXP_GRAPH
5319 printf("State %d is final\n", ctxt->state->no);
5320#endif
5321 ctxt->state->type = XML_REGEXP_FINAL_STATE;
5322 }
Daniel Veillard4255d502002-04-16 15:50:10 +00005323 if (CUR != '|') {
5324 ctxt->end = ctxt->state;
5325 return;
5326 }
5327 end = ctxt->state;
5328 while ((CUR == '|') && (ctxt->error == 0)) {
5329 NEXT;
5330 ctxt->state = start;
Daniel Veillard2cbf5962004-03-31 15:50:43 +00005331 ctxt->end = NULL;
Daniel Veillard54eb0242006-03-21 23:17:57 +00005332 xmlFAParseBranch(ctxt, end);
Daniel Veillard4255d502002-04-16 15:50:10 +00005333 }
Daniel Veillard2cbf5962004-03-31 15:50:43 +00005334 if (!top) {
5335 ctxt->state = end;
5336 ctxt->end = end;
5337 }
Daniel Veillard4255d502002-04-16 15:50:10 +00005338}
5339
5340/************************************************************************
5341 * *
5342 * The basic API *
5343 * *
5344 ************************************************************************/
5345
5346/**
5347 * xmlRegexpPrint:
5348 * @output: the file for the output debug
5349 * @regexp: the compiled regexp
5350 *
5351 * Print the content of the compiled regular expression
5352 */
5353void
5354xmlRegexpPrint(FILE *output, xmlRegexpPtr regexp) {
5355 int i;
5356
Daniel Veillarda82b1822004-11-08 16:24:57 +00005357 if (output == NULL)
5358 return;
Daniel Veillard4255d502002-04-16 15:50:10 +00005359 fprintf(output, " regexp: ");
5360 if (regexp == NULL) {
5361 fprintf(output, "NULL\n");
5362 return;
5363 }
5364 fprintf(output, "'%s' ", regexp->string);
5365 fprintf(output, "\n");
5366 fprintf(output, "%d atoms:\n", regexp->nbAtoms);
5367 for (i = 0;i < regexp->nbAtoms; i++) {
5368 fprintf(output, " %02d ", i);
5369 xmlRegPrintAtom(output, regexp->atoms[i]);
5370 }
5371 fprintf(output, "%d states:", regexp->nbStates);
5372 fprintf(output, "\n");
5373 for (i = 0;i < regexp->nbStates; i++) {
5374 xmlRegPrintState(output, regexp->states[i]);
5375 }
5376 fprintf(output, "%d counters:\n", regexp->nbCounters);
5377 for (i = 0;i < regexp->nbCounters; i++) {
5378 fprintf(output, " %d: min %d max %d\n", i, regexp->counters[i].min,
5379 regexp->counters[i].max);
5380 }
5381}
5382
5383/**
5384 * xmlRegexpCompile:
5385 * @regexp: a regular expression string
5386 *
5387 * Parses a regular expression conforming to XML Schemas Part 2 Datatype
William M. Brackddf71d62004-05-06 04:17:26 +00005388 * Appendix F and builds an automata suitable for testing strings against
Daniel Veillard4255d502002-04-16 15:50:10 +00005389 * that regular expression
5390 *
5391 * Returns the compiled expression or NULL in case of error
5392 */
5393xmlRegexpPtr
5394xmlRegexpCompile(const xmlChar *regexp) {
5395 xmlRegexpPtr ret;
5396 xmlRegParserCtxtPtr ctxt;
5397
5398 ctxt = xmlRegNewParserCtxt(regexp);
5399 if (ctxt == NULL)
5400 return(NULL);
5401
5402 /* initialize the parser */
5403 ctxt->end = NULL;
5404 ctxt->start = ctxt->state = xmlRegNewState(ctxt);
5405 xmlRegStatePush(ctxt, ctxt->start);
5406
5407 /* parse the expression building an automata */
5408 xmlFAParseRegExp(ctxt, 1);
5409 if (CUR != 0) {
5410 ERROR("xmlFAParseRegExp: extra characters");
5411 }
Daniel Veillardcb4284e2007-04-25 13:55:20 +00005412 if (ctxt->error != 0) {
5413 xmlRegFreeParserCtxt(ctxt);
5414 return(NULL);
5415 }
Daniel Veillard4255d502002-04-16 15:50:10 +00005416 ctxt->end = ctxt->state;
5417 ctxt->start->type = XML_REGEXP_START_STATE;
5418 ctxt->end->type = XML_REGEXP_FINAL_STATE;
5419
5420 /* remove the Epsilon except for counted transitions */
5421 xmlFAEliminateEpsilonTransitions(ctxt);
5422
5423
5424 if (ctxt->error != 0) {
5425 xmlRegFreeParserCtxt(ctxt);
5426 return(NULL);
5427 }
5428 ret = xmlRegEpxFromParse(ctxt);
5429 xmlRegFreeParserCtxt(ctxt);
5430 return(ret);
5431}
5432
5433/**
5434 * xmlRegexpExec:
5435 * @comp: the compiled regular expression
5436 * @content: the value to check against the regular expression
5437 *
William M. Brackddf71d62004-05-06 04:17:26 +00005438 * Check if the regular expression generates the value
Daniel Veillard4255d502002-04-16 15:50:10 +00005439 *
William M. Brackddf71d62004-05-06 04:17:26 +00005440 * Returns 1 if it matches, 0 if not and a negative value in case of error
Daniel Veillard4255d502002-04-16 15:50:10 +00005441 */
5442int
5443xmlRegexpExec(xmlRegexpPtr comp, const xmlChar *content) {
5444 if ((comp == NULL) || (content == NULL))
5445 return(-1);
5446 return(xmlFARegExec(comp, content));
5447}
5448
5449/**
Daniel Veillard23e73572002-09-19 19:56:43 +00005450 * xmlRegexpIsDeterminist:
5451 * @comp: the compiled regular expression
5452 *
5453 * Check if the regular expression is determinist
5454 *
William M. Brackddf71d62004-05-06 04:17:26 +00005455 * Returns 1 if it yes, 0 if not and a negative value in case of error
Daniel Veillard23e73572002-09-19 19:56:43 +00005456 */
5457int
5458xmlRegexpIsDeterminist(xmlRegexpPtr comp) {
5459 xmlAutomataPtr am;
5460 int ret;
5461
5462 if (comp == NULL)
5463 return(-1);
5464 if (comp->determinist != -1)
5465 return(comp->determinist);
5466
5467 am = xmlNewAutomata();
Daniel Veillardbd9afb52002-09-25 22:25:35 +00005468 if (am->states != NULL) {
5469 int i;
5470
5471 for (i = 0;i < am->nbStates;i++)
5472 xmlRegFreeState(am->states[i]);
5473 xmlFree(am->states);
5474 }
Daniel Veillard23e73572002-09-19 19:56:43 +00005475 am->nbAtoms = comp->nbAtoms;
5476 am->atoms = comp->atoms;
5477 am->nbStates = comp->nbStates;
5478 am->states = comp->states;
5479 am->determinist = -1;
5480 ret = xmlFAComputesDeterminism(am);
5481 am->atoms = NULL;
5482 am->states = NULL;
5483 xmlFreeAutomata(am);
5484 return(ret);
5485}
5486
5487/**
Daniel Veillard4255d502002-04-16 15:50:10 +00005488 * xmlRegFreeRegexp:
5489 * @regexp: the regexp
5490 *
5491 * Free a regexp
5492 */
5493void
5494xmlRegFreeRegexp(xmlRegexpPtr regexp) {
5495 int i;
5496 if (regexp == NULL)
5497 return;
5498
5499 if (regexp->string != NULL)
5500 xmlFree(regexp->string);
5501 if (regexp->states != NULL) {
5502 for (i = 0;i < regexp->nbStates;i++)
5503 xmlRegFreeState(regexp->states[i]);
5504 xmlFree(regexp->states);
5505 }
5506 if (regexp->atoms != NULL) {
5507 for (i = 0;i < regexp->nbAtoms;i++)
5508 xmlRegFreeAtom(regexp->atoms[i]);
5509 xmlFree(regexp->atoms);
5510 }
5511 if (regexp->counters != NULL)
5512 xmlFree(regexp->counters);
Daniel Veillard23e73572002-09-19 19:56:43 +00005513 if (regexp->compact != NULL)
5514 xmlFree(regexp->compact);
Daniel Veillard118aed72002-09-24 14:13:13 +00005515 if (regexp->transdata != NULL)
5516 xmlFree(regexp->transdata);
Daniel Veillard23e73572002-09-19 19:56:43 +00005517 if (regexp->stringMap != NULL) {
5518 for (i = 0; i < regexp->nbstrings;i++)
5519 xmlFree(regexp->stringMap[i]);
5520 xmlFree(regexp->stringMap);
5521 }
5522
Daniel Veillard4255d502002-04-16 15:50:10 +00005523 xmlFree(regexp);
5524}
5525
5526#ifdef LIBXML_AUTOMATA_ENABLED
5527/************************************************************************
5528 * *
5529 * The Automata interface *
5530 * *
5531 ************************************************************************/
5532
5533/**
5534 * xmlNewAutomata:
5535 *
5536 * Create a new automata
5537 *
5538 * Returns the new object or NULL in case of failure
5539 */
5540xmlAutomataPtr
5541xmlNewAutomata(void) {
5542 xmlAutomataPtr ctxt;
5543
5544 ctxt = xmlRegNewParserCtxt(NULL);
5545 if (ctxt == NULL)
5546 return(NULL);
5547
5548 /* initialize the parser */
5549 ctxt->end = NULL;
5550 ctxt->start = ctxt->state = xmlRegNewState(ctxt);
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00005551 if (ctxt->start == NULL) {
5552 xmlFreeAutomata(ctxt);
5553 return(NULL);
5554 }
Daniel Veillardd0271472006-01-02 10:22:02 +00005555 ctxt->start->type = XML_REGEXP_START_STATE;
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00005556 if (xmlRegStatePush(ctxt, ctxt->start) < 0) {
5557 xmlRegFreeState(ctxt->start);
5558 xmlFreeAutomata(ctxt);
5559 return(NULL);
5560 }
Daniel Veillard4255d502002-04-16 15:50:10 +00005561
5562 return(ctxt);
5563}
5564
5565/**
5566 * xmlFreeAutomata:
5567 * @am: an automata
5568 *
5569 * Free an automata
5570 */
5571void
5572xmlFreeAutomata(xmlAutomataPtr am) {
5573 if (am == NULL)
5574 return;
5575 xmlRegFreeParserCtxt(am);
5576}
5577
5578/**
5579 * xmlAutomataGetInitState:
5580 * @am: an automata
5581 *
Daniel Veillarda9b66d02002-12-11 14:23:49 +00005582 * Initial state lookup
5583 *
Daniel Veillard4255d502002-04-16 15:50:10 +00005584 * Returns the initial state of the automata
5585 */
5586xmlAutomataStatePtr
5587xmlAutomataGetInitState(xmlAutomataPtr am) {
5588 if (am == NULL)
5589 return(NULL);
5590 return(am->start);
5591}
5592
5593/**
5594 * xmlAutomataSetFinalState:
5595 * @am: an automata
5596 * @state: a state in this automata
5597 *
5598 * Makes that state a final state
5599 *
5600 * Returns 0 or -1 in case of error
5601 */
5602int
5603xmlAutomataSetFinalState(xmlAutomataPtr am, xmlAutomataStatePtr state) {
5604 if ((am == NULL) || (state == NULL))
5605 return(-1);
5606 state->type = XML_REGEXP_FINAL_STATE;
5607 return(0);
5608}
5609
5610/**
5611 * xmlAutomataNewTransition:
5612 * @am: an automata
5613 * @from: the starting point of the transition
5614 * @to: the target point of the transition or NULL
5615 * @token: the input string associated to that transition
5616 * @data: data passed to the callback function if the transition is activated
5617 *
William M. Brackddf71d62004-05-06 04:17:26 +00005618 * If @to is NULL, this creates first a new target state in the automata
Daniel Veillard4255d502002-04-16 15:50:10 +00005619 * and then adds a transition from the @from state to the target state
5620 * activated by the value of @token
5621 *
5622 * Returns the target state or NULL in case of error
5623 */
5624xmlAutomataStatePtr
5625xmlAutomataNewTransition(xmlAutomataPtr am, xmlAutomataStatePtr from,
5626 xmlAutomataStatePtr to, const xmlChar *token,
5627 void *data) {
5628 xmlRegAtomPtr atom;
5629
5630 if ((am == NULL) || (from == NULL) || (token == NULL))
5631 return(NULL);
5632 atom = xmlRegNewAtom(am, XML_REGEXP_STRING);
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00005633 if (atom == NULL)
5634 return(NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +00005635 atom->data = data;
5636 if (atom == NULL)
5637 return(NULL);
5638 atom->valuep = xmlStrdup(token);
5639
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00005640 if (xmlFAGenerateTransitions(am, from, to, atom) < 0) {
5641 xmlRegFreeAtom(atom);
5642 return(NULL);
5643 }
Daniel Veillard4255d502002-04-16 15:50:10 +00005644 if (to == NULL)
5645 return(am->state);
5646 return(to);
5647}
5648
5649/**
Daniel Veillard52b48c72003-04-13 19:53:42 +00005650 * xmlAutomataNewTransition2:
5651 * @am: an automata
5652 * @from: the starting point of the transition
5653 * @to: the target point of the transition or NULL
5654 * @token: the first input string associated to that transition
5655 * @token2: the second input string associated to that transition
5656 * @data: data passed to the callback function if the transition is activated
5657 *
William M. Brackddf71d62004-05-06 04:17:26 +00005658 * If @to is NULL, this creates first a new target state in the automata
Daniel Veillard52b48c72003-04-13 19:53:42 +00005659 * and then adds a transition from the @from state to the target state
5660 * activated by the value of @token
5661 *
5662 * Returns the target state or NULL in case of error
5663 */
5664xmlAutomataStatePtr
5665xmlAutomataNewTransition2(xmlAutomataPtr am, xmlAutomataStatePtr from,
5666 xmlAutomataStatePtr to, const xmlChar *token,
5667 const xmlChar *token2, void *data) {
5668 xmlRegAtomPtr atom;
5669
5670 if ((am == NULL) || (from == NULL) || (token == NULL))
5671 return(NULL);
5672 atom = xmlRegNewAtom(am, XML_REGEXP_STRING);
Daniel Veillard52b48c72003-04-13 19:53:42 +00005673 if (atom == NULL)
5674 return(NULL);
Daniel Veillard11ce4002006-03-10 00:36:23 +00005675 atom->data = data;
Daniel Veillard52b48c72003-04-13 19:53:42 +00005676 if ((token2 == NULL) || (*token2 == 0)) {
5677 atom->valuep = xmlStrdup(token);
5678 } else {
5679 int lenn, lenp;
5680 xmlChar *str;
5681
5682 lenn = strlen((char *) token2);
5683 lenp = strlen((char *) token);
5684
Daniel Veillard3c908dc2003-04-19 00:07:51 +00005685 str = (xmlChar *) xmlMallocAtomic(lenn + lenp + 2);
Daniel Veillard52b48c72003-04-13 19:53:42 +00005686 if (str == NULL) {
5687 xmlRegFreeAtom(atom);
5688 return(NULL);
5689 }
5690 memcpy(&str[0], token, lenp);
5691 str[lenp] = '|';
5692 memcpy(&str[lenp + 1], token2, lenn);
5693 str[lenn + lenp + 1] = 0;
5694
5695 atom->valuep = str;
5696 }
5697
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00005698 if (xmlFAGenerateTransitions(am, from, to, atom) < 0) {
5699 xmlRegFreeAtom(atom);
5700 return(NULL);
5701 }
Daniel Veillard52b48c72003-04-13 19:53:42 +00005702 if (to == NULL)
5703 return(am->state);
5704 return(to);
5705}
5706
5707/**
Daniel Veillard9efc4762005-07-19 14:33:55 +00005708 * xmlAutomataNewNegTrans:
5709 * @am: an automata
5710 * @from: the starting point of the transition
5711 * @to: the target point of the transition or NULL
5712 * @token: the first input string associated to that transition
5713 * @token2: the second input string associated to that transition
5714 * @data: data passed to the callback function if the transition is activated
5715 *
5716 * If @to is NULL, this creates first a new target state in the automata
5717 * and then adds a transition from the @from state to the target state
5718 * activated by any value except (@token,@token2)
Daniel Veillard6e65e152005-08-09 11:09:52 +00005719 * Note that if @token2 is not NULL, then (X, NULL) won't match to follow
5720 # the semantic of XSD ##other
Daniel Veillard9efc4762005-07-19 14:33:55 +00005721 *
5722 * Returns the target state or NULL in case of error
5723 */
5724xmlAutomataStatePtr
5725xmlAutomataNewNegTrans(xmlAutomataPtr am, xmlAutomataStatePtr from,
5726 xmlAutomataStatePtr to, const xmlChar *token,
5727 const xmlChar *token2, void *data) {
5728 xmlRegAtomPtr atom;
Daniel Veillard77005e62005-07-19 16:26:18 +00005729 xmlChar err_msg[200];
Daniel Veillard9efc4762005-07-19 14:33:55 +00005730
5731 if ((am == NULL) || (from == NULL) || (token == NULL))
5732 return(NULL);
5733 atom = xmlRegNewAtom(am, XML_REGEXP_STRING);
5734 if (atom == NULL)
5735 return(NULL);
5736 atom->data = data;
5737 atom->neg = 1;
5738 if ((token2 == NULL) || (*token2 == 0)) {
5739 atom->valuep = xmlStrdup(token);
5740 } else {
5741 int lenn, lenp;
5742 xmlChar *str;
5743
5744 lenn = strlen((char *) token2);
5745 lenp = strlen((char *) token);
5746
5747 str = (xmlChar *) xmlMallocAtomic(lenn + lenp + 2);
5748 if (str == NULL) {
5749 xmlRegFreeAtom(atom);
5750 return(NULL);
5751 }
5752 memcpy(&str[0], token, lenp);
5753 str[lenp] = '|';
5754 memcpy(&str[lenp + 1], token2, lenn);
5755 str[lenn + lenp + 1] = 0;
5756
5757 atom->valuep = str;
5758 }
Daniel Veillarddb68b742005-07-30 13:18:24 +00005759 snprintf((char *) err_msg, 199, "not %s", (const char *) atom->valuep);
Daniel Veillard77005e62005-07-19 16:26:18 +00005760 err_msg[199] = 0;
5761 atom->valuep2 = xmlStrdup(err_msg);
Daniel Veillard9efc4762005-07-19 14:33:55 +00005762
5763 if (xmlFAGenerateTransitions(am, from, to, atom) < 0) {
5764 xmlRegFreeAtom(atom);
5765 return(NULL);
5766 }
Daniel Veillard6e65e152005-08-09 11:09:52 +00005767 am->negs++;
Daniel Veillard9efc4762005-07-19 14:33:55 +00005768 if (to == NULL)
5769 return(am->state);
5770 return(to);
5771}
5772
5773/**
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00005774 * xmlAutomataNewCountTrans2:
5775 * @am: an automata
5776 * @from: the starting point of the transition
5777 * @to: the target point of the transition or NULL
5778 * @token: the input string associated to that transition
5779 * @token2: the second input string associated to that transition
5780 * @min: the minimum successive occurences of token
5781 * @max: the maximum successive occurences of token
5782 * @data: data associated to the transition
5783 *
5784 * If @to is NULL, this creates first a new target state in the automata
5785 * and then adds a transition from the @from state to the target state
5786 * activated by a succession of input of value @token and @token2 and
5787 * whose number is between @min and @max
5788 *
5789 * Returns the target state or NULL in case of error
5790 */
5791xmlAutomataStatePtr
5792xmlAutomataNewCountTrans2(xmlAutomataPtr am, xmlAutomataStatePtr from,
5793 xmlAutomataStatePtr to, const xmlChar *token,
5794 const xmlChar *token2,
5795 int min, int max, void *data) {
5796 xmlRegAtomPtr atom;
5797 int counter;
5798
5799 if ((am == NULL) || (from == NULL) || (token == NULL))
5800 return(NULL);
5801 if (min < 0)
5802 return(NULL);
5803 if ((max < min) || (max < 1))
5804 return(NULL);
5805 atom = xmlRegNewAtom(am, XML_REGEXP_STRING);
5806 if (atom == NULL)
5807 return(NULL);
5808 if ((token2 == NULL) || (*token2 == 0)) {
5809 atom->valuep = xmlStrdup(token);
5810 } else {
5811 int lenn, lenp;
5812 xmlChar *str;
5813
5814 lenn = strlen((char *) token2);
5815 lenp = strlen((char *) token);
5816
5817 str = (xmlChar *) xmlMallocAtomic(lenn + lenp + 2);
5818 if (str == NULL) {
5819 xmlRegFreeAtom(atom);
5820 return(NULL);
5821 }
5822 memcpy(&str[0], token, lenp);
5823 str[lenp] = '|';
5824 memcpy(&str[lenp + 1], token2, lenn);
5825 str[lenn + lenp + 1] = 0;
5826
5827 atom->valuep = str;
5828 }
5829 atom->data = data;
5830 if (min == 0)
5831 atom->min = 1;
5832 else
5833 atom->min = min;
5834 atom->max = max;
5835
5836 /*
5837 * associate a counter to the transition.
5838 */
5839 counter = xmlRegGetCounter(am);
5840 am->counters[counter].min = min;
5841 am->counters[counter].max = max;
5842
5843 /* xmlFAGenerateTransitions(am, from, to, atom); */
5844 if (to == NULL) {
5845 to = xmlRegNewState(am);
5846 xmlRegStatePush(am, to);
5847 }
Daniel Veillard5de09382005-09-26 17:18:17 +00005848 xmlRegStateAddTrans(am, from, atom, to, counter, -1);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00005849 xmlRegAtomPush(am, atom);
5850 am->state = to;
5851
5852 if (to == NULL)
5853 to = am->state;
5854 if (to == NULL)
5855 return(NULL);
5856 if (min == 0)
5857 xmlFAGenerateEpsilonTransition(am, from, to);
5858 return(to);
5859}
5860
5861/**
Daniel Veillard4255d502002-04-16 15:50:10 +00005862 * xmlAutomataNewCountTrans:
5863 * @am: an automata
5864 * @from: the starting point of the transition
5865 * @to: the target point of the transition or NULL
5866 * @token: the input string associated to that transition
5867 * @min: the minimum successive occurences of token
Daniel Veillarda9b66d02002-12-11 14:23:49 +00005868 * @max: the maximum successive occurences of token
5869 * @data: data associated to the transition
Daniel Veillard4255d502002-04-16 15:50:10 +00005870 *
William M. Brackddf71d62004-05-06 04:17:26 +00005871 * If @to is NULL, this creates first a new target state in the automata
Daniel Veillard4255d502002-04-16 15:50:10 +00005872 * and then adds a transition from the @from state to the target state
5873 * activated by a succession of input of value @token and whose number
5874 * is between @min and @max
5875 *
5876 * Returns the target state or NULL in case of error
5877 */
5878xmlAutomataStatePtr
5879xmlAutomataNewCountTrans(xmlAutomataPtr am, xmlAutomataStatePtr from,
5880 xmlAutomataStatePtr to, const xmlChar *token,
5881 int min, int max, void *data) {
5882 xmlRegAtomPtr atom;
Daniel Veillard0ddb21c2004-02-12 12:43:49 +00005883 int counter;
Daniel Veillard4255d502002-04-16 15:50:10 +00005884
5885 if ((am == NULL) || (from == NULL) || (token == NULL))
5886 return(NULL);
5887 if (min < 0)
5888 return(NULL);
5889 if ((max < min) || (max < 1))
5890 return(NULL);
5891 atom = xmlRegNewAtom(am, XML_REGEXP_STRING);
5892 if (atom == NULL)
5893 return(NULL);
5894 atom->valuep = xmlStrdup(token);
5895 atom->data = data;
5896 if (min == 0)
5897 atom->min = 1;
5898 else
5899 atom->min = min;
5900 atom->max = max;
5901
Daniel Veillard0ddb21c2004-02-12 12:43:49 +00005902 /*
5903 * associate a counter to the transition.
5904 */
5905 counter = xmlRegGetCounter(am);
5906 am->counters[counter].min = min;
5907 am->counters[counter].max = max;
5908
5909 /* xmlFAGenerateTransitions(am, from, to, atom); */
5910 if (to == NULL) {
5911 to = xmlRegNewState(am);
5912 xmlRegStatePush(am, to);
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00005913 }
Daniel Veillard5de09382005-09-26 17:18:17 +00005914 xmlRegStateAddTrans(am, from, atom, to, counter, -1);
Daniel Veillard0ddb21c2004-02-12 12:43:49 +00005915 xmlRegAtomPush(am, atom);
5916 am->state = to;
5917
Daniel Veillard4255d502002-04-16 15:50:10 +00005918 if (to == NULL)
5919 to = am->state;
5920 if (to == NULL)
5921 return(NULL);
5922 if (min == 0)
5923 xmlFAGenerateEpsilonTransition(am, from, to);
5924 return(to);
5925}
5926
5927/**
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00005928 * xmlAutomataNewOnceTrans2:
5929 * @am: an automata
5930 * @from: the starting point of the transition
5931 * @to: the target point of the transition or NULL
5932 * @token: the input string associated to that transition
5933 * @token2: the second input string associated to that transition
5934 * @min: the minimum successive occurences of token
5935 * @max: the maximum successive occurences of token
5936 * @data: data associated to the transition
5937 *
5938 * If @to is NULL, this creates first a new target state in the automata
5939 * and then adds a transition from the @from state to the target state
5940 * activated by a succession of input of value @token and @token2 and whose
5941 * number is between @min and @max, moreover that transition can only be
5942 * crossed once.
5943 *
5944 * Returns the target state or NULL in case of error
5945 */
5946xmlAutomataStatePtr
5947xmlAutomataNewOnceTrans2(xmlAutomataPtr am, xmlAutomataStatePtr from,
5948 xmlAutomataStatePtr to, const xmlChar *token,
5949 const xmlChar *token2,
5950 int min, int max, void *data) {
5951 xmlRegAtomPtr atom;
5952 int counter;
5953
5954 if ((am == NULL) || (from == NULL) || (token == NULL))
5955 return(NULL);
5956 if (min < 1)
5957 return(NULL);
5958 if ((max < min) || (max < 1))
5959 return(NULL);
5960 atom = xmlRegNewAtom(am, XML_REGEXP_STRING);
5961 if (atom == NULL)
5962 return(NULL);
5963 if ((token2 == NULL) || (*token2 == 0)) {
5964 atom->valuep = xmlStrdup(token);
5965 } else {
5966 int lenn, lenp;
5967 xmlChar *str;
5968
5969 lenn = strlen((char *) token2);
5970 lenp = strlen((char *) token);
5971
5972 str = (xmlChar *) xmlMallocAtomic(lenn + lenp + 2);
5973 if (str == NULL) {
5974 xmlRegFreeAtom(atom);
5975 return(NULL);
5976 }
5977 memcpy(&str[0], token, lenp);
5978 str[lenp] = '|';
5979 memcpy(&str[lenp + 1], token2, lenn);
5980 str[lenn + lenp + 1] = 0;
5981
5982 atom->valuep = str;
5983 }
5984 atom->data = data;
5985 atom->quant = XML_REGEXP_QUANT_ONCEONLY;
Daniel Veillard11ce4002006-03-10 00:36:23 +00005986 atom->min = min;
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00005987 atom->max = max;
5988 /*
5989 * associate a counter to the transition.
5990 */
5991 counter = xmlRegGetCounter(am);
5992 am->counters[counter].min = 1;
5993 am->counters[counter].max = 1;
5994
5995 /* xmlFAGenerateTransitions(am, from, to, atom); */
5996 if (to == NULL) {
5997 to = xmlRegNewState(am);
5998 xmlRegStatePush(am, to);
5999 }
Daniel Veillard5de09382005-09-26 17:18:17 +00006000 xmlRegStateAddTrans(am, from, atom, to, counter, -1);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00006001 xmlRegAtomPush(am, atom);
6002 am->state = to;
6003 return(to);
6004}
6005
6006
6007
6008/**
Daniel Veillard7646b182002-04-20 06:41:40 +00006009 * xmlAutomataNewOnceTrans:
6010 * @am: an automata
6011 * @from: the starting point of the transition
6012 * @to: the target point of the transition or NULL
6013 * @token: the input string associated to that transition
6014 * @min: the minimum successive occurences of token
Daniel Veillarda9b66d02002-12-11 14:23:49 +00006015 * @max: the maximum successive occurences of token
6016 * @data: data associated to the transition
Daniel Veillard7646b182002-04-20 06:41:40 +00006017 *
William M. Brackddf71d62004-05-06 04:17:26 +00006018 * If @to is NULL, this creates first a new target state in the automata
Daniel Veillard7646b182002-04-20 06:41:40 +00006019 * and then adds a transition from the @from state to the target state
6020 * activated by a succession of input of value @token and whose number
William M. Brackddf71d62004-05-06 04:17:26 +00006021 * is between @min and @max, moreover that transition can only be crossed
Daniel Veillard7646b182002-04-20 06:41:40 +00006022 * once.
6023 *
6024 * Returns the target state or NULL in case of error
6025 */
6026xmlAutomataStatePtr
6027xmlAutomataNewOnceTrans(xmlAutomataPtr am, xmlAutomataStatePtr from,
6028 xmlAutomataStatePtr to, const xmlChar *token,
6029 int min, int max, void *data) {
6030 xmlRegAtomPtr atom;
6031 int counter;
6032
6033 if ((am == NULL) || (from == NULL) || (token == NULL))
6034 return(NULL);
6035 if (min < 1)
6036 return(NULL);
6037 if ((max < min) || (max < 1))
6038 return(NULL);
6039 atom = xmlRegNewAtom(am, XML_REGEXP_STRING);
6040 if (atom == NULL)
6041 return(NULL);
6042 atom->valuep = xmlStrdup(token);
6043 atom->data = data;
6044 atom->quant = XML_REGEXP_QUANT_ONCEONLY;
Daniel Veillard11ce4002006-03-10 00:36:23 +00006045 atom->min = min;
Daniel Veillard7646b182002-04-20 06:41:40 +00006046 atom->max = max;
6047 /*
6048 * associate a counter to the transition.
6049 */
6050 counter = xmlRegGetCounter(am);
6051 am->counters[counter].min = 1;
6052 am->counters[counter].max = 1;
6053
6054 /* xmlFAGenerateTransitions(am, from, to, atom); */
6055 if (to == NULL) {
6056 to = xmlRegNewState(am);
6057 xmlRegStatePush(am, to);
6058 }
Daniel Veillard5de09382005-09-26 17:18:17 +00006059 xmlRegStateAddTrans(am, from, atom, to, counter, -1);
Daniel Veillard7646b182002-04-20 06:41:40 +00006060 xmlRegAtomPush(am, atom);
6061 am->state = to;
Daniel Veillard7646b182002-04-20 06:41:40 +00006062 return(to);
6063}
6064
6065/**
Daniel Veillard4255d502002-04-16 15:50:10 +00006066 * xmlAutomataNewState:
6067 * @am: an automata
6068 *
6069 * Create a new disconnected state in the automata
6070 *
6071 * Returns the new state or NULL in case of error
6072 */
6073xmlAutomataStatePtr
6074xmlAutomataNewState(xmlAutomataPtr am) {
6075 xmlAutomataStatePtr to;
6076
6077 if (am == NULL)
6078 return(NULL);
6079 to = xmlRegNewState(am);
6080 xmlRegStatePush(am, to);
6081 return(to);
6082}
6083
6084/**
Daniel Veillarda9b66d02002-12-11 14:23:49 +00006085 * xmlAutomataNewEpsilon:
Daniel Veillard4255d502002-04-16 15:50:10 +00006086 * @am: an automata
6087 * @from: the starting point of the transition
6088 * @to: the target point of the transition or NULL
6089 *
William M. Brackddf71d62004-05-06 04:17:26 +00006090 * If @to is NULL, this creates first a new target state in the automata
6091 * and then adds an epsilon transition from the @from state to the
Daniel Veillard4255d502002-04-16 15:50:10 +00006092 * target state
6093 *
6094 * Returns the target state or NULL in case of error
6095 */
6096xmlAutomataStatePtr
6097xmlAutomataNewEpsilon(xmlAutomataPtr am, xmlAutomataStatePtr from,
6098 xmlAutomataStatePtr to) {
6099 if ((am == NULL) || (from == NULL))
6100 return(NULL);
6101 xmlFAGenerateEpsilonTransition(am, from, to);
6102 if (to == NULL)
6103 return(am->state);
6104 return(to);
6105}
6106
Daniel Veillardb509f152002-04-17 16:28:10 +00006107/**
Daniel Veillard7646b182002-04-20 06:41:40 +00006108 * xmlAutomataNewAllTrans:
6109 * @am: an automata
6110 * @from: the starting point of the transition
6111 * @to: the target point of the transition or NULL
Daniel Veillarda9b66d02002-12-11 14:23:49 +00006112 * @lax: allow to transition if not all all transitions have been activated
Daniel Veillard7646b182002-04-20 06:41:40 +00006113 *
William M. Brackddf71d62004-05-06 04:17:26 +00006114 * If @to is NULL, this creates first a new target state in the automata
Daniel Veillard7646b182002-04-20 06:41:40 +00006115 * and then adds a an ALL transition from the @from state to the
6116 * target state. That transition is an epsilon transition allowed only when
6117 * all transitions from the @from node have been activated.
6118 *
6119 * Returns the target state or NULL in case of error
6120 */
6121xmlAutomataStatePtr
6122xmlAutomataNewAllTrans(xmlAutomataPtr am, xmlAutomataStatePtr from,
Daniel Veillard441bc322002-04-20 17:38:48 +00006123 xmlAutomataStatePtr to, int lax) {
Daniel Veillard7646b182002-04-20 06:41:40 +00006124 if ((am == NULL) || (from == NULL))
6125 return(NULL);
Daniel Veillard441bc322002-04-20 17:38:48 +00006126 xmlFAGenerateAllTransition(am, from, to, lax);
Daniel Veillard7646b182002-04-20 06:41:40 +00006127 if (to == NULL)
6128 return(am->state);
6129 return(to);
6130}
6131
6132/**
Daniel Veillardb509f152002-04-17 16:28:10 +00006133 * xmlAutomataNewCounter:
6134 * @am: an automata
6135 * @min: the minimal value on the counter
6136 * @max: the maximal value on the counter
6137 *
6138 * Create a new counter
6139 *
6140 * Returns the counter number or -1 in case of error
6141 */
6142int
6143xmlAutomataNewCounter(xmlAutomataPtr am, int min, int max) {
6144 int ret;
6145
6146 if (am == NULL)
6147 return(-1);
6148
6149 ret = xmlRegGetCounter(am);
6150 if (ret < 0)
6151 return(-1);
6152 am->counters[ret].min = min;
6153 am->counters[ret].max = max;
6154 return(ret);
6155}
6156
6157/**
6158 * xmlAutomataNewCountedTrans:
6159 * @am: an automata
6160 * @from: the starting point of the transition
6161 * @to: the target point of the transition or NULL
6162 * @counter: the counter associated to that transition
6163 *
William M. Brackddf71d62004-05-06 04:17:26 +00006164 * If @to is NULL, this creates first a new target state in the automata
Daniel Veillardb509f152002-04-17 16:28:10 +00006165 * and then adds an epsilon transition from the @from state to the target state
6166 * which will increment the counter provided
6167 *
6168 * Returns the target state or NULL in case of error
6169 */
6170xmlAutomataStatePtr
6171xmlAutomataNewCountedTrans(xmlAutomataPtr am, xmlAutomataStatePtr from,
6172 xmlAutomataStatePtr to, int counter) {
6173 if ((am == NULL) || (from == NULL) || (counter < 0))
6174 return(NULL);
6175 xmlFAGenerateCountedEpsilonTransition(am, from, to, counter);
6176 if (to == NULL)
6177 return(am->state);
6178 return(to);
6179}
6180
6181/**
6182 * xmlAutomataNewCounterTrans:
6183 * @am: an automata
6184 * @from: the starting point of the transition
6185 * @to: the target point of the transition or NULL
6186 * @counter: the counter associated to that transition
6187 *
William M. Brackddf71d62004-05-06 04:17:26 +00006188 * If @to is NULL, this creates first a new target state in the automata
Daniel Veillardb509f152002-04-17 16:28:10 +00006189 * and then adds an epsilon transition from the @from state to the target state
6190 * which will be allowed only if the counter is within the right range.
6191 *
6192 * Returns the target state or NULL in case of error
6193 */
6194xmlAutomataStatePtr
6195xmlAutomataNewCounterTrans(xmlAutomataPtr am, xmlAutomataStatePtr from,
6196 xmlAutomataStatePtr to, int counter) {
6197 if ((am == NULL) || (from == NULL) || (counter < 0))
6198 return(NULL);
6199 xmlFAGenerateCountedTransition(am, from, to, counter);
6200 if (to == NULL)
6201 return(am->state);
6202 return(to);
6203}
Daniel Veillard4255d502002-04-16 15:50:10 +00006204
6205/**
6206 * xmlAutomataCompile:
6207 * @am: an automata
6208 *
6209 * Compile the automata into a Reg Exp ready for being executed.
6210 * The automata should be free after this point.
6211 *
6212 * Returns the compiled regexp or NULL in case of error
6213 */
6214xmlRegexpPtr
6215xmlAutomataCompile(xmlAutomataPtr am) {
6216 xmlRegexpPtr ret;
6217
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00006218 if ((am == NULL) || (am->error != 0)) return(NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +00006219 xmlFAEliminateEpsilonTransitions(am);
Daniel Veillard23e73572002-09-19 19:56:43 +00006220 /* xmlFAComputesDeterminism(am); */
Daniel Veillard4255d502002-04-16 15:50:10 +00006221 ret = xmlRegEpxFromParse(am);
6222
6223 return(ret);
6224}
Daniel Veillarde19fc232002-04-22 16:01:24 +00006225
6226/**
6227 * xmlAutomataIsDeterminist:
6228 * @am: an automata
6229 *
6230 * Checks if an automata is determinist.
6231 *
6232 * Returns 1 if true, 0 if not, and -1 in case of error
6233 */
6234int
6235xmlAutomataIsDeterminist(xmlAutomataPtr am) {
6236 int ret;
6237
6238 if (am == NULL)
6239 return(-1);
6240
6241 ret = xmlFAComputesDeterminism(am);
6242 return(ret);
6243}
Daniel Veillard4255d502002-04-16 15:50:10 +00006244#endif /* LIBXML_AUTOMATA_ENABLED */
Daniel Veillard81a8ec62005-08-22 00:20:58 +00006245
6246#ifdef LIBXML_EXPR_ENABLED
6247/************************************************************************
6248 * *
6249 * Formal Expression handling code *
6250 * *
6251 ************************************************************************/
Daniel Veillard81a8ec62005-08-22 00:20:58 +00006252/************************************************************************
6253 * *
6254 * Expression handling context *
6255 * *
6256 ************************************************************************/
6257
6258struct _xmlExpCtxt {
6259 xmlDictPtr dict;
6260 xmlExpNodePtr *table;
6261 int size;
6262 int nbElems;
6263 int nb_nodes;
6264 const char *expr;
6265 const char *cur;
6266 int nb_cons;
Daniel Veillard81a8ec62005-08-22 00:20:58 +00006267 int tabSize;
6268};
6269
6270/**
6271 * xmlExpNewCtxt:
6272 * @maxNodes: the maximum number of nodes
6273 * @dict: optional dictionnary to use internally
6274 *
6275 * Creates a new context for manipulating expressions
6276 *
6277 * Returns the context or NULL in case of error
6278 */
6279xmlExpCtxtPtr
6280xmlExpNewCtxt(int maxNodes, xmlDictPtr dict) {
6281 xmlExpCtxtPtr ret;
6282 int size = 256;
6283
6284 if (maxNodes <= 4096)
6285 maxNodes = 4096;
6286
6287 ret = (xmlExpCtxtPtr) xmlMalloc(sizeof(xmlExpCtxt));
6288 if (ret == NULL)
6289 return(NULL);
6290 memset(ret, 0, sizeof(xmlExpCtxt));
6291 ret->size = size;
6292 ret->nbElems = 0;
6293 ret->table = xmlMalloc(size * sizeof(xmlExpNodePtr));
6294 if (ret->table == NULL) {
6295 xmlFree(ret);
6296 return(NULL);
6297 }
6298 memset(ret->table, 0, size * sizeof(xmlExpNodePtr));
6299 if (dict == NULL) {
6300 ret->dict = xmlDictCreate();
6301 if (ret->dict == NULL) {
6302 xmlFree(ret->table);
6303 xmlFree(ret);
6304 return(NULL);
6305 }
6306 } else {
6307 ret->dict = dict;
6308 xmlDictReference(ret->dict);
6309 }
6310 return(ret);
6311}
6312
6313/**
6314 * xmlExpFreeCtxt:
6315 * @ctxt: an expression context
6316 *
6317 * Free an expression context
6318 */
6319void
6320xmlExpFreeCtxt(xmlExpCtxtPtr ctxt) {
6321 if (ctxt == NULL)
6322 return;
6323 xmlDictFree(ctxt->dict);
6324 if (ctxt->table != NULL)
6325 xmlFree(ctxt->table);
6326 xmlFree(ctxt);
6327}
6328
6329/************************************************************************
6330 * *
6331 * Structure associated to an expression node *
6332 * *
6333 ************************************************************************/
Daniel Veillard465a0002005-08-22 12:07:04 +00006334#define MAX_NODES 10000
6335
6336/* #define DEBUG_DERIV */
6337
6338/*
6339 * TODO:
6340 * - Wildcards
6341 * - public API for creation
6342 *
6343 * Started
6344 * - regression testing
6345 *
6346 * Done
6347 * - split into module and test tool
6348 * - memleaks
6349 */
Daniel Veillard81a8ec62005-08-22 00:20:58 +00006350
6351typedef enum {
6352 XML_EXP_NILABLE = (1 << 0)
6353} xmlExpNodeInfo;
6354
6355#define IS_NILLABLE(node) ((node)->info & XML_EXP_NILABLE)
6356
6357struct _xmlExpNode {
6358 unsigned char type;/* xmlExpNodeType */
6359 unsigned char info;/* OR of xmlExpNodeInfo */
6360 unsigned short key; /* the hash key */
6361 unsigned int ref; /* The number of references */
6362 int c_max; /* the maximum length it can consume */
6363 xmlExpNodePtr exp_left;
6364 xmlExpNodePtr next;/* the next node in the hash table or free list */
6365 union {
6366 struct {
6367 int f_min;
6368 int f_max;
6369 } count;
6370 struct {
6371 xmlExpNodePtr f_right;
6372 } children;
6373 const xmlChar *f_str;
6374 } field;
6375};
6376
6377#define exp_min field.count.f_min
6378#define exp_max field.count.f_max
6379/* #define exp_left field.children.f_left */
6380#define exp_right field.children.f_right
6381#define exp_str field.f_str
6382
6383static xmlExpNodePtr xmlExpNewNode(xmlExpCtxtPtr ctxt, xmlExpNodeType type);
6384static xmlExpNode forbiddenExpNode = {
6385 XML_EXP_FORBID, 0, 0, 0, 0, NULL, NULL, {{ 0, 0}}
6386};
6387xmlExpNodePtr forbiddenExp = &forbiddenExpNode;
6388static xmlExpNode emptyExpNode = {
6389 XML_EXP_EMPTY, 1, 0, 0, 0, NULL, NULL, {{ 0, 0}}
6390};
6391xmlExpNodePtr emptyExp = &emptyExpNode;
6392
6393/************************************************************************
6394 * *
6395 * The custom hash table for unicity and canonicalization *
6396 * of sub-expressions pointers *
6397 * *
6398 ************************************************************************/
6399/*
6400 * xmlExpHashNameComputeKey:
6401 * Calculate the hash key for a token
6402 */
6403static unsigned short
6404xmlExpHashNameComputeKey(const xmlChar *name) {
6405 unsigned short value = 0L;
6406 char ch;
6407
6408 if (name != NULL) {
6409 value += 30 * (*name);
6410 while ((ch = *name++) != 0) {
6411 value = value ^ ((value << 5) + (value >> 3) + (unsigned long)ch);
6412 }
6413 }
6414 return (value);
6415}
6416
6417/*
6418 * xmlExpHashComputeKey:
6419 * Calculate the hash key for a compound expression
6420 */
6421static unsigned short
6422xmlExpHashComputeKey(xmlExpNodeType type, xmlExpNodePtr left,
6423 xmlExpNodePtr right) {
6424 unsigned long value;
6425 unsigned short ret;
6426
6427 switch (type) {
6428 case XML_EXP_SEQ:
6429 value = left->key;
6430 value += right->key;
6431 value *= 3;
6432 ret = (unsigned short) value;
6433 break;
6434 case XML_EXP_OR:
6435 value = left->key;
6436 value += right->key;
6437 value *= 7;
6438 ret = (unsigned short) value;
6439 break;
6440 case XML_EXP_COUNT:
6441 value = left->key;
6442 value += right->key;
6443 ret = (unsigned short) value;
6444 break;
6445 default:
6446 ret = 0;
6447 }
6448 return(ret);
6449}
6450
6451
6452static xmlExpNodePtr
6453xmlExpNewNode(xmlExpCtxtPtr ctxt, xmlExpNodeType type) {
6454 xmlExpNodePtr ret;
6455
6456 if (ctxt->nb_nodes >= MAX_NODES)
6457 return(NULL);
6458 ret = (xmlExpNodePtr) xmlMalloc(sizeof(xmlExpNode));
6459 if (ret == NULL)
6460 return(NULL);
6461 memset(ret, 0, sizeof(xmlExpNode));
6462 ret->type = type;
6463 ret->next = NULL;
6464 ctxt->nb_nodes++;
6465 ctxt->nb_cons++;
6466 return(ret);
6467}
6468
6469/**
6470 * xmlExpHashGetEntry:
6471 * @table: the hash table
6472 *
6473 * Get the unique entry from the hash table. The entry is created if
6474 * needed. @left and @right are consumed, i.e. their ref count will
6475 * be decremented by the operation.
6476 *
6477 * Returns the pointer or NULL in case of error
6478 */
6479static xmlExpNodePtr
6480xmlExpHashGetEntry(xmlExpCtxtPtr ctxt, xmlExpNodeType type,
6481 xmlExpNodePtr left, xmlExpNodePtr right,
6482 const xmlChar *name, int min, int max) {
6483 unsigned short kbase, key;
6484 xmlExpNodePtr entry;
6485 xmlExpNodePtr insert;
6486
6487 if (ctxt == NULL)
6488 return(NULL);
6489
6490 /*
6491 * Check for duplicate and insertion location.
6492 */
6493 if (type == XML_EXP_ATOM) {
6494 kbase = xmlExpHashNameComputeKey(name);
6495 } else if (type == XML_EXP_COUNT) {
6496 /* COUNT reduction rule 1 */
6497 /* a{1} -> a */
6498 if (min == max) {
6499 if (min == 1) {
6500 return(left);
6501 }
6502 if (min == 0) {
6503 xmlExpFree(ctxt, left);
6504 return(emptyExp);
6505 }
6506 }
6507 if (min < 0) {
6508 xmlExpFree(ctxt, left);
6509 return(forbiddenExp);
6510 }
6511 if (max == -1)
6512 kbase = min + 79;
6513 else
6514 kbase = max - min;
6515 kbase += left->key;
6516 } else if (type == XML_EXP_OR) {
6517 /* Forbid reduction rules */
6518 if (left->type == XML_EXP_FORBID) {
6519 xmlExpFree(ctxt, left);
6520 return(right);
6521 }
6522 if (right->type == XML_EXP_FORBID) {
6523 xmlExpFree(ctxt, right);
6524 return(left);
6525 }
6526
6527 /* OR reduction rule 1 */
6528 /* a | a reduced to a */
6529 if (left == right) {
6530 left->ref--;
6531 return(left);
6532 }
6533 /* OR canonicalization rule 1 */
6534 /* linearize (a | b) | c into a | (b | c) */
6535 if ((left->type == XML_EXP_OR) && (right->type != XML_EXP_OR)) {
6536 xmlExpNodePtr tmp = left;
6537 left = right;
6538 right = tmp;
6539 }
6540 /* OR reduction rule 2 */
6541 /* a | (a | b) and b | (a | b) are reduced to a | b */
6542 if (right->type == XML_EXP_OR) {
6543 if ((left == right->exp_left) ||
6544 (left == right->exp_right)) {
6545 xmlExpFree(ctxt, left);
6546 return(right);
6547 }
6548 }
6549 /* OR canonicalization rule 2 */
6550 /* linearize (a | b) | c into a | (b | c) */
6551 if (left->type == XML_EXP_OR) {
6552 xmlExpNodePtr tmp;
6553
6554 /* OR canonicalization rule 2 */
6555 if ((left->exp_right->type != XML_EXP_OR) &&
6556 (left->exp_right->key < left->exp_left->key)) {
6557 tmp = left->exp_right;
6558 left->exp_right = left->exp_left;
6559 left->exp_left = tmp;
6560 }
6561 left->exp_right->ref++;
6562 tmp = xmlExpHashGetEntry(ctxt, XML_EXP_OR, left->exp_right, right,
6563 NULL, 0, 0);
6564 left->exp_left->ref++;
6565 tmp = xmlExpHashGetEntry(ctxt, XML_EXP_OR, left->exp_left, tmp,
6566 NULL, 0, 0);
6567
6568 xmlExpFree(ctxt, left);
6569 return(tmp);
6570 }
6571 if (right->type == XML_EXP_OR) {
6572 /* Ordering in the tree */
6573 /* C | (A | B) -> A | (B | C) */
6574 if (left->key > right->exp_right->key) {
6575 xmlExpNodePtr tmp;
6576 right->exp_right->ref++;
6577 tmp = xmlExpHashGetEntry(ctxt, XML_EXP_OR, right->exp_right,
6578 left, NULL, 0, 0);
6579 right->exp_left->ref++;
6580 tmp = xmlExpHashGetEntry(ctxt, XML_EXP_OR, right->exp_left,
6581 tmp, NULL, 0, 0);
6582 xmlExpFree(ctxt, right);
6583 return(tmp);
6584 }
6585 /* Ordering in the tree */
6586 /* B | (A | C) -> A | (B | C) */
6587 if (left->key > right->exp_left->key) {
6588 xmlExpNodePtr tmp;
6589 right->exp_right->ref++;
6590 tmp = xmlExpHashGetEntry(ctxt, XML_EXP_OR, left,
6591 right->exp_right, NULL, 0, 0);
6592 right->exp_left->ref++;
6593 tmp = xmlExpHashGetEntry(ctxt, XML_EXP_OR, right->exp_left,
6594 tmp, NULL, 0, 0);
6595 xmlExpFree(ctxt, right);
6596 return(tmp);
6597 }
6598 }
6599 /* we know both types are != XML_EXP_OR here */
6600 else if (left->key > right->key) {
6601 xmlExpNodePtr tmp = left;
6602 left = right;
6603 right = tmp;
6604 }
6605 kbase = xmlExpHashComputeKey(type, left, right);
6606 } else if (type == XML_EXP_SEQ) {
6607 /* Forbid reduction rules */
6608 if (left->type == XML_EXP_FORBID) {
6609 xmlExpFree(ctxt, right);
6610 return(left);
6611 }
6612 if (right->type == XML_EXP_FORBID) {
6613 xmlExpFree(ctxt, left);
6614 return(right);
6615 }
6616 /* Empty reduction rules */
6617 if (right->type == XML_EXP_EMPTY) {
6618 return(left);
6619 }
6620 if (left->type == XML_EXP_EMPTY) {
6621 return(right);
6622 }
6623 kbase = xmlExpHashComputeKey(type, left, right);
6624 } else
6625 return(NULL);
6626
6627 key = kbase % ctxt->size;
6628 if (ctxt->table[key] != NULL) {
6629 for (insert = ctxt->table[key]; insert != NULL;
6630 insert = insert->next) {
6631 if ((insert->key == kbase) &&
6632 (insert->type == type)) {
6633 if (type == XML_EXP_ATOM) {
6634 if (name == insert->exp_str) {
6635 insert->ref++;
6636 return(insert);
6637 }
6638 } else if (type == XML_EXP_COUNT) {
6639 if ((insert->exp_min == min) && (insert->exp_max == max) &&
6640 (insert->exp_left == left)) {
6641 insert->ref++;
6642 left->ref--;
6643 return(insert);
6644 }
6645 } else if ((insert->exp_left == left) &&
6646 (insert->exp_right == right)) {
6647 insert->ref++;
6648 left->ref--;
6649 right->ref--;
6650 return(insert);
6651 }
6652 }
6653 }
6654 }
6655
6656 entry = xmlExpNewNode(ctxt, type);
6657 if (entry == NULL)
6658 return(NULL);
6659 entry->key = kbase;
6660 if (type == XML_EXP_ATOM) {
6661 entry->exp_str = name;
6662 entry->c_max = 1;
6663 } else if (type == XML_EXP_COUNT) {
6664 entry->exp_min = min;
6665 entry->exp_max = max;
6666 entry->exp_left = left;
6667 if ((min == 0) || (IS_NILLABLE(left)))
6668 entry->info |= XML_EXP_NILABLE;
6669 if (max < 0)
6670 entry->c_max = -1;
6671 else
6672 entry->c_max = max * entry->exp_left->c_max;
6673 } else {
6674 entry->exp_left = left;
6675 entry->exp_right = right;
6676 if (type == XML_EXP_OR) {
6677 if ((IS_NILLABLE(left)) || (IS_NILLABLE(right)))
6678 entry->info |= XML_EXP_NILABLE;
6679 if ((entry->exp_left->c_max == -1) ||
6680 (entry->exp_right->c_max == -1))
6681 entry->c_max = -1;
6682 else if (entry->exp_left->c_max > entry->exp_right->c_max)
6683 entry->c_max = entry->exp_left->c_max;
6684 else
6685 entry->c_max = entry->exp_right->c_max;
6686 } else {
6687 if ((IS_NILLABLE(left)) && (IS_NILLABLE(right)))
6688 entry->info |= XML_EXP_NILABLE;
6689 if ((entry->exp_left->c_max == -1) ||
6690 (entry->exp_right->c_max == -1))
6691 entry->c_max = -1;
6692 else
6693 entry->c_max = entry->exp_left->c_max + entry->exp_right->c_max;
6694 }
6695 }
6696 entry->ref = 1;
6697 if (ctxt->table[key] != NULL)
6698 entry->next = ctxt->table[key];
6699
6700 ctxt->table[key] = entry;
6701 ctxt->nbElems++;
6702
6703 return(entry);
6704}
6705
6706/**
6707 * xmlExpFree:
6708 * @ctxt: the expression context
6709 * @exp: the expression
6710 *
6711 * Dereference the expression
6712 */
6713void
6714xmlExpFree(xmlExpCtxtPtr ctxt, xmlExpNodePtr exp) {
6715 if ((exp == NULL) || (exp == forbiddenExp) || (exp == emptyExp))
6716 return;
6717 exp->ref--;
Daniel Veillard81a8ec62005-08-22 00:20:58 +00006718 if (exp->ref == 0) {
6719 unsigned short key;
6720
6721 /* Unlink it first from the hash table */
6722 key = exp->key % ctxt->size;
6723 if (ctxt->table[key] == exp) {
6724 ctxt->table[key] = exp->next;
6725 } else {
6726 xmlExpNodePtr tmp;
6727
6728 tmp = ctxt->table[key];
6729 while (tmp != NULL) {
6730 if (tmp->next == exp) {
6731 tmp->next = exp->next;
6732 break;
6733 }
6734 tmp = tmp->next;
6735 }
6736 }
6737
6738 if ((exp->type == XML_EXP_SEQ) || (exp->type == XML_EXP_OR)) {
6739 xmlExpFree(ctxt, exp->exp_left);
6740 xmlExpFree(ctxt, exp->exp_right);
6741 } else if (exp->type == XML_EXP_COUNT) {
6742 xmlExpFree(ctxt, exp->exp_left);
6743 }
6744 xmlFree(exp);
Daniel Veillard81a8ec62005-08-22 00:20:58 +00006745 ctxt->nb_nodes--;
6746 }
6747}
6748
6749/**
6750 * xmlExpRef:
6751 * @exp: the expression
6752 *
6753 * Increase the reference count of the expression
6754 */
6755void
6756xmlExpRef(xmlExpNodePtr exp) {
6757 if (exp != NULL)
6758 exp->ref++;
6759}
6760
Daniel Veillardccb4d412005-08-23 13:41:17 +00006761/**
6762 * xmlExpNewAtom:
6763 * @ctxt: the expression context
6764 * @name: the atom name
6765 * @len: the atom name lenght in byte (or -1);
6766 *
6767 * Get the atom associated to this name from that context
6768 *
6769 * Returns the node or NULL in case of error
6770 */
6771xmlExpNodePtr
6772xmlExpNewAtom(xmlExpCtxtPtr ctxt, const xmlChar *name, int len) {
6773 if ((ctxt == NULL) || (name == NULL))
6774 return(NULL);
6775 name = xmlDictLookup(ctxt->dict, name, len);
6776 if (name == NULL)
6777 return(NULL);
6778 return(xmlExpHashGetEntry(ctxt, XML_EXP_ATOM, NULL, NULL, name, 0, 0));
6779}
6780
6781/**
6782 * xmlExpNewOr:
6783 * @ctxt: the expression context
6784 * @left: left expression
6785 * @right: right expression
6786 *
6787 * Get the atom associated to the choice @left | @right
6788 * Note that @left and @right are consumed in the operation, to keep
6789 * an handle on them use xmlExpRef() and use xmlExpFree() to release them,
6790 * this is true even in case of failure (unless ctxt == NULL).
6791 *
6792 * Returns the node or NULL in case of error
6793 */
6794xmlExpNodePtr
6795xmlExpNewOr(xmlExpCtxtPtr ctxt, xmlExpNodePtr left, xmlExpNodePtr right) {
Daniel Veillard11ce4002006-03-10 00:36:23 +00006796 if (ctxt == NULL)
6797 return(NULL);
6798 if ((left == NULL) || (right == NULL)) {
Daniel Veillardccb4d412005-08-23 13:41:17 +00006799 xmlExpFree(ctxt, left);
6800 xmlExpFree(ctxt, right);
6801 return(NULL);
6802 }
6803 return(xmlExpHashGetEntry(ctxt, XML_EXP_OR, left, right, NULL, 0, 0));
6804}
6805
6806/**
6807 * xmlExpNewSeq:
6808 * @ctxt: the expression context
6809 * @left: left expression
6810 * @right: right expression
6811 *
6812 * Get the atom associated to the sequence @left , @right
6813 * Note that @left and @right are consumed in the operation, to keep
6814 * an handle on them use xmlExpRef() and use xmlExpFree() to release them,
6815 * this is true even in case of failure (unless ctxt == NULL).
6816 *
6817 * Returns the node or NULL in case of error
6818 */
6819xmlExpNodePtr
6820xmlExpNewSeq(xmlExpCtxtPtr ctxt, xmlExpNodePtr left, xmlExpNodePtr right) {
Daniel Veillard11ce4002006-03-10 00:36:23 +00006821 if (ctxt == NULL)
6822 return(NULL);
6823 if ((left == NULL) || (right == NULL)) {
Daniel Veillardccb4d412005-08-23 13:41:17 +00006824 xmlExpFree(ctxt, left);
6825 xmlExpFree(ctxt, right);
6826 return(NULL);
6827 }
6828 return(xmlExpHashGetEntry(ctxt, XML_EXP_SEQ, left, right, NULL, 0, 0));
6829}
6830
6831/**
6832 * xmlExpNewRange:
6833 * @ctxt: the expression context
6834 * @subset: the expression to be repeated
6835 * @min: the lower bound for the repetition
6836 * @max: the upper bound for the repetition, -1 means infinite
6837 *
6838 * Get the atom associated to the range (@subset){@min, @max}
6839 * Note that @subset is consumed in the operation, to keep
6840 * an handle on it use xmlExpRef() and use xmlExpFree() to release it,
6841 * this is true even in case of failure (unless ctxt == NULL).
6842 *
6843 * Returns the node or NULL in case of error
6844 */
6845xmlExpNodePtr
6846xmlExpNewRange(xmlExpCtxtPtr ctxt, xmlExpNodePtr subset, int min, int max) {
Daniel Veillard11ce4002006-03-10 00:36:23 +00006847 if (ctxt == NULL)
6848 return(NULL);
6849 if ((subset == NULL) || (min < 0) || (max < -1) ||
Daniel Veillardccb4d412005-08-23 13:41:17 +00006850 ((max >= 0) && (min > max))) {
6851 xmlExpFree(ctxt, subset);
6852 return(NULL);
6853 }
6854 return(xmlExpHashGetEntry(ctxt, XML_EXP_COUNT, subset,
6855 NULL, NULL, min, max));
6856}
6857
Daniel Veillard81a8ec62005-08-22 00:20:58 +00006858/************************************************************************
6859 * *
6860 * Public API for operations on expressions *
6861 * *
6862 ************************************************************************/
6863
6864static int
6865xmlExpGetLanguageInt(xmlExpCtxtPtr ctxt, xmlExpNodePtr exp,
6866 const xmlChar**list, int len, int nb) {
6867 int tmp, tmp2;
6868tail:
6869 switch (exp->type) {
6870 case XML_EXP_EMPTY:
6871 return(0);
6872 case XML_EXP_ATOM:
6873 for (tmp = 0;tmp < nb;tmp++)
6874 if (list[tmp] == exp->exp_str)
6875 return(0);
6876 if (nb >= len)
6877 return(-2);
6878 list[nb++] = exp->exp_str;
6879 return(1);
6880 case XML_EXP_COUNT:
6881 exp = exp->exp_left;
6882 goto tail;
6883 case XML_EXP_SEQ:
6884 case XML_EXP_OR:
6885 tmp = xmlExpGetLanguageInt(ctxt, exp->exp_left, list, len, nb);
6886 if (tmp < 0)
6887 return(tmp);
6888 tmp2 = xmlExpGetLanguageInt(ctxt, exp->exp_right, list, len,
6889 nb + tmp);
6890 if (tmp2 < 0)
6891 return(tmp2);
6892 return(tmp + tmp2);
6893 }
6894 return(-1);
6895}
6896
6897/**
6898 * xmlExpGetLanguage:
6899 * @ctxt: the expression context
6900 * @exp: the expression
Daniel Veillard7802ba52005-10-27 11:56:20 +00006901 * @langList: where to store the tokens
Daniel Veillard81a8ec62005-08-22 00:20:58 +00006902 * @len: the allocated lenght of @list
6903 *
6904 * Find all the strings used in @exp and store them in @list
6905 *
6906 * Returns the number of unique strings found, -1 in case of errors and
6907 * -2 if there is more than @len strings
6908 */
6909int
6910xmlExpGetLanguage(xmlExpCtxtPtr ctxt, xmlExpNodePtr exp,
Daniel Veillard7802ba52005-10-27 11:56:20 +00006911 const xmlChar**langList, int len) {
6912 if ((ctxt == NULL) || (exp == NULL) || (langList == NULL) || (len <= 0))
Daniel Veillard81a8ec62005-08-22 00:20:58 +00006913 return(-1);
Daniel Veillard7802ba52005-10-27 11:56:20 +00006914 return(xmlExpGetLanguageInt(ctxt, exp, langList, len, 0));
Daniel Veillard81a8ec62005-08-22 00:20:58 +00006915}
6916
6917static int
6918xmlExpGetStartInt(xmlExpCtxtPtr ctxt, xmlExpNodePtr exp,
6919 const xmlChar**list, int len, int nb) {
6920 int tmp, tmp2;
6921tail:
6922 switch (exp->type) {
6923 case XML_EXP_FORBID:
6924 return(0);
6925 case XML_EXP_EMPTY:
6926 return(0);
6927 case XML_EXP_ATOM:
6928 for (tmp = 0;tmp < nb;tmp++)
6929 if (list[tmp] == exp->exp_str)
6930 return(0);
6931 if (nb >= len)
6932 return(-2);
6933 list[nb++] = exp->exp_str;
6934 return(1);
6935 case XML_EXP_COUNT:
6936 exp = exp->exp_left;
6937 goto tail;
6938 case XML_EXP_SEQ:
6939 tmp = xmlExpGetStartInt(ctxt, exp->exp_left, list, len, nb);
6940 if (tmp < 0)
6941 return(tmp);
6942 if (IS_NILLABLE(exp->exp_left)) {
6943 tmp2 = xmlExpGetStartInt(ctxt, exp->exp_right, list, len,
6944 nb + tmp);
6945 if (tmp2 < 0)
6946 return(tmp2);
6947 tmp += tmp2;
6948 }
6949 return(tmp);
6950 case XML_EXP_OR:
6951 tmp = xmlExpGetStartInt(ctxt, exp->exp_left, list, len, nb);
6952 if (tmp < 0)
6953 return(tmp);
6954 tmp2 = xmlExpGetStartInt(ctxt, exp->exp_right, list, len,
6955 nb + tmp);
6956 if (tmp2 < 0)
6957 return(tmp2);
6958 return(tmp + tmp2);
6959 }
6960 return(-1);
6961}
6962
6963/**
6964 * xmlExpGetStart:
6965 * @ctxt: the expression context
6966 * @exp: the expression
Daniel Veillard7802ba52005-10-27 11:56:20 +00006967 * @tokList: where to store the tokens
Daniel Veillard81a8ec62005-08-22 00:20:58 +00006968 * @len: the allocated lenght of @list
6969 *
6970 * Find all the strings that appears at the start of the languages
6971 * accepted by @exp and store them in @list. E.g. for (a, b) | c
6972 * it will return the list [a, c]
6973 *
6974 * Returns the number of unique strings found, -1 in case of errors and
6975 * -2 if there is more than @len strings
6976 */
6977int
6978xmlExpGetStart(xmlExpCtxtPtr ctxt, xmlExpNodePtr exp,
Daniel Veillard7802ba52005-10-27 11:56:20 +00006979 const xmlChar**tokList, int len) {
6980 if ((ctxt == NULL) || (exp == NULL) || (tokList == NULL) || (len <= 0))
Daniel Veillard81a8ec62005-08-22 00:20:58 +00006981 return(-1);
Daniel Veillard7802ba52005-10-27 11:56:20 +00006982 return(xmlExpGetStartInt(ctxt, exp, tokList, len, 0));
Daniel Veillard81a8ec62005-08-22 00:20:58 +00006983}
6984
6985/**
6986 * xmlExpIsNillable:
6987 * @exp: the expression
6988 *
6989 * Finds if the expression is nillable, i.e. if it accepts the empty sequqnce
6990 *
6991 * Returns 1 if nillable, 0 if not and -1 in case of error
6992 */
6993int
6994xmlExpIsNillable(xmlExpNodePtr exp) {
6995 if (exp == NULL)
6996 return(-1);
6997 return(IS_NILLABLE(exp) != 0);
6998}
6999
7000static xmlExpNodePtr
7001xmlExpStringDeriveInt(xmlExpCtxtPtr ctxt, xmlExpNodePtr exp, const xmlChar *str)
7002{
7003 xmlExpNodePtr ret;
7004
7005 switch (exp->type) {
7006 case XML_EXP_EMPTY:
7007 return(forbiddenExp);
7008 case XML_EXP_FORBID:
7009 return(forbiddenExp);
7010 case XML_EXP_ATOM:
7011 if (exp->exp_str == str) {
7012#ifdef DEBUG_DERIV
7013 printf("deriv atom: equal => Empty\n");
7014#endif
7015 ret = emptyExp;
7016 } else {
7017#ifdef DEBUG_DERIV
7018 printf("deriv atom: mismatch => forbid\n");
7019#endif
7020 /* TODO wildcards here */
7021 ret = forbiddenExp;
7022 }
7023 return(ret);
7024 case XML_EXP_OR: {
7025 xmlExpNodePtr tmp;
7026
7027#ifdef DEBUG_DERIV
7028 printf("deriv or: => or(derivs)\n");
7029#endif
7030 tmp = xmlExpStringDeriveInt(ctxt, exp->exp_left, str);
7031 if (tmp == NULL) {
7032 return(NULL);
7033 }
7034 ret = xmlExpStringDeriveInt(ctxt, exp->exp_right, str);
7035 if (ret == NULL) {
7036 xmlExpFree(ctxt, tmp);
7037 return(NULL);
7038 }
7039 ret = xmlExpHashGetEntry(ctxt, XML_EXP_OR, tmp, ret,
7040 NULL, 0, 0);
7041 return(ret);
7042 }
7043 case XML_EXP_SEQ:
7044#ifdef DEBUG_DERIV
7045 printf("deriv seq: starting with left\n");
7046#endif
7047 ret = xmlExpStringDeriveInt(ctxt, exp->exp_left, str);
7048 if (ret == NULL) {
7049 return(NULL);
7050 } else if (ret == forbiddenExp) {
7051 if (IS_NILLABLE(exp->exp_left)) {
7052#ifdef DEBUG_DERIV
7053 printf("deriv seq: left failed but nillable\n");
7054#endif
7055 ret = xmlExpStringDeriveInt(ctxt, exp->exp_right, str);
7056 }
7057 } else {
7058#ifdef DEBUG_DERIV
7059 printf("deriv seq: left match => sequence\n");
7060#endif
7061 exp->exp_right->ref++;
7062 ret = xmlExpHashGetEntry(ctxt, XML_EXP_SEQ, ret, exp->exp_right,
7063 NULL, 0, 0);
7064 }
7065 return(ret);
7066 case XML_EXP_COUNT: {
7067 int min, max;
7068 xmlExpNodePtr tmp;
7069
7070 if (exp->exp_max == 0)
7071 return(forbiddenExp);
7072 ret = xmlExpStringDeriveInt(ctxt, exp->exp_left, str);
7073 if (ret == NULL)
7074 return(NULL);
7075 if (ret == forbiddenExp) {
7076#ifdef DEBUG_DERIV
7077 printf("deriv count: pattern mismatch => forbid\n");
7078#endif
7079 return(ret);
7080 }
7081 if (exp->exp_max == 1)
7082 return(ret);
7083 if (exp->exp_max < 0) /* unbounded */
7084 max = -1;
7085 else
7086 max = exp->exp_max - 1;
7087 if (exp->exp_min > 0)
7088 min = exp->exp_min - 1;
7089 else
7090 min = 0;
7091 exp->exp_left->ref++;
7092 tmp = xmlExpHashGetEntry(ctxt, XML_EXP_COUNT, exp->exp_left, NULL,
7093 NULL, min, max);
7094 if (ret == emptyExp) {
7095#ifdef DEBUG_DERIV
7096 printf("deriv count: match to empty => new count\n");
7097#endif
7098 return(tmp);
7099 }
7100#ifdef DEBUG_DERIV
7101 printf("deriv count: match => sequence with new count\n");
7102#endif
7103 return(xmlExpHashGetEntry(ctxt, XML_EXP_SEQ, ret, tmp,
7104 NULL, 0, 0));
7105 }
7106 }
7107 return(NULL);
7108}
7109
7110/**
7111 * xmlExpStringDerive:
7112 * @ctxt: the expression context
7113 * @exp: the expression
7114 * @str: the string
7115 * @len: the string len in bytes if available
7116 *
7117 * Do one step of Brzozowski derivation of the expression @exp with
7118 * respect to the input string
7119 *
7120 * Returns the resulting expression or NULL in case of internal error
7121 */
7122xmlExpNodePtr
7123xmlExpStringDerive(xmlExpCtxtPtr ctxt, xmlExpNodePtr exp,
7124 const xmlChar *str, int len) {
7125 const xmlChar *input;
7126
7127 if ((exp == NULL) || (ctxt == NULL) || (str == NULL)) {
7128 return(NULL);
7129 }
7130 /*
7131 * check the string is in the dictionnary, if yes use an interned
7132 * copy, otherwise we know it's not an acceptable input
7133 */
7134 input = xmlDictExists(ctxt->dict, str, len);
7135 if (input == NULL) {
7136 return(forbiddenExp);
7137 }
7138 return(xmlExpStringDeriveInt(ctxt, exp, input));
7139}
7140
7141static int
7142xmlExpCheckCard(xmlExpNodePtr exp, xmlExpNodePtr sub) {
7143 int ret = 1;
7144
7145 if (sub->c_max == -1) {
7146 if (exp->c_max != -1)
7147 ret = 0;
7148 } else if ((exp->c_max >= 0) && (exp->c_max < sub->c_max)) {
7149 ret = 0;
7150 }
7151#if 0
7152 if ((IS_NILLABLE(sub)) && (!IS_NILLABLE(exp)))
7153 ret = 0;
7154#endif
7155 return(ret);
7156}
7157
7158static xmlExpNodePtr xmlExpExpDeriveInt(xmlExpCtxtPtr ctxt, xmlExpNodePtr exp,
7159 xmlExpNodePtr sub);
7160/**
7161 * xmlExpDivide:
7162 * @ctxt: the expressions context
7163 * @exp: the englobing expression
7164 * @sub: the subexpression
7165 * @mult: the multiple expression
7166 * @remain: the remain from the derivation of the multiple
7167 *
7168 * Check if exp is a multiple of sub, i.e. if there is a finite number n
7169 * so that sub{n} subsume exp
7170 *
7171 * Returns the multiple value if successful, 0 if it is not a multiple
7172 * and -1 in case of internel error.
7173 */
7174
7175static int
7176xmlExpDivide(xmlExpCtxtPtr ctxt, xmlExpNodePtr exp, xmlExpNodePtr sub,
7177 xmlExpNodePtr *mult, xmlExpNodePtr *remain) {
7178 int i;
7179 xmlExpNodePtr tmp, tmp2;
7180
7181 if (mult != NULL) *mult = NULL;
7182 if (remain != NULL) *remain = NULL;
7183 if (exp->c_max == -1) return(0);
7184 if (IS_NILLABLE(exp) && (!IS_NILLABLE(sub))) return(0);
7185
7186 for (i = 1;i <= exp->c_max;i++) {
7187 sub->ref++;
7188 tmp = xmlExpHashGetEntry(ctxt, XML_EXP_COUNT,
7189 sub, NULL, NULL, i, i);
7190 if (tmp == NULL) {
7191 return(-1);
7192 }
7193 if (!xmlExpCheckCard(tmp, exp)) {
7194 xmlExpFree(ctxt, tmp);
7195 continue;
7196 }
7197 tmp2 = xmlExpExpDeriveInt(ctxt, tmp, exp);
7198 if (tmp2 == NULL) {
7199 xmlExpFree(ctxt, tmp);
7200 return(-1);
7201 }
7202 if ((tmp2 != forbiddenExp) && (IS_NILLABLE(tmp2))) {
7203 if (remain != NULL)
7204 *remain = tmp2;
7205 else
7206 xmlExpFree(ctxt, tmp2);
7207 if (mult != NULL)
7208 *mult = tmp;
7209 else
7210 xmlExpFree(ctxt, tmp);
7211#ifdef DEBUG_DERIV
7212 printf("Divide succeeded %d\n", i);
7213#endif
7214 return(i);
7215 }
7216 xmlExpFree(ctxt, tmp);
7217 xmlExpFree(ctxt, tmp2);
7218 }
7219#ifdef DEBUG_DERIV
7220 printf("Divide failed\n");
7221#endif
7222 return(0);
7223}
7224
7225/**
7226 * xmlExpExpDeriveInt:
7227 * @ctxt: the expressions context
7228 * @exp: the englobing expression
7229 * @sub: the subexpression
7230 *
7231 * Try to do a step of Brzozowski derivation but at a higher level
7232 * the input being a subexpression.
7233 *
7234 * Returns the resulting expression or NULL in case of internal error
7235 */
7236static xmlExpNodePtr
7237xmlExpExpDeriveInt(xmlExpCtxtPtr ctxt, xmlExpNodePtr exp, xmlExpNodePtr sub) {
7238 xmlExpNodePtr ret, tmp, tmp2, tmp3;
7239 const xmlChar **tab;
7240 int len, i;
7241
7242 /*
7243 * In case of equality and if the expression can only consume a finite
7244 * amount, then the derivation is empty
7245 */
7246 if ((exp == sub) && (exp->c_max >= 0)) {
7247#ifdef DEBUG_DERIV
7248 printf("Equal(exp, sub) and finite -> Empty\n");
7249#endif
7250 return(emptyExp);
7251 }
7252 /*
7253 * decompose sub sequence first
7254 */
7255 if (sub->type == XML_EXP_EMPTY) {
7256#ifdef DEBUG_DERIV
7257 printf("Empty(sub) -> Empty\n");
7258#endif
7259 exp->ref++;
7260 return(exp);
7261 }
7262 if (sub->type == XML_EXP_SEQ) {
7263#ifdef DEBUG_DERIV
7264 printf("Seq(sub) -> decompose\n");
7265#endif
7266 tmp = xmlExpExpDeriveInt(ctxt, exp, sub->exp_left);
7267 if (tmp == NULL)
7268 return(NULL);
7269 if (tmp == forbiddenExp)
7270 return(tmp);
7271 ret = xmlExpExpDeriveInt(ctxt, tmp, sub->exp_right);
7272 xmlExpFree(ctxt, tmp);
7273 return(ret);
7274 }
7275 if (sub->type == XML_EXP_OR) {
7276#ifdef DEBUG_DERIV
7277 printf("Or(sub) -> decompose\n");
7278#endif
7279 tmp = xmlExpExpDeriveInt(ctxt, exp, sub->exp_left);
7280 if (tmp == forbiddenExp)
7281 return(tmp);
7282 if (tmp == NULL)
7283 return(NULL);
7284 ret = xmlExpExpDeriveInt(ctxt, exp, sub->exp_right);
7285 if ((ret == NULL) || (ret == forbiddenExp)) {
7286 xmlExpFree(ctxt, tmp);
7287 return(ret);
7288 }
7289 return(xmlExpHashGetEntry(ctxt, XML_EXP_OR, tmp, ret, NULL, 0, 0));
7290 }
7291 if (!xmlExpCheckCard(exp, sub)) {
7292#ifdef DEBUG_DERIV
7293 printf("CheckCard(exp, sub) failed -> Forbid\n");
7294#endif
7295 return(forbiddenExp);
7296 }
7297 switch (exp->type) {
7298 case XML_EXP_EMPTY:
7299 if (sub == emptyExp)
7300 return(emptyExp);
7301#ifdef DEBUG_DERIV
7302 printf("Empty(exp) -> Forbid\n");
7303#endif
7304 return(forbiddenExp);
7305 case XML_EXP_FORBID:
7306#ifdef DEBUG_DERIV
7307 printf("Forbid(exp) -> Forbid\n");
7308#endif
7309 return(forbiddenExp);
7310 case XML_EXP_ATOM:
7311 if (sub->type == XML_EXP_ATOM) {
7312 /* TODO: handle wildcards */
7313 if (exp->exp_str == sub->exp_str) {
7314#ifdef DEBUG_DERIV
7315 printf("Atom match -> Empty\n");
7316#endif
7317 return(emptyExp);
7318 }
7319#ifdef DEBUG_DERIV
7320 printf("Atom mismatch -> Forbid\n");
7321#endif
7322 return(forbiddenExp);
7323 }
7324 if ((sub->type == XML_EXP_COUNT) &&
7325 (sub->exp_max == 1) &&
7326 (sub->exp_left->type == XML_EXP_ATOM)) {
7327 /* TODO: handle wildcards */
7328 if (exp->exp_str == sub->exp_left->exp_str) {
7329#ifdef DEBUG_DERIV
7330 printf("Atom match -> Empty\n");
7331#endif
7332 return(emptyExp);
7333 }
7334#ifdef DEBUG_DERIV
7335 printf("Atom mismatch -> Forbid\n");
7336#endif
7337 return(forbiddenExp);
7338 }
7339#ifdef DEBUG_DERIV
7340 printf("Compex exp vs Atom -> Forbid\n");
7341#endif
7342 return(forbiddenExp);
7343 case XML_EXP_SEQ:
7344 /* try to get the sequence consumed only if possible */
7345 if (xmlExpCheckCard(exp->exp_left, sub)) {
7346 /* See if the sequence can be consumed directly */
7347#ifdef DEBUG_DERIV
7348 printf("Seq trying left only\n");
7349#endif
7350 ret = xmlExpExpDeriveInt(ctxt, exp->exp_left, sub);
7351 if ((ret != forbiddenExp) && (ret != NULL)) {
7352#ifdef DEBUG_DERIV
7353 printf("Seq trying left only worked\n");
7354#endif
7355 /*
7356 * TODO: assumption here that we are determinist
7357 * i.e. we won't get to a nillable exp left
7358 * subset which could be matched by the right
7359 * part too.
7360 * e.g.: (a | b)+,(a | c) and 'a+,a'
7361 */
7362 exp->exp_right->ref++;
7363 return(xmlExpHashGetEntry(ctxt, XML_EXP_SEQ, ret,
7364 exp->exp_right, NULL, 0, 0));
7365 }
7366#ifdef DEBUG_DERIV
7367 } else {
7368 printf("Seq: left too short\n");
7369#endif
7370 }
7371 /* Try instead to decompose */
7372 if (sub->type == XML_EXP_COUNT) {
7373 int min, max;
7374
7375#ifdef DEBUG_DERIV
7376 printf("Seq: sub is a count\n");
7377#endif
7378 ret = xmlExpExpDeriveInt(ctxt, exp->exp_left, sub->exp_left);
7379 if (ret == NULL)
7380 return(NULL);
7381 if (ret != forbiddenExp) {
7382#ifdef DEBUG_DERIV
7383 printf("Seq , Count match on left\n");
7384#endif
7385 if (sub->exp_max < 0)
7386 max = -1;
7387 else
7388 max = sub->exp_max -1;
7389 if (sub->exp_min > 0)
7390 min = sub->exp_min -1;
7391 else
7392 min = 0;
7393 exp->exp_right->ref++;
7394 tmp = xmlExpHashGetEntry(ctxt, XML_EXP_SEQ, ret,
7395 exp->exp_right, NULL, 0, 0);
7396 if (tmp == NULL)
7397 return(NULL);
7398
7399 sub->exp_left->ref++;
7400 tmp2 = xmlExpHashGetEntry(ctxt, XML_EXP_COUNT,
7401 sub->exp_left, NULL, NULL, min, max);
7402 if (tmp2 == NULL) {
7403 xmlExpFree(ctxt, tmp);
7404 return(NULL);
7405 }
7406 ret = xmlExpExpDeriveInt(ctxt, tmp, tmp2);
7407 xmlExpFree(ctxt, tmp);
7408 xmlExpFree(ctxt, tmp2);
7409 return(ret);
7410 }
7411 }
7412 /* we made no progress on structured operations */
7413 break;
7414 case XML_EXP_OR:
7415#ifdef DEBUG_DERIV
7416 printf("Or , trying both side\n");
7417#endif
7418 ret = xmlExpExpDeriveInt(ctxt, exp->exp_left, sub);
7419 if (ret == NULL)
7420 return(NULL);
7421 tmp = xmlExpExpDeriveInt(ctxt, exp->exp_right, sub);
7422 if (tmp == NULL) {
7423 xmlExpFree(ctxt, ret);
7424 return(NULL);
7425 }
7426 return(xmlExpHashGetEntry(ctxt, XML_EXP_OR, ret, tmp, NULL, 0, 0));
7427 case XML_EXP_COUNT: {
7428 int min, max;
7429
7430 if (sub->type == XML_EXP_COUNT) {
7431 /*
7432 * Try to see if the loop is completely subsumed
7433 */
7434 tmp = xmlExpExpDeriveInt(ctxt, exp->exp_left, sub->exp_left);
7435 if (tmp == NULL)
7436 return(NULL);
7437 if (tmp == forbiddenExp) {
7438 int mult;
7439
7440#ifdef DEBUG_DERIV
7441 printf("Count, Count inner don't subsume\n");
7442#endif
7443 mult = xmlExpDivide(ctxt, sub->exp_left, exp->exp_left,
7444 NULL, &tmp);
7445 if (mult <= 0) {
7446#ifdef DEBUG_DERIV
7447 printf("Count, Count not multiple => forbidden\n");
7448#endif
7449 return(forbiddenExp);
7450 }
7451 if (sub->exp_max == -1) {
7452 max = -1;
7453 if (exp->exp_max == -1) {
7454 if (exp->exp_min <= sub->exp_min * mult)
7455 min = 0;
7456 else
7457 min = exp->exp_min - sub->exp_min * mult;
7458 } else {
7459#ifdef DEBUG_DERIV
7460 printf("Count, Count finite can't subsume infinite\n");
7461#endif
7462 xmlExpFree(ctxt, tmp);
7463 return(forbiddenExp);
7464 }
7465 } else {
7466 if (exp->exp_max == -1) {
7467#ifdef DEBUG_DERIV
7468 printf("Infinite loop consume mult finite loop\n");
7469#endif
7470 if (exp->exp_min > sub->exp_min * mult) {
7471 max = -1;
7472 min = exp->exp_min - sub->exp_min * mult;
7473 } else {
7474 max = -1;
7475 min = 0;
7476 }
7477 } else {
7478 if (exp->exp_max < sub->exp_max * mult) {
7479#ifdef DEBUG_DERIV
7480 printf("loops max mult mismatch => forbidden\n");
7481#endif
7482 xmlExpFree(ctxt, tmp);
7483 return(forbiddenExp);
7484 }
7485 if (sub->exp_max * mult > exp->exp_min)
7486 min = 0;
7487 else
7488 min = exp->exp_min - sub->exp_max * mult;
7489 max = exp->exp_max - sub->exp_max * mult;
7490 }
7491 }
7492 } else if (!IS_NILLABLE(tmp)) {
7493 /*
7494 * TODO: loop here to try to grow if working on finite
7495 * blocks.
7496 */
7497#ifdef DEBUG_DERIV
7498 printf("Count, Count remain not nillable => forbidden\n");
7499#endif
7500 xmlExpFree(ctxt, tmp);
7501 return(forbiddenExp);
7502 } else if (sub->exp_max == -1) {
7503 if (exp->exp_max == -1) {
7504 if (exp->exp_min <= sub->exp_min) {
7505#ifdef DEBUG_DERIV
7506 printf("Infinite loops Okay => COUNT(0,Inf)\n");
7507#endif
7508 max = -1;
7509 min = 0;
7510 } else {
7511#ifdef DEBUG_DERIV
7512 printf("Infinite loops min => Count(X,Inf)\n");
7513#endif
7514 max = -1;
7515 min = exp->exp_min - sub->exp_min;
7516 }
7517 } else if (exp->exp_min > sub->exp_min) {
7518#ifdef DEBUG_DERIV
7519 printf("loops min mismatch 1 => forbidden ???\n");
7520#endif
7521 xmlExpFree(ctxt, tmp);
7522 return(forbiddenExp);
7523 } else {
7524 max = -1;
7525 min = 0;
7526 }
7527 } else {
7528 if (exp->exp_max == -1) {
7529#ifdef DEBUG_DERIV
7530 printf("Infinite loop consume finite loop\n");
7531#endif
7532 if (exp->exp_min > sub->exp_min) {
7533 max = -1;
7534 min = exp->exp_min - sub->exp_min;
7535 } else {
7536 max = -1;
7537 min = 0;
7538 }
7539 } else {
7540 if (exp->exp_max < sub->exp_max) {
7541#ifdef DEBUG_DERIV
7542 printf("loops max mismatch => forbidden\n");
7543#endif
7544 xmlExpFree(ctxt, tmp);
7545 return(forbiddenExp);
7546 }
7547 if (sub->exp_max > exp->exp_min)
7548 min = 0;
7549 else
7550 min = exp->exp_min - sub->exp_max;
7551 max = exp->exp_max - sub->exp_max;
7552 }
7553 }
7554#ifdef DEBUG_DERIV
7555 printf("loops match => SEQ(COUNT())\n");
7556#endif
7557 exp->exp_left->ref++;
7558 tmp2 = xmlExpHashGetEntry(ctxt, XML_EXP_COUNT, exp->exp_left,
7559 NULL, NULL, min, max);
7560 if (tmp2 == NULL) {
7561 return(NULL);
7562 }
7563 ret = xmlExpHashGetEntry(ctxt, XML_EXP_SEQ, tmp, tmp2,
7564 NULL, 0, 0);
7565 return(ret);
7566 }
7567 tmp = xmlExpExpDeriveInt(ctxt, exp->exp_left, sub);
7568 if (tmp == NULL)
7569 return(NULL);
7570 if (tmp == forbiddenExp) {
7571#ifdef DEBUG_DERIV
7572 printf("loop mismatch => forbidden\n");
7573#endif
7574 return(forbiddenExp);
7575 }
7576 if (exp->exp_min > 0)
7577 min = exp->exp_min - 1;
7578 else
7579 min = 0;
7580 if (exp->exp_max < 0)
7581 max = -1;
7582 else
7583 max = exp->exp_max - 1;
7584
7585#ifdef DEBUG_DERIV
7586 printf("loop match => SEQ(COUNT())\n");
7587#endif
7588 exp->exp_left->ref++;
7589 tmp2 = xmlExpHashGetEntry(ctxt, XML_EXP_COUNT, exp->exp_left,
7590 NULL, NULL, min, max);
7591 if (tmp2 == NULL)
7592 return(NULL);
7593 ret = xmlExpHashGetEntry(ctxt, XML_EXP_SEQ, tmp, tmp2,
7594 NULL, 0, 0);
7595 return(ret);
7596 }
7597 }
7598
Daniel Veillardccb4d412005-08-23 13:41:17 +00007599#ifdef DEBUG_DERIV
7600 printf("Fallback to derivative\n");
7601#endif
7602 if (IS_NILLABLE(sub)) {
7603 if (!(IS_NILLABLE(exp)))
7604 return(forbiddenExp);
7605 else
7606 ret = emptyExp;
7607 } else
7608 ret = NULL;
Daniel Veillard81a8ec62005-08-22 00:20:58 +00007609 /*
7610 * here the structured derivation made no progress so
7611 * we use the default token based derivation to force one more step
7612 */
7613 if (ctxt->tabSize == 0)
7614 ctxt->tabSize = 40;
7615
7616 tab = (const xmlChar **) xmlMalloc(ctxt->tabSize *
7617 sizeof(const xmlChar *));
7618 if (tab == NULL) {
7619 return(NULL);
7620 }
7621
Daniel Veillard81a8ec62005-08-22 00:20:58 +00007622 /*
7623 * collect all the strings accepted by the subexpression on input
7624 */
7625 len = xmlExpGetStartInt(ctxt, sub, tab, ctxt->tabSize, 0);
7626 while (len < 0) {
7627 const xmlChar **temp;
Rob Richards54a8f672005-10-07 02:33:00 +00007628 temp = (const xmlChar **) xmlRealloc((xmlChar **) tab, ctxt->tabSize * 2 *
Daniel Veillard81a8ec62005-08-22 00:20:58 +00007629 sizeof(const xmlChar *));
7630 if (temp == NULL) {
Rob Richards54a8f672005-10-07 02:33:00 +00007631 xmlFree((xmlChar **) tab);
Daniel Veillard81a8ec62005-08-22 00:20:58 +00007632 return(NULL);
7633 }
7634 tab = temp;
7635 ctxt->tabSize *= 2;
7636 len = xmlExpGetStartInt(ctxt, sub, tab, ctxt->tabSize, 0);
7637 }
Daniel Veillard81a8ec62005-08-22 00:20:58 +00007638 for (i = 0;i < len;i++) {
7639 tmp = xmlExpStringDeriveInt(ctxt, exp, tab[i]);
7640 if ((tmp == NULL) || (tmp == forbiddenExp)) {
7641 xmlExpFree(ctxt, ret);
Rob Richards54a8f672005-10-07 02:33:00 +00007642 xmlFree((xmlChar **) tab);
Daniel Veillard81a8ec62005-08-22 00:20:58 +00007643 return(tmp);
7644 }
7645 tmp2 = xmlExpStringDeriveInt(ctxt, sub, tab[i]);
7646 if ((tmp2 == NULL) || (tmp2 == forbiddenExp)) {
7647 xmlExpFree(ctxt, tmp);
7648 xmlExpFree(ctxt, ret);
Rob Richards54a8f672005-10-07 02:33:00 +00007649 xmlFree((xmlChar **) tab);
Daniel Veillard81a8ec62005-08-22 00:20:58 +00007650 return(tmp);
7651 }
7652 tmp3 = xmlExpExpDeriveInt(ctxt, tmp, tmp2);
7653 xmlExpFree(ctxt, tmp);
7654 xmlExpFree(ctxt, tmp2);
7655
7656 if ((tmp3 == NULL) || (tmp3 == forbiddenExp)) {
7657 xmlExpFree(ctxt, ret);
Rob Richards54a8f672005-10-07 02:33:00 +00007658 xmlFree((xmlChar **) tab);
Daniel Veillard81a8ec62005-08-22 00:20:58 +00007659 return(tmp3);
7660 }
7661
7662 if (ret == NULL)
7663 ret = tmp3;
7664 else {
7665 ret = xmlExpHashGetEntry(ctxt, XML_EXP_OR, ret, tmp3, NULL, 0, 0);
7666 if (ret == NULL) {
Rob Richards54a8f672005-10-07 02:33:00 +00007667 xmlFree((xmlChar **) tab);
Daniel Veillard81a8ec62005-08-22 00:20:58 +00007668 return(NULL);
7669 }
7670 }
7671 }
Rob Richards54a8f672005-10-07 02:33:00 +00007672 xmlFree((xmlChar **) tab);
Daniel Veillard81a8ec62005-08-22 00:20:58 +00007673 return(ret);
7674}
7675
7676/**
Daniel Veillard0090bd52005-08-22 14:43:43 +00007677 * xmlExpExpDerive:
7678 * @ctxt: the expressions context
7679 * @exp: the englobing expression
7680 * @sub: the subexpression
7681 *
7682 * Evaluates the expression resulting from @exp consuming a sub expression @sub
7683 * Based on algebraic derivation and sometimes direct Brzozowski derivation
7684 * it usually tatkes less than linear time and can handle expressions generating
7685 * infinite languages.
7686 *
7687 * Returns the resulting expression or NULL in case of internal error, the
7688 * result must be freed
7689 */
7690xmlExpNodePtr
7691xmlExpExpDerive(xmlExpCtxtPtr ctxt, xmlExpNodePtr exp, xmlExpNodePtr sub) {
7692 if ((exp == NULL) || (ctxt == NULL) || (sub == NULL))
7693 return(NULL);
7694
7695 /*
7696 * O(1) speedups
7697 */
7698 if (IS_NILLABLE(sub) && (!IS_NILLABLE(exp))) {
7699#ifdef DEBUG_DERIV
7700 printf("Sub nillable and not exp : can't subsume\n");
7701#endif
7702 return(forbiddenExp);
7703 }
7704 if (xmlExpCheckCard(exp, sub) == 0) {
7705#ifdef DEBUG_DERIV
7706 printf("sub generate longuer sequances than exp : can't subsume\n");
7707#endif
7708 return(forbiddenExp);
7709 }
7710 return(xmlExpExpDeriveInt(ctxt, exp, sub));
7711}
7712
7713/**
Daniel Veillard81a8ec62005-08-22 00:20:58 +00007714 * xmlExpSubsume:
7715 * @ctxt: the expressions context
7716 * @exp: the englobing expression
7717 * @sub: the subexpression
7718 *
7719 * Check whether @exp accepts all the languages accexpted by @sub
7720 * the input being a subexpression.
7721 *
7722 * Returns 1 if true 0 if false and -1 in case of failure.
7723 */
7724int
7725xmlExpSubsume(xmlExpCtxtPtr ctxt, xmlExpNodePtr exp, xmlExpNodePtr sub) {
7726 xmlExpNodePtr tmp;
7727
7728 if ((exp == NULL) || (ctxt == NULL) || (sub == NULL))
7729 return(-1);
7730
7731 /*
7732 * TODO: speedup by checking the language of sub is a subset of the
7733 * language of exp
7734 */
7735 /*
7736 * O(1) speedups
7737 */
7738 if (IS_NILLABLE(sub) && (!IS_NILLABLE(exp))) {
7739#ifdef DEBUG_DERIV
7740 printf("Sub nillable and not exp : can't subsume\n");
7741#endif
7742 return(0);
7743 }
7744 if (xmlExpCheckCard(exp, sub) == 0) {
7745#ifdef DEBUG_DERIV
7746 printf("sub generate longuer sequances than exp : can't subsume\n");
7747#endif
7748 return(0);
7749 }
7750 tmp = xmlExpExpDeriveInt(ctxt, exp, sub);
7751#ifdef DEBUG_DERIV
7752 printf("Result derivation :\n");
7753 PRINT_EXP(tmp);
7754#endif
7755 if (tmp == NULL)
7756 return(-1);
7757 if (tmp == forbiddenExp)
7758 return(0);
7759 if (tmp == emptyExp)
7760 return(1);
7761 if ((tmp != NULL) && (IS_NILLABLE(tmp))) {
7762 xmlExpFree(ctxt, tmp);
7763 return(1);
7764 }
7765 xmlExpFree(ctxt, tmp);
7766 return(0);
7767}
Daniel Veillard465a0002005-08-22 12:07:04 +00007768
7769/************************************************************************
7770 * *
7771 * Parsing expression *
7772 * *
7773 ************************************************************************/
7774
7775static xmlExpNodePtr xmlExpParseExpr(xmlExpCtxtPtr ctxt);
7776
7777#undef CUR
7778#define CUR (*ctxt->cur)
7779#undef NEXT
7780#define NEXT ctxt->cur++;
7781#undef IS_BLANK
7782#define IS_BLANK(c) ((c == ' ') || (c == '\n') || (c == '\r') || (c == '\t'))
7783#define SKIP_BLANKS while (IS_BLANK(*ctxt->cur)) ctxt->cur++;
7784
7785static int
7786xmlExpParseNumber(xmlExpCtxtPtr ctxt) {
7787 int ret = 0;
7788
7789 SKIP_BLANKS
7790 if (CUR == '*') {
7791 NEXT
7792 return(-1);
7793 }
7794 if ((CUR < '0') || (CUR > '9'))
7795 return(-1);
7796 while ((CUR >= '0') && (CUR <= '9')) {
7797 ret = ret * 10 + (CUR - '0');
7798 NEXT
7799 }
7800 return(ret);
7801}
7802
7803static xmlExpNodePtr
7804xmlExpParseOr(xmlExpCtxtPtr ctxt) {
7805 const char *base;
7806 xmlExpNodePtr ret;
7807 const xmlChar *val;
7808
7809 SKIP_BLANKS
7810 base = ctxt->cur;
7811 if (*ctxt->cur == '(') {
7812 NEXT
7813 ret = xmlExpParseExpr(ctxt);
7814 SKIP_BLANKS
7815 if (*ctxt->cur != ')') {
7816 fprintf(stderr, "unbalanced '(' : %s\n", base);
7817 xmlExpFree(ctxt, ret);
7818 return(NULL);
7819 }
7820 NEXT;
7821 SKIP_BLANKS
7822 goto parse_quantifier;
7823 }
7824 while ((CUR != 0) && (!(IS_BLANK(CUR))) && (CUR != '(') &&
7825 (CUR != ')') && (CUR != '|') && (CUR != ',') && (CUR != '{') &&
7826 (CUR != '*') && (CUR != '+') && (CUR != '?') && (CUR != '}'))
7827 NEXT;
7828 val = xmlDictLookup(ctxt->dict, BAD_CAST base, ctxt->cur - base);
7829 if (val == NULL)
7830 return(NULL);
7831 ret = xmlExpHashGetEntry(ctxt, XML_EXP_ATOM, NULL, NULL, val, 0, 0);
7832 if (ret == NULL)
7833 return(NULL);
7834 SKIP_BLANKS
7835parse_quantifier:
7836 if (CUR == '{') {
7837 int min, max;
7838
7839 NEXT
7840 min = xmlExpParseNumber(ctxt);
7841 if (min < 0) {
7842 xmlExpFree(ctxt, ret);
7843 return(NULL);
7844 }
7845 SKIP_BLANKS
7846 if (CUR == ',') {
7847 NEXT
7848 max = xmlExpParseNumber(ctxt);
7849 SKIP_BLANKS
7850 } else
7851 max = min;
7852 if (CUR != '}') {
7853 xmlExpFree(ctxt, ret);
7854 return(NULL);
7855 }
7856 NEXT
7857 ret = xmlExpHashGetEntry(ctxt, XML_EXP_COUNT, ret, NULL, NULL,
7858 min, max);
7859 SKIP_BLANKS
7860 } else if (CUR == '?') {
7861 NEXT
7862 ret = xmlExpHashGetEntry(ctxt, XML_EXP_COUNT, ret, NULL, NULL,
7863 0, 1);
7864 SKIP_BLANKS
7865 } else if (CUR == '+') {
7866 NEXT
7867 ret = xmlExpHashGetEntry(ctxt, XML_EXP_COUNT, ret, NULL, NULL,
7868 1, -1);
7869 SKIP_BLANKS
7870 } else if (CUR == '*') {
7871 NEXT
7872 ret = xmlExpHashGetEntry(ctxt, XML_EXP_COUNT, ret, NULL, NULL,
7873 0, -1);
7874 SKIP_BLANKS
7875 }
7876 return(ret);
7877}
7878
7879
7880static xmlExpNodePtr
7881xmlExpParseSeq(xmlExpCtxtPtr ctxt) {
7882 xmlExpNodePtr ret, right;
7883
7884 ret = xmlExpParseOr(ctxt);
7885 SKIP_BLANKS
7886 while (CUR == '|') {
7887 NEXT
7888 right = xmlExpParseOr(ctxt);
7889 if (right == NULL) {
7890 xmlExpFree(ctxt, ret);
7891 return(NULL);
7892 }
7893 ret = xmlExpHashGetEntry(ctxt, XML_EXP_OR, ret, right, NULL, 0, 0);
7894 if (ret == NULL)
7895 return(NULL);
7896 }
7897 return(ret);
7898}
7899
7900static xmlExpNodePtr
7901xmlExpParseExpr(xmlExpCtxtPtr ctxt) {
7902 xmlExpNodePtr ret, right;
7903
7904 ret = xmlExpParseSeq(ctxt);
7905 SKIP_BLANKS
7906 while (CUR == ',') {
7907 NEXT
7908 right = xmlExpParseSeq(ctxt);
7909 if (right == NULL) {
7910 xmlExpFree(ctxt, ret);
7911 return(NULL);
7912 }
7913 ret = xmlExpHashGetEntry(ctxt, XML_EXP_SEQ, ret, right, NULL, 0, 0);
7914 if (ret == NULL)
7915 return(NULL);
7916 }
7917 return(ret);
7918}
7919
7920/**
7921 * xmlExpParse:
7922 * @ctxt: the expressions context
7923 * @expr: the 0 terminated string
7924 *
7925 * Minimal parser for regexps, it understand the following constructs
7926 * - string terminals
7927 * - choice operator |
7928 * - sequence operator ,
7929 * - subexpressions (...)
7930 * - usual cardinality operators + * and ?
7931 * - finite sequences { min, max }
7932 * - infinite sequences { min, * }
7933 * There is minimal checkings made especially no checking on strings values
7934 *
7935 * Returns a new expression or NULL in case of failure
7936 */
7937xmlExpNodePtr
7938xmlExpParse(xmlExpCtxtPtr ctxt, const char *expr) {
7939 xmlExpNodePtr ret;
7940
7941 ctxt->expr = expr;
7942 ctxt->cur = expr;
7943
7944 ret = xmlExpParseExpr(ctxt);
7945 SKIP_BLANKS
7946 if (*ctxt->cur != 0) {
7947 xmlExpFree(ctxt, ret);
7948 return(NULL);
7949 }
7950 return(ret);
7951}
7952
7953static void
7954xmlExpDumpInt(xmlBufferPtr buf, xmlExpNodePtr expr, int glob) {
7955 xmlExpNodePtr c;
7956
7957 if (expr == NULL) return;
7958 if (glob) xmlBufferWriteChar(buf, "(");
7959 switch (expr->type) {
7960 case XML_EXP_EMPTY:
7961 xmlBufferWriteChar(buf, "empty");
7962 break;
7963 case XML_EXP_FORBID:
7964 xmlBufferWriteChar(buf, "forbidden");
7965 break;
7966 case XML_EXP_ATOM:
7967 xmlBufferWriteCHAR(buf, expr->exp_str);
7968 break;
7969 case XML_EXP_SEQ:
7970 c = expr->exp_left;
7971 if ((c->type == XML_EXP_SEQ) || (c->type == XML_EXP_OR))
7972 xmlExpDumpInt(buf, c, 1);
7973 else
7974 xmlExpDumpInt(buf, c, 0);
7975 xmlBufferWriteChar(buf, " , ");
7976 c = expr->exp_right;
7977 if ((c->type == XML_EXP_SEQ) || (c->type == XML_EXP_OR))
7978 xmlExpDumpInt(buf, c, 1);
7979 else
7980 xmlExpDumpInt(buf, c, 0);
7981 break;
7982 case XML_EXP_OR:
7983 c = expr->exp_left;
7984 if ((c->type == XML_EXP_SEQ) || (c->type == XML_EXP_OR))
7985 xmlExpDumpInt(buf, c, 1);
7986 else
7987 xmlExpDumpInt(buf, c, 0);
7988 xmlBufferWriteChar(buf, " | ");
7989 c = expr->exp_right;
7990 if ((c->type == XML_EXP_SEQ) || (c->type == XML_EXP_OR))
7991 xmlExpDumpInt(buf, c, 1);
7992 else
7993 xmlExpDumpInt(buf, c, 0);
7994 break;
7995 case XML_EXP_COUNT: {
7996 char rep[40];
7997
7998 c = expr->exp_left;
7999 if ((c->type == XML_EXP_SEQ) || (c->type == XML_EXP_OR))
8000 xmlExpDumpInt(buf, c, 1);
8001 else
8002 xmlExpDumpInt(buf, c, 0);
8003 if ((expr->exp_min == 0) && (expr->exp_max == 1)) {
8004 rep[0] = '?';
8005 rep[1] = 0;
8006 } else if ((expr->exp_min == 0) && (expr->exp_max == -1)) {
8007 rep[0] = '*';
8008 rep[1] = 0;
8009 } else if ((expr->exp_min == 1) && (expr->exp_max == -1)) {
8010 rep[0] = '+';
8011 rep[1] = 0;
8012 } else if (expr->exp_max == expr->exp_min) {
8013 snprintf(rep, 39, "{%d}", expr->exp_min);
8014 } else if (expr->exp_max < 0) {
8015 snprintf(rep, 39, "{%d,inf}", expr->exp_min);
8016 } else {
8017 snprintf(rep, 39, "{%d,%d}", expr->exp_min, expr->exp_max);
8018 }
8019 rep[39] = 0;
8020 xmlBufferWriteChar(buf, rep);
8021 break;
8022 }
8023 default:
8024 fprintf(stderr, "Error in tree\n");
8025 }
8026 if (glob)
8027 xmlBufferWriteChar(buf, ")");
8028}
8029/**
8030 * xmlExpDump:
8031 * @buf: a buffer to receive the output
8032 * @expr: the compiled expression
8033 *
8034 * Serialize the expression as compiled to the buffer
8035 */
8036void
Daniel Veillard5eee7672005-08-22 21:22:27 +00008037xmlExpDump(xmlBufferPtr buf, xmlExpNodePtr expr) {
8038 if ((buf == NULL) || (expr == NULL))
Daniel Veillard465a0002005-08-22 12:07:04 +00008039 return;
Daniel Veillard5eee7672005-08-22 21:22:27 +00008040 xmlExpDumpInt(buf, expr, 0);
Daniel Veillard465a0002005-08-22 12:07:04 +00008041}
8042
8043/**
8044 * xmlExpMaxToken:
8045 * @expr: a compiled expression
8046 *
8047 * Indicate the maximum number of input a expression can accept
8048 *
8049 * Returns the maximum length or -1 in case of error
8050 */
8051int
8052xmlExpMaxToken(xmlExpNodePtr expr) {
8053 if (expr == NULL)
8054 return(-1);
8055 return(expr->c_max);
8056}
8057
8058/**
8059 * xmlExpCtxtNbNodes:
8060 * @ctxt: an expression context
8061 *
8062 * Debugging facility provides the number of allocated nodes at a that point
8063 *
8064 * Returns the number of nodes in use or -1 in case of error
8065 */
8066int
8067xmlExpCtxtNbNodes(xmlExpCtxtPtr ctxt) {
8068 if (ctxt == NULL)
8069 return(-1);
8070 return(ctxt->nb_nodes);
8071}
8072
8073/**
8074 * xmlExpCtxtNbCons:
8075 * @ctxt: an expression context
8076 *
8077 * Debugging facility provides the number of allocated nodes over lifetime
8078 *
8079 * Returns the number of nodes ever allocated or -1 in case of error
8080 */
8081int
8082xmlExpCtxtNbCons(xmlExpCtxtPtr ctxt) {
8083 if (ctxt == NULL)
8084 return(-1);
8085 return(ctxt->nb_cons);
8086}
8087
Daniel Veillard81a8ec62005-08-22 00:20:58 +00008088#endif /* LIBXML_EXPR_ENABLED */
Daniel Veillard5d4644e2005-04-01 13:11:58 +00008089#define bottom_xmlregexp
8090#include "elfgcchack.h"
Daniel Veillard4255d502002-04-16 15:50:10 +00008091#endif /* LIBXML_REGEXP_ENABLED */