Coverage Report

Created: 2026-07-16 06:18

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