Coverage Report

Created: 2025-07-11 06:47

/src/tinysparql/subprojects/libxml2-2.13.1/xmlregexp.c
Line
Count
Source (jump to first uncovered line)
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
 * Daniel Veillard <veillard@redhat.com>
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
#include <libxml/xmlunicode.h>
31
32
#include "private/error.h"
33
#include "private/regexp.h"
34
35
#ifndef SIZE_MAX
36
0
#define SIZE_MAX ((size_t) -1)
37
#endif
38
39
0
#define MAX_PUSH 10000000
40
41
#ifdef ERROR
42
#undef ERROR
43
#endif
44
#define ERROR(str)              \
45
0
    ctxt->error = XML_REGEXP_COMPILE_ERROR;       \
46
0
    xmlRegexpErrCompile(ctxt, str);
47
0
#define NEXT ctxt->cur++
48
0
#define CUR (*(ctxt->cur))
49
0
#define NXT(index) (ctxt->cur[index])
50
51
0
#define NEXTL(l) ctxt->cur += l;
52
0
#define XML_REG_STRING_SEPARATOR '|'
53
/*
54
 * Need PREV to check on a '-' within a Character Group. May only be used
55
 * when it's guaranteed that cur is not at the beginning of ctxt->string!
56
 */
57
0
#define PREV (ctxt->cur[-1])
58
59
/************************************************************************
60
 *                  *
61
 *      Datatypes and structures      *
62
 *                  *
63
 ************************************************************************/
64
65
/*
66
 * Note: the order of the enums below is significant, do not shuffle
67
 */
68
typedef enum {
69
    XML_REGEXP_EPSILON = 1,
70
    XML_REGEXP_CHARVAL,
71
    XML_REGEXP_RANGES,
72
    XML_REGEXP_SUBREG,  /* used for () sub regexps */
73
    XML_REGEXP_STRING,
74
    XML_REGEXP_ANYCHAR, /* . */
75
    XML_REGEXP_ANYSPACE, /* \s */
76
    XML_REGEXP_NOTSPACE, /* \S */
77
    XML_REGEXP_INITNAME, /* \l */
78
    XML_REGEXP_NOTINITNAME, /* \L */
79
    XML_REGEXP_NAMECHAR, /* \c */
80
    XML_REGEXP_NOTNAMECHAR, /* \C */
81
    XML_REGEXP_DECIMAL, /* \d */
82
    XML_REGEXP_NOTDECIMAL, /* \D */
83
    XML_REGEXP_REALCHAR, /* \w */
84
    XML_REGEXP_NOTREALCHAR, /* \W */
85
    XML_REGEXP_LETTER = 100,
86
    XML_REGEXP_LETTER_UPPERCASE,
87
    XML_REGEXP_LETTER_LOWERCASE,
88
    XML_REGEXP_LETTER_TITLECASE,
89
    XML_REGEXP_LETTER_MODIFIER,
90
    XML_REGEXP_LETTER_OTHERS,
91
    XML_REGEXP_MARK,
92
    XML_REGEXP_MARK_NONSPACING,
93
    XML_REGEXP_MARK_SPACECOMBINING,
94
    XML_REGEXP_MARK_ENCLOSING,
95
    XML_REGEXP_NUMBER,
96
    XML_REGEXP_NUMBER_DECIMAL,
97
    XML_REGEXP_NUMBER_LETTER,
98
    XML_REGEXP_NUMBER_OTHERS,
99
    XML_REGEXP_PUNCT,
100
    XML_REGEXP_PUNCT_CONNECTOR,
101
    XML_REGEXP_PUNCT_DASH,
102
    XML_REGEXP_PUNCT_OPEN,
103
    XML_REGEXP_PUNCT_CLOSE,
104
    XML_REGEXP_PUNCT_INITQUOTE,
105
    XML_REGEXP_PUNCT_FINQUOTE,
106
    XML_REGEXP_PUNCT_OTHERS,
107
    XML_REGEXP_SEPAR,
108
    XML_REGEXP_SEPAR_SPACE,
109
    XML_REGEXP_SEPAR_LINE,
110
    XML_REGEXP_SEPAR_PARA,
111
    XML_REGEXP_SYMBOL,
112
    XML_REGEXP_SYMBOL_MATH,
113
    XML_REGEXP_SYMBOL_CURRENCY,
114
    XML_REGEXP_SYMBOL_MODIFIER,
115
    XML_REGEXP_SYMBOL_OTHERS,
116
    XML_REGEXP_OTHER,
117
    XML_REGEXP_OTHER_CONTROL,
118
    XML_REGEXP_OTHER_FORMAT,
119
    XML_REGEXP_OTHER_PRIVATE,
120
    XML_REGEXP_OTHER_NA,
121
    XML_REGEXP_BLOCK_NAME
122
} xmlRegAtomType;
123
124
typedef enum {
125
    XML_REGEXP_QUANT_EPSILON = 1,
126
    XML_REGEXP_QUANT_ONCE,
127
    XML_REGEXP_QUANT_OPT,
128
    XML_REGEXP_QUANT_MULT,
129
    XML_REGEXP_QUANT_PLUS,
130
    XML_REGEXP_QUANT_ONCEONLY,
131
    XML_REGEXP_QUANT_ALL,
132
    XML_REGEXP_QUANT_RANGE
133
} xmlRegQuantType;
134
135
typedef enum {
136
    XML_REGEXP_START_STATE = 1,
137
    XML_REGEXP_FINAL_STATE,
138
    XML_REGEXP_TRANS_STATE,
139
    XML_REGEXP_SINK_STATE,
140
    XML_REGEXP_UNREACH_STATE
141
} xmlRegStateType;
142
143
typedef enum {
144
    XML_REGEXP_MARK_NORMAL = 0,
145
    XML_REGEXP_MARK_START,
146
    XML_REGEXP_MARK_VISITED
147
} xmlRegMarkedType;
148
149
typedef struct _xmlRegRange xmlRegRange;
150
typedef xmlRegRange *xmlRegRangePtr;
151
152
struct _xmlRegRange {
153
    int neg;    /* 0 normal, 1 not, 2 exclude */
154
    xmlRegAtomType type;
155
    int start;
156
    int end;
157
    xmlChar *blockName;
158
};
159
160
typedef struct _xmlRegAtom xmlRegAtom;
161
typedef xmlRegAtom *xmlRegAtomPtr;
162
163
typedef struct _xmlAutomataState xmlRegState;
164
typedef xmlRegState *xmlRegStatePtr;
165
166
struct _xmlRegAtom {
167
    int no;
168
    xmlRegAtomType type;
169
    xmlRegQuantType quant;
170
    int min;
171
    int max;
172
173
    void *valuep;
174
    void *valuep2;
175
    int neg;
176
    int codepoint;
177
    xmlRegStatePtr start;
178
    xmlRegStatePtr start0;
179
    xmlRegStatePtr stop;
180
    int maxRanges;
181
    int nbRanges;
182
    xmlRegRangePtr *ranges;
183
    void *data;
184
};
185
186
typedef struct _xmlRegCounter xmlRegCounter;
187
typedef xmlRegCounter *xmlRegCounterPtr;
188
189
struct _xmlRegCounter {
190
    int min;
191
    int max;
192
};
193
194
typedef struct _xmlRegTrans xmlRegTrans;
195
typedef xmlRegTrans *xmlRegTransPtr;
196
197
struct _xmlRegTrans {
198
    xmlRegAtomPtr atom;
199
    int to;
200
    int counter;
201
    int count;
202
    int nd;
203
};
204
205
struct _xmlAutomataState {
206
    xmlRegStateType type;
207
    xmlRegMarkedType mark;
208
    xmlRegMarkedType markd;
209
    xmlRegMarkedType reached;
210
    int no;
211
    int maxTrans;
212
    int nbTrans;
213
    xmlRegTrans *trans;
214
    /*  knowing states pointing to us can speed things up */
215
    int maxTransTo;
216
    int nbTransTo;
217
    int *transTo;
218
};
219
220
typedef struct _xmlAutomata xmlRegParserCtxt;
221
typedef xmlRegParserCtxt *xmlRegParserCtxtPtr;
222
223
0
#define AM_AUTOMATA_RNG 1
224
225
struct _xmlAutomata {
226
    xmlChar *string;
227
    xmlChar *cur;
228
229
    int error;
230
    int neg;
231
232
    xmlRegStatePtr start;
233
    xmlRegStatePtr end;
234
    xmlRegStatePtr state;
235
236
    xmlRegAtomPtr atom;
237
238
    int maxAtoms;
239
    int nbAtoms;
240
    xmlRegAtomPtr *atoms;
241
242
    int maxStates;
243
    int nbStates;
244
    xmlRegStatePtr *states;
245
246
    int maxCounters;
247
    int nbCounters;
248
    xmlRegCounter *counters;
249
250
    int determinist;
251
    int negs;
252
    int flags;
253
254
    int depth;
255
};
256
257
struct _xmlRegexp {
258
    xmlChar *string;
259
    int nbStates;
260
    xmlRegStatePtr *states;
261
    int nbAtoms;
262
    xmlRegAtomPtr *atoms;
263
    int nbCounters;
264
    xmlRegCounter *counters;
265
    int determinist;
266
    int flags;
267
    /*
268
     * That's the compact form for determinists automatas
269
     */
270
    int nbstates;
271
    int *compact;
272
    void **transdata;
273
    int nbstrings;
274
    xmlChar **stringMap;
275
};
276
277
typedef struct _xmlRegExecRollback xmlRegExecRollback;
278
typedef xmlRegExecRollback *xmlRegExecRollbackPtr;
279
280
struct _xmlRegExecRollback {
281
    xmlRegStatePtr state;/* the current state */
282
    int index;    /* the index in the input stack */
283
    int nextbranch; /* the next transition to explore in that state */
284
    int *counts;  /* save the automata state if it has some */
285
};
286
287
typedef struct _xmlRegInputToken xmlRegInputToken;
288
typedef xmlRegInputToken *xmlRegInputTokenPtr;
289
290
struct _xmlRegInputToken {
291
    xmlChar *value;
292
    void *data;
293
};
294
295
struct _xmlRegExecCtxt {
296
    int status;   /* execution status != 0 indicate an error */
297
    int determinist;  /* did we find an indeterministic behaviour */
298
    xmlRegexpPtr comp;  /* the compiled regexp */
299
    xmlRegExecCallbacks callback;
300
    void *data;
301
302
    xmlRegStatePtr state;/* the current state */
303
    int transno;  /* the current transition on that state */
304
    int transcount; /* the number of chars in char counted transitions */
305
306
    /*
307
     * A stack of rollback states
308
     */
309
    int maxRollbacks;
310
    int nbRollbacks;
311
    xmlRegExecRollback *rollbacks;
312
313
    /*
314
     * The state of the automata if any
315
     */
316
    int *counts;
317
318
    /*
319
     * The input stack
320
     */
321
    int inputStackMax;
322
    int inputStackNr;
323
    int index;
324
    int *charStack;
325
    const xmlChar *inputString; /* when operating on characters */
326
    xmlRegInputTokenPtr inputStack;/* when operating on strings */
327
328
    /*
329
     * error handling
330
     */
331
    int errStateNo;   /* the error state number */
332
    xmlRegStatePtr errState;    /* the error state */
333
    xmlChar *errString;   /* the string raising the error */
334
    int *errCounts;   /* counters at the error state */
335
    int nbPush;
336
};
337
338
0
#define REGEXP_ALL_COUNTER  0x123456
339
0
#define REGEXP_ALL_LAX_COUNTER  0x123457
340
341
static void xmlFAParseRegExp(xmlRegParserCtxtPtr ctxt, int top);
342
static void xmlRegFreeState(xmlRegStatePtr state);
343
static void xmlRegFreeAtom(xmlRegAtomPtr atom);
344
static int xmlRegStrEqualWildcard(const xmlChar *expStr, const xmlChar *valStr);
345
static int xmlRegCheckCharacter(xmlRegAtomPtr atom, int codepoint);
346
static int xmlRegCheckCharacterRange(xmlRegAtomType type, int codepoint,
347
                  int neg, int start, int end, const xmlChar *blockName);
348
349
/************************************************************************
350
 *                  *
351
 *    Regexp memory error handler       *
352
 *                  *
353
 ************************************************************************/
354
/**
355
 * xmlRegexpErrMemory:
356
 * @extra:  extra information
357
 *
358
 * Handle an out of memory condition
359
 */
360
static void
361
xmlRegexpErrMemory(xmlRegParserCtxtPtr ctxt)
362
0
{
363
0
    if (ctxt != NULL)
364
0
        ctxt->error = XML_ERR_NO_MEMORY;
365
366
0
    xmlRaiseMemoryError(NULL, NULL, NULL, XML_FROM_REGEXP, NULL);
367
0
}
368
369
/**
370
 * xmlRegexpErrCompile:
371
 * @extra:  extra information
372
 *
373
 * Handle a compilation failure
374
 */
375
static void
376
xmlRegexpErrCompile(xmlRegParserCtxtPtr ctxt, const char *extra)
377
0
{
378
0
    const char *regexp = NULL;
379
0
    int idx = 0;
380
0
    int res;
381
382
0
    if (ctxt != NULL) {
383
0
        regexp = (const char *) ctxt->string;
384
0
  idx = ctxt->cur - ctxt->string;
385
0
  ctxt->error = XML_REGEXP_COMPILE_ERROR;
386
0
    }
387
388
0
    res = __xmlRaiseError(NULL, NULL, NULL, NULL, NULL, XML_FROM_REGEXP,
389
0
                          XML_REGEXP_COMPILE_ERROR, XML_ERR_FATAL,
390
0
                          NULL, 0, extra, regexp, NULL, idx, 0,
391
0
                          "failed to compile: %s\n", extra);
392
0
    if (res < 0)
393
0
        xmlRegexpErrMemory(ctxt);
394
0
}
395
396
/************************************************************************
397
 *                  *
398
 *      Allocation/Deallocation       *
399
 *                  *
400
 ************************************************************************/
401
402
static int xmlFAComputesDeterminism(xmlRegParserCtxtPtr ctxt);
403
404
/**
405
 * xmlRegCalloc2:
406
 * @dim1:  size of first dimension
407
 * @dim2:  size of second dimension
408
 * @elemSize:  size of element
409
 *
410
 * Allocate a two-dimensional array and set all elements to zero.
411
 *
412
 * Returns the new array or NULL in case of error.
413
 */
414
static void*
415
0
xmlRegCalloc2(size_t dim1, size_t dim2, size_t elemSize) {
416
0
    size_t totalSize;
417
0
    void *ret;
418
419
    /* Check for overflow */
420
0
    if ((dim2 == 0) || (elemSize == 0) ||
421
0
        (dim1 > SIZE_MAX / dim2 / elemSize))
422
0
        return (NULL);
423
0
    totalSize = dim1 * dim2 * elemSize;
424
0
    ret = xmlMalloc(totalSize);
425
0
    if (ret != NULL)
426
0
        memset(ret, 0, totalSize);
427
0
    return (ret);
428
0
}
429
430
/**
431
 * xmlRegEpxFromParse:
432
 * @ctxt:  the parser context used to build it
433
 *
434
 * Allocate a new regexp and fill it with the result from the parser
435
 *
436
 * Returns the new regexp or NULL in case of error
437
 */
438
static xmlRegexpPtr
439
0
xmlRegEpxFromParse(xmlRegParserCtxtPtr ctxt) {
440
0
    xmlRegexpPtr ret;
441
442
0
    ret = (xmlRegexpPtr) xmlMalloc(sizeof(xmlRegexp));
443
0
    if (ret == NULL) {
444
0
  xmlRegexpErrMemory(ctxt);
445
0
  return(NULL);
446
0
    }
447
0
    memset(ret, 0, sizeof(xmlRegexp));
448
0
    ret->string = ctxt->string;
449
0
    ret->nbStates = ctxt->nbStates;
450
0
    ret->states = ctxt->states;
451
0
    ret->nbAtoms = ctxt->nbAtoms;
452
0
    ret->atoms = ctxt->atoms;
453
0
    ret->nbCounters = ctxt->nbCounters;
454
0
    ret->counters = ctxt->counters;
455
0
    ret->determinist = ctxt->determinist;
456
0
    ret->flags = ctxt->flags;
457
0
    if (ret->determinist == -1) {
458
0
        if (xmlRegexpIsDeterminist(ret) < 0) {
459
0
            xmlRegexpErrMemory(ctxt);
460
0
            xmlFree(ret);
461
0
            return(NULL);
462
0
        }
463
0
    }
464
465
0
    if ((ret->determinist != 0) &&
466
0
  (ret->nbCounters == 0) &&
467
0
  (ctxt->negs == 0) &&
468
0
  (ret->atoms != NULL) &&
469
0
  (ret->atoms[0] != NULL) &&
470
0
  (ret->atoms[0]->type == XML_REGEXP_STRING)) {
471
0
  int i, j, nbstates = 0, nbatoms = 0;
472
0
  int *stateRemap;
473
0
  int *stringRemap;
474
0
  int *transitions;
475
0
  void **transdata;
476
0
  xmlChar **stringMap;
477
0
        xmlChar *value;
478
479
  /*
480
   * Switch to a compact representation
481
   * 1/ counting the effective number of states left
482
   * 2/ counting the unique number of atoms, and check that
483
   *    they are all of the string type
484
   * 3/ build a table state x atom for the transitions
485
   */
486
487
0
  stateRemap = xmlMalloc(ret->nbStates * sizeof(int));
488
0
  if (stateRemap == NULL) {
489
0
      xmlRegexpErrMemory(ctxt);
490
0
      xmlFree(ret);
491
0
      return(NULL);
492
0
  }
493
0
  for (i = 0;i < ret->nbStates;i++) {
494
0
      if (ret->states[i] != NULL) {
495
0
    stateRemap[i] = nbstates;
496
0
    nbstates++;
497
0
      } else {
498
0
    stateRemap[i] = -1;
499
0
      }
500
0
  }
501
0
  stringMap = xmlMalloc(ret->nbAtoms * sizeof(char *));
502
0
  if (stringMap == NULL) {
503
0
      xmlRegexpErrMemory(ctxt);
504
0
      xmlFree(stateRemap);
505
0
      xmlFree(ret);
506
0
      return(NULL);
507
0
  }
508
0
  stringRemap = xmlMalloc(ret->nbAtoms * sizeof(int));
509
0
  if (stringRemap == NULL) {
510
0
      xmlRegexpErrMemory(ctxt);
511
0
      xmlFree(stringMap);
512
0
      xmlFree(stateRemap);
513
0
      xmlFree(ret);
514
0
      return(NULL);
515
0
  }
516
0
  for (i = 0;i < ret->nbAtoms;i++) {
517
0
      if ((ret->atoms[i]->type == XML_REGEXP_STRING) &&
518
0
    (ret->atoms[i]->quant == XML_REGEXP_QUANT_ONCE)) {
519
0
    value = ret->atoms[i]->valuep;
520
0
                for (j = 0;j < nbatoms;j++) {
521
0
        if (xmlStrEqual(stringMap[j], value)) {
522
0
      stringRemap[i] = j;
523
0
      break;
524
0
        }
525
0
    }
526
0
    if (j >= nbatoms) {
527
0
        stringRemap[i] = nbatoms;
528
0
        stringMap[nbatoms] = xmlStrdup(value);
529
0
        if (stringMap[nbatoms] == NULL) {
530
0
      for (i = 0;i < nbatoms;i++)
531
0
          xmlFree(stringMap[i]);
532
0
      xmlFree(stringRemap);
533
0
      xmlFree(stringMap);
534
0
      xmlFree(stateRemap);
535
0
      xmlFree(ret);
536
0
      return(NULL);
537
0
        }
538
0
        nbatoms++;
539
0
    }
540
0
      } else {
541
0
    xmlFree(stateRemap);
542
0
    xmlFree(stringRemap);
543
0
    for (i = 0;i < nbatoms;i++)
544
0
        xmlFree(stringMap[i]);
545
0
    xmlFree(stringMap);
546
0
    xmlFree(ret);
547
0
    return(NULL);
548
0
      }
549
0
  }
550
0
  transitions = (int *) xmlRegCalloc2(nbstates + 1, nbatoms + 1,
551
0
                                            sizeof(int));
552
0
  if (transitions == NULL) {
553
0
      xmlFree(stateRemap);
554
0
      xmlFree(stringRemap);
555
0
            for (i = 0;i < nbatoms;i++)
556
0
    xmlFree(stringMap[i]);
557
0
      xmlFree(stringMap);
558
0
      xmlFree(ret);
559
0
      return(NULL);
560
0
  }
561
562
  /*
563
   * Allocate the transition table. The first entry for each
564
   * state corresponds to the state type.
565
   */
566
0
  transdata = NULL;
567
568
0
  for (i = 0;i < ret->nbStates;i++) {
569
0
      int stateno, atomno, targetno, prev;
570
0
      xmlRegStatePtr state;
571
0
      xmlRegTransPtr trans;
572
573
0
      stateno = stateRemap[i];
574
0
      if (stateno == -1)
575
0
    continue;
576
0
      state = ret->states[i];
577
578
0
      transitions[stateno * (nbatoms + 1)] = state->type;
579
580
0
      for (j = 0;j < state->nbTrans;j++) {
581
0
    trans = &(state->trans[j]);
582
0
    if ((trans->to < 0) || (trans->atom == NULL))
583
0
        continue;
584
0
                atomno = stringRemap[trans->atom->no];
585
0
    if ((trans->atom->data != NULL) && (transdata == NULL)) {
586
0
        transdata = (void **) xmlRegCalloc2(nbstates, nbatoms,
587
0
                                      sizeof(void *));
588
0
        if (transdata == NULL) {
589
0
      xmlRegexpErrMemory(ctxt);
590
0
      break;
591
0
        }
592
0
    }
593
0
    targetno = stateRemap[trans->to];
594
    /*
595
     * if the same atom can generate transitions to 2 different
596
     * states then it means the automata is not deterministic and
597
     * the compact form can't be used !
598
     */
599
0
    prev = transitions[stateno * (nbatoms + 1) + atomno + 1];
600
0
    if (prev != 0) {
601
0
        if (prev != targetno + 1) {
602
0
      ret->determinist = 0;
603
0
      if (transdata != NULL)
604
0
          xmlFree(transdata);
605
0
      xmlFree(transitions);
606
0
      xmlFree(stateRemap);
607
0
      xmlFree(stringRemap);
608
0
      for (i = 0;i < nbatoms;i++)
609
0
          xmlFree(stringMap[i]);
610
0
      xmlFree(stringMap);
611
0
      goto not_determ;
612
0
        }
613
0
    } else {
614
#if 0
615
        printf("State %d trans %d: atom %d to %d : %d to %d\n",
616
         i, j, trans->atom->no, trans->to, atomno, targetno);
617
#endif
618
0
        transitions[stateno * (nbatoms + 1) + atomno + 1] =
619
0
      targetno + 1; /* to avoid 0 */
620
0
        if (transdata != NULL)
621
0
      transdata[stateno * nbatoms + atomno] =
622
0
          trans->atom->data;
623
0
    }
624
0
      }
625
0
  }
626
0
  ret->determinist = 1;
627
  /*
628
   * Cleanup of the old data
629
   */
630
0
  if (ret->states != NULL) {
631
0
      for (i = 0;i < ret->nbStates;i++)
632
0
    xmlRegFreeState(ret->states[i]);
633
0
      xmlFree(ret->states);
634
0
  }
635
0
  ret->states = NULL;
636
0
  ret->nbStates = 0;
637
0
  if (ret->atoms != NULL) {
638
0
      for (i = 0;i < ret->nbAtoms;i++)
639
0
    xmlRegFreeAtom(ret->atoms[i]);
640
0
      xmlFree(ret->atoms);
641
0
  }
642
0
  ret->atoms = NULL;
643
0
  ret->nbAtoms = 0;
644
645
0
  ret->compact = transitions;
646
0
  ret->transdata = transdata;
647
0
  ret->stringMap = stringMap;
648
0
  ret->nbstrings = nbatoms;
649
0
  ret->nbstates = nbstates;
650
0
  xmlFree(stateRemap);
651
0
  xmlFree(stringRemap);
652
0
    }
653
0
not_determ:
654
0
    ctxt->string = NULL;
655
0
    ctxt->nbStates = 0;
656
0
    ctxt->states = NULL;
657
0
    ctxt->nbAtoms = 0;
658
0
    ctxt->atoms = NULL;
659
0
    ctxt->nbCounters = 0;
660
0
    ctxt->counters = NULL;
661
0
    return(ret);
662
0
}
663
664
/**
665
 * xmlRegNewParserCtxt:
666
 * @string:  the string to parse
667
 *
668
 * Allocate a new regexp parser context
669
 *
670
 * Returns the new context or NULL in case of error
671
 */
672
static xmlRegParserCtxtPtr
673
0
xmlRegNewParserCtxt(const xmlChar *string) {
674
0
    xmlRegParserCtxtPtr ret;
675
676
0
    ret = (xmlRegParserCtxtPtr) xmlMalloc(sizeof(xmlRegParserCtxt));
677
0
    if (ret == NULL)
678
0
  return(NULL);
679
0
    memset(ret, 0, sizeof(xmlRegParserCtxt));
680
0
    if (string != NULL) {
681
0
  ret->string = xmlStrdup(string);
682
0
        if (ret->string == NULL) {
683
0
            xmlFree(ret);
684
0
            return(NULL);
685
0
        }
686
0
    }
687
0
    ret->cur = ret->string;
688
0
    ret->neg = 0;
689
0
    ret->negs = 0;
690
0
    ret->error = 0;
691
0
    ret->determinist = -1;
692
0
    return(ret);
693
0
}
694
695
/**
696
 * xmlRegNewRange:
697
 * @ctxt:  the regexp parser context
698
 * @neg:  is that negative
699
 * @type:  the type of range
700
 * @start:  the start codepoint
701
 * @end:  the end codepoint
702
 *
703
 * Allocate a new regexp range
704
 *
705
 * Returns the new range or NULL in case of error
706
 */
707
static xmlRegRangePtr
708
xmlRegNewRange(xmlRegParserCtxtPtr ctxt,
709
0
         int neg, xmlRegAtomType type, int start, int end) {
710
0
    xmlRegRangePtr ret;
711
712
0
    ret = (xmlRegRangePtr) xmlMalloc(sizeof(xmlRegRange));
713
0
    if (ret == NULL) {
714
0
  xmlRegexpErrMemory(ctxt);
715
0
  return(NULL);
716
0
    }
717
0
    ret->neg = neg;
718
0
    ret->type = type;
719
0
    ret->start = start;
720
0
    ret->end = end;
721
0
    return(ret);
722
0
}
723
724
/**
725
 * xmlRegFreeRange:
726
 * @range:  the regexp range
727
 *
728
 * Free a regexp range
729
 */
730
static void
731
0
xmlRegFreeRange(xmlRegRangePtr range) {
732
0
    if (range == NULL)
733
0
  return;
734
735
0
    if (range->blockName != NULL)
736
0
  xmlFree(range->blockName);
737
0
    xmlFree(range);
738
0
}
739
740
/**
741
 * xmlRegCopyRange:
742
 * @range:  the regexp range
743
 *
744
 * Copy a regexp range
745
 *
746
 * Returns the new copy or NULL in case of error.
747
 */
748
static xmlRegRangePtr
749
0
xmlRegCopyRange(xmlRegParserCtxtPtr ctxt, xmlRegRangePtr range) {
750
0
    xmlRegRangePtr ret;
751
752
0
    if (range == NULL)
753
0
  return(NULL);
754
755
0
    ret = xmlRegNewRange(ctxt, range->neg, range->type, range->start,
756
0
                         range->end);
757
0
    if (ret == NULL)
758
0
        return(NULL);
759
0
    if (range->blockName != NULL) {
760
0
  ret->blockName = xmlStrdup(range->blockName);
761
0
  if (ret->blockName == NULL) {
762
0
      xmlRegexpErrMemory(ctxt);
763
0
      xmlRegFreeRange(ret);
764
0
      return(NULL);
765
0
  }
766
0
    }
767
0
    return(ret);
768
0
}
769
770
/**
771
 * xmlRegNewAtom:
772
 * @ctxt:  the regexp parser context
773
 * @type:  the type of atom
774
 *
775
 * Allocate a new atom
776
 *
777
 * Returns the new atom or NULL in case of error
778
 */
779
static xmlRegAtomPtr
780
0
xmlRegNewAtom(xmlRegParserCtxtPtr ctxt, xmlRegAtomType type) {
781
0
    xmlRegAtomPtr ret;
782
783
0
    ret = (xmlRegAtomPtr) xmlMalloc(sizeof(xmlRegAtom));
784
0
    if (ret == NULL) {
785
0
  xmlRegexpErrMemory(ctxt);
786
0
  return(NULL);
787
0
    }
788
0
    memset(ret, 0, sizeof(xmlRegAtom));
789
0
    ret->type = type;
790
0
    ret->quant = XML_REGEXP_QUANT_ONCE;
791
0
    ret->min = 0;
792
0
    ret->max = 0;
793
0
    return(ret);
794
0
}
795
796
/**
797
 * xmlRegFreeAtom:
798
 * @atom:  the regexp atom
799
 *
800
 * Free a regexp atom
801
 */
802
static void
803
0
xmlRegFreeAtom(xmlRegAtomPtr atom) {
804
0
    int i;
805
806
0
    if (atom == NULL)
807
0
  return;
808
809
0
    for (i = 0;i < atom->nbRanges;i++)
810
0
  xmlRegFreeRange(atom->ranges[i]);
811
0
    if (atom->ranges != NULL)
812
0
  xmlFree(atom->ranges);
813
0
    if ((atom->type == XML_REGEXP_STRING) && (atom->valuep != NULL))
814
0
  xmlFree(atom->valuep);
815
0
    if ((atom->type == XML_REGEXP_STRING) && (atom->valuep2 != NULL))
816
0
  xmlFree(atom->valuep2);
817
0
    if ((atom->type == XML_REGEXP_BLOCK_NAME) && (atom->valuep != NULL))
818
0
  xmlFree(atom->valuep);
819
0
    xmlFree(atom);
820
0
}
821
822
/**
823
 * xmlRegCopyAtom:
824
 * @ctxt:  the regexp parser context
825
 * @atom:  the original atom
826
 *
827
 * Allocate a new regexp range
828
 *
829
 * Returns the new atom or NULL in case of error
830
 */
831
static xmlRegAtomPtr
832
0
xmlRegCopyAtom(xmlRegParserCtxtPtr ctxt, xmlRegAtomPtr atom) {
833
0
    xmlRegAtomPtr ret;
834
835
0
    ret = (xmlRegAtomPtr) xmlMalloc(sizeof(xmlRegAtom));
836
0
    if (ret == NULL) {
837
0
  xmlRegexpErrMemory(ctxt);
838
0
  return(NULL);
839
0
    }
840
0
    memset(ret, 0, sizeof(xmlRegAtom));
841
0
    ret->type = atom->type;
842
0
    ret->quant = atom->quant;
843
0
    ret->min = atom->min;
844
0
    ret->max = atom->max;
845
0
    if (atom->nbRanges > 0) {
846
0
        int i;
847
848
0
        ret->ranges = (xmlRegRangePtr *) xmlMalloc(sizeof(xmlRegRangePtr) *
849
0
                                             atom->nbRanges);
850
0
  if (ret->ranges == NULL) {
851
0
      xmlRegexpErrMemory(ctxt);
852
0
      goto error;
853
0
  }
854
0
  for (i = 0;i < atom->nbRanges;i++) {
855
0
      ret->ranges[i] = xmlRegCopyRange(ctxt, atom->ranges[i]);
856
0
      if (ret->ranges[i] == NULL)
857
0
          goto error;
858
0
      ret->nbRanges = i + 1;
859
0
  }
860
0
    }
861
0
    return(ret);
862
863
0
error:
864
0
    xmlRegFreeAtom(ret);
865
0
    return(NULL);
866
0
}
867
868
static xmlRegStatePtr
869
0
xmlRegNewState(xmlRegParserCtxtPtr ctxt) {
870
0
    xmlRegStatePtr ret;
871
872
0
    ret = (xmlRegStatePtr) xmlMalloc(sizeof(xmlRegState));
873
0
    if (ret == NULL) {
874
0
  xmlRegexpErrMemory(ctxt);
875
0
  return(NULL);
876
0
    }
877
0
    memset(ret, 0, sizeof(xmlRegState));
878
0
    ret->type = XML_REGEXP_TRANS_STATE;
879
0
    ret->mark = XML_REGEXP_MARK_NORMAL;
880
0
    return(ret);
881
0
}
882
883
/**
884
 * xmlRegFreeState:
885
 * @state:  the regexp state
886
 *
887
 * Free a regexp state
888
 */
889
static void
890
0
xmlRegFreeState(xmlRegStatePtr state) {
891
0
    if (state == NULL)
892
0
  return;
893
894
0
    if (state->trans != NULL)
895
0
  xmlFree(state->trans);
896
0
    if (state->transTo != NULL)
897
0
  xmlFree(state->transTo);
898
0
    xmlFree(state);
899
0
}
900
901
/**
902
 * xmlRegFreeParserCtxt:
903
 * @ctxt:  the regexp parser context
904
 *
905
 * Free a regexp parser context
906
 */
907
static void
908
0
xmlRegFreeParserCtxt(xmlRegParserCtxtPtr ctxt) {
909
0
    int i;
910
0
    if (ctxt == NULL)
911
0
  return;
912
913
0
    if (ctxt->string != NULL)
914
0
  xmlFree(ctxt->string);
915
0
    if (ctxt->states != NULL) {
916
0
  for (i = 0;i < ctxt->nbStates;i++)
917
0
      xmlRegFreeState(ctxt->states[i]);
918
0
  xmlFree(ctxt->states);
919
0
    }
920
0
    if (ctxt->atoms != NULL) {
921
0
  for (i = 0;i < ctxt->nbAtoms;i++)
922
0
      xmlRegFreeAtom(ctxt->atoms[i]);
923
0
  xmlFree(ctxt->atoms);
924
0
    }
925
0
    if (ctxt->counters != NULL)
926
0
  xmlFree(ctxt->counters);
927
0
    xmlFree(ctxt);
928
0
}
929
930
/************************************************************************
931
 *                  *
932
 *      Display of Data structures      *
933
 *                  *
934
 ************************************************************************/
935
936
static void
937
0
xmlRegPrintAtomType(FILE *output, xmlRegAtomType type) {
938
0
    switch (type) {
939
0
        case XML_REGEXP_EPSILON:
940
0
      fprintf(output, "epsilon "); break;
941
0
        case XML_REGEXP_CHARVAL:
942
0
      fprintf(output, "charval "); break;
943
0
        case XML_REGEXP_RANGES:
944
0
      fprintf(output, "ranges "); break;
945
0
        case XML_REGEXP_SUBREG:
946
0
      fprintf(output, "subexpr "); break;
947
0
        case XML_REGEXP_STRING:
948
0
      fprintf(output, "string "); break;
949
0
        case XML_REGEXP_ANYCHAR:
950
0
      fprintf(output, "anychar "); break;
951
0
        case XML_REGEXP_ANYSPACE:
952
0
      fprintf(output, "anyspace "); break;
953
0
        case XML_REGEXP_NOTSPACE:
954
0
      fprintf(output, "notspace "); break;
955
0
        case XML_REGEXP_INITNAME:
956
0
      fprintf(output, "initname "); break;
957
0
        case XML_REGEXP_NOTINITNAME:
958
0
      fprintf(output, "notinitname "); break;
959
0
        case XML_REGEXP_NAMECHAR:
960
0
      fprintf(output, "namechar "); break;
961
0
        case XML_REGEXP_NOTNAMECHAR:
962
0
      fprintf(output, "notnamechar "); break;
963
0
        case XML_REGEXP_DECIMAL:
964
0
      fprintf(output, "decimal "); break;
965
0
        case XML_REGEXP_NOTDECIMAL:
966
0
      fprintf(output, "notdecimal "); break;
967
0
        case XML_REGEXP_REALCHAR:
968
0
      fprintf(output, "realchar "); break;
969
0
        case XML_REGEXP_NOTREALCHAR:
970
0
      fprintf(output, "notrealchar "); break;
971
0
        case XML_REGEXP_LETTER:
972
0
            fprintf(output, "LETTER "); break;
973
0
        case XML_REGEXP_LETTER_UPPERCASE:
974
0
            fprintf(output, "LETTER_UPPERCASE "); break;
975
0
        case XML_REGEXP_LETTER_LOWERCASE:
976
0
            fprintf(output, "LETTER_LOWERCASE "); break;
977
0
        case XML_REGEXP_LETTER_TITLECASE:
978
0
            fprintf(output, "LETTER_TITLECASE "); break;
979
0
        case XML_REGEXP_LETTER_MODIFIER:
980
0
            fprintf(output, "LETTER_MODIFIER "); break;
981
0
        case XML_REGEXP_LETTER_OTHERS:
982
0
            fprintf(output, "LETTER_OTHERS "); break;
983
0
        case XML_REGEXP_MARK:
984
0
            fprintf(output, "MARK "); break;
985
0
        case XML_REGEXP_MARK_NONSPACING:
986
0
            fprintf(output, "MARK_NONSPACING "); break;
987
0
        case XML_REGEXP_MARK_SPACECOMBINING:
988
0
            fprintf(output, "MARK_SPACECOMBINING "); break;
989
0
        case XML_REGEXP_MARK_ENCLOSING:
990
0
            fprintf(output, "MARK_ENCLOSING "); break;
991
0
        case XML_REGEXP_NUMBER:
992
0
            fprintf(output, "NUMBER "); break;
993
0
        case XML_REGEXP_NUMBER_DECIMAL:
994
0
            fprintf(output, "NUMBER_DECIMAL "); break;
995
0
        case XML_REGEXP_NUMBER_LETTER:
996
0
            fprintf(output, "NUMBER_LETTER "); break;
997
0
        case XML_REGEXP_NUMBER_OTHERS:
998
0
            fprintf(output, "NUMBER_OTHERS "); break;
999
0
        case XML_REGEXP_PUNCT:
1000
0
            fprintf(output, "PUNCT "); break;
1001
0
        case XML_REGEXP_PUNCT_CONNECTOR:
1002
0
            fprintf(output, "PUNCT_CONNECTOR "); break;
1003
0
        case XML_REGEXP_PUNCT_DASH:
1004
0
            fprintf(output, "PUNCT_DASH "); break;
1005
0
        case XML_REGEXP_PUNCT_OPEN:
1006
0
            fprintf(output, "PUNCT_OPEN "); break;
1007
0
        case XML_REGEXP_PUNCT_CLOSE:
1008
0
            fprintf(output, "PUNCT_CLOSE "); break;
1009
0
        case XML_REGEXP_PUNCT_INITQUOTE:
1010
0
            fprintf(output, "PUNCT_INITQUOTE "); break;
1011
0
        case XML_REGEXP_PUNCT_FINQUOTE:
1012
0
            fprintf(output, "PUNCT_FINQUOTE "); break;
1013
0
        case XML_REGEXP_PUNCT_OTHERS:
1014
0
            fprintf(output, "PUNCT_OTHERS "); break;
1015
0
        case XML_REGEXP_SEPAR:
1016
0
            fprintf(output, "SEPAR "); break;
1017
0
        case XML_REGEXP_SEPAR_SPACE:
1018
0
            fprintf(output, "SEPAR_SPACE "); break;
1019
0
        case XML_REGEXP_SEPAR_LINE:
1020
0
            fprintf(output, "SEPAR_LINE "); break;
1021
0
        case XML_REGEXP_SEPAR_PARA:
1022
0
            fprintf(output, "SEPAR_PARA "); break;
1023
0
        case XML_REGEXP_SYMBOL:
1024
0
            fprintf(output, "SYMBOL "); break;
1025
0
        case XML_REGEXP_SYMBOL_MATH:
1026
0
            fprintf(output, "SYMBOL_MATH "); break;
1027
0
        case XML_REGEXP_SYMBOL_CURRENCY:
1028
0
            fprintf(output, "SYMBOL_CURRENCY "); break;
1029
0
        case XML_REGEXP_SYMBOL_MODIFIER:
1030
0
            fprintf(output, "SYMBOL_MODIFIER "); break;
1031
0
        case XML_REGEXP_SYMBOL_OTHERS:
1032
0
            fprintf(output, "SYMBOL_OTHERS "); break;
1033
0
        case XML_REGEXP_OTHER:
1034
0
            fprintf(output, "OTHER "); break;
1035
0
        case XML_REGEXP_OTHER_CONTROL:
1036
0
            fprintf(output, "OTHER_CONTROL "); break;
1037
0
        case XML_REGEXP_OTHER_FORMAT:
1038
0
            fprintf(output, "OTHER_FORMAT "); break;
1039
0
        case XML_REGEXP_OTHER_PRIVATE:
1040
0
            fprintf(output, "OTHER_PRIVATE "); break;
1041
0
        case XML_REGEXP_OTHER_NA:
1042
0
            fprintf(output, "OTHER_NA "); break;
1043
0
        case XML_REGEXP_BLOCK_NAME:
1044
0
      fprintf(output, "BLOCK "); break;
1045
0
    }
1046
0
}
1047
1048
static void
1049
0
xmlRegPrintQuantType(FILE *output, xmlRegQuantType type) {
1050
0
    switch (type) {
1051
0
        case XML_REGEXP_QUANT_EPSILON:
1052
0
      fprintf(output, "epsilon "); break;
1053
0
        case XML_REGEXP_QUANT_ONCE:
1054
0
      fprintf(output, "once "); break;
1055
0
        case XML_REGEXP_QUANT_OPT:
1056
0
      fprintf(output, "? "); break;
1057
0
        case XML_REGEXP_QUANT_MULT:
1058
0
      fprintf(output, "* "); break;
1059
0
        case XML_REGEXP_QUANT_PLUS:
1060
0
      fprintf(output, "+ "); break;
1061
0
  case XML_REGEXP_QUANT_RANGE:
1062
0
      fprintf(output, "range "); break;
1063
0
  case XML_REGEXP_QUANT_ONCEONLY:
1064
0
      fprintf(output, "onceonly "); break;
1065
0
  case XML_REGEXP_QUANT_ALL:
1066
0
      fprintf(output, "all "); break;
1067
0
    }
1068
0
}
1069
static void
1070
0
xmlRegPrintRange(FILE *output, xmlRegRangePtr range) {
1071
0
    fprintf(output, "  range: ");
1072
0
    if (range->neg)
1073
0
  fprintf(output, "negative ");
1074
0
    xmlRegPrintAtomType(output, range->type);
1075
0
    fprintf(output, "%c - %c\n", range->start, range->end);
1076
0
}
1077
1078
static void
1079
0
xmlRegPrintAtom(FILE *output, xmlRegAtomPtr atom) {
1080
0
    fprintf(output, " atom: ");
1081
0
    if (atom == NULL) {
1082
0
  fprintf(output, "NULL\n");
1083
0
  return;
1084
0
    }
1085
0
    if (atom->neg)
1086
0
        fprintf(output, "not ");
1087
0
    xmlRegPrintAtomType(output, atom->type);
1088
0
    xmlRegPrintQuantType(output, atom->quant);
1089
0
    if (atom->quant == XML_REGEXP_QUANT_RANGE)
1090
0
  fprintf(output, "%d-%d ", atom->min, atom->max);
1091
0
    if (atom->type == XML_REGEXP_STRING)
1092
0
  fprintf(output, "'%s' ", (char *) atom->valuep);
1093
0
    if (atom->type == XML_REGEXP_CHARVAL)
1094
0
  fprintf(output, "char %c\n", atom->codepoint);
1095
0
    else if (atom->type == XML_REGEXP_RANGES) {
1096
0
  int i;
1097
0
  fprintf(output, "%d entries\n", atom->nbRanges);
1098
0
  for (i = 0; i < atom->nbRanges;i++)
1099
0
      xmlRegPrintRange(output, atom->ranges[i]);
1100
0
    } else if (atom->type == XML_REGEXP_SUBREG) {
1101
0
  fprintf(output, "start %d end %d\n", atom->start->no, atom->stop->no);
1102
0
    } else {
1103
0
  fprintf(output, "\n");
1104
0
    }
1105
0
}
1106
1107
static void
1108
0
xmlRegPrintTrans(FILE *output, xmlRegTransPtr trans) {
1109
0
    fprintf(output, "  trans: ");
1110
0
    if (trans == NULL) {
1111
0
  fprintf(output, "NULL\n");
1112
0
  return;
1113
0
    }
1114
0
    if (trans->to < 0) {
1115
0
  fprintf(output, "removed\n");
1116
0
  return;
1117
0
    }
1118
0
    if (trans->nd != 0) {
1119
0
  if (trans->nd == 2)
1120
0
      fprintf(output, "last not determinist, ");
1121
0
  else
1122
0
      fprintf(output, "not determinist, ");
1123
0
    }
1124
0
    if (trans->counter >= 0) {
1125
0
  fprintf(output, "counted %d, ", trans->counter);
1126
0
    }
1127
0
    if (trans->count == REGEXP_ALL_COUNTER) {
1128
0
  fprintf(output, "all transition, ");
1129
0
    } else if (trans->count >= 0) {
1130
0
  fprintf(output, "count based %d, ", trans->count);
1131
0
    }
1132
0
    if (trans->atom == NULL) {
1133
0
  fprintf(output, "epsilon to %d\n", trans->to);
1134
0
  return;
1135
0
    }
1136
0
    if (trans->atom->type == XML_REGEXP_CHARVAL)
1137
0
  fprintf(output, "char %c ", trans->atom->codepoint);
1138
0
    fprintf(output, "atom %d, to %d\n", trans->atom->no, trans->to);
1139
0
}
1140
1141
static void
1142
0
xmlRegPrintState(FILE *output, xmlRegStatePtr state) {
1143
0
    int i;
1144
1145
0
    fprintf(output, " state: ");
1146
0
    if (state == NULL) {
1147
0
  fprintf(output, "NULL\n");
1148
0
  return;
1149
0
    }
1150
0
    if (state->type == XML_REGEXP_START_STATE)
1151
0
  fprintf(output, "START ");
1152
0
    if (state->type == XML_REGEXP_FINAL_STATE)
1153
0
  fprintf(output, "FINAL ");
1154
1155
0
    fprintf(output, "%d, %d transitions:\n", state->no, state->nbTrans);
1156
0
    for (i = 0;i < state->nbTrans; i++) {
1157
0
  xmlRegPrintTrans(output, &(state->trans[i]));
1158
0
    }
1159
0
}
1160
1161
/************************************************************************
1162
 *                  *
1163
 *     Finite Automata structures manipulations   *
1164
 *                  *
1165
 ************************************************************************/
1166
1167
static xmlRegRangePtr
1168
xmlRegAtomAddRange(xmlRegParserCtxtPtr ctxt, xmlRegAtomPtr atom,
1169
             int neg, xmlRegAtomType type, int start, int end,
1170
0
       xmlChar *blockName) {
1171
0
    xmlRegRangePtr range;
1172
1173
0
    if (atom == NULL) {
1174
0
  ERROR("add range: atom is NULL");
1175
0
  return(NULL);
1176
0
    }
1177
0
    if (atom->type != XML_REGEXP_RANGES) {
1178
0
  ERROR("add range: atom is not ranges");
1179
0
  return(NULL);
1180
0
    }
1181
0
    if (atom->maxRanges == 0) {
1182
0
  atom->maxRanges = 4;
1183
0
  atom->ranges = (xmlRegRangePtr *) xmlMalloc(atom->maxRanges *
1184
0
                                 sizeof(xmlRegRangePtr));
1185
0
  if (atom->ranges == NULL) {
1186
0
      xmlRegexpErrMemory(ctxt);
1187
0
      atom->maxRanges = 0;
1188
0
      return(NULL);
1189
0
  }
1190
0
    } else if (atom->nbRanges >= atom->maxRanges) {
1191
0
  xmlRegRangePtr *tmp;
1192
0
  atom->maxRanges *= 2;
1193
0
  tmp = (xmlRegRangePtr *) xmlRealloc(atom->ranges, atom->maxRanges *
1194
0
                                 sizeof(xmlRegRangePtr));
1195
0
  if (tmp == NULL) {
1196
0
      xmlRegexpErrMemory(ctxt);
1197
0
      atom->maxRanges /= 2;
1198
0
      return(NULL);
1199
0
  }
1200
0
  atom->ranges = tmp;
1201
0
    }
1202
0
    range = xmlRegNewRange(ctxt, neg, type, start, end);
1203
0
    if (range == NULL)
1204
0
  return(NULL);
1205
0
    range->blockName = blockName;
1206
0
    atom->ranges[atom->nbRanges++] = range;
1207
1208
0
    return(range);
1209
0
}
1210
1211
static int
1212
0
xmlRegGetCounter(xmlRegParserCtxtPtr ctxt) {
1213
0
    if (ctxt->maxCounters == 0) {
1214
0
  ctxt->maxCounters = 4;
1215
0
  ctxt->counters = (xmlRegCounter *) xmlMalloc(ctxt->maxCounters *
1216
0
                                 sizeof(xmlRegCounter));
1217
0
  if (ctxt->counters == NULL) {
1218
0
      xmlRegexpErrMemory(ctxt);
1219
0
      ctxt->maxCounters = 0;
1220
0
      return(-1);
1221
0
  }
1222
0
    } else if (ctxt->nbCounters >= ctxt->maxCounters) {
1223
0
  xmlRegCounter *tmp;
1224
0
  ctxt->maxCounters *= 2;
1225
0
  tmp = (xmlRegCounter *) xmlRealloc(ctxt->counters, ctxt->maxCounters *
1226
0
                               sizeof(xmlRegCounter));
1227
0
  if (tmp == NULL) {
1228
0
      xmlRegexpErrMemory(ctxt);
1229
0
      ctxt->maxCounters /= 2;
1230
0
      return(-1);
1231
0
  }
1232
0
  ctxt->counters = tmp;
1233
0
    }
1234
0
    ctxt->counters[ctxt->nbCounters].min = -1;
1235
0
    ctxt->counters[ctxt->nbCounters].max = -1;
1236
0
    return(ctxt->nbCounters++);
1237
0
}
1238
1239
static int
1240
0
xmlRegAtomPush(xmlRegParserCtxtPtr ctxt, xmlRegAtomPtr atom) {
1241
0
    if (atom == NULL) {
1242
0
  ERROR("atom push: atom is NULL");
1243
0
  return(-1);
1244
0
    }
1245
0
    if (ctxt->nbAtoms >= ctxt->maxAtoms) {
1246
0
        size_t newSize = ctxt->maxAtoms ? ctxt->maxAtoms * 2 : 4;
1247
0
  xmlRegAtomPtr *tmp;
1248
1249
0
  tmp = xmlRealloc(ctxt->atoms, newSize * sizeof(xmlRegAtomPtr));
1250
0
  if (tmp == NULL) {
1251
0
      xmlRegexpErrMemory(ctxt);
1252
0
      return(-1);
1253
0
  }
1254
0
  ctxt->atoms = tmp;
1255
0
        ctxt->maxAtoms = newSize;
1256
0
    }
1257
0
    atom->no = ctxt->nbAtoms;
1258
0
    ctxt->atoms[ctxt->nbAtoms++] = atom;
1259
0
    return(0);
1260
0
}
1261
1262
static void
1263
xmlRegStateAddTransTo(xmlRegParserCtxtPtr ctxt, xmlRegStatePtr target,
1264
0
                      int from) {
1265
0
    if (target->maxTransTo == 0) {
1266
0
  target->maxTransTo = 8;
1267
0
  target->transTo = (int *) xmlMalloc(target->maxTransTo *
1268
0
                                 sizeof(int));
1269
0
  if (target->transTo == NULL) {
1270
0
      xmlRegexpErrMemory(ctxt);
1271
0
      target->maxTransTo = 0;
1272
0
      return;
1273
0
  }
1274
0
    } else if (target->nbTransTo >= target->maxTransTo) {
1275
0
  int *tmp;
1276
0
  target->maxTransTo *= 2;
1277
0
  tmp = (int *) xmlRealloc(target->transTo, target->maxTransTo *
1278
0
                                 sizeof(int));
1279
0
  if (tmp == NULL) {
1280
0
      xmlRegexpErrMemory(ctxt);
1281
0
      target->maxTransTo /= 2;
1282
0
      return;
1283
0
  }
1284
0
  target->transTo = tmp;
1285
0
    }
1286
0
    target->transTo[target->nbTransTo] = from;
1287
0
    target->nbTransTo++;
1288
0
}
1289
1290
static void
1291
xmlRegStateAddTrans(xmlRegParserCtxtPtr ctxt, xmlRegStatePtr state,
1292
              xmlRegAtomPtr atom, xmlRegStatePtr target,
1293
0
        int counter, int count) {
1294
1295
0
    int nrtrans;
1296
1297
0
    if (state == NULL) {
1298
0
  ERROR("add state: state is NULL");
1299
0
  return;
1300
0
    }
1301
0
    if (target == NULL) {
1302
0
  ERROR("add state: target is NULL");
1303
0
  return;
1304
0
    }
1305
    /*
1306
     * Other routines follow the philosophy 'When in doubt, add a transition'
1307
     * so we check here whether such a transition is already present and, if
1308
     * so, silently ignore this request.
1309
     */
1310
1311
0
    for (nrtrans = state->nbTrans - 1; nrtrans >= 0; nrtrans--) {
1312
0
  xmlRegTransPtr trans = &(state->trans[nrtrans]);
1313
0
  if ((trans->atom == atom) &&
1314
0
      (trans->to == target->no) &&
1315
0
      (trans->counter == counter) &&
1316
0
      (trans->count == count)) {
1317
0
      return;
1318
0
  }
1319
0
    }
1320
1321
0
    if (state->maxTrans == 0) {
1322
0
  state->maxTrans = 8;
1323
0
  state->trans = (xmlRegTrans *) xmlMalloc(state->maxTrans *
1324
0
                                 sizeof(xmlRegTrans));
1325
0
  if (state->trans == NULL) {
1326
0
      xmlRegexpErrMemory(ctxt);
1327
0
      state->maxTrans = 0;
1328
0
      return;
1329
0
  }
1330
0
    } else if (state->nbTrans >= state->maxTrans) {
1331
0
  xmlRegTrans *tmp;
1332
0
  state->maxTrans *= 2;
1333
0
  tmp = (xmlRegTrans *) xmlRealloc(state->trans, state->maxTrans *
1334
0
                                 sizeof(xmlRegTrans));
1335
0
  if (tmp == NULL) {
1336
0
      xmlRegexpErrMemory(ctxt);
1337
0
      state->maxTrans /= 2;
1338
0
      return;
1339
0
  }
1340
0
  state->trans = tmp;
1341
0
    }
1342
1343
0
    state->trans[state->nbTrans].atom = atom;
1344
0
    state->trans[state->nbTrans].to = target->no;
1345
0
    state->trans[state->nbTrans].counter = counter;
1346
0
    state->trans[state->nbTrans].count = count;
1347
0
    state->trans[state->nbTrans].nd = 0;
1348
0
    state->nbTrans++;
1349
0
    xmlRegStateAddTransTo(ctxt, target, state->no);
1350
0
}
1351
1352
static xmlRegStatePtr
1353
0
xmlRegStatePush(xmlRegParserCtxtPtr ctxt) {
1354
0
    xmlRegStatePtr state;
1355
1356
0
    if (ctxt->nbStates >= ctxt->maxStates) {
1357
0
        size_t newSize = ctxt->maxStates ? ctxt->maxStates * 2 : 4;
1358
0
  xmlRegStatePtr *tmp;
1359
1360
0
  tmp = xmlRealloc(ctxt->states, newSize * sizeof(tmp[0]));
1361
0
  if (tmp == NULL) {
1362
0
      xmlRegexpErrMemory(ctxt);
1363
0
      return(NULL);
1364
0
  }
1365
0
  ctxt->states = tmp;
1366
0
  ctxt->maxStates = newSize;
1367
0
    }
1368
1369
0
    state = xmlRegNewState(ctxt);
1370
0
    if (state == NULL)
1371
0
        return(NULL);
1372
1373
0
    state->no = ctxt->nbStates;
1374
0
    ctxt->states[ctxt->nbStates++] = state;
1375
1376
0
    return(state);
1377
0
}
1378
1379
/**
1380
 * xmlFAGenerateAllTransition:
1381
 * @ctxt:  a regexp parser context
1382
 * @from:  the from state
1383
 * @to:  the target state or NULL for building a new one
1384
 * @lax:
1385
 *
1386
 */
1387
static int
1388
xmlFAGenerateAllTransition(xmlRegParserCtxtPtr ctxt,
1389
         xmlRegStatePtr from, xmlRegStatePtr to,
1390
0
         int lax) {
1391
0
    if (to == NULL) {
1392
0
  to = xmlRegStatePush(ctxt);
1393
0
        if (to == NULL)
1394
0
            return(-1);
1395
0
  ctxt->state = to;
1396
0
    }
1397
0
    if (lax)
1398
0
  xmlRegStateAddTrans(ctxt, from, NULL, to, -1, REGEXP_ALL_LAX_COUNTER);
1399
0
    else
1400
0
  xmlRegStateAddTrans(ctxt, from, NULL, to, -1, REGEXP_ALL_COUNTER);
1401
0
    return(0);
1402
0
}
1403
1404
/**
1405
 * xmlFAGenerateEpsilonTransition:
1406
 * @ctxt:  a regexp parser context
1407
 * @from:  the from state
1408
 * @to:  the target state or NULL for building a new one
1409
 *
1410
 */
1411
static int
1412
xmlFAGenerateEpsilonTransition(xmlRegParserCtxtPtr ctxt,
1413
0
             xmlRegStatePtr from, xmlRegStatePtr to) {
1414
0
    if (to == NULL) {
1415
0
  to = xmlRegStatePush(ctxt);
1416
0
        if (to == NULL)
1417
0
            return(-1);
1418
0
  ctxt->state = to;
1419
0
    }
1420
0
    xmlRegStateAddTrans(ctxt, from, NULL, to, -1, -1);
1421
0
    return(0);
1422
0
}
1423
1424
/**
1425
 * xmlFAGenerateCountedEpsilonTransition:
1426
 * @ctxt:  a regexp parser context
1427
 * @from:  the from state
1428
 * @to:  the target state or NULL for building a new one
1429
 * counter:  the counter for that transition
1430
 *
1431
 */
1432
static int
1433
xmlFAGenerateCountedEpsilonTransition(xmlRegParserCtxtPtr ctxt,
1434
0
      xmlRegStatePtr from, xmlRegStatePtr to, int counter) {
1435
0
    if (to == NULL) {
1436
0
  to = xmlRegStatePush(ctxt);
1437
0
        if (to == NULL)
1438
0
            return(-1);
1439
0
  ctxt->state = to;
1440
0
    }
1441
0
    xmlRegStateAddTrans(ctxt, from, NULL, to, counter, -1);
1442
0
    return(0);
1443
0
}
1444
1445
/**
1446
 * xmlFAGenerateCountedTransition:
1447
 * @ctxt:  a regexp parser context
1448
 * @from:  the from state
1449
 * @to:  the target state or NULL for building a new one
1450
 * counter:  the counter for that transition
1451
 *
1452
 */
1453
static int
1454
xmlFAGenerateCountedTransition(xmlRegParserCtxtPtr ctxt,
1455
0
      xmlRegStatePtr from, xmlRegStatePtr to, int counter) {
1456
0
    if (to == NULL) {
1457
0
  to = xmlRegStatePush(ctxt);
1458
0
        if (to == NULL)
1459
0
            return(-1);
1460
0
  ctxt->state = to;
1461
0
    }
1462
0
    xmlRegStateAddTrans(ctxt, from, NULL, to, -1, counter);
1463
0
    return(0);
1464
0
}
1465
1466
/**
1467
 * xmlFAGenerateTransitions:
1468
 * @ctxt:  a regexp parser context
1469
 * @from:  the from state
1470
 * @to:  the target state or NULL for building a new one
1471
 * @atom:  the atom generating the transition
1472
 *
1473
 * Returns 0 if success and -1 in case of error.
1474
 */
1475
static int
1476
xmlFAGenerateTransitions(xmlRegParserCtxtPtr ctxt, xmlRegStatePtr from,
1477
0
                   xmlRegStatePtr to, xmlRegAtomPtr atom) {
1478
0
    xmlRegStatePtr end;
1479
0
    int nullable = 0;
1480
1481
0
    if (atom == NULL) {
1482
0
  ERROR("generate transition: atom == NULL");
1483
0
  return(-1);
1484
0
    }
1485
0
    if (atom->type == XML_REGEXP_SUBREG) {
1486
  /*
1487
   * this is a subexpression handling one should not need to
1488
   * create a new node except for XML_REGEXP_QUANT_RANGE.
1489
   */
1490
0
  if ((to != NULL) && (atom->stop != to) &&
1491
0
      (atom->quant != XML_REGEXP_QUANT_RANGE)) {
1492
      /*
1493
       * Generate an epsilon transition to link to the target
1494
       */
1495
0
      xmlFAGenerateEpsilonTransition(ctxt, atom->stop, to);
1496
#ifdef DV
1497
  } else if ((to == NULL) && (atom->quant != XML_REGEXP_QUANT_RANGE) &&
1498
       (atom->quant != XML_REGEXP_QUANT_ONCE)) {
1499
      to = xmlRegStatePush(ctxt, to);
1500
            if (to == NULL)
1501
                return(-1);
1502
      ctxt->state = to;
1503
      xmlFAGenerateEpsilonTransition(ctxt, atom->stop, to);
1504
#endif
1505
0
  }
1506
0
  switch (atom->quant) {
1507
0
      case XML_REGEXP_QUANT_OPT:
1508
0
    atom->quant = XML_REGEXP_QUANT_ONCE;
1509
    /*
1510
     * transition done to the state after end of atom.
1511
     *      1. set transition from atom start to new state
1512
     *      2. set transition from atom end to this state.
1513
     */
1514
0
                if (to == NULL) {
1515
0
                    xmlFAGenerateEpsilonTransition(ctxt, atom->start, 0);
1516
0
                    xmlFAGenerateEpsilonTransition(ctxt, atom->stop,
1517
0
                                                   ctxt->state);
1518
0
                } else {
1519
0
                    xmlFAGenerateEpsilonTransition(ctxt, atom->start, to);
1520
0
                }
1521
0
    break;
1522
0
      case XML_REGEXP_QUANT_MULT:
1523
0
    atom->quant = XML_REGEXP_QUANT_ONCE;
1524
0
    xmlFAGenerateEpsilonTransition(ctxt, atom->start, atom->stop);
1525
0
    xmlFAGenerateEpsilonTransition(ctxt, atom->stop, atom->start);
1526
0
    break;
1527
0
      case XML_REGEXP_QUANT_PLUS:
1528
0
    atom->quant = XML_REGEXP_QUANT_ONCE;
1529
0
    xmlFAGenerateEpsilonTransition(ctxt, atom->stop, atom->start);
1530
0
    break;
1531
0
      case XML_REGEXP_QUANT_RANGE: {
1532
0
    int counter;
1533
0
    xmlRegStatePtr inter, newstate;
1534
1535
    /*
1536
     * create the final state now if needed
1537
     */
1538
0
    if (to != NULL) {
1539
0
        newstate = to;
1540
0
    } else {
1541
0
        newstate = xmlRegStatePush(ctxt);
1542
0
                    if (newstate == NULL)
1543
0
                        return(-1);
1544
0
    }
1545
1546
    /*
1547
     * The principle here is to use counted transition
1548
     * to avoid explosion in the number of states in the
1549
     * graph. This is clearly more complex but should not
1550
     * be exploitable at runtime.
1551
     */
1552
0
    if ((atom->min == 0) && (atom->start0 == NULL)) {
1553
0
        xmlRegAtomPtr copy;
1554
        /*
1555
         * duplicate a transition based on atom to count next
1556
         * occurrences after 1. We cannot loop to atom->start
1557
         * directly because we need an epsilon transition to
1558
         * newstate.
1559
         */
1560
         /* ???? For some reason it seems we never reach that
1561
            case, I suppose this got optimized out before when
1562
      building the automata */
1563
0
        copy = xmlRegCopyAtom(ctxt, atom);
1564
0
        if (copy == NULL)
1565
0
            return(-1);
1566
0
        copy->quant = XML_REGEXP_QUANT_ONCE;
1567
0
        copy->min = 0;
1568
0
        copy->max = 0;
1569
1570
0
        if (xmlFAGenerateTransitions(ctxt, atom->start, NULL, copy)
1571
0
            < 0) {
1572
0
                        xmlRegFreeAtom(copy);
1573
0
      return(-1);
1574
0
                    }
1575
0
        inter = ctxt->state;
1576
0
        counter = xmlRegGetCounter(ctxt);
1577
0
                    if (counter < 0)
1578
0
                        return(-1);
1579
0
        ctxt->counters[counter].min = atom->min - 1;
1580
0
        ctxt->counters[counter].max = atom->max - 1;
1581
        /* count the number of times we see it again */
1582
0
        xmlFAGenerateCountedEpsilonTransition(ctxt, inter,
1583
0
               atom->stop, counter);
1584
        /* allow a way out based on the count */
1585
0
        xmlFAGenerateCountedTransition(ctxt, inter,
1586
0
                                 newstate, counter);
1587
        /* and also allow a direct exit for 0 */
1588
0
        xmlFAGenerateEpsilonTransition(ctxt, atom->start,
1589
0
                                       newstate);
1590
0
    } else {
1591
        /*
1592
         * either we need the atom at least once or there
1593
         * is an atom->start0 allowing to easily plug the
1594
         * epsilon transition.
1595
         */
1596
0
        counter = xmlRegGetCounter(ctxt);
1597
0
                    if (counter < 0)
1598
0
                        return(-1);
1599
0
        ctxt->counters[counter].min = atom->min - 1;
1600
0
        ctxt->counters[counter].max = atom->max - 1;
1601
        /* allow a way out based on the count */
1602
0
        xmlFAGenerateCountedTransition(ctxt, atom->stop,
1603
0
                                 newstate, counter);
1604
        /* count the number of times we see it again */
1605
0
        xmlFAGenerateCountedEpsilonTransition(ctxt, atom->stop,
1606
0
               atom->start, counter);
1607
        /* and if needed allow a direct exit for 0 */
1608
0
        if (atom->min == 0)
1609
0
      xmlFAGenerateEpsilonTransition(ctxt, atom->start0,
1610
0
                   newstate);
1611
1612
0
    }
1613
0
    atom->min = 0;
1614
0
    atom->max = 0;
1615
0
    atom->quant = XML_REGEXP_QUANT_ONCE;
1616
0
    ctxt->state = newstate;
1617
0
      }
1618
0
      default:
1619
0
    break;
1620
0
  }
1621
0
  if (xmlRegAtomPush(ctxt, atom) < 0)
1622
0
      return(-1);
1623
0
  return(0);
1624
0
    }
1625
0
    if ((atom->min == 0) && (atom->max == 0) &&
1626
0
               (atom->quant == XML_REGEXP_QUANT_RANGE)) {
1627
        /*
1628
   * we can discard the atom and generate an epsilon transition instead
1629
   */
1630
0
  if (to == NULL) {
1631
0
      to = xmlRegStatePush(ctxt);
1632
0
      if (to == NULL)
1633
0
    return(-1);
1634
0
  }
1635
0
  xmlFAGenerateEpsilonTransition(ctxt, from, to);
1636
0
  ctxt->state = to;
1637
0
  xmlRegFreeAtom(atom);
1638
0
  return(0);
1639
0
    }
1640
0
    if (to == NULL) {
1641
0
  to = xmlRegStatePush(ctxt);
1642
0
  if (to == NULL)
1643
0
      return(-1);
1644
0
    }
1645
0
    end = to;
1646
0
    if ((atom->quant == XML_REGEXP_QUANT_MULT) ||
1647
0
        (atom->quant == XML_REGEXP_QUANT_PLUS)) {
1648
  /*
1649
   * Do not pollute the target state by adding transitions from
1650
   * it as it is likely to be the shared target of multiple branches.
1651
   * So isolate with an epsilon transition.
1652
   */
1653
0
        xmlRegStatePtr tmp;
1654
1655
0
  tmp = xmlRegStatePush(ctxt);
1656
0
        if (tmp == NULL)
1657
0
      return(-1);
1658
0
  xmlFAGenerateEpsilonTransition(ctxt, tmp, to);
1659
0
  to = tmp;
1660
0
    }
1661
0
    if ((atom->quant == XML_REGEXP_QUANT_RANGE) &&
1662
0
        (atom->min == 0) && (atom->max > 0)) {
1663
0
  nullable = 1;
1664
0
  atom->min = 1;
1665
0
        if (atom->max == 1)
1666
0
      atom->quant = XML_REGEXP_QUANT_OPT;
1667
0
    }
1668
0
    xmlRegStateAddTrans(ctxt, from, atom, to, -1, -1);
1669
0
    ctxt->state = end;
1670
0
    switch (atom->quant) {
1671
0
  case XML_REGEXP_QUANT_OPT:
1672
0
      atom->quant = XML_REGEXP_QUANT_ONCE;
1673
0
      xmlFAGenerateEpsilonTransition(ctxt, from, to);
1674
0
      break;
1675
0
  case XML_REGEXP_QUANT_MULT:
1676
0
      atom->quant = XML_REGEXP_QUANT_ONCE;
1677
0
      xmlFAGenerateEpsilonTransition(ctxt, from, to);
1678
0
      xmlRegStateAddTrans(ctxt, to, atom, to, -1, -1);
1679
0
      break;
1680
0
  case XML_REGEXP_QUANT_PLUS:
1681
0
      atom->quant = XML_REGEXP_QUANT_ONCE;
1682
0
      xmlRegStateAddTrans(ctxt, to, atom, to, -1, -1);
1683
0
      break;
1684
0
  case XML_REGEXP_QUANT_RANGE:
1685
0
      if (nullable)
1686
0
    xmlFAGenerateEpsilonTransition(ctxt, from, to);
1687
0
      break;
1688
0
  default:
1689
0
      break;
1690
0
    }
1691
0
    if (xmlRegAtomPush(ctxt, atom) < 0)
1692
0
  return(-1);
1693
0
    return(0);
1694
0
}
1695
1696
/**
1697
 * xmlFAReduceEpsilonTransitions:
1698
 * @ctxt:  a regexp parser context
1699
 * @fromnr:  the from state
1700
 * @tonr:  the to state
1701
 * @counter:  should that transition be associated to a counted
1702
 *
1703
 */
1704
static void
1705
xmlFAReduceEpsilonTransitions(xmlRegParserCtxtPtr ctxt, int fromnr,
1706
0
                        int tonr, int counter) {
1707
0
    int transnr;
1708
0
    xmlRegStatePtr from;
1709
0
    xmlRegStatePtr to;
1710
1711
0
    from = ctxt->states[fromnr];
1712
0
    if (from == NULL)
1713
0
  return;
1714
0
    to = ctxt->states[tonr];
1715
0
    if (to == NULL)
1716
0
  return;
1717
0
    if ((to->mark == XML_REGEXP_MARK_START) ||
1718
0
  (to->mark == XML_REGEXP_MARK_VISITED))
1719
0
  return;
1720
1721
0
    to->mark = XML_REGEXP_MARK_VISITED;
1722
0
    if (to->type == XML_REGEXP_FINAL_STATE) {
1723
0
  from->type = XML_REGEXP_FINAL_STATE;
1724
0
    }
1725
0
    for (transnr = 0;transnr < to->nbTrans;transnr++) {
1726
0
        xmlRegTransPtr t1 = &to->trans[transnr];
1727
0
        int tcounter;
1728
1729
0
        if (t1->to < 0)
1730
0
      continue;
1731
0
        if (t1->counter >= 0) {
1732
            /* assert(counter < 0); */
1733
0
            tcounter = t1->counter;
1734
0
        } else {
1735
0
            tcounter = counter;
1736
0
        }
1737
0
  if (t1->atom == NULL) {
1738
      /*
1739
       * Don't remove counted transitions
1740
       * Don't loop either
1741
       */
1742
0
      if (t1->to != fromnr) {
1743
0
    if (t1->count >= 0) {
1744
0
        xmlRegStateAddTrans(ctxt, from, NULL, ctxt->states[t1->to],
1745
0
          -1, t1->count);
1746
0
    } else {
1747
0
                    xmlFAReduceEpsilonTransitions(ctxt, fromnr, t1->to,
1748
0
                                                  tcounter);
1749
0
    }
1750
0
      }
1751
0
  } else {
1752
0
            xmlRegStateAddTrans(ctxt, from, t1->atom,
1753
0
                                ctxt->states[t1->to], tcounter, -1);
1754
0
  }
1755
0
    }
1756
0
}
1757
1758
/**
1759
 * xmlFAFinishReduceEpsilonTransitions:
1760
 * @ctxt:  a regexp parser context
1761
 * @fromnr:  the from state
1762
 * @tonr:  the to state
1763
 * @counter:  should that transition be associated to a counted
1764
 *
1765
 */
1766
static void
1767
0
xmlFAFinishReduceEpsilonTransitions(xmlRegParserCtxtPtr ctxt, int tonr) {
1768
0
    int transnr;
1769
0
    xmlRegStatePtr to;
1770
1771
0
    to = ctxt->states[tonr];
1772
0
    if (to == NULL)
1773
0
  return;
1774
0
    if ((to->mark == XML_REGEXP_MARK_START) ||
1775
0
  (to->mark == XML_REGEXP_MARK_NORMAL))
1776
0
  return;
1777
1778
0
    to->mark = XML_REGEXP_MARK_NORMAL;
1779
0
    for (transnr = 0;transnr < to->nbTrans;transnr++) {
1780
0
  xmlRegTransPtr t1 = &to->trans[transnr];
1781
0
  if ((t1->to >= 0) && (t1->atom == NULL))
1782
0
            xmlFAFinishReduceEpsilonTransitions(ctxt, t1->to);
1783
0
    }
1784
0
}
1785
1786
/**
1787
 * xmlFAEliminateSimpleEpsilonTransitions:
1788
 * @ctxt:  a regexp parser context
1789
 *
1790
 * Eliminating general epsilon transitions can get costly in the general
1791
 * algorithm due to the large amount of generated new transitions and
1792
 * associated comparisons. However for simple epsilon transition used just
1793
 * to separate building blocks when generating the automata this can be
1794
 * reduced to state elimination:
1795
 *    - if there exists an epsilon from X to Y
1796
 *    - if there is no other transition from X
1797
 * then X and Y are semantically equivalent and X can be eliminated
1798
 * If X is the start state then make Y the start state, else replace the
1799
 * target of all transitions to X by transitions to Y.
1800
 *
1801
 * If X is a final state, skip it.
1802
 * Otherwise it would be necessary to manipulate counters for this case when
1803
 * eliminating state 2:
1804
 * State 1 has a transition with an atom to state 2.
1805
 * State 2 is final and has an epsilon transition to state 1.
1806
 */
1807
static void
1808
0
xmlFAEliminateSimpleEpsilonTransitions(xmlRegParserCtxtPtr ctxt) {
1809
0
    int statenr, i, j, newto;
1810
0
    xmlRegStatePtr state, tmp;
1811
1812
0
    for (statenr = 0;statenr < ctxt->nbStates;statenr++) {
1813
0
  state = ctxt->states[statenr];
1814
0
  if (state == NULL)
1815
0
      continue;
1816
0
  if (state->nbTrans != 1)
1817
0
      continue;
1818
0
       if (state->type == XML_REGEXP_UNREACH_STATE ||
1819
0
           state->type == XML_REGEXP_FINAL_STATE)
1820
0
      continue;
1821
  /* is the only transition out a basic transition */
1822
0
  if ((state->trans[0].atom == NULL) &&
1823
0
      (state->trans[0].to >= 0) &&
1824
0
      (state->trans[0].to != statenr) &&
1825
0
      (state->trans[0].counter < 0) &&
1826
0
      (state->trans[0].count < 0)) {
1827
0
      newto = state->trans[0].to;
1828
1829
0
            if (state->type == XML_REGEXP_START_STATE) {
1830
0
            } else {
1831
0
          for (i = 0;i < state->nbTransTo;i++) {
1832
0
        tmp = ctxt->states[state->transTo[i]];
1833
0
        for (j = 0;j < tmp->nbTrans;j++) {
1834
0
      if (tmp->trans[j].to == statenr) {
1835
0
          tmp->trans[j].to = -1;
1836
0
          xmlRegStateAddTrans(ctxt, tmp, tmp->trans[j].atom,
1837
0
            ctxt->states[newto],
1838
0
                  tmp->trans[j].counter,
1839
0
            tmp->trans[j].count);
1840
0
      }
1841
0
        }
1842
0
    }
1843
0
    if (state->type == XML_REGEXP_FINAL_STATE)
1844
0
        ctxt->states[newto]->type = XML_REGEXP_FINAL_STATE;
1845
    /* eliminate the transition completely */
1846
0
    state->nbTrans = 0;
1847
1848
0
                state->type = XML_REGEXP_UNREACH_STATE;
1849
1850
0
      }
1851
1852
0
  }
1853
0
    }
1854
0
}
1855
/**
1856
 * xmlFAEliminateEpsilonTransitions:
1857
 * @ctxt:  a regexp parser context
1858
 *
1859
 */
1860
static void
1861
0
xmlFAEliminateEpsilonTransitions(xmlRegParserCtxtPtr ctxt) {
1862
0
    int statenr, transnr;
1863
0
    xmlRegStatePtr state;
1864
0
    int has_epsilon;
1865
1866
0
    if (ctxt->states == NULL) return;
1867
1868
    /*
1869
     * Eliminate simple epsilon transition and the associated unreachable
1870
     * states.
1871
     */
1872
0
    xmlFAEliminateSimpleEpsilonTransitions(ctxt);
1873
0
    for (statenr = 0;statenr < ctxt->nbStates;statenr++) {
1874
0
  state = ctxt->states[statenr];
1875
0
  if ((state != NULL) && (state->type == XML_REGEXP_UNREACH_STATE)) {
1876
0
      xmlRegFreeState(state);
1877
0
      ctxt->states[statenr] = NULL;
1878
0
  }
1879
0
    }
1880
1881
0
    has_epsilon = 0;
1882
1883
    /*
1884
     * Build the completed transitions bypassing the epsilons
1885
     * Use a marking algorithm to avoid loops
1886
     * Mark sink states too.
1887
     * Process from the latest states backward to the start when
1888
     * there is long cascading epsilon chains this minimize the
1889
     * recursions and transition compares when adding the new ones
1890
     */
1891
0
    for (statenr = ctxt->nbStates - 1;statenr >= 0;statenr--) {
1892
0
  state = ctxt->states[statenr];
1893
0
  if (state == NULL)
1894
0
      continue;
1895
0
  if ((state->nbTrans == 0) &&
1896
0
      (state->type != XML_REGEXP_FINAL_STATE)) {
1897
0
      state->type = XML_REGEXP_SINK_STATE;
1898
0
  }
1899
0
  for (transnr = 0;transnr < state->nbTrans;transnr++) {
1900
0
      if ((state->trans[transnr].atom == NULL) &&
1901
0
    (state->trans[transnr].to >= 0)) {
1902
0
    if (state->trans[transnr].to == statenr) {
1903
0
        state->trans[transnr].to = -1;
1904
0
    } else if (state->trans[transnr].count < 0) {
1905
0
        int newto = state->trans[transnr].to;
1906
1907
0
        has_epsilon = 1;
1908
0
        state->trans[transnr].to = -2;
1909
0
        state->mark = XML_REGEXP_MARK_START;
1910
0
        xmlFAReduceEpsilonTransitions(ctxt, statenr,
1911
0
              newto, state->trans[transnr].counter);
1912
0
        xmlFAFinishReduceEpsilonTransitions(ctxt, newto);
1913
0
        state->mark = XML_REGEXP_MARK_NORMAL;
1914
0
          }
1915
0
      }
1916
0
  }
1917
0
    }
1918
    /*
1919
     * Eliminate the epsilon transitions
1920
     */
1921
0
    if (has_epsilon) {
1922
0
  for (statenr = 0;statenr < ctxt->nbStates;statenr++) {
1923
0
      state = ctxt->states[statenr];
1924
0
      if (state == NULL)
1925
0
    continue;
1926
0
      for (transnr = 0;transnr < state->nbTrans;transnr++) {
1927
0
    xmlRegTransPtr trans = &(state->trans[transnr]);
1928
0
    if ((trans->atom == NULL) &&
1929
0
        (trans->count < 0) &&
1930
0
        (trans->to >= 0)) {
1931
0
        trans->to = -1;
1932
0
    }
1933
0
      }
1934
0
  }
1935
0
    }
1936
1937
    /*
1938
     * Use this pass to detect unreachable states too
1939
     */
1940
0
    for (statenr = 0;statenr < ctxt->nbStates;statenr++) {
1941
0
  state = ctxt->states[statenr];
1942
0
  if (state != NULL)
1943
0
      state->reached = XML_REGEXP_MARK_NORMAL;
1944
0
    }
1945
0
    state = ctxt->states[0];
1946
0
    if (state != NULL)
1947
0
  state->reached = XML_REGEXP_MARK_START;
1948
0
    while (state != NULL) {
1949
0
  xmlRegStatePtr target = NULL;
1950
0
  state->reached = XML_REGEXP_MARK_VISITED;
1951
  /*
1952
   * Mark all states reachable from the current reachable state
1953
   */
1954
0
  for (transnr = 0;transnr < state->nbTrans;transnr++) {
1955
0
      if ((state->trans[transnr].to >= 0) &&
1956
0
    ((state->trans[transnr].atom != NULL) ||
1957
0
     (state->trans[transnr].count >= 0))) {
1958
0
    int newto = state->trans[transnr].to;
1959
1960
0
    if (ctxt->states[newto] == NULL)
1961
0
        continue;
1962
0
    if (ctxt->states[newto]->reached == XML_REGEXP_MARK_NORMAL) {
1963
0
        ctxt->states[newto]->reached = XML_REGEXP_MARK_START;
1964
0
        target = ctxt->states[newto];
1965
0
    }
1966
0
      }
1967
0
  }
1968
1969
  /*
1970
   * find the next accessible state not explored
1971
   */
1972
0
  if (target == NULL) {
1973
0
      for (statenr = 1;statenr < ctxt->nbStates;statenr++) {
1974
0
    state = ctxt->states[statenr];
1975
0
    if ((state != NULL) && (state->reached ==
1976
0
      XML_REGEXP_MARK_START)) {
1977
0
        target = state;
1978
0
        break;
1979
0
    }
1980
0
      }
1981
0
  }
1982
0
  state = target;
1983
0
    }
1984
0
    for (statenr = 0;statenr < ctxt->nbStates;statenr++) {
1985
0
  state = ctxt->states[statenr];
1986
0
  if ((state != NULL) && (state->reached == XML_REGEXP_MARK_NORMAL)) {
1987
0
      xmlRegFreeState(state);
1988
0
      ctxt->states[statenr] = NULL;
1989
0
  }
1990
0
    }
1991
1992
0
}
1993
1994
static int
1995
0
xmlFACompareRanges(xmlRegRangePtr range1, xmlRegRangePtr range2) {
1996
0
    int ret = 0;
1997
1998
0
    if ((range1->type == XML_REGEXP_RANGES) ||
1999
0
        (range2->type == XML_REGEXP_RANGES) ||
2000
0
        (range2->type == XML_REGEXP_SUBREG) ||
2001
0
        (range1->type == XML_REGEXP_SUBREG) ||
2002
0
        (range1->type == XML_REGEXP_STRING) ||
2003
0
        (range2->type == XML_REGEXP_STRING))
2004
0
  return(-1);
2005
2006
    /* put them in order */
2007
0
    if (range1->type > range2->type) {
2008
0
        xmlRegRangePtr tmp;
2009
2010
0
  tmp = range1;
2011
0
  range1 = range2;
2012
0
  range2 = tmp;
2013
0
    }
2014
0
    if ((range1->type == XML_REGEXP_ANYCHAR) ||
2015
0
        (range2->type == XML_REGEXP_ANYCHAR)) {
2016
0
  ret = 1;
2017
0
    } else if ((range1->type == XML_REGEXP_EPSILON) ||
2018
0
               (range2->type == XML_REGEXP_EPSILON)) {
2019
0
  return(0);
2020
0
    } else if (range1->type == range2->type) {
2021
0
        if (range1->type != XML_REGEXP_CHARVAL)
2022
0
            ret = 1;
2023
0
        else if ((range1->end < range2->start) ||
2024
0
           (range2->end < range1->start))
2025
0
      ret = 0;
2026
0
  else
2027
0
      ret = 1;
2028
0
    } else if (range1->type == XML_REGEXP_CHARVAL) {
2029
0
        int codepoint;
2030
0
  int neg = 0;
2031
2032
  /*
2033
   * just check all codepoints in the range for acceptance,
2034
   * this is usually way cheaper since done only once at
2035
   * compilation than testing over and over at runtime or
2036
   * pushing too many states when evaluating.
2037
   */
2038
0
  if (((range1->neg == 0) && (range2->neg != 0)) ||
2039
0
      ((range1->neg != 0) && (range2->neg == 0)))
2040
0
      neg = 1;
2041
2042
0
  for (codepoint = range1->start;codepoint <= range1->end ;codepoint++) {
2043
0
      ret = xmlRegCheckCharacterRange(range2->type, codepoint,
2044
0
              0, range2->start, range2->end,
2045
0
              range2->blockName);
2046
0
      if (ret < 0)
2047
0
          return(-1);
2048
0
      if (((neg == 1) && (ret == 0)) ||
2049
0
          ((neg == 0) && (ret == 1)))
2050
0
    return(1);
2051
0
  }
2052
0
  return(0);
2053
0
    } else if ((range1->type == XML_REGEXP_BLOCK_NAME) ||
2054
0
               (range2->type == XML_REGEXP_BLOCK_NAME)) {
2055
0
  if (range1->type == range2->type) {
2056
0
      ret = xmlStrEqual(range1->blockName, range2->blockName);
2057
0
  } else {
2058
      /*
2059
       * comparing a block range with anything else is way
2060
       * too costly, and maintaining the table is like too much
2061
       * memory too, so let's force the automata to save state
2062
       * here.
2063
       */
2064
0
      return(1);
2065
0
  }
2066
0
    } else if ((range1->type < XML_REGEXP_LETTER) ||
2067
0
               (range2->type < XML_REGEXP_LETTER)) {
2068
0
  if ((range1->type == XML_REGEXP_ANYSPACE) &&
2069
0
      (range2->type == XML_REGEXP_NOTSPACE))
2070
0
      ret = 0;
2071
0
  else if ((range1->type == XML_REGEXP_INITNAME) &&
2072
0
           (range2->type == XML_REGEXP_NOTINITNAME))
2073
0
      ret = 0;
2074
0
  else if ((range1->type == XML_REGEXP_NAMECHAR) &&
2075
0
           (range2->type == XML_REGEXP_NOTNAMECHAR))
2076
0
      ret = 0;
2077
0
  else if ((range1->type == XML_REGEXP_DECIMAL) &&
2078
0
           (range2->type == XML_REGEXP_NOTDECIMAL))
2079
0
      ret = 0;
2080
0
  else if ((range1->type == XML_REGEXP_REALCHAR) &&
2081
0
           (range2->type == XML_REGEXP_NOTREALCHAR))
2082
0
      ret = 0;
2083
0
  else {
2084
      /* same thing to limit complexity */
2085
0
      return(1);
2086
0
  }
2087
0
    } else {
2088
0
        ret = 0;
2089
        /* range1->type < range2->type here */
2090
0
        switch (range1->type) {
2091
0
      case XML_REGEXP_LETTER:
2092
           /* all disjoint except in the subgroups */
2093
0
           if ((range2->type == XML_REGEXP_LETTER_UPPERCASE) ||
2094
0
         (range2->type == XML_REGEXP_LETTER_LOWERCASE) ||
2095
0
         (range2->type == XML_REGEXP_LETTER_TITLECASE) ||
2096
0
         (range2->type == XML_REGEXP_LETTER_MODIFIER) ||
2097
0
         (range2->type == XML_REGEXP_LETTER_OTHERS))
2098
0
         ret = 1;
2099
0
     break;
2100
0
      case XML_REGEXP_MARK:
2101
0
           if ((range2->type == XML_REGEXP_MARK_NONSPACING) ||
2102
0
         (range2->type == XML_REGEXP_MARK_SPACECOMBINING) ||
2103
0
         (range2->type == XML_REGEXP_MARK_ENCLOSING))
2104
0
         ret = 1;
2105
0
     break;
2106
0
      case XML_REGEXP_NUMBER:
2107
0
           if ((range2->type == XML_REGEXP_NUMBER_DECIMAL) ||
2108
0
         (range2->type == XML_REGEXP_NUMBER_LETTER) ||
2109
0
         (range2->type == XML_REGEXP_NUMBER_OTHERS))
2110
0
         ret = 1;
2111
0
     break;
2112
0
      case XML_REGEXP_PUNCT:
2113
0
           if ((range2->type == XML_REGEXP_PUNCT_CONNECTOR) ||
2114
0
         (range2->type == XML_REGEXP_PUNCT_DASH) ||
2115
0
         (range2->type == XML_REGEXP_PUNCT_OPEN) ||
2116
0
         (range2->type == XML_REGEXP_PUNCT_CLOSE) ||
2117
0
         (range2->type == XML_REGEXP_PUNCT_INITQUOTE) ||
2118
0
         (range2->type == XML_REGEXP_PUNCT_FINQUOTE) ||
2119
0
         (range2->type == XML_REGEXP_PUNCT_OTHERS))
2120
0
         ret = 1;
2121
0
     break;
2122
0
      case XML_REGEXP_SEPAR:
2123
0
           if ((range2->type == XML_REGEXP_SEPAR_SPACE) ||
2124
0
         (range2->type == XML_REGEXP_SEPAR_LINE) ||
2125
0
         (range2->type == XML_REGEXP_SEPAR_PARA))
2126
0
         ret = 1;
2127
0
     break;
2128
0
      case XML_REGEXP_SYMBOL:
2129
0
           if ((range2->type == XML_REGEXP_SYMBOL_MATH) ||
2130
0
         (range2->type == XML_REGEXP_SYMBOL_CURRENCY) ||
2131
0
         (range2->type == XML_REGEXP_SYMBOL_MODIFIER) ||
2132
0
         (range2->type == XML_REGEXP_SYMBOL_OTHERS))
2133
0
         ret = 1;
2134
0
     break;
2135
0
      case XML_REGEXP_OTHER:
2136
0
           if ((range2->type == XML_REGEXP_OTHER_CONTROL) ||
2137
0
         (range2->type == XML_REGEXP_OTHER_FORMAT) ||
2138
0
         (range2->type == XML_REGEXP_OTHER_PRIVATE))
2139
0
         ret = 1;
2140
0
     break;
2141
0
            default:
2142
0
           if ((range2->type >= XML_REGEXP_LETTER) &&
2143
0
         (range2->type < XML_REGEXP_BLOCK_NAME))
2144
0
         ret = 0;
2145
0
     else {
2146
         /* safety net ! */
2147
0
         return(1);
2148
0
     }
2149
0
  }
2150
0
    }
2151
0
    if (((range1->neg == 0) && (range2->neg != 0)) ||
2152
0
        ((range1->neg != 0) && (range2->neg == 0)))
2153
0
  ret = !ret;
2154
0
    return(ret);
2155
0
}
2156
2157
/**
2158
 * xmlFACompareAtomTypes:
2159
 * @type1:  an atom type
2160
 * @type2:  an atom type
2161
 *
2162
 * Compares two atoms type to check whether they intersect in some ways,
2163
 * this is used by xmlFACompareAtoms only
2164
 *
2165
 * Returns 1 if they may intersect and 0 otherwise
2166
 */
2167
static int
2168
0
xmlFACompareAtomTypes(xmlRegAtomType type1, xmlRegAtomType type2) {
2169
0
    if ((type1 == XML_REGEXP_EPSILON) ||
2170
0
        (type1 == XML_REGEXP_CHARVAL) ||
2171
0
  (type1 == XML_REGEXP_RANGES) ||
2172
0
  (type1 == XML_REGEXP_SUBREG) ||
2173
0
  (type1 == XML_REGEXP_STRING) ||
2174
0
  (type1 == XML_REGEXP_ANYCHAR))
2175
0
  return(1);
2176
0
    if ((type2 == XML_REGEXP_EPSILON) ||
2177
0
        (type2 == XML_REGEXP_CHARVAL) ||
2178
0
  (type2 == XML_REGEXP_RANGES) ||
2179
0
  (type2 == XML_REGEXP_SUBREG) ||
2180
0
  (type2 == XML_REGEXP_STRING) ||
2181
0
  (type2 == XML_REGEXP_ANYCHAR))
2182
0
  return(1);
2183
2184
0
    if (type1 == type2) return(1);
2185
2186
    /* simplify subsequent compares by making sure type1 < type2 */
2187
0
    if (type1 > type2) {
2188
0
        xmlRegAtomType tmp = type1;
2189
0
  type1 = type2;
2190
0
  type2 = tmp;
2191
0
    }
2192
0
    switch (type1) {
2193
0
        case XML_REGEXP_ANYSPACE: /* \s */
2194
      /* can't be a letter, number, mark, punctuation, symbol */
2195
0
      if ((type2 == XML_REGEXP_NOTSPACE) ||
2196
0
    ((type2 >= XML_REGEXP_LETTER) &&
2197
0
     (type2 <= XML_REGEXP_LETTER_OTHERS)) ||
2198
0
          ((type2 >= XML_REGEXP_NUMBER) &&
2199
0
     (type2 <= XML_REGEXP_NUMBER_OTHERS)) ||
2200
0
          ((type2 >= XML_REGEXP_MARK) &&
2201
0
     (type2 <= XML_REGEXP_MARK_ENCLOSING)) ||
2202
0
          ((type2 >= XML_REGEXP_PUNCT) &&
2203
0
     (type2 <= XML_REGEXP_PUNCT_OTHERS)) ||
2204
0
          ((type2 >= XML_REGEXP_SYMBOL) &&
2205
0
     (type2 <= XML_REGEXP_SYMBOL_OTHERS))
2206
0
          ) return(0);
2207
0
      break;
2208
0
        case XML_REGEXP_NOTSPACE: /* \S */
2209
0
      break;
2210
0
        case XML_REGEXP_INITNAME: /* \l */
2211
      /* can't be a number, mark, separator, punctuation, symbol or other */
2212
0
      if ((type2 == XML_REGEXP_NOTINITNAME) ||
2213
0
          ((type2 >= XML_REGEXP_NUMBER) &&
2214
0
     (type2 <= XML_REGEXP_NUMBER_OTHERS)) ||
2215
0
          ((type2 >= XML_REGEXP_MARK) &&
2216
0
     (type2 <= XML_REGEXP_MARK_ENCLOSING)) ||
2217
0
          ((type2 >= XML_REGEXP_SEPAR) &&
2218
0
     (type2 <= XML_REGEXP_SEPAR_PARA)) ||
2219
0
          ((type2 >= XML_REGEXP_PUNCT) &&
2220
0
     (type2 <= XML_REGEXP_PUNCT_OTHERS)) ||
2221
0
          ((type2 >= XML_REGEXP_SYMBOL) &&
2222
0
     (type2 <= XML_REGEXP_SYMBOL_OTHERS)) ||
2223
0
          ((type2 >= XML_REGEXP_OTHER) &&
2224
0
     (type2 <= XML_REGEXP_OTHER_NA))
2225
0
    ) return(0);
2226
0
      break;
2227
0
        case XML_REGEXP_NOTINITNAME: /* \L */
2228
0
      break;
2229
0
        case XML_REGEXP_NAMECHAR: /* \c */
2230
      /* can't be a mark, separator, punctuation, symbol or other */
2231
0
      if ((type2 == XML_REGEXP_NOTNAMECHAR) ||
2232
0
          ((type2 >= XML_REGEXP_MARK) &&
2233
0
     (type2 <= XML_REGEXP_MARK_ENCLOSING)) ||
2234
0
          ((type2 >= XML_REGEXP_PUNCT) &&
2235
0
     (type2 <= XML_REGEXP_PUNCT_OTHERS)) ||
2236
0
          ((type2 >= XML_REGEXP_SEPAR) &&
2237
0
     (type2 <= XML_REGEXP_SEPAR_PARA)) ||
2238
0
          ((type2 >= XML_REGEXP_SYMBOL) &&
2239
0
     (type2 <= XML_REGEXP_SYMBOL_OTHERS)) ||
2240
0
          ((type2 >= XML_REGEXP_OTHER) &&
2241
0
     (type2 <= XML_REGEXP_OTHER_NA))
2242
0
    ) return(0);
2243
0
      break;
2244
0
        case XML_REGEXP_NOTNAMECHAR: /* \C */
2245
0
      break;
2246
0
        case XML_REGEXP_DECIMAL: /* \d */
2247
      /* can't be a letter, mark, separator, punctuation, symbol or other */
2248
0
      if ((type2 == XML_REGEXP_NOTDECIMAL) ||
2249
0
          (type2 == XML_REGEXP_REALCHAR) ||
2250
0
    ((type2 >= XML_REGEXP_LETTER) &&
2251
0
     (type2 <= XML_REGEXP_LETTER_OTHERS)) ||
2252
0
          ((type2 >= XML_REGEXP_MARK) &&
2253
0
     (type2 <= XML_REGEXP_MARK_ENCLOSING)) ||
2254
0
          ((type2 >= XML_REGEXP_PUNCT) &&
2255
0
     (type2 <= XML_REGEXP_PUNCT_OTHERS)) ||
2256
0
          ((type2 >= XML_REGEXP_SEPAR) &&
2257
0
     (type2 <= XML_REGEXP_SEPAR_PARA)) ||
2258
0
          ((type2 >= XML_REGEXP_SYMBOL) &&
2259
0
     (type2 <= XML_REGEXP_SYMBOL_OTHERS)) ||
2260
0
          ((type2 >= XML_REGEXP_OTHER) &&
2261
0
     (type2 <= XML_REGEXP_OTHER_NA))
2262
0
    )return(0);
2263
0
      break;
2264
0
        case XML_REGEXP_NOTDECIMAL: /* \D */
2265
0
      break;
2266
0
        case XML_REGEXP_REALCHAR: /* \w */
2267
      /* can't be a mark, separator, punctuation, symbol or other */
2268
0
      if ((type2 == XML_REGEXP_NOTDECIMAL) ||
2269
0
          ((type2 >= XML_REGEXP_MARK) &&
2270
0
     (type2 <= XML_REGEXP_MARK_ENCLOSING)) ||
2271
0
          ((type2 >= XML_REGEXP_PUNCT) &&
2272
0
     (type2 <= XML_REGEXP_PUNCT_OTHERS)) ||
2273
0
          ((type2 >= XML_REGEXP_SEPAR) &&
2274
0
     (type2 <= XML_REGEXP_SEPAR_PARA)) ||
2275
0
          ((type2 >= XML_REGEXP_SYMBOL) &&
2276
0
     (type2 <= XML_REGEXP_SYMBOL_OTHERS)) ||
2277
0
          ((type2 >= XML_REGEXP_OTHER) &&
2278
0
     (type2 <= XML_REGEXP_OTHER_NA))
2279
0
    )return(0);
2280
0
      break;
2281
0
        case XML_REGEXP_NOTREALCHAR: /* \W */
2282
0
      break;
2283
  /*
2284
   * at that point we know both type 1 and type2 are from
2285
   * character categories are ordered and are different,
2286
   * it becomes simple because this is a partition
2287
   */
2288
0
        case XML_REGEXP_LETTER:
2289
0
      if (type2 <= XML_REGEXP_LETTER_OTHERS)
2290
0
          return(1);
2291
0
      return(0);
2292
0
        case XML_REGEXP_LETTER_UPPERCASE:
2293
0
        case XML_REGEXP_LETTER_LOWERCASE:
2294
0
        case XML_REGEXP_LETTER_TITLECASE:
2295
0
        case XML_REGEXP_LETTER_MODIFIER:
2296
0
        case XML_REGEXP_LETTER_OTHERS:
2297
0
      return(0);
2298
0
        case XML_REGEXP_MARK:
2299
0
      if (type2 <= XML_REGEXP_MARK_ENCLOSING)
2300
0
          return(1);
2301
0
      return(0);
2302
0
        case XML_REGEXP_MARK_NONSPACING:
2303
0
        case XML_REGEXP_MARK_SPACECOMBINING:
2304
0
        case XML_REGEXP_MARK_ENCLOSING:
2305
0
      return(0);
2306
0
        case XML_REGEXP_NUMBER:
2307
0
      if (type2 <= XML_REGEXP_NUMBER_OTHERS)
2308
0
          return(1);
2309
0
      return(0);
2310
0
        case XML_REGEXP_NUMBER_DECIMAL:
2311
0
        case XML_REGEXP_NUMBER_LETTER:
2312
0
        case XML_REGEXP_NUMBER_OTHERS:
2313
0
      return(0);
2314
0
        case XML_REGEXP_PUNCT:
2315
0
      if (type2 <= XML_REGEXP_PUNCT_OTHERS)
2316
0
          return(1);
2317
0
      return(0);
2318
0
        case XML_REGEXP_PUNCT_CONNECTOR:
2319
0
        case XML_REGEXP_PUNCT_DASH:
2320
0
        case XML_REGEXP_PUNCT_OPEN:
2321
0
        case XML_REGEXP_PUNCT_CLOSE:
2322
0
        case XML_REGEXP_PUNCT_INITQUOTE:
2323
0
        case XML_REGEXP_PUNCT_FINQUOTE:
2324
0
        case XML_REGEXP_PUNCT_OTHERS:
2325
0
      return(0);
2326
0
        case XML_REGEXP_SEPAR:
2327
0
      if (type2 <= XML_REGEXP_SEPAR_PARA)
2328
0
          return(1);
2329
0
      return(0);
2330
0
        case XML_REGEXP_SEPAR_SPACE:
2331
0
        case XML_REGEXP_SEPAR_LINE:
2332
0
        case XML_REGEXP_SEPAR_PARA:
2333
0
      return(0);
2334
0
        case XML_REGEXP_SYMBOL:
2335
0
      if (type2 <= XML_REGEXP_SYMBOL_OTHERS)
2336
0
          return(1);
2337
0
      return(0);
2338
0
        case XML_REGEXP_SYMBOL_MATH:
2339
0
        case XML_REGEXP_SYMBOL_CURRENCY:
2340
0
        case XML_REGEXP_SYMBOL_MODIFIER:
2341
0
        case XML_REGEXP_SYMBOL_OTHERS:
2342
0
      return(0);
2343
0
        case XML_REGEXP_OTHER:
2344
0
      if (type2 <= XML_REGEXP_OTHER_NA)
2345
0
          return(1);
2346
0
      return(0);
2347
0
        case XML_REGEXP_OTHER_CONTROL:
2348
0
        case XML_REGEXP_OTHER_FORMAT:
2349
0
        case XML_REGEXP_OTHER_PRIVATE:
2350
0
        case XML_REGEXP_OTHER_NA:
2351
0
      return(0);
2352
0
  default:
2353
0
      break;
2354
0
    }
2355
0
    return(1);
2356
0
}
2357
2358
/**
2359
 * xmlFAEqualAtoms:
2360
 * @atom1:  an atom
2361
 * @atom2:  an atom
2362
 * @deep: if not set only compare string pointers
2363
 *
2364
 * Compares two atoms to check whether they are the same exactly
2365
 * this is used to remove equivalent transitions
2366
 *
2367
 * Returns 1 if same and 0 otherwise
2368
 */
2369
static int
2370
0
xmlFAEqualAtoms(xmlRegAtomPtr atom1, xmlRegAtomPtr atom2, int deep) {
2371
0
    int ret = 0;
2372
2373
0
    if (atom1 == atom2)
2374
0
  return(1);
2375
0
    if ((atom1 == NULL) || (atom2 == NULL))
2376
0
  return(0);
2377
2378
0
    if (atom1->type != atom2->type)
2379
0
        return(0);
2380
0
    switch (atom1->type) {
2381
0
        case XML_REGEXP_EPSILON:
2382
0
      ret = 0;
2383
0
      break;
2384
0
        case XML_REGEXP_STRING:
2385
0
            if (!deep)
2386
0
                ret = (atom1->valuep == atom2->valuep);
2387
0
            else
2388
0
                ret = xmlStrEqual((xmlChar *)atom1->valuep,
2389
0
                                  (xmlChar *)atom2->valuep);
2390
0
      break;
2391
0
        case XML_REGEXP_CHARVAL:
2392
0
      ret = (atom1->codepoint == atom2->codepoint);
2393
0
      break;
2394
0
  case XML_REGEXP_RANGES:
2395
      /* too hard to do in the general case */
2396
0
      ret = 0;
2397
0
  default:
2398
0
      break;
2399
0
    }
2400
0
    return(ret);
2401
0
}
2402
2403
/**
2404
 * xmlFACompareAtoms:
2405
 * @atom1:  an atom
2406
 * @atom2:  an atom
2407
 * @deep: if not set only compare string pointers
2408
 *
2409
 * Compares two atoms to check whether they intersect in some ways,
2410
 * this is used by xmlFAComputesDeterminism and xmlFARecurseDeterminism only
2411
 *
2412
 * Returns 1 if yes and 0 otherwise
2413
 */
2414
static int
2415
0
xmlFACompareAtoms(xmlRegAtomPtr atom1, xmlRegAtomPtr atom2, int deep) {
2416
0
    int ret = 1;
2417
2418
0
    if (atom1 == atom2)
2419
0
  return(1);
2420
0
    if ((atom1 == NULL) || (atom2 == NULL))
2421
0
  return(0);
2422
2423
0
    if ((atom1->type == XML_REGEXP_ANYCHAR) ||
2424
0
        (atom2->type == XML_REGEXP_ANYCHAR))
2425
0
  return(1);
2426
2427
0
    if (atom1->type > atom2->type) {
2428
0
  xmlRegAtomPtr tmp;
2429
0
  tmp = atom1;
2430
0
  atom1 = atom2;
2431
0
  atom2 = tmp;
2432
0
    }
2433
0
    if (atom1->type != atom2->type) {
2434
0
        ret = xmlFACompareAtomTypes(atom1->type, atom2->type);
2435
  /* if they can't intersect at the type level break now */
2436
0
  if (ret == 0)
2437
0
      return(0);
2438
0
    }
2439
0
    switch (atom1->type) {
2440
0
        case XML_REGEXP_STRING:
2441
0
            if (!deep)
2442
0
                ret = (atom1->valuep != atom2->valuep);
2443
0
            else {
2444
0
                xmlChar *val1 = (xmlChar *)atom1->valuep;
2445
0
                xmlChar *val2 = (xmlChar *)atom2->valuep;
2446
0
                int compound1 = (xmlStrchr(val1, '|') != NULL);
2447
0
                int compound2 = (xmlStrchr(val2, '|') != NULL);
2448
2449
                /* Ignore negative match flag for ##other namespaces */
2450
0
                if (compound1 != compound2)
2451
0
                    return(0);
2452
2453
0
                ret = xmlRegStrEqualWildcard(val1, val2);
2454
0
            }
2455
0
      break;
2456
0
        case XML_REGEXP_EPSILON:
2457
0
      goto not_determinist;
2458
0
        case XML_REGEXP_CHARVAL:
2459
0
      if (atom2->type == XML_REGEXP_CHARVAL) {
2460
0
    ret = (atom1->codepoint == atom2->codepoint);
2461
0
      } else {
2462
0
          ret = xmlRegCheckCharacter(atom2, atom1->codepoint);
2463
0
    if (ret < 0)
2464
0
        ret = 1;
2465
0
      }
2466
0
      break;
2467
0
        case XML_REGEXP_RANGES:
2468
0
      if (atom2->type == XML_REGEXP_RANGES) {
2469
0
          int i, j, res;
2470
0
    xmlRegRangePtr r1, r2;
2471
2472
    /*
2473
     * need to check that none of the ranges eventually matches
2474
     */
2475
0
    for (i = 0;i < atom1->nbRanges;i++) {
2476
0
        for (j = 0;j < atom2->nbRanges;j++) {
2477
0
      r1 = atom1->ranges[i];
2478
0
      r2 = atom2->ranges[j];
2479
0
      res = xmlFACompareRanges(r1, r2);
2480
0
      if (res == 1) {
2481
0
          ret = 1;
2482
0
          goto done;
2483
0
      }
2484
0
        }
2485
0
    }
2486
0
    ret = 0;
2487
0
      }
2488
0
      break;
2489
0
  default:
2490
0
      goto not_determinist;
2491
0
    }
2492
0
done:
2493
0
    if (atom1->neg != atom2->neg) {
2494
0
        ret = !ret;
2495
0
    }
2496
0
    if (ret == 0)
2497
0
        return(0);
2498
0
not_determinist:
2499
0
    return(1);
2500
0
}
2501
2502
/**
2503
 * xmlFARecurseDeterminism:
2504
 * @ctxt:  a regexp parser context
2505
 *
2506
 * Check whether the associated regexp is determinist,
2507
 * should be called after xmlFAEliminateEpsilonTransitions()
2508
 *
2509
 */
2510
static int
2511
xmlFARecurseDeterminism(xmlRegParserCtxtPtr ctxt, xmlRegStatePtr state,
2512
0
                  int fromnr, int tonr, xmlRegAtomPtr atom) {
2513
0
    int ret = 1;
2514
0
    int res;
2515
0
    int transnr, nbTrans;
2516
0
    xmlRegTransPtr t1;
2517
0
    int deep = 1;
2518
2519
0
    if (state == NULL)
2520
0
  return(ret);
2521
0
    if (state->markd == XML_REGEXP_MARK_VISITED)
2522
0
  return(ret);
2523
2524
0
    if (ctxt->flags & AM_AUTOMATA_RNG)
2525
0
        deep = 0;
2526
2527
    /*
2528
     * don't recurse on transitions potentially added in the course of
2529
     * the elimination.
2530
     */
2531
0
    nbTrans = state->nbTrans;
2532
0
    for (transnr = 0;transnr < nbTrans;transnr++) {
2533
0
  t1 = &(state->trans[transnr]);
2534
  /*
2535
   * check transitions conflicting with the one looked at
2536
   */
2537
0
        if ((t1->to < 0) || (t1->to == fromnr))
2538
0
            continue;
2539
0
  if (t1->atom == NULL) {
2540
0
      state->markd = XML_REGEXP_MARK_VISITED;
2541
0
      res = xmlFARecurseDeterminism(ctxt, ctxt->states[t1->to],
2542
0
                              fromnr, tonr, atom);
2543
0
      if (res == 0) {
2544
0
          ret = 0;
2545
    /* t1->nd = 1; */
2546
0
      }
2547
0
      continue;
2548
0
  }
2549
0
  if (xmlFACompareAtoms(t1->atom, atom, deep)) {
2550
            /* Treat equal transitions as deterministic. */
2551
0
            if ((t1->to != tonr) ||
2552
0
                (!xmlFAEqualAtoms(t1->atom, atom, deep)))
2553
0
                ret = 0;
2554
      /* mark the transition as non-deterministic */
2555
0
      t1->nd = 1;
2556
0
  }
2557
0
    }
2558
0
    return(ret);
2559
0
}
2560
2561
/**
2562
 * xmlFAFinishRecurseDeterminism:
2563
 * @ctxt:  a regexp parser context
2564
 *
2565
 * Reset flags after checking determinism.
2566
 */
2567
static void
2568
0
xmlFAFinishRecurseDeterminism(xmlRegParserCtxtPtr ctxt, xmlRegStatePtr state) {
2569
0
    int transnr, nbTrans;
2570
2571
0
    if (state == NULL)
2572
0
  return;
2573
0
    if (state->markd != XML_REGEXP_MARK_VISITED)
2574
0
  return;
2575
0
    state->markd = 0;
2576
2577
0
    nbTrans = state->nbTrans;
2578
0
    for (transnr = 0; transnr < nbTrans; transnr++) {
2579
0
  xmlRegTransPtr t1 = &state->trans[transnr];
2580
0
  if ((t1->atom == NULL) && (t1->to >= 0))
2581
0
      xmlFAFinishRecurseDeterminism(ctxt, ctxt->states[t1->to]);
2582
0
    }
2583
0
}
2584
2585
/**
2586
 * xmlFAComputesDeterminism:
2587
 * @ctxt:  a regexp parser context
2588
 *
2589
 * Check whether the associated regexp is determinist,
2590
 * should be called after xmlFAEliminateEpsilonTransitions()
2591
 *
2592
 */
2593
static int
2594
0
xmlFAComputesDeterminism(xmlRegParserCtxtPtr ctxt) {
2595
0
    int statenr, transnr;
2596
0
    xmlRegStatePtr state;
2597
0
    xmlRegTransPtr t1, t2, last;
2598
0
    int i;
2599
0
    int ret = 1;
2600
0
    int deep = 1;
2601
2602
0
    if (ctxt->determinist != -1)
2603
0
  return(ctxt->determinist);
2604
2605
0
    if (ctxt->flags & AM_AUTOMATA_RNG)
2606
0
        deep = 0;
2607
2608
    /*
2609
     * First cleanup the automata removing cancelled transitions
2610
     */
2611
0
    for (statenr = 0;statenr < ctxt->nbStates;statenr++) {
2612
0
  state = ctxt->states[statenr];
2613
0
  if (state == NULL)
2614
0
      continue;
2615
0
  if (state->nbTrans < 2)
2616
0
      continue;
2617
0
  for (transnr = 0;transnr < state->nbTrans;transnr++) {
2618
0
      t1 = &(state->trans[transnr]);
2619
      /*
2620
       * Determinism checks in case of counted or all transitions
2621
       * will have to be handled separately
2622
       */
2623
0
      if (t1->atom == NULL) {
2624
    /* t1->nd = 1; */
2625
0
    continue;
2626
0
      }
2627
0
      if (t1->to < 0) /* eliminated */
2628
0
    continue;
2629
0
      for (i = 0;i < transnr;i++) {
2630
0
    t2 = &(state->trans[i]);
2631
0
    if (t2->to < 0) /* eliminated */
2632
0
        continue;
2633
0
    if (t2->atom != NULL) {
2634
0
        if (t1->to == t2->to) {
2635
                        /*
2636
                         * Here we use deep because we want to keep the
2637
                         * transitions which indicate a conflict
2638
                         */
2639
0
      if (xmlFAEqualAtoms(t1->atom, t2->atom, deep) &&
2640
0
                            (t1->counter == t2->counter) &&
2641
0
                            (t1->count == t2->count))
2642
0
          t2->to = -1; /* eliminated */
2643
0
        }
2644
0
    }
2645
0
      }
2646
0
  }
2647
0
    }
2648
2649
    /*
2650
     * Check for all states that there aren't 2 transitions
2651
     * with the same atom and a different target.
2652
     */
2653
0
    for (statenr = 0;statenr < ctxt->nbStates;statenr++) {
2654
0
  state = ctxt->states[statenr];
2655
0
  if (state == NULL)
2656
0
      continue;
2657
0
  if (state->nbTrans < 2)
2658
0
      continue;
2659
0
  last = NULL;
2660
0
  for (transnr = 0;transnr < state->nbTrans;transnr++) {
2661
0
      t1 = &(state->trans[transnr]);
2662
      /*
2663
       * Determinism checks in case of counted or all transitions
2664
       * will have to be handled separately
2665
       */
2666
0
      if (t1->atom == NULL) {
2667
0
    continue;
2668
0
      }
2669
0
      if (t1->to < 0) /* eliminated */
2670
0
    continue;
2671
0
      for (i = 0;i < transnr;i++) {
2672
0
    t2 = &(state->trans[i]);
2673
0
    if (t2->to < 0) /* eliminated */
2674
0
        continue;
2675
0
    if (t2->atom != NULL) {
2676
                    /*
2677
                     * But here we don't use deep because we want to
2678
                     * find transitions which indicate a conflict
2679
                     */
2680
0
        if (xmlFACompareAtoms(t1->atom, t2->atom, 1)) {
2681
                        /*
2682
                         * Treat equal counter transitions that couldn't be
2683
                         * eliminated as deterministic.
2684
                         */
2685
0
                        if ((t1->to != t2->to) ||
2686
0
                            (t1->counter == t2->counter) ||
2687
0
                            (!xmlFAEqualAtoms(t1->atom, t2->atom, deep)))
2688
0
                            ret = 0;
2689
      /* mark the transitions as non-deterministic ones */
2690
0
      t1->nd = 1;
2691
0
      t2->nd = 1;
2692
0
      last = t1;
2693
0
        }
2694
0
    } else {
2695
0
                    int res;
2696
2697
        /*
2698
         * do the closure in case of remaining specific
2699
         * epsilon transitions like choices or all
2700
         */
2701
0
        res = xmlFARecurseDeterminism(ctxt, ctxt->states[t2->to],
2702
0
              statenr, t1->to, t1->atom);
2703
0
                    xmlFAFinishRecurseDeterminism(ctxt, ctxt->states[t2->to]);
2704
        /* don't shortcut the computation so all non deterministic
2705
           transition get marked down
2706
        if (ret == 0)
2707
      return(0);
2708
         */
2709
0
        if (res == 0) {
2710
0
      t1->nd = 1;
2711
      /* t2->nd = 1; */
2712
0
      last = t1;
2713
0
                        ret = 0;
2714
0
        }
2715
0
    }
2716
0
      }
2717
      /* don't shortcut the computation so all non deterministic
2718
         transition get marked down
2719
      if (ret == 0)
2720
    break; */
2721
0
  }
2722
2723
  /*
2724
   * mark specifically the last non-deterministic transition
2725
   * from a state since there is no need to set-up rollback
2726
   * from it
2727
   */
2728
0
  if (last != NULL) {
2729
0
      last->nd = 2;
2730
0
  }
2731
2732
  /* don't shortcut the computation so all non deterministic
2733
     transition get marked down
2734
  if (ret == 0)
2735
      break; */
2736
0
    }
2737
2738
0
    ctxt->determinist = ret;
2739
0
    return(ret);
2740
0
}
2741
2742
/************************************************************************
2743
 *                  *
2744
 *  Routines to check input against transition atoms    *
2745
 *                  *
2746
 ************************************************************************/
2747
2748
static int
2749
xmlRegCheckCharacterRange(xmlRegAtomType type, int codepoint, int neg,
2750
0
                    int start, int end, const xmlChar *blockName) {
2751
0
    int ret = 0;
2752
2753
0
    switch (type) {
2754
0
        case XML_REGEXP_STRING:
2755
0
        case XML_REGEXP_SUBREG:
2756
0
        case XML_REGEXP_RANGES:
2757
0
        case XML_REGEXP_EPSILON:
2758
0
      return(-1);
2759
0
        case XML_REGEXP_ANYCHAR:
2760
0
      ret = ((codepoint != '\n') && (codepoint != '\r'));
2761
0
      break;
2762
0
        case XML_REGEXP_CHARVAL:
2763
0
      ret = ((codepoint >= start) && (codepoint <= end));
2764
0
      break;
2765
0
        case XML_REGEXP_NOTSPACE:
2766
0
      neg = !neg;
2767
            /* Falls through. */
2768
0
        case XML_REGEXP_ANYSPACE:
2769
0
      ret = ((codepoint == '\n') || (codepoint == '\r') ||
2770
0
       (codepoint == '\t') || (codepoint == ' '));
2771
0
      break;
2772
0
        case XML_REGEXP_NOTINITNAME:
2773
0
      neg = !neg;
2774
            /* Falls through. */
2775
0
        case XML_REGEXP_INITNAME:
2776
0
      ret = (IS_LETTER(codepoint) ||
2777
0
       (codepoint == '_') || (codepoint == ':'));
2778
0
      break;
2779
0
        case XML_REGEXP_NOTNAMECHAR:
2780
0
      neg = !neg;
2781
            /* Falls through. */
2782
0
        case XML_REGEXP_NAMECHAR:
2783
0
      ret = (IS_LETTER(codepoint) || IS_DIGIT(codepoint) ||
2784
0
       (codepoint == '.') || (codepoint == '-') ||
2785
0
       (codepoint == '_') || (codepoint == ':') ||
2786
0
       IS_COMBINING(codepoint) || IS_EXTENDER(codepoint));
2787
0
      break;
2788
0
        case XML_REGEXP_NOTDECIMAL:
2789
0
      neg = !neg;
2790
            /* Falls through. */
2791
0
        case XML_REGEXP_DECIMAL:
2792
0
      ret = xmlUCSIsCatNd(codepoint);
2793
0
      break;
2794
0
        case XML_REGEXP_REALCHAR:
2795
0
      neg = !neg;
2796
            /* Falls through. */
2797
0
        case XML_REGEXP_NOTREALCHAR:
2798
0
      ret = xmlUCSIsCatP(codepoint);
2799
0
      if (ret == 0)
2800
0
    ret = xmlUCSIsCatZ(codepoint);
2801
0
      if (ret == 0)
2802
0
    ret = xmlUCSIsCatC(codepoint);
2803
0
      break;
2804
0
        case XML_REGEXP_LETTER:
2805
0
      ret = xmlUCSIsCatL(codepoint);
2806
0
      break;
2807
0
        case XML_REGEXP_LETTER_UPPERCASE:
2808
0
      ret = xmlUCSIsCatLu(codepoint);
2809
0
      break;
2810
0
        case XML_REGEXP_LETTER_LOWERCASE:
2811
0
      ret = xmlUCSIsCatLl(codepoint);
2812
0
      break;
2813
0
        case XML_REGEXP_LETTER_TITLECASE:
2814
0
      ret = xmlUCSIsCatLt(codepoint);
2815
0
      break;
2816
0
        case XML_REGEXP_LETTER_MODIFIER:
2817
0
      ret = xmlUCSIsCatLm(codepoint);
2818
0
      break;
2819
0
        case XML_REGEXP_LETTER_OTHERS:
2820
0
      ret = xmlUCSIsCatLo(codepoint);
2821
0
      break;
2822
0
        case XML_REGEXP_MARK:
2823
0
      ret = xmlUCSIsCatM(codepoint);
2824
0
      break;
2825
0
        case XML_REGEXP_MARK_NONSPACING:
2826
0
      ret = xmlUCSIsCatMn(codepoint);
2827
0
      break;
2828
0
        case XML_REGEXP_MARK_SPACECOMBINING:
2829
0
      ret = xmlUCSIsCatMc(codepoint);
2830
0
      break;
2831
0
        case XML_REGEXP_MARK_ENCLOSING:
2832
0
      ret = xmlUCSIsCatMe(codepoint);
2833
0
      break;
2834
0
        case XML_REGEXP_NUMBER:
2835
0
      ret = xmlUCSIsCatN(codepoint);
2836
0
      break;
2837
0
        case XML_REGEXP_NUMBER_DECIMAL:
2838
0
      ret = xmlUCSIsCatNd(codepoint);
2839
0
      break;
2840
0
        case XML_REGEXP_NUMBER_LETTER:
2841
0
      ret = xmlUCSIsCatNl(codepoint);
2842
0
      break;
2843
0
        case XML_REGEXP_NUMBER_OTHERS:
2844
0
      ret = xmlUCSIsCatNo(codepoint);
2845
0
      break;
2846
0
        case XML_REGEXP_PUNCT:
2847
0
      ret = xmlUCSIsCatP(codepoint);
2848
0
      break;
2849
0
        case XML_REGEXP_PUNCT_CONNECTOR:
2850
0
      ret = xmlUCSIsCatPc(codepoint);
2851
0
      break;
2852
0
        case XML_REGEXP_PUNCT_DASH:
2853
0
      ret = xmlUCSIsCatPd(codepoint);
2854
0
      break;
2855
0
        case XML_REGEXP_PUNCT_OPEN:
2856
0
      ret = xmlUCSIsCatPs(codepoint);
2857
0
      break;
2858
0
        case XML_REGEXP_PUNCT_CLOSE:
2859
0
      ret = xmlUCSIsCatPe(codepoint);
2860
0
      break;
2861
0
        case XML_REGEXP_PUNCT_INITQUOTE:
2862
0
      ret = xmlUCSIsCatPi(codepoint);
2863
0
      break;
2864
0
        case XML_REGEXP_PUNCT_FINQUOTE:
2865
0
      ret = xmlUCSIsCatPf(codepoint);
2866
0
      break;
2867
0
        case XML_REGEXP_PUNCT_OTHERS:
2868
0
      ret = xmlUCSIsCatPo(codepoint);
2869
0
      break;
2870
0
        case XML_REGEXP_SEPAR:
2871
0
      ret = xmlUCSIsCatZ(codepoint);
2872
0
      break;
2873
0
        case XML_REGEXP_SEPAR_SPACE:
2874
0
      ret = xmlUCSIsCatZs(codepoint);
2875
0
      break;
2876
0
        case XML_REGEXP_SEPAR_LINE:
2877
0
      ret = xmlUCSIsCatZl(codepoint);
2878
0
      break;
2879
0
        case XML_REGEXP_SEPAR_PARA:
2880
0
      ret = xmlUCSIsCatZp(codepoint);
2881
0
      break;
2882
0
        case XML_REGEXP_SYMBOL:
2883
0
      ret = xmlUCSIsCatS(codepoint);
2884
0
      break;
2885
0
        case XML_REGEXP_SYMBOL_MATH:
2886
0
      ret = xmlUCSIsCatSm(codepoint);
2887
0
      break;
2888
0
        case XML_REGEXP_SYMBOL_CURRENCY:
2889
0
      ret = xmlUCSIsCatSc(codepoint);
2890
0
      break;
2891
0
        case XML_REGEXP_SYMBOL_MODIFIER:
2892
0
      ret = xmlUCSIsCatSk(codepoint);
2893
0
      break;
2894
0
        case XML_REGEXP_SYMBOL_OTHERS:
2895
0
      ret = xmlUCSIsCatSo(codepoint);
2896
0
      break;
2897
0
        case XML_REGEXP_OTHER:
2898
0
      ret = xmlUCSIsCatC(codepoint);
2899
0
      break;
2900
0
        case XML_REGEXP_OTHER_CONTROL:
2901
0
      ret = xmlUCSIsCatCc(codepoint);
2902
0
      break;
2903
0
        case XML_REGEXP_OTHER_FORMAT:
2904
0
      ret = xmlUCSIsCatCf(codepoint);
2905
0
      break;
2906
0
        case XML_REGEXP_OTHER_PRIVATE:
2907
0
      ret = xmlUCSIsCatCo(codepoint);
2908
0
      break;
2909
0
        case XML_REGEXP_OTHER_NA:
2910
      /* ret = xmlUCSIsCatCn(codepoint); */
2911
      /* Seems it doesn't exist anymore in recent Unicode releases */
2912
0
      ret = 0;
2913
0
      break;
2914
0
        case XML_REGEXP_BLOCK_NAME:
2915
0
      ret = xmlUCSIsBlock(codepoint, (const char *) blockName);
2916
0
      break;
2917
0
    }
2918
0
    if (neg)
2919
0
  return(!ret);
2920
0
    return(ret);
2921
0
}
2922
2923
static int
2924
0
xmlRegCheckCharacter(xmlRegAtomPtr atom, int codepoint) {
2925
0
    int i, ret = 0;
2926
0
    xmlRegRangePtr range;
2927
2928
0
    if ((atom == NULL) || (!IS_CHAR(codepoint)))
2929
0
  return(-1);
2930
2931
0
    switch (atom->type) {
2932
0
        case XML_REGEXP_SUBREG:
2933
0
        case XML_REGEXP_EPSILON:
2934
0
      return(-1);
2935
0
        case XML_REGEXP_CHARVAL:
2936
0
            return(codepoint == atom->codepoint);
2937
0
        case XML_REGEXP_RANGES: {
2938
0
      int accept = 0;
2939
2940
0
      for (i = 0;i < atom->nbRanges;i++) {
2941
0
    range = atom->ranges[i];
2942
0
    if (range->neg == 2) {
2943
0
        ret = xmlRegCheckCharacterRange(range->type, codepoint,
2944
0
            0, range->start, range->end,
2945
0
            range->blockName);
2946
0
        if (ret != 0)
2947
0
      return(0); /* excluded char */
2948
0
    } else if (range->neg) {
2949
0
        ret = xmlRegCheckCharacterRange(range->type, codepoint,
2950
0
            0, range->start, range->end,
2951
0
            range->blockName);
2952
0
        if (ret == 0)
2953
0
            accept = 1;
2954
0
        else
2955
0
            return(0);
2956
0
    } else {
2957
0
        ret = xmlRegCheckCharacterRange(range->type, codepoint,
2958
0
            0, range->start, range->end,
2959
0
            range->blockName);
2960
0
        if (ret != 0)
2961
0
      accept = 1; /* might still be excluded */
2962
0
    }
2963
0
      }
2964
0
      return(accept);
2965
0
  }
2966
0
        case XML_REGEXP_STRING:
2967
0
      printf("TODO: XML_REGEXP_STRING\n");
2968
0
      return(-1);
2969
0
        case XML_REGEXP_ANYCHAR:
2970
0
        case XML_REGEXP_ANYSPACE:
2971
0
        case XML_REGEXP_NOTSPACE:
2972
0
        case XML_REGEXP_INITNAME:
2973
0
        case XML_REGEXP_NOTINITNAME:
2974
0
        case XML_REGEXP_NAMECHAR:
2975
0
        case XML_REGEXP_NOTNAMECHAR:
2976
0
        case XML_REGEXP_DECIMAL:
2977
0
        case XML_REGEXP_NOTDECIMAL:
2978
0
        case XML_REGEXP_REALCHAR:
2979
0
        case XML_REGEXP_NOTREALCHAR:
2980
0
        case XML_REGEXP_LETTER:
2981
0
        case XML_REGEXP_LETTER_UPPERCASE:
2982
0
        case XML_REGEXP_LETTER_LOWERCASE:
2983
0
        case XML_REGEXP_LETTER_TITLECASE:
2984
0
        case XML_REGEXP_LETTER_MODIFIER:
2985
0
        case XML_REGEXP_LETTER_OTHERS:
2986
0
        case XML_REGEXP_MARK:
2987
0
        case XML_REGEXP_MARK_NONSPACING:
2988
0
        case XML_REGEXP_MARK_SPACECOMBINING:
2989
0
        case XML_REGEXP_MARK_ENCLOSING:
2990
0
        case XML_REGEXP_NUMBER:
2991
0
        case XML_REGEXP_NUMBER_DECIMAL:
2992
0
        case XML_REGEXP_NUMBER_LETTER:
2993
0
        case XML_REGEXP_NUMBER_OTHERS:
2994
0
        case XML_REGEXP_PUNCT:
2995
0
        case XML_REGEXP_PUNCT_CONNECTOR:
2996
0
        case XML_REGEXP_PUNCT_DASH:
2997
0
        case XML_REGEXP_PUNCT_OPEN:
2998
0
        case XML_REGEXP_PUNCT_CLOSE:
2999
0
        case XML_REGEXP_PUNCT_INITQUOTE:
3000
0
        case XML_REGEXP_PUNCT_FINQUOTE:
3001
0
        case XML_REGEXP_PUNCT_OTHERS:
3002
0
        case XML_REGEXP_SEPAR:
3003
0
        case XML_REGEXP_SEPAR_SPACE:
3004
0
        case XML_REGEXP_SEPAR_LINE:
3005
0
        case XML_REGEXP_SEPAR_PARA:
3006
0
        case XML_REGEXP_SYMBOL:
3007
0
        case XML_REGEXP_SYMBOL_MATH:
3008
0
        case XML_REGEXP_SYMBOL_CURRENCY:
3009
0
        case XML_REGEXP_SYMBOL_MODIFIER:
3010
0
        case XML_REGEXP_SYMBOL_OTHERS:
3011
0
        case XML_REGEXP_OTHER:
3012
0
        case XML_REGEXP_OTHER_CONTROL:
3013
0
        case XML_REGEXP_OTHER_FORMAT:
3014
0
        case XML_REGEXP_OTHER_PRIVATE:
3015
0
        case XML_REGEXP_OTHER_NA:
3016
0
  case XML_REGEXP_BLOCK_NAME:
3017
0
      ret = xmlRegCheckCharacterRange(atom->type, codepoint, 0, 0, 0,
3018
0
                                (const xmlChar *)atom->valuep);
3019
0
      if (atom->neg)
3020
0
    ret = !ret;
3021
0
      break;
3022
0
    }
3023
0
    return(ret);
3024
0
}
3025
3026
/************************************************************************
3027
 *                  *
3028
 *  Saving and restoring state of an execution context    *
3029
 *                  *
3030
 ************************************************************************/
3031
3032
static void
3033
0
xmlFARegExecSave(xmlRegExecCtxtPtr exec) {
3034
0
#ifdef MAX_PUSH
3035
0
    if (exec->nbPush > MAX_PUSH) {
3036
0
        exec->status = XML_REGEXP_INTERNAL_LIMIT;
3037
0
        return;
3038
0
    }
3039
0
    exec->nbPush++;
3040
0
#endif
3041
3042
0
    if (exec->maxRollbacks == 0) {
3043
0
  exec->maxRollbacks = 4;
3044
0
  exec->rollbacks = (xmlRegExecRollback *) xmlMalloc(exec->maxRollbacks *
3045
0
                                 sizeof(xmlRegExecRollback));
3046
0
  if (exec->rollbacks == NULL) {
3047
0
      exec->maxRollbacks = 0;
3048
0
            exec->status = XML_REGEXP_OUT_OF_MEMORY;
3049
0
      return;
3050
0
  }
3051
0
  memset(exec->rollbacks, 0,
3052
0
         exec->maxRollbacks * sizeof(xmlRegExecRollback));
3053
0
    } else if (exec->nbRollbacks >= exec->maxRollbacks) {
3054
0
  xmlRegExecRollback *tmp;
3055
0
  int len = exec->maxRollbacks;
3056
3057
0
  exec->maxRollbacks *= 2;
3058
0
  tmp = (xmlRegExecRollback *) xmlRealloc(exec->rollbacks,
3059
0
      exec->maxRollbacks * sizeof(xmlRegExecRollback));
3060
0
  if (tmp == NULL) {
3061
0
      exec->maxRollbacks /= 2;
3062
0
            exec->status = XML_REGEXP_OUT_OF_MEMORY;
3063
0
      return;
3064
0
  }
3065
0
  exec->rollbacks = tmp;
3066
0
  tmp = &exec->rollbacks[len];
3067
0
  memset(tmp, 0, (exec->maxRollbacks - len) * sizeof(xmlRegExecRollback));
3068
0
    }
3069
0
    exec->rollbacks[exec->nbRollbacks].state = exec->state;
3070
0
    exec->rollbacks[exec->nbRollbacks].index = exec->index;
3071
0
    exec->rollbacks[exec->nbRollbacks].nextbranch = exec->transno + 1;
3072
0
    if (exec->comp->nbCounters > 0) {
3073
0
  if (exec->rollbacks[exec->nbRollbacks].counts == NULL) {
3074
0
      exec->rollbacks[exec->nbRollbacks].counts = (int *)
3075
0
    xmlMalloc(exec->comp->nbCounters * sizeof(int));
3076
0
      if (exec->rollbacks[exec->nbRollbacks].counts == NULL) {
3077
0
    exec->status = XML_REGEXP_OUT_OF_MEMORY;
3078
0
    return;
3079
0
      }
3080
0
  }
3081
0
  memcpy(exec->rollbacks[exec->nbRollbacks].counts, exec->counts,
3082
0
         exec->comp->nbCounters * sizeof(int));
3083
0
    }
3084
0
    exec->nbRollbacks++;
3085
0
}
3086
3087
static void
3088
0
xmlFARegExecRollBack(xmlRegExecCtxtPtr exec) {
3089
0
    if (exec->status != XML_REGEXP_OK)
3090
0
        return;
3091
0
    if (exec->nbRollbacks <= 0) {
3092
0
  exec->status = XML_REGEXP_NOT_FOUND;
3093
0
  return;
3094
0
    }
3095
0
    exec->nbRollbacks--;
3096
0
    exec->state = exec->rollbacks[exec->nbRollbacks].state;
3097
0
    exec->index = exec->rollbacks[exec->nbRollbacks].index;
3098
0
    exec->transno = exec->rollbacks[exec->nbRollbacks].nextbranch;
3099
0
    if (exec->comp->nbCounters > 0) {
3100
0
  if (exec->rollbacks[exec->nbRollbacks].counts == NULL) {
3101
0
      fprintf(stderr, "exec save: allocation failed");
3102
0
      exec->status = XML_REGEXP_INTERNAL_ERROR;
3103
0
      return;
3104
0
  }
3105
0
  if (exec->counts) {
3106
0
      memcpy(exec->counts, exec->rollbacks[exec->nbRollbacks].counts,
3107
0
         exec->comp->nbCounters * sizeof(int));
3108
0
  }
3109
0
    }
3110
0
}
3111
3112
/************************************************************************
3113
 *                  *
3114
 *  Verifier, running an input against a compiled regexp    *
3115
 *                  *
3116
 ************************************************************************/
3117
3118
static int
3119
0
xmlFARegExec(xmlRegexpPtr comp, const xmlChar *content) {
3120
0
    xmlRegExecCtxt execval;
3121
0
    xmlRegExecCtxtPtr exec = &execval;
3122
0
    int ret, codepoint = 0, len, deter;
3123
3124
0
    exec->inputString = content;
3125
0
    exec->index = 0;
3126
0
    exec->nbPush = 0;
3127
0
    exec->determinist = 1;
3128
0
    exec->maxRollbacks = 0;
3129
0
    exec->nbRollbacks = 0;
3130
0
    exec->rollbacks = NULL;
3131
0
    exec->status = XML_REGEXP_OK;
3132
0
    exec->comp = comp;
3133
0
    exec->state = comp->states[0];
3134
0
    exec->transno = 0;
3135
0
    exec->transcount = 0;
3136
0
    exec->inputStack = NULL;
3137
0
    exec->inputStackMax = 0;
3138
0
    if (comp->nbCounters > 0) {
3139
0
  exec->counts = (int *) xmlMalloc(comp->nbCounters * sizeof(int));
3140
0
  if (exec->counts == NULL) {
3141
0
      return(XML_REGEXP_OUT_OF_MEMORY);
3142
0
  }
3143
0
        memset(exec->counts, 0, comp->nbCounters * sizeof(int));
3144
0
    } else
3145
0
  exec->counts = NULL;
3146
0
    while ((exec->status == XML_REGEXP_OK) && (exec->state != NULL) &&
3147
0
     ((exec->inputString[exec->index] != 0) ||
3148
0
      ((exec->state != NULL) &&
3149
0
       (exec->state->type != XML_REGEXP_FINAL_STATE)))) {
3150
0
  xmlRegTransPtr trans;
3151
0
  xmlRegAtomPtr atom;
3152
3153
  /*
3154
   * If end of input on non-terminal state, rollback, however we may
3155
   * still have epsilon like transition for counted transitions
3156
   * on counters, in that case don't break too early.  Additionally,
3157
   * if we are working on a range like "AB{0,2}", where B is not present,
3158
   * we don't want to break.
3159
   */
3160
0
  len = 1;
3161
0
  if ((exec->inputString[exec->index] == 0) && (exec->counts == NULL)) {
3162
      /*
3163
       * if there is a transition, we must check if
3164
       *  atom allows minOccurs of 0
3165
       */
3166
0
      if (exec->transno < exec->state->nbTrans) {
3167
0
          trans = &exec->state->trans[exec->transno];
3168
0
    if (trans->to >=0) {
3169
0
        atom = trans->atom;
3170
0
        if (!((atom->min == 0) && (atom->max > 0)))
3171
0
            goto rollback;
3172
0
    }
3173
0
      } else
3174
0
          goto rollback;
3175
0
  }
3176
3177
0
  exec->transcount = 0;
3178
0
  for (;exec->transno < exec->state->nbTrans;exec->transno++) {
3179
0
      trans = &exec->state->trans[exec->transno];
3180
0
      if (trans->to < 0)
3181
0
    continue;
3182
0
      atom = trans->atom;
3183
0
      ret = 0;
3184
0
      deter = 1;
3185
0
      if (trans->count >= 0) {
3186
0
    int count;
3187
0
    xmlRegCounterPtr counter;
3188
3189
0
    if (exec->counts == NULL) {
3190
0
        exec->status = XML_REGEXP_INTERNAL_ERROR;
3191
0
        goto error;
3192
0
    }
3193
    /*
3194
     * A counted transition.
3195
     */
3196
3197
0
    count = exec->counts[trans->count];
3198
0
    counter = &exec->comp->counters[trans->count];
3199
0
    ret = ((count >= counter->min) && (count <= counter->max));
3200
0
    if ((ret) && (counter->min != counter->max))
3201
0
        deter = 0;
3202
0
      } else if (atom == NULL) {
3203
0
    fprintf(stderr, "epsilon transition left at runtime\n");
3204
0
    exec->status = XML_REGEXP_INTERNAL_ERROR;
3205
0
    break;
3206
0
      } else if (exec->inputString[exec->index] != 0) {
3207
0
                len = 4;
3208
0
                codepoint = xmlGetUTF8Char(&exec->inputString[exec->index],
3209
0
                                           &len);
3210
0
                if (codepoint < 0) {
3211
0
                    exec->status = XML_REGEXP_INVALID_UTF8;
3212
0
                    goto error;
3213
0
                }
3214
0
    ret = xmlRegCheckCharacter(atom, codepoint);
3215
0
    if ((ret == 1) && (atom->min >= 0) && (atom->max > 0)) {
3216
0
        xmlRegStatePtr to = comp->states[trans->to];
3217
3218
        /*
3219
         * this is a multiple input sequence
3220
         * If there is a counter associated increment it now.
3221
         * do not increment if the counter is already over the
3222
         * maximum limit in which case get to next transition
3223
         */
3224
0
        if (trans->counter >= 0) {
3225
0
      xmlRegCounterPtr counter;
3226
3227
0
      if ((exec->counts == NULL) ||
3228
0
          (exec->comp == NULL) ||
3229
0
          (exec->comp->counters == NULL)) {
3230
0
          exec->status = XML_REGEXP_INTERNAL_ERROR;
3231
0
          goto error;
3232
0
      }
3233
0
      counter = &exec->comp->counters[trans->counter];
3234
0
      if (exec->counts[trans->counter] >= counter->max)
3235
0
          continue; /* for loop on transitions */
3236
0
                    }
3237
                    /* Save before incrementing */
3238
0
        if (exec->state->nbTrans > exec->transno + 1) {
3239
0
      xmlFARegExecSave(exec);
3240
0
                        if (exec->status != XML_REGEXP_OK)
3241
0
                            goto error;
3242
0
        }
3243
0
        if (trans->counter >= 0) {
3244
0
      exec->counts[trans->counter]++;
3245
0
        }
3246
0
        exec->transcount = 1;
3247
0
        do {
3248
      /*
3249
       * Try to progress as much as possible on the input
3250
       */
3251
0
      if (exec->transcount == atom->max) {
3252
0
          break;
3253
0
      }
3254
0
      exec->index += len;
3255
      /*
3256
       * End of input: stop here
3257
       */
3258
0
      if (exec->inputString[exec->index] == 0) {
3259
0
          exec->index -= len;
3260
0
          break;
3261
0
      }
3262
0
      if (exec->transcount >= atom->min) {
3263
0
          int transno = exec->transno;
3264
0
          xmlRegStatePtr state = exec->state;
3265
3266
          /*
3267
           * The transition is acceptable save it
3268
           */
3269
0
          exec->transno = -1; /* trick */
3270
0
          exec->state = to;
3271
0
          xmlFARegExecSave(exec);
3272
0
                            if (exec->status != XML_REGEXP_OK)
3273
0
                                goto error;
3274
0
          exec->transno = transno;
3275
0
          exec->state = state;
3276
0
      }
3277
0
                        len = 4;
3278
0
                        codepoint = xmlGetUTF8Char(
3279
0
                                &exec->inputString[exec->index], &len);
3280
0
                        if (codepoint < 0) {
3281
0
                            exec->status = XML_REGEXP_INVALID_UTF8;
3282
0
                            goto error;
3283
0
                        }
3284
0
      ret = xmlRegCheckCharacter(atom, codepoint);
3285
0
      exec->transcount++;
3286
0
        } while (ret == 1);
3287
0
        if (exec->transcount < atom->min)
3288
0
      ret = 0;
3289
3290
        /*
3291
         * If the last check failed but one transition was found
3292
         * possible, rollback
3293
         */
3294
0
        if (ret < 0)
3295
0
      ret = 0;
3296
0
        if (ret == 0) {
3297
0
      goto rollback;
3298
0
        }
3299
0
        if (trans->counter >= 0) {
3300
0
      if (exec->counts == NULL) {
3301
0
          exec->status = XML_REGEXP_INTERNAL_ERROR;
3302
0
          goto error;
3303
0
      }
3304
0
      exec->counts[trans->counter]--;
3305
0
        }
3306
0
    } else if ((ret == 0) && (atom->min == 0) && (atom->max > 0)) {
3307
        /*
3308
         * we don't match on the codepoint, but minOccurs of 0
3309
         * says that's ok.  Setting len to 0 inhibits stepping
3310
         * over the codepoint.
3311
         */
3312
0
        exec->transcount = 1;
3313
0
        len = 0;
3314
0
        ret = 1;
3315
0
    }
3316
0
      } else if ((atom->min == 0) && (atom->max > 0)) {
3317
          /* another spot to match when minOccurs is 0 */
3318
0
    exec->transcount = 1;
3319
0
    len = 0;
3320
0
    ret = 1;
3321
0
      }
3322
0
      if (ret == 1) {
3323
0
    if ((trans->nd == 1) ||
3324
0
        ((trans->count >= 0) && (deter == 0) &&
3325
0
         (exec->state->nbTrans > exec->transno + 1))) {
3326
0
        xmlFARegExecSave(exec);
3327
0
                    if (exec->status != XML_REGEXP_OK)
3328
0
                        goto error;
3329
0
    }
3330
0
    if (trans->counter >= 0) {
3331
0
        xmlRegCounterPtr counter;
3332
3333
                    /* make sure we don't go over the counter maximum value */
3334
0
        if ((exec->counts == NULL) ||
3335
0
      (exec->comp == NULL) ||
3336
0
      (exec->comp->counters == NULL)) {
3337
0
      exec->status = XML_REGEXP_INTERNAL_ERROR;
3338
0
      goto error;
3339
0
        }
3340
0
        counter = &exec->comp->counters[trans->counter];
3341
0
        if (exec->counts[trans->counter] >= counter->max)
3342
0
      continue; /* for loop on transitions */
3343
0
        exec->counts[trans->counter]++;
3344
0
    }
3345
0
    if ((trans->count >= 0) &&
3346
0
        (trans->count < REGEXP_ALL_COUNTER)) {
3347
0
        if (exec->counts == NULL) {
3348
0
            exec->status = XML_REGEXP_INTERNAL_ERROR;
3349
0
      goto error;
3350
0
        }
3351
0
        exec->counts[trans->count] = 0;
3352
0
    }
3353
0
    exec->state = comp->states[trans->to];
3354
0
    exec->transno = 0;
3355
0
    if (trans->atom != NULL) {
3356
0
        exec->index += len;
3357
0
    }
3358
0
    goto progress;
3359
0
      } else if (ret < 0) {
3360
0
    exec->status = XML_REGEXP_INTERNAL_ERROR;
3361
0
    break;
3362
0
      }
3363
0
  }
3364
0
  if ((exec->transno != 0) || (exec->state->nbTrans == 0)) {
3365
0
rollback:
3366
      /*
3367
       * Failed to find a way out
3368
       */
3369
0
      exec->determinist = 0;
3370
0
      xmlFARegExecRollBack(exec);
3371
0
  }
3372
0
progress:
3373
0
  continue;
3374
0
    }
3375
0
error:
3376
0
    if (exec->rollbacks != NULL) {
3377
0
  if (exec->counts != NULL) {
3378
0
      int i;
3379
3380
0
      for (i = 0;i < exec->maxRollbacks;i++)
3381
0
    if (exec->rollbacks[i].counts != NULL)
3382
0
        xmlFree(exec->rollbacks[i].counts);
3383
0
  }
3384
0
  xmlFree(exec->rollbacks);
3385
0
    }
3386
0
    if (exec->state == NULL)
3387
0
        return(XML_REGEXP_INTERNAL_ERROR);
3388
0
    if (exec->counts != NULL)
3389
0
  xmlFree(exec->counts);
3390
0
    if (exec->status == XML_REGEXP_OK)
3391
0
  return(1);
3392
0
    if (exec->status == XML_REGEXP_NOT_FOUND)
3393
0
  return(0);
3394
0
    return(exec->status);
3395
0
}
3396
3397
/************************************************************************
3398
 *                  *
3399
 *  Progressive interface to the verifier one atom at a time  *
3400
 *                  *
3401
 ************************************************************************/
3402
3403
/**
3404
 * xmlRegNewExecCtxt:
3405
 * @comp: a precompiled regular expression
3406
 * @callback: a callback function used for handling progresses in the
3407
 *            automata matching phase
3408
 * @data: the context data associated to the callback in this context
3409
 *
3410
 * Build a context used for progressive evaluation of a regexp.
3411
 *
3412
 * Returns the new context
3413
 */
3414
xmlRegExecCtxtPtr
3415
0
xmlRegNewExecCtxt(xmlRegexpPtr comp, xmlRegExecCallbacks callback, void *data) {
3416
0
    xmlRegExecCtxtPtr exec;
3417
3418
0
    if (comp == NULL)
3419
0
  return(NULL);
3420
0
    if ((comp->compact == NULL) && (comp->states == NULL))
3421
0
        return(NULL);
3422
0
    exec = (xmlRegExecCtxtPtr) xmlMalloc(sizeof(xmlRegExecCtxt));
3423
0
    if (exec == NULL)
3424
0
  return(NULL);
3425
0
    memset(exec, 0, sizeof(xmlRegExecCtxt));
3426
0
    exec->inputString = NULL;
3427
0
    exec->index = 0;
3428
0
    exec->determinist = 1;
3429
0
    exec->maxRollbacks = 0;
3430
0
    exec->nbRollbacks = 0;
3431
0
    exec->rollbacks = NULL;
3432
0
    exec->status = XML_REGEXP_OK;
3433
0
    exec->comp = comp;
3434
0
    if (comp->compact == NULL)
3435
0
  exec->state = comp->states[0];
3436
0
    exec->transno = 0;
3437
0
    exec->transcount = 0;
3438
0
    exec->callback = callback;
3439
0
    exec->data = data;
3440
0
    if (comp->nbCounters > 0) {
3441
        /*
3442
   * For error handling, exec->counts is allocated twice the size
3443
   * the second half is used to store the data in case of rollback
3444
   */
3445
0
  exec->counts = (int *) xmlMalloc(comp->nbCounters * sizeof(int)
3446
0
                                   * 2);
3447
0
  if (exec->counts == NULL) {
3448
0
      xmlFree(exec);
3449
0
      return(NULL);
3450
0
  }
3451
0
        memset(exec->counts, 0, comp->nbCounters * sizeof(int) * 2);
3452
0
  exec->errCounts = &exec->counts[comp->nbCounters];
3453
0
    } else {
3454
0
  exec->counts = NULL;
3455
0
  exec->errCounts = NULL;
3456
0
    }
3457
0
    exec->inputStackMax = 0;
3458
0
    exec->inputStackNr = 0;
3459
0
    exec->inputStack = NULL;
3460
0
    exec->errStateNo = -1;
3461
0
    exec->errString = NULL;
3462
0
    exec->nbPush = 0;
3463
0
    return(exec);
3464
0
}
3465
3466
/**
3467
 * xmlRegFreeExecCtxt:
3468
 * @exec: a regular expression evaluation context
3469
 *
3470
 * Free the structures associated to a regular expression evaluation context.
3471
 */
3472
void
3473
0
xmlRegFreeExecCtxt(xmlRegExecCtxtPtr exec) {
3474
0
    if (exec == NULL)
3475
0
  return;
3476
3477
0
    if (exec->rollbacks != NULL) {
3478
0
  if (exec->counts != NULL) {
3479
0
      int i;
3480
3481
0
      for (i = 0;i < exec->maxRollbacks;i++)
3482
0
    if (exec->rollbacks[i].counts != NULL)
3483
0
        xmlFree(exec->rollbacks[i].counts);
3484
0
  }
3485
0
  xmlFree(exec->rollbacks);
3486
0
    }
3487
0
    if (exec->counts != NULL)
3488
0
  xmlFree(exec->counts);
3489
0
    if (exec->inputStack != NULL) {
3490
0
  int i;
3491
3492
0
  for (i = 0;i < exec->inputStackNr;i++) {
3493
0
      if (exec->inputStack[i].value != NULL)
3494
0
    xmlFree(exec->inputStack[i].value);
3495
0
  }
3496
0
  xmlFree(exec->inputStack);
3497
0
    }
3498
0
    if (exec->errString != NULL)
3499
0
        xmlFree(exec->errString);
3500
0
    xmlFree(exec);
3501
0
}
3502
3503
static int
3504
0
xmlRegExecSetErrString(xmlRegExecCtxtPtr exec, const xmlChar *value) {
3505
0
    if (exec->errString != NULL)
3506
0
        xmlFree(exec->errString);
3507
0
    if (value == NULL) {
3508
0
        exec->errString = NULL;
3509
0
    } else {
3510
0
        exec->errString = xmlStrdup(value);
3511
0
        if (exec->errString == NULL) {
3512
0
            exec->status = XML_REGEXP_OUT_OF_MEMORY;
3513
0
            return(-1);
3514
0
        }
3515
0
    }
3516
0
    return(0);
3517
0
}
3518
3519
static void
3520
xmlFARegExecSaveInputString(xmlRegExecCtxtPtr exec, const xmlChar *value,
3521
0
                      void *data) {
3522
0
    if (exec->inputStackMax == 0) {
3523
0
  exec->inputStackMax = 4;
3524
0
  exec->inputStack = (xmlRegInputTokenPtr)
3525
0
      xmlMalloc(exec->inputStackMax * sizeof(xmlRegInputToken));
3526
0
  if (exec->inputStack == NULL) {
3527
0
      exec->inputStackMax = 0;
3528
0
            exec->status = XML_REGEXP_OUT_OF_MEMORY;
3529
0
      return;
3530
0
  }
3531
0
    } else if (exec->inputStackNr + 1 >= exec->inputStackMax) {
3532
0
  xmlRegInputTokenPtr tmp;
3533
3534
0
  exec->inputStackMax *= 2;
3535
0
  tmp = (xmlRegInputTokenPtr) xmlRealloc(exec->inputStack,
3536
0
      exec->inputStackMax * sizeof(xmlRegInputToken));
3537
0
  if (tmp == NULL) {
3538
0
      exec->inputStackMax /= 2;
3539
0
            exec->status = XML_REGEXP_OUT_OF_MEMORY;
3540
0
      return;
3541
0
  }
3542
0
  exec->inputStack = tmp;
3543
0
    }
3544
0
    if (value == NULL) {
3545
0
        exec->inputStack[exec->inputStackNr].value = NULL;
3546
0
    } else {
3547
0
        exec->inputStack[exec->inputStackNr].value = xmlStrdup(value);
3548
0
        if (exec->inputStack[exec->inputStackNr].value == NULL) {
3549
0
            exec->status = XML_REGEXP_OUT_OF_MEMORY;
3550
0
            return;
3551
0
        }
3552
0
    }
3553
0
    exec->inputStack[exec->inputStackNr].data = data;
3554
0
    exec->inputStackNr++;
3555
0
    exec->inputStack[exec->inputStackNr].value = NULL;
3556
0
    exec->inputStack[exec->inputStackNr].data = NULL;
3557
0
}
3558
3559
/**
3560
 * xmlRegStrEqualWildcard:
3561
 * @expStr:  the string to be evaluated
3562
 * @valStr:  the validation string
3563
 *
3564
 * Checks if both strings are equal or have the same content. "*"
3565
 * can be used as a wildcard in @valStr; "|" is used as a separator of
3566
 * substrings in both @expStr and @valStr.
3567
 *
3568
 * Returns 1 if the comparison is satisfied and the number of substrings
3569
 * is equal, 0 otherwise.
3570
 */
3571
3572
static int
3573
0
xmlRegStrEqualWildcard(const xmlChar *expStr, const xmlChar *valStr) {
3574
0
    if (expStr == valStr) return(1);
3575
0
    if (expStr == NULL) return(0);
3576
0
    if (valStr == NULL) return(0);
3577
0
    do {
3578
  /*
3579
  * Eval if we have a wildcard for the current item.
3580
  */
3581
0
        if (*expStr != *valStr) {
3582
      /* if one of them starts with a wildcard make valStr be it */
3583
0
      if (*valStr == '*') {
3584
0
          const xmlChar *tmp;
3585
3586
0
    tmp = valStr;
3587
0
    valStr = expStr;
3588
0
    expStr = tmp;
3589
0
      }
3590
0
      if ((*valStr != 0) && (*expStr != 0) && (*expStr++ == '*')) {
3591
0
    do {
3592
0
        if (*valStr == XML_REG_STRING_SEPARATOR)
3593
0
      break;
3594
0
        valStr++;
3595
0
    } while (*valStr != 0);
3596
0
    continue;
3597
0
      } else
3598
0
    return(0);
3599
0
  }
3600
0
  expStr++;
3601
0
  valStr++;
3602
0
    } while (*valStr != 0);
3603
0
    if (*expStr != 0)
3604
0
  return (0);
3605
0
    else
3606
0
  return (1);
3607
0
}
3608
3609
/**
3610
 * xmlRegCompactPushString:
3611
 * @exec: a regexp execution context
3612
 * @comp:  the precompiled exec with a compact table
3613
 * @value: a string token input
3614
 * @data: data associated to the token to reuse in callbacks
3615
 *
3616
 * Push one input token in the execution context
3617
 *
3618
 * Returns: 1 if the regexp reached a final state, 0 if non-final, and
3619
 *     a negative value in case of error.
3620
 */
3621
static int
3622
xmlRegCompactPushString(xmlRegExecCtxtPtr exec,
3623
                  xmlRegexpPtr comp,
3624
                  const xmlChar *value,
3625
0
                  void *data) {
3626
0
    int state = exec->index;
3627
0
    int i, target;
3628
3629
0
    if ((comp == NULL) || (comp->compact == NULL) || (comp->stringMap == NULL))
3630
0
  return(-1);
3631
3632
0
    if (value == NULL) {
3633
  /*
3634
   * are we at a final state ?
3635
   */
3636
0
  if (comp->compact[state * (comp->nbstrings + 1)] ==
3637
0
            XML_REGEXP_FINAL_STATE)
3638
0
      return(1);
3639
0
  return(0);
3640
0
    }
3641
3642
    /*
3643
     * Examine all outside transitions from current state
3644
     */
3645
0
    for (i = 0;i < comp->nbstrings;i++) {
3646
0
  target = comp->compact[state * (comp->nbstrings + 1) + i + 1];
3647
0
  if ((target > 0) && (target <= comp->nbstates)) {
3648
0
      target--; /* to avoid 0 */
3649
0
      if (xmlRegStrEqualWildcard(comp->stringMap[i], value)) {
3650
0
    exec->index = target;
3651
0
    if ((exec->callback != NULL) && (comp->transdata != NULL)) {
3652
0
        exec->callback(exec->data, value,
3653
0
        comp->transdata[state * comp->nbstrings + i], data);
3654
0
    }
3655
0
    if (comp->compact[target * (comp->nbstrings + 1)] ==
3656
0
        XML_REGEXP_SINK_STATE)
3657
0
        goto error;
3658
3659
0
    if (comp->compact[target * (comp->nbstrings + 1)] ==
3660
0
        XML_REGEXP_FINAL_STATE)
3661
0
        return(1);
3662
0
    return(0);
3663
0
      }
3664
0
  }
3665
0
    }
3666
    /*
3667
     * Failed to find an exit transition out from current state for the
3668
     * current token
3669
     */
3670
0
error:
3671
0
    exec->errStateNo = state;
3672
0
    exec->status = XML_REGEXP_NOT_FOUND;
3673
0
    xmlRegExecSetErrString(exec, value);
3674
0
    return(exec->status);
3675
0
}
3676
3677
/**
3678
 * xmlRegExecPushStringInternal:
3679
 * @exec: a regexp execution context or NULL to indicate the end
3680
 * @value: a string token input
3681
 * @data: data associated to the token to reuse in callbacks
3682
 * @compound: value was assembled from 2 strings
3683
 *
3684
 * Push one input token in the execution context
3685
 *
3686
 * Returns: 1 if the regexp reached a final state, 0 if non-final, and
3687
 *     a negative value in case of error.
3688
 */
3689
static int
3690
xmlRegExecPushStringInternal(xmlRegExecCtxtPtr exec, const xmlChar *value,
3691
0
                       void *data, int compound) {
3692
0
    xmlRegTransPtr trans;
3693
0
    xmlRegAtomPtr atom;
3694
0
    int ret;
3695
0
    int final = 0;
3696
0
    int progress = 1;
3697
3698
0
    if (exec == NULL)
3699
0
  return(-1);
3700
0
    if (exec->comp == NULL)
3701
0
  return(-1);
3702
0
    if (exec->status != XML_REGEXP_OK)
3703
0
  return(exec->status);
3704
3705
0
    if (exec->comp->compact != NULL)
3706
0
  return(xmlRegCompactPushString(exec, exec->comp, value, data));
3707
3708
0
    if (value == NULL) {
3709
0
        if (exec->state->type == XML_REGEXP_FINAL_STATE)
3710
0
      return(1);
3711
0
  final = 1;
3712
0
    }
3713
3714
    /*
3715
     * If we have an active rollback stack push the new value there
3716
     * and get back to where we were left
3717
     */
3718
0
    if ((value != NULL) && (exec->inputStackNr > 0)) {
3719
0
  xmlFARegExecSaveInputString(exec, value, data);
3720
0
  value = exec->inputStack[exec->index].value;
3721
0
  data = exec->inputStack[exec->index].data;
3722
0
    }
3723
3724
0
    while ((exec->status == XML_REGEXP_OK) &&
3725
0
     ((value != NULL) ||
3726
0
      ((final == 1) &&
3727
0
       (exec->state->type != XML_REGEXP_FINAL_STATE)))) {
3728
3729
  /*
3730
   * End of input on non-terminal state, rollback, however we may
3731
   * still have epsilon like transition for counted transitions
3732
   * on counters, in that case don't break too early.
3733
   */
3734
0
  if ((value == NULL) && (exec->counts == NULL))
3735
0
      goto rollback;
3736
3737
0
  exec->transcount = 0;
3738
0
  for (;exec->transno < exec->state->nbTrans;exec->transno++) {
3739
0
      trans = &exec->state->trans[exec->transno];
3740
0
      if (trans->to < 0)
3741
0
    continue;
3742
0
      atom = trans->atom;
3743
0
      ret = 0;
3744
0
      if (trans->count == REGEXP_ALL_LAX_COUNTER) {
3745
0
    int i;
3746
0
    int count;
3747
0
    xmlRegTransPtr t;
3748
0
    xmlRegCounterPtr counter;
3749
3750
0
    ret = 0;
3751
3752
    /*
3753
     * Check all counted transitions from the current state
3754
     */
3755
0
    if ((value == NULL) && (final)) {
3756
0
        ret = 1;
3757
0
    } else if (value != NULL) {
3758
0
        for (i = 0;i < exec->state->nbTrans;i++) {
3759
0
      t = &exec->state->trans[i];
3760
0
      if ((t->counter < 0) || (t == trans))
3761
0
          continue;
3762
0
      counter = &exec->comp->counters[t->counter];
3763
0
      count = exec->counts[t->counter];
3764
0
      if ((count < counter->max) &&
3765
0
                (t->atom != NULL) &&
3766
0
          (xmlStrEqual(value, t->atom->valuep))) {
3767
0
          ret = 0;
3768
0
          break;
3769
0
      }
3770
0
      if ((count >= counter->min) &&
3771
0
          (count < counter->max) &&
3772
0
          (t->atom != NULL) &&
3773
0
          (xmlStrEqual(value, t->atom->valuep))) {
3774
0
          ret = 1;
3775
0
          break;
3776
0
      }
3777
0
        }
3778
0
    }
3779
0
      } else if (trans->count == REGEXP_ALL_COUNTER) {
3780
0
    int i;
3781
0
    int count;
3782
0
    xmlRegTransPtr t;
3783
0
    xmlRegCounterPtr counter;
3784
3785
0
    ret = 1;
3786
3787
    /*
3788
     * Check all counted transitions from the current state
3789
     */
3790
0
    for (i = 0;i < exec->state->nbTrans;i++) {
3791
0
                    t = &exec->state->trans[i];
3792
0
        if ((t->counter < 0) || (t == trans))
3793
0
      continue;
3794
0
                    counter = &exec->comp->counters[t->counter];
3795
0
        count = exec->counts[t->counter];
3796
0
        if ((count < counter->min) || (count > counter->max)) {
3797
0
      ret = 0;
3798
0
      break;
3799
0
        }
3800
0
    }
3801
0
      } else if (trans->count >= 0) {
3802
0
    int count;
3803
0
    xmlRegCounterPtr counter;
3804
3805
    /*
3806
     * A counted transition.
3807
     */
3808
3809
0
    count = exec->counts[trans->count];
3810
0
    counter = &exec->comp->counters[trans->count];
3811
0
    ret = ((count >= counter->min) && (count <= counter->max));
3812
0
      } else if (atom == NULL) {
3813
0
    fprintf(stderr, "epsilon transition left at runtime\n");
3814
0
    exec->status = XML_REGEXP_INTERNAL_ERROR;
3815
0
    break;
3816
0
      } else if (value != NULL) {
3817
0
    ret = xmlRegStrEqualWildcard(atom->valuep, value);
3818
0
    if (atom->neg) {
3819
0
        ret = !ret;
3820
0
        if (!compound)
3821
0
            ret = 0;
3822
0
    }
3823
0
    if ((ret == 1) && (trans->counter >= 0)) {
3824
0
        xmlRegCounterPtr counter;
3825
0
        int count;
3826
3827
0
        count = exec->counts[trans->counter];
3828
0
        counter = &exec->comp->counters[trans->counter];
3829
0
        if (count >= counter->max)
3830
0
      ret = 0;
3831
0
    }
3832
3833
0
    if ((ret == 1) && (atom->min > 0) && (atom->max > 0)) {
3834
0
        xmlRegStatePtr to = exec->comp->states[trans->to];
3835
3836
        /*
3837
         * this is a multiple input sequence
3838
         */
3839
0
        if (exec->state->nbTrans > exec->transno + 1) {
3840
0
      if (exec->inputStackNr <= 0) {
3841
0
          xmlFARegExecSaveInputString(exec, value, data);
3842
0
      }
3843
0
      xmlFARegExecSave(exec);
3844
0
        }
3845
0
        exec->transcount = 1;
3846
0
        do {
3847
      /*
3848
       * Try to progress as much as possible on the input
3849
       */
3850
0
      if (exec->transcount == atom->max) {
3851
0
          break;
3852
0
      }
3853
0
      exec->index++;
3854
0
      value = exec->inputStack[exec->index].value;
3855
0
      data = exec->inputStack[exec->index].data;
3856
3857
      /*
3858
       * End of input: stop here
3859
       */
3860
0
      if (value == NULL) {
3861
0
          exec->index --;
3862
0
          break;
3863
0
      }
3864
0
      if (exec->transcount >= atom->min) {
3865
0
          int transno = exec->transno;
3866
0
          xmlRegStatePtr state = exec->state;
3867
3868
          /*
3869
           * The transition is acceptable save it
3870
           */
3871
0
          exec->transno = -1; /* trick */
3872
0
          exec->state = to;
3873
0
          if (exec->inputStackNr <= 0) {
3874
0
        xmlFARegExecSaveInputString(exec, value, data);
3875
0
          }
3876
0
          xmlFARegExecSave(exec);
3877
0
          exec->transno = transno;
3878
0
          exec->state = state;
3879
0
      }
3880
0
      ret = xmlStrEqual(value, atom->valuep);
3881
0
      exec->transcount++;
3882
0
        } while (ret == 1);
3883
0
        if (exec->transcount < atom->min)
3884
0
      ret = 0;
3885
3886
        /*
3887
         * If the last check failed but one transition was found
3888
         * possible, rollback
3889
         */
3890
0
        if (ret < 0)
3891
0
      ret = 0;
3892
0
        if (ret == 0) {
3893
0
      goto rollback;
3894
0
        }
3895
0
    }
3896
0
      }
3897
0
      if (ret == 1) {
3898
0
    if ((exec->callback != NULL) && (atom != NULL) &&
3899
0
      (data != NULL)) {
3900
0
        exec->callback(exec->data, atom->valuep,
3901
0
                 atom->data, data);
3902
0
    }
3903
0
    if (exec->state->nbTrans > exec->transno + 1) {
3904
0
        if (exec->inputStackNr <= 0) {
3905
0
      xmlFARegExecSaveInputString(exec, value, data);
3906
0
        }
3907
0
        xmlFARegExecSave(exec);
3908
0
    }
3909
0
    if (trans->counter >= 0) {
3910
0
        exec->counts[trans->counter]++;
3911
0
    }
3912
0
    if ((trans->count >= 0) &&
3913
0
        (trans->count < REGEXP_ALL_COUNTER)) {
3914
0
        exec->counts[trans->count] = 0;
3915
0
    }
3916
0
                if ((exec->comp->states[trans->to] != NULL) &&
3917
0
        (exec->comp->states[trans->to]->type ==
3918
0
         XML_REGEXP_SINK_STATE)) {
3919
        /*
3920
         * entering a sink state, save the current state as error
3921
         * state.
3922
         */
3923
0
                    if (xmlRegExecSetErrString(exec, value) < 0)
3924
0
                        break;
3925
0
        exec->errState = exec->state;
3926
0
        memcpy(exec->errCounts, exec->counts,
3927
0
         exec->comp->nbCounters * sizeof(int));
3928
0
    }
3929
0
    exec->state = exec->comp->states[trans->to];
3930
0
    exec->transno = 0;
3931
0
    if (trans->atom != NULL) {
3932
0
        if (exec->inputStack != NULL) {
3933
0
      exec->index++;
3934
0
      if (exec->index < exec->inputStackNr) {
3935
0
          value = exec->inputStack[exec->index].value;
3936
0
          data = exec->inputStack[exec->index].data;
3937
0
      } else {
3938
0
          value = NULL;
3939
0
          data = NULL;
3940
0
      }
3941
0
        } else {
3942
0
      value = NULL;
3943
0
      data = NULL;
3944
0
        }
3945
0
    }
3946
0
    goto progress;
3947
0
      } else if (ret < 0) {
3948
0
    exec->status = XML_REGEXP_INTERNAL_ERROR;
3949
0
    break;
3950
0
      }
3951
0
  }
3952
0
  if ((exec->transno != 0) || (exec->state->nbTrans == 0)) {
3953
0
rollback:
3954
            /*
3955
       * if we didn't yet rollback on the current input
3956
       * store the current state as the error state.
3957
       */
3958
0
      if ((progress) && (exec->state != NULL) &&
3959
0
          (exec->state->type != XML_REGEXP_SINK_STATE)) {
3960
0
          progress = 0;
3961
0
                if (xmlRegExecSetErrString(exec, value) < 0)
3962
0
                    break;
3963
0
    exec->errState = exec->state;
3964
0
                if (exec->comp->nbCounters)
3965
0
                    memcpy(exec->errCounts, exec->counts,
3966
0
                           exec->comp->nbCounters * sizeof(int));
3967
0
      }
3968
3969
      /*
3970
       * Failed to find a way out
3971
       */
3972
0
      exec->determinist = 0;
3973
0
      xmlFARegExecRollBack(exec);
3974
0
      if ((exec->inputStack != NULL ) &&
3975
0
                (exec->status == XML_REGEXP_OK)) {
3976
0
    value = exec->inputStack[exec->index].value;
3977
0
    data = exec->inputStack[exec->index].data;
3978
0
      }
3979
0
  }
3980
0
  continue;
3981
0
progress:
3982
0
        progress = 1;
3983
0
  continue;
3984
0
    }
3985
0
    if (exec->status == XML_REGEXP_OK) {
3986
0
        return(exec->state->type == XML_REGEXP_FINAL_STATE);
3987
0
    }
3988
0
    return(exec->status);
3989
0
}
3990
3991
/**
3992
 * xmlRegExecPushString:
3993
 * @exec: a regexp execution context or NULL to indicate the end
3994
 * @value: a string token input
3995
 * @data: data associated to the token to reuse in callbacks
3996
 *
3997
 * Push one input token in the execution context
3998
 *
3999
 * Returns: 1 if the regexp reached a final state, 0 if non-final, and
4000
 *     a negative value in case of error.
4001
 */
4002
int
4003
xmlRegExecPushString(xmlRegExecCtxtPtr exec, const xmlChar *value,
4004
0
               void *data) {
4005
0
    return(xmlRegExecPushStringInternal(exec, value, data, 0));
4006
0
}
4007
4008
/**
4009
 * xmlRegExecPushString2:
4010
 * @exec: a regexp execution context or NULL to indicate the end
4011
 * @value: the first string token input
4012
 * @value2: the second string token input
4013
 * @data: data associated to the token to reuse in callbacks
4014
 *
4015
 * Push one input token in the execution context
4016
 *
4017
 * Returns: 1 if the regexp reached a final state, 0 if non-final, and
4018
 *     a negative value in case of error.
4019
 */
4020
int
4021
xmlRegExecPushString2(xmlRegExecCtxtPtr exec, const xmlChar *value,
4022
0
                      const xmlChar *value2, void *data) {
4023
0
    xmlChar buf[150];
4024
0
    int lenn, lenp, ret;
4025
0
    xmlChar *str;
4026
4027
0
    if (exec == NULL)
4028
0
  return(-1);
4029
0
    if (exec->comp == NULL)
4030
0
  return(-1);
4031
0
    if (exec->status != XML_REGEXP_OK)
4032
0
  return(exec->status);
4033
4034
0
    if (value2 == NULL)
4035
0
        return(xmlRegExecPushString(exec, value, data));
4036
4037
0
    lenn = strlen((char *) value2);
4038
0
    lenp = strlen((char *) value);
4039
4040
0
    if (150 < lenn + lenp + 2) {
4041
0
  str = (xmlChar *) xmlMallocAtomic(lenn + lenp + 2);
4042
0
  if (str == NULL) {
4043
0
      exec->status = XML_REGEXP_OUT_OF_MEMORY;
4044
0
      return(-1);
4045
0
  }
4046
0
    } else {
4047
0
  str = buf;
4048
0
    }
4049
0
    memcpy(&str[0], value, lenp);
4050
0
    str[lenp] = XML_REG_STRING_SEPARATOR;
4051
0
    memcpy(&str[lenp + 1], value2, lenn);
4052
0
    str[lenn + lenp + 1] = 0;
4053
4054
0
    if (exec->comp->compact != NULL)
4055
0
  ret = xmlRegCompactPushString(exec, exec->comp, str, data);
4056
0
    else
4057
0
        ret = xmlRegExecPushStringInternal(exec, str, data, 1);
4058
4059
0
    if (str != buf)
4060
0
        xmlFree(str);
4061
0
    return(ret);
4062
0
}
4063
4064
/**
4065
 * xmlRegExecGetValues:
4066
 * @exec: a regexp execution context
4067
 * @err: error extraction or normal one
4068
 * @nbval: pointer to the number of accepted values IN/OUT
4069
 * @nbneg: return number of negative transitions
4070
 * @values: pointer to the array of acceptable values
4071
 * @terminal: return value if this was a terminal state
4072
 *
4073
 * Extract information from the regexp execution, internal routine to
4074
 * implement xmlRegExecNextValues() and xmlRegExecErrInfo()
4075
 *
4076
 * Returns: 0 in case of success or -1 in case of error.
4077
 */
4078
static int
4079
xmlRegExecGetValues(xmlRegExecCtxtPtr exec, int err,
4080
                    int *nbval, int *nbneg,
4081
0
        xmlChar **values, int *terminal) {
4082
0
    int maxval;
4083
0
    int nb = 0;
4084
4085
0
    if ((exec == NULL) || (nbval == NULL) || (nbneg == NULL) ||
4086
0
        (values == NULL) || (*nbval <= 0))
4087
0
        return(-1);
4088
4089
0
    maxval = *nbval;
4090
0
    *nbval = 0;
4091
0
    *nbneg = 0;
4092
0
    if ((exec->comp != NULL) && (exec->comp->compact != NULL)) {
4093
0
        xmlRegexpPtr comp;
4094
0
  int target, i, state;
4095
4096
0
        comp = exec->comp;
4097
4098
0
  if (err) {
4099
0
      if (exec->errStateNo == -1) return(-1);
4100
0
      state = exec->errStateNo;
4101
0
  } else {
4102
0
      state = exec->index;
4103
0
  }
4104
0
  if (terminal != NULL) {
4105
0
      if (comp->compact[state * (comp->nbstrings + 1)] ==
4106
0
          XML_REGEXP_FINAL_STATE)
4107
0
    *terminal = 1;
4108
0
      else
4109
0
    *terminal = 0;
4110
0
  }
4111
0
  for (i = 0;(i < comp->nbstrings) && (nb < maxval);i++) {
4112
0
      target = comp->compact[state * (comp->nbstrings + 1) + i + 1];
4113
0
      if ((target > 0) && (target <= comp->nbstates) &&
4114
0
          (comp->compact[(target - 1) * (comp->nbstrings + 1)] !=
4115
0
     XML_REGEXP_SINK_STATE)) {
4116
0
          values[nb++] = comp->stringMap[i];
4117
0
    (*nbval)++;
4118
0
      }
4119
0
  }
4120
0
  for (i = 0;(i < comp->nbstrings) && (nb < maxval);i++) {
4121
0
      target = comp->compact[state * (comp->nbstrings + 1) + i + 1];
4122
0
      if ((target > 0) && (target <= comp->nbstates) &&
4123
0
          (comp->compact[(target - 1) * (comp->nbstrings + 1)] ==
4124
0
     XML_REGEXP_SINK_STATE)) {
4125
0
          values[nb++] = comp->stringMap[i];
4126
0
    (*nbneg)++;
4127
0
      }
4128
0
  }
4129
0
    } else {
4130
0
        int transno;
4131
0
  xmlRegTransPtr trans;
4132
0
  xmlRegAtomPtr atom;
4133
0
  xmlRegStatePtr state;
4134
4135
0
  if (terminal != NULL) {
4136
0
      if (exec->state->type == XML_REGEXP_FINAL_STATE)
4137
0
    *terminal = 1;
4138
0
      else
4139
0
    *terminal = 0;
4140
0
  }
4141
4142
0
  if (err) {
4143
0
      if (exec->errState == NULL) return(-1);
4144
0
      state = exec->errState;
4145
0
  } else {
4146
0
      if (exec->state == NULL) return(-1);
4147
0
      state = exec->state;
4148
0
  }
4149
0
  for (transno = 0;
4150
0
       (transno < state->nbTrans) && (nb < maxval);
4151
0
       transno++) {
4152
0
      trans = &state->trans[transno];
4153
0
      if (trans->to < 0)
4154
0
    continue;
4155
0
      atom = trans->atom;
4156
0
      if ((atom == NULL) || (atom->valuep == NULL))
4157
0
    continue;
4158
0
      if (trans->count == REGEXP_ALL_LAX_COUNTER) {
4159
          /* this should not be reached but ... */
4160
0
      } else if (trans->count == REGEXP_ALL_COUNTER) {
4161
          /* this should not be reached but ... */
4162
0
      } else if (trans->counter >= 0) {
4163
0
    xmlRegCounterPtr counter = NULL;
4164
0
    int count;
4165
4166
0
    if (err)
4167
0
        count = exec->errCounts[trans->counter];
4168
0
    else
4169
0
        count = exec->counts[trans->counter];
4170
0
    if (exec->comp != NULL)
4171
0
        counter = &exec->comp->counters[trans->counter];
4172
0
    if ((counter == NULL) || (count < counter->max)) {
4173
0
        if (atom->neg)
4174
0
      values[nb++] = (xmlChar *) atom->valuep2;
4175
0
        else
4176
0
      values[nb++] = (xmlChar *) atom->valuep;
4177
0
        (*nbval)++;
4178
0
    }
4179
0
      } else {
4180
0
                if ((exec->comp != NULL) && (exec->comp->states[trans->to] != NULL) &&
4181
0
        (exec->comp->states[trans->to]->type !=
4182
0
         XML_REGEXP_SINK_STATE)) {
4183
0
        if (atom->neg)
4184
0
      values[nb++] = (xmlChar *) atom->valuep2;
4185
0
        else
4186
0
      values[nb++] = (xmlChar *) atom->valuep;
4187
0
        (*nbval)++;
4188
0
    }
4189
0
      }
4190
0
  }
4191
0
  for (transno = 0;
4192
0
       (transno < state->nbTrans) && (nb < maxval);
4193
0
       transno++) {
4194
0
      trans = &state->trans[transno];
4195
0
      if (trans->to < 0)
4196
0
    continue;
4197
0
      atom = trans->atom;
4198
0
      if ((atom == NULL) || (atom->valuep == NULL))
4199
0
    continue;
4200
0
      if (trans->count == REGEXP_ALL_LAX_COUNTER) {
4201
0
          continue;
4202
0
      } else if (trans->count == REGEXP_ALL_COUNTER) {
4203
0
          continue;
4204
0
      } else if (trans->counter >= 0) {
4205
0
          continue;
4206
0
      } else {
4207
0
                if ((exec->comp->states[trans->to] != NULL) &&
4208
0
        (exec->comp->states[trans->to]->type ==
4209
0
         XML_REGEXP_SINK_STATE)) {
4210
0
        if (atom->neg)
4211
0
      values[nb++] = (xmlChar *) atom->valuep2;
4212
0
        else
4213
0
      values[nb++] = (xmlChar *) atom->valuep;
4214
0
        (*nbneg)++;
4215
0
    }
4216
0
      }
4217
0
  }
4218
0
    }
4219
0
    return(0);
4220
0
}
4221
4222
/**
4223
 * xmlRegExecNextValues:
4224
 * @exec: a regexp execution context
4225
 * @nbval: pointer to the number of accepted values IN/OUT
4226
 * @nbneg: return number of negative transitions
4227
 * @values: pointer to the array of acceptable values
4228
 * @terminal: return value if this was a terminal state
4229
 *
4230
 * Extract information from the regexp execution,
4231
 * the parameter @values must point to an array of @nbval string pointers
4232
 * on return nbval will contain the number of possible strings in that
4233
 * state and the @values array will be updated with them. The string values
4234
 * returned will be freed with the @exec context and don't need to be
4235
 * deallocated.
4236
 *
4237
 * Returns: 0 in case of success or -1 in case of error.
4238
 */
4239
int
4240
xmlRegExecNextValues(xmlRegExecCtxtPtr exec, int *nbval, int *nbneg,
4241
0
                     xmlChar **values, int *terminal) {
4242
0
    return(xmlRegExecGetValues(exec, 0, nbval, nbneg, values, terminal));
4243
0
}
4244
4245
/**
4246
 * xmlRegExecErrInfo:
4247
 * @exec: a regexp execution context generating an error
4248
 * @string: return value for the error string
4249
 * @nbval: pointer to the number of accepted values IN/OUT
4250
 * @nbneg: return number of negative transitions
4251
 * @values: pointer to the array of acceptable values
4252
 * @terminal: return value if this was a terminal state
4253
 *
4254
 * Extract error information from the regexp execution, the parameter
4255
 * @string will be updated with the value pushed and not accepted,
4256
 * the parameter @values must point to an array of @nbval string pointers
4257
 * on return nbval will contain the number of possible strings in that
4258
 * state and the @values array will be updated with them. The string values
4259
 * returned will be freed with the @exec context and don't need to be
4260
 * deallocated.
4261
 *
4262
 * Returns: 0 in case of success or -1 in case of error.
4263
 */
4264
int
4265
xmlRegExecErrInfo(xmlRegExecCtxtPtr exec, const xmlChar **string,
4266
0
                  int *nbval, int *nbneg, xmlChar **values, int *terminal) {
4267
0
    if (exec == NULL)
4268
0
        return(-1);
4269
0
    if (string != NULL) {
4270
0
        if (exec->status != XML_REGEXP_OK)
4271
0
      *string = exec->errString;
4272
0
  else
4273
0
      *string = NULL;
4274
0
    }
4275
0
    return(xmlRegExecGetValues(exec, 1, nbval, nbneg, values, terminal));
4276
0
}
4277
4278
#if 0
4279
static int
4280
xmlRegExecPushChar(xmlRegExecCtxtPtr exec, int UCS) {
4281
    xmlRegTransPtr trans;
4282
    xmlRegAtomPtr atom;
4283
    int ret;
4284
    int codepoint, len;
4285
4286
    if (exec == NULL)
4287
  return(-1);
4288
    if (exec->status != XML_REGEXP_OK)
4289
  return(exec->status);
4290
4291
    while ((exec->status == XML_REGEXP_OK) &&
4292
     ((exec->inputString[exec->index] != 0) ||
4293
      (exec->state->type != XML_REGEXP_FINAL_STATE))) {
4294
4295
  /*
4296
   * End of input on non-terminal state, rollback, however we may
4297
   * still have epsilon like transition for counted transitions
4298
   * on counters, in that case don't break too early.
4299
   */
4300
  if ((exec->inputString[exec->index] == 0) && (exec->counts == NULL))
4301
      goto rollback;
4302
4303
  exec->transcount = 0;
4304
  for (;exec->transno < exec->state->nbTrans;exec->transno++) {
4305
      trans = &exec->state->trans[exec->transno];
4306
      if (trans->to < 0)
4307
    continue;
4308
      atom = trans->atom;
4309
      ret = 0;
4310
      if (trans->count >= 0) {
4311
    int count;
4312
    xmlRegCounterPtr counter;
4313
4314
    /*
4315
     * A counted transition.
4316
     */
4317
4318
    count = exec->counts[trans->count];
4319
    counter = &exec->comp->counters[trans->count];
4320
    ret = ((count >= counter->min) && (count <= counter->max));
4321
      } else if (atom == NULL) {
4322
    fprintf(stderr, "epsilon transition left at runtime\n");
4323
    exec->status = XML_REGEXP_INTERNAL_ERROR;
4324
    break;
4325
      } else if (exec->inputString[exec->index] != 0) {
4326
                codepoint = CUR_SCHAR(&(exec->inputString[exec->index]), len);
4327
    ret = xmlRegCheckCharacter(atom, codepoint);
4328
    if ((ret == 1) && (atom->min > 0) && (atom->max > 0)) {
4329
        xmlRegStatePtr to = exec->comp->states[trans->to];
4330
4331
        /*
4332
         * this is a multiple input sequence
4333
         */
4334
        if (exec->state->nbTrans > exec->transno + 1) {
4335
      xmlFARegExecSave(exec);
4336
        }
4337
        exec->transcount = 1;
4338
        do {
4339
      /*
4340
       * Try to progress as much as possible on the input
4341
       */
4342
      if (exec->transcount == atom->max) {
4343
          break;
4344
      }
4345
      exec->index += len;
4346
      /*
4347
       * End of input: stop here
4348
       */
4349
      if (exec->inputString[exec->index] == 0) {
4350
          exec->index -= len;
4351
          break;
4352
      }
4353
      if (exec->transcount >= atom->min) {
4354
          int transno = exec->transno;
4355
          xmlRegStatePtr state = exec->state;
4356
4357
          /*
4358
           * The transition is acceptable save it
4359
           */
4360
          exec->transno = -1; /* trick */
4361
          exec->state = to;
4362
          xmlFARegExecSave(exec);
4363
          exec->transno = transno;
4364
          exec->state = state;
4365
      }
4366
      codepoint = CUR_SCHAR(&(exec->inputString[exec->index]),
4367
                      len);
4368
      ret = xmlRegCheckCharacter(atom, codepoint);
4369
      exec->transcount++;
4370
        } while (ret == 1);
4371
        if (exec->transcount < atom->min)
4372
      ret = 0;
4373
4374
        /*
4375
         * If the last check failed but one transition was found
4376
         * possible, rollback
4377
         */
4378
        if (ret < 0)
4379
      ret = 0;
4380
        if (ret == 0) {
4381
      goto rollback;
4382
        }
4383
    }
4384
      }
4385
      if (ret == 1) {
4386
    if (exec->state->nbTrans > exec->transno + 1) {
4387
        xmlFARegExecSave(exec);
4388
    }
4389
    /*
4390
     * restart count for expressions like this ((abc){2})*
4391
     */
4392
    if (trans->count >= 0) {
4393
        exec->counts[trans->count] = 0;
4394
    }
4395
    if (trans->counter >= 0) {
4396
        exec->counts[trans->counter]++;
4397
    }
4398
    exec->state = exec->comp->states[trans->to];
4399
    exec->transno = 0;
4400
    if (trans->atom != NULL) {
4401
        exec->index += len;
4402
    }
4403
    goto progress;
4404
      } else if (ret < 0) {
4405
    exec->status = XML_REGEXP_INTERNAL_ERROR;
4406
    break;
4407
      }
4408
  }
4409
  if ((exec->transno != 0) || (exec->state->nbTrans == 0)) {
4410
rollback:
4411
      /*
4412
       * Failed to find a way out
4413
       */
4414
      exec->determinist = 0;
4415
      xmlFARegExecRollBack(exec);
4416
  }
4417
progress:
4418
  continue;
4419
    }
4420
}
4421
#endif
4422
/************************************************************************
4423
 *                  *
4424
 *  Parser for the Schemas Datatype Regular Expressions   *
4425
 *  http://www.w3.org/TR/2001/REC-xmlschema-2-20010502/#regexs  *
4426
 *                  *
4427
 ************************************************************************/
4428
4429
/**
4430
 * xmlFAIsChar:
4431
 * @ctxt:  a regexp parser context
4432
 *
4433
 * [10]   Char   ::=   [^.\?*+()|#x5B#x5D]
4434
 */
4435
static int
4436
0
xmlFAIsChar(xmlRegParserCtxtPtr ctxt) {
4437
0
    int cur;
4438
0
    int len;
4439
4440
0
    len = 4;
4441
0
    cur = xmlGetUTF8Char(ctxt->cur, &len);
4442
0
    if (cur < 0) {
4443
0
        ERROR("Invalid UTF-8");
4444
0
        return(0);
4445
0
    }
4446
0
    if ((cur == '.') || (cur == '\\') || (cur == '?') ||
4447
0
  (cur == '*') || (cur == '+') || (cur == '(') ||
4448
0
  (cur == ')') || (cur == '|') || (cur == 0x5B) ||
4449
0
  (cur == 0x5D) || (cur == 0))
4450
0
  return(-1);
4451
0
    return(cur);
4452
0
}
4453
4454
/**
4455
 * xmlFAParseCharProp:
4456
 * @ctxt:  a regexp parser context
4457
 *
4458
 * [27]   charProp   ::=   IsCategory | IsBlock
4459
 * [28]   IsCategory ::= Letters | Marks | Numbers | Punctuation |
4460
 *                       Separators | Symbols | Others
4461
 * [29]   Letters   ::=   'L' [ultmo]?
4462
 * [30]   Marks   ::=   'M' [nce]?
4463
 * [31]   Numbers   ::=   'N' [dlo]?
4464
 * [32]   Punctuation   ::=   'P' [cdseifo]?
4465
 * [33]   Separators   ::=   'Z' [slp]?
4466
 * [34]   Symbols   ::=   'S' [mcko]?
4467
 * [35]   Others   ::=   'C' [cfon]?
4468
 * [36]   IsBlock   ::=   'Is' [a-zA-Z0-9#x2D]+
4469
 */
4470
static void
4471
0
xmlFAParseCharProp(xmlRegParserCtxtPtr ctxt) {
4472
0
    int cur;
4473
0
    xmlRegAtomType type = (xmlRegAtomType) 0;
4474
0
    xmlChar *blockName = NULL;
4475
4476
0
    cur = CUR;
4477
0
    if (cur == 'L') {
4478
0
  NEXT;
4479
0
  cur = CUR;
4480
0
  if (cur == 'u') {
4481
0
      NEXT;
4482
0
      type = XML_REGEXP_LETTER_UPPERCASE;
4483
0
  } else if (cur == 'l') {
4484
0
      NEXT;
4485
0
      type = XML_REGEXP_LETTER_LOWERCASE;
4486
0
  } else if (cur == 't') {
4487
0
      NEXT;
4488
0
      type = XML_REGEXP_LETTER_TITLECASE;
4489
0
  } else if (cur == 'm') {
4490
0
      NEXT;
4491
0
      type = XML_REGEXP_LETTER_MODIFIER;
4492
0
  } else if (cur == 'o') {
4493
0
      NEXT;
4494
0
      type = XML_REGEXP_LETTER_OTHERS;
4495
0
  } else {
4496
0
      type = XML_REGEXP_LETTER;
4497
0
  }
4498
0
    } else if (cur == 'M') {
4499
0
  NEXT;
4500
0
  cur = CUR;
4501
0
  if (cur == 'n') {
4502
0
      NEXT;
4503
      /* nonspacing */
4504
0
      type = XML_REGEXP_MARK_NONSPACING;
4505
0
  } else if (cur == 'c') {
4506
0
      NEXT;
4507
      /* spacing combining */
4508
0
      type = XML_REGEXP_MARK_SPACECOMBINING;
4509
0
  } else if (cur == 'e') {
4510
0
      NEXT;
4511
      /* enclosing */
4512
0
      type = XML_REGEXP_MARK_ENCLOSING;
4513
0
  } else {
4514
      /* all marks */
4515
0
      type = XML_REGEXP_MARK;
4516
0
  }
4517
0
    } else if (cur == 'N') {
4518
0
  NEXT;
4519
0
  cur = CUR;
4520
0
  if (cur == 'd') {
4521
0
      NEXT;
4522
      /* digital */
4523
0
      type = XML_REGEXP_NUMBER_DECIMAL;
4524
0
  } else if (cur == 'l') {
4525
0
      NEXT;
4526
      /* letter */
4527
0
      type = XML_REGEXP_NUMBER_LETTER;
4528
0
  } else if (cur == 'o') {
4529
0
      NEXT;
4530
      /* other */
4531
0
      type = XML_REGEXP_NUMBER_OTHERS;
4532
0
  } else {
4533
      /* all numbers */
4534
0
      type = XML_REGEXP_NUMBER;
4535
0
  }
4536
0
    } else if (cur == 'P') {
4537
0
  NEXT;
4538
0
  cur = CUR;
4539
0
  if (cur == 'c') {
4540
0
      NEXT;
4541
      /* connector */
4542
0
      type = XML_REGEXP_PUNCT_CONNECTOR;
4543
0
  } else if (cur == 'd') {
4544
0
      NEXT;
4545
      /* dash */
4546
0
      type = XML_REGEXP_PUNCT_DASH;
4547
0
  } else if (cur == 's') {
4548
0
      NEXT;
4549
      /* open */
4550
0
      type = XML_REGEXP_PUNCT_OPEN;
4551
0
  } else if (cur == 'e') {
4552
0
      NEXT;
4553
      /* close */
4554
0
      type = XML_REGEXP_PUNCT_CLOSE;
4555
0
  } else if (cur == 'i') {
4556
0
      NEXT;
4557
      /* initial quote */
4558
0
      type = XML_REGEXP_PUNCT_INITQUOTE;
4559
0
  } else if (cur == 'f') {
4560
0
      NEXT;
4561
      /* final quote */
4562
0
      type = XML_REGEXP_PUNCT_FINQUOTE;
4563
0
  } else if (cur == 'o') {
4564
0
      NEXT;
4565
      /* other */
4566
0
      type = XML_REGEXP_PUNCT_OTHERS;
4567
0
  } else {
4568
      /* all punctuation */
4569
0
      type = XML_REGEXP_PUNCT;
4570
0
  }
4571
0
    } else if (cur == 'Z') {
4572
0
  NEXT;
4573
0
  cur = CUR;
4574
0
  if (cur == 's') {
4575
0
      NEXT;
4576
      /* space */
4577
0
      type = XML_REGEXP_SEPAR_SPACE;
4578
0
  } else if (cur == 'l') {
4579
0
      NEXT;
4580
      /* line */
4581
0
      type = XML_REGEXP_SEPAR_LINE;
4582
0
  } else if (cur == 'p') {
4583
0
      NEXT;
4584
      /* paragraph */
4585
0
      type = XML_REGEXP_SEPAR_PARA;
4586
0
  } else {
4587
      /* all separators */
4588
0
      type = XML_REGEXP_SEPAR;
4589
0
  }
4590
0
    } else if (cur == 'S') {
4591
0
  NEXT;
4592
0
  cur = CUR;
4593
0
  if (cur == 'm') {
4594
0
      NEXT;
4595
0
      type = XML_REGEXP_SYMBOL_MATH;
4596
      /* math */
4597
0
  } else if (cur == 'c') {
4598
0
      NEXT;
4599
0
      type = XML_REGEXP_SYMBOL_CURRENCY;
4600
      /* currency */
4601
0
  } else if (cur == 'k') {
4602
0
      NEXT;
4603
0
      type = XML_REGEXP_SYMBOL_MODIFIER;
4604
      /* modifiers */
4605
0
  } else if (cur == 'o') {
4606
0
      NEXT;
4607
0
      type = XML_REGEXP_SYMBOL_OTHERS;
4608
      /* other */
4609
0
  } else {
4610
      /* all symbols */
4611
0
      type = XML_REGEXP_SYMBOL;
4612
0
  }
4613
0
    } else if (cur == 'C') {
4614
0
  NEXT;
4615
0
  cur = CUR;
4616
0
  if (cur == 'c') {
4617
0
      NEXT;
4618
      /* control */
4619
0
      type = XML_REGEXP_OTHER_CONTROL;
4620
0
  } else if (cur == 'f') {
4621
0
      NEXT;
4622
      /* format */
4623
0
      type = XML_REGEXP_OTHER_FORMAT;
4624
0
  } else if (cur == 'o') {
4625
0
      NEXT;
4626
      /* private use */
4627
0
      type = XML_REGEXP_OTHER_PRIVATE;
4628
0
  } else if (cur == 'n') {
4629
0
      NEXT;
4630
      /* not assigned */
4631
0
      type = XML_REGEXP_OTHER_NA;
4632
0
  } else {
4633
      /* all others */
4634
0
      type = XML_REGEXP_OTHER;
4635
0
  }
4636
0
    } else if (cur == 'I') {
4637
0
  const xmlChar *start;
4638
0
  NEXT;
4639
0
  cur = CUR;
4640
0
  if (cur != 's') {
4641
0
      ERROR("IsXXXX expected");
4642
0
      return;
4643
0
  }
4644
0
  NEXT;
4645
0
  start = ctxt->cur;
4646
0
  cur = CUR;
4647
0
  if (((cur >= 'a') && (cur <= 'z')) ||
4648
0
      ((cur >= 'A') && (cur <= 'Z')) ||
4649
0
      ((cur >= '0') && (cur <= '9')) ||
4650
0
      (cur == 0x2D)) {
4651
0
      NEXT;
4652
0
      cur = CUR;
4653
0
      while (((cur >= 'a') && (cur <= 'z')) ||
4654
0
    ((cur >= 'A') && (cur <= 'Z')) ||
4655
0
    ((cur >= '0') && (cur <= '9')) ||
4656
0
    (cur == 0x2D)) {
4657
0
    NEXT;
4658
0
    cur = CUR;
4659
0
      }
4660
0
  }
4661
0
  type = XML_REGEXP_BLOCK_NAME;
4662
0
  blockName = xmlStrndup(start, ctxt->cur - start);
4663
0
        if (blockName == NULL)
4664
0
      xmlRegexpErrMemory(ctxt);
4665
0
    } else {
4666
0
  ERROR("Unknown char property");
4667
0
  return;
4668
0
    }
4669
0
    if (ctxt->atom == NULL) {
4670
0
  ctxt->atom = xmlRegNewAtom(ctxt, type);
4671
0
        if (ctxt->atom == NULL) {
4672
0
            xmlFree(blockName);
4673
0
            return;
4674
0
        }
4675
0
  ctxt->atom->valuep = blockName;
4676
0
    } else if (ctxt->atom->type == XML_REGEXP_RANGES) {
4677
0
        if (xmlRegAtomAddRange(ctxt, ctxt->atom, ctxt->neg,
4678
0
                               type, 0, 0, blockName) == NULL) {
4679
0
            xmlFree(blockName);
4680
0
        }
4681
0
    }
4682
0
}
4683
4684
static int parse_escaped_codeunit(xmlRegParserCtxtPtr ctxt)
4685
0
{
4686
0
    int val = 0, i, cur;
4687
0
    for (i = 0; i < 4; i++) {
4688
0
  NEXT;
4689
0
  val *= 16;
4690
0
  cur = CUR;
4691
0
  if (cur >= '0' && cur <= '9') {
4692
0
      val += cur - '0';
4693
0
  } else if (cur >= 'A' && cur <= 'F') {
4694
0
      val += cur - 'A' + 10;
4695
0
  } else if (cur >= 'a' && cur <= 'f') {
4696
0
      val += cur - 'a' + 10;
4697
0
  } else {
4698
0
      ERROR("Expecting hex digit");
4699
0
      return -1;
4700
0
  }
4701
0
    }
4702
0
    return val;
4703
0
}
4704
4705
static int parse_escaped_codepoint(xmlRegParserCtxtPtr ctxt)
4706
0
{
4707
0
    int val = parse_escaped_codeunit(ctxt);
4708
0
    if (0xD800 <= val && val <= 0xDBFF) {
4709
0
  NEXT;
4710
0
  if (CUR == '\\') {
4711
0
      NEXT;
4712
0
      if (CUR == 'u') {
4713
0
    int low = parse_escaped_codeunit(ctxt);
4714
0
    if (0xDC00 <= low && low <= 0xDFFF) {
4715
0
        return (val - 0xD800) * 0x400 + (low - 0xDC00) + 0x10000;
4716
0
    }
4717
0
      }
4718
0
  }
4719
0
  ERROR("Invalid low surrogate pair code unit");
4720
0
  val = -1;
4721
0
    }
4722
0
    return val;
4723
0
}
4724
4725
/**
4726
 * xmlFAParseCharClassEsc:
4727
 * @ctxt:  a regexp parser context
4728
 *
4729
 * [23] charClassEsc ::= ( SingleCharEsc | MultiCharEsc | catEsc | complEsc )
4730
 * [24] SingleCharEsc ::= '\' [nrt\|.?*+(){}#x2D#x5B#x5D#x5E]
4731
 * [25] catEsc   ::=   '\p{' charProp '}'
4732
 * [26] complEsc ::=   '\P{' charProp '}'
4733
 * [37] MultiCharEsc ::= '.' | ('\' [sSiIcCdDwW])
4734
 */
4735
static void
4736
0
xmlFAParseCharClassEsc(xmlRegParserCtxtPtr ctxt) {
4737
0
    int cur;
4738
4739
0
    if (CUR == '.') {
4740
0
  if (ctxt->atom == NULL) {
4741
0
      ctxt->atom = xmlRegNewAtom(ctxt, XML_REGEXP_ANYCHAR);
4742
0
  } else if (ctxt->atom->type == XML_REGEXP_RANGES) {
4743
0
      xmlRegAtomAddRange(ctxt, ctxt->atom, ctxt->neg,
4744
0
             XML_REGEXP_ANYCHAR, 0, 0, NULL);
4745
0
  }
4746
0
  NEXT;
4747
0
  return;
4748
0
    }
4749
0
    if (CUR != '\\') {
4750
0
  ERROR("Escaped sequence: expecting \\");
4751
0
  return;
4752
0
    }
4753
0
    NEXT;
4754
0
    cur = CUR;
4755
0
    if (cur == 'p') {
4756
0
  NEXT;
4757
0
  if (CUR != '{') {
4758
0
      ERROR("Expecting '{'");
4759
0
      return;
4760
0
  }
4761
0
  NEXT;
4762
0
  xmlFAParseCharProp(ctxt);
4763
0
  if (CUR != '}') {
4764
0
      ERROR("Expecting '}'");
4765
0
      return;
4766
0
  }
4767
0
  NEXT;
4768
0
    } else if (cur == 'P') {
4769
0
  NEXT;
4770
0
  if (CUR != '{') {
4771
0
      ERROR("Expecting '{'");
4772
0
      return;
4773
0
  }
4774
0
  NEXT;
4775
0
  xmlFAParseCharProp(ctxt);
4776
0
        if (ctxt->atom != NULL)
4777
0
      ctxt->atom->neg = 1;
4778
0
  if (CUR != '}') {
4779
0
      ERROR("Expecting '}'");
4780
0
      return;
4781
0
  }
4782
0
  NEXT;
4783
0
    } else if ((cur == 'n') || (cur == 'r') || (cur == 't') || (cur == '\\') ||
4784
0
  (cur == '|') || (cur == '.') || (cur == '?') || (cur == '*') ||
4785
0
  (cur == '+') || (cur == '(') || (cur == ')') || (cur == '{') ||
4786
0
  (cur == '}') || (cur == 0x2D) || (cur == 0x5B) || (cur == 0x5D) ||
4787
0
  (cur == 0x5E) ||
4788
4789
  /* Non-standard escape sequences:
4790
   *                  Java 1.8|.NET Core 3.1|MSXML 6 */
4791
0
  (cur == '!') ||     /*   +  |     +       |    +   */
4792
0
  (cur == '"') ||     /*   +  |     +       |    +   */
4793
0
  (cur == '#') ||     /*   +  |     +       |    +   */
4794
0
  (cur == '$') ||     /*   +  |     +       |    +   */
4795
0
  (cur == '%') ||     /*   +  |     +       |    +   */
4796
0
  (cur == ',') ||     /*   +  |     +       |    +   */
4797
0
  (cur == '/') ||     /*   +  |     +       |    +   */
4798
0
  (cur == ':') ||     /*   +  |     +       |    +   */
4799
0
  (cur == ';') ||     /*   +  |     +       |    +   */
4800
0
  (cur == '=') ||     /*   +  |     +       |    +   */
4801
0
  (cur == '>') ||     /*      |     +       |    +   */
4802
0
  (cur == '@') ||     /*   +  |     +       |    +   */
4803
0
  (cur == '`') ||     /*   +  |     +       |    +   */
4804
0
  (cur == '~') ||     /*   +  |     +       |    +   */
4805
0
  (cur == 'u')) {     /*      |     +       |    +   */
4806
0
  if (ctxt->atom == NULL) {
4807
0
      ctxt->atom = xmlRegNewAtom(ctxt, XML_REGEXP_CHARVAL);
4808
0
      if (ctxt->atom != NULL) {
4809
0
          switch (cur) {
4810
0
        case 'n':
4811
0
            ctxt->atom->codepoint = '\n';
4812
0
      break;
4813
0
        case 'r':
4814
0
            ctxt->atom->codepoint = '\r';
4815
0
      break;
4816
0
        case 't':
4817
0
            ctxt->atom->codepoint = '\t';
4818
0
      break;
4819
0
        case 'u':
4820
0
      cur = parse_escaped_codepoint(ctxt);
4821
0
      if (cur < 0) {
4822
0
          return;
4823
0
      }
4824
0
      ctxt->atom->codepoint = cur;
4825
0
      break;
4826
0
        default:
4827
0
      ctxt->atom->codepoint = cur;
4828
0
    }
4829
0
      }
4830
0
  } else if (ctxt->atom->type == XML_REGEXP_RANGES) {
4831
0
            switch (cur) {
4832
0
                case 'n':
4833
0
                    cur = '\n';
4834
0
                    break;
4835
0
                case 'r':
4836
0
                    cur = '\r';
4837
0
                    break;
4838
0
                case 't':
4839
0
                    cur = '\t';
4840
0
                    break;
4841
0
            }
4842
0
      xmlRegAtomAddRange(ctxt, ctxt->atom, ctxt->neg,
4843
0
             XML_REGEXP_CHARVAL, cur, cur, NULL);
4844
0
  }
4845
0
  NEXT;
4846
0
    } else if ((cur == 's') || (cur == 'S') || (cur == 'i') || (cur == 'I') ||
4847
0
  (cur == 'c') || (cur == 'C') || (cur == 'd') || (cur == 'D') ||
4848
0
  (cur == 'w') || (cur == 'W')) {
4849
0
  xmlRegAtomType type = XML_REGEXP_ANYSPACE;
4850
4851
0
  switch (cur) {
4852
0
      case 's':
4853
0
    type = XML_REGEXP_ANYSPACE;
4854
0
    break;
4855
0
      case 'S':
4856
0
    type = XML_REGEXP_NOTSPACE;
4857
0
    break;
4858
0
      case 'i':
4859
0
    type = XML_REGEXP_INITNAME;
4860
0
    break;
4861
0
      case 'I':
4862
0
    type = XML_REGEXP_NOTINITNAME;
4863
0
    break;
4864
0
      case 'c':
4865
0
    type = XML_REGEXP_NAMECHAR;
4866
0
    break;
4867
0
      case 'C':
4868
0
    type = XML_REGEXP_NOTNAMECHAR;
4869
0
    break;
4870
0
      case 'd':
4871
0
    type = XML_REGEXP_DECIMAL;
4872
0
    break;
4873
0
      case 'D':
4874
0
    type = XML_REGEXP_NOTDECIMAL;
4875
0
    break;
4876
0
      case 'w':
4877
0
    type = XML_REGEXP_REALCHAR;
4878
0
    break;
4879
0
      case 'W':
4880
0
    type = XML_REGEXP_NOTREALCHAR;
4881
0
    break;
4882
0
  }
4883
0
  NEXT;
4884
0
  if (ctxt->atom == NULL) {
4885
0
      ctxt->atom = xmlRegNewAtom(ctxt, type);
4886
0
  } else if (ctxt->atom->type == XML_REGEXP_RANGES) {
4887
0
      xmlRegAtomAddRange(ctxt, ctxt->atom, ctxt->neg,
4888
0
             type, 0, 0, NULL);
4889
0
  }
4890
0
    } else {
4891
0
  ERROR("Wrong escape sequence, misuse of character '\\'");
4892
0
    }
4893
0
}
4894
4895
/**
4896
 * xmlFAParseCharRange:
4897
 * @ctxt:  a regexp parser context
4898
 *
4899
 * [17]   charRange   ::=     seRange | XmlCharRef | XmlCharIncDash
4900
 * [18]   seRange   ::=   charOrEsc '-' charOrEsc
4901
 * [20]   charOrEsc   ::=   XmlChar | SingleCharEsc
4902
 * [21]   XmlChar   ::=   [^\#x2D#x5B#x5D]
4903
 * [22]   XmlCharIncDash   ::=   [^\#x5B#x5D]
4904
 */
4905
static void
4906
0
xmlFAParseCharRange(xmlRegParserCtxtPtr ctxt) {
4907
0
    int cur, len;
4908
0
    int start = -1;
4909
0
    int end = -1;
4910
4911
0
    if (CUR == '\0') {
4912
0
        ERROR("Expecting ']'");
4913
0
  return;
4914
0
    }
4915
4916
0
    cur = CUR;
4917
0
    if (cur == '\\') {
4918
0
  NEXT;
4919
0
  cur = CUR;
4920
0
  switch (cur) {
4921
0
      case 'n': start = 0xA; break;
4922
0
      case 'r': start = 0xD; break;
4923
0
      case 't': start = 0x9; break;
4924
0
      case '\\': case '|': case '.': case '-': case '^': case '?':
4925
0
      case '*': case '+': case '{': case '}': case '(': case ')':
4926
0
      case '[': case ']':
4927
0
    start = cur; break;
4928
0
      default:
4929
0
    ERROR("Invalid escape value");
4930
0
    return;
4931
0
  }
4932
0
  end = start;
4933
0
        len = 1;
4934
0
    } else if ((cur != 0x5B) && (cur != 0x5D)) {
4935
0
        len = 4;
4936
0
        end = start = xmlGetUTF8Char(ctxt->cur, &len);
4937
0
        if (start < 0) {
4938
0
            ERROR("Invalid UTF-8");
4939
0
            return;
4940
0
        }
4941
0
    } else {
4942
0
  ERROR("Expecting a char range");
4943
0
  return;
4944
0
    }
4945
    /*
4946
     * Since we are "inside" a range, we can assume ctxt->cur is past
4947
     * the start of ctxt->string, and PREV should be safe
4948
     */
4949
0
    if ((start == '-') && (NXT(1) != ']') && (PREV != '[') && (PREV != '^')) {
4950
0
  NEXTL(len);
4951
0
  return;
4952
0
    }
4953
0
    NEXTL(len);
4954
0
    cur = CUR;
4955
0
    if ((cur != '-') || (NXT(1) == '[') || (NXT(1) == ']')) {
4956
0
        xmlRegAtomAddRange(ctxt, ctxt->atom, ctxt->neg,
4957
0
                  XML_REGEXP_CHARVAL, start, end, NULL);
4958
0
  return;
4959
0
    }
4960
0
    NEXT;
4961
0
    cur = CUR;
4962
0
    if (cur == '\\') {
4963
0
  NEXT;
4964
0
  cur = CUR;
4965
0
  switch (cur) {
4966
0
      case 'n': end = 0xA; break;
4967
0
      case 'r': end = 0xD; break;
4968
0
      case 't': end = 0x9; break;
4969
0
      case '\\': case '|': case '.': case '-': case '^': case '?':
4970
0
      case '*': case '+': case '{': case '}': case '(': case ')':
4971
0
      case '[': case ']':
4972
0
    end = cur; break;
4973
0
      default:
4974
0
    ERROR("Invalid escape value");
4975
0
    return;
4976
0
  }
4977
0
        len = 1;
4978
0
    } else if ((cur != '\0') && (cur != 0x5B) && (cur != 0x5D)) {
4979
0
        len = 4;
4980
0
        end = xmlGetUTF8Char(ctxt->cur, &len);
4981
0
        if (end < 0) {
4982
0
            ERROR("Invalid UTF-8");
4983
0
            return;
4984
0
        }
4985
0
    } else {
4986
0
  ERROR("Expecting the end of a char range");
4987
0
  return;
4988
0
    }
4989
4990
    /* TODO check that the values are acceptable character ranges for XML */
4991
0
    if (end < start) {
4992
0
  ERROR("End of range is before start of range");
4993
0
    } else {
4994
0
        NEXTL(len);
4995
0
        xmlRegAtomAddRange(ctxt, ctxt->atom, ctxt->neg,
4996
0
               XML_REGEXP_CHARVAL, start, end, NULL);
4997
0
    }
4998
0
    return;
4999
0
}
5000
5001
/**
5002
 * xmlFAParsePosCharGroup:
5003
 * @ctxt:  a regexp parser context
5004
 *
5005
 * [14]   posCharGroup ::= ( charRange | charClassEsc  )+
5006
 */
5007
static void
5008
0
xmlFAParsePosCharGroup(xmlRegParserCtxtPtr ctxt) {
5009
0
    do {
5010
0
  if (CUR == '\\') {
5011
0
      xmlFAParseCharClassEsc(ctxt);
5012
0
  } else {
5013
0
      xmlFAParseCharRange(ctxt);
5014
0
  }
5015
0
    } while ((CUR != ']') && (CUR != '-') &&
5016
0
             (CUR != 0) && (ctxt->error == 0));
5017
0
}
5018
5019
/**
5020
 * xmlFAParseCharGroup:
5021
 * @ctxt:  a regexp parser context
5022
 *
5023
 * [13]   charGroup    ::= posCharGroup | negCharGroup | charClassSub
5024
 * [15]   negCharGroup ::= '^' posCharGroup
5025
 * [16]   charClassSub ::= ( posCharGroup | negCharGroup ) '-' charClassExpr
5026
 * [12]   charClassExpr ::= '[' charGroup ']'
5027
 */
5028
static void
5029
0
xmlFAParseCharGroup(xmlRegParserCtxtPtr ctxt) {
5030
0
    int neg = ctxt->neg;
5031
5032
0
    if (CUR == '^') {
5033
0
  NEXT;
5034
0
  ctxt->neg = !ctxt->neg;
5035
0
  xmlFAParsePosCharGroup(ctxt);
5036
0
  ctxt->neg = neg;
5037
0
    }
5038
0
    while ((CUR != ']') && (ctxt->error == 0)) {
5039
0
  if ((CUR == '-') && (NXT(1) == '[')) {
5040
0
      NEXT; /* eat the '-' */
5041
0
      NEXT; /* eat the '[' */
5042
0
      ctxt->neg = 2;
5043
0
      xmlFAParseCharGroup(ctxt);
5044
0
      ctxt->neg = neg;
5045
0
      if (CUR == ']') {
5046
0
    NEXT;
5047
0
      } else {
5048
0
    ERROR("charClassExpr: ']' expected");
5049
0
      }
5050
0
      break;
5051
0
  } else {
5052
0
      xmlFAParsePosCharGroup(ctxt);
5053
0
  }
5054
0
    }
5055
0
}
5056
5057
/**
5058
 * xmlFAParseCharClass:
5059
 * @ctxt:  a regexp parser context
5060
 *
5061
 * [11]   charClass   ::=     charClassEsc | charClassExpr
5062
 * [12]   charClassExpr   ::=   '[' charGroup ']'
5063
 */
5064
static void
5065
0
xmlFAParseCharClass(xmlRegParserCtxtPtr ctxt) {
5066
0
    if (CUR == '[') {
5067
0
  NEXT;
5068
0
  ctxt->atom = xmlRegNewAtom(ctxt, XML_REGEXP_RANGES);
5069
0
  if (ctxt->atom == NULL)
5070
0
      return;
5071
0
  xmlFAParseCharGroup(ctxt);
5072
0
  if (CUR == ']') {
5073
0
      NEXT;
5074
0
  } else {
5075
0
      ERROR("xmlFAParseCharClass: ']' expected");
5076
0
  }
5077
0
    } else {
5078
0
  xmlFAParseCharClassEsc(ctxt);
5079
0
    }
5080
0
}
5081
5082
/**
5083
 * xmlFAParseQuantExact:
5084
 * @ctxt:  a regexp parser context
5085
 *
5086
 * [8]   QuantExact   ::=   [0-9]+
5087
 *
5088
 * Returns 0 if success or -1 in case of error
5089
 */
5090
static int
5091
0
xmlFAParseQuantExact(xmlRegParserCtxtPtr ctxt) {
5092
0
    int ret = 0;
5093
0
    int ok = 0;
5094
0
    int overflow = 0;
5095
5096
0
    while ((CUR >= '0') && (CUR <= '9')) {
5097
0
        if (ret > INT_MAX / 10) {
5098
0
            overflow = 1;
5099
0
        } else {
5100
0
            int digit = CUR - '0';
5101
5102
0
            ret *= 10;
5103
0
            if (ret > INT_MAX - digit)
5104
0
                overflow = 1;
5105
0
            else
5106
0
                ret += digit;
5107
0
        }
5108
0
  ok = 1;
5109
0
  NEXT;
5110
0
    }
5111
0
    if ((ok != 1) || (overflow == 1)) {
5112
0
  return(-1);
5113
0
    }
5114
0
    return(ret);
5115
0
}
5116
5117
/**
5118
 * xmlFAParseQuantifier:
5119
 * @ctxt:  a regexp parser context
5120
 *
5121
 * [4]   quantifier   ::=   [?*+] | ( '{' quantity '}' )
5122
 * [5]   quantity   ::=   quantRange | quantMin | QuantExact
5123
 * [6]   quantRange   ::=   QuantExact ',' QuantExact
5124
 * [7]   quantMin   ::=   QuantExact ','
5125
 * [8]   QuantExact   ::=   [0-9]+
5126
 */
5127
static int
5128
0
xmlFAParseQuantifier(xmlRegParserCtxtPtr ctxt) {
5129
0
    int cur;
5130
5131
0
    cur = CUR;
5132
0
    if ((cur == '?') || (cur == '*') || (cur == '+')) {
5133
0
  if (ctxt->atom != NULL) {
5134
0
      if (cur == '?')
5135
0
    ctxt->atom->quant = XML_REGEXP_QUANT_OPT;
5136
0
      else if (cur == '*')
5137
0
    ctxt->atom->quant = XML_REGEXP_QUANT_MULT;
5138
0
      else if (cur == '+')
5139
0
    ctxt->atom->quant = XML_REGEXP_QUANT_PLUS;
5140
0
  }
5141
0
  NEXT;
5142
0
  return(1);
5143
0
    }
5144
0
    if (cur == '{') {
5145
0
  int min = 0, max = 0;
5146
5147
0
  NEXT;
5148
0
  cur = xmlFAParseQuantExact(ctxt);
5149
0
  if (cur >= 0)
5150
0
      min = cur;
5151
0
        else {
5152
0
            ERROR("Improper quantifier");
5153
0
        }
5154
0
  if (CUR == ',') {
5155
0
      NEXT;
5156
0
      if (CUR == '}')
5157
0
          max = INT_MAX;
5158
0
      else {
5159
0
          cur = xmlFAParseQuantExact(ctxt);
5160
0
          if (cur >= 0)
5161
0
        max = cur;
5162
0
    else {
5163
0
        ERROR("Improper quantifier");
5164
0
    }
5165
0
      }
5166
0
  }
5167
0
  if (CUR == '}') {
5168
0
      NEXT;
5169
0
  } else {
5170
0
      ERROR("Unterminated quantifier");
5171
0
  }
5172
0
  if (max == 0)
5173
0
      max = min;
5174
0
  if (ctxt->atom != NULL) {
5175
0
      ctxt->atom->quant = XML_REGEXP_QUANT_RANGE;
5176
0
      ctxt->atom->min = min;
5177
0
      ctxt->atom->max = max;
5178
0
  }
5179
0
  return(1);
5180
0
    }
5181
0
    return(0);
5182
0
}
5183
5184
/**
5185
 * xmlFAParseAtom:
5186
 * @ctxt:  a regexp parser context
5187
 *
5188
 * [9]   atom   ::=   Char | charClass | ( '(' regExp ')' )
5189
 */
5190
static int
5191
0
xmlFAParseAtom(xmlRegParserCtxtPtr ctxt) {
5192
0
    int codepoint, len;
5193
5194
0
    codepoint = xmlFAIsChar(ctxt);
5195
0
    if (codepoint > 0) {
5196
0
  ctxt->atom = xmlRegNewAtom(ctxt, XML_REGEXP_CHARVAL);
5197
0
  if (ctxt->atom == NULL)
5198
0
      return(-1);
5199
0
        len = 4;
5200
0
        codepoint = xmlGetUTF8Char(ctxt->cur, &len);
5201
0
        if (codepoint < 0) {
5202
0
            ERROR("Invalid UTF-8");
5203
0
            return(-1);
5204
0
        }
5205
0
  ctxt->atom->codepoint = codepoint;
5206
0
  NEXTL(len);
5207
0
  return(1);
5208
0
    } else if (CUR == '|') {
5209
0
  return(0);
5210
0
    } else if (CUR == 0) {
5211
0
  return(0);
5212
0
    } else if (CUR == ')') {
5213
0
  return(0);
5214
0
    } else if (CUR == '(') {
5215
0
  xmlRegStatePtr start, oldend, start0;
5216
5217
0
  NEXT;
5218
0
        if (ctxt->depth >= 50) {
5219
0
      ERROR("xmlFAParseAtom: maximum nesting depth exceeded");
5220
0
            return(-1);
5221
0
        }
5222
  /*
5223
   * this extra Epsilon transition is needed if we count with 0 allowed
5224
   * unfortunately this can't be known at that point
5225
   */
5226
0
  xmlFAGenerateEpsilonTransition(ctxt, ctxt->state, NULL);
5227
0
  start0 = ctxt->state;
5228
0
  xmlFAGenerateEpsilonTransition(ctxt, ctxt->state, NULL);
5229
0
  start = ctxt->state;
5230
0
  oldend = ctxt->end;
5231
0
  ctxt->end = NULL;
5232
0
  ctxt->atom = NULL;
5233
0
        ctxt->depth++;
5234
0
  xmlFAParseRegExp(ctxt, 0);
5235
0
        ctxt->depth--;
5236
0
  if (CUR == ')') {
5237
0
      NEXT;
5238
0
  } else {
5239
0
      ERROR("xmlFAParseAtom: expecting ')'");
5240
0
  }
5241
0
  ctxt->atom = xmlRegNewAtom(ctxt, XML_REGEXP_SUBREG);
5242
0
  if (ctxt->atom == NULL)
5243
0
      return(-1);
5244
0
  ctxt->atom->start = start;
5245
0
  ctxt->atom->start0 = start0;
5246
0
  ctxt->atom->stop = ctxt->state;
5247
0
  ctxt->end = oldend;
5248
0
  return(1);
5249
0
    } else if ((CUR == '[') || (CUR == '\\') || (CUR == '.')) {
5250
0
  xmlFAParseCharClass(ctxt);
5251
0
  return(1);
5252
0
    }
5253
0
    return(0);
5254
0
}
5255
5256
/**
5257
 * xmlFAParsePiece:
5258
 * @ctxt:  a regexp parser context
5259
 *
5260
 * [3]   piece   ::=   atom quantifier?
5261
 */
5262
static int
5263
0
xmlFAParsePiece(xmlRegParserCtxtPtr ctxt) {
5264
0
    int ret;
5265
5266
0
    ctxt->atom = NULL;
5267
0
    ret = xmlFAParseAtom(ctxt);
5268
0
    if (ret == 0)
5269
0
  return(0);
5270
0
    if (ctxt->atom == NULL) {
5271
0
  ERROR("internal: no atom generated");
5272
0
    }
5273
0
    xmlFAParseQuantifier(ctxt);
5274
0
    return(1);
5275
0
}
5276
5277
/**
5278
 * xmlFAParseBranch:
5279
 * @ctxt:  a regexp parser context
5280
 * @to: optional target to the end of the branch
5281
 *
5282
 * @to is used to optimize by removing duplicate path in automata
5283
 * in expressions like (a|b)(c|d)
5284
 *
5285
 * [2]   branch   ::=   piece*
5286
 */
5287
static int
5288
0
xmlFAParseBranch(xmlRegParserCtxtPtr ctxt, xmlRegStatePtr to) {
5289
0
    xmlRegStatePtr previous;
5290
0
    int ret;
5291
5292
0
    previous = ctxt->state;
5293
0
    ret = xmlFAParsePiece(ctxt);
5294
0
    if (ret == 0) {
5295
        /* Empty branch */
5296
0
  xmlFAGenerateEpsilonTransition(ctxt, previous, to);
5297
0
    } else {
5298
0
  if (xmlFAGenerateTransitions(ctxt, previous,
5299
0
          (CUR=='|' || CUR==')' || CUR==0) ? to : NULL,
5300
0
                ctxt->atom) < 0) {
5301
0
            xmlRegFreeAtom(ctxt->atom);
5302
0
            ctxt->atom = NULL;
5303
0
      return(-1);
5304
0
        }
5305
0
  previous = ctxt->state;
5306
0
  ctxt->atom = NULL;
5307
0
    }
5308
0
    while ((ret != 0) && (ctxt->error == 0)) {
5309
0
  ret = xmlFAParsePiece(ctxt);
5310
0
  if (ret != 0) {
5311
0
      if (xmlFAGenerateTransitions(ctxt, previous,
5312
0
              (CUR=='|' || CUR==')' || CUR==0) ? to : NULL,
5313
0
                    ctxt->atom) < 0) {
5314
0
                xmlRegFreeAtom(ctxt->atom);
5315
0
                ctxt->atom = NULL;
5316
0
                return(-1);
5317
0
            }
5318
0
      previous = ctxt->state;
5319
0
      ctxt->atom = NULL;
5320
0
  }
5321
0
    }
5322
0
    return(0);
5323
0
}
5324
5325
/**
5326
 * xmlFAParseRegExp:
5327
 * @ctxt:  a regexp parser context
5328
 * @top:  is this the top-level expression ?
5329
 *
5330
 * [1]   regExp   ::=     branch  ( '|' branch )*
5331
 */
5332
static void
5333
0
xmlFAParseRegExp(xmlRegParserCtxtPtr ctxt, int top) {
5334
0
    xmlRegStatePtr start, end;
5335
5336
    /* if not top start should have been generated by an epsilon trans */
5337
0
    start = ctxt->state;
5338
0
    ctxt->end = NULL;
5339
0
    xmlFAParseBranch(ctxt, NULL);
5340
0
    if (top) {
5341
0
  ctxt->state->type = XML_REGEXP_FINAL_STATE;
5342
0
    }
5343
0
    if (CUR != '|') {
5344
0
  ctxt->end = ctxt->state;
5345
0
  return;
5346
0
    }
5347
0
    end = ctxt->state;
5348
0
    while ((CUR == '|') && (ctxt->error == 0)) {
5349
0
  NEXT;
5350
0
  ctxt->state = start;
5351
0
  ctxt->end = NULL;
5352
0
  xmlFAParseBranch(ctxt, end);
5353
0
    }
5354
0
    if (!top) {
5355
0
  ctxt->state = end;
5356
0
  ctxt->end = end;
5357
0
    }
5358
0
}
5359
5360
/************************************************************************
5361
 *                  *
5362
 *      The basic API         *
5363
 *                  *
5364
 ************************************************************************/
5365
5366
/**
5367
 * xmlRegexpPrint:
5368
 * @output: the file for the output debug
5369
 * @regexp: the compiled regexp
5370
 *
5371
 * Print the content of the compiled regular expression
5372
 */
5373
void
5374
0
xmlRegexpPrint(FILE *output, xmlRegexpPtr regexp) {
5375
0
    int i;
5376
5377
0
    if (output == NULL)
5378
0
        return;
5379
0
    fprintf(output, " regexp: ");
5380
0
    if (regexp == NULL) {
5381
0
  fprintf(output, "NULL\n");
5382
0
  return;
5383
0
    }
5384
0
    fprintf(output, "'%s' ", regexp->string);
5385
0
    fprintf(output, "\n");
5386
0
    fprintf(output, "%d atoms:\n", regexp->nbAtoms);
5387
0
    for (i = 0;i < regexp->nbAtoms; i++) {
5388
0
  fprintf(output, " %02d ", i);
5389
0
  xmlRegPrintAtom(output, regexp->atoms[i]);
5390
0
    }
5391
0
    fprintf(output, "%d states:", regexp->nbStates);
5392
0
    fprintf(output, "\n");
5393
0
    for (i = 0;i < regexp->nbStates; i++) {
5394
0
  xmlRegPrintState(output, regexp->states[i]);
5395
0
    }
5396
0
    fprintf(output, "%d counters:\n", regexp->nbCounters);
5397
0
    for (i = 0;i < regexp->nbCounters; i++) {
5398
0
  fprintf(output, " %d: min %d max %d\n", i, regexp->counters[i].min,
5399
0
                                    regexp->counters[i].max);
5400
0
    }
5401
0
}
5402
5403
/**
5404
 * xmlRegexpCompile:
5405
 * @regexp:  a regular expression string
5406
 *
5407
 * Parses a regular expression conforming to XML Schemas Part 2 Datatype
5408
 * Appendix F and builds an automata suitable for testing strings against
5409
 * that regular expression
5410
 *
5411
 * Returns the compiled expression or NULL in case of error
5412
 */
5413
xmlRegexpPtr
5414
0
xmlRegexpCompile(const xmlChar *regexp) {
5415
0
    xmlRegexpPtr ret = NULL;
5416
0
    xmlRegParserCtxtPtr ctxt;
5417
5418
0
    if (regexp == NULL)
5419
0
        return(NULL);
5420
5421
0
    ctxt = xmlRegNewParserCtxt(regexp);
5422
0
    if (ctxt == NULL)
5423
0
  return(NULL);
5424
5425
    /* initialize the parser */
5426
0
    ctxt->state = xmlRegStatePush(ctxt);
5427
0
    if (ctxt->state == NULL)
5428
0
        goto error;
5429
0
    ctxt->start = ctxt->state;
5430
0
    ctxt->end = NULL;
5431
5432
    /* parse the expression building an automata */
5433
0
    xmlFAParseRegExp(ctxt, 1);
5434
0
    if (CUR != 0) {
5435
0
  ERROR("xmlFAParseRegExp: extra characters");
5436
0
    }
5437
0
    if (ctxt->error != 0)
5438
0
        goto error;
5439
0
    ctxt->end = ctxt->state;
5440
0
    ctxt->start->type = XML_REGEXP_START_STATE;
5441
0
    ctxt->end->type = XML_REGEXP_FINAL_STATE;
5442
5443
    /* remove the Epsilon except for counted transitions */
5444
0
    xmlFAEliminateEpsilonTransitions(ctxt);
5445
5446
5447
0
    if (ctxt->error != 0)
5448
0
        goto error;
5449
0
    ret = xmlRegEpxFromParse(ctxt);
5450
5451
0
error:
5452
0
    xmlRegFreeParserCtxt(ctxt);
5453
0
    return(ret);
5454
0
}
5455
5456
/**
5457
 * xmlRegexpExec:
5458
 * @comp:  the compiled regular expression
5459
 * @content:  the value to check against the regular expression
5460
 *
5461
 * Check if the regular expression generates the value
5462
 *
5463
 * Returns 1 if it matches, 0 if not and a negative value in case of error
5464
 */
5465
int
5466
0
xmlRegexpExec(xmlRegexpPtr comp, const xmlChar *content) {
5467
0
    if ((comp == NULL) || (content == NULL))
5468
0
  return(-1);
5469
0
    return(xmlFARegExec(comp, content));
5470
0
}
5471
5472
/**
5473
 * xmlRegexpIsDeterminist:
5474
 * @comp:  the compiled regular expression
5475
 *
5476
 * Check if the regular expression is determinist
5477
 *
5478
 * Returns 1 if it yes, 0 if not and a negative value in case of error
5479
 */
5480
int
5481
0
xmlRegexpIsDeterminist(xmlRegexpPtr comp) {
5482
0
    xmlAutomataPtr am;
5483
0
    int ret;
5484
5485
0
    if (comp == NULL)
5486
0
  return(-1);
5487
0
    if (comp->determinist != -1)
5488
0
  return(comp->determinist);
5489
5490
0
    am = xmlNewAutomata();
5491
0
    if (am == NULL)
5492
0
        return(-1);
5493
0
    if (am->states != NULL) {
5494
0
  int i;
5495
5496
0
  for (i = 0;i < am->nbStates;i++)
5497
0
      xmlRegFreeState(am->states[i]);
5498
0
  xmlFree(am->states);
5499
0
    }
5500
0
    am->nbAtoms = comp->nbAtoms;
5501
0
    am->atoms = comp->atoms;
5502
0
    am->nbStates = comp->nbStates;
5503
0
    am->states = comp->states;
5504
0
    am->determinist = -1;
5505
0
    am->flags = comp->flags;
5506
0
    ret = xmlFAComputesDeterminism(am);
5507
0
    am->atoms = NULL;
5508
0
    am->states = NULL;
5509
0
    xmlFreeAutomata(am);
5510
0
    comp->determinist = ret;
5511
0
    return(ret);
5512
0
}
5513
5514
/**
5515
 * xmlRegFreeRegexp:
5516
 * @regexp:  the regexp
5517
 *
5518
 * Free a regexp
5519
 */
5520
void
5521
0
xmlRegFreeRegexp(xmlRegexpPtr regexp) {
5522
0
    int i;
5523
0
    if (regexp == NULL)
5524
0
  return;
5525
5526
0
    if (regexp->string != NULL)
5527
0
  xmlFree(regexp->string);
5528
0
    if (regexp->states != NULL) {
5529
0
  for (i = 0;i < regexp->nbStates;i++)
5530
0
      xmlRegFreeState(regexp->states[i]);
5531
0
  xmlFree(regexp->states);
5532
0
    }
5533
0
    if (regexp->atoms != NULL) {
5534
0
  for (i = 0;i < regexp->nbAtoms;i++)
5535
0
      xmlRegFreeAtom(regexp->atoms[i]);
5536
0
  xmlFree(regexp->atoms);
5537
0
    }
5538
0
    if (regexp->counters != NULL)
5539
0
  xmlFree(regexp->counters);
5540
0
    if (regexp->compact != NULL)
5541
0
  xmlFree(regexp->compact);
5542
0
    if (regexp->transdata != NULL)
5543
0
  xmlFree(regexp->transdata);
5544
0
    if (regexp->stringMap != NULL) {
5545
0
  for (i = 0; i < regexp->nbstrings;i++)
5546
0
      xmlFree(regexp->stringMap[i]);
5547
0
  xmlFree(regexp->stringMap);
5548
0
    }
5549
5550
0
    xmlFree(regexp);
5551
0
}
5552
5553
#ifdef LIBXML_AUTOMATA_ENABLED
5554
/************************************************************************
5555
 *                  *
5556
 *      The Automata interface        *
5557
 *                  *
5558
 ************************************************************************/
5559
5560
/**
5561
 * xmlNewAutomata:
5562
 *
5563
 * Create a new automata
5564
 *
5565
 * Returns the new object or NULL in case of failure
5566
 */
5567
xmlAutomataPtr
5568
0
xmlNewAutomata(void) {
5569
0
    xmlAutomataPtr ctxt;
5570
5571
0
    ctxt = xmlRegNewParserCtxt(NULL);
5572
0
    if (ctxt == NULL)
5573
0
  return(NULL);
5574
5575
    /* initialize the parser */
5576
0
    ctxt->state = xmlRegStatePush(ctxt);
5577
0
    if (ctxt->state == NULL) {
5578
0
  xmlFreeAutomata(ctxt);
5579
0
  return(NULL);
5580
0
    }
5581
0
    ctxt->start = ctxt->state;
5582
0
    ctxt->end = NULL;
5583
5584
0
    ctxt->start->type = XML_REGEXP_START_STATE;
5585
0
    ctxt->flags = 0;
5586
5587
0
    return(ctxt);
5588
0
}
5589
5590
/**
5591
 * xmlFreeAutomata:
5592
 * @am: an automata
5593
 *
5594
 * Free an automata
5595
 */
5596
void
5597
0
xmlFreeAutomata(xmlAutomataPtr am) {
5598
0
    if (am == NULL)
5599
0
  return;
5600
0
    xmlRegFreeParserCtxt(am);
5601
0
}
5602
5603
/**
5604
 * xmlAutomataSetFlags:
5605
 * @am: an automata
5606
 * @flags:  a set of internal flags
5607
 *
5608
 * Set some flags on the automata
5609
 */
5610
void
5611
0
xmlAutomataSetFlags(xmlAutomataPtr am, int flags) {
5612
0
    if (am == NULL)
5613
0
  return;
5614
0
    am->flags |= flags;
5615
0
}
5616
5617
/**
5618
 * xmlAutomataGetInitState:
5619
 * @am: an automata
5620
 *
5621
 * Initial state lookup
5622
 *
5623
 * Returns the initial state of the automata
5624
 */
5625
xmlAutomataStatePtr
5626
0
xmlAutomataGetInitState(xmlAutomataPtr am) {
5627
0
    if (am == NULL)
5628
0
  return(NULL);
5629
0
    return(am->start);
5630
0
}
5631
5632
/**
5633
 * xmlAutomataSetFinalState:
5634
 * @am: an automata
5635
 * @state: a state in this automata
5636
 *
5637
 * Makes that state a final state
5638
 *
5639
 * Returns 0 or -1 in case of error
5640
 */
5641
int
5642
0
xmlAutomataSetFinalState(xmlAutomataPtr am, xmlAutomataStatePtr state) {
5643
0
    if ((am == NULL) || (state == NULL))
5644
0
  return(-1);
5645
0
    state->type = XML_REGEXP_FINAL_STATE;
5646
0
    return(0);
5647
0
}
5648
5649
/**
5650
 * xmlAutomataNewTransition:
5651
 * @am: an automata
5652
 * @from: the starting point of the transition
5653
 * @to: the target point of the transition or NULL
5654
 * @token: the input string associated to that transition
5655
 * @data: data passed to the callback function if the transition is activated
5656
 *
5657
 * If @to is NULL, this creates first a new target state in the automata
5658
 * and then adds a transition from the @from state to the target state
5659
 * activated by the value of @token
5660
 *
5661
 * Returns the target state or NULL in case of error
5662
 */
5663
xmlAutomataStatePtr
5664
xmlAutomataNewTransition(xmlAutomataPtr am, xmlAutomataStatePtr from,
5665
       xmlAutomataStatePtr to, const xmlChar *token,
5666
0
       void *data) {
5667
0
    xmlRegAtomPtr atom;
5668
5669
0
    if ((am == NULL) || (from == NULL) || (token == NULL))
5670
0
  return(NULL);
5671
0
    atom = xmlRegNewAtom(am, XML_REGEXP_STRING);
5672
0
    if (atom == NULL)
5673
0
        return(NULL);
5674
0
    atom->data = data;
5675
0
    atom->valuep = xmlStrdup(token);
5676
0
    if (atom->valuep == NULL) {
5677
0
        xmlRegFreeAtom(atom);
5678
0
        xmlRegexpErrMemory(am);
5679
0
        return(NULL);
5680
0
    }
5681
5682
0
    if (xmlFAGenerateTransitions(am, from, to, atom) < 0) {
5683
0
        xmlRegFreeAtom(atom);
5684
0
  return(NULL);
5685
0
    }
5686
0
    if (to == NULL)
5687
0
  return(am->state);
5688
0
    return(to);
5689
0
}
5690
5691
/**
5692
 * xmlAutomataNewTransition2:
5693
 * @am: an automata
5694
 * @from: the starting point of the transition
5695
 * @to: the target point of the transition or NULL
5696
 * @token: the first input string associated to that transition
5697
 * @token2: the second input string associated to that transition
5698
 * @data: data passed to the callback function if the transition is activated
5699
 *
5700
 * If @to is NULL, this creates first a new target state in the automata
5701
 * and then adds a transition from the @from state to the target state
5702
 * activated by the value of @token
5703
 *
5704
 * Returns the target state or NULL in case of error
5705
 */
5706
xmlAutomataStatePtr
5707
xmlAutomataNewTransition2(xmlAutomataPtr am, xmlAutomataStatePtr from,
5708
        xmlAutomataStatePtr to, const xmlChar *token,
5709
0
        const xmlChar *token2, void *data) {
5710
0
    xmlRegAtomPtr atom;
5711
5712
0
    if ((am == NULL) || (from == NULL) || (token == NULL))
5713
0
  return(NULL);
5714
0
    atom = xmlRegNewAtom(am, XML_REGEXP_STRING);
5715
0
    if (atom == NULL)
5716
0
  return(NULL);
5717
0
    atom->data = data;
5718
0
    if ((token2 == NULL) || (*token2 == 0)) {
5719
0
  atom->valuep = xmlStrdup(token);
5720
0
    } else {
5721
0
  int lenn, lenp;
5722
0
  xmlChar *str;
5723
5724
0
  lenn = strlen((char *) token2);
5725
0
  lenp = strlen((char *) token);
5726
5727
0
  str = (xmlChar *) xmlMallocAtomic(lenn + lenp + 2);
5728
0
  if (str == NULL) {
5729
0
      xmlRegFreeAtom(atom);
5730
0
      return(NULL);
5731
0
  }
5732
0
  memcpy(&str[0], token, lenp);
5733
0
  str[lenp] = '|';
5734
0
  memcpy(&str[lenp + 1], token2, lenn);
5735
0
  str[lenn + lenp + 1] = 0;
5736
5737
0
  atom->valuep = str;
5738
0
    }
5739
5740
0
    if (xmlFAGenerateTransitions(am, from, to, atom) < 0) {
5741
0
        xmlRegFreeAtom(atom);
5742
0
  return(NULL);
5743
0
    }
5744
0
    if (to == NULL)
5745
0
  return(am->state);
5746
0
    return(to);
5747
0
}
5748
5749
/**
5750
 * xmlAutomataNewNegTrans:
5751
 * @am: an automata
5752
 * @from: the starting point of the transition
5753
 * @to: the target point of the transition or NULL
5754
 * @token: the first input string associated to that transition
5755
 * @token2: the second input string associated to that transition
5756
 * @data: data passed to the callback function if the transition is activated
5757
 *
5758
 * If @to is NULL, this creates first a new target state in the automata
5759
 * and then adds a transition from the @from state to the target state
5760
 * activated by any value except (@token,@token2)
5761
 * Note that if @token2 is not NULL, then (X, NULL) won't match to follow
5762
 # the semantic of XSD ##other
5763
 *
5764
 * Returns the target state or NULL in case of error
5765
 */
5766
xmlAutomataStatePtr
5767
xmlAutomataNewNegTrans(xmlAutomataPtr am, xmlAutomataStatePtr from,
5768
           xmlAutomataStatePtr to, const xmlChar *token,
5769
0
           const xmlChar *token2, void *data) {
5770
0
    xmlRegAtomPtr atom;
5771
0
    xmlChar err_msg[200];
5772
5773
0
    if ((am == NULL) || (from == NULL) || (token == NULL))
5774
0
  return(NULL);
5775
0
    atom = xmlRegNewAtom(am, XML_REGEXP_STRING);
5776
0
    if (atom == NULL)
5777
0
  return(NULL);
5778
0
    atom->data = data;
5779
0
    atom->neg = 1;
5780
0
    if ((token2 == NULL) || (*token2 == 0)) {
5781
0
  atom->valuep = xmlStrdup(token);
5782
0
    } else {
5783
0
  int lenn, lenp;
5784
0
  xmlChar *str;
5785
5786
0
  lenn = strlen((char *) token2);
5787
0
  lenp = strlen((char *) token);
5788
5789
0
  str = (xmlChar *) xmlMallocAtomic(lenn + lenp + 2);
5790
0
  if (str == NULL) {
5791
0
      xmlRegFreeAtom(atom);
5792
0
      return(NULL);
5793
0
  }
5794
0
  memcpy(&str[0], token, lenp);
5795
0
  str[lenp] = '|';
5796
0
  memcpy(&str[lenp + 1], token2, lenn);
5797
0
  str[lenn + lenp + 1] = 0;
5798
5799
0
  atom->valuep = str;
5800
0
    }
5801
0
    snprintf((char *) err_msg, 199, "not %s", (const char *) atom->valuep);
5802
0
    err_msg[199] = 0;
5803
0
    atom->valuep2 = xmlStrdup(err_msg);
5804
5805
0
    if (xmlFAGenerateTransitions(am, from, to, atom) < 0) {
5806
0
        xmlRegFreeAtom(atom);
5807
0
  return(NULL);
5808
0
    }
5809
0
    am->negs++;
5810
0
    if (to == NULL)
5811
0
  return(am->state);
5812
0
    return(to);
5813
0
}
5814
5815
/**
5816
 * xmlAutomataNewCountTrans2:
5817
 * @am: an automata
5818
 * @from: the starting point of the transition
5819
 * @to: the target point of the transition or NULL
5820
 * @token: the input string associated to that transition
5821
 * @token2: the second input string associated to that transition
5822
 * @min:  the minimum successive occurrences of token
5823
 * @max:  the maximum successive occurrences of token
5824
 * @data:  data associated to the transition
5825
 *
5826
 * If @to is NULL, this creates first a new target state in the automata
5827
 * and then adds a transition from the @from state to the target state
5828
 * activated by a succession of input of value @token and @token2 and
5829
 * whose number is between @min and @max
5830
 *
5831
 * Returns the target state or NULL in case of error
5832
 */
5833
xmlAutomataStatePtr
5834
xmlAutomataNewCountTrans2(xmlAutomataPtr am, xmlAutomataStatePtr from,
5835
       xmlAutomataStatePtr to, const xmlChar *token,
5836
       const xmlChar *token2,
5837
0
       int min, int max, void *data) {
5838
0
    xmlRegAtomPtr atom;
5839
0
    int counter;
5840
5841
0
    if ((am == NULL) || (from == NULL) || (token == NULL))
5842
0
  return(NULL);
5843
0
    if (min < 0)
5844
0
  return(NULL);
5845
0
    if ((max < min) || (max < 1))
5846
0
  return(NULL);
5847
0
    atom = xmlRegNewAtom(am, XML_REGEXP_STRING);
5848
0
    if (atom == NULL)
5849
0
  return(NULL);
5850
0
    if ((token2 == NULL) || (*token2 == 0)) {
5851
0
  atom->valuep = xmlStrdup(token);
5852
0
        if (atom->valuep == NULL)
5853
0
            goto error;
5854
0
    } else {
5855
0
  int lenn, lenp;
5856
0
  xmlChar *str;
5857
5858
0
  lenn = strlen((char *) token2);
5859
0
  lenp = strlen((char *) token);
5860
5861
0
  str = (xmlChar *) xmlMallocAtomic(lenn + lenp + 2);
5862
0
  if (str == NULL)
5863
0
      goto error;
5864
0
  memcpy(&str[0], token, lenp);
5865
0
  str[lenp] = '|';
5866
0
  memcpy(&str[lenp + 1], token2, lenn);
5867
0
  str[lenn + lenp + 1] = 0;
5868
5869
0
  atom->valuep = str;
5870
0
    }
5871
0
    atom->data = data;
5872
0
    if (min == 0)
5873
0
  atom->min = 1;
5874
0
    else
5875
0
  atom->min = min;
5876
0
    atom->max = max;
5877
5878
    /*
5879
     * associate a counter to the transition.
5880
     */
5881
0
    counter = xmlRegGetCounter(am);
5882
0
    if (counter < 0)
5883
0
        goto error;
5884
0
    am->counters[counter].min = min;
5885
0
    am->counters[counter].max = max;
5886
5887
    /* xmlFAGenerateTransitions(am, from, to, atom); */
5888
0
    if (to == NULL) {
5889
0
  to = xmlRegStatePush(am);
5890
0
        if (to == NULL)
5891
0
            goto error;
5892
0
    }
5893
0
    xmlRegStateAddTrans(am, from, atom, to, counter, -1);
5894
0
    if (xmlRegAtomPush(am, atom) < 0)
5895
0
        goto error;
5896
0
    am->state = to;
5897
5898
0
    if (to == NULL)
5899
0
  to = am->state;
5900
0
    if (to == NULL)
5901
0
  return(NULL);
5902
0
    if (min == 0)
5903
0
  xmlFAGenerateEpsilonTransition(am, from, to);
5904
0
    return(to);
5905
5906
0
error:
5907
0
    xmlRegFreeAtom(atom);
5908
0
    return(NULL);
5909
0
}
5910
5911
/**
5912
 * xmlAutomataNewCountTrans:
5913
 * @am: an automata
5914
 * @from: the starting point of the transition
5915
 * @to: the target point of the transition or NULL
5916
 * @token: the input string associated to that transition
5917
 * @min:  the minimum successive occurrences of token
5918
 * @max:  the maximum successive occurrences of token
5919
 * @data:  data associated to the transition
5920
 *
5921
 * If @to is NULL, this creates first a new target state in the automata
5922
 * and then adds a transition from the @from state to the target state
5923
 * activated by a succession of input of value @token and whose number
5924
 * is between @min and @max
5925
 *
5926
 * Returns the target state or NULL in case of error
5927
 */
5928
xmlAutomataStatePtr
5929
xmlAutomataNewCountTrans(xmlAutomataPtr am, xmlAutomataStatePtr from,
5930
       xmlAutomataStatePtr to, const xmlChar *token,
5931
0
       int min, int max, void *data) {
5932
0
    xmlRegAtomPtr atom;
5933
0
    int counter;
5934
5935
0
    if ((am == NULL) || (from == NULL) || (token == NULL))
5936
0
  return(NULL);
5937
0
    if (min < 0)
5938
0
  return(NULL);
5939
0
    if ((max < min) || (max < 1))
5940
0
  return(NULL);
5941
0
    atom = xmlRegNewAtom(am, XML_REGEXP_STRING);
5942
0
    if (atom == NULL)
5943
0
  return(NULL);
5944
0
    atom->valuep = xmlStrdup(token);
5945
0
    if (atom->valuep == NULL)
5946
0
        goto error;
5947
0
    atom->data = data;
5948
0
    if (min == 0)
5949
0
  atom->min = 1;
5950
0
    else
5951
0
  atom->min = min;
5952
0
    atom->max = max;
5953
5954
    /*
5955
     * associate a counter to the transition.
5956
     */
5957
0
    counter = xmlRegGetCounter(am);
5958
0
    if (counter < 0)
5959
0
        goto error;
5960
0
    am->counters[counter].min = min;
5961
0
    am->counters[counter].max = max;
5962
5963
    /* xmlFAGenerateTransitions(am, from, to, atom); */
5964
0
    if (to == NULL) {
5965
0
  to = xmlRegStatePush(am);
5966
0
        if (to == NULL)
5967
0
            goto error;
5968
0
    }
5969
0
    xmlRegStateAddTrans(am, from, atom, to, counter, -1);
5970
0
    if (xmlRegAtomPush(am, atom) < 0)
5971
0
        goto error;
5972
0
    am->state = to;
5973
5974
0
    if (to == NULL)
5975
0
  to = am->state;
5976
0
    if (to == NULL)
5977
0
  return(NULL);
5978
0
    if (min == 0)
5979
0
  xmlFAGenerateEpsilonTransition(am, from, to);
5980
0
    return(to);
5981
5982
0
error:
5983
0
    xmlRegFreeAtom(atom);
5984
0
    return(NULL);
5985
0
}
5986
5987
/**
5988
 * xmlAutomataNewOnceTrans2:
5989
 * @am: an automata
5990
 * @from: the starting point of the transition
5991
 * @to: the target point of the transition or NULL
5992
 * @token: the input string associated to that transition
5993
 * @token2: the second input string associated to that transition
5994
 * @min:  the minimum successive occurrences of token
5995
 * @max:  the maximum successive occurrences of token
5996
 * @data:  data associated to the transition
5997
 *
5998
 * If @to is NULL, this creates first a new target state in the automata
5999
 * and then adds a transition from the @from state to the target state
6000
 * activated by a succession of input of value @token and @token2 and whose
6001
 * number is between @min and @max, moreover that transition can only be
6002
 * crossed once.
6003
 *
6004
 * Returns the target state or NULL in case of error
6005
 */
6006
xmlAutomataStatePtr
6007
xmlAutomataNewOnceTrans2(xmlAutomataPtr am, xmlAutomataStatePtr from,
6008
       xmlAutomataStatePtr to, const xmlChar *token,
6009
       const xmlChar *token2,
6010
0
       int min, int max, void *data) {
6011
0
    xmlRegAtomPtr atom;
6012
0
    int counter;
6013
6014
0
    if ((am == NULL) || (from == NULL) || (token == NULL))
6015
0
  return(NULL);
6016
0
    if (min < 1)
6017
0
  return(NULL);
6018
0
    if (max < min)
6019
0
  return(NULL);
6020
0
    atom = xmlRegNewAtom(am, XML_REGEXP_STRING);
6021
0
    if (atom == NULL)
6022
0
  return(NULL);
6023
0
    if ((token2 == NULL) || (*token2 == 0)) {
6024
0
  atom->valuep = xmlStrdup(token);
6025
0
        if (atom->valuep == NULL)
6026
0
            goto error;
6027
0
    } else {
6028
0
  int lenn, lenp;
6029
0
  xmlChar *str;
6030
6031
0
  lenn = strlen((char *) token2);
6032
0
  lenp = strlen((char *) token);
6033
6034
0
  str = (xmlChar *) xmlMallocAtomic(lenn + lenp + 2);
6035
0
  if (str == NULL)
6036
0
      goto error;
6037
0
  memcpy(&str[0], token, lenp);
6038
0
  str[lenp] = '|';
6039
0
  memcpy(&str[lenp + 1], token2, lenn);
6040
0
  str[lenn + lenp + 1] = 0;
6041
6042
0
  atom->valuep = str;
6043
0
    }
6044
0
    atom->data = data;
6045
0
    atom->quant = XML_REGEXP_QUANT_ONCEONLY;
6046
0
    atom->min = min;
6047
0
    atom->max = max;
6048
    /*
6049
     * associate a counter to the transition.
6050
     */
6051
0
    counter = xmlRegGetCounter(am);
6052
0
    if (counter < 0)
6053
0
        goto error;
6054
0
    am->counters[counter].min = 1;
6055
0
    am->counters[counter].max = 1;
6056
6057
    /* xmlFAGenerateTransitions(am, from, to, atom); */
6058
0
    if (to == NULL) {
6059
0
  to = xmlRegStatePush(am);
6060
0
        if (to == NULL)
6061
0
            goto error;
6062
0
    }
6063
0
    xmlRegStateAddTrans(am, from, atom, to, counter, -1);
6064
0
    if (xmlRegAtomPush(am, atom) < 0)
6065
0
        goto error;
6066
0
    am->state = to;
6067
0
    return(to);
6068
6069
0
error:
6070
0
    xmlRegFreeAtom(atom);
6071
0
    return(NULL);
6072
0
}
6073
6074
6075
6076
/**
6077
 * xmlAutomataNewOnceTrans:
6078
 * @am: an automata
6079
 * @from: the starting point of the transition
6080
 * @to: the target point of the transition or NULL
6081
 * @token: the input string associated to that transition
6082
 * @min:  the minimum successive occurrences of token
6083
 * @max:  the maximum successive occurrences of token
6084
 * @data:  data associated to the transition
6085
 *
6086
 * If @to is NULL, this creates first a new target state in the automata
6087
 * and then adds a transition from the @from state to the target state
6088
 * activated by a succession of input of value @token and whose number
6089
 * is between @min and @max, moreover that transition can only be crossed
6090
 * once.
6091
 *
6092
 * Returns the target state or NULL in case of error
6093
 */
6094
xmlAutomataStatePtr
6095
xmlAutomataNewOnceTrans(xmlAutomataPtr am, xmlAutomataStatePtr from,
6096
       xmlAutomataStatePtr to, const xmlChar *token,
6097
0
       int min, int max, void *data) {
6098
0
    xmlRegAtomPtr atom;
6099
0
    int counter;
6100
6101
0
    if ((am == NULL) || (from == NULL) || (token == NULL))
6102
0
  return(NULL);
6103
0
    if (min < 1)
6104
0
  return(NULL);
6105
0
    if (max < min)
6106
0
  return(NULL);
6107
0
    atom = xmlRegNewAtom(am, XML_REGEXP_STRING);
6108
0
    if (atom == NULL)
6109
0
  return(NULL);
6110
0
    atom->valuep = xmlStrdup(token);
6111
0
    atom->data = data;
6112
0
    atom->quant = XML_REGEXP_QUANT_ONCEONLY;
6113
0
    atom->min = min;
6114
0
    atom->max = max;
6115
    /*
6116
     * associate a counter to the transition.
6117
     */
6118
0
    counter = xmlRegGetCounter(am);
6119
0
    if (counter < 0)
6120
0
        goto error;
6121
0
    am->counters[counter].min = 1;
6122
0
    am->counters[counter].max = 1;
6123
6124
    /* xmlFAGenerateTransitions(am, from, to, atom); */
6125
0
    if (to == NULL) {
6126
0
  to = xmlRegStatePush(am);
6127
0
        if (to == NULL)
6128
0
            goto error;
6129
0
    }
6130
0
    xmlRegStateAddTrans(am, from, atom, to, counter, -1);
6131
0
    if (xmlRegAtomPush(am, atom) < 0)
6132
0
        goto error;
6133
0
    am->state = to;
6134
0
    return(to);
6135
6136
0
error:
6137
0
    xmlRegFreeAtom(atom);
6138
0
    return(NULL);
6139
0
}
6140
6141
/**
6142
 * xmlAutomataNewState:
6143
 * @am: an automata
6144
 *
6145
 * Create a new disconnected state in the automata
6146
 *
6147
 * Returns the new state or NULL in case of error
6148
 */
6149
xmlAutomataStatePtr
6150
0
xmlAutomataNewState(xmlAutomataPtr am) {
6151
0
    if (am == NULL)
6152
0
  return(NULL);
6153
0
    return(xmlRegStatePush(am));
6154
0
}
6155
6156
/**
6157
 * xmlAutomataNewEpsilon:
6158
 * @am: an automata
6159
 * @from: the starting point of the transition
6160
 * @to: the target point of the transition or NULL
6161
 *
6162
 * If @to is NULL, this creates first a new target state in the automata
6163
 * and then adds an epsilon transition from the @from state to the
6164
 * target state
6165
 *
6166
 * Returns the target state or NULL in case of error
6167
 */
6168
xmlAutomataStatePtr
6169
xmlAutomataNewEpsilon(xmlAutomataPtr am, xmlAutomataStatePtr from,
6170
0
          xmlAutomataStatePtr to) {
6171
0
    if ((am == NULL) || (from == NULL))
6172
0
  return(NULL);
6173
0
    xmlFAGenerateEpsilonTransition(am, from, to);
6174
0
    if (to == NULL)
6175
0
  return(am->state);
6176
0
    return(to);
6177
0
}
6178
6179
/**
6180
 * xmlAutomataNewAllTrans:
6181
 * @am: an automata
6182
 * @from: the starting point of the transition
6183
 * @to: the target point of the transition or NULL
6184
 * @lax: allow to transition if not all all transitions have been activated
6185
 *
6186
 * If @to is NULL, this creates first a new target state in the automata
6187
 * and then adds a an ALL transition from the @from state to the
6188
 * target state. That transition is an epsilon transition allowed only when
6189
 * all transitions from the @from node have been activated.
6190
 *
6191
 * Returns the target state or NULL in case of error
6192
 */
6193
xmlAutomataStatePtr
6194
xmlAutomataNewAllTrans(xmlAutomataPtr am, xmlAutomataStatePtr from,
6195
0
           xmlAutomataStatePtr to, int lax) {
6196
0
    if ((am == NULL) || (from == NULL))
6197
0
  return(NULL);
6198
0
    xmlFAGenerateAllTransition(am, from, to, lax);
6199
0
    if (to == NULL)
6200
0
  return(am->state);
6201
0
    return(to);
6202
0
}
6203
6204
/**
6205
 * xmlAutomataNewCounter:
6206
 * @am: an automata
6207
 * @min:  the minimal value on the counter
6208
 * @max:  the maximal value on the counter
6209
 *
6210
 * Create a new counter
6211
 *
6212
 * Returns the counter number or -1 in case of error
6213
 */
6214
int
6215
0
xmlAutomataNewCounter(xmlAutomataPtr am, int min, int max) {
6216
0
    int ret;
6217
6218
0
    if (am == NULL)
6219
0
  return(-1);
6220
6221
0
    ret = xmlRegGetCounter(am);
6222
0
    if (ret < 0)
6223
0
  return(-1);
6224
0
    am->counters[ret].min = min;
6225
0
    am->counters[ret].max = max;
6226
0
    return(ret);
6227
0
}
6228
6229
/**
6230
 * xmlAutomataNewCountedTrans:
6231
 * @am: an automata
6232
 * @from: the starting point of the transition
6233
 * @to: the target point of the transition or NULL
6234
 * @counter: the counter associated to that transition
6235
 *
6236
 * If @to is NULL, this creates first a new target state in the automata
6237
 * and then adds an epsilon transition from the @from state to the target state
6238
 * which will increment the counter provided
6239
 *
6240
 * Returns the target state or NULL in case of error
6241
 */
6242
xmlAutomataStatePtr
6243
xmlAutomataNewCountedTrans(xmlAutomataPtr am, xmlAutomataStatePtr from,
6244
0
    xmlAutomataStatePtr to, int counter) {
6245
0
    if ((am == NULL) || (from == NULL) || (counter < 0))
6246
0
  return(NULL);
6247
0
    xmlFAGenerateCountedEpsilonTransition(am, from, to, counter);
6248
0
    if (to == NULL)
6249
0
  return(am->state);
6250
0
    return(to);
6251
0
}
6252
6253
/**
6254
 * xmlAutomataNewCounterTrans:
6255
 * @am: an automata
6256
 * @from: the starting point of the transition
6257
 * @to: the target point of the transition or NULL
6258
 * @counter: the counter associated to that transition
6259
 *
6260
 * If @to is NULL, this creates first a new target state in the automata
6261
 * and then adds an epsilon transition from the @from state to the target state
6262
 * which will be allowed only if the counter is within the right range.
6263
 *
6264
 * Returns the target state or NULL in case of error
6265
 */
6266
xmlAutomataStatePtr
6267
xmlAutomataNewCounterTrans(xmlAutomataPtr am, xmlAutomataStatePtr from,
6268
0
    xmlAutomataStatePtr to, int counter) {
6269
0
    if ((am == NULL) || (from == NULL) || (counter < 0))
6270
0
  return(NULL);
6271
0
    xmlFAGenerateCountedTransition(am, from, to, counter);
6272
0
    if (to == NULL)
6273
0
  return(am->state);
6274
0
    return(to);
6275
0
}
6276
6277
/**
6278
 * xmlAutomataCompile:
6279
 * @am: an automata
6280
 *
6281
 * Compile the automata into a Reg Exp ready for being executed.
6282
 * The automata should be free after this point.
6283
 *
6284
 * Returns the compiled regexp or NULL in case of error
6285
 */
6286
xmlRegexpPtr
6287
0
xmlAutomataCompile(xmlAutomataPtr am) {
6288
0
    xmlRegexpPtr ret;
6289
6290
0
    if ((am == NULL) || (am->error != 0)) return(NULL);
6291
0
    xmlFAEliminateEpsilonTransitions(am);
6292
0
    if (am->error != 0)
6293
0
        return(NULL);
6294
    /* xmlFAComputesDeterminism(am); */
6295
0
    ret = xmlRegEpxFromParse(am);
6296
6297
0
    return(ret);
6298
0
}
6299
6300
/**
6301
 * xmlAutomataIsDeterminist:
6302
 * @am: an automata
6303
 *
6304
 * Checks if an automata is determinist.
6305
 *
6306
 * Returns 1 if true, 0 if not, and -1 in case of error
6307
 */
6308
int
6309
0
xmlAutomataIsDeterminist(xmlAutomataPtr am) {
6310
0
    int ret;
6311
6312
0
    if (am == NULL)
6313
0
  return(-1);
6314
6315
0
    ret = xmlFAComputesDeterminism(am);
6316
0
    return(ret);
6317
0
}
6318
#endif /* LIBXML_AUTOMATA_ENABLED */
6319
6320
#ifdef LIBXML_EXPR_ENABLED
6321
/************************************************************************
6322
 *                  *
6323
 *    Formal Expression handling code       *
6324
 *                  *
6325
 ************************************************************************/
6326
/************************************************************************
6327
 *                  *
6328
 *    Expression handling context       *
6329
 *                  *
6330
 ************************************************************************/
6331
6332
struct _xmlExpCtxt {
6333
    xmlDictPtr dict;
6334
    xmlExpNodePtr *table;
6335
    int size;
6336
    int nbElems;
6337
    int nb_nodes;
6338
    int maxNodes;
6339
    const char *expr;
6340
    const char *cur;
6341
    int nb_cons;
6342
    int tabSize;
6343
};
6344
6345
/**
6346
 * xmlExpNewCtxt:
6347
 * @maxNodes:  the maximum number of nodes
6348
 * @dict:  optional dictionary to use internally
6349
 *
6350
 * Creates a new context for manipulating expressions
6351
 *
6352
 * Returns the context or NULL in case of error
6353
 */
6354
xmlExpCtxtPtr
6355
xmlExpNewCtxt(int maxNodes, xmlDictPtr dict) {
6356
    xmlExpCtxtPtr ret;
6357
    int size = 256;
6358
6359
    if (maxNodes <= 4096)
6360
        maxNodes = 4096;
6361
6362
    ret = (xmlExpCtxtPtr) xmlMalloc(sizeof(xmlExpCtxt));
6363
    if (ret == NULL)
6364
        return(NULL);
6365
    memset(ret, 0, sizeof(xmlExpCtxt));
6366
    ret->size = size;
6367
    ret->nbElems = 0;
6368
    ret->maxNodes = maxNodes;
6369
    ret->table = xmlMalloc(size * sizeof(xmlExpNodePtr));
6370
    if (ret->table == NULL) {
6371
        xmlFree(ret);
6372
  return(NULL);
6373
    }
6374
    memset(ret->table, 0, size * sizeof(xmlExpNodePtr));
6375
    if (dict == NULL) {
6376
        ret->dict = xmlDictCreate();
6377
  if (ret->dict == NULL) {
6378
      xmlFree(ret->table);
6379
      xmlFree(ret);
6380
      return(NULL);
6381
  }
6382
    } else {
6383
        ret->dict = dict;
6384
  xmlDictReference(ret->dict);
6385
    }
6386
    return(ret);
6387
}
6388
6389
/**
6390
 * xmlExpFreeCtxt:
6391
 * @ctxt:  an expression context
6392
 *
6393
 * Free an expression context
6394
 */
6395
void
6396
xmlExpFreeCtxt(xmlExpCtxtPtr ctxt) {
6397
    if (ctxt == NULL)
6398
        return;
6399
    xmlDictFree(ctxt->dict);
6400
    if (ctxt->table != NULL)
6401
  xmlFree(ctxt->table);
6402
    xmlFree(ctxt);
6403
}
6404
6405
/************************************************************************
6406
 *                  *
6407
 *    Structure associated to an expression node    *
6408
 *                  *
6409
 ************************************************************************/
6410
#define MAX_NODES 10000
6411
6412
/*
6413
 * TODO:
6414
 * - Wildcards
6415
 * - public API for creation
6416
 *
6417
 * Started
6418
 * - regression testing
6419
 *
6420
 * Done
6421
 * - split into module and test tool
6422
 * - memleaks
6423
 */
6424
6425
typedef enum {
6426
    XML_EXP_NILABLE = (1 << 0)
6427
} xmlExpNodeInfo;
6428
6429
#define IS_NILLABLE(node) ((node)->info & XML_EXP_NILABLE)
6430
6431
struct _xmlExpNode {
6432
    unsigned char type;/* xmlExpNodeType */
6433
    unsigned char info;/* OR of xmlExpNodeInfo */
6434
    unsigned short key; /* the hash key */
6435
    unsigned int ref; /* The number of references */
6436
    int c_max;    /* the maximum length it can consume */
6437
    xmlExpNodePtr exp_left;
6438
    xmlExpNodePtr next;/* the next node in the hash table or free list */
6439
    union {
6440
  struct {
6441
      int f_min;
6442
      int f_max;
6443
  } count;
6444
  struct {
6445
      xmlExpNodePtr f_right;
6446
  } children;
6447
        const xmlChar *f_str;
6448
    } field;
6449
};
6450
6451
#define exp_min field.count.f_min
6452
#define exp_max field.count.f_max
6453
/* #define exp_left field.children.f_left */
6454
#define exp_right field.children.f_right
6455
#define exp_str field.f_str
6456
6457
static xmlExpNodePtr xmlExpNewNode(xmlExpCtxtPtr ctxt, xmlExpNodeType type);
6458
static xmlExpNode forbiddenExpNode = {
6459
    XML_EXP_FORBID, 0, 0, 0, 0, NULL, NULL, {{ 0, 0}}
6460
};
6461
xmlExpNodePtr forbiddenExp = &forbiddenExpNode;
6462
static xmlExpNode emptyExpNode = {
6463
    XML_EXP_EMPTY, 1, 0, 0, 0, NULL, NULL, {{ 0, 0}}
6464
};
6465
xmlExpNodePtr emptyExp = &emptyExpNode;
6466
6467
/************************************************************************
6468
 *                  *
6469
 *  The custom hash table for unicity and canonicalization    *
6470
 *  of sub-expressions pointers           *
6471
 *                  *
6472
 ************************************************************************/
6473
/*
6474
 * xmlExpHashNameComputeKey:
6475
 * Calculate the hash key for a token
6476
 */
6477
static unsigned short
6478
xmlExpHashNameComputeKey(const xmlChar *name) {
6479
    unsigned short value = 0L;
6480
    char ch;
6481
6482
    if (name != NULL) {
6483
  value += 30 * (*name);
6484
  while ((ch = *name++) != 0) {
6485
      value = value ^ ((value << 5) + (value >> 3) + (unsigned long)ch);
6486
  }
6487
    }
6488
    return (value);
6489
}
6490
6491
/*
6492
 * xmlExpHashComputeKey:
6493
 * Calculate the hash key for a compound expression
6494
 */
6495
static unsigned short
6496
xmlExpHashComputeKey(xmlExpNodeType type, xmlExpNodePtr left,
6497
                     xmlExpNodePtr right) {
6498
    unsigned long value;
6499
    unsigned short ret;
6500
6501
    switch (type) {
6502
        case XML_EXP_SEQ:
6503
      value = left->key;
6504
      value += right->key;
6505
      value *= 3;
6506
      ret = (unsigned short) value;
6507
      break;
6508
        case XML_EXP_OR:
6509
      value = left->key;
6510
      value += right->key;
6511
      value *= 7;
6512
      ret = (unsigned short) value;
6513
      break;
6514
        case XML_EXP_COUNT:
6515
      value = left->key;
6516
      value += right->key;
6517
      ret = (unsigned short) value;
6518
      break;
6519
  default:
6520
      ret = 0;
6521
    }
6522
    return(ret);
6523
}
6524
6525
6526
static xmlExpNodePtr
6527
xmlExpNewNode(xmlExpCtxtPtr ctxt, xmlExpNodeType type) {
6528
    xmlExpNodePtr ret;
6529
6530
    if (ctxt->nb_nodes >= MAX_NODES)
6531
        return(NULL);
6532
    ret = (xmlExpNodePtr) xmlMalloc(sizeof(xmlExpNode));
6533
    if (ret == NULL)
6534
        return(NULL);
6535
    memset(ret, 0, sizeof(xmlExpNode));
6536
    ret->type = type;
6537
    ret->next = NULL;
6538
    ctxt->nb_nodes++;
6539
    ctxt->nb_cons++;
6540
    return(ret);
6541
}
6542
6543
/**
6544
 * xmlExpHashGetEntry:
6545
 * @table: the hash table
6546
 *
6547
 * Get the unique entry from the hash table. The entry is created if
6548
 * needed. @left and @right are consumed, i.e. their ref count will
6549
 * be decremented by the operation.
6550
 *
6551
 * Returns the pointer or NULL in case of error
6552
 */
6553
static xmlExpNodePtr
6554
xmlExpHashGetEntry(xmlExpCtxtPtr ctxt, xmlExpNodeType type,
6555
                   xmlExpNodePtr left, xmlExpNodePtr right,
6556
       const xmlChar *name, int min, int max) {
6557
    unsigned short kbase, key;
6558
    xmlExpNodePtr entry;
6559
    xmlExpNodePtr insert;
6560
6561
    if (ctxt == NULL)
6562
  return(NULL);
6563
6564
    /*
6565
     * Check for duplicate and insertion location.
6566
     */
6567
    if (type == XML_EXP_ATOM) {
6568
  kbase = xmlExpHashNameComputeKey(name);
6569
    } else if (type == XML_EXP_COUNT) {
6570
        /* COUNT reduction rule 1 */
6571
  /* a{1} -> a */
6572
  if (min == max) {
6573
      if (min == 1) {
6574
    return(left);
6575
      }
6576
      if (min == 0) {
6577
    xmlExpFree(ctxt, left);
6578
          return(emptyExp);
6579
      }
6580
  }
6581
  if (min < 0) {
6582
      xmlExpFree(ctxt, left);
6583
      return(forbiddenExp);
6584
  }
6585
        if (max == -1)
6586
      kbase = min + 79;
6587
  else
6588
      kbase = max - min;
6589
  kbase += left->key;
6590
    } else if (type == XML_EXP_OR) {
6591
        /* Forbid reduction rules */
6592
        if (left->type == XML_EXP_FORBID) {
6593
      xmlExpFree(ctxt, left);
6594
      return(right);
6595
  }
6596
        if (right->type == XML_EXP_FORBID) {
6597
      xmlExpFree(ctxt, right);
6598
      return(left);
6599
  }
6600
6601
        /* OR reduction rule 1 */
6602
  /* a | a reduced to a */
6603
        if (left == right) {
6604
      xmlExpFree(ctxt, right);
6605
      return(left);
6606
  }
6607
        /* OR canonicalization rule 1 */
6608
  /* linearize (a | b) | c into a | (b | c) */
6609
        if ((left->type == XML_EXP_OR) && (right->type != XML_EXP_OR)) {
6610
      xmlExpNodePtr tmp = left;
6611
            left = right;
6612
      right = tmp;
6613
  }
6614
        /* OR reduction rule 2 */
6615
  /* a | (a | b) and b | (a | b) are reduced to a | b */
6616
        if (right->type == XML_EXP_OR) {
6617
      if ((left == right->exp_left) ||
6618
          (left == right->exp_right)) {
6619
    xmlExpFree(ctxt, left);
6620
    return(right);
6621
      }
6622
  }
6623
        /* OR canonicalization rule 2 */
6624
  /* linearize (a | b) | c into a | (b | c) */
6625
        if (left->type == XML_EXP_OR) {
6626
      xmlExpNodePtr tmp;
6627
6628
      /* OR canonicalization rule 2 */
6629
      if ((left->exp_right->type != XML_EXP_OR) &&
6630
          (left->exp_right->key < left->exp_left->key)) {
6631
          tmp = left->exp_right;
6632
    left->exp_right = left->exp_left;
6633
    left->exp_left = tmp;
6634
      }
6635
      left->exp_right->ref++;
6636
      tmp = xmlExpHashGetEntry(ctxt, XML_EXP_OR, left->exp_right, right,
6637
                               NULL, 0, 0);
6638
      left->exp_left->ref++;
6639
      tmp = xmlExpHashGetEntry(ctxt, XML_EXP_OR, left->exp_left, tmp,
6640
                               NULL, 0, 0);
6641
6642
      xmlExpFree(ctxt, left);
6643
      return(tmp);
6644
  }
6645
  if (right->type == XML_EXP_OR) {
6646
      /* Ordering in the tree */
6647
      /* C | (A | B) -> A | (B | C) */
6648
      if (left->key > right->exp_right->key) {
6649
    xmlExpNodePtr tmp;
6650
    right->exp_right->ref++;
6651
    tmp = xmlExpHashGetEntry(ctxt, XML_EXP_OR, right->exp_right,
6652
                             left, NULL, 0, 0);
6653
    right->exp_left->ref++;
6654
    tmp = xmlExpHashGetEntry(ctxt, XML_EXP_OR, right->exp_left,
6655
                             tmp, NULL, 0, 0);
6656
    xmlExpFree(ctxt, right);
6657
    return(tmp);
6658
      }
6659
      /* Ordering in the tree */
6660
      /* B | (A | C) -> A | (B | C) */
6661
      if (left->key > right->exp_left->key) {
6662
    xmlExpNodePtr tmp;
6663
    right->exp_right->ref++;
6664
    tmp = xmlExpHashGetEntry(ctxt, XML_EXP_OR, left,
6665
                             right->exp_right, NULL, 0, 0);
6666
    right->exp_left->ref++;
6667
    tmp = xmlExpHashGetEntry(ctxt, XML_EXP_OR, right->exp_left,
6668
                             tmp, NULL, 0, 0);
6669
    xmlExpFree(ctxt, right);
6670
    return(tmp);
6671
      }
6672
  }
6673
  /* we know both types are != XML_EXP_OR here */
6674
        else if (left->key > right->key) {
6675
      xmlExpNodePtr tmp = left;
6676
            left = right;
6677
      right = tmp;
6678
  }
6679
  kbase = xmlExpHashComputeKey(type, left, right);
6680
    } else if (type == XML_EXP_SEQ) {
6681
        /* Forbid reduction rules */
6682
        if (left->type == XML_EXP_FORBID) {
6683
      xmlExpFree(ctxt, right);
6684
      return(left);
6685
  }
6686
        if (right->type == XML_EXP_FORBID) {
6687
      xmlExpFree(ctxt, left);
6688
      return(right);
6689
  }
6690
        /* Empty reduction rules */
6691
        if (right->type == XML_EXP_EMPTY) {
6692
      return(left);
6693
  }
6694
        if (left->type == XML_EXP_EMPTY) {
6695
      return(right);
6696
  }
6697
  kbase = xmlExpHashComputeKey(type, left, right);
6698
    } else
6699
        return(NULL);
6700
6701
    key = kbase % ctxt->size;
6702
    if (ctxt->table[key] != NULL) {
6703
  for (insert = ctxt->table[key]; insert != NULL;
6704
       insert = insert->next) {
6705
      if ((insert->key == kbase) &&
6706
          (insert->type == type)) {
6707
    if (type == XML_EXP_ATOM) {
6708
        if (name == insert->exp_str) {
6709
      insert->ref++;
6710
      return(insert);
6711
        }
6712
    } else if (type == XML_EXP_COUNT) {
6713
        if ((insert->exp_min == min) && (insert->exp_max == max) &&
6714
            (insert->exp_left == left)) {
6715
      insert->ref++;
6716
      left->ref--;
6717
      return(insert);
6718
        }
6719
    } else if ((insert->exp_left == left) &&
6720
         (insert->exp_right == right)) {
6721
        insert->ref++;
6722
        left->ref--;
6723
        right->ref--;
6724
        return(insert);
6725
    }
6726
      }
6727
  }
6728
    }
6729
6730
    entry = xmlExpNewNode(ctxt, type);
6731
    if (entry == NULL)
6732
        return(NULL);
6733
    entry->key = kbase;
6734
    if (type == XML_EXP_ATOM) {
6735
  entry->exp_str = name;
6736
  entry->c_max = 1;
6737
    } else if (type == XML_EXP_COUNT) {
6738
        entry->exp_min = min;
6739
        entry->exp_max = max;
6740
  entry->exp_left = left;
6741
  if ((min == 0) || (IS_NILLABLE(left)))
6742
      entry->info |= XML_EXP_NILABLE;
6743
  if (max < 0)
6744
      entry->c_max = -1;
6745
  else
6746
      entry->c_max = max * entry->exp_left->c_max;
6747
    } else {
6748
  entry->exp_left = left;
6749
  entry->exp_right = right;
6750
  if (type == XML_EXP_OR) {
6751
      if ((IS_NILLABLE(left)) || (IS_NILLABLE(right)))
6752
    entry->info |= XML_EXP_NILABLE;
6753
      if ((entry->exp_left->c_max == -1) ||
6754
          (entry->exp_right->c_max == -1))
6755
    entry->c_max = -1;
6756
      else if (entry->exp_left->c_max > entry->exp_right->c_max)
6757
          entry->c_max = entry->exp_left->c_max;
6758
      else
6759
          entry->c_max = entry->exp_right->c_max;
6760
  } else {
6761
      if ((IS_NILLABLE(left)) && (IS_NILLABLE(right)))
6762
    entry->info |= XML_EXP_NILABLE;
6763
      if ((entry->exp_left->c_max == -1) ||
6764
          (entry->exp_right->c_max == -1))
6765
    entry->c_max = -1;
6766
      else
6767
          entry->c_max = entry->exp_left->c_max + entry->exp_right->c_max;
6768
  }
6769
    }
6770
    entry->ref = 1;
6771
    if (ctxt->table[key] != NULL)
6772
        entry->next = ctxt->table[key];
6773
6774
    ctxt->table[key] = entry;
6775
    ctxt->nbElems++;
6776
6777
    return(entry);
6778
}
6779
6780
/**
6781
 * xmlExpFree:
6782
 * @ctxt: the expression context
6783
 * @exp: the expression
6784
 *
6785
 * Dereference the expression
6786
 */
6787
void
6788
xmlExpFree(xmlExpCtxtPtr ctxt, xmlExpNodePtr exp) {
6789
    if ((exp == NULL) || (exp == forbiddenExp) || (exp == emptyExp))
6790
        return;
6791
    exp->ref--;
6792
    if (exp->ref == 0) {
6793
        unsigned short key;
6794
6795
        /* Unlink it first from the hash table */
6796
  key = exp->key % ctxt->size;
6797
  if (ctxt->table[key] == exp) {
6798
      ctxt->table[key] = exp->next;
6799
  } else {
6800
      xmlExpNodePtr tmp;
6801
6802
      tmp = ctxt->table[key];
6803
      while (tmp != NULL) {
6804
          if (tmp->next == exp) {
6805
        tmp->next = exp->next;
6806
        break;
6807
    }
6808
          tmp = tmp->next;
6809
      }
6810
  }
6811
6812
        if ((exp->type == XML_EXP_SEQ) || (exp->type == XML_EXP_OR)) {
6813
      xmlExpFree(ctxt, exp->exp_left);
6814
      xmlExpFree(ctxt, exp->exp_right);
6815
  } else if (exp->type == XML_EXP_COUNT) {
6816
      xmlExpFree(ctxt, exp->exp_left);
6817
  }
6818
        xmlFree(exp);
6819
  ctxt->nb_nodes--;
6820
    }
6821
}
6822
6823
/**
6824
 * xmlExpRef:
6825
 * @exp: the expression
6826
 *
6827
 * Increase the reference count of the expression
6828
 */
6829
void
6830
xmlExpRef(xmlExpNodePtr exp) {
6831
    if (exp != NULL)
6832
        exp->ref++;
6833
}
6834
6835
/**
6836
 * xmlExpNewAtom:
6837
 * @ctxt: the expression context
6838
 * @name: the atom name
6839
 * @len: the atom name length in byte (or -1);
6840
 *
6841
 * Get the atom associated to this name from that context
6842
 *
6843
 * Returns the node or NULL in case of error
6844
 */
6845
xmlExpNodePtr
6846
xmlExpNewAtom(xmlExpCtxtPtr ctxt, const xmlChar *name, int len) {
6847
    if ((ctxt == NULL) || (name == NULL))
6848
        return(NULL);
6849
    name = xmlDictLookup(ctxt->dict, name, len);
6850
    if (name == NULL)
6851
        return(NULL);
6852
    return(xmlExpHashGetEntry(ctxt, XML_EXP_ATOM, NULL, NULL, name, 0, 0));
6853
}
6854
6855
/**
6856
 * xmlExpNewOr:
6857
 * @ctxt: the expression context
6858
 * @left: left expression
6859
 * @right: right expression
6860
 *
6861
 * Get the atom associated to the choice @left | @right
6862
 * Note that @left and @right are consumed in the operation, to keep
6863
 * an handle on them use xmlExpRef() and use xmlExpFree() to release them,
6864
 * this is true even in case of failure (unless ctxt == NULL).
6865
 *
6866
 * Returns the node or NULL in case of error
6867
 */
6868
xmlExpNodePtr
6869
xmlExpNewOr(xmlExpCtxtPtr ctxt, xmlExpNodePtr left, xmlExpNodePtr right) {
6870
    if (ctxt == NULL)
6871
        return(NULL);
6872
    if ((left == NULL) || (right == NULL)) {
6873
        xmlExpFree(ctxt, left);
6874
        xmlExpFree(ctxt, right);
6875
        return(NULL);
6876
    }
6877
    return(xmlExpHashGetEntry(ctxt, XML_EXP_OR, left, right, NULL, 0, 0));
6878
}
6879
6880
/**
6881
 * xmlExpNewSeq:
6882
 * @ctxt: the expression context
6883
 * @left: left expression
6884
 * @right: right expression
6885
 *
6886
 * Get the atom associated to the sequence @left , @right
6887
 * Note that @left and @right are consumed in the operation, to keep
6888
 * an handle on them use xmlExpRef() and use xmlExpFree() to release them,
6889
 * this is true even in case of failure (unless ctxt == NULL).
6890
 *
6891
 * Returns the node or NULL in case of error
6892
 */
6893
xmlExpNodePtr
6894
xmlExpNewSeq(xmlExpCtxtPtr ctxt, xmlExpNodePtr left, xmlExpNodePtr right) {
6895
    if (ctxt == NULL)
6896
        return(NULL);
6897
    if ((left == NULL) || (right == NULL)) {
6898
        xmlExpFree(ctxt, left);
6899
        xmlExpFree(ctxt, right);
6900
        return(NULL);
6901
    }
6902
    return(xmlExpHashGetEntry(ctxt, XML_EXP_SEQ, left, right, NULL, 0, 0));
6903
}
6904
6905
/**
6906
 * xmlExpNewRange:
6907
 * @ctxt: the expression context
6908
 * @subset: the expression to be repeated
6909
 * @min: the lower bound for the repetition
6910
 * @max: the upper bound for the repetition, -1 means infinite
6911
 *
6912
 * Get the atom associated to the range (@subset){@min, @max}
6913
 * Note that @subset is consumed in the operation, to keep
6914
 * an handle on it use xmlExpRef() and use xmlExpFree() to release it,
6915
 * this is true even in case of failure (unless ctxt == NULL).
6916
 *
6917
 * Returns the node or NULL in case of error
6918
 */
6919
xmlExpNodePtr
6920
xmlExpNewRange(xmlExpCtxtPtr ctxt, xmlExpNodePtr subset, int min, int max) {
6921
    if (ctxt == NULL)
6922
        return(NULL);
6923
    if ((subset == NULL) || (min < 0) || (max < -1) ||
6924
        ((max >= 0) && (min > max))) {
6925
  xmlExpFree(ctxt, subset);
6926
        return(NULL);
6927
    }
6928
    return(xmlExpHashGetEntry(ctxt, XML_EXP_COUNT, subset,
6929
                              NULL, NULL, min, max));
6930
}
6931
6932
/************************************************************************
6933
 *                  *
6934
 *    Public API for operations on expressions    *
6935
 *                  *
6936
 ************************************************************************/
6937
6938
static int
6939
xmlExpGetLanguageInt(xmlExpCtxtPtr ctxt, xmlExpNodePtr exp,
6940
                     const xmlChar**list, int len, int nb) {
6941
    int tmp, tmp2;
6942
tail:
6943
    switch (exp->type) {
6944
        case XML_EXP_EMPTY:
6945
      return(0);
6946
        case XML_EXP_ATOM:
6947
      for (tmp = 0;tmp < nb;tmp++)
6948
          if (list[tmp] == exp->exp_str)
6949
        return(0);
6950
            if (nb >= len)
6951
          return(-2);
6952
      list[nb] = exp->exp_str;
6953
      return(1);
6954
        case XML_EXP_COUNT:
6955
      exp = exp->exp_left;
6956
      goto tail;
6957
        case XML_EXP_SEQ:
6958
        case XML_EXP_OR:
6959
      tmp = xmlExpGetLanguageInt(ctxt, exp->exp_left, list, len, nb);
6960
      if (tmp < 0)
6961
          return(tmp);
6962
      tmp2 = xmlExpGetLanguageInt(ctxt, exp->exp_right, list, len,
6963
                                  nb + tmp);
6964
      if (tmp2 < 0)
6965
          return(tmp2);
6966
            return(tmp + tmp2);
6967
    }
6968
    return(-1);
6969
}
6970
6971
/**
6972
 * xmlExpGetLanguage:
6973
 * @ctxt: the expression context
6974
 * @exp: the expression
6975
 * @langList: where to store the tokens
6976
 * @len: the allocated length of @list
6977
 *
6978
 * Find all the strings used in @exp and store them in @list
6979
 *
6980
 * Returns the number of unique strings found, -1 in case of errors and
6981
 *         -2 if there is more than @len strings
6982
 */
6983
int
6984
xmlExpGetLanguage(xmlExpCtxtPtr ctxt, xmlExpNodePtr exp,
6985
                  const xmlChar**langList, int len) {
6986
    if ((ctxt == NULL) || (exp == NULL) || (langList == NULL) || (len <= 0))
6987
        return(-1);
6988
    return(xmlExpGetLanguageInt(ctxt, exp, langList, len, 0));
6989
}
6990
6991
static int
6992
xmlExpGetStartInt(xmlExpCtxtPtr ctxt, xmlExpNodePtr exp,
6993
                  const xmlChar**list, int len, int nb) {
6994
    int tmp, tmp2;
6995
tail:
6996
    switch (exp->type) {
6997
        case XML_EXP_FORBID:
6998
      return(0);
6999
        case XML_EXP_EMPTY:
7000
      return(0);
7001
        case XML_EXP_ATOM:
7002
      for (tmp = 0;tmp < nb;tmp++)
7003
          if (list[tmp] == exp->exp_str)
7004
        return(0);
7005
            if (nb >= len)
7006
          return(-2);
7007
      list[nb] = exp->exp_str;
7008
      return(1);
7009
        case XML_EXP_COUNT:
7010
      exp = exp->exp_left;
7011
      goto tail;
7012
        case XML_EXP_SEQ:
7013
      tmp = xmlExpGetStartInt(ctxt, exp->exp_left, list, len, nb);
7014
      if (tmp < 0)
7015
          return(tmp);
7016
      if (IS_NILLABLE(exp->exp_left)) {
7017
    tmp2 = xmlExpGetStartInt(ctxt, exp->exp_right, list, len,
7018
              nb + tmp);
7019
    if (tmp2 < 0)
7020
        return(tmp2);
7021
    tmp += tmp2;
7022
      }
7023
            return(tmp);
7024
        case XML_EXP_OR:
7025
      tmp = xmlExpGetStartInt(ctxt, exp->exp_left, list, len, nb);
7026
      if (tmp < 0)
7027
          return(tmp);
7028
      tmp2 = xmlExpGetStartInt(ctxt, exp->exp_right, list, len,
7029
                                  nb + tmp);
7030
      if (tmp2 < 0)
7031
          return(tmp2);
7032
            return(tmp + tmp2);
7033
    }
7034
    return(-1);
7035
}
7036
7037
/**
7038
 * xmlExpGetStart:
7039
 * @ctxt: the expression context
7040
 * @exp: the expression
7041
 * @tokList: where to store the tokens
7042
 * @len: the allocated length of @list
7043
 *
7044
 * Find all the strings that appears at the start of the languages
7045
 * accepted by @exp and store them in @list. E.g. for (a, b) | c
7046
 * it will return the list [a, c]
7047
 *
7048
 * Returns the number of unique strings found, -1 in case of errors and
7049
 *         -2 if there is more than @len strings
7050
 */
7051
int
7052
xmlExpGetStart(xmlExpCtxtPtr ctxt, xmlExpNodePtr exp,
7053
               const xmlChar**tokList, int len) {
7054
    if ((ctxt == NULL) || (exp == NULL) || (tokList == NULL) || (len <= 0))
7055
        return(-1);
7056
    return(xmlExpGetStartInt(ctxt, exp, tokList, len, 0));
7057
}
7058
7059
/**
7060
 * xmlExpIsNillable:
7061
 * @exp: the expression
7062
 *
7063
 * Finds if the expression is nillable, i.e. if it accepts the empty sequence
7064
 *
7065
 * Returns 1 if nillable, 0 if not and -1 in case of error
7066
 */
7067
int
7068
xmlExpIsNillable(xmlExpNodePtr exp) {
7069
    if (exp == NULL)
7070
        return(-1);
7071
    return(IS_NILLABLE(exp) != 0);
7072
}
7073
7074
static xmlExpNodePtr
7075
xmlExpStringDeriveInt(xmlExpCtxtPtr ctxt, xmlExpNodePtr exp, const xmlChar *str)
7076
{
7077
    xmlExpNodePtr ret;
7078
7079
    switch (exp->type) {
7080
  case XML_EXP_EMPTY:
7081
      return(forbiddenExp);
7082
  case XML_EXP_FORBID:
7083
      return(forbiddenExp);
7084
  case XML_EXP_ATOM:
7085
      if (exp->exp_str == str) {
7086
          ret = emptyExp;
7087
      } else {
7088
          /* TODO wildcards here */
7089
    ret = forbiddenExp;
7090
      }
7091
      return(ret);
7092
  case XML_EXP_OR: {
7093
      xmlExpNodePtr tmp;
7094
7095
      tmp = xmlExpStringDeriveInt(ctxt, exp->exp_left, str);
7096
      if (tmp == NULL) {
7097
    return(NULL);
7098
      }
7099
      ret = xmlExpStringDeriveInt(ctxt, exp->exp_right, str);
7100
      if (ret == NULL) {
7101
          xmlExpFree(ctxt, tmp);
7102
    return(NULL);
7103
      }
7104
            ret = xmlExpHashGetEntry(ctxt, XML_EXP_OR, tmp, ret,
7105
           NULL, 0, 0);
7106
      return(ret);
7107
  }
7108
  case XML_EXP_SEQ:
7109
      ret = xmlExpStringDeriveInt(ctxt, exp->exp_left, str);
7110
      if (ret == NULL) {
7111
          return(NULL);
7112
      } else if (ret == forbiddenExp) {
7113
          if (IS_NILLABLE(exp->exp_left)) {
7114
        ret = xmlExpStringDeriveInt(ctxt, exp->exp_right, str);
7115
    }
7116
      } else {
7117
          exp->exp_right->ref++;
7118
          ret = xmlExpHashGetEntry(ctxt, XML_EXP_SEQ, ret, exp->exp_right,
7119
                             NULL, 0, 0);
7120
      }
7121
      return(ret);
7122
  case XML_EXP_COUNT: {
7123
      int min, max;
7124
      xmlExpNodePtr tmp;
7125
7126
      if (exp->exp_max == 0)
7127
    return(forbiddenExp);
7128
      ret = xmlExpStringDeriveInt(ctxt, exp->exp_left, str);
7129
      if (ret == NULL)
7130
          return(NULL);
7131
      if (ret == forbiddenExp) {
7132
          return(ret);
7133
      }
7134
      if (exp->exp_max == 1)
7135
    return(ret);
7136
      if (exp->exp_max < 0) /* unbounded */
7137
    max = -1;
7138
      else
7139
    max = exp->exp_max - 1;
7140
      if (exp->exp_min > 0)
7141
    min = exp->exp_min - 1;
7142
      else
7143
    min = 0;
7144
      exp->exp_left->ref++;
7145
      tmp = xmlExpHashGetEntry(ctxt, XML_EXP_COUNT, exp->exp_left, NULL,
7146
             NULL, min, max);
7147
      if (ret == emptyExp) {
7148
          return(tmp);
7149
      }
7150
      return(xmlExpHashGetEntry(ctxt, XML_EXP_SEQ, ret, tmp,
7151
                                NULL, 0, 0));
7152
  }
7153
    }
7154
    return(NULL);
7155
}
7156
7157
/**
7158
 * xmlExpStringDerive:
7159
 * @ctxt: the expression context
7160
 * @exp: the expression
7161
 * @str: the string
7162
 * @len: the string len in bytes if available
7163
 *
7164
 * Do one step of Brzozowski derivation of the expression @exp with
7165
 * respect to the input string
7166
 *
7167
 * Returns the resulting expression or NULL in case of internal error
7168
 */
7169
xmlExpNodePtr
7170
xmlExpStringDerive(xmlExpCtxtPtr ctxt, xmlExpNodePtr exp,
7171
                   const xmlChar *str, int len) {
7172
    const xmlChar *input;
7173
7174
    if ((exp == NULL) || (ctxt == NULL) || (str == NULL)) {
7175
        return(NULL);
7176
    }
7177
    /*
7178
     * check the string is in the dictionary, if yes use an interned
7179
     * copy, otherwise we know it's not an acceptable input
7180
     */
7181
    input = xmlDictExists(ctxt->dict, str, len);
7182
    if (input == NULL) {
7183
        return(forbiddenExp);
7184
    }
7185
    return(xmlExpStringDeriveInt(ctxt, exp, input));
7186
}
7187
7188
static int
7189
xmlExpCheckCard(xmlExpNodePtr exp, xmlExpNodePtr sub) {
7190
    int ret = 1;
7191
7192
    if (sub->c_max == -1) {
7193
        if (exp->c_max != -1)
7194
      ret = 0;
7195
    } else if ((exp->c_max >= 0) && (exp->c_max < sub->c_max)) {
7196
        ret = 0;
7197
    }
7198
#if 0
7199
    if ((IS_NILLABLE(sub)) && (!IS_NILLABLE(exp)))
7200
        ret = 0;
7201
#endif
7202
    return(ret);
7203
}
7204
7205
static xmlExpNodePtr xmlExpExpDeriveInt(xmlExpCtxtPtr ctxt, xmlExpNodePtr exp,
7206
                                        xmlExpNodePtr sub);
7207
/**
7208
 * xmlExpDivide:
7209
 * @ctxt: the expressions context
7210
 * @exp: the englobing expression
7211
 * @sub: the subexpression
7212
 * @mult: the multiple expression
7213
 * @remain: the remain from the derivation of the multiple
7214
 *
7215
 * Check if exp is a multiple of sub, i.e. if there is a finite number n
7216
 * so that sub{n} subsume exp
7217
 *
7218
 * Returns the multiple value if successful, 0 if it is not a multiple
7219
 *         and -1 in case of internal error.
7220
 */
7221
7222
static int
7223
xmlExpDivide(xmlExpCtxtPtr ctxt, xmlExpNodePtr exp, xmlExpNodePtr sub,
7224
             xmlExpNodePtr *mult, xmlExpNodePtr *remain) {
7225
    int i;
7226
    xmlExpNodePtr tmp, tmp2;
7227
7228
    if (mult != NULL) *mult = NULL;
7229
    if (remain != NULL) *remain = NULL;
7230
    if (exp->c_max == -1) return(0);
7231
    if (IS_NILLABLE(exp) && (!IS_NILLABLE(sub))) return(0);
7232
7233
    for (i = 1;i <= exp->c_max;i++) {
7234
        sub->ref++;
7235
        tmp = xmlExpHashGetEntry(ctxt, XML_EXP_COUNT,
7236
         sub, NULL, NULL, i, i);
7237
  if (tmp == NULL) {
7238
      return(-1);
7239
  }
7240
  if (!xmlExpCheckCard(tmp, exp)) {
7241
      xmlExpFree(ctxt, tmp);
7242
      continue;
7243
  }
7244
  tmp2 = xmlExpExpDeriveInt(ctxt, tmp, exp);
7245
  if (tmp2 == NULL) {
7246
      xmlExpFree(ctxt, tmp);
7247
      return(-1);
7248
  }
7249
  if ((tmp2 != forbiddenExp) && (IS_NILLABLE(tmp2))) {
7250
      if (remain != NULL)
7251
          *remain = tmp2;
7252
      else
7253
          xmlExpFree(ctxt, tmp2);
7254
      if (mult != NULL)
7255
          *mult = tmp;
7256
      else
7257
          xmlExpFree(ctxt, tmp);
7258
      return(i);
7259
  }
7260
  xmlExpFree(ctxt, tmp);
7261
  xmlExpFree(ctxt, tmp2);
7262
    }
7263
    return(0);
7264
}
7265
7266
/**
7267
 * xmlExpExpDeriveInt:
7268
 * @ctxt: the expressions context
7269
 * @exp: the englobing expression
7270
 * @sub: the subexpression
7271
 *
7272
 * Try to do a step of Brzozowski derivation but at a higher level
7273
 * the input being a subexpression.
7274
 *
7275
 * Returns the resulting expression or NULL in case of internal error
7276
 */
7277
static xmlExpNodePtr
7278
xmlExpExpDeriveInt(xmlExpCtxtPtr ctxt, xmlExpNodePtr exp, xmlExpNodePtr sub) {
7279
    xmlExpNodePtr ret, tmp, tmp2, tmp3;
7280
    const xmlChar **tab;
7281
    int len, i;
7282
7283
    /*
7284
     * In case of equality and if the expression can only consume a finite
7285
     * amount, then the derivation is empty
7286
     */
7287
    if ((exp == sub) && (exp->c_max >= 0)) {
7288
        return(emptyExp);
7289
    }
7290
    /*
7291
     * decompose sub sequence first
7292
     */
7293
    if (sub->type == XML_EXP_EMPTY) {
7294
  exp->ref++;
7295
        return(exp);
7296
    }
7297
    if (sub->type == XML_EXP_SEQ) {
7298
        tmp = xmlExpExpDeriveInt(ctxt, exp, sub->exp_left);
7299
  if (tmp == NULL)
7300
      return(NULL);
7301
  if (tmp == forbiddenExp)
7302
      return(tmp);
7303
  ret = xmlExpExpDeriveInt(ctxt, tmp, sub->exp_right);
7304
  xmlExpFree(ctxt, tmp);
7305
  return(ret);
7306
    }
7307
    if (sub->type == XML_EXP_OR) {
7308
        tmp = xmlExpExpDeriveInt(ctxt, exp, sub->exp_left);
7309
  if (tmp == forbiddenExp)
7310
      return(tmp);
7311
  if (tmp == NULL)
7312
      return(NULL);
7313
  ret = xmlExpExpDeriveInt(ctxt, exp, sub->exp_right);
7314
  if ((ret == NULL) || (ret == forbiddenExp)) {
7315
      xmlExpFree(ctxt, tmp);
7316
      return(ret);
7317
  }
7318
  return(xmlExpHashGetEntry(ctxt, XML_EXP_OR, tmp, ret, NULL, 0, 0));
7319
    }
7320
    if (!xmlExpCheckCard(exp, sub)) {
7321
        return(forbiddenExp);
7322
    }
7323
    switch (exp->type) {
7324
        case XML_EXP_EMPTY:
7325
      if (sub == emptyExp)
7326
          return(emptyExp);
7327
      return(forbiddenExp);
7328
        case XML_EXP_FORBID:
7329
      return(forbiddenExp);
7330
        case XML_EXP_ATOM:
7331
      if (sub->type == XML_EXP_ATOM) {
7332
          /* TODO: handle wildcards */
7333
          if (exp->exp_str == sub->exp_str) {
7334
        return(emptyExp);
7335
                }
7336
          return(forbiddenExp);
7337
      }
7338
      if ((sub->type == XML_EXP_COUNT) &&
7339
          (sub->exp_max == 1) &&
7340
          (sub->exp_left->type == XML_EXP_ATOM)) {
7341
          /* TODO: handle wildcards */
7342
          if (exp->exp_str == sub->exp_left->exp_str) {
7343
        return(emptyExp);
7344
    }
7345
          return(forbiddenExp);
7346
      }
7347
      return(forbiddenExp);
7348
        case XML_EXP_SEQ:
7349
      /* try to get the sequence consumed only if possible */
7350
      if (xmlExpCheckCard(exp->exp_left, sub)) {
7351
    /* See if the sequence can be consumed directly */
7352
    ret = xmlExpExpDeriveInt(ctxt, exp->exp_left, sub);
7353
    if ((ret != forbiddenExp) && (ret != NULL)) {
7354
        /*
7355
         * TODO: assumption here that we are determinist
7356
         *       i.e. we won't get to a nillable exp left
7357
         *       subset which could be matched by the right
7358
         *       part too.
7359
         * e.g.: (a | b)+,(a | c) and 'a+,a'
7360
         */
7361
        exp->exp_right->ref++;
7362
        return(xmlExpHashGetEntry(ctxt, XML_EXP_SEQ, ret,
7363
                exp->exp_right, NULL, 0, 0));
7364
    }
7365
      }
7366
      /* Try instead to decompose */
7367
      if (sub->type == XML_EXP_COUNT) {
7368
    int min, max;
7369
7370
          ret = xmlExpExpDeriveInt(ctxt, exp->exp_left, sub->exp_left);
7371
    if (ret == NULL)
7372
        return(NULL);
7373
    if (ret != forbiddenExp) {
7374
        if (sub->exp_max < 0)
7375
            max = -1;
7376
              else
7377
            max = sub->exp_max -1;
7378
        if (sub->exp_min > 0)
7379
            min = sub->exp_min -1;
7380
        else
7381
            min = 0;
7382
        exp->exp_right->ref++;
7383
        tmp = xmlExpHashGetEntry(ctxt, XML_EXP_SEQ, ret,
7384
                                 exp->exp_right, NULL, 0, 0);
7385
        if (tmp == NULL)
7386
            return(NULL);
7387
7388
        sub->exp_left->ref++;
7389
        tmp2 = xmlExpHashGetEntry(ctxt, XML_EXP_COUNT,
7390
              sub->exp_left, NULL, NULL, min, max);
7391
        if (tmp2 == NULL) {
7392
            xmlExpFree(ctxt, tmp);
7393
      return(NULL);
7394
        }
7395
        ret = xmlExpExpDeriveInt(ctxt, tmp, tmp2);
7396
        xmlExpFree(ctxt, tmp);
7397
        xmlExpFree(ctxt, tmp2);
7398
        return(ret);
7399
    }
7400
      }
7401
      /* we made no progress on structured operations */
7402
      break;
7403
        case XML_EXP_OR:
7404
      ret = xmlExpExpDeriveInt(ctxt, exp->exp_left, sub);
7405
      if (ret == NULL)
7406
          return(NULL);
7407
      tmp = xmlExpExpDeriveInt(ctxt, exp->exp_right, sub);
7408
      if (tmp == NULL) {
7409
    xmlExpFree(ctxt, ret);
7410
          return(NULL);
7411
      }
7412
      return(xmlExpHashGetEntry(ctxt, XML_EXP_OR, ret, tmp, NULL, 0, 0));
7413
        case XML_EXP_COUNT: {
7414
      int min, max;
7415
7416
      if (sub->type == XML_EXP_COUNT) {
7417
          /*
7418
     * Try to see if the loop is completely subsumed
7419
     */
7420
          tmp = xmlExpExpDeriveInt(ctxt, exp->exp_left, sub->exp_left);
7421
    if (tmp == NULL)
7422
        return(NULL);
7423
    if (tmp == forbiddenExp) {
7424
        int mult;
7425
7426
        mult = xmlExpDivide(ctxt, sub->exp_left, exp->exp_left,
7427
                            NULL, &tmp);
7428
        if (mult <= 0) {
7429
                        return(forbiddenExp);
7430
        }
7431
        if (sub->exp_max == -1) {
7432
            max = -1;
7433
      if (exp->exp_max == -1) {
7434
          if (exp->exp_min <= sub->exp_min * mult)
7435
              min = 0;
7436
          else
7437
              min = exp->exp_min - sub->exp_min * mult;
7438
      } else {
7439
                            xmlExpFree(ctxt, tmp);
7440
          return(forbiddenExp);
7441
      }
7442
        } else {
7443
      if (exp->exp_max == -1) {
7444
          if (exp->exp_min > sub->exp_min * mult) {
7445
        max = -1;
7446
        min = exp->exp_min - sub->exp_min * mult;
7447
          } else {
7448
        max = -1;
7449
        min = 0;
7450
          }
7451
      } else {
7452
          if (exp->exp_max < sub->exp_max * mult) {
7453
        xmlExpFree(ctxt, tmp);
7454
        return(forbiddenExp);
7455
          }
7456
          if (sub->exp_max * mult > exp->exp_min)
7457
        min = 0;
7458
          else
7459
        min = exp->exp_min - sub->exp_max * mult;
7460
          max = exp->exp_max - sub->exp_max * mult;
7461
      }
7462
        }
7463
    } else if (!IS_NILLABLE(tmp)) {
7464
        /*
7465
         * TODO: loop here to try to grow if working on finite
7466
         *       blocks.
7467
         */
7468
        xmlExpFree(ctxt, tmp);
7469
        return(forbiddenExp);
7470
    } else if (sub->exp_max == -1) {
7471
        if (exp->exp_max == -1) {
7472
            if (exp->exp_min <= sub->exp_min) {
7473
                            max = -1;
7474
          min = 0;
7475
      } else {
7476
                            max = -1;
7477
          min = exp->exp_min - sub->exp_min;
7478
      }
7479
        } else if (exp->exp_min > sub->exp_min) {
7480
            xmlExpFree(ctxt, tmp);
7481
            return(forbiddenExp);
7482
        } else {
7483
      max = -1;
7484
      min = 0;
7485
        }
7486
    } else {
7487
        if (exp->exp_max == -1) {
7488
            if (exp->exp_min > sub->exp_min) {
7489
          max = -1;
7490
          min = exp->exp_min - sub->exp_min;
7491
      } else {
7492
          max = -1;
7493
          min = 0;
7494
      }
7495
        } else {
7496
            if (exp->exp_max < sub->exp_max) {
7497
          xmlExpFree(ctxt, tmp);
7498
          return(forbiddenExp);
7499
      }
7500
      if (sub->exp_max > exp->exp_min)
7501
          min = 0;
7502
      else
7503
          min = exp->exp_min - sub->exp_max;
7504
      max = exp->exp_max - sub->exp_max;
7505
        }
7506
    }
7507
    exp->exp_left->ref++;
7508
    tmp2 = xmlExpHashGetEntry(ctxt, XML_EXP_COUNT, exp->exp_left,
7509
                              NULL, NULL, min, max);
7510
    if (tmp2 == NULL) {
7511
        return(NULL);
7512
    }
7513
                ret = xmlExpHashGetEntry(ctxt, XML_EXP_SEQ, tmp, tmp2,
7514
                             NULL, 0, 0);
7515
    return(ret);
7516
      }
7517
      tmp = xmlExpExpDeriveInt(ctxt, exp->exp_left, sub);
7518
      if (tmp == NULL)
7519
    return(NULL);
7520
      if (tmp == forbiddenExp) {
7521
    return(forbiddenExp);
7522
      }
7523
      if (exp->exp_min > 0)
7524
    min = exp->exp_min - 1;
7525
      else
7526
    min = 0;
7527
      if (exp->exp_max < 0)
7528
    max = -1;
7529
      else
7530
    max = exp->exp_max - 1;
7531
7532
      exp->exp_left->ref++;
7533
      tmp2 = xmlExpHashGetEntry(ctxt, XML_EXP_COUNT, exp->exp_left,
7534
              NULL, NULL, min, max);
7535
      if (tmp2 == NULL)
7536
    return(NULL);
7537
      ret = xmlExpHashGetEntry(ctxt, XML_EXP_SEQ, tmp, tmp2,
7538
             NULL, 0, 0);
7539
      return(ret);
7540
  }
7541
    }
7542
7543
    if (IS_NILLABLE(sub)) {
7544
        if (!(IS_NILLABLE(exp)))
7545
      return(forbiddenExp);
7546
  else
7547
      ret = emptyExp;
7548
    } else
7549
  ret = NULL;
7550
    /*
7551
     * here the structured derivation made no progress so
7552
     * we use the default token based derivation to force one more step
7553
     */
7554
    if (ctxt->tabSize == 0)
7555
        ctxt->tabSize = 40;
7556
7557
    tab = (const xmlChar **) xmlMalloc(ctxt->tabSize *
7558
                                 sizeof(const xmlChar *));
7559
    if (tab == NULL) {
7560
  return(NULL);
7561
    }
7562
7563
    /*
7564
     * collect all the strings accepted by the subexpression on input
7565
     */
7566
    len = xmlExpGetStartInt(ctxt, sub, tab, ctxt->tabSize, 0);
7567
    while (len < 0) {
7568
        const xmlChar **temp;
7569
  temp = (const xmlChar **) xmlRealloc((xmlChar **) tab, ctxt->tabSize * 2 *
7570
                                       sizeof(const xmlChar *));
7571
  if (temp == NULL) {
7572
      xmlFree((xmlChar **) tab);
7573
      return(NULL);
7574
  }
7575
  tab = temp;
7576
  ctxt->tabSize *= 2;
7577
  len = xmlExpGetStartInt(ctxt, sub, tab, ctxt->tabSize, 0);
7578
    }
7579
    for (i = 0;i < len;i++) {
7580
        tmp = xmlExpStringDeriveInt(ctxt, exp, tab[i]);
7581
  if ((tmp == NULL) || (tmp == forbiddenExp)) {
7582
      xmlExpFree(ctxt, ret);
7583
      xmlFree((xmlChar **) tab);
7584
      return(tmp);
7585
  }
7586
  tmp2 = xmlExpStringDeriveInt(ctxt, sub, tab[i]);
7587
  if ((tmp2 == NULL) || (tmp2 == forbiddenExp)) {
7588
      xmlExpFree(ctxt, tmp);
7589
      xmlExpFree(ctxt, ret);
7590
      xmlFree((xmlChar **) tab);
7591
      return(tmp);
7592
  }
7593
  tmp3 = xmlExpExpDeriveInt(ctxt, tmp, tmp2);
7594
  xmlExpFree(ctxt, tmp);
7595
  xmlExpFree(ctxt, tmp2);
7596
7597
  if ((tmp3 == NULL) || (tmp3 == forbiddenExp)) {
7598
      xmlExpFree(ctxt, ret);
7599
      xmlFree((xmlChar **) tab);
7600
      return(tmp3);
7601
  }
7602
7603
  if (ret == NULL)
7604
      ret = tmp3;
7605
  else {
7606
      ret = xmlExpHashGetEntry(ctxt, XML_EXP_OR, ret, tmp3, NULL, 0, 0);
7607
      if (ret == NULL) {
7608
    xmlFree((xmlChar **) tab);
7609
          return(NULL);
7610
      }
7611
  }
7612
    }
7613
    xmlFree((xmlChar **) tab);
7614
    return(ret);
7615
}
7616
7617
/**
7618
 * xmlExpExpDerive:
7619
 * @ctxt: the expressions context
7620
 * @exp: the englobing expression
7621
 * @sub: the subexpression
7622
 *
7623
 * Evaluates the expression resulting from @exp consuming a sub expression @sub
7624
 * Based on algebraic derivation and sometimes direct Brzozowski derivation
7625
 * it usually takes less than linear time and can handle expressions generating
7626
 * infinite languages.
7627
 *
7628
 * Returns the resulting expression or NULL in case of internal error, the
7629
 *         result must be freed
7630
 */
7631
xmlExpNodePtr
7632
xmlExpExpDerive(xmlExpCtxtPtr ctxt, xmlExpNodePtr exp, xmlExpNodePtr sub) {
7633
    if ((exp == NULL) || (ctxt == NULL) || (sub == NULL))
7634
        return(NULL);
7635
7636
    /*
7637
     * O(1) speedups
7638
     */
7639
    if (IS_NILLABLE(sub) && (!IS_NILLABLE(exp))) {
7640
        return(forbiddenExp);
7641
    }
7642
    if (xmlExpCheckCard(exp, sub) == 0) {
7643
        return(forbiddenExp);
7644
    }
7645
    return(xmlExpExpDeriveInt(ctxt, exp, sub));
7646
}
7647
7648
/**
7649
 * xmlExpSubsume:
7650
 * @ctxt: the expressions context
7651
 * @exp: the englobing expression
7652
 * @sub: the subexpression
7653
 *
7654
 * Check whether @exp accepts all the languages accepted by @sub
7655
 * the input being a subexpression.
7656
 *
7657
 * Returns 1 if true 0 if false and -1 in case of failure.
7658
 */
7659
int
7660
xmlExpSubsume(xmlExpCtxtPtr ctxt, xmlExpNodePtr exp, xmlExpNodePtr sub) {
7661
    xmlExpNodePtr tmp;
7662
7663
    if ((exp == NULL) || (ctxt == NULL) || (sub == NULL))
7664
        return(-1);
7665
7666
    /*
7667
     * TODO: speedup by checking the language of sub is a subset of the
7668
     *       language of exp
7669
     */
7670
    /*
7671
     * O(1) speedups
7672
     */
7673
    if (IS_NILLABLE(sub) && (!IS_NILLABLE(exp))) {
7674
        return(0);
7675
    }
7676
    if (xmlExpCheckCard(exp, sub) == 0) {
7677
        return(0);
7678
    }
7679
    tmp = xmlExpExpDeriveInt(ctxt, exp, sub);
7680
    if (tmp == NULL)
7681
        return(-1);
7682
    if (tmp == forbiddenExp)
7683
  return(0);
7684
    if (tmp == emptyExp)
7685
  return(1);
7686
    if ((tmp != NULL) && (IS_NILLABLE(tmp))) {
7687
        xmlExpFree(ctxt, tmp);
7688
        return(1);
7689
    }
7690
    xmlExpFree(ctxt, tmp);
7691
    return(0);
7692
}
7693
7694
/************************************************************************
7695
 *                  *
7696
 *      Parsing expression        *
7697
 *                  *
7698
 ************************************************************************/
7699
7700
static xmlExpNodePtr xmlExpParseExpr(xmlExpCtxtPtr ctxt);
7701
7702
#undef CUR
7703
#define CUR (*ctxt->cur)
7704
#undef NEXT
7705
#define NEXT ctxt->cur++;
7706
#undef IS_BLANK
7707
#define IS_BLANK(c) ((c == ' ') || (c == '\n') || (c == '\r') || (c == '\t'))
7708
#define SKIP_BLANKS while (IS_BLANK(*ctxt->cur)) ctxt->cur++;
7709
7710
static int
7711
xmlExpParseNumber(xmlExpCtxtPtr ctxt) {
7712
    int ret = 0;
7713
7714
    SKIP_BLANKS
7715
    if (CUR == '*') {
7716
  NEXT
7717
  return(-1);
7718
    }
7719
    if ((CUR < '0') || (CUR > '9'))
7720
        return(-1);
7721
    while ((CUR >= '0') && (CUR <= '9')) {
7722
        ret = ret * 10 + (CUR - '0');
7723
  NEXT
7724
    }
7725
    return(ret);
7726
}
7727
7728
static xmlExpNodePtr
7729
xmlExpParseOr(xmlExpCtxtPtr ctxt) {
7730
    const char *base;
7731
    xmlExpNodePtr ret;
7732
    const xmlChar *val;
7733
7734
    SKIP_BLANKS
7735
    base = ctxt->cur;
7736
    if (*ctxt->cur == '(') {
7737
        NEXT
7738
  ret = xmlExpParseExpr(ctxt);
7739
  SKIP_BLANKS
7740
  if (*ctxt->cur != ')') {
7741
      fprintf(stderr, "unbalanced '(' : %s\n", base);
7742
      xmlExpFree(ctxt, ret);
7743
      return(NULL);
7744
  }
7745
  NEXT;
7746
  SKIP_BLANKS
7747
  goto parse_quantifier;
7748
    }
7749
    while ((CUR != 0) && (!(IS_BLANK(CUR))) && (CUR != '(') &&
7750
           (CUR != ')') && (CUR != '|') && (CUR != ',') && (CUR != '{') &&
7751
     (CUR != '*') && (CUR != '+') && (CUR != '?') && (CUR != '}'))
7752
  NEXT;
7753
    val = xmlDictLookup(ctxt->dict, BAD_CAST base, ctxt->cur - base);
7754
    if (val == NULL)
7755
        return(NULL);
7756
    ret = xmlExpHashGetEntry(ctxt, XML_EXP_ATOM, NULL, NULL, val, 0, 0);
7757
    if (ret == NULL)
7758
        return(NULL);
7759
    SKIP_BLANKS
7760
parse_quantifier:
7761
    if (CUR == '{') {
7762
        int min, max;
7763
7764
        NEXT
7765
  min = xmlExpParseNumber(ctxt);
7766
  if (min < 0) {
7767
      xmlExpFree(ctxt, ret);
7768
      return(NULL);
7769
  }
7770
  SKIP_BLANKS
7771
  if (CUR == ',') {
7772
      NEXT
7773
      max = xmlExpParseNumber(ctxt);
7774
      SKIP_BLANKS
7775
  } else
7776
      max = min;
7777
  if (CUR != '}') {
7778
      xmlExpFree(ctxt, ret);
7779
      return(NULL);
7780
  }
7781
        NEXT
7782
  ret = xmlExpHashGetEntry(ctxt, XML_EXP_COUNT, ret, NULL, NULL,
7783
                           min, max);
7784
  SKIP_BLANKS
7785
    } else if (CUR == '?') {
7786
        NEXT
7787
  ret = xmlExpHashGetEntry(ctxt, XML_EXP_COUNT, ret, NULL, NULL,
7788
                           0, 1);
7789
  SKIP_BLANKS
7790
    } else if (CUR == '+') {
7791
        NEXT
7792
  ret = xmlExpHashGetEntry(ctxt, XML_EXP_COUNT, ret, NULL, NULL,
7793
                           1, -1);
7794
  SKIP_BLANKS
7795
    } else if (CUR == '*') {
7796
        NEXT
7797
  ret = xmlExpHashGetEntry(ctxt, XML_EXP_COUNT, ret, NULL, NULL,
7798
                           0, -1);
7799
  SKIP_BLANKS
7800
    }
7801
    return(ret);
7802
}
7803
7804
7805
static xmlExpNodePtr
7806
xmlExpParseSeq(xmlExpCtxtPtr ctxt) {
7807
    xmlExpNodePtr ret, right;
7808
7809
    ret = xmlExpParseOr(ctxt);
7810
    SKIP_BLANKS
7811
    while (CUR == '|') {
7812
        NEXT
7813
  right = xmlExpParseOr(ctxt);
7814
  if (right == NULL) {
7815
      xmlExpFree(ctxt, ret);
7816
      return(NULL);
7817
  }
7818
  ret = xmlExpHashGetEntry(ctxt, XML_EXP_OR, ret, right, NULL, 0, 0);
7819
  if (ret == NULL)
7820
      return(NULL);
7821
    }
7822
    return(ret);
7823
}
7824
7825
static xmlExpNodePtr
7826
xmlExpParseExpr(xmlExpCtxtPtr ctxt) {
7827
    xmlExpNodePtr ret, right;
7828
7829
    ret = xmlExpParseSeq(ctxt);
7830
    SKIP_BLANKS
7831
    while (CUR == ',') {
7832
        NEXT
7833
  right = xmlExpParseSeq(ctxt);
7834
  if (right == NULL) {
7835
      xmlExpFree(ctxt, ret);
7836
      return(NULL);
7837
  }
7838
  ret = xmlExpHashGetEntry(ctxt, XML_EXP_SEQ, ret, right, NULL, 0, 0);
7839
  if (ret == NULL)
7840
      return(NULL);
7841
    }
7842
    return(ret);
7843
}
7844
7845
/**
7846
 * xmlExpParse:
7847
 * @ctxt: the expressions context
7848
 * @expr: the 0 terminated string
7849
 *
7850
 * Minimal parser for regexps, it understand the following constructs
7851
 *  - string terminals
7852
 *  - choice operator |
7853
 *  - sequence operator ,
7854
 *  - subexpressions (...)
7855
 *  - usual cardinality operators + * and ?
7856
 *  - finite sequences  { min, max }
7857
 *  - infinite sequences { min, * }
7858
 * There is minimal checkings made especially no checking on strings values
7859
 *
7860
 * Returns a new expression or NULL in case of failure
7861
 */
7862
xmlExpNodePtr
7863
xmlExpParse(xmlExpCtxtPtr ctxt, const char *expr) {
7864
    xmlExpNodePtr ret;
7865
7866
    ctxt->expr = expr;
7867
    ctxt->cur = expr;
7868
7869
    ret = xmlExpParseExpr(ctxt);
7870
    SKIP_BLANKS
7871
    if (*ctxt->cur != 0) {
7872
        xmlExpFree(ctxt, ret);
7873
        return(NULL);
7874
    }
7875
    return(ret);
7876
}
7877
7878
static void
7879
xmlExpDumpInt(xmlBufferPtr buf, xmlExpNodePtr expr, int glob) {
7880
    xmlExpNodePtr c;
7881
7882
    if (expr == NULL) return;
7883
    if (glob) xmlBufferWriteChar(buf, "(");
7884
    switch (expr->type) {
7885
        case XML_EXP_EMPTY:
7886
      xmlBufferWriteChar(buf, "empty");
7887
      break;
7888
        case XML_EXP_FORBID:
7889
      xmlBufferWriteChar(buf, "forbidden");
7890
      break;
7891
        case XML_EXP_ATOM:
7892
      xmlBufferWriteCHAR(buf, expr->exp_str);
7893
      break;
7894
        case XML_EXP_SEQ:
7895
      c = expr->exp_left;
7896
      if ((c->type == XML_EXP_SEQ) || (c->type == XML_EXP_OR))
7897
          xmlExpDumpInt(buf, c, 1);
7898
      else
7899
          xmlExpDumpInt(buf, c, 0);
7900
      xmlBufferWriteChar(buf, " , ");
7901
      c = expr->exp_right;
7902
      if ((c->type == XML_EXP_SEQ) || (c->type == XML_EXP_OR))
7903
          xmlExpDumpInt(buf, c, 1);
7904
      else
7905
          xmlExpDumpInt(buf, c, 0);
7906
            break;
7907
        case XML_EXP_OR:
7908
      c = expr->exp_left;
7909
      if ((c->type == XML_EXP_SEQ) || (c->type == XML_EXP_OR))
7910
          xmlExpDumpInt(buf, c, 1);
7911
      else
7912
          xmlExpDumpInt(buf, c, 0);
7913
      xmlBufferWriteChar(buf, " | ");
7914
      c = expr->exp_right;
7915
      if ((c->type == XML_EXP_SEQ) || (c->type == XML_EXP_OR))
7916
          xmlExpDumpInt(buf, c, 1);
7917
      else
7918
          xmlExpDumpInt(buf, c, 0);
7919
            break;
7920
        case XML_EXP_COUNT: {
7921
      char rep[40];
7922
7923
      c = expr->exp_left;
7924
      if ((c->type == XML_EXP_SEQ) || (c->type == XML_EXP_OR))
7925
          xmlExpDumpInt(buf, c, 1);
7926
      else
7927
          xmlExpDumpInt(buf, c, 0);
7928
      if ((expr->exp_min == 0) && (expr->exp_max == 1)) {
7929
    rep[0] = '?';
7930
    rep[1] = 0;
7931
      } else if ((expr->exp_min == 0) && (expr->exp_max == -1)) {
7932
    rep[0] = '*';
7933
    rep[1] = 0;
7934
      } else if ((expr->exp_min == 1) && (expr->exp_max == -1)) {
7935
    rep[0] = '+';
7936
    rep[1] = 0;
7937
      } else if (expr->exp_max == expr->exp_min) {
7938
          snprintf(rep, 39, "{%d}", expr->exp_min);
7939
      } else if (expr->exp_max < 0) {
7940
          snprintf(rep, 39, "{%d,inf}", expr->exp_min);
7941
      } else {
7942
          snprintf(rep, 39, "{%d,%d}", expr->exp_min, expr->exp_max);
7943
      }
7944
      rep[39] = 0;
7945
      xmlBufferWriteChar(buf, rep);
7946
      break;
7947
  }
7948
  default:
7949
      fprintf(stderr, "Error in tree\n");
7950
    }
7951
    if (glob)
7952
        xmlBufferWriteChar(buf, ")");
7953
}
7954
/**
7955
 * xmlExpDump:
7956
 * @buf:  a buffer to receive the output
7957
 * @expr:  the compiled expression
7958
 *
7959
 * Serialize the expression as compiled to the buffer
7960
 */
7961
void
7962
xmlExpDump(xmlBufferPtr buf, xmlExpNodePtr expr) {
7963
    if ((buf == NULL) || (expr == NULL))
7964
        return;
7965
    xmlExpDumpInt(buf, expr, 0);
7966
}
7967
7968
/**
7969
 * xmlExpMaxToken:
7970
 * @expr: a compiled expression
7971
 *
7972
 * Indicate the maximum number of input a expression can accept
7973
 *
7974
 * Returns the maximum length or -1 in case of error
7975
 */
7976
int
7977
xmlExpMaxToken(xmlExpNodePtr expr) {
7978
    if (expr == NULL)
7979
        return(-1);
7980
    return(expr->c_max);
7981
}
7982
7983
/**
7984
 * xmlExpCtxtNbNodes:
7985
 * @ctxt: an expression context
7986
 *
7987
 * Debugging facility provides the number of allocated nodes at a that point
7988
 *
7989
 * Returns the number of nodes in use or -1 in case of error
7990
 */
7991
int
7992
xmlExpCtxtNbNodes(xmlExpCtxtPtr ctxt) {
7993
    if (ctxt == NULL)
7994
        return(-1);
7995
    return(ctxt->nb_nodes);
7996
}
7997
7998
/**
7999
 * xmlExpCtxtNbCons:
8000
 * @ctxt: an expression context
8001
 *
8002
 * Debugging facility provides the number of allocated nodes over lifetime
8003
 *
8004
 * Returns the number of nodes ever allocated or -1 in case of error
8005
 */
8006
int
8007
xmlExpCtxtNbCons(xmlExpCtxtPtr ctxt) {
8008
    if (ctxt == NULL)
8009
        return(-1);
8010
    return(ctxt->nb_cons);
8011
}
8012
8013
#endif /* LIBXML_EXPR_ENABLED */
8014
8015
#endif /* LIBXML_REGEXP_ENABLED */