blob: 2bce367031b696c03ec3dacf982c344d84b01a40 [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) {
1535 if (atom == NULL) {
1536 ERROR("genrate transition: atom == NULL");
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001537 return(-1);
Daniel Veillard4255d502002-04-16 15:50:10 +00001538 }
1539 if (atom->type == XML_REGEXP_SUBREG) {
1540 /*
1541 * this is a subexpression handling one should not need to
William M. Brackddf71d62004-05-06 04:17:26 +00001542 * create a new node except for XML_REGEXP_QUANT_RANGE.
Daniel Veillard4255d502002-04-16 15:50:10 +00001543 */
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001544 if (xmlRegAtomPush(ctxt, atom) < 0) {
1545 return(-1);
1546 }
Daniel Veillard4255d502002-04-16 15:50:10 +00001547 if ((to != NULL) && (atom->stop != to) &&
1548 (atom->quant != XML_REGEXP_QUANT_RANGE)) {
1549 /*
1550 * Generate an epsilon transition to link to the target
1551 */
1552 xmlFAGenerateEpsilonTransition(ctxt, atom->stop, to);
Daniel Veillardaa622012005-10-20 15:55:25 +00001553#ifdef DV
1554 } else if ((to == NULL) && (atom->quant != XML_REGEXP_QUANT_RANGE) &&
1555 (atom->quant != XML_REGEXP_QUANT_ONCE)) {
1556 to = xmlRegNewState(ctxt);
1557 xmlRegStatePush(ctxt, to);
1558 ctxt->state = to;
1559 xmlFAGenerateEpsilonTransition(ctxt, atom->stop, to);
1560#endif
Daniel Veillard4255d502002-04-16 15:50:10 +00001561 }
1562 switch (atom->quant) {
1563 case XML_REGEXP_QUANT_OPT:
1564 atom->quant = XML_REGEXP_QUANT_ONCE;
Daniel Veillard54eb0242006-03-21 23:17:57 +00001565 /*
1566 * transition done to the state after end of atom.
1567 * 1. set transition from atom start to new state
1568 * 2. set transition from atom end to this state.
1569 */
1570 xmlFAGenerateEpsilonTransition(ctxt, atom->start, 0);
1571 xmlFAGenerateEpsilonTransition(ctxt, atom->stop, ctxt->state);
Daniel Veillard4255d502002-04-16 15:50:10 +00001572 break;
1573 case XML_REGEXP_QUANT_MULT:
1574 atom->quant = XML_REGEXP_QUANT_ONCE;
1575 xmlFAGenerateEpsilonTransition(ctxt, atom->start, atom->stop);
1576 xmlFAGenerateEpsilonTransition(ctxt, atom->stop, atom->start);
1577 break;
1578 case XML_REGEXP_QUANT_PLUS:
1579 atom->quant = XML_REGEXP_QUANT_ONCE;
1580 xmlFAGenerateEpsilonTransition(ctxt, atom->stop, atom->start);
1581 break;
1582 case XML_REGEXP_QUANT_RANGE: {
1583 int counter;
Daniel Veillard76d59b62007-08-22 16:29:21 +00001584 xmlRegStatePtr inter, newstate;
Daniel Veillard4255d502002-04-16 15:50:10 +00001585
1586 /*
Daniel Veillard76d59b62007-08-22 16:29:21 +00001587 * create the final state now if needed
Daniel Veillard4255d502002-04-16 15:50:10 +00001588 */
Daniel Veillard4255d502002-04-16 15:50:10 +00001589 if (to != NULL) {
1590 newstate = to;
1591 } else {
1592 newstate = xmlRegNewState(ctxt);
1593 xmlRegStatePush(ctxt, newstate);
Daniel Veillard4255d502002-04-16 15:50:10 +00001594 }
Daniel Veillard76d59b62007-08-22 16:29:21 +00001595
1596 /*
1597 * The principle here is to use counted transition
1598 * to avoid explosion in the number of states in the
1599 * graph. This is clearly more complex but should not
1600 * be exploitable at runtime.
Daniel Veillard54eb0242006-03-21 23:17:57 +00001601 */
Daniel Veillard76d59b62007-08-22 16:29:21 +00001602 if ((atom->min == 0) && (atom->start0 == NULL)) {
1603 xmlRegAtomPtr copy;
1604 /*
1605 * duplicate a transition based on atom to count next
1606 * occurences after 1. We cannot loop to atom->start
1607 * directly because we need an epsilon transition to
1608 * newstate.
1609 */
1610 /* ???? For some reason it seems we never reach that
1611 case, I suppose this got optimized out before when
1612 building the automata */
Daniel Veillardc821e032007-08-28 17:33:45 +00001613 copy = xmlRegCopyAtom(ctxt, atom);
William M. Brackec720082007-08-24 02:57:38 +00001614 copy = xmlRegCopyAtom(ctxt, atom);
Daniel Veillard76d59b62007-08-22 16:29:21 +00001615 if (copy == NULL)
1616 return(-1);
Daniel Veillard76d59b62007-08-22 16:29:21 +00001617 copy->quant = XML_REGEXP_QUANT_ONCE;
1618 copy->min = 0;
1619 copy->max = 0;
1620
1621 if (xmlFAGenerateTransitions(ctxt, atom->start, NULL, copy)
1622 < 0)
1623 return(-1);
1624 inter = ctxt->state;
1625 counter = xmlRegGetCounter(ctxt);
1626 ctxt->counters[counter].min = atom->min - 1;
1627 ctxt->counters[counter].max = atom->max - 1;
1628 /* count the number of times we see it again */
1629 xmlFAGenerateCountedEpsilonTransition(ctxt, inter,
1630 atom->stop, counter);
1631 /* allow a way out based on the count */
1632 xmlFAGenerateCountedTransition(ctxt, inter,
1633 newstate, counter);
1634 /* and also allow a direct exit for 0 */
1635 xmlFAGenerateEpsilonTransition(ctxt, atom->start,
1636 newstate);
1637 } else {
1638 /*
1639 * either we need the atom at least once or there
1640 * is an atom->start0 allowing to easilly plug the
1641 * epsilon transition.
1642 */
1643 counter = xmlRegGetCounter(ctxt);
1644 ctxt->counters[counter].min = atom->min - 1;
1645 ctxt->counters[counter].max = atom->max - 1;
1646 /* count the number of times we see it again */
1647 xmlFAGenerateCountedEpsilonTransition(ctxt, atom->stop,
1648 atom->start, counter);
1649 /* allow a way out based on the count */
1650 xmlFAGenerateCountedTransition(ctxt, atom->stop,
1651 newstate, counter);
1652 /* and if needed allow a direct exit for 0 */
1653 if (atom->min == 0)
1654 xmlFAGenerateEpsilonTransition(ctxt, atom->start0,
1655 newstate);
1656
1657 }
1658 atom->min = 0;
1659 atom->max = 0;
1660 atom->quant = XML_REGEXP_QUANT_ONCE;
1661 ctxt->state = newstate;
Daniel Veillard4255d502002-04-16 15:50:10 +00001662 }
1663 default:
1664 break;
1665 }
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001666 return(0);
Daniel Veillard567a45b2005-10-18 19:11:55 +00001667 }
1668 if ((atom->min == 0) && (atom->max == 0) &&
Daniel Veillard99c394d2005-07-14 12:58:49 +00001669 (atom->quant == XML_REGEXP_QUANT_RANGE)) {
1670 /*
1671 * we can discard the atom and generate an epsilon transition instead
1672 */
1673 if (to == NULL) {
1674 to = xmlRegNewState(ctxt);
1675 if (to != NULL)
1676 xmlRegStatePush(ctxt, to);
1677 else {
1678 return(-1);
1679 }
1680 }
1681 xmlFAGenerateEpsilonTransition(ctxt, from, to);
1682 ctxt->state = to;
1683 xmlRegFreeAtom(atom);
1684 return(0);
Daniel Veillard567a45b2005-10-18 19:11:55 +00001685 }
1686 if (to == NULL) {
1687 to = xmlRegNewState(ctxt);
1688 if (to != NULL)
1689 xmlRegStatePush(ctxt, to);
1690 else {
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001691 return(-1);
Daniel Veillard4255d502002-04-16 15:50:10 +00001692 }
Daniel Veillard4255d502002-04-16 15:50:10 +00001693 }
Daniel Veillard567a45b2005-10-18 19:11:55 +00001694 if (xmlRegAtomPush(ctxt, atom) < 0) {
1695 return(-1);
1696 }
1697 xmlRegStateAddTrans(ctxt, from, atom, to, -1, -1);
1698 ctxt->state = to;
Daniel Veillard4255d502002-04-16 15:50:10 +00001699 switch (atom->quant) {
1700 case XML_REGEXP_QUANT_OPT:
1701 atom->quant = XML_REGEXP_QUANT_ONCE;
1702 xmlFAGenerateEpsilonTransition(ctxt, from, to);
1703 break;
1704 case XML_REGEXP_QUANT_MULT:
1705 atom->quant = XML_REGEXP_QUANT_ONCE;
1706 xmlFAGenerateEpsilonTransition(ctxt, from, to);
Daniel Veillard5de09382005-09-26 17:18:17 +00001707 xmlRegStateAddTrans(ctxt, to, atom, to, -1, -1);
Daniel Veillard4255d502002-04-16 15:50:10 +00001708 break;
1709 case XML_REGEXP_QUANT_PLUS:
1710 atom->quant = XML_REGEXP_QUANT_ONCE;
Daniel Veillard5de09382005-09-26 17:18:17 +00001711 xmlRegStateAddTrans(ctxt, to, atom, to, -1, -1);
Daniel Veillard4255d502002-04-16 15:50:10 +00001712 break;
William M. Brack56578372007-04-11 14:33:46 +00001713 case XML_REGEXP_QUANT_RANGE:
Daniel Veillardc821e032007-08-28 17:33:45 +00001714#if DV_test
William M. Brack56578372007-04-11 14:33:46 +00001715 if (atom->min == 0) {
1716 xmlFAGenerateEpsilonTransition(ctxt, from, to);
1717 }
Daniel Veillardc821e032007-08-28 17:33:45 +00001718#endif
William M. Brack56578372007-04-11 14:33:46 +00001719 break;
Daniel Veillard4255d502002-04-16 15:50:10 +00001720 default:
1721 break;
1722 }
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001723 return(0);
Daniel Veillard4255d502002-04-16 15:50:10 +00001724}
1725
1726/**
1727 * xmlFAReduceEpsilonTransitions:
Daniel Veillard441bc322002-04-20 17:38:48 +00001728 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00001729 * @fromnr: the from state
1730 * @tonr: the to state
William M. Brackddf71d62004-05-06 04:17:26 +00001731 * @counter: should that transition be associated to a counted
Daniel Veillard4255d502002-04-16 15:50:10 +00001732 *
1733 */
1734static void
1735xmlFAReduceEpsilonTransitions(xmlRegParserCtxtPtr ctxt, int fromnr,
1736 int tonr, int counter) {
1737 int transnr;
1738 xmlRegStatePtr from;
1739 xmlRegStatePtr to;
1740
1741#ifdef DEBUG_REGEXP_GRAPH
1742 printf("xmlFAReduceEpsilonTransitions(%d, %d)\n", fromnr, tonr);
1743#endif
1744 from = ctxt->states[fromnr];
1745 if (from == NULL)
1746 return;
1747 to = ctxt->states[tonr];
1748 if (to == NULL)
1749 return;
1750 if ((to->mark == XML_REGEXP_MARK_START) ||
1751 (to->mark == XML_REGEXP_MARK_VISITED))
1752 return;
1753
1754 to->mark = XML_REGEXP_MARK_VISITED;
1755 if (to->type == XML_REGEXP_FINAL_STATE) {
1756#ifdef DEBUG_REGEXP_GRAPH
1757 printf("State %d is final, so %d becomes final\n", tonr, fromnr);
1758#endif
1759 from->type = XML_REGEXP_FINAL_STATE;
1760 }
1761 for (transnr = 0;transnr < to->nbTrans;transnr++) {
Daniel Veillarddb68b742005-07-30 13:18:24 +00001762 if (to->trans[transnr].to < 0)
1763 continue;
Daniel Veillard4255d502002-04-16 15:50:10 +00001764 if (to->trans[transnr].atom == NULL) {
1765 /*
1766 * Don't remove counted transitions
1767 * Don't loop either
1768 */
Daniel Veillardb509f152002-04-17 16:28:10 +00001769 if (to->trans[transnr].to != fromnr) {
1770 if (to->trans[transnr].count >= 0) {
1771 int newto = to->trans[transnr].to;
1772
1773 xmlRegStateAddTrans(ctxt, from, NULL,
1774 ctxt->states[newto],
Daniel Veillard5de09382005-09-26 17:18:17 +00001775 -1, to->trans[transnr].count);
Daniel Veillardb509f152002-04-17 16:28:10 +00001776 } else {
Daniel Veillard4255d502002-04-16 15:50:10 +00001777#ifdef DEBUG_REGEXP_GRAPH
Daniel Veillardb509f152002-04-17 16:28:10 +00001778 printf("Found epsilon trans %d from %d to %d\n",
1779 transnr, tonr, to->trans[transnr].to);
Daniel Veillard4255d502002-04-16 15:50:10 +00001780#endif
Daniel Veillardb509f152002-04-17 16:28:10 +00001781 if (to->trans[transnr].counter >= 0) {
1782 xmlFAReduceEpsilonTransitions(ctxt, fromnr,
1783 to->trans[transnr].to,
1784 to->trans[transnr].counter);
1785 } else {
1786 xmlFAReduceEpsilonTransitions(ctxt, fromnr,
1787 to->trans[transnr].to,
1788 counter);
1789 }
1790 }
Daniel Veillard4255d502002-04-16 15:50:10 +00001791 }
1792 } else {
1793 int newto = to->trans[transnr].to;
1794
Daniel Veillardb509f152002-04-17 16:28:10 +00001795 if (to->trans[transnr].counter >= 0) {
1796 xmlRegStateAddTrans(ctxt, from, to->trans[transnr].atom,
1797 ctxt->states[newto],
Daniel Veillard5de09382005-09-26 17:18:17 +00001798 to->trans[transnr].counter, -1);
Daniel Veillardb509f152002-04-17 16:28:10 +00001799 } else {
1800 xmlRegStateAddTrans(ctxt, from, to->trans[transnr].atom,
Daniel Veillard5de09382005-09-26 17:18:17 +00001801 ctxt->states[newto], counter, -1);
Daniel Veillardb509f152002-04-17 16:28:10 +00001802 }
Daniel Veillard4255d502002-04-16 15:50:10 +00001803 }
1804 }
1805 to->mark = XML_REGEXP_MARK_NORMAL;
1806}
1807
1808/**
Daniel Veillarddb68b742005-07-30 13:18:24 +00001809 * xmlFAEliminateSimpleEpsilonTransitions:
1810 * @ctxt: a regexp parser context
1811 *
1812 * Eliminating general epsilon transitions can get costly in the general
1813 * algorithm due to the large amount of generated new transitions and
1814 * associated comparisons. However for simple epsilon transition used just
1815 * to separate building blocks when generating the automata this can be
1816 * reduced to state elimination:
1817 * - if there exists an epsilon from X to Y
1818 * - if there is no other transition from X
1819 * then X and Y are semantically equivalent and X can be eliminated
1820 * If X is the start state then make Y the start state, else replace the
1821 * target of all transitions to X by transitions to Y.
1822 */
1823static void
1824xmlFAEliminateSimpleEpsilonTransitions(xmlRegParserCtxtPtr ctxt) {
1825 int statenr, i, j, newto;
1826 xmlRegStatePtr state, tmp;
1827
1828 for (statenr = 0;statenr < ctxt->nbStates;statenr++) {
1829 state = ctxt->states[statenr];
1830 if (state == NULL)
1831 continue;
1832 if (state->nbTrans != 1)
1833 continue;
Daniel Veillard0e05f4c2006-11-01 15:33:04 +00001834 if (state->type == XML_REGEXP_UNREACH_STATE)
1835 continue;
Daniel Veillarddb68b742005-07-30 13:18:24 +00001836 /* is the only transition out a basic transition */
1837 if ((state->trans[0].atom == NULL) &&
1838 (state->trans[0].to >= 0) &&
1839 (state->trans[0].to != statenr) &&
1840 (state->trans[0].counter < 0) &&
1841 (state->trans[0].count < 0)) {
1842 newto = state->trans[0].to;
1843
1844 if (state->type == XML_REGEXP_START_STATE) {
1845#ifdef DEBUG_REGEXP_GRAPH
1846 printf("Found simple epsilon trans from start %d to %d\n",
1847 statenr, newto);
1848#endif
1849 } else {
1850#ifdef DEBUG_REGEXP_GRAPH
1851 printf("Found simple epsilon trans from %d to %d\n",
1852 statenr, newto);
1853#endif
1854 for (i = 0;i < state->nbTransTo;i++) {
1855 tmp = ctxt->states[state->transTo[i]];
1856 for (j = 0;j < tmp->nbTrans;j++) {
1857 if (tmp->trans[j].to == statenr) {
Daniel Veillarddb68b742005-07-30 13:18:24 +00001858#ifdef DEBUG_REGEXP_GRAPH
1859 printf("Changed transition %d on %d to go to %d\n",
1860 j, tmp->no, newto);
1861#endif
Daniel Veillard0e05f4c2006-11-01 15:33:04 +00001862 tmp->trans[j].to = -1;
1863 xmlRegStateAddTrans(ctxt, tmp, tmp->trans[j].atom,
1864 ctxt->states[newto],
1865 tmp->trans[j].counter,
1866 tmp->trans[j].count);
Daniel Veillarddb68b742005-07-30 13:18:24 +00001867 }
1868 }
1869 }
Daniel Veillarddb68b742005-07-30 13:18:24 +00001870 if (state->type == XML_REGEXP_FINAL_STATE)
1871 ctxt->states[newto]->type = XML_REGEXP_FINAL_STATE;
1872 /* eliminate the transition completely */
1873 state->nbTrans = 0;
1874
Daniel Veillard0e05f4c2006-11-01 15:33:04 +00001875 state->type = XML_REGEXP_UNREACH_STATE;
Daniel Veillarddb68b742005-07-30 13:18:24 +00001876
1877 }
1878
1879 }
1880 }
1881}
1882/**
Daniel Veillard4255d502002-04-16 15:50:10 +00001883 * xmlFAEliminateEpsilonTransitions:
Daniel Veillard441bc322002-04-20 17:38:48 +00001884 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00001885 *
1886 */
1887static void
1888xmlFAEliminateEpsilonTransitions(xmlRegParserCtxtPtr ctxt) {
1889 int statenr, transnr;
1890 xmlRegStatePtr state;
Daniel Veillarddb68b742005-07-30 13:18:24 +00001891 int has_epsilon;
Daniel Veillard4255d502002-04-16 15:50:10 +00001892
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001893 if (ctxt->states == NULL) return;
1894
Daniel Veillard0e05f4c2006-11-01 15:33:04 +00001895 /*
1896 * Eliminate simple epsilon transition and the associated unreachable
1897 * states.
1898 */
Daniel Veillarddb68b742005-07-30 13:18:24 +00001899 xmlFAEliminateSimpleEpsilonTransitions(ctxt);
Daniel Veillard0e05f4c2006-11-01 15:33:04 +00001900 for (statenr = 0;statenr < ctxt->nbStates;statenr++) {
1901 state = ctxt->states[statenr];
1902 if ((state != NULL) && (state->type == XML_REGEXP_UNREACH_STATE)) {
1903#ifdef DEBUG_REGEXP_GRAPH
1904 printf("Removed unreachable state %d\n", statenr);
1905#endif
1906 xmlRegFreeState(state);
1907 ctxt->states[statenr] = NULL;
1908 }
1909 }
Daniel Veillarddb68b742005-07-30 13:18:24 +00001910
1911 has_epsilon = 0;
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001912
Daniel Veillard4255d502002-04-16 15:50:10 +00001913 /*
Daniel Veillardfcd18ff2006-11-02 10:28:04 +00001914 * Build the completed transitions bypassing the epsilons
Daniel Veillard4255d502002-04-16 15:50:10 +00001915 * Use a marking algorithm to avoid loops
Daniel Veillardfcd18ff2006-11-02 10:28:04 +00001916 * Mark sink states too.
1917 * Process from the latests states backward to the start when
1918 * there is long cascading epsilon chains this minimize the
1919 * recursions and transition compares when adding the new ones
Daniel Veillard4255d502002-04-16 15:50:10 +00001920 */
Daniel Veillardfcd18ff2006-11-02 10:28:04 +00001921 for (statenr = ctxt->nbStates - 1;statenr >= 0;statenr--) {
Daniel Veillard4255d502002-04-16 15:50:10 +00001922 state = ctxt->states[statenr];
1923 if (state == NULL)
1924 continue;
Daniel Veillardcc026dc2005-01-12 13:21:17 +00001925 if ((state->nbTrans == 0) &&
1926 (state->type != XML_REGEXP_FINAL_STATE)) {
1927 state->type = XML_REGEXP_SINK_STATE;
1928 }
Daniel Veillard4255d502002-04-16 15:50:10 +00001929 for (transnr = 0;transnr < state->nbTrans;transnr++) {
1930 if ((state->trans[transnr].atom == NULL) &&
1931 (state->trans[transnr].to >= 0)) {
1932 if (state->trans[transnr].to == statenr) {
1933 state->trans[transnr].to = -1;
1934#ifdef DEBUG_REGEXP_GRAPH
1935 printf("Removed loopback epsilon trans %d on %d\n",
1936 transnr, statenr);
1937#endif
1938 } else if (state->trans[transnr].count < 0) {
1939 int newto = state->trans[transnr].to;
1940
1941#ifdef DEBUG_REGEXP_GRAPH
1942 printf("Found epsilon trans %d from %d to %d\n",
1943 transnr, statenr, newto);
1944#endif
Daniel Veillarddb68b742005-07-30 13:18:24 +00001945 has_epsilon = 1;
Daniel Veillard0e05f4c2006-11-01 15:33:04 +00001946 state->trans[transnr].to = -2;
1947 state->mark = XML_REGEXP_MARK_START;
Daniel Veillard4255d502002-04-16 15:50:10 +00001948 xmlFAReduceEpsilonTransitions(ctxt, statenr,
1949 newto, state->trans[transnr].counter);
1950 state->mark = XML_REGEXP_MARK_NORMAL;
1951#ifdef DEBUG_REGEXP_GRAPH
1952 } else {
1953 printf("Found counted transition %d on %d\n",
1954 transnr, statenr);
1955#endif
1956 }
1957 }
1958 }
1959 }
1960 /*
1961 * Eliminate the epsilon transitions
1962 */
Daniel Veillarddb68b742005-07-30 13:18:24 +00001963 if (has_epsilon) {
1964 for (statenr = 0;statenr < ctxt->nbStates;statenr++) {
1965 state = ctxt->states[statenr];
1966 if (state == NULL)
1967 continue;
1968 for (transnr = 0;transnr < state->nbTrans;transnr++) {
1969 xmlRegTransPtr trans = &(state->trans[transnr]);
1970 if ((trans->atom == NULL) &&
1971 (trans->count < 0) &&
1972 (trans->to >= 0)) {
1973 trans->to = -1;
1974 }
Daniel Veillard4255d502002-04-16 15:50:10 +00001975 }
1976 }
1977 }
Daniel Veillard23e73572002-09-19 19:56:43 +00001978
1979 /*
1980 * Use this pass to detect unreachable states too
1981 */
1982 for (statenr = 0;statenr < ctxt->nbStates;statenr++) {
1983 state = ctxt->states[statenr];
1984 if (state != NULL)
William M. Brack779af002003-08-01 15:55:39 +00001985 state->reached = XML_REGEXP_MARK_NORMAL;
Daniel Veillard23e73572002-09-19 19:56:43 +00001986 }
1987 state = ctxt->states[0];
1988 if (state != NULL)
William M. Brack779af002003-08-01 15:55:39 +00001989 state->reached = XML_REGEXP_MARK_START;
Daniel Veillard23e73572002-09-19 19:56:43 +00001990 while (state != NULL) {
1991 xmlRegStatePtr target = NULL;
William M. Brack779af002003-08-01 15:55:39 +00001992 state->reached = XML_REGEXP_MARK_VISITED;
Daniel Veillard23e73572002-09-19 19:56:43 +00001993 /*
William M. Brackddf71d62004-05-06 04:17:26 +00001994 * Mark all states reachable from the current reachable state
Daniel Veillard23e73572002-09-19 19:56:43 +00001995 */
1996 for (transnr = 0;transnr < state->nbTrans;transnr++) {
1997 if ((state->trans[transnr].to >= 0) &&
1998 ((state->trans[transnr].atom != NULL) ||
1999 (state->trans[transnr].count >= 0))) {
2000 int newto = state->trans[transnr].to;
2001
2002 if (ctxt->states[newto] == NULL)
2003 continue;
William M. Brack779af002003-08-01 15:55:39 +00002004 if (ctxt->states[newto]->reached == XML_REGEXP_MARK_NORMAL) {
2005 ctxt->states[newto]->reached = XML_REGEXP_MARK_START;
Daniel Veillard23e73572002-09-19 19:56:43 +00002006 target = ctxt->states[newto];
2007 }
2008 }
2009 }
Daniel Veillardcc026dc2005-01-12 13:21:17 +00002010
Daniel Veillard23e73572002-09-19 19:56:43 +00002011 /*
2012 * find the next accessible state not explored
2013 */
2014 if (target == NULL) {
2015 for (statenr = 1;statenr < ctxt->nbStates;statenr++) {
2016 state = ctxt->states[statenr];
William M. Brack779af002003-08-01 15:55:39 +00002017 if ((state != NULL) && (state->reached ==
2018 XML_REGEXP_MARK_START)) {
Daniel Veillard23e73572002-09-19 19:56:43 +00002019 target = state;
2020 break;
2021 }
2022 }
2023 }
2024 state = target;
2025 }
2026 for (statenr = 0;statenr < ctxt->nbStates;statenr++) {
2027 state = ctxt->states[statenr];
William M. Brack779af002003-08-01 15:55:39 +00002028 if ((state != NULL) && (state->reached == XML_REGEXP_MARK_NORMAL)) {
Daniel Veillard23e73572002-09-19 19:56:43 +00002029#ifdef DEBUG_REGEXP_GRAPH
2030 printf("Removed unreachable state %d\n", statenr);
2031#endif
2032 xmlRegFreeState(state);
2033 ctxt->states[statenr] = NULL;
2034 }
2035 }
2036
Daniel Veillard4255d502002-04-16 15:50:10 +00002037}
2038
Daniel Veillard567a45b2005-10-18 19:11:55 +00002039static int
2040xmlFACompareRanges(xmlRegRangePtr range1, xmlRegRangePtr range2) {
2041 int ret = 0;
2042
2043 if ((range1->type == XML_REGEXP_RANGES) ||
2044 (range2->type == XML_REGEXP_RANGES) ||
2045 (range2->type == XML_REGEXP_SUBREG) ||
2046 (range1->type == XML_REGEXP_SUBREG) ||
2047 (range1->type == XML_REGEXP_STRING) ||
2048 (range2->type == XML_REGEXP_STRING))
2049 return(-1);
2050
2051 /* put them in order */
2052 if (range1->type > range2->type) {
2053 xmlRegRangePtr tmp;
2054
2055 tmp = range1;
2056 range1 = range2;
2057 range2 = tmp;
2058 }
2059 if ((range1->type == XML_REGEXP_ANYCHAR) ||
2060 (range2->type == XML_REGEXP_ANYCHAR)) {
2061 ret = 1;
2062 } else if ((range1->type == XML_REGEXP_EPSILON) ||
2063 (range2->type == XML_REGEXP_EPSILON)) {
2064 return(0);
2065 } else if (range1->type == range2->type) {
2066 if ((range1->type != XML_REGEXP_CHARVAL) ||
2067 (range1->end < range2->start) ||
2068 (range2->end < range1->start))
2069 ret = 1;
2070 else
2071 ret = 0;
2072 } else if (range1->type == XML_REGEXP_CHARVAL) {
2073 int codepoint;
2074 int neg = 0;
2075
2076 /*
2077 * just check all codepoints in the range for acceptance,
2078 * this is usually way cheaper since done only once at
2079 * compilation than testing over and over at runtime or
2080 * pushing too many states when evaluating.
2081 */
2082 if (((range1->neg == 0) && (range2->neg != 0)) ||
2083 ((range1->neg != 0) && (range2->neg == 0)))
2084 neg = 1;
2085
2086 for (codepoint = range1->start;codepoint <= range1->end ;codepoint++) {
2087 ret = xmlRegCheckCharacterRange(range2->type, codepoint,
2088 0, range2->start, range2->end,
2089 range2->blockName);
2090 if (ret < 0)
2091 return(-1);
2092 if (((neg == 1) && (ret == 0)) ||
2093 ((neg == 0) && (ret == 1)))
2094 return(1);
2095 }
2096 return(0);
2097 } else if ((range1->type == XML_REGEXP_BLOCK_NAME) ||
2098 (range2->type == XML_REGEXP_BLOCK_NAME)) {
2099 if (range1->type == range2->type) {
2100 ret = xmlStrEqual(range1->blockName, range2->blockName);
2101 } else {
2102 /*
2103 * comparing a block range with anything else is way
2104 * too costly, and maintining the table is like too much
2105 * memory too, so let's force the automata to save state
2106 * here.
2107 */
2108 return(1);
2109 }
2110 } else if ((range1->type < XML_REGEXP_LETTER) ||
2111 (range2->type < XML_REGEXP_LETTER)) {
2112 if ((range1->type == XML_REGEXP_ANYSPACE) &&
2113 (range2->type == XML_REGEXP_NOTSPACE))
2114 ret = 0;
2115 else if ((range1->type == XML_REGEXP_INITNAME) &&
2116 (range2->type == XML_REGEXP_NOTINITNAME))
2117 ret = 0;
2118 else if ((range1->type == XML_REGEXP_NAMECHAR) &&
2119 (range2->type == XML_REGEXP_NOTNAMECHAR))
2120 ret = 0;
2121 else if ((range1->type == XML_REGEXP_DECIMAL) &&
2122 (range2->type == XML_REGEXP_NOTDECIMAL))
2123 ret = 0;
2124 else if ((range1->type == XML_REGEXP_REALCHAR) &&
2125 (range2->type == XML_REGEXP_NOTREALCHAR))
2126 ret = 0;
2127 else {
2128 /* same thing to limit complexity */
2129 return(1);
2130 }
2131 } else {
2132 ret = 0;
2133 /* range1->type < range2->type here */
2134 switch (range1->type) {
2135 case XML_REGEXP_LETTER:
2136 /* all disjoint except in the subgroups */
2137 if ((range2->type == XML_REGEXP_LETTER_UPPERCASE) ||
2138 (range2->type == XML_REGEXP_LETTER_LOWERCASE) ||
2139 (range2->type == XML_REGEXP_LETTER_TITLECASE) ||
2140 (range2->type == XML_REGEXP_LETTER_MODIFIER) ||
2141 (range2->type == XML_REGEXP_LETTER_OTHERS))
2142 ret = 1;
2143 break;
2144 case XML_REGEXP_MARK:
2145 if ((range2->type == XML_REGEXP_MARK_NONSPACING) ||
2146 (range2->type == XML_REGEXP_MARK_SPACECOMBINING) ||
2147 (range2->type == XML_REGEXP_MARK_ENCLOSING))
2148 ret = 1;
2149 break;
2150 case XML_REGEXP_NUMBER:
2151 if ((range2->type == XML_REGEXP_NUMBER_DECIMAL) ||
2152 (range2->type == XML_REGEXP_NUMBER_LETTER) ||
2153 (range2->type == XML_REGEXP_NUMBER_OTHERS))
2154 ret = 1;
2155 break;
2156 case XML_REGEXP_PUNCT:
2157 if ((range2->type == XML_REGEXP_PUNCT_CONNECTOR) ||
2158 (range2->type == XML_REGEXP_PUNCT_DASH) ||
2159 (range2->type == XML_REGEXP_PUNCT_OPEN) ||
2160 (range2->type == XML_REGEXP_PUNCT_CLOSE) ||
2161 (range2->type == XML_REGEXP_PUNCT_INITQUOTE) ||
2162 (range2->type == XML_REGEXP_PUNCT_FINQUOTE) ||
2163 (range2->type == XML_REGEXP_PUNCT_OTHERS))
2164 ret = 1;
2165 break;
2166 case XML_REGEXP_SEPAR:
2167 if ((range2->type == XML_REGEXP_SEPAR_SPACE) ||
2168 (range2->type == XML_REGEXP_SEPAR_LINE) ||
2169 (range2->type == XML_REGEXP_SEPAR_PARA))
2170 ret = 1;
2171 break;
2172 case XML_REGEXP_SYMBOL:
2173 if ((range2->type == XML_REGEXP_SYMBOL_MATH) ||
2174 (range2->type == XML_REGEXP_SYMBOL_CURRENCY) ||
2175 (range2->type == XML_REGEXP_SYMBOL_MODIFIER) ||
2176 (range2->type == XML_REGEXP_SYMBOL_OTHERS))
2177 ret = 1;
2178 break;
2179 case XML_REGEXP_OTHER:
2180 if ((range2->type == XML_REGEXP_OTHER_CONTROL) ||
2181 (range2->type == XML_REGEXP_OTHER_FORMAT) ||
2182 (range2->type == XML_REGEXP_OTHER_PRIVATE))
2183 ret = 1;
2184 break;
2185 default:
2186 if ((range2->type >= XML_REGEXP_LETTER) &&
2187 (range2->type < XML_REGEXP_BLOCK_NAME))
2188 ret = 0;
2189 else {
2190 /* safety net ! */
2191 return(1);
2192 }
2193 }
2194 }
2195 if (((range1->neg == 0) && (range2->neg != 0)) ||
2196 ((range1->neg != 0) && (range2->neg == 0)))
2197 ret = !ret;
2198 return(1);
2199}
2200
Daniel Veillarde19fc232002-04-22 16:01:24 +00002201/**
Daniel Veillardfc011b72006-02-12 19:14:15 +00002202 * xmlFACompareAtomTypes:
2203 * @type1: an atom type
2204 * @type2: an atom type
2205 *
2206 * Compares two atoms type to check whether they intersect in some ways,
2207 * this is used by xmlFACompareAtoms only
2208 *
2209 * Returns 1 if they may intersect and 0 otherwise
2210 */
2211static int
2212xmlFACompareAtomTypes(xmlRegAtomType type1, xmlRegAtomType type2) {
2213 if ((type1 == XML_REGEXP_EPSILON) ||
2214 (type1 == XML_REGEXP_CHARVAL) ||
2215 (type1 == XML_REGEXP_RANGES) ||
2216 (type1 == XML_REGEXP_SUBREG) ||
2217 (type1 == XML_REGEXP_STRING) ||
2218 (type1 == XML_REGEXP_ANYCHAR))
2219 return(1);
2220 if ((type2 == XML_REGEXP_EPSILON) ||
2221 (type2 == XML_REGEXP_CHARVAL) ||
2222 (type2 == XML_REGEXP_RANGES) ||
2223 (type2 == XML_REGEXP_SUBREG) ||
2224 (type2 == XML_REGEXP_STRING) ||
2225 (type2 == XML_REGEXP_ANYCHAR))
2226 return(1);
2227
2228 if (type1 == type2) return(1);
2229
2230 /* simplify subsequent compares by making sure type1 < type2 */
2231 if (type1 > type2) {
2232 xmlRegAtomType tmp = type1;
2233 type1 = type2;
2234 type2 = tmp;
2235 }
2236 switch (type1) {
2237 case XML_REGEXP_ANYSPACE: /* \s */
2238 /* can't be a letter, number, mark, pontuation, symbol */
2239 if ((type2 == XML_REGEXP_NOTSPACE) ||
2240 ((type2 >= XML_REGEXP_LETTER) &&
2241 (type2 <= XML_REGEXP_LETTER_OTHERS)) ||
2242 ((type2 >= XML_REGEXP_NUMBER) &&
2243 (type2 <= XML_REGEXP_NUMBER_OTHERS)) ||
2244 ((type2 >= XML_REGEXP_MARK) &&
2245 (type2 <= XML_REGEXP_MARK_ENCLOSING)) ||
2246 ((type2 >= XML_REGEXP_PUNCT) &&
2247 (type2 <= XML_REGEXP_PUNCT_OTHERS)) ||
2248 ((type2 >= XML_REGEXP_SYMBOL) &&
2249 (type2 <= XML_REGEXP_SYMBOL_OTHERS))
2250 ) return(0);
2251 break;
2252 case XML_REGEXP_NOTSPACE: /* \S */
2253 break;
2254 case XML_REGEXP_INITNAME: /* \l */
2255 /* can't be a number, mark, separator, pontuation, symbol or other */
2256 if ((type2 == XML_REGEXP_NOTINITNAME) ||
2257 ((type2 >= XML_REGEXP_NUMBER) &&
2258 (type2 <= XML_REGEXP_NUMBER_OTHERS)) ||
2259 ((type2 >= XML_REGEXP_MARK) &&
2260 (type2 <= XML_REGEXP_MARK_ENCLOSING)) ||
2261 ((type2 >= XML_REGEXP_SEPAR) &&
2262 (type2 <= XML_REGEXP_SEPAR_PARA)) ||
2263 ((type2 >= XML_REGEXP_PUNCT) &&
2264 (type2 <= XML_REGEXP_PUNCT_OTHERS)) ||
2265 ((type2 >= XML_REGEXP_SYMBOL) &&
2266 (type2 <= XML_REGEXP_SYMBOL_OTHERS)) ||
2267 ((type2 >= XML_REGEXP_OTHER) &&
2268 (type2 <= XML_REGEXP_OTHER_NA))
2269 ) return(0);
2270 break;
2271 case XML_REGEXP_NOTINITNAME: /* \L */
2272 break;
2273 case XML_REGEXP_NAMECHAR: /* \c */
2274 /* can't be a mark, separator, pontuation, symbol or other */
2275 if ((type2 == XML_REGEXP_NOTNAMECHAR) ||
2276 ((type2 >= XML_REGEXP_MARK) &&
2277 (type2 <= XML_REGEXP_MARK_ENCLOSING)) ||
2278 ((type2 >= XML_REGEXP_PUNCT) &&
2279 (type2 <= XML_REGEXP_PUNCT_OTHERS)) ||
2280 ((type2 >= XML_REGEXP_SEPAR) &&
2281 (type2 <= XML_REGEXP_SEPAR_PARA)) ||
2282 ((type2 >= XML_REGEXP_SYMBOL) &&
2283 (type2 <= XML_REGEXP_SYMBOL_OTHERS)) ||
2284 ((type2 >= XML_REGEXP_OTHER) &&
2285 (type2 <= XML_REGEXP_OTHER_NA))
2286 ) return(0);
2287 break;
2288 case XML_REGEXP_NOTNAMECHAR: /* \C */
2289 break;
2290 case XML_REGEXP_DECIMAL: /* \d */
2291 /* can't be a letter, mark, separator, pontuation, symbol or other */
2292 if ((type2 == XML_REGEXP_NOTDECIMAL) ||
2293 (type2 == XML_REGEXP_REALCHAR) ||
2294 ((type2 >= XML_REGEXP_LETTER) &&
2295 (type2 <= XML_REGEXP_LETTER_OTHERS)) ||
2296 ((type2 >= XML_REGEXP_MARK) &&
2297 (type2 <= XML_REGEXP_MARK_ENCLOSING)) ||
2298 ((type2 >= XML_REGEXP_PUNCT) &&
2299 (type2 <= XML_REGEXP_PUNCT_OTHERS)) ||
2300 ((type2 >= XML_REGEXP_SEPAR) &&
2301 (type2 <= XML_REGEXP_SEPAR_PARA)) ||
2302 ((type2 >= XML_REGEXP_SYMBOL) &&
2303 (type2 <= XML_REGEXP_SYMBOL_OTHERS)) ||
2304 ((type2 >= XML_REGEXP_OTHER) &&
2305 (type2 <= XML_REGEXP_OTHER_NA))
2306 )return(0);
2307 break;
2308 case XML_REGEXP_NOTDECIMAL: /* \D */
2309 break;
2310 case XML_REGEXP_REALCHAR: /* \w */
2311 /* can't be a mark, separator, pontuation, symbol or other */
2312 if ((type2 == XML_REGEXP_NOTDECIMAL) ||
2313 ((type2 >= XML_REGEXP_MARK) &&
2314 (type2 <= XML_REGEXP_MARK_ENCLOSING)) ||
2315 ((type2 >= XML_REGEXP_PUNCT) &&
2316 (type2 <= XML_REGEXP_PUNCT_OTHERS)) ||
2317 ((type2 >= XML_REGEXP_SEPAR) &&
2318 (type2 <= XML_REGEXP_SEPAR_PARA)) ||
2319 ((type2 >= XML_REGEXP_SYMBOL) &&
2320 (type2 <= XML_REGEXP_SYMBOL_OTHERS)) ||
2321 ((type2 >= XML_REGEXP_OTHER) &&
2322 (type2 <= XML_REGEXP_OTHER_NA))
2323 )return(0);
2324 break;
2325 case XML_REGEXP_NOTREALCHAR: /* \W */
2326 break;
2327 /*
2328 * at that point we know both type 1 and type2 are from
2329 * character categories are ordered and are different,
2330 * it becomes simple because this is a partition
2331 */
2332 case XML_REGEXP_LETTER:
2333 if (type2 <= XML_REGEXP_LETTER_OTHERS)
2334 return(1);
2335 return(0);
2336 case XML_REGEXP_LETTER_UPPERCASE:
2337 case XML_REGEXP_LETTER_LOWERCASE:
2338 case XML_REGEXP_LETTER_TITLECASE:
2339 case XML_REGEXP_LETTER_MODIFIER:
2340 case XML_REGEXP_LETTER_OTHERS:
2341 return(0);
2342 case XML_REGEXP_MARK:
2343 if (type2 <= XML_REGEXP_MARK_ENCLOSING)
2344 return(1);
2345 return(0);
2346 case XML_REGEXP_MARK_NONSPACING:
2347 case XML_REGEXP_MARK_SPACECOMBINING:
2348 case XML_REGEXP_MARK_ENCLOSING:
2349 return(0);
2350 case XML_REGEXP_NUMBER:
2351 if (type2 <= XML_REGEXP_NUMBER_OTHERS)
2352 return(1);
2353 return(0);
2354 case XML_REGEXP_NUMBER_DECIMAL:
2355 case XML_REGEXP_NUMBER_LETTER:
2356 case XML_REGEXP_NUMBER_OTHERS:
2357 return(0);
2358 case XML_REGEXP_PUNCT:
2359 if (type2 <= XML_REGEXP_PUNCT_OTHERS)
2360 return(1);
2361 return(0);
2362 case XML_REGEXP_PUNCT_CONNECTOR:
2363 case XML_REGEXP_PUNCT_DASH:
2364 case XML_REGEXP_PUNCT_OPEN:
2365 case XML_REGEXP_PUNCT_CLOSE:
2366 case XML_REGEXP_PUNCT_INITQUOTE:
2367 case XML_REGEXP_PUNCT_FINQUOTE:
2368 case XML_REGEXP_PUNCT_OTHERS:
2369 return(0);
2370 case XML_REGEXP_SEPAR:
2371 if (type2 <= XML_REGEXP_SEPAR_PARA)
2372 return(1);
2373 return(0);
2374 case XML_REGEXP_SEPAR_SPACE:
2375 case XML_REGEXP_SEPAR_LINE:
2376 case XML_REGEXP_SEPAR_PARA:
2377 return(0);
2378 case XML_REGEXP_SYMBOL:
2379 if (type2 <= XML_REGEXP_SYMBOL_OTHERS)
2380 return(1);
2381 return(0);
2382 case XML_REGEXP_SYMBOL_MATH:
2383 case XML_REGEXP_SYMBOL_CURRENCY:
2384 case XML_REGEXP_SYMBOL_MODIFIER:
2385 case XML_REGEXP_SYMBOL_OTHERS:
2386 return(0);
2387 case XML_REGEXP_OTHER:
2388 if (type2 <= XML_REGEXP_OTHER_NA)
2389 return(1);
2390 return(0);
2391 case XML_REGEXP_OTHER_CONTROL:
2392 case XML_REGEXP_OTHER_FORMAT:
2393 case XML_REGEXP_OTHER_PRIVATE:
2394 case XML_REGEXP_OTHER_NA:
2395 return(0);
2396 default:
2397 break;
2398 }
2399 return(1);
2400}
2401
2402/**
2403 * xmlFAEqualAtoms:
Daniel Veillarde19fc232002-04-22 16:01:24 +00002404 * @atom1: an atom
2405 * @atom2: an atom
2406 *
Daniel Veillardfc011b72006-02-12 19:14:15 +00002407 * Compares two atoms to check whether they are the same exactly
2408 * this is used to remove equivalent transitions
Daniel Veillarde19fc232002-04-22 16:01:24 +00002409 *
Daniel Veillardfc011b72006-02-12 19:14:15 +00002410 * Returns 1 if same and 0 otherwise
Daniel Veillarde19fc232002-04-22 16:01:24 +00002411 */
2412static int
Daniel Veillardfc011b72006-02-12 19:14:15 +00002413xmlFAEqualAtoms(xmlRegAtomPtr atom1, xmlRegAtomPtr atom2) {
2414 int ret = 0;
Daniel Veillard9efc4762005-07-19 14:33:55 +00002415
Daniel Veillarde19fc232002-04-22 16:01:24 +00002416 if (atom1 == atom2)
2417 return(1);
2418 if ((atom1 == NULL) || (atom2 == NULL))
2419 return(0);
2420
Daniel Veillardfc011b72006-02-12 19:14:15 +00002421 if (atom1->type != atom2->type)
2422 return(0);
2423 switch (atom1->type) {
2424 case XML_REGEXP_EPSILON:
2425 ret = 0;
2426 break;
2427 case XML_REGEXP_STRING:
2428 ret = xmlStrEqual((xmlChar *)atom1->valuep,
2429 (xmlChar *)atom2->valuep);
2430 break;
2431 case XML_REGEXP_CHARVAL:
2432 ret = (atom1->codepoint == atom2->codepoint);
2433 break;
2434 case XML_REGEXP_RANGES:
2435 /* too hard to do in the general case */
2436 ret = 0;
2437 default:
2438 break;
2439 }
2440 return(ret);
2441}
2442
2443/**
2444 * xmlFACompareAtoms:
2445 * @atom1: an atom
2446 * @atom2: an atom
2447 *
2448 * Compares two atoms to check whether they intersect in some ways,
2449 * this is used by xmlFAComputesDeterminism and xmlFARecurseDeterminism only
2450 *
2451 * Returns 1 if yes and 0 otherwise
2452 */
2453static int
2454xmlFACompareAtoms(xmlRegAtomPtr atom1, xmlRegAtomPtr atom2) {
2455 int ret = 1;
2456
2457 if (atom1 == atom2)
2458 return(1);
2459 if ((atom1 == NULL) || (atom2 == NULL))
2460 return(0);
2461
2462 if ((atom1->type == XML_REGEXP_ANYCHAR) ||
2463 (atom2->type == XML_REGEXP_ANYCHAR))
2464 return(1);
2465
2466 if (atom1->type > atom2->type) {
Daniel Veillard567a45b2005-10-18 19:11:55 +00002467 xmlRegAtomPtr tmp;
2468 tmp = atom1;
2469 atom1 = atom2;
2470 atom2 = tmp;
Daniel Veillardfc011b72006-02-12 19:14:15 +00002471 }
2472 if (atom1->type != atom2->type) {
2473 ret = xmlFACompareAtomTypes(atom1->type, atom2->type);
2474 /* if they can't intersect at the type level break now */
2475 if (ret == 0)
2476 return(0);
Daniel Veillard567a45b2005-10-18 19:11:55 +00002477 }
Daniel Veillarde19fc232002-04-22 16:01:24 +00002478 switch (atom1->type) {
2479 case XML_REGEXP_STRING:
Daniel Veillard9efc4762005-07-19 14:33:55 +00002480 ret = xmlRegStrEqualWildcard((xmlChar *)atom1->valuep,
2481 (xmlChar *)atom2->valuep);
2482 break;
Daniel Veillarde19fc232002-04-22 16:01:24 +00002483 case XML_REGEXP_EPSILON:
Daniel Veillard567a45b2005-10-18 19:11:55 +00002484 goto not_determinist;
Daniel Veillarde19fc232002-04-22 16:01:24 +00002485 case XML_REGEXP_CHARVAL:
Daniel Veillardfc011b72006-02-12 19:14:15 +00002486 if (atom2->type == XML_REGEXP_CHARVAL) {
2487 ret = (atom1->codepoint == atom2->codepoint);
2488 } else {
2489 ret = xmlRegCheckCharacter(atom2, atom1->codepoint);
2490 if (ret < 0)
2491 ret = 1;
2492 }
Daniel Veillard9efc4762005-07-19 14:33:55 +00002493 break;
Daniel Veillarde19fc232002-04-22 16:01:24 +00002494 case XML_REGEXP_RANGES:
Daniel Veillardfc011b72006-02-12 19:14:15 +00002495 if (atom2->type == XML_REGEXP_RANGES) {
Daniel Veillard567a45b2005-10-18 19:11:55 +00002496 int i, j, res;
2497 xmlRegRangePtr r1, r2;
2498
2499 /*
2500 * need to check that none of the ranges eventually matches
2501 */
2502 for (i = 0;i < atom1->nbRanges;i++) {
2503 for (j = 0;j < atom2->nbRanges;j++) {
2504 r1 = atom1->ranges[i];
2505 r2 = atom2->ranges[j];
2506 res = xmlFACompareRanges(r1, r2);
2507 if (res == 1) {
2508 ret = 1;
2509 goto done;
2510 }
2511 }
2512 }
2513 ret = 0;
2514 }
2515 break;
Daniel Veillarde19fc232002-04-22 16:01:24 +00002516 default:
Daniel Veillard567a45b2005-10-18 19:11:55 +00002517 goto not_determinist;
Daniel Veillarde19fc232002-04-22 16:01:24 +00002518 }
Daniel Veillard567a45b2005-10-18 19:11:55 +00002519done:
Daniel Veillard6e65e152005-08-09 11:09:52 +00002520 if (atom1->neg != atom2->neg) {
Daniel Veillard9efc4762005-07-19 14:33:55 +00002521 ret = !ret;
Daniel Veillard6e65e152005-08-09 11:09:52 +00002522 }
Daniel Veillard567a45b2005-10-18 19:11:55 +00002523 if (ret == 0)
2524 return(0);
2525not_determinist:
2526 return(1);
Daniel Veillarde19fc232002-04-22 16:01:24 +00002527}
2528
2529/**
2530 * xmlFARecurseDeterminism:
2531 * @ctxt: a regexp parser context
2532 *
2533 * Check whether the associated regexp is determinist,
2534 * should be called after xmlFAEliminateEpsilonTransitions()
2535 *
2536 */
2537static int
2538xmlFARecurseDeterminism(xmlRegParserCtxtPtr ctxt, xmlRegStatePtr state,
2539 int to, xmlRegAtomPtr atom) {
2540 int ret = 1;
Daniel Veillard567a45b2005-10-18 19:11:55 +00002541 int res;
Daniel Veillard5de09382005-09-26 17:18:17 +00002542 int transnr, nbTrans;
Daniel Veillarde19fc232002-04-22 16:01:24 +00002543 xmlRegTransPtr t1;
2544
2545 if (state == NULL)
2546 return(ret);
Daniel Veillard5de09382005-09-26 17:18:17 +00002547 /*
2548 * don't recurse on transitions potentially added in the course of
2549 * the elimination.
2550 */
2551 nbTrans = state->nbTrans;
2552 for (transnr = 0;transnr < nbTrans;transnr++) {
Daniel Veillarde19fc232002-04-22 16:01:24 +00002553 t1 = &(state->trans[transnr]);
2554 /*
2555 * check transitions conflicting with the one looked at
2556 */
2557 if (t1->atom == NULL) {
Daniel Veillard0e05f4c2006-11-01 15:33:04 +00002558 if (t1->to < 0)
Daniel Veillarde19fc232002-04-22 16:01:24 +00002559 continue;
Daniel Veillard567a45b2005-10-18 19:11:55 +00002560 res = xmlFARecurseDeterminism(ctxt, ctxt->states[t1->to],
Daniel Veillarde19fc232002-04-22 16:01:24 +00002561 to, atom);
Daniel Veillard567a45b2005-10-18 19:11:55 +00002562 if (res == 0) {
2563 ret = 0;
Daniel Veillardaa622012005-10-20 15:55:25 +00002564 /* t1->nd = 1; */
Daniel Veillard567a45b2005-10-18 19:11:55 +00002565 }
Daniel Veillarde19fc232002-04-22 16:01:24 +00002566 continue;
2567 }
2568 if (t1->to != to)
2569 continue;
Daniel Veillard567a45b2005-10-18 19:11:55 +00002570 if (xmlFACompareAtoms(t1->atom, atom)) {
2571 ret = 0;
2572 /* mark the transition as non-deterministic */
2573 t1->nd = 1;
2574 }
Daniel Veillarde19fc232002-04-22 16:01:24 +00002575 }
2576 return(ret);
2577}
2578
2579/**
2580 * xmlFAComputesDeterminism:
2581 * @ctxt: a regexp parser context
2582 *
2583 * Check whether the associated regexp is determinist,
2584 * should be called after xmlFAEliminateEpsilonTransitions()
2585 *
2586 */
2587static int
2588xmlFAComputesDeterminism(xmlRegParserCtxtPtr ctxt) {
2589 int statenr, transnr;
2590 xmlRegStatePtr state;
Daniel Veillard567a45b2005-10-18 19:11:55 +00002591 xmlRegTransPtr t1, t2, last;
Daniel Veillarde19fc232002-04-22 16:01:24 +00002592 int i;
2593 int ret = 1;
2594
Daniel Veillard4402ab42002-09-12 16:02:56 +00002595#ifdef DEBUG_REGEXP_GRAPH
2596 printf("xmlFAComputesDeterminism\n");
2597 xmlRegPrintCtxt(stdout, ctxt);
2598#endif
Daniel Veillarde19fc232002-04-22 16:01:24 +00002599 if (ctxt->determinist != -1)
2600 return(ctxt->determinist);
2601
2602 /*
Daniel Veillard567a45b2005-10-18 19:11:55 +00002603 * First cleanup the automata removing cancelled transitions
Daniel Veillarde19fc232002-04-22 16:01:24 +00002604 */
2605 for (statenr = 0;statenr < ctxt->nbStates;statenr++) {
2606 state = ctxt->states[statenr];
2607 if (state == NULL)
2608 continue;
Daniel Veillard4f82c8a2005-08-09 21:40:08 +00002609 if (state->nbTrans < 2)
2610 continue;
Daniel Veillarde19fc232002-04-22 16:01:24 +00002611 for (transnr = 0;transnr < state->nbTrans;transnr++) {
2612 t1 = &(state->trans[transnr]);
2613 /*
2614 * Determinism checks in case of counted or all transitions
2615 * will have to be handled separately
2616 */
Daniel Veillard567a45b2005-10-18 19:11:55 +00002617 if (t1->atom == NULL) {
Daniel Veillardaa622012005-10-20 15:55:25 +00002618 /* t1->nd = 1; */
Daniel Veillarde19fc232002-04-22 16:01:24 +00002619 continue;
Daniel Veillard567a45b2005-10-18 19:11:55 +00002620 }
Daniel Veillarde19fc232002-04-22 16:01:24 +00002621 if (t1->to == -1) /* eliminated */
2622 continue;
2623 for (i = 0;i < transnr;i++) {
2624 t2 = &(state->trans[i]);
2625 if (t2->to == -1) /* eliminated */
2626 continue;
2627 if (t2->atom != NULL) {
2628 if (t1->to == t2->to) {
Daniel Veillardfc011b72006-02-12 19:14:15 +00002629 if (xmlFAEqualAtoms(t1->atom, t2->atom))
William M. Brackddf71d62004-05-06 04:17:26 +00002630 t2->to = -1; /* eliminated */
Daniel Veillard567a45b2005-10-18 19:11:55 +00002631 }
2632 }
2633 }
2634 }
2635 }
2636
2637 /*
2638 * Check for all states that there aren't 2 transitions
2639 * with the same atom and a different target.
2640 */
2641 for (statenr = 0;statenr < ctxt->nbStates;statenr++) {
2642 state = ctxt->states[statenr];
2643 if (state == NULL)
2644 continue;
2645 if (state->nbTrans < 2)
2646 continue;
2647 last = NULL;
2648 for (transnr = 0;transnr < state->nbTrans;transnr++) {
2649 t1 = &(state->trans[transnr]);
2650 /*
2651 * Determinism checks in case of counted or all transitions
2652 * will have to be handled separately
2653 */
2654 if (t1->atom == NULL) {
2655 continue;
2656 }
2657 if (t1->to == -1) /* eliminated */
2658 continue;
2659 for (i = 0;i < transnr;i++) {
2660 t2 = &(state->trans[i]);
2661 if (t2->to == -1) /* eliminated */
2662 continue;
2663 if (t2->atom != NULL) {
2664 /* not determinist ! */
2665 if (xmlFACompareAtoms(t1->atom, t2->atom)) {
2666 ret = 0;
2667 /* mark the transitions as non-deterministic ones */
2668 t1->nd = 1;
2669 t2->nd = 1;
2670 last = t1;
Daniel Veillarde19fc232002-04-22 16:01:24 +00002671 }
2672 } else if (t1->to != -1) {
2673 /*
2674 * do the closure in case of remaining specific
2675 * epsilon transitions like choices or all
2676 */
2677 ret = xmlFARecurseDeterminism(ctxt, ctxt->states[t1->to],
2678 t2->to, t2->atom);
Daniel Veillard567a45b2005-10-18 19:11:55 +00002679 /* don't shortcut the computation so all non deterministic
2680 transition get marked down
Daniel Veillarde19fc232002-04-22 16:01:24 +00002681 if (ret == 0)
Daniel Veillardaa622012005-10-20 15:55:25 +00002682 return(0);
2683 */
Daniel Veillard567a45b2005-10-18 19:11:55 +00002684 if (ret == 0) {
2685 t1->nd = 1;
Daniel Veillardaa622012005-10-20 15:55:25 +00002686 /* t2->nd = 1; */
Daniel Veillard567a45b2005-10-18 19:11:55 +00002687 last = t1;
2688 }
Daniel Veillarde19fc232002-04-22 16:01:24 +00002689 }
2690 }
Daniel Veillard567a45b2005-10-18 19:11:55 +00002691 /* don't shortcut the computation so all non deterministic
2692 transition get marked down
Daniel Veillarde19fc232002-04-22 16:01:24 +00002693 if (ret == 0)
Daniel Veillard567a45b2005-10-18 19:11:55 +00002694 break; */
Daniel Veillarde19fc232002-04-22 16:01:24 +00002695 }
Daniel Veillard567a45b2005-10-18 19:11:55 +00002696
2697 /*
2698 * mark specifically the last non-deterministic transition
2699 * from a state since there is no need to set-up rollback
2700 * from it
2701 */
2702 if (last != NULL) {
2703 last->nd = 2;
2704 }
2705
2706 /* 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 Veillard567a45b2005-10-18 19:11:55 +00002709 break; */
Daniel Veillarde19fc232002-04-22 16:01:24 +00002710 }
Daniel Veillard567a45b2005-10-18 19:11:55 +00002711
Daniel Veillarde19fc232002-04-22 16:01:24 +00002712 ctxt->determinist = ret;
2713 return(ret);
2714}
2715
Daniel Veillard4255d502002-04-16 15:50:10 +00002716/************************************************************************
2717 * *
2718 * Routines to check input against transition atoms *
2719 * *
2720 ************************************************************************/
2721
2722static int
2723xmlRegCheckCharacterRange(xmlRegAtomType type, int codepoint, int neg,
2724 int start, int end, const xmlChar *blockName) {
2725 int ret = 0;
2726
2727 switch (type) {
2728 case XML_REGEXP_STRING:
2729 case XML_REGEXP_SUBREG:
2730 case XML_REGEXP_RANGES:
2731 case XML_REGEXP_EPSILON:
2732 return(-1);
2733 case XML_REGEXP_ANYCHAR:
2734 ret = ((codepoint != '\n') && (codepoint != '\r'));
2735 break;
2736 case XML_REGEXP_CHARVAL:
2737 ret = ((codepoint >= start) && (codepoint <= end));
2738 break;
2739 case XML_REGEXP_NOTSPACE:
2740 neg = !neg;
2741 case XML_REGEXP_ANYSPACE:
2742 ret = ((codepoint == '\n') || (codepoint == '\r') ||
2743 (codepoint == '\t') || (codepoint == ' '));
2744 break;
2745 case XML_REGEXP_NOTINITNAME:
2746 neg = !neg;
2747 case XML_REGEXP_INITNAME:
William M. Brack871611b2003-10-18 04:53:14 +00002748 ret = (IS_LETTER(codepoint) ||
Daniel Veillard4255d502002-04-16 15:50:10 +00002749 (codepoint == '_') || (codepoint == ':'));
2750 break;
2751 case XML_REGEXP_NOTNAMECHAR:
2752 neg = !neg;
2753 case XML_REGEXP_NAMECHAR:
William M. Brack871611b2003-10-18 04:53:14 +00002754 ret = (IS_LETTER(codepoint) || IS_DIGIT(codepoint) ||
Daniel Veillard4255d502002-04-16 15:50:10 +00002755 (codepoint == '.') || (codepoint == '-') ||
2756 (codepoint == '_') || (codepoint == ':') ||
William M. Brack871611b2003-10-18 04:53:14 +00002757 IS_COMBINING(codepoint) || IS_EXTENDER(codepoint));
Daniel Veillard4255d502002-04-16 15:50:10 +00002758 break;
2759 case XML_REGEXP_NOTDECIMAL:
2760 neg = !neg;
2761 case XML_REGEXP_DECIMAL:
2762 ret = xmlUCSIsCatNd(codepoint);
2763 break;
2764 case XML_REGEXP_REALCHAR:
2765 neg = !neg;
2766 case XML_REGEXP_NOTREALCHAR:
2767 ret = xmlUCSIsCatP(codepoint);
2768 if (ret == 0)
2769 ret = xmlUCSIsCatZ(codepoint);
2770 if (ret == 0)
2771 ret = xmlUCSIsCatC(codepoint);
2772 break;
2773 case XML_REGEXP_LETTER:
2774 ret = xmlUCSIsCatL(codepoint);
2775 break;
2776 case XML_REGEXP_LETTER_UPPERCASE:
2777 ret = xmlUCSIsCatLu(codepoint);
2778 break;
2779 case XML_REGEXP_LETTER_LOWERCASE:
2780 ret = xmlUCSIsCatLl(codepoint);
2781 break;
2782 case XML_REGEXP_LETTER_TITLECASE:
2783 ret = xmlUCSIsCatLt(codepoint);
2784 break;
2785 case XML_REGEXP_LETTER_MODIFIER:
2786 ret = xmlUCSIsCatLm(codepoint);
2787 break;
2788 case XML_REGEXP_LETTER_OTHERS:
2789 ret = xmlUCSIsCatLo(codepoint);
2790 break;
2791 case XML_REGEXP_MARK:
2792 ret = xmlUCSIsCatM(codepoint);
2793 break;
2794 case XML_REGEXP_MARK_NONSPACING:
2795 ret = xmlUCSIsCatMn(codepoint);
2796 break;
2797 case XML_REGEXP_MARK_SPACECOMBINING:
2798 ret = xmlUCSIsCatMc(codepoint);
2799 break;
2800 case XML_REGEXP_MARK_ENCLOSING:
2801 ret = xmlUCSIsCatMe(codepoint);
2802 break;
2803 case XML_REGEXP_NUMBER:
2804 ret = xmlUCSIsCatN(codepoint);
2805 break;
2806 case XML_REGEXP_NUMBER_DECIMAL:
2807 ret = xmlUCSIsCatNd(codepoint);
2808 break;
2809 case XML_REGEXP_NUMBER_LETTER:
2810 ret = xmlUCSIsCatNl(codepoint);
2811 break;
2812 case XML_REGEXP_NUMBER_OTHERS:
2813 ret = xmlUCSIsCatNo(codepoint);
2814 break;
2815 case XML_REGEXP_PUNCT:
2816 ret = xmlUCSIsCatP(codepoint);
2817 break;
2818 case XML_REGEXP_PUNCT_CONNECTOR:
2819 ret = xmlUCSIsCatPc(codepoint);
2820 break;
2821 case XML_REGEXP_PUNCT_DASH:
2822 ret = xmlUCSIsCatPd(codepoint);
2823 break;
2824 case XML_REGEXP_PUNCT_OPEN:
2825 ret = xmlUCSIsCatPs(codepoint);
2826 break;
2827 case XML_REGEXP_PUNCT_CLOSE:
2828 ret = xmlUCSIsCatPe(codepoint);
2829 break;
2830 case XML_REGEXP_PUNCT_INITQUOTE:
2831 ret = xmlUCSIsCatPi(codepoint);
2832 break;
2833 case XML_REGEXP_PUNCT_FINQUOTE:
2834 ret = xmlUCSIsCatPf(codepoint);
2835 break;
2836 case XML_REGEXP_PUNCT_OTHERS:
2837 ret = xmlUCSIsCatPo(codepoint);
2838 break;
2839 case XML_REGEXP_SEPAR:
2840 ret = xmlUCSIsCatZ(codepoint);
2841 break;
2842 case XML_REGEXP_SEPAR_SPACE:
2843 ret = xmlUCSIsCatZs(codepoint);
2844 break;
2845 case XML_REGEXP_SEPAR_LINE:
2846 ret = xmlUCSIsCatZl(codepoint);
2847 break;
2848 case XML_REGEXP_SEPAR_PARA:
2849 ret = xmlUCSIsCatZp(codepoint);
2850 break;
2851 case XML_REGEXP_SYMBOL:
2852 ret = xmlUCSIsCatS(codepoint);
2853 break;
2854 case XML_REGEXP_SYMBOL_MATH:
2855 ret = xmlUCSIsCatSm(codepoint);
2856 break;
2857 case XML_REGEXP_SYMBOL_CURRENCY:
2858 ret = xmlUCSIsCatSc(codepoint);
2859 break;
2860 case XML_REGEXP_SYMBOL_MODIFIER:
2861 ret = xmlUCSIsCatSk(codepoint);
2862 break;
2863 case XML_REGEXP_SYMBOL_OTHERS:
2864 ret = xmlUCSIsCatSo(codepoint);
2865 break;
2866 case XML_REGEXP_OTHER:
2867 ret = xmlUCSIsCatC(codepoint);
2868 break;
2869 case XML_REGEXP_OTHER_CONTROL:
2870 ret = xmlUCSIsCatCc(codepoint);
2871 break;
2872 case XML_REGEXP_OTHER_FORMAT:
2873 ret = xmlUCSIsCatCf(codepoint);
2874 break;
2875 case XML_REGEXP_OTHER_PRIVATE:
2876 ret = xmlUCSIsCatCo(codepoint);
2877 break;
2878 case XML_REGEXP_OTHER_NA:
2879 /* ret = xmlUCSIsCatCn(codepoint); */
2880 /* Seems it doesn't exist anymore in recent Unicode releases */
2881 ret = 0;
2882 break;
2883 case XML_REGEXP_BLOCK_NAME:
2884 ret = xmlUCSIsBlock(codepoint, (const char *) blockName);
2885 break;
2886 }
2887 if (neg)
2888 return(!ret);
2889 return(ret);
2890}
2891
2892static int
2893xmlRegCheckCharacter(xmlRegAtomPtr atom, int codepoint) {
2894 int i, ret = 0;
2895 xmlRegRangePtr range;
2896
William M. Brack871611b2003-10-18 04:53:14 +00002897 if ((atom == NULL) || (!IS_CHAR(codepoint)))
Daniel Veillard4255d502002-04-16 15:50:10 +00002898 return(-1);
2899
2900 switch (atom->type) {
2901 case XML_REGEXP_SUBREG:
2902 case XML_REGEXP_EPSILON:
2903 return(-1);
2904 case XML_REGEXP_CHARVAL:
2905 return(codepoint == atom->codepoint);
2906 case XML_REGEXP_RANGES: {
2907 int accept = 0;
Daniel Veillardf2a12832003-11-24 13:04:35 +00002908
Daniel Veillard4255d502002-04-16 15:50:10 +00002909 for (i = 0;i < atom->nbRanges;i++) {
2910 range = atom->ranges[i];
Daniel Veillardf8b9de32003-11-24 14:27:26 +00002911 if (range->neg == 2) {
Daniel Veillard4255d502002-04-16 15:50:10 +00002912 ret = xmlRegCheckCharacterRange(range->type, codepoint,
2913 0, range->start, range->end,
2914 range->blockName);
2915 if (ret != 0)
2916 return(0); /* excluded char */
Daniel Veillardf8b9de32003-11-24 14:27:26 +00002917 } else if (range->neg) {
2918 ret = xmlRegCheckCharacterRange(range->type, codepoint,
2919 0, range->start, range->end,
2920 range->blockName);
2921 if (ret == 0)
Daniel Veillardf2a12832003-11-24 13:04:35 +00002922 accept = 1;
Daniel Veillardf8b9de32003-11-24 14:27:26 +00002923 else
2924 return(0);
Daniel Veillard4255d502002-04-16 15:50:10 +00002925 } else {
2926 ret = xmlRegCheckCharacterRange(range->type, codepoint,
2927 0, range->start, range->end,
2928 range->blockName);
2929 if (ret != 0)
2930 accept = 1; /* might still be excluded */
2931 }
2932 }
2933 return(accept);
2934 }
2935 case XML_REGEXP_STRING:
2936 printf("TODO: XML_REGEXP_STRING\n");
2937 return(-1);
2938 case XML_REGEXP_ANYCHAR:
2939 case XML_REGEXP_ANYSPACE:
2940 case XML_REGEXP_NOTSPACE:
2941 case XML_REGEXP_INITNAME:
2942 case XML_REGEXP_NOTINITNAME:
2943 case XML_REGEXP_NAMECHAR:
2944 case XML_REGEXP_NOTNAMECHAR:
2945 case XML_REGEXP_DECIMAL:
2946 case XML_REGEXP_NOTDECIMAL:
2947 case XML_REGEXP_REALCHAR:
2948 case XML_REGEXP_NOTREALCHAR:
2949 case XML_REGEXP_LETTER:
2950 case XML_REGEXP_LETTER_UPPERCASE:
2951 case XML_REGEXP_LETTER_LOWERCASE:
2952 case XML_REGEXP_LETTER_TITLECASE:
2953 case XML_REGEXP_LETTER_MODIFIER:
2954 case XML_REGEXP_LETTER_OTHERS:
2955 case XML_REGEXP_MARK:
2956 case XML_REGEXP_MARK_NONSPACING:
2957 case XML_REGEXP_MARK_SPACECOMBINING:
2958 case XML_REGEXP_MARK_ENCLOSING:
2959 case XML_REGEXP_NUMBER:
2960 case XML_REGEXP_NUMBER_DECIMAL:
2961 case XML_REGEXP_NUMBER_LETTER:
2962 case XML_REGEXP_NUMBER_OTHERS:
2963 case XML_REGEXP_PUNCT:
2964 case XML_REGEXP_PUNCT_CONNECTOR:
2965 case XML_REGEXP_PUNCT_DASH:
2966 case XML_REGEXP_PUNCT_OPEN:
2967 case XML_REGEXP_PUNCT_CLOSE:
2968 case XML_REGEXP_PUNCT_INITQUOTE:
2969 case XML_REGEXP_PUNCT_FINQUOTE:
2970 case XML_REGEXP_PUNCT_OTHERS:
2971 case XML_REGEXP_SEPAR:
2972 case XML_REGEXP_SEPAR_SPACE:
2973 case XML_REGEXP_SEPAR_LINE:
2974 case XML_REGEXP_SEPAR_PARA:
2975 case XML_REGEXP_SYMBOL:
2976 case XML_REGEXP_SYMBOL_MATH:
2977 case XML_REGEXP_SYMBOL_CURRENCY:
2978 case XML_REGEXP_SYMBOL_MODIFIER:
2979 case XML_REGEXP_SYMBOL_OTHERS:
2980 case XML_REGEXP_OTHER:
2981 case XML_REGEXP_OTHER_CONTROL:
2982 case XML_REGEXP_OTHER_FORMAT:
2983 case XML_REGEXP_OTHER_PRIVATE:
2984 case XML_REGEXP_OTHER_NA:
2985 case XML_REGEXP_BLOCK_NAME:
2986 ret = xmlRegCheckCharacterRange(atom->type, codepoint, 0, 0, 0,
2987 (const xmlChar *)atom->valuep);
2988 if (atom->neg)
2989 ret = !ret;
2990 break;
2991 }
2992 return(ret);
2993}
2994
2995/************************************************************************
2996 * *
William M. Brackddf71d62004-05-06 04:17:26 +00002997 * Saving and restoring state of an execution context *
Daniel Veillard4255d502002-04-16 15:50:10 +00002998 * *
2999 ************************************************************************/
3000
3001#ifdef DEBUG_REGEXP_EXEC
3002static void
3003xmlFARegDebugExec(xmlRegExecCtxtPtr exec) {
3004 printf("state: %d:%d:idx %d", exec->state->no, exec->transno, exec->index);
3005 if (exec->inputStack != NULL) {
3006 int i;
3007 printf(": ");
3008 for (i = 0;(i < 3) && (i < exec->inputStackNr);i++)
Daniel Veillard0e05f4c2006-11-01 15:33:04 +00003009 printf("%s ", (const char *)
3010 exec->inputStack[exec->inputStackNr - (i + 1)].value);
Daniel Veillard4255d502002-04-16 15:50:10 +00003011 } else {
3012 printf(": %s", &(exec->inputString[exec->index]));
3013 }
3014 printf("\n");
3015}
3016#endif
3017
3018static void
3019xmlFARegExecSave(xmlRegExecCtxtPtr exec) {
3020#ifdef DEBUG_REGEXP_EXEC
3021 printf("saving ");
3022 exec->transno++;
3023 xmlFARegDebugExec(exec);
3024 exec->transno--;
3025#endif
Daniel Veillard94cc1032005-09-15 13:09:00 +00003026#ifdef MAX_PUSH
3027 if (exec->nbPush > MAX_PUSH) {
3028 return;
3029 }
3030 exec->nbPush++;
3031#endif
Daniel Veillard4255d502002-04-16 15:50:10 +00003032
3033 if (exec->maxRollbacks == 0) {
3034 exec->maxRollbacks = 4;
3035 exec->rollbacks = (xmlRegExecRollback *) xmlMalloc(exec->maxRollbacks *
3036 sizeof(xmlRegExecRollback));
3037 if (exec->rollbacks == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00003038 xmlRegexpErrMemory(NULL, "saving regexp");
Daniel Veillard4255d502002-04-16 15:50:10 +00003039 exec->maxRollbacks = 0;
3040 return;
3041 }
3042 memset(exec->rollbacks, 0,
3043 exec->maxRollbacks * sizeof(xmlRegExecRollback));
3044 } else if (exec->nbRollbacks >= exec->maxRollbacks) {
3045 xmlRegExecRollback *tmp;
3046 int len = exec->maxRollbacks;
3047
3048 exec->maxRollbacks *= 2;
3049 tmp = (xmlRegExecRollback *) xmlRealloc(exec->rollbacks,
3050 exec->maxRollbacks * sizeof(xmlRegExecRollback));
3051 if (tmp == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00003052 xmlRegexpErrMemory(NULL, "saving regexp");
Daniel Veillard4255d502002-04-16 15:50:10 +00003053 exec->maxRollbacks /= 2;
3054 return;
3055 }
3056 exec->rollbacks = tmp;
3057 tmp = &exec->rollbacks[len];
3058 memset(tmp, 0, (exec->maxRollbacks - len) * sizeof(xmlRegExecRollback));
3059 }
3060 exec->rollbacks[exec->nbRollbacks].state = exec->state;
3061 exec->rollbacks[exec->nbRollbacks].index = exec->index;
3062 exec->rollbacks[exec->nbRollbacks].nextbranch = exec->transno + 1;
3063 if (exec->comp->nbCounters > 0) {
3064 if (exec->rollbacks[exec->nbRollbacks].counts == NULL) {
3065 exec->rollbacks[exec->nbRollbacks].counts = (int *)
3066 xmlMalloc(exec->comp->nbCounters * sizeof(int));
3067 if (exec->rollbacks[exec->nbRollbacks].counts == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00003068 xmlRegexpErrMemory(NULL, "saving regexp");
Daniel Veillard4255d502002-04-16 15:50:10 +00003069 exec->status = -5;
3070 return;
3071 }
3072 }
3073 memcpy(exec->rollbacks[exec->nbRollbacks].counts, exec->counts,
3074 exec->comp->nbCounters * sizeof(int));
3075 }
3076 exec->nbRollbacks++;
3077}
3078
3079static void
3080xmlFARegExecRollBack(xmlRegExecCtxtPtr exec) {
3081 if (exec->nbRollbacks <= 0) {
3082 exec->status = -1;
3083#ifdef DEBUG_REGEXP_EXEC
3084 printf("rollback failed on empty stack\n");
3085#endif
3086 return;
3087 }
3088 exec->nbRollbacks--;
3089 exec->state = exec->rollbacks[exec->nbRollbacks].state;
3090 exec->index = exec->rollbacks[exec->nbRollbacks].index;
3091 exec->transno = exec->rollbacks[exec->nbRollbacks].nextbranch;
3092 if (exec->comp->nbCounters > 0) {
3093 if (exec->rollbacks[exec->nbRollbacks].counts == NULL) {
3094 fprintf(stderr, "exec save: allocation failed");
3095 exec->status = -6;
3096 return;
3097 }
3098 memcpy(exec->counts, exec->rollbacks[exec->nbRollbacks].counts,
3099 exec->comp->nbCounters * sizeof(int));
3100 }
3101
3102#ifdef DEBUG_REGEXP_EXEC
3103 printf("restored ");
3104 xmlFARegDebugExec(exec);
3105#endif
3106}
3107
3108/************************************************************************
3109 * *
William M. Brackddf71d62004-05-06 04:17:26 +00003110 * Verifier, running an input against a compiled regexp *
Daniel Veillard4255d502002-04-16 15:50:10 +00003111 * *
3112 ************************************************************************/
3113
3114static int
3115xmlFARegExec(xmlRegexpPtr comp, const xmlChar *content) {
3116 xmlRegExecCtxt execval;
3117 xmlRegExecCtxtPtr exec = &execval;
Daniel Veillard567a45b2005-10-18 19:11:55 +00003118 int ret, codepoint = 0, len, deter;
Daniel Veillard4255d502002-04-16 15:50:10 +00003119
3120 exec->inputString = content;
3121 exec->index = 0;
Daniel Veillard94cc1032005-09-15 13:09:00 +00003122 exec->nbPush = 0;
Daniel Veillard4255d502002-04-16 15:50:10 +00003123 exec->determinist = 1;
3124 exec->maxRollbacks = 0;
3125 exec->nbRollbacks = 0;
3126 exec->rollbacks = NULL;
3127 exec->status = 0;
3128 exec->comp = comp;
3129 exec->state = comp->states[0];
3130 exec->transno = 0;
3131 exec->transcount = 0;
Daniel Veillardf2a12832003-11-24 13:04:35 +00003132 exec->inputStack = NULL;
3133 exec->inputStackMax = 0;
Daniel Veillard4255d502002-04-16 15:50:10 +00003134 if (comp->nbCounters > 0) {
3135 exec->counts = (int *) xmlMalloc(comp->nbCounters * sizeof(int));
Daniel Veillardff46a042003-10-08 08:53:17 +00003136 if (exec->counts == NULL) {
3137 xmlRegexpErrMemory(NULL, "running regexp");
Daniel Veillard4255d502002-04-16 15:50:10 +00003138 return(-1);
Daniel Veillardff46a042003-10-08 08:53:17 +00003139 }
Daniel Veillard4255d502002-04-16 15:50:10 +00003140 memset(exec->counts, 0, comp->nbCounters * sizeof(int));
3141 } else
3142 exec->counts = NULL;
3143 while ((exec->status == 0) &&
3144 ((exec->inputString[exec->index] != 0) ||
3145 (exec->state->type != XML_REGEXP_FINAL_STATE))) {
3146 xmlRegTransPtr trans;
3147 xmlRegAtomPtr atom;
3148
3149 /*
William M. Brack0e00b282004-04-26 15:40:47 +00003150 * If end of input on non-terminal state, rollback, however we may
Daniel Veillard4255d502002-04-16 15:50:10 +00003151 * still have epsilon like transition for counted transitions
William M. Brack0e00b282004-04-26 15:40:47 +00003152 * on counters, in that case don't break too early. Additionally,
3153 * if we are working on a range like "AB{0,2}", where B is not present,
3154 * we don't want to break.
Daniel Veillard4255d502002-04-16 15:50:10 +00003155 */
Daniel Veillard11ce4002006-03-10 00:36:23 +00003156 len = 1;
William M. Brack0e00b282004-04-26 15:40:47 +00003157 if ((exec->inputString[exec->index] == 0) && (exec->counts == NULL)) {
William M. Brackddf71d62004-05-06 04:17:26 +00003158 /*
3159 * if there is a transition, we must check if
3160 * atom allows minOccurs of 0
3161 */
3162 if (exec->transno < exec->state->nbTrans) {
William M. Brack0e00b282004-04-26 15:40:47 +00003163 trans = &exec->state->trans[exec->transno];
3164 if (trans->to >=0) {
3165 atom = trans->atom;
3166 if (!((atom->min == 0) && (atom->max > 0)))
3167 goto rollback;
3168 }
3169 } else
3170 goto rollback;
3171 }
Daniel Veillard4255d502002-04-16 15:50:10 +00003172
3173 exec->transcount = 0;
3174 for (;exec->transno < exec->state->nbTrans;exec->transno++) {
3175 trans = &exec->state->trans[exec->transno];
3176 if (trans->to < 0)
3177 continue;
3178 atom = trans->atom;
3179 ret = 0;
Daniel Veillard567a45b2005-10-18 19:11:55 +00003180 deter = 1;
Daniel Veillard4255d502002-04-16 15:50:10 +00003181 if (trans->count >= 0) {
3182 int count;
3183 xmlRegCounterPtr counter;
3184
Daniel Veillard11ce4002006-03-10 00:36:23 +00003185 if (exec->counts == NULL) {
3186 exec->status = -1;
3187 goto error;
3188 }
Daniel Veillard4255d502002-04-16 15:50:10 +00003189 /*
3190 * A counted transition.
3191 */
3192
3193 count = exec->counts[trans->count];
3194 counter = &exec->comp->counters[trans->count];
3195#ifdef DEBUG_REGEXP_EXEC
3196 printf("testing count %d: val %d, min %d, max %d\n",
3197 trans->count, count, counter->min, counter->max);
3198#endif
3199 ret = ((count >= counter->min) && (count <= counter->max));
Daniel Veillard567a45b2005-10-18 19:11:55 +00003200 if ((ret) && (counter->min != counter->max))
3201 deter = 0;
Daniel Veillard4255d502002-04-16 15:50:10 +00003202 } else if (atom == NULL) {
3203 fprintf(stderr, "epsilon transition left at runtime\n");
3204 exec->status = -2;
3205 break;
3206 } else if (exec->inputString[exec->index] != 0) {
3207 codepoint = CUR_SCHAR(&(exec->inputString[exec->index]), len);
3208 ret = xmlRegCheckCharacter(atom, codepoint);
William M. Brack0e00b282004-04-26 15:40:47 +00003209 if ((ret == 1) && (atom->min >= 0) && (atom->max > 0)) {
Daniel Veillard4255d502002-04-16 15:50:10 +00003210 xmlRegStatePtr to = comp->states[trans->to];
3211
3212 /*
3213 * this is a multiple input sequence
Daniel Veillardfc6eca02005-11-01 15:24:02 +00003214 * If there is a counter associated increment it now.
3215 * before potentially saving and rollback
Daniel Veillardc821e032007-08-28 17:33:45 +00003216 * do not increment if the counter is already over the
3217 * maximum limit in which case get to next transition
Daniel Veillard4255d502002-04-16 15:50:10 +00003218 */
Daniel Veillardfc6eca02005-11-01 15:24:02 +00003219 if (trans->counter >= 0) {
Daniel Veillardc821e032007-08-28 17:33:45 +00003220 xmlRegCounterPtr counter;
3221
3222 if ((exec->counts == NULL) ||
3223 (exec->comp == NULL) ||
3224 (exec->comp->counters == NULL)) {
Daniel Veillard11ce4002006-03-10 00:36:23 +00003225 exec->status = -1;
3226 goto error;
3227 }
Daniel Veillardc821e032007-08-28 17:33:45 +00003228 counter = &exec->comp->counters[trans->counter];
3229 if (exec->counts[trans->counter] >= counter->max)
3230 continue; /* for loop on transitions */
3231
Daniel Veillardfc6eca02005-11-01 15:24:02 +00003232#ifdef DEBUG_REGEXP_EXEC
3233 printf("Increasing count %d\n", trans->counter);
3234#endif
3235 exec->counts[trans->counter]++;
3236 }
Daniel Veillard4255d502002-04-16 15:50:10 +00003237 if (exec->state->nbTrans > exec->transno + 1) {
3238 xmlFARegExecSave(exec);
3239 }
3240 exec->transcount = 1;
3241 do {
3242 /*
3243 * Try to progress as much as possible on the input
3244 */
3245 if (exec->transcount == atom->max) {
3246 break;
3247 }
3248 exec->index += len;
3249 /*
3250 * End of input: stop here
3251 */
3252 if (exec->inputString[exec->index] == 0) {
3253 exec->index -= len;
3254 break;
3255 }
3256 if (exec->transcount >= atom->min) {
3257 int transno = exec->transno;
3258 xmlRegStatePtr state = exec->state;
3259
3260 /*
3261 * The transition is acceptable save it
3262 */
3263 exec->transno = -1; /* trick */
3264 exec->state = to;
3265 xmlFARegExecSave(exec);
3266 exec->transno = transno;
3267 exec->state = state;
3268 }
3269 codepoint = CUR_SCHAR(&(exec->inputString[exec->index]),
3270 len);
3271 ret = xmlRegCheckCharacter(atom, codepoint);
3272 exec->transcount++;
3273 } while (ret == 1);
3274 if (exec->transcount < atom->min)
3275 ret = 0;
3276
3277 /*
3278 * If the last check failed but one transition was found
3279 * possible, rollback
3280 */
3281 if (ret < 0)
3282 ret = 0;
3283 if (ret == 0) {
3284 goto rollback;
3285 }
Daniel Veillardfc6eca02005-11-01 15:24:02 +00003286 if (trans->counter >= 0) {
Daniel Veillard11ce4002006-03-10 00:36:23 +00003287 if (exec->counts == NULL) {
3288 exec->status = -1;
3289 goto error;
3290 }
Daniel Veillardfc6eca02005-11-01 15:24:02 +00003291#ifdef DEBUG_REGEXP_EXEC
3292 printf("Decreasing count %d\n", trans->counter);
3293#endif
3294 exec->counts[trans->counter]--;
3295 }
William M. Brack0e00b282004-04-26 15:40:47 +00003296 } else if ((ret == 0) && (atom->min == 0) && (atom->max > 0)) {
3297 /*
3298 * we don't match on the codepoint, but minOccurs of 0
3299 * says that's ok. Setting len to 0 inhibits stepping
3300 * over the codepoint.
3301 */
3302 exec->transcount = 1;
3303 len = 0;
3304 ret = 1;
Daniel Veillard4255d502002-04-16 15:50:10 +00003305 }
William M. Brack0e00b282004-04-26 15:40:47 +00003306 } else if ((atom->min == 0) && (atom->max > 0)) {
3307 /* another spot to match when minOccurs is 0 */
3308 exec->transcount = 1;
3309 len = 0;
3310 ret = 1;
Daniel Veillard4255d502002-04-16 15:50:10 +00003311 }
3312 if (ret == 1) {
Daniel Veillard567a45b2005-10-18 19:11:55 +00003313 if ((trans->nd == 1) ||
3314 ((trans->count >= 0) && (deter == 0) &&
3315 (exec->state->nbTrans > exec->transno + 1))) {
Daniel Veillardaa622012005-10-20 15:55:25 +00003316#ifdef DEBUG_REGEXP_EXEC
3317 if (trans->nd == 1)
3318 printf("Saving on nd transition atom %d for %c at %d\n",
3319 trans->atom->no, codepoint, exec->index);
3320 else
3321 printf("Saving on counted transition count %d for %c at %d\n",
3322 trans->count, codepoint, exec->index);
3323#endif
Daniel Veillard4255d502002-04-16 15:50:10 +00003324 xmlFARegExecSave(exec);
3325 }
3326 if (trans->counter >= 0) {
Daniel Veillardc821e032007-08-28 17:33:45 +00003327 xmlRegCounterPtr counter;
3328
3329 /* make sure we don't go over the counter maximum value */
3330 if ((exec->counts == NULL) ||
3331 (exec->comp == NULL) ||
3332 (exec->comp->counters == NULL)) {
3333 exec->status = -1;
Daniel Veillard11ce4002006-03-10 00:36:23 +00003334 goto error;
3335 }
Daniel Veillardc821e032007-08-28 17:33:45 +00003336 counter = &exec->comp->counters[trans->counter];
3337 if (exec->counts[trans->counter] >= counter->max)
3338 continue; /* for loop on transitions */
Daniel Veillard4255d502002-04-16 15:50:10 +00003339#ifdef DEBUG_REGEXP_EXEC
3340 printf("Increasing count %d\n", trans->counter);
3341#endif
3342 exec->counts[trans->counter]++;
3343 }
Daniel Veillard10752282005-08-08 13:05:13 +00003344 if ((trans->count >= 0) &&
3345 (trans->count < REGEXP_ALL_COUNTER)) {
Daniel Veillard11ce4002006-03-10 00:36:23 +00003346 if (exec->counts == NULL) {
3347 exec->status = -1;
3348 goto error;
3349 }
Daniel Veillard10752282005-08-08 13:05:13 +00003350#ifdef DEBUG_REGEXP_EXEC
3351 printf("resetting count %d on transition\n",
3352 trans->count);
3353#endif
3354 exec->counts[trans->count] = 0;
3355 }
Daniel Veillard4255d502002-04-16 15:50:10 +00003356#ifdef DEBUG_REGEXP_EXEC
3357 printf("entering state %d\n", trans->to);
3358#endif
3359 exec->state = comp->states[trans->to];
3360 exec->transno = 0;
3361 if (trans->atom != NULL) {
3362 exec->index += len;
3363 }
3364 goto progress;
3365 } else if (ret < 0) {
3366 exec->status = -4;
3367 break;
3368 }
3369 }
3370 if ((exec->transno != 0) || (exec->state->nbTrans == 0)) {
3371rollback:
3372 /*
3373 * Failed to find a way out
3374 */
3375 exec->determinist = 0;
Daniel Veillardaa622012005-10-20 15:55:25 +00003376#ifdef DEBUG_REGEXP_EXEC
3377 printf("rollback from state %d on %d:%c\n", exec->state->no,
3378 codepoint,codepoint);
3379#endif
Daniel Veillard4255d502002-04-16 15:50:10 +00003380 xmlFARegExecRollBack(exec);
3381 }
3382progress:
3383 continue;
3384 }
Daniel Veillard11ce4002006-03-10 00:36:23 +00003385error:
Daniel Veillard4255d502002-04-16 15:50:10 +00003386 if (exec->rollbacks != NULL) {
3387 if (exec->counts != NULL) {
3388 int i;
3389
3390 for (i = 0;i < exec->maxRollbacks;i++)
3391 if (exec->rollbacks[i].counts != NULL)
3392 xmlFree(exec->rollbacks[i].counts);
3393 }
3394 xmlFree(exec->rollbacks);
3395 }
3396 if (exec->counts != NULL)
3397 xmlFree(exec->counts);
3398 if (exec->status == 0)
3399 return(1);
Daniel Veillard94cc1032005-09-15 13:09:00 +00003400 if (exec->status == -1) {
3401 if (exec->nbPush > MAX_PUSH)
3402 return(-1);
Daniel Veillard4255d502002-04-16 15:50:10 +00003403 return(0);
Daniel Veillard94cc1032005-09-15 13:09:00 +00003404 }
Daniel Veillard4255d502002-04-16 15:50:10 +00003405 return(exec->status);
3406}
3407
3408/************************************************************************
3409 * *
William M. Brackddf71d62004-05-06 04:17:26 +00003410 * Progressive interface to the verifier one atom at a time *
Daniel Veillard4255d502002-04-16 15:50:10 +00003411 * *
3412 ************************************************************************/
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003413#ifdef DEBUG_ERR
3414static void testerr(xmlRegExecCtxtPtr exec);
3415#endif
Daniel Veillard4255d502002-04-16 15:50:10 +00003416
3417/**
Daniel Veillard01c13b52002-12-10 15:19:08 +00003418 * xmlRegNewExecCtxt:
Daniel Veillard4255d502002-04-16 15:50:10 +00003419 * @comp: a precompiled regular expression
3420 * @callback: a callback function used for handling progresses in the
3421 * automata matching phase
3422 * @data: the context data associated to the callback in this context
3423 *
3424 * Build a context used for progressive evaluation of a regexp.
Daniel Veillard01c13b52002-12-10 15:19:08 +00003425 *
3426 * Returns the new context
Daniel Veillard4255d502002-04-16 15:50:10 +00003427 */
3428xmlRegExecCtxtPtr
3429xmlRegNewExecCtxt(xmlRegexpPtr comp, xmlRegExecCallbacks callback, void *data) {
3430 xmlRegExecCtxtPtr exec;
3431
3432 if (comp == NULL)
3433 return(NULL);
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00003434 if ((comp->compact == NULL) && (comp->states == NULL))
3435 return(NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +00003436 exec = (xmlRegExecCtxtPtr) xmlMalloc(sizeof(xmlRegExecCtxt));
3437 if (exec == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00003438 xmlRegexpErrMemory(NULL, "creating execution context");
Daniel Veillard4255d502002-04-16 15:50:10 +00003439 return(NULL);
3440 }
3441 memset(exec, 0, sizeof(xmlRegExecCtxt));
3442 exec->inputString = NULL;
3443 exec->index = 0;
3444 exec->determinist = 1;
3445 exec->maxRollbacks = 0;
3446 exec->nbRollbacks = 0;
3447 exec->rollbacks = NULL;
3448 exec->status = 0;
3449 exec->comp = comp;
Daniel Veillard23e73572002-09-19 19:56:43 +00003450 if (comp->compact == NULL)
3451 exec->state = comp->states[0];
Daniel Veillard4255d502002-04-16 15:50:10 +00003452 exec->transno = 0;
3453 exec->transcount = 0;
3454 exec->callback = callback;
3455 exec->data = data;
3456 if (comp->nbCounters > 0) {
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003457 /*
3458 * For error handling, exec->counts is allocated twice the size
3459 * the second half is used to store the data in case of rollback
3460 */
3461 exec->counts = (int *) xmlMalloc(comp->nbCounters * sizeof(int)
3462 * 2);
Daniel Veillard4255d502002-04-16 15:50:10 +00003463 if (exec->counts == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00003464 xmlRegexpErrMemory(NULL, "creating execution context");
Daniel Veillard4255d502002-04-16 15:50:10 +00003465 xmlFree(exec);
3466 return(NULL);
3467 }
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003468 memset(exec->counts, 0, comp->nbCounters * sizeof(int) * 2);
3469 exec->errCounts = &exec->counts[comp->nbCounters];
3470 } else {
Daniel Veillard4255d502002-04-16 15:50:10 +00003471 exec->counts = NULL;
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003472 exec->errCounts = NULL;
3473 }
Daniel Veillard4255d502002-04-16 15:50:10 +00003474 exec->inputStackMax = 0;
3475 exec->inputStackNr = 0;
3476 exec->inputStack = NULL;
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003477 exec->errStateNo = -1;
3478 exec->errString = NULL;
Daniel Veillard94cc1032005-09-15 13:09:00 +00003479 exec->nbPush = 0;
Daniel Veillard4255d502002-04-16 15:50:10 +00003480 return(exec);
3481}
3482
3483/**
3484 * xmlRegFreeExecCtxt:
3485 * @exec: a regular expression evaulation context
3486 *
3487 * Free the structures associated to a regular expression evaulation context.
3488 */
3489void
3490xmlRegFreeExecCtxt(xmlRegExecCtxtPtr exec) {
3491 if (exec == NULL)
3492 return;
3493
3494 if (exec->rollbacks != NULL) {
3495 if (exec->counts != NULL) {
3496 int i;
3497
3498 for (i = 0;i < exec->maxRollbacks;i++)
3499 if (exec->rollbacks[i].counts != NULL)
3500 xmlFree(exec->rollbacks[i].counts);
3501 }
3502 xmlFree(exec->rollbacks);
3503 }
3504 if (exec->counts != NULL)
3505 xmlFree(exec->counts);
3506 if (exec->inputStack != NULL) {
3507 int i;
3508
Daniel Veillard32370232002-10-16 14:08:14 +00003509 for (i = 0;i < exec->inputStackNr;i++) {
3510 if (exec->inputStack[i].value != NULL)
3511 xmlFree(exec->inputStack[i].value);
3512 }
Daniel Veillard4255d502002-04-16 15:50:10 +00003513 xmlFree(exec->inputStack);
3514 }
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003515 if (exec->errString != NULL)
3516 xmlFree(exec->errString);
Daniel Veillard4255d502002-04-16 15:50:10 +00003517 xmlFree(exec);
3518}
3519
3520static void
3521xmlFARegExecSaveInputString(xmlRegExecCtxtPtr exec, const xmlChar *value,
3522 void *data) {
3523#ifdef DEBUG_PUSH
3524 printf("saving value: %d:%s\n", exec->inputStackNr, value);
3525#endif
3526 if (exec->inputStackMax == 0) {
3527 exec->inputStackMax = 4;
3528 exec->inputStack = (xmlRegInputTokenPtr)
3529 xmlMalloc(exec->inputStackMax * sizeof(xmlRegInputToken));
3530 if (exec->inputStack == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00003531 xmlRegexpErrMemory(NULL, "pushing input string");
Daniel Veillard4255d502002-04-16 15:50:10 +00003532 exec->inputStackMax = 0;
3533 return;
3534 }
3535 } else if (exec->inputStackNr + 1 >= exec->inputStackMax) {
3536 xmlRegInputTokenPtr tmp;
3537
3538 exec->inputStackMax *= 2;
3539 tmp = (xmlRegInputTokenPtr) xmlRealloc(exec->inputStack,
3540 exec->inputStackMax * sizeof(xmlRegInputToken));
3541 if (tmp == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00003542 xmlRegexpErrMemory(NULL, "pushing input string");
Daniel Veillard4255d502002-04-16 15:50:10 +00003543 exec->inputStackMax /= 2;
3544 return;
3545 }
3546 exec->inputStack = tmp;
3547 }
3548 exec->inputStack[exec->inputStackNr].value = xmlStrdup(value);
3549 exec->inputStack[exec->inputStackNr].data = data;
3550 exec->inputStackNr++;
3551 exec->inputStack[exec->inputStackNr].value = NULL;
3552 exec->inputStack[exec->inputStackNr].data = NULL;
3553}
3554
Daniel Veillardc0826a72004-08-10 14:17:33 +00003555/**
3556 * xmlRegStrEqualWildcard:
3557 * @expStr: the string to be evaluated
3558 * @valStr: the validation string
3559 *
3560 * Checks if both strings are equal or have the same content. "*"
3561 * can be used as a wildcard in @valStr; "|" is used as a seperator of
3562 * substrings in both @expStr and @valStr.
3563 *
3564 * Returns 1 if the comparison is satisfied and the number of substrings
3565 * is equal, 0 otherwise.
3566 */
3567
3568static int
3569xmlRegStrEqualWildcard(const xmlChar *expStr, const xmlChar *valStr) {
3570 if (expStr == valStr) return(1);
3571 if (expStr == NULL) return(0);
3572 if (valStr == NULL) return(0);
3573 do {
3574 /*
3575 * Eval if we have a wildcard for the current item.
3576 */
3577 if (*expStr != *valStr) {
Daniel Veillard4f82c8a2005-08-09 21:40:08 +00003578 /* if one of them starts with a wildcard make valStr be it */
3579 if (*valStr == '*') {
3580 const xmlChar *tmp;
3581
3582 tmp = valStr;
3583 valStr = expStr;
3584 expStr = tmp;
3585 }
Daniel Veillardc0826a72004-08-10 14:17:33 +00003586 if ((*valStr != 0) && (*expStr != 0) && (*expStr++ == '*')) {
3587 do {
3588 if (*valStr == XML_REG_STRING_SEPARATOR)
3589 break;
Kasimier T. Buchcikc0e833f2005-04-19 15:02:20 +00003590 valStr++;
Daniel Veillardc0826a72004-08-10 14:17:33 +00003591 } while (*valStr != 0);
3592 continue;
3593 } else
3594 return(0);
3595 }
Kasimier T. Buchcikc0e833f2005-04-19 15:02:20 +00003596 expStr++;
3597 valStr++;
Daniel Veillardc0826a72004-08-10 14:17:33 +00003598 } while (*valStr != 0);
3599 if (*expStr != 0)
3600 return (0);
3601 else
3602 return (1);
3603}
Daniel Veillard4255d502002-04-16 15:50:10 +00003604
3605/**
Daniel Veillard23e73572002-09-19 19:56:43 +00003606 * xmlRegCompactPushString:
3607 * @exec: a regexp execution context
3608 * @comp: the precompiled exec with a compact table
3609 * @value: a string token input
3610 * @data: data associated to the token to reuse in callbacks
3611 *
3612 * Push one input token in the execution context
3613 *
3614 * Returns: 1 if the regexp reached a final state, 0 if non-final, and
3615 * a negative value in case of error.
3616 */
3617static int
3618xmlRegCompactPushString(xmlRegExecCtxtPtr exec,
3619 xmlRegexpPtr comp,
3620 const xmlChar *value,
3621 void *data) {
3622 int state = exec->index;
3623 int i, target;
3624
3625 if ((comp == NULL) || (comp->compact == NULL) || (comp->stringMap == NULL))
3626 return(-1);
3627
3628 if (value == NULL) {
3629 /*
3630 * are we at a final state ?
3631 */
3632 if (comp->compact[state * (comp->nbstrings + 1)] ==
3633 XML_REGEXP_FINAL_STATE)
3634 return(1);
3635 return(0);
3636 }
3637
3638#ifdef DEBUG_PUSH
3639 printf("value pushed: %s\n", value);
3640#endif
3641
3642 /*
William M. Brackddf71d62004-05-06 04:17:26 +00003643 * Examine all outside transitions from current state
Daniel Veillard23e73572002-09-19 19:56:43 +00003644 */
3645 for (i = 0;i < comp->nbstrings;i++) {
3646 target = comp->compact[state * (comp->nbstrings + 1) + i + 1];
3647 if ((target > 0) && (target <= comp->nbstates)) {
Daniel Veillardc0826a72004-08-10 14:17:33 +00003648 target--; /* to avoid 0 */
3649 if (xmlRegStrEqualWildcard(comp->stringMap[i], value)) {
3650 exec->index = target;
Daniel Veillard118aed72002-09-24 14:13:13 +00003651 if ((exec->callback != NULL) && (comp->transdata != NULL)) {
3652 exec->callback(exec->data, value,
3653 comp->transdata[state * comp->nbstrings + i], data);
3654 }
Daniel Veillard23e73572002-09-19 19:56:43 +00003655#ifdef DEBUG_PUSH
3656 printf("entering state %d\n", target);
3657#endif
3658 if (comp->compact[target * (comp->nbstrings + 1)] ==
Daniel Veillardcc026dc2005-01-12 13:21:17 +00003659 XML_REGEXP_SINK_STATE)
3660 goto error;
3661
3662 if (comp->compact[target * (comp->nbstrings + 1)] ==
Daniel Veillard23e73572002-09-19 19:56:43 +00003663 XML_REGEXP_FINAL_STATE)
3664 return(1);
3665 return(0);
3666 }
3667 }
3668 }
3669 /*
3670 * Failed to find an exit transition out from current state for the
3671 * current token
3672 */
3673#ifdef DEBUG_PUSH
3674 printf("failed to find a transition for %s on state %d\n", value, state);
3675#endif
Daniel Veillardcc026dc2005-01-12 13:21:17 +00003676error:
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003677 if (exec->errString != NULL)
3678 xmlFree(exec->errString);
3679 exec->errString = xmlStrdup(value);
3680 exec->errStateNo = state;
Daniel Veillard23e73572002-09-19 19:56:43 +00003681 exec->status = -1;
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003682#ifdef DEBUG_ERR
3683 testerr(exec);
3684#endif
Daniel Veillard23e73572002-09-19 19:56:43 +00003685 return(-1);
3686}
3687
3688/**
Daniel Veillard6e65e152005-08-09 11:09:52 +00003689 * xmlRegExecPushStringInternal:
Daniel Veillardea7751d2002-12-20 00:16:24 +00003690 * @exec: a regexp execution context or NULL to indicate the end
Daniel Veillard4255d502002-04-16 15:50:10 +00003691 * @value: a string token input
3692 * @data: data associated to the token to reuse in callbacks
Daniel Veillard6e65e152005-08-09 11:09:52 +00003693 * @compound: value was assembled from 2 strings
Daniel Veillard4255d502002-04-16 15:50:10 +00003694 *
3695 * Push one input token in the execution context
3696 *
3697 * Returns: 1 if the regexp reached a final state, 0 if non-final, and
3698 * a negative value in case of error.
3699 */
Daniel Veillard6e65e152005-08-09 11:09:52 +00003700static int
3701xmlRegExecPushStringInternal(xmlRegExecCtxtPtr exec, const xmlChar *value,
3702 void *data, int compound) {
Daniel Veillard4255d502002-04-16 15:50:10 +00003703 xmlRegTransPtr trans;
3704 xmlRegAtomPtr atom;
3705 int ret;
3706 int final = 0;
Daniel Veillard90700152005-01-08 22:05:09 +00003707 int progress = 1;
Daniel Veillard4255d502002-04-16 15:50:10 +00003708
3709 if (exec == NULL)
3710 return(-1);
Daniel Veillard23e73572002-09-19 19:56:43 +00003711 if (exec->comp == NULL)
3712 return(-1);
Daniel Veillard4255d502002-04-16 15:50:10 +00003713 if (exec->status != 0)
3714 return(exec->status);
3715
Daniel Veillard23e73572002-09-19 19:56:43 +00003716 if (exec->comp->compact != NULL)
3717 return(xmlRegCompactPushString(exec, exec->comp, value, data));
3718
Daniel Veillard4255d502002-04-16 15:50:10 +00003719 if (value == NULL) {
3720 if (exec->state->type == XML_REGEXP_FINAL_STATE)
3721 return(1);
3722 final = 1;
3723 }
3724
3725#ifdef DEBUG_PUSH
3726 printf("value pushed: %s\n", value);
3727#endif
3728 /*
3729 * If we have an active rollback stack push the new value there
3730 * and get back to where we were left
3731 */
3732 if ((value != NULL) && (exec->inputStackNr > 0)) {
3733 xmlFARegExecSaveInputString(exec, value, data);
3734 value = exec->inputStack[exec->index].value;
3735 data = exec->inputStack[exec->index].data;
3736#ifdef DEBUG_PUSH
3737 printf("value loaded: %s\n", value);
3738#endif
3739 }
3740
3741 while ((exec->status == 0) &&
3742 ((value != NULL) ||
3743 ((final == 1) &&
3744 (exec->state->type != XML_REGEXP_FINAL_STATE)))) {
3745
3746 /*
3747 * End of input on non-terminal state, rollback, however we may
3748 * still have epsilon like transition for counted transitions
3749 * on counters, in that case don't break too early.
3750 */
Daniel Veillardb509f152002-04-17 16:28:10 +00003751 if ((value == NULL) && (exec->counts == NULL))
Daniel Veillard4255d502002-04-16 15:50:10 +00003752 goto rollback;
3753
3754 exec->transcount = 0;
3755 for (;exec->transno < exec->state->nbTrans;exec->transno++) {
3756 trans = &exec->state->trans[exec->transno];
3757 if (trans->to < 0)
3758 continue;
3759 atom = trans->atom;
3760 ret = 0;
Daniel Veillard441bc322002-04-20 17:38:48 +00003761 if (trans->count == REGEXP_ALL_LAX_COUNTER) {
3762 int i;
3763 int count;
3764 xmlRegTransPtr t;
3765 xmlRegCounterPtr counter;
3766
3767 ret = 0;
3768
3769#ifdef DEBUG_PUSH
3770 printf("testing all lax %d\n", trans->count);
3771#endif
3772 /*
3773 * Check all counted transitions from the current state
3774 */
3775 if ((value == NULL) && (final)) {
3776 ret = 1;
3777 } else if (value != NULL) {
3778 for (i = 0;i < exec->state->nbTrans;i++) {
3779 t = &exec->state->trans[i];
3780 if ((t->counter < 0) || (t == trans))
3781 continue;
3782 counter = &exec->comp->counters[t->counter];
3783 count = exec->counts[t->counter];
3784 if ((count < counter->max) &&
3785 (t->atom != NULL) &&
3786 (xmlStrEqual(value, t->atom->valuep))) {
3787 ret = 0;
3788 break;
3789 }
3790 if ((count >= counter->min) &&
3791 (count < counter->max) &&
Daniel Veillard11ce4002006-03-10 00:36:23 +00003792 (t->atom != NULL) &&
Daniel Veillard441bc322002-04-20 17:38:48 +00003793 (xmlStrEqual(value, t->atom->valuep))) {
3794 ret = 1;
3795 break;
3796 }
3797 }
3798 }
3799 } else if (trans->count == REGEXP_ALL_COUNTER) {
Daniel Veillard8a001f62002-04-20 07:24:11 +00003800 int i;
3801 int count;
3802 xmlRegTransPtr t;
3803 xmlRegCounterPtr counter;
3804
3805 ret = 1;
3806
3807#ifdef DEBUG_PUSH
3808 printf("testing all %d\n", trans->count);
3809#endif
3810 /*
3811 * Check all counted transitions from the current state
3812 */
3813 for (i = 0;i < exec->state->nbTrans;i++) {
3814 t = &exec->state->trans[i];
3815 if ((t->counter < 0) || (t == trans))
3816 continue;
3817 counter = &exec->comp->counters[t->counter];
3818 count = exec->counts[t->counter];
3819 if ((count < counter->min) || (count > counter->max)) {
3820 ret = 0;
3821 break;
3822 }
3823 }
3824 } else if (trans->count >= 0) {
Daniel Veillard4255d502002-04-16 15:50:10 +00003825 int count;
3826 xmlRegCounterPtr counter;
3827
3828 /*
3829 * A counted transition.
3830 */
3831
3832 count = exec->counts[trans->count];
3833 counter = &exec->comp->counters[trans->count];
3834#ifdef DEBUG_PUSH
3835 printf("testing count %d: val %d, min %d, max %d\n",
3836 trans->count, count, counter->min, counter->max);
3837#endif
3838 ret = ((count >= counter->min) && (count <= counter->max));
3839 } else if (atom == NULL) {
3840 fprintf(stderr, "epsilon transition left at runtime\n");
3841 exec->status = -2;
3842 break;
3843 } else if (value != NULL) {
Daniel Veillardc0826a72004-08-10 14:17:33 +00003844 ret = xmlRegStrEqualWildcard(atom->valuep, value);
Daniel Veillard6e65e152005-08-09 11:09:52 +00003845 if (atom->neg) {
Daniel Veillard9efc4762005-07-19 14:33:55 +00003846 ret = !ret;
Daniel Veillard6e65e152005-08-09 11:09:52 +00003847 if (!compound)
3848 ret = 0;
3849 }
Daniel Veillard441bc322002-04-20 17:38:48 +00003850 if ((ret == 1) && (trans->counter >= 0)) {
3851 xmlRegCounterPtr counter;
3852 int count;
3853
3854 count = exec->counts[trans->counter];
3855 counter = &exec->comp->counters[trans->counter];
3856 if (count >= counter->max)
3857 ret = 0;
3858 }
3859
Daniel Veillard4255d502002-04-16 15:50:10 +00003860 if ((ret == 1) && (atom->min > 0) && (atom->max > 0)) {
3861 xmlRegStatePtr to = exec->comp->states[trans->to];
3862
3863 /*
3864 * this is a multiple input sequence
3865 */
3866 if (exec->state->nbTrans > exec->transno + 1) {
3867 if (exec->inputStackNr <= 0) {
3868 xmlFARegExecSaveInputString(exec, value, data);
3869 }
3870 xmlFARegExecSave(exec);
3871 }
3872 exec->transcount = 1;
3873 do {
3874 /*
3875 * Try to progress as much as possible on the input
3876 */
3877 if (exec->transcount == atom->max) {
3878 break;
3879 }
3880 exec->index++;
3881 value = exec->inputStack[exec->index].value;
3882 data = exec->inputStack[exec->index].data;
3883#ifdef DEBUG_PUSH
3884 printf("value loaded: %s\n", value);
3885#endif
3886
3887 /*
3888 * End of input: stop here
3889 */
3890 if (value == NULL) {
3891 exec->index --;
3892 break;
3893 }
3894 if (exec->transcount >= atom->min) {
3895 int transno = exec->transno;
3896 xmlRegStatePtr state = exec->state;
3897
3898 /*
3899 * The transition is acceptable save it
3900 */
3901 exec->transno = -1; /* trick */
3902 exec->state = to;
3903 if (exec->inputStackNr <= 0) {
3904 xmlFARegExecSaveInputString(exec, value, data);
3905 }
3906 xmlFARegExecSave(exec);
3907 exec->transno = transno;
3908 exec->state = state;
3909 }
3910 ret = xmlStrEqual(value, atom->valuep);
3911 exec->transcount++;
3912 } while (ret == 1);
3913 if (exec->transcount < atom->min)
3914 ret = 0;
3915
3916 /*
3917 * If the last check failed but one transition was found
3918 * possible, rollback
3919 */
3920 if (ret < 0)
3921 ret = 0;
3922 if (ret == 0) {
3923 goto rollback;
3924 }
3925 }
3926 }
3927 if (ret == 1) {
William M. Brack98873952003-12-26 06:03:14 +00003928 if ((exec->callback != NULL) && (atom != NULL) &&
3929 (data != NULL)) {
Daniel Veillard4255d502002-04-16 15:50:10 +00003930 exec->callback(exec->data, atom->valuep,
3931 atom->data, data);
3932 }
3933 if (exec->state->nbTrans > exec->transno + 1) {
3934 if (exec->inputStackNr <= 0) {
3935 xmlFARegExecSaveInputString(exec, value, data);
3936 }
3937 xmlFARegExecSave(exec);
3938 }
3939 if (trans->counter >= 0) {
3940#ifdef DEBUG_PUSH
3941 printf("Increasing count %d\n", trans->counter);
3942#endif
3943 exec->counts[trans->counter]++;
3944 }
Daniel Veillard10752282005-08-08 13:05:13 +00003945 if ((trans->count >= 0) &&
3946 (trans->count < REGEXP_ALL_COUNTER)) {
3947#ifdef DEBUG_REGEXP_EXEC
3948 printf("resetting count %d on transition\n",
3949 trans->count);
3950#endif
3951 exec->counts[trans->count] = 0;
3952 }
Daniel Veillard4255d502002-04-16 15:50:10 +00003953#ifdef DEBUG_PUSH
3954 printf("entering state %d\n", trans->to);
3955#endif
Daniel Veillardcc026dc2005-01-12 13:21:17 +00003956 if ((exec->comp->states[trans->to] != NULL) &&
3957 (exec->comp->states[trans->to]->type ==
3958 XML_REGEXP_SINK_STATE)) {
3959 /*
3960 * entering a sink state, save the current state as error
3961 * state.
3962 */
3963 if (exec->errString != NULL)
3964 xmlFree(exec->errString);
3965 exec->errString = xmlStrdup(value);
3966 exec->errState = exec->state;
3967 memcpy(exec->errCounts, exec->counts,
3968 exec->comp->nbCounters * sizeof(int));
3969 }
Daniel Veillard4255d502002-04-16 15:50:10 +00003970 exec->state = exec->comp->states[trans->to];
3971 exec->transno = 0;
3972 if (trans->atom != NULL) {
3973 if (exec->inputStack != NULL) {
3974 exec->index++;
3975 if (exec->index < exec->inputStackNr) {
3976 value = exec->inputStack[exec->index].value;
3977 data = exec->inputStack[exec->index].data;
3978#ifdef DEBUG_PUSH
3979 printf("value loaded: %s\n", value);
3980#endif
3981 } else {
3982 value = NULL;
3983 data = NULL;
3984#ifdef DEBUG_PUSH
3985 printf("end of input\n");
3986#endif
3987 }
3988 } else {
3989 value = NULL;
3990 data = NULL;
3991#ifdef DEBUG_PUSH
3992 printf("end of input\n");
3993#endif
3994 }
3995 }
3996 goto progress;
3997 } else if (ret < 0) {
3998 exec->status = -4;
3999 break;
4000 }
4001 }
4002 if ((exec->transno != 0) || (exec->state->nbTrans == 0)) {
4003rollback:
Daniel Veillard90700152005-01-08 22:05:09 +00004004 /*
Daniel Veillardcc026dc2005-01-12 13:21:17 +00004005 * if we didn't yet rollback on the current input
4006 * store the current state as the error state.
Daniel Veillard90700152005-01-08 22:05:09 +00004007 */
Daniel Veillardcc026dc2005-01-12 13:21:17 +00004008 if ((progress) && (exec->state != NULL) &&
4009 (exec->state->type != XML_REGEXP_SINK_STATE)) {
Daniel Veillard90700152005-01-08 22:05:09 +00004010 progress = 0;
4011 if (exec->errString != NULL)
4012 xmlFree(exec->errString);
4013 exec->errString = xmlStrdup(value);
4014 exec->errState = exec->state;
4015 memcpy(exec->errCounts, exec->counts,
4016 exec->comp->nbCounters * sizeof(int));
4017 }
4018
Daniel Veillard4255d502002-04-16 15:50:10 +00004019 /*
4020 * Failed to find a way out
4021 */
4022 exec->determinist = 0;
4023 xmlFARegExecRollBack(exec);
4024 if (exec->status == 0) {
4025 value = exec->inputStack[exec->index].value;
4026 data = exec->inputStack[exec->index].data;
4027#ifdef DEBUG_PUSH
4028 printf("value loaded: %s\n", value);
4029#endif
4030 }
4031 }
Daniel Veillard90700152005-01-08 22:05:09 +00004032 continue;
Daniel Veillard4255d502002-04-16 15:50:10 +00004033progress:
Daniel Veillard90700152005-01-08 22:05:09 +00004034 progress = 1;
Daniel Veillard4255d502002-04-16 15:50:10 +00004035 continue;
4036 }
4037 if (exec->status == 0) {
4038 return(exec->state->type == XML_REGEXP_FINAL_STATE);
4039 }
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00004040#ifdef DEBUG_ERR
Daniel Veillard90700152005-01-08 22:05:09 +00004041 if (exec->status < 0) {
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00004042 testerr(exec);
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00004043 }
Daniel Veillard90700152005-01-08 22:05:09 +00004044#endif
Daniel Veillard4255d502002-04-16 15:50:10 +00004045 return(exec->status);
4046}
4047
Daniel Veillard52b48c72003-04-13 19:53:42 +00004048/**
Daniel Veillard6e65e152005-08-09 11:09:52 +00004049 * xmlRegExecPushString:
4050 * @exec: a regexp execution context or NULL to indicate the end
4051 * @value: a string token input
4052 * @data: data associated to the token to reuse in callbacks
4053 *
4054 * Push one input token in the execution context
4055 *
4056 * Returns: 1 if the regexp reached a final state, 0 if non-final, and
4057 * a negative value in case of error.
4058 */
4059int
4060xmlRegExecPushString(xmlRegExecCtxtPtr exec, const xmlChar *value,
4061 void *data) {
4062 return(xmlRegExecPushStringInternal(exec, value, data, 0));
4063}
4064
4065/**
Daniel Veillard52b48c72003-04-13 19:53:42 +00004066 * xmlRegExecPushString2:
4067 * @exec: a regexp execution context or NULL to indicate the end
4068 * @value: the first string token input
4069 * @value2: the second string token input
4070 * @data: data associated to the token to reuse in callbacks
4071 *
4072 * Push one input token in the execution context
4073 *
4074 * Returns: 1 if the regexp reached a final state, 0 if non-final, and
4075 * a negative value in case of error.
4076 */
4077int
4078xmlRegExecPushString2(xmlRegExecCtxtPtr exec, const xmlChar *value,
4079 const xmlChar *value2, void *data) {
4080 xmlChar buf[150];
4081 int lenn, lenp, ret;
4082 xmlChar *str;
4083
4084 if (exec == NULL)
4085 return(-1);
4086 if (exec->comp == NULL)
4087 return(-1);
4088 if (exec->status != 0)
4089 return(exec->status);
4090
4091 if (value2 == NULL)
4092 return(xmlRegExecPushString(exec, value, data));
4093
4094 lenn = strlen((char *) value2);
4095 lenp = strlen((char *) value);
4096
4097 if (150 < lenn + lenp + 2) {
Daniel Veillard3c908dc2003-04-19 00:07:51 +00004098 str = (xmlChar *) xmlMallocAtomic(lenn + lenp + 2);
Daniel Veillard52b48c72003-04-13 19:53:42 +00004099 if (str == NULL) {
4100 exec->status = -1;
4101 return(-1);
4102 }
4103 } else {
4104 str = buf;
4105 }
4106 memcpy(&str[0], value, lenp);
Daniel Veillardc0826a72004-08-10 14:17:33 +00004107 str[lenp] = XML_REG_STRING_SEPARATOR;
Daniel Veillard52b48c72003-04-13 19:53:42 +00004108 memcpy(&str[lenp + 1], value2, lenn);
4109 str[lenn + lenp + 1] = 0;
4110
4111 if (exec->comp->compact != NULL)
4112 ret = xmlRegCompactPushString(exec, exec->comp, str, data);
4113 else
Daniel Veillard6e65e152005-08-09 11:09:52 +00004114 ret = xmlRegExecPushStringInternal(exec, str, data, 1);
Daniel Veillard52b48c72003-04-13 19:53:42 +00004115
4116 if (str != buf)
Daniel Veillard0b1ff142005-12-28 21:13:33 +00004117 xmlFree(str);
Daniel Veillard52b48c72003-04-13 19:53:42 +00004118 return(ret);
4119}
4120
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00004121/**
Daniel Veillard77005e62005-07-19 16:26:18 +00004122 * xmlRegExecGetValues:
Daniel Veillardfc0b6f62005-01-09 17:48:02 +00004123 * @exec: a regexp execution context
4124 * @err: error extraction or normal one
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00004125 * @nbval: pointer to the number of accepted values IN/OUT
Daniel Veillardcc026dc2005-01-12 13:21:17 +00004126 * @nbneg: return number of negative transitions
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00004127 * @values: pointer to the array of acceptable values
Daniel Veillardfc0b6f62005-01-09 17:48:02 +00004128 * @terminal: return value if this was a terminal state
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00004129 *
Daniel Veillardfc0b6f62005-01-09 17:48:02 +00004130 * Extract informations from the regexp execution, internal routine to
4131 * implement xmlRegExecNextValues() and xmlRegExecErrInfo()
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00004132 *
4133 * Returns: 0 in case of success or -1 in case of error.
4134 */
Daniel Veillardfc0b6f62005-01-09 17:48:02 +00004135static int
4136xmlRegExecGetValues(xmlRegExecCtxtPtr exec, int err,
Daniel Veillardcc026dc2005-01-12 13:21:17 +00004137 int *nbval, int *nbneg,
4138 xmlChar **values, int *terminal) {
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00004139 int maxval;
Daniel Veillardcc026dc2005-01-12 13:21:17 +00004140 int nb = 0;
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00004141
Daniel Veillardcc026dc2005-01-12 13:21:17 +00004142 if ((exec == NULL) || (nbval == NULL) || (nbneg == NULL) ||
4143 (values == NULL) || (*nbval <= 0))
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00004144 return(-1);
Daniel Veillardfc0b6f62005-01-09 17:48:02 +00004145
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00004146 maxval = *nbval;
4147 *nbval = 0;
Daniel Veillardcc026dc2005-01-12 13:21:17 +00004148 *nbneg = 0;
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00004149 if ((exec->comp != NULL) && (exec->comp->compact != NULL)) {
4150 xmlRegexpPtr comp;
4151 int target, i, state;
4152
4153 comp = exec->comp;
Daniel Veillardfc0b6f62005-01-09 17:48:02 +00004154
4155 if (err) {
4156 if (exec->errStateNo == -1) return(-1);
4157 state = exec->errStateNo;
4158 } else {
4159 state = exec->index;
4160 }
4161 if (terminal != NULL) {
4162 if (comp->compact[state * (comp->nbstrings + 1)] ==
4163 XML_REGEXP_FINAL_STATE)
4164 *terminal = 1;
4165 else
4166 *terminal = 0;
4167 }
Daniel Veillardcc026dc2005-01-12 13:21:17 +00004168 for (i = 0;(i < comp->nbstrings) && (nb < maxval);i++) {
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00004169 target = comp->compact[state * (comp->nbstrings + 1) + i + 1];
Daniel Veillardcc026dc2005-01-12 13:21:17 +00004170 if ((target > 0) && (target <= comp->nbstates) &&
4171 (comp->compact[(target - 1) * (comp->nbstrings + 1)] !=
4172 XML_REGEXP_SINK_STATE)) {
4173 values[nb++] = comp->stringMap[i];
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00004174 (*nbval)++;
4175 }
4176 }
Daniel Veillardcc026dc2005-01-12 13:21:17 +00004177 for (i = 0;(i < comp->nbstrings) && (nb < maxval);i++) {
4178 target = comp->compact[state * (comp->nbstrings + 1) + i + 1];
4179 if ((target > 0) && (target <= comp->nbstates) &&
4180 (comp->compact[(target - 1) * (comp->nbstrings + 1)] ==
4181 XML_REGEXP_SINK_STATE)) {
4182 values[nb++] = comp->stringMap[i];
4183 (*nbneg)++;
4184 }
4185 }
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00004186 } else {
4187 int transno;
4188 xmlRegTransPtr trans;
4189 xmlRegAtomPtr atom;
Daniel Veillardfc0b6f62005-01-09 17:48:02 +00004190 xmlRegStatePtr state;
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00004191
Daniel Veillardfc0b6f62005-01-09 17:48:02 +00004192 if (terminal != NULL) {
4193 if (exec->state->type == XML_REGEXP_FINAL_STATE)
4194 *terminal = 1;
4195 else
4196 *terminal = 0;
4197 }
4198
4199 if (err) {
4200 if (exec->errState == NULL) return(-1);
4201 state = exec->errState;
4202 } else {
4203 if (exec->state == NULL) return(-1);
4204 state = exec->state;
4205 }
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00004206 for (transno = 0;
Daniel Veillardcc026dc2005-01-12 13:21:17 +00004207 (transno < state->nbTrans) && (nb < maxval);
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00004208 transno++) {
Daniel Veillardfc0b6f62005-01-09 17:48:02 +00004209 trans = &state->trans[transno];
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00004210 if (trans->to < 0)
4211 continue;
4212 atom = trans->atom;
4213 if ((atom == NULL) || (atom->valuep == NULL))
4214 continue;
4215 if (trans->count == REGEXP_ALL_LAX_COUNTER) {
Daniel Veillardcc026dc2005-01-12 13:21:17 +00004216 /* this should not be reached but ... */
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00004217 TODO;
4218 } else if (trans->count == REGEXP_ALL_COUNTER) {
Daniel Veillardcc026dc2005-01-12 13:21:17 +00004219 /* this should not be reached but ... */
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00004220 TODO;
4221 } else if (trans->counter >= 0) {
Daniel Veillard11ce4002006-03-10 00:36:23 +00004222 xmlRegCounterPtr counter = NULL;
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00004223 int count;
4224
Daniel Veillardfc0b6f62005-01-09 17:48:02 +00004225 if (err)
4226 count = exec->errCounts[trans->counter];
4227 else
4228 count = exec->counts[trans->counter];
Daniel Veillard11ce4002006-03-10 00:36:23 +00004229 if (exec->comp != NULL)
4230 counter = &exec->comp->counters[trans->counter];
4231 if ((counter == NULL) || (count < counter->max)) {
Daniel Veillard77005e62005-07-19 16:26:18 +00004232 if (atom->neg)
4233 values[nb++] = (xmlChar *) atom->valuep2;
4234 else
4235 values[nb++] = (xmlChar *) atom->valuep;
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00004236 (*nbval)++;
4237 }
4238 } else {
Daniel Veillardcc026dc2005-01-12 13:21:17 +00004239 if ((exec->comp->states[trans->to] != NULL) &&
4240 (exec->comp->states[trans->to]->type !=
4241 XML_REGEXP_SINK_STATE)) {
Daniel Veillard77005e62005-07-19 16:26:18 +00004242 if (atom->neg)
4243 values[nb++] = (xmlChar *) atom->valuep2;
4244 else
4245 values[nb++] = (xmlChar *) atom->valuep;
Daniel Veillardcc026dc2005-01-12 13:21:17 +00004246 (*nbval)++;
4247 }
4248 }
4249 }
4250 for (transno = 0;
4251 (transno < state->nbTrans) && (nb < maxval);
4252 transno++) {
4253 trans = &state->trans[transno];
4254 if (trans->to < 0)
4255 continue;
4256 atom = trans->atom;
4257 if ((atom == NULL) || (atom->valuep == NULL))
4258 continue;
4259 if (trans->count == REGEXP_ALL_LAX_COUNTER) {
4260 continue;
4261 } else if (trans->count == REGEXP_ALL_COUNTER) {
4262 continue;
4263 } else if (trans->counter >= 0) {
4264 continue;
4265 } else {
4266 if ((exec->comp->states[trans->to] != NULL) &&
4267 (exec->comp->states[trans->to]->type ==
4268 XML_REGEXP_SINK_STATE)) {
Daniel Veillard77005e62005-07-19 16:26:18 +00004269 if (atom->neg)
4270 values[nb++] = (xmlChar *) atom->valuep2;
4271 else
4272 values[nb++] = (xmlChar *) atom->valuep;
Daniel Veillardcc026dc2005-01-12 13:21:17 +00004273 (*nbneg)++;
4274 }
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00004275 }
4276 }
4277 }
4278 return(0);
4279}
4280
Daniel Veillardfc0b6f62005-01-09 17:48:02 +00004281/**
4282 * xmlRegExecNextValues:
4283 * @exec: a regexp execution context
4284 * @nbval: pointer to the number of accepted values IN/OUT
Daniel Veillardcc026dc2005-01-12 13:21:17 +00004285 * @nbneg: return number of negative transitions
Daniel Veillardfc0b6f62005-01-09 17:48:02 +00004286 * @values: pointer to the array of acceptable values
4287 * @terminal: return value if this was a terminal state
4288 *
4289 * Extract informations from the regexp execution,
4290 * the parameter @values must point to an array of @nbval string pointers
4291 * on return nbval will contain the number of possible strings in that
4292 * state and the @values array will be updated with them. The string values
4293 * returned will be freed with the @exec context and don't need to be
4294 * deallocated.
4295 *
4296 * Returns: 0 in case of success or -1 in case of error.
4297 */
4298int
Daniel Veillardcc026dc2005-01-12 13:21:17 +00004299xmlRegExecNextValues(xmlRegExecCtxtPtr exec, int *nbval, int *nbneg,
4300 xmlChar **values, int *terminal) {
4301 return(xmlRegExecGetValues(exec, 0, nbval, nbneg, values, terminal));
Daniel Veillardfc0b6f62005-01-09 17:48:02 +00004302}
4303
4304/**
4305 * xmlRegExecErrInfo:
4306 * @exec: a regexp execution context generating an error
4307 * @string: return value for the error string
4308 * @nbval: pointer to the number of accepted values IN/OUT
Daniel Veillardcc026dc2005-01-12 13:21:17 +00004309 * @nbneg: return number of negative transitions
Daniel Veillardfc0b6f62005-01-09 17:48:02 +00004310 * @values: pointer to the array of acceptable values
4311 * @terminal: return value if this was a terminal state
4312 *
4313 * Extract error informations from the regexp execution, the parameter
4314 * @string will be updated with the value pushed and not accepted,
4315 * the parameter @values must point to an array of @nbval string pointers
4316 * on return nbval will contain the number of possible strings in that
4317 * state and the @values array will be updated with them. The string values
4318 * returned will be freed with the @exec context and don't need to be
4319 * deallocated.
4320 *
4321 * Returns: 0 in case of success or -1 in case of error.
4322 */
4323int
4324xmlRegExecErrInfo(xmlRegExecCtxtPtr exec, const xmlChar **string,
Daniel Veillardcc026dc2005-01-12 13:21:17 +00004325 int *nbval, int *nbneg, xmlChar **values, int *terminal) {
Daniel Veillardfc0b6f62005-01-09 17:48:02 +00004326 if (exec == NULL)
4327 return(-1);
4328 if (string != NULL) {
4329 if (exec->status != 0)
4330 *string = exec->errString;
4331 else
4332 *string = NULL;
4333 }
Daniel Veillardcc026dc2005-01-12 13:21:17 +00004334 return(xmlRegExecGetValues(exec, 1, nbval, nbneg, values, terminal));
Daniel Veillardfc0b6f62005-01-09 17:48:02 +00004335}
4336
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00004337#ifdef DEBUG_ERR
4338static void testerr(xmlRegExecCtxtPtr exec) {
4339 const xmlChar *string;
Daniel Veillardcee2b3a2005-01-25 00:22:52 +00004340 xmlChar *values[5];
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00004341 int nb = 5;
Daniel Veillardcc026dc2005-01-12 13:21:17 +00004342 int nbneg;
Daniel Veillardfc0b6f62005-01-09 17:48:02 +00004343 int terminal;
Daniel Veillardcc026dc2005-01-12 13:21:17 +00004344 xmlRegExecErrInfo(exec, &string, &nb, &nbneg, &values[0], &terminal);
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00004345}
4346#endif
4347
Daniel Veillard4255d502002-04-16 15:50:10 +00004348#if 0
4349static int
4350xmlRegExecPushChar(xmlRegExecCtxtPtr exec, int UCS) {
4351 xmlRegTransPtr trans;
4352 xmlRegAtomPtr atom;
4353 int ret;
4354 int codepoint, len;
4355
4356 if (exec == NULL)
4357 return(-1);
4358 if (exec->status != 0)
4359 return(exec->status);
4360
4361 while ((exec->status == 0) &&
4362 ((exec->inputString[exec->index] != 0) ||
4363 (exec->state->type != XML_REGEXP_FINAL_STATE))) {
4364
4365 /*
4366 * End of input on non-terminal state, rollback, however we may
4367 * still have epsilon like transition for counted transitions
4368 * on counters, in that case don't break too early.
4369 */
4370 if ((exec->inputString[exec->index] == 0) && (exec->counts == NULL))
4371 goto rollback;
4372
4373 exec->transcount = 0;
4374 for (;exec->transno < exec->state->nbTrans;exec->transno++) {
4375 trans = &exec->state->trans[exec->transno];
4376 if (trans->to < 0)
4377 continue;
4378 atom = trans->atom;
4379 ret = 0;
4380 if (trans->count >= 0) {
4381 int count;
4382 xmlRegCounterPtr counter;
4383
4384 /*
4385 * A counted transition.
4386 */
4387
4388 count = exec->counts[trans->count];
4389 counter = &exec->comp->counters[trans->count];
4390#ifdef DEBUG_REGEXP_EXEC
4391 printf("testing count %d: val %d, min %d, max %d\n",
4392 trans->count, count, counter->min, counter->max);
4393#endif
4394 ret = ((count >= counter->min) && (count <= counter->max));
4395 } else if (atom == NULL) {
4396 fprintf(stderr, "epsilon transition left at runtime\n");
4397 exec->status = -2;
4398 break;
4399 } else if (exec->inputString[exec->index] != 0) {
4400 codepoint = CUR_SCHAR(&(exec->inputString[exec->index]), len);
4401 ret = xmlRegCheckCharacter(atom, codepoint);
4402 if ((ret == 1) && (atom->min > 0) && (atom->max > 0)) {
4403 xmlRegStatePtr to = exec->comp->states[trans->to];
4404
4405 /*
4406 * this is a multiple input sequence
4407 */
4408 if (exec->state->nbTrans > exec->transno + 1) {
4409 xmlFARegExecSave(exec);
4410 }
4411 exec->transcount = 1;
4412 do {
4413 /*
4414 * Try to progress as much as possible on the input
4415 */
4416 if (exec->transcount == atom->max) {
4417 break;
4418 }
4419 exec->index += len;
4420 /*
4421 * End of input: stop here
4422 */
4423 if (exec->inputString[exec->index] == 0) {
4424 exec->index -= len;
4425 break;
4426 }
4427 if (exec->transcount >= atom->min) {
4428 int transno = exec->transno;
4429 xmlRegStatePtr state = exec->state;
4430
4431 /*
4432 * The transition is acceptable save it
4433 */
4434 exec->transno = -1; /* trick */
4435 exec->state = to;
4436 xmlFARegExecSave(exec);
4437 exec->transno = transno;
4438 exec->state = state;
4439 }
4440 codepoint = CUR_SCHAR(&(exec->inputString[exec->index]),
4441 len);
4442 ret = xmlRegCheckCharacter(atom, codepoint);
4443 exec->transcount++;
4444 } while (ret == 1);
4445 if (exec->transcount < atom->min)
4446 ret = 0;
4447
4448 /*
4449 * If the last check failed but one transition was found
4450 * possible, rollback
4451 */
4452 if (ret < 0)
4453 ret = 0;
4454 if (ret == 0) {
4455 goto rollback;
4456 }
4457 }
4458 }
4459 if (ret == 1) {
4460 if (exec->state->nbTrans > exec->transno + 1) {
4461 xmlFARegExecSave(exec);
4462 }
Daniel Veillard54eb0242006-03-21 23:17:57 +00004463 /*
4464 * restart count for expressions like this ((abc){2})*
4465 */
4466 if (trans->count >= 0) {
4467#ifdef DEBUG_REGEXP_EXEC
4468 printf("Reset count %d\n", trans->count);
4469#endif
4470 exec->counts[trans->count] = 0;
4471 }
Daniel Veillard4255d502002-04-16 15:50:10 +00004472 if (trans->counter >= 0) {
4473#ifdef DEBUG_REGEXP_EXEC
4474 printf("Increasing count %d\n", trans->counter);
4475#endif
4476 exec->counts[trans->counter]++;
4477 }
4478#ifdef DEBUG_REGEXP_EXEC
4479 printf("entering state %d\n", trans->to);
4480#endif
4481 exec->state = exec->comp->states[trans->to];
4482 exec->transno = 0;
4483 if (trans->atom != NULL) {
4484 exec->index += len;
4485 }
4486 goto progress;
4487 } else if (ret < 0) {
4488 exec->status = -4;
4489 break;
4490 }
4491 }
4492 if ((exec->transno != 0) || (exec->state->nbTrans == 0)) {
4493rollback:
4494 /*
4495 * Failed to find a way out
4496 */
4497 exec->determinist = 0;
4498 xmlFARegExecRollBack(exec);
4499 }
4500progress:
4501 continue;
4502 }
4503}
4504#endif
4505/************************************************************************
4506 * *
William M. Brackddf71d62004-05-06 04:17:26 +00004507 * Parser for the Schemas Datatype Regular Expressions *
Daniel Veillard4255d502002-04-16 15:50:10 +00004508 * http://www.w3.org/TR/2001/REC-xmlschema-2-20010502/#regexs *
4509 * *
4510 ************************************************************************/
4511
4512/**
4513 * xmlFAIsChar:
Daniel Veillard441bc322002-04-20 17:38:48 +00004514 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00004515 *
4516 * [10] Char ::= [^.\?*+()|#x5B#x5D]
4517 */
4518static int
4519xmlFAIsChar(xmlRegParserCtxtPtr ctxt) {
4520 int cur;
4521 int len;
4522
4523 cur = CUR_SCHAR(ctxt->cur, len);
4524 if ((cur == '.') || (cur == '\\') || (cur == '?') ||
4525 (cur == '*') || (cur == '+') || (cur == '(') ||
4526 (cur == ')') || (cur == '|') || (cur == 0x5B) ||
4527 (cur == 0x5D) || (cur == 0))
4528 return(-1);
4529 return(cur);
4530}
4531
4532/**
4533 * xmlFAParseCharProp:
Daniel Veillard441bc322002-04-20 17:38:48 +00004534 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00004535 *
4536 * [27] charProp ::= IsCategory | IsBlock
4537 * [28] IsCategory ::= Letters | Marks | Numbers | Punctuation |
4538 * Separators | Symbols | Others
4539 * [29] Letters ::= 'L' [ultmo]?
4540 * [30] Marks ::= 'M' [nce]?
4541 * [31] Numbers ::= 'N' [dlo]?
4542 * [32] Punctuation ::= 'P' [cdseifo]?
4543 * [33] Separators ::= 'Z' [slp]?
4544 * [34] Symbols ::= 'S' [mcko]?
4545 * [35] Others ::= 'C' [cfon]?
4546 * [36] IsBlock ::= 'Is' [a-zA-Z0-9#x2D]+
4547 */
4548static void
4549xmlFAParseCharProp(xmlRegParserCtxtPtr ctxt) {
4550 int cur;
William M. Brack779af002003-08-01 15:55:39 +00004551 xmlRegAtomType type = (xmlRegAtomType) 0;
Daniel Veillard4255d502002-04-16 15:50:10 +00004552 xmlChar *blockName = NULL;
4553
4554 cur = CUR;
4555 if (cur == 'L') {
4556 NEXT;
4557 cur = CUR;
4558 if (cur == 'u') {
4559 NEXT;
4560 type = XML_REGEXP_LETTER_UPPERCASE;
4561 } else if (cur == 'l') {
4562 NEXT;
4563 type = XML_REGEXP_LETTER_LOWERCASE;
4564 } else if (cur == 't') {
4565 NEXT;
4566 type = XML_REGEXP_LETTER_TITLECASE;
4567 } else if (cur == 'm') {
4568 NEXT;
4569 type = XML_REGEXP_LETTER_MODIFIER;
4570 } else if (cur == 'o') {
4571 NEXT;
4572 type = XML_REGEXP_LETTER_OTHERS;
4573 } else {
4574 type = XML_REGEXP_LETTER;
4575 }
4576 } else if (cur == 'M') {
4577 NEXT;
4578 cur = CUR;
4579 if (cur == 'n') {
4580 NEXT;
4581 /* nonspacing */
4582 type = XML_REGEXP_MARK_NONSPACING;
4583 } else if (cur == 'c') {
4584 NEXT;
4585 /* spacing combining */
4586 type = XML_REGEXP_MARK_SPACECOMBINING;
4587 } else if (cur == 'e') {
4588 NEXT;
4589 /* enclosing */
4590 type = XML_REGEXP_MARK_ENCLOSING;
4591 } else {
4592 /* all marks */
4593 type = XML_REGEXP_MARK;
4594 }
4595 } else if (cur == 'N') {
4596 NEXT;
4597 cur = CUR;
4598 if (cur == 'd') {
4599 NEXT;
4600 /* digital */
4601 type = XML_REGEXP_NUMBER_DECIMAL;
4602 } else if (cur == 'l') {
4603 NEXT;
4604 /* letter */
4605 type = XML_REGEXP_NUMBER_LETTER;
4606 } else if (cur == 'o') {
4607 NEXT;
4608 /* other */
4609 type = XML_REGEXP_NUMBER_OTHERS;
4610 } else {
4611 /* all numbers */
4612 type = XML_REGEXP_NUMBER;
4613 }
4614 } else if (cur == 'P') {
4615 NEXT;
4616 cur = CUR;
4617 if (cur == 'c') {
4618 NEXT;
4619 /* connector */
4620 type = XML_REGEXP_PUNCT_CONNECTOR;
4621 } else if (cur == 'd') {
4622 NEXT;
4623 /* dash */
4624 type = XML_REGEXP_PUNCT_DASH;
4625 } else if (cur == 's') {
4626 NEXT;
4627 /* open */
4628 type = XML_REGEXP_PUNCT_OPEN;
4629 } else if (cur == 'e') {
4630 NEXT;
4631 /* close */
4632 type = XML_REGEXP_PUNCT_CLOSE;
4633 } else if (cur == 'i') {
4634 NEXT;
4635 /* initial quote */
4636 type = XML_REGEXP_PUNCT_INITQUOTE;
4637 } else if (cur == 'f') {
4638 NEXT;
4639 /* final quote */
4640 type = XML_REGEXP_PUNCT_FINQUOTE;
4641 } else if (cur == 'o') {
4642 NEXT;
4643 /* other */
4644 type = XML_REGEXP_PUNCT_OTHERS;
4645 } else {
4646 /* all punctuation */
4647 type = XML_REGEXP_PUNCT;
4648 }
4649 } else if (cur == 'Z') {
4650 NEXT;
4651 cur = CUR;
4652 if (cur == 's') {
4653 NEXT;
4654 /* space */
4655 type = XML_REGEXP_SEPAR_SPACE;
4656 } else if (cur == 'l') {
4657 NEXT;
4658 /* line */
4659 type = XML_REGEXP_SEPAR_LINE;
4660 } else if (cur == 'p') {
4661 NEXT;
4662 /* paragraph */
4663 type = XML_REGEXP_SEPAR_PARA;
4664 } else {
4665 /* all separators */
4666 type = XML_REGEXP_SEPAR;
4667 }
4668 } else if (cur == 'S') {
4669 NEXT;
4670 cur = CUR;
4671 if (cur == 'm') {
4672 NEXT;
4673 type = XML_REGEXP_SYMBOL_MATH;
4674 /* math */
4675 } else if (cur == 'c') {
4676 NEXT;
4677 type = XML_REGEXP_SYMBOL_CURRENCY;
4678 /* currency */
4679 } else if (cur == 'k') {
4680 NEXT;
4681 type = XML_REGEXP_SYMBOL_MODIFIER;
4682 /* modifiers */
4683 } else if (cur == 'o') {
4684 NEXT;
4685 type = XML_REGEXP_SYMBOL_OTHERS;
4686 /* other */
4687 } else {
4688 /* all symbols */
4689 type = XML_REGEXP_SYMBOL;
4690 }
4691 } else if (cur == 'C') {
4692 NEXT;
4693 cur = CUR;
4694 if (cur == 'c') {
4695 NEXT;
4696 /* control */
4697 type = XML_REGEXP_OTHER_CONTROL;
4698 } else if (cur == 'f') {
4699 NEXT;
4700 /* format */
4701 type = XML_REGEXP_OTHER_FORMAT;
4702 } else if (cur == 'o') {
4703 NEXT;
4704 /* private use */
4705 type = XML_REGEXP_OTHER_PRIVATE;
4706 } else if (cur == 'n') {
4707 NEXT;
4708 /* not assigned */
4709 type = XML_REGEXP_OTHER_NA;
4710 } else {
4711 /* all others */
4712 type = XML_REGEXP_OTHER;
4713 }
4714 } else if (cur == 'I') {
4715 const xmlChar *start;
4716 NEXT;
4717 cur = CUR;
4718 if (cur != 's') {
4719 ERROR("IsXXXX expected");
4720 return;
4721 }
4722 NEXT;
4723 start = ctxt->cur;
4724 cur = CUR;
4725 if (((cur >= 'a') && (cur <= 'z')) ||
4726 ((cur >= 'A') && (cur <= 'Z')) ||
4727 ((cur >= '0') && (cur <= '9')) ||
4728 (cur == 0x2D)) {
4729 NEXT;
4730 cur = CUR;
4731 while (((cur >= 'a') && (cur <= 'z')) ||
4732 ((cur >= 'A') && (cur <= 'Z')) ||
4733 ((cur >= '0') && (cur <= '9')) ||
4734 (cur == 0x2D)) {
4735 NEXT;
4736 cur = CUR;
4737 }
4738 }
4739 type = XML_REGEXP_BLOCK_NAME;
4740 blockName = xmlStrndup(start, ctxt->cur - start);
4741 } else {
4742 ERROR("Unknown char property");
4743 return;
4744 }
4745 if (ctxt->atom == NULL) {
4746 ctxt->atom = xmlRegNewAtom(ctxt, type);
4747 if (ctxt->atom != NULL)
4748 ctxt->atom->valuep = blockName;
4749 } else if (ctxt->atom->type == XML_REGEXP_RANGES) {
4750 xmlRegAtomAddRange(ctxt, ctxt->atom, ctxt->neg,
4751 type, 0, 0, blockName);
4752 }
4753}
4754
4755/**
4756 * xmlFAParseCharClassEsc:
Daniel Veillard441bc322002-04-20 17:38:48 +00004757 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00004758 *
4759 * [23] charClassEsc ::= ( SingleCharEsc | MultiCharEsc | catEsc | complEsc )
4760 * [24] SingleCharEsc ::= '\' [nrt\|.?*+(){}#x2D#x5B#x5D#x5E]
4761 * [25] catEsc ::= '\p{' charProp '}'
4762 * [26] complEsc ::= '\P{' charProp '}'
4763 * [37] MultiCharEsc ::= '.' | ('\' [sSiIcCdDwW])
4764 */
4765static void
4766xmlFAParseCharClassEsc(xmlRegParserCtxtPtr ctxt) {
4767 int cur;
4768
4769 if (CUR == '.') {
4770 if (ctxt->atom == NULL) {
4771 ctxt->atom = xmlRegNewAtom(ctxt, XML_REGEXP_ANYCHAR);
4772 } else if (ctxt->atom->type == XML_REGEXP_RANGES) {
4773 xmlRegAtomAddRange(ctxt, ctxt->atom, ctxt->neg,
4774 XML_REGEXP_ANYCHAR, 0, 0, NULL);
4775 }
4776 NEXT;
4777 return;
4778 }
4779 if (CUR != '\\') {
4780 ERROR("Escaped sequence: expecting \\");
4781 return;
4782 }
4783 NEXT;
4784 cur = CUR;
4785 if (cur == 'p') {
4786 NEXT;
4787 if (CUR != '{') {
4788 ERROR("Expecting '{'");
4789 return;
4790 }
4791 NEXT;
4792 xmlFAParseCharProp(ctxt);
4793 if (CUR != '}') {
4794 ERROR("Expecting '}'");
4795 return;
4796 }
4797 NEXT;
4798 } else if (cur == 'P') {
4799 NEXT;
4800 if (CUR != '{') {
4801 ERROR("Expecting '{'");
4802 return;
4803 }
4804 NEXT;
4805 xmlFAParseCharProp(ctxt);
4806 ctxt->atom->neg = 1;
4807 if (CUR != '}') {
4808 ERROR("Expecting '}'");
4809 return;
4810 }
4811 NEXT;
4812 } else if ((cur == 'n') || (cur == 'r') || (cur == 't') || (cur == '\\') ||
4813 (cur == '|') || (cur == '.') || (cur == '?') || (cur == '*') ||
4814 (cur == '+') || (cur == '(') || (cur == ')') || (cur == '{') ||
4815 (cur == '}') || (cur == 0x2D) || (cur == 0x5B) || (cur == 0x5D) ||
4816 (cur == 0x5E)) {
4817 if (ctxt->atom == NULL) {
4818 ctxt->atom = xmlRegNewAtom(ctxt, XML_REGEXP_CHARVAL);
Daniel Veillard99c394d2005-07-14 12:58:49 +00004819 if (ctxt->atom != NULL) {
4820 switch (cur) {
4821 case 'n':
4822 ctxt->atom->codepoint = '\n';
4823 break;
4824 case 'r':
4825 ctxt->atom->codepoint = '\r';
4826 break;
4827 case 't':
4828 ctxt->atom->codepoint = '\t';
4829 break;
4830 default:
4831 ctxt->atom->codepoint = cur;
4832 }
4833 }
Daniel Veillard4255d502002-04-16 15:50:10 +00004834 } else if (ctxt->atom->type == XML_REGEXP_RANGES) {
4835 xmlRegAtomAddRange(ctxt, ctxt->atom, ctxt->neg,
4836 XML_REGEXP_CHARVAL, cur, cur, NULL);
4837 }
4838 NEXT;
4839 } else if ((cur == 's') || (cur == 'S') || (cur == 'i') || (cur == 'I') ||
4840 (cur == 'c') || (cur == 'C') || (cur == 'd') || (cur == 'D') ||
4841 (cur == 'w') || (cur == 'W')) {
Daniel Veillardb509f152002-04-17 16:28:10 +00004842 xmlRegAtomType type = XML_REGEXP_ANYSPACE;
Daniel Veillard4255d502002-04-16 15:50:10 +00004843
4844 switch (cur) {
4845 case 's':
4846 type = XML_REGEXP_ANYSPACE;
4847 break;
4848 case 'S':
4849 type = XML_REGEXP_NOTSPACE;
4850 break;
4851 case 'i':
4852 type = XML_REGEXP_INITNAME;
4853 break;
4854 case 'I':
4855 type = XML_REGEXP_NOTINITNAME;
4856 break;
4857 case 'c':
4858 type = XML_REGEXP_NAMECHAR;
4859 break;
4860 case 'C':
4861 type = XML_REGEXP_NOTNAMECHAR;
4862 break;
4863 case 'd':
4864 type = XML_REGEXP_DECIMAL;
4865 break;
4866 case 'D':
4867 type = XML_REGEXP_NOTDECIMAL;
4868 break;
4869 case 'w':
4870 type = XML_REGEXP_REALCHAR;
4871 break;
4872 case 'W':
4873 type = XML_REGEXP_NOTREALCHAR;
4874 break;
4875 }
4876 NEXT;
4877 if (ctxt->atom == NULL) {
4878 ctxt->atom = xmlRegNewAtom(ctxt, type);
4879 } else if (ctxt->atom->type == XML_REGEXP_RANGES) {
4880 xmlRegAtomAddRange(ctxt, ctxt->atom, ctxt->neg,
4881 type, 0, 0, NULL);
4882 }
Daniel Veillardcb4284e2007-04-25 13:55:20 +00004883 } else {
4884 ERROR("Wrong escape sequence, misuse of character '\\'");
Daniel Veillard4255d502002-04-16 15:50:10 +00004885 }
4886}
4887
4888/**
4889 * xmlFAParseCharRef:
Daniel Veillard441bc322002-04-20 17:38:48 +00004890 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00004891 *
4892 * [19] XmlCharRef ::= ( '&#' [0-9]+ ';' ) | (' &#x' [0-9a-fA-F]+ ';' )
4893 */
4894static int
4895xmlFAParseCharRef(xmlRegParserCtxtPtr ctxt) {
4896 int ret = 0, cur;
4897
4898 if ((CUR != '&') || (NXT(1) != '#'))
4899 return(-1);
4900 NEXT;
4901 NEXT;
4902 cur = CUR;
4903 if (cur == 'x') {
4904 NEXT;
4905 cur = CUR;
4906 if (((cur >= '0') && (cur <= '9')) ||
4907 ((cur >= 'a') && (cur <= 'f')) ||
4908 ((cur >= 'A') && (cur <= 'F'))) {
4909 while (((cur >= '0') && (cur <= '9')) ||
Daniel Veillard11ce4002006-03-10 00:36:23 +00004910 ((cur >= 'a') && (cur <= 'f')) ||
Daniel Veillard4255d502002-04-16 15:50:10 +00004911 ((cur >= 'A') && (cur <= 'F'))) {
4912 if ((cur >= '0') && (cur <= '9'))
4913 ret = ret * 16 + cur - '0';
4914 else if ((cur >= 'a') && (cur <= 'f'))
4915 ret = ret * 16 + 10 + (cur - 'a');
4916 else
4917 ret = ret * 16 + 10 + (cur - 'A');
4918 NEXT;
4919 cur = CUR;
4920 }
4921 } else {
4922 ERROR("Char ref: expecting [0-9A-F]");
4923 return(-1);
4924 }
4925 } else {
4926 if ((cur >= '0') && (cur <= '9')) {
4927 while ((cur >= '0') && (cur <= '9')) {
4928 ret = ret * 10 + cur - '0';
4929 NEXT;
4930 cur = CUR;
4931 }
4932 } else {
4933 ERROR("Char ref: expecting [0-9]");
4934 return(-1);
4935 }
4936 }
4937 if (cur != ';') {
4938 ERROR("Char ref: expecting ';'");
4939 return(-1);
4940 } else {
4941 NEXT;
4942 }
4943 return(ret);
4944}
4945
4946/**
4947 * xmlFAParseCharRange:
Daniel Veillard441bc322002-04-20 17:38:48 +00004948 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00004949 *
4950 * [17] charRange ::= seRange | XmlCharRef | XmlCharIncDash
4951 * [18] seRange ::= charOrEsc '-' charOrEsc
4952 * [20] charOrEsc ::= XmlChar | SingleCharEsc
4953 * [21] XmlChar ::= [^\#x2D#x5B#x5D]
4954 * [22] XmlCharIncDash ::= [^\#x5B#x5D]
4955 */
4956static void
4957xmlFAParseCharRange(xmlRegParserCtxtPtr ctxt) {
William M. Brackdc99df92003-12-27 01:54:25 +00004958 int cur, len;
Daniel Veillard4255d502002-04-16 15:50:10 +00004959 int start = -1;
4960 int end = -1;
4961
Daniel Veillard777737e2006-10-17 21:23:17 +00004962 if (CUR == '\0') {
4963 ERROR("Expecting ']'");
4964 return;
4965 }
4966
Daniel Veillard4255d502002-04-16 15:50:10 +00004967 if ((CUR == '&') && (NXT(1) == '#')) {
4968 end = start = xmlFAParseCharRef(ctxt);
4969 xmlRegAtomAddRange(ctxt, ctxt->atom, ctxt->neg,
4970 XML_REGEXP_CHARVAL, start, end, NULL);
4971 return;
4972 }
4973 cur = CUR;
4974 if (cur == '\\') {
4975 NEXT;
4976 cur = CUR;
4977 switch (cur) {
4978 case 'n': start = 0xA; break;
4979 case 'r': start = 0xD; break;
4980 case 't': start = 0x9; break;
4981 case '\\': case '|': case '.': case '-': case '^': case '?':
4982 case '*': case '+': case '{': case '}': case '(': case ')':
4983 case '[': case ']':
4984 start = cur; break;
4985 default:
4986 ERROR("Invalid escape value");
4987 return;
4988 }
4989 end = start;
William M. Brackdc99df92003-12-27 01:54:25 +00004990 len = 1;
Daniel Veillard4255d502002-04-16 15:50:10 +00004991 } else if ((cur != 0x5B) && (cur != 0x5D)) {
William M. Brackdc99df92003-12-27 01:54:25 +00004992 end = start = CUR_SCHAR(ctxt->cur, len);
Daniel Veillard4255d502002-04-16 15:50:10 +00004993 } else {
4994 ERROR("Expecting a char range");
4995 return;
4996 }
William M. Bracka9cbf282007-03-21 13:16:33 +00004997 /*
4998 * Since we are "inside" a range, we can assume ctxt->cur is past
4999 * the start of ctxt->string, and PREV should be safe
5000 */
5001 if ((start == '-') && (NXT(1) != ']') && (PREV != '[') && (PREV != '^')) {
5002 NEXTL(len);
Daniel Veillard4255d502002-04-16 15:50:10 +00005003 return;
5004 }
William M. Bracka9cbf282007-03-21 13:16:33 +00005005 NEXTL(len);
Daniel Veillard4255d502002-04-16 15:50:10 +00005006 cur = CUR;
William M. Brack10f1ef42004-03-20 14:51:25 +00005007 if ((cur != '-') || (NXT(1) == ']')) {
Daniel Veillard4255d502002-04-16 15:50:10 +00005008 xmlRegAtomAddRange(ctxt, ctxt->atom, ctxt->neg,
5009 XML_REGEXP_CHARVAL, start, end, NULL);
5010 return;
5011 }
5012 NEXT;
5013 cur = CUR;
5014 if (cur == '\\') {
5015 NEXT;
5016 cur = CUR;
5017 switch (cur) {
5018 case 'n': end = 0xA; break;
5019 case 'r': end = 0xD; break;
5020 case 't': end = 0x9; break;
5021 case '\\': case '|': case '.': case '-': case '^': case '?':
5022 case '*': case '+': case '{': case '}': case '(': case ')':
5023 case '[': case ']':
5024 end = cur; break;
5025 default:
5026 ERROR("Invalid escape value");
5027 return;
5028 }
William M. Brackdc99df92003-12-27 01:54:25 +00005029 len = 1;
Daniel Veillard4255d502002-04-16 15:50:10 +00005030 } else if ((cur != 0x5B) && (cur != 0x5D)) {
William M. Brackdc99df92003-12-27 01:54:25 +00005031 end = CUR_SCHAR(ctxt->cur, len);
Daniel Veillard4255d502002-04-16 15:50:10 +00005032 } else {
5033 ERROR("Expecting the end of a char range");
5034 return;
5035 }
William M. Brackdc99df92003-12-27 01:54:25 +00005036 NEXTL(len);
Daniel Veillard4255d502002-04-16 15:50:10 +00005037 /* TODO check that the values are acceptable character ranges for XML */
5038 if (end < start) {
5039 ERROR("End of range is before start of range");
5040 } else {
5041 xmlRegAtomAddRange(ctxt, ctxt->atom, ctxt->neg,
5042 XML_REGEXP_CHARVAL, start, end, NULL);
5043 }
5044 return;
5045}
5046
5047/**
5048 * xmlFAParsePosCharGroup:
Daniel Veillard441bc322002-04-20 17:38:48 +00005049 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00005050 *
5051 * [14] posCharGroup ::= ( charRange | charClassEsc )+
5052 */
5053static void
5054xmlFAParsePosCharGroup(xmlRegParserCtxtPtr ctxt) {
5055 do {
5056 if ((CUR == '\\') || (CUR == '.')) {
5057 xmlFAParseCharClassEsc(ctxt);
5058 } else {
5059 xmlFAParseCharRange(ctxt);
5060 }
5061 } while ((CUR != ']') && (CUR != '^') && (CUR != '-') &&
Daniel Veillard777737e2006-10-17 21:23:17 +00005062 (CUR != 0) && (ctxt->error == 0));
Daniel Veillard4255d502002-04-16 15:50:10 +00005063}
5064
5065/**
5066 * xmlFAParseCharGroup:
Daniel Veillard441bc322002-04-20 17:38:48 +00005067 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00005068 *
5069 * [13] charGroup ::= posCharGroup | negCharGroup | charClassSub
5070 * [15] negCharGroup ::= '^' posCharGroup
5071 * [16] charClassSub ::= ( posCharGroup | negCharGroup ) '-' charClassExpr
5072 * [12] charClassExpr ::= '[' charGroup ']'
5073 */
5074static void
5075xmlFAParseCharGroup(xmlRegParserCtxtPtr ctxt) {
5076 int n = ctxt->neg;
5077 while ((CUR != ']') && (ctxt->error == 0)) {
5078 if (CUR == '^') {
5079 int neg = ctxt->neg;
5080
5081 NEXT;
5082 ctxt->neg = !ctxt->neg;
5083 xmlFAParsePosCharGroup(ctxt);
5084 ctxt->neg = neg;
William M. Brack10f1ef42004-03-20 14:51:25 +00005085 } else if ((CUR == '-') && (NXT(1) == '[')) {
Daniel Veillardf8b9de32003-11-24 14:27:26 +00005086 int neg = ctxt->neg;
Daniel Veillardf8b9de32003-11-24 14:27:26 +00005087 ctxt->neg = 2;
William M. Brack10f1ef42004-03-20 14:51:25 +00005088 NEXT; /* eat the '-' */
5089 NEXT; /* eat the '[' */
Daniel Veillard4255d502002-04-16 15:50:10 +00005090 xmlFAParseCharGroup(ctxt);
5091 if (CUR == ']') {
5092 NEXT;
5093 } else {
5094 ERROR("charClassExpr: ']' expected");
5095 break;
5096 }
Daniel Veillardf8b9de32003-11-24 14:27:26 +00005097 ctxt->neg = neg;
Daniel Veillard4255d502002-04-16 15:50:10 +00005098 break;
5099 } else if (CUR != ']') {
5100 xmlFAParsePosCharGroup(ctxt);
5101 }
5102 }
5103 ctxt->neg = n;
5104}
5105
5106/**
5107 * xmlFAParseCharClass:
Daniel Veillard441bc322002-04-20 17:38:48 +00005108 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00005109 *
5110 * [11] charClass ::= charClassEsc | charClassExpr
5111 * [12] charClassExpr ::= '[' charGroup ']'
5112 */
5113static void
5114xmlFAParseCharClass(xmlRegParserCtxtPtr ctxt) {
5115 if (CUR == '[') {
5116 NEXT;
5117 ctxt->atom = xmlRegNewAtom(ctxt, XML_REGEXP_RANGES);
5118 if (ctxt->atom == NULL)
5119 return;
5120 xmlFAParseCharGroup(ctxt);
5121 if (CUR == ']') {
5122 NEXT;
5123 } else {
5124 ERROR("xmlFAParseCharClass: ']' expected");
5125 }
5126 } else {
5127 xmlFAParseCharClassEsc(ctxt);
5128 }
5129}
5130
5131/**
5132 * xmlFAParseQuantExact:
Daniel Veillard441bc322002-04-20 17:38:48 +00005133 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00005134 *
5135 * [8] QuantExact ::= [0-9]+
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00005136 *
5137 * Returns 0 if success or -1 in case of error
Daniel Veillard4255d502002-04-16 15:50:10 +00005138 */
5139static int
5140xmlFAParseQuantExact(xmlRegParserCtxtPtr ctxt) {
5141 int ret = 0;
5142 int ok = 0;
5143
5144 while ((CUR >= '0') && (CUR <= '9')) {
5145 ret = ret * 10 + (CUR - '0');
5146 ok = 1;
5147 NEXT;
5148 }
5149 if (ok != 1) {
5150 return(-1);
5151 }
5152 return(ret);
5153}
5154
5155/**
5156 * xmlFAParseQuantifier:
Daniel Veillard441bc322002-04-20 17:38:48 +00005157 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00005158 *
5159 * [4] quantifier ::= [?*+] | ( '{' quantity '}' )
5160 * [5] quantity ::= quantRange | quantMin | QuantExact
5161 * [6] quantRange ::= QuantExact ',' QuantExact
5162 * [7] quantMin ::= QuantExact ','
5163 * [8] QuantExact ::= [0-9]+
5164 */
5165static int
5166xmlFAParseQuantifier(xmlRegParserCtxtPtr ctxt) {
5167 int cur;
5168
5169 cur = CUR;
5170 if ((cur == '?') || (cur == '*') || (cur == '+')) {
5171 if (ctxt->atom != NULL) {
5172 if (cur == '?')
5173 ctxt->atom->quant = XML_REGEXP_QUANT_OPT;
5174 else if (cur == '*')
5175 ctxt->atom->quant = XML_REGEXP_QUANT_MULT;
5176 else if (cur == '+')
5177 ctxt->atom->quant = XML_REGEXP_QUANT_PLUS;
5178 }
5179 NEXT;
5180 return(1);
5181 }
5182 if (cur == '{') {
5183 int min = 0, max = 0;
5184
5185 NEXT;
5186 cur = xmlFAParseQuantExact(ctxt);
5187 if (cur >= 0)
5188 min = cur;
5189 if (CUR == ',') {
5190 NEXT;
Daniel Veillardebe48c62003-12-03 12:12:27 +00005191 if (CUR == '}')
5192 max = INT_MAX;
5193 else {
5194 cur = xmlFAParseQuantExact(ctxt);
5195 if (cur >= 0)
5196 max = cur;
5197 else {
5198 ERROR("Improper quantifier");
5199 }
5200 }
Daniel Veillard4255d502002-04-16 15:50:10 +00005201 }
5202 if (CUR == '}') {
5203 NEXT;
5204 } else {
5205 ERROR("Unterminated quantifier");
5206 }
5207 if (max == 0)
5208 max = min;
5209 if (ctxt->atom != NULL) {
5210 ctxt->atom->quant = XML_REGEXP_QUANT_RANGE;
5211 ctxt->atom->min = min;
5212 ctxt->atom->max = max;
5213 }
5214 return(1);
5215 }
5216 return(0);
5217}
5218
5219/**
5220 * xmlFAParseAtom:
Daniel Veillard441bc322002-04-20 17:38:48 +00005221 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00005222 *
5223 * [9] atom ::= Char | charClass | ( '(' regExp ')' )
5224 */
5225static int
5226xmlFAParseAtom(xmlRegParserCtxtPtr ctxt) {
5227 int codepoint, len;
5228
5229 codepoint = xmlFAIsChar(ctxt);
5230 if (codepoint > 0) {
5231 ctxt->atom = xmlRegNewAtom(ctxt, XML_REGEXP_CHARVAL);
5232 if (ctxt->atom == NULL)
5233 return(-1);
5234 codepoint = CUR_SCHAR(ctxt->cur, len);
5235 ctxt->atom->codepoint = codepoint;
5236 NEXTL(len);
5237 return(1);
5238 } else if (CUR == '|') {
5239 return(0);
5240 } else if (CUR == 0) {
5241 return(0);
5242 } else if (CUR == ')') {
5243 return(0);
5244 } else if (CUR == '(') {
Daniel Veillard76d59b62007-08-22 16:29:21 +00005245 xmlRegStatePtr start, oldend, start0;
Daniel Veillard4255d502002-04-16 15:50:10 +00005246
5247 NEXT;
Daniel Veillard76d59b62007-08-22 16:29:21 +00005248 /*
5249 * this extra Epsilon transition is needed if we count with 0 allowed
5250 * unfortunately this can't be known at that point
5251 */
5252 xmlFAGenerateEpsilonTransition(ctxt, ctxt->state, NULL);
5253 start0 = ctxt->state;
Daniel Veillard4255d502002-04-16 15:50:10 +00005254 xmlFAGenerateEpsilonTransition(ctxt, ctxt->state, NULL);
5255 start = ctxt->state;
5256 oldend = ctxt->end;
5257 ctxt->end = NULL;
5258 ctxt->atom = NULL;
5259 xmlFAParseRegExp(ctxt, 0);
5260 if (CUR == ')') {
5261 NEXT;
5262 } else {
5263 ERROR("xmlFAParseAtom: expecting ')'");
5264 }
5265 ctxt->atom = xmlRegNewAtom(ctxt, XML_REGEXP_SUBREG);
5266 if (ctxt->atom == NULL)
5267 return(-1);
5268 ctxt->atom->start = start;
Daniel Veillard76d59b62007-08-22 16:29:21 +00005269 ctxt->atom->start0 = start0;
Daniel Veillard4255d502002-04-16 15:50:10 +00005270 ctxt->atom->stop = ctxt->state;
5271 ctxt->end = oldend;
5272 return(1);
5273 } else if ((CUR == '[') || (CUR == '\\') || (CUR == '.')) {
5274 xmlFAParseCharClass(ctxt);
5275 return(1);
5276 }
5277 return(0);
5278}
5279
5280/**
5281 * xmlFAParsePiece:
Daniel Veillard441bc322002-04-20 17:38:48 +00005282 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00005283 *
5284 * [3] piece ::= atom quantifier?
5285 */
5286static int
5287xmlFAParsePiece(xmlRegParserCtxtPtr ctxt) {
5288 int ret;
5289
5290 ctxt->atom = NULL;
5291 ret = xmlFAParseAtom(ctxt);
5292 if (ret == 0)
5293 return(0);
5294 if (ctxt->atom == NULL) {
5295 ERROR("internal: no atom generated");
5296 }
5297 xmlFAParseQuantifier(ctxt);
5298 return(1);
5299}
5300
5301/**
5302 * xmlFAParseBranch:
Daniel Veillard441bc322002-04-20 17:38:48 +00005303 * @ctxt: a regexp parser context
Daniel Veillard54eb0242006-03-21 23:17:57 +00005304 * @to: optional target to the end of the branch
5305 *
5306 * @to is used to optimize by removing duplicate path in automata
5307 * in expressions like (a|b)(c|d)
Daniel Veillard4255d502002-04-16 15:50:10 +00005308 *
5309 * [2] branch ::= piece*
5310 */
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00005311static int
Daniel Veillard54eb0242006-03-21 23:17:57 +00005312xmlFAParseBranch(xmlRegParserCtxtPtr ctxt, xmlRegStatePtr to) {
Daniel Veillard4255d502002-04-16 15:50:10 +00005313 xmlRegStatePtr previous;
Daniel Veillard4255d502002-04-16 15:50:10 +00005314 int ret;
5315
5316 previous = ctxt->state;
5317 ret = xmlFAParsePiece(ctxt);
5318 if (ret != 0) {
Daniel Veillard54eb0242006-03-21 23:17:57 +00005319 if (xmlFAGenerateTransitions(ctxt, previous,
5320 (CUR=='|' || CUR==')') ? to : NULL, ctxt->atom) < 0)
Daniel Veillard2cbf5962004-03-31 15:50:43 +00005321 return(-1);
5322 previous = ctxt->state;
Daniel Veillard4255d502002-04-16 15:50:10 +00005323 ctxt->atom = NULL;
5324 }
5325 while ((ret != 0) && (ctxt->error == 0)) {
5326 ret = xmlFAParsePiece(ctxt);
5327 if (ret != 0) {
Daniel Veillard54eb0242006-03-21 23:17:57 +00005328 if (xmlFAGenerateTransitions(ctxt, previous,
5329 (CUR=='|' || CUR==')') ? to : NULL, ctxt->atom) < 0)
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00005330 return(-1);
Daniel Veillard4255d502002-04-16 15:50:10 +00005331 previous = ctxt->state;
5332 ctxt->atom = NULL;
5333 }
5334 }
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00005335 return(0);
Daniel Veillard4255d502002-04-16 15:50:10 +00005336}
5337
5338/**
5339 * xmlFAParseRegExp:
Daniel Veillard441bc322002-04-20 17:38:48 +00005340 * @ctxt: a regexp parser context
William M. Brackddf71d62004-05-06 04:17:26 +00005341 * @top: is this the top-level expression ?
Daniel Veillard4255d502002-04-16 15:50:10 +00005342 *
5343 * [1] regExp ::= branch ( '|' branch )*
5344 */
5345static void
5346xmlFAParseRegExp(xmlRegParserCtxtPtr ctxt, int top) {
Daniel Veillardc7e3cc42004-09-28 12:33:52 +00005347 xmlRegStatePtr start, end;
Daniel Veillard4255d502002-04-16 15:50:10 +00005348
Daniel Veillard2cbf5962004-03-31 15:50:43 +00005349 /* if not top start should have been generated by an epsilon trans */
Daniel Veillard4255d502002-04-16 15:50:10 +00005350 start = ctxt->state;
Daniel Veillard2cbf5962004-03-31 15:50:43 +00005351 ctxt->end = NULL;
Daniel Veillard54eb0242006-03-21 23:17:57 +00005352 xmlFAParseBranch(ctxt, NULL);
Daniel Veillard2cbf5962004-03-31 15:50:43 +00005353 if (top) {
5354#ifdef DEBUG_REGEXP_GRAPH
5355 printf("State %d is final\n", ctxt->state->no);
5356#endif
5357 ctxt->state->type = XML_REGEXP_FINAL_STATE;
5358 }
Daniel Veillard4255d502002-04-16 15:50:10 +00005359 if (CUR != '|') {
5360 ctxt->end = ctxt->state;
5361 return;
5362 }
5363 end = ctxt->state;
5364 while ((CUR == '|') && (ctxt->error == 0)) {
5365 NEXT;
5366 ctxt->state = start;
Daniel Veillard2cbf5962004-03-31 15:50:43 +00005367 ctxt->end = NULL;
Daniel Veillard54eb0242006-03-21 23:17:57 +00005368 xmlFAParseBranch(ctxt, end);
Daniel Veillard4255d502002-04-16 15:50:10 +00005369 }
Daniel Veillard2cbf5962004-03-31 15:50:43 +00005370 if (!top) {
5371 ctxt->state = end;
5372 ctxt->end = end;
5373 }
Daniel Veillard4255d502002-04-16 15:50:10 +00005374}
5375
5376/************************************************************************
5377 * *
5378 * The basic API *
5379 * *
5380 ************************************************************************/
5381
5382/**
5383 * xmlRegexpPrint:
5384 * @output: the file for the output debug
5385 * @regexp: the compiled regexp
5386 *
5387 * Print the content of the compiled regular expression
5388 */
5389void
5390xmlRegexpPrint(FILE *output, xmlRegexpPtr regexp) {
5391 int i;
5392
Daniel Veillarda82b1822004-11-08 16:24:57 +00005393 if (output == NULL)
5394 return;
Daniel Veillard4255d502002-04-16 15:50:10 +00005395 fprintf(output, " regexp: ");
5396 if (regexp == NULL) {
5397 fprintf(output, "NULL\n");
5398 return;
5399 }
5400 fprintf(output, "'%s' ", regexp->string);
5401 fprintf(output, "\n");
5402 fprintf(output, "%d atoms:\n", regexp->nbAtoms);
5403 for (i = 0;i < regexp->nbAtoms; i++) {
5404 fprintf(output, " %02d ", i);
5405 xmlRegPrintAtom(output, regexp->atoms[i]);
5406 }
5407 fprintf(output, "%d states:", regexp->nbStates);
5408 fprintf(output, "\n");
5409 for (i = 0;i < regexp->nbStates; i++) {
5410 xmlRegPrintState(output, regexp->states[i]);
5411 }
5412 fprintf(output, "%d counters:\n", regexp->nbCounters);
5413 for (i = 0;i < regexp->nbCounters; i++) {
5414 fprintf(output, " %d: min %d max %d\n", i, regexp->counters[i].min,
5415 regexp->counters[i].max);
5416 }
5417}
5418
5419/**
5420 * xmlRegexpCompile:
5421 * @regexp: a regular expression string
5422 *
5423 * Parses a regular expression conforming to XML Schemas Part 2 Datatype
William M. Brackddf71d62004-05-06 04:17:26 +00005424 * Appendix F and builds an automata suitable for testing strings against
Daniel Veillard4255d502002-04-16 15:50:10 +00005425 * that regular expression
5426 *
5427 * Returns the compiled expression or NULL in case of error
5428 */
5429xmlRegexpPtr
5430xmlRegexpCompile(const xmlChar *regexp) {
5431 xmlRegexpPtr ret;
5432 xmlRegParserCtxtPtr ctxt;
5433
5434 ctxt = xmlRegNewParserCtxt(regexp);
5435 if (ctxt == NULL)
5436 return(NULL);
5437
5438 /* initialize the parser */
5439 ctxt->end = NULL;
5440 ctxt->start = ctxt->state = xmlRegNewState(ctxt);
5441 xmlRegStatePush(ctxt, ctxt->start);
5442
5443 /* parse the expression building an automata */
5444 xmlFAParseRegExp(ctxt, 1);
5445 if (CUR != 0) {
5446 ERROR("xmlFAParseRegExp: extra characters");
5447 }
Daniel Veillardcb4284e2007-04-25 13:55:20 +00005448 if (ctxt->error != 0) {
5449 xmlRegFreeParserCtxt(ctxt);
5450 return(NULL);
5451 }
Daniel Veillard4255d502002-04-16 15:50:10 +00005452 ctxt->end = ctxt->state;
5453 ctxt->start->type = XML_REGEXP_START_STATE;
5454 ctxt->end->type = XML_REGEXP_FINAL_STATE;
5455
5456 /* remove the Epsilon except for counted transitions */
5457 xmlFAEliminateEpsilonTransitions(ctxt);
5458
5459
5460 if (ctxt->error != 0) {
5461 xmlRegFreeParserCtxt(ctxt);
5462 return(NULL);
5463 }
5464 ret = xmlRegEpxFromParse(ctxt);
5465 xmlRegFreeParserCtxt(ctxt);
5466 return(ret);
5467}
5468
5469/**
5470 * xmlRegexpExec:
5471 * @comp: the compiled regular expression
5472 * @content: the value to check against the regular expression
5473 *
William M. Brackddf71d62004-05-06 04:17:26 +00005474 * Check if the regular expression generates the value
Daniel Veillard4255d502002-04-16 15:50:10 +00005475 *
William M. Brackddf71d62004-05-06 04:17:26 +00005476 * Returns 1 if it matches, 0 if not and a negative value in case of error
Daniel Veillard4255d502002-04-16 15:50:10 +00005477 */
5478int
5479xmlRegexpExec(xmlRegexpPtr comp, const xmlChar *content) {
5480 if ((comp == NULL) || (content == NULL))
5481 return(-1);
5482 return(xmlFARegExec(comp, content));
5483}
5484
5485/**
Daniel Veillard23e73572002-09-19 19:56:43 +00005486 * xmlRegexpIsDeterminist:
5487 * @comp: the compiled regular expression
5488 *
5489 * Check if the regular expression is determinist
5490 *
William M. Brackddf71d62004-05-06 04:17:26 +00005491 * Returns 1 if it yes, 0 if not and a negative value in case of error
Daniel Veillard23e73572002-09-19 19:56:43 +00005492 */
5493int
5494xmlRegexpIsDeterminist(xmlRegexpPtr comp) {
5495 xmlAutomataPtr am;
5496 int ret;
5497
5498 if (comp == NULL)
5499 return(-1);
5500 if (comp->determinist != -1)
5501 return(comp->determinist);
5502
5503 am = xmlNewAutomata();
Daniel Veillardbd9afb52002-09-25 22:25:35 +00005504 if (am->states != NULL) {
5505 int i;
5506
5507 for (i = 0;i < am->nbStates;i++)
5508 xmlRegFreeState(am->states[i]);
5509 xmlFree(am->states);
5510 }
Daniel Veillard23e73572002-09-19 19:56:43 +00005511 am->nbAtoms = comp->nbAtoms;
5512 am->atoms = comp->atoms;
5513 am->nbStates = comp->nbStates;
5514 am->states = comp->states;
5515 am->determinist = -1;
5516 ret = xmlFAComputesDeterminism(am);
5517 am->atoms = NULL;
5518 am->states = NULL;
5519 xmlFreeAutomata(am);
5520 return(ret);
5521}
5522
5523/**
Daniel Veillard4255d502002-04-16 15:50:10 +00005524 * xmlRegFreeRegexp:
5525 * @regexp: the regexp
5526 *
5527 * Free a regexp
5528 */
5529void
5530xmlRegFreeRegexp(xmlRegexpPtr regexp) {
5531 int i;
5532 if (regexp == NULL)
5533 return;
5534
5535 if (regexp->string != NULL)
5536 xmlFree(regexp->string);
5537 if (regexp->states != NULL) {
5538 for (i = 0;i < regexp->nbStates;i++)
5539 xmlRegFreeState(regexp->states[i]);
5540 xmlFree(regexp->states);
5541 }
5542 if (regexp->atoms != NULL) {
5543 for (i = 0;i < regexp->nbAtoms;i++)
5544 xmlRegFreeAtom(regexp->atoms[i]);
5545 xmlFree(regexp->atoms);
5546 }
5547 if (regexp->counters != NULL)
5548 xmlFree(regexp->counters);
Daniel Veillard23e73572002-09-19 19:56:43 +00005549 if (regexp->compact != NULL)
5550 xmlFree(regexp->compact);
Daniel Veillard118aed72002-09-24 14:13:13 +00005551 if (regexp->transdata != NULL)
5552 xmlFree(regexp->transdata);
Daniel Veillard23e73572002-09-19 19:56:43 +00005553 if (regexp->stringMap != NULL) {
5554 for (i = 0; i < regexp->nbstrings;i++)
5555 xmlFree(regexp->stringMap[i]);
5556 xmlFree(regexp->stringMap);
5557 }
5558
Daniel Veillard4255d502002-04-16 15:50:10 +00005559 xmlFree(regexp);
5560}
5561
5562#ifdef LIBXML_AUTOMATA_ENABLED
5563/************************************************************************
5564 * *
5565 * The Automata interface *
5566 * *
5567 ************************************************************************/
5568
5569/**
5570 * xmlNewAutomata:
5571 *
5572 * Create a new automata
5573 *
5574 * Returns the new object or NULL in case of failure
5575 */
5576xmlAutomataPtr
5577xmlNewAutomata(void) {
5578 xmlAutomataPtr ctxt;
5579
5580 ctxt = xmlRegNewParserCtxt(NULL);
5581 if (ctxt == NULL)
5582 return(NULL);
5583
5584 /* initialize the parser */
5585 ctxt->end = NULL;
5586 ctxt->start = ctxt->state = xmlRegNewState(ctxt);
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00005587 if (ctxt->start == NULL) {
5588 xmlFreeAutomata(ctxt);
5589 return(NULL);
5590 }
Daniel Veillardd0271472006-01-02 10:22:02 +00005591 ctxt->start->type = XML_REGEXP_START_STATE;
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00005592 if (xmlRegStatePush(ctxt, ctxt->start) < 0) {
5593 xmlRegFreeState(ctxt->start);
5594 xmlFreeAutomata(ctxt);
5595 return(NULL);
5596 }
Daniel Veillard4255d502002-04-16 15:50:10 +00005597
5598 return(ctxt);
5599}
5600
5601/**
5602 * xmlFreeAutomata:
5603 * @am: an automata
5604 *
5605 * Free an automata
5606 */
5607void
5608xmlFreeAutomata(xmlAutomataPtr am) {
5609 if (am == NULL)
5610 return;
5611 xmlRegFreeParserCtxt(am);
5612}
5613
5614/**
5615 * xmlAutomataGetInitState:
5616 * @am: an automata
5617 *
Daniel Veillarda9b66d02002-12-11 14:23:49 +00005618 * Initial state lookup
5619 *
Daniel Veillard4255d502002-04-16 15:50:10 +00005620 * Returns the initial state of the automata
5621 */
5622xmlAutomataStatePtr
5623xmlAutomataGetInitState(xmlAutomataPtr am) {
5624 if (am == NULL)
5625 return(NULL);
5626 return(am->start);
5627}
5628
5629/**
5630 * xmlAutomataSetFinalState:
5631 * @am: an automata
5632 * @state: a state in this automata
5633 *
5634 * Makes that state a final state
5635 *
5636 * Returns 0 or -1 in case of error
5637 */
5638int
5639xmlAutomataSetFinalState(xmlAutomataPtr am, xmlAutomataStatePtr state) {
5640 if ((am == NULL) || (state == NULL))
5641 return(-1);
5642 state->type = XML_REGEXP_FINAL_STATE;
5643 return(0);
5644}
5645
5646/**
5647 * xmlAutomataNewTransition:
5648 * @am: an automata
5649 * @from: the starting point of the transition
5650 * @to: the target point of the transition or NULL
5651 * @token: the input string associated to that transition
5652 * @data: data passed to the callback function if the transition is activated
5653 *
William M. Brackddf71d62004-05-06 04:17:26 +00005654 * If @to is NULL, this creates first a new target state in the automata
Daniel Veillard4255d502002-04-16 15:50:10 +00005655 * and then adds a transition from the @from state to the target state
5656 * activated by the value of @token
5657 *
5658 * Returns the target state or NULL in case of error
5659 */
5660xmlAutomataStatePtr
5661xmlAutomataNewTransition(xmlAutomataPtr am, xmlAutomataStatePtr from,
5662 xmlAutomataStatePtr to, const xmlChar *token,
5663 void *data) {
5664 xmlRegAtomPtr atom;
5665
5666 if ((am == NULL) || (from == NULL) || (token == NULL))
5667 return(NULL);
5668 atom = xmlRegNewAtom(am, XML_REGEXP_STRING);
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00005669 if (atom == NULL)
5670 return(NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +00005671 atom->data = data;
5672 if (atom == NULL)
5673 return(NULL);
5674 atom->valuep = xmlStrdup(token);
5675
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00005676 if (xmlFAGenerateTransitions(am, from, to, atom) < 0) {
5677 xmlRegFreeAtom(atom);
5678 return(NULL);
5679 }
Daniel Veillard4255d502002-04-16 15:50:10 +00005680 if (to == NULL)
5681 return(am->state);
5682 return(to);
5683}
5684
5685/**
Daniel Veillard52b48c72003-04-13 19:53:42 +00005686 * xmlAutomataNewTransition2:
5687 * @am: an automata
5688 * @from: the starting point of the transition
5689 * @to: the target point of the transition or NULL
5690 * @token: the first input string associated to that transition
5691 * @token2: the second input string associated to that transition
5692 * @data: data passed to the callback function if the transition is activated
5693 *
William M. Brackddf71d62004-05-06 04:17:26 +00005694 * If @to is NULL, this creates first a new target state in the automata
Daniel Veillard52b48c72003-04-13 19:53:42 +00005695 * and then adds a transition from the @from state to the target state
5696 * activated by the value of @token
5697 *
5698 * Returns the target state or NULL in case of error
5699 */
5700xmlAutomataStatePtr
5701xmlAutomataNewTransition2(xmlAutomataPtr am, xmlAutomataStatePtr from,
5702 xmlAutomataStatePtr to, const xmlChar *token,
5703 const xmlChar *token2, void *data) {
5704 xmlRegAtomPtr atom;
5705
5706 if ((am == NULL) || (from == NULL) || (token == NULL))
5707 return(NULL);
5708 atom = xmlRegNewAtom(am, XML_REGEXP_STRING);
Daniel Veillard52b48c72003-04-13 19:53:42 +00005709 if (atom == NULL)
5710 return(NULL);
Daniel Veillard11ce4002006-03-10 00:36:23 +00005711 atom->data = data;
Daniel Veillard52b48c72003-04-13 19:53:42 +00005712 if ((token2 == NULL) || (*token2 == 0)) {
5713 atom->valuep = xmlStrdup(token);
5714 } else {
5715 int lenn, lenp;
5716 xmlChar *str;
5717
5718 lenn = strlen((char *) token2);
5719 lenp = strlen((char *) token);
5720
Daniel Veillard3c908dc2003-04-19 00:07:51 +00005721 str = (xmlChar *) xmlMallocAtomic(lenn + lenp + 2);
Daniel Veillard52b48c72003-04-13 19:53:42 +00005722 if (str == NULL) {
5723 xmlRegFreeAtom(atom);
5724 return(NULL);
5725 }
5726 memcpy(&str[0], token, lenp);
5727 str[lenp] = '|';
5728 memcpy(&str[lenp + 1], token2, lenn);
5729 str[lenn + lenp + 1] = 0;
5730
5731 atom->valuep = str;
5732 }
5733
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00005734 if (xmlFAGenerateTransitions(am, from, to, atom) < 0) {
5735 xmlRegFreeAtom(atom);
5736 return(NULL);
5737 }
Daniel Veillard52b48c72003-04-13 19:53:42 +00005738 if (to == NULL)
5739 return(am->state);
5740 return(to);
5741}
5742
5743/**
Daniel Veillard9efc4762005-07-19 14:33:55 +00005744 * xmlAutomataNewNegTrans:
5745 * @am: an automata
5746 * @from: the starting point of the transition
5747 * @to: the target point of the transition or NULL
5748 * @token: the first input string associated to that transition
5749 * @token2: the second input string associated to that transition
5750 * @data: data passed to the callback function if the transition is activated
5751 *
5752 * If @to is NULL, this creates first a new target state in the automata
5753 * and then adds a transition from the @from state to the target state
5754 * activated by any value except (@token,@token2)
Daniel Veillard6e65e152005-08-09 11:09:52 +00005755 * Note that if @token2 is not NULL, then (X, NULL) won't match to follow
5756 # the semantic of XSD ##other
Daniel Veillard9efc4762005-07-19 14:33:55 +00005757 *
5758 * Returns the target state or NULL in case of error
5759 */
5760xmlAutomataStatePtr
5761xmlAutomataNewNegTrans(xmlAutomataPtr am, xmlAutomataStatePtr from,
5762 xmlAutomataStatePtr to, const xmlChar *token,
5763 const xmlChar *token2, void *data) {
5764 xmlRegAtomPtr atom;
Daniel Veillard77005e62005-07-19 16:26:18 +00005765 xmlChar err_msg[200];
Daniel Veillard9efc4762005-07-19 14:33:55 +00005766
5767 if ((am == NULL) || (from == NULL) || (token == NULL))
5768 return(NULL);
5769 atom = xmlRegNewAtom(am, XML_REGEXP_STRING);
5770 if (atom == NULL)
5771 return(NULL);
5772 atom->data = data;
5773 atom->neg = 1;
5774 if ((token2 == NULL) || (*token2 == 0)) {
5775 atom->valuep = xmlStrdup(token);
5776 } else {
5777 int lenn, lenp;
5778 xmlChar *str;
5779
5780 lenn = strlen((char *) token2);
5781 lenp = strlen((char *) token);
5782
5783 str = (xmlChar *) xmlMallocAtomic(lenn + lenp + 2);
5784 if (str == NULL) {
5785 xmlRegFreeAtom(atom);
5786 return(NULL);
5787 }
5788 memcpy(&str[0], token, lenp);
5789 str[lenp] = '|';
5790 memcpy(&str[lenp + 1], token2, lenn);
5791 str[lenn + lenp + 1] = 0;
5792
5793 atom->valuep = str;
5794 }
Daniel Veillarddb68b742005-07-30 13:18:24 +00005795 snprintf((char *) err_msg, 199, "not %s", (const char *) atom->valuep);
Daniel Veillard77005e62005-07-19 16:26:18 +00005796 err_msg[199] = 0;
5797 atom->valuep2 = xmlStrdup(err_msg);
Daniel Veillard9efc4762005-07-19 14:33:55 +00005798
5799 if (xmlFAGenerateTransitions(am, from, to, atom) < 0) {
5800 xmlRegFreeAtom(atom);
5801 return(NULL);
5802 }
Daniel Veillard6e65e152005-08-09 11:09:52 +00005803 am->negs++;
Daniel Veillard9efc4762005-07-19 14:33:55 +00005804 if (to == NULL)
5805 return(am->state);
5806 return(to);
5807}
5808
5809/**
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00005810 * xmlAutomataNewCountTrans2:
5811 * @am: an automata
5812 * @from: the starting point of the transition
5813 * @to: the target point of the transition or NULL
5814 * @token: the input string associated to that transition
5815 * @token2: the second input string associated to that transition
5816 * @min: the minimum successive occurences of token
5817 * @max: the maximum successive occurences of token
5818 * @data: data associated to the transition
5819 *
5820 * If @to is NULL, this creates first a new target state in the automata
5821 * and then adds a transition from the @from state to the target state
5822 * activated by a succession of input of value @token and @token2 and
5823 * whose number is between @min and @max
5824 *
5825 * Returns the target state or NULL in case of error
5826 */
5827xmlAutomataStatePtr
5828xmlAutomataNewCountTrans2(xmlAutomataPtr am, xmlAutomataStatePtr from,
5829 xmlAutomataStatePtr to, const xmlChar *token,
5830 const xmlChar *token2,
5831 int min, int max, void *data) {
5832 xmlRegAtomPtr atom;
5833 int counter;
5834
5835 if ((am == NULL) || (from == NULL) || (token == NULL))
5836 return(NULL);
5837 if (min < 0)
5838 return(NULL);
5839 if ((max < min) || (max < 1))
5840 return(NULL);
5841 atom = xmlRegNewAtom(am, XML_REGEXP_STRING);
5842 if (atom == NULL)
5843 return(NULL);
5844 if ((token2 == NULL) || (*token2 == 0)) {
5845 atom->valuep = xmlStrdup(token);
5846 } else {
5847 int lenn, lenp;
5848 xmlChar *str;
5849
5850 lenn = strlen((char *) token2);
5851 lenp = strlen((char *) token);
5852
5853 str = (xmlChar *) xmlMallocAtomic(lenn + lenp + 2);
5854 if (str == NULL) {
5855 xmlRegFreeAtom(atom);
5856 return(NULL);
5857 }
5858 memcpy(&str[0], token, lenp);
5859 str[lenp] = '|';
5860 memcpy(&str[lenp + 1], token2, lenn);
5861 str[lenn + lenp + 1] = 0;
5862
5863 atom->valuep = str;
5864 }
5865 atom->data = data;
5866 if (min == 0)
5867 atom->min = 1;
5868 else
5869 atom->min = min;
5870 atom->max = max;
5871
5872 /*
5873 * associate a counter to the transition.
5874 */
5875 counter = xmlRegGetCounter(am);
5876 am->counters[counter].min = min;
5877 am->counters[counter].max = max;
5878
5879 /* xmlFAGenerateTransitions(am, from, to, atom); */
5880 if (to == NULL) {
5881 to = xmlRegNewState(am);
5882 xmlRegStatePush(am, to);
5883 }
Daniel Veillard5de09382005-09-26 17:18:17 +00005884 xmlRegStateAddTrans(am, from, atom, to, counter, -1);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00005885 xmlRegAtomPush(am, atom);
5886 am->state = to;
5887
5888 if (to == NULL)
5889 to = am->state;
5890 if (to == NULL)
5891 return(NULL);
5892 if (min == 0)
5893 xmlFAGenerateEpsilonTransition(am, from, to);
5894 return(to);
5895}
5896
5897/**
Daniel Veillard4255d502002-04-16 15:50:10 +00005898 * xmlAutomataNewCountTrans:
5899 * @am: an automata
5900 * @from: the starting point of the transition
5901 * @to: the target point of the transition or NULL
5902 * @token: the input string associated to that transition
5903 * @min: the minimum successive occurences of token
Daniel Veillarda9b66d02002-12-11 14:23:49 +00005904 * @max: the maximum successive occurences of token
5905 * @data: data associated to the transition
Daniel Veillard4255d502002-04-16 15:50:10 +00005906 *
William M. Brackddf71d62004-05-06 04:17:26 +00005907 * If @to is NULL, this creates first a new target state in the automata
Daniel Veillard4255d502002-04-16 15:50:10 +00005908 * and then adds a transition from the @from state to the target state
5909 * activated by a succession of input of value @token and whose number
5910 * is between @min and @max
5911 *
5912 * Returns the target state or NULL in case of error
5913 */
5914xmlAutomataStatePtr
5915xmlAutomataNewCountTrans(xmlAutomataPtr am, xmlAutomataStatePtr from,
5916 xmlAutomataStatePtr to, const xmlChar *token,
5917 int min, int max, void *data) {
5918 xmlRegAtomPtr atom;
Daniel Veillard0ddb21c2004-02-12 12:43:49 +00005919 int counter;
Daniel Veillard4255d502002-04-16 15:50:10 +00005920
5921 if ((am == NULL) || (from == NULL) || (token == NULL))
5922 return(NULL);
5923 if (min < 0)
5924 return(NULL);
5925 if ((max < min) || (max < 1))
5926 return(NULL);
5927 atom = xmlRegNewAtom(am, XML_REGEXP_STRING);
5928 if (atom == NULL)
5929 return(NULL);
5930 atom->valuep = xmlStrdup(token);
5931 atom->data = data;
5932 if (min == 0)
5933 atom->min = 1;
5934 else
5935 atom->min = min;
5936 atom->max = max;
5937
Daniel Veillard0ddb21c2004-02-12 12:43:49 +00005938 /*
5939 * associate a counter to the transition.
5940 */
5941 counter = xmlRegGetCounter(am);
5942 am->counters[counter].min = min;
5943 am->counters[counter].max = max;
5944
5945 /* xmlFAGenerateTransitions(am, from, to, atom); */
5946 if (to == NULL) {
5947 to = xmlRegNewState(am);
5948 xmlRegStatePush(am, to);
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00005949 }
Daniel Veillard5de09382005-09-26 17:18:17 +00005950 xmlRegStateAddTrans(am, from, atom, to, counter, -1);
Daniel Veillard0ddb21c2004-02-12 12:43:49 +00005951 xmlRegAtomPush(am, atom);
5952 am->state = to;
5953
Daniel Veillard4255d502002-04-16 15:50:10 +00005954 if (to == NULL)
5955 to = am->state;
5956 if (to == NULL)
5957 return(NULL);
5958 if (min == 0)
5959 xmlFAGenerateEpsilonTransition(am, from, to);
5960 return(to);
5961}
5962
5963/**
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00005964 * xmlAutomataNewOnceTrans2:
5965 * @am: an automata
5966 * @from: the starting point of the transition
5967 * @to: the target point of the transition or NULL
5968 * @token: the input string associated to that transition
5969 * @token2: the second input string associated to that transition
5970 * @min: the minimum successive occurences of token
5971 * @max: the maximum successive occurences of token
5972 * @data: data associated to the transition
5973 *
5974 * If @to is NULL, this creates first a new target state in the automata
5975 * and then adds a transition from the @from state to the target state
5976 * activated by a succession of input of value @token and @token2 and whose
5977 * number is between @min and @max, moreover that transition can only be
5978 * crossed once.
5979 *
5980 * Returns the target state or NULL in case of error
5981 */
5982xmlAutomataStatePtr
5983xmlAutomataNewOnceTrans2(xmlAutomataPtr am, xmlAutomataStatePtr from,
5984 xmlAutomataStatePtr to, const xmlChar *token,
5985 const xmlChar *token2,
5986 int min, int max, void *data) {
5987 xmlRegAtomPtr atom;
5988 int counter;
5989
5990 if ((am == NULL) || (from == NULL) || (token == NULL))
5991 return(NULL);
5992 if (min < 1)
5993 return(NULL);
5994 if ((max < min) || (max < 1))
5995 return(NULL);
5996 atom = xmlRegNewAtom(am, XML_REGEXP_STRING);
5997 if (atom == NULL)
5998 return(NULL);
5999 if ((token2 == NULL) || (*token2 == 0)) {
6000 atom->valuep = xmlStrdup(token);
6001 } else {
6002 int lenn, lenp;
6003 xmlChar *str;
6004
6005 lenn = strlen((char *) token2);
6006 lenp = strlen((char *) token);
6007
6008 str = (xmlChar *) xmlMallocAtomic(lenn + lenp + 2);
6009 if (str == NULL) {
6010 xmlRegFreeAtom(atom);
6011 return(NULL);
6012 }
6013 memcpy(&str[0], token, lenp);
6014 str[lenp] = '|';
6015 memcpy(&str[lenp + 1], token2, lenn);
6016 str[lenn + lenp + 1] = 0;
6017
6018 atom->valuep = str;
6019 }
6020 atom->data = data;
6021 atom->quant = XML_REGEXP_QUANT_ONCEONLY;
Daniel Veillard11ce4002006-03-10 00:36:23 +00006022 atom->min = min;
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00006023 atom->max = max;
6024 /*
6025 * associate a counter to the transition.
6026 */
6027 counter = xmlRegGetCounter(am);
6028 am->counters[counter].min = 1;
6029 am->counters[counter].max = 1;
6030
6031 /* xmlFAGenerateTransitions(am, from, to, atom); */
6032 if (to == NULL) {
6033 to = xmlRegNewState(am);
6034 xmlRegStatePush(am, to);
6035 }
Daniel Veillard5de09382005-09-26 17:18:17 +00006036 xmlRegStateAddTrans(am, from, atom, to, counter, -1);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00006037 xmlRegAtomPush(am, atom);
6038 am->state = to;
6039 return(to);
6040}
6041
6042
6043
6044/**
Daniel Veillard7646b182002-04-20 06:41:40 +00006045 * xmlAutomataNewOnceTrans:
6046 * @am: an automata
6047 * @from: the starting point of the transition
6048 * @to: the target point of the transition or NULL
6049 * @token: the input string associated to that transition
6050 * @min: the minimum successive occurences of token
Daniel Veillarda9b66d02002-12-11 14:23:49 +00006051 * @max: the maximum successive occurences of token
6052 * @data: data associated to the transition
Daniel Veillard7646b182002-04-20 06:41:40 +00006053 *
William M. Brackddf71d62004-05-06 04:17:26 +00006054 * If @to is NULL, this creates first a new target state in the automata
Daniel Veillard7646b182002-04-20 06:41:40 +00006055 * and then adds a transition from the @from state to the target state
6056 * activated by a succession of input of value @token and whose number
William M. Brackddf71d62004-05-06 04:17:26 +00006057 * is between @min and @max, moreover that transition can only be crossed
Daniel Veillard7646b182002-04-20 06:41:40 +00006058 * once.
6059 *
6060 * Returns the target state or NULL in case of error
6061 */
6062xmlAutomataStatePtr
6063xmlAutomataNewOnceTrans(xmlAutomataPtr am, xmlAutomataStatePtr from,
6064 xmlAutomataStatePtr to, const xmlChar *token,
6065 int min, int max, void *data) {
6066 xmlRegAtomPtr atom;
6067 int counter;
6068
6069 if ((am == NULL) || (from == NULL) || (token == NULL))
6070 return(NULL);
6071 if (min < 1)
6072 return(NULL);
6073 if ((max < min) || (max < 1))
6074 return(NULL);
6075 atom = xmlRegNewAtom(am, XML_REGEXP_STRING);
6076 if (atom == NULL)
6077 return(NULL);
6078 atom->valuep = xmlStrdup(token);
6079 atom->data = data;
6080 atom->quant = XML_REGEXP_QUANT_ONCEONLY;
Daniel Veillard11ce4002006-03-10 00:36:23 +00006081 atom->min = min;
Daniel Veillard7646b182002-04-20 06:41:40 +00006082 atom->max = max;
6083 /*
6084 * associate a counter to the transition.
6085 */
6086 counter = xmlRegGetCounter(am);
6087 am->counters[counter].min = 1;
6088 am->counters[counter].max = 1;
6089
6090 /* xmlFAGenerateTransitions(am, from, to, atom); */
6091 if (to == NULL) {
6092 to = xmlRegNewState(am);
6093 xmlRegStatePush(am, to);
6094 }
Daniel Veillard5de09382005-09-26 17:18:17 +00006095 xmlRegStateAddTrans(am, from, atom, to, counter, -1);
Daniel Veillard7646b182002-04-20 06:41:40 +00006096 xmlRegAtomPush(am, atom);
6097 am->state = to;
Daniel Veillard7646b182002-04-20 06:41:40 +00006098 return(to);
6099}
6100
6101/**
Daniel Veillard4255d502002-04-16 15:50:10 +00006102 * xmlAutomataNewState:
6103 * @am: an automata
6104 *
6105 * Create a new disconnected state in the automata
6106 *
6107 * Returns the new state or NULL in case of error
6108 */
6109xmlAutomataStatePtr
6110xmlAutomataNewState(xmlAutomataPtr am) {
6111 xmlAutomataStatePtr to;
6112
6113 if (am == NULL)
6114 return(NULL);
6115 to = xmlRegNewState(am);
6116 xmlRegStatePush(am, to);
6117 return(to);
6118}
6119
6120/**
Daniel Veillarda9b66d02002-12-11 14:23:49 +00006121 * xmlAutomataNewEpsilon:
Daniel Veillard4255d502002-04-16 15:50:10 +00006122 * @am: an automata
6123 * @from: the starting point of the transition
6124 * @to: the target point of the transition or NULL
6125 *
William M. Brackddf71d62004-05-06 04:17:26 +00006126 * If @to is NULL, this creates first a new target state in the automata
6127 * and then adds an epsilon transition from the @from state to the
Daniel Veillard4255d502002-04-16 15:50:10 +00006128 * target state
6129 *
6130 * Returns the target state or NULL in case of error
6131 */
6132xmlAutomataStatePtr
6133xmlAutomataNewEpsilon(xmlAutomataPtr am, xmlAutomataStatePtr from,
6134 xmlAutomataStatePtr to) {
6135 if ((am == NULL) || (from == NULL))
6136 return(NULL);
6137 xmlFAGenerateEpsilonTransition(am, from, to);
6138 if (to == NULL)
6139 return(am->state);
6140 return(to);
6141}
6142
Daniel Veillardb509f152002-04-17 16:28:10 +00006143/**
Daniel Veillard7646b182002-04-20 06:41:40 +00006144 * xmlAutomataNewAllTrans:
6145 * @am: an automata
6146 * @from: the starting point of the transition
6147 * @to: the target point of the transition or NULL
Daniel Veillarda9b66d02002-12-11 14:23:49 +00006148 * @lax: allow to transition if not all all transitions have been activated
Daniel Veillard7646b182002-04-20 06:41:40 +00006149 *
William M. Brackddf71d62004-05-06 04:17:26 +00006150 * If @to is NULL, this creates first a new target state in the automata
Daniel Veillard7646b182002-04-20 06:41:40 +00006151 * and then adds a an ALL transition from the @from state to the
6152 * target state. That transition is an epsilon transition allowed only when
6153 * all transitions from the @from node have been activated.
6154 *
6155 * Returns the target state or NULL in case of error
6156 */
6157xmlAutomataStatePtr
6158xmlAutomataNewAllTrans(xmlAutomataPtr am, xmlAutomataStatePtr from,
Daniel Veillard441bc322002-04-20 17:38:48 +00006159 xmlAutomataStatePtr to, int lax) {
Daniel Veillard7646b182002-04-20 06:41:40 +00006160 if ((am == NULL) || (from == NULL))
6161 return(NULL);
Daniel Veillard441bc322002-04-20 17:38:48 +00006162 xmlFAGenerateAllTransition(am, from, to, lax);
Daniel Veillard7646b182002-04-20 06:41:40 +00006163 if (to == NULL)
6164 return(am->state);
6165 return(to);
6166}
6167
6168/**
Daniel Veillardb509f152002-04-17 16:28:10 +00006169 * xmlAutomataNewCounter:
6170 * @am: an automata
6171 * @min: the minimal value on the counter
6172 * @max: the maximal value on the counter
6173 *
6174 * Create a new counter
6175 *
6176 * Returns the counter number or -1 in case of error
6177 */
6178int
6179xmlAutomataNewCounter(xmlAutomataPtr am, int min, int max) {
6180 int ret;
6181
6182 if (am == NULL)
6183 return(-1);
6184
6185 ret = xmlRegGetCounter(am);
6186 if (ret < 0)
6187 return(-1);
6188 am->counters[ret].min = min;
6189 am->counters[ret].max = max;
6190 return(ret);
6191}
6192
6193/**
6194 * xmlAutomataNewCountedTrans:
6195 * @am: an automata
6196 * @from: the starting point of the transition
6197 * @to: the target point of the transition or NULL
6198 * @counter: the counter associated to that transition
6199 *
William M. Brackddf71d62004-05-06 04:17:26 +00006200 * If @to is NULL, this creates first a new target state in the automata
Daniel Veillardb509f152002-04-17 16:28:10 +00006201 * and then adds an epsilon transition from the @from state to the target state
6202 * which will increment the counter provided
6203 *
6204 * Returns the target state or NULL in case of error
6205 */
6206xmlAutomataStatePtr
6207xmlAutomataNewCountedTrans(xmlAutomataPtr am, xmlAutomataStatePtr from,
6208 xmlAutomataStatePtr to, int counter) {
6209 if ((am == NULL) || (from == NULL) || (counter < 0))
6210 return(NULL);
6211 xmlFAGenerateCountedEpsilonTransition(am, from, to, counter);
6212 if (to == NULL)
6213 return(am->state);
6214 return(to);
6215}
6216
6217/**
6218 * xmlAutomataNewCounterTrans:
6219 * @am: an automata
6220 * @from: the starting point of the transition
6221 * @to: the target point of the transition or NULL
6222 * @counter: the counter associated to that transition
6223 *
William M. Brackddf71d62004-05-06 04:17:26 +00006224 * If @to is NULL, this creates first a new target state in the automata
Daniel Veillardb509f152002-04-17 16:28:10 +00006225 * and then adds an epsilon transition from the @from state to the target state
6226 * which will be allowed only if the counter is within the right range.
6227 *
6228 * Returns the target state or NULL in case of error
6229 */
6230xmlAutomataStatePtr
6231xmlAutomataNewCounterTrans(xmlAutomataPtr am, xmlAutomataStatePtr from,
6232 xmlAutomataStatePtr to, int counter) {
6233 if ((am == NULL) || (from == NULL) || (counter < 0))
6234 return(NULL);
6235 xmlFAGenerateCountedTransition(am, from, to, counter);
6236 if (to == NULL)
6237 return(am->state);
6238 return(to);
6239}
Daniel Veillard4255d502002-04-16 15:50:10 +00006240
6241/**
6242 * xmlAutomataCompile:
6243 * @am: an automata
6244 *
6245 * Compile the automata into a Reg Exp ready for being executed.
6246 * The automata should be free after this point.
6247 *
6248 * Returns the compiled regexp or NULL in case of error
6249 */
6250xmlRegexpPtr
6251xmlAutomataCompile(xmlAutomataPtr am) {
6252 xmlRegexpPtr ret;
6253
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00006254 if ((am == NULL) || (am->error != 0)) return(NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +00006255 xmlFAEliminateEpsilonTransitions(am);
Daniel Veillard23e73572002-09-19 19:56:43 +00006256 /* xmlFAComputesDeterminism(am); */
Daniel Veillard4255d502002-04-16 15:50:10 +00006257 ret = xmlRegEpxFromParse(am);
6258
6259 return(ret);
6260}
Daniel Veillarde19fc232002-04-22 16:01:24 +00006261
6262/**
6263 * xmlAutomataIsDeterminist:
6264 * @am: an automata
6265 *
6266 * Checks if an automata is determinist.
6267 *
6268 * Returns 1 if true, 0 if not, and -1 in case of error
6269 */
6270int
6271xmlAutomataIsDeterminist(xmlAutomataPtr am) {
6272 int ret;
6273
6274 if (am == NULL)
6275 return(-1);
6276
6277 ret = xmlFAComputesDeterminism(am);
6278 return(ret);
6279}
Daniel Veillard4255d502002-04-16 15:50:10 +00006280#endif /* LIBXML_AUTOMATA_ENABLED */
Daniel Veillard81a8ec62005-08-22 00:20:58 +00006281
6282#ifdef LIBXML_EXPR_ENABLED
6283/************************************************************************
6284 * *
6285 * Formal Expression handling code *
6286 * *
6287 ************************************************************************/
Daniel Veillard81a8ec62005-08-22 00:20:58 +00006288/************************************************************************
6289 * *
6290 * Expression handling context *
6291 * *
6292 ************************************************************************/
6293
6294struct _xmlExpCtxt {
6295 xmlDictPtr dict;
6296 xmlExpNodePtr *table;
6297 int size;
6298 int nbElems;
6299 int nb_nodes;
6300 const char *expr;
6301 const char *cur;
6302 int nb_cons;
Daniel Veillard81a8ec62005-08-22 00:20:58 +00006303 int tabSize;
6304};
6305
6306/**
6307 * xmlExpNewCtxt:
6308 * @maxNodes: the maximum number of nodes
6309 * @dict: optional dictionnary to use internally
6310 *
6311 * Creates a new context for manipulating expressions
6312 *
6313 * Returns the context or NULL in case of error
6314 */
6315xmlExpCtxtPtr
6316xmlExpNewCtxt(int maxNodes, xmlDictPtr dict) {
6317 xmlExpCtxtPtr ret;
6318 int size = 256;
6319
6320 if (maxNodes <= 4096)
6321 maxNodes = 4096;
6322
6323 ret = (xmlExpCtxtPtr) xmlMalloc(sizeof(xmlExpCtxt));
6324 if (ret == NULL)
6325 return(NULL);
6326 memset(ret, 0, sizeof(xmlExpCtxt));
6327 ret->size = size;
6328 ret->nbElems = 0;
6329 ret->table = xmlMalloc(size * sizeof(xmlExpNodePtr));
6330 if (ret->table == NULL) {
6331 xmlFree(ret);
6332 return(NULL);
6333 }
6334 memset(ret->table, 0, size * sizeof(xmlExpNodePtr));
6335 if (dict == NULL) {
6336 ret->dict = xmlDictCreate();
6337 if (ret->dict == NULL) {
6338 xmlFree(ret->table);
6339 xmlFree(ret);
6340 return(NULL);
6341 }
6342 } else {
6343 ret->dict = dict;
6344 xmlDictReference(ret->dict);
6345 }
6346 return(ret);
6347}
6348
6349/**
6350 * xmlExpFreeCtxt:
6351 * @ctxt: an expression context
6352 *
6353 * Free an expression context
6354 */
6355void
6356xmlExpFreeCtxt(xmlExpCtxtPtr ctxt) {
6357 if (ctxt == NULL)
6358 return;
6359 xmlDictFree(ctxt->dict);
6360 if (ctxt->table != NULL)
6361 xmlFree(ctxt->table);
6362 xmlFree(ctxt);
6363}
6364
6365/************************************************************************
6366 * *
6367 * Structure associated to an expression node *
6368 * *
6369 ************************************************************************/
Daniel Veillard465a0002005-08-22 12:07:04 +00006370#define MAX_NODES 10000
6371
6372/* #define DEBUG_DERIV */
6373
6374/*
6375 * TODO:
6376 * - Wildcards
6377 * - public API for creation
6378 *
6379 * Started
6380 * - regression testing
6381 *
6382 * Done
6383 * - split into module and test tool
6384 * - memleaks
6385 */
Daniel Veillard81a8ec62005-08-22 00:20:58 +00006386
6387typedef enum {
6388 XML_EXP_NILABLE = (1 << 0)
6389} xmlExpNodeInfo;
6390
6391#define IS_NILLABLE(node) ((node)->info & XML_EXP_NILABLE)
6392
6393struct _xmlExpNode {
6394 unsigned char type;/* xmlExpNodeType */
6395 unsigned char info;/* OR of xmlExpNodeInfo */
6396 unsigned short key; /* the hash key */
6397 unsigned int ref; /* The number of references */
6398 int c_max; /* the maximum length it can consume */
6399 xmlExpNodePtr exp_left;
6400 xmlExpNodePtr next;/* the next node in the hash table or free list */
6401 union {
6402 struct {
6403 int f_min;
6404 int f_max;
6405 } count;
6406 struct {
6407 xmlExpNodePtr f_right;
6408 } children;
6409 const xmlChar *f_str;
6410 } field;
6411};
6412
6413#define exp_min field.count.f_min
6414#define exp_max field.count.f_max
6415/* #define exp_left field.children.f_left */
6416#define exp_right field.children.f_right
6417#define exp_str field.f_str
6418
6419static xmlExpNodePtr xmlExpNewNode(xmlExpCtxtPtr ctxt, xmlExpNodeType type);
6420static xmlExpNode forbiddenExpNode = {
6421 XML_EXP_FORBID, 0, 0, 0, 0, NULL, NULL, {{ 0, 0}}
6422};
6423xmlExpNodePtr forbiddenExp = &forbiddenExpNode;
6424static xmlExpNode emptyExpNode = {
6425 XML_EXP_EMPTY, 1, 0, 0, 0, NULL, NULL, {{ 0, 0}}
6426};
6427xmlExpNodePtr emptyExp = &emptyExpNode;
6428
6429/************************************************************************
6430 * *
6431 * The custom hash table for unicity and canonicalization *
6432 * of sub-expressions pointers *
6433 * *
6434 ************************************************************************/
6435/*
6436 * xmlExpHashNameComputeKey:
6437 * Calculate the hash key for a token
6438 */
6439static unsigned short
6440xmlExpHashNameComputeKey(const xmlChar *name) {
6441 unsigned short value = 0L;
6442 char ch;
6443
6444 if (name != NULL) {
6445 value += 30 * (*name);
6446 while ((ch = *name++) != 0) {
6447 value = value ^ ((value << 5) + (value >> 3) + (unsigned long)ch);
6448 }
6449 }
6450 return (value);
6451}
6452
6453/*
6454 * xmlExpHashComputeKey:
6455 * Calculate the hash key for a compound expression
6456 */
6457static unsigned short
6458xmlExpHashComputeKey(xmlExpNodeType type, xmlExpNodePtr left,
6459 xmlExpNodePtr right) {
6460 unsigned long value;
6461 unsigned short ret;
6462
6463 switch (type) {
6464 case XML_EXP_SEQ:
6465 value = left->key;
6466 value += right->key;
6467 value *= 3;
6468 ret = (unsigned short) value;
6469 break;
6470 case XML_EXP_OR:
6471 value = left->key;
6472 value += right->key;
6473 value *= 7;
6474 ret = (unsigned short) value;
6475 break;
6476 case XML_EXP_COUNT:
6477 value = left->key;
6478 value += right->key;
6479 ret = (unsigned short) value;
6480 break;
6481 default:
6482 ret = 0;
6483 }
6484 return(ret);
6485}
6486
6487
6488static xmlExpNodePtr
6489xmlExpNewNode(xmlExpCtxtPtr ctxt, xmlExpNodeType type) {
6490 xmlExpNodePtr ret;
6491
6492 if (ctxt->nb_nodes >= MAX_NODES)
6493 return(NULL);
6494 ret = (xmlExpNodePtr) xmlMalloc(sizeof(xmlExpNode));
6495 if (ret == NULL)
6496 return(NULL);
6497 memset(ret, 0, sizeof(xmlExpNode));
6498 ret->type = type;
6499 ret->next = NULL;
6500 ctxt->nb_nodes++;
6501 ctxt->nb_cons++;
6502 return(ret);
6503}
6504
6505/**
6506 * xmlExpHashGetEntry:
6507 * @table: the hash table
6508 *
6509 * Get the unique entry from the hash table. The entry is created if
6510 * needed. @left and @right are consumed, i.e. their ref count will
6511 * be decremented by the operation.
6512 *
6513 * Returns the pointer or NULL in case of error
6514 */
6515static xmlExpNodePtr
6516xmlExpHashGetEntry(xmlExpCtxtPtr ctxt, xmlExpNodeType type,
6517 xmlExpNodePtr left, xmlExpNodePtr right,
6518 const xmlChar *name, int min, int max) {
6519 unsigned short kbase, key;
6520 xmlExpNodePtr entry;
6521 xmlExpNodePtr insert;
6522
6523 if (ctxt == NULL)
6524 return(NULL);
6525
6526 /*
6527 * Check for duplicate and insertion location.
6528 */
6529 if (type == XML_EXP_ATOM) {
6530 kbase = xmlExpHashNameComputeKey(name);
6531 } else if (type == XML_EXP_COUNT) {
6532 /* COUNT reduction rule 1 */
6533 /* a{1} -> a */
6534 if (min == max) {
6535 if (min == 1) {
6536 return(left);
6537 }
6538 if (min == 0) {
6539 xmlExpFree(ctxt, left);
6540 return(emptyExp);
6541 }
6542 }
6543 if (min < 0) {
6544 xmlExpFree(ctxt, left);
6545 return(forbiddenExp);
6546 }
6547 if (max == -1)
6548 kbase = min + 79;
6549 else
6550 kbase = max - min;
6551 kbase += left->key;
6552 } else if (type == XML_EXP_OR) {
6553 /* Forbid reduction rules */
6554 if (left->type == XML_EXP_FORBID) {
6555 xmlExpFree(ctxt, left);
6556 return(right);
6557 }
6558 if (right->type == XML_EXP_FORBID) {
6559 xmlExpFree(ctxt, right);
6560 return(left);
6561 }
6562
6563 /* OR reduction rule 1 */
6564 /* a | a reduced to a */
6565 if (left == right) {
6566 left->ref--;
6567 return(left);
6568 }
6569 /* OR canonicalization rule 1 */
6570 /* linearize (a | b) | c into a | (b | c) */
6571 if ((left->type == XML_EXP_OR) && (right->type != XML_EXP_OR)) {
6572 xmlExpNodePtr tmp = left;
6573 left = right;
6574 right = tmp;
6575 }
6576 /* OR reduction rule 2 */
6577 /* a | (a | b) and b | (a | b) are reduced to a | b */
6578 if (right->type == XML_EXP_OR) {
6579 if ((left == right->exp_left) ||
6580 (left == right->exp_right)) {
6581 xmlExpFree(ctxt, left);
6582 return(right);
6583 }
6584 }
6585 /* OR canonicalization rule 2 */
6586 /* linearize (a | b) | c into a | (b | c) */
6587 if (left->type == XML_EXP_OR) {
6588 xmlExpNodePtr tmp;
6589
6590 /* OR canonicalization rule 2 */
6591 if ((left->exp_right->type != XML_EXP_OR) &&
6592 (left->exp_right->key < left->exp_left->key)) {
6593 tmp = left->exp_right;
6594 left->exp_right = left->exp_left;
6595 left->exp_left = tmp;
6596 }
6597 left->exp_right->ref++;
6598 tmp = xmlExpHashGetEntry(ctxt, XML_EXP_OR, left->exp_right, right,
6599 NULL, 0, 0);
6600 left->exp_left->ref++;
6601 tmp = xmlExpHashGetEntry(ctxt, XML_EXP_OR, left->exp_left, tmp,
6602 NULL, 0, 0);
6603
6604 xmlExpFree(ctxt, left);
6605 return(tmp);
6606 }
6607 if (right->type == XML_EXP_OR) {
6608 /* Ordering in the tree */
6609 /* C | (A | B) -> A | (B | C) */
6610 if (left->key > right->exp_right->key) {
6611 xmlExpNodePtr tmp;
6612 right->exp_right->ref++;
6613 tmp = xmlExpHashGetEntry(ctxt, XML_EXP_OR, right->exp_right,
6614 left, NULL, 0, 0);
6615 right->exp_left->ref++;
6616 tmp = xmlExpHashGetEntry(ctxt, XML_EXP_OR, right->exp_left,
6617 tmp, NULL, 0, 0);
6618 xmlExpFree(ctxt, right);
6619 return(tmp);
6620 }
6621 /* Ordering in the tree */
6622 /* B | (A | C) -> A | (B | C) */
6623 if (left->key > right->exp_left->key) {
6624 xmlExpNodePtr tmp;
6625 right->exp_right->ref++;
6626 tmp = xmlExpHashGetEntry(ctxt, XML_EXP_OR, left,
6627 right->exp_right, NULL, 0, 0);
6628 right->exp_left->ref++;
6629 tmp = xmlExpHashGetEntry(ctxt, XML_EXP_OR, right->exp_left,
6630 tmp, NULL, 0, 0);
6631 xmlExpFree(ctxt, right);
6632 return(tmp);
6633 }
6634 }
6635 /* we know both types are != XML_EXP_OR here */
6636 else if (left->key > right->key) {
6637 xmlExpNodePtr tmp = left;
6638 left = right;
6639 right = tmp;
6640 }
6641 kbase = xmlExpHashComputeKey(type, left, right);
6642 } else if (type == XML_EXP_SEQ) {
6643 /* Forbid reduction rules */
6644 if (left->type == XML_EXP_FORBID) {
6645 xmlExpFree(ctxt, right);
6646 return(left);
6647 }
6648 if (right->type == XML_EXP_FORBID) {
6649 xmlExpFree(ctxt, left);
6650 return(right);
6651 }
6652 /* Empty reduction rules */
6653 if (right->type == XML_EXP_EMPTY) {
6654 return(left);
6655 }
6656 if (left->type == XML_EXP_EMPTY) {
6657 return(right);
6658 }
6659 kbase = xmlExpHashComputeKey(type, left, right);
6660 } else
6661 return(NULL);
6662
6663 key = kbase % ctxt->size;
6664 if (ctxt->table[key] != NULL) {
6665 for (insert = ctxt->table[key]; insert != NULL;
6666 insert = insert->next) {
6667 if ((insert->key == kbase) &&
6668 (insert->type == type)) {
6669 if (type == XML_EXP_ATOM) {
6670 if (name == insert->exp_str) {
6671 insert->ref++;
6672 return(insert);
6673 }
6674 } else if (type == XML_EXP_COUNT) {
6675 if ((insert->exp_min == min) && (insert->exp_max == max) &&
6676 (insert->exp_left == left)) {
6677 insert->ref++;
6678 left->ref--;
6679 return(insert);
6680 }
6681 } else if ((insert->exp_left == left) &&
6682 (insert->exp_right == right)) {
6683 insert->ref++;
6684 left->ref--;
6685 right->ref--;
6686 return(insert);
6687 }
6688 }
6689 }
6690 }
6691
6692 entry = xmlExpNewNode(ctxt, type);
6693 if (entry == NULL)
6694 return(NULL);
6695 entry->key = kbase;
6696 if (type == XML_EXP_ATOM) {
6697 entry->exp_str = name;
6698 entry->c_max = 1;
6699 } else if (type == XML_EXP_COUNT) {
6700 entry->exp_min = min;
6701 entry->exp_max = max;
6702 entry->exp_left = left;
6703 if ((min == 0) || (IS_NILLABLE(left)))
6704 entry->info |= XML_EXP_NILABLE;
6705 if (max < 0)
6706 entry->c_max = -1;
6707 else
6708 entry->c_max = max * entry->exp_left->c_max;
6709 } else {
6710 entry->exp_left = left;
6711 entry->exp_right = right;
6712 if (type == XML_EXP_OR) {
6713 if ((IS_NILLABLE(left)) || (IS_NILLABLE(right)))
6714 entry->info |= XML_EXP_NILABLE;
6715 if ((entry->exp_left->c_max == -1) ||
6716 (entry->exp_right->c_max == -1))
6717 entry->c_max = -1;
6718 else if (entry->exp_left->c_max > entry->exp_right->c_max)
6719 entry->c_max = entry->exp_left->c_max;
6720 else
6721 entry->c_max = entry->exp_right->c_max;
6722 } else {
6723 if ((IS_NILLABLE(left)) && (IS_NILLABLE(right)))
6724 entry->info |= XML_EXP_NILABLE;
6725 if ((entry->exp_left->c_max == -1) ||
6726 (entry->exp_right->c_max == -1))
6727 entry->c_max = -1;
6728 else
6729 entry->c_max = entry->exp_left->c_max + entry->exp_right->c_max;
6730 }
6731 }
6732 entry->ref = 1;
6733 if (ctxt->table[key] != NULL)
6734 entry->next = ctxt->table[key];
6735
6736 ctxt->table[key] = entry;
6737 ctxt->nbElems++;
6738
6739 return(entry);
6740}
6741
6742/**
6743 * xmlExpFree:
6744 * @ctxt: the expression context
6745 * @exp: the expression
6746 *
6747 * Dereference the expression
6748 */
6749void
6750xmlExpFree(xmlExpCtxtPtr ctxt, xmlExpNodePtr exp) {
6751 if ((exp == NULL) || (exp == forbiddenExp) || (exp == emptyExp))
6752 return;
6753 exp->ref--;
Daniel Veillard81a8ec62005-08-22 00:20:58 +00006754 if (exp->ref == 0) {
6755 unsigned short key;
6756
6757 /* Unlink it first from the hash table */
6758 key = exp->key % ctxt->size;
6759 if (ctxt->table[key] == exp) {
6760 ctxt->table[key] = exp->next;
6761 } else {
6762 xmlExpNodePtr tmp;
6763
6764 tmp = ctxt->table[key];
6765 while (tmp != NULL) {
6766 if (tmp->next == exp) {
6767 tmp->next = exp->next;
6768 break;
6769 }
6770 tmp = tmp->next;
6771 }
6772 }
6773
6774 if ((exp->type == XML_EXP_SEQ) || (exp->type == XML_EXP_OR)) {
6775 xmlExpFree(ctxt, exp->exp_left);
6776 xmlExpFree(ctxt, exp->exp_right);
6777 } else if (exp->type == XML_EXP_COUNT) {
6778 xmlExpFree(ctxt, exp->exp_left);
6779 }
6780 xmlFree(exp);
Daniel Veillard81a8ec62005-08-22 00:20:58 +00006781 ctxt->nb_nodes--;
6782 }
6783}
6784
6785/**
6786 * xmlExpRef:
6787 * @exp: the expression
6788 *
6789 * Increase the reference count of the expression
6790 */
6791void
6792xmlExpRef(xmlExpNodePtr exp) {
6793 if (exp != NULL)
6794 exp->ref++;
6795}
6796
Daniel Veillardccb4d412005-08-23 13:41:17 +00006797/**
6798 * xmlExpNewAtom:
6799 * @ctxt: the expression context
6800 * @name: the atom name
6801 * @len: the atom name lenght in byte (or -1);
6802 *
6803 * Get the atom associated to this name from that context
6804 *
6805 * Returns the node or NULL in case of error
6806 */
6807xmlExpNodePtr
6808xmlExpNewAtom(xmlExpCtxtPtr ctxt, const xmlChar *name, int len) {
6809 if ((ctxt == NULL) || (name == NULL))
6810 return(NULL);
6811 name = xmlDictLookup(ctxt->dict, name, len);
6812 if (name == NULL)
6813 return(NULL);
6814 return(xmlExpHashGetEntry(ctxt, XML_EXP_ATOM, NULL, NULL, name, 0, 0));
6815}
6816
6817/**
6818 * xmlExpNewOr:
6819 * @ctxt: the expression context
6820 * @left: left expression
6821 * @right: right expression
6822 *
6823 * Get the atom associated to the choice @left | @right
6824 * Note that @left and @right are consumed in the operation, to keep
6825 * an handle on them use xmlExpRef() and use xmlExpFree() to release them,
6826 * this is true even in case of failure (unless ctxt == NULL).
6827 *
6828 * Returns the node or NULL in case of error
6829 */
6830xmlExpNodePtr
6831xmlExpNewOr(xmlExpCtxtPtr ctxt, xmlExpNodePtr left, xmlExpNodePtr right) {
Daniel Veillard11ce4002006-03-10 00:36:23 +00006832 if (ctxt == NULL)
6833 return(NULL);
6834 if ((left == NULL) || (right == NULL)) {
Daniel Veillardccb4d412005-08-23 13:41:17 +00006835 xmlExpFree(ctxt, left);
6836 xmlExpFree(ctxt, right);
6837 return(NULL);
6838 }
6839 return(xmlExpHashGetEntry(ctxt, XML_EXP_OR, left, right, NULL, 0, 0));
6840}
6841
6842/**
6843 * xmlExpNewSeq:
6844 * @ctxt: the expression context
6845 * @left: left expression
6846 * @right: right expression
6847 *
6848 * Get the atom associated to the sequence @left , @right
6849 * Note that @left and @right are consumed in the operation, to keep
6850 * an handle on them use xmlExpRef() and use xmlExpFree() to release them,
6851 * this is true even in case of failure (unless ctxt == NULL).
6852 *
6853 * Returns the node or NULL in case of error
6854 */
6855xmlExpNodePtr
6856xmlExpNewSeq(xmlExpCtxtPtr ctxt, xmlExpNodePtr left, xmlExpNodePtr right) {
Daniel Veillard11ce4002006-03-10 00:36:23 +00006857 if (ctxt == NULL)
6858 return(NULL);
6859 if ((left == NULL) || (right == NULL)) {
Daniel Veillardccb4d412005-08-23 13:41:17 +00006860 xmlExpFree(ctxt, left);
6861 xmlExpFree(ctxt, right);
6862 return(NULL);
6863 }
6864 return(xmlExpHashGetEntry(ctxt, XML_EXP_SEQ, left, right, NULL, 0, 0));
6865}
6866
6867/**
6868 * xmlExpNewRange:
6869 * @ctxt: the expression context
6870 * @subset: the expression to be repeated
6871 * @min: the lower bound for the repetition
6872 * @max: the upper bound for the repetition, -1 means infinite
6873 *
6874 * Get the atom associated to the range (@subset){@min, @max}
6875 * Note that @subset is consumed in the operation, to keep
6876 * an handle on it use xmlExpRef() and use xmlExpFree() to release it,
6877 * this is true even in case of failure (unless ctxt == NULL).
6878 *
6879 * Returns the node or NULL in case of error
6880 */
6881xmlExpNodePtr
6882xmlExpNewRange(xmlExpCtxtPtr ctxt, xmlExpNodePtr subset, int min, int max) {
Daniel Veillard11ce4002006-03-10 00:36:23 +00006883 if (ctxt == NULL)
6884 return(NULL);
6885 if ((subset == NULL) || (min < 0) || (max < -1) ||
Daniel Veillardccb4d412005-08-23 13:41:17 +00006886 ((max >= 0) && (min > max))) {
6887 xmlExpFree(ctxt, subset);
6888 return(NULL);
6889 }
6890 return(xmlExpHashGetEntry(ctxt, XML_EXP_COUNT, subset,
6891 NULL, NULL, min, max));
6892}
6893
Daniel Veillard81a8ec62005-08-22 00:20:58 +00006894/************************************************************************
6895 * *
6896 * Public API for operations on expressions *
6897 * *
6898 ************************************************************************/
6899
6900static int
6901xmlExpGetLanguageInt(xmlExpCtxtPtr ctxt, xmlExpNodePtr exp,
6902 const xmlChar**list, int len, int nb) {
6903 int tmp, tmp2;
6904tail:
6905 switch (exp->type) {
6906 case XML_EXP_EMPTY:
6907 return(0);
6908 case XML_EXP_ATOM:
6909 for (tmp = 0;tmp < nb;tmp++)
6910 if (list[tmp] == exp->exp_str)
6911 return(0);
6912 if (nb >= len)
6913 return(-2);
6914 list[nb++] = exp->exp_str;
6915 return(1);
6916 case XML_EXP_COUNT:
6917 exp = exp->exp_left;
6918 goto tail;
6919 case XML_EXP_SEQ:
6920 case XML_EXP_OR:
6921 tmp = xmlExpGetLanguageInt(ctxt, exp->exp_left, list, len, nb);
6922 if (tmp < 0)
6923 return(tmp);
6924 tmp2 = xmlExpGetLanguageInt(ctxt, exp->exp_right, list, len,
6925 nb + tmp);
6926 if (tmp2 < 0)
6927 return(tmp2);
6928 return(tmp + tmp2);
6929 }
6930 return(-1);
6931}
6932
6933/**
6934 * xmlExpGetLanguage:
6935 * @ctxt: the expression context
6936 * @exp: the expression
Daniel Veillard7802ba52005-10-27 11:56:20 +00006937 * @langList: where to store the tokens
Daniel Veillard81a8ec62005-08-22 00:20:58 +00006938 * @len: the allocated lenght of @list
6939 *
6940 * Find all the strings used in @exp and store them in @list
6941 *
6942 * Returns the number of unique strings found, -1 in case of errors and
6943 * -2 if there is more than @len strings
6944 */
6945int
6946xmlExpGetLanguage(xmlExpCtxtPtr ctxt, xmlExpNodePtr exp,
Daniel Veillard7802ba52005-10-27 11:56:20 +00006947 const xmlChar**langList, int len) {
6948 if ((ctxt == NULL) || (exp == NULL) || (langList == NULL) || (len <= 0))
Daniel Veillard81a8ec62005-08-22 00:20:58 +00006949 return(-1);
Daniel Veillard7802ba52005-10-27 11:56:20 +00006950 return(xmlExpGetLanguageInt(ctxt, exp, langList, len, 0));
Daniel Veillard81a8ec62005-08-22 00:20:58 +00006951}
6952
6953static int
6954xmlExpGetStartInt(xmlExpCtxtPtr ctxt, xmlExpNodePtr exp,
6955 const xmlChar**list, int len, int nb) {
6956 int tmp, tmp2;
6957tail:
6958 switch (exp->type) {
6959 case XML_EXP_FORBID:
6960 return(0);
6961 case XML_EXP_EMPTY:
6962 return(0);
6963 case XML_EXP_ATOM:
6964 for (tmp = 0;tmp < nb;tmp++)
6965 if (list[tmp] == exp->exp_str)
6966 return(0);
6967 if (nb >= len)
6968 return(-2);
6969 list[nb++] = exp->exp_str;
6970 return(1);
6971 case XML_EXP_COUNT:
6972 exp = exp->exp_left;
6973 goto tail;
6974 case XML_EXP_SEQ:
6975 tmp = xmlExpGetStartInt(ctxt, exp->exp_left, list, len, nb);
6976 if (tmp < 0)
6977 return(tmp);
6978 if (IS_NILLABLE(exp->exp_left)) {
6979 tmp2 = xmlExpGetStartInt(ctxt, exp->exp_right, list, len,
6980 nb + tmp);
6981 if (tmp2 < 0)
6982 return(tmp2);
6983 tmp += tmp2;
6984 }
6985 return(tmp);
6986 case XML_EXP_OR:
6987 tmp = xmlExpGetStartInt(ctxt, exp->exp_left, list, len, nb);
6988 if (tmp < 0)
6989 return(tmp);
6990 tmp2 = xmlExpGetStartInt(ctxt, exp->exp_right, list, len,
6991 nb + tmp);
6992 if (tmp2 < 0)
6993 return(tmp2);
6994 return(tmp + tmp2);
6995 }
6996 return(-1);
6997}
6998
6999/**
7000 * xmlExpGetStart:
7001 * @ctxt: the expression context
7002 * @exp: the expression
Daniel Veillard7802ba52005-10-27 11:56:20 +00007003 * @tokList: where to store the tokens
Daniel Veillard81a8ec62005-08-22 00:20:58 +00007004 * @len: the allocated lenght of @list
7005 *
7006 * Find all the strings that appears at the start of the languages
7007 * accepted by @exp and store them in @list. E.g. for (a, b) | c
7008 * it will return the list [a, c]
7009 *
7010 * Returns the number of unique strings found, -1 in case of errors and
7011 * -2 if there is more than @len strings
7012 */
7013int
7014xmlExpGetStart(xmlExpCtxtPtr ctxt, xmlExpNodePtr exp,
Daniel Veillard7802ba52005-10-27 11:56:20 +00007015 const xmlChar**tokList, int len) {
7016 if ((ctxt == NULL) || (exp == NULL) || (tokList == NULL) || (len <= 0))
Daniel Veillard81a8ec62005-08-22 00:20:58 +00007017 return(-1);
Daniel Veillard7802ba52005-10-27 11:56:20 +00007018 return(xmlExpGetStartInt(ctxt, exp, tokList, len, 0));
Daniel Veillard81a8ec62005-08-22 00:20:58 +00007019}
7020
7021/**
7022 * xmlExpIsNillable:
7023 * @exp: the expression
7024 *
7025 * Finds if the expression is nillable, i.e. if it accepts the empty sequqnce
7026 *
7027 * Returns 1 if nillable, 0 if not and -1 in case of error
7028 */
7029int
7030xmlExpIsNillable(xmlExpNodePtr exp) {
7031 if (exp == NULL)
7032 return(-1);
7033 return(IS_NILLABLE(exp) != 0);
7034}
7035
7036static xmlExpNodePtr
7037xmlExpStringDeriveInt(xmlExpCtxtPtr ctxt, xmlExpNodePtr exp, const xmlChar *str)
7038{
7039 xmlExpNodePtr ret;
7040
7041 switch (exp->type) {
7042 case XML_EXP_EMPTY:
7043 return(forbiddenExp);
7044 case XML_EXP_FORBID:
7045 return(forbiddenExp);
7046 case XML_EXP_ATOM:
7047 if (exp->exp_str == str) {
7048#ifdef DEBUG_DERIV
7049 printf("deriv atom: equal => Empty\n");
7050#endif
7051 ret = emptyExp;
7052 } else {
7053#ifdef DEBUG_DERIV
7054 printf("deriv atom: mismatch => forbid\n");
7055#endif
7056 /* TODO wildcards here */
7057 ret = forbiddenExp;
7058 }
7059 return(ret);
7060 case XML_EXP_OR: {
7061 xmlExpNodePtr tmp;
7062
7063#ifdef DEBUG_DERIV
7064 printf("deriv or: => or(derivs)\n");
7065#endif
7066 tmp = xmlExpStringDeriveInt(ctxt, exp->exp_left, str);
7067 if (tmp == NULL) {
7068 return(NULL);
7069 }
7070 ret = xmlExpStringDeriveInt(ctxt, exp->exp_right, str);
7071 if (ret == NULL) {
7072 xmlExpFree(ctxt, tmp);
7073 return(NULL);
7074 }
7075 ret = xmlExpHashGetEntry(ctxt, XML_EXP_OR, tmp, ret,
7076 NULL, 0, 0);
7077 return(ret);
7078 }
7079 case XML_EXP_SEQ:
7080#ifdef DEBUG_DERIV
7081 printf("deriv seq: starting with left\n");
7082#endif
7083 ret = xmlExpStringDeriveInt(ctxt, exp->exp_left, str);
7084 if (ret == NULL) {
7085 return(NULL);
7086 } else if (ret == forbiddenExp) {
7087 if (IS_NILLABLE(exp->exp_left)) {
7088#ifdef DEBUG_DERIV
7089 printf("deriv seq: left failed but nillable\n");
7090#endif
7091 ret = xmlExpStringDeriveInt(ctxt, exp->exp_right, str);
7092 }
7093 } else {
7094#ifdef DEBUG_DERIV
7095 printf("deriv seq: left match => sequence\n");
7096#endif
7097 exp->exp_right->ref++;
7098 ret = xmlExpHashGetEntry(ctxt, XML_EXP_SEQ, ret, exp->exp_right,
7099 NULL, 0, 0);
7100 }
7101 return(ret);
7102 case XML_EXP_COUNT: {
7103 int min, max;
7104 xmlExpNodePtr tmp;
7105
7106 if (exp->exp_max == 0)
7107 return(forbiddenExp);
7108 ret = xmlExpStringDeriveInt(ctxt, exp->exp_left, str);
7109 if (ret == NULL)
7110 return(NULL);
7111 if (ret == forbiddenExp) {
7112#ifdef DEBUG_DERIV
7113 printf("deriv count: pattern mismatch => forbid\n");
7114#endif
7115 return(ret);
7116 }
7117 if (exp->exp_max == 1)
7118 return(ret);
7119 if (exp->exp_max < 0) /* unbounded */
7120 max = -1;
7121 else
7122 max = exp->exp_max - 1;
7123 if (exp->exp_min > 0)
7124 min = exp->exp_min - 1;
7125 else
7126 min = 0;
7127 exp->exp_left->ref++;
7128 tmp = xmlExpHashGetEntry(ctxt, XML_EXP_COUNT, exp->exp_left, NULL,
7129 NULL, min, max);
7130 if (ret == emptyExp) {
7131#ifdef DEBUG_DERIV
7132 printf("deriv count: match to empty => new count\n");
7133#endif
7134 return(tmp);
7135 }
7136#ifdef DEBUG_DERIV
7137 printf("deriv count: match => sequence with new count\n");
7138#endif
7139 return(xmlExpHashGetEntry(ctxt, XML_EXP_SEQ, ret, tmp,
7140 NULL, 0, 0));
7141 }
7142 }
7143 return(NULL);
7144}
7145
7146/**
7147 * xmlExpStringDerive:
7148 * @ctxt: the expression context
7149 * @exp: the expression
7150 * @str: the string
7151 * @len: the string len in bytes if available
7152 *
7153 * Do one step of Brzozowski derivation of the expression @exp with
7154 * respect to the input string
7155 *
7156 * Returns the resulting expression or NULL in case of internal error
7157 */
7158xmlExpNodePtr
7159xmlExpStringDerive(xmlExpCtxtPtr ctxt, xmlExpNodePtr exp,
7160 const xmlChar *str, int len) {
7161 const xmlChar *input;
7162
7163 if ((exp == NULL) || (ctxt == NULL) || (str == NULL)) {
7164 return(NULL);
7165 }
7166 /*
7167 * check the string is in the dictionnary, if yes use an interned
7168 * copy, otherwise we know it's not an acceptable input
7169 */
7170 input = xmlDictExists(ctxt->dict, str, len);
7171 if (input == NULL) {
7172 return(forbiddenExp);
7173 }
7174 return(xmlExpStringDeriveInt(ctxt, exp, input));
7175}
7176
7177static int
7178xmlExpCheckCard(xmlExpNodePtr exp, xmlExpNodePtr sub) {
7179 int ret = 1;
7180
7181 if (sub->c_max == -1) {
7182 if (exp->c_max != -1)
7183 ret = 0;
7184 } else if ((exp->c_max >= 0) && (exp->c_max < sub->c_max)) {
7185 ret = 0;
7186 }
7187#if 0
7188 if ((IS_NILLABLE(sub)) && (!IS_NILLABLE(exp)))
7189 ret = 0;
7190#endif
7191 return(ret);
7192}
7193
7194static xmlExpNodePtr xmlExpExpDeriveInt(xmlExpCtxtPtr ctxt, xmlExpNodePtr exp,
7195 xmlExpNodePtr sub);
7196/**
7197 * xmlExpDivide:
7198 * @ctxt: the expressions context
7199 * @exp: the englobing expression
7200 * @sub: the subexpression
7201 * @mult: the multiple expression
7202 * @remain: the remain from the derivation of the multiple
7203 *
7204 * Check if exp is a multiple of sub, i.e. if there is a finite number n
7205 * so that sub{n} subsume exp
7206 *
7207 * Returns the multiple value if successful, 0 if it is not a multiple
7208 * and -1 in case of internel error.
7209 */
7210
7211static int
7212xmlExpDivide(xmlExpCtxtPtr ctxt, xmlExpNodePtr exp, xmlExpNodePtr sub,
7213 xmlExpNodePtr *mult, xmlExpNodePtr *remain) {
7214 int i;
7215 xmlExpNodePtr tmp, tmp2;
7216
7217 if (mult != NULL) *mult = NULL;
7218 if (remain != NULL) *remain = NULL;
7219 if (exp->c_max == -1) return(0);
7220 if (IS_NILLABLE(exp) && (!IS_NILLABLE(sub))) return(0);
7221
7222 for (i = 1;i <= exp->c_max;i++) {
7223 sub->ref++;
7224 tmp = xmlExpHashGetEntry(ctxt, XML_EXP_COUNT,
7225 sub, NULL, NULL, i, i);
7226 if (tmp == NULL) {
7227 return(-1);
7228 }
7229 if (!xmlExpCheckCard(tmp, exp)) {
7230 xmlExpFree(ctxt, tmp);
7231 continue;
7232 }
7233 tmp2 = xmlExpExpDeriveInt(ctxt, tmp, exp);
7234 if (tmp2 == NULL) {
7235 xmlExpFree(ctxt, tmp);
7236 return(-1);
7237 }
7238 if ((tmp2 != forbiddenExp) && (IS_NILLABLE(tmp2))) {
7239 if (remain != NULL)
7240 *remain = tmp2;
7241 else
7242 xmlExpFree(ctxt, tmp2);
7243 if (mult != NULL)
7244 *mult = tmp;
7245 else
7246 xmlExpFree(ctxt, tmp);
7247#ifdef DEBUG_DERIV
7248 printf("Divide succeeded %d\n", i);
7249#endif
7250 return(i);
7251 }
7252 xmlExpFree(ctxt, tmp);
7253 xmlExpFree(ctxt, tmp2);
7254 }
7255#ifdef DEBUG_DERIV
7256 printf("Divide failed\n");
7257#endif
7258 return(0);
7259}
7260
7261/**
7262 * xmlExpExpDeriveInt:
7263 * @ctxt: the expressions context
7264 * @exp: the englobing expression
7265 * @sub: the subexpression
7266 *
7267 * Try to do a step of Brzozowski derivation but at a higher level
7268 * the input being a subexpression.
7269 *
7270 * Returns the resulting expression or NULL in case of internal error
7271 */
7272static xmlExpNodePtr
7273xmlExpExpDeriveInt(xmlExpCtxtPtr ctxt, xmlExpNodePtr exp, xmlExpNodePtr sub) {
7274 xmlExpNodePtr ret, tmp, tmp2, tmp3;
7275 const xmlChar **tab;
7276 int len, i;
7277
7278 /*
7279 * In case of equality and if the expression can only consume a finite
7280 * amount, then the derivation is empty
7281 */
7282 if ((exp == sub) && (exp->c_max >= 0)) {
7283#ifdef DEBUG_DERIV
7284 printf("Equal(exp, sub) and finite -> Empty\n");
7285#endif
7286 return(emptyExp);
7287 }
7288 /*
7289 * decompose sub sequence first
7290 */
7291 if (sub->type == XML_EXP_EMPTY) {
7292#ifdef DEBUG_DERIV
7293 printf("Empty(sub) -> Empty\n");
7294#endif
7295 exp->ref++;
7296 return(exp);
7297 }
7298 if (sub->type == XML_EXP_SEQ) {
7299#ifdef DEBUG_DERIV
7300 printf("Seq(sub) -> decompose\n");
7301#endif
7302 tmp = xmlExpExpDeriveInt(ctxt, exp, sub->exp_left);
7303 if (tmp == NULL)
7304 return(NULL);
7305 if (tmp == forbiddenExp)
7306 return(tmp);
7307 ret = xmlExpExpDeriveInt(ctxt, tmp, sub->exp_right);
7308 xmlExpFree(ctxt, tmp);
7309 return(ret);
7310 }
7311 if (sub->type == XML_EXP_OR) {
7312#ifdef DEBUG_DERIV
7313 printf("Or(sub) -> decompose\n");
7314#endif
7315 tmp = xmlExpExpDeriveInt(ctxt, exp, sub->exp_left);
7316 if (tmp == forbiddenExp)
7317 return(tmp);
7318 if (tmp == NULL)
7319 return(NULL);
7320 ret = xmlExpExpDeriveInt(ctxt, exp, sub->exp_right);
7321 if ((ret == NULL) || (ret == forbiddenExp)) {
7322 xmlExpFree(ctxt, tmp);
7323 return(ret);
7324 }
7325 return(xmlExpHashGetEntry(ctxt, XML_EXP_OR, tmp, ret, NULL, 0, 0));
7326 }
7327 if (!xmlExpCheckCard(exp, sub)) {
7328#ifdef DEBUG_DERIV
7329 printf("CheckCard(exp, sub) failed -> Forbid\n");
7330#endif
7331 return(forbiddenExp);
7332 }
7333 switch (exp->type) {
7334 case XML_EXP_EMPTY:
7335 if (sub == emptyExp)
7336 return(emptyExp);
7337#ifdef DEBUG_DERIV
7338 printf("Empty(exp) -> Forbid\n");
7339#endif
7340 return(forbiddenExp);
7341 case XML_EXP_FORBID:
7342#ifdef DEBUG_DERIV
7343 printf("Forbid(exp) -> Forbid\n");
7344#endif
7345 return(forbiddenExp);
7346 case XML_EXP_ATOM:
7347 if (sub->type == XML_EXP_ATOM) {
7348 /* TODO: handle wildcards */
7349 if (exp->exp_str == sub->exp_str) {
7350#ifdef DEBUG_DERIV
7351 printf("Atom match -> Empty\n");
7352#endif
7353 return(emptyExp);
7354 }
7355#ifdef DEBUG_DERIV
7356 printf("Atom mismatch -> Forbid\n");
7357#endif
7358 return(forbiddenExp);
7359 }
7360 if ((sub->type == XML_EXP_COUNT) &&
7361 (sub->exp_max == 1) &&
7362 (sub->exp_left->type == XML_EXP_ATOM)) {
7363 /* TODO: handle wildcards */
7364 if (exp->exp_str == sub->exp_left->exp_str) {
7365#ifdef DEBUG_DERIV
7366 printf("Atom match -> Empty\n");
7367#endif
7368 return(emptyExp);
7369 }
7370#ifdef DEBUG_DERIV
7371 printf("Atom mismatch -> Forbid\n");
7372#endif
7373 return(forbiddenExp);
7374 }
7375#ifdef DEBUG_DERIV
7376 printf("Compex exp vs Atom -> Forbid\n");
7377#endif
7378 return(forbiddenExp);
7379 case XML_EXP_SEQ:
7380 /* try to get the sequence consumed only if possible */
7381 if (xmlExpCheckCard(exp->exp_left, sub)) {
7382 /* See if the sequence can be consumed directly */
7383#ifdef DEBUG_DERIV
7384 printf("Seq trying left only\n");
7385#endif
7386 ret = xmlExpExpDeriveInt(ctxt, exp->exp_left, sub);
7387 if ((ret != forbiddenExp) && (ret != NULL)) {
7388#ifdef DEBUG_DERIV
7389 printf("Seq trying left only worked\n");
7390#endif
7391 /*
7392 * TODO: assumption here that we are determinist
7393 * i.e. we won't get to a nillable exp left
7394 * subset which could be matched by the right
7395 * part too.
7396 * e.g.: (a | b)+,(a | c) and 'a+,a'
7397 */
7398 exp->exp_right->ref++;
7399 return(xmlExpHashGetEntry(ctxt, XML_EXP_SEQ, ret,
7400 exp->exp_right, NULL, 0, 0));
7401 }
7402#ifdef DEBUG_DERIV
7403 } else {
7404 printf("Seq: left too short\n");
7405#endif
7406 }
7407 /* Try instead to decompose */
7408 if (sub->type == XML_EXP_COUNT) {
7409 int min, max;
7410
7411#ifdef DEBUG_DERIV
7412 printf("Seq: sub is a count\n");
7413#endif
7414 ret = xmlExpExpDeriveInt(ctxt, exp->exp_left, sub->exp_left);
7415 if (ret == NULL)
7416 return(NULL);
7417 if (ret != forbiddenExp) {
7418#ifdef DEBUG_DERIV
7419 printf("Seq , Count match on left\n");
7420#endif
7421 if (sub->exp_max < 0)
7422 max = -1;
7423 else
7424 max = sub->exp_max -1;
7425 if (sub->exp_min > 0)
7426 min = sub->exp_min -1;
7427 else
7428 min = 0;
7429 exp->exp_right->ref++;
7430 tmp = xmlExpHashGetEntry(ctxt, XML_EXP_SEQ, ret,
7431 exp->exp_right, NULL, 0, 0);
7432 if (tmp == NULL)
7433 return(NULL);
7434
7435 sub->exp_left->ref++;
7436 tmp2 = xmlExpHashGetEntry(ctxt, XML_EXP_COUNT,
7437 sub->exp_left, NULL, NULL, min, max);
7438 if (tmp2 == NULL) {
7439 xmlExpFree(ctxt, tmp);
7440 return(NULL);
7441 }
7442 ret = xmlExpExpDeriveInt(ctxt, tmp, tmp2);
7443 xmlExpFree(ctxt, tmp);
7444 xmlExpFree(ctxt, tmp2);
7445 return(ret);
7446 }
7447 }
7448 /* we made no progress on structured operations */
7449 break;
7450 case XML_EXP_OR:
7451#ifdef DEBUG_DERIV
7452 printf("Or , trying both side\n");
7453#endif
7454 ret = xmlExpExpDeriveInt(ctxt, exp->exp_left, sub);
7455 if (ret == NULL)
7456 return(NULL);
7457 tmp = xmlExpExpDeriveInt(ctxt, exp->exp_right, sub);
7458 if (tmp == NULL) {
7459 xmlExpFree(ctxt, ret);
7460 return(NULL);
7461 }
7462 return(xmlExpHashGetEntry(ctxt, XML_EXP_OR, ret, tmp, NULL, 0, 0));
7463 case XML_EXP_COUNT: {
7464 int min, max;
7465
7466 if (sub->type == XML_EXP_COUNT) {
7467 /*
7468 * Try to see if the loop is completely subsumed
7469 */
7470 tmp = xmlExpExpDeriveInt(ctxt, exp->exp_left, sub->exp_left);
7471 if (tmp == NULL)
7472 return(NULL);
7473 if (tmp == forbiddenExp) {
7474 int mult;
7475
7476#ifdef DEBUG_DERIV
7477 printf("Count, Count inner don't subsume\n");
7478#endif
7479 mult = xmlExpDivide(ctxt, sub->exp_left, exp->exp_left,
7480 NULL, &tmp);
7481 if (mult <= 0) {
7482#ifdef DEBUG_DERIV
7483 printf("Count, Count not multiple => forbidden\n");
7484#endif
7485 return(forbiddenExp);
7486 }
7487 if (sub->exp_max == -1) {
7488 max = -1;
7489 if (exp->exp_max == -1) {
7490 if (exp->exp_min <= sub->exp_min * mult)
7491 min = 0;
7492 else
7493 min = exp->exp_min - sub->exp_min * mult;
7494 } else {
7495#ifdef DEBUG_DERIV
7496 printf("Count, Count finite can't subsume infinite\n");
7497#endif
7498 xmlExpFree(ctxt, tmp);
7499 return(forbiddenExp);
7500 }
7501 } else {
7502 if (exp->exp_max == -1) {
7503#ifdef DEBUG_DERIV
7504 printf("Infinite loop consume mult finite loop\n");
7505#endif
7506 if (exp->exp_min > sub->exp_min * mult) {
7507 max = -1;
7508 min = exp->exp_min - sub->exp_min * mult;
7509 } else {
7510 max = -1;
7511 min = 0;
7512 }
7513 } else {
7514 if (exp->exp_max < sub->exp_max * mult) {
7515#ifdef DEBUG_DERIV
7516 printf("loops max mult mismatch => forbidden\n");
7517#endif
7518 xmlExpFree(ctxt, tmp);
7519 return(forbiddenExp);
7520 }
7521 if (sub->exp_max * mult > exp->exp_min)
7522 min = 0;
7523 else
7524 min = exp->exp_min - sub->exp_max * mult;
7525 max = exp->exp_max - sub->exp_max * mult;
7526 }
7527 }
7528 } else if (!IS_NILLABLE(tmp)) {
7529 /*
7530 * TODO: loop here to try to grow if working on finite
7531 * blocks.
7532 */
7533#ifdef DEBUG_DERIV
7534 printf("Count, Count remain not nillable => forbidden\n");
7535#endif
7536 xmlExpFree(ctxt, tmp);
7537 return(forbiddenExp);
7538 } else if (sub->exp_max == -1) {
7539 if (exp->exp_max == -1) {
7540 if (exp->exp_min <= sub->exp_min) {
7541#ifdef DEBUG_DERIV
7542 printf("Infinite loops Okay => COUNT(0,Inf)\n");
7543#endif
7544 max = -1;
7545 min = 0;
7546 } else {
7547#ifdef DEBUG_DERIV
7548 printf("Infinite loops min => Count(X,Inf)\n");
7549#endif
7550 max = -1;
7551 min = exp->exp_min - sub->exp_min;
7552 }
7553 } else if (exp->exp_min > sub->exp_min) {
7554#ifdef DEBUG_DERIV
7555 printf("loops min mismatch 1 => forbidden ???\n");
7556#endif
7557 xmlExpFree(ctxt, tmp);
7558 return(forbiddenExp);
7559 } else {
7560 max = -1;
7561 min = 0;
7562 }
7563 } else {
7564 if (exp->exp_max == -1) {
7565#ifdef DEBUG_DERIV
7566 printf("Infinite loop consume finite loop\n");
7567#endif
7568 if (exp->exp_min > sub->exp_min) {
7569 max = -1;
7570 min = exp->exp_min - sub->exp_min;
7571 } else {
7572 max = -1;
7573 min = 0;
7574 }
7575 } else {
7576 if (exp->exp_max < sub->exp_max) {
7577#ifdef DEBUG_DERIV
7578 printf("loops max mismatch => forbidden\n");
7579#endif
7580 xmlExpFree(ctxt, tmp);
7581 return(forbiddenExp);
7582 }
7583 if (sub->exp_max > exp->exp_min)
7584 min = 0;
7585 else
7586 min = exp->exp_min - sub->exp_max;
7587 max = exp->exp_max - sub->exp_max;
7588 }
7589 }
7590#ifdef DEBUG_DERIV
7591 printf("loops match => SEQ(COUNT())\n");
7592#endif
7593 exp->exp_left->ref++;
7594 tmp2 = xmlExpHashGetEntry(ctxt, XML_EXP_COUNT, exp->exp_left,
7595 NULL, NULL, min, max);
7596 if (tmp2 == NULL) {
7597 return(NULL);
7598 }
7599 ret = xmlExpHashGetEntry(ctxt, XML_EXP_SEQ, tmp, tmp2,
7600 NULL, 0, 0);
7601 return(ret);
7602 }
7603 tmp = xmlExpExpDeriveInt(ctxt, exp->exp_left, sub);
7604 if (tmp == NULL)
7605 return(NULL);
7606 if (tmp == forbiddenExp) {
7607#ifdef DEBUG_DERIV
7608 printf("loop mismatch => forbidden\n");
7609#endif
7610 return(forbiddenExp);
7611 }
7612 if (exp->exp_min > 0)
7613 min = exp->exp_min - 1;
7614 else
7615 min = 0;
7616 if (exp->exp_max < 0)
7617 max = -1;
7618 else
7619 max = exp->exp_max - 1;
7620
7621#ifdef DEBUG_DERIV
7622 printf("loop match => SEQ(COUNT())\n");
7623#endif
7624 exp->exp_left->ref++;
7625 tmp2 = xmlExpHashGetEntry(ctxt, XML_EXP_COUNT, exp->exp_left,
7626 NULL, NULL, min, max);
7627 if (tmp2 == NULL)
7628 return(NULL);
7629 ret = xmlExpHashGetEntry(ctxt, XML_EXP_SEQ, tmp, tmp2,
7630 NULL, 0, 0);
7631 return(ret);
7632 }
7633 }
7634
Daniel Veillardccb4d412005-08-23 13:41:17 +00007635#ifdef DEBUG_DERIV
7636 printf("Fallback to derivative\n");
7637#endif
7638 if (IS_NILLABLE(sub)) {
7639 if (!(IS_NILLABLE(exp)))
7640 return(forbiddenExp);
7641 else
7642 ret = emptyExp;
7643 } else
7644 ret = NULL;
Daniel Veillard81a8ec62005-08-22 00:20:58 +00007645 /*
7646 * here the structured derivation made no progress so
7647 * we use the default token based derivation to force one more step
7648 */
7649 if (ctxt->tabSize == 0)
7650 ctxt->tabSize = 40;
7651
7652 tab = (const xmlChar **) xmlMalloc(ctxt->tabSize *
7653 sizeof(const xmlChar *));
7654 if (tab == NULL) {
7655 return(NULL);
7656 }
7657
Daniel Veillard81a8ec62005-08-22 00:20:58 +00007658 /*
7659 * collect all the strings accepted by the subexpression on input
7660 */
7661 len = xmlExpGetStartInt(ctxt, sub, tab, ctxt->tabSize, 0);
7662 while (len < 0) {
7663 const xmlChar **temp;
Rob Richards54a8f672005-10-07 02:33:00 +00007664 temp = (const xmlChar **) xmlRealloc((xmlChar **) tab, ctxt->tabSize * 2 *
Daniel Veillard81a8ec62005-08-22 00:20:58 +00007665 sizeof(const xmlChar *));
7666 if (temp == NULL) {
Rob Richards54a8f672005-10-07 02:33:00 +00007667 xmlFree((xmlChar **) tab);
Daniel Veillard81a8ec62005-08-22 00:20:58 +00007668 return(NULL);
7669 }
7670 tab = temp;
7671 ctxt->tabSize *= 2;
7672 len = xmlExpGetStartInt(ctxt, sub, tab, ctxt->tabSize, 0);
7673 }
Daniel Veillard81a8ec62005-08-22 00:20:58 +00007674 for (i = 0;i < len;i++) {
7675 tmp = xmlExpStringDeriveInt(ctxt, exp, tab[i]);
7676 if ((tmp == NULL) || (tmp == forbiddenExp)) {
7677 xmlExpFree(ctxt, ret);
Rob Richards54a8f672005-10-07 02:33:00 +00007678 xmlFree((xmlChar **) tab);
Daniel Veillard81a8ec62005-08-22 00:20:58 +00007679 return(tmp);
7680 }
7681 tmp2 = xmlExpStringDeriveInt(ctxt, sub, tab[i]);
7682 if ((tmp2 == NULL) || (tmp2 == forbiddenExp)) {
7683 xmlExpFree(ctxt, tmp);
7684 xmlExpFree(ctxt, ret);
Rob Richards54a8f672005-10-07 02:33:00 +00007685 xmlFree((xmlChar **) tab);
Daniel Veillard81a8ec62005-08-22 00:20:58 +00007686 return(tmp);
7687 }
7688 tmp3 = xmlExpExpDeriveInt(ctxt, tmp, tmp2);
7689 xmlExpFree(ctxt, tmp);
7690 xmlExpFree(ctxt, tmp2);
7691
7692 if ((tmp3 == NULL) || (tmp3 == forbiddenExp)) {
7693 xmlExpFree(ctxt, ret);
Rob Richards54a8f672005-10-07 02:33:00 +00007694 xmlFree((xmlChar **) tab);
Daniel Veillard81a8ec62005-08-22 00:20:58 +00007695 return(tmp3);
7696 }
7697
7698 if (ret == NULL)
7699 ret = tmp3;
7700 else {
7701 ret = xmlExpHashGetEntry(ctxt, XML_EXP_OR, ret, tmp3, NULL, 0, 0);
7702 if (ret == NULL) {
Rob Richards54a8f672005-10-07 02:33:00 +00007703 xmlFree((xmlChar **) tab);
Daniel Veillard81a8ec62005-08-22 00:20:58 +00007704 return(NULL);
7705 }
7706 }
7707 }
Rob Richards54a8f672005-10-07 02:33:00 +00007708 xmlFree((xmlChar **) tab);
Daniel Veillard81a8ec62005-08-22 00:20:58 +00007709 return(ret);
7710}
7711
7712/**
Daniel Veillard0090bd52005-08-22 14:43:43 +00007713 * xmlExpExpDerive:
7714 * @ctxt: the expressions context
7715 * @exp: the englobing expression
7716 * @sub: the subexpression
7717 *
7718 * Evaluates the expression resulting from @exp consuming a sub expression @sub
7719 * Based on algebraic derivation and sometimes direct Brzozowski derivation
7720 * it usually tatkes less than linear time and can handle expressions generating
7721 * infinite languages.
7722 *
7723 * Returns the resulting expression or NULL in case of internal error, the
7724 * result must be freed
7725 */
7726xmlExpNodePtr
7727xmlExpExpDerive(xmlExpCtxtPtr ctxt, xmlExpNodePtr exp, xmlExpNodePtr sub) {
7728 if ((exp == NULL) || (ctxt == NULL) || (sub == NULL))
7729 return(NULL);
7730
7731 /*
7732 * O(1) speedups
7733 */
7734 if (IS_NILLABLE(sub) && (!IS_NILLABLE(exp))) {
7735#ifdef DEBUG_DERIV
7736 printf("Sub nillable and not exp : can't subsume\n");
7737#endif
7738 return(forbiddenExp);
7739 }
7740 if (xmlExpCheckCard(exp, sub) == 0) {
7741#ifdef DEBUG_DERIV
7742 printf("sub generate longuer sequances than exp : can't subsume\n");
7743#endif
7744 return(forbiddenExp);
7745 }
7746 return(xmlExpExpDeriveInt(ctxt, exp, sub));
7747}
7748
7749/**
Daniel Veillard81a8ec62005-08-22 00:20:58 +00007750 * xmlExpSubsume:
7751 * @ctxt: the expressions context
7752 * @exp: the englobing expression
7753 * @sub: the subexpression
7754 *
7755 * Check whether @exp accepts all the languages accexpted by @sub
7756 * the input being a subexpression.
7757 *
7758 * Returns 1 if true 0 if false and -1 in case of failure.
7759 */
7760int
7761xmlExpSubsume(xmlExpCtxtPtr ctxt, xmlExpNodePtr exp, xmlExpNodePtr sub) {
7762 xmlExpNodePtr tmp;
7763
7764 if ((exp == NULL) || (ctxt == NULL) || (sub == NULL))
7765 return(-1);
7766
7767 /*
7768 * TODO: speedup by checking the language of sub is a subset of the
7769 * language of exp
7770 */
7771 /*
7772 * O(1) speedups
7773 */
7774 if (IS_NILLABLE(sub) && (!IS_NILLABLE(exp))) {
7775#ifdef DEBUG_DERIV
7776 printf("Sub nillable and not exp : can't subsume\n");
7777#endif
7778 return(0);
7779 }
7780 if (xmlExpCheckCard(exp, sub) == 0) {
7781#ifdef DEBUG_DERIV
7782 printf("sub generate longuer sequances than exp : can't subsume\n");
7783#endif
7784 return(0);
7785 }
7786 tmp = xmlExpExpDeriveInt(ctxt, exp, sub);
7787#ifdef DEBUG_DERIV
7788 printf("Result derivation :\n");
7789 PRINT_EXP(tmp);
7790#endif
7791 if (tmp == NULL)
7792 return(-1);
7793 if (tmp == forbiddenExp)
7794 return(0);
7795 if (tmp == emptyExp)
7796 return(1);
7797 if ((tmp != NULL) && (IS_NILLABLE(tmp))) {
7798 xmlExpFree(ctxt, tmp);
7799 return(1);
7800 }
7801 xmlExpFree(ctxt, tmp);
7802 return(0);
7803}
Daniel Veillard465a0002005-08-22 12:07:04 +00007804
7805/************************************************************************
7806 * *
7807 * Parsing expression *
7808 * *
7809 ************************************************************************/
7810
7811static xmlExpNodePtr xmlExpParseExpr(xmlExpCtxtPtr ctxt);
7812
7813#undef CUR
7814#define CUR (*ctxt->cur)
7815#undef NEXT
7816#define NEXT ctxt->cur++;
7817#undef IS_BLANK
7818#define IS_BLANK(c) ((c == ' ') || (c == '\n') || (c == '\r') || (c == '\t'))
7819#define SKIP_BLANKS while (IS_BLANK(*ctxt->cur)) ctxt->cur++;
7820
7821static int
7822xmlExpParseNumber(xmlExpCtxtPtr ctxt) {
7823 int ret = 0;
7824
7825 SKIP_BLANKS
7826 if (CUR == '*') {
7827 NEXT
7828 return(-1);
7829 }
7830 if ((CUR < '0') || (CUR > '9'))
7831 return(-1);
7832 while ((CUR >= '0') && (CUR <= '9')) {
7833 ret = ret * 10 + (CUR - '0');
7834 NEXT
7835 }
7836 return(ret);
7837}
7838
7839static xmlExpNodePtr
7840xmlExpParseOr(xmlExpCtxtPtr ctxt) {
7841 const char *base;
7842 xmlExpNodePtr ret;
7843 const xmlChar *val;
7844
7845 SKIP_BLANKS
7846 base = ctxt->cur;
7847 if (*ctxt->cur == '(') {
7848 NEXT
7849 ret = xmlExpParseExpr(ctxt);
7850 SKIP_BLANKS
7851 if (*ctxt->cur != ')') {
7852 fprintf(stderr, "unbalanced '(' : %s\n", base);
7853 xmlExpFree(ctxt, ret);
7854 return(NULL);
7855 }
7856 NEXT;
7857 SKIP_BLANKS
7858 goto parse_quantifier;
7859 }
7860 while ((CUR != 0) && (!(IS_BLANK(CUR))) && (CUR != '(') &&
7861 (CUR != ')') && (CUR != '|') && (CUR != ',') && (CUR != '{') &&
7862 (CUR != '*') && (CUR != '+') && (CUR != '?') && (CUR != '}'))
7863 NEXT;
7864 val = xmlDictLookup(ctxt->dict, BAD_CAST base, ctxt->cur - base);
7865 if (val == NULL)
7866 return(NULL);
7867 ret = xmlExpHashGetEntry(ctxt, XML_EXP_ATOM, NULL, NULL, val, 0, 0);
7868 if (ret == NULL)
7869 return(NULL);
7870 SKIP_BLANKS
7871parse_quantifier:
7872 if (CUR == '{') {
7873 int min, max;
7874
7875 NEXT
7876 min = xmlExpParseNumber(ctxt);
7877 if (min < 0) {
7878 xmlExpFree(ctxt, ret);
7879 return(NULL);
7880 }
7881 SKIP_BLANKS
7882 if (CUR == ',') {
7883 NEXT
7884 max = xmlExpParseNumber(ctxt);
7885 SKIP_BLANKS
7886 } else
7887 max = min;
7888 if (CUR != '}') {
7889 xmlExpFree(ctxt, ret);
7890 return(NULL);
7891 }
7892 NEXT
7893 ret = xmlExpHashGetEntry(ctxt, XML_EXP_COUNT, ret, NULL, NULL,
7894 min, max);
7895 SKIP_BLANKS
7896 } else if (CUR == '?') {
7897 NEXT
7898 ret = xmlExpHashGetEntry(ctxt, XML_EXP_COUNT, ret, NULL, NULL,
7899 0, 1);
7900 SKIP_BLANKS
7901 } else if (CUR == '+') {
7902 NEXT
7903 ret = xmlExpHashGetEntry(ctxt, XML_EXP_COUNT, ret, NULL, NULL,
7904 1, -1);
7905 SKIP_BLANKS
7906 } else if (CUR == '*') {
7907 NEXT
7908 ret = xmlExpHashGetEntry(ctxt, XML_EXP_COUNT, ret, NULL, NULL,
7909 0, -1);
7910 SKIP_BLANKS
7911 }
7912 return(ret);
7913}
7914
7915
7916static xmlExpNodePtr
7917xmlExpParseSeq(xmlExpCtxtPtr ctxt) {
7918 xmlExpNodePtr ret, right;
7919
7920 ret = xmlExpParseOr(ctxt);
7921 SKIP_BLANKS
7922 while (CUR == '|') {
7923 NEXT
7924 right = xmlExpParseOr(ctxt);
7925 if (right == NULL) {
7926 xmlExpFree(ctxt, ret);
7927 return(NULL);
7928 }
7929 ret = xmlExpHashGetEntry(ctxt, XML_EXP_OR, ret, right, NULL, 0, 0);
7930 if (ret == NULL)
7931 return(NULL);
7932 }
7933 return(ret);
7934}
7935
7936static xmlExpNodePtr
7937xmlExpParseExpr(xmlExpCtxtPtr ctxt) {
7938 xmlExpNodePtr ret, right;
7939
7940 ret = xmlExpParseSeq(ctxt);
7941 SKIP_BLANKS
7942 while (CUR == ',') {
7943 NEXT
7944 right = xmlExpParseSeq(ctxt);
7945 if (right == NULL) {
7946 xmlExpFree(ctxt, ret);
7947 return(NULL);
7948 }
7949 ret = xmlExpHashGetEntry(ctxt, XML_EXP_SEQ, ret, right, NULL, 0, 0);
7950 if (ret == NULL)
7951 return(NULL);
7952 }
7953 return(ret);
7954}
7955
7956/**
7957 * xmlExpParse:
7958 * @ctxt: the expressions context
7959 * @expr: the 0 terminated string
7960 *
7961 * Minimal parser for regexps, it understand the following constructs
7962 * - string terminals
7963 * - choice operator |
7964 * - sequence operator ,
7965 * - subexpressions (...)
7966 * - usual cardinality operators + * and ?
7967 * - finite sequences { min, max }
7968 * - infinite sequences { min, * }
7969 * There is minimal checkings made especially no checking on strings values
7970 *
7971 * Returns a new expression or NULL in case of failure
7972 */
7973xmlExpNodePtr
7974xmlExpParse(xmlExpCtxtPtr ctxt, const char *expr) {
7975 xmlExpNodePtr ret;
7976
7977 ctxt->expr = expr;
7978 ctxt->cur = expr;
7979
7980 ret = xmlExpParseExpr(ctxt);
7981 SKIP_BLANKS
7982 if (*ctxt->cur != 0) {
7983 xmlExpFree(ctxt, ret);
7984 return(NULL);
7985 }
7986 return(ret);
7987}
7988
7989static void
7990xmlExpDumpInt(xmlBufferPtr buf, xmlExpNodePtr expr, int glob) {
7991 xmlExpNodePtr c;
7992
7993 if (expr == NULL) return;
7994 if (glob) xmlBufferWriteChar(buf, "(");
7995 switch (expr->type) {
7996 case XML_EXP_EMPTY:
7997 xmlBufferWriteChar(buf, "empty");
7998 break;
7999 case XML_EXP_FORBID:
8000 xmlBufferWriteChar(buf, "forbidden");
8001 break;
8002 case XML_EXP_ATOM:
8003 xmlBufferWriteCHAR(buf, expr->exp_str);
8004 break;
8005 case XML_EXP_SEQ:
8006 c = expr->exp_left;
8007 if ((c->type == XML_EXP_SEQ) || (c->type == XML_EXP_OR))
8008 xmlExpDumpInt(buf, c, 1);
8009 else
8010 xmlExpDumpInt(buf, c, 0);
8011 xmlBufferWriteChar(buf, " , ");
8012 c = expr->exp_right;
8013 if ((c->type == XML_EXP_SEQ) || (c->type == XML_EXP_OR))
8014 xmlExpDumpInt(buf, c, 1);
8015 else
8016 xmlExpDumpInt(buf, c, 0);
8017 break;
8018 case XML_EXP_OR:
8019 c = expr->exp_left;
8020 if ((c->type == XML_EXP_SEQ) || (c->type == XML_EXP_OR))
8021 xmlExpDumpInt(buf, c, 1);
8022 else
8023 xmlExpDumpInt(buf, c, 0);
8024 xmlBufferWriteChar(buf, " | ");
8025 c = expr->exp_right;
8026 if ((c->type == XML_EXP_SEQ) || (c->type == XML_EXP_OR))
8027 xmlExpDumpInt(buf, c, 1);
8028 else
8029 xmlExpDumpInt(buf, c, 0);
8030 break;
8031 case XML_EXP_COUNT: {
8032 char rep[40];
8033
8034 c = expr->exp_left;
8035 if ((c->type == XML_EXP_SEQ) || (c->type == XML_EXP_OR))
8036 xmlExpDumpInt(buf, c, 1);
8037 else
8038 xmlExpDumpInt(buf, c, 0);
8039 if ((expr->exp_min == 0) && (expr->exp_max == 1)) {
8040 rep[0] = '?';
8041 rep[1] = 0;
8042 } else if ((expr->exp_min == 0) && (expr->exp_max == -1)) {
8043 rep[0] = '*';
8044 rep[1] = 0;
8045 } else if ((expr->exp_min == 1) && (expr->exp_max == -1)) {
8046 rep[0] = '+';
8047 rep[1] = 0;
8048 } else if (expr->exp_max == expr->exp_min) {
8049 snprintf(rep, 39, "{%d}", expr->exp_min);
8050 } else if (expr->exp_max < 0) {
8051 snprintf(rep, 39, "{%d,inf}", expr->exp_min);
8052 } else {
8053 snprintf(rep, 39, "{%d,%d}", expr->exp_min, expr->exp_max);
8054 }
8055 rep[39] = 0;
8056 xmlBufferWriteChar(buf, rep);
8057 break;
8058 }
8059 default:
8060 fprintf(stderr, "Error in tree\n");
8061 }
8062 if (glob)
8063 xmlBufferWriteChar(buf, ")");
8064}
8065/**
8066 * xmlExpDump:
8067 * @buf: a buffer to receive the output
8068 * @expr: the compiled expression
8069 *
8070 * Serialize the expression as compiled to the buffer
8071 */
8072void
Daniel Veillard5eee7672005-08-22 21:22:27 +00008073xmlExpDump(xmlBufferPtr buf, xmlExpNodePtr expr) {
8074 if ((buf == NULL) || (expr == NULL))
Daniel Veillard465a0002005-08-22 12:07:04 +00008075 return;
Daniel Veillard5eee7672005-08-22 21:22:27 +00008076 xmlExpDumpInt(buf, expr, 0);
Daniel Veillard465a0002005-08-22 12:07:04 +00008077}
8078
8079/**
8080 * xmlExpMaxToken:
8081 * @expr: a compiled expression
8082 *
8083 * Indicate the maximum number of input a expression can accept
8084 *
8085 * Returns the maximum length or -1 in case of error
8086 */
8087int
8088xmlExpMaxToken(xmlExpNodePtr expr) {
8089 if (expr == NULL)
8090 return(-1);
8091 return(expr->c_max);
8092}
8093
8094/**
8095 * xmlExpCtxtNbNodes:
8096 * @ctxt: an expression context
8097 *
8098 * Debugging facility provides the number of allocated nodes at a that point
8099 *
8100 * Returns the number of nodes in use or -1 in case of error
8101 */
8102int
8103xmlExpCtxtNbNodes(xmlExpCtxtPtr ctxt) {
8104 if (ctxt == NULL)
8105 return(-1);
8106 return(ctxt->nb_nodes);
8107}
8108
8109/**
8110 * xmlExpCtxtNbCons:
8111 * @ctxt: an expression context
8112 *
8113 * Debugging facility provides the number of allocated nodes over lifetime
8114 *
8115 * Returns the number of nodes ever allocated or -1 in case of error
8116 */
8117int
8118xmlExpCtxtNbCons(xmlExpCtxtPtr ctxt) {
8119 if (ctxt == NULL)
8120 return(-1);
8121 return(ctxt->nb_cons);
8122}
8123
Daniel Veillard81a8ec62005-08-22 00:20:58 +00008124#endif /* LIBXML_EXPR_ENABLED */
Daniel Veillard5d4644e2005-04-01 13:11:58 +00008125#define bottom_xmlregexp
8126#include "elfgcchack.h"
Daniel Veillard4255d502002-04-16 15:50:10 +00008127#endif /* LIBXML_REGEXP_ENABLED */