blob: f971f0c82b70661def884fb5c31c83a7b9da2043 [file] [log] [blame]
Daniel Veillard4255d502002-04-16 15:50:10 +00001/*
2 * regexp.c: generic and extensible Regular Expression engine
3 *
Daniel Veillardf8e3db02012-09-11 13:26:36 +08004 * Basically designed with the purpose of compiling regexps for
Haibo Huangcfd91dc2020-07-30 23:01:33 -07005 * the variety of validation/schemas mechanisms now available in
William M. Brackddf71d62004-05-06 04:17:26 +00006 * XML related specifications these include:
Daniel Veillard4255d502002-04-16 15:50:10 +00007 * - XML-1.0 DTD validation
8 * - XML Schemas structure part 1
9 * - XML Schemas Datatypes part 2 especially Appendix F
10 * - RELAX-NG/TREX i.e. the counter proposal
11 *
12 * See Copyright for the status of this software.
13 *
14 * Daniel Veillard <veillard@redhat.com>
15 */
16
17#define IN_LIBXML
18#include "libxml.h"
19
20#ifdef LIBXML_REGEXP_ENABLED
21
Daniel Veillardcee2b3a2005-01-25 00:22:52 +000022/* #define DEBUG_ERR */
Daniel Veillardfc0b6f62005-01-09 17:48:02 +000023
Daniel Veillard4255d502002-04-16 15:50:10 +000024#include <stdio.h>
25#include <string.h>
Daniel Veillardebe48c62003-12-03 12:12:27 +000026#ifdef HAVE_LIMITS_H
27#include <limits.h>
28#endif
Haibo Huangcfd91dc2020-07-30 23:01:33 -070029#ifdef HAVE_STDINT_H
30#include <stdint.h>
31#endif
Daniel Veillardebe48c62003-12-03 12:12:27 +000032
Daniel Veillard4255d502002-04-16 15:50:10 +000033#include <libxml/tree.h>
34#include <libxml/parserInternals.h>
35#include <libxml/xmlregexp.h>
36#include <libxml/xmlautomata.h>
37#include <libxml/xmlunicode.h>
38
Daniel Veillardebe48c62003-12-03 12:12:27 +000039#ifndef INT_MAX
40#define INT_MAX 123456789 /* easy to flag and big enough for our needs */
41#endif
Haibo Huangcfd91dc2020-07-30 23:01:33 -070042#ifndef SIZE_MAX
43#define SIZE_MAX ((size_t) -1)
44#endif
Daniel Veillardebe48c62003-12-03 12:12:27 +000045
Daniel Veillardc0826a72004-08-10 14:17:33 +000046/* #define DEBUG_REGEXP_GRAPH */
Daniel Veillard10752282005-08-08 13:05:13 +000047/* #define DEBUG_REGEXP_EXEC */
Daniel Veillard4255d502002-04-16 15:50:10 +000048/* #define DEBUG_PUSH */
Daniel Veillard23e73572002-09-19 19:56:43 +000049/* #define DEBUG_COMPACTION */
Daniel Veillard4255d502002-04-16 15:50:10 +000050
Daniel Veillard567a45b2005-10-18 19:11:55 +000051#define MAX_PUSH 10000000
Daniel Veillard94cc1032005-09-15 13:09:00 +000052
Patrick R. Gansterer204f1f12012-05-10 20:24:00 +080053#ifdef ERROR
54#undef ERROR
55#endif
Daniel Veillardff46a042003-10-08 08:53:17 +000056#define ERROR(str) \
57 ctxt->error = XML_REGEXP_COMPILE_ERROR; \
58 xmlRegexpErrCompile(ctxt, str);
Daniel Veillard4255d502002-04-16 15:50:10 +000059#define NEXT ctxt->cur++
60#define CUR (*(ctxt->cur))
61#define NXT(index) (ctxt->cur[index])
62
63#define CUR_SCHAR(s, l) xmlStringCurrentChar(NULL, s, &l)
64#define NEXTL(l) ctxt->cur += l;
Daniel Veillardc0826a72004-08-10 14:17:33 +000065#define XML_REG_STRING_SEPARATOR '|'
William M. Bracka9cbf282007-03-21 13:16:33 +000066/*
67 * Need PREV to check on a '-' within a Character Group. May only be used
68 * when it's guaranteed that cur is not at the beginning of ctxt->string!
69 */
70#define PREV (ctxt->cur[-1])
Daniel Veillard4255d502002-04-16 15:50:10 +000071
Daniel Veillarde19fc232002-04-22 16:01:24 +000072/**
73 * TODO:
74 *
75 * macro to flag unimplemented blocks
76 */
Daniel Veillardf8e3db02012-09-11 13:26:36 +080077#define TODO \
Daniel Veillarde19fc232002-04-22 16:01:24 +000078 xmlGenericError(xmlGenericErrorContext, \
79 "Unimplemented block at %s:%d\n", \
80 __FILE__, __LINE__);
81
Daniel Veillard4255d502002-04-16 15:50:10 +000082/************************************************************************
Daniel Veillardf8e3db02012-09-11 13:26:36 +080083 * *
84 * Datatypes and structures *
85 * *
Daniel Veillard4255d502002-04-16 15:50:10 +000086 ************************************************************************/
87
Daniel Veillardfc011b72006-02-12 19:14:15 +000088/*
89 * Note: the order of the enums below is significant, do not shuffle
90 */
Daniel Veillard4255d502002-04-16 15:50:10 +000091typedef enum {
92 XML_REGEXP_EPSILON = 1,
93 XML_REGEXP_CHARVAL,
94 XML_REGEXP_RANGES,
Daniel Veillard567a45b2005-10-18 19:11:55 +000095 XML_REGEXP_SUBREG, /* used for () sub regexps */
Daniel Veillard4255d502002-04-16 15:50:10 +000096 XML_REGEXP_STRING,
97 XML_REGEXP_ANYCHAR, /* . */
98 XML_REGEXP_ANYSPACE, /* \s */
99 XML_REGEXP_NOTSPACE, /* \S */
100 XML_REGEXP_INITNAME, /* \l */
Daniel Veillard567a45b2005-10-18 19:11:55 +0000101 XML_REGEXP_NOTINITNAME, /* \L */
Daniel Veillard4255d502002-04-16 15:50:10 +0000102 XML_REGEXP_NAMECHAR, /* \c */
103 XML_REGEXP_NOTNAMECHAR, /* \C */
104 XML_REGEXP_DECIMAL, /* \d */
Daniel Veillard567a45b2005-10-18 19:11:55 +0000105 XML_REGEXP_NOTDECIMAL, /* \D */
Daniel Veillard4255d502002-04-16 15:50:10 +0000106 XML_REGEXP_REALCHAR, /* \w */
Daniel Veillard567a45b2005-10-18 19:11:55 +0000107 XML_REGEXP_NOTREALCHAR, /* \W */
108 XML_REGEXP_LETTER = 100,
Daniel Veillard4255d502002-04-16 15:50:10 +0000109 XML_REGEXP_LETTER_UPPERCASE,
110 XML_REGEXP_LETTER_LOWERCASE,
111 XML_REGEXP_LETTER_TITLECASE,
112 XML_REGEXP_LETTER_MODIFIER,
113 XML_REGEXP_LETTER_OTHERS,
114 XML_REGEXP_MARK,
115 XML_REGEXP_MARK_NONSPACING,
116 XML_REGEXP_MARK_SPACECOMBINING,
117 XML_REGEXP_MARK_ENCLOSING,
118 XML_REGEXP_NUMBER,
119 XML_REGEXP_NUMBER_DECIMAL,
120 XML_REGEXP_NUMBER_LETTER,
121 XML_REGEXP_NUMBER_OTHERS,
122 XML_REGEXP_PUNCT,
123 XML_REGEXP_PUNCT_CONNECTOR,
124 XML_REGEXP_PUNCT_DASH,
125 XML_REGEXP_PUNCT_OPEN,
126 XML_REGEXP_PUNCT_CLOSE,
127 XML_REGEXP_PUNCT_INITQUOTE,
128 XML_REGEXP_PUNCT_FINQUOTE,
129 XML_REGEXP_PUNCT_OTHERS,
130 XML_REGEXP_SEPAR,
131 XML_REGEXP_SEPAR_SPACE,
132 XML_REGEXP_SEPAR_LINE,
133 XML_REGEXP_SEPAR_PARA,
134 XML_REGEXP_SYMBOL,
135 XML_REGEXP_SYMBOL_MATH,
136 XML_REGEXP_SYMBOL_CURRENCY,
137 XML_REGEXP_SYMBOL_MODIFIER,
138 XML_REGEXP_SYMBOL_OTHERS,
139 XML_REGEXP_OTHER,
140 XML_REGEXP_OTHER_CONTROL,
141 XML_REGEXP_OTHER_FORMAT,
142 XML_REGEXP_OTHER_PRIVATE,
143 XML_REGEXP_OTHER_NA,
144 XML_REGEXP_BLOCK_NAME
145} xmlRegAtomType;
146
147typedef enum {
148 XML_REGEXP_QUANT_EPSILON = 1,
149 XML_REGEXP_QUANT_ONCE,
150 XML_REGEXP_QUANT_OPT,
151 XML_REGEXP_QUANT_MULT,
152 XML_REGEXP_QUANT_PLUS,
Daniel Veillard7646b182002-04-20 06:41:40 +0000153 XML_REGEXP_QUANT_ONCEONLY,
154 XML_REGEXP_QUANT_ALL,
Daniel Veillard4255d502002-04-16 15:50:10 +0000155 XML_REGEXP_QUANT_RANGE
156} xmlRegQuantType;
157
158typedef enum {
159 XML_REGEXP_START_STATE = 1,
160 XML_REGEXP_FINAL_STATE,
Daniel Veillardcc026dc2005-01-12 13:21:17 +0000161 XML_REGEXP_TRANS_STATE,
Daniel Veillard0e05f4c2006-11-01 15:33:04 +0000162 XML_REGEXP_SINK_STATE,
163 XML_REGEXP_UNREACH_STATE
Daniel Veillard4255d502002-04-16 15:50:10 +0000164} xmlRegStateType;
165
166typedef enum {
167 XML_REGEXP_MARK_NORMAL = 0,
168 XML_REGEXP_MARK_START,
169 XML_REGEXP_MARK_VISITED
170} xmlRegMarkedType;
171
172typedef struct _xmlRegRange xmlRegRange;
173typedef xmlRegRange *xmlRegRangePtr;
174
175struct _xmlRegRange {
Daniel Veillardf8b9de32003-11-24 14:27:26 +0000176 int neg; /* 0 normal, 1 not, 2 exclude */
Daniel Veillard4255d502002-04-16 15:50:10 +0000177 xmlRegAtomType type;
178 int start;
179 int end;
180 xmlChar *blockName;
181};
182
183typedef struct _xmlRegAtom xmlRegAtom;
184typedef xmlRegAtom *xmlRegAtomPtr;
185
186typedef struct _xmlAutomataState xmlRegState;
187typedef xmlRegState *xmlRegStatePtr;
188
189struct _xmlRegAtom {
190 int no;
191 xmlRegAtomType type;
192 xmlRegQuantType quant;
193 int min;
194 int max;
195
196 void *valuep;
Daniel Veillarda646cfd2002-09-17 21:50:03 +0000197 void *valuep2;
Daniel Veillard4255d502002-04-16 15:50:10 +0000198 int neg;
199 int codepoint;
200 xmlRegStatePtr start;
Daniel Veillard76d59b62007-08-22 16:29:21 +0000201 xmlRegStatePtr start0;
Daniel Veillard4255d502002-04-16 15:50:10 +0000202 xmlRegStatePtr stop;
203 int maxRanges;
204 int nbRanges;
205 xmlRegRangePtr *ranges;
206 void *data;
207};
208
209typedef struct _xmlRegCounter xmlRegCounter;
210typedef xmlRegCounter *xmlRegCounterPtr;
211
212struct _xmlRegCounter {
213 int min;
214 int max;
215};
216
217typedef struct _xmlRegTrans xmlRegTrans;
218typedef xmlRegTrans *xmlRegTransPtr;
219
220struct _xmlRegTrans {
221 xmlRegAtomPtr atom;
222 int to;
223 int counter;
224 int count;
Daniel Veillard567a45b2005-10-18 19:11:55 +0000225 int nd;
Daniel Veillard4255d502002-04-16 15:50:10 +0000226};
227
228struct _xmlAutomataState {
229 xmlRegStateType type;
230 xmlRegMarkedType mark;
Daniel Veillard466fcda2012-08-27 12:03:40 +0800231 xmlRegMarkedType markd;
Daniel Veillard23e73572002-09-19 19:56:43 +0000232 xmlRegMarkedType reached;
Daniel Veillard4255d502002-04-16 15:50:10 +0000233 int no;
Daniel Veillard4255d502002-04-16 15:50:10 +0000234 int maxTrans;
235 int nbTrans;
236 xmlRegTrans *trans;
Haibo Huangcfd91dc2020-07-30 23:01:33 -0700237 /* knowing states pointing to us can speed things up */
Daniel Veillarddb68b742005-07-30 13:18:24 +0000238 int maxTransTo;
239 int nbTransTo;
240 int *transTo;
Daniel Veillard4255d502002-04-16 15:50:10 +0000241};
242
243typedef struct _xmlAutomata xmlRegParserCtxt;
244typedef xmlRegParserCtxt *xmlRegParserCtxtPtr;
245
Daniel Veillard1ba2aca2009-08-31 16:47:39 +0200246#define AM_AUTOMATA_RNG 1
247
Daniel Veillard4255d502002-04-16 15:50:10 +0000248struct _xmlAutomata {
249 xmlChar *string;
250 xmlChar *cur;
251
252 int error;
253 int neg;
254
255 xmlRegStatePtr start;
256 xmlRegStatePtr end;
257 xmlRegStatePtr state;
258
259 xmlRegAtomPtr atom;
260
261 int maxAtoms;
262 int nbAtoms;
263 xmlRegAtomPtr *atoms;
264
265 int maxStates;
266 int nbStates;
267 xmlRegStatePtr *states;
268
269 int maxCounters;
270 int nbCounters;
271 xmlRegCounter *counters;
Daniel Veillarde19fc232002-04-22 16:01:24 +0000272
273 int determinist;
Daniel Veillard6e65e152005-08-09 11:09:52 +0000274 int negs;
Daniel Veillard1ba2aca2009-08-31 16:47:39 +0200275 int flags;
Haibo Huangcfd91dc2020-07-30 23:01:33 -0700276
277 int depth;
Daniel Veillard4255d502002-04-16 15:50:10 +0000278};
279
280struct _xmlRegexp {
281 xmlChar *string;
282 int nbStates;
283 xmlRegStatePtr *states;
284 int nbAtoms;
285 xmlRegAtomPtr *atoms;
286 int nbCounters;
287 xmlRegCounter *counters;
Daniel Veillarde19fc232002-04-22 16:01:24 +0000288 int determinist;
Daniel Veillard1ba2aca2009-08-31 16:47:39 +0200289 int flags;
Daniel Veillard23e73572002-09-19 19:56:43 +0000290 /*
291 * That's the compact form for determinists automatas
292 */
293 int nbstates;
294 int *compact;
Daniel Veillard118aed72002-09-24 14:13:13 +0000295 void **transdata;
Daniel Veillard23e73572002-09-19 19:56:43 +0000296 int nbstrings;
297 xmlChar **stringMap;
Daniel Veillard4255d502002-04-16 15:50:10 +0000298};
299
300typedef struct _xmlRegExecRollback xmlRegExecRollback;
301typedef xmlRegExecRollback *xmlRegExecRollbackPtr;
302
303struct _xmlRegExecRollback {
304 xmlRegStatePtr state;/* the current state */
305 int index; /* the index in the input stack */
306 int nextbranch; /* the next transition to explore in that state */
William M. Brackddf71d62004-05-06 04:17:26 +0000307 int *counts; /* save the automata state if it has some */
Daniel Veillard4255d502002-04-16 15:50:10 +0000308};
309
310typedef struct _xmlRegInputToken xmlRegInputToken;
311typedef xmlRegInputToken *xmlRegInputTokenPtr;
312
313struct _xmlRegInputToken {
314 xmlChar *value;
315 void *data;
316};
317
318struct _xmlRegExecCtxt {
319 int status; /* execution status != 0 indicate an error */
William M. Brackddf71d62004-05-06 04:17:26 +0000320 int determinist; /* did we find an indeterministic behaviour */
Daniel Veillard4255d502002-04-16 15:50:10 +0000321 xmlRegexpPtr comp; /* the compiled regexp */
322 xmlRegExecCallbacks callback;
323 void *data;
324
325 xmlRegStatePtr state;/* the current state */
326 int transno; /* the current transition on that state */
William M. Brackddf71d62004-05-06 04:17:26 +0000327 int transcount; /* the number of chars in char counted transitions */
Daniel Veillard4255d502002-04-16 15:50:10 +0000328
329 /*
330 * A stack of rollback states
331 */
332 int maxRollbacks;
333 int nbRollbacks;
334 xmlRegExecRollback *rollbacks;
335
336 /*
337 * The state of the automata if any
338 */
339 int *counts;
340
341 /*
342 * The input stack
343 */
344 int inputStackMax;
345 int inputStackNr;
346 int index;
347 int *charStack;
348 const xmlChar *inputString; /* when operating on characters */
349 xmlRegInputTokenPtr inputStack;/* when operating on strings */
350
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +0000351 /*
352 * error handling
353 */
354 int errStateNo; /* the error state number */
355 xmlRegStatePtr errState; /* the error state */
356 xmlChar *errString; /* the string raising the error */
357 int *errCounts; /* counters at the error state */
Daniel Veillard94cc1032005-09-15 13:09:00 +0000358 int nbPush;
Daniel Veillard4255d502002-04-16 15:50:10 +0000359};
360
Daniel Veillard441bc322002-04-20 17:38:48 +0000361#define REGEXP_ALL_COUNTER 0x123456
362#define REGEXP_ALL_LAX_COUNTER 0x123457
Daniel Veillard7646b182002-04-20 06:41:40 +0000363
Daniel Veillard4255d502002-04-16 15:50:10 +0000364static void xmlFAParseRegExp(xmlRegParserCtxtPtr ctxt, int top);
Daniel Veillard23e73572002-09-19 19:56:43 +0000365static void xmlRegFreeState(xmlRegStatePtr state);
366static void xmlRegFreeAtom(xmlRegAtomPtr atom);
Daniel Veillard9efc4762005-07-19 14:33:55 +0000367static int xmlRegStrEqualWildcard(const xmlChar *expStr, const xmlChar *valStr);
Daniel Veillard567a45b2005-10-18 19:11:55 +0000368static int xmlRegCheckCharacter(xmlRegAtomPtr atom, int codepoint);
369static int xmlRegCheckCharacterRange(xmlRegAtomType type, int codepoint,
370 int neg, int start, int end, const xmlChar *blockName);
Daniel Veillard4255d502002-04-16 15:50:10 +0000371
Daniel Veillard1ba2aca2009-08-31 16:47:39 +0200372void xmlAutomataSetFlags(xmlAutomataPtr am, int flags);
373
Daniel Veillard4255d502002-04-16 15:50:10 +0000374/************************************************************************
Daniel Veillardff46a042003-10-08 08:53:17 +0000375 * *
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800376 * Regexp memory error handler *
Daniel Veillardff46a042003-10-08 08:53:17 +0000377 * *
378 ************************************************************************/
379/**
380 * xmlRegexpErrMemory:
William M. Brackddf71d62004-05-06 04:17:26 +0000381 * @extra: extra information
Daniel Veillardff46a042003-10-08 08:53:17 +0000382 *
383 * Handle an out of memory condition
384 */
385static void
386xmlRegexpErrMemory(xmlRegParserCtxtPtr ctxt, const char *extra)
387{
388 const char *regexp = NULL;
389 if (ctxt != NULL) {
390 regexp = (const char *) ctxt->string;
391 ctxt->error = XML_ERR_NO_MEMORY;
392 }
Daniel Veillard659e71e2003-10-10 14:10:40 +0000393 __xmlRaiseError(NULL, NULL, NULL, NULL, NULL, XML_FROM_REGEXP,
Daniel Veillardff46a042003-10-08 08:53:17 +0000394 XML_ERR_NO_MEMORY, XML_ERR_FATAL, NULL, 0, extra,
395 regexp, NULL, 0, 0,
396 "Memory allocation failed : %s\n", extra);
397}
398
399/**
400 * xmlRegexpErrCompile:
William M. Brackddf71d62004-05-06 04:17:26 +0000401 * @extra: extra information
Daniel Veillardff46a042003-10-08 08:53:17 +0000402 *
William M. Brackddf71d62004-05-06 04:17:26 +0000403 * Handle a compilation failure
Daniel Veillardff46a042003-10-08 08:53:17 +0000404 */
405static void
406xmlRegexpErrCompile(xmlRegParserCtxtPtr ctxt, const char *extra)
407{
408 const char *regexp = NULL;
409 int idx = 0;
410
411 if (ctxt != NULL) {
412 regexp = (const char *) ctxt->string;
413 idx = ctxt->cur - ctxt->string;
414 ctxt->error = XML_REGEXP_COMPILE_ERROR;
415 }
Daniel Veillard659e71e2003-10-10 14:10:40 +0000416 __xmlRaiseError(NULL, NULL, NULL, NULL, NULL, XML_FROM_REGEXP,
Daniel Veillardff46a042003-10-08 08:53:17 +0000417 XML_REGEXP_COMPILE_ERROR, XML_ERR_FATAL, NULL, 0, extra,
418 regexp, NULL, idx, 0,
419 "failed to compile: %s\n", extra);
420}
421
422/************************************************************************
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800423 * *
424 * Allocation/Deallocation *
425 * *
Daniel Veillard4255d502002-04-16 15:50:10 +0000426 ************************************************************************/
427
Daniel Veillard23e73572002-09-19 19:56:43 +0000428static int xmlFAComputesDeterminism(xmlRegParserCtxtPtr ctxt);
Haibo Huangcfd91dc2020-07-30 23:01:33 -0700429
430/**
431 * xmlRegCalloc2:
432 * @dim1: size of first dimension
433 * @dim2: size of second dimension
434 * @elemSize: size of element
435 *
436 * Allocate a two-dimensional array and set all elements to zero.
437 *
438 * Returns the new array or NULL in case of error.
439 */
440static void*
441xmlRegCalloc2(size_t dim1, size_t dim2, size_t elemSize) {
442 size_t totalSize;
443 void *ret;
444
445 /* Check for overflow */
446 if (dim1 > SIZE_MAX / dim2 / elemSize)
447 return (NULL);
448 totalSize = dim1 * dim2 * elemSize;
449 ret = xmlMalloc(totalSize);
450 if (ret != NULL)
451 memset(ret, 0, totalSize);
452 return (ret);
453}
454
Daniel Veillard4255d502002-04-16 15:50:10 +0000455/**
456 * xmlRegEpxFromParse:
457 * @ctxt: the parser context used to build it
458 *
William M. Brackddf71d62004-05-06 04:17:26 +0000459 * Allocate a new regexp and fill it with the result from the parser
Daniel Veillard4255d502002-04-16 15:50:10 +0000460 *
461 * Returns the new regexp or NULL in case of error
462 */
463static xmlRegexpPtr
464xmlRegEpxFromParse(xmlRegParserCtxtPtr ctxt) {
465 xmlRegexpPtr ret;
466
467 ret = (xmlRegexpPtr) xmlMalloc(sizeof(xmlRegexp));
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000468 if (ret == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +0000469 xmlRegexpErrMemory(ctxt, "compiling regexp");
Daniel Veillard4255d502002-04-16 15:50:10 +0000470 return(NULL);
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000471 }
Daniel Veillard4255d502002-04-16 15:50:10 +0000472 memset(ret, 0, sizeof(xmlRegexp));
473 ret->string = ctxt->string;
Daniel Veillard4255d502002-04-16 15:50:10 +0000474 ret->nbStates = ctxt->nbStates;
Daniel Veillard4255d502002-04-16 15:50:10 +0000475 ret->states = ctxt->states;
Daniel Veillard4255d502002-04-16 15:50:10 +0000476 ret->nbAtoms = ctxt->nbAtoms;
Daniel Veillard4255d502002-04-16 15:50:10 +0000477 ret->atoms = ctxt->atoms;
Daniel Veillard4255d502002-04-16 15:50:10 +0000478 ret->nbCounters = ctxt->nbCounters;
Daniel Veillard4255d502002-04-16 15:50:10 +0000479 ret->counters = ctxt->counters;
Daniel Veillarde19fc232002-04-22 16:01:24 +0000480 ret->determinist = ctxt->determinist;
Daniel Veillard1ba2aca2009-08-31 16:47:39 +0200481 ret->flags = ctxt->flags;
Daniel Veillard567a45b2005-10-18 19:11:55 +0000482 if (ret->determinist == -1) {
483 xmlRegexpIsDeterminist(ret);
484 }
Daniel Veillard23e73572002-09-19 19:56:43 +0000485
486 if ((ret->determinist != 0) &&
487 (ret->nbCounters == 0) &&
Daniel Veillard6e65e152005-08-09 11:09:52 +0000488 (ctxt->negs == 0) &&
Daniel Veillard118aed72002-09-24 14:13:13 +0000489 (ret->atoms != NULL) &&
Daniel Veillard23e73572002-09-19 19:56:43 +0000490 (ret->atoms[0] != NULL) &&
491 (ret->atoms[0]->type == XML_REGEXP_STRING)) {
492 int i, j, nbstates = 0, nbatoms = 0;
493 int *stateRemap;
494 int *stringRemap;
495 int *transitions;
Daniel Veillard118aed72002-09-24 14:13:13 +0000496 void **transdata;
Daniel Veillard23e73572002-09-19 19:56:43 +0000497 xmlChar **stringMap;
498 xmlChar *value;
499
500 /*
501 * Switch to a compact representation
502 * 1/ counting the effective number of states left
William M. Brackddf71d62004-05-06 04:17:26 +0000503 * 2/ counting the unique number of atoms, and check that
Daniel Veillard23e73572002-09-19 19:56:43 +0000504 * they are all of the string type
505 * 3/ build a table state x atom for the transitions
506 */
507
508 stateRemap = xmlMalloc(ret->nbStates * sizeof(int));
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000509 if (stateRemap == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +0000510 xmlRegexpErrMemory(ctxt, "compiling regexp");
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000511 xmlFree(ret);
512 return(NULL);
513 }
Daniel Veillard23e73572002-09-19 19:56:43 +0000514 for (i = 0;i < ret->nbStates;i++) {
515 if (ret->states[i] != NULL) {
516 stateRemap[i] = nbstates;
517 nbstates++;
518 } else {
519 stateRemap[i] = -1;
520 }
521 }
522#ifdef DEBUG_COMPACTION
523 printf("Final: %d states\n", nbstates);
524#endif
525 stringMap = xmlMalloc(ret->nbAtoms * sizeof(char *));
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000526 if (stringMap == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +0000527 xmlRegexpErrMemory(ctxt, "compiling regexp");
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000528 xmlFree(stateRemap);
529 xmlFree(ret);
530 return(NULL);
531 }
Daniel Veillard23e73572002-09-19 19:56:43 +0000532 stringRemap = xmlMalloc(ret->nbAtoms * sizeof(int));
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000533 if (stringRemap == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +0000534 xmlRegexpErrMemory(ctxt, "compiling regexp");
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000535 xmlFree(stringMap);
536 xmlFree(stateRemap);
537 xmlFree(ret);
538 return(NULL);
539 }
Daniel Veillard23e73572002-09-19 19:56:43 +0000540 for (i = 0;i < ret->nbAtoms;i++) {
541 if ((ret->atoms[i]->type == XML_REGEXP_STRING) &&
542 (ret->atoms[i]->quant == XML_REGEXP_QUANT_ONCE)) {
543 value = ret->atoms[i]->valuep;
544 for (j = 0;j < nbatoms;j++) {
545 if (xmlStrEqual(stringMap[j], value)) {
546 stringRemap[i] = j;
547 break;
548 }
549 }
550 if (j >= nbatoms) {
551 stringRemap[i] = nbatoms;
552 stringMap[nbatoms] = xmlStrdup(value);
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000553 if (stringMap[nbatoms] == NULL) {
554 for (i = 0;i < nbatoms;i++)
555 xmlFree(stringMap[i]);
556 xmlFree(stringRemap);
557 xmlFree(stringMap);
558 xmlFree(stateRemap);
559 xmlFree(ret);
560 return(NULL);
561 }
Daniel Veillard23e73572002-09-19 19:56:43 +0000562 nbatoms++;
563 }
564 } else {
565 xmlFree(stateRemap);
566 xmlFree(stringRemap);
567 for (i = 0;i < nbatoms;i++)
568 xmlFree(stringMap[i]);
569 xmlFree(stringMap);
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000570 xmlFree(ret);
571 return(NULL);
Daniel Veillard23e73572002-09-19 19:56:43 +0000572 }
573 }
574#ifdef DEBUG_COMPACTION
575 printf("Final: %d atoms\n", nbatoms);
576#endif
Haibo Huangcfd91dc2020-07-30 23:01:33 -0700577 transitions = (int *) xmlRegCalloc2(nbstates + 1, nbatoms + 1,
578 sizeof(int));
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000579 if (transitions == NULL) {
580 xmlFree(stateRemap);
581 xmlFree(stringRemap);
Haibo Huangcfd91dc2020-07-30 23:01:33 -0700582 for (i = 0;i < nbatoms;i++)
583 xmlFree(stringMap[i]);
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000584 xmlFree(stringMap);
585 xmlFree(ret);
586 return(NULL);
587 }
Daniel Veillard23e73572002-09-19 19:56:43 +0000588
589 /*
590 * Allocate the transition table. The first entry for each
William M. Brackddf71d62004-05-06 04:17:26 +0000591 * state corresponds to the state type.
Daniel Veillard23e73572002-09-19 19:56:43 +0000592 */
Daniel Veillard118aed72002-09-24 14:13:13 +0000593 transdata = NULL;
Daniel Veillard23e73572002-09-19 19:56:43 +0000594
595 for (i = 0;i < ret->nbStates;i++) {
596 int stateno, atomno, targetno, prev;
597 xmlRegStatePtr state;
598 xmlRegTransPtr trans;
599
600 stateno = stateRemap[i];
601 if (stateno == -1)
602 continue;
603 state = ret->states[i];
604
605 transitions[stateno * (nbatoms + 1)] = state->type;
606
607 for (j = 0;j < state->nbTrans;j++) {
608 trans = &(state->trans[j]);
609 if ((trans->to == -1) || (trans->atom == NULL))
610 continue;
611 atomno = stringRemap[trans->atom->no];
Daniel Veillard118aed72002-09-24 14:13:13 +0000612 if ((trans->atom->data != NULL) && (transdata == NULL)) {
Haibo Huangcfd91dc2020-07-30 23:01:33 -0700613 transdata = (void **) xmlRegCalloc2(nbstates, nbatoms,
614 sizeof(void *));
615 if (transdata == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +0000616 xmlRegexpErrMemory(ctxt, "compiling regexp");
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000617 break;
618 }
Daniel Veillard118aed72002-09-24 14:13:13 +0000619 }
Daniel Veillard23e73572002-09-19 19:56:43 +0000620 targetno = stateRemap[trans->to];
621 /*
William M. Brackddf71d62004-05-06 04:17:26 +0000622 * if the same atom can generate transitions to 2 different
Haibo Huangcfd91dc2020-07-30 23:01:33 -0700623 * states then it means the automata is not deterministic and
Daniel Veillard23e73572002-09-19 19:56:43 +0000624 * the compact form can't be used !
625 */
626 prev = transitions[stateno * (nbatoms + 1) + atomno + 1];
627 if (prev != 0) {
628 if (prev != targetno + 1) {
Daniel Veillard23e73572002-09-19 19:56:43 +0000629 ret->determinist = 0;
630#ifdef DEBUG_COMPACTION
631 printf("Indet: state %d trans %d, atom %d to %d : %d to %d\n",
632 i, j, trans->atom->no, trans->to, atomno, targetno);
633 printf(" previous to is %d\n", prev);
634#endif
Daniel Veillard118aed72002-09-24 14:13:13 +0000635 if (transdata != NULL)
636 xmlFree(transdata);
Daniel Veillard23e73572002-09-19 19:56:43 +0000637 xmlFree(transitions);
638 xmlFree(stateRemap);
639 xmlFree(stringRemap);
640 for (i = 0;i < nbatoms;i++)
641 xmlFree(stringMap[i]);
642 xmlFree(stringMap);
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000643 goto not_determ;
Daniel Veillard23e73572002-09-19 19:56:43 +0000644 }
645 } else {
646#if 0
647 printf("State %d trans %d: atom %d to %d : %d to %d\n",
648 i, j, trans->atom->no, trans->to, atomno, targetno);
649#endif
650 transitions[stateno * (nbatoms + 1) + atomno + 1] =
Daniel Veillard118aed72002-09-24 14:13:13 +0000651 targetno + 1; /* to avoid 0 */
652 if (transdata != NULL)
653 transdata[stateno * nbatoms + atomno] =
654 trans->atom->data;
Daniel Veillard23e73572002-09-19 19:56:43 +0000655 }
656 }
657 }
658 ret->determinist = 1;
659#ifdef DEBUG_COMPACTION
660 /*
661 * Debug
662 */
663 for (i = 0;i < nbstates;i++) {
664 for (j = 0;j < nbatoms + 1;j++) {
665 printf("%02d ", transitions[i * (nbatoms + 1) + j]);
666 }
667 printf("\n");
668 }
669 printf("\n");
670#endif
671 /*
672 * Cleanup of the old data
673 */
674 if (ret->states != NULL) {
675 for (i = 0;i < ret->nbStates;i++)
676 xmlRegFreeState(ret->states[i]);
677 xmlFree(ret->states);
678 }
679 ret->states = NULL;
680 ret->nbStates = 0;
681 if (ret->atoms != NULL) {
682 for (i = 0;i < ret->nbAtoms;i++)
683 xmlRegFreeAtom(ret->atoms[i]);
684 xmlFree(ret->atoms);
685 }
686 ret->atoms = NULL;
687 ret->nbAtoms = 0;
688
689 ret->compact = transitions;
Daniel Veillard118aed72002-09-24 14:13:13 +0000690 ret->transdata = transdata;
Daniel Veillard23e73572002-09-19 19:56:43 +0000691 ret->stringMap = stringMap;
692 ret->nbstrings = nbatoms;
693 ret->nbstates = nbstates;
694 xmlFree(stateRemap);
695 xmlFree(stringRemap);
696 }
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000697not_determ:
698 ctxt->string = NULL;
699 ctxt->nbStates = 0;
700 ctxt->states = NULL;
701 ctxt->nbAtoms = 0;
702 ctxt->atoms = NULL;
703 ctxt->nbCounters = 0;
704 ctxt->counters = NULL;
Daniel Veillard4255d502002-04-16 15:50:10 +0000705 return(ret);
706}
707
708/**
709 * xmlRegNewParserCtxt:
710 * @string: the string to parse
711 *
712 * Allocate a new regexp parser context
713 *
714 * Returns the new context or NULL in case of error
715 */
716static xmlRegParserCtxtPtr
717xmlRegNewParserCtxt(const xmlChar *string) {
718 xmlRegParserCtxtPtr ret;
719
720 ret = (xmlRegParserCtxtPtr) xmlMalloc(sizeof(xmlRegParserCtxt));
721 if (ret == NULL)
722 return(NULL);
723 memset(ret, 0, sizeof(xmlRegParserCtxt));
724 if (string != NULL)
725 ret->string = xmlStrdup(string);
726 ret->cur = ret->string;
727 ret->neg = 0;
Daniel Veillard6e65e152005-08-09 11:09:52 +0000728 ret->negs = 0;
Daniel Veillard4255d502002-04-16 15:50:10 +0000729 ret->error = 0;
Daniel Veillarde19fc232002-04-22 16:01:24 +0000730 ret->determinist = -1;
Daniel Veillard4255d502002-04-16 15:50:10 +0000731 return(ret);
732}
733
734/**
735 * xmlRegNewRange:
736 * @ctxt: the regexp parser context
737 * @neg: is that negative
738 * @type: the type of range
739 * @start: the start codepoint
740 * @end: the end codepoint
741 *
742 * Allocate a new regexp range
743 *
744 * Returns the new range or NULL in case of error
745 */
746static xmlRegRangePtr
747xmlRegNewRange(xmlRegParserCtxtPtr ctxt,
748 int neg, xmlRegAtomType type, int start, int end) {
749 xmlRegRangePtr ret;
750
751 ret = (xmlRegRangePtr) xmlMalloc(sizeof(xmlRegRange));
752 if (ret == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +0000753 xmlRegexpErrMemory(ctxt, "allocating range");
Daniel Veillard4255d502002-04-16 15:50:10 +0000754 return(NULL);
755 }
756 ret->neg = neg;
757 ret->type = type;
758 ret->start = start;
759 ret->end = end;
760 return(ret);
761}
762
763/**
764 * xmlRegFreeRange:
765 * @range: the regexp range
766 *
767 * Free a regexp range
768 */
769static void
770xmlRegFreeRange(xmlRegRangePtr range) {
771 if (range == NULL)
772 return;
773
774 if (range->blockName != NULL)
775 xmlFree(range->blockName);
776 xmlFree(range);
777}
778
779/**
Daniel Veillard76d59b62007-08-22 16:29:21 +0000780 * xmlRegCopyRange:
781 * @range: the regexp range
782 *
783 * Copy a regexp range
784 *
785 * Returns the new copy or NULL in case of error.
786 */
787static xmlRegRangePtr
788xmlRegCopyRange(xmlRegParserCtxtPtr ctxt, xmlRegRangePtr range) {
789 xmlRegRangePtr ret;
790
791 if (range == NULL)
792 return(NULL);
793
794 ret = xmlRegNewRange(ctxt, range->neg, range->type, range->start,
795 range->end);
796 if (ret == NULL)
797 return(NULL);
798 if (range->blockName != NULL) {
799 ret->blockName = xmlStrdup(range->blockName);
800 if (ret->blockName == NULL) {
801 xmlRegexpErrMemory(ctxt, "allocating range");
802 xmlRegFreeRange(ret);
803 return(NULL);
804 }
805 }
806 return(ret);
807}
808
809/**
Daniel Veillard4255d502002-04-16 15:50:10 +0000810 * xmlRegNewAtom:
811 * @ctxt: the regexp parser context
812 * @type: the type of atom
813 *
Daniel Veillard76d59b62007-08-22 16:29:21 +0000814 * Allocate a new atom
Daniel Veillard4255d502002-04-16 15:50:10 +0000815 *
816 * Returns the new atom or NULL in case of error
817 */
818static xmlRegAtomPtr
819xmlRegNewAtom(xmlRegParserCtxtPtr ctxt, xmlRegAtomType type) {
820 xmlRegAtomPtr ret;
821
822 ret = (xmlRegAtomPtr) xmlMalloc(sizeof(xmlRegAtom));
823 if (ret == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +0000824 xmlRegexpErrMemory(ctxt, "allocating atom");
Daniel Veillard4255d502002-04-16 15:50:10 +0000825 return(NULL);
826 }
827 memset(ret, 0, sizeof(xmlRegAtom));
828 ret->type = type;
829 ret->quant = XML_REGEXP_QUANT_ONCE;
830 ret->min = 0;
831 ret->max = 0;
832 return(ret);
833}
834
835/**
836 * xmlRegFreeAtom:
837 * @atom: the regexp atom
838 *
839 * Free a regexp atom
840 */
841static void
842xmlRegFreeAtom(xmlRegAtomPtr atom) {
843 int i;
844
845 if (atom == NULL)
846 return;
847
848 for (i = 0;i < atom->nbRanges;i++)
849 xmlRegFreeRange(atom->ranges[i]);
850 if (atom->ranges != NULL)
851 xmlFree(atom->ranges);
Daniel Veillardde0e4982005-07-03 14:35:44 +0000852 if ((atom->type == XML_REGEXP_STRING) && (atom->valuep != NULL))
853 xmlFree(atom->valuep);
Daniel Veillard77005e62005-07-19 16:26:18 +0000854 if ((atom->type == XML_REGEXP_STRING) && (atom->valuep2 != NULL))
855 xmlFree(atom->valuep2);
Daniel Veillardde0e4982005-07-03 14:35:44 +0000856 if ((atom->type == XML_REGEXP_BLOCK_NAME) && (atom->valuep != NULL))
Daniel Veillard4255d502002-04-16 15:50:10 +0000857 xmlFree(atom->valuep);
858 xmlFree(atom);
859}
860
Daniel Veillard76d59b62007-08-22 16:29:21 +0000861/**
862 * xmlRegCopyAtom:
863 * @ctxt: the regexp parser context
Haibo Huangcfd91dc2020-07-30 23:01:33 -0700864 * @atom: the original atom
Daniel Veillard76d59b62007-08-22 16:29:21 +0000865 *
866 * Allocate a new regexp range
867 *
868 * Returns the new atom or NULL in case of error
869 */
870static xmlRegAtomPtr
871xmlRegCopyAtom(xmlRegParserCtxtPtr ctxt, xmlRegAtomPtr atom) {
872 xmlRegAtomPtr ret;
873
874 ret = (xmlRegAtomPtr) xmlMalloc(sizeof(xmlRegAtom));
875 if (ret == NULL) {
876 xmlRegexpErrMemory(ctxt, "copying atom");
877 return(NULL);
878 }
879 memset(ret, 0, sizeof(xmlRegAtom));
880 ret->type = atom->type;
881 ret->quant = atom->quant;
882 ret->min = atom->min;
883 ret->max = atom->max;
884 if (atom->nbRanges > 0) {
885 int i;
886
887 ret->ranges = (xmlRegRangePtr *) xmlMalloc(sizeof(xmlRegRangePtr) *
888 atom->nbRanges);
889 if (ret->ranges == NULL) {
890 xmlRegexpErrMemory(ctxt, "copying atom");
891 goto error;
892 }
893 for (i = 0;i < atom->nbRanges;i++) {
894 ret->ranges[i] = xmlRegCopyRange(ctxt, atom->ranges[i]);
895 if (ret->ranges[i] == NULL)
896 goto error;
897 ret->nbRanges = i + 1;
898 }
899 }
900 return(ret);
901
902error:
903 xmlRegFreeAtom(ret);
904 return(NULL);
905}
906
Daniel Veillard4255d502002-04-16 15:50:10 +0000907static xmlRegStatePtr
908xmlRegNewState(xmlRegParserCtxtPtr ctxt) {
909 xmlRegStatePtr ret;
910
911 ret = (xmlRegStatePtr) xmlMalloc(sizeof(xmlRegState));
912 if (ret == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +0000913 xmlRegexpErrMemory(ctxt, "allocating state");
Daniel Veillard4255d502002-04-16 15:50:10 +0000914 return(NULL);
915 }
916 memset(ret, 0, sizeof(xmlRegState));
917 ret->type = XML_REGEXP_TRANS_STATE;
918 ret->mark = XML_REGEXP_MARK_NORMAL;
919 return(ret);
920}
921
922/**
923 * xmlRegFreeState:
924 * @state: the regexp state
925 *
926 * Free a regexp state
927 */
928static void
929xmlRegFreeState(xmlRegStatePtr state) {
930 if (state == NULL)
931 return;
932
933 if (state->trans != NULL)
934 xmlFree(state->trans);
Daniel Veillarddb68b742005-07-30 13:18:24 +0000935 if (state->transTo != NULL)
936 xmlFree(state->transTo);
Daniel Veillard4255d502002-04-16 15:50:10 +0000937 xmlFree(state);
938}
939
940/**
941 * xmlRegFreeParserCtxt:
942 * @ctxt: the regexp parser context
943 *
944 * Free a regexp parser context
945 */
946static void
947xmlRegFreeParserCtxt(xmlRegParserCtxtPtr ctxt) {
948 int i;
949 if (ctxt == NULL)
950 return;
951
952 if (ctxt->string != NULL)
953 xmlFree(ctxt->string);
954 if (ctxt->states != NULL) {
955 for (i = 0;i < ctxt->nbStates;i++)
956 xmlRegFreeState(ctxt->states[i]);
957 xmlFree(ctxt->states);
958 }
959 if (ctxt->atoms != NULL) {
960 for (i = 0;i < ctxt->nbAtoms;i++)
961 xmlRegFreeAtom(ctxt->atoms[i]);
962 xmlFree(ctxt->atoms);
963 }
964 if (ctxt->counters != NULL)
965 xmlFree(ctxt->counters);
966 xmlFree(ctxt);
967}
968
969/************************************************************************
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800970 * *
971 * Display of Data structures *
972 * *
Daniel Veillard4255d502002-04-16 15:50:10 +0000973 ************************************************************************/
974
975static void
976xmlRegPrintAtomType(FILE *output, xmlRegAtomType type) {
977 switch (type) {
978 case XML_REGEXP_EPSILON:
979 fprintf(output, "epsilon "); break;
980 case XML_REGEXP_CHARVAL:
981 fprintf(output, "charval "); break;
982 case XML_REGEXP_RANGES:
983 fprintf(output, "ranges "); break;
984 case XML_REGEXP_SUBREG:
985 fprintf(output, "subexpr "); break;
986 case XML_REGEXP_STRING:
987 fprintf(output, "string "); break;
988 case XML_REGEXP_ANYCHAR:
989 fprintf(output, "anychar "); break;
990 case XML_REGEXP_ANYSPACE:
991 fprintf(output, "anyspace "); break;
992 case XML_REGEXP_NOTSPACE:
993 fprintf(output, "notspace "); break;
994 case XML_REGEXP_INITNAME:
995 fprintf(output, "initname "); break;
996 case XML_REGEXP_NOTINITNAME:
997 fprintf(output, "notinitname "); break;
998 case XML_REGEXP_NAMECHAR:
999 fprintf(output, "namechar "); break;
1000 case XML_REGEXP_NOTNAMECHAR:
1001 fprintf(output, "notnamechar "); break;
1002 case XML_REGEXP_DECIMAL:
1003 fprintf(output, "decimal "); break;
1004 case XML_REGEXP_NOTDECIMAL:
1005 fprintf(output, "notdecimal "); break;
1006 case XML_REGEXP_REALCHAR:
1007 fprintf(output, "realchar "); break;
1008 case XML_REGEXP_NOTREALCHAR:
1009 fprintf(output, "notrealchar "); break;
1010 case XML_REGEXP_LETTER:
1011 fprintf(output, "LETTER "); break;
1012 case XML_REGEXP_LETTER_UPPERCASE:
1013 fprintf(output, "LETTER_UPPERCASE "); break;
1014 case XML_REGEXP_LETTER_LOWERCASE:
1015 fprintf(output, "LETTER_LOWERCASE "); break;
1016 case XML_REGEXP_LETTER_TITLECASE:
1017 fprintf(output, "LETTER_TITLECASE "); break;
1018 case XML_REGEXP_LETTER_MODIFIER:
1019 fprintf(output, "LETTER_MODIFIER "); break;
1020 case XML_REGEXP_LETTER_OTHERS:
1021 fprintf(output, "LETTER_OTHERS "); break;
1022 case XML_REGEXP_MARK:
1023 fprintf(output, "MARK "); break;
1024 case XML_REGEXP_MARK_NONSPACING:
1025 fprintf(output, "MARK_NONSPACING "); break;
1026 case XML_REGEXP_MARK_SPACECOMBINING:
1027 fprintf(output, "MARK_SPACECOMBINING "); break;
1028 case XML_REGEXP_MARK_ENCLOSING:
1029 fprintf(output, "MARK_ENCLOSING "); break;
1030 case XML_REGEXP_NUMBER:
1031 fprintf(output, "NUMBER "); break;
1032 case XML_REGEXP_NUMBER_DECIMAL:
1033 fprintf(output, "NUMBER_DECIMAL "); break;
1034 case XML_REGEXP_NUMBER_LETTER:
1035 fprintf(output, "NUMBER_LETTER "); break;
1036 case XML_REGEXP_NUMBER_OTHERS:
1037 fprintf(output, "NUMBER_OTHERS "); break;
1038 case XML_REGEXP_PUNCT:
1039 fprintf(output, "PUNCT "); break;
1040 case XML_REGEXP_PUNCT_CONNECTOR:
1041 fprintf(output, "PUNCT_CONNECTOR "); break;
1042 case XML_REGEXP_PUNCT_DASH:
1043 fprintf(output, "PUNCT_DASH "); break;
1044 case XML_REGEXP_PUNCT_OPEN:
1045 fprintf(output, "PUNCT_OPEN "); break;
1046 case XML_REGEXP_PUNCT_CLOSE:
1047 fprintf(output, "PUNCT_CLOSE "); break;
1048 case XML_REGEXP_PUNCT_INITQUOTE:
1049 fprintf(output, "PUNCT_INITQUOTE "); break;
1050 case XML_REGEXP_PUNCT_FINQUOTE:
1051 fprintf(output, "PUNCT_FINQUOTE "); break;
1052 case XML_REGEXP_PUNCT_OTHERS:
1053 fprintf(output, "PUNCT_OTHERS "); break;
1054 case XML_REGEXP_SEPAR:
1055 fprintf(output, "SEPAR "); break;
1056 case XML_REGEXP_SEPAR_SPACE:
1057 fprintf(output, "SEPAR_SPACE "); break;
1058 case XML_REGEXP_SEPAR_LINE:
1059 fprintf(output, "SEPAR_LINE "); break;
1060 case XML_REGEXP_SEPAR_PARA:
1061 fprintf(output, "SEPAR_PARA "); break;
1062 case XML_REGEXP_SYMBOL:
1063 fprintf(output, "SYMBOL "); break;
1064 case XML_REGEXP_SYMBOL_MATH:
1065 fprintf(output, "SYMBOL_MATH "); break;
1066 case XML_REGEXP_SYMBOL_CURRENCY:
1067 fprintf(output, "SYMBOL_CURRENCY "); break;
1068 case XML_REGEXP_SYMBOL_MODIFIER:
1069 fprintf(output, "SYMBOL_MODIFIER "); break;
1070 case XML_REGEXP_SYMBOL_OTHERS:
1071 fprintf(output, "SYMBOL_OTHERS "); break;
1072 case XML_REGEXP_OTHER:
1073 fprintf(output, "OTHER "); break;
1074 case XML_REGEXP_OTHER_CONTROL:
1075 fprintf(output, "OTHER_CONTROL "); break;
1076 case XML_REGEXP_OTHER_FORMAT:
1077 fprintf(output, "OTHER_FORMAT "); break;
1078 case XML_REGEXP_OTHER_PRIVATE:
1079 fprintf(output, "OTHER_PRIVATE "); break;
1080 case XML_REGEXP_OTHER_NA:
1081 fprintf(output, "OTHER_NA "); break;
1082 case XML_REGEXP_BLOCK_NAME:
1083 fprintf(output, "BLOCK "); break;
1084 }
1085}
1086
1087static void
1088xmlRegPrintQuantType(FILE *output, xmlRegQuantType type) {
1089 switch (type) {
1090 case XML_REGEXP_QUANT_EPSILON:
1091 fprintf(output, "epsilon "); break;
1092 case XML_REGEXP_QUANT_ONCE:
1093 fprintf(output, "once "); break;
1094 case XML_REGEXP_QUANT_OPT:
1095 fprintf(output, "? "); break;
1096 case XML_REGEXP_QUANT_MULT:
1097 fprintf(output, "* "); break;
1098 case XML_REGEXP_QUANT_PLUS:
1099 fprintf(output, "+ "); break;
1100 case XML_REGEXP_QUANT_RANGE:
1101 fprintf(output, "range "); break;
Daniel Veillard7646b182002-04-20 06:41:40 +00001102 case XML_REGEXP_QUANT_ONCEONLY:
1103 fprintf(output, "onceonly "); break;
1104 case XML_REGEXP_QUANT_ALL:
1105 fprintf(output, "all "); break;
Daniel Veillard4255d502002-04-16 15:50:10 +00001106 }
1107}
1108static void
1109xmlRegPrintRange(FILE *output, xmlRegRangePtr range) {
1110 fprintf(output, " range: ");
1111 if (range->neg)
1112 fprintf(output, "negative ");
1113 xmlRegPrintAtomType(output, range->type);
1114 fprintf(output, "%c - %c\n", range->start, range->end);
1115}
1116
1117static void
1118xmlRegPrintAtom(FILE *output, xmlRegAtomPtr atom) {
1119 fprintf(output, " atom: ");
1120 if (atom == NULL) {
1121 fprintf(output, "NULL\n");
1122 return;
1123 }
Daniel Veillard9efc4762005-07-19 14:33:55 +00001124 if (atom->neg)
1125 fprintf(output, "not ");
Daniel Veillard4255d502002-04-16 15:50:10 +00001126 xmlRegPrintAtomType(output, atom->type);
1127 xmlRegPrintQuantType(output, atom->quant);
1128 if (atom->quant == XML_REGEXP_QUANT_RANGE)
1129 fprintf(output, "%d-%d ", atom->min, atom->max);
1130 if (atom->type == XML_REGEXP_STRING)
1131 fprintf(output, "'%s' ", (char *) atom->valuep);
1132 if (atom->type == XML_REGEXP_CHARVAL)
1133 fprintf(output, "char %c\n", atom->codepoint);
1134 else if (atom->type == XML_REGEXP_RANGES) {
1135 int i;
1136 fprintf(output, "%d entries\n", atom->nbRanges);
1137 for (i = 0; i < atom->nbRanges;i++)
1138 xmlRegPrintRange(output, atom->ranges[i]);
1139 } else if (atom->type == XML_REGEXP_SUBREG) {
1140 fprintf(output, "start %d end %d\n", atom->start->no, atom->stop->no);
1141 } else {
1142 fprintf(output, "\n");
1143 }
1144}
1145
1146static void
1147xmlRegPrintTrans(FILE *output, xmlRegTransPtr trans) {
1148 fprintf(output, " trans: ");
1149 if (trans == NULL) {
1150 fprintf(output, "NULL\n");
1151 return;
1152 }
1153 if (trans->to < 0) {
1154 fprintf(output, "removed\n");
1155 return;
1156 }
Daniel Veillard567a45b2005-10-18 19:11:55 +00001157 if (trans->nd != 0) {
1158 if (trans->nd == 2)
1159 fprintf(output, "last not determinist, ");
1160 else
1161 fprintf(output, "not determinist, ");
1162 }
Daniel Veillard4255d502002-04-16 15:50:10 +00001163 if (trans->counter >= 0) {
1164 fprintf(output, "counted %d, ", trans->counter);
1165 }
Daniel Veillard8a001f62002-04-20 07:24:11 +00001166 if (trans->count == REGEXP_ALL_COUNTER) {
1167 fprintf(output, "all transition, ");
1168 } else if (trans->count >= 0) {
Daniel Veillard4255d502002-04-16 15:50:10 +00001169 fprintf(output, "count based %d, ", trans->count);
1170 }
1171 if (trans->atom == NULL) {
1172 fprintf(output, "epsilon to %d\n", trans->to);
1173 return;
1174 }
1175 if (trans->atom->type == XML_REGEXP_CHARVAL)
1176 fprintf(output, "char %c ", trans->atom->codepoint);
1177 fprintf(output, "atom %d, to %d\n", trans->atom->no, trans->to);
1178}
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001179
Daniel Veillard4255d502002-04-16 15:50:10 +00001180static void
1181xmlRegPrintState(FILE *output, xmlRegStatePtr state) {
1182 int i;
1183
1184 fprintf(output, " state: ");
1185 if (state == NULL) {
1186 fprintf(output, "NULL\n");
1187 return;
1188 }
1189 if (state->type == XML_REGEXP_START_STATE)
1190 fprintf(output, "START ");
1191 if (state->type == XML_REGEXP_FINAL_STATE)
1192 fprintf(output, "FINAL ");
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001193
Daniel Veillard4255d502002-04-16 15:50:10 +00001194 fprintf(output, "%d, %d transitions:\n", state->no, state->nbTrans);
1195 for (i = 0;i < state->nbTrans; i++) {
1196 xmlRegPrintTrans(output, &(state->trans[i]));
1197 }
1198}
1199
Daniel Veillard23e73572002-09-19 19:56:43 +00001200#ifdef DEBUG_REGEXP_GRAPH
Daniel Veillard4255d502002-04-16 15:50:10 +00001201static void
1202xmlRegPrintCtxt(FILE *output, xmlRegParserCtxtPtr ctxt) {
1203 int i;
1204
1205 fprintf(output, " ctxt: ");
1206 if (ctxt == NULL) {
1207 fprintf(output, "NULL\n");
1208 return;
1209 }
1210 fprintf(output, "'%s' ", ctxt->string);
1211 if (ctxt->error)
1212 fprintf(output, "error ");
1213 if (ctxt->neg)
1214 fprintf(output, "neg ");
1215 fprintf(output, "\n");
1216 fprintf(output, "%d atoms:\n", ctxt->nbAtoms);
1217 for (i = 0;i < ctxt->nbAtoms; i++) {
1218 fprintf(output, " %02d ", i);
1219 xmlRegPrintAtom(output, ctxt->atoms[i]);
1220 }
1221 if (ctxt->atom != NULL) {
1222 fprintf(output, "current atom:\n");
1223 xmlRegPrintAtom(output, ctxt->atom);
1224 }
1225 fprintf(output, "%d states:", ctxt->nbStates);
1226 if (ctxt->start != NULL)
1227 fprintf(output, " start: %d", ctxt->start->no);
1228 if (ctxt->end != NULL)
1229 fprintf(output, " end: %d", ctxt->end->no);
1230 fprintf(output, "\n");
1231 for (i = 0;i < ctxt->nbStates; i++) {
1232 xmlRegPrintState(output, ctxt->states[i]);
1233 }
1234 fprintf(output, "%d counters:\n", ctxt->nbCounters);
1235 for (i = 0;i < ctxt->nbCounters; i++) {
1236 fprintf(output, " %d: min %d max %d\n", i, ctxt->counters[i].min,
1237 ctxt->counters[i].max);
1238 }
1239}
Daniel Veillard23e73572002-09-19 19:56:43 +00001240#endif
Daniel Veillard4255d502002-04-16 15:50:10 +00001241
1242/************************************************************************
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001243 * *
Daniel Veillard4255d502002-04-16 15:50:10 +00001244 * Finite Automata structures manipulations *
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001245 * *
Daniel Veillard4255d502002-04-16 15:50:10 +00001246 ************************************************************************/
1247
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001248static void
Daniel Veillard4255d502002-04-16 15:50:10 +00001249xmlRegAtomAddRange(xmlRegParserCtxtPtr ctxt, xmlRegAtomPtr atom,
1250 int neg, xmlRegAtomType type, int start, int end,
1251 xmlChar *blockName) {
1252 xmlRegRangePtr range;
1253
1254 if (atom == NULL) {
1255 ERROR("add range: atom is NULL");
1256 return;
1257 }
1258 if (atom->type != XML_REGEXP_RANGES) {
1259 ERROR("add range: atom is not ranges");
1260 return;
1261 }
1262 if (atom->maxRanges == 0) {
1263 atom->maxRanges = 4;
1264 atom->ranges = (xmlRegRangePtr *) xmlMalloc(atom->maxRanges *
1265 sizeof(xmlRegRangePtr));
1266 if (atom->ranges == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00001267 xmlRegexpErrMemory(ctxt, "adding ranges");
Daniel Veillard4255d502002-04-16 15:50:10 +00001268 atom->maxRanges = 0;
1269 return;
1270 }
1271 } else if (atom->nbRanges >= atom->maxRanges) {
1272 xmlRegRangePtr *tmp;
1273 atom->maxRanges *= 2;
1274 tmp = (xmlRegRangePtr *) xmlRealloc(atom->ranges, atom->maxRanges *
1275 sizeof(xmlRegRangePtr));
1276 if (tmp == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00001277 xmlRegexpErrMemory(ctxt, "adding ranges");
Daniel Veillard4255d502002-04-16 15:50:10 +00001278 atom->maxRanges /= 2;
1279 return;
1280 }
1281 atom->ranges = tmp;
1282 }
1283 range = xmlRegNewRange(ctxt, neg, type, start, end);
1284 if (range == NULL)
1285 return;
1286 range->blockName = blockName;
1287 atom->ranges[atom->nbRanges++] = range;
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001288
Daniel Veillard4255d502002-04-16 15:50:10 +00001289}
1290
1291static int
1292xmlRegGetCounter(xmlRegParserCtxtPtr ctxt) {
1293 if (ctxt->maxCounters == 0) {
1294 ctxt->maxCounters = 4;
1295 ctxt->counters = (xmlRegCounter *) xmlMalloc(ctxt->maxCounters *
1296 sizeof(xmlRegCounter));
1297 if (ctxt->counters == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00001298 xmlRegexpErrMemory(ctxt, "allocating counter");
Daniel Veillard4255d502002-04-16 15:50:10 +00001299 ctxt->maxCounters = 0;
1300 return(-1);
1301 }
1302 } else if (ctxt->nbCounters >= ctxt->maxCounters) {
1303 xmlRegCounter *tmp;
1304 ctxt->maxCounters *= 2;
1305 tmp = (xmlRegCounter *) xmlRealloc(ctxt->counters, ctxt->maxCounters *
1306 sizeof(xmlRegCounter));
1307 if (tmp == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00001308 xmlRegexpErrMemory(ctxt, "allocating counter");
Daniel Veillard4255d502002-04-16 15:50:10 +00001309 ctxt->maxCounters /= 2;
1310 return(-1);
1311 }
1312 ctxt->counters = tmp;
1313 }
1314 ctxt->counters[ctxt->nbCounters].min = -1;
1315 ctxt->counters[ctxt->nbCounters].max = -1;
1316 return(ctxt->nbCounters++);
1317}
1318
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001319static int
Daniel Veillard4255d502002-04-16 15:50:10 +00001320xmlRegAtomPush(xmlRegParserCtxtPtr ctxt, xmlRegAtomPtr atom) {
1321 if (atom == NULL) {
1322 ERROR("atom push: atom is NULL");
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001323 return(-1);
Daniel Veillard4255d502002-04-16 15:50:10 +00001324 }
1325 if (ctxt->maxAtoms == 0) {
1326 ctxt->maxAtoms = 4;
1327 ctxt->atoms = (xmlRegAtomPtr *) xmlMalloc(ctxt->maxAtoms *
1328 sizeof(xmlRegAtomPtr));
1329 if (ctxt->atoms == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00001330 xmlRegexpErrMemory(ctxt, "pushing atom");
Daniel Veillard4255d502002-04-16 15:50:10 +00001331 ctxt->maxAtoms = 0;
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001332 return(-1);
Daniel Veillard4255d502002-04-16 15:50:10 +00001333 }
1334 } else if (ctxt->nbAtoms >= ctxt->maxAtoms) {
1335 xmlRegAtomPtr *tmp;
1336 ctxt->maxAtoms *= 2;
1337 tmp = (xmlRegAtomPtr *) xmlRealloc(ctxt->atoms, ctxt->maxAtoms *
1338 sizeof(xmlRegAtomPtr));
1339 if (tmp == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00001340 xmlRegexpErrMemory(ctxt, "allocating counter");
Daniel Veillard4255d502002-04-16 15:50:10 +00001341 ctxt->maxAtoms /= 2;
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001342 return(-1);
Daniel Veillard4255d502002-04-16 15:50:10 +00001343 }
1344 ctxt->atoms = tmp;
1345 }
1346 atom->no = ctxt->nbAtoms;
1347 ctxt->atoms[ctxt->nbAtoms++] = atom;
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001348 return(0);
Daniel Veillard4255d502002-04-16 15:50:10 +00001349}
1350
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001351static void
Daniel Veillarddb68b742005-07-30 13:18:24 +00001352xmlRegStateAddTransTo(xmlRegParserCtxtPtr ctxt, xmlRegStatePtr target,
1353 int from) {
1354 if (target->maxTransTo == 0) {
1355 target->maxTransTo = 8;
1356 target->transTo = (int *) xmlMalloc(target->maxTransTo *
1357 sizeof(int));
1358 if (target->transTo == NULL) {
1359 xmlRegexpErrMemory(ctxt, "adding transition");
1360 target->maxTransTo = 0;
1361 return;
1362 }
1363 } else if (target->nbTransTo >= target->maxTransTo) {
1364 int *tmp;
1365 target->maxTransTo *= 2;
1366 tmp = (int *) xmlRealloc(target->transTo, target->maxTransTo *
1367 sizeof(int));
1368 if (tmp == NULL) {
1369 xmlRegexpErrMemory(ctxt, "adding transition");
1370 target->maxTransTo /= 2;
1371 return;
1372 }
1373 target->transTo = tmp;
1374 }
1375 target->transTo[target->nbTransTo] = from;
1376 target->nbTransTo++;
1377}
1378
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001379static void
Daniel Veillard4255d502002-04-16 15:50:10 +00001380xmlRegStateAddTrans(xmlRegParserCtxtPtr ctxt, xmlRegStatePtr state,
1381 xmlRegAtomPtr atom, xmlRegStatePtr target,
Daniel Veillard5de09382005-09-26 17:18:17 +00001382 int counter, int count) {
William M. Brackf9b5fa22004-05-10 07:52:15 +00001383
1384 int nrtrans;
1385
Daniel Veillard4255d502002-04-16 15:50:10 +00001386 if (state == NULL) {
1387 ERROR("add state: state is NULL");
1388 return;
1389 }
1390 if (target == NULL) {
1391 ERROR("add state: target is NULL");
1392 return;
1393 }
William M. Brackf9b5fa22004-05-10 07:52:15 +00001394 /*
1395 * Other routines follow the philosophy 'When in doubt, add a transition'
1396 * so we check here whether such a transition is already present and, if
1397 * so, silently ignore this request.
1398 */
1399
Daniel Veillard5de09382005-09-26 17:18:17 +00001400 for (nrtrans = state->nbTrans - 1; nrtrans >= 0; nrtrans--) {
1401 xmlRegTransPtr trans = &(state->trans[nrtrans]);
1402 if ((trans->atom == atom) &&
1403 (trans->to == target->no) &&
1404 (trans->counter == counter) &&
1405 (trans->count == count)) {
William M. Brackf9b5fa22004-05-10 07:52:15 +00001406#ifdef DEBUG_REGEXP_GRAPH
Daniel Veillard5de09382005-09-26 17:18:17 +00001407 printf("Ignoring duplicate transition from %d to %d\n",
1408 state->no, target->no);
William M. Brackf9b5fa22004-05-10 07:52:15 +00001409#endif
Daniel Veillard5de09382005-09-26 17:18:17 +00001410 return;
Daniel Veillarddb68b742005-07-30 13:18:24 +00001411 }
William M. Brackf9b5fa22004-05-10 07:52:15 +00001412 }
1413
Daniel Veillard4255d502002-04-16 15:50:10 +00001414 if (state->maxTrans == 0) {
Daniel Veillarddb68b742005-07-30 13:18:24 +00001415 state->maxTrans = 8;
Daniel Veillard4255d502002-04-16 15:50:10 +00001416 state->trans = (xmlRegTrans *) xmlMalloc(state->maxTrans *
1417 sizeof(xmlRegTrans));
1418 if (state->trans == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00001419 xmlRegexpErrMemory(ctxt, "adding transition");
Daniel Veillard4255d502002-04-16 15:50:10 +00001420 state->maxTrans = 0;
1421 return;
1422 }
1423 } else if (state->nbTrans >= state->maxTrans) {
1424 xmlRegTrans *tmp;
1425 state->maxTrans *= 2;
1426 tmp = (xmlRegTrans *) xmlRealloc(state->trans, state->maxTrans *
1427 sizeof(xmlRegTrans));
1428 if (tmp == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00001429 xmlRegexpErrMemory(ctxt, "adding transition");
Daniel Veillard4255d502002-04-16 15:50:10 +00001430 state->maxTrans /= 2;
1431 return;
1432 }
1433 state->trans = tmp;
1434 }
1435#ifdef DEBUG_REGEXP_GRAPH
1436 printf("Add trans from %d to %d ", state->no, target->no);
Daniel Veillard8a001f62002-04-20 07:24:11 +00001437 if (count == REGEXP_ALL_COUNTER)
Daniel Veillard2cbf5962004-03-31 15:50:43 +00001438 printf("all transition\n");
Daniel Veillard4402ab42002-09-12 16:02:56 +00001439 else if (count >= 0)
Daniel Veillard2cbf5962004-03-31 15:50:43 +00001440 printf("count based %d\n", count);
Daniel Veillard4255d502002-04-16 15:50:10 +00001441 else if (counter >= 0)
Daniel Veillard2cbf5962004-03-31 15:50:43 +00001442 printf("counted %d\n", counter);
Daniel Veillard4255d502002-04-16 15:50:10 +00001443 else if (atom == NULL)
Daniel Veillard2cbf5962004-03-31 15:50:43 +00001444 printf("epsilon transition\n");
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001445 else if (atom != NULL)
Daniel Veillard2cbf5962004-03-31 15:50:43 +00001446 xmlRegPrintAtom(stdout, atom);
Daniel Veillard4255d502002-04-16 15:50:10 +00001447#endif
1448
1449 state->trans[state->nbTrans].atom = atom;
1450 state->trans[state->nbTrans].to = target->no;
1451 state->trans[state->nbTrans].counter = counter;
1452 state->trans[state->nbTrans].count = count;
Daniel Veillard567a45b2005-10-18 19:11:55 +00001453 state->trans[state->nbTrans].nd = 0;
Daniel Veillard4255d502002-04-16 15:50:10 +00001454 state->nbTrans++;
Daniel Veillarddb68b742005-07-30 13:18:24 +00001455 xmlRegStateAddTransTo(ctxt, target, state->no);
Daniel Veillard4255d502002-04-16 15:50:10 +00001456}
1457
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001458static int
Daniel Veillard4255d502002-04-16 15:50:10 +00001459xmlRegStatePush(xmlRegParserCtxtPtr ctxt, xmlRegStatePtr state) {
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001460 if (state == NULL) return(-1);
Daniel Veillard4255d502002-04-16 15:50:10 +00001461 if (ctxt->maxStates == 0) {
1462 ctxt->maxStates = 4;
1463 ctxt->states = (xmlRegStatePtr *) xmlMalloc(ctxt->maxStates *
1464 sizeof(xmlRegStatePtr));
1465 if (ctxt->states == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00001466 xmlRegexpErrMemory(ctxt, "adding state");
Daniel Veillard4255d502002-04-16 15:50:10 +00001467 ctxt->maxStates = 0;
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001468 return(-1);
Daniel Veillard4255d502002-04-16 15:50:10 +00001469 }
1470 } else if (ctxt->nbStates >= ctxt->maxStates) {
1471 xmlRegStatePtr *tmp;
1472 ctxt->maxStates *= 2;
1473 tmp = (xmlRegStatePtr *) xmlRealloc(ctxt->states, ctxt->maxStates *
1474 sizeof(xmlRegStatePtr));
1475 if (tmp == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00001476 xmlRegexpErrMemory(ctxt, "adding state");
Daniel Veillard4255d502002-04-16 15:50:10 +00001477 ctxt->maxStates /= 2;
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001478 return(-1);
Daniel Veillard4255d502002-04-16 15:50:10 +00001479 }
1480 ctxt->states = tmp;
1481 }
1482 state->no = ctxt->nbStates;
1483 ctxt->states[ctxt->nbStates++] = state;
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001484 return(0);
Daniel Veillard4255d502002-04-16 15:50:10 +00001485}
1486
1487/**
Daniel Veillard7646b182002-04-20 06:41:40 +00001488 * xmlFAGenerateAllTransition:
Daniel Veillard441bc322002-04-20 17:38:48 +00001489 * @ctxt: a regexp parser context
1490 * @from: the from state
1491 * @to: the target state or NULL for building a new one
1492 * @lax:
Daniel Veillard7646b182002-04-20 06:41:40 +00001493 *
1494 */
1495static void
1496xmlFAGenerateAllTransition(xmlRegParserCtxtPtr ctxt,
Daniel Veillard441bc322002-04-20 17:38:48 +00001497 xmlRegStatePtr from, xmlRegStatePtr to,
1498 int lax) {
Daniel Veillard7646b182002-04-20 06:41:40 +00001499 if (to == NULL) {
1500 to = xmlRegNewState(ctxt);
1501 xmlRegStatePush(ctxt, to);
1502 ctxt->state = to;
1503 }
Daniel Veillard441bc322002-04-20 17:38:48 +00001504 if (lax)
Daniel Veillard5de09382005-09-26 17:18:17 +00001505 xmlRegStateAddTrans(ctxt, from, NULL, to, -1, REGEXP_ALL_LAX_COUNTER);
Daniel Veillard441bc322002-04-20 17:38:48 +00001506 else
Daniel Veillard5de09382005-09-26 17:18:17 +00001507 xmlRegStateAddTrans(ctxt, from, NULL, to, -1, REGEXP_ALL_COUNTER);
Daniel Veillard7646b182002-04-20 06:41:40 +00001508}
1509
1510/**
Daniel Veillard4255d502002-04-16 15:50:10 +00001511 * xmlFAGenerateEpsilonTransition:
Daniel Veillard441bc322002-04-20 17:38:48 +00001512 * @ctxt: a regexp parser context
1513 * @from: the from state
1514 * @to: the target state or NULL for building a new one
Daniel Veillard4255d502002-04-16 15:50:10 +00001515 *
1516 */
1517static void
1518xmlFAGenerateEpsilonTransition(xmlRegParserCtxtPtr ctxt,
1519 xmlRegStatePtr from, xmlRegStatePtr to) {
1520 if (to == NULL) {
1521 to = xmlRegNewState(ctxt);
1522 xmlRegStatePush(ctxt, to);
1523 ctxt->state = to;
1524 }
Daniel Veillard5de09382005-09-26 17:18:17 +00001525 xmlRegStateAddTrans(ctxt, from, NULL, to, -1, -1);
Daniel Veillard4255d502002-04-16 15:50:10 +00001526}
1527
1528/**
1529 * xmlFAGenerateCountedEpsilonTransition:
Daniel Veillard441bc322002-04-20 17:38:48 +00001530 * @ctxt: a regexp parser context
1531 * @from: the from state
1532 * @to: the target state or NULL for building a new one
Daniel Veillard4255d502002-04-16 15:50:10 +00001533 * counter: the counter for that transition
1534 *
1535 */
1536static void
1537xmlFAGenerateCountedEpsilonTransition(xmlRegParserCtxtPtr ctxt,
1538 xmlRegStatePtr from, xmlRegStatePtr to, int counter) {
1539 if (to == NULL) {
1540 to = xmlRegNewState(ctxt);
1541 xmlRegStatePush(ctxt, to);
1542 ctxt->state = to;
1543 }
Daniel Veillard5de09382005-09-26 17:18:17 +00001544 xmlRegStateAddTrans(ctxt, from, NULL, to, counter, -1);
Daniel Veillard4255d502002-04-16 15:50:10 +00001545}
1546
1547/**
1548 * xmlFAGenerateCountedTransition:
Daniel Veillard441bc322002-04-20 17:38:48 +00001549 * @ctxt: a regexp parser context
1550 * @from: the from state
1551 * @to: the target state or NULL for building a new one
Daniel Veillard4255d502002-04-16 15:50:10 +00001552 * counter: the counter for that transition
1553 *
1554 */
1555static void
1556xmlFAGenerateCountedTransition(xmlRegParserCtxtPtr ctxt,
1557 xmlRegStatePtr from, xmlRegStatePtr to, int counter) {
1558 if (to == NULL) {
1559 to = xmlRegNewState(ctxt);
1560 xmlRegStatePush(ctxt, to);
1561 ctxt->state = to;
1562 }
Daniel Veillard5de09382005-09-26 17:18:17 +00001563 xmlRegStateAddTrans(ctxt, from, NULL, to, -1, counter);
Daniel Veillard4255d502002-04-16 15:50:10 +00001564}
1565
1566/**
1567 * xmlFAGenerateTransitions:
Daniel Veillard441bc322002-04-20 17:38:48 +00001568 * @ctxt: a regexp parser context
1569 * @from: the from state
1570 * @to: the target state or NULL for building a new one
1571 * @atom: the atom generating the transition
Daniel Veillard4255d502002-04-16 15:50:10 +00001572 *
William M. Brackddf71d62004-05-06 04:17:26 +00001573 * Returns 0 if success and -1 in case of error.
Daniel Veillard4255d502002-04-16 15:50:10 +00001574 */
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001575static int
Daniel Veillard4255d502002-04-16 15:50:10 +00001576xmlFAGenerateTransitions(xmlRegParserCtxtPtr ctxt, xmlRegStatePtr from,
1577 xmlRegStatePtr to, xmlRegAtomPtr atom) {
Daniel Veillard10bda622008-03-13 07:27:24 +00001578 xmlRegStatePtr end;
Daniel Veillard34b35002016-05-09 09:28:38 +08001579 int nullable = 0;
Daniel Veillard10bda622008-03-13 07:27:24 +00001580
Daniel Veillard4255d502002-04-16 15:50:10 +00001581 if (atom == NULL) {
Haibo Huangcfd91dc2020-07-30 23:01:33 -07001582 ERROR("generate transition: atom == NULL");
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001583 return(-1);
Daniel Veillard4255d502002-04-16 15:50:10 +00001584 }
1585 if (atom->type == XML_REGEXP_SUBREG) {
1586 /*
1587 * this is a subexpression handling one should not need to
William M. Brackddf71d62004-05-06 04:17:26 +00001588 * create a new node except for XML_REGEXP_QUANT_RANGE.
Daniel Veillard4255d502002-04-16 15:50:10 +00001589 */
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001590 if (xmlRegAtomPush(ctxt, atom) < 0) {
1591 return(-1);
1592 }
Daniel Veillard4255d502002-04-16 15:50:10 +00001593 if ((to != NULL) && (atom->stop != to) &&
1594 (atom->quant != XML_REGEXP_QUANT_RANGE)) {
1595 /*
1596 * Generate an epsilon transition to link to the target
1597 */
1598 xmlFAGenerateEpsilonTransition(ctxt, atom->stop, to);
Daniel Veillardaa622012005-10-20 15:55:25 +00001599#ifdef DV
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001600 } else if ((to == NULL) && (atom->quant != XML_REGEXP_QUANT_RANGE) &&
Daniel Veillardaa622012005-10-20 15:55:25 +00001601 (atom->quant != XML_REGEXP_QUANT_ONCE)) {
1602 to = xmlRegNewState(ctxt);
1603 xmlRegStatePush(ctxt, to);
1604 ctxt->state = to;
1605 xmlFAGenerateEpsilonTransition(ctxt, atom->stop, to);
1606#endif
Daniel Veillard4255d502002-04-16 15:50:10 +00001607 }
1608 switch (atom->quant) {
1609 case XML_REGEXP_QUANT_OPT:
1610 atom->quant = XML_REGEXP_QUANT_ONCE;
Daniel Veillard54eb0242006-03-21 23:17:57 +00001611 /*
1612 * transition done to the state after end of atom.
1613 * 1. set transition from atom start to new state
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001614 * 2. set transition from atom end to this state.
Daniel Veillard54eb0242006-03-21 23:17:57 +00001615 */
Daniel Veillardd80d0722009-08-22 18:56:01 +02001616 if (to == NULL) {
1617 xmlFAGenerateEpsilonTransition(ctxt, atom->start, 0);
1618 xmlFAGenerateEpsilonTransition(ctxt, atom->stop,
1619 ctxt->state);
1620 } else {
1621 xmlFAGenerateEpsilonTransition(ctxt, atom->start, to);
1622 }
Daniel Veillard4255d502002-04-16 15:50:10 +00001623 break;
1624 case XML_REGEXP_QUANT_MULT:
1625 atom->quant = XML_REGEXP_QUANT_ONCE;
1626 xmlFAGenerateEpsilonTransition(ctxt, atom->start, atom->stop);
1627 xmlFAGenerateEpsilonTransition(ctxt, atom->stop, atom->start);
1628 break;
1629 case XML_REGEXP_QUANT_PLUS:
1630 atom->quant = XML_REGEXP_QUANT_ONCE;
1631 xmlFAGenerateEpsilonTransition(ctxt, atom->stop, atom->start);
1632 break;
1633 case XML_REGEXP_QUANT_RANGE: {
1634 int counter;
Daniel Veillard76d59b62007-08-22 16:29:21 +00001635 xmlRegStatePtr inter, newstate;
Daniel Veillard4255d502002-04-16 15:50:10 +00001636
1637 /*
Daniel Veillard76d59b62007-08-22 16:29:21 +00001638 * create the final state now if needed
Daniel Veillard4255d502002-04-16 15:50:10 +00001639 */
Daniel Veillard4255d502002-04-16 15:50:10 +00001640 if (to != NULL) {
1641 newstate = to;
1642 } else {
1643 newstate = xmlRegNewState(ctxt);
1644 xmlRegStatePush(ctxt, newstate);
Daniel Veillard4255d502002-04-16 15:50:10 +00001645 }
Daniel Veillard76d59b62007-08-22 16:29:21 +00001646
1647 /*
1648 * The principle here is to use counted transition
1649 * to avoid explosion in the number of states in the
1650 * graph. This is clearly more complex but should not
1651 * be exploitable at runtime.
Daniel Veillard54eb0242006-03-21 23:17:57 +00001652 */
Daniel Veillard76d59b62007-08-22 16:29:21 +00001653 if ((atom->min == 0) && (atom->start0 == NULL)) {
1654 xmlRegAtomPtr copy;
1655 /*
1656 * duplicate a transition based on atom to count next
Haibo Huangcfd91dc2020-07-30 23:01:33 -07001657 * occurrences after 1. We cannot loop to atom->start
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001658 * directly because we need an epsilon transition to
Daniel Veillard76d59b62007-08-22 16:29:21 +00001659 * newstate.
1660 */
1661 /* ???? For some reason it seems we never reach that
1662 case, I suppose this got optimized out before when
1663 building the automata */
Daniel Veillardc821e032007-08-28 17:33:45 +00001664 copy = xmlRegCopyAtom(ctxt, atom);
Daniel Veillard76d59b62007-08-22 16:29:21 +00001665 if (copy == NULL)
1666 return(-1);
Daniel Veillard76d59b62007-08-22 16:29:21 +00001667 copy->quant = XML_REGEXP_QUANT_ONCE;
1668 copy->min = 0;
1669 copy->max = 0;
1670
1671 if (xmlFAGenerateTransitions(ctxt, atom->start, NULL, copy)
1672 < 0)
1673 return(-1);
1674 inter = ctxt->state;
1675 counter = xmlRegGetCounter(ctxt);
1676 ctxt->counters[counter].min = atom->min - 1;
1677 ctxt->counters[counter].max = atom->max - 1;
1678 /* count the number of times we see it again */
1679 xmlFAGenerateCountedEpsilonTransition(ctxt, inter,
1680 atom->stop, counter);
1681 /* allow a way out based on the count */
1682 xmlFAGenerateCountedTransition(ctxt, inter,
1683 newstate, counter);
1684 /* and also allow a direct exit for 0 */
1685 xmlFAGenerateEpsilonTransition(ctxt, atom->start,
1686 newstate);
1687 } else {
1688 /*
1689 * either we need the atom at least once or there
Haibo Huangcfd91dc2020-07-30 23:01:33 -07001690 * is an atom->start0 allowing to easily plug the
Daniel Veillard76d59b62007-08-22 16:29:21 +00001691 * epsilon transition.
1692 */
1693 counter = xmlRegGetCounter(ctxt);
1694 ctxt->counters[counter].min = atom->min - 1;
1695 ctxt->counters[counter].max = atom->max - 1;
1696 /* count the number of times we see it again */
1697 xmlFAGenerateCountedEpsilonTransition(ctxt, atom->stop,
1698 atom->start, counter);
1699 /* allow a way out based on the count */
1700 xmlFAGenerateCountedTransition(ctxt, atom->stop,
1701 newstate, counter);
1702 /* and if needed allow a direct exit for 0 */
1703 if (atom->min == 0)
1704 xmlFAGenerateEpsilonTransition(ctxt, atom->start0,
1705 newstate);
1706
1707 }
1708 atom->min = 0;
1709 atom->max = 0;
1710 atom->quant = XML_REGEXP_QUANT_ONCE;
1711 ctxt->state = newstate;
Daniel Veillard4255d502002-04-16 15:50:10 +00001712 }
1713 default:
1714 break;
1715 }
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001716 return(0);
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001717 }
Daniel Veillard567a45b2005-10-18 19:11:55 +00001718 if ((atom->min == 0) && (atom->max == 0) &&
Daniel Veillard99c394d2005-07-14 12:58:49 +00001719 (atom->quant == XML_REGEXP_QUANT_RANGE)) {
1720 /*
1721 * we can discard the atom and generate an epsilon transition instead
1722 */
1723 if (to == NULL) {
1724 to = xmlRegNewState(ctxt);
1725 if (to != NULL)
1726 xmlRegStatePush(ctxt, to);
1727 else {
1728 return(-1);
1729 }
1730 }
1731 xmlFAGenerateEpsilonTransition(ctxt, from, to);
1732 ctxt->state = to;
1733 xmlRegFreeAtom(atom);
1734 return(0);
Daniel Veillard567a45b2005-10-18 19:11:55 +00001735 }
1736 if (to == NULL) {
1737 to = xmlRegNewState(ctxt);
1738 if (to != NULL)
1739 xmlRegStatePush(ctxt, to);
1740 else {
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001741 return(-1);
Daniel Veillard4255d502002-04-16 15:50:10 +00001742 }
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001743 }
Daniel Veillard10bda622008-03-13 07:27:24 +00001744 end = to;
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001745 if ((atom->quant == XML_REGEXP_QUANT_MULT) ||
Daniel Veillard10bda622008-03-13 07:27:24 +00001746 (atom->quant == XML_REGEXP_QUANT_PLUS)) {
1747 /*
1748 * Do not pollute the target state by adding transitions from
1749 * it as it is likely to be the shared target of multiple branches.
1750 * So isolate with an epsilon transition.
1751 */
1752 xmlRegStatePtr tmp;
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001753
Daniel Veillard10bda622008-03-13 07:27:24 +00001754 tmp = xmlRegNewState(ctxt);
1755 if (tmp != NULL)
1756 xmlRegStatePush(ctxt, tmp);
1757 else {
1758 return(-1);
1759 }
1760 xmlFAGenerateEpsilonTransition(ctxt, tmp, to);
1761 to = tmp;
Daniel Veillard4255d502002-04-16 15:50:10 +00001762 }
Daniel Veillard567a45b2005-10-18 19:11:55 +00001763 if (xmlRegAtomPush(ctxt, atom) < 0) {
1764 return(-1);
1765 }
Daniel Veillard34b35002016-05-09 09:28:38 +08001766 if ((atom->quant == XML_REGEXP_QUANT_RANGE) &&
1767 (atom->min == 0) && (atom->max > 0)) {
1768 nullable = 1;
1769 atom->min = 1;
1770 if (atom->max == 1)
1771 atom->quant = XML_REGEXP_QUANT_OPT;
1772 }
Daniel Veillard567a45b2005-10-18 19:11:55 +00001773 xmlRegStateAddTrans(ctxt, from, atom, to, -1, -1);
Daniel Veillard10bda622008-03-13 07:27:24 +00001774 ctxt->state = end;
Daniel Veillard4255d502002-04-16 15:50:10 +00001775 switch (atom->quant) {
1776 case XML_REGEXP_QUANT_OPT:
1777 atom->quant = XML_REGEXP_QUANT_ONCE;
1778 xmlFAGenerateEpsilonTransition(ctxt, from, to);
1779 break;
1780 case XML_REGEXP_QUANT_MULT:
1781 atom->quant = XML_REGEXP_QUANT_ONCE;
1782 xmlFAGenerateEpsilonTransition(ctxt, from, to);
Daniel Veillard5de09382005-09-26 17:18:17 +00001783 xmlRegStateAddTrans(ctxt, to, atom, to, -1, -1);
Daniel Veillard4255d502002-04-16 15:50:10 +00001784 break;
1785 case XML_REGEXP_QUANT_PLUS:
1786 atom->quant = XML_REGEXP_QUANT_ONCE;
Daniel Veillard5de09382005-09-26 17:18:17 +00001787 xmlRegStateAddTrans(ctxt, to, atom, to, -1, -1);
Daniel Veillard4255d502002-04-16 15:50:10 +00001788 break;
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001789 case XML_REGEXP_QUANT_RANGE:
Daniel Veillard34b35002016-05-09 09:28:38 +08001790 if (nullable)
William M. Brack56578372007-04-11 14:33:46 +00001791 xmlFAGenerateEpsilonTransition(ctxt, from, to);
William M. Brack56578372007-04-11 14:33:46 +00001792 break;
Daniel Veillard4255d502002-04-16 15:50:10 +00001793 default:
1794 break;
1795 }
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001796 return(0);
Daniel Veillard4255d502002-04-16 15:50:10 +00001797}
1798
1799/**
1800 * xmlFAReduceEpsilonTransitions:
Daniel Veillard441bc322002-04-20 17:38:48 +00001801 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00001802 * @fromnr: the from state
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001803 * @tonr: the to state
William M. Brackddf71d62004-05-06 04:17:26 +00001804 * @counter: should that transition be associated to a counted
Daniel Veillard4255d502002-04-16 15:50:10 +00001805 *
1806 */
1807static void
1808xmlFAReduceEpsilonTransitions(xmlRegParserCtxtPtr ctxt, int fromnr,
1809 int tonr, int counter) {
1810 int transnr;
1811 xmlRegStatePtr from;
1812 xmlRegStatePtr to;
1813
1814#ifdef DEBUG_REGEXP_GRAPH
1815 printf("xmlFAReduceEpsilonTransitions(%d, %d)\n", fromnr, tonr);
1816#endif
1817 from = ctxt->states[fromnr];
1818 if (from == NULL)
1819 return;
1820 to = ctxt->states[tonr];
1821 if (to == NULL)
1822 return;
1823 if ((to->mark == XML_REGEXP_MARK_START) ||
1824 (to->mark == XML_REGEXP_MARK_VISITED))
1825 return;
1826
1827 to->mark = XML_REGEXP_MARK_VISITED;
1828 if (to->type == XML_REGEXP_FINAL_STATE) {
1829#ifdef DEBUG_REGEXP_GRAPH
1830 printf("State %d is final, so %d becomes final\n", tonr, fromnr);
1831#endif
1832 from->type = XML_REGEXP_FINAL_STATE;
1833 }
1834 for (transnr = 0;transnr < to->nbTrans;transnr++) {
Daniel Veillarddb68b742005-07-30 13:18:24 +00001835 if (to->trans[transnr].to < 0)
1836 continue;
Daniel Veillard4255d502002-04-16 15:50:10 +00001837 if (to->trans[transnr].atom == NULL) {
1838 /*
1839 * Don't remove counted transitions
1840 * Don't loop either
1841 */
Daniel Veillardb509f152002-04-17 16:28:10 +00001842 if (to->trans[transnr].to != fromnr) {
1843 if (to->trans[transnr].count >= 0) {
1844 int newto = to->trans[transnr].to;
1845
1846 xmlRegStateAddTrans(ctxt, from, NULL,
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001847 ctxt->states[newto],
Daniel Veillard5de09382005-09-26 17:18:17 +00001848 -1, to->trans[transnr].count);
Daniel Veillardb509f152002-04-17 16:28:10 +00001849 } else {
Daniel Veillard4255d502002-04-16 15:50:10 +00001850#ifdef DEBUG_REGEXP_GRAPH
Daniel Veillardb509f152002-04-17 16:28:10 +00001851 printf("Found epsilon trans %d from %d to %d\n",
1852 transnr, tonr, to->trans[transnr].to);
Daniel Veillard4255d502002-04-16 15:50:10 +00001853#endif
Daniel Veillardb509f152002-04-17 16:28:10 +00001854 if (to->trans[transnr].counter >= 0) {
1855 xmlFAReduceEpsilonTransitions(ctxt, fromnr,
1856 to->trans[transnr].to,
1857 to->trans[transnr].counter);
1858 } else {
1859 xmlFAReduceEpsilonTransitions(ctxt, fromnr,
1860 to->trans[transnr].to,
1861 counter);
1862 }
1863 }
Daniel Veillard4255d502002-04-16 15:50:10 +00001864 }
1865 } else {
1866 int newto = to->trans[transnr].to;
1867
Daniel Veillardb509f152002-04-17 16:28:10 +00001868 if (to->trans[transnr].counter >= 0) {
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001869 xmlRegStateAddTrans(ctxt, from, to->trans[transnr].atom,
1870 ctxt->states[newto],
Daniel Veillard5de09382005-09-26 17:18:17 +00001871 to->trans[transnr].counter, -1);
Daniel Veillardb509f152002-04-17 16:28:10 +00001872 } else {
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001873 xmlRegStateAddTrans(ctxt, from, to->trans[transnr].atom,
Daniel Veillard5de09382005-09-26 17:18:17 +00001874 ctxt->states[newto], counter, -1);
Daniel Veillardb509f152002-04-17 16:28:10 +00001875 }
Daniel Veillard4255d502002-04-16 15:50:10 +00001876 }
1877 }
1878 to->mark = XML_REGEXP_MARK_NORMAL;
1879}
1880
1881/**
Daniel Veillarddb68b742005-07-30 13:18:24 +00001882 * xmlFAEliminateSimpleEpsilonTransitions:
1883 * @ctxt: a regexp parser context
1884 *
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001885 * Eliminating general epsilon transitions can get costly in the general
Daniel Veillarddb68b742005-07-30 13:18:24 +00001886 * algorithm due to the large amount of generated new transitions and
1887 * associated comparisons. However for simple epsilon transition used just
1888 * to separate building blocks when generating the automata this can be
1889 * reduced to state elimination:
1890 * - if there exists an epsilon from X to Y
1891 * - if there is no other transition from X
1892 * then X and Y are semantically equivalent and X can be eliminated
1893 * If X is the start state then make Y the start state, else replace the
1894 * target of all transitions to X by transitions to Y.
1895 */
1896static void
1897xmlFAEliminateSimpleEpsilonTransitions(xmlRegParserCtxtPtr ctxt) {
1898 int statenr, i, j, newto;
1899 xmlRegStatePtr state, tmp;
1900
1901 for (statenr = 0;statenr < ctxt->nbStates;statenr++) {
1902 state = ctxt->states[statenr];
1903 if (state == NULL)
1904 continue;
1905 if (state->nbTrans != 1)
1906 continue;
Daniel Veillard0e05f4c2006-11-01 15:33:04 +00001907 if (state->type == XML_REGEXP_UNREACH_STATE)
1908 continue;
Daniel Veillarddb68b742005-07-30 13:18:24 +00001909 /* is the only transition out a basic transition */
1910 if ((state->trans[0].atom == NULL) &&
1911 (state->trans[0].to >= 0) &&
1912 (state->trans[0].to != statenr) &&
1913 (state->trans[0].counter < 0) &&
1914 (state->trans[0].count < 0)) {
1915 newto = state->trans[0].to;
1916
1917 if (state->type == XML_REGEXP_START_STATE) {
1918#ifdef DEBUG_REGEXP_GRAPH
1919 printf("Found simple epsilon trans from start %d to %d\n",
1920 statenr, newto);
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001921#endif
Daniel Veillarddb68b742005-07-30 13:18:24 +00001922 } else {
1923#ifdef DEBUG_REGEXP_GRAPH
1924 printf("Found simple epsilon trans from %d to %d\n",
1925 statenr, newto);
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001926#endif
Daniel Veillarddb68b742005-07-30 13:18:24 +00001927 for (i = 0;i < state->nbTransTo;i++) {
1928 tmp = ctxt->states[state->transTo[i]];
1929 for (j = 0;j < tmp->nbTrans;j++) {
1930 if (tmp->trans[j].to == statenr) {
Daniel Veillarddb68b742005-07-30 13:18:24 +00001931#ifdef DEBUG_REGEXP_GRAPH
1932 printf("Changed transition %d on %d to go to %d\n",
1933 j, tmp->no, newto);
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001934#endif
Daniel Veillard0e05f4c2006-11-01 15:33:04 +00001935 tmp->trans[j].to = -1;
1936 xmlRegStateAddTrans(ctxt, tmp, tmp->trans[j].atom,
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001937 ctxt->states[newto],
Daniel Veillard0e05f4c2006-11-01 15:33:04 +00001938 tmp->trans[j].counter,
1939 tmp->trans[j].count);
Daniel Veillarddb68b742005-07-30 13:18:24 +00001940 }
1941 }
1942 }
Daniel Veillarddb68b742005-07-30 13:18:24 +00001943 if (state->type == XML_REGEXP_FINAL_STATE)
1944 ctxt->states[newto]->type = XML_REGEXP_FINAL_STATE;
1945 /* eliminate the transition completely */
1946 state->nbTrans = 0;
1947
Daniel Veillard0e05f4c2006-11-01 15:33:04 +00001948 state->type = XML_REGEXP_UNREACH_STATE;
Daniel Veillarddb68b742005-07-30 13:18:24 +00001949
1950 }
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001951
Daniel Veillarddb68b742005-07-30 13:18:24 +00001952 }
1953 }
1954}
1955/**
Daniel Veillard4255d502002-04-16 15:50:10 +00001956 * xmlFAEliminateEpsilonTransitions:
Daniel Veillard441bc322002-04-20 17:38:48 +00001957 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00001958 *
1959 */
1960static void
1961xmlFAEliminateEpsilonTransitions(xmlRegParserCtxtPtr ctxt) {
1962 int statenr, transnr;
1963 xmlRegStatePtr state;
Daniel Veillarddb68b742005-07-30 13:18:24 +00001964 int has_epsilon;
Daniel Veillard4255d502002-04-16 15:50:10 +00001965
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001966 if (ctxt->states == NULL) return;
1967
Daniel Veillard0e05f4c2006-11-01 15:33:04 +00001968 /*
1969 * Eliminate simple epsilon transition and the associated unreachable
1970 * states.
1971 */
Daniel Veillarddb68b742005-07-30 13:18:24 +00001972 xmlFAEliminateSimpleEpsilonTransitions(ctxt);
Daniel Veillard0e05f4c2006-11-01 15:33:04 +00001973 for (statenr = 0;statenr < ctxt->nbStates;statenr++) {
1974 state = ctxt->states[statenr];
1975 if ((state != NULL) && (state->type == XML_REGEXP_UNREACH_STATE)) {
1976#ifdef DEBUG_REGEXP_GRAPH
1977 printf("Removed unreachable state %d\n", statenr);
1978#endif
1979 xmlRegFreeState(state);
1980 ctxt->states[statenr] = NULL;
1981 }
1982 }
Daniel Veillarddb68b742005-07-30 13:18:24 +00001983
1984 has_epsilon = 0;
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001985
Daniel Veillard4255d502002-04-16 15:50:10 +00001986 /*
Daniel Veillardfcd18ff2006-11-02 10:28:04 +00001987 * Build the completed transitions bypassing the epsilons
Daniel Veillard4255d502002-04-16 15:50:10 +00001988 * Use a marking algorithm to avoid loops
Daniel Veillardfcd18ff2006-11-02 10:28:04 +00001989 * Mark sink states too.
Haibo Huangcfd91dc2020-07-30 23:01:33 -07001990 * Process from the latest states backward to the start when
Daniel Veillardfcd18ff2006-11-02 10:28:04 +00001991 * there is long cascading epsilon chains this minimize the
1992 * recursions and transition compares when adding the new ones
Daniel Veillard4255d502002-04-16 15:50:10 +00001993 */
Daniel Veillardfcd18ff2006-11-02 10:28:04 +00001994 for (statenr = ctxt->nbStates - 1;statenr >= 0;statenr--) {
Daniel Veillard4255d502002-04-16 15:50:10 +00001995 state = ctxt->states[statenr];
1996 if (state == NULL)
1997 continue;
Daniel Veillardcc026dc2005-01-12 13:21:17 +00001998 if ((state->nbTrans == 0) &&
1999 (state->type != XML_REGEXP_FINAL_STATE)) {
2000 state->type = XML_REGEXP_SINK_STATE;
2001 }
Daniel Veillard4255d502002-04-16 15:50:10 +00002002 for (transnr = 0;transnr < state->nbTrans;transnr++) {
2003 if ((state->trans[transnr].atom == NULL) &&
2004 (state->trans[transnr].to >= 0)) {
2005 if (state->trans[transnr].to == statenr) {
2006 state->trans[transnr].to = -1;
2007#ifdef DEBUG_REGEXP_GRAPH
2008 printf("Removed loopback epsilon trans %d on %d\n",
2009 transnr, statenr);
2010#endif
2011 } else if (state->trans[transnr].count < 0) {
2012 int newto = state->trans[transnr].to;
2013
2014#ifdef DEBUG_REGEXP_GRAPH
2015 printf("Found epsilon trans %d from %d to %d\n",
2016 transnr, statenr, newto);
2017#endif
Daniel Veillarddb68b742005-07-30 13:18:24 +00002018 has_epsilon = 1;
Daniel Veillard0e05f4c2006-11-01 15:33:04 +00002019 state->trans[transnr].to = -2;
2020 state->mark = XML_REGEXP_MARK_START;
Daniel Veillard4255d502002-04-16 15:50:10 +00002021 xmlFAReduceEpsilonTransitions(ctxt, statenr,
2022 newto, state->trans[transnr].counter);
2023 state->mark = XML_REGEXP_MARK_NORMAL;
2024#ifdef DEBUG_REGEXP_GRAPH
2025 } else {
2026 printf("Found counted transition %d on %d\n",
2027 transnr, statenr);
2028#endif
2029 }
2030 }
2031 }
2032 }
2033 /*
2034 * Eliminate the epsilon transitions
2035 */
Daniel Veillarddb68b742005-07-30 13:18:24 +00002036 if (has_epsilon) {
2037 for (statenr = 0;statenr < ctxt->nbStates;statenr++) {
2038 state = ctxt->states[statenr];
2039 if (state == NULL)
2040 continue;
2041 for (transnr = 0;transnr < state->nbTrans;transnr++) {
2042 xmlRegTransPtr trans = &(state->trans[transnr]);
2043 if ((trans->atom == NULL) &&
2044 (trans->count < 0) &&
2045 (trans->to >= 0)) {
2046 trans->to = -1;
2047 }
Daniel Veillard4255d502002-04-16 15:50:10 +00002048 }
2049 }
2050 }
Daniel Veillard23e73572002-09-19 19:56:43 +00002051
2052 /*
2053 * Use this pass to detect unreachable states too
2054 */
2055 for (statenr = 0;statenr < ctxt->nbStates;statenr++) {
2056 state = ctxt->states[statenr];
2057 if (state != NULL)
William M. Brack779af002003-08-01 15:55:39 +00002058 state->reached = XML_REGEXP_MARK_NORMAL;
Daniel Veillard23e73572002-09-19 19:56:43 +00002059 }
2060 state = ctxt->states[0];
2061 if (state != NULL)
William M. Brack779af002003-08-01 15:55:39 +00002062 state->reached = XML_REGEXP_MARK_START;
Daniel Veillard23e73572002-09-19 19:56:43 +00002063 while (state != NULL) {
2064 xmlRegStatePtr target = NULL;
William M. Brack779af002003-08-01 15:55:39 +00002065 state->reached = XML_REGEXP_MARK_VISITED;
Daniel Veillard23e73572002-09-19 19:56:43 +00002066 /*
William M. Brackddf71d62004-05-06 04:17:26 +00002067 * Mark all states reachable from the current reachable state
Daniel Veillard23e73572002-09-19 19:56:43 +00002068 */
2069 for (transnr = 0;transnr < state->nbTrans;transnr++) {
2070 if ((state->trans[transnr].to >= 0) &&
2071 ((state->trans[transnr].atom != NULL) ||
2072 (state->trans[transnr].count >= 0))) {
2073 int newto = state->trans[transnr].to;
2074
2075 if (ctxt->states[newto] == NULL)
2076 continue;
William M. Brack779af002003-08-01 15:55:39 +00002077 if (ctxt->states[newto]->reached == XML_REGEXP_MARK_NORMAL) {
2078 ctxt->states[newto]->reached = XML_REGEXP_MARK_START;
Daniel Veillard23e73572002-09-19 19:56:43 +00002079 target = ctxt->states[newto];
2080 }
2081 }
2082 }
Daniel Veillardcc026dc2005-01-12 13:21:17 +00002083
Daniel Veillard23e73572002-09-19 19:56:43 +00002084 /*
2085 * find the next accessible state not explored
2086 */
2087 if (target == NULL) {
2088 for (statenr = 1;statenr < ctxt->nbStates;statenr++) {
2089 state = ctxt->states[statenr];
William M. Brack779af002003-08-01 15:55:39 +00002090 if ((state != NULL) && (state->reached ==
2091 XML_REGEXP_MARK_START)) {
Daniel Veillard23e73572002-09-19 19:56:43 +00002092 target = state;
2093 break;
2094 }
2095 }
2096 }
2097 state = target;
2098 }
2099 for (statenr = 0;statenr < ctxt->nbStates;statenr++) {
2100 state = ctxt->states[statenr];
William M. Brack779af002003-08-01 15:55:39 +00002101 if ((state != NULL) && (state->reached == XML_REGEXP_MARK_NORMAL)) {
Daniel Veillard23e73572002-09-19 19:56:43 +00002102#ifdef DEBUG_REGEXP_GRAPH
2103 printf("Removed unreachable state %d\n", statenr);
2104#endif
2105 xmlRegFreeState(state);
2106 ctxt->states[statenr] = NULL;
2107 }
2108 }
2109
Daniel Veillard4255d502002-04-16 15:50:10 +00002110}
2111
Daniel Veillard567a45b2005-10-18 19:11:55 +00002112static int
2113xmlFACompareRanges(xmlRegRangePtr range1, xmlRegRangePtr range2) {
2114 int ret = 0;
2115
2116 if ((range1->type == XML_REGEXP_RANGES) ||
2117 (range2->type == XML_REGEXP_RANGES) ||
2118 (range2->type == XML_REGEXP_SUBREG) ||
2119 (range1->type == XML_REGEXP_SUBREG) ||
2120 (range1->type == XML_REGEXP_STRING) ||
2121 (range2->type == XML_REGEXP_STRING))
2122 return(-1);
2123
2124 /* put them in order */
2125 if (range1->type > range2->type) {
2126 xmlRegRangePtr tmp;
2127
2128 tmp = range1;
2129 range1 = range2;
2130 range2 = tmp;
2131 }
2132 if ((range1->type == XML_REGEXP_ANYCHAR) ||
2133 (range2->type == XML_REGEXP_ANYCHAR)) {
2134 ret = 1;
2135 } else if ((range1->type == XML_REGEXP_EPSILON) ||
2136 (range2->type == XML_REGEXP_EPSILON)) {
2137 return(0);
2138 } else if (range1->type == range2->type) {
Daniel Veillard9332b482009-09-23 18:28:43 +02002139 if (range1->type != XML_REGEXP_CHARVAL)
2140 ret = 1;
2141 else if ((range1->end < range2->start) ||
2142 (range2->end < range1->start))
Daniel Veillard567a45b2005-10-18 19:11:55 +00002143 ret = 0;
Daniel Veillard9332b482009-09-23 18:28:43 +02002144 else
2145 ret = 1;
Daniel Veillard567a45b2005-10-18 19:11:55 +00002146 } else if (range1->type == XML_REGEXP_CHARVAL) {
2147 int codepoint;
2148 int neg = 0;
2149
2150 /*
2151 * just check all codepoints in the range for acceptance,
2152 * this is usually way cheaper since done only once at
Daniel Veillardf8e3db02012-09-11 13:26:36 +08002153 * compilation than testing over and over at runtime or
Daniel Veillard567a45b2005-10-18 19:11:55 +00002154 * pushing too many states when evaluating.
2155 */
2156 if (((range1->neg == 0) && (range2->neg != 0)) ||
2157 ((range1->neg != 0) && (range2->neg == 0)))
2158 neg = 1;
2159
2160 for (codepoint = range1->start;codepoint <= range1->end ;codepoint++) {
2161 ret = xmlRegCheckCharacterRange(range2->type, codepoint,
2162 0, range2->start, range2->end,
2163 range2->blockName);
2164 if (ret < 0)
2165 return(-1);
2166 if (((neg == 1) && (ret == 0)) ||
2167 ((neg == 0) && (ret == 1)))
2168 return(1);
2169 }
2170 return(0);
2171 } else if ((range1->type == XML_REGEXP_BLOCK_NAME) ||
2172 (range2->type == XML_REGEXP_BLOCK_NAME)) {
2173 if (range1->type == range2->type) {
2174 ret = xmlStrEqual(range1->blockName, range2->blockName);
2175 } else {
2176 /*
2177 * comparing a block range with anything else is way
Haibo Huangcfd91dc2020-07-30 23:01:33 -07002178 * too costly, and maintaining the table is like too much
Daniel Veillard567a45b2005-10-18 19:11:55 +00002179 * memory too, so let's force the automata to save state
2180 * here.
2181 */
2182 return(1);
2183 }
2184 } else if ((range1->type < XML_REGEXP_LETTER) ||
2185 (range2->type < XML_REGEXP_LETTER)) {
2186 if ((range1->type == XML_REGEXP_ANYSPACE) &&
2187 (range2->type == XML_REGEXP_NOTSPACE))
2188 ret = 0;
2189 else if ((range1->type == XML_REGEXP_INITNAME) &&
2190 (range2->type == XML_REGEXP_NOTINITNAME))
2191 ret = 0;
2192 else if ((range1->type == XML_REGEXP_NAMECHAR) &&
2193 (range2->type == XML_REGEXP_NOTNAMECHAR))
2194 ret = 0;
2195 else if ((range1->type == XML_REGEXP_DECIMAL) &&
2196 (range2->type == XML_REGEXP_NOTDECIMAL))
2197 ret = 0;
2198 else if ((range1->type == XML_REGEXP_REALCHAR) &&
2199 (range2->type == XML_REGEXP_NOTREALCHAR))
2200 ret = 0;
2201 else {
2202 /* same thing to limit complexity */
2203 return(1);
2204 }
2205 } else {
2206 ret = 0;
2207 /* range1->type < range2->type here */
2208 switch (range1->type) {
2209 case XML_REGEXP_LETTER:
2210 /* all disjoint except in the subgroups */
2211 if ((range2->type == XML_REGEXP_LETTER_UPPERCASE) ||
2212 (range2->type == XML_REGEXP_LETTER_LOWERCASE) ||
2213 (range2->type == XML_REGEXP_LETTER_TITLECASE) ||
2214 (range2->type == XML_REGEXP_LETTER_MODIFIER) ||
2215 (range2->type == XML_REGEXP_LETTER_OTHERS))
2216 ret = 1;
2217 break;
2218 case XML_REGEXP_MARK:
2219 if ((range2->type == XML_REGEXP_MARK_NONSPACING) ||
2220 (range2->type == XML_REGEXP_MARK_SPACECOMBINING) ||
2221 (range2->type == XML_REGEXP_MARK_ENCLOSING))
2222 ret = 1;
2223 break;
2224 case XML_REGEXP_NUMBER:
2225 if ((range2->type == XML_REGEXP_NUMBER_DECIMAL) ||
2226 (range2->type == XML_REGEXP_NUMBER_LETTER) ||
2227 (range2->type == XML_REGEXP_NUMBER_OTHERS))
2228 ret = 1;
2229 break;
2230 case XML_REGEXP_PUNCT:
2231 if ((range2->type == XML_REGEXP_PUNCT_CONNECTOR) ||
2232 (range2->type == XML_REGEXP_PUNCT_DASH) ||
2233 (range2->type == XML_REGEXP_PUNCT_OPEN) ||
2234 (range2->type == XML_REGEXP_PUNCT_CLOSE) ||
2235 (range2->type == XML_REGEXP_PUNCT_INITQUOTE) ||
2236 (range2->type == XML_REGEXP_PUNCT_FINQUOTE) ||
2237 (range2->type == XML_REGEXP_PUNCT_OTHERS))
2238 ret = 1;
2239 break;
2240 case XML_REGEXP_SEPAR:
2241 if ((range2->type == XML_REGEXP_SEPAR_SPACE) ||
2242 (range2->type == XML_REGEXP_SEPAR_LINE) ||
2243 (range2->type == XML_REGEXP_SEPAR_PARA))
2244 ret = 1;
2245 break;
2246 case XML_REGEXP_SYMBOL:
2247 if ((range2->type == XML_REGEXP_SYMBOL_MATH) ||
2248 (range2->type == XML_REGEXP_SYMBOL_CURRENCY) ||
2249 (range2->type == XML_REGEXP_SYMBOL_MODIFIER) ||
2250 (range2->type == XML_REGEXP_SYMBOL_OTHERS))
2251 ret = 1;
2252 break;
2253 case XML_REGEXP_OTHER:
2254 if ((range2->type == XML_REGEXP_OTHER_CONTROL) ||
2255 (range2->type == XML_REGEXP_OTHER_FORMAT) ||
2256 (range2->type == XML_REGEXP_OTHER_PRIVATE))
2257 ret = 1;
2258 break;
2259 default:
2260 if ((range2->type >= XML_REGEXP_LETTER) &&
2261 (range2->type < XML_REGEXP_BLOCK_NAME))
2262 ret = 0;
2263 else {
2264 /* safety net ! */
2265 return(1);
2266 }
2267 }
2268 }
2269 if (((range1->neg == 0) && (range2->neg != 0)) ||
2270 ((range1->neg != 0) && (range2->neg == 0)))
2271 ret = !ret;
Daniel Veillard594e5df2009-09-07 14:58:47 +02002272 return(ret);
Daniel Veillard567a45b2005-10-18 19:11:55 +00002273}
2274
Daniel Veillarde19fc232002-04-22 16:01:24 +00002275/**
Daniel Veillardfc011b72006-02-12 19:14:15 +00002276 * xmlFACompareAtomTypes:
2277 * @type1: an atom type
2278 * @type2: an atom type
2279 *
2280 * Compares two atoms type to check whether they intersect in some ways,
2281 * this is used by xmlFACompareAtoms only
2282 *
2283 * Returns 1 if they may intersect and 0 otherwise
2284 */
2285static int
2286xmlFACompareAtomTypes(xmlRegAtomType type1, xmlRegAtomType type2) {
2287 if ((type1 == XML_REGEXP_EPSILON) ||
2288 (type1 == XML_REGEXP_CHARVAL) ||
2289 (type1 == XML_REGEXP_RANGES) ||
2290 (type1 == XML_REGEXP_SUBREG) ||
2291 (type1 == XML_REGEXP_STRING) ||
2292 (type1 == XML_REGEXP_ANYCHAR))
2293 return(1);
2294 if ((type2 == XML_REGEXP_EPSILON) ||
2295 (type2 == XML_REGEXP_CHARVAL) ||
2296 (type2 == XML_REGEXP_RANGES) ||
2297 (type2 == XML_REGEXP_SUBREG) ||
2298 (type2 == XML_REGEXP_STRING) ||
2299 (type2 == XML_REGEXP_ANYCHAR))
2300 return(1);
2301
2302 if (type1 == type2) return(1);
2303
2304 /* simplify subsequent compares by making sure type1 < type2 */
2305 if (type1 > type2) {
2306 xmlRegAtomType tmp = type1;
2307 type1 = type2;
2308 type2 = tmp;
2309 }
2310 switch (type1) {
2311 case XML_REGEXP_ANYSPACE: /* \s */
Haibo Huangcfd91dc2020-07-30 23:01:33 -07002312 /* can't be a letter, number, mark, punctuation, symbol */
Daniel Veillardfc011b72006-02-12 19:14:15 +00002313 if ((type2 == XML_REGEXP_NOTSPACE) ||
2314 ((type2 >= XML_REGEXP_LETTER) &&
2315 (type2 <= XML_REGEXP_LETTER_OTHERS)) ||
2316 ((type2 >= XML_REGEXP_NUMBER) &&
2317 (type2 <= XML_REGEXP_NUMBER_OTHERS)) ||
2318 ((type2 >= XML_REGEXP_MARK) &&
2319 (type2 <= XML_REGEXP_MARK_ENCLOSING)) ||
2320 ((type2 >= XML_REGEXP_PUNCT) &&
2321 (type2 <= XML_REGEXP_PUNCT_OTHERS)) ||
2322 ((type2 >= XML_REGEXP_SYMBOL) &&
2323 (type2 <= XML_REGEXP_SYMBOL_OTHERS))
2324 ) return(0);
2325 break;
2326 case XML_REGEXP_NOTSPACE: /* \S */
2327 break;
2328 case XML_REGEXP_INITNAME: /* \l */
Haibo Huangcfd91dc2020-07-30 23:01:33 -07002329 /* can't be a number, mark, separator, punctuation, symbol or other */
Daniel Veillardfc011b72006-02-12 19:14:15 +00002330 if ((type2 == XML_REGEXP_NOTINITNAME) ||
2331 ((type2 >= XML_REGEXP_NUMBER) &&
2332 (type2 <= XML_REGEXP_NUMBER_OTHERS)) ||
2333 ((type2 >= XML_REGEXP_MARK) &&
2334 (type2 <= XML_REGEXP_MARK_ENCLOSING)) ||
2335 ((type2 >= XML_REGEXP_SEPAR) &&
2336 (type2 <= XML_REGEXP_SEPAR_PARA)) ||
2337 ((type2 >= XML_REGEXP_PUNCT) &&
2338 (type2 <= XML_REGEXP_PUNCT_OTHERS)) ||
2339 ((type2 >= XML_REGEXP_SYMBOL) &&
2340 (type2 <= XML_REGEXP_SYMBOL_OTHERS)) ||
2341 ((type2 >= XML_REGEXP_OTHER) &&
2342 (type2 <= XML_REGEXP_OTHER_NA))
2343 ) return(0);
2344 break;
2345 case XML_REGEXP_NOTINITNAME: /* \L */
2346 break;
2347 case XML_REGEXP_NAMECHAR: /* \c */
Haibo Huangcfd91dc2020-07-30 23:01:33 -07002348 /* can't be a mark, separator, punctuation, symbol or other */
Daniel Veillardfc011b72006-02-12 19:14:15 +00002349 if ((type2 == XML_REGEXP_NOTNAMECHAR) ||
2350 ((type2 >= XML_REGEXP_MARK) &&
2351 (type2 <= XML_REGEXP_MARK_ENCLOSING)) ||
2352 ((type2 >= XML_REGEXP_PUNCT) &&
2353 (type2 <= XML_REGEXP_PUNCT_OTHERS)) ||
2354 ((type2 >= XML_REGEXP_SEPAR) &&
2355 (type2 <= XML_REGEXP_SEPAR_PARA)) ||
2356 ((type2 >= XML_REGEXP_SYMBOL) &&
2357 (type2 <= XML_REGEXP_SYMBOL_OTHERS)) ||
2358 ((type2 >= XML_REGEXP_OTHER) &&
2359 (type2 <= XML_REGEXP_OTHER_NA))
2360 ) return(0);
2361 break;
2362 case XML_REGEXP_NOTNAMECHAR: /* \C */
2363 break;
2364 case XML_REGEXP_DECIMAL: /* \d */
Haibo Huangcfd91dc2020-07-30 23:01:33 -07002365 /* can't be a letter, mark, separator, punctuation, symbol or other */
Daniel Veillardfc011b72006-02-12 19:14:15 +00002366 if ((type2 == XML_REGEXP_NOTDECIMAL) ||
2367 (type2 == XML_REGEXP_REALCHAR) ||
2368 ((type2 >= XML_REGEXP_LETTER) &&
2369 (type2 <= XML_REGEXP_LETTER_OTHERS)) ||
2370 ((type2 >= XML_REGEXP_MARK) &&
2371 (type2 <= XML_REGEXP_MARK_ENCLOSING)) ||
2372 ((type2 >= XML_REGEXP_PUNCT) &&
2373 (type2 <= XML_REGEXP_PUNCT_OTHERS)) ||
2374 ((type2 >= XML_REGEXP_SEPAR) &&
2375 (type2 <= XML_REGEXP_SEPAR_PARA)) ||
2376 ((type2 >= XML_REGEXP_SYMBOL) &&
2377 (type2 <= XML_REGEXP_SYMBOL_OTHERS)) ||
2378 ((type2 >= XML_REGEXP_OTHER) &&
2379 (type2 <= XML_REGEXP_OTHER_NA))
2380 )return(0);
2381 break;
2382 case XML_REGEXP_NOTDECIMAL: /* \D */
2383 break;
2384 case XML_REGEXP_REALCHAR: /* \w */
Haibo Huangcfd91dc2020-07-30 23:01:33 -07002385 /* can't be a mark, separator, punctuation, symbol or other */
Daniel Veillardfc011b72006-02-12 19:14:15 +00002386 if ((type2 == XML_REGEXP_NOTDECIMAL) ||
2387 ((type2 >= XML_REGEXP_MARK) &&
2388 (type2 <= XML_REGEXP_MARK_ENCLOSING)) ||
2389 ((type2 >= XML_REGEXP_PUNCT) &&
2390 (type2 <= XML_REGEXP_PUNCT_OTHERS)) ||
2391 ((type2 >= XML_REGEXP_SEPAR) &&
2392 (type2 <= XML_REGEXP_SEPAR_PARA)) ||
2393 ((type2 >= XML_REGEXP_SYMBOL) &&
2394 (type2 <= XML_REGEXP_SYMBOL_OTHERS)) ||
2395 ((type2 >= XML_REGEXP_OTHER) &&
2396 (type2 <= XML_REGEXP_OTHER_NA))
2397 )return(0);
2398 break;
2399 case XML_REGEXP_NOTREALCHAR: /* \W */
2400 break;
2401 /*
2402 * at that point we know both type 1 and type2 are from
2403 * character categories are ordered and are different,
2404 * it becomes simple because this is a partition
2405 */
2406 case XML_REGEXP_LETTER:
2407 if (type2 <= XML_REGEXP_LETTER_OTHERS)
2408 return(1);
2409 return(0);
2410 case XML_REGEXP_LETTER_UPPERCASE:
2411 case XML_REGEXP_LETTER_LOWERCASE:
2412 case XML_REGEXP_LETTER_TITLECASE:
2413 case XML_REGEXP_LETTER_MODIFIER:
2414 case XML_REGEXP_LETTER_OTHERS:
2415 return(0);
2416 case XML_REGEXP_MARK:
2417 if (type2 <= XML_REGEXP_MARK_ENCLOSING)
2418 return(1);
2419 return(0);
2420 case XML_REGEXP_MARK_NONSPACING:
2421 case XML_REGEXP_MARK_SPACECOMBINING:
2422 case XML_REGEXP_MARK_ENCLOSING:
2423 return(0);
2424 case XML_REGEXP_NUMBER:
2425 if (type2 <= XML_REGEXP_NUMBER_OTHERS)
2426 return(1);
2427 return(0);
2428 case XML_REGEXP_NUMBER_DECIMAL:
2429 case XML_REGEXP_NUMBER_LETTER:
2430 case XML_REGEXP_NUMBER_OTHERS:
2431 return(0);
2432 case XML_REGEXP_PUNCT:
2433 if (type2 <= XML_REGEXP_PUNCT_OTHERS)
2434 return(1);
2435 return(0);
2436 case XML_REGEXP_PUNCT_CONNECTOR:
2437 case XML_REGEXP_PUNCT_DASH:
2438 case XML_REGEXP_PUNCT_OPEN:
2439 case XML_REGEXP_PUNCT_CLOSE:
2440 case XML_REGEXP_PUNCT_INITQUOTE:
2441 case XML_REGEXP_PUNCT_FINQUOTE:
2442 case XML_REGEXP_PUNCT_OTHERS:
2443 return(0);
2444 case XML_REGEXP_SEPAR:
2445 if (type2 <= XML_REGEXP_SEPAR_PARA)
2446 return(1);
2447 return(0);
2448 case XML_REGEXP_SEPAR_SPACE:
2449 case XML_REGEXP_SEPAR_LINE:
2450 case XML_REGEXP_SEPAR_PARA:
2451 return(0);
2452 case XML_REGEXP_SYMBOL:
2453 if (type2 <= XML_REGEXP_SYMBOL_OTHERS)
2454 return(1);
2455 return(0);
2456 case XML_REGEXP_SYMBOL_MATH:
2457 case XML_REGEXP_SYMBOL_CURRENCY:
2458 case XML_REGEXP_SYMBOL_MODIFIER:
2459 case XML_REGEXP_SYMBOL_OTHERS:
2460 return(0);
2461 case XML_REGEXP_OTHER:
2462 if (type2 <= XML_REGEXP_OTHER_NA)
2463 return(1);
2464 return(0);
2465 case XML_REGEXP_OTHER_CONTROL:
2466 case XML_REGEXP_OTHER_FORMAT:
2467 case XML_REGEXP_OTHER_PRIVATE:
2468 case XML_REGEXP_OTHER_NA:
2469 return(0);
2470 default:
2471 break;
2472 }
2473 return(1);
2474}
2475
2476/**
2477 * xmlFAEqualAtoms:
Daniel Veillarde19fc232002-04-22 16:01:24 +00002478 * @atom1: an atom
2479 * @atom2: an atom
Daniel Veillard1ba2aca2009-08-31 16:47:39 +02002480 * @deep: if not set only compare string pointers
Daniel Veillarde19fc232002-04-22 16:01:24 +00002481 *
Daniel Veillardfc011b72006-02-12 19:14:15 +00002482 * Compares two atoms to check whether they are the same exactly
2483 * this is used to remove equivalent transitions
Daniel Veillarde19fc232002-04-22 16:01:24 +00002484 *
Daniel Veillardfc011b72006-02-12 19:14:15 +00002485 * Returns 1 if same and 0 otherwise
Daniel Veillarde19fc232002-04-22 16:01:24 +00002486 */
2487static int
Daniel Veillard1ba2aca2009-08-31 16:47:39 +02002488xmlFAEqualAtoms(xmlRegAtomPtr atom1, xmlRegAtomPtr atom2, int deep) {
Daniel Veillardfc011b72006-02-12 19:14:15 +00002489 int ret = 0;
Daniel Veillard9efc4762005-07-19 14:33:55 +00002490
Daniel Veillarde19fc232002-04-22 16:01:24 +00002491 if (atom1 == atom2)
2492 return(1);
2493 if ((atom1 == NULL) || (atom2 == NULL))
2494 return(0);
2495
Daniel Veillardfc011b72006-02-12 19:14:15 +00002496 if (atom1->type != atom2->type)
2497 return(0);
2498 switch (atom1->type) {
2499 case XML_REGEXP_EPSILON:
2500 ret = 0;
2501 break;
2502 case XML_REGEXP_STRING:
Daniel Veillard1ba2aca2009-08-31 16:47:39 +02002503 if (!deep)
2504 ret = (atom1->valuep == atom2->valuep);
2505 else
2506 ret = xmlStrEqual((xmlChar *)atom1->valuep,
2507 (xmlChar *)atom2->valuep);
Daniel Veillardfc011b72006-02-12 19:14:15 +00002508 break;
2509 case XML_REGEXP_CHARVAL:
2510 ret = (atom1->codepoint == atom2->codepoint);
2511 break;
2512 case XML_REGEXP_RANGES:
2513 /* too hard to do in the general case */
2514 ret = 0;
2515 default:
2516 break;
2517 }
2518 return(ret);
2519}
2520
2521/**
2522 * xmlFACompareAtoms:
2523 * @atom1: an atom
2524 * @atom2: an atom
Daniel Veillard1ba2aca2009-08-31 16:47:39 +02002525 * @deep: if not set only compare string pointers
Daniel Veillardfc011b72006-02-12 19:14:15 +00002526 *
2527 * Compares two atoms to check whether they intersect in some ways,
2528 * this is used by xmlFAComputesDeterminism and xmlFARecurseDeterminism only
2529 *
2530 * Returns 1 if yes and 0 otherwise
2531 */
2532static int
Daniel Veillard1ba2aca2009-08-31 16:47:39 +02002533xmlFACompareAtoms(xmlRegAtomPtr atom1, xmlRegAtomPtr atom2, int deep) {
Daniel Veillardfc011b72006-02-12 19:14:15 +00002534 int ret = 1;
2535
2536 if (atom1 == atom2)
2537 return(1);
2538 if ((atom1 == NULL) || (atom2 == NULL))
2539 return(0);
2540
2541 if ((atom1->type == XML_REGEXP_ANYCHAR) ||
2542 (atom2->type == XML_REGEXP_ANYCHAR))
2543 return(1);
2544
2545 if (atom1->type > atom2->type) {
Daniel Veillard567a45b2005-10-18 19:11:55 +00002546 xmlRegAtomPtr tmp;
2547 tmp = atom1;
2548 atom1 = atom2;
2549 atom2 = tmp;
Daniel Veillardfc011b72006-02-12 19:14:15 +00002550 }
2551 if (atom1->type != atom2->type) {
2552 ret = xmlFACompareAtomTypes(atom1->type, atom2->type);
2553 /* if they can't intersect at the type level break now */
2554 if (ret == 0)
2555 return(0);
Daniel Veillard567a45b2005-10-18 19:11:55 +00002556 }
Daniel Veillarde19fc232002-04-22 16:01:24 +00002557 switch (atom1->type) {
2558 case XML_REGEXP_STRING:
Daniel Veillard1ba2aca2009-08-31 16:47:39 +02002559 if (!deep)
2560 ret = (atom1->valuep != atom2->valuep);
Haibo Huangcfd91dc2020-07-30 23:01:33 -07002561 else {
2562 xmlChar *val1 = (xmlChar *)atom1->valuep;
2563 xmlChar *val2 = (xmlChar *)atom2->valuep;
2564 int compound1 = (xmlStrchr(val1, '|') != NULL);
2565 int compound2 = (xmlStrchr(val2, '|') != NULL);
2566
2567 /* Ignore negative match flag for ##other namespaces */
2568 if (compound1 != compound2)
2569 return(0);
2570
2571 ret = xmlRegStrEqualWildcard(val1, val2);
2572 }
Daniel Veillard9efc4762005-07-19 14:33:55 +00002573 break;
Daniel Veillarde19fc232002-04-22 16:01:24 +00002574 case XML_REGEXP_EPSILON:
Daniel Veillard567a45b2005-10-18 19:11:55 +00002575 goto not_determinist;
Daniel Veillarde19fc232002-04-22 16:01:24 +00002576 case XML_REGEXP_CHARVAL:
Daniel Veillardfc011b72006-02-12 19:14:15 +00002577 if (atom2->type == XML_REGEXP_CHARVAL) {
2578 ret = (atom1->codepoint == atom2->codepoint);
2579 } else {
2580 ret = xmlRegCheckCharacter(atom2, atom1->codepoint);
2581 if (ret < 0)
2582 ret = 1;
2583 }
Daniel Veillard9efc4762005-07-19 14:33:55 +00002584 break;
Daniel Veillarde19fc232002-04-22 16:01:24 +00002585 case XML_REGEXP_RANGES:
Daniel Veillardfc011b72006-02-12 19:14:15 +00002586 if (atom2->type == XML_REGEXP_RANGES) {
Daniel Veillard567a45b2005-10-18 19:11:55 +00002587 int i, j, res;
2588 xmlRegRangePtr r1, r2;
2589
2590 /*
2591 * need to check that none of the ranges eventually matches
2592 */
2593 for (i = 0;i < atom1->nbRanges;i++) {
2594 for (j = 0;j < atom2->nbRanges;j++) {
2595 r1 = atom1->ranges[i];
2596 r2 = atom2->ranges[j];
2597 res = xmlFACompareRanges(r1, r2);
2598 if (res == 1) {
2599 ret = 1;
2600 goto done;
2601 }
2602 }
2603 }
2604 ret = 0;
2605 }
2606 break;
Daniel Veillarde19fc232002-04-22 16:01:24 +00002607 default:
Daniel Veillard567a45b2005-10-18 19:11:55 +00002608 goto not_determinist;
Daniel Veillarde19fc232002-04-22 16:01:24 +00002609 }
Daniel Veillard567a45b2005-10-18 19:11:55 +00002610done:
Daniel Veillard6e65e152005-08-09 11:09:52 +00002611 if (atom1->neg != atom2->neg) {
Daniel Veillard9efc4762005-07-19 14:33:55 +00002612 ret = !ret;
Daniel Veillard6e65e152005-08-09 11:09:52 +00002613 }
Daniel Veillard567a45b2005-10-18 19:11:55 +00002614 if (ret == 0)
2615 return(0);
2616not_determinist:
2617 return(1);
Daniel Veillarde19fc232002-04-22 16:01:24 +00002618}
2619
2620/**
2621 * xmlFARecurseDeterminism:
2622 * @ctxt: a regexp parser context
2623 *
2624 * Check whether the associated regexp is determinist,
2625 * should be called after xmlFAEliminateEpsilonTransitions()
2626 *
2627 */
2628static int
2629xmlFARecurseDeterminism(xmlRegParserCtxtPtr ctxt, xmlRegStatePtr state,
2630 int to, xmlRegAtomPtr atom) {
2631 int ret = 1;
Daniel Veillard567a45b2005-10-18 19:11:55 +00002632 int res;
Daniel Veillard5de09382005-09-26 17:18:17 +00002633 int transnr, nbTrans;
Daniel Veillarde19fc232002-04-22 16:01:24 +00002634 xmlRegTransPtr t1;
Daniel Veillard1ba2aca2009-08-31 16:47:39 +02002635 int deep = 1;
Daniel Veillarde19fc232002-04-22 16:01:24 +00002636
2637 if (state == NULL)
2638 return(ret);
Daniel Veillard466fcda2012-08-27 12:03:40 +08002639 if (state->markd == XML_REGEXP_MARK_VISITED)
2640 return(ret);
Daniel Veillard1ba2aca2009-08-31 16:47:39 +02002641
2642 if (ctxt->flags & AM_AUTOMATA_RNG)
2643 deep = 0;
2644
Daniel Veillard5de09382005-09-26 17:18:17 +00002645 /*
2646 * don't recurse on transitions potentially added in the course of
2647 * the elimination.
2648 */
2649 nbTrans = state->nbTrans;
2650 for (transnr = 0;transnr < nbTrans;transnr++) {
Daniel Veillarde19fc232002-04-22 16:01:24 +00002651 t1 = &(state->trans[transnr]);
2652 /*
2653 * check transitions conflicting with the one looked at
2654 */
2655 if (t1->atom == NULL) {
Daniel Veillard0e05f4c2006-11-01 15:33:04 +00002656 if (t1->to < 0)
Daniel Veillarde19fc232002-04-22 16:01:24 +00002657 continue;
Daniel Veillard466fcda2012-08-27 12:03:40 +08002658 state->markd = XML_REGEXP_MARK_VISITED;
Daniel Veillard567a45b2005-10-18 19:11:55 +00002659 res = xmlFARecurseDeterminism(ctxt, ctxt->states[t1->to],
Daniel Veillarde19fc232002-04-22 16:01:24 +00002660 to, atom);
Daniel Veillard567a45b2005-10-18 19:11:55 +00002661 if (res == 0) {
2662 ret = 0;
Daniel Veillardaa622012005-10-20 15:55:25 +00002663 /* t1->nd = 1; */
Daniel Veillard567a45b2005-10-18 19:11:55 +00002664 }
Daniel Veillarde19fc232002-04-22 16:01:24 +00002665 continue;
2666 }
2667 if (t1->to != to)
2668 continue;
Daniel Veillard1ba2aca2009-08-31 16:47:39 +02002669 if (xmlFACompareAtoms(t1->atom, atom, deep)) {
Daniel Veillard567a45b2005-10-18 19:11:55 +00002670 ret = 0;
2671 /* mark the transition as non-deterministic */
2672 t1->nd = 1;
2673 }
Daniel Veillarde19fc232002-04-22 16:01:24 +00002674 }
2675 return(ret);
2676}
2677
2678/**
Haibo Huang35812382020-07-31 15:23:22 -07002679 * xmlFAFinishRecurseDeterminism:
2680 * @ctxt: a regexp parser context
2681 *
2682 * Reset flags after checking determinism.
2683 */
2684static void
2685xmlFAFinishRecurseDeterminism(xmlRegParserCtxtPtr ctxt, xmlRegStatePtr state) {
2686 int transnr, nbTrans;
2687
2688 if (state == NULL)
2689 return;
2690 if (state->markd != XML_REGEXP_MARK_VISITED)
2691 return;
2692 state->markd = 0;
2693
2694 nbTrans = state->nbTrans;
2695 for (transnr = 0; transnr < nbTrans; transnr++) {
2696 xmlRegTransPtr t1 = &state->trans[transnr];
2697 if ((t1->atom == NULL) && (t1->to >= 0))
2698 xmlFAFinishRecurseDeterminism(ctxt, ctxt->states[t1->to]);
2699 }
2700}
2701
2702/**
Daniel Veillarde19fc232002-04-22 16:01:24 +00002703 * xmlFAComputesDeterminism:
2704 * @ctxt: a regexp parser context
2705 *
2706 * Check whether the associated regexp is determinist,
2707 * should be called after xmlFAEliminateEpsilonTransitions()
2708 *
2709 */
2710static int
2711xmlFAComputesDeterminism(xmlRegParserCtxtPtr ctxt) {
2712 int statenr, transnr;
2713 xmlRegStatePtr state;
Daniel Veillard567a45b2005-10-18 19:11:55 +00002714 xmlRegTransPtr t1, t2, last;
Daniel Veillarde19fc232002-04-22 16:01:24 +00002715 int i;
2716 int ret = 1;
Daniel Veillard1ba2aca2009-08-31 16:47:39 +02002717 int deep = 1;
Daniel Veillarde19fc232002-04-22 16:01:24 +00002718
Daniel Veillard4402ab42002-09-12 16:02:56 +00002719#ifdef DEBUG_REGEXP_GRAPH
2720 printf("xmlFAComputesDeterminism\n");
2721 xmlRegPrintCtxt(stdout, ctxt);
2722#endif
Daniel Veillarde19fc232002-04-22 16:01:24 +00002723 if (ctxt->determinist != -1)
2724 return(ctxt->determinist);
2725
Daniel Veillard1ba2aca2009-08-31 16:47:39 +02002726 if (ctxt->flags & AM_AUTOMATA_RNG)
2727 deep = 0;
2728
Daniel Veillarde19fc232002-04-22 16:01:24 +00002729 /*
Daniel Veillard567a45b2005-10-18 19:11:55 +00002730 * First cleanup the automata removing cancelled transitions
Daniel Veillarde19fc232002-04-22 16:01:24 +00002731 */
2732 for (statenr = 0;statenr < ctxt->nbStates;statenr++) {
2733 state = ctxt->states[statenr];
2734 if (state == NULL)
2735 continue;
Daniel Veillard4f82c8a2005-08-09 21:40:08 +00002736 if (state->nbTrans < 2)
2737 continue;
Daniel Veillarde19fc232002-04-22 16:01:24 +00002738 for (transnr = 0;transnr < state->nbTrans;transnr++) {
2739 t1 = &(state->trans[transnr]);
2740 /*
2741 * Determinism checks in case of counted or all transitions
2742 * will have to be handled separately
2743 */
Daniel Veillard567a45b2005-10-18 19:11:55 +00002744 if (t1->atom == NULL) {
Daniel Veillardaa622012005-10-20 15:55:25 +00002745 /* t1->nd = 1; */
Daniel Veillarde19fc232002-04-22 16:01:24 +00002746 continue;
Daniel Veillard567a45b2005-10-18 19:11:55 +00002747 }
Daniel Veillarde19fc232002-04-22 16:01:24 +00002748 if (t1->to == -1) /* eliminated */
2749 continue;
2750 for (i = 0;i < transnr;i++) {
2751 t2 = &(state->trans[i]);
2752 if (t2->to == -1) /* eliminated */
2753 continue;
2754 if (t2->atom != NULL) {
2755 if (t1->to == t2->to) {
Daniel Veillard1ba2aca2009-08-31 16:47:39 +02002756 /*
2757 * Here we use deep because we want to keep the
2758 * transitions which indicate a conflict
2759 */
2760 if (xmlFAEqualAtoms(t1->atom, t2->atom, deep) &&
Daniel Veillard11e28e42009-08-12 12:21:42 +02002761 (t1->counter == t2->counter) &&
2762 (t1->count == t2->count))
William M. Brackddf71d62004-05-06 04:17:26 +00002763 t2->to = -1; /* eliminated */
Daniel Veillard567a45b2005-10-18 19:11:55 +00002764 }
2765 }
2766 }
2767 }
2768 }
2769
2770 /*
2771 * Check for all states that there aren't 2 transitions
2772 * with the same atom and a different target.
2773 */
2774 for (statenr = 0;statenr < ctxt->nbStates;statenr++) {
2775 state = ctxt->states[statenr];
2776 if (state == NULL)
2777 continue;
2778 if (state->nbTrans < 2)
2779 continue;
2780 last = NULL;
2781 for (transnr = 0;transnr < state->nbTrans;transnr++) {
2782 t1 = &(state->trans[transnr]);
2783 /*
2784 * Determinism checks in case of counted or all transitions
2785 * will have to be handled separately
2786 */
2787 if (t1->atom == NULL) {
2788 continue;
2789 }
2790 if (t1->to == -1) /* eliminated */
2791 continue;
2792 for (i = 0;i < transnr;i++) {
2793 t2 = &(state->trans[i]);
2794 if (t2->to == -1) /* eliminated */
2795 continue;
2796 if (t2->atom != NULL) {
Daniel Veillard1ba2aca2009-08-31 16:47:39 +02002797 /*
2798 * But here we don't use deep because we want to
2799 * find transitions which indicate a conflict
2800 */
2801 if (xmlFACompareAtoms(t1->atom, t2->atom, 1)) {
Daniel Veillard567a45b2005-10-18 19:11:55 +00002802 ret = 0;
2803 /* mark the transitions as non-deterministic ones */
2804 t1->nd = 1;
2805 t2->nd = 1;
2806 last = t1;
Daniel Veillarde19fc232002-04-22 16:01:24 +00002807 }
2808 } else if (t1->to != -1) {
2809 /*
2810 * do the closure in case of remaining specific
2811 * epsilon transitions like choices or all
2812 */
2813 ret = xmlFARecurseDeterminism(ctxt, ctxt->states[t1->to],
2814 t2->to, t2->atom);
Haibo Huang35812382020-07-31 15:23:22 -07002815 xmlFAFinishRecurseDeterminism(ctxt, ctxt->states[t1->to]);
Daniel Veillard567a45b2005-10-18 19:11:55 +00002816 /* don't shortcut the computation so all non deterministic
2817 transition get marked down
Daniel Veillarde19fc232002-04-22 16:01:24 +00002818 if (ret == 0)
Daniel Veillardaa622012005-10-20 15:55:25 +00002819 return(0);
2820 */
Daniel Veillard567a45b2005-10-18 19:11:55 +00002821 if (ret == 0) {
2822 t1->nd = 1;
Daniel Veillardaa622012005-10-20 15:55:25 +00002823 /* t2->nd = 1; */
Daniel Veillard567a45b2005-10-18 19:11:55 +00002824 last = t1;
2825 }
Daniel Veillarde19fc232002-04-22 16:01:24 +00002826 }
2827 }
Daniel Veillard567a45b2005-10-18 19:11:55 +00002828 /* don't shortcut the computation so all non deterministic
2829 transition get marked down
Daniel Veillarde19fc232002-04-22 16:01:24 +00002830 if (ret == 0)
Daniel Veillard567a45b2005-10-18 19:11:55 +00002831 break; */
Daniel Veillarde19fc232002-04-22 16:01:24 +00002832 }
Daniel Veillard567a45b2005-10-18 19:11:55 +00002833
2834 /*
2835 * mark specifically the last non-deterministic transition
2836 * from a state since there is no need to set-up rollback
2837 * from it
2838 */
2839 if (last != NULL) {
2840 last->nd = 2;
2841 }
2842
2843 /* don't shortcut the computation so all non deterministic
2844 transition get marked down
Daniel Veillarde19fc232002-04-22 16:01:24 +00002845 if (ret == 0)
Daniel Veillard567a45b2005-10-18 19:11:55 +00002846 break; */
Daniel Veillarde19fc232002-04-22 16:01:24 +00002847 }
Daniel Veillard567a45b2005-10-18 19:11:55 +00002848
Daniel Veillarde19fc232002-04-22 16:01:24 +00002849 ctxt->determinist = ret;
2850 return(ret);
2851}
2852
Daniel Veillard4255d502002-04-16 15:50:10 +00002853/************************************************************************
Daniel Veillardf8e3db02012-09-11 13:26:36 +08002854 * *
Daniel Veillard4255d502002-04-16 15:50:10 +00002855 * Routines to check input against transition atoms *
Daniel Veillardf8e3db02012-09-11 13:26:36 +08002856 * *
Daniel Veillard4255d502002-04-16 15:50:10 +00002857 ************************************************************************/
2858
2859static int
2860xmlRegCheckCharacterRange(xmlRegAtomType type, int codepoint, int neg,
2861 int start, int end, const xmlChar *blockName) {
2862 int ret = 0;
2863
2864 switch (type) {
2865 case XML_REGEXP_STRING:
2866 case XML_REGEXP_SUBREG:
2867 case XML_REGEXP_RANGES:
2868 case XML_REGEXP_EPSILON:
2869 return(-1);
2870 case XML_REGEXP_ANYCHAR:
2871 ret = ((codepoint != '\n') && (codepoint != '\r'));
2872 break;
2873 case XML_REGEXP_CHARVAL:
2874 ret = ((codepoint >= start) && (codepoint <= end));
2875 break;
2876 case XML_REGEXP_NOTSPACE:
2877 neg = !neg;
J. Peter Mugaasd2c329a2017-10-21 13:49:31 +02002878 /* Falls through. */
Daniel Veillard4255d502002-04-16 15:50:10 +00002879 case XML_REGEXP_ANYSPACE:
2880 ret = ((codepoint == '\n') || (codepoint == '\r') ||
2881 (codepoint == '\t') || (codepoint == ' '));
2882 break;
2883 case XML_REGEXP_NOTINITNAME:
2884 neg = !neg;
J. Peter Mugaasd2c329a2017-10-21 13:49:31 +02002885 /* Falls through. */
Daniel Veillard4255d502002-04-16 15:50:10 +00002886 case XML_REGEXP_INITNAME:
Daniel Veillardf8e3db02012-09-11 13:26:36 +08002887 ret = (IS_LETTER(codepoint) ||
Daniel Veillard4255d502002-04-16 15:50:10 +00002888 (codepoint == '_') || (codepoint == ':'));
2889 break;
2890 case XML_REGEXP_NOTNAMECHAR:
2891 neg = !neg;
J. Peter Mugaasd2c329a2017-10-21 13:49:31 +02002892 /* Falls through. */
Daniel Veillard4255d502002-04-16 15:50:10 +00002893 case XML_REGEXP_NAMECHAR:
William M. Brack871611b2003-10-18 04:53:14 +00002894 ret = (IS_LETTER(codepoint) || IS_DIGIT(codepoint) ||
Daniel Veillard4255d502002-04-16 15:50:10 +00002895 (codepoint == '.') || (codepoint == '-') ||
2896 (codepoint == '_') || (codepoint == ':') ||
William M. Brack871611b2003-10-18 04:53:14 +00002897 IS_COMBINING(codepoint) || IS_EXTENDER(codepoint));
Daniel Veillard4255d502002-04-16 15:50:10 +00002898 break;
2899 case XML_REGEXP_NOTDECIMAL:
2900 neg = !neg;
J. Peter Mugaasd2c329a2017-10-21 13:49:31 +02002901 /* Falls through. */
Daniel Veillard4255d502002-04-16 15:50:10 +00002902 case XML_REGEXP_DECIMAL:
2903 ret = xmlUCSIsCatNd(codepoint);
2904 break;
2905 case XML_REGEXP_REALCHAR:
2906 neg = !neg;
J. Peter Mugaasd2c329a2017-10-21 13:49:31 +02002907 /* Falls through. */
Daniel Veillard4255d502002-04-16 15:50:10 +00002908 case XML_REGEXP_NOTREALCHAR:
2909 ret = xmlUCSIsCatP(codepoint);
2910 if (ret == 0)
2911 ret = xmlUCSIsCatZ(codepoint);
2912 if (ret == 0)
2913 ret = xmlUCSIsCatC(codepoint);
2914 break;
2915 case XML_REGEXP_LETTER:
2916 ret = xmlUCSIsCatL(codepoint);
2917 break;
2918 case XML_REGEXP_LETTER_UPPERCASE:
2919 ret = xmlUCSIsCatLu(codepoint);
2920 break;
2921 case XML_REGEXP_LETTER_LOWERCASE:
2922 ret = xmlUCSIsCatLl(codepoint);
2923 break;
2924 case XML_REGEXP_LETTER_TITLECASE:
2925 ret = xmlUCSIsCatLt(codepoint);
2926 break;
2927 case XML_REGEXP_LETTER_MODIFIER:
2928 ret = xmlUCSIsCatLm(codepoint);
2929 break;
2930 case XML_REGEXP_LETTER_OTHERS:
2931 ret = xmlUCSIsCatLo(codepoint);
2932 break;
2933 case XML_REGEXP_MARK:
2934 ret = xmlUCSIsCatM(codepoint);
2935 break;
2936 case XML_REGEXP_MARK_NONSPACING:
2937 ret = xmlUCSIsCatMn(codepoint);
2938 break;
2939 case XML_REGEXP_MARK_SPACECOMBINING:
2940 ret = xmlUCSIsCatMc(codepoint);
2941 break;
2942 case XML_REGEXP_MARK_ENCLOSING:
2943 ret = xmlUCSIsCatMe(codepoint);
2944 break;
2945 case XML_REGEXP_NUMBER:
2946 ret = xmlUCSIsCatN(codepoint);
2947 break;
2948 case XML_REGEXP_NUMBER_DECIMAL:
2949 ret = xmlUCSIsCatNd(codepoint);
2950 break;
2951 case XML_REGEXP_NUMBER_LETTER:
2952 ret = xmlUCSIsCatNl(codepoint);
2953 break;
2954 case XML_REGEXP_NUMBER_OTHERS:
2955 ret = xmlUCSIsCatNo(codepoint);
2956 break;
2957 case XML_REGEXP_PUNCT:
2958 ret = xmlUCSIsCatP(codepoint);
2959 break;
2960 case XML_REGEXP_PUNCT_CONNECTOR:
2961 ret = xmlUCSIsCatPc(codepoint);
2962 break;
2963 case XML_REGEXP_PUNCT_DASH:
2964 ret = xmlUCSIsCatPd(codepoint);
2965 break;
2966 case XML_REGEXP_PUNCT_OPEN:
2967 ret = xmlUCSIsCatPs(codepoint);
2968 break;
2969 case XML_REGEXP_PUNCT_CLOSE:
2970 ret = xmlUCSIsCatPe(codepoint);
2971 break;
2972 case XML_REGEXP_PUNCT_INITQUOTE:
2973 ret = xmlUCSIsCatPi(codepoint);
2974 break;
2975 case XML_REGEXP_PUNCT_FINQUOTE:
2976 ret = xmlUCSIsCatPf(codepoint);
2977 break;
2978 case XML_REGEXP_PUNCT_OTHERS:
2979 ret = xmlUCSIsCatPo(codepoint);
2980 break;
2981 case XML_REGEXP_SEPAR:
2982 ret = xmlUCSIsCatZ(codepoint);
2983 break;
2984 case XML_REGEXP_SEPAR_SPACE:
2985 ret = xmlUCSIsCatZs(codepoint);
2986 break;
2987 case XML_REGEXP_SEPAR_LINE:
2988 ret = xmlUCSIsCatZl(codepoint);
2989 break;
2990 case XML_REGEXP_SEPAR_PARA:
2991 ret = xmlUCSIsCatZp(codepoint);
2992 break;
2993 case XML_REGEXP_SYMBOL:
2994 ret = xmlUCSIsCatS(codepoint);
2995 break;
2996 case XML_REGEXP_SYMBOL_MATH:
2997 ret = xmlUCSIsCatSm(codepoint);
2998 break;
2999 case XML_REGEXP_SYMBOL_CURRENCY:
3000 ret = xmlUCSIsCatSc(codepoint);
3001 break;
3002 case XML_REGEXP_SYMBOL_MODIFIER:
3003 ret = xmlUCSIsCatSk(codepoint);
3004 break;
3005 case XML_REGEXP_SYMBOL_OTHERS:
3006 ret = xmlUCSIsCatSo(codepoint);
3007 break;
3008 case XML_REGEXP_OTHER:
3009 ret = xmlUCSIsCatC(codepoint);
3010 break;
3011 case XML_REGEXP_OTHER_CONTROL:
3012 ret = xmlUCSIsCatCc(codepoint);
3013 break;
3014 case XML_REGEXP_OTHER_FORMAT:
3015 ret = xmlUCSIsCatCf(codepoint);
3016 break;
3017 case XML_REGEXP_OTHER_PRIVATE:
3018 ret = xmlUCSIsCatCo(codepoint);
3019 break;
3020 case XML_REGEXP_OTHER_NA:
3021 /* ret = xmlUCSIsCatCn(codepoint); */
3022 /* Seems it doesn't exist anymore in recent Unicode releases */
3023 ret = 0;
3024 break;
3025 case XML_REGEXP_BLOCK_NAME:
3026 ret = xmlUCSIsBlock(codepoint, (const char *) blockName);
3027 break;
3028 }
3029 if (neg)
3030 return(!ret);
3031 return(ret);
3032}
3033
3034static int
3035xmlRegCheckCharacter(xmlRegAtomPtr atom, int codepoint) {
3036 int i, ret = 0;
3037 xmlRegRangePtr range;
3038
William M. Brack871611b2003-10-18 04:53:14 +00003039 if ((atom == NULL) || (!IS_CHAR(codepoint)))
Daniel Veillard4255d502002-04-16 15:50:10 +00003040 return(-1);
3041
3042 switch (atom->type) {
3043 case XML_REGEXP_SUBREG:
3044 case XML_REGEXP_EPSILON:
3045 return(-1);
3046 case XML_REGEXP_CHARVAL:
3047 return(codepoint == atom->codepoint);
3048 case XML_REGEXP_RANGES: {
3049 int accept = 0;
Daniel Veillardf2a12832003-11-24 13:04:35 +00003050
Daniel Veillard4255d502002-04-16 15:50:10 +00003051 for (i = 0;i < atom->nbRanges;i++) {
3052 range = atom->ranges[i];
Daniel Veillardf8b9de32003-11-24 14:27:26 +00003053 if (range->neg == 2) {
Daniel Veillard4255d502002-04-16 15:50:10 +00003054 ret = xmlRegCheckCharacterRange(range->type, codepoint,
3055 0, range->start, range->end,
3056 range->blockName);
3057 if (ret != 0)
3058 return(0); /* excluded char */
Daniel Veillardf8b9de32003-11-24 14:27:26 +00003059 } else if (range->neg) {
3060 ret = xmlRegCheckCharacterRange(range->type, codepoint,
3061 0, range->start, range->end,
3062 range->blockName);
3063 if (ret == 0)
Daniel Veillardf2a12832003-11-24 13:04:35 +00003064 accept = 1;
Daniel Veillardf8b9de32003-11-24 14:27:26 +00003065 else
3066 return(0);
Daniel Veillard4255d502002-04-16 15:50:10 +00003067 } else {
3068 ret = xmlRegCheckCharacterRange(range->type, codepoint,
3069 0, range->start, range->end,
3070 range->blockName);
3071 if (ret != 0)
3072 accept = 1; /* might still be excluded */
3073 }
3074 }
3075 return(accept);
3076 }
3077 case XML_REGEXP_STRING:
3078 printf("TODO: XML_REGEXP_STRING\n");
3079 return(-1);
3080 case XML_REGEXP_ANYCHAR:
3081 case XML_REGEXP_ANYSPACE:
3082 case XML_REGEXP_NOTSPACE:
3083 case XML_REGEXP_INITNAME:
3084 case XML_REGEXP_NOTINITNAME:
3085 case XML_REGEXP_NAMECHAR:
3086 case XML_REGEXP_NOTNAMECHAR:
3087 case XML_REGEXP_DECIMAL:
3088 case XML_REGEXP_NOTDECIMAL:
3089 case XML_REGEXP_REALCHAR:
3090 case XML_REGEXP_NOTREALCHAR:
3091 case XML_REGEXP_LETTER:
3092 case XML_REGEXP_LETTER_UPPERCASE:
3093 case XML_REGEXP_LETTER_LOWERCASE:
3094 case XML_REGEXP_LETTER_TITLECASE:
3095 case XML_REGEXP_LETTER_MODIFIER:
3096 case XML_REGEXP_LETTER_OTHERS:
3097 case XML_REGEXP_MARK:
3098 case XML_REGEXP_MARK_NONSPACING:
3099 case XML_REGEXP_MARK_SPACECOMBINING:
3100 case XML_REGEXP_MARK_ENCLOSING:
3101 case XML_REGEXP_NUMBER:
3102 case XML_REGEXP_NUMBER_DECIMAL:
3103 case XML_REGEXP_NUMBER_LETTER:
3104 case XML_REGEXP_NUMBER_OTHERS:
3105 case XML_REGEXP_PUNCT:
3106 case XML_REGEXP_PUNCT_CONNECTOR:
3107 case XML_REGEXP_PUNCT_DASH:
3108 case XML_REGEXP_PUNCT_OPEN:
3109 case XML_REGEXP_PUNCT_CLOSE:
3110 case XML_REGEXP_PUNCT_INITQUOTE:
3111 case XML_REGEXP_PUNCT_FINQUOTE:
3112 case XML_REGEXP_PUNCT_OTHERS:
3113 case XML_REGEXP_SEPAR:
3114 case XML_REGEXP_SEPAR_SPACE:
3115 case XML_REGEXP_SEPAR_LINE:
3116 case XML_REGEXP_SEPAR_PARA:
3117 case XML_REGEXP_SYMBOL:
3118 case XML_REGEXP_SYMBOL_MATH:
3119 case XML_REGEXP_SYMBOL_CURRENCY:
3120 case XML_REGEXP_SYMBOL_MODIFIER:
3121 case XML_REGEXP_SYMBOL_OTHERS:
3122 case XML_REGEXP_OTHER:
3123 case XML_REGEXP_OTHER_CONTROL:
3124 case XML_REGEXP_OTHER_FORMAT:
3125 case XML_REGEXP_OTHER_PRIVATE:
3126 case XML_REGEXP_OTHER_NA:
3127 case XML_REGEXP_BLOCK_NAME:
3128 ret = xmlRegCheckCharacterRange(atom->type, codepoint, 0, 0, 0,
3129 (const xmlChar *)atom->valuep);
3130 if (atom->neg)
3131 ret = !ret;
3132 break;
3133 }
3134 return(ret);
3135}
3136
3137/************************************************************************
Daniel Veillardf8e3db02012-09-11 13:26:36 +08003138 * *
William M. Brackddf71d62004-05-06 04:17:26 +00003139 * Saving and restoring state of an execution context *
Daniel Veillardf8e3db02012-09-11 13:26:36 +08003140 * *
Daniel Veillard4255d502002-04-16 15:50:10 +00003141 ************************************************************************/
3142
3143#ifdef DEBUG_REGEXP_EXEC
3144static void
3145xmlFARegDebugExec(xmlRegExecCtxtPtr exec) {
3146 printf("state: %d:%d:idx %d", exec->state->no, exec->transno, exec->index);
3147 if (exec->inputStack != NULL) {
3148 int i;
3149 printf(": ");
3150 for (i = 0;(i < 3) && (i < exec->inputStackNr);i++)
Daniel Veillard0e05f4c2006-11-01 15:33:04 +00003151 printf("%s ", (const char *)
3152 exec->inputStack[exec->inputStackNr - (i + 1)].value);
Daniel Veillard4255d502002-04-16 15:50:10 +00003153 } else {
3154 printf(": %s", &(exec->inputString[exec->index]));
3155 }
3156 printf("\n");
3157}
3158#endif
3159
3160static void
3161xmlFARegExecSave(xmlRegExecCtxtPtr exec) {
3162#ifdef DEBUG_REGEXP_EXEC
3163 printf("saving ");
3164 exec->transno++;
3165 xmlFARegDebugExec(exec);
3166 exec->transno--;
3167#endif
Daniel Veillard94cc1032005-09-15 13:09:00 +00003168#ifdef MAX_PUSH
3169 if (exec->nbPush > MAX_PUSH) {
3170 return;
3171 }
3172 exec->nbPush++;
3173#endif
Daniel Veillard4255d502002-04-16 15:50:10 +00003174
3175 if (exec->maxRollbacks == 0) {
3176 exec->maxRollbacks = 4;
3177 exec->rollbacks = (xmlRegExecRollback *) xmlMalloc(exec->maxRollbacks *
3178 sizeof(xmlRegExecRollback));
3179 if (exec->rollbacks == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00003180 xmlRegexpErrMemory(NULL, "saving regexp");
Daniel Veillard4255d502002-04-16 15:50:10 +00003181 exec->maxRollbacks = 0;
3182 return;
3183 }
3184 memset(exec->rollbacks, 0,
3185 exec->maxRollbacks * sizeof(xmlRegExecRollback));
3186 } else if (exec->nbRollbacks >= exec->maxRollbacks) {
3187 xmlRegExecRollback *tmp;
3188 int len = exec->maxRollbacks;
3189
3190 exec->maxRollbacks *= 2;
3191 tmp = (xmlRegExecRollback *) xmlRealloc(exec->rollbacks,
3192 exec->maxRollbacks * sizeof(xmlRegExecRollback));
3193 if (tmp == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00003194 xmlRegexpErrMemory(NULL, "saving regexp");
Daniel Veillard4255d502002-04-16 15:50:10 +00003195 exec->maxRollbacks /= 2;
3196 return;
3197 }
3198 exec->rollbacks = tmp;
3199 tmp = &exec->rollbacks[len];
3200 memset(tmp, 0, (exec->maxRollbacks - len) * sizeof(xmlRegExecRollback));
3201 }
3202 exec->rollbacks[exec->nbRollbacks].state = exec->state;
3203 exec->rollbacks[exec->nbRollbacks].index = exec->index;
3204 exec->rollbacks[exec->nbRollbacks].nextbranch = exec->transno + 1;
3205 if (exec->comp->nbCounters > 0) {
3206 if (exec->rollbacks[exec->nbRollbacks].counts == NULL) {
3207 exec->rollbacks[exec->nbRollbacks].counts = (int *)
3208 xmlMalloc(exec->comp->nbCounters * sizeof(int));
3209 if (exec->rollbacks[exec->nbRollbacks].counts == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00003210 xmlRegexpErrMemory(NULL, "saving regexp");
Daniel Veillard4255d502002-04-16 15:50:10 +00003211 exec->status = -5;
3212 return;
3213 }
3214 }
3215 memcpy(exec->rollbacks[exec->nbRollbacks].counts, exec->counts,
3216 exec->comp->nbCounters * sizeof(int));
3217 }
3218 exec->nbRollbacks++;
3219}
3220
3221static void
3222xmlFARegExecRollBack(xmlRegExecCtxtPtr exec) {
3223 if (exec->nbRollbacks <= 0) {
3224 exec->status = -1;
3225#ifdef DEBUG_REGEXP_EXEC
3226 printf("rollback failed on empty stack\n");
3227#endif
3228 return;
3229 }
3230 exec->nbRollbacks--;
3231 exec->state = exec->rollbacks[exec->nbRollbacks].state;
3232 exec->index = exec->rollbacks[exec->nbRollbacks].index;
3233 exec->transno = exec->rollbacks[exec->nbRollbacks].nextbranch;
3234 if (exec->comp->nbCounters > 0) {
3235 if (exec->rollbacks[exec->nbRollbacks].counts == NULL) {
3236 fprintf(stderr, "exec save: allocation failed");
3237 exec->status = -6;
3238 return;
3239 }
Gaurav2671b012013-09-11 14:59:06 +08003240 if (exec->counts) {
3241 memcpy(exec->counts, exec->rollbacks[exec->nbRollbacks].counts,
Daniel Veillard4255d502002-04-16 15:50:10 +00003242 exec->comp->nbCounters * sizeof(int));
Gaurav2671b012013-09-11 14:59:06 +08003243 }
Daniel Veillard4255d502002-04-16 15:50:10 +00003244 }
3245
3246#ifdef DEBUG_REGEXP_EXEC
3247 printf("restored ");
3248 xmlFARegDebugExec(exec);
3249#endif
3250}
3251
3252/************************************************************************
Daniel Veillardf8e3db02012-09-11 13:26:36 +08003253 * *
William M. Brackddf71d62004-05-06 04:17:26 +00003254 * Verifier, running an input against a compiled regexp *
Daniel Veillardf8e3db02012-09-11 13:26:36 +08003255 * *
Daniel Veillard4255d502002-04-16 15:50:10 +00003256 ************************************************************************/
3257
3258static int
3259xmlFARegExec(xmlRegexpPtr comp, const xmlChar *content) {
3260 xmlRegExecCtxt execval;
3261 xmlRegExecCtxtPtr exec = &execval;
Daniel Veillard567a45b2005-10-18 19:11:55 +00003262 int ret, codepoint = 0, len, deter;
Daniel Veillard4255d502002-04-16 15:50:10 +00003263
3264 exec->inputString = content;
3265 exec->index = 0;
Daniel Veillard94cc1032005-09-15 13:09:00 +00003266 exec->nbPush = 0;
Daniel Veillard4255d502002-04-16 15:50:10 +00003267 exec->determinist = 1;
3268 exec->maxRollbacks = 0;
3269 exec->nbRollbacks = 0;
3270 exec->rollbacks = NULL;
3271 exec->status = 0;
3272 exec->comp = comp;
3273 exec->state = comp->states[0];
3274 exec->transno = 0;
3275 exec->transcount = 0;
Daniel Veillardf2a12832003-11-24 13:04:35 +00003276 exec->inputStack = NULL;
3277 exec->inputStackMax = 0;
Daniel Veillard4255d502002-04-16 15:50:10 +00003278 if (comp->nbCounters > 0) {
3279 exec->counts = (int *) xmlMalloc(comp->nbCounters * sizeof(int));
Daniel Veillardff46a042003-10-08 08:53:17 +00003280 if (exec->counts == NULL) {
3281 xmlRegexpErrMemory(NULL, "running regexp");
Daniel Veillard4255d502002-04-16 15:50:10 +00003282 return(-1);
Daniel Veillardff46a042003-10-08 08:53:17 +00003283 }
Daniel Veillard4255d502002-04-16 15:50:10 +00003284 memset(exec->counts, 0, comp->nbCounters * sizeof(int));
3285 } else
3286 exec->counts = NULL;
Daniel Veillard40851d02012-08-17 20:34:05 +08003287 while ((exec->status == 0) && (exec->state != NULL) &&
Daniel Veillard4255d502002-04-16 15:50:10 +00003288 ((exec->inputString[exec->index] != 0) ||
Daniel Veillardad559982008-05-12 13:15:35 +00003289 ((exec->state != NULL) &&
3290 (exec->state->type != XML_REGEXP_FINAL_STATE)))) {
Daniel Veillard4255d502002-04-16 15:50:10 +00003291 xmlRegTransPtr trans;
3292 xmlRegAtomPtr atom;
3293
3294 /*
William M. Brack0e00b282004-04-26 15:40:47 +00003295 * If end of input on non-terminal state, rollback, however we may
Daniel Veillard4255d502002-04-16 15:50:10 +00003296 * still have epsilon like transition for counted transitions
William M. Brack0e00b282004-04-26 15:40:47 +00003297 * on counters, in that case don't break too early. Additionally,
3298 * if we are working on a range like "AB{0,2}", where B is not present,
3299 * we don't want to break.
Daniel Veillard4255d502002-04-16 15:50:10 +00003300 */
Daniel Veillard11ce4002006-03-10 00:36:23 +00003301 len = 1;
William M. Brack0e00b282004-04-26 15:40:47 +00003302 if ((exec->inputString[exec->index] == 0) && (exec->counts == NULL)) {
William M. Brackddf71d62004-05-06 04:17:26 +00003303 /*
3304 * if there is a transition, we must check if
3305 * atom allows minOccurs of 0
3306 */
3307 if (exec->transno < exec->state->nbTrans) {
William M. Brack0e00b282004-04-26 15:40:47 +00003308 trans = &exec->state->trans[exec->transno];
3309 if (trans->to >=0) {
3310 atom = trans->atom;
3311 if (!((atom->min == 0) && (atom->max > 0)))
3312 goto rollback;
3313 }
3314 } else
3315 goto rollback;
3316 }
Daniel Veillard4255d502002-04-16 15:50:10 +00003317
3318 exec->transcount = 0;
3319 for (;exec->transno < exec->state->nbTrans;exec->transno++) {
3320 trans = &exec->state->trans[exec->transno];
3321 if (trans->to < 0)
3322 continue;
3323 atom = trans->atom;
3324 ret = 0;
Daniel Veillard567a45b2005-10-18 19:11:55 +00003325 deter = 1;
Daniel Veillard4255d502002-04-16 15:50:10 +00003326 if (trans->count >= 0) {
3327 int count;
3328 xmlRegCounterPtr counter;
3329
Daniel Veillard11ce4002006-03-10 00:36:23 +00003330 if (exec->counts == NULL) {
3331 exec->status = -1;
3332 goto error;
3333 }
Daniel Veillard4255d502002-04-16 15:50:10 +00003334 /*
3335 * A counted transition.
3336 */
3337
3338 count = exec->counts[trans->count];
3339 counter = &exec->comp->counters[trans->count];
3340#ifdef DEBUG_REGEXP_EXEC
3341 printf("testing count %d: val %d, min %d, max %d\n",
3342 trans->count, count, counter->min, counter->max);
3343#endif
3344 ret = ((count >= counter->min) && (count <= counter->max));
Daniel Veillard567a45b2005-10-18 19:11:55 +00003345 if ((ret) && (counter->min != counter->max))
3346 deter = 0;
Daniel Veillard4255d502002-04-16 15:50:10 +00003347 } else if (atom == NULL) {
3348 fprintf(stderr, "epsilon transition left at runtime\n");
3349 exec->status = -2;
3350 break;
3351 } else if (exec->inputString[exec->index] != 0) {
3352 codepoint = CUR_SCHAR(&(exec->inputString[exec->index]), len);
3353 ret = xmlRegCheckCharacter(atom, codepoint);
William M. Brack0e00b282004-04-26 15:40:47 +00003354 if ((ret == 1) && (atom->min >= 0) && (atom->max > 0)) {
Daniel Veillard4255d502002-04-16 15:50:10 +00003355 xmlRegStatePtr to = comp->states[trans->to];
3356
3357 /*
3358 * this is a multiple input sequence
Daniel Veillardfc6eca02005-11-01 15:24:02 +00003359 * If there is a counter associated increment it now.
3360 * before potentially saving and rollback
Daniel Veillardc821e032007-08-28 17:33:45 +00003361 * do not increment if the counter is already over the
3362 * maximum limit in which case get to next transition
Daniel Veillard4255d502002-04-16 15:50:10 +00003363 */
Daniel Veillardfc6eca02005-11-01 15:24:02 +00003364 if (trans->counter >= 0) {
Daniel Veillardc821e032007-08-28 17:33:45 +00003365 xmlRegCounterPtr counter;
3366
3367 if ((exec->counts == NULL) ||
3368 (exec->comp == NULL) ||
3369 (exec->comp->counters == NULL)) {
Daniel Veillard11ce4002006-03-10 00:36:23 +00003370 exec->status = -1;
3371 goto error;
3372 }
Daniel Veillardc821e032007-08-28 17:33:45 +00003373 counter = &exec->comp->counters[trans->counter];
3374 if (exec->counts[trans->counter] >= counter->max)
3375 continue; /* for loop on transitions */
3376
Daniel Veillardfc6eca02005-11-01 15:24:02 +00003377#ifdef DEBUG_REGEXP_EXEC
3378 printf("Increasing count %d\n", trans->counter);
3379#endif
3380 exec->counts[trans->counter]++;
3381 }
Daniel Veillard4255d502002-04-16 15:50:10 +00003382 if (exec->state->nbTrans > exec->transno + 1) {
3383 xmlFARegExecSave(exec);
3384 }
3385 exec->transcount = 1;
3386 do {
3387 /*
3388 * Try to progress as much as possible on the input
3389 */
3390 if (exec->transcount == atom->max) {
3391 break;
3392 }
3393 exec->index += len;
3394 /*
3395 * End of input: stop here
3396 */
3397 if (exec->inputString[exec->index] == 0) {
3398 exec->index -= len;
3399 break;
3400 }
3401 if (exec->transcount >= atom->min) {
3402 int transno = exec->transno;
3403 xmlRegStatePtr state = exec->state;
3404
3405 /*
3406 * The transition is acceptable save it
3407 */
3408 exec->transno = -1; /* trick */
3409 exec->state = to;
3410 xmlFARegExecSave(exec);
3411 exec->transno = transno;
3412 exec->state = state;
3413 }
3414 codepoint = CUR_SCHAR(&(exec->inputString[exec->index]),
3415 len);
3416 ret = xmlRegCheckCharacter(atom, codepoint);
3417 exec->transcount++;
3418 } while (ret == 1);
3419 if (exec->transcount < atom->min)
3420 ret = 0;
3421
3422 /*
3423 * If the last check failed but one transition was found
3424 * possible, rollback
3425 */
3426 if (ret < 0)
3427 ret = 0;
3428 if (ret == 0) {
3429 goto rollback;
3430 }
Daniel Veillardfc6eca02005-11-01 15:24:02 +00003431 if (trans->counter >= 0) {
Daniel Veillard11ce4002006-03-10 00:36:23 +00003432 if (exec->counts == NULL) {
3433 exec->status = -1;
3434 goto error;
3435 }
Daniel Veillardfc6eca02005-11-01 15:24:02 +00003436#ifdef DEBUG_REGEXP_EXEC
3437 printf("Decreasing count %d\n", trans->counter);
3438#endif
3439 exec->counts[trans->counter]--;
3440 }
William M. Brack0e00b282004-04-26 15:40:47 +00003441 } else if ((ret == 0) && (atom->min == 0) && (atom->max > 0)) {
3442 /*
3443 * we don't match on the codepoint, but minOccurs of 0
3444 * says that's ok. Setting len to 0 inhibits stepping
3445 * over the codepoint.
3446 */
3447 exec->transcount = 1;
3448 len = 0;
3449 ret = 1;
Daniel Veillard4255d502002-04-16 15:50:10 +00003450 }
William M. Brack0e00b282004-04-26 15:40:47 +00003451 } else if ((atom->min == 0) && (atom->max > 0)) {
3452 /* another spot to match when minOccurs is 0 */
3453 exec->transcount = 1;
3454 len = 0;
3455 ret = 1;
Daniel Veillard4255d502002-04-16 15:50:10 +00003456 }
3457 if (ret == 1) {
Daniel Veillard567a45b2005-10-18 19:11:55 +00003458 if ((trans->nd == 1) ||
3459 ((trans->count >= 0) && (deter == 0) &&
3460 (exec->state->nbTrans > exec->transno + 1))) {
Daniel Veillardaa622012005-10-20 15:55:25 +00003461#ifdef DEBUG_REGEXP_EXEC
3462 if (trans->nd == 1)
3463 printf("Saving on nd transition atom %d for %c at %d\n",
3464 trans->atom->no, codepoint, exec->index);
3465 else
3466 printf("Saving on counted transition count %d for %c at %d\n",
3467 trans->count, codepoint, exec->index);
3468#endif
Daniel Veillard4255d502002-04-16 15:50:10 +00003469 xmlFARegExecSave(exec);
3470 }
3471 if (trans->counter >= 0) {
Daniel Veillardc821e032007-08-28 17:33:45 +00003472 xmlRegCounterPtr counter;
3473
3474 /* make sure we don't go over the counter maximum value */
3475 if ((exec->counts == NULL) ||
3476 (exec->comp == NULL) ||
3477 (exec->comp->counters == NULL)) {
3478 exec->status = -1;
Daniel Veillard11ce4002006-03-10 00:36:23 +00003479 goto error;
3480 }
Daniel Veillardc821e032007-08-28 17:33:45 +00003481 counter = &exec->comp->counters[trans->counter];
3482 if (exec->counts[trans->counter] >= counter->max)
3483 continue; /* for loop on transitions */
Daniel Veillard4255d502002-04-16 15:50:10 +00003484#ifdef DEBUG_REGEXP_EXEC
3485 printf("Increasing count %d\n", trans->counter);
3486#endif
3487 exec->counts[trans->counter]++;
3488 }
Daniel Veillard10752282005-08-08 13:05:13 +00003489 if ((trans->count >= 0) &&
3490 (trans->count < REGEXP_ALL_COUNTER)) {
Daniel Veillard11ce4002006-03-10 00:36:23 +00003491 if (exec->counts == NULL) {
3492 exec->status = -1;
3493 goto error;
3494 }
Daniel Veillard10752282005-08-08 13:05:13 +00003495#ifdef DEBUG_REGEXP_EXEC
3496 printf("resetting count %d on transition\n",
3497 trans->count);
3498#endif
3499 exec->counts[trans->count] = 0;
3500 }
Daniel Veillard4255d502002-04-16 15:50:10 +00003501#ifdef DEBUG_REGEXP_EXEC
3502 printf("entering state %d\n", trans->to);
3503#endif
3504 exec->state = comp->states[trans->to];
3505 exec->transno = 0;
3506 if (trans->atom != NULL) {
3507 exec->index += len;
3508 }
3509 goto progress;
3510 } else if (ret < 0) {
3511 exec->status = -4;
3512 break;
3513 }
3514 }
3515 if ((exec->transno != 0) || (exec->state->nbTrans == 0)) {
3516rollback:
3517 /*
3518 * Failed to find a way out
3519 */
3520 exec->determinist = 0;
Daniel Veillardaa622012005-10-20 15:55:25 +00003521#ifdef DEBUG_REGEXP_EXEC
3522 printf("rollback from state %d on %d:%c\n", exec->state->no,
3523 codepoint,codepoint);
3524#endif
Daniel Veillard4255d502002-04-16 15:50:10 +00003525 xmlFARegExecRollBack(exec);
3526 }
3527progress:
3528 continue;
3529 }
Daniel Veillard11ce4002006-03-10 00:36:23 +00003530error:
Daniel Veillard4255d502002-04-16 15:50:10 +00003531 if (exec->rollbacks != NULL) {
3532 if (exec->counts != NULL) {
3533 int i;
3534
3535 for (i = 0;i < exec->maxRollbacks;i++)
3536 if (exec->rollbacks[i].counts != NULL)
3537 xmlFree(exec->rollbacks[i].counts);
3538 }
3539 xmlFree(exec->rollbacks);
3540 }
Daniel Veillard40851d02012-08-17 20:34:05 +08003541 if (exec->state == NULL)
3542 return(-1);
Daniel Veillard4255d502002-04-16 15:50:10 +00003543 if (exec->counts != NULL)
3544 xmlFree(exec->counts);
3545 if (exec->status == 0)
3546 return(1);
Daniel Veillard94cc1032005-09-15 13:09:00 +00003547 if (exec->status == -1) {
3548 if (exec->nbPush > MAX_PUSH)
3549 return(-1);
Daniel Veillard4255d502002-04-16 15:50:10 +00003550 return(0);
Daniel Veillard94cc1032005-09-15 13:09:00 +00003551 }
Daniel Veillard4255d502002-04-16 15:50:10 +00003552 return(exec->status);
3553}
3554
3555/************************************************************************
Daniel Veillardf8e3db02012-09-11 13:26:36 +08003556 * *
William M. Brackddf71d62004-05-06 04:17:26 +00003557 * Progressive interface to the verifier one atom at a time *
Daniel Veillardf8e3db02012-09-11 13:26:36 +08003558 * *
Daniel Veillard4255d502002-04-16 15:50:10 +00003559 ************************************************************************/
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003560#ifdef DEBUG_ERR
3561static void testerr(xmlRegExecCtxtPtr exec);
3562#endif
Daniel Veillard4255d502002-04-16 15:50:10 +00003563
3564/**
Daniel Veillard01c13b52002-12-10 15:19:08 +00003565 * xmlRegNewExecCtxt:
Daniel Veillard4255d502002-04-16 15:50:10 +00003566 * @comp: a precompiled regular expression
3567 * @callback: a callback function used for handling progresses in the
3568 * automata matching phase
3569 * @data: the context data associated to the callback in this context
3570 *
3571 * Build a context used for progressive evaluation of a regexp.
Daniel Veillard01c13b52002-12-10 15:19:08 +00003572 *
3573 * Returns the new context
Daniel Veillard4255d502002-04-16 15:50:10 +00003574 */
3575xmlRegExecCtxtPtr
3576xmlRegNewExecCtxt(xmlRegexpPtr comp, xmlRegExecCallbacks callback, void *data) {
3577 xmlRegExecCtxtPtr exec;
3578
3579 if (comp == NULL)
3580 return(NULL);
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00003581 if ((comp->compact == NULL) && (comp->states == NULL))
3582 return(NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +00003583 exec = (xmlRegExecCtxtPtr) xmlMalloc(sizeof(xmlRegExecCtxt));
3584 if (exec == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00003585 xmlRegexpErrMemory(NULL, "creating execution context");
Daniel Veillard4255d502002-04-16 15:50:10 +00003586 return(NULL);
3587 }
3588 memset(exec, 0, sizeof(xmlRegExecCtxt));
3589 exec->inputString = NULL;
3590 exec->index = 0;
3591 exec->determinist = 1;
3592 exec->maxRollbacks = 0;
3593 exec->nbRollbacks = 0;
3594 exec->rollbacks = NULL;
3595 exec->status = 0;
3596 exec->comp = comp;
Daniel Veillard23e73572002-09-19 19:56:43 +00003597 if (comp->compact == NULL)
3598 exec->state = comp->states[0];
Daniel Veillard4255d502002-04-16 15:50:10 +00003599 exec->transno = 0;
3600 exec->transcount = 0;
3601 exec->callback = callback;
3602 exec->data = data;
3603 if (comp->nbCounters > 0) {
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003604 /*
3605 * For error handling, exec->counts is allocated twice the size
3606 * the second half is used to store the data in case of rollback
3607 */
3608 exec->counts = (int *) xmlMalloc(comp->nbCounters * sizeof(int)
3609 * 2);
Daniel Veillard4255d502002-04-16 15:50:10 +00003610 if (exec->counts == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00003611 xmlRegexpErrMemory(NULL, "creating execution context");
Daniel Veillard4255d502002-04-16 15:50:10 +00003612 xmlFree(exec);
3613 return(NULL);
3614 }
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003615 memset(exec->counts, 0, comp->nbCounters * sizeof(int) * 2);
3616 exec->errCounts = &exec->counts[comp->nbCounters];
3617 } else {
Daniel Veillard4255d502002-04-16 15:50:10 +00003618 exec->counts = NULL;
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003619 exec->errCounts = NULL;
3620 }
Daniel Veillard4255d502002-04-16 15:50:10 +00003621 exec->inputStackMax = 0;
3622 exec->inputStackNr = 0;
3623 exec->inputStack = NULL;
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003624 exec->errStateNo = -1;
3625 exec->errString = NULL;
Daniel Veillard94cc1032005-09-15 13:09:00 +00003626 exec->nbPush = 0;
Daniel Veillard4255d502002-04-16 15:50:10 +00003627 return(exec);
3628}
3629
3630/**
3631 * xmlRegFreeExecCtxt:
Haibo Huangcfd91dc2020-07-30 23:01:33 -07003632 * @exec: a regular expression evaluation context
Daniel Veillard4255d502002-04-16 15:50:10 +00003633 *
Haibo Huangcfd91dc2020-07-30 23:01:33 -07003634 * Free the structures associated to a regular expression evaluation context.
Daniel Veillard4255d502002-04-16 15:50:10 +00003635 */
3636void
3637xmlRegFreeExecCtxt(xmlRegExecCtxtPtr exec) {
3638 if (exec == NULL)
3639 return;
3640
3641 if (exec->rollbacks != NULL) {
3642 if (exec->counts != NULL) {
3643 int i;
3644
3645 for (i = 0;i < exec->maxRollbacks;i++)
3646 if (exec->rollbacks[i].counts != NULL)
3647 xmlFree(exec->rollbacks[i].counts);
3648 }
3649 xmlFree(exec->rollbacks);
3650 }
3651 if (exec->counts != NULL)
3652 xmlFree(exec->counts);
3653 if (exec->inputStack != NULL) {
3654 int i;
3655
Daniel Veillard32370232002-10-16 14:08:14 +00003656 for (i = 0;i < exec->inputStackNr;i++) {
3657 if (exec->inputStack[i].value != NULL)
3658 xmlFree(exec->inputStack[i].value);
3659 }
Daniel Veillard4255d502002-04-16 15:50:10 +00003660 xmlFree(exec->inputStack);
3661 }
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003662 if (exec->errString != NULL)
3663 xmlFree(exec->errString);
Daniel Veillard4255d502002-04-16 15:50:10 +00003664 xmlFree(exec);
3665}
3666
3667static void
3668xmlFARegExecSaveInputString(xmlRegExecCtxtPtr exec, const xmlChar *value,
3669 void *data) {
3670#ifdef DEBUG_PUSH
3671 printf("saving value: %d:%s\n", exec->inputStackNr, value);
3672#endif
3673 if (exec->inputStackMax == 0) {
3674 exec->inputStackMax = 4;
Daniel Veillardf8e3db02012-09-11 13:26:36 +08003675 exec->inputStack = (xmlRegInputTokenPtr)
Daniel Veillard4255d502002-04-16 15:50:10 +00003676 xmlMalloc(exec->inputStackMax * sizeof(xmlRegInputToken));
3677 if (exec->inputStack == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00003678 xmlRegexpErrMemory(NULL, "pushing input string");
Daniel Veillard4255d502002-04-16 15:50:10 +00003679 exec->inputStackMax = 0;
3680 return;
3681 }
3682 } else if (exec->inputStackNr + 1 >= exec->inputStackMax) {
3683 xmlRegInputTokenPtr tmp;
3684
3685 exec->inputStackMax *= 2;
3686 tmp = (xmlRegInputTokenPtr) xmlRealloc(exec->inputStack,
3687 exec->inputStackMax * sizeof(xmlRegInputToken));
3688 if (tmp == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00003689 xmlRegexpErrMemory(NULL, "pushing input string");
Daniel Veillard4255d502002-04-16 15:50:10 +00003690 exec->inputStackMax /= 2;
3691 return;
3692 }
3693 exec->inputStack = tmp;
3694 }
3695 exec->inputStack[exec->inputStackNr].value = xmlStrdup(value);
3696 exec->inputStack[exec->inputStackNr].data = data;
3697 exec->inputStackNr++;
3698 exec->inputStack[exec->inputStackNr].value = NULL;
3699 exec->inputStack[exec->inputStackNr].data = NULL;
3700}
3701
Daniel Veillardc0826a72004-08-10 14:17:33 +00003702/**
3703 * xmlRegStrEqualWildcard:
Daniel Veillardf8e3db02012-09-11 13:26:36 +08003704 * @expStr: the string to be evaluated
Daniel Veillardc0826a72004-08-10 14:17:33 +00003705 * @valStr: the validation string
3706 *
3707 * Checks if both strings are equal or have the same content. "*"
Haibo Huangcfd91dc2020-07-30 23:01:33 -07003708 * can be used as a wildcard in @valStr; "|" is used as a separator of
Daniel Veillardc0826a72004-08-10 14:17:33 +00003709 * substrings in both @expStr and @valStr.
3710 *
3711 * Returns 1 if the comparison is satisfied and the number of substrings
3712 * is equal, 0 otherwise.
3713 */
3714
3715static int
3716xmlRegStrEqualWildcard(const xmlChar *expStr, const xmlChar *valStr) {
3717 if (expStr == valStr) return(1);
3718 if (expStr == NULL) return(0);
3719 if (valStr == NULL) return(0);
3720 do {
3721 /*
3722 * Eval if we have a wildcard for the current item.
3723 */
3724 if (*expStr != *valStr) {
Daniel Veillard4f82c8a2005-08-09 21:40:08 +00003725 /* if one of them starts with a wildcard make valStr be it */
3726 if (*valStr == '*') {
3727 const xmlChar *tmp;
3728
3729 tmp = valStr;
3730 valStr = expStr;
3731 expStr = tmp;
3732 }
Daniel Veillardc0826a72004-08-10 14:17:33 +00003733 if ((*valStr != 0) && (*expStr != 0) && (*expStr++ == '*')) {
3734 do {
3735 if (*valStr == XML_REG_STRING_SEPARATOR)
3736 break;
Kasimier T. Buchcikc0e833f2005-04-19 15:02:20 +00003737 valStr++;
Daniel Veillardc0826a72004-08-10 14:17:33 +00003738 } while (*valStr != 0);
3739 continue;
3740 } else
3741 return(0);
3742 }
Kasimier T. Buchcikc0e833f2005-04-19 15:02:20 +00003743 expStr++;
3744 valStr++;
Daniel Veillardc0826a72004-08-10 14:17:33 +00003745 } while (*valStr != 0);
3746 if (*expStr != 0)
3747 return (0);
3748 else
3749 return (1);
3750}
Daniel Veillard4255d502002-04-16 15:50:10 +00003751
3752/**
Daniel Veillard23e73572002-09-19 19:56:43 +00003753 * xmlRegCompactPushString:
3754 * @exec: a regexp execution context
3755 * @comp: the precompiled exec with a compact table
3756 * @value: a string token input
3757 * @data: data associated to the token to reuse in callbacks
3758 *
3759 * Push one input token in the execution context
3760 *
3761 * Returns: 1 if the regexp reached a final state, 0 if non-final, and
3762 * a negative value in case of error.
3763 */
3764static int
3765xmlRegCompactPushString(xmlRegExecCtxtPtr exec,
3766 xmlRegexpPtr comp,
3767 const xmlChar *value,
3768 void *data) {
3769 int state = exec->index;
3770 int i, target;
3771
3772 if ((comp == NULL) || (comp->compact == NULL) || (comp->stringMap == NULL))
3773 return(-1);
Daniel Veillardf8e3db02012-09-11 13:26:36 +08003774
Daniel Veillard23e73572002-09-19 19:56:43 +00003775 if (value == NULL) {
3776 /*
3777 * are we at a final state ?
3778 */
3779 if (comp->compact[state * (comp->nbstrings + 1)] ==
3780 XML_REGEXP_FINAL_STATE)
3781 return(1);
3782 return(0);
3783 }
3784
3785#ifdef DEBUG_PUSH
3786 printf("value pushed: %s\n", value);
3787#endif
3788
3789 /*
William M. Brackddf71d62004-05-06 04:17:26 +00003790 * Examine all outside transitions from current state
Daniel Veillard23e73572002-09-19 19:56:43 +00003791 */
3792 for (i = 0;i < comp->nbstrings;i++) {
3793 target = comp->compact[state * (comp->nbstrings + 1) + i + 1];
3794 if ((target > 0) && (target <= comp->nbstates)) {
Daniel Veillardf8e3db02012-09-11 13:26:36 +08003795 target--; /* to avoid 0 */
Daniel Veillardc0826a72004-08-10 14:17:33 +00003796 if (xmlRegStrEqualWildcard(comp->stringMap[i], value)) {
Daniel Veillardf8e3db02012-09-11 13:26:36 +08003797 exec->index = target;
Daniel Veillard118aed72002-09-24 14:13:13 +00003798 if ((exec->callback != NULL) && (comp->transdata != NULL)) {
3799 exec->callback(exec->data, value,
3800 comp->transdata[state * comp->nbstrings + i], data);
3801 }
Daniel Veillard23e73572002-09-19 19:56:43 +00003802#ifdef DEBUG_PUSH
3803 printf("entering state %d\n", target);
3804#endif
3805 if (comp->compact[target * (comp->nbstrings + 1)] ==
Daniel Veillardcc026dc2005-01-12 13:21:17 +00003806 XML_REGEXP_SINK_STATE)
3807 goto error;
3808
3809 if (comp->compact[target * (comp->nbstrings + 1)] ==
Daniel Veillard23e73572002-09-19 19:56:43 +00003810 XML_REGEXP_FINAL_STATE)
3811 return(1);
3812 return(0);
3813 }
3814 }
3815 }
3816 /*
3817 * Failed to find an exit transition out from current state for the
3818 * current token
3819 */
3820#ifdef DEBUG_PUSH
3821 printf("failed to find a transition for %s on state %d\n", value, state);
3822#endif
Daniel Veillardcc026dc2005-01-12 13:21:17 +00003823error:
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003824 if (exec->errString != NULL)
3825 xmlFree(exec->errString);
3826 exec->errString = xmlStrdup(value);
3827 exec->errStateNo = state;
Daniel Veillard23e73572002-09-19 19:56:43 +00003828 exec->status = -1;
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003829#ifdef DEBUG_ERR
3830 testerr(exec);
3831#endif
Daniel Veillard23e73572002-09-19 19:56:43 +00003832 return(-1);
3833}
3834
3835/**
Daniel Veillard6e65e152005-08-09 11:09:52 +00003836 * xmlRegExecPushStringInternal:
Daniel Veillardea7751d2002-12-20 00:16:24 +00003837 * @exec: a regexp execution context or NULL to indicate the end
Daniel Veillard4255d502002-04-16 15:50:10 +00003838 * @value: a string token input
3839 * @data: data associated to the token to reuse in callbacks
Daniel Veillard6e65e152005-08-09 11:09:52 +00003840 * @compound: value was assembled from 2 strings
Daniel Veillard4255d502002-04-16 15:50:10 +00003841 *
3842 * Push one input token in the execution context
3843 *
3844 * Returns: 1 if the regexp reached a final state, 0 if non-final, and
3845 * a negative value in case of error.
3846 */
Daniel Veillard6e65e152005-08-09 11:09:52 +00003847static int
3848xmlRegExecPushStringInternal(xmlRegExecCtxtPtr exec, const xmlChar *value,
3849 void *data, int compound) {
Daniel Veillard4255d502002-04-16 15:50:10 +00003850 xmlRegTransPtr trans;
3851 xmlRegAtomPtr atom;
3852 int ret;
3853 int final = 0;
Daniel Veillard90700152005-01-08 22:05:09 +00003854 int progress = 1;
Daniel Veillard4255d502002-04-16 15:50:10 +00003855
3856 if (exec == NULL)
3857 return(-1);
Daniel Veillard23e73572002-09-19 19:56:43 +00003858 if (exec->comp == NULL)
3859 return(-1);
Daniel Veillard4255d502002-04-16 15:50:10 +00003860 if (exec->status != 0)
3861 return(exec->status);
3862
Daniel Veillard23e73572002-09-19 19:56:43 +00003863 if (exec->comp->compact != NULL)
3864 return(xmlRegCompactPushString(exec, exec->comp, value, data));
3865
Daniel Veillard4255d502002-04-16 15:50:10 +00003866 if (value == NULL) {
3867 if (exec->state->type == XML_REGEXP_FINAL_STATE)
3868 return(1);
3869 final = 1;
3870 }
3871
3872#ifdef DEBUG_PUSH
3873 printf("value pushed: %s\n", value);
3874#endif
3875 /*
3876 * If we have an active rollback stack push the new value there
3877 * and get back to where we were left
3878 */
3879 if ((value != NULL) && (exec->inputStackNr > 0)) {
3880 xmlFARegExecSaveInputString(exec, value, data);
3881 value = exec->inputStack[exec->index].value;
3882 data = exec->inputStack[exec->index].data;
3883#ifdef DEBUG_PUSH
3884 printf("value loaded: %s\n", value);
3885#endif
3886 }
3887
3888 while ((exec->status == 0) &&
3889 ((value != NULL) ||
3890 ((final == 1) &&
3891 (exec->state->type != XML_REGEXP_FINAL_STATE)))) {
3892
3893 /*
3894 * End of input on non-terminal state, rollback, however we may
3895 * still have epsilon like transition for counted transitions
3896 * on counters, in that case don't break too early.
3897 */
Daniel Veillardb509f152002-04-17 16:28:10 +00003898 if ((value == NULL) && (exec->counts == NULL))
Daniel Veillard4255d502002-04-16 15:50:10 +00003899 goto rollback;
3900
3901 exec->transcount = 0;
3902 for (;exec->transno < exec->state->nbTrans;exec->transno++) {
3903 trans = &exec->state->trans[exec->transno];
3904 if (trans->to < 0)
3905 continue;
3906 atom = trans->atom;
3907 ret = 0;
Daniel Veillard441bc322002-04-20 17:38:48 +00003908 if (trans->count == REGEXP_ALL_LAX_COUNTER) {
3909 int i;
3910 int count;
3911 xmlRegTransPtr t;
3912 xmlRegCounterPtr counter;
3913
3914 ret = 0;
3915
3916#ifdef DEBUG_PUSH
3917 printf("testing all lax %d\n", trans->count);
3918#endif
3919 /*
3920 * Check all counted transitions from the current state
3921 */
3922 if ((value == NULL) && (final)) {
3923 ret = 1;
3924 } else if (value != NULL) {
3925 for (i = 0;i < exec->state->nbTrans;i++) {
3926 t = &exec->state->trans[i];
3927 if ((t->counter < 0) || (t == trans))
3928 continue;
3929 counter = &exec->comp->counters[t->counter];
3930 count = exec->counts[t->counter];
Daniel Veillardf8e3db02012-09-11 13:26:36 +08003931 if ((count < counter->max) &&
Daniel Veillard441bc322002-04-20 17:38:48 +00003932 (t->atom != NULL) &&
3933 (xmlStrEqual(value, t->atom->valuep))) {
3934 ret = 0;
3935 break;
3936 }
3937 if ((count >= counter->min) &&
3938 (count < counter->max) &&
Daniel Veillard11ce4002006-03-10 00:36:23 +00003939 (t->atom != NULL) &&
Daniel Veillard441bc322002-04-20 17:38:48 +00003940 (xmlStrEqual(value, t->atom->valuep))) {
3941 ret = 1;
3942 break;
3943 }
3944 }
3945 }
3946 } else if (trans->count == REGEXP_ALL_COUNTER) {
Daniel Veillard8a001f62002-04-20 07:24:11 +00003947 int i;
3948 int count;
3949 xmlRegTransPtr t;
3950 xmlRegCounterPtr counter;
3951
3952 ret = 1;
3953
3954#ifdef DEBUG_PUSH
3955 printf("testing all %d\n", trans->count);
3956#endif
3957 /*
3958 * Check all counted transitions from the current state
3959 */
3960 for (i = 0;i < exec->state->nbTrans;i++) {
3961 t = &exec->state->trans[i];
3962 if ((t->counter < 0) || (t == trans))
3963 continue;
3964 counter = &exec->comp->counters[t->counter];
3965 count = exec->counts[t->counter];
3966 if ((count < counter->min) || (count > counter->max)) {
3967 ret = 0;
3968 break;
3969 }
3970 }
3971 } else if (trans->count >= 0) {
Daniel Veillard4255d502002-04-16 15:50:10 +00003972 int count;
3973 xmlRegCounterPtr counter;
3974
3975 /*
3976 * A counted transition.
3977 */
3978
3979 count = exec->counts[trans->count];
3980 counter = &exec->comp->counters[trans->count];
3981#ifdef DEBUG_PUSH
3982 printf("testing count %d: val %d, min %d, max %d\n",
3983 trans->count, count, counter->min, counter->max);
3984#endif
3985 ret = ((count >= counter->min) && (count <= counter->max));
3986 } else if (atom == NULL) {
3987 fprintf(stderr, "epsilon transition left at runtime\n");
3988 exec->status = -2;
3989 break;
3990 } else if (value != NULL) {
Daniel Veillardc0826a72004-08-10 14:17:33 +00003991 ret = xmlRegStrEqualWildcard(atom->valuep, value);
Daniel Veillard6e65e152005-08-09 11:09:52 +00003992 if (atom->neg) {
Daniel Veillard9efc4762005-07-19 14:33:55 +00003993 ret = !ret;
Daniel Veillard6e65e152005-08-09 11:09:52 +00003994 if (!compound)
3995 ret = 0;
3996 }
Daniel Veillard441bc322002-04-20 17:38:48 +00003997 if ((ret == 1) && (trans->counter >= 0)) {
3998 xmlRegCounterPtr counter;
3999 int count;
4000
4001 count = exec->counts[trans->counter];
4002 counter = &exec->comp->counters[trans->counter];
4003 if (count >= counter->max)
4004 ret = 0;
4005 }
4006
Daniel Veillard4255d502002-04-16 15:50:10 +00004007 if ((ret == 1) && (atom->min > 0) && (atom->max > 0)) {
4008 xmlRegStatePtr to = exec->comp->states[trans->to];
4009
4010 /*
4011 * this is a multiple input sequence
4012 */
4013 if (exec->state->nbTrans > exec->transno + 1) {
4014 if (exec->inputStackNr <= 0) {
4015 xmlFARegExecSaveInputString(exec, value, data);
4016 }
4017 xmlFARegExecSave(exec);
4018 }
4019 exec->transcount = 1;
4020 do {
4021 /*
4022 * Try to progress as much as possible on the input
4023 */
4024 if (exec->transcount == atom->max) {
4025 break;
4026 }
4027 exec->index++;
4028 value = exec->inputStack[exec->index].value;
4029 data = exec->inputStack[exec->index].data;
4030#ifdef DEBUG_PUSH
4031 printf("value loaded: %s\n", value);
4032#endif
4033
4034 /*
4035 * End of input: stop here
4036 */
4037 if (value == NULL) {
4038 exec->index --;
4039 break;
4040 }
4041 if (exec->transcount >= atom->min) {
4042 int transno = exec->transno;
4043 xmlRegStatePtr state = exec->state;
4044
4045 /*
4046 * The transition is acceptable save it
4047 */
4048 exec->transno = -1; /* trick */
4049 exec->state = to;
4050 if (exec->inputStackNr <= 0) {
4051 xmlFARegExecSaveInputString(exec, value, data);
4052 }
4053 xmlFARegExecSave(exec);
4054 exec->transno = transno;
4055 exec->state = state;
4056 }
4057 ret = xmlStrEqual(value, atom->valuep);
4058 exec->transcount++;
4059 } while (ret == 1);
4060 if (exec->transcount < atom->min)
4061 ret = 0;
4062
4063 /*
4064 * If the last check failed but one transition was found
4065 * possible, rollback
4066 */
4067 if (ret < 0)
4068 ret = 0;
4069 if (ret == 0) {
4070 goto rollback;
4071 }
4072 }
4073 }
4074 if (ret == 1) {
William M. Brack98873952003-12-26 06:03:14 +00004075 if ((exec->callback != NULL) && (atom != NULL) &&
4076 (data != NULL)) {
Daniel Veillard4255d502002-04-16 15:50:10 +00004077 exec->callback(exec->data, atom->valuep,
4078 atom->data, data);
4079 }
4080 if (exec->state->nbTrans > exec->transno + 1) {
4081 if (exec->inputStackNr <= 0) {
4082 xmlFARegExecSaveInputString(exec, value, data);
4083 }
4084 xmlFARegExecSave(exec);
4085 }
4086 if (trans->counter >= 0) {
4087#ifdef DEBUG_PUSH
4088 printf("Increasing count %d\n", trans->counter);
4089#endif
4090 exec->counts[trans->counter]++;
4091 }
Daniel Veillard10752282005-08-08 13:05:13 +00004092 if ((trans->count >= 0) &&
4093 (trans->count < REGEXP_ALL_COUNTER)) {
4094#ifdef DEBUG_REGEXP_EXEC
4095 printf("resetting count %d on transition\n",
4096 trans->count);
4097#endif
4098 exec->counts[trans->count] = 0;
4099 }
Daniel Veillard4255d502002-04-16 15:50:10 +00004100#ifdef DEBUG_PUSH
4101 printf("entering state %d\n", trans->to);
4102#endif
Daniel Veillardcc026dc2005-01-12 13:21:17 +00004103 if ((exec->comp->states[trans->to] != NULL) &&
4104 (exec->comp->states[trans->to]->type ==
4105 XML_REGEXP_SINK_STATE)) {
4106 /*
4107 * entering a sink state, save the current state as error
4108 * state.
4109 */
4110 if (exec->errString != NULL)
4111 xmlFree(exec->errString);
4112 exec->errString = xmlStrdup(value);
4113 exec->errState = exec->state;
4114 memcpy(exec->errCounts, exec->counts,
4115 exec->comp->nbCounters * sizeof(int));
4116 }
Daniel Veillard4255d502002-04-16 15:50:10 +00004117 exec->state = exec->comp->states[trans->to];
4118 exec->transno = 0;
4119 if (trans->atom != NULL) {
4120 if (exec->inputStack != NULL) {
4121 exec->index++;
4122 if (exec->index < exec->inputStackNr) {
4123 value = exec->inputStack[exec->index].value;
4124 data = exec->inputStack[exec->index].data;
4125#ifdef DEBUG_PUSH
4126 printf("value loaded: %s\n", value);
4127#endif
4128 } else {
4129 value = NULL;
4130 data = NULL;
4131#ifdef DEBUG_PUSH
4132 printf("end of input\n");
4133#endif
4134 }
4135 } else {
4136 value = NULL;
4137 data = NULL;
4138#ifdef DEBUG_PUSH
4139 printf("end of input\n");
4140#endif
4141 }
4142 }
4143 goto progress;
4144 } else if (ret < 0) {
4145 exec->status = -4;
4146 break;
4147 }
4148 }
4149 if ((exec->transno != 0) || (exec->state->nbTrans == 0)) {
4150rollback:
Daniel Veillard90700152005-01-08 22:05:09 +00004151 /*
Daniel Veillardcc026dc2005-01-12 13:21:17 +00004152 * if we didn't yet rollback on the current input
4153 * store the current state as the error state.
Daniel Veillard90700152005-01-08 22:05:09 +00004154 */
Daniel Veillardcc026dc2005-01-12 13:21:17 +00004155 if ((progress) && (exec->state != NULL) &&
4156 (exec->state->type != XML_REGEXP_SINK_STATE)) {
Daniel Veillard90700152005-01-08 22:05:09 +00004157 progress = 0;
4158 if (exec->errString != NULL)
4159 xmlFree(exec->errString);
4160 exec->errString = xmlStrdup(value);
4161 exec->errState = exec->state;
Nick Wellnhofer34e44562017-05-31 16:48:27 +02004162 if (exec->comp->nbCounters)
4163 memcpy(exec->errCounts, exec->counts,
4164 exec->comp->nbCounters * sizeof(int));
Daniel Veillard90700152005-01-08 22:05:09 +00004165 }
4166
Daniel Veillard4255d502002-04-16 15:50:10 +00004167 /*
4168 * Failed to find a way out
4169 */
4170 exec->determinist = 0;
4171 xmlFARegExecRollBack(exec);
Gaurav2671b012013-09-11 14:59:06 +08004172 if ((exec->inputStack != NULL ) && (exec->status == 0)) {
Daniel Veillard4255d502002-04-16 15:50:10 +00004173 value = exec->inputStack[exec->index].value;
4174 data = exec->inputStack[exec->index].data;
4175#ifdef DEBUG_PUSH
4176 printf("value loaded: %s\n", value);
4177#endif
4178 }
4179 }
Daniel Veillard90700152005-01-08 22:05:09 +00004180 continue;
Daniel Veillard4255d502002-04-16 15:50:10 +00004181progress:
Daniel Veillard90700152005-01-08 22:05:09 +00004182 progress = 1;
Daniel Veillard4255d502002-04-16 15:50:10 +00004183 continue;
4184 }
4185 if (exec->status == 0) {
4186 return(exec->state->type == XML_REGEXP_FINAL_STATE);
4187 }
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00004188#ifdef DEBUG_ERR
Daniel Veillard90700152005-01-08 22:05:09 +00004189 if (exec->status < 0) {
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00004190 testerr(exec);
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00004191 }
Daniel Veillard90700152005-01-08 22:05:09 +00004192#endif
Daniel Veillard4255d502002-04-16 15:50:10 +00004193 return(exec->status);
4194}
4195
Daniel Veillard52b48c72003-04-13 19:53:42 +00004196/**
Daniel Veillard6e65e152005-08-09 11:09:52 +00004197 * xmlRegExecPushString:
4198 * @exec: a regexp execution context or NULL to indicate the end
4199 * @value: a string token input
4200 * @data: data associated to the token to reuse in callbacks
4201 *
4202 * Push one input token in the execution context
4203 *
4204 * Returns: 1 if the regexp reached a final state, 0 if non-final, and
4205 * a negative value in case of error.
4206 */
4207int
4208xmlRegExecPushString(xmlRegExecCtxtPtr exec, const xmlChar *value,
4209 void *data) {
4210 return(xmlRegExecPushStringInternal(exec, value, data, 0));
4211}
4212
4213/**
Daniel Veillard52b48c72003-04-13 19:53:42 +00004214 * xmlRegExecPushString2:
4215 * @exec: a regexp execution context or NULL to indicate the end
4216 * @value: the first string token input
4217 * @value2: the second string token input
4218 * @data: data associated to the token to reuse in callbacks
4219 *
4220 * Push one input token in the execution context
4221 *
4222 * Returns: 1 if the regexp reached a final state, 0 if non-final, and
4223 * a negative value in case of error.
4224 */
4225int
4226xmlRegExecPushString2(xmlRegExecCtxtPtr exec, const xmlChar *value,
4227 const xmlChar *value2, void *data) {
4228 xmlChar buf[150];
4229 int lenn, lenp, ret;
4230 xmlChar *str;
4231
4232 if (exec == NULL)
4233 return(-1);
4234 if (exec->comp == NULL)
4235 return(-1);
4236 if (exec->status != 0)
4237 return(exec->status);
4238
4239 if (value2 == NULL)
4240 return(xmlRegExecPushString(exec, value, data));
4241
4242 lenn = strlen((char *) value2);
4243 lenp = strlen((char *) value);
4244
4245 if (150 < lenn + lenp + 2) {
Daniel Veillard3c908dc2003-04-19 00:07:51 +00004246 str = (xmlChar *) xmlMallocAtomic(lenn + lenp + 2);
Daniel Veillard52b48c72003-04-13 19:53:42 +00004247 if (str == NULL) {
4248 exec->status = -1;
4249 return(-1);
4250 }
4251 } else {
4252 str = buf;
4253 }
4254 memcpy(&str[0], value, lenp);
Daniel Veillardc0826a72004-08-10 14:17:33 +00004255 str[lenp] = XML_REG_STRING_SEPARATOR;
Daniel Veillard52b48c72003-04-13 19:53:42 +00004256 memcpy(&str[lenp + 1], value2, lenn);
4257 str[lenn + lenp + 1] = 0;
4258
4259 if (exec->comp->compact != NULL)
4260 ret = xmlRegCompactPushString(exec, exec->comp, str, data);
4261 else
Daniel Veillard6e65e152005-08-09 11:09:52 +00004262 ret = xmlRegExecPushStringInternal(exec, str, data, 1);
Daniel Veillard52b48c72003-04-13 19:53:42 +00004263
4264 if (str != buf)
Daniel Veillard0b1ff142005-12-28 21:13:33 +00004265 xmlFree(str);
Daniel Veillard52b48c72003-04-13 19:53:42 +00004266 return(ret);
4267}
4268
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00004269/**
Daniel Veillard77005e62005-07-19 16:26:18 +00004270 * xmlRegExecGetValues:
Daniel Veillardfc0b6f62005-01-09 17:48:02 +00004271 * @exec: a regexp execution context
4272 * @err: error extraction or normal one
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00004273 * @nbval: pointer to the number of accepted values IN/OUT
Daniel Veillardcc026dc2005-01-12 13:21:17 +00004274 * @nbneg: return number of negative transitions
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00004275 * @values: pointer to the array of acceptable values
Daniel Veillardfc0b6f62005-01-09 17:48:02 +00004276 * @terminal: return value if this was a terminal state
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00004277 *
Haibo Huangcfd91dc2020-07-30 23:01:33 -07004278 * Extract information from the regexp execution, internal routine to
Daniel Veillardfc0b6f62005-01-09 17:48:02 +00004279 * implement xmlRegExecNextValues() and xmlRegExecErrInfo()
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00004280 *
4281 * Returns: 0 in case of success or -1 in case of error.
4282 */
Daniel Veillardfc0b6f62005-01-09 17:48:02 +00004283static int
4284xmlRegExecGetValues(xmlRegExecCtxtPtr exec, int err,
Daniel Veillardcc026dc2005-01-12 13:21:17 +00004285 int *nbval, int *nbneg,
4286 xmlChar **values, int *terminal) {
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00004287 int maxval;
Daniel Veillardcc026dc2005-01-12 13:21:17 +00004288 int nb = 0;
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00004289
Daniel Veillardf8e3db02012-09-11 13:26:36 +08004290 if ((exec == NULL) || (nbval == NULL) || (nbneg == NULL) ||
Daniel Veillardcc026dc2005-01-12 13:21:17 +00004291 (values == NULL) || (*nbval <= 0))
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00004292 return(-1);
Daniel Veillardfc0b6f62005-01-09 17:48:02 +00004293
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00004294 maxval = *nbval;
4295 *nbval = 0;
Daniel Veillardcc026dc2005-01-12 13:21:17 +00004296 *nbneg = 0;
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00004297 if ((exec->comp != NULL) && (exec->comp->compact != NULL)) {
4298 xmlRegexpPtr comp;
4299 int target, i, state;
4300
4301 comp = exec->comp;
Daniel Veillardfc0b6f62005-01-09 17:48:02 +00004302
4303 if (err) {
4304 if (exec->errStateNo == -1) return(-1);
4305 state = exec->errStateNo;
4306 } else {
4307 state = exec->index;
4308 }
4309 if (terminal != NULL) {
4310 if (comp->compact[state * (comp->nbstrings + 1)] ==
4311 XML_REGEXP_FINAL_STATE)
4312 *terminal = 1;
4313 else
4314 *terminal = 0;
4315 }
Daniel Veillardcc026dc2005-01-12 13:21:17 +00004316 for (i = 0;(i < comp->nbstrings) && (nb < maxval);i++) {
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00004317 target = comp->compact[state * (comp->nbstrings + 1) + i + 1];
Daniel Veillardcc026dc2005-01-12 13:21:17 +00004318 if ((target > 0) && (target <= comp->nbstates) &&
4319 (comp->compact[(target - 1) * (comp->nbstrings + 1)] !=
4320 XML_REGEXP_SINK_STATE)) {
4321 values[nb++] = comp->stringMap[i];
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00004322 (*nbval)++;
4323 }
4324 }
Daniel Veillardcc026dc2005-01-12 13:21:17 +00004325 for (i = 0;(i < comp->nbstrings) && (nb < maxval);i++) {
4326 target = comp->compact[state * (comp->nbstrings + 1) + i + 1];
4327 if ((target > 0) && (target <= comp->nbstates) &&
4328 (comp->compact[(target - 1) * (comp->nbstrings + 1)] ==
4329 XML_REGEXP_SINK_STATE)) {
4330 values[nb++] = comp->stringMap[i];
4331 (*nbneg)++;
4332 }
4333 }
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00004334 } else {
4335 int transno;
4336 xmlRegTransPtr trans;
4337 xmlRegAtomPtr atom;
Daniel Veillardfc0b6f62005-01-09 17:48:02 +00004338 xmlRegStatePtr state;
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00004339
Daniel Veillardfc0b6f62005-01-09 17:48:02 +00004340 if (terminal != NULL) {
4341 if (exec->state->type == XML_REGEXP_FINAL_STATE)
4342 *terminal = 1;
4343 else
4344 *terminal = 0;
4345 }
4346
4347 if (err) {
4348 if (exec->errState == NULL) return(-1);
4349 state = exec->errState;
4350 } else {
4351 if (exec->state == NULL) return(-1);
4352 state = exec->state;
4353 }
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00004354 for (transno = 0;
Daniel Veillardcc026dc2005-01-12 13:21:17 +00004355 (transno < state->nbTrans) && (nb < maxval);
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00004356 transno++) {
Daniel Veillardfc0b6f62005-01-09 17:48:02 +00004357 trans = &state->trans[transno];
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00004358 if (trans->to < 0)
4359 continue;
4360 atom = trans->atom;
4361 if ((atom == NULL) || (atom->valuep == NULL))
4362 continue;
4363 if (trans->count == REGEXP_ALL_LAX_COUNTER) {
Daniel Veillardcc026dc2005-01-12 13:21:17 +00004364 /* this should not be reached but ... */
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00004365 TODO;
4366 } else if (trans->count == REGEXP_ALL_COUNTER) {
Daniel Veillardcc026dc2005-01-12 13:21:17 +00004367 /* this should not be reached but ... */
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00004368 TODO;
4369 } else if (trans->counter >= 0) {
Daniel Veillard11ce4002006-03-10 00:36:23 +00004370 xmlRegCounterPtr counter = NULL;
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00004371 int count;
4372
Daniel Veillardfc0b6f62005-01-09 17:48:02 +00004373 if (err)
4374 count = exec->errCounts[trans->counter];
4375 else
4376 count = exec->counts[trans->counter];
Daniel Veillard11ce4002006-03-10 00:36:23 +00004377 if (exec->comp != NULL)
4378 counter = &exec->comp->counters[trans->counter];
4379 if ((counter == NULL) || (count < counter->max)) {
Daniel Veillard77005e62005-07-19 16:26:18 +00004380 if (atom->neg)
4381 values[nb++] = (xmlChar *) atom->valuep2;
4382 else
4383 values[nb++] = (xmlChar *) atom->valuep;
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00004384 (*nbval)++;
4385 }
4386 } else {
Gaurav2671b012013-09-11 14:59:06 +08004387 if ((exec->comp != NULL) && (exec->comp->states[trans->to] != NULL) &&
Daniel Veillardcc026dc2005-01-12 13:21:17 +00004388 (exec->comp->states[trans->to]->type !=
4389 XML_REGEXP_SINK_STATE)) {
Daniel Veillard77005e62005-07-19 16:26:18 +00004390 if (atom->neg)
4391 values[nb++] = (xmlChar *) atom->valuep2;
4392 else
4393 values[nb++] = (xmlChar *) atom->valuep;
Daniel Veillardcc026dc2005-01-12 13:21:17 +00004394 (*nbval)++;
4395 }
Daniel Veillardf8e3db02012-09-11 13:26:36 +08004396 }
Daniel Veillardcc026dc2005-01-12 13:21:17 +00004397 }
4398 for (transno = 0;
4399 (transno < state->nbTrans) && (nb < maxval);
4400 transno++) {
4401 trans = &state->trans[transno];
4402 if (trans->to < 0)
4403 continue;
4404 atom = trans->atom;
4405 if ((atom == NULL) || (atom->valuep == NULL))
4406 continue;
4407 if (trans->count == REGEXP_ALL_LAX_COUNTER) {
4408 continue;
4409 } else if (trans->count == REGEXP_ALL_COUNTER) {
4410 continue;
4411 } else if (trans->counter >= 0) {
4412 continue;
4413 } else {
4414 if ((exec->comp->states[trans->to] != NULL) &&
4415 (exec->comp->states[trans->to]->type ==
4416 XML_REGEXP_SINK_STATE)) {
Daniel Veillard77005e62005-07-19 16:26:18 +00004417 if (atom->neg)
4418 values[nb++] = (xmlChar *) atom->valuep2;
4419 else
4420 values[nb++] = (xmlChar *) atom->valuep;
Daniel Veillardcc026dc2005-01-12 13:21:17 +00004421 (*nbneg)++;
4422 }
Daniel Veillardf8e3db02012-09-11 13:26:36 +08004423 }
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00004424 }
4425 }
4426 return(0);
4427}
4428
Daniel Veillardfc0b6f62005-01-09 17:48:02 +00004429/**
4430 * xmlRegExecNextValues:
4431 * @exec: a regexp execution context
4432 * @nbval: pointer to the number of accepted values IN/OUT
Daniel Veillardcc026dc2005-01-12 13:21:17 +00004433 * @nbneg: return number of negative transitions
Daniel Veillardfc0b6f62005-01-09 17:48:02 +00004434 * @values: pointer to the array of acceptable values
4435 * @terminal: return value if this was a terminal state
4436 *
Haibo Huangcfd91dc2020-07-30 23:01:33 -07004437 * Extract information from the regexp execution,
Daniel Veillardfc0b6f62005-01-09 17:48:02 +00004438 * the parameter @values must point to an array of @nbval string pointers
4439 * on return nbval will contain the number of possible strings in that
4440 * state and the @values array will be updated with them. The string values
4441 * returned will be freed with the @exec context and don't need to be
4442 * deallocated.
4443 *
4444 * Returns: 0 in case of success or -1 in case of error.
4445 */
4446int
Daniel Veillardcc026dc2005-01-12 13:21:17 +00004447xmlRegExecNextValues(xmlRegExecCtxtPtr exec, int *nbval, int *nbneg,
4448 xmlChar **values, int *terminal) {
4449 return(xmlRegExecGetValues(exec, 0, nbval, nbneg, values, terminal));
Daniel Veillardfc0b6f62005-01-09 17:48:02 +00004450}
4451
4452/**
4453 * xmlRegExecErrInfo:
4454 * @exec: a regexp execution context generating an error
4455 * @string: return value for the error string
4456 * @nbval: pointer to the number of accepted values IN/OUT
Daniel Veillardcc026dc2005-01-12 13:21:17 +00004457 * @nbneg: return number of negative transitions
Daniel Veillardfc0b6f62005-01-09 17:48:02 +00004458 * @values: pointer to the array of acceptable values
4459 * @terminal: return value if this was a terminal state
4460 *
Haibo Huangcfd91dc2020-07-30 23:01:33 -07004461 * Extract error information from the regexp execution, the parameter
Daniel Veillardfc0b6f62005-01-09 17:48:02 +00004462 * @string will be updated with the value pushed and not accepted,
4463 * the parameter @values must point to an array of @nbval string pointers
4464 * on return nbval will contain the number of possible strings in that
4465 * state and the @values array will be updated with them. The string values
4466 * returned will be freed with the @exec context and don't need to be
4467 * deallocated.
4468 *
4469 * Returns: 0 in case of success or -1 in case of error.
4470 */
4471int
4472xmlRegExecErrInfo(xmlRegExecCtxtPtr exec, const xmlChar **string,
Daniel Veillardcc026dc2005-01-12 13:21:17 +00004473 int *nbval, int *nbneg, xmlChar **values, int *terminal) {
Daniel Veillardfc0b6f62005-01-09 17:48:02 +00004474 if (exec == NULL)
4475 return(-1);
4476 if (string != NULL) {
4477 if (exec->status != 0)
4478 *string = exec->errString;
4479 else
4480 *string = NULL;
4481 }
Daniel Veillardcc026dc2005-01-12 13:21:17 +00004482 return(xmlRegExecGetValues(exec, 1, nbval, nbneg, values, terminal));
Daniel Veillardfc0b6f62005-01-09 17:48:02 +00004483}
4484
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00004485#ifdef DEBUG_ERR
4486static void testerr(xmlRegExecCtxtPtr exec) {
4487 const xmlChar *string;
Daniel Veillardcee2b3a2005-01-25 00:22:52 +00004488 xmlChar *values[5];
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00004489 int nb = 5;
Daniel Veillardcc026dc2005-01-12 13:21:17 +00004490 int nbneg;
Daniel Veillardfc0b6f62005-01-09 17:48:02 +00004491 int terminal;
Daniel Veillardcc026dc2005-01-12 13:21:17 +00004492 xmlRegExecErrInfo(exec, &string, &nb, &nbneg, &values[0], &terminal);
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00004493}
4494#endif
4495
Daniel Veillard4255d502002-04-16 15:50:10 +00004496#if 0
4497static int
4498xmlRegExecPushChar(xmlRegExecCtxtPtr exec, int UCS) {
4499 xmlRegTransPtr trans;
4500 xmlRegAtomPtr atom;
4501 int ret;
4502 int codepoint, len;
4503
4504 if (exec == NULL)
4505 return(-1);
4506 if (exec->status != 0)
4507 return(exec->status);
4508
4509 while ((exec->status == 0) &&
4510 ((exec->inputString[exec->index] != 0) ||
4511 (exec->state->type != XML_REGEXP_FINAL_STATE))) {
4512
4513 /*
4514 * End of input on non-terminal state, rollback, however we may
4515 * still have epsilon like transition for counted transitions
4516 * on counters, in that case don't break too early.
4517 */
4518 if ((exec->inputString[exec->index] == 0) && (exec->counts == NULL))
4519 goto rollback;
4520
4521 exec->transcount = 0;
4522 for (;exec->transno < exec->state->nbTrans;exec->transno++) {
4523 trans = &exec->state->trans[exec->transno];
4524 if (trans->to < 0)
4525 continue;
4526 atom = trans->atom;
4527 ret = 0;
4528 if (trans->count >= 0) {
4529 int count;
4530 xmlRegCounterPtr counter;
4531
4532 /*
4533 * A counted transition.
4534 */
4535
4536 count = exec->counts[trans->count];
4537 counter = &exec->comp->counters[trans->count];
4538#ifdef DEBUG_REGEXP_EXEC
4539 printf("testing count %d: val %d, min %d, max %d\n",
4540 trans->count, count, counter->min, counter->max);
4541#endif
4542 ret = ((count >= counter->min) && (count <= counter->max));
4543 } else if (atom == NULL) {
4544 fprintf(stderr, "epsilon transition left at runtime\n");
4545 exec->status = -2;
4546 break;
4547 } else if (exec->inputString[exec->index] != 0) {
4548 codepoint = CUR_SCHAR(&(exec->inputString[exec->index]), len);
4549 ret = xmlRegCheckCharacter(atom, codepoint);
4550 if ((ret == 1) && (atom->min > 0) && (atom->max > 0)) {
4551 xmlRegStatePtr to = exec->comp->states[trans->to];
4552
4553 /*
4554 * this is a multiple input sequence
4555 */
4556 if (exec->state->nbTrans > exec->transno + 1) {
4557 xmlFARegExecSave(exec);
4558 }
4559 exec->transcount = 1;
4560 do {
4561 /*
4562 * Try to progress as much as possible on the input
4563 */
4564 if (exec->transcount == atom->max) {
4565 break;
4566 }
4567 exec->index += len;
4568 /*
4569 * End of input: stop here
4570 */
4571 if (exec->inputString[exec->index] == 0) {
4572 exec->index -= len;
4573 break;
4574 }
4575 if (exec->transcount >= atom->min) {
4576 int transno = exec->transno;
4577 xmlRegStatePtr state = exec->state;
4578
4579 /*
4580 * The transition is acceptable save it
4581 */
4582 exec->transno = -1; /* trick */
4583 exec->state = to;
4584 xmlFARegExecSave(exec);
4585 exec->transno = transno;
4586 exec->state = state;
4587 }
4588 codepoint = CUR_SCHAR(&(exec->inputString[exec->index]),
4589 len);
4590 ret = xmlRegCheckCharacter(atom, codepoint);
4591 exec->transcount++;
4592 } while (ret == 1);
4593 if (exec->transcount < atom->min)
4594 ret = 0;
4595
4596 /*
4597 * If the last check failed but one transition was found
4598 * possible, rollback
4599 */
4600 if (ret < 0)
4601 ret = 0;
4602 if (ret == 0) {
4603 goto rollback;
4604 }
4605 }
4606 }
4607 if (ret == 1) {
4608 if (exec->state->nbTrans > exec->transno + 1) {
4609 xmlFARegExecSave(exec);
4610 }
Daniel Veillard54eb0242006-03-21 23:17:57 +00004611 /*
4612 * restart count for expressions like this ((abc){2})*
4613 */
4614 if (trans->count >= 0) {
4615#ifdef DEBUG_REGEXP_EXEC
4616 printf("Reset count %d\n", trans->count);
4617#endif
4618 exec->counts[trans->count] = 0;
4619 }
Daniel Veillard4255d502002-04-16 15:50:10 +00004620 if (trans->counter >= 0) {
4621#ifdef DEBUG_REGEXP_EXEC
4622 printf("Increasing count %d\n", trans->counter);
4623#endif
4624 exec->counts[trans->counter]++;
4625 }
4626#ifdef DEBUG_REGEXP_EXEC
4627 printf("entering state %d\n", trans->to);
4628#endif
4629 exec->state = exec->comp->states[trans->to];
4630 exec->transno = 0;
4631 if (trans->atom != NULL) {
4632 exec->index += len;
4633 }
4634 goto progress;
4635 } else if (ret < 0) {
4636 exec->status = -4;
4637 break;
4638 }
4639 }
4640 if ((exec->transno != 0) || (exec->state->nbTrans == 0)) {
4641rollback:
4642 /*
4643 * Failed to find a way out
4644 */
4645 exec->determinist = 0;
4646 xmlFARegExecRollBack(exec);
4647 }
4648progress:
4649 continue;
4650 }
4651}
4652#endif
4653/************************************************************************
Daniel Veillardf8e3db02012-09-11 13:26:36 +08004654 * *
William M. Brackddf71d62004-05-06 04:17:26 +00004655 * Parser for the Schemas Datatype Regular Expressions *
Daniel Veillard4255d502002-04-16 15:50:10 +00004656 * http://www.w3.org/TR/2001/REC-xmlschema-2-20010502/#regexs *
Daniel Veillardf8e3db02012-09-11 13:26:36 +08004657 * *
Daniel Veillard4255d502002-04-16 15:50:10 +00004658 ************************************************************************/
4659
4660/**
4661 * xmlFAIsChar:
Daniel Veillard441bc322002-04-20 17:38:48 +00004662 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00004663 *
4664 * [10] Char ::= [^.\?*+()|#x5B#x5D]
4665 */
4666static int
4667xmlFAIsChar(xmlRegParserCtxtPtr ctxt) {
4668 int cur;
4669 int len;
4670
4671 cur = CUR_SCHAR(ctxt->cur, len);
4672 if ((cur == '.') || (cur == '\\') || (cur == '?') ||
4673 (cur == '*') || (cur == '+') || (cur == '(') ||
4674 (cur == ')') || (cur == '|') || (cur == 0x5B) ||
4675 (cur == 0x5D) || (cur == 0))
4676 return(-1);
4677 return(cur);
4678}
4679
4680/**
4681 * xmlFAParseCharProp:
Daniel Veillard441bc322002-04-20 17:38:48 +00004682 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00004683 *
4684 * [27] charProp ::= IsCategory | IsBlock
4685 * [28] IsCategory ::= Letters | Marks | Numbers | Punctuation |
Daniel Veillardf8e3db02012-09-11 13:26:36 +08004686 * Separators | Symbols | Others
Daniel Veillard4255d502002-04-16 15:50:10 +00004687 * [29] Letters ::= 'L' [ultmo]?
4688 * [30] Marks ::= 'M' [nce]?
4689 * [31] Numbers ::= 'N' [dlo]?
4690 * [32] Punctuation ::= 'P' [cdseifo]?
4691 * [33] Separators ::= 'Z' [slp]?
4692 * [34] Symbols ::= 'S' [mcko]?
4693 * [35] Others ::= 'C' [cfon]?
4694 * [36] IsBlock ::= 'Is' [a-zA-Z0-9#x2D]+
4695 */
4696static void
4697xmlFAParseCharProp(xmlRegParserCtxtPtr ctxt) {
4698 int cur;
William M. Brack779af002003-08-01 15:55:39 +00004699 xmlRegAtomType type = (xmlRegAtomType) 0;
Daniel Veillard4255d502002-04-16 15:50:10 +00004700 xmlChar *blockName = NULL;
Daniel Veillardf8e3db02012-09-11 13:26:36 +08004701
Daniel Veillard4255d502002-04-16 15:50:10 +00004702 cur = CUR;
4703 if (cur == 'L') {
4704 NEXT;
4705 cur = CUR;
4706 if (cur == 'u') {
4707 NEXT;
4708 type = XML_REGEXP_LETTER_UPPERCASE;
4709 } else if (cur == 'l') {
4710 NEXT;
4711 type = XML_REGEXP_LETTER_LOWERCASE;
4712 } else if (cur == 't') {
4713 NEXT;
4714 type = XML_REGEXP_LETTER_TITLECASE;
4715 } else if (cur == 'm') {
4716 NEXT;
4717 type = XML_REGEXP_LETTER_MODIFIER;
4718 } else if (cur == 'o') {
4719 NEXT;
4720 type = XML_REGEXP_LETTER_OTHERS;
4721 } else {
4722 type = XML_REGEXP_LETTER;
4723 }
4724 } else if (cur == 'M') {
4725 NEXT;
4726 cur = CUR;
4727 if (cur == 'n') {
4728 NEXT;
4729 /* nonspacing */
4730 type = XML_REGEXP_MARK_NONSPACING;
4731 } else if (cur == 'c') {
4732 NEXT;
4733 /* spacing combining */
4734 type = XML_REGEXP_MARK_SPACECOMBINING;
4735 } else if (cur == 'e') {
4736 NEXT;
4737 /* enclosing */
4738 type = XML_REGEXP_MARK_ENCLOSING;
4739 } else {
4740 /* all marks */
4741 type = XML_REGEXP_MARK;
4742 }
4743 } else if (cur == 'N') {
4744 NEXT;
4745 cur = CUR;
4746 if (cur == 'd') {
4747 NEXT;
4748 /* digital */
4749 type = XML_REGEXP_NUMBER_DECIMAL;
4750 } else if (cur == 'l') {
4751 NEXT;
4752 /* letter */
4753 type = XML_REGEXP_NUMBER_LETTER;
4754 } else if (cur == 'o') {
4755 NEXT;
4756 /* other */
4757 type = XML_REGEXP_NUMBER_OTHERS;
4758 } else {
4759 /* all numbers */
4760 type = XML_REGEXP_NUMBER;
4761 }
4762 } else if (cur == 'P') {
4763 NEXT;
4764 cur = CUR;
4765 if (cur == 'c') {
4766 NEXT;
4767 /* connector */
4768 type = XML_REGEXP_PUNCT_CONNECTOR;
4769 } else if (cur == 'd') {
4770 NEXT;
4771 /* dash */
4772 type = XML_REGEXP_PUNCT_DASH;
4773 } else if (cur == 's') {
4774 NEXT;
4775 /* open */
4776 type = XML_REGEXP_PUNCT_OPEN;
4777 } else if (cur == 'e') {
4778 NEXT;
4779 /* close */
4780 type = XML_REGEXP_PUNCT_CLOSE;
4781 } else if (cur == 'i') {
4782 NEXT;
4783 /* initial quote */
4784 type = XML_REGEXP_PUNCT_INITQUOTE;
4785 } else if (cur == 'f') {
4786 NEXT;
4787 /* final quote */
4788 type = XML_REGEXP_PUNCT_FINQUOTE;
4789 } else if (cur == 'o') {
4790 NEXT;
4791 /* other */
4792 type = XML_REGEXP_PUNCT_OTHERS;
4793 } else {
4794 /* all punctuation */
4795 type = XML_REGEXP_PUNCT;
4796 }
4797 } else if (cur == 'Z') {
4798 NEXT;
4799 cur = CUR;
4800 if (cur == 's') {
4801 NEXT;
4802 /* space */
4803 type = XML_REGEXP_SEPAR_SPACE;
4804 } else if (cur == 'l') {
4805 NEXT;
4806 /* line */
4807 type = XML_REGEXP_SEPAR_LINE;
4808 } else if (cur == 'p') {
4809 NEXT;
4810 /* paragraph */
4811 type = XML_REGEXP_SEPAR_PARA;
4812 } else {
4813 /* all separators */
4814 type = XML_REGEXP_SEPAR;
4815 }
4816 } else if (cur == 'S') {
4817 NEXT;
4818 cur = CUR;
4819 if (cur == 'm') {
4820 NEXT;
4821 type = XML_REGEXP_SYMBOL_MATH;
4822 /* math */
4823 } else if (cur == 'c') {
4824 NEXT;
4825 type = XML_REGEXP_SYMBOL_CURRENCY;
4826 /* currency */
4827 } else if (cur == 'k') {
4828 NEXT;
4829 type = XML_REGEXP_SYMBOL_MODIFIER;
4830 /* modifiers */
4831 } else if (cur == 'o') {
4832 NEXT;
4833 type = XML_REGEXP_SYMBOL_OTHERS;
4834 /* other */
4835 } else {
4836 /* all symbols */
4837 type = XML_REGEXP_SYMBOL;
4838 }
4839 } else if (cur == 'C') {
4840 NEXT;
4841 cur = CUR;
4842 if (cur == 'c') {
4843 NEXT;
4844 /* control */
4845 type = XML_REGEXP_OTHER_CONTROL;
4846 } else if (cur == 'f') {
4847 NEXT;
4848 /* format */
4849 type = XML_REGEXP_OTHER_FORMAT;
4850 } else if (cur == 'o') {
4851 NEXT;
4852 /* private use */
4853 type = XML_REGEXP_OTHER_PRIVATE;
4854 } else if (cur == 'n') {
4855 NEXT;
4856 /* not assigned */
4857 type = XML_REGEXP_OTHER_NA;
4858 } else {
4859 /* all others */
4860 type = XML_REGEXP_OTHER;
4861 }
4862 } else if (cur == 'I') {
4863 const xmlChar *start;
4864 NEXT;
4865 cur = CUR;
4866 if (cur != 's') {
4867 ERROR("IsXXXX expected");
4868 return;
4869 }
4870 NEXT;
4871 start = ctxt->cur;
4872 cur = CUR;
Daniel Veillardf8e3db02012-09-11 13:26:36 +08004873 if (((cur >= 'a') && (cur <= 'z')) ||
4874 ((cur >= 'A') && (cur <= 'Z')) ||
4875 ((cur >= '0') && (cur <= '9')) ||
Daniel Veillard4255d502002-04-16 15:50:10 +00004876 (cur == 0x2D)) {
4877 NEXT;
4878 cur = CUR;
Daniel Veillardf8e3db02012-09-11 13:26:36 +08004879 while (((cur >= 'a') && (cur <= 'z')) ||
4880 ((cur >= 'A') && (cur <= 'Z')) ||
4881 ((cur >= '0') && (cur <= '9')) ||
Daniel Veillard4255d502002-04-16 15:50:10 +00004882 (cur == 0x2D)) {
4883 NEXT;
4884 cur = CUR;
4885 }
4886 }
4887 type = XML_REGEXP_BLOCK_NAME;
4888 blockName = xmlStrndup(start, ctxt->cur - start);
4889 } else {
4890 ERROR("Unknown char property");
4891 return;
4892 }
4893 if (ctxt->atom == NULL) {
4894 ctxt->atom = xmlRegNewAtom(ctxt, type);
4895 if (ctxt->atom != NULL)
4896 ctxt->atom->valuep = blockName;
4897 } else if (ctxt->atom->type == XML_REGEXP_RANGES) {
4898 xmlRegAtomAddRange(ctxt, ctxt->atom, ctxt->neg,
4899 type, 0, 0, blockName);
4900 }
4901}
4902
4903/**
4904 * xmlFAParseCharClassEsc:
Daniel Veillard441bc322002-04-20 17:38:48 +00004905 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00004906 *
Daniel Veillardf8e3db02012-09-11 13:26:36 +08004907 * [23] charClassEsc ::= ( SingleCharEsc | MultiCharEsc | catEsc | complEsc )
Daniel Veillard4255d502002-04-16 15:50:10 +00004908 * [24] SingleCharEsc ::= '\' [nrt\|.?*+(){}#x2D#x5B#x5D#x5E]
4909 * [25] catEsc ::= '\p{' charProp '}'
4910 * [26] complEsc ::= '\P{' charProp '}'
4911 * [37] MultiCharEsc ::= '.' | ('\' [sSiIcCdDwW])
4912 */
4913static void
4914xmlFAParseCharClassEsc(xmlRegParserCtxtPtr ctxt) {
4915 int cur;
4916
4917 if (CUR == '.') {
4918 if (ctxt->atom == NULL) {
4919 ctxt->atom = xmlRegNewAtom(ctxt, XML_REGEXP_ANYCHAR);
4920 } else if (ctxt->atom->type == XML_REGEXP_RANGES) {
4921 xmlRegAtomAddRange(ctxt, ctxt->atom, ctxt->neg,
4922 XML_REGEXP_ANYCHAR, 0, 0, NULL);
4923 }
4924 NEXT;
4925 return;
4926 }
4927 if (CUR != '\\') {
4928 ERROR("Escaped sequence: expecting \\");
4929 return;
4930 }
4931 NEXT;
4932 cur = CUR;
4933 if (cur == 'p') {
4934 NEXT;
4935 if (CUR != '{') {
4936 ERROR("Expecting '{'");
4937 return;
4938 }
4939 NEXT;
4940 xmlFAParseCharProp(ctxt);
4941 if (CUR != '}') {
4942 ERROR("Expecting '}'");
4943 return;
4944 }
4945 NEXT;
4946 } else if (cur == 'P') {
4947 NEXT;
4948 if (CUR != '{') {
4949 ERROR("Expecting '{'");
4950 return;
4951 }
4952 NEXT;
4953 xmlFAParseCharProp(ctxt);
Nick Wellnhofer8a0c6692017-07-04 17:13:06 +02004954 if (ctxt->atom != NULL)
4955 ctxt->atom->neg = 1;
Daniel Veillard4255d502002-04-16 15:50:10 +00004956 if (CUR != '}') {
4957 ERROR("Expecting '}'");
4958 return;
4959 }
4960 NEXT;
4961 } else if ((cur == 'n') || (cur == 'r') || (cur == 't') || (cur == '\\') ||
4962 (cur == '|') || (cur == '.') || (cur == '?') || (cur == '*') ||
4963 (cur == '+') || (cur == '(') || (cur == ')') || (cur == '{') ||
4964 (cur == '}') || (cur == 0x2D) || (cur == 0x5B) || (cur == 0x5D) ||
4965 (cur == 0x5E)) {
4966 if (ctxt->atom == NULL) {
4967 ctxt->atom = xmlRegNewAtom(ctxt, XML_REGEXP_CHARVAL);
Daniel Veillard99c394d2005-07-14 12:58:49 +00004968 if (ctxt->atom != NULL) {
4969 switch (cur) {
4970 case 'n':
4971 ctxt->atom->codepoint = '\n';
4972 break;
4973 case 'r':
4974 ctxt->atom->codepoint = '\r';
4975 break;
4976 case 't':
4977 ctxt->atom->codepoint = '\t';
4978 break;
4979 default:
4980 ctxt->atom->codepoint = cur;
4981 }
4982 }
Daniel Veillard4255d502002-04-16 15:50:10 +00004983 } else if (ctxt->atom->type == XML_REGEXP_RANGES) {
Daniel Veillard9543aee2010-03-15 11:13:39 +01004984 switch (cur) {
4985 case 'n':
4986 cur = '\n';
4987 break;
4988 case 'r':
4989 cur = '\r';
4990 break;
4991 case 't':
4992 cur = '\t';
4993 break;
4994 }
Daniel Veillard4255d502002-04-16 15:50:10 +00004995 xmlRegAtomAddRange(ctxt, ctxt->atom, ctxt->neg,
4996 XML_REGEXP_CHARVAL, cur, cur, NULL);
4997 }
4998 NEXT;
4999 } else if ((cur == 's') || (cur == 'S') || (cur == 'i') || (cur == 'I') ||
5000 (cur == 'c') || (cur == 'C') || (cur == 'd') || (cur == 'D') ||
5001 (cur == 'w') || (cur == 'W')) {
Daniel Veillardb509f152002-04-17 16:28:10 +00005002 xmlRegAtomType type = XML_REGEXP_ANYSPACE;
Daniel Veillard4255d502002-04-16 15:50:10 +00005003
5004 switch (cur) {
Daniel Veillardf8e3db02012-09-11 13:26:36 +08005005 case 's':
Daniel Veillard4255d502002-04-16 15:50:10 +00005006 type = XML_REGEXP_ANYSPACE;
5007 break;
Daniel Veillardf8e3db02012-09-11 13:26:36 +08005008 case 'S':
Daniel Veillard4255d502002-04-16 15:50:10 +00005009 type = XML_REGEXP_NOTSPACE;
5010 break;
Daniel Veillardf8e3db02012-09-11 13:26:36 +08005011 case 'i':
Daniel Veillard4255d502002-04-16 15:50:10 +00005012 type = XML_REGEXP_INITNAME;
5013 break;
Daniel Veillardf8e3db02012-09-11 13:26:36 +08005014 case 'I':
Daniel Veillard4255d502002-04-16 15:50:10 +00005015 type = XML_REGEXP_NOTINITNAME;
5016 break;
Daniel Veillardf8e3db02012-09-11 13:26:36 +08005017 case 'c':
Daniel Veillard4255d502002-04-16 15:50:10 +00005018 type = XML_REGEXP_NAMECHAR;
5019 break;
Daniel Veillardf8e3db02012-09-11 13:26:36 +08005020 case 'C':
Daniel Veillard4255d502002-04-16 15:50:10 +00005021 type = XML_REGEXP_NOTNAMECHAR;
5022 break;
Daniel Veillardf8e3db02012-09-11 13:26:36 +08005023 case 'd':
Daniel Veillard4255d502002-04-16 15:50:10 +00005024 type = XML_REGEXP_DECIMAL;
5025 break;
Daniel Veillardf8e3db02012-09-11 13:26:36 +08005026 case 'D':
Daniel Veillard4255d502002-04-16 15:50:10 +00005027 type = XML_REGEXP_NOTDECIMAL;
5028 break;
Daniel Veillardf8e3db02012-09-11 13:26:36 +08005029 case 'w':
Daniel Veillard4255d502002-04-16 15:50:10 +00005030 type = XML_REGEXP_REALCHAR;
5031 break;
Daniel Veillardf8e3db02012-09-11 13:26:36 +08005032 case 'W':
Daniel Veillard4255d502002-04-16 15:50:10 +00005033 type = XML_REGEXP_NOTREALCHAR;
5034 break;
5035 }
5036 NEXT;
5037 if (ctxt->atom == NULL) {
5038 ctxt->atom = xmlRegNewAtom(ctxt, type);
5039 } else if (ctxt->atom->type == XML_REGEXP_RANGES) {
5040 xmlRegAtomAddRange(ctxt, ctxt->atom, ctxt->neg,
5041 type, 0, 0, NULL);
5042 }
Daniel Veillardcb4284e2007-04-25 13:55:20 +00005043 } else {
5044 ERROR("Wrong escape sequence, misuse of character '\\'");
Daniel Veillard4255d502002-04-16 15:50:10 +00005045 }
5046}
5047
5048/**
Daniel Veillard4255d502002-04-16 15:50:10 +00005049 * xmlFAParseCharRange:
Daniel Veillard441bc322002-04-20 17:38:48 +00005050 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00005051 *
Daniel Veillardf8e3db02012-09-11 13:26:36 +08005052 * [17] charRange ::= seRange | XmlCharRef | XmlCharIncDash
Daniel Veillard4255d502002-04-16 15:50:10 +00005053 * [18] seRange ::= charOrEsc '-' charOrEsc
5054 * [20] charOrEsc ::= XmlChar | SingleCharEsc
5055 * [21] XmlChar ::= [^\#x2D#x5B#x5D]
5056 * [22] XmlCharIncDash ::= [^\#x5B#x5D]
5057 */
5058static void
5059xmlFAParseCharRange(xmlRegParserCtxtPtr ctxt) {
William M. Brackdc99df92003-12-27 01:54:25 +00005060 int cur, len;
Daniel Veillard4255d502002-04-16 15:50:10 +00005061 int start = -1;
5062 int end = -1;
5063
Daniel Veillard777737e2006-10-17 21:23:17 +00005064 if (CUR == '\0') {
5065 ERROR("Expecting ']'");
5066 return;
5067 }
5068
Daniel Veillard4255d502002-04-16 15:50:10 +00005069 cur = CUR;
5070 if (cur == '\\') {
5071 NEXT;
5072 cur = CUR;
5073 switch (cur) {
5074 case 'n': start = 0xA; break;
5075 case 'r': start = 0xD; break;
5076 case 't': start = 0x9; break;
5077 case '\\': case '|': case '.': case '-': case '^': case '?':
5078 case '*': case '+': case '{': case '}': case '(': case ')':
5079 case '[': case ']':
5080 start = cur; break;
5081 default:
5082 ERROR("Invalid escape value");
5083 return;
5084 }
5085 end = start;
William M. Brackdc99df92003-12-27 01:54:25 +00005086 len = 1;
Daniel Veillard4255d502002-04-16 15:50:10 +00005087 } else if ((cur != 0x5B) && (cur != 0x5D)) {
William M. Brackdc99df92003-12-27 01:54:25 +00005088 end = start = CUR_SCHAR(ctxt->cur, len);
Daniel Veillard4255d502002-04-16 15:50:10 +00005089 } else {
5090 ERROR("Expecting a char range");
5091 return;
5092 }
William M. Bracka9cbf282007-03-21 13:16:33 +00005093 /*
5094 * Since we are "inside" a range, we can assume ctxt->cur is past
5095 * the start of ctxt->string, and PREV should be safe
5096 */
5097 if ((start == '-') && (NXT(1) != ']') && (PREV != '[') && (PREV != '^')) {
5098 NEXTL(len);
Daniel Veillard4255d502002-04-16 15:50:10 +00005099 return;
5100 }
William M. Bracka9cbf282007-03-21 13:16:33 +00005101 NEXTL(len);
Daniel Veillard4255d502002-04-16 15:50:10 +00005102 cur = CUR;
William M. Brack10f1ef42004-03-20 14:51:25 +00005103 if ((cur != '-') || (NXT(1) == ']')) {
Daniel Veillard4255d502002-04-16 15:50:10 +00005104 xmlRegAtomAddRange(ctxt, ctxt->atom, ctxt->neg,
5105 XML_REGEXP_CHARVAL, start, end, NULL);
5106 return;
5107 }
5108 NEXT;
5109 cur = CUR;
5110 if (cur == '\\') {
5111 NEXT;
5112 cur = CUR;
5113 switch (cur) {
5114 case 'n': end = 0xA; break;
5115 case 'r': end = 0xD; break;
5116 case 't': end = 0x9; break;
5117 case '\\': case '|': case '.': case '-': case '^': case '?':
5118 case '*': case '+': case '{': case '}': case '(': case ')':
5119 case '[': case ']':
5120 end = cur; break;
5121 default:
5122 ERROR("Invalid escape value");
5123 return;
5124 }
William M. Brackdc99df92003-12-27 01:54:25 +00005125 len = 1;
David Kilzerfb56f802017-07-04 18:38:03 +02005126 } else if ((cur != '\0') && (cur != 0x5B) && (cur != 0x5D)) {
William M. Brackdc99df92003-12-27 01:54:25 +00005127 end = CUR_SCHAR(ctxt->cur, len);
Daniel Veillard4255d502002-04-16 15:50:10 +00005128 } else {
5129 ERROR("Expecting the end of a char range");
5130 return;
5131 }
Pranjal Jumdecbb27162016-03-07 06:34:26 -08005132
Daniel Veillard4255d502002-04-16 15:50:10 +00005133 /* TODO check that the values are acceptable character ranges for XML */
5134 if (end < start) {
5135 ERROR("End of range is before start of range");
5136 } else {
Pranjal Jumdecbb27162016-03-07 06:34:26 -08005137 NEXTL(len);
Daniel Veillard4255d502002-04-16 15:50:10 +00005138 xmlRegAtomAddRange(ctxt, ctxt->atom, ctxt->neg,
5139 XML_REGEXP_CHARVAL, start, end, NULL);
5140 }
5141 return;
5142}
5143
5144/**
5145 * xmlFAParsePosCharGroup:
Daniel Veillard441bc322002-04-20 17:38:48 +00005146 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00005147 *
5148 * [14] posCharGroup ::= ( charRange | charClassEsc )+
5149 */
5150static void
5151xmlFAParsePosCharGroup(xmlRegParserCtxtPtr ctxt) {
5152 do {
Daniel Veillard041b6872008-02-08 10:37:18 +00005153 if (CUR == '\\') {
Daniel Veillard4255d502002-04-16 15:50:10 +00005154 xmlFAParseCharClassEsc(ctxt);
5155 } else {
5156 xmlFAParseCharRange(ctxt);
5157 }
5158 } while ((CUR != ']') && (CUR != '^') && (CUR != '-') &&
Daniel Veillard777737e2006-10-17 21:23:17 +00005159 (CUR != 0) && (ctxt->error == 0));
Daniel Veillard4255d502002-04-16 15:50:10 +00005160}
5161
5162/**
5163 * xmlFAParseCharGroup:
Daniel Veillard441bc322002-04-20 17:38:48 +00005164 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00005165 *
5166 * [13] charGroup ::= posCharGroup | negCharGroup | charClassSub
5167 * [15] negCharGroup ::= '^' posCharGroup
Daniel Veillardf8e3db02012-09-11 13:26:36 +08005168 * [16] charClassSub ::= ( posCharGroup | negCharGroup ) '-' charClassExpr
Daniel Veillard4255d502002-04-16 15:50:10 +00005169 * [12] charClassExpr ::= '[' charGroup ']'
5170 */
5171static void
5172xmlFAParseCharGroup(xmlRegParserCtxtPtr ctxt) {
5173 int n = ctxt->neg;
5174 while ((CUR != ']') && (ctxt->error == 0)) {
5175 if (CUR == '^') {
5176 int neg = ctxt->neg;
5177
5178 NEXT;
5179 ctxt->neg = !ctxt->neg;
5180 xmlFAParsePosCharGroup(ctxt);
5181 ctxt->neg = neg;
William M. Brack10f1ef42004-03-20 14:51:25 +00005182 } else if ((CUR == '-') && (NXT(1) == '[')) {
Daniel Veillardf8b9de32003-11-24 14:27:26 +00005183 int neg = ctxt->neg;
Daniel Veillardf8b9de32003-11-24 14:27:26 +00005184 ctxt->neg = 2;
William M. Brack10f1ef42004-03-20 14:51:25 +00005185 NEXT; /* eat the '-' */
5186 NEXT; /* eat the '[' */
Daniel Veillard4255d502002-04-16 15:50:10 +00005187 xmlFAParseCharGroup(ctxt);
5188 if (CUR == ']') {
5189 NEXT;
5190 } else {
5191 ERROR("charClassExpr: ']' expected");
5192 break;
5193 }
Daniel Veillardf8b9de32003-11-24 14:27:26 +00005194 ctxt->neg = neg;
Daniel Veillard4255d502002-04-16 15:50:10 +00005195 break;
5196 } else if (CUR != ']') {
5197 xmlFAParsePosCharGroup(ctxt);
5198 }
5199 }
5200 ctxt->neg = n;
5201}
5202
5203/**
5204 * xmlFAParseCharClass:
Daniel Veillard441bc322002-04-20 17:38:48 +00005205 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00005206 *
5207 * [11] charClass ::= charClassEsc | charClassExpr
5208 * [12] charClassExpr ::= '[' charGroup ']'
5209 */
5210static void
5211xmlFAParseCharClass(xmlRegParserCtxtPtr ctxt) {
5212 if (CUR == '[') {
5213 NEXT;
5214 ctxt->atom = xmlRegNewAtom(ctxt, XML_REGEXP_RANGES);
5215 if (ctxt->atom == NULL)
5216 return;
5217 xmlFAParseCharGroup(ctxt);
5218 if (CUR == ']') {
5219 NEXT;
5220 } else {
5221 ERROR("xmlFAParseCharClass: ']' expected");
5222 }
5223 } else {
5224 xmlFAParseCharClassEsc(ctxt);
5225 }
5226}
5227
5228/**
5229 * xmlFAParseQuantExact:
Daniel Veillard441bc322002-04-20 17:38:48 +00005230 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00005231 *
5232 * [8] QuantExact ::= [0-9]+
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00005233 *
5234 * Returns 0 if success or -1 in case of error
Daniel Veillard4255d502002-04-16 15:50:10 +00005235 */
5236static int
5237xmlFAParseQuantExact(xmlRegParserCtxtPtr ctxt) {
5238 int ret = 0;
5239 int ok = 0;
Haibo Huangcfd91dc2020-07-30 23:01:33 -07005240 int overflow = 0;
Daniel Veillard4255d502002-04-16 15:50:10 +00005241
5242 while ((CUR >= '0') && (CUR <= '9')) {
Haibo Huangcfd91dc2020-07-30 23:01:33 -07005243 if (ret > INT_MAX / 10) {
5244 overflow = 1;
5245 } else {
5246 int digit = CUR - '0';
5247
5248 ret *= 10;
5249 if (ret > INT_MAX - digit)
5250 overflow = 1;
5251 else
5252 ret += digit;
5253 }
Daniel Veillard4255d502002-04-16 15:50:10 +00005254 ok = 1;
5255 NEXT;
5256 }
Haibo Huangcfd91dc2020-07-30 23:01:33 -07005257 if ((ok != 1) || (overflow == 1)) {
Daniel Veillard4255d502002-04-16 15:50:10 +00005258 return(-1);
5259 }
5260 return(ret);
5261}
5262
5263/**
5264 * xmlFAParseQuantifier:
Daniel Veillard441bc322002-04-20 17:38:48 +00005265 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00005266 *
5267 * [4] quantifier ::= [?*+] | ( '{' quantity '}' )
5268 * [5] quantity ::= quantRange | quantMin | QuantExact
5269 * [6] quantRange ::= QuantExact ',' QuantExact
5270 * [7] quantMin ::= QuantExact ','
5271 * [8] QuantExact ::= [0-9]+
5272 */
5273static int
5274xmlFAParseQuantifier(xmlRegParserCtxtPtr ctxt) {
5275 int cur;
5276
5277 cur = CUR;
5278 if ((cur == '?') || (cur == '*') || (cur == '+')) {
5279 if (ctxt->atom != NULL) {
5280 if (cur == '?')
5281 ctxt->atom->quant = XML_REGEXP_QUANT_OPT;
5282 else if (cur == '*')
5283 ctxt->atom->quant = XML_REGEXP_QUANT_MULT;
5284 else if (cur == '+')
5285 ctxt->atom->quant = XML_REGEXP_QUANT_PLUS;
5286 }
5287 NEXT;
5288 return(1);
5289 }
5290 if (cur == '{') {
5291 int min = 0, max = 0;
5292
5293 NEXT;
5294 cur = xmlFAParseQuantExact(ctxt);
5295 if (cur >= 0)
5296 min = cur;
Haibo Huangcfd91dc2020-07-30 23:01:33 -07005297 else {
5298 ERROR("Improper quantifier");
5299 }
Daniel Veillard4255d502002-04-16 15:50:10 +00005300 if (CUR == ',') {
5301 NEXT;
Daniel Veillardebe48c62003-12-03 12:12:27 +00005302 if (CUR == '}')
5303 max = INT_MAX;
5304 else {
5305 cur = xmlFAParseQuantExact(ctxt);
5306 if (cur >= 0)
5307 max = cur;
5308 else {
5309 ERROR("Improper quantifier");
5310 }
5311 }
Daniel Veillard4255d502002-04-16 15:50:10 +00005312 }
5313 if (CUR == '}') {
5314 NEXT;
5315 } else {
5316 ERROR("Unterminated quantifier");
5317 }
5318 if (max == 0)
5319 max = min;
5320 if (ctxt->atom != NULL) {
5321 ctxt->atom->quant = XML_REGEXP_QUANT_RANGE;
5322 ctxt->atom->min = min;
5323 ctxt->atom->max = max;
5324 }
5325 return(1);
5326 }
5327 return(0);
5328}
5329
5330/**
5331 * xmlFAParseAtom:
Daniel Veillard441bc322002-04-20 17:38:48 +00005332 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00005333 *
5334 * [9] atom ::= Char | charClass | ( '(' regExp ')' )
5335 */
5336static int
5337xmlFAParseAtom(xmlRegParserCtxtPtr ctxt) {
5338 int codepoint, len;
5339
5340 codepoint = xmlFAIsChar(ctxt);
5341 if (codepoint > 0) {
5342 ctxt->atom = xmlRegNewAtom(ctxt, XML_REGEXP_CHARVAL);
5343 if (ctxt->atom == NULL)
5344 return(-1);
5345 codepoint = CUR_SCHAR(ctxt->cur, len);
5346 ctxt->atom->codepoint = codepoint;
5347 NEXTL(len);
5348 return(1);
5349 } else if (CUR == '|') {
5350 return(0);
5351 } else if (CUR == 0) {
5352 return(0);
5353 } else if (CUR == ')') {
5354 return(0);
5355 } else if (CUR == '(') {
Daniel Veillard76d59b62007-08-22 16:29:21 +00005356 xmlRegStatePtr start, oldend, start0;
Daniel Veillard4255d502002-04-16 15:50:10 +00005357
5358 NEXT;
Haibo Huangcfd91dc2020-07-30 23:01:33 -07005359 if (ctxt->depth >= 50) {
5360 ERROR("xmlFAParseAtom: maximum nesting depth exceeded");
5361 return(-1);
5362 }
Daniel Veillard76d59b62007-08-22 16:29:21 +00005363 /*
5364 * this extra Epsilon transition is needed if we count with 0 allowed
5365 * unfortunately this can't be known at that point
5366 */
5367 xmlFAGenerateEpsilonTransition(ctxt, ctxt->state, NULL);
5368 start0 = ctxt->state;
Daniel Veillard4255d502002-04-16 15:50:10 +00005369 xmlFAGenerateEpsilonTransition(ctxt, ctxt->state, NULL);
5370 start = ctxt->state;
5371 oldend = ctxt->end;
5372 ctxt->end = NULL;
5373 ctxt->atom = NULL;
Haibo Huangcfd91dc2020-07-30 23:01:33 -07005374 ctxt->depth++;
Daniel Veillard4255d502002-04-16 15:50:10 +00005375 xmlFAParseRegExp(ctxt, 0);
Haibo Huangcfd91dc2020-07-30 23:01:33 -07005376 ctxt->depth--;
Daniel Veillard4255d502002-04-16 15:50:10 +00005377 if (CUR == ')') {
5378 NEXT;
5379 } else {
5380 ERROR("xmlFAParseAtom: expecting ')'");
5381 }
5382 ctxt->atom = xmlRegNewAtom(ctxt, XML_REGEXP_SUBREG);
5383 if (ctxt->atom == NULL)
5384 return(-1);
5385 ctxt->atom->start = start;
Daniel Veillard76d59b62007-08-22 16:29:21 +00005386 ctxt->atom->start0 = start0;
Daniel Veillard4255d502002-04-16 15:50:10 +00005387 ctxt->atom->stop = ctxt->state;
5388 ctxt->end = oldend;
5389 return(1);
5390 } else if ((CUR == '[') || (CUR == '\\') || (CUR == '.')) {
5391 xmlFAParseCharClass(ctxt);
5392 return(1);
5393 }
5394 return(0);
5395}
5396
5397/**
5398 * xmlFAParsePiece:
Daniel Veillard441bc322002-04-20 17:38:48 +00005399 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00005400 *
5401 * [3] piece ::= atom quantifier?
5402 */
5403static int
5404xmlFAParsePiece(xmlRegParserCtxtPtr ctxt) {
5405 int ret;
5406
5407 ctxt->atom = NULL;
5408 ret = xmlFAParseAtom(ctxt);
5409 if (ret == 0)
5410 return(0);
5411 if (ctxt->atom == NULL) {
5412 ERROR("internal: no atom generated");
5413 }
5414 xmlFAParseQuantifier(ctxt);
5415 return(1);
5416}
5417
5418/**
5419 * xmlFAParseBranch:
Daniel Veillard441bc322002-04-20 17:38:48 +00005420 * @ctxt: a regexp parser context
Daniel Veillard54eb0242006-03-21 23:17:57 +00005421 * @to: optional target to the end of the branch
5422 *
5423 * @to is used to optimize by removing duplicate path in automata
5424 * in expressions like (a|b)(c|d)
Daniel Veillard4255d502002-04-16 15:50:10 +00005425 *
5426 * [2] branch ::= piece*
5427 */
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00005428static int
Daniel Veillard54eb0242006-03-21 23:17:57 +00005429xmlFAParseBranch(xmlRegParserCtxtPtr ctxt, xmlRegStatePtr to) {
Daniel Veillard4255d502002-04-16 15:50:10 +00005430 xmlRegStatePtr previous;
Daniel Veillard4255d502002-04-16 15:50:10 +00005431 int ret;
5432
5433 previous = ctxt->state;
5434 ret = xmlFAParsePiece(ctxt);
Haibo Huangcfd91dc2020-07-30 23:01:33 -07005435 if (ret == 0) {
5436 /* Empty branch */
5437 xmlFAGenerateEpsilonTransition(ctxt, previous, to);
5438 } else {
Daniel Veillardf8e3db02012-09-11 13:26:36 +08005439 if (xmlFAGenerateTransitions(ctxt, previous,
Haibo Huangcfd91dc2020-07-30 23:01:33 -07005440 (CUR=='|' || CUR==')' || CUR==0) ? to : NULL, ctxt->atom) < 0)
Daniel Veillard2cbf5962004-03-31 15:50:43 +00005441 return(-1);
5442 previous = ctxt->state;
Daniel Veillard4255d502002-04-16 15:50:10 +00005443 ctxt->atom = NULL;
5444 }
5445 while ((ret != 0) && (ctxt->error == 0)) {
5446 ret = xmlFAParsePiece(ctxt);
5447 if (ret != 0) {
Daniel Veillardf8e3db02012-09-11 13:26:36 +08005448 if (xmlFAGenerateTransitions(ctxt, previous,
Haibo Huangcfd91dc2020-07-30 23:01:33 -07005449 (CUR=='|' || CUR==')' || CUR==0) ? to : NULL,
5450 ctxt->atom) < 0)
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00005451 return(-1);
Daniel Veillard4255d502002-04-16 15:50:10 +00005452 previous = ctxt->state;
5453 ctxt->atom = NULL;
5454 }
5455 }
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00005456 return(0);
Daniel Veillard4255d502002-04-16 15:50:10 +00005457}
5458
5459/**
5460 * xmlFAParseRegExp:
Daniel Veillard441bc322002-04-20 17:38:48 +00005461 * @ctxt: a regexp parser context
William M. Brackddf71d62004-05-06 04:17:26 +00005462 * @top: is this the top-level expression ?
Daniel Veillard4255d502002-04-16 15:50:10 +00005463 *
5464 * [1] regExp ::= branch ( '|' branch )*
5465 */
5466static void
5467xmlFAParseRegExp(xmlRegParserCtxtPtr ctxt, int top) {
Daniel Veillardc7e3cc42004-09-28 12:33:52 +00005468 xmlRegStatePtr start, end;
Daniel Veillard4255d502002-04-16 15:50:10 +00005469
Daniel Veillard2cbf5962004-03-31 15:50:43 +00005470 /* if not top start should have been generated by an epsilon trans */
Daniel Veillard4255d502002-04-16 15:50:10 +00005471 start = ctxt->state;
Daniel Veillard2cbf5962004-03-31 15:50:43 +00005472 ctxt->end = NULL;
Daniel Veillard54eb0242006-03-21 23:17:57 +00005473 xmlFAParseBranch(ctxt, NULL);
Daniel Veillard2cbf5962004-03-31 15:50:43 +00005474 if (top) {
5475#ifdef DEBUG_REGEXP_GRAPH
5476 printf("State %d is final\n", ctxt->state->no);
5477#endif
5478 ctxt->state->type = XML_REGEXP_FINAL_STATE;
5479 }
Daniel Veillard4255d502002-04-16 15:50:10 +00005480 if (CUR != '|') {
5481 ctxt->end = ctxt->state;
5482 return;
5483 }
5484 end = ctxt->state;
5485 while ((CUR == '|') && (ctxt->error == 0)) {
5486 NEXT;
5487 ctxt->state = start;
Daniel Veillard2cbf5962004-03-31 15:50:43 +00005488 ctxt->end = NULL;
Daniel Veillard54eb0242006-03-21 23:17:57 +00005489 xmlFAParseBranch(ctxt, end);
Daniel Veillard4255d502002-04-16 15:50:10 +00005490 }
Daniel Veillard2cbf5962004-03-31 15:50:43 +00005491 if (!top) {
5492 ctxt->state = end;
5493 ctxt->end = end;
5494 }
Daniel Veillard4255d502002-04-16 15:50:10 +00005495}
5496
5497/************************************************************************
Daniel Veillardf8e3db02012-09-11 13:26:36 +08005498 * *
5499 * The basic API *
5500 * *
Daniel Veillard4255d502002-04-16 15:50:10 +00005501 ************************************************************************/
5502
5503/**
5504 * xmlRegexpPrint:
5505 * @output: the file for the output debug
5506 * @regexp: the compiled regexp
5507 *
5508 * Print the content of the compiled regular expression
5509 */
5510void
5511xmlRegexpPrint(FILE *output, xmlRegexpPtr regexp) {
5512 int i;
5513
Daniel Veillarda82b1822004-11-08 16:24:57 +00005514 if (output == NULL)
5515 return;
Daniel Veillard4255d502002-04-16 15:50:10 +00005516 fprintf(output, " regexp: ");
5517 if (regexp == NULL) {
5518 fprintf(output, "NULL\n");
5519 return;
5520 }
5521 fprintf(output, "'%s' ", regexp->string);
5522 fprintf(output, "\n");
5523 fprintf(output, "%d atoms:\n", regexp->nbAtoms);
5524 for (i = 0;i < regexp->nbAtoms; i++) {
5525 fprintf(output, " %02d ", i);
5526 xmlRegPrintAtom(output, regexp->atoms[i]);
5527 }
5528 fprintf(output, "%d states:", regexp->nbStates);
5529 fprintf(output, "\n");
5530 for (i = 0;i < regexp->nbStates; i++) {
5531 xmlRegPrintState(output, regexp->states[i]);
5532 }
5533 fprintf(output, "%d counters:\n", regexp->nbCounters);
5534 for (i = 0;i < regexp->nbCounters; i++) {
5535 fprintf(output, " %d: min %d max %d\n", i, regexp->counters[i].min,
5536 regexp->counters[i].max);
5537 }
5538}
5539
5540/**
5541 * xmlRegexpCompile:
5542 * @regexp: a regular expression string
5543 *
5544 * Parses a regular expression conforming to XML Schemas Part 2 Datatype
William M. Brackddf71d62004-05-06 04:17:26 +00005545 * Appendix F and builds an automata suitable for testing strings against
Daniel Veillard4255d502002-04-16 15:50:10 +00005546 * that regular expression
5547 *
5548 * Returns the compiled expression or NULL in case of error
5549 */
5550xmlRegexpPtr
5551xmlRegexpCompile(const xmlChar *regexp) {
5552 xmlRegexpPtr ret;
5553 xmlRegParserCtxtPtr ctxt;
5554
5555 ctxt = xmlRegNewParserCtxt(regexp);
5556 if (ctxt == NULL)
5557 return(NULL);
5558
5559 /* initialize the parser */
5560 ctxt->end = NULL;
5561 ctxt->start = ctxt->state = xmlRegNewState(ctxt);
5562 xmlRegStatePush(ctxt, ctxt->start);
5563
5564 /* parse the expression building an automata */
5565 xmlFAParseRegExp(ctxt, 1);
5566 if (CUR != 0) {
5567 ERROR("xmlFAParseRegExp: extra characters");
5568 }
Daniel Veillardcb4284e2007-04-25 13:55:20 +00005569 if (ctxt->error != 0) {
5570 xmlRegFreeParserCtxt(ctxt);
5571 return(NULL);
5572 }
Daniel Veillard4255d502002-04-16 15:50:10 +00005573 ctxt->end = ctxt->state;
5574 ctxt->start->type = XML_REGEXP_START_STATE;
5575 ctxt->end->type = XML_REGEXP_FINAL_STATE;
5576
5577 /* remove the Epsilon except for counted transitions */
5578 xmlFAEliminateEpsilonTransitions(ctxt);
5579
5580
5581 if (ctxt->error != 0) {
5582 xmlRegFreeParserCtxt(ctxt);
5583 return(NULL);
5584 }
5585 ret = xmlRegEpxFromParse(ctxt);
5586 xmlRegFreeParserCtxt(ctxt);
5587 return(ret);
5588}
5589
5590/**
5591 * xmlRegexpExec:
5592 * @comp: the compiled regular expression
5593 * @content: the value to check against the regular expression
5594 *
William M. Brackddf71d62004-05-06 04:17:26 +00005595 * Check if the regular expression generates the value
Daniel Veillard4255d502002-04-16 15:50:10 +00005596 *
William M. Brackddf71d62004-05-06 04:17:26 +00005597 * Returns 1 if it matches, 0 if not and a negative value in case of error
Daniel Veillard4255d502002-04-16 15:50:10 +00005598 */
5599int
5600xmlRegexpExec(xmlRegexpPtr comp, const xmlChar *content) {
5601 if ((comp == NULL) || (content == NULL))
5602 return(-1);
5603 return(xmlFARegExec(comp, content));
5604}
5605
5606/**
Daniel Veillard23e73572002-09-19 19:56:43 +00005607 * xmlRegexpIsDeterminist:
5608 * @comp: the compiled regular expression
5609 *
5610 * Check if the regular expression is determinist
5611 *
William M. Brackddf71d62004-05-06 04:17:26 +00005612 * Returns 1 if it yes, 0 if not and a negative value in case of error
Daniel Veillard23e73572002-09-19 19:56:43 +00005613 */
5614int
5615xmlRegexpIsDeterminist(xmlRegexpPtr comp) {
5616 xmlAutomataPtr am;
5617 int ret;
5618
5619 if (comp == NULL)
5620 return(-1);
5621 if (comp->determinist != -1)
5622 return(comp->determinist);
5623
5624 am = xmlNewAutomata();
Haibo Huangcfd91dc2020-07-30 23:01:33 -07005625 if (am == NULL)
5626 return(-1);
Daniel Veillardbd9afb52002-09-25 22:25:35 +00005627 if (am->states != NULL) {
5628 int i;
5629
5630 for (i = 0;i < am->nbStates;i++)
5631 xmlRegFreeState(am->states[i]);
5632 xmlFree(am->states);
5633 }
Daniel Veillard23e73572002-09-19 19:56:43 +00005634 am->nbAtoms = comp->nbAtoms;
5635 am->atoms = comp->atoms;
5636 am->nbStates = comp->nbStates;
5637 am->states = comp->states;
5638 am->determinist = -1;
Daniel Veillard1ba2aca2009-08-31 16:47:39 +02005639 am->flags = comp->flags;
Daniel Veillard23e73572002-09-19 19:56:43 +00005640 ret = xmlFAComputesDeterminism(am);
5641 am->atoms = NULL;
5642 am->states = NULL;
5643 xmlFreeAutomata(am);
Daniel Veillard1ba2aca2009-08-31 16:47:39 +02005644 comp->determinist = ret;
Daniel Veillard23e73572002-09-19 19:56:43 +00005645 return(ret);
5646}
5647
5648/**
Daniel Veillard4255d502002-04-16 15:50:10 +00005649 * xmlRegFreeRegexp:
5650 * @regexp: the regexp
5651 *
5652 * Free a regexp
5653 */
5654void
5655xmlRegFreeRegexp(xmlRegexpPtr regexp) {
5656 int i;
5657 if (regexp == NULL)
5658 return;
5659
5660 if (regexp->string != NULL)
5661 xmlFree(regexp->string);
5662 if (regexp->states != NULL) {
5663 for (i = 0;i < regexp->nbStates;i++)
5664 xmlRegFreeState(regexp->states[i]);
5665 xmlFree(regexp->states);
5666 }
5667 if (regexp->atoms != NULL) {
5668 for (i = 0;i < regexp->nbAtoms;i++)
5669 xmlRegFreeAtom(regexp->atoms[i]);
5670 xmlFree(regexp->atoms);
5671 }
5672 if (regexp->counters != NULL)
5673 xmlFree(regexp->counters);
Daniel Veillard23e73572002-09-19 19:56:43 +00005674 if (regexp->compact != NULL)
5675 xmlFree(regexp->compact);
Daniel Veillard118aed72002-09-24 14:13:13 +00005676 if (regexp->transdata != NULL)
5677 xmlFree(regexp->transdata);
Daniel Veillard23e73572002-09-19 19:56:43 +00005678 if (regexp->stringMap != NULL) {
5679 for (i = 0; i < regexp->nbstrings;i++)
5680 xmlFree(regexp->stringMap[i]);
5681 xmlFree(regexp->stringMap);
5682 }
5683
Daniel Veillard4255d502002-04-16 15:50:10 +00005684 xmlFree(regexp);
5685}
5686
5687#ifdef LIBXML_AUTOMATA_ENABLED
5688/************************************************************************
Daniel Veillardf8e3db02012-09-11 13:26:36 +08005689 * *
5690 * The Automata interface *
5691 * *
Daniel Veillard4255d502002-04-16 15:50:10 +00005692 ************************************************************************/
5693
5694/**
5695 * xmlNewAutomata:
5696 *
5697 * Create a new automata
5698 *
5699 * Returns the new object or NULL in case of failure
5700 */
5701xmlAutomataPtr
5702xmlNewAutomata(void) {
5703 xmlAutomataPtr ctxt;
5704
5705 ctxt = xmlRegNewParserCtxt(NULL);
5706 if (ctxt == NULL)
5707 return(NULL);
5708
5709 /* initialize the parser */
5710 ctxt->end = NULL;
5711 ctxt->start = ctxt->state = xmlRegNewState(ctxt);
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00005712 if (ctxt->start == NULL) {
5713 xmlFreeAutomata(ctxt);
5714 return(NULL);
5715 }
Daniel Veillardd0271472006-01-02 10:22:02 +00005716 ctxt->start->type = XML_REGEXP_START_STATE;
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00005717 if (xmlRegStatePush(ctxt, ctxt->start) < 0) {
5718 xmlRegFreeState(ctxt->start);
5719 xmlFreeAutomata(ctxt);
5720 return(NULL);
5721 }
Daniel Veillard1ba2aca2009-08-31 16:47:39 +02005722 ctxt->flags = 0;
Daniel Veillard4255d502002-04-16 15:50:10 +00005723
5724 return(ctxt);
5725}
5726
5727/**
5728 * xmlFreeAutomata:
5729 * @am: an automata
5730 *
5731 * Free an automata
5732 */
5733void
5734xmlFreeAutomata(xmlAutomataPtr am) {
5735 if (am == NULL)
5736 return;
5737 xmlRegFreeParserCtxt(am);
5738}
5739
5740/**
Daniel Veillard29341682009-09-10 18:23:39 +02005741 * xmlAutomataSetFlags:
Daniel Veillard1ba2aca2009-08-31 16:47:39 +02005742 * @am: an automata
5743 * @flags: a set of internal flags
5744 *
5745 * Set some flags on the automata
5746 */
5747void
5748xmlAutomataSetFlags(xmlAutomataPtr am, int flags) {
5749 if (am == NULL)
5750 return;
5751 am->flags |= flags;
5752}
5753
5754/**
Daniel Veillard4255d502002-04-16 15:50:10 +00005755 * xmlAutomataGetInitState:
5756 * @am: an automata
5757 *
Daniel Veillarda9b66d02002-12-11 14:23:49 +00005758 * Initial state lookup
5759 *
Daniel Veillard4255d502002-04-16 15:50:10 +00005760 * Returns the initial state of the automata
5761 */
5762xmlAutomataStatePtr
5763xmlAutomataGetInitState(xmlAutomataPtr am) {
5764 if (am == NULL)
5765 return(NULL);
5766 return(am->start);
5767}
5768
5769/**
5770 * xmlAutomataSetFinalState:
5771 * @am: an automata
5772 * @state: a state in this automata
5773 *
5774 * Makes that state a final state
5775 *
5776 * Returns 0 or -1 in case of error
5777 */
5778int
5779xmlAutomataSetFinalState(xmlAutomataPtr am, xmlAutomataStatePtr state) {
5780 if ((am == NULL) || (state == NULL))
5781 return(-1);
5782 state->type = XML_REGEXP_FINAL_STATE;
5783 return(0);
5784}
5785
5786/**
5787 * xmlAutomataNewTransition:
5788 * @am: an automata
5789 * @from: the starting point of the transition
5790 * @to: the target point of the transition or NULL
5791 * @token: the input string associated to that transition
5792 * @data: data passed to the callback function if the transition is activated
5793 *
William M. Brackddf71d62004-05-06 04:17:26 +00005794 * If @to is NULL, this creates first a new target state in the automata
Daniel Veillard4255d502002-04-16 15:50:10 +00005795 * and then adds a transition from the @from state to the target state
5796 * activated by the value of @token
5797 *
5798 * Returns the target state or NULL in case of error
5799 */
5800xmlAutomataStatePtr
5801xmlAutomataNewTransition(xmlAutomataPtr am, xmlAutomataStatePtr from,
5802 xmlAutomataStatePtr to, const xmlChar *token,
5803 void *data) {
5804 xmlRegAtomPtr atom;
5805
5806 if ((am == NULL) || (from == NULL) || (token == NULL))
5807 return(NULL);
5808 atom = xmlRegNewAtom(am, XML_REGEXP_STRING);
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00005809 if (atom == NULL)
5810 return(NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +00005811 atom->data = data;
Daniel Veillard4255d502002-04-16 15:50:10 +00005812 atom->valuep = xmlStrdup(token);
5813
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00005814 if (xmlFAGenerateTransitions(am, from, to, atom) < 0) {
5815 xmlRegFreeAtom(atom);
5816 return(NULL);
5817 }
Daniel Veillard4255d502002-04-16 15:50:10 +00005818 if (to == NULL)
5819 return(am->state);
5820 return(to);
5821}
5822
5823/**
Daniel Veillard52b48c72003-04-13 19:53:42 +00005824 * xmlAutomataNewTransition2:
5825 * @am: an automata
5826 * @from: the starting point of the transition
5827 * @to: the target point of the transition or NULL
5828 * @token: the first input string associated to that transition
5829 * @token2: the second input string associated to that transition
5830 * @data: data passed to the callback function if the transition is activated
5831 *
William M. Brackddf71d62004-05-06 04:17:26 +00005832 * If @to is NULL, this creates first a new target state in the automata
Daniel Veillard52b48c72003-04-13 19:53:42 +00005833 * and then adds a transition from the @from state to the target state
5834 * activated by the value of @token
5835 *
5836 * Returns the target state or NULL in case of error
5837 */
5838xmlAutomataStatePtr
5839xmlAutomataNewTransition2(xmlAutomataPtr am, xmlAutomataStatePtr from,
5840 xmlAutomataStatePtr to, const xmlChar *token,
5841 const xmlChar *token2, void *data) {
5842 xmlRegAtomPtr atom;
5843
5844 if ((am == NULL) || (from == NULL) || (token == NULL))
5845 return(NULL);
5846 atom = xmlRegNewAtom(am, XML_REGEXP_STRING);
Daniel Veillard52b48c72003-04-13 19:53:42 +00005847 if (atom == NULL)
5848 return(NULL);
Daniel Veillard11ce4002006-03-10 00:36:23 +00005849 atom->data = data;
Daniel Veillard52b48c72003-04-13 19:53:42 +00005850 if ((token2 == NULL) || (*token2 == 0)) {
5851 atom->valuep = xmlStrdup(token);
5852 } else {
5853 int lenn, lenp;
5854 xmlChar *str;
5855
5856 lenn = strlen((char *) token2);
5857 lenp = strlen((char *) token);
5858
Daniel Veillard3c908dc2003-04-19 00:07:51 +00005859 str = (xmlChar *) xmlMallocAtomic(lenn + lenp + 2);
Daniel Veillard52b48c72003-04-13 19:53:42 +00005860 if (str == NULL) {
5861 xmlRegFreeAtom(atom);
5862 return(NULL);
5863 }
5864 memcpy(&str[0], token, lenp);
5865 str[lenp] = '|';
5866 memcpy(&str[lenp + 1], token2, lenn);
5867 str[lenn + lenp + 1] = 0;
5868
5869 atom->valuep = str;
5870 }
5871
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00005872 if (xmlFAGenerateTransitions(am, from, to, atom) < 0) {
5873 xmlRegFreeAtom(atom);
5874 return(NULL);
5875 }
Daniel Veillard52b48c72003-04-13 19:53:42 +00005876 if (to == NULL)
5877 return(am->state);
5878 return(to);
5879}
5880
5881/**
Daniel Veillard9efc4762005-07-19 14:33:55 +00005882 * xmlAutomataNewNegTrans:
5883 * @am: an automata
5884 * @from: the starting point of the transition
5885 * @to: the target point of the transition or NULL
5886 * @token: the first input string associated to that transition
5887 * @token2: the second input string associated to that transition
5888 * @data: data passed to the callback function if the transition is activated
5889 *
5890 * If @to is NULL, this creates first a new target state in the automata
5891 * and then adds a transition from the @from state to the target state
5892 * activated by any value except (@token,@token2)
Daniel Veillard6e65e152005-08-09 11:09:52 +00005893 * Note that if @token2 is not NULL, then (X, NULL) won't match to follow
5894 # the semantic of XSD ##other
Daniel Veillard9efc4762005-07-19 14:33:55 +00005895 *
5896 * Returns the target state or NULL in case of error
5897 */
5898xmlAutomataStatePtr
5899xmlAutomataNewNegTrans(xmlAutomataPtr am, xmlAutomataStatePtr from,
5900 xmlAutomataStatePtr to, const xmlChar *token,
5901 const xmlChar *token2, void *data) {
5902 xmlRegAtomPtr atom;
Daniel Veillard77005e62005-07-19 16:26:18 +00005903 xmlChar err_msg[200];
Daniel Veillard9efc4762005-07-19 14:33:55 +00005904
5905 if ((am == NULL) || (from == NULL) || (token == NULL))
5906 return(NULL);
5907 atom = xmlRegNewAtom(am, XML_REGEXP_STRING);
5908 if (atom == NULL)
5909 return(NULL);
5910 atom->data = data;
5911 atom->neg = 1;
5912 if ((token2 == NULL) || (*token2 == 0)) {
5913 atom->valuep = xmlStrdup(token);
5914 } else {
5915 int lenn, lenp;
5916 xmlChar *str;
5917
5918 lenn = strlen((char *) token2);
5919 lenp = strlen((char *) token);
5920
5921 str = (xmlChar *) xmlMallocAtomic(lenn + lenp + 2);
5922 if (str == NULL) {
5923 xmlRegFreeAtom(atom);
5924 return(NULL);
5925 }
5926 memcpy(&str[0], token, lenp);
5927 str[lenp] = '|';
5928 memcpy(&str[lenp + 1], token2, lenn);
5929 str[lenn + lenp + 1] = 0;
5930
5931 atom->valuep = str;
5932 }
Daniel Veillarddb68b742005-07-30 13:18:24 +00005933 snprintf((char *) err_msg, 199, "not %s", (const char *) atom->valuep);
Daniel Veillard77005e62005-07-19 16:26:18 +00005934 err_msg[199] = 0;
5935 atom->valuep2 = xmlStrdup(err_msg);
Daniel Veillard9efc4762005-07-19 14:33:55 +00005936
5937 if (xmlFAGenerateTransitions(am, from, to, atom) < 0) {
5938 xmlRegFreeAtom(atom);
5939 return(NULL);
5940 }
Daniel Veillard6e65e152005-08-09 11:09:52 +00005941 am->negs++;
Daniel Veillard9efc4762005-07-19 14:33:55 +00005942 if (to == NULL)
5943 return(am->state);
5944 return(to);
5945}
5946
5947/**
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00005948 * xmlAutomataNewCountTrans2:
5949 * @am: an automata
5950 * @from: the starting point of the transition
5951 * @to: the target point of the transition or NULL
5952 * @token: the input string associated to that transition
5953 * @token2: the second input string associated to that transition
Haibo Huangcfd91dc2020-07-30 23:01:33 -07005954 * @min: the minimum successive occurrences of token
5955 * @max: the maximum successive occurrences of token
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00005956 * @data: data associated to the transition
5957 *
5958 * If @to is NULL, this creates first a new target state in the automata
5959 * and then adds a transition from the @from state to the target state
Daniel Veillardf8e3db02012-09-11 13:26:36 +08005960 * activated by a succession of input of value @token and @token2 and
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00005961 * whose number is between @min and @max
5962 *
5963 * Returns the target state or NULL in case of error
5964 */
5965xmlAutomataStatePtr
5966xmlAutomataNewCountTrans2(xmlAutomataPtr am, xmlAutomataStatePtr from,
5967 xmlAutomataStatePtr to, const xmlChar *token,
5968 const xmlChar *token2,
5969 int min, int max, void *data) {
5970 xmlRegAtomPtr atom;
5971 int counter;
5972
5973 if ((am == NULL) || (from == NULL) || (token == NULL))
5974 return(NULL);
5975 if (min < 0)
5976 return(NULL);
5977 if ((max < min) || (max < 1))
5978 return(NULL);
5979 atom = xmlRegNewAtom(am, XML_REGEXP_STRING);
5980 if (atom == NULL)
5981 return(NULL);
5982 if ((token2 == NULL) || (*token2 == 0)) {
5983 atom->valuep = xmlStrdup(token);
5984 } else {
5985 int lenn, lenp;
5986 xmlChar *str;
5987
5988 lenn = strlen((char *) token2);
5989 lenp = strlen((char *) token);
5990
5991 str = (xmlChar *) xmlMallocAtomic(lenn + lenp + 2);
5992 if (str == NULL) {
5993 xmlRegFreeAtom(atom);
5994 return(NULL);
5995 }
5996 memcpy(&str[0], token, lenp);
5997 str[lenp] = '|';
5998 memcpy(&str[lenp + 1], token2, lenn);
5999 str[lenn + lenp + 1] = 0;
6000
6001 atom->valuep = str;
6002 }
6003 atom->data = data;
6004 if (min == 0)
6005 atom->min = 1;
6006 else
6007 atom->min = min;
6008 atom->max = max;
6009
6010 /*
6011 * associate a counter to the transition.
6012 */
6013 counter = xmlRegGetCounter(am);
6014 am->counters[counter].min = min;
6015 am->counters[counter].max = max;
6016
6017 /* xmlFAGenerateTransitions(am, from, to, atom); */
6018 if (to == NULL) {
6019 to = xmlRegNewState(am);
6020 xmlRegStatePush(am, to);
6021 }
Daniel Veillard5de09382005-09-26 17:18:17 +00006022 xmlRegStateAddTrans(am, from, atom, to, counter, -1);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00006023 xmlRegAtomPush(am, atom);
6024 am->state = to;
6025
6026 if (to == NULL)
6027 to = am->state;
6028 if (to == NULL)
6029 return(NULL);
6030 if (min == 0)
6031 xmlFAGenerateEpsilonTransition(am, from, to);
6032 return(to);
6033}
6034
6035/**
Daniel Veillard4255d502002-04-16 15:50:10 +00006036 * xmlAutomataNewCountTrans:
6037 * @am: an automata
6038 * @from: the starting point of the transition
6039 * @to: the target point of the transition or NULL
6040 * @token: the input string associated to that transition
Haibo Huangcfd91dc2020-07-30 23:01:33 -07006041 * @min: the minimum successive occurrences of token
6042 * @max: the maximum successive occurrences of token
Daniel Veillarda9b66d02002-12-11 14:23:49 +00006043 * @data: data associated to the transition
Daniel Veillard4255d502002-04-16 15:50:10 +00006044 *
William M. Brackddf71d62004-05-06 04:17:26 +00006045 * If @to is NULL, this creates first a new target state in the automata
Daniel Veillard4255d502002-04-16 15:50:10 +00006046 * and then adds a transition from the @from state to the target state
6047 * activated by a succession of input of value @token and whose number
6048 * is between @min and @max
6049 *
6050 * Returns the target state or NULL in case of error
6051 */
6052xmlAutomataStatePtr
6053xmlAutomataNewCountTrans(xmlAutomataPtr am, xmlAutomataStatePtr from,
6054 xmlAutomataStatePtr to, const xmlChar *token,
6055 int min, int max, void *data) {
6056 xmlRegAtomPtr atom;
Daniel Veillard0ddb21c2004-02-12 12:43:49 +00006057 int counter;
Daniel Veillard4255d502002-04-16 15:50:10 +00006058
6059 if ((am == NULL) || (from == NULL) || (token == NULL))
6060 return(NULL);
6061 if (min < 0)
6062 return(NULL);
6063 if ((max < min) || (max < 1))
6064 return(NULL);
6065 atom = xmlRegNewAtom(am, XML_REGEXP_STRING);
6066 if (atom == NULL)
6067 return(NULL);
6068 atom->valuep = xmlStrdup(token);
6069 atom->data = data;
6070 if (min == 0)
6071 atom->min = 1;
6072 else
6073 atom->min = min;
6074 atom->max = max;
6075
Daniel Veillard0ddb21c2004-02-12 12:43:49 +00006076 /*
6077 * associate a counter to the transition.
6078 */
6079 counter = xmlRegGetCounter(am);
6080 am->counters[counter].min = min;
6081 am->counters[counter].max = max;
6082
6083 /* xmlFAGenerateTransitions(am, from, to, atom); */
6084 if (to == NULL) {
6085 to = xmlRegNewState(am);
6086 xmlRegStatePush(am, to);
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00006087 }
Daniel Veillard5de09382005-09-26 17:18:17 +00006088 xmlRegStateAddTrans(am, from, atom, to, counter, -1);
Daniel Veillard0ddb21c2004-02-12 12:43:49 +00006089 xmlRegAtomPush(am, atom);
6090 am->state = to;
6091
Daniel Veillard4255d502002-04-16 15:50:10 +00006092 if (to == NULL)
6093 to = am->state;
6094 if (to == NULL)
6095 return(NULL);
6096 if (min == 0)
6097 xmlFAGenerateEpsilonTransition(am, from, to);
6098 return(to);
6099}
6100
6101/**
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00006102 * xmlAutomataNewOnceTrans2:
6103 * @am: an automata
6104 * @from: the starting point of the transition
6105 * @to: the target point of the transition or NULL
6106 * @token: the input string associated to that transition
6107 * @token2: the second input string associated to that transition
Haibo Huangcfd91dc2020-07-30 23:01:33 -07006108 * @min: the minimum successive occurrences of token
6109 * @max: the maximum successive occurrences of token
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00006110 * @data: data associated to the transition
6111 *
6112 * If @to is NULL, this creates first a new target state in the automata
6113 * and then adds a transition from the @from state to the target state
Daniel Veillardf8e3db02012-09-11 13:26:36 +08006114 * activated by a succession of input of value @token and @token2 and whose
6115 * number is between @min and @max, moreover that transition can only be
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00006116 * crossed once.
6117 *
6118 * Returns the target state or NULL in case of error
6119 */
6120xmlAutomataStatePtr
6121xmlAutomataNewOnceTrans2(xmlAutomataPtr am, xmlAutomataStatePtr from,
6122 xmlAutomataStatePtr to, const xmlChar *token,
6123 const xmlChar *token2,
6124 int min, int max, void *data) {
6125 xmlRegAtomPtr atom;
6126 int counter;
6127
6128 if ((am == NULL) || (from == NULL) || (token == NULL))
6129 return(NULL);
6130 if (min < 1)
6131 return(NULL);
Haibo Huangcfd91dc2020-07-30 23:01:33 -07006132 if (max < min)
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00006133 return(NULL);
6134 atom = xmlRegNewAtom(am, XML_REGEXP_STRING);
6135 if (atom == NULL)
6136 return(NULL);
6137 if ((token2 == NULL) || (*token2 == 0)) {
6138 atom->valuep = xmlStrdup(token);
6139 } else {
6140 int lenn, lenp;
6141 xmlChar *str;
6142
6143 lenn = strlen((char *) token2);
6144 lenp = strlen((char *) token);
6145
6146 str = (xmlChar *) xmlMallocAtomic(lenn + lenp + 2);
6147 if (str == NULL) {
6148 xmlRegFreeAtom(atom);
6149 return(NULL);
6150 }
6151 memcpy(&str[0], token, lenp);
6152 str[lenp] = '|';
6153 memcpy(&str[lenp + 1], token2, lenn);
6154 str[lenn + lenp + 1] = 0;
6155
6156 atom->valuep = str;
Daniel Veillardf8e3db02012-09-11 13:26:36 +08006157 }
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00006158 atom->data = data;
6159 atom->quant = XML_REGEXP_QUANT_ONCEONLY;
Daniel Veillard11ce4002006-03-10 00:36:23 +00006160 atom->min = min;
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00006161 atom->max = max;
6162 /*
6163 * associate a counter to the transition.
6164 */
6165 counter = xmlRegGetCounter(am);
6166 am->counters[counter].min = 1;
6167 am->counters[counter].max = 1;
6168
6169 /* xmlFAGenerateTransitions(am, from, to, atom); */
6170 if (to == NULL) {
6171 to = xmlRegNewState(am);
6172 xmlRegStatePush(am, to);
6173 }
Daniel Veillard5de09382005-09-26 17:18:17 +00006174 xmlRegStateAddTrans(am, from, atom, to, counter, -1);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00006175 xmlRegAtomPush(am, atom);
6176 am->state = to;
6177 return(to);
6178}
6179
Daniel Veillardf8e3db02012-09-11 13:26:36 +08006180
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00006181
6182/**
Daniel Veillard7646b182002-04-20 06:41:40 +00006183 * xmlAutomataNewOnceTrans:
6184 * @am: an automata
6185 * @from: the starting point of the transition
6186 * @to: the target point of the transition or NULL
6187 * @token: the input string associated to that transition
Haibo Huangcfd91dc2020-07-30 23:01:33 -07006188 * @min: the minimum successive occurrences of token
6189 * @max: the maximum successive occurrences of token
Daniel Veillarda9b66d02002-12-11 14:23:49 +00006190 * @data: data associated to the transition
Daniel Veillard7646b182002-04-20 06:41:40 +00006191 *
William M. Brackddf71d62004-05-06 04:17:26 +00006192 * If @to is NULL, this creates first a new target state in the automata
Daniel Veillard7646b182002-04-20 06:41:40 +00006193 * and then adds a transition from the @from state to the target state
6194 * activated by a succession of input of value @token and whose number
William M. Brackddf71d62004-05-06 04:17:26 +00006195 * is between @min and @max, moreover that transition can only be crossed
Daniel Veillard7646b182002-04-20 06:41:40 +00006196 * once.
6197 *
6198 * Returns the target state or NULL in case of error
6199 */
6200xmlAutomataStatePtr
6201xmlAutomataNewOnceTrans(xmlAutomataPtr am, xmlAutomataStatePtr from,
6202 xmlAutomataStatePtr to, const xmlChar *token,
6203 int min, int max, void *data) {
6204 xmlRegAtomPtr atom;
6205 int counter;
6206
6207 if ((am == NULL) || (from == NULL) || (token == NULL))
6208 return(NULL);
6209 if (min < 1)
6210 return(NULL);
Haibo Huangcfd91dc2020-07-30 23:01:33 -07006211 if (max < min)
Daniel Veillard7646b182002-04-20 06:41:40 +00006212 return(NULL);
6213 atom = xmlRegNewAtom(am, XML_REGEXP_STRING);
6214 if (atom == NULL)
6215 return(NULL);
6216 atom->valuep = xmlStrdup(token);
6217 atom->data = data;
6218 atom->quant = XML_REGEXP_QUANT_ONCEONLY;
Daniel Veillard11ce4002006-03-10 00:36:23 +00006219 atom->min = min;
Daniel Veillard7646b182002-04-20 06:41:40 +00006220 atom->max = max;
6221 /*
6222 * associate a counter to the transition.
6223 */
6224 counter = xmlRegGetCounter(am);
6225 am->counters[counter].min = 1;
6226 am->counters[counter].max = 1;
6227
6228 /* xmlFAGenerateTransitions(am, from, to, atom); */
6229 if (to == NULL) {
6230 to = xmlRegNewState(am);
6231 xmlRegStatePush(am, to);
6232 }
Daniel Veillard5de09382005-09-26 17:18:17 +00006233 xmlRegStateAddTrans(am, from, atom, to, counter, -1);
Daniel Veillard7646b182002-04-20 06:41:40 +00006234 xmlRegAtomPush(am, atom);
6235 am->state = to;
Daniel Veillard7646b182002-04-20 06:41:40 +00006236 return(to);
6237}
6238
6239/**
Daniel Veillard4255d502002-04-16 15:50:10 +00006240 * xmlAutomataNewState:
6241 * @am: an automata
6242 *
6243 * Create a new disconnected state in the automata
6244 *
6245 * Returns the new state or NULL in case of error
6246 */
6247xmlAutomataStatePtr
6248xmlAutomataNewState(xmlAutomataPtr am) {
Daniel Veillardf8e3db02012-09-11 13:26:36 +08006249 xmlAutomataStatePtr to;
Daniel Veillard4255d502002-04-16 15:50:10 +00006250
6251 if (am == NULL)
6252 return(NULL);
6253 to = xmlRegNewState(am);
6254 xmlRegStatePush(am, to);
6255 return(to);
6256}
6257
6258/**
Daniel Veillarda9b66d02002-12-11 14:23:49 +00006259 * xmlAutomataNewEpsilon:
Daniel Veillard4255d502002-04-16 15:50:10 +00006260 * @am: an automata
6261 * @from: the starting point of the transition
6262 * @to: the target point of the transition or NULL
6263 *
William M. Brackddf71d62004-05-06 04:17:26 +00006264 * If @to is NULL, this creates first a new target state in the automata
6265 * and then adds an epsilon transition from the @from state to the
Daniel Veillard4255d502002-04-16 15:50:10 +00006266 * target state
6267 *
6268 * Returns the target state or NULL in case of error
6269 */
6270xmlAutomataStatePtr
6271xmlAutomataNewEpsilon(xmlAutomataPtr am, xmlAutomataStatePtr from,
6272 xmlAutomataStatePtr to) {
6273 if ((am == NULL) || (from == NULL))
6274 return(NULL);
6275 xmlFAGenerateEpsilonTransition(am, from, to);
6276 if (to == NULL)
6277 return(am->state);
6278 return(to);
6279}
6280
Daniel Veillardb509f152002-04-17 16:28:10 +00006281/**
Daniel Veillard7646b182002-04-20 06:41:40 +00006282 * xmlAutomataNewAllTrans:
6283 * @am: an automata
6284 * @from: the starting point of the transition
6285 * @to: the target point of the transition or NULL
Daniel Veillarda9b66d02002-12-11 14:23:49 +00006286 * @lax: allow to transition if not all all transitions have been activated
Daniel Veillard7646b182002-04-20 06:41:40 +00006287 *
William M. Brackddf71d62004-05-06 04:17:26 +00006288 * If @to is NULL, this creates first a new target state in the automata
Daniel Veillard7646b182002-04-20 06:41:40 +00006289 * and then adds a an ALL transition from the @from state to the
6290 * target state. That transition is an epsilon transition allowed only when
6291 * all transitions from the @from node have been activated.
6292 *
6293 * Returns the target state or NULL in case of error
6294 */
6295xmlAutomataStatePtr
6296xmlAutomataNewAllTrans(xmlAutomataPtr am, xmlAutomataStatePtr from,
Daniel Veillard441bc322002-04-20 17:38:48 +00006297 xmlAutomataStatePtr to, int lax) {
Daniel Veillard7646b182002-04-20 06:41:40 +00006298 if ((am == NULL) || (from == NULL))
6299 return(NULL);
Daniel Veillard441bc322002-04-20 17:38:48 +00006300 xmlFAGenerateAllTransition(am, from, to, lax);
Daniel Veillard7646b182002-04-20 06:41:40 +00006301 if (to == NULL)
6302 return(am->state);
6303 return(to);
6304}
6305
6306/**
Daniel Veillardb509f152002-04-17 16:28:10 +00006307 * xmlAutomataNewCounter:
6308 * @am: an automata
6309 * @min: the minimal value on the counter
6310 * @max: the maximal value on the counter
6311 *
6312 * Create a new counter
6313 *
6314 * Returns the counter number or -1 in case of error
6315 */
Daniel Veillardf8e3db02012-09-11 13:26:36 +08006316int
Daniel Veillardb509f152002-04-17 16:28:10 +00006317xmlAutomataNewCounter(xmlAutomataPtr am, int min, int max) {
6318 int ret;
6319
6320 if (am == NULL)
6321 return(-1);
6322
6323 ret = xmlRegGetCounter(am);
6324 if (ret < 0)
6325 return(-1);
6326 am->counters[ret].min = min;
6327 am->counters[ret].max = max;
6328 return(ret);
6329}
6330
6331/**
6332 * xmlAutomataNewCountedTrans:
6333 * @am: an automata
6334 * @from: the starting point of the transition
6335 * @to: the target point of the transition or NULL
6336 * @counter: the counter associated to that transition
6337 *
William M. Brackddf71d62004-05-06 04:17:26 +00006338 * If @to is NULL, this creates first a new target state in the automata
Daniel Veillardb509f152002-04-17 16:28:10 +00006339 * and then adds an epsilon transition from the @from state to the target state
6340 * which will increment the counter provided
6341 *
6342 * Returns the target state or NULL in case of error
6343 */
6344xmlAutomataStatePtr
6345xmlAutomataNewCountedTrans(xmlAutomataPtr am, xmlAutomataStatePtr from,
6346 xmlAutomataStatePtr to, int counter) {
6347 if ((am == NULL) || (from == NULL) || (counter < 0))
6348 return(NULL);
6349 xmlFAGenerateCountedEpsilonTransition(am, from, to, counter);
6350 if (to == NULL)
6351 return(am->state);
6352 return(to);
6353}
6354
6355/**
6356 * xmlAutomataNewCounterTrans:
6357 * @am: an automata
6358 * @from: the starting point of the transition
6359 * @to: the target point of the transition or NULL
6360 * @counter: the counter associated to that transition
6361 *
William M. Brackddf71d62004-05-06 04:17:26 +00006362 * If @to is NULL, this creates first a new target state in the automata
Daniel Veillardb509f152002-04-17 16:28:10 +00006363 * and then adds an epsilon transition from the @from state to the target state
6364 * which will be allowed only if the counter is within the right range.
6365 *
6366 * Returns the target state or NULL in case of error
6367 */
6368xmlAutomataStatePtr
6369xmlAutomataNewCounterTrans(xmlAutomataPtr am, xmlAutomataStatePtr from,
6370 xmlAutomataStatePtr to, int counter) {
6371 if ((am == NULL) || (from == NULL) || (counter < 0))
6372 return(NULL);
6373 xmlFAGenerateCountedTransition(am, from, to, counter);
6374 if (to == NULL)
6375 return(am->state);
6376 return(to);
6377}
Daniel Veillard4255d502002-04-16 15:50:10 +00006378
6379/**
6380 * xmlAutomataCompile:
6381 * @am: an automata
6382 *
6383 * Compile the automata into a Reg Exp ready for being executed.
6384 * The automata should be free after this point.
6385 *
6386 * Returns the compiled regexp or NULL in case of error
6387 */
Daniel Veillardf8e3db02012-09-11 13:26:36 +08006388xmlRegexpPtr
Daniel Veillard4255d502002-04-16 15:50:10 +00006389xmlAutomataCompile(xmlAutomataPtr am) {
6390 xmlRegexpPtr ret;
6391
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00006392 if ((am == NULL) || (am->error != 0)) return(NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +00006393 xmlFAEliminateEpsilonTransitions(am);
Daniel Veillard23e73572002-09-19 19:56:43 +00006394 /* xmlFAComputesDeterminism(am); */
Daniel Veillard4255d502002-04-16 15:50:10 +00006395 ret = xmlRegEpxFromParse(am);
6396
6397 return(ret);
6398}
Daniel Veillarde19fc232002-04-22 16:01:24 +00006399
6400/**
6401 * xmlAutomataIsDeterminist:
6402 * @am: an automata
6403 *
6404 * Checks if an automata is determinist.
6405 *
6406 * Returns 1 if true, 0 if not, and -1 in case of error
6407 */
Daniel Veillardf8e3db02012-09-11 13:26:36 +08006408int
Daniel Veillarde19fc232002-04-22 16:01:24 +00006409xmlAutomataIsDeterminist(xmlAutomataPtr am) {
6410 int ret;
6411
6412 if (am == NULL)
6413 return(-1);
6414
6415 ret = xmlFAComputesDeterminism(am);
6416 return(ret);
6417}
Daniel Veillard4255d502002-04-16 15:50:10 +00006418#endif /* LIBXML_AUTOMATA_ENABLED */
Daniel Veillard81a8ec62005-08-22 00:20:58 +00006419
6420#ifdef LIBXML_EXPR_ENABLED
6421/************************************************************************
6422 * *
6423 * Formal Expression handling code *
6424 * *
6425 ************************************************************************/
Daniel Veillard81a8ec62005-08-22 00:20:58 +00006426/************************************************************************
6427 * *
6428 * Expression handling context *
6429 * *
6430 ************************************************************************/
6431
6432struct _xmlExpCtxt {
6433 xmlDictPtr dict;
6434 xmlExpNodePtr *table;
6435 int size;
6436 int nbElems;
6437 int nb_nodes;
Daniel Veillard594e5df2009-09-07 14:58:47 +02006438 int maxNodes;
Daniel Veillard81a8ec62005-08-22 00:20:58 +00006439 const char *expr;
6440 const char *cur;
6441 int nb_cons;
Daniel Veillard81a8ec62005-08-22 00:20:58 +00006442 int tabSize;
6443};
6444
6445/**
6446 * xmlExpNewCtxt:
6447 * @maxNodes: the maximum number of nodes
Jan Pokornýbb654fe2016-04-13 16:56:07 +02006448 * @dict: optional dictionary to use internally
Daniel Veillard81a8ec62005-08-22 00:20:58 +00006449 *
6450 * Creates a new context for manipulating expressions
6451 *
6452 * Returns the context or NULL in case of error
6453 */
6454xmlExpCtxtPtr
6455xmlExpNewCtxt(int maxNodes, xmlDictPtr dict) {
6456 xmlExpCtxtPtr ret;
6457 int size = 256;
6458
6459 if (maxNodes <= 4096)
6460 maxNodes = 4096;
Daniel Veillardf8e3db02012-09-11 13:26:36 +08006461
Daniel Veillard81a8ec62005-08-22 00:20:58 +00006462 ret = (xmlExpCtxtPtr) xmlMalloc(sizeof(xmlExpCtxt));
6463 if (ret == NULL)
6464 return(NULL);
6465 memset(ret, 0, sizeof(xmlExpCtxt));
6466 ret->size = size;
6467 ret->nbElems = 0;
Daniel Veillard594e5df2009-09-07 14:58:47 +02006468 ret->maxNodes = maxNodes;
Daniel Veillard81a8ec62005-08-22 00:20:58 +00006469 ret->table = xmlMalloc(size * sizeof(xmlExpNodePtr));
6470 if (ret->table == NULL) {
6471 xmlFree(ret);
6472 return(NULL);
6473 }
6474 memset(ret->table, 0, size * sizeof(xmlExpNodePtr));
6475 if (dict == NULL) {
6476 ret->dict = xmlDictCreate();
6477 if (ret->dict == NULL) {
6478 xmlFree(ret->table);
6479 xmlFree(ret);
6480 return(NULL);
6481 }
6482 } else {
6483 ret->dict = dict;
6484 xmlDictReference(ret->dict);
6485 }
6486 return(ret);
6487}
6488
6489/**
6490 * xmlExpFreeCtxt:
6491 * @ctxt: an expression context
6492 *
6493 * Free an expression context
6494 */
6495void
6496xmlExpFreeCtxt(xmlExpCtxtPtr ctxt) {
6497 if (ctxt == NULL)
6498 return;
6499 xmlDictFree(ctxt->dict);
6500 if (ctxt->table != NULL)
6501 xmlFree(ctxt->table);
6502 xmlFree(ctxt);
6503}
6504
6505/************************************************************************
6506 * *
6507 * Structure associated to an expression node *
6508 * *
6509 ************************************************************************/
Daniel Veillard465a0002005-08-22 12:07:04 +00006510#define MAX_NODES 10000
6511
6512/* #define DEBUG_DERIV */
6513
6514/*
Daniel Veillardf8e3db02012-09-11 13:26:36 +08006515 * TODO:
Daniel Veillard465a0002005-08-22 12:07:04 +00006516 * - Wildcards
6517 * - public API for creation
6518 *
6519 * Started
6520 * - regression testing
6521 *
6522 * Done
6523 * - split into module and test tool
6524 * - memleaks
6525 */
Daniel Veillard81a8ec62005-08-22 00:20:58 +00006526
6527typedef enum {
6528 XML_EXP_NILABLE = (1 << 0)
6529} xmlExpNodeInfo;
6530
6531#define IS_NILLABLE(node) ((node)->info & XML_EXP_NILABLE)
6532
6533struct _xmlExpNode {
6534 unsigned char type;/* xmlExpNodeType */
6535 unsigned char info;/* OR of xmlExpNodeInfo */
6536 unsigned short key; /* the hash key */
6537 unsigned int ref; /* The number of references */
6538 int c_max; /* the maximum length it can consume */
6539 xmlExpNodePtr exp_left;
6540 xmlExpNodePtr next;/* the next node in the hash table or free list */
6541 union {
6542 struct {
6543 int f_min;
6544 int f_max;
6545 } count;
6546 struct {
6547 xmlExpNodePtr f_right;
6548 } children;
6549 const xmlChar *f_str;
6550 } field;
6551};
6552
6553#define exp_min field.count.f_min
6554#define exp_max field.count.f_max
6555/* #define exp_left field.children.f_left */
6556#define exp_right field.children.f_right
6557#define exp_str field.f_str
6558
6559static xmlExpNodePtr xmlExpNewNode(xmlExpCtxtPtr ctxt, xmlExpNodeType type);
6560static xmlExpNode forbiddenExpNode = {
6561 XML_EXP_FORBID, 0, 0, 0, 0, NULL, NULL, {{ 0, 0}}
6562};
6563xmlExpNodePtr forbiddenExp = &forbiddenExpNode;
6564static xmlExpNode emptyExpNode = {
6565 XML_EXP_EMPTY, 1, 0, 0, 0, NULL, NULL, {{ 0, 0}}
6566};
6567xmlExpNodePtr emptyExp = &emptyExpNode;
6568
6569/************************************************************************
6570 * *
6571 * The custom hash table for unicity and canonicalization *
6572 * of sub-expressions pointers *
6573 * *
6574 ************************************************************************/
6575/*
6576 * xmlExpHashNameComputeKey:
6577 * Calculate the hash key for a token
6578 */
6579static unsigned short
6580xmlExpHashNameComputeKey(const xmlChar *name) {
6581 unsigned short value = 0L;
6582 char ch;
Daniel Veillardf8e3db02012-09-11 13:26:36 +08006583
Daniel Veillard81a8ec62005-08-22 00:20:58 +00006584 if (name != NULL) {
6585 value += 30 * (*name);
6586 while ((ch = *name++) != 0) {
6587 value = value ^ ((value << 5) + (value >> 3) + (unsigned long)ch);
6588 }
6589 }
6590 return (value);
6591}
6592
6593/*
6594 * xmlExpHashComputeKey:
6595 * Calculate the hash key for a compound expression
6596 */
6597static unsigned short
6598xmlExpHashComputeKey(xmlExpNodeType type, xmlExpNodePtr left,
6599 xmlExpNodePtr right) {
6600 unsigned long value;
6601 unsigned short ret;
Daniel Veillardf8e3db02012-09-11 13:26:36 +08006602
Daniel Veillard81a8ec62005-08-22 00:20:58 +00006603 switch (type) {
6604 case XML_EXP_SEQ:
6605 value = left->key;
6606 value += right->key;
6607 value *= 3;
6608 ret = (unsigned short) value;
6609 break;
6610 case XML_EXP_OR:
6611 value = left->key;
6612 value += right->key;
6613 value *= 7;
6614 ret = (unsigned short) value;
6615 break;
6616 case XML_EXP_COUNT:
6617 value = left->key;
6618 value += right->key;
6619 ret = (unsigned short) value;
6620 break;
6621 default:
6622 ret = 0;
6623 }
6624 return(ret);
6625}
6626
6627
6628static xmlExpNodePtr
6629xmlExpNewNode(xmlExpCtxtPtr ctxt, xmlExpNodeType type) {
6630 xmlExpNodePtr ret;
6631
6632 if (ctxt->nb_nodes >= MAX_NODES)
6633 return(NULL);
6634 ret = (xmlExpNodePtr) xmlMalloc(sizeof(xmlExpNode));
6635 if (ret == NULL)
6636 return(NULL);
6637 memset(ret, 0, sizeof(xmlExpNode));
6638 ret->type = type;
6639 ret->next = NULL;
6640 ctxt->nb_nodes++;
6641 ctxt->nb_cons++;
6642 return(ret);
6643}
6644
6645/**
6646 * xmlExpHashGetEntry:
6647 * @table: the hash table
6648 *
6649 * Get the unique entry from the hash table. The entry is created if
6650 * needed. @left and @right are consumed, i.e. their ref count will
6651 * be decremented by the operation.
6652 *
6653 * Returns the pointer or NULL in case of error
6654 */
6655static xmlExpNodePtr
6656xmlExpHashGetEntry(xmlExpCtxtPtr ctxt, xmlExpNodeType type,
6657 xmlExpNodePtr left, xmlExpNodePtr right,
6658 const xmlChar *name, int min, int max) {
6659 unsigned short kbase, key;
6660 xmlExpNodePtr entry;
6661 xmlExpNodePtr insert;
6662
6663 if (ctxt == NULL)
6664 return(NULL);
6665
6666 /*
6667 * Check for duplicate and insertion location.
6668 */
6669 if (type == XML_EXP_ATOM) {
6670 kbase = xmlExpHashNameComputeKey(name);
6671 } else if (type == XML_EXP_COUNT) {
6672 /* COUNT reduction rule 1 */
6673 /* a{1} -> a */
6674 if (min == max) {
6675 if (min == 1) {
6676 return(left);
6677 }
6678 if (min == 0) {
6679 xmlExpFree(ctxt, left);
6680 return(emptyExp);
6681 }
6682 }
6683 if (min < 0) {
6684 xmlExpFree(ctxt, left);
6685 return(forbiddenExp);
6686 }
6687 if (max == -1)
6688 kbase = min + 79;
6689 else
6690 kbase = max - min;
6691 kbase += left->key;
6692 } else if (type == XML_EXP_OR) {
6693 /* Forbid reduction rules */
6694 if (left->type == XML_EXP_FORBID) {
6695 xmlExpFree(ctxt, left);
6696 return(right);
6697 }
6698 if (right->type == XML_EXP_FORBID) {
6699 xmlExpFree(ctxt, right);
6700 return(left);
6701 }
6702
6703 /* OR reduction rule 1 */
6704 /* a | a reduced to a */
6705 if (left == right) {
Haibo Huangcfd91dc2020-07-30 23:01:33 -07006706 xmlExpFree(ctxt, right);
Daniel Veillard81a8ec62005-08-22 00:20:58 +00006707 return(left);
6708 }
6709 /* OR canonicalization rule 1 */
6710 /* linearize (a | b) | c into a | (b | c) */
6711 if ((left->type == XML_EXP_OR) && (right->type != XML_EXP_OR)) {
6712 xmlExpNodePtr tmp = left;
6713 left = right;
6714 right = tmp;
6715 }
6716 /* OR reduction rule 2 */
6717 /* a | (a | b) and b | (a | b) are reduced to a | b */
6718 if (right->type == XML_EXP_OR) {
6719 if ((left == right->exp_left) ||
6720 (left == right->exp_right)) {
6721 xmlExpFree(ctxt, left);
6722 return(right);
6723 }
6724 }
6725 /* OR canonicalization rule 2 */
6726 /* linearize (a | b) | c into a | (b | c) */
6727 if (left->type == XML_EXP_OR) {
6728 xmlExpNodePtr tmp;
6729
6730 /* OR canonicalization rule 2 */
6731 if ((left->exp_right->type != XML_EXP_OR) &&
6732 (left->exp_right->key < left->exp_left->key)) {
6733 tmp = left->exp_right;
6734 left->exp_right = left->exp_left;
6735 left->exp_left = tmp;
6736 }
6737 left->exp_right->ref++;
6738 tmp = xmlExpHashGetEntry(ctxt, XML_EXP_OR, left->exp_right, right,
6739 NULL, 0, 0);
6740 left->exp_left->ref++;
6741 tmp = xmlExpHashGetEntry(ctxt, XML_EXP_OR, left->exp_left, tmp,
6742 NULL, 0, 0);
Daniel Veillardf8e3db02012-09-11 13:26:36 +08006743
Daniel Veillard81a8ec62005-08-22 00:20:58 +00006744 xmlExpFree(ctxt, left);
6745 return(tmp);
6746 }
6747 if (right->type == XML_EXP_OR) {
6748 /* Ordering in the tree */
6749 /* C | (A | B) -> A | (B | C) */
6750 if (left->key > right->exp_right->key) {
6751 xmlExpNodePtr tmp;
6752 right->exp_right->ref++;
6753 tmp = xmlExpHashGetEntry(ctxt, XML_EXP_OR, right->exp_right,
6754 left, NULL, 0, 0);
6755 right->exp_left->ref++;
6756 tmp = xmlExpHashGetEntry(ctxt, XML_EXP_OR, right->exp_left,
6757 tmp, NULL, 0, 0);
6758 xmlExpFree(ctxt, right);
6759 return(tmp);
6760 }
6761 /* Ordering in the tree */
6762 /* B | (A | C) -> A | (B | C) */
6763 if (left->key > right->exp_left->key) {
6764 xmlExpNodePtr tmp;
6765 right->exp_right->ref++;
6766 tmp = xmlExpHashGetEntry(ctxt, XML_EXP_OR, left,
6767 right->exp_right, NULL, 0, 0);
6768 right->exp_left->ref++;
6769 tmp = xmlExpHashGetEntry(ctxt, XML_EXP_OR, right->exp_left,
6770 tmp, NULL, 0, 0);
6771 xmlExpFree(ctxt, right);
6772 return(tmp);
6773 }
6774 }
6775 /* we know both types are != XML_EXP_OR here */
6776 else if (left->key > right->key) {
6777 xmlExpNodePtr tmp = left;
6778 left = right;
6779 right = tmp;
6780 }
6781 kbase = xmlExpHashComputeKey(type, left, right);
6782 } else if (type == XML_EXP_SEQ) {
6783 /* Forbid reduction rules */
6784 if (left->type == XML_EXP_FORBID) {
6785 xmlExpFree(ctxt, right);
6786 return(left);
6787 }
6788 if (right->type == XML_EXP_FORBID) {
6789 xmlExpFree(ctxt, left);
6790 return(right);
6791 }
6792 /* Empty reduction rules */
6793 if (right->type == XML_EXP_EMPTY) {
6794 return(left);
6795 }
6796 if (left->type == XML_EXP_EMPTY) {
6797 return(right);
6798 }
6799 kbase = xmlExpHashComputeKey(type, left, right);
Daniel Veillardf8e3db02012-09-11 13:26:36 +08006800 } else
Daniel Veillard81a8ec62005-08-22 00:20:58 +00006801 return(NULL);
6802
6803 key = kbase % ctxt->size;
6804 if (ctxt->table[key] != NULL) {
6805 for (insert = ctxt->table[key]; insert != NULL;
6806 insert = insert->next) {
6807 if ((insert->key == kbase) &&
6808 (insert->type == type)) {
6809 if (type == XML_EXP_ATOM) {
6810 if (name == insert->exp_str) {
6811 insert->ref++;
6812 return(insert);
6813 }
6814 } else if (type == XML_EXP_COUNT) {
6815 if ((insert->exp_min == min) && (insert->exp_max == max) &&
6816 (insert->exp_left == left)) {
6817 insert->ref++;
6818 left->ref--;
6819 return(insert);
6820 }
6821 } else if ((insert->exp_left == left) &&
6822 (insert->exp_right == right)) {
6823 insert->ref++;
6824 left->ref--;
6825 right->ref--;
6826 return(insert);
6827 }
6828 }
6829 }
6830 }
6831
6832 entry = xmlExpNewNode(ctxt, type);
6833 if (entry == NULL)
6834 return(NULL);
6835 entry->key = kbase;
6836 if (type == XML_EXP_ATOM) {
6837 entry->exp_str = name;
6838 entry->c_max = 1;
6839 } else if (type == XML_EXP_COUNT) {
6840 entry->exp_min = min;
6841 entry->exp_max = max;
6842 entry->exp_left = left;
6843 if ((min == 0) || (IS_NILLABLE(left)))
6844 entry->info |= XML_EXP_NILABLE;
6845 if (max < 0)
6846 entry->c_max = -1;
6847 else
6848 entry->c_max = max * entry->exp_left->c_max;
6849 } else {
6850 entry->exp_left = left;
6851 entry->exp_right = right;
6852 if (type == XML_EXP_OR) {
6853 if ((IS_NILLABLE(left)) || (IS_NILLABLE(right)))
6854 entry->info |= XML_EXP_NILABLE;
6855 if ((entry->exp_left->c_max == -1) ||
6856 (entry->exp_right->c_max == -1))
6857 entry->c_max = -1;
6858 else if (entry->exp_left->c_max > entry->exp_right->c_max)
6859 entry->c_max = entry->exp_left->c_max;
6860 else
6861 entry->c_max = entry->exp_right->c_max;
6862 } else {
6863 if ((IS_NILLABLE(left)) && (IS_NILLABLE(right)))
6864 entry->info |= XML_EXP_NILABLE;
6865 if ((entry->exp_left->c_max == -1) ||
6866 (entry->exp_right->c_max == -1))
6867 entry->c_max = -1;
6868 else
6869 entry->c_max = entry->exp_left->c_max + entry->exp_right->c_max;
6870 }
6871 }
6872 entry->ref = 1;
6873 if (ctxt->table[key] != NULL)
6874 entry->next = ctxt->table[key];
6875
6876 ctxt->table[key] = entry;
6877 ctxt->nbElems++;
6878
6879 return(entry);
6880}
6881
6882/**
6883 * xmlExpFree:
6884 * @ctxt: the expression context
6885 * @exp: the expression
6886 *
6887 * Dereference the expression
6888 */
6889void
6890xmlExpFree(xmlExpCtxtPtr ctxt, xmlExpNodePtr exp) {
6891 if ((exp == NULL) || (exp == forbiddenExp) || (exp == emptyExp))
6892 return;
6893 exp->ref--;
Daniel Veillard81a8ec62005-08-22 00:20:58 +00006894 if (exp->ref == 0) {
6895 unsigned short key;
6896
6897 /* Unlink it first from the hash table */
6898 key = exp->key % ctxt->size;
6899 if (ctxt->table[key] == exp) {
6900 ctxt->table[key] = exp->next;
6901 } else {
6902 xmlExpNodePtr tmp;
6903
6904 tmp = ctxt->table[key];
6905 while (tmp != NULL) {
6906 if (tmp->next == exp) {
6907 tmp->next = exp->next;
6908 break;
6909 }
6910 tmp = tmp->next;
6911 }
6912 }
6913
6914 if ((exp->type == XML_EXP_SEQ) || (exp->type == XML_EXP_OR)) {
6915 xmlExpFree(ctxt, exp->exp_left);
6916 xmlExpFree(ctxt, exp->exp_right);
6917 } else if (exp->type == XML_EXP_COUNT) {
6918 xmlExpFree(ctxt, exp->exp_left);
6919 }
6920 xmlFree(exp);
Daniel Veillard81a8ec62005-08-22 00:20:58 +00006921 ctxt->nb_nodes--;
6922 }
6923}
6924
6925/**
6926 * xmlExpRef:
6927 * @exp: the expression
6928 *
6929 * Increase the reference count of the expression
6930 */
6931void
6932xmlExpRef(xmlExpNodePtr exp) {
6933 if (exp != NULL)
6934 exp->ref++;
6935}
6936
Daniel Veillardccb4d412005-08-23 13:41:17 +00006937/**
6938 * xmlExpNewAtom:
6939 * @ctxt: the expression context
6940 * @name: the atom name
Michael Woodfb27e2c2012-09-28 08:59:33 +02006941 * @len: the atom name length in byte (or -1);
Daniel Veillardccb4d412005-08-23 13:41:17 +00006942 *
6943 * Get the atom associated to this name from that context
6944 *
6945 * Returns the node or NULL in case of error
6946 */
6947xmlExpNodePtr
6948xmlExpNewAtom(xmlExpCtxtPtr ctxt, const xmlChar *name, int len) {
6949 if ((ctxt == NULL) || (name == NULL))
6950 return(NULL);
6951 name = xmlDictLookup(ctxt->dict, name, len);
6952 if (name == NULL)
6953 return(NULL);
6954 return(xmlExpHashGetEntry(ctxt, XML_EXP_ATOM, NULL, NULL, name, 0, 0));
6955}
6956
6957/**
6958 * xmlExpNewOr:
6959 * @ctxt: the expression context
6960 * @left: left expression
6961 * @right: right expression
6962 *
6963 * Get the atom associated to the choice @left | @right
6964 * Note that @left and @right are consumed in the operation, to keep
6965 * an handle on them use xmlExpRef() and use xmlExpFree() to release them,
6966 * this is true even in case of failure (unless ctxt == NULL).
6967 *
6968 * Returns the node or NULL in case of error
6969 */
6970xmlExpNodePtr
6971xmlExpNewOr(xmlExpCtxtPtr ctxt, xmlExpNodePtr left, xmlExpNodePtr right) {
Daniel Veillard11ce4002006-03-10 00:36:23 +00006972 if (ctxt == NULL)
6973 return(NULL);
6974 if ((left == NULL) || (right == NULL)) {
Daniel Veillardccb4d412005-08-23 13:41:17 +00006975 xmlExpFree(ctxt, left);
6976 xmlExpFree(ctxt, right);
6977 return(NULL);
6978 }
6979 return(xmlExpHashGetEntry(ctxt, XML_EXP_OR, left, right, NULL, 0, 0));
6980}
6981
6982/**
6983 * xmlExpNewSeq:
6984 * @ctxt: the expression context
6985 * @left: left expression
6986 * @right: right expression
6987 *
6988 * Get the atom associated to the sequence @left , @right
6989 * Note that @left and @right are consumed in the operation, to keep
6990 * an handle on them use xmlExpRef() and use xmlExpFree() to release them,
6991 * this is true even in case of failure (unless ctxt == NULL).
6992 *
6993 * Returns the node or NULL in case of error
6994 */
6995xmlExpNodePtr
6996xmlExpNewSeq(xmlExpCtxtPtr ctxt, xmlExpNodePtr left, xmlExpNodePtr right) {
Daniel Veillard11ce4002006-03-10 00:36:23 +00006997 if (ctxt == NULL)
6998 return(NULL);
6999 if ((left == NULL) || (right == NULL)) {
Daniel Veillardccb4d412005-08-23 13:41:17 +00007000 xmlExpFree(ctxt, left);
7001 xmlExpFree(ctxt, right);
7002 return(NULL);
7003 }
7004 return(xmlExpHashGetEntry(ctxt, XML_EXP_SEQ, left, right, NULL, 0, 0));
7005}
7006
7007/**
7008 * xmlExpNewRange:
7009 * @ctxt: the expression context
7010 * @subset: the expression to be repeated
7011 * @min: the lower bound for the repetition
7012 * @max: the upper bound for the repetition, -1 means infinite
7013 *
7014 * Get the atom associated to the range (@subset){@min, @max}
7015 * Note that @subset is consumed in the operation, to keep
7016 * an handle on it use xmlExpRef() and use xmlExpFree() to release it,
7017 * this is true even in case of failure (unless ctxt == NULL).
7018 *
7019 * Returns the node or NULL in case of error
7020 */
7021xmlExpNodePtr
7022xmlExpNewRange(xmlExpCtxtPtr ctxt, xmlExpNodePtr subset, int min, int max) {
Daniel Veillard11ce4002006-03-10 00:36:23 +00007023 if (ctxt == NULL)
7024 return(NULL);
7025 if ((subset == NULL) || (min < 0) || (max < -1) ||
Daniel Veillardccb4d412005-08-23 13:41:17 +00007026 ((max >= 0) && (min > max))) {
7027 xmlExpFree(ctxt, subset);
7028 return(NULL);
7029 }
7030 return(xmlExpHashGetEntry(ctxt, XML_EXP_COUNT, subset,
7031 NULL, NULL, min, max));
7032}
7033
Daniel Veillard81a8ec62005-08-22 00:20:58 +00007034/************************************************************************
7035 * *
7036 * Public API for operations on expressions *
7037 * *
7038 ************************************************************************/
7039
7040static int
Daniel Veillardf8e3db02012-09-11 13:26:36 +08007041xmlExpGetLanguageInt(xmlExpCtxtPtr ctxt, xmlExpNodePtr exp,
Daniel Veillard81a8ec62005-08-22 00:20:58 +00007042 const xmlChar**list, int len, int nb) {
7043 int tmp, tmp2;
7044tail:
7045 switch (exp->type) {
7046 case XML_EXP_EMPTY:
7047 return(0);
7048 case XML_EXP_ATOM:
7049 for (tmp = 0;tmp < nb;tmp++)
7050 if (list[tmp] == exp->exp_str)
7051 return(0);
7052 if (nb >= len)
7053 return(-2);
Daniel Veillard13cee4e2009-09-05 14:52:55 +02007054 list[nb] = exp->exp_str;
Daniel Veillard81a8ec62005-08-22 00:20:58 +00007055 return(1);
7056 case XML_EXP_COUNT:
7057 exp = exp->exp_left;
7058 goto tail;
7059 case XML_EXP_SEQ:
7060 case XML_EXP_OR:
7061 tmp = xmlExpGetLanguageInt(ctxt, exp->exp_left, list, len, nb);
7062 if (tmp < 0)
7063 return(tmp);
7064 tmp2 = xmlExpGetLanguageInt(ctxt, exp->exp_right, list, len,
7065 nb + tmp);
7066 if (tmp2 < 0)
7067 return(tmp2);
7068 return(tmp + tmp2);
7069 }
7070 return(-1);
7071}
7072
7073/**
7074 * xmlExpGetLanguage:
7075 * @ctxt: the expression context
7076 * @exp: the expression
Daniel Veillard7802ba52005-10-27 11:56:20 +00007077 * @langList: where to store the tokens
Michael Woodfb27e2c2012-09-28 08:59:33 +02007078 * @len: the allocated length of @list
Daniel Veillard81a8ec62005-08-22 00:20:58 +00007079 *
7080 * Find all the strings used in @exp and store them in @list
7081 *
7082 * Returns the number of unique strings found, -1 in case of errors and
7083 * -2 if there is more than @len strings
7084 */
7085int
Daniel Veillardf8e3db02012-09-11 13:26:36 +08007086xmlExpGetLanguage(xmlExpCtxtPtr ctxt, xmlExpNodePtr exp,
Daniel Veillard7802ba52005-10-27 11:56:20 +00007087 const xmlChar**langList, int len) {
7088 if ((ctxt == NULL) || (exp == NULL) || (langList == NULL) || (len <= 0))
Daniel Veillard81a8ec62005-08-22 00:20:58 +00007089 return(-1);
Daniel Veillard7802ba52005-10-27 11:56:20 +00007090 return(xmlExpGetLanguageInt(ctxt, exp, langList, len, 0));
Daniel Veillard81a8ec62005-08-22 00:20:58 +00007091}
7092
7093static int
Daniel Veillardf8e3db02012-09-11 13:26:36 +08007094xmlExpGetStartInt(xmlExpCtxtPtr ctxt, xmlExpNodePtr exp,
Daniel Veillard81a8ec62005-08-22 00:20:58 +00007095 const xmlChar**list, int len, int nb) {
7096 int tmp, tmp2;
7097tail:
7098 switch (exp->type) {
7099 case XML_EXP_FORBID:
7100 return(0);
7101 case XML_EXP_EMPTY:
7102 return(0);
7103 case XML_EXP_ATOM:
7104 for (tmp = 0;tmp < nb;tmp++)
7105 if (list[tmp] == exp->exp_str)
7106 return(0);
7107 if (nb >= len)
7108 return(-2);
Daniel Veillard13cee4e2009-09-05 14:52:55 +02007109 list[nb] = exp->exp_str;
Daniel Veillard81a8ec62005-08-22 00:20:58 +00007110 return(1);
7111 case XML_EXP_COUNT:
7112 exp = exp->exp_left;
7113 goto tail;
7114 case XML_EXP_SEQ:
7115 tmp = xmlExpGetStartInt(ctxt, exp->exp_left, list, len, nb);
7116 if (tmp < 0)
7117 return(tmp);
7118 if (IS_NILLABLE(exp->exp_left)) {
7119 tmp2 = xmlExpGetStartInt(ctxt, exp->exp_right, list, len,
7120 nb + tmp);
7121 if (tmp2 < 0)
7122 return(tmp2);
7123 tmp += tmp2;
7124 }
7125 return(tmp);
7126 case XML_EXP_OR:
7127 tmp = xmlExpGetStartInt(ctxt, exp->exp_left, list, len, nb);
7128 if (tmp < 0)
7129 return(tmp);
7130 tmp2 = xmlExpGetStartInt(ctxt, exp->exp_right, list, len,
7131 nb + tmp);
7132 if (tmp2 < 0)
7133 return(tmp2);
7134 return(tmp + tmp2);
7135 }
7136 return(-1);
7137}
7138
7139/**
7140 * xmlExpGetStart:
7141 * @ctxt: the expression context
7142 * @exp: the expression
Daniel Veillard7802ba52005-10-27 11:56:20 +00007143 * @tokList: where to store the tokens
Michael Woodfb27e2c2012-09-28 08:59:33 +02007144 * @len: the allocated length of @list
Daniel Veillard81a8ec62005-08-22 00:20:58 +00007145 *
7146 * Find all the strings that appears at the start of the languages
7147 * accepted by @exp and store them in @list. E.g. for (a, b) | c
7148 * it will return the list [a, c]
7149 *
7150 * Returns the number of unique strings found, -1 in case of errors and
7151 * -2 if there is more than @len strings
7152 */
7153int
Daniel Veillardf8e3db02012-09-11 13:26:36 +08007154xmlExpGetStart(xmlExpCtxtPtr ctxt, xmlExpNodePtr exp,
Daniel Veillard7802ba52005-10-27 11:56:20 +00007155 const xmlChar**tokList, int len) {
7156 if ((ctxt == NULL) || (exp == NULL) || (tokList == NULL) || (len <= 0))
Daniel Veillard81a8ec62005-08-22 00:20:58 +00007157 return(-1);
Daniel Veillard7802ba52005-10-27 11:56:20 +00007158 return(xmlExpGetStartInt(ctxt, exp, tokList, len, 0));
Daniel Veillard81a8ec62005-08-22 00:20:58 +00007159}
7160
7161/**
7162 * xmlExpIsNillable:
7163 * @exp: the expression
7164 *
Haibo Huangcfd91dc2020-07-30 23:01:33 -07007165 * Finds if the expression is nillable, i.e. if it accepts the empty sequence
Daniel Veillard81a8ec62005-08-22 00:20:58 +00007166 *
7167 * Returns 1 if nillable, 0 if not and -1 in case of error
7168 */
7169int
7170xmlExpIsNillable(xmlExpNodePtr exp) {
7171 if (exp == NULL)
7172 return(-1);
7173 return(IS_NILLABLE(exp) != 0);
7174}
7175
7176static xmlExpNodePtr
7177xmlExpStringDeriveInt(xmlExpCtxtPtr ctxt, xmlExpNodePtr exp, const xmlChar *str)
7178{
7179 xmlExpNodePtr ret;
7180
7181 switch (exp->type) {
7182 case XML_EXP_EMPTY:
7183 return(forbiddenExp);
7184 case XML_EXP_FORBID:
7185 return(forbiddenExp);
7186 case XML_EXP_ATOM:
7187 if (exp->exp_str == str) {
7188#ifdef DEBUG_DERIV
7189 printf("deriv atom: equal => Empty\n");
7190#endif
7191 ret = emptyExp;
7192 } else {
7193#ifdef DEBUG_DERIV
7194 printf("deriv atom: mismatch => forbid\n");
7195#endif
7196 /* TODO wildcards here */
7197 ret = forbiddenExp;
7198 }
7199 return(ret);
7200 case XML_EXP_OR: {
7201 xmlExpNodePtr tmp;
7202
7203#ifdef DEBUG_DERIV
7204 printf("deriv or: => or(derivs)\n");
7205#endif
7206 tmp = xmlExpStringDeriveInt(ctxt, exp->exp_left, str);
7207 if (tmp == NULL) {
7208 return(NULL);
7209 }
7210 ret = xmlExpStringDeriveInt(ctxt, exp->exp_right, str);
7211 if (ret == NULL) {
7212 xmlExpFree(ctxt, tmp);
7213 return(NULL);
7214 }
7215 ret = xmlExpHashGetEntry(ctxt, XML_EXP_OR, tmp, ret,
7216 NULL, 0, 0);
7217 return(ret);
7218 }
7219 case XML_EXP_SEQ:
7220#ifdef DEBUG_DERIV
7221 printf("deriv seq: starting with left\n");
7222#endif
7223 ret = xmlExpStringDeriveInt(ctxt, exp->exp_left, str);
7224 if (ret == NULL) {
7225 return(NULL);
7226 } else if (ret == forbiddenExp) {
7227 if (IS_NILLABLE(exp->exp_left)) {
7228#ifdef DEBUG_DERIV
7229 printf("deriv seq: left failed but nillable\n");
7230#endif
7231 ret = xmlExpStringDeriveInt(ctxt, exp->exp_right, str);
7232 }
7233 } else {
7234#ifdef DEBUG_DERIV
7235 printf("deriv seq: left match => sequence\n");
7236#endif
7237 exp->exp_right->ref++;
7238 ret = xmlExpHashGetEntry(ctxt, XML_EXP_SEQ, ret, exp->exp_right,
7239 NULL, 0, 0);
7240 }
7241 return(ret);
7242 case XML_EXP_COUNT: {
7243 int min, max;
7244 xmlExpNodePtr tmp;
7245
7246 if (exp->exp_max == 0)
7247 return(forbiddenExp);
7248 ret = xmlExpStringDeriveInt(ctxt, exp->exp_left, str);
7249 if (ret == NULL)
7250 return(NULL);
7251 if (ret == forbiddenExp) {
7252#ifdef DEBUG_DERIV
7253 printf("deriv count: pattern mismatch => forbid\n");
7254#endif
7255 return(ret);
7256 }
7257 if (exp->exp_max == 1)
7258 return(ret);
7259 if (exp->exp_max < 0) /* unbounded */
7260 max = -1;
7261 else
7262 max = exp->exp_max - 1;
7263 if (exp->exp_min > 0)
7264 min = exp->exp_min - 1;
7265 else
7266 min = 0;
7267 exp->exp_left->ref++;
7268 tmp = xmlExpHashGetEntry(ctxt, XML_EXP_COUNT, exp->exp_left, NULL,
7269 NULL, min, max);
7270 if (ret == emptyExp) {
7271#ifdef DEBUG_DERIV
7272 printf("deriv count: match to empty => new count\n");
7273#endif
7274 return(tmp);
7275 }
7276#ifdef DEBUG_DERIV
7277 printf("deriv count: match => sequence with new count\n");
7278#endif
7279 return(xmlExpHashGetEntry(ctxt, XML_EXP_SEQ, ret, tmp,
7280 NULL, 0, 0));
7281 }
7282 }
7283 return(NULL);
7284}
7285
7286/**
7287 * xmlExpStringDerive:
7288 * @ctxt: the expression context
7289 * @exp: the expression
7290 * @str: the string
7291 * @len: the string len in bytes if available
7292 *
7293 * Do one step of Brzozowski derivation of the expression @exp with
7294 * respect to the input string
7295 *
7296 * Returns the resulting expression or NULL in case of internal error
7297 */
7298xmlExpNodePtr
7299xmlExpStringDerive(xmlExpCtxtPtr ctxt, xmlExpNodePtr exp,
7300 const xmlChar *str, int len) {
7301 const xmlChar *input;
7302
7303 if ((exp == NULL) || (ctxt == NULL) || (str == NULL)) {
7304 return(NULL);
7305 }
7306 /*
Jan Pokornýbb654fe2016-04-13 16:56:07 +02007307 * check the string is in the dictionary, if yes use an interned
Daniel Veillard81a8ec62005-08-22 00:20:58 +00007308 * copy, otherwise we know it's not an acceptable input
7309 */
7310 input = xmlDictExists(ctxt->dict, str, len);
7311 if (input == NULL) {
7312 return(forbiddenExp);
7313 }
7314 return(xmlExpStringDeriveInt(ctxt, exp, input));
7315}
7316
7317static int
7318xmlExpCheckCard(xmlExpNodePtr exp, xmlExpNodePtr sub) {
7319 int ret = 1;
7320
7321 if (sub->c_max == -1) {
7322 if (exp->c_max != -1)
7323 ret = 0;
7324 } else if ((exp->c_max >= 0) && (exp->c_max < sub->c_max)) {
7325 ret = 0;
7326 }
7327#if 0
7328 if ((IS_NILLABLE(sub)) && (!IS_NILLABLE(exp)))
7329 ret = 0;
7330#endif
7331 return(ret);
7332}
7333
7334static xmlExpNodePtr xmlExpExpDeriveInt(xmlExpCtxtPtr ctxt, xmlExpNodePtr exp,
7335 xmlExpNodePtr sub);
7336/**
7337 * xmlExpDivide:
7338 * @ctxt: the expressions context
7339 * @exp: the englobing expression
7340 * @sub: the subexpression
7341 * @mult: the multiple expression
7342 * @remain: the remain from the derivation of the multiple
7343 *
7344 * Check if exp is a multiple of sub, i.e. if there is a finite number n
7345 * so that sub{n} subsume exp
7346 *
7347 * Returns the multiple value if successful, 0 if it is not a multiple
Haibo Huangcfd91dc2020-07-30 23:01:33 -07007348 * and -1 in case of internal error.
Daniel Veillard81a8ec62005-08-22 00:20:58 +00007349 */
7350
7351static int
7352xmlExpDivide(xmlExpCtxtPtr ctxt, xmlExpNodePtr exp, xmlExpNodePtr sub,
7353 xmlExpNodePtr *mult, xmlExpNodePtr *remain) {
7354 int i;
7355 xmlExpNodePtr tmp, tmp2;
7356
7357 if (mult != NULL) *mult = NULL;
7358 if (remain != NULL) *remain = NULL;
7359 if (exp->c_max == -1) return(0);
7360 if (IS_NILLABLE(exp) && (!IS_NILLABLE(sub))) return(0);
7361
7362 for (i = 1;i <= exp->c_max;i++) {
7363 sub->ref++;
7364 tmp = xmlExpHashGetEntry(ctxt, XML_EXP_COUNT,
7365 sub, NULL, NULL, i, i);
7366 if (tmp == NULL) {
7367 return(-1);
7368 }
7369 if (!xmlExpCheckCard(tmp, exp)) {
7370 xmlExpFree(ctxt, tmp);
7371 continue;
7372 }
7373 tmp2 = xmlExpExpDeriveInt(ctxt, tmp, exp);
7374 if (tmp2 == NULL) {
7375 xmlExpFree(ctxt, tmp);
7376 return(-1);
7377 }
7378 if ((tmp2 != forbiddenExp) && (IS_NILLABLE(tmp2))) {
7379 if (remain != NULL)
7380 *remain = tmp2;
7381 else
7382 xmlExpFree(ctxt, tmp2);
7383 if (mult != NULL)
7384 *mult = tmp;
7385 else
7386 xmlExpFree(ctxt, tmp);
7387#ifdef DEBUG_DERIV
7388 printf("Divide succeeded %d\n", i);
7389#endif
7390 return(i);
7391 }
7392 xmlExpFree(ctxt, tmp);
7393 xmlExpFree(ctxt, tmp2);
7394 }
7395#ifdef DEBUG_DERIV
7396 printf("Divide failed\n");
7397#endif
7398 return(0);
7399}
7400
7401/**
7402 * xmlExpExpDeriveInt:
7403 * @ctxt: the expressions context
7404 * @exp: the englobing expression
7405 * @sub: the subexpression
7406 *
7407 * Try to do a step of Brzozowski derivation but at a higher level
7408 * the input being a subexpression.
7409 *
7410 * Returns the resulting expression or NULL in case of internal error
7411 */
7412static xmlExpNodePtr
7413xmlExpExpDeriveInt(xmlExpCtxtPtr ctxt, xmlExpNodePtr exp, xmlExpNodePtr sub) {
7414 xmlExpNodePtr ret, tmp, tmp2, tmp3;
7415 const xmlChar **tab;
7416 int len, i;
7417
7418 /*
7419 * In case of equality and if the expression can only consume a finite
7420 * amount, then the derivation is empty
7421 */
7422 if ((exp == sub) && (exp->c_max >= 0)) {
7423#ifdef DEBUG_DERIV
7424 printf("Equal(exp, sub) and finite -> Empty\n");
7425#endif
7426 return(emptyExp);
7427 }
7428 /*
7429 * decompose sub sequence first
7430 */
7431 if (sub->type == XML_EXP_EMPTY) {
7432#ifdef DEBUG_DERIV
7433 printf("Empty(sub) -> Empty\n");
7434#endif
7435 exp->ref++;
7436 return(exp);
7437 }
7438 if (sub->type == XML_EXP_SEQ) {
7439#ifdef DEBUG_DERIV
7440 printf("Seq(sub) -> decompose\n");
7441#endif
7442 tmp = xmlExpExpDeriveInt(ctxt, exp, sub->exp_left);
7443 if (tmp == NULL)
7444 return(NULL);
7445 if (tmp == forbiddenExp)
7446 return(tmp);
7447 ret = xmlExpExpDeriveInt(ctxt, tmp, sub->exp_right);
7448 xmlExpFree(ctxt, tmp);
7449 return(ret);
7450 }
7451 if (sub->type == XML_EXP_OR) {
7452#ifdef DEBUG_DERIV
7453 printf("Or(sub) -> decompose\n");
7454#endif
7455 tmp = xmlExpExpDeriveInt(ctxt, exp, sub->exp_left);
7456 if (tmp == forbiddenExp)
7457 return(tmp);
7458 if (tmp == NULL)
7459 return(NULL);
7460 ret = xmlExpExpDeriveInt(ctxt, exp, sub->exp_right);
7461 if ((ret == NULL) || (ret == forbiddenExp)) {
7462 xmlExpFree(ctxt, tmp);
7463 return(ret);
7464 }
7465 return(xmlExpHashGetEntry(ctxt, XML_EXP_OR, tmp, ret, NULL, 0, 0));
7466 }
7467 if (!xmlExpCheckCard(exp, sub)) {
7468#ifdef DEBUG_DERIV
7469 printf("CheckCard(exp, sub) failed -> Forbid\n");
7470#endif
7471 return(forbiddenExp);
7472 }
7473 switch (exp->type) {
7474 case XML_EXP_EMPTY:
7475 if (sub == emptyExp)
7476 return(emptyExp);
7477#ifdef DEBUG_DERIV
7478 printf("Empty(exp) -> Forbid\n");
7479#endif
7480 return(forbiddenExp);
7481 case XML_EXP_FORBID:
7482#ifdef DEBUG_DERIV
7483 printf("Forbid(exp) -> Forbid\n");
7484#endif
7485 return(forbiddenExp);
7486 case XML_EXP_ATOM:
7487 if (sub->type == XML_EXP_ATOM) {
7488 /* TODO: handle wildcards */
7489 if (exp->exp_str == sub->exp_str) {
7490#ifdef DEBUG_DERIV
7491 printf("Atom match -> Empty\n");
7492#endif
7493 return(emptyExp);
7494 }
7495#ifdef DEBUG_DERIV
7496 printf("Atom mismatch -> Forbid\n");
7497#endif
7498 return(forbiddenExp);
7499 }
7500 if ((sub->type == XML_EXP_COUNT) &&
7501 (sub->exp_max == 1) &&
7502 (sub->exp_left->type == XML_EXP_ATOM)) {
7503 /* TODO: handle wildcards */
7504 if (exp->exp_str == sub->exp_left->exp_str) {
7505#ifdef DEBUG_DERIV
7506 printf("Atom match -> Empty\n");
7507#endif
7508 return(emptyExp);
7509 }
7510#ifdef DEBUG_DERIV
7511 printf("Atom mismatch -> Forbid\n");
7512#endif
7513 return(forbiddenExp);
7514 }
7515#ifdef DEBUG_DERIV
Haibo Huangcfd91dc2020-07-30 23:01:33 -07007516 printf("Complex exp vs Atom -> Forbid\n");
Daniel Veillard81a8ec62005-08-22 00:20:58 +00007517#endif
7518 return(forbiddenExp);
7519 case XML_EXP_SEQ:
7520 /* try to get the sequence consumed only if possible */
7521 if (xmlExpCheckCard(exp->exp_left, sub)) {
7522 /* See if the sequence can be consumed directly */
7523#ifdef DEBUG_DERIV
7524 printf("Seq trying left only\n");
7525#endif
7526 ret = xmlExpExpDeriveInt(ctxt, exp->exp_left, sub);
7527 if ((ret != forbiddenExp) && (ret != NULL)) {
7528#ifdef DEBUG_DERIV
7529 printf("Seq trying left only worked\n");
7530#endif
7531 /*
7532 * TODO: assumption here that we are determinist
7533 * i.e. we won't get to a nillable exp left
7534 * subset which could be matched by the right
7535 * part too.
7536 * e.g.: (a | b)+,(a | c) and 'a+,a'
7537 */
7538 exp->exp_right->ref++;
7539 return(xmlExpHashGetEntry(ctxt, XML_EXP_SEQ, ret,
7540 exp->exp_right, NULL, 0, 0));
7541 }
7542#ifdef DEBUG_DERIV
7543 } else {
7544 printf("Seq: left too short\n");
7545#endif
7546 }
7547 /* Try instead to decompose */
7548 if (sub->type == XML_EXP_COUNT) {
7549 int min, max;
7550
7551#ifdef DEBUG_DERIV
7552 printf("Seq: sub is a count\n");
7553#endif
7554 ret = xmlExpExpDeriveInt(ctxt, exp->exp_left, sub->exp_left);
7555 if (ret == NULL)
7556 return(NULL);
7557 if (ret != forbiddenExp) {
7558#ifdef DEBUG_DERIV
7559 printf("Seq , Count match on left\n");
7560#endif
7561 if (sub->exp_max < 0)
7562 max = -1;
7563 else
7564 max = sub->exp_max -1;
7565 if (sub->exp_min > 0)
7566 min = sub->exp_min -1;
7567 else
7568 min = 0;
7569 exp->exp_right->ref++;
7570 tmp = xmlExpHashGetEntry(ctxt, XML_EXP_SEQ, ret,
7571 exp->exp_right, NULL, 0, 0);
7572 if (tmp == NULL)
7573 return(NULL);
7574
7575 sub->exp_left->ref++;
7576 tmp2 = xmlExpHashGetEntry(ctxt, XML_EXP_COUNT,
7577 sub->exp_left, NULL, NULL, min, max);
7578 if (tmp2 == NULL) {
7579 xmlExpFree(ctxt, tmp);
7580 return(NULL);
7581 }
7582 ret = xmlExpExpDeriveInt(ctxt, tmp, tmp2);
7583 xmlExpFree(ctxt, tmp);
7584 xmlExpFree(ctxt, tmp2);
7585 return(ret);
7586 }
7587 }
7588 /* we made no progress on structured operations */
7589 break;
7590 case XML_EXP_OR:
7591#ifdef DEBUG_DERIV
7592 printf("Or , trying both side\n");
7593#endif
7594 ret = xmlExpExpDeriveInt(ctxt, exp->exp_left, sub);
7595 if (ret == NULL)
7596 return(NULL);
7597 tmp = xmlExpExpDeriveInt(ctxt, exp->exp_right, sub);
7598 if (tmp == NULL) {
7599 xmlExpFree(ctxt, ret);
7600 return(NULL);
7601 }
7602 return(xmlExpHashGetEntry(ctxt, XML_EXP_OR, ret, tmp, NULL, 0, 0));
7603 case XML_EXP_COUNT: {
7604 int min, max;
7605
7606 if (sub->type == XML_EXP_COUNT) {
7607 /*
7608 * Try to see if the loop is completely subsumed
7609 */
7610 tmp = xmlExpExpDeriveInt(ctxt, exp->exp_left, sub->exp_left);
7611 if (tmp == NULL)
7612 return(NULL);
7613 if (tmp == forbiddenExp) {
7614 int mult;
7615
7616#ifdef DEBUG_DERIV
7617 printf("Count, Count inner don't subsume\n");
7618#endif
7619 mult = xmlExpDivide(ctxt, sub->exp_left, exp->exp_left,
7620 NULL, &tmp);
7621 if (mult <= 0) {
7622#ifdef DEBUG_DERIV
7623 printf("Count, Count not multiple => forbidden\n");
7624#endif
7625 return(forbiddenExp);
7626 }
7627 if (sub->exp_max == -1) {
7628 max = -1;
7629 if (exp->exp_max == -1) {
7630 if (exp->exp_min <= sub->exp_min * mult)
7631 min = 0;
7632 else
7633 min = exp->exp_min - sub->exp_min * mult;
7634 } else {
7635#ifdef DEBUG_DERIV
7636 printf("Count, Count finite can't subsume infinite\n");
7637#endif
7638 xmlExpFree(ctxt, tmp);
7639 return(forbiddenExp);
7640 }
7641 } else {
7642 if (exp->exp_max == -1) {
7643#ifdef DEBUG_DERIV
7644 printf("Infinite loop consume mult finite loop\n");
7645#endif
7646 if (exp->exp_min > sub->exp_min * mult) {
7647 max = -1;
7648 min = exp->exp_min - sub->exp_min * mult;
7649 } else {
7650 max = -1;
7651 min = 0;
7652 }
7653 } else {
7654 if (exp->exp_max < sub->exp_max * mult) {
7655#ifdef DEBUG_DERIV
7656 printf("loops max mult mismatch => forbidden\n");
7657#endif
7658 xmlExpFree(ctxt, tmp);
7659 return(forbiddenExp);
7660 }
7661 if (sub->exp_max * mult > exp->exp_min)
7662 min = 0;
7663 else
7664 min = exp->exp_min - sub->exp_max * mult;
7665 max = exp->exp_max - sub->exp_max * mult;
7666 }
7667 }
7668 } else if (!IS_NILLABLE(tmp)) {
7669 /*
7670 * TODO: loop here to try to grow if working on finite
7671 * blocks.
7672 */
7673#ifdef DEBUG_DERIV
7674 printf("Count, Count remain not nillable => forbidden\n");
7675#endif
7676 xmlExpFree(ctxt, tmp);
7677 return(forbiddenExp);
7678 } else if (sub->exp_max == -1) {
7679 if (exp->exp_max == -1) {
7680 if (exp->exp_min <= sub->exp_min) {
7681#ifdef DEBUG_DERIV
7682 printf("Infinite loops Okay => COUNT(0,Inf)\n");
7683#endif
7684 max = -1;
7685 min = 0;
7686 } else {
7687#ifdef DEBUG_DERIV
7688 printf("Infinite loops min => Count(X,Inf)\n");
7689#endif
7690 max = -1;
7691 min = exp->exp_min - sub->exp_min;
7692 }
7693 } else if (exp->exp_min > sub->exp_min) {
7694#ifdef DEBUG_DERIV
7695 printf("loops min mismatch 1 => forbidden ???\n");
7696#endif
7697 xmlExpFree(ctxt, tmp);
7698 return(forbiddenExp);
7699 } else {
7700 max = -1;
7701 min = 0;
7702 }
7703 } else {
7704 if (exp->exp_max == -1) {
7705#ifdef DEBUG_DERIV
7706 printf("Infinite loop consume finite loop\n");
7707#endif
7708 if (exp->exp_min > sub->exp_min) {
7709 max = -1;
7710 min = exp->exp_min - sub->exp_min;
7711 } else {
7712 max = -1;
7713 min = 0;
7714 }
7715 } else {
7716 if (exp->exp_max < sub->exp_max) {
7717#ifdef DEBUG_DERIV
7718 printf("loops max mismatch => forbidden\n");
7719#endif
7720 xmlExpFree(ctxt, tmp);
7721 return(forbiddenExp);
7722 }
7723 if (sub->exp_max > exp->exp_min)
7724 min = 0;
7725 else
7726 min = exp->exp_min - sub->exp_max;
7727 max = exp->exp_max - sub->exp_max;
7728 }
7729 }
7730#ifdef DEBUG_DERIV
7731 printf("loops match => SEQ(COUNT())\n");
7732#endif
7733 exp->exp_left->ref++;
7734 tmp2 = xmlExpHashGetEntry(ctxt, XML_EXP_COUNT, exp->exp_left,
7735 NULL, NULL, min, max);
7736 if (tmp2 == NULL) {
7737 return(NULL);
7738 }
7739 ret = xmlExpHashGetEntry(ctxt, XML_EXP_SEQ, tmp, tmp2,
7740 NULL, 0, 0);
7741 return(ret);
7742 }
7743 tmp = xmlExpExpDeriveInt(ctxt, exp->exp_left, sub);
7744 if (tmp == NULL)
7745 return(NULL);
7746 if (tmp == forbiddenExp) {
7747#ifdef DEBUG_DERIV
7748 printf("loop mismatch => forbidden\n");
7749#endif
7750 return(forbiddenExp);
7751 }
7752 if (exp->exp_min > 0)
7753 min = exp->exp_min - 1;
7754 else
7755 min = 0;
7756 if (exp->exp_max < 0)
7757 max = -1;
7758 else
7759 max = exp->exp_max - 1;
7760
7761#ifdef DEBUG_DERIV
7762 printf("loop match => SEQ(COUNT())\n");
7763#endif
7764 exp->exp_left->ref++;
7765 tmp2 = xmlExpHashGetEntry(ctxt, XML_EXP_COUNT, exp->exp_left,
7766 NULL, NULL, min, max);
7767 if (tmp2 == NULL)
7768 return(NULL);
7769 ret = xmlExpHashGetEntry(ctxt, XML_EXP_SEQ, tmp, tmp2,
7770 NULL, 0, 0);
7771 return(ret);
7772 }
7773 }
7774
Daniel Veillardccb4d412005-08-23 13:41:17 +00007775#ifdef DEBUG_DERIV
7776 printf("Fallback to derivative\n");
7777#endif
7778 if (IS_NILLABLE(sub)) {
7779 if (!(IS_NILLABLE(exp)))
7780 return(forbiddenExp);
7781 else
7782 ret = emptyExp;
7783 } else
7784 ret = NULL;
Daniel Veillard81a8ec62005-08-22 00:20:58 +00007785 /*
7786 * here the structured derivation made no progress so
7787 * we use the default token based derivation to force one more step
7788 */
7789 if (ctxt->tabSize == 0)
7790 ctxt->tabSize = 40;
7791
7792 tab = (const xmlChar **) xmlMalloc(ctxt->tabSize *
7793 sizeof(const xmlChar *));
7794 if (tab == NULL) {
7795 return(NULL);
7796 }
7797
Daniel Veillard81a8ec62005-08-22 00:20:58 +00007798 /*
7799 * collect all the strings accepted by the subexpression on input
7800 */
7801 len = xmlExpGetStartInt(ctxt, sub, tab, ctxt->tabSize, 0);
7802 while (len < 0) {
7803 const xmlChar **temp;
Rob Richards54a8f672005-10-07 02:33:00 +00007804 temp = (const xmlChar **) xmlRealloc((xmlChar **) tab, ctxt->tabSize * 2 *
Daniel Veillard81a8ec62005-08-22 00:20:58 +00007805 sizeof(const xmlChar *));
7806 if (temp == NULL) {
Rob Richards54a8f672005-10-07 02:33:00 +00007807 xmlFree((xmlChar **) tab);
Daniel Veillard81a8ec62005-08-22 00:20:58 +00007808 return(NULL);
7809 }
7810 tab = temp;
7811 ctxt->tabSize *= 2;
7812 len = xmlExpGetStartInt(ctxt, sub, tab, ctxt->tabSize, 0);
7813 }
Daniel Veillard81a8ec62005-08-22 00:20:58 +00007814 for (i = 0;i < len;i++) {
7815 tmp = xmlExpStringDeriveInt(ctxt, exp, tab[i]);
7816 if ((tmp == NULL) || (tmp == forbiddenExp)) {
7817 xmlExpFree(ctxt, ret);
Rob Richards54a8f672005-10-07 02:33:00 +00007818 xmlFree((xmlChar **) tab);
Daniel Veillard81a8ec62005-08-22 00:20:58 +00007819 return(tmp);
7820 }
7821 tmp2 = xmlExpStringDeriveInt(ctxt, sub, tab[i]);
7822 if ((tmp2 == NULL) || (tmp2 == forbiddenExp)) {
7823 xmlExpFree(ctxt, tmp);
7824 xmlExpFree(ctxt, ret);
Rob Richards54a8f672005-10-07 02:33:00 +00007825 xmlFree((xmlChar **) tab);
Daniel Veillard81a8ec62005-08-22 00:20:58 +00007826 return(tmp);
7827 }
7828 tmp3 = xmlExpExpDeriveInt(ctxt, tmp, tmp2);
7829 xmlExpFree(ctxt, tmp);
7830 xmlExpFree(ctxt, tmp2);
7831
7832 if ((tmp3 == NULL) || (tmp3 == forbiddenExp)) {
7833 xmlExpFree(ctxt, ret);
Rob Richards54a8f672005-10-07 02:33:00 +00007834 xmlFree((xmlChar **) tab);
Daniel Veillard81a8ec62005-08-22 00:20:58 +00007835 return(tmp3);
7836 }
7837
7838 if (ret == NULL)
7839 ret = tmp3;
7840 else {
7841 ret = xmlExpHashGetEntry(ctxt, XML_EXP_OR, ret, tmp3, NULL, 0, 0);
7842 if (ret == NULL) {
Rob Richards54a8f672005-10-07 02:33:00 +00007843 xmlFree((xmlChar **) tab);
Daniel Veillard81a8ec62005-08-22 00:20:58 +00007844 return(NULL);
7845 }
7846 }
7847 }
Rob Richards54a8f672005-10-07 02:33:00 +00007848 xmlFree((xmlChar **) tab);
Daniel Veillard81a8ec62005-08-22 00:20:58 +00007849 return(ret);
7850}
Daniel Veillardf8e3db02012-09-11 13:26:36 +08007851
Daniel Veillard81a8ec62005-08-22 00:20:58 +00007852/**
Daniel Veillard0090bd52005-08-22 14:43:43 +00007853 * xmlExpExpDerive:
7854 * @ctxt: the expressions context
7855 * @exp: the englobing expression
7856 * @sub: the subexpression
7857 *
7858 * Evaluates the expression resulting from @exp consuming a sub expression @sub
7859 * Based on algebraic derivation and sometimes direct Brzozowski derivation
Haibo Huangcfd91dc2020-07-30 23:01:33 -07007860 * it usually takes less than linear time and can handle expressions generating
Daniel Veillard0090bd52005-08-22 14:43:43 +00007861 * infinite languages.
7862 *
7863 * Returns the resulting expression or NULL in case of internal error, the
7864 * result must be freed
7865 */
7866xmlExpNodePtr
7867xmlExpExpDerive(xmlExpCtxtPtr ctxt, xmlExpNodePtr exp, xmlExpNodePtr sub) {
7868 if ((exp == NULL) || (ctxt == NULL) || (sub == NULL))
7869 return(NULL);
7870
7871 /*
7872 * O(1) speedups
7873 */
7874 if (IS_NILLABLE(sub) && (!IS_NILLABLE(exp))) {
7875#ifdef DEBUG_DERIV
7876 printf("Sub nillable and not exp : can't subsume\n");
7877#endif
7878 return(forbiddenExp);
7879 }
7880 if (xmlExpCheckCard(exp, sub) == 0) {
7881#ifdef DEBUG_DERIV
Haibo Huangcfd91dc2020-07-30 23:01:33 -07007882 printf("sub generate longer sequences than exp : can't subsume\n");
Daniel Veillard0090bd52005-08-22 14:43:43 +00007883#endif
7884 return(forbiddenExp);
7885 }
7886 return(xmlExpExpDeriveInt(ctxt, exp, sub));
7887}
7888
7889/**
Daniel Veillard81a8ec62005-08-22 00:20:58 +00007890 * xmlExpSubsume:
7891 * @ctxt: the expressions context
7892 * @exp: the englobing expression
7893 * @sub: the subexpression
7894 *
Haibo Huangcfd91dc2020-07-30 23:01:33 -07007895 * Check whether @exp accepts all the languages accepted by @sub
Daniel Veillard81a8ec62005-08-22 00:20:58 +00007896 * the input being a subexpression.
7897 *
7898 * Returns 1 if true 0 if false and -1 in case of failure.
7899 */
7900int
7901xmlExpSubsume(xmlExpCtxtPtr ctxt, xmlExpNodePtr exp, xmlExpNodePtr sub) {
7902 xmlExpNodePtr tmp;
Daniel Veillardf8e3db02012-09-11 13:26:36 +08007903
Daniel Veillard81a8ec62005-08-22 00:20:58 +00007904 if ((exp == NULL) || (ctxt == NULL) || (sub == NULL))
7905 return(-1);
7906
7907 /*
7908 * TODO: speedup by checking the language of sub is a subset of the
7909 * language of exp
7910 */
7911 /*
7912 * O(1) speedups
7913 */
7914 if (IS_NILLABLE(sub) && (!IS_NILLABLE(exp))) {
7915#ifdef DEBUG_DERIV
7916 printf("Sub nillable and not exp : can't subsume\n");
7917#endif
7918 return(0);
7919 }
7920 if (xmlExpCheckCard(exp, sub) == 0) {
7921#ifdef DEBUG_DERIV
Haibo Huangcfd91dc2020-07-30 23:01:33 -07007922 printf("sub generate longer sequences than exp : can't subsume\n");
Daniel Veillard81a8ec62005-08-22 00:20:58 +00007923#endif
7924 return(0);
7925 }
7926 tmp = xmlExpExpDeriveInt(ctxt, exp, sub);
7927#ifdef DEBUG_DERIV
7928 printf("Result derivation :\n");
7929 PRINT_EXP(tmp);
7930#endif
7931 if (tmp == NULL)
7932 return(-1);
7933 if (tmp == forbiddenExp)
7934 return(0);
7935 if (tmp == emptyExp)
7936 return(1);
7937 if ((tmp != NULL) && (IS_NILLABLE(tmp))) {
7938 xmlExpFree(ctxt, tmp);
7939 return(1);
7940 }
7941 xmlExpFree(ctxt, tmp);
7942 return(0);
7943}
Daniel Veillard465a0002005-08-22 12:07:04 +00007944
7945/************************************************************************
7946 * *
Daniel Veillardf8e3db02012-09-11 13:26:36 +08007947 * Parsing expression *
Daniel Veillard465a0002005-08-22 12:07:04 +00007948 * *
7949 ************************************************************************/
7950
7951static xmlExpNodePtr xmlExpParseExpr(xmlExpCtxtPtr ctxt);
7952
7953#undef CUR
7954#define CUR (*ctxt->cur)
7955#undef NEXT
7956#define NEXT ctxt->cur++;
7957#undef IS_BLANK
7958#define IS_BLANK(c) ((c == ' ') || (c == '\n') || (c == '\r') || (c == '\t'))
7959#define SKIP_BLANKS while (IS_BLANK(*ctxt->cur)) ctxt->cur++;
7960
7961static int
7962xmlExpParseNumber(xmlExpCtxtPtr ctxt) {
7963 int ret = 0;
7964
7965 SKIP_BLANKS
7966 if (CUR == '*') {
7967 NEXT
7968 return(-1);
7969 }
7970 if ((CUR < '0') || (CUR > '9'))
7971 return(-1);
7972 while ((CUR >= '0') && (CUR <= '9')) {
7973 ret = ret * 10 + (CUR - '0');
7974 NEXT
7975 }
7976 return(ret);
7977}
7978
7979static xmlExpNodePtr
7980xmlExpParseOr(xmlExpCtxtPtr ctxt) {
7981 const char *base;
7982 xmlExpNodePtr ret;
7983 const xmlChar *val;
7984
7985 SKIP_BLANKS
7986 base = ctxt->cur;
7987 if (*ctxt->cur == '(') {
7988 NEXT
7989 ret = xmlExpParseExpr(ctxt);
7990 SKIP_BLANKS
7991 if (*ctxt->cur != ')') {
7992 fprintf(stderr, "unbalanced '(' : %s\n", base);
7993 xmlExpFree(ctxt, ret);
7994 return(NULL);
7995 }
7996 NEXT;
7997 SKIP_BLANKS
7998 goto parse_quantifier;
7999 }
8000 while ((CUR != 0) && (!(IS_BLANK(CUR))) && (CUR != '(') &&
8001 (CUR != ')') && (CUR != '|') && (CUR != ',') && (CUR != '{') &&
8002 (CUR != '*') && (CUR != '+') && (CUR != '?') && (CUR != '}'))
8003 NEXT;
8004 val = xmlDictLookup(ctxt->dict, BAD_CAST base, ctxt->cur - base);
8005 if (val == NULL)
8006 return(NULL);
8007 ret = xmlExpHashGetEntry(ctxt, XML_EXP_ATOM, NULL, NULL, val, 0, 0);
8008 if (ret == NULL)
8009 return(NULL);
8010 SKIP_BLANKS
8011parse_quantifier:
8012 if (CUR == '{') {
8013 int min, max;
8014
8015 NEXT
8016 min = xmlExpParseNumber(ctxt);
8017 if (min < 0) {
8018 xmlExpFree(ctxt, ret);
8019 return(NULL);
8020 }
8021 SKIP_BLANKS
8022 if (CUR == ',') {
8023 NEXT
8024 max = xmlExpParseNumber(ctxt);
8025 SKIP_BLANKS
8026 } else
8027 max = min;
8028 if (CUR != '}') {
8029 xmlExpFree(ctxt, ret);
8030 return(NULL);
8031 }
8032 NEXT
8033 ret = xmlExpHashGetEntry(ctxt, XML_EXP_COUNT, ret, NULL, NULL,
8034 min, max);
8035 SKIP_BLANKS
8036 } else if (CUR == '?') {
8037 NEXT
8038 ret = xmlExpHashGetEntry(ctxt, XML_EXP_COUNT, ret, NULL, NULL,
8039 0, 1);
8040 SKIP_BLANKS
8041 } else if (CUR == '+') {
8042 NEXT
8043 ret = xmlExpHashGetEntry(ctxt, XML_EXP_COUNT, ret, NULL, NULL,
8044 1, -1);
8045 SKIP_BLANKS
8046 } else if (CUR == '*') {
8047 NEXT
8048 ret = xmlExpHashGetEntry(ctxt, XML_EXP_COUNT, ret, NULL, NULL,
8049 0, -1);
8050 SKIP_BLANKS
Daniel Veillardf8e3db02012-09-11 13:26:36 +08008051 }
Daniel Veillard465a0002005-08-22 12:07:04 +00008052 return(ret);
8053}
8054
8055
8056static xmlExpNodePtr
8057xmlExpParseSeq(xmlExpCtxtPtr ctxt) {
8058 xmlExpNodePtr ret, right;
8059
8060 ret = xmlExpParseOr(ctxt);
8061 SKIP_BLANKS
8062 while (CUR == '|') {
8063 NEXT
8064 right = xmlExpParseOr(ctxt);
8065 if (right == NULL) {
8066 xmlExpFree(ctxt, ret);
8067 return(NULL);
8068 }
8069 ret = xmlExpHashGetEntry(ctxt, XML_EXP_OR, ret, right, NULL, 0, 0);
8070 if (ret == NULL)
8071 return(NULL);
8072 }
8073 return(ret);
8074}
8075
8076static xmlExpNodePtr
8077xmlExpParseExpr(xmlExpCtxtPtr ctxt) {
8078 xmlExpNodePtr ret, right;
8079
8080 ret = xmlExpParseSeq(ctxt);
8081 SKIP_BLANKS
8082 while (CUR == ',') {
8083 NEXT
8084 right = xmlExpParseSeq(ctxt);
8085 if (right == NULL) {
8086 xmlExpFree(ctxt, ret);
8087 return(NULL);
8088 }
8089 ret = xmlExpHashGetEntry(ctxt, XML_EXP_SEQ, ret, right, NULL, 0, 0);
8090 if (ret == NULL)
8091 return(NULL);
8092 }
8093 return(ret);
8094}
8095
8096/**
8097 * xmlExpParse:
8098 * @ctxt: the expressions context
8099 * @expr: the 0 terminated string
8100 *
8101 * Minimal parser for regexps, it understand the following constructs
8102 * - string terminals
8103 * - choice operator |
8104 * - sequence operator ,
8105 * - subexpressions (...)
8106 * - usual cardinality operators + * and ?
8107 * - finite sequences { min, max }
8108 * - infinite sequences { min, * }
8109 * There is minimal checkings made especially no checking on strings values
8110 *
8111 * Returns a new expression or NULL in case of failure
8112 */
8113xmlExpNodePtr
8114xmlExpParse(xmlExpCtxtPtr ctxt, const char *expr) {
8115 xmlExpNodePtr ret;
8116
8117 ctxt->expr = expr;
8118 ctxt->cur = expr;
8119
8120 ret = xmlExpParseExpr(ctxt);
8121 SKIP_BLANKS
8122 if (*ctxt->cur != 0) {
8123 xmlExpFree(ctxt, ret);
8124 return(NULL);
8125 }
8126 return(ret);
8127}
8128
8129static void
8130xmlExpDumpInt(xmlBufferPtr buf, xmlExpNodePtr expr, int glob) {
8131 xmlExpNodePtr c;
8132
8133 if (expr == NULL) return;
8134 if (glob) xmlBufferWriteChar(buf, "(");
8135 switch (expr->type) {
8136 case XML_EXP_EMPTY:
8137 xmlBufferWriteChar(buf, "empty");
8138 break;
8139 case XML_EXP_FORBID:
8140 xmlBufferWriteChar(buf, "forbidden");
8141 break;
8142 case XML_EXP_ATOM:
8143 xmlBufferWriteCHAR(buf, expr->exp_str);
8144 break;
8145 case XML_EXP_SEQ:
8146 c = expr->exp_left;
8147 if ((c->type == XML_EXP_SEQ) || (c->type == XML_EXP_OR))
8148 xmlExpDumpInt(buf, c, 1);
8149 else
8150 xmlExpDumpInt(buf, c, 0);
8151 xmlBufferWriteChar(buf, " , ");
8152 c = expr->exp_right;
8153 if ((c->type == XML_EXP_SEQ) || (c->type == XML_EXP_OR))
8154 xmlExpDumpInt(buf, c, 1);
8155 else
8156 xmlExpDumpInt(buf, c, 0);
8157 break;
8158 case XML_EXP_OR:
8159 c = expr->exp_left;
8160 if ((c->type == XML_EXP_SEQ) || (c->type == XML_EXP_OR))
8161 xmlExpDumpInt(buf, c, 1);
8162 else
8163 xmlExpDumpInt(buf, c, 0);
8164 xmlBufferWriteChar(buf, " | ");
8165 c = expr->exp_right;
8166 if ((c->type == XML_EXP_SEQ) || (c->type == XML_EXP_OR))
8167 xmlExpDumpInt(buf, c, 1);
8168 else
8169 xmlExpDumpInt(buf, c, 0);
8170 break;
8171 case XML_EXP_COUNT: {
8172 char rep[40];
Daniel Veillardf8e3db02012-09-11 13:26:36 +08008173
Daniel Veillard465a0002005-08-22 12:07:04 +00008174 c = expr->exp_left;
8175 if ((c->type == XML_EXP_SEQ) || (c->type == XML_EXP_OR))
8176 xmlExpDumpInt(buf, c, 1);
8177 else
8178 xmlExpDumpInt(buf, c, 0);
8179 if ((expr->exp_min == 0) && (expr->exp_max == 1)) {
8180 rep[0] = '?';
8181 rep[1] = 0;
8182 } else if ((expr->exp_min == 0) && (expr->exp_max == -1)) {
8183 rep[0] = '*';
8184 rep[1] = 0;
8185 } else if ((expr->exp_min == 1) && (expr->exp_max == -1)) {
8186 rep[0] = '+';
8187 rep[1] = 0;
8188 } else if (expr->exp_max == expr->exp_min) {
8189 snprintf(rep, 39, "{%d}", expr->exp_min);
8190 } else if (expr->exp_max < 0) {
8191 snprintf(rep, 39, "{%d,inf}", expr->exp_min);
8192 } else {
8193 snprintf(rep, 39, "{%d,%d}", expr->exp_min, expr->exp_max);
8194 }
8195 rep[39] = 0;
8196 xmlBufferWriteChar(buf, rep);
8197 break;
8198 }
8199 default:
8200 fprintf(stderr, "Error in tree\n");
8201 }
8202 if (glob)
8203 xmlBufferWriteChar(buf, ")");
8204}
8205/**
8206 * xmlExpDump:
8207 * @buf: a buffer to receive the output
8208 * @expr: the compiled expression
8209 *
8210 * Serialize the expression as compiled to the buffer
8211 */
8212void
Daniel Veillard5eee7672005-08-22 21:22:27 +00008213xmlExpDump(xmlBufferPtr buf, xmlExpNodePtr expr) {
8214 if ((buf == NULL) || (expr == NULL))
Daniel Veillard465a0002005-08-22 12:07:04 +00008215 return;
Daniel Veillard5eee7672005-08-22 21:22:27 +00008216 xmlExpDumpInt(buf, expr, 0);
Daniel Veillard465a0002005-08-22 12:07:04 +00008217}
8218
8219/**
8220 * xmlExpMaxToken:
8221 * @expr: a compiled expression
8222 *
8223 * Indicate the maximum number of input a expression can accept
8224 *
8225 * Returns the maximum length or -1 in case of error
8226 */
8227int
8228xmlExpMaxToken(xmlExpNodePtr expr) {
8229 if (expr == NULL)
8230 return(-1);
8231 return(expr->c_max);
8232}
8233
8234/**
8235 * xmlExpCtxtNbNodes:
8236 * @ctxt: an expression context
8237 *
8238 * Debugging facility provides the number of allocated nodes at a that point
8239 *
8240 * Returns the number of nodes in use or -1 in case of error
8241 */
8242int
8243xmlExpCtxtNbNodes(xmlExpCtxtPtr ctxt) {
8244 if (ctxt == NULL)
8245 return(-1);
8246 return(ctxt->nb_nodes);
8247}
8248
8249/**
8250 * xmlExpCtxtNbCons:
8251 * @ctxt: an expression context
8252 *
8253 * Debugging facility provides the number of allocated nodes over lifetime
8254 *
8255 * Returns the number of nodes ever allocated or -1 in case of error
8256 */
8257int
8258xmlExpCtxtNbCons(xmlExpCtxtPtr ctxt) {
8259 if (ctxt == NULL)
8260 return(-1);
8261 return(ctxt->nb_cons);
8262}
8263
Daniel Veillard81a8ec62005-08-22 00:20:58 +00008264#endif /* LIBXML_EXPR_ENABLED */
Daniel Veillard5d4644e2005-04-01 13:11:58 +00008265#define bottom_xmlregexp
8266#include "elfgcchack.h"
Daniel Veillard4255d502002-04-16 15:50:10 +00008267#endif /* LIBXML_REGEXP_ENABLED */