Coverage Report

Created: 2025-10-13 06:09

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