blob: eba09a9f3ef912c9bb4bcf5fea0d9f11aa455184 [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 */
41/* #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;
214};
215
216typedef struct _xmlAutomata xmlRegParserCtxt;
217typedef xmlRegParserCtxt *xmlRegParserCtxtPtr;
218
219struct _xmlAutomata {
220 xmlChar *string;
221 xmlChar *cur;
222
223 int error;
224 int neg;
225
226 xmlRegStatePtr start;
227 xmlRegStatePtr end;
228 xmlRegStatePtr state;
229
230 xmlRegAtomPtr atom;
231
232 int maxAtoms;
233 int nbAtoms;
234 xmlRegAtomPtr *atoms;
235
236 int maxStates;
237 int nbStates;
238 xmlRegStatePtr *states;
239
240 int maxCounters;
241 int nbCounters;
242 xmlRegCounter *counters;
Daniel Veillarde19fc232002-04-22 16:01:24 +0000243
244 int determinist;
Daniel Veillard4255d502002-04-16 15:50:10 +0000245};
246
247struct _xmlRegexp {
248 xmlChar *string;
249 int nbStates;
250 xmlRegStatePtr *states;
251 int nbAtoms;
252 xmlRegAtomPtr *atoms;
253 int nbCounters;
254 xmlRegCounter *counters;
Daniel Veillarde19fc232002-04-22 16:01:24 +0000255 int determinist;
Daniel Veillard23e73572002-09-19 19:56:43 +0000256 /*
257 * That's the compact form for determinists automatas
258 */
259 int nbstates;
260 int *compact;
Daniel Veillard118aed72002-09-24 14:13:13 +0000261 void **transdata;
Daniel Veillard23e73572002-09-19 19:56:43 +0000262 int nbstrings;
263 xmlChar **stringMap;
Daniel Veillard4255d502002-04-16 15:50:10 +0000264};
265
266typedef struct _xmlRegExecRollback xmlRegExecRollback;
267typedef xmlRegExecRollback *xmlRegExecRollbackPtr;
268
269struct _xmlRegExecRollback {
270 xmlRegStatePtr state;/* the current state */
271 int index; /* the index in the input stack */
272 int nextbranch; /* the next transition to explore in that state */
William M. Brackddf71d62004-05-06 04:17:26 +0000273 int *counts; /* save the automata state if it has some */
Daniel Veillard4255d502002-04-16 15:50:10 +0000274};
275
276typedef struct _xmlRegInputToken xmlRegInputToken;
277typedef xmlRegInputToken *xmlRegInputTokenPtr;
278
279struct _xmlRegInputToken {
280 xmlChar *value;
281 void *data;
282};
283
284struct _xmlRegExecCtxt {
285 int status; /* execution status != 0 indicate an error */
William M. Brackddf71d62004-05-06 04:17:26 +0000286 int determinist; /* did we find an indeterministic behaviour */
Daniel Veillard4255d502002-04-16 15:50:10 +0000287 xmlRegexpPtr comp; /* the compiled regexp */
288 xmlRegExecCallbacks callback;
289 void *data;
290
291 xmlRegStatePtr state;/* the current state */
292 int transno; /* the current transition on that state */
William M. Brackddf71d62004-05-06 04:17:26 +0000293 int transcount; /* the number of chars in char counted transitions */
Daniel Veillard4255d502002-04-16 15:50:10 +0000294
295 /*
296 * A stack of rollback states
297 */
298 int maxRollbacks;
299 int nbRollbacks;
300 xmlRegExecRollback *rollbacks;
301
302 /*
303 * The state of the automata if any
304 */
305 int *counts;
306
307 /*
308 * The input stack
309 */
310 int inputStackMax;
311 int inputStackNr;
312 int index;
313 int *charStack;
314 const xmlChar *inputString; /* when operating on characters */
315 xmlRegInputTokenPtr inputStack;/* when operating on strings */
316
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +0000317 /*
318 * error handling
319 */
320 int errStateNo; /* the error state number */
321 xmlRegStatePtr errState; /* the error state */
322 xmlChar *errString; /* the string raising the error */
323 int *errCounts; /* counters at the error state */
Daniel Veillard4255d502002-04-16 15:50:10 +0000324};
325
Daniel Veillard441bc322002-04-20 17:38:48 +0000326#define REGEXP_ALL_COUNTER 0x123456
327#define REGEXP_ALL_LAX_COUNTER 0x123457
Daniel Veillard7646b182002-04-20 06:41:40 +0000328
Daniel Veillard4255d502002-04-16 15:50:10 +0000329static void xmlFAParseRegExp(xmlRegParserCtxtPtr ctxt, int top);
Daniel Veillard23e73572002-09-19 19:56:43 +0000330static void xmlRegFreeState(xmlRegStatePtr state);
331static void xmlRegFreeAtom(xmlRegAtomPtr atom);
Daniel Veillard9efc4762005-07-19 14:33:55 +0000332static int xmlRegStrEqualWildcard(const xmlChar *expStr, const xmlChar *valStr);
Daniel Veillard4255d502002-04-16 15:50:10 +0000333
334/************************************************************************
Daniel Veillardff46a042003-10-08 08:53:17 +0000335 * *
336 * Regexp memory error handler *
337 * *
338 ************************************************************************/
339/**
340 * xmlRegexpErrMemory:
William M. Brackddf71d62004-05-06 04:17:26 +0000341 * @extra: extra information
Daniel Veillardff46a042003-10-08 08:53:17 +0000342 *
343 * Handle an out of memory condition
344 */
345static void
346xmlRegexpErrMemory(xmlRegParserCtxtPtr ctxt, const char *extra)
347{
348 const char *regexp = NULL;
349 if (ctxt != NULL) {
350 regexp = (const char *) ctxt->string;
351 ctxt->error = XML_ERR_NO_MEMORY;
352 }
Daniel Veillard659e71e2003-10-10 14:10:40 +0000353 __xmlRaiseError(NULL, NULL, NULL, NULL, NULL, XML_FROM_REGEXP,
Daniel Veillardff46a042003-10-08 08:53:17 +0000354 XML_ERR_NO_MEMORY, XML_ERR_FATAL, NULL, 0, extra,
355 regexp, NULL, 0, 0,
356 "Memory allocation failed : %s\n", extra);
357}
358
359/**
360 * xmlRegexpErrCompile:
William M. Brackddf71d62004-05-06 04:17:26 +0000361 * @extra: extra information
Daniel Veillardff46a042003-10-08 08:53:17 +0000362 *
William M. Brackddf71d62004-05-06 04:17:26 +0000363 * Handle a compilation failure
Daniel Veillardff46a042003-10-08 08:53:17 +0000364 */
365static void
366xmlRegexpErrCompile(xmlRegParserCtxtPtr ctxt, const char *extra)
367{
368 const char *regexp = NULL;
369 int idx = 0;
370
371 if (ctxt != NULL) {
372 regexp = (const char *) ctxt->string;
373 idx = ctxt->cur - ctxt->string;
374 ctxt->error = XML_REGEXP_COMPILE_ERROR;
375 }
Daniel Veillard659e71e2003-10-10 14:10:40 +0000376 __xmlRaiseError(NULL, NULL, NULL, NULL, NULL, XML_FROM_REGEXP,
Daniel Veillardff46a042003-10-08 08:53:17 +0000377 XML_REGEXP_COMPILE_ERROR, XML_ERR_FATAL, NULL, 0, extra,
378 regexp, NULL, idx, 0,
379 "failed to compile: %s\n", extra);
380}
381
382/************************************************************************
Daniel Veillard4255d502002-04-16 15:50:10 +0000383 * *
384 * Allocation/Deallocation *
385 * *
386 ************************************************************************/
387
Daniel Veillard23e73572002-09-19 19:56:43 +0000388static int xmlFAComputesDeterminism(xmlRegParserCtxtPtr ctxt);
Daniel Veillard4255d502002-04-16 15:50:10 +0000389/**
390 * xmlRegEpxFromParse:
391 * @ctxt: the parser context used to build it
392 *
William M. Brackddf71d62004-05-06 04:17:26 +0000393 * Allocate a new regexp and fill it with the result from the parser
Daniel Veillard4255d502002-04-16 15:50:10 +0000394 *
395 * Returns the new regexp or NULL in case of error
396 */
397static xmlRegexpPtr
398xmlRegEpxFromParse(xmlRegParserCtxtPtr ctxt) {
399 xmlRegexpPtr ret;
400
401 ret = (xmlRegexpPtr) xmlMalloc(sizeof(xmlRegexp));
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000402 if (ret == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +0000403 xmlRegexpErrMemory(ctxt, "compiling regexp");
Daniel Veillard4255d502002-04-16 15:50:10 +0000404 return(NULL);
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000405 }
Daniel Veillard4255d502002-04-16 15:50:10 +0000406 memset(ret, 0, sizeof(xmlRegexp));
407 ret->string = ctxt->string;
Daniel Veillard4255d502002-04-16 15:50:10 +0000408 ret->nbStates = ctxt->nbStates;
Daniel Veillard4255d502002-04-16 15:50:10 +0000409 ret->states = ctxt->states;
Daniel Veillard4255d502002-04-16 15:50:10 +0000410 ret->nbAtoms = ctxt->nbAtoms;
Daniel Veillard4255d502002-04-16 15:50:10 +0000411 ret->atoms = ctxt->atoms;
Daniel Veillard4255d502002-04-16 15:50:10 +0000412 ret->nbCounters = ctxt->nbCounters;
Daniel Veillard4255d502002-04-16 15:50:10 +0000413 ret->counters = ctxt->counters;
Daniel Veillarde19fc232002-04-22 16:01:24 +0000414 ret->determinist = ctxt->determinist;
Daniel Veillard23e73572002-09-19 19:56:43 +0000415
416 if ((ret->determinist != 0) &&
417 (ret->nbCounters == 0) &&
Daniel Veillard118aed72002-09-24 14:13:13 +0000418 (ret->atoms != NULL) &&
Daniel Veillard23e73572002-09-19 19:56:43 +0000419 (ret->atoms[0] != NULL) &&
420 (ret->atoms[0]->type == XML_REGEXP_STRING)) {
421 int i, j, nbstates = 0, nbatoms = 0;
422 int *stateRemap;
423 int *stringRemap;
424 int *transitions;
Daniel Veillard118aed72002-09-24 14:13:13 +0000425 void **transdata;
Daniel Veillard23e73572002-09-19 19:56:43 +0000426 xmlChar **stringMap;
427 xmlChar *value;
428
429 /*
430 * Switch to a compact representation
431 * 1/ counting the effective number of states left
William M. Brackddf71d62004-05-06 04:17:26 +0000432 * 2/ counting the unique number of atoms, and check that
Daniel Veillard23e73572002-09-19 19:56:43 +0000433 * they are all of the string type
434 * 3/ build a table state x atom for the transitions
435 */
436
437 stateRemap = xmlMalloc(ret->nbStates * sizeof(int));
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000438 if (stateRemap == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +0000439 xmlRegexpErrMemory(ctxt, "compiling regexp");
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000440 xmlFree(ret);
441 return(NULL);
442 }
Daniel Veillard23e73572002-09-19 19:56:43 +0000443 for (i = 0;i < ret->nbStates;i++) {
444 if (ret->states[i] != NULL) {
445 stateRemap[i] = nbstates;
446 nbstates++;
447 } else {
448 stateRemap[i] = -1;
449 }
450 }
451#ifdef DEBUG_COMPACTION
452 printf("Final: %d states\n", nbstates);
453#endif
454 stringMap = xmlMalloc(ret->nbAtoms * sizeof(char *));
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000455 if (stringMap == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +0000456 xmlRegexpErrMemory(ctxt, "compiling regexp");
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000457 xmlFree(stateRemap);
458 xmlFree(ret);
459 return(NULL);
460 }
Daniel Veillard23e73572002-09-19 19:56:43 +0000461 stringRemap = xmlMalloc(ret->nbAtoms * sizeof(int));
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000462 if (stringRemap == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +0000463 xmlRegexpErrMemory(ctxt, "compiling regexp");
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000464 xmlFree(stringMap);
465 xmlFree(stateRemap);
466 xmlFree(ret);
467 return(NULL);
468 }
Daniel Veillard23e73572002-09-19 19:56:43 +0000469 for (i = 0;i < ret->nbAtoms;i++) {
470 if ((ret->atoms[i]->type == XML_REGEXP_STRING) &&
471 (ret->atoms[i]->quant == XML_REGEXP_QUANT_ONCE)) {
472 value = ret->atoms[i]->valuep;
473 for (j = 0;j < nbatoms;j++) {
474 if (xmlStrEqual(stringMap[j], value)) {
475 stringRemap[i] = j;
476 break;
477 }
478 }
479 if (j >= nbatoms) {
480 stringRemap[i] = nbatoms;
481 stringMap[nbatoms] = xmlStrdup(value);
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000482 if (stringMap[nbatoms] == NULL) {
483 for (i = 0;i < nbatoms;i++)
484 xmlFree(stringMap[i]);
485 xmlFree(stringRemap);
486 xmlFree(stringMap);
487 xmlFree(stateRemap);
488 xmlFree(ret);
489 return(NULL);
490 }
Daniel Veillard23e73572002-09-19 19:56:43 +0000491 nbatoms++;
492 }
493 } else {
494 xmlFree(stateRemap);
495 xmlFree(stringRemap);
496 for (i = 0;i < nbatoms;i++)
497 xmlFree(stringMap[i]);
498 xmlFree(stringMap);
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000499 xmlFree(ret);
500 return(NULL);
Daniel Veillard23e73572002-09-19 19:56:43 +0000501 }
502 }
503#ifdef DEBUG_COMPACTION
504 printf("Final: %d atoms\n", nbatoms);
505#endif
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000506 transitions = (int *) xmlMalloc((nbstates + 1) *
507 (nbatoms + 1) * sizeof(int));
508 if (transitions == NULL) {
509 xmlFree(stateRemap);
510 xmlFree(stringRemap);
511 xmlFree(stringMap);
512 xmlFree(ret);
513 return(NULL);
514 }
515 memset(transitions, 0, (nbstates + 1) * (nbatoms + 1) * sizeof(int));
Daniel Veillard23e73572002-09-19 19:56:43 +0000516
517 /*
518 * Allocate the transition table. The first entry for each
William M. Brackddf71d62004-05-06 04:17:26 +0000519 * state corresponds to the state type.
Daniel Veillard23e73572002-09-19 19:56:43 +0000520 */
Daniel Veillard118aed72002-09-24 14:13:13 +0000521 transdata = NULL;
Daniel Veillard23e73572002-09-19 19:56:43 +0000522
523 for (i = 0;i < ret->nbStates;i++) {
524 int stateno, atomno, targetno, prev;
525 xmlRegStatePtr state;
526 xmlRegTransPtr trans;
527
528 stateno = stateRemap[i];
529 if (stateno == -1)
530 continue;
531 state = ret->states[i];
532
533 transitions[stateno * (nbatoms + 1)] = state->type;
534
535 for (j = 0;j < state->nbTrans;j++) {
536 trans = &(state->trans[j]);
537 if ((trans->to == -1) || (trans->atom == NULL))
538 continue;
539 atomno = stringRemap[trans->atom->no];
Daniel Veillard118aed72002-09-24 14:13:13 +0000540 if ((trans->atom->data != NULL) && (transdata == NULL)) {
541 transdata = (void **) xmlMalloc(nbstates * nbatoms *
542 sizeof(void *));
543 if (transdata != NULL)
544 memset(transdata, 0,
545 nbstates * nbatoms * sizeof(void *));
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000546 else {
Daniel Veillardff46a042003-10-08 08:53:17 +0000547 xmlRegexpErrMemory(ctxt, "compiling regexp");
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000548 break;
549 }
Daniel Veillard118aed72002-09-24 14:13:13 +0000550 }
Daniel Veillard23e73572002-09-19 19:56:43 +0000551 targetno = stateRemap[trans->to];
552 /*
William M. Brackddf71d62004-05-06 04:17:26 +0000553 * if the same atom can generate transitions to 2 different
Daniel Veillard23e73572002-09-19 19:56:43 +0000554 * states then it means the automata is not determinist and
555 * the compact form can't be used !
556 */
557 prev = transitions[stateno * (nbatoms + 1) + atomno + 1];
558 if (prev != 0) {
559 if (prev != targetno + 1) {
Daniel Veillard23e73572002-09-19 19:56:43 +0000560 ret->determinist = 0;
561#ifdef DEBUG_COMPACTION
562 printf("Indet: state %d trans %d, atom %d to %d : %d to %d\n",
563 i, j, trans->atom->no, trans->to, atomno, targetno);
564 printf(" previous to is %d\n", prev);
565#endif
566 ret->determinist = 0;
Daniel Veillard118aed72002-09-24 14:13:13 +0000567 if (transdata != NULL)
568 xmlFree(transdata);
Daniel Veillard23e73572002-09-19 19:56:43 +0000569 xmlFree(transitions);
570 xmlFree(stateRemap);
571 xmlFree(stringRemap);
572 for (i = 0;i < nbatoms;i++)
573 xmlFree(stringMap[i]);
574 xmlFree(stringMap);
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000575 goto not_determ;
Daniel Veillard23e73572002-09-19 19:56:43 +0000576 }
577 } else {
578#if 0
579 printf("State %d trans %d: atom %d to %d : %d to %d\n",
580 i, j, trans->atom->no, trans->to, atomno, targetno);
581#endif
582 transitions[stateno * (nbatoms + 1) + atomno + 1] =
Daniel Veillard118aed72002-09-24 14:13:13 +0000583 targetno + 1; /* to avoid 0 */
584 if (transdata != NULL)
585 transdata[stateno * nbatoms + atomno] =
586 trans->atom->data;
Daniel Veillard23e73572002-09-19 19:56:43 +0000587 }
588 }
589 }
590 ret->determinist = 1;
591#ifdef DEBUG_COMPACTION
592 /*
593 * Debug
594 */
595 for (i = 0;i < nbstates;i++) {
596 for (j = 0;j < nbatoms + 1;j++) {
597 printf("%02d ", transitions[i * (nbatoms + 1) + j]);
598 }
599 printf("\n");
600 }
601 printf("\n");
602#endif
603 /*
604 * Cleanup of the old data
605 */
606 if (ret->states != NULL) {
607 for (i = 0;i < ret->nbStates;i++)
608 xmlRegFreeState(ret->states[i]);
609 xmlFree(ret->states);
610 }
611 ret->states = NULL;
612 ret->nbStates = 0;
613 if (ret->atoms != NULL) {
614 for (i = 0;i < ret->nbAtoms;i++)
615 xmlRegFreeAtom(ret->atoms[i]);
616 xmlFree(ret->atoms);
617 }
618 ret->atoms = NULL;
619 ret->nbAtoms = 0;
620
621 ret->compact = transitions;
Daniel Veillard118aed72002-09-24 14:13:13 +0000622 ret->transdata = transdata;
Daniel Veillard23e73572002-09-19 19:56:43 +0000623 ret->stringMap = stringMap;
624 ret->nbstrings = nbatoms;
625 ret->nbstates = nbstates;
626 xmlFree(stateRemap);
627 xmlFree(stringRemap);
628 }
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000629not_determ:
630 ctxt->string = NULL;
631 ctxt->nbStates = 0;
632 ctxt->states = NULL;
633 ctxt->nbAtoms = 0;
634 ctxt->atoms = NULL;
635 ctxt->nbCounters = 0;
636 ctxt->counters = NULL;
Daniel Veillard4255d502002-04-16 15:50:10 +0000637 return(ret);
638}
639
640/**
641 * xmlRegNewParserCtxt:
642 * @string: the string to parse
643 *
644 * Allocate a new regexp parser context
645 *
646 * Returns the new context or NULL in case of error
647 */
648static xmlRegParserCtxtPtr
649xmlRegNewParserCtxt(const xmlChar *string) {
650 xmlRegParserCtxtPtr ret;
651
652 ret = (xmlRegParserCtxtPtr) xmlMalloc(sizeof(xmlRegParserCtxt));
653 if (ret == NULL)
654 return(NULL);
655 memset(ret, 0, sizeof(xmlRegParserCtxt));
656 if (string != NULL)
657 ret->string = xmlStrdup(string);
658 ret->cur = ret->string;
659 ret->neg = 0;
660 ret->error = 0;
Daniel Veillarde19fc232002-04-22 16:01:24 +0000661 ret->determinist = -1;
Daniel Veillard4255d502002-04-16 15:50:10 +0000662 return(ret);
663}
664
665/**
666 * xmlRegNewRange:
667 * @ctxt: the regexp parser context
668 * @neg: is that negative
669 * @type: the type of range
670 * @start: the start codepoint
671 * @end: the end codepoint
672 *
673 * Allocate a new regexp range
674 *
675 * Returns the new range or NULL in case of error
676 */
677static xmlRegRangePtr
678xmlRegNewRange(xmlRegParserCtxtPtr ctxt,
679 int neg, xmlRegAtomType type, int start, int end) {
680 xmlRegRangePtr ret;
681
682 ret = (xmlRegRangePtr) xmlMalloc(sizeof(xmlRegRange));
683 if (ret == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +0000684 xmlRegexpErrMemory(ctxt, "allocating range");
Daniel Veillard4255d502002-04-16 15:50:10 +0000685 return(NULL);
686 }
687 ret->neg = neg;
688 ret->type = type;
689 ret->start = start;
690 ret->end = end;
691 return(ret);
692}
693
694/**
695 * xmlRegFreeRange:
696 * @range: the regexp range
697 *
698 * Free a regexp range
699 */
700static void
701xmlRegFreeRange(xmlRegRangePtr range) {
702 if (range == NULL)
703 return;
704
705 if (range->blockName != NULL)
706 xmlFree(range->blockName);
707 xmlFree(range);
708}
709
710/**
711 * xmlRegNewAtom:
712 * @ctxt: the regexp parser context
713 * @type: the type of atom
714 *
715 * Allocate a new regexp range
716 *
717 * Returns the new atom or NULL in case of error
718 */
719static xmlRegAtomPtr
720xmlRegNewAtom(xmlRegParserCtxtPtr ctxt, xmlRegAtomType type) {
721 xmlRegAtomPtr ret;
722
723 ret = (xmlRegAtomPtr) xmlMalloc(sizeof(xmlRegAtom));
724 if (ret == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +0000725 xmlRegexpErrMemory(ctxt, "allocating atom");
Daniel Veillard4255d502002-04-16 15:50:10 +0000726 return(NULL);
727 }
728 memset(ret, 0, sizeof(xmlRegAtom));
729 ret->type = type;
730 ret->quant = XML_REGEXP_QUANT_ONCE;
731 ret->min = 0;
732 ret->max = 0;
733 return(ret);
734}
735
736/**
737 * xmlRegFreeAtom:
738 * @atom: the regexp atom
739 *
740 * Free a regexp atom
741 */
742static void
743xmlRegFreeAtom(xmlRegAtomPtr atom) {
744 int i;
745
746 if (atom == NULL)
747 return;
748
749 for (i = 0;i < atom->nbRanges;i++)
750 xmlRegFreeRange(atom->ranges[i]);
751 if (atom->ranges != NULL)
752 xmlFree(atom->ranges);
Daniel Veillardde0e4982005-07-03 14:35:44 +0000753 if ((atom->type == XML_REGEXP_STRING) && (atom->valuep != NULL))
754 xmlFree(atom->valuep);
Daniel Veillard77005e62005-07-19 16:26:18 +0000755 if ((atom->type == XML_REGEXP_STRING) && (atom->valuep2 != NULL))
756 xmlFree(atom->valuep2);
Daniel Veillardde0e4982005-07-03 14:35:44 +0000757 if ((atom->type == XML_REGEXP_BLOCK_NAME) && (atom->valuep != NULL))
Daniel Veillard4255d502002-04-16 15:50:10 +0000758 xmlFree(atom->valuep);
759 xmlFree(atom);
760}
761
762static xmlRegStatePtr
763xmlRegNewState(xmlRegParserCtxtPtr ctxt) {
764 xmlRegStatePtr ret;
765
766 ret = (xmlRegStatePtr) xmlMalloc(sizeof(xmlRegState));
767 if (ret == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +0000768 xmlRegexpErrMemory(ctxt, "allocating state");
Daniel Veillard4255d502002-04-16 15:50:10 +0000769 return(NULL);
770 }
771 memset(ret, 0, sizeof(xmlRegState));
772 ret->type = XML_REGEXP_TRANS_STATE;
773 ret->mark = XML_REGEXP_MARK_NORMAL;
774 return(ret);
775}
776
777/**
778 * xmlRegFreeState:
779 * @state: the regexp state
780 *
781 * Free a regexp state
782 */
783static void
784xmlRegFreeState(xmlRegStatePtr state) {
785 if (state == NULL)
786 return;
787
788 if (state->trans != NULL)
789 xmlFree(state->trans);
790 xmlFree(state);
791}
792
793/**
794 * xmlRegFreeParserCtxt:
795 * @ctxt: the regexp parser context
796 *
797 * Free a regexp parser context
798 */
799static void
800xmlRegFreeParserCtxt(xmlRegParserCtxtPtr ctxt) {
801 int i;
802 if (ctxt == NULL)
803 return;
804
805 if (ctxt->string != NULL)
806 xmlFree(ctxt->string);
807 if (ctxt->states != NULL) {
808 for (i = 0;i < ctxt->nbStates;i++)
809 xmlRegFreeState(ctxt->states[i]);
810 xmlFree(ctxt->states);
811 }
812 if (ctxt->atoms != NULL) {
813 for (i = 0;i < ctxt->nbAtoms;i++)
814 xmlRegFreeAtom(ctxt->atoms[i]);
815 xmlFree(ctxt->atoms);
816 }
817 if (ctxt->counters != NULL)
818 xmlFree(ctxt->counters);
819 xmlFree(ctxt);
820}
821
822/************************************************************************
823 * *
824 * Display of Data structures *
825 * *
826 ************************************************************************/
827
828static void
829xmlRegPrintAtomType(FILE *output, xmlRegAtomType type) {
830 switch (type) {
831 case XML_REGEXP_EPSILON:
832 fprintf(output, "epsilon "); break;
833 case XML_REGEXP_CHARVAL:
834 fprintf(output, "charval "); break;
835 case XML_REGEXP_RANGES:
836 fprintf(output, "ranges "); break;
837 case XML_REGEXP_SUBREG:
838 fprintf(output, "subexpr "); break;
839 case XML_REGEXP_STRING:
840 fprintf(output, "string "); break;
841 case XML_REGEXP_ANYCHAR:
842 fprintf(output, "anychar "); break;
843 case XML_REGEXP_ANYSPACE:
844 fprintf(output, "anyspace "); break;
845 case XML_REGEXP_NOTSPACE:
846 fprintf(output, "notspace "); break;
847 case XML_REGEXP_INITNAME:
848 fprintf(output, "initname "); break;
849 case XML_REGEXP_NOTINITNAME:
850 fprintf(output, "notinitname "); break;
851 case XML_REGEXP_NAMECHAR:
852 fprintf(output, "namechar "); break;
853 case XML_REGEXP_NOTNAMECHAR:
854 fprintf(output, "notnamechar "); break;
855 case XML_REGEXP_DECIMAL:
856 fprintf(output, "decimal "); break;
857 case XML_REGEXP_NOTDECIMAL:
858 fprintf(output, "notdecimal "); break;
859 case XML_REGEXP_REALCHAR:
860 fprintf(output, "realchar "); break;
861 case XML_REGEXP_NOTREALCHAR:
862 fprintf(output, "notrealchar "); break;
863 case XML_REGEXP_LETTER:
864 fprintf(output, "LETTER "); break;
865 case XML_REGEXP_LETTER_UPPERCASE:
866 fprintf(output, "LETTER_UPPERCASE "); break;
867 case XML_REGEXP_LETTER_LOWERCASE:
868 fprintf(output, "LETTER_LOWERCASE "); break;
869 case XML_REGEXP_LETTER_TITLECASE:
870 fprintf(output, "LETTER_TITLECASE "); break;
871 case XML_REGEXP_LETTER_MODIFIER:
872 fprintf(output, "LETTER_MODIFIER "); break;
873 case XML_REGEXP_LETTER_OTHERS:
874 fprintf(output, "LETTER_OTHERS "); break;
875 case XML_REGEXP_MARK:
876 fprintf(output, "MARK "); break;
877 case XML_REGEXP_MARK_NONSPACING:
878 fprintf(output, "MARK_NONSPACING "); break;
879 case XML_REGEXP_MARK_SPACECOMBINING:
880 fprintf(output, "MARK_SPACECOMBINING "); break;
881 case XML_REGEXP_MARK_ENCLOSING:
882 fprintf(output, "MARK_ENCLOSING "); break;
883 case XML_REGEXP_NUMBER:
884 fprintf(output, "NUMBER "); break;
885 case XML_REGEXP_NUMBER_DECIMAL:
886 fprintf(output, "NUMBER_DECIMAL "); break;
887 case XML_REGEXP_NUMBER_LETTER:
888 fprintf(output, "NUMBER_LETTER "); break;
889 case XML_REGEXP_NUMBER_OTHERS:
890 fprintf(output, "NUMBER_OTHERS "); break;
891 case XML_REGEXP_PUNCT:
892 fprintf(output, "PUNCT "); break;
893 case XML_REGEXP_PUNCT_CONNECTOR:
894 fprintf(output, "PUNCT_CONNECTOR "); break;
895 case XML_REGEXP_PUNCT_DASH:
896 fprintf(output, "PUNCT_DASH "); break;
897 case XML_REGEXP_PUNCT_OPEN:
898 fprintf(output, "PUNCT_OPEN "); break;
899 case XML_REGEXP_PUNCT_CLOSE:
900 fprintf(output, "PUNCT_CLOSE "); break;
901 case XML_REGEXP_PUNCT_INITQUOTE:
902 fprintf(output, "PUNCT_INITQUOTE "); break;
903 case XML_REGEXP_PUNCT_FINQUOTE:
904 fprintf(output, "PUNCT_FINQUOTE "); break;
905 case XML_REGEXP_PUNCT_OTHERS:
906 fprintf(output, "PUNCT_OTHERS "); break;
907 case XML_REGEXP_SEPAR:
908 fprintf(output, "SEPAR "); break;
909 case XML_REGEXP_SEPAR_SPACE:
910 fprintf(output, "SEPAR_SPACE "); break;
911 case XML_REGEXP_SEPAR_LINE:
912 fprintf(output, "SEPAR_LINE "); break;
913 case XML_REGEXP_SEPAR_PARA:
914 fprintf(output, "SEPAR_PARA "); break;
915 case XML_REGEXP_SYMBOL:
916 fprintf(output, "SYMBOL "); break;
917 case XML_REGEXP_SYMBOL_MATH:
918 fprintf(output, "SYMBOL_MATH "); break;
919 case XML_REGEXP_SYMBOL_CURRENCY:
920 fprintf(output, "SYMBOL_CURRENCY "); break;
921 case XML_REGEXP_SYMBOL_MODIFIER:
922 fprintf(output, "SYMBOL_MODIFIER "); break;
923 case XML_REGEXP_SYMBOL_OTHERS:
924 fprintf(output, "SYMBOL_OTHERS "); break;
925 case XML_REGEXP_OTHER:
926 fprintf(output, "OTHER "); break;
927 case XML_REGEXP_OTHER_CONTROL:
928 fprintf(output, "OTHER_CONTROL "); break;
929 case XML_REGEXP_OTHER_FORMAT:
930 fprintf(output, "OTHER_FORMAT "); break;
931 case XML_REGEXP_OTHER_PRIVATE:
932 fprintf(output, "OTHER_PRIVATE "); break;
933 case XML_REGEXP_OTHER_NA:
934 fprintf(output, "OTHER_NA "); break;
935 case XML_REGEXP_BLOCK_NAME:
936 fprintf(output, "BLOCK "); break;
937 }
938}
939
940static void
941xmlRegPrintQuantType(FILE *output, xmlRegQuantType type) {
942 switch (type) {
943 case XML_REGEXP_QUANT_EPSILON:
944 fprintf(output, "epsilon "); break;
945 case XML_REGEXP_QUANT_ONCE:
946 fprintf(output, "once "); break;
947 case XML_REGEXP_QUANT_OPT:
948 fprintf(output, "? "); break;
949 case XML_REGEXP_QUANT_MULT:
950 fprintf(output, "* "); break;
951 case XML_REGEXP_QUANT_PLUS:
952 fprintf(output, "+ "); break;
953 case XML_REGEXP_QUANT_RANGE:
954 fprintf(output, "range "); break;
Daniel Veillard7646b182002-04-20 06:41:40 +0000955 case XML_REGEXP_QUANT_ONCEONLY:
956 fprintf(output, "onceonly "); break;
957 case XML_REGEXP_QUANT_ALL:
958 fprintf(output, "all "); break;
Daniel Veillard4255d502002-04-16 15:50:10 +0000959 }
960}
961static void
962xmlRegPrintRange(FILE *output, xmlRegRangePtr range) {
963 fprintf(output, " range: ");
964 if (range->neg)
965 fprintf(output, "negative ");
966 xmlRegPrintAtomType(output, range->type);
967 fprintf(output, "%c - %c\n", range->start, range->end);
968}
969
970static void
971xmlRegPrintAtom(FILE *output, xmlRegAtomPtr atom) {
972 fprintf(output, " atom: ");
973 if (atom == NULL) {
974 fprintf(output, "NULL\n");
975 return;
976 }
Daniel Veillard9efc4762005-07-19 14:33:55 +0000977 if (atom->neg)
978 fprintf(output, "not ");
Daniel Veillard4255d502002-04-16 15:50:10 +0000979 xmlRegPrintAtomType(output, atom->type);
980 xmlRegPrintQuantType(output, atom->quant);
981 if (atom->quant == XML_REGEXP_QUANT_RANGE)
982 fprintf(output, "%d-%d ", atom->min, atom->max);
983 if (atom->type == XML_REGEXP_STRING)
984 fprintf(output, "'%s' ", (char *) atom->valuep);
985 if (atom->type == XML_REGEXP_CHARVAL)
986 fprintf(output, "char %c\n", atom->codepoint);
987 else if (atom->type == XML_REGEXP_RANGES) {
988 int i;
989 fprintf(output, "%d entries\n", atom->nbRanges);
990 for (i = 0; i < atom->nbRanges;i++)
991 xmlRegPrintRange(output, atom->ranges[i]);
992 } else if (atom->type == XML_REGEXP_SUBREG) {
993 fprintf(output, "start %d end %d\n", atom->start->no, atom->stop->no);
994 } else {
995 fprintf(output, "\n");
996 }
997}
998
999static void
1000xmlRegPrintTrans(FILE *output, xmlRegTransPtr trans) {
1001 fprintf(output, " trans: ");
1002 if (trans == NULL) {
1003 fprintf(output, "NULL\n");
1004 return;
1005 }
1006 if (trans->to < 0) {
1007 fprintf(output, "removed\n");
1008 return;
1009 }
1010 if (trans->counter >= 0) {
1011 fprintf(output, "counted %d, ", trans->counter);
1012 }
Daniel Veillard8a001f62002-04-20 07:24:11 +00001013 if (trans->count == REGEXP_ALL_COUNTER) {
1014 fprintf(output, "all transition, ");
1015 } else if (trans->count >= 0) {
Daniel Veillard4255d502002-04-16 15:50:10 +00001016 fprintf(output, "count based %d, ", trans->count);
1017 }
1018 if (trans->atom == NULL) {
1019 fprintf(output, "epsilon to %d\n", trans->to);
1020 return;
1021 }
1022 if (trans->atom->type == XML_REGEXP_CHARVAL)
1023 fprintf(output, "char %c ", trans->atom->codepoint);
1024 fprintf(output, "atom %d, to %d\n", trans->atom->no, trans->to);
1025}
1026
1027static void
1028xmlRegPrintState(FILE *output, xmlRegStatePtr state) {
1029 int i;
1030
1031 fprintf(output, " state: ");
1032 if (state == NULL) {
1033 fprintf(output, "NULL\n");
1034 return;
1035 }
1036 if (state->type == XML_REGEXP_START_STATE)
1037 fprintf(output, "START ");
1038 if (state->type == XML_REGEXP_FINAL_STATE)
1039 fprintf(output, "FINAL ");
1040
1041 fprintf(output, "%d, %d transitions:\n", state->no, state->nbTrans);
1042 for (i = 0;i < state->nbTrans; i++) {
1043 xmlRegPrintTrans(output, &(state->trans[i]));
1044 }
1045}
1046
Daniel Veillard23e73572002-09-19 19:56:43 +00001047#ifdef DEBUG_REGEXP_GRAPH
Daniel Veillard4255d502002-04-16 15:50:10 +00001048static void
1049xmlRegPrintCtxt(FILE *output, xmlRegParserCtxtPtr ctxt) {
1050 int i;
1051
1052 fprintf(output, " ctxt: ");
1053 if (ctxt == NULL) {
1054 fprintf(output, "NULL\n");
1055 return;
1056 }
1057 fprintf(output, "'%s' ", ctxt->string);
1058 if (ctxt->error)
1059 fprintf(output, "error ");
1060 if (ctxt->neg)
1061 fprintf(output, "neg ");
1062 fprintf(output, "\n");
1063 fprintf(output, "%d atoms:\n", ctxt->nbAtoms);
1064 for (i = 0;i < ctxt->nbAtoms; i++) {
1065 fprintf(output, " %02d ", i);
1066 xmlRegPrintAtom(output, ctxt->atoms[i]);
1067 }
1068 if (ctxt->atom != NULL) {
1069 fprintf(output, "current atom:\n");
1070 xmlRegPrintAtom(output, ctxt->atom);
1071 }
1072 fprintf(output, "%d states:", ctxt->nbStates);
1073 if (ctxt->start != NULL)
1074 fprintf(output, " start: %d", ctxt->start->no);
1075 if (ctxt->end != NULL)
1076 fprintf(output, " end: %d", ctxt->end->no);
1077 fprintf(output, "\n");
1078 for (i = 0;i < ctxt->nbStates; i++) {
1079 xmlRegPrintState(output, ctxt->states[i]);
1080 }
1081 fprintf(output, "%d counters:\n", ctxt->nbCounters);
1082 for (i = 0;i < ctxt->nbCounters; i++) {
1083 fprintf(output, " %d: min %d max %d\n", i, ctxt->counters[i].min,
1084 ctxt->counters[i].max);
1085 }
1086}
Daniel Veillard23e73572002-09-19 19:56:43 +00001087#endif
Daniel Veillard4255d502002-04-16 15:50:10 +00001088
1089/************************************************************************
1090 * *
1091 * Finite Automata structures manipulations *
1092 * *
1093 ************************************************************************/
1094
1095static void
1096xmlRegAtomAddRange(xmlRegParserCtxtPtr ctxt, xmlRegAtomPtr atom,
1097 int neg, xmlRegAtomType type, int start, int end,
1098 xmlChar *blockName) {
1099 xmlRegRangePtr range;
1100
1101 if (atom == NULL) {
1102 ERROR("add range: atom is NULL");
1103 return;
1104 }
1105 if (atom->type != XML_REGEXP_RANGES) {
1106 ERROR("add range: atom is not ranges");
1107 return;
1108 }
1109 if (atom->maxRanges == 0) {
1110 atom->maxRanges = 4;
1111 atom->ranges = (xmlRegRangePtr *) xmlMalloc(atom->maxRanges *
1112 sizeof(xmlRegRangePtr));
1113 if (atom->ranges == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00001114 xmlRegexpErrMemory(ctxt, "adding ranges");
Daniel Veillard4255d502002-04-16 15:50:10 +00001115 atom->maxRanges = 0;
1116 return;
1117 }
1118 } else if (atom->nbRanges >= atom->maxRanges) {
1119 xmlRegRangePtr *tmp;
1120 atom->maxRanges *= 2;
1121 tmp = (xmlRegRangePtr *) xmlRealloc(atom->ranges, atom->maxRanges *
1122 sizeof(xmlRegRangePtr));
1123 if (tmp == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00001124 xmlRegexpErrMemory(ctxt, "adding ranges");
Daniel Veillard4255d502002-04-16 15:50:10 +00001125 atom->maxRanges /= 2;
1126 return;
1127 }
1128 atom->ranges = tmp;
1129 }
1130 range = xmlRegNewRange(ctxt, neg, type, start, end);
1131 if (range == NULL)
1132 return;
1133 range->blockName = blockName;
1134 atom->ranges[atom->nbRanges++] = range;
1135
1136}
1137
1138static int
1139xmlRegGetCounter(xmlRegParserCtxtPtr ctxt) {
1140 if (ctxt->maxCounters == 0) {
1141 ctxt->maxCounters = 4;
1142 ctxt->counters = (xmlRegCounter *) xmlMalloc(ctxt->maxCounters *
1143 sizeof(xmlRegCounter));
1144 if (ctxt->counters == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00001145 xmlRegexpErrMemory(ctxt, "allocating counter");
Daniel Veillard4255d502002-04-16 15:50:10 +00001146 ctxt->maxCounters = 0;
1147 return(-1);
1148 }
1149 } else if (ctxt->nbCounters >= ctxt->maxCounters) {
1150 xmlRegCounter *tmp;
1151 ctxt->maxCounters *= 2;
1152 tmp = (xmlRegCounter *) xmlRealloc(ctxt->counters, ctxt->maxCounters *
1153 sizeof(xmlRegCounter));
1154 if (tmp == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00001155 xmlRegexpErrMemory(ctxt, "allocating counter");
Daniel Veillard4255d502002-04-16 15:50:10 +00001156 ctxt->maxCounters /= 2;
1157 return(-1);
1158 }
1159 ctxt->counters = tmp;
1160 }
1161 ctxt->counters[ctxt->nbCounters].min = -1;
1162 ctxt->counters[ctxt->nbCounters].max = -1;
1163 return(ctxt->nbCounters++);
1164}
1165
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001166static int
Daniel Veillard4255d502002-04-16 15:50:10 +00001167xmlRegAtomPush(xmlRegParserCtxtPtr ctxt, xmlRegAtomPtr atom) {
1168 if (atom == NULL) {
1169 ERROR("atom push: atom is NULL");
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001170 return(-1);
Daniel Veillard4255d502002-04-16 15:50:10 +00001171 }
1172 if (ctxt->maxAtoms == 0) {
1173 ctxt->maxAtoms = 4;
1174 ctxt->atoms = (xmlRegAtomPtr *) xmlMalloc(ctxt->maxAtoms *
1175 sizeof(xmlRegAtomPtr));
1176 if (ctxt->atoms == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00001177 xmlRegexpErrMemory(ctxt, "pushing atom");
Daniel Veillard4255d502002-04-16 15:50:10 +00001178 ctxt->maxAtoms = 0;
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001179 return(-1);
Daniel Veillard4255d502002-04-16 15:50:10 +00001180 }
1181 } else if (ctxt->nbAtoms >= ctxt->maxAtoms) {
1182 xmlRegAtomPtr *tmp;
1183 ctxt->maxAtoms *= 2;
1184 tmp = (xmlRegAtomPtr *) xmlRealloc(ctxt->atoms, ctxt->maxAtoms *
1185 sizeof(xmlRegAtomPtr));
1186 if (tmp == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00001187 xmlRegexpErrMemory(ctxt, "allocating counter");
Daniel Veillard4255d502002-04-16 15:50:10 +00001188 ctxt->maxAtoms /= 2;
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001189 return(-1);
Daniel Veillard4255d502002-04-16 15:50:10 +00001190 }
1191 ctxt->atoms = tmp;
1192 }
1193 atom->no = ctxt->nbAtoms;
1194 ctxt->atoms[ctxt->nbAtoms++] = atom;
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001195 return(0);
Daniel Veillard4255d502002-04-16 15:50:10 +00001196}
1197
1198static void
1199xmlRegStateAddTrans(xmlRegParserCtxtPtr ctxt, xmlRegStatePtr state,
1200 xmlRegAtomPtr atom, xmlRegStatePtr target,
1201 int counter, int count) {
William M. Brackf9b5fa22004-05-10 07:52:15 +00001202
1203 int nrtrans;
1204
Daniel Veillard4255d502002-04-16 15:50:10 +00001205 if (state == NULL) {
1206 ERROR("add state: state is NULL");
1207 return;
1208 }
1209 if (target == NULL) {
1210 ERROR("add state: target is NULL");
1211 return;
1212 }
William M. Brackf9b5fa22004-05-10 07:52:15 +00001213 /*
1214 * Other routines follow the philosophy 'When in doubt, add a transition'
1215 * so we check here whether such a transition is already present and, if
1216 * so, silently ignore this request.
1217 */
1218
1219 for (nrtrans=0; nrtrans<state->nbTrans; nrtrans++) {
1220 if ((state->trans[nrtrans].atom == atom) &&
1221 (state->trans[nrtrans].to == target->no) &&
1222 (state->trans[nrtrans].counter == counter) &&
1223 (state->trans[nrtrans].count == count)) {
1224#ifdef DEBUG_REGEXP_GRAPH
1225 printf("Ignoring duplicate transition from %d to %d\n",
1226 state->no, target->no);
1227#endif
1228 return;
1229 }
1230 }
1231
Daniel Veillard4255d502002-04-16 15:50:10 +00001232 if (state->maxTrans == 0) {
1233 state->maxTrans = 4;
1234 state->trans = (xmlRegTrans *) xmlMalloc(state->maxTrans *
1235 sizeof(xmlRegTrans));
1236 if (state->trans == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00001237 xmlRegexpErrMemory(ctxt, "adding transition");
Daniel Veillard4255d502002-04-16 15:50:10 +00001238 state->maxTrans = 0;
1239 return;
1240 }
1241 } else if (state->nbTrans >= state->maxTrans) {
1242 xmlRegTrans *tmp;
1243 state->maxTrans *= 2;
1244 tmp = (xmlRegTrans *) xmlRealloc(state->trans, state->maxTrans *
1245 sizeof(xmlRegTrans));
1246 if (tmp == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00001247 xmlRegexpErrMemory(ctxt, "adding transition");
Daniel Veillard4255d502002-04-16 15:50:10 +00001248 state->maxTrans /= 2;
1249 return;
1250 }
1251 state->trans = tmp;
1252 }
1253#ifdef DEBUG_REGEXP_GRAPH
1254 printf("Add trans from %d to %d ", state->no, target->no);
Daniel Veillard8a001f62002-04-20 07:24:11 +00001255 if (count == REGEXP_ALL_COUNTER)
Daniel Veillard2cbf5962004-03-31 15:50:43 +00001256 printf("all transition\n");
Daniel Veillard4402ab42002-09-12 16:02:56 +00001257 else if (count >= 0)
Daniel Veillard2cbf5962004-03-31 15:50:43 +00001258 printf("count based %d\n", count);
Daniel Veillard4255d502002-04-16 15:50:10 +00001259 else if (counter >= 0)
Daniel Veillard2cbf5962004-03-31 15:50:43 +00001260 printf("counted %d\n", counter);
Daniel Veillard4255d502002-04-16 15:50:10 +00001261 else if (atom == NULL)
Daniel Veillard2cbf5962004-03-31 15:50:43 +00001262 printf("epsilon transition\n");
1263 else if (atom != NULL)
1264 xmlRegPrintAtom(stdout, atom);
Daniel Veillard4255d502002-04-16 15:50:10 +00001265#endif
1266
1267 state->trans[state->nbTrans].atom = atom;
1268 state->trans[state->nbTrans].to = target->no;
1269 state->trans[state->nbTrans].counter = counter;
1270 state->trans[state->nbTrans].count = count;
1271 state->nbTrans++;
1272}
1273
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001274static int
Daniel Veillard4255d502002-04-16 15:50:10 +00001275xmlRegStatePush(xmlRegParserCtxtPtr ctxt, xmlRegStatePtr state) {
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001276 if (state == NULL) return(-1);
Daniel Veillard4255d502002-04-16 15:50:10 +00001277 if (ctxt->maxStates == 0) {
1278 ctxt->maxStates = 4;
1279 ctxt->states = (xmlRegStatePtr *) xmlMalloc(ctxt->maxStates *
1280 sizeof(xmlRegStatePtr));
1281 if (ctxt->states == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00001282 xmlRegexpErrMemory(ctxt, "adding state");
Daniel Veillard4255d502002-04-16 15:50:10 +00001283 ctxt->maxStates = 0;
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001284 return(-1);
Daniel Veillard4255d502002-04-16 15:50:10 +00001285 }
1286 } else if (ctxt->nbStates >= ctxt->maxStates) {
1287 xmlRegStatePtr *tmp;
1288 ctxt->maxStates *= 2;
1289 tmp = (xmlRegStatePtr *) xmlRealloc(ctxt->states, ctxt->maxStates *
1290 sizeof(xmlRegStatePtr));
1291 if (tmp == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00001292 xmlRegexpErrMemory(ctxt, "adding state");
Daniel Veillard4255d502002-04-16 15:50:10 +00001293 ctxt->maxStates /= 2;
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001294 return(-1);
Daniel Veillard4255d502002-04-16 15:50:10 +00001295 }
1296 ctxt->states = tmp;
1297 }
1298 state->no = ctxt->nbStates;
1299 ctxt->states[ctxt->nbStates++] = state;
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001300 return(0);
Daniel Veillard4255d502002-04-16 15:50:10 +00001301}
1302
1303/**
Daniel Veillard7646b182002-04-20 06:41:40 +00001304 * xmlFAGenerateAllTransition:
Daniel Veillard441bc322002-04-20 17:38:48 +00001305 * @ctxt: a regexp parser context
1306 * @from: the from state
1307 * @to: the target state or NULL for building a new one
1308 * @lax:
Daniel Veillard7646b182002-04-20 06:41:40 +00001309 *
1310 */
1311static void
1312xmlFAGenerateAllTransition(xmlRegParserCtxtPtr ctxt,
Daniel Veillard441bc322002-04-20 17:38:48 +00001313 xmlRegStatePtr from, xmlRegStatePtr to,
1314 int lax) {
Daniel Veillard7646b182002-04-20 06:41:40 +00001315 if (to == NULL) {
1316 to = xmlRegNewState(ctxt);
1317 xmlRegStatePush(ctxt, to);
1318 ctxt->state = to;
1319 }
Daniel Veillard441bc322002-04-20 17:38:48 +00001320 if (lax)
1321 xmlRegStateAddTrans(ctxt, from, NULL, to, -1, REGEXP_ALL_LAX_COUNTER);
1322 else
1323 xmlRegStateAddTrans(ctxt, from, NULL, to, -1, REGEXP_ALL_COUNTER);
Daniel Veillard7646b182002-04-20 06:41:40 +00001324}
1325
1326/**
Daniel Veillard4255d502002-04-16 15:50:10 +00001327 * xmlFAGenerateEpsilonTransition:
Daniel Veillard441bc322002-04-20 17:38:48 +00001328 * @ctxt: a regexp parser context
1329 * @from: the from state
1330 * @to: the target state or NULL for building a new one
Daniel Veillard4255d502002-04-16 15:50:10 +00001331 *
1332 */
1333static void
1334xmlFAGenerateEpsilonTransition(xmlRegParserCtxtPtr ctxt,
1335 xmlRegStatePtr from, xmlRegStatePtr to) {
1336 if (to == NULL) {
1337 to = xmlRegNewState(ctxt);
1338 xmlRegStatePush(ctxt, to);
1339 ctxt->state = to;
1340 }
1341 xmlRegStateAddTrans(ctxt, from, NULL, to, -1, -1);
1342}
1343
1344/**
1345 * xmlFAGenerateCountedEpsilonTransition:
Daniel Veillard441bc322002-04-20 17:38:48 +00001346 * @ctxt: a regexp parser context
1347 * @from: the from state
1348 * @to: the target state or NULL for building a new one
Daniel Veillard4255d502002-04-16 15:50:10 +00001349 * counter: the counter for that transition
1350 *
1351 */
1352static void
1353xmlFAGenerateCountedEpsilonTransition(xmlRegParserCtxtPtr ctxt,
1354 xmlRegStatePtr from, xmlRegStatePtr to, int counter) {
1355 if (to == NULL) {
1356 to = xmlRegNewState(ctxt);
1357 xmlRegStatePush(ctxt, to);
1358 ctxt->state = to;
1359 }
1360 xmlRegStateAddTrans(ctxt, from, NULL, to, counter, -1);
1361}
1362
1363/**
1364 * xmlFAGenerateCountedTransition:
Daniel Veillard441bc322002-04-20 17:38:48 +00001365 * @ctxt: a regexp parser context
1366 * @from: the from state
1367 * @to: the target state or NULL for building a new one
Daniel Veillard4255d502002-04-16 15:50:10 +00001368 * counter: the counter for that transition
1369 *
1370 */
1371static void
1372xmlFAGenerateCountedTransition(xmlRegParserCtxtPtr ctxt,
1373 xmlRegStatePtr from, xmlRegStatePtr to, int counter) {
1374 if (to == NULL) {
1375 to = xmlRegNewState(ctxt);
1376 xmlRegStatePush(ctxt, to);
1377 ctxt->state = to;
1378 }
1379 xmlRegStateAddTrans(ctxt, from, NULL, to, -1, counter);
1380}
1381
1382/**
1383 * xmlFAGenerateTransitions:
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
1387 * @atom: the atom generating the transition
Daniel Veillard4255d502002-04-16 15:50:10 +00001388 *
William M. Brackddf71d62004-05-06 04:17:26 +00001389 * Returns 0 if success and -1 in case of error.
Daniel Veillard4255d502002-04-16 15:50:10 +00001390 */
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001391static int
Daniel Veillard4255d502002-04-16 15:50:10 +00001392xmlFAGenerateTransitions(xmlRegParserCtxtPtr ctxt, xmlRegStatePtr from,
1393 xmlRegStatePtr to, xmlRegAtomPtr atom) {
1394 if (atom == NULL) {
1395 ERROR("genrate transition: atom == NULL");
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001396 return(-1);
Daniel Veillard4255d502002-04-16 15:50:10 +00001397 }
1398 if (atom->type == XML_REGEXP_SUBREG) {
1399 /*
1400 * this is a subexpression handling one should not need to
William M. Brackddf71d62004-05-06 04:17:26 +00001401 * create a new node except for XML_REGEXP_QUANT_RANGE.
Daniel Veillard4255d502002-04-16 15:50:10 +00001402 */
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001403 if (xmlRegAtomPush(ctxt, atom) < 0) {
1404 return(-1);
1405 }
Daniel Veillard4255d502002-04-16 15:50:10 +00001406 if ((to != NULL) && (atom->stop != to) &&
1407 (atom->quant != XML_REGEXP_QUANT_RANGE)) {
1408 /*
1409 * Generate an epsilon transition to link to the target
1410 */
1411 xmlFAGenerateEpsilonTransition(ctxt, atom->stop, to);
1412 }
1413 switch (atom->quant) {
1414 case XML_REGEXP_QUANT_OPT:
1415 atom->quant = XML_REGEXP_QUANT_ONCE;
1416 xmlFAGenerateEpsilonTransition(ctxt, atom->start, atom->stop);
1417 break;
1418 case XML_REGEXP_QUANT_MULT:
1419 atom->quant = XML_REGEXP_QUANT_ONCE;
1420 xmlFAGenerateEpsilonTransition(ctxt, atom->start, atom->stop);
1421 xmlFAGenerateEpsilonTransition(ctxt, atom->stop, atom->start);
1422 break;
1423 case XML_REGEXP_QUANT_PLUS:
1424 atom->quant = XML_REGEXP_QUANT_ONCE;
1425 xmlFAGenerateEpsilonTransition(ctxt, atom->stop, atom->start);
1426 break;
1427 case XML_REGEXP_QUANT_RANGE: {
1428 int counter;
1429 xmlRegStatePtr newstate;
1430
1431 /*
1432 * This one is nasty:
William M. Brackddf71d62004-05-06 04:17:26 +00001433 * 1/ if range has minOccurs == 0, create a new state
1434 * and create epsilon transitions from atom->start
1435 * to atom->stop, as well as atom->start to the new
1436 * state
1437 * 2/ register a new counter
1438 * 3/ register an epsilon transition associated to
Daniel Veillard4255d502002-04-16 15:50:10 +00001439 * this counter going from atom->stop to atom->start
William M. Brackddf71d62004-05-06 04:17:26 +00001440 * 4/ create a new state
1441 * 5/ generate a counted transition from atom->stop to
Daniel Veillard4255d502002-04-16 15:50:10 +00001442 * that state
1443 */
William M. Brackddf71d62004-05-06 04:17:26 +00001444 if (atom->min == 0) {
1445 xmlFAGenerateEpsilonTransition(ctxt, atom->start,
1446 atom->stop);
1447 newstate = xmlRegNewState(ctxt);
1448 xmlRegStatePush(ctxt, newstate);
1449 ctxt->state = newstate;
1450 xmlFAGenerateEpsilonTransition(ctxt, atom->start,
1451 newstate);
1452 }
Daniel Veillard4255d502002-04-16 15:50:10 +00001453 counter = xmlRegGetCounter(ctxt);
1454 ctxt->counters[counter].min = atom->min - 1;
1455 ctxt->counters[counter].max = atom->max - 1;
1456 atom->min = 0;
1457 atom->max = 0;
1458 atom->quant = XML_REGEXP_QUANT_ONCE;
1459 xmlFAGenerateCountedEpsilonTransition(ctxt, atom->stop,
1460 atom->start, counter);
1461 if (to != NULL) {
1462 newstate = to;
1463 } else {
1464 newstate = xmlRegNewState(ctxt);
1465 xmlRegStatePush(ctxt, newstate);
1466 ctxt->state = newstate;
1467 }
1468 xmlFAGenerateCountedTransition(ctxt, atom->stop,
1469 newstate, counter);
1470 }
1471 default:
1472 break;
1473 }
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001474 return(0);
Daniel Veillard99c394d2005-07-14 12:58:49 +00001475 } else if ((atom->min == 0) && (atom->max == 0) &&
1476 (atom->quant == XML_REGEXP_QUANT_RANGE)) {
1477 /*
1478 * we can discard the atom and generate an epsilon transition instead
1479 */
1480 if (to == NULL) {
1481 to = xmlRegNewState(ctxt);
1482 if (to != NULL)
1483 xmlRegStatePush(ctxt, to);
1484 else {
1485 return(-1);
1486 }
1487 }
1488 xmlFAGenerateEpsilonTransition(ctxt, from, to);
1489 ctxt->state = to;
1490 xmlRegFreeAtom(atom);
1491 return(0);
Daniel Veillard4255d502002-04-16 15:50:10 +00001492 } else {
1493 if (to == NULL) {
1494 to = xmlRegNewState(ctxt);
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001495 if (to != NULL)
1496 xmlRegStatePush(ctxt, to);
1497 else {
1498 return(-1);
1499 }
1500 }
1501 if (xmlRegAtomPush(ctxt, atom) < 0) {
1502 return(-1);
Daniel Veillard4255d502002-04-16 15:50:10 +00001503 }
1504 xmlRegStateAddTrans(ctxt, from, atom, to, -1, -1);
Daniel Veillard4255d502002-04-16 15:50:10 +00001505 ctxt->state = to;
1506 }
1507 switch (atom->quant) {
1508 case XML_REGEXP_QUANT_OPT:
1509 atom->quant = XML_REGEXP_QUANT_ONCE;
1510 xmlFAGenerateEpsilonTransition(ctxt, from, to);
1511 break;
1512 case XML_REGEXP_QUANT_MULT:
1513 atom->quant = XML_REGEXP_QUANT_ONCE;
1514 xmlFAGenerateEpsilonTransition(ctxt, from, to);
1515 xmlRegStateAddTrans(ctxt, to, atom, to, -1, -1);
1516 break;
1517 case XML_REGEXP_QUANT_PLUS:
1518 atom->quant = XML_REGEXP_QUANT_ONCE;
1519 xmlRegStateAddTrans(ctxt, to, atom, to, -1, -1);
1520 break;
1521 default:
1522 break;
1523 }
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001524 return(0);
Daniel Veillard4255d502002-04-16 15:50:10 +00001525}
1526
1527/**
1528 * xmlFAReduceEpsilonTransitions:
Daniel Veillard441bc322002-04-20 17:38:48 +00001529 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00001530 * @fromnr: the from state
1531 * @tonr: the to state
William M. Brackddf71d62004-05-06 04:17:26 +00001532 * @counter: should that transition be associated to a counted
Daniel Veillard4255d502002-04-16 15:50:10 +00001533 *
1534 */
1535static void
1536xmlFAReduceEpsilonTransitions(xmlRegParserCtxtPtr ctxt, int fromnr,
1537 int tonr, int counter) {
1538 int transnr;
1539 xmlRegStatePtr from;
1540 xmlRegStatePtr to;
1541
1542#ifdef DEBUG_REGEXP_GRAPH
1543 printf("xmlFAReduceEpsilonTransitions(%d, %d)\n", fromnr, tonr);
1544#endif
1545 from = ctxt->states[fromnr];
1546 if (from == NULL)
1547 return;
1548 to = ctxt->states[tonr];
1549 if (to == NULL)
1550 return;
1551 if ((to->mark == XML_REGEXP_MARK_START) ||
1552 (to->mark == XML_REGEXP_MARK_VISITED))
1553 return;
1554
1555 to->mark = XML_REGEXP_MARK_VISITED;
1556 if (to->type == XML_REGEXP_FINAL_STATE) {
1557#ifdef DEBUG_REGEXP_GRAPH
1558 printf("State %d is final, so %d becomes final\n", tonr, fromnr);
1559#endif
1560 from->type = XML_REGEXP_FINAL_STATE;
1561 }
1562 for (transnr = 0;transnr < to->nbTrans;transnr++) {
1563 if (to->trans[transnr].atom == NULL) {
1564 /*
1565 * Don't remove counted transitions
1566 * Don't loop either
1567 */
Daniel Veillardb509f152002-04-17 16:28:10 +00001568 if (to->trans[transnr].to != fromnr) {
1569 if (to->trans[transnr].count >= 0) {
1570 int newto = to->trans[transnr].to;
1571
1572 xmlRegStateAddTrans(ctxt, from, NULL,
1573 ctxt->states[newto],
1574 -1, to->trans[transnr].count);
1575 } else {
Daniel Veillard4255d502002-04-16 15:50:10 +00001576#ifdef DEBUG_REGEXP_GRAPH
Daniel Veillardb509f152002-04-17 16:28:10 +00001577 printf("Found epsilon trans %d from %d to %d\n",
1578 transnr, tonr, to->trans[transnr].to);
Daniel Veillard4255d502002-04-16 15:50:10 +00001579#endif
Daniel Veillardb509f152002-04-17 16:28:10 +00001580 if (to->trans[transnr].counter >= 0) {
1581 xmlFAReduceEpsilonTransitions(ctxt, fromnr,
1582 to->trans[transnr].to,
1583 to->trans[transnr].counter);
1584 } else {
1585 xmlFAReduceEpsilonTransitions(ctxt, fromnr,
1586 to->trans[transnr].to,
1587 counter);
1588 }
1589 }
Daniel Veillard4255d502002-04-16 15:50:10 +00001590 }
1591 } else {
1592 int newto = to->trans[transnr].to;
1593
Daniel Veillardb509f152002-04-17 16:28:10 +00001594 if (to->trans[transnr].counter >= 0) {
1595 xmlRegStateAddTrans(ctxt, from, to->trans[transnr].atom,
1596 ctxt->states[newto],
1597 to->trans[transnr].counter, -1);
1598 } else {
1599 xmlRegStateAddTrans(ctxt, from, to->trans[transnr].atom,
1600 ctxt->states[newto], counter, -1);
1601 }
Daniel Veillard4255d502002-04-16 15:50:10 +00001602 }
1603 }
1604 to->mark = XML_REGEXP_MARK_NORMAL;
1605}
1606
1607/**
1608 * xmlFAEliminateEpsilonTransitions:
Daniel Veillard441bc322002-04-20 17:38:48 +00001609 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00001610 *
1611 */
1612static void
1613xmlFAEliminateEpsilonTransitions(xmlRegParserCtxtPtr ctxt) {
1614 int statenr, transnr;
1615 xmlRegStatePtr state;
1616
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001617 if (ctxt->states == NULL) return;
1618
1619
Daniel Veillard4255d502002-04-16 15:50:10 +00001620 /*
1621 * build the completed transitions bypassing the epsilons
1622 * Use a marking algorithm to avoid loops
Daniel Veillardcc026dc2005-01-12 13:21:17 +00001623 * mark sink states too.
Daniel Veillard4255d502002-04-16 15:50:10 +00001624 */
1625 for (statenr = 0;statenr < ctxt->nbStates;statenr++) {
1626 state = ctxt->states[statenr];
1627 if (state == NULL)
1628 continue;
Daniel Veillardcc026dc2005-01-12 13:21:17 +00001629 if ((state->nbTrans == 0) &&
1630 (state->type != XML_REGEXP_FINAL_STATE)) {
1631 state->type = XML_REGEXP_SINK_STATE;
1632 }
Daniel Veillard4255d502002-04-16 15:50:10 +00001633 for (transnr = 0;transnr < state->nbTrans;transnr++) {
1634 if ((state->trans[transnr].atom == NULL) &&
1635 (state->trans[transnr].to >= 0)) {
1636 if (state->trans[transnr].to == statenr) {
1637 state->trans[transnr].to = -1;
1638#ifdef DEBUG_REGEXP_GRAPH
1639 printf("Removed loopback epsilon trans %d on %d\n",
1640 transnr, statenr);
1641#endif
1642 } else if (state->trans[transnr].count < 0) {
1643 int newto = state->trans[transnr].to;
1644
1645#ifdef DEBUG_REGEXP_GRAPH
1646 printf("Found epsilon trans %d from %d to %d\n",
1647 transnr, statenr, newto);
1648#endif
1649 state->mark = XML_REGEXP_MARK_START;
1650 xmlFAReduceEpsilonTransitions(ctxt, statenr,
1651 newto, state->trans[transnr].counter);
1652 state->mark = XML_REGEXP_MARK_NORMAL;
1653#ifdef DEBUG_REGEXP_GRAPH
1654 } else {
1655 printf("Found counted transition %d on %d\n",
1656 transnr, statenr);
1657#endif
1658 }
1659 }
1660 }
1661 }
1662 /*
1663 * Eliminate the epsilon transitions
1664 */
1665 for (statenr = 0;statenr < ctxt->nbStates;statenr++) {
1666 state = ctxt->states[statenr];
1667 if (state == NULL)
1668 continue;
1669 for (transnr = 0;transnr < state->nbTrans;transnr++) {
1670 if ((state->trans[transnr].atom == NULL) &&
1671 (state->trans[transnr].count < 0) &&
1672 (state->trans[transnr].to >= 0)) {
1673 state->trans[transnr].to = -1;
1674 }
1675 }
1676 }
Daniel Veillard23e73572002-09-19 19:56:43 +00001677
1678 /*
1679 * Use this pass to detect unreachable states too
1680 */
1681 for (statenr = 0;statenr < ctxt->nbStates;statenr++) {
1682 state = ctxt->states[statenr];
1683 if (state != NULL)
William M. Brack779af002003-08-01 15:55:39 +00001684 state->reached = XML_REGEXP_MARK_NORMAL;
Daniel Veillard23e73572002-09-19 19:56:43 +00001685 }
1686 state = ctxt->states[0];
1687 if (state != NULL)
William M. Brack779af002003-08-01 15:55:39 +00001688 state->reached = XML_REGEXP_MARK_START;
Daniel Veillard23e73572002-09-19 19:56:43 +00001689 while (state != NULL) {
1690 xmlRegStatePtr target = NULL;
William M. Brack779af002003-08-01 15:55:39 +00001691 state->reached = XML_REGEXP_MARK_VISITED;
Daniel Veillard23e73572002-09-19 19:56:43 +00001692 /*
William M. Brackddf71d62004-05-06 04:17:26 +00001693 * Mark all states reachable from the current reachable state
Daniel Veillard23e73572002-09-19 19:56:43 +00001694 */
1695 for (transnr = 0;transnr < state->nbTrans;transnr++) {
1696 if ((state->trans[transnr].to >= 0) &&
1697 ((state->trans[transnr].atom != NULL) ||
1698 (state->trans[transnr].count >= 0))) {
1699 int newto = state->trans[transnr].to;
1700
1701 if (ctxt->states[newto] == NULL)
1702 continue;
William M. Brack779af002003-08-01 15:55:39 +00001703 if (ctxt->states[newto]->reached == XML_REGEXP_MARK_NORMAL) {
1704 ctxt->states[newto]->reached = XML_REGEXP_MARK_START;
Daniel Veillard23e73572002-09-19 19:56:43 +00001705 target = ctxt->states[newto];
1706 }
1707 }
1708 }
Daniel Veillardcc026dc2005-01-12 13:21:17 +00001709
Daniel Veillard23e73572002-09-19 19:56:43 +00001710 /*
1711 * find the next accessible state not explored
1712 */
1713 if (target == NULL) {
1714 for (statenr = 1;statenr < ctxt->nbStates;statenr++) {
1715 state = ctxt->states[statenr];
William M. Brack779af002003-08-01 15:55:39 +00001716 if ((state != NULL) && (state->reached ==
1717 XML_REGEXP_MARK_START)) {
Daniel Veillard23e73572002-09-19 19:56:43 +00001718 target = state;
1719 break;
1720 }
1721 }
1722 }
1723 state = target;
1724 }
1725 for (statenr = 0;statenr < ctxt->nbStates;statenr++) {
1726 state = ctxt->states[statenr];
William M. Brack779af002003-08-01 15:55:39 +00001727 if ((state != NULL) && (state->reached == XML_REGEXP_MARK_NORMAL)) {
Daniel Veillard23e73572002-09-19 19:56:43 +00001728#ifdef DEBUG_REGEXP_GRAPH
1729 printf("Removed unreachable state %d\n", statenr);
1730#endif
1731 xmlRegFreeState(state);
1732 ctxt->states[statenr] = NULL;
1733 }
1734 }
1735
Daniel Veillard4255d502002-04-16 15:50:10 +00001736}
1737
Daniel Veillarde19fc232002-04-22 16:01:24 +00001738/**
1739 * xmlFACompareAtoms:
1740 * @atom1: an atom
1741 * @atom2: an atom
1742 *
William M. Brackddf71d62004-05-06 04:17:26 +00001743 * Compares two atoms to check whether they are equivalents
Daniel Veillarde19fc232002-04-22 16:01:24 +00001744 *
1745 * Returns 1 if yes and 0 otherwise
1746 */
1747static int
1748xmlFACompareAtoms(xmlRegAtomPtr atom1, xmlRegAtomPtr atom2) {
Daniel Veillard9efc4762005-07-19 14:33:55 +00001749 int ret;
1750
Daniel Veillarde19fc232002-04-22 16:01:24 +00001751 if (atom1 == atom2)
1752 return(1);
1753 if ((atom1 == NULL) || (atom2 == NULL))
1754 return(0);
1755
1756 if (atom1->type != atom2->type)
1757 return(0);
1758 switch (atom1->type) {
1759 case XML_REGEXP_STRING:
Daniel Veillard9efc4762005-07-19 14:33:55 +00001760 ret = xmlRegStrEqualWildcard((xmlChar *)atom1->valuep,
1761 (xmlChar *)atom2->valuep);
1762 break;
Daniel Veillarde19fc232002-04-22 16:01:24 +00001763 case XML_REGEXP_EPSILON:
1764 return(1);
1765 case XML_REGEXP_CHARVAL:
Daniel Veillard9efc4762005-07-19 14:33:55 +00001766 ret = atom1->codepoint == atom2->codepoint;
1767 break;
Daniel Veillarde19fc232002-04-22 16:01:24 +00001768 case XML_REGEXP_RANGES:
1769 TODO;
1770 return(0);
1771 default:
Daniel Veillard9efc4762005-07-19 14:33:55 +00001772 return(1);
Daniel Veillarde19fc232002-04-22 16:01:24 +00001773 }
Daniel Veillard9efc4762005-07-19 14:33:55 +00001774 if (atom1->neg != atom2->neg)
1775 ret = !ret;
1776 return(ret);
Daniel Veillarde19fc232002-04-22 16:01:24 +00001777}
1778
1779/**
1780 * xmlFARecurseDeterminism:
1781 * @ctxt: a regexp parser context
1782 *
1783 * Check whether the associated regexp is determinist,
1784 * should be called after xmlFAEliminateEpsilonTransitions()
1785 *
1786 */
1787static int
1788xmlFARecurseDeterminism(xmlRegParserCtxtPtr ctxt, xmlRegStatePtr state,
1789 int to, xmlRegAtomPtr atom) {
1790 int ret = 1;
1791 int transnr;
1792 xmlRegTransPtr t1;
1793
1794 if (state == NULL)
1795 return(ret);
1796 for (transnr = 0;transnr < state->nbTrans;transnr++) {
1797 t1 = &(state->trans[transnr]);
1798 /*
1799 * check transitions conflicting with the one looked at
1800 */
1801 if (t1->atom == NULL) {
1802 if (t1->to == -1)
1803 continue;
1804 ret = xmlFARecurseDeterminism(ctxt, ctxt->states[t1->to],
1805 to, atom);
1806 if (ret == 0)
1807 return(0);
1808 continue;
1809 }
1810 if (t1->to != to)
1811 continue;
1812 if (xmlFACompareAtoms(t1->atom, atom))
1813 return(0);
1814 }
1815 return(ret);
1816}
1817
1818/**
1819 * xmlFAComputesDeterminism:
1820 * @ctxt: a regexp parser context
1821 *
1822 * Check whether the associated regexp is determinist,
1823 * should be called after xmlFAEliminateEpsilonTransitions()
1824 *
1825 */
1826static int
1827xmlFAComputesDeterminism(xmlRegParserCtxtPtr ctxt) {
1828 int statenr, transnr;
1829 xmlRegStatePtr state;
1830 xmlRegTransPtr t1, t2;
1831 int i;
1832 int ret = 1;
1833
Daniel Veillard4402ab42002-09-12 16:02:56 +00001834#ifdef DEBUG_REGEXP_GRAPH
1835 printf("xmlFAComputesDeterminism\n");
1836 xmlRegPrintCtxt(stdout, ctxt);
1837#endif
Daniel Veillarde19fc232002-04-22 16:01:24 +00001838 if (ctxt->determinist != -1)
1839 return(ctxt->determinist);
1840
1841 /*
William M. Brackddf71d62004-05-06 04:17:26 +00001842 * Check for all states that there aren't 2 transitions
Daniel Veillarde19fc232002-04-22 16:01:24 +00001843 * with the same atom and a different target.
1844 */
1845 for (statenr = 0;statenr < ctxt->nbStates;statenr++) {
1846 state = ctxt->states[statenr];
1847 if (state == NULL)
1848 continue;
1849 for (transnr = 0;transnr < state->nbTrans;transnr++) {
1850 t1 = &(state->trans[transnr]);
1851 /*
1852 * Determinism checks in case of counted or all transitions
1853 * will have to be handled separately
1854 */
1855 if (t1->atom == NULL)
1856 continue;
1857 if (t1->to == -1) /* eliminated */
1858 continue;
1859 for (i = 0;i < transnr;i++) {
1860 t2 = &(state->trans[i]);
1861 if (t2->to == -1) /* eliminated */
1862 continue;
1863 if (t2->atom != NULL) {
1864 if (t1->to == t2->to) {
1865 if (xmlFACompareAtoms(t1->atom, t2->atom))
William M. Brackddf71d62004-05-06 04:17:26 +00001866 t2->to = -1; /* eliminated */
Daniel Veillarde19fc232002-04-22 16:01:24 +00001867 } else {
1868 /* not determinist ! */
1869 if (xmlFACompareAtoms(t1->atom, t2->atom))
1870 ret = 0;
1871 }
1872 } else if (t1->to != -1) {
1873 /*
1874 * do the closure in case of remaining specific
1875 * epsilon transitions like choices or all
1876 */
1877 ret = xmlFARecurseDeterminism(ctxt, ctxt->states[t1->to],
1878 t2->to, t2->atom);
1879 if (ret == 0)
1880 return(0);
1881 }
1882 }
1883 if (ret == 0)
1884 break;
1885 }
1886 if (ret == 0)
1887 break;
1888 }
1889 ctxt->determinist = ret;
1890 return(ret);
1891}
1892
Daniel Veillard4255d502002-04-16 15:50:10 +00001893/************************************************************************
1894 * *
1895 * Routines to check input against transition atoms *
1896 * *
1897 ************************************************************************/
1898
1899static int
1900xmlRegCheckCharacterRange(xmlRegAtomType type, int codepoint, int neg,
1901 int start, int end, const xmlChar *blockName) {
1902 int ret = 0;
1903
1904 switch (type) {
1905 case XML_REGEXP_STRING:
1906 case XML_REGEXP_SUBREG:
1907 case XML_REGEXP_RANGES:
1908 case XML_REGEXP_EPSILON:
1909 return(-1);
1910 case XML_REGEXP_ANYCHAR:
1911 ret = ((codepoint != '\n') && (codepoint != '\r'));
1912 break;
1913 case XML_REGEXP_CHARVAL:
1914 ret = ((codepoint >= start) && (codepoint <= end));
1915 break;
1916 case XML_REGEXP_NOTSPACE:
1917 neg = !neg;
1918 case XML_REGEXP_ANYSPACE:
1919 ret = ((codepoint == '\n') || (codepoint == '\r') ||
1920 (codepoint == '\t') || (codepoint == ' '));
1921 break;
1922 case XML_REGEXP_NOTINITNAME:
1923 neg = !neg;
1924 case XML_REGEXP_INITNAME:
William M. Brack871611b2003-10-18 04:53:14 +00001925 ret = (IS_LETTER(codepoint) ||
Daniel Veillard4255d502002-04-16 15:50:10 +00001926 (codepoint == '_') || (codepoint == ':'));
1927 break;
1928 case XML_REGEXP_NOTNAMECHAR:
1929 neg = !neg;
1930 case XML_REGEXP_NAMECHAR:
William M. Brack871611b2003-10-18 04:53:14 +00001931 ret = (IS_LETTER(codepoint) || IS_DIGIT(codepoint) ||
Daniel Veillard4255d502002-04-16 15:50:10 +00001932 (codepoint == '.') || (codepoint == '-') ||
1933 (codepoint == '_') || (codepoint == ':') ||
William M. Brack871611b2003-10-18 04:53:14 +00001934 IS_COMBINING(codepoint) || IS_EXTENDER(codepoint));
Daniel Veillard4255d502002-04-16 15:50:10 +00001935 break;
1936 case XML_REGEXP_NOTDECIMAL:
1937 neg = !neg;
1938 case XML_REGEXP_DECIMAL:
1939 ret = xmlUCSIsCatNd(codepoint);
1940 break;
1941 case XML_REGEXP_REALCHAR:
1942 neg = !neg;
1943 case XML_REGEXP_NOTREALCHAR:
1944 ret = xmlUCSIsCatP(codepoint);
1945 if (ret == 0)
1946 ret = xmlUCSIsCatZ(codepoint);
1947 if (ret == 0)
1948 ret = xmlUCSIsCatC(codepoint);
1949 break;
1950 case XML_REGEXP_LETTER:
1951 ret = xmlUCSIsCatL(codepoint);
1952 break;
1953 case XML_REGEXP_LETTER_UPPERCASE:
1954 ret = xmlUCSIsCatLu(codepoint);
1955 break;
1956 case XML_REGEXP_LETTER_LOWERCASE:
1957 ret = xmlUCSIsCatLl(codepoint);
1958 break;
1959 case XML_REGEXP_LETTER_TITLECASE:
1960 ret = xmlUCSIsCatLt(codepoint);
1961 break;
1962 case XML_REGEXP_LETTER_MODIFIER:
1963 ret = xmlUCSIsCatLm(codepoint);
1964 break;
1965 case XML_REGEXP_LETTER_OTHERS:
1966 ret = xmlUCSIsCatLo(codepoint);
1967 break;
1968 case XML_REGEXP_MARK:
1969 ret = xmlUCSIsCatM(codepoint);
1970 break;
1971 case XML_REGEXP_MARK_NONSPACING:
1972 ret = xmlUCSIsCatMn(codepoint);
1973 break;
1974 case XML_REGEXP_MARK_SPACECOMBINING:
1975 ret = xmlUCSIsCatMc(codepoint);
1976 break;
1977 case XML_REGEXP_MARK_ENCLOSING:
1978 ret = xmlUCSIsCatMe(codepoint);
1979 break;
1980 case XML_REGEXP_NUMBER:
1981 ret = xmlUCSIsCatN(codepoint);
1982 break;
1983 case XML_REGEXP_NUMBER_DECIMAL:
1984 ret = xmlUCSIsCatNd(codepoint);
1985 break;
1986 case XML_REGEXP_NUMBER_LETTER:
1987 ret = xmlUCSIsCatNl(codepoint);
1988 break;
1989 case XML_REGEXP_NUMBER_OTHERS:
1990 ret = xmlUCSIsCatNo(codepoint);
1991 break;
1992 case XML_REGEXP_PUNCT:
1993 ret = xmlUCSIsCatP(codepoint);
1994 break;
1995 case XML_REGEXP_PUNCT_CONNECTOR:
1996 ret = xmlUCSIsCatPc(codepoint);
1997 break;
1998 case XML_REGEXP_PUNCT_DASH:
1999 ret = xmlUCSIsCatPd(codepoint);
2000 break;
2001 case XML_REGEXP_PUNCT_OPEN:
2002 ret = xmlUCSIsCatPs(codepoint);
2003 break;
2004 case XML_REGEXP_PUNCT_CLOSE:
2005 ret = xmlUCSIsCatPe(codepoint);
2006 break;
2007 case XML_REGEXP_PUNCT_INITQUOTE:
2008 ret = xmlUCSIsCatPi(codepoint);
2009 break;
2010 case XML_REGEXP_PUNCT_FINQUOTE:
2011 ret = xmlUCSIsCatPf(codepoint);
2012 break;
2013 case XML_REGEXP_PUNCT_OTHERS:
2014 ret = xmlUCSIsCatPo(codepoint);
2015 break;
2016 case XML_REGEXP_SEPAR:
2017 ret = xmlUCSIsCatZ(codepoint);
2018 break;
2019 case XML_REGEXP_SEPAR_SPACE:
2020 ret = xmlUCSIsCatZs(codepoint);
2021 break;
2022 case XML_REGEXP_SEPAR_LINE:
2023 ret = xmlUCSIsCatZl(codepoint);
2024 break;
2025 case XML_REGEXP_SEPAR_PARA:
2026 ret = xmlUCSIsCatZp(codepoint);
2027 break;
2028 case XML_REGEXP_SYMBOL:
2029 ret = xmlUCSIsCatS(codepoint);
2030 break;
2031 case XML_REGEXP_SYMBOL_MATH:
2032 ret = xmlUCSIsCatSm(codepoint);
2033 break;
2034 case XML_REGEXP_SYMBOL_CURRENCY:
2035 ret = xmlUCSIsCatSc(codepoint);
2036 break;
2037 case XML_REGEXP_SYMBOL_MODIFIER:
2038 ret = xmlUCSIsCatSk(codepoint);
2039 break;
2040 case XML_REGEXP_SYMBOL_OTHERS:
2041 ret = xmlUCSIsCatSo(codepoint);
2042 break;
2043 case XML_REGEXP_OTHER:
2044 ret = xmlUCSIsCatC(codepoint);
2045 break;
2046 case XML_REGEXP_OTHER_CONTROL:
2047 ret = xmlUCSIsCatCc(codepoint);
2048 break;
2049 case XML_REGEXP_OTHER_FORMAT:
2050 ret = xmlUCSIsCatCf(codepoint);
2051 break;
2052 case XML_REGEXP_OTHER_PRIVATE:
2053 ret = xmlUCSIsCatCo(codepoint);
2054 break;
2055 case XML_REGEXP_OTHER_NA:
2056 /* ret = xmlUCSIsCatCn(codepoint); */
2057 /* Seems it doesn't exist anymore in recent Unicode releases */
2058 ret = 0;
2059 break;
2060 case XML_REGEXP_BLOCK_NAME:
2061 ret = xmlUCSIsBlock(codepoint, (const char *) blockName);
2062 break;
2063 }
2064 if (neg)
2065 return(!ret);
2066 return(ret);
2067}
2068
2069static int
2070xmlRegCheckCharacter(xmlRegAtomPtr atom, int codepoint) {
2071 int i, ret = 0;
2072 xmlRegRangePtr range;
2073
William M. Brack871611b2003-10-18 04:53:14 +00002074 if ((atom == NULL) || (!IS_CHAR(codepoint)))
Daniel Veillard4255d502002-04-16 15:50:10 +00002075 return(-1);
2076
2077 switch (atom->type) {
2078 case XML_REGEXP_SUBREG:
2079 case XML_REGEXP_EPSILON:
2080 return(-1);
2081 case XML_REGEXP_CHARVAL:
2082 return(codepoint == atom->codepoint);
2083 case XML_REGEXP_RANGES: {
2084 int accept = 0;
Daniel Veillardf2a12832003-11-24 13:04:35 +00002085
Daniel Veillard4255d502002-04-16 15:50:10 +00002086 for (i = 0;i < atom->nbRanges;i++) {
2087 range = atom->ranges[i];
Daniel Veillardf8b9de32003-11-24 14:27:26 +00002088 if (range->neg == 2) {
Daniel Veillard4255d502002-04-16 15:50:10 +00002089 ret = xmlRegCheckCharacterRange(range->type, codepoint,
2090 0, range->start, range->end,
2091 range->blockName);
2092 if (ret != 0)
2093 return(0); /* excluded char */
Daniel Veillardf8b9de32003-11-24 14:27:26 +00002094 } else if (range->neg) {
2095 ret = xmlRegCheckCharacterRange(range->type, codepoint,
2096 0, range->start, range->end,
2097 range->blockName);
2098 if (ret == 0)
Daniel Veillardf2a12832003-11-24 13:04:35 +00002099 accept = 1;
Daniel Veillardf8b9de32003-11-24 14:27:26 +00002100 else
2101 return(0);
Daniel Veillard4255d502002-04-16 15:50:10 +00002102 } else {
2103 ret = xmlRegCheckCharacterRange(range->type, codepoint,
2104 0, range->start, range->end,
2105 range->blockName);
2106 if (ret != 0)
2107 accept = 1; /* might still be excluded */
2108 }
2109 }
2110 return(accept);
2111 }
2112 case XML_REGEXP_STRING:
2113 printf("TODO: XML_REGEXP_STRING\n");
2114 return(-1);
2115 case XML_REGEXP_ANYCHAR:
2116 case XML_REGEXP_ANYSPACE:
2117 case XML_REGEXP_NOTSPACE:
2118 case XML_REGEXP_INITNAME:
2119 case XML_REGEXP_NOTINITNAME:
2120 case XML_REGEXP_NAMECHAR:
2121 case XML_REGEXP_NOTNAMECHAR:
2122 case XML_REGEXP_DECIMAL:
2123 case XML_REGEXP_NOTDECIMAL:
2124 case XML_REGEXP_REALCHAR:
2125 case XML_REGEXP_NOTREALCHAR:
2126 case XML_REGEXP_LETTER:
2127 case XML_REGEXP_LETTER_UPPERCASE:
2128 case XML_REGEXP_LETTER_LOWERCASE:
2129 case XML_REGEXP_LETTER_TITLECASE:
2130 case XML_REGEXP_LETTER_MODIFIER:
2131 case XML_REGEXP_LETTER_OTHERS:
2132 case XML_REGEXP_MARK:
2133 case XML_REGEXP_MARK_NONSPACING:
2134 case XML_REGEXP_MARK_SPACECOMBINING:
2135 case XML_REGEXP_MARK_ENCLOSING:
2136 case XML_REGEXP_NUMBER:
2137 case XML_REGEXP_NUMBER_DECIMAL:
2138 case XML_REGEXP_NUMBER_LETTER:
2139 case XML_REGEXP_NUMBER_OTHERS:
2140 case XML_REGEXP_PUNCT:
2141 case XML_REGEXP_PUNCT_CONNECTOR:
2142 case XML_REGEXP_PUNCT_DASH:
2143 case XML_REGEXP_PUNCT_OPEN:
2144 case XML_REGEXP_PUNCT_CLOSE:
2145 case XML_REGEXP_PUNCT_INITQUOTE:
2146 case XML_REGEXP_PUNCT_FINQUOTE:
2147 case XML_REGEXP_PUNCT_OTHERS:
2148 case XML_REGEXP_SEPAR:
2149 case XML_REGEXP_SEPAR_SPACE:
2150 case XML_REGEXP_SEPAR_LINE:
2151 case XML_REGEXP_SEPAR_PARA:
2152 case XML_REGEXP_SYMBOL:
2153 case XML_REGEXP_SYMBOL_MATH:
2154 case XML_REGEXP_SYMBOL_CURRENCY:
2155 case XML_REGEXP_SYMBOL_MODIFIER:
2156 case XML_REGEXP_SYMBOL_OTHERS:
2157 case XML_REGEXP_OTHER:
2158 case XML_REGEXP_OTHER_CONTROL:
2159 case XML_REGEXP_OTHER_FORMAT:
2160 case XML_REGEXP_OTHER_PRIVATE:
2161 case XML_REGEXP_OTHER_NA:
2162 case XML_REGEXP_BLOCK_NAME:
2163 ret = xmlRegCheckCharacterRange(atom->type, codepoint, 0, 0, 0,
2164 (const xmlChar *)atom->valuep);
2165 if (atom->neg)
2166 ret = !ret;
2167 break;
2168 }
2169 return(ret);
2170}
2171
2172/************************************************************************
2173 * *
William M. Brackddf71d62004-05-06 04:17:26 +00002174 * Saving and restoring state of an execution context *
Daniel Veillard4255d502002-04-16 15:50:10 +00002175 * *
2176 ************************************************************************/
2177
2178#ifdef DEBUG_REGEXP_EXEC
2179static void
2180xmlFARegDebugExec(xmlRegExecCtxtPtr exec) {
2181 printf("state: %d:%d:idx %d", exec->state->no, exec->transno, exec->index);
2182 if (exec->inputStack != NULL) {
2183 int i;
2184 printf(": ");
2185 for (i = 0;(i < 3) && (i < exec->inputStackNr);i++)
2186 printf("%s ", exec->inputStack[exec->inputStackNr - (i + 1)]);
2187 } else {
2188 printf(": %s", &(exec->inputString[exec->index]));
2189 }
2190 printf("\n");
2191}
2192#endif
2193
2194static void
2195xmlFARegExecSave(xmlRegExecCtxtPtr exec) {
2196#ifdef DEBUG_REGEXP_EXEC
2197 printf("saving ");
2198 exec->transno++;
2199 xmlFARegDebugExec(exec);
2200 exec->transno--;
2201#endif
2202
2203 if (exec->maxRollbacks == 0) {
2204 exec->maxRollbacks = 4;
2205 exec->rollbacks = (xmlRegExecRollback *) xmlMalloc(exec->maxRollbacks *
2206 sizeof(xmlRegExecRollback));
2207 if (exec->rollbacks == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00002208 xmlRegexpErrMemory(NULL, "saving regexp");
Daniel Veillard4255d502002-04-16 15:50:10 +00002209 exec->maxRollbacks = 0;
2210 return;
2211 }
2212 memset(exec->rollbacks, 0,
2213 exec->maxRollbacks * sizeof(xmlRegExecRollback));
2214 } else if (exec->nbRollbacks >= exec->maxRollbacks) {
2215 xmlRegExecRollback *tmp;
2216 int len = exec->maxRollbacks;
2217
2218 exec->maxRollbacks *= 2;
2219 tmp = (xmlRegExecRollback *) xmlRealloc(exec->rollbacks,
2220 exec->maxRollbacks * sizeof(xmlRegExecRollback));
2221 if (tmp == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00002222 xmlRegexpErrMemory(NULL, "saving regexp");
Daniel Veillard4255d502002-04-16 15:50:10 +00002223 exec->maxRollbacks /= 2;
2224 return;
2225 }
2226 exec->rollbacks = tmp;
2227 tmp = &exec->rollbacks[len];
2228 memset(tmp, 0, (exec->maxRollbacks - len) * sizeof(xmlRegExecRollback));
2229 }
2230 exec->rollbacks[exec->nbRollbacks].state = exec->state;
2231 exec->rollbacks[exec->nbRollbacks].index = exec->index;
2232 exec->rollbacks[exec->nbRollbacks].nextbranch = exec->transno + 1;
2233 if (exec->comp->nbCounters > 0) {
2234 if (exec->rollbacks[exec->nbRollbacks].counts == NULL) {
2235 exec->rollbacks[exec->nbRollbacks].counts = (int *)
2236 xmlMalloc(exec->comp->nbCounters * sizeof(int));
2237 if (exec->rollbacks[exec->nbRollbacks].counts == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00002238 xmlRegexpErrMemory(NULL, "saving regexp");
Daniel Veillard4255d502002-04-16 15:50:10 +00002239 exec->status = -5;
2240 return;
2241 }
2242 }
2243 memcpy(exec->rollbacks[exec->nbRollbacks].counts, exec->counts,
2244 exec->comp->nbCounters * sizeof(int));
2245 }
2246 exec->nbRollbacks++;
2247}
2248
2249static void
2250xmlFARegExecRollBack(xmlRegExecCtxtPtr exec) {
2251 if (exec->nbRollbacks <= 0) {
2252 exec->status = -1;
2253#ifdef DEBUG_REGEXP_EXEC
2254 printf("rollback failed on empty stack\n");
2255#endif
2256 return;
2257 }
2258 exec->nbRollbacks--;
2259 exec->state = exec->rollbacks[exec->nbRollbacks].state;
2260 exec->index = exec->rollbacks[exec->nbRollbacks].index;
2261 exec->transno = exec->rollbacks[exec->nbRollbacks].nextbranch;
2262 if (exec->comp->nbCounters > 0) {
2263 if (exec->rollbacks[exec->nbRollbacks].counts == NULL) {
2264 fprintf(stderr, "exec save: allocation failed");
2265 exec->status = -6;
2266 return;
2267 }
2268 memcpy(exec->counts, exec->rollbacks[exec->nbRollbacks].counts,
2269 exec->comp->nbCounters * sizeof(int));
2270 }
2271
2272#ifdef DEBUG_REGEXP_EXEC
2273 printf("restored ");
2274 xmlFARegDebugExec(exec);
2275#endif
2276}
2277
2278/************************************************************************
2279 * *
William M. Brackddf71d62004-05-06 04:17:26 +00002280 * Verifier, running an input against a compiled regexp *
Daniel Veillard4255d502002-04-16 15:50:10 +00002281 * *
2282 ************************************************************************/
2283
2284static int
2285xmlFARegExec(xmlRegexpPtr comp, const xmlChar *content) {
2286 xmlRegExecCtxt execval;
2287 xmlRegExecCtxtPtr exec = &execval;
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00002288 int ret, codepoint = 0, len;
Daniel Veillard4255d502002-04-16 15:50:10 +00002289
2290 exec->inputString = content;
2291 exec->index = 0;
2292 exec->determinist = 1;
2293 exec->maxRollbacks = 0;
2294 exec->nbRollbacks = 0;
2295 exec->rollbacks = NULL;
2296 exec->status = 0;
2297 exec->comp = comp;
2298 exec->state = comp->states[0];
2299 exec->transno = 0;
2300 exec->transcount = 0;
Daniel Veillardf2a12832003-11-24 13:04:35 +00002301 exec->inputStack = NULL;
2302 exec->inputStackMax = 0;
Daniel Veillard4255d502002-04-16 15:50:10 +00002303 if (comp->nbCounters > 0) {
2304 exec->counts = (int *) xmlMalloc(comp->nbCounters * sizeof(int));
Daniel Veillardff46a042003-10-08 08:53:17 +00002305 if (exec->counts == NULL) {
2306 xmlRegexpErrMemory(NULL, "running regexp");
Daniel Veillard4255d502002-04-16 15:50:10 +00002307 return(-1);
Daniel Veillardff46a042003-10-08 08:53:17 +00002308 }
Daniel Veillard4255d502002-04-16 15:50:10 +00002309 memset(exec->counts, 0, comp->nbCounters * sizeof(int));
2310 } else
2311 exec->counts = NULL;
2312 while ((exec->status == 0) &&
2313 ((exec->inputString[exec->index] != 0) ||
2314 (exec->state->type != XML_REGEXP_FINAL_STATE))) {
2315 xmlRegTransPtr trans;
2316 xmlRegAtomPtr atom;
2317
2318 /*
William M. Brack0e00b282004-04-26 15:40:47 +00002319 * If end of input on non-terminal state, rollback, however we may
Daniel Veillard4255d502002-04-16 15:50:10 +00002320 * still have epsilon like transition for counted transitions
William M. Brack0e00b282004-04-26 15:40:47 +00002321 * on counters, in that case don't break too early. Additionally,
2322 * if we are working on a range like "AB{0,2}", where B is not present,
2323 * we don't want to break.
Daniel Veillard4255d502002-04-16 15:50:10 +00002324 */
William M. Brack0e00b282004-04-26 15:40:47 +00002325 if ((exec->inputString[exec->index] == 0) && (exec->counts == NULL)) {
William M. Brackddf71d62004-05-06 04:17:26 +00002326 /*
2327 * if there is a transition, we must check if
2328 * atom allows minOccurs of 0
2329 */
2330 if (exec->transno < exec->state->nbTrans) {
William M. Brack0e00b282004-04-26 15:40:47 +00002331 trans = &exec->state->trans[exec->transno];
2332 if (trans->to >=0) {
2333 atom = trans->atom;
2334 if (!((atom->min == 0) && (atom->max > 0)))
2335 goto rollback;
2336 }
2337 } else
2338 goto rollback;
2339 }
Daniel Veillard4255d502002-04-16 15:50:10 +00002340
2341 exec->transcount = 0;
2342 for (;exec->transno < exec->state->nbTrans;exec->transno++) {
2343 trans = &exec->state->trans[exec->transno];
2344 if (trans->to < 0)
2345 continue;
2346 atom = trans->atom;
2347 ret = 0;
2348 if (trans->count >= 0) {
2349 int count;
2350 xmlRegCounterPtr counter;
2351
2352 /*
2353 * A counted transition.
2354 */
2355
2356 count = exec->counts[trans->count];
2357 counter = &exec->comp->counters[trans->count];
2358#ifdef DEBUG_REGEXP_EXEC
2359 printf("testing count %d: val %d, min %d, max %d\n",
2360 trans->count, count, counter->min, counter->max);
2361#endif
2362 ret = ((count >= counter->min) && (count <= counter->max));
2363 } else if (atom == NULL) {
2364 fprintf(stderr, "epsilon transition left at runtime\n");
2365 exec->status = -2;
2366 break;
2367 } else if (exec->inputString[exec->index] != 0) {
2368 codepoint = CUR_SCHAR(&(exec->inputString[exec->index]), len);
2369 ret = xmlRegCheckCharacter(atom, codepoint);
William M. Brack0e00b282004-04-26 15:40:47 +00002370 if ((ret == 1) && (atom->min >= 0) && (atom->max > 0)) {
Daniel Veillard4255d502002-04-16 15:50:10 +00002371 xmlRegStatePtr to = comp->states[trans->to];
2372
2373 /*
2374 * this is a multiple input sequence
2375 */
2376 if (exec->state->nbTrans > exec->transno + 1) {
2377 xmlFARegExecSave(exec);
2378 }
2379 exec->transcount = 1;
2380 do {
2381 /*
2382 * Try to progress as much as possible on the input
2383 */
2384 if (exec->transcount == atom->max) {
2385 break;
2386 }
2387 exec->index += len;
2388 /*
2389 * End of input: stop here
2390 */
2391 if (exec->inputString[exec->index] == 0) {
2392 exec->index -= len;
2393 break;
2394 }
2395 if (exec->transcount >= atom->min) {
2396 int transno = exec->transno;
2397 xmlRegStatePtr state = exec->state;
2398
2399 /*
2400 * The transition is acceptable save it
2401 */
2402 exec->transno = -1; /* trick */
2403 exec->state = to;
2404 xmlFARegExecSave(exec);
2405 exec->transno = transno;
2406 exec->state = state;
2407 }
2408 codepoint = CUR_SCHAR(&(exec->inputString[exec->index]),
2409 len);
2410 ret = xmlRegCheckCharacter(atom, codepoint);
2411 exec->transcount++;
2412 } while (ret == 1);
2413 if (exec->transcount < atom->min)
2414 ret = 0;
2415
2416 /*
2417 * If the last check failed but one transition was found
2418 * possible, rollback
2419 */
2420 if (ret < 0)
2421 ret = 0;
2422 if (ret == 0) {
2423 goto rollback;
2424 }
William M. Brack0e00b282004-04-26 15:40:47 +00002425 } else if ((ret == 0) && (atom->min == 0) && (atom->max > 0)) {
2426 /*
2427 * we don't match on the codepoint, but minOccurs of 0
2428 * says that's ok. Setting len to 0 inhibits stepping
2429 * over the codepoint.
2430 */
2431 exec->transcount = 1;
2432 len = 0;
2433 ret = 1;
Daniel Veillard4255d502002-04-16 15:50:10 +00002434 }
William M. Brack0e00b282004-04-26 15:40:47 +00002435 } else if ((atom->min == 0) && (atom->max > 0)) {
2436 /* another spot to match when minOccurs is 0 */
2437 exec->transcount = 1;
2438 len = 0;
2439 ret = 1;
Daniel Veillard4255d502002-04-16 15:50:10 +00002440 }
2441 if (ret == 1) {
2442 if (exec->state->nbTrans > exec->transno + 1) {
2443 xmlFARegExecSave(exec);
2444 }
2445 if (trans->counter >= 0) {
2446#ifdef DEBUG_REGEXP_EXEC
2447 printf("Increasing count %d\n", trans->counter);
2448#endif
2449 exec->counts[trans->counter]++;
2450 }
2451#ifdef DEBUG_REGEXP_EXEC
2452 printf("entering state %d\n", trans->to);
2453#endif
2454 exec->state = comp->states[trans->to];
2455 exec->transno = 0;
2456 if (trans->atom != NULL) {
2457 exec->index += len;
2458 }
2459 goto progress;
2460 } else if (ret < 0) {
2461 exec->status = -4;
2462 break;
2463 }
2464 }
2465 if ((exec->transno != 0) || (exec->state->nbTrans == 0)) {
2466rollback:
2467 /*
2468 * Failed to find a way out
2469 */
2470 exec->determinist = 0;
2471 xmlFARegExecRollBack(exec);
2472 }
2473progress:
2474 continue;
2475 }
2476 if (exec->rollbacks != NULL) {
2477 if (exec->counts != NULL) {
2478 int i;
2479
2480 for (i = 0;i < exec->maxRollbacks;i++)
2481 if (exec->rollbacks[i].counts != NULL)
2482 xmlFree(exec->rollbacks[i].counts);
2483 }
2484 xmlFree(exec->rollbacks);
2485 }
2486 if (exec->counts != NULL)
2487 xmlFree(exec->counts);
2488 if (exec->status == 0)
2489 return(1);
2490 if (exec->status == -1)
2491 return(0);
2492 return(exec->status);
2493}
2494
2495/************************************************************************
2496 * *
William M. Brackddf71d62004-05-06 04:17:26 +00002497 * Progressive interface to the verifier one atom at a time *
Daniel Veillard4255d502002-04-16 15:50:10 +00002498 * *
2499 ************************************************************************/
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00002500#ifdef DEBUG_ERR
2501static void testerr(xmlRegExecCtxtPtr exec);
2502#endif
Daniel Veillard4255d502002-04-16 15:50:10 +00002503
2504/**
Daniel Veillard01c13b52002-12-10 15:19:08 +00002505 * xmlRegNewExecCtxt:
Daniel Veillard4255d502002-04-16 15:50:10 +00002506 * @comp: a precompiled regular expression
2507 * @callback: a callback function used for handling progresses in the
2508 * automata matching phase
2509 * @data: the context data associated to the callback in this context
2510 *
2511 * Build a context used for progressive evaluation of a regexp.
Daniel Veillard01c13b52002-12-10 15:19:08 +00002512 *
2513 * Returns the new context
Daniel Veillard4255d502002-04-16 15:50:10 +00002514 */
2515xmlRegExecCtxtPtr
2516xmlRegNewExecCtxt(xmlRegexpPtr comp, xmlRegExecCallbacks callback, void *data) {
2517 xmlRegExecCtxtPtr exec;
2518
2519 if (comp == NULL)
2520 return(NULL);
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00002521 if ((comp->compact == NULL) && (comp->states == NULL))
2522 return(NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +00002523 exec = (xmlRegExecCtxtPtr) xmlMalloc(sizeof(xmlRegExecCtxt));
2524 if (exec == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00002525 xmlRegexpErrMemory(NULL, "creating execution context");
Daniel Veillard4255d502002-04-16 15:50:10 +00002526 return(NULL);
2527 }
2528 memset(exec, 0, sizeof(xmlRegExecCtxt));
2529 exec->inputString = NULL;
2530 exec->index = 0;
2531 exec->determinist = 1;
2532 exec->maxRollbacks = 0;
2533 exec->nbRollbacks = 0;
2534 exec->rollbacks = NULL;
2535 exec->status = 0;
2536 exec->comp = comp;
Daniel Veillard23e73572002-09-19 19:56:43 +00002537 if (comp->compact == NULL)
2538 exec->state = comp->states[0];
Daniel Veillard4255d502002-04-16 15:50:10 +00002539 exec->transno = 0;
2540 exec->transcount = 0;
2541 exec->callback = callback;
2542 exec->data = data;
2543 if (comp->nbCounters > 0) {
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00002544 /*
2545 * For error handling, exec->counts is allocated twice the size
2546 * the second half is used to store the data in case of rollback
2547 */
2548 exec->counts = (int *) xmlMalloc(comp->nbCounters * sizeof(int)
2549 * 2);
Daniel Veillard4255d502002-04-16 15:50:10 +00002550 if (exec->counts == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00002551 xmlRegexpErrMemory(NULL, "creating execution context");
Daniel Veillard4255d502002-04-16 15:50:10 +00002552 xmlFree(exec);
2553 return(NULL);
2554 }
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00002555 memset(exec->counts, 0, comp->nbCounters * sizeof(int) * 2);
2556 exec->errCounts = &exec->counts[comp->nbCounters];
2557 } else {
Daniel Veillard4255d502002-04-16 15:50:10 +00002558 exec->counts = NULL;
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00002559 exec->errCounts = NULL;
2560 }
Daniel Veillard4255d502002-04-16 15:50:10 +00002561 exec->inputStackMax = 0;
2562 exec->inputStackNr = 0;
2563 exec->inputStack = NULL;
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00002564 exec->errStateNo = -1;
2565 exec->errString = NULL;
Daniel Veillard4255d502002-04-16 15:50:10 +00002566 return(exec);
2567}
2568
2569/**
2570 * xmlRegFreeExecCtxt:
2571 * @exec: a regular expression evaulation context
2572 *
2573 * Free the structures associated to a regular expression evaulation context.
2574 */
2575void
2576xmlRegFreeExecCtxt(xmlRegExecCtxtPtr exec) {
2577 if (exec == NULL)
2578 return;
2579
2580 if (exec->rollbacks != NULL) {
2581 if (exec->counts != NULL) {
2582 int i;
2583
2584 for (i = 0;i < exec->maxRollbacks;i++)
2585 if (exec->rollbacks[i].counts != NULL)
2586 xmlFree(exec->rollbacks[i].counts);
2587 }
2588 xmlFree(exec->rollbacks);
2589 }
2590 if (exec->counts != NULL)
2591 xmlFree(exec->counts);
2592 if (exec->inputStack != NULL) {
2593 int i;
2594
Daniel Veillard32370232002-10-16 14:08:14 +00002595 for (i = 0;i < exec->inputStackNr;i++) {
2596 if (exec->inputStack[i].value != NULL)
2597 xmlFree(exec->inputStack[i].value);
2598 }
Daniel Veillard4255d502002-04-16 15:50:10 +00002599 xmlFree(exec->inputStack);
2600 }
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00002601 if (exec->errString != NULL)
2602 xmlFree(exec->errString);
Daniel Veillard4255d502002-04-16 15:50:10 +00002603 xmlFree(exec);
2604}
2605
2606static void
2607xmlFARegExecSaveInputString(xmlRegExecCtxtPtr exec, const xmlChar *value,
2608 void *data) {
2609#ifdef DEBUG_PUSH
2610 printf("saving value: %d:%s\n", exec->inputStackNr, value);
2611#endif
2612 if (exec->inputStackMax == 0) {
2613 exec->inputStackMax = 4;
2614 exec->inputStack = (xmlRegInputTokenPtr)
2615 xmlMalloc(exec->inputStackMax * sizeof(xmlRegInputToken));
2616 if (exec->inputStack == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00002617 xmlRegexpErrMemory(NULL, "pushing input string");
Daniel Veillard4255d502002-04-16 15:50:10 +00002618 exec->inputStackMax = 0;
2619 return;
2620 }
2621 } else if (exec->inputStackNr + 1 >= exec->inputStackMax) {
2622 xmlRegInputTokenPtr tmp;
2623
2624 exec->inputStackMax *= 2;
2625 tmp = (xmlRegInputTokenPtr) xmlRealloc(exec->inputStack,
2626 exec->inputStackMax * sizeof(xmlRegInputToken));
2627 if (tmp == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00002628 xmlRegexpErrMemory(NULL, "pushing input string");
Daniel Veillard4255d502002-04-16 15:50:10 +00002629 exec->inputStackMax /= 2;
2630 return;
2631 }
2632 exec->inputStack = tmp;
2633 }
2634 exec->inputStack[exec->inputStackNr].value = xmlStrdup(value);
2635 exec->inputStack[exec->inputStackNr].data = data;
2636 exec->inputStackNr++;
2637 exec->inputStack[exec->inputStackNr].value = NULL;
2638 exec->inputStack[exec->inputStackNr].data = NULL;
2639}
2640
Daniel Veillardc0826a72004-08-10 14:17:33 +00002641/**
2642 * xmlRegStrEqualWildcard:
2643 * @expStr: the string to be evaluated
2644 * @valStr: the validation string
2645 *
2646 * Checks if both strings are equal or have the same content. "*"
2647 * can be used as a wildcard in @valStr; "|" is used as a seperator of
2648 * substrings in both @expStr and @valStr.
2649 *
2650 * Returns 1 if the comparison is satisfied and the number of substrings
2651 * is equal, 0 otherwise.
2652 */
2653
2654static int
2655xmlRegStrEqualWildcard(const xmlChar *expStr, const xmlChar *valStr) {
2656 if (expStr == valStr) return(1);
2657 if (expStr == NULL) return(0);
2658 if (valStr == NULL) return(0);
2659 do {
2660 /*
2661 * Eval if we have a wildcard for the current item.
2662 */
2663 if (*expStr != *valStr) {
2664 if ((*valStr != 0) && (*expStr != 0) && (*expStr++ == '*')) {
2665 do {
2666 if (*valStr == XML_REG_STRING_SEPARATOR)
2667 break;
Kasimier T. Buchcikc0e833f2005-04-19 15:02:20 +00002668 valStr++;
Daniel Veillardc0826a72004-08-10 14:17:33 +00002669 } while (*valStr != 0);
2670 continue;
2671 } else
2672 return(0);
2673 }
Kasimier T. Buchcikc0e833f2005-04-19 15:02:20 +00002674 expStr++;
2675 valStr++;
Daniel Veillardc0826a72004-08-10 14:17:33 +00002676 } while (*valStr != 0);
2677 if (*expStr != 0)
2678 return (0);
2679 else
2680 return (1);
2681}
Daniel Veillard4255d502002-04-16 15:50:10 +00002682
2683/**
Daniel Veillard23e73572002-09-19 19:56:43 +00002684 * xmlRegCompactPushString:
2685 * @exec: a regexp execution context
2686 * @comp: the precompiled exec with a compact table
2687 * @value: a string token input
2688 * @data: data associated to the token to reuse in callbacks
2689 *
2690 * Push one input token in the execution context
2691 *
2692 * Returns: 1 if the regexp reached a final state, 0 if non-final, and
2693 * a negative value in case of error.
2694 */
2695static int
2696xmlRegCompactPushString(xmlRegExecCtxtPtr exec,
2697 xmlRegexpPtr comp,
2698 const xmlChar *value,
2699 void *data) {
2700 int state = exec->index;
2701 int i, target;
2702
2703 if ((comp == NULL) || (comp->compact == NULL) || (comp->stringMap == NULL))
2704 return(-1);
2705
2706 if (value == NULL) {
2707 /*
2708 * are we at a final state ?
2709 */
2710 if (comp->compact[state * (comp->nbstrings + 1)] ==
2711 XML_REGEXP_FINAL_STATE)
2712 return(1);
2713 return(0);
2714 }
2715
2716#ifdef DEBUG_PUSH
2717 printf("value pushed: %s\n", value);
2718#endif
2719
2720 /*
William M. Brackddf71d62004-05-06 04:17:26 +00002721 * Examine all outside transitions from current state
Daniel Veillard23e73572002-09-19 19:56:43 +00002722 */
2723 for (i = 0;i < comp->nbstrings;i++) {
2724 target = comp->compact[state * (comp->nbstrings + 1) + i + 1];
2725 if ((target > 0) && (target <= comp->nbstates)) {
Daniel Veillardc0826a72004-08-10 14:17:33 +00002726 target--; /* to avoid 0 */
2727 if (xmlRegStrEqualWildcard(comp->stringMap[i], value)) {
2728 exec->index = target;
Daniel Veillard118aed72002-09-24 14:13:13 +00002729 if ((exec->callback != NULL) && (comp->transdata != NULL)) {
2730 exec->callback(exec->data, value,
2731 comp->transdata[state * comp->nbstrings + i], data);
2732 }
Daniel Veillard23e73572002-09-19 19:56:43 +00002733#ifdef DEBUG_PUSH
2734 printf("entering state %d\n", target);
2735#endif
2736 if (comp->compact[target * (comp->nbstrings + 1)] ==
Daniel Veillardcc026dc2005-01-12 13:21:17 +00002737 XML_REGEXP_SINK_STATE)
2738 goto error;
2739
2740 if (comp->compact[target * (comp->nbstrings + 1)] ==
Daniel Veillard23e73572002-09-19 19:56:43 +00002741 XML_REGEXP_FINAL_STATE)
2742 return(1);
2743 return(0);
2744 }
2745 }
2746 }
2747 /*
2748 * Failed to find an exit transition out from current state for the
2749 * current token
2750 */
2751#ifdef DEBUG_PUSH
2752 printf("failed to find a transition for %s on state %d\n", value, state);
2753#endif
Daniel Veillardcc026dc2005-01-12 13:21:17 +00002754error:
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00002755 if (exec->errString != NULL)
2756 xmlFree(exec->errString);
2757 exec->errString = xmlStrdup(value);
2758 exec->errStateNo = state;
Daniel Veillard23e73572002-09-19 19:56:43 +00002759 exec->status = -1;
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00002760#ifdef DEBUG_ERR
2761 testerr(exec);
2762#endif
Daniel Veillard23e73572002-09-19 19:56:43 +00002763 return(-1);
2764}
2765
2766/**
Daniel Veillard4255d502002-04-16 15:50:10 +00002767 * xmlRegExecPushString:
Daniel Veillardea7751d2002-12-20 00:16:24 +00002768 * @exec: a regexp execution context or NULL to indicate the end
Daniel Veillard4255d502002-04-16 15:50:10 +00002769 * @value: a string token input
2770 * @data: data associated to the token to reuse in callbacks
2771 *
2772 * Push one input token in the execution context
2773 *
2774 * Returns: 1 if the regexp reached a final state, 0 if non-final, and
2775 * a negative value in case of error.
2776 */
2777int
2778xmlRegExecPushString(xmlRegExecCtxtPtr exec, const xmlChar *value,
2779 void *data) {
2780 xmlRegTransPtr trans;
2781 xmlRegAtomPtr atom;
2782 int ret;
2783 int final = 0;
Daniel Veillard90700152005-01-08 22:05:09 +00002784 int progress = 1;
Daniel Veillard4255d502002-04-16 15:50:10 +00002785
2786 if (exec == NULL)
2787 return(-1);
Daniel Veillard23e73572002-09-19 19:56:43 +00002788 if (exec->comp == NULL)
2789 return(-1);
Daniel Veillard4255d502002-04-16 15:50:10 +00002790 if (exec->status != 0)
2791 return(exec->status);
2792
Daniel Veillard23e73572002-09-19 19:56:43 +00002793 if (exec->comp->compact != NULL)
2794 return(xmlRegCompactPushString(exec, exec->comp, value, data));
2795
Daniel Veillard4255d502002-04-16 15:50:10 +00002796 if (value == NULL) {
2797 if (exec->state->type == XML_REGEXP_FINAL_STATE)
2798 return(1);
2799 final = 1;
2800 }
2801
2802#ifdef DEBUG_PUSH
2803 printf("value pushed: %s\n", value);
2804#endif
2805 /*
2806 * If we have an active rollback stack push the new value there
2807 * and get back to where we were left
2808 */
2809 if ((value != NULL) && (exec->inputStackNr > 0)) {
2810 xmlFARegExecSaveInputString(exec, value, data);
2811 value = exec->inputStack[exec->index].value;
2812 data = exec->inputStack[exec->index].data;
2813#ifdef DEBUG_PUSH
2814 printf("value loaded: %s\n", value);
2815#endif
2816 }
2817
2818 while ((exec->status == 0) &&
2819 ((value != NULL) ||
2820 ((final == 1) &&
2821 (exec->state->type != XML_REGEXP_FINAL_STATE)))) {
2822
2823 /*
2824 * End of input on non-terminal state, rollback, however we may
2825 * still have epsilon like transition for counted transitions
2826 * on counters, in that case don't break too early.
2827 */
Daniel Veillardb509f152002-04-17 16:28:10 +00002828 if ((value == NULL) && (exec->counts == NULL))
Daniel Veillard4255d502002-04-16 15:50:10 +00002829 goto rollback;
2830
2831 exec->transcount = 0;
2832 for (;exec->transno < exec->state->nbTrans;exec->transno++) {
2833 trans = &exec->state->trans[exec->transno];
2834 if (trans->to < 0)
2835 continue;
2836 atom = trans->atom;
2837 ret = 0;
Daniel Veillard441bc322002-04-20 17:38:48 +00002838 if (trans->count == REGEXP_ALL_LAX_COUNTER) {
2839 int i;
2840 int count;
2841 xmlRegTransPtr t;
2842 xmlRegCounterPtr counter;
2843
2844 ret = 0;
2845
2846#ifdef DEBUG_PUSH
2847 printf("testing all lax %d\n", trans->count);
2848#endif
2849 /*
2850 * Check all counted transitions from the current state
2851 */
2852 if ((value == NULL) && (final)) {
2853 ret = 1;
2854 } else if (value != NULL) {
2855 for (i = 0;i < exec->state->nbTrans;i++) {
2856 t = &exec->state->trans[i];
2857 if ((t->counter < 0) || (t == trans))
2858 continue;
2859 counter = &exec->comp->counters[t->counter];
2860 count = exec->counts[t->counter];
2861 if ((count < counter->max) &&
2862 (t->atom != NULL) &&
2863 (xmlStrEqual(value, t->atom->valuep))) {
2864 ret = 0;
2865 break;
2866 }
2867 if ((count >= counter->min) &&
2868 (count < counter->max) &&
2869 (xmlStrEqual(value, t->atom->valuep))) {
2870 ret = 1;
2871 break;
2872 }
2873 }
2874 }
2875 } else if (trans->count == REGEXP_ALL_COUNTER) {
Daniel Veillard8a001f62002-04-20 07:24:11 +00002876 int i;
2877 int count;
2878 xmlRegTransPtr t;
2879 xmlRegCounterPtr counter;
2880
2881 ret = 1;
2882
2883#ifdef DEBUG_PUSH
2884 printf("testing all %d\n", trans->count);
2885#endif
2886 /*
2887 * Check all counted transitions from the current state
2888 */
2889 for (i = 0;i < exec->state->nbTrans;i++) {
2890 t = &exec->state->trans[i];
2891 if ((t->counter < 0) || (t == trans))
2892 continue;
2893 counter = &exec->comp->counters[t->counter];
2894 count = exec->counts[t->counter];
2895 if ((count < counter->min) || (count > counter->max)) {
2896 ret = 0;
2897 break;
2898 }
2899 }
2900 } else if (trans->count >= 0) {
Daniel Veillard4255d502002-04-16 15:50:10 +00002901 int count;
2902 xmlRegCounterPtr counter;
2903
2904 /*
2905 * A counted transition.
2906 */
2907
2908 count = exec->counts[trans->count];
2909 counter = &exec->comp->counters[trans->count];
2910#ifdef DEBUG_PUSH
2911 printf("testing count %d: val %d, min %d, max %d\n",
2912 trans->count, count, counter->min, counter->max);
2913#endif
2914 ret = ((count >= counter->min) && (count <= counter->max));
2915 } else if (atom == NULL) {
2916 fprintf(stderr, "epsilon transition left at runtime\n");
2917 exec->status = -2;
2918 break;
2919 } else if (value != NULL) {
Daniel Veillardc0826a72004-08-10 14:17:33 +00002920 ret = xmlRegStrEqualWildcard(atom->valuep, value);
Daniel Veillard9efc4762005-07-19 14:33:55 +00002921 if (atom->neg)
2922 ret = !ret;
Daniel Veillard441bc322002-04-20 17:38:48 +00002923 if ((ret == 1) && (trans->counter >= 0)) {
2924 xmlRegCounterPtr counter;
2925 int count;
2926
2927 count = exec->counts[trans->counter];
2928 counter = &exec->comp->counters[trans->counter];
2929 if (count >= counter->max)
2930 ret = 0;
2931 }
2932
Daniel Veillard4255d502002-04-16 15:50:10 +00002933 if ((ret == 1) && (atom->min > 0) && (atom->max > 0)) {
2934 xmlRegStatePtr to = exec->comp->states[trans->to];
2935
2936 /*
2937 * this is a multiple input sequence
2938 */
2939 if (exec->state->nbTrans > exec->transno + 1) {
2940 if (exec->inputStackNr <= 0) {
2941 xmlFARegExecSaveInputString(exec, value, data);
2942 }
2943 xmlFARegExecSave(exec);
2944 }
2945 exec->transcount = 1;
2946 do {
2947 /*
2948 * Try to progress as much as possible on the input
2949 */
2950 if (exec->transcount == atom->max) {
2951 break;
2952 }
2953 exec->index++;
2954 value = exec->inputStack[exec->index].value;
2955 data = exec->inputStack[exec->index].data;
2956#ifdef DEBUG_PUSH
2957 printf("value loaded: %s\n", value);
2958#endif
2959
2960 /*
2961 * End of input: stop here
2962 */
2963 if (value == NULL) {
2964 exec->index --;
2965 break;
2966 }
2967 if (exec->transcount >= atom->min) {
2968 int transno = exec->transno;
2969 xmlRegStatePtr state = exec->state;
2970
2971 /*
2972 * The transition is acceptable save it
2973 */
2974 exec->transno = -1; /* trick */
2975 exec->state = to;
2976 if (exec->inputStackNr <= 0) {
2977 xmlFARegExecSaveInputString(exec, value, data);
2978 }
2979 xmlFARegExecSave(exec);
2980 exec->transno = transno;
2981 exec->state = state;
2982 }
2983 ret = xmlStrEqual(value, atom->valuep);
2984 exec->transcount++;
2985 } while (ret == 1);
2986 if (exec->transcount < atom->min)
2987 ret = 0;
2988
2989 /*
2990 * If the last check failed but one transition was found
2991 * possible, rollback
2992 */
2993 if (ret < 0)
2994 ret = 0;
2995 if (ret == 0) {
2996 goto rollback;
2997 }
2998 }
2999 }
3000 if (ret == 1) {
William M. Brack98873952003-12-26 06:03:14 +00003001 if ((exec->callback != NULL) && (atom != NULL) &&
3002 (data != NULL)) {
Daniel Veillard4255d502002-04-16 15:50:10 +00003003 exec->callback(exec->data, atom->valuep,
3004 atom->data, data);
3005 }
3006 if (exec->state->nbTrans > exec->transno + 1) {
3007 if (exec->inputStackNr <= 0) {
3008 xmlFARegExecSaveInputString(exec, value, data);
3009 }
3010 xmlFARegExecSave(exec);
3011 }
3012 if (trans->counter >= 0) {
3013#ifdef DEBUG_PUSH
3014 printf("Increasing count %d\n", trans->counter);
3015#endif
3016 exec->counts[trans->counter]++;
3017 }
3018#ifdef DEBUG_PUSH
3019 printf("entering state %d\n", trans->to);
3020#endif
Daniel Veillardcc026dc2005-01-12 13:21:17 +00003021 if ((exec->comp->states[trans->to] != NULL) &&
3022 (exec->comp->states[trans->to]->type ==
3023 XML_REGEXP_SINK_STATE)) {
3024 /*
3025 * entering a sink state, save the current state as error
3026 * state.
3027 */
3028 if (exec->errString != NULL)
3029 xmlFree(exec->errString);
3030 exec->errString = xmlStrdup(value);
3031 exec->errState = exec->state;
3032 memcpy(exec->errCounts, exec->counts,
3033 exec->comp->nbCounters * sizeof(int));
3034 }
Daniel Veillard4255d502002-04-16 15:50:10 +00003035 exec->state = exec->comp->states[trans->to];
3036 exec->transno = 0;
3037 if (trans->atom != NULL) {
3038 if (exec->inputStack != NULL) {
3039 exec->index++;
3040 if (exec->index < exec->inputStackNr) {
3041 value = exec->inputStack[exec->index].value;
3042 data = exec->inputStack[exec->index].data;
3043#ifdef DEBUG_PUSH
3044 printf("value loaded: %s\n", value);
3045#endif
3046 } else {
3047 value = NULL;
3048 data = NULL;
3049#ifdef DEBUG_PUSH
3050 printf("end of input\n");
3051#endif
3052 }
3053 } else {
3054 value = NULL;
3055 data = NULL;
3056#ifdef DEBUG_PUSH
3057 printf("end of input\n");
3058#endif
3059 }
3060 }
3061 goto progress;
3062 } else if (ret < 0) {
3063 exec->status = -4;
3064 break;
3065 }
3066 }
3067 if ((exec->transno != 0) || (exec->state->nbTrans == 0)) {
3068rollback:
Daniel Veillard90700152005-01-08 22:05:09 +00003069 /*
Daniel Veillardcc026dc2005-01-12 13:21:17 +00003070 * if we didn't yet rollback on the current input
3071 * store the current state as the error state.
Daniel Veillard90700152005-01-08 22:05:09 +00003072 */
Daniel Veillardcc026dc2005-01-12 13:21:17 +00003073 if ((progress) && (exec->state != NULL) &&
3074 (exec->state->type != XML_REGEXP_SINK_STATE)) {
Daniel Veillard90700152005-01-08 22:05:09 +00003075 progress = 0;
3076 if (exec->errString != NULL)
3077 xmlFree(exec->errString);
3078 exec->errString = xmlStrdup(value);
3079 exec->errState = exec->state;
3080 memcpy(exec->errCounts, exec->counts,
3081 exec->comp->nbCounters * sizeof(int));
3082 }
3083
Daniel Veillard4255d502002-04-16 15:50:10 +00003084 /*
3085 * Failed to find a way out
3086 */
3087 exec->determinist = 0;
3088 xmlFARegExecRollBack(exec);
3089 if (exec->status == 0) {
3090 value = exec->inputStack[exec->index].value;
3091 data = exec->inputStack[exec->index].data;
3092#ifdef DEBUG_PUSH
3093 printf("value loaded: %s\n", value);
3094#endif
3095 }
3096 }
Daniel Veillard90700152005-01-08 22:05:09 +00003097 continue;
Daniel Veillard4255d502002-04-16 15:50:10 +00003098progress:
Daniel Veillard90700152005-01-08 22:05:09 +00003099 progress = 1;
Daniel Veillard4255d502002-04-16 15:50:10 +00003100 continue;
3101 }
3102 if (exec->status == 0) {
3103 return(exec->state->type == XML_REGEXP_FINAL_STATE);
3104 }
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003105#ifdef DEBUG_ERR
Daniel Veillard90700152005-01-08 22:05:09 +00003106 if (exec->status < 0) {
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003107 testerr(exec);
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003108 }
Daniel Veillard90700152005-01-08 22:05:09 +00003109#endif
Daniel Veillard4255d502002-04-16 15:50:10 +00003110 return(exec->status);
3111}
3112
Daniel Veillard52b48c72003-04-13 19:53:42 +00003113/**
3114 * xmlRegExecPushString2:
3115 * @exec: a regexp execution context or NULL to indicate the end
3116 * @value: the first string token input
3117 * @value2: the second string token input
3118 * @data: data associated to the token to reuse in callbacks
3119 *
3120 * Push one input token in the execution context
3121 *
3122 * Returns: 1 if the regexp reached a final state, 0 if non-final, and
3123 * a negative value in case of error.
3124 */
3125int
3126xmlRegExecPushString2(xmlRegExecCtxtPtr exec, const xmlChar *value,
3127 const xmlChar *value2, void *data) {
3128 xmlChar buf[150];
3129 int lenn, lenp, ret;
3130 xmlChar *str;
3131
3132 if (exec == NULL)
3133 return(-1);
3134 if (exec->comp == NULL)
3135 return(-1);
3136 if (exec->status != 0)
3137 return(exec->status);
3138
3139 if (value2 == NULL)
3140 return(xmlRegExecPushString(exec, value, data));
3141
3142 lenn = strlen((char *) value2);
3143 lenp = strlen((char *) value);
3144
3145 if (150 < lenn + lenp + 2) {
Daniel Veillard3c908dc2003-04-19 00:07:51 +00003146 str = (xmlChar *) xmlMallocAtomic(lenn + lenp + 2);
Daniel Veillard52b48c72003-04-13 19:53:42 +00003147 if (str == NULL) {
3148 exec->status = -1;
3149 return(-1);
3150 }
3151 } else {
3152 str = buf;
3153 }
3154 memcpy(&str[0], value, lenp);
Daniel Veillardc0826a72004-08-10 14:17:33 +00003155 str[lenp] = XML_REG_STRING_SEPARATOR;
Daniel Veillard52b48c72003-04-13 19:53:42 +00003156 memcpy(&str[lenp + 1], value2, lenn);
3157 str[lenn + lenp + 1] = 0;
3158
3159 if (exec->comp->compact != NULL)
3160 ret = xmlRegCompactPushString(exec, exec->comp, str, data);
3161 else
3162 ret = xmlRegExecPushString(exec, str, data);
3163
3164 if (str != buf)
3165 xmlFree(buf);
3166 return(ret);
3167}
3168
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003169/**
Daniel Veillard77005e62005-07-19 16:26:18 +00003170 * xmlRegExecGetValues:
Daniel Veillardfc0b6f62005-01-09 17:48:02 +00003171 * @exec: a regexp execution context
3172 * @err: error extraction or normal one
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003173 * @nbval: pointer to the number of accepted values IN/OUT
Daniel Veillardcc026dc2005-01-12 13:21:17 +00003174 * @nbneg: return number of negative transitions
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003175 * @values: pointer to the array of acceptable values
Daniel Veillardfc0b6f62005-01-09 17:48:02 +00003176 * @terminal: return value if this was a terminal state
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003177 *
Daniel Veillardfc0b6f62005-01-09 17:48:02 +00003178 * Extract informations from the regexp execution, internal routine to
3179 * implement xmlRegExecNextValues() and xmlRegExecErrInfo()
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003180 *
3181 * Returns: 0 in case of success or -1 in case of error.
3182 */
Daniel Veillardfc0b6f62005-01-09 17:48:02 +00003183static int
3184xmlRegExecGetValues(xmlRegExecCtxtPtr exec, int err,
Daniel Veillardcc026dc2005-01-12 13:21:17 +00003185 int *nbval, int *nbneg,
3186 xmlChar **values, int *terminal) {
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003187 int maxval;
Daniel Veillardcc026dc2005-01-12 13:21:17 +00003188 int nb = 0;
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003189
Daniel Veillardcc026dc2005-01-12 13:21:17 +00003190 if ((exec == NULL) || (nbval == NULL) || (nbneg == NULL) ||
3191 (values == NULL) || (*nbval <= 0))
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003192 return(-1);
Daniel Veillardfc0b6f62005-01-09 17:48:02 +00003193
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003194 maxval = *nbval;
3195 *nbval = 0;
Daniel Veillardcc026dc2005-01-12 13:21:17 +00003196 *nbneg = 0;
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003197 if ((exec->comp != NULL) && (exec->comp->compact != NULL)) {
3198 xmlRegexpPtr comp;
3199 int target, i, state;
3200
3201 comp = exec->comp;
Daniel Veillardfc0b6f62005-01-09 17:48:02 +00003202
3203 if (err) {
3204 if (exec->errStateNo == -1) return(-1);
3205 state = exec->errStateNo;
3206 } else {
3207 state = exec->index;
3208 }
3209 if (terminal != NULL) {
3210 if (comp->compact[state * (comp->nbstrings + 1)] ==
3211 XML_REGEXP_FINAL_STATE)
3212 *terminal = 1;
3213 else
3214 *terminal = 0;
3215 }
Daniel Veillardcc026dc2005-01-12 13:21:17 +00003216 for (i = 0;(i < comp->nbstrings) && (nb < maxval);i++) {
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003217 target = comp->compact[state * (comp->nbstrings + 1) + i + 1];
Daniel Veillardcc026dc2005-01-12 13:21:17 +00003218 if ((target > 0) && (target <= comp->nbstates) &&
3219 (comp->compact[(target - 1) * (comp->nbstrings + 1)] !=
3220 XML_REGEXP_SINK_STATE)) {
3221 values[nb++] = comp->stringMap[i];
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003222 (*nbval)++;
3223 }
3224 }
Daniel Veillardcc026dc2005-01-12 13:21:17 +00003225 for (i = 0;(i < comp->nbstrings) && (nb < maxval);i++) {
3226 target = comp->compact[state * (comp->nbstrings + 1) + i + 1];
3227 if ((target > 0) && (target <= comp->nbstates) &&
3228 (comp->compact[(target - 1) * (comp->nbstrings + 1)] ==
3229 XML_REGEXP_SINK_STATE)) {
3230 values[nb++] = comp->stringMap[i];
3231 (*nbneg)++;
3232 }
3233 }
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003234 } else {
3235 int transno;
3236 xmlRegTransPtr trans;
3237 xmlRegAtomPtr atom;
Daniel Veillardfc0b6f62005-01-09 17:48:02 +00003238 xmlRegStatePtr state;
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003239
Daniel Veillardfc0b6f62005-01-09 17:48:02 +00003240 if (terminal != NULL) {
3241 if (exec->state->type == XML_REGEXP_FINAL_STATE)
3242 *terminal = 1;
3243 else
3244 *terminal = 0;
3245 }
3246
3247 if (err) {
3248 if (exec->errState == NULL) return(-1);
3249 state = exec->errState;
3250 } else {
3251 if (exec->state == NULL) return(-1);
3252 state = exec->state;
3253 }
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003254 for (transno = 0;
Daniel Veillardcc026dc2005-01-12 13:21:17 +00003255 (transno < state->nbTrans) && (nb < maxval);
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003256 transno++) {
Daniel Veillardfc0b6f62005-01-09 17:48:02 +00003257 trans = &state->trans[transno];
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003258 if (trans->to < 0)
3259 continue;
3260 atom = trans->atom;
3261 if ((atom == NULL) || (atom->valuep == NULL))
3262 continue;
3263 if (trans->count == REGEXP_ALL_LAX_COUNTER) {
Daniel Veillardcc026dc2005-01-12 13:21:17 +00003264 /* this should not be reached but ... */
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003265 TODO;
3266 } else if (trans->count == REGEXP_ALL_COUNTER) {
Daniel Veillardcc026dc2005-01-12 13:21:17 +00003267 /* this should not be reached but ... */
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003268 TODO;
3269 } else if (trans->counter >= 0) {
3270 xmlRegCounterPtr counter;
3271 int count;
3272
Daniel Veillardfc0b6f62005-01-09 17:48:02 +00003273 if (err)
3274 count = exec->errCounts[trans->counter];
3275 else
3276 count = exec->counts[trans->counter];
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003277 counter = &exec->comp->counters[trans->counter];
3278 if (count < counter->max) {
Daniel Veillard77005e62005-07-19 16:26:18 +00003279 if (atom->neg)
3280 values[nb++] = (xmlChar *) atom->valuep2;
3281 else
3282 values[nb++] = (xmlChar *) atom->valuep;
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003283 (*nbval)++;
3284 }
3285 } else {
Daniel Veillardcc026dc2005-01-12 13:21:17 +00003286 if ((exec->comp->states[trans->to] != NULL) &&
3287 (exec->comp->states[trans->to]->type !=
3288 XML_REGEXP_SINK_STATE)) {
Daniel Veillard77005e62005-07-19 16:26:18 +00003289 if (atom->neg)
3290 values[nb++] = (xmlChar *) atom->valuep2;
3291 else
3292 values[nb++] = (xmlChar *) atom->valuep;
Daniel Veillardcc026dc2005-01-12 13:21:17 +00003293 (*nbval)++;
3294 }
3295 }
3296 }
3297 for (transno = 0;
3298 (transno < state->nbTrans) && (nb < maxval);
3299 transno++) {
3300 trans = &state->trans[transno];
3301 if (trans->to < 0)
3302 continue;
3303 atom = trans->atom;
3304 if ((atom == NULL) || (atom->valuep == NULL))
3305 continue;
3306 if (trans->count == REGEXP_ALL_LAX_COUNTER) {
3307 continue;
3308 } else if (trans->count == REGEXP_ALL_COUNTER) {
3309 continue;
3310 } else if (trans->counter >= 0) {
3311 continue;
3312 } else {
3313 if ((exec->comp->states[trans->to] != NULL) &&
3314 (exec->comp->states[trans->to]->type ==
3315 XML_REGEXP_SINK_STATE)) {
Daniel Veillard77005e62005-07-19 16:26:18 +00003316 if (atom->neg)
3317 values[nb++] = (xmlChar *) atom->valuep2;
3318 else
3319 values[nb++] = (xmlChar *) atom->valuep;
Daniel Veillardcc026dc2005-01-12 13:21:17 +00003320 (*nbneg)++;
3321 }
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003322 }
3323 }
3324 }
3325 return(0);
3326}
3327
Daniel Veillardfc0b6f62005-01-09 17:48:02 +00003328/**
3329 * xmlRegExecNextValues:
3330 * @exec: a regexp execution context
3331 * @nbval: pointer to the number of accepted values IN/OUT
Daniel Veillardcc026dc2005-01-12 13:21:17 +00003332 * @nbneg: return number of negative transitions
Daniel Veillardfc0b6f62005-01-09 17:48:02 +00003333 * @values: pointer to the array of acceptable values
3334 * @terminal: return value if this was a terminal state
3335 *
3336 * Extract informations from the regexp execution,
3337 * the parameter @values must point to an array of @nbval string pointers
3338 * on return nbval will contain the number of possible strings in that
3339 * state and the @values array will be updated with them. The string values
3340 * returned will be freed with the @exec context and don't need to be
3341 * deallocated.
3342 *
3343 * Returns: 0 in case of success or -1 in case of error.
3344 */
3345int
Daniel Veillardcc026dc2005-01-12 13:21:17 +00003346xmlRegExecNextValues(xmlRegExecCtxtPtr exec, int *nbval, int *nbneg,
3347 xmlChar **values, int *terminal) {
3348 return(xmlRegExecGetValues(exec, 0, nbval, nbneg, values, terminal));
Daniel Veillardfc0b6f62005-01-09 17:48:02 +00003349}
3350
3351/**
3352 * xmlRegExecErrInfo:
3353 * @exec: a regexp execution context generating an error
3354 * @string: return value for the error string
3355 * @nbval: pointer to the number of accepted values IN/OUT
Daniel Veillardcc026dc2005-01-12 13:21:17 +00003356 * @nbneg: return number of negative transitions
Daniel Veillardfc0b6f62005-01-09 17:48:02 +00003357 * @values: pointer to the array of acceptable values
3358 * @terminal: return value if this was a terminal state
3359 *
3360 * Extract error informations from the regexp execution, the parameter
3361 * @string will be updated with the value pushed and not accepted,
3362 * the parameter @values must point to an array of @nbval string pointers
3363 * on return nbval will contain the number of possible strings in that
3364 * state and the @values array will be updated with them. The string values
3365 * returned will be freed with the @exec context and don't need to be
3366 * deallocated.
3367 *
3368 * Returns: 0 in case of success or -1 in case of error.
3369 */
3370int
3371xmlRegExecErrInfo(xmlRegExecCtxtPtr exec, const xmlChar **string,
Daniel Veillardcc026dc2005-01-12 13:21:17 +00003372 int *nbval, int *nbneg, xmlChar **values, int *terminal) {
Daniel Veillardfc0b6f62005-01-09 17:48:02 +00003373 if (exec == NULL)
3374 return(-1);
3375 if (string != NULL) {
3376 if (exec->status != 0)
3377 *string = exec->errString;
3378 else
3379 *string = NULL;
3380 }
Daniel Veillardcc026dc2005-01-12 13:21:17 +00003381 return(xmlRegExecGetValues(exec, 1, nbval, nbneg, values, terminal));
Daniel Veillardfc0b6f62005-01-09 17:48:02 +00003382}
3383
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003384#ifdef DEBUG_ERR
3385static void testerr(xmlRegExecCtxtPtr exec) {
3386 const xmlChar *string;
Daniel Veillardcee2b3a2005-01-25 00:22:52 +00003387 xmlChar *values[5];
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003388 int nb = 5;
Daniel Veillardcc026dc2005-01-12 13:21:17 +00003389 int nbneg;
Daniel Veillardfc0b6f62005-01-09 17:48:02 +00003390 int terminal;
Daniel Veillardcc026dc2005-01-12 13:21:17 +00003391 xmlRegExecErrInfo(exec, &string, &nb, &nbneg, &values[0], &terminal);
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003392}
3393#endif
3394
Daniel Veillard4255d502002-04-16 15:50:10 +00003395#if 0
3396static int
3397xmlRegExecPushChar(xmlRegExecCtxtPtr exec, int UCS) {
3398 xmlRegTransPtr trans;
3399 xmlRegAtomPtr atom;
3400 int ret;
3401 int codepoint, len;
3402
3403 if (exec == NULL)
3404 return(-1);
3405 if (exec->status != 0)
3406 return(exec->status);
3407
3408 while ((exec->status == 0) &&
3409 ((exec->inputString[exec->index] != 0) ||
3410 (exec->state->type != XML_REGEXP_FINAL_STATE))) {
3411
3412 /*
3413 * End of input on non-terminal state, rollback, however we may
3414 * still have epsilon like transition for counted transitions
3415 * on counters, in that case don't break too early.
3416 */
3417 if ((exec->inputString[exec->index] == 0) && (exec->counts == NULL))
3418 goto rollback;
3419
3420 exec->transcount = 0;
3421 for (;exec->transno < exec->state->nbTrans;exec->transno++) {
3422 trans = &exec->state->trans[exec->transno];
3423 if (trans->to < 0)
3424 continue;
3425 atom = trans->atom;
3426 ret = 0;
3427 if (trans->count >= 0) {
3428 int count;
3429 xmlRegCounterPtr counter;
3430
3431 /*
3432 * A counted transition.
3433 */
3434
3435 count = exec->counts[trans->count];
3436 counter = &exec->comp->counters[trans->count];
3437#ifdef DEBUG_REGEXP_EXEC
3438 printf("testing count %d: val %d, min %d, max %d\n",
3439 trans->count, count, counter->min, counter->max);
3440#endif
3441 ret = ((count >= counter->min) && (count <= counter->max));
3442 } else if (atom == NULL) {
3443 fprintf(stderr, "epsilon transition left at runtime\n");
3444 exec->status = -2;
3445 break;
3446 } else if (exec->inputString[exec->index] != 0) {
3447 codepoint = CUR_SCHAR(&(exec->inputString[exec->index]), len);
3448 ret = xmlRegCheckCharacter(atom, codepoint);
3449 if ((ret == 1) && (atom->min > 0) && (atom->max > 0)) {
3450 xmlRegStatePtr to = exec->comp->states[trans->to];
3451
3452 /*
3453 * this is a multiple input sequence
3454 */
3455 if (exec->state->nbTrans > exec->transno + 1) {
3456 xmlFARegExecSave(exec);
3457 }
3458 exec->transcount = 1;
3459 do {
3460 /*
3461 * Try to progress as much as possible on the input
3462 */
3463 if (exec->transcount == atom->max) {
3464 break;
3465 }
3466 exec->index += len;
3467 /*
3468 * End of input: stop here
3469 */
3470 if (exec->inputString[exec->index] == 0) {
3471 exec->index -= len;
3472 break;
3473 }
3474 if (exec->transcount >= atom->min) {
3475 int transno = exec->transno;
3476 xmlRegStatePtr state = exec->state;
3477
3478 /*
3479 * The transition is acceptable save it
3480 */
3481 exec->transno = -1; /* trick */
3482 exec->state = to;
3483 xmlFARegExecSave(exec);
3484 exec->transno = transno;
3485 exec->state = state;
3486 }
3487 codepoint = CUR_SCHAR(&(exec->inputString[exec->index]),
3488 len);
3489 ret = xmlRegCheckCharacter(atom, codepoint);
3490 exec->transcount++;
3491 } while (ret == 1);
3492 if (exec->transcount < atom->min)
3493 ret = 0;
3494
3495 /*
3496 * If the last check failed but one transition was found
3497 * possible, rollback
3498 */
3499 if (ret < 0)
3500 ret = 0;
3501 if (ret == 0) {
3502 goto rollback;
3503 }
3504 }
3505 }
3506 if (ret == 1) {
3507 if (exec->state->nbTrans > exec->transno + 1) {
3508 xmlFARegExecSave(exec);
3509 }
3510 if (trans->counter >= 0) {
3511#ifdef DEBUG_REGEXP_EXEC
3512 printf("Increasing count %d\n", trans->counter);
3513#endif
3514 exec->counts[trans->counter]++;
3515 }
3516#ifdef DEBUG_REGEXP_EXEC
3517 printf("entering state %d\n", trans->to);
3518#endif
3519 exec->state = exec->comp->states[trans->to];
3520 exec->transno = 0;
3521 if (trans->atom != NULL) {
3522 exec->index += len;
3523 }
3524 goto progress;
3525 } else if (ret < 0) {
3526 exec->status = -4;
3527 break;
3528 }
3529 }
3530 if ((exec->transno != 0) || (exec->state->nbTrans == 0)) {
3531rollback:
3532 /*
3533 * Failed to find a way out
3534 */
3535 exec->determinist = 0;
3536 xmlFARegExecRollBack(exec);
3537 }
3538progress:
3539 continue;
3540 }
3541}
3542#endif
3543/************************************************************************
3544 * *
William M. Brackddf71d62004-05-06 04:17:26 +00003545 * Parser for the Schemas Datatype Regular Expressions *
Daniel Veillard4255d502002-04-16 15:50:10 +00003546 * http://www.w3.org/TR/2001/REC-xmlschema-2-20010502/#regexs *
3547 * *
3548 ************************************************************************/
3549
3550/**
3551 * xmlFAIsChar:
Daniel Veillard441bc322002-04-20 17:38:48 +00003552 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00003553 *
3554 * [10] Char ::= [^.\?*+()|#x5B#x5D]
3555 */
3556static int
3557xmlFAIsChar(xmlRegParserCtxtPtr ctxt) {
3558 int cur;
3559 int len;
3560
3561 cur = CUR_SCHAR(ctxt->cur, len);
3562 if ((cur == '.') || (cur == '\\') || (cur == '?') ||
3563 (cur == '*') || (cur == '+') || (cur == '(') ||
3564 (cur == ')') || (cur == '|') || (cur == 0x5B) ||
3565 (cur == 0x5D) || (cur == 0))
3566 return(-1);
3567 return(cur);
3568}
3569
3570/**
3571 * xmlFAParseCharProp:
Daniel Veillard441bc322002-04-20 17:38:48 +00003572 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00003573 *
3574 * [27] charProp ::= IsCategory | IsBlock
3575 * [28] IsCategory ::= Letters | Marks | Numbers | Punctuation |
3576 * Separators | Symbols | Others
3577 * [29] Letters ::= 'L' [ultmo]?
3578 * [30] Marks ::= 'M' [nce]?
3579 * [31] Numbers ::= 'N' [dlo]?
3580 * [32] Punctuation ::= 'P' [cdseifo]?
3581 * [33] Separators ::= 'Z' [slp]?
3582 * [34] Symbols ::= 'S' [mcko]?
3583 * [35] Others ::= 'C' [cfon]?
3584 * [36] IsBlock ::= 'Is' [a-zA-Z0-9#x2D]+
3585 */
3586static void
3587xmlFAParseCharProp(xmlRegParserCtxtPtr ctxt) {
3588 int cur;
William M. Brack779af002003-08-01 15:55:39 +00003589 xmlRegAtomType type = (xmlRegAtomType) 0;
Daniel Veillard4255d502002-04-16 15:50:10 +00003590 xmlChar *blockName = NULL;
3591
3592 cur = CUR;
3593 if (cur == 'L') {
3594 NEXT;
3595 cur = CUR;
3596 if (cur == 'u') {
3597 NEXT;
3598 type = XML_REGEXP_LETTER_UPPERCASE;
3599 } else if (cur == 'l') {
3600 NEXT;
3601 type = XML_REGEXP_LETTER_LOWERCASE;
3602 } else if (cur == 't') {
3603 NEXT;
3604 type = XML_REGEXP_LETTER_TITLECASE;
3605 } else if (cur == 'm') {
3606 NEXT;
3607 type = XML_REGEXP_LETTER_MODIFIER;
3608 } else if (cur == 'o') {
3609 NEXT;
3610 type = XML_REGEXP_LETTER_OTHERS;
3611 } else {
3612 type = XML_REGEXP_LETTER;
3613 }
3614 } else if (cur == 'M') {
3615 NEXT;
3616 cur = CUR;
3617 if (cur == 'n') {
3618 NEXT;
3619 /* nonspacing */
3620 type = XML_REGEXP_MARK_NONSPACING;
3621 } else if (cur == 'c') {
3622 NEXT;
3623 /* spacing combining */
3624 type = XML_REGEXP_MARK_SPACECOMBINING;
3625 } else if (cur == 'e') {
3626 NEXT;
3627 /* enclosing */
3628 type = XML_REGEXP_MARK_ENCLOSING;
3629 } else {
3630 /* all marks */
3631 type = XML_REGEXP_MARK;
3632 }
3633 } else if (cur == 'N') {
3634 NEXT;
3635 cur = CUR;
3636 if (cur == 'd') {
3637 NEXT;
3638 /* digital */
3639 type = XML_REGEXP_NUMBER_DECIMAL;
3640 } else if (cur == 'l') {
3641 NEXT;
3642 /* letter */
3643 type = XML_REGEXP_NUMBER_LETTER;
3644 } else if (cur == 'o') {
3645 NEXT;
3646 /* other */
3647 type = XML_REGEXP_NUMBER_OTHERS;
3648 } else {
3649 /* all numbers */
3650 type = XML_REGEXP_NUMBER;
3651 }
3652 } else if (cur == 'P') {
3653 NEXT;
3654 cur = CUR;
3655 if (cur == 'c') {
3656 NEXT;
3657 /* connector */
3658 type = XML_REGEXP_PUNCT_CONNECTOR;
3659 } else if (cur == 'd') {
3660 NEXT;
3661 /* dash */
3662 type = XML_REGEXP_PUNCT_DASH;
3663 } else if (cur == 's') {
3664 NEXT;
3665 /* open */
3666 type = XML_REGEXP_PUNCT_OPEN;
3667 } else if (cur == 'e') {
3668 NEXT;
3669 /* close */
3670 type = XML_REGEXP_PUNCT_CLOSE;
3671 } else if (cur == 'i') {
3672 NEXT;
3673 /* initial quote */
3674 type = XML_REGEXP_PUNCT_INITQUOTE;
3675 } else if (cur == 'f') {
3676 NEXT;
3677 /* final quote */
3678 type = XML_REGEXP_PUNCT_FINQUOTE;
3679 } else if (cur == 'o') {
3680 NEXT;
3681 /* other */
3682 type = XML_REGEXP_PUNCT_OTHERS;
3683 } else {
3684 /* all punctuation */
3685 type = XML_REGEXP_PUNCT;
3686 }
3687 } else if (cur == 'Z') {
3688 NEXT;
3689 cur = CUR;
3690 if (cur == 's') {
3691 NEXT;
3692 /* space */
3693 type = XML_REGEXP_SEPAR_SPACE;
3694 } else if (cur == 'l') {
3695 NEXT;
3696 /* line */
3697 type = XML_REGEXP_SEPAR_LINE;
3698 } else if (cur == 'p') {
3699 NEXT;
3700 /* paragraph */
3701 type = XML_REGEXP_SEPAR_PARA;
3702 } else {
3703 /* all separators */
3704 type = XML_REGEXP_SEPAR;
3705 }
3706 } else if (cur == 'S') {
3707 NEXT;
3708 cur = CUR;
3709 if (cur == 'm') {
3710 NEXT;
3711 type = XML_REGEXP_SYMBOL_MATH;
3712 /* math */
3713 } else if (cur == 'c') {
3714 NEXT;
3715 type = XML_REGEXP_SYMBOL_CURRENCY;
3716 /* currency */
3717 } else if (cur == 'k') {
3718 NEXT;
3719 type = XML_REGEXP_SYMBOL_MODIFIER;
3720 /* modifiers */
3721 } else if (cur == 'o') {
3722 NEXT;
3723 type = XML_REGEXP_SYMBOL_OTHERS;
3724 /* other */
3725 } else {
3726 /* all symbols */
3727 type = XML_REGEXP_SYMBOL;
3728 }
3729 } else if (cur == 'C') {
3730 NEXT;
3731 cur = CUR;
3732 if (cur == 'c') {
3733 NEXT;
3734 /* control */
3735 type = XML_REGEXP_OTHER_CONTROL;
3736 } else if (cur == 'f') {
3737 NEXT;
3738 /* format */
3739 type = XML_REGEXP_OTHER_FORMAT;
3740 } else if (cur == 'o') {
3741 NEXT;
3742 /* private use */
3743 type = XML_REGEXP_OTHER_PRIVATE;
3744 } else if (cur == 'n') {
3745 NEXT;
3746 /* not assigned */
3747 type = XML_REGEXP_OTHER_NA;
3748 } else {
3749 /* all others */
3750 type = XML_REGEXP_OTHER;
3751 }
3752 } else if (cur == 'I') {
3753 const xmlChar *start;
3754 NEXT;
3755 cur = CUR;
3756 if (cur != 's') {
3757 ERROR("IsXXXX expected");
3758 return;
3759 }
3760 NEXT;
3761 start = ctxt->cur;
3762 cur = CUR;
3763 if (((cur >= 'a') && (cur <= 'z')) ||
3764 ((cur >= 'A') && (cur <= 'Z')) ||
3765 ((cur >= '0') && (cur <= '9')) ||
3766 (cur == 0x2D)) {
3767 NEXT;
3768 cur = CUR;
3769 while (((cur >= 'a') && (cur <= 'z')) ||
3770 ((cur >= 'A') && (cur <= 'Z')) ||
3771 ((cur >= '0') && (cur <= '9')) ||
3772 (cur == 0x2D)) {
3773 NEXT;
3774 cur = CUR;
3775 }
3776 }
3777 type = XML_REGEXP_BLOCK_NAME;
3778 blockName = xmlStrndup(start, ctxt->cur - start);
3779 } else {
3780 ERROR("Unknown char property");
3781 return;
3782 }
3783 if (ctxt->atom == NULL) {
3784 ctxt->atom = xmlRegNewAtom(ctxt, type);
3785 if (ctxt->atom != NULL)
3786 ctxt->atom->valuep = blockName;
3787 } else if (ctxt->atom->type == XML_REGEXP_RANGES) {
3788 xmlRegAtomAddRange(ctxt, ctxt->atom, ctxt->neg,
3789 type, 0, 0, blockName);
3790 }
3791}
3792
3793/**
3794 * xmlFAParseCharClassEsc:
Daniel Veillard441bc322002-04-20 17:38:48 +00003795 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00003796 *
3797 * [23] charClassEsc ::= ( SingleCharEsc | MultiCharEsc | catEsc | complEsc )
3798 * [24] SingleCharEsc ::= '\' [nrt\|.?*+(){}#x2D#x5B#x5D#x5E]
3799 * [25] catEsc ::= '\p{' charProp '}'
3800 * [26] complEsc ::= '\P{' charProp '}'
3801 * [37] MultiCharEsc ::= '.' | ('\' [sSiIcCdDwW])
3802 */
3803static void
3804xmlFAParseCharClassEsc(xmlRegParserCtxtPtr ctxt) {
3805 int cur;
3806
3807 if (CUR == '.') {
3808 if (ctxt->atom == NULL) {
3809 ctxt->atom = xmlRegNewAtom(ctxt, XML_REGEXP_ANYCHAR);
3810 } else if (ctxt->atom->type == XML_REGEXP_RANGES) {
3811 xmlRegAtomAddRange(ctxt, ctxt->atom, ctxt->neg,
3812 XML_REGEXP_ANYCHAR, 0, 0, NULL);
3813 }
3814 NEXT;
3815 return;
3816 }
3817 if (CUR != '\\') {
3818 ERROR("Escaped sequence: expecting \\");
3819 return;
3820 }
3821 NEXT;
3822 cur = CUR;
3823 if (cur == 'p') {
3824 NEXT;
3825 if (CUR != '{') {
3826 ERROR("Expecting '{'");
3827 return;
3828 }
3829 NEXT;
3830 xmlFAParseCharProp(ctxt);
3831 if (CUR != '}') {
3832 ERROR("Expecting '}'");
3833 return;
3834 }
3835 NEXT;
3836 } else if (cur == 'P') {
3837 NEXT;
3838 if (CUR != '{') {
3839 ERROR("Expecting '{'");
3840 return;
3841 }
3842 NEXT;
3843 xmlFAParseCharProp(ctxt);
3844 ctxt->atom->neg = 1;
3845 if (CUR != '}') {
3846 ERROR("Expecting '}'");
3847 return;
3848 }
3849 NEXT;
3850 } else if ((cur == 'n') || (cur == 'r') || (cur == 't') || (cur == '\\') ||
3851 (cur == '|') || (cur == '.') || (cur == '?') || (cur == '*') ||
3852 (cur == '+') || (cur == '(') || (cur == ')') || (cur == '{') ||
3853 (cur == '}') || (cur == 0x2D) || (cur == 0x5B) || (cur == 0x5D) ||
3854 (cur == 0x5E)) {
3855 if (ctxt->atom == NULL) {
3856 ctxt->atom = xmlRegNewAtom(ctxt, XML_REGEXP_CHARVAL);
Daniel Veillard99c394d2005-07-14 12:58:49 +00003857 if (ctxt->atom != NULL) {
3858 switch (cur) {
3859 case 'n':
3860 ctxt->atom->codepoint = '\n';
3861 break;
3862 case 'r':
3863 ctxt->atom->codepoint = '\r';
3864 break;
3865 case 't':
3866 ctxt->atom->codepoint = '\t';
3867 break;
3868 default:
3869 ctxt->atom->codepoint = cur;
3870 }
3871 }
Daniel Veillard4255d502002-04-16 15:50:10 +00003872 } else if (ctxt->atom->type == XML_REGEXP_RANGES) {
3873 xmlRegAtomAddRange(ctxt, ctxt->atom, ctxt->neg,
3874 XML_REGEXP_CHARVAL, cur, cur, NULL);
3875 }
3876 NEXT;
3877 } else if ((cur == 's') || (cur == 'S') || (cur == 'i') || (cur == 'I') ||
3878 (cur == 'c') || (cur == 'C') || (cur == 'd') || (cur == 'D') ||
3879 (cur == 'w') || (cur == 'W')) {
Daniel Veillardb509f152002-04-17 16:28:10 +00003880 xmlRegAtomType type = XML_REGEXP_ANYSPACE;
Daniel Veillard4255d502002-04-16 15:50:10 +00003881
3882 switch (cur) {
3883 case 's':
3884 type = XML_REGEXP_ANYSPACE;
3885 break;
3886 case 'S':
3887 type = XML_REGEXP_NOTSPACE;
3888 break;
3889 case 'i':
3890 type = XML_REGEXP_INITNAME;
3891 break;
3892 case 'I':
3893 type = XML_REGEXP_NOTINITNAME;
3894 break;
3895 case 'c':
3896 type = XML_REGEXP_NAMECHAR;
3897 break;
3898 case 'C':
3899 type = XML_REGEXP_NOTNAMECHAR;
3900 break;
3901 case 'd':
3902 type = XML_REGEXP_DECIMAL;
3903 break;
3904 case 'D':
3905 type = XML_REGEXP_NOTDECIMAL;
3906 break;
3907 case 'w':
3908 type = XML_REGEXP_REALCHAR;
3909 break;
3910 case 'W':
3911 type = XML_REGEXP_NOTREALCHAR;
3912 break;
3913 }
3914 NEXT;
3915 if (ctxt->atom == NULL) {
3916 ctxt->atom = xmlRegNewAtom(ctxt, type);
3917 } else if (ctxt->atom->type == XML_REGEXP_RANGES) {
3918 xmlRegAtomAddRange(ctxt, ctxt->atom, ctxt->neg,
3919 type, 0, 0, NULL);
3920 }
3921 }
3922}
3923
3924/**
3925 * xmlFAParseCharRef:
Daniel Veillard441bc322002-04-20 17:38:48 +00003926 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00003927 *
3928 * [19] XmlCharRef ::= ( '&#' [0-9]+ ';' ) | (' &#x' [0-9a-fA-F]+ ';' )
3929 */
3930static int
3931xmlFAParseCharRef(xmlRegParserCtxtPtr ctxt) {
3932 int ret = 0, cur;
3933
3934 if ((CUR != '&') || (NXT(1) != '#'))
3935 return(-1);
3936 NEXT;
3937 NEXT;
3938 cur = CUR;
3939 if (cur == 'x') {
3940 NEXT;
3941 cur = CUR;
3942 if (((cur >= '0') && (cur <= '9')) ||
3943 ((cur >= 'a') && (cur <= 'f')) ||
3944 ((cur >= 'A') && (cur <= 'F'))) {
3945 while (((cur >= '0') && (cur <= '9')) ||
3946 ((cur >= 'A') && (cur <= 'F'))) {
3947 if ((cur >= '0') && (cur <= '9'))
3948 ret = ret * 16 + cur - '0';
3949 else if ((cur >= 'a') && (cur <= 'f'))
3950 ret = ret * 16 + 10 + (cur - 'a');
3951 else
3952 ret = ret * 16 + 10 + (cur - 'A');
3953 NEXT;
3954 cur = CUR;
3955 }
3956 } else {
3957 ERROR("Char ref: expecting [0-9A-F]");
3958 return(-1);
3959 }
3960 } else {
3961 if ((cur >= '0') && (cur <= '9')) {
3962 while ((cur >= '0') && (cur <= '9')) {
3963 ret = ret * 10 + cur - '0';
3964 NEXT;
3965 cur = CUR;
3966 }
3967 } else {
3968 ERROR("Char ref: expecting [0-9]");
3969 return(-1);
3970 }
3971 }
3972 if (cur != ';') {
3973 ERROR("Char ref: expecting ';'");
3974 return(-1);
3975 } else {
3976 NEXT;
3977 }
3978 return(ret);
3979}
3980
3981/**
3982 * xmlFAParseCharRange:
Daniel Veillard441bc322002-04-20 17:38:48 +00003983 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00003984 *
3985 * [17] charRange ::= seRange | XmlCharRef | XmlCharIncDash
3986 * [18] seRange ::= charOrEsc '-' charOrEsc
3987 * [20] charOrEsc ::= XmlChar | SingleCharEsc
3988 * [21] XmlChar ::= [^\#x2D#x5B#x5D]
3989 * [22] XmlCharIncDash ::= [^\#x5B#x5D]
3990 */
3991static void
3992xmlFAParseCharRange(xmlRegParserCtxtPtr ctxt) {
William M. Brackdc99df92003-12-27 01:54:25 +00003993 int cur, len;
Daniel Veillard4255d502002-04-16 15:50:10 +00003994 int start = -1;
3995 int end = -1;
3996
3997 if ((CUR == '&') && (NXT(1) == '#')) {
3998 end = start = xmlFAParseCharRef(ctxt);
3999 xmlRegAtomAddRange(ctxt, ctxt->atom, ctxt->neg,
4000 XML_REGEXP_CHARVAL, start, end, NULL);
4001 return;
4002 }
4003 cur = CUR;
4004 if (cur == '\\') {
4005 NEXT;
4006 cur = CUR;
4007 switch (cur) {
4008 case 'n': start = 0xA; break;
4009 case 'r': start = 0xD; break;
4010 case 't': start = 0x9; break;
4011 case '\\': case '|': case '.': case '-': case '^': case '?':
4012 case '*': case '+': case '{': case '}': case '(': case ')':
4013 case '[': case ']':
4014 start = cur; break;
4015 default:
4016 ERROR("Invalid escape value");
4017 return;
4018 }
4019 end = start;
William M. Brackdc99df92003-12-27 01:54:25 +00004020 len = 1;
Daniel Veillard4255d502002-04-16 15:50:10 +00004021 } else if ((cur != 0x5B) && (cur != 0x5D)) {
William M. Brackdc99df92003-12-27 01:54:25 +00004022 end = start = CUR_SCHAR(ctxt->cur, len);
Daniel Veillard4255d502002-04-16 15:50:10 +00004023 } else {
4024 ERROR("Expecting a char range");
4025 return;
4026 }
William M. Brackdc99df92003-12-27 01:54:25 +00004027 NEXTL(len);
Daniel Veillard4255d502002-04-16 15:50:10 +00004028 if (start == '-') {
4029 return;
4030 }
4031 cur = CUR;
William M. Brack10f1ef42004-03-20 14:51:25 +00004032 if ((cur != '-') || (NXT(1) == ']')) {
Daniel Veillard4255d502002-04-16 15:50:10 +00004033 xmlRegAtomAddRange(ctxt, ctxt->atom, ctxt->neg,
4034 XML_REGEXP_CHARVAL, start, end, NULL);
4035 return;
4036 }
4037 NEXT;
4038 cur = CUR;
4039 if (cur == '\\') {
4040 NEXT;
4041 cur = CUR;
4042 switch (cur) {
4043 case 'n': end = 0xA; break;
4044 case 'r': end = 0xD; break;
4045 case 't': end = 0x9; break;
4046 case '\\': case '|': case '.': case '-': case '^': case '?':
4047 case '*': case '+': case '{': case '}': case '(': case ')':
4048 case '[': case ']':
4049 end = cur; break;
4050 default:
4051 ERROR("Invalid escape value");
4052 return;
4053 }
William M. Brackdc99df92003-12-27 01:54:25 +00004054 len = 1;
Daniel Veillard4255d502002-04-16 15:50:10 +00004055 } else if ((cur != 0x5B) && (cur != 0x5D)) {
William M. Brackdc99df92003-12-27 01:54:25 +00004056 end = CUR_SCHAR(ctxt->cur, len);
Daniel Veillard4255d502002-04-16 15:50:10 +00004057 } else {
4058 ERROR("Expecting the end of a char range");
4059 return;
4060 }
William M. Brackdc99df92003-12-27 01:54:25 +00004061 NEXTL(len);
Daniel Veillard4255d502002-04-16 15:50:10 +00004062 /* TODO check that the values are acceptable character ranges for XML */
4063 if (end < start) {
4064 ERROR("End of range is before start of range");
4065 } else {
4066 xmlRegAtomAddRange(ctxt, ctxt->atom, ctxt->neg,
4067 XML_REGEXP_CHARVAL, start, end, NULL);
4068 }
4069 return;
4070}
4071
4072/**
4073 * xmlFAParsePosCharGroup:
Daniel Veillard441bc322002-04-20 17:38:48 +00004074 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00004075 *
4076 * [14] posCharGroup ::= ( charRange | charClassEsc )+
4077 */
4078static void
4079xmlFAParsePosCharGroup(xmlRegParserCtxtPtr ctxt) {
4080 do {
4081 if ((CUR == '\\') || (CUR == '.')) {
4082 xmlFAParseCharClassEsc(ctxt);
4083 } else {
4084 xmlFAParseCharRange(ctxt);
4085 }
4086 } while ((CUR != ']') && (CUR != '^') && (CUR != '-') &&
4087 (ctxt->error == 0));
4088}
4089
4090/**
4091 * xmlFAParseCharGroup:
Daniel Veillard441bc322002-04-20 17:38:48 +00004092 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00004093 *
4094 * [13] charGroup ::= posCharGroup | negCharGroup | charClassSub
4095 * [15] negCharGroup ::= '^' posCharGroup
4096 * [16] charClassSub ::= ( posCharGroup | negCharGroup ) '-' charClassExpr
4097 * [12] charClassExpr ::= '[' charGroup ']'
4098 */
4099static void
4100xmlFAParseCharGroup(xmlRegParserCtxtPtr ctxt) {
4101 int n = ctxt->neg;
4102 while ((CUR != ']') && (ctxt->error == 0)) {
4103 if (CUR == '^') {
4104 int neg = ctxt->neg;
4105
4106 NEXT;
4107 ctxt->neg = !ctxt->neg;
4108 xmlFAParsePosCharGroup(ctxt);
4109 ctxt->neg = neg;
William M. Brack10f1ef42004-03-20 14:51:25 +00004110 } else if ((CUR == '-') && (NXT(1) == '[')) {
Daniel Veillardf8b9de32003-11-24 14:27:26 +00004111 int neg = ctxt->neg;
Daniel Veillardf8b9de32003-11-24 14:27:26 +00004112 ctxt->neg = 2;
William M. Brack10f1ef42004-03-20 14:51:25 +00004113 NEXT; /* eat the '-' */
4114 NEXT; /* eat the '[' */
Daniel Veillard4255d502002-04-16 15:50:10 +00004115 xmlFAParseCharGroup(ctxt);
4116 if (CUR == ']') {
4117 NEXT;
4118 } else {
4119 ERROR("charClassExpr: ']' expected");
4120 break;
4121 }
Daniel Veillardf8b9de32003-11-24 14:27:26 +00004122 ctxt->neg = neg;
Daniel Veillard4255d502002-04-16 15:50:10 +00004123 break;
4124 } else if (CUR != ']') {
4125 xmlFAParsePosCharGroup(ctxt);
4126 }
4127 }
4128 ctxt->neg = n;
4129}
4130
4131/**
4132 * xmlFAParseCharClass:
Daniel Veillard441bc322002-04-20 17:38:48 +00004133 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00004134 *
4135 * [11] charClass ::= charClassEsc | charClassExpr
4136 * [12] charClassExpr ::= '[' charGroup ']'
4137 */
4138static void
4139xmlFAParseCharClass(xmlRegParserCtxtPtr ctxt) {
4140 if (CUR == '[') {
4141 NEXT;
4142 ctxt->atom = xmlRegNewAtom(ctxt, XML_REGEXP_RANGES);
4143 if (ctxt->atom == NULL)
4144 return;
4145 xmlFAParseCharGroup(ctxt);
4146 if (CUR == ']') {
4147 NEXT;
4148 } else {
4149 ERROR("xmlFAParseCharClass: ']' expected");
4150 }
4151 } else {
4152 xmlFAParseCharClassEsc(ctxt);
4153 }
4154}
4155
4156/**
4157 * xmlFAParseQuantExact:
Daniel Veillard441bc322002-04-20 17:38:48 +00004158 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00004159 *
4160 * [8] QuantExact ::= [0-9]+
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00004161 *
4162 * Returns 0 if success or -1 in case of error
Daniel Veillard4255d502002-04-16 15:50:10 +00004163 */
4164static int
4165xmlFAParseQuantExact(xmlRegParserCtxtPtr ctxt) {
4166 int ret = 0;
4167 int ok = 0;
4168
4169 while ((CUR >= '0') && (CUR <= '9')) {
4170 ret = ret * 10 + (CUR - '0');
4171 ok = 1;
4172 NEXT;
4173 }
4174 if (ok != 1) {
4175 return(-1);
4176 }
4177 return(ret);
4178}
4179
4180/**
4181 * xmlFAParseQuantifier:
Daniel Veillard441bc322002-04-20 17:38:48 +00004182 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00004183 *
4184 * [4] quantifier ::= [?*+] | ( '{' quantity '}' )
4185 * [5] quantity ::= quantRange | quantMin | QuantExact
4186 * [6] quantRange ::= QuantExact ',' QuantExact
4187 * [7] quantMin ::= QuantExact ','
4188 * [8] QuantExact ::= [0-9]+
4189 */
4190static int
4191xmlFAParseQuantifier(xmlRegParserCtxtPtr ctxt) {
4192 int cur;
4193
4194 cur = CUR;
4195 if ((cur == '?') || (cur == '*') || (cur == '+')) {
4196 if (ctxt->atom != NULL) {
4197 if (cur == '?')
4198 ctxt->atom->quant = XML_REGEXP_QUANT_OPT;
4199 else if (cur == '*')
4200 ctxt->atom->quant = XML_REGEXP_QUANT_MULT;
4201 else if (cur == '+')
4202 ctxt->atom->quant = XML_REGEXP_QUANT_PLUS;
4203 }
4204 NEXT;
4205 return(1);
4206 }
4207 if (cur == '{') {
4208 int min = 0, max = 0;
4209
4210 NEXT;
4211 cur = xmlFAParseQuantExact(ctxt);
4212 if (cur >= 0)
4213 min = cur;
4214 if (CUR == ',') {
4215 NEXT;
Daniel Veillardebe48c62003-12-03 12:12:27 +00004216 if (CUR == '}')
4217 max = INT_MAX;
4218 else {
4219 cur = xmlFAParseQuantExact(ctxt);
4220 if (cur >= 0)
4221 max = cur;
4222 else {
4223 ERROR("Improper quantifier");
4224 }
4225 }
Daniel Veillard4255d502002-04-16 15:50:10 +00004226 }
4227 if (CUR == '}') {
4228 NEXT;
4229 } else {
4230 ERROR("Unterminated quantifier");
4231 }
4232 if (max == 0)
4233 max = min;
4234 if (ctxt->atom != NULL) {
4235 ctxt->atom->quant = XML_REGEXP_QUANT_RANGE;
4236 ctxt->atom->min = min;
4237 ctxt->atom->max = max;
4238 }
4239 return(1);
4240 }
4241 return(0);
4242}
4243
4244/**
4245 * xmlFAParseAtom:
Daniel Veillard441bc322002-04-20 17:38:48 +00004246 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00004247 *
4248 * [9] atom ::= Char | charClass | ( '(' regExp ')' )
4249 */
4250static int
4251xmlFAParseAtom(xmlRegParserCtxtPtr ctxt) {
4252 int codepoint, len;
4253
4254 codepoint = xmlFAIsChar(ctxt);
4255 if (codepoint > 0) {
4256 ctxt->atom = xmlRegNewAtom(ctxt, XML_REGEXP_CHARVAL);
4257 if (ctxt->atom == NULL)
4258 return(-1);
4259 codepoint = CUR_SCHAR(ctxt->cur, len);
4260 ctxt->atom->codepoint = codepoint;
4261 NEXTL(len);
4262 return(1);
4263 } else if (CUR == '|') {
4264 return(0);
4265 } else if (CUR == 0) {
4266 return(0);
4267 } else if (CUR == ')') {
4268 return(0);
4269 } else if (CUR == '(') {
4270 xmlRegStatePtr start, oldend;
4271
4272 NEXT;
4273 xmlFAGenerateEpsilonTransition(ctxt, ctxt->state, NULL);
4274 start = ctxt->state;
4275 oldend = ctxt->end;
4276 ctxt->end = NULL;
4277 ctxt->atom = NULL;
4278 xmlFAParseRegExp(ctxt, 0);
4279 if (CUR == ')') {
4280 NEXT;
4281 } else {
4282 ERROR("xmlFAParseAtom: expecting ')'");
4283 }
4284 ctxt->atom = xmlRegNewAtom(ctxt, XML_REGEXP_SUBREG);
4285 if (ctxt->atom == NULL)
4286 return(-1);
4287 ctxt->atom->start = start;
4288 ctxt->atom->stop = ctxt->state;
4289 ctxt->end = oldend;
4290 return(1);
4291 } else if ((CUR == '[') || (CUR == '\\') || (CUR == '.')) {
4292 xmlFAParseCharClass(ctxt);
4293 return(1);
4294 }
4295 return(0);
4296}
4297
4298/**
4299 * xmlFAParsePiece:
Daniel Veillard441bc322002-04-20 17:38:48 +00004300 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00004301 *
4302 * [3] piece ::= atom quantifier?
4303 */
4304static int
4305xmlFAParsePiece(xmlRegParserCtxtPtr ctxt) {
4306 int ret;
4307
4308 ctxt->atom = NULL;
4309 ret = xmlFAParseAtom(ctxt);
4310 if (ret == 0)
4311 return(0);
4312 if (ctxt->atom == NULL) {
4313 ERROR("internal: no atom generated");
4314 }
4315 xmlFAParseQuantifier(ctxt);
4316 return(1);
4317}
4318
4319/**
4320 * xmlFAParseBranch:
Daniel Veillard441bc322002-04-20 17:38:48 +00004321 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00004322 *
4323 * [2] branch ::= piece*
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00004324 8
Daniel Veillard4255d502002-04-16 15:50:10 +00004325 */
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00004326static int
Daniel Veillard2cbf5962004-03-31 15:50:43 +00004327xmlFAParseBranch(xmlRegParserCtxtPtr ctxt) {
Daniel Veillard4255d502002-04-16 15:50:10 +00004328 xmlRegStatePtr previous;
Daniel Veillard4255d502002-04-16 15:50:10 +00004329 int ret;
4330
4331 previous = ctxt->state;
4332 ret = xmlFAParsePiece(ctxt);
4333 if (ret != 0) {
Daniel Veillard2cbf5962004-03-31 15:50:43 +00004334 if (xmlFAGenerateTransitions(ctxt, previous, NULL, ctxt->atom) < 0)
4335 return(-1);
4336 previous = ctxt->state;
Daniel Veillard4255d502002-04-16 15:50:10 +00004337 ctxt->atom = NULL;
4338 }
4339 while ((ret != 0) && (ctxt->error == 0)) {
4340 ret = xmlFAParsePiece(ctxt);
4341 if (ret != 0) {
Daniel Veillard2cbf5962004-03-31 15:50:43 +00004342 if (xmlFAGenerateTransitions(ctxt, previous, NULL,
4343 ctxt->atom) < 0)
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00004344 return(-1);
Daniel Veillard4255d502002-04-16 15:50:10 +00004345 previous = ctxt->state;
4346 ctxt->atom = NULL;
4347 }
4348 }
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00004349 return(0);
Daniel Veillard4255d502002-04-16 15:50:10 +00004350}
4351
4352/**
4353 * xmlFAParseRegExp:
Daniel Veillard441bc322002-04-20 17:38:48 +00004354 * @ctxt: a regexp parser context
William M. Brackddf71d62004-05-06 04:17:26 +00004355 * @top: is this the top-level expression ?
Daniel Veillard4255d502002-04-16 15:50:10 +00004356 *
4357 * [1] regExp ::= branch ( '|' branch )*
4358 */
4359static void
4360xmlFAParseRegExp(xmlRegParserCtxtPtr ctxt, int top) {
Daniel Veillardc7e3cc42004-09-28 12:33:52 +00004361 xmlRegStatePtr start, end;
Daniel Veillard4255d502002-04-16 15:50:10 +00004362
Daniel Veillard2cbf5962004-03-31 15:50:43 +00004363 /* if not top start should have been generated by an epsilon trans */
Daniel Veillard4255d502002-04-16 15:50:10 +00004364 start = ctxt->state;
Daniel Veillard2cbf5962004-03-31 15:50:43 +00004365 ctxt->end = NULL;
4366 xmlFAParseBranch(ctxt);
4367 if (top) {
4368#ifdef DEBUG_REGEXP_GRAPH
4369 printf("State %d is final\n", ctxt->state->no);
4370#endif
4371 ctxt->state->type = XML_REGEXP_FINAL_STATE;
4372 }
Daniel Veillard4255d502002-04-16 15:50:10 +00004373 if (CUR != '|') {
4374 ctxt->end = ctxt->state;
4375 return;
4376 }
4377 end = ctxt->state;
4378 while ((CUR == '|') && (ctxt->error == 0)) {
4379 NEXT;
4380 ctxt->state = start;
Daniel Veillard2cbf5962004-03-31 15:50:43 +00004381 ctxt->end = NULL;
4382 xmlFAParseBranch(ctxt);
4383 if (top) {
4384 ctxt->state->type = XML_REGEXP_FINAL_STATE;
4385#ifdef DEBUG_REGEXP_GRAPH
4386 printf("State %d is final\n", ctxt->state->no);
4387#endif
4388 } else {
4389 xmlFAGenerateEpsilonTransition(ctxt, ctxt->state, end);
4390 }
Daniel Veillard4255d502002-04-16 15:50:10 +00004391 }
Daniel Veillard2cbf5962004-03-31 15:50:43 +00004392 if (!top) {
4393 ctxt->state = end;
4394 ctxt->end = end;
4395 }
Daniel Veillard4255d502002-04-16 15:50:10 +00004396}
4397
4398/************************************************************************
4399 * *
4400 * The basic API *
4401 * *
4402 ************************************************************************/
4403
4404/**
4405 * xmlRegexpPrint:
4406 * @output: the file for the output debug
4407 * @regexp: the compiled regexp
4408 *
4409 * Print the content of the compiled regular expression
4410 */
4411void
4412xmlRegexpPrint(FILE *output, xmlRegexpPtr regexp) {
4413 int i;
4414
Daniel Veillarda82b1822004-11-08 16:24:57 +00004415 if (output == NULL)
4416 return;
Daniel Veillard4255d502002-04-16 15:50:10 +00004417 fprintf(output, " regexp: ");
4418 if (regexp == NULL) {
4419 fprintf(output, "NULL\n");
4420 return;
4421 }
4422 fprintf(output, "'%s' ", regexp->string);
4423 fprintf(output, "\n");
4424 fprintf(output, "%d atoms:\n", regexp->nbAtoms);
4425 for (i = 0;i < regexp->nbAtoms; i++) {
4426 fprintf(output, " %02d ", i);
4427 xmlRegPrintAtom(output, regexp->atoms[i]);
4428 }
4429 fprintf(output, "%d states:", regexp->nbStates);
4430 fprintf(output, "\n");
4431 for (i = 0;i < regexp->nbStates; i++) {
4432 xmlRegPrintState(output, regexp->states[i]);
4433 }
4434 fprintf(output, "%d counters:\n", regexp->nbCounters);
4435 for (i = 0;i < regexp->nbCounters; i++) {
4436 fprintf(output, " %d: min %d max %d\n", i, regexp->counters[i].min,
4437 regexp->counters[i].max);
4438 }
4439}
4440
4441/**
4442 * xmlRegexpCompile:
4443 * @regexp: a regular expression string
4444 *
4445 * Parses a regular expression conforming to XML Schemas Part 2 Datatype
William M. Brackddf71d62004-05-06 04:17:26 +00004446 * Appendix F and builds an automata suitable for testing strings against
Daniel Veillard4255d502002-04-16 15:50:10 +00004447 * that regular expression
4448 *
4449 * Returns the compiled expression or NULL in case of error
4450 */
4451xmlRegexpPtr
4452xmlRegexpCompile(const xmlChar *regexp) {
4453 xmlRegexpPtr ret;
4454 xmlRegParserCtxtPtr ctxt;
4455
4456 ctxt = xmlRegNewParserCtxt(regexp);
4457 if (ctxt == NULL)
4458 return(NULL);
4459
4460 /* initialize the parser */
4461 ctxt->end = NULL;
4462 ctxt->start = ctxt->state = xmlRegNewState(ctxt);
4463 xmlRegStatePush(ctxt, ctxt->start);
4464
4465 /* parse the expression building an automata */
4466 xmlFAParseRegExp(ctxt, 1);
4467 if (CUR != 0) {
4468 ERROR("xmlFAParseRegExp: extra characters");
4469 }
4470 ctxt->end = ctxt->state;
4471 ctxt->start->type = XML_REGEXP_START_STATE;
4472 ctxt->end->type = XML_REGEXP_FINAL_STATE;
4473
4474 /* remove the Epsilon except for counted transitions */
4475 xmlFAEliminateEpsilonTransitions(ctxt);
4476
4477
4478 if (ctxt->error != 0) {
4479 xmlRegFreeParserCtxt(ctxt);
4480 return(NULL);
4481 }
4482 ret = xmlRegEpxFromParse(ctxt);
4483 xmlRegFreeParserCtxt(ctxt);
4484 return(ret);
4485}
4486
4487/**
4488 * xmlRegexpExec:
4489 * @comp: the compiled regular expression
4490 * @content: the value to check against the regular expression
4491 *
William M. Brackddf71d62004-05-06 04:17:26 +00004492 * Check if the regular expression generates the value
Daniel Veillard4255d502002-04-16 15:50:10 +00004493 *
William M. Brackddf71d62004-05-06 04:17:26 +00004494 * Returns 1 if it matches, 0 if not and a negative value in case of error
Daniel Veillard4255d502002-04-16 15:50:10 +00004495 */
4496int
4497xmlRegexpExec(xmlRegexpPtr comp, const xmlChar *content) {
4498 if ((comp == NULL) || (content == NULL))
4499 return(-1);
4500 return(xmlFARegExec(comp, content));
4501}
4502
4503/**
Daniel Veillard23e73572002-09-19 19:56:43 +00004504 * xmlRegexpIsDeterminist:
4505 * @comp: the compiled regular expression
4506 *
4507 * Check if the regular expression is determinist
4508 *
William M. Brackddf71d62004-05-06 04:17:26 +00004509 * Returns 1 if it yes, 0 if not and a negative value in case of error
Daniel Veillard23e73572002-09-19 19:56:43 +00004510 */
4511int
4512xmlRegexpIsDeterminist(xmlRegexpPtr comp) {
4513 xmlAutomataPtr am;
4514 int ret;
4515
4516 if (comp == NULL)
4517 return(-1);
4518 if (comp->determinist != -1)
4519 return(comp->determinist);
4520
4521 am = xmlNewAutomata();
Daniel Veillardbd9afb52002-09-25 22:25:35 +00004522 if (am->states != NULL) {
4523 int i;
4524
4525 for (i = 0;i < am->nbStates;i++)
4526 xmlRegFreeState(am->states[i]);
4527 xmlFree(am->states);
4528 }
Daniel Veillard23e73572002-09-19 19:56:43 +00004529 am->nbAtoms = comp->nbAtoms;
4530 am->atoms = comp->atoms;
4531 am->nbStates = comp->nbStates;
4532 am->states = comp->states;
4533 am->determinist = -1;
4534 ret = xmlFAComputesDeterminism(am);
4535 am->atoms = NULL;
4536 am->states = NULL;
4537 xmlFreeAutomata(am);
4538 return(ret);
4539}
4540
4541/**
Daniel Veillard4255d502002-04-16 15:50:10 +00004542 * xmlRegFreeRegexp:
4543 * @regexp: the regexp
4544 *
4545 * Free a regexp
4546 */
4547void
4548xmlRegFreeRegexp(xmlRegexpPtr regexp) {
4549 int i;
4550 if (regexp == NULL)
4551 return;
4552
4553 if (regexp->string != NULL)
4554 xmlFree(regexp->string);
4555 if (regexp->states != NULL) {
4556 for (i = 0;i < regexp->nbStates;i++)
4557 xmlRegFreeState(regexp->states[i]);
4558 xmlFree(regexp->states);
4559 }
4560 if (regexp->atoms != NULL) {
4561 for (i = 0;i < regexp->nbAtoms;i++)
4562 xmlRegFreeAtom(regexp->atoms[i]);
4563 xmlFree(regexp->atoms);
4564 }
4565 if (regexp->counters != NULL)
4566 xmlFree(regexp->counters);
Daniel Veillard23e73572002-09-19 19:56:43 +00004567 if (regexp->compact != NULL)
4568 xmlFree(regexp->compact);
Daniel Veillard118aed72002-09-24 14:13:13 +00004569 if (regexp->transdata != NULL)
4570 xmlFree(regexp->transdata);
Daniel Veillard23e73572002-09-19 19:56:43 +00004571 if (regexp->stringMap != NULL) {
4572 for (i = 0; i < regexp->nbstrings;i++)
4573 xmlFree(regexp->stringMap[i]);
4574 xmlFree(regexp->stringMap);
4575 }
4576
Daniel Veillard4255d502002-04-16 15:50:10 +00004577 xmlFree(regexp);
4578}
4579
4580#ifdef LIBXML_AUTOMATA_ENABLED
4581/************************************************************************
4582 * *
4583 * The Automata interface *
4584 * *
4585 ************************************************************************/
4586
4587/**
4588 * xmlNewAutomata:
4589 *
4590 * Create a new automata
4591 *
4592 * Returns the new object or NULL in case of failure
4593 */
4594xmlAutomataPtr
4595xmlNewAutomata(void) {
4596 xmlAutomataPtr ctxt;
4597
4598 ctxt = xmlRegNewParserCtxt(NULL);
4599 if (ctxt == NULL)
4600 return(NULL);
4601
4602 /* initialize the parser */
4603 ctxt->end = NULL;
4604 ctxt->start = ctxt->state = xmlRegNewState(ctxt);
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00004605 if (ctxt->start == NULL) {
4606 xmlFreeAutomata(ctxt);
4607 return(NULL);
4608 }
4609 if (xmlRegStatePush(ctxt, ctxt->start) < 0) {
4610 xmlRegFreeState(ctxt->start);
4611 xmlFreeAutomata(ctxt);
4612 return(NULL);
4613 }
Daniel Veillard4255d502002-04-16 15:50:10 +00004614
4615 return(ctxt);
4616}
4617
4618/**
4619 * xmlFreeAutomata:
4620 * @am: an automata
4621 *
4622 * Free an automata
4623 */
4624void
4625xmlFreeAutomata(xmlAutomataPtr am) {
4626 if (am == NULL)
4627 return;
4628 xmlRegFreeParserCtxt(am);
4629}
4630
4631/**
4632 * xmlAutomataGetInitState:
4633 * @am: an automata
4634 *
Daniel Veillarda9b66d02002-12-11 14:23:49 +00004635 * Initial state lookup
4636 *
Daniel Veillard4255d502002-04-16 15:50:10 +00004637 * Returns the initial state of the automata
4638 */
4639xmlAutomataStatePtr
4640xmlAutomataGetInitState(xmlAutomataPtr am) {
4641 if (am == NULL)
4642 return(NULL);
4643 return(am->start);
4644}
4645
4646/**
4647 * xmlAutomataSetFinalState:
4648 * @am: an automata
4649 * @state: a state in this automata
4650 *
4651 * Makes that state a final state
4652 *
4653 * Returns 0 or -1 in case of error
4654 */
4655int
4656xmlAutomataSetFinalState(xmlAutomataPtr am, xmlAutomataStatePtr state) {
4657 if ((am == NULL) || (state == NULL))
4658 return(-1);
4659 state->type = XML_REGEXP_FINAL_STATE;
4660 return(0);
4661}
4662
4663/**
4664 * xmlAutomataNewTransition:
4665 * @am: an automata
4666 * @from: the starting point of the transition
4667 * @to: the target point of the transition or NULL
4668 * @token: the input string associated to that transition
4669 * @data: data passed to the callback function if the transition is activated
4670 *
William M. Brackddf71d62004-05-06 04:17:26 +00004671 * If @to is NULL, this creates first a new target state in the automata
Daniel Veillard4255d502002-04-16 15:50:10 +00004672 * and then adds a transition from the @from state to the target state
4673 * activated by the value of @token
4674 *
4675 * Returns the target state or NULL in case of error
4676 */
4677xmlAutomataStatePtr
4678xmlAutomataNewTransition(xmlAutomataPtr am, xmlAutomataStatePtr from,
4679 xmlAutomataStatePtr to, const xmlChar *token,
4680 void *data) {
4681 xmlRegAtomPtr atom;
4682
4683 if ((am == NULL) || (from == NULL) || (token == NULL))
4684 return(NULL);
4685 atom = xmlRegNewAtom(am, XML_REGEXP_STRING);
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00004686 if (atom == NULL)
4687 return(NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +00004688 atom->data = data;
4689 if (atom == NULL)
4690 return(NULL);
4691 atom->valuep = xmlStrdup(token);
4692
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00004693 if (xmlFAGenerateTransitions(am, from, to, atom) < 0) {
4694 xmlRegFreeAtom(atom);
4695 return(NULL);
4696 }
Daniel Veillard4255d502002-04-16 15:50:10 +00004697 if (to == NULL)
4698 return(am->state);
4699 return(to);
4700}
4701
4702/**
Daniel Veillard52b48c72003-04-13 19:53:42 +00004703 * xmlAutomataNewTransition2:
4704 * @am: an automata
4705 * @from: the starting point of the transition
4706 * @to: the target point of the transition or NULL
4707 * @token: the first input string associated to that transition
4708 * @token2: the second input string associated to that transition
4709 * @data: data passed to the callback function if the transition is activated
4710 *
William M. Brackddf71d62004-05-06 04:17:26 +00004711 * If @to is NULL, this creates first a new target state in the automata
Daniel Veillard52b48c72003-04-13 19:53:42 +00004712 * and then adds a transition from the @from state to the target state
4713 * activated by the value of @token
4714 *
4715 * Returns the target state or NULL in case of error
4716 */
4717xmlAutomataStatePtr
4718xmlAutomataNewTransition2(xmlAutomataPtr am, xmlAutomataStatePtr from,
4719 xmlAutomataStatePtr to, const xmlChar *token,
4720 const xmlChar *token2, void *data) {
4721 xmlRegAtomPtr atom;
4722
4723 if ((am == NULL) || (from == NULL) || (token == NULL))
4724 return(NULL);
4725 atom = xmlRegNewAtom(am, XML_REGEXP_STRING);
4726 atom->data = data;
4727 if (atom == NULL)
4728 return(NULL);
4729 if ((token2 == NULL) || (*token2 == 0)) {
4730 atom->valuep = xmlStrdup(token);
4731 } else {
4732 int lenn, lenp;
4733 xmlChar *str;
4734
4735 lenn = strlen((char *) token2);
4736 lenp = strlen((char *) token);
4737
Daniel Veillard3c908dc2003-04-19 00:07:51 +00004738 str = (xmlChar *) xmlMallocAtomic(lenn + lenp + 2);
Daniel Veillard52b48c72003-04-13 19:53:42 +00004739 if (str == NULL) {
4740 xmlRegFreeAtom(atom);
4741 return(NULL);
4742 }
4743 memcpy(&str[0], token, lenp);
4744 str[lenp] = '|';
4745 memcpy(&str[lenp + 1], token2, lenn);
4746 str[lenn + lenp + 1] = 0;
4747
4748 atom->valuep = str;
4749 }
4750
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00004751 if (xmlFAGenerateTransitions(am, from, to, atom) < 0) {
4752 xmlRegFreeAtom(atom);
4753 return(NULL);
4754 }
Daniel Veillard52b48c72003-04-13 19:53:42 +00004755 if (to == NULL)
4756 return(am->state);
4757 return(to);
4758}
4759
4760/**
Daniel Veillard9efc4762005-07-19 14:33:55 +00004761 * xmlAutomataNewNegTrans:
4762 * @am: an automata
4763 * @from: the starting point of the transition
4764 * @to: the target point of the transition or NULL
4765 * @token: the first input string associated to that transition
4766 * @token2: the second input string associated to that transition
4767 * @data: data passed to the callback function if the transition is activated
4768 *
4769 * If @to is NULL, this creates first a new target state in the automata
4770 * and then adds a transition from the @from state to the target state
4771 * activated by any value except (@token,@token2)
4772 *
4773 * Returns the target state or NULL in case of error
4774 */
4775xmlAutomataStatePtr
4776xmlAutomataNewNegTrans(xmlAutomataPtr am, xmlAutomataStatePtr from,
4777 xmlAutomataStatePtr to, const xmlChar *token,
4778 const xmlChar *token2, void *data) {
4779 xmlRegAtomPtr atom;
Daniel Veillard77005e62005-07-19 16:26:18 +00004780 xmlChar err_msg[200];
Daniel Veillard9efc4762005-07-19 14:33:55 +00004781
4782 if ((am == NULL) || (from == NULL) || (token == NULL))
4783 return(NULL);
4784 atom = xmlRegNewAtom(am, XML_REGEXP_STRING);
4785 if (atom == NULL)
4786 return(NULL);
4787 atom->data = data;
4788 atom->neg = 1;
4789 if ((token2 == NULL) || (*token2 == 0)) {
4790 atom->valuep = xmlStrdup(token);
4791 } else {
4792 int lenn, lenp;
4793 xmlChar *str;
4794
4795 lenn = strlen((char *) token2);
4796 lenp = strlen((char *) token);
4797
4798 str = (xmlChar *) xmlMallocAtomic(lenn + lenp + 2);
4799 if (str == NULL) {
4800 xmlRegFreeAtom(atom);
4801 return(NULL);
4802 }
4803 memcpy(&str[0], token, lenp);
4804 str[lenp] = '|';
4805 memcpy(&str[lenp + 1], token2, lenn);
4806 str[lenn + lenp + 1] = 0;
4807
4808 atom->valuep = str;
4809 }
Daniel Veillard77005e62005-07-19 16:26:18 +00004810 snprintf(err_msg, 199, "not %s", atom->valuep);
4811 err_msg[199] = 0;
4812 atom->valuep2 = xmlStrdup(err_msg);
Daniel Veillard9efc4762005-07-19 14:33:55 +00004813
4814 if (xmlFAGenerateTransitions(am, from, to, atom) < 0) {
4815 xmlRegFreeAtom(atom);
4816 return(NULL);
4817 }
4818 if (to == NULL)
4819 return(am->state);
4820 return(to);
4821}
4822
4823/**
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00004824 * xmlAutomataNewCountTrans2:
4825 * @am: an automata
4826 * @from: the starting point of the transition
4827 * @to: the target point of the transition or NULL
4828 * @token: the input string associated to that transition
4829 * @token2: the second input string associated to that transition
4830 * @min: the minimum successive occurences of token
4831 * @max: the maximum successive occurences of token
4832 * @data: data associated to the transition
4833 *
4834 * If @to is NULL, this creates first a new target state in the automata
4835 * and then adds a transition from the @from state to the target state
4836 * activated by a succession of input of value @token and @token2 and
4837 * whose number is between @min and @max
4838 *
4839 * Returns the target state or NULL in case of error
4840 */
4841xmlAutomataStatePtr
4842xmlAutomataNewCountTrans2(xmlAutomataPtr am, xmlAutomataStatePtr from,
4843 xmlAutomataStatePtr to, const xmlChar *token,
4844 const xmlChar *token2,
4845 int min, int max, void *data) {
4846 xmlRegAtomPtr atom;
4847 int counter;
4848
4849 if ((am == NULL) || (from == NULL) || (token == NULL))
4850 return(NULL);
4851 if (min < 0)
4852 return(NULL);
4853 if ((max < min) || (max < 1))
4854 return(NULL);
4855 atom = xmlRegNewAtom(am, XML_REGEXP_STRING);
4856 if (atom == NULL)
4857 return(NULL);
4858 if ((token2 == NULL) || (*token2 == 0)) {
4859 atom->valuep = xmlStrdup(token);
4860 } else {
4861 int lenn, lenp;
4862 xmlChar *str;
4863
4864 lenn = strlen((char *) token2);
4865 lenp = strlen((char *) token);
4866
4867 str = (xmlChar *) xmlMallocAtomic(lenn + lenp + 2);
4868 if (str == NULL) {
4869 xmlRegFreeAtom(atom);
4870 return(NULL);
4871 }
4872 memcpy(&str[0], token, lenp);
4873 str[lenp] = '|';
4874 memcpy(&str[lenp + 1], token2, lenn);
4875 str[lenn + lenp + 1] = 0;
4876
4877 atom->valuep = str;
4878 }
4879 atom->data = data;
4880 if (min == 0)
4881 atom->min = 1;
4882 else
4883 atom->min = min;
4884 atom->max = max;
4885
4886 /*
4887 * associate a counter to the transition.
4888 */
4889 counter = xmlRegGetCounter(am);
4890 am->counters[counter].min = min;
4891 am->counters[counter].max = max;
4892
4893 /* xmlFAGenerateTransitions(am, from, to, atom); */
4894 if (to == NULL) {
4895 to = xmlRegNewState(am);
4896 xmlRegStatePush(am, to);
4897 }
4898 xmlRegStateAddTrans(am, from, atom, to, counter, -1);
4899 xmlRegAtomPush(am, atom);
4900 am->state = to;
4901
4902 if (to == NULL)
4903 to = am->state;
4904 if (to == NULL)
4905 return(NULL);
4906 if (min == 0)
4907 xmlFAGenerateEpsilonTransition(am, from, to);
4908 return(to);
4909}
4910
4911/**
Daniel Veillard4255d502002-04-16 15:50:10 +00004912 * xmlAutomataNewCountTrans:
4913 * @am: an automata
4914 * @from: the starting point of the transition
4915 * @to: the target point of the transition or NULL
4916 * @token: the input string associated to that transition
4917 * @min: the minimum successive occurences of token
Daniel Veillarda9b66d02002-12-11 14:23:49 +00004918 * @max: the maximum successive occurences of token
4919 * @data: data associated to the transition
Daniel Veillard4255d502002-04-16 15:50:10 +00004920 *
William M. Brackddf71d62004-05-06 04:17:26 +00004921 * If @to is NULL, this creates first a new target state in the automata
Daniel Veillard4255d502002-04-16 15:50:10 +00004922 * and then adds a transition from the @from state to the target state
4923 * activated by a succession of input of value @token and whose number
4924 * is between @min and @max
4925 *
4926 * Returns the target state or NULL in case of error
4927 */
4928xmlAutomataStatePtr
4929xmlAutomataNewCountTrans(xmlAutomataPtr am, xmlAutomataStatePtr from,
4930 xmlAutomataStatePtr to, const xmlChar *token,
4931 int min, int max, void *data) {
4932 xmlRegAtomPtr atom;
Daniel Veillard0ddb21c2004-02-12 12:43:49 +00004933 int counter;
Daniel Veillard4255d502002-04-16 15:50:10 +00004934
4935 if ((am == NULL) || (from == NULL) || (token == NULL))
4936 return(NULL);
4937 if (min < 0)
4938 return(NULL);
4939 if ((max < min) || (max < 1))
4940 return(NULL);
4941 atom = xmlRegNewAtom(am, XML_REGEXP_STRING);
4942 if (atom == NULL)
4943 return(NULL);
4944 atom->valuep = xmlStrdup(token);
4945 atom->data = data;
4946 if (min == 0)
4947 atom->min = 1;
4948 else
4949 atom->min = min;
4950 atom->max = max;
4951
Daniel Veillard0ddb21c2004-02-12 12:43:49 +00004952 /*
4953 * associate a counter to the transition.
4954 */
4955 counter = xmlRegGetCounter(am);
4956 am->counters[counter].min = min;
4957 am->counters[counter].max = max;
4958
4959 /* xmlFAGenerateTransitions(am, from, to, atom); */
4960 if (to == NULL) {
4961 to = xmlRegNewState(am);
4962 xmlRegStatePush(am, to);
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00004963 }
Daniel Veillard0ddb21c2004-02-12 12:43:49 +00004964 xmlRegStateAddTrans(am, from, atom, to, counter, -1);
4965 xmlRegAtomPush(am, atom);
4966 am->state = to;
4967
Daniel Veillard4255d502002-04-16 15:50:10 +00004968 if (to == NULL)
4969 to = am->state;
4970 if (to == NULL)
4971 return(NULL);
4972 if (min == 0)
4973 xmlFAGenerateEpsilonTransition(am, from, to);
4974 return(to);
4975}
4976
4977/**
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00004978 * xmlAutomataNewOnceTrans2:
4979 * @am: an automata
4980 * @from: the starting point of the transition
4981 * @to: the target point of the transition or NULL
4982 * @token: the input string associated to that transition
4983 * @token2: the second input string associated to that transition
4984 * @min: the minimum successive occurences of token
4985 * @max: the maximum successive occurences of token
4986 * @data: data associated to the transition
4987 *
4988 * If @to is NULL, this creates first a new target state in the automata
4989 * and then adds a transition from the @from state to the target state
4990 * activated by a succession of input of value @token and @token2 and whose
4991 * number is between @min and @max, moreover that transition can only be
4992 * crossed once.
4993 *
4994 * Returns the target state or NULL in case of error
4995 */
4996xmlAutomataStatePtr
4997xmlAutomataNewOnceTrans2(xmlAutomataPtr am, xmlAutomataStatePtr from,
4998 xmlAutomataStatePtr to, const xmlChar *token,
4999 const xmlChar *token2,
5000 int min, int max, void *data) {
5001 xmlRegAtomPtr atom;
5002 int counter;
5003
5004 if ((am == NULL) || (from == NULL) || (token == NULL))
5005 return(NULL);
5006 if (min < 1)
5007 return(NULL);
5008 if ((max < min) || (max < 1))
5009 return(NULL);
5010 atom = xmlRegNewAtom(am, XML_REGEXP_STRING);
5011 if (atom == NULL)
5012 return(NULL);
5013 if ((token2 == NULL) || (*token2 == 0)) {
5014 atom->valuep = xmlStrdup(token);
5015 } else {
5016 int lenn, lenp;
5017 xmlChar *str;
5018
5019 lenn = strlen((char *) token2);
5020 lenp = strlen((char *) token);
5021
5022 str = (xmlChar *) xmlMallocAtomic(lenn + lenp + 2);
5023 if (str == NULL) {
5024 xmlRegFreeAtom(atom);
5025 return(NULL);
5026 }
5027 memcpy(&str[0], token, lenp);
5028 str[lenp] = '|';
5029 memcpy(&str[lenp + 1], token2, lenn);
5030 str[lenn + lenp + 1] = 0;
5031
5032 atom->valuep = str;
5033 }
5034 atom->data = data;
5035 atom->quant = XML_REGEXP_QUANT_ONCEONLY;
5036 if (min == 0)
5037 atom->min = 1;
5038 else
5039 atom->min = min;
5040 atom->max = max;
5041 /*
5042 * associate a counter to the transition.
5043 */
5044 counter = xmlRegGetCounter(am);
5045 am->counters[counter].min = 1;
5046 am->counters[counter].max = 1;
5047
5048 /* xmlFAGenerateTransitions(am, from, to, atom); */
5049 if (to == NULL) {
5050 to = xmlRegNewState(am);
5051 xmlRegStatePush(am, to);
5052 }
5053 xmlRegStateAddTrans(am, from, atom, to, counter, -1);
5054 xmlRegAtomPush(am, atom);
5055 am->state = to;
5056 return(to);
5057}
5058
5059
5060
5061/**
Daniel Veillard7646b182002-04-20 06:41:40 +00005062 * xmlAutomataNewOnceTrans:
5063 * @am: an automata
5064 * @from: the starting point of the transition
5065 * @to: the target point of the transition or NULL
5066 * @token: the input string associated to that transition
5067 * @min: the minimum successive occurences of token
Daniel Veillarda9b66d02002-12-11 14:23:49 +00005068 * @max: the maximum successive occurences of token
5069 * @data: data associated to the transition
Daniel Veillard7646b182002-04-20 06:41:40 +00005070 *
William M. Brackddf71d62004-05-06 04:17:26 +00005071 * If @to is NULL, this creates first a new target state in the automata
Daniel Veillard7646b182002-04-20 06:41:40 +00005072 * and then adds a transition from the @from state to the target state
5073 * activated by a succession of input of value @token and whose number
William M. Brackddf71d62004-05-06 04:17:26 +00005074 * is between @min and @max, moreover that transition can only be crossed
Daniel Veillard7646b182002-04-20 06:41:40 +00005075 * once.
5076 *
5077 * Returns the target state or NULL in case of error
5078 */
5079xmlAutomataStatePtr
5080xmlAutomataNewOnceTrans(xmlAutomataPtr am, xmlAutomataStatePtr from,
5081 xmlAutomataStatePtr to, const xmlChar *token,
5082 int min, int max, void *data) {
5083 xmlRegAtomPtr atom;
5084 int counter;
5085
5086 if ((am == NULL) || (from == NULL) || (token == NULL))
5087 return(NULL);
5088 if (min < 1)
5089 return(NULL);
5090 if ((max < min) || (max < 1))
5091 return(NULL);
5092 atom = xmlRegNewAtom(am, XML_REGEXP_STRING);
5093 if (atom == NULL)
5094 return(NULL);
5095 atom->valuep = xmlStrdup(token);
5096 atom->data = data;
5097 atom->quant = XML_REGEXP_QUANT_ONCEONLY;
5098 if (min == 0)
5099 atom->min = 1;
5100 else
5101 atom->min = min;
5102 atom->max = max;
5103 /*
5104 * associate a counter to the transition.
5105 */
5106 counter = xmlRegGetCounter(am);
5107 am->counters[counter].min = 1;
5108 am->counters[counter].max = 1;
5109
5110 /* xmlFAGenerateTransitions(am, from, to, atom); */
5111 if (to == NULL) {
5112 to = xmlRegNewState(am);
5113 xmlRegStatePush(am, to);
5114 }
5115 xmlRegStateAddTrans(am, from, atom, to, counter, -1);
5116 xmlRegAtomPush(am, atom);
5117 am->state = to;
Daniel Veillard7646b182002-04-20 06:41:40 +00005118 return(to);
5119}
5120
5121/**
Daniel Veillard4255d502002-04-16 15:50:10 +00005122 * xmlAutomataNewState:
5123 * @am: an automata
5124 *
5125 * Create a new disconnected state in the automata
5126 *
5127 * Returns the new state or NULL in case of error
5128 */
5129xmlAutomataStatePtr
5130xmlAutomataNewState(xmlAutomataPtr am) {
5131 xmlAutomataStatePtr to;
5132
5133 if (am == NULL)
5134 return(NULL);
5135 to = xmlRegNewState(am);
5136 xmlRegStatePush(am, to);
5137 return(to);
5138}
5139
5140/**
Daniel Veillarda9b66d02002-12-11 14:23:49 +00005141 * xmlAutomataNewEpsilon:
Daniel Veillard4255d502002-04-16 15:50:10 +00005142 * @am: an automata
5143 * @from: the starting point of the transition
5144 * @to: the target point of the transition or NULL
5145 *
William M. Brackddf71d62004-05-06 04:17:26 +00005146 * If @to is NULL, this creates first a new target state in the automata
5147 * and then adds an epsilon transition from the @from state to the
Daniel Veillard4255d502002-04-16 15:50:10 +00005148 * target state
5149 *
5150 * Returns the target state or NULL in case of error
5151 */
5152xmlAutomataStatePtr
5153xmlAutomataNewEpsilon(xmlAutomataPtr am, xmlAutomataStatePtr from,
5154 xmlAutomataStatePtr to) {
5155 if ((am == NULL) || (from == NULL))
5156 return(NULL);
5157 xmlFAGenerateEpsilonTransition(am, from, to);
5158 if (to == NULL)
5159 return(am->state);
5160 return(to);
5161}
5162
Daniel Veillardb509f152002-04-17 16:28:10 +00005163/**
Daniel Veillard7646b182002-04-20 06:41:40 +00005164 * xmlAutomataNewAllTrans:
5165 * @am: an automata
5166 * @from: the starting point of the transition
5167 * @to: the target point of the transition or NULL
Daniel Veillarda9b66d02002-12-11 14:23:49 +00005168 * @lax: allow to transition if not all all transitions have been activated
Daniel Veillard7646b182002-04-20 06:41:40 +00005169 *
William M. Brackddf71d62004-05-06 04:17:26 +00005170 * If @to is NULL, this creates first a new target state in the automata
Daniel Veillard7646b182002-04-20 06:41:40 +00005171 * and then adds a an ALL transition from the @from state to the
5172 * target state. That transition is an epsilon transition allowed only when
5173 * all transitions from the @from node have been activated.
5174 *
5175 * Returns the target state or NULL in case of error
5176 */
5177xmlAutomataStatePtr
5178xmlAutomataNewAllTrans(xmlAutomataPtr am, xmlAutomataStatePtr from,
Daniel Veillard441bc322002-04-20 17:38:48 +00005179 xmlAutomataStatePtr to, int lax) {
Daniel Veillard7646b182002-04-20 06:41:40 +00005180 if ((am == NULL) || (from == NULL))
5181 return(NULL);
Daniel Veillard441bc322002-04-20 17:38:48 +00005182 xmlFAGenerateAllTransition(am, from, to, lax);
Daniel Veillard7646b182002-04-20 06:41:40 +00005183 if (to == NULL)
5184 return(am->state);
5185 return(to);
5186}
5187
5188/**
Daniel Veillardb509f152002-04-17 16:28:10 +00005189 * xmlAutomataNewCounter:
5190 * @am: an automata
5191 * @min: the minimal value on the counter
5192 * @max: the maximal value on the counter
5193 *
5194 * Create a new counter
5195 *
5196 * Returns the counter number or -1 in case of error
5197 */
5198int
5199xmlAutomataNewCounter(xmlAutomataPtr am, int min, int max) {
5200 int ret;
5201
5202 if (am == NULL)
5203 return(-1);
5204
5205 ret = xmlRegGetCounter(am);
5206 if (ret < 0)
5207 return(-1);
5208 am->counters[ret].min = min;
5209 am->counters[ret].max = max;
5210 return(ret);
5211}
5212
5213/**
5214 * xmlAutomataNewCountedTrans:
5215 * @am: an automata
5216 * @from: the starting point of the transition
5217 * @to: the target point of the transition or NULL
5218 * @counter: the counter associated to that transition
5219 *
William M. Brackddf71d62004-05-06 04:17:26 +00005220 * If @to is NULL, this creates first a new target state in the automata
Daniel Veillardb509f152002-04-17 16:28:10 +00005221 * and then adds an epsilon transition from the @from state to the target state
5222 * which will increment the counter provided
5223 *
5224 * Returns the target state or NULL in case of error
5225 */
5226xmlAutomataStatePtr
5227xmlAutomataNewCountedTrans(xmlAutomataPtr am, xmlAutomataStatePtr from,
5228 xmlAutomataStatePtr to, int counter) {
5229 if ((am == NULL) || (from == NULL) || (counter < 0))
5230 return(NULL);
5231 xmlFAGenerateCountedEpsilonTransition(am, from, to, counter);
5232 if (to == NULL)
5233 return(am->state);
5234 return(to);
5235}
5236
5237/**
5238 * xmlAutomataNewCounterTrans:
5239 * @am: an automata
5240 * @from: the starting point of the transition
5241 * @to: the target point of the transition or NULL
5242 * @counter: the counter associated to that transition
5243 *
William M. Brackddf71d62004-05-06 04:17:26 +00005244 * If @to is NULL, this creates first a new target state in the automata
Daniel Veillardb509f152002-04-17 16:28:10 +00005245 * and then adds an epsilon transition from the @from state to the target state
5246 * which will be allowed only if the counter is within the right range.
5247 *
5248 * Returns the target state or NULL in case of error
5249 */
5250xmlAutomataStatePtr
5251xmlAutomataNewCounterTrans(xmlAutomataPtr am, xmlAutomataStatePtr from,
5252 xmlAutomataStatePtr to, int counter) {
5253 if ((am == NULL) || (from == NULL) || (counter < 0))
5254 return(NULL);
5255 xmlFAGenerateCountedTransition(am, from, to, counter);
5256 if (to == NULL)
5257 return(am->state);
5258 return(to);
5259}
Daniel Veillard4255d502002-04-16 15:50:10 +00005260
5261/**
5262 * xmlAutomataCompile:
5263 * @am: an automata
5264 *
5265 * Compile the automata into a Reg Exp ready for being executed.
5266 * The automata should be free after this point.
5267 *
5268 * Returns the compiled regexp or NULL in case of error
5269 */
5270xmlRegexpPtr
5271xmlAutomataCompile(xmlAutomataPtr am) {
5272 xmlRegexpPtr ret;
5273
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00005274 if ((am == NULL) || (am->error != 0)) return(NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +00005275 xmlFAEliminateEpsilonTransitions(am);
Daniel Veillard23e73572002-09-19 19:56:43 +00005276 /* xmlFAComputesDeterminism(am); */
Daniel Veillard4255d502002-04-16 15:50:10 +00005277 ret = xmlRegEpxFromParse(am);
5278
5279 return(ret);
5280}
Daniel Veillarde19fc232002-04-22 16:01:24 +00005281
5282/**
5283 * xmlAutomataIsDeterminist:
5284 * @am: an automata
5285 *
5286 * Checks if an automata is determinist.
5287 *
5288 * Returns 1 if true, 0 if not, and -1 in case of error
5289 */
5290int
5291xmlAutomataIsDeterminist(xmlAutomataPtr am) {
5292 int ret;
5293
5294 if (am == NULL)
5295 return(-1);
5296
5297 ret = xmlFAComputesDeterminism(am);
5298 return(ret);
5299}
Daniel Veillard4255d502002-04-16 15:50:10 +00005300#endif /* LIBXML_AUTOMATA_ENABLED */
Daniel Veillard5d4644e2005-04-01 13:11:58 +00005301#define bottom_xmlregexp
5302#include "elfgcchack.h"
Daniel Veillard4255d502002-04-16 15:50:10 +00005303#endif /* LIBXML_REGEXP_ENABLED */