blob: 9c76ef60ae04721c9cff0284dd4b4a09e6ed6a68 [file] [log] [blame]
Daniel Veillard4255d502002-04-16 15:50:10 +00001/*
2 * regexp.c: generic and extensible Regular Expression engine
3 *
4 * Basically designed with the purpose of compiling regexps for
5 * the variety of validation/shemas mechanisms now available in
William M. Brackddf71d62004-05-06 04:17:26 +00006 * XML related specifications these include:
Daniel Veillard4255d502002-04-16 15:50:10 +00007 * - XML-1.0 DTD validation
8 * - XML Schemas structure part 1
9 * - XML Schemas Datatypes part 2 especially Appendix F
10 * - RELAX-NG/TREX i.e. the counter proposal
11 *
12 * See Copyright for the status of this software.
13 *
14 * Daniel Veillard <veillard@redhat.com>
15 */
16
17#define IN_LIBXML
18#include "libxml.h"
19
20#ifdef LIBXML_REGEXP_ENABLED
21
Daniel Veillardcee2b3a2005-01-25 00:22:52 +000022/* #define DEBUG_ERR */
Daniel Veillardfc0b6f62005-01-09 17:48:02 +000023
Daniel Veillard4255d502002-04-16 15:50:10 +000024#include <stdio.h>
25#include <string.h>
Daniel Veillardebe48c62003-12-03 12:12:27 +000026#ifdef HAVE_LIMITS_H
27#include <limits.h>
28#endif
29
Daniel Veillard4255d502002-04-16 15:50:10 +000030#include <libxml/tree.h>
31#include <libxml/parserInternals.h>
32#include <libxml/xmlregexp.h>
33#include <libxml/xmlautomata.h>
34#include <libxml/xmlunicode.h>
35
Daniel Veillardebe48c62003-12-03 12:12:27 +000036#ifndef INT_MAX
37#define INT_MAX 123456789 /* easy to flag and big enough for our needs */
38#endif
39
Daniel Veillardc0826a72004-08-10 14:17:33 +000040/* #define DEBUG_REGEXP_GRAPH */
Daniel Veillard10752282005-08-08 13:05:13 +000041/* #define DEBUG_REGEXP_EXEC */
Daniel Veillard4255d502002-04-16 15:50:10 +000042/* #define DEBUG_PUSH */
Daniel Veillard23e73572002-09-19 19:56:43 +000043/* #define DEBUG_COMPACTION */
Daniel Veillard4255d502002-04-16 15:50:10 +000044
Daniel Veillard567a45b2005-10-18 19:11:55 +000045#define MAX_PUSH 10000000
Daniel Veillard94cc1032005-09-15 13:09:00 +000046
Daniel Veillardff46a042003-10-08 08:53:17 +000047#define ERROR(str) \
48 ctxt->error = XML_REGEXP_COMPILE_ERROR; \
49 xmlRegexpErrCompile(ctxt, str);
Daniel Veillard4255d502002-04-16 15:50:10 +000050#define NEXT ctxt->cur++
51#define CUR (*(ctxt->cur))
52#define NXT(index) (ctxt->cur[index])
53
54#define CUR_SCHAR(s, l) xmlStringCurrentChar(NULL, s, &l)
55#define NEXTL(l) ctxt->cur += l;
Daniel Veillardc0826a72004-08-10 14:17:33 +000056#define XML_REG_STRING_SEPARATOR '|'
Daniel Veillard4255d502002-04-16 15:50:10 +000057
Daniel Veillarde19fc232002-04-22 16:01:24 +000058/**
59 * TODO:
60 *
61 * macro to flag unimplemented blocks
62 */
63#define TODO \
64 xmlGenericError(xmlGenericErrorContext, \
65 "Unimplemented block at %s:%d\n", \
66 __FILE__, __LINE__);
67
Daniel Veillard4255d502002-04-16 15:50:10 +000068/************************************************************************
69 * *
70 * Datatypes and structures *
71 * *
72 ************************************************************************/
73
74typedef enum {
75 XML_REGEXP_EPSILON = 1,
76 XML_REGEXP_CHARVAL,
77 XML_REGEXP_RANGES,
Daniel Veillard567a45b2005-10-18 19:11:55 +000078 XML_REGEXP_SUBREG, /* used for () sub regexps */
Daniel Veillard4255d502002-04-16 15:50:10 +000079 XML_REGEXP_STRING,
80 XML_REGEXP_ANYCHAR, /* . */
81 XML_REGEXP_ANYSPACE, /* \s */
82 XML_REGEXP_NOTSPACE, /* \S */
83 XML_REGEXP_INITNAME, /* \l */
Daniel Veillard567a45b2005-10-18 19:11:55 +000084 XML_REGEXP_NOTINITNAME, /* \L */
Daniel Veillard4255d502002-04-16 15:50:10 +000085 XML_REGEXP_NAMECHAR, /* \c */
86 XML_REGEXP_NOTNAMECHAR, /* \C */
87 XML_REGEXP_DECIMAL, /* \d */
Daniel Veillard567a45b2005-10-18 19:11:55 +000088 XML_REGEXP_NOTDECIMAL, /* \D */
Daniel Veillard4255d502002-04-16 15:50:10 +000089 XML_REGEXP_REALCHAR, /* \w */
Daniel Veillard567a45b2005-10-18 19:11:55 +000090 XML_REGEXP_NOTREALCHAR, /* \W */
91 XML_REGEXP_LETTER = 100,
Daniel Veillard4255d502002-04-16 15:50:10 +000092 XML_REGEXP_LETTER_UPPERCASE,
93 XML_REGEXP_LETTER_LOWERCASE,
94 XML_REGEXP_LETTER_TITLECASE,
95 XML_REGEXP_LETTER_MODIFIER,
96 XML_REGEXP_LETTER_OTHERS,
97 XML_REGEXP_MARK,
98 XML_REGEXP_MARK_NONSPACING,
99 XML_REGEXP_MARK_SPACECOMBINING,
100 XML_REGEXP_MARK_ENCLOSING,
101 XML_REGEXP_NUMBER,
102 XML_REGEXP_NUMBER_DECIMAL,
103 XML_REGEXP_NUMBER_LETTER,
104 XML_REGEXP_NUMBER_OTHERS,
105 XML_REGEXP_PUNCT,
106 XML_REGEXP_PUNCT_CONNECTOR,
107 XML_REGEXP_PUNCT_DASH,
108 XML_REGEXP_PUNCT_OPEN,
109 XML_REGEXP_PUNCT_CLOSE,
110 XML_REGEXP_PUNCT_INITQUOTE,
111 XML_REGEXP_PUNCT_FINQUOTE,
112 XML_REGEXP_PUNCT_OTHERS,
113 XML_REGEXP_SEPAR,
114 XML_REGEXP_SEPAR_SPACE,
115 XML_REGEXP_SEPAR_LINE,
116 XML_REGEXP_SEPAR_PARA,
117 XML_REGEXP_SYMBOL,
118 XML_REGEXP_SYMBOL_MATH,
119 XML_REGEXP_SYMBOL_CURRENCY,
120 XML_REGEXP_SYMBOL_MODIFIER,
121 XML_REGEXP_SYMBOL_OTHERS,
122 XML_REGEXP_OTHER,
123 XML_REGEXP_OTHER_CONTROL,
124 XML_REGEXP_OTHER_FORMAT,
125 XML_REGEXP_OTHER_PRIVATE,
126 XML_REGEXP_OTHER_NA,
127 XML_REGEXP_BLOCK_NAME
128} xmlRegAtomType;
129
130typedef enum {
131 XML_REGEXP_QUANT_EPSILON = 1,
132 XML_REGEXP_QUANT_ONCE,
133 XML_REGEXP_QUANT_OPT,
134 XML_REGEXP_QUANT_MULT,
135 XML_REGEXP_QUANT_PLUS,
Daniel Veillard7646b182002-04-20 06:41:40 +0000136 XML_REGEXP_QUANT_ONCEONLY,
137 XML_REGEXP_QUANT_ALL,
Daniel Veillard4255d502002-04-16 15:50:10 +0000138 XML_REGEXP_QUANT_RANGE
139} xmlRegQuantType;
140
141typedef enum {
142 XML_REGEXP_START_STATE = 1,
143 XML_REGEXP_FINAL_STATE,
Daniel Veillardcc026dc2005-01-12 13:21:17 +0000144 XML_REGEXP_TRANS_STATE,
145 XML_REGEXP_SINK_STATE
Daniel Veillard4255d502002-04-16 15:50:10 +0000146} xmlRegStateType;
147
148typedef enum {
149 XML_REGEXP_MARK_NORMAL = 0,
150 XML_REGEXP_MARK_START,
151 XML_REGEXP_MARK_VISITED
152} xmlRegMarkedType;
153
154typedef struct _xmlRegRange xmlRegRange;
155typedef xmlRegRange *xmlRegRangePtr;
156
157struct _xmlRegRange {
Daniel Veillardf8b9de32003-11-24 14:27:26 +0000158 int neg; /* 0 normal, 1 not, 2 exclude */
Daniel Veillard4255d502002-04-16 15:50:10 +0000159 xmlRegAtomType type;
160 int start;
161 int end;
162 xmlChar *blockName;
163};
164
165typedef struct _xmlRegAtom xmlRegAtom;
166typedef xmlRegAtom *xmlRegAtomPtr;
167
168typedef struct _xmlAutomataState xmlRegState;
169typedef xmlRegState *xmlRegStatePtr;
170
171struct _xmlRegAtom {
172 int no;
173 xmlRegAtomType type;
174 xmlRegQuantType quant;
175 int min;
176 int max;
177
178 void *valuep;
Daniel Veillarda646cfd2002-09-17 21:50:03 +0000179 void *valuep2;
Daniel Veillard4255d502002-04-16 15:50:10 +0000180 int neg;
181 int codepoint;
182 xmlRegStatePtr start;
183 xmlRegStatePtr stop;
184 int maxRanges;
185 int nbRanges;
186 xmlRegRangePtr *ranges;
187 void *data;
188};
189
190typedef struct _xmlRegCounter xmlRegCounter;
191typedef xmlRegCounter *xmlRegCounterPtr;
192
193struct _xmlRegCounter {
194 int min;
195 int max;
196};
197
198typedef struct _xmlRegTrans xmlRegTrans;
199typedef xmlRegTrans *xmlRegTransPtr;
200
201struct _xmlRegTrans {
202 xmlRegAtomPtr atom;
203 int to;
204 int counter;
205 int count;
Daniel Veillard567a45b2005-10-18 19:11:55 +0000206 int nd;
Daniel Veillard4255d502002-04-16 15:50:10 +0000207};
208
209struct _xmlAutomataState {
210 xmlRegStateType type;
211 xmlRegMarkedType mark;
Daniel Veillard23e73572002-09-19 19:56:43 +0000212 xmlRegMarkedType reached;
Daniel Veillard4255d502002-04-16 15:50:10 +0000213 int no;
Daniel Veillard4255d502002-04-16 15:50:10 +0000214 int maxTrans;
215 int nbTrans;
216 xmlRegTrans *trans;
Daniel Veillarddb68b742005-07-30 13:18:24 +0000217 /* knowing states ponting to us can speed things up */
218 int maxTransTo;
219 int nbTransTo;
220 int *transTo;
Daniel Veillard4255d502002-04-16 15:50:10 +0000221};
222
223typedef struct _xmlAutomata xmlRegParserCtxt;
224typedef xmlRegParserCtxt *xmlRegParserCtxtPtr;
225
226struct _xmlAutomata {
227 xmlChar *string;
228 xmlChar *cur;
229
230 int error;
231 int neg;
232
233 xmlRegStatePtr start;
234 xmlRegStatePtr end;
235 xmlRegStatePtr state;
236
237 xmlRegAtomPtr atom;
238
239 int maxAtoms;
240 int nbAtoms;
241 xmlRegAtomPtr *atoms;
242
243 int maxStates;
244 int nbStates;
245 xmlRegStatePtr *states;
246
247 int maxCounters;
248 int nbCounters;
249 xmlRegCounter *counters;
Daniel Veillarde19fc232002-04-22 16:01:24 +0000250
251 int determinist;
Daniel Veillard6e65e152005-08-09 11:09:52 +0000252 int negs;
Daniel Veillard4255d502002-04-16 15:50:10 +0000253};
254
255struct _xmlRegexp {
256 xmlChar *string;
257 int nbStates;
258 xmlRegStatePtr *states;
259 int nbAtoms;
260 xmlRegAtomPtr *atoms;
261 int nbCounters;
262 xmlRegCounter *counters;
Daniel Veillarde19fc232002-04-22 16:01:24 +0000263 int determinist;
Daniel Veillard23e73572002-09-19 19:56:43 +0000264 /*
265 * That's the compact form for determinists automatas
266 */
267 int nbstates;
268 int *compact;
Daniel Veillard118aed72002-09-24 14:13:13 +0000269 void **transdata;
Daniel Veillard23e73572002-09-19 19:56:43 +0000270 int nbstrings;
271 xmlChar **stringMap;
Daniel Veillard4255d502002-04-16 15:50:10 +0000272};
273
274typedef struct _xmlRegExecRollback xmlRegExecRollback;
275typedef xmlRegExecRollback *xmlRegExecRollbackPtr;
276
277struct _xmlRegExecRollback {
278 xmlRegStatePtr state;/* the current state */
279 int index; /* the index in the input stack */
280 int nextbranch; /* the next transition to explore in that state */
William M. Brackddf71d62004-05-06 04:17:26 +0000281 int *counts; /* save the automata state if it has some */
Daniel Veillard4255d502002-04-16 15:50:10 +0000282};
283
284typedef struct _xmlRegInputToken xmlRegInputToken;
285typedef xmlRegInputToken *xmlRegInputTokenPtr;
286
287struct _xmlRegInputToken {
288 xmlChar *value;
289 void *data;
290};
291
292struct _xmlRegExecCtxt {
293 int status; /* execution status != 0 indicate an error */
William M. Brackddf71d62004-05-06 04:17:26 +0000294 int determinist; /* did we find an indeterministic behaviour */
Daniel Veillard4255d502002-04-16 15:50:10 +0000295 xmlRegexpPtr comp; /* the compiled regexp */
296 xmlRegExecCallbacks callback;
297 void *data;
298
299 xmlRegStatePtr state;/* the current state */
300 int transno; /* the current transition on that state */
William M. Brackddf71d62004-05-06 04:17:26 +0000301 int transcount; /* the number of chars in char counted transitions */
Daniel Veillard4255d502002-04-16 15:50:10 +0000302
303 /*
304 * A stack of rollback states
305 */
306 int maxRollbacks;
307 int nbRollbacks;
308 xmlRegExecRollback *rollbacks;
309
310 /*
311 * The state of the automata if any
312 */
313 int *counts;
314
315 /*
316 * The input stack
317 */
318 int inputStackMax;
319 int inputStackNr;
320 int index;
321 int *charStack;
322 const xmlChar *inputString; /* when operating on characters */
323 xmlRegInputTokenPtr inputStack;/* when operating on strings */
324
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +0000325 /*
326 * error handling
327 */
328 int errStateNo; /* the error state number */
329 xmlRegStatePtr errState; /* the error state */
330 xmlChar *errString; /* the string raising the error */
331 int *errCounts; /* counters at the error state */
Daniel Veillard94cc1032005-09-15 13:09:00 +0000332 int nbPush;
Daniel Veillard4255d502002-04-16 15:50:10 +0000333};
334
Daniel Veillard441bc322002-04-20 17:38:48 +0000335#define REGEXP_ALL_COUNTER 0x123456
336#define REGEXP_ALL_LAX_COUNTER 0x123457
Daniel Veillard7646b182002-04-20 06:41:40 +0000337
Daniel Veillard4255d502002-04-16 15:50:10 +0000338static void xmlFAParseRegExp(xmlRegParserCtxtPtr ctxt, int top);
Daniel Veillard23e73572002-09-19 19:56:43 +0000339static void xmlRegFreeState(xmlRegStatePtr state);
340static void xmlRegFreeAtom(xmlRegAtomPtr atom);
Daniel Veillard9efc4762005-07-19 14:33:55 +0000341static int xmlRegStrEqualWildcard(const xmlChar *expStr, const xmlChar *valStr);
Daniel Veillard567a45b2005-10-18 19:11:55 +0000342static int xmlRegCheckCharacter(xmlRegAtomPtr atom, int codepoint);
343static int xmlRegCheckCharacterRange(xmlRegAtomType type, int codepoint,
344 int neg, int start, int end, const xmlChar *blockName);
Daniel Veillard4255d502002-04-16 15:50:10 +0000345
346/************************************************************************
Daniel Veillardff46a042003-10-08 08:53:17 +0000347 * *
348 * Regexp memory error handler *
349 * *
350 ************************************************************************/
351/**
352 * xmlRegexpErrMemory:
William M. Brackddf71d62004-05-06 04:17:26 +0000353 * @extra: extra information
Daniel Veillardff46a042003-10-08 08:53:17 +0000354 *
355 * Handle an out of memory condition
356 */
357static void
358xmlRegexpErrMemory(xmlRegParserCtxtPtr ctxt, const char *extra)
359{
360 const char *regexp = NULL;
361 if (ctxt != NULL) {
362 regexp = (const char *) ctxt->string;
363 ctxt->error = XML_ERR_NO_MEMORY;
364 }
Daniel Veillard659e71e2003-10-10 14:10:40 +0000365 __xmlRaiseError(NULL, NULL, NULL, NULL, NULL, XML_FROM_REGEXP,
Daniel Veillardff46a042003-10-08 08:53:17 +0000366 XML_ERR_NO_MEMORY, XML_ERR_FATAL, NULL, 0, extra,
367 regexp, NULL, 0, 0,
368 "Memory allocation failed : %s\n", extra);
369}
370
371/**
372 * xmlRegexpErrCompile:
William M. Brackddf71d62004-05-06 04:17:26 +0000373 * @extra: extra information
Daniel Veillardff46a042003-10-08 08:53:17 +0000374 *
William M. Brackddf71d62004-05-06 04:17:26 +0000375 * Handle a compilation failure
Daniel Veillardff46a042003-10-08 08:53:17 +0000376 */
377static void
378xmlRegexpErrCompile(xmlRegParserCtxtPtr ctxt, const char *extra)
379{
380 const char *regexp = NULL;
381 int idx = 0;
382
383 if (ctxt != NULL) {
384 regexp = (const char *) ctxt->string;
385 idx = ctxt->cur - ctxt->string;
386 ctxt->error = XML_REGEXP_COMPILE_ERROR;
387 }
Daniel Veillard659e71e2003-10-10 14:10:40 +0000388 __xmlRaiseError(NULL, NULL, NULL, NULL, NULL, XML_FROM_REGEXP,
Daniel Veillardff46a042003-10-08 08:53:17 +0000389 XML_REGEXP_COMPILE_ERROR, XML_ERR_FATAL, NULL, 0, extra,
390 regexp, NULL, idx, 0,
391 "failed to compile: %s\n", extra);
392}
393
394/************************************************************************
Daniel Veillard4255d502002-04-16 15:50:10 +0000395 * *
396 * Allocation/Deallocation *
397 * *
398 ************************************************************************/
399
Daniel Veillard23e73572002-09-19 19:56:43 +0000400static int xmlFAComputesDeterminism(xmlRegParserCtxtPtr ctxt);
Daniel Veillard4255d502002-04-16 15:50:10 +0000401/**
402 * xmlRegEpxFromParse:
403 * @ctxt: the parser context used to build it
404 *
William M. Brackddf71d62004-05-06 04:17:26 +0000405 * Allocate a new regexp and fill it with the result from the parser
Daniel Veillard4255d502002-04-16 15:50:10 +0000406 *
407 * Returns the new regexp or NULL in case of error
408 */
409static xmlRegexpPtr
410xmlRegEpxFromParse(xmlRegParserCtxtPtr ctxt) {
411 xmlRegexpPtr ret;
412
413 ret = (xmlRegexpPtr) xmlMalloc(sizeof(xmlRegexp));
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000414 if (ret == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +0000415 xmlRegexpErrMemory(ctxt, "compiling regexp");
Daniel Veillard4255d502002-04-16 15:50:10 +0000416 return(NULL);
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000417 }
Daniel Veillard4255d502002-04-16 15:50:10 +0000418 memset(ret, 0, sizeof(xmlRegexp));
419 ret->string = ctxt->string;
Daniel Veillard4255d502002-04-16 15:50:10 +0000420 ret->nbStates = ctxt->nbStates;
Daniel Veillard4255d502002-04-16 15:50:10 +0000421 ret->states = ctxt->states;
Daniel Veillard4255d502002-04-16 15:50:10 +0000422 ret->nbAtoms = ctxt->nbAtoms;
Daniel Veillard4255d502002-04-16 15:50:10 +0000423 ret->atoms = ctxt->atoms;
Daniel Veillard4255d502002-04-16 15:50:10 +0000424 ret->nbCounters = ctxt->nbCounters;
Daniel Veillard4255d502002-04-16 15:50:10 +0000425 ret->counters = ctxt->counters;
Daniel Veillarde19fc232002-04-22 16:01:24 +0000426 ret->determinist = ctxt->determinist;
Daniel Veillard567a45b2005-10-18 19:11:55 +0000427 if (ret->determinist == -1) {
428 xmlRegexpIsDeterminist(ret);
429 }
Daniel Veillard23e73572002-09-19 19:56:43 +0000430
431 if ((ret->determinist != 0) &&
432 (ret->nbCounters == 0) &&
Daniel Veillard6e65e152005-08-09 11:09:52 +0000433 (ctxt->negs == 0) &&
Daniel Veillard118aed72002-09-24 14:13:13 +0000434 (ret->atoms != NULL) &&
Daniel Veillard23e73572002-09-19 19:56:43 +0000435 (ret->atoms[0] != NULL) &&
436 (ret->atoms[0]->type == XML_REGEXP_STRING)) {
437 int i, j, nbstates = 0, nbatoms = 0;
438 int *stateRemap;
439 int *stringRemap;
440 int *transitions;
Daniel Veillard118aed72002-09-24 14:13:13 +0000441 void **transdata;
Daniel Veillard23e73572002-09-19 19:56:43 +0000442 xmlChar **stringMap;
443 xmlChar *value;
444
445 /*
446 * Switch to a compact representation
447 * 1/ counting the effective number of states left
William M. Brackddf71d62004-05-06 04:17:26 +0000448 * 2/ counting the unique number of atoms, and check that
Daniel Veillard23e73572002-09-19 19:56:43 +0000449 * they are all of the string type
450 * 3/ build a table state x atom for the transitions
451 */
452
453 stateRemap = xmlMalloc(ret->nbStates * sizeof(int));
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000454 if (stateRemap == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +0000455 xmlRegexpErrMemory(ctxt, "compiling regexp");
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000456 xmlFree(ret);
457 return(NULL);
458 }
Daniel Veillard23e73572002-09-19 19:56:43 +0000459 for (i = 0;i < ret->nbStates;i++) {
460 if (ret->states[i] != NULL) {
461 stateRemap[i] = nbstates;
462 nbstates++;
463 } else {
464 stateRemap[i] = -1;
465 }
466 }
467#ifdef DEBUG_COMPACTION
468 printf("Final: %d states\n", nbstates);
469#endif
470 stringMap = xmlMalloc(ret->nbAtoms * sizeof(char *));
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000471 if (stringMap == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +0000472 xmlRegexpErrMemory(ctxt, "compiling regexp");
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000473 xmlFree(stateRemap);
474 xmlFree(ret);
475 return(NULL);
476 }
Daniel Veillard23e73572002-09-19 19:56:43 +0000477 stringRemap = xmlMalloc(ret->nbAtoms * sizeof(int));
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000478 if (stringRemap == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +0000479 xmlRegexpErrMemory(ctxt, "compiling regexp");
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000480 xmlFree(stringMap);
481 xmlFree(stateRemap);
482 xmlFree(ret);
483 return(NULL);
484 }
Daniel Veillard23e73572002-09-19 19:56:43 +0000485 for (i = 0;i < ret->nbAtoms;i++) {
486 if ((ret->atoms[i]->type == XML_REGEXP_STRING) &&
487 (ret->atoms[i]->quant == XML_REGEXP_QUANT_ONCE)) {
488 value = ret->atoms[i]->valuep;
489 for (j = 0;j < nbatoms;j++) {
490 if (xmlStrEqual(stringMap[j], value)) {
491 stringRemap[i] = j;
492 break;
493 }
494 }
495 if (j >= nbatoms) {
496 stringRemap[i] = nbatoms;
497 stringMap[nbatoms] = xmlStrdup(value);
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000498 if (stringMap[nbatoms] == NULL) {
499 for (i = 0;i < nbatoms;i++)
500 xmlFree(stringMap[i]);
501 xmlFree(stringRemap);
502 xmlFree(stringMap);
503 xmlFree(stateRemap);
504 xmlFree(ret);
505 return(NULL);
506 }
Daniel Veillard23e73572002-09-19 19:56:43 +0000507 nbatoms++;
508 }
509 } else {
510 xmlFree(stateRemap);
511 xmlFree(stringRemap);
512 for (i = 0;i < nbatoms;i++)
513 xmlFree(stringMap[i]);
514 xmlFree(stringMap);
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000515 xmlFree(ret);
516 return(NULL);
Daniel Veillard23e73572002-09-19 19:56:43 +0000517 }
518 }
519#ifdef DEBUG_COMPACTION
520 printf("Final: %d atoms\n", nbatoms);
521#endif
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000522 transitions = (int *) xmlMalloc((nbstates + 1) *
523 (nbatoms + 1) * sizeof(int));
524 if (transitions == NULL) {
525 xmlFree(stateRemap);
526 xmlFree(stringRemap);
527 xmlFree(stringMap);
528 xmlFree(ret);
529 return(NULL);
530 }
531 memset(transitions, 0, (nbstates + 1) * (nbatoms + 1) * sizeof(int));
Daniel Veillard23e73572002-09-19 19:56:43 +0000532
533 /*
534 * Allocate the transition table. The first entry for each
William M. Brackddf71d62004-05-06 04:17:26 +0000535 * state corresponds to the state type.
Daniel Veillard23e73572002-09-19 19:56:43 +0000536 */
Daniel Veillard118aed72002-09-24 14:13:13 +0000537 transdata = NULL;
Daniel Veillard23e73572002-09-19 19:56:43 +0000538
539 for (i = 0;i < ret->nbStates;i++) {
540 int stateno, atomno, targetno, prev;
541 xmlRegStatePtr state;
542 xmlRegTransPtr trans;
543
544 stateno = stateRemap[i];
545 if (stateno == -1)
546 continue;
547 state = ret->states[i];
548
549 transitions[stateno * (nbatoms + 1)] = state->type;
550
551 for (j = 0;j < state->nbTrans;j++) {
552 trans = &(state->trans[j]);
553 if ((trans->to == -1) || (trans->atom == NULL))
554 continue;
555 atomno = stringRemap[trans->atom->no];
Daniel Veillard118aed72002-09-24 14:13:13 +0000556 if ((trans->atom->data != NULL) && (transdata == NULL)) {
557 transdata = (void **) xmlMalloc(nbstates * nbatoms *
558 sizeof(void *));
559 if (transdata != NULL)
560 memset(transdata, 0,
561 nbstates * nbatoms * sizeof(void *));
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000562 else {
Daniel Veillardff46a042003-10-08 08:53:17 +0000563 xmlRegexpErrMemory(ctxt, "compiling regexp");
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000564 break;
565 }
Daniel Veillard118aed72002-09-24 14:13:13 +0000566 }
Daniel Veillard23e73572002-09-19 19:56:43 +0000567 targetno = stateRemap[trans->to];
568 /*
William M. Brackddf71d62004-05-06 04:17:26 +0000569 * if the same atom can generate transitions to 2 different
Daniel Veillard23e73572002-09-19 19:56:43 +0000570 * states then it means the automata is not determinist and
571 * the compact form can't be used !
572 */
573 prev = transitions[stateno * (nbatoms + 1) + atomno + 1];
574 if (prev != 0) {
575 if (prev != targetno + 1) {
Daniel Veillard23e73572002-09-19 19:56:43 +0000576 ret->determinist = 0;
577#ifdef DEBUG_COMPACTION
578 printf("Indet: state %d trans %d, atom %d to %d : %d to %d\n",
579 i, j, trans->atom->no, trans->to, atomno, targetno);
580 printf(" previous to is %d\n", prev);
581#endif
Daniel Veillard118aed72002-09-24 14:13:13 +0000582 if (transdata != NULL)
583 xmlFree(transdata);
Daniel Veillard23e73572002-09-19 19:56:43 +0000584 xmlFree(transitions);
585 xmlFree(stateRemap);
586 xmlFree(stringRemap);
587 for (i = 0;i < nbatoms;i++)
588 xmlFree(stringMap[i]);
589 xmlFree(stringMap);
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000590 goto not_determ;
Daniel Veillard23e73572002-09-19 19:56:43 +0000591 }
592 } else {
593#if 0
594 printf("State %d trans %d: atom %d to %d : %d to %d\n",
595 i, j, trans->atom->no, trans->to, atomno, targetno);
596#endif
597 transitions[stateno * (nbatoms + 1) + atomno + 1] =
Daniel Veillard118aed72002-09-24 14:13:13 +0000598 targetno + 1; /* to avoid 0 */
599 if (transdata != NULL)
600 transdata[stateno * nbatoms + atomno] =
601 trans->atom->data;
Daniel Veillard23e73572002-09-19 19:56:43 +0000602 }
603 }
604 }
605 ret->determinist = 1;
606#ifdef DEBUG_COMPACTION
607 /*
608 * Debug
609 */
610 for (i = 0;i < nbstates;i++) {
611 for (j = 0;j < nbatoms + 1;j++) {
612 printf("%02d ", transitions[i * (nbatoms + 1) + j]);
613 }
614 printf("\n");
615 }
616 printf("\n");
617#endif
618 /*
619 * Cleanup of the old data
620 */
621 if (ret->states != NULL) {
622 for (i = 0;i < ret->nbStates;i++)
623 xmlRegFreeState(ret->states[i]);
624 xmlFree(ret->states);
625 }
626 ret->states = NULL;
627 ret->nbStates = 0;
628 if (ret->atoms != NULL) {
629 for (i = 0;i < ret->nbAtoms;i++)
630 xmlRegFreeAtom(ret->atoms[i]);
631 xmlFree(ret->atoms);
632 }
633 ret->atoms = NULL;
634 ret->nbAtoms = 0;
635
636 ret->compact = transitions;
Daniel Veillard118aed72002-09-24 14:13:13 +0000637 ret->transdata = transdata;
Daniel Veillard23e73572002-09-19 19:56:43 +0000638 ret->stringMap = stringMap;
639 ret->nbstrings = nbatoms;
640 ret->nbstates = nbstates;
641 xmlFree(stateRemap);
642 xmlFree(stringRemap);
643 }
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000644not_determ:
645 ctxt->string = NULL;
646 ctxt->nbStates = 0;
647 ctxt->states = NULL;
648 ctxt->nbAtoms = 0;
649 ctxt->atoms = NULL;
650 ctxt->nbCounters = 0;
651 ctxt->counters = NULL;
Daniel Veillard4255d502002-04-16 15:50:10 +0000652 return(ret);
653}
654
655/**
656 * xmlRegNewParserCtxt:
657 * @string: the string to parse
658 *
659 * Allocate a new regexp parser context
660 *
661 * Returns the new context or NULL in case of error
662 */
663static xmlRegParserCtxtPtr
664xmlRegNewParserCtxt(const xmlChar *string) {
665 xmlRegParserCtxtPtr ret;
666
667 ret = (xmlRegParserCtxtPtr) xmlMalloc(sizeof(xmlRegParserCtxt));
668 if (ret == NULL)
669 return(NULL);
670 memset(ret, 0, sizeof(xmlRegParserCtxt));
671 if (string != NULL)
672 ret->string = xmlStrdup(string);
673 ret->cur = ret->string;
674 ret->neg = 0;
Daniel Veillard6e65e152005-08-09 11:09:52 +0000675 ret->negs = 0;
Daniel Veillard4255d502002-04-16 15:50:10 +0000676 ret->error = 0;
Daniel Veillarde19fc232002-04-22 16:01:24 +0000677 ret->determinist = -1;
Daniel Veillard4255d502002-04-16 15:50:10 +0000678 return(ret);
679}
680
681/**
682 * xmlRegNewRange:
683 * @ctxt: the regexp parser context
684 * @neg: is that negative
685 * @type: the type of range
686 * @start: the start codepoint
687 * @end: the end codepoint
688 *
689 * Allocate a new regexp range
690 *
691 * Returns the new range or NULL in case of error
692 */
693static xmlRegRangePtr
694xmlRegNewRange(xmlRegParserCtxtPtr ctxt,
695 int neg, xmlRegAtomType type, int start, int end) {
696 xmlRegRangePtr ret;
697
698 ret = (xmlRegRangePtr) xmlMalloc(sizeof(xmlRegRange));
699 if (ret == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +0000700 xmlRegexpErrMemory(ctxt, "allocating range");
Daniel Veillard4255d502002-04-16 15:50:10 +0000701 return(NULL);
702 }
703 ret->neg = neg;
704 ret->type = type;
705 ret->start = start;
706 ret->end = end;
707 return(ret);
708}
709
710/**
711 * xmlRegFreeRange:
712 * @range: the regexp range
713 *
714 * Free a regexp range
715 */
716static void
717xmlRegFreeRange(xmlRegRangePtr range) {
718 if (range == NULL)
719 return;
720
721 if (range->blockName != NULL)
722 xmlFree(range->blockName);
723 xmlFree(range);
724}
725
726/**
727 * xmlRegNewAtom:
728 * @ctxt: the regexp parser context
729 * @type: the type of atom
730 *
731 * Allocate a new regexp range
732 *
733 * Returns the new atom or NULL in case of error
734 */
735static xmlRegAtomPtr
736xmlRegNewAtom(xmlRegParserCtxtPtr ctxt, xmlRegAtomType type) {
737 xmlRegAtomPtr ret;
738
739 ret = (xmlRegAtomPtr) xmlMalloc(sizeof(xmlRegAtom));
740 if (ret == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +0000741 xmlRegexpErrMemory(ctxt, "allocating atom");
Daniel Veillard4255d502002-04-16 15:50:10 +0000742 return(NULL);
743 }
744 memset(ret, 0, sizeof(xmlRegAtom));
745 ret->type = type;
746 ret->quant = XML_REGEXP_QUANT_ONCE;
747 ret->min = 0;
748 ret->max = 0;
749 return(ret);
750}
751
752/**
753 * xmlRegFreeAtom:
754 * @atom: the regexp atom
755 *
756 * Free a regexp atom
757 */
758static void
759xmlRegFreeAtom(xmlRegAtomPtr atom) {
760 int i;
761
762 if (atom == NULL)
763 return;
764
765 for (i = 0;i < atom->nbRanges;i++)
766 xmlRegFreeRange(atom->ranges[i]);
767 if (atom->ranges != NULL)
768 xmlFree(atom->ranges);
Daniel Veillardde0e4982005-07-03 14:35:44 +0000769 if ((atom->type == XML_REGEXP_STRING) && (atom->valuep != NULL))
770 xmlFree(atom->valuep);
Daniel Veillard77005e62005-07-19 16:26:18 +0000771 if ((atom->type == XML_REGEXP_STRING) && (atom->valuep2 != NULL))
772 xmlFree(atom->valuep2);
Daniel Veillardde0e4982005-07-03 14:35:44 +0000773 if ((atom->type == XML_REGEXP_BLOCK_NAME) && (atom->valuep != NULL))
Daniel Veillard4255d502002-04-16 15:50:10 +0000774 xmlFree(atom->valuep);
775 xmlFree(atom);
776}
777
778static xmlRegStatePtr
779xmlRegNewState(xmlRegParserCtxtPtr ctxt) {
780 xmlRegStatePtr ret;
781
782 ret = (xmlRegStatePtr) xmlMalloc(sizeof(xmlRegState));
783 if (ret == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +0000784 xmlRegexpErrMemory(ctxt, "allocating state");
Daniel Veillard4255d502002-04-16 15:50:10 +0000785 return(NULL);
786 }
787 memset(ret, 0, sizeof(xmlRegState));
788 ret->type = XML_REGEXP_TRANS_STATE;
789 ret->mark = XML_REGEXP_MARK_NORMAL;
790 return(ret);
791}
792
793/**
794 * xmlRegFreeState:
795 * @state: the regexp state
796 *
797 * Free a regexp state
798 */
799static void
800xmlRegFreeState(xmlRegStatePtr state) {
801 if (state == NULL)
802 return;
803
804 if (state->trans != NULL)
805 xmlFree(state->trans);
Daniel Veillarddb68b742005-07-30 13:18:24 +0000806 if (state->transTo != NULL)
807 xmlFree(state->transTo);
Daniel Veillard4255d502002-04-16 15:50:10 +0000808 xmlFree(state);
809}
810
811/**
812 * xmlRegFreeParserCtxt:
813 * @ctxt: the regexp parser context
814 *
815 * Free a regexp parser context
816 */
817static void
818xmlRegFreeParserCtxt(xmlRegParserCtxtPtr ctxt) {
819 int i;
820 if (ctxt == NULL)
821 return;
822
823 if (ctxt->string != NULL)
824 xmlFree(ctxt->string);
825 if (ctxt->states != NULL) {
826 for (i = 0;i < ctxt->nbStates;i++)
827 xmlRegFreeState(ctxt->states[i]);
828 xmlFree(ctxt->states);
829 }
830 if (ctxt->atoms != NULL) {
831 for (i = 0;i < ctxt->nbAtoms;i++)
832 xmlRegFreeAtom(ctxt->atoms[i]);
833 xmlFree(ctxt->atoms);
834 }
835 if (ctxt->counters != NULL)
836 xmlFree(ctxt->counters);
837 xmlFree(ctxt);
838}
839
840/************************************************************************
841 * *
842 * Display of Data structures *
843 * *
844 ************************************************************************/
845
846static void
847xmlRegPrintAtomType(FILE *output, xmlRegAtomType type) {
848 switch (type) {
849 case XML_REGEXP_EPSILON:
850 fprintf(output, "epsilon "); break;
851 case XML_REGEXP_CHARVAL:
852 fprintf(output, "charval "); break;
853 case XML_REGEXP_RANGES:
854 fprintf(output, "ranges "); break;
855 case XML_REGEXP_SUBREG:
856 fprintf(output, "subexpr "); break;
857 case XML_REGEXP_STRING:
858 fprintf(output, "string "); break;
859 case XML_REGEXP_ANYCHAR:
860 fprintf(output, "anychar "); break;
861 case XML_REGEXP_ANYSPACE:
862 fprintf(output, "anyspace "); break;
863 case XML_REGEXP_NOTSPACE:
864 fprintf(output, "notspace "); break;
865 case XML_REGEXP_INITNAME:
866 fprintf(output, "initname "); break;
867 case XML_REGEXP_NOTINITNAME:
868 fprintf(output, "notinitname "); break;
869 case XML_REGEXP_NAMECHAR:
870 fprintf(output, "namechar "); break;
871 case XML_REGEXP_NOTNAMECHAR:
872 fprintf(output, "notnamechar "); break;
873 case XML_REGEXP_DECIMAL:
874 fprintf(output, "decimal "); break;
875 case XML_REGEXP_NOTDECIMAL:
876 fprintf(output, "notdecimal "); break;
877 case XML_REGEXP_REALCHAR:
878 fprintf(output, "realchar "); break;
879 case XML_REGEXP_NOTREALCHAR:
880 fprintf(output, "notrealchar "); break;
881 case XML_REGEXP_LETTER:
882 fprintf(output, "LETTER "); break;
883 case XML_REGEXP_LETTER_UPPERCASE:
884 fprintf(output, "LETTER_UPPERCASE "); break;
885 case XML_REGEXP_LETTER_LOWERCASE:
886 fprintf(output, "LETTER_LOWERCASE "); break;
887 case XML_REGEXP_LETTER_TITLECASE:
888 fprintf(output, "LETTER_TITLECASE "); break;
889 case XML_REGEXP_LETTER_MODIFIER:
890 fprintf(output, "LETTER_MODIFIER "); break;
891 case XML_REGEXP_LETTER_OTHERS:
892 fprintf(output, "LETTER_OTHERS "); break;
893 case XML_REGEXP_MARK:
894 fprintf(output, "MARK "); break;
895 case XML_REGEXP_MARK_NONSPACING:
896 fprintf(output, "MARK_NONSPACING "); break;
897 case XML_REGEXP_MARK_SPACECOMBINING:
898 fprintf(output, "MARK_SPACECOMBINING "); break;
899 case XML_REGEXP_MARK_ENCLOSING:
900 fprintf(output, "MARK_ENCLOSING "); break;
901 case XML_REGEXP_NUMBER:
902 fprintf(output, "NUMBER "); break;
903 case XML_REGEXP_NUMBER_DECIMAL:
904 fprintf(output, "NUMBER_DECIMAL "); break;
905 case XML_REGEXP_NUMBER_LETTER:
906 fprintf(output, "NUMBER_LETTER "); break;
907 case XML_REGEXP_NUMBER_OTHERS:
908 fprintf(output, "NUMBER_OTHERS "); break;
909 case XML_REGEXP_PUNCT:
910 fprintf(output, "PUNCT "); break;
911 case XML_REGEXP_PUNCT_CONNECTOR:
912 fprintf(output, "PUNCT_CONNECTOR "); break;
913 case XML_REGEXP_PUNCT_DASH:
914 fprintf(output, "PUNCT_DASH "); break;
915 case XML_REGEXP_PUNCT_OPEN:
916 fprintf(output, "PUNCT_OPEN "); break;
917 case XML_REGEXP_PUNCT_CLOSE:
918 fprintf(output, "PUNCT_CLOSE "); break;
919 case XML_REGEXP_PUNCT_INITQUOTE:
920 fprintf(output, "PUNCT_INITQUOTE "); break;
921 case XML_REGEXP_PUNCT_FINQUOTE:
922 fprintf(output, "PUNCT_FINQUOTE "); break;
923 case XML_REGEXP_PUNCT_OTHERS:
924 fprintf(output, "PUNCT_OTHERS "); break;
925 case XML_REGEXP_SEPAR:
926 fprintf(output, "SEPAR "); break;
927 case XML_REGEXP_SEPAR_SPACE:
928 fprintf(output, "SEPAR_SPACE "); break;
929 case XML_REGEXP_SEPAR_LINE:
930 fprintf(output, "SEPAR_LINE "); break;
931 case XML_REGEXP_SEPAR_PARA:
932 fprintf(output, "SEPAR_PARA "); break;
933 case XML_REGEXP_SYMBOL:
934 fprintf(output, "SYMBOL "); break;
935 case XML_REGEXP_SYMBOL_MATH:
936 fprintf(output, "SYMBOL_MATH "); break;
937 case XML_REGEXP_SYMBOL_CURRENCY:
938 fprintf(output, "SYMBOL_CURRENCY "); break;
939 case XML_REGEXP_SYMBOL_MODIFIER:
940 fprintf(output, "SYMBOL_MODIFIER "); break;
941 case XML_REGEXP_SYMBOL_OTHERS:
942 fprintf(output, "SYMBOL_OTHERS "); break;
943 case XML_REGEXP_OTHER:
944 fprintf(output, "OTHER "); break;
945 case XML_REGEXP_OTHER_CONTROL:
946 fprintf(output, "OTHER_CONTROL "); break;
947 case XML_REGEXP_OTHER_FORMAT:
948 fprintf(output, "OTHER_FORMAT "); break;
949 case XML_REGEXP_OTHER_PRIVATE:
950 fprintf(output, "OTHER_PRIVATE "); break;
951 case XML_REGEXP_OTHER_NA:
952 fprintf(output, "OTHER_NA "); break;
953 case XML_REGEXP_BLOCK_NAME:
954 fprintf(output, "BLOCK "); break;
955 }
956}
957
958static void
959xmlRegPrintQuantType(FILE *output, xmlRegQuantType type) {
960 switch (type) {
961 case XML_REGEXP_QUANT_EPSILON:
962 fprintf(output, "epsilon "); break;
963 case XML_REGEXP_QUANT_ONCE:
964 fprintf(output, "once "); break;
965 case XML_REGEXP_QUANT_OPT:
966 fprintf(output, "? "); break;
967 case XML_REGEXP_QUANT_MULT:
968 fprintf(output, "* "); break;
969 case XML_REGEXP_QUANT_PLUS:
970 fprintf(output, "+ "); break;
971 case XML_REGEXP_QUANT_RANGE:
972 fprintf(output, "range "); break;
Daniel Veillard7646b182002-04-20 06:41:40 +0000973 case XML_REGEXP_QUANT_ONCEONLY:
974 fprintf(output, "onceonly "); break;
975 case XML_REGEXP_QUANT_ALL:
976 fprintf(output, "all "); break;
Daniel Veillard4255d502002-04-16 15:50:10 +0000977 }
978}
979static void
980xmlRegPrintRange(FILE *output, xmlRegRangePtr range) {
981 fprintf(output, " range: ");
982 if (range->neg)
983 fprintf(output, "negative ");
984 xmlRegPrintAtomType(output, range->type);
985 fprintf(output, "%c - %c\n", range->start, range->end);
986}
987
988static void
989xmlRegPrintAtom(FILE *output, xmlRegAtomPtr atom) {
990 fprintf(output, " atom: ");
991 if (atom == NULL) {
992 fprintf(output, "NULL\n");
993 return;
994 }
Daniel Veillard9efc4762005-07-19 14:33:55 +0000995 if (atom->neg)
996 fprintf(output, "not ");
Daniel Veillard4255d502002-04-16 15:50:10 +0000997 xmlRegPrintAtomType(output, atom->type);
998 xmlRegPrintQuantType(output, atom->quant);
999 if (atom->quant == XML_REGEXP_QUANT_RANGE)
1000 fprintf(output, "%d-%d ", atom->min, atom->max);
1001 if (atom->type == XML_REGEXP_STRING)
1002 fprintf(output, "'%s' ", (char *) atom->valuep);
1003 if (atom->type == XML_REGEXP_CHARVAL)
1004 fprintf(output, "char %c\n", atom->codepoint);
1005 else if (atom->type == XML_REGEXP_RANGES) {
1006 int i;
1007 fprintf(output, "%d entries\n", atom->nbRanges);
1008 for (i = 0; i < atom->nbRanges;i++)
1009 xmlRegPrintRange(output, atom->ranges[i]);
1010 } else if (atom->type == XML_REGEXP_SUBREG) {
1011 fprintf(output, "start %d end %d\n", atom->start->no, atom->stop->no);
1012 } else {
1013 fprintf(output, "\n");
1014 }
1015}
1016
1017static void
1018xmlRegPrintTrans(FILE *output, xmlRegTransPtr trans) {
1019 fprintf(output, " trans: ");
1020 if (trans == NULL) {
1021 fprintf(output, "NULL\n");
1022 return;
1023 }
1024 if (trans->to < 0) {
1025 fprintf(output, "removed\n");
1026 return;
1027 }
Daniel Veillard567a45b2005-10-18 19:11:55 +00001028 if (trans->nd != 0) {
1029 if (trans->nd == 2)
1030 fprintf(output, "last not determinist, ");
1031 else
1032 fprintf(output, "not determinist, ");
1033 }
Daniel Veillard4255d502002-04-16 15:50:10 +00001034 if (trans->counter >= 0) {
1035 fprintf(output, "counted %d, ", trans->counter);
1036 }
Daniel Veillard8a001f62002-04-20 07:24:11 +00001037 if (trans->count == REGEXP_ALL_COUNTER) {
1038 fprintf(output, "all transition, ");
1039 } else if (trans->count >= 0) {
Daniel Veillard4255d502002-04-16 15:50:10 +00001040 fprintf(output, "count based %d, ", trans->count);
1041 }
1042 if (trans->atom == NULL) {
1043 fprintf(output, "epsilon to %d\n", trans->to);
1044 return;
1045 }
1046 if (trans->atom->type == XML_REGEXP_CHARVAL)
1047 fprintf(output, "char %c ", trans->atom->codepoint);
1048 fprintf(output, "atom %d, to %d\n", trans->atom->no, trans->to);
1049}
1050
1051static void
1052xmlRegPrintState(FILE *output, xmlRegStatePtr state) {
1053 int i;
1054
1055 fprintf(output, " state: ");
1056 if (state == NULL) {
1057 fprintf(output, "NULL\n");
1058 return;
1059 }
1060 if (state->type == XML_REGEXP_START_STATE)
1061 fprintf(output, "START ");
1062 if (state->type == XML_REGEXP_FINAL_STATE)
1063 fprintf(output, "FINAL ");
1064
1065 fprintf(output, "%d, %d transitions:\n", state->no, state->nbTrans);
1066 for (i = 0;i < state->nbTrans; i++) {
1067 xmlRegPrintTrans(output, &(state->trans[i]));
1068 }
1069}
1070
Daniel Veillard23e73572002-09-19 19:56:43 +00001071#ifdef DEBUG_REGEXP_GRAPH
Daniel Veillard4255d502002-04-16 15:50:10 +00001072static void
1073xmlRegPrintCtxt(FILE *output, xmlRegParserCtxtPtr ctxt) {
1074 int i;
1075
1076 fprintf(output, " ctxt: ");
1077 if (ctxt == NULL) {
1078 fprintf(output, "NULL\n");
1079 return;
1080 }
1081 fprintf(output, "'%s' ", ctxt->string);
1082 if (ctxt->error)
1083 fprintf(output, "error ");
1084 if (ctxt->neg)
1085 fprintf(output, "neg ");
1086 fprintf(output, "\n");
1087 fprintf(output, "%d atoms:\n", ctxt->nbAtoms);
1088 for (i = 0;i < ctxt->nbAtoms; i++) {
1089 fprintf(output, " %02d ", i);
1090 xmlRegPrintAtom(output, ctxt->atoms[i]);
1091 }
1092 if (ctxt->atom != NULL) {
1093 fprintf(output, "current atom:\n");
1094 xmlRegPrintAtom(output, ctxt->atom);
1095 }
1096 fprintf(output, "%d states:", ctxt->nbStates);
1097 if (ctxt->start != NULL)
1098 fprintf(output, " start: %d", ctxt->start->no);
1099 if (ctxt->end != NULL)
1100 fprintf(output, " end: %d", ctxt->end->no);
1101 fprintf(output, "\n");
1102 for (i = 0;i < ctxt->nbStates; i++) {
1103 xmlRegPrintState(output, ctxt->states[i]);
1104 }
1105 fprintf(output, "%d counters:\n", ctxt->nbCounters);
1106 for (i = 0;i < ctxt->nbCounters; i++) {
1107 fprintf(output, " %d: min %d max %d\n", i, ctxt->counters[i].min,
1108 ctxt->counters[i].max);
1109 }
1110}
Daniel Veillard23e73572002-09-19 19:56:43 +00001111#endif
Daniel Veillard4255d502002-04-16 15:50:10 +00001112
1113/************************************************************************
1114 * *
1115 * Finite Automata structures manipulations *
1116 * *
1117 ************************************************************************/
1118
1119static void
1120xmlRegAtomAddRange(xmlRegParserCtxtPtr ctxt, xmlRegAtomPtr atom,
1121 int neg, xmlRegAtomType type, int start, int end,
1122 xmlChar *blockName) {
1123 xmlRegRangePtr range;
1124
1125 if (atom == NULL) {
1126 ERROR("add range: atom is NULL");
1127 return;
1128 }
1129 if (atom->type != XML_REGEXP_RANGES) {
1130 ERROR("add range: atom is not ranges");
1131 return;
1132 }
1133 if (atom->maxRanges == 0) {
1134 atom->maxRanges = 4;
1135 atom->ranges = (xmlRegRangePtr *) xmlMalloc(atom->maxRanges *
1136 sizeof(xmlRegRangePtr));
1137 if (atom->ranges == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00001138 xmlRegexpErrMemory(ctxt, "adding ranges");
Daniel Veillard4255d502002-04-16 15:50:10 +00001139 atom->maxRanges = 0;
1140 return;
1141 }
1142 } else if (atom->nbRanges >= atom->maxRanges) {
1143 xmlRegRangePtr *tmp;
1144 atom->maxRanges *= 2;
1145 tmp = (xmlRegRangePtr *) xmlRealloc(atom->ranges, atom->maxRanges *
1146 sizeof(xmlRegRangePtr));
1147 if (tmp == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00001148 xmlRegexpErrMemory(ctxt, "adding ranges");
Daniel Veillard4255d502002-04-16 15:50:10 +00001149 atom->maxRanges /= 2;
1150 return;
1151 }
1152 atom->ranges = tmp;
1153 }
1154 range = xmlRegNewRange(ctxt, neg, type, start, end);
1155 if (range == NULL)
1156 return;
1157 range->blockName = blockName;
1158 atom->ranges[atom->nbRanges++] = range;
1159
1160}
1161
1162static int
1163xmlRegGetCounter(xmlRegParserCtxtPtr ctxt) {
1164 if (ctxt->maxCounters == 0) {
1165 ctxt->maxCounters = 4;
1166 ctxt->counters = (xmlRegCounter *) xmlMalloc(ctxt->maxCounters *
1167 sizeof(xmlRegCounter));
1168 if (ctxt->counters == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00001169 xmlRegexpErrMemory(ctxt, "allocating counter");
Daniel Veillard4255d502002-04-16 15:50:10 +00001170 ctxt->maxCounters = 0;
1171 return(-1);
1172 }
1173 } else if (ctxt->nbCounters >= ctxt->maxCounters) {
1174 xmlRegCounter *tmp;
1175 ctxt->maxCounters *= 2;
1176 tmp = (xmlRegCounter *) xmlRealloc(ctxt->counters, ctxt->maxCounters *
1177 sizeof(xmlRegCounter));
1178 if (tmp == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00001179 xmlRegexpErrMemory(ctxt, "allocating counter");
Daniel Veillard4255d502002-04-16 15:50:10 +00001180 ctxt->maxCounters /= 2;
1181 return(-1);
1182 }
1183 ctxt->counters = tmp;
1184 }
1185 ctxt->counters[ctxt->nbCounters].min = -1;
1186 ctxt->counters[ctxt->nbCounters].max = -1;
1187 return(ctxt->nbCounters++);
1188}
1189
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001190static int
Daniel Veillard4255d502002-04-16 15:50:10 +00001191xmlRegAtomPush(xmlRegParserCtxtPtr ctxt, xmlRegAtomPtr atom) {
1192 if (atom == NULL) {
1193 ERROR("atom push: atom is NULL");
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001194 return(-1);
Daniel Veillard4255d502002-04-16 15:50:10 +00001195 }
1196 if (ctxt->maxAtoms == 0) {
1197 ctxt->maxAtoms = 4;
1198 ctxt->atoms = (xmlRegAtomPtr *) xmlMalloc(ctxt->maxAtoms *
1199 sizeof(xmlRegAtomPtr));
1200 if (ctxt->atoms == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00001201 xmlRegexpErrMemory(ctxt, "pushing atom");
Daniel Veillard4255d502002-04-16 15:50:10 +00001202 ctxt->maxAtoms = 0;
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001203 return(-1);
Daniel Veillard4255d502002-04-16 15:50:10 +00001204 }
1205 } else if (ctxt->nbAtoms >= ctxt->maxAtoms) {
1206 xmlRegAtomPtr *tmp;
1207 ctxt->maxAtoms *= 2;
1208 tmp = (xmlRegAtomPtr *) xmlRealloc(ctxt->atoms, ctxt->maxAtoms *
1209 sizeof(xmlRegAtomPtr));
1210 if (tmp == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00001211 xmlRegexpErrMemory(ctxt, "allocating counter");
Daniel Veillard4255d502002-04-16 15:50:10 +00001212 ctxt->maxAtoms /= 2;
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001213 return(-1);
Daniel Veillard4255d502002-04-16 15:50:10 +00001214 }
1215 ctxt->atoms = tmp;
1216 }
1217 atom->no = ctxt->nbAtoms;
1218 ctxt->atoms[ctxt->nbAtoms++] = atom;
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001219 return(0);
Daniel Veillard4255d502002-04-16 15:50:10 +00001220}
1221
1222static void
Daniel Veillarddb68b742005-07-30 13:18:24 +00001223xmlRegStateAddTransTo(xmlRegParserCtxtPtr ctxt, xmlRegStatePtr target,
1224 int from) {
1225 if (target->maxTransTo == 0) {
1226 target->maxTransTo = 8;
1227 target->transTo = (int *) xmlMalloc(target->maxTransTo *
1228 sizeof(int));
1229 if (target->transTo == NULL) {
1230 xmlRegexpErrMemory(ctxt, "adding transition");
1231 target->maxTransTo = 0;
1232 return;
1233 }
1234 } else if (target->nbTransTo >= target->maxTransTo) {
1235 int *tmp;
1236 target->maxTransTo *= 2;
1237 tmp = (int *) xmlRealloc(target->transTo, target->maxTransTo *
1238 sizeof(int));
1239 if (tmp == NULL) {
1240 xmlRegexpErrMemory(ctxt, "adding transition");
1241 target->maxTransTo /= 2;
1242 return;
1243 }
1244 target->transTo = tmp;
1245 }
1246 target->transTo[target->nbTransTo] = from;
1247 target->nbTransTo++;
1248}
1249
1250static void
Daniel Veillard4255d502002-04-16 15:50:10 +00001251xmlRegStateAddTrans(xmlRegParserCtxtPtr ctxt, xmlRegStatePtr state,
1252 xmlRegAtomPtr atom, xmlRegStatePtr target,
Daniel Veillard5de09382005-09-26 17:18:17 +00001253 int counter, int count) {
William M. Brackf9b5fa22004-05-10 07:52:15 +00001254
1255 int nrtrans;
1256
Daniel Veillard4255d502002-04-16 15:50:10 +00001257 if (state == NULL) {
1258 ERROR("add state: state is NULL");
1259 return;
1260 }
1261 if (target == NULL) {
1262 ERROR("add state: target is NULL");
1263 return;
1264 }
William M. Brackf9b5fa22004-05-10 07:52:15 +00001265 /*
1266 * Other routines follow the philosophy 'When in doubt, add a transition'
1267 * so we check here whether such a transition is already present and, if
1268 * so, silently ignore this request.
1269 */
1270
Daniel Veillard5de09382005-09-26 17:18:17 +00001271 for (nrtrans = state->nbTrans - 1; nrtrans >= 0; nrtrans--) {
1272 xmlRegTransPtr trans = &(state->trans[nrtrans]);
1273 if ((trans->atom == atom) &&
1274 (trans->to == target->no) &&
1275 (trans->counter == counter) &&
1276 (trans->count == count)) {
William M. Brackf9b5fa22004-05-10 07:52:15 +00001277#ifdef DEBUG_REGEXP_GRAPH
Daniel Veillard5de09382005-09-26 17:18:17 +00001278 printf("Ignoring duplicate transition from %d to %d\n",
1279 state->no, target->no);
William M. Brackf9b5fa22004-05-10 07:52:15 +00001280#endif
Daniel Veillard5de09382005-09-26 17:18:17 +00001281 return;
Daniel Veillarddb68b742005-07-30 13:18:24 +00001282 }
William M. Brackf9b5fa22004-05-10 07:52:15 +00001283 }
1284
Daniel Veillard4255d502002-04-16 15:50:10 +00001285 if (state->maxTrans == 0) {
Daniel Veillarddb68b742005-07-30 13:18:24 +00001286 state->maxTrans = 8;
Daniel Veillard4255d502002-04-16 15:50:10 +00001287 state->trans = (xmlRegTrans *) xmlMalloc(state->maxTrans *
1288 sizeof(xmlRegTrans));
1289 if (state->trans == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00001290 xmlRegexpErrMemory(ctxt, "adding transition");
Daniel Veillard4255d502002-04-16 15:50:10 +00001291 state->maxTrans = 0;
1292 return;
1293 }
1294 } else if (state->nbTrans >= state->maxTrans) {
1295 xmlRegTrans *tmp;
1296 state->maxTrans *= 2;
1297 tmp = (xmlRegTrans *) xmlRealloc(state->trans, state->maxTrans *
1298 sizeof(xmlRegTrans));
1299 if (tmp == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00001300 xmlRegexpErrMemory(ctxt, "adding transition");
Daniel Veillard4255d502002-04-16 15:50:10 +00001301 state->maxTrans /= 2;
1302 return;
1303 }
1304 state->trans = tmp;
1305 }
1306#ifdef DEBUG_REGEXP_GRAPH
1307 printf("Add trans from %d to %d ", state->no, target->no);
Daniel Veillard8a001f62002-04-20 07:24:11 +00001308 if (count == REGEXP_ALL_COUNTER)
Daniel Veillard2cbf5962004-03-31 15:50:43 +00001309 printf("all transition\n");
Daniel Veillard4402ab42002-09-12 16:02:56 +00001310 else if (count >= 0)
Daniel Veillard2cbf5962004-03-31 15:50:43 +00001311 printf("count based %d\n", count);
Daniel Veillard4255d502002-04-16 15:50:10 +00001312 else if (counter >= 0)
Daniel Veillard2cbf5962004-03-31 15:50:43 +00001313 printf("counted %d\n", counter);
Daniel Veillard4255d502002-04-16 15:50:10 +00001314 else if (atom == NULL)
Daniel Veillard2cbf5962004-03-31 15:50:43 +00001315 printf("epsilon transition\n");
1316 else if (atom != NULL)
1317 xmlRegPrintAtom(stdout, atom);
Daniel Veillard4255d502002-04-16 15:50:10 +00001318#endif
1319
1320 state->trans[state->nbTrans].atom = atom;
1321 state->trans[state->nbTrans].to = target->no;
1322 state->trans[state->nbTrans].counter = counter;
1323 state->trans[state->nbTrans].count = count;
Daniel Veillard567a45b2005-10-18 19:11:55 +00001324 state->trans[state->nbTrans].nd = 0;
Daniel Veillard4255d502002-04-16 15:50:10 +00001325 state->nbTrans++;
Daniel Veillarddb68b742005-07-30 13:18:24 +00001326 xmlRegStateAddTransTo(ctxt, target, state->no);
Daniel Veillard4255d502002-04-16 15:50:10 +00001327}
1328
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001329static int
Daniel Veillard4255d502002-04-16 15:50:10 +00001330xmlRegStatePush(xmlRegParserCtxtPtr ctxt, xmlRegStatePtr state) {
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001331 if (state == NULL) return(-1);
Daniel Veillard4255d502002-04-16 15:50:10 +00001332 if (ctxt->maxStates == 0) {
1333 ctxt->maxStates = 4;
1334 ctxt->states = (xmlRegStatePtr *) xmlMalloc(ctxt->maxStates *
1335 sizeof(xmlRegStatePtr));
1336 if (ctxt->states == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00001337 xmlRegexpErrMemory(ctxt, "adding state");
Daniel Veillard4255d502002-04-16 15:50:10 +00001338 ctxt->maxStates = 0;
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001339 return(-1);
Daniel Veillard4255d502002-04-16 15:50:10 +00001340 }
1341 } else if (ctxt->nbStates >= ctxt->maxStates) {
1342 xmlRegStatePtr *tmp;
1343 ctxt->maxStates *= 2;
1344 tmp = (xmlRegStatePtr *) xmlRealloc(ctxt->states, ctxt->maxStates *
1345 sizeof(xmlRegStatePtr));
1346 if (tmp == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00001347 xmlRegexpErrMemory(ctxt, "adding state");
Daniel Veillard4255d502002-04-16 15:50:10 +00001348 ctxt->maxStates /= 2;
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001349 return(-1);
Daniel Veillard4255d502002-04-16 15:50:10 +00001350 }
1351 ctxt->states = tmp;
1352 }
1353 state->no = ctxt->nbStates;
1354 ctxt->states[ctxt->nbStates++] = state;
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001355 return(0);
Daniel Veillard4255d502002-04-16 15:50:10 +00001356}
1357
1358/**
Daniel Veillard7646b182002-04-20 06:41:40 +00001359 * xmlFAGenerateAllTransition:
Daniel Veillard441bc322002-04-20 17:38:48 +00001360 * @ctxt: a regexp parser context
1361 * @from: the from state
1362 * @to: the target state or NULL for building a new one
1363 * @lax:
Daniel Veillard7646b182002-04-20 06:41:40 +00001364 *
1365 */
1366static void
1367xmlFAGenerateAllTransition(xmlRegParserCtxtPtr ctxt,
Daniel Veillard441bc322002-04-20 17:38:48 +00001368 xmlRegStatePtr from, xmlRegStatePtr to,
1369 int lax) {
Daniel Veillard7646b182002-04-20 06:41:40 +00001370 if (to == NULL) {
1371 to = xmlRegNewState(ctxt);
1372 xmlRegStatePush(ctxt, to);
1373 ctxt->state = to;
1374 }
Daniel Veillard441bc322002-04-20 17:38:48 +00001375 if (lax)
Daniel Veillard5de09382005-09-26 17:18:17 +00001376 xmlRegStateAddTrans(ctxt, from, NULL, to, -1, REGEXP_ALL_LAX_COUNTER);
Daniel Veillard441bc322002-04-20 17:38:48 +00001377 else
Daniel Veillard5de09382005-09-26 17:18:17 +00001378 xmlRegStateAddTrans(ctxt, from, NULL, to, -1, REGEXP_ALL_COUNTER);
Daniel Veillard7646b182002-04-20 06:41:40 +00001379}
1380
1381/**
Daniel Veillard4255d502002-04-16 15:50:10 +00001382 * xmlFAGenerateEpsilonTransition:
Daniel Veillard441bc322002-04-20 17:38:48 +00001383 * @ctxt: a regexp parser context
1384 * @from: the from state
1385 * @to: the target state or NULL for building a new one
Daniel Veillard4255d502002-04-16 15:50:10 +00001386 *
1387 */
1388static void
1389xmlFAGenerateEpsilonTransition(xmlRegParserCtxtPtr ctxt,
1390 xmlRegStatePtr from, xmlRegStatePtr to) {
1391 if (to == NULL) {
1392 to = xmlRegNewState(ctxt);
1393 xmlRegStatePush(ctxt, to);
1394 ctxt->state = to;
1395 }
Daniel Veillard5de09382005-09-26 17:18:17 +00001396 xmlRegStateAddTrans(ctxt, from, NULL, to, -1, -1);
Daniel Veillard4255d502002-04-16 15:50:10 +00001397}
1398
1399/**
1400 * xmlFAGenerateCountedEpsilonTransition:
Daniel Veillard441bc322002-04-20 17:38:48 +00001401 * @ctxt: a regexp parser context
1402 * @from: the from state
1403 * @to: the target state or NULL for building a new one
Daniel Veillard4255d502002-04-16 15:50:10 +00001404 * counter: the counter for that transition
1405 *
1406 */
1407static void
1408xmlFAGenerateCountedEpsilonTransition(xmlRegParserCtxtPtr ctxt,
1409 xmlRegStatePtr from, xmlRegStatePtr to, int counter) {
1410 if (to == NULL) {
1411 to = xmlRegNewState(ctxt);
1412 xmlRegStatePush(ctxt, to);
1413 ctxt->state = to;
1414 }
Daniel Veillard5de09382005-09-26 17:18:17 +00001415 xmlRegStateAddTrans(ctxt, from, NULL, to, counter, -1);
Daniel Veillard4255d502002-04-16 15:50:10 +00001416}
1417
1418/**
1419 * xmlFAGenerateCountedTransition:
Daniel Veillard441bc322002-04-20 17:38:48 +00001420 * @ctxt: a regexp parser context
1421 * @from: the from state
1422 * @to: the target state or NULL for building a new one
Daniel Veillard4255d502002-04-16 15:50:10 +00001423 * counter: the counter for that transition
1424 *
1425 */
1426static void
1427xmlFAGenerateCountedTransition(xmlRegParserCtxtPtr ctxt,
1428 xmlRegStatePtr from, xmlRegStatePtr to, int counter) {
1429 if (to == NULL) {
1430 to = xmlRegNewState(ctxt);
1431 xmlRegStatePush(ctxt, to);
1432 ctxt->state = to;
1433 }
Daniel Veillard5de09382005-09-26 17:18:17 +00001434 xmlRegStateAddTrans(ctxt, from, NULL, to, -1, counter);
Daniel Veillard4255d502002-04-16 15:50:10 +00001435}
1436
1437/**
1438 * xmlFAGenerateTransitions:
Daniel Veillard441bc322002-04-20 17:38:48 +00001439 * @ctxt: a regexp parser context
1440 * @from: the from state
1441 * @to: the target state or NULL for building a new one
1442 * @atom: the atom generating the transition
Daniel Veillard4255d502002-04-16 15:50:10 +00001443 *
William M. Brackddf71d62004-05-06 04:17:26 +00001444 * Returns 0 if success and -1 in case of error.
Daniel Veillard4255d502002-04-16 15:50:10 +00001445 */
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001446static int
Daniel Veillard4255d502002-04-16 15:50:10 +00001447xmlFAGenerateTransitions(xmlRegParserCtxtPtr ctxt, xmlRegStatePtr from,
1448 xmlRegStatePtr to, xmlRegAtomPtr atom) {
1449 if (atom == NULL) {
1450 ERROR("genrate transition: atom == NULL");
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001451 return(-1);
Daniel Veillard4255d502002-04-16 15:50:10 +00001452 }
1453 if (atom->type == XML_REGEXP_SUBREG) {
1454 /*
1455 * this is a subexpression handling one should not need to
William M. Brackddf71d62004-05-06 04:17:26 +00001456 * create a new node except for XML_REGEXP_QUANT_RANGE.
Daniel Veillard4255d502002-04-16 15:50:10 +00001457 */
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001458 if (xmlRegAtomPush(ctxt, atom) < 0) {
1459 return(-1);
1460 }
Daniel Veillard4255d502002-04-16 15:50:10 +00001461 if ((to != NULL) && (atom->stop != to) &&
1462 (atom->quant != XML_REGEXP_QUANT_RANGE)) {
1463 /*
1464 * Generate an epsilon transition to link to the target
1465 */
1466 xmlFAGenerateEpsilonTransition(ctxt, atom->stop, to);
1467 }
1468 switch (atom->quant) {
1469 case XML_REGEXP_QUANT_OPT:
1470 atom->quant = XML_REGEXP_QUANT_ONCE;
1471 xmlFAGenerateEpsilonTransition(ctxt, atom->start, atom->stop);
1472 break;
1473 case XML_REGEXP_QUANT_MULT:
1474 atom->quant = XML_REGEXP_QUANT_ONCE;
1475 xmlFAGenerateEpsilonTransition(ctxt, atom->start, atom->stop);
1476 xmlFAGenerateEpsilonTransition(ctxt, atom->stop, atom->start);
1477 break;
1478 case XML_REGEXP_QUANT_PLUS:
1479 atom->quant = XML_REGEXP_QUANT_ONCE;
1480 xmlFAGenerateEpsilonTransition(ctxt, atom->stop, atom->start);
1481 break;
1482 case XML_REGEXP_QUANT_RANGE: {
1483 int counter;
1484 xmlRegStatePtr newstate;
1485
1486 /*
1487 * This one is nasty:
William M. Brackddf71d62004-05-06 04:17:26 +00001488 * 1/ if range has minOccurs == 0, create a new state
1489 * and create epsilon transitions from atom->start
1490 * to atom->stop, as well as atom->start to the new
1491 * state
1492 * 2/ register a new counter
1493 * 3/ register an epsilon transition associated to
Daniel Veillard4255d502002-04-16 15:50:10 +00001494 * this counter going from atom->stop to atom->start
William M. Brackddf71d62004-05-06 04:17:26 +00001495 * 4/ create a new state
1496 * 5/ generate a counted transition from atom->stop to
Daniel Veillard4255d502002-04-16 15:50:10 +00001497 * that state
1498 */
William M. Brackddf71d62004-05-06 04:17:26 +00001499 if (atom->min == 0) {
1500 xmlFAGenerateEpsilonTransition(ctxt, atom->start,
1501 atom->stop);
1502 newstate = xmlRegNewState(ctxt);
1503 xmlRegStatePush(ctxt, newstate);
1504 ctxt->state = newstate;
1505 xmlFAGenerateEpsilonTransition(ctxt, atom->start,
1506 newstate);
1507 }
Daniel Veillard4255d502002-04-16 15:50:10 +00001508 counter = xmlRegGetCounter(ctxt);
1509 ctxt->counters[counter].min = atom->min - 1;
1510 ctxt->counters[counter].max = atom->max - 1;
1511 atom->min = 0;
1512 atom->max = 0;
1513 atom->quant = XML_REGEXP_QUANT_ONCE;
1514 xmlFAGenerateCountedEpsilonTransition(ctxt, atom->stop,
1515 atom->start, counter);
1516 if (to != NULL) {
1517 newstate = to;
1518 } else {
1519 newstate = xmlRegNewState(ctxt);
1520 xmlRegStatePush(ctxt, newstate);
1521 ctxt->state = newstate;
1522 }
1523 xmlFAGenerateCountedTransition(ctxt, atom->stop,
1524 newstate, counter);
1525 }
1526 default:
1527 break;
1528 }
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001529 return(0);
Daniel Veillard567a45b2005-10-18 19:11:55 +00001530 }
1531 if ((atom->min == 0) && (atom->max == 0) &&
Daniel Veillard99c394d2005-07-14 12:58:49 +00001532 (atom->quant == XML_REGEXP_QUANT_RANGE)) {
1533 /*
1534 * we can discard the atom and generate an epsilon transition instead
1535 */
1536 if (to == NULL) {
1537 to = xmlRegNewState(ctxt);
1538 if (to != NULL)
1539 xmlRegStatePush(ctxt, to);
1540 else {
1541 return(-1);
1542 }
1543 }
1544 xmlFAGenerateEpsilonTransition(ctxt, from, to);
1545 ctxt->state = to;
1546 xmlRegFreeAtom(atom);
1547 return(0);
Daniel Veillard567a45b2005-10-18 19:11:55 +00001548 }
1549 if (to == NULL) {
1550 to = xmlRegNewState(ctxt);
1551 if (to != NULL)
1552 xmlRegStatePush(ctxt, to);
1553 else {
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001554 return(-1);
Daniel Veillard4255d502002-04-16 15:50:10 +00001555 }
Daniel Veillard4255d502002-04-16 15:50:10 +00001556 }
Daniel Veillard567a45b2005-10-18 19:11:55 +00001557 if (xmlRegAtomPush(ctxt, atom) < 0) {
1558 return(-1);
1559 }
1560 xmlRegStateAddTrans(ctxt, from, atom, to, -1, -1);
1561 ctxt->state = to;
Daniel Veillard4255d502002-04-16 15:50:10 +00001562 switch (atom->quant) {
1563 case XML_REGEXP_QUANT_OPT:
1564 atom->quant = XML_REGEXP_QUANT_ONCE;
1565 xmlFAGenerateEpsilonTransition(ctxt, from, to);
1566 break;
1567 case XML_REGEXP_QUANT_MULT:
1568 atom->quant = XML_REGEXP_QUANT_ONCE;
1569 xmlFAGenerateEpsilonTransition(ctxt, from, to);
Daniel Veillard5de09382005-09-26 17:18:17 +00001570 xmlRegStateAddTrans(ctxt, to, atom, to, -1, -1);
Daniel Veillard4255d502002-04-16 15:50:10 +00001571 break;
1572 case XML_REGEXP_QUANT_PLUS:
1573 atom->quant = XML_REGEXP_QUANT_ONCE;
Daniel Veillard5de09382005-09-26 17:18:17 +00001574 xmlRegStateAddTrans(ctxt, to, atom, to, -1, -1);
Daniel Veillard4255d502002-04-16 15:50:10 +00001575 break;
1576 default:
1577 break;
1578 }
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001579 return(0);
Daniel Veillard4255d502002-04-16 15:50:10 +00001580}
1581
1582/**
1583 * xmlFAReduceEpsilonTransitions:
Daniel Veillard441bc322002-04-20 17:38:48 +00001584 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00001585 * @fromnr: the from state
1586 * @tonr: the to state
William M. Brackddf71d62004-05-06 04:17:26 +00001587 * @counter: should that transition be associated to a counted
Daniel Veillard4255d502002-04-16 15:50:10 +00001588 *
1589 */
1590static void
1591xmlFAReduceEpsilonTransitions(xmlRegParserCtxtPtr ctxt, int fromnr,
1592 int tonr, int counter) {
1593 int transnr;
1594 xmlRegStatePtr from;
1595 xmlRegStatePtr to;
1596
1597#ifdef DEBUG_REGEXP_GRAPH
1598 printf("xmlFAReduceEpsilonTransitions(%d, %d)\n", fromnr, tonr);
1599#endif
1600 from = ctxt->states[fromnr];
1601 if (from == NULL)
1602 return;
1603 to = ctxt->states[tonr];
1604 if (to == NULL)
1605 return;
1606 if ((to->mark == XML_REGEXP_MARK_START) ||
1607 (to->mark == XML_REGEXP_MARK_VISITED))
1608 return;
1609
1610 to->mark = XML_REGEXP_MARK_VISITED;
1611 if (to->type == XML_REGEXP_FINAL_STATE) {
1612#ifdef DEBUG_REGEXP_GRAPH
1613 printf("State %d is final, so %d becomes final\n", tonr, fromnr);
1614#endif
1615 from->type = XML_REGEXP_FINAL_STATE;
1616 }
1617 for (transnr = 0;transnr < to->nbTrans;transnr++) {
Daniel Veillarddb68b742005-07-30 13:18:24 +00001618 if (to->trans[transnr].to < 0)
1619 continue;
Daniel Veillard4255d502002-04-16 15:50:10 +00001620 if (to->trans[transnr].atom == NULL) {
1621 /*
1622 * Don't remove counted transitions
1623 * Don't loop either
1624 */
Daniel Veillardb509f152002-04-17 16:28:10 +00001625 if (to->trans[transnr].to != fromnr) {
1626 if (to->trans[transnr].count >= 0) {
1627 int newto = to->trans[transnr].to;
1628
1629 xmlRegStateAddTrans(ctxt, from, NULL,
1630 ctxt->states[newto],
Daniel Veillard5de09382005-09-26 17:18:17 +00001631 -1, to->trans[transnr].count);
Daniel Veillardb509f152002-04-17 16:28:10 +00001632 } else {
Daniel Veillard4255d502002-04-16 15:50:10 +00001633#ifdef DEBUG_REGEXP_GRAPH
Daniel Veillardb509f152002-04-17 16:28:10 +00001634 printf("Found epsilon trans %d from %d to %d\n",
1635 transnr, tonr, to->trans[transnr].to);
Daniel Veillard4255d502002-04-16 15:50:10 +00001636#endif
Daniel Veillardb509f152002-04-17 16:28:10 +00001637 if (to->trans[transnr].counter >= 0) {
1638 xmlFAReduceEpsilonTransitions(ctxt, fromnr,
1639 to->trans[transnr].to,
1640 to->trans[transnr].counter);
1641 } else {
1642 xmlFAReduceEpsilonTransitions(ctxt, fromnr,
1643 to->trans[transnr].to,
1644 counter);
1645 }
1646 }
Daniel Veillard4255d502002-04-16 15:50:10 +00001647 }
1648 } else {
1649 int newto = to->trans[transnr].to;
1650
Daniel Veillardb509f152002-04-17 16:28:10 +00001651 if (to->trans[transnr].counter >= 0) {
1652 xmlRegStateAddTrans(ctxt, from, to->trans[transnr].atom,
1653 ctxt->states[newto],
Daniel Veillard5de09382005-09-26 17:18:17 +00001654 to->trans[transnr].counter, -1);
Daniel Veillardb509f152002-04-17 16:28:10 +00001655 } else {
1656 xmlRegStateAddTrans(ctxt, from, to->trans[transnr].atom,
Daniel Veillard5de09382005-09-26 17:18:17 +00001657 ctxt->states[newto], counter, -1);
Daniel Veillardb509f152002-04-17 16:28:10 +00001658 }
Daniel Veillard4255d502002-04-16 15:50:10 +00001659 }
1660 }
1661 to->mark = XML_REGEXP_MARK_NORMAL;
1662}
1663
1664/**
Daniel Veillarddb68b742005-07-30 13:18:24 +00001665 * xmlFAEliminateSimpleEpsilonTransitions:
1666 * @ctxt: a regexp parser context
1667 *
1668 * Eliminating general epsilon transitions can get costly in the general
1669 * algorithm due to the large amount of generated new transitions and
1670 * associated comparisons. However for simple epsilon transition used just
1671 * to separate building blocks when generating the automata this can be
1672 * reduced to state elimination:
1673 * - if there exists an epsilon from X to Y
1674 * - if there is no other transition from X
1675 * then X and Y are semantically equivalent and X can be eliminated
1676 * If X is the start state then make Y the start state, else replace the
1677 * target of all transitions to X by transitions to Y.
1678 */
1679static void
1680xmlFAEliminateSimpleEpsilonTransitions(xmlRegParserCtxtPtr ctxt) {
1681 int statenr, i, j, newto;
1682 xmlRegStatePtr state, tmp;
1683
1684 for (statenr = 0;statenr < ctxt->nbStates;statenr++) {
1685 state = ctxt->states[statenr];
1686 if (state == NULL)
1687 continue;
1688 if (state->nbTrans != 1)
1689 continue;
1690 /* is the only transition out a basic transition */
1691 if ((state->trans[0].atom == NULL) &&
1692 (state->trans[0].to >= 0) &&
1693 (state->trans[0].to != statenr) &&
1694 (state->trans[0].counter < 0) &&
1695 (state->trans[0].count < 0)) {
1696 newto = state->trans[0].to;
1697
1698 if (state->type == XML_REGEXP_START_STATE) {
1699#ifdef DEBUG_REGEXP_GRAPH
1700 printf("Found simple epsilon trans from start %d to %d\n",
1701 statenr, newto);
1702#endif
1703 } else {
1704#ifdef DEBUG_REGEXP_GRAPH
1705 printf("Found simple epsilon trans from %d to %d\n",
1706 statenr, newto);
1707#endif
1708 for (i = 0;i < state->nbTransTo;i++) {
1709 tmp = ctxt->states[state->transTo[i]];
1710 for (j = 0;j < tmp->nbTrans;j++) {
1711 if (tmp->trans[j].to == statenr) {
1712 tmp->trans[j].to = newto;
1713#ifdef DEBUG_REGEXP_GRAPH
1714 printf("Changed transition %d on %d to go to %d\n",
1715 j, tmp->no, newto);
1716#endif
1717 xmlRegStateAddTransTo(ctxt, ctxt->states[newto],
1718 tmp->no);
1719 }
1720 }
1721 }
1722#if 0
1723 for (i = 0;i < ctxt->nbStates;i++) {
1724 tmp = ctxt->states[i];
1725 for (j = 0;j < tmp->nbTrans;j++) {
1726 if (tmp->trans[j].to == statenr) {
1727 tmp->trans[j].to = newto;
1728#ifdef DEBUG_REGEXP_GRAPH
1729 printf("Changed transition %d on %d to go to %d\n",
1730 j, tmp->no, newto);
1731#endif
1732 }
1733 }
1734 }
1735#endif
1736 if (state->type == XML_REGEXP_FINAL_STATE)
1737 ctxt->states[newto]->type = XML_REGEXP_FINAL_STATE;
1738 /* eliminate the transition completely */
1739 state->nbTrans = 0;
1740
1741
1742 }
1743
1744 }
1745 }
1746}
1747/**
Daniel Veillard4255d502002-04-16 15:50:10 +00001748 * xmlFAEliminateEpsilonTransitions:
Daniel Veillard441bc322002-04-20 17:38:48 +00001749 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00001750 *
1751 */
1752static void
1753xmlFAEliminateEpsilonTransitions(xmlRegParserCtxtPtr ctxt) {
1754 int statenr, transnr;
1755 xmlRegStatePtr state;
Daniel Veillarddb68b742005-07-30 13:18:24 +00001756 int has_epsilon;
Daniel Veillard4255d502002-04-16 15:50:10 +00001757
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001758 if (ctxt->states == NULL) return;
1759
Daniel Veillarddb68b742005-07-30 13:18:24 +00001760 xmlFAEliminateSimpleEpsilonTransitions(ctxt);
1761
1762 has_epsilon = 0;
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001763
Daniel Veillard4255d502002-04-16 15:50:10 +00001764 /*
1765 * build the completed transitions bypassing the epsilons
1766 * Use a marking algorithm to avoid loops
Daniel Veillardcc026dc2005-01-12 13:21:17 +00001767 * mark sink states too.
Daniel Veillard4255d502002-04-16 15:50:10 +00001768 */
1769 for (statenr = 0;statenr < ctxt->nbStates;statenr++) {
1770 state = ctxt->states[statenr];
1771 if (state == NULL)
1772 continue;
Daniel Veillardcc026dc2005-01-12 13:21:17 +00001773 if ((state->nbTrans == 0) &&
1774 (state->type != XML_REGEXP_FINAL_STATE)) {
1775 state->type = XML_REGEXP_SINK_STATE;
1776 }
Daniel Veillard4255d502002-04-16 15:50:10 +00001777 for (transnr = 0;transnr < state->nbTrans;transnr++) {
1778 if ((state->trans[transnr].atom == NULL) &&
1779 (state->trans[transnr].to >= 0)) {
1780 if (state->trans[transnr].to == statenr) {
1781 state->trans[transnr].to = -1;
1782#ifdef DEBUG_REGEXP_GRAPH
1783 printf("Removed loopback epsilon trans %d on %d\n",
1784 transnr, statenr);
1785#endif
1786 } else if (state->trans[transnr].count < 0) {
1787 int newto = state->trans[transnr].to;
1788
1789#ifdef DEBUG_REGEXP_GRAPH
1790 printf("Found epsilon trans %d from %d to %d\n",
1791 transnr, statenr, newto);
1792#endif
1793 state->mark = XML_REGEXP_MARK_START;
Daniel Veillarddb68b742005-07-30 13:18:24 +00001794 has_epsilon = 1;
Daniel Veillard4255d502002-04-16 15:50:10 +00001795 xmlFAReduceEpsilonTransitions(ctxt, statenr,
1796 newto, state->trans[transnr].counter);
1797 state->mark = XML_REGEXP_MARK_NORMAL;
1798#ifdef DEBUG_REGEXP_GRAPH
1799 } else {
1800 printf("Found counted transition %d on %d\n",
1801 transnr, statenr);
1802#endif
1803 }
1804 }
1805 }
1806 }
1807 /*
1808 * Eliminate the epsilon transitions
1809 */
Daniel Veillarddb68b742005-07-30 13:18:24 +00001810 if (has_epsilon) {
1811 for (statenr = 0;statenr < ctxt->nbStates;statenr++) {
1812 state = ctxt->states[statenr];
1813 if (state == NULL)
1814 continue;
1815 for (transnr = 0;transnr < state->nbTrans;transnr++) {
1816 xmlRegTransPtr trans = &(state->trans[transnr]);
1817 if ((trans->atom == NULL) &&
1818 (trans->count < 0) &&
1819 (trans->to >= 0)) {
1820 trans->to = -1;
1821 }
Daniel Veillard4255d502002-04-16 15:50:10 +00001822 }
1823 }
1824 }
Daniel Veillard23e73572002-09-19 19:56:43 +00001825
1826 /*
1827 * Use this pass to detect unreachable states too
1828 */
1829 for (statenr = 0;statenr < ctxt->nbStates;statenr++) {
1830 state = ctxt->states[statenr];
1831 if (state != NULL)
William M. Brack779af002003-08-01 15:55:39 +00001832 state->reached = XML_REGEXP_MARK_NORMAL;
Daniel Veillard23e73572002-09-19 19:56:43 +00001833 }
1834 state = ctxt->states[0];
1835 if (state != NULL)
William M. Brack779af002003-08-01 15:55:39 +00001836 state->reached = XML_REGEXP_MARK_START;
Daniel Veillard23e73572002-09-19 19:56:43 +00001837 while (state != NULL) {
1838 xmlRegStatePtr target = NULL;
William M. Brack779af002003-08-01 15:55:39 +00001839 state->reached = XML_REGEXP_MARK_VISITED;
Daniel Veillard23e73572002-09-19 19:56:43 +00001840 /*
William M. Brackddf71d62004-05-06 04:17:26 +00001841 * Mark all states reachable from the current reachable state
Daniel Veillard23e73572002-09-19 19:56:43 +00001842 */
1843 for (transnr = 0;transnr < state->nbTrans;transnr++) {
1844 if ((state->trans[transnr].to >= 0) &&
1845 ((state->trans[transnr].atom != NULL) ||
1846 (state->trans[transnr].count >= 0))) {
1847 int newto = state->trans[transnr].to;
1848
1849 if (ctxt->states[newto] == NULL)
1850 continue;
William M. Brack779af002003-08-01 15:55:39 +00001851 if (ctxt->states[newto]->reached == XML_REGEXP_MARK_NORMAL) {
1852 ctxt->states[newto]->reached = XML_REGEXP_MARK_START;
Daniel Veillard23e73572002-09-19 19:56:43 +00001853 target = ctxt->states[newto];
1854 }
1855 }
1856 }
Daniel Veillardcc026dc2005-01-12 13:21:17 +00001857
Daniel Veillard23e73572002-09-19 19:56:43 +00001858 /*
1859 * find the next accessible state not explored
1860 */
1861 if (target == NULL) {
1862 for (statenr = 1;statenr < ctxt->nbStates;statenr++) {
1863 state = ctxt->states[statenr];
William M. Brack779af002003-08-01 15:55:39 +00001864 if ((state != NULL) && (state->reached ==
1865 XML_REGEXP_MARK_START)) {
Daniel Veillard23e73572002-09-19 19:56:43 +00001866 target = state;
1867 break;
1868 }
1869 }
1870 }
1871 state = target;
1872 }
1873 for (statenr = 0;statenr < ctxt->nbStates;statenr++) {
1874 state = ctxt->states[statenr];
William M. Brack779af002003-08-01 15:55:39 +00001875 if ((state != NULL) && (state->reached == XML_REGEXP_MARK_NORMAL)) {
Daniel Veillard23e73572002-09-19 19:56:43 +00001876#ifdef DEBUG_REGEXP_GRAPH
1877 printf("Removed unreachable state %d\n", statenr);
1878#endif
1879 xmlRegFreeState(state);
1880 ctxt->states[statenr] = NULL;
1881 }
1882 }
1883
Daniel Veillard4255d502002-04-16 15:50:10 +00001884}
1885
Daniel Veillard567a45b2005-10-18 19:11:55 +00001886static int
1887xmlFACompareRanges(xmlRegRangePtr range1, xmlRegRangePtr range2) {
1888 int ret = 0;
1889
1890 if ((range1->type == XML_REGEXP_RANGES) ||
1891 (range2->type == XML_REGEXP_RANGES) ||
1892 (range2->type == XML_REGEXP_SUBREG) ||
1893 (range1->type == XML_REGEXP_SUBREG) ||
1894 (range1->type == XML_REGEXP_STRING) ||
1895 (range2->type == XML_REGEXP_STRING))
1896 return(-1);
1897
1898 /* put them in order */
1899 if (range1->type > range2->type) {
1900 xmlRegRangePtr tmp;
1901
1902 tmp = range1;
1903 range1 = range2;
1904 range2 = tmp;
1905 }
1906 if ((range1->type == XML_REGEXP_ANYCHAR) ||
1907 (range2->type == XML_REGEXP_ANYCHAR)) {
1908 ret = 1;
1909 } else if ((range1->type == XML_REGEXP_EPSILON) ||
1910 (range2->type == XML_REGEXP_EPSILON)) {
1911 return(0);
1912 } else if (range1->type == range2->type) {
1913 if ((range1->type != XML_REGEXP_CHARVAL) ||
1914 (range1->end < range2->start) ||
1915 (range2->end < range1->start))
1916 ret = 1;
1917 else
1918 ret = 0;
1919 } else if (range1->type == XML_REGEXP_CHARVAL) {
1920 int codepoint;
1921 int neg = 0;
1922
1923 /*
1924 * just check all codepoints in the range for acceptance,
1925 * this is usually way cheaper since done only once at
1926 * compilation than testing over and over at runtime or
1927 * pushing too many states when evaluating.
1928 */
1929 if (((range1->neg == 0) && (range2->neg != 0)) ||
1930 ((range1->neg != 0) && (range2->neg == 0)))
1931 neg = 1;
1932
1933 for (codepoint = range1->start;codepoint <= range1->end ;codepoint++) {
1934 ret = xmlRegCheckCharacterRange(range2->type, codepoint,
1935 0, range2->start, range2->end,
1936 range2->blockName);
1937 if (ret < 0)
1938 return(-1);
1939 if (((neg == 1) && (ret == 0)) ||
1940 ((neg == 0) && (ret == 1)))
1941 return(1);
1942 }
1943 return(0);
1944 } else if ((range1->type == XML_REGEXP_BLOCK_NAME) ||
1945 (range2->type == XML_REGEXP_BLOCK_NAME)) {
1946 if (range1->type == range2->type) {
1947 ret = xmlStrEqual(range1->blockName, range2->blockName);
1948 } else {
1949 /*
1950 * comparing a block range with anything else is way
1951 * too costly, and maintining the table is like too much
1952 * memory too, so let's force the automata to save state
1953 * here.
1954 */
1955 return(1);
1956 }
1957 } else if ((range1->type < XML_REGEXP_LETTER) ||
1958 (range2->type < XML_REGEXP_LETTER)) {
1959 if ((range1->type == XML_REGEXP_ANYSPACE) &&
1960 (range2->type == XML_REGEXP_NOTSPACE))
1961 ret = 0;
1962 else if ((range1->type == XML_REGEXP_INITNAME) &&
1963 (range2->type == XML_REGEXP_NOTINITNAME))
1964 ret = 0;
1965 else if ((range1->type == XML_REGEXP_NAMECHAR) &&
1966 (range2->type == XML_REGEXP_NOTNAMECHAR))
1967 ret = 0;
1968 else if ((range1->type == XML_REGEXP_DECIMAL) &&
1969 (range2->type == XML_REGEXP_NOTDECIMAL))
1970 ret = 0;
1971 else if ((range1->type == XML_REGEXP_REALCHAR) &&
1972 (range2->type == XML_REGEXP_NOTREALCHAR))
1973 ret = 0;
1974 else {
1975 /* same thing to limit complexity */
1976 return(1);
1977 }
1978 } else {
1979 ret = 0;
1980 /* range1->type < range2->type here */
1981 switch (range1->type) {
1982 case XML_REGEXP_LETTER:
1983 /* all disjoint except in the subgroups */
1984 if ((range2->type == XML_REGEXP_LETTER_UPPERCASE) ||
1985 (range2->type == XML_REGEXP_LETTER_LOWERCASE) ||
1986 (range2->type == XML_REGEXP_LETTER_TITLECASE) ||
1987 (range2->type == XML_REGEXP_LETTER_MODIFIER) ||
1988 (range2->type == XML_REGEXP_LETTER_OTHERS))
1989 ret = 1;
1990 break;
1991 case XML_REGEXP_MARK:
1992 if ((range2->type == XML_REGEXP_MARK_NONSPACING) ||
1993 (range2->type == XML_REGEXP_MARK_SPACECOMBINING) ||
1994 (range2->type == XML_REGEXP_MARK_ENCLOSING))
1995 ret = 1;
1996 break;
1997 case XML_REGEXP_NUMBER:
1998 if ((range2->type == XML_REGEXP_NUMBER_DECIMAL) ||
1999 (range2->type == XML_REGEXP_NUMBER_LETTER) ||
2000 (range2->type == XML_REGEXP_NUMBER_OTHERS))
2001 ret = 1;
2002 break;
2003 case XML_REGEXP_PUNCT:
2004 if ((range2->type == XML_REGEXP_PUNCT_CONNECTOR) ||
2005 (range2->type == XML_REGEXP_PUNCT_DASH) ||
2006 (range2->type == XML_REGEXP_PUNCT_OPEN) ||
2007 (range2->type == XML_REGEXP_PUNCT_CLOSE) ||
2008 (range2->type == XML_REGEXP_PUNCT_INITQUOTE) ||
2009 (range2->type == XML_REGEXP_PUNCT_FINQUOTE) ||
2010 (range2->type == XML_REGEXP_PUNCT_OTHERS))
2011 ret = 1;
2012 break;
2013 case XML_REGEXP_SEPAR:
2014 if ((range2->type == XML_REGEXP_SEPAR_SPACE) ||
2015 (range2->type == XML_REGEXP_SEPAR_LINE) ||
2016 (range2->type == XML_REGEXP_SEPAR_PARA))
2017 ret = 1;
2018 break;
2019 case XML_REGEXP_SYMBOL:
2020 if ((range2->type == XML_REGEXP_SYMBOL_MATH) ||
2021 (range2->type == XML_REGEXP_SYMBOL_CURRENCY) ||
2022 (range2->type == XML_REGEXP_SYMBOL_MODIFIER) ||
2023 (range2->type == XML_REGEXP_SYMBOL_OTHERS))
2024 ret = 1;
2025 break;
2026 case XML_REGEXP_OTHER:
2027 if ((range2->type == XML_REGEXP_OTHER_CONTROL) ||
2028 (range2->type == XML_REGEXP_OTHER_FORMAT) ||
2029 (range2->type == XML_REGEXP_OTHER_PRIVATE))
2030 ret = 1;
2031 break;
2032 default:
2033 if ((range2->type >= XML_REGEXP_LETTER) &&
2034 (range2->type < XML_REGEXP_BLOCK_NAME))
2035 ret = 0;
2036 else {
2037 /* safety net ! */
2038 return(1);
2039 }
2040 }
2041 }
2042 if (((range1->neg == 0) && (range2->neg != 0)) ||
2043 ((range1->neg != 0) && (range2->neg == 0)))
2044 ret = !ret;
2045 return(1);
2046}
2047
Daniel Veillarde19fc232002-04-22 16:01:24 +00002048/**
2049 * xmlFACompareAtoms:
2050 * @atom1: an atom
2051 * @atom2: an atom
2052 *
Daniel Veillard567a45b2005-10-18 19:11:55 +00002053 * Compares two atoms to check whether they intersect in some ways,
2054 * this is used by xmlFAComputesDeterminism only
Daniel Veillarde19fc232002-04-22 16:01:24 +00002055 *
2056 * Returns 1 if yes and 0 otherwise
2057 */
2058static int
2059xmlFACompareAtoms(xmlRegAtomPtr atom1, xmlRegAtomPtr atom2) {
Daniel Veillard9efc4762005-07-19 14:33:55 +00002060 int ret;
2061
Daniel Veillarde19fc232002-04-22 16:01:24 +00002062 if (atom1 == atom2)
2063 return(1);
2064 if ((atom1 == NULL) || (atom2 == NULL))
2065 return(0);
2066
Daniel Veillard567a45b2005-10-18 19:11:55 +00002067 if ((atom1->type == XML_REGEXP_RANGES) &&
2068 (atom2->type == XML_REGEXP_CHARVAL)) {
2069 } else if ((atom1->type == XML_REGEXP_CHARVAL) &&
2070 (atom2->type == XML_REGEXP_RANGES)) {
2071 xmlRegAtomPtr tmp;
2072 tmp = atom1;
2073 atom1 = atom2;
2074 atom2 = tmp;
2075 } else if (atom1->type != atom2->type) {
Daniel Veillarde19fc232002-04-22 16:01:24 +00002076 return(0);
Daniel Veillard567a45b2005-10-18 19:11:55 +00002077 }
Daniel Veillarde19fc232002-04-22 16:01:24 +00002078 switch (atom1->type) {
2079 case XML_REGEXP_STRING:
Daniel Veillard9efc4762005-07-19 14:33:55 +00002080 ret = xmlRegStrEqualWildcard((xmlChar *)atom1->valuep,
2081 (xmlChar *)atom2->valuep);
2082 break;
Daniel Veillarde19fc232002-04-22 16:01:24 +00002083 case XML_REGEXP_EPSILON:
Daniel Veillard567a45b2005-10-18 19:11:55 +00002084 goto not_determinist;
Daniel Veillarde19fc232002-04-22 16:01:24 +00002085 case XML_REGEXP_CHARVAL:
Daniel Veillard567a45b2005-10-18 19:11:55 +00002086 ret = (atom1->codepoint == atom2->codepoint);
Daniel Veillard9efc4762005-07-19 14:33:55 +00002087 break;
Daniel Veillarde19fc232002-04-22 16:01:24 +00002088 case XML_REGEXP_RANGES:
Daniel Veillard567a45b2005-10-18 19:11:55 +00002089 if (atom2->type == XML_REGEXP_CHARVAL) {
2090 ret = xmlRegCheckCharacter(atom1, atom2->codepoint);
2091 if (ret < 0)
2092 return(-1);
2093 break;
2094 } else {
2095 int i, j, res;
2096 xmlRegRangePtr r1, r2;
2097
2098 /*
2099 * need to check that none of the ranges eventually matches
2100 */
2101 for (i = 0;i < atom1->nbRanges;i++) {
2102 for (j = 0;j < atom2->nbRanges;j++) {
2103 r1 = atom1->ranges[i];
2104 r2 = atom2->ranges[j];
2105 res = xmlFACompareRanges(r1, r2);
2106 if (res == 1) {
2107 ret = 1;
2108 goto done;
2109 }
2110 }
2111 }
2112 ret = 0;
2113 }
2114 break;
Daniel Veillarde19fc232002-04-22 16:01:24 +00002115 default:
Daniel Veillard567a45b2005-10-18 19:11:55 +00002116 goto not_determinist;
Daniel Veillarde19fc232002-04-22 16:01:24 +00002117 }
Daniel Veillard567a45b2005-10-18 19:11:55 +00002118done:
Daniel Veillard6e65e152005-08-09 11:09:52 +00002119 if (atom1->neg != atom2->neg) {
Daniel Veillard9efc4762005-07-19 14:33:55 +00002120 ret = !ret;
Daniel Veillard6e65e152005-08-09 11:09:52 +00002121 }
Daniel Veillard567a45b2005-10-18 19:11:55 +00002122 if (ret == 0)
2123 return(0);
2124not_determinist:
2125 return(1);
Daniel Veillarde19fc232002-04-22 16:01:24 +00002126}
2127
2128/**
2129 * xmlFARecurseDeterminism:
2130 * @ctxt: a regexp parser context
2131 *
2132 * Check whether the associated regexp is determinist,
2133 * should be called after xmlFAEliminateEpsilonTransitions()
2134 *
2135 */
2136static int
2137xmlFARecurseDeterminism(xmlRegParserCtxtPtr ctxt, xmlRegStatePtr state,
2138 int to, xmlRegAtomPtr atom) {
2139 int ret = 1;
Daniel Veillard567a45b2005-10-18 19:11:55 +00002140 int res;
Daniel Veillard5de09382005-09-26 17:18:17 +00002141 int transnr, nbTrans;
Daniel Veillarde19fc232002-04-22 16:01:24 +00002142 xmlRegTransPtr t1;
2143
2144 if (state == NULL)
2145 return(ret);
Daniel Veillard5de09382005-09-26 17:18:17 +00002146 /*
2147 * don't recurse on transitions potentially added in the course of
2148 * the elimination.
2149 */
2150 nbTrans = state->nbTrans;
2151 for (transnr = 0;transnr < nbTrans;transnr++) {
Daniel Veillarde19fc232002-04-22 16:01:24 +00002152 t1 = &(state->trans[transnr]);
2153 /*
2154 * check transitions conflicting with the one looked at
2155 */
2156 if (t1->atom == NULL) {
2157 if (t1->to == -1)
2158 continue;
Daniel Veillard567a45b2005-10-18 19:11:55 +00002159 res = xmlFARecurseDeterminism(ctxt, ctxt->states[t1->to],
Daniel Veillarde19fc232002-04-22 16:01:24 +00002160 to, atom);
Daniel Veillard567a45b2005-10-18 19:11:55 +00002161 if (res == 0) {
2162 ret = 0;
2163 t1->nd = 1;
2164 }
Daniel Veillarde19fc232002-04-22 16:01:24 +00002165 continue;
2166 }
2167 if (t1->to != to)
2168 continue;
Daniel Veillard567a45b2005-10-18 19:11:55 +00002169 if (xmlFACompareAtoms(t1->atom, atom)) {
2170 ret = 0;
2171 /* mark the transition as non-deterministic */
2172 t1->nd = 1;
2173 }
Daniel Veillarde19fc232002-04-22 16:01:24 +00002174 }
2175 return(ret);
2176}
2177
2178/**
2179 * xmlFAComputesDeterminism:
2180 * @ctxt: a regexp parser context
2181 *
2182 * Check whether the associated regexp is determinist,
2183 * should be called after xmlFAEliminateEpsilonTransitions()
2184 *
2185 */
2186static int
2187xmlFAComputesDeterminism(xmlRegParserCtxtPtr ctxt) {
2188 int statenr, transnr;
2189 xmlRegStatePtr state;
Daniel Veillard567a45b2005-10-18 19:11:55 +00002190 xmlRegTransPtr t1, t2, last;
Daniel Veillarde19fc232002-04-22 16:01:24 +00002191 int i;
2192 int ret = 1;
2193
Daniel Veillard4402ab42002-09-12 16:02:56 +00002194#ifdef DEBUG_REGEXP_GRAPH
2195 printf("xmlFAComputesDeterminism\n");
2196 xmlRegPrintCtxt(stdout, ctxt);
2197#endif
Daniel Veillarde19fc232002-04-22 16:01:24 +00002198 if (ctxt->determinist != -1)
2199 return(ctxt->determinist);
2200
2201 /*
Daniel Veillard567a45b2005-10-18 19:11:55 +00002202 * First cleanup the automata removing cancelled transitions
Daniel Veillarde19fc232002-04-22 16:01:24 +00002203 */
2204 for (statenr = 0;statenr < ctxt->nbStates;statenr++) {
2205 state = ctxt->states[statenr];
2206 if (state == NULL)
2207 continue;
Daniel Veillard4f82c8a2005-08-09 21:40:08 +00002208 if (state->nbTrans < 2)
2209 continue;
Daniel Veillarde19fc232002-04-22 16:01:24 +00002210 for (transnr = 0;transnr < state->nbTrans;transnr++) {
2211 t1 = &(state->trans[transnr]);
2212 /*
2213 * Determinism checks in case of counted or all transitions
2214 * will have to be handled separately
2215 */
Daniel Veillard567a45b2005-10-18 19:11:55 +00002216 if (t1->atom == NULL) {
2217 t1->nd = 1;
Daniel Veillarde19fc232002-04-22 16:01:24 +00002218 continue;
Daniel Veillard567a45b2005-10-18 19:11:55 +00002219 }
Daniel Veillarde19fc232002-04-22 16:01:24 +00002220 if (t1->to == -1) /* eliminated */
2221 continue;
2222 for (i = 0;i < transnr;i++) {
2223 t2 = &(state->trans[i]);
2224 if (t2->to == -1) /* eliminated */
2225 continue;
2226 if (t2->atom != NULL) {
2227 if (t1->to == t2->to) {
2228 if (xmlFACompareAtoms(t1->atom, t2->atom))
William M. Brackddf71d62004-05-06 04:17:26 +00002229 t2->to = -1; /* eliminated */
Daniel Veillard567a45b2005-10-18 19:11:55 +00002230 }
2231 }
2232 }
2233 }
2234 }
2235
2236 /*
2237 * Check for all states that there aren't 2 transitions
2238 * with the same atom and a different target.
2239 */
2240 for (statenr = 0;statenr < ctxt->nbStates;statenr++) {
2241 state = ctxt->states[statenr];
2242 if (state == NULL)
2243 continue;
2244 if (state->nbTrans < 2)
2245 continue;
2246 last = NULL;
2247 for (transnr = 0;transnr < state->nbTrans;transnr++) {
2248 t1 = &(state->trans[transnr]);
2249 /*
2250 * Determinism checks in case of counted or all transitions
2251 * will have to be handled separately
2252 */
2253 if (t1->atom == NULL) {
2254 continue;
2255 }
2256 if (t1->to == -1) /* eliminated */
2257 continue;
2258 for (i = 0;i < transnr;i++) {
2259 t2 = &(state->trans[i]);
2260 if (t2->to == -1) /* eliminated */
2261 continue;
2262 if (t2->atom != NULL) {
2263 /* not determinist ! */
2264 if (xmlFACompareAtoms(t1->atom, t2->atom)) {
2265 ret = 0;
2266 /* mark the transitions as non-deterministic ones */
2267 t1->nd = 1;
2268 t2->nd = 1;
2269 last = t1;
Daniel Veillarde19fc232002-04-22 16:01:24 +00002270 }
2271 } else if (t1->to != -1) {
2272 /*
2273 * do the closure in case of remaining specific
2274 * epsilon transitions like choices or all
2275 */
2276 ret = xmlFARecurseDeterminism(ctxt, ctxt->states[t1->to],
2277 t2->to, t2->atom);
Daniel Veillard567a45b2005-10-18 19:11:55 +00002278 /* don't shortcut the computation so all non deterministic
2279 transition get marked down
Daniel Veillarde19fc232002-04-22 16:01:24 +00002280 if (ret == 0)
Daniel Veillard567a45b2005-10-18 19:11:55 +00002281 return(0); */
2282 if (ret == 0) {
2283 t1->nd = 1;
2284 t2->nd = 1;
2285 last = t1;
2286 }
Daniel Veillarde19fc232002-04-22 16:01:24 +00002287 }
2288 }
Daniel Veillard567a45b2005-10-18 19:11:55 +00002289 /* don't shortcut the computation so all non deterministic
2290 transition get marked down
Daniel Veillarde19fc232002-04-22 16:01:24 +00002291 if (ret == 0)
Daniel Veillard567a45b2005-10-18 19:11:55 +00002292 break; */
Daniel Veillarde19fc232002-04-22 16:01:24 +00002293 }
Daniel Veillard567a45b2005-10-18 19:11:55 +00002294
2295 /*
2296 * mark specifically the last non-deterministic transition
2297 * from a state since there is no need to set-up rollback
2298 * from it
2299 */
2300 if (last != NULL) {
2301 last->nd = 2;
2302 }
2303
2304 /* don't shortcut the computation so all non deterministic
2305 transition get marked down
Daniel Veillarde19fc232002-04-22 16:01:24 +00002306 if (ret == 0)
Daniel Veillard567a45b2005-10-18 19:11:55 +00002307 break; */
Daniel Veillarde19fc232002-04-22 16:01:24 +00002308 }
Daniel Veillard567a45b2005-10-18 19:11:55 +00002309
Daniel Veillarde19fc232002-04-22 16:01:24 +00002310 ctxt->determinist = ret;
2311 return(ret);
2312}
2313
Daniel Veillard4255d502002-04-16 15:50:10 +00002314/************************************************************************
2315 * *
2316 * Routines to check input against transition atoms *
2317 * *
2318 ************************************************************************/
2319
2320static int
2321xmlRegCheckCharacterRange(xmlRegAtomType type, int codepoint, int neg,
2322 int start, int end, const xmlChar *blockName) {
2323 int ret = 0;
2324
2325 switch (type) {
2326 case XML_REGEXP_STRING:
2327 case XML_REGEXP_SUBREG:
2328 case XML_REGEXP_RANGES:
2329 case XML_REGEXP_EPSILON:
2330 return(-1);
2331 case XML_REGEXP_ANYCHAR:
2332 ret = ((codepoint != '\n') && (codepoint != '\r'));
2333 break;
2334 case XML_REGEXP_CHARVAL:
2335 ret = ((codepoint >= start) && (codepoint <= end));
2336 break;
2337 case XML_REGEXP_NOTSPACE:
2338 neg = !neg;
2339 case XML_REGEXP_ANYSPACE:
2340 ret = ((codepoint == '\n') || (codepoint == '\r') ||
2341 (codepoint == '\t') || (codepoint == ' '));
2342 break;
2343 case XML_REGEXP_NOTINITNAME:
2344 neg = !neg;
2345 case XML_REGEXP_INITNAME:
William M. Brack871611b2003-10-18 04:53:14 +00002346 ret = (IS_LETTER(codepoint) ||
Daniel Veillard4255d502002-04-16 15:50:10 +00002347 (codepoint == '_') || (codepoint == ':'));
2348 break;
2349 case XML_REGEXP_NOTNAMECHAR:
2350 neg = !neg;
2351 case XML_REGEXP_NAMECHAR:
William M. Brack871611b2003-10-18 04:53:14 +00002352 ret = (IS_LETTER(codepoint) || IS_DIGIT(codepoint) ||
Daniel Veillard4255d502002-04-16 15:50:10 +00002353 (codepoint == '.') || (codepoint == '-') ||
2354 (codepoint == '_') || (codepoint == ':') ||
William M. Brack871611b2003-10-18 04:53:14 +00002355 IS_COMBINING(codepoint) || IS_EXTENDER(codepoint));
Daniel Veillard4255d502002-04-16 15:50:10 +00002356 break;
2357 case XML_REGEXP_NOTDECIMAL:
2358 neg = !neg;
2359 case XML_REGEXP_DECIMAL:
2360 ret = xmlUCSIsCatNd(codepoint);
2361 break;
2362 case XML_REGEXP_REALCHAR:
2363 neg = !neg;
2364 case XML_REGEXP_NOTREALCHAR:
2365 ret = xmlUCSIsCatP(codepoint);
2366 if (ret == 0)
2367 ret = xmlUCSIsCatZ(codepoint);
2368 if (ret == 0)
2369 ret = xmlUCSIsCatC(codepoint);
2370 break;
2371 case XML_REGEXP_LETTER:
2372 ret = xmlUCSIsCatL(codepoint);
2373 break;
2374 case XML_REGEXP_LETTER_UPPERCASE:
2375 ret = xmlUCSIsCatLu(codepoint);
2376 break;
2377 case XML_REGEXP_LETTER_LOWERCASE:
2378 ret = xmlUCSIsCatLl(codepoint);
2379 break;
2380 case XML_REGEXP_LETTER_TITLECASE:
2381 ret = xmlUCSIsCatLt(codepoint);
2382 break;
2383 case XML_REGEXP_LETTER_MODIFIER:
2384 ret = xmlUCSIsCatLm(codepoint);
2385 break;
2386 case XML_REGEXP_LETTER_OTHERS:
2387 ret = xmlUCSIsCatLo(codepoint);
2388 break;
2389 case XML_REGEXP_MARK:
2390 ret = xmlUCSIsCatM(codepoint);
2391 break;
2392 case XML_REGEXP_MARK_NONSPACING:
2393 ret = xmlUCSIsCatMn(codepoint);
2394 break;
2395 case XML_REGEXP_MARK_SPACECOMBINING:
2396 ret = xmlUCSIsCatMc(codepoint);
2397 break;
2398 case XML_REGEXP_MARK_ENCLOSING:
2399 ret = xmlUCSIsCatMe(codepoint);
2400 break;
2401 case XML_REGEXP_NUMBER:
2402 ret = xmlUCSIsCatN(codepoint);
2403 break;
2404 case XML_REGEXP_NUMBER_DECIMAL:
2405 ret = xmlUCSIsCatNd(codepoint);
2406 break;
2407 case XML_REGEXP_NUMBER_LETTER:
2408 ret = xmlUCSIsCatNl(codepoint);
2409 break;
2410 case XML_REGEXP_NUMBER_OTHERS:
2411 ret = xmlUCSIsCatNo(codepoint);
2412 break;
2413 case XML_REGEXP_PUNCT:
2414 ret = xmlUCSIsCatP(codepoint);
2415 break;
2416 case XML_REGEXP_PUNCT_CONNECTOR:
2417 ret = xmlUCSIsCatPc(codepoint);
2418 break;
2419 case XML_REGEXP_PUNCT_DASH:
2420 ret = xmlUCSIsCatPd(codepoint);
2421 break;
2422 case XML_REGEXP_PUNCT_OPEN:
2423 ret = xmlUCSIsCatPs(codepoint);
2424 break;
2425 case XML_REGEXP_PUNCT_CLOSE:
2426 ret = xmlUCSIsCatPe(codepoint);
2427 break;
2428 case XML_REGEXP_PUNCT_INITQUOTE:
2429 ret = xmlUCSIsCatPi(codepoint);
2430 break;
2431 case XML_REGEXP_PUNCT_FINQUOTE:
2432 ret = xmlUCSIsCatPf(codepoint);
2433 break;
2434 case XML_REGEXP_PUNCT_OTHERS:
2435 ret = xmlUCSIsCatPo(codepoint);
2436 break;
2437 case XML_REGEXP_SEPAR:
2438 ret = xmlUCSIsCatZ(codepoint);
2439 break;
2440 case XML_REGEXP_SEPAR_SPACE:
2441 ret = xmlUCSIsCatZs(codepoint);
2442 break;
2443 case XML_REGEXP_SEPAR_LINE:
2444 ret = xmlUCSIsCatZl(codepoint);
2445 break;
2446 case XML_REGEXP_SEPAR_PARA:
2447 ret = xmlUCSIsCatZp(codepoint);
2448 break;
2449 case XML_REGEXP_SYMBOL:
2450 ret = xmlUCSIsCatS(codepoint);
2451 break;
2452 case XML_REGEXP_SYMBOL_MATH:
2453 ret = xmlUCSIsCatSm(codepoint);
2454 break;
2455 case XML_REGEXP_SYMBOL_CURRENCY:
2456 ret = xmlUCSIsCatSc(codepoint);
2457 break;
2458 case XML_REGEXP_SYMBOL_MODIFIER:
2459 ret = xmlUCSIsCatSk(codepoint);
2460 break;
2461 case XML_REGEXP_SYMBOL_OTHERS:
2462 ret = xmlUCSIsCatSo(codepoint);
2463 break;
2464 case XML_REGEXP_OTHER:
2465 ret = xmlUCSIsCatC(codepoint);
2466 break;
2467 case XML_REGEXP_OTHER_CONTROL:
2468 ret = xmlUCSIsCatCc(codepoint);
2469 break;
2470 case XML_REGEXP_OTHER_FORMAT:
2471 ret = xmlUCSIsCatCf(codepoint);
2472 break;
2473 case XML_REGEXP_OTHER_PRIVATE:
2474 ret = xmlUCSIsCatCo(codepoint);
2475 break;
2476 case XML_REGEXP_OTHER_NA:
2477 /* ret = xmlUCSIsCatCn(codepoint); */
2478 /* Seems it doesn't exist anymore in recent Unicode releases */
2479 ret = 0;
2480 break;
2481 case XML_REGEXP_BLOCK_NAME:
2482 ret = xmlUCSIsBlock(codepoint, (const char *) blockName);
2483 break;
2484 }
2485 if (neg)
2486 return(!ret);
2487 return(ret);
2488}
2489
2490static int
2491xmlRegCheckCharacter(xmlRegAtomPtr atom, int codepoint) {
2492 int i, ret = 0;
2493 xmlRegRangePtr range;
2494
William M. Brack871611b2003-10-18 04:53:14 +00002495 if ((atom == NULL) || (!IS_CHAR(codepoint)))
Daniel Veillard4255d502002-04-16 15:50:10 +00002496 return(-1);
2497
2498 switch (atom->type) {
2499 case XML_REGEXP_SUBREG:
2500 case XML_REGEXP_EPSILON:
2501 return(-1);
2502 case XML_REGEXP_CHARVAL:
2503 return(codepoint == atom->codepoint);
2504 case XML_REGEXP_RANGES: {
2505 int accept = 0;
Daniel Veillardf2a12832003-11-24 13:04:35 +00002506
Daniel Veillard4255d502002-04-16 15:50:10 +00002507 for (i = 0;i < atom->nbRanges;i++) {
2508 range = atom->ranges[i];
Daniel Veillardf8b9de32003-11-24 14:27:26 +00002509 if (range->neg == 2) {
Daniel Veillard4255d502002-04-16 15:50:10 +00002510 ret = xmlRegCheckCharacterRange(range->type, codepoint,
2511 0, range->start, range->end,
2512 range->blockName);
2513 if (ret != 0)
2514 return(0); /* excluded char */
Daniel Veillardf8b9de32003-11-24 14:27:26 +00002515 } else if (range->neg) {
2516 ret = xmlRegCheckCharacterRange(range->type, codepoint,
2517 0, range->start, range->end,
2518 range->blockName);
2519 if (ret == 0)
Daniel Veillardf2a12832003-11-24 13:04:35 +00002520 accept = 1;
Daniel Veillardf8b9de32003-11-24 14:27:26 +00002521 else
2522 return(0);
Daniel Veillard4255d502002-04-16 15:50:10 +00002523 } else {
2524 ret = xmlRegCheckCharacterRange(range->type, codepoint,
2525 0, range->start, range->end,
2526 range->blockName);
2527 if (ret != 0)
2528 accept = 1; /* might still be excluded */
2529 }
2530 }
2531 return(accept);
2532 }
2533 case XML_REGEXP_STRING:
2534 printf("TODO: XML_REGEXP_STRING\n");
2535 return(-1);
2536 case XML_REGEXP_ANYCHAR:
2537 case XML_REGEXP_ANYSPACE:
2538 case XML_REGEXP_NOTSPACE:
2539 case XML_REGEXP_INITNAME:
2540 case XML_REGEXP_NOTINITNAME:
2541 case XML_REGEXP_NAMECHAR:
2542 case XML_REGEXP_NOTNAMECHAR:
2543 case XML_REGEXP_DECIMAL:
2544 case XML_REGEXP_NOTDECIMAL:
2545 case XML_REGEXP_REALCHAR:
2546 case XML_REGEXP_NOTREALCHAR:
2547 case XML_REGEXP_LETTER:
2548 case XML_REGEXP_LETTER_UPPERCASE:
2549 case XML_REGEXP_LETTER_LOWERCASE:
2550 case XML_REGEXP_LETTER_TITLECASE:
2551 case XML_REGEXP_LETTER_MODIFIER:
2552 case XML_REGEXP_LETTER_OTHERS:
2553 case XML_REGEXP_MARK:
2554 case XML_REGEXP_MARK_NONSPACING:
2555 case XML_REGEXP_MARK_SPACECOMBINING:
2556 case XML_REGEXP_MARK_ENCLOSING:
2557 case XML_REGEXP_NUMBER:
2558 case XML_REGEXP_NUMBER_DECIMAL:
2559 case XML_REGEXP_NUMBER_LETTER:
2560 case XML_REGEXP_NUMBER_OTHERS:
2561 case XML_REGEXP_PUNCT:
2562 case XML_REGEXP_PUNCT_CONNECTOR:
2563 case XML_REGEXP_PUNCT_DASH:
2564 case XML_REGEXP_PUNCT_OPEN:
2565 case XML_REGEXP_PUNCT_CLOSE:
2566 case XML_REGEXP_PUNCT_INITQUOTE:
2567 case XML_REGEXP_PUNCT_FINQUOTE:
2568 case XML_REGEXP_PUNCT_OTHERS:
2569 case XML_REGEXP_SEPAR:
2570 case XML_REGEXP_SEPAR_SPACE:
2571 case XML_REGEXP_SEPAR_LINE:
2572 case XML_REGEXP_SEPAR_PARA:
2573 case XML_REGEXP_SYMBOL:
2574 case XML_REGEXP_SYMBOL_MATH:
2575 case XML_REGEXP_SYMBOL_CURRENCY:
2576 case XML_REGEXP_SYMBOL_MODIFIER:
2577 case XML_REGEXP_SYMBOL_OTHERS:
2578 case XML_REGEXP_OTHER:
2579 case XML_REGEXP_OTHER_CONTROL:
2580 case XML_REGEXP_OTHER_FORMAT:
2581 case XML_REGEXP_OTHER_PRIVATE:
2582 case XML_REGEXP_OTHER_NA:
2583 case XML_REGEXP_BLOCK_NAME:
2584 ret = xmlRegCheckCharacterRange(atom->type, codepoint, 0, 0, 0,
2585 (const xmlChar *)atom->valuep);
2586 if (atom->neg)
2587 ret = !ret;
2588 break;
2589 }
2590 return(ret);
2591}
2592
2593/************************************************************************
2594 * *
William M. Brackddf71d62004-05-06 04:17:26 +00002595 * Saving and restoring state of an execution context *
Daniel Veillard4255d502002-04-16 15:50:10 +00002596 * *
2597 ************************************************************************/
2598
2599#ifdef DEBUG_REGEXP_EXEC
2600static void
2601xmlFARegDebugExec(xmlRegExecCtxtPtr exec) {
2602 printf("state: %d:%d:idx %d", exec->state->no, exec->transno, exec->index);
2603 if (exec->inputStack != NULL) {
2604 int i;
2605 printf(": ");
2606 for (i = 0;(i < 3) && (i < exec->inputStackNr);i++)
2607 printf("%s ", exec->inputStack[exec->inputStackNr - (i + 1)]);
2608 } else {
2609 printf(": %s", &(exec->inputString[exec->index]));
2610 }
2611 printf("\n");
2612}
2613#endif
2614
2615static void
2616xmlFARegExecSave(xmlRegExecCtxtPtr exec) {
2617#ifdef DEBUG_REGEXP_EXEC
2618 printf("saving ");
2619 exec->transno++;
2620 xmlFARegDebugExec(exec);
2621 exec->transno--;
2622#endif
Daniel Veillard94cc1032005-09-15 13:09:00 +00002623#ifdef MAX_PUSH
2624 if (exec->nbPush > MAX_PUSH) {
2625 return;
2626 }
2627 exec->nbPush++;
2628#endif
Daniel Veillard4255d502002-04-16 15:50:10 +00002629
2630 if (exec->maxRollbacks == 0) {
2631 exec->maxRollbacks = 4;
2632 exec->rollbacks = (xmlRegExecRollback *) xmlMalloc(exec->maxRollbacks *
2633 sizeof(xmlRegExecRollback));
2634 if (exec->rollbacks == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00002635 xmlRegexpErrMemory(NULL, "saving regexp");
Daniel Veillard4255d502002-04-16 15:50:10 +00002636 exec->maxRollbacks = 0;
2637 return;
2638 }
2639 memset(exec->rollbacks, 0,
2640 exec->maxRollbacks * sizeof(xmlRegExecRollback));
2641 } else if (exec->nbRollbacks >= exec->maxRollbacks) {
2642 xmlRegExecRollback *tmp;
2643 int len = exec->maxRollbacks;
2644
2645 exec->maxRollbacks *= 2;
2646 tmp = (xmlRegExecRollback *) xmlRealloc(exec->rollbacks,
2647 exec->maxRollbacks * sizeof(xmlRegExecRollback));
2648 if (tmp == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00002649 xmlRegexpErrMemory(NULL, "saving regexp");
Daniel Veillard4255d502002-04-16 15:50:10 +00002650 exec->maxRollbacks /= 2;
2651 return;
2652 }
2653 exec->rollbacks = tmp;
2654 tmp = &exec->rollbacks[len];
2655 memset(tmp, 0, (exec->maxRollbacks - len) * sizeof(xmlRegExecRollback));
2656 }
2657 exec->rollbacks[exec->nbRollbacks].state = exec->state;
2658 exec->rollbacks[exec->nbRollbacks].index = exec->index;
2659 exec->rollbacks[exec->nbRollbacks].nextbranch = exec->transno + 1;
2660 if (exec->comp->nbCounters > 0) {
2661 if (exec->rollbacks[exec->nbRollbacks].counts == NULL) {
2662 exec->rollbacks[exec->nbRollbacks].counts = (int *)
2663 xmlMalloc(exec->comp->nbCounters * sizeof(int));
2664 if (exec->rollbacks[exec->nbRollbacks].counts == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00002665 xmlRegexpErrMemory(NULL, "saving regexp");
Daniel Veillard4255d502002-04-16 15:50:10 +00002666 exec->status = -5;
2667 return;
2668 }
2669 }
2670 memcpy(exec->rollbacks[exec->nbRollbacks].counts, exec->counts,
2671 exec->comp->nbCounters * sizeof(int));
2672 }
2673 exec->nbRollbacks++;
2674}
2675
2676static void
2677xmlFARegExecRollBack(xmlRegExecCtxtPtr exec) {
2678 if (exec->nbRollbacks <= 0) {
2679 exec->status = -1;
2680#ifdef DEBUG_REGEXP_EXEC
2681 printf("rollback failed on empty stack\n");
2682#endif
2683 return;
2684 }
2685 exec->nbRollbacks--;
2686 exec->state = exec->rollbacks[exec->nbRollbacks].state;
2687 exec->index = exec->rollbacks[exec->nbRollbacks].index;
2688 exec->transno = exec->rollbacks[exec->nbRollbacks].nextbranch;
2689 if (exec->comp->nbCounters > 0) {
2690 if (exec->rollbacks[exec->nbRollbacks].counts == NULL) {
2691 fprintf(stderr, "exec save: allocation failed");
2692 exec->status = -6;
2693 return;
2694 }
2695 memcpy(exec->counts, exec->rollbacks[exec->nbRollbacks].counts,
2696 exec->comp->nbCounters * sizeof(int));
2697 }
2698
2699#ifdef DEBUG_REGEXP_EXEC
2700 printf("restored ");
2701 xmlFARegDebugExec(exec);
2702#endif
2703}
2704
2705/************************************************************************
2706 * *
William M. Brackddf71d62004-05-06 04:17:26 +00002707 * Verifier, running an input against a compiled regexp *
Daniel Veillard4255d502002-04-16 15:50:10 +00002708 * *
2709 ************************************************************************/
2710
2711static int
2712xmlFARegExec(xmlRegexpPtr comp, const xmlChar *content) {
2713 xmlRegExecCtxt execval;
2714 xmlRegExecCtxtPtr exec = &execval;
Daniel Veillard567a45b2005-10-18 19:11:55 +00002715 int ret, codepoint = 0, len, deter;
Daniel Veillard4255d502002-04-16 15:50:10 +00002716
2717 exec->inputString = content;
2718 exec->index = 0;
Daniel Veillard94cc1032005-09-15 13:09:00 +00002719 exec->nbPush = 0;
Daniel Veillard4255d502002-04-16 15:50:10 +00002720 exec->determinist = 1;
2721 exec->maxRollbacks = 0;
2722 exec->nbRollbacks = 0;
2723 exec->rollbacks = NULL;
2724 exec->status = 0;
2725 exec->comp = comp;
2726 exec->state = comp->states[0];
2727 exec->transno = 0;
2728 exec->transcount = 0;
Daniel Veillardf2a12832003-11-24 13:04:35 +00002729 exec->inputStack = NULL;
2730 exec->inputStackMax = 0;
Daniel Veillard4255d502002-04-16 15:50:10 +00002731 if (comp->nbCounters > 0) {
2732 exec->counts = (int *) xmlMalloc(comp->nbCounters * sizeof(int));
Daniel Veillardff46a042003-10-08 08:53:17 +00002733 if (exec->counts == NULL) {
2734 xmlRegexpErrMemory(NULL, "running regexp");
Daniel Veillard4255d502002-04-16 15:50:10 +00002735 return(-1);
Daniel Veillardff46a042003-10-08 08:53:17 +00002736 }
Daniel Veillard4255d502002-04-16 15:50:10 +00002737 memset(exec->counts, 0, comp->nbCounters * sizeof(int));
2738 } else
2739 exec->counts = NULL;
2740 while ((exec->status == 0) &&
2741 ((exec->inputString[exec->index] != 0) ||
2742 (exec->state->type != XML_REGEXP_FINAL_STATE))) {
2743 xmlRegTransPtr trans;
2744 xmlRegAtomPtr atom;
2745
2746 /*
William M. Brack0e00b282004-04-26 15:40:47 +00002747 * If end of input on non-terminal state, rollback, however we may
Daniel Veillard4255d502002-04-16 15:50:10 +00002748 * still have epsilon like transition for counted transitions
William M. Brack0e00b282004-04-26 15:40:47 +00002749 * on counters, in that case don't break too early. Additionally,
2750 * if we are working on a range like "AB{0,2}", where B is not present,
2751 * we don't want to break.
Daniel Veillard4255d502002-04-16 15:50:10 +00002752 */
William M. Brack0e00b282004-04-26 15:40:47 +00002753 if ((exec->inputString[exec->index] == 0) && (exec->counts == NULL)) {
William M. Brackddf71d62004-05-06 04:17:26 +00002754 /*
2755 * if there is a transition, we must check if
2756 * atom allows minOccurs of 0
2757 */
2758 if (exec->transno < exec->state->nbTrans) {
William M. Brack0e00b282004-04-26 15:40:47 +00002759 trans = &exec->state->trans[exec->transno];
2760 if (trans->to >=0) {
2761 atom = trans->atom;
2762 if (!((atom->min == 0) && (atom->max > 0)))
2763 goto rollback;
2764 }
2765 } else
2766 goto rollback;
2767 }
Daniel Veillard4255d502002-04-16 15:50:10 +00002768
2769 exec->transcount = 0;
2770 for (;exec->transno < exec->state->nbTrans;exec->transno++) {
2771 trans = &exec->state->trans[exec->transno];
2772 if (trans->to < 0)
2773 continue;
2774 atom = trans->atom;
2775 ret = 0;
Daniel Veillard567a45b2005-10-18 19:11:55 +00002776 deter = 1;
Daniel Veillard4255d502002-04-16 15:50:10 +00002777 if (trans->count >= 0) {
2778 int count;
2779 xmlRegCounterPtr counter;
2780
2781 /*
2782 * A counted transition.
2783 */
2784
2785 count = exec->counts[trans->count];
2786 counter = &exec->comp->counters[trans->count];
2787#ifdef DEBUG_REGEXP_EXEC
2788 printf("testing count %d: val %d, min %d, max %d\n",
2789 trans->count, count, counter->min, counter->max);
2790#endif
2791 ret = ((count >= counter->min) && (count <= counter->max));
Daniel Veillard567a45b2005-10-18 19:11:55 +00002792 if ((ret) && (counter->min != counter->max))
2793 deter = 0;
Daniel Veillard4255d502002-04-16 15:50:10 +00002794 } else if (atom == NULL) {
2795 fprintf(stderr, "epsilon transition left at runtime\n");
2796 exec->status = -2;
2797 break;
2798 } else if (exec->inputString[exec->index] != 0) {
2799 codepoint = CUR_SCHAR(&(exec->inputString[exec->index]), len);
2800 ret = xmlRegCheckCharacter(atom, codepoint);
William M. Brack0e00b282004-04-26 15:40:47 +00002801 if ((ret == 1) && (atom->min >= 0) && (atom->max > 0)) {
Daniel Veillard4255d502002-04-16 15:50:10 +00002802 xmlRegStatePtr to = comp->states[trans->to];
2803
2804 /*
2805 * this is a multiple input sequence
2806 */
2807 if (exec->state->nbTrans > exec->transno + 1) {
2808 xmlFARegExecSave(exec);
2809 }
2810 exec->transcount = 1;
2811 do {
2812 /*
2813 * Try to progress as much as possible on the input
2814 */
2815 if (exec->transcount == atom->max) {
2816 break;
2817 }
2818 exec->index += len;
2819 /*
2820 * End of input: stop here
2821 */
2822 if (exec->inputString[exec->index] == 0) {
2823 exec->index -= len;
2824 break;
2825 }
2826 if (exec->transcount >= atom->min) {
2827 int transno = exec->transno;
2828 xmlRegStatePtr state = exec->state;
2829
2830 /*
2831 * The transition is acceptable save it
2832 */
2833 exec->transno = -1; /* trick */
2834 exec->state = to;
2835 xmlFARegExecSave(exec);
2836 exec->transno = transno;
2837 exec->state = state;
2838 }
2839 codepoint = CUR_SCHAR(&(exec->inputString[exec->index]),
2840 len);
2841 ret = xmlRegCheckCharacter(atom, codepoint);
2842 exec->transcount++;
2843 } while (ret == 1);
2844 if (exec->transcount < atom->min)
2845 ret = 0;
2846
2847 /*
2848 * If the last check failed but one transition was found
2849 * possible, rollback
2850 */
2851 if (ret < 0)
2852 ret = 0;
2853 if (ret == 0) {
2854 goto rollback;
2855 }
William M. Brack0e00b282004-04-26 15:40:47 +00002856 } else if ((ret == 0) && (atom->min == 0) && (atom->max > 0)) {
2857 /*
2858 * we don't match on the codepoint, but minOccurs of 0
2859 * says that's ok. Setting len to 0 inhibits stepping
2860 * over the codepoint.
2861 */
2862 exec->transcount = 1;
2863 len = 0;
2864 ret = 1;
Daniel Veillard4255d502002-04-16 15:50:10 +00002865 }
William M. Brack0e00b282004-04-26 15:40:47 +00002866 } else if ((atom->min == 0) && (atom->max > 0)) {
2867 /* another spot to match when minOccurs is 0 */
2868 exec->transcount = 1;
2869 len = 0;
2870 ret = 1;
Daniel Veillard4255d502002-04-16 15:50:10 +00002871 }
2872 if (ret == 1) {
Daniel Veillard567a45b2005-10-18 19:11:55 +00002873 if ((trans->nd == 1) ||
2874 ((trans->count >= 0) && (deter == 0) &&
2875 (exec->state->nbTrans > exec->transno + 1))) {
Daniel Veillard4255d502002-04-16 15:50:10 +00002876 xmlFARegExecSave(exec);
2877 }
2878 if (trans->counter >= 0) {
2879#ifdef DEBUG_REGEXP_EXEC
2880 printf("Increasing count %d\n", trans->counter);
2881#endif
2882 exec->counts[trans->counter]++;
2883 }
Daniel Veillard10752282005-08-08 13:05:13 +00002884 if ((trans->count >= 0) &&
2885 (trans->count < REGEXP_ALL_COUNTER)) {
2886#ifdef DEBUG_REGEXP_EXEC
2887 printf("resetting count %d on transition\n",
2888 trans->count);
2889#endif
2890 exec->counts[trans->count] = 0;
2891 }
Daniel Veillard4255d502002-04-16 15:50:10 +00002892#ifdef DEBUG_REGEXP_EXEC
2893 printf("entering state %d\n", trans->to);
2894#endif
2895 exec->state = comp->states[trans->to];
2896 exec->transno = 0;
2897 if (trans->atom != NULL) {
2898 exec->index += len;
2899 }
2900 goto progress;
2901 } else if (ret < 0) {
2902 exec->status = -4;
2903 break;
2904 }
2905 }
2906 if ((exec->transno != 0) || (exec->state->nbTrans == 0)) {
2907rollback:
2908 /*
2909 * Failed to find a way out
2910 */
2911 exec->determinist = 0;
2912 xmlFARegExecRollBack(exec);
2913 }
2914progress:
2915 continue;
2916 }
2917 if (exec->rollbacks != NULL) {
2918 if (exec->counts != NULL) {
2919 int i;
2920
2921 for (i = 0;i < exec->maxRollbacks;i++)
2922 if (exec->rollbacks[i].counts != NULL)
2923 xmlFree(exec->rollbacks[i].counts);
2924 }
2925 xmlFree(exec->rollbacks);
2926 }
2927 if (exec->counts != NULL)
2928 xmlFree(exec->counts);
2929 if (exec->status == 0)
2930 return(1);
Daniel Veillard94cc1032005-09-15 13:09:00 +00002931 if (exec->status == -1) {
2932 if (exec->nbPush > MAX_PUSH)
2933 return(-1);
Daniel Veillard4255d502002-04-16 15:50:10 +00002934 return(0);
Daniel Veillard94cc1032005-09-15 13:09:00 +00002935 }
Daniel Veillard4255d502002-04-16 15:50:10 +00002936 return(exec->status);
2937}
2938
2939/************************************************************************
2940 * *
William M. Brackddf71d62004-05-06 04:17:26 +00002941 * Progressive interface to the verifier one atom at a time *
Daniel Veillard4255d502002-04-16 15:50:10 +00002942 * *
2943 ************************************************************************/
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00002944#ifdef DEBUG_ERR
2945static void testerr(xmlRegExecCtxtPtr exec);
2946#endif
Daniel Veillard4255d502002-04-16 15:50:10 +00002947
2948/**
Daniel Veillard01c13b52002-12-10 15:19:08 +00002949 * xmlRegNewExecCtxt:
Daniel Veillard4255d502002-04-16 15:50:10 +00002950 * @comp: a precompiled regular expression
2951 * @callback: a callback function used for handling progresses in the
2952 * automata matching phase
2953 * @data: the context data associated to the callback in this context
2954 *
2955 * Build a context used for progressive evaluation of a regexp.
Daniel Veillard01c13b52002-12-10 15:19:08 +00002956 *
2957 * Returns the new context
Daniel Veillard4255d502002-04-16 15:50:10 +00002958 */
2959xmlRegExecCtxtPtr
2960xmlRegNewExecCtxt(xmlRegexpPtr comp, xmlRegExecCallbacks callback, void *data) {
2961 xmlRegExecCtxtPtr exec;
2962
2963 if (comp == NULL)
2964 return(NULL);
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00002965 if ((comp->compact == NULL) && (comp->states == NULL))
2966 return(NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +00002967 exec = (xmlRegExecCtxtPtr) xmlMalloc(sizeof(xmlRegExecCtxt));
2968 if (exec == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00002969 xmlRegexpErrMemory(NULL, "creating execution context");
Daniel Veillard4255d502002-04-16 15:50:10 +00002970 return(NULL);
2971 }
2972 memset(exec, 0, sizeof(xmlRegExecCtxt));
2973 exec->inputString = NULL;
2974 exec->index = 0;
2975 exec->determinist = 1;
2976 exec->maxRollbacks = 0;
2977 exec->nbRollbacks = 0;
2978 exec->rollbacks = NULL;
2979 exec->status = 0;
2980 exec->comp = comp;
Daniel Veillard23e73572002-09-19 19:56:43 +00002981 if (comp->compact == NULL)
2982 exec->state = comp->states[0];
Daniel Veillard4255d502002-04-16 15:50:10 +00002983 exec->transno = 0;
2984 exec->transcount = 0;
2985 exec->callback = callback;
2986 exec->data = data;
2987 if (comp->nbCounters > 0) {
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00002988 /*
2989 * For error handling, exec->counts is allocated twice the size
2990 * the second half is used to store the data in case of rollback
2991 */
2992 exec->counts = (int *) xmlMalloc(comp->nbCounters * sizeof(int)
2993 * 2);
Daniel Veillard4255d502002-04-16 15:50:10 +00002994 if (exec->counts == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00002995 xmlRegexpErrMemory(NULL, "creating execution context");
Daniel Veillard4255d502002-04-16 15:50:10 +00002996 xmlFree(exec);
2997 return(NULL);
2998 }
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00002999 memset(exec->counts, 0, comp->nbCounters * sizeof(int) * 2);
3000 exec->errCounts = &exec->counts[comp->nbCounters];
3001 } else {
Daniel Veillard4255d502002-04-16 15:50:10 +00003002 exec->counts = NULL;
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003003 exec->errCounts = NULL;
3004 }
Daniel Veillard4255d502002-04-16 15:50:10 +00003005 exec->inputStackMax = 0;
3006 exec->inputStackNr = 0;
3007 exec->inputStack = NULL;
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003008 exec->errStateNo = -1;
3009 exec->errString = NULL;
Daniel Veillard94cc1032005-09-15 13:09:00 +00003010 exec->nbPush = 0;
Daniel Veillard4255d502002-04-16 15:50:10 +00003011 return(exec);
3012}
3013
3014/**
3015 * xmlRegFreeExecCtxt:
3016 * @exec: a regular expression evaulation context
3017 *
3018 * Free the structures associated to a regular expression evaulation context.
3019 */
3020void
3021xmlRegFreeExecCtxt(xmlRegExecCtxtPtr exec) {
3022 if (exec == NULL)
3023 return;
3024
3025 if (exec->rollbacks != NULL) {
3026 if (exec->counts != NULL) {
3027 int i;
3028
3029 for (i = 0;i < exec->maxRollbacks;i++)
3030 if (exec->rollbacks[i].counts != NULL)
3031 xmlFree(exec->rollbacks[i].counts);
3032 }
3033 xmlFree(exec->rollbacks);
3034 }
3035 if (exec->counts != NULL)
3036 xmlFree(exec->counts);
3037 if (exec->inputStack != NULL) {
3038 int i;
3039
Daniel Veillard32370232002-10-16 14:08:14 +00003040 for (i = 0;i < exec->inputStackNr;i++) {
3041 if (exec->inputStack[i].value != NULL)
3042 xmlFree(exec->inputStack[i].value);
3043 }
Daniel Veillard4255d502002-04-16 15:50:10 +00003044 xmlFree(exec->inputStack);
3045 }
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003046 if (exec->errString != NULL)
3047 xmlFree(exec->errString);
Daniel Veillard4255d502002-04-16 15:50:10 +00003048 xmlFree(exec);
3049}
3050
3051static void
3052xmlFARegExecSaveInputString(xmlRegExecCtxtPtr exec, const xmlChar *value,
3053 void *data) {
3054#ifdef DEBUG_PUSH
3055 printf("saving value: %d:%s\n", exec->inputStackNr, value);
3056#endif
3057 if (exec->inputStackMax == 0) {
3058 exec->inputStackMax = 4;
3059 exec->inputStack = (xmlRegInputTokenPtr)
3060 xmlMalloc(exec->inputStackMax * sizeof(xmlRegInputToken));
3061 if (exec->inputStack == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00003062 xmlRegexpErrMemory(NULL, "pushing input string");
Daniel Veillard4255d502002-04-16 15:50:10 +00003063 exec->inputStackMax = 0;
3064 return;
3065 }
3066 } else if (exec->inputStackNr + 1 >= exec->inputStackMax) {
3067 xmlRegInputTokenPtr tmp;
3068
3069 exec->inputStackMax *= 2;
3070 tmp = (xmlRegInputTokenPtr) xmlRealloc(exec->inputStack,
3071 exec->inputStackMax * sizeof(xmlRegInputToken));
3072 if (tmp == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00003073 xmlRegexpErrMemory(NULL, "pushing input string");
Daniel Veillard4255d502002-04-16 15:50:10 +00003074 exec->inputStackMax /= 2;
3075 return;
3076 }
3077 exec->inputStack = tmp;
3078 }
3079 exec->inputStack[exec->inputStackNr].value = xmlStrdup(value);
3080 exec->inputStack[exec->inputStackNr].data = data;
3081 exec->inputStackNr++;
3082 exec->inputStack[exec->inputStackNr].value = NULL;
3083 exec->inputStack[exec->inputStackNr].data = NULL;
3084}
3085
Daniel Veillardc0826a72004-08-10 14:17:33 +00003086/**
3087 * xmlRegStrEqualWildcard:
3088 * @expStr: the string to be evaluated
3089 * @valStr: the validation string
3090 *
3091 * Checks if both strings are equal or have the same content. "*"
3092 * can be used as a wildcard in @valStr; "|" is used as a seperator of
3093 * substrings in both @expStr and @valStr.
3094 *
3095 * Returns 1 if the comparison is satisfied and the number of substrings
3096 * is equal, 0 otherwise.
3097 */
3098
3099static int
3100xmlRegStrEqualWildcard(const xmlChar *expStr, const xmlChar *valStr) {
3101 if (expStr == valStr) return(1);
3102 if (expStr == NULL) return(0);
3103 if (valStr == NULL) return(0);
3104 do {
3105 /*
3106 * Eval if we have a wildcard for the current item.
3107 */
3108 if (*expStr != *valStr) {
Daniel Veillard4f82c8a2005-08-09 21:40:08 +00003109 /* if one of them starts with a wildcard make valStr be it */
3110 if (*valStr == '*') {
3111 const xmlChar *tmp;
3112
3113 tmp = valStr;
3114 valStr = expStr;
3115 expStr = tmp;
3116 }
Daniel Veillardc0826a72004-08-10 14:17:33 +00003117 if ((*valStr != 0) && (*expStr != 0) && (*expStr++ == '*')) {
3118 do {
3119 if (*valStr == XML_REG_STRING_SEPARATOR)
3120 break;
Kasimier T. Buchcikc0e833f2005-04-19 15:02:20 +00003121 valStr++;
Daniel Veillardc0826a72004-08-10 14:17:33 +00003122 } while (*valStr != 0);
3123 continue;
3124 } else
3125 return(0);
3126 }
Kasimier T. Buchcikc0e833f2005-04-19 15:02:20 +00003127 expStr++;
3128 valStr++;
Daniel Veillardc0826a72004-08-10 14:17:33 +00003129 } while (*valStr != 0);
3130 if (*expStr != 0)
3131 return (0);
3132 else
3133 return (1);
3134}
Daniel Veillard4255d502002-04-16 15:50:10 +00003135
3136/**
Daniel Veillard23e73572002-09-19 19:56:43 +00003137 * xmlRegCompactPushString:
3138 * @exec: a regexp execution context
3139 * @comp: the precompiled exec with a compact table
3140 * @value: a string token input
3141 * @data: data associated to the token to reuse in callbacks
3142 *
3143 * Push one input token in the execution context
3144 *
3145 * Returns: 1 if the regexp reached a final state, 0 if non-final, and
3146 * a negative value in case of error.
3147 */
3148static int
3149xmlRegCompactPushString(xmlRegExecCtxtPtr exec,
3150 xmlRegexpPtr comp,
3151 const xmlChar *value,
3152 void *data) {
3153 int state = exec->index;
3154 int i, target;
3155
3156 if ((comp == NULL) || (comp->compact == NULL) || (comp->stringMap == NULL))
3157 return(-1);
3158
3159 if (value == NULL) {
3160 /*
3161 * are we at a final state ?
3162 */
3163 if (comp->compact[state * (comp->nbstrings + 1)] ==
3164 XML_REGEXP_FINAL_STATE)
3165 return(1);
3166 return(0);
3167 }
3168
3169#ifdef DEBUG_PUSH
3170 printf("value pushed: %s\n", value);
3171#endif
3172
3173 /*
William M. Brackddf71d62004-05-06 04:17:26 +00003174 * Examine all outside transitions from current state
Daniel Veillard23e73572002-09-19 19:56:43 +00003175 */
3176 for (i = 0;i < comp->nbstrings;i++) {
3177 target = comp->compact[state * (comp->nbstrings + 1) + i + 1];
3178 if ((target > 0) && (target <= comp->nbstates)) {
Daniel Veillardc0826a72004-08-10 14:17:33 +00003179 target--; /* to avoid 0 */
3180 if (xmlRegStrEqualWildcard(comp->stringMap[i], value)) {
3181 exec->index = target;
Daniel Veillard118aed72002-09-24 14:13:13 +00003182 if ((exec->callback != NULL) && (comp->transdata != NULL)) {
3183 exec->callback(exec->data, value,
3184 comp->transdata[state * comp->nbstrings + i], data);
3185 }
Daniel Veillard23e73572002-09-19 19:56:43 +00003186#ifdef DEBUG_PUSH
3187 printf("entering state %d\n", target);
3188#endif
3189 if (comp->compact[target * (comp->nbstrings + 1)] ==
Daniel Veillardcc026dc2005-01-12 13:21:17 +00003190 XML_REGEXP_SINK_STATE)
3191 goto error;
3192
3193 if (comp->compact[target * (comp->nbstrings + 1)] ==
Daniel Veillard23e73572002-09-19 19:56:43 +00003194 XML_REGEXP_FINAL_STATE)
3195 return(1);
3196 return(0);
3197 }
3198 }
3199 }
3200 /*
3201 * Failed to find an exit transition out from current state for the
3202 * current token
3203 */
3204#ifdef DEBUG_PUSH
3205 printf("failed to find a transition for %s on state %d\n", value, state);
3206#endif
Daniel Veillardcc026dc2005-01-12 13:21:17 +00003207error:
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003208 if (exec->errString != NULL)
3209 xmlFree(exec->errString);
3210 exec->errString = xmlStrdup(value);
3211 exec->errStateNo = state;
Daniel Veillard23e73572002-09-19 19:56:43 +00003212 exec->status = -1;
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003213#ifdef DEBUG_ERR
3214 testerr(exec);
3215#endif
Daniel Veillard23e73572002-09-19 19:56:43 +00003216 return(-1);
3217}
3218
3219/**
Daniel Veillard6e65e152005-08-09 11:09:52 +00003220 * xmlRegExecPushStringInternal:
Daniel Veillardea7751d2002-12-20 00:16:24 +00003221 * @exec: a regexp execution context or NULL to indicate the end
Daniel Veillard4255d502002-04-16 15:50:10 +00003222 * @value: a string token input
3223 * @data: data associated to the token to reuse in callbacks
Daniel Veillard6e65e152005-08-09 11:09:52 +00003224 * @compound: value was assembled from 2 strings
Daniel Veillard4255d502002-04-16 15:50:10 +00003225 *
3226 * Push one input token in the execution context
3227 *
3228 * Returns: 1 if the regexp reached a final state, 0 if non-final, and
3229 * a negative value in case of error.
3230 */
Daniel Veillard6e65e152005-08-09 11:09:52 +00003231static int
3232xmlRegExecPushStringInternal(xmlRegExecCtxtPtr exec, const xmlChar *value,
3233 void *data, int compound) {
Daniel Veillard4255d502002-04-16 15:50:10 +00003234 xmlRegTransPtr trans;
3235 xmlRegAtomPtr atom;
3236 int ret;
3237 int final = 0;
Daniel Veillard90700152005-01-08 22:05:09 +00003238 int progress = 1;
Daniel Veillard4255d502002-04-16 15:50:10 +00003239
3240 if (exec == NULL)
3241 return(-1);
Daniel Veillard23e73572002-09-19 19:56:43 +00003242 if (exec->comp == NULL)
3243 return(-1);
Daniel Veillard4255d502002-04-16 15:50:10 +00003244 if (exec->status != 0)
3245 return(exec->status);
3246
Daniel Veillard23e73572002-09-19 19:56:43 +00003247 if (exec->comp->compact != NULL)
3248 return(xmlRegCompactPushString(exec, exec->comp, value, data));
3249
Daniel Veillard4255d502002-04-16 15:50:10 +00003250 if (value == NULL) {
3251 if (exec->state->type == XML_REGEXP_FINAL_STATE)
3252 return(1);
3253 final = 1;
3254 }
3255
3256#ifdef DEBUG_PUSH
3257 printf("value pushed: %s\n", value);
3258#endif
3259 /*
3260 * If we have an active rollback stack push the new value there
3261 * and get back to where we were left
3262 */
3263 if ((value != NULL) && (exec->inputStackNr > 0)) {
3264 xmlFARegExecSaveInputString(exec, value, data);
3265 value = exec->inputStack[exec->index].value;
3266 data = exec->inputStack[exec->index].data;
3267#ifdef DEBUG_PUSH
3268 printf("value loaded: %s\n", value);
3269#endif
3270 }
3271
3272 while ((exec->status == 0) &&
3273 ((value != NULL) ||
3274 ((final == 1) &&
3275 (exec->state->type != XML_REGEXP_FINAL_STATE)))) {
3276
3277 /*
3278 * End of input on non-terminal state, rollback, however we may
3279 * still have epsilon like transition for counted transitions
3280 * on counters, in that case don't break too early.
3281 */
Daniel Veillardb509f152002-04-17 16:28:10 +00003282 if ((value == NULL) && (exec->counts == NULL))
Daniel Veillard4255d502002-04-16 15:50:10 +00003283 goto rollback;
3284
3285 exec->transcount = 0;
3286 for (;exec->transno < exec->state->nbTrans;exec->transno++) {
3287 trans = &exec->state->trans[exec->transno];
3288 if (trans->to < 0)
3289 continue;
3290 atom = trans->atom;
3291 ret = 0;
Daniel Veillard441bc322002-04-20 17:38:48 +00003292 if (trans->count == REGEXP_ALL_LAX_COUNTER) {
3293 int i;
3294 int count;
3295 xmlRegTransPtr t;
3296 xmlRegCounterPtr counter;
3297
3298 ret = 0;
3299
3300#ifdef DEBUG_PUSH
3301 printf("testing all lax %d\n", trans->count);
3302#endif
3303 /*
3304 * Check all counted transitions from the current state
3305 */
3306 if ((value == NULL) && (final)) {
3307 ret = 1;
3308 } else if (value != NULL) {
3309 for (i = 0;i < exec->state->nbTrans;i++) {
3310 t = &exec->state->trans[i];
3311 if ((t->counter < 0) || (t == trans))
3312 continue;
3313 counter = &exec->comp->counters[t->counter];
3314 count = exec->counts[t->counter];
3315 if ((count < counter->max) &&
3316 (t->atom != NULL) &&
3317 (xmlStrEqual(value, t->atom->valuep))) {
3318 ret = 0;
3319 break;
3320 }
3321 if ((count >= counter->min) &&
3322 (count < counter->max) &&
3323 (xmlStrEqual(value, t->atom->valuep))) {
3324 ret = 1;
3325 break;
3326 }
3327 }
3328 }
3329 } else if (trans->count == REGEXP_ALL_COUNTER) {
Daniel Veillard8a001f62002-04-20 07:24:11 +00003330 int i;
3331 int count;
3332 xmlRegTransPtr t;
3333 xmlRegCounterPtr counter;
3334
3335 ret = 1;
3336
3337#ifdef DEBUG_PUSH
3338 printf("testing all %d\n", trans->count);
3339#endif
3340 /*
3341 * Check all counted transitions from the current state
3342 */
3343 for (i = 0;i < exec->state->nbTrans;i++) {
3344 t = &exec->state->trans[i];
3345 if ((t->counter < 0) || (t == trans))
3346 continue;
3347 counter = &exec->comp->counters[t->counter];
3348 count = exec->counts[t->counter];
3349 if ((count < counter->min) || (count > counter->max)) {
3350 ret = 0;
3351 break;
3352 }
3353 }
3354 } else if (trans->count >= 0) {
Daniel Veillard4255d502002-04-16 15:50:10 +00003355 int count;
3356 xmlRegCounterPtr counter;
3357
3358 /*
3359 * A counted transition.
3360 */
3361
3362 count = exec->counts[trans->count];
3363 counter = &exec->comp->counters[trans->count];
3364#ifdef DEBUG_PUSH
3365 printf("testing count %d: val %d, min %d, max %d\n",
3366 trans->count, count, counter->min, counter->max);
3367#endif
3368 ret = ((count >= counter->min) && (count <= counter->max));
3369 } else if (atom == NULL) {
3370 fprintf(stderr, "epsilon transition left at runtime\n");
3371 exec->status = -2;
3372 break;
3373 } else if (value != NULL) {
Daniel Veillardc0826a72004-08-10 14:17:33 +00003374 ret = xmlRegStrEqualWildcard(atom->valuep, value);
Daniel Veillard6e65e152005-08-09 11:09:52 +00003375 if (atom->neg) {
Daniel Veillard9efc4762005-07-19 14:33:55 +00003376 ret = !ret;
Daniel Veillard6e65e152005-08-09 11:09:52 +00003377 if (!compound)
3378 ret = 0;
3379 }
Daniel Veillard441bc322002-04-20 17:38:48 +00003380 if ((ret == 1) && (trans->counter >= 0)) {
3381 xmlRegCounterPtr counter;
3382 int count;
3383
3384 count = exec->counts[trans->counter];
3385 counter = &exec->comp->counters[trans->counter];
3386 if (count >= counter->max)
3387 ret = 0;
3388 }
3389
Daniel Veillard4255d502002-04-16 15:50:10 +00003390 if ((ret == 1) && (atom->min > 0) && (atom->max > 0)) {
3391 xmlRegStatePtr to = exec->comp->states[trans->to];
3392
3393 /*
3394 * this is a multiple input sequence
3395 */
3396 if (exec->state->nbTrans > exec->transno + 1) {
3397 if (exec->inputStackNr <= 0) {
3398 xmlFARegExecSaveInputString(exec, value, data);
3399 }
3400 xmlFARegExecSave(exec);
3401 }
3402 exec->transcount = 1;
3403 do {
3404 /*
3405 * Try to progress as much as possible on the input
3406 */
3407 if (exec->transcount == atom->max) {
3408 break;
3409 }
3410 exec->index++;
3411 value = exec->inputStack[exec->index].value;
3412 data = exec->inputStack[exec->index].data;
3413#ifdef DEBUG_PUSH
3414 printf("value loaded: %s\n", value);
3415#endif
3416
3417 /*
3418 * End of input: stop here
3419 */
3420 if (value == NULL) {
3421 exec->index --;
3422 break;
3423 }
3424 if (exec->transcount >= atom->min) {
3425 int transno = exec->transno;
3426 xmlRegStatePtr state = exec->state;
3427
3428 /*
3429 * The transition is acceptable save it
3430 */
3431 exec->transno = -1; /* trick */
3432 exec->state = to;
3433 if (exec->inputStackNr <= 0) {
3434 xmlFARegExecSaveInputString(exec, value, data);
3435 }
3436 xmlFARegExecSave(exec);
3437 exec->transno = transno;
3438 exec->state = state;
3439 }
3440 ret = xmlStrEqual(value, atom->valuep);
3441 exec->transcount++;
3442 } while (ret == 1);
3443 if (exec->transcount < atom->min)
3444 ret = 0;
3445
3446 /*
3447 * If the last check failed but one transition was found
3448 * possible, rollback
3449 */
3450 if (ret < 0)
3451 ret = 0;
3452 if (ret == 0) {
3453 goto rollback;
3454 }
3455 }
3456 }
3457 if (ret == 1) {
William M. Brack98873952003-12-26 06:03:14 +00003458 if ((exec->callback != NULL) && (atom != NULL) &&
3459 (data != NULL)) {
Daniel Veillard4255d502002-04-16 15:50:10 +00003460 exec->callback(exec->data, atom->valuep,
3461 atom->data, data);
3462 }
3463 if (exec->state->nbTrans > exec->transno + 1) {
3464 if (exec->inputStackNr <= 0) {
3465 xmlFARegExecSaveInputString(exec, value, data);
3466 }
3467 xmlFARegExecSave(exec);
3468 }
3469 if (trans->counter >= 0) {
3470#ifdef DEBUG_PUSH
3471 printf("Increasing count %d\n", trans->counter);
3472#endif
3473 exec->counts[trans->counter]++;
3474 }
Daniel Veillard10752282005-08-08 13:05:13 +00003475 if ((trans->count >= 0) &&
3476 (trans->count < REGEXP_ALL_COUNTER)) {
3477#ifdef DEBUG_REGEXP_EXEC
3478 printf("resetting count %d on transition\n",
3479 trans->count);
3480#endif
3481 exec->counts[trans->count] = 0;
3482 }
Daniel Veillard4255d502002-04-16 15:50:10 +00003483#ifdef DEBUG_PUSH
3484 printf("entering state %d\n", trans->to);
3485#endif
Daniel Veillardcc026dc2005-01-12 13:21:17 +00003486 if ((exec->comp->states[trans->to] != NULL) &&
3487 (exec->comp->states[trans->to]->type ==
3488 XML_REGEXP_SINK_STATE)) {
3489 /*
3490 * entering a sink state, save the current state as error
3491 * state.
3492 */
3493 if (exec->errString != NULL)
3494 xmlFree(exec->errString);
3495 exec->errString = xmlStrdup(value);
3496 exec->errState = exec->state;
3497 memcpy(exec->errCounts, exec->counts,
3498 exec->comp->nbCounters * sizeof(int));
3499 }
Daniel Veillard4255d502002-04-16 15:50:10 +00003500 exec->state = exec->comp->states[trans->to];
3501 exec->transno = 0;
3502 if (trans->atom != NULL) {
3503 if (exec->inputStack != NULL) {
3504 exec->index++;
3505 if (exec->index < exec->inputStackNr) {
3506 value = exec->inputStack[exec->index].value;
3507 data = exec->inputStack[exec->index].data;
3508#ifdef DEBUG_PUSH
3509 printf("value loaded: %s\n", value);
3510#endif
3511 } else {
3512 value = NULL;
3513 data = NULL;
3514#ifdef DEBUG_PUSH
3515 printf("end of input\n");
3516#endif
3517 }
3518 } else {
3519 value = NULL;
3520 data = NULL;
3521#ifdef DEBUG_PUSH
3522 printf("end of input\n");
3523#endif
3524 }
3525 }
3526 goto progress;
3527 } else if (ret < 0) {
3528 exec->status = -4;
3529 break;
3530 }
3531 }
3532 if ((exec->transno != 0) || (exec->state->nbTrans == 0)) {
3533rollback:
Daniel Veillard90700152005-01-08 22:05:09 +00003534 /*
Daniel Veillardcc026dc2005-01-12 13:21:17 +00003535 * if we didn't yet rollback on the current input
3536 * store the current state as the error state.
Daniel Veillard90700152005-01-08 22:05:09 +00003537 */
Daniel Veillardcc026dc2005-01-12 13:21:17 +00003538 if ((progress) && (exec->state != NULL) &&
3539 (exec->state->type != XML_REGEXP_SINK_STATE)) {
Daniel Veillard90700152005-01-08 22:05:09 +00003540 progress = 0;
3541 if (exec->errString != NULL)
3542 xmlFree(exec->errString);
3543 exec->errString = xmlStrdup(value);
3544 exec->errState = exec->state;
3545 memcpy(exec->errCounts, exec->counts,
3546 exec->comp->nbCounters * sizeof(int));
3547 }
3548
Daniel Veillard4255d502002-04-16 15:50:10 +00003549 /*
3550 * Failed to find a way out
3551 */
3552 exec->determinist = 0;
3553 xmlFARegExecRollBack(exec);
3554 if (exec->status == 0) {
3555 value = exec->inputStack[exec->index].value;
3556 data = exec->inputStack[exec->index].data;
3557#ifdef DEBUG_PUSH
3558 printf("value loaded: %s\n", value);
3559#endif
3560 }
3561 }
Daniel Veillard90700152005-01-08 22:05:09 +00003562 continue;
Daniel Veillard4255d502002-04-16 15:50:10 +00003563progress:
Daniel Veillard90700152005-01-08 22:05:09 +00003564 progress = 1;
Daniel Veillard4255d502002-04-16 15:50:10 +00003565 continue;
3566 }
3567 if (exec->status == 0) {
3568 return(exec->state->type == XML_REGEXP_FINAL_STATE);
3569 }
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003570#ifdef DEBUG_ERR
Daniel Veillard90700152005-01-08 22:05:09 +00003571 if (exec->status < 0) {
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003572 testerr(exec);
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003573 }
Daniel Veillard90700152005-01-08 22:05:09 +00003574#endif
Daniel Veillard4255d502002-04-16 15:50:10 +00003575 return(exec->status);
3576}
3577
Daniel Veillard52b48c72003-04-13 19:53:42 +00003578/**
Daniel Veillard6e65e152005-08-09 11:09:52 +00003579 * xmlRegExecPushString:
3580 * @exec: a regexp execution context or NULL to indicate the end
3581 * @value: a string token input
3582 * @data: data associated to the token to reuse in callbacks
3583 *
3584 * Push one input token in the execution context
3585 *
3586 * Returns: 1 if the regexp reached a final state, 0 if non-final, and
3587 * a negative value in case of error.
3588 */
3589int
3590xmlRegExecPushString(xmlRegExecCtxtPtr exec, const xmlChar *value,
3591 void *data) {
3592 return(xmlRegExecPushStringInternal(exec, value, data, 0));
3593}
3594
3595/**
Daniel Veillard52b48c72003-04-13 19:53:42 +00003596 * xmlRegExecPushString2:
3597 * @exec: a regexp execution context or NULL to indicate the end
3598 * @value: the first string token input
3599 * @value2: the second string token input
3600 * @data: data associated to the token to reuse in callbacks
3601 *
3602 * Push one input token in the execution context
3603 *
3604 * Returns: 1 if the regexp reached a final state, 0 if non-final, and
3605 * a negative value in case of error.
3606 */
3607int
3608xmlRegExecPushString2(xmlRegExecCtxtPtr exec, const xmlChar *value,
3609 const xmlChar *value2, void *data) {
3610 xmlChar buf[150];
3611 int lenn, lenp, ret;
3612 xmlChar *str;
3613
3614 if (exec == NULL)
3615 return(-1);
3616 if (exec->comp == NULL)
3617 return(-1);
3618 if (exec->status != 0)
3619 return(exec->status);
3620
3621 if (value2 == NULL)
3622 return(xmlRegExecPushString(exec, value, data));
3623
3624 lenn = strlen((char *) value2);
3625 lenp = strlen((char *) value);
3626
3627 if (150 < lenn + lenp + 2) {
Daniel Veillard3c908dc2003-04-19 00:07:51 +00003628 str = (xmlChar *) xmlMallocAtomic(lenn + lenp + 2);
Daniel Veillard52b48c72003-04-13 19:53:42 +00003629 if (str == NULL) {
3630 exec->status = -1;
3631 return(-1);
3632 }
3633 } else {
3634 str = buf;
3635 }
3636 memcpy(&str[0], value, lenp);
Daniel Veillardc0826a72004-08-10 14:17:33 +00003637 str[lenp] = XML_REG_STRING_SEPARATOR;
Daniel Veillard52b48c72003-04-13 19:53:42 +00003638 memcpy(&str[lenp + 1], value2, lenn);
3639 str[lenn + lenp + 1] = 0;
3640
3641 if (exec->comp->compact != NULL)
3642 ret = xmlRegCompactPushString(exec, exec->comp, str, data);
3643 else
Daniel Veillard6e65e152005-08-09 11:09:52 +00003644 ret = xmlRegExecPushStringInternal(exec, str, data, 1);
Daniel Veillard52b48c72003-04-13 19:53:42 +00003645
3646 if (str != buf)
3647 xmlFree(buf);
3648 return(ret);
3649}
3650
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003651/**
Daniel Veillard77005e62005-07-19 16:26:18 +00003652 * xmlRegExecGetValues:
Daniel Veillardfc0b6f62005-01-09 17:48:02 +00003653 * @exec: a regexp execution context
3654 * @err: error extraction or normal one
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003655 * @nbval: pointer to the number of accepted values IN/OUT
Daniel Veillardcc026dc2005-01-12 13:21:17 +00003656 * @nbneg: return number of negative transitions
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003657 * @values: pointer to the array of acceptable values
Daniel Veillardfc0b6f62005-01-09 17:48:02 +00003658 * @terminal: return value if this was a terminal state
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003659 *
Daniel Veillardfc0b6f62005-01-09 17:48:02 +00003660 * Extract informations from the regexp execution, internal routine to
3661 * implement xmlRegExecNextValues() and xmlRegExecErrInfo()
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003662 *
3663 * Returns: 0 in case of success or -1 in case of error.
3664 */
Daniel Veillardfc0b6f62005-01-09 17:48:02 +00003665static int
3666xmlRegExecGetValues(xmlRegExecCtxtPtr exec, int err,
Daniel Veillardcc026dc2005-01-12 13:21:17 +00003667 int *nbval, int *nbneg,
3668 xmlChar **values, int *terminal) {
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003669 int maxval;
Daniel Veillardcc026dc2005-01-12 13:21:17 +00003670 int nb = 0;
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003671
Daniel Veillardcc026dc2005-01-12 13:21:17 +00003672 if ((exec == NULL) || (nbval == NULL) || (nbneg == NULL) ||
3673 (values == NULL) || (*nbval <= 0))
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003674 return(-1);
Daniel Veillardfc0b6f62005-01-09 17:48:02 +00003675
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003676 maxval = *nbval;
3677 *nbval = 0;
Daniel Veillardcc026dc2005-01-12 13:21:17 +00003678 *nbneg = 0;
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003679 if ((exec->comp != NULL) && (exec->comp->compact != NULL)) {
3680 xmlRegexpPtr comp;
3681 int target, i, state;
3682
3683 comp = exec->comp;
Daniel Veillardfc0b6f62005-01-09 17:48:02 +00003684
3685 if (err) {
3686 if (exec->errStateNo == -1) return(-1);
3687 state = exec->errStateNo;
3688 } else {
3689 state = exec->index;
3690 }
3691 if (terminal != NULL) {
3692 if (comp->compact[state * (comp->nbstrings + 1)] ==
3693 XML_REGEXP_FINAL_STATE)
3694 *terminal = 1;
3695 else
3696 *terminal = 0;
3697 }
Daniel Veillardcc026dc2005-01-12 13:21:17 +00003698 for (i = 0;(i < comp->nbstrings) && (nb < maxval);i++) {
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003699 target = comp->compact[state * (comp->nbstrings + 1) + i + 1];
Daniel Veillardcc026dc2005-01-12 13:21:17 +00003700 if ((target > 0) && (target <= comp->nbstates) &&
3701 (comp->compact[(target - 1) * (comp->nbstrings + 1)] !=
3702 XML_REGEXP_SINK_STATE)) {
3703 values[nb++] = comp->stringMap[i];
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003704 (*nbval)++;
3705 }
3706 }
Daniel Veillardcc026dc2005-01-12 13:21:17 +00003707 for (i = 0;(i < comp->nbstrings) && (nb < maxval);i++) {
3708 target = comp->compact[state * (comp->nbstrings + 1) + i + 1];
3709 if ((target > 0) && (target <= comp->nbstates) &&
3710 (comp->compact[(target - 1) * (comp->nbstrings + 1)] ==
3711 XML_REGEXP_SINK_STATE)) {
3712 values[nb++] = comp->stringMap[i];
3713 (*nbneg)++;
3714 }
3715 }
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003716 } else {
3717 int transno;
3718 xmlRegTransPtr trans;
3719 xmlRegAtomPtr atom;
Daniel Veillardfc0b6f62005-01-09 17:48:02 +00003720 xmlRegStatePtr state;
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003721
Daniel Veillardfc0b6f62005-01-09 17:48:02 +00003722 if (terminal != NULL) {
3723 if (exec->state->type == XML_REGEXP_FINAL_STATE)
3724 *terminal = 1;
3725 else
3726 *terminal = 0;
3727 }
3728
3729 if (err) {
3730 if (exec->errState == NULL) return(-1);
3731 state = exec->errState;
3732 } else {
3733 if (exec->state == NULL) return(-1);
3734 state = exec->state;
3735 }
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003736 for (transno = 0;
Daniel Veillardcc026dc2005-01-12 13:21:17 +00003737 (transno < state->nbTrans) && (nb < maxval);
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003738 transno++) {
Daniel Veillardfc0b6f62005-01-09 17:48:02 +00003739 trans = &state->trans[transno];
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003740 if (trans->to < 0)
3741 continue;
3742 atom = trans->atom;
3743 if ((atom == NULL) || (atom->valuep == NULL))
3744 continue;
3745 if (trans->count == REGEXP_ALL_LAX_COUNTER) {
Daniel Veillardcc026dc2005-01-12 13:21:17 +00003746 /* this should not be reached but ... */
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003747 TODO;
3748 } else if (trans->count == REGEXP_ALL_COUNTER) {
Daniel Veillardcc026dc2005-01-12 13:21:17 +00003749 /* this should not be reached but ... */
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003750 TODO;
3751 } else if (trans->counter >= 0) {
3752 xmlRegCounterPtr counter;
3753 int count;
3754
Daniel Veillardfc0b6f62005-01-09 17:48:02 +00003755 if (err)
3756 count = exec->errCounts[trans->counter];
3757 else
3758 count = exec->counts[trans->counter];
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003759 counter = &exec->comp->counters[trans->counter];
3760 if (count < counter->max) {
Daniel Veillard77005e62005-07-19 16:26:18 +00003761 if (atom->neg)
3762 values[nb++] = (xmlChar *) atom->valuep2;
3763 else
3764 values[nb++] = (xmlChar *) atom->valuep;
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003765 (*nbval)++;
3766 }
3767 } else {
Daniel Veillardcc026dc2005-01-12 13:21:17 +00003768 if ((exec->comp->states[trans->to] != NULL) &&
3769 (exec->comp->states[trans->to]->type !=
3770 XML_REGEXP_SINK_STATE)) {
Daniel Veillard77005e62005-07-19 16:26:18 +00003771 if (atom->neg)
3772 values[nb++] = (xmlChar *) atom->valuep2;
3773 else
3774 values[nb++] = (xmlChar *) atom->valuep;
Daniel Veillardcc026dc2005-01-12 13:21:17 +00003775 (*nbval)++;
3776 }
3777 }
3778 }
3779 for (transno = 0;
3780 (transno < state->nbTrans) && (nb < maxval);
3781 transno++) {
3782 trans = &state->trans[transno];
3783 if (trans->to < 0)
3784 continue;
3785 atom = trans->atom;
3786 if ((atom == NULL) || (atom->valuep == NULL))
3787 continue;
3788 if (trans->count == REGEXP_ALL_LAX_COUNTER) {
3789 continue;
3790 } else if (trans->count == REGEXP_ALL_COUNTER) {
3791 continue;
3792 } else if (trans->counter >= 0) {
3793 continue;
3794 } else {
3795 if ((exec->comp->states[trans->to] != NULL) &&
3796 (exec->comp->states[trans->to]->type ==
3797 XML_REGEXP_SINK_STATE)) {
Daniel Veillard77005e62005-07-19 16:26:18 +00003798 if (atom->neg)
3799 values[nb++] = (xmlChar *) atom->valuep2;
3800 else
3801 values[nb++] = (xmlChar *) atom->valuep;
Daniel Veillardcc026dc2005-01-12 13:21:17 +00003802 (*nbneg)++;
3803 }
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003804 }
3805 }
3806 }
3807 return(0);
3808}
3809
Daniel Veillardfc0b6f62005-01-09 17:48:02 +00003810/**
3811 * xmlRegExecNextValues:
3812 * @exec: a regexp execution context
3813 * @nbval: pointer to the number of accepted values IN/OUT
Daniel Veillardcc026dc2005-01-12 13:21:17 +00003814 * @nbneg: return number of negative transitions
Daniel Veillardfc0b6f62005-01-09 17:48:02 +00003815 * @values: pointer to the array of acceptable values
3816 * @terminal: return value if this was a terminal state
3817 *
3818 * Extract informations from the regexp execution,
3819 * the parameter @values must point to an array of @nbval string pointers
3820 * on return nbval will contain the number of possible strings in that
3821 * state and the @values array will be updated with them. The string values
3822 * returned will be freed with the @exec context and don't need to be
3823 * deallocated.
3824 *
3825 * Returns: 0 in case of success or -1 in case of error.
3826 */
3827int
Daniel Veillardcc026dc2005-01-12 13:21:17 +00003828xmlRegExecNextValues(xmlRegExecCtxtPtr exec, int *nbval, int *nbneg,
3829 xmlChar **values, int *terminal) {
3830 return(xmlRegExecGetValues(exec, 0, nbval, nbneg, values, terminal));
Daniel Veillardfc0b6f62005-01-09 17:48:02 +00003831}
3832
3833/**
3834 * xmlRegExecErrInfo:
3835 * @exec: a regexp execution context generating an error
3836 * @string: return value for the error string
3837 * @nbval: pointer to the number of accepted values IN/OUT
Daniel Veillardcc026dc2005-01-12 13:21:17 +00003838 * @nbneg: return number of negative transitions
Daniel Veillardfc0b6f62005-01-09 17:48:02 +00003839 * @values: pointer to the array of acceptable values
3840 * @terminal: return value if this was a terminal state
3841 *
3842 * Extract error informations from the regexp execution, the parameter
3843 * @string will be updated with the value pushed and not accepted,
3844 * the parameter @values must point to an array of @nbval string pointers
3845 * on return nbval will contain the number of possible strings in that
3846 * state and the @values array will be updated with them. The string values
3847 * returned will be freed with the @exec context and don't need to be
3848 * deallocated.
3849 *
3850 * Returns: 0 in case of success or -1 in case of error.
3851 */
3852int
3853xmlRegExecErrInfo(xmlRegExecCtxtPtr exec, const xmlChar **string,
Daniel Veillardcc026dc2005-01-12 13:21:17 +00003854 int *nbval, int *nbneg, xmlChar **values, int *terminal) {
Daniel Veillardfc0b6f62005-01-09 17:48:02 +00003855 if (exec == NULL)
3856 return(-1);
3857 if (string != NULL) {
3858 if (exec->status != 0)
3859 *string = exec->errString;
3860 else
3861 *string = NULL;
3862 }
Daniel Veillardcc026dc2005-01-12 13:21:17 +00003863 return(xmlRegExecGetValues(exec, 1, nbval, nbneg, values, terminal));
Daniel Veillardfc0b6f62005-01-09 17:48:02 +00003864}
3865
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003866#ifdef DEBUG_ERR
3867static void testerr(xmlRegExecCtxtPtr exec) {
3868 const xmlChar *string;
Daniel Veillardcee2b3a2005-01-25 00:22:52 +00003869 xmlChar *values[5];
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003870 int nb = 5;
Daniel Veillardcc026dc2005-01-12 13:21:17 +00003871 int nbneg;
Daniel Veillardfc0b6f62005-01-09 17:48:02 +00003872 int terminal;
Daniel Veillardcc026dc2005-01-12 13:21:17 +00003873 xmlRegExecErrInfo(exec, &string, &nb, &nbneg, &values[0], &terminal);
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003874}
3875#endif
3876
Daniel Veillard4255d502002-04-16 15:50:10 +00003877#if 0
3878static int
3879xmlRegExecPushChar(xmlRegExecCtxtPtr exec, int UCS) {
3880 xmlRegTransPtr trans;
3881 xmlRegAtomPtr atom;
3882 int ret;
3883 int codepoint, len;
3884
3885 if (exec == NULL)
3886 return(-1);
3887 if (exec->status != 0)
3888 return(exec->status);
3889
3890 while ((exec->status == 0) &&
3891 ((exec->inputString[exec->index] != 0) ||
3892 (exec->state->type != XML_REGEXP_FINAL_STATE))) {
3893
3894 /*
3895 * End of input on non-terminal state, rollback, however we may
3896 * still have epsilon like transition for counted transitions
3897 * on counters, in that case don't break too early.
3898 */
3899 if ((exec->inputString[exec->index] == 0) && (exec->counts == NULL))
3900 goto rollback;
3901
3902 exec->transcount = 0;
3903 for (;exec->transno < exec->state->nbTrans;exec->transno++) {
3904 trans = &exec->state->trans[exec->transno];
3905 if (trans->to < 0)
3906 continue;
3907 atom = trans->atom;
3908 ret = 0;
3909 if (trans->count >= 0) {
3910 int count;
3911 xmlRegCounterPtr counter;
3912
3913 /*
3914 * A counted transition.
3915 */
3916
3917 count = exec->counts[trans->count];
3918 counter = &exec->comp->counters[trans->count];
3919#ifdef DEBUG_REGEXP_EXEC
3920 printf("testing count %d: val %d, min %d, max %d\n",
3921 trans->count, count, counter->min, counter->max);
3922#endif
3923 ret = ((count >= counter->min) && (count <= counter->max));
3924 } else if (atom == NULL) {
3925 fprintf(stderr, "epsilon transition left at runtime\n");
3926 exec->status = -2;
3927 break;
3928 } else if (exec->inputString[exec->index] != 0) {
3929 codepoint = CUR_SCHAR(&(exec->inputString[exec->index]), len);
3930 ret = xmlRegCheckCharacter(atom, codepoint);
3931 if ((ret == 1) && (atom->min > 0) && (atom->max > 0)) {
3932 xmlRegStatePtr to = exec->comp->states[trans->to];
3933
3934 /*
3935 * this is a multiple input sequence
3936 */
3937 if (exec->state->nbTrans > exec->transno + 1) {
3938 xmlFARegExecSave(exec);
3939 }
3940 exec->transcount = 1;
3941 do {
3942 /*
3943 * Try to progress as much as possible on the input
3944 */
3945 if (exec->transcount == atom->max) {
3946 break;
3947 }
3948 exec->index += len;
3949 /*
3950 * End of input: stop here
3951 */
3952 if (exec->inputString[exec->index] == 0) {
3953 exec->index -= len;
3954 break;
3955 }
3956 if (exec->transcount >= atom->min) {
3957 int transno = exec->transno;
3958 xmlRegStatePtr state = exec->state;
3959
3960 /*
3961 * The transition is acceptable save it
3962 */
3963 exec->transno = -1; /* trick */
3964 exec->state = to;
3965 xmlFARegExecSave(exec);
3966 exec->transno = transno;
3967 exec->state = state;
3968 }
3969 codepoint = CUR_SCHAR(&(exec->inputString[exec->index]),
3970 len);
3971 ret = xmlRegCheckCharacter(atom, codepoint);
3972 exec->transcount++;
3973 } while (ret == 1);
3974 if (exec->transcount < atom->min)
3975 ret = 0;
3976
3977 /*
3978 * If the last check failed but one transition was found
3979 * possible, rollback
3980 */
3981 if (ret < 0)
3982 ret = 0;
3983 if (ret == 0) {
3984 goto rollback;
3985 }
3986 }
3987 }
3988 if (ret == 1) {
3989 if (exec->state->nbTrans > exec->transno + 1) {
3990 xmlFARegExecSave(exec);
3991 }
3992 if (trans->counter >= 0) {
3993#ifdef DEBUG_REGEXP_EXEC
3994 printf("Increasing count %d\n", trans->counter);
3995#endif
3996 exec->counts[trans->counter]++;
3997 }
3998#ifdef DEBUG_REGEXP_EXEC
3999 printf("entering state %d\n", trans->to);
4000#endif
4001 exec->state = exec->comp->states[trans->to];
4002 exec->transno = 0;
4003 if (trans->atom != NULL) {
4004 exec->index += len;
4005 }
4006 goto progress;
4007 } else if (ret < 0) {
4008 exec->status = -4;
4009 break;
4010 }
4011 }
4012 if ((exec->transno != 0) || (exec->state->nbTrans == 0)) {
4013rollback:
4014 /*
4015 * Failed to find a way out
4016 */
4017 exec->determinist = 0;
4018 xmlFARegExecRollBack(exec);
4019 }
4020progress:
4021 continue;
4022 }
4023}
4024#endif
4025/************************************************************************
4026 * *
William M. Brackddf71d62004-05-06 04:17:26 +00004027 * Parser for the Schemas Datatype Regular Expressions *
Daniel Veillard4255d502002-04-16 15:50:10 +00004028 * http://www.w3.org/TR/2001/REC-xmlschema-2-20010502/#regexs *
4029 * *
4030 ************************************************************************/
4031
4032/**
4033 * xmlFAIsChar:
Daniel Veillard441bc322002-04-20 17:38:48 +00004034 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00004035 *
4036 * [10] Char ::= [^.\?*+()|#x5B#x5D]
4037 */
4038static int
4039xmlFAIsChar(xmlRegParserCtxtPtr ctxt) {
4040 int cur;
4041 int len;
4042
4043 cur = CUR_SCHAR(ctxt->cur, len);
4044 if ((cur == '.') || (cur == '\\') || (cur == '?') ||
4045 (cur == '*') || (cur == '+') || (cur == '(') ||
4046 (cur == ')') || (cur == '|') || (cur == 0x5B) ||
4047 (cur == 0x5D) || (cur == 0))
4048 return(-1);
4049 return(cur);
4050}
4051
4052/**
4053 * xmlFAParseCharProp:
Daniel Veillard441bc322002-04-20 17:38:48 +00004054 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00004055 *
4056 * [27] charProp ::= IsCategory | IsBlock
4057 * [28] IsCategory ::= Letters | Marks | Numbers | Punctuation |
4058 * Separators | Symbols | Others
4059 * [29] Letters ::= 'L' [ultmo]?
4060 * [30] Marks ::= 'M' [nce]?
4061 * [31] Numbers ::= 'N' [dlo]?
4062 * [32] Punctuation ::= 'P' [cdseifo]?
4063 * [33] Separators ::= 'Z' [slp]?
4064 * [34] Symbols ::= 'S' [mcko]?
4065 * [35] Others ::= 'C' [cfon]?
4066 * [36] IsBlock ::= 'Is' [a-zA-Z0-9#x2D]+
4067 */
4068static void
4069xmlFAParseCharProp(xmlRegParserCtxtPtr ctxt) {
4070 int cur;
William M. Brack779af002003-08-01 15:55:39 +00004071 xmlRegAtomType type = (xmlRegAtomType) 0;
Daniel Veillard4255d502002-04-16 15:50:10 +00004072 xmlChar *blockName = NULL;
4073
4074 cur = CUR;
4075 if (cur == 'L') {
4076 NEXT;
4077 cur = CUR;
4078 if (cur == 'u') {
4079 NEXT;
4080 type = XML_REGEXP_LETTER_UPPERCASE;
4081 } else if (cur == 'l') {
4082 NEXT;
4083 type = XML_REGEXP_LETTER_LOWERCASE;
4084 } else if (cur == 't') {
4085 NEXT;
4086 type = XML_REGEXP_LETTER_TITLECASE;
4087 } else if (cur == 'm') {
4088 NEXT;
4089 type = XML_REGEXP_LETTER_MODIFIER;
4090 } else if (cur == 'o') {
4091 NEXT;
4092 type = XML_REGEXP_LETTER_OTHERS;
4093 } else {
4094 type = XML_REGEXP_LETTER;
4095 }
4096 } else if (cur == 'M') {
4097 NEXT;
4098 cur = CUR;
4099 if (cur == 'n') {
4100 NEXT;
4101 /* nonspacing */
4102 type = XML_REGEXP_MARK_NONSPACING;
4103 } else if (cur == 'c') {
4104 NEXT;
4105 /* spacing combining */
4106 type = XML_REGEXP_MARK_SPACECOMBINING;
4107 } else if (cur == 'e') {
4108 NEXT;
4109 /* enclosing */
4110 type = XML_REGEXP_MARK_ENCLOSING;
4111 } else {
4112 /* all marks */
4113 type = XML_REGEXP_MARK;
4114 }
4115 } else if (cur == 'N') {
4116 NEXT;
4117 cur = CUR;
4118 if (cur == 'd') {
4119 NEXT;
4120 /* digital */
4121 type = XML_REGEXP_NUMBER_DECIMAL;
4122 } else if (cur == 'l') {
4123 NEXT;
4124 /* letter */
4125 type = XML_REGEXP_NUMBER_LETTER;
4126 } else if (cur == 'o') {
4127 NEXT;
4128 /* other */
4129 type = XML_REGEXP_NUMBER_OTHERS;
4130 } else {
4131 /* all numbers */
4132 type = XML_REGEXP_NUMBER;
4133 }
4134 } else if (cur == 'P') {
4135 NEXT;
4136 cur = CUR;
4137 if (cur == 'c') {
4138 NEXT;
4139 /* connector */
4140 type = XML_REGEXP_PUNCT_CONNECTOR;
4141 } else if (cur == 'd') {
4142 NEXT;
4143 /* dash */
4144 type = XML_REGEXP_PUNCT_DASH;
4145 } else if (cur == 's') {
4146 NEXT;
4147 /* open */
4148 type = XML_REGEXP_PUNCT_OPEN;
4149 } else if (cur == 'e') {
4150 NEXT;
4151 /* close */
4152 type = XML_REGEXP_PUNCT_CLOSE;
4153 } else if (cur == 'i') {
4154 NEXT;
4155 /* initial quote */
4156 type = XML_REGEXP_PUNCT_INITQUOTE;
4157 } else if (cur == 'f') {
4158 NEXT;
4159 /* final quote */
4160 type = XML_REGEXP_PUNCT_FINQUOTE;
4161 } else if (cur == 'o') {
4162 NEXT;
4163 /* other */
4164 type = XML_REGEXP_PUNCT_OTHERS;
4165 } else {
4166 /* all punctuation */
4167 type = XML_REGEXP_PUNCT;
4168 }
4169 } else if (cur == 'Z') {
4170 NEXT;
4171 cur = CUR;
4172 if (cur == 's') {
4173 NEXT;
4174 /* space */
4175 type = XML_REGEXP_SEPAR_SPACE;
4176 } else if (cur == 'l') {
4177 NEXT;
4178 /* line */
4179 type = XML_REGEXP_SEPAR_LINE;
4180 } else if (cur == 'p') {
4181 NEXT;
4182 /* paragraph */
4183 type = XML_REGEXP_SEPAR_PARA;
4184 } else {
4185 /* all separators */
4186 type = XML_REGEXP_SEPAR;
4187 }
4188 } else if (cur == 'S') {
4189 NEXT;
4190 cur = CUR;
4191 if (cur == 'm') {
4192 NEXT;
4193 type = XML_REGEXP_SYMBOL_MATH;
4194 /* math */
4195 } else if (cur == 'c') {
4196 NEXT;
4197 type = XML_REGEXP_SYMBOL_CURRENCY;
4198 /* currency */
4199 } else if (cur == 'k') {
4200 NEXT;
4201 type = XML_REGEXP_SYMBOL_MODIFIER;
4202 /* modifiers */
4203 } else if (cur == 'o') {
4204 NEXT;
4205 type = XML_REGEXP_SYMBOL_OTHERS;
4206 /* other */
4207 } else {
4208 /* all symbols */
4209 type = XML_REGEXP_SYMBOL;
4210 }
4211 } else if (cur == 'C') {
4212 NEXT;
4213 cur = CUR;
4214 if (cur == 'c') {
4215 NEXT;
4216 /* control */
4217 type = XML_REGEXP_OTHER_CONTROL;
4218 } else if (cur == 'f') {
4219 NEXT;
4220 /* format */
4221 type = XML_REGEXP_OTHER_FORMAT;
4222 } else if (cur == 'o') {
4223 NEXT;
4224 /* private use */
4225 type = XML_REGEXP_OTHER_PRIVATE;
4226 } else if (cur == 'n') {
4227 NEXT;
4228 /* not assigned */
4229 type = XML_REGEXP_OTHER_NA;
4230 } else {
4231 /* all others */
4232 type = XML_REGEXP_OTHER;
4233 }
4234 } else if (cur == 'I') {
4235 const xmlChar *start;
4236 NEXT;
4237 cur = CUR;
4238 if (cur != 's') {
4239 ERROR("IsXXXX expected");
4240 return;
4241 }
4242 NEXT;
4243 start = ctxt->cur;
4244 cur = CUR;
4245 if (((cur >= 'a') && (cur <= 'z')) ||
4246 ((cur >= 'A') && (cur <= 'Z')) ||
4247 ((cur >= '0') && (cur <= '9')) ||
4248 (cur == 0x2D)) {
4249 NEXT;
4250 cur = CUR;
4251 while (((cur >= 'a') && (cur <= 'z')) ||
4252 ((cur >= 'A') && (cur <= 'Z')) ||
4253 ((cur >= '0') && (cur <= '9')) ||
4254 (cur == 0x2D)) {
4255 NEXT;
4256 cur = CUR;
4257 }
4258 }
4259 type = XML_REGEXP_BLOCK_NAME;
4260 blockName = xmlStrndup(start, ctxt->cur - start);
4261 } else {
4262 ERROR("Unknown char property");
4263 return;
4264 }
4265 if (ctxt->atom == NULL) {
4266 ctxt->atom = xmlRegNewAtom(ctxt, type);
4267 if (ctxt->atom != NULL)
4268 ctxt->atom->valuep = blockName;
4269 } else if (ctxt->atom->type == XML_REGEXP_RANGES) {
4270 xmlRegAtomAddRange(ctxt, ctxt->atom, ctxt->neg,
4271 type, 0, 0, blockName);
4272 }
4273}
4274
4275/**
4276 * xmlFAParseCharClassEsc:
Daniel Veillard441bc322002-04-20 17:38:48 +00004277 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00004278 *
4279 * [23] charClassEsc ::= ( SingleCharEsc | MultiCharEsc | catEsc | complEsc )
4280 * [24] SingleCharEsc ::= '\' [nrt\|.?*+(){}#x2D#x5B#x5D#x5E]
4281 * [25] catEsc ::= '\p{' charProp '}'
4282 * [26] complEsc ::= '\P{' charProp '}'
4283 * [37] MultiCharEsc ::= '.' | ('\' [sSiIcCdDwW])
4284 */
4285static void
4286xmlFAParseCharClassEsc(xmlRegParserCtxtPtr ctxt) {
4287 int cur;
4288
4289 if (CUR == '.') {
4290 if (ctxt->atom == NULL) {
4291 ctxt->atom = xmlRegNewAtom(ctxt, XML_REGEXP_ANYCHAR);
4292 } else if (ctxt->atom->type == XML_REGEXP_RANGES) {
4293 xmlRegAtomAddRange(ctxt, ctxt->atom, ctxt->neg,
4294 XML_REGEXP_ANYCHAR, 0, 0, NULL);
4295 }
4296 NEXT;
4297 return;
4298 }
4299 if (CUR != '\\') {
4300 ERROR("Escaped sequence: expecting \\");
4301 return;
4302 }
4303 NEXT;
4304 cur = CUR;
4305 if (cur == 'p') {
4306 NEXT;
4307 if (CUR != '{') {
4308 ERROR("Expecting '{'");
4309 return;
4310 }
4311 NEXT;
4312 xmlFAParseCharProp(ctxt);
4313 if (CUR != '}') {
4314 ERROR("Expecting '}'");
4315 return;
4316 }
4317 NEXT;
4318 } else if (cur == 'P') {
4319 NEXT;
4320 if (CUR != '{') {
4321 ERROR("Expecting '{'");
4322 return;
4323 }
4324 NEXT;
4325 xmlFAParseCharProp(ctxt);
4326 ctxt->atom->neg = 1;
4327 if (CUR != '}') {
4328 ERROR("Expecting '}'");
4329 return;
4330 }
4331 NEXT;
4332 } else if ((cur == 'n') || (cur == 'r') || (cur == 't') || (cur == '\\') ||
4333 (cur == '|') || (cur == '.') || (cur == '?') || (cur == '*') ||
4334 (cur == '+') || (cur == '(') || (cur == ')') || (cur == '{') ||
4335 (cur == '}') || (cur == 0x2D) || (cur == 0x5B) || (cur == 0x5D) ||
4336 (cur == 0x5E)) {
4337 if (ctxt->atom == NULL) {
4338 ctxt->atom = xmlRegNewAtom(ctxt, XML_REGEXP_CHARVAL);
Daniel Veillard99c394d2005-07-14 12:58:49 +00004339 if (ctxt->atom != NULL) {
4340 switch (cur) {
4341 case 'n':
4342 ctxt->atom->codepoint = '\n';
4343 break;
4344 case 'r':
4345 ctxt->atom->codepoint = '\r';
4346 break;
4347 case 't':
4348 ctxt->atom->codepoint = '\t';
4349 break;
4350 default:
4351 ctxt->atom->codepoint = cur;
4352 }
4353 }
Daniel Veillard4255d502002-04-16 15:50:10 +00004354 } else if (ctxt->atom->type == XML_REGEXP_RANGES) {
4355 xmlRegAtomAddRange(ctxt, ctxt->atom, ctxt->neg,
4356 XML_REGEXP_CHARVAL, cur, cur, NULL);
4357 }
4358 NEXT;
4359 } else if ((cur == 's') || (cur == 'S') || (cur == 'i') || (cur == 'I') ||
4360 (cur == 'c') || (cur == 'C') || (cur == 'd') || (cur == 'D') ||
4361 (cur == 'w') || (cur == 'W')) {
Daniel Veillardb509f152002-04-17 16:28:10 +00004362 xmlRegAtomType type = XML_REGEXP_ANYSPACE;
Daniel Veillard4255d502002-04-16 15:50:10 +00004363
4364 switch (cur) {
4365 case 's':
4366 type = XML_REGEXP_ANYSPACE;
4367 break;
4368 case 'S':
4369 type = XML_REGEXP_NOTSPACE;
4370 break;
4371 case 'i':
4372 type = XML_REGEXP_INITNAME;
4373 break;
4374 case 'I':
4375 type = XML_REGEXP_NOTINITNAME;
4376 break;
4377 case 'c':
4378 type = XML_REGEXP_NAMECHAR;
4379 break;
4380 case 'C':
4381 type = XML_REGEXP_NOTNAMECHAR;
4382 break;
4383 case 'd':
4384 type = XML_REGEXP_DECIMAL;
4385 break;
4386 case 'D':
4387 type = XML_REGEXP_NOTDECIMAL;
4388 break;
4389 case 'w':
4390 type = XML_REGEXP_REALCHAR;
4391 break;
4392 case 'W':
4393 type = XML_REGEXP_NOTREALCHAR;
4394 break;
4395 }
4396 NEXT;
4397 if (ctxt->atom == NULL) {
4398 ctxt->atom = xmlRegNewAtom(ctxt, type);
4399 } else if (ctxt->atom->type == XML_REGEXP_RANGES) {
4400 xmlRegAtomAddRange(ctxt, ctxt->atom, ctxt->neg,
4401 type, 0, 0, NULL);
4402 }
4403 }
4404}
4405
4406/**
4407 * xmlFAParseCharRef:
Daniel Veillard441bc322002-04-20 17:38:48 +00004408 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00004409 *
4410 * [19] XmlCharRef ::= ( '&#' [0-9]+ ';' ) | (' &#x' [0-9a-fA-F]+ ';' )
4411 */
4412static int
4413xmlFAParseCharRef(xmlRegParserCtxtPtr ctxt) {
4414 int ret = 0, cur;
4415
4416 if ((CUR != '&') || (NXT(1) != '#'))
4417 return(-1);
4418 NEXT;
4419 NEXT;
4420 cur = CUR;
4421 if (cur == 'x') {
4422 NEXT;
4423 cur = CUR;
4424 if (((cur >= '0') && (cur <= '9')) ||
4425 ((cur >= 'a') && (cur <= 'f')) ||
4426 ((cur >= 'A') && (cur <= 'F'))) {
4427 while (((cur >= '0') && (cur <= '9')) ||
4428 ((cur >= 'A') && (cur <= 'F'))) {
4429 if ((cur >= '0') && (cur <= '9'))
4430 ret = ret * 16 + cur - '0';
4431 else if ((cur >= 'a') && (cur <= 'f'))
4432 ret = ret * 16 + 10 + (cur - 'a');
4433 else
4434 ret = ret * 16 + 10 + (cur - 'A');
4435 NEXT;
4436 cur = CUR;
4437 }
4438 } else {
4439 ERROR("Char ref: expecting [0-9A-F]");
4440 return(-1);
4441 }
4442 } else {
4443 if ((cur >= '0') && (cur <= '9')) {
4444 while ((cur >= '0') && (cur <= '9')) {
4445 ret = ret * 10 + cur - '0';
4446 NEXT;
4447 cur = CUR;
4448 }
4449 } else {
4450 ERROR("Char ref: expecting [0-9]");
4451 return(-1);
4452 }
4453 }
4454 if (cur != ';') {
4455 ERROR("Char ref: expecting ';'");
4456 return(-1);
4457 } else {
4458 NEXT;
4459 }
4460 return(ret);
4461}
4462
4463/**
4464 * xmlFAParseCharRange:
Daniel Veillard441bc322002-04-20 17:38:48 +00004465 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00004466 *
4467 * [17] charRange ::= seRange | XmlCharRef | XmlCharIncDash
4468 * [18] seRange ::= charOrEsc '-' charOrEsc
4469 * [20] charOrEsc ::= XmlChar | SingleCharEsc
4470 * [21] XmlChar ::= [^\#x2D#x5B#x5D]
4471 * [22] XmlCharIncDash ::= [^\#x5B#x5D]
4472 */
4473static void
4474xmlFAParseCharRange(xmlRegParserCtxtPtr ctxt) {
William M. Brackdc99df92003-12-27 01:54:25 +00004475 int cur, len;
Daniel Veillard4255d502002-04-16 15:50:10 +00004476 int start = -1;
4477 int end = -1;
4478
4479 if ((CUR == '&') && (NXT(1) == '#')) {
4480 end = start = xmlFAParseCharRef(ctxt);
4481 xmlRegAtomAddRange(ctxt, ctxt->atom, ctxt->neg,
4482 XML_REGEXP_CHARVAL, start, end, NULL);
4483 return;
4484 }
4485 cur = CUR;
4486 if (cur == '\\') {
4487 NEXT;
4488 cur = CUR;
4489 switch (cur) {
4490 case 'n': start = 0xA; break;
4491 case 'r': start = 0xD; break;
4492 case 't': start = 0x9; break;
4493 case '\\': case '|': case '.': case '-': case '^': case '?':
4494 case '*': case '+': case '{': case '}': case '(': case ')':
4495 case '[': case ']':
4496 start = cur; break;
4497 default:
4498 ERROR("Invalid escape value");
4499 return;
4500 }
4501 end = start;
William M. Brackdc99df92003-12-27 01:54:25 +00004502 len = 1;
Daniel Veillard4255d502002-04-16 15:50:10 +00004503 } else if ((cur != 0x5B) && (cur != 0x5D)) {
William M. Brackdc99df92003-12-27 01:54:25 +00004504 end = start = CUR_SCHAR(ctxt->cur, len);
Daniel Veillard4255d502002-04-16 15:50:10 +00004505 } else {
4506 ERROR("Expecting a char range");
4507 return;
4508 }
William M. Brackdc99df92003-12-27 01:54:25 +00004509 NEXTL(len);
Daniel Veillard4255d502002-04-16 15:50:10 +00004510 if (start == '-') {
4511 return;
4512 }
4513 cur = CUR;
William M. Brack10f1ef42004-03-20 14:51:25 +00004514 if ((cur != '-') || (NXT(1) == ']')) {
Daniel Veillard4255d502002-04-16 15:50:10 +00004515 xmlRegAtomAddRange(ctxt, ctxt->atom, ctxt->neg,
4516 XML_REGEXP_CHARVAL, start, end, NULL);
4517 return;
4518 }
4519 NEXT;
4520 cur = CUR;
4521 if (cur == '\\') {
4522 NEXT;
4523 cur = CUR;
4524 switch (cur) {
4525 case 'n': end = 0xA; break;
4526 case 'r': end = 0xD; break;
4527 case 't': end = 0x9; break;
4528 case '\\': case '|': case '.': case '-': case '^': case '?':
4529 case '*': case '+': case '{': case '}': case '(': case ')':
4530 case '[': case ']':
4531 end = cur; break;
4532 default:
4533 ERROR("Invalid escape value");
4534 return;
4535 }
William M. Brackdc99df92003-12-27 01:54:25 +00004536 len = 1;
Daniel Veillard4255d502002-04-16 15:50:10 +00004537 } else if ((cur != 0x5B) && (cur != 0x5D)) {
William M. Brackdc99df92003-12-27 01:54:25 +00004538 end = CUR_SCHAR(ctxt->cur, len);
Daniel Veillard4255d502002-04-16 15:50:10 +00004539 } else {
4540 ERROR("Expecting the end of a char range");
4541 return;
4542 }
William M. Brackdc99df92003-12-27 01:54:25 +00004543 NEXTL(len);
Daniel Veillard4255d502002-04-16 15:50:10 +00004544 /* TODO check that the values are acceptable character ranges for XML */
4545 if (end < start) {
4546 ERROR("End of range is before start of range");
4547 } else {
4548 xmlRegAtomAddRange(ctxt, ctxt->atom, ctxt->neg,
4549 XML_REGEXP_CHARVAL, start, end, NULL);
4550 }
4551 return;
4552}
4553
4554/**
4555 * xmlFAParsePosCharGroup:
Daniel Veillard441bc322002-04-20 17:38:48 +00004556 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00004557 *
4558 * [14] posCharGroup ::= ( charRange | charClassEsc )+
4559 */
4560static void
4561xmlFAParsePosCharGroup(xmlRegParserCtxtPtr ctxt) {
4562 do {
4563 if ((CUR == '\\') || (CUR == '.')) {
4564 xmlFAParseCharClassEsc(ctxt);
4565 } else {
4566 xmlFAParseCharRange(ctxt);
4567 }
4568 } while ((CUR != ']') && (CUR != '^') && (CUR != '-') &&
4569 (ctxt->error == 0));
4570}
4571
4572/**
4573 * xmlFAParseCharGroup:
Daniel Veillard441bc322002-04-20 17:38:48 +00004574 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00004575 *
4576 * [13] charGroup ::= posCharGroup | negCharGroup | charClassSub
4577 * [15] negCharGroup ::= '^' posCharGroup
4578 * [16] charClassSub ::= ( posCharGroup | negCharGroup ) '-' charClassExpr
4579 * [12] charClassExpr ::= '[' charGroup ']'
4580 */
4581static void
4582xmlFAParseCharGroup(xmlRegParserCtxtPtr ctxt) {
4583 int n = ctxt->neg;
4584 while ((CUR != ']') && (ctxt->error == 0)) {
4585 if (CUR == '^') {
4586 int neg = ctxt->neg;
4587
4588 NEXT;
4589 ctxt->neg = !ctxt->neg;
4590 xmlFAParsePosCharGroup(ctxt);
4591 ctxt->neg = neg;
William M. Brack10f1ef42004-03-20 14:51:25 +00004592 } else if ((CUR == '-') && (NXT(1) == '[')) {
Daniel Veillardf8b9de32003-11-24 14:27:26 +00004593 int neg = ctxt->neg;
Daniel Veillardf8b9de32003-11-24 14:27:26 +00004594 ctxt->neg = 2;
William M. Brack10f1ef42004-03-20 14:51:25 +00004595 NEXT; /* eat the '-' */
4596 NEXT; /* eat the '[' */
Daniel Veillard4255d502002-04-16 15:50:10 +00004597 xmlFAParseCharGroup(ctxt);
4598 if (CUR == ']') {
4599 NEXT;
4600 } else {
4601 ERROR("charClassExpr: ']' expected");
4602 break;
4603 }
Daniel Veillardf8b9de32003-11-24 14:27:26 +00004604 ctxt->neg = neg;
Daniel Veillard4255d502002-04-16 15:50:10 +00004605 break;
4606 } else if (CUR != ']') {
4607 xmlFAParsePosCharGroup(ctxt);
4608 }
4609 }
4610 ctxt->neg = n;
4611}
4612
4613/**
4614 * xmlFAParseCharClass:
Daniel Veillard441bc322002-04-20 17:38:48 +00004615 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00004616 *
4617 * [11] charClass ::= charClassEsc | charClassExpr
4618 * [12] charClassExpr ::= '[' charGroup ']'
4619 */
4620static void
4621xmlFAParseCharClass(xmlRegParserCtxtPtr ctxt) {
4622 if (CUR == '[') {
4623 NEXT;
4624 ctxt->atom = xmlRegNewAtom(ctxt, XML_REGEXP_RANGES);
4625 if (ctxt->atom == NULL)
4626 return;
4627 xmlFAParseCharGroup(ctxt);
4628 if (CUR == ']') {
4629 NEXT;
4630 } else {
4631 ERROR("xmlFAParseCharClass: ']' expected");
4632 }
4633 } else {
4634 xmlFAParseCharClassEsc(ctxt);
4635 }
4636}
4637
4638/**
4639 * xmlFAParseQuantExact:
Daniel Veillard441bc322002-04-20 17:38:48 +00004640 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00004641 *
4642 * [8] QuantExact ::= [0-9]+
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00004643 *
4644 * Returns 0 if success or -1 in case of error
Daniel Veillard4255d502002-04-16 15:50:10 +00004645 */
4646static int
4647xmlFAParseQuantExact(xmlRegParserCtxtPtr ctxt) {
4648 int ret = 0;
4649 int ok = 0;
4650
4651 while ((CUR >= '0') && (CUR <= '9')) {
4652 ret = ret * 10 + (CUR - '0');
4653 ok = 1;
4654 NEXT;
4655 }
4656 if (ok != 1) {
4657 return(-1);
4658 }
4659 return(ret);
4660}
4661
4662/**
4663 * xmlFAParseQuantifier:
Daniel Veillard441bc322002-04-20 17:38:48 +00004664 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00004665 *
4666 * [4] quantifier ::= [?*+] | ( '{' quantity '}' )
4667 * [5] quantity ::= quantRange | quantMin | QuantExact
4668 * [6] quantRange ::= QuantExact ',' QuantExact
4669 * [7] quantMin ::= QuantExact ','
4670 * [8] QuantExact ::= [0-9]+
4671 */
4672static int
4673xmlFAParseQuantifier(xmlRegParserCtxtPtr ctxt) {
4674 int cur;
4675
4676 cur = CUR;
4677 if ((cur == '?') || (cur == '*') || (cur == '+')) {
4678 if (ctxt->atom != NULL) {
4679 if (cur == '?')
4680 ctxt->atom->quant = XML_REGEXP_QUANT_OPT;
4681 else if (cur == '*')
4682 ctxt->atom->quant = XML_REGEXP_QUANT_MULT;
4683 else if (cur == '+')
4684 ctxt->atom->quant = XML_REGEXP_QUANT_PLUS;
4685 }
4686 NEXT;
4687 return(1);
4688 }
4689 if (cur == '{') {
4690 int min = 0, max = 0;
4691
4692 NEXT;
4693 cur = xmlFAParseQuantExact(ctxt);
4694 if (cur >= 0)
4695 min = cur;
4696 if (CUR == ',') {
4697 NEXT;
Daniel Veillardebe48c62003-12-03 12:12:27 +00004698 if (CUR == '}')
4699 max = INT_MAX;
4700 else {
4701 cur = xmlFAParseQuantExact(ctxt);
4702 if (cur >= 0)
4703 max = cur;
4704 else {
4705 ERROR("Improper quantifier");
4706 }
4707 }
Daniel Veillard4255d502002-04-16 15:50:10 +00004708 }
4709 if (CUR == '}') {
4710 NEXT;
4711 } else {
4712 ERROR("Unterminated quantifier");
4713 }
4714 if (max == 0)
4715 max = min;
4716 if (ctxt->atom != NULL) {
4717 ctxt->atom->quant = XML_REGEXP_QUANT_RANGE;
4718 ctxt->atom->min = min;
4719 ctxt->atom->max = max;
4720 }
4721 return(1);
4722 }
4723 return(0);
4724}
4725
4726/**
4727 * xmlFAParseAtom:
Daniel Veillard441bc322002-04-20 17:38:48 +00004728 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00004729 *
4730 * [9] atom ::= Char | charClass | ( '(' regExp ')' )
4731 */
4732static int
4733xmlFAParseAtom(xmlRegParserCtxtPtr ctxt) {
4734 int codepoint, len;
4735
4736 codepoint = xmlFAIsChar(ctxt);
4737 if (codepoint > 0) {
4738 ctxt->atom = xmlRegNewAtom(ctxt, XML_REGEXP_CHARVAL);
4739 if (ctxt->atom == NULL)
4740 return(-1);
4741 codepoint = CUR_SCHAR(ctxt->cur, len);
4742 ctxt->atom->codepoint = codepoint;
4743 NEXTL(len);
4744 return(1);
4745 } else if (CUR == '|') {
4746 return(0);
4747 } else if (CUR == 0) {
4748 return(0);
4749 } else if (CUR == ')') {
4750 return(0);
4751 } else if (CUR == '(') {
4752 xmlRegStatePtr start, oldend;
4753
4754 NEXT;
4755 xmlFAGenerateEpsilonTransition(ctxt, ctxt->state, NULL);
4756 start = ctxt->state;
4757 oldend = ctxt->end;
4758 ctxt->end = NULL;
4759 ctxt->atom = NULL;
4760 xmlFAParseRegExp(ctxt, 0);
4761 if (CUR == ')') {
4762 NEXT;
4763 } else {
4764 ERROR("xmlFAParseAtom: expecting ')'");
4765 }
4766 ctxt->atom = xmlRegNewAtom(ctxt, XML_REGEXP_SUBREG);
4767 if (ctxt->atom == NULL)
4768 return(-1);
4769 ctxt->atom->start = start;
4770 ctxt->atom->stop = ctxt->state;
4771 ctxt->end = oldend;
4772 return(1);
4773 } else if ((CUR == '[') || (CUR == '\\') || (CUR == '.')) {
4774 xmlFAParseCharClass(ctxt);
4775 return(1);
4776 }
4777 return(0);
4778}
4779
4780/**
4781 * xmlFAParsePiece:
Daniel Veillard441bc322002-04-20 17:38:48 +00004782 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00004783 *
4784 * [3] piece ::= atom quantifier?
4785 */
4786static int
4787xmlFAParsePiece(xmlRegParserCtxtPtr ctxt) {
4788 int ret;
4789
4790 ctxt->atom = NULL;
4791 ret = xmlFAParseAtom(ctxt);
4792 if (ret == 0)
4793 return(0);
4794 if (ctxt->atom == NULL) {
4795 ERROR("internal: no atom generated");
4796 }
4797 xmlFAParseQuantifier(ctxt);
4798 return(1);
4799}
4800
4801/**
4802 * xmlFAParseBranch:
Daniel Veillard441bc322002-04-20 17:38:48 +00004803 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00004804 *
4805 * [2] branch ::= piece*
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00004806 8
Daniel Veillard4255d502002-04-16 15:50:10 +00004807 */
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00004808static int
Daniel Veillard2cbf5962004-03-31 15:50:43 +00004809xmlFAParseBranch(xmlRegParserCtxtPtr ctxt) {
Daniel Veillard4255d502002-04-16 15:50:10 +00004810 xmlRegStatePtr previous;
Daniel Veillard4255d502002-04-16 15:50:10 +00004811 int ret;
4812
4813 previous = ctxt->state;
4814 ret = xmlFAParsePiece(ctxt);
4815 if (ret != 0) {
Daniel Veillard2cbf5962004-03-31 15:50:43 +00004816 if (xmlFAGenerateTransitions(ctxt, previous, NULL, ctxt->atom) < 0)
4817 return(-1);
4818 previous = ctxt->state;
Daniel Veillard4255d502002-04-16 15:50:10 +00004819 ctxt->atom = NULL;
4820 }
4821 while ((ret != 0) && (ctxt->error == 0)) {
4822 ret = xmlFAParsePiece(ctxt);
4823 if (ret != 0) {
Daniel Veillard2cbf5962004-03-31 15:50:43 +00004824 if (xmlFAGenerateTransitions(ctxt, previous, NULL,
4825 ctxt->atom) < 0)
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00004826 return(-1);
Daniel Veillard4255d502002-04-16 15:50:10 +00004827 previous = ctxt->state;
4828 ctxt->atom = NULL;
4829 }
4830 }
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00004831 return(0);
Daniel Veillard4255d502002-04-16 15:50:10 +00004832}
4833
4834/**
4835 * xmlFAParseRegExp:
Daniel Veillard441bc322002-04-20 17:38:48 +00004836 * @ctxt: a regexp parser context
William M. Brackddf71d62004-05-06 04:17:26 +00004837 * @top: is this the top-level expression ?
Daniel Veillard4255d502002-04-16 15:50:10 +00004838 *
4839 * [1] regExp ::= branch ( '|' branch )*
4840 */
4841static void
4842xmlFAParseRegExp(xmlRegParserCtxtPtr ctxt, int top) {
Daniel Veillardc7e3cc42004-09-28 12:33:52 +00004843 xmlRegStatePtr start, end;
Daniel Veillard4255d502002-04-16 15:50:10 +00004844
Daniel Veillard2cbf5962004-03-31 15:50:43 +00004845 /* if not top start should have been generated by an epsilon trans */
Daniel Veillard4255d502002-04-16 15:50:10 +00004846 start = ctxt->state;
Daniel Veillard2cbf5962004-03-31 15:50:43 +00004847 ctxt->end = NULL;
4848 xmlFAParseBranch(ctxt);
4849 if (top) {
4850#ifdef DEBUG_REGEXP_GRAPH
4851 printf("State %d is final\n", ctxt->state->no);
4852#endif
4853 ctxt->state->type = XML_REGEXP_FINAL_STATE;
4854 }
Daniel Veillard4255d502002-04-16 15:50:10 +00004855 if (CUR != '|') {
4856 ctxt->end = ctxt->state;
4857 return;
4858 }
4859 end = ctxt->state;
4860 while ((CUR == '|') && (ctxt->error == 0)) {
4861 NEXT;
4862 ctxt->state = start;
Daniel Veillard2cbf5962004-03-31 15:50:43 +00004863 ctxt->end = NULL;
4864 xmlFAParseBranch(ctxt);
4865 if (top) {
4866 ctxt->state->type = XML_REGEXP_FINAL_STATE;
4867#ifdef DEBUG_REGEXP_GRAPH
4868 printf("State %d is final\n", ctxt->state->no);
4869#endif
4870 } else {
4871 xmlFAGenerateEpsilonTransition(ctxt, ctxt->state, end);
4872 }
Daniel Veillard4255d502002-04-16 15:50:10 +00004873 }
Daniel Veillard2cbf5962004-03-31 15:50:43 +00004874 if (!top) {
4875 ctxt->state = end;
4876 ctxt->end = end;
4877 }
Daniel Veillard4255d502002-04-16 15:50:10 +00004878}
4879
4880/************************************************************************
4881 * *
4882 * The basic API *
4883 * *
4884 ************************************************************************/
4885
4886/**
4887 * xmlRegexpPrint:
4888 * @output: the file for the output debug
4889 * @regexp: the compiled regexp
4890 *
4891 * Print the content of the compiled regular expression
4892 */
4893void
4894xmlRegexpPrint(FILE *output, xmlRegexpPtr regexp) {
4895 int i;
4896
Daniel Veillarda82b1822004-11-08 16:24:57 +00004897 if (output == NULL)
4898 return;
Daniel Veillard4255d502002-04-16 15:50:10 +00004899 fprintf(output, " regexp: ");
4900 if (regexp == NULL) {
4901 fprintf(output, "NULL\n");
4902 return;
4903 }
4904 fprintf(output, "'%s' ", regexp->string);
4905 fprintf(output, "\n");
4906 fprintf(output, "%d atoms:\n", regexp->nbAtoms);
4907 for (i = 0;i < regexp->nbAtoms; i++) {
4908 fprintf(output, " %02d ", i);
4909 xmlRegPrintAtom(output, regexp->atoms[i]);
4910 }
4911 fprintf(output, "%d states:", regexp->nbStates);
4912 fprintf(output, "\n");
4913 for (i = 0;i < regexp->nbStates; i++) {
4914 xmlRegPrintState(output, regexp->states[i]);
4915 }
4916 fprintf(output, "%d counters:\n", regexp->nbCounters);
4917 for (i = 0;i < regexp->nbCounters; i++) {
4918 fprintf(output, " %d: min %d max %d\n", i, regexp->counters[i].min,
4919 regexp->counters[i].max);
4920 }
4921}
4922
4923/**
4924 * xmlRegexpCompile:
4925 * @regexp: a regular expression string
4926 *
4927 * Parses a regular expression conforming to XML Schemas Part 2 Datatype
William M. Brackddf71d62004-05-06 04:17:26 +00004928 * Appendix F and builds an automata suitable for testing strings against
Daniel Veillard4255d502002-04-16 15:50:10 +00004929 * that regular expression
4930 *
4931 * Returns the compiled expression or NULL in case of error
4932 */
4933xmlRegexpPtr
4934xmlRegexpCompile(const xmlChar *regexp) {
4935 xmlRegexpPtr ret;
4936 xmlRegParserCtxtPtr ctxt;
4937
4938 ctxt = xmlRegNewParserCtxt(regexp);
4939 if (ctxt == NULL)
4940 return(NULL);
4941
4942 /* initialize the parser */
4943 ctxt->end = NULL;
4944 ctxt->start = ctxt->state = xmlRegNewState(ctxt);
4945 xmlRegStatePush(ctxt, ctxt->start);
4946
4947 /* parse the expression building an automata */
4948 xmlFAParseRegExp(ctxt, 1);
4949 if (CUR != 0) {
4950 ERROR("xmlFAParseRegExp: extra characters");
4951 }
4952 ctxt->end = ctxt->state;
4953 ctxt->start->type = XML_REGEXP_START_STATE;
4954 ctxt->end->type = XML_REGEXP_FINAL_STATE;
4955
4956 /* remove the Epsilon except for counted transitions */
4957 xmlFAEliminateEpsilonTransitions(ctxt);
4958
4959
4960 if (ctxt->error != 0) {
4961 xmlRegFreeParserCtxt(ctxt);
4962 return(NULL);
4963 }
4964 ret = xmlRegEpxFromParse(ctxt);
4965 xmlRegFreeParserCtxt(ctxt);
4966 return(ret);
4967}
4968
4969/**
4970 * xmlRegexpExec:
4971 * @comp: the compiled regular expression
4972 * @content: the value to check against the regular expression
4973 *
William M. Brackddf71d62004-05-06 04:17:26 +00004974 * Check if the regular expression generates the value
Daniel Veillard4255d502002-04-16 15:50:10 +00004975 *
William M. Brackddf71d62004-05-06 04:17:26 +00004976 * Returns 1 if it matches, 0 if not and a negative value in case of error
Daniel Veillard4255d502002-04-16 15:50:10 +00004977 */
4978int
4979xmlRegexpExec(xmlRegexpPtr comp, const xmlChar *content) {
4980 if ((comp == NULL) || (content == NULL))
4981 return(-1);
4982 return(xmlFARegExec(comp, content));
4983}
4984
4985/**
Daniel Veillard23e73572002-09-19 19:56:43 +00004986 * xmlRegexpIsDeterminist:
4987 * @comp: the compiled regular expression
4988 *
4989 * Check if the regular expression is determinist
4990 *
William M. Brackddf71d62004-05-06 04:17:26 +00004991 * Returns 1 if it yes, 0 if not and a negative value in case of error
Daniel Veillard23e73572002-09-19 19:56:43 +00004992 */
4993int
4994xmlRegexpIsDeterminist(xmlRegexpPtr comp) {
4995 xmlAutomataPtr am;
4996 int ret;
4997
4998 if (comp == NULL)
4999 return(-1);
5000 if (comp->determinist != -1)
5001 return(comp->determinist);
5002
5003 am = xmlNewAutomata();
Daniel Veillardbd9afb52002-09-25 22:25:35 +00005004 if (am->states != NULL) {
5005 int i;
5006
5007 for (i = 0;i < am->nbStates;i++)
5008 xmlRegFreeState(am->states[i]);
5009 xmlFree(am->states);
5010 }
Daniel Veillard23e73572002-09-19 19:56:43 +00005011 am->nbAtoms = comp->nbAtoms;
5012 am->atoms = comp->atoms;
5013 am->nbStates = comp->nbStates;
5014 am->states = comp->states;
5015 am->determinist = -1;
5016 ret = xmlFAComputesDeterminism(am);
5017 am->atoms = NULL;
5018 am->states = NULL;
5019 xmlFreeAutomata(am);
5020 return(ret);
5021}
5022
5023/**
Daniel Veillard4255d502002-04-16 15:50:10 +00005024 * xmlRegFreeRegexp:
5025 * @regexp: the regexp
5026 *
5027 * Free a regexp
5028 */
5029void
5030xmlRegFreeRegexp(xmlRegexpPtr regexp) {
5031 int i;
5032 if (regexp == NULL)
5033 return;
5034
5035 if (regexp->string != NULL)
5036 xmlFree(regexp->string);
5037 if (regexp->states != NULL) {
5038 for (i = 0;i < regexp->nbStates;i++)
5039 xmlRegFreeState(regexp->states[i]);
5040 xmlFree(regexp->states);
5041 }
5042 if (regexp->atoms != NULL) {
5043 for (i = 0;i < regexp->nbAtoms;i++)
5044 xmlRegFreeAtom(regexp->atoms[i]);
5045 xmlFree(regexp->atoms);
5046 }
5047 if (regexp->counters != NULL)
5048 xmlFree(regexp->counters);
Daniel Veillard23e73572002-09-19 19:56:43 +00005049 if (regexp->compact != NULL)
5050 xmlFree(regexp->compact);
Daniel Veillard118aed72002-09-24 14:13:13 +00005051 if (regexp->transdata != NULL)
5052 xmlFree(regexp->transdata);
Daniel Veillard23e73572002-09-19 19:56:43 +00005053 if (regexp->stringMap != NULL) {
5054 for (i = 0; i < regexp->nbstrings;i++)
5055 xmlFree(regexp->stringMap[i]);
5056 xmlFree(regexp->stringMap);
5057 }
5058
Daniel Veillard4255d502002-04-16 15:50:10 +00005059 xmlFree(regexp);
5060}
5061
5062#ifdef LIBXML_AUTOMATA_ENABLED
5063/************************************************************************
5064 * *
5065 * The Automata interface *
5066 * *
5067 ************************************************************************/
5068
5069/**
5070 * xmlNewAutomata:
5071 *
5072 * Create a new automata
5073 *
5074 * Returns the new object or NULL in case of failure
5075 */
5076xmlAutomataPtr
5077xmlNewAutomata(void) {
5078 xmlAutomataPtr ctxt;
5079
5080 ctxt = xmlRegNewParserCtxt(NULL);
5081 if (ctxt == NULL)
5082 return(NULL);
5083
5084 /* initialize the parser */
5085 ctxt->end = NULL;
5086 ctxt->start = ctxt->state = xmlRegNewState(ctxt);
Daniel Veillarddb68b742005-07-30 13:18:24 +00005087 ctxt->start->type = XML_REGEXP_START_STATE;
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00005088 if (ctxt->start == NULL) {
5089 xmlFreeAutomata(ctxt);
5090 return(NULL);
5091 }
5092 if (xmlRegStatePush(ctxt, ctxt->start) < 0) {
5093 xmlRegFreeState(ctxt->start);
5094 xmlFreeAutomata(ctxt);
5095 return(NULL);
5096 }
Daniel Veillard4255d502002-04-16 15:50:10 +00005097
5098 return(ctxt);
5099}
5100
5101/**
5102 * xmlFreeAutomata:
5103 * @am: an automata
5104 *
5105 * Free an automata
5106 */
5107void
5108xmlFreeAutomata(xmlAutomataPtr am) {
5109 if (am == NULL)
5110 return;
5111 xmlRegFreeParserCtxt(am);
5112}
5113
5114/**
5115 * xmlAutomataGetInitState:
5116 * @am: an automata
5117 *
Daniel Veillarda9b66d02002-12-11 14:23:49 +00005118 * Initial state lookup
5119 *
Daniel Veillard4255d502002-04-16 15:50:10 +00005120 * Returns the initial state of the automata
5121 */
5122xmlAutomataStatePtr
5123xmlAutomataGetInitState(xmlAutomataPtr am) {
5124 if (am == NULL)
5125 return(NULL);
5126 return(am->start);
5127}
5128
5129/**
5130 * xmlAutomataSetFinalState:
5131 * @am: an automata
5132 * @state: a state in this automata
5133 *
5134 * Makes that state a final state
5135 *
5136 * Returns 0 or -1 in case of error
5137 */
5138int
5139xmlAutomataSetFinalState(xmlAutomataPtr am, xmlAutomataStatePtr state) {
5140 if ((am == NULL) || (state == NULL))
5141 return(-1);
5142 state->type = XML_REGEXP_FINAL_STATE;
5143 return(0);
5144}
5145
5146/**
5147 * xmlAutomataNewTransition:
5148 * @am: an automata
5149 * @from: the starting point of the transition
5150 * @to: the target point of the transition or NULL
5151 * @token: the input string associated to that transition
5152 * @data: data passed to the callback function if the transition is activated
5153 *
William M. Brackddf71d62004-05-06 04:17:26 +00005154 * If @to is NULL, this creates first a new target state in the automata
Daniel Veillard4255d502002-04-16 15:50:10 +00005155 * and then adds a transition from the @from state to the target state
5156 * activated by the value of @token
5157 *
5158 * Returns the target state or NULL in case of error
5159 */
5160xmlAutomataStatePtr
5161xmlAutomataNewTransition(xmlAutomataPtr am, xmlAutomataStatePtr from,
5162 xmlAutomataStatePtr to, const xmlChar *token,
5163 void *data) {
5164 xmlRegAtomPtr atom;
5165
5166 if ((am == NULL) || (from == NULL) || (token == NULL))
5167 return(NULL);
5168 atom = xmlRegNewAtom(am, XML_REGEXP_STRING);
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00005169 if (atom == NULL)
5170 return(NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +00005171 atom->data = data;
5172 if (atom == NULL)
5173 return(NULL);
5174 atom->valuep = xmlStrdup(token);
5175
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00005176 if (xmlFAGenerateTransitions(am, from, to, atom) < 0) {
5177 xmlRegFreeAtom(atom);
5178 return(NULL);
5179 }
Daniel Veillard4255d502002-04-16 15:50:10 +00005180 if (to == NULL)
5181 return(am->state);
5182 return(to);
5183}
5184
5185/**
Daniel Veillard52b48c72003-04-13 19:53:42 +00005186 * xmlAutomataNewTransition2:
5187 * @am: an automata
5188 * @from: the starting point of the transition
5189 * @to: the target point of the transition or NULL
5190 * @token: the first input string associated to that transition
5191 * @token2: the second input string associated to that transition
5192 * @data: data passed to the callback function if the transition is activated
5193 *
William M. Brackddf71d62004-05-06 04:17:26 +00005194 * If @to is NULL, this creates first a new target state in the automata
Daniel Veillard52b48c72003-04-13 19:53:42 +00005195 * and then adds a transition from the @from state to the target state
5196 * activated by the value of @token
5197 *
5198 * Returns the target state or NULL in case of error
5199 */
5200xmlAutomataStatePtr
5201xmlAutomataNewTransition2(xmlAutomataPtr am, xmlAutomataStatePtr from,
5202 xmlAutomataStatePtr to, const xmlChar *token,
5203 const xmlChar *token2, void *data) {
5204 xmlRegAtomPtr atom;
5205
5206 if ((am == NULL) || (from == NULL) || (token == NULL))
5207 return(NULL);
5208 atom = xmlRegNewAtom(am, XML_REGEXP_STRING);
5209 atom->data = data;
5210 if (atom == NULL)
5211 return(NULL);
5212 if ((token2 == NULL) || (*token2 == 0)) {
5213 atom->valuep = xmlStrdup(token);
5214 } else {
5215 int lenn, lenp;
5216 xmlChar *str;
5217
5218 lenn = strlen((char *) token2);
5219 lenp = strlen((char *) token);
5220
Daniel Veillard3c908dc2003-04-19 00:07:51 +00005221 str = (xmlChar *) xmlMallocAtomic(lenn + lenp + 2);
Daniel Veillard52b48c72003-04-13 19:53:42 +00005222 if (str == NULL) {
5223 xmlRegFreeAtom(atom);
5224 return(NULL);
5225 }
5226 memcpy(&str[0], token, lenp);
5227 str[lenp] = '|';
5228 memcpy(&str[lenp + 1], token2, lenn);
5229 str[lenn + lenp + 1] = 0;
5230
5231 atom->valuep = str;
5232 }
5233
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00005234 if (xmlFAGenerateTransitions(am, from, to, atom) < 0) {
5235 xmlRegFreeAtom(atom);
5236 return(NULL);
5237 }
Daniel Veillard52b48c72003-04-13 19:53:42 +00005238 if (to == NULL)
5239 return(am->state);
5240 return(to);
5241}
5242
5243/**
Daniel Veillard9efc4762005-07-19 14:33:55 +00005244 * xmlAutomataNewNegTrans:
5245 * @am: an automata
5246 * @from: the starting point of the transition
5247 * @to: the target point of the transition or NULL
5248 * @token: the first input string associated to that transition
5249 * @token2: the second input string associated to that transition
5250 * @data: data passed to the callback function if the transition is activated
5251 *
5252 * If @to is NULL, this creates first a new target state in the automata
5253 * and then adds a transition from the @from state to the target state
5254 * activated by any value except (@token,@token2)
Daniel Veillard6e65e152005-08-09 11:09:52 +00005255 * Note that if @token2 is not NULL, then (X, NULL) won't match to follow
5256 # the semantic of XSD ##other
Daniel Veillard9efc4762005-07-19 14:33:55 +00005257 *
5258 * Returns the target state or NULL in case of error
5259 */
5260xmlAutomataStatePtr
5261xmlAutomataNewNegTrans(xmlAutomataPtr am, xmlAutomataStatePtr from,
5262 xmlAutomataStatePtr to, const xmlChar *token,
5263 const xmlChar *token2, void *data) {
5264 xmlRegAtomPtr atom;
Daniel Veillard77005e62005-07-19 16:26:18 +00005265 xmlChar err_msg[200];
Daniel Veillard9efc4762005-07-19 14:33:55 +00005266
5267 if ((am == NULL) || (from == NULL) || (token == NULL))
5268 return(NULL);
5269 atom = xmlRegNewAtom(am, XML_REGEXP_STRING);
5270 if (atom == NULL)
5271 return(NULL);
5272 atom->data = data;
5273 atom->neg = 1;
5274 if ((token2 == NULL) || (*token2 == 0)) {
5275 atom->valuep = xmlStrdup(token);
5276 } else {
5277 int lenn, lenp;
5278 xmlChar *str;
5279
5280 lenn = strlen((char *) token2);
5281 lenp = strlen((char *) token);
5282
5283 str = (xmlChar *) xmlMallocAtomic(lenn + lenp + 2);
5284 if (str == NULL) {
5285 xmlRegFreeAtom(atom);
5286 return(NULL);
5287 }
5288 memcpy(&str[0], token, lenp);
5289 str[lenp] = '|';
5290 memcpy(&str[lenp + 1], token2, lenn);
5291 str[lenn + lenp + 1] = 0;
5292
5293 atom->valuep = str;
5294 }
Daniel Veillarddb68b742005-07-30 13:18:24 +00005295 snprintf((char *) err_msg, 199, "not %s", (const char *) atom->valuep);
Daniel Veillard77005e62005-07-19 16:26:18 +00005296 err_msg[199] = 0;
5297 atom->valuep2 = xmlStrdup(err_msg);
Daniel Veillard9efc4762005-07-19 14:33:55 +00005298
5299 if (xmlFAGenerateTransitions(am, from, to, atom) < 0) {
5300 xmlRegFreeAtom(atom);
5301 return(NULL);
5302 }
Daniel Veillard6e65e152005-08-09 11:09:52 +00005303 am->negs++;
Daniel Veillard9efc4762005-07-19 14:33:55 +00005304 if (to == NULL)
5305 return(am->state);
5306 return(to);
5307}
5308
5309/**
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00005310 * xmlAutomataNewCountTrans2:
5311 * @am: an automata
5312 * @from: the starting point of the transition
5313 * @to: the target point of the transition or NULL
5314 * @token: the input string associated to that transition
5315 * @token2: the second input string associated to that transition
5316 * @min: the minimum successive occurences of token
5317 * @max: the maximum successive occurences of token
5318 * @data: data associated to the transition
5319 *
5320 * If @to is NULL, this creates first a new target state in the automata
5321 * and then adds a transition from the @from state to the target state
5322 * activated by a succession of input of value @token and @token2 and
5323 * whose number is between @min and @max
5324 *
5325 * Returns the target state or NULL in case of error
5326 */
5327xmlAutomataStatePtr
5328xmlAutomataNewCountTrans2(xmlAutomataPtr am, xmlAutomataStatePtr from,
5329 xmlAutomataStatePtr to, const xmlChar *token,
5330 const xmlChar *token2,
5331 int min, int max, void *data) {
5332 xmlRegAtomPtr atom;
5333 int counter;
5334
5335 if ((am == NULL) || (from == NULL) || (token == NULL))
5336 return(NULL);
5337 if (min < 0)
5338 return(NULL);
5339 if ((max < min) || (max < 1))
5340 return(NULL);
5341 atom = xmlRegNewAtom(am, XML_REGEXP_STRING);
5342 if (atom == NULL)
5343 return(NULL);
5344 if ((token2 == NULL) || (*token2 == 0)) {
5345 atom->valuep = xmlStrdup(token);
5346 } else {
5347 int lenn, lenp;
5348 xmlChar *str;
5349
5350 lenn = strlen((char *) token2);
5351 lenp = strlen((char *) token);
5352
5353 str = (xmlChar *) xmlMallocAtomic(lenn + lenp + 2);
5354 if (str == NULL) {
5355 xmlRegFreeAtom(atom);
5356 return(NULL);
5357 }
5358 memcpy(&str[0], token, lenp);
5359 str[lenp] = '|';
5360 memcpy(&str[lenp + 1], token2, lenn);
5361 str[lenn + lenp + 1] = 0;
5362
5363 atom->valuep = str;
5364 }
5365 atom->data = data;
5366 if (min == 0)
5367 atom->min = 1;
5368 else
5369 atom->min = min;
5370 atom->max = max;
5371
5372 /*
5373 * associate a counter to the transition.
5374 */
5375 counter = xmlRegGetCounter(am);
5376 am->counters[counter].min = min;
5377 am->counters[counter].max = max;
5378
5379 /* xmlFAGenerateTransitions(am, from, to, atom); */
5380 if (to == NULL) {
5381 to = xmlRegNewState(am);
5382 xmlRegStatePush(am, to);
5383 }
Daniel Veillard5de09382005-09-26 17:18:17 +00005384 xmlRegStateAddTrans(am, from, atom, to, counter, -1);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00005385 xmlRegAtomPush(am, atom);
5386 am->state = to;
5387
5388 if (to == NULL)
5389 to = am->state;
5390 if (to == NULL)
5391 return(NULL);
5392 if (min == 0)
5393 xmlFAGenerateEpsilonTransition(am, from, to);
5394 return(to);
5395}
5396
5397/**
Daniel Veillard4255d502002-04-16 15:50:10 +00005398 * xmlAutomataNewCountTrans:
5399 * @am: an automata
5400 * @from: the starting point of the transition
5401 * @to: the target point of the transition or NULL
5402 * @token: the input string associated to that transition
5403 * @min: the minimum successive occurences of token
Daniel Veillarda9b66d02002-12-11 14:23:49 +00005404 * @max: the maximum successive occurences of token
5405 * @data: data associated to the transition
Daniel Veillard4255d502002-04-16 15:50:10 +00005406 *
William M. Brackddf71d62004-05-06 04:17:26 +00005407 * If @to is NULL, this creates first a new target state in the automata
Daniel Veillard4255d502002-04-16 15:50:10 +00005408 * and then adds a transition from the @from state to the target state
5409 * activated by a succession of input of value @token and whose number
5410 * is between @min and @max
5411 *
5412 * Returns the target state or NULL in case of error
5413 */
5414xmlAutomataStatePtr
5415xmlAutomataNewCountTrans(xmlAutomataPtr am, xmlAutomataStatePtr from,
5416 xmlAutomataStatePtr to, const xmlChar *token,
5417 int min, int max, void *data) {
5418 xmlRegAtomPtr atom;
Daniel Veillard0ddb21c2004-02-12 12:43:49 +00005419 int counter;
Daniel Veillard4255d502002-04-16 15:50:10 +00005420
5421 if ((am == NULL) || (from == NULL) || (token == NULL))
5422 return(NULL);
5423 if (min < 0)
5424 return(NULL);
5425 if ((max < min) || (max < 1))
5426 return(NULL);
5427 atom = xmlRegNewAtom(am, XML_REGEXP_STRING);
5428 if (atom == NULL)
5429 return(NULL);
5430 atom->valuep = xmlStrdup(token);
5431 atom->data = data;
5432 if (min == 0)
5433 atom->min = 1;
5434 else
5435 atom->min = min;
5436 atom->max = max;
5437
Daniel Veillard0ddb21c2004-02-12 12:43:49 +00005438 /*
5439 * associate a counter to the transition.
5440 */
5441 counter = xmlRegGetCounter(am);
5442 am->counters[counter].min = min;
5443 am->counters[counter].max = max;
5444
5445 /* xmlFAGenerateTransitions(am, from, to, atom); */
5446 if (to == NULL) {
5447 to = xmlRegNewState(am);
5448 xmlRegStatePush(am, to);
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00005449 }
Daniel Veillard5de09382005-09-26 17:18:17 +00005450 xmlRegStateAddTrans(am, from, atom, to, counter, -1);
Daniel Veillard0ddb21c2004-02-12 12:43:49 +00005451 xmlRegAtomPush(am, atom);
5452 am->state = to;
5453
Daniel Veillard4255d502002-04-16 15:50:10 +00005454 if (to == NULL)
5455 to = am->state;
5456 if (to == NULL)
5457 return(NULL);
5458 if (min == 0)
5459 xmlFAGenerateEpsilonTransition(am, from, to);
5460 return(to);
5461}
5462
5463/**
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00005464 * xmlAutomataNewOnceTrans2:
5465 * @am: an automata
5466 * @from: the starting point of the transition
5467 * @to: the target point of the transition or NULL
5468 * @token: the input string associated to that transition
5469 * @token2: the second input string associated to that transition
5470 * @min: the minimum successive occurences of token
5471 * @max: the maximum successive occurences of token
5472 * @data: data associated to the transition
5473 *
5474 * If @to is NULL, this creates first a new target state in the automata
5475 * and then adds a transition from the @from state to the target state
5476 * activated by a succession of input of value @token and @token2 and whose
5477 * number is between @min and @max, moreover that transition can only be
5478 * crossed once.
5479 *
5480 * Returns the target state or NULL in case of error
5481 */
5482xmlAutomataStatePtr
5483xmlAutomataNewOnceTrans2(xmlAutomataPtr am, xmlAutomataStatePtr from,
5484 xmlAutomataStatePtr to, const xmlChar *token,
5485 const xmlChar *token2,
5486 int min, int max, void *data) {
5487 xmlRegAtomPtr atom;
5488 int counter;
5489
5490 if ((am == NULL) || (from == NULL) || (token == NULL))
5491 return(NULL);
5492 if (min < 1)
5493 return(NULL);
5494 if ((max < min) || (max < 1))
5495 return(NULL);
5496 atom = xmlRegNewAtom(am, XML_REGEXP_STRING);
5497 if (atom == NULL)
5498 return(NULL);
5499 if ((token2 == NULL) || (*token2 == 0)) {
5500 atom->valuep = xmlStrdup(token);
5501 } else {
5502 int lenn, lenp;
5503 xmlChar *str;
5504
5505 lenn = strlen((char *) token2);
5506 lenp = strlen((char *) token);
5507
5508 str = (xmlChar *) xmlMallocAtomic(lenn + lenp + 2);
5509 if (str == NULL) {
5510 xmlRegFreeAtom(atom);
5511 return(NULL);
5512 }
5513 memcpy(&str[0], token, lenp);
5514 str[lenp] = '|';
5515 memcpy(&str[lenp + 1], token2, lenn);
5516 str[lenn + lenp + 1] = 0;
5517
5518 atom->valuep = str;
5519 }
5520 atom->data = data;
5521 atom->quant = XML_REGEXP_QUANT_ONCEONLY;
5522 if (min == 0)
5523 atom->min = 1;
5524 else
5525 atom->min = min;
5526 atom->max = max;
5527 /*
5528 * associate a counter to the transition.
5529 */
5530 counter = xmlRegGetCounter(am);
5531 am->counters[counter].min = 1;
5532 am->counters[counter].max = 1;
5533
5534 /* xmlFAGenerateTransitions(am, from, to, atom); */
5535 if (to == NULL) {
5536 to = xmlRegNewState(am);
5537 xmlRegStatePush(am, to);
5538 }
Daniel Veillard5de09382005-09-26 17:18:17 +00005539 xmlRegStateAddTrans(am, from, atom, to, counter, -1);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00005540 xmlRegAtomPush(am, atom);
5541 am->state = to;
5542 return(to);
5543}
5544
5545
5546
5547/**
Daniel Veillard7646b182002-04-20 06:41:40 +00005548 * xmlAutomataNewOnceTrans:
5549 * @am: an automata
5550 * @from: the starting point of the transition
5551 * @to: the target point of the transition or NULL
5552 * @token: the input string associated to that transition
5553 * @min: the minimum successive occurences of token
Daniel Veillarda9b66d02002-12-11 14:23:49 +00005554 * @max: the maximum successive occurences of token
5555 * @data: data associated to the transition
Daniel Veillard7646b182002-04-20 06:41:40 +00005556 *
William M. Brackddf71d62004-05-06 04:17:26 +00005557 * If @to is NULL, this creates first a new target state in the automata
Daniel Veillard7646b182002-04-20 06:41:40 +00005558 * and then adds a transition from the @from state to the target state
5559 * activated by a succession of input of value @token and whose number
William M. Brackddf71d62004-05-06 04:17:26 +00005560 * is between @min and @max, moreover that transition can only be crossed
Daniel Veillard7646b182002-04-20 06:41:40 +00005561 * once.
5562 *
5563 * Returns the target state or NULL in case of error
5564 */
5565xmlAutomataStatePtr
5566xmlAutomataNewOnceTrans(xmlAutomataPtr am, xmlAutomataStatePtr from,
5567 xmlAutomataStatePtr to, const xmlChar *token,
5568 int min, int max, void *data) {
5569 xmlRegAtomPtr atom;
5570 int counter;
5571
5572 if ((am == NULL) || (from == NULL) || (token == NULL))
5573 return(NULL);
5574 if (min < 1)
5575 return(NULL);
5576 if ((max < min) || (max < 1))
5577 return(NULL);
5578 atom = xmlRegNewAtom(am, XML_REGEXP_STRING);
5579 if (atom == NULL)
5580 return(NULL);
5581 atom->valuep = xmlStrdup(token);
5582 atom->data = data;
5583 atom->quant = XML_REGEXP_QUANT_ONCEONLY;
5584 if (min == 0)
5585 atom->min = 1;
5586 else
5587 atom->min = min;
5588 atom->max = max;
5589 /*
5590 * associate a counter to the transition.
5591 */
5592 counter = xmlRegGetCounter(am);
5593 am->counters[counter].min = 1;
5594 am->counters[counter].max = 1;
5595
5596 /* xmlFAGenerateTransitions(am, from, to, atom); */
5597 if (to == NULL) {
5598 to = xmlRegNewState(am);
5599 xmlRegStatePush(am, to);
5600 }
Daniel Veillard5de09382005-09-26 17:18:17 +00005601 xmlRegStateAddTrans(am, from, atom, to, counter, -1);
Daniel Veillard7646b182002-04-20 06:41:40 +00005602 xmlRegAtomPush(am, atom);
5603 am->state = to;
Daniel Veillard7646b182002-04-20 06:41:40 +00005604 return(to);
5605}
5606
5607/**
Daniel Veillard4255d502002-04-16 15:50:10 +00005608 * xmlAutomataNewState:
5609 * @am: an automata
5610 *
5611 * Create a new disconnected state in the automata
5612 *
5613 * Returns the new state or NULL in case of error
5614 */
5615xmlAutomataStatePtr
5616xmlAutomataNewState(xmlAutomataPtr am) {
5617 xmlAutomataStatePtr to;
5618
5619 if (am == NULL)
5620 return(NULL);
5621 to = xmlRegNewState(am);
5622 xmlRegStatePush(am, to);
5623 return(to);
5624}
5625
5626/**
Daniel Veillarda9b66d02002-12-11 14:23:49 +00005627 * xmlAutomataNewEpsilon:
Daniel Veillard4255d502002-04-16 15:50:10 +00005628 * @am: an automata
5629 * @from: the starting point of the transition
5630 * @to: the target point of the transition or NULL
5631 *
William M. Brackddf71d62004-05-06 04:17:26 +00005632 * If @to is NULL, this creates first a new target state in the automata
5633 * and then adds an epsilon transition from the @from state to the
Daniel Veillard4255d502002-04-16 15:50:10 +00005634 * target state
5635 *
5636 * Returns the target state or NULL in case of error
5637 */
5638xmlAutomataStatePtr
5639xmlAutomataNewEpsilon(xmlAutomataPtr am, xmlAutomataStatePtr from,
5640 xmlAutomataStatePtr to) {
5641 if ((am == NULL) || (from == NULL))
5642 return(NULL);
5643 xmlFAGenerateEpsilonTransition(am, from, to);
5644 if (to == NULL)
5645 return(am->state);
5646 return(to);
5647}
5648
Daniel Veillardb509f152002-04-17 16:28:10 +00005649/**
Daniel Veillard7646b182002-04-20 06:41:40 +00005650 * xmlAutomataNewAllTrans:
5651 * @am: an automata
5652 * @from: the starting point of the transition
5653 * @to: the target point of the transition or NULL
Daniel Veillarda9b66d02002-12-11 14:23:49 +00005654 * @lax: allow to transition if not all all transitions have been activated
Daniel Veillard7646b182002-04-20 06:41:40 +00005655 *
William M. Brackddf71d62004-05-06 04:17:26 +00005656 * If @to is NULL, this creates first a new target state in the automata
Daniel Veillard7646b182002-04-20 06:41:40 +00005657 * and then adds a an ALL transition from the @from state to the
5658 * target state. That transition is an epsilon transition allowed only when
5659 * all transitions from the @from node have been activated.
5660 *
5661 * Returns the target state or NULL in case of error
5662 */
5663xmlAutomataStatePtr
5664xmlAutomataNewAllTrans(xmlAutomataPtr am, xmlAutomataStatePtr from,
Daniel Veillard441bc322002-04-20 17:38:48 +00005665 xmlAutomataStatePtr to, int lax) {
Daniel Veillard7646b182002-04-20 06:41:40 +00005666 if ((am == NULL) || (from == NULL))
5667 return(NULL);
Daniel Veillard441bc322002-04-20 17:38:48 +00005668 xmlFAGenerateAllTransition(am, from, to, lax);
Daniel Veillard7646b182002-04-20 06:41:40 +00005669 if (to == NULL)
5670 return(am->state);
5671 return(to);
5672}
5673
5674/**
Daniel Veillardb509f152002-04-17 16:28:10 +00005675 * xmlAutomataNewCounter:
5676 * @am: an automata
5677 * @min: the minimal value on the counter
5678 * @max: the maximal value on the counter
5679 *
5680 * Create a new counter
5681 *
5682 * Returns the counter number or -1 in case of error
5683 */
5684int
5685xmlAutomataNewCounter(xmlAutomataPtr am, int min, int max) {
5686 int ret;
5687
5688 if (am == NULL)
5689 return(-1);
5690
5691 ret = xmlRegGetCounter(am);
5692 if (ret < 0)
5693 return(-1);
5694 am->counters[ret].min = min;
5695 am->counters[ret].max = max;
5696 return(ret);
5697}
5698
5699/**
5700 * xmlAutomataNewCountedTrans:
5701 * @am: an automata
5702 * @from: the starting point of the transition
5703 * @to: the target point of the transition or NULL
5704 * @counter: the counter associated to that transition
5705 *
William M. Brackddf71d62004-05-06 04:17:26 +00005706 * If @to is NULL, this creates first a new target state in the automata
Daniel Veillardb509f152002-04-17 16:28:10 +00005707 * and then adds an epsilon transition from the @from state to the target state
5708 * which will increment the counter provided
5709 *
5710 * Returns the target state or NULL in case of error
5711 */
5712xmlAutomataStatePtr
5713xmlAutomataNewCountedTrans(xmlAutomataPtr am, xmlAutomataStatePtr from,
5714 xmlAutomataStatePtr to, int counter) {
5715 if ((am == NULL) || (from == NULL) || (counter < 0))
5716 return(NULL);
5717 xmlFAGenerateCountedEpsilonTransition(am, from, to, counter);
5718 if (to == NULL)
5719 return(am->state);
5720 return(to);
5721}
5722
5723/**
5724 * xmlAutomataNewCounterTrans:
5725 * @am: an automata
5726 * @from: the starting point of the transition
5727 * @to: the target point of the transition or NULL
5728 * @counter: the counter associated to that transition
5729 *
William M. Brackddf71d62004-05-06 04:17:26 +00005730 * If @to is NULL, this creates first a new target state in the automata
Daniel Veillardb509f152002-04-17 16:28:10 +00005731 * and then adds an epsilon transition from the @from state to the target state
5732 * which will be allowed only if the counter is within the right range.
5733 *
5734 * Returns the target state or NULL in case of error
5735 */
5736xmlAutomataStatePtr
5737xmlAutomataNewCounterTrans(xmlAutomataPtr am, xmlAutomataStatePtr from,
5738 xmlAutomataStatePtr to, int counter) {
5739 if ((am == NULL) || (from == NULL) || (counter < 0))
5740 return(NULL);
5741 xmlFAGenerateCountedTransition(am, from, to, counter);
5742 if (to == NULL)
5743 return(am->state);
5744 return(to);
5745}
Daniel Veillard4255d502002-04-16 15:50:10 +00005746
5747/**
5748 * xmlAutomataCompile:
5749 * @am: an automata
5750 *
5751 * Compile the automata into a Reg Exp ready for being executed.
5752 * The automata should be free after this point.
5753 *
5754 * Returns the compiled regexp or NULL in case of error
5755 */
5756xmlRegexpPtr
5757xmlAutomataCompile(xmlAutomataPtr am) {
5758 xmlRegexpPtr ret;
5759
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00005760 if ((am == NULL) || (am->error != 0)) return(NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +00005761 xmlFAEliminateEpsilonTransitions(am);
Daniel Veillard23e73572002-09-19 19:56:43 +00005762 /* xmlFAComputesDeterminism(am); */
Daniel Veillard4255d502002-04-16 15:50:10 +00005763 ret = xmlRegEpxFromParse(am);
5764
5765 return(ret);
5766}
Daniel Veillarde19fc232002-04-22 16:01:24 +00005767
5768/**
5769 * xmlAutomataIsDeterminist:
5770 * @am: an automata
5771 *
5772 * Checks if an automata is determinist.
5773 *
5774 * Returns 1 if true, 0 if not, and -1 in case of error
5775 */
5776int
5777xmlAutomataIsDeterminist(xmlAutomataPtr am) {
5778 int ret;
5779
5780 if (am == NULL)
5781 return(-1);
5782
5783 ret = xmlFAComputesDeterminism(am);
5784 return(ret);
5785}
Daniel Veillard4255d502002-04-16 15:50:10 +00005786#endif /* LIBXML_AUTOMATA_ENABLED */
Daniel Veillard81a8ec62005-08-22 00:20:58 +00005787
5788#ifdef LIBXML_EXPR_ENABLED
5789/************************************************************************
5790 * *
5791 * Formal Expression handling code *
5792 * *
5793 ************************************************************************/
Daniel Veillard81a8ec62005-08-22 00:20:58 +00005794/************************************************************************
5795 * *
5796 * Expression handling context *
5797 * *
5798 ************************************************************************/
5799
5800struct _xmlExpCtxt {
5801 xmlDictPtr dict;
5802 xmlExpNodePtr *table;
5803 int size;
5804 int nbElems;
5805 int nb_nodes;
5806 const char *expr;
5807 const char *cur;
5808 int nb_cons;
Daniel Veillard81a8ec62005-08-22 00:20:58 +00005809 int tabSize;
5810};
5811
5812/**
5813 * xmlExpNewCtxt:
5814 * @maxNodes: the maximum number of nodes
5815 * @dict: optional dictionnary to use internally
5816 *
5817 * Creates a new context for manipulating expressions
5818 *
5819 * Returns the context or NULL in case of error
5820 */
5821xmlExpCtxtPtr
5822xmlExpNewCtxt(int maxNodes, xmlDictPtr dict) {
5823 xmlExpCtxtPtr ret;
5824 int size = 256;
5825
5826 if (maxNodes <= 4096)
5827 maxNodes = 4096;
5828
5829 ret = (xmlExpCtxtPtr) xmlMalloc(sizeof(xmlExpCtxt));
5830 if (ret == NULL)
5831 return(NULL);
5832 memset(ret, 0, sizeof(xmlExpCtxt));
5833 ret->size = size;
5834 ret->nbElems = 0;
5835 ret->table = xmlMalloc(size * sizeof(xmlExpNodePtr));
5836 if (ret->table == NULL) {
5837 xmlFree(ret);
5838 return(NULL);
5839 }
5840 memset(ret->table, 0, size * sizeof(xmlExpNodePtr));
5841 if (dict == NULL) {
5842 ret->dict = xmlDictCreate();
5843 if (ret->dict == NULL) {
5844 xmlFree(ret->table);
5845 xmlFree(ret);
5846 return(NULL);
5847 }
5848 } else {
5849 ret->dict = dict;
5850 xmlDictReference(ret->dict);
5851 }
5852 return(ret);
5853}
5854
5855/**
5856 * xmlExpFreeCtxt:
5857 * @ctxt: an expression context
5858 *
5859 * Free an expression context
5860 */
5861void
5862xmlExpFreeCtxt(xmlExpCtxtPtr ctxt) {
5863 if (ctxt == NULL)
5864 return;
5865 xmlDictFree(ctxt->dict);
5866 if (ctxt->table != NULL)
5867 xmlFree(ctxt->table);
5868 xmlFree(ctxt);
5869}
5870
5871/************************************************************************
5872 * *
5873 * Structure associated to an expression node *
5874 * *
5875 ************************************************************************/
Daniel Veillard465a0002005-08-22 12:07:04 +00005876#define MAX_NODES 10000
5877
5878/* #define DEBUG_DERIV */
5879
5880/*
5881 * TODO:
5882 * - Wildcards
5883 * - public API for creation
5884 *
5885 * Started
5886 * - regression testing
5887 *
5888 * Done
5889 * - split into module and test tool
5890 * - memleaks
5891 */
Daniel Veillard81a8ec62005-08-22 00:20:58 +00005892
5893typedef enum {
5894 XML_EXP_NILABLE = (1 << 0)
5895} xmlExpNodeInfo;
5896
5897#define IS_NILLABLE(node) ((node)->info & XML_EXP_NILABLE)
5898
5899struct _xmlExpNode {
5900 unsigned char type;/* xmlExpNodeType */
5901 unsigned char info;/* OR of xmlExpNodeInfo */
5902 unsigned short key; /* the hash key */
5903 unsigned int ref; /* The number of references */
5904 int c_max; /* the maximum length it can consume */
5905 xmlExpNodePtr exp_left;
5906 xmlExpNodePtr next;/* the next node in the hash table or free list */
5907 union {
5908 struct {
5909 int f_min;
5910 int f_max;
5911 } count;
5912 struct {
5913 xmlExpNodePtr f_right;
5914 } children;
5915 const xmlChar *f_str;
5916 } field;
5917};
5918
5919#define exp_min field.count.f_min
5920#define exp_max field.count.f_max
5921/* #define exp_left field.children.f_left */
5922#define exp_right field.children.f_right
5923#define exp_str field.f_str
5924
5925static xmlExpNodePtr xmlExpNewNode(xmlExpCtxtPtr ctxt, xmlExpNodeType type);
5926static xmlExpNode forbiddenExpNode = {
5927 XML_EXP_FORBID, 0, 0, 0, 0, NULL, NULL, {{ 0, 0}}
5928};
5929xmlExpNodePtr forbiddenExp = &forbiddenExpNode;
5930static xmlExpNode emptyExpNode = {
5931 XML_EXP_EMPTY, 1, 0, 0, 0, NULL, NULL, {{ 0, 0}}
5932};
5933xmlExpNodePtr emptyExp = &emptyExpNode;
5934
5935/************************************************************************
5936 * *
5937 * The custom hash table for unicity and canonicalization *
5938 * of sub-expressions pointers *
5939 * *
5940 ************************************************************************/
5941/*
5942 * xmlExpHashNameComputeKey:
5943 * Calculate the hash key for a token
5944 */
5945static unsigned short
5946xmlExpHashNameComputeKey(const xmlChar *name) {
5947 unsigned short value = 0L;
5948 char ch;
5949
5950 if (name != NULL) {
5951 value += 30 * (*name);
5952 while ((ch = *name++) != 0) {
5953 value = value ^ ((value << 5) + (value >> 3) + (unsigned long)ch);
5954 }
5955 }
5956 return (value);
5957}
5958
5959/*
5960 * xmlExpHashComputeKey:
5961 * Calculate the hash key for a compound expression
5962 */
5963static unsigned short
5964xmlExpHashComputeKey(xmlExpNodeType type, xmlExpNodePtr left,
5965 xmlExpNodePtr right) {
5966 unsigned long value;
5967 unsigned short ret;
5968
5969 switch (type) {
5970 case XML_EXP_SEQ:
5971 value = left->key;
5972 value += right->key;
5973 value *= 3;
5974 ret = (unsigned short) value;
5975 break;
5976 case XML_EXP_OR:
5977 value = left->key;
5978 value += right->key;
5979 value *= 7;
5980 ret = (unsigned short) value;
5981 break;
5982 case XML_EXP_COUNT:
5983 value = left->key;
5984 value += right->key;
5985 ret = (unsigned short) value;
5986 break;
5987 default:
5988 ret = 0;
5989 }
5990 return(ret);
5991}
5992
5993
5994static xmlExpNodePtr
5995xmlExpNewNode(xmlExpCtxtPtr ctxt, xmlExpNodeType type) {
5996 xmlExpNodePtr ret;
5997
5998 if (ctxt->nb_nodes >= MAX_NODES)
5999 return(NULL);
6000 ret = (xmlExpNodePtr) xmlMalloc(sizeof(xmlExpNode));
6001 if (ret == NULL)
6002 return(NULL);
6003 memset(ret, 0, sizeof(xmlExpNode));
6004 ret->type = type;
6005 ret->next = NULL;
6006 ctxt->nb_nodes++;
6007 ctxt->nb_cons++;
6008 return(ret);
6009}
6010
6011/**
6012 * xmlExpHashGetEntry:
6013 * @table: the hash table
6014 *
6015 * Get the unique entry from the hash table. The entry is created if
6016 * needed. @left and @right are consumed, i.e. their ref count will
6017 * be decremented by the operation.
6018 *
6019 * Returns the pointer or NULL in case of error
6020 */
6021static xmlExpNodePtr
6022xmlExpHashGetEntry(xmlExpCtxtPtr ctxt, xmlExpNodeType type,
6023 xmlExpNodePtr left, xmlExpNodePtr right,
6024 const xmlChar *name, int min, int max) {
6025 unsigned short kbase, key;
6026 xmlExpNodePtr entry;
6027 xmlExpNodePtr insert;
6028
6029 if (ctxt == NULL)
6030 return(NULL);
6031
6032 /*
6033 * Check for duplicate and insertion location.
6034 */
6035 if (type == XML_EXP_ATOM) {
6036 kbase = xmlExpHashNameComputeKey(name);
6037 } else if (type == XML_EXP_COUNT) {
6038 /* COUNT reduction rule 1 */
6039 /* a{1} -> a */
6040 if (min == max) {
6041 if (min == 1) {
6042 return(left);
6043 }
6044 if (min == 0) {
6045 xmlExpFree(ctxt, left);
6046 return(emptyExp);
6047 }
6048 }
6049 if (min < 0) {
6050 xmlExpFree(ctxt, left);
6051 return(forbiddenExp);
6052 }
6053 if (max == -1)
6054 kbase = min + 79;
6055 else
6056 kbase = max - min;
6057 kbase += left->key;
6058 } else if (type == XML_EXP_OR) {
6059 /* Forbid reduction rules */
6060 if (left->type == XML_EXP_FORBID) {
6061 xmlExpFree(ctxt, left);
6062 return(right);
6063 }
6064 if (right->type == XML_EXP_FORBID) {
6065 xmlExpFree(ctxt, right);
6066 return(left);
6067 }
6068
6069 /* OR reduction rule 1 */
6070 /* a | a reduced to a */
6071 if (left == right) {
6072 left->ref--;
6073 return(left);
6074 }
6075 /* OR canonicalization rule 1 */
6076 /* linearize (a | b) | c into a | (b | c) */
6077 if ((left->type == XML_EXP_OR) && (right->type != XML_EXP_OR)) {
6078 xmlExpNodePtr tmp = left;
6079 left = right;
6080 right = tmp;
6081 }
6082 /* OR reduction rule 2 */
6083 /* a | (a | b) and b | (a | b) are reduced to a | b */
6084 if (right->type == XML_EXP_OR) {
6085 if ((left == right->exp_left) ||
6086 (left == right->exp_right)) {
6087 xmlExpFree(ctxt, left);
6088 return(right);
6089 }
6090 }
6091 /* OR canonicalization rule 2 */
6092 /* linearize (a | b) | c into a | (b | c) */
6093 if (left->type == XML_EXP_OR) {
6094 xmlExpNodePtr tmp;
6095
6096 /* OR canonicalization rule 2 */
6097 if ((left->exp_right->type != XML_EXP_OR) &&
6098 (left->exp_right->key < left->exp_left->key)) {
6099 tmp = left->exp_right;
6100 left->exp_right = left->exp_left;
6101 left->exp_left = tmp;
6102 }
6103 left->exp_right->ref++;
6104 tmp = xmlExpHashGetEntry(ctxt, XML_EXP_OR, left->exp_right, right,
6105 NULL, 0, 0);
6106 left->exp_left->ref++;
6107 tmp = xmlExpHashGetEntry(ctxt, XML_EXP_OR, left->exp_left, tmp,
6108 NULL, 0, 0);
6109
6110 xmlExpFree(ctxt, left);
6111 return(tmp);
6112 }
6113 if (right->type == XML_EXP_OR) {
6114 /* Ordering in the tree */
6115 /* C | (A | B) -> A | (B | C) */
6116 if (left->key > right->exp_right->key) {
6117 xmlExpNodePtr tmp;
6118 right->exp_right->ref++;
6119 tmp = xmlExpHashGetEntry(ctxt, XML_EXP_OR, right->exp_right,
6120 left, NULL, 0, 0);
6121 right->exp_left->ref++;
6122 tmp = xmlExpHashGetEntry(ctxt, XML_EXP_OR, right->exp_left,
6123 tmp, NULL, 0, 0);
6124 xmlExpFree(ctxt, right);
6125 return(tmp);
6126 }
6127 /* Ordering in the tree */
6128 /* B | (A | C) -> A | (B | C) */
6129 if (left->key > right->exp_left->key) {
6130 xmlExpNodePtr tmp;
6131 right->exp_right->ref++;
6132 tmp = xmlExpHashGetEntry(ctxt, XML_EXP_OR, left,
6133 right->exp_right, NULL, 0, 0);
6134 right->exp_left->ref++;
6135 tmp = xmlExpHashGetEntry(ctxt, XML_EXP_OR, right->exp_left,
6136 tmp, NULL, 0, 0);
6137 xmlExpFree(ctxt, right);
6138 return(tmp);
6139 }
6140 }
6141 /* we know both types are != XML_EXP_OR here */
6142 else if (left->key > right->key) {
6143 xmlExpNodePtr tmp = left;
6144 left = right;
6145 right = tmp;
6146 }
6147 kbase = xmlExpHashComputeKey(type, left, right);
6148 } else if (type == XML_EXP_SEQ) {
6149 /* Forbid reduction rules */
6150 if (left->type == XML_EXP_FORBID) {
6151 xmlExpFree(ctxt, right);
6152 return(left);
6153 }
6154 if (right->type == XML_EXP_FORBID) {
6155 xmlExpFree(ctxt, left);
6156 return(right);
6157 }
6158 /* Empty reduction rules */
6159 if (right->type == XML_EXP_EMPTY) {
6160 return(left);
6161 }
6162 if (left->type == XML_EXP_EMPTY) {
6163 return(right);
6164 }
6165 kbase = xmlExpHashComputeKey(type, left, right);
6166 } else
6167 return(NULL);
6168
6169 key = kbase % ctxt->size;
6170 if (ctxt->table[key] != NULL) {
6171 for (insert = ctxt->table[key]; insert != NULL;
6172 insert = insert->next) {
6173 if ((insert->key == kbase) &&
6174 (insert->type == type)) {
6175 if (type == XML_EXP_ATOM) {
6176 if (name == insert->exp_str) {
6177 insert->ref++;
6178 return(insert);
6179 }
6180 } else if (type == XML_EXP_COUNT) {
6181 if ((insert->exp_min == min) && (insert->exp_max == max) &&
6182 (insert->exp_left == left)) {
6183 insert->ref++;
6184 left->ref--;
6185 return(insert);
6186 }
6187 } else if ((insert->exp_left == left) &&
6188 (insert->exp_right == right)) {
6189 insert->ref++;
6190 left->ref--;
6191 right->ref--;
6192 return(insert);
6193 }
6194 }
6195 }
6196 }
6197
6198 entry = xmlExpNewNode(ctxt, type);
6199 if (entry == NULL)
6200 return(NULL);
6201 entry->key = kbase;
6202 if (type == XML_EXP_ATOM) {
6203 entry->exp_str = name;
6204 entry->c_max = 1;
6205 } else if (type == XML_EXP_COUNT) {
6206 entry->exp_min = min;
6207 entry->exp_max = max;
6208 entry->exp_left = left;
6209 if ((min == 0) || (IS_NILLABLE(left)))
6210 entry->info |= XML_EXP_NILABLE;
6211 if (max < 0)
6212 entry->c_max = -1;
6213 else
6214 entry->c_max = max * entry->exp_left->c_max;
6215 } else {
6216 entry->exp_left = left;
6217 entry->exp_right = right;
6218 if (type == XML_EXP_OR) {
6219 if ((IS_NILLABLE(left)) || (IS_NILLABLE(right)))
6220 entry->info |= XML_EXP_NILABLE;
6221 if ((entry->exp_left->c_max == -1) ||
6222 (entry->exp_right->c_max == -1))
6223 entry->c_max = -1;
6224 else if (entry->exp_left->c_max > entry->exp_right->c_max)
6225 entry->c_max = entry->exp_left->c_max;
6226 else
6227 entry->c_max = entry->exp_right->c_max;
6228 } else {
6229 if ((IS_NILLABLE(left)) && (IS_NILLABLE(right)))
6230 entry->info |= XML_EXP_NILABLE;
6231 if ((entry->exp_left->c_max == -1) ||
6232 (entry->exp_right->c_max == -1))
6233 entry->c_max = -1;
6234 else
6235 entry->c_max = entry->exp_left->c_max + entry->exp_right->c_max;
6236 }
6237 }
6238 entry->ref = 1;
6239 if (ctxt->table[key] != NULL)
6240 entry->next = ctxt->table[key];
6241
6242 ctxt->table[key] = entry;
6243 ctxt->nbElems++;
6244
6245 return(entry);
6246}
6247
6248/**
6249 * xmlExpFree:
6250 * @ctxt: the expression context
6251 * @exp: the expression
6252 *
6253 * Dereference the expression
6254 */
6255void
6256xmlExpFree(xmlExpCtxtPtr ctxt, xmlExpNodePtr exp) {
6257 if ((exp == NULL) || (exp == forbiddenExp) || (exp == emptyExp))
6258 return;
6259 exp->ref--;
Daniel Veillard81a8ec62005-08-22 00:20:58 +00006260 if (exp->ref == 0) {
6261 unsigned short key;
6262
6263 /* Unlink it first from the hash table */
6264 key = exp->key % ctxt->size;
6265 if (ctxt->table[key] == exp) {
6266 ctxt->table[key] = exp->next;
6267 } else {
6268 xmlExpNodePtr tmp;
6269
6270 tmp = ctxt->table[key];
6271 while (tmp != NULL) {
6272 if (tmp->next == exp) {
6273 tmp->next = exp->next;
6274 break;
6275 }
6276 tmp = tmp->next;
6277 }
6278 }
6279
6280 if ((exp->type == XML_EXP_SEQ) || (exp->type == XML_EXP_OR)) {
6281 xmlExpFree(ctxt, exp->exp_left);
6282 xmlExpFree(ctxt, exp->exp_right);
6283 } else if (exp->type == XML_EXP_COUNT) {
6284 xmlExpFree(ctxt, exp->exp_left);
6285 }
6286 xmlFree(exp);
Daniel Veillard81a8ec62005-08-22 00:20:58 +00006287 ctxt->nb_nodes--;
6288 }
6289}
6290
6291/**
6292 * xmlExpRef:
6293 * @exp: the expression
6294 *
6295 * Increase the reference count of the expression
6296 */
6297void
6298xmlExpRef(xmlExpNodePtr exp) {
6299 if (exp != NULL)
6300 exp->ref++;
6301}
6302
Daniel Veillardccb4d412005-08-23 13:41:17 +00006303/**
6304 * xmlExpNewAtom:
6305 * @ctxt: the expression context
6306 * @name: the atom name
6307 * @len: the atom name lenght in byte (or -1);
6308 *
6309 * Get the atom associated to this name from that context
6310 *
6311 * Returns the node or NULL in case of error
6312 */
6313xmlExpNodePtr
6314xmlExpNewAtom(xmlExpCtxtPtr ctxt, const xmlChar *name, int len) {
6315 if ((ctxt == NULL) || (name == NULL))
6316 return(NULL);
6317 name = xmlDictLookup(ctxt->dict, name, len);
6318 if (name == NULL)
6319 return(NULL);
6320 return(xmlExpHashGetEntry(ctxt, XML_EXP_ATOM, NULL, NULL, name, 0, 0));
6321}
6322
6323/**
6324 * xmlExpNewOr:
6325 * @ctxt: the expression context
6326 * @left: left expression
6327 * @right: right expression
6328 *
6329 * Get the atom associated to the choice @left | @right
6330 * Note that @left and @right are consumed in the operation, to keep
6331 * an handle on them use xmlExpRef() and use xmlExpFree() to release them,
6332 * this is true even in case of failure (unless ctxt == NULL).
6333 *
6334 * Returns the node or NULL in case of error
6335 */
6336xmlExpNodePtr
6337xmlExpNewOr(xmlExpCtxtPtr ctxt, xmlExpNodePtr left, xmlExpNodePtr right) {
6338 if ((ctxt == NULL) || (left == NULL) || (right == NULL)) {
6339 xmlExpFree(ctxt, left);
6340 xmlExpFree(ctxt, right);
6341 return(NULL);
6342 }
6343 return(xmlExpHashGetEntry(ctxt, XML_EXP_OR, left, right, NULL, 0, 0));
6344}
6345
6346/**
6347 * xmlExpNewSeq:
6348 * @ctxt: the expression context
6349 * @left: left expression
6350 * @right: right expression
6351 *
6352 * Get the atom associated to the sequence @left , @right
6353 * Note that @left and @right are consumed in the operation, to keep
6354 * an handle on them use xmlExpRef() and use xmlExpFree() to release them,
6355 * this is true even in case of failure (unless ctxt == NULL).
6356 *
6357 * Returns the node or NULL in case of error
6358 */
6359xmlExpNodePtr
6360xmlExpNewSeq(xmlExpCtxtPtr ctxt, xmlExpNodePtr left, xmlExpNodePtr right) {
6361 if ((ctxt == NULL) || (left == NULL) || (right == NULL)) {
6362 xmlExpFree(ctxt, left);
6363 xmlExpFree(ctxt, right);
6364 return(NULL);
6365 }
6366 return(xmlExpHashGetEntry(ctxt, XML_EXP_SEQ, left, right, NULL, 0, 0));
6367}
6368
6369/**
6370 * xmlExpNewRange:
6371 * @ctxt: the expression context
6372 * @subset: the expression to be repeated
6373 * @min: the lower bound for the repetition
6374 * @max: the upper bound for the repetition, -1 means infinite
6375 *
6376 * Get the atom associated to the range (@subset){@min, @max}
6377 * Note that @subset is consumed in the operation, to keep
6378 * an handle on it use xmlExpRef() and use xmlExpFree() to release it,
6379 * this is true even in case of failure (unless ctxt == NULL).
6380 *
6381 * Returns the node or NULL in case of error
6382 */
6383xmlExpNodePtr
6384xmlExpNewRange(xmlExpCtxtPtr ctxt, xmlExpNodePtr subset, int min, int max) {
6385 if ((ctxt == NULL) || (subset == NULL) || (min < 0) || (max < -1) ||
6386 ((max >= 0) && (min > max))) {
6387 xmlExpFree(ctxt, subset);
6388 return(NULL);
6389 }
6390 return(xmlExpHashGetEntry(ctxt, XML_EXP_COUNT, subset,
6391 NULL, NULL, min, max));
6392}
6393
Daniel Veillard81a8ec62005-08-22 00:20:58 +00006394/************************************************************************
6395 * *
6396 * Public API for operations on expressions *
6397 * *
6398 ************************************************************************/
6399
6400static int
6401xmlExpGetLanguageInt(xmlExpCtxtPtr ctxt, xmlExpNodePtr exp,
6402 const xmlChar**list, int len, int nb) {
6403 int tmp, tmp2;
6404tail:
6405 switch (exp->type) {
6406 case XML_EXP_EMPTY:
6407 return(0);
6408 case XML_EXP_ATOM:
6409 for (tmp = 0;tmp < nb;tmp++)
6410 if (list[tmp] == exp->exp_str)
6411 return(0);
6412 if (nb >= len)
6413 return(-2);
6414 list[nb++] = exp->exp_str;
6415 return(1);
6416 case XML_EXP_COUNT:
6417 exp = exp->exp_left;
6418 goto tail;
6419 case XML_EXP_SEQ:
6420 case XML_EXP_OR:
6421 tmp = xmlExpGetLanguageInt(ctxt, exp->exp_left, list, len, nb);
6422 if (tmp < 0)
6423 return(tmp);
6424 tmp2 = xmlExpGetLanguageInt(ctxt, exp->exp_right, list, len,
6425 nb + tmp);
6426 if (tmp2 < 0)
6427 return(tmp2);
6428 return(tmp + tmp2);
6429 }
6430 return(-1);
6431}
6432
6433/**
6434 * xmlExpGetLanguage:
6435 * @ctxt: the expression context
6436 * @exp: the expression
6437 * @list: where to store the tokens
6438 * @len: the allocated lenght of @list
6439 *
6440 * Find all the strings used in @exp and store them in @list
6441 *
6442 * Returns the number of unique strings found, -1 in case of errors and
6443 * -2 if there is more than @len strings
6444 */
6445int
6446xmlExpGetLanguage(xmlExpCtxtPtr ctxt, xmlExpNodePtr exp,
6447 const xmlChar**list, int len) {
6448 if ((ctxt == NULL) || (exp == NULL) || (list == NULL) || (len <= 0))
6449 return(-1);
6450 return(xmlExpGetLanguageInt(ctxt, exp, list, len, 0));
6451}
6452
6453static int
6454xmlExpGetStartInt(xmlExpCtxtPtr ctxt, xmlExpNodePtr exp,
6455 const xmlChar**list, int len, int nb) {
6456 int tmp, tmp2;
6457tail:
6458 switch (exp->type) {
6459 case XML_EXP_FORBID:
6460 return(0);
6461 case XML_EXP_EMPTY:
6462 return(0);
6463 case XML_EXP_ATOM:
6464 for (tmp = 0;tmp < nb;tmp++)
6465 if (list[tmp] == exp->exp_str)
6466 return(0);
6467 if (nb >= len)
6468 return(-2);
6469 list[nb++] = exp->exp_str;
6470 return(1);
6471 case XML_EXP_COUNT:
6472 exp = exp->exp_left;
6473 goto tail;
6474 case XML_EXP_SEQ:
6475 tmp = xmlExpGetStartInt(ctxt, exp->exp_left, list, len, nb);
6476 if (tmp < 0)
6477 return(tmp);
6478 if (IS_NILLABLE(exp->exp_left)) {
6479 tmp2 = xmlExpGetStartInt(ctxt, exp->exp_right, list, len,
6480 nb + tmp);
6481 if (tmp2 < 0)
6482 return(tmp2);
6483 tmp += tmp2;
6484 }
6485 return(tmp);
6486 case XML_EXP_OR:
6487 tmp = xmlExpGetStartInt(ctxt, exp->exp_left, list, len, nb);
6488 if (tmp < 0)
6489 return(tmp);
6490 tmp2 = xmlExpGetStartInt(ctxt, exp->exp_right, list, len,
6491 nb + tmp);
6492 if (tmp2 < 0)
6493 return(tmp2);
6494 return(tmp + tmp2);
6495 }
6496 return(-1);
6497}
6498
6499/**
6500 * xmlExpGetStart:
6501 * @ctxt: the expression context
6502 * @exp: the expression
6503 * @list: where to store the tokens
6504 * @len: the allocated lenght of @list
6505 *
6506 * Find all the strings that appears at the start of the languages
6507 * accepted by @exp and store them in @list. E.g. for (a, b) | c
6508 * it will return the list [a, c]
6509 *
6510 * Returns the number of unique strings found, -1 in case of errors and
6511 * -2 if there is more than @len strings
6512 */
6513int
6514xmlExpGetStart(xmlExpCtxtPtr ctxt, xmlExpNodePtr exp,
6515 const xmlChar**list, int len) {
6516 if ((ctxt == NULL) || (exp == NULL) || (list == NULL) || (len <= 0))
6517 return(-1);
6518 return(xmlExpGetStartInt(ctxt, exp, list, len, 0));
6519}
6520
6521/**
6522 * xmlExpIsNillable:
6523 * @exp: the expression
6524 *
6525 * Finds if the expression is nillable, i.e. if it accepts the empty sequqnce
6526 *
6527 * Returns 1 if nillable, 0 if not and -1 in case of error
6528 */
6529int
6530xmlExpIsNillable(xmlExpNodePtr exp) {
6531 if (exp == NULL)
6532 return(-1);
6533 return(IS_NILLABLE(exp) != 0);
6534}
6535
6536static xmlExpNodePtr
6537xmlExpStringDeriveInt(xmlExpCtxtPtr ctxt, xmlExpNodePtr exp, const xmlChar *str)
6538{
6539 xmlExpNodePtr ret;
6540
6541 switch (exp->type) {
6542 case XML_EXP_EMPTY:
6543 return(forbiddenExp);
6544 case XML_EXP_FORBID:
6545 return(forbiddenExp);
6546 case XML_EXP_ATOM:
6547 if (exp->exp_str == str) {
6548#ifdef DEBUG_DERIV
6549 printf("deriv atom: equal => Empty\n");
6550#endif
6551 ret = emptyExp;
6552 } else {
6553#ifdef DEBUG_DERIV
6554 printf("deriv atom: mismatch => forbid\n");
6555#endif
6556 /* TODO wildcards here */
6557 ret = forbiddenExp;
6558 }
6559 return(ret);
6560 case XML_EXP_OR: {
6561 xmlExpNodePtr tmp;
6562
6563#ifdef DEBUG_DERIV
6564 printf("deriv or: => or(derivs)\n");
6565#endif
6566 tmp = xmlExpStringDeriveInt(ctxt, exp->exp_left, str);
6567 if (tmp == NULL) {
6568 return(NULL);
6569 }
6570 ret = xmlExpStringDeriveInt(ctxt, exp->exp_right, str);
6571 if (ret == NULL) {
6572 xmlExpFree(ctxt, tmp);
6573 return(NULL);
6574 }
6575 ret = xmlExpHashGetEntry(ctxt, XML_EXP_OR, tmp, ret,
6576 NULL, 0, 0);
6577 return(ret);
6578 }
6579 case XML_EXP_SEQ:
6580#ifdef DEBUG_DERIV
6581 printf("deriv seq: starting with left\n");
6582#endif
6583 ret = xmlExpStringDeriveInt(ctxt, exp->exp_left, str);
6584 if (ret == NULL) {
6585 return(NULL);
6586 } else if (ret == forbiddenExp) {
6587 if (IS_NILLABLE(exp->exp_left)) {
6588#ifdef DEBUG_DERIV
6589 printf("deriv seq: left failed but nillable\n");
6590#endif
6591 ret = xmlExpStringDeriveInt(ctxt, exp->exp_right, str);
6592 }
6593 } else {
6594#ifdef DEBUG_DERIV
6595 printf("deriv seq: left match => sequence\n");
6596#endif
6597 exp->exp_right->ref++;
6598 ret = xmlExpHashGetEntry(ctxt, XML_EXP_SEQ, ret, exp->exp_right,
6599 NULL, 0, 0);
6600 }
6601 return(ret);
6602 case XML_EXP_COUNT: {
6603 int min, max;
6604 xmlExpNodePtr tmp;
6605
6606 if (exp->exp_max == 0)
6607 return(forbiddenExp);
6608 ret = xmlExpStringDeriveInt(ctxt, exp->exp_left, str);
6609 if (ret == NULL)
6610 return(NULL);
6611 if (ret == forbiddenExp) {
6612#ifdef DEBUG_DERIV
6613 printf("deriv count: pattern mismatch => forbid\n");
6614#endif
6615 return(ret);
6616 }
6617 if (exp->exp_max == 1)
6618 return(ret);
6619 if (exp->exp_max < 0) /* unbounded */
6620 max = -1;
6621 else
6622 max = exp->exp_max - 1;
6623 if (exp->exp_min > 0)
6624 min = exp->exp_min - 1;
6625 else
6626 min = 0;
6627 exp->exp_left->ref++;
6628 tmp = xmlExpHashGetEntry(ctxt, XML_EXP_COUNT, exp->exp_left, NULL,
6629 NULL, min, max);
6630 if (ret == emptyExp) {
6631#ifdef DEBUG_DERIV
6632 printf("deriv count: match to empty => new count\n");
6633#endif
6634 return(tmp);
6635 }
6636#ifdef DEBUG_DERIV
6637 printf("deriv count: match => sequence with new count\n");
6638#endif
6639 return(xmlExpHashGetEntry(ctxt, XML_EXP_SEQ, ret, tmp,
6640 NULL, 0, 0));
6641 }
6642 }
6643 return(NULL);
6644}
6645
6646/**
6647 * xmlExpStringDerive:
6648 * @ctxt: the expression context
6649 * @exp: the expression
6650 * @str: the string
6651 * @len: the string len in bytes if available
6652 *
6653 * Do one step of Brzozowski derivation of the expression @exp with
6654 * respect to the input string
6655 *
6656 * Returns the resulting expression or NULL in case of internal error
6657 */
6658xmlExpNodePtr
6659xmlExpStringDerive(xmlExpCtxtPtr ctxt, xmlExpNodePtr exp,
6660 const xmlChar *str, int len) {
6661 const xmlChar *input;
6662
6663 if ((exp == NULL) || (ctxt == NULL) || (str == NULL)) {
6664 return(NULL);
6665 }
6666 /*
6667 * check the string is in the dictionnary, if yes use an interned
6668 * copy, otherwise we know it's not an acceptable input
6669 */
6670 input = xmlDictExists(ctxt->dict, str, len);
6671 if (input == NULL) {
6672 return(forbiddenExp);
6673 }
6674 return(xmlExpStringDeriveInt(ctxt, exp, input));
6675}
6676
6677static int
6678xmlExpCheckCard(xmlExpNodePtr exp, xmlExpNodePtr sub) {
6679 int ret = 1;
6680
6681 if (sub->c_max == -1) {
6682 if (exp->c_max != -1)
6683 ret = 0;
6684 } else if ((exp->c_max >= 0) && (exp->c_max < sub->c_max)) {
6685 ret = 0;
6686 }
6687#if 0
6688 if ((IS_NILLABLE(sub)) && (!IS_NILLABLE(exp)))
6689 ret = 0;
6690#endif
6691 return(ret);
6692}
6693
6694static xmlExpNodePtr xmlExpExpDeriveInt(xmlExpCtxtPtr ctxt, xmlExpNodePtr exp,
6695 xmlExpNodePtr sub);
6696/**
6697 * xmlExpDivide:
6698 * @ctxt: the expressions context
6699 * @exp: the englobing expression
6700 * @sub: the subexpression
6701 * @mult: the multiple expression
6702 * @remain: the remain from the derivation of the multiple
6703 *
6704 * Check if exp is a multiple of sub, i.e. if there is a finite number n
6705 * so that sub{n} subsume exp
6706 *
6707 * Returns the multiple value if successful, 0 if it is not a multiple
6708 * and -1 in case of internel error.
6709 */
6710
6711static int
6712xmlExpDivide(xmlExpCtxtPtr ctxt, xmlExpNodePtr exp, xmlExpNodePtr sub,
6713 xmlExpNodePtr *mult, xmlExpNodePtr *remain) {
6714 int i;
6715 xmlExpNodePtr tmp, tmp2;
6716
6717 if (mult != NULL) *mult = NULL;
6718 if (remain != NULL) *remain = NULL;
6719 if (exp->c_max == -1) return(0);
6720 if (IS_NILLABLE(exp) && (!IS_NILLABLE(sub))) return(0);
6721
6722 for (i = 1;i <= exp->c_max;i++) {
6723 sub->ref++;
6724 tmp = xmlExpHashGetEntry(ctxt, XML_EXP_COUNT,
6725 sub, NULL, NULL, i, i);
6726 if (tmp == NULL) {
6727 return(-1);
6728 }
6729 if (!xmlExpCheckCard(tmp, exp)) {
6730 xmlExpFree(ctxt, tmp);
6731 continue;
6732 }
6733 tmp2 = xmlExpExpDeriveInt(ctxt, tmp, exp);
6734 if (tmp2 == NULL) {
6735 xmlExpFree(ctxt, tmp);
6736 return(-1);
6737 }
6738 if ((tmp2 != forbiddenExp) && (IS_NILLABLE(tmp2))) {
6739 if (remain != NULL)
6740 *remain = tmp2;
6741 else
6742 xmlExpFree(ctxt, tmp2);
6743 if (mult != NULL)
6744 *mult = tmp;
6745 else
6746 xmlExpFree(ctxt, tmp);
6747#ifdef DEBUG_DERIV
6748 printf("Divide succeeded %d\n", i);
6749#endif
6750 return(i);
6751 }
6752 xmlExpFree(ctxt, tmp);
6753 xmlExpFree(ctxt, tmp2);
6754 }
6755#ifdef DEBUG_DERIV
6756 printf("Divide failed\n");
6757#endif
6758 return(0);
6759}
6760
6761/**
6762 * xmlExpExpDeriveInt:
6763 * @ctxt: the expressions context
6764 * @exp: the englobing expression
6765 * @sub: the subexpression
6766 *
6767 * Try to do a step of Brzozowski derivation but at a higher level
6768 * the input being a subexpression.
6769 *
6770 * Returns the resulting expression or NULL in case of internal error
6771 */
6772static xmlExpNodePtr
6773xmlExpExpDeriveInt(xmlExpCtxtPtr ctxt, xmlExpNodePtr exp, xmlExpNodePtr sub) {
6774 xmlExpNodePtr ret, tmp, tmp2, tmp3;
6775 const xmlChar **tab;
6776 int len, i;
6777
6778 /*
6779 * In case of equality and if the expression can only consume a finite
6780 * amount, then the derivation is empty
6781 */
6782 if ((exp == sub) && (exp->c_max >= 0)) {
6783#ifdef DEBUG_DERIV
6784 printf("Equal(exp, sub) and finite -> Empty\n");
6785#endif
6786 return(emptyExp);
6787 }
6788 /*
6789 * decompose sub sequence first
6790 */
6791 if (sub->type == XML_EXP_EMPTY) {
6792#ifdef DEBUG_DERIV
6793 printf("Empty(sub) -> Empty\n");
6794#endif
6795 exp->ref++;
6796 return(exp);
6797 }
6798 if (sub->type == XML_EXP_SEQ) {
6799#ifdef DEBUG_DERIV
6800 printf("Seq(sub) -> decompose\n");
6801#endif
6802 tmp = xmlExpExpDeriveInt(ctxt, exp, sub->exp_left);
6803 if (tmp == NULL)
6804 return(NULL);
6805 if (tmp == forbiddenExp)
6806 return(tmp);
6807 ret = xmlExpExpDeriveInt(ctxt, tmp, sub->exp_right);
6808 xmlExpFree(ctxt, tmp);
6809 return(ret);
6810 }
6811 if (sub->type == XML_EXP_OR) {
6812#ifdef DEBUG_DERIV
6813 printf("Or(sub) -> decompose\n");
6814#endif
6815 tmp = xmlExpExpDeriveInt(ctxt, exp, sub->exp_left);
6816 if (tmp == forbiddenExp)
6817 return(tmp);
6818 if (tmp == NULL)
6819 return(NULL);
6820 ret = xmlExpExpDeriveInt(ctxt, exp, sub->exp_right);
6821 if ((ret == NULL) || (ret == forbiddenExp)) {
6822 xmlExpFree(ctxt, tmp);
6823 return(ret);
6824 }
6825 return(xmlExpHashGetEntry(ctxt, XML_EXP_OR, tmp, ret, NULL, 0, 0));
6826 }
6827 if (!xmlExpCheckCard(exp, sub)) {
6828#ifdef DEBUG_DERIV
6829 printf("CheckCard(exp, sub) failed -> Forbid\n");
6830#endif
6831 return(forbiddenExp);
6832 }
6833 switch (exp->type) {
6834 case XML_EXP_EMPTY:
6835 if (sub == emptyExp)
6836 return(emptyExp);
6837#ifdef DEBUG_DERIV
6838 printf("Empty(exp) -> Forbid\n");
6839#endif
6840 return(forbiddenExp);
6841 case XML_EXP_FORBID:
6842#ifdef DEBUG_DERIV
6843 printf("Forbid(exp) -> Forbid\n");
6844#endif
6845 return(forbiddenExp);
6846 case XML_EXP_ATOM:
6847 if (sub->type == XML_EXP_ATOM) {
6848 /* TODO: handle wildcards */
6849 if (exp->exp_str == sub->exp_str) {
6850#ifdef DEBUG_DERIV
6851 printf("Atom match -> Empty\n");
6852#endif
6853 return(emptyExp);
6854 }
6855#ifdef DEBUG_DERIV
6856 printf("Atom mismatch -> Forbid\n");
6857#endif
6858 return(forbiddenExp);
6859 }
6860 if ((sub->type == XML_EXP_COUNT) &&
6861 (sub->exp_max == 1) &&
6862 (sub->exp_left->type == XML_EXP_ATOM)) {
6863 /* TODO: handle wildcards */
6864 if (exp->exp_str == sub->exp_left->exp_str) {
6865#ifdef DEBUG_DERIV
6866 printf("Atom match -> Empty\n");
6867#endif
6868 return(emptyExp);
6869 }
6870#ifdef DEBUG_DERIV
6871 printf("Atom mismatch -> Forbid\n");
6872#endif
6873 return(forbiddenExp);
6874 }
6875#ifdef DEBUG_DERIV
6876 printf("Compex exp vs Atom -> Forbid\n");
6877#endif
6878 return(forbiddenExp);
6879 case XML_EXP_SEQ:
6880 /* try to get the sequence consumed only if possible */
6881 if (xmlExpCheckCard(exp->exp_left, sub)) {
6882 /* See if the sequence can be consumed directly */
6883#ifdef DEBUG_DERIV
6884 printf("Seq trying left only\n");
6885#endif
6886 ret = xmlExpExpDeriveInt(ctxt, exp->exp_left, sub);
6887 if ((ret != forbiddenExp) && (ret != NULL)) {
6888#ifdef DEBUG_DERIV
6889 printf("Seq trying left only worked\n");
6890#endif
6891 /*
6892 * TODO: assumption here that we are determinist
6893 * i.e. we won't get to a nillable exp left
6894 * subset which could be matched by the right
6895 * part too.
6896 * e.g.: (a | b)+,(a | c) and 'a+,a'
6897 */
6898 exp->exp_right->ref++;
6899 return(xmlExpHashGetEntry(ctxt, XML_EXP_SEQ, ret,
6900 exp->exp_right, NULL, 0, 0));
6901 }
6902#ifdef DEBUG_DERIV
6903 } else {
6904 printf("Seq: left too short\n");
6905#endif
6906 }
6907 /* Try instead to decompose */
6908 if (sub->type == XML_EXP_COUNT) {
6909 int min, max;
6910
6911#ifdef DEBUG_DERIV
6912 printf("Seq: sub is a count\n");
6913#endif
6914 ret = xmlExpExpDeriveInt(ctxt, exp->exp_left, sub->exp_left);
6915 if (ret == NULL)
6916 return(NULL);
6917 if (ret != forbiddenExp) {
6918#ifdef DEBUG_DERIV
6919 printf("Seq , Count match on left\n");
6920#endif
6921 if (sub->exp_max < 0)
6922 max = -1;
6923 else
6924 max = sub->exp_max -1;
6925 if (sub->exp_min > 0)
6926 min = sub->exp_min -1;
6927 else
6928 min = 0;
6929 exp->exp_right->ref++;
6930 tmp = xmlExpHashGetEntry(ctxt, XML_EXP_SEQ, ret,
6931 exp->exp_right, NULL, 0, 0);
6932 if (tmp == NULL)
6933 return(NULL);
6934
6935 sub->exp_left->ref++;
6936 tmp2 = xmlExpHashGetEntry(ctxt, XML_EXP_COUNT,
6937 sub->exp_left, NULL, NULL, min, max);
6938 if (tmp2 == NULL) {
6939 xmlExpFree(ctxt, tmp);
6940 return(NULL);
6941 }
6942 ret = xmlExpExpDeriveInt(ctxt, tmp, tmp2);
6943 xmlExpFree(ctxt, tmp);
6944 xmlExpFree(ctxt, tmp2);
6945 return(ret);
6946 }
6947 }
6948 /* we made no progress on structured operations */
6949 break;
6950 case XML_EXP_OR:
6951#ifdef DEBUG_DERIV
6952 printf("Or , trying both side\n");
6953#endif
6954 ret = xmlExpExpDeriveInt(ctxt, exp->exp_left, sub);
6955 if (ret == NULL)
6956 return(NULL);
6957 tmp = xmlExpExpDeriveInt(ctxt, exp->exp_right, sub);
6958 if (tmp == NULL) {
6959 xmlExpFree(ctxt, ret);
6960 return(NULL);
6961 }
6962 return(xmlExpHashGetEntry(ctxt, XML_EXP_OR, ret, tmp, NULL, 0, 0));
6963 case XML_EXP_COUNT: {
6964 int min, max;
6965
6966 if (sub->type == XML_EXP_COUNT) {
6967 /*
6968 * Try to see if the loop is completely subsumed
6969 */
6970 tmp = xmlExpExpDeriveInt(ctxt, exp->exp_left, sub->exp_left);
6971 if (tmp == NULL)
6972 return(NULL);
6973 if (tmp == forbiddenExp) {
6974 int mult;
6975
6976#ifdef DEBUG_DERIV
6977 printf("Count, Count inner don't subsume\n");
6978#endif
6979 mult = xmlExpDivide(ctxt, sub->exp_left, exp->exp_left,
6980 NULL, &tmp);
6981 if (mult <= 0) {
6982#ifdef DEBUG_DERIV
6983 printf("Count, Count not multiple => forbidden\n");
6984#endif
6985 return(forbiddenExp);
6986 }
6987 if (sub->exp_max == -1) {
6988 max = -1;
6989 if (exp->exp_max == -1) {
6990 if (exp->exp_min <= sub->exp_min * mult)
6991 min = 0;
6992 else
6993 min = exp->exp_min - sub->exp_min * mult;
6994 } else {
6995#ifdef DEBUG_DERIV
6996 printf("Count, Count finite can't subsume infinite\n");
6997#endif
6998 xmlExpFree(ctxt, tmp);
6999 return(forbiddenExp);
7000 }
7001 } else {
7002 if (exp->exp_max == -1) {
7003#ifdef DEBUG_DERIV
7004 printf("Infinite loop consume mult finite loop\n");
7005#endif
7006 if (exp->exp_min > sub->exp_min * mult) {
7007 max = -1;
7008 min = exp->exp_min - sub->exp_min * mult;
7009 } else {
7010 max = -1;
7011 min = 0;
7012 }
7013 } else {
7014 if (exp->exp_max < sub->exp_max * mult) {
7015#ifdef DEBUG_DERIV
7016 printf("loops max mult mismatch => forbidden\n");
7017#endif
7018 xmlExpFree(ctxt, tmp);
7019 return(forbiddenExp);
7020 }
7021 if (sub->exp_max * mult > exp->exp_min)
7022 min = 0;
7023 else
7024 min = exp->exp_min - sub->exp_max * mult;
7025 max = exp->exp_max - sub->exp_max * mult;
7026 }
7027 }
7028 } else if (!IS_NILLABLE(tmp)) {
7029 /*
7030 * TODO: loop here to try to grow if working on finite
7031 * blocks.
7032 */
7033#ifdef DEBUG_DERIV
7034 printf("Count, Count remain not nillable => forbidden\n");
7035#endif
7036 xmlExpFree(ctxt, tmp);
7037 return(forbiddenExp);
7038 } else if (sub->exp_max == -1) {
7039 if (exp->exp_max == -1) {
7040 if (exp->exp_min <= sub->exp_min) {
7041#ifdef DEBUG_DERIV
7042 printf("Infinite loops Okay => COUNT(0,Inf)\n");
7043#endif
7044 max = -1;
7045 min = 0;
7046 } else {
7047#ifdef DEBUG_DERIV
7048 printf("Infinite loops min => Count(X,Inf)\n");
7049#endif
7050 max = -1;
7051 min = exp->exp_min - sub->exp_min;
7052 }
7053 } else if (exp->exp_min > sub->exp_min) {
7054#ifdef DEBUG_DERIV
7055 printf("loops min mismatch 1 => forbidden ???\n");
7056#endif
7057 xmlExpFree(ctxt, tmp);
7058 return(forbiddenExp);
7059 } else {
7060 max = -1;
7061 min = 0;
7062 }
7063 } else {
7064 if (exp->exp_max == -1) {
7065#ifdef DEBUG_DERIV
7066 printf("Infinite loop consume finite loop\n");
7067#endif
7068 if (exp->exp_min > sub->exp_min) {
7069 max = -1;
7070 min = exp->exp_min - sub->exp_min;
7071 } else {
7072 max = -1;
7073 min = 0;
7074 }
7075 } else {
7076 if (exp->exp_max < sub->exp_max) {
7077#ifdef DEBUG_DERIV
7078 printf("loops max mismatch => forbidden\n");
7079#endif
7080 xmlExpFree(ctxt, tmp);
7081 return(forbiddenExp);
7082 }
7083 if (sub->exp_max > exp->exp_min)
7084 min = 0;
7085 else
7086 min = exp->exp_min - sub->exp_max;
7087 max = exp->exp_max - sub->exp_max;
7088 }
7089 }
7090#ifdef DEBUG_DERIV
7091 printf("loops match => SEQ(COUNT())\n");
7092#endif
7093 exp->exp_left->ref++;
7094 tmp2 = xmlExpHashGetEntry(ctxt, XML_EXP_COUNT, exp->exp_left,
7095 NULL, NULL, min, max);
7096 if (tmp2 == NULL) {
7097 return(NULL);
7098 }
7099 ret = xmlExpHashGetEntry(ctxt, XML_EXP_SEQ, tmp, tmp2,
7100 NULL, 0, 0);
7101 return(ret);
7102 }
7103 tmp = xmlExpExpDeriveInt(ctxt, exp->exp_left, sub);
7104 if (tmp == NULL)
7105 return(NULL);
7106 if (tmp == forbiddenExp) {
7107#ifdef DEBUG_DERIV
7108 printf("loop mismatch => forbidden\n");
7109#endif
7110 return(forbiddenExp);
7111 }
7112 if (exp->exp_min > 0)
7113 min = exp->exp_min - 1;
7114 else
7115 min = 0;
7116 if (exp->exp_max < 0)
7117 max = -1;
7118 else
7119 max = exp->exp_max - 1;
7120
7121#ifdef DEBUG_DERIV
7122 printf("loop match => SEQ(COUNT())\n");
7123#endif
7124 exp->exp_left->ref++;
7125 tmp2 = xmlExpHashGetEntry(ctxt, XML_EXP_COUNT, exp->exp_left,
7126 NULL, NULL, min, max);
7127 if (tmp2 == NULL)
7128 return(NULL);
7129 ret = xmlExpHashGetEntry(ctxt, XML_EXP_SEQ, tmp, tmp2,
7130 NULL, 0, 0);
7131 return(ret);
7132 }
7133 }
7134
Daniel Veillardccb4d412005-08-23 13:41:17 +00007135#ifdef DEBUG_DERIV
7136 printf("Fallback to derivative\n");
7137#endif
7138 if (IS_NILLABLE(sub)) {
7139 if (!(IS_NILLABLE(exp)))
7140 return(forbiddenExp);
7141 else
7142 ret = emptyExp;
7143 } else
7144 ret = NULL;
Daniel Veillard81a8ec62005-08-22 00:20:58 +00007145 /*
7146 * here the structured derivation made no progress so
7147 * we use the default token based derivation to force one more step
7148 */
7149 if (ctxt->tabSize == 0)
7150 ctxt->tabSize = 40;
7151
7152 tab = (const xmlChar **) xmlMalloc(ctxt->tabSize *
7153 sizeof(const xmlChar *));
7154 if (tab == NULL) {
7155 return(NULL);
7156 }
7157
Daniel Veillard81a8ec62005-08-22 00:20:58 +00007158 /*
7159 * collect all the strings accepted by the subexpression on input
7160 */
7161 len = xmlExpGetStartInt(ctxt, sub, tab, ctxt->tabSize, 0);
7162 while (len < 0) {
7163 const xmlChar **temp;
Rob Richards54a8f672005-10-07 02:33:00 +00007164 temp = (const xmlChar **) xmlRealloc((xmlChar **) tab, ctxt->tabSize * 2 *
Daniel Veillard81a8ec62005-08-22 00:20:58 +00007165 sizeof(const xmlChar *));
7166 if (temp == NULL) {
Rob Richards54a8f672005-10-07 02:33:00 +00007167 xmlFree((xmlChar **) tab);
Daniel Veillard81a8ec62005-08-22 00:20:58 +00007168 return(NULL);
7169 }
7170 tab = temp;
7171 ctxt->tabSize *= 2;
7172 len = xmlExpGetStartInt(ctxt, sub, tab, ctxt->tabSize, 0);
7173 }
Daniel Veillard81a8ec62005-08-22 00:20:58 +00007174 for (i = 0;i < len;i++) {
7175 tmp = xmlExpStringDeriveInt(ctxt, exp, tab[i]);
7176 if ((tmp == NULL) || (tmp == forbiddenExp)) {
7177 xmlExpFree(ctxt, ret);
Rob Richards54a8f672005-10-07 02:33:00 +00007178 xmlFree((xmlChar **) tab);
Daniel Veillard81a8ec62005-08-22 00:20:58 +00007179 return(tmp);
7180 }
7181 tmp2 = xmlExpStringDeriveInt(ctxt, sub, tab[i]);
7182 if ((tmp2 == NULL) || (tmp2 == forbiddenExp)) {
7183 xmlExpFree(ctxt, tmp);
7184 xmlExpFree(ctxt, ret);
Rob Richards54a8f672005-10-07 02:33:00 +00007185 xmlFree((xmlChar **) tab);
Daniel Veillard81a8ec62005-08-22 00:20:58 +00007186 return(tmp);
7187 }
7188 tmp3 = xmlExpExpDeriveInt(ctxt, tmp, tmp2);
7189 xmlExpFree(ctxt, tmp);
7190 xmlExpFree(ctxt, tmp2);
7191
7192 if ((tmp3 == NULL) || (tmp3 == forbiddenExp)) {
7193 xmlExpFree(ctxt, ret);
Rob Richards54a8f672005-10-07 02:33:00 +00007194 xmlFree((xmlChar **) tab);
Daniel Veillard81a8ec62005-08-22 00:20:58 +00007195 return(tmp3);
7196 }
7197
7198 if (ret == NULL)
7199 ret = tmp3;
7200 else {
7201 ret = xmlExpHashGetEntry(ctxt, XML_EXP_OR, ret, tmp3, NULL, 0, 0);
7202 if (ret == NULL) {
Rob Richards54a8f672005-10-07 02:33:00 +00007203 xmlFree((xmlChar **) tab);
Daniel Veillard81a8ec62005-08-22 00:20:58 +00007204 return(NULL);
7205 }
7206 }
7207 }
Rob Richards54a8f672005-10-07 02:33:00 +00007208 xmlFree((xmlChar **) tab);
Daniel Veillard81a8ec62005-08-22 00:20:58 +00007209 return(ret);
7210}
7211
7212/**
Daniel Veillard0090bd52005-08-22 14:43:43 +00007213 * xmlExpExpDerive:
7214 * @ctxt: the expressions context
7215 * @exp: the englobing expression
7216 * @sub: the subexpression
7217 *
7218 * Evaluates the expression resulting from @exp consuming a sub expression @sub
7219 * Based on algebraic derivation and sometimes direct Brzozowski derivation
7220 * it usually tatkes less than linear time and can handle expressions generating
7221 * infinite languages.
7222 *
7223 * Returns the resulting expression or NULL in case of internal error, the
7224 * result must be freed
7225 */
7226xmlExpNodePtr
7227xmlExpExpDerive(xmlExpCtxtPtr ctxt, xmlExpNodePtr exp, xmlExpNodePtr sub) {
7228 if ((exp == NULL) || (ctxt == NULL) || (sub == NULL))
7229 return(NULL);
7230
7231 /*
7232 * O(1) speedups
7233 */
7234 if (IS_NILLABLE(sub) && (!IS_NILLABLE(exp))) {
7235#ifdef DEBUG_DERIV
7236 printf("Sub nillable and not exp : can't subsume\n");
7237#endif
7238 return(forbiddenExp);
7239 }
7240 if (xmlExpCheckCard(exp, sub) == 0) {
7241#ifdef DEBUG_DERIV
7242 printf("sub generate longuer sequances than exp : can't subsume\n");
7243#endif
7244 return(forbiddenExp);
7245 }
7246 return(xmlExpExpDeriveInt(ctxt, exp, sub));
7247}
7248
7249/**
Daniel Veillard81a8ec62005-08-22 00:20:58 +00007250 * xmlExpSubsume:
7251 * @ctxt: the expressions context
7252 * @exp: the englobing expression
7253 * @sub: the subexpression
7254 *
7255 * Check whether @exp accepts all the languages accexpted by @sub
7256 * the input being a subexpression.
7257 *
7258 * Returns 1 if true 0 if false and -1 in case of failure.
7259 */
7260int
7261xmlExpSubsume(xmlExpCtxtPtr ctxt, xmlExpNodePtr exp, xmlExpNodePtr sub) {
7262 xmlExpNodePtr tmp;
7263
7264 if ((exp == NULL) || (ctxt == NULL) || (sub == NULL))
7265 return(-1);
7266
7267 /*
7268 * TODO: speedup by checking the language of sub is a subset of the
7269 * language of exp
7270 */
7271 /*
7272 * O(1) speedups
7273 */
7274 if (IS_NILLABLE(sub) && (!IS_NILLABLE(exp))) {
7275#ifdef DEBUG_DERIV
7276 printf("Sub nillable and not exp : can't subsume\n");
7277#endif
7278 return(0);
7279 }
7280 if (xmlExpCheckCard(exp, sub) == 0) {
7281#ifdef DEBUG_DERIV
7282 printf("sub generate longuer sequances than exp : can't subsume\n");
7283#endif
7284 return(0);
7285 }
7286 tmp = xmlExpExpDeriveInt(ctxt, exp, sub);
7287#ifdef DEBUG_DERIV
7288 printf("Result derivation :\n");
7289 PRINT_EXP(tmp);
7290#endif
7291 if (tmp == NULL)
7292 return(-1);
7293 if (tmp == forbiddenExp)
7294 return(0);
7295 if (tmp == emptyExp)
7296 return(1);
7297 if ((tmp != NULL) && (IS_NILLABLE(tmp))) {
7298 xmlExpFree(ctxt, tmp);
7299 return(1);
7300 }
7301 xmlExpFree(ctxt, tmp);
7302 return(0);
7303}
Daniel Veillard465a0002005-08-22 12:07:04 +00007304
7305/************************************************************************
7306 * *
7307 * Parsing expression *
7308 * *
7309 ************************************************************************/
7310
7311static xmlExpNodePtr xmlExpParseExpr(xmlExpCtxtPtr ctxt);
7312
7313#undef CUR
7314#define CUR (*ctxt->cur)
7315#undef NEXT
7316#define NEXT ctxt->cur++;
7317#undef IS_BLANK
7318#define IS_BLANK(c) ((c == ' ') || (c == '\n') || (c == '\r') || (c == '\t'))
7319#define SKIP_BLANKS while (IS_BLANK(*ctxt->cur)) ctxt->cur++;
7320
7321static int
7322xmlExpParseNumber(xmlExpCtxtPtr ctxt) {
7323 int ret = 0;
7324
7325 SKIP_BLANKS
7326 if (CUR == '*') {
7327 NEXT
7328 return(-1);
7329 }
7330 if ((CUR < '0') || (CUR > '9'))
7331 return(-1);
7332 while ((CUR >= '0') && (CUR <= '9')) {
7333 ret = ret * 10 + (CUR - '0');
7334 NEXT
7335 }
7336 return(ret);
7337}
7338
7339static xmlExpNodePtr
7340xmlExpParseOr(xmlExpCtxtPtr ctxt) {
7341 const char *base;
7342 xmlExpNodePtr ret;
7343 const xmlChar *val;
7344
7345 SKIP_BLANKS
7346 base = ctxt->cur;
7347 if (*ctxt->cur == '(') {
7348 NEXT
7349 ret = xmlExpParseExpr(ctxt);
7350 SKIP_BLANKS
7351 if (*ctxt->cur != ')') {
7352 fprintf(stderr, "unbalanced '(' : %s\n", base);
7353 xmlExpFree(ctxt, ret);
7354 return(NULL);
7355 }
7356 NEXT;
7357 SKIP_BLANKS
7358 goto parse_quantifier;
7359 }
7360 while ((CUR != 0) && (!(IS_BLANK(CUR))) && (CUR != '(') &&
7361 (CUR != ')') && (CUR != '|') && (CUR != ',') && (CUR != '{') &&
7362 (CUR != '*') && (CUR != '+') && (CUR != '?') && (CUR != '}'))
7363 NEXT;
7364 val = xmlDictLookup(ctxt->dict, BAD_CAST base, ctxt->cur - base);
7365 if (val == NULL)
7366 return(NULL);
7367 ret = xmlExpHashGetEntry(ctxt, XML_EXP_ATOM, NULL, NULL, val, 0, 0);
7368 if (ret == NULL)
7369 return(NULL);
7370 SKIP_BLANKS
7371parse_quantifier:
7372 if (CUR == '{') {
7373 int min, max;
7374
7375 NEXT
7376 min = xmlExpParseNumber(ctxt);
7377 if (min < 0) {
7378 xmlExpFree(ctxt, ret);
7379 return(NULL);
7380 }
7381 SKIP_BLANKS
7382 if (CUR == ',') {
7383 NEXT
7384 max = xmlExpParseNumber(ctxt);
7385 SKIP_BLANKS
7386 } else
7387 max = min;
7388 if (CUR != '}') {
7389 xmlExpFree(ctxt, ret);
7390 return(NULL);
7391 }
7392 NEXT
7393 ret = xmlExpHashGetEntry(ctxt, XML_EXP_COUNT, ret, NULL, NULL,
7394 min, max);
7395 SKIP_BLANKS
7396 } else if (CUR == '?') {
7397 NEXT
7398 ret = xmlExpHashGetEntry(ctxt, XML_EXP_COUNT, ret, NULL, NULL,
7399 0, 1);
7400 SKIP_BLANKS
7401 } else if (CUR == '+') {
7402 NEXT
7403 ret = xmlExpHashGetEntry(ctxt, XML_EXP_COUNT, ret, NULL, NULL,
7404 1, -1);
7405 SKIP_BLANKS
7406 } else if (CUR == '*') {
7407 NEXT
7408 ret = xmlExpHashGetEntry(ctxt, XML_EXP_COUNT, ret, NULL, NULL,
7409 0, -1);
7410 SKIP_BLANKS
7411 }
7412 return(ret);
7413}
7414
7415
7416static xmlExpNodePtr
7417xmlExpParseSeq(xmlExpCtxtPtr ctxt) {
7418 xmlExpNodePtr ret, right;
7419
7420 ret = xmlExpParseOr(ctxt);
7421 SKIP_BLANKS
7422 while (CUR == '|') {
7423 NEXT
7424 right = xmlExpParseOr(ctxt);
7425 if (right == NULL) {
7426 xmlExpFree(ctxt, ret);
7427 return(NULL);
7428 }
7429 ret = xmlExpHashGetEntry(ctxt, XML_EXP_OR, ret, right, NULL, 0, 0);
7430 if (ret == NULL)
7431 return(NULL);
7432 }
7433 return(ret);
7434}
7435
7436static xmlExpNodePtr
7437xmlExpParseExpr(xmlExpCtxtPtr ctxt) {
7438 xmlExpNodePtr ret, right;
7439
7440 ret = xmlExpParseSeq(ctxt);
7441 SKIP_BLANKS
7442 while (CUR == ',') {
7443 NEXT
7444 right = xmlExpParseSeq(ctxt);
7445 if (right == NULL) {
7446 xmlExpFree(ctxt, ret);
7447 return(NULL);
7448 }
7449 ret = xmlExpHashGetEntry(ctxt, XML_EXP_SEQ, ret, right, NULL, 0, 0);
7450 if (ret == NULL)
7451 return(NULL);
7452 }
7453 return(ret);
7454}
7455
7456/**
7457 * xmlExpParse:
7458 * @ctxt: the expressions context
7459 * @expr: the 0 terminated string
7460 *
7461 * Minimal parser for regexps, it understand the following constructs
7462 * - string terminals
7463 * - choice operator |
7464 * - sequence operator ,
7465 * - subexpressions (...)
7466 * - usual cardinality operators + * and ?
7467 * - finite sequences { min, max }
7468 * - infinite sequences { min, * }
7469 * There is minimal checkings made especially no checking on strings values
7470 *
7471 * Returns a new expression or NULL in case of failure
7472 */
7473xmlExpNodePtr
7474xmlExpParse(xmlExpCtxtPtr ctxt, const char *expr) {
7475 xmlExpNodePtr ret;
7476
7477 ctxt->expr = expr;
7478 ctxt->cur = expr;
7479
7480 ret = xmlExpParseExpr(ctxt);
7481 SKIP_BLANKS
7482 if (*ctxt->cur != 0) {
7483 xmlExpFree(ctxt, ret);
7484 return(NULL);
7485 }
7486 return(ret);
7487}
7488
7489static void
7490xmlExpDumpInt(xmlBufferPtr buf, xmlExpNodePtr expr, int glob) {
7491 xmlExpNodePtr c;
7492
7493 if (expr == NULL) return;
7494 if (glob) xmlBufferWriteChar(buf, "(");
7495 switch (expr->type) {
7496 case XML_EXP_EMPTY:
7497 xmlBufferWriteChar(buf, "empty");
7498 break;
7499 case XML_EXP_FORBID:
7500 xmlBufferWriteChar(buf, "forbidden");
7501 break;
7502 case XML_EXP_ATOM:
7503 xmlBufferWriteCHAR(buf, expr->exp_str);
7504 break;
7505 case XML_EXP_SEQ:
7506 c = expr->exp_left;
7507 if ((c->type == XML_EXP_SEQ) || (c->type == XML_EXP_OR))
7508 xmlExpDumpInt(buf, c, 1);
7509 else
7510 xmlExpDumpInt(buf, c, 0);
7511 xmlBufferWriteChar(buf, " , ");
7512 c = expr->exp_right;
7513 if ((c->type == XML_EXP_SEQ) || (c->type == XML_EXP_OR))
7514 xmlExpDumpInt(buf, c, 1);
7515 else
7516 xmlExpDumpInt(buf, c, 0);
7517 break;
7518 case XML_EXP_OR:
7519 c = expr->exp_left;
7520 if ((c->type == XML_EXP_SEQ) || (c->type == XML_EXP_OR))
7521 xmlExpDumpInt(buf, c, 1);
7522 else
7523 xmlExpDumpInt(buf, c, 0);
7524 xmlBufferWriteChar(buf, " | ");
7525 c = expr->exp_right;
7526 if ((c->type == XML_EXP_SEQ) || (c->type == XML_EXP_OR))
7527 xmlExpDumpInt(buf, c, 1);
7528 else
7529 xmlExpDumpInt(buf, c, 0);
7530 break;
7531 case XML_EXP_COUNT: {
7532 char rep[40];
7533
7534 c = expr->exp_left;
7535 if ((c->type == XML_EXP_SEQ) || (c->type == XML_EXP_OR))
7536 xmlExpDumpInt(buf, c, 1);
7537 else
7538 xmlExpDumpInt(buf, c, 0);
7539 if ((expr->exp_min == 0) && (expr->exp_max == 1)) {
7540 rep[0] = '?';
7541 rep[1] = 0;
7542 } else if ((expr->exp_min == 0) && (expr->exp_max == -1)) {
7543 rep[0] = '*';
7544 rep[1] = 0;
7545 } else if ((expr->exp_min == 1) && (expr->exp_max == -1)) {
7546 rep[0] = '+';
7547 rep[1] = 0;
7548 } else if (expr->exp_max == expr->exp_min) {
7549 snprintf(rep, 39, "{%d}", expr->exp_min);
7550 } else if (expr->exp_max < 0) {
7551 snprintf(rep, 39, "{%d,inf}", expr->exp_min);
7552 } else {
7553 snprintf(rep, 39, "{%d,%d}", expr->exp_min, expr->exp_max);
7554 }
7555 rep[39] = 0;
7556 xmlBufferWriteChar(buf, rep);
7557 break;
7558 }
7559 default:
7560 fprintf(stderr, "Error in tree\n");
7561 }
7562 if (glob)
7563 xmlBufferWriteChar(buf, ")");
7564}
7565/**
7566 * xmlExpDump:
7567 * @buf: a buffer to receive the output
7568 * @expr: the compiled expression
7569 *
7570 * Serialize the expression as compiled to the buffer
7571 */
7572void
Daniel Veillard5eee7672005-08-22 21:22:27 +00007573xmlExpDump(xmlBufferPtr buf, xmlExpNodePtr expr) {
7574 if ((buf == NULL) || (expr == NULL))
Daniel Veillard465a0002005-08-22 12:07:04 +00007575 return;
Daniel Veillard5eee7672005-08-22 21:22:27 +00007576 xmlExpDumpInt(buf, expr, 0);
Daniel Veillard465a0002005-08-22 12:07:04 +00007577}
7578
7579/**
7580 * xmlExpMaxToken:
7581 * @expr: a compiled expression
7582 *
7583 * Indicate the maximum number of input a expression can accept
7584 *
7585 * Returns the maximum length or -1 in case of error
7586 */
7587int
7588xmlExpMaxToken(xmlExpNodePtr expr) {
7589 if (expr == NULL)
7590 return(-1);
7591 return(expr->c_max);
7592}
7593
7594/**
7595 * xmlExpCtxtNbNodes:
7596 * @ctxt: an expression context
7597 *
7598 * Debugging facility provides the number of allocated nodes at a that point
7599 *
7600 * Returns the number of nodes in use or -1 in case of error
7601 */
7602int
7603xmlExpCtxtNbNodes(xmlExpCtxtPtr ctxt) {
7604 if (ctxt == NULL)
7605 return(-1);
7606 return(ctxt->nb_nodes);
7607}
7608
7609/**
7610 * xmlExpCtxtNbCons:
7611 * @ctxt: an expression context
7612 *
7613 * Debugging facility provides the number of allocated nodes over lifetime
7614 *
7615 * Returns the number of nodes ever allocated or -1 in case of error
7616 */
7617int
7618xmlExpCtxtNbCons(xmlExpCtxtPtr ctxt) {
7619 if (ctxt == NULL)
7620 return(-1);
7621 return(ctxt->nb_cons);
7622}
7623
Daniel Veillard81a8ec62005-08-22 00:20:58 +00007624#endif /* LIBXML_EXPR_ENABLED */
Daniel Veillard5d4644e2005-04-01 13:11:58 +00007625#define bottom_xmlregexp
7626#include "elfgcchack.h"
Daniel Veillard4255d502002-04-16 15:50:10 +00007627#endif /* LIBXML_REGEXP_ENABLED */