blob: f42b38c66bd72ef080a5fec78cb9f85c4ca8d130 [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)
1217 printf("all transition");
Daniel Veillard4402ab42002-09-12 16:02:56 +00001218 else if (count >= 0)
Daniel Veillard4255d502002-04-16 15:50:10 +00001219 printf("count based %d", count);
1220 else if (counter >= 0)
1221 printf("counted %d", counter);
1222 else if (atom == NULL)
1223 printf("epsilon transition");
1224 printf("\n");
1225#endif
1226
1227 state->trans[state->nbTrans].atom = atom;
1228 state->trans[state->nbTrans].to = target->no;
1229 state->trans[state->nbTrans].counter = counter;
1230 state->trans[state->nbTrans].count = count;
1231 state->nbTrans++;
1232}
1233
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001234static int
Daniel Veillard4255d502002-04-16 15:50:10 +00001235xmlRegStatePush(xmlRegParserCtxtPtr ctxt, xmlRegStatePtr state) {
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001236 if (state == NULL) return(-1);
Daniel Veillard4255d502002-04-16 15:50:10 +00001237 if (ctxt->maxStates == 0) {
1238 ctxt->maxStates = 4;
1239 ctxt->states = (xmlRegStatePtr *) xmlMalloc(ctxt->maxStates *
1240 sizeof(xmlRegStatePtr));
1241 if (ctxt->states == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00001242 xmlRegexpErrMemory(ctxt, "adding state");
Daniel Veillard4255d502002-04-16 15:50:10 +00001243 ctxt->maxStates = 0;
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001244 return(-1);
Daniel Veillard4255d502002-04-16 15:50:10 +00001245 }
1246 } else if (ctxt->nbStates >= ctxt->maxStates) {
1247 xmlRegStatePtr *tmp;
1248 ctxt->maxStates *= 2;
1249 tmp = (xmlRegStatePtr *) xmlRealloc(ctxt->states, ctxt->maxStates *
1250 sizeof(xmlRegStatePtr));
1251 if (tmp == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00001252 xmlRegexpErrMemory(ctxt, "adding state");
Daniel Veillard4255d502002-04-16 15:50:10 +00001253 ctxt->maxStates /= 2;
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001254 return(-1);
Daniel Veillard4255d502002-04-16 15:50:10 +00001255 }
1256 ctxt->states = tmp;
1257 }
1258 state->no = ctxt->nbStates;
1259 ctxt->states[ctxt->nbStates++] = state;
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001260 return(0);
Daniel Veillard4255d502002-04-16 15:50:10 +00001261}
1262
1263/**
Daniel Veillard7646b182002-04-20 06:41:40 +00001264 * xmlFAGenerateAllTransition:
Daniel Veillard441bc322002-04-20 17:38:48 +00001265 * @ctxt: a regexp parser context
1266 * @from: the from state
1267 * @to: the target state or NULL for building a new one
1268 * @lax:
Daniel Veillard7646b182002-04-20 06:41:40 +00001269 *
1270 */
1271static void
1272xmlFAGenerateAllTransition(xmlRegParserCtxtPtr ctxt,
Daniel Veillard441bc322002-04-20 17:38:48 +00001273 xmlRegStatePtr from, xmlRegStatePtr to,
1274 int lax) {
Daniel Veillard7646b182002-04-20 06:41:40 +00001275 if (to == NULL) {
1276 to = xmlRegNewState(ctxt);
1277 xmlRegStatePush(ctxt, to);
1278 ctxt->state = to;
1279 }
Daniel Veillard441bc322002-04-20 17:38:48 +00001280 if (lax)
1281 xmlRegStateAddTrans(ctxt, from, NULL, to, -1, REGEXP_ALL_LAX_COUNTER);
1282 else
1283 xmlRegStateAddTrans(ctxt, from, NULL, to, -1, REGEXP_ALL_COUNTER);
Daniel Veillard7646b182002-04-20 06:41:40 +00001284}
1285
1286/**
Daniel Veillard4255d502002-04-16 15:50:10 +00001287 * xmlFAGenerateEpsilonTransition:
Daniel Veillard441bc322002-04-20 17:38:48 +00001288 * @ctxt: a regexp parser context
1289 * @from: the from state
1290 * @to: the target state or NULL for building a new one
Daniel Veillard4255d502002-04-16 15:50:10 +00001291 *
1292 */
1293static void
1294xmlFAGenerateEpsilonTransition(xmlRegParserCtxtPtr ctxt,
1295 xmlRegStatePtr from, xmlRegStatePtr to) {
1296 if (to == NULL) {
1297 to = xmlRegNewState(ctxt);
1298 xmlRegStatePush(ctxt, to);
1299 ctxt->state = to;
1300 }
1301 xmlRegStateAddTrans(ctxt, from, NULL, to, -1, -1);
1302}
1303
1304/**
1305 * xmlFAGenerateCountedEpsilonTransition:
Daniel Veillard441bc322002-04-20 17:38:48 +00001306 * @ctxt: a regexp parser context
1307 * @from: the from state
1308 * @to: the target state or NULL for building a new one
Daniel Veillard4255d502002-04-16 15:50:10 +00001309 * counter: the counter for that transition
1310 *
1311 */
1312static void
1313xmlFAGenerateCountedEpsilonTransition(xmlRegParserCtxtPtr ctxt,
1314 xmlRegStatePtr from, xmlRegStatePtr to, int counter) {
1315 if (to == NULL) {
1316 to = xmlRegNewState(ctxt);
1317 xmlRegStatePush(ctxt, to);
1318 ctxt->state = to;
1319 }
1320 xmlRegStateAddTrans(ctxt, from, NULL, to, counter, -1);
1321}
1322
1323/**
1324 * xmlFAGenerateCountedTransition:
Daniel Veillard441bc322002-04-20 17:38:48 +00001325 * @ctxt: a regexp parser context
1326 * @from: the from state
1327 * @to: the target state or NULL for building a new one
Daniel Veillard4255d502002-04-16 15:50:10 +00001328 * counter: the counter for that transition
1329 *
1330 */
1331static void
1332xmlFAGenerateCountedTransition(xmlRegParserCtxtPtr ctxt,
1333 xmlRegStatePtr from, xmlRegStatePtr to, int counter) {
1334 if (to == NULL) {
1335 to = xmlRegNewState(ctxt);
1336 xmlRegStatePush(ctxt, to);
1337 ctxt->state = to;
1338 }
1339 xmlRegStateAddTrans(ctxt, from, NULL, to, -1, counter);
1340}
1341
1342/**
1343 * xmlFAGenerateTransitions:
Daniel Veillard441bc322002-04-20 17:38:48 +00001344 * @ctxt: a regexp parser context
1345 * @from: the from state
1346 * @to: the target state or NULL for building a new one
1347 * @atom: the atom generating the transition
Daniel Veillard4255d502002-04-16 15:50:10 +00001348 *
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001349 * Returns 0 if succes and -1 in case of error.
Daniel Veillard4255d502002-04-16 15:50:10 +00001350 */
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001351static int
Daniel Veillard4255d502002-04-16 15:50:10 +00001352xmlFAGenerateTransitions(xmlRegParserCtxtPtr ctxt, xmlRegStatePtr from,
1353 xmlRegStatePtr to, xmlRegAtomPtr atom) {
1354 if (atom == NULL) {
1355 ERROR("genrate transition: atom == NULL");
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001356 return(-1);
Daniel Veillard4255d502002-04-16 15:50:10 +00001357 }
1358 if (atom->type == XML_REGEXP_SUBREG) {
1359 /*
1360 * this is a subexpression handling one should not need to
1361 * create a new node excep for XML_REGEXP_QUANT_RANGE.
1362 */
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001363 if (xmlRegAtomPush(ctxt, atom) < 0) {
1364 return(-1);
1365 }
Daniel Veillard4255d502002-04-16 15:50:10 +00001366 if ((to != NULL) && (atom->stop != to) &&
1367 (atom->quant != XML_REGEXP_QUANT_RANGE)) {
1368 /*
1369 * Generate an epsilon transition to link to the target
1370 */
1371 xmlFAGenerateEpsilonTransition(ctxt, atom->stop, to);
1372 }
1373 switch (atom->quant) {
1374 case XML_REGEXP_QUANT_OPT:
1375 atom->quant = XML_REGEXP_QUANT_ONCE;
1376 xmlFAGenerateEpsilonTransition(ctxt, atom->start, atom->stop);
1377 break;
1378 case XML_REGEXP_QUANT_MULT:
1379 atom->quant = XML_REGEXP_QUANT_ONCE;
1380 xmlFAGenerateEpsilonTransition(ctxt, atom->start, atom->stop);
1381 xmlFAGenerateEpsilonTransition(ctxt, atom->stop, atom->start);
1382 break;
1383 case XML_REGEXP_QUANT_PLUS:
1384 atom->quant = XML_REGEXP_QUANT_ONCE;
1385 xmlFAGenerateEpsilonTransition(ctxt, atom->stop, atom->start);
1386 break;
1387 case XML_REGEXP_QUANT_RANGE: {
1388 int counter;
1389 xmlRegStatePtr newstate;
1390
1391 /*
1392 * This one is nasty:
1393 * 1/ register a new counter
1394 * 2/ register an epsilon transition associated to
1395 * this counter going from atom->stop to atom->start
1396 * 3/ create a new state
1397 * 4/ generate a counted transition from atom->stop to
1398 * that state
1399 */
1400 counter = xmlRegGetCounter(ctxt);
1401 ctxt->counters[counter].min = atom->min - 1;
1402 ctxt->counters[counter].max = atom->max - 1;
1403 atom->min = 0;
1404 atom->max = 0;
1405 atom->quant = XML_REGEXP_QUANT_ONCE;
1406 xmlFAGenerateCountedEpsilonTransition(ctxt, atom->stop,
1407 atom->start, counter);
1408 if (to != NULL) {
1409 newstate = to;
1410 } else {
1411 newstate = xmlRegNewState(ctxt);
1412 xmlRegStatePush(ctxt, newstate);
1413 ctxt->state = newstate;
1414 }
1415 xmlFAGenerateCountedTransition(ctxt, atom->stop,
1416 newstate, counter);
1417 }
1418 default:
1419 break;
1420 }
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001421 return(0);
Daniel Veillard4255d502002-04-16 15:50:10 +00001422 } else {
1423 if (to == NULL) {
1424 to = xmlRegNewState(ctxt);
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001425 if (to != NULL)
1426 xmlRegStatePush(ctxt, to);
1427 else {
1428 return(-1);
1429 }
1430 }
1431 if (xmlRegAtomPush(ctxt, atom) < 0) {
1432 return(-1);
Daniel Veillard4255d502002-04-16 15:50:10 +00001433 }
1434 xmlRegStateAddTrans(ctxt, from, atom, to, -1, -1);
Daniel Veillard4255d502002-04-16 15:50:10 +00001435 ctxt->state = to;
1436 }
1437 switch (atom->quant) {
1438 case XML_REGEXP_QUANT_OPT:
1439 atom->quant = XML_REGEXP_QUANT_ONCE;
1440 xmlFAGenerateEpsilonTransition(ctxt, from, to);
1441 break;
1442 case XML_REGEXP_QUANT_MULT:
1443 atom->quant = XML_REGEXP_QUANT_ONCE;
1444 xmlFAGenerateEpsilonTransition(ctxt, from, to);
1445 xmlRegStateAddTrans(ctxt, to, atom, to, -1, -1);
1446 break;
1447 case XML_REGEXP_QUANT_PLUS:
1448 atom->quant = XML_REGEXP_QUANT_ONCE;
1449 xmlRegStateAddTrans(ctxt, to, atom, to, -1, -1);
1450 break;
1451 default:
1452 break;
1453 }
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001454 return(0);
Daniel Veillard4255d502002-04-16 15:50:10 +00001455}
1456
1457/**
1458 * xmlFAReduceEpsilonTransitions:
Daniel Veillard441bc322002-04-20 17:38:48 +00001459 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00001460 * @fromnr: the from state
1461 * @tonr: the to state
1462 * @cpunter: should that transition be associted to a counted
1463 *
1464 */
1465static void
1466xmlFAReduceEpsilonTransitions(xmlRegParserCtxtPtr ctxt, int fromnr,
1467 int tonr, int counter) {
1468 int transnr;
1469 xmlRegStatePtr from;
1470 xmlRegStatePtr to;
1471
1472#ifdef DEBUG_REGEXP_GRAPH
1473 printf("xmlFAReduceEpsilonTransitions(%d, %d)\n", fromnr, tonr);
1474#endif
1475 from = ctxt->states[fromnr];
1476 if (from == NULL)
1477 return;
1478 to = ctxt->states[tonr];
1479 if (to == NULL)
1480 return;
1481 if ((to->mark == XML_REGEXP_MARK_START) ||
1482 (to->mark == XML_REGEXP_MARK_VISITED))
1483 return;
1484
1485 to->mark = XML_REGEXP_MARK_VISITED;
1486 if (to->type == XML_REGEXP_FINAL_STATE) {
1487#ifdef DEBUG_REGEXP_GRAPH
1488 printf("State %d is final, so %d becomes final\n", tonr, fromnr);
1489#endif
1490 from->type = XML_REGEXP_FINAL_STATE;
1491 }
1492 for (transnr = 0;transnr < to->nbTrans;transnr++) {
1493 if (to->trans[transnr].atom == NULL) {
1494 /*
1495 * Don't remove counted transitions
1496 * Don't loop either
1497 */
Daniel Veillardb509f152002-04-17 16:28:10 +00001498 if (to->trans[transnr].to != fromnr) {
1499 if (to->trans[transnr].count >= 0) {
1500 int newto = to->trans[transnr].to;
1501
1502 xmlRegStateAddTrans(ctxt, from, NULL,
1503 ctxt->states[newto],
1504 -1, to->trans[transnr].count);
1505 } else {
Daniel Veillard4255d502002-04-16 15:50:10 +00001506#ifdef DEBUG_REGEXP_GRAPH
Daniel Veillardb509f152002-04-17 16:28:10 +00001507 printf("Found epsilon trans %d from %d to %d\n",
1508 transnr, tonr, to->trans[transnr].to);
Daniel Veillard4255d502002-04-16 15:50:10 +00001509#endif
Daniel Veillardb509f152002-04-17 16:28:10 +00001510 if (to->trans[transnr].counter >= 0) {
1511 xmlFAReduceEpsilonTransitions(ctxt, fromnr,
1512 to->trans[transnr].to,
1513 to->trans[transnr].counter);
1514 } else {
1515 xmlFAReduceEpsilonTransitions(ctxt, fromnr,
1516 to->trans[transnr].to,
1517 counter);
1518 }
1519 }
Daniel Veillard4255d502002-04-16 15:50:10 +00001520 }
1521 } else {
1522 int newto = to->trans[transnr].to;
1523
Daniel Veillardb509f152002-04-17 16:28:10 +00001524 if (to->trans[transnr].counter >= 0) {
1525 xmlRegStateAddTrans(ctxt, from, to->trans[transnr].atom,
1526 ctxt->states[newto],
1527 to->trans[transnr].counter, -1);
1528 } else {
1529 xmlRegStateAddTrans(ctxt, from, to->trans[transnr].atom,
1530 ctxt->states[newto], counter, -1);
1531 }
Daniel Veillard4255d502002-04-16 15:50:10 +00001532 }
1533 }
1534 to->mark = XML_REGEXP_MARK_NORMAL;
1535}
1536
1537/**
1538 * xmlFAEliminateEpsilonTransitions:
Daniel Veillard441bc322002-04-20 17:38:48 +00001539 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00001540 *
1541 */
1542static void
1543xmlFAEliminateEpsilonTransitions(xmlRegParserCtxtPtr ctxt) {
1544 int statenr, transnr;
1545 xmlRegStatePtr state;
1546
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001547 if (ctxt->states == NULL) return;
1548
1549
Daniel Veillard4255d502002-04-16 15:50:10 +00001550 /*
1551 * build the completed transitions bypassing the epsilons
1552 * Use a marking algorithm to avoid loops
1553 */
1554 for (statenr = 0;statenr < ctxt->nbStates;statenr++) {
1555 state = ctxt->states[statenr];
1556 if (state == NULL)
1557 continue;
1558 for (transnr = 0;transnr < state->nbTrans;transnr++) {
1559 if ((state->trans[transnr].atom == NULL) &&
1560 (state->trans[transnr].to >= 0)) {
1561 if (state->trans[transnr].to == statenr) {
1562 state->trans[transnr].to = -1;
1563#ifdef DEBUG_REGEXP_GRAPH
1564 printf("Removed loopback epsilon trans %d on %d\n",
1565 transnr, statenr);
1566#endif
1567 } else if (state->trans[transnr].count < 0) {
1568 int newto = state->trans[transnr].to;
1569
1570#ifdef DEBUG_REGEXP_GRAPH
1571 printf("Found epsilon trans %d from %d to %d\n",
1572 transnr, statenr, newto);
1573#endif
1574 state->mark = XML_REGEXP_MARK_START;
1575 xmlFAReduceEpsilonTransitions(ctxt, statenr,
1576 newto, state->trans[transnr].counter);
1577 state->mark = XML_REGEXP_MARK_NORMAL;
1578#ifdef DEBUG_REGEXP_GRAPH
1579 } else {
1580 printf("Found counted transition %d on %d\n",
1581 transnr, statenr);
1582#endif
1583 }
1584 }
1585 }
1586 }
1587 /*
1588 * Eliminate the epsilon transitions
1589 */
1590 for (statenr = 0;statenr < ctxt->nbStates;statenr++) {
1591 state = ctxt->states[statenr];
1592 if (state == NULL)
1593 continue;
1594 for (transnr = 0;transnr < state->nbTrans;transnr++) {
1595 if ((state->trans[transnr].atom == NULL) &&
1596 (state->trans[transnr].count < 0) &&
1597 (state->trans[transnr].to >= 0)) {
1598 state->trans[transnr].to = -1;
1599 }
1600 }
1601 }
Daniel Veillard23e73572002-09-19 19:56:43 +00001602
1603 /*
1604 * Use this pass to detect unreachable states too
1605 */
1606 for (statenr = 0;statenr < ctxt->nbStates;statenr++) {
1607 state = ctxt->states[statenr];
1608 if (state != NULL)
William M. Brack779af002003-08-01 15:55:39 +00001609 state->reached = XML_REGEXP_MARK_NORMAL;
Daniel Veillard23e73572002-09-19 19:56:43 +00001610 }
1611 state = ctxt->states[0];
1612 if (state != NULL)
William M. Brack779af002003-08-01 15:55:39 +00001613 state->reached = XML_REGEXP_MARK_START;
Daniel Veillard23e73572002-09-19 19:56:43 +00001614 while (state != NULL) {
1615 xmlRegStatePtr target = NULL;
William M. Brack779af002003-08-01 15:55:39 +00001616 state->reached = XML_REGEXP_MARK_VISITED;
Daniel Veillard23e73572002-09-19 19:56:43 +00001617 /*
1618 * Mark all state reachable from the current reachable state
1619 */
1620 for (transnr = 0;transnr < state->nbTrans;transnr++) {
1621 if ((state->trans[transnr].to >= 0) &&
1622 ((state->trans[transnr].atom != NULL) ||
1623 (state->trans[transnr].count >= 0))) {
1624 int newto = state->trans[transnr].to;
1625
1626 if (ctxt->states[newto] == NULL)
1627 continue;
William M. Brack779af002003-08-01 15:55:39 +00001628 if (ctxt->states[newto]->reached == XML_REGEXP_MARK_NORMAL) {
1629 ctxt->states[newto]->reached = XML_REGEXP_MARK_START;
Daniel Veillard23e73572002-09-19 19:56:43 +00001630 target = ctxt->states[newto];
1631 }
1632 }
1633 }
1634 /*
1635 * find the next accessible state not explored
1636 */
1637 if (target == NULL) {
1638 for (statenr = 1;statenr < ctxt->nbStates;statenr++) {
1639 state = ctxt->states[statenr];
William M. Brack779af002003-08-01 15:55:39 +00001640 if ((state != NULL) && (state->reached ==
1641 XML_REGEXP_MARK_START)) {
Daniel Veillard23e73572002-09-19 19:56:43 +00001642 target = state;
1643 break;
1644 }
1645 }
1646 }
1647 state = target;
1648 }
1649 for (statenr = 0;statenr < ctxt->nbStates;statenr++) {
1650 state = ctxt->states[statenr];
William M. Brack779af002003-08-01 15:55:39 +00001651 if ((state != NULL) && (state->reached == XML_REGEXP_MARK_NORMAL)) {
Daniel Veillard23e73572002-09-19 19:56:43 +00001652#ifdef DEBUG_REGEXP_GRAPH
1653 printf("Removed unreachable state %d\n", statenr);
1654#endif
1655 xmlRegFreeState(state);
1656 ctxt->states[statenr] = NULL;
1657 }
1658 }
1659
Daniel Veillard4255d502002-04-16 15:50:10 +00001660}
1661
Daniel Veillarde19fc232002-04-22 16:01:24 +00001662/**
1663 * xmlFACompareAtoms:
1664 * @atom1: an atom
1665 * @atom2: an atom
1666 *
1667 * Compares two atoms to check whether they are equivatents
1668 *
1669 * Returns 1 if yes and 0 otherwise
1670 */
1671static int
1672xmlFACompareAtoms(xmlRegAtomPtr atom1, xmlRegAtomPtr atom2) {
1673 if (atom1 == atom2)
1674 return(1);
1675 if ((atom1 == NULL) || (atom2 == NULL))
1676 return(0);
1677
1678 if (atom1->type != atom2->type)
1679 return(0);
1680 switch (atom1->type) {
1681 case XML_REGEXP_STRING:
1682 return(xmlStrEqual((xmlChar *)atom1->valuep,
1683 (xmlChar *)atom2->valuep));
1684 case XML_REGEXP_EPSILON:
1685 return(1);
1686 case XML_REGEXP_CHARVAL:
1687 return(atom1->codepoint == atom2->codepoint);
1688 case XML_REGEXP_RANGES:
1689 TODO;
1690 return(0);
1691 default:
1692 break;
1693 }
1694 return(1);
1695}
1696
1697/**
1698 * xmlFARecurseDeterminism:
1699 * @ctxt: a regexp parser context
1700 *
1701 * Check whether the associated regexp is determinist,
1702 * should be called after xmlFAEliminateEpsilonTransitions()
1703 *
1704 */
1705static int
1706xmlFARecurseDeterminism(xmlRegParserCtxtPtr ctxt, xmlRegStatePtr state,
1707 int to, xmlRegAtomPtr atom) {
1708 int ret = 1;
1709 int transnr;
1710 xmlRegTransPtr t1;
1711
1712 if (state == NULL)
1713 return(ret);
1714 for (transnr = 0;transnr < state->nbTrans;transnr++) {
1715 t1 = &(state->trans[transnr]);
1716 /*
1717 * check transitions conflicting with the one looked at
1718 */
1719 if (t1->atom == NULL) {
1720 if (t1->to == -1)
1721 continue;
1722 ret = xmlFARecurseDeterminism(ctxt, ctxt->states[t1->to],
1723 to, atom);
1724 if (ret == 0)
1725 return(0);
1726 continue;
1727 }
1728 if (t1->to != to)
1729 continue;
1730 if (xmlFACompareAtoms(t1->atom, atom))
1731 return(0);
1732 }
1733 return(ret);
1734}
1735
1736/**
1737 * xmlFAComputesDeterminism:
1738 * @ctxt: a regexp parser context
1739 *
1740 * Check whether the associated regexp is determinist,
1741 * should be called after xmlFAEliminateEpsilonTransitions()
1742 *
1743 */
1744static int
1745xmlFAComputesDeterminism(xmlRegParserCtxtPtr ctxt) {
1746 int statenr, transnr;
1747 xmlRegStatePtr state;
1748 xmlRegTransPtr t1, t2;
1749 int i;
1750 int ret = 1;
1751
Daniel Veillard4402ab42002-09-12 16:02:56 +00001752#ifdef DEBUG_REGEXP_GRAPH
1753 printf("xmlFAComputesDeterminism\n");
1754 xmlRegPrintCtxt(stdout, ctxt);
1755#endif
Daniel Veillarde19fc232002-04-22 16:01:24 +00001756 if (ctxt->determinist != -1)
1757 return(ctxt->determinist);
1758
1759 /*
1760 * Check for all states that there isn't 2 transitions
1761 * with the same atom and a different target.
1762 */
1763 for (statenr = 0;statenr < ctxt->nbStates;statenr++) {
1764 state = ctxt->states[statenr];
1765 if (state == NULL)
1766 continue;
1767 for (transnr = 0;transnr < state->nbTrans;transnr++) {
1768 t1 = &(state->trans[transnr]);
1769 /*
1770 * Determinism checks in case of counted or all transitions
1771 * will have to be handled separately
1772 */
1773 if (t1->atom == NULL)
1774 continue;
1775 if (t1->to == -1) /* eliminated */
1776 continue;
1777 for (i = 0;i < transnr;i++) {
1778 t2 = &(state->trans[i]);
1779 if (t2->to == -1) /* eliminated */
1780 continue;
1781 if (t2->atom != NULL) {
1782 if (t1->to == t2->to) {
1783 if (xmlFACompareAtoms(t1->atom, t2->atom))
1784 t2->to = -1; /* eliminate */
1785 } else {
1786 /* not determinist ! */
1787 if (xmlFACompareAtoms(t1->atom, t2->atom))
1788 ret = 0;
1789 }
1790 } else if (t1->to != -1) {
1791 /*
1792 * do the closure in case of remaining specific
1793 * epsilon transitions like choices or all
1794 */
1795 ret = xmlFARecurseDeterminism(ctxt, ctxt->states[t1->to],
1796 t2->to, t2->atom);
1797 if (ret == 0)
1798 return(0);
1799 }
1800 }
1801 if (ret == 0)
1802 break;
1803 }
1804 if (ret == 0)
1805 break;
1806 }
1807 ctxt->determinist = ret;
1808 return(ret);
1809}
1810
Daniel Veillard4255d502002-04-16 15:50:10 +00001811/************************************************************************
1812 * *
1813 * Routines to check input against transition atoms *
1814 * *
1815 ************************************************************************/
1816
1817static int
1818xmlRegCheckCharacterRange(xmlRegAtomType type, int codepoint, int neg,
1819 int start, int end, const xmlChar *blockName) {
1820 int ret = 0;
1821
1822 switch (type) {
1823 case XML_REGEXP_STRING:
1824 case XML_REGEXP_SUBREG:
1825 case XML_REGEXP_RANGES:
1826 case XML_REGEXP_EPSILON:
1827 return(-1);
1828 case XML_REGEXP_ANYCHAR:
1829 ret = ((codepoint != '\n') && (codepoint != '\r'));
1830 break;
1831 case XML_REGEXP_CHARVAL:
1832 ret = ((codepoint >= start) && (codepoint <= end));
1833 break;
1834 case XML_REGEXP_NOTSPACE:
1835 neg = !neg;
1836 case XML_REGEXP_ANYSPACE:
1837 ret = ((codepoint == '\n') || (codepoint == '\r') ||
1838 (codepoint == '\t') || (codepoint == ' '));
1839 break;
1840 case XML_REGEXP_NOTINITNAME:
1841 neg = !neg;
1842 case XML_REGEXP_INITNAME:
William M. Brack871611b2003-10-18 04:53:14 +00001843 ret = (IS_LETTER(codepoint) ||
Daniel Veillard4255d502002-04-16 15:50:10 +00001844 (codepoint == '_') || (codepoint == ':'));
1845 break;
1846 case XML_REGEXP_NOTNAMECHAR:
1847 neg = !neg;
1848 case XML_REGEXP_NAMECHAR:
William M. Brack871611b2003-10-18 04:53:14 +00001849 ret = (IS_LETTER(codepoint) || IS_DIGIT(codepoint) ||
Daniel Veillard4255d502002-04-16 15:50:10 +00001850 (codepoint == '.') || (codepoint == '-') ||
1851 (codepoint == '_') || (codepoint == ':') ||
William M. Brack871611b2003-10-18 04:53:14 +00001852 IS_COMBINING(codepoint) || IS_EXTENDER(codepoint));
Daniel Veillard4255d502002-04-16 15:50:10 +00001853 break;
1854 case XML_REGEXP_NOTDECIMAL:
1855 neg = !neg;
1856 case XML_REGEXP_DECIMAL:
1857 ret = xmlUCSIsCatNd(codepoint);
1858 break;
1859 case XML_REGEXP_REALCHAR:
1860 neg = !neg;
1861 case XML_REGEXP_NOTREALCHAR:
1862 ret = xmlUCSIsCatP(codepoint);
1863 if (ret == 0)
1864 ret = xmlUCSIsCatZ(codepoint);
1865 if (ret == 0)
1866 ret = xmlUCSIsCatC(codepoint);
1867 break;
1868 case XML_REGEXP_LETTER:
1869 ret = xmlUCSIsCatL(codepoint);
1870 break;
1871 case XML_REGEXP_LETTER_UPPERCASE:
1872 ret = xmlUCSIsCatLu(codepoint);
1873 break;
1874 case XML_REGEXP_LETTER_LOWERCASE:
1875 ret = xmlUCSIsCatLl(codepoint);
1876 break;
1877 case XML_REGEXP_LETTER_TITLECASE:
1878 ret = xmlUCSIsCatLt(codepoint);
1879 break;
1880 case XML_REGEXP_LETTER_MODIFIER:
1881 ret = xmlUCSIsCatLm(codepoint);
1882 break;
1883 case XML_REGEXP_LETTER_OTHERS:
1884 ret = xmlUCSIsCatLo(codepoint);
1885 break;
1886 case XML_REGEXP_MARK:
1887 ret = xmlUCSIsCatM(codepoint);
1888 break;
1889 case XML_REGEXP_MARK_NONSPACING:
1890 ret = xmlUCSIsCatMn(codepoint);
1891 break;
1892 case XML_REGEXP_MARK_SPACECOMBINING:
1893 ret = xmlUCSIsCatMc(codepoint);
1894 break;
1895 case XML_REGEXP_MARK_ENCLOSING:
1896 ret = xmlUCSIsCatMe(codepoint);
1897 break;
1898 case XML_REGEXP_NUMBER:
1899 ret = xmlUCSIsCatN(codepoint);
1900 break;
1901 case XML_REGEXP_NUMBER_DECIMAL:
1902 ret = xmlUCSIsCatNd(codepoint);
1903 break;
1904 case XML_REGEXP_NUMBER_LETTER:
1905 ret = xmlUCSIsCatNl(codepoint);
1906 break;
1907 case XML_REGEXP_NUMBER_OTHERS:
1908 ret = xmlUCSIsCatNo(codepoint);
1909 break;
1910 case XML_REGEXP_PUNCT:
1911 ret = xmlUCSIsCatP(codepoint);
1912 break;
1913 case XML_REGEXP_PUNCT_CONNECTOR:
1914 ret = xmlUCSIsCatPc(codepoint);
1915 break;
1916 case XML_REGEXP_PUNCT_DASH:
1917 ret = xmlUCSIsCatPd(codepoint);
1918 break;
1919 case XML_REGEXP_PUNCT_OPEN:
1920 ret = xmlUCSIsCatPs(codepoint);
1921 break;
1922 case XML_REGEXP_PUNCT_CLOSE:
1923 ret = xmlUCSIsCatPe(codepoint);
1924 break;
1925 case XML_REGEXP_PUNCT_INITQUOTE:
1926 ret = xmlUCSIsCatPi(codepoint);
1927 break;
1928 case XML_REGEXP_PUNCT_FINQUOTE:
1929 ret = xmlUCSIsCatPf(codepoint);
1930 break;
1931 case XML_REGEXP_PUNCT_OTHERS:
1932 ret = xmlUCSIsCatPo(codepoint);
1933 break;
1934 case XML_REGEXP_SEPAR:
1935 ret = xmlUCSIsCatZ(codepoint);
1936 break;
1937 case XML_REGEXP_SEPAR_SPACE:
1938 ret = xmlUCSIsCatZs(codepoint);
1939 break;
1940 case XML_REGEXP_SEPAR_LINE:
1941 ret = xmlUCSIsCatZl(codepoint);
1942 break;
1943 case XML_REGEXP_SEPAR_PARA:
1944 ret = xmlUCSIsCatZp(codepoint);
1945 break;
1946 case XML_REGEXP_SYMBOL:
1947 ret = xmlUCSIsCatS(codepoint);
1948 break;
1949 case XML_REGEXP_SYMBOL_MATH:
1950 ret = xmlUCSIsCatSm(codepoint);
1951 break;
1952 case XML_REGEXP_SYMBOL_CURRENCY:
1953 ret = xmlUCSIsCatSc(codepoint);
1954 break;
1955 case XML_REGEXP_SYMBOL_MODIFIER:
1956 ret = xmlUCSIsCatSk(codepoint);
1957 break;
1958 case XML_REGEXP_SYMBOL_OTHERS:
1959 ret = xmlUCSIsCatSo(codepoint);
1960 break;
1961 case XML_REGEXP_OTHER:
1962 ret = xmlUCSIsCatC(codepoint);
1963 break;
1964 case XML_REGEXP_OTHER_CONTROL:
1965 ret = xmlUCSIsCatCc(codepoint);
1966 break;
1967 case XML_REGEXP_OTHER_FORMAT:
1968 ret = xmlUCSIsCatCf(codepoint);
1969 break;
1970 case XML_REGEXP_OTHER_PRIVATE:
1971 ret = xmlUCSIsCatCo(codepoint);
1972 break;
1973 case XML_REGEXP_OTHER_NA:
1974 /* ret = xmlUCSIsCatCn(codepoint); */
1975 /* Seems it doesn't exist anymore in recent Unicode releases */
1976 ret = 0;
1977 break;
1978 case XML_REGEXP_BLOCK_NAME:
1979 ret = xmlUCSIsBlock(codepoint, (const char *) blockName);
1980 break;
1981 }
1982 if (neg)
1983 return(!ret);
1984 return(ret);
1985}
1986
1987static int
1988xmlRegCheckCharacter(xmlRegAtomPtr atom, int codepoint) {
1989 int i, ret = 0;
1990 xmlRegRangePtr range;
1991
William M. Brack871611b2003-10-18 04:53:14 +00001992 if ((atom == NULL) || (!IS_CHAR(codepoint)))
Daniel Veillard4255d502002-04-16 15:50:10 +00001993 return(-1);
1994
1995 switch (atom->type) {
1996 case XML_REGEXP_SUBREG:
1997 case XML_REGEXP_EPSILON:
1998 return(-1);
1999 case XML_REGEXP_CHARVAL:
2000 return(codepoint == atom->codepoint);
2001 case XML_REGEXP_RANGES: {
2002 int accept = 0;
Daniel Veillardf2a12832003-11-24 13:04:35 +00002003
Daniel Veillard4255d502002-04-16 15:50:10 +00002004 for (i = 0;i < atom->nbRanges;i++) {
2005 range = atom->ranges[i];
Daniel Veillardf8b9de32003-11-24 14:27:26 +00002006 if (range->neg == 2) {
Daniel Veillard4255d502002-04-16 15:50:10 +00002007 ret = xmlRegCheckCharacterRange(range->type, codepoint,
2008 0, range->start, range->end,
2009 range->blockName);
2010 if (ret != 0)
2011 return(0); /* excluded char */
Daniel Veillardf8b9de32003-11-24 14:27:26 +00002012 } else if (range->neg) {
2013 ret = xmlRegCheckCharacterRange(range->type, codepoint,
2014 0, range->start, range->end,
2015 range->blockName);
2016 if (ret == 0)
Daniel Veillardf2a12832003-11-24 13:04:35 +00002017 accept = 1;
Daniel Veillardf8b9de32003-11-24 14:27:26 +00002018 else
2019 return(0);
Daniel Veillard4255d502002-04-16 15:50:10 +00002020 } else {
2021 ret = xmlRegCheckCharacterRange(range->type, codepoint,
2022 0, range->start, range->end,
2023 range->blockName);
2024 if (ret != 0)
2025 accept = 1; /* might still be excluded */
2026 }
2027 }
2028 return(accept);
2029 }
2030 case XML_REGEXP_STRING:
2031 printf("TODO: XML_REGEXP_STRING\n");
2032 return(-1);
2033 case XML_REGEXP_ANYCHAR:
2034 case XML_REGEXP_ANYSPACE:
2035 case XML_REGEXP_NOTSPACE:
2036 case XML_REGEXP_INITNAME:
2037 case XML_REGEXP_NOTINITNAME:
2038 case XML_REGEXP_NAMECHAR:
2039 case XML_REGEXP_NOTNAMECHAR:
2040 case XML_REGEXP_DECIMAL:
2041 case XML_REGEXP_NOTDECIMAL:
2042 case XML_REGEXP_REALCHAR:
2043 case XML_REGEXP_NOTREALCHAR:
2044 case XML_REGEXP_LETTER:
2045 case XML_REGEXP_LETTER_UPPERCASE:
2046 case XML_REGEXP_LETTER_LOWERCASE:
2047 case XML_REGEXP_LETTER_TITLECASE:
2048 case XML_REGEXP_LETTER_MODIFIER:
2049 case XML_REGEXP_LETTER_OTHERS:
2050 case XML_REGEXP_MARK:
2051 case XML_REGEXP_MARK_NONSPACING:
2052 case XML_REGEXP_MARK_SPACECOMBINING:
2053 case XML_REGEXP_MARK_ENCLOSING:
2054 case XML_REGEXP_NUMBER:
2055 case XML_REGEXP_NUMBER_DECIMAL:
2056 case XML_REGEXP_NUMBER_LETTER:
2057 case XML_REGEXP_NUMBER_OTHERS:
2058 case XML_REGEXP_PUNCT:
2059 case XML_REGEXP_PUNCT_CONNECTOR:
2060 case XML_REGEXP_PUNCT_DASH:
2061 case XML_REGEXP_PUNCT_OPEN:
2062 case XML_REGEXP_PUNCT_CLOSE:
2063 case XML_REGEXP_PUNCT_INITQUOTE:
2064 case XML_REGEXP_PUNCT_FINQUOTE:
2065 case XML_REGEXP_PUNCT_OTHERS:
2066 case XML_REGEXP_SEPAR:
2067 case XML_REGEXP_SEPAR_SPACE:
2068 case XML_REGEXP_SEPAR_LINE:
2069 case XML_REGEXP_SEPAR_PARA:
2070 case XML_REGEXP_SYMBOL:
2071 case XML_REGEXP_SYMBOL_MATH:
2072 case XML_REGEXP_SYMBOL_CURRENCY:
2073 case XML_REGEXP_SYMBOL_MODIFIER:
2074 case XML_REGEXP_SYMBOL_OTHERS:
2075 case XML_REGEXP_OTHER:
2076 case XML_REGEXP_OTHER_CONTROL:
2077 case XML_REGEXP_OTHER_FORMAT:
2078 case XML_REGEXP_OTHER_PRIVATE:
2079 case XML_REGEXP_OTHER_NA:
2080 case XML_REGEXP_BLOCK_NAME:
2081 ret = xmlRegCheckCharacterRange(atom->type, codepoint, 0, 0, 0,
2082 (const xmlChar *)atom->valuep);
2083 if (atom->neg)
2084 ret = !ret;
2085 break;
2086 }
2087 return(ret);
2088}
2089
2090/************************************************************************
2091 * *
2092 * Saving an restoring state of an execution context *
2093 * *
2094 ************************************************************************/
2095
2096#ifdef DEBUG_REGEXP_EXEC
2097static void
2098xmlFARegDebugExec(xmlRegExecCtxtPtr exec) {
2099 printf("state: %d:%d:idx %d", exec->state->no, exec->transno, exec->index);
2100 if (exec->inputStack != NULL) {
2101 int i;
2102 printf(": ");
2103 for (i = 0;(i < 3) && (i < exec->inputStackNr);i++)
2104 printf("%s ", exec->inputStack[exec->inputStackNr - (i + 1)]);
2105 } else {
2106 printf(": %s", &(exec->inputString[exec->index]));
2107 }
2108 printf("\n");
2109}
2110#endif
2111
2112static void
2113xmlFARegExecSave(xmlRegExecCtxtPtr exec) {
2114#ifdef DEBUG_REGEXP_EXEC
2115 printf("saving ");
2116 exec->transno++;
2117 xmlFARegDebugExec(exec);
2118 exec->transno--;
2119#endif
2120
2121 if (exec->maxRollbacks == 0) {
2122 exec->maxRollbacks = 4;
2123 exec->rollbacks = (xmlRegExecRollback *) xmlMalloc(exec->maxRollbacks *
2124 sizeof(xmlRegExecRollback));
2125 if (exec->rollbacks == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00002126 xmlRegexpErrMemory(NULL, "saving regexp");
Daniel Veillard4255d502002-04-16 15:50:10 +00002127 exec->maxRollbacks = 0;
2128 return;
2129 }
2130 memset(exec->rollbacks, 0,
2131 exec->maxRollbacks * sizeof(xmlRegExecRollback));
2132 } else if (exec->nbRollbacks >= exec->maxRollbacks) {
2133 xmlRegExecRollback *tmp;
2134 int len = exec->maxRollbacks;
2135
2136 exec->maxRollbacks *= 2;
2137 tmp = (xmlRegExecRollback *) xmlRealloc(exec->rollbacks,
2138 exec->maxRollbacks * sizeof(xmlRegExecRollback));
2139 if (tmp == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00002140 xmlRegexpErrMemory(NULL, "saving regexp");
Daniel Veillard4255d502002-04-16 15:50:10 +00002141 exec->maxRollbacks /= 2;
2142 return;
2143 }
2144 exec->rollbacks = tmp;
2145 tmp = &exec->rollbacks[len];
2146 memset(tmp, 0, (exec->maxRollbacks - len) * sizeof(xmlRegExecRollback));
2147 }
2148 exec->rollbacks[exec->nbRollbacks].state = exec->state;
2149 exec->rollbacks[exec->nbRollbacks].index = exec->index;
2150 exec->rollbacks[exec->nbRollbacks].nextbranch = exec->transno + 1;
2151 if (exec->comp->nbCounters > 0) {
2152 if (exec->rollbacks[exec->nbRollbacks].counts == NULL) {
2153 exec->rollbacks[exec->nbRollbacks].counts = (int *)
2154 xmlMalloc(exec->comp->nbCounters * sizeof(int));
2155 if (exec->rollbacks[exec->nbRollbacks].counts == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00002156 xmlRegexpErrMemory(NULL, "saving regexp");
Daniel Veillard4255d502002-04-16 15:50:10 +00002157 exec->status = -5;
2158 return;
2159 }
2160 }
2161 memcpy(exec->rollbacks[exec->nbRollbacks].counts, exec->counts,
2162 exec->comp->nbCounters * sizeof(int));
2163 }
2164 exec->nbRollbacks++;
2165}
2166
2167static void
2168xmlFARegExecRollBack(xmlRegExecCtxtPtr exec) {
2169 if (exec->nbRollbacks <= 0) {
2170 exec->status = -1;
2171#ifdef DEBUG_REGEXP_EXEC
2172 printf("rollback failed on empty stack\n");
2173#endif
2174 return;
2175 }
2176 exec->nbRollbacks--;
2177 exec->state = exec->rollbacks[exec->nbRollbacks].state;
2178 exec->index = exec->rollbacks[exec->nbRollbacks].index;
2179 exec->transno = exec->rollbacks[exec->nbRollbacks].nextbranch;
2180 if (exec->comp->nbCounters > 0) {
2181 if (exec->rollbacks[exec->nbRollbacks].counts == NULL) {
2182 fprintf(stderr, "exec save: allocation failed");
2183 exec->status = -6;
2184 return;
2185 }
2186 memcpy(exec->counts, exec->rollbacks[exec->nbRollbacks].counts,
2187 exec->comp->nbCounters * sizeof(int));
2188 }
2189
2190#ifdef DEBUG_REGEXP_EXEC
2191 printf("restored ");
2192 xmlFARegDebugExec(exec);
2193#endif
2194}
2195
2196/************************************************************************
2197 * *
2198 * Verifyer, running an input against a compiled regexp *
2199 * *
2200 ************************************************************************/
2201
2202static int
2203xmlFARegExec(xmlRegexpPtr comp, const xmlChar *content) {
2204 xmlRegExecCtxt execval;
2205 xmlRegExecCtxtPtr exec = &execval;
2206 int ret, codepoint, len;
2207
2208 exec->inputString = content;
2209 exec->index = 0;
2210 exec->determinist = 1;
2211 exec->maxRollbacks = 0;
2212 exec->nbRollbacks = 0;
2213 exec->rollbacks = NULL;
2214 exec->status = 0;
2215 exec->comp = comp;
2216 exec->state = comp->states[0];
2217 exec->transno = 0;
2218 exec->transcount = 0;
Daniel Veillardf2a12832003-11-24 13:04:35 +00002219 exec->inputStack = NULL;
2220 exec->inputStackMax = 0;
Daniel Veillard4255d502002-04-16 15:50:10 +00002221 if (comp->nbCounters > 0) {
2222 exec->counts = (int *) xmlMalloc(comp->nbCounters * sizeof(int));
Daniel Veillardff46a042003-10-08 08:53:17 +00002223 if (exec->counts == NULL) {
2224 xmlRegexpErrMemory(NULL, "running regexp");
Daniel Veillard4255d502002-04-16 15:50:10 +00002225 return(-1);
Daniel Veillardff46a042003-10-08 08:53:17 +00002226 }
Daniel Veillard4255d502002-04-16 15:50:10 +00002227 memset(exec->counts, 0, comp->nbCounters * sizeof(int));
2228 } else
2229 exec->counts = NULL;
2230 while ((exec->status == 0) &&
2231 ((exec->inputString[exec->index] != 0) ||
2232 (exec->state->type != XML_REGEXP_FINAL_STATE))) {
2233 xmlRegTransPtr trans;
2234 xmlRegAtomPtr atom;
2235
2236 /*
2237 * End of input on non-terminal state, rollback, however we may
2238 * still have epsilon like transition for counted transitions
2239 * on counters, in that case don't break too early.
2240 */
2241 if ((exec->inputString[exec->index] == 0) && (exec->counts == NULL))
2242 goto rollback;
2243
2244 exec->transcount = 0;
2245 for (;exec->transno < exec->state->nbTrans;exec->transno++) {
2246 trans = &exec->state->trans[exec->transno];
2247 if (trans->to < 0)
2248 continue;
2249 atom = trans->atom;
2250 ret = 0;
2251 if (trans->count >= 0) {
2252 int count;
2253 xmlRegCounterPtr counter;
2254
2255 /*
2256 * A counted transition.
2257 */
2258
2259 count = exec->counts[trans->count];
2260 counter = &exec->comp->counters[trans->count];
2261#ifdef DEBUG_REGEXP_EXEC
2262 printf("testing count %d: val %d, min %d, max %d\n",
2263 trans->count, count, counter->min, counter->max);
2264#endif
2265 ret = ((count >= counter->min) && (count <= counter->max));
2266 } else if (atom == NULL) {
2267 fprintf(stderr, "epsilon transition left at runtime\n");
2268 exec->status = -2;
2269 break;
2270 } else if (exec->inputString[exec->index] != 0) {
2271 codepoint = CUR_SCHAR(&(exec->inputString[exec->index]), len);
2272 ret = xmlRegCheckCharacter(atom, codepoint);
2273 if ((ret == 1) && (atom->min > 0) && (atom->max > 0)) {
2274 xmlRegStatePtr to = comp->states[trans->to];
2275
2276 /*
2277 * this is a multiple input sequence
2278 */
2279 if (exec->state->nbTrans > exec->transno + 1) {
2280 xmlFARegExecSave(exec);
2281 }
2282 exec->transcount = 1;
2283 do {
2284 /*
2285 * Try to progress as much as possible on the input
2286 */
2287 if (exec->transcount == atom->max) {
2288 break;
2289 }
2290 exec->index += len;
2291 /*
2292 * End of input: stop here
2293 */
2294 if (exec->inputString[exec->index] == 0) {
2295 exec->index -= len;
2296 break;
2297 }
2298 if (exec->transcount >= atom->min) {
2299 int transno = exec->transno;
2300 xmlRegStatePtr state = exec->state;
2301
2302 /*
2303 * The transition is acceptable save it
2304 */
2305 exec->transno = -1; /* trick */
2306 exec->state = to;
2307 xmlFARegExecSave(exec);
2308 exec->transno = transno;
2309 exec->state = state;
2310 }
2311 codepoint = CUR_SCHAR(&(exec->inputString[exec->index]),
2312 len);
2313 ret = xmlRegCheckCharacter(atom, codepoint);
2314 exec->transcount++;
2315 } while (ret == 1);
2316 if (exec->transcount < atom->min)
2317 ret = 0;
2318
2319 /*
2320 * If the last check failed but one transition was found
2321 * possible, rollback
2322 */
2323 if (ret < 0)
2324 ret = 0;
2325 if (ret == 0) {
2326 goto rollback;
2327 }
2328 }
2329 }
2330 if (ret == 1) {
2331 if (exec->state->nbTrans > exec->transno + 1) {
2332 xmlFARegExecSave(exec);
2333 }
2334 if (trans->counter >= 0) {
2335#ifdef DEBUG_REGEXP_EXEC
2336 printf("Increasing count %d\n", trans->counter);
2337#endif
2338 exec->counts[trans->counter]++;
2339 }
2340#ifdef DEBUG_REGEXP_EXEC
2341 printf("entering state %d\n", trans->to);
2342#endif
2343 exec->state = comp->states[trans->to];
2344 exec->transno = 0;
2345 if (trans->atom != NULL) {
2346 exec->index += len;
2347 }
2348 goto progress;
2349 } else if (ret < 0) {
2350 exec->status = -4;
2351 break;
2352 }
2353 }
2354 if ((exec->transno != 0) || (exec->state->nbTrans == 0)) {
2355rollback:
2356 /*
2357 * Failed to find a way out
2358 */
2359 exec->determinist = 0;
2360 xmlFARegExecRollBack(exec);
2361 }
2362progress:
2363 continue;
2364 }
2365 if (exec->rollbacks != NULL) {
2366 if (exec->counts != NULL) {
2367 int i;
2368
2369 for (i = 0;i < exec->maxRollbacks;i++)
2370 if (exec->rollbacks[i].counts != NULL)
2371 xmlFree(exec->rollbacks[i].counts);
2372 }
2373 xmlFree(exec->rollbacks);
2374 }
2375 if (exec->counts != NULL)
2376 xmlFree(exec->counts);
2377 if (exec->status == 0)
2378 return(1);
2379 if (exec->status == -1)
2380 return(0);
2381 return(exec->status);
2382}
2383
2384/************************************************************************
2385 * *
2386 * Progressive interface to the verifyer one atom at a time *
2387 * *
2388 ************************************************************************/
2389
2390/**
Daniel Veillard01c13b52002-12-10 15:19:08 +00002391 * xmlRegNewExecCtxt:
Daniel Veillard4255d502002-04-16 15:50:10 +00002392 * @comp: a precompiled regular expression
2393 * @callback: a callback function used for handling progresses in the
2394 * automata matching phase
2395 * @data: the context data associated to the callback in this context
2396 *
2397 * Build a context used for progressive evaluation of a regexp.
Daniel Veillard01c13b52002-12-10 15:19:08 +00002398 *
2399 * Returns the new context
Daniel Veillard4255d502002-04-16 15:50:10 +00002400 */
2401xmlRegExecCtxtPtr
2402xmlRegNewExecCtxt(xmlRegexpPtr comp, xmlRegExecCallbacks callback, void *data) {
2403 xmlRegExecCtxtPtr exec;
2404
2405 if (comp == NULL)
2406 return(NULL);
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00002407 if ((comp->compact == NULL) && (comp->states == NULL))
2408 return(NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +00002409 exec = (xmlRegExecCtxtPtr) xmlMalloc(sizeof(xmlRegExecCtxt));
2410 if (exec == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00002411 xmlRegexpErrMemory(NULL, "creating execution context");
Daniel Veillard4255d502002-04-16 15:50:10 +00002412 return(NULL);
2413 }
2414 memset(exec, 0, sizeof(xmlRegExecCtxt));
2415 exec->inputString = NULL;
2416 exec->index = 0;
2417 exec->determinist = 1;
2418 exec->maxRollbacks = 0;
2419 exec->nbRollbacks = 0;
2420 exec->rollbacks = NULL;
2421 exec->status = 0;
2422 exec->comp = comp;
Daniel Veillard23e73572002-09-19 19:56:43 +00002423 if (comp->compact == NULL)
2424 exec->state = comp->states[0];
Daniel Veillard4255d502002-04-16 15:50:10 +00002425 exec->transno = 0;
2426 exec->transcount = 0;
2427 exec->callback = callback;
2428 exec->data = data;
2429 if (comp->nbCounters > 0) {
2430 exec->counts = (int *) xmlMalloc(comp->nbCounters * sizeof(int));
2431 if (exec->counts == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00002432 xmlRegexpErrMemory(NULL, "creating execution context");
Daniel Veillard4255d502002-04-16 15:50:10 +00002433 xmlFree(exec);
2434 return(NULL);
2435 }
2436 memset(exec->counts, 0, comp->nbCounters * sizeof(int));
2437 } else
2438 exec->counts = NULL;
2439 exec->inputStackMax = 0;
2440 exec->inputStackNr = 0;
2441 exec->inputStack = NULL;
2442 return(exec);
2443}
2444
2445/**
2446 * xmlRegFreeExecCtxt:
2447 * @exec: a regular expression evaulation context
2448 *
2449 * Free the structures associated to a regular expression evaulation context.
2450 */
2451void
2452xmlRegFreeExecCtxt(xmlRegExecCtxtPtr exec) {
2453 if (exec == NULL)
2454 return;
2455
2456 if (exec->rollbacks != NULL) {
2457 if (exec->counts != NULL) {
2458 int i;
2459
2460 for (i = 0;i < exec->maxRollbacks;i++)
2461 if (exec->rollbacks[i].counts != NULL)
2462 xmlFree(exec->rollbacks[i].counts);
2463 }
2464 xmlFree(exec->rollbacks);
2465 }
2466 if (exec->counts != NULL)
2467 xmlFree(exec->counts);
2468 if (exec->inputStack != NULL) {
2469 int i;
2470
Daniel Veillard32370232002-10-16 14:08:14 +00002471 for (i = 0;i < exec->inputStackNr;i++) {
2472 if (exec->inputStack[i].value != NULL)
2473 xmlFree(exec->inputStack[i].value);
2474 }
Daniel Veillard4255d502002-04-16 15:50:10 +00002475 xmlFree(exec->inputStack);
2476 }
2477 xmlFree(exec);
2478}
2479
2480static void
2481xmlFARegExecSaveInputString(xmlRegExecCtxtPtr exec, const xmlChar *value,
2482 void *data) {
2483#ifdef DEBUG_PUSH
2484 printf("saving value: %d:%s\n", exec->inputStackNr, value);
2485#endif
2486 if (exec->inputStackMax == 0) {
2487 exec->inputStackMax = 4;
2488 exec->inputStack = (xmlRegInputTokenPtr)
2489 xmlMalloc(exec->inputStackMax * sizeof(xmlRegInputToken));
2490 if (exec->inputStack == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00002491 xmlRegexpErrMemory(NULL, "pushing input string");
Daniel Veillard4255d502002-04-16 15:50:10 +00002492 exec->inputStackMax = 0;
2493 return;
2494 }
2495 } else if (exec->inputStackNr + 1 >= exec->inputStackMax) {
2496 xmlRegInputTokenPtr tmp;
2497
2498 exec->inputStackMax *= 2;
2499 tmp = (xmlRegInputTokenPtr) xmlRealloc(exec->inputStack,
2500 exec->inputStackMax * sizeof(xmlRegInputToken));
2501 if (tmp == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00002502 xmlRegexpErrMemory(NULL, "pushing input string");
Daniel Veillard4255d502002-04-16 15:50:10 +00002503 exec->inputStackMax /= 2;
2504 return;
2505 }
2506 exec->inputStack = tmp;
2507 }
2508 exec->inputStack[exec->inputStackNr].value = xmlStrdup(value);
2509 exec->inputStack[exec->inputStackNr].data = data;
2510 exec->inputStackNr++;
2511 exec->inputStack[exec->inputStackNr].value = NULL;
2512 exec->inputStack[exec->inputStackNr].data = NULL;
2513}
2514
2515
2516/**
Daniel Veillard23e73572002-09-19 19:56:43 +00002517 * xmlRegCompactPushString:
2518 * @exec: a regexp execution context
2519 * @comp: the precompiled exec with a compact table
2520 * @value: a string token input
2521 * @data: data associated to the token to reuse in callbacks
2522 *
2523 * Push one input token in the execution context
2524 *
2525 * Returns: 1 if the regexp reached a final state, 0 if non-final, and
2526 * a negative value in case of error.
2527 */
2528static int
2529xmlRegCompactPushString(xmlRegExecCtxtPtr exec,
2530 xmlRegexpPtr comp,
2531 const xmlChar *value,
2532 void *data) {
2533 int state = exec->index;
2534 int i, target;
2535
2536 if ((comp == NULL) || (comp->compact == NULL) || (comp->stringMap == NULL))
2537 return(-1);
2538
2539 if (value == NULL) {
2540 /*
2541 * are we at a final state ?
2542 */
2543 if (comp->compact[state * (comp->nbstrings + 1)] ==
2544 XML_REGEXP_FINAL_STATE)
2545 return(1);
2546 return(0);
2547 }
2548
2549#ifdef DEBUG_PUSH
2550 printf("value pushed: %s\n", value);
2551#endif
2552
2553 /*
2554 * Examine all outside transition from current state
2555 */
2556 for (i = 0;i < comp->nbstrings;i++) {
2557 target = comp->compact[state * (comp->nbstrings + 1) + i + 1];
2558 if ((target > 0) && (target <= comp->nbstates)) {
2559 target--; /* to avoid 0 */
2560 if (xmlStrEqual(comp->stringMap[i], value)) {
2561 exec->index = target;
Daniel Veillard118aed72002-09-24 14:13:13 +00002562 if ((exec->callback != NULL) && (comp->transdata != NULL)) {
2563 exec->callback(exec->data, value,
2564 comp->transdata[state * comp->nbstrings + i], data);
2565 }
Daniel Veillard23e73572002-09-19 19:56:43 +00002566#ifdef DEBUG_PUSH
2567 printf("entering state %d\n", target);
2568#endif
2569 if (comp->compact[target * (comp->nbstrings + 1)] ==
2570 XML_REGEXP_FINAL_STATE)
2571 return(1);
2572 return(0);
2573 }
2574 }
2575 }
2576 /*
2577 * Failed to find an exit transition out from current state for the
2578 * current token
2579 */
2580#ifdef DEBUG_PUSH
2581 printf("failed to find a transition for %s on state %d\n", value, state);
2582#endif
2583 exec->status = -1;
2584 return(-1);
2585}
2586
2587/**
Daniel Veillard4255d502002-04-16 15:50:10 +00002588 * xmlRegExecPushString:
Daniel Veillardea7751d2002-12-20 00:16:24 +00002589 * @exec: a regexp execution context or NULL to indicate the end
Daniel Veillard4255d502002-04-16 15:50:10 +00002590 * @value: a string token input
2591 * @data: data associated to the token to reuse in callbacks
2592 *
2593 * Push one input token in the execution context
2594 *
2595 * Returns: 1 if the regexp reached a final state, 0 if non-final, and
2596 * a negative value in case of error.
2597 */
2598int
2599xmlRegExecPushString(xmlRegExecCtxtPtr exec, const xmlChar *value,
2600 void *data) {
2601 xmlRegTransPtr trans;
2602 xmlRegAtomPtr atom;
2603 int ret;
2604 int final = 0;
2605
2606 if (exec == NULL)
2607 return(-1);
Daniel Veillard23e73572002-09-19 19:56:43 +00002608 if (exec->comp == NULL)
2609 return(-1);
Daniel Veillard4255d502002-04-16 15:50:10 +00002610 if (exec->status != 0)
2611 return(exec->status);
2612
Daniel Veillard23e73572002-09-19 19:56:43 +00002613 if (exec->comp->compact != NULL)
2614 return(xmlRegCompactPushString(exec, exec->comp, value, data));
2615
Daniel Veillard4255d502002-04-16 15:50:10 +00002616 if (value == NULL) {
2617 if (exec->state->type == XML_REGEXP_FINAL_STATE)
2618 return(1);
2619 final = 1;
2620 }
2621
2622#ifdef DEBUG_PUSH
2623 printf("value pushed: %s\n", value);
2624#endif
2625 /*
2626 * If we have an active rollback stack push the new value there
2627 * and get back to where we were left
2628 */
2629 if ((value != NULL) && (exec->inputStackNr > 0)) {
2630 xmlFARegExecSaveInputString(exec, value, data);
2631 value = exec->inputStack[exec->index].value;
2632 data = exec->inputStack[exec->index].data;
2633#ifdef DEBUG_PUSH
2634 printf("value loaded: %s\n", value);
2635#endif
2636 }
2637
2638 while ((exec->status == 0) &&
2639 ((value != NULL) ||
2640 ((final == 1) &&
2641 (exec->state->type != XML_REGEXP_FINAL_STATE)))) {
2642
2643 /*
2644 * End of input on non-terminal state, rollback, however we may
2645 * still have epsilon like transition for counted transitions
2646 * on counters, in that case don't break too early.
2647 */
Daniel Veillardb509f152002-04-17 16:28:10 +00002648 if ((value == NULL) && (exec->counts == NULL))
Daniel Veillard4255d502002-04-16 15:50:10 +00002649 goto rollback;
2650
2651 exec->transcount = 0;
2652 for (;exec->transno < exec->state->nbTrans;exec->transno++) {
2653 trans = &exec->state->trans[exec->transno];
2654 if (trans->to < 0)
2655 continue;
2656 atom = trans->atom;
2657 ret = 0;
Daniel Veillard441bc322002-04-20 17:38:48 +00002658 if (trans->count == REGEXP_ALL_LAX_COUNTER) {
2659 int i;
2660 int count;
2661 xmlRegTransPtr t;
2662 xmlRegCounterPtr counter;
2663
2664 ret = 0;
2665
2666#ifdef DEBUG_PUSH
2667 printf("testing all lax %d\n", trans->count);
2668#endif
2669 /*
2670 * Check all counted transitions from the current state
2671 */
2672 if ((value == NULL) && (final)) {
2673 ret = 1;
2674 } else if (value != NULL) {
2675 for (i = 0;i < exec->state->nbTrans;i++) {
2676 t = &exec->state->trans[i];
2677 if ((t->counter < 0) || (t == trans))
2678 continue;
2679 counter = &exec->comp->counters[t->counter];
2680 count = exec->counts[t->counter];
2681 if ((count < counter->max) &&
2682 (t->atom != NULL) &&
2683 (xmlStrEqual(value, t->atom->valuep))) {
2684 ret = 0;
2685 break;
2686 }
2687 if ((count >= counter->min) &&
2688 (count < counter->max) &&
2689 (xmlStrEqual(value, t->atom->valuep))) {
2690 ret = 1;
2691 break;
2692 }
2693 }
2694 }
2695 } else if (trans->count == REGEXP_ALL_COUNTER) {
Daniel Veillard8a001f62002-04-20 07:24:11 +00002696 int i;
2697 int count;
2698 xmlRegTransPtr t;
2699 xmlRegCounterPtr counter;
2700
2701 ret = 1;
2702
2703#ifdef DEBUG_PUSH
2704 printf("testing all %d\n", trans->count);
2705#endif
2706 /*
2707 * Check all counted transitions from the current state
2708 */
2709 for (i = 0;i < exec->state->nbTrans;i++) {
2710 t = &exec->state->trans[i];
2711 if ((t->counter < 0) || (t == trans))
2712 continue;
2713 counter = &exec->comp->counters[t->counter];
2714 count = exec->counts[t->counter];
2715 if ((count < counter->min) || (count > counter->max)) {
2716 ret = 0;
2717 break;
2718 }
2719 }
2720 } else if (trans->count >= 0) {
Daniel Veillard4255d502002-04-16 15:50:10 +00002721 int count;
2722 xmlRegCounterPtr counter;
2723
2724 /*
2725 * A counted transition.
2726 */
2727
2728 count = exec->counts[trans->count];
2729 counter = &exec->comp->counters[trans->count];
2730#ifdef DEBUG_PUSH
2731 printf("testing count %d: val %d, min %d, max %d\n",
2732 trans->count, count, counter->min, counter->max);
2733#endif
2734 ret = ((count >= counter->min) && (count <= counter->max));
2735 } else if (atom == NULL) {
2736 fprintf(stderr, "epsilon transition left at runtime\n");
2737 exec->status = -2;
2738 break;
2739 } else if (value != NULL) {
2740 ret = xmlStrEqual(value, atom->valuep);
Daniel Veillard441bc322002-04-20 17:38:48 +00002741 if ((ret == 1) && (trans->counter >= 0)) {
2742 xmlRegCounterPtr counter;
2743 int count;
2744
2745 count = exec->counts[trans->counter];
2746 counter = &exec->comp->counters[trans->counter];
2747 if (count >= counter->max)
2748 ret = 0;
2749 }
2750
Daniel Veillard4255d502002-04-16 15:50:10 +00002751 if ((ret == 1) && (atom->min > 0) && (atom->max > 0)) {
2752 xmlRegStatePtr to = exec->comp->states[trans->to];
2753
2754 /*
2755 * this is a multiple input sequence
2756 */
2757 if (exec->state->nbTrans > exec->transno + 1) {
2758 if (exec->inputStackNr <= 0) {
2759 xmlFARegExecSaveInputString(exec, value, data);
2760 }
2761 xmlFARegExecSave(exec);
2762 }
2763 exec->transcount = 1;
2764 do {
2765 /*
2766 * Try to progress as much as possible on the input
2767 */
2768 if (exec->transcount == atom->max) {
2769 break;
2770 }
2771 exec->index++;
2772 value = exec->inputStack[exec->index].value;
2773 data = exec->inputStack[exec->index].data;
2774#ifdef DEBUG_PUSH
2775 printf("value loaded: %s\n", value);
2776#endif
2777
2778 /*
2779 * End of input: stop here
2780 */
2781 if (value == NULL) {
2782 exec->index --;
2783 break;
2784 }
2785 if (exec->transcount >= atom->min) {
2786 int transno = exec->transno;
2787 xmlRegStatePtr state = exec->state;
2788
2789 /*
2790 * The transition is acceptable save it
2791 */
2792 exec->transno = -1; /* trick */
2793 exec->state = to;
2794 if (exec->inputStackNr <= 0) {
2795 xmlFARegExecSaveInputString(exec, value, data);
2796 }
2797 xmlFARegExecSave(exec);
2798 exec->transno = transno;
2799 exec->state = state;
2800 }
2801 ret = xmlStrEqual(value, atom->valuep);
2802 exec->transcount++;
2803 } while (ret == 1);
2804 if (exec->transcount < atom->min)
2805 ret = 0;
2806
2807 /*
2808 * If the last check failed but one transition was found
2809 * possible, rollback
2810 */
2811 if (ret < 0)
2812 ret = 0;
2813 if (ret == 0) {
2814 goto rollback;
2815 }
2816 }
2817 }
2818 if (ret == 1) {
2819 if ((exec->callback != NULL) && (atom != NULL)) {
2820 exec->callback(exec->data, atom->valuep,
2821 atom->data, data);
2822 }
2823 if (exec->state->nbTrans > exec->transno + 1) {
2824 if (exec->inputStackNr <= 0) {
2825 xmlFARegExecSaveInputString(exec, value, data);
2826 }
2827 xmlFARegExecSave(exec);
2828 }
2829 if (trans->counter >= 0) {
2830#ifdef DEBUG_PUSH
2831 printf("Increasing count %d\n", trans->counter);
2832#endif
2833 exec->counts[trans->counter]++;
2834 }
2835#ifdef DEBUG_PUSH
2836 printf("entering state %d\n", trans->to);
2837#endif
2838 exec->state = exec->comp->states[trans->to];
2839 exec->transno = 0;
2840 if (trans->atom != NULL) {
2841 if (exec->inputStack != NULL) {
2842 exec->index++;
2843 if (exec->index < exec->inputStackNr) {
2844 value = exec->inputStack[exec->index].value;
2845 data = exec->inputStack[exec->index].data;
2846#ifdef DEBUG_PUSH
2847 printf("value loaded: %s\n", value);
2848#endif
2849 } else {
2850 value = NULL;
2851 data = NULL;
2852#ifdef DEBUG_PUSH
2853 printf("end of input\n");
2854#endif
2855 }
2856 } else {
2857 value = NULL;
2858 data = NULL;
2859#ifdef DEBUG_PUSH
2860 printf("end of input\n");
2861#endif
2862 }
2863 }
2864 goto progress;
2865 } else if (ret < 0) {
2866 exec->status = -4;
2867 break;
2868 }
2869 }
2870 if ((exec->transno != 0) || (exec->state->nbTrans == 0)) {
2871rollback:
2872 /*
2873 * Failed to find a way out
2874 */
2875 exec->determinist = 0;
2876 xmlFARegExecRollBack(exec);
2877 if (exec->status == 0) {
2878 value = exec->inputStack[exec->index].value;
2879 data = exec->inputStack[exec->index].data;
2880#ifdef DEBUG_PUSH
2881 printf("value loaded: %s\n", value);
2882#endif
2883 }
2884 }
2885progress:
2886 continue;
2887 }
2888 if (exec->status == 0) {
2889 return(exec->state->type == XML_REGEXP_FINAL_STATE);
2890 }
2891 return(exec->status);
2892}
2893
Daniel Veillard52b48c72003-04-13 19:53:42 +00002894/**
2895 * xmlRegExecPushString2:
2896 * @exec: a regexp execution context or NULL to indicate the end
2897 * @value: the first string token input
2898 * @value2: the second string token input
2899 * @data: data associated to the token to reuse in callbacks
2900 *
2901 * Push one input token in the execution context
2902 *
2903 * Returns: 1 if the regexp reached a final state, 0 if non-final, and
2904 * a negative value in case of error.
2905 */
2906int
2907xmlRegExecPushString2(xmlRegExecCtxtPtr exec, const xmlChar *value,
2908 const xmlChar *value2, void *data) {
2909 xmlChar buf[150];
2910 int lenn, lenp, ret;
2911 xmlChar *str;
2912
2913 if (exec == NULL)
2914 return(-1);
2915 if (exec->comp == NULL)
2916 return(-1);
2917 if (exec->status != 0)
2918 return(exec->status);
2919
2920 if (value2 == NULL)
2921 return(xmlRegExecPushString(exec, value, data));
2922
2923 lenn = strlen((char *) value2);
2924 lenp = strlen((char *) value);
2925
2926 if (150 < lenn + lenp + 2) {
Daniel Veillard3c908dc2003-04-19 00:07:51 +00002927 str = (xmlChar *) xmlMallocAtomic(lenn + lenp + 2);
Daniel Veillard52b48c72003-04-13 19:53:42 +00002928 if (str == NULL) {
2929 exec->status = -1;
2930 return(-1);
2931 }
2932 } else {
2933 str = buf;
2934 }
2935 memcpy(&str[0], value, lenp);
2936 str[lenp] = '|';
2937 memcpy(&str[lenp + 1], value2, lenn);
2938 str[lenn + lenp + 1] = 0;
2939
2940 if (exec->comp->compact != NULL)
2941 ret = xmlRegCompactPushString(exec, exec->comp, str, data);
2942 else
2943 ret = xmlRegExecPushString(exec, str, data);
2944
2945 if (str != buf)
2946 xmlFree(buf);
2947 return(ret);
2948}
2949
Daniel Veillard4255d502002-04-16 15:50:10 +00002950#if 0
2951static int
2952xmlRegExecPushChar(xmlRegExecCtxtPtr exec, int UCS) {
2953 xmlRegTransPtr trans;
2954 xmlRegAtomPtr atom;
2955 int ret;
2956 int codepoint, len;
2957
2958 if (exec == NULL)
2959 return(-1);
2960 if (exec->status != 0)
2961 return(exec->status);
2962
2963 while ((exec->status == 0) &&
2964 ((exec->inputString[exec->index] != 0) ||
2965 (exec->state->type != XML_REGEXP_FINAL_STATE))) {
2966
2967 /*
2968 * End of input on non-terminal state, rollback, however we may
2969 * still have epsilon like transition for counted transitions
2970 * on counters, in that case don't break too early.
2971 */
2972 if ((exec->inputString[exec->index] == 0) && (exec->counts == NULL))
2973 goto rollback;
2974
2975 exec->transcount = 0;
2976 for (;exec->transno < exec->state->nbTrans;exec->transno++) {
2977 trans = &exec->state->trans[exec->transno];
2978 if (trans->to < 0)
2979 continue;
2980 atom = trans->atom;
2981 ret = 0;
2982 if (trans->count >= 0) {
2983 int count;
2984 xmlRegCounterPtr counter;
2985
2986 /*
2987 * A counted transition.
2988 */
2989
2990 count = exec->counts[trans->count];
2991 counter = &exec->comp->counters[trans->count];
2992#ifdef DEBUG_REGEXP_EXEC
2993 printf("testing count %d: val %d, min %d, max %d\n",
2994 trans->count, count, counter->min, counter->max);
2995#endif
2996 ret = ((count >= counter->min) && (count <= counter->max));
2997 } else if (atom == NULL) {
2998 fprintf(stderr, "epsilon transition left at runtime\n");
2999 exec->status = -2;
3000 break;
3001 } else if (exec->inputString[exec->index] != 0) {
3002 codepoint = CUR_SCHAR(&(exec->inputString[exec->index]), len);
3003 ret = xmlRegCheckCharacter(atom, codepoint);
3004 if ((ret == 1) && (atom->min > 0) && (atom->max > 0)) {
3005 xmlRegStatePtr to = exec->comp->states[trans->to];
3006
3007 /*
3008 * this is a multiple input sequence
3009 */
3010 if (exec->state->nbTrans > exec->transno + 1) {
3011 xmlFARegExecSave(exec);
3012 }
3013 exec->transcount = 1;
3014 do {
3015 /*
3016 * Try to progress as much as possible on the input
3017 */
3018 if (exec->transcount == atom->max) {
3019 break;
3020 }
3021 exec->index += len;
3022 /*
3023 * End of input: stop here
3024 */
3025 if (exec->inputString[exec->index] == 0) {
3026 exec->index -= len;
3027 break;
3028 }
3029 if (exec->transcount >= atom->min) {
3030 int transno = exec->transno;
3031 xmlRegStatePtr state = exec->state;
3032
3033 /*
3034 * The transition is acceptable save it
3035 */
3036 exec->transno = -1; /* trick */
3037 exec->state = to;
3038 xmlFARegExecSave(exec);
3039 exec->transno = transno;
3040 exec->state = state;
3041 }
3042 codepoint = CUR_SCHAR(&(exec->inputString[exec->index]),
3043 len);
3044 ret = xmlRegCheckCharacter(atom, codepoint);
3045 exec->transcount++;
3046 } while (ret == 1);
3047 if (exec->transcount < atom->min)
3048 ret = 0;
3049
3050 /*
3051 * If the last check failed but one transition was found
3052 * possible, rollback
3053 */
3054 if (ret < 0)
3055 ret = 0;
3056 if (ret == 0) {
3057 goto rollback;
3058 }
3059 }
3060 }
3061 if (ret == 1) {
3062 if (exec->state->nbTrans > exec->transno + 1) {
3063 xmlFARegExecSave(exec);
3064 }
3065 if (trans->counter >= 0) {
3066#ifdef DEBUG_REGEXP_EXEC
3067 printf("Increasing count %d\n", trans->counter);
3068#endif
3069 exec->counts[trans->counter]++;
3070 }
3071#ifdef DEBUG_REGEXP_EXEC
3072 printf("entering state %d\n", trans->to);
3073#endif
3074 exec->state = exec->comp->states[trans->to];
3075 exec->transno = 0;
3076 if (trans->atom != NULL) {
3077 exec->index += len;
3078 }
3079 goto progress;
3080 } else if (ret < 0) {
3081 exec->status = -4;
3082 break;
3083 }
3084 }
3085 if ((exec->transno != 0) || (exec->state->nbTrans == 0)) {
3086rollback:
3087 /*
3088 * Failed to find a way out
3089 */
3090 exec->determinist = 0;
3091 xmlFARegExecRollBack(exec);
3092 }
3093progress:
3094 continue;
3095 }
3096}
3097#endif
3098/************************************************************************
3099 * *
3100 * Parser for the Shemas Datatype Regular Expressions *
3101 * http://www.w3.org/TR/2001/REC-xmlschema-2-20010502/#regexs *
3102 * *
3103 ************************************************************************/
3104
3105/**
3106 * xmlFAIsChar:
Daniel Veillard441bc322002-04-20 17:38:48 +00003107 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00003108 *
3109 * [10] Char ::= [^.\?*+()|#x5B#x5D]
3110 */
3111static int
3112xmlFAIsChar(xmlRegParserCtxtPtr ctxt) {
3113 int cur;
3114 int len;
3115
3116 cur = CUR_SCHAR(ctxt->cur, len);
3117 if ((cur == '.') || (cur == '\\') || (cur == '?') ||
3118 (cur == '*') || (cur == '+') || (cur == '(') ||
3119 (cur == ')') || (cur == '|') || (cur == 0x5B) ||
3120 (cur == 0x5D) || (cur == 0))
3121 return(-1);
3122 return(cur);
3123}
3124
3125/**
3126 * xmlFAParseCharProp:
Daniel Veillard441bc322002-04-20 17:38:48 +00003127 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00003128 *
3129 * [27] charProp ::= IsCategory | IsBlock
3130 * [28] IsCategory ::= Letters | Marks | Numbers | Punctuation |
3131 * Separators | Symbols | Others
3132 * [29] Letters ::= 'L' [ultmo]?
3133 * [30] Marks ::= 'M' [nce]?
3134 * [31] Numbers ::= 'N' [dlo]?
3135 * [32] Punctuation ::= 'P' [cdseifo]?
3136 * [33] Separators ::= 'Z' [slp]?
3137 * [34] Symbols ::= 'S' [mcko]?
3138 * [35] Others ::= 'C' [cfon]?
3139 * [36] IsBlock ::= 'Is' [a-zA-Z0-9#x2D]+
3140 */
3141static void
3142xmlFAParseCharProp(xmlRegParserCtxtPtr ctxt) {
3143 int cur;
William M. Brack779af002003-08-01 15:55:39 +00003144 xmlRegAtomType type = (xmlRegAtomType) 0;
Daniel Veillard4255d502002-04-16 15:50:10 +00003145 xmlChar *blockName = NULL;
3146
3147 cur = CUR;
3148 if (cur == 'L') {
3149 NEXT;
3150 cur = CUR;
3151 if (cur == 'u') {
3152 NEXT;
3153 type = XML_REGEXP_LETTER_UPPERCASE;
3154 } else if (cur == 'l') {
3155 NEXT;
3156 type = XML_REGEXP_LETTER_LOWERCASE;
3157 } else if (cur == 't') {
3158 NEXT;
3159 type = XML_REGEXP_LETTER_TITLECASE;
3160 } else if (cur == 'm') {
3161 NEXT;
3162 type = XML_REGEXP_LETTER_MODIFIER;
3163 } else if (cur == 'o') {
3164 NEXT;
3165 type = XML_REGEXP_LETTER_OTHERS;
3166 } else {
3167 type = XML_REGEXP_LETTER;
3168 }
3169 } else if (cur == 'M') {
3170 NEXT;
3171 cur = CUR;
3172 if (cur == 'n') {
3173 NEXT;
3174 /* nonspacing */
3175 type = XML_REGEXP_MARK_NONSPACING;
3176 } else if (cur == 'c') {
3177 NEXT;
3178 /* spacing combining */
3179 type = XML_REGEXP_MARK_SPACECOMBINING;
3180 } else if (cur == 'e') {
3181 NEXT;
3182 /* enclosing */
3183 type = XML_REGEXP_MARK_ENCLOSING;
3184 } else {
3185 /* all marks */
3186 type = XML_REGEXP_MARK;
3187 }
3188 } else if (cur == 'N') {
3189 NEXT;
3190 cur = CUR;
3191 if (cur == 'd') {
3192 NEXT;
3193 /* digital */
3194 type = XML_REGEXP_NUMBER_DECIMAL;
3195 } else if (cur == 'l') {
3196 NEXT;
3197 /* letter */
3198 type = XML_REGEXP_NUMBER_LETTER;
3199 } else if (cur == 'o') {
3200 NEXT;
3201 /* other */
3202 type = XML_REGEXP_NUMBER_OTHERS;
3203 } else {
3204 /* all numbers */
3205 type = XML_REGEXP_NUMBER;
3206 }
3207 } else if (cur == 'P') {
3208 NEXT;
3209 cur = CUR;
3210 if (cur == 'c') {
3211 NEXT;
3212 /* connector */
3213 type = XML_REGEXP_PUNCT_CONNECTOR;
3214 } else if (cur == 'd') {
3215 NEXT;
3216 /* dash */
3217 type = XML_REGEXP_PUNCT_DASH;
3218 } else if (cur == 's') {
3219 NEXT;
3220 /* open */
3221 type = XML_REGEXP_PUNCT_OPEN;
3222 } else if (cur == 'e') {
3223 NEXT;
3224 /* close */
3225 type = XML_REGEXP_PUNCT_CLOSE;
3226 } else if (cur == 'i') {
3227 NEXT;
3228 /* initial quote */
3229 type = XML_REGEXP_PUNCT_INITQUOTE;
3230 } else if (cur == 'f') {
3231 NEXT;
3232 /* final quote */
3233 type = XML_REGEXP_PUNCT_FINQUOTE;
3234 } else if (cur == 'o') {
3235 NEXT;
3236 /* other */
3237 type = XML_REGEXP_PUNCT_OTHERS;
3238 } else {
3239 /* all punctuation */
3240 type = XML_REGEXP_PUNCT;
3241 }
3242 } else if (cur == 'Z') {
3243 NEXT;
3244 cur = CUR;
3245 if (cur == 's') {
3246 NEXT;
3247 /* space */
3248 type = XML_REGEXP_SEPAR_SPACE;
3249 } else if (cur == 'l') {
3250 NEXT;
3251 /* line */
3252 type = XML_REGEXP_SEPAR_LINE;
3253 } else if (cur == 'p') {
3254 NEXT;
3255 /* paragraph */
3256 type = XML_REGEXP_SEPAR_PARA;
3257 } else {
3258 /* all separators */
3259 type = XML_REGEXP_SEPAR;
3260 }
3261 } else if (cur == 'S') {
3262 NEXT;
3263 cur = CUR;
3264 if (cur == 'm') {
3265 NEXT;
3266 type = XML_REGEXP_SYMBOL_MATH;
3267 /* math */
3268 } else if (cur == 'c') {
3269 NEXT;
3270 type = XML_REGEXP_SYMBOL_CURRENCY;
3271 /* currency */
3272 } else if (cur == 'k') {
3273 NEXT;
3274 type = XML_REGEXP_SYMBOL_MODIFIER;
3275 /* modifiers */
3276 } else if (cur == 'o') {
3277 NEXT;
3278 type = XML_REGEXP_SYMBOL_OTHERS;
3279 /* other */
3280 } else {
3281 /* all symbols */
3282 type = XML_REGEXP_SYMBOL;
3283 }
3284 } else if (cur == 'C') {
3285 NEXT;
3286 cur = CUR;
3287 if (cur == 'c') {
3288 NEXT;
3289 /* control */
3290 type = XML_REGEXP_OTHER_CONTROL;
3291 } else if (cur == 'f') {
3292 NEXT;
3293 /* format */
3294 type = XML_REGEXP_OTHER_FORMAT;
3295 } else if (cur == 'o') {
3296 NEXT;
3297 /* private use */
3298 type = XML_REGEXP_OTHER_PRIVATE;
3299 } else if (cur == 'n') {
3300 NEXT;
3301 /* not assigned */
3302 type = XML_REGEXP_OTHER_NA;
3303 } else {
3304 /* all others */
3305 type = XML_REGEXP_OTHER;
3306 }
3307 } else if (cur == 'I') {
3308 const xmlChar *start;
3309 NEXT;
3310 cur = CUR;
3311 if (cur != 's') {
3312 ERROR("IsXXXX expected");
3313 return;
3314 }
3315 NEXT;
3316 start = ctxt->cur;
3317 cur = CUR;
3318 if (((cur >= 'a') && (cur <= 'z')) ||
3319 ((cur >= 'A') && (cur <= 'Z')) ||
3320 ((cur >= '0') && (cur <= '9')) ||
3321 (cur == 0x2D)) {
3322 NEXT;
3323 cur = CUR;
3324 while (((cur >= 'a') && (cur <= 'z')) ||
3325 ((cur >= 'A') && (cur <= 'Z')) ||
3326 ((cur >= '0') && (cur <= '9')) ||
3327 (cur == 0x2D)) {
3328 NEXT;
3329 cur = CUR;
3330 }
3331 }
3332 type = XML_REGEXP_BLOCK_NAME;
3333 blockName = xmlStrndup(start, ctxt->cur - start);
3334 } else {
3335 ERROR("Unknown char property");
3336 return;
3337 }
3338 if (ctxt->atom == NULL) {
3339 ctxt->atom = xmlRegNewAtom(ctxt, type);
3340 if (ctxt->atom != NULL)
3341 ctxt->atom->valuep = blockName;
3342 } else if (ctxt->atom->type == XML_REGEXP_RANGES) {
3343 xmlRegAtomAddRange(ctxt, ctxt->atom, ctxt->neg,
3344 type, 0, 0, blockName);
3345 }
3346}
3347
3348/**
3349 * xmlFAParseCharClassEsc:
Daniel Veillard441bc322002-04-20 17:38:48 +00003350 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00003351 *
3352 * [23] charClassEsc ::= ( SingleCharEsc | MultiCharEsc | catEsc | complEsc )
3353 * [24] SingleCharEsc ::= '\' [nrt\|.?*+(){}#x2D#x5B#x5D#x5E]
3354 * [25] catEsc ::= '\p{' charProp '}'
3355 * [26] complEsc ::= '\P{' charProp '}'
3356 * [37] MultiCharEsc ::= '.' | ('\' [sSiIcCdDwW])
3357 */
3358static void
3359xmlFAParseCharClassEsc(xmlRegParserCtxtPtr ctxt) {
3360 int cur;
3361
3362 if (CUR == '.') {
3363 if (ctxt->atom == NULL) {
3364 ctxt->atom = xmlRegNewAtom(ctxt, XML_REGEXP_ANYCHAR);
3365 } else if (ctxt->atom->type == XML_REGEXP_RANGES) {
3366 xmlRegAtomAddRange(ctxt, ctxt->atom, ctxt->neg,
3367 XML_REGEXP_ANYCHAR, 0, 0, NULL);
3368 }
3369 NEXT;
3370 return;
3371 }
3372 if (CUR != '\\') {
3373 ERROR("Escaped sequence: expecting \\");
3374 return;
3375 }
3376 NEXT;
3377 cur = CUR;
3378 if (cur == 'p') {
3379 NEXT;
3380 if (CUR != '{') {
3381 ERROR("Expecting '{'");
3382 return;
3383 }
3384 NEXT;
3385 xmlFAParseCharProp(ctxt);
3386 if (CUR != '}') {
3387 ERROR("Expecting '}'");
3388 return;
3389 }
3390 NEXT;
3391 } else if (cur == 'P') {
3392 NEXT;
3393 if (CUR != '{') {
3394 ERROR("Expecting '{'");
3395 return;
3396 }
3397 NEXT;
3398 xmlFAParseCharProp(ctxt);
3399 ctxt->atom->neg = 1;
3400 if (CUR != '}') {
3401 ERROR("Expecting '}'");
3402 return;
3403 }
3404 NEXT;
3405 } else if ((cur == 'n') || (cur == 'r') || (cur == 't') || (cur == '\\') ||
3406 (cur == '|') || (cur == '.') || (cur == '?') || (cur == '*') ||
3407 (cur == '+') || (cur == '(') || (cur == ')') || (cur == '{') ||
3408 (cur == '}') || (cur == 0x2D) || (cur == 0x5B) || (cur == 0x5D) ||
3409 (cur == 0x5E)) {
3410 if (ctxt->atom == NULL) {
3411 ctxt->atom = xmlRegNewAtom(ctxt, XML_REGEXP_CHARVAL);
3412 if (ctxt->atom != NULL)
3413 ctxt->atom->codepoint = cur;
3414 } else if (ctxt->atom->type == XML_REGEXP_RANGES) {
3415 xmlRegAtomAddRange(ctxt, ctxt->atom, ctxt->neg,
3416 XML_REGEXP_CHARVAL, cur, cur, NULL);
3417 }
3418 NEXT;
3419 } else if ((cur == 's') || (cur == 'S') || (cur == 'i') || (cur == 'I') ||
3420 (cur == 'c') || (cur == 'C') || (cur == 'd') || (cur == 'D') ||
3421 (cur == 'w') || (cur == 'W')) {
Daniel Veillardb509f152002-04-17 16:28:10 +00003422 xmlRegAtomType type = XML_REGEXP_ANYSPACE;
Daniel Veillard4255d502002-04-16 15:50:10 +00003423
3424 switch (cur) {
3425 case 's':
3426 type = XML_REGEXP_ANYSPACE;
3427 break;
3428 case 'S':
3429 type = XML_REGEXP_NOTSPACE;
3430 break;
3431 case 'i':
3432 type = XML_REGEXP_INITNAME;
3433 break;
3434 case 'I':
3435 type = XML_REGEXP_NOTINITNAME;
3436 break;
3437 case 'c':
3438 type = XML_REGEXP_NAMECHAR;
3439 break;
3440 case 'C':
3441 type = XML_REGEXP_NOTNAMECHAR;
3442 break;
3443 case 'd':
3444 type = XML_REGEXP_DECIMAL;
3445 break;
3446 case 'D':
3447 type = XML_REGEXP_NOTDECIMAL;
3448 break;
3449 case 'w':
3450 type = XML_REGEXP_REALCHAR;
3451 break;
3452 case 'W':
3453 type = XML_REGEXP_NOTREALCHAR;
3454 break;
3455 }
3456 NEXT;
3457 if (ctxt->atom == NULL) {
3458 ctxt->atom = xmlRegNewAtom(ctxt, type);
3459 } else if (ctxt->atom->type == XML_REGEXP_RANGES) {
3460 xmlRegAtomAddRange(ctxt, ctxt->atom, ctxt->neg,
3461 type, 0, 0, NULL);
3462 }
3463 }
3464}
3465
3466/**
3467 * xmlFAParseCharRef:
Daniel Veillard441bc322002-04-20 17:38:48 +00003468 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00003469 *
3470 * [19] XmlCharRef ::= ( '&#' [0-9]+ ';' ) | (' &#x' [0-9a-fA-F]+ ';' )
3471 */
3472static int
3473xmlFAParseCharRef(xmlRegParserCtxtPtr ctxt) {
3474 int ret = 0, cur;
3475
3476 if ((CUR != '&') || (NXT(1) != '#'))
3477 return(-1);
3478 NEXT;
3479 NEXT;
3480 cur = CUR;
3481 if (cur == 'x') {
3482 NEXT;
3483 cur = CUR;
3484 if (((cur >= '0') && (cur <= '9')) ||
3485 ((cur >= 'a') && (cur <= 'f')) ||
3486 ((cur >= 'A') && (cur <= 'F'))) {
3487 while (((cur >= '0') && (cur <= '9')) ||
3488 ((cur >= 'A') && (cur <= 'F'))) {
3489 if ((cur >= '0') && (cur <= '9'))
3490 ret = ret * 16 + cur - '0';
3491 else if ((cur >= 'a') && (cur <= 'f'))
3492 ret = ret * 16 + 10 + (cur - 'a');
3493 else
3494 ret = ret * 16 + 10 + (cur - 'A');
3495 NEXT;
3496 cur = CUR;
3497 }
3498 } else {
3499 ERROR("Char ref: expecting [0-9A-F]");
3500 return(-1);
3501 }
3502 } else {
3503 if ((cur >= '0') && (cur <= '9')) {
3504 while ((cur >= '0') && (cur <= '9')) {
3505 ret = ret * 10 + cur - '0';
3506 NEXT;
3507 cur = CUR;
3508 }
3509 } else {
3510 ERROR("Char ref: expecting [0-9]");
3511 return(-1);
3512 }
3513 }
3514 if (cur != ';') {
3515 ERROR("Char ref: expecting ';'");
3516 return(-1);
3517 } else {
3518 NEXT;
3519 }
3520 return(ret);
3521}
3522
3523/**
3524 * xmlFAParseCharRange:
Daniel Veillard441bc322002-04-20 17:38:48 +00003525 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00003526 *
3527 * [17] charRange ::= seRange | XmlCharRef | XmlCharIncDash
3528 * [18] seRange ::= charOrEsc '-' charOrEsc
3529 * [20] charOrEsc ::= XmlChar | SingleCharEsc
3530 * [21] XmlChar ::= [^\#x2D#x5B#x5D]
3531 * [22] XmlCharIncDash ::= [^\#x5B#x5D]
3532 */
3533static void
3534xmlFAParseCharRange(xmlRegParserCtxtPtr ctxt) {
3535 int cur;
3536 int start = -1;
3537 int end = -1;
3538
3539 if ((CUR == '&') && (NXT(1) == '#')) {
3540 end = start = xmlFAParseCharRef(ctxt);
3541 xmlRegAtomAddRange(ctxt, ctxt->atom, ctxt->neg,
3542 XML_REGEXP_CHARVAL, start, end, NULL);
3543 return;
3544 }
3545 cur = CUR;
3546 if (cur == '\\') {
3547 NEXT;
3548 cur = CUR;
3549 switch (cur) {
3550 case 'n': start = 0xA; break;
3551 case 'r': start = 0xD; break;
3552 case 't': start = 0x9; break;
3553 case '\\': case '|': case '.': case '-': case '^': case '?':
3554 case '*': case '+': case '{': case '}': case '(': case ')':
3555 case '[': case ']':
3556 start = cur; break;
3557 default:
3558 ERROR("Invalid escape value");
3559 return;
3560 }
3561 end = start;
3562 } else if ((cur != 0x5B) && (cur != 0x5D)) {
3563 end = start = cur;
3564 } else {
3565 ERROR("Expecting a char range");
3566 return;
3567 }
3568 NEXT;
3569 if (start == '-') {
3570 return;
3571 }
3572 cur = CUR;
3573 if (cur != '-') {
3574 xmlRegAtomAddRange(ctxt, ctxt->atom, ctxt->neg,
3575 XML_REGEXP_CHARVAL, start, end, NULL);
3576 return;
3577 }
3578 NEXT;
3579 cur = CUR;
3580 if (cur == '\\') {
3581 NEXT;
3582 cur = CUR;
3583 switch (cur) {
3584 case 'n': end = 0xA; break;
3585 case 'r': end = 0xD; break;
3586 case 't': end = 0x9; break;
3587 case '\\': case '|': case '.': case '-': case '^': case '?':
3588 case '*': case '+': case '{': case '}': case '(': case ')':
3589 case '[': case ']':
3590 end = cur; break;
3591 default:
3592 ERROR("Invalid escape value");
3593 return;
3594 }
3595 } else if ((cur != 0x5B) && (cur != 0x5D)) {
3596 end = cur;
3597 } else {
3598 ERROR("Expecting the end of a char range");
3599 return;
3600 }
3601 NEXT;
3602 /* TODO check that the values are acceptable character ranges for XML */
3603 if (end < start) {
3604 ERROR("End of range is before start of range");
3605 } else {
3606 xmlRegAtomAddRange(ctxt, ctxt->atom, ctxt->neg,
3607 XML_REGEXP_CHARVAL, start, end, NULL);
3608 }
3609 return;
3610}
3611
3612/**
3613 * xmlFAParsePosCharGroup:
Daniel Veillard441bc322002-04-20 17:38:48 +00003614 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00003615 *
3616 * [14] posCharGroup ::= ( charRange | charClassEsc )+
3617 */
3618static void
3619xmlFAParsePosCharGroup(xmlRegParserCtxtPtr ctxt) {
3620 do {
3621 if ((CUR == '\\') || (CUR == '.')) {
3622 xmlFAParseCharClassEsc(ctxt);
3623 } else {
3624 xmlFAParseCharRange(ctxt);
3625 }
3626 } while ((CUR != ']') && (CUR != '^') && (CUR != '-') &&
3627 (ctxt->error == 0));
3628}
3629
3630/**
3631 * xmlFAParseCharGroup:
Daniel Veillard441bc322002-04-20 17:38:48 +00003632 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00003633 *
3634 * [13] charGroup ::= posCharGroup | negCharGroup | charClassSub
3635 * [15] negCharGroup ::= '^' posCharGroup
3636 * [16] charClassSub ::= ( posCharGroup | negCharGroup ) '-' charClassExpr
3637 * [12] charClassExpr ::= '[' charGroup ']'
3638 */
3639static void
3640xmlFAParseCharGroup(xmlRegParserCtxtPtr ctxt) {
3641 int n = ctxt->neg;
3642 while ((CUR != ']') && (ctxt->error == 0)) {
3643 if (CUR == '^') {
3644 int neg = ctxt->neg;
3645
3646 NEXT;
3647 ctxt->neg = !ctxt->neg;
3648 xmlFAParsePosCharGroup(ctxt);
3649 ctxt->neg = neg;
3650 } else if (CUR == '-') {
Daniel Veillardf8b9de32003-11-24 14:27:26 +00003651 int neg = ctxt->neg;
Daniel Veillard4255d502002-04-16 15:50:10 +00003652 NEXT;
Daniel Veillardf8b9de32003-11-24 14:27:26 +00003653 ctxt->neg = 2;
Daniel Veillard4255d502002-04-16 15:50:10 +00003654 if (CUR != '[') {
3655 ERROR("charClassExpr: '[' expected");
3656 break;
3657 }
3658 NEXT;
3659 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
3866 * @first: is taht the first
Daniel Veillard4255d502002-04-16 15:50:10 +00003867 *
3868 * [2] branch ::= piece*
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00003869 8
Daniel Veillard4255d502002-04-16 15:50:10 +00003870 */
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00003871static int
Daniel Veillard4255d502002-04-16 15:50:10 +00003872xmlFAParseBranch(xmlRegParserCtxtPtr ctxt, int first) {
3873 xmlRegStatePtr previous;
3874 xmlRegAtomPtr prevatom = NULL;
3875 int ret;
3876
3877 previous = ctxt->state;
3878 ret = xmlFAParsePiece(ctxt);
3879 if (ret != 0) {
3880 if (first) {
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00003881 if (xmlFAGenerateTransitions(ctxt, previous, NULL, ctxt->atom) < 0)
3882 return(-1);
Daniel Veillard4255d502002-04-16 15:50:10 +00003883 previous = ctxt->state;
3884 } else {
3885 prevatom = ctxt->atom;
3886 }
3887 ctxt->atom = NULL;
3888 }
3889 while ((ret != 0) && (ctxt->error == 0)) {
3890 ret = xmlFAParsePiece(ctxt);
3891 if (ret != 0) {
3892 if (first) {
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00003893 if (xmlFAGenerateTransitions(ctxt, previous, NULL,
3894 ctxt->atom) < 0)
3895 return(-1);
Daniel Veillard4255d502002-04-16 15:50:10 +00003896 } else {
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00003897 if (xmlFAGenerateTransitions(ctxt, previous, NULL,
3898 prevatom) < 0)
3899 return(-1);
Daniel Veillard4255d502002-04-16 15:50:10 +00003900 prevatom = ctxt->atom;
3901 }
3902 previous = ctxt->state;
3903 ctxt->atom = NULL;
3904 }
3905 }
3906 if (!first) {
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00003907 if (xmlFAGenerateTransitions(ctxt, previous, ctxt->end, prevatom) < 0)
3908 return(-1);
Daniel Veillard4255d502002-04-16 15:50:10 +00003909 }
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00003910 return(0);
Daniel Veillard4255d502002-04-16 15:50:10 +00003911}
3912
3913/**
3914 * xmlFAParseRegExp:
Daniel Veillard441bc322002-04-20 17:38:48 +00003915 * @ctxt: a regexp parser context
3916 * @top: is that the top-level expressions ?
Daniel Veillard4255d502002-04-16 15:50:10 +00003917 *
3918 * [1] regExp ::= branch ( '|' branch )*
3919 */
3920static void
3921xmlFAParseRegExp(xmlRegParserCtxtPtr ctxt, int top) {
3922 xmlRegStatePtr start, end, oldend;
3923
3924 oldend = ctxt->end;
3925
3926 start = ctxt->state;
3927 xmlFAParseBranch(ctxt, (ctxt->end == NULL));
3928 if (CUR != '|') {
3929 ctxt->end = ctxt->state;
3930 return;
3931 }
3932 end = ctxt->state;
3933 while ((CUR == '|') && (ctxt->error == 0)) {
3934 NEXT;
3935 ctxt->state = start;
3936 ctxt->end = end;
3937 xmlFAParseBranch(ctxt, 0);
3938 }
3939 if (!top)
3940 ctxt->end = oldend;
3941}
3942
3943/************************************************************************
3944 * *
3945 * The basic API *
3946 * *
3947 ************************************************************************/
3948
3949/**
3950 * xmlRegexpPrint:
3951 * @output: the file for the output debug
3952 * @regexp: the compiled regexp
3953 *
3954 * Print the content of the compiled regular expression
3955 */
3956void
3957xmlRegexpPrint(FILE *output, xmlRegexpPtr regexp) {
3958 int i;
3959
3960 fprintf(output, " regexp: ");
3961 if (regexp == NULL) {
3962 fprintf(output, "NULL\n");
3963 return;
3964 }
3965 fprintf(output, "'%s' ", regexp->string);
3966 fprintf(output, "\n");
3967 fprintf(output, "%d atoms:\n", regexp->nbAtoms);
3968 for (i = 0;i < regexp->nbAtoms; i++) {
3969 fprintf(output, " %02d ", i);
3970 xmlRegPrintAtom(output, regexp->atoms[i]);
3971 }
3972 fprintf(output, "%d states:", regexp->nbStates);
3973 fprintf(output, "\n");
3974 for (i = 0;i < regexp->nbStates; i++) {
3975 xmlRegPrintState(output, regexp->states[i]);
3976 }
3977 fprintf(output, "%d counters:\n", regexp->nbCounters);
3978 for (i = 0;i < regexp->nbCounters; i++) {
3979 fprintf(output, " %d: min %d max %d\n", i, regexp->counters[i].min,
3980 regexp->counters[i].max);
3981 }
3982}
3983
3984/**
3985 * xmlRegexpCompile:
3986 * @regexp: a regular expression string
3987 *
3988 * Parses a regular expression conforming to XML Schemas Part 2 Datatype
3989 * Appendix F and build an automata suitable for testing strings against
3990 * that regular expression
3991 *
3992 * Returns the compiled expression or NULL in case of error
3993 */
3994xmlRegexpPtr
3995xmlRegexpCompile(const xmlChar *regexp) {
3996 xmlRegexpPtr ret;
3997 xmlRegParserCtxtPtr ctxt;
3998
3999 ctxt = xmlRegNewParserCtxt(regexp);
4000 if (ctxt == NULL)
4001 return(NULL);
4002
4003 /* initialize the parser */
4004 ctxt->end = NULL;
4005 ctxt->start = ctxt->state = xmlRegNewState(ctxt);
4006 xmlRegStatePush(ctxt, ctxt->start);
4007
4008 /* parse the expression building an automata */
4009 xmlFAParseRegExp(ctxt, 1);
4010 if (CUR != 0) {
4011 ERROR("xmlFAParseRegExp: extra characters");
4012 }
4013 ctxt->end = ctxt->state;
4014 ctxt->start->type = XML_REGEXP_START_STATE;
4015 ctxt->end->type = XML_REGEXP_FINAL_STATE;
4016
4017 /* remove the Epsilon except for counted transitions */
4018 xmlFAEliminateEpsilonTransitions(ctxt);
4019
4020
4021 if (ctxt->error != 0) {
4022 xmlRegFreeParserCtxt(ctxt);
4023 return(NULL);
4024 }
4025 ret = xmlRegEpxFromParse(ctxt);
4026 xmlRegFreeParserCtxt(ctxt);
4027 return(ret);
4028}
4029
4030/**
4031 * xmlRegexpExec:
4032 * @comp: the compiled regular expression
4033 * @content: the value to check against the regular expression
4034 *
4035 * Check if the regular expression generate the value
4036 *
4037 * Returns 1 if it matches, 0 if not and a negativa value in case of error
4038 */
4039int
4040xmlRegexpExec(xmlRegexpPtr comp, const xmlChar *content) {
4041 if ((comp == NULL) || (content == NULL))
4042 return(-1);
4043 return(xmlFARegExec(comp, content));
4044}
4045
4046/**
Daniel Veillard23e73572002-09-19 19:56:43 +00004047 * xmlRegexpIsDeterminist:
4048 * @comp: the compiled regular expression
4049 *
4050 * Check if the regular expression is determinist
4051 *
4052 * Returns 1 if it yes, 0 if not and a negativa value in case of error
4053 */
4054int
4055xmlRegexpIsDeterminist(xmlRegexpPtr comp) {
4056 xmlAutomataPtr am;
4057 int ret;
4058
4059 if (comp == NULL)
4060 return(-1);
4061 if (comp->determinist != -1)
4062 return(comp->determinist);
4063
4064 am = xmlNewAutomata();
Daniel Veillardbd9afb52002-09-25 22:25:35 +00004065 if (am->states != NULL) {
4066 int i;
4067
4068 for (i = 0;i < am->nbStates;i++)
4069 xmlRegFreeState(am->states[i]);
4070 xmlFree(am->states);
4071 }
Daniel Veillard23e73572002-09-19 19:56:43 +00004072 am->nbAtoms = comp->nbAtoms;
4073 am->atoms = comp->atoms;
4074 am->nbStates = comp->nbStates;
4075 am->states = comp->states;
4076 am->determinist = -1;
4077 ret = xmlFAComputesDeterminism(am);
4078 am->atoms = NULL;
4079 am->states = NULL;
4080 xmlFreeAutomata(am);
4081 return(ret);
4082}
4083
4084/**
Daniel Veillard4255d502002-04-16 15:50:10 +00004085 * xmlRegFreeRegexp:
4086 * @regexp: the regexp
4087 *
4088 * Free a regexp
4089 */
4090void
4091xmlRegFreeRegexp(xmlRegexpPtr regexp) {
4092 int i;
4093 if (regexp == NULL)
4094 return;
4095
4096 if (regexp->string != NULL)
4097 xmlFree(regexp->string);
4098 if (regexp->states != NULL) {
4099 for (i = 0;i < regexp->nbStates;i++)
4100 xmlRegFreeState(regexp->states[i]);
4101 xmlFree(regexp->states);
4102 }
4103 if (regexp->atoms != NULL) {
4104 for (i = 0;i < regexp->nbAtoms;i++)
4105 xmlRegFreeAtom(regexp->atoms[i]);
4106 xmlFree(regexp->atoms);
4107 }
4108 if (regexp->counters != NULL)
4109 xmlFree(regexp->counters);
Daniel Veillard23e73572002-09-19 19:56:43 +00004110 if (regexp->compact != NULL)
4111 xmlFree(regexp->compact);
Daniel Veillard118aed72002-09-24 14:13:13 +00004112 if (regexp->transdata != NULL)
4113 xmlFree(regexp->transdata);
Daniel Veillard23e73572002-09-19 19:56:43 +00004114 if (regexp->stringMap != NULL) {
4115 for (i = 0; i < regexp->nbstrings;i++)
4116 xmlFree(regexp->stringMap[i]);
4117 xmlFree(regexp->stringMap);
4118 }
4119
Daniel Veillard4255d502002-04-16 15:50:10 +00004120 xmlFree(regexp);
4121}
4122
4123#ifdef LIBXML_AUTOMATA_ENABLED
4124/************************************************************************
4125 * *
4126 * The Automata interface *
4127 * *
4128 ************************************************************************/
4129
4130/**
4131 * xmlNewAutomata:
4132 *
4133 * Create a new automata
4134 *
4135 * Returns the new object or NULL in case of failure
4136 */
4137xmlAutomataPtr
4138xmlNewAutomata(void) {
4139 xmlAutomataPtr ctxt;
4140
4141 ctxt = xmlRegNewParserCtxt(NULL);
4142 if (ctxt == NULL)
4143 return(NULL);
4144
4145 /* initialize the parser */
4146 ctxt->end = NULL;
4147 ctxt->start = ctxt->state = xmlRegNewState(ctxt);
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00004148 if (ctxt->start == NULL) {
4149 xmlFreeAutomata(ctxt);
4150 return(NULL);
4151 }
4152 if (xmlRegStatePush(ctxt, ctxt->start) < 0) {
4153 xmlRegFreeState(ctxt->start);
4154 xmlFreeAutomata(ctxt);
4155 return(NULL);
4156 }
Daniel Veillard4255d502002-04-16 15:50:10 +00004157
4158 return(ctxt);
4159}
4160
4161/**
4162 * xmlFreeAutomata:
4163 * @am: an automata
4164 *
4165 * Free an automata
4166 */
4167void
4168xmlFreeAutomata(xmlAutomataPtr am) {
4169 if (am == NULL)
4170 return;
4171 xmlRegFreeParserCtxt(am);
4172}
4173
4174/**
4175 * xmlAutomataGetInitState:
4176 * @am: an automata
4177 *
Daniel Veillarda9b66d02002-12-11 14:23:49 +00004178 * Initial state lookup
4179 *
Daniel Veillard4255d502002-04-16 15:50:10 +00004180 * Returns the initial state of the automata
4181 */
4182xmlAutomataStatePtr
4183xmlAutomataGetInitState(xmlAutomataPtr am) {
4184 if (am == NULL)
4185 return(NULL);
4186 return(am->start);
4187}
4188
4189/**
4190 * xmlAutomataSetFinalState:
4191 * @am: an automata
4192 * @state: a state in this automata
4193 *
4194 * Makes that state a final state
4195 *
4196 * Returns 0 or -1 in case of error
4197 */
4198int
4199xmlAutomataSetFinalState(xmlAutomataPtr am, xmlAutomataStatePtr state) {
4200 if ((am == NULL) || (state == NULL))
4201 return(-1);
4202 state->type = XML_REGEXP_FINAL_STATE;
4203 return(0);
4204}
4205
4206/**
4207 * xmlAutomataNewTransition:
4208 * @am: an automata
4209 * @from: the starting point of the transition
4210 * @to: the target point of the transition or NULL
4211 * @token: the input string associated to that transition
4212 * @data: data passed to the callback function if the transition is activated
4213 *
4214 * If @to is NULL, this create first a new target state in the automata
4215 * and then adds a transition from the @from state to the target state
4216 * activated by the value of @token
4217 *
4218 * Returns the target state or NULL in case of error
4219 */
4220xmlAutomataStatePtr
4221xmlAutomataNewTransition(xmlAutomataPtr am, xmlAutomataStatePtr from,
4222 xmlAutomataStatePtr to, const xmlChar *token,
4223 void *data) {
4224 xmlRegAtomPtr atom;
4225
4226 if ((am == NULL) || (from == NULL) || (token == NULL))
4227 return(NULL);
4228 atom = xmlRegNewAtom(am, XML_REGEXP_STRING);
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00004229 if (atom == NULL)
4230 return(NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +00004231 atom->data = data;
4232 if (atom == NULL)
4233 return(NULL);
4234 atom->valuep = xmlStrdup(token);
4235
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00004236 if (xmlFAGenerateTransitions(am, from, to, atom) < 0) {
4237 xmlRegFreeAtom(atom);
4238 return(NULL);
4239 }
Daniel Veillard4255d502002-04-16 15:50:10 +00004240 if (to == NULL)
4241 return(am->state);
4242 return(to);
4243}
4244
4245/**
Daniel Veillard52b48c72003-04-13 19:53:42 +00004246 * xmlAutomataNewTransition2:
4247 * @am: an automata
4248 * @from: the starting point of the transition
4249 * @to: the target point of the transition or NULL
4250 * @token: the first input string associated to that transition
4251 * @token2: the second input string associated to that transition
4252 * @data: data passed to the callback function if the transition is activated
4253 *
4254 * If @to is NULL, this create first a new target state in the automata
4255 * and then adds a transition from the @from state to the target state
4256 * activated by the value of @token
4257 *
4258 * Returns the target state or NULL in case of error
4259 */
4260xmlAutomataStatePtr
4261xmlAutomataNewTransition2(xmlAutomataPtr am, xmlAutomataStatePtr from,
4262 xmlAutomataStatePtr to, const xmlChar *token,
4263 const xmlChar *token2, void *data) {
4264 xmlRegAtomPtr atom;
4265
4266 if ((am == NULL) || (from == NULL) || (token == NULL))
4267 return(NULL);
4268 atom = xmlRegNewAtom(am, XML_REGEXP_STRING);
4269 atom->data = data;
4270 if (atom == NULL)
4271 return(NULL);
4272 if ((token2 == NULL) || (*token2 == 0)) {
4273 atom->valuep = xmlStrdup(token);
4274 } else {
4275 int lenn, lenp;
4276 xmlChar *str;
4277
4278 lenn = strlen((char *) token2);
4279 lenp = strlen((char *) token);
4280
Daniel Veillard3c908dc2003-04-19 00:07:51 +00004281 str = (xmlChar *) xmlMallocAtomic(lenn + lenp + 2);
Daniel Veillard52b48c72003-04-13 19:53:42 +00004282 if (str == NULL) {
4283 xmlRegFreeAtom(atom);
4284 return(NULL);
4285 }
4286 memcpy(&str[0], token, lenp);
4287 str[lenp] = '|';
4288 memcpy(&str[lenp + 1], token2, lenn);
4289 str[lenn + lenp + 1] = 0;
4290
4291 atom->valuep = str;
4292 }
4293
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00004294 if (xmlFAGenerateTransitions(am, from, to, atom) < 0) {
4295 xmlRegFreeAtom(atom);
4296 return(NULL);
4297 }
Daniel Veillard52b48c72003-04-13 19:53:42 +00004298 if (to == NULL)
4299 return(am->state);
4300 return(to);
4301}
4302
4303/**
Daniel Veillard4255d502002-04-16 15:50:10 +00004304 * xmlAutomataNewCountTrans:
4305 * @am: an automata
4306 * @from: the starting point of the transition
4307 * @to: the target point of the transition or NULL
4308 * @token: the input string associated to that transition
4309 * @min: the minimum successive occurences of token
Daniel Veillarda9b66d02002-12-11 14:23:49 +00004310 * @max: the maximum successive occurences of token
4311 * @data: data associated to the transition
Daniel Veillard4255d502002-04-16 15:50:10 +00004312 *
4313 * If @to is NULL, this create first a new target state in the automata
4314 * and then adds a transition from the @from state to the target state
4315 * activated by a succession of input of value @token and whose number
4316 * is between @min and @max
4317 *
4318 * Returns the target state or NULL in case of error
4319 */
4320xmlAutomataStatePtr
4321xmlAutomataNewCountTrans(xmlAutomataPtr am, xmlAutomataStatePtr from,
4322 xmlAutomataStatePtr to, const xmlChar *token,
4323 int min, int max, void *data) {
4324 xmlRegAtomPtr atom;
4325
4326 if ((am == NULL) || (from == NULL) || (token == NULL))
4327 return(NULL);
4328 if (min < 0)
4329 return(NULL);
4330 if ((max < min) || (max < 1))
4331 return(NULL);
4332 atom = xmlRegNewAtom(am, XML_REGEXP_STRING);
4333 if (atom == NULL)
4334 return(NULL);
4335 atom->valuep = xmlStrdup(token);
4336 atom->data = data;
4337 if (min == 0)
4338 atom->min = 1;
4339 else
4340 atom->min = min;
4341 atom->max = max;
4342
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00004343 if (xmlFAGenerateTransitions(am, from, to, atom) < 0) {
4344 xmlRegFreeAtom(atom);
4345 return(NULL);
4346 }
Daniel Veillard4255d502002-04-16 15:50:10 +00004347 if (to == NULL)
4348 to = am->state;
4349 if (to == NULL)
4350 return(NULL);
4351 if (min == 0)
4352 xmlFAGenerateEpsilonTransition(am, from, to);
4353 return(to);
4354}
4355
4356/**
Daniel Veillard7646b182002-04-20 06:41:40 +00004357 * xmlAutomataNewOnceTrans:
4358 * @am: an automata
4359 * @from: the starting point of the transition
4360 * @to: the target point of the transition or NULL
4361 * @token: the input string associated to that transition
4362 * @min: the minimum successive occurences of token
Daniel Veillarda9b66d02002-12-11 14:23:49 +00004363 * @max: the maximum successive occurences of token
4364 * @data: data associated to the transition
Daniel Veillard7646b182002-04-20 06:41:40 +00004365 *
4366 * If @to is NULL, this create first a new target state in the automata
4367 * and then adds a transition from the @from state to the target state
4368 * activated by a succession of input of value @token and whose number
4369 * is between @min and @max, moreover that transistion can only be crossed
4370 * once.
4371 *
4372 * Returns the target state or NULL in case of error
4373 */
4374xmlAutomataStatePtr
4375xmlAutomataNewOnceTrans(xmlAutomataPtr am, xmlAutomataStatePtr from,
4376 xmlAutomataStatePtr to, const xmlChar *token,
4377 int min, int max, void *data) {
4378 xmlRegAtomPtr atom;
4379 int counter;
4380
4381 if ((am == NULL) || (from == NULL) || (token == NULL))
4382 return(NULL);
4383 if (min < 1)
4384 return(NULL);
4385 if ((max < min) || (max < 1))
4386 return(NULL);
4387 atom = xmlRegNewAtom(am, XML_REGEXP_STRING);
4388 if (atom == NULL)
4389 return(NULL);
4390 atom->valuep = xmlStrdup(token);
4391 atom->data = data;
4392 atom->quant = XML_REGEXP_QUANT_ONCEONLY;
4393 if (min == 0)
4394 atom->min = 1;
4395 else
4396 atom->min = min;
4397 atom->max = max;
4398 /*
4399 * associate a counter to the transition.
4400 */
4401 counter = xmlRegGetCounter(am);
4402 am->counters[counter].min = 1;
4403 am->counters[counter].max = 1;
4404
4405 /* xmlFAGenerateTransitions(am, from, to, atom); */
4406 if (to == NULL) {
4407 to = xmlRegNewState(am);
4408 xmlRegStatePush(am, to);
4409 }
4410 xmlRegStateAddTrans(am, from, atom, to, counter, -1);
4411 xmlRegAtomPush(am, atom);
4412 am->state = to;
4413 if (to == NULL)
4414 to = am->state;
4415 if (to == NULL)
4416 return(NULL);
4417 return(to);
4418}
4419
4420/**
Daniel Veillard4255d502002-04-16 15:50:10 +00004421 * xmlAutomataNewState:
4422 * @am: an automata
4423 *
4424 * Create a new disconnected state in the automata
4425 *
4426 * Returns the new state or NULL in case of error
4427 */
4428xmlAutomataStatePtr
4429xmlAutomataNewState(xmlAutomataPtr am) {
4430 xmlAutomataStatePtr to;
4431
4432 if (am == NULL)
4433 return(NULL);
4434 to = xmlRegNewState(am);
4435 xmlRegStatePush(am, to);
4436 return(to);
4437}
4438
4439/**
Daniel Veillarda9b66d02002-12-11 14:23:49 +00004440 * xmlAutomataNewEpsilon:
Daniel Veillard4255d502002-04-16 15:50:10 +00004441 * @am: an automata
4442 * @from: the starting point of the transition
4443 * @to: the target point of the transition or NULL
4444 *
4445 * If @to is NULL, this create first a new target state in the automata
4446 * and then adds a an epsilon transition from the @from state to the
4447 * target state
4448 *
4449 * Returns the target state or NULL in case of error
4450 */
4451xmlAutomataStatePtr
4452xmlAutomataNewEpsilon(xmlAutomataPtr am, xmlAutomataStatePtr from,
4453 xmlAutomataStatePtr to) {
4454 if ((am == NULL) || (from == NULL))
4455 return(NULL);
4456 xmlFAGenerateEpsilonTransition(am, from, to);
4457 if (to == NULL)
4458 return(am->state);
4459 return(to);
4460}
4461
Daniel Veillardb509f152002-04-17 16:28:10 +00004462/**
Daniel Veillard7646b182002-04-20 06:41:40 +00004463 * xmlAutomataNewAllTrans:
4464 * @am: an automata
4465 * @from: the starting point of the transition
4466 * @to: the target point of the transition or NULL
Daniel Veillarda9b66d02002-12-11 14:23:49 +00004467 * @lax: allow to transition if not all all transitions have been activated
Daniel Veillard7646b182002-04-20 06:41:40 +00004468 *
4469 * If @to is NULL, this create first a new target state in the automata
4470 * and then adds a an ALL transition from the @from state to the
4471 * target state. That transition is an epsilon transition allowed only when
4472 * all transitions from the @from node have been activated.
4473 *
4474 * Returns the target state or NULL in case of error
4475 */
4476xmlAutomataStatePtr
4477xmlAutomataNewAllTrans(xmlAutomataPtr am, xmlAutomataStatePtr from,
Daniel Veillard441bc322002-04-20 17:38:48 +00004478 xmlAutomataStatePtr to, int lax) {
Daniel Veillard7646b182002-04-20 06:41:40 +00004479 if ((am == NULL) || (from == NULL))
4480 return(NULL);
Daniel Veillard441bc322002-04-20 17:38:48 +00004481 xmlFAGenerateAllTransition(am, from, to, lax);
Daniel Veillard7646b182002-04-20 06:41:40 +00004482 if (to == NULL)
4483 return(am->state);
4484 return(to);
4485}
4486
4487/**
Daniel Veillardb509f152002-04-17 16:28:10 +00004488 * xmlAutomataNewCounter:
4489 * @am: an automata
4490 * @min: the minimal value on the counter
4491 * @max: the maximal value on the counter
4492 *
4493 * Create a new counter
4494 *
4495 * Returns the counter number or -1 in case of error
4496 */
4497int
4498xmlAutomataNewCounter(xmlAutomataPtr am, int min, int max) {
4499 int ret;
4500
4501 if (am == NULL)
4502 return(-1);
4503
4504 ret = xmlRegGetCounter(am);
4505 if (ret < 0)
4506 return(-1);
4507 am->counters[ret].min = min;
4508 am->counters[ret].max = max;
4509 return(ret);
4510}
4511
4512/**
4513 * xmlAutomataNewCountedTrans:
4514 * @am: an automata
4515 * @from: the starting point of the transition
4516 * @to: the target point of the transition or NULL
4517 * @counter: the counter associated to that transition
4518 *
4519 * If @to is NULL, this create first a new target state in the automata
4520 * and then adds an epsilon transition from the @from state to the target state
4521 * which will increment the counter provided
4522 *
4523 * Returns the target state or NULL in case of error
4524 */
4525xmlAutomataStatePtr
4526xmlAutomataNewCountedTrans(xmlAutomataPtr am, xmlAutomataStatePtr from,
4527 xmlAutomataStatePtr to, int counter) {
4528 if ((am == NULL) || (from == NULL) || (counter < 0))
4529 return(NULL);
4530 xmlFAGenerateCountedEpsilonTransition(am, from, to, counter);
4531 if (to == NULL)
4532 return(am->state);
4533 return(to);
4534}
4535
4536/**
4537 * xmlAutomataNewCounterTrans:
4538 * @am: an automata
4539 * @from: the starting point of the transition
4540 * @to: the target point of the transition or NULL
4541 * @counter: the counter associated to that transition
4542 *
4543 * If @to is NULL, this create first a new target state in the automata
4544 * and then adds an epsilon transition from the @from state to the target state
4545 * which will be allowed only if the counter is within the right range.
4546 *
4547 * Returns the target state or NULL in case of error
4548 */
4549xmlAutomataStatePtr
4550xmlAutomataNewCounterTrans(xmlAutomataPtr am, xmlAutomataStatePtr from,
4551 xmlAutomataStatePtr to, int counter) {
4552 if ((am == NULL) || (from == NULL) || (counter < 0))
4553 return(NULL);
4554 xmlFAGenerateCountedTransition(am, from, to, counter);
4555 if (to == NULL)
4556 return(am->state);
4557 return(to);
4558}
Daniel Veillard4255d502002-04-16 15:50:10 +00004559
4560/**
4561 * xmlAutomataCompile:
4562 * @am: an automata
4563 *
4564 * Compile the automata into a Reg Exp ready for being executed.
4565 * The automata should be free after this point.
4566 *
4567 * Returns the compiled regexp or NULL in case of error
4568 */
4569xmlRegexpPtr
4570xmlAutomataCompile(xmlAutomataPtr am) {
4571 xmlRegexpPtr ret;
4572
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00004573 if ((am == NULL) || (am->error != 0)) return(NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +00004574 xmlFAEliminateEpsilonTransitions(am);
Daniel Veillard23e73572002-09-19 19:56:43 +00004575 /* xmlFAComputesDeterminism(am); */
Daniel Veillard4255d502002-04-16 15:50:10 +00004576 ret = xmlRegEpxFromParse(am);
4577
4578 return(ret);
4579}
Daniel Veillarde19fc232002-04-22 16:01:24 +00004580
4581/**
4582 * xmlAutomataIsDeterminist:
4583 * @am: an automata
4584 *
4585 * Checks if an automata is determinist.
4586 *
4587 * Returns 1 if true, 0 if not, and -1 in case of error
4588 */
4589int
4590xmlAutomataIsDeterminist(xmlAutomataPtr am) {
4591 int ret;
4592
4593 if (am == NULL)
4594 return(-1);
4595
4596 ret = xmlFAComputesDeterminism(am);
4597 return(ret);
4598}
Daniel Veillard4255d502002-04-16 15:50:10 +00004599#endif /* LIBXML_AUTOMATA_ENABLED */
4600#endif /* LIBXML_REGEXP_ENABLED */