blob: 700b886f812bf14d18c56e01228363712eb122d8 [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 Veillardff46a042003-10-08 08:53:17 +000045#define ERROR(str) \
46 ctxt->error = XML_REGEXP_COMPILE_ERROR; \
47 xmlRegexpErrCompile(ctxt, str);
Daniel Veillard4255d502002-04-16 15:50:10 +000048#define NEXT ctxt->cur++
49#define CUR (*(ctxt->cur))
50#define NXT(index) (ctxt->cur[index])
51
52#define CUR_SCHAR(s, l) xmlStringCurrentChar(NULL, s, &l)
53#define NEXTL(l) ctxt->cur += l;
Daniel Veillardc0826a72004-08-10 14:17:33 +000054#define XML_REG_STRING_SEPARATOR '|'
Daniel Veillard4255d502002-04-16 15:50:10 +000055
Daniel Veillarde19fc232002-04-22 16:01:24 +000056/**
57 * TODO:
58 *
59 * macro to flag unimplemented blocks
60 */
61#define TODO \
62 xmlGenericError(xmlGenericErrorContext, \
63 "Unimplemented block at %s:%d\n", \
64 __FILE__, __LINE__);
65
Daniel Veillard4255d502002-04-16 15:50:10 +000066/************************************************************************
67 * *
68 * Datatypes and structures *
69 * *
70 ************************************************************************/
71
72typedef enum {
73 XML_REGEXP_EPSILON = 1,
74 XML_REGEXP_CHARVAL,
75 XML_REGEXP_RANGES,
76 XML_REGEXP_SUBREG,
77 XML_REGEXP_STRING,
78 XML_REGEXP_ANYCHAR, /* . */
79 XML_REGEXP_ANYSPACE, /* \s */
80 XML_REGEXP_NOTSPACE, /* \S */
81 XML_REGEXP_INITNAME, /* \l */
82 XML_REGEXP_NOTINITNAME, /* \l */
83 XML_REGEXP_NAMECHAR, /* \c */
84 XML_REGEXP_NOTNAMECHAR, /* \C */
85 XML_REGEXP_DECIMAL, /* \d */
86 XML_REGEXP_NOTDECIMAL, /* \d */
87 XML_REGEXP_REALCHAR, /* \w */
88 XML_REGEXP_NOTREALCHAR, /* \w */
89 XML_REGEXP_LETTER,
90 XML_REGEXP_LETTER_UPPERCASE,
91 XML_REGEXP_LETTER_LOWERCASE,
92 XML_REGEXP_LETTER_TITLECASE,
93 XML_REGEXP_LETTER_MODIFIER,
94 XML_REGEXP_LETTER_OTHERS,
95 XML_REGEXP_MARK,
96 XML_REGEXP_MARK_NONSPACING,
97 XML_REGEXP_MARK_SPACECOMBINING,
98 XML_REGEXP_MARK_ENCLOSING,
99 XML_REGEXP_NUMBER,
100 XML_REGEXP_NUMBER_DECIMAL,
101 XML_REGEXP_NUMBER_LETTER,
102 XML_REGEXP_NUMBER_OTHERS,
103 XML_REGEXP_PUNCT,
104 XML_REGEXP_PUNCT_CONNECTOR,
105 XML_REGEXP_PUNCT_DASH,
106 XML_REGEXP_PUNCT_OPEN,
107 XML_REGEXP_PUNCT_CLOSE,
108 XML_REGEXP_PUNCT_INITQUOTE,
109 XML_REGEXP_PUNCT_FINQUOTE,
110 XML_REGEXP_PUNCT_OTHERS,
111 XML_REGEXP_SEPAR,
112 XML_REGEXP_SEPAR_SPACE,
113 XML_REGEXP_SEPAR_LINE,
114 XML_REGEXP_SEPAR_PARA,
115 XML_REGEXP_SYMBOL,
116 XML_REGEXP_SYMBOL_MATH,
117 XML_REGEXP_SYMBOL_CURRENCY,
118 XML_REGEXP_SYMBOL_MODIFIER,
119 XML_REGEXP_SYMBOL_OTHERS,
120 XML_REGEXP_OTHER,
121 XML_REGEXP_OTHER_CONTROL,
122 XML_REGEXP_OTHER_FORMAT,
123 XML_REGEXP_OTHER_PRIVATE,
124 XML_REGEXP_OTHER_NA,
125 XML_REGEXP_BLOCK_NAME
126} xmlRegAtomType;
127
128typedef enum {
129 XML_REGEXP_QUANT_EPSILON = 1,
130 XML_REGEXP_QUANT_ONCE,
131 XML_REGEXP_QUANT_OPT,
132 XML_REGEXP_QUANT_MULT,
133 XML_REGEXP_QUANT_PLUS,
Daniel Veillard7646b182002-04-20 06:41:40 +0000134 XML_REGEXP_QUANT_ONCEONLY,
135 XML_REGEXP_QUANT_ALL,
Daniel Veillard4255d502002-04-16 15:50:10 +0000136 XML_REGEXP_QUANT_RANGE
137} xmlRegQuantType;
138
139typedef enum {
140 XML_REGEXP_START_STATE = 1,
141 XML_REGEXP_FINAL_STATE,
Daniel Veillardcc026dc2005-01-12 13:21:17 +0000142 XML_REGEXP_TRANS_STATE,
143 XML_REGEXP_SINK_STATE
Daniel Veillard4255d502002-04-16 15:50:10 +0000144} xmlRegStateType;
145
146typedef enum {
147 XML_REGEXP_MARK_NORMAL = 0,
148 XML_REGEXP_MARK_START,
149 XML_REGEXP_MARK_VISITED
150} xmlRegMarkedType;
151
152typedef struct _xmlRegRange xmlRegRange;
153typedef xmlRegRange *xmlRegRangePtr;
154
155struct _xmlRegRange {
Daniel Veillardf8b9de32003-11-24 14:27:26 +0000156 int neg; /* 0 normal, 1 not, 2 exclude */
Daniel Veillard4255d502002-04-16 15:50:10 +0000157 xmlRegAtomType type;
158 int start;
159 int end;
160 xmlChar *blockName;
161};
162
163typedef struct _xmlRegAtom xmlRegAtom;
164typedef xmlRegAtom *xmlRegAtomPtr;
165
166typedef struct _xmlAutomataState xmlRegState;
167typedef xmlRegState *xmlRegStatePtr;
168
169struct _xmlRegAtom {
170 int no;
171 xmlRegAtomType type;
172 xmlRegQuantType quant;
173 int min;
174 int max;
175
176 void *valuep;
Daniel Veillarda646cfd2002-09-17 21:50:03 +0000177 void *valuep2;
Daniel Veillard4255d502002-04-16 15:50:10 +0000178 int neg;
179 int codepoint;
180 xmlRegStatePtr start;
181 xmlRegStatePtr stop;
182 int maxRanges;
183 int nbRanges;
184 xmlRegRangePtr *ranges;
185 void *data;
186};
187
188typedef struct _xmlRegCounter xmlRegCounter;
189typedef xmlRegCounter *xmlRegCounterPtr;
190
191struct _xmlRegCounter {
192 int min;
193 int max;
194};
195
196typedef struct _xmlRegTrans xmlRegTrans;
197typedef xmlRegTrans *xmlRegTransPtr;
198
199struct _xmlRegTrans {
200 xmlRegAtomPtr atom;
201 int to;
202 int counter;
203 int count;
204};
205
206struct _xmlAutomataState {
207 xmlRegStateType type;
208 xmlRegMarkedType mark;
Daniel Veillard23e73572002-09-19 19:56:43 +0000209 xmlRegMarkedType reached;
Daniel Veillard4255d502002-04-16 15:50:10 +0000210 int no;
Daniel Veillard4255d502002-04-16 15:50:10 +0000211 int maxTrans;
212 int nbTrans;
213 xmlRegTrans *trans;
Daniel Veillarddb68b742005-07-30 13:18:24 +0000214 /* knowing states ponting to us can speed things up */
215 int maxTransTo;
216 int nbTransTo;
217 int *transTo;
Daniel Veillard4255d502002-04-16 15:50:10 +0000218};
219
220typedef struct _xmlAutomata xmlRegParserCtxt;
221typedef xmlRegParserCtxt *xmlRegParserCtxtPtr;
222
223struct _xmlAutomata {
224 xmlChar *string;
225 xmlChar *cur;
226
227 int error;
228 int neg;
229
230 xmlRegStatePtr start;
231 xmlRegStatePtr end;
232 xmlRegStatePtr state;
233
234 xmlRegAtomPtr atom;
235
236 int maxAtoms;
237 int nbAtoms;
238 xmlRegAtomPtr *atoms;
239
240 int maxStates;
241 int nbStates;
242 xmlRegStatePtr *states;
243
244 int maxCounters;
245 int nbCounters;
246 xmlRegCounter *counters;
Daniel Veillarde19fc232002-04-22 16:01:24 +0000247
248 int determinist;
Daniel Veillard4255d502002-04-16 15:50:10 +0000249};
250
251struct _xmlRegexp {
252 xmlChar *string;
253 int nbStates;
254 xmlRegStatePtr *states;
255 int nbAtoms;
256 xmlRegAtomPtr *atoms;
257 int nbCounters;
258 xmlRegCounter *counters;
Daniel Veillarde19fc232002-04-22 16:01:24 +0000259 int determinist;
Daniel Veillard23e73572002-09-19 19:56:43 +0000260 /*
261 * That's the compact form for determinists automatas
262 */
263 int nbstates;
264 int *compact;
Daniel Veillard118aed72002-09-24 14:13:13 +0000265 void **transdata;
Daniel Veillard23e73572002-09-19 19:56:43 +0000266 int nbstrings;
267 xmlChar **stringMap;
Daniel Veillard4255d502002-04-16 15:50:10 +0000268};
269
270typedef struct _xmlRegExecRollback xmlRegExecRollback;
271typedef xmlRegExecRollback *xmlRegExecRollbackPtr;
272
273struct _xmlRegExecRollback {
274 xmlRegStatePtr state;/* the current state */
275 int index; /* the index in the input stack */
276 int nextbranch; /* the next transition to explore in that state */
William M. Brackddf71d62004-05-06 04:17:26 +0000277 int *counts; /* save the automata state if it has some */
Daniel Veillard4255d502002-04-16 15:50:10 +0000278};
279
280typedef struct _xmlRegInputToken xmlRegInputToken;
281typedef xmlRegInputToken *xmlRegInputTokenPtr;
282
283struct _xmlRegInputToken {
284 xmlChar *value;
285 void *data;
286};
287
288struct _xmlRegExecCtxt {
289 int status; /* execution status != 0 indicate an error */
William M. Brackddf71d62004-05-06 04:17:26 +0000290 int determinist; /* did we find an indeterministic behaviour */
Daniel Veillard4255d502002-04-16 15:50:10 +0000291 xmlRegexpPtr comp; /* the compiled regexp */
292 xmlRegExecCallbacks callback;
293 void *data;
294
295 xmlRegStatePtr state;/* the current state */
296 int transno; /* the current transition on that state */
William M. Brackddf71d62004-05-06 04:17:26 +0000297 int transcount; /* the number of chars in char counted transitions */
Daniel Veillard4255d502002-04-16 15:50:10 +0000298
299 /*
300 * A stack of rollback states
301 */
302 int maxRollbacks;
303 int nbRollbacks;
304 xmlRegExecRollback *rollbacks;
305
306 /*
307 * The state of the automata if any
308 */
309 int *counts;
310
311 /*
312 * The input stack
313 */
314 int inputStackMax;
315 int inputStackNr;
316 int index;
317 int *charStack;
318 const xmlChar *inputString; /* when operating on characters */
319 xmlRegInputTokenPtr inputStack;/* when operating on strings */
320
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +0000321 /*
322 * error handling
323 */
324 int errStateNo; /* the error state number */
325 xmlRegStatePtr errState; /* the error state */
326 xmlChar *errString; /* the string raising the error */
327 int *errCounts; /* counters at the error state */
Daniel Veillard4255d502002-04-16 15:50:10 +0000328};
329
Daniel Veillard441bc322002-04-20 17:38:48 +0000330#define REGEXP_ALL_COUNTER 0x123456
331#define REGEXP_ALL_LAX_COUNTER 0x123457
Daniel Veillard7646b182002-04-20 06:41:40 +0000332
Daniel Veillard4255d502002-04-16 15:50:10 +0000333static void xmlFAParseRegExp(xmlRegParserCtxtPtr ctxt, int top);
Daniel Veillard23e73572002-09-19 19:56:43 +0000334static void xmlRegFreeState(xmlRegStatePtr state);
335static void xmlRegFreeAtom(xmlRegAtomPtr atom);
Daniel Veillard9efc4762005-07-19 14:33:55 +0000336static int xmlRegStrEqualWildcard(const xmlChar *expStr, const xmlChar *valStr);
Daniel Veillard4255d502002-04-16 15:50:10 +0000337
338/************************************************************************
Daniel Veillardff46a042003-10-08 08:53:17 +0000339 * *
340 * Regexp memory error handler *
341 * *
342 ************************************************************************/
343/**
344 * xmlRegexpErrMemory:
William M. Brackddf71d62004-05-06 04:17:26 +0000345 * @extra: extra information
Daniel Veillardff46a042003-10-08 08:53:17 +0000346 *
347 * Handle an out of memory condition
348 */
349static void
350xmlRegexpErrMemory(xmlRegParserCtxtPtr ctxt, const char *extra)
351{
352 const char *regexp = NULL;
353 if (ctxt != NULL) {
354 regexp = (const char *) ctxt->string;
355 ctxt->error = XML_ERR_NO_MEMORY;
356 }
Daniel Veillard659e71e2003-10-10 14:10:40 +0000357 __xmlRaiseError(NULL, NULL, NULL, NULL, NULL, XML_FROM_REGEXP,
Daniel Veillardff46a042003-10-08 08:53:17 +0000358 XML_ERR_NO_MEMORY, XML_ERR_FATAL, NULL, 0, extra,
359 regexp, NULL, 0, 0,
360 "Memory allocation failed : %s\n", extra);
361}
362
363/**
364 * xmlRegexpErrCompile:
William M. Brackddf71d62004-05-06 04:17:26 +0000365 * @extra: extra information
Daniel Veillardff46a042003-10-08 08:53:17 +0000366 *
William M. Brackddf71d62004-05-06 04:17:26 +0000367 * Handle a compilation failure
Daniel Veillardff46a042003-10-08 08:53:17 +0000368 */
369static void
370xmlRegexpErrCompile(xmlRegParserCtxtPtr ctxt, const char *extra)
371{
372 const char *regexp = NULL;
373 int idx = 0;
374
375 if (ctxt != NULL) {
376 regexp = (const char *) ctxt->string;
377 idx = ctxt->cur - ctxt->string;
378 ctxt->error = XML_REGEXP_COMPILE_ERROR;
379 }
Daniel Veillard659e71e2003-10-10 14:10:40 +0000380 __xmlRaiseError(NULL, NULL, NULL, NULL, NULL, XML_FROM_REGEXP,
Daniel Veillardff46a042003-10-08 08:53:17 +0000381 XML_REGEXP_COMPILE_ERROR, XML_ERR_FATAL, NULL, 0, extra,
382 regexp, NULL, idx, 0,
383 "failed to compile: %s\n", extra);
384}
385
386/************************************************************************
Daniel Veillard4255d502002-04-16 15:50:10 +0000387 * *
388 * Allocation/Deallocation *
389 * *
390 ************************************************************************/
391
Daniel Veillard23e73572002-09-19 19:56:43 +0000392static int xmlFAComputesDeterminism(xmlRegParserCtxtPtr ctxt);
Daniel Veillard4255d502002-04-16 15:50:10 +0000393/**
394 * xmlRegEpxFromParse:
395 * @ctxt: the parser context used to build it
396 *
William M. Brackddf71d62004-05-06 04:17:26 +0000397 * Allocate a new regexp and fill it with the result from the parser
Daniel Veillard4255d502002-04-16 15:50:10 +0000398 *
399 * Returns the new regexp or NULL in case of error
400 */
401static xmlRegexpPtr
402xmlRegEpxFromParse(xmlRegParserCtxtPtr ctxt) {
403 xmlRegexpPtr ret;
404
405 ret = (xmlRegexpPtr) xmlMalloc(sizeof(xmlRegexp));
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000406 if (ret == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +0000407 xmlRegexpErrMemory(ctxt, "compiling regexp");
Daniel Veillard4255d502002-04-16 15:50:10 +0000408 return(NULL);
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000409 }
Daniel Veillard4255d502002-04-16 15:50:10 +0000410 memset(ret, 0, sizeof(xmlRegexp));
411 ret->string = ctxt->string;
Daniel Veillard4255d502002-04-16 15:50:10 +0000412 ret->nbStates = ctxt->nbStates;
Daniel Veillard4255d502002-04-16 15:50:10 +0000413 ret->states = ctxt->states;
Daniel Veillard4255d502002-04-16 15:50:10 +0000414 ret->nbAtoms = ctxt->nbAtoms;
Daniel Veillard4255d502002-04-16 15:50:10 +0000415 ret->atoms = ctxt->atoms;
Daniel Veillard4255d502002-04-16 15:50:10 +0000416 ret->nbCounters = ctxt->nbCounters;
Daniel Veillard4255d502002-04-16 15:50:10 +0000417 ret->counters = ctxt->counters;
Daniel Veillarde19fc232002-04-22 16:01:24 +0000418 ret->determinist = ctxt->determinist;
Daniel Veillard23e73572002-09-19 19:56:43 +0000419
420 if ((ret->determinist != 0) &&
421 (ret->nbCounters == 0) &&
Daniel Veillard118aed72002-09-24 14:13:13 +0000422 (ret->atoms != NULL) &&
Daniel Veillard23e73572002-09-19 19:56:43 +0000423 (ret->atoms[0] != NULL) &&
424 (ret->atoms[0]->type == XML_REGEXP_STRING)) {
425 int i, j, nbstates = 0, nbatoms = 0;
426 int *stateRemap;
427 int *stringRemap;
428 int *transitions;
Daniel Veillard118aed72002-09-24 14:13:13 +0000429 void **transdata;
Daniel Veillard23e73572002-09-19 19:56:43 +0000430 xmlChar **stringMap;
431 xmlChar *value;
432
433 /*
434 * Switch to a compact representation
435 * 1/ counting the effective number of states left
William M. Brackddf71d62004-05-06 04:17:26 +0000436 * 2/ counting the unique number of atoms, and check that
Daniel Veillard23e73572002-09-19 19:56:43 +0000437 * they are all of the string type
438 * 3/ build a table state x atom for the transitions
439 */
440
441 stateRemap = xmlMalloc(ret->nbStates * sizeof(int));
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000442 if (stateRemap == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +0000443 xmlRegexpErrMemory(ctxt, "compiling regexp");
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000444 xmlFree(ret);
445 return(NULL);
446 }
Daniel Veillard23e73572002-09-19 19:56:43 +0000447 for (i = 0;i < ret->nbStates;i++) {
448 if (ret->states[i] != NULL) {
449 stateRemap[i] = nbstates;
450 nbstates++;
451 } else {
452 stateRemap[i] = -1;
453 }
454 }
455#ifdef DEBUG_COMPACTION
456 printf("Final: %d states\n", nbstates);
457#endif
458 stringMap = xmlMalloc(ret->nbAtoms * sizeof(char *));
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000459 if (stringMap == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +0000460 xmlRegexpErrMemory(ctxt, "compiling regexp");
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000461 xmlFree(stateRemap);
462 xmlFree(ret);
463 return(NULL);
464 }
Daniel Veillard23e73572002-09-19 19:56:43 +0000465 stringRemap = xmlMalloc(ret->nbAtoms * sizeof(int));
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000466 if (stringRemap == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +0000467 xmlRegexpErrMemory(ctxt, "compiling regexp");
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000468 xmlFree(stringMap);
469 xmlFree(stateRemap);
470 xmlFree(ret);
471 return(NULL);
472 }
Daniel Veillard23e73572002-09-19 19:56:43 +0000473 for (i = 0;i < ret->nbAtoms;i++) {
474 if ((ret->atoms[i]->type == XML_REGEXP_STRING) &&
475 (ret->atoms[i]->quant == XML_REGEXP_QUANT_ONCE)) {
476 value = ret->atoms[i]->valuep;
477 for (j = 0;j < nbatoms;j++) {
478 if (xmlStrEqual(stringMap[j], value)) {
479 stringRemap[i] = j;
480 break;
481 }
482 }
483 if (j >= nbatoms) {
484 stringRemap[i] = nbatoms;
485 stringMap[nbatoms] = xmlStrdup(value);
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000486 if (stringMap[nbatoms] == NULL) {
487 for (i = 0;i < nbatoms;i++)
488 xmlFree(stringMap[i]);
489 xmlFree(stringRemap);
490 xmlFree(stringMap);
491 xmlFree(stateRemap);
492 xmlFree(ret);
493 return(NULL);
494 }
Daniel Veillard23e73572002-09-19 19:56:43 +0000495 nbatoms++;
496 }
497 } else {
498 xmlFree(stateRemap);
499 xmlFree(stringRemap);
500 for (i = 0;i < nbatoms;i++)
501 xmlFree(stringMap[i]);
502 xmlFree(stringMap);
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000503 xmlFree(ret);
504 return(NULL);
Daniel Veillard23e73572002-09-19 19:56:43 +0000505 }
506 }
507#ifdef DEBUG_COMPACTION
508 printf("Final: %d atoms\n", nbatoms);
509#endif
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000510 transitions = (int *) xmlMalloc((nbstates + 1) *
511 (nbatoms + 1) * sizeof(int));
512 if (transitions == NULL) {
513 xmlFree(stateRemap);
514 xmlFree(stringRemap);
515 xmlFree(stringMap);
516 xmlFree(ret);
517 return(NULL);
518 }
519 memset(transitions, 0, (nbstates + 1) * (nbatoms + 1) * sizeof(int));
Daniel Veillard23e73572002-09-19 19:56:43 +0000520
521 /*
522 * Allocate the transition table. The first entry for each
William M. Brackddf71d62004-05-06 04:17:26 +0000523 * state corresponds to the state type.
Daniel Veillard23e73572002-09-19 19:56:43 +0000524 */
Daniel Veillard118aed72002-09-24 14:13:13 +0000525 transdata = NULL;
Daniel Veillard23e73572002-09-19 19:56:43 +0000526
527 for (i = 0;i < ret->nbStates;i++) {
528 int stateno, atomno, targetno, prev;
529 xmlRegStatePtr state;
530 xmlRegTransPtr trans;
531
532 stateno = stateRemap[i];
533 if (stateno == -1)
534 continue;
535 state = ret->states[i];
536
537 transitions[stateno * (nbatoms + 1)] = state->type;
538
539 for (j = 0;j < state->nbTrans;j++) {
540 trans = &(state->trans[j]);
541 if ((trans->to == -1) || (trans->atom == NULL))
542 continue;
543 atomno = stringRemap[trans->atom->no];
Daniel Veillard118aed72002-09-24 14:13:13 +0000544 if ((trans->atom->data != NULL) && (transdata == NULL)) {
545 transdata = (void **) xmlMalloc(nbstates * nbatoms *
546 sizeof(void *));
547 if (transdata != NULL)
548 memset(transdata, 0,
549 nbstates * nbatoms * sizeof(void *));
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000550 else {
Daniel Veillardff46a042003-10-08 08:53:17 +0000551 xmlRegexpErrMemory(ctxt, "compiling regexp");
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000552 break;
553 }
Daniel Veillard118aed72002-09-24 14:13:13 +0000554 }
Daniel Veillard23e73572002-09-19 19:56:43 +0000555 targetno = stateRemap[trans->to];
556 /*
William M. Brackddf71d62004-05-06 04:17:26 +0000557 * if the same atom can generate transitions to 2 different
Daniel Veillard23e73572002-09-19 19:56:43 +0000558 * states then it means the automata is not determinist and
559 * the compact form can't be used !
560 */
561 prev = transitions[stateno * (nbatoms + 1) + atomno + 1];
562 if (prev != 0) {
563 if (prev != targetno + 1) {
Daniel Veillard23e73572002-09-19 19:56:43 +0000564 ret->determinist = 0;
565#ifdef DEBUG_COMPACTION
566 printf("Indet: state %d trans %d, atom %d to %d : %d to %d\n",
567 i, j, trans->atom->no, trans->to, atomno, targetno);
568 printf(" previous to is %d\n", prev);
569#endif
570 ret->determinist = 0;
Daniel Veillard118aed72002-09-24 14:13:13 +0000571 if (transdata != NULL)
572 xmlFree(transdata);
Daniel Veillard23e73572002-09-19 19:56:43 +0000573 xmlFree(transitions);
574 xmlFree(stateRemap);
575 xmlFree(stringRemap);
576 for (i = 0;i < nbatoms;i++)
577 xmlFree(stringMap[i]);
578 xmlFree(stringMap);
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000579 goto not_determ;
Daniel Veillard23e73572002-09-19 19:56:43 +0000580 }
581 } else {
582#if 0
583 printf("State %d trans %d: atom %d to %d : %d to %d\n",
584 i, j, trans->atom->no, trans->to, atomno, targetno);
585#endif
586 transitions[stateno * (nbatoms + 1) + atomno + 1] =
Daniel Veillard118aed72002-09-24 14:13:13 +0000587 targetno + 1; /* to avoid 0 */
588 if (transdata != NULL)
589 transdata[stateno * nbatoms + atomno] =
590 trans->atom->data;
Daniel Veillard23e73572002-09-19 19:56:43 +0000591 }
592 }
593 }
594 ret->determinist = 1;
595#ifdef DEBUG_COMPACTION
596 /*
597 * Debug
598 */
599 for (i = 0;i < nbstates;i++) {
600 for (j = 0;j < nbatoms + 1;j++) {
601 printf("%02d ", transitions[i * (nbatoms + 1) + j]);
602 }
603 printf("\n");
604 }
605 printf("\n");
606#endif
607 /*
608 * Cleanup of the old data
609 */
610 if (ret->states != NULL) {
611 for (i = 0;i < ret->nbStates;i++)
612 xmlRegFreeState(ret->states[i]);
613 xmlFree(ret->states);
614 }
615 ret->states = NULL;
616 ret->nbStates = 0;
617 if (ret->atoms != NULL) {
618 for (i = 0;i < ret->nbAtoms;i++)
619 xmlRegFreeAtom(ret->atoms[i]);
620 xmlFree(ret->atoms);
621 }
622 ret->atoms = NULL;
623 ret->nbAtoms = 0;
624
625 ret->compact = transitions;
Daniel Veillard118aed72002-09-24 14:13:13 +0000626 ret->transdata = transdata;
Daniel Veillard23e73572002-09-19 19:56:43 +0000627 ret->stringMap = stringMap;
628 ret->nbstrings = nbatoms;
629 ret->nbstates = nbstates;
630 xmlFree(stateRemap);
631 xmlFree(stringRemap);
632 }
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000633not_determ:
634 ctxt->string = NULL;
635 ctxt->nbStates = 0;
636 ctxt->states = NULL;
637 ctxt->nbAtoms = 0;
638 ctxt->atoms = NULL;
639 ctxt->nbCounters = 0;
640 ctxt->counters = NULL;
Daniel Veillard4255d502002-04-16 15:50:10 +0000641 return(ret);
642}
643
644/**
645 * xmlRegNewParserCtxt:
646 * @string: the string to parse
647 *
648 * Allocate a new regexp parser context
649 *
650 * Returns the new context or NULL in case of error
651 */
652static xmlRegParserCtxtPtr
653xmlRegNewParserCtxt(const xmlChar *string) {
654 xmlRegParserCtxtPtr ret;
655
656 ret = (xmlRegParserCtxtPtr) xmlMalloc(sizeof(xmlRegParserCtxt));
657 if (ret == NULL)
658 return(NULL);
659 memset(ret, 0, sizeof(xmlRegParserCtxt));
660 if (string != NULL)
661 ret->string = xmlStrdup(string);
662 ret->cur = ret->string;
663 ret->neg = 0;
664 ret->error = 0;
Daniel Veillarde19fc232002-04-22 16:01:24 +0000665 ret->determinist = -1;
Daniel Veillard4255d502002-04-16 15:50:10 +0000666 return(ret);
667}
668
669/**
670 * xmlRegNewRange:
671 * @ctxt: the regexp parser context
672 * @neg: is that negative
673 * @type: the type of range
674 * @start: the start codepoint
675 * @end: the end codepoint
676 *
677 * Allocate a new regexp range
678 *
679 * Returns the new range or NULL in case of error
680 */
681static xmlRegRangePtr
682xmlRegNewRange(xmlRegParserCtxtPtr ctxt,
683 int neg, xmlRegAtomType type, int start, int end) {
684 xmlRegRangePtr ret;
685
686 ret = (xmlRegRangePtr) xmlMalloc(sizeof(xmlRegRange));
687 if (ret == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +0000688 xmlRegexpErrMemory(ctxt, "allocating range");
Daniel Veillard4255d502002-04-16 15:50:10 +0000689 return(NULL);
690 }
691 ret->neg = neg;
692 ret->type = type;
693 ret->start = start;
694 ret->end = end;
695 return(ret);
696}
697
698/**
699 * xmlRegFreeRange:
700 * @range: the regexp range
701 *
702 * Free a regexp range
703 */
704static void
705xmlRegFreeRange(xmlRegRangePtr range) {
706 if (range == NULL)
707 return;
708
709 if (range->blockName != NULL)
710 xmlFree(range->blockName);
711 xmlFree(range);
712}
713
714/**
715 * xmlRegNewAtom:
716 * @ctxt: the regexp parser context
717 * @type: the type of atom
718 *
719 * Allocate a new regexp range
720 *
721 * Returns the new atom or NULL in case of error
722 */
723static xmlRegAtomPtr
724xmlRegNewAtom(xmlRegParserCtxtPtr ctxt, xmlRegAtomType type) {
725 xmlRegAtomPtr ret;
726
727 ret = (xmlRegAtomPtr) xmlMalloc(sizeof(xmlRegAtom));
728 if (ret == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +0000729 xmlRegexpErrMemory(ctxt, "allocating atom");
Daniel Veillard4255d502002-04-16 15:50:10 +0000730 return(NULL);
731 }
732 memset(ret, 0, sizeof(xmlRegAtom));
733 ret->type = type;
734 ret->quant = XML_REGEXP_QUANT_ONCE;
735 ret->min = 0;
736 ret->max = 0;
737 return(ret);
738}
739
740/**
741 * xmlRegFreeAtom:
742 * @atom: the regexp atom
743 *
744 * Free a regexp atom
745 */
746static void
747xmlRegFreeAtom(xmlRegAtomPtr atom) {
748 int i;
749
750 if (atom == NULL)
751 return;
752
753 for (i = 0;i < atom->nbRanges;i++)
754 xmlRegFreeRange(atom->ranges[i]);
755 if (atom->ranges != NULL)
756 xmlFree(atom->ranges);
Daniel Veillardde0e4982005-07-03 14:35:44 +0000757 if ((atom->type == XML_REGEXP_STRING) && (atom->valuep != NULL))
758 xmlFree(atom->valuep);
Daniel Veillard77005e62005-07-19 16:26:18 +0000759 if ((atom->type == XML_REGEXP_STRING) && (atom->valuep2 != NULL))
760 xmlFree(atom->valuep2);
Daniel Veillardde0e4982005-07-03 14:35:44 +0000761 if ((atom->type == XML_REGEXP_BLOCK_NAME) && (atom->valuep != NULL))
Daniel Veillard4255d502002-04-16 15:50:10 +0000762 xmlFree(atom->valuep);
763 xmlFree(atom);
764}
765
766static xmlRegStatePtr
767xmlRegNewState(xmlRegParserCtxtPtr ctxt) {
768 xmlRegStatePtr ret;
769
770 ret = (xmlRegStatePtr) xmlMalloc(sizeof(xmlRegState));
771 if (ret == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +0000772 xmlRegexpErrMemory(ctxt, "allocating state");
Daniel Veillard4255d502002-04-16 15:50:10 +0000773 return(NULL);
774 }
775 memset(ret, 0, sizeof(xmlRegState));
776 ret->type = XML_REGEXP_TRANS_STATE;
777 ret->mark = XML_REGEXP_MARK_NORMAL;
778 return(ret);
779}
780
781/**
782 * xmlRegFreeState:
783 * @state: the regexp state
784 *
785 * Free a regexp state
786 */
787static void
788xmlRegFreeState(xmlRegStatePtr state) {
789 if (state == NULL)
790 return;
791
792 if (state->trans != NULL)
793 xmlFree(state->trans);
Daniel Veillarddb68b742005-07-30 13:18:24 +0000794 if (state->transTo != NULL)
795 xmlFree(state->transTo);
Daniel Veillard4255d502002-04-16 15:50:10 +0000796 xmlFree(state);
797}
798
799/**
800 * xmlRegFreeParserCtxt:
801 * @ctxt: the regexp parser context
802 *
803 * Free a regexp parser context
804 */
805static void
806xmlRegFreeParserCtxt(xmlRegParserCtxtPtr ctxt) {
807 int i;
808 if (ctxt == NULL)
809 return;
810
811 if (ctxt->string != NULL)
812 xmlFree(ctxt->string);
813 if (ctxt->states != NULL) {
814 for (i = 0;i < ctxt->nbStates;i++)
815 xmlRegFreeState(ctxt->states[i]);
816 xmlFree(ctxt->states);
817 }
818 if (ctxt->atoms != NULL) {
819 for (i = 0;i < ctxt->nbAtoms;i++)
820 xmlRegFreeAtom(ctxt->atoms[i]);
821 xmlFree(ctxt->atoms);
822 }
823 if (ctxt->counters != NULL)
824 xmlFree(ctxt->counters);
825 xmlFree(ctxt);
826}
827
828/************************************************************************
829 * *
830 * Display of Data structures *
831 * *
832 ************************************************************************/
833
834static void
835xmlRegPrintAtomType(FILE *output, xmlRegAtomType type) {
836 switch (type) {
837 case XML_REGEXP_EPSILON:
838 fprintf(output, "epsilon "); break;
839 case XML_REGEXP_CHARVAL:
840 fprintf(output, "charval "); break;
841 case XML_REGEXP_RANGES:
842 fprintf(output, "ranges "); break;
843 case XML_REGEXP_SUBREG:
844 fprintf(output, "subexpr "); break;
845 case XML_REGEXP_STRING:
846 fprintf(output, "string "); break;
847 case XML_REGEXP_ANYCHAR:
848 fprintf(output, "anychar "); break;
849 case XML_REGEXP_ANYSPACE:
850 fprintf(output, "anyspace "); break;
851 case XML_REGEXP_NOTSPACE:
852 fprintf(output, "notspace "); break;
853 case XML_REGEXP_INITNAME:
854 fprintf(output, "initname "); break;
855 case XML_REGEXP_NOTINITNAME:
856 fprintf(output, "notinitname "); break;
857 case XML_REGEXP_NAMECHAR:
858 fprintf(output, "namechar "); break;
859 case XML_REGEXP_NOTNAMECHAR:
860 fprintf(output, "notnamechar "); break;
861 case XML_REGEXP_DECIMAL:
862 fprintf(output, "decimal "); break;
863 case XML_REGEXP_NOTDECIMAL:
864 fprintf(output, "notdecimal "); break;
865 case XML_REGEXP_REALCHAR:
866 fprintf(output, "realchar "); break;
867 case XML_REGEXP_NOTREALCHAR:
868 fprintf(output, "notrealchar "); break;
869 case XML_REGEXP_LETTER:
870 fprintf(output, "LETTER "); break;
871 case XML_REGEXP_LETTER_UPPERCASE:
872 fprintf(output, "LETTER_UPPERCASE "); break;
873 case XML_REGEXP_LETTER_LOWERCASE:
874 fprintf(output, "LETTER_LOWERCASE "); break;
875 case XML_REGEXP_LETTER_TITLECASE:
876 fprintf(output, "LETTER_TITLECASE "); break;
877 case XML_REGEXP_LETTER_MODIFIER:
878 fprintf(output, "LETTER_MODIFIER "); break;
879 case XML_REGEXP_LETTER_OTHERS:
880 fprintf(output, "LETTER_OTHERS "); break;
881 case XML_REGEXP_MARK:
882 fprintf(output, "MARK "); break;
883 case XML_REGEXP_MARK_NONSPACING:
884 fprintf(output, "MARK_NONSPACING "); break;
885 case XML_REGEXP_MARK_SPACECOMBINING:
886 fprintf(output, "MARK_SPACECOMBINING "); break;
887 case XML_REGEXP_MARK_ENCLOSING:
888 fprintf(output, "MARK_ENCLOSING "); break;
889 case XML_REGEXP_NUMBER:
890 fprintf(output, "NUMBER "); break;
891 case XML_REGEXP_NUMBER_DECIMAL:
892 fprintf(output, "NUMBER_DECIMAL "); break;
893 case XML_REGEXP_NUMBER_LETTER:
894 fprintf(output, "NUMBER_LETTER "); break;
895 case XML_REGEXP_NUMBER_OTHERS:
896 fprintf(output, "NUMBER_OTHERS "); break;
897 case XML_REGEXP_PUNCT:
898 fprintf(output, "PUNCT "); break;
899 case XML_REGEXP_PUNCT_CONNECTOR:
900 fprintf(output, "PUNCT_CONNECTOR "); break;
901 case XML_REGEXP_PUNCT_DASH:
902 fprintf(output, "PUNCT_DASH "); break;
903 case XML_REGEXP_PUNCT_OPEN:
904 fprintf(output, "PUNCT_OPEN "); break;
905 case XML_REGEXP_PUNCT_CLOSE:
906 fprintf(output, "PUNCT_CLOSE "); break;
907 case XML_REGEXP_PUNCT_INITQUOTE:
908 fprintf(output, "PUNCT_INITQUOTE "); break;
909 case XML_REGEXP_PUNCT_FINQUOTE:
910 fprintf(output, "PUNCT_FINQUOTE "); break;
911 case XML_REGEXP_PUNCT_OTHERS:
912 fprintf(output, "PUNCT_OTHERS "); break;
913 case XML_REGEXP_SEPAR:
914 fprintf(output, "SEPAR "); break;
915 case XML_REGEXP_SEPAR_SPACE:
916 fprintf(output, "SEPAR_SPACE "); break;
917 case XML_REGEXP_SEPAR_LINE:
918 fprintf(output, "SEPAR_LINE "); break;
919 case XML_REGEXP_SEPAR_PARA:
920 fprintf(output, "SEPAR_PARA "); break;
921 case XML_REGEXP_SYMBOL:
922 fprintf(output, "SYMBOL "); break;
923 case XML_REGEXP_SYMBOL_MATH:
924 fprintf(output, "SYMBOL_MATH "); break;
925 case XML_REGEXP_SYMBOL_CURRENCY:
926 fprintf(output, "SYMBOL_CURRENCY "); break;
927 case XML_REGEXP_SYMBOL_MODIFIER:
928 fprintf(output, "SYMBOL_MODIFIER "); break;
929 case XML_REGEXP_SYMBOL_OTHERS:
930 fprintf(output, "SYMBOL_OTHERS "); break;
931 case XML_REGEXP_OTHER:
932 fprintf(output, "OTHER "); break;
933 case XML_REGEXP_OTHER_CONTROL:
934 fprintf(output, "OTHER_CONTROL "); break;
935 case XML_REGEXP_OTHER_FORMAT:
936 fprintf(output, "OTHER_FORMAT "); break;
937 case XML_REGEXP_OTHER_PRIVATE:
938 fprintf(output, "OTHER_PRIVATE "); break;
939 case XML_REGEXP_OTHER_NA:
940 fprintf(output, "OTHER_NA "); break;
941 case XML_REGEXP_BLOCK_NAME:
942 fprintf(output, "BLOCK "); break;
943 }
944}
945
946static void
947xmlRegPrintQuantType(FILE *output, xmlRegQuantType type) {
948 switch (type) {
949 case XML_REGEXP_QUANT_EPSILON:
950 fprintf(output, "epsilon "); break;
951 case XML_REGEXP_QUANT_ONCE:
952 fprintf(output, "once "); break;
953 case XML_REGEXP_QUANT_OPT:
954 fprintf(output, "? "); break;
955 case XML_REGEXP_QUANT_MULT:
956 fprintf(output, "* "); break;
957 case XML_REGEXP_QUANT_PLUS:
958 fprintf(output, "+ "); break;
959 case XML_REGEXP_QUANT_RANGE:
960 fprintf(output, "range "); break;
Daniel Veillard7646b182002-04-20 06:41:40 +0000961 case XML_REGEXP_QUANT_ONCEONLY:
962 fprintf(output, "onceonly "); break;
963 case XML_REGEXP_QUANT_ALL:
964 fprintf(output, "all "); break;
Daniel Veillard4255d502002-04-16 15:50:10 +0000965 }
966}
967static void
968xmlRegPrintRange(FILE *output, xmlRegRangePtr range) {
969 fprintf(output, " range: ");
970 if (range->neg)
971 fprintf(output, "negative ");
972 xmlRegPrintAtomType(output, range->type);
973 fprintf(output, "%c - %c\n", range->start, range->end);
974}
975
976static void
977xmlRegPrintAtom(FILE *output, xmlRegAtomPtr atom) {
978 fprintf(output, " atom: ");
979 if (atom == NULL) {
980 fprintf(output, "NULL\n");
981 return;
982 }
Daniel Veillard9efc4762005-07-19 14:33:55 +0000983 if (atom->neg)
984 fprintf(output, "not ");
Daniel Veillard4255d502002-04-16 15:50:10 +0000985 xmlRegPrintAtomType(output, atom->type);
986 xmlRegPrintQuantType(output, atom->quant);
987 if (atom->quant == XML_REGEXP_QUANT_RANGE)
988 fprintf(output, "%d-%d ", atom->min, atom->max);
989 if (atom->type == XML_REGEXP_STRING)
990 fprintf(output, "'%s' ", (char *) atom->valuep);
991 if (atom->type == XML_REGEXP_CHARVAL)
992 fprintf(output, "char %c\n", atom->codepoint);
993 else if (atom->type == XML_REGEXP_RANGES) {
994 int i;
995 fprintf(output, "%d entries\n", atom->nbRanges);
996 for (i = 0; i < atom->nbRanges;i++)
997 xmlRegPrintRange(output, atom->ranges[i]);
998 } else if (atom->type == XML_REGEXP_SUBREG) {
999 fprintf(output, "start %d end %d\n", atom->start->no, atom->stop->no);
1000 } else {
1001 fprintf(output, "\n");
1002 }
1003}
1004
1005static void
1006xmlRegPrintTrans(FILE *output, xmlRegTransPtr trans) {
1007 fprintf(output, " trans: ");
1008 if (trans == NULL) {
1009 fprintf(output, "NULL\n");
1010 return;
1011 }
1012 if (trans->to < 0) {
1013 fprintf(output, "removed\n");
1014 return;
1015 }
1016 if (trans->counter >= 0) {
1017 fprintf(output, "counted %d, ", trans->counter);
1018 }
Daniel Veillard8a001f62002-04-20 07:24:11 +00001019 if (trans->count == REGEXP_ALL_COUNTER) {
1020 fprintf(output, "all transition, ");
1021 } else if (trans->count >= 0) {
Daniel Veillard4255d502002-04-16 15:50:10 +00001022 fprintf(output, "count based %d, ", trans->count);
1023 }
1024 if (trans->atom == NULL) {
1025 fprintf(output, "epsilon to %d\n", trans->to);
1026 return;
1027 }
1028 if (trans->atom->type == XML_REGEXP_CHARVAL)
1029 fprintf(output, "char %c ", trans->atom->codepoint);
1030 fprintf(output, "atom %d, to %d\n", trans->atom->no, trans->to);
1031}
1032
1033static void
1034xmlRegPrintState(FILE *output, xmlRegStatePtr state) {
1035 int i;
1036
1037 fprintf(output, " state: ");
1038 if (state == NULL) {
1039 fprintf(output, "NULL\n");
1040 return;
1041 }
1042 if (state->type == XML_REGEXP_START_STATE)
1043 fprintf(output, "START ");
1044 if (state->type == XML_REGEXP_FINAL_STATE)
1045 fprintf(output, "FINAL ");
1046
1047 fprintf(output, "%d, %d transitions:\n", state->no, state->nbTrans);
1048 for (i = 0;i < state->nbTrans; i++) {
1049 xmlRegPrintTrans(output, &(state->trans[i]));
1050 }
1051}
1052
Daniel Veillard23e73572002-09-19 19:56:43 +00001053#ifdef DEBUG_REGEXP_GRAPH
Daniel Veillard4255d502002-04-16 15:50:10 +00001054static void
1055xmlRegPrintCtxt(FILE *output, xmlRegParserCtxtPtr ctxt) {
1056 int i;
1057
1058 fprintf(output, " ctxt: ");
1059 if (ctxt == NULL) {
1060 fprintf(output, "NULL\n");
1061 return;
1062 }
1063 fprintf(output, "'%s' ", ctxt->string);
1064 if (ctxt->error)
1065 fprintf(output, "error ");
1066 if (ctxt->neg)
1067 fprintf(output, "neg ");
1068 fprintf(output, "\n");
1069 fprintf(output, "%d atoms:\n", ctxt->nbAtoms);
1070 for (i = 0;i < ctxt->nbAtoms; i++) {
1071 fprintf(output, " %02d ", i);
1072 xmlRegPrintAtom(output, ctxt->atoms[i]);
1073 }
1074 if (ctxt->atom != NULL) {
1075 fprintf(output, "current atom:\n");
1076 xmlRegPrintAtom(output, ctxt->atom);
1077 }
1078 fprintf(output, "%d states:", ctxt->nbStates);
1079 if (ctxt->start != NULL)
1080 fprintf(output, " start: %d", ctxt->start->no);
1081 if (ctxt->end != NULL)
1082 fprintf(output, " end: %d", ctxt->end->no);
1083 fprintf(output, "\n");
1084 for (i = 0;i < ctxt->nbStates; i++) {
1085 xmlRegPrintState(output, ctxt->states[i]);
1086 }
1087 fprintf(output, "%d counters:\n", ctxt->nbCounters);
1088 for (i = 0;i < ctxt->nbCounters; i++) {
1089 fprintf(output, " %d: min %d max %d\n", i, ctxt->counters[i].min,
1090 ctxt->counters[i].max);
1091 }
1092}
Daniel Veillard23e73572002-09-19 19:56:43 +00001093#endif
Daniel Veillard4255d502002-04-16 15:50:10 +00001094
1095/************************************************************************
1096 * *
1097 * Finite Automata structures manipulations *
1098 * *
1099 ************************************************************************/
1100
1101static void
1102xmlRegAtomAddRange(xmlRegParserCtxtPtr ctxt, xmlRegAtomPtr atom,
1103 int neg, xmlRegAtomType type, int start, int end,
1104 xmlChar *blockName) {
1105 xmlRegRangePtr range;
1106
1107 if (atom == NULL) {
1108 ERROR("add range: atom is NULL");
1109 return;
1110 }
1111 if (atom->type != XML_REGEXP_RANGES) {
1112 ERROR("add range: atom is not ranges");
1113 return;
1114 }
1115 if (atom->maxRanges == 0) {
1116 atom->maxRanges = 4;
1117 atom->ranges = (xmlRegRangePtr *) xmlMalloc(atom->maxRanges *
1118 sizeof(xmlRegRangePtr));
1119 if (atom->ranges == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00001120 xmlRegexpErrMemory(ctxt, "adding ranges");
Daniel Veillard4255d502002-04-16 15:50:10 +00001121 atom->maxRanges = 0;
1122 return;
1123 }
1124 } else if (atom->nbRanges >= atom->maxRanges) {
1125 xmlRegRangePtr *tmp;
1126 atom->maxRanges *= 2;
1127 tmp = (xmlRegRangePtr *) xmlRealloc(atom->ranges, atom->maxRanges *
1128 sizeof(xmlRegRangePtr));
1129 if (tmp == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00001130 xmlRegexpErrMemory(ctxt, "adding ranges");
Daniel Veillard4255d502002-04-16 15:50:10 +00001131 atom->maxRanges /= 2;
1132 return;
1133 }
1134 atom->ranges = tmp;
1135 }
1136 range = xmlRegNewRange(ctxt, neg, type, start, end);
1137 if (range == NULL)
1138 return;
1139 range->blockName = blockName;
1140 atom->ranges[atom->nbRanges++] = range;
1141
1142}
1143
1144static int
1145xmlRegGetCounter(xmlRegParserCtxtPtr ctxt) {
1146 if (ctxt->maxCounters == 0) {
1147 ctxt->maxCounters = 4;
1148 ctxt->counters = (xmlRegCounter *) xmlMalloc(ctxt->maxCounters *
1149 sizeof(xmlRegCounter));
1150 if (ctxt->counters == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00001151 xmlRegexpErrMemory(ctxt, "allocating counter");
Daniel Veillard4255d502002-04-16 15:50:10 +00001152 ctxt->maxCounters = 0;
1153 return(-1);
1154 }
1155 } else if (ctxt->nbCounters >= ctxt->maxCounters) {
1156 xmlRegCounter *tmp;
1157 ctxt->maxCounters *= 2;
1158 tmp = (xmlRegCounter *) xmlRealloc(ctxt->counters, ctxt->maxCounters *
1159 sizeof(xmlRegCounter));
1160 if (tmp == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00001161 xmlRegexpErrMemory(ctxt, "allocating counter");
Daniel Veillard4255d502002-04-16 15:50:10 +00001162 ctxt->maxCounters /= 2;
1163 return(-1);
1164 }
1165 ctxt->counters = tmp;
1166 }
1167 ctxt->counters[ctxt->nbCounters].min = -1;
1168 ctxt->counters[ctxt->nbCounters].max = -1;
1169 return(ctxt->nbCounters++);
1170}
1171
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001172static int
Daniel Veillard4255d502002-04-16 15:50:10 +00001173xmlRegAtomPush(xmlRegParserCtxtPtr ctxt, xmlRegAtomPtr atom) {
1174 if (atom == NULL) {
1175 ERROR("atom push: atom is NULL");
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001176 return(-1);
Daniel Veillard4255d502002-04-16 15:50:10 +00001177 }
1178 if (ctxt->maxAtoms == 0) {
1179 ctxt->maxAtoms = 4;
1180 ctxt->atoms = (xmlRegAtomPtr *) xmlMalloc(ctxt->maxAtoms *
1181 sizeof(xmlRegAtomPtr));
1182 if (ctxt->atoms == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00001183 xmlRegexpErrMemory(ctxt, "pushing atom");
Daniel Veillard4255d502002-04-16 15:50:10 +00001184 ctxt->maxAtoms = 0;
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001185 return(-1);
Daniel Veillard4255d502002-04-16 15:50:10 +00001186 }
1187 } else if (ctxt->nbAtoms >= ctxt->maxAtoms) {
1188 xmlRegAtomPtr *tmp;
1189 ctxt->maxAtoms *= 2;
1190 tmp = (xmlRegAtomPtr *) xmlRealloc(ctxt->atoms, ctxt->maxAtoms *
1191 sizeof(xmlRegAtomPtr));
1192 if (tmp == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00001193 xmlRegexpErrMemory(ctxt, "allocating counter");
Daniel Veillard4255d502002-04-16 15:50:10 +00001194 ctxt->maxAtoms /= 2;
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001195 return(-1);
Daniel Veillard4255d502002-04-16 15:50:10 +00001196 }
1197 ctxt->atoms = tmp;
1198 }
1199 atom->no = ctxt->nbAtoms;
1200 ctxt->atoms[ctxt->nbAtoms++] = atom;
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001201 return(0);
Daniel Veillard4255d502002-04-16 15:50:10 +00001202}
1203
1204static void
Daniel Veillarddb68b742005-07-30 13:18:24 +00001205xmlRegStateAddTransTo(xmlRegParserCtxtPtr ctxt, xmlRegStatePtr target,
1206 int from) {
1207 if (target->maxTransTo == 0) {
1208 target->maxTransTo = 8;
1209 target->transTo = (int *) xmlMalloc(target->maxTransTo *
1210 sizeof(int));
1211 if (target->transTo == NULL) {
1212 xmlRegexpErrMemory(ctxt, "adding transition");
1213 target->maxTransTo = 0;
1214 return;
1215 }
1216 } else if (target->nbTransTo >= target->maxTransTo) {
1217 int *tmp;
1218 target->maxTransTo *= 2;
1219 tmp = (int *) xmlRealloc(target->transTo, target->maxTransTo *
1220 sizeof(int));
1221 if (tmp == NULL) {
1222 xmlRegexpErrMemory(ctxt, "adding transition");
1223 target->maxTransTo /= 2;
1224 return;
1225 }
1226 target->transTo = tmp;
1227 }
1228 target->transTo[target->nbTransTo] = from;
1229 target->nbTransTo++;
1230}
1231
1232static void
Daniel Veillard4255d502002-04-16 15:50:10 +00001233xmlRegStateAddTrans(xmlRegParserCtxtPtr ctxt, xmlRegStatePtr state,
1234 xmlRegAtomPtr atom, xmlRegStatePtr target,
Daniel Veillarddb68b742005-07-30 13:18:24 +00001235 int counter, int count, int nchk) {
William M. Brackf9b5fa22004-05-10 07:52:15 +00001236
1237 int nrtrans;
1238
Daniel Veillard4255d502002-04-16 15:50:10 +00001239 if (state == NULL) {
1240 ERROR("add state: state is NULL");
1241 return;
1242 }
1243 if (target == NULL) {
1244 ERROR("add state: target is NULL");
1245 return;
1246 }
William M. Brackf9b5fa22004-05-10 07:52:15 +00001247 /*
1248 * Other routines follow the philosophy 'When in doubt, add a transition'
1249 * so we check here whether such a transition is already present and, if
1250 * so, silently ignore this request.
1251 */
1252
Daniel Veillarddb68b742005-07-30 13:18:24 +00001253 if (nchk == 0) {
1254 for (nrtrans = state->nbTrans - 1; nrtrans >= 0; nrtrans--) {
1255 xmlRegTransPtr trans = &(state->trans[nrtrans]);
1256 if ((trans->atom == atom) &&
1257 (trans->to == target->no) &&
1258 (trans->counter == counter) &&
1259 (trans->count == count)) {
William M. Brackf9b5fa22004-05-10 07:52:15 +00001260#ifdef DEBUG_REGEXP_GRAPH
Daniel Veillarddb68b742005-07-30 13:18:24 +00001261 printf("Ignoring duplicate transition from %d to %d\n",
1262 state->no, target->no);
William M. Brackf9b5fa22004-05-10 07:52:15 +00001263#endif
Daniel Veillarddb68b742005-07-30 13:18:24 +00001264 return;
1265 }
1266 }
William M. Brackf9b5fa22004-05-10 07:52:15 +00001267 }
1268
Daniel Veillard4255d502002-04-16 15:50:10 +00001269 if (state->maxTrans == 0) {
Daniel Veillarddb68b742005-07-30 13:18:24 +00001270 state->maxTrans = 8;
Daniel Veillard4255d502002-04-16 15:50:10 +00001271 state->trans = (xmlRegTrans *) xmlMalloc(state->maxTrans *
1272 sizeof(xmlRegTrans));
1273 if (state->trans == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00001274 xmlRegexpErrMemory(ctxt, "adding transition");
Daniel Veillard4255d502002-04-16 15:50:10 +00001275 state->maxTrans = 0;
1276 return;
1277 }
1278 } else if (state->nbTrans >= state->maxTrans) {
1279 xmlRegTrans *tmp;
1280 state->maxTrans *= 2;
1281 tmp = (xmlRegTrans *) xmlRealloc(state->trans, state->maxTrans *
1282 sizeof(xmlRegTrans));
1283 if (tmp == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00001284 xmlRegexpErrMemory(ctxt, "adding transition");
Daniel Veillard4255d502002-04-16 15:50:10 +00001285 state->maxTrans /= 2;
1286 return;
1287 }
1288 state->trans = tmp;
1289 }
1290#ifdef DEBUG_REGEXP_GRAPH
1291 printf("Add trans from %d to %d ", state->no, target->no);
Daniel Veillard8a001f62002-04-20 07:24:11 +00001292 if (count == REGEXP_ALL_COUNTER)
Daniel Veillard2cbf5962004-03-31 15:50:43 +00001293 printf("all transition\n");
Daniel Veillard4402ab42002-09-12 16:02:56 +00001294 else if (count >= 0)
Daniel Veillard2cbf5962004-03-31 15:50:43 +00001295 printf("count based %d\n", count);
Daniel Veillard4255d502002-04-16 15:50:10 +00001296 else if (counter >= 0)
Daniel Veillard2cbf5962004-03-31 15:50:43 +00001297 printf("counted %d\n", counter);
Daniel Veillard4255d502002-04-16 15:50:10 +00001298 else if (atom == NULL)
Daniel Veillard2cbf5962004-03-31 15:50:43 +00001299 printf("epsilon transition\n");
1300 else if (atom != NULL)
1301 xmlRegPrintAtom(stdout, atom);
Daniel Veillard4255d502002-04-16 15:50:10 +00001302#endif
1303
1304 state->trans[state->nbTrans].atom = atom;
1305 state->trans[state->nbTrans].to = target->no;
1306 state->trans[state->nbTrans].counter = counter;
1307 state->trans[state->nbTrans].count = count;
1308 state->nbTrans++;
Daniel Veillarddb68b742005-07-30 13:18:24 +00001309 xmlRegStateAddTransTo(ctxt, target, state->no);
Daniel Veillard4255d502002-04-16 15:50:10 +00001310}
1311
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001312static int
Daniel Veillard4255d502002-04-16 15:50:10 +00001313xmlRegStatePush(xmlRegParserCtxtPtr ctxt, xmlRegStatePtr state) {
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001314 if (state == NULL) return(-1);
Daniel Veillard4255d502002-04-16 15:50:10 +00001315 if (ctxt->maxStates == 0) {
1316 ctxt->maxStates = 4;
1317 ctxt->states = (xmlRegStatePtr *) xmlMalloc(ctxt->maxStates *
1318 sizeof(xmlRegStatePtr));
1319 if (ctxt->states == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00001320 xmlRegexpErrMemory(ctxt, "adding state");
Daniel Veillard4255d502002-04-16 15:50:10 +00001321 ctxt->maxStates = 0;
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001322 return(-1);
Daniel Veillard4255d502002-04-16 15:50:10 +00001323 }
1324 } else if (ctxt->nbStates >= ctxt->maxStates) {
1325 xmlRegStatePtr *tmp;
1326 ctxt->maxStates *= 2;
1327 tmp = (xmlRegStatePtr *) xmlRealloc(ctxt->states, ctxt->maxStates *
1328 sizeof(xmlRegStatePtr));
1329 if (tmp == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00001330 xmlRegexpErrMemory(ctxt, "adding state");
Daniel Veillard4255d502002-04-16 15:50:10 +00001331 ctxt->maxStates /= 2;
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001332 return(-1);
Daniel Veillard4255d502002-04-16 15:50:10 +00001333 }
1334 ctxt->states = tmp;
1335 }
1336 state->no = ctxt->nbStates;
1337 ctxt->states[ctxt->nbStates++] = state;
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001338 return(0);
Daniel Veillard4255d502002-04-16 15:50:10 +00001339}
1340
1341/**
Daniel Veillard7646b182002-04-20 06:41:40 +00001342 * xmlFAGenerateAllTransition:
Daniel Veillard441bc322002-04-20 17:38:48 +00001343 * @ctxt: a regexp parser context
1344 * @from: the from state
1345 * @to: the target state or NULL for building a new one
1346 * @lax:
Daniel Veillard7646b182002-04-20 06:41:40 +00001347 *
1348 */
1349static void
1350xmlFAGenerateAllTransition(xmlRegParserCtxtPtr ctxt,
Daniel Veillard441bc322002-04-20 17:38:48 +00001351 xmlRegStatePtr from, xmlRegStatePtr to,
1352 int lax) {
Daniel Veillard7646b182002-04-20 06:41:40 +00001353 if (to == NULL) {
1354 to = xmlRegNewState(ctxt);
1355 xmlRegStatePush(ctxt, to);
1356 ctxt->state = to;
1357 }
Daniel Veillard441bc322002-04-20 17:38:48 +00001358 if (lax)
Daniel Veillarddb68b742005-07-30 13:18:24 +00001359 xmlRegStateAddTrans(ctxt, from, NULL, to, -1, REGEXP_ALL_LAX_COUNTER, 0);
Daniel Veillard441bc322002-04-20 17:38:48 +00001360 else
Daniel Veillarddb68b742005-07-30 13:18:24 +00001361 xmlRegStateAddTrans(ctxt, from, NULL, to, -1, REGEXP_ALL_COUNTER, 0);
Daniel Veillard7646b182002-04-20 06:41:40 +00001362}
1363
1364/**
Daniel Veillard4255d502002-04-16 15:50:10 +00001365 * xmlFAGenerateEpsilonTransition:
Daniel Veillard441bc322002-04-20 17:38:48 +00001366 * @ctxt: a regexp parser context
1367 * @from: the from state
1368 * @to: the target state or NULL for building a new one
Daniel Veillard4255d502002-04-16 15:50:10 +00001369 *
1370 */
1371static void
1372xmlFAGenerateEpsilonTransition(xmlRegParserCtxtPtr ctxt,
1373 xmlRegStatePtr from, xmlRegStatePtr to) {
1374 if (to == NULL) {
1375 to = xmlRegNewState(ctxt);
1376 xmlRegStatePush(ctxt, to);
1377 ctxt->state = to;
1378 }
Daniel Veillarddb68b742005-07-30 13:18:24 +00001379 xmlRegStateAddTrans(ctxt, from, NULL, to, -1, -1, 0);
Daniel Veillard4255d502002-04-16 15:50:10 +00001380}
1381
1382/**
1383 * xmlFAGenerateCountedEpsilonTransition:
Daniel Veillard441bc322002-04-20 17:38:48 +00001384 * @ctxt: a regexp parser context
1385 * @from: the from state
1386 * @to: the target state or NULL for building a new one
Daniel Veillard4255d502002-04-16 15:50:10 +00001387 * counter: the counter for that transition
1388 *
1389 */
1390static void
1391xmlFAGenerateCountedEpsilonTransition(xmlRegParserCtxtPtr ctxt,
1392 xmlRegStatePtr from, xmlRegStatePtr to, int counter) {
1393 if (to == NULL) {
1394 to = xmlRegNewState(ctxt);
1395 xmlRegStatePush(ctxt, to);
1396 ctxt->state = to;
1397 }
Daniel Veillarddb68b742005-07-30 13:18:24 +00001398 xmlRegStateAddTrans(ctxt, from, NULL, to, counter, -1, 0);
Daniel Veillard4255d502002-04-16 15:50:10 +00001399}
1400
1401/**
1402 * xmlFAGenerateCountedTransition:
Daniel Veillard441bc322002-04-20 17:38:48 +00001403 * @ctxt: a regexp parser context
1404 * @from: the from state
1405 * @to: the target state or NULL for building a new one
Daniel Veillard4255d502002-04-16 15:50:10 +00001406 * counter: the counter for that transition
1407 *
1408 */
1409static void
1410xmlFAGenerateCountedTransition(xmlRegParserCtxtPtr ctxt,
1411 xmlRegStatePtr from, xmlRegStatePtr to, int counter) {
1412 if (to == NULL) {
1413 to = xmlRegNewState(ctxt);
1414 xmlRegStatePush(ctxt, to);
1415 ctxt->state = to;
1416 }
Daniel Veillarddb68b742005-07-30 13:18:24 +00001417 xmlRegStateAddTrans(ctxt, from, NULL, to, -1, counter, 0);
Daniel Veillard4255d502002-04-16 15:50:10 +00001418}
1419
1420/**
1421 * xmlFAGenerateTransitions:
Daniel Veillard441bc322002-04-20 17:38:48 +00001422 * @ctxt: a regexp parser context
1423 * @from: the from state
1424 * @to: the target state or NULL for building a new one
1425 * @atom: the atom generating the transition
Daniel Veillard4255d502002-04-16 15:50:10 +00001426 *
William M. Brackddf71d62004-05-06 04:17:26 +00001427 * Returns 0 if success and -1 in case of error.
Daniel Veillard4255d502002-04-16 15:50:10 +00001428 */
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001429static int
Daniel Veillard4255d502002-04-16 15:50:10 +00001430xmlFAGenerateTransitions(xmlRegParserCtxtPtr ctxt, xmlRegStatePtr from,
1431 xmlRegStatePtr to, xmlRegAtomPtr atom) {
1432 if (atom == NULL) {
1433 ERROR("genrate transition: atom == NULL");
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001434 return(-1);
Daniel Veillard4255d502002-04-16 15:50:10 +00001435 }
1436 if (atom->type == XML_REGEXP_SUBREG) {
1437 /*
1438 * this is a subexpression handling one should not need to
William M. Brackddf71d62004-05-06 04:17:26 +00001439 * create a new node except for XML_REGEXP_QUANT_RANGE.
Daniel Veillard4255d502002-04-16 15:50:10 +00001440 */
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001441 if (xmlRegAtomPush(ctxt, atom) < 0) {
1442 return(-1);
1443 }
Daniel Veillard4255d502002-04-16 15:50:10 +00001444 if ((to != NULL) && (atom->stop != to) &&
1445 (atom->quant != XML_REGEXP_QUANT_RANGE)) {
1446 /*
1447 * Generate an epsilon transition to link to the target
1448 */
1449 xmlFAGenerateEpsilonTransition(ctxt, atom->stop, to);
1450 }
1451 switch (atom->quant) {
1452 case XML_REGEXP_QUANT_OPT:
1453 atom->quant = XML_REGEXP_QUANT_ONCE;
1454 xmlFAGenerateEpsilonTransition(ctxt, atom->start, atom->stop);
1455 break;
1456 case XML_REGEXP_QUANT_MULT:
1457 atom->quant = XML_REGEXP_QUANT_ONCE;
1458 xmlFAGenerateEpsilonTransition(ctxt, atom->start, atom->stop);
1459 xmlFAGenerateEpsilonTransition(ctxt, atom->stop, atom->start);
1460 break;
1461 case XML_REGEXP_QUANT_PLUS:
1462 atom->quant = XML_REGEXP_QUANT_ONCE;
1463 xmlFAGenerateEpsilonTransition(ctxt, atom->stop, atom->start);
1464 break;
1465 case XML_REGEXP_QUANT_RANGE: {
1466 int counter;
1467 xmlRegStatePtr newstate;
1468
1469 /*
1470 * This one is nasty:
William M. Brackddf71d62004-05-06 04:17:26 +00001471 * 1/ if range has minOccurs == 0, create a new state
1472 * and create epsilon transitions from atom->start
1473 * to atom->stop, as well as atom->start to the new
1474 * state
1475 * 2/ register a new counter
1476 * 3/ register an epsilon transition associated to
Daniel Veillard4255d502002-04-16 15:50:10 +00001477 * this counter going from atom->stop to atom->start
William M. Brackddf71d62004-05-06 04:17:26 +00001478 * 4/ create a new state
1479 * 5/ generate a counted transition from atom->stop to
Daniel Veillard4255d502002-04-16 15:50:10 +00001480 * that state
1481 */
William M. Brackddf71d62004-05-06 04:17:26 +00001482 if (atom->min == 0) {
1483 xmlFAGenerateEpsilonTransition(ctxt, atom->start,
1484 atom->stop);
1485 newstate = xmlRegNewState(ctxt);
1486 xmlRegStatePush(ctxt, newstate);
1487 ctxt->state = newstate;
1488 xmlFAGenerateEpsilonTransition(ctxt, atom->start,
1489 newstate);
1490 }
Daniel Veillard4255d502002-04-16 15:50:10 +00001491 counter = xmlRegGetCounter(ctxt);
1492 ctxt->counters[counter].min = atom->min - 1;
1493 ctxt->counters[counter].max = atom->max - 1;
1494 atom->min = 0;
1495 atom->max = 0;
1496 atom->quant = XML_REGEXP_QUANT_ONCE;
1497 xmlFAGenerateCountedEpsilonTransition(ctxt, atom->stop,
1498 atom->start, counter);
1499 if (to != NULL) {
1500 newstate = to;
1501 } else {
1502 newstate = xmlRegNewState(ctxt);
1503 xmlRegStatePush(ctxt, newstate);
1504 ctxt->state = newstate;
1505 }
1506 xmlFAGenerateCountedTransition(ctxt, atom->stop,
1507 newstate, counter);
1508 }
1509 default:
1510 break;
1511 }
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001512 return(0);
Daniel Veillard99c394d2005-07-14 12:58:49 +00001513 } else if ((atom->min == 0) && (atom->max == 0) &&
1514 (atom->quant == XML_REGEXP_QUANT_RANGE)) {
1515 /*
1516 * we can discard the atom and generate an epsilon transition instead
1517 */
1518 if (to == NULL) {
1519 to = xmlRegNewState(ctxt);
1520 if (to != NULL)
1521 xmlRegStatePush(ctxt, to);
1522 else {
1523 return(-1);
1524 }
1525 }
1526 xmlFAGenerateEpsilonTransition(ctxt, from, to);
1527 ctxt->state = to;
1528 xmlRegFreeAtom(atom);
1529 return(0);
Daniel Veillard4255d502002-04-16 15:50:10 +00001530 } else {
1531 if (to == NULL) {
1532 to = xmlRegNewState(ctxt);
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001533 if (to != NULL)
1534 xmlRegStatePush(ctxt, to);
1535 else {
1536 return(-1);
1537 }
1538 }
1539 if (xmlRegAtomPush(ctxt, atom) < 0) {
1540 return(-1);
Daniel Veillard4255d502002-04-16 15:50:10 +00001541 }
Daniel Veillarddb68b742005-07-30 13:18:24 +00001542 xmlRegStateAddTrans(ctxt, from, atom, to, -1, -1, 0);
Daniel Veillard4255d502002-04-16 15:50:10 +00001543 ctxt->state = to;
1544 }
1545 switch (atom->quant) {
1546 case XML_REGEXP_QUANT_OPT:
1547 atom->quant = XML_REGEXP_QUANT_ONCE;
1548 xmlFAGenerateEpsilonTransition(ctxt, from, to);
1549 break;
1550 case XML_REGEXP_QUANT_MULT:
1551 atom->quant = XML_REGEXP_QUANT_ONCE;
1552 xmlFAGenerateEpsilonTransition(ctxt, from, to);
Daniel Veillarddb68b742005-07-30 13:18:24 +00001553 xmlRegStateAddTrans(ctxt, to, atom, to, -1, -1, 0);
Daniel Veillard4255d502002-04-16 15:50:10 +00001554 break;
1555 case XML_REGEXP_QUANT_PLUS:
1556 atom->quant = XML_REGEXP_QUANT_ONCE;
Daniel Veillarddb68b742005-07-30 13:18:24 +00001557 xmlRegStateAddTrans(ctxt, to, atom, to, -1, -1, 0);
Daniel Veillard4255d502002-04-16 15:50:10 +00001558 break;
1559 default:
1560 break;
1561 }
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001562 return(0);
Daniel Veillard4255d502002-04-16 15:50:10 +00001563}
1564
1565/**
1566 * xmlFAReduceEpsilonTransitions:
Daniel Veillard441bc322002-04-20 17:38:48 +00001567 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00001568 * @fromnr: the from state
1569 * @tonr: the to state
William M. Brackddf71d62004-05-06 04:17:26 +00001570 * @counter: should that transition be associated to a counted
Daniel Veillard4255d502002-04-16 15:50:10 +00001571 *
1572 */
1573static void
1574xmlFAReduceEpsilonTransitions(xmlRegParserCtxtPtr ctxt, int fromnr,
1575 int tonr, int counter) {
1576 int transnr;
1577 xmlRegStatePtr from;
1578 xmlRegStatePtr to;
1579
1580#ifdef DEBUG_REGEXP_GRAPH
1581 printf("xmlFAReduceEpsilonTransitions(%d, %d)\n", fromnr, tonr);
1582#endif
1583 from = ctxt->states[fromnr];
1584 if (from == NULL)
1585 return;
1586 to = ctxt->states[tonr];
1587 if (to == NULL)
1588 return;
1589 if ((to->mark == XML_REGEXP_MARK_START) ||
1590 (to->mark == XML_REGEXP_MARK_VISITED))
1591 return;
1592
1593 to->mark = XML_REGEXP_MARK_VISITED;
1594 if (to->type == XML_REGEXP_FINAL_STATE) {
1595#ifdef DEBUG_REGEXP_GRAPH
1596 printf("State %d is final, so %d becomes final\n", tonr, fromnr);
1597#endif
1598 from->type = XML_REGEXP_FINAL_STATE;
1599 }
1600 for (transnr = 0;transnr < to->nbTrans;transnr++) {
Daniel Veillarddb68b742005-07-30 13:18:24 +00001601 if (to->trans[transnr].to < 0)
1602 continue;
Daniel Veillard4255d502002-04-16 15:50:10 +00001603 if (to->trans[transnr].atom == NULL) {
1604 /*
1605 * Don't remove counted transitions
1606 * Don't loop either
1607 */
Daniel Veillardb509f152002-04-17 16:28:10 +00001608 if (to->trans[transnr].to != fromnr) {
1609 if (to->trans[transnr].count >= 0) {
1610 int newto = to->trans[transnr].to;
1611
1612 xmlRegStateAddTrans(ctxt, from, NULL,
1613 ctxt->states[newto],
Daniel Veillarddb68b742005-07-30 13:18:24 +00001614 -1, to->trans[transnr].count, 0);
Daniel Veillardb509f152002-04-17 16:28:10 +00001615 } else {
Daniel Veillard4255d502002-04-16 15:50:10 +00001616#ifdef DEBUG_REGEXP_GRAPH
Daniel Veillardb509f152002-04-17 16:28:10 +00001617 printf("Found epsilon trans %d from %d to %d\n",
1618 transnr, tonr, to->trans[transnr].to);
Daniel Veillard4255d502002-04-16 15:50:10 +00001619#endif
Daniel Veillardb509f152002-04-17 16:28:10 +00001620 if (to->trans[transnr].counter >= 0) {
1621 xmlFAReduceEpsilonTransitions(ctxt, fromnr,
1622 to->trans[transnr].to,
1623 to->trans[transnr].counter);
1624 } else {
1625 xmlFAReduceEpsilonTransitions(ctxt, fromnr,
1626 to->trans[transnr].to,
1627 counter);
1628 }
1629 }
Daniel Veillard4255d502002-04-16 15:50:10 +00001630 }
1631 } else {
1632 int newto = to->trans[transnr].to;
1633
Daniel Veillardb509f152002-04-17 16:28:10 +00001634 if (to->trans[transnr].counter >= 0) {
1635 xmlRegStateAddTrans(ctxt, from, to->trans[transnr].atom,
1636 ctxt->states[newto],
Daniel Veillarddb68b742005-07-30 13:18:24 +00001637 to->trans[transnr].counter, -1, 1);
Daniel Veillardb509f152002-04-17 16:28:10 +00001638 } else {
1639 xmlRegStateAddTrans(ctxt, from, to->trans[transnr].atom,
Daniel Veillarddb68b742005-07-30 13:18:24 +00001640 ctxt->states[newto], counter, -1, 1);
Daniel Veillardb509f152002-04-17 16:28:10 +00001641 }
Daniel Veillard4255d502002-04-16 15:50:10 +00001642 }
1643 }
1644 to->mark = XML_REGEXP_MARK_NORMAL;
1645}
1646
1647/**
Daniel Veillarddb68b742005-07-30 13:18:24 +00001648 * xmlFAEliminateSimpleEpsilonTransitions:
1649 * @ctxt: a regexp parser context
1650 *
1651 * Eliminating general epsilon transitions can get costly in the general
1652 * algorithm due to the large amount of generated new transitions and
1653 * associated comparisons. However for simple epsilon transition used just
1654 * to separate building blocks when generating the automata this can be
1655 * reduced to state elimination:
1656 * - if there exists an epsilon from X to Y
1657 * - if there is no other transition from X
1658 * then X and Y are semantically equivalent and X can be eliminated
1659 * If X is the start state then make Y the start state, else replace the
1660 * target of all transitions to X by transitions to Y.
1661 */
1662static void
1663xmlFAEliminateSimpleEpsilonTransitions(xmlRegParserCtxtPtr ctxt) {
1664 int statenr, i, j, newto;
1665 xmlRegStatePtr state, tmp;
1666
1667 for (statenr = 0;statenr < ctxt->nbStates;statenr++) {
1668 state = ctxt->states[statenr];
1669 if (state == NULL)
1670 continue;
1671 if (state->nbTrans != 1)
1672 continue;
1673 /* is the only transition out a basic transition */
1674 if ((state->trans[0].atom == NULL) &&
1675 (state->trans[0].to >= 0) &&
1676 (state->trans[0].to != statenr) &&
1677 (state->trans[0].counter < 0) &&
1678 (state->trans[0].count < 0)) {
1679 newto = state->trans[0].to;
1680
1681 if (state->type == XML_REGEXP_START_STATE) {
1682#ifdef DEBUG_REGEXP_GRAPH
1683 printf("Found simple epsilon trans from start %d to %d\n",
1684 statenr, newto);
1685#endif
1686 } else {
1687#ifdef DEBUG_REGEXP_GRAPH
1688 printf("Found simple epsilon trans from %d to %d\n",
1689 statenr, newto);
1690#endif
1691 for (i = 0;i < state->nbTransTo;i++) {
1692 tmp = ctxt->states[state->transTo[i]];
1693 for (j = 0;j < tmp->nbTrans;j++) {
1694 if (tmp->trans[j].to == statenr) {
1695 tmp->trans[j].to = newto;
1696#ifdef DEBUG_REGEXP_GRAPH
1697 printf("Changed transition %d on %d to go to %d\n",
1698 j, tmp->no, newto);
1699#endif
1700 xmlRegStateAddTransTo(ctxt, ctxt->states[newto],
1701 tmp->no);
1702 }
1703 }
1704 }
1705#if 0
1706 for (i = 0;i < ctxt->nbStates;i++) {
1707 tmp = ctxt->states[i];
1708 for (j = 0;j < tmp->nbTrans;j++) {
1709 if (tmp->trans[j].to == statenr) {
1710 tmp->trans[j].to = newto;
1711#ifdef DEBUG_REGEXP_GRAPH
1712 printf("Changed transition %d on %d to go to %d\n",
1713 j, tmp->no, newto);
1714#endif
1715 }
1716 }
1717 }
1718#endif
1719 if (state->type == XML_REGEXP_FINAL_STATE)
1720 ctxt->states[newto]->type = XML_REGEXP_FINAL_STATE;
1721 /* eliminate the transition completely */
1722 state->nbTrans = 0;
1723
1724
1725 }
1726
1727 }
1728 }
1729}
1730/**
Daniel Veillard4255d502002-04-16 15:50:10 +00001731 * xmlFAEliminateEpsilonTransitions:
Daniel Veillard441bc322002-04-20 17:38:48 +00001732 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00001733 *
1734 */
1735static void
1736xmlFAEliminateEpsilonTransitions(xmlRegParserCtxtPtr ctxt) {
1737 int statenr, transnr;
1738 xmlRegStatePtr state;
Daniel Veillarddb68b742005-07-30 13:18:24 +00001739 int has_epsilon;
Daniel Veillard4255d502002-04-16 15:50:10 +00001740
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001741 if (ctxt->states == NULL) return;
1742
Daniel Veillarddb68b742005-07-30 13:18:24 +00001743 xmlFAEliminateSimpleEpsilonTransitions(ctxt);
1744
1745 has_epsilon = 0;
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001746
Daniel Veillard4255d502002-04-16 15:50:10 +00001747 /*
1748 * build the completed transitions bypassing the epsilons
1749 * Use a marking algorithm to avoid loops
Daniel Veillardcc026dc2005-01-12 13:21:17 +00001750 * mark sink states too.
Daniel Veillard4255d502002-04-16 15:50:10 +00001751 */
1752 for (statenr = 0;statenr < ctxt->nbStates;statenr++) {
1753 state = ctxt->states[statenr];
1754 if (state == NULL)
1755 continue;
Daniel Veillardcc026dc2005-01-12 13:21:17 +00001756 if ((state->nbTrans == 0) &&
1757 (state->type != XML_REGEXP_FINAL_STATE)) {
1758 state->type = XML_REGEXP_SINK_STATE;
1759 }
Daniel Veillard4255d502002-04-16 15:50:10 +00001760 for (transnr = 0;transnr < state->nbTrans;transnr++) {
1761 if ((state->trans[transnr].atom == NULL) &&
1762 (state->trans[transnr].to >= 0)) {
1763 if (state->trans[transnr].to == statenr) {
1764 state->trans[transnr].to = -1;
1765#ifdef DEBUG_REGEXP_GRAPH
1766 printf("Removed loopback epsilon trans %d on %d\n",
1767 transnr, statenr);
1768#endif
1769 } else if (state->trans[transnr].count < 0) {
1770 int newto = state->trans[transnr].to;
1771
1772#ifdef DEBUG_REGEXP_GRAPH
1773 printf("Found epsilon trans %d from %d to %d\n",
1774 transnr, statenr, newto);
1775#endif
1776 state->mark = XML_REGEXP_MARK_START;
Daniel Veillarddb68b742005-07-30 13:18:24 +00001777 has_epsilon = 1;
Daniel Veillard4255d502002-04-16 15:50:10 +00001778 xmlFAReduceEpsilonTransitions(ctxt, statenr,
1779 newto, state->trans[transnr].counter);
1780 state->mark = XML_REGEXP_MARK_NORMAL;
1781#ifdef DEBUG_REGEXP_GRAPH
1782 } else {
1783 printf("Found counted transition %d on %d\n",
1784 transnr, statenr);
1785#endif
1786 }
1787 }
1788 }
1789 }
1790 /*
1791 * Eliminate the epsilon transitions
1792 */
Daniel Veillarddb68b742005-07-30 13:18:24 +00001793 if (has_epsilon) {
1794 for (statenr = 0;statenr < ctxt->nbStates;statenr++) {
1795 state = ctxt->states[statenr];
1796 if (state == NULL)
1797 continue;
1798 for (transnr = 0;transnr < state->nbTrans;transnr++) {
1799 xmlRegTransPtr trans = &(state->trans[transnr]);
1800 if ((trans->atom == NULL) &&
1801 (trans->count < 0) &&
1802 (trans->to >= 0)) {
1803 trans->to = -1;
1804 }
Daniel Veillard4255d502002-04-16 15:50:10 +00001805 }
1806 }
1807 }
Daniel Veillard23e73572002-09-19 19:56:43 +00001808
1809 /*
1810 * Use this pass to detect unreachable states too
1811 */
1812 for (statenr = 0;statenr < ctxt->nbStates;statenr++) {
1813 state = ctxt->states[statenr];
1814 if (state != NULL)
William M. Brack779af002003-08-01 15:55:39 +00001815 state->reached = XML_REGEXP_MARK_NORMAL;
Daniel Veillard23e73572002-09-19 19:56:43 +00001816 }
1817 state = ctxt->states[0];
1818 if (state != NULL)
William M. Brack779af002003-08-01 15:55:39 +00001819 state->reached = XML_REGEXP_MARK_START;
Daniel Veillard23e73572002-09-19 19:56:43 +00001820 while (state != NULL) {
1821 xmlRegStatePtr target = NULL;
William M. Brack779af002003-08-01 15:55:39 +00001822 state->reached = XML_REGEXP_MARK_VISITED;
Daniel Veillard23e73572002-09-19 19:56:43 +00001823 /*
William M. Brackddf71d62004-05-06 04:17:26 +00001824 * Mark all states reachable from the current reachable state
Daniel Veillard23e73572002-09-19 19:56:43 +00001825 */
1826 for (transnr = 0;transnr < state->nbTrans;transnr++) {
1827 if ((state->trans[transnr].to >= 0) &&
1828 ((state->trans[transnr].atom != NULL) ||
1829 (state->trans[transnr].count >= 0))) {
1830 int newto = state->trans[transnr].to;
1831
1832 if (ctxt->states[newto] == NULL)
1833 continue;
William M. Brack779af002003-08-01 15:55:39 +00001834 if (ctxt->states[newto]->reached == XML_REGEXP_MARK_NORMAL) {
1835 ctxt->states[newto]->reached = XML_REGEXP_MARK_START;
Daniel Veillard23e73572002-09-19 19:56:43 +00001836 target = ctxt->states[newto];
1837 }
1838 }
1839 }
Daniel Veillardcc026dc2005-01-12 13:21:17 +00001840
Daniel Veillard23e73572002-09-19 19:56:43 +00001841 /*
1842 * find the next accessible state not explored
1843 */
1844 if (target == NULL) {
1845 for (statenr = 1;statenr < ctxt->nbStates;statenr++) {
1846 state = ctxt->states[statenr];
William M. Brack779af002003-08-01 15:55:39 +00001847 if ((state != NULL) && (state->reached ==
1848 XML_REGEXP_MARK_START)) {
Daniel Veillard23e73572002-09-19 19:56:43 +00001849 target = state;
1850 break;
1851 }
1852 }
1853 }
1854 state = target;
1855 }
1856 for (statenr = 0;statenr < ctxt->nbStates;statenr++) {
1857 state = ctxt->states[statenr];
William M. Brack779af002003-08-01 15:55:39 +00001858 if ((state != NULL) && (state->reached == XML_REGEXP_MARK_NORMAL)) {
Daniel Veillard23e73572002-09-19 19:56:43 +00001859#ifdef DEBUG_REGEXP_GRAPH
1860 printf("Removed unreachable state %d\n", statenr);
1861#endif
1862 xmlRegFreeState(state);
1863 ctxt->states[statenr] = NULL;
1864 }
1865 }
1866
Daniel Veillard4255d502002-04-16 15:50:10 +00001867}
1868
Daniel Veillarde19fc232002-04-22 16:01:24 +00001869/**
1870 * xmlFACompareAtoms:
1871 * @atom1: an atom
1872 * @atom2: an atom
1873 *
William M. Brackddf71d62004-05-06 04:17:26 +00001874 * Compares two atoms to check whether they are equivalents
Daniel Veillarde19fc232002-04-22 16:01:24 +00001875 *
1876 * Returns 1 if yes and 0 otherwise
1877 */
1878static int
1879xmlFACompareAtoms(xmlRegAtomPtr atom1, xmlRegAtomPtr atom2) {
Daniel Veillard9efc4762005-07-19 14:33:55 +00001880 int ret;
1881
Daniel Veillarde19fc232002-04-22 16:01:24 +00001882 if (atom1 == atom2)
1883 return(1);
1884 if ((atom1 == NULL) || (atom2 == NULL))
1885 return(0);
1886
1887 if (atom1->type != atom2->type)
1888 return(0);
1889 switch (atom1->type) {
1890 case XML_REGEXP_STRING:
Daniel Veillard9efc4762005-07-19 14:33:55 +00001891 ret = xmlRegStrEqualWildcard((xmlChar *)atom1->valuep,
1892 (xmlChar *)atom2->valuep);
1893 break;
Daniel Veillarde19fc232002-04-22 16:01:24 +00001894 case XML_REGEXP_EPSILON:
1895 return(1);
1896 case XML_REGEXP_CHARVAL:
Daniel Veillard9efc4762005-07-19 14:33:55 +00001897 ret = atom1->codepoint == atom2->codepoint;
1898 break;
Daniel Veillarde19fc232002-04-22 16:01:24 +00001899 case XML_REGEXP_RANGES:
1900 TODO;
1901 return(0);
1902 default:
Daniel Veillard9efc4762005-07-19 14:33:55 +00001903 return(1);
Daniel Veillarde19fc232002-04-22 16:01:24 +00001904 }
Daniel Veillard9efc4762005-07-19 14:33:55 +00001905 if (atom1->neg != atom2->neg)
1906 ret = !ret;
1907 return(ret);
Daniel Veillarde19fc232002-04-22 16:01:24 +00001908}
1909
1910/**
1911 * xmlFARecurseDeterminism:
1912 * @ctxt: a regexp parser context
1913 *
1914 * Check whether the associated regexp is determinist,
1915 * should be called after xmlFAEliminateEpsilonTransitions()
1916 *
1917 */
1918static int
1919xmlFARecurseDeterminism(xmlRegParserCtxtPtr ctxt, xmlRegStatePtr state,
1920 int to, xmlRegAtomPtr atom) {
1921 int ret = 1;
1922 int transnr;
1923 xmlRegTransPtr t1;
1924
1925 if (state == NULL)
1926 return(ret);
1927 for (transnr = 0;transnr < state->nbTrans;transnr++) {
1928 t1 = &(state->trans[transnr]);
1929 /*
1930 * check transitions conflicting with the one looked at
1931 */
1932 if (t1->atom == NULL) {
1933 if (t1->to == -1)
1934 continue;
1935 ret = xmlFARecurseDeterminism(ctxt, ctxt->states[t1->to],
1936 to, atom);
1937 if (ret == 0)
1938 return(0);
1939 continue;
1940 }
1941 if (t1->to != to)
1942 continue;
1943 if (xmlFACompareAtoms(t1->atom, atom))
1944 return(0);
1945 }
1946 return(ret);
1947}
1948
1949/**
1950 * xmlFAComputesDeterminism:
1951 * @ctxt: a regexp parser context
1952 *
1953 * Check whether the associated regexp is determinist,
1954 * should be called after xmlFAEliminateEpsilonTransitions()
1955 *
1956 */
1957static int
1958xmlFAComputesDeterminism(xmlRegParserCtxtPtr ctxt) {
1959 int statenr, transnr;
1960 xmlRegStatePtr state;
1961 xmlRegTransPtr t1, t2;
1962 int i;
1963 int ret = 1;
1964
Daniel Veillard4402ab42002-09-12 16:02:56 +00001965#ifdef DEBUG_REGEXP_GRAPH
1966 printf("xmlFAComputesDeterminism\n");
1967 xmlRegPrintCtxt(stdout, ctxt);
1968#endif
Daniel Veillarde19fc232002-04-22 16:01:24 +00001969 if (ctxt->determinist != -1)
1970 return(ctxt->determinist);
1971
1972 /*
William M. Brackddf71d62004-05-06 04:17:26 +00001973 * Check for all states that there aren't 2 transitions
Daniel Veillarde19fc232002-04-22 16:01:24 +00001974 * with the same atom and a different target.
1975 */
1976 for (statenr = 0;statenr < ctxt->nbStates;statenr++) {
1977 state = ctxt->states[statenr];
1978 if (state == NULL)
1979 continue;
1980 for (transnr = 0;transnr < state->nbTrans;transnr++) {
1981 t1 = &(state->trans[transnr]);
1982 /*
1983 * Determinism checks in case of counted or all transitions
1984 * will have to be handled separately
1985 */
1986 if (t1->atom == NULL)
1987 continue;
1988 if (t1->to == -1) /* eliminated */
1989 continue;
1990 for (i = 0;i < transnr;i++) {
1991 t2 = &(state->trans[i]);
1992 if (t2->to == -1) /* eliminated */
1993 continue;
1994 if (t2->atom != NULL) {
1995 if (t1->to == t2->to) {
1996 if (xmlFACompareAtoms(t1->atom, t2->atom))
William M. Brackddf71d62004-05-06 04:17:26 +00001997 t2->to = -1; /* eliminated */
Daniel Veillarde19fc232002-04-22 16:01:24 +00001998 } else {
1999 /* not determinist ! */
2000 if (xmlFACompareAtoms(t1->atom, t2->atom))
2001 ret = 0;
2002 }
2003 } else if (t1->to != -1) {
2004 /*
2005 * do the closure in case of remaining specific
2006 * epsilon transitions like choices or all
2007 */
2008 ret = xmlFARecurseDeterminism(ctxt, ctxt->states[t1->to],
2009 t2->to, t2->atom);
2010 if (ret == 0)
2011 return(0);
2012 }
2013 }
2014 if (ret == 0)
2015 break;
2016 }
2017 if (ret == 0)
2018 break;
2019 }
2020 ctxt->determinist = ret;
2021 return(ret);
2022}
2023
Daniel Veillard4255d502002-04-16 15:50:10 +00002024/************************************************************************
2025 * *
2026 * Routines to check input against transition atoms *
2027 * *
2028 ************************************************************************/
2029
2030static int
2031xmlRegCheckCharacterRange(xmlRegAtomType type, int codepoint, int neg,
2032 int start, int end, const xmlChar *blockName) {
2033 int ret = 0;
2034
2035 switch (type) {
2036 case XML_REGEXP_STRING:
2037 case XML_REGEXP_SUBREG:
2038 case XML_REGEXP_RANGES:
2039 case XML_REGEXP_EPSILON:
2040 return(-1);
2041 case XML_REGEXP_ANYCHAR:
2042 ret = ((codepoint != '\n') && (codepoint != '\r'));
2043 break;
2044 case XML_REGEXP_CHARVAL:
2045 ret = ((codepoint >= start) && (codepoint <= end));
2046 break;
2047 case XML_REGEXP_NOTSPACE:
2048 neg = !neg;
2049 case XML_REGEXP_ANYSPACE:
2050 ret = ((codepoint == '\n') || (codepoint == '\r') ||
2051 (codepoint == '\t') || (codepoint == ' '));
2052 break;
2053 case XML_REGEXP_NOTINITNAME:
2054 neg = !neg;
2055 case XML_REGEXP_INITNAME:
William M. Brack871611b2003-10-18 04:53:14 +00002056 ret = (IS_LETTER(codepoint) ||
Daniel Veillard4255d502002-04-16 15:50:10 +00002057 (codepoint == '_') || (codepoint == ':'));
2058 break;
2059 case XML_REGEXP_NOTNAMECHAR:
2060 neg = !neg;
2061 case XML_REGEXP_NAMECHAR:
William M. Brack871611b2003-10-18 04:53:14 +00002062 ret = (IS_LETTER(codepoint) || IS_DIGIT(codepoint) ||
Daniel Veillard4255d502002-04-16 15:50:10 +00002063 (codepoint == '.') || (codepoint == '-') ||
2064 (codepoint == '_') || (codepoint == ':') ||
William M. Brack871611b2003-10-18 04:53:14 +00002065 IS_COMBINING(codepoint) || IS_EXTENDER(codepoint));
Daniel Veillard4255d502002-04-16 15:50:10 +00002066 break;
2067 case XML_REGEXP_NOTDECIMAL:
2068 neg = !neg;
2069 case XML_REGEXP_DECIMAL:
2070 ret = xmlUCSIsCatNd(codepoint);
2071 break;
2072 case XML_REGEXP_REALCHAR:
2073 neg = !neg;
2074 case XML_REGEXP_NOTREALCHAR:
2075 ret = xmlUCSIsCatP(codepoint);
2076 if (ret == 0)
2077 ret = xmlUCSIsCatZ(codepoint);
2078 if (ret == 0)
2079 ret = xmlUCSIsCatC(codepoint);
2080 break;
2081 case XML_REGEXP_LETTER:
2082 ret = xmlUCSIsCatL(codepoint);
2083 break;
2084 case XML_REGEXP_LETTER_UPPERCASE:
2085 ret = xmlUCSIsCatLu(codepoint);
2086 break;
2087 case XML_REGEXP_LETTER_LOWERCASE:
2088 ret = xmlUCSIsCatLl(codepoint);
2089 break;
2090 case XML_REGEXP_LETTER_TITLECASE:
2091 ret = xmlUCSIsCatLt(codepoint);
2092 break;
2093 case XML_REGEXP_LETTER_MODIFIER:
2094 ret = xmlUCSIsCatLm(codepoint);
2095 break;
2096 case XML_REGEXP_LETTER_OTHERS:
2097 ret = xmlUCSIsCatLo(codepoint);
2098 break;
2099 case XML_REGEXP_MARK:
2100 ret = xmlUCSIsCatM(codepoint);
2101 break;
2102 case XML_REGEXP_MARK_NONSPACING:
2103 ret = xmlUCSIsCatMn(codepoint);
2104 break;
2105 case XML_REGEXP_MARK_SPACECOMBINING:
2106 ret = xmlUCSIsCatMc(codepoint);
2107 break;
2108 case XML_REGEXP_MARK_ENCLOSING:
2109 ret = xmlUCSIsCatMe(codepoint);
2110 break;
2111 case XML_REGEXP_NUMBER:
2112 ret = xmlUCSIsCatN(codepoint);
2113 break;
2114 case XML_REGEXP_NUMBER_DECIMAL:
2115 ret = xmlUCSIsCatNd(codepoint);
2116 break;
2117 case XML_REGEXP_NUMBER_LETTER:
2118 ret = xmlUCSIsCatNl(codepoint);
2119 break;
2120 case XML_REGEXP_NUMBER_OTHERS:
2121 ret = xmlUCSIsCatNo(codepoint);
2122 break;
2123 case XML_REGEXP_PUNCT:
2124 ret = xmlUCSIsCatP(codepoint);
2125 break;
2126 case XML_REGEXP_PUNCT_CONNECTOR:
2127 ret = xmlUCSIsCatPc(codepoint);
2128 break;
2129 case XML_REGEXP_PUNCT_DASH:
2130 ret = xmlUCSIsCatPd(codepoint);
2131 break;
2132 case XML_REGEXP_PUNCT_OPEN:
2133 ret = xmlUCSIsCatPs(codepoint);
2134 break;
2135 case XML_REGEXP_PUNCT_CLOSE:
2136 ret = xmlUCSIsCatPe(codepoint);
2137 break;
2138 case XML_REGEXP_PUNCT_INITQUOTE:
2139 ret = xmlUCSIsCatPi(codepoint);
2140 break;
2141 case XML_REGEXP_PUNCT_FINQUOTE:
2142 ret = xmlUCSIsCatPf(codepoint);
2143 break;
2144 case XML_REGEXP_PUNCT_OTHERS:
2145 ret = xmlUCSIsCatPo(codepoint);
2146 break;
2147 case XML_REGEXP_SEPAR:
2148 ret = xmlUCSIsCatZ(codepoint);
2149 break;
2150 case XML_REGEXP_SEPAR_SPACE:
2151 ret = xmlUCSIsCatZs(codepoint);
2152 break;
2153 case XML_REGEXP_SEPAR_LINE:
2154 ret = xmlUCSIsCatZl(codepoint);
2155 break;
2156 case XML_REGEXP_SEPAR_PARA:
2157 ret = xmlUCSIsCatZp(codepoint);
2158 break;
2159 case XML_REGEXP_SYMBOL:
2160 ret = xmlUCSIsCatS(codepoint);
2161 break;
2162 case XML_REGEXP_SYMBOL_MATH:
2163 ret = xmlUCSIsCatSm(codepoint);
2164 break;
2165 case XML_REGEXP_SYMBOL_CURRENCY:
2166 ret = xmlUCSIsCatSc(codepoint);
2167 break;
2168 case XML_REGEXP_SYMBOL_MODIFIER:
2169 ret = xmlUCSIsCatSk(codepoint);
2170 break;
2171 case XML_REGEXP_SYMBOL_OTHERS:
2172 ret = xmlUCSIsCatSo(codepoint);
2173 break;
2174 case XML_REGEXP_OTHER:
2175 ret = xmlUCSIsCatC(codepoint);
2176 break;
2177 case XML_REGEXP_OTHER_CONTROL:
2178 ret = xmlUCSIsCatCc(codepoint);
2179 break;
2180 case XML_REGEXP_OTHER_FORMAT:
2181 ret = xmlUCSIsCatCf(codepoint);
2182 break;
2183 case XML_REGEXP_OTHER_PRIVATE:
2184 ret = xmlUCSIsCatCo(codepoint);
2185 break;
2186 case XML_REGEXP_OTHER_NA:
2187 /* ret = xmlUCSIsCatCn(codepoint); */
2188 /* Seems it doesn't exist anymore in recent Unicode releases */
2189 ret = 0;
2190 break;
2191 case XML_REGEXP_BLOCK_NAME:
2192 ret = xmlUCSIsBlock(codepoint, (const char *) blockName);
2193 break;
2194 }
2195 if (neg)
2196 return(!ret);
2197 return(ret);
2198}
2199
2200static int
2201xmlRegCheckCharacter(xmlRegAtomPtr atom, int codepoint) {
2202 int i, ret = 0;
2203 xmlRegRangePtr range;
2204
William M. Brack871611b2003-10-18 04:53:14 +00002205 if ((atom == NULL) || (!IS_CHAR(codepoint)))
Daniel Veillard4255d502002-04-16 15:50:10 +00002206 return(-1);
2207
2208 switch (atom->type) {
2209 case XML_REGEXP_SUBREG:
2210 case XML_REGEXP_EPSILON:
2211 return(-1);
2212 case XML_REGEXP_CHARVAL:
2213 return(codepoint == atom->codepoint);
2214 case XML_REGEXP_RANGES: {
2215 int accept = 0;
Daniel Veillardf2a12832003-11-24 13:04:35 +00002216
Daniel Veillard4255d502002-04-16 15:50:10 +00002217 for (i = 0;i < atom->nbRanges;i++) {
2218 range = atom->ranges[i];
Daniel Veillardf8b9de32003-11-24 14:27:26 +00002219 if (range->neg == 2) {
Daniel Veillard4255d502002-04-16 15:50:10 +00002220 ret = xmlRegCheckCharacterRange(range->type, codepoint,
2221 0, range->start, range->end,
2222 range->blockName);
2223 if (ret != 0)
2224 return(0); /* excluded char */
Daniel Veillardf8b9de32003-11-24 14:27:26 +00002225 } else if (range->neg) {
2226 ret = xmlRegCheckCharacterRange(range->type, codepoint,
2227 0, range->start, range->end,
2228 range->blockName);
2229 if (ret == 0)
Daniel Veillardf2a12832003-11-24 13:04:35 +00002230 accept = 1;
Daniel Veillardf8b9de32003-11-24 14:27:26 +00002231 else
2232 return(0);
Daniel Veillard4255d502002-04-16 15:50:10 +00002233 } else {
2234 ret = xmlRegCheckCharacterRange(range->type, codepoint,
2235 0, range->start, range->end,
2236 range->blockName);
2237 if (ret != 0)
2238 accept = 1; /* might still be excluded */
2239 }
2240 }
2241 return(accept);
2242 }
2243 case XML_REGEXP_STRING:
2244 printf("TODO: XML_REGEXP_STRING\n");
2245 return(-1);
2246 case XML_REGEXP_ANYCHAR:
2247 case XML_REGEXP_ANYSPACE:
2248 case XML_REGEXP_NOTSPACE:
2249 case XML_REGEXP_INITNAME:
2250 case XML_REGEXP_NOTINITNAME:
2251 case XML_REGEXP_NAMECHAR:
2252 case XML_REGEXP_NOTNAMECHAR:
2253 case XML_REGEXP_DECIMAL:
2254 case XML_REGEXP_NOTDECIMAL:
2255 case XML_REGEXP_REALCHAR:
2256 case XML_REGEXP_NOTREALCHAR:
2257 case XML_REGEXP_LETTER:
2258 case XML_REGEXP_LETTER_UPPERCASE:
2259 case XML_REGEXP_LETTER_LOWERCASE:
2260 case XML_REGEXP_LETTER_TITLECASE:
2261 case XML_REGEXP_LETTER_MODIFIER:
2262 case XML_REGEXP_LETTER_OTHERS:
2263 case XML_REGEXP_MARK:
2264 case XML_REGEXP_MARK_NONSPACING:
2265 case XML_REGEXP_MARK_SPACECOMBINING:
2266 case XML_REGEXP_MARK_ENCLOSING:
2267 case XML_REGEXP_NUMBER:
2268 case XML_REGEXP_NUMBER_DECIMAL:
2269 case XML_REGEXP_NUMBER_LETTER:
2270 case XML_REGEXP_NUMBER_OTHERS:
2271 case XML_REGEXP_PUNCT:
2272 case XML_REGEXP_PUNCT_CONNECTOR:
2273 case XML_REGEXP_PUNCT_DASH:
2274 case XML_REGEXP_PUNCT_OPEN:
2275 case XML_REGEXP_PUNCT_CLOSE:
2276 case XML_REGEXP_PUNCT_INITQUOTE:
2277 case XML_REGEXP_PUNCT_FINQUOTE:
2278 case XML_REGEXP_PUNCT_OTHERS:
2279 case XML_REGEXP_SEPAR:
2280 case XML_REGEXP_SEPAR_SPACE:
2281 case XML_REGEXP_SEPAR_LINE:
2282 case XML_REGEXP_SEPAR_PARA:
2283 case XML_REGEXP_SYMBOL:
2284 case XML_REGEXP_SYMBOL_MATH:
2285 case XML_REGEXP_SYMBOL_CURRENCY:
2286 case XML_REGEXP_SYMBOL_MODIFIER:
2287 case XML_REGEXP_SYMBOL_OTHERS:
2288 case XML_REGEXP_OTHER:
2289 case XML_REGEXP_OTHER_CONTROL:
2290 case XML_REGEXP_OTHER_FORMAT:
2291 case XML_REGEXP_OTHER_PRIVATE:
2292 case XML_REGEXP_OTHER_NA:
2293 case XML_REGEXP_BLOCK_NAME:
2294 ret = xmlRegCheckCharacterRange(atom->type, codepoint, 0, 0, 0,
2295 (const xmlChar *)atom->valuep);
2296 if (atom->neg)
2297 ret = !ret;
2298 break;
2299 }
2300 return(ret);
2301}
2302
2303/************************************************************************
2304 * *
William M. Brackddf71d62004-05-06 04:17:26 +00002305 * Saving and restoring state of an execution context *
Daniel Veillard4255d502002-04-16 15:50:10 +00002306 * *
2307 ************************************************************************/
2308
2309#ifdef DEBUG_REGEXP_EXEC
2310static void
2311xmlFARegDebugExec(xmlRegExecCtxtPtr exec) {
2312 printf("state: %d:%d:idx %d", exec->state->no, exec->transno, exec->index);
2313 if (exec->inputStack != NULL) {
2314 int i;
2315 printf(": ");
2316 for (i = 0;(i < 3) && (i < exec->inputStackNr);i++)
2317 printf("%s ", exec->inputStack[exec->inputStackNr - (i + 1)]);
2318 } else {
2319 printf(": %s", &(exec->inputString[exec->index]));
2320 }
2321 printf("\n");
2322}
2323#endif
2324
2325static void
2326xmlFARegExecSave(xmlRegExecCtxtPtr exec) {
2327#ifdef DEBUG_REGEXP_EXEC
2328 printf("saving ");
2329 exec->transno++;
2330 xmlFARegDebugExec(exec);
2331 exec->transno--;
2332#endif
2333
2334 if (exec->maxRollbacks == 0) {
2335 exec->maxRollbacks = 4;
2336 exec->rollbacks = (xmlRegExecRollback *) xmlMalloc(exec->maxRollbacks *
2337 sizeof(xmlRegExecRollback));
2338 if (exec->rollbacks == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00002339 xmlRegexpErrMemory(NULL, "saving regexp");
Daniel Veillard4255d502002-04-16 15:50:10 +00002340 exec->maxRollbacks = 0;
2341 return;
2342 }
2343 memset(exec->rollbacks, 0,
2344 exec->maxRollbacks * sizeof(xmlRegExecRollback));
2345 } else if (exec->nbRollbacks >= exec->maxRollbacks) {
2346 xmlRegExecRollback *tmp;
2347 int len = exec->maxRollbacks;
2348
2349 exec->maxRollbacks *= 2;
2350 tmp = (xmlRegExecRollback *) xmlRealloc(exec->rollbacks,
2351 exec->maxRollbacks * sizeof(xmlRegExecRollback));
2352 if (tmp == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00002353 xmlRegexpErrMemory(NULL, "saving regexp");
Daniel Veillard4255d502002-04-16 15:50:10 +00002354 exec->maxRollbacks /= 2;
2355 return;
2356 }
2357 exec->rollbacks = tmp;
2358 tmp = &exec->rollbacks[len];
2359 memset(tmp, 0, (exec->maxRollbacks - len) * sizeof(xmlRegExecRollback));
2360 }
2361 exec->rollbacks[exec->nbRollbacks].state = exec->state;
2362 exec->rollbacks[exec->nbRollbacks].index = exec->index;
2363 exec->rollbacks[exec->nbRollbacks].nextbranch = exec->transno + 1;
2364 if (exec->comp->nbCounters > 0) {
2365 if (exec->rollbacks[exec->nbRollbacks].counts == NULL) {
2366 exec->rollbacks[exec->nbRollbacks].counts = (int *)
2367 xmlMalloc(exec->comp->nbCounters * sizeof(int));
2368 if (exec->rollbacks[exec->nbRollbacks].counts == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00002369 xmlRegexpErrMemory(NULL, "saving regexp");
Daniel Veillard4255d502002-04-16 15:50:10 +00002370 exec->status = -5;
2371 return;
2372 }
2373 }
2374 memcpy(exec->rollbacks[exec->nbRollbacks].counts, exec->counts,
2375 exec->comp->nbCounters * sizeof(int));
2376 }
2377 exec->nbRollbacks++;
2378}
2379
2380static void
2381xmlFARegExecRollBack(xmlRegExecCtxtPtr exec) {
2382 if (exec->nbRollbacks <= 0) {
2383 exec->status = -1;
2384#ifdef DEBUG_REGEXP_EXEC
2385 printf("rollback failed on empty stack\n");
2386#endif
2387 return;
2388 }
2389 exec->nbRollbacks--;
2390 exec->state = exec->rollbacks[exec->nbRollbacks].state;
2391 exec->index = exec->rollbacks[exec->nbRollbacks].index;
2392 exec->transno = exec->rollbacks[exec->nbRollbacks].nextbranch;
2393 if (exec->comp->nbCounters > 0) {
2394 if (exec->rollbacks[exec->nbRollbacks].counts == NULL) {
2395 fprintf(stderr, "exec save: allocation failed");
2396 exec->status = -6;
2397 return;
2398 }
2399 memcpy(exec->counts, exec->rollbacks[exec->nbRollbacks].counts,
2400 exec->comp->nbCounters * sizeof(int));
2401 }
2402
2403#ifdef DEBUG_REGEXP_EXEC
2404 printf("restored ");
2405 xmlFARegDebugExec(exec);
2406#endif
2407}
2408
2409/************************************************************************
2410 * *
William M. Brackddf71d62004-05-06 04:17:26 +00002411 * Verifier, running an input against a compiled regexp *
Daniel Veillard4255d502002-04-16 15:50:10 +00002412 * *
2413 ************************************************************************/
2414
2415static int
2416xmlFARegExec(xmlRegexpPtr comp, const xmlChar *content) {
2417 xmlRegExecCtxt execval;
2418 xmlRegExecCtxtPtr exec = &execval;
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00002419 int ret, codepoint = 0, len;
Daniel Veillard4255d502002-04-16 15:50:10 +00002420
2421 exec->inputString = content;
2422 exec->index = 0;
2423 exec->determinist = 1;
2424 exec->maxRollbacks = 0;
2425 exec->nbRollbacks = 0;
2426 exec->rollbacks = NULL;
2427 exec->status = 0;
2428 exec->comp = comp;
2429 exec->state = comp->states[0];
2430 exec->transno = 0;
2431 exec->transcount = 0;
Daniel Veillardf2a12832003-11-24 13:04:35 +00002432 exec->inputStack = NULL;
2433 exec->inputStackMax = 0;
Daniel Veillard4255d502002-04-16 15:50:10 +00002434 if (comp->nbCounters > 0) {
2435 exec->counts = (int *) xmlMalloc(comp->nbCounters * sizeof(int));
Daniel Veillardff46a042003-10-08 08:53:17 +00002436 if (exec->counts == NULL) {
2437 xmlRegexpErrMemory(NULL, "running regexp");
Daniel Veillard4255d502002-04-16 15:50:10 +00002438 return(-1);
Daniel Veillardff46a042003-10-08 08:53:17 +00002439 }
Daniel Veillard4255d502002-04-16 15:50:10 +00002440 memset(exec->counts, 0, comp->nbCounters * sizeof(int));
2441 } else
2442 exec->counts = NULL;
2443 while ((exec->status == 0) &&
2444 ((exec->inputString[exec->index] != 0) ||
2445 (exec->state->type != XML_REGEXP_FINAL_STATE))) {
2446 xmlRegTransPtr trans;
2447 xmlRegAtomPtr atom;
2448
2449 /*
William M. Brack0e00b282004-04-26 15:40:47 +00002450 * If end of input on non-terminal state, rollback, however we may
Daniel Veillard4255d502002-04-16 15:50:10 +00002451 * still have epsilon like transition for counted transitions
William M. Brack0e00b282004-04-26 15:40:47 +00002452 * on counters, in that case don't break too early. Additionally,
2453 * if we are working on a range like "AB{0,2}", where B is not present,
2454 * we don't want to break.
Daniel Veillard4255d502002-04-16 15:50:10 +00002455 */
William M. Brack0e00b282004-04-26 15:40:47 +00002456 if ((exec->inputString[exec->index] == 0) && (exec->counts == NULL)) {
William M. Brackddf71d62004-05-06 04:17:26 +00002457 /*
2458 * if there is a transition, we must check if
2459 * atom allows minOccurs of 0
2460 */
2461 if (exec->transno < exec->state->nbTrans) {
William M. Brack0e00b282004-04-26 15:40:47 +00002462 trans = &exec->state->trans[exec->transno];
2463 if (trans->to >=0) {
2464 atom = trans->atom;
2465 if (!((atom->min == 0) && (atom->max > 0)))
2466 goto rollback;
2467 }
2468 } else
2469 goto rollback;
2470 }
Daniel Veillard4255d502002-04-16 15:50:10 +00002471
2472 exec->transcount = 0;
2473 for (;exec->transno < exec->state->nbTrans;exec->transno++) {
2474 trans = &exec->state->trans[exec->transno];
2475 if (trans->to < 0)
2476 continue;
2477 atom = trans->atom;
2478 ret = 0;
2479 if (trans->count >= 0) {
2480 int count;
2481 xmlRegCounterPtr counter;
2482
2483 /*
2484 * A counted transition.
2485 */
2486
2487 count = exec->counts[trans->count];
2488 counter = &exec->comp->counters[trans->count];
2489#ifdef DEBUG_REGEXP_EXEC
2490 printf("testing count %d: val %d, min %d, max %d\n",
2491 trans->count, count, counter->min, counter->max);
2492#endif
2493 ret = ((count >= counter->min) && (count <= counter->max));
2494 } else if (atom == NULL) {
2495 fprintf(stderr, "epsilon transition left at runtime\n");
2496 exec->status = -2;
2497 break;
2498 } else if (exec->inputString[exec->index] != 0) {
2499 codepoint = CUR_SCHAR(&(exec->inputString[exec->index]), len);
2500 ret = xmlRegCheckCharacter(atom, codepoint);
William M. Brack0e00b282004-04-26 15:40:47 +00002501 if ((ret == 1) && (atom->min >= 0) && (atom->max > 0)) {
Daniel Veillard4255d502002-04-16 15:50:10 +00002502 xmlRegStatePtr to = comp->states[trans->to];
2503
2504 /*
2505 * this is a multiple input sequence
2506 */
2507 if (exec->state->nbTrans > exec->transno + 1) {
2508 xmlFARegExecSave(exec);
2509 }
2510 exec->transcount = 1;
2511 do {
2512 /*
2513 * Try to progress as much as possible on the input
2514 */
2515 if (exec->transcount == atom->max) {
2516 break;
2517 }
2518 exec->index += len;
2519 /*
2520 * End of input: stop here
2521 */
2522 if (exec->inputString[exec->index] == 0) {
2523 exec->index -= len;
2524 break;
2525 }
2526 if (exec->transcount >= atom->min) {
2527 int transno = exec->transno;
2528 xmlRegStatePtr state = exec->state;
2529
2530 /*
2531 * The transition is acceptable save it
2532 */
2533 exec->transno = -1; /* trick */
2534 exec->state = to;
2535 xmlFARegExecSave(exec);
2536 exec->transno = transno;
2537 exec->state = state;
2538 }
2539 codepoint = CUR_SCHAR(&(exec->inputString[exec->index]),
2540 len);
2541 ret = xmlRegCheckCharacter(atom, codepoint);
2542 exec->transcount++;
2543 } while (ret == 1);
2544 if (exec->transcount < atom->min)
2545 ret = 0;
2546
2547 /*
2548 * If the last check failed but one transition was found
2549 * possible, rollback
2550 */
2551 if (ret < 0)
2552 ret = 0;
2553 if (ret == 0) {
2554 goto rollback;
2555 }
William M. Brack0e00b282004-04-26 15:40:47 +00002556 } else if ((ret == 0) && (atom->min == 0) && (atom->max > 0)) {
2557 /*
2558 * we don't match on the codepoint, but minOccurs of 0
2559 * says that's ok. Setting len to 0 inhibits stepping
2560 * over the codepoint.
2561 */
2562 exec->transcount = 1;
2563 len = 0;
2564 ret = 1;
Daniel Veillard4255d502002-04-16 15:50:10 +00002565 }
William M. Brack0e00b282004-04-26 15:40:47 +00002566 } else if ((atom->min == 0) && (atom->max > 0)) {
2567 /* another spot to match when minOccurs is 0 */
2568 exec->transcount = 1;
2569 len = 0;
2570 ret = 1;
Daniel Veillard4255d502002-04-16 15:50:10 +00002571 }
2572 if (ret == 1) {
2573 if (exec->state->nbTrans > exec->transno + 1) {
2574 xmlFARegExecSave(exec);
2575 }
2576 if (trans->counter >= 0) {
2577#ifdef DEBUG_REGEXP_EXEC
2578 printf("Increasing count %d\n", trans->counter);
2579#endif
2580 exec->counts[trans->counter]++;
2581 }
Daniel Veillard10752282005-08-08 13:05:13 +00002582 if ((trans->count >= 0) &&
2583 (trans->count < REGEXP_ALL_COUNTER)) {
2584#ifdef DEBUG_REGEXP_EXEC
2585 printf("resetting count %d on transition\n",
2586 trans->count);
2587#endif
2588 exec->counts[trans->count] = 0;
2589 }
Daniel Veillard4255d502002-04-16 15:50:10 +00002590#ifdef DEBUG_REGEXP_EXEC
2591 printf("entering state %d\n", trans->to);
2592#endif
2593 exec->state = comp->states[trans->to];
2594 exec->transno = 0;
2595 if (trans->atom != NULL) {
2596 exec->index += len;
2597 }
2598 goto progress;
2599 } else if (ret < 0) {
2600 exec->status = -4;
2601 break;
2602 }
2603 }
2604 if ((exec->transno != 0) || (exec->state->nbTrans == 0)) {
2605rollback:
2606 /*
2607 * Failed to find a way out
2608 */
2609 exec->determinist = 0;
2610 xmlFARegExecRollBack(exec);
2611 }
2612progress:
2613 continue;
2614 }
2615 if (exec->rollbacks != NULL) {
2616 if (exec->counts != NULL) {
2617 int i;
2618
2619 for (i = 0;i < exec->maxRollbacks;i++)
2620 if (exec->rollbacks[i].counts != NULL)
2621 xmlFree(exec->rollbacks[i].counts);
2622 }
2623 xmlFree(exec->rollbacks);
2624 }
2625 if (exec->counts != NULL)
2626 xmlFree(exec->counts);
2627 if (exec->status == 0)
2628 return(1);
2629 if (exec->status == -1)
2630 return(0);
2631 return(exec->status);
2632}
2633
2634/************************************************************************
2635 * *
William M. Brackddf71d62004-05-06 04:17:26 +00002636 * Progressive interface to the verifier one atom at a time *
Daniel Veillard4255d502002-04-16 15:50:10 +00002637 * *
2638 ************************************************************************/
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00002639#ifdef DEBUG_ERR
2640static void testerr(xmlRegExecCtxtPtr exec);
2641#endif
Daniel Veillard4255d502002-04-16 15:50:10 +00002642
2643/**
Daniel Veillard01c13b52002-12-10 15:19:08 +00002644 * xmlRegNewExecCtxt:
Daniel Veillard4255d502002-04-16 15:50:10 +00002645 * @comp: a precompiled regular expression
2646 * @callback: a callback function used for handling progresses in the
2647 * automata matching phase
2648 * @data: the context data associated to the callback in this context
2649 *
2650 * Build a context used for progressive evaluation of a regexp.
Daniel Veillard01c13b52002-12-10 15:19:08 +00002651 *
2652 * Returns the new context
Daniel Veillard4255d502002-04-16 15:50:10 +00002653 */
2654xmlRegExecCtxtPtr
2655xmlRegNewExecCtxt(xmlRegexpPtr comp, xmlRegExecCallbacks callback, void *data) {
2656 xmlRegExecCtxtPtr exec;
2657
2658 if (comp == NULL)
2659 return(NULL);
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00002660 if ((comp->compact == NULL) && (comp->states == NULL))
2661 return(NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +00002662 exec = (xmlRegExecCtxtPtr) xmlMalloc(sizeof(xmlRegExecCtxt));
2663 if (exec == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00002664 xmlRegexpErrMemory(NULL, "creating execution context");
Daniel Veillard4255d502002-04-16 15:50:10 +00002665 return(NULL);
2666 }
2667 memset(exec, 0, sizeof(xmlRegExecCtxt));
2668 exec->inputString = NULL;
2669 exec->index = 0;
2670 exec->determinist = 1;
2671 exec->maxRollbacks = 0;
2672 exec->nbRollbacks = 0;
2673 exec->rollbacks = NULL;
2674 exec->status = 0;
2675 exec->comp = comp;
Daniel Veillard23e73572002-09-19 19:56:43 +00002676 if (comp->compact == NULL)
2677 exec->state = comp->states[0];
Daniel Veillard4255d502002-04-16 15:50:10 +00002678 exec->transno = 0;
2679 exec->transcount = 0;
2680 exec->callback = callback;
2681 exec->data = data;
2682 if (comp->nbCounters > 0) {
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00002683 /*
2684 * For error handling, exec->counts is allocated twice the size
2685 * the second half is used to store the data in case of rollback
2686 */
2687 exec->counts = (int *) xmlMalloc(comp->nbCounters * sizeof(int)
2688 * 2);
Daniel Veillard4255d502002-04-16 15:50:10 +00002689 if (exec->counts == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00002690 xmlRegexpErrMemory(NULL, "creating execution context");
Daniel Veillard4255d502002-04-16 15:50:10 +00002691 xmlFree(exec);
2692 return(NULL);
2693 }
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00002694 memset(exec->counts, 0, comp->nbCounters * sizeof(int) * 2);
2695 exec->errCounts = &exec->counts[comp->nbCounters];
2696 } else {
Daniel Veillard4255d502002-04-16 15:50:10 +00002697 exec->counts = NULL;
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00002698 exec->errCounts = NULL;
2699 }
Daniel Veillard4255d502002-04-16 15:50:10 +00002700 exec->inputStackMax = 0;
2701 exec->inputStackNr = 0;
2702 exec->inputStack = NULL;
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00002703 exec->errStateNo = -1;
2704 exec->errString = NULL;
Daniel Veillard4255d502002-04-16 15:50:10 +00002705 return(exec);
2706}
2707
2708/**
2709 * xmlRegFreeExecCtxt:
2710 * @exec: a regular expression evaulation context
2711 *
2712 * Free the structures associated to a regular expression evaulation context.
2713 */
2714void
2715xmlRegFreeExecCtxt(xmlRegExecCtxtPtr exec) {
2716 if (exec == NULL)
2717 return;
2718
2719 if (exec->rollbacks != NULL) {
2720 if (exec->counts != NULL) {
2721 int i;
2722
2723 for (i = 0;i < exec->maxRollbacks;i++)
2724 if (exec->rollbacks[i].counts != NULL)
2725 xmlFree(exec->rollbacks[i].counts);
2726 }
2727 xmlFree(exec->rollbacks);
2728 }
2729 if (exec->counts != NULL)
2730 xmlFree(exec->counts);
2731 if (exec->inputStack != NULL) {
2732 int i;
2733
Daniel Veillard32370232002-10-16 14:08:14 +00002734 for (i = 0;i < exec->inputStackNr;i++) {
2735 if (exec->inputStack[i].value != NULL)
2736 xmlFree(exec->inputStack[i].value);
2737 }
Daniel Veillard4255d502002-04-16 15:50:10 +00002738 xmlFree(exec->inputStack);
2739 }
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00002740 if (exec->errString != NULL)
2741 xmlFree(exec->errString);
Daniel Veillard4255d502002-04-16 15:50:10 +00002742 xmlFree(exec);
2743}
2744
2745static void
2746xmlFARegExecSaveInputString(xmlRegExecCtxtPtr exec, const xmlChar *value,
2747 void *data) {
2748#ifdef DEBUG_PUSH
2749 printf("saving value: %d:%s\n", exec->inputStackNr, value);
2750#endif
2751 if (exec->inputStackMax == 0) {
2752 exec->inputStackMax = 4;
2753 exec->inputStack = (xmlRegInputTokenPtr)
2754 xmlMalloc(exec->inputStackMax * sizeof(xmlRegInputToken));
2755 if (exec->inputStack == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00002756 xmlRegexpErrMemory(NULL, "pushing input string");
Daniel Veillard4255d502002-04-16 15:50:10 +00002757 exec->inputStackMax = 0;
2758 return;
2759 }
2760 } else if (exec->inputStackNr + 1 >= exec->inputStackMax) {
2761 xmlRegInputTokenPtr tmp;
2762
2763 exec->inputStackMax *= 2;
2764 tmp = (xmlRegInputTokenPtr) xmlRealloc(exec->inputStack,
2765 exec->inputStackMax * sizeof(xmlRegInputToken));
2766 if (tmp == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00002767 xmlRegexpErrMemory(NULL, "pushing input string");
Daniel Veillard4255d502002-04-16 15:50:10 +00002768 exec->inputStackMax /= 2;
2769 return;
2770 }
2771 exec->inputStack = tmp;
2772 }
2773 exec->inputStack[exec->inputStackNr].value = xmlStrdup(value);
2774 exec->inputStack[exec->inputStackNr].data = data;
2775 exec->inputStackNr++;
2776 exec->inputStack[exec->inputStackNr].value = NULL;
2777 exec->inputStack[exec->inputStackNr].data = NULL;
2778}
2779
Daniel Veillardc0826a72004-08-10 14:17:33 +00002780/**
2781 * xmlRegStrEqualWildcard:
2782 * @expStr: the string to be evaluated
2783 * @valStr: the validation string
2784 *
2785 * Checks if both strings are equal or have the same content. "*"
2786 * can be used as a wildcard in @valStr; "|" is used as a seperator of
2787 * substrings in both @expStr and @valStr.
2788 *
2789 * Returns 1 if the comparison is satisfied and the number of substrings
2790 * is equal, 0 otherwise.
2791 */
2792
2793static int
2794xmlRegStrEqualWildcard(const xmlChar *expStr, const xmlChar *valStr) {
2795 if (expStr == valStr) return(1);
2796 if (expStr == NULL) return(0);
2797 if (valStr == NULL) return(0);
2798 do {
2799 /*
2800 * Eval if we have a wildcard for the current item.
2801 */
2802 if (*expStr != *valStr) {
2803 if ((*valStr != 0) && (*expStr != 0) && (*expStr++ == '*')) {
2804 do {
2805 if (*valStr == XML_REG_STRING_SEPARATOR)
2806 break;
Kasimier T. Buchcikc0e833f2005-04-19 15:02:20 +00002807 valStr++;
Daniel Veillardc0826a72004-08-10 14:17:33 +00002808 } while (*valStr != 0);
2809 continue;
2810 } else
2811 return(0);
2812 }
Kasimier T. Buchcikc0e833f2005-04-19 15:02:20 +00002813 expStr++;
2814 valStr++;
Daniel Veillardc0826a72004-08-10 14:17:33 +00002815 } while (*valStr != 0);
2816 if (*expStr != 0)
2817 return (0);
2818 else
2819 return (1);
2820}
Daniel Veillard4255d502002-04-16 15:50:10 +00002821
2822/**
Daniel Veillard23e73572002-09-19 19:56:43 +00002823 * xmlRegCompactPushString:
2824 * @exec: a regexp execution context
2825 * @comp: the precompiled exec with a compact table
2826 * @value: a string token input
2827 * @data: data associated to the token to reuse in callbacks
2828 *
2829 * Push one input token in the execution context
2830 *
2831 * Returns: 1 if the regexp reached a final state, 0 if non-final, and
2832 * a negative value in case of error.
2833 */
2834static int
2835xmlRegCompactPushString(xmlRegExecCtxtPtr exec,
2836 xmlRegexpPtr comp,
2837 const xmlChar *value,
2838 void *data) {
2839 int state = exec->index;
2840 int i, target;
2841
2842 if ((comp == NULL) || (comp->compact == NULL) || (comp->stringMap == NULL))
2843 return(-1);
2844
2845 if (value == NULL) {
2846 /*
2847 * are we at a final state ?
2848 */
2849 if (comp->compact[state * (comp->nbstrings + 1)] ==
2850 XML_REGEXP_FINAL_STATE)
2851 return(1);
2852 return(0);
2853 }
2854
2855#ifdef DEBUG_PUSH
2856 printf("value pushed: %s\n", value);
2857#endif
2858
2859 /*
William M. Brackddf71d62004-05-06 04:17:26 +00002860 * Examine all outside transitions from current state
Daniel Veillard23e73572002-09-19 19:56:43 +00002861 */
2862 for (i = 0;i < comp->nbstrings;i++) {
2863 target = comp->compact[state * (comp->nbstrings + 1) + i + 1];
2864 if ((target > 0) && (target <= comp->nbstates)) {
Daniel Veillardc0826a72004-08-10 14:17:33 +00002865 target--; /* to avoid 0 */
2866 if (xmlRegStrEqualWildcard(comp->stringMap[i], value)) {
2867 exec->index = target;
Daniel Veillard118aed72002-09-24 14:13:13 +00002868 if ((exec->callback != NULL) && (comp->transdata != NULL)) {
2869 exec->callback(exec->data, value,
2870 comp->transdata[state * comp->nbstrings + i], data);
2871 }
Daniel Veillard23e73572002-09-19 19:56:43 +00002872#ifdef DEBUG_PUSH
2873 printf("entering state %d\n", target);
2874#endif
2875 if (comp->compact[target * (comp->nbstrings + 1)] ==
Daniel Veillardcc026dc2005-01-12 13:21:17 +00002876 XML_REGEXP_SINK_STATE)
2877 goto error;
2878
2879 if (comp->compact[target * (comp->nbstrings + 1)] ==
Daniel Veillard23e73572002-09-19 19:56:43 +00002880 XML_REGEXP_FINAL_STATE)
2881 return(1);
2882 return(0);
2883 }
2884 }
2885 }
2886 /*
2887 * Failed to find an exit transition out from current state for the
2888 * current token
2889 */
2890#ifdef DEBUG_PUSH
2891 printf("failed to find a transition for %s on state %d\n", value, state);
2892#endif
Daniel Veillardcc026dc2005-01-12 13:21:17 +00002893error:
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00002894 if (exec->errString != NULL)
2895 xmlFree(exec->errString);
2896 exec->errString = xmlStrdup(value);
2897 exec->errStateNo = state;
Daniel Veillard23e73572002-09-19 19:56:43 +00002898 exec->status = -1;
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00002899#ifdef DEBUG_ERR
2900 testerr(exec);
2901#endif
Daniel Veillard23e73572002-09-19 19:56:43 +00002902 return(-1);
2903}
2904
2905/**
Daniel Veillard4255d502002-04-16 15:50:10 +00002906 * xmlRegExecPushString:
Daniel Veillardea7751d2002-12-20 00:16:24 +00002907 * @exec: a regexp execution context or NULL to indicate the end
Daniel Veillard4255d502002-04-16 15:50:10 +00002908 * @value: a string token input
2909 * @data: data associated to the token to reuse in callbacks
2910 *
2911 * Push one input token in the execution context
2912 *
2913 * Returns: 1 if the regexp reached a final state, 0 if non-final, and
2914 * a negative value in case of error.
2915 */
2916int
2917xmlRegExecPushString(xmlRegExecCtxtPtr exec, const xmlChar *value,
2918 void *data) {
2919 xmlRegTransPtr trans;
2920 xmlRegAtomPtr atom;
2921 int ret;
2922 int final = 0;
Daniel Veillard90700152005-01-08 22:05:09 +00002923 int progress = 1;
Daniel Veillard4255d502002-04-16 15:50:10 +00002924
2925 if (exec == NULL)
2926 return(-1);
Daniel Veillard23e73572002-09-19 19:56:43 +00002927 if (exec->comp == NULL)
2928 return(-1);
Daniel Veillard4255d502002-04-16 15:50:10 +00002929 if (exec->status != 0)
2930 return(exec->status);
2931
Daniel Veillard23e73572002-09-19 19:56:43 +00002932 if (exec->comp->compact != NULL)
2933 return(xmlRegCompactPushString(exec, exec->comp, value, data));
2934
Daniel Veillard4255d502002-04-16 15:50:10 +00002935 if (value == NULL) {
2936 if (exec->state->type == XML_REGEXP_FINAL_STATE)
2937 return(1);
2938 final = 1;
2939 }
2940
2941#ifdef DEBUG_PUSH
2942 printf("value pushed: %s\n", value);
2943#endif
2944 /*
2945 * If we have an active rollback stack push the new value there
2946 * and get back to where we were left
2947 */
2948 if ((value != NULL) && (exec->inputStackNr > 0)) {
2949 xmlFARegExecSaveInputString(exec, value, data);
2950 value = exec->inputStack[exec->index].value;
2951 data = exec->inputStack[exec->index].data;
2952#ifdef DEBUG_PUSH
2953 printf("value loaded: %s\n", value);
2954#endif
2955 }
2956
2957 while ((exec->status == 0) &&
2958 ((value != NULL) ||
2959 ((final == 1) &&
2960 (exec->state->type != XML_REGEXP_FINAL_STATE)))) {
2961
2962 /*
2963 * End of input on non-terminal state, rollback, however we may
2964 * still have epsilon like transition for counted transitions
2965 * on counters, in that case don't break too early.
2966 */
Daniel Veillardb509f152002-04-17 16:28:10 +00002967 if ((value == NULL) && (exec->counts == NULL))
Daniel Veillard4255d502002-04-16 15:50:10 +00002968 goto rollback;
2969
2970 exec->transcount = 0;
2971 for (;exec->transno < exec->state->nbTrans;exec->transno++) {
2972 trans = &exec->state->trans[exec->transno];
2973 if (trans->to < 0)
2974 continue;
2975 atom = trans->atom;
2976 ret = 0;
Daniel Veillard441bc322002-04-20 17:38:48 +00002977 if (trans->count == REGEXP_ALL_LAX_COUNTER) {
2978 int i;
2979 int count;
2980 xmlRegTransPtr t;
2981 xmlRegCounterPtr counter;
2982
2983 ret = 0;
2984
2985#ifdef DEBUG_PUSH
2986 printf("testing all lax %d\n", trans->count);
2987#endif
2988 /*
2989 * Check all counted transitions from the current state
2990 */
2991 if ((value == NULL) && (final)) {
2992 ret = 1;
2993 } else if (value != NULL) {
2994 for (i = 0;i < exec->state->nbTrans;i++) {
2995 t = &exec->state->trans[i];
2996 if ((t->counter < 0) || (t == trans))
2997 continue;
2998 counter = &exec->comp->counters[t->counter];
2999 count = exec->counts[t->counter];
3000 if ((count < counter->max) &&
3001 (t->atom != NULL) &&
3002 (xmlStrEqual(value, t->atom->valuep))) {
3003 ret = 0;
3004 break;
3005 }
3006 if ((count >= counter->min) &&
3007 (count < counter->max) &&
3008 (xmlStrEqual(value, t->atom->valuep))) {
3009 ret = 1;
3010 break;
3011 }
3012 }
3013 }
3014 } else if (trans->count == REGEXP_ALL_COUNTER) {
Daniel Veillard8a001f62002-04-20 07:24:11 +00003015 int i;
3016 int count;
3017 xmlRegTransPtr t;
3018 xmlRegCounterPtr counter;
3019
3020 ret = 1;
3021
3022#ifdef DEBUG_PUSH
3023 printf("testing all %d\n", trans->count);
3024#endif
3025 /*
3026 * Check all counted transitions from the current state
3027 */
3028 for (i = 0;i < exec->state->nbTrans;i++) {
3029 t = &exec->state->trans[i];
3030 if ((t->counter < 0) || (t == trans))
3031 continue;
3032 counter = &exec->comp->counters[t->counter];
3033 count = exec->counts[t->counter];
3034 if ((count < counter->min) || (count > counter->max)) {
3035 ret = 0;
3036 break;
3037 }
3038 }
3039 } else if (trans->count >= 0) {
Daniel Veillard4255d502002-04-16 15:50:10 +00003040 int count;
3041 xmlRegCounterPtr counter;
3042
3043 /*
3044 * A counted transition.
3045 */
3046
3047 count = exec->counts[trans->count];
3048 counter = &exec->comp->counters[trans->count];
3049#ifdef DEBUG_PUSH
3050 printf("testing count %d: val %d, min %d, max %d\n",
3051 trans->count, count, counter->min, counter->max);
3052#endif
3053 ret = ((count >= counter->min) && (count <= counter->max));
3054 } else if (atom == NULL) {
3055 fprintf(stderr, "epsilon transition left at runtime\n");
3056 exec->status = -2;
3057 break;
3058 } else if (value != NULL) {
Daniel Veillardc0826a72004-08-10 14:17:33 +00003059 ret = xmlRegStrEqualWildcard(atom->valuep, value);
Daniel Veillard9efc4762005-07-19 14:33:55 +00003060 if (atom->neg)
3061 ret = !ret;
Daniel Veillard441bc322002-04-20 17:38:48 +00003062 if ((ret == 1) && (trans->counter >= 0)) {
3063 xmlRegCounterPtr counter;
3064 int count;
3065
3066 count = exec->counts[trans->counter];
3067 counter = &exec->comp->counters[trans->counter];
3068 if (count >= counter->max)
3069 ret = 0;
3070 }
3071
Daniel Veillard4255d502002-04-16 15:50:10 +00003072 if ((ret == 1) && (atom->min > 0) && (atom->max > 0)) {
3073 xmlRegStatePtr to = exec->comp->states[trans->to];
3074
3075 /*
3076 * this is a multiple input sequence
3077 */
3078 if (exec->state->nbTrans > exec->transno + 1) {
3079 if (exec->inputStackNr <= 0) {
3080 xmlFARegExecSaveInputString(exec, value, data);
3081 }
3082 xmlFARegExecSave(exec);
3083 }
3084 exec->transcount = 1;
3085 do {
3086 /*
3087 * Try to progress as much as possible on the input
3088 */
3089 if (exec->transcount == atom->max) {
3090 break;
3091 }
3092 exec->index++;
3093 value = exec->inputStack[exec->index].value;
3094 data = exec->inputStack[exec->index].data;
3095#ifdef DEBUG_PUSH
3096 printf("value loaded: %s\n", value);
3097#endif
3098
3099 /*
3100 * End of input: stop here
3101 */
3102 if (value == NULL) {
3103 exec->index --;
3104 break;
3105 }
3106 if (exec->transcount >= atom->min) {
3107 int transno = exec->transno;
3108 xmlRegStatePtr state = exec->state;
3109
3110 /*
3111 * The transition is acceptable save it
3112 */
3113 exec->transno = -1; /* trick */
3114 exec->state = to;
3115 if (exec->inputStackNr <= 0) {
3116 xmlFARegExecSaveInputString(exec, value, data);
3117 }
3118 xmlFARegExecSave(exec);
3119 exec->transno = transno;
3120 exec->state = state;
3121 }
3122 ret = xmlStrEqual(value, atom->valuep);
3123 exec->transcount++;
3124 } while (ret == 1);
3125 if (exec->transcount < atom->min)
3126 ret = 0;
3127
3128 /*
3129 * If the last check failed but one transition was found
3130 * possible, rollback
3131 */
3132 if (ret < 0)
3133 ret = 0;
3134 if (ret == 0) {
3135 goto rollback;
3136 }
3137 }
3138 }
3139 if (ret == 1) {
William M. Brack98873952003-12-26 06:03:14 +00003140 if ((exec->callback != NULL) && (atom != NULL) &&
3141 (data != NULL)) {
Daniel Veillard4255d502002-04-16 15:50:10 +00003142 exec->callback(exec->data, atom->valuep,
3143 atom->data, data);
3144 }
3145 if (exec->state->nbTrans > exec->transno + 1) {
3146 if (exec->inputStackNr <= 0) {
3147 xmlFARegExecSaveInputString(exec, value, data);
3148 }
3149 xmlFARegExecSave(exec);
3150 }
3151 if (trans->counter >= 0) {
3152#ifdef DEBUG_PUSH
3153 printf("Increasing count %d\n", trans->counter);
3154#endif
3155 exec->counts[trans->counter]++;
3156 }
Daniel Veillard10752282005-08-08 13:05:13 +00003157 if ((trans->count >= 0) &&
3158 (trans->count < REGEXP_ALL_COUNTER)) {
3159#ifdef DEBUG_REGEXP_EXEC
3160 printf("resetting count %d on transition\n",
3161 trans->count);
3162#endif
3163 exec->counts[trans->count] = 0;
3164 }
Daniel Veillard4255d502002-04-16 15:50:10 +00003165#ifdef DEBUG_PUSH
3166 printf("entering state %d\n", trans->to);
3167#endif
Daniel Veillardcc026dc2005-01-12 13:21:17 +00003168 if ((exec->comp->states[trans->to] != NULL) &&
3169 (exec->comp->states[trans->to]->type ==
3170 XML_REGEXP_SINK_STATE)) {
3171 /*
3172 * entering a sink state, save the current state as error
3173 * state.
3174 */
3175 if (exec->errString != NULL)
3176 xmlFree(exec->errString);
3177 exec->errString = xmlStrdup(value);
3178 exec->errState = exec->state;
3179 memcpy(exec->errCounts, exec->counts,
3180 exec->comp->nbCounters * sizeof(int));
3181 }
Daniel Veillard4255d502002-04-16 15:50:10 +00003182 exec->state = exec->comp->states[trans->to];
3183 exec->transno = 0;
3184 if (trans->atom != NULL) {
3185 if (exec->inputStack != NULL) {
3186 exec->index++;
3187 if (exec->index < exec->inputStackNr) {
3188 value = exec->inputStack[exec->index].value;
3189 data = exec->inputStack[exec->index].data;
3190#ifdef DEBUG_PUSH
3191 printf("value loaded: %s\n", value);
3192#endif
3193 } else {
3194 value = NULL;
3195 data = NULL;
3196#ifdef DEBUG_PUSH
3197 printf("end of input\n");
3198#endif
3199 }
3200 } else {
3201 value = NULL;
3202 data = NULL;
3203#ifdef DEBUG_PUSH
3204 printf("end of input\n");
3205#endif
3206 }
3207 }
3208 goto progress;
3209 } else if (ret < 0) {
3210 exec->status = -4;
3211 break;
3212 }
3213 }
3214 if ((exec->transno != 0) || (exec->state->nbTrans == 0)) {
3215rollback:
Daniel Veillard90700152005-01-08 22:05:09 +00003216 /*
Daniel Veillardcc026dc2005-01-12 13:21:17 +00003217 * if we didn't yet rollback on the current input
3218 * store the current state as the error state.
Daniel Veillard90700152005-01-08 22:05:09 +00003219 */
Daniel Veillardcc026dc2005-01-12 13:21:17 +00003220 if ((progress) && (exec->state != NULL) &&
3221 (exec->state->type != XML_REGEXP_SINK_STATE)) {
Daniel Veillard90700152005-01-08 22:05:09 +00003222 progress = 0;
3223 if (exec->errString != NULL)
3224 xmlFree(exec->errString);
3225 exec->errString = xmlStrdup(value);
3226 exec->errState = exec->state;
3227 memcpy(exec->errCounts, exec->counts,
3228 exec->comp->nbCounters * sizeof(int));
3229 }
3230
Daniel Veillard4255d502002-04-16 15:50:10 +00003231 /*
3232 * Failed to find a way out
3233 */
3234 exec->determinist = 0;
3235 xmlFARegExecRollBack(exec);
3236 if (exec->status == 0) {
3237 value = exec->inputStack[exec->index].value;
3238 data = exec->inputStack[exec->index].data;
3239#ifdef DEBUG_PUSH
3240 printf("value loaded: %s\n", value);
3241#endif
3242 }
3243 }
Daniel Veillard90700152005-01-08 22:05:09 +00003244 continue;
Daniel Veillard4255d502002-04-16 15:50:10 +00003245progress:
Daniel Veillard90700152005-01-08 22:05:09 +00003246 progress = 1;
Daniel Veillard4255d502002-04-16 15:50:10 +00003247 continue;
3248 }
3249 if (exec->status == 0) {
3250 return(exec->state->type == XML_REGEXP_FINAL_STATE);
3251 }
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003252#ifdef DEBUG_ERR
Daniel Veillard90700152005-01-08 22:05:09 +00003253 if (exec->status < 0) {
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003254 testerr(exec);
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003255 }
Daniel Veillard90700152005-01-08 22:05:09 +00003256#endif
Daniel Veillard4255d502002-04-16 15:50:10 +00003257 return(exec->status);
3258}
3259
Daniel Veillard52b48c72003-04-13 19:53:42 +00003260/**
3261 * xmlRegExecPushString2:
3262 * @exec: a regexp execution context or NULL to indicate the end
3263 * @value: the first string token input
3264 * @value2: the second string token input
3265 * @data: data associated to the token to reuse in callbacks
3266 *
3267 * Push one input token in the execution context
3268 *
3269 * Returns: 1 if the regexp reached a final state, 0 if non-final, and
3270 * a negative value in case of error.
3271 */
3272int
3273xmlRegExecPushString2(xmlRegExecCtxtPtr exec, const xmlChar *value,
3274 const xmlChar *value2, void *data) {
3275 xmlChar buf[150];
3276 int lenn, lenp, ret;
3277 xmlChar *str;
3278
3279 if (exec == NULL)
3280 return(-1);
3281 if (exec->comp == NULL)
3282 return(-1);
3283 if (exec->status != 0)
3284 return(exec->status);
3285
3286 if (value2 == NULL)
3287 return(xmlRegExecPushString(exec, value, data));
3288
3289 lenn = strlen((char *) value2);
3290 lenp = strlen((char *) value);
3291
3292 if (150 < lenn + lenp + 2) {
Daniel Veillard3c908dc2003-04-19 00:07:51 +00003293 str = (xmlChar *) xmlMallocAtomic(lenn + lenp + 2);
Daniel Veillard52b48c72003-04-13 19:53:42 +00003294 if (str == NULL) {
3295 exec->status = -1;
3296 return(-1);
3297 }
3298 } else {
3299 str = buf;
3300 }
3301 memcpy(&str[0], value, lenp);
Daniel Veillardc0826a72004-08-10 14:17:33 +00003302 str[lenp] = XML_REG_STRING_SEPARATOR;
Daniel Veillard52b48c72003-04-13 19:53:42 +00003303 memcpy(&str[lenp + 1], value2, lenn);
3304 str[lenn + lenp + 1] = 0;
3305
3306 if (exec->comp->compact != NULL)
3307 ret = xmlRegCompactPushString(exec, exec->comp, str, data);
3308 else
3309 ret = xmlRegExecPushString(exec, str, data);
3310
3311 if (str != buf)
3312 xmlFree(buf);
3313 return(ret);
3314}
3315
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003316/**
Daniel Veillard77005e62005-07-19 16:26:18 +00003317 * xmlRegExecGetValues:
Daniel Veillardfc0b6f62005-01-09 17:48:02 +00003318 * @exec: a regexp execution context
3319 * @err: error extraction or normal one
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003320 * @nbval: pointer to the number of accepted values IN/OUT
Daniel Veillardcc026dc2005-01-12 13:21:17 +00003321 * @nbneg: return number of negative transitions
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003322 * @values: pointer to the array of acceptable values
Daniel Veillardfc0b6f62005-01-09 17:48:02 +00003323 * @terminal: return value if this was a terminal state
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003324 *
Daniel Veillardfc0b6f62005-01-09 17:48:02 +00003325 * Extract informations from the regexp execution, internal routine to
3326 * implement xmlRegExecNextValues() and xmlRegExecErrInfo()
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003327 *
3328 * Returns: 0 in case of success or -1 in case of error.
3329 */
Daniel Veillardfc0b6f62005-01-09 17:48:02 +00003330static int
3331xmlRegExecGetValues(xmlRegExecCtxtPtr exec, int err,
Daniel Veillardcc026dc2005-01-12 13:21:17 +00003332 int *nbval, int *nbneg,
3333 xmlChar **values, int *terminal) {
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003334 int maxval;
Daniel Veillardcc026dc2005-01-12 13:21:17 +00003335 int nb = 0;
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003336
Daniel Veillardcc026dc2005-01-12 13:21:17 +00003337 if ((exec == NULL) || (nbval == NULL) || (nbneg == NULL) ||
3338 (values == NULL) || (*nbval <= 0))
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003339 return(-1);
Daniel Veillardfc0b6f62005-01-09 17:48:02 +00003340
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003341 maxval = *nbval;
3342 *nbval = 0;
Daniel Veillardcc026dc2005-01-12 13:21:17 +00003343 *nbneg = 0;
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003344 if ((exec->comp != NULL) && (exec->comp->compact != NULL)) {
3345 xmlRegexpPtr comp;
3346 int target, i, state;
3347
3348 comp = exec->comp;
Daniel Veillardfc0b6f62005-01-09 17:48:02 +00003349
3350 if (err) {
3351 if (exec->errStateNo == -1) return(-1);
3352 state = exec->errStateNo;
3353 } else {
3354 state = exec->index;
3355 }
3356 if (terminal != NULL) {
3357 if (comp->compact[state * (comp->nbstrings + 1)] ==
3358 XML_REGEXP_FINAL_STATE)
3359 *terminal = 1;
3360 else
3361 *terminal = 0;
3362 }
Daniel Veillardcc026dc2005-01-12 13:21:17 +00003363 for (i = 0;(i < comp->nbstrings) && (nb < maxval);i++) {
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003364 target = comp->compact[state * (comp->nbstrings + 1) + i + 1];
Daniel Veillardcc026dc2005-01-12 13:21:17 +00003365 if ((target > 0) && (target <= comp->nbstates) &&
3366 (comp->compact[(target - 1) * (comp->nbstrings + 1)] !=
3367 XML_REGEXP_SINK_STATE)) {
3368 values[nb++] = comp->stringMap[i];
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003369 (*nbval)++;
3370 }
3371 }
Daniel Veillardcc026dc2005-01-12 13:21:17 +00003372 for (i = 0;(i < comp->nbstrings) && (nb < maxval);i++) {
3373 target = comp->compact[state * (comp->nbstrings + 1) + i + 1];
3374 if ((target > 0) && (target <= comp->nbstates) &&
3375 (comp->compact[(target - 1) * (comp->nbstrings + 1)] ==
3376 XML_REGEXP_SINK_STATE)) {
3377 values[nb++] = comp->stringMap[i];
3378 (*nbneg)++;
3379 }
3380 }
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003381 } else {
3382 int transno;
3383 xmlRegTransPtr trans;
3384 xmlRegAtomPtr atom;
Daniel Veillardfc0b6f62005-01-09 17:48:02 +00003385 xmlRegStatePtr state;
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003386
Daniel Veillardfc0b6f62005-01-09 17:48:02 +00003387 if (terminal != NULL) {
3388 if (exec->state->type == XML_REGEXP_FINAL_STATE)
3389 *terminal = 1;
3390 else
3391 *terminal = 0;
3392 }
3393
3394 if (err) {
3395 if (exec->errState == NULL) return(-1);
3396 state = exec->errState;
3397 } else {
3398 if (exec->state == NULL) return(-1);
3399 state = exec->state;
3400 }
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003401 for (transno = 0;
Daniel Veillardcc026dc2005-01-12 13:21:17 +00003402 (transno < state->nbTrans) && (nb < maxval);
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003403 transno++) {
Daniel Veillardfc0b6f62005-01-09 17:48:02 +00003404 trans = &state->trans[transno];
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003405 if (trans->to < 0)
3406 continue;
3407 atom = trans->atom;
3408 if ((atom == NULL) || (atom->valuep == NULL))
3409 continue;
3410 if (trans->count == REGEXP_ALL_LAX_COUNTER) {
Daniel Veillardcc026dc2005-01-12 13:21:17 +00003411 /* this should not be reached but ... */
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003412 TODO;
3413 } else if (trans->count == REGEXP_ALL_COUNTER) {
Daniel Veillardcc026dc2005-01-12 13:21:17 +00003414 /* this should not be reached but ... */
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003415 TODO;
3416 } else if (trans->counter >= 0) {
3417 xmlRegCounterPtr counter;
3418 int count;
3419
Daniel Veillardfc0b6f62005-01-09 17:48:02 +00003420 if (err)
3421 count = exec->errCounts[trans->counter];
3422 else
3423 count = exec->counts[trans->counter];
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003424 counter = &exec->comp->counters[trans->counter];
3425 if (count < counter->max) {
Daniel Veillard77005e62005-07-19 16:26:18 +00003426 if (atom->neg)
3427 values[nb++] = (xmlChar *) atom->valuep2;
3428 else
3429 values[nb++] = (xmlChar *) atom->valuep;
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003430 (*nbval)++;
3431 }
3432 } else {
Daniel Veillardcc026dc2005-01-12 13:21:17 +00003433 if ((exec->comp->states[trans->to] != NULL) &&
3434 (exec->comp->states[trans->to]->type !=
3435 XML_REGEXP_SINK_STATE)) {
Daniel Veillard77005e62005-07-19 16:26:18 +00003436 if (atom->neg)
3437 values[nb++] = (xmlChar *) atom->valuep2;
3438 else
3439 values[nb++] = (xmlChar *) atom->valuep;
Daniel Veillardcc026dc2005-01-12 13:21:17 +00003440 (*nbval)++;
3441 }
3442 }
3443 }
3444 for (transno = 0;
3445 (transno < state->nbTrans) && (nb < maxval);
3446 transno++) {
3447 trans = &state->trans[transno];
3448 if (trans->to < 0)
3449 continue;
3450 atom = trans->atom;
3451 if ((atom == NULL) || (atom->valuep == NULL))
3452 continue;
3453 if (trans->count == REGEXP_ALL_LAX_COUNTER) {
3454 continue;
3455 } else if (trans->count == REGEXP_ALL_COUNTER) {
3456 continue;
3457 } else if (trans->counter >= 0) {
3458 continue;
3459 } else {
3460 if ((exec->comp->states[trans->to] != NULL) &&
3461 (exec->comp->states[trans->to]->type ==
3462 XML_REGEXP_SINK_STATE)) {
Daniel Veillard77005e62005-07-19 16:26:18 +00003463 if (atom->neg)
3464 values[nb++] = (xmlChar *) atom->valuep2;
3465 else
3466 values[nb++] = (xmlChar *) atom->valuep;
Daniel Veillardcc026dc2005-01-12 13:21:17 +00003467 (*nbneg)++;
3468 }
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003469 }
3470 }
3471 }
3472 return(0);
3473}
3474
Daniel Veillardfc0b6f62005-01-09 17:48:02 +00003475/**
3476 * xmlRegExecNextValues:
3477 * @exec: a regexp execution context
3478 * @nbval: pointer to the number of accepted values IN/OUT
Daniel Veillardcc026dc2005-01-12 13:21:17 +00003479 * @nbneg: return number of negative transitions
Daniel Veillardfc0b6f62005-01-09 17:48:02 +00003480 * @values: pointer to the array of acceptable values
3481 * @terminal: return value if this was a terminal state
3482 *
3483 * Extract informations from the regexp execution,
3484 * the parameter @values must point to an array of @nbval string pointers
3485 * on return nbval will contain the number of possible strings in that
3486 * state and the @values array will be updated with them. The string values
3487 * returned will be freed with the @exec context and don't need to be
3488 * deallocated.
3489 *
3490 * Returns: 0 in case of success or -1 in case of error.
3491 */
3492int
Daniel Veillardcc026dc2005-01-12 13:21:17 +00003493xmlRegExecNextValues(xmlRegExecCtxtPtr exec, int *nbval, int *nbneg,
3494 xmlChar **values, int *terminal) {
3495 return(xmlRegExecGetValues(exec, 0, nbval, nbneg, values, terminal));
Daniel Veillardfc0b6f62005-01-09 17:48:02 +00003496}
3497
3498/**
3499 * xmlRegExecErrInfo:
3500 * @exec: a regexp execution context generating an error
3501 * @string: return value for the error string
3502 * @nbval: pointer to the number of accepted values IN/OUT
Daniel Veillardcc026dc2005-01-12 13:21:17 +00003503 * @nbneg: return number of negative transitions
Daniel Veillardfc0b6f62005-01-09 17:48:02 +00003504 * @values: pointer to the array of acceptable values
3505 * @terminal: return value if this was a terminal state
3506 *
3507 * Extract error informations from the regexp execution, the parameter
3508 * @string will be updated with the value pushed and not accepted,
3509 * the parameter @values must point to an array of @nbval string pointers
3510 * on return nbval will contain the number of possible strings in that
3511 * state and the @values array will be updated with them. The string values
3512 * returned will be freed with the @exec context and don't need to be
3513 * deallocated.
3514 *
3515 * Returns: 0 in case of success or -1 in case of error.
3516 */
3517int
3518xmlRegExecErrInfo(xmlRegExecCtxtPtr exec, const xmlChar **string,
Daniel Veillardcc026dc2005-01-12 13:21:17 +00003519 int *nbval, int *nbneg, xmlChar **values, int *terminal) {
Daniel Veillardfc0b6f62005-01-09 17:48:02 +00003520 if (exec == NULL)
3521 return(-1);
3522 if (string != NULL) {
3523 if (exec->status != 0)
3524 *string = exec->errString;
3525 else
3526 *string = NULL;
3527 }
Daniel Veillardcc026dc2005-01-12 13:21:17 +00003528 return(xmlRegExecGetValues(exec, 1, nbval, nbneg, values, terminal));
Daniel Veillardfc0b6f62005-01-09 17:48:02 +00003529}
3530
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003531#ifdef DEBUG_ERR
3532static void testerr(xmlRegExecCtxtPtr exec) {
3533 const xmlChar *string;
Daniel Veillardcee2b3a2005-01-25 00:22:52 +00003534 xmlChar *values[5];
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003535 int nb = 5;
Daniel Veillardcc026dc2005-01-12 13:21:17 +00003536 int nbneg;
Daniel Veillardfc0b6f62005-01-09 17:48:02 +00003537 int terminal;
Daniel Veillardcc026dc2005-01-12 13:21:17 +00003538 xmlRegExecErrInfo(exec, &string, &nb, &nbneg, &values[0], &terminal);
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003539}
3540#endif
3541
Daniel Veillard4255d502002-04-16 15:50:10 +00003542#if 0
3543static int
3544xmlRegExecPushChar(xmlRegExecCtxtPtr exec, int UCS) {
3545 xmlRegTransPtr trans;
3546 xmlRegAtomPtr atom;
3547 int ret;
3548 int codepoint, len;
3549
3550 if (exec == NULL)
3551 return(-1);
3552 if (exec->status != 0)
3553 return(exec->status);
3554
3555 while ((exec->status == 0) &&
3556 ((exec->inputString[exec->index] != 0) ||
3557 (exec->state->type != XML_REGEXP_FINAL_STATE))) {
3558
3559 /*
3560 * End of input on non-terminal state, rollback, however we may
3561 * still have epsilon like transition for counted transitions
3562 * on counters, in that case don't break too early.
3563 */
3564 if ((exec->inputString[exec->index] == 0) && (exec->counts == NULL))
3565 goto rollback;
3566
3567 exec->transcount = 0;
3568 for (;exec->transno < exec->state->nbTrans;exec->transno++) {
3569 trans = &exec->state->trans[exec->transno];
3570 if (trans->to < 0)
3571 continue;
3572 atom = trans->atom;
3573 ret = 0;
3574 if (trans->count >= 0) {
3575 int count;
3576 xmlRegCounterPtr counter;
3577
3578 /*
3579 * A counted transition.
3580 */
3581
3582 count = exec->counts[trans->count];
3583 counter = &exec->comp->counters[trans->count];
3584#ifdef DEBUG_REGEXP_EXEC
3585 printf("testing count %d: val %d, min %d, max %d\n",
3586 trans->count, count, counter->min, counter->max);
3587#endif
3588 ret = ((count >= counter->min) && (count <= counter->max));
3589 } else if (atom == NULL) {
3590 fprintf(stderr, "epsilon transition left at runtime\n");
3591 exec->status = -2;
3592 break;
3593 } else if (exec->inputString[exec->index] != 0) {
3594 codepoint = CUR_SCHAR(&(exec->inputString[exec->index]), len);
3595 ret = xmlRegCheckCharacter(atom, codepoint);
3596 if ((ret == 1) && (atom->min > 0) && (atom->max > 0)) {
3597 xmlRegStatePtr to = exec->comp->states[trans->to];
3598
3599 /*
3600 * this is a multiple input sequence
3601 */
3602 if (exec->state->nbTrans > exec->transno + 1) {
3603 xmlFARegExecSave(exec);
3604 }
3605 exec->transcount = 1;
3606 do {
3607 /*
3608 * Try to progress as much as possible on the input
3609 */
3610 if (exec->transcount == atom->max) {
3611 break;
3612 }
3613 exec->index += len;
3614 /*
3615 * End of input: stop here
3616 */
3617 if (exec->inputString[exec->index] == 0) {
3618 exec->index -= len;
3619 break;
3620 }
3621 if (exec->transcount >= atom->min) {
3622 int transno = exec->transno;
3623 xmlRegStatePtr state = exec->state;
3624
3625 /*
3626 * The transition is acceptable save it
3627 */
3628 exec->transno = -1; /* trick */
3629 exec->state = to;
3630 xmlFARegExecSave(exec);
3631 exec->transno = transno;
3632 exec->state = state;
3633 }
3634 codepoint = CUR_SCHAR(&(exec->inputString[exec->index]),
3635 len);
3636 ret = xmlRegCheckCharacter(atom, codepoint);
3637 exec->transcount++;
3638 } while (ret == 1);
3639 if (exec->transcount < atom->min)
3640 ret = 0;
3641
3642 /*
3643 * If the last check failed but one transition was found
3644 * possible, rollback
3645 */
3646 if (ret < 0)
3647 ret = 0;
3648 if (ret == 0) {
3649 goto rollback;
3650 }
3651 }
3652 }
3653 if (ret == 1) {
3654 if (exec->state->nbTrans > exec->transno + 1) {
3655 xmlFARegExecSave(exec);
3656 }
3657 if (trans->counter >= 0) {
3658#ifdef DEBUG_REGEXP_EXEC
3659 printf("Increasing count %d\n", trans->counter);
3660#endif
3661 exec->counts[trans->counter]++;
3662 }
3663#ifdef DEBUG_REGEXP_EXEC
3664 printf("entering state %d\n", trans->to);
3665#endif
3666 exec->state = exec->comp->states[trans->to];
3667 exec->transno = 0;
3668 if (trans->atom != NULL) {
3669 exec->index += len;
3670 }
3671 goto progress;
3672 } else if (ret < 0) {
3673 exec->status = -4;
3674 break;
3675 }
3676 }
3677 if ((exec->transno != 0) || (exec->state->nbTrans == 0)) {
3678rollback:
3679 /*
3680 * Failed to find a way out
3681 */
3682 exec->determinist = 0;
3683 xmlFARegExecRollBack(exec);
3684 }
3685progress:
3686 continue;
3687 }
3688}
3689#endif
3690/************************************************************************
3691 * *
William M. Brackddf71d62004-05-06 04:17:26 +00003692 * Parser for the Schemas Datatype Regular Expressions *
Daniel Veillard4255d502002-04-16 15:50:10 +00003693 * http://www.w3.org/TR/2001/REC-xmlschema-2-20010502/#regexs *
3694 * *
3695 ************************************************************************/
3696
3697/**
3698 * xmlFAIsChar:
Daniel Veillard441bc322002-04-20 17:38:48 +00003699 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00003700 *
3701 * [10] Char ::= [^.\?*+()|#x5B#x5D]
3702 */
3703static int
3704xmlFAIsChar(xmlRegParserCtxtPtr ctxt) {
3705 int cur;
3706 int len;
3707
3708 cur = CUR_SCHAR(ctxt->cur, len);
3709 if ((cur == '.') || (cur == '\\') || (cur == '?') ||
3710 (cur == '*') || (cur == '+') || (cur == '(') ||
3711 (cur == ')') || (cur == '|') || (cur == 0x5B) ||
3712 (cur == 0x5D) || (cur == 0))
3713 return(-1);
3714 return(cur);
3715}
3716
3717/**
3718 * xmlFAParseCharProp:
Daniel Veillard441bc322002-04-20 17:38:48 +00003719 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00003720 *
3721 * [27] charProp ::= IsCategory | IsBlock
3722 * [28] IsCategory ::= Letters | Marks | Numbers | Punctuation |
3723 * Separators | Symbols | Others
3724 * [29] Letters ::= 'L' [ultmo]?
3725 * [30] Marks ::= 'M' [nce]?
3726 * [31] Numbers ::= 'N' [dlo]?
3727 * [32] Punctuation ::= 'P' [cdseifo]?
3728 * [33] Separators ::= 'Z' [slp]?
3729 * [34] Symbols ::= 'S' [mcko]?
3730 * [35] Others ::= 'C' [cfon]?
3731 * [36] IsBlock ::= 'Is' [a-zA-Z0-9#x2D]+
3732 */
3733static void
3734xmlFAParseCharProp(xmlRegParserCtxtPtr ctxt) {
3735 int cur;
William M. Brack779af002003-08-01 15:55:39 +00003736 xmlRegAtomType type = (xmlRegAtomType) 0;
Daniel Veillard4255d502002-04-16 15:50:10 +00003737 xmlChar *blockName = NULL;
3738
3739 cur = CUR;
3740 if (cur == 'L') {
3741 NEXT;
3742 cur = CUR;
3743 if (cur == 'u') {
3744 NEXT;
3745 type = XML_REGEXP_LETTER_UPPERCASE;
3746 } else if (cur == 'l') {
3747 NEXT;
3748 type = XML_REGEXP_LETTER_LOWERCASE;
3749 } else if (cur == 't') {
3750 NEXT;
3751 type = XML_REGEXP_LETTER_TITLECASE;
3752 } else if (cur == 'm') {
3753 NEXT;
3754 type = XML_REGEXP_LETTER_MODIFIER;
3755 } else if (cur == 'o') {
3756 NEXT;
3757 type = XML_REGEXP_LETTER_OTHERS;
3758 } else {
3759 type = XML_REGEXP_LETTER;
3760 }
3761 } else if (cur == 'M') {
3762 NEXT;
3763 cur = CUR;
3764 if (cur == 'n') {
3765 NEXT;
3766 /* nonspacing */
3767 type = XML_REGEXP_MARK_NONSPACING;
3768 } else if (cur == 'c') {
3769 NEXT;
3770 /* spacing combining */
3771 type = XML_REGEXP_MARK_SPACECOMBINING;
3772 } else if (cur == 'e') {
3773 NEXT;
3774 /* enclosing */
3775 type = XML_REGEXP_MARK_ENCLOSING;
3776 } else {
3777 /* all marks */
3778 type = XML_REGEXP_MARK;
3779 }
3780 } else if (cur == 'N') {
3781 NEXT;
3782 cur = CUR;
3783 if (cur == 'd') {
3784 NEXT;
3785 /* digital */
3786 type = XML_REGEXP_NUMBER_DECIMAL;
3787 } else if (cur == 'l') {
3788 NEXT;
3789 /* letter */
3790 type = XML_REGEXP_NUMBER_LETTER;
3791 } else if (cur == 'o') {
3792 NEXT;
3793 /* other */
3794 type = XML_REGEXP_NUMBER_OTHERS;
3795 } else {
3796 /* all numbers */
3797 type = XML_REGEXP_NUMBER;
3798 }
3799 } else if (cur == 'P') {
3800 NEXT;
3801 cur = CUR;
3802 if (cur == 'c') {
3803 NEXT;
3804 /* connector */
3805 type = XML_REGEXP_PUNCT_CONNECTOR;
3806 } else if (cur == 'd') {
3807 NEXT;
3808 /* dash */
3809 type = XML_REGEXP_PUNCT_DASH;
3810 } else if (cur == 's') {
3811 NEXT;
3812 /* open */
3813 type = XML_REGEXP_PUNCT_OPEN;
3814 } else if (cur == 'e') {
3815 NEXT;
3816 /* close */
3817 type = XML_REGEXP_PUNCT_CLOSE;
3818 } else if (cur == 'i') {
3819 NEXT;
3820 /* initial quote */
3821 type = XML_REGEXP_PUNCT_INITQUOTE;
3822 } else if (cur == 'f') {
3823 NEXT;
3824 /* final quote */
3825 type = XML_REGEXP_PUNCT_FINQUOTE;
3826 } else if (cur == 'o') {
3827 NEXT;
3828 /* other */
3829 type = XML_REGEXP_PUNCT_OTHERS;
3830 } else {
3831 /* all punctuation */
3832 type = XML_REGEXP_PUNCT;
3833 }
3834 } else if (cur == 'Z') {
3835 NEXT;
3836 cur = CUR;
3837 if (cur == 's') {
3838 NEXT;
3839 /* space */
3840 type = XML_REGEXP_SEPAR_SPACE;
3841 } else if (cur == 'l') {
3842 NEXT;
3843 /* line */
3844 type = XML_REGEXP_SEPAR_LINE;
3845 } else if (cur == 'p') {
3846 NEXT;
3847 /* paragraph */
3848 type = XML_REGEXP_SEPAR_PARA;
3849 } else {
3850 /* all separators */
3851 type = XML_REGEXP_SEPAR;
3852 }
3853 } else if (cur == 'S') {
3854 NEXT;
3855 cur = CUR;
3856 if (cur == 'm') {
3857 NEXT;
3858 type = XML_REGEXP_SYMBOL_MATH;
3859 /* math */
3860 } else if (cur == 'c') {
3861 NEXT;
3862 type = XML_REGEXP_SYMBOL_CURRENCY;
3863 /* currency */
3864 } else if (cur == 'k') {
3865 NEXT;
3866 type = XML_REGEXP_SYMBOL_MODIFIER;
3867 /* modifiers */
3868 } else if (cur == 'o') {
3869 NEXT;
3870 type = XML_REGEXP_SYMBOL_OTHERS;
3871 /* other */
3872 } else {
3873 /* all symbols */
3874 type = XML_REGEXP_SYMBOL;
3875 }
3876 } else if (cur == 'C') {
3877 NEXT;
3878 cur = CUR;
3879 if (cur == 'c') {
3880 NEXT;
3881 /* control */
3882 type = XML_REGEXP_OTHER_CONTROL;
3883 } else if (cur == 'f') {
3884 NEXT;
3885 /* format */
3886 type = XML_REGEXP_OTHER_FORMAT;
3887 } else if (cur == 'o') {
3888 NEXT;
3889 /* private use */
3890 type = XML_REGEXP_OTHER_PRIVATE;
3891 } else if (cur == 'n') {
3892 NEXT;
3893 /* not assigned */
3894 type = XML_REGEXP_OTHER_NA;
3895 } else {
3896 /* all others */
3897 type = XML_REGEXP_OTHER;
3898 }
3899 } else if (cur == 'I') {
3900 const xmlChar *start;
3901 NEXT;
3902 cur = CUR;
3903 if (cur != 's') {
3904 ERROR("IsXXXX expected");
3905 return;
3906 }
3907 NEXT;
3908 start = ctxt->cur;
3909 cur = CUR;
3910 if (((cur >= 'a') && (cur <= 'z')) ||
3911 ((cur >= 'A') && (cur <= 'Z')) ||
3912 ((cur >= '0') && (cur <= '9')) ||
3913 (cur == 0x2D)) {
3914 NEXT;
3915 cur = CUR;
3916 while (((cur >= 'a') && (cur <= 'z')) ||
3917 ((cur >= 'A') && (cur <= 'Z')) ||
3918 ((cur >= '0') && (cur <= '9')) ||
3919 (cur == 0x2D)) {
3920 NEXT;
3921 cur = CUR;
3922 }
3923 }
3924 type = XML_REGEXP_BLOCK_NAME;
3925 blockName = xmlStrndup(start, ctxt->cur - start);
3926 } else {
3927 ERROR("Unknown char property");
3928 return;
3929 }
3930 if (ctxt->atom == NULL) {
3931 ctxt->atom = xmlRegNewAtom(ctxt, type);
3932 if (ctxt->atom != NULL)
3933 ctxt->atom->valuep = blockName;
3934 } else if (ctxt->atom->type == XML_REGEXP_RANGES) {
3935 xmlRegAtomAddRange(ctxt, ctxt->atom, ctxt->neg,
3936 type, 0, 0, blockName);
3937 }
3938}
3939
3940/**
3941 * xmlFAParseCharClassEsc:
Daniel Veillard441bc322002-04-20 17:38:48 +00003942 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00003943 *
3944 * [23] charClassEsc ::= ( SingleCharEsc | MultiCharEsc | catEsc | complEsc )
3945 * [24] SingleCharEsc ::= '\' [nrt\|.?*+(){}#x2D#x5B#x5D#x5E]
3946 * [25] catEsc ::= '\p{' charProp '}'
3947 * [26] complEsc ::= '\P{' charProp '}'
3948 * [37] MultiCharEsc ::= '.' | ('\' [sSiIcCdDwW])
3949 */
3950static void
3951xmlFAParseCharClassEsc(xmlRegParserCtxtPtr ctxt) {
3952 int cur;
3953
3954 if (CUR == '.') {
3955 if (ctxt->atom == NULL) {
3956 ctxt->atom = xmlRegNewAtom(ctxt, XML_REGEXP_ANYCHAR);
3957 } else if (ctxt->atom->type == XML_REGEXP_RANGES) {
3958 xmlRegAtomAddRange(ctxt, ctxt->atom, ctxt->neg,
3959 XML_REGEXP_ANYCHAR, 0, 0, NULL);
3960 }
3961 NEXT;
3962 return;
3963 }
3964 if (CUR != '\\') {
3965 ERROR("Escaped sequence: expecting \\");
3966 return;
3967 }
3968 NEXT;
3969 cur = CUR;
3970 if (cur == 'p') {
3971 NEXT;
3972 if (CUR != '{') {
3973 ERROR("Expecting '{'");
3974 return;
3975 }
3976 NEXT;
3977 xmlFAParseCharProp(ctxt);
3978 if (CUR != '}') {
3979 ERROR("Expecting '}'");
3980 return;
3981 }
3982 NEXT;
3983 } else if (cur == 'P') {
3984 NEXT;
3985 if (CUR != '{') {
3986 ERROR("Expecting '{'");
3987 return;
3988 }
3989 NEXT;
3990 xmlFAParseCharProp(ctxt);
3991 ctxt->atom->neg = 1;
3992 if (CUR != '}') {
3993 ERROR("Expecting '}'");
3994 return;
3995 }
3996 NEXT;
3997 } else if ((cur == 'n') || (cur == 'r') || (cur == 't') || (cur == '\\') ||
3998 (cur == '|') || (cur == '.') || (cur == '?') || (cur == '*') ||
3999 (cur == '+') || (cur == '(') || (cur == ')') || (cur == '{') ||
4000 (cur == '}') || (cur == 0x2D) || (cur == 0x5B) || (cur == 0x5D) ||
4001 (cur == 0x5E)) {
4002 if (ctxt->atom == NULL) {
4003 ctxt->atom = xmlRegNewAtom(ctxt, XML_REGEXP_CHARVAL);
Daniel Veillard99c394d2005-07-14 12:58:49 +00004004 if (ctxt->atom != NULL) {
4005 switch (cur) {
4006 case 'n':
4007 ctxt->atom->codepoint = '\n';
4008 break;
4009 case 'r':
4010 ctxt->atom->codepoint = '\r';
4011 break;
4012 case 't':
4013 ctxt->atom->codepoint = '\t';
4014 break;
4015 default:
4016 ctxt->atom->codepoint = cur;
4017 }
4018 }
Daniel Veillard4255d502002-04-16 15:50:10 +00004019 } else if (ctxt->atom->type == XML_REGEXP_RANGES) {
4020 xmlRegAtomAddRange(ctxt, ctxt->atom, ctxt->neg,
4021 XML_REGEXP_CHARVAL, cur, cur, NULL);
4022 }
4023 NEXT;
4024 } else if ((cur == 's') || (cur == 'S') || (cur == 'i') || (cur == 'I') ||
4025 (cur == 'c') || (cur == 'C') || (cur == 'd') || (cur == 'D') ||
4026 (cur == 'w') || (cur == 'W')) {
Daniel Veillardb509f152002-04-17 16:28:10 +00004027 xmlRegAtomType type = XML_REGEXP_ANYSPACE;
Daniel Veillard4255d502002-04-16 15:50:10 +00004028
4029 switch (cur) {
4030 case 's':
4031 type = XML_REGEXP_ANYSPACE;
4032 break;
4033 case 'S':
4034 type = XML_REGEXP_NOTSPACE;
4035 break;
4036 case 'i':
4037 type = XML_REGEXP_INITNAME;
4038 break;
4039 case 'I':
4040 type = XML_REGEXP_NOTINITNAME;
4041 break;
4042 case 'c':
4043 type = XML_REGEXP_NAMECHAR;
4044 break;
4045 case 'C':
4046 type = XML_REGEXP_NOTNAMECHAR;
4047 break;
4048 case 'd':
4049 type = XML_REGEXP_DECIMAL;
4050 break;
4051 case 'D':
4052 type = XML_REGEXP_NOTDECIMAL;
4053 break;
4054 case 'w':
4055 type = XML_REGEXP_REALCHAR;
4056 break;
4057 case 'W':
4058 type = XML_REGEXP_NOTREALCHAR;
4059 break;
4060 }
4061 NEXT;
4062 if (ctxt->atom == NULL) {
4063 ctxt->atom = xmlRegNewAtom(ctxt, type);
4064 } else if (ctxt->atom->type == XML_REGEXP_RANGES) {
4065 xmlRegAtomAddRange(ctxt, ctxt->atom, ctxt->neg,
4066 type, 0, 0, NULL);
4067 }
4068 }
4069}
4070
4071/**
4072 * xmlFAParseCharRef:
Daniel Veillard441bc322002-04-20 17:38:48 +00004073 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00004074 *
4075 * [19] XmlCharRef ::= ( '&#' [0-9]+ ';' ) | (' &#x' [0-9a-fA-F]+ ';' )
4076 */
4077static int
4078xmlFAParseCharRef(xmlRegParserCtxtPtr ctxt) {
4079 int ret = 0, cur;
4080
4081 if ((CUR != '&') || (NXT(1) != '#'))
4082 return(-1);
4083 NEXT;
4084 NEXT;
4085 cur = CUR;
4086 if (cur == 'x') {
4087 NEXT;
4088 cur = CUR;
4089 if (((cur >= '0') && (cur <= '9')) ||
4090 ((cur >= 'a') && (cur <= 'f')) ||
4091 ((cur >= 'A') && (cur <= 'F'))) {
4092 while (((cur >= '0') && (cur <= '9')) ||
4093 ((cur >= 'A') && (cur <= 'F'))) {
4094 if ((cur >= '0') && (cur <= '9'))
4095 ret = ret * 16 + cur - '0';
4096 else if ((cur >= 'a') && (cur <= 'f'))
4097 ret = ret * 16 + 10 + (cur - 'a');
4098 else
4099 ret = ret * 16 + 10 + (cur - 'A');
4100 NEXT;
4101 cur = CUR;
4102 }
4103 } else {
4104 ERROR("Char ref: expecting [0-9A-F]");
4105 return(-1);
4106 }
4107 } else {
4108 if ((cur >= '0') && (cur <= '9')) {
4109 while ((cur >= '0') && (cur <= '9')) {
4110 ret = ret * 10 + cur - '0';
4111 NEXT;
4112 cur = CUR;
4113 }
4114 } else {
4115 ERROR("Char ref: expecting [0-9]");
4116 return(-1);
4117 }
4118 }
4119 if (cur != ';') {
4120 ERROR("Char ref: expecting ';'");
4121 return(-1);
4122 } else {
4123 NEXT;
4124 }
4125 return(ret);
4126}
4127
4128/**
4129 * xmlFAParseCharRange:
Daniel Veillard441bc322002-04-20 17:38:48 +00004130 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00004131 *
4132 * [17] charRange ::= seRange | XmlCharRef | XmlCharIncDash
4133 * [18] seRange ::= charOrEsc '-' charOrEsc
4134 * [20] charOrEsc ::= XmlChar | SingleCharEsc
4135 * [21] XmlChar ::= [^\#x2D#x5B#x5D]
4136 * [22] XmlCharIncDash ::= [^\#x5B#x5D]
4137 */
4138static void
4139xmlFAParseCharRange(xmlRegParserCtxtPtr ctxt) {
William M. Brackdc99df92003-12-27 01:54:25 +00004140 int cur, len;
Daniel Veillard4255d502002-04-16 15:50:10 +00004141 int start = -1;
4142 int end = -1;
4143
4144 if ((CUR == '&') && (NXT(1) == '#')) {
4145 end = start = xmlFAParseCharRef(ctxt);
4146 xmlRegAtomAddRange(ctxt, ctxt->atom, ctxt->neg,
4147 XML_REGEXP_CHARVAL, start, end, NULL);
4148 return;
4149 }
4150 cur = CUR;
4151 if (cur == '\\') {
4152 NEXT;
4153 cur = CUR;
4154 switch (cur) {
4155 case 'n': start = 0xA; break;
4156 case 'r': start = 0xD; break;
4157 case 't': start = 0x9; break;
4158 case '\\': case '|': case '.': case '-': case '^': case '?':
4159 case '*': case '+': case '{': case '}': case '(': case ')':
4160 case '[': case ']':
4161 start = cur; break;
4162 default:
4163 ERROR("Invalid escape value");
4164 return;
4165 }
4166 end = start;
William M. Brackdc99df92003-12-27 01:54:25 +00004167 len = 1;
Daniel Veillard4255d502002-04-16 15:50:10 +00004168 } else if ((cur != 0x5B) && (cur != 0x5D)) {
William M. Brackdc99df92003-12-27 01:54:25 +00004169 end = start = CUR_SCHAR(ctxt->cur, len);
Daniel Veillard4255d502002-04-16 15:50:10 +00004170 } else {
4171 ERROR("Expecting a char range");
4172 return;
4173 }
William M. Brackdc99df92003-12-27 01:54:25 +00004174 NEXTL(len);
Daniel Veillard4255d502002-04-16 15:50:10 +00004175 if (start == '-') {
4176 return;
4177 }
4178 cur = CUR;
William M. Brack10f1ef42004-03-20 14:51:25 +00004179 if ((cur != '-') || (NXT(1) == ']')) {
Daniel Veillard4255d502002-04-16 15:50:10 +00004180 xmlRegAtomAddRange(ctxt, ctxt->atom, ctxt->neg,
4181 XML_REGEXP_CHARVAL, start, end, NULL);
4182 return;
4183 }
4184 NEXT;
4185 cur = CUR;
4186 if (cur == '\\') {
4187 NEXT;
4188 cur = CUR;
4189 switch (cur) {
4190 case 'n': end = 0xA; break;
4191 case 'r': end = 0xD; break;
4192 case 't': end = 0x9; break;
4193 case '\\': case '|': case '.': case '-': case '^': case '?':
4194 case '*': case '+': case '{': case '}': case '(': case ')':
4195 case '[': case ']':
4196 end = cur; break;
4197 default:
4198 ERROR("Invalid escape value");
4199 return;
4200 }
William M. Brackdc99df92003-12-27 01:54:25 +00004201 len = 1;
Daniel Veillard4255d502002-04-16 15:50:10 +00004202 } else if ((cur != 0x5B) && (cur != 0x5D)) {
William M. Brackdc99df92003-12-27 01:54:25 +00004203 end = CUR_SCHAR(ctxt->cur, len);
Daniel Veillard4255d502002-04-16 15:50:10 +00004204 } else {
4205 ERROR("Expecting the end of a char range");
4206 return;
4207 }
William M. Brackdc99df92003-12-27 01:54:25 +00004208 NEXTL(len);
Daniel Veillard4255d502002-04-16 15:50:10 +00004209 /* TODO check that the values are acceptable character ranges for XML */
4210 if (end < start) {
4211 ERROR("End of range is before start of range");
4212 } else {
4213 xmlRegAtomAddRange(ctxt, ctxt->atom, ctxt->neg,
4214 XML_REGEXP_CHARVAL, start, end, NULL);
4215 }
4216 return;
4217}
4218
4219/**
4220 * xmlFAParsePosCharGroup:
Daniel Veillard441bc322002-04-20 17:38:48 +00004221 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00004222 *
4223 * [14] posCharGroup ::= ( charRange | charClassEsc )+
4224 */
4225static void
4226xmlFAParsePosCharGroup(xmlRegParserCtxtPtr ctxt) {
4227 do {
4228 if ((CUR == '\\') || (CUR == '.')) {
4229 xmlFAParseCharClassEsc(ctxt);
4230 } else {
4231 xmlFAParseCharRange(ctxt);
4232 }
4233 } while ((CUR != ']') && (CUR != '^') && (CUR != '-') &&
4234 (ctxt->error == 0));
4235}
4236
4237/**
4238 * xmlFAParseCharGroup:
Daniel Veillard441bc322002-04-20 17:38:48 +00004239 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00004240 *
4241 * [13] charGroup ::= posCharGroup | negCharGroup | charClassSub
4242 * [15] negCharGroup ::= '^' posCharGroup
4243 * [16] charClassSub ::= ( posCharGroup | negCharGroup ) '-' charClassExpr
4244 * [12] charClassExpr ::= '[' charGroup ']'
4245 */
4246static void
4247xmlFAParseCharGroup(xmlRegParserCtxtPtr ctxt) {
4248 int n = ctxt->neg;
4249 while ((CUR != ']') && (ctxt->error == 0)) {
4250 if (CUR == '^') {
4251 int neg = ctxt->neg;
4252
4253 NEXT;
4254 ctxt->neg = !ctxt->neg;
4255 xmlFAParsePosCharGroup(ctxt);
4256 ctxt->neg = neg;
William M. Brack10f1ef42004-03-20 14:51:25 +00004257 } else if ((CUR == '-') && (NXT(1) == '[')) {
Daniel Veillardf8b9de32003-11-24 14:27:26 +00004258 int neg = ctxt->neg;
Daniel Veillardf8b9de32003-11-24 14:27:26 +00004259 ctxt->neg = 2;
William M. Brack10f1ef42004-03-20 14:51:25 +00004260 NEXT; /* eat the '-' */
4261 NEXT; /* eat the '[' */
Daniel Veillard4255d502002-04-16 15:50:10 +00004262 xmlFAParseCharGroup(ctxt);
4263 if (CUR == ']') {
4264 NEXT;
4265 } else {
4266 ERROR("charClassExpr: ']' expected");
4267 break;
4268 }
Daniel Veillardf8b9de32003-11-24 14:27:26 +00004269 ctxt->neg = neg;
Daniel Veillard4255d502002-04-16 15:50:10 +00004270 break;
4271 } else if (CUR != ']') {
4272 xmlFAParsePosCharGroup(ctxt);
4273 }
4274 }
4275 ctxt->neg = n;
4276}
4277
4278/**
4279 * xmlFAParseCharClass:
Daniel Veillard441bc322002-04-20 17:38:48 +00004280 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00004281 *
4282 * [11] charClass ::= charClassEsc | charClassExpr
4283 * [12] charClassExpr ::= '[' charGroup ']'
4284 */
4285static void
4286xmlFAParseCharClass(xmlRegParserCtxtPtr ctxt) {
4287 if (CUR == '[') {
4288 NEXT;
4289 ctxt->atom = xmlRegNewAtom(ctxt, XML_REGEXP_RANGES);
4290 if (ctxt->atom == NULL)
4291 return;
4292 xmlFAParseCharGroup(ctxt);
4293 if (CUR == ']') {
4294 NEXT;
4295 } else {
4296 ERROR("xmlFAParseCharClass: ']' expected");
4297 }
4298 } else {
4299 xmlFAParseCharClassEsc(ctxt);
4300 }
4301}
4302
4303/**
4304 * xmlFAParseQuantExact:
Daniel Veillard441bc322002-04-20 17:38:48 +00004305 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00004306 *
4307 * [8] QuantExact ::= [0-9]+
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00004308 *
4309 * Returns 0 if success or -1 in case of error
Daniel Veillard4255d502002-04-16 15:50:10 +00004310 */
4311static int
4312xmlFAParseQuantExact(xmlRegParserCtxtPtr ctxt) {
4313 int ret = 0;
4314 int ok = 0;
4315
4316 while ((CUR >= '0') && (CUR <= '9')) {
4317 ret = ret * 10 + (CUR - '0');
4318 ok = 1;
4319 NEXT;
4320 }
4321 if (ok != 1) {
4322 return(-1);
4323 }
4324 return(ret);
4325}
4326
4327/**
4328 * xmlFAParseQuantifier:
Daniel Veillard441bc322002-04-20 17:38:48 +00004329 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00004330 *
4331 * [4] quantifier ::= [?*+] | ( '{' quantity '}' )
4332 * [5] quantity ::= quantRange | quantMin | QuantExact
4333 * [6] quantRange ::= QuantExact ',' QuantExact
4334 * [7] quantMin ::= QuantExact ','
4335 * [8] QuantExact ::= [0-9]+
4336 */
4337static int
4338xmlFAParseQuantifier(xmlRegParserCtxtPtr ctxt) {
4339 int cur;
4340
4341 cur = CUR;
4342 if ((cur == '?') || (cur == '*') || (cur == '+')) {
4343 if (ctxt->atom != NULL) {
4344 if (cur == '?')
4345 ctxt->atom->quant = XML_REGEXP_QUANT_OPT;
4346 else if (cur == '*')
4347 ctxt->atom->quant = XML_REGEXP_QUANT_MULT;
4348 else if (cur == '+')
4349 ctxt->atom->quant = XML_REGEXP_QUANT_PLUS;
4350 }
4351 NEXT;
4352 return(1);
4353 }
4354 if (cur == '{') {
4355 int min = 0, max = 0;
4356
4357 NEXT;
4358 cur = xmlFAParseQuantExact(ctxt);
4359 if (cur >= 0)
4360 min = cur;
4361 if (CUR == ',') {
4362 NEXT;
Daniel Veillardebe48c62003-12-03 12:12:27 +00004363 if (CUR == '}')
4364 max = INT_MAX;
4365 else {
4366 cur = xmlFAParseQuantExact(ctxt);
4367 if (cur >= 0)
4368 max = cur;
4369 else {
4370 ERROR("Improper quantifier");
4371 }
4372 }
Daniel Veillard4255d502002-04-16 15:50:10 +00004373 }
4374 if (CUR == '}') {
4375 NEXT;
4376 } else {
4377 ERROR("Unterminated quantifier");
4378 }
4379 if (max == 0)
4380 max = min;
4381 if (ctxt->atom != NULL) {
4382 ctxt->atom->quant = XML_REGEXP_QUANT_RANGE;
4383 ctxt->atom->min = min;
4384 ctxt->atom->max = max;
4385 }
4386 return(1);
4387 }
4388 return(0);
4389}
4390
4391/**
4392 * xmlFAParseAtom:
Daniel Veillard441bc322002-04-20 17:38:48 +00004393 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00004394 *
4395 * [9] atom ::= Char | charClass | ( '(' regExp ')' )
4396 */
4397static int
4398xmlFAParseAtom(xmlRegParserCtxtPtr ctxt) {
4399 int codepoint, len;
4400
4401 codepoint = xmlFAIsChar(ctxt);
4402 if (codepoint > 0) {
4403 ctxt->atom = xmlRegNewAtom(ctxt, XML_REGEXP_CHARVAL);
4404 if (ctxt->atom == NULL)
4405 return(-1);
4406 codepoint = CUR_SCHAR(ctxt->cur, len);
4407 ctxt->atom->codepoint = codepoint;
4408 NEXTL(len);
4409 return(1);
4410 } else if (CUR == '|') {
4411 return(0);
4412 } else if (CUR == 0) {
4413 return(0);
4414 } else if (CUR == ')') {
4415 return(0);
4416 } else if (CUR == '(') {
4417 xmlRegStatePtr start, oldend;
4418
4419 NEXT;
4420 xmlFAGenerateEpsilonTransition(ctxt, ctxt->state, NULL);
4421 start = ctxt->state;
4422 oldend = ctxt->end;
4423 ctxt->end = NULL;
4424 ctxt->atom = NULL;
4425 xmlFAParseRegExp(ctxt, 0);
4426 if (CUR == ')') {
4427 NEXT;
4428 } else {
4429 ERROR("xmlFAParseAtom: expecting ')'");
4430 }
4431 ctxt->atom = xmlRegNewAtom(ctxt, XML_REGEXP_SUBREG);
4432 if (ctxt->atom == NULL)
4433 return(-1);
4434 ctxt->atom->start = start;
4435 ctxt->atom->stop = ctxt->state;
4436 ctxt->end = oldend;
4437 return(1);
4438 } else if ((CUR == '[') || (CUR == '\\') || (CUR == '.')) {
4439 xmlFAParseCharClass(ctxt);
4440 return(1);
4441 }
4442 return(0);
4443}
4444
4445/**
4446 * xmlFAParsePiece:
Daniel Veillard441bc322002-04-20 17:38:48 +00004447 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00004448 *
4449 * [3] piece ::= atom quantifier?
4450 */
4451static int
4452xmlFAParsePiece(xmlRegParserCtxtPtr ctxt) {
4453 int ret;
4454
4455 ctxt->atom = NULL;
4456 ret = xmlFAParseAtom(ctxt);
4457 if (ret == 0)
4458 return(0);
4459 if (ctxt->atom == NULL) {
4460 ERROR("internal: no atom generated");
4461 }
4462 xmlFAParseQuantifier(ctxt);
4463 return(1);
4464}
4465
4466/**
4467 * xmlFAParseBranch:
Daniel Veillard441bc322002-04-20 17:38:48 +00004468 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00004469 *
4470 * [2] branch ::= piece*
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00004471 8
Daniel Veillard4255d502002-04-16 15:50:10 +00004472 */
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00004473static int
Daniel Veillard2cbf5962004-03-31 15:50:43 +00004474xmlFAParseBranch(xmlRegParserCtxtPtr ctxt) {
Daniel Veillard4255d502002-04-16 15:50:10 +00004475 xmlRegStatePtr previous;
Daniel Veillard4255d502002-04-16 15:50:10 +00004476 int ret;
4477
4478 previous = ctxt->state;
4479 ret = xmlFAParsePiece(ctxt);
4480 if (ret != 0) {
Daniel Veillard2cbf5962004-03-31 15:50:43 +00004481 if (xmlFAGenerateTransitions(ctxt, previous, NULL, ctxt->atom) < 0)
4482 return(-1);
4483 previous = ctxt->state;
Daniel Veillard4255d502002-04-16 15:50:10 +00004484 ctxt->atom = NULL;
4485 }
4486 while ((ret != 0) && (ctxt->error == 0)) {
4487 ret = xmlFAParsePiece(ctxt);
4488 if (ret != 0) {
Daniel Veillard2cbf5962004-03-31 15:50:43 +00004489 if (xmlFAGenerateTransitions(ctxt, previous, NULL,
4490 ctxt->atom) < 0)
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00004491 return(-1);
Daniel Veillard4255d502002-04-16 15:50:10 +00004492 previous = ctxt->state;
4493 ctxt->atom = NULL;
4494 }
4495 }
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00004496 return(0);
Daniel Veillard4255d502002-04-16 15:50:10 +00004497}
4498
4499/**
4500 * xmlFAParseRegExp:
Daniel Veillard441bc322002-04-20 17:38:48 +00004501 * @ctxt: a regexp parser context
William M. Brackddf71d62004-05-06 04:17:26 +00004502 * @top: is this the top-level expression ?
Daniel Veillard4255d502002-04-16 15:50:10 +00004503 *
4504 * [1] regExp ::= branch ( '|' branch )*
4505 */
4506static void
4507xmlFAParseRegExp(xmlRegParserCtxtPtr ctxt, int top) {
Daniel Veillardc7e3cc42004-09-28 12:33:52 +00004508 xmlRegStatePtr start, end;
Daniel Veillard4255d502002-04-16 15:50:10 +00004509
Daniel Veillard2cbf5962004-03-31 15:50:43 +00004510 /* if not top start should have been generated by an epsilon trans */
Daniel Veillard4255d502002-04-16 15:50:10 +00004511 start = ctxt->state;
Daniel Veillard2cbf5962004-03-31 15:50:43 +00004512 ctxt->end = NULL;
4513 xmlFAParseBranch(ctxt);
4514 if (top) {
4515#ifdef DEBUG_REGEXP_GRAPH
4516 printf("State %d is final\n", ctxt->state->no);
4517#endif
4518 ctxt->state->type = XML_REGEXP_FINAL_STATE;
4519 }
Daniel Veillard4255d502002-04-16 15:50:10 +00004520 if (CUR != '|') {
4521 ctxt->end = ctxt->state;
4522 return;
4523 }
4524 end = ctxt->state;
4525 while ((CUR == '|') && (ctxt->error == 0)) {
4526 NEXT;
4527 ctxt->state = start;
Daniel Veillard2cbf5962004-03-31 15:50:43 +00004528 ctxt->end = NULL;
4529 xmlFAParseBranch(ctxt);
4530 if (top) {
4531 ctxt->state->type = XML_REGEXP_FINAL_STATE;
4532#ifdef DEBUG_REGEXP_GRAPH
4533 printf("State %d is final\n", ctxt->state->no);
4534#endif
4535 } else {
4536 xmlFAGenerateEpsilonTransition(ctxt, ctxt->state, end);
4537 }
Daniel Veillard4255d502002-04-16 15:50:10 +00004538 }
Daniel Veillard2cbf5962004-03-31 15:50:43 +00004539 if (!top) {
4540 ctxt->state = end;
4541 ctxt->end = end;
4542 }
Daniel Veillard4255d502002-04-16 15:50:10 +00004543}
4544
4545/************************************************************************
4546 * *
4547 * The basic API *
4548 * *
4549 ************************************************************************/
4550
4551/**
4552 * xmlRegexpPrint:
4553 * @output: the file for the output debug
4554 * @regexp: the compiled regexp
4555 *
4556 * Print the content of the compiled regular expression
4557 */
4558void
4559xmlRegexpPrint(FILE *output, xmlRegexpPtr regexp) {
4560 int i;
4561
Daniel Veillarda82b1822004-11-08 16:24:57 +00004562 if (output == NULL)
4563 return;
Daniel Veillard4255d502002-04-16 15:50:10 +00004564 fprintf(output, " regexp: ");
4565 if (regexp == NULL) {
4566 fprintf(output, "NULL\n");
4567 return;
4568 }
4569 fprintf(output, "'%s' ", regexp->string);
4570 fprintf(output, "\n");
4571 fprintf(output, "%d atoms:\n", regexp->nbAtoms);
4572 for (i = 0;i < regexp->nbAtoms; i++) {
4573 fprintf(output, " %02d ", i);
4574 xmlRegPrintAtom(output, regexp->atoms[i]);
4575 }
4576 fprintf(output, "%d states:", regexp->nbStates);
4577 fprintf(output, "\n");
4578 for (i = 0;i < regexp->nbStates; i++) {
4579 xmlRegPrintState(output, regexp->states[i]);
4580 }
4581 fprintf(output, "%d counters:\n", regexp->nbCounters);
4582 for (i = 0;i < regexp->nbCounters; i++) {
4583 fprintf(output, " %d: min %d max %d\n", i, regexp->counters[i].min,
4584 regexp->counters[i].max);
4585 }
4586}
4587
4588/**
4589 * xmlRegexpCompile:
4590 * @regexp: a regular expression string
4591 *
4592 * Parses a regular expression conforming to XML Schemas Part 2 Datatype
William M. Brackddf71d62004-05-06 04:17:26 +00004593 * Appendix F and builds an automata suitable for testing strings against
Daniel Veillard4255d502002-04-16 15:50:10 +00004594 * that regular expression
4595 *
4596 * Returns the compiled expression or NULL in case of error
4597 */
4598xmlRegexpPtr
4599xmlRegexpCompile(const xmlChar *regexp) {
4600 xmlRegexpPtr ret;
4601 xmlRegParserCtxtPtr ctxt;
4602
4603 ctxt = xmlRegNewParserCtxt(regexp);
4604 if (ctxt == NULL)
4605 return(NULL);
4606
4607 /* initialize the parser */
4608 ctxt->end = NULL;
4609 ctxt->start = ctxt->state = xmlRegNewState(ctxt);
4610 xmlRegStatePush(ctxt, ctxt->start);
4611
4612 /* parse the expression building an automata */
4613 xmlFAParseRegExp(ctxt, 1);
4614 if (CUR != 0) {
4615 ERROR("xmlFAParseRegExp: extra characters");
4616 }
4617 ctxt->end = ctxt->state;
4618 ctxt->start->type = XML_REGEXP_START_STATE;
4619 ctxt->end->type = XML_REGEXP_FINAL_STATE;
4620
4621 /* remove the Epsilon except for counted transitions */
4622 xmlFAEliminateEpsilonTransitions(ctxt);
4623
4624
4625 if (ctxt->error != 0) {
4626 xmlRegFreeParserCtxt(ctxt);
4627 return(NULL);
4628 }
4629 ret = xmlRegEpxFromParse(ctxt);
4630 xmlRegFreeParserCtxt(ctxt);
4631 return(ret);
4632}
4633
4634/**
4635 * xmlRegexpExec:
4636 * @comp: the compiled regular expression
4637 * @content: the value to check against the regular expression
4638 *
William M. Brackddf71d62004-05-06 04:17:26 +00004639 * Check if the regular expression generates the value
Daniel Veillard4255d502002-04-16 15:50:10 +00004640 *
William M. Brackddf71d62004-05-06 04:17:26 +00004641 * Returns 1 if it matches, 0 if not and a negative value in case of error
Daniel Veillard4255d502002-04-16 15:50:10 +00004642 */
4643int
4644xmlRegexpExec(xmlRegexpPtr comp, const xmlChar *content) {
4645 if ((comp == NULL) || (content == NULL))
4646 return(-1);
4647 return(xmlFARegExec(comp, content));
4648}
4649
4650/**
Daniel Veillard23e73572002-09-19 19:56:43 +00004651 * xmlRegexpIsDeterminist:
4652 * @comp: the compiled regular expression
4653 *
4654 * Check if the regular expression is determinist
4655 *
William M. Brackddf71d62004-05-06 04:17:26 +00004656 * Returns 1 if it yes, 0 if not and a negative value in case of error
Daniel Veillard23e73572002-09-19 19:56:43 +00004657 */
4658int
4659xmlRegexpIsDeterminist(xmlRegexpPtr comp) {
4660 xmlAutomataPtr am;
4661 int ret;
4662
4663 if (comp == NULL)
4664 return(-1);
4665 if (comp->determinist != -1)
4666 return(comp->determinist);
4667
4668 am = xmlNewAutomata();
Daniel Veillardbd9afb52002-09-25 22:25:35 +00004669 if (am->states != NULL) {
4670 int i;
4671
4672 for (i = 0;i < am->nbStates;i++)
4673 xmlRegFreeState(am->states[i]);
4674 xmlFree(am->states);
4675 }
Daniel Veillard23e73572002-09-19 19:56:43 +00004676 am->nbAtoms = comp->nbAtoms;
4677 am->atoms = comp->atoms;
4678 am->nbStates = comp->nbStates;
4679 am->states = comp->states;
4680 am->determinist = -1;
4681 ret = xmlFAComputesDeterminism(am);
4682 am->atoms = NULL;
4683 am->states = NULL;
4684 xmlFreeAutomata(am);
4685 return(ret);
4686}
4687
4688/**
Daniel Veillard4255d502002-04-16 15:50:10 +00004689 * xmlRegFreeRegexp:
4690 * @regexp: the regexp
4691 *
4692 * Free a regexp
4693 */
4694void
4695xmlRegFreeRegexp(xmlRegexpPtr regexp) {
4696 int i;
4697 if (regexp == NULL)
4698 return;
4699
4700 if (regexp->string != NULL)
4701 xmlFree(regexp->string);
4702 if (regexp->states != NULL) {
4703 for (i = 0;i < regexp->nbStates;i++)
4704 xmlRegFreeState(regexp->states[i]);
4705 xmlFree(regexp->states);
4706 }
4707 if (regexp->atoms != NULL) {
4708 for (i = 0;i < regexp->nbAtoms;i++)
4709 xmlRegFreeAtom(regexp->atoms[i]);
4710 xmlFree(regexp->atoms);
4711 }
4712 if (regexp->counters != NULL)
4713 xmlFree(regexp->counters);
Daniel Veillard23e73572002-09-19 19:56:43 +00004714 if (regexp->compact != NULL)
4715 xmlFree(regexp->compact);
Daniel Veillard118aed72002-09-24 14:13:13 +00004716 if (regexp->transdata != NULL)
4717 xmlFree(regexp->transdata);
Daniel Veillard23e73572002-09-19 19:56:43 +00004718 if (regexp->stringMap != NULL) {
4719 for (i = 0; i < regexp->nbstrings;i++)
4720 xmlFree(regexp->stringMap[i]);
4721 xmlFree(regexp->stringMap);
4722 }
4723
Daniel Veillard4255d502002-04-16 15:50:10 +00004724 xmlFree(regexp);
4725}
4726
4727#ifdef LIBXML_AUTOMATA_ENABLED
4728/************************************************************************
4729 * *
4730 * The Automata interface *
4731 * *
4732 ************************************************************************/
4733
4734/**
4735 * xmlNewAutomata:
4736 *
4737 * Create a new automata
4738 *
4739 * Returns the new object or NULL in case of failure
4740 */
4741xmlAutomataPtr
4742xmlNewAutomata(void) {
4743 xmlAutomataPtr ctxt;
4744
4745 ctxt = xmlRegNewParserCtxt(NULL);
4746 if (ctxt == NULL)
4747 return(NULL);
4748
4749 /* initialize the parser */
4750 ctxt->end = NULL;
4751 ctxt->start = ctxt->state = xmlRegNewState(ctxt);
Daniel Veillarddb68b742005-07-30 13:18:24 +00004752 ctxt->start->type = XML_REGEXP_START_STATE;
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00004753 if (ctxt->start == NULL) {
4754 xmlFreeAutomata(ctxt);
4755 return(NULL);
4756 }
4757 if (xmlRegStatePush(ctxt, ctxt->start) < 0) {
4758 xmlRegFreeState(ctxt->start);
4759 xmlFreeAutomata(ctxt);
4760 return(NULL);
4761 }
Daniel Veillard4255d502002-04-16 15:50:10 +00004762
4763 return(ctxt);
4764}
4765
4766/**
4767 * xmlFreeAutomata:
4768 * @am: an automata
4769 *
4770 * Free an automata
4771 */
4772void
4773xmlFreeAutomata(xmlAutomataPtr am) {
4774 if (am == NULL)
4775 return;
4776 xmlRegFreeParserCtxt(am);
4777}
4778
4779/**
4780 * xmlAutomataGetInitState:
4781 * @am: an automata
4782 *
Daniel Veillarda9b66d02002-12-11 14:23:49 +00004783 * Initial state lookup
4784 *
Daniel Veillard4255d502002-04-16 15:50:10 +00004785 * Returns the initial state of the automata
4786 */
4787xmlAutomataStatePtr
4788xmlAutomataGetInitState(xmlAutomataPtr am) {
4789 if (am == NULL)
4790 return(NULL);
4791 return(am->start);
4792}
4793
4794/**
4795 * xmlAutomataSetFinalState:
4796 * @am: an automata
4797 * @state: a state in this automata
4798 *
4799 * Makes that state a final state
4800 *
4801 * Returns 0 or -1 in case of error
4802 */
4803int
4804xmlAutomataSetFinalState(xmlAutomataPtr am, xmlAutomataStatePtr state) {
4805 if ((am == NULL) || (state == NULL))
4806 return(-1);
4807 state->type = XML_REGEXP_FINAL_STATE;
4808 return(0);
4809}
4810
4811/**
4812 * xmlAutomataNewTransition:
4813 * @am: an automata
4814 * @from: the starting point of the transition
4815 * @to: the target point of the transition or NULL
4816 * @token: the input string associated to that transition
4817 * @data: data passed to the callback function if the transition is activated
4818 *
William M. Brackddf71d62004-05-06 04:17:26 +00004819 * If @to is NULL, this creates first a new target state in the automata
Daniel Veillard4255d502002-04-16 15:50:10 +00004820 * and then adds a transition from the @from state to the target state
4821 * activated by the value of @token
4822 *
4823 * Returns the target state or NULL in case of error
4824 */
4825xmlAutomataStatePtr
4826xmlAutomataNewTransition(xmlAutomataPtr am, xmlAutomataStatePtr from,
4827 xmlAutomataStatePtr to, const xmlChar *token,
4828 void *data) {
4829 xmlRegAtomPtr atom;
4830
4831 if ((am == NULL) || (from == NULL) || (token == NULL))
4832 return(NULL);
4833 atom = xmlRegNewAtom(am, XML_REGEXP_STRING);
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00004834 if (atom == NULL)
4835 return(NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +00004836 atom->data = data;
4837 if (atom == NULL)
4838 return(NULL);
4839 atom->valuep = xmlStrdup(token);
4840
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00004841 if (xmlFAGenerateTransitions(am, from, to, atom) < 0) {
4842 xmlRegFreeAtom(atom);
4843 return(NULL);
4844 }
Daniel Veillard4255d502002-04-16 15:50:10 +00004845 if (to == NULL)
4846 return(am->state);
4847 return(to);
4848}
4849
4850/**
Daniel Veillard52b48c72003-04-13 19:53:42 +00004851 * xmlAutomataNewTransition2:
4852 * @am: an automata
4853 * @from: the starting point of the transition
4854 * @to: the target point of the transition or NULL
4855 * @token: the first input string associated to that transition
4856 * @token2: the second input string associated to that transition
4857 * @data: data passed to the callback function if the transition is activated
4858 *
William M. Brackddf71d62004-05-06 04:17:26 +00004859 * If @to is NULL, this creates first a new target state in the automata
Daniel Veillard52b48c72003-04-13 19:53:42 +00004860 * and then adds a transition from the @from state to the target state
4861 * activated by the value of @token
4862 *
4863 * Returns the target state or NULL in case of error
4864 */
4865xmlAutomataStatePtr
4866xmlAutomataNewTransition2(xmlAutomataPtr am, xmlAutomataStatePtr from,
4867 xmlAutomataStatePtr to, const xmlChar *token,
4868 const xmlChar *token2, void *data) {
4869 xmlRegAtomPtr atom;
4870
4871 if ((am == NULL) || (from == NULL) || (token == NULL))
4872 return(NULL);
4873 atom = xmlRegNewAtom(am, XML_REGEXP_STRING);
4874 atom->data = data;
4875 if (atom == NULL)
4876 return(NULL);
4877 if ((token2 == NULL) || (*token2 == 0)) {
4878 atom->valuep = xmlStrdup(token);
4879 } else {
4880 int lenn, lenp;
4881 xmlChar *str;
4882
4883 lenn = strlen((char *) token2);
4884 lenp = strlen((char *) token);
4885
Daniel Veillard3c908dc2003-04-19 00:07:51 +00004886 str = (xmlChar *) xmlMallocAtomic(lenn + lenp + 2);
Daniel Veillard52b48c72003-04-13 19:53:42 +00004887 if (str == NULL) {
4888 xmlRegFreeAtom(atom);
4889 return(NULL);
4890 }
4891 memcpy(&str[0], token, lenp);
4892 str[lenp] = '|';
4893 memcpy(&str[lenp + 1], token2, lenn);
4894 str[lenn + lenp + 1] = 0;
4895
4896 atom->valuep = str;
4897 }
4898
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00004899 if (xmlFAGenerateTransitions(am, from, to, atom) < 0) {
4900 xmlRegFreeAtom(atom);
4901 return(NULL);
4902 }
Daniel Veillard52b48c72003-04-13 19:53:42 +00004903 if (to == NULL)
4904 return(am->state);
4905 return(to);
4906}
4907
4908/**
Daniel Veillard9efc4762005-07-19 14:33:55 +00004909 * xmlAutomataNewNegTrans:
4910 * @am: an automata
4911 * @from: the starting point of the transition
4912 * @to: the target point of the transition or NULL
4913 * @token: the first input string associated to that transition
4914 * @token2: the second input string associated to that transition
4915 * @data: data passed to the callback function if the transition is activated
4916 *
4917 * If @to is NULL, this creates first a new target state in the automata
4918 * and then adds a transition from the @from state to the target state
4919 * activated by any value except (@token,@token2)
4920 *
4921 * Returns the target state or NULL in case of error
4922 */
4923xmlAutomataStatePtr
4924xmlAutomataNewNegTrans(xmlAutomataPtr am, xmlAutomataStatePtr from,
4925 xmlAutomataStatePtr to, const xmlChar *token,
4926 const xmlChar *token2, void *data) {
4927 xmlRegAtomPtr atom;
Daniel Veillard77005e62005-07-19 16:26:18 +00004928 xmlChar err_msg[200];
Daniel Veillard9efc4762005-07-19 14:33:55 +00004929
4930 if ((am == NULL) || (from == NULL) || (token == NULL))
4931 return(NULL);
4932 atom = xmlRegNewAtom(am, XML_REGEXP_STRING);
4933 if (atom == NULL)
4934 return(NULL);
4935 atom->data = data;
4936 atom->neg = 1;
4937 if ((token2 == NULL) || (*token2 == 0)) {
4938 atom->valuep = xmlStrdup(token);
4939 } else {
4940 int lenn, lenp;
4941 xmlChar *str;
4942
4943 lenn = strlen((char *) token2);
4944 lenp = strlen((char *) token);
4945
4946 str = (xmlChar *) xmlMallocAtomic(lenn + lenp + 2);
4947 if (str == NULL) {
4948 xmlRegFreeAtom(atom);
4949 return(NULL);
4950 }
4951 memcpy(&str[0], token, lenp);
4952 str[lenp] = '|';
4953 memcpy(&str[lenp + 1], token2, lenn);
4954 str[lenn + lenp + 1] = 0;
4955
4956 atom->valuep = str;
4957 }
Daniel Veillarddb68b742005-07-30 13:18:24 +00004958 snprintf((char *) err_msg, 199, "not %s", (const char *) atom->valuep);
Daniel Veillard77005e62005-07-19 16:26:18 +00004959 err_msg[199] = 0;
4960 atom->valuep2 = xmlStrdup(err_msg);
Daniel Veillard9efc4762005-07-19 14:33:55 +00004961
4962 if (xmlFAGenerateTransitions(am, from, to, atom) < 0) {
4963 xmlRegFreeAtom(atom);
4964 return(NULL);
4965 }
4966 if (to == NULL)
4967 return(am->state);
4968 return(to);
4969}
4970
4971/**
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00004972 * xmlAutomataNewCountTrans2:
4973 * @am: an automata
4974 * @from: the starting point of the transition
4975 * @to: the target point of the transition or NULL
4976 * @token: the input string associated to that transition
4977 * @token2: the second input string associated to that transition
4978 * @min: the minimum successive occurences of token
4979 * @max: the maximum successive occurences of token
4980 * @data: data associated to the transition
4981 *
4982 * If @to is NULL, this creates first a new target state in the automata
4983 * and then adds a transition from the @from state to the target state
4984 * activated by a succession of input of value @token and @token2 and
4985 * whose number is between @min and @max
4986 *
4987 * Returns the target state or NULL in case of error
4988 */
4989xmlAutomataStatePtr
4990xmlAutomataNewCountTrans2(xmlAutomataPtr am, xmlAutomataStatePtr from,
4991 xmlAutomataStatePtr to, const xmlChar *token,
4992 const xmlChar *token2,
4993 int min, int max, void *data) {
4994 xmlRegAtomPtr atom;
4995 int counter;
4996
4997 if ((am == NULL) || (from == NULL) || (token == NULL))
4998 return(NULL);
4999 if (min < 0)
5000 return(NULL);
5001 if ((max < min) || (max < 1))
5002 return(NULL);
5003 atom = xmlRegNewAtom(am, XML_REGEXP_STRING);
5004 if (atom == NULL)
5005 return(NULL);
5006 if ((token2 == NULL) || (*token2 == 0)) {
5007 atom->valuep = xmlStrdup(token);
5008 } else {
5009 int lenn, lenp;
5010 xmlChar *str;
5011
5012 lenn = strlen((char *) token2);
5013 lenp = strlen((char *) token);
5014
5015 str = (xmlChar *) xmlMallocAtomic(lenn + lenp + 2);
5016 if (str == NULL) {
5017 xmlRegFreeAtom(atom);
5018 return(NULL);
5019 }
5020 memcpy(&str[0], token, lenp);
5021 str[lenp] = '|';
5022 memcpy(&str[lenp + 1], token2, lenn);
5023 str[lenn + lenp + 1] = 0;
5024
5025 atom->valuep = str;
5026 }
5027 atom->data = data;
5028 if (min == 0)
5029 atom->min = 1;
5030 else
5031 atom->min = min;
5032 atom->max = max;
5033
5034 /*
5035 * associate a counter to the transition.
5036 */
5037 counter = xmlRegGetCounter(am);
5038 am->counters[counter].min = min;
5039 am->counters[counter].max = max;
5040
5041 /* xmlFAGenerateTransitions(am, from, to, atom); */
5042 if (to == NULL) {
5043 to = xmlRegNewState(am);
5044 xmlRegStatePush(am, to);
5045 }
Daniel Veillarddb68b742005-07-30 13:18:24 +00005046 xmlRegStateAddTrans(am, from, atom, to, counter, -1, 0);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00005047 xmlRegAtomPush(am, atom);
5048 am->state = to;
5049
5050 if (to == NULL)
5051 to = am->state;
5052 if (to == NULL)
5053 return(NULL);
5054 if (min == 0)
5055 xmlFAGenerateEpsilonTransition(am, from, to);
5056 return(to);
5057}
5058
5059/**
Daniel Veillard4255d502002-04-16 15:50:10 +00005060 * xmlAutomataNewCountTrans:
5061 * @am: an automata
5062 * @from: the starting point of the transition
5063 * @to: the target point of the transition or NULL
5064 * @token: the input string associated to that transition
5065 * @min: the minimum successive occurences of token
Daniel Veillarda9b66d02002-12-11 14:23:49 +00005066 * @max: the maximum successive occurences of token
5067 * @data: data associated to the transition
Daniel Veillard4255d502002-04-16 15:50:10 +00005068 *
William M. Brackddf71d62004-05-06 04:17:26 +00005069 * If @to is NULL, this creates first a new target state in the automata
Daniel Veillard4255d502002-04-16 15:50:10 +00005070 * and then adds a transition from the @from state to the target state
5071 * activated by a succession of input of value @token and whose number
5072 * is between @min and @max
5073 *
5074 * Returns the target state or NULL in case of error
5075 */
5076xmlAutomataStatePtr
5077xmlAutomataNewCountTrans(xmlAutomataPtr am, xmlAutomataStatePtr from,
5078 xmlAutomataStatePtr to, const xmlChar *token,
5079 int min, int max, void *data) {
5080 xmlRegAtomPtr atom;
Daniel Veillard0ddb21c2004-02-12 12:43:49 +00005081 int counter;
Daniel Veillard4255d502002-04-16 15:50:10 +00005082
5083 if ((am == NULL) || (from == NULL) || (token == NULL))
5084 return(NULL);
5085 if (min < 0)
5086 return(NULL);
5087 if ((max < min) || (max < 1))
5088 return(NULL);
5089 atom = xmlRegNewAtom(am, XML_REGEXP_STRING);
5090 if (atom == NULL)
5091 return(NULL);
5092 atom->valuep = xmlStrdup(token);
5093 atom->data = data;
5094 if (min == 0)
5095 atom->min = 1;
5096 else
5097 atom->min = min;
5098 atom->max = max;
5099
Daniel Veillard0ddb21c2004-02-12 12:43:49 +00005100 /*
5101 * associate a counter to the transition.
5102 */
5103 counter = xmlRegGetCounter(am);
5104 am->counters[counter].min = min;
5105 am->counters[counter].max = max;
5106
5107 /* xmlFAGenerateTransitions(am, from, to, atom); */
5108 if (to == NULL) {
5109 to = xmlRegNewState(am);
5110 xmlRegStatePush(am, to);
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00005111 }
Daniel Veillarddb68b742005-07-30 13:18:24 +00005112 xmlRegStateAddTrans(am, from, atom, to, counter, -1, 0);
Daniel Veillard0ddb21c2004-02-12 12:43:49 +00005113 xmlRegAtomPush(am, atom);
5114 am->state = to;
5115
Daniel Veillard4255d502002-04-16 15:50:10 +00005116 if (to == NULL)
5117 to = am->state;
5118 if (to == NULL)
5119 return(NULL);
5120 if (min == 0)
5121 xmlFAGenerateEpsilonTransition(am, from, to);
5122 return(to);
5123}
5124
5125/**
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00005126 * xmlAutomataNewOnceTrans2:
5127 * @am: an automata
5128 * @from: the starting point of the transition
5129 * @to: the target point of the transition or NULL
5130 * @token: the input string associated to that transition
5131 * @token2: the second input string associated to that transition
5132 * @min: the minimum successive occurences of token
5133 * @max: the maximum successive occurences of token
5134 * @data: data associated to the transition
5135 *
5136 * If @to is NULL, this creates first a new target state in the automata
5137 * and then adds a transition from the @from state to the target state
5138 * activated by a succession of input of value @token and @token2 and whose
5139 * number is between @min and @max, moreover that transition can only be
5140 * crossed once.
5141 *
5142 * Returns the target state or NULL in case of error
5143 */
5144xmlAutomataStatePtr
5145xmlAutomataNewOnceTrans2(xmlAutomataPtr am, xmlAutomataStatePtr from,
5146 xmlAutomataStatePtr to, const xmlChar *token,
5147 const xmlChar *token2,
5148 int min, int max, void *data) {
5149 xmlRegAtomPtr atom;
5150 int counter;
5151
5152 if ((am == NULL) || (from == NULL) || (token == NULL))
5153 return(NULL);
5154 if (min < 1)
5155 return(NULL);
5156 if ((max < min) || (max < 1))
5157 return(NULL);
5158 atom = xmlRegNewAtom(am, XML_REGEXP_STRING);
5159 if (atom == NULL)
5160 return(NULL);
5161 if ((token2 == NULL) || (*token2 == 0)) {
5162 atom->valuep = xmlStrdup(token);
5163 } else {
5164 int lenn, lenp;
5165 xmlChar *str;
5166
5167 lenn = strlen((char *) token2);
5168 lenp = strlen((char *) token);
5169
5170 str = (xmlChar *) xmlMallocAtomic(lenn + lenp + 2);
5171 if (str == NULL) {
5172 xmlRegFreeAtom(atom);
5173 return(NULL);
5174 }
5175 memcpy(&str[0], token, lenp);
5176 str[lenp] = '|';
5177 memcpy(&str[lenp + 1], token2, lenn);
5178 str[lenn + lenp + 1] = 0;
5179
5180 atom->valuep = str;
5181 }
5182 atom->data = data;
5183 atom->quant = XML_REGEXP_QUANT_ONCEONLY;
5184 if (min == 0)
5185 atom->min = 1;
5186 else
5187 atom->min = min;
5188 atom->max = max;
5189 /*
5190 * associate a counter to the transition.
5191 */
5192 counter = xmlRegGetCounter(am);
5193 am->counters[counter].min = 1;
5194 am->counters[counter].max = 1;
5195
5196 /* xmlFAGenerateTransitions(am, from, to, atom); */
5197 if (to == NULL) {
5198 to = xmlRegNewState(am);
5199 xmlRegStatePush(am, to);
5200 }
Daniel Veillarddb68b742005-07-30 13:18:24 +00005201 xmlRegStateAddTrans(am, from, atom, to, counter, -1, 0);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00005202 xmlRegAtomPush(am, atom);
5203 am->state = to;
5204 return(to);
5205}
5206
5207
5208
5209/**
Daniel Veillard7646b182002-04-20 06:41:40 +00005210 * xmlAutomataNewOnceTrans:
5211 * @am: an automata
5212 * @from: the starting point of the transition
5213 * @to: the target point of the transition or NULL
5214 * @token: the input string associated to that transition
5215 * @min: the minimum successive occurences of token
Daniel Veillarda9b66d02002-12-11 14:23:49 +00005216 * @max: the maximum successive occurences of token
5217 * @data: data associated to the transition
Daniel Veillard7646b182002-04-20 06:41:40 +00005218 *
William M. Brackddf71d62004-05-06 04:17:26 +00005219 * If @to is NULL, this creates first a new target state in the automata
Daniel Veillard7646b182002-04-20 06:41:40 +00005220 * and then adds a transition from the @from state to the target state
5221 * activated by a succession of input of value @token and whose number
William M. Brackddf71d62004-05-06 04:17:26 +00005222 * is between @min and @max, moreover that transition can only be crossed
Daniel Veillard7646b182002-04-20 06:41:40 +00005223 * once.
5224 *
5225 * Returns the target state or NULL in case of error
5226 */
5227xmlAutomataStatePtr
5228xmlAutomataNewOnceTrans(xmlAutomataPtr am, xmlAutomataStatePtr from,
5229 xmlAutomataStatePtr to, const xmlChar *token,
5230 int min, int max, void *data) {
5231 xmlRegAtomPtr atom;
5232 int counter;
5233
5234 if ((am == NULL) || (from == NULL) || (token == NULL))
5235 return(NULL);
5236 if (min < 1)
5237 return(NULL);
5238 if ((max < min) || (max < 1))
5239 return(NULL);
5240 atom = xmlRegNewAtom(am, XML_REGEXP_STRING);
5241 if (atom == NULL)
5242 return(NULL);
5243 atom->valuep = xmlStrdup(token);
5244 atom->data = data;
5245 atom->quant = XML_REGEXP_QUANT_ONCEONLY;
5246 if (min == 0)
5247 atom->min = 1;
5248 else
5249 atom->min = min;
5250 atom->max = max;
5251 /*
5252 * associate a counter to the transition.
5253 */
5254 counter = xmlRegGetCounter(am);
5255 am->counters[counter].min = 1;
5256 am->counters[counter].max = 1;
5257
5258 /* xmlFAGenerateTransitions(am, from, to, atom); */
5259 if (to == NULL) {
5260 to = xmlRegNewState(am);
5261 xmlRegStatePush(am, to);
5262 }
Daniel Veillarddb68b742005-07-30 13:18:24 +00005263 xmlRegStateAddTrans(am, from, atom, to, counter, -1, 0);
Daniel Veillard7646b182002-04-20 06:41:40 +00005264 xmlRegAtomPush(am, atom);
5265 am->state = to;
Daniel Veillard7646b182002-04-20 06:41:40 +00005266 return(to);
5267}
5268
5269/**
Daniel Veillard4255d502002-04-16 15:50:10 +00005270 * xmlAutomataNewState:
5271 * @am: an automata
5272 *
5273 * Create a new disconnected state in the automata
5274 *
5275 * Returns the new state or NULL in case of error
5276 */
5277xmlAutomataStatePtr
5278xmlAutomataNewState(xmlAutomataPtr am) {
5279 xmlAutomataStatePtr to;
5280
5281 if (am == NULL)
5282 return(NULL);
5283 to = xmlRegNewState(am);
5284 xmlRegStatePush(am, to);
5285 return(to);
5286}
5287
5288/**
Daniel Veillarda9b66d02002-12-11 14:23:49 +00005289 * xmlAutomataNewEpsilon:
Daniel Veillard4255d502002-04-16 15:50:10 +00005290 * @am: an automata
5291 * @from: the starting point of the transition
5292 * @to: the target point of the transition or NULL
5293 *
William M. Brackddf71d62004-05-06 04:17:26 +00005294 * If @to is NULL, this creates first a new target state in the automata
5295 * and then adds an epsilon transition from the @from state to the
Daniel Veillard4255d502002-04-16 15:50:10 +00005296 * target state
5297 *
5298 * Returns the target state or NULL in case of error
5299 */
5300xmlAutomataStatePtr
5301xmlAutomataNewEpsilon(xmlAutomataPtr am, xmlAutomataStatePtr from,
5302 xmlAutomataStatePtr to) {
5303 if ((am == NULL) || (from == NULL))
5304 return(NULL);
5305 xmlFAGenerateEpsilonTransition(am, from, to);
5306 if (to == NULL)
5307 return(am->state);
5308 return(to);
5309}
5310
Daniel Veillardb509f152002-04-17 16:28:10 +00005311/**
Daniel Veillard7646b182002-04-20 06:41:40 +00005312 * xmlAutomataNewAllTrans:
5313 * @am: an automata
5314 * @from: the starting point of the transition
5315 * @to: the target point of the transition or NULL
Daniel Veillarda9b66d02002-12-11 14:23:49 +00005316 * @lax: allow to transition if not all all transitions have been activated
Daniel Veillard7646b182002-04-20 06:41:40 +00005317 *
William M. Brackddf71d62004-05-06 04:17:26 +00005318 * If @to is NULL, this creates first a new target state in the automata
Daniel Veillard7646b182002-04-20 06:41:40 +00005319 * and then adds a an ALL transition from the @from state to the
5320 * target state. That transition is an epsilon transition allowed only when
5321 * all transitions from the @from node have been activated.
5322 *
5323 * Returns the target state or NULL in case of error
5324 */
5325xmlAutomataStatePtr
5326xmlAutomataNewAllTrans(xmlAutomataPtr am, xmlAutomataStatePtr from,
Daniel Veillard441bc322002-04-20 17:38:48 +00005327 xmlAutomataStatePtr to, int lax) {
Daniel Veillard7646b182002-04-20 06:41:40 +00005328 if ((am == NULL) || (from == NULL))
5329 return(NULL);
Daniel Veillard441bc322002-04-20 17:38:48 +00005330 xmlFAGenerateAllTransition(am, from, to, lax);
Daniel Veillard7646b182002-04-20 06:41:40 +00005331 if (to == NULL)
5332 return(am->state);
5333 return(to);
5334}
5335
5336/**
Daniel Veillardb509f152002-04-17 16:28:10 +00005337 * xmlAutomataNewCounter:
5338 * @am: an automata
5339 * @min: the minimal value on the counter
5340 * @max: the maximal value on the counter
5341 *
5342 * Create a new counter
5343 *
5344 * Returns the counter number or -1 in case of error
5345 */
5346int
5347xmlAutomataNewCounter(xmlAutomataPtr am, int min, int max) {
5348 int ret;
5349
5350 if (am == NULL)
5351 return(-1);
5352
5353 ret = xmlRegGetCounter(am);
5354 if (ret < 0)
5355 return(-1);
5356 am->counters[ret].min = min;
5357 am->counters[ret].max = max;
5358 return(ret);
5359}
5360
5361/**
5362 * xmlAutomataNewCountedTrans:
5363 * @am: an automata
5364 * @from: the starting point of the transition
5365 * @to: the target point of the transition or NULL
5366 * @counter: the counter associated to that transition
5367 *
William M. Brackddf71d62004-05-06 04:17:26 +00005368 * If @to is NULL, this creates first a new target state in the automata
Daniel Veillardb509f152002-04-17 16:28:10 +00005369 * and then adds an epsilon transition from the @from state to the target state
5370 * which will increment the counter provided
5371 *
5372 * Returns the target state or NULL in case of error
5373 */
5374xmlAutomataStatePtr
5375xmlAutomataNewCountedTrans(xmlAutomataPtr am, xmlAutomataStatePtr from,
5376 xmlAutomataStatePtr to, int counter) {
5377 if ((am == NULL) || (from == NULL) || (counter < 0))
5378 return(NULL);
5379 xmlFAGenerateCountedEpsilonTransition(am, from, to, counter);
5380 if (to == NULL)
5381 return(am->state);
5382 return(to);
5383}
5384
5385/**
5386 * xmlAutomataNewCounterTrans:
5387 * @am: an automata
5388 * @from: the starting point of the transition
5389 * @to: the target point of the transition or NULL
5390 * @counter: the counter associated to that transition
5391 *
William M. Brackddf71d62004-05-06 04:17:26 +00005392 * If @to is NULL, this creates first a new target state in the automata
Daniel Veillardb509f152002-04-17 16:28:10 +00005393 * and then adds an epsilon transition from the @from state to the target state
5394 * which will be allowed only if the counter is within the right range.
5395 *
5396 * Returns the target state or NULL in case of error
5397 */
5398xmlAutomataStatePtr
5399xmlAutomataNewCounterTrans(xmlAutomataPtr am, xmlAutomataStatePtr from,
5400 xmlAutomataStatePtr to, int counter) {
5401 if ((am == NULL) || (from == NULL) || (counter < 0))
5402 return(NULL);
5403 xmlFAGenerateCountedTransition(am, from, to, counter);
5404 if (to == NULL)
5405 return(am->state);
5406 return(to);
5407}
Daniel Veillard4255d502002-04-16 15:50:10 +00005408
5409/**
5410 * xmlAutomataCompile:
5411 * @am: an automata
5412 *
5413 * Compile the automata into a Reg Exp ready for being executed.
5414 * The automata should be free after this point.
5415 *
5416 * Returns the compiled regexp or NULL in case of error
5417 */
5418xmlRegexpPtr
5419xmlAutomataCompile(xmlAutomataPtr am) {
5420 xmlRegexpPtr ret;
5421
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00005422 if ((am == NULL) || (am->error != 0)) return(NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +00005423 xmlFAEliminateEpsilonTransitions(am);
Daniel Veillard23e73572002-09-19 19:56:43 +00005424 /* xmlFAComputesDeterminism(am); */
Daniel Veillard4255d502002-04-16 15:50:10 +00005425 ret = xmlRegEpxFromParse(am);
5426
5427 return(ret);
5428}
Daniel Veillarde19fc232002-04-22 16:01:24 +00005429
5430/**
5431 * xmlAutomataIsDeterminist:
5432 * @am: an automata
5433 *
5434 * Checks if an automata is determinist.
5435 *
5436 * Returns 1 if true, 0 if not, and -1 in case of error
5437 */
5438int
5439xmlAutomataIsDeterminist(xmlAutomataPtr am) {
5440 int ret;
5441
5442 if (am == NULL)
5443 return(-1);
5444
5445 ret = xmlFAComputesDeterminism(am);
5446 return(ret);
5447}
Daniel Veillard4255d502002-04-16 15:50:10 +00005448#endif /* LIBXML_AUTOMATA_ENABLED */
Daniel Veillard5d4644e2005-04-01 13:11:58 +00005449#define bottom_xmlregexp
5450#include "elfgcchack.h"
Daniel Veillard4255d502002-04-16 15:50:10 +00005451#endif /* LIBXML_REGEXP_ENABLED */