Coverage Report

Created: 2026-10-17 06:12

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