Coverage Report

Created: 2026-04-27 07:05

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