blob: d1e6f38d2c0cd4b5f2b2af218130f4d3b317762e [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
6 * XML related specifications thise includes:
7 * - 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
22#include <stdio.h>
23#include <string.h>
Daniel Veillardebe48c62003-12-03 12:12:27 +000024#ifdef HAVE_LIMITS_H
25#include <limits.h>
26#endif
27
Daniel Veillard4255d502002-04-16 15:50:10 +000028#include <libxml/tree.h>
29#include <libxml/parserInternals.h>
30#include <libxml/xmlregexp.h>
31#include <libxml/xmlautomata.h>
32#include <libxml/xmlunicode.h>
33
Daniel Veillardebe48c62003-12-03 12:12:27 +000034#ifndef INT_MAX
35#define INT_MAX 123456789 /* easy to flag and big enough for our needs */
36#endif
37
Daniel Veillard4255d502002-04-16 15:50:10 +000038/* #define DEBUG_REGEXP_GRAPH */
39/* #define DEBUG_REGEXP_EXEC */
40/* #define DEBUG_PUSH */
Daniel Veillard23e73572002-09-19 19:56:43 +000041/* #define DEBUG_COMPACTION */
Daniel Veillard4255d502002-04-16 15:50:10 +000042
Daniel Veillardff46a042003-10-08 08:53:17 +000043#define ERROR(str) \
44 ctxt->error = XML_REGEXP_COMPILE_ERROR; \
45 xmlRegexpErrCompile(ctxt, str);
Daniel Veillard4255d502002-04-16 15:50:10 +000046#define NEXT ctxt->cur++
47#define CUR (*(ctxt->cur))
48#define NXT(index) (ctxt->cur[index])
49
50#define CUR_SCHAR(s, l) xmlStringCurrentChar(NULL, s, &l)
51#define NEXTL(l) ctxt->cur += l;
52
Daniel Veillarde19fc232002-04-22 16:01:24 +000053/**
54 * TODO:
55 *
56 * macro to flag unimplemented blocks
57 */
58#define TODO \
59 xmlGenericError(xmlGenericErrorContext, \
60 "Unimplemented block at %s:%d\n", \
61 __FILE__, __LINE__);
62
Daniel Veillard4255d502002-04-16 15:50:10 +000063/************************************************************************
64 * *
65 * Datatypes and structures *
66 * *
67 ************************************************************************/
68
69typedef enum {
70 XML_REGEXP_EPSILON = 1,
71 XML_REGEXP_CHARVAL,
72 XML_REGEXP_RANGES,
73 XML_REGEXP_SUBREG,
74 XML_REGEXP_STRING,
75 XML_REGEXP_ANYCHAR, /* . */
76 XML_REGEXP_ANYSPACE, /* \s */
77 XML_REGEXP_NOTSPACE, /* \S */
78 XML_REGEXP_INITNAME, /* \l */
79 XML_REGEXP_NOTINITNAME, /* \l */
80 XML_REGEXP_NAMECHAR, /* \c */
81 XML_REGEXP_NOTNAMECHAR, /* \C */
82 XML_REGEXP_DECIMAL, /* \d */
83 XML_REGEXP_NOTDECIMAL, /* \d */
84 XML_REGEXP_REALCHAR, /* \w */
85 XML_REGEXP_NOTREALCHAR, /* \w */
86 XML_REGEXP_LETTER,
87 XML_REGEXP_LETTER_UPPERCASE,
88 XML_REGEXP_LETTER_LOWERCASE,
89 XML_REGEXP_LETTER_TITLECASE,
90 XML_REGEXP_LETTER_MODIFIER,
91 XML_REGEXP_LETTER_OTHERS,
92 XML_REGEXP_MARK,
93 XML_REGEXP_MARK_NONSPACING,
94 XML_REGEXP_MARK_SPACECOMBINING,
95 XML_REGEXP_MARK_ENCLOSING,
96 XML_REGEXP_NUMBER,
97 XML_REGEXP_NUMBER_DECIMAL,
98 XML_REGEXP_NUMBER_LETTER,
99 XML_REGEXP_NUMBER_OTHERS,
100 XML_REGEXP_PUNCT,
101 XML_REGEXP_PUNCT_CONNECTOR,
102 XML_REGEXP_PUNCT_DASH,
103 XML_REGEXP_PUNCT_OPEN,
104 XML_REGEXP_PUNCT_CLOSE,
105 XML_REGEXP_PUNCT_INITQUOTE,
106 XML_REGEXP_PUNCT_FINQUOTE,
107 XML_REGEXP_PUNCT_OTHERS,
108 XML_REGEXP_SEPAR,
109 XML_REGEXP_SEPAR_SPACE,
110 XML_REGEXP_SEPAR_LINE,
111 XML_REGEXP_SEPAR_PARA,
112 XML_REGEXP_SYMBOL,
113 XML_REGEXP_SYMBOL_MATH,
114 XML_REGEXP_SYMBOL_CURRENCY,
115 XML_REGEXP_SYMBOL_MODIFIER,
116 XML_REGEXP_SYMBOL_OTHERS,
117 XML_REGEXP_OTHER,
118 XML_REGEXP_OTHER_CONTROL,
119 XML_REGEXP_OTHER_FORMAT,
120 XML_REGEXP_OTHER_PRIVATE,
121 XML_REGEXP_OTHER_NA,
122 XML_REGEXP_BLOCK_NAME
123} xmlRegAtomType;
124
125typedef enum {
126 XML_REGEXP_QUANT_EPSILON = 1,
127 XML_REGEXP_QUANT_ONCE,
128 XML_REGEXP_QUANT_OPT,
129 XML_REGEXP_QUANT_MULT,
130 XML_REGEXP_QUANT_PLUS,
Daniel Veillard7646b182002-04-20 06:41:40 +0000131 XML_REGEXP_QUANT_ONCEONLY,
132 XML_REGEXP_QUANT_ALL,
Daniel Veillard4255d502002-04-16 15:50:10 +0000133 XML_REGEXP_QUANT_RANGE
134} xmlRegQuantType;
135
136typedef enum {
137 XML_REGEXP_START_STATE = 1,
138 XML_REGEXP_FINAL_STATE,
139 XML_REGEXP_TRANS_STATE
140} xmlRegStateType;
141
142typedef enum {
143 XML_REGEXP_MARK_NORMAL = 0,
144 XML_REGEXP_MARK_START,
145 XML_REGEXP_MARK_VISITED
146} xmlRegMarkedType;
147
148typedef struct _xmlRegRange xmlRegRange;
149typedef xmlRegRange *xmlRegRangePtr;
150
151struct _xmlRegRange {
Daniel Veillardf8b9de32003-11-24 14:27:26 +0000152 int neg; /* 0 normal, 1 not, 2 exclude */
Daniel Veillard4255d502002-04-16 15:50:10 +0000153 xmlRegAtomType type;
154 int start;
155 int end;
156 xmlChar *blockName;
157};
158
159typedef struct _xmlRegAtom xmlRegAtom;
160typedef xmlRegAtom *xmlRegAtomPtr;
161
162typedef struct _xmlAutomataState xmlRegState;
163typedef xmlRegState *xmlRegStatePtr;
164
165struct _xmlRegAtom {
166 int no;
167 xmlRegAtomType type;
168 xmlRegQuantType quant;
169 int min;
170 int max;
171
172 void *valuep;
Daniel Veillarda646cfd2002-09-17 21:50:03 +0000173 void *valuep2;
Daniel Veillard4255d502002-04-16 15:50:10 +0000174 int neg;
175 int codepoint;
176 xmlRegStatePtr start;
177 xmlRegStatePtr stop;
178 int maxRanges;
179 int nbRanges;
180 xmlRegRangePtr *ranges;
181 void *data;
182};
183
184typedef struct _xmlRegCounter xmlRegCounter;
185typedef xmlRegCounter *xmlRegCounterPtr;
186
187struct _xmlRegCounter {
188 int min;
189 int max;
190};
191
192typedef struct _xmlRegTrans xmlRegTrans;
193typedef xmlRegTrans *xmlRegTransPtr;
194
195struct _xmlRegTrans {
196 xmlRegAtomPtr atom;
197 int to;
198 int counter;
199 int count;
200};
201
202struct _xmlAutomataState {
203 xmlRegStateType type;
204 xmlRegMarkedType mark;
Daniel Veillard23e73572002-09-19 19:56:43 +0000205 xmlRegMarkedType reached;
Daniel Veillard4255d502002-04-16 15:50:10 +0000206 int no;
207
208 int maxTrans;
209 int nbTrans;
210 xmlRegTrans *trans;
211};
212
213typedef struct _xmlAutomata xmlRegParserCtxt;
214typedef xmlRegParserCtxt *xmlRegParserCtxtPtr;
215
216struct _xmlAutomata {
217 xmlChar *string;
218 xmlChar *cur;
219
220 int error;
221 int neg;
222
223 xmlRegStatePtr start;
224 xmlRegStatePtr end;
225 xmlRegStatePtr state;
226
227 xmlRegAtomPtr atom;
228
229 int maxAtoms;
230 int nbAtoms;
231 xmlRegAtomPtr *atoms;
232
233 int maxStates;
234 int nbStates;
235 xmlRegStatePtr *states;
236
237 int maxCounters;
238 int nbCounters;
239 xmlRegCounter *counters;
Daniel Veillarde19fc232002-04-22 16:01:24 +0000240
241 int determinist;
Daniel Veillard4255d502002-04-16 15:50:10 +0000242};
243
244struct _xmlRegexp {
245 xmlChar *string;
246 int nbStates;
247 xmlRegStatePtr *states;
248 int nbAtoms;
249 xmlRegAtomPtr *atoms;
250 int nbCounters;
251 xmlRegCounter *counters;
Daniel Veillarde19fc232002-04-22 16:01:24 +0000252 int determinist;
Daniel Veillard23e73572002-09-19 19:56:43 +0000253 /*
254 * That's the compact form for determinists automatas
255 */
256 int nbstates;
257 int *compact;
Daniel Veillard118aed72002-09-24 14:13:13 +0000258 void **transdata;
Daniel Veillard23e73572002-09-19 19:56:43 +0000259 int nbstrings;
260 xmlChar **stringMap;
Daniel Veillard4255d502002-04-16 15:50:10 +0000261};
262
263typedef struct _xmlRegExecRollback xmlRegExecRollback;
264typedef xmlRegExecRollback *xmlRegExecRollbackPtr;
265
266struct _xmlRegExecRollback {
267 xmlRegStatePtr state;/* the current state */
268 int index; /* the index in the input stack */
269 int nextbranch; /* the next transition to explore in that state */
270 int *counts; /* save the automate state if it has some */
271};
272
273typedef struct _xmlRegInputToken xmlRegInputToken;
274typedef xmlRegInputToken *xmlRegInputTokenPtr;
275
276struct _xmlRegInputToken {
277 xmlChar *value;
278 void *data;
279};
280
281struct _xmlRegExecCtxt {
282 int status; /* execution status != 0 indicate an error */
283 int determinist; /* did we found an inderterministic behaviour */
284 xmlRegexpPtr comp; /* the compiled regexp */
285 xmlRegExecCallbacks callback;
286 void *data;
287
288 xmlRegStatePtr state;/* the current state */
289 int transno; /* the current transition on that state */
290 int transcount; /* the number of char in char counted transitions */
291
292 /*
293 * A stack of rollback states
294 */
295 int maxRollbacks;
296 int nbRollbacks;
297 xmlRegExecRollback *rollbacks;
298
299 /*
300 * The state of the automata if any
301 */
302 int *counts;
303
304 /*
305 * The input stack
306 */
307 int inputStackMax;
308 int inputStackNr;
309 int index;
310 int *charStack;
311 const xmlChar *inputString; /* when operating on characters */
312 xmlRegInputTokenPtr inputStack;/* when operating on strings */
313
314};
315
Daniel Veillard441bc322002-04-20 17:38:48 +0000316#define REGEXP_ALL_COUNTER 0x123456
317#define REGEXP_ALL_LAX_COUNTER 0x123457
Daniel Veillard7646b182002-04-20 06:41:40 +0000318
Daniel Veillard4255d502002-04-16 15:50:10 +0000319static void xmlFAParseRegExp(xmlRegParserCtxtPtr ctxt, int top);
Daniel Veillard23e73572002-09-19 19:56:43 +0000320static void xmlRegFreeState(xmlRegStatePtr state);
321static void xmlRegFreeAtom(xmlRegAtomPtr atom);
Daniel Veillard4255d502002-04-16 15:50:10 +0000322
323/************************************************************************
Daniel Veillardff46a042003-10-08 08:53:17 +0000324 * *
325 * Regexp memory error handler *
326 * *
327 ************************************************************************/
328/**
329 * xmlRegexpErrMemory:
330 * @extra: extra informations
331 *
332 * Handle an out of memory condition
333 */
334static void
335xmlRegexpErrMemory(xmlRegParserCtxtPtr ctxt, const char *extra)
336{
337 const char *regexp = NULL;
338 if (ctxt != NULL) {
339 regexp = (const char *) ctxt->string;
340 ctxt->error = XML_ERR_NO_MEMORY;
341 }
Daniel Veillard659e71e2003-10-10 14:10:40 +0000342 __xmlRaiseError(NULL, NULL, NULL, NULL, NULL, XML_FROM_REGEXP,
Daniel Veillardff46a042003-10-08 08:53:17 +0000343 XML_ERR_NO_MEMORY, XML_ERR_FATAL, NULL, 0, extra,
344 regexp, NULL, 0, 0,
345 "Memory allocation failed : %s\n", extra);
346}
347
348/**
349 * xmlRegexpErrCompile:
350 * @extra: extra informations
351 *
352 * Handle an compilation failure
353 */
354static void
355xmlRegexpErrCompile(xmlRegParserCtxtPtr ctxt, const char *extra)
356{
357 const char *regexp = NULL;
358 int idx = 0;
359
360 if (ctxt != NULL) {
361 regexp = (const char *) ctxt->string;
362 idx = ctxt->cur - ctxt->string;
363 ctxt->error = XML_REGEXP_COMPILE_ERROR;
364 }
Daniel Veillard659e71e2003-10-10 14:10:40 +0000365 __xmlRaiseError(NULL, NULL, NULL, NULL, NULL, XML_FROM_REGEXP,
Daniel Veillardff46a042003-10-08 08:53:17 +0000366 XML_REGEXP_COMPILE_ERROR, XML_ERR_FATAL, NULL, 0, extra,
367 regexp, NULL, idx, 0,
368 "failed to compile: %s\n", extra);
369}
370
371/************************************************************************
Daniel Veillard4255d502002-04-16 15:50:10 +0000372 * *
373 * Allocation/Deallocation *
374 * *
375 ************************************************************************/
376
Daniel Veillard23e73572002-09-19 19:56:43 +0000377static int xmlFAComputesDeterminism(xmlRegParserCtxtPtr ctxt);
Daniel Veillard4255d502002-04-16 15:50:10 +0000378/**
379 * xmlRegEpxFromParse:
380 * @ctxt: the parser context used to build it
381 *
382 * Allocate a new regexp and fill it with the reult from the parser
383 *
384 * Returns the new regexp or NULL in case of error
385 */
386static xmlRegexpPtr
387xmlRegEpxFromParse(xmlRegParserCtxtPtr ctxt) {
388 xmlRegexpPtr ret;
389
390 ret = (xmlRegexpPtr) xmlMalloc(sizeof(xmlRegexp));
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000391 if (ret == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +0000392 xmlRegexpErrMemory(ctxt, "compiling regexp");
Daniel Veillard4255d502002-04-16 15:50:10 +0000393 return(NULL);
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000394 }
Daniel Veillard4255d502002-04-16 15:50:10 +0000395 memset(ret, 0, sizeof(xmlRegexp));
396 ret->string = ctxt->string;
Daniel Veillard4255d502002-04-16 15:50:10 +0000397 ret->nbStates = ctxt->nbStates;
Daniel Veillard4255d502002-04-16 15:50:10 +0000398 ret->states = ctxt->states;
Daniel Veillard4255d502002-04-16 15:50:10 +0000399 ret->nbAtoms = ctxt->nbAtoms;
Daniel Veillard4255d502002-04-16 15:50:10 +0000400 ret->atoms = ctxt->atoms;
Daniel Veillard4255d502002-04-16 15:50:10 +0000401 ret->nbCounters = ctxt->nbCounters;
Daniel Veillard4255d502002-04-16 15:50:10 +0000402 ret->counters = ctxt->counters;
Daniel Veillarde19fc232002-04-22 16:01:24 +0000403 ret->determinist = ctxt->determinist;
Daniel Veillard23e73572002-09-19 19:56:43 +0000404
405 if ((ret->determinist != 0) &&
406 (ret->nbCounters == 0) &&
Daniel Veillard118aed72002-09-24 14:13:13 +0000407 (ret->atoms != NULL) &&
Daniel Veillard23e73572002-09-19 19:56:43 +0000408 (ret->atoms[0] != NULL) &&
409 (ret->atoms[0]->type == XML_REGEXP_STRING)) {
410 int i, j, nbstates = 0, nbatoms = 0;
411 int *stateRemap;
412 int *stringRemap;
413 int *transitions;
Daniel Veillard118aed72002-09-24 14:13:13 +0000414 void **transdata;
Daniel Veillard23e73572002-09-19 19:56:43 +0000415 xmlChar **stringMap;
416 xmlChar *value;
417
418 /*
419 * Switch to a compact representation
420 * 1/ counting the effective number of states left
421 * 2/ conting the unique number of atoms, and check that
422 * they are all of the string type
423 * 3/ build a table state x atom for the transitions
424 */
425
426 stateRemap = xmlMalloc(ret->nbStates * sizeof(int));
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000427 if (stateRemap == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +0000428 xmlRegexpErrMemory(ctxt, "compiling regexp");
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000429 xmlFree(ret);
430 return(NULL);
431 }
Daniel Veillard23e73572002-09-19 19:56:43 +0000432 for (i = 0;i < ret->nbStates;i++) {
433 if (ret->states[i] != NULL) {
434 stateRemap[i] = nbstates;
435 nbstates++;
436 } else {
437 stateRemap[i] = -1;
438 }
439 }
440#ifdef DEBUG_COMPACTION
441 printf("Final: %d states\n", nbstates);
442#endif
443 stringMap = xmlMalloc(ret->nbAtoms * sizeof(char *));
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000444 if (stringMap == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +0000445 xmlRegexpErrMemory(ctxt, "compiling regexp");
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000446 xmlFree(stateRemap);
447 xmlFree(ret);
448 return(NULL);
449 }
Daniel Veillard23e73572002-09-19 19:56:43 +0000450 stringRemap = xmlMalloc(ret->nbAtoms * sizeof(int));
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000451 if (stringRemap == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +0000452 xmlRegexpErrMemory(ctxt, "compiling regexp");
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000453 xmlFree(stringMap);
454 xmlFree(stateRemap);
455 xmlFree(ret);
456 return(NULL);
457 }
Daniel Veillard23e73572002-09-19 19:56:43 +0000458 for (i = 0;i < ret->nbAtoms;i++) {
459 if ((ret->atoms[i]->type == XML_REGEXP_STRING) &&
460 (ret->atoms[i]->quant == XML_REGEXP_QUANT_ONCE)) {
461 value = ret->atoms[i]->valuep;
462 for (j = 0;j < nbatoms;j++) {
463 if (xmlStrEqual(stringMap[j], value)) {
464 stringRemap[i] = j;
465 break;
466 }
467 }
468 if (j >= nbatoms) {
469 stringRemap[i] = nbatoms;
470 stringMap[nbatoms] = xmlStrdup(value);
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000471 if (stringMap[nbatoms] == NULL) {
472 for (i = 0;i < nbatoms;i++)
473 xmlFree(stringMap[i]);
474 xmlFree(stringRemap);
475 xmlFree(stringMap);
476 xmlFree(stateRemap);
477 xmlFree(ret);
478 return(NULL);
479 }
Daniel Veillard23e73572002-09-19 19:56:43 +0000480 nbatoms++;
481 }
482 } else {
483 xmlFree(stateRemap);
484 xmlFree(stringRemap);
485 for (i = 0;i < nbatoms;i++)
486 xmlFree(stringMap[i]);
487 xmlFree(stringMap);
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000488 xmlFree(ret);
489 return(NULL);
Daniel Veillard23e73572002-09-19 19:56:43 +0000490 }
491 }
492#ifdef DEBUG_COMPACTION
493 printf("Final: %d atoms\n", nbatoms);
494#endif
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000495 transitions = (int *) xmlMalloc((nbstates + 1) *
496 (nbatoms + 1) * sizeof(int));
497 if (transitions == NULL) {
498 xmlFree(stateRemap);
499 xmlFree(stringRemap);
500 xmlFree(stringMap);
501 xmlFree(ret);
502 return(NULL);
503 }
504 memset(transitions, 0, (nbstates + 1) * (nbatoms + 1) * sizeof(int));
Daniel Veillard23e73572002-09-19 19:56:43 +0000505
506 /*
507 * Allocate the transition table. The first entry for each
508 * state correspond to the state type.
509 */
Daniel Veillard118aed72002-09-24 14:13:13 +0000510 transdata = NULL;
Daniel Veillard23e73572002-09-19 19:56:43 +0000511
512 for (i = 0;i < ret->nbStates;i++) {
513 int stateno, atomno, targetno, prev;
514 xmlRegStatePtr state;
515 xmlRegTransPtr trans;
516
517 stateno = stateRemap[i];
518 if (stateno == -1)
519 continue;
520 state = ret->states[i];
521
522 transitions[stateno * (nbatoms + 1)] = state->type;
523
524 for (j = 0;j < state->nbTrans;j++) {
525 trans = &(state->trans[j]);
526 if ((trans->to == -1) || (trans->atom == NULL))
527 continue;
528 atomno = stringRemap[trans->atom->no];
Daniel Veillard118aed72002-09-24 14:13:13 +0000529 if ((trans->atom->data != NULL) && (transdata == NULL)) {
530 transdata = (void **) xmlMalloc(nbstates * nbatoms *
531 sizeof(void *));
532 if (transdata != NULL)
533 memset(transdata, 0,
534 nbstates * nbatoms * sizeof(void *));
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000535 else {
Daniel Veillardff46a042003-10-08 08:53:17 +0000536 xmlRegexpErrMemory(ctxt, "compiling regexp");
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000537 break;
538 }
Daniel Veillard118aed72002-09-24 14:13:13 +0000539 }
Daniel Veillard23e73572002-09-19 19:56:43 +0000540 targetno = stateRemap[trans->to];
541 /*
542 * if the same atome can generate transition to 2 different
543 * states then it means the automata is not determinist and
544 * the compact form can't be used !
545 */
546 prev = transitions[stateno * (nbatoms + 1) + atomno + 1];
547 if (prev != 0) {
548 if (prev != targetno + 1) {
Daniel Veillard23e73572002-09-19 19:56:43 +0000549 ret->determinist = 0;
550#ifdef DEBUG_COMPACTION
551 printf("Indet: state %d trans %d, atom %d to %d : %d to %d\n",
552 i, j, trans->atom->no, trans->to, atomno, targetno);
553 printf(" previous to is %d\n", prev);
554#endif
555 ret->determinist = 0;
Daniel Veillard118aed72002-09-24 14:13:13 +0000556 if (transdata != NULL)
557 xmlFree(transdata);
Daniel Veillard23e73572002-09-19 19:56:43 +0000558 xmlFree(transitions);
559 xmlFree(stateRemap);
560 xmlFree(stringRemap);
561 for (i = 0;i < nbatoms;i++)
562 xmlFree(stringMap[i]);
563 xmlFree(stringMap);
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000564 goto not_determ;
Daniel Veillard23e73572002-09-19 19:56:43 +0000565 }
566 } else {
567#if 0
568 printf("State %d trans %d: atom %d to %d : %d to %d\n",
569 i, j, trans->atom->no, trans->to, atomno, targetno);
570#endif
571 transitions[stateno * (nbatoms + 1) + atomno + 1] =
Daniel Veillard118aed72002-09-24 14:13:13 +0000572 targetno + 1; /* to avoid 0 */
573 if (transdata != NULL)
574 transdata[stateno * nbatoms + atomno] =
575 trans->atom->data;
Daniel Veillard23e73572002-09-19 19:56:43 +0000576 }
577 }
578 }
579 ret->determinist = 1;
580#ifdef DEBUG_COMPACTION
581 /*
582 * Debug
583 */
584 for (i = 0;i < nbstates;i++) {
585 for (j = 0;j < nbatoms + 1;j++) {
586 printf("%02d ", transitions[i * (nbatoms + 1) + j]);
587 }
588 printf("\n");
589 }
590 printf("\n");
591#endif
592 /*
593 * Cleanup of the old data
594 */
595 if (ret->states != NULL) {
596 for (i = 0;i < ret->nbStates;i++)
597 xmlRegFreeState(ret->states[i]);
598 xmlFree(ret->states);
599 }
600 ret->states = NULL;
601 ret->nbStates = 0;
602 if (ret->atoms != NULL) {
603 for (i = 0;i < ret->nbAtoms;i++)
604 xmlRegFreeAtom(ret->atoms[i]);
605 xmlFree(ret->atoms);
606 }
607 ret->atoms = NULL;
608 ret->nbAtoms = 0;
609
610 ret->compact = transitions;
Daniel Veillard118aed72002-09-24 14:13:13 +0000611 ret->transdata = transdata;
Daniel Veillard23e73572002-09-19 19:56:43 +0000612 ret->stringMap = stringMap;
613 ret->nbstrings = nbatoms;
614 ret->nbstates = nbstates;
615 xmlFree(stateRemap);
616 xmlFree(stringRemap);
617 }
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000618not_determ:
619 ctxt->string = NULL;
620 ctxt->nbStates = 0;
621 ctxt->states = NULL;
622 ctxt->nbAtoms = 0;
623 ctxt->atoms = NULL;
624 ctxt->nbCounters = 0;
625 ctxt->counters = NULL;
Daniel Veillard4255d502002-04-16 15:50:10 +0000626 return(ret);
627}
628
629/**
630 * xmlRegNewParserCtxt:
631 * @string: the string to parse
632 *
633 * Allocate a new regexp parser context
634 *
635 * Returns the new context or NULL in case of error
636 */
637static xmlRegParserCtxtPtr
638xmlRegNewParserCtxt(const xmlChar *string) {
639 xmlRegParserCtxtPtr ret;
640
641 ret = (xmlRegParserCtxtPtr) xmlMalloc(sizeof(xmlRegParserCtxt));
642 if (ret == NULL)
643 return(NULL);
644 memset(ret, 0, sizeof(xmlRegParserCtxt));
645 if (string != NULL)
646 ret->string = xmlStrdup(string);
647 ret->cur = ret->string;
648 ret->neg = 0;
649 ret->error = 0;
Daniel Veillarde19fc232002-04-22 16:01:24 +0000650 ret->determinist = -1;
Daniel Veillard4255d502002-04-16 15:50:10 +0000651 return(ret);
652}
653
654/**
655 * xmlRegNewRange:
656 * @ctxt: the regexp parser context
657 * @neg: is that negative
658 * @type: the type of range
659 * @start: the start codepoint
660 * @end: the end codepoint
661 *
662 * Allocate a new regexp range
663 *
664 * Returns the new range or NULL in case of error
665 */
666static xmlRegRangePtr
667xmlRegNewRange(xmlRegParserCtxtPtr ctxt,
668 int neg, xmlRegAtomType type, int start, int end) {
669 xmlRegRangePtr ret;
670
671 ret = (xmlRegRangePtr) xmlMalloc(sizeof(xmlRegRange));
672 if (ret == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +0000673 xmlRegexpErrMemory(ctxt, "allocating range");
Daniel Veillard4255d502002-04-16 15:50:10 +0000674 return(NULL);
675 }
676 ret->neg = neg;
677 ret->type = type;
678 ret->start = start;
679 ret->end = end;
680 return(ret);
681}
682
683/**
684 * xmlRegFreeRange:
685 * @range: the regexp range
686 *
687 * Free a regexp range
688 */
689static void
690xmlRegFreeRange(xmlRegRangePtr range) {
691 if (range == NULL)
692 return;
693
694 if (range->blockName != NULL)
695 xmlFree(range->blockName);
696 xmlFree(range);
697}
698
699/**
700 * xmlRegNewAtom:
701 * @ctxt: the regexp parser context
702 * @type: the type of atom
703 *
704 * Allocate a new regexp range
705 *
706 * Returns the new atom or NULL in case of error
707 */
708static xmlRegAtomPtr
709xmlRegNewAtom(xmlRegParserCtxtPtr ctxt, xmlRegAtomType type) {
710 xmlRegAtomPtr ret;
711
712 ret = (xmlRegAtomPtr) xmlMalloc(sizeof(xmlRegAtom));
713 if (ret == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +0000714 xmlRegexpErrMemory(ctxt, "allocating atom");
Daniel Veillard4255d502002-04-16 15:50:10 +0000715 return(NULL);
716 }
717 memset(ret, 0, sizeof(xmlRegAtom));
718 ret->type = type;
719 ret->quant = XML_REGEXP_QUANT_ONCE;
720 ret->min = 0;
721 ret->max = 0;
722 return(ret);
723}
724
725/**
726 * xmlRegFreeAtom:
727 * @atom: the regexp atom
728 *
729 * Free a regexp atom
730 */
731static void
732xmlRegFreeAtom(xmlRegAtomPtr atom) {
733 int i;
734
735 if (atom == NULL)
736 return;
737
738 for (i = 0;i < atom->nbRanges;i++)
739 xmlRegFreeRange(atom->ranges[i]);
740 if (atom->ranges != NULL)
741 xmlFree(atom->ranges);
742 if (atom->type == XML_REGEXP_STRING)
743 xmlFree(atom->valuep);
744 xmlFree(atom);
745}
746
747static xmlRegStatePtr
748xmlRegNewState(xmlRegParserCtxtPtr ctxt) {
749 xmlRegStatePtr ret;
750
751 ret = (xmlRegStatePtr) xmlMalloc(sizeof(xmlRegState));
752 if (ret == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +0000753 xmlRegexpErrMemory(ctxt, "allocating state");
Daniel Veillard4255d502002-04-16 15:50:10 +0000754 return(NULL);
755 }
756 memset(ret, 0, sizeof(xmlRegState));
757 ret->type = XML_REGEXP_TRANS_STATE;
758 ret->mark = XML_REGEXP_MARK_NORMAL;
759 return(ret);
760}
761
762/**
763 * xmlRegFreeState:
764 * @state: the regexp state
765 *
766 * Free a regexp state
767 */
768static void
769xmlRegFreeState(xmlRegStatePtr state) {
770 if (state == NULL)
771 return;
772
773 if (state->trans != NULL)
774 xmlFree(state->trans);
775 xmlFree(state);
776}
777
778/**
779 * xmlRegFreeParserCtxt:
780 * @ctxt: the regexp parser context
781 *
782 * Free a regexp parser context
783 */
784static void
785xmlRegFreeParserCtxt(xmlRegParserCtxtPtr ctxt) {
786 int i;
787 if (ctxt == NULL)
788 return;
789
790 if (ctxt->string != NULL)
791 xmlFree(ctxt->string);
792 if (ctxt->states != NULL) {
793 for (i = 0;i < ctxt->nbStates;i++)
794 xmlRegFreeState(ctxt->states[i]);
795 xmlFree(ctxt->states);
796 }
797 if (ctxt->atoms != NULL) {
798 for (i = 0;i < ctxt->nbAtoms;i++)
799 xmlRegFreeAtom(ctxt->atoms[i]);
800 xmlFree(ctxt->atoms);
801 }
802 if (ctxt->counters != NULL)
803 xmlFree(ctxt->counters);
804 xmlFree(ctxt);
805}
806
807/************************************************************************
808 * *
809 * Display of Data structures *
810 * *
811 ************************************************************************/
812
813static void
814xmlRegPrintAtomType(FILE *output, xmlRegAtomType type) {
815 switch (type) {
816 case XML_REGEXP_EPSILON:
817 fprintf(output, "epsilon "); break;
818 case XML_REGEXP_CHARVAL:
819 fprintf(output, "charval "); break;
820 case XML_REGEXP_RANGES:
821 fprintf(output, "ranges "); break;
822 case XML_REGEXP_SUBREG:
823 fprintf(output, "subexpr "); break;
824 case XML_REGEXP_STRING:
825 fprintf(output, "string "); break;
826 case XML_REGEXP_ANYCHAR:
827 fprintf(output, "anychar "); break;
828 case XML_REGEXP_ANYSPACE:
829 fprintf(output, "anyspace "); break;
830 case XML_REGEXP_NOTSPACE:
831 fprintf(output, "notspace "); break;
832 case XML_REGEXP_INITNAME:
833 fprintf(output, "initname "); break;
834 case XML_REGEXP_NOTINITNAME:
835 fprintf(output, "notinitname "); break;
836 case XML_REGEXP_NAMECHAR:
837 fprintf(output, "namechar "); break;
838 case XML_REGEXP_NOTNAMECHAR:
839 fprintf(output, "notnamechar "); break;
840 case XML_REGEXP_DECIMAL:
841 fprintf(output, "decimal "); break;
842 case XML_REGEXP_NOTDECIMAL:
843 fprintf(output, "notdecimal "); break;
844 case XML_REGEXP_REALCHAR:
845 fprintf(output, "realchar "); break;
846 case XML_REGEXP_NOTREALCHAR:
847 fprintf(output, "notrealchar "); break;
848 case XML_REGEXP_LETTER:
849 fprintf(output, "LETTER "); break;
850 case XML_REGEXP_LETTER_UPPERCASE:
851 fprintf(output, "LETTER_UPPERCASE "); break;
852 case XML_REGEXP_LETTER_LOWERCASE:
853 fprintf(output, "LETTER_LOWERCASE "); break;
854 case XML_REGEXP_LETTER_TITLECASE:
855 fprintf(output, "LETTER_TITLECASE "); break;
856 case XML_REGEXP_LETTER_MODIFIER:
857 fprintf(output, "LETTER_MODIFIER "); break;
858 case XML_REGEXP_LETTER_OTHERS:
859 fprintf(output, "LETTER_OTHERS "); break;
860 case XML_REGEXP_MARK:
861 fprintf(output, "MARK "); break;
862 case XML_REGEXP_MARK_NONSPACING:
863 fprintf(output, "MARK_NONSPACING "); break;
864 case XML_REGEXP_MARK_SPACECOMBINING:
865 fprintf(output, "MARK_SPACECOMBINING "); break;
866 case XML_REGEXP_MARK_ENCLOSING:
867 fprintf(output, "MARK_ENCLOSING "); break;
868 case XML_REGEXP_NUMBER:
869 fprintf(output, "NUMBER "); break;
870 case XML_REGEXP_NUMBER_DECIMAL:
871 fprintf(output, "NUMBER_DECIMAL "); break;
872 case XML_REGEXP_NUMBER_LETTER:
873 fprintf(output, "NUMBER_LETTER "); break;
874 case XML_REGEXP_NUMBER_OTHERS:
875 fprintf(output, "NUMBER_OTHERS "); break;
876 case XML_REGEXP_PUNCT:
877 fprintf(output, "PUNCT "); break;
878 case XML_REGEXP_PUNCT_CONNECTOR:
879 fprintf(output, "PUNCT_CONNECTOR "); break;
880 case XML_REGEXP_PUNCT_DASH:
881 fprintf(output, "PUNCT_DASH "); break;
882 case XML_REGEXP_PUNCT_OPEN:
883 fprintf(output, "PUNCT_OPEN "); break;
884 case XML_REGEXP_PUNCT_CLOSE:
885 fprintf(output, "PUNCT_CLOSE "); break;
886 case XML_REGEXP_PUNCT_INITQUOTE:
887 fprintf(output, "PUNCT_INITQUOTE "); break;
888 case XML_REGEXP_PUNCT_FINQUOTE:
889 fprintf(output, "PUNCT_FINQUOTE "); break;
890 case XML_REGEXP_PUNCT_OTHERS:
891 fprintf(output, "PUNCT_OTHERS "); break;
892 case XML_REGEXP_SEPAR:
893 fprintf(output, "SEPAR "); break;
894 case XML_REGEXP_SEPAR_SPACE:
895 fprintf(output, "SEPAR_SPACE "); break;
896 case XML_REGEXP_SEPAR_LINE:
897 fprintf(output, "SEPAR_LINE "); break;
898 case XML_REGEXP_SEPAR_PARA:
899 fprintf(output, "SEPAR_PARA "); break;
900 case XML_REGEXP_SYMBOL:
901 fprintf(output, "SYMBOL "); break;
902 case XML_REGEXP_SYMBOL_MATH:
903 fprintf(output, "SYMBOL_MATH "); break;
904 case XML_REGEXP_SYMBOL_CURRENCY:
905 fprintf(output, "SYMBOL_CURRENCY "); break;
906 case XML_REGEXP_SYMBOL_MODIFIER:
907 fprintf(output, "SYMBOL_MODIFIER "); break;
908 case XML_REGEXP_SYMBOL_OTHERS:
909 fprintf(output, "SYMBOL_OTHERS "); break;
910 case XML_REGEXP_OTHER:
911 fprintf(output, "OTHER "); break;
912 case XML_REGEXP_OTHER_CONTROL:
913 fprintf(output, "OTHER_CONTROL "); break;
914 case XML_REGEXP_OTHER_FORMAT:
915 fprintf(output, "OTHER_FORMAT "); break;
916 case XML_REGEXP_OTHER_PRIVATE:
917 fprintf(output, "OTHER_PRIVATE "); break;
918 case XML_REGEXP_OTHER_NA:
919 fprintf(output, "OTHER_NA "); break;
920 case XML_REGEXP_BLOCK_NAME:
921 fprintf(output, "BLOCK "); break;
922 }
923}
924
925static void
926xmlRegPrintQuantType(FILE *output, xmlRegQuantType type) {
927 switch (type) {
928 case XML_REGEXP_QUANT_EPSILON:
929 fprintf(output, "epsilon "); break;
930 case XML_REGEXP_QUANT_ONCE:
931 fprintf(output, "once "); break;
932 case XML_REGEXP_QUANT_OPT:
933 fprintf(output, "? "); break;
934 case XML_REGEXP_QUANT_MULT:
935 fprintf(output, "* "); break;
936 case XML_REGEXP_QUANT_PLUS:
937 fprintf(output, "+ "); break;
938 case XML_REGEXP_QUANT_RANGE:
939 fprintf(output, "range "); break;
Daniel Veillard7646b182002-04-20 06:41:40 +0000940 case XML_REGEXP_QUANT_ONCEONLY:
941 fprintf(output, "onceonly "); break;
942 case XML_REGEXP_QUANT_ALL:
943 fprintf(output, "all "); break;
Daniel Veillard4255d502002-04-16 15:50:10 +0000944 }
945}
946static void
947xmlRegPrintRange(FILE *output, xmlRegRangePtr range) {
948 fprintf(output, " range: ");
949 if (range->neg)
950 fprintf(output, "negative ");
951 xmlRegPrintAtomType(output, range->type);
952 fprintf(output, "%c - %c\n", range->start, range->end);
953}
954
955static void
956xmlRegPrintAtom(FILE *output, xmlRegAtomPtr atom) {
957 fprintf(output, " atom: ");
958 if (atom == NULL) {
959 fprintf(output, "NULL\n");
960 return;
961 }
962 xmlRegPrintAtomType(output, atom->type);
963 xmlRegPrintQuantType(output, atom->quant);
964 if (atom->quant == XML_REGEXP_QUANT_RANGE)
965 fprintf(output, "%d-%d ", atom->min, atom->max);
966 if (atom->type == XML_REGEXP_STRING)
967 fprintf(output, "'%s' ", (char *) atom->valuep);
968 if (atom->type == XML_REGEXP_CHARVAL)
969 fprintf(output, "char %c\n", atom->codepoint);
970 else if (atom->type == XML_REGEXP_RANGES) {
971 int i;
972 fprintf(output, "%d entries\n", atom->nbRanges);
973 for (i = 0; i < atom->nbRanges;i++)
974 xmlRegPrintRange(output, atom->ranges[i]);
975 } else if (atom->type == XML_REGEXP_SUBREG) {
976 fprintf(output, "start %d end %d\n", atom->start->no, atom->stop->no);
977 } else {
978 fprintf(output, "\n");
979 }
980}
981
982static void
983xmlRegPrintTrans(FILE *output, xmlRegTransPtr trans) {
984 fprintf(output, " trans: ");
985 if (trans == NULL) {
986 fprintf(output, "NULL\n");
987 return;
988 }
989 if (trans->to < 0) {
990 fprintf(output, "removed\n");
991 return;
992 }
993 if (trans->counter >= 0) {
994 fprintf(output, "counted %d, ", trans->counter);
995 }
Daniel Veillard8a001f62002-04-20 07:24:11 +0000996 if (trans->count == REGEXP_ALL_COUNTER) {
997 fprintf(output, "all transition, ");
998 } else if (trans->count >= 0) {
Daniel Veillard4255d502002-04-16 15:50:10 +0000999 fprintf(output, "count based %d, ", trans->count);
1000 }
1001 if (trans->atom == NULL) {
1002 fprintf(output, "epsilon to %d\n", trans->to);
1003 return;
1004 }
1005 if (trans->atom->type == XML_REGEXP_CHARVAL)
1006 fprintf(output, "char %c ", trans->atom->codepoint);
1007 fprintf(output, "atom %d, to %d\n", trans->atom->no, trans->to);
1008}
1009
1010static void
1011xmlRegPrintState(FILE *output, xmlRegStatePtr state) {
1012 int i;
1013
1014 fprintf(output, " state: ");
1015 if (state == NULL) {
1016 fprintf(output, "NULL\n");
1017 return;
1018 }
1019 if (state->type == XML_REGEXP_START_STATE)
1020 fprintf(output, "START ");
1021 if (state->type == XML_REGEXP_FINAL_STATE)
1022 fprintf(output, "FINAL ");
1023
1024 fprintf(output, "%d, %d transitions:\n", state->no, state->nbTrans);
1025 for (i = 0;i < state->nbTrans; i++) {
1026 xmlRegPrintTrans(output, &(state->trans[i]));
1027 }
1028}
1029
Daniel Veillard23e73572002-09-19 19:56:43 +00001030#ifdef DEBUG_REGEXP_GRAPH
Daniel Veillard4255d502002-04-16 15:50:10 +00001031static void
1032xmlRegPrintCtxt(FILE *output, xmlRegParserCtxtPtr ctxt) {
1033 int i;
1034
1035 fprintf(output, " ctxt: ");
1036 if (ctxt == NULL) {
1037 fprintf(output, "NULL\n");
1038 return;
1039 }
1040 fprintf(output, "'%s' ", ctxt->string);
1041 if (ctxt->error)
1042 fprintf(output, "error ");
1043 if (ctxt->neg)
1044 fprintf(output, "neg ");
1045 fprintf(output, "\n");
1046 fprintf(output, "%d atoms:\n", ctxt->nbAtoms);
1047 for (i = 0;i < ctxt->nbAtoms; i++) {
1048 fprintf(output, " %02d ", i);
1049 xmlRegPrintAtom(output, ctxt->atoms[i]);
1050 }
1051 if (ctxt->atom != NULL) {
1052 fprintf(output, "current atom:\n");
1053 xmlRegPrintAtom(output, ctxt->atom);
1054 }
1055 fprintf(output, "%d states:", ctxt->nbStates);
1056 if (ctxt->start != NULL)
1057 fprintf(output, " start: %d", ctxt->start->no);
1058 if (ctxt->end != NULL)
1059 fprintf(output, " end: %d", ctxt->end->no);
1060 fprintf(output, "\n");
1061 for (i = 0;i < ctxt->nbStates; i++) {
1062 xmlRegPrintState(output, ctxt->states[i]);
1063 }
1064 fprintf(output, "%d counters:\n", ctxt->nbCounters);
1065 for (i = 0;i < ctxt->nbCounters; i++) {
1066 fprintf(output, " %d: min %d max %d\n", i, ctxt->counters[i].min,
1067 ctxt->counters[i].max);
1068 }
1069}
Daniel Veillard23e73572002-09-19 19:56:43 +00001070#endif
Daniel Veillard4255d502002-04-16 15:50:10 +00001071
1072/************************************************************************
1073 * *
1074 * Finite Automata structures manipulations *
1075 * *
1076 ************************************************************************/
1077
1078static void
1079xmlRegAtomAddRange(xmlRegParserCtxtPtr ctxt, xmlRegAtomPtr atom,
1080 int neg, xmlRegAtomType type, int start, int end,
1081 xmlChar *blockName) {
1082 xmlRegRangePtr range;
1083
1084 if (atom == NULL) {
1085 ERROR("add range: atom is NULL");
1086 return;
1087 }
1088 if (atom->type != XML_REGEXP_RANGES) {
1089 ERROR("add range: atom is not ranges");
1090 return;
1091 }
1092 if (atom->maxRanges == 0) {
1093 atom->maxRanges = 4;
1094 atom->ranges = (xmlRegRangePtr *) xmlMalloc(atom->maxRanges *
1095 sizeof(xmlRegRangePtr));
1096 if (atom->ranges == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00001097 xmlRegexpErrMemory(ctxt, "adding ranges");
Daniel Veillard4255d502002-04-16 15:50:10 +00001098 atom->maxRanges = 0;
1099 return;
1100 }
1101 } else if (atom->nbRanges >= atom->maxRanges) {
1102 xmlRegRangePtr *tmp;
1103 atom->maxRanges *= 2;
1104 tmp = (xmlRegRangePtr *) xmlRealloc(atom->ranges, atom->maxRanges *
1105 sizeof(xmlRegRangePtr));
1106 if (tmp == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00001107 xmlRegexpErrMemory(ctxt, "adding ranges");
Daniel Veillard4255d502002-04-16 15:50:10 +00001108 atom->maxRanges /= 2;
1109 return;
1110 }
1111 atom->ranges = tmp;
1112 }
1113 range = xmlRegNewRange(ctxt, neg, type, start, end);
1114 if (range == NULL)
1115 return;
1116 range->blockName = blockName;
1117 atom->ranges[atom->nbRanges++] = range;
1118
1119}
1120
1121static int
1122xmlRegGetCounter(xmlRegParserCtxtPtr ctxt) {
1123 if (ctxt->maxCounters == 0) {
1124 ctxt->maxCounters = 4;
1125 ctxt->counters = (xmlRegCounter *) xmlMalloc(ctxt->maxCounters *
1126 sizeof(xmlRegCounter));
1127 if (ctxt->counters == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00001128 xmlRegexpErrMemory(ctxt, "allocating counter");
Daniel Veillard4255d502002-04-16 15:50:10 +00001129 ctxt->maxCounters = 0;
1130 return(-1);
1131 }
1132 } else if (ctxt->nbCounters >= ctxt->maxCounters) {
1133 xmlRegCounter *tmp;
1134 ctxt->maxCounters *= 2;
1135 tmp = (xmlRegCounter *) xmlRealloc(ctxt->counters, ctxt->maxCounters *
1136 sizeof(xmlRegCounter));
1137 if (tmp == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00001138 xmlRegexpErrMemory(ctxt, "allocating counter");
Daniel Veillard4255d502002-04-16 15:50:10 +00001139 ctxt->maxCounters /= 2;
1140 return(-1);
1141 }
1142 ctxt->counters = tmp;
1143 }
1144 ctxt->counters[ctxt->nbCounters].min = -1;
1145 ctxt->counters[ctxt->nbCounters].max = -1;
1146 return(ctxt->nbCounters++);
1147}
1148
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001149static int
Daniel Veillard4255d502002-04-16 15:50:10 +00001150xmlRegAtomPush(xmlRegParserCtxtPtr ctxt, xmlRegAtomPtr atom) {
1151 if (atom == NULL) {
1152 ERROR("atom push: atom is NULL");
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001153 return(-1);
Daniel Veillard4255d502002-04-16 15:50:10 +00001154 }
1155 if (ctxt->maxAtoms == 0) {
1156 ctxt->maxAtoms = 4;
1157 ctxt->atoms = (xmlRegAtomPtr *) xmlMalloc(ctxt->maxAtoms *
1158 sizeof(xmlRegAtomPtr));
1159 if (ctxt->atoms == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00001160 xmlRegexpErrMemory(ctxt, "pushing atom");
Daniel Veillard4255d502002-04-16 15:50:10 +00001161 ctxt->maxAtoms = 0;
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001162 return(-1);
Daniel Veillard4255d502002-04-16 15:50:10 +00001163 }
1164 } else if (ctxt->nbAtoms >= ctxt->maxAtoms) {
1165 xmlRegAtomPtr *tmp;
1166 ctxt->maxAtoms *= 2;
1167 tmp = (xmlRegAtomPtr *) xmlRealloc(ctxt->atoms, ctxt->maxAtoms *
1168 sizeof(xmlRegAtomPtr));
1169 if (tmp == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00001170 xmlRegexpErrMemory(ctxt, "allocating counter");
Daniel Veillard4255d502002-04-16 15:50:10 +00001171 ctxt->maxAtoms /= 2;
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001172 return(-1);
Daniel Veillard4255d502002-04-16 15:50:10 +00001173 }
1174 ctxt->atoms = tmp;
1175 }
1176 atom->no = ctxt->nbAtoms;
1177 ctxt->atoms[ctxt->nbAtoms++] = atom;
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001178 return(0);
Daniel Veillard4255d502002-04-16 15:50:10 +00001179}
1180
1181static void
1182xmlRegStateAddTrans(xmlRegParserCtxtPtr ctxt, xmlRegStatePtr state,
1183 xmlRegAtomPtr atom, xmlRegStatePtr target,
1184 int counter, int count) {
1185 if (state == NULL) {
1186 ERROR("add state: state is NULL");
1187 return;
1188 }
1189 if (target == NULL) {
1190 ERROR("add state: target is NULL");
1191 return;
1192 }
1193 if (state->maxTrans == 0) {
1194 state->maxTrans = 4;
1195 state->trans = (xmlRegTrans *) xmlMalloc(state->maxTrans *
1196 sizeof(xmlRegTrans));
1197 if (state->trans == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00001198 xmlRegexpErrMemory(ctxt, "adding transition");
Daniel Veillard4255d502002-04-16 15:50:10 +00001199 state->maxTrans = 0;
1200 return;
1201 }
1202 } else if (state->nbTrans >= state->maxTrans) {
1203 xmlRegTrans *tmp;
1204 state->maxTrans *= 2;
1205 tmp = (xmlRegTrans *) xmlRealloc(state->trans, state->maxTrans *
1206 sizeof(xmlRegTrans));
1207 if (tmp == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00001208 xmlRegexpErrMemory(ctxt, "adding transition");
Daniel Veillard4255d502002-04-16 15:50:10 +00001209 state->maxTrans /= 2;
1210 return;
1211 }
1212 state->trans = tmp;
1213 }
1214#ifdef DEBUG_REGEXP_GRAPH
1215 printf("Add trans from %d to %d ", state->no, target->no);
Daniel Veillard8a001f62002-04-20 07:24:11 +00001216 if (count == REGEXP_ALL_COUNTER)
Daniel Veillard2cbf5962004-03-31 15:50:43 +00001217 printf("all transition\n");
Daniel Veillard4402ab42002-09-12 16:02:56 +00001218 else if (count >= 0)
Daniel Veillard2cbf5962004-03-31 15:50:43 +00001219 printf("count based %d\n", count);
Daniel Veillard4255d502002-04-16 15:50:10 +00001220 else if (counter >= 0)
Daniel Veillard2cbf5962004-03-31 15:50:43 +00001221 printf("counted %d\n", counter);
Daniel Veillard4255d502002-04-16 15:50:10 +00001222 else if (atom == NULL)
Daniel Veillard2cbf5962004-03-31 15:50:43 +00001223 printf("epsilon transition\n");
1224 else if (atom != NULL)
1225 xmlRegPrintAtom(stdout, atom);
Daniel Veillard4255d502002-04-16 15:50:10 +00001226#endif
1227
1228 state->trans[state->nbTrans].atom = atom;
1229 state->trans[state->nbTrans].to = target->no;
1230 state->trans[state->nbTrans].counter = counter;
1231 state->trans[state->nbTrans].count = count;
1232 state->nbTrans++;
1233}
1234
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001235static int
Daniel Veillard4255d502002-04-16 15:50:10 +00001236xmlRegStatePush(xmlRegParserCtxtPtr ctxt, xmlRegStatePtr state) {
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001237 if (state == NULL) return(-1);
Daniel Veillard4255d502002-04-16 15:50:10 +00001238 if (ctxt->maxStates == 0) {
1239 ctxt->maxStates = 4;
1240 ctxt->states = (xmlRegStatePtr *) xmlMalloc(ctxt->maxStates *
1241 sizeof(xmlRegStatePtr));
1242 if (ctxt->states == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00001243 xmlRegexpErrMemory(ctxt, "adding state");
Daniel Veillard4255d502002-04-16 15:50:10 +00001244 ctxt->maxStates = 0;
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001245 return(-1);
Daniel Veillard4255d502002-04-16 15:50:10 +00001246 }
1247 } else if (ctxt->nbStates >= ctxt->maxStates) {
1248 xmlRegStatePtr *tmp;
1249 ctxt->maxStates *= 2;
1250 tmp = (xmlRegStatePtr *) xmlRealloc(ctxt->states, ctxt->maxStates *
1251 sizeof(xmlRegStatePtr));
1252 if (tmp == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00001253 xmlRegexpErrMemory(ctxt, "adding state");
Daniel Veillard4255d502002-04-16 15:50:10 +00001254 ctxt->maxStates /= 2;
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001255 return(-1);
Daniel Veillard4255d502002-04-16 15:50:10 +00001256 }
1257 ctxt->states = tmp;
1258 }
1259 state->no = ctxt->nbStates;
1260 ctxt->states[ctxt->nbStates++] = state;
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001261 return(0);
Daniel Veillard4255d502002-04-16 15:50:10 +00001262}
1263
1264/**
Daniel Veillard7646b182002-04-20 06:41:40 +00001265 * xmlFAGenerateAllTransition:
Daniel Veillard441bc322002-04-20 17:38:48 +00001266 * @ctxt: a regexp parser context
1267 * @from: the from state
1268 * @to: the target state or NULL for building a new one
1269 * @lax:
Daniel Veillard7646b182002-04-20 06:41:40 +00001270 *
1271 */
1272static void
1273xmlFAGenerateAllTransition(xmlRegParserCtxtPtr ctxt,
Daniel Veillard441bc322002-04-20 17:38:48 +00001274 xmlRegStatePtr from, xmlRegStatePtr to,
1275 int lax) {
Daniel Veillard7646b182002-04-20 06:41:40 +00001276 if (to == NULL) {
1277 to = xmlRegNewState(ctxt);
1278 xmlRegStatePush(ctxt, to);
1279 ctxt->state = to;
1280 }
Daniel Veillard441bc322002-04-20 17:38:48 +00001281 if (lax)
1282 xmlRegStateAddTrans(ctxt, from, NULL, to, -1, REGEXP_ALL_LAX_COUNTER);
1283 else
1284 xmlRegStateAddTrans(ctxt, from, NULL, to, -1, REGEXP_ALL_COUNTER);
Daniel Veillard7646b182002-04-20 06:41:40 +00001285}
1286
1287/**
Daniel Veillard4255d502002-04-16 15:50:10 +00001288 * xmlFAGenerateEpsilonTransition:
Daniel Veillard441bc322002-04-20 17:38:48 +00001289 * @ctxt: a regexp parser context
1290 * @from: the from state
1291 * @to: the target state or NULL for building a new one
Daniel Veillard4255d502002-04-16 15:50:10 +00001292 *
1293 */
1294static void
1295xmlFAGenerateEpsilonTransition(xmlRegParserCtxtPtr ctxt,
1296 xmlRegStatePtr from, xmlRegStatePtr to) {
1297 if (to == NULL) {
1298 to = xmlRegNewState(ctxt);
1299 xmlRegStatePush(ctxt, to);
1300 ctxt->state = to;
1301 }
1302 xmlRegStateAddTrans(ctxt, from, NULL, to, -1, -1);
1303}
1304
1305/**
1306 * xmlFAGenerateCountedEpsilonTransition:
Daniel Veillard441bc322002-04-20 17:38:48 +00001307 * @ctxt: a regexp parser context
1308 * @from: the from state
1309 * @to: the target state or NULL for building a new one
Daniel Veillard4255d502002-04-16 15:50:10 +00001310 * counter: the counter for that transition
1311 *
1312 */
1313static void
1314xmlFAGenerateCountedEpsilonTransition(xmlRegParserCtxtPtr ctxt,
1315 xmlRegStatePtr from, xmlRegStatePtr to, int counter) {
1316 if (to == NULL) {
1317 to = xmlRegNewState(ctxt);
1318 xmlRegStatePush(ctxt, to);
1319 ctxt->state = to;
1320 }
1321 xmlRegStateAddTrans(ctxt, from, NULL, to, counter, -1);
1322}
1323
1324/**
1325 * xmlFAGenerateCountedTransition:
Daniel Veillard441bc322002-04-20 17:38:48 +00001326 * @ctxt: a regexp parser context
1327 * @from: the from state
1328 * @to: the target state or NULL for building a new one
Daniel Veillard4255d502002-04-16 15:50:10 +00001329 * counter: the counter for that transition
1330 *
1331 */
1332static void
1333xmlFAGenerateCountedTransition(xmlRegParserCtxtPtr ctxt,
1334 xmlRegStatePtr from, xmlRegStatePtr to, int counter) {
1335 if (to == NULL) {
1336 to = xmlRegNewState(ctxt);
1337 xmlRegStatePush(ctxt, to);
1338 ctxt->state = to;
1339 }
1340 xmlRegStateAddTrans(ctxt, from, NULL, to, -1, counter);
1341}
1342
1343/**
1344 * xmlFAGenerateTransitions:
Daniel Veillard441bc322002-04-20 17:38:48 +00001345 * @ctxt: a regexp parser context
1346 * @from: the from state
1347 * @to: the target state or NULL for building a new one
1348 * @atom: the atom generating the transition
Daniel Veillard4255d502002-04-16 15:50:10 +00001349 *
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001350 * Returns 0 if succes and -1 in case of error.
Daniel Veillard4255d502002-04-16 15:50:10 +00001351 */
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001352static int
Daniel Veillard4255d502002-04-16 15:50:10 +00001353xmlFAGenerateTransitions(xmlRegParserCtxtPtr ctxt, xmlRegStatePtr from,
1354 xmlRegStatePtr to, xmlRegAtomPtr atom) {
1355 if (atom == NULL) {
1356 ERROR("genrate transition: atom == NULL");
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001357 return(-1);
Daniel Veillard4255d502002-04-16 15:50:10 +00001358 }
1359 if (atom->type == XML_REGEXP_SUBREG) {
1360 /*
1361 * this is a subexpression handling one should not need to
1362 * create a new node excep for XML_REGEXP_QUANT_RANGE.
1363 */
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001364 if (xmlRegAtomPush(ctxt, atom) < 0) {
1365 return(-1);
1366 }
Daniel Veillard4255d502002-04-16 15:50:10 +00001367 if ((to != NULL) && (atom->stop != to) &&
1368 (atom->quant != XML_REGEXP_QUANT_RANGE)) {
1369 /*
1370 * Generate an epsilon transition to link to the target
1371 */
1372 xmlFAGenerateEpsilonTransition(ctxt, atom->stop, to);
1373 }
1374 switch (atom->quant) {
1375 case XML_REGEXP_QUANT_OPT:
1376 atom->quant = XML_REGEXP_QUANT_ONCE;
1377 xmlFAGenerateEpsilonTransition(ctxt, atom->start, atom->stop);
1378 break;
1379 case XML_REGEXP_QUANT_MULT:
1380 atom->quant = XML_REGEXP_QUANT_ONCE;
1381 xmlFAGenerateEpsilonTransition(ctxt, atom->start, atom->stop);
1382 xmlFAGenerateEpsilonTransition(ctxt, atom->stop, atom->start);
1383 break;
1384 case XML_REGEXP_QUANT_PLUS:
1385 atom->quant = XML_REGEXP_QUANT_ONCE;
1386 xmlFAGenerateEpsilonTransition(ctxt, atom->stop, atom->start);
1387 break;
1388 case XML_REGEXP_QUANT_RANGE: {
1389 int counter;
1390 xmlRegStatePtr newstate;
1391
1392 /*
1393 * This one is nasty:
1394 * 1/ register a new counter
1395 * 2/ register an epsilon transition associated to
1396 * this counter going from atom->stop to atom->start
1397 * 3/ create a new state
1398 * 4/ generate a counted transition from atom->stop to
1399 * that state
1400 */
1401 counter = xmlRegGetCounter(ctxt);
1402 ctxt->counters[counter].min = atom->min - 1;
1403 ctxt->counters[counter].max = atom->max - 1;
1404 atom->min = 0;
1405 atom->max = 0;
1406 atom->quant = XML_REGEXP_QUANT_ONCE;
1407 xmlFAGenerateCountedEpsilonTransition(ctxt, atom->stop,
1408 atom->start, counter);
1409 if (to != NULL) {
1410 newstate = to;
1411 } else {
1412 newstate = xmlRegNewState(ctxt);
1413 xmlRegStatePush(ctxt, newstate);
1414 ctxt->state = newstate;
1415 }
1416 xmlFAGenerateCountedTransition(ctxt, atom->stop,
1417 newstate, counter);
1418 }
1419 default:
1420 break;
1421 }
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001422 return(0);
Daniel Veillard4255d502002-04-16 15:50:10 +00001423 } else {
1424 if (to == NULL) {
1425 to = xmlRegNewState(ctxt);
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001426 if (to != NULL)
1427 xmlRegStatePush(ctxt, to);
1428 else {
1429 return(-1);
1430 }
1431 }
1432 if (xmlRegAtomPush(ctxt, atom) < 0) {
1433 return(-1);
Daniel Veillard4255d502002-04-16 15:50:10 +00001434 }
1435 xmlRegStateAddTrans(ctxt, from, atom, to, -1, -1);
Daniel Veillard4255d502002-04-16 15:50:10 +00001436 ctxt->state = to;
1437 }
1438 switch (atom->quant) {
1439 case XML_REGEXP_QUANT_OPT:
1440 atom->quant = XML_REGEXP_QUANT_ONCE;
1441 xmlFAGenerateEpsilonTransition(ctxt, from, to);
1442 break;
1443 case XML_REGEXP_QUANT_MULT:
1444 atom->quant = XML_REGEXP_QUANT_ONCE;
1445 xmlFAGenerateEpsilonTransition(ctxt, from, to);
1446 xmlRegStateAddTrans(ctxt, to, atom, to, -1, -1);
1447 break;
1448 case XML_REGEXP_QUANT_PLUS:
1449 atom->quant = XML_REGEXP_QUANT_ONCE;
1450 xmlRegStateAddTrans(ctxt, to, atom, to, -1, -1);
1451 break;
1452 default:
1453 break;
1454 }
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001455 return(0);
Daniel Veillard4255d502002-04-16 15:50:10 +00001456}
1457
1458/**
1459 * xmlFAReduceEpsilonTransitions:
Daniel Veillard441bc322002-04-20 17:38:48 +00001460 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00001461 * @fromnr: the from state
1462 * @tonr: the to state
1463 * @cpunter: should that transition be associted to a counted
1464 *
1465 */
1466static void
1467xmlFAReduceEpsilonTransitions(xmlRegParserCtxtPtr ctxt, int fromnr,
1468 int tonr, int counter) {
1469 int transnr;
1470 xmlRegStatePtr from;
1471 xmlRegStatePtr to;
1472
1473#ifdef DEBUG_REGEXP_GRAPH
1474 printf("xmlFAReduceEpsilonTransitions(%d, %d)\n", fromnr, tonr);
1475#endif
1476 from = ctxt->states[fromnr];
1477 if (from == NULL)
1478 return;
1479 to = ctxt->states[tonr];
1480 if (to == NULL)
1481 return;
1482 if ((to->mark == XML_REGEXP_MARK_START) ||
1483 (to->mark == XML_REGEXP_MARK_VISITED))
1484 return;
1485
1486 to->mark = XML_REGEXP_MARK_VISITED;
1487 if (to->type == XML_REGEXP_FINAL_STATE) {
1488#ifdef DEBUG_REGEXP_GRAPH
1489 printf("State %d is final, so %d becomes final\n", tonr, fromnr);
1490#endif
1491 from->type = XML_REGEXP_FINAL_STATE;
1492 }
1493 for (transnr = 0;transnr < to->nbTrans;transnr++) {
1494 if (to->trans[transnr].atom == NULL) {
1495 /*
1496 * Don't remove counted transitions
1497 * Don't loop either
1498 */
Daniel Veillardb509f152002-04-17 16:28:10 +00001499 if (to->trans[transnr].to != fromnr) {
1500 if (to->trans[transnr].count >= 0) {
1501 int newto = to->trans[transnr].to;
1502
1503 xmlRegStateAddTrans(ctxt, from, NULL,
1504 ctxt->states[newto],
1505 -1, to->trans[transnr].count);
1506 } else {
Daniel Veillard4255d502002-04-16 15:50:10 +00001507#ifdef DEBUG_REGEXP_GRAPH
Daniel Veillardb509f152002-04-17 16:28:10 +00001508 printf("Found epsilon trans %d from %d to %d\n",
1509 transnr, tonr, to->trans[transnr].to);
Daniel Veillard4255d502002-04-16 15:50:10 +00001510#endif
Daniel Veillardb509f152002-04-17 16:28:10 +00001511 if (to->trans[transnr].counter >= 0) {
1512 xmlFAReduceEpsilonTransitions(ctxt, fromnr,
1513 to->trans[transnr].to,
1514 to->trans[transnr].counter);
1515 } else {
1516 xmlFAReduceEpsilonTransitions(ctxt, fromnr,
1517 to->trans[transnr].to,
1518 counter);
1519 }
1520 }
Daniel Veillard4255d502002-04-16 15:50:10 +00001521 }
1522 } else {
1523 int newto = to->trans[transnr].to;
1524
Daniel Veillardb509f152002-04-17 16:28:10 +00001525 if (to->trans[transnr].counter >= 0) {
1526 xmlRegStateAddTrans(ctxt, from, to->trans[transnr].atom,
1527 ctxt->states[newto],
1528 to->trans[transnr].counter, -1);
1529 } else {
1530 xmlRegStateAddTrans(ctxt, from, to->trans[transnr].atom,
1531 ctxt->states[newto], counter, -1);
1532 }
Daniel Veillard4255d502002-04-16 15:50:10 +00001533 }
1534 }
1535 to->mark = XML_REGEXP_MARK_NORMAL;
1536}
1537
1538/**
1539 * xmlFAEliminateEpsilonTransitions:
Daniel Veillard441bc322002-04-20 17:38:48 +00001540 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00001541 *
1542 */
1543static void
1544xmlFAEliminateEpsilonTransitions(xmlRegParserCtxtPtr ctxt) {
1545 int statenr, transnr;
1546 xmlRegStatePtr state;
1547
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001548 if (ctxt->states == NULL) return;
1549
1550
Daniel Veillard4255d502002-04-16 15:50:10 +00001551 /*
1552 * build the completed transitions bypassing the epsilons
1553 * Use a marking algorithm to avoid loops
1554 */
1555 for (statenr = 0;statenr < ctxt->nbStates;statenr++) {
1556 state = ctxt->states[statenr];
1557 if (state == NULL)
1558 continue;
1559 for (transnr = 0;transnr < state->nbTrans;transnr++) {
1560 if ((state->trans[transnr].atom == NULL) &&
1561 (state->trans[transnr].to >= 0)) {
1562 if (state->trans[transnr].to == statenr) {
1563 state->trans[transnr].to = -1;
1564#ifdef DEBUG_REGEXP_GRAPH
1565 printf("Removed loopback epsilon trans %d on %d\n",
1566 transnr, statenr);
1567#endif
1568 } else if (state->trans[transnr].count < 0) {
1569 int newto = state->trans[transnr].to;
1570
1571#ifdef DEBUG_REGEXP_GRAPH
1572 printf("Found epsilon trans %d from %d to %d\n",
1573 transnr, statenr, newto);
1574#endif
1575 state->mark = XML_REGEXP_MARK_START;
1576 xmlFAReduceEpsilonTransitions(ctxt, statenr,
1577 newto, state->trans[transnr].counter);
1578 state->mark = XML_REGEXP_MARK_NORMAL;
1579#ifdef DEBUG_REGEXP_GRAPH
1580 } else {
1581 printf("Found counted transition %d on %d\n",
1582 transnr, statenr);
1583#endif
1584 }
1585 }
1586 }
1587 }
1588 /*
1589 * Eliminate the epsilon transitions
1590 */
1591 for (statenr = 0;statenr < ctxt->nbStates;statenr++) {
1592 state = ctxt->states[statenr];
1593 if (state == NULL)
1594 continue;
1595 for (transnr = 0;transnr < state->nbTrans;transnr++) {
1596 if ((state->trans[transnr].atom == NULL) &&
1597 (state->trans[transnr].count < 0) &&
1598 (state->trans[transnr].to >= 0)) {
1599 state->trans[transnr].to = -1;
1600 }
1601 }
1602 }
Daniel Veillard23e73572002-09-19 19:56:43 +00001603
1604 /*
1605 * Use this pass to detect unreachable states too
1606 */
1607 for (statenr = 0;statenr < ctxt->nbStates;statenr++) {
1608 state = ctxt->states[statenr];
1609 if (state != NULL)
William M. Brack779af002003-08-01 15:55:39 +00001610 state->reached = XML_REGEXP_MARK_NORMAL;
Daniel Veillard23e73572002-09-19 19:56:43 +00001611 }
1612 state = ctxt->states[0];
1613 if (state != NULL)
William M. Brack779af002003-08-01 15:55:39 +00001614 state->reached = XML_REGEXP_MARK_START;
Daniel Veillard23e73572002-09-19 19:56:43 +00001615 while (state != NULL) {
1616 xmlRegStatePtr target = NULL;
William M. Brack779af002003-08-01 15:55:39 +00001617 state->reached = XML_REGEXP_MARK_VISITED;
Daniel Veillard23e73572002-09-19 19:56:43 +00001618 /*
1619 * Mark all state reachable from the current reachable state
1620 */
1621 for (transnr = 0;transnr < state->nbTrans;transnr++) {
1622 if ((state->trans[transnr].to >= 0) &&
1623 ((state->trans[transnr].atom != NULL) ||
1624 (state->trans[transnr].count >= 0))) {
1625 int newto = state->trans[transnr].to;
1626
1627 if (ctxt->states[newto] == NULL)
1628 continue;
William M. Brack779af002003-08-01 15:55:39 +00001629 if (ctxt->states[newto]->reached == XML_REGEXP_MARK_NORMAL) {
1630 ctxt->states[newto]->reached = XML_REGEXP_MARK_START;
Daniel Veillard23e73572002-09-19 19:56:43 +00001631 target = ctxt->states[newto];
1632 }
1633 }
1634 }
1635 /*
1636 * find the next accessible state not explored
1637 */
1638 if (target == NULL) {
1639 for (statenr = 1;statenr < ctxt->nbStates;statenr++) {
1640 state = ctxt->states[statenr];
William M. Brack779af002003-08-01 15:55:39 +00001641 if ((state != NULL) && (state->reached ==
1642 XML_REGEXP_MARK_START)) {
Daniel Veillard23e73572002-09-19 19:56:43 +00001643 target = state;
1644 break;
1645 }
1646 }
1647 }
1648 state = target;
1649 }
1650 for (statenr = 0;statenr < ctxt->nbStates;statenr++) {
1651 state = ctxt->states[statenr];
William M. Brack779af002003-08-01 15:55:39 +00001652 if ((state != NULL) && (state->reached == XML_REGEXP_MARK_NORMAL)) {
Daniel Veillard23e73572002-09-19 19:56:43 +00001653#ifdef DEBUG_REGEXP_GRAPH
1654 printf("Removed unreachable state %d\n", statenr);
1655#endif
1656 xmlRegFreeState(state);
1657 ctxt->states[statenr] = NULL;
1658 }
1659 }
1660
Daniel Veillard4255d502002-04-16 15:50:10 +00001661}
1662
Daniel Veillarde19fc232002-04-22 16:01:24 +00001663/**
1664 * xmlFACompareAtoms:
1665 * @atom1: an atom
1666 * @atom2: an atom
1667 *
1668 * Compares two atoms to check whether they are equivatents
1669 *
1670 * Returns 1 if yes and 0 otherwise
1671 */
1672static int
1673xmlFACompareAtoms(xmlRegAtomPtr atom1, xmlRegAtomPtr atom2) {
1674 if (atom1 == atom2)
1675 return(1);
1676 if ((atom1 == NULL) || (atom2 == NULL))
1677 return(0);
1678
1679 if (atom1->type != atom2->type)
1680 return(0);
1681 switch (atom1->type) {
1682 case XML_REGEXP_STRING:
1683 return(xmlStrEqual((xmlChar *)atom1->valuep,
1684 (xmlChar *)atom2->valuep));
1685 case XML_REGEXP_EPSILON:
1686 return(1);
1687 case XML_REGEXP_CHARVAL:
1688 return(atom1->codepoint == atom2->codepoint);
1689 case XML_REGEXP_RANGES:
1690 TODO;
1691 return(0);
1692 default:
1693 break;
1694 }
1695 return(1);
1696}
1697
1698/**
1699 * xmlFARecurseDeterminism:
1700 * @ctxt: a regexp parser context
1701 *
1702 * Check whether the associated regexp is determinist,
1703 * should be called after xmlFAEliminateEpsilonTransitions()
1704 *
1705 */
1706static int
1707xmlFARecurseDeterminism(xmlRegParserCtxtPtr ctxt, xmlRegStatePtr state,
1708 int to, xmlRegAtomPtr atom) {
1709 int ret = 1;
1710 int transnr;
1711 xmlRegTransPtr t1;
1712
1713 if (state == NULL)
1714 return(ret);
1715 for (transnr = 0;transnr < state->nbTrans;transnr++) {
1716 t1 = &(state->trans[transnr]);
1717 /*
1718 * check transitions conflicting with the one looked at
1719 */
1720 if (t1->atom == NULL) {
1721 if (t1->to == -1)
1722 continue;
1723 ret = xmlFARecurseDeterminism(ctxt, ctxt->states[t1->to],
1724 to, atom);
1725 if (ret == 0)
1726 return(0);
1727 continue;
1728 }
1729 if (t1->to != to)
1730 continue;
1731 if (xmlFACompareAtoms(t1->atom, atom))
1732 return(0);
1733 }
1734 return(ret);
1735}
1736
1737/**
1738 * xmlFAComputesDeterminism:
1739 * @ctxt: a regexp parser context
1740 *
1741 * Check whether the associated regexp is determinist,
1742 * should be called after xmlFAEliminateEpsilonTransitions()
1743 *
1744 */
1745static int
1746xmlFAComputesDeterminism(xmlRegParserCtxtPtr ctxt) {
1747 int statenr, transnr;
1748 xmlRegStatePtr state;
1749 xmlRegTransPtr t1, t2;
1750 int i;
1751 int ret = 1;
1752
Daniel Veillard4402ab42002-09-12 16:02:56 +00001753#ifdef DEBUG_REGEXP_GRAPH
1754 printf("xmlFAComputesDeterminism\n");
1755 xmlRegPrintCtxt(stdout, ctxt);
1756#endif
Daniel Veillarde19fc232002-04-22 16:01:24 +00001757 if (ctxt->determinist != -1)
1758 return(ctxt->determinist);
1759
1760 /*
1761 * Check for all states that there isn't 2 transitions
1762 * with the same atom and a different target.
1763 */
1764 for (statenr = 0;statenr < ctxt->nbStates;statenr++) {
1765 state = ctxt->states[statenr];
1766 if (state == NULL)
1767 continue;
1768 for (transnr = 0;transnr < state->nbTrans;transnr++) {
1769 t1 = &(state->trans[transnr]);
1770 /*
1771 * Determinism checks in case of counted or all transitions
1772 * will have to be handled separately
1773 */
1774 if (t1->atom == NULL)
1775 continue;
1776 if (t1->to == -1) /* eliminated */
1777 continue;
1778 for (i = 0;i < transnr;i++) {
1779 t2 = &(state->trans[i]);
1780 if (t2->to == -1) /* eliminated */
1781 continue;
1782 if (t2->atom != NULL) {
1783 if (t1->to == t2->to) {
1784 if (xmlFACompareAtoms(t1->atom, t2->atom))
1785 t2->to = -1; /* eliminate */
1786 } else {
1787 /* not determinist ! */
1788 if (xmlFACompareAtoms(t1->atom, t2->atom))
1789 ret = 0;
1790 }
1791 } else if (t1->to != -1) {
1792 /*
1793 * do the closure in case of remaining specific
1794 * epsilon transitions like choices or all
1795 */
1796 ret = xmlFARecurseDeterminism(ctxt, ctxt->states[t1->to],
1797 t2->to, t2->atom);
1798 if (ret == 0)
1799 return(0);
1800 }
1801 }
1802 if (ret == 0)
1803 break;
1804 }
1805 if (ret == 0)
1806 break;
1807 }
1808 ctxt->determinist = ret;
1809 return(ret);
1810}
1811
Daniel Veillard4255d502002-04-16 15:50:10 +00001812/************************************************************************
1813 * *
1814 * Routines to check input against transition atoms *
1815 * *
1816 ************************************************************************/
1817
1818static int
1819xmlRegCheckCharacterRange(xmlRegAtomType type, int codepoint, int neg,
1820 int start, int end, const xmlChar *blockName) {
1821 int ret = 0;
1822
1823 switch (type) {
1824 case XML_REGEXP_STRING:
1825 case XML_REGEXP_SUBREG:
1826 case XML_REGEXP_RANGES:
1827 case XML_REGEXP_EPSILON:
1828 return(-1);
1829 case XML_REGEXP_ANYCHAR:
1830 ret = ((codepoint != '\n') && (codepoint != '\r'));
1831 break;
1832 case XML_REGEXP_CHARVAL:
1833 ret = ((codepoint >= start) && (codepoint <= end));
1834 break;
1835 case XML_REGEXP_NOTSPACE:
1836 neg = !neg;
1837 case XML_REGEXP_ANYSPACE:
1838 ret = ((codepoint == '\n') || (codepoint == '\r') ||
1839 (codepoint == '\t') || (codepoint == ' '));
1840 break;
1841 case XML_REGEXP_NOTINITNAME:
1842 neg = !neg;
1843 case XML_REGEXP_INITNAME:
William M. Brack871611b2003-10-18 04:53:14 +00001844 ret = (IS_LETTER(codepoint) ||
Daniel Veillard4255d502002-04-16 15:50:10 +00001845 (codepoint == '_') || (codepoint == ':'));
1846 break;
1847 case XML_REGEXP_NOTNAMECHAR:
1848 neg = !neg;
1849 case XML_REGEXP_NAMECHAR:
William M. Brack871611b2003-10-18 04:53:14 +00001850 ret = (IS_LETTER(codepoint) || IS_DIGIT(codepoint) ||
Daniel Veillard4255d502002-04-16 15:50:10 +00001851 (codepoint == '.') || (codepoint == '-') ||
1852 (codepoint == '_') || (codepoint == ':') ||
William M. Brack871611b2003-10-18 04:53:14 +00001853 IS_COMBINING(codepoint) || IS_EXTENDER(codepoint));
Daniel Veillard4255d502002-04-16 15:50:10 +00001854 break;
1855 case XML_REGEXP_NOTDECIMAL:
1856 neg = !neg;
1857 case XML_REGEXP_DECIMAL:
1858 ret = xmlUCSIsCatNd(codepoint);
1859 break;
1860 case XML_REGEXP_REALCHAR:
1861 neg = !neg;
1862 case XML_REGEXP_NOTREALCHAR:
1863 ret = xmlUCSIsCatP(codepoint);
1864 if (ret == 0)
1865 ret = xmlUCSIsCatZ(codepoint);
1866 if (ret == 0)
1867 ret = xmlUCSIsCatC(codepoint);
1868 break;
1869 case XML_REGEXP_LETTER:
1870 ret = xmlUCSIsCatL(codepoint);
1871 break;
1872 case XML_REGEXP_LETTER_UPPERCASE:
1873 ret = xmlUCSIsCatLu(codepoint);
1874 break;
1875 case XML_REGEXP_LETTER_LOWERCASE:
1876 ret = xmlUCSIsCatLl(codepoint);
1877 break;
1878 case XML_REGEXP_LETTER_TITLECASE:
1879 ret = xmlUCSIsCatLt(codepoint);
1880 break;
1881 case XML_REGEXP_LETTER_MODIFIER:
1882 ret = xmlUCSIsCatLm(codepoint);
1883 break;
1884 case XML_REGEXP_LETTER_OTHERS:
1885 ret = xmlUCSIsCatLo(codepoint);
1886 break;
1887 case XML_REGEXP_MARK:
1888 ret = xmlUCSIsCatM(codepoint);
1889 break;
1890 case XML_REGEXP_MARK_NONSPACING:
1891 ret = xmlUCSIsCatMn(codepoint);
1892 break;
1893 case XML_REGEXP_MARK_SPACECOMBINING:
1894 ret = xmlUCSIsCatMc(codepoint);
1895 break;
1896 case XML_REGEXP_MARK_ENCLOSING:
1897 ret = xmlUCSIsCatMe(codepoint);
1898 break;
1899 case XML_REGEXP_NUMBER:
1900 ret = xmlUCSIsCatN(codepoint);
1901 break;
1902 case XML_REGEXP_NUMBER_DECIMAL:
1903 ret = xmlUCSIsCatNd(codepoint);
1904 break;
1905 case XML_REGEXP_NUMBER_LETTER:
1906 ret = xmlUCSIsCatNl(codepoint);
1907 break;
1908 case XML_REGEXP_NUMBER_OTHERS:
1909 ret = xmlUCSIsCatNo(codepoint);
1910 break;
1911 case XML_REGEXP_PUNCT:
1912 ret = xmlUCSIsCatP(codepoint);
1913 break;
1914 case XML_REGEXP_PUNCT_CONNECTOR:
1915 ret = xmlUCSIsCatPc(codepoint);
1916 break;
1917 case XML_REGEXP_PUNCT_DASH:
1918 ret = xmlUCSIsCatPd(codepoint);
1919 break;
1920 case XML_REGEXP_PUNCT_OPEN:
1921 ret = xmlUCSIsCatPs(codepoint);
1922 break;
1923 case XML_REGEXP_PUNCT_CLOSE:
1924 ret = xmlUCSIsCatPe(codepoint);
1925 break;
1926 case XML_REGEXP_PUNCT_INITQUOTE:
1927 ret = xmlUCSIsCatPi(codepoint);
1928 break;
1929 case XML_REGEXP_PUNCT_FINQUOTE:
1930 ret = xmlUCSIsCatPf(codepoint);
1931 break;
1932 case XML_REGEXP_PUNCT_OTHERS:
1933 ret = xmlUCSIsCatPo(codepoint);
1934 break;
1935 case XML_REGEXP_SEPAR:
1936 ret = xmlUCSIsCatZ(codepoint);
1937 break;
1938 case XML_REGEXP_SEPAR_SPACE:
1939 ret = xmlUCSIsCatZs(codepoint);
1940 break;
1941 case XML_REGEXP_SEPAR_LINE:
1942 ret = xmlUCSIsCatZl(codepoint);
1943 break;
1944 case XML_REGEXP_SEPAR_PARA:
1945 ret = xmlUCSIsCatZp(codepoint);
1946 break;
1947 case XML_REGEXP_SYMBOL:
1948 ret = xmlUCSIsCatS(codepoint);
1949 break;
1950 case XML_REGEXP_SYMBOL_MATH:
1951 ret = xmlUCSIsCatSm(codepoint);
1952 break;
1953 case XML_REGEXP_SYMBOL_CURRENCY:
1954 ret = xmlUCSIsCatSc(codepoint);
1955 break;
1956 case XML_REGEXP_SYMBOL_MODIFIER:
1957 ret = xmlUCSIsCatSk(codepoint);
1958 break;
1959 case XML_REGEXP_SYMBOL_OTHERS:
1960 ret = xmlUCSIsCatSo(codepoint);
1961 break;
1962 case XML_REGEXP_OTHER:
1963 ret = xmlUCSIsCatC(codepoint);
1964 break;
1965 case XML_REGEXP_OTHER_CONTROL:
1966 ret = xmlUCSIsCatCc(codepoint);
1967 break;
1968 case XML_REGEXP_OTHER_FORMAT:
1969 ret = xmlUCSIsCatCf(codepoint);
1970 break;
1971 case XML_REGEXP_OTHER_PRIVATE:
1972 ret = xmlUCSIsCatCo(codepoint);
1973 break;
1974 case XML_REGEXP_OTHER_NA:
1975 /* ret = xmlUCSIsCatCn(codepoint); */
1976 /* Seems it doesn't exist anymore in recent Unicode releases */
1977 ret = 0;
1978 break;
1979 case XML_REGEXP_BLOCK_NAME:
1980 ret = xmlUCSIsBlock(codepoint, (const char *) blockName);
1981 break;
1982 }
1983 if (neg)
1984 return(!ret);
1985 return(ret);
1986}
1987
1988static int
1989xmlRegCheckCharacter(xmlRegAtomPtr atom, int codepoint) {
1990 int i, ret = 0;
1991 xmlRegRangePtr range;
1992
William M. Brack871611b2003-10-18 04:53:14 +00001993 if ((atom == NULL) || (!IS_CHAR(codepoint)))
Daniel Veillard4255d502002-04-16 15:50:10 +00001994 return(-1);
1995
1996 switch (atom->type) {
1997 case XML_REGEXP_SUBREG:
1998 case XML_REGEXP_EPSILON:
1999 return(-1);
2000 case XML_REGEXP_CHARVAL:
2001 return(codepoint == atom->codepoint);
2002 case XML_REGEXP_RANGES: {
2003 int accept = 0;
Daniel Veillardf2a12832003-11-24 13:04:35 +00002004
Daniel Veillard4255d502002-04-16 15:50:10 +00002005 for (i = 0;i < atom->nbRanges;i++) {
2006 range = atom->ranges[i];
Daniel Veillardf8b9de32003-11-24 14:27:26 +00002007 if (range->neg == 2) {
Daniel Veillard4255d502002-04-16 15:50:10 +00002008 ret = xmlRegCheckCharacterRange(range->type, codepoint,
2009 0, range->start, range->end,
2010 range->blockName);
2011 if (ret != 0)
2012 return(0); /* excluded char */
Daniel Veillardf8b9de32003-11-24 14:27:26 +00002013 } else if (range->neg) {
2014 ret = xmlRegCheckCharacterRange(range->type, codepoint,
2015 0, range->start, range->end,
2016 range->blockName);
2017 if (ret == 0)
Daniel Veillardf2a12832003-11-24 13:04:35 +00002018 accept = 1;
Daniel Veillardf8b9de32003-11-24 14:27:26 +00002019 else
2020 return(0);
Daniel Veillard4255d502002-04-16 15:50:10 +00002021 } else {
2022 ret = xmlRegCheckCharacterRange(range->type, codepoint,
2023 0, range->start, range->end,
2024 range->blockName);
2025 if (ret != 0)
2026 accept = 1; /* might still be excluded */
2027 }
2028 }
2029 return(accept);
2030 }
2031 case XML_REGEXP_STRING:
2032 printf("TODO: XML_REGEXP_STRING\n");
2033 return(-1);
2034 case XML_REGEXP_ANYCHAR:
2035 case XML_REGEXP_ANYSPACE:
2036 case XML_REGEXP_NOTSPACE:
2037 case XML_REGEXP_INITNAME:
2038 case XML_REGEXP_NOTINITNAME:
2039 case XML_REGEXP_NAMECHAR:
2040 case XML_REGEXP_NOTNAMECHAR:
2041 case XML_REGEXP_DECIMAL:
2042 case XML_REGEXP_NOTDECIMAL:
2043 case XML_REGEXP_REALCHAR:
2044 case XML_REGEXP_NOTREALCHAR:
2045 case XML_REGEXP_LETTER:
2046 case XML_REGEXP_LETTER_UPPERCASE:
2047 case XML_REGEXP_LETTER_LOWERCASE:
2048 case XML_REGEXP_LETTER_TITLECASE:
2049 case XML_REGEXP_LETTER_MODIFIER:
2050 case XML_REGEXP_LETTER_OTHERS:
2051 case XML_REGEXP_MARK:
2052 case XML_REGEXP_MARK_NONSPACING:
2053 case XML_REGEXP_MARK_SPACECOMBINING:
2054 case XML_REGEXP_MARK_ENCLOSING:
2055 case XML_REGEXP_NUMBER:
2056 case XML_REGEXP_NUMBER_DECIMAL:
2057 case XML_REGEXP_NUMBER_LETTER:
2058 case XML_REGEXP_NUMBER_OTHERS:
2059 case XML_REGEXP_PUNCT:
2060 case XML_REGEXP_PUNCT_CONNECTOR:
2061 case XML_REGEXP_PUNCT_DASH:
2062 case XML_REGEXP_PUNCT_OPEN:
2063 case XML_REGEXP_PUNCT_CLOSE:
2064 case XML_REGEXP_PUNCT_INITQUOTE:
2065 case XML_REGEXP_PUNCT_FINQUOTE:
2066 case XML_REGEXP_PUNCT_OTHERS:
2067 case XML_REGEXP_SEPAR:
2068 case XML_REGEXP_SEPAR_SPACE:
2069 case XML_REGEXP_SEPAR_LINE:
2070 case XML_REGEXP_SEPAR_PARA:
2071 case XML_REGEXP_SYMBOL:
2072 case XML_REGEXP_SYMBOL_MATH:
2073 case XML_REGEXP_SYMBOL_CURRENCY:
2074 case XML_REGEXP_SYMBOL_MODIFIER:
2075 case XML_REGEXP_SYMBOL_OTHERS:
2076 case XML_REGEXP_OTHER:
2077 case XML_REGEXP_OTHER_CONTROL:
2078 case XML_REGEXP_OTHER_FORMAT:
2079 case XML_REGEXP_OTHER_PRIVATE:
2080 case XML_REGEXP_OTHER_NA:
2081 case XML_REGEXP_BLOCK_NAME:
2082 ret = xmlRegCheckCharacterRange(atom->type, codepoint, 0, 0, 0,
2083 (const xmlChar *)atom->valuep);
2084 if (atom->neg)
2085 ret = !ret;
2086 break;
2087 }
2088 return(ret);
2089}
2090
2091/************************************************************************
2092 * *
2093 * Saving an restoring state of an execution context *
2094 * *
2095 ************************************************************************/
2096
2097#ifdef DEBUG_REGEXP_EXEC
2098static void
2099xmlFARegDebugExec(xmlRegExecCtxtPtr exec) {
2100 printf("state: %d:%d:idx %d", exec->state->no, exec->transno, exec->index);
2101 if (exec->inputStack != NULL) {
2102 int i;
2103 printf(": ");
2104 for (i = 0;(i < 3) && (i < exec->inputStackNr);i++)
2105 printf("%s ", exec->inputStack[exec->inputStackNr - (i + 1)]);
2106 } else {
2107 printf(": %s", &(exec->inputString[exec->index]));
2108 }
2109 printf("\n");
2110}
2111#endif
2112
2113static void
2114xmlFARegExecSave(xmlRegExecCtxtPtr exec) {
2115#ifdef DEBUG_REGEXP_EXEC
2116 printf("saving ");
2117 exec->transno++;
2118 xmlFARegDebugExec(exec);
2119 exec->transno--;
2120#endif
2121
2122 if (exec->maxRollbacks == 0) {
2123 exec->maxRollbacks = 4;
2124 exec->rollbacks = (xmlRegExecRollback *) xmlMalloc(exec->maxRollbacks *
2125 sizeof(xmlRegExecRollback));
2126 if (exec->rollbacks == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00002127 xmlRegexpErrMemory(NULL, "saving regexp");
Daniel Veillard4255d502002-04-16 15:50:10 +00002128 exec->maxRollbacks = 0;
2129 return;
2130 }
2131 memset(exec->rollbacks, 0,
2132 exec->maxRollbacks * sizeof(xmlRegExecRollback));
2133 } else if (exec->nbRollbacks >= exec->maxRollbacks) {
2134 xmlRegExecRollback *tmp;
2135 int len = exec->maxRollbacks;
2136
2137 exec->maxRollbacks *= 2;
2138 tmp = (xmlRegExecRollback *) xmlRealloc(exec->rollbacks,
2139 exec->maxRollbacks * sizeof(xmlRegExecRollback));
2140 if (tmp == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00002141 xmlRegexpErrMemory(NULL, "saving regexp");
Daniel Veillard4255d502002-04-16 15:50:10 +00002142 exec->maxRollbacks /= 2;
2143 return;
2144 }
2145 exec->rollbacks = tmp;
2146 tmp = &exec->rollbacks[len];
2147 memset(tmp, 0, (exec->maxRollbacks - len) * sizeof(xmlRegExecRollback));
2148 }
2149 exec->rollbacks[exec->nbRollbacks].state = exec->state;
2150 exec->rollbacks[exec->nbRollbacks].index = exec->index;
2151 exec->rollbacks[exec->nbRollbacks].nextbranch = exec->transno + 1;
2152 if (exec->comp->nbCounters > 0) {
2153 if (exec->rollbacks[exec->nbRollbacks].counts == NULL) {
2154 exec->rollbacks[exec->nbRollbacks].counts = (int *)
2155 xmlMalloc(exec->comp->nbCounters * sizeof(int));
2156 if (exec->rollbacks[exec->nbRollbacks].counts == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00002157 xmlRegexpErrMemory(NULL, "saving regexp");
Daniel Veillard4255d502002-04-16 15:50:10 +00002158 exec->status = -5;
2159 return;
2160 }
2161 }
2162 memcpy(exec->rollbacks[exec->nbRollbacks].counts, exec->counts,
2163 exec->comp->nbCounters * sizeof(int));
2164 }
2165 exec->nbRollbacks++;
2166}
2167
2168static void
2169xmlFARegExecRollBack(xmlRegExecCtxtPtr exec) {
2170 if (exec->nbRollbacks <= 0) {
2171 exec->status = -1;
2172#ifdef DEBUG_REGEXP_EXEC
2173 printf("rollback failed on empty stack\n");
2174#endif
2175 return;
2176 }
2177 exec->nbRollbacks--;
2178 exec->state = exec->rollbacks[exec->nbRollbacks].state;
2179 exec->index = exec->rollbacks[exec->nbRollbacks].index;
2180 exec->transno = exec->rollbacks[exec->nbRollbacks].nextbranch;
2181 if (exec->comp->nbCounters > 0) {
2182 if (exec->rollbacks[exec->nbRollbacks].counts == NULL) {
2183 fprintf(stderr, "exec save: allocation failed");
2184 exec->status = -6;
2185 return;
2186 }
2187 memcpy(exec->counts, exec->rollbacks[exec->nbRollbacks].counts,
2188 exec->comp->nbCounters * sizeof(int));
2189 }
2190
2191#ifdef DEBUG_REGEXP_EXEC
2192 printf("restored ");
2193 xmlFARegDebugExec(exec);
2194#endif
2195}
2196
2197/************************************************************************
2198 * *
2199 * Verifyer, running an input against a compiled regexp *
2200 * *
2201 ************************************************************************/
2202
2203static int
2204xmlFARegExec(xmlRegexpPtr comp, const xmlChar *content) {
2205 xmlRegExecCtxt execval;
2206 xmlRegExecCtxtPtr exec = &execval;
2207 int ret, codepoint, len;
2208
2209 exec->inputString = content;
2210 exec->index = 0;
2211 exec->determinist = 1;
2212 exec->maxRollbacks = 0;
2213 exec->nbRollbacks = 0;
2214 exec->rollbacks = NULL;
2215 exec->status = 0;
2216 exec->comp = comp;
2217 exec->state = comp->states[0];
2218 exec->transno = 0;
2219 exec->transcount = 0;
Daniel Veillardf2a12832003-11-24 13:04:35 +00002220 exec->inputStack = NULL;
2221 exec->inputStackMax = 0;
Daniel Veillard4255d502002-04-16 15:50:10 +00002222 if (comp->nbCounters > 0) {
2223 exec->counts = (int *) xmlMalloc(comp->nbCounters * sizeof(int));
Daniel Veillardff46a042003-10-08 08:53:17 +00002224 if (exec->counts == NULL) {
2225 xmlRegexpErrMemory(NULL, "running regexp");
Daniel Veillard4255d502002-04-16 15:50:10 +00002226 return(-1);
Daniel Veillardff46a042003-10-08 08:53:17 +00002227 }
Daniel Veillard4255d502002-04-16 15:50:10 +00002228 memset(exec->counts, 0, comp->nbCounters * sizeof(int));
2229 } else
2230 exec->counts = NULL;
2231 while ((exec->status == 0) &&
2232 ((exec->inputString[exec->index] != 0) ||
2233 (exec->state->type != XML_REGEXP_FINAL_STATE))) {
2234 xmlRegTransPtr trans;
2235 xmlRegAtomPtr atom;
2236
2237 /*
2238 * End of input on non-terminal state, rollback, however we may
2239 * still have epsilon like transition for counted transitions
2240 * on counters, in that case don't break too early.
2241 */
2242 if ((exec->inputString[exec->index] == 0) && (exec->counts == NULL))
2243 goto rollback;
2244
2245 exec->transcount = 0;
2246 for (;exec->transno < exec->state->nbTrans;exec->transno++) {
2247 trans = &exec->state->trans[exec->transno];
2248 if (trans->to < 0)
2249 continue;
2250 atom = trans->atom;
2251 ret = 0;
2252 if (trans->count >= 0) {
2253 int count;
2254 xmlRegCounterPtr counter;
2255
2256 /*
2257 * A counted transition.
2258 */
2259
2260 count = exec->counts[trans->count];
2261 counter = &exec->comp->counters[trans->count];
2262#ifdef DEBUG_REGEXP_EXEC
2263 printf("testing count %d: val %d, min %d, max %d\n",
2264 trans->count, count, counter->min, counter->max);
2265#endif
2266 ret = ((count >= counter->min) && (count <= counter->max));
2267 } else if (atom == NULL) {
2268 fprintf(stderr, "epsilon transition left at runtime\n");
2269 exec->status = -2;
2270 break;
2271 } else if (exec->inputString[exec->index] != 0) {
2272 codepoint = CUR_SCHAR(&(exec->inputString[exec->index]), len);
2273 ret = xmlRegCheckCharacter(atom, codepoint);
2274 if ((ret == 1) && (atom->min > 0) && (atom->max > 0)) {
2275 xmlRegStatePtr to = comp->states[trans->to];
2276
2277 /*
2278 * this is a multiple input sequence
2279 */
2280 if (exec->state->nbTrans > exec->transno + 1) {
2281 xmlFARegExecSave(exec);
2282 }
2283 exec->transcount = 1;
2284 do {
2285 /*
2286 * Try to progress as much as possible on the input
2287 */
2288 if (exec->transcount == atom->max) {
2289 break;
2290 }
2291 exec->index += len;
2292 /*
2293 * End of input: stop here
2294 */
2295 if (exec->inputString[exec->index] == 0) {
2296 exec->index -= len;
2297 break;
2298 }
2299 if (exec->transcount >= atom->min) {
2300 int transno = exec->transno;
2301 xmlRegStatePtr state = exec->state;
2302
2303 /*
2304 * The transition is acceptable save it
2305 */
2306 exec->transno = -1; /* trick */
2307 exec->state = to;
2308 xmlFARegExecSave(exec);
2309 exec->transno = transno;
2310 exec->state = state;
2311 }
2312 codepoint = CUR_SCHAR(&(exec->inputString[exec->index]),
2313 len);
2314 ret = xmlRegCheckCharacter(atom, codepoint);
2315 exec->transcount++;
2316 } while (ret == 1);
2317 if (exec->transcount < atom->min)
2318 ret = 0;
2319
2320 /*
2321 * If the last check failed but one transition was found
2322 * possible, rollback
2323 */
2324 if (ret < 0)
2325 ret = 0;
2326 if (ret == 0) {
2327 goto rollback;
2328 }
2329 }
2330 }
2331 if (ret == 1) {
2332 if (exec->state->nbTrans > exec->transno + 1) {
2333 xmlFARegExecSave(exec);
2334 }
2335 if (trans->counter >= 0) {
2336#ifdef DEBUG_REGEXP_EXEC
2337 printf("Increasing count %d\n", trans->counter);
2338#endif
2339 exec->counts[trans->counter]++;
2340 }
2341#ifdef DEBUG_REGEXP_EXEC
2342 printf("entering state %d\n", trans->to);
2343#endif
2344 exec->state = comp->states[trans->to];
2345 exec->transno = 0;
2346 if (trans->atom != NULL) {
2347 exec->index += len;
2348 }
2349 goto progress;
2350 } else if (ret < 0) {
2351 exec->status = -4;
2352 break;
2353 }
2354 }
2355 if ((exec->transno != 0) || (exec->state->nbTrans == 0)) {
2356rollback:
2357 /*
2358 * Failed to find a way out
2359 */
2360 exec->determinist = 0;
2361 xmlFARegExecRollBack(exec);
2362 }
2363progress:
2364 continue;
2365 }
2366 if (exec->rollbacks != NULL) {
2367 if (exec->counts != NULL) {
2368 int i;
2369
2370 for (i = 0;i < exec->maxRollbacks;i++)
2371 if (exec->rollbacks[i].counts != NULL)
2372 xmlFree(exec->rollbacks[i].counts);
2373 }
2374 xmlFree(exec->rollbacks);
2375 }
2376 if (exec->counts != NULL)
2377 xmlFree(exec->counts);
2378 if (exec->status == 0)
2379 return(1);
2380 if (exec->status == -1)
2381 return(0);
2382 return(exec->status);
2383}
2384
2385/************************************************************************
2386 * *
2387 * Progressive interface to the verifyer one atom at a time *
2388 * *
2389 ************************************************************************/
2390
2391/**
Daniel Veillard01c13b52002-12-10 15:19:08 +00002392 * xmlRegNewExecCtxt:
Daniel Veillard4255d502002-04-16 15:50:10 +00002393 * @comp: a precompiled regular expression
2394 * @callback: a callback function used for handling progresses in the
2395 * automata matching phase
2396 * @data: the context data associated to the callback in this context
2397 *
2398 * Build a context used for progressive evaluation of a regexp.
Daniel Veillard01c13b52002-12-10 15:19:08 +00002399 *
2400 * Returns the new context
Daniel Veillard4255d502002-04-16 15:50:10 +00002401 */
2402xmlRegExecCtxtPtr
2403xmlRegNewExecCtxt(xmlRegexpPtr comp, xmlRegExecCallbacks callback, void *data) {
2404 xmlRegExecCtxtPtr exec;
2405
2406 if (comp == NULL)
2407 return(NULL);
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00002408 if ((comp->compact == NULL) && (comp->states == NULL))
2409 return(NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +00002410 exec = (xmlRegExecCtxtPtr) xmlMalloc(sizeof(xmlRegExecCtxt));
2411 if (exec == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00002412 xmlRegexpErrMemory(NULL, "creating execution context");
Daniel Veillard4255d502002-04-16 15:50:10 +00002413 return(NULL);
2414 }
2415 memset(exec, 0, sizeof(xmlRegExecCtxt));
2416 exec->inputString = NULL;
2417 exec->index = 0;
2418 exec->determinist = 1;
2419 exec->maxRollbacks = 0;
2420 exec->nbRollbacks = 0;
2421 exec->rollbacks = NULL;
2422 exec->status = 0;
2423 exec->comp = comp;
Daniel Veillard23e73572002-09-19 19:56:43 +00002424 if (comp->compact == NULL)
2425 exec->state = comp->states[0];
Daniel Veillard4255d502002-04-16 15:50:10 +00002426 exec->transno = 0;
2427 exec->transcount = 0;
2428 exec->callback = callback;
2429 exec->data = data;
2430 if (comp->nbCounters > 0) {
2431 exec->counts = (int *) xmlMalloc(comp->nbCounters * sizeof(int));
2432 if (exec->counts == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00002433 xmlRegexpErrMemory(NULL, "creating execution context");
Daniel Veillard4255d502002-04-16 15:50:10 +00002434 xmlFree(exec);
2435 return(NULL);
2436 }
2437 memset(exec->counts, 0, comp->nbCounters * sizeof(int));
2438 } else
2439 exec->counts = NULL;
2440 exec->inputStackMax = 0;
2441 exec->inputStackNr = 0;
2442 exec->inputStack = NULL;
2443 return(exec);
2444}
2445
2446/**
2447 * xmlRegFreeExecCtxt:
2448 * @exec: a regular expression evaulation context
2449 *
2450 * Free the structures associated to a regular expression evaulation context.
2451 */
2452void
2453xmlRegFreeExecCtxt(xmlRegExecCtxtPtr exec) {
2454 if (exec == NULL)
2455 return;
2456
2457 if (exec->rollbacks != NULL) {
2458 if (exec->counts != NULL) {
2459 int i;
2460
2461 for (i = 0;i < exec->maxRollbacks;i++)
2462 if (exec->rollbacks[i].counts != NULL)
2463 xmlFree(exec->rollbacks[i].counts);
2464 }
2465 xmlFree(exec->rollbacks);
2466 }
2467 if (exec->counts != NULL)
2468 xmlFree(exec->counts);
2469 if (exec->inputStack != NULL) {
2470 int i;
2471
Daniel Veillard32370232002-10-16 14:08:14 +00002472 for (i = 0;i < exec->inputStackNr;i++) {
2473 if (exec->inputStack[i].value != NULL)
2474 xmlFree(exec->inputStack[i].value);
2475 }
Daniel Veillard4255d502002-04-16 15:50:10 +00002476 xmlFree(exec->inputStack);
2477 }
2478 xmlFree(exec);
2479}
2480
2481static void
2482xmlFARegExecSaveInputString(xmlRegExecCtxtPtr exec, const xmlChar *value,
2483 void *data) {
2484#ifdef DEBUG_PUSH
2485 printf("saving value: %d:%s\n", exec->inputStackNr, value);
2486#endif
2487 if (exec->inputStackMax == 0) {
2488 exec->inputStackMax = 4;
2489 exec->inputStack = (xmlRegInputTokenPtr)
2490 xmlMalloc(exec->inputStackMax * sizeof(xmlRegInputToken));
2491 if (exec->inputStack == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00002492 xmlRegexpErrMemory(NULL, "pushing input string");
Daniel Veillard4255d502002-04-16 15:50:10 +00002493 exec->inputStackMax = 0;
2494 return;
2495 }
2496 } else if (exec->inputStackNr + 1 >= exec->inputStackMax) {
2497 xmlRegInputTokenPtr tmp;
2498
2499 exec->inputStackMax *= 2;
2500 tmp = (xmlRegInputTokenPtr) xmlRealloc(exec->inputStack,
2501 exec->inputStackMax * sizeof(xmlRegInputToken));
2502 if (tmp == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00002503 xmlRegexpErrMemory(NULL, "pushing input string");
Daniel Veillard4255d502002-04-16 15:50:10 +00002504 exec->inputStackMax /= 2;
2505 return;
2506 }
2507 exec->inputStack = tmp;
2508 }
2509 exec->inputStack[exec->inputStackNr].value = xmlStrdup(value);
2510 exec->inputStack[exec->inputStackNr].data = data;
2511 exec->inputStackNr++;
2512 exec->inputStack[exec->inputStackNr].value = NULL;
2513 exec->inputStack[exec->inputStackNr].data = NULL;
2514}
2515
2516
2517/**
Daniel Veillard23e73572002-09-19 19:56:43 +00002518 * xmlRegCompactPushString:
2519 * @exec: a regexp execution context
2520 * @comp: the precompiled exec with a compact table
2521 * @value: a string token input
2522 * @data: data associated to the token to reuse in callbacks
2523 *
2524 * Push one input token in the execution context
2525 *
2526 * Returns: 1 if the regexp reached a final state, 0 if non-final, and
2527 * a negative value in case of error.
2528 */
2529static int
2530xmlRegCompactPushString(xmlRegExecCtxtPtr exec,
2531 xmlRegexpPtr comp,
2532 const xmlChar *value,
2533 void *data) {
2534 int state = exec->index;
2535 int i, target;
2536
2537 if ((comp == NULL) || (comp->compact == NULL) || (comp->stringMap == NULL))
2538 return(-1);
2539
2540 if (value == NULL) {
2541 /*
2542 * are we at a final state ?
2543 */
2544 if (comp->compact[state * (comp->nbstrings + 1)] ==
2545 XML_REGEXP_FINAL_STATE)
2546 return(1);
2547 return(0);
2548 }
2549
2550#ifdef DEBUG_PUSH
2551 printf("value pushed: %s\n", value);
2552#endif
2553
2554 /*
2555 * Examine all outside transition from current state
2556 */
2557 for (i = 0;i < comp->nbstrings;i++) {
2558 target = comp->compact[state * (comp->nbstrings + 1) + i + 1];
2559 if ((target > 0) && (target <= comp->nbstates)) {
2560 target--; /* to avoid 0 */
2561 if (xmlStrEqual(comp->stringMap[i], value)) {
2562 exec->index = target;
Daniel Veillard118aed72002-09-24 14:13:13 +00002563 if ((exec->callback != NULL) && (comp->transdata != NULL)) {
2564 exec->callback(exec->data, value,
2565 comp->transdata[state * comp->nbstrings + i], data);
2566 }
Daniel Veillard23e73572002-09-19 19:56:43 +00002567#ifdef DEBUG_PUSH
2568 printf("entering state %d\n", target);
2569#endif
2570 if (comp->compact[target * (comp->nbstrings + 1)] ==
2571 XML_REGEXP_FINAL_STATE)
2572 return(1);
2573 return(0);
2574 }
2575 }
2576 }
2577 /*
2578 * Failed to find an exit transition out from current state for the
2579 * current token
2580 */
2581#ifdef DEBUG_PUSH
2582 printf("failed to find a transition for %s on state %d\n", value, state);
2583#endif
2584 exec->status = -1;
2585 return(-1);
2586}
2587
2588/**
Daniel Veillard4255d502002-04-16 15:50:10 +00002589 * xmlRegExecPushString:
Daniel Veillardea7751d2002-12-20 00:16:24 +00002590 * @exec: a regexp execution context or NULL to indicate the end
Daniel Veillard4255d502002-04-16 15:50:10 +00002591 * @value: a string token input
2592 * @data: data associated to the token to reuse in callbacks
2593 *
2594 * Push one input token in the execution context
2595 *
2596 * Returns: 1 if the regexp reached a final state, 0 if non-final, and
2597 * a negative value in case of error.
2598 */
2599int
2600xmlRegExecPushString(xmlRegExecCtxtPtr exec, const xmlChar *value,
2601 void *data) {
2602 xmlRegTransPtr trans;
2603 xmlRegAtomPtr atom;
2604 int ret;
2605 int final = 0;
2606
2607 if (exec == NULL)
2608 return(-1);
Daniel Veillard23e73572002-09-19 19:56:43 +00002609 if (exec->comp == NULL)
2610 return(-1);
Daniel Veillard4255d502002-04-16 15:50:10 +00002611 if (exec->status != 0)
2612 return(exec->status);
2613
Daniel Veillard23e73572002-09-19 19:56:43 +00002614 if (exec->comp->compact != NULL)
2615 return(xmlRegCompactPushString(exec, exec->comp, value, data));
2616
Daniel Veillard4255d502002-04-16 15:50:10 +00002617 if (value == NULL) {
2618 if (exec->state->type == XML_REGEXP_FINAL_STATE)
2619 return(1);
2620 final = 1;
2621 }
2622
2623#ifdef DEBUG_PUSH
2624 printf("value pushed: %s\n", value);
2625#endif
2626 /*
2627 * If we have an active rollback stack push the new value there
2628 * and get back to where we were left
2629 */
2630 if ((value != NULL) && (exec->inputStackNr > 0)) {
2631 xmlFARegExecSaveInputString(exec, value, data);
2632 value = exec->inputStack[exec->index].value;
2633 data = exec->inputStack[exec->index].data;
2634#ifdef DEBUG_PUSH
2635 printf("value loaded: %s\n", value);
2636#endif
2637 }
2638
2639 while ((exec->status == 0) &&
2640 ((value != NULL) ||
2641 ((final == 1) &&
2642 (exec->state->type != XML_REGEXP_FINAL_STATE)))) {
2643
2644 /*
2645 * End of input on non-terminal state, rollback, however we may
2646 * still have epsilon like transition for counted transitions
2647 * on counters, in that case don't break too early.
2648 */
Daniel Veillardb509f152002-04-17 16:28:10 +00002649 if ((value == NULL) && (exec->counts == NULL))
Daniel Veillard4255d502002-04-16 15:50:10 +00002650 goto rollback;
2651
2652 exec->transcount = 0;
2653 for (;exec->transno < exec->state->nbTrans;exec->transno++) {
2654 trans = &exec->state->trans[exec->transno];
2655 if (trans->to < 0)
2656 continue;
2657 atom = trans->atom;
2658 ret = 0;
Daniel Veillard441bc322002-04-20 17:38:48 +00002659 if (trans->count == REGEXP_ALL_LAX_COUNTER) {
2660 int i;
2661 int count;
2662 xmlRegTransPtr t;
2663 xmlRegCounterPtr counter;
2664
2665 ret = 0;
2666
2667#ifdef DEBUG_PUSH
2668 printf("testing all lax %d\n", trans->count);
2669#endif
2670 /*
2671 * Check all counted transitions from the current state
2672 */
2673 if ((value == NULL) && (final)) {
2674 ret = 1;
2675 } else if (value != NULL) {
2676 for (i = 0;i < exec->state->nbTrans;i++) {
2677 t = &exec->state->trans[i];
2678 if ((t->counter < 0) || (t == trans))
2679 continue;
2680 counter = &exec->comp->counters[t->counter];
2681 count = exec->counts[t->counter];
2682 if ((count < counter->max) &&
2683 (t->atom != NULL) &&
2684 (xmlStrEqual(value, t->atom->valuep))) {
2685 ret = 0;
2686 break;
2687 }
2688 if ((count >= counter->min) &&
2689 (count < counter->max) &&
2690 (xmlStrEqual(value, t->atom->valuep))) {
2691 ret = 1;
2692 break;
2693 }
2694 }
2695 }
2696 } else if (trans->count == REGEXP_ALL_COUNTER) {
Daniel Veillard8a001f62002-04-20 07:24:11 +00002697 int i;
2698 int count;
2699 xmlRegTransPtr t;
2700 xmlRegCounterPtr counter;
2701
2702 ret = 1;
2703
2704#ifdef DEBUG_PUSH
2705 printf("testing all %d\n", trans->count);
2706#endif
2707 /*
2708 * Check all counted transitions from the current state
2709 */
2710 for (i = 0;i < exec->state->nbTrans;i++) {
2711 t = &exec->state->trans[i];
2712 if ((t->counter < 0) || (t == trans))
2713 continue;
2714 counter = &exec->comp->counters[t->counter];
2715 count = exec->counts[t->counter];
2716 if ((count < counter->min) || (count > counter->max)) {
2717 ret = 0;
2718 break;
2719 }
2720 }
2721 } else if (trans->count >= 0) {
Daniel Veillard4255d502002-04-16 15:50:10 +00002722 int count;
2723 xmlRegCounterPtr counter;
2724
2725 /*
2726 * A counted transition.
2727 */
2728
2729 count = exec->counts[trans->count];
2730 counter = &exec->comp->counters[trans->count];
2731#ifdef DEBUG_PUSH
2732 printf("testing count %d: val %d, min %d, max %d\n",
2733 trans->count, count, counter->min, counter->max);
2734#endif
2735 ret = ((count >= counter->min) && (count <= counter->max));
2736 } else if (atom == NULL) {
2737 fprintf(stderr, "epsilon transition left at runtime\n");
2738 exec->status = -2;
2739 break;
2740 } else if (value != NULL) {
2741 ret = xmlStrEqual(value, atom->valuep);
Daniel Veillard441bc322002-04-20 17:38:48 +00002742 if ((ret == 1) && (trans->counter >= 0)) {
2743 xmlRegCounterPtr counter;
2744 int count;
2745
2746 count = exec->counts[trans->counter];
2747 counter = &exec->comp->counters[trans->counter];
2748 if (count >= counter->max)
2749 ret = 0;
2750 }
2751
Daniel Veillard4255d502002-04-16 15:50:10 +00002752 if ((ret == 1) && (atom->min > 0) && (atom->max > 0)) {
2753 xmlRegStatePtr to = exec->comp->states[trans->to];
2754
2755 /*
2756 * this is a multiple input sequence
2757 */
2758 if (exec->state->nbTrans > exec->transno + 1) {
2759 if (exec->inputStackNr <= 0) {
2760 xmlFARegExecSaveInputString(exec, value, data);
2761 }
2762 xmlFARegExecSave(exec);
2763 }
2764 exec->transcount = 1;
2765 do {
2766 /*
2767 * Try to progress as much as possible on the input
2768 */
2769 if (exec->transcount == atom->max) {
2770 break;
2771 }
2772 exec->index++;
2773 value = exec->inputStack[exec->index].value;
2774 data = exec->inputStack[exec->index].data;
2775#ifdef DEBUG_PUSH
2776 printf("value loaded: %s\n", value);
2777#endif
2778
2779 /*
2780 * End of input: stop here
2781 */
2782 if (value == NULL) {
2783 exec->index --;
2784 break;
2785 }
2786 if (exec->transcount >= atom->min) {
2787 int transno = exec->transno;
2788 xmlRegStatePtr state = exec->state;
2789
2790 /*
2791 * The transition is acceptable save it
2792 */
2793 exec->transno = -1; /* trick */
2794 exec->state = to;
2795 if (exec->inputStackNr <= 0) {
2796 xmlFARegExecSaveInputString(exec, value, data);
2797 }
2798 xmlFARegExecSave(exec);
2799 exec->transno = transno;
2800 exec->state = state;
2801 }
2802 ret = xmlStrEqual(value, atom->valuep);
2803 exec->transcount++;
2804 } while (ret == 1);
2805 if (exec->transcount < atom->min)
2806 ret = 0;
2807
2808 /*
2809 * If the last check failed but one transition was found
2810 * possible, rollback
2811 */
2812 if (ret < 0)
2813 ret = 0;
2814 if (ret == 0) {
2815 goto rollback;
2816 }
2817 }
2818 }
2819 if (ret == 1) {
William M. Brack98873952003-12-26 06:03:14 +00002820 if ((exec->callback != NULL) && (atom != NULL) &&
2821 (data != NULL)) {
Daniel Veillard4255d502002-04-16 15:50:10 +00002822 exec->callback(exec->data, atom->valuep,
2823 atom->data, data);
2824 }
2825 if (exec->state->nbTrans > exec->transno + 1) {
2826 if (exec->inputStackNr <= 0) {
2827 xmlFARegExecSaveInputString(exec, value, data);
2828 }
2829 xmlFARegExecSave(exec);
2830 }
2831 if (trans->counter >= 0) {
2832#ifdef DEBUG_PUSH
2833 printf("Increasing count %d\n", trans->counter);
2834#endif
2835 exec->counts[trans->counter]++;
2836 }
2837#ifdef DEBUG_PUSH
2838 printf("entering state %d\n", trans->to);
2839#endif
2840 exec->state = exec->comp->states[trans->to];
2841 exec->transno = 0;
2842 if (trans->atom != NULL) {
2843 if (exec->inputStack != NULL) {
2844 exec->index++;
2845 if (exec->index < exec->inputStackNr) {
2846 value = exec->inputStack[exec->index].value;
2847 data = exec->inputStack[exec->index].data;
2848#ifdef DEBUG_PUSH
2849 printf("value loaded: %s\n", value);
2850#endif
2851 } else {
2852 value = NULL;
2853 data = NULL;
2854#ifdef DEBUG_PUSH
2855 printf("end of input\n");
2856#endif
2857 }
2858 } else {
2859 value = NULL;
2860 data = NULL;
2861#ifdef DEBUG_PUSH
2862 printf("end of input\n");
2863#endif
2864 }
2865 }
2866 goto progress;
2867 } else if (ret < 0) {
2868 exec->status = -4;
2869 break;
2870 }
2871 }
2872 if ((exec->transno != 0) || (exec->state->nbTrans == 0)) {
2873rollback:
2874 /*
2875 * Failed to find a way out
2876 */
2877 exec->determinist = 0;
2878 xmlFARegExecRollBack(exec);
2879 if (exec->status == 0) {
2880 value = exec->inputStack[exec->index].value;
2881 data = exec->inputStack[exec->index].data;
2882#ifdef DEBUG_PUSH
2883 printf("value loaded: %s\n", value);
2884#endif
2885 }
2886 }
2887progress:
2888 continue;
2889 }
2890 if (exec->status == 0) {
2891 return(exec->state->type == XML_REGEXP_FINAL_STATE);
2892 }
2893 return(exec->status);
2894}
2895
Daniel Veillard52b48c72003-04-13 19:53:42 +00002896/**
2897 * xmlRegExecPushString2:
2898 * @exec: a regexp execution context or NULL to indicate the end
2899 * @value: the first string token input
2900 * @value2: the second string token input
2901 * @data: data associated to the token to reuse in callbacks
2902 *
2903 * Push one input token in the execution context
2904 *
2905 * Returns: 1 if the regexp reached a final state, 0 if non-final, and
2906 * a negative value in case of error.
2907 */
2908int
2909xmlRegExecPushString2(xmlRegExecCtxtPtr exec, const xmlChar *value,
2910 const xmlChar *value2, void *data) {
2911 xmlChar buf[150];
2912 int lenn, lenp, ret;
2913 xmlChar *str;
2914
2915 if (exec == NULL)
2916 return(-1);
2917 if (exec->comp == NULL)
2918 return(-1);
2919 if (exec->status != 0)
2920 return(exec->status);
2921
2922 if (value2 == NULL)
2923 return(xmlRegExecPushString(exec, value, data));
2924
2925 lenn = strlen((char *) value2);
2926 lenp = strlen((char *) value);
2927
2928 if (150 < lenn + lenp + 2) {
Daniel Veillard3c908dc2003-04-19 00:07:51 +00002929 str = (xmlChar *) xmlMallocAtomic(lenn + lenp + 2);
Daniel Veillard52b48c72003-04-13 19:53:42 +00002930 if (str == NULL) {
2931 exec->status = -1;
2932 return(-1);
2933 }
2934 } else {
2935 str = buf;
2936 }
2937 memcpy(&str[0], value, lenp);
2938 str[lenp] = '|';
2939 memcpy(&str[lenp + 1], value2, lenn);
2940 str[lenn + lenp + 1] = 0;
2941
2942 if (exec->comp->compact != NULL)
2943 ret = xmlRegCompactPushString(exec, exec->comp, str, data);
2944 else
2945 ret = xmlRegExecPushString(exec, str, data);
2946
2947 if (str != buf)
2948 xmlFree(buf);
2949 return(ret);
2950}
2951
Daniel Veillard4255d502002-04-16 15:50:10 +00002952#if 0
2953static int
2954xmlRegExecPushChar(xmlRegExecCtxtPtr exec, int UCS) {
2955 xmlRegTransPtr trans;
2956 xmlRegAtomPtr atom;
2957 int ret;
2958 int codepoint, len;
2959
2960 if (exec == NULL)
2961 return(-1);
2962 if (exec->status != 0)
2963 return(exec->status);
2964
2965 while ((exec->status == 0) &&
2966 ((exec->inputString[exec->index] != 0) ||
2967 (exec->state->type != XML_REGEXP_FINAL_STATE))) {
2968
2969 /*
2970 * End of input on non-terminal state, rollback, however we may
2971 * still have epsilon like transition for counted transitions
2972 * on counters, in that case don't break too early.
2973 */
2974 if ((exec->inputString[exec->index] == 0) && (exec->counts == NULL))
2975 goto rollback;
2976
2977 exec->transcount = 0;
2978 for (;exec->transno < exec->state->nbTrans;exec->transno++) {
2979 trans = &exec->state->trans[exec->transno];
2980 if (trans->to < 0)
2981 continue;
2982 atom = trans->atom;
2983 ret = 0;
2984 if (trans->count >= 0) {
2985 int count;
2986 xmlRegCounterPtr counter;
2987
2988 /*
2989 * A counted transition.
2990 */
2991
2992 count = exec->counts[trans->count];
2993 counter = &exec->comp->counters[trans->count];
2994#ifdef DEBUG_REGEXP_EXEC
2995 printf("testing count %d: val %d, min %d, max %d\n",
2996 trans->count, count, counter->min, counter->max);
2997#endif
2998 ret = ((count >= counter->min) && (count <= counter->max));
2999 } else if (atom == NULL) {
3000 fprintf(stderr, "epsilon transition left at runtime\n");
3001 exec->status = -2;
3002 break;
3003 } else if (exec->inputString[exec->index] != 0) {
3004 codepoint = CUR_SCHAR(&(exec->inputString[exec->index]), len);
3005 ret = xmlRegCheckCharacter(atom, codepoint);
3006 if ((ret == 1) && (atom->min > 0) && (atom->max > 0)) {
3007 xmlRegStatePtr to = exec->comp->states[trans->to];
3008
3009 /*
3010 * this is a multiple input sequence
3011 */
3012 if (exec->state->nbTrans > exec->transno + 1) {
3013 xmlFARegExecSave(exec);
3014 }
3015 exec->transcount = 1;
3016 do {
3017 /*
3018 * Try to progress as much as possible on the input
3019 */
3020 if (exec->transcount == atom->max) {
3021 break;
3022 }
3023 exec->index += len;
3024 /*
3025 * End of input: stop here
3026 */
3027 if (exec->inputString[exec->index] == 0) {
3028 exec->index -= len;
3029 break;
3030 }
3031 if (exec->transcount >= atom->min) {
3032 int transno = exec->transno;
3033 xmlRegStatePtr state = exec->state;
3034
3035 /*
3036 * The transition is acceptable save it
3037 */
3038 exec->transno = -1; /* trick */
3039 exec->state = to;
3040 xmlFARegExecSave(exec);
3041 exec->transno = transno;
3042 exec->state = state;
3043 }
3044 codepoint = CUR_SCHAR(&(exec->inputString[exec->index]),
3045 len);
3046 ret = xmlRegCheckCharacter(atom, codepoint);
3047 exec->transcount++;
3048 } while (ret == 1);
3049 if (exec->transcount < atom->min)
3050 ret = 0;
3051
3052 /*
3053 * If the last check failed but one transition was found
3054 * possible, rollback
3055 */
3056 if (ret < 0)
3057 ret = 0;
3058 if (ret == 0) {
3059 goto rollback;
3060 }
3061 }
3062 }
3063 if (ret == 1) {
3064 if (exec->state->nbTrans > exec->transno + 1) {
3065 xmlFARegExecSave(exec);
3066 }
3067 if (trans->counter >= 0) {
3068#ifdef DEBUG_REGEXP_EXEC
3069 printf("Increasing count %d\n", trans->counter);
3070#endif
3071 exec->counts[trans->counter]++;
3072 }
3073#ifdef DEBUG_REGEXP_EXEC
3074 printf("entering state %d\n", trans->to);
3075#endif
3076 exec->state = exec->comp->states[trans->to];
3077 exec->transno = 0;
3078 if (trans->atom != NULL) {
3079 exec->index += len;
3080 }
3081 goto progress;
3082 } else if (ret < 0) {
3083 exec->status = -4;
3084 break;
3085 }
3086 }
3087 if ((exec->transno != 0) || (exec->state->nbTrans == 0)) {
3088rollback:
3089 /*
3090 * Failed to find a way out
3091 */
3092 exec->determinist = 0;
3093 xmlFARegExecRollBack(exec);
3094 }
3095progress:
3096 continue;
3097 }
3098}
3099#endif
3100/************************************************************************
3101 * *
3102 * Parser for the Shemas Datatype Regular Expressions *
3103 * http://www.w3.org/TR/2001/REC-xmlschema-2-20010502/#regexs *
3104 * *
3105 ************************************************************************/
3106
3107/**
3108 * xmlFAIsChar:
Daniel Veillard441bc322002-04-20 17:38:48 +00003109 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00003110 *
3111 * [10] Char ::= [^.\?*+()|#x5B#x5D]
3112 */
3113static int
3114xmlFAIsChar(xmlRegParserCtxtPtr ctxt) {
3115 int cur;
3116 int len;
3117
3118 cur = CUR_SCHAR(ctxt->cur, len);
3119 if ((cur == '.') || (cur == '\\') || (cur == '?') ||
3120 (cur == '*') || (cur == '+') || (cur == '(') ||
3121 (cur == ')') || (cur == '|') || (cur == 0x5B) ||
3122 (cur == 0x5D) || (cur == 0))
3123 return(-1);
3124 return(cur);
3125}
3126
3127/**
3128 * xmlFAParseCharProp:
Daniel Veillard441bc322002-04-20 17:38:48 +00003129 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00003130 *
3131 * [27] charProp ::= IsCategory | IsBlock
3132 * [28] IsCategory ::= Letters | Marks | Numbers | Punctuation |
3133 * Separators | Symbols | Others
3134 * [29] Letters ::= 'L' [ultmo]?
3135 * [30] Marks ::= 'M' [nce]?
3136 * [31] Numbers ::= 'N' [dlo]?
3137 * [32] Punctuation ::= 'P' [cdseifo]?
3138 * [33] Separators ::= 'Z' [slp]?
3139 * [34] Symbols ::= 'S' [mcko]?
3140 * [35] Others ::= 'C' [cfon]?
3141 * [36] IsBlock ::= 'Is' [a-zA-Z0-9#x2D]+
3142 */
3143static void
3144xmlFAParseCharProp(xmlRegParserCtxtPtr ctxt) {
3145 int cur;
William M. Brack779af002003-08-01 15:55:39 +00003146 xmlRegAtomType type = (xmlRegAtomType) 0;
Daniel Veillard4255d502002-04-16 15:50:10 +00003147 xmlChar *blockName = NULL;
3148
3149 cur = CUR;
3150 if (cur == 'L') {
3151 NEXT;
3152 cur = CUR;
3153 if (cur == 'u') {
3154 NEXT;
3155 type = XML_REGEXP_LETTER_UPPERCASE;
3156 } else if (cur == 'l') {
3157 NEXT;
3158 type = XML_REGEXP_LETTER_LOWERCASE;
3159 } else if (cur == 't') {
3160 NEXT;
3161 type = XML_REGEXP_LETTER_TITLECASE;
3162 } else if (cur == 'm') {
3163 NEXT;
3164 type = XML_REGEXP_LETTER_MODIFIER;
3165 } else if (cur == 'o') {
3166 NEXT;
3167 type = XML_REGEXP_LETTER_OTHERS;
3168 } else {
3169 type = XML_REGEXP_LETTER;
3170 }
3171 } else if (cur == 'M') {
3172 NEXT;
3173 cur = CUR;
3174 if (cur == 'n') {
3175 NEXT;
3176 /* nonspacing */
3177 type = XML_REGEXP_MARK_NONSPACING;
3178 } else if (cur == 'c') {
3179 NEXT;
3180 /* spacing combining */
3181 type = XML_REGEXP_MARK_SPACECOMBINING;
3182 } else if (cur == 'e') {
3183 NEXT;
3184 /* enclosing */
3185 type = XML_REGEXP_MARK_ENCLOSING;
3186 } else {
3187 /* all marks */
3188 type = XML_REGEXP_MARK;
3189 }
3190 } else if (cur == 'N') {
3191 NEXT;
3192 cur = CUR;
3193 if (cur == 'd') {
3194 NEXT;
3195 /* digital */
3196 type = XML_REGEXP_NUMBER_DECIMAL;
3197 } else if (cur == 'l') {
3198 NEXT;
3199 /* letter */
3200 type = XML_REGEXP_NUMBER_LETTER;
3201 } else if (cur == 'o') {
3202 NEXT;
3203 /* other */
3204 type = XML_REGEXP_NUMBER_OTHERS;
3205 } else {
3206 /* all numbers */
3207 type = XML_REGEXP_NUMBER;
3208 }
3209 } else if (cur == 'P') {
3210 NEXT;
3211 cur = CUR;
3212 if (cur == 'c') {
3213 NEXT;
3214 /* connector */
3215 type = XML_REGEXP_PUNCT_CONNECTOR;
3216 } else if (cur == 'd') {
3217 NEXT;
3218 /* dash */
3219 type = XML_REGEXP_PUNCT_DASH;
3220 } else if (cur == 's') {
3221 NEXT;
3222 /* open */
3223 type = XML_REGEXP_PUNCT_OPEN;
3224 } else if (cur == 'e') {
3225 NEXT;
3226 /* close */
3227 type = XML_REGEXP_PUNCT_CLOSE;
3228 } else if (cur == 'i') {
3229 NEXT;
3230 /* initial quote */
3231 type = XML_REGEXP_PUNCT_INITQUOTE;
3232 } else if (cur == 'f') {
3233 NEXT;
3234 /* final quote */
3235 type = XML_REGEXP_PUNCT_FINQUOTE;
3236 } else if (cur == 'o') {
3237 NEXT;
3238 /* other */
3239 type = XML_REGEXP_PUNCT_OTHERS;
3240 } else {
3241 /* all punctuation */
3242 type = XML_REGEXP_PUNCT;
3243 }
3244 } else if (cur == 'Z') {
3245 NEXT;
3246 cur = CUR;
3247 if (cur == 's') {
3248 NEXT;
3249 /* space */
3250 type = XML_REGEXP_SEPAR_SPACE;
3251 } else if (cur == 'l') {
3252 NEXT;
3253 /* line */
3254 type = XML_REGEXP_SEPAR_LINE;
3255 } else if (cur == 'p') {
3256 NEXT;
3257 /* paragraph */
3258 type = XML_REGEXP_SEPAR_PARA;
3259 } else {
3260 /* all separators */
3261 type = XML_REGEXP_SEPAR;
3262 }
3263 } else if (cur == 'S') {
3264 NEXT;
3265 cur = CUR;
3266 if (cur == 'm') {
3267 NEXT;
3268 type = XML_REGEXP_SYMBOL_MATH;
3269 /* math */
3270 } else if (cur == 'c') {
3271 NEXT;
3272 type = XML_REGEXP_SYMBOL_CURRENCY;
3273 /* currency */
3274 } else if (cur == 'k') {
3275 NEXT;
3276 type = XML_REGEXP_SYMBOL_MODIFIER;
3277 /* modifiers */
3278 } else if (cur == 'o') {
3279 NEXT;
3280 type = XML_REGEXP_SYMBOL_OTHERS;
3281 /* other */
3282 } else {
3283 /* all symbols */
3284 type = XML_REGEXP_SYMBOL;
3285 }
3286 } else if (cur == 'C') {
3287 NEXT;
3288 cur = CUR;
3289 if (cur == 'c') {
3290 NEXT;
3291 /* control */
3292 type = XML_REGEXP_OTHER_CONTROL;
3293 } else if (cur == 'f') {
3294 NEXT;
3295 /* format */
3296 type = XML_REGEXP_OTHER_FORMAT;
3297 } else if (cur == 'o') {
3298 NEXT;
3299 /* private use */
3300 type = XML_REGEXP_OTHER_PRIVATE;
3301 } else if (cur == 'n') {
3302 NEXT;
3303 /* not assigned */
3304 type = XML_REGEXP_OTHER_NA;
3305 } else {
3306 /* all others */
3307 type = XML_REGEXP_OTHER;
3308 }
3309 } else if (cur == 'I') {
3310 const xmlChar *start;
3311 NEXT;
3312 cur = CUR;
3313 if (cur != 's') {
3314 ERROR("IsXXXX expected");
3315 return;
3316 }
3317 NEXT;
3318 start = ctxt->cur;
3319 cur = CUR;
3320 if (((cur >= 'a') && (cur <= 'z')) ||
3321 ((cur >= 'A') && (cur <= 'Z')) ||
3322 ((cur >= '0') && (cur <= '9')) ||
3323 (cur == 0x2D)) {
3324 NEXT;
3325 cur = CUR;
3326 while (((cur >= 'a') && (cur <= 'z')) ||
3327 ((cur >= 'A') && (cur <= 'Z')) ||
3328 ((cur >= '0') && (cur <= '9')) ||
3329 (cur == 0x2D)) {
3330 NEXT;
3331 cur = CUR;
3332 }
3333 }
3334 type = XML_REGEXP_BLOCK_NAME;
3335 blockName = xmlStrndup(start, ctxt->cur - start);
3336 } else {
3337 ERROR("Unknown char property");
3338 return;
3339 }
3340 if (ctxt->atom == NULL) {
3341 ctxt->atom = xmlRegNewAtom(ctxt, type);
3342 if (ctxt->atom != NULL)
3343 ctxt->atom->valuep = blockName;
3344 } else if (ctxt->atom->type == XML_REGEXP_RANGES) {
3345 xmlRegAtomAddRange(ctxt, ctxt->atom, ctxt->neg,
3346 type, 0, 0, blockName);
3347 }
3348}
3349
3350/**
3351 * xmlFAParseCharClassEsc:
Daniel Veillard441bc322002-04-20 17:38:48 +00003352 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00003353 *
3354 * [23] charClassEsc ::= ( SingleCharEsc | MultiCharEsc | catEsc | complEsc )
3355 * [24] SingleCharEsc ::= '\' [nrt\|.?*+(){}#x2D#x5B#x5D#x5E]
3356 * [25] catEsc ::= '\p{' charProp '}'
3357 * [26] complEsc ::= '\P{' charProp '}'
3358 * [37] MultiCharEsc ::= '.' | ('\' [sSiIcCdDwW])
3359 */
3360static void
3361xmlFAParseCharClassEsc(xmlRegParserCtxtPtr ctxt) {
3362 int cur;
3363
3364 if (CUR == '.') {
3365 if (ctxt->atom == NULL) {
3366 ctxt->atom = xmlRegNewAtom(ctxt, XML_REGEXP_ANYCHAR);
3367 } else if (ctxt->atom->type == XML_REGEXP_RANGES) {
3368 xmlRegAtomAddRange(ctxt, ctxt->atom, ctxt->neg,
3369 XML_REGEXP_ANYCHAR, 0, 0, NULL);
3370 }
3371 NEXT;
3372 return;
3373 }
3374 if (CUR != '\\') {
3375 ERROR("Escaped sequence: expecting \\");
3376 return;
3377 }
3378 NEXT;
3379 cur = CUR;
3380 if (cur == 'p') {
3381 NEXT;
3382 if (CUR != '{') {
3383 ERROR("Expecting '{'");
3384 return;
3385 }
3386 NEXT;
3387 xmlFAParseCharProp(ctxt);
3388 if (CUR != '}') {
3389 ERROR("Expecting '}'");
3390 return;
3391 }
3392 NEXT;
3393 } else if (cur == 'P') {
3394 NEXT;
3395 if (CUR != '{') {
3396 ERROR("Expecting '{'");
3397 return;
3398 }
3399 NEXT;
3400 xmlFAParseCharProp(ctxt);
3401 ctxt->atom->neg = 1;
3402 if (CUR != '}') {
3403 ERROR("Expecting '}'");
3404 return;
3405 }
3406 NEXT;
3407 } else if ((cur == 'n') || (cur == 'r') || (cur == 't') || (cur == '\\') ||
3408 (cur == '|') || (cur == '.') || (cur == '?') || (cur == '*') ||
3409 (cur == '+') || (cur == '(') || (cur == ')') || (cur == '{') ||
3410 (cur == '}') || (cur == 0x2D) || (cur == 0x5B) || (cur == 0x5D) ||
3411 (cur == 0x5E)) {
3412 if (ctxt->atom == NULL) {
3413 ctxt->atom = xmlRegNewAtom(ctxt, XML_REGEXP_CHARVAL);
3414 if (ctxt->atom != NULL)
3415 ctxt->atom->codepoint = cur;
3416 } else if (ctxt->atom->type == XML_REGEXP_RANGES) {
3417 xmlRegAtomAddRange(ctxt, ctxt->atom, ctxt->neg,
3418 XML_REGEXP_CHARVAL, cur, cur, NULL);
3419 }
3420 NEXT;
3421 } else if ((cur == 's') || (cur == 'S') || (cur == 'i') || (cur == 'I') ||
3422 (cur == 'c') || (cur == 'C') || (cur == 'd') || (cur == 'D') ||
3423 (cur == 'w') || (cur == 'W')) {
Daniel Veillardb509f152002-04-17 16:28:10 +00003424 xmlRegAtomType type = XML_REGEXP_ANYSPACE;
Daniel Veillard4255d502002-04-16 15:50:10 +00003425
3426 switch (cur) {
3427 case 's':
3428 type = XML_REGEXP_ANYSPACE;
3429 break;
3430 case 'S':
3431 type = XML_REGEXP_NOTSPACE;
3432 break;
3433 case 'i':
3434 type = XML_REGEXP_INITNAME;
3435 break;
3436 case 'I':
3437 type = XML_REGEXP_NOTINITNAME;
3438 break;
3439 case 'c':
3440 type = XML_REGEXP_NAMECHAR;
3441 break;
3442 case 'C':
3443 type = XML_REGEXP_NOTNAMECHAR;
3444 break;
3445 case 'd':
3446 type = XML_REGEXP_DECIMAL;
3447 break;
3448 case 'D':
3449 type = XML_REGEXP_NOTDECIMAL;
3450 break;
3451 case 'w':
3452 type = XML_REGEXP_REALCHAR;
3453 break;
3454 case 'W':
3455 type = XML_REGEXP_NOTREALCHAR;
3456 break;
3457 }
3458 NEXT;
3459 if (ctxt->atom == NULL) {
3460 ctxt->atom = xmlRegNewAtom(ctxt, type);
3461 } else if (ctxt->atom->type == XML_REGEXP_RANGES) {
3462 xmlRegAtomAddRange(ctxt, ctxt->atom, ctxt->neg,
3463 type, 0, 0, NULL);
3464 }
3465 }
3466}
3467
3468/**
3469 * xmlFAParseCharRef:
Daniel Veillard441bc322002-04-20 17:38:48 +00003470 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00003471 *
3472 * [19] XmlCharRef ::= ( '&#' [0-9]+ ';' ) | (' &#x' [0-9a-fA-F]+ ';' )
3473 */
3474static int
3475xmlFAParseCharRef(xmlRegParserCtxtPtr ctxt) {
3476 int ret = 0, cur;
3477
3478 if ((CUR != '&') || (NXT(1) != '#'))
3479 return(-1);
3480 NEXT;
3481 NEXT;
3482 cur = CUR;
3483 if (cur == 'x') {
3484 NEXT;
3485 cur = CUR;
3486 if (((cur >= '0') && (cur <= '9')) ||
3487 ((cur >= 'a') && (cur <= 'f')) ||
3488 ((cur >= 'A') && (cur <= 'F'))) {
3489 while (((cur >= '0') && (cur <= '9')) ||
3490 ((cur >= 'A') && (cur <= 'F'))) {
3491 if ((cur >= '0') && (cur <= '9'))
3492 ret = ret * 16 + cur - '0';
3493 else if ((cur >= 'a') && (cur <= 'f'))
3494 ret = ret * 16 + 10 + (cur - 'a');
3495 else
3496 ret = ret * 16 + 10 + (cur - 'A');
3497 NEXT;
3498 cur = CUR;
3499 }
3500 } else {
3501 ERROR("Char ref: expecting [0-9A-F]");
3502 return(-1);
3503 }
3504 } else {
3505 if ((cur >= '0') && (cur <= '9')) {
3506 while ((cur >= '0') && (cur <= '9')) {
3507 ret = ret * 10 + cur - '0';
3508 NEXT;
3509 cur = CUR;
3510 }
3511 } else {
3512 ERROR("Char ref: expecting [0-9]");
3513 return(-1);
3514 }
3515 }
3516 if (cur != ';') {
3517 ERROR("Char ref: expecting ';'");
3518 return(-1);
3519 } else {
3520 NEXT;
3521 }
3522 return(ret);
3523}
3524
3525/**
3526 * xmlFAParseCharRange:
Daniel Veillard441bc322002-04-20 17:38:48 +00003527 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00003528 *
3529 * [17] charRange ::= seRange | XmlCharRef | XmlCharIncDash
3530 * [18] seRange ::= charOrEsc '-' charOrEsc
3531 * [20] charOrEsc ::= XmlChar | SingleCharEsc
3532 * [21] XmlChar ::= [^\#x2D#x5B#x5D]
3533 * [22] XmlCharIncDash ::= [^\#x5B#x5D]
3534 */
3535static void
3536xmlFAParseCharRange(xmlRegParserCtxtPtr ctxt) {
William M. Brackdc99df92003-12-27 01:54:25 +00003537 int cur, len;
Daniel Veillard4255d502002-04-16 15:50:10 +00003538 int start = -1;
3539 int end = -1;
3540
3541 if ((CUR == '&') && (NXT(1) == '#')) {
3542 end = start = xmlFAParseCharRef(ctxt);
3543 xmlRegAtomAddRange(ctxt, ctxt->atom, ctxt->neg,
3544 XML_REGEXP_CHARVAL, start, end, NULL);
3545 return;
3546 }
3547 cur = CUR;
3548 if (cur == '\\') {
3549 NEXT;
3550 cur = CUR;
3551 switch (cur) {
3552 case 'n': start = 0xA; break;
3553 case 'r': start = 0xD; break;
3554 case 't': start = 0x9; break;
3555 case '\\': case '|': case '.': case '-': case '^': case '?':
3556 case '*': case '+': case '{': case '}': case '(': case ')':
3557 case '[': case ']':
3558 start = cur; break;
3559 default:
3560 ERROR("Invalid escape value");
3561 return;
3562 }
3563 end = start;
William M. Brackdc99df92003-12-27 01:54:25 +00003564 len = 1;
Daniel Veillard4255d502002-04-16 15:50:10 +00003565 } else if ((cur != 0x5B) && (cur != 0x5D)) {
William M. Brackdc99df92003-12-27 01:54:25 +00003566 end = start = CUR_SCHAR(ctxt->cur, len);
Daniel Veillard4255d502002-04-16 15:50:10 +00003567 } else {
3568 ERROR("Expecting a char range");
3569 return;
3570 }
William M. Brackdc99df92003-12-27 01:54:25 +00003571 NEXTL(len);
Daniel Veillard4255d502002-04-16 15:50:10 +00003572 if (start == '-') {
3573 return;
3574 }
3575 cur = CUR;
William M. Brack10f1ef42004-03-20 14:51:25 +00003576 if ((cur != '-') || (NXT(1) == ']')) {
Daniel Veillard4255d502002-04-16 15:50:10 +00003577 xmlRegAtomAddRange(ctxt, ctxt->atom, ctxt->neg,
3578 XML_REGEXP_CHARVAL, start, end, NULL);
3579 return;
3580 }
3581 NEXT;
3582 cur = CUR;
3583 if (cur == '\\') {
3584 NEXT;
3585 cur = CUR;
3586 switch (cur) {
3587 case 'n': end = 0xA; break;
3588 case 'r': end = 0xD; break;
3589 case 't': end = 0x9; break;
3590 case '\\': case '|': case '.': case '-': case '^': case '?':
3591 case '*': case '+': case '{': case '}': case '(': case ')':
3592 case '[': case ']':
3593 end = cur; break;
3594 default:
3595 ERROR("Invalid escape value");
3596 return;
3597 }
William M. Brackdc99df92003-12-27 01:54:25 +00003598 len = 1;
Daniel Veillard4255d502002-04-16 15:50:10 +00003599 } else if ((cur != 0x5B) && (cur != 0x5D)) {
William M. Brackdc99df92003-12-27 01:54:25 +00003600 end = CUR_SCHAR(ctxt->cur, len);
Daniel Veillard4255d502002-04-16 15:50:10 +00003601 } else {
3602 ERROR("Expecting the end of a char range");
3603 return;
3604 }
William M. Brackdc99df92003-12-27 01:54:25 +00003605 NEXTL(len);
Daniel Veillard4255d502002-04-16 15:50:10 +00003606 /* TODO check that the values are acceptable character ranges for XML */
3607 if (end < start) {
3608 ERROR("End of range is before start of range");
3609 } else {
3610 xmlRegAtomAddRange(ctxt, ctxt->atom, ctxt->neg,
3611 XML_REGEXP_CHARVAL, start, end, NULL);
3612 }
3613 return;
3614}
3615
3616/**
3617 * xmlFAParsePosCharGroup:
Daniel Veillard441bc322002-04-20 17:38:48 +00003618 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00003619 *
3620 * [14] posCharGroup ::= ( charRange | charClassEsc )+
3621 */
3622static void
3623xmlFAParsePosCharGroup(xmlRegParserCtxtPtr ctxt) {
3624 do {
3625 if ((CUR == '\\') || (CUR == '.')) {
3626 xmlFAParseCharClassEsc(ctxt);
3627 } else {
3628 xmlFAParseCharRange(ctxt);
3629 }
3630 } while ((CUR != ']') && (CUR != '^') && (CUR != '-') &&
3631 (ctxt->error == 0));
3632}
3633
3634/**
3635 * xmlFAParseCharGroup:
Daniel Veillard441bc322002-04-20 17:38:48 +00003636 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00003637 *
3638 * [13] charGroup ::= posCharGroup | negCharGroup | charClassSub
3639 * [15] negCharGroup ::= '^' posCharGroup
3640 * [16] charClassSub ::= ( posCharGroup | negCharGroup ) '-' charClassExpr
3641 * [12] charClassExpr ::= '[' charGroup ']'
3642 */
3643static void
3644xmlFAParseCharGroup(xmlRegParserCtxtPtr ctxt) {
3645 int n = ctxt->neg;
3646 while ((CUR != ']') && (ctxt->error == 0)) {
3647 if (CUR == '^') {
3648 int neg = ctxt->neg;
3649
3650 NEXT;
3651 ctxt->neg = !ctxt->neg;
3652 xmlFAParsePosCharGroup(ctxt);
3653 ctxt->neg = neg;
William M. Brack10f1ef42004-03-20 14:51:25 +00003654 } else if ((CUR == '-') && (NXT(1) == '[')) {
Daniel Veillardf8b9de32003-11-24 14:27:26 +00003655 int neg = ctxt->neg;
Daniel Veillardf8b9de32003-11-24 14:27:26 +00003656 ctxt->neg = 2;
William M. Brack10f1ef42004-03-20 14:51:25 +00003657 NEXT; /* eat the '-' */
3658 NEXT; /* eat the '[' */
Daniel Veillard4255d502002-04-16 15:50:10 +00003659 xmlFAParseCharGroup(ctxt);
3660 if (CUR == ']') {
3661 NEXT;
3662 } else {
3663 ERROR("charClassExpr: ']' expected");
3664 break;
3665 }
Daniel Veillardf8b9de32003-11-24 14:27:26 +00003666 ctxt->neg = neg;
Daniel Veillard4255d502002-04-16 15:50:10 +00003667 break;
3668 } else if (CUR != ']') {
3669 xmlFAParsePosCharGroup(ctxt);
3670 }
3671 }
3672 ctxt->neg = n;
3673}
3674
3675/**
3676 * xmlFAParseCharClass:
Daniel Veillard441bc322002-04-20 17:38:48 +00003677 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00003678 *
3679 * [11] charClass ::= charClassEsc | charClassExpr
3680 * [12] charClassExpr ::= '[' charGroup ']'
3681 */
3682static void
3683xmlFAParseCharClass(xmlRegParserCtxtPtr ctxt) {
3684 if (CUR == '[') {
3685 NEXT;
3686 ctxt->atom = xmlRegNewAtom(ctxt, XML_REGEXP_RANGES);
3687 if (ctxt->atom == NULL)
3688 return;
3689 xmlFAParseCharGroup(ctxt);
3690 if (CUR == ']') {
3691 NEXT;
3692 } else {
3693 ERROR("xmlFAParseCharClass: ']' expected");
3694 }
3695 } else {
3696 xmlFAParseCharClassEsc(ctxt);
3697 }
3698}
3699
3700/**
3701 * xmlFAParseQuantExact:
Daniel Veillard441bc322002-04-20 17:38:48 +00003702 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00003703 *
3704 * [8] QuantExact ::= [0-9]+
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00003705 *
3706 * Returns 0 if success or -1 in case of error
Daniel Veillard4255d502002-04-16 15:50:10 +00003707 */
3708static int
3709xmlFAParseQuantExact(xmlRegParserCtxtPtr ctxt) {
3710 int ret = 0;
3711 int ok = 0;
3712
3713 while ((CUR >= '0') && (CUR <= '9')) {
3714 ret = ret * 10 + (CUR - '0');
3715 ok = 1;
3716 NEXT;
3717 }
3718 if (ok != 1) {
3719 return(-1);
3720 }
3721 return(ret);
3722}
3723
3724/**
3725 * xmlFAParseQuantifier:
Daniel Veillard441bc322002-04-20 17:38:48 +00003726 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00003727 *
3728 * [4] quantifier ::= [?*+] | ( '{' quantity '}' )
3729 * [5] quantity ::= quantRange | quantMin | QuantExact
3730 * [6] quantRange ::= QuantExact ',' QuantExact
3731 * [7] quantMin ::= QuantExact ','
3732 * [8] QuantExact ::= [0-9]+
3733 */
3734static int
3735xmlFAParseQuantifier(xmlRegParserCtxtPtr ctxt) {
3736 int cur;
3737
3738 cur = CUR;
3739 if ((cur == '?') || (cur == '*') || (cur == '+')) {
3740 if (ctxt->atom != NULL) {
3741 if (cur == '?')
3742 ctxt->atom->quant = XML_REGEXP_QUANT_OPT;
3743 else if (cur == '*')
3744 ctxt->atom->quant = XML_REGEXP_QUANT_MULT;
3745 else if (cur == '+')
3746 ctxt->atom->quant = XML_REGEXP_QUANT_PLUS;
3747 }
3748 NEXT;
3749 return(1);
3750 }
3751 if (cur == '{') {
3752 int min = 0, max = 0;
3753
3754 NEXT;
3755 cur = xmlFAParseQuantExact(ctxt);
3756 if (cur >= 0)
3757 min = cur;
3758 if (CUR == ',') {
3759 NEXT;
Daniel Veillardebe48c62003-12-03 12:12:27 +00003760 if (CUR == '}')
3761 max = INT_MAX;
3762 else {
3763 cur = xmlFAParseQuantExact(ctxt);
3764 if (cur >= 0)
3765 max = cur;
3766 else {
3767 ERROR("Improper quantifier");
3768 }
3769 }
Daniel Veillard4255d502002-04-16 15:50:10 +00003770 }
3771 if (CUR == '}') {
3772 NEXT;
3773 } else {
3774 ERROR("Unterminated quantifier");
3775 }
3776 if (max == 0)
3777 max = min;
3778 if (ctxt->atom != NULL) {
3779 ctxt->atom->quant = XML_REGEXP_QUANT_RANGE;
3780 ctxt->atom->min = min;
3781 ctxt->atom->max = max;
3782 }
3783 return(1);
3784 }
3785 return(0);
3786}
3787
3788/**
3789 * xmlFAParseAtom:
Daniel Veillard441bc322002-04-20 17:38:48 +00003790 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00003791 *
3792 * [9] atom ::= Char | charClass | ( '(' regExp ')' )
3793 */
3794static int
3795xmlFAParseAtom(xmlRegParserCtxtPtr ctxt) {
3796 int codepoint, len;
3797
3798 codepoint = xmlFAIsChar(ctxt);
3799 if (codepoint > 0) {
3800 ctxt->atom = xmlRegNewAtom(ctxt, XML_REGEXP_CHARVAL);
3801 if (ctxt->atom == NULL)
3802 return(-1);
3803 codepoint = CUR_SCHAR(ctxt->cur, len);
3804 ctxt->atom->codepoint = codepoint;
3805 NEXTL(len);
3806 return(1);
3807 } else if (CUR == '|') {
3808 return(0);
3809 } else if (CUR == 0) {
3810 return(0);
3811 } else if (CUR == ')') {
3812 return(0);
3813 } else if (CUR == '(') {
3814 xmlRegStatePtr start, oldend;
3815
3816 NEXT;
3817 xmlFAGenerateEpsilonTransition(ctxt, ctxt->state, NULL);
3818 start = ctxt->state;
3819 oldend = ctxt->end;
3820 ctxt->end = NULL;
3821 ctxt->atom = NULL;
3822 xmlFAParseRegExp(ctxt, 0);
3823 if (CUR == ')') {
3824 NEXT;
3825 } else {
3826 ERROR("xmlFAParseAtom: expecting ')'");
3827 }
3828 ctxt->atom = xmlRegNewAtom(ctxt, XML_REGEXP_SUBREG);
3829 if (ctxt->atom == NULL)
3830 return(-1);
3831 ctxt->atom->start = start;
3832 ctxt->atom->stop = ctxt->state;
3833 ctxt->end = oldend;
3834 return(1);
3835 } else if ((CUR == '[') || (CUR == '\\') || (CUR == '.')) {
3836 xmlFAParseCharClass(ctxt);
3837 return(1);
3838 }
3839 return(0);
3840}
3841
3842/**
3843 * xmlFAParsePiece:
Daniel Veillard441bc322002-04-20 17:38:48 +00003844 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00003845 *
3846 * [3] piece ::= atom quantifier?
3847 */
3848static int
3849xmlFAParsePiece(xmlRegParserCtxtPtr ctxt) {
3850 int ret;
3851
3852 ctxt->atom = NULL;
3853 ret = xmlFAParseAtom(ctxt);
3854 if (ret == 0)
3855 return(0);
3856 if (ctxt->atom == NULL) {
3857 ERROR("internal: no atom generated");
3858 }
3859 xmlFAParseQuantifier(ctxt);
3860 return(1);
3861}
3862
3863/**
3864 * xmlFAParseBranch:
Daniel Veillard441bc322002-04-20 17:38:48 +00003865 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00003866 *
3867 * [2] branch ::= piece*
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00003868 8
Daniel Veillard4255d502002-04-16 15:50:10 +00003869 */
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00003870static int
Daniel Veillard2cbf5962004-03-31 15:50:43 +00003871xmlFAParseBranch(xmlRegParserCtxtPtr ctxt) {
Daniel Veillard4255d502002-04-16 15:50:10 +00003872 xmlRegStatePtr previous;
Daniel Veillard4255d502002-04-16 15:50:10 +00003873 int ret;
3874
3875 previous = ctxt->state;
3876 ret = xmlFAParsePiece(ctxt);
3877 if (ret != 0) {
Daniel Veillard2cbf5962004-03-31 15:50:43 +00003878 if (xmlFAGenerateTransitions(ctxt, previous, NULL, ctxt->atom) < 0)
3879 return(-1);
3880 previous = ctxt->state;
Daniel Veillard4255d502002-04-16 15:50:10 +00003881 ctxt->atom = NULL;
3882 }
3883 while ((ret != 0) && (ctxt->error == 0)) {
3884 ret = xmlFAParsePiece(ctxt);
3885 if (ret != 0) {
Daniel Veillard2cbf5962004-03-31 15:50:43 +00003886 if (xmlFAGenerateTransitions(ctxt, previous, NULL,
3887 ctxt->atom) < 0)
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00003888 return(-1);
Daniel Veillard4255d502002-04-16 15:50:10 +00003889 previous = ctxt->state;
3890 ctxt->atom = NULL;
3891 }
3892 }
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00003893 return(0);
Daniel Veillard4255d502002-04-16 15:50:10 +00003894}
3895
3896/**
3897 * xmlFAParseRegExp:
Daniel Veillard441bc322002-04-20 17:38:48 +00003898 * @ctxt: a regexp parser context
3899 * @top: is that the top-level expressions ?
Daniel Veillard4255d502002-04-16 15:50:10 +00003900 *
3901 * [1] regExp ::= branch ( '|' branch )*
3902 */
3903static void
3904xmlFAParseRegExp(xmlRegParserCtxtPtr ctxt, int top) {
Daniel Veillard2cbf5962004-03-31 15:50:43 +00003905 xmlRegStatePtr start, end, oldend, oldstart;
Daniel Veillard4255d502002-04-16 15:50:10 +00003906
3907 oldend = ctxt->end;
3908
Daniel Veillard2cbf5962004-03-31 15:50:43 +00003909 oldstart = ctxt->state;
3910 /* if not top start should have been generated by an epsilon trans */
Daniel Veillard4255d502002-04-16 15:50:10 +00003911 start = ctxt->state;
Daniel Veillard2cbf5962004-03-31 15:50:43 +00003912 ctxt->end = NULL;
3913 xmlFAParseBranch(ctxt);
3914 if (top) {
3915#ifdef DEBUG_REGEXP_GRAPH
3916 printf("State %d is final\n", ctxt->state->no);
3917#endif
3918 ctxt->state->type = XML_REGEXP_FINAL_STATE;
3919 }
Daniel Veillard4255d502002-04-16 15:50:10 +00003920 if (CUR != '|') {
3921 ctxt->end = ctxt->state;
3922 return;
3923 }
3924 end = ctxt->state;
3925 while ((CUR == '|') && (ctxt->error == 0)) {
3926 NEXT;
3927 ctxt->state = start;
Daniel Veillard2cbf5962004-03-31 15:50:43 +00003928 ctxt->end = NULL;
3929 xmlFAParseBranch(ctxt);
3930 if (top) {
3931 ctxt->state->type = XML_REGEXP_FINAL_STATE;
3932#ifdef DEBUG_REGEXP_GRAPH
3933 printf("State %d is final\n", ctxt->state->no);
3934#endif
3935 } else {
3936 xmlFAGenerateEpsilonTransition(ctxt, ctxt->state, end);
3937 }
Daniel Veillard4255d502002-04-16 15:50:10 +00003938 }
Daniel Veillard2cbf5962004-03-31 15:50:43 +00003939 if (!top) {
3940 ctxt->state = end;
3941 ctxt->end = end;
3942 }
Daniel Veillard4255d502002-04-16 15:50:10 +00003943}
3944
3945/************************************************************************
3946 * *
3947 * The basic API *
3948 * *
3949 ************************************************************************/
3950
3951/**
3952 * xmlRegexpPrint:
3953 * @output: the file for the output debug
3954 * @regexp: the compiled regexp
3955 *
3956 * Print the content of the compiled regular expression
3957 */
3958void
3959xmlRegexpPrint(FILE *output, xmlRegexpPtr regexp) {
3960 int i;
3961
3962 fprintf(output, " regexp: ");
3963 if (regexp == NULL) {
3964 fprintf(output, "NULL\n");
3965 return;
3966 }
3967 fprintf(output, "'%s' ", regexp->string);
3968 fprintf(output, "\n");
3969 fprintf(output, "%d atoms:\n", regexp->nbAtoms);
3970 for (i = 0;i < regexp->nbAtoms; i++) {
3971 fprintf(output, " %02d ", i);
3972 xmlRegPrintAtom(output, regexp->atoms[i]);
3973 }
3974 fprintf(output, "%d states:", regexp->nbStates);
3975 fprintf(output, "\n");
3976 for (i = 0;i < regexp->nbStates; i++) {
3977 xmlRegPrintState(output, regexp->states[i]);
3978 }
3979 fprintf(output, "%d counters:\n", regexp->nbCounters);
3980 for (i = 0;i < regexp->nbCounters; i++) {
3981 fprintf(output, " %d: min %d max %d\n", i, regexp->counters[i].min,
3982 regexp->counters[i].max);
3983 }
3984}
3985
3986/**
3987 * xmlRegexpCompile:
3988 * @regexp: a regular expression string
3989 *
3990 * Parses a regular expression conforming to XML Schemas Part 2 Datatype
3991 * Appendix F and build an automata suitable for testing strings against
3992 * that regular expression
3993 *
3994 * Returns the compiled expression or NULL in case of error
3995 */
3996xmlRegexpPtr
3997xmlRegexpCompile(const xmlChar *regexp) {
3998 xmlRegexpPtr ret;
3999 xmlRegParserCtxtPtr ctxt;
4000
4001 ctxt = xmlRegNewParserCtxt(regexp);
4002 if (ctxt == NULL)
4003 return(NULL);
4004
4005 /* initialize the parser */
4006 ctxt->end = NULL;
4007 ctxt->start = ctxt->state = xmlRegNewState(ctxt);
4008 xmlRegStatePush(ctxt, ctxt->start);
4009
4010 /* parse the expression building an automata */
4011 xmlFAParseRegExp(ctxt, 1);
4012 if (CUR != 0) {
4013 ERROR("xmlFAParseRegExp: extra characters");
4014 }
4015 ctxt->end = ctxt->state;
4016 ctxt->start->type = XML_REGEXP_START_STATE;
4017 ctxt->end->type = XML_REGEXP_FINAL_STATE;
4018
4019 /* remove the Epsilon except for counted transitions */
4020 xmlFAEliminateEpsilonTransitions(ctxt);
4021
4022
4023 if (ctxt->error != 0) {
4024 xmlRegFreeParserCtxt(ctxt);
4025 return(NULL);
4026 }
4027 ret = xmlRegEpxFromParse(ctxt);
4028 xmlRegFreeParserCtxt(ctxt);
4029 return(ret);
4030}
4031
4032/**
4033 * xmlRegexpExec:
4034 * @comp: the compiled regular expression
4035 * @content: the value to check against the regular expression
4036 *
4037 * Check if the regular expression generate the value
4038 *
4039 * Returns 1 if it matches, 0 if not and a negativa value in case of error
4040 */
4041int
4042xmlRegexpExec(xmlRegexpPtr comp, const xmlChar *content) {
4043 if ((comp == NULL) || (content == NULL))
4044 return(-1);
4045 return(xmlFARegExec(comp, content));
4046}
4047
4048/**
Daniel Veillard23e73572002-09-19 19:56:43 +00004049 * xmlRegexpIsDeterminist:
4050 * @comp: the compiled regular expression
4051 *
4052 * Check if the regular expression is determinist
4053 *
4054 * Returns 1 if it yes, 0 if not and a negativa value in case of error
4055 */
4056int
4057xmlRegexpIsDeterminist(xmlRegexpPtr comp) {
4058 xmlAutomataPtr am;
4059 int ret;
4060
4061 if (comp == NULL)
4062 return(-1);
4063 if (comp->determinist != -1)
4064 return(comp->determinist);
4065
4066 am = xmlNewAutomata();
Daniel Veillardbd9afb52002-09-25 22:25:35 +00004067 if (am->states != NULL) {
4068 int i;
4069
4070 for (i = 0;i < am->nbStates;i++)
4071 xmlRegFreeState(am->states[i]);
4072 xmlFree(am->states);
4073 }
Daniel Veillard23e73572002-09-19 19:56:43 +00004074 am->nbAtoms = comp->nbAtoms;
4075 am->atoms = comp->atoms;
4076 am->nbStates = comp->nbStates;
4077 am->states = comp->states;
4078 am->determinist = -1;
4079 ret = xmlFAComputesDeterminism(am);
4080 am->atoms = NULL;
4081 am->states = NULL;
4082 xmlFreeAutomata(am);
4083 return(ret);
4084}
4085
4086/**
Daniel Veillard4255d502002-04-16 15:50:10 +00004087 * xmlRegFreeRegexp:
4088 * @regexp: the regexp
4089 *
4090 * Free a regexp
4091 */
4092void
4093xmlRegFreeRegexp(xmlRegexpPtr regexp) {
4094 int i;
4095 if (regexp == NULL)
4096 return;
4097
4098 if (regexp->string != NULL)
4099 xmlFree(regexp->string);
4100 if (regexp->states != NULL) {
4101 for (i = 0;i < regexp->nbStates;i++)
4102 xmlRegFreeState(regexp->states[i]);
4103 xmlFree(regexp->states);
4104 }
4105 if (regexp->atoms != NULL) {
4106 for (i = 0;i < regexp->nbAtoms;i++)
4107 xmlRegFreeAtom(regexp->atoms[i]);
4108 xmlFree(regexp->atoms);
4109 }
4110 if (regexp->counters != NULL)
4111 xmlFree(regexp->counters);
Daniel Veillard23e73572002-09-19 19:56:43 +00004112 if (regexp->compact != NULL)
4113 xmlFree(regexp->compact);
Daniel Veillard118aed72002-09-24 14:13:13 +00004114 if (regexp->transdata != NULL)
4115 xmlFree(regexp->transdata);
Daniel Veillard23e73572002-09-19 19:56:43 +00004116 if (regexp->stringMap != NULL) {
4117 for (i = 0; i < regexp->nbstrings;i++)
4118 xmlFree(regexp->stringMap[i]);
4119 xmlFree(regexp->stringMap);
4120 }
4121
Daniel Veillard4255d502002-04-16 15:50:10 +00004122 xmlFree(regexp);
4123}
4124
4125#ifdef LIBXML_AUTOMATA_ENABLED
4126/************************************************************************
4127 * *
4128 * The Automata interface *
4129 * *
4130 ************************************************************************/
4131
4132/**
4133 * xmlNewAutomata:
4134 *
4135 * Create a new automata
4136 *
4137 * Returns the new object or NULL in case of failure
4138 */
4139xmlAutomataPtr
4140xmlNewAutomata(void) {
4141 xmlAutomataPtr ctxt;
4142
4143 ctxt = xmlRegNewParserCtxt(NULL);
4144 if (ctxt == NULL)
4145 return(NULL);
4146
4147 /* initialize the parser */
4148 ctxt->end = NULL;
4149 ctxt->start = ctxt->state = xmlRegNewState(ctxt);
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00004150 if (ctxt->start == NULL) {
4151 xmlFreeAutomata(ctxt);
4152 return(NULL);
4153 }
4154 if (xmlRegStatePush(ctxt, ctxt->start) < 0) {
4155 xmlRegFreeState(ctxt->start);
4156 xmlFreeAutomata(ctxt);
4157 return(NULL);
4158 }
Daniel Veillard4255d502002-04-16 15:50:10 +00004159
4160 return(ctxt);
4161}
4162
4163/**
4164 * xmlFreeAutomata:
4165 * @am: an automata
4166 *
4167 * Free an automata
4168 */
4169void
4170xmlFreeAutomata(xmlAutomataPtr am) {
4171 if (am == NULL)
4172 return;
4173 xmlRegFreeParserCtxt(am);
4174}
4175
4176/**
4177 * xmlAutomataGetInitState:
4178 * @am: an automata
4179 *
Daniel Veillarda9b66d02002-12-11 14:23:49 +00004180 * Initial state lookup
4181 *
Daniel Veillard4255d502002-04-16 15:50:10 +00004182 * Returns the initial state of the automata
4183 */
4184xmlAutomataStatePtr
4185xmlAutomataGetInitState(xmlAutomataPtr am) {
4186 if (am == NULL)
4187 return(NULL);
4188 return(am->start);
4189}
4190
4191/**
4192 * xmlAutomataSetFinalState:
4193 * @am: an automata
4194 * @state: a state in this automata
4195 *
4196 * Makes that state a final state
4197 *
4198 * Returns 0 or -1 in case of error
4199 */
4200int
4201xmlAutomataSetFinalState(xmlAutomataPtr am, xmlAutomataStatePtr state) {
4202 if ((am == NULL) || (state == NULL))
4203 return(-1);
4204 state->type = XML_REGEXP_FINAL_STATE;
4205 return(0);
4206}
4207
4208/**
4209 * xmlAutomataNewTransition:
4210 * @am: an automata
4211 * @from: the starting point of the transition
4212 * @to: the target point of the transition or NULL
4213 * @token: the input string associated to that transition
4214 * @data: data passed to the callback function if the transition is activated
4215 *
4216 * If @to is NULL, this create first a new target state in the automata
4217 * and then adds a transition from the @from state to the target state
4218 * activated by the value of @token
4219 *
4220 * Returns the target state or NULL in case of error
4221 */
4222xmlAutomataStatePtr
4223xmlAutomataNewTransition(xmlAutomataPtr am, xmlAutomataStatePtr from,
4224 xmlAutomataStatePtr to, const xmlChar *token,
4225 void *data) {
4226 xmlRegAtomPtr atom;
4227
4228 if ((am == NULL) || (from == NULL) || (token == NULL))
4229 return(NULL);
4230 atom = xmlRegNewAtom(am, XML_REGEXP_STRING);
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00004231 if (atom == NULL)
4232 return(NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +00004233 atom->data = data;
4234 if (atom == NULL)
4235 return(NULL);
4236 atom->valuep = xmlStrdup(token);
4237
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00004238 if (xmlFAGenerateTransitions(am, from, to, atom) < 0) {
4239 xmlRegFreeAtom(atom);
4240 return(NULL);
4241 }
Daniel Veillard4255d502002-04-16 15:50:10 +00004242 if (to == NULL)
4243 return(am->state);
4244 return(to);
4245}
4246
4247/**
Daniel Veillard52b48c72003-04-13 19:53:42 +00004248 * xmlAutomataNewTransition2:
4249 * @am: an automata
4250 * @from: the starting point of the transition
4251 * @to: the target point of the transition or NULL
4252 * @token: the first input string associated to that transition
4253 * @token2: the second input string associated to that transition
4254 * @data: data passed to the callback function if the transition is activated
4255 *
4256 * If @to is NULL, this create first a new target state in the automata
4257 * and then adds a transition from the @from state to the target state
4258 * activated by the value of @token
4259 *
4260 * Returns the target state or NULL in case of error
4261 */
4262xmlAutomataStatePtr
4263xmlAutomataNewTransition2(xmlAutomataPtr am, xmlAutomataStatePtr from,
4264 xmlAutomataStatePtr to, const xmlChar *token,
4265 const xmlChar *token2, void *data) {
4266 xmlRegAtomPtr atom;
4267
4268 if ((am == NULL) || (from == NULL) || (token == NULL))
4269 return(NULL);
4270 atom = xmlRegNewAtom(am, XML_REGEXP_STRING);
4271 atom->data = data;
4272 if (atom == NULL)
4273 return(NULL);
4274 if ((token2 == NULL) || (*token2 == 0)) {
4275 atom->valuep = xmlStrdup(token);
4276 } else {
4277 int lenn, lenp;
4278 xmlChar *str;
4279
4280 lenn = strlen((char *) token2);
4281 lenp = strlen((char *) token);
4282
Daniel Veillard3c908dc2003-04-19 00:07:51 +00004283 str = (xmlChar *) xmlMallocAtomic(lenn + lenp + 2);
Daniel Veillard52b48c72003-04-13 19:53:42 +00004284 if (str == NULL) {
4285 xmlRegFreeAtom(atom);
4286 return(NULL);
4287 }
4288 memcpy(&str[0], token, lenp);
4289 str[lenp] = '|';
4290 memcpy(&str[lenp + 1], token2, lenn);
4291 str[lenn + lenp + 1] = 0;
4292
4293 atom->valuep = str;
4294 }
4295
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00004296 if (xmlFAGenerateTransitions(am, from, to, atom) < 0) {
4297 xmlRegFreeAtom(atom);
4298 return(NULL);
4299 }
Daniel Veillard52b48c72003-04-13 19:53:42 +00004300 if (to == NULL)
4301 return(am->state);
4302 return(to);
4303}
4304
4305/**
Daniel Veillard4255d502002-04-16 15:50:10 +00004306 * xmlAutomataNewCountTrans:
4307 * @am: an automata
4308 * @from: the starting point of the transition
4309 * @to: the target point of the transition or NULL
4310 * @token: the input string associated to that transition
4311 * @min: the minimum successive occurences of token
Daniel Veillarda9b66d02002-12-11 14:23:49 +00004312 * @max: the maximum successive occurences of token
4313 * @data: data associated to the transition
Daniel Veillard4255d502002-04-16 15:50:10 +00004314 *
4315 * If @to is NULL, this create first a new target state in the automata
4316 * and then adds a transition from the @from state to the target state
4317 * activated by a succession of input of value @token and whose number
4318 * is between @min and @max
4319 *
4320 * Returns the target state or NULL in case of error
4321 */
4322xmlAutomataStatePtr
4323xmlAutomataNewCountTrans(xmlAutomataPtr am, xmlAutomataStatePtr from,
4324 xmlAutomataStatePtr to, const xmlChar *token,
4325 int min, int max, void *data) {
4326 xmlRegAtomPtr atom;
Daniel Veillard0ddb21c2004-02-12 12:43:49 +00004327 int counter;
Daniel Veillard4255d502002-04-16 15:50:10 +00004328
4329 if ((am == NULL) || (from == NULL) || (token == NULL))
4330 return(NULL);
4331 if (min < 0)
4332 return(NULL);
4333 if ((max < min) || (max < 1))
4334 return(NULL);
4335 atom = xmlRegNewAtom(am, XML_REGEXP_STRING);
4336 if (atom == NULL)
4337 return(NULL);
4338 atom->valuep = xmlStrdup(token);
4339 atom->data = data;
4340 if (min == 0)
4341 atom->min = 1;
4342 else
4343 atom->min = min;
4344 atom->max = max;
4345
Daniel Veillard0ddb21c2004-02-12 12:43:49 +00004346 /*
4347 * associate a counter to the transition.
4348 */
4349 counter = xmlRegGetCounter(am);
4350 am->counters[counter].min = min;
4351 am->counters[counter].max = max;
4352
4353 /* xmlFAGenerateTransitions(am, from, to, atom); */
4354 if (to == NULL) {
4355 to = xmlRegNewState(am);
4356 xmlRegStatePush(am, to);
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00004357 }
Daniel Veillard0ddb21c2004-02-12 12:43:49 +00004358 xmlRegStateAddTrans(am, from, atom, to, counter, -1);
4359 xmlRegAtomPush(am, atom);
4360 am->state = to;
4361
Daniel Veillard4255d502002-04-16 15:50:10 +00004362 if (to == NULL)
4363 to = am->state;
4364 if (to == NULL)
4365 return(NULL);
4366 if (min == 0)
4367 xmlFAGenerateEpsilonTransition(am, from, to);
4368 return(to);
4369}
4370
4371/**
Daniel Veillard7646b182002-04-20 06:41:40 +00004372 * xmlAutomataNewOnceTrans:
4373 * @am: an automata
4374 * @from: the starting point of the transition
4375 * @to: the target point of the transition or NULL
4376 * @token: the input string associated to that transition
4377 * @min: the minimum successive occurences of token
Daniel Veillarda9b66d02002-12-11 14:23:49 +00004378 * @max: the maximum successive occurences of token
4379 * @data: data associated to the transition
Daniel Veillard7646b182002-04-20 06:41:40 +00004380 *
4381 * If @to is NULL, this create first a new target state in the automata
4382 * and then adds a transition from the @from state to the target state
4383 * activated by a succession of input of value @token and whose number
4384 * is between @min and @max, moreover that transistion can only be crossed
4385 * once.
4386 *
4387 * Returns the target state or NULL in case of error
4388 */
4389xmlAutomataStatePtr
4390xmlAutomataNewOnceTrans(xmlAutomataPtr am, xmlAutomataStatePtr from,
4391 xmlAutomataStatePtr to, const xmlChar *token,
4392 int min, int max, void *data) {
4393 xmlRegAtomPtr atom;
4394 int counter;
4395
4396 if ((am == NULL) || (from == NULL) || (token == NULL))
4397 return(NULL);
4398 if (min < 1)
4399 return(NULL);
4400 if ((max < min) || (max < 1))
4401 return(NULL);
4402 atom = xmlRegNewAtom(am, XML_REGEXP_STRING);
4403 if (atom == NULL)
4404 return(NULL);
4405 atom->valuep = xmlStrdup(token);
4406 atom->data = data;
4407 atom->quant = XML_REGEXP_QUANT_ONCEONLY;
4408 if (min == 0)
4409 atom->min = 1;
4410 else
4411 atom->min = min;
4412 atom->max = max;
4413 /*
4414 * associate a counter to the transition.
4415 */
4416 counter = xmlRegGetCounter(am);
4417 am->counters[counter].min = 1;
4418 am->counters[counter].max = 1;
4419
4420 /* xmlFAGenerateTransitions(am, from, to, atom); */
4421 if (to == NULL) {
4422 to = xmlRegNewState(am);
4423 xmlRegStatePush(am, to);
4424 }
4425 xmlRegStateAddTrans(am, from, atom, to, counter, -1);
4426 xmlRegAtomPush(am, atom);
4427 am->state = to;
4428 if (to == NULL)
4429 to = am->state;
4430 if (to == NULL)
4431 return(NULL);
4432 return(to);
4433}
4434
4435/**
Daniel Veillard4255d502002-04-16 15:50:10 +00004436 * xmlAutomataNewState:
4437 * @am: an automata
4438 *
4439 * Create a new disconnected state in the automata
4440 *
4441 * Returns the new state or NULL in case of error
4442 */
4443xmlAutomataStatePtr
4444xmlAutomataNewState(xmlAutomataPtr am) {
4445 xmlAutomataStatePtr to;
4446
4447 if (am == NULL)
4448 return(NULL);
4449 to = xmlRegNewState(am);
4450 xmlRegStatePush(am, to);
4451 return(to);
4452}
4453
4454/**
Daniel Veillarda9b66d02002-12-11 14:23:49 +00004455 * xmlAutomataNewEpsilon:
Daniel Veillard4255d502002-04-16 15:50:10 +00004456 * @am: an automata
4457 * @from: the starting point of the transition
4458 * @to: the target point of the transition or NULL
4459 *
4460 * If @to is NULL, this create first a new target state in the automata
4461 * and then adds a an epsilon transition from the @from state to the
4462 * target state
4463 *
4464 * Returns the target state or NULL in case of error
4465 */
4466xmlAutomataStatePtr
4467xmlAutomataNewEpsilon(xmlAutomataPtr am, xmlAutomataStatePtr from,
4468 xmlAutomataStatePtr to) {
4469 if ((am == NULL) || (from == NULL))
4470 return(NULL);
4471 xmlFAGenerateEpsilonTransition(am, from, to);
4472 if (to == NULL)
4473 return(am->state);
4474 return(to);
4475}
4476
Daniel Veillardb509f152002-04-17 16:28:10 +00004477/**
Daniel Veillard7646b182002-04-20 06:41:40 +00004478 * xmlAutomataNewAllTrans:
4479 * @am: an automata
4480 * @from: the starting point of the transition
4481 * @to: the target point of the transition or NULL
Daniel Veillarda9b66d02002-12-11 14:23:49 +00004482 * @lax: allow to transition if not all all transitions have been activated
Daniel Veillard7646b182002-04-20 06:41:40 +00004483 *
4484 * If @to is NULL, this create first a new target state in the automata
4485 * and then adds a an ALL transition from the @from state to the
4486 * target state. That transition is an epsilon transition allowed only when
4487 * all transitions from the @from node have been activated.
4488 *
4489 * Returns the target state or NULL in case of error
4490 */
4491xmlAutomataStatePtr
4492xmlAutomataNewAllTrans(xmlAutomataPtr am, xmlAutomataStatePtr from,
Daniel Veillard441bc322002-04-20 17:38:48 +00004493 xmlAutomataStatePtr to, int lax) {
Daniel Veillard7646b182002-04-20 06:41:40 +00004494 if ((am == NULL) || (from == NULL))
4495 return(NULL);
Daniel Veillard441bc322002-04-20 17:38:48 +00004496 xmlFAGenerateAllTransition(am, from, to, lax);
Daniel Veillard7646b182002-04-20 06:41:40 +00004497 if (to == NULL)
4498 return(am->state);
4499 return(to);
4500}
4501
4502/**
Daniel Veillardb509f152002-04-17 16:28:10 +00004503 * xmlAutomataNewCounter:
4504 * @am: an automata
4505 * @min: the minimal value on the counter
4506 * @max: the maximal value on the counter
4507 *
4508 * Create a new counter
4509 *
4510 * Returns the counter number or -1 in case of error
4511 */
4512int
4513xmlAutomataNewCounter(xmlAutomataPtr am, int min, int max) {
4514 int ret;
4515
4516 if (am == NULL)
4517 return(-1);
4518
4519 ret = xmlRegGetCounter(am);
4520 if (ret < 0)
4521 return(-1);
4522 am->counters[ret].min = min;
4523 am->counters[ret].max = max;
4524 return(ret);
4525}
4526
4527/**
4528 * xmlAutomataNewCountedTrans:
4529 * @am: an automata
4530 * @from: the starting point of the transition
4531 * @to: the target point of the transition or NULL
4532 * @counter: the counter associated to that transition
4533 *
4534 * If @to is NULL, this create first a new target state in the automata
4535 * and then adds an epsilon transition from the @from state to the target state
4536 * which will increment the counter provided
4537 *
4538 * Returns the target state or NULL in case of error
4539 */
4540xmlAutomataStatePtr
4541xmlAutomataNewCountedTrans(xmlAutomataPtr am, xmlAutomataStatePtr from,
4542 xmlAutomataStatePtr to, int counter) {
4543 if ((am == NULL) || (from == NULL) || (counter < 0))
4544 return(NULL);
4545 xmlFAGenerateCountedEpsilonTransition(am, from, to, counter);
4546 if (to == NULL)
4547 return(am->state);
4548 return(to);
4549}
4550
4551/**
4552 * xmlAutomataNewCounterTrans:
4553 * @am: an automata
4554 * @from: the starting point of the transition
4555 * @to: the target point of the transition or NULL
4556 * @counter: the counter associated to that transition
4557 *
4558 * If @to is NULL, this create first a new target state in the automata
4559 * and then adds an epsilon transition from the @from state to the target state
4560 * which will be allowed only if the counter is within the right range.
4561 *
4562 * Returns the target state or NULL in case of error
4563 */
4564xmlAutomataStatePtr
4565xmlAutomataNewCounterTrans(xmlAutomataPtr am, xmlAutomataStatePtr from,
4566 xmlAutomataStatePtr to, int counter) {
4567 if ((am == NULL) || (from == NULL) || (counter < 0))
4568 return(NULL);
4569 xmlFAGenerateCountedTransition(am, from, to, counter);
4570 if (to == NULL)
4571 return(am->state);
4572 return(to);
4573}
Daniel Veillard4255d502002-04-16 15:50:10 +00004574
4575/**
4576 * xmlAutomataCompile:
4577 * @am: an automata
4578 *
4579 * Compile the automata into a Reg Exp ready for being executed.
4580 * The automata should be free after this point.
4581 *
4582 * Returns the compiled regexp or NULL in case of error
4583 */
4584xmlRegexpPtr
4585xmlAutomataCompile(xmlAutomataPtr am) {
4586 xmlRegexpPtr ret;
4587
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00004588 if ((am == NULL) || (am->error != 0)) return(NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +00004589 xmlFAEliminateEpsilonTransitions(am);
Daniel Veillard23e73572002-09-19 19:56:43 +00004590 /* xmlFAComputesDeterminism(am); */
Daniel Veillard4255d502002-04-16 15:50:10 +00004591 ret = xmlRegEpxFromParse(am);
4592
4593 return(ret);
4594}
Daniel Veillarde19fc232002-04-22 16:01:24 +00004595
4596/**
4597 * xmlAutomataIsDeterminist:
4598 * @am: an automata
4599 *
4600 * Checks if an automata is determinist.
4601 *
4602 * Returns 1 if true, 0 if not, and -1 in case of error
4603 */
4604int
4605xmlAutomataIsDeterminist(xmlAutomataPtr am) {
4606 int ret;
4607
4608 if (am == NULL)
4609 return(-1);
4610
4611 ret = xmlFAComputesDeterminism(am);
4612 return(ret);
4613}
Daniel Veillard4255d502002-04-16 15:50:10 +00004614#endif /* LIBXML_AUTOMATA_ENABLED */
4615#endif /* LIBXML_REGEXP_ENABLED */