Coverage Report

Created: 2025-07-18 06:55

/src/libxml2/xmlregexp.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * regexp.c: generic and extensible Regular Expression engine
3
 *
4
 * Basically designed with the purpose of compiling regexps for
5
 * the variety of validation/schemas mechanisms now available in
6
 * XML related specifications these include:
7
 *    - XML-1.0 DTD validation
8
 *    - XML Schemas structure part 1
9
 *    - XML Schemas Datatypes part 2 especially Appendix F
10
 *    - RELAX-NG/TREX i.e. the counter proposal
11
 *
12
 * See Copyright for the status of this software.
13
 *
14
 * Author: Daniel Veillard
15
 */
16
17
#define IN_LIBXML
18
#include "libxml.h"
19
20
#ifdef LIBXML_REGEXP_ENABLED
21
22
#include <stdio.h>
23
#include <string.h>
24
#include <limits.h>
25
26
#include <libxml/tree.h>
27
#include <libxml/parserInternals.h>
28
#include <libxml/xmlregexp.h>
29
#include <libxml/xmlautomata.h>
30
31
#include "private/error.h"
32
#include "private/memory.h"
33
#include "private/regexp.h"
34
35
#ifndef SIZE_MAX
36
#define SIZE_MAX ((size_t) -1)
37
#endif
38
39
/* #define DEBUG_REGEXP */
40
41
0
#define MAX_PUSH 10000000
42
43
#ifdef ERROR
44
#undef ERROR
45
#endif
46
#define ERROR(str)              \
47
9.68k
    ctxt->error = XML_REGEXP_COMPILE_ERROR;       \
48
9.68k
    xmlRegexpErrCompile(ctxt, str);
49
100k
#define NEXT ctxt->cur++
50
488k
#define CUR (*(ctxt->cur))
51
5.50k
#define NXT(index) (ctxt->cur[index])
52
53
16.2k
#define NEXTL(l) ctxt->cur += l;
54
0
#define XML_REG_STRING_SEPARATOR '|'
55
/*
56
 * Need PREV to check on a '-' within a Character Group. May only be used
57
 * when it's guaranteed that cur is not at the beginning of ctxt->string!
58
 */
59
1.45k
#define PREV (ctxt->cur[-1])
60
61
/************************************************************************
62
 *                  *
63
 *      Unicode support         *
64
 *                  *
65
 ************************************************************************/
66
67
typedef struct {
68
    const char *rangename;
69
    const xmlChRangeGroup group;
70
} xmlUnicodeRange;
71
72
#include "codegen/unicode.inc"
73
74
/**
75
 * binary table lookup for user-supplied name
76
 *
77
 * @param sptr  a table of xmlUnicodeRange structs
78
 * @param numentries  number of table entries
79
 * @param tname  name to be found
80
 * @returns pointer to range function if found, otherwise NULL
81
 */
82
static const xmlChRangeGroup *
83
xmlUnicodeLookup(const xmlUnicodeRange *sptr, int numentries,
84
8.59k
                 const char *tname) {
85
8.59k
    int low, high, mid, cmp;
86
87
8.59k
    if (tname == NULL) return(NULL);
88
89
8.59k
    low = 0;
90
8.59k
    high = numentries - 1;
91
58.1k
    while (low <= high) {
92
56.8k
  mid = (low + high) / 2;
93
56.8k
  cmp = strcmp(tname, sptr[mid].rangename);
94
56.8k
  if (cmp == 0)
95
7.36k
      return (&sptr[mid].group);
96
49.5k
  if (cmp < 0)
97
20.4k
      high = mid - 1;
98
29.0k
  else
99
29.0k
      low = mid + 1;
100
49.5k
    }
101
1.23k
    return (NULL);
102
8.59k
}
103
104
/**
105
 * Check whether the character is part of the UCS Block
106
 *
107
 * @param code  UCS code point
108
 * @param block  UCS block name
109
 * @returns 1 if true, 0 if false and -1 on unknown block
110
 */
111
static int
112
8.59k
xmlUCSIsBlock(int code, const char *block) {
113
8.59k
    const xmlChRangeGroup *group;
114
115
8.59k
    group = xmlUnicodeLookup(xmlUnicodeBlocks,
116
8.59k
            sizeof(xmlUnicodeBlocks) / sizeof(xmlUnicodeBlocks[0]), block);
117
8.59k
    if (group == NULL)
118
1.23k
  return (-1);
119
7.36k
    return (xmlCharInRange(code, group));
120
8.59k
}
121
122
/************************************************************************
123
 *                  *
124
 *      Datatypes and structures      *
125
 *                  *
126
 ************************************************************************/
127
128
/*
129
 * Note: the order of the enums below is significant, do not shuffle
130
 */
131
typedef enum {
132
    XML_REGEXP_EPSILON = 1,
133
    XML_REGEXP_CHARVAL,
134
    XML_REGEXP_RANGES,
135
    XML_REGEXP_SUBREG,  /* used for () sub regexps */
136
    XML_REGEXP_STRING,
137
    XML_REGEXP_ANYCHAR, /* . */
138
    XML_REGEXP_ANYSPACE, /* \s */
139
    XML_REGEXP_NOTSPACE, /* \S */
140
    XML_REGEXP_INITNAME, /* \l */
141
    XML_REGEXP_NOTINITNAME, /* \L */
142
    XML_REGEXP_NAMECHAR, /* \c */
143
    XML_REGEXP_NOTNAMECHAR, /* \C */
144
    XML_REGEXP_DECIMAL, /* \d */
145
    XML_REGEXP_NOTDECIMAL, /* \D */
146
    XML_REGEXP_REALCHAR, /* \w */
147
    XML_REGEXP_NOTREALCHAR, /* \W */
148
    XML_REGEXP_LETTER = 100,
149
    XML_REGEXP_LETTER_UPPERCASE,
150
    XML_REGEXP_LETTER_LOWERCASE,
151
    XML_REGEXP_LETTER_TITLECASE,
152
    XML_REGEXP_LETTER_MODIFIER,
153
    XML_REGEXP_LETTER_OTHERS,
154
    XML_REGEXP_MARK,
155
    XML_REGEXP_MARK_NONSPACING,
156
    XML_REGEXP_MARK_SPACECOMBINING,
157
    XML_REGEXP_MARK_ENCLOSING,
158
    XML_REGEXP_NUMBER,
159
    XML_REGEXP_NUMBER_DECIMAL,
160
    XML_REGEXP_NUMBER_LETTER,
161
    XML_REGEXP_NUMBER_OTHERS,
162
    XML_REGEXP_PUNCT,
163
    XML_REGEXP_PUNCT_CONNECTOR,
164
    XML_REGEXP_PUNCT_DASH,
165
    XML_REGEXP_PUNCT_OPEN,
166
    XML_REGEXP_PUNCT_CLOSE,
167
    XML_REGEXP_PUNCT_INITQUOTE,
168
    XML_REGEXP_PUNCT_FINQUOTE,
169
    XML_REGEXP_PUNCT_OTHERS,
170
    XML_REGEXP_SEPAR,
171
    XML_REGEXP_SEPAR_SPACE,
172
    XML_REGEXP_SEPAR_LINE,
173
    XML_REGEXP_SEPAR_PARA,
174
    XML_REGEXP_SYMBOL,
175
    XML_REGEXP_SYMBOL_MATH,
176
    XML_REGEXP_SYMBOL_CURRENCY,
177
    XML_REGEXP_SYMBOL_MODIFIER,
178
    XML_REGEXP_SYMBOL_OTHERS,
179
    XML_REGEXP_OTHER,
180
    XML_REGEXP_OTHER_CONTROL,
181
    XML_REGEXP_OTHER_FORMAT,
182
    XML_REGEXP_OTHER_PRIVATE,
183
    XML_REGEXP_OTHER_NA,
184
    XML_REGEXP_BLOCK_NAME
185
} xmlRegAtomType;
186
187
typedef enum {
188
    XML_REGEXP_QUANT_EPSILON = 1,
189
    XML_REGEXP_QUANT_ONCE,
190
    XML_REGEXP_QUANT_OPT,
191
    XML_REGEXP_QUANT_MULT,
192
    XML_REGEXP_QUANT_PLUS,
193
    XML_REGEXP_QUANT_ONCEONLY,
194
    XML_REGEXP_QUANT_ALL,
195
    XML_REGEXP_QUANT_RANGE
196
} xmlRegQuantType;
197
198
typedef enum {
199
    XML_REGEXP_START_STATE = 1,
200
    XML_REGEXP_FINAL_STATE,
201
    XML_REGEXP_TRANS_STATE,
202
    XML_REGEXP_SINK_STATE,
203
    XML_REGEXP_UNREACH_STATE
204
} xmlRegStateType;
205
206
typedef enum {
207
    XML_REGEXP_MARK_NORMAL = 0,
208
    XML_REGEXP_MARK_START,
209
    XML_REGEXP_MARK_VISITED
210
} xmlRegMarkedType;
211
212
typedef struct _xmlRegRange xmlRegRange;
213
typedef xmlRegRange *xmlRegRangePtr;
214
215
struct _xmlRegRange {
216
    int neg;    /* 0 normal, 1 not, 2 exclude */
217
    xmlRegAtomType type;
218
    int start;
219
    int end;
220
    xmlChar *blockName;
221
};
222
223
typedef struct _xmlRegAtom xmlRegAtom;
224
typedef xmlRegAtom *xmlRegAtomPtr;
225
226
typedef struct _xmlAutomataState xmlRegState;
227
typedef xmlRegState *xmlRegStatePtr;
228
229
struct _xmlRegAtom {
230
    int no;
231
    xmlRegAtomType type;
232
    xmlRegQuantType quant;
233
    int min;
234
    int max;
235
236
    void *valuep;
237
    void *valuep2;
238
    int neg;
239
    int codepoint;
240
    xmlRegStatePtr start;
241
    xmlRegStatePtr start0;
242
    xmlRegStatePtr stop;
243
    int maxRanges;
244
    int nbRanges;
245
    xmlRegRangePtr *ranges;
246
    void *data;
247
};
248
249
typedef struct _xmlRegCounter xmlRegCounter;
250
typedef xmlRegCounter *xmlRegCounterPtr;
251
252
struct _xmlRegCounter {
253
    int min;
254
    int max;
255
};
256
257
typedef struct _xmlRegTrans xmlRegTrans;
258
typedef xmlRegTrans *xmlRegTransPtr;
259
260
struct _xmlRegTrans {
261
    xmlRegAtomPtr atom;
262
    int to;
263
    int counter;
264
    int count;
265
    int nd;
266
};
267
268
struct _xmlAutomataState {
269
    xmlRegStateType type;
270
    xmlRegMarkedType mark;
271
    xmlRegMarkedType markd;
272
    xmlRegMarkedType reached;
273
    int no;
274
    int maxTrans;
275
    int nbTrans;
276
    xmlRegTrans *trans;
277
    /*  knowing states pointing to us can speed things up */
278
    int maxTransTo;
279
    int nbTransTo;
280
    int *transTo;
281
};
282
283
typedef struct _xmlAutomata xmlRegParserCtxt;
284
typedef xmlRegParserCtxt *xmlRegParserCtxtPtr;
285
286
51.1k
#define AM_AUTOMATA_RNG 1
287
288
struct _xmlAutomata {
289
    xmlChar *string;
290
    xmlChar *cur;
291
292
    int error;
293
    int neg;
294
295
    xmlRegStatePtr start;
296
    xmlRegStatePtr end;
297
    xmlRegStatePtr state;
298
299
    xmlRegAtomPtr atom;
300
301
    int maxAtoms;
302
    int nbAtoms;
303
    xmlRegAtomPtr *atoms;
304
305
    int maxStates;
306
    int nbStates;
307
    xmlRegStatePtr *states;
308
309
    int maxCounters;
310
    int nbCounters;
311
    xmlRegCounter *counters;
312
313
    int determinist;
314
    int negs;
315
    int flags;
316
317
    int depth;
318
};
319
320
struct _xmlRegexp {
321
    xmlChar *string;
322
    int nbStates;
323
    xmlRegStatePtr *states;
324
    int nbAtoms;
325
    xmlRegAtomPtr *atoms;
326
    int nbCounters;
327
    xmlRegCounter *counters;
328
    int determinist;
329
    int flags;
330
    /*
331
     * That's the compact form for determinists automatas
332
     */
333
    int nbstates;
334
    int *compact;
335
    void **transdata;
336
    int nbstrings;
337
    xmlChar **stringMap;
338
};
339
340
typedef struct _xmlRegExecRollback xmlRegExecRollback;
341
typedef xmlRegExecRollback *xmlRegExecRollbackPtr;
342
343
struct _xmlRegExecRollback {
344
    xmlRegStatePtr state;/* the current state */
345
    int index;    /* the index in the input stack */
346
    int nextbranch; /* the next transition to explore in that state */
347
    int *counts;  /* save the automata state if it has some */
348
};
349
350
typedef struct _xmlRegInputToken xmlRegInputToken;
351
typedef xmlRegInputToken *xmlRegInputTokenPtr;
352
353
struct _xmlRegInputToken {
354
    xmlChar *value;
355
    void *data;
356
};
357
358
struct _xmlRegExecCtxt {
359
    int status;   /* execution status != 0 indicate an error */
360
    int determinist;  /* did we find an indeterministic behaviour */
361
    xmlRegexpPtr comp;  /* the compiled regexp */
362
    xmlRegExecCallbacks callback;
363
    void *data;
364
365
    xmlRegStatePtr state;/* the current state */
366
    int transno;  /* the current transition on that state */
367
    int transcount; /* the number of chars in char counted transitions */
368
369
    /*
370
     * A stack of rollback states
371
     */
372
    int maxRollbacks;
373
    int nbRollbacks;
374
    xmlRegExecRollback *rollbacks;
375
376
    /*
377
     * The state of the automata if any
378
     */
379
    int *counts;
380
381
    /*
382
     * The input stack
383
     */
384
    int inputStackMax;
385
    int inputStackNr;
386
    int index;
387
    int *charStack;
388
    const xmlChar *inputString; /* when operating on characters */
389
    xmlRegInputTokenPtr inputStack;/* when operating on strings */
390
391
    /*
392
     * error handling
393
     */
394
    int errStateNo;   /* the error state number */
395
    xmlRegStatePtr errState;    /* the error state */
396
    xmlChar *errString;   /* the string raising the error */
397
    int *errCounts;   /* counters at the error state */
398
    int nbPush;
399
};
400
401
0
#define REGEXP_ALL_COUNTER  0x123456
402
0
#define REGEXP_ALL_LAX_COUNTER  0x123457
403
404
static void xmlFAParseRegExp(xmlRegParserCtxtPtr ctxt, int top);
405
static void xmlRegFreeState(xmlRegStatePtr state);
406
static void xmlRegFreeAtom(xmlRegAtomPtr atom);
407
static int xmlRegStrEqualWildcard(const xmlChar *expStr, const xmlChar *valStr);
408
static int xmlRegCheckCharacter(xmlRegAtomPtr atom, int codepoint);
409
static int xmlRegCheckCharacterRange(xmlRegAtomType type, int codepoint,
410
                  int neg, int start, int end, const xmlChar *blockName);
411
412
/************************************************************************
413
 *                  *
414
 *    Regexp memory error handler       *
415
 *                  *
416
 ************************************************************************/
417
/**
418
 * Handle an out of memory condition
419
 *
420
 * @param ctxt  regexp parser context
421
 */
422
static void
423
xmlRegexpErrMemory(xmlRegParserCtxtPtr ctxt)
424
561
{
425
561
    if (ctxt != NULL)
426
561
        ctxt->error = XML_ERR_NO_MEMORY;
427
428
561
    xmlRaiseMemoryError(NULL, NULL, NULL, XML_FROM_REGEXP, NULL);
429
561
}
430
431
/**
432
 * Handle a compilation failure
433
 *
434
 * @param ctxt  regexp parser context
435
 * @param extra  extra information
436
 */
437
static void
438
xmlRegexpErrCompile(xmlRegParserCtxtPtr ctxt, const char *extra)
439
9.68k
{
440
9.68k
    const char *regexp = NULL;
441
9.68k
    int idx = 0;
442
9.68k
    int res;
443
444
9.68k
    if (ctxt != NULL) {
445
9.68k
        regexp = (const char *) ctxt->string;
446
9.68k
  idx = ctxt->cur - ctxt->string;
447
9.68k
  ctxt->error = XML_REGEXP_COMPILE_ERROR;
448
9.68k
    }
449
450
9.68k
    res = xmlRaiseError(NULL, NULL, NULL, NULL, NULL, XML_FROM_REGEXP,
451
9.68k
                        XML_REGEXP_COMPILE_ERROR, XML_ERR_FATAL,
452
9.68k
                        NULL, 0, extra, regexp, NULL, idx, 0,
453
9.68k
                        "failed to compile: %s\n", extra);
454
9.68k
    if (res < 0)
455
81
        xmlRegexpErrMemory(ctxt);
456
9.68k
}
457
458
/************************************************************************
459
 *                  *
460
 *      Allocation/Deallocation       *
461
 *                  *
462
 ************************************************************************/
463
464
static int xmlFAComputesDeterminism(xmlRegParserCtxtPtr ctxt);
465
466
/**
467
 * Allocate a two-dimensional array and set all elements to zero.
468
 *
469
 * @param dim1  size of first dimension
470
 * @param dim2  size of second dimension
471
 * @param elemSize  size of element
472
 * @returns the new array or NULL in case of error.
473
 */
474
static void*
475
0
xmlRegCalloc2(size_t dim1, size_t dim2, size_t elemSize) {
476
0
    size_t totalSize;
477
0
    void *ret;
478
479
    /* Check for overflow */
480
0
    if ((dim2 == 0) || (elemSize == 0) ||
481
0
        (dim1 > SIZE_MAX / dim2 / elemSize))
482
0
        return (NULL);
483
0
    totalSize = dim1 * dim2 * elemSize;
484
0
    ret = xmlMalloc(totalSize);
485
0
    if (ret != NULL)
486
0
        memset(ret, 0, totalSize);
487
0
    return (ret);
488
0
}
489
490
/**
491
 * Allocate a new regexp and fill it with the result from the parser
492
 *
493
 * @param ctxt  the parser context used to build it
494
 * @returns the new regexp or NULL in case of error
495
 */
496
static xmlRegexpPtr
497
4.80k
xmlRegEpxFromParse(xmlRegParserCtxtPtr ctxt) {
498
4.80k
    xmlRegexpPtr ret;
499
500
4.80k
    ret = (xmlRegexpPtr) xmlMalloc(sizeof(xmlRegexp));
501
4.80k
    if (ret == NULL) {
502
6
  xmlRegexpErrMemory(ctxt);
503
6
  return(NULL);
504
6
    }
505
4.80k
    memset(ret, 0, sizeof(xmlRegexp));
506
4.80k
    ret->string = ctxt->string;
507
4.80k
    ret->nbStates = ctxt->nbStates;
508
4.80k
    ret->states = ctxt->states;
509
4.80k
    ret->nbAtoms = ctxt->nbAtoms;
510
4.80k
    ret->atoms = ctxt->atoms;
511
4.80k
    ret->nbCounters = ctxt->nbCounters;
512
4.80k
    ret->counters = ctxt->counters;
513
4.80k
    ret->determinist = ctxt->determinist;
514
4.80k
    ret->flags = ctxt->flags;
515
4.80k
    if (ret->determinist == -1) {
516
4.80k
        if (xmlRegexpIsDeterminist(ret) < 0) {
517
15
            xmlRegexpErrMemory(ctxt);
518
15
            xmlFree(ret);
519
15
            return(NULL);
520
15
        }
521
4.80k
    }
522
523
4.78k
    if ((ret->determinist != 0) &&
524
4.78k
  (ret->nbCounters == 0) &&
525
4.78k
  (ctxt->negs == 0) &&
526
4.78k
  (ret->atoms != NULL) &&
527
4.78k
  (ret->atoms[0] != NULL) &&
528
4.78k
  (ret->atoms[0]->type == XML_REGEXP_STRING)) {
529
0
  int i, j, nbstates = 0, nbatoms = 0;
530
0
  int *stateRemap;
531
0
  int *stringRemap;
532
0
  int *transitions;
533
0
  void **transdata;
534
0
  xmlChar **stringMap;
535
0
        xmlChar *value;
536
537
  /*
538
   * Switch to a compact representation
539
   * 1/ counting the effective number of states left
540
   * 2/ counting the unique number of atoms, and check that
541
   *    they are all of the string type
542
   * 3/ build a table state x atom for the transitions
543
   */
544
545
0
  stateRemap = xmlMalloc(ret->nbStates * sizeof(int));
546
0
  if (stateRemap == NULL) {
547
0
      xmlRegexpErrMemory(ctxt);
548
0
      xmlFree(ret);
549
0
      return(NULL);
550
0
  }
551
0
  for (i = 0;i < ret->nbStates;i++) {
552
0
      if (ret->states[i] != NULL) {
553
0
    stateRemap[i] = nbstates;
554
0
    nbstates++;
555
0
      } else {
556
0
    stateRemap[i] = -1;
557
0
      }
558
0
  }
559
0
  stringMap = xmlMalloc(ret->nbAtoms * sizeof(char *));
560
0
  if (stringMap == NULL) {
561
0
      xmlRegexpErrMemory(ctxt);
562
0
      xmlFree(stateRemap);
563
0
      xmlFree(ret);
564
0
      return(NULL);
565
0
  }
566
0
  stringRemap = xmlMalloc(ret->nbAtoms * sizeof(int));
567
0
  if (stringRemap == NULL) {
568
0
      xmlRegexpErrMemory(ctxt);
569
0
      xmlFree(stringMap);
570
0
      xmlFree(stateRemap);
571
0
      xmlFree(ret);
572
0
      return(NULL);
573
0
  }
574
0
  for (i = 0;i < ret->nbAtoms;i++) {
575
0
      if ((ret->atoms[i]->type == XML_REGEXP_STRING) &&
576
0
    (ret->atoms[i]->quant == XML_REGEXP_QUANT_ONCE)) {
577
0
    value = ret->atoms[i]->valuep;
578
0
                for (j = 0;j < nbatoms;j++) {
579
0
        if (xmlStrEqual(stringMap[j], value)) {
580
0
      stringRemap[i] = j;
581
0
      break;
582
0
        }
583
0
    }
584
0
    if (j >= nbatoms) {
585
0
        stringRemap[i] = nbatoms;
586
0
        stringMap[nbatoms] = xmlStrdup(value);
587
0
        if (stringMap[nbatoms] == NULL) {
588
0
      for (i = 0;i < nbatoms;i++)
589
0
          xmlFree(stringMap[i]);
590
0
      xmlFree(stringRemap);
591
0
      xmlFree(stringMap);
592
0
      xmlFree(stateRemap);
593
0
      xmlFree(ret);
594
0
      return(NULL);
595
0
        }
596
0
        nbatoms++;
597
0
    }
598
0
      } else {
599
0
    xmlFree(stateRemap);
600
0
    xmlFree(stringRemap);
601
0
    for (i = 0;i < nbatoms;i++)
602
0
        xmlFree(stringMap[i]);
603
0
    xmlFree(stringMap);
604
0
    xmlFree(ret);
605
0
    return(NULL);
606
0
      }
607
0
  }
608
0
  transitions = (int *) xmlRegCalloc2(nbstates + 1, nbatoms + 1,
609
0
                                            sizeof(int));
610
0
  if (transitions == NULL) {
611
0
      xmlFree(stateRemap);
612
0
      xmlFree(stringRemap);
613
0
            for (i = 0;i < nbatoms;i++)
614
0
    xmlFree(stringMap[i]);
615
0
      xmlFree(stringMap);
616
0
      xmlFree(ret);
617
0
      return(NULL);
618
0
  }
619
620
  /*
621
   * Allocate the transition table. The first entry for each
622
   * state corresponds to the state type.
623
   */
624
0
  transdata = NULL;
625
626
0
  for (i = 0;i < ret->nbStates;i++) {
627
0
      int stateno, atomno, targetno, prev;
628
0
      xmlRegStatePtr state;
629
0
      xmlRegTransPtr trans;
630
631
0
      stateno = stateRemap[i];
632
0
      if (stateno == -1)
633
0
    continue;
634
0
      state = ret->states[i];
635
636
0
      transitions[stateno * (nbatoms + 1)] = state->type;
637
638
0
      for (j = 0;j < state->nbTrans;j++) {
639
0
    trans = &(state->trans[j]);
640
0
    if ((trans->to < 0) || (trans->atom == NULL))
641
0
        continue;
642
0
                atomno = stringRemap[trans->atom->no];
643
0
    if ((trans->atom->data != NULL) && (transdata == NULL)) {
644
0
        transdata = (void **) xmlRegCalloc2(nbstates, nbatoms,
645
0
                                      sizeof(void *));
646
0
        if (transdata == NULL) {
647
0
      xmlRegexpErrMemory(ctxt);
648
0
      break;
649
0
        }
650
0
    }
651
0
    targetno = stateRemap[trans->to];
652
    /*
653
     * if the same atom can generate transitions to 2 different
654
     * states then it means the automata is not deterministic and
655
     * the compact form can't be used !
656
     */
657
0
    prev = transitions[stateno * (nbatoms + 1) + atomno + 1];
658
0
    if (prev != 0) {
659
0
        if (prev != targetno + 1) {
660
0
      ret->determinist = 0;
661
0
      if (transdata != NULL)
662
0
          xmlFree(transdata);
663
0
      xmlFree(transitions);
664
0
      xmlFree(stateRemap);
665
0
      xmlFree(stringRemap);
666
0
      for (i = 0;i < nbatoms;i++)
667
0
          xmlFree(stringMap[i]);
668
0
      xmlFree(stringMap);
669
0
      goto not_determ;
670
0
        }
671
0
    } else {
672
0
        transitions[stateno * (nbatoms + 1) + atomno + 1] =
673
0
      targetno + 1; /* to avoid 0 */
674
0
        if (transdata != NULL)
675
0
      transdata[stateno * nbatoms + atomno] =
676
0
          trans->atom->data;
677
0
    }
678
0
      }
679
0
  }
680
0
  ret->determinist = 1;
681
  /*
682
   * Cleanup of the old data
683
   */
684
0
  if (ret->states != NULL) {
685
0
      for (i = 0;i < ret->nbStates;i++)
686
0
    xmlRegFreeState(ret->states[i]);
687
0
      xmlFree(ret->states);
688
0
  }
689
0
  ret->states = NULL;
690
0
  ret->nbStates = 0;
691
0
  if (ret->atoms != NULL) {
692
0
      for (i = 0;i < ret->nbAtoms;i++)
693
0
    xmlRegFreeAtom(ret->atoms[i]);
694
0
      xmlFree(ret->atoms);
695
0
  }
696
0
  ret->atoms = NULL;
697
0
  ret->nbAtoms = 0;
698
699
0
  ret->compact = transitions;
700
0
  ret->transdata = transdata;
701
0
  ret->stringMap = stringMap;
702
0
  ret->nbstrings = nbatoms;
703
0
  ret->nbstates = nbstates;
704
0
  xmlFree(stateRemap);
705
0
  xmlFree(stringRemap);
706
0
    }
707
4.78k
not_determ:
708
4.78k
    ctxt->string = NULL;
709
4.78k
    ctxt->nbStates = 0;
710
4.78k
    ctxt->states = NULL;
711
4.78k
    ctxt->nbAtoms = 0;
712
4.78k
    ctxt->atoms = NULL;
713
4.78k
    ctxt->nbCounters = 0;
714
4.78k
    ctxt->counters = NULL;
715
4.78k
    return(ret);
716
4.78k
}
717
718
/**
719
 * Allocate a new regexp parser context
720
 *
721
 * @param string  the string to parse
722
 * @returns the new context or NULL in case of error
723
 */
724
static xmlRegParserCtxtPtr
725
11.5k
xmlRegNewParserCtxt(const xmlChar *string) {
726
11.5k
    xmlRegParserCtxtPtr ret;
727
728
11.5k
    ret = (xmlRegParserCtxtPtr) xmlMalloc(sizeof(xmlRegParserCtxt));
729
11.5k
    if (ret == NULL)
730
8
  return(NULL);
731
11.5k
    memset(ret, 0, sizeof(xmlRegParserCtxt));
732
11.5k
    if (string != NULL) {
733
6.74k
  ret->string = xmlStrdup(string);
734
6.74k
        if (ret->string == NULL) {
735
1
            xmlFree(ret);
736
1
            return(NULL);
737
1
        }
738
6.74k
    }
739
11.5k
    ret->cur = ret->string;
740
11.5k
    ret->neg = 0;
741
11.5k
    ret->negs = 0;
742
11.5k
    ret->error = 0;
743
11.5k
    ret->determinist = -1;
744
11.5k
    return(ret);
745
11.5k
}
746
747
/**
748
 * Allocate a new regexp range
749
 *
750
 * @param ctxt  the regexp parser context
751
 * @param neg  is that negative
752
 * @param type  the type of range
753
 * @param start  the start codepoint
754
 * @param end  the end codepoint
755
 * @returns the new range or NULL in case of error
756
 */
757
static xmlRegRangePtr
758
xmlRegNewRange(xmlRegParserCtxtPtr ctxt,
759
5.92k
         int neg, xmlRegAtomType type, int start, int end) {
760
5.92k
    xmlRegRangePtr ret;
761
762
5.92k
    ret = (xmlRegRangePtr) xmlMalloc(sizeof(xmlRegRange));
763
5.92k
    if (ret == NULL) {
764
1
  xmlRegexpErrMemory(ctxt);
765
1
  return(NULL);
766
1
    }
767
5.92k
    ret->neg = neg;
768
5.92k
    ret->type = type;
769
5.92k
    ret->start = start;
770
5.92k
    ret->end = end;
771
5.92k
    return(ret);
772
5.92k
}
773
774
/**
775
 * Free a regexp range
776
 *
777
 * @param range  the regexp range
778
 */
779
static void
780
5.92k
xmlRegFreeRange(xmlRegRangePtr range) {
781
5.92k
    if (range == NULL)
782
0
  return;
783
784
5.92k
    if (range->blockName != NULL)
785
98
  xmlFree(range->blockName);
786
5.92k
    xmlFree(range);
787
5.92k
}
788
789
/**
790
 * Copy a regexp range
791
 *
792
 * @param ctxt  regexp parser context
793
 * @param range  the regexp range
794
 * @returns the new copy or NULL in case of error.
795
 */
796
static xmlRegRangePtr
797
0
xmlRegCopyRange(xmlRegParserCtxtPtr ctxt, xmlRegRangePtr range) {
798
0
    xmlRegRangePtr ret;
799
800
0
    if (range == NULL)
801
0
  return(NULL);
802
803
0
    ret = xmlRegNewRange(ctxt, range->neg, range->type, range->start,
804
0
                         range->end);
805
0
    if (ret == NULL)
806
0
        return(NULL);
807
0
    if (range->blockName != NULL) {
808
0
  ret->blockName = xmlStrdup(range->blockName);
809
0
  if (ret->blockName == NULL) {
810
0
      xmlRegexpErrMemory(ctxt);
811
0
      xmlRegFreeRange(ret);
812
0
      return(NULL);
813
0
  }
814
0
    }
815
0
    return(ret);
816
0
}
817
818
/**
819
 * Allocate a new atom
820
 *
821
 * @param ctxt  the regexp parser context
822
 * @param type  the type of atom
823
 * @returns the new atom or NULL in case of error
824
 */
825
static xmlRegAtomPtr
826
33.9k
xmlRegNewAtom(xmlRegParserCtxtPtr ctxt, xmlRegAtomType type) {
827
33.9k
    xmlRegAtomPtr ret;
828
829
33.9k
    ret = (xmlRegAtomPtr) xmlMalloc(sizeof(xmlRegAtom));
830
33.9k
    if (ret == NULL) {
831
231
  xmlRegexpErrMemory(ctxt);
832
231
  return(NULL);
833
231
    }
834
33.6k
    memset(ret, 0, sizeof(xmlRegAtom));
835
33.6k
    ret->type = type;
836
33.6k
    ret->quant = XML_REGEXP_QUANT_ONCE;
837
33.6k
    ret->min = 0;
838
33.6k
    ret->max = 0;
839
33.6k
    return(ret);
840
33.9k
}
841
842
/**
843
 * Free a regexp atom
844
 *
845
 * @param atom  the regexp atom
846
 */
847
static void
848
34.0k
xmlRegFreeAtom(xmlRegAtomPtr atom) {
849
34.0k
    int i;
850
851
34.0k
    if (atom == NULL)
852
408
  return;
853
854
39.6k
    for (i = 0;i < atom->nbRanges;i++)
855
5.92k
  xmlRegFreeRange(atom->ranges[i]);
856
33.6k
    if (atom->ranges != NULL)
857
2.53k
  xmlFree(atom->ranges);
858
33.6k
    if ((atom->type == XML_REGEXP_STRING) && (atom->valuep != NULL))
859
0
  xmlFree(atom->valuep);
860
33.6k
    if ((atom->type == XML_REGEXP_STRING) && (atom->valuep2 != NULL))
861
0
  xmlFree(atom->valuep2);
862
33.6k
    if ((atom->type == XML_REGEXP_BLOCK_NAME) && (atom->valuep != NULL))
863
610
  xmlFree(atom->valuep);
864
33.6k
    xmlFree(atom);
865
33.6k
}
866
867
/**
868
 * Allocate a new regexp range
869
 *
870
 * @param ctxt  the regexp parser context
871
 * @param atom  the original atom
872
 * @returns the new atom or NULL in case of error
873
 */
874
static xmlRegAtomPtr
875
0
xmlRegCopyAtom(xmlRegParserCtxtPtr ctxt, xmlRegAtomPtr atom) {
876
0
    xmlRegAtomPtr ret;
877
878
0
    ret = (xmlRegAtomPtr) xmlMalloc(sizeof(xmlRegAtom));
879
0
    if (ret == NULL) {
880
0
  xmlRegexpErrMemory(ctxt);
881
0
  return(NULL);
882
0
    }
883
0
    memset(ret, 0, sizeof(xmlRegAtom));
884
0
    ret->type = atom->type;
885
0
    ret->quant = atom->quant;
886
0
    ret->min = atom->min;
887
0
    ret->max = atom->max;
888
0
    if (atom->nbRanges > 0) {
889
0
        int i;
890
891
0
        ret->ranges = (xmlRegRangePtr *) xmlMalloc(sizeof(xmlRegRangePtr) *
892
0
                                             atom->nbRanges);
893
0
  if (ret->ranges == NULL) {
894
0
      xmlRegexpErrMemory(ctxt);
895
0
      goto error;
896
0
  }
897
0
  for (i = 0;i < atom->nbRanges;i++) {
898
0
      ret->ranges[i] = xmlRegCopyRange(ctxt, atom->ranges[i]);
899
0
      if (ret->ranges[i] == NULL)
900
0
          goto error;
901
0
      ret->nbRanges = i + 1;
902
0
  }
903
0
    }
904
0
    return(ret);
905
906
0
error:
907
0
    xmlRegFreeAtom(ret);
908
0
    return(NULL);
909
0
}
910
911
static xmlRegStatePtr
912
67.4k
xmlRegNewState(xmlRegParserCtxtPtr ctxt) {
913
67.4k
    xmlRegStatePtr ret;
914
915
67.4k
    ret = (xmlRegStatePtr) xmlMalloc(sizeof(xmlRegState));
916
67.4k
    if (ret == NULL) {
917
27
  xmlRegexpErrMemory(ctxt);
918
27
  return(NULL);
919
27
    }
920
67.4k
    memset(ret, 0, sizeof(xmlRegState));
921
67.4k
    ret->type = XML_REGEXP_TRANS_STATE;
922
67.4k
    ret->mark = XML_REGEXP_MARK_NORMAL;
923
67.4k
    return(ret);
924
67.4k
}
925
926
/**
927
 * Free a regexp state
928
 *
929
 * @param state  the regexp state
930
 */
931
static void
932
89.6k
xmlRegFreeState(xmlRegStatePtr state) {
933
89.6k
    if (state == NULL)
934
22.2k
  return;
935
936
67.4k
    if (state->trans != NULL)
937
55.9k
  xmlFree(state->trans);
938
67.4k
    if (state->transTo != NULL)
939
55.8k
  xmlFree(state->transTo);
940
67.4k
    xmlFree(state);
941
67.4k
}
942
943
/**
944
 * Free a regexp parser context
945
 *
946
 * @param ctxt  the regexp parser context
947
 */
948
static void
949
11.5k
xmlRegFreeParserCtxt(xmlRegParserCtxtPtr ctxt) {
950
11.5k
    int i;
951
11.5k
    if (ctxt == NULL)
952
0
  return;
953
954
11.5k
    if (ctxt->string != NULL)
955
1.95k
  xmlFree(ctxt->string);
956
11.5k
    if (ctxt->states != NULL) {
957
22.1k
  for (i = 0;i < ctxt->nbStates;i++)
958
20.1k
      xmlRegFreeState(ctxt->states[i]);
959
1.95k
  xmlFree(ctxt->states);
960
1.95k
    }
961
11.5k
    if (ctxt->atoms != NULL) {
962
11.8k
  for (i = 0;i < ctxt->nbAtoms;i++)
963
10.3k
      xmlRegFreeAtom(ctxt->atoms[i]);
964
1.53k
  xmlFree(ctxt->atoms);
965
1.53k
    }
966
11.5k
    if (ctxt->counters != NULL)
967
154
  xmlFree(ctxt->counters);
968
11.5k
    xmlFree(ctxt);
969
11.5k
}
970
971
/************************************************************************
972
 *                  *
973
 *      Display of Data structures      *
974
 *                  *
975
 ************************************************************************/
976
977
#ifdef DEBUG_REGEXP
978
static void
979
xmlRegPrintAtomType(FILE *output, xmlRegAtomType type) {
980
    switch (type) {
981
        case XML_REGEXP_EPSILON:
982
      fprintf(output, "epsilon "); break;
983
        case XML_REGEXP_CHARVAL:
984
      fprintf(output, "charval "); break;
985
        case XML_REGEXP_RANGES:
986
      fprintf(output, "ranges "); break;
987
        case XML_REGEXP_SUBREG:
988
      fprintf(output, "subexpr "); break;
989
        case XML_REGEXP_STRING:
990
      fprintf(output, "string "); break;
991
        case XML_REGEXP_ANYCHAR:
992
      fprintf(output, "anychar "); break;
993
        case XML_REGEXP_ANYSPACE:
994
      fprintf(output, "anyspace "); break;
995
        case XML_REGEXP_NOTSPACE:
996
      fprintf(output, "notspace "); break;
997
        case XML_REGEXP_INITNAME:
998
      fprintf(output, "initname "); break;
999
        case XML_REGEXP_NOTINITNAME:
1000
      fprintf(output, "notinitname "); break;
1001
        case XML_REGEXP_NAMECHAR:
1002
      fprintf(output, "namechar "); break;
1003
        case XML_REGEXP_NOTNAMECHAR:
1004
      fprintf(output, "notnamechar "); break;
1005
        case XML_REGEXP_DECIMAL:
1006
      fprintf(output, "decimal "); break;
1007
        case XML_REGEXP_NOTDECIMAL:
1008
      fprintf(output, "notdecimal "); break;
1009
        case XML_REGEXP_REALCHAR:
1010
      fprintf(output, "realchar "); break;
1011
        case XML_REGEXP_NOTREALCHAR:
1012
      fprintf(output, "notrealchar "); break;
1013
        case XML_REGEXP_LETTER:
1014
            fprintf(output, "LETTER "); break;
1015
        case XML_REGEXP_LETTER_UPPERCASE:
1016
            fprintf(output, "LETTER_UPPERCASE "); break;
1017
        case XML_REGEXP_LETTER_LOWERCASE:
1018
            fprintf(output, "LETTER_LOWERCASE "); break;
1019
        case XML_REGEXP_LETTER_TITLECASE:
1020
            fprintf(output, "LETTER_TITLECASE "); break;
1021
        case XML_REGEXP_LETTER_MODIFIER:
1022
            fprintf(output, "LETTER_MODIFIER "); break;
1023
        case XML_REGEXP_LETTER_OTHERS:
1024
            fprintf(output, "LETTER_OTHERS "); break;
1025
        case XML_REGEXP_MARK:
1026
            fprintf(output, "MARK "); break;
1027
        case XML_REGEXP_MARK_NONSPACING:
1028
            fprintf(output, "MARK_NONSPACING "); break;
1029
        case XML_REGEXP_MARK_SPACECOMBINING:
1030
            fprintf(output, "MARK_SPACECOMBINING "); break;
1031
        case XML_REGEXP_MARK_ENCLOSING:
1032
            fprintf(output, "MARK_ENCLOSING "); break;
1033
        case XML_REGEXP_NUMBER:
1034
            fprintf(output, "NUMBER "); break;
1035
        case XML_REGEXP_NUMBER_DECIMAL:
1036
            fprintf(output, "NUMBER_DECIMAL "); break;
1037
        case XML_REGEXP_NUMBER_LETTER:
1038
            fprintf(output, "NUMBER_LETTER "); break;
1039
        case XML_REGEXP_NUMBER_OTHERS:
1040
            fprintf(output, "NUMBER_OTHERS "); break;
1041
        case XML_REGEXP_PUNCT:
1042
            fprintf(output, "PUNCT "); break;
1043
        case XML_REGEXP_PUNCT_CONNECTOR:
1044
            fprintf(output, "PUNCT_CONNECTOR "); break;
1045
        case XML_REGEXP_PUNCT_DASH:
1046
            fprintf(output, "PUNCT_DASH "); break;
1047
        case XML_REGEXP_PUNCT_OPEN:
1048
            fprintf(output, "PUNCT_OPEN "); break;
1049
        case XML_REGEXP_PUNCT_CLOSE:
1050
            fprintf(output, "PUNCT_CLOSE "); break;
1051
        case XML_REGEXP_PUNCT_INITQUOTE:
1052
            fprintf(output, "PUNCT_INITQUOTE "); break;
1053
        case XML_REGEXP_PUNCT_FINQUOTE:
1054
            fprintf(output, "PUNCT_FINQUOTE "); break;
1055
        case XML_REGEXP_PUNCT_OTHERS:
1056
            fprintf(output, "PUNCT_OTHERS "); break;
1057
        case XML_REGEXP_SEPAR:
1058
            fprintf(output, "SEPAR "); break;
1059
        case XML_REGEXP_SEPAR_SPACE:
1060
            fprintf(output, "SEPAR_SPACE "); break;
1061
        case XML_REGEXP_SEPAR_LINE:
1062
            fprintf(output, "SEPAR_LINE "); break;
1063
        case XML_REGEXP_SEPAR_PARA:
1064
            fprintf(output, "SEPAR_PARA "); break;
1065
        case XML_REGEXP_SYMBOL:
1066
            fprintf(output, "SYMBOL "); break;
1067
        case XML_REGEXP_SYMBOL_MATH:
1068
            fprintf(output, "SYMBOL_MATH "); break;
1069
        case XML_REGEXP_SYMBOL_CURRENCY:
1070
            fprintf(output, "SYMBOL_CURRENCY "); break;
1071
        case XML_REGEXP_SYMBOL_MODIFIER:
1072
            fprintf(output, "SYMBOL_MODIFIER "); break;
1073
        case XML_REGEXP_SYMBOL_OTHERS:
1074
            fprintf(output, "SYMBOL_OTHERS "); break;
1075
        case XML_REGEXP_OTHER:
1076
            fprintf(output, "OTHER "); break;
1077
        case XML_REGEXP_OTHER_CONTROL:
1078
            fprintf(output, "OTHER_CONTROL "); break;
1079
        case XML_REGEXP_OTHER_FORMAT:
1080
            fprintf(output, "OTHER_FORMAT "); break;
1081
        case XML_REGEXP_OTHER_PRIVATE:
1082
            fprintf(output, "OTHER_PRIVATE "); break;
1083
        case XML_REGEXP_OTHER_NA:
1084
            fprintf(output, "OTHER_NA "); break;
1085
        case XML_REGEXP_BLOCK_NAME:
1086
      fprintf(output, "BLOCK "); break;
1087
    }
1088
}
1089
1090
static void
1091
xmlRegPrintQuantType(FILE *output, xmlRegQuantType type) {
1092
    switch (type) {
1093
        case XML_REGEXP_QUANT_EPSILON:
1094
      fprintf(output, "epsilon "); break;
1095
        case XML_REGEXP_QUANT_ONCE:
1096
      fprintf(output, "once "); break;
1097
        case XML_REGEXP_QUANT_OPT:
1098
      fprintf(output, "? "); break;
1099
        case XML_REGEXP_QUANT_MULT:
1100
      fprintf(output, "* "); break;
1101
        case XML_REGEXP_QUANT_PLUS:
1102
      fprintf(output, "+ "); break;
1103
  case XML_REGEXP_QUANT_RANGE:
1104
      fprintf(output, "range "); break;
1105
  case XML_REGEXP_QUANT_ONCEONLY:
1106
      fprintf(output, "onceonly "); break;
1107
  case XML_REGEXP_QUANT_ALL:
1108
      fprintf(output, "all "); break;
1109
    }
1110
}
1111
static void
1112
xmlRegPrintRange(FILE *output, xmlRegRangePtr range) {
1113
    fprintf(output, "  range: ");
1114
    if (range->neg)
1115
  fprintf(output, "negative ");
1116
    xmlRegPrintAtomType(output, range->type);
1117
    fprintf(output, "%c - %c\n", range->start, range->end);
1118
}
1119
1120
static void
1121
xmlRegPrintAtom(FILE *output, xmlRegAtomPtr atom) {
1122
    fprintf(output, " atom: ");
1123
    if (atom == NULL) {
1124
  fprintf(output, "NULL\n");
1125
  return;
1126
    }
1127
    if (atom->neg)
1128
        fprintf(output, "not ");
1129
    xmlRegPrintAtomType(output, atom->type);
1130
    xmlRegPrintQuantType(output, atom->quant);
1131
    if (atom->quant == XML_REGEXP_QUANT_RANGE)
1132
  fprintf(output, "%d-%d ", atom->min, atom->max);
1133
    if (atom->type == XML_REGEXP_STRING)
1134
  fprintf(output, "'%s' ", (char *) atom->valuep);
1135
    if (atom->type == XML_REGEXP_CHARVAL)
1136
  fprintf(output, "char %c\n", atom->codepoint);
1137
    else if (atom->type == XML_REGEXP_RANGES) {
1138
  int i;
1139
  fprintf(output, "%d entries\n", atom->nbRanges);
1140
  for (i = 0; i < atom->nbRanges;i++)
1141
      xmlRegPrintRange(output, atom->ranges[i]);
1142
    } else {
1143
  fprintf(output, "\n");
1144
    }
1145
}
1146
1147
static void
1148
xmlRegPrintAtomCompact(FILE* output, xmlRegexpPtr regexp, int atom)
1149
{
1150
    if (output == NULL || regexp == NULL || atom < 0 || 
1151
        atom >= regexp->nbstrings) {
1152
        return;
1153
    }
1154
    fprintf(output, " atom: ");
1155
1156
    xmlRegPrintAtomType(output, XML_REGEXP_STRING);
1157
    xmlRegPrintQuantType(output, XML_REGEXP_QUANT_ONCE);
1158
    fprintf(output, "'%s' ", (char *) regexp->stringMap[atom]);
1159
    fprintf(output, "\n");
1160
}
1161
1162
static void
1163
xmlRegPrintTrans(FILE *output, xmlRegTransPtr trans) {
1164
    fprintf(output, "  trans: ");
1165
    if (trans == NULL) {
1166
  fprintf(output, "NULL\n");
1167
  return;
1168
    }
1169
    if (trans->to < 0) {
1170
  fprintf(output, "removed\n");
1171
  return;
1172
    }
1173
    if (trans->nd != 0) {
1174
  if (trans->nd == 2)
1175
      fprintf(output, "last not determinist, ");
1176
  else
1177
      fprintf(output, "not determinist, ");
1178
    }
1179
    if (trans->counter >= 0) {
1180
  fprintf(output, "counted %d, ", trans->counter);
1181
    }
1182
    if (trans->count == REGEXP_ALL_COUNTER) {
1183
  fprintf(output, "all transition, ");
1184
    } else if (trans->count >= 0) {
1185
  fprintf(output, "count based %d, ", trans->count);
1186
    }
1187
    if (trans->atom == NULL) {
1188
  fprintf(output, "epsilon to %d\n", trans->to);
1189
  return;
1190
    }
1191
    if (trans->atom->type == XML_REGEXP_CHARVAL)
1192
  fprintf(output, "char %c ", trans->atom->codepoint);
1193
    fprintf(output, "atom %d, to %d\n", trans->atom->no, trans->to);
1194
}
1195
1196
static void
1197
xmlRegPrintTransCompact(
1198
    FILE* output,
1199
    xmlRegexpPtr regexp,
1200
    int state,
1201
    int atom
1202
)
1203
{
1204
    int target;
1205
    if (output == NULL || regexp == NULL || regexp->compact == NULL || 
1206
        state < 0 || atom < 0) {
1207
        return;
1208
    }
1209
    target = regexp->compact[state * (regexp->nbstrings + 1) + atom + 1];
1210
    fprintf(output, "  trans: ");
1211
1212
    /* TODO maybe skip 'removed' transitions, because they actually never existed */
1213
    if (target < 0) {
1214
        fprintf(output, "removed\n");
1215
        return;
1216
    }
1217
1218
    /* We will ignore most of the attributes used in xmlRegPrintTrans,
1219
     * since the compact form is much simpler and uses only a part of the 
1220
     * features provided by the libxml2 regexp libary 
1221
     * (no rollbacks, counters etc.) */
1222
1223
    /* Compared to the standard representation, an automata written using the
1224
     * compact form will ALWAYS be deterministic! 
1225
     * From    xmlRegPrintTrans:
1226
         if (trans->nd != 0) {
1227
            ...
1228
      * trans->nd will always be 0! */
1229
1230
    /* In automata represented in compact form, the transitions will not use
1231
     * counters. 
1232
     * From    xmlRegPrintTrans:
1233
         if (trans->counter >= 0) {
1234
            ...
1235
     * regexp->counters == NULL, so trans->counter < 0 */
1236
1237
    /* In compact form, we won't use */
1238
1239
    /* An automata in the compact representation will always use string 
1240
     * atoms. 
1241
     * From    xmlRegPrintTrans:
1242
         if (trans->atom->type == XML_REGEXP_CHARVAL)
1243
             ...
1244
     * trans->atom != NULL && trans->atom->type == XML_REGEXP_STRING */
1245
1246
    fprintf(output, "atom %d, to %d\n", atom, target);
1247
}
1248
1249
static void
1250
xmlRegPrintState(FILE *output, xmlRegStatePtr state) {
1251
    int i;
1252
1253
    fprintf(output, " state: ");
1254
    if (state == NULL) {
1255
  fprintf(output, "NULL\n");
1256
  return;
1257
    }
1258
    if (state->type == XML_REGEXP_START_STATE)
1259
  fprintf(output, "START ");
1260
    if (state->type == XML_REGEXP_FINAL_STATE)
1261
  fprintf(output, "FINAL ");
1262
1263
    fprintf(output, "%d, %d transitions:\n", state->no, state->nbTrans);
1264
    for (i = 0;i < state->nbTrans; i++) {
1265
  xmlRegPrintTrans(output, &(state->trans[i]));
1266
    }
1267
}
1268
1269
static void
1270
xmlRegPrintStateCompact(FILE* output, xmlRegexpPtr regexp, int state)
1271
{
1272
    int nbTrans = 0;
1273
    int i;
1274
    int target;
1275
    xmlRegStateType stateType;
1276
1277
    if (output == NULL || regexp == NULL || regexp->compact == NULL ||
1278
        state < 0) {
1279
        return;
1280
    }
1281
    
1282
    fprintf(output, " state: ");
1283
1284
    stateType = regexp->compact[state * (regexp->nbstrings + 1)];
1285
    if (stateType == XML_REGEXP_START_STATE) {
1286
        fprintf(output, " START ");
1287
    }
1288
    
1289
    if (stateType == XML_REGEXP_FINAL_STATE) {
1290
        fprintf(output, " FINAL ");
1291
    }
1292
1293
    /* Print all atoms. */
1294
    for (i = 0; i < regexp->nbstrings; i++) {
1295
        xmlRegPrintAtomCompact(output, regexp, i);
1296
    }
1297
1298
    /* Count all the transitions from the compact representation. */
1299
    for (i = 0; i < regexp->nbstrings; i++) {
1300
        target = regexp->compact[state * (regexp->nbstrings + 1) + i + 1];
1301
        if (target > 0 && target <= regexp->nbstates && 
1302
            regexp->compact[(target - 1) * (regexp->nbstrings + 1)] == 
1303
            XML_REGEXP_SINK_STATE) {
1304
                nbTrans++;
1305
            }
1306
    }
1307
1308
    fprintf(output, "%d, %d transitions:\n", state, nbTrans);
1309
    
1310
    /* Print all transitions */
1311
    for (i = 0; i < regexp->nbstrings; i++) {
1312
        xmlRegPrintTransCompact(output, regexp, state, i);
1313
    }
1314
}
1315
1316
/*
1317
 * @param output  an output stream
1318
 * @param regexp  the regexp instance
1319
 * 
1320
 * Print the compact representation of a regexp, in the same fashion as the
1321
 * public #xmlRegexpPrint function.
1322
 */
1323
static void
1324
xmlRegPrintCompact(FILE* output, xmlRegexpPtr regexp)
1325
{
1326
    int i;
1327
    if (output == NULL || regexp == NULL || regexp->compact == NULL) {
1328
        return;
1329
    }
1330
    
1331
    fprintf(output, "'%s' ", regexp->string);
1332
1333
    fprintf(output, "%d atoms:\n", regexp->nbstrings);
1334
    fprintf(output, "\n");
1335
    for (i = 0; i < regexp->nbstrings; i++) {
1336
        fprintf(output, " %02d ", i);
1337
        xmlRegPrintAtomCompact(output, regexp, i);
1338
    }
1339
1340
    fprintf(output, "%d states:", regexp->nbstates);
1341
    fprintf(output, "\n");
1342
    for (i = 0; i < regexp->nbstates; i++) {
1343
        xmlRegPrintStateCompact(output, regexp, i);
1344
    }
1345
1346
    fprintf(output, "%d counters:\n", 0);
1347
}
1348
1349
static void
1350
xmlRegexpPrintInternal(FILE *output, xmlRegexpPtr regexp) {
1351
    int i;
1352
1353
    if (output == NULL)
1354
        return;
1355
    fprintf(output, " regexp: ");
1356
    if (regexp == NULL) {
1357
  fprintf(output, "NULL\n");
1358
  return;
1359
    }
1360
  if (regexp->compact) {
1361
    xmlRegPrintCompact(output, regexp);
1362
    return;
1363
  }
1364
1365
    fprintf(output, "'%s' ", regexp->string);
1366
    fprintf(output, "\n");
1367
    fprintf(output, "%d atoms:\n", regexp->nbAtoms);
1368
    for (i = 0;i < regexp->nbAtoms; i++) {
1369
  fprintf(output, " %02d ", i);
1370
  xmlRegPrintAtom(output, regexp->atoms[i]);
1371
    }
1372
    fprintf(output, "%d states:", regexp->nbStates);
1373
    fprintf(output, "\n");
1374
    for (i = 0;i < regexp->nbStates; i++) {
1375
  xmlRegPrintState(output, regexp->states[i]);
1376
    }
1377
    fprintf(output, "%d counters:\n", regexp->nbCounters);
1378
    for (i = 0;i < regexp->nbCounters; i++) {
1379
  fprintf(output, " %d: min %d max %d\n", i, regexp->counters[i].min,
1380
                                    regexp->counters[i].max);
1381
    }
1382
}
1383
#endif /* DEBUG_REGEXP */
1384
1385
/************************************************************************
1386
 *                  *
1387
 *     Finite Automata structures manipulations   *
1388
 *                  *
1389
 ************************************************************************/
1390
1391
static xmlRegRangePtr
1392
xmlRegAtomAddRange(xmlRegParserCtxtPtr ctxt, xmlRegAtomPtr atom,
1393
             int neg, xmlRegAtomType type, int start, int end,
1394
5.93k
       xmlChar *blockName) {
1395
5.93k
    xmlRegRangePtr range;
1396
1397
5.93k
    if (atom == NULL) {
1398
0
  ERROR("add range: atom is NULL");
1399
0
  return(NULL);
1400
0
    }
1401
5.93k
    if (atom->type != XML_REGEXP_RANGES) {
1402
0
  ERROR("add range: atom is not ranges");
1403
0
  return(NULL);
1404
0
    }
1405
5.93k
    if (atom->nbRanges >= atom->maxRanges) {
1406
3.98k
  xmlRegRangePtr *tmp;
1407
3.98k
        int newSize;
1408
1409
3.98k
        newSize = xmlGrowCapacity(atom->maxRanges, sizeof(tmp[0]),
1410
3.98k
                                  4, XML_MAX_ITEMS);
1411
3.98k
        if (newSize < 0) {
1412
0
      xmlRegexpErrMemory(ctxt);
1413
0
      return(NULL);
1414
0
        }
1415
3.98k
  tmp = xmlRealloc(atom->ranges, newSize * sizeof(tmp[0]));
1416
3.98k
  if (tmp == NULL) {
1417
6
      xmlRegexpErrMemory(ctxt);
1418
6
      return(NULL);
1419
6
  }
1420
3.98k
  atom->ranges = tmp;
1421
3.98k
  atom->maxRanges = newSize;
1422
3.98k
    }
1423
5.92k
    range = xmlRegNewRange(ctxt, neg, type, start, end);
1424
5.92k
    if (range == NULL)
1425
1
  return(NULL);
1426
5.92k
    range->blockName = blockName;
1427
5.92k
    atom->ranges[atom->nbRanges++] = range;
1428
1429
5.92k
    return(range);
1430
5.92k
}
1431
1432
static int
1433
3.09k
xmlRegGetCounter(xmlRegParserCtxtPtr ctxt) {
1434
3.09k
    if (ctxt->nbCounters >= ctxt->maxCounters) {
1435
2.29k
  xmlRegCounter *tmp;
1436
2.29k
        int newSize;
1437
1438
2.29k
        newSize = xmlGrowCapacity(ctxt->maxCounters, sizeof(tmp[0]),
1439
2.29k
                                  4, XML_MAX_ITEMS);
1440
2.29k
  if (newSize < 0) {
1441
0
      xmlRegexpErrMemory(ctxt);
1442
0
      return(-1);
1443
0
  }
1444
2.29k
  tmp = xmlRealloc(ctxt->counters, newSize * sizeof(tmp[0]));
1445
2.29k
  if (tmp == NULL) {
1446
4
      xmlRegexpErrMemory(ctxt);
1447
4
      return(-1);
1448
4
  }
1449
2.29k
  ctxt->counters = tmp;
1450
2.29k
  ctxt->maxCounters = newSize;
1451
2.29k
    }
1452
3.09k
    ctxt->counters[ctxt->nbCounters].min = -1;
1453
3.09k
    ctxt->counters[ctxt->nbCounters].max = -1;
1454
3.09k
    return(ctxt->nbCounters++);
1455
3.09k
}
1456
1457
static int
1458
33.4k
xmlRegAtomPush(xmlRegParserCtxtPtr ctxt, xmlRegAtomPtr atom) {
1459
33.4k
    if (atom == NULL) {
1460
0
  ERROR("atom push: atom is NULL");
1461
0
  return(-1);
1462
0
    }
1463
33.4k
    if (ctxt->nbAtoms >= ctxt->maxAtoms) {
1464
19.3k
  xmlRegAtomPtr *tmp;
1465
19.3k
        int newSize;
1466
1467
19.3k
        newSize = xmlGrowCapacity(ctxt->maxAtoms, sizeof(tmp[0]),
1468
19.3k
                                  4, XML_MAX_ITEMS);
1469
19.3k
  if (newSize < 0) {
1470
0
      xmlRegexpErrMemory(ctxt);
1471
0
      return(-1);
1472
0
  }
1473
19.3k
  tmp = xmlRealloc(ctxt->atoms, newSize * sizeof(tmp[0]));
1474
19.3k
  if (tmp == NULL) {
1475
17
      xmlRegexpErrMemory(ctxt);
1476
17
      return(-1);
1477
17
  }
1478
19.3k
  ctxt->atoms = tmp;
1479
19.3k
        ctxt->maxAtoms = newSize;
1480
19.3k
    }
1481
33.4k
    atom->no = ctxt->nbAtoms;
1482
33.4k
    ctxt->atoms[ctxt->nbAtoms++] = atom;
1483
33.4k
    return(0);
1484
33.4k
}
1485
1486
static void
1487
xmlRegStateAddTransTo(xmlRegParserCtxtPtr ctxt, xmlRegStatePtr target,
1488
247k
                      int from) {
1489
247k
    if (target->nbTransTo >= target->maxTransTo) {
1490
136k
  int *tmp;
1491
136k
        int newSize;
1492
1493
136k
        newSize = xmlGrowCapacity(target->maxTransTo, sizeof(tmp[0]),
1494
136k
                                  8, XML_MAX_ITEMS);
1495
136k
  if (newSize < 0) {
1496
0
      xmlRegexpErrMemory(ctxt);
1497
0
      return;
1498
0
  }
1499
136k
  tmp = xmlRealloc(target->transTo, newSize * sizeof(tmp[0]));
1500
136k
  if (tmp == NULL) {
1501
76
      xmlRegexpErrMemory(ctxt);
1502
76
      return;
1503
76
  }
1504
136k
  target->transTo = tmp;
1505
136k
  target->maxTransTo = newSize;
1506
136k
    }
1507
247k
    target->transTo[target->nbTransTo] = from;
1508
247k
    target->nbTransTo++;
1509
247k
}
1510
1511
static void
1512
xmlRegStateAddTrans(xmlRegParserCtxtPtr ctxt, xmlRegStatePtr state,
1513
              xmlRegAtomPtr atom, xmlRegStatePtr target,
1514
271k
        int counter, int count) {
1515
1516
271k
    int nrtrans;
1517
1518
271k
    if (state == NULL) {
1519
0
  ERROR("add state: state is NULL");
1520
0
  return;
1521
0
    }
1522
271k
    if (target == NULL) {
1523
68
  ERROR("add state: target is NULL");
1524
68
  return;
1525
68
    }
1526
    /*
1527
     * Other routines follow the philosophy 'When in doubt, add a transition'
1528
     * so we check here whether such a transition is already present and, if
1529
     * so, silently ignore this request.
1530
     */
1531
1532
78.8M
    for (nrtrans = state->nbTrans - 1; nrtrans >= 0; nrtrans--) {
1533
78.6M
  xmlRegTransPtr trans = &(state->trans[nrtrans]);
1534
78.6M
  if ((trans->atom == atom) &&
1535
78.6M
      (trans->to == target->no) &&
1536
78.6M
      (trans->counter == counter) &&
1537
78.6M
      (trans->count == count)) {
1538
23.1k
      return;
1539
23.1k
  }
1540
78.6M
    }
1541
1542
248k
    if (state->nbTrans >= state->maxTrans) {
1543
129k
  xmlRegTrans *tmp;
1544
129k
        int newSize;
1545
1546
129k
        newSize = xmlGrowCapacity(state->maxTrans, sizeof(tmp[0]),
1547
129k
                                  8, XML_MAX_ITEMS);
1548
129k
  if (newSize < 0) {
1549
0
      xmlRegexpErrMemory(ctxt);
1550
0
      return;
1551
0
  }
1552
129k
  tmp = xmlRealloc(state->trans, newSize * sizeof(tmp[0]));
1553
129k
  if (tmp == NULL) {
1554
69
      xmlRegexpErrMemory(ctxt);
1555
69
      return;
1556
69
  }
1557
129k
  state->trans = tmp;
1558
129k
  state->maxTrans = newSize;
1559
129k
    }
1560
1561
247k
    state->trans[state->nbTrans].atom = atom;
1562
247k
    state->trans[state->nbTrans].to = target->no;
1563
247k
    state->trans[state->nbTrans].counter = counter;
1564
247k
    state->trans[state->nbTrans].count = count;
1565
247k
    state->trans[state->nbTrans].nd = 0;
1566
247k
    state->nbTrans++;
1567
247k
    xmlRegStateAddTransTo(ctxt, target, state->no);
1568
247k
}
1569
1570
static xmlRegStatePtr
1571
67.4k
xmlRegStatePush(xmlRegParserCtxtPtr ctxt) {
1572
67.4k
    xmlRegStatePtr state;
1573
1574
67.4k
    if (ctxt->nbStates >= ctxt->maxStates) {
1575
33.7k
  xmlRegStatePtr *tmp;
1576
33.7k
        int newSize;
1577
1578
33.7k
        newSize = xmlGrowCapacity(ctxt->maxStates, sizeof(tmp[0]),
1579
33.7k
                                  4, XML_MAX_ITEMS);
1580
33.7k
  if (newSize < 0) {
1581
0
      xmlRegexpErrMemory(ctxt);
1582
0
      return(NULL);
1583
0
  }
1584
33.7k
  tmp = xmlRealloc(ctxt->states, newSize * sizeof(tmp[0]));
1585
33.7k
  if (tmp == NULL) {
1586
26
      xmlRegexpErrMemory(ctxt);
1587
26
      return(NULL);
1588
26
  }
1589
33.7k
  ctxt->states = tmp;
1590
33.7k
  ctxt->maxStates = newSize;
1591
33.7k
    }
1592
1593
67.4k
    state = xmlRegNewState(ctxt);
1594
67.4k
    if (state == NULL)
1595
27
        return(NULL);
1596
1597
67.4k
    state->no = ctxt->nbStates;
1598
67.4k
    ctxt->states[ctxt->nbStates++] = state;
1599
1600
67.4k
    return(state);
1601
67.4k
}
1602
1603
/**
1604
 * @param ctxt  a regexp parser context
1605
 * @param from  the from state
1606
 * @param to  the target state or NULL for building a new one
1607
 * @param lax  
1608
 */
1609
static int
1610
xmlFAGenerateAllTransition(xmlRegParserCtxtPtr ctxt,
1611
         xmlRegStatePtr from, xmlRegStatePtr to,
1612
0
         int lax) {
1613
0
    if (to == NULL) {
1614
0
  to = xmlRegStatePush(ctxt);
1615
0
        if (to == NULL)
1616
0
            return(-1);
1617
0
  ctxt->state = to;
1618
0
    }
1619
0
    if (lax)
1620
0
  xmlRegStateAddTrans(ctxt, from, NULL, to, -1, REGEXP_ALL_LAX_COUNTER);
1621
0
    else
1622
0
  xmlRegStateAddTrans(ctxt, from, NULL, to, -1, REGEXP_ALL_COUNTER);
1623
0
    return(0);
1624
0
}
1625
1626
/**
1627
 * @param ctxt  a regexp parser context
1628
 * @param from  the from state
1629
 * @param to  the target state or NULL for building a new one
1630
 */
1631
static int
1632
xmlFAGenerateEpsilonTransition(xmlRegParserCtxtPtr ctxt,
1633
42.8k
             xmlRegStatePtr from, xmlRegStatePtr to) {
1634
42.8k
    if (to == NULL) {
1635
24.8k
  to = xmlRegStatePush(ctxt);
1636
24.8k
        if (to == NULL)
1637
28
            return(-1);
1638
24.7k
  ctxt->state = to;
1639
24.7k
    }
1640
42.8k
    xmlRegStateAddTrans(ctxt, from, NULL, to, -1, -1);
1641
42.8k
    return(0);
1642
42.8k
}
1643
1644
/**
1645
 * @param ctxt  a regexp parser context
1646
 * @param from  the from state
1647
 * @param to  the target state or NULL for building a new one
1648
 * @param counter  the counter for that transition
1649
 */
1650
static int
1651
xmlFAGenerateCountedEpsilonTransition(xmlRegParserCtxtPtr ctxt,
1652
3.09k
      xmlRegStatePtr from, xmlRegStatePtr to, int counter) {
1653
3.09k
    if (to == NULL) {
1654
0
  to = xmlRegStatePush(ctxt);
1655
0
        if (to == NULL)
1656
0
            return(-1);
1657
0
  ctxt->state = to;
1658
0
    }
1659
3.09k
    xmlRegStateAddTrans(ctxt, from, NULL, to, counter, -1);
1660
3.09k
    return(0);
1661
3.09k
}
1662
1663
/**
1664
 * @param ctxt  a regexp parser context
1665
 * @param from  the from state
1666
 * @param to  the target state or NULL for building a new one
1667
 * @param counter  the counter for that transition
1668
 */
1669
static int
1670
xmlFAGenerateCountedTransition(xmlRegParserCtxtPtr ctxt,
1671
3.09k
      xmlRegStatePtr from, xmlRegStatePtr to, int counter) {
1672
3.09k
    if (to == NULL) {
1673
0
  to = xmlRegStatePush(ctxt);
1674
0
        if (to == NULL)
1675
0
            return(-1);
1676
0
  ctxt->state = to;
1677
0
    }
1678
3.09k
    xmlRegStateAddTrans(ctxt, from, NULL, to, -1, counter);
1679
3.09k
    return(0);
1680
3.09k
}
1681
1682
/**
1683
 * @param ctxt  a regexp parser context
1684
 * @param from  the from state
1685
 * @param to  the target state or NULL for building a new one
1686
 * @param atom  the atom generating the transition
1687
 * @returns 0 if success and -1 in case of error.
1688
 */
1689
static int
1690
xmlFAGenerateTransitions(xmlRegParserCtxtPtr ctxt, xmlRegStatePtr from,
1691
34.0k
                   xmlRegStatePtr to, xmlRegAtomPtr atom) {
1692
34.0k
    xmlRegStatePtr end;
1693
34.0k
    int nullable = 0;
1694
1695
34.0k
    if (atom == NULL) {
1696
408
  ERROR("generate transition: atom == NULL");
1697
408
  return(-1);
1698
408
    }
1699
33.6k
    if (atom->type == XML_REGEXP_SUBREG) {
1700
  /*
1701
   * this is a subexpression handling one should not need to
1702
   * create a new node except for XML_REGEXP_QUANT_RANGE.
1703
   */
1704
9.86k
  if ((to != NULL) && (atom->stop != to) &&
1705
9.86k
      (atom->quant != XML_REGEXP_QUANT_RANGE)) {
1706
      /*
1707
       * Generate an epsilon transition to link to the target
1708
       */
1709
449
      xmlFAGenerateEpsilonTransition(ctxt, atom->stop, to);
1710
#ifdef DV
1711
  } else if ((to == NULL) && (atom->quant != XML_REGEXP_QUANT_RANGE) &&
1712
       (atom->quant != XML_REGEXP_QUANT_ONCE)) {
1713
      to = xmlRegStatePush(ctxt, to);
1714
            if (to == NULL)
1715
                return(-1);
1716
      ctxt->state = to;
1717
      xmlFAGenerateEpsilonTransition(ctxt, atom->stop, to);
1718
#endif
1719
449
  }
1720
9.86k
  switch (atom->quant) {
1721
258
      case XML_REGEXP_QUANT_OPT:
1722
258
    atom->quant = XML_REGEXP_QUANT_ONCE;
1723
    /*
1724
     * transition done to the state after end of atom.
1725
     *      1. set transition from atom start to new state
1726
     *      2. set transition from atom end to this state.
1727
     */
1728
258
                if (to == NULL) {
1729
85
                    xmlFAGenerateEpsilonTransition(ctxt, atom->start, 0);
1730
85
                    xmlFAGenerateEpsilonTransition(ctxt, atom->stop,
1731
85
                                                   ctxt->state);
1732
173
                } else {
1733
173
                    xmlFAGenerateEpsilonTransition(ctxt, atom->start, to);
1734
173
                }
1735
258
    break;
1736
187
      case XML_REGEXP_QUANT_MULT:
1737
187
    atom->quant = XML_REGEXP_QUANT_ONCE;
1738
187
    xmlFAGenerateEpsilonTransition(ctxt, atom->start, atom->stop);
1739
187
    xmlFAGenerateEpsilonTransition(ctxt, atom->stop, atom->start);
1740
187
    break;
1741
161
      case XML_REGEXP_QUANT_PLUS:
1742
161
    atom->quant = XML_REGEXP_QUANT_ONCE;
1743
161
    xmlFAGenerateEpsilonTransition(ctxt, atom->stop, atom->start);
1744
161
    break;
1745
3.09k
      case XML_REGEXP_QUANT_RANGE: {
1746
3.09k
    int counter;
1747
3.09k
    xmlRegStatePtr inter, newstate;
1748
1749
    /*
1750
     * create the final state now if needed
1751
     */
1752
3.09k
    if (to != NULL) {
1753
119
        newstate = to;
1754
2.97k
    } else {
1755
2.97k
        newstate = xmlRegStatePush(ctxt);
1756
2.97k
                    if (newstate == NULL)
1757
2
                        return(-1);
1758
2.97k
    }
1759
1760
    /*
1761
     * The principle here is to use counted transition
1762
     * to avoid explosion in the number of states in the
1763
     * graph. This is clearly more complex but should not
1764
     * be exploitable at runtime.
1765
     */
1766
3.09k
    if ((atom->min == 0) && (atom->start0 == NULL)) {
1767
0
        xmlRegAtomPtr copy;
1768
        /*
1769
         * duplicate a transition based on atom to count next
1770
         * occurrences after 1. We cannot loop to atom->start
1771
         * directly because we need an epsilon transition to
1772
         * newstate.
1773
         */
1774
         /* ???? For some reason it seems we never reach that
1775
            case, I suppose this got optimized out before when
1776
      building the automata */
1777
0
        copy = xmlRegCopyAtom(ctxt, atom);
1778
0
        if (copy == NULL)
1779
0
            return(-1);
1780
0
        copy->quant = XML_REGEXP_QUANT_ONCE;
1781
0
        copy->min = 0;
1782
0
        copy->max = 0;
1783
1784
0
        if (xmlFAGenerateTransitions(ctxt, atom->start, NULL, copy)
1785
0
            < 0) {
1786
0
                        xmlRegFreeAtom(copy);
1787
0
      return(-1);
1788
0
                    }
1789
0
        inter = ctxt->state;
1790
0
        counter = xmlRegGetCounter(ctxt);
1791
0
                    if (counter < 0)
1792
0
                        return(-1);
1793
0
        ctxt->counters[counter].min = atom->min - 1;
1794
0
        ctxt->counters[counter].max = atom->max - 1;
1795
        /* count the number of times we see it again */
1796
0
        xmlFAGenerateCountedEpsilonTransition(ctxt, inter,
1797
0
               atom->stop, counter);
1798
        /* allow a way out based on the count */
1799
0
        xmlFAGenerateCountedTransition(ctxt, inter,
1800
0
                                 newstate, counter);
1801
        /* and also allow a direct exit for 0 */
1802
0
        xmlFAGenerateEpsilonTransition(ctxt, atom->start,
1803
0
                                       newstate);
1804
3.09k
    } else {
1805
        /*
1806
         * either we need the atom at least once or there
1807
         * is an atom->start0 allowing to easily plug the
1808
         * epsilon transition.
1809
         */
1810
3.09k
        counter = xmlRegGetCounter(ctxt);
1811
3.09k
                    if (counter < 0)
1812
4
                        return(-1);
1813
3.09k
        ctxt->counters[counter].min = atom->min - 1;
1814
3.09k
        ctxt->counters[counter].max = atom->max - 1;
1815
        /* allow a way out based on the count */
1816
3.09k
        xmlFAGenerateCountedTransition(ctxt, atom->stop,
1817
3.09k
                                 newstate, counter);
1818
        /* count the number of times we see it again */
1819
3.09k
        xmlFAGenerateCountedEpsilonTransition(ctxt, atom->stop,
1820
3.09k
               atom->start, counter);
1821
        /* and if needed allow a direct exit for 0 */
1822
3.09k
        if (atom->min == 0)
1823
2.28k
      xmlFAGenerateEpsilonTransition(ctxt, atom->start0,
1824
2.28k
                   newstate);
1825
1826
3.09k
    }
1827
3.09k
    atom->min = 0;
1828
3.09k
    atom->max = 0;
1829
3.09k
    atom->quant = XML_REGEXP_QUANT_ONCE;
1830
3.09k
    ctxt->state = newstate;
1831
3.09k
      }
1832
9.25k
      default:
1833
9.25k
    break;
1834
9.86k
  }
1835
9.85k
        atom->start = NULL;
1836
9.85k
        atom->start0 = NULL;
1837
9.85k
        atom->stop = NULL;
1838
9.85k
  if (xmlRegAtomPush(ctxt, atom) < 0)
1839
9
      return(-1);
1840
9.84k
  return(0);
1841
9.85k
    }
1842
23.8k
    if ((atom->min == 0) && (atom->max == 0) &&
1843
23.8k
               (atom->quant == XML_REGEXP_QUANT_RANGE)) {
1844
        /*
1845
   * we can discard the atom and generate an epsilon transition instead
1846
   */
1847
181
  if (to == NULL) {
1848
114
      to = xmlRegStatePush(ctxt);
1849
114
      if (to == NULL)
1850
1
    return(-1);
1851
114
  }
1852
180
  xmlFAGenerateEpsilonTransition(ctxt, from, to);
1853
180
  ctxt->state = to;
1854
180
  xmlRegFreeAtom(atom);
1855
180
  return(0);
1856
181
    }
1857
23.6k
    if (to == NULL) {
1858
21.3k
  to = xmlRegStatePush(ctxt);
1859
21.3k
  if (to == NULL)
1860
10
      return(-1);
1861
21.3k
    }
1862
23.6k
    end = to;
1863
23.6k
    if ((atom->quant == XML_REGEXP_QUANT_MULT) ||
1864
23.6k
        (atom->quant == XML_REGEXP_QUANT_PLUS)) {
1865
  /*
1866
   * Do not pollute the target state by adding transitions from
1867
   * it as it is likely to be the shared target of multiple branches.
1868
   * So isolate with an epsilon transition.
1869
   */
1870
6.65k
        xmlRegStatePtr tmp;
1871
1872
6.65k
  tmp = xmlRegStatePush(ctxt);
1873
6.65k
        if (tmp == NULL)
1874
1
      return(-1);
1875
6.65k
  xmlFAGenerateEpsilonTransition(ctxt, tmp, to);
1876
6.65k
  to = tmp;
1877
6.65k
    }
1878
23.6k
    if ((atom->quant == XML_REGEXP_QUANT_RANGE) &&
1879
23.6k
        (atom->min == 0) && (atom->max > 0)) {
1880
174
  nullable = 1;
1881
174
  atom->min = 1;
1882
174
        if (atom->max == 1)
1883
67
      atom->quant = XML_REGEXP_QUANT_OPT;
1884
174
    }
1885
23.6k
    xmlRegStateAddTrans(ctxt, from, atom, to, -1, -1);
1886
23.6k
    ctxt->state = end;
1887
23.6k
    switch (atom->quant) {
1888
698
  case XML_REGEXP_QUANT_OPT:
1889
698
      atom->quant = XML_REGEXP_QUANT_ONCE;
1890
698
      xmlFAGenerateEpsilonTransition(ctxt, from, to);
1891
698
      break;
1892
6.19k
  case XML_REGEXP_QUANT_MULT:
1893
6.19k
      atom->quant = XML_REGEXP_QUANT_ONCE;
1894
6.19k
      xmlFAGenerateEpsilonTransition(ctxt, from, to);
1895
6.19k
      xmlRegStateAddTrans(ctxt, to, atom, to, -1, -1);
1896
6.19k
      break;
1897
458
  case XML_REGEXP_QUANT_PLUS:
1898
458
      atom->quant = XML_REGEXP_QUANT_ONCE;
1899
458
      xmlRegStateAddTrans(ctxt, to, atom, to, -1, -1);
1900
458
      break;
1901
205
  case XML_REGEXP_QUANT_RANGE:
1902
205
      if (nullable)
1903
107
    xmlFAGenerateEpsilonTransition(ctxt, from, to);
1904
205
      break;
1905
16.0k
  default:
1906
16.0k
      break;
1907
23.6k
    }
1908
23.6k
    if (xmlRegAtomPush(ctxt, atom) < 0)
1909
8
  return(-1);
1910
23.6k
    return(0);
1911
23.6k
}
1912
1913
/**
1914
 * @param ctxt  a regexp parser context
1915
 * @param fromnr  the from state
1916
 * @param tonr  the to state
1917
 * @param counter  should that transition be associated to a counted
1918
 */
1919
static void
1920
xmlFAReduceEpsilonTransitions(xmlRegParserCtxtPtr ctxt, int fromnr,
1921
38.8k
                        int tonr, int counter) {
1922
38.8k
    int transnr;
1923
38.8k
    xmlRegStatePtr from;
1924
38.8k
    xmlRegStatePtr to;
1925
1926
38.8k
    from = ctxt->states[fromnr];
1927
38.8k
    if (from == NULL)
1928
0
  return;
1929
38.8k
    to = ctxt->states[tonr];
1930
38.8k
    if (to == NULL)
1931
79
  return;
1932
38.7k
    if ((to->mark == XML_REGEXP_MARK_START) ||
1933
38.7k
  (to->mark == XML_REGEXP_MARK_VISITED))
1934
2.66k
  return;
1935
1936
36.0k
    to->mark = XML_REGEXP_MARK_VISITED;
1937
36.0k
    if (to->type == XML_REGEXP_FINAL_STATE) {
1938
3.42k
  from->type = XML_REGEXP_FINAL_STATE;
1939
3.42k
    }
1940
235k
    for (transnr = 0;transnr < to->nbTrans;transnr++) {
1941
199k
        xmlRegTransPtr t1 = &to->trans[transnr];
1942
199k
        int tcounter;
1943
1944
199k
        if (t1->to < 0)
1945
55.5k
      continue;
1946
144k
        if (t1->counter >= 0) {
1947
            /* assert(counter < 0); */
1948
37.7k
            tcounter = t1->counter;
1949
106k
        } else {
1950
106k
            tcounter = counter;
1951
106k
        }
1952
144k
  if (t1->atom == NULL) {
1953
      /*
1954
       * Don't remove counted transitions
1955
       * Don't loop either
1956
       */
1957
34.8k
      if (t1->to != fromnr) {
1958
32.2k
    if (t1->count >= 0) {
1959
13.2k
        xmlRegStateAddTrans(ctxt, from, NULL, ctxt->states[t1->to],
1960
13.2k
          -1, t1->count);
1961
18.9k
    } else {
1962
18.9k
                    xmlFAReduceEpsilonTransitions(ctxt, fromnr, t1->to,
1963
18.9k
                                                  tcounter);
1964
18.9k
    }
1965
32.2k
      }
1966
109k
  } else {
1967
109k
            xmlRegStateAddTrans(ctxt, from, t1->atom,
1968
109k
                                ctxt->states[t1->to], tcounter, -1);
1969
109k
  }
1970
144k
    }
1971
36.0k
}
1972
1973
/**
1974
 * @param ctxt  a regexp parser context
1975
 * @param tonr  the to state
1976
 */
1977
static void
1978
54.6k
xmlFAFinishReduceEpsilonTransitions(xmlRegParserCtxtPtr ctxt, int tonr) {
1979
54.6k
    int transnr;
1980
54.6k
    xmlRegStatePtr to;
1981
1982
54.6k
    to = ctxt->states[tonr];
1983
54.6k
    if (to == NULL)
1984
79
  return;
1985
54.5k
    if ((to->mark == XML_REGEXP_MARK_START) ||
1986
54.5k
  (to->mark == XML_REGEXP_MARK_NORMAL))
1987
18.4k
  return;
1988
1989
36.0k
    to->mark = XML_REGEXP_MARK_NORMAL;
1990
235k
    for (transnr = 0;transnr < to->nbTrans;transnr++) {
1991
199k
  xmlRegTransPtr t1 = &to->trans[transnr];
1992
199k
  if ((t1->to >= 0) && (t1->atom == NULL))
1993
34.8k
            xmlFAFinishReduceEpsilonTransitions(ctxt, t1->to);
1994
199k
    }
1995
36.0k
}
1996
1997
/**
1998
 * Eliminating general epsilon transitions can get costly in the general
1999
 * algorithm due to the large amount of generated new transitions and
2000
 * associated comparisons. However for simple epsilon transition used just
2001
 * to separate building blocks when generating the automata this can be
2002
 * reduced to state elimination:
2003
 *    - if there exists an epsilon from X to Y
2004
 *    - if there is no other transition from X
2005
 * then X and Y are semantically equivalent and X can be eliminated
2006
 * If X is the start state then make Y the start state, else replace the
2007
 * target of all transitions to X by transitions to Y.
2008
 *
2009
 * If X is a final state, skip it.
2010
 * Otherwise it would be necessary to manipulate counters for this case when
2011
 * eliminating state 2:
2012
 * State 1 has a transition with an atom to state 2.
2013
 * State 2 is final and has an epsilon transition to state 1.
2014
 *
2015
 * @param ctxt  a regexp parser context
2016
 */
2017
static void
2018
4.87k
xmlFAEliminateSimpleEpsilonTransitions(xmlRegParserCtxtPtr ctxt) {
2019
4.87k
    int statenr, i, j, newto;
2020
4.87k
    xmlRegStatePtr state, tmp;
2021
2022
50.4k
    for (statenr = 0;statenr < ctxt->nbStates;statenr++) {
2023
45.5k
  state = ctxt->states[statenr];
2024
45.5k
  if (state == NULL)
2025
0
      continue;
2026
45.5k
  if (state->nbTrans != 1)
2027
22.6k
      continue;
2028
22.9k
       if (state->type == XML_REGEXP_UNREACH_STATE ||
2029
22.9k
           state->type == XML_REGEXP_FINAL_STATE)
2030
86
      continue;
2031
  /* is the only transition out a basic transition */
2032
22.8k
  if ((state->trans[0].atom == NULL) &&
2033
22.8k
      (state->trans[0].to >= 0) &&
2034
22.8k
      (state->trans[0].to != statenr) &&
2035
22.8k
      (state->trans[0].counter < 0) &&
2036
22.8k
      (state->trans[0].count < 0)) {
2037
13.7k
      newto = state->trans[0].to;
2038
2039
13.7k
            if (state->type == XML_REGEXP_START_STATE) {
2040
12.6k
            } else {
2041
92.0k
          for (i = 0;i < state->nbTransTo;i++) {
2042
79.3k
        tmp = ctxt->states[state->transTo[i]];
2043
78.2M
        for (j = 0;j < tmp->nbTrans;j++) {
2044
78.2M
      if (tmp->trans[j].to == statenr) {
2045
69.3k
          tmp->trans[j].to = -1;
2046
69.3k
          xmlRegStateAddTrans(ctxt, tmp, tmp->trans[j].atom,
2047
69.3k
            ctxt->states[newto],
2048
69.3k
                  tmp->trans[j].counter,
2049
69.3k
            tmp->trans[j].count);
2050
69.3k
      }
2051
78.2M
        }
2052
79.3k
    }
2053
12.6k
    if (state->type == XML_REGEXP_FINAL_STATE)
2054
0
        ctxt->states[newto]->type = XML_REGEXP_FINAL_STATE;
2055
    /* eliminate the transition completely */
2056
12.6k
    state->nbTrans = 0;
2057
2058
12.6k
                state->type = XML_REGEXP_UNREACH_STATE;
2059
2060
12.6k
      }
2061
2062
13.7k
  }
2063
22.8k
    }
2064
4.87k
}
2065
/**
2066
 * @param ctxt  a regexp parser context
2067
 */
2068
static void
2069
4.87k
xmlFAEliminateEpsilonTransitions(xmlRegParserCtxtPtr ctxt) {
2070
4.87k
    int statenr, transnr;
2071
4.87k
    xmlRegStatePtr state;
2072
4.87k
    int has_epsilon;
2073
2074
4.87k
    if (ctxt->states == NULL) return;
2075
2076
    /*
2077
     * Eliminate simple epsilon transition and the associated unreachable
2078
     * states.
2079
     */
2080
4.87k
    xmlFAEliminateSimpleEpsilonTransitions(ctxt);
2081
50.4k
    for (statenr = 0;statenr < ctxt->nbStates;statenr++) {
2082
45.5k
  state = ctxt->states[statenr];
2083
45.5k
  if ((state != NULL) && (state->type == XML_REGEXP_UNREACH_STATE)) {
2084
12.6k
      xmlRegFreeState(state);
2085
12.6k
      ctxt->states[statenr] = NULL;
2086
12.6k
  }
2087
45.5k
    }
2088
2089
4.87k
    has_epsilon = 0;
2090
2091
    /*
2092
     * Build the completed transitions bypassing the epsilons
2093
     * Use a marking algorithm to avoid loops
2094
     * Mark sink states too.
2095
     * Process from the latest states backward to the start when
2096
     * there is long cascading epsilon chains this minimize the
2097
     * recursions and transition compares when adding the new ones
2098
     */
2099
50.4k
    for (statenr = ctxt->nbStates - 1;statenr >= 0;statenr--) {
2100
45.5k
  state = ctxt->states[statenr];
2101
45.5k
  if (state == NULL)
2102
12.6k
      continue;
2103
32.9k
  if ((state->nbTrans == 0) &&
2104
32.9k
      (state->type != XML_REGEXP_FINAL_STATE)) {
2105
0
      state->type = XML_REGEXP_SINK_STATE;
2106
0
  }
2107
250k
  for (transnr = 0;transnr < state->nbTrans;transnr++) {
2108
217k
      if ((state->trans[transnr].atom == NULL) &&
2109
217k
    (state->trans[transnr].to >= 0)) {
2110
34.2k
    if (state->trans[transnr].to == statenr) {
2111
581
        state->trans[transnr].to = -1;
2112
33.6k
    } else if (state->trans[transnr].count < 0) {
2113
19.8k
        int newto = state->trans[transnr].to;
2114
2115
19.8k
        has_epsilon = 1;
2116
19.8k
        state->trans[transnr].to = -2;
2117
19.8k
        state->mark = XML_REGEXP_MARK_START;
2118
19.8k
        xmlFAReduceEpsilonTransitions(ctxt, statenr,
2119
19.8k
              newto, state->trans[transnr].counter);
2120
19.8k
        xmlFAFinishReduceEpsilonTransitions(ctxt, newto);
2121
19.8k
        state->mark = XML_REGEXP_MARK_NORMAL;
2122
19.8k
          }
2123
34.2k
      }
2124
217k
  }
2125
32.9k
    }
2126
    /*
2127
     * Eliminate the epsilon transitions
2128
     */
2129
4.87k
    if (has_epsilon) {
2130
42.2k
  for (statenr = 0;statenr < ctxt->nbStates;statenr++) {
2131
38.4k
      state = ctxt->states[statenr];
2132
38.4k
      if (state == NULL)
2133
12.0k
    continue;
2134
236k
      for (transnr = 0;transnr < state->nbTrans;transnr++) {
2135
210k
    xmlRegTransPtr trans = &(state->trans[transnr]);
2136
210k
    if ((trans->atom == NULL) &&
2137
210k
        (trans->count < 0) &&
2138
210k
        (trans->to >= 0)) {
2139
0
        trans->to = -1;
2140
0
    }
2141
210k
      }
2142
26.4k
  }
2143
3.74k
    }
2144
2145
    /*
2146
     * Use this pass to detect unreachable states too
2147
     */
2148
50.4k
    for (statenr = 0;statenr < ctxt->nbStates;statenr++) {
2149
45.5k
  state = ctxt->states[statenr];
2150
45.5k
  if (state != NULL)
2151
32.9k
      state->reached = XML_REGEXP_MARK_NORMAL;
2152
45.5k
    }
2153
4.87k
    state = ctxt->states[0];
2154
4.87k
    if (state != NULL)
2155
4.87k
  state->reached = XML_REGEXP_MARK_START;
2156
28.1k
    while (state != NULL) {
2157
23.3k
  xmlRegStatePtr target = NULL;
2158
23.3k
  state->reached = XML_REGEXP_MARK_VISITED;
2159
  /*
2160
   * Mark all states reachable from the current reachable state
2161
   */
2162
153k
  for (transnr = 0;transnr < state->nbTrans;transnr++) {
2163
130k
      if ((state->trans[transnr].to >= 0) &&
2164
130k
    ((state->trans[transnr].atom != NULL) ||
2165
75.5k
     (state->trans[transnr].count >= 0))) {
2166
75.5k
    int newto = state->trans[transnr].to;
2167
2168
75.5k
    if (ctxt->states[newto] == NULL)
2169
2
        continue;
2170
75.5k
    if (ctxt->states[newto]->reached == XML_REGEXP_MARK_NORMAL) {
2171
18.4k
        ctxt->states[newto]->reached = XML_REGEXP_MARK_START;
2172
18.4k
        target = ctxt->states[newto];
2173
18.4k
    }
2174
75.5k
      }
2175
130k
  }
2176
2177
  /*
2178
   * find the next accessible state not explored
2179
   */
2180
23.3k
  if (target == NULL) {
2181
126k
      for (statenr = 1;statenr < ctxt->nbStates;statenr++) {
2182
121k
    state = ctxt->states[statenr];
2183
121k
    if ((state != NULL) && (state->reached ==
2184
94.3k
      XML_REGEXP_MARK_START)) {
2185
7.93k
        target = state;
2186
7.93k
        break;
2187
7.93k
    }
2188
121k
      }
2189
12.8k
  }
2190
23.3k
  state = target;
2191
23.3k
    }
2192
50.4k
    for (statenr = 0;statenr < ctxt->nbStates;statenr++) {
2193
45.5k
  state = ctxt->states[statenr];
2194
45.5k
  if ((state != NULL) && (state->reached == XML_REGEXP_MARK_NORMAL)) {
2195
9.66k
      xmlRegFreeState(state);
2196
9.66k
      ctxt->states[statenr] = NULL;
2197
9.66k
  }
2198
45.5k
    }
2199
2200
4.87k
}
2201
2202
static int
2203
14.7k
xmlFACompareRanges(xmlRegRangePtr range1, xmlRegRangePtr range2) {
2204
14.7k
    int ret = 0;
2205
2206
14.7k
    if ((range1->type == XML_REGEXP_RANGES) ||
2207
14.7k
        (range2->type == XML_REGEXP_RANGES) ||
2208
14.7k
        (range2->type == XML_REGEXP_SUBREG) ||
2209
14.7k
        (range1->type == XML_REGEXP_SUBREG) ||
2210
14.7k
        (range1->type == XML_REGEXP_STRING) ||
2211
14.7k
        (range2->type == XML_REGEXP_STRING))
2212
0
  return(-1);
2213
2214
    /* put them in order */
2215
14.7k
    if (range1->type > range2->type) {
2216
5.93k
        xmlRegRangePtr tmp;
2217
2218
5.93k
  tmp = range1;
2219
5.93k
  range1 = range2;
2220
5.93k
  range2 = tmp;
2221
5.93k
    }
2222
14.7k
    if ((range1->type == XML_REGEXP_ANYCHAR) ||
2223
14.7k
        (range2->type == XML_REGEXP_ANYCHAR)) {
2224
0
  ret = 1;
2225
14.7k
    } else if ((range1->type == XML_REGEXP_EPSILON) ||
2226
14.7k
               (range2->type == XML_REGEXP_EPSILON)) {
2227
0
  return(0);
2228
14.7k
    } else if (range1->type == range2->type) {
2229
2.81k
        if (range1->type != XML_REGEXP_CHARVAL)
2230
230
            ret = 1;
2231
2.58k
        else if ((range1->end < range2->start) ||
2232
2.58k
           (range2->end < range1->start))
2233
1.60k
      ret = 0;
2234
983
  else
2235
983
      ret = 1;
2236
11.8k
    } else if (range1->type == XML_REGEXP_CHARVAL) {
2237
2.23k
        int codepoint;
2238
2.23k
  int neg = 0;
2239
2240
  /*
2241
   * just check all codepoints in the range for acceptance,
2242
   * this is usually way cheaper since done only once at
2243
   * compilation than testing over and over at runtime or
2244
   * pushing too many states when evaluating.
2245
   */
2246
2.23k
  if (((range1->neg == 0) && (range2->neg != 0)) ||
2247
2.23k
      ((range1->neg != 0) && (range2->neg == 0)))
2248
651
      neg = 1;
2249
2250
6.63M
  for (codepoint = range1->start;codepoint <= range1->end ;codepoint++) {
2251
6.63M
      ret = xmlRegCheckCharacterRange(range2->type, codepoint,
2252
6.63M
              0, range2->start, range2->end,
2253
6.63M
              range2->blockName);
2254
6.63M
      if (ret < 0)
2255
216
          return(-1);
2256
6.63M
      if (((neg == 1) && (ret == 0)) ||
2257
6.63M
          ((neg == 0) && (ret == 1)))
2258
678
    return(1);
2259
6.63M
  }
2260
1.33k
  return(0);
2261
9.66k
    } else if ((range1->type == XML_REGEXP_BLOCK_NAME) ||
2262
9.66k
               (range2->type == XML_REGEXP_BLOCK_NAME)) {
2263
209
  if (range1->type == range2->type) {
2264
0
      ret = xmlStrEqual(range1->blockName, range2->blockName);
2265
209
  } else {
2266
      /*
2267
       * comparing a block range with anything else is way
2268
       * too costly, and maintaining the table is like too much
2269
       * memory too, so let's force the automata to save state
2270
       * here.
2271
       */
2272
209
      return(1);
2273
209
  }
2274
9.45k
    } else if ((range1->type < XML_REGEXP_LETTER) ||
2275
9.45k
               (range2->type < XML_REGEXP_LETTER)) {
2276
2.10k
  if ((range1->type == XML_REGEXP_ANYSPACE) &&
2277
2.10k
      (range2->type == XML_REGEXP_NOTSPACE))
2278
206
      ret = 0;
2279
1.89k
  else if ((range1->type == XML_REGEXP_INITNAME) &&
2280
1.89k
           (range2->type == XML_REGEXP_NOTINITNAME))
2281
211
      ret = 0;
2282
1.68k
  else if ((range1->type == XML_REGEXP_NAMECHAR) &&
2283
1.68k
           (range2->type == XML_REGEXP_NOTNAMECHAR))
2284
211
      ret = 0;
2285
1.47k
  else if ((range1->type == XML_REGEXP_DECIMAL) &&
2286
1.47k
           (range2->type == XML_REGEXP_NOTDECIMAL))
2287
199
      ret = 0;
2288
1.27k
  else if ((range1->type == XML_REGEXP_REALCHAR) &&
2289
1.27k
           (range2->type == XML_REGEXP_NOTREALCHAR))
2290
201
      ret = 0;
2291
1.07k
  else {
2292
      /* same thing to limit complexity */
2293
1.07k
      return(1);
2294
1.07k
  }
2295
7.35k
    } else {
2296
7.35k
        ret = 0;
2297
        /* range1->type < range2->type here */
2298
7.35k
        switch (range1->type) {
2299
1.21k
      case XML_REGEXP_LETTER:
2300
           /* all disjoint except in the subgroups */
2301
1.21k
           if ((range2->type == XML_REGEXP_LETTER_UPPERCASE) ||
2302
1.21k
         (range2->type == XML_REGEXP_LETTER_LOWERCASE) ||
2303
1.21k
         (range2->type == XML_REGEXP_LETTER_TITLECASE) ||
2304
1.21k
         (range2->type == XML_REGEXP_LETTER_MODIFIER) ||
2305
1.21k
         (range2->type == XML_REGEXP_LETTER_OTHERS))
2306
1.00k
         ret = 1;
2307
1.21k
     break;
2308
822
      case XML_REGEXP_MARK:
2309
822
           if ((range2->type == XML_REGEXP_MARK_NONSPACING) ||
2310
822
         (range2->type == XML_REGEXP_MARK_SPACECOMBINING) ||
2311
822
         (range2->type == XML_REGEXP_MARK_ENCLOSING))
2312
609
         ret = 1;
2313
822
     break;
2314
819
      case XML_REGEXP_NUMBER:
2315
819
           if ((range2->type == XML_REGEXP_NUMBER_DECIMAL) ||
2316
819
         (range2->type == XML_REGEXP_NUMBER_LETTER) ||
2317
819
         (range2->type == XML_REGEXP_NUMBER_OTHERS))
2318
608
         ret = 1;
2319
819
     break;
2320
1.63k
      case XML_REGEXP_PUNCT:
2321
1.63k
           if ((range2->type == XML_REGEXP_PUNCT_CONNECTOR) ||
2322
1.63k
         (range2->type == XML_REGEXP_PUNCT_DASH) ||
2323
1.63k
         (range2->type == XML_REGEXP_PUNCT_OPEN) ||
2324
1.63k
         (range2->type == XML_REGEXP_PUNCT_CLOSE) ||
2325
1.63k
         (range2->type == XML_REGEXP_PUNCT_INITQUOTE) ||
2326
1.63k
         (range2->type == XML_REGEXP_PUNCT_FINQUOTE) ||
2327
1.63k
         (range2->type == XML_REGEXP_PUNCT_OTHERS))
2328
1.42k
         ret = 1;
2329
1.63k
     break;
2330
830
      case XML_REGEXP_SEPAR:
2331
830
           if ((range2->type == XML_REGEXP_SEPAR_SPACE) ||
2332
830
         (range2->type == XML_REGEXP_SEPAR_LINE) ||
2333
830
         (range2->type == XML_REGEXP_SEPAR_PARA))
2334
619
         ret = 1;
2335
830
     break;
2336
1.02k
      case XML_REGEXP_SYMBOL:
2337
1.02k
           if ((range2->type == XML_REGEXP_SYMBOL_MATH) ||
2338
1.02k
         (range2->type == XML_REGEXP_SYMBOL_CURRENCY) ||
2339
1.02k
         (range2->type == XML_REGEXP_SYMBOL_MODIFIER) ||
2340
1.02k
         (range2->type == XML_REGEXP_SYMBOL_OTHERS))
2341
810
         ret = 1;
2342
1.02k
     break;
2343
808
      case XML_REGEXP_OTHER:
2344
808
           if ((range2->type == XML_REGEXP_OTHER_CONTROL) ||
2345
808
         (range2->type == XML_REGEXP_OTHER_FORMAT) ||
2346
808
         (range2->type == XML_REGEXP_OTHER_PRIVATE))
2347
605
         ret = 1;
2348
808
     break;
2349
205
            default:
2350
205
           if ((range2->type >= XML_REGEXP_LETTER) &&
2351
205
         (range2->type < XML_REGEXP_BLOCK_NAME))
2352
205
         ret = 0;
2353
0
     else {
2354
         /* safety net ! */
2355
0
         return(1);
2356
0
     }
2357
7.35k
  }
2358
7.35k
    }
2359
11.1k
    if (((range1->neg == 0) && (range2->neg != 0)) ||
2360
11.1k
        ((range1->neg != 0) && (range2->neg == 0)))
2361
714
  ret = !ret;
2362
11.1k
    return(ret);
2363
14.7k
}
2364
2365
/**
2366
 * Compares two atoms type to check whether they intersect in some ways,
2367
 * this is used by xmlFACompareAtoms only
2368
 *
2369
 * @param type1  an atom type
2370
 * @param type2  an atom type
2371
 * @returns 1 if they may intersect and 0 otherwise
2372
 */
2373
static int
2374
68.9k
xmlFACompareAtomTypes(xmlRegAtomType type1, xmlRegAtomType type2) {
2375
68.9k
    if ((type1 == XML_REGEXP_EPSILON) ||
2376
68.9k
        (type1 == XML_REGEXP_CHARVAL) ||
2377
68.9k
  (type1 == XML_REGEXP_RANGES) ||
2378
68.9k
  (type1 == XML_REGEXP_SUBREG) ||
2379
68.9k
  (type1 == XML_REGEXP_STRING) ||
2380
68.9k
  (type1 == XML_REGEXP_ANYCHAR))
2381
50.9k
  return(1);
2382
17.9k
    if ((type2 == XML_REGEXP_EPSILON) ||
2383
17.9k
        (type2 == XML_REGEXP_CHARVAL) ||
2384
17.9k
  (type2 == XML_REGEXP_RANGES) ||
2385
17.9k
  (type2 == XML_REGEXP_SUBREG) ||
2386
17.9k
  (type2 == XML_REGEXP_STRING) ||
2387
17.9k
  (type2 == XML_REGEXP_ANYCHAR))
2388
0
  return(1);
2389
2390
17.9k
    if (type1 == type2) return(1);
2391
2392
    /* simplify subsequent compares by making sure type1 < type2 */
2393
17.9k
    if (type1 > type2) {
2394
0
        xmlRegAtomType tmp = type1;
2395
0
  type1 = type2;
2396
0
  type2 = tmp;
2397
0
    }
2398
17.9k
    switch (type1) {
2399
1.68k
        case XML_REGEXP_ANYSPACE: /* \s */
2400
      /* can't be a letter, number, mark, punctuation, symbol */
2401
1.68k
      if ((type2 == XML_REGEXP_NOTSPACE) ||
2402
1.68k
    ((type2 >= XML_REGEXP_LETTER) &&
2403
1.47k
     (type2 <= XML_REGEXP_LETTER_OTHERS)) ||
2404
1.68k
          ((type2 >= XML_REGEXP_NUMBER) &&
2405
1.26k
     (type2 <= XML_REGEXP_NUMBER_OTHERS)) ||
2406
1.68k
          ((type2 >= XML_REGEXP_MARK) &&
2407
1.06k
     (type2 <= XML_REGEXP_MARK_ENCLOSING)) ||
2408
1.68k
          ((type2 >= XML_REGEXP_PUNCT) &&
2409
850
     (type2 <= XML_REGEXP_PUNCT_OTHERS)) ||
2410
1.68k
          ((type2 >= XML_REGEXP_SYMBOL) &&
2411
635
     (type2 <= XML_REGEXP_SYMBOL_OTHERS))
2412
1.68k
          ) return(0);
2413
426
      break;
2414
426
        case XML_REGEXP_NOTSPACE: /* \S */
2415
28
      break;
2416
2.01k
        case XML_REGEXP_INITNAME: /* \l */
2417
      /* can't be a number, mark, separator, punctuation, symbol or other */
2418
2.01k
      if ((type2 == XML_REGEXP_NOTINITNAME) ||
2419
2.01k
          ((type2 >= XML_REGEXP_NUMBER) &&
2420
1.68k
     (type2 <= XML_REGEXP_NUMBER_OTHERS)) ||
2421
2.01k
          ((type2 >= XML_REGEXP_MARK) &&
2422
1.47k
     (type2 <= XML_REGEXP_MARK_ENCLOSING)) ||
2423
2.01k
          ((type2 >= XML_REGEXP_SEPAR) &&
2424
1.27k
     (type2 <= XML_REGEXP_SEPAR_PARA)) ||
2425
2.01k
          ((type2 >= XML_REGEXP_PUNCT) &&
2426
1.06k
     (type2 <= XML_REGEXP_PUNCT_OTHERS)) ||
2427
2.01k
          ((type2 >= XML_REGEXP_SYMBOL) &&
2428
856
     (type2 <= XML_REGEXP_SYMBOL_OTHERS)) ||
2429
2.01k
          ((type2 >= XML_REGEXP_OTHER) &&
2430
648
     (type2 <= XML_REGEXP_OTHER_NA))
2431
2.01k
    ) return(0);
2432
437
      break;
2433
437
        case XML_REGEXP_NOTINITNAME: /* \L */
2434
159
      break;
2435
1.74k
        case XML_REGEXP_NAMECHAR: /* \c */
2436
      /* can't be a mark, separator, punctuation, symbol or other */
2437
1.74k
      if ((type2 == XML_REGEXP_NOTNAMECHAR) ||
2438
1.74k
          ((type2 >= XML_REGEXP_MARK) &&
2439
1.46k
     (type2 <= XML_REGEXP_MARK_ENCLOSING)) ||
2440
1.74k
          ((type2 >= XML_REGEXP_PUNCT) &&
2441
1.26k
     (type2 <= XML_REGEXP_PUNCT_OTHERS)) ||
2442
1.74k
          ((type2 >= XML_REGEXP_SEPAR) &&
2443
1.05k
     (type2 <= XML_REGEXP_SEPAR_PARA)) ||
2444
1.74k
          ((type2 >= XML_REGEXP_SYMBOL) &&
2445
839
     (type2 <= XML_REGEXP_SYMBOL_OTHERS)) ||
2446
1.74k
          ((type2 >= XML_REGEXP_OTHER) &&
2447
631
     (type2 <= XML_REGEXP_OTHER_NA))
2448
1.74k
    ) return(0);
2449
424
      break;
2450
424
        case XML_REGEXP_NOTNAMECHAR: /* \C */
2451
60
      break;
2452
2.11k
        case XML_REGEXP_DECIMAL: /* \d */
2453
      /* can't be a letter, mark, separator, punctuation, symbol or other */
2454
2.11k
      if ((type2 == XML_REGEXP_NOTDECIMAL) ||
2455
2.11k
          (type2 == XML_REGEXP_REALCHAR) ||
2456
2.11k
    ((type2 >= XML_REGEXP_LETTER) &&
2457
1.69k
     (type2 <= XML_REGEXP_LETTER_OTHERS)) ||
2458
2.11k
          ((type2 >= XML_REGEXP_MARK) &&
2459
1.48k
     (type2 <= XML_REGEXP_MARK_ENCLOSING)) ||
2460
2.11k
          ((type2 >= XML_REGEXP_PUNCT) &&
2461
1.27k
     (type2 <= XML_REGEXP_PUNCT_OTHERS)) ||
2462
2.11k
          ((type2 >= XML_REGEXP_SEPAR) &&
2463
1.06k
     (type2 <= XML_REGEXP_SEPAR_PARA)) ||
2464
2.11k
          ((type2 >= XML_REGEXP_SYMBOL) &&
2465
860
     (type2 <= XML_REGEXP_SYMBOL_OTHERS)) ||
2466
2.11k
          ((type2 >= XML_REGEXP_OTHER) &&
2467
656
     (type2 <= XML_REGEXP_OTHER_NA))
2468
2.11k
    )return(0);
2469
447
      break;
2470
447
        case XML_REGEXP_NOTDECIMAL: /* \D */
2471
0
      break;
2472
1.47k
        case XML_REGEXP_REALCHAR: /* \w */
2473
      /* can't be a mark, separator, punctuation, symbol or other */
2474
1.47k
      if ((type2 == XML_REGEXP_NOTDECIMAL) ||
2475
1.47k
          ((type2 >= XML_REGEXP_MARK) &&
2476
1.47k
     (type2 <= XML_REGEXP_MARK_ENCLOSING)) ||
2477
1.47k
          ((type2 >= XML_REGEXP_PUNCT) &&
2478
1.25k
     (type2 <= XML_REGEXP_PUNCT_OTHERS)) ||
2479
1.47k
          ((type2 >= XML_REGEXP_SEPAR) &&
2480
1.04k
     (type2 <= XML_REGEXP_SEPAR_PARA)) ||
2481
1.47k
          ((type2 >= XML_REGEXP_SYMBOL) &&
2482
840
     (type2 <= XML_REGEXP_SYMBOL_OTHERS)) ||
2483
1.47k
          ((type2 >= XML_REGEXP_OTHER) &&
2484
632
     (type2 <= XML_REGEXP_OTHER_NA))
2485
1.47k
    )return(0);
2486
423
      break;
2487
423
        case XML_REGEXP_NOTREALCHAR: /* \W */
2488
0
      break;
2489
  /*
2490
   * at that point we know both type 1 and type2 are from
2491
   * character categories are ordered and are different,
2492
   * it becomes simple because this is a partition
2493
   */
2494
404
        case XML_REGEXP_LETTER:
2495
404
      if (type2 <= XML_REGEXP_LETTER_OTHERS)
2496
200
          return(1);
2497
204
      return(0);
2498
199
        case XML_REGEXP_LETTER_UPPERCASE:
2499
396
        case XML_REGEXP_LETTER_LOWERCASE:
2500
594
        case XML_REGEXP_LETTER_TITLECASE:
2501
794
        case XML_REGEXP_LETTER_MODIFIER:
2502
999
        case XML_REGEXP_LETTER_OTHERS:
2503
999
      return(0);
2504
402
        case XML_REGEXP_MARK:
2505
402
      if (type2 <= XML_REGEXP_MARK_ENCLOSING)
2506
200
          return(1);
2507
202
      return(0);
2508
205
        case XML_REGEXP_MARK_NONSPACING:
2509
406
        case XML_REGEXP_MARK_SPACECOMBINING:
2510
605
        case XML_REGEXP_MARK_ENCLOSING:
2511
605
      return(0);
2512
408
        case XML_REGEXP_NUMBER:
2513
408
      if (type2 <= XML_REGEXP_NUMBER_OTHERS)
2514
205
          return(1);
2515
203
      return(0);
2516
205
        case XML_REGEXP_NUMBER_DECIMAL:
2517
409
        case XML_REGEXP_NUMBER_LETTER:
2518
613
        case XML_REGEXP_NUMBER_OTHERS:
2519
613
      return(0);
2520
411
        case XML_REGEXP_PUNCT:
2521
411
      if (type2 <= XML_REGEXP_PUNCT_OTHERS)
2522
205
          return(1);
2523
206
      return(0);
2524
198
        case XML_REGEXP_PUNCT_CONNECTOR:
2525
402
        case XML_REGEXP_PUNCT_DASH:
2526
603
        case XML_REGEXP_PUNCT_OPEN:
2527
804
        case XML_REGEXP_PUNCT_CLOSE:
2528
1.00k
        case XML_REGEXP_PUNCT_INITQUOTE:
2529
1.20k
        case XML_REGEXP_PUNCT_FINQUOTE:
2530
1.40k
        case XML_REGEXP_PUNCT_OTHERS:
2531
1.40k
      return(0);
2532
401
        case XML_REGEXP_SEPAR:
2533
401
      if (type2 <= XML_REGEXP_SEPAR_PARA)
2534
197
          return(1);
2535
204
      return(0);
2536
197
        case XML_REGEXP_SEPAR_SPACE:
2537
396
        case XML_REGEXP_SEPAR_LINE:
2538
593
        case XML_REGEXP_SEPAR_PARA:
2539
593
      return(0);
2540
412
        case XML_REGEXP_SYMBOL:
2541
412
      if (type2 <= XML_REGEXP_SYMBOL_OTHERS)
2542
208
          return(1);
2543
204
      return(0);
2544
213
        case XML_REGEXP_SYMBOL_MATH:
2545
413
        case XML_REGEXP_SYMBOL_CURRENCY:
2546
618
        case XML_REGEXP_SYMBOL_MODIFIER:
2547
823
        case XML_REGEXP_SYMBOL_OTHERS:
2548
823
      return(0);
2549
403
        case XML_REGEXP_OTHER:
2550
403
      if (type2 <= XML_REGEXP_OTHER_NA)
2551
201
          return(1);
2552
202
      return(0);
2553
202
        case XML_REGEXP_OTHER_CONTROL:
2554
402
        case XML_REGEXP_OTHER_FORMAT:
2555
604
        case XML_REGEXP_OTHER_PRIVATE:
2556
807
        case XML_REGEXP_OTHER_NA:
2557
807
      return(0);
2558
0
  default:
2559
0
      break;
2560
17.9k
    }
2561
2.40k
    return(1);
2562
17.9k
}
2563
2564
/**
2565
 * Compares two atoms to check whether they are the same exactly
2566
 * this is used to remove equivalent transitions
2567
 *
2568
 * @param atom1  an atom
2569
 * @param atom2  an atom
2570
 * @param deep  if not set only compare string pointers
2571
 * @returns 1 if same and 0 otherwise
2572
 */
2573
static int
2574
74.0k
xmlFAEqualAtoms(xmlRegAtomPtr atom1, xmlRegAtomPtr atom2, int deep) {
2575
74.0k
    int ret = 0;
2576
2577
74.0k
    if (atom1 == atom2)
2578
51.5k
  return(1);
2579
22.5k
    if ((atom1 == NULL) || (atom2 == NULL))
2580
0
  return(0);
2581
2582
22.5k
    if (atom1->type != atom2->type)
2583
2.67k
        return(0);
2584
19.8k
    switch (atom1->type) {
2585
0
        case XML_REGEXP_EPSILON:
2586
0
      ret = 0;
2587
0
      break;
2588
0
        case XML_REGEXP_STRING:
2589
0
            if (!deep)
2590
0
                ret = (atom1->valuep == atom2->valuep);
2591
0
            else
2592
0
                ret = xmlStrEqual((xmlChar *)atom1->valuep,
2593
0
                                  (xmlChar *)atom2->valuep);
2594
0
      break;
2595
17.7k
        case XML_REGEXP_CHARVAL:
2596
17.7k
      ret = (atom1->codepoint == atom2->codepoint);
2597
17.7k
      break;
2598
16
  case XML_REGEXP_RANGES:
2599
      /* too hard to do in the general case */
2600
16
      ret = 0;
2601
2.12k
  default:
2602
2.12k
      break;
2603
19.8k
    }
2604
19.8k
    return(ret);
2605
19.8k
}
2606
2607
/**
2608
 * Compares two atoms to check whether they intersect in some ways,
2609
 * this is used by xmlFAComputesDeterminism and xmlFARecurseDeterminism only
2610
 *
2611
 * @param atom1  an atom
2612
 * @param atom2  an atom
2613
 * @param deep  if not set only compare string pointers
2614
 * @returns 1 if yes and 0 otherwise
2615
 */
2616
static int
2617
218k
xmlFACompareAtoms(xmlRegAtomPtr atom1, xmlRegAtomPtr atom2, int deep) {
2618
218k
    int ret = 1;
2619
2620
218k
    if (atom1 == atom2)
2621
37.5k
  return(1);
2622
180k
    if ((atom1 == NULL) || (atom2 == NULL))
2623
0
  return(0);
2624
2625
180k
    if ((atom1->type == XML_REGEXP_ANYCHAR) ||
2626
180k
        (atom2->type == XML_REGEXP_ANYCHAR))
2627
17.4k
  return(1);
2628
2629
163k
    if (atom1->type > atom2->type) {
2630
39.8k
  xmlRegAtomPtr tmp;
2631
39.8k
  tmp = atom1;
2632
39.8k
  atom1 = atom2;
2633
39.8k
  atom2 = tmp;
2634
39.8k
    }
2635
163k
    if (atom1->type != atom2->type) {
2636
68.9k
        ret = xmlFACompareAtomTypes(atom1->type, atom2->type);
2637
  /* if they can't intersect at the type level break now */
2638
68.9k
  if (ret == 0)
2639
14.1k
      return(0);
2640
68.9k
    }
2641
149k
    switch (atom1->type) {
2642
0
        case XML_REGEXP_STRING:
2643
0
            if (!deep)
2644
0
                ret = (atom1->valuep != atom2->valuep);
2645
0
            else {
2646
0
                xmlChar *val1 = (xmlChar *)atom1->valuep;
2647
0
                xmlChar *val2 = (xmlChar *)atom2->valuep;
2648
0
                int compound1 = (xmlStrchr(val1, '|') != NULL);
2649
0
                int compound2 = (xmlStrchr(val2, '|') != NULL);
2650
2651
                /* Ignore negative match flag for ##other namespaces */
2652
0
                if (compound1 != compound2)
2653
0
                    return(0);
2654
2655
0
                ret = xmlRegStrEqualWildcard(val1, val2);
2656
0
            }
2657
0
      break;
2658
0
        case XML_REGEXP_EPSILON:
2659
0
      goto not_determinist;
2660
130k
        case XML_REGEXP_CHARVAL:
2661
130k
      if (atom2->type == XML_REGEXP_CHARVAL) {
2662
79.5k
    ret = (atom1->codepoint == atom2->codepoint);
2663
79.5k
      } else {
2664
50.7k
          ret = xmlRegCheckCharacter(atom2, atom1->codepoint);
2665
50.7k
    if (ret < 0)
2666
5.11k
        ret = 1;
2667
50.7k
      }
2668
130k
      break;
2669
12.1k
        case XML_REGEXP_RANGES:
2670
12.1k
      if (atom2->type == XML_REGEXP_RANGES) {
2671
11.9k
          int i, j, res;
2672
11.9k
    xmlRegRangePtr r1, r2;
2673
2674
    /*
2675
     * need to check that none of the ranges eventually matches
2676
     */
2677
16.6k
    for (i = 0;i < atom1->nbRanges;i++) {
2678
19.3k
        for (j = 0;j < atom2->nbRanges;j++) {
2679
14.7k
      r1 = atom1->ranges[i];
2680
14.7k
      r2 = atom2->ranges[j];
2681
14.7k
      res = xmlFACompareRanges(r1, r2);
2682
14.7k
      if (res == 1) {
2683
8.16k
          ret = 1;
2684
8.16k
          goto done;
2685
8.16k
      }
2686
14.7k
        }
2687
12.7k
    }
2688
3.81k
    ret = 0;
2689
3.81k
      }
2690
4.00k
      break;
2691
6.84k
  default:
2692
6.84k
      goto not_determinist;
2693
149k
    }
2694
142k
done:
2695
142k
    if (atom1->neg != atom2->neg) {
2696
11.5k
        ret = !ret;
2697
11.5k
    }
2698
142k
    if (ret == 0)
2699
93.6k
        return(0);
2700
55.6k
not_determinist:
2701
55.6k
    return(1);
2702
142k
}
2703
2704
/**
2705
 * Check whether the associated regexp is determinist,
2706
 * should be called after xmlFAEliminateEpsilonTransitions
2707
 *
2708
 * @param ctxt  a regexp parser context
2709
 * @param state  regexp state
2710
 * @param fromnr  the from state
2711
 * @param tonr  the to state
2712
 * @param atom  the atom
2713
 */
2714
static int
2715
xmlFARecurseDeterminism(xmlRegParserCtxtPtr ctxt, xmlRegStatePtr state,
2716
58.1k
                  int fromnr, int tonr, xmlRegAtomPtr atom) {
2717
58.1k
    int ret = 1;
2718
58.1k
    int res;
2719
58.1k
    int transnr, nbTrans;
2720
58.1k
    xmlRegTransPtr t1;
2721
58.1k
    int deep = 1;
2722
2723
58.1k
    if (state == NULL)
2724
0
  return(ret);
2725
58.1k
    if (state->markd == XML_REGEXP_MARK_VISITED)
2726
11.7k
  return(ret);
2727
2728
46.3k
    if (ctxt->flags & AM_AUTOMATA_RNG)
2729
0
        deep = 0;
2730
2731
    /*
2732
     * don't recurse on transitions potentially added in the course of
2733
     * the elimination.
2734
     */
2735
46.3k
    nbTrans = state->nbTrans;
2736
259k
    for (transnr = 0;transnr < nbTrans;transnr++) {
2737
213k
  t1 = &(state->trans[transnr]);
2738
  /*
2739
   * check transitions conflicting with the one looked at
2740
   */
2741
213k
        if ((t1->to < 0) || (t1->to == fromnr))
2742
106k
            continue;
2743
106k
  if (t1->atom == NULL) {
2744
33.3k
      state->markd = XML_REGEXP_MARK_VISITED;
2745
33.3k
      res = xmlFARecurseDeterminism(ctxt, ctxt->states[t1->to],
2746
33.3k
                              fromnr, tonr, atom);
2747
33.3k
      if (res == 0) {
2748
3.82k
          ret = 0;
2749
    /* t1->nd = 1; */
2750
3.82k
      }
2751
33.3k
      continue;
2752
33.3k
  }
2753
73.2k
  if (xmlFACompareAtoms(t1->atom, atom, deep)) {
2754
            /* Treat equal transitions as deterministic. */
2755
43.1k
            if ((t1->to != tonr) ||
2756
43.1k
                (!xmlFAEqualAtoms(t1->atom, atom, deep)))
2757
19.2k
                ret = 0;
2758
      /* mark the transition as non-deterministic */
2759
43.1k
      t1->nd = 1;
2760
43.1k
  }
2761
73.2k
    }
2762
46.3k
    return(ret);
2763
58.1k
}
2764
2765
/**
2766
 * Reset flags after checking determinism.
2767
 *
2768
 * @param ctxt  a regexp parser context
2769
 * @param state  regexp state
2770
 */
2771
static void
2772
59.8k
xmlFAFinishRecurseDeterminism(xmlRegParserCtxtPtr ctxt, xmlRegStatePtr state) {
2773
59.8k
    int transnr, nbTrans;
2774
2775
59.8k
    if (state == NULL)
2776
0
  return;
2777
59.8k
    if (state->markd != XML_REGEXP_MARK_VISITED)
2778
44.2k
  return;
2779
15.6k
    state->markd = 0;
2780
2781
15.6k
    nbTrans = state->nbTrans;
2782
214k
    for (transnr = 0; transnr < nbTrans; transnr++) {
2783
199k
  xmlRegTransPtr t1 = &state->trans[transnr];
2784
199k
  if ((t1->atom == NULL) && (t1->to >= 0))
2785
35.1k
      xmlFAFinishRecurseDeterminism(ctxt, ctxt->states[t1->to]);
2786
199k
    }
2787
15.6k
}
2788
2789
/**
2790
 * Check whether the associated regexp is determinist,
2791
 * should be called after xmlFAEliminateEpsilonTransitions
2792
 *
2793
 * @param ctxt  a regexp parser context
2794
 */
2795
static int
2796
4.78k
xmlFAComputesDeterminism(xmlRegParserCtxtPtr ctxt) {
2797
4.78k
    int statenr, transnr;
2798
4.78k
    xmlRegStatePtr state;
2799
4.78k
    xmlRegTransPtr t1, t2, last;
2800
4.78k
    int i;
2801
4.78k
    int ret = 1;
2802
4.78k
    int deep = 1;
2803
2804
4.78k
    if (ctxt->determinist != -1)
2805
0
  return(ctxt->determinist);
2806
2807
4.78k
    if (ctxt->flags & AM_AUTOMATA_RNG)
2808
0
        deep = 0;
2809
2810
    /*
2811
     * First cleanup the automata removing cancelled transitions
2812
     */
2813
47.2k
    for (statenr = 0;statenr < ctxt->nbStates;statenr++) {
2814
42.4k
  state = ctxt->states[statenr];
2815
42.4k
  if (state == NULL)
2816
19.7k
      continue;
2817
22.7k
  if (state->nbTrans < 2)
2818
10.7k
      continue;
2819
122k
  for (transnr = 0;transnr < state->nbTrans;transnr++) {
2820
110k
      t1 = &(state->trans[transnr]);
2821
      /*
2822
       * Determinism checks in case of counted or all transitions
2823
       * will have to be handled separately
2824
       */
2825
110k
      if (t1->atom == NULL) {
2826
    /* t1->nd = 1; */
2827
25.7k
    continue;
2828
25.7k
      }
2829
84.5k
      if (t1->to < 0) /* eliminated */
2830
34.6k
    continue;
2831
1.28M
      for (i = 0;i < transnr;i++) {
2832
1.23M
    t2 = &(state->trans[i]);
2833
1.23M
    if (t2->to < 0) /* eliminated */
2834
1.05M
        continue;
2835
188k
    if (t2->atom != NULL) {
2836
163k
        if (t1->to == t2->to) {
2837
                        /*
2838
                         * Here we use deep because we want to keep the
2839
                         * transitions which indicate a conflict
2840
                         */
2841
33.9k
      if (xmlFAEqualAtoms(t1->atom, t2->atom, deep) &&
2842
33.9k
                            (t1->counter == t2->counter) &&
2843
33.9k
                            (t1->count == t2->count))
2844
1.44k
          t2->to = -1; /* eliminated */
2845
33.9k
        }
2846
163k
    }
2847
188k
      }
2848
49.8k
  }
2849
11.9k
    }
2850
2851
    /*
2852
     * Check for all states that there aren't 2 transitions
2853
     * with the same atom and a different target.
2854
     */
2855
47.2k
    for (statenr = 0;statenr < ctxt->nbStates;statenr++) {
2856
42.4k
  state = ctxt->states[statenr];
2857
42.4k
  if (state == NULL)
2858
19.7k
      continue;
2859
22.7k
  if (state->nbTrans < 2)
2860
10.7k
      continue;
2861
11.9k
  last = NULL;
2862
122k
  for (transnr = 0;transnr < state->nbTrans;transnr++) {
2863
110k
      t1 = &(state->trans[transnr]);
2864
      /*
2865
       * Determinism checks in case of counted or all transitions
2866
       * will have to be handled separately
2867
       */
2868
110k
      if (t1->atom == NULL) {
2869
25.7k
    continue;
2870
25.7k
      }
2871
84.5k
      if (t1->to < 0) /* eliminated */
2872
36.1k
    continue;
2873
728k
      for (i = 0;i < transnr;i++) {
2874
679k
    t2 = &(state->trans[i]);
2875
679k
    if (t2->to < 0) /* eliminated */
2876
509k
        continue;
2877
170k
    if (t2->atom != NULL) {
2878
                    /*
2879
                     * But here we don't use deep because we want to
2880
                     * find transitions which indicate a conflict
2881
                     */
2882
145k
        if (xmlFACompareAtoms(t1->atom, t2->atom, 1)) {
2883
                        /*
2884
                         * Treat equal counter transitions that couldn't be
2885
                         * eliminated as deterministic.
2886
                         */
2887
67.5k
                        if ((t1->to != t2->to) ||
2888
67.5k
                            (t1->counter == t2->counter) ||
2889
67.5k
                            (!xmlFAEqualAtoms(t1->atom, t2->atom, deep)))
2890
53.3k
                            ret = 0;
2891
      /* mark the transitions as non-deterministic ones */
2892
67.5k
      t1->nd = 1;
2893
67.5k
      t2->nd = 1;
2894
67.5k
      last = t1;
2895
67.5k
        }
2896
145k
    } else {
2897
24.7k
                    int res;
2898
2899
        /*
2900
         * do the closure in case of remaining specific
2901
         * epsilon transitions like choices or all
2902
         */
2903
24.7k
        res = xmlFARecurseDeterminism(ctxt, ctxt->states[t2->to],
2904
24.7k
              statenr, t1->to, t1->atom);
2905
24.7k
                    xmlFAFinishRecurseDeterminism(ctxt, ctxt->states[t2->to]);
2906
        /* don't shortcut the computation so all non deterministic
2907
           transition get marked down
2908
        if (ret == 0)
2909
      return(0);
2910
         */
2911
24.7k
        if (res == 0) {
2912
5.26k
      t1->nd = 1;
2913
      /* t2->nd = 1; */
2914
5.26k
      last = t1;
2915
5.26k
                        ret = 0;
2916
5.26k
        }
2917
24.7k
    }
2918
170k
      }
2919
      /* don't shortcut the computation so all non deterministic
2920
         transition get marked down
2921
      if (ret == 0)
2922
    break; */
2923
48.3k
  }
2924
2925
  /*
2926
   * mark specifically the last non-deterministic transition
2927
   * from a state since there is no need to set-up rollback
2928
   * from it
2929
   */
2930
11.9k
  if (last != NULL) {
2931
7.93k
      last->nd = 2;
2932
7.93k
  }
2933
2934
  /* don't shortcut the computation so all non deterministic
2935
     transition get marked down
2936
  if (ret == 0)
2937
      break; */
2938
11.9k
    }
2939
2940
4.78k
    ctxt->determinist = ret;
2941
4.78k
    return(ret);
2942
4.78k
}
2943
2944
/************************************************************************
2945
 *                  *
2946
 *  Routines to check input against transition atoms    *
2947
 *                  *
2948
 ************************************************************************/
2949
2950
static int
2951
xmlRegCheckCharacterRange(xmlRegAtomType type, int codepoint, int neg,
2952
6.68M
                    int start, int end, const xmlChar *blockName) {
2953
6.68M
    int ret = 0;
2954
2955
6.68M
    switch (type) {
2956
0
        case XML_REGEXP_STRING:
2957
0
        case XML_REGEXP_SUBREG:
2958
0
        case XML_REGEXP_RANGES:
2959
0
        case XML_REGEXP_EPSILON:
2960
0
      return(-1);
2961
0
        case XML_REGEXP_ANYCHAR:
2962
0
      ret = ((codepoint != '\n') && (codepoint != '\r'));
2963
0
      break;
2964
2.45k
        case XML_REGEXP_CHARVAL:
2965
2.45k
      ret = ((codepoint >= start) && (codepoint <= end));
2966
2.45k
      break;
2967
859
        case XML_REGEXP_NOTSPACE:
2968
859
      neg = !neg;
2969
            /* Falls through. */
2970
4.32M
        case XML_REGEXP_ANYSPACE:
2971
4.32M
      ret = ((codepoint == '\n') || (codepoint == '\r') ||
2972
4.32M
       (codepoint == '\t') || (codepoint == ' '));
2973
4.32M
      break;
2974
4.49k
        case XML_REGEXP_NOTINITNAME:
2975
4.49k
      neg = !neg;
2976
            /* Falls through. */
2977
2.00M
        case XML_REGEXP_INITNAME:
2978
2.00M
      ret = (IS_LETTER(codepoint) ||
2979
2.00M
       (codepoint == '_') || (codepoint == ':'));
2980
2.00M
      break;
2981
8.49k
        case XML_REGEXP_NOTNAMECHAR:
2982
8.49k
      neg = !neg;
2983
            /* Falls through. */
2984
12.4k
        case XML_REGEXP_NAMECHAR:
2985
12.4k
      ret = (IS_LETTER(codepoint) || IS_DIGIT(codepoint) ||
2986
12.4k
       (codepoint == '.') || (codepoint == '-') ||
2987
12.4k
       (codepoint == '_') || (codepoint == ':') ||
2988
12.4k
       IS_COMBINING(codepoint) || IS_EXTENDER(codepoint));
2989
12.4k
      break;
2990
714
        case XML_REGEXP_NOTDECIMAL:
2991
714
      neg = !neg;
2992
            /* Falls through. */
2993
2.44k
        case XML_REGEXP_DECIMAL:
2994
2.44k
      ret = xmlUCSIsCatNd(codepoint);
2995
2.44k
      break;
2996
1.86k
        case XML_REGEXP_REALCHAR:
2997
1.86k
      neg = !neg;
2998
            /* Falls through. */
2999
2.76k
        case XML_REGEXP_NOTREALCHAR:
3000
2.76k
      ret = xmlUCSIsCatP(codepoint);
3001
2.76k
      if (ret == 0)
3002
2.17k
    ret = xmlUCSIsCatZ(codepoint);
3003
2.76k
      if (ret == 0)
3004
2.17k
    ret = xmlUCSIsCatC(codepoint);
3005
2.76k
      break;
3006
1.27k
        case XML_REGEXP_LETTER:
3007
1.27k
      ret = xmlUCSIsCatL(codepoint);
3008
1.27k
      break;
3009
676
        case XML_REGEXP_LETTER_UPPERCASE:
3010
676
      ret = xmlUCSIsCatLu(codepoint);
3011
676
      break;
3012
621
        case XML_REGEXP_LETTER_LOWERCASE:
3013
621
      ret = xmlUCSIsCatLl(codepoint);
3014
621
      break;
3015
698
        case XML_REGEXP_LETTER_TITLECASE:
3016
698
      ret = xmlUCSIsCatLt(codepoint);
3017
698
      break;
3018
721
        case XML_REGEXP_LETTER_MODIFIER:
3019
721
      ret = xmlUCSIsCatLm(codepoint);
3020
721
      break;
3021
668
        case XML_REGEXP_LETTER_OTHERS:
3022
668
      ret = xmlUCSIsCatLo(codepoint);
3023
668
      break;
3024
1.35k
        case XML_REGEXP_MARK:
3025
1.35k
      ret = xmlUCSIsCatM(codepoint);
3026
1.35k
      break;
3027
531
        case XML_REGEXP_MARK_NONSPACING:
3028
531
      ret = xmlUCSIsCatMn(codepoint);
3029
531
      break;
3030
782
        case XML_REGEXP_MARK_SPACECOMBINING:
3031
782
      ret = xmlUCSIsCatMc(codepoint);
3032
782
      break;
3033
81.2k
        case XML_REGEXP_MARK_ENCLOSING:
3034
81.2k
      ret = xmlUCSIsCatMe(codepoint);
3035
81.2k
      break;
3036
1.41k
        case XML_REGEXP_NUMBER:
3037
1.41k
      ret = xmlUCSIsCatN(codepoint);
3038
1.41k
      break;
3039
595
        case XML_REGEXP_NUMBER_DECIMAL:
3040
595
      ret = xmlUCSIsCatNd(codepoint);
3041
595
      break;
3042
39.2k
        case XML_REGEXP_NUMBER_LETTER:
3043
39.2k
      ret = xmlUCSIsCatNl(codepoint);
3044
39.2k
      break;
3045
701
        case XML_REGEXP_NUMBER_OTHERS:
3046
701
      ret = xmlUCSIsCatNo(codepoint);
3047
701
      break;
3048
2.79k
        case XML_REGEXP_PUNCT:
3049
2.79k
      ret = xmlUCSIsCatP(codepoint);
3050
2.79k
      break;
3051
3.01k
        case XML_REGEXP_PUNCT_CONNECTOR:
3052
3.01k
      ret = xmlUCSIsCatPc(codepoint);
3053
3.01k
      break;
3054
676
        case XML_REGEXP_PUNCT_DASH:
3055
676
      ret = xmlUCSIsCatPd(codepoint);
3056
676
      break;
3057
890
        case XML_REGEXP_PUNCT_OPEN:
3058
890
      ret = xmlUCSIsCatPs(codepoint);
3059
890
      break;
3060
2.77k
        case XML_REGEXP_PUNCT_CLOSE:
3061
2.77k
      ret = xmlUCSIsCatPe(codepoint);
3062
2.77k
      break;
3063
13.8k
        case XML_REGEXP_PUNCT_INITQUOTE:
3064
13.8k
      ret = xmlUCSIsCatPi(codepoint);
3065
13.8k
      break;
3066
1.43k
        case XML_REGEXP_PUNCT_FINQUOTE:
3067
1.43k
      ret = xmlUCSIsCatPf(codepoint);
3068
1.43k
      break;
3069
556
        case XML_REGEXP_PUNCT_OTHERS:
3070
556
      ret = xmlUCSIsCatPo(codepoint);
3071
556
      break;
3072
1.28k
        case XML_REGEXP_SEPAR:
3073
1.28k
      ret = xmlUCSIsCatZ(codepoint);
3074
1.28k
      break;
3075
27.3k
        case XML_REGEXP_SEPAR_SPACE:
3076
27.3k
      ret = xmlUCSIsCatZs(codepoint);
3077
27.3k
      break;
3078
699
        case XML_REGEXP_SEPAR_LINE:
3079
699
      ret = xmlUCSIsCatZl(codepoint);
3080
699
      break;
3081
691
        case XML_REGEXP_SEPAR_PARA:
3082
691
      ret = xmlUCSIsCatZp(codepoint);
3083
691
      break;
3084
1.60k
        case XML_REGEXP_SYMBOL:
3085
1.60k
      ret = xmlUCSIsCatS(codepoint);
3086
1.60k
      break;
3087
701
        case XML_REGEXP_SYMBOL_MATH:
3088
701
      ret = xmlUCSIsCatSm(codepoint);
3089
701
      break;
3090
719
        case XML_REGEXP_SYMBOL_CURRENCY:
3091
719
      ret = xmlUCSIsCatSc(codepoint);
3092
719
      break;
3093
612
        case XML_REGEXP_SYMBOL_MODIFIER:
3094
612
      ret = xmlUCSIsCatSk(codepoint);
3095
612
      break;
3096
683
        case XML_REGEXP_SYMBOL_OTHERS:
3097
683
      ret = xmlUCSIsCatSo(codepoint);
3098
683
      break;
3099
3.30k
        case XML_REGEXP_OTHER:
3100
3.30k
      ret = xmlUCSIsCatC(codepoint);
3101
3.30k
      break;
3102
4.62k
        case XML_REGEXP_OTHER_CONTROL:
3103
4.62k
      ret = xmlUCSIsCatCc(codepoint);
3104
4.62k
      break;
3105
747
        case XML_REGEXP_OTHER_FORMAT:
3106
747
      ret = xmlUCSIsCatCf(codepoint);
3107
747
      break;
3108
116k
        case XML_REGEXP_OTHER_PRIVATE:
3109
116k
      ret = xmlUCSIsCatCo(codepoint);
3110
116k
      break;
3111
598
        case XML_REGEXP_OTHER_NA:
3112
      /* ret = xmlUCSIsCatCn(codepoint); */
3113
      /* Seems it doesn't exist anymore in recent Unicode releases */
3114
598
      ret = 0;
3115
598
      break;
3116
8.59k
        case XML_REGEXP_BLOCK_NAME:
3117
8.59k
      ret = xmlUCSIsBlock(codepoint, (const char *) blockName);
3118
8.59k
      break;
3119
6.68M
    }
3120
6.68M
    if (neg)
3121
16.4k
  return(!ret);
3122
6.66M
    return(ret);
3123
6.68M
}
3124
3125
static int
3126
50.7k
xmlRegCheckCharacter(xmlRegAtomPtr atom, int codepoint) {
3127
50.7k
    int i, ret = 0;
3128
50.7k
    xmlRegRangePtr range;
3129
3130
50.7k
    if ((atom == NULL) || (!IS_CHAR(codepoint)))
3131
4.38k
  return(-1);
3132
3133
46.3k
    switch (atom->type) {
3134
0
        case XML_REGEXP_SUBREG:
3135
0
        case XML_REGEXP_EPSILON:
3136
0
      return(-1);
3137
0
        case XML_REGEXP_CHARVAL:
3138
0
            return(codepoint == atom->codepoint);
3139
9.59k
        case XML_REGEXP_RANGES: {
3140
9.59k
      int accept = 0;
3141
3142
19.4k
      for (i = 0;i < atom->nbRanges;i++) {
3143
10.2k
    range = atom->ranges[i];
3144
10.2k
    if (range->neg == 2) {
3145
507
        ret = xmlRegCheckCharacterRange(range->type, codepoint,
3146
507
            0, range->start, range->end,
3147
507
            range->blockName);
3148
507
        if (ret != 0)
3149
206
      return(0); /* excluded char */
3150
9.76k
    } else if (range->neg) {
3151
543
        ret = xmlRegCheckCharacterRange(range->type, codepoint,
3152
543
            0, range->start, range->end,
3153
543
            range->blockName);
3154
543
        if (ret == 0)
3155
312
            accept = 1;
3156
231
        else
3157
231
            return(0);
3158
9.22k
    } else {
3159
9.22k
        ret = xmlRegCheckCharacterRange(range->type, codepoint,
3160
9.22k
            0, range->start, range->end,
3161
9.22k
            range->blockName);
3162
9.22k
        if (ret != 0)
3163
2.22k
      accept = 1; /* might still be excluded */
3164
9.22k
    }
3165
10.2k
      }
3166
9.15k
      return(accept);
3167
9.59k
  }
3168
0
        case XML_REGEXP_STRING:
3169
0
      return(-1);
3170
0
        case XML_REGEXP_ANYCHAR:
3171
1.03k
        case XML_REGEXP_ANYSPACE:
3172
1.33k
        case XML_REGEXP_NOTSPACE:
3173
2.71k
        case XML_REGEXP_INITNAME:
3174
3.62k
        case XML_REGEXP_NOTINITNAME:
3175
5.21k
        case XML_REGEXP_NAMECHAR:
3176
6.97k
        case XML_REGEXP_NOTNAMECHAR:
3177
8.17k
        case XML_REGEXP_DECIMAL:
3178
8.58k
        case XML_REGEXP_NOTDECIMAL:
3179
9.62k
        case XML_REGEXP_REALCHAR:
3180
10.0k
        case XML_REGEXP_NOTREALCHAR:
3181
10.5k
        case XML_REGEXP_LETTER:
3182
10.8k
        case XML_REGEXP_LETTER_UPPERCASE:
3183
11.1k
        case XML_REGEXP_LETTER_LOWERCASE:
3184
11.4k
        case XML_REGEXP_LETTER_TITLECASE:
3185
11.8k
        case XML_REGEXP_LETTER_MODIFIER:
3186
12.1k
        case XML_REGEXP_LETTER_OTHERS:
3187
12.8k
        case XML_REGEXP_MARK:
3188
13.1k
        case XML_REGEXP_MARK_NONSPACING:
3189
13.4k
        case XML_REGEXP_MARK_SPACECOMBINING:
3190
14.6k
        case XML_REGEXP_MARK_ENCLOSING:
3191
15.3k
        case XML_REGEXP_NUMBER:
3192
15.6k
        case XML_REGEXP_NUMBER_DECIMAL:
3193
17.4k
        case XML_REGEXP_NUMBER_LETTER:
3194
17.8k
        case XML_REGEXP_NUMBER_OTHERS:
3195
18.5k
        case XML_REGEXP_PUNCT:
3196
20.7k
        case XML_REGEXP_PUNCT_CONNECTOR:
3197
21.0k
        case XML_REGEXP_PUNCT_DASH:
3198
21.3k
        case XML_REGEXP_PUNCT_OPEN:
3199
21.6k
        case XML_REGEXP_PUNCT_CLOSE:
3200
23.1k
        case XML_REGEXP_PUNCT_INITQUOTE:
3201
24.3k
        case XML_REGEXP_PUNCT_FINQUOTE:
3202
24.6k
        case XML_REGEXP_PUNCT_OTHERS:
3203
25.3k
        case XML_REGEXP_SEPAR:
3204
27.6k
        case XML_REGEXP_SEPAR_SPACE:
3205
27.9k
        case XML_REGEXP_SEPAR_LINE:
3206
28.3k
        case XML_REGEXP_SEPAR_PARA:
3207
29.1k
        case XML_REGEXP_SYMBOL:
3208
29.4k
        case XML_REGEXP_SYMBOL_MATH:
3209
29.8k
        case XML_REGEXP_SYMBOL_CURRENCY:
3210
30.1k
        case XML_REGEXP_SYMBOL_MODIFIER:
3211
30.5k
        case XML_REGEXP_SYMBOL_OTHERS:
3212
32.9k
        case XML_REGEXP_OTHER:
3213
33.4k
        case XML_REGEXP_OTHER_CONTROL:
3214
33.8k
        case XML_REGEXP_OTHER_FORMAT:
3215
35.5k
        case XML_REGEXP_OTHER_PRIVATE:
3216
35.8k
        case XML_REGEXP_OTHER_NA:
3217
36.7k
  case XML_REGEXP_BLOCK_NAME:
3218
36.7k
      ret = xmlRegCheckCharacterRange(atom->type, codepoint, 0, 0, 0,
3219
36.7k
                                (const xmlChar *)atom->valuep);
3220
36.7k
      if (atom->neg)
3221
6.56k
    ret = !ret;
3222
36.7k
      break;
3223
46.3k
    }
3224
36.7k
    return(ret);
3225
46.3k
}
3226
3227
/************************************************************************
3228
 *                  *
3229
 *  Saving and restoring state of an execution context    *
3230
 *                  *
3231
 ************************************************************************/
3232
3233
static void
3234
0
xmlFARegExecSave(xmlRegExecCtxtPtr exec) {
3235
0
#ifdef MAX_PUSH
3236
0
    if (exec->nbPush > MAX_PUSH) {
3237
0
        exec->status = XML_REGEXP_INTERNAL_LIMIT;
3238
0
        return;
3239
0
    }
3240
0
    exec->nbPush++;
3241
0
#endif
3242
3243
0
    if (exec->nbRollbacks >= exec->maxRollbacks) {
3244
0
  xmlRegExecRollback *tmp;
3245
0
        int newSize;
3246
0
  int len = exec->nbRollbacks;
3247
3248
0
        newSize = xmlGrowCapacity(exec->maxRollbacks, sizeof(tmp[0]),
3249
0
                                  4, XML_MAX_ITEMS);
3250
0
  if (newSize < 0) {
3251
0
            exec->status = XML_REGEXP_OUT_OF_MEMORY;
3252
0
      return;
3253
0
  }
3254
0
  tmp = xmlRealloc(exec->rollbacks, newSize * sizeof(tmp[0]));
3255
0
  if (tmp == NULL) {
3256
0
            exec->status = XML_REGEXP_OUT_OF_MEMORY;
3257
0
      return;
3258
0
  }
3259
0
  exec->rollbacks = tmp;
3260
0
  exec->maxRollbacks = newSize;
3261
0
  tmp = &exec->rollbacks[len];
3262
0
  memset(tmp, 0, (exec->maxRollbacks - len) * sizeof(xmlRegExecRollback));
3263
0
    }
3264
0
    exec->rollbacks[exec->nbRollbacks].state = exec->state;
3265
0
    exec->rollbacks[exec->nbRollbacks].index = exec->index;
3266
0
    exec->rollbacks[exec->nbRollbacks].nextbranch = exec->transno + 1;
3267
0
    if (exec->comp->nbCounters > 0) {
3268
0
  if (exec->rollbacks[exec->nbRollbacks].counts == NULL) {
3269
0
      exec->rollbacks[exec->nbRollbacks].counts = (int *)
3270
0
    xmlMalloc(exec->comp->nbCounters * sizeof(int));
3271
0
      if (exec->rollbacks[exec->nbRollbacks].counts == NULL) {
3272
0
    exec->status = XML_REGEXP_OUT_OF_MEMORY;
3273
0
    return;
3274
0
      }
3275
0
  }
3276
0
  memcpy(exec->rollbacks[exec->nbRollbacks].counts, exec->counts,
3277
0
         exec->comp->nbCounters * sizeof(int));
3278
0
    }
3279
0
    exec->nbRollbacks++;
3280
0
}
3281
3282
static void
3283
0
xmlFARegExecRollBack(xmlRegExecCtxtPtr exec) {
3284
0
    if (exec->status != XML_REGEXP_OK)
3285
0
        return;
3286
0
    if (exec->nbRollbacks <= 0) {
3287
0
  exec->status = XML_REGEXP_NOT_FOUND;
3288
0
  return;
3289
0
    }
3290
0
    exec->nbRollbacks--;
3291
0
    exec->state = exec->rollbacks[exec->nbRollbacks].state;
3292
0
    exec->index = exec->rollbacks[exec->nbRollbacks].index;
3293
0
    exec->transno = exec->rollbacks[exec->nbRollbacks].nextbranch;
3294
0
    if (exec->comp->nbCounters > 0) {
3295
0
  if (exec->rollbacks[exec->nbRollbacks].counts == NULL) {
3296
0
      exec->status = XML_REGEXP_INTERNAL_ERROR;
3297
0
      return;
3298
0
  }
3299
0
  if (exec->counts) {
3300
0
      memcpy(exec->counts, exec->rollbacks[exec->nbRollbacks].counts,
3301
0
         exec->comp->nbCounters * sizeof(int));
3302
0
  }
3303
0
    }
3304
0
}
3305
3306
/************************************************************************
3307
 *                  *
3308
 *  Verifier, running an input against a compiled regexp    *
3309
 *                  *
3310
 ************************************************************************/
3311
3312
static int
3313
0
xmlFARegExec(xmlRegexpPtr comp, const xmlChar *content) {
3314
0
    xmlRegExecCtxt execval;
3315
0
    xmlRegExecCtxtPtr exec = &execval;
3316
0
    int ret, codepoint = 0, len, deter;
3317
3318
0
    exec->inputString = content;
3319
0
    exec->index = 0;
3320
0
    exec->nbPush = 0;
3321
0
    exec->determinist = 1;
3322
0
    exec->maxRollbacks = 0;
3323
0
    exec->nbRollbacks = 0;
3324
0
    exec->rollbacks = NULL;
3325
0
    exec->status = XML_REGEXP_OK;
3326
0
    exec->comp = comp;
3327
0
    exec->state = comp->states[0];
3328
0
    exec->transno = 0;
3329
0
    exec->transcount = 0;
3330
0
    exec->inputStack = NULL;
3331
0
    exec->inputStackMax = 0;
3332
0
    if (comp->nbCounters > 0) {
3333
0
  exec->counts = (int *) xmlMalloc(comp->nbCounters * sizeof(int));
3334
0
  if (exec->counts == NULL) {
3335
0
      return(XML_REGEXP_OUT_OF_MEMORY);
3336
0
  }
3337
0
        memset(exec->counts, 0, comp->nbCounters * sizeof(int));
3338
0
    } else
3339
0
  exec->counts = NULL;
3340
0
    while ((exec->status == XML_REGEXP_OK) && (exec->state != NULL) &&
3341
0
     ((exec->inputString[exec->index] != 0) ||
3342
0
      ((exec->state != NULL) &&
3343
0
       (exec->state->type != XML_REGEXP_FINAL_STATE)))) {
3344
0
  xmlRegTransPtr trans;
3345
0
  xmlRegAtomPtr atom;
3346
3347
  /*
3348
   * If end of input on non-terminal state, rollback, however we may
3349
   * still have epsilon like transition for counted transitions
3350
   * on counters, in that case don't break too early.  Additionally,
3351
   * if we are working on a range like "AB{0,2}", where B is not present,
3352
   * we don't want to break.
3353
   */
3354
0
  len = 1;
3355
0
  if ((exec->inputString[exec->index] == 0) && (exec->counts == NULL)) {
3356
      /*
3357
       * if there is a transition, we must check if
3358
       *  atom allows minOccurs of 0
3359
       */
3360
0
      if (exec->transno < exec->state->nbTrans) {
3361
0
          trans = &exec->state->trans[exec->transno];
3362
0
    if (trans->to >=0) {
3363
0
        atom = trans->atom;
3364
0
        if (!((atom->min == 0) && (atom->max > 0)))
3365
0
            goto rollback;
3366
0
    }
3367
0
      } else
3368
0
          goto rollback;
3369
0
  }
3370
3371
0
  exec->transcount = 0;
3372
0
  for (;exec->transno < exec->state->nbTrans;exec->transno++) {
3373
0
      trans = &exec->state->trans[exec->transno];
3374
0
      if (trans->to < 0)
3375
0
    continue;
3376
0
      atom = trans->atom;
3377
0
      ret = 0;
3378
0
      deter = 1;
3379
0
      if (trans->count >= 0) {
3380
0
    int count;
3381
0
    xmlRegCounterPtr counter;
3382
3383
0
    if (exec->counts == NULL) {
3384
0
        exec->status = XML_REGEXP_INTERNAL_ERROR;
3385
0
        goto error;
3386
0
    }
3387
    /*
3388
     * A counted transition.
3389
     */
3390
3391
0
    count = exec->counts[trans->count];
3392
0
    counter = &exec->comp->counters[trans->count];
3393
0
    ret = ((count >= counter->min) && (count <= counter->max));
3394
0
    if ((ret) && (counter->min != counter->max))
3395
0
        deter = 0;
3396
0
      } else if (atom == NULL) {
3397
0
    exec->status = XML_REGEXP_INTERNAL_ERROR;
3398
0
    break;
3399
0
      } else if (exec->inputString[exec->index] != 0) {
3400
0
                len = 4;
3401
0
                codepoint = xmlGetUTF8Char(&exec->inputString[exec->index],
3402
0
                                           &len);
3403
0
                if (codepoint < 0) {
3404
0
                    exec->status = XML_REGEXP_INVALID_UTF8;
3405
0
                    goto error;
3406
0
                }
3407
0
    ret = xmlRegCheckCharacter(atom, codepoint);
3408
0
    if ((ret == 1) && (atom->min >= 0) && (atom->max > 0)) {
3409
0
        xmlRegStatePtr to = comp->states[trans->to];
3410
3411
        /*
3412
         * this is a multiple input sequence
3413
         * If there is a counter associated increment it now.
3414
         * do not increment if the counter is already over the
3415
         * maximum limit in which case get to next transition
3416
         */
3417
0
        if (trans->counter >= 0) {
3418
0
      xmlRegCounterPtr counter;
3419
3420
0
      if ((exec->counts == NULL) ||
3421
0
          (exec->comp == NULL) ||
3422
0
          (exec->comp->counters == NULL)) {
3423
0
          exec->status = XML_REGEXP_INTERNAL_ERROR;
3424
0
          goto error;
3425
0
      }
3426
0
      counter = &exec->comp->counters[trans->counter];
3427
0
      if (exec->counts[trans->counter] >= counter->max)
3428
0
          continue; /* for loop on transitions */
3429
0
                    }
3430
                    /* Save before incrementing */
3431
0
        if (exec->state->nbTrans > exec->transno + 1) {
3432
0
      xmlFARegExecSave(exec);
3433
0
                        if (exec->status != XML_REGEXP_OK)
3434
0
                            goto error;
3435
0
        }
3436
0
        if (trans->counter >= 0) {
3437
0
      exec->counts[trans->counter]++;
3438
0
        }
3439
0
        exec->transcount = 1;
3440
0
        do {
3441
      /*
3442
       * Try to progress as much as possible on the input
3443
       */
3444
0
      if (exec->transcount == atom->max) {
3445
0
          break;
3446
0
      }
3447
0
      exec->index += len;
3448
      /*
3449
       * End of input: stop here
3450
       */
3451
0
      if (exec->inputString[exec->index] == 0) {
3452
0
          exec->index -= len;
3453
0
          break;
3454
0
      }
3455
0
      if (exec->transcount >= atom->min) {
3456
0
          int transno = exec->transno;
3457
0
          xmlRegStatePtr state = exec->state;
3458
3459
          /*
3460
           * The transition is acceptable save it
3461
           */
3462
0
          exec->transno = -1; /* trick */
3463
0
          exec->state = to;
3464
0
          xmlFARegExecSave(exec);
3465
0
                            if (exec->status != XML_REGEXP_OK)
3466
0
                                goto error;
3467
0
          exec->transno = transno;
3468
0
          exec->state = state;
3469
0
      }
3470
0
                        len = 4;
3471
0
                        codepoint = xmlGetUTF8Char(
3472
0
                                &exec->inputString[exec->index], &len);
3473
0
                        if (codepoint < 0) {
3474
0
                            exec->status = XML_REGEXP_INVALID_UTF8;
3475
0
                            goto error;
3476
0
                        }
3477
0
      ret = xmlRegCheckCharacter(atom, codepoint);
3478
0
      exec->transcount++;
3479
0
        } while (ret == 1);
3480
0
        if (exec->transcount < atom->min)
3481
0
      ret = 0;
3482
3483
        /*
3484
         * If the last check failed but one transition was found
3485
         * possible, rollback
3486
         */
3487
0
        if (ret < 0)
3488
0
      ret = 0;
3489
0
        if (ret == 0) {
3490
0
      goto rollback;
3491
0
        }
3492
0
        if (trans->counter >= 0) {
3493
0
      if (exec->counts == NULL) {
3494
0
          exec->status = XML_REGEXP_INTERNAL_ERROR;
3495
0
          goto error;
3496
0
      }
3497
0
      exec->counts[trans->counter]--;
3498
0
        }
3499
0
    } else if ((ret == 0) && (atom->min == 0) && (atom->max > 0)) {
3500
        /*
3501
         * we don't match on the codepoint, but minOccurs of 0
3502
         * says that's ok.  Setting len to 0 inhibits stepping
3503
         * over the codepoint.
3504
         */
3505
0
        exec->transcount = 1;
3506
0
        len = 0;
3507
0
        ret = 1;
3508
0
    }
3509
0
      } else if ((atom->min == 0) && (atom->max > 0)) {
3510
          /* another spot to match when minOccurs is 0 */
3511
0
    exec->transcount = 1;
3512
0
    len = 0;
3513
0
    ret = 1;
3514
0
      }
3515
0
      if (ret == 1) {
3516
0
    if ((trans->nd == 1) ||
3517
0
        ((trans->count >= 0) && (deter == 0) &&
3518
0
         (exec->state->nbTrans > exec->transno + 1))) {
3519
0
        xmlFARegExecSave(exec);
3520
0
                    if (exec->status != XML_REGEXP_OK)
3521
0
                        goto error;
3522
0
    }
3523
0
    if (trans->counter >= 0) {
3524
0
        xmlRegCounterPtr counter;
3525
3526
                    /* make sure we don't go over the counter maximum value */
3527
0
        if ((exec->counts == NULL) ||
3528
0
      (exec->comp == NULL) ||
3529
0
      (exec->comp->counters == NULL)) {
3530
0
      exec->status = XML_REGEXP_INTERNAL_ERROR;
3531
0
      goto error;
3532
0
        }
3533
0
        counter = &exec->comp->counters[trans->counter];
3534
0
        if (exec->counts[trans->counter] >= counter->max)
3535
0
      continue; /* for loop on transitions */
3536
0
        exec->counts[trans->counter]++;
3537
0
    }
3538
0
    if ((trans->count >= 0) &&
3539
0
        (trans->count < REGEXP_ALL_COUNTER)) {
3540
0
        if (exec->counts == NULL) {
3541
0
            exec->status = XML_REGEXP_INTERNAL_ERROR;
3542
0
      goto error;
3543
0
        }
3544
0
        exec->counts[trans->count] = 0;
3545
0
    }
3546
0
    exec->state = comp->states[trans->to];
3547
0
    exec->transno = 0;
3548
0
    if (trans->atom != NULL) {
3549
0
        exec->index += len;
3550
0
    }
3551
0
    goto progress;
3552
0
      } else if (ret < 0) {
3553
0
    exec->status = XML_REGEXP_INTERNAL_ERROR;
3554
0
    break;
3555
0
      }
3556
0
  }
3557
0
  if ((exec->transno != 0) || (exec->state->nbTrans == 0)) {
3558
0
rollback:
3559
      /*
3560
       * Failed to find a way out
3561
       */
3562
0
      exec->determinist = 0;
3563
0
      xmlFARegExecRollBack(exec);
3564
0
  }
3565
0
progress:
3566
0
  continue;
3567
0
    }
3568
0
error:
3569
0
    if (exec->rollbacks != NULL) {
3570
0
  if (exec->counts != NULL) {
3571
0
      int i;
3572
3573
0
      for (i = 0;i < exec->maxRollbacks;i++)
3574
0
    if (exec->rollbacks[i].counts != NULL)
3575
0
        xmlFree(exec->rollbacks[i].counts);
3576
0
  }
3577
0
  xmlFree(exec->rollbacks);
3578
0
    }
3579
0
    if (exec->state == NULL)
3580
0
        return(XML_REGEXP_INTERNAL_ERROR);
3581
0
    if (exec->counts != NULL)
3582
0
  xmlFree(exec->counts);
3583
0
    if (exec->status == XML_REGEXP_OK)
3584
0
  return(1);
3585
0
    if (exec->status == XML_REGEXP_NOT_FOUND)
3586
0
  return(0);
3587
0
    return(exec->status);
3588
0
}
3589
3590
/************************************************************************
3591
 *                  *
3592
 *  Progressive interface to the verifier one atom at a time  *
3593
 *                  *
3594
 ************************************************************************/
3595
3596
/**
3597
 * Build a context used for progressive evaluation of a regexp.
3598
 *
3599
 * @deprecated Internal function, don't use.
3600
 *
3601
 * @param comp  a precompiled regular expression
3602
 * @param callback  a callback function used for handling progresses in the
3603
 *            automata matching phase
3604
 * @param data  the context data associated to the callback in this context
3605
 * @returns the new context
3606
 */
3607
xmlRegExecCtxt *
3608
0
xmlRegNewExecCtxt(xmlRegexp *comp, xmlRegExecCallbacks callback, void *data) {
3609
0
    xmlRegExecCtxtPtr exec;
3610
3611
0
    if (comp == NULL)
3612
0
  return(NULL);
3613
0
    if ((comp->compact == NULL) && (comp->states == NULL))
3614
0
        return(NULL);
3615
0
    exec = (xmlRegExecCtxtPtr) xmlMalloc(sizeof(xmlRegExecCtxt));
3616
0
    if (exec == NULL)
3617
0
  return(NULL);
3618
0
    memset(exec, 0, sizeof(xmlRegExecCtxt));
3619
0
    exec->inputString = NULL;
3620
0
    exec->index = 0;
3621
0
    exec->determinist = 1;
3622
0
    exec->maxRollbacks = 0;
3623
0
    exec->nbRollbacks = 0;
3624
0
    exec->rollbacks = NULL;
3625
0
    exec->status = XML_REGEXP_OK;
3626
0
    exec->comp = comp;
3627
0
    if (comp->compact == NULL)
3628
0
  exec->state = comp->states[0];
3629
0
    exec->transno = 0;
3630
0
    exec->transcount = 0;
3631
0
    exec->callback = callback;
3632
0
    exec->data = data;
3633
0
    if (comp->nbCounters > 0) {
3634
        /*
3635
   * For error handling, exec->counts is allocated twice the size
3636
   * the second half is used to store the data in case of rollback
3637
   */
3638
0
  exec->counts = (int *) xmlMalloc(comp->nbCounters * sizeof(int)
3639
0
                                   * 2);
3640
0
  if (exec->counts == NULL) {
3641
0
      xmlFree(exec);
3642
0
      return(NULL);
3643
0
  }
3644
0
        memset(exec->counts, 0, comp->nbCounters * sizeof(int) * 2);
3645
0
  exec->errCounts = &exec->counts[comp->nbCounters];
3646
0
    } else {
3647
0
  exec->counts = NULL;
3648
0
  exec->errCounts = NULL;
3649
0
    }
3650
0
    exec->inputStackMax = 0;
3651
0
    exec->inputStackNr = 0;
3652
0
    exec->inputStack = NULL;
3653
0
    exec->errStateNo = -1;
3654
0
    exec->errString = NULL;
3655
0
    exec->nbPush = 0;
3656
0
    return(exec);
3657
0
}
3658
3659
/**
3660
 * Free the structures associated to a regular expression evaluation context.
3661
 *
3662
 * @deprecated Internal function, don't use.
3663
 *
3664
 * @param exec  a regular expression evaluation context
3665
 */
3666
void
3667
0
xmlRegFreeExecCtxt(xmlRegExecCtxt *exec) {
3668
0
    if (exec == NULL)
3669
0
  return;
3670
3671
0
    if (exec->rollbacks != NULL) {
3672
0
  if (exec->counts != NULL) {
3673
0
      int i;
3674
3675
0
      for (i = 0;i < exec->maxRollbacks;i++)
3676
0
    if (exec->rollbacks[i].counts != NULL)
3677
0
        xmlFree(exec->rollbacks[i].counts);
3678
0
  }
3679
0
  xmlFree(exec->rollbacks);
3680
0
    }
3681
0
    if (exec->counts != NULL)
3682
0
  xmlFree(exec->counts);
3683
0
    if (exec->inputStack != NULL) {
3684
0
  int i;
3685
3686
0
  for (i = 0;i < exec->inputStackNr;i++) {
3687
0
      if (exec->inputStack[i].value != NULL)
3688
0
    xmlFree(exec->inputStack[i].value);
3689
0
  }
3690
0
  xmlFree(exec->inputStack);
3691
0
    }
3692
0
    if (exec->errString != NULL)
3693
0
        xmlFree(exec->errString);
3694
0
    xmlFree(exec);
3695
0
}
3696
3697
static int
3698
0
xmlRegExecSetErrString(xmlRegExecCtxtPtr exec, const xmlChar *value) {
3699
0
    if (exec->errString != NULL)
3700
0
        xmlFree(exec->errString);
3701
0
    if (value == NULL) {
3702
0
        exec->errString = NULL;
3703
0
    } else {
3704
0
        exec->errString = xmlStrdup(value);
3705
0
        if (exec->errString == NULL) {
3706
0
            exec->status = XML_REGEXP_OUT_OF_MEMORY;
3707
0
            return(-1);
3708
0
        }
3709
0
    }
3710
0
    return(0);
3711
0
}
3712
3713
static void
3714
xmlFARegExecSaveInputString(xmlRegExecCtxtPtr exec, const xmlChar *value,
3715
0
                      void *data) {
3716
0
    if (exec->inputStackNr + 1 >= exec->inputStackMax) {
3717
0
  xmlRegInputTokenPtr tmp;
3718
0
        int newSize;
3719
3720
0
        newSize = xmlGrowCapacity(exec->inputStackMax, sizeof(tmp[0]),
3721
0
                                  4, XML_MAX_ITEMS);
3722
0
  if (newSize < 0) {
3723
0
            exec->status = XML_REGEXP_OUT_OF_MEMORY;
3724
0
      return;
3725
0
  }
3726
0
#ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
3727
0
        if (newSize < 2)
3728
0
            newSize = 2;
3729
0
#endif
3730
0
  tmp = xmlRealloc(exec->inputStack, newSize * sizeof(tmp[0]));
3731
0
  if (tmp == NULL) {
3732
0
            exec->status = XML_REGEXP_OUT_OF_MEMORY;
3733
0
      return;
3734
0
  }
3735
0
  exec->inputStack = tmp;
3736
0
  exec->inputStackMax = newSize;
3737
0
    }
3738
0
    if (value == NULL) {
3739
0
        exec->inputStack[exec->inputStackNr].value = NULL;
3740
0
    } else {
3741
0
        exec->inputStack[exec->inputStackNr].value = xmlStrdup(value);
3742
0
        if (exec->inputStack[exec->inputStackNr].value == NULL) {
3743
0
            exec->status = XML_REGEXP_OUT_OF_MEMORY;
3744
0
            return;
3745
0
        }
3746
0
    }
3747
0
    exec->inputStack[exec->inputStackNr].data = data;
3748
0
    exec->inputStackNr++;
3749
0
    exec->inputStack[exec->inputStackNr].value = NULL;
3750
0
    exec->inputStack[exec->inputStackNr].data = NULL;
3751
0
}
3752
3753
/**
3754
 * Checks if both strings are equal or have the same content. "*"
3755
 * can be used as a wildcard in `valStr`; "|" is used as a separator of
3756
 * substrings in both `expStr` and `valStr`.
3757
 *
3758
 * @param expStr  the string to be evaluated
3759
 * @param valStr  the validation string
3760
 * @returns 1 if the comparison is satisfied and the number of substrings
3761
 * is equal, 0 otherwise.
3762
 */
3763
3764
static int
3765
0
xmlRegStrEqualWildcard(const xmlChar *expStr, const xmlChar *valStr) {
3766
0
    if (expStr == valStr) return(1);
3767
0
    if (expStr == NULL) return(0);
3768
0
    if (valStr == NULL) return(0);
3769
0
    do {
3770
  /*
3771
  * Eval if we have a wildcard for the current item.
3772
  */
3773
0
        if (*expStr != *valStr) {
3774
      /* if one of them starts with a wildcard make valStr be it */
3775
0
      if (*valStr == '*') {
3776
0
          const xmlChar *tmp;
3777
3778
0
    tmp = valStr;
3779
0
    valStr = expStr;
3780
0
    expStr = tmp;
3781
0
      }
3782
0
      if ((*valStr != 0) && (*expStr != 0) && (*expStr++ == '*')) {
3783
0
    do {
3784
0
        if (*valStr == XML_REG_STRING_SEPARATOR)
3785
0
      break;
3786
0
        valStr++;
3787
0
    } while (*valStr != 0);
3788
0
    continue;
3789
0
      } else
3790
0
    return(0);
3791
0
  }
3792
0
  expStr++;
3793
0
  valStr++;
3794
0
    } while (*valStr != 0);
3795
0
    if (*expStr != 0)
3796
0
  return (0);
3797
0
    else
3798
0
  return (1);
3799
0
}
3800
3801
/**
3802
 * Push one input token in the execution context
3803
 *
3804
 * @param exec  a regexp execution context
3805
 * @param comp  the precompiled exec with a compact table
3806
 * @param value  a string token input
3807
 * @param data  data associated to the token to reuse in callbacks
3808
 * @returns 1 if the regexp reached a final state, 0 if non-final, and
3809
 *     a negative value in case of error.
3810
 */
3811
static int
3812
xmlRegCompactPushString(xmlRegExecCtxtPtr exec,
3813
                  xmlRegexpPtr comp,
3814
                  const xmlChar *value,
3815
0
                  void *data) {
3816
0
    int state = exec->index;
3817
0
    int i, target;
3818
3819
0
    if ((comp == NULL) || (comp->compact == NULL) || (comp->stringMap == NULL))
3820
0
  return(-1);
3821
3822
0
    if (value == NULL) {
3823
  /*
3824
   * are we at a final state ?
3825
   */
3826
0
  if (comp->compact[state * (comp->nbstrings + 1)] ==
3827
0
            XML_REGEXP_FINAL_STATE)
3828
0
      return(1);
3829
0
  return(0);
3830
0
    }
3831
3832
    /*
3833
     * Examine all outside transitions from current state
3834
     */
3835
0
    for (i = 0;i < comp->nbstrings;i++) {
3836
0
  target = comp->compact[state * (comp->nbstrings + 1) + i + 1];
3837
0
  if ((target > 0) && (target <= comp->nbstates)) {
3838
0
      target--; /* to avoid 0 */
3839
0
      if (xmlRegStrEqualWildcard(comp->stringMap[i], value)) {
3840
0
    exec->index = target;
3841
0
    if ((exec->callback != NULL) && (comp->transdata != NULL)) {
3842
0
        exec->callback(exec->data, value,
3843
0
        comp->transdata[state * comp->nbstrings + i], data);
3844
0
    }
3845
0
    if (comp->compact[target * (comp->nbstrings + 1)] ==
3846
0
        XML_REGEXP_SINK_STATE)
3847
0
        goto error;
3848
3849
0
    if (comp->compact[target * (comp->nbstrings + 1)] ==
3850
0
        XML_REGEXP_FINAL_STATE)
3851
0
        return(1);
3852
0
    return(0);
3853
0
      }
3854
0
  }
3855
0
    }
3856
    /*
3857
     * Failed to find an exit transition out from current state for the
3858
     * current token
3859
     */
3860
0
error:
3861
0
    exec->errStateNo = state;
3862
0
    exec->status = XML_REGEXP_NOT_FOUND;
3863
0
    xmlRegExecSetErrString(exec, value);
3864
0
    return(exec->status);
3865
0
}
3866
3867
/**
3868
 * Push one input token in the execution context
3869
 *
3870
 * @param exec  a regexp execution context or NULL to indicate the end
3871
 * @param value  a string token input
3872
 * @param data  data associated to the token to reuse in callbacks
3873
 * @param compound  value was assembled from 2 strings
3874
 * @returns 1 if the regexp reached a final state, 0 if non-final, and
3875
 *     a negative value in case of error.
3876
 */
3877
static int
3878
xmlRegExecPushStringInternal(xmlRegExecCtxtPtr exec, const xmlChar *value,
3879
0
                       void *data, int compound) {
3880
0
    xmlRegTransPtr trans;
3881
0
    xmlRegAtomPtr atom;
3882
0
    int ret;
3883
0
    int final = 0;
3884
0
    int progress = 1;
3885
3886
0
    if (exec == NULL)
3887
0
  return(-1);
3888
0
    if (exec->comp == NULL)
3889
0
  return(-1);
3890
0
    if (exec->status != XML_REGEXP_OK)
3891
0
  return(exec->status);
3892
3893
0
    if (exec->comp->compact != NULL)
3894
0
  return(xmlRegCompactPushString(exec, exec->comp, value, data));
3895
3896
0
    if (value == NULL) {
3897
0
        if (exec->state->type == XML_REGEXP_FINAL_STATE)
3898
0
      return(1);
3899
0
  final = 1;
3900
0
    }
3901
3902
    /*
3903
     * If we have an active rollback stack push the new value there
3904
     * and get back to where we were left
3905
     */
3906
0
    if ((value != NULL) && (exec->inputStackNr > 0)) {
3907
0
  xmlFARegExecSaveInputString(exec, value, data);
3908
0
  value = exec->inputStack[exec->index].value;
3909
0
  data = exec->inputStack[exec->index].data;
3910
0
    }
3911
3912
0
    while ((exec->status == XML_REGEXP_OK) &&
3913
0
     ((value != NULL) ||
3914
0
      ((final == 1) &&
3915
0
       (exec->state->type != XML_REGEXP_FINAL_STATE)))) {
3916
3917
  /*
3918
   * End of input on non-terminal state, rollback, however we may
3919
   * still have epsilon like transition for counted transitions
3920
   * on counters, in that case don't break too early.
3921
   */
3922
0
  if ((value == NULL) && (exec->counts == NULL))
3923
0
      goto rollback;
3924
3925
0
  exec->transcount = 0;
3926
0
  for (;exec->transno < exec->state->nbTrans;exec->transno++) {
3927
0
      trans = &exec->state->trans[exec->transno];
3928
0
      if (trans->to < 0)
3929
0
    continue;
3930
0
      atom = trans->atom;
3931
0
      ret = 0;
3932
0
      if (trans->count == REGEXP_ALL_LAX_COUNTER) {
3933
0
    int i;
3934
0
    int count;
3935
0
    xmlRegTransPtr t;
3936
0
    xmlRegCounterPtr counter;
3937
3938
0
    ret = 0;
3939
3940
    /*
3941
     * Check all counted transitions from the current state
3942
     */
3943
0
    if ((value == NULL) && (final)) {
3944
0
        ret = 1;
3945
0
    } else if (value != NULL) {
3946
0
        for (i = 0;i < exec->state->nbTrans;i++) {
3947
0
      t = &exec->state->trans[i];
3948
0
      if ((t->counter < 0) || (t == trans))
3949
0
          continue;
3950
0
      counter = &exec->comp->counters[t->counter];
3951
0
      count = exec->counts[t->counter];
3952
0
      if ((count < counter->max) &&
3953
0
                (t->atom != NULL) &&
3954
0
          (xmlStrEqual(value, t->atom->valuep))) {
3955
0
          ret = 0;
3956
0
          break;
3957
0
      }
3958
0
      if ((count >= counter->min) &&
3959
0
          (count < counter->max) &&
3960
0
          (t->atom != NULL) &&
3961
0
          (xmlStrEqual(value, t->atom->valuep))) {
3962
0
          ret = 1;
3963
0
          break;
3964
0
      }
3965
0
        }
3966
0
    }
3967
0
      } else if (trans->count == REGEXP_ALL_COUNTER) {
3968
0
    int i;
3969
0
    int count;
3970
0
    xmlRegTransPtr t;
3971
0
    xmlRegCounterPtr counter;
3972
3973
0
    ret = 1;
3974
3975
    /*
3976
     * Check all counted transitions from the current state
3977
     */
3978
0
    for (i = 0;i < exec->state->nbTrans;i++) {
3979
0
                    t = &exec->state->trans[i];
3980
0
        if ((t->counter < 0) || (t == trans))
3981
0
      continue;
3982
0
                    counter = &exec->comp->counters[t->counter];
3983
0
        count = exec->counts[t->counter];
3984
0
        if ((count < counter->min) || (count > counter->max)) {
3985
0
      ret = 0;
3986
0
      break;
3987
0
        }
3988
0
    }
3989
0
      } else if (trans->count >= 0) {
3990
0
    int count;
3991
0
    xmlRegCounterPtr counter;
3992
3993
    /*
3994
     * A counted transition.
3995
     */
3996
3997
0
    count = exec->counts[trans->count];
3998
0
    counter = &exec->comp->counters[trans->count];
3999
0
    ret = ((count >= counter->min) && (count <= counter->max));
4000
0
      } else if (atom == NULL) {
4001
0
    exec->status = XML_REGEXP_INTERNAL_ERROR;
4002
0
    break;
4003
0
      } else if (value != NULL) {
4004
0
    ret = xmlRegStrEqualWildcard(atom->valuep, value);
4005
0
    if (atom->neg) {
4006
0
        ret = !ret;
4007
0
        if (!compound)
4008
0
            ret = 0;
4009
0
    }
4010
0
    if ((ret == 1) && (trans->counter >= 0)) {
4011
0
        xmlRegCounterPtr counter;
4012
0
        int count;
4013
4014
0
        count = exec->counts[trans->counter];
4015
0
        counter = &exec->comp->counters[trans->counter];
4016
0
        if (count >= counter->max)
4017
0
      ret = 0;
4018
0
    }
4019
4020
0
    if ((ret == 1) && (atom->min > 0) && (atom->max > 0)) {
4021
0
        xmlRegStatePtr to = exec->comp->states[trans->to];
4022
4023
        /*
4024
         * this is a multiple input sequence
4025
         */
4026
0
        if (exec->state->nbTrans > exec->transno + 1) {
4027
0
      if (exec->inputStackNr <= 0) {
4028
0
          xmlFARegExecSaveInputString(exec, value, data);
4029
0
      }
4030
0
      xmlFARegExecSave(exec);
4031
0
        }
4032
0
        exec->transcount = 1;
4033
0
        do {
4034
      /*
4035
       * Try to progress as much as possible on the input
4036
       */
4037
0
      if (exec->transcount == atom->max) {
4038
0
          break;
4039
0
      }
4040
0
      exec->index++;
4041
0
      value = exec->inputStack[exec->index].value;
4042
0
      data = exec->inputStack[exec->index].data;
4043
4044
      /*
4045
       * End of input: stop here
4046
       */
4047
0
      if (value == NULL) {
4048
0
          exec->index --;
4049
0
          break;
4050
0
      }
4051
0
      if (exec->transcount >= atom->min) {
4052
0
          int transno = exec->transno;
4053
0
          xmlRegStatePtr state = exec->state;
4054
4055
          /*
4056
           * The transition is acceptable save it
4057
           */
4058
0
          exec->transno = -1; /* trick */
4059
0
          exec->state = to;
4060
0
          if (exec->inputStackNr <= 0) {
4061
0
        xmlFARegExecSaveInputString(exec, value, data);
4062
0
          }
4063
0
          xmlFARegExecSave(exec);
4064
0
          exec->transno = transno;
4065
0
          exec->state = state;
4066
0
      }
4067
0
      ret = xmlStrEqual(value, atom->valuep);
4068
0
      exec->transcount++;
4069
0
        } while (ret == 1);
4070
0
        if (exec->transcount < atom->min)
4071
0
      ret = 0;
4072
4073
        /*
4074
         * If the last check failed but one transition was found
4075
         * possible, rollback
4076
         */
4077
0
        if (ret < 0)
4078
0
      ret = 0;
4079
0
        if (ret == 0) {
4080
0
      goto rollback;
4081
0
        }
4082
0
    }
4083
0
      }
4084
0
      if (ret == 1) {
4085
0
    if ((exec->callback != NULL) && (atom != NULL) &&
4086
0
      (data != NULL)) {
4087
0
        exec->callback(exec->data, atom->valuep,
4088
0
                 atom->data, data);
4089
0
    }
4090
0
    if (exec->state->nbTrans > exec->transno + 1) {
4091
0
        if (exec->inputStackNr <= 0) {
4092
0
      xmlFARegExecSaveInputString(exec, value, data);
4093
0
        }
4094
0
        xmlFARegExecSave(exec);
4095
0
    }
4096
0
    if (trans->counter >= 0) {
4097
0
        exec->counts[trans->counter]++;
4098
0
    }
4099
0
    if ((trans->count >= 0) &&
4100
0
        (trans->count < REGEXP_ALL_COUNTER)) {
4101
0
        exec->counts[trans->count] = 0;
4102
0
    }
4103
0
                if ((exec->comp->states[trans->to] != NULL) &&
4104
0
        (exec->comp->states[trans->to]->type ==
4105
0
         XML_REGEXP_SINK_STATE)) {
4106
        /*
4107
         * entering a sink state, save the current state as error
4108
         * state.
4109
         */
4110
0
                    if (xmlRegExecSetErrString(exec, value) < 0)
4111
0
                        break;
4112
0
        exec->errState = exec->state;
4113
0
        memcpy(exec->errCounts, exec->counts,
4114
0
         exec->comp->nbCounters * sizeof(int));
4115
0
    }
4116
0
    exec->state = exec->comp->states[trans->to];
4117
0
    exec->transno = 0;
4118
0
    if (trans->atom != NULL) {
4119
0
        if (exec->inputStack != NULL) {
4120
0
      exec->index++;
4121
0
      if (exec->index < exec->inputStackNr) {
4122
0
          value = exec->inputStack[exec->index].value;
4123
0
          data = exec->inputStack[exec->index].data;
4124
0
      } else {
4125
0
          value = NULL;
4126
0
          data = NULL;
4127
0
      }
4128
0
        } else {
4129
0
      value = NULL;
4130
0
      data = NULL;
4131
0
        }
4132
0
    }
4133
0
    goto progress;
4134
0
      } else if (ret < 0) {
4135
0
    exec->status = XML_REGEXP_INTERNAL_ERROR;
4136
0
    break;
4137
0
      }
4138
0
  }
4139
0
  if ((exec->transno != 0) || (exec->state->nbTrans == 0)) {
4140
0
rollback:
4141
            /*
4142
       * if we didn't yet rollback on the current input
4143
       * store the current state as the error state.
4144
       */
4145
0
      if ((progress) && (exec->state != NULL) &&
4146
0
          (exec->state->type != XML_REGEXP_SINK_STATE)) {
4147
0
          progress = 0;
4148
0
                if (xmlRegExecSetErrString(exec, value) < 0)
4149
0
                    break;
4150
0
    exec->errState = exec->state;
4151
0
                if (exec->comp->nbCounters)
4152
0
                    memcpy(exec->errCounts, exec->counts,
4153
0
                           exec->comp->nbCounters * sizeof(int));
4154
0
      }
4155
4156
      /*
4157
       * Failed to find a way out
4158
       */
4159
0
      exec->determinist = 0;
4160
0
      xmlFARegExecRollBack(exec);
4161
0
      if ((exec->inputStack != NULL ) &&
4162
0
                (exec->status == XML_REGEXP_OK)) {
4163
0
    value = exec->inputStack[exec->index].value;
4164
0
    data = exec->inputStack[exec->index].data;
4165
0
      }
4166
0
  }
4167
0
  continue;
4168
0
progress:
4169
0
        progress = 1;
4170
0
    }
4171
0
    if (exec->status == XML_REGEXP_OK) {
4172
0
        return(exec->state->type == XML_REGEXP_FINAL_STATE);
4173
0
    }
4174
0
    return(exec->status);
4175
0
}
4176
4177
/**
4178
 * Push one input token in the execution context
4179
 *
4180
 * @deprecated Internal function, don't use.
4181
 *
4182
 * @param exec  a regexp execution context or NULL to indicate the end
4183
 * @param value  a string token input
4184
 * @param data  data associated to the token to reuse in callbacks
4185
 * @returns 1 if the regexp reached a final state, 0 if non-final, and
4186
 *     a negative value in case of error.
4187
 */
4188
int
4189
xmlRegExecPushString(xmlRegExecCtxt *exec, const xmlChar *value,
4190
0
               void *data) {
4191
0
    return(xmlRegExecPushStringInternal(exec, value, data, 0));
4192
0
}
4193
4194
/**
4195
 * Push one input token in the execution context
4196
 *
4197
 * @deprecated Internal function, don't use.
4198
 *
4199
 * @param exec  a regexp execution context or NULL to indicate the end
4200
 * @param value  the first string token input
4201
 * @param value2  the second string token input
4202
 * @param data  data associated to the token to reuse in callbacks
4203
 * @returns 1 if the regexp reached a final state, 0 if non-final, and
4204
 *     a negative value in case of error.
4205
 */
4206
int
4207
xmlRegExecPushString2(xmlRegExecCtxt *exec, const xmlChar *value,
4208
0
                      const xmlChar *value2, void *data) {
4209
0
    xmlChar buf[150];
4210
0
    int lenn, lenp, ret;
4211
0
    xmlChar *str;
4212
4213
0
    if (exec == NULL)
4214
0
  return(-1);
4215
0
    if (exec->comp == NULL)
4216
0
  return(-1);
4217
0
    if (exec->status != XML_REGEXP_OK)
4218
0
  return(exec->status);
4219
4220
0
    if (value2 == NULL)
4221
0
        return(xmlRegExecPushString(exec, value, data));
4222
4223
0
    lenn = strlen((char *) value2);
4224
0
    lenp = strlen((char *) value);
4225
4226
0
    if (150 < lenn + lenp + 2) {
4227
0
  str = xmlMalloc(lenn + lenp + 2);
4228
0
  if (str == NULL) {
4229
0
      exec->status = XML_REGEXP_OUT_OF_MEMORY;
4230
0
      return(-1);
4231
0
  }
4232
0
    } else {
4233
0
  str = buf;
4234
0
    }
4235
0
    memcpy(&str[0], value, lenp);
4236
0
    str[lenp] = XML_REG_STRING_SEPARATOR;
4237
0
    memcpy(&str[lenp + 1], value2, lenn);
4238
0
    str[lenn + lenp + 1] = 0;
4239
4240
0
    if (exec->comp->compact != NULL)
4241
0
  ret = xmlRegCompactPushString(exec, exec->comp, str, data);
4242
0
    else
4243
0
        ret = xmlRegExecPushStringInternal(exec, str, data, 1);
4244
4245
0
    if (str != buf)
4246
0
        xmlFree(str);
4247
0
    return(ret);
4248
0
}
4249
4250
/**
4251
 * Extract information from the regexp execution. Internal routine to
4252
 * implement #xmlRegExecNextValues and #xmlRegExecErrInfo
4253
 *
4254
 * @param exec  a regexp execution context
4255
 * @param err  error extraction or normal one
4256
 * @param nbval  pointer to the number of accepted values IN/OUT
4257
 * @param nbneg  return number of negative transitions
4258
 * @param values  pointer to the array of acceptable values
4259
 * @param terminal  return value if this was a terminal state
4260
 * @returns 0 in case of success or -1 in case of error.
4261
 */
4262
static int
4263
xmlRegExecGetValues(xmlRegExecCtxtPtr exec, int err,
4264
                    int *nbval, int *nbneg,
4265
0
        xmlChar **values, int *terminal) {
4266
0
    int maxval;
4267
0
    int nb = 0;
4268
4269
0
    if ((exec == NULL) || (nbval == NULL) || (nbneg == NULL) ||
4270
0
        (values == NULL) || (*nbval <= 0))
4271
0
        return(-1);
4272
4273
0
    maxval = *nbval;
4274
0
    *nbval = 0;
4275
0
    *nbneg = 0;
4276
0
    if ((exec->comp != NULL) && (exec->comp->compact != NULL)) {
4277
0
        xmlRegexpPtr comp;
4278
0
  int target, i, state;
4279
4280
0
        comp = exec->comp;
4281
4282
0
  if (err) {
4283
0
      if (exec->errStateNo == -1) return(-1);
4284
0
      state = exec->errStateNo;
4285
0
  } else {
4286
0
      state = exec->index;
4287
0
  }
4288
0
  if (terminal != NULL) {
4289
0
      if (comp->compact[state * (comp->nbstrings + 1)] ==
4290
0
          XML_REGEXP_FINAL_STATE)
4291
0
    *terminal = 1;
4292
0
      else
4293
0
    *terminal = 0;
4294
0
  }
4295
0
  for (i = 0;(i < comp->nbstrings) && (nb < maxval);i++) {
4296
0
      target = comp->compact[state * (comp->nbstrings + 1) + i + 1];
4297
0
      if ((target > 0) && (target <= comp->nbstates) &&
4298
0
          (comp->compact[(target - 1) * (comp->nbstrings + 1)] !=
4299
0
     XML_REGEXP_SINK_STATE)) {
4300
0
          values[nb++] = comp->stringMap[i];
4301
0
    (*nbval)++;
4302
0
      }
4303
0
  }
4304
0
  for (i = 0;(i < comp->nbstrings) && (nb < maxval);i++) {
4305
0
      target = comp->compact[state * (comp->nbstrings + 1) + i + 1];
4306
0
      if ((target > 0) && (target <= comp->nbstates) &&
4307
0
          (comp->compact[(target - 1) * (comp->nbstrings + 1)] ==
4308
0
     XML_REGEXP_SINK_STATE)) {
4309
0
          values[nb++] = comp->stringMap[i];
4310
0
    (*nbneg)++;
4311
0
      }
4312
0
  }
4313
0
    } else {
4314
0
        int transno;
4315
0
  xmlRegTransPtr trans;
4316
0
  xmlRegAtomPtr atom;
4317
0
  xmlRegStatePtr state;
4318
4319
0
  if (terminal != NULL) {
4320
0
      if (exec->state->type == XML_REGEXP_FINAL_STATE)
4321
0
    *terminal = 1;
4322
0
      else
4323
0
    *terminal = 0;
4324
0
  }
4325
4326
0
  if (err) {
4327
0
      if (exec->errState == NULL) return(-1);
4328
0
      state = exec->errState;
4329
0
  } else {
4330
0
      if (exec->state == NULL) return(-1);
4331
0
      state = exec->state;
4332
0
  }
4333
0
  for (transno = 0;
4334
0
       (transno < state->nbTrans) && (nb < maxval);
4335
0
       transno++) {
4336
0
      trans = &state->trans[transno];
4337
0
      if (trans->to < 0)
4338
0
    continue;
4339
0
      atom = trans->atom;
4340
0
      if ((atom == NULL) || (atom->valuep == NULL))
4341
0
    continue;
4342
0
      if (trans->count == REGEXP_ALL_LAX_COUNTER) {
4343
          /* this should not be reached but ... */
4344
0
      } else if (trans->count == REGEXP_ALL_COUNTER) {
4345
          /* this should not be reached but ... */
4346
0
      } else if (trans->counter >= 0) {
4347
0
    xmlRegCounterPtr counter = NULL;
4348
0
    int count;
4349
4350
0
    if (err)
4351
0
        count = exec->errCounts[trans->counter];
4352
0
    else
4353
0
        count = exec->counts[trans->counter];
4354
0
    if (exec->comp != NULL)
4355
0
        counter = &exec->comp->counters[trans->counter];
4356
0
    if ((counter == NULL) || (count < counter->max)) {
4357
0
        if (atom->neg)
4358
0
      values[nb++] = (xmlChar *) atom->valuep2;
4359
0
        else
4360
0
      values[nb++] = (xmlChar *) atom->valuep;
4361
0
        (*nbval)++;
4362
0
    }
4363
0
      } else {
4364
0
                if ((exec->comp != NULL) && (exec->comp->states[trans->to] != NULL) &&
4365
0
        (exec->comp->states[trans->to]->type !=
4366
0
         XML_REGEXP_SINK_STATE)) {
4367
0
        if (atom->neg)
4368
0
      values[nb++] = (xmlChar *) atom->valuep2;
4369
0
        else
4370
0
      values[nb++] = (xmlChar *) atom->valuep;
4371
0
        (*nbval)++;
4372
0
    }
4373
0
      }
4374
0
  }
4375
0
  for (transno = 0;
4376
0
       (transno < state->nbTrans) && (nb < maxval);
4377
0
       transno++) {
4378
0
      trans = &state->trans[transno];
4379
0
      if (trans->to < 0)
4380
0
    continue;
4381
0
      atom = trans->atom;
4382
0
      if ((atom == NULL) || (atom->valuep == NULL))
4383
0
    continue;
4384
0
      if (trans->count == REGEXP_ALL_LAX_COUNTER) {
4385
0
          continue;
4386
0
      } else if (trans->count == REGEXP_ALL_COUNTER) {
4387
0
          continue;
4388
0
      } else if (trans->counter >= 0) {
4389
0
          continue;
4390
0
      } else {
4391
0
                if ((exec->comp->states[trans->to] != NULL) &&
4392
0
        (exec->comp->states[trans->to]->type ==
4393
0
         XML_REGEXP_SINK_STATE)) {
4394
0
        if (atom->neg)
4395
0
      values[nb++] = (xmlChar *) atom->valuep2;
4396
0
        else
4397
0
      values[nb++] = (xmlChar *) atom->valuep;
4398
0
        (*nbneg)++;
4399
0
    }
4400
0
      }
4401
0
  }
4402
0
    }
4403
0
    return(0);
4404
0
}
4405
4406
/**
4407
 * Extract information from the regexp execution.
4408
 * The parameter `values` must point to an array of `nbval` string pointers
4409
 * on return nbval will contain the number of possible strings in that
4410
 * state and the `values` array will be updated with them. The string values
4411
 * returned will be freed with the `exec` context and don't need to be
4412
 * deallocated.
4413
 *
4414
 * @deprecated Internal function, don't use.
4415
 *
4416
 * @param exec  a regexp execution context
4417
 * @param nbval  pointer to the number of accepted values IN/OUT
4418
 * @param nbneg  return number of negative transitions
4419
 * @param values  pointer to the array of acceptable values
4420
 * @param terminal  return value if this was a terminal state
4421
 * @returns 0 in case of success or -1 in case of error.
4422
 */
4423
int
4424
xmlRegExecNextValues(xmlRegExecCtxt *exec, int *nbval, int *nbneg,
4425
0
                     xmlChar **values, int *terminal) {
4426
0
    return(xmlRegExecGetValues(exec, 0, nbval, nbneg, values, terminal));
4427
0
}
4428
4429
/**
4430
 * Extract error information from the regexp execution. The parameter
4431
 * `string` will be updated with the value pushed and not accepted,
4432
 * the parameter `values` must point to an array of `nbval` string pointers
4433
 * on return nbval will contain the number of possible strings in that
4434
 * state and the `values` array will be updated with them. The string values
4435
 * returned will be freed with the `exec` context and don't need to be
4436
 * deallocated.
4437
 *
4438
 * @deprecated Internal function, don't use.
4439
 *
4440
 * @param exec  a regexp execution context generating an error
4441
 * @param string  return value for the error string
4442
 * @param nbval  pointer to the number of accepted values IN/OUT
4443
 * @param nbneg  return number of negative transitions
4444
 * @param values  pointer to the array of acceptable values
4445
 * @param terminal  return value if this was a terminal state
4446
 * @returns 0 in case of success or -1 in case of error.
4447
 */
4448
int
4449
xmlRegExecErrInfo(xmlRegExecCtxt *exec, const xmlChar **string,
4450
0
                  int *nbval, int *nbneg, xmlChar **values, int *terminal) {
4451
0
    if (exec == NULL)
4452
0
        return(-1);
4453
0
    if (string != NULL) {
4454
0
        if (exec->status != XML_REGEXP_OK)
4455
0
      *string = exec->errString;
4456
0
  else
4457
0
      *string = NULL;
4458
0
    }
4459
0
    return(xmlRegExecGetValues(exec, 1, nbval, nbneg, values, terminal));
4460
0
}
4461
4462
/************************************************************************
4463
 *                  *
4464
 *  Parser for the Schemas Datatype Regular Expressions   *
4465
 *  http://www.w3.org/TR/2001/REC-xmlschema-2-20010502/#regexs  *
4466
 *                  *
4467
 ************************************************************************/
4468
4469
/**
4470
 * [10]   Char   ::=   [^.\?*+()|\#x5B\#x5D]
4471
 *
4472
 * @param ctxt  a regexp parser context
4473
 */
4474
static int
4475
49.2k
xmlFAIsChar(xmlRegParserCtxtPtr ctxt) {
4476
49.2k
    int cur;
4477
49.2k
    int len;
4478
4479
49.2k
    len = 4;
4480
49.2k
    cur = xmlGetUTF8Char(ctxt->cur, &len);
4481
49.2k
    if (cur < 0) {
4482
100
        ERROR("Invalid UTF-8");
4483
100
        return(0);
4484
100
    }
4485
49.1k
    if ((cur == '.') || (cur == '\\') || (cur == '?') ||
4486
49.1k
  (cur == '*') || (cur == '+') || (cur == '(') ||
4487
49.1k
  (cur == ')') || (cur == '|') || (cur == 0x5B) ||
4488
49.1k
  (cur == 0x5D) || (cur == 0))
4489
38.9k
  return(-1);
4490
10.2k
    return(cur);
4491
49.1k
}
4492
4493
/**
4494
 * [27]   charProp   ::=   IsCategory | IsBlock
4495
 * [28]   IsCategory ::= Letters | Marks | Numbers | Punctuation |
4496
 *                       Separators | Symbols | Others
4497
 * [29]   Letters   ::=   'L' [ultmo]?
4498
 * [30]   Marks   ::=   'M' [nce]?
4499
 * [31]   Numbers   ::=   'N' [dlo]?
4500
 * [32]   Punctuation   ::=   'P' [cdseifo]?
4501
 * [33]   Separators   ::=   'Z' [slp]?
4502
 * [34]   Symbols   ::=   'S' [mcko]?
4503
 * [35]   Others   ::=   'C' [cfon]?
4504
 * [36]   IsBlock   ::=   'Is' [a-zA-Z0-9\#x2D]+
4505
 *
4506
 * @param ctxt  a regexp parser context
4507
 */
4508
static void
4509
6.40k
xmlFAParseCharProp(xmlRegParserCtxtPtr ctxt) {
4510
6.40k
    int cur;
4511
6.40k
    xmlRegAtomType type = (xmlRegAtomType) 0;
4512
6.40k
    xmlChar *blockName = NULL;
4513
4514
6.40k
    cur = CUR;
4515
6.40k
    if (cur == 'L') {
4516
781
  NEXT;
4517
781
  cur = CUR;
4518
781
  if (cur == 'u') {
4519
99
      NEXT;
4520
99
      type = XML_REGEXP_LETTER_UPPERCASE;
4521
682
  } else if (cur == 'l') {
4522
98
      NEXT;
4523
98
      type = XML_REGEXP_LETTER_LOWERCASE;
4524
584
  } else if (cur == 't') {
4525
96
      NEXT;
4526
96
      type = XML_REGEXP_LETTER_TITLECASE;
4527
488
  } else if (cur == 'm') {
4528
95
      NEXT;
4529
95
      type = XML_REGEXP_LETTER_MODIFIER;
4530
393
  } else if (cur == 'o') {
4531
96
      NEXT;
4532
96
      type = XML_REGEXP_LETTER_OTHERS;
4533
297
  } else {
4534
297
      type = XML_REGEXP_LETTER;
4535
297
  }
4536
5.62k
    } else if (cur == 'M') {
4537
608
  NEXT;
4538
608
  cur = CUR;
4539
608
  if (cur == 'n') {
4540
96
      NEXT;
4541
      /* nonspacing */
4542
96
      type = XML_REGEXP_MARK_NONSPACING;
4543
512
  } else if (cur == 'c') {
4544
101
      NEXT;
4545
      /* spacing combining */
4546
101
      type = XML_REGEXP_MARK_SPACECOMBINING;
4547
411
  } else if (cur == 'e') {
4548
195
      NEXT;
4549
      /* enclosing */
4550
195
      type = XML_REGEXP_MARK_ENCLOSING;
4551
216
  } else {
4552
      /* all marks */
4553
216
      type = XML_REGEXP_MARK;
4554
216
  }
4555
5.01k
    } else if (cur == 'N') {
4556
650
  NEXT;
4557
650
  cur = CUR;
4558
650
  if (cur == 'd') {
4559
98
      NEXT;
4560
      /* digital */
4561
98
      type = XML_REGEXP_NUMBER_DECIMAL;
4562
552
  } else if (cur == 'l') {
4563
259
      NEXT;
4564
      /* letter */
4565
259
      type = XML_REGEXP_NUMBER_LETTER;
4566
293
  } else if (cur == 'o') {
4567
102
      NEXT;
4568
      /* other */
4569
102
      type = XML_REGEXP_NUMBER_OTHERS;
4570
191
  } else {
4571
      /* all numbers */
4572
191
      type = XML_REGEXP_NUMBER;
4573
191
  }
4574
4.36k
    } else if (cur == 'P') {
4575
1.28k
  NEXT;
4576
1.28k
  cur = CUR;
4577
1.28k
  if (cur == 'c') {
4578
251
      NEXT;
4579
      /* connector */
4580
251
      type = XML_REGEXP_PUNCT_CONNECTOR;
4581
1.03k
  } else if (cur == 'd') {
4582
95
      NEXT;
4583
      /* dash */
4584
95
      type = XML_REGEXP_PUNCT_DASH;
4585
935
  } else if (cur == 's') {
4586
100
      NEXT;
4587
      /* open */
4588
100
      type = XML_REGEXP_PUNCT_OPEN;
4589
835
  } else if (cur == 'e') {
4590
95
      NEXT;
4591
      /* close */
4592
95
      type = XML_REGEXP_PUNCT_CLOSE;
4593
740
  } else if (cur == 'i') {
4594
189
      NEXT;
4595
      /* initial quote */
4596
189
      type = XML_REGEXP_PUNCT_INITQUOTE;
4597
551
  } else if (cur == 'f') {
4598
152
      NEXT;
4599
      /* final quote */
4600
152
      type = XML_REGEXP_PUNCT_FINQUOTE;
4601
399
  } else if (cur == 'o') {
4602
100
      NEXT;
4603
      /* other */
4604
100
      type = XML_REGEXP_PUNCT_OTHERS;
4605
299
  } else {
4606
      /* all punctuation */
4607
299
      type = XML_REGEXP_PUNCT;
4608
299
  }
4609
3.08k
    } else if (cur == 'Z') {
4610
730
  NEXT;
4611
730
  cur = CUR;
4612
730
  if (cur == 's') {
4613
242
      NEXT;
4614
      /* space */
4615
242
      type = XML_REGEXP_SEPAR_SPACE;
4616
488
  } else if (cur == 'l') {
4617
117
      NEXT;
4618
      /* line */
4619
117
      type = XML_REGEXP_SEPAR_LINE;
4620
371
  } else if (cur == 'p') {
4621
122
      NEXT;
4622
      /* paragraph */
4623
122
      type = XML_REGEXP_SEPAR_PARA;
4624
249
  } else {
4625
      /* all separators */
4626
249
      type = XML_REGEXP_SEPAR;
4627
249
  }
4628
2.35k
    } else if (cur == 'S') {
4629
653
  NEXT;
4630
653
  cur = CUR;
4631
653
  if (cur == 'm') {
4632
104
      NEXT;
4633
104
      type = XML_REGEXP_SYMBOL_MATH;
4634
      /* math */
4635
549
  } else if (cur == 'c') {
4636
101
      NEXT;
4637
101
      type = XML_REGEXP_SYMBOL_CURRENCY;
4638
      /* currency */
4639
448
  } else if (cur == 'k') {
4640
102
      NEXT;
4641
102
      type = XML_REGEXP_SYMBOL_MODIFIER;
4642
      /* modifiers */
4643
346
  } else if (cur == 'o') {
4644
99
      NEXT;
4645
99
      type = XML_REGEXP_SYMBOL_OTHERS;
4646
      /* other */
4647
247
  } else {
4648
      /* all symbols */
4649
247
      type = XML_REGEXP_SYMBOL;
4650
247
  }
4651
1.70k
    } else if (cur == 'C') {
4652
931
  NEXT;
4653
931
  cur = CUR;
4654
931
  if (cur == 'c') {
4655
153
      NEXT;
4656
      /* control */
4657
153
      type = XML_REGEXP_OTHER_CONTROL;
4658
778
  } else if (cur == 'f') {
4659
103
      NEXT;
4660
      /* format */
4661
103
      type = XML_REGEXP_OTHER_FORMAT;
4662
675
  } else if (cur == 'o') {
4663
226
      NEXT;
4664
      /* private use */
4665
226
      type = XML_REGEXP_OTHER_PRIVATE;
4666
449
  } else if (cur == 'n') {
4667
102
      NEXT;
4668
      /* not assigned */
4669
102
      type = XML_REGEXP_OTHER_NA;
4670
347
  } else {
4671
      /* all others */
4672
347
      type = XML_REGEXP_OTHER;
4673
347
  }
4674
931
    } else if (cur == 'I') {
4675
714
  const xmlChar *start;
4676
714
  NEXT;
4677
714
  cur = CUR;
4678
714
  if (cur != 's') {
4679
4
      ERROR("IsXXXX expected");
4680
4
      return;
4681
4
  }
4682
710
  NEXT;
4683
710
  start = ctxt->cur;
4684
710
  cur = CUR;
4685
710
  if (((cur >= 'a') && (cur <= 'z')) ||
4686
710
      ((cur >= 'A') && (cur <= 'Z')) ||
4687
710
      ((cur >= '0') && (cur <= '9')) ||
4688
710
      (cur == 0x2D)) {
4689
383
      NEXT;
4690
383
      cur = CUR;
4691
2.25k
      while (((cur >= 'a') && (cur <= 'z')) ||
4692
2.25k
    ((cur >= 'A') && (cur <= 'Z')) ||
4693
2.25k
    ((cur >= '0') && (cur <= '9')) ||
4694
2.25k
    (cur == 0x2D)) {
4695
1.87k
    NEXT;
4696
1.87k
    cur = CUR;
4697
1.87k
      }
4698
383
  }
4699
710
  type = XML_REGEXP_BLOCK_NAME;
4700
710
  blockName = xmlStrndup(start, ctxt->cur - start);
4701
710
        if (blockName == NULL)
4702
2
      xmlRegexpErrMemory(ctxt);
4703
710
    } else {
4704
55
  ERROR("Unknown char property");
4705
55
  return;
4706
55
    }
4707
6.34k
    if (ctxt->atom == NULL) {
4708
5.37k
  ctxt->atom = xmlRegNewAtom(ctxt, type);
4709
5.37k
        if (ctxt->atom == NULL) {
4710
1
            xmlFree(blockName);
4711
1
            return;
4712
1
        }
4713
5.37k
  ctxt->atom->valuep = blockName;
4714
5.37k
    } else if (ctxt->atom->type == XML_REGEXP_RANGES) {
4715
969
        if (xmlRegAtomAddRange(ctxt, ctxt->atom, ctxt->neg,
4716
969
                               type, 0, 0, blockName) == NULL) {
4717
1
            xmlFree(blockName);
4718
1
        }
4719
969
    }
4720
6.34k
}
4721
4722
static int parse_escaped_codeunit(xmlRegParserCtxtPtr ctxt)
4723
438
{
4724
438
    int val = 0, i, cur;
4725
2.02k
    for (i = 0; i < 4; i++) {
4726
1.63k
  NEXT;
4727
1.63k
  val *= 16;
4728
1.63k
  cur = CUR;
4729
1.63k
  if (cur >= '0' && cur <= '9') {
4730
566
      val += cur - '0';
4731
1.06k
  } else if (cur >= 'A' && cur <= 'F') {
4732
618
      val += cur - 'A' + 10;
4733
618
  } else if (cur >= 'a' && cur <= 'f') {
4734
401
      val += cur - 'a' + 10;
4735
401
  } else {
4736
49
      ERROR("Expecting hex digit");
4737
49
      return -1;
4738
49
  }
4739
1.63k
    }
4740
389
    return val;
4741
438
}
4742
4743
static int parse_escaped_codepoint(xmlRegParserCtxtPtr ctxt)
4744
374
{
4745
374
    int val = parse_escaped_codeunit(ctxt);
4746
374
    if (0xD800 <= val && val <= 0xDBFF) {
4747
75
  NEXT;
4748
75
  if (CUR == '\\') {
4749
71
      NEXT;
4750
71
      if (CUR == 'u') {
4751
64
    int low = parse_escaped_codeunit(ctxt);
4752
64
    if (0xDC00 <= low && low <= 0xDFFF) {
4753
42
        return (val - 0xD800) * 0x400 + (low - 0xDC00) + 0x10000;
4754
42
    }
4755
64
      }
4756
71
  }
4757
33
  ERROR("Invalid low surrogate pair code unit");
4758
33
  val = -1;
4759
33
    }
4760
332
    return val;
4761
374
}
4762
4763
/**
4764
 * ```
4765
 * [23] charClassEsc ::= ( SingleCharEsc | MultiCharEsc | catEsc | complEsc )
4766
 * [24] SingleCharEsc ::= '\' [nrt\|.?*+(){}\#x2D\#x5B\#x5D\#x5E]
4767
 * [25] catEsc   ::=   '\p{' charProp '}'
4768
 * [26] complEsc ::=   '\P{' charProp '}'
4769
 * [37] MultiCharEsc ::= '.' | ('\' [sSiIcCdDwW])
4770
 * ```
4771
 *
4772
 * @param ctxt  a regexp parser context
4773
 */
4774
static void
4775
12.9k
xmlFAParseCharClassEsc(xmlRegParserCtxtPtr ctxt) {
4776
12.9k
    int cur;
4777
4778
12.9k
    if (CUR == '.') {
4779
866
  if (ctxt->atom == NULL) {
4780
866
      ctxt->atom = xmlRegNewAtom(ctxt, XML_REGEXP_ANYCHAR);
4781
866
  } else if (ctxt->atom->type == XML_REGEXP_RANGES) {
4782
0
      xmlRegAtomAddRange(ctxt, ctxt->atom, ctxt->neg,
4783
0
             XML_REGEXP_ANYCHAR, 0, 0, NULL);
4784
0
  }
4785
866
  NEXT;
4786
866
  return;
4787
866
    }
4788
12.1k
    if (CUR != '\\') {
4789
0
  ERROR("Escaped sequence: expecting \\");
4790
0
  return;
4791
0
    }
4792
12.1k
    NEXT;
4793
12.1k
    cur = CUR;
4794
12.1k
    if (cur == 'p') {
4795
5.31k
  NEXT;
4796
5.31k
  if (CUR != '{') {
4797
15
      ERROR("Expecting '{'");
4798
15
      return;
4799
15
  }
4800
5.30k
  NEXT;
4801
5.30k
  xmlFAParseCharProp(ctxt);
4802
5.30k
  if (CUR != '}') {
4803
438
      ERROR("Expecting '}'");
4804
438
      return;
4805
438
  }
4806
4.86k
  NEXT;
4807
6.78k
    } else if (cur == 'P') {
4808
1.11k
  NEXT;
4809
1.11k
  if (CUR != '{') {
4810
10
      ERROR("Expecting '{'");
4811
10
      return;
4812
10
  }
4813
1.10k
  NEXT;
4814
1.10k
  xmlFAParseCharProp(ctxt);
4815
1.10k
        if (ctxt->atom != NULL)
4816
1.08k
      ctxt->atom->neg = 1;
4817
1.10k
  if (CUR != '}') {
4818
79
      ERROR("Expecting '}'");
4819
79
      return;
4820
79
  }
4821
1.02k
  NEXT;
4822
5.67k
    } else if ((cur == 'n') || (cur == 'r') || (cur == 't') || (cur == '\\') ||
4823
5.67k
  (cur == '|') || (cur == '.') || (cur == '?') || (cur == '*') ||
4824
5.67k
  (cur == '+') || (cur == '(') || (cur == ')') || (cur == '{') ||
4825
5.67k
  (cur == '}') || (cur == 0x2D) || (cur == 0x5B) || (cur == 0x5D) ||
4826
5.67k
  (cur == 0x5E) ||
4827
4828
  /* Non-standard escape sequences:
4829
   *                  Java 1.8|.NET Core 3.1|MSXML 6 */
4830
5.67k
  (cur == '!') ||     /*   +  |     +       |    +   */
4831
5.67k
  (cur == '"') ||     /*   +  |     +       |    +   */
4832
5.67k
  (cur == '#') ||     /*   +  |     +       |    +   */
4833
5.67k
  (cur == '$') ||     /*   +  |     +       |    +   */
4834
5.67k
  (cur == '%') ||     /*   +  |     +       |    +   */
4835
5.67k
  (cur == ',') ||     /*   +  |     +       |    +   */
4836
5.67k
  (cur == '/') ||     /*   +  |     +       |    +   */
4837
5.67k
  (cur == ':') ||     /*   +  |     +       |    +   */
4838
5.67k
  (cur == ';') ||     /*   +  |     +       |    +   */
4839
5.67k
  (cur == '=') ||     /*   +  |     +       |    +   */
4840
5.67k
  (cur == '>') ||     /*      |     +       |    +   */
4841
5.67k
  (cur == '@') ||     /*   +  |     +       |    +   */
4842
5.67k
  (cur == '`') ||     /*   +  |     +       |    +   */
4843
5.67k
  (cur == '~') ||     /*   +  |     +       |    +   */
4844
5.67k
  (cur == 'u')) {     /*      |     +       |    +   */
4845
2.87k
  if (ctxt->atom == NULL) {
4846
2.52k
      ctxt->atom = xmlRegNewAtom(ctxt, XML_REGEXP_CHARVAL);
4847
2.52k
      if (ctxt->atom != NULL) {
4848
2.52k
          switch (cur) {
4849
68
        case 'n':
4850
68
            ctxt->atom->codepoint = '\n';
4851
68
      break;
4852
66
        case 'r':
4853
66
            ctxt->atom->codepoint = '\r';
4854
66
      break;
4855
66
        case 't':
4856
66
            ctxt->atom->codepoint = '\t';
4857
66
      break;
4858
374
        case 'u':
4859
374
      cur = parse_escaped_codepoint(ctxt);
4860
374
      if (cur < 0) {
4861
81
          return;
4862
81
      }
4863
293
      ctxt->atom->codepoint = cur;
4864
293
      break;
4865
1.94k
        default:
4866
1.94k
      ctxt->atom->codepoint = cur;
4867
2.52k
    }
4868
2.52k
      }
4869
2.52k
  } else if (ctxt->atom->type == XML_REGEXP_RANGES) {
4870
355
            switch (cur) {
4871
66
                case 'n':
4872
66
                    cur = '\n';
4873
66
                    break;
4874
66
                case 'r':
4875
66
                    cur = '\r';
4876
66
                    break;
4877
66
                case 't':
4878
66
                    cur = '\t';
4879
66
                    break;
4880
355
            }
4881
355
      xmlRegAtomAddRange(ctxt, ctxt->atom, ctxt->neg,
4882
355
             XML_REGEXP_CHARVAL, cur, cur, NULL);
4883
355
  }
4884
2.79k
  NEXT;
4885
2.79k
    } else if ((cur == 's') || (cur == 'S') || (cur == 'i') || (cur == 'I') ||
4886
2.79k
  (cur == 'c') || (cur == 'C') || (cur == 'd') || (cur == 'D') ||
4887
2.79k
  (cur == 'w') || (cur == 'W')) {
4888
2.68k
  xmlRegAtomType type = XML_REGEXP_ANYSPACE;
4889
4890
2.68k
  switch (cur) {
4891
290
      case 's':
4892
290
    type = XML_REGEXP_ANYSPACE;
4893
290
    break;
4894
142
      case 'S':
4895
142
    type = XML_REGEXP_NOTSPACE;
4896
142
    break;
4897
402
      case 'i':
4898
402
    type = XML_REGEXP_INITNAME;
4899
402
    break;
4900
284
      case 'I':
4901
284
    type = XML_REGEXP_NOTINITNAME;
4902
284
    break;
4903
398
      case 'c':
4904
398
    type = XML_REGEXP_NAMECHAR;
4905
398
    break;
4906
312
      case 'C':
4907
312
    type = XML_REGEXP_NOTNAMECHAR;
4908
312
    break;
4909
294
      case 'd':
4910
294
    type = XML_REGEXP_DECIMAL;
4911
294
    break;
4912
118
      case 'D':
4913
118
    type = XML_REGEXP_NOTDECIMAL;
4914
118
    break;
4915
288
      case 'w':
4916
288
    type = XML_REGEXP_REALCHAR;
4917
288
    break;
4918
161
      case 'W':
4919
161
    type = XML_REGEXP_NOTREALCHAR;
4920
161
    break;
4921
2.68k
  }
4922
2.68k
  NEXT;
4923
2.68k
  if (ctxt->atom == NULL) {
4924
2.17k
      ctxt->atom = xmlRegNewAtom(ctxt, type);
4925
2.17k
  } else if (ctxt->atom->type == XML_REGEXP_RANGES) {
4926
511
      xmlRegAtomAddRange(ctxt, ctxt->atom, ctxt->neg,
4927
511
             type, 0, 0, NULL);
4928
511
  }
4929
2.68k
    } else {
4930
109
  ERROR("Wrong escape sequence, misuse of character '\\'");
4931
109
    }
4932
12.1k
}
4933
4934
/**
4935
 * ```
4936
 * [17]   charRange   ::=     seRange | XmlCharRef | XmlCharIncDash
4937
 * [18]   seRange   ::=   charOrEsc '-' charOrEsc
4938
 * [20]   charOrEsc   ::=   XmlChar | SingleCharEsc
4939
 * [21]   XmlChar   ::=   [^\\#x2D\#x5B\#x5D]
4940
 * [22]   XmlCharIncDash   ::=   [^\\#x5B\#x5D]
4941
 * ```
4942
 *
4943
 * @param ctxt  a regexp parser context
4944
 */
4945
static void
4946
5.04k
xmlFAParseCharRange(xmlRegParserCtxtPtr ctxt) {
4947
5.04k
    int cur, len;
4948
5.04k
    int start = -1;
4949
5.04k
    int end = -1;
4950
4951
5.04k
    if (CUR == '\0') {
4952
333
        ERROR("Expecting ']'");
4953
333
  return;
4954
333
    }
4955
4956
4.70k
    cur = CUR;
4957
4.70k
    if (cur == '\\') {
4958
0
  NEXT;
4959
0
  cur = CUR;
4960
0
  switch (cur) {
4961
0
      case 'n': start = 0xA; break;
4962
0
      case 'r': start = 0xD; break;
4963
0
      case 't': start = 0x9; break;
4964
0
      case '\\': case '|': case '.': case '-': case '^': case '?':
4965
0
      case '*': case '+': case '{': case '}': case '(': case ')':
4966
0
      case '[': case ']':
4967
0
    start = cur; break;
4968
0
      default:
4969
0
    ERROR("Invalid escape value");
4970
0
    return;
4971
0
  }
4972
0
  end = start;
4973
0
        len = 1;
4974
4.70k
    } else if ((cur != 0x5B) && (cur != 0x5D)) {
4975
4.70k
        len = 4;
4976
4.70k
        end = start = xmlGetUTF8Char(ctxt->cur, &len);
4977
4.70k
        if (start < 0) {
4978
29
            ERROR("Invalid UTF-8");
4979
29
            return;
4980
29
        }
4981
4.70k
    } else {
4982
6
  ERROR("Expecting a char range");
4983
6
  return;
4984
6
    }
4985
    /*
4986
     * Since we are "inside" a range, we can assume ctxt->cur is past
4987
     * the start of ctxt->string, and PREV should be safe
4988
     */
4989
4.67k
    if ((start == '-') && (NXT(1) != ']') && (PREV != '[') && (PREV != '^')) {
4990
548
  NEXTL(len);
4991
548
  return;
4992
548
    }
4993
4.12k
    NEXTL(len);
4994
4.12k
    cur = CUR;
4995
4.12k
    if ((cur != '-') || (NXT(1) == '[') || (NXT(1) == ']')) {
4996
2.56k
        xmlRegAtomAddRange(ctxt, ctxt->atom, ctxt->neg,
4997
2.56k
                  XML_REGEXP_CHARVAL, start, end, NULL);
4998
2.56k
  return;
4999
2.56k
    }
5000
1.56k
    NEXT;
5001
1.56k
    cur = CUR;
5002
1.56k
    if (cur == '\\') {
5003
1.12k
  NEXT;
5004
1.12k
  cur = CUR;
5005
1.12k
  switch (cur) {
5006
66
      case 'n': end = 0xA; break;
5007
66
      case 'r': end = 0xD; break;
5008
66
      case 't': end = 0x9; break;
5009
396
      case '\\': case '|': case '.': case '-': case '^': case '?':
5010
792
      case '*': case '+': case '{': case '}': case '(': case ')':
5011
924
      case '[': case ']':
5012
924
    end = cur; break;
5013
1
      default:
5014
1
    ERROR("Invalid escape value");
5015
1
    return;
5016
1.12k
  }
5017
1.12k
        len = 1;
5018
1.12k
    } else if ((cur != '\0') && (cur != 0x5B) && (cur != 0x5D)) {
5019
436
        len = 4;
5020
436
        end = xmlGetUTF8Char(ctxt->cur, &len);
5021
436
        if (end < 0) {
5022
7
            ERROR("Invalid UTF-8");
5023
7
            return;
5024
7
        }
5025
436
    } else {
5026
2
  ERROR("Expecting the end of a char range");
5027
2
  return;
5028
2
    }
5029
5030
    /* TODO check that the values are acceptable character ranges for XML */
5031
1.55k
    if (end < start) {
5032
17
  ERROR("End of range is before start of range");
5033
1.53k
    } else {
5034
1.53k
        NEXTL(len);
5035
1.53k
        xmlRegAtomAddRange(ctxt, ctxt->atom, ctxt->neg,
5036
1.53k
               XML_REGEXP_CHARVAL, start, end, NULL);
5037
1.53k
    }
5038
1.55k
}
5039
5040
/**
5041
 * [14]   posCharGroup ::= ( charRange | charClassEsc  )+
5042
 *
5043
 * @param ctxt  a regexp parser context
5044
 */
5045
static void
5046
3.77k
xmlFAParsePosCharGroup(xmlRegParserCtxtPtr ctxt) {
5047
6.89k
    do {
5048
6.89k
  if (CUR == '\\') {
5049
1.85k
      xmlFAParseCharClassEsc(ctxt);
5050
5.04k
  } else {
5051
5.04k
      xmlFAParseCharRange(ctxt);
5052
5.04k
  }
5053
6.89k
    } while ((CUR != ']') && (CUR != '-') &&
5054
6.89k
             (CUR != 0) && (ctxt->error == 0));
5055
3.77k
}
5056
5057
/**
5058
 * [13]   charGroup    ::= posCharGroup | negCharGroup | charClassSub
5059
 * [15]   negCharGroup ::= '^' posCharGroup
5060
 * [16]   charClassSub ::= ( posCharGroup | negCharGroup ) '-' charClassExpr
5061
 * [12]   charClassExpr ::= '[' charGroup ']'
5062
 *
5063
 * @param ctxt  a regexp parser context
5064
 */
5065
static void
5066
3.16k
xmlFAParseCharGroup(xmlRegParserCtxtPtr ctxt) {
5067
3.16k
    int neg = ctxt->neg;
5068
5069
3.16k
    if (CUR == '^') {
5070
174
  NEXT;
5071
174
  ctxt->neg = !ctxt->neg;
5072
174
  xmlFAParsePosCharGroup(ctxt);
5073
174
  ctxt->neg = neg;
5074
174
    }
5075
6.76k
    while ((CUR != ']') && (ctxt->error == 0)) {
5076
3.92k
  if ((CUR == '-') && (NXT(1) == '[')) {
5077
331
      NEXT; /* eat the '-' */
5078
331
      NEXT; /* eat the '[' */
5079
331
      ctxt->neg = 2;
5080
331
      xmlFAParseCharGroup(ctxt);
5081
331
      ctxt->neg = neg;
5082
331
      if (CUR == ']') {
5083
83
    NEXT;
5084
248
      } else {
5085
248
    ERROR("charClassExpr: ']' expected");
5086
248
      }
5087
331
      break;
5088
3.59k
  } else {
5089
3.59k
      xmlFAParsePosCharGroup(ctxt);
5090
3.59k
  }
5091
3.92k
    }
5092
3.16k
}
5093
5094
/**
5095
 * [11]   charClass   ::=     charClassEsc | charClassExpr
5096
 * [12]   charClassExpr   ::=   '[' charGroup ']'
5097
 *
5098
 * @param ctxt  a regexp parser context
5099
 */
5100
static void
5101
13.9k
xmlFAParseCharClass(xmlRegParserCtxtPtr ctxt) {
5102
13.9k
    if (CUR == '[') {
5103
2.83k
  NEXT;
5104
2.83k
  ctxt->atom = xmlRegNewAtom(ctxt, XML_REGEXP_RANGES);
5105
2.83k
  if (ctxt->atom == NULL)
5106
2
      return;
5107
2.83k
  xmlFAParseCharGroup(ctxt);
5108
2.83k
  if (CUR == ']') {
5109
2.39k
      NEXT;
5110
2.39k
  } else {
5111
438
      ERROR("xmlFAParseCharClass: ']' expected");
5112
438
  }
5113
11.1k
    } else {
5114
11.1k
  xmlFAParseCharClassEsc(ctxt);
5115
11.1k
    }
5116
13.9k
}
5117
5118
/**
5119
 * [8]   QuantExact   ::=   [0-9]+
5120
 *
5121
 * @param ctxt  a regexp parser context
5122
 * @returns 0 if success or -1 in case of error
5123
 */
5124
static int
5125
4.12k
xmlFAParseQuantExact(xmlRegParserCtxtPtr ctxt) {
5126
4.12k
    int ret = 0;
5127
4.12k
    int ok = 0;
5128
4.12k
    int overflow = 0;
5129
5130
8.83k
    while ((CUR >= '0') && (CUR <= '9')) {
5131
4.71k
        if (ret > INT_MAX / 10) {
5132
389
            overflow = 1;
5133
4.32k
        } else {
5134
4.32k
            int digit = CUR - '0';
5135
5136
4.32k
            ret *= 10;
5137
4.32k
            if (ret > INT_MAX - digit)
5138
53
                overflow = 1;
5139
4.26k
            else
5140
4.26k
                ret += digit;
5141
4.32k
        }
5142
4.71k
  ok = 1;
5143
4.71k
  NEXT;
5144
4.71k
    }
5145
4.12k
    if ((ok != 1) || (overflow == 1)) {
5146
1.47k
  return(-1);
5147
1.47k
    }
5148
2.64k
    return(ret);
5149
4.12k
}
5150
5151
/**
5152
 * [4]   quantifier   ::=   [?*+] | ( '{' quantity '}' )
5153
 * [5]   quantity   ::=   quantRange | quantMin | QuantExact
5154
 * [6]   quantRange   ::=   QuantExact ',' QuantExact
5155
 * [7]   quantMin   ::=   QuantExact ','
5156
 * [8]   QuantExact   ::=   [0-9]+
5157
 *
5158
 * @param ctxt  a regexp parser context
5159
 */
5160
static int
5161
34.0k
xmlFAParseQuantifier(xmlRegParserCtxtPtr ctxt) {
5162
34.0k
    int cur;
5163
5164
34.0k
    cur = CUR;
5165
34.0k
    if ((cur == '?') || (cur == '*') || (cur == '+')) {
5166
7.90k
  if (ctxt->atom != NULL) {
5167
7.89k
      if (cur == '?')
5168
889
    ctxt->atom->quant = XML_REGEXP_QUANT_OPT;
5169
7.00k
      else if (cur == '*')
5170
6.38k
    ctxt->atom->quant = XML_REGEXP_QUANT_MULT;
5171
620
      else if (cur == '+')
5172
620
    ctxt->atom->quant = XML_REGEXP_QUANT_PLUS;
5173
7.89k
  }
5174
7.90k
  NEXT;
5175
7.90k
  return(1);
5176
7.90k
    }
5177
26.1k
    if (cur == '{') {
5178
3.71k
  int min = 0, max = 0;
5179
5180
3.71k
  NEXT;
5181
3.71k
  cur = xmlFAParseQuantExact(ctxt);
5182
3.71k
  if (cur >= 0)
5183
2.43k
      min = cur;
5184
1.27k
        else {
5185
1.27k
            ERROR("Improper quantifier");
5186
1.27k
        }
5187
3.71k
  if (CUR == ',') {
5188
511
      NEXT;
5189
511
      if (CUR == '}')
5190
101
          max = INT_MAX;
5191
410
      else {
5192
410
          cur = xmlFAParseQuantExact(ctxt);
5193
410
          if (cur >= 0)
5194
207
        max = cur;
5195
203
    else {
5196
203
        ERROR("Improper quantifier");
5197
203
    }
5198
410
      }
5199
511
  }
5200
3.71k
  if (CUR == '}') {
5201
2.32k
      NEXT;
5202
2.32k
  } else {
5203
1.38k
      ERROR("Unterminated quantifier");
5204
1.38k
  }
5205
3.71k
  if (max == 0)
5206
3.40k
      max = min;
5207
3.71k
  if (ctxt->atom != NULL) {
5208
3.54k
      ctxt->atom->quant = XML_REGEXP_QUANT_RANGE;
5209
3.54k
      ctxt->atom->min = min;
5210
3.54k
      ctxt->atom->max = max;
5211
3.54k
  }
5212
3.71k
  return(1);
5213
3.71k
    }
5214
22.4k
    return(0);
5215
26.1k
}
5216
5217
/**
5218
 * [9]   atom   ::=   Char | charClass | ( '(' regExp ')' )
5219
 *
5220
 * @param ctxt  a regexp parser context
5221
 */
5222
static int
5223
49.2k
xmlFAParseAtom(xmlRegParserCtxtPtr ctxt) {
5224
49.2k
    int codepoint, len;
5225
5226
49.2k
    codepoint = xmlFAIsChar(ctxt);
5227
49.2k
    if (codepoint > 0) {
5228
10.2k
  ctxt->atom = xmlRegNewAtom(ctxt, XML_REGEXP_CHARVAL);
5229
10.2k
  if (ctxt->atom == NULL)
5230
209
      return(-1);
5231
10.0k
        len = 4;
5232
10.0k
        codepoint = xmlGetUTF8Char(ctxt->cur, &len);
5233
10.0k
        if (codepoint < 0) {
5234
0
            ERROR("Invalid UTF-8");
5235
0
            return(-1);
5236
0
        }
5237
10.0k
  ctxt->atom->codepoint = codepoint;
5238
10.0k
  NEXTL(len);
5239
10.0k
  return(1);
5240
39.0k
    } else if (CUR == '|') {
5241
3.68k
  return(0);
5242
35.3k
    } else if (CUR == 0) {
5243
4.96k
  return(0);
5244
30.3k
    } else if (CUR == ')') {
5245
6.38k
  return(0);
5246
23.9k
    } else if (CUR == '(') {
5247
9.88k
  xmlRegStatePtr start, oldend, start0;
5248
5249
9.88k
  NEXT;
5250
9.88k
        if (ctxt->depth >= 50) {
5251
1
      ERROR("xmlFAParseAtom: maximum nesting depth exceeded");
5252
1
            return(-1);
5253
1
        }
5254
  /*
5255
   * this extra Epsilon transition is needed if we count with 0 allowed
5256
   * unfortunately this can't be known at that point
5257
   */
5258
9.87k
  xmlFAGenerateEpsilonTransition(ctxt, ctxt->state, NULL);
5259
9.87k
  start0 = ctxt->state;
5260
9.87k
  xmlFAGenerateEpsilonTransition(ctxt, ctxt->state, NULL);
5261
9.87k
  start = ctxt->state;
5262
9.87k
  oldend = ctxt->end;
5263
9.87k
  ctxt->end = NULL;
5264
9.87k
  ctxt->atom = NULL;
5265
9.87k
        ctxt->depth++;
5266
9.87k
  xmlFAParseRegExp(ctxt, 0);
5267
9.87k
        ctxt->depth--;
5268
9.87k
  if (CUR == ')') {
5269
6.52k
      NEXT;
5270
6.52k
  } else {
5271
3.35k
      ERROR("xmlFAParseAtom: expecting ')'");
5272
3.35k
  }
5273
9.87k
  ctxt->atom = xmlRegNewAtom(ctxt, XML_REGEXP_SUBREG);
5274
9.87k
  if (ctxt->atom == NULL)
5275
15
      return(-1);
5276
9.86k
  ctxt->atom->start = start;
5277
9.86k
  ctxt->atom->start0 = start0;
5278
9.86k
  ctxt->atom->stop = ctxt->state;
5279
9.86k
  ctxt->end = oldend;
5280
9.86k
  return(1);
5281
14.1k
    } else if ((CUR == '[') || (CUR == '\\') || (CUR == '.')) {
5282
13.9k
  xmlFAParseCharClass(ctxt);
5283
13.9k
  return(1);
5284
13.9k
    }
5285
150
    return(0);
5286
49.2k
}
5287
5288
/**
5289
 * [3]   piece   ::=   atom quantifier?
5290
 *
5291
 * @param ctxt  a regexp parser context
5292
 */
5293
static int
5294
49.2k
xmlFAParsePiece(xmlRegParserCtxtPtr ctxt) {
5295
49.2k
    int ret;
5296
5297
49.2k
    ctxt->atom = NULL;
5298
49.2k
    ret = xmlFAParseAtom(ctxt);
5299
49.2k
    if (ret == 0)
5300
15.1k
  return(0);
5301
34.0k
    if (ctxt->atom == NULL) {
5302
408
  ERROR("internal: no atom generated");
5303
408
    }
5304
34.0k
    xmlFAParseQuantifier(ctxt);
5305
34.0k
    return(1);
5306
49.2k
}
5307
5308
/**
5309
 * `to` is used to optimize by removing duplicate path in automata
5310
 * in expressions like (a|b)(c|d)
5311
 *
5312
 * [2]   branch   ::=   piece*
5313
 *
5314
 * @param ctxt  a regexp parser context
5315
 * @param to  optional target to the end of the branch
5316
 */
5317
static int
5318
20.2k
xmlFAParseBranch(xmlRegParserCtxtPtr ctxt, xmlRegStatePtr to) {
5319
20.2k
    xmlRegStatePtr previous;
5320
20.2k
    int ret;
5321
5322
20.2k
    previous = ctxt->state;
5323
20.2k
    ret = xmlFAParsePiece(ctxt);
5324
20.2k
    if (ret == 0) {
5325
        /* Empty branch */
5326
5.68k
  xmlFAGenerateEpsilonTransition(ctxt, previous, to);
5327
14.6k
    } else {
5328
14.6k
  if (xmlFAGenerateTransitions(ctxt, previous,
5329
14.6k
          (CUR=='|' || CUR==')' || CUR==0) ? to : NULL,
5330
14.6k
                ctxt->atom) < 0) {
5331
384
            xmlRegFreeAtom(ctxt->atom);
5332
384
            ctxt->atom = NULL;
5333
384
      return(-1);
5334
384
        }
5335
14.2k
  previous = ctxt->state;
5336
14.2k
  ctxt->atom = NULL;
5337
14.2k
    }
5338
48.8k
    while ((ret != 0) && (ctxt->error == 0)) {
5339
28.9k
  ret = xmlFAParsePiece(ctxt);
5340
28.9k
  if (ret != 0) {
5341
19.4k
      if (xmlFAGenerateTransitions(ctxt, previous,
5342
19.4k
              (CUR=='|' || CUR==')' || CUR==0) ? to : NULL,
5343
19.4k
                    ctxt->atom) < 0) {
5344
59
                xmlRegFreeAtom(ctxt->atom);
5345
59
                ctxt->atom = NULL;
5346
59
                return(-1);
5347
59
            }
5348
19.4k
      previous = ctxt->state;
5349
19.4k
      ctxt->atom = NULL;
5350
19.4k
  }
5351
28.9k
    }
5352
19.8k
    return(0);
5353
19.9k
}
5354
5355
/**
5356
 * [1]   regExp   ::=     branch  ( '|' branch )*
5357
 *
5358
 * @param ctxt  a regexp parser context
5359
 * @param top  is this the top-level expression ?
5360
 */
5361
static void
5362
16.6k
xmlFAParseRegExp(xmlRegParserCtxtPtr ctxt, int top) {
5363
16.6k
    xmlRegStatePtr start, end;
5364
5365
    /* if not top start should have been generated by an epsilon trans */
5366
16.6k
    start = ctxt->state;
5367
16.6k
    ctxt->end = NULL;
5368
16.6k
    xmlFAParseBranch(ctxt, NULL);
5369
16.6k
    if (top) {
5370
6.73k
  ctxt->state->type = XML_REGEXP_FINAL_STATE;
5371
6.73k
    }
5372
16.6k
    if (CUR != '|') {
5373
14.9k
  ctxt->end = ctxt->state;
5374
14.9k
  return;
5375
14.9k
    }
5376
1.71k
    end = ctxt->state;
5377
5.38k
    while ((CUR == '|') && (ctxt->error == 0)) {
5378
3.67k
  NEXT;
5379
3.67k
  ctxt->state = start;
5380
3.67k
  ctxt->end = NULL;
5381
3.67k
  xmlFAParseBranch(ctxt, end);
5382
3.67k
    }
5383
1.71k
    if (!top) {
5384
948
  ctxt->state = end;
5385
948
  ctxt->end = end;
5386
948
    }
5387
1.71k
}
5388
5389
/************************************************************************
5390
 *                  *
5391
 *      The basic API         *
5392
 *                  *
5393
 ************************************************************************/
5394
5395
/**
5396
 * No-op since 2.14.0.
5397
 *
5398
 * @deprecated Don't use.
5399
 *
5400
 * @param output  the file for the output debug
5401
 * @param regexp  the compiled regexp
5402
 */
5403
void
5404
xmlRegexpPrint(FILE *output ATTRIBUTE_UNUSED,
5405
0
               xmlRegexp *regexp ATTRIBUTE_UNUSED) {
5406
0
}
5407
5408
/**
5409
 * Parses an XML Schemas regular expression.
5410
 *
5411
 * Parses a regular expression conforming to XML Schemas Part 2 Datatype
5412
 * Appendix F and builds an automata suitable for testing strings against
5413
 * that regular expression.
5414
 *
5415
 * @param regexp  a regular expression string
5416
 * @returns the compiled expression or NULL in case of error
5417
 */
5418
xmlRegexp *
5419
6.74k
xmlRegexpCompile(const xmlChar *regexp) {
5420
6.74k
    xmlRegexpPtr ret = NULL;
5421
6.74k
    xmlRegParserCtxtPtr ctxt;
5422
5423
6.74k
    if (regexp == NULL)
5424
5
        return(NULL);
5425
5426
6.74k
    ctxt = xmlRegNewParserCtxt(regexp);
5427
6.74k
    if (ctxt == NULL)
5428
3
  return(NULL);
5429
5430
    /* initialize the parser */
5431
6.74k
    ctxt->state = xmlRegStatePush(ctxt);
5432
6.74k
    if (ctxt->state == NULL)
5433
2
        goto error;
5434
6.73k
    ctxt->start = ctxt->state;
5435
6.73k
    ctxt->end = NULL;
5436
5437
    /* parse the expression building an automata */
5438
6.73k
    xmlFAParseRegExp(ctxt, 1);
5439
6.73k
    if (CUR != 0) {
5440
603
  ERROR("xmlFAParseRegExp: extra characters");
5441
603
    }
5442
6.73k
    if (ctxt->error != 0)
5443
1.86k
        goto error;
5444
4.87k
    ctxt->end = ctxt->state;
5445
4.87k
    ctxt->start->type = XML_REGEXP_START_STATE;
5446
4.87k
    ctxt->end->type = XML_REGEXP_FINAL_STATE;
5447
5448
    /* remove the Epsilon except for counted transitions */
5449
4.87k
    xmlFAEliminateEpsilonTransitions(ctxt);
5450
5451
5452
4.87k
    if (ctxt->error != 0)
5453
67
        goto error;
5454
4.80k
    ret = xmlRegEpxFromParse(ctxt);
5455
5456
6.74k
error:
5457
6.74k
    xmlRegFreeParserCtxt(ctxt);
5458
6.74k
    return(ret);
5459
4.80k
}
5460
5461
/**
5462
 * Check if the regular expression matches a string.
5463
 *
5464
 * @param comp  the compiled regular expression
5465
 * @param content  the value to check against the regular expression
5466
 * @returns 1 if it matches, 0 if not and a negative value in case of error
5467
 */
5468
int
5469
0
xmlRegexpExec(xmlRegexp *comp, const xmlChar *content) {
5470
0
    if ((comp == NULL) || (content == NULL))
5471
0
  return(-1);
5472
0
    return(xmlFARegExec(comp, content));
5473
0
}
5474
5475
/**
5476
 * Check if the regular expression is deterministic.
5477
 *
5478
 * DTD and XML Schemas require a deterministic content model,
5479
 * so the automaton compiled from the regex must be a DFA.
5480
 *
5481
 * The runtime of this function is quadratic in the number of
5482
 * outgoing edges, causing serious worst-case performance issues.
5483
 *
5484
 * @deprecated: Internal function, don't use.
5485
 *
5486
 * @param comp  the compiled regular expression
5487
 * @returns 1 if it yes, 0 if not and a negative value in case
5488
 * of error
5489
 */
5490
int
5491
4.80k
xmlRegexpIsDeterminist(xmlRegexp *comp) {
5492
4.80k
    xmlAutomataPtr am;
5493
4.80k
    int ret;
5494
5495
4.80k
    if (comp == NULL)
5496
0
  return(-1);
5497
4.80k
    if (comp->determinist != -1)
5498
0
  return(comp->determinist);
5499
5500
4.80k
    am = xmlNewAutomata();
5501
4.80k
    if (am == NULL)
5502
15
        return(-1);
5503
4.78k
    if (am->states != NULL) {
5504
4.78k
  int i;
5505
5506
9.57k
  for (i = 0;i < am->nbStates;i++)
5507
4.78k
      xmlRegFreeState(am->states[i]);
5508
4.78k
  xmlFree(am->states);
5509
4.78k
    }
5510
4.78k
    am->nbAtoms = comp->nbAtoms;
5511
4.78k
    am->atoms = comp->atoms;
5512
4.78k
    am->nbStates = comp->nbStates;
5513
4.78k
    am->states = comp->states;
5514
4.78k
    am->determinist = -1;
5515
4.78k
    am->flags = comp->flags;
5516
4.78k
    ret = xmlFAComputesDeterminism(am);
5517
4.78k
    am->atoms = NULL;
5518
4.78k
    am->states = NULL;
5519
4.78k
    xmlFreeAutomata(am);
5520
4.78k
    comp->determinist = ret;
5521
4.78k
    return(ret);
5522
4.80k
}
5523
5524
/**
5525
 * Free a regexp.
5526
 *
5527
 * @param regexp  the regexp
5528
 */
5529
void
5530
6.74k
xmlRegFreeRegexp(xmlRegexp *regexp) {
5531
6.74k
    int i;
5532
6.74k
    if (regexp == NULL)
5533
1.96k
  return;
5534
5535
4.78k
    if (regexp->string != NULL)
5536
4.78k
  xmlFree(regexp->string);
5537
4.78k
    if (regexp->states != NULL) {
5538
47.2k
  for (i = 0;i < regexp->nbStates;i++)
5539
42.4k
      xmlRegFreeState(regexp->states[i]);
5540
4.78k
  xmlFree(regexp->states);
5541
4.78k
    }
5542
4.78k
    if (regexp->atoms != NULL) {
5543
27.9k
  for (i = 0;i < regexp->nbAtoms;i++)
5544
23.1k
      xmlRegFreeAtom(regexp->atoms[i]);
5545
4.77k
  xmlFree(regexp->atoms);
5546
4.77k
    }
5547
4.78k
    if (regexp->counters != NULL)
5548
1.09k
  xmlFree(regexp->counters);
5549
4.78k
    if (regexp->compact != NULL)
5550
0
  xmlFree(regexp->compact);
5551
4.78k
    if (regexp->transdata != NULL)
5552
0
  xmlFree(regexp->transdata);
5553
4.78k
    if (regexp->stringMap != NULL) {
5554
0
  for (i = 0; i < regexp->nbstrings;i++)
5555
0
      xmlFree(regexp->stringMap[i]);
5556
0
  xmlFree(regexp->stringMap);
5557
0
    }
5558
5559
4.78k
    xmlFree(regexp);
5560
4.78k
}
5561
5562
/************************************************************************
5563
 *                  *
5564
 *      The Automata interface        *
5565
 *                  *
5566
 ************************************************************************/
5567
5568
/**
5569
 * Create a new automata
5570
 *
5571
 * @deprecated Internal function, don't use.
5572
 *
5573
 * @returns the new object or NULL in case of failure
5574
 */
5575
xmlAutomata *
5576
4.80k
xmlNewAutomata(void) {
5577
4.80k
    xmlAutomataPtr ctxt;
5578
5579
4.80k
    ctxt = xmlRegNewParserCtxt(NULL);
5580
4.80k
    if (ctxt == NULL)
5581
6
  return(NULL);
5582
5583
    /* initialize the parser */
5584
4.79k
    ctxt->state = xmlRegStatePush(ctxt);
5585
4.79k
    if (ctxt->state == NULL) {
5586
9
  xmlFreeAutomata(ctxt);
5587
9
  return(NULL);
5588
9
    }
5589
4.78k
    ctxt->start = ctxt->state;
5590
4.78k
    ctxt->end = NULL;
5591
5592
4.78k
    ctxt->start->type = XML_REGEXP_START_STATE;
5593
4.78k
    ctxt->flags = 0;
5594
5595
4.78k
    return(ctxt);
5596
4.79k
}
5597
5598
/**
5599
 * Free an automata
5600
 *
5601
 * @deprecated Internal function, don't use.
5602
 *
5603
 * @param am  an automata
5604
 */
5605
void
5606
4.79k
xmlFreeAutomata(xmlAutomata *am) {
5607
4.79k
    if (am == NULL)
5608
0
  return;
5609
4.79k
    xmlRegFreeParserCtxt(am);
5610
4.79k
}
5611
5612
/**
5613
 * Set some flags on the automata
5614
 *
5615
 * @deprecated Internal function, don't use.
5616
 *
5617
 * @param am  an automata
5618
 * @param flags  a set of internal flags
5619
 */
5620
void
5621
0
xmlAutomataSetFlags(xmlAutomata *am, int flags) {
5622
0
    if (am == NULL)
5623
0
  return;
5624
0
    am->flags |= flags;
5625
0
}
5626
5627
/**
5628
 * Initial state lookup
5629
 *
5630
 * @deprecated Internal function, don't use.
5631
 *
5632
 * @param am  an automata
5633
 * @returns the initial state of the automata
5634
 */
5635
xmlAutomataState *
5636
0
xmlAutomataGetInitState(xmlAutomata *am) {
5637
0
    if (am == NULL)
5638
0
  return(NULL);
5639
0
    return(am->start);
5640
0
}
5641
5642
/**
5643
 * Makes that state a final state
5644
 *
5645
 * @deprecated Internal function, don't use.
5646
 *
5647
 * @param am  an automata
5648
 * @param state  a state in this automata
5649
 * @returns 0 or -1 in case of error
5650
 */
5651
int
5652
0
xmlAutomataSetFinalState(xmlAutomata *am, xmlAutomataState *state) {
5653
0
    if ((am == NULL) || (state == NULL))
5654
0
  return(-1);
5655
0
    state->type = XML_REGEXP_FINAL_STATE;
5656
0
    return(0);
5657
0
}
5658
5659
/**
5660
 * Add a transition.
5661
 *
5662
 * If `to` is NULL, this creates first a new target state in the automata
5663
 * and then adds a transition from the `from` state to the target state
5664
 * activated by the value of `token`
5665
 *
5666
 * @deprecated Internal function, don't use.
5667
 *
5668
 * @param am  an automata
5669
 * @param from  the starting point of the transition
5670
 * @param to  the target point of the transition or NULL
5671
 * @param token  the input string associated to that transition
5672
 * @param data  data passed to the callback function if the transition is activated
5673
 * @returns the target state or NULL in case of error
5674
 */
5675
xmlAutomataState *
5676
xmlAutomataNewTransition(xmlAutomata *am, xmlAutomataState *from,
5677
       xmlAutomataState *to, const xmlChar *token,
5678
0
       void *data) {
5679
0
    xmlRegAtomPtr atom;
5680
5681
0
    if ((am == NULL) || (from == NULL) || (token == NULL))
5682
0
  return(NULL);
5683
0
    atom = xmlRegNewAtom(am, XML_REGEXP_STRING);
5684
0
    if (atom == NULL)
5685
0
        return(NULL);
5686
0
    atom->data = data;
5687
0
    atom->valuep = xmlStrdup(token);
5688
0
    if (atom->valuep == NULL) {
5689
0
        xmlRegFreeAtom(atom);
5690
0
        xmlRegexpErrMemory(am);
5691
0
        return(NULL);
5692
0
    }
5693
5694
0
    if (xmlFAGenerateTransitions(am, from, to, atom) < 0) {
5695
0
        xmlRegFreeAtom(atom);
5696
0
  return(NULL);
5697
0
    }
5698
0
    if (to == NULL)
5699
0
  return(am->state);
5700
0
    return(to);
5701
0
}
5702
5703
/**
5704
 * If `to` is NULL, this creates first a new target state in the automata
5705
 * and then adds a transition from the `from` state to the target state
5706
 * activated by the value of `token`
5707
 *
5708
 * @deprecated Internal function, don't use.
5709
 *
5710
 * @param am  an automata
5711
 * @param from  the starting point of the transition
5712
 * @param to  the target point of the transition or NULL
5713
 * @param token  the first input string associated to that transition
5714
 * @param token2  the second input string associated to that transition
5715
 * @param data  data passed to the callback function if the transition is activated
5716
 * @returns the target state or NULL in case of error
5717
 */
5718
xmlAutomataState *
5719
xmlAutomataNewTransition2(xmlAutomata *am, xmlAutomataState *from,
5720
        xmlAutomataState *to, const xmlChar *token,
5721
0
        const xmlChar *token2, void *data) {
5722
0
    xmlRegAtomPtr atom;
5723
5724
0
    if ((am == NULL) || (from == NULL) || (token == NULL))
5725
0
  return(NULL);
5726
0
    atom = xmlRegNewAtom(am, XML_REGEXP_STRING);
5727
0
    if (atom == NULL)
5728
0
  return(NULL);
5729
0
    atom->data = data;
5730
0
    if ((token2 == NULL) || (*token2 == 0)) {
5731
0
  atom->valuep = xmlStrdup(token);
5732
0
    } else {
5733
0
  int lenn, lenp;
5734
0
  xmlChar *str;
5735
5736
0
  lenn = strlen((char *) token2);
5737
0
  lenp = strlen((char *) token);
5738
5739
0
  str = xmlMalloc(lenn + lenp + 2);
5740
0
  if (str == NULL) {
5741
0
      xmlRegFreeAtom(atom);
5742
0
      return(NULL);
5743
0
  }
5744
0
  memcpy(&str[0], token, lenp);
5745
0
  str[lenp] = '|';
5746
0
  memcpy(&str[lenp + 1], token2, lenn);
5747
0
  str[lenn + lenp + 1] = 0;
5748
5749
0
  atom->valuep = str;
5750
0
    }
5751
5752
0
    if (xmlFAGenerateTransitions(am, from, to, atom) < 0) {
5753
0
        xmlRegFreeAtom(atom);
5754
0
  return(NULL);
5755
0
    }
5756
0
    if (to == NULL)
5757
0
  return(am->state);
5758
0
    return(to);
5759
0
}
5760
5761
/**
5762
 * If `to` is NULL, this creates first a new target state in the automata
5763
 * and then adds a transition from the `from` state to the target state
5764
 * activated by any value except (`token`,`token2`)
5765
 * Note that if `token2` is not NULL, then (X, NULL) won't match to follow
5766
 * the semantic of XSD \#\#other
5767
 *
5768
 * @deprecated Internal function, don't use.
5769
 *
5770
 * @param am  an automata
5771
 * @param from  the starting point of the transition
5772
 * @param to  the target point of the transition or NULL
5773
 * @param token  the first input string associated to that transition
5774
 * @param token2  the second input string associated to that transition
5775
 * @param data  data passed to the callback function if the transition is activated
5776
 * @returns the target state or NULL in case of error
5777
 */
5778
xmlAutomataState *
5779
xmlAutomataNewNegTrans(xmlAutomata *am, xmlAutomataState *from,
5780
           xmlAutomataState *to, const xmlChar *token,
5781
0
           const xmlChar *token2, void *data) {
5782
0
    xmlRegAtomPtr atom;
5783
0
    xmlChar err_msg[200];
5784
5785
0
    if ((am == NULL) || (from == NULL) || (token == NULL))
5786
0
  return(NULL);
5787
0
    atom = xmlRegNewAtom(am, XML_REGEXP_STRING);
5788
0
    if (atom == NULL)
5789
0
  return(NULL);
5790
0
    atom->data = data;
5791
0
    atom->neg = 1;
5792
0
    if ((token2 == NULL) || (*token2 == 0)) {
5793
0
  atom->valuep = xmlStrdup(token);
5794
0
    } else {
5795
0
  int lenn, lenp;
5796
0
  xmlChar *str;
5797
5798
0
  lenn = strlen((char *) token2);
5799
0
  lenp = strlen((char *) token);
5800
5801
0
  str = xmlMalloc(lenn + lenp + 2);
5802
0
  if (str == NULL) {
5803
0
      xmlRegFreeAtom(atom);
5804
0
      return(NULL);
5805
0
  }
5806
0
  memcpy(&str[0], token, lenp);
5807
0
  str[lenp] = '|';
5808
0
  memcpy(&str[lenp + 1], token2, lenn);
5809
0
  str[lenn + lenp + 1] = 0;
5810
5811
0
  atom->valuep = str;
5812
0
    }
5813
0
    snprintf((char *) err_msg, 199, "not %s", (const char *) atom->valuep);
5814
0
    err_msg[199] = 0;
5815
0
    atom->valuep2 = xmlStrdup(err_msg);
5816
5817
0
    if (xmlFAGenerateTransitions(am, from, to, atom) < 0) {
5818
0
        xmlRegFreeAtom(atom);
5819
0
  return(NULL);
5820
0
    }
5821
0
    am->negs++;
5822
0
    if (to == NULL)
5823
0
  return(am->state);
5824
0
    return(to);
5825
0
}
5826
5827
/**
5828
 * If `to` is NULL, this creates first a new target state in the automata
5829
 * and then adds a transition from the `from` state to the target state
5830
 * activated by a succession of input of value `token` and `token2` and
5831
 * whose number is between `min` and `max`
5832
 *
5833
 * @deprecated Internal function, don't use.
5834
 *
5835
 * @param am  an automata
5836
 * @param from  the starting point of the transition
5837
 * @param to  the target point of the transition or NULL
5838
 * @param token  the input string associated to that transition
5839
 * @param token2  the second input string associated to that transition
5840
 * @param min  the minimum successive occurrences of token
5841
 * @param max  the maximum successive occurrences of token
5842
 * @param data  data associated to the transition
5843
 * @returns the target state or NULL in case of error
5844
 */
5845
xmlAutomataState *
5846
xmlAutomataNewCountTrans2(xmlAutomata *am, xmlAutomataState *from,
5847
       xmlAutomataState *to, const xmlChar *token,
5848
       const xmlChar *token2,
5849
0
       int min, int max, void *data) {
5850
0
    xmlRegAtomPtr atom;
5851
0
    int counter;
5852
5853
0
    if ((am == NULL) || (from == NULL) || (token == NULL))
5854
0
  return(NULL);
5855
0
    if (min < 0)
5856
0
  return(NULL);
5857
0
    if ((max < min) || (max < 1))
5858
0
  return(NULL);
5859
0
    atom = xmlRegNewAtom(am, XML_REGEXP_STRING);
5860
0
    if (atom == NULL)
5861
0
  return(NULL);
5862
0
    if ((token2 == NULL) || (*token2 == 0)) {
5863
0
  atom->valuep = xmlStrdup(token);
5864
0
        if (atom->valuep == NULL)
5865
0
            goto error;
5866
0
    } else {
5867
0
  int lenn, lenp;
5868
0
  xmlChar *str;
5869
5870
0
  lenn = strlen((char *) token2);
5871
0
  lenp = strlen((char *) token);
5872
5873
0
  str = xmlMalloc(lenn + lenp + 2);
5874
0
  if (str == NULL)
5875
0
      goto error;
5876
0
  memcpy(&str[0], token, lenp);
5877
0
  str[lenp] = '|';
5878
0
  memcpy(&str[lenp + 1], token2, lenn);
5879
0
  str[lenn + lenp + 1] = 0;
5880
5881
0
  atom->valuep = str;
5882
0
    }
5883
0
    atom->data = data;
5884
0
    if (min == 0)
5885
0
  atom->min = 1;
5886
0
    else
5887
0
  atom->min = min;
5888
0
    atom->max = max;
5889
5890
    /*
5891
     * associate a counter to the transition.
5892
     */
5893
0
    counter = xmlRegGetCounter(am);
5894
0
    if (counter < 0)
5895
0
        goto error;
5896
0
    am->counters[counter].min = min;
5897
0
    am->counters[counter].max = max;
5898
5899
    /* xmlFAGenerateTransitions(am, from, to, atom); */
5900
0
    if (to == NULL) {
5901
0
  to = xmlRegStatePush(am);
5902
0
        if (to == NULL)
5903
0
            goto error;
5904
0
    }
5905
0
    xmlRegStateAddTrans(am, from, atom, to, counter, -1);
5906
0
    if (xmlRegAtomPush(am, atom) < 0)
5907
0
        goto error;
5908
0
    am->state = to;
5909
5910
0
    if (to == NULL)
5911
0
  to = am->state;
5912
0
    if (to == NULL)
5913
0
  return(NULL);
5914
0
    if (min == 0)
5915
0
  xmlFAGenerateEpsilonTransition(am, from, to);
5916
0
    return(to);
5917
5918
0
error:
5919
0
    xmlRegFreeAtom(atom);
5920
0
    return(NULL);
5921
0
}
5922
5923
/**
5924
 * If `to` is NULL, this creates first a new target state in the automata
5925
 * and then adds a transition from the `from` state to the target state
5926
 * activated by a succession of input of value `token` and whose number
5927
 * is between `min` and `max`
5928
 *
5929
 * @deprecated Internal function, don't use.
5930
 *
5931
 * @param am  an automata
5932
 * @param from  the starting point of the transition
5933
 * @param to  the target point of the transition or NULL
5934
 * @param token  the input string associated to that transition
5935
 * @param min  the minimum successive occurrences of token
5936
 * @param max  the maximum successive occurrences of token
5937
 * @param data  data associated to the transition
5938
 * @returns the target state or NULL in case of error
5939
 */
5940
xmlAutomataState *
5941
xmlAutomataNewCountTrans(xmlAutomata *am, xmlAutomataState *from,
5942
       xmlAutomataState *to, const xmlChar *token,
5943
0
       int min, int max, void *data) {
5944
0
    xmlRegAtomPtr atom;
5945
0
    int counter;
5946
5947
0
    if ((am == NULL) || (from == NULL) || (token == NULL))
5948
0
  return(NULL);
5949
0
    if (min < 0)
5950
0
  return(NULL);
5951
0
    if ((max < min) || (max < 1))
5952
0
  return(NULL);
5953
0
    atom = xmlRegNewAtom(am, XML_REGEXP_STRING);
5954
0
    if (atom == NULL)
5955
0
  return(NULL);
5956
0
    atom->valuep = xmlStrdup(token);
5957
0
    if (atom->valuep == NULL)
5958
0
        goto error;
5959
0
    atom->data = data;
5960
0
    if (min == 0)
5961
0
  atom->min = 1;
5962
0
    else
5963
0
  atom->min = min;
5964
0
    atom->max = max;
5965
5966
    /*
5967
     * associate a counter to the transition.
5968
     */
5969
0
    counter = xmlRegGetCounter(am);
5970
0
    if (counter < 0)
5971
0
        goto error;
5972
0
    am->counters[counter].min = min;
5973
0
    am->counters[counter].max = max;
5974
5975
    /* xmlFAGenerateTransitions(am, from, to, atom); */
5976
0
    if (to == NULL) {
5977
0
  to = xmlRegStatePush(am);
5978
0
        if (to == NULL)
5979
0
            goto error;
5980
0
    }
5981
0
    xmlRegStateAddTrans(am, from, atom, to, counter, -1);
5982
0
    if (xmlRegAtomPush(am, atom) < 0)
5983
0
        goto error;
5984
0
    am->state = to;
5985
5986
0
    if (to == NULL)
5987
0
  to = am->state;
5988
0
    if (to == NULL)
5989
0
  return(NULL);
5990
0
    if (min == 0)
5991
0
  xmlFAGenerateEpsilonTransition(am, from, to);
5992
0
    return(to);
5993
5994
0
error:
5995
0
    xmlRegFreeAtom(atom);
5996
0
    return(NULL);
5997
0
}
5998
5999
/**
6000
 * If `to` is NULL, this creates first a new target state in the automata
6001
 * and then adds a transition from the `from` state to the target state
6002
 * activated by a succession of input of value `token` and `token2` and whose
6003
 * number is between `min` and `max`, moreover that transition can only be
6004
 * crossed once.
6005
 *
6006
 * @deprecated Internal function, don't use.
6007
 *
6008
 * @param am  an automata
6009
 * @param from  the starting point of the transition
6010
 * @param to  the target point of the transition or NULL
6011
 * @param token  the input string associated to that transition
6012
 * @param token2  the second input string associated to that transition
6013
 * @param min  the minimum successive occurrences of token
6014
 * @param max  the maximum successive occurrences of token
6015
 * @param data  data associated to the transition
6016
 * @returns the target state or NULL in case of error
6017
 */
6018
xmlAutomataState *
6019
xmlAutomataNewOnceTrans2(xmlAutomata *am, xmlAutomataState *from,
6020
       xmlAutomataState *to, const xmlChar *token,
6021
       const xmlChar *token2,
6022
0
       int min, int max, void *data) {
6023
0
    xmlRegAtomPtr atom;
6024
0
    int counter;
6025
6026
0
    if ((am == NULL) || (from == NULL) || (token == NULL))
6027
0
  return(NULL);
6028
0
    if (min < 1)
6029
0
  return(NULL);
6030
0
    if (max < min)
6031
0
  return(NULL);
6032
0
    atom = xmlRegNewAtom(am, XML_REGEXP_STRING);
6033
0
    if (atom == NULL)
6034
0
  return(NULL);
6035
0
    if ((token2 == NULL) || (*token2 == 0)) {
6036
0
  atom->valuep = xmlStrdup(token);
6037
0
        if (atom->valuep == NULL)
6038
0
            goto error;
6039
0
    } else {
6040
0
  int lenn, lenp;
6041
0
  xmlChar *str;
6042
6043
0
  lenn = strlen((char *) token2);
6044
0
  lenp = strlen((char *) token);
6045
6046
0
  str = xmlMalloc(lenn + lenp + 2);
6047
0
  if (str == NULL)
6048
0
      goto error;
6049
0
  memcpy(&str[0], token, lenp);
6050
0
  str[lenp] = '|';
6051
0
  memcpy(&str[lenp + 1], token2, lenn);
6052
0
  str[lenn + lenp + 1] = 0;
6053
6054
0
  atom->valuep = str;
6055
0
    }
6056
0
    atom->data = data;
6057
0
    atom->quant = XML_REGEXP_QUANT_ONCEONLY;
6058
0
    atom->min = min;
6059
0
    atom->max = max;
6060
    /*
6061
     * associate a counter to the transition.
6062
     */
6063
0
    counter = xmlRegGetCounter(am);
6064
0
    if (counter < 0)
6065
0
        goto error;
6066
0
    am->counters[counter].min = 1;
6067
0
    am->counters[counter].max = 1;
6068
6069
    /* xmlFAGenerateTransitions(am, from, to, atom); */
6070
0
    if (to == NULL) {
6071
0
  to = xmlRegStatePush(am);
6072
0
        if (to == NULL)
6073
0
            goto error;
6074
0
    }
6075
0
    xmlRegStateAddTrans(am, from, atom, to, counter, -1);
6076
0
    if (xmlRegAtomPush(am, atom) < 0)
6077
0
        goto error;
6078
0
    am->state = to;
6079
0
    return(to);
6080
6081
0
error:
6082
0
    xmlRegFreeAtom(atom);
6083
0
    return(NULL);
6084
0
}
6085
6086
6087
6088
/**
6089
 * If `to` is NULL, this creates first a new target state in the automata
6090
 * and then adds a transition from the `from` state to the target state
6091
 * activated by a succession of input of value `token` and whose number
6092
 * is between `min` and `max`, moreover that transition can only be crossed
6093
 * once.
6094
 *
6095
 * @deprecated Internal function, don't use.
6096
 *
6097
 * @param am  an automata
6098
 * @param from  the starting point of the transition
6099
 * @param to  the target point of the transition or NULL
6100
 * @param token  the input string associated to that transition
6101
 * @param min  the minimum successive occurrences of token
6102
 * @param max  the maximum successive occurrences of token
6103
 * @param data  data associated to the transition
6104
 * @returns the target state or NULL in case of error
6105
 */
6106
xmlAutomataState *
6107
xmlAutomataNewOnceTrans(xmlAutomata *am, xmlAutomataState *from,
6108
       xmlAutomataState *to, const xmlChar *token,
6109
0
       int min, int max, void *data) {
6110
0
    xmlRegAtomPtr atom;
6111
0
    int counter;
6112
6113
0
    if ((am == NULL) || (from == NULL) || (token == NULL))
6114
0
  return(NULL);
6115
0
    if (min < 1)
6116
0
  return(NULL);
6117
0
    if (max < min)
6118
0
  return(NULL);
6119
0
    atom = xmlRegNewAtom(am, XML_REGEXP_STRING);
6120
0
    if (atom == NULL)
6121
0
  return(NULL);
6122
0
    atom->valuep = xmlStrdup(token);
6123
0
    atom->data = data;
6124
0
    atom->quant = XML_REGEXP_QUANT_ONCEONLY;
6125
0
    atom->min = min;
6126
0
    atom->max = max;
6127
    /*
6128
     * associate a counter to the transition.
6129
     */
6130
0
    counter = xmlRegGetCounter(am);
6131
0
    if (counter < 0)
6132
0
        goto error;
6133
0
    am->counters[counter].min = 1;
6134
0
    am->counters[counter].max = 1;
6135
6136
    /* xmlFAGenerateTransitions(am, from, to, atom); */
6137
0
    if (to == NULL) {
6138
0
  to = xmlRegStatePush(am);
6139
0
        if (to == NULL)
6140
0
            goto error;
6141
0
    }
6142
0
    xmlRegStateAddTrans(am, from, atom, to, counter, -1);
6143
0
    if (xmlRegAtomPush(am, atom) < 0)
6144
0
        goto error;
6145
0
    am->state = to;
6146
0
    return(to);
6147
6148
0
error:
6149
0
    xmlRegFreeAtom(atom);
6150
0
    return(NULL);
6151
0
}
6152
6153
/**
6154
 * Create a new disconnected state in the automata
6155
 *
6156
 * @deprecated Internal function, don't use.
6157
 *
6158
 * @param am  an automata
6159
 * @returns the new state or NULL in case of error
6160
 */
6161
xmlAutomataState *
6162
0
xmlAutomataNewState(xmlAutomata *am) {
6163
0
    if (am == NULL)
6164
0
  return(NULL);
6165
0
    return(xmlRegStatePush(am));
6166
0
}
6167
6168
/**
6169
 * If `to` is NULL, this creates first a new target state in the automata
6170
 * and then adds an epsilon transition from the `from` state to the
6171
 * target state
6172
 *
6173
 * @deprecated Internal function, don't use.
6174
 *
6175
 * @param am  an automata
6176
 * @param from  the starting point of the transition
6177
 * @param to  the target point of the transition or NULL
6178
 * @returns the target state or NULL in case of error
6179
 */
6180
xmlAutomataState *
6181
xmlAutomataNewEpsilon(xmlAutomata *am, xmlAutomataState *from,
6182
0
          xmlAutomataState *to) {
6183
0
    if ((am == NULL) || (from == NULL))
6184
0
  return(NULL);
6185
0
    xmlFAGenerateEpsilonTransition(am, from, to);
6186
0
    if (to == NULL)
6187
0
  return(am->state);
6188
0
    return(to);
6189
0
}
6190
6191
/**
6192
 * If `to` is NULL, this creates first a new target state in the automata
6193
 * and then adds a an ALL transition from the `from` state to the
6194
 * target state. That transition is an epsilon transition allowed only when
6195
 * all transitions from the `from` node have been activated.
6196
 *
6197
 * @deprecated Internal function, don't use.
6198
 *
6199
 * @param am  an automata
6200
 * @param from  the starting point of the transition
6201
 * @param to  the target point of the transition or NULL
6202
 * @param lax  allow to transition if not all all transitions have been activated
6203
 * @returns the target state or NULL in case of error
6204
 */
6205
xmlAutomataState *
6206
xmlAutomataNewAllTrans(xmlAutomata *am, xmlAutomataState *from,
6207
0
           xmlAutomataState *to, int lax) {
6208
0
    if ((am == NULL) || (from == NULL))
6209
0
  return(NULL);
6210
0
    xmlFAGenerateAllTransition(am, from, to, lax);
6211
0
    if (to == NULL)
6212
0
  return(am->state);
6213
0
    return(to);
6214
0
}
6215
6216
/**
6217
 * Create a new counter
6218
 *
6219
 * @deprecated Internal function, don't use.
6220
 *
6221
 * @param am  an automata
6222
 * @param min  the minimal value on the counter
6223
 * @param max  the maximal value on the counter
6224
 * @returns the counter number or -1 in case of error
6225
 */
6226
int
6227
0
xmlAutomataNewCounter(xmlAutomata *am, int min, int max) {
6228
0
    int ret;
6229
6230
0
    if (am == NULL)
6231
0
  return(-1);
6232
6233
0
    ret = xmlRegGetCounter(am);
6234
0
    if (ret < 0)
6235
0
  return(-1);
6236
0
    am->counters[ret].min = min;
6237
0
    am->counters[ret].max = max;
6238
0
    return(ret);
6239
0
}
6240
6241
/**
6242
 * If `to` is NULL, this creates first a new target state in the automata
6243
 * and then adds an epsilon transition from the `from` state to the target state
6244
 * which will increment the counter provided
6245
 *
6246
 * @deprecated Internal function, don't use.
6247
 *
6248
 * @param am  an automata
6249
 * @param from  the starting point of the transition
6250
 * @param to  the target point of the transition or NULL
6251
 * @param counter  the counter associated to that transition
6252
 * @returns the target state or NULL in case of error
6253
 */
6254
xmlAutomataState *
6255
xmlAutomataNewCountedTrans(xmlAutomata *am, xmlAutomataState *from,
6256
0
    xmlAutomataState *to, int counter) {
6257
0
    if ((am == NULL) || (from == NULL) || (counter < 0))
6258
0
  return(NULL);
6259
0
    xmlFAGenerateCountedEpsilonTransition(am, from, to, counter);
6260
0
    if (to == NULL)
6261
0
  return(am->state);
6262
0
    return(to);
6263
0
}
6264
6265
/**
6266
 * If `to` is NULL, this creates first a new target state in the automata
6267
 * and then adds an epsilon transition from the `from` state to the target state
6268
 * which will be allowed only if the counter is within the right range.
6269
 *
6270
 * @deprecated Internal function, don't use.
6271
 *
6272
 * @param am  an automata
6273
 * @param from  the starting point of the transition
6274
 * @param to  the target point of the transition or NULL
6275
 * @param counter  the counter associated to that transition
6276
 * @returns the target state or NULL in case of error
6277
 */
6278
xmlAutomataState *
6279
xmlAutomataNewCounterTrans(xmlAutomata *am, xmlAutomataState *from,
6280
0
    xmlAutomataState *to, int counter) {
6281
0
    if ((am == NULL) || (from == NULL) || (counter < 0))
6282
0
  return(NULL);
6283
0
    xmlFAGenerateCountedTransition(am, from, to, counter);
6284
0
    if (to == NULL)
6285
0
  return(am->state);
6286
0
    return(to);
6287
0
}
6288
6289
/**
6290
 * Compile the automata into a Reg Exp ready for being executed.
6291
 * The automata should be free after this point.
6292
 *
6293
 * @deprecated Internal function, don't use.
6294
 *
6295
 * @param am  an automata
6296
 * @returns the compiled regexp or NULL in case of error
6297
 */
6298
xmlRegexp *
6299
0
xmlAutomataCompile(xmlAutomata *am) {
6300
0
    xmlRegexpPtr ret;
6301
6302
0
    if ((am == NULL) || (am->error != 0)) return(NULL);
6303
0
    xmlFAEliminateEpsilonTransitions(am);
6304
0
    if (am->error != 0)
6305
0
        return(NULL);
6306
    /* xmlFAComputesDeterminism(am); */
6307
0
    ret = xmlRegEpxFromParse(am);
6308
6309
0
    return(ret);
6310
0
}
6311
6312
/**
6313
 * Checks if an automata is determinist.
6314
 *
6315
 * @deprecated Internal function, don't use.
6316
 *
6317
 * @param am  an automata
6318
 * @returns 1 if true, 0 if not, and -1 in case of error
6319
 */
6320
int
6321
0
xmlAutomataIsDeterminist(xmlAutomata *am) {
6322
0
    int ret;
6323
6324
0
    if (am == NULL)
6325
0
  return(-1);
6326
6327
0
    ret = xmlFAComputesDeterminism(am);
6328
0
    return(ret);
6329
0
}
6330
6331
#endif /* LIBXML_REGEXP_ENABLED */