Coverage Report

Created: 2025-11-24 06:35

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