Coverage Report

Created: 2024-02-04 06:19

/src/libxml2/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 void
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
    }
3514
0
}
3515
3516
static void
3517
xmlFARegExecSaveInputString(xmlRegExecCtxtPtr exec, const xmlChar *value,
3518
0
                      void *data) {
3519
0
    if (exec->inputStackMax == 0) {
3520
0
  exec->inputStackMax = 4;
3521
0
  exec->inputStack = (xmlRegInputTokenPtr)
3522
0
      xmlMalloc(exec->inputStackMax * sizeof(xmlRegInputToken));
3523
0
  if (exec->inputStack == NULL) {
3524
0
      exec->inputStackMax = 0;
3525
0
            exec->status = XML_REGEXP_OUT_OF_MEMORY;
3526
0
      return;
3527
0
  }
3528
0
    } else if (exec->inputStackNr + 1 >= exec->inputStackMax) {
3529
0
  xmlRegInputTokenPtr tmp;
3530
3531
0
  exec->inputStackMax *= 2;
3532
0
  tmp = (xmlRegInputTokenPtr) xmlRealloc(exec->inputStack,
3533
0
      exec->inputStackMax * sizeof(xmlRegInputToken));
3534
0
  if (tmp == NULL) {
3535
0
      exec->inputStackMax /= 2;
3536
0
            exec->status = XML_REGEXP_OUT_OF_MEMORY;
3537
0
      return;
3538
0
  }
3539
0
  exec->inputStack = tmp;
3540
0
    }
3541
0
    if (value == NULL) {
3542
0
        exec->inputStack[exec->inputStackNr].value = NULL;
3543
0
    } else {
3544
0
        exec->inputStack[exec->inputStackNr].value = xmlStrdup(value);
3545
0
        if (exec->inputStack[exec->inputStackNr].value == NULL) {
3546
0
            exec->status = XML_REGEXP_OUT_OF_MEMORY;
3547
0
            return;
3548
0
        }
3549
0
    }
3550
0
    exec->inputStack[exec->inputStackNr].data = data;
3551
0
    exec->inputStackNr++;
3552
0
    exec->inputStack[exec->inputStackNr].value = NULL;
3553
0
    exec->inputStack[exec->inputStackNr].data = NULL;
3554
0
}
3555
3556
/**
3557
 * xmlRegStrEqualWildcard:
3558
 * @expStr:  the string to be evaluated
3559
 * @valStr:  the validation string
3560
 *
3561
 * Checks if both strings are equal or have the same content. "*"
3562
 * can be used as a wildcard in @valStr; "|" is used as a separator of
3563
 * substrings in both @expStr and @valStr.
3564
 *
3565
 * Returns 1 if the comparison is satisfied and the number of substrings
3566
 * is equal, 0 otherwise.
3567
 */
3568
3569
static int
3570
0
xmlRegStrEqualWildcard(const xmlChar *expStr, const xmlChar *valStr) {
3571
0
    if (expStr == valStr) return(1);
3572
0
    if (expStr == NULL) return(0);
3573
0
    if (valStr == NULL) return(0);
3574
0
    do {
3575
  /*
3576
  * Eval if we have a wildcard for the current item.
3577
  */
3578
0
        if (*expStr != *valStr) {
3579
      /* if one of them starts with a wildcard make valStr be it */
3580
0
      if (*valStr == '*') {
3581
0
          const xmlChar *tmp;
3582
3583
0
    tmp = valStr;
3584
0
    valStr = expStr;
3585
0
    expStr = tmp;
3586
0
      }
3587
0
      if ((*valStr != 0) && (*expStr != 0) && (*expStr++ == '*')) {
3588
0
    do {
3589
0
        if (*valStr == XML_REG_STRING_SEPARATOR)
3590
0
      break;
3591
0
        valStr++;
3592
0
    } while (*valStr != 0);
3593
0
    continue;
3594
0
      } else
3595
0
    return(0);
3596
0
  }
3597
0
  expStr++;
3598
0
  valStr++;
3599
0
    } while (*valStr != 0);
3600
0
    if (*expStr != 0)
3601
0
  return (0);
3602
0
    else
3603
0
  return (1);
3604
0
}
3605
3606
/**
3607
 * xmlRegCompactPushString:
3608
 * @exec: a regexp execution context
3609
 * @comp:  the precompiled exec with a compact table
3610
 * @value: a string token input
3611
 * @data: data associated to the token to reuse in callbacks
3612
 *
3613
 * Push one input token in the execution context
3614
 *
3615
 * Returns: 1 if the regexp reached a final state, 0 if non-final, and
3616
 *     a negative value in case of error.
3617
 */
3618
static int
3619
xmlRegCompactPushString(xmlRegExecCtxtPtr exec,
3620
                  xmlRegexpPtr comp,
3621
                  const xmlChar *value,
3622
0
                  void *data) {
3623
0
    int state = exec->index;
3624
0
    int i, target;
3625
3626
0
    if ((comp == NULL) || (comp->compact == NULL) || (comp->stringMap == NULL))
3627
0
  return(-1);
3628
3629
0
    if (value == NULL) {
3630
  /*
3631
   * are we at a final state ?
3632
   */
3633
0
  if (comp->compact[state * (comp->nbstrings + 1)] ==
3634
0
            XML_REGEXP_FINAL_STATE)
3635
0
      return(1);
3636
0
  return(0);
3637
0
    }
3638
3639
    /*
3640
     * Examine all outside transitions from current state
3641
     */
3642
0
    for (i = 0;i < comp->nbstrings;i++) {
3643
0
  target = comp->compact[state * (comp->nbstrings + 1) + i + 1];
3644
0
  if ((target > 0) && (target <= comp->nbstates)) {
3645
0
      target--; /* to avoid 0 */
3646
0
      if (xmlRegStrEqualWildcard(comp->stringMap[i], value)) {
3647
0
    exec->index = target;
3648
0
    if ((exec->callback != NULL) && (comp->transdata != NULL)) {
3649
0
        exec->callback(exec->data, value,
3650
0
        comp->transdata[state * comp->nbstrings + i], data);
3651
0
    }
3652
0
    if (comp->compact[target * (comp->nbstrings + 1)] ==
3653
0
        XML_REGEXP_SINK_STATE)
3654
0
        goto error;
3655
3656
0
    if (comp->compact[target * (comp->nbstrings + 1)] ==
3657
0
        XML_REGEXP_FINAL_STATE)
3658
0
        return(1);
3659
0
    return(0);
3660
0
      }
3661
0
  }
3662
0
    }
3663
    /*
3664
     * Failed to find an exit transition out from current state for the
3665
     * current token
3666
     */
3667
0
error:
3668
0
    exec->errStateNo = state;
3669
0
    exec->status = XML_REGEXP_NOT_FOUND;
3670
0
    xmlRegExecSetErrString(exec, value);
3671
0
    return(exec->status);
3672
0
}
3673
3674
/**
3675
 * xmlRegExecPushStringInternal:
3676
 * @exec: a regexp execution context or NULL to indicate the end
3677
 * @value: a string token input
3678
 * @data: data associated to the token to reuse in callbacks
3679
 * @compound: value was assembled from 2 strings
3680
 *
3681
 * Push one input token in the execution context
3682
 *
3683
 * Returns: 1 if the regexp reached a final state, 0 if non-final, and
3684
 *     a negative value in case of error.
3685
 */
3686
static int
3687
xmlRegExecPushStringInternal(xmlRegExecCtxtPtr exec, const xmlChar *value,
3688
0
                       void *data, int compound) {
3689
0
    xmlRegTransPtr trans;
3690
0
    xmlRegAtomPtr atom;
3691
0
    int ret;
3692
0
    int final = 0;
3693
0
    int progress = 1;
3694
3695
0
    if (exec == NULL)
3696
0
  return(-1);
3697
0
    if (exec->comp == NULL)
3698
0
  return(-1);
3699
0
    if (exec->status != XML_REGEXP_OK)
3700
0
  return(exec->status);
3701
3702
0
    if (exec->comp->compact != NULL)
3703
0
  return(xmlRegCompactPushString(exec, exec->comp, value, data));
3704
3705
0
    if (value == NULL) {
3706
0
        if (exec->state->type == XML_REGEXP_FINAL_STATE)
3707
0
      return(1);
3708
0
  final = 1;
3709
0
    }
3710
3711
    /*
3712
     * If we have an active rollback stack push the new value there
3713
     * and get back to where we were left
3714
     */
3715
0
    if ((value != NULL) && (exec->inputStackNr > 0)) {
3716
0
  xmlFARegExecSaveInputString(exec, value, data);
3717
0
  value = exec->inputStack[exec->index].value;
3718
0
  data = exec->inputStack[exec->index].data;
3719
0
    }
3720
3721
0
    while ((exec->status == XML_REGEXP_OK) &&
3722
0
     ((value != NULL) ||
3723
0
      ((final == 1) &&
3724
0
       (exec->state->type != XML_REGEXP_FINAL_STATE)))) {
3725
3726
  /*
3727
   * End of input on non-terminal state, rollback, however we may
3728
   * still have epsilon like transition for counted transitions
3729
   * on counters, in that case don't break too early.
3730
   */
3731
0
  if ((value == NULL) && (exec->counts == NULL))
3732
0
      goto rollback;
3733
3734
0
  exec->transcount = 0;
3735
0
  for (;exec->transno < exec->state->nbTrans;exec->transno++) {
3736
0
      trans = &exec->state->trans[exec->transno];
3737
0
      if (trans->to < 0)
3738
0
    continue;
3739
0
      atom = trans->atom;
3740
0
      ret = 0;
3741
0
      if (trans->count == REGEXP_ALL_LAX_COUNTER) {
3742
0
    int i;
3743
0
    int count;
3744
0
    xmlRegTransPtr t;
3745
0
    xmlRegCounterPtr counter;
3746
3747
0
    ret = 0;
3748
3749
    /*
3750
     * Check all counted transitions from the current state
3751
     */
3752
0
    if ((value == NULL) && (final)) {
3753
0
        ret = 1;
3754
0
    } else if (value != NULL) {
3755
0
        for (i = 0;i < exec->state->nbTrans;i++) {
3756
0
      t = &exec->state->trans[i];
3757
0
      if ((t->counter < 0) || (t == trans))
3758
0
          continue;
3759
0
      counter = &exec->comp->counters[t->counter];
3760
0
      count = exec->counts[t->counter];
3761
0
      if ((count < counter->max) &&
3762
0
                (t->atom != NULL) &&
3763
0
          (xmlStrEqual(value, t->atom->valuep))) {
3764
0
          ret = 0;
3765
0
          break;
3766
0
      }
3767
0
      if ((count >= counter->min) &&
3768
0
          (count < counter->max) &&
3769
0
          (t->atom != NULL) &&
3770
0
          (xmlStrEqual(value, t->atom->valuep))) {
3771
0
          ret = 1;
3772
0
          break;
3773
0
      }
3774
0
        }
3775
0
    }
3776
0
      } else if (trans->count == REGEXP_ALL_COUNTER) {
3777
0
    int i;
3778
0
    int count;
3779
0
    xmlRegTransPtr t;
3780
0
    xmlRegCounterPtr counter;
3781
3782
0
    ret = 1;
3783
3784
    /*
3785
     * Check all counted transitions from the current state
3786
     */
3787
0
    for (i = 0;i < exec->state->nbTrans;i++) {
3788
0
                    t = &exec->state->trans[i];
3789
0
        if ((t->counter < 0) || (t == trans))
3790
0
      continue;
3791
0
                    counter = &exec->comp->counters[t->counter];
3792
0
        count = exec->counts[t->counter];
3793
0
        if ((count < counter->min) || (count > counter->max)) {
3794
0
      ret = 0;
3795
0
      break;
3796
0
        }
3797
0
    }
3798
0
      } else if (trans->count >= 0) {
3799
0
    int count;
3800
0
    xmlRegCounterPtr counter;
3801
3802
    /*
3803
     * A counted transition.
3804
     */
3805
3806
0
    count = exec->counts[trans->count];
3807
0
    counter = &exec->comp->counters[trans->count];
3808
0
    ret = ((count >= counter->min) && (count <= counter->max));
3809
0
      } else if (atom == NULL) {
3810
0
    fprintf(stderr, "epsilon transition left at runtime\n");
3811
0
    exec->status = XML_REGEXP_INTERNAL_ERROR;
3812
0
    break;
3813
0
      } else if (value != NULL) {
3814
0
    ret = xmlRegStrEqualWildcard(atom->valuep, value);
3815
0
    if (atom->neg) {
3816
0
        ret = !ret;
3817
0
        if (!compound)
3818
0
            ret = 0;
3819
0
    }
3820
0
    if ((ret == 1) && (trans->counter >= 0)) {
3821
0
        xmlRegCounterPtr counter;
3822
0
        int count;
3823
3824
0
        count = exec->counts[trans->counter];
3825
0
        counter = &exec->comp->counters[trans->counter];
3826
0
        if (count >= counter->max)
3827
0
      ret = 0;
3828
0
    }
3829
3830
0
    if ((ret == 1) && (atom->min > 0) && (atom->max > 0)) {
3831
0
        xmlRegStatePtr to = exec->comp->states[trans->to];
3832
3833
        /*
3834
         * this is a multiple input sequence
3835
         */
3836
0
        if (exec->state->nbTrans > exec->transno + 1) {
3837
0
      if (exec->inputStackNr <= 0) {
3838
0
          xmlFARegExecSaveInputString(exec, value, data);
3839
0
      }
3840
0
      xmlFARegExecSave(exec);
3841
0
        }
3842
0
        exec->transcount = 1;
3843
0
        do {
3844
      /*
3845
       * Try to progress as much as possible on the input
3846
       */
3847
0
      if (exec->transcount == atom->max) {
3848
0
          break;
3849
0
      }
3850
0
      exec->index++;
3851
0
      value = exec->inputStack[exec->index].value;
3852
0
      data = exec->inputStack[exec->index].data;
3853
3854
      /*
3855
       * End of input: stop here
3856
       */
3857
0
      if (value == NULL) {
3858
0
          exec->index --;
3859
0
          break;
3860
0
      }
3861
0
      if (exec->transcount >= atom->min) {
3862
0
          int transno = exec->transno;
3863
0
          xmlRegStatePtr state = exec->state;
3864
3865
          /*
3866
           * The transition is acceptable save it
3867
           */
3868
0
          exec->transno = -1; /* trick */
3869
0
          exec->state = to;
3870
0
          if (exec->inputStackNr <= 0) {
3871
0
        xmlFARegExecSaveInputString(exec, value, data);
3872
0
          }
3873
0
          xmlFARegExecSave(exec);
3874
0
          exec->transno = transno;
3875
0
          exec->state = state;
3876
0
      }
3877
0
      ret = xmlStrEqual(value, atom->valuep);
3878
0
      exec->transcount++;
3879
0
        } while (ret == 1);
3880
0
        if (exec->transcount < atom->min)
3881
0
      ret = 0;
3882
3883
        /*
3884
         * If the last check failed but one transition was found
3885
         * possible, rollback
3886
         */
3887
0
        if (ret < 0)
3888
0
      ret = 0;
3889
0
        if (ret == 0) {
3890
0
      goto rollback;
3891
0
        }
3892
0
    }
3893
0
      }
3894
0
      if (ret == 1) {
3895
0
    if ((exec->callback != NULL) && (atom != NULL) &&
3896
0
      (data != NULL)) {
3897
0
        exec->callback(exec->data, atom->valuep,
3898
0
                 atom->data, data);
3899
0
    }
3900
0
    if (exec->state->nbTrans > exec->transno + 1) {
3901
0
        if (exec->inputStackNr <= 0) {
3902
0
      xmlFARegExecSaveInputString(exec, value, data);
3903
0
        }
3904
0
        xmlFARegExecSave(exec);
3905
0
    }
3906
0
    if (trans->counter >= 0) {
3907
0
        exec->counts[trans->counter]++;
3908
0
    }
3909
0
    if ((trans->count >= 0) &&
3910
0
        (trans->count < REGEXP_ALL_COUNTER)) {
3911
0
        exec->counts[trans->count] = 0;
3912
0
    }
3913
0
                if ((exec->comp->states[trans->to] != NULL) &&
3914
0
        (exec->comp->states[trans->to]->type ==
3915
0
         XML_REGEXP_SINK_STATE)) {
3916
        /*
3917
         * entering a sink state, save the current state as error
3918
         * state.
3919
         */
3920
0
                    xmlRegExecSetErrString(exec, value);
3921
0
        exec->errState = exec->state;
3922
0
        memcpy(exec->errCounts, exec->counts,
3923
0
         exec->comp->nbCounters * sizeof(int));
3924
0
    }
3925
0
    exec->state = exec->comp->states[trans->to];
3926
0
    exec->transno = 0;
3927
0
    if (trans->atom != NULL) {
3928
0
        if (exec->inputStack != NULL) {
3929
0
      exec->index++;
3930
0
      if (exec->index < exec->inputStackNr) {
3931
0
          value = exec->inputStack[exec->index].value;
3932
0
          data = exec->inputStack[exec->index].data;
3933
0
      } else {
3934
0
          value = NULL;
3935
0
          data = NULL;
3936
0
      }
3937
0
        } else {
3938
0
      value = NULL;
3939
0
      data = NULL;
3940
0
        }
3941
0
    }
3942
0
    goto progress;
3943
0
      } else if (ret < 0) {
3944
0
    exec->status = XML_REGEXP_INTERNAL_ERROR;
3945
0
    break;
3946
0
      }
3947
0
  }
3948
0
  if ((exec->transno != 0) || (exec->state->nbTrans == 0)) {
3949
0
rollback:
3950
            /*
3951
       * if we didn't yet rollback on the current input
3952
       * store the current state as the error state.
3953
       */
3954
0
      if ((progress) && (exec->state != NULL) &&
3955
0
          (exec->state->type != XML_REGEXP_SINK_STATE)) {
3956
0
          progress = 0;
3957
0
                xmlRegExecSetErrString(exec, value);
3958
0
    exec->errState = exec->state;
3959
0
                if (exec->comp->nbCounters)
3960
0
                    memcpy(exec->errCounts, exec->counts,
3961
0
                           exec->comp->nbCounters * sizeof(int));
3962
0
      }
3963
3964
      /*
3965
       * Failed to find a way out
3966
       */
3967
0
      exec->determinist = 0;
3968
0
      xmlFARegExecRollBack(exec);
3969
0
      if ((exec->inputStack != NULL ) &&
3970
0
                (exec->status == XML_REGEXP_OK)) {
3971
0
    value = exec->inputStack[exec->index].value;
3972
0
    data = exec->inputStack[exec->index].data;
3973
0
      }
3974
0
  }
3975
0
  continue;
3976
0
progress:
3977
0
        progress = 1;
3978
0
  continue;
3979
0
    }
3980
0
    if (exec->status == XML_REGEXP_OK) {
3981
0
        return(exec->state->type == XML_REGEXP_FINAL_STATE);
3982
0
    }
3983
0
    return(exec->status);
3984
0
}
3985
3986
/**
3987
 * xmlRegExecPushString:
3988
 * @exec: a regexp execution context or NULL to indicate the end
3989
 * @value: a string token input
3990
 * @data: data associated to the token to reuse in callbacks
3991
 *
3992
 * Push one input token in the execution context
3993
 *
3994
 * Returns: 1 if the regexp reached a final state, 0 if non-final, and
3995
 *     a negative value in case of error.
3996
 */
3997
int
3998
xmlRegExecPushString(xmlRegExecCtxtPtr exec, const xmlChar *value,
3999
0
               void *data) {
4000
0
    return(xmlRegExecPushStringInternal(exec, value, data, 0));
4001
0
}
4002
4003
/**
4004
 * xmlRegExecPushString2:
4005
 * @exec: a regexp execution context or NULL to indicate the end
4006
 * @value: the first string token input
4007
 * @value2: the second string token input
4008
 * @data: data associated to the token to reuse in callbacks
4009
 *
4010
 * Push one input token in the execution context
4011
 *
4012
 * Returns: 1 if the regexp reached a final state, 0 if non-final, and
4013
 *     a negative value in case of error.
4014
 */
4015
int
4016
xmlRegExecPushString2(xmlRegExecCtxtPtr exec, const xmlChar *value,
4017
0
                      const xmlChar *value2, void *data) {
4018
0
    xmlChar buf[150];
4019
0
    int lenn, lenp, ret;
4020
0
    xmlChar *str;
4021
4022
0
    if (exec == NULL)
4023
0
  return(-1);
4024
0
    if (exec->comp == NULL)
4025
0
  return(-1);
4026
0
    if (exec->status != XML_REGEXP_OK)
4027
0
  return(exec->status);
4028
4029
0
    if (value2 == NULL)
4030
0
        return(xmlRegExecPushString(exec, value, data));
4031
4032
0
    lenn = strlen((char *) value2);
4033
0
    lenp = strlen((char *) value);
4034
4035
0
    if (150 < lenn + lenp + 2) {
4036
0
  str = (xmlChar *) xmlMallocAtomic(lenn + lenp + 2);
4037
0
  if (str == NULL) {
4038
0
      exec->status = XML_REGEXP_OUT_OF_MEMORY;
4039
0
      return(-1);
4040
0
  }
4041
0
    } else {
4042
0
  str = buf;
4043
0
    }
4044
0
    memcpy(&str[0], value, lenp);
4045
0
    str[lenp] = XML_REG_STRING_SEPARATOR;
4046
0
    memcpy(&str[lenp + 1], value2, lenn);
4047
0
    str[lenn + lenp + 1] = 0;
4048
4049
0
    if (exec->comp->compact != NULL)
4050
0
  ret = xmlRegCompactPushString(exec, exec->comp, str, data);
4051
0
    else
4052
0
        ret = xmlRegExecPushStringInternal(exec, str, data, 1);
4053
4054
0
    if (str != buf)
4055
0
        xmlFree(str);
4056
0
    return(ret);
4057
0
}
4058
4059
/**
4060
 * xmlRegExecGetValues:
4061
 * @exec: a regexp execution context
4062
 * @err: error extraction or normal one
4063
 * @nbval: pointer to the number of accepted values IN/OUT
4064
 * @nbneg: return number of negative transitions
4065
 * @values: pointer to the array of acceptable values
4066
 * @terminal: return value if this was a terminal state
4067
 *
4068
 * Extract information from the regexp execution, internal routine to
4069
 * implement xmlRegExecNextValues() and xmlRegExecErrInfo()
4070
 *
4071
 * Returns: 0 in case of success or -1 in case of error.
4072
 */
4073
static int
4074
xmlRegExecGetValues(xmlRegExecCtxtPtr exec, int err,
4075
                    int *nbval, int *nbneg,
4076
0
        xmlChar **values, int *terminal) {
4077
0
    int maxval;
4078
0
    int nb = 0;
4079
4080
0
    if ((exec == NULL) || (nbval == NULL) || (nbneg == NULL) ||
4081
0
        (values == NULL) || (*nbval <= 0))
4082
0
        return(-1);
4083
4084
0
    maxval = *nbval;
4085
0
    *nbval = 0;
4086
0
    *nbneg = 0;
4087
0
    if ((exec->comp != NULL) && (exec->comp->compact != NULL)) {
4088
0
        xmlRegexpPtr comp;
4089
0
  int target, i, state;
4090
4091
0
        comp = exec->comp;
4092
4093
0
  if (err) {
4094
0
      if (exec->errStateNo == -1) return(-1);
4095
0
      state = exec->errStateNo;
4096
0
  } else {
4097
0
      state = exec->index;
4098
0
  }
4099
0
  if (terminal != NULL) {
4100
0
      if (comp->compact[state * (comp->nbstrings + 1)] ==
4101
0
          XML_REGEXP_FINAL_STATE)
4102
0
    *terminal = 1;
4103
0
      else
4104
0
    *terminal = 0;
4105
0
  }
4106
0
  for (i = 0;(i < comp->nbstrings) && (nb < maxval);i++) {
4107
0
      target = comp->compact[state * (comp->nbstrings + 1) + i + 1];
4108
0
      if ((target > 0) && (target <= comp->nbstates) &&
4109
0
          (comp->compact[(target - 1) * (comp->nbstrings + 1)] !=
4110
0
     XML_REGEXP_SINK_STATE)) {
4111
0
          values[nb++] = comp->stringMap[i];
4112
0
    (*nbval)++;
4113
0
      }
4114
0
  }
4115
0
  for (i = 0;(i < comp->nbstrings) && (nb < maxval);i++) {
4116
0
      target = comp->compact[state * (comp->nbstrings + 1) + i + 1];
4117
0
      if ((target > 0) && (target <= comp->nbstates) &&
4118
0
          (comp->compact[(target - 1) * (comp->nbstrings + 1)] ==
4119
0
     XML_REGEXP_SINK_STATE)) {
4120
0
          values[nb++] = comp->stringMap[i];
4121
0
    (*nbneg)++;
4122
0
      }
4123
0
  }
4124
0
    } else {
4125
0
        int transno;
4126
0
  xmlRegTransPtr trans;
4127
0
  xmlRegAtomPtr atom;
4128
0
  xmlRegStatePtr state;
4129
4130
0
  if (terminal != NULL) {
4131
0
      if (exec->state->type == XML_REGEXP_FINAL_STATE)
4132
0
    *terminal = 1;
4133
0
      else
4134
0
    *terminal = 0;
4135
0
  }
4136
4137
0
  if (err) {
4138
0
      if (exec->errState == NULL) return(-1);
4139
0
      state = exec->errState;
4140
0
  } else {
4141
0
      if (exec->state == NULL) return(-1);
4142
0
      state = exec->state;
4143
0
  }
4144
0
  for (transno = 0;
4145
0
       (transno < state->nbTrans) && (nb < maxval);
4146
0
       transno++) {
4147
0
      trans = &state->trans[transno];
4148
0
      if (trans->to < 0)
4149
0
    continue;
4150
0
      atom = trans->atom;
4151
0
      if ((atom == NULL) || (atom->valuep == NULL))
4152
0
    continue;
4153
0
      if (trans->count == REGEXP_ALL_LAX_COUNTER) {
4154
          /* this should not be reached but ... */
4155
0
      } else if (trans->count == REGEXP_ALL_COUNTER) {
4156
          /* this should not be reached but ... */
4157
0
      } else if (trans->counter >= 0) {
4158
0
    xmlRegCounterPtr counter = NULL;
4159
0
    int count;
4160
4161
0
    if (err)
4162
0
        count = exec->errCounts[trans->counter];
4163
0
    else
4164
0
        count = exec->counts[trans->counter];
4165
0
    if (exec->comp != NULL)
4166
0
        counter = &exec->comp->counters[trans->counter];
4167
0
    if ((counter == NULL) || (count < counter->max)) {
4168
0
        if (atom->neg)
4169
0
      values[nb++] = (xmlChar *) atom->valuep2;
4170
0
        else
4171
0
      values[nb++] = (xmlChar *) atom->valuep;
4172
0
        (*nbval)++;
4173
0
    }
4174
0
      } else {
4175
0
                if ((exec->comp != NULL) && (exec->comp->states[trans->to] != NULL) &&
4176
0
        (exec->comp->states[trans->to]->type !=
4177
0
         XML_REGEXP_SINK_STATE)) {
4178
0
        if (atom->neg)
4179
0
      values[nb++] = (xmlChar *) atom->valuep2;
4180
0
        else
4181
0
      values[nb++] = (xmlChar *) atom->valuep;
4182
0
        (*nbval)++;
4183
0
    }
4184
0
      }
4185
0
  }
4186
0
  for (transno = 0;
4187
0
       (transno < state->nbTrans) && (nb < maxval);
4188
0
       transno++) {
4189
0
      trans = &state->trans[transno];
4190
0
      if (trans->to < 0)
4191
0
    continue;
4192
0
      atom = trans->atom;
4193
0
      if ((atom == NULL) || (atom->valuep == NULL))
4194
0
    continue;
4195
0
      if (trans->count == REGEXP_ALL_LAX_COUNTER) {
4196
0
          continue;
4197
0
      } else if (trans->count == REGEXP_ALL_COUNTER) {
4198
0
          continue;
4199
0
      } else if (trans->counter >= 0) {
4200
0
          continue;
4201
0
      } else {
4202
0
                if ((exec->comp->states[trans->to] != NULL) &&
4203
0
        (exec->comp->states[trans->to]->type ==
4204
0
         XML_REGEXP_SINK_STATE)) {
4205
0
        if (atom->neg)
4206
0
      values[nb++] = (xmlChar *) atom->valuep2;
4207
0
        else
4208
0
      values[nb++] = (xmlChar *) atom->valuep;
4209
0
        (*nbneg)++;
4210
0
    }
4211
0
      }
4212
0
  }
4213
0
    }
4214
0
    return(0);
4215
0
}
4216
4217
/**
4218
 * xmlRegExecNextValues:
4219
 * @exec: a regexp execution context
4220
 * @nbval: pointer to the number of accepted values IN/OUT
4221
 * @nbneg: return number of negative transitions
4222
 * @values: pointer to the array of acceptable values
4223
 * @terminal: return value if this was a terminal state
4224
 *
4225
 * Extract information from the regexp execution,
4226
 * the parameter @values must point to an array of @nbval string pointers
4227
 * on return nbval will contain the number of possible strings in that
4228
 * state and the @values array will be updated with them. The string values
4229
 * returned will be freed with the @exec context and don't need to be
4230
 * deallocated.
4231
 *
4232
 * Returns: 0 in case of success or -1 in case of error.
4233
 */
4234
int
4235
xmlRegExecNextValues(xmlRegExecCtxtPtr exec, int *nbval, int *nbneg,
4236
0
                     xmlChar **values, int *terminal) {
4237
0
    return(xmlRegExecGetValues(exec, 0, nbval, nbneg, values, terminal));
4238
0
}
4239
4240
/**
4241
 * xmlRegExecErrInfo:
4242
 * @exec: a regexp execution context generating an error
4243
 * @string: return value for the error string
4244
 * @nbval: pointer to the number of accepted values IN/OUT
4245
 * @nbneg: return number of negative transitions
4246
 * @values: pointer to the array of acceptable values
4247
 * @terminal: return value if this was a terminal state
4248
 *
4249
 * Extract error information from the regexp execution, the parameter
4250
 * @string will be updated with the value pushed and not accepted,
4251
 * the parameter @values must point to an array of @nbval string pointers
4252
 * on return nbval will contain the number of possible strings in that
4253
 * state and the @values array will be updated with them. The string values
4254
 * returned will be freed with the @exec context and don't need to be
4255
 * deallocated.
4256
 *
4257
 * Returns: 0 in case of success or -1 in case of error.
4258
 */
4259
int
4260
xmlRegExecErrInfo(xmlRegExecCtxtPtr exec, const xmlChar **string,
4261
0
                  int *nbval, int *nbneg, xmlChar **values, int *terminal) {
4262
0
    if (exec == NULL)
4263
0
        return(-1);
4264
0
    if (string != NULL) {
4265
0
        if (exec->status != XML_REGEXP_OK)
4266
0
      *string = exec->errString;
4267
0
  else
4268
0
      *string = NULL;
4269
0
    }
4270
0
    return(xmlRegExecGetValues(exec, 1, nbval, nbneg, values, terminal));
4271
0
}
4272
4273
#if 0
4274
static int
4275
xmlRegExecPushChar(xmlRegExecCtxtPtr exec, int UCS) {
4276
    xmlRegTransPtr trans;
4277
    xmlRegAtomPtr atom;
4278
    int ret;
4279
    int codepoint, len;
4280
4281
    if (exec == NULL)
4282
  return(-1);
4283
    if (exec->status != XML_REGEXP_OK)
4284
  return(exec->status);
4285
4286
    while ((exec->status == XML_REGEXP_OK) &&
4287
     ((exec->inputString[exec->index] != 0) ||
4288
      (exec->state->type != XML_REGEXP_FINAL_STATE))) {
4289
4290
  /*
4291
   * End of input on non-terminal state, rollback, however we may
4292
   * still have epsilon like transition for counted transitions
4293
   * on counters, in that case don't break too early.
4294
   */
4295
  if ((exec->inputString[exec->index] == 0) && (exec->counts == NULL))
4296
      goto rollback;
4297
4298
  exec->transcount = 0;
4299
  for (;exec->transno < exec->state->nbTrans;exec->transno++) {
4300
      trans = &exec->state->trans[exec->transno];
4301
      if (trans->to < 0)
4302
    continue;
4303
      atom = trans->atom;
4304
      ret = 0;
4305
      if (trans->count >= 0) {
4306
    int count;
4307
    xmlRegCounterPtr counter;
4308
4309
    /*
4310
     * A counted transition.
4311
     */
4312
4313
    count = exec->counts[trans->count];
4314
    counter = &exec->comp->counters[trans->count];
4315
    ret = ((count >= counter->min) && (count <= counter->max));
4316
      } else if (atom == NULL) {
4317
    fprintf(stderr, "epsilon transition left at runtime\n");
4318
    exec->status = XML_REGEXP_INTERNAL_ERROR;
4319
    break;
4320
      } else if (exec->inputString[exec->index] != 0) {
4321
                codepoint = CUR_SCHAR(&(exec->inputString[exec->index]), len);
4322
    ret = xmlRegCheckCharacter(atom, codepoint);
4323
    if ((ret == 1) && (atom->min > 0) && (atom->max > 0)) {
4324
        xmlRegStatePtr to = exec->comp->states[trans->to];
4325
4326
        /*
4327
         * this is a multiple input sequence
4328
         */
4329
        if (exec->state->nbTrans > exec->transno + 1) {
4330
      xmlFARegExecSave(exec);
4331
        }
4332
        exec->transcount = 1;
4333
        do {
4334
      /*
4335
       * Try to progress as much as possible on the input
4336
       */
4337
      if (exec->transcount == atom->max) {
4338
          break;
4339
      }
4340
      exec->index += len;
4341
      /*
4342
       * End of input: stop here
4343
       */
4344
      if (exec->inputString[exec->index] == 0) {
4345
          exec->index -= len;
4346
          break;
4347
      }
4348
      if (exec->transcount >= atom->min) {
4349
          int transno = exec->transno;
4350
          xmlRegStatePtr state = exec->state;
4351
4352
          /*
4353
           * The transition is acceptable save it
4354
           */
4355
          exec->transno = -1; /* trick */
4356
          exec->state = to;
4357
          xmlFARegExecSave(exec);
4358
          exec->transno = transno;
4359
          exec->state = state;
4360
      }
4361
      codepoint = CUR_SCHAR(&(exec->inputString[exec->index]),
4362
                      len);
4363
      ret = xmlRegCheckCharacter(atom, codepoint);
4364
      exec->transcount++;
4365
        } while (ret == 1);
4366
        if (exec->transcount < atom->min)
4367
      ret = 0;
4368
4369
        /*
4370
         * If the last check failed but one transition was found
4371
         * possible, rollback
4372
         */
4373
        if (ret < 0)
4374
      ret = 0;
4375
        if (ret == 0) {
4376
      goto rollback;
4377
        }
4378
    }
4379
      }
4380
      if (ret == 1) {
4381
    if (exec->state->nbTrans > exec->transno + 1) {
4382
        xmlFARegExecSave(exec);
4383
    }
4384
    /*
4385
     * restart count for expressions like this ((abc){2})*
4386
     */
4387
    if (trans->count >= 0) {
4388
        exec->counts[trans->count] = 0;
4389
    }
4390
    if (trans->counter >= 0) {
4391
        exec->counts[trans->counter]++;
4392
    }
4393
    exec->state = exec->comp->states[trans->to];
4394
    exec->transno = 0;
4395
    if (trans->atom != NULL) {
4396
        exec->index += len;
4397
    }
4398
    goto progress;
4399
      } else if (ret < 0) {
4400
    exec->status = XML_REGEXP_INTERNAL_ERROR;
4401
    break;
4402
      }
4403
  }
4404
  if ((exec->transno != 0) || (exec->state->nbTrans == 0)) {
4405
rollback:
4406
      /*
4407
       * Failed to find a way out
4408
       */
4409
      exec->determinist = 0;
4410
      xmlFARegExecRollBack(exec);
4411
  }
4412
progress:
4413
  continue;
4414
    }
4415
}
4416
#endif
4417
/************************************************************************
4418
 *                  *
4419
 *  Parser for the Schemas Datatype Regular Expressions   *
4420
 *  http://www.w3.org/TR/2001/REC-xmlschema-2-20010502/#regexs  *
4421
 *                  *
4422
 ************************************************************************/
4423
4424
/**
4425
 * xmlFAIsChar:
4426
 * @ctxt:  a regexp parser context
4427
 *
4428
 * [10]   Char   ::=   [^.\?*+()|#x5B#x5D]
4429
 */
4430
static int
4431
0
xmlFAIsChar(xmlRegParserCtxtPtr ctxt) {
4432
0
    int cur;
4433
0
    int len;
4434
4435
0
    len = 4;
4436
0
    cur = xmlGetUTF8Char(ctxt->cur, &len);
4437
0
    if (cur < 0) {
4438
0
        ERROR("Invalid UTF-8");
4439
0
        return(0);
4440
0
    }
4441
0
    if ((cur == '.') || (cur == '\\') || (cur == '?') ||
4442
0
  (cur == '*') || (cur == '+') || (cur == '(') ||
4443
0
  (cur == ')') || (cur == '|') || (cur == 0x5B) ||
4444
0
  (cur == 0x5D) || (cur == 0))
4445
0
  return(-1);
4446
0
    return(cur);
4447
0
}
4448
4449
/**
4450
 * xmlFAParseCharProp:
4451
 * @ctxt:  a regexp parser context
4452
 *
4453
 * [27]   charProp   ::=   IsCategory | IsBlock
4454
 * [28]   IsCategory ::= Letters | Marks | Numbers | Punctuation |
4455
 *                       Separators | Symbols | Others
4456
 * [29]   Letters   ::=   'L' [ultmo]?
4457
 * [30]   Marks   ::=   'M' [nce]?
4458
 * [31]   Numbers   ::=   'N' [dlo]?
4459
 * [32]   Punctuation   ::=   'P' [cdseifo]?
4460
 * [33]   Separators   ::=   'Z' [slp]?
4461
 * [34]   Symbols   ::=   'S' [mcko]?
4462
 * [35]   Others   ::=   'C' [cfon]?
4463
 * [36]   IsBlock   ::=   'Is' [a-zA-Z0-9#x2D]+
4464
 */
4465
static void
4466
0
xmlFAParseCharProp(xmlRegParserCtxtPtr ctxt) {
4467
0
    int cur;
4468
0
    xmlRegAtomType type = (xmlRegAtomType) 0;
4469
0
    xmlChar *blockName = NULL;
4470
4471
0
    cur = CUR;
4472
0
    if (cur == 'L') {
4473
0
  NEXT;
4474
0
  cur = CUR;
4475
0
  if (cur == 'u') {
4476
0
      NEXT;
4477
0
      type = XML_REGEXP_LETTER_UPPERCASE;
4478
0
  } else if (cur == 'l') {
4479
0
      NEXT;
4480
0
      type = XML_REGEXP_LETTER_LOWERCASE;
4481
0
  } else if (cur == 't') {
4482
0
      NEXT;
4483
0
      type = XML_REGEXP_LETTER_TITLECASE;
4484
0
  } else if (cur == 'm') {
4485
0
      NEXT;
4486
0
      type = XML_REGEXP_LETTER_MODIFIER;
4487
0
  } else if (cur == 'o') {
4488
0
      NEXT;
4489
0
      type = XML_REGEXP_LETTER_OTHERS;
4490
0
  } else {
4491
0
      type = XML_REGEXP_LETTER;
4492
0
  }
4493
0
    } else if (cur == 'M') {
4494
0
  NEXT;
4495
0
  cur = CUR;
4496
0
  if (cur == 'n') {
4497
0
      NEXT;
4498
      /* nonspacing */
4499
0
      type = XML_REGEXP_MARK_NONSPACING;
4500
0
  } else if (cur == 'c') {
4501
0
      NEXT;
4502
      /* spacing combining */
4503
0
      type = XML_REGEXP_MARK_SPACECOMBINING;
4504
0
  } else if (cur == 'e') {
4505
0
      NEXT;
4506
      /* enclosing */
4507
0
      type = XML_REGEXP_MARK_ENCLOSING;
4508
0
  } else {
4509
      /* all marks */
4510
0
      type = XML_REGEXP_MARK;
4511
0
  }
4512
0
    } else if (cur == 'N') {
4513
0
  NEXT;
4514
0
  cur = CUR;
4515
0
  if (cur == 'd') {
4516
0
      NEXT;
4517
      /* digital */
4518
0
      type = XML_REGEXP_NUMBER_DECIMAL;
4519
0
  } else if (cur == 'l') {
4520
0
      NEXT;
4521
      /* letter */
4522
0
      type = XML_REGEXP_NUMBER_LETTER;
4523
0
  } else if (cur == 'o') {
4524
0
      NEXT;
4525
      /* other */
4526
0
      type = XML_REGEXP_NUMBER_OTHERS;
4527
0
  } else {
4528
      /* all numbers */
4529
0
      type = XML_REGEXP_NUMBER;
4530
0
  }
4531
0
    } else if (cur == 'P') {
4532
0
  NEXT;
4533
0
  cur = CUR;
4534
0
  if (cur == 'c') {
4535
0
      NEXT;
4536
      /* connector */
4537
0
      type = XML_REGEXP_PUNCT_CONNECTOR;
4538
0
  } else if (cur == 'd') {
4539
0
      NEXT;
4540
      /* dash */
4541
0
      type = XML_REGEXP_PUNCT_DASH;
4542
0
  } else if (cur == 's') {
4543
0
      NEXT;
4544
      /* open */
4545
0
      type = XML_REGEXP_PUNCT_OPEN;
4546
0
  } else if (cur == 'e') {
4547
0
      NEXT;
4548
      /* close */
4549
0
      type = XML_REGEXP_PUNCT_CLOSE;
4550
0
  } else if (cur == 'i') {
4551
0
      NEXT;
4552
      /* initial quote */
4553
0
      type = XML_REGEXP_PUNCT_INITQUOTE;
4554
0
  } else if (cur == 'f') {
4555
0
      NEXT;
4556
      /* final quote */
4557
0
      type = XML_REGEXP_PUNCT_FINQUOTE;
4558
0
  } else if (cur == 'o') {
4559
0
      NEXT;
4560
      /* other */
4561
0
      type = XML_REGEXP_PUNCT_OTHERS;
4562
0
  } else {
4563
      /* all punctuation */
4564
0
      type = XML_REGEXP_PUNCT;
4565
0
  }
4566
0
    } else if (cur == 'Z') {
4567
0
  NEXT;
4568
0
  cur = CUR;
4569
0
  if (cur == 's') {
4570
0
      NEXT;
4571
      /* space */
4572
0
      type = XML_REGEXP_SEPAR_SPACE;
4573
0
  } else if (cur == 'l') {
4574
0
      NEXT;
4575
      /* line */
4576
0
      type = XML_REGEXP_SEPAR_LINE;
4577
0
  } else if (cur == 'p') {
4578
0
      NEXT;
4579
      /* paragraph */
4580
0
      type = XML_REGEXP_SEPAR_PARA;
4581
0
  } else {
4582
      /* all separators */
4583
0
      type = XML_REGEXP_SEPAR;
4584
0
  }
4585
0
    } else if (cur == 'S') {
4586
0
  NEXT;
4587
0
  cur = CUR;
4588
0
  if (cur == 'm') {
4589
0
      NEXT;
4590
0
      type = XML_REGEXP_SYMBOL_MATH;
4591
      /* math */
4592
0
  } else if (cur == 'c') {
4593
0
      NEXT;
4594
0
      type = XML_REGEXP_SYMBOL_CURRENCY;
4595
      /* currency */
4596
0
  } else if (cur == 'k') {
4597
0
      NEXT;
4598
0
      type = XML_REGEXP_SYMBOL_MODIFIER;
4599
      /* modifiers */
4600
0
  } else if (cur == 'o') {
4601
0
      NEXT;
4602
0
      type = XML_REGEXP_SYMBOL_OTHERS;
4603
      /* other */
4604
0
  } else {
4605
      /* all symbols */
4606
0
      type = XML_REGEXP_SYMBOL;
4607
0
  }
4608
0
    } else if (cur == 'C') {
4609
0
  NEXT;
4610
0
  cur = CUR;
4611
0
  if (cur == 'c') {
4612
0
      NEXT;
4613
      /* control */
4614
0
      type = XML_REGEXP_OTHER_CONTROL;
4615
0
  } else if (cur == 'f') {
4616
0
      NEXT;
4617
      /* format */
4618
0
      type = XML_REGEXP_OTHER_FORMAT;
4619
0
  } else if (cur == 'o') {
4620
0
      NEXT;
4621
      /* private use */
4622
0
      type = XML_REGEXP_OTHER_PRIVATE;
4623
0
  } else if (cur == 'n') {
4624
0
      NEXT;
4625
      /* not assigned */
4626
0
      type = XML_REGEXP_OTHER_NA;
4627
0
  } else {
4628
      /* all others */
4629
0
      type = XML_REGEXP_OTHER;
4630
0
  }
4631
0
    } else if (cur == 'I') {
4632
0
  const xmlChar *start;
4633
0
  NEXT;
4634
0
  cur = CUR;
4635
0
  if (cur != 's') {
4636
0
      ERROR("IsXXXX expected");
4637
0
      return;
4638
0
  }
4639
0
  NEXT;
4640
0
  start = ctxt->cur;
4641
0
  cur = CUR;
4642
0
  if (((cur >= 'a') && (cur <= 'z')) ||
4643
0
      ((cur >= 'A') && (cur <= 'Z')) ||
4644
0
      ((cur >= '0') && (cur <= '9')) ||
4645
0
      (cur == 0x2D)) {
4646
0
      NEXT;
4647
0
      cur = CUR;
4648
0
      while (((cur >= 'a') && (cur <= 'z')) ||
4649
0
    ((cur >= 'A') && (cur <= 'Z')) ||
4650
0
    ((cur >= '0') && (cur <= '9')) ||
4651
0
    (cur == 0x2D)) {
4652
0
    NEXT;
4653
0
    cur = CUR;
4654
0
      }
4655
0
  }
4656
0
  type = XML_REGEXP_BLOCK_NAME;
4657
0
  blockName = xmlStrndup(start, ctxt->cur - start);
4658
0
        if (blockName == NULL)
4659
0
      xmlRegexpErrMemory(ctxt);
4660
0
    } else {
4661
0
  ERROR("Unknown char property");
4662
0
  return;
4663
0
    }
4664
0
    if (ctxt->atom == NULL) {
4665
0
  ctxt->atom = xmlRegNewAtom(ctxt, type);
4666
0
        if (ctxt->atom == NULL) {
4667
0
            xmlFree(blockName);
4668
0
            return;
4669
0
        }
4670
0
  ctxt->atom->valuep = blockName;
4671
0
    } else if (ctxt->atom->type == XML_REGEXP_RANGES) {
4672
0
        if (xmlRegAtomAddRange(ctxt, ctxt->atom, ctxt->neg,
4673
0
                               type, 0, 0, blockName) == NULL) {
4674
0
            xmlFree(blockName);
4675
0
        }
4676
0
    }
4677
0
}
4678
4679
static int parse_escaped_codeunit(xmlRegParserCtxtPtr ctxt)
4680
0
{
4681
0
    int val = 0, i, cur;
4682
0
    for (i = 0; i < 4; i++) {
4683
0
  NEXT;
4684
0
  val *= 16;
4685
0
  cur = CUR;
4686
0
  if (cur >= '0' && cur <= '9') {
4687
0
      val += cur - '0';
4688
0
  } else if (cur >= 'A' && cur <= 'F') {
4689
0
      val += cur - 'A' + 10;
4690
0
  } else if (cur >= 'a' && cur <= 'f') {
4691
0
      val += cur - 'a' + 10;
4692
0
  } else {
4693
0
      ERROR("Expecting hex digit");
4694
0
      return -1;
4695
0
  }
4696
0
    }
4697
0
    return val;
4698
0
}
4699
4700
static int parse_escaped_codepoint(xmlRegParserCtxtPtr ctxt)
4701
0
{
4702
0
    int val = parse_escaped_codeunit(ctxt);
4703
0
    if (0xD800 <= val && val <= 0xDBFF) {
4704
0
  NEXT;
4705
0
  if (CUR == '\\') {
4706
0
      NEXT;
4707
0
      if (CUR == 'u') {
4708
0
    int low = parse_escaped_codeunit(ctxt);
4709
0
    if (0xDC00 <= low && low <= 0xDFFF) {
4710
0
        return (val - 0xD800) * 0x400 + (low - 0xDC00) + 0x10000;
4711
0
    }
4712
0
      }
4713
0
  }
4714
0
  ERROR("Invalid low surrogate pair code unit");
4715
0
  val = -1;
4716
0
    }
4717
0
    return val;
4718
0
}
4719
4720
/**
4721
 * xmlFAParseCharClassEsc:
4722
 * @ctxt:  a regexp parser context
4723
 *
4724
 * [23] charClassEsc ::= ( SingleCharEsc | MultiCharEsc | catEsc | complEsc )
4725
 * [24] SingleCharEsc ::= '\' [nrt\|.?*+(){}#x2D#x5B#x5D#x5E]
4726
 * [25] catEsc   ::=   '\p{' charProp '}'
4727
 * [26] complEsc ::=   '\P{' charProp '}'
4728
 * [37] MultiCharEsc ::= '.' | ('\' [sSiIcCdDwW])
4729
 */
4730
static void
4731
0
xmlFAParseCharClassEsc(xmlRegParserCtxtPtr ctxt) {
4732
0
    int cur;
4733
4734
0
    if (CUR == '.') {
4735
0
  if (ctxt->atom == NULL) {
4736
0
      ctxt->atom = xmlRegNewAtom(ctxt, XML_REGEXP_ANYCHAR);
4737
0
  } else if (ctxt->atom->type == XML_REGEXP_RANGES) {
4738
0
      xmlRegAtomAddRange(ctxt, ctxt->atom, ctxt->neg,
4739
0
             XML_REGEXP_ANYCHAR, 0, 0, NULL);
4740
0
  }
4741
0
  NEXT;
4742
0
  return;
4743
0
    }
4744
0
    if (CUR != '\\') {
4745
0
  ERROR("Escaped sequence: expecting \\");
4746
0
  return;
4747
0
    }
4748
0
    NEXT;
4749
0
    cur = CUR;
4750
0
    if (cur == 'p') {
4751
0
  NEXT;
4752
0
  if (CUR != '{') {
4753
0
      ERROR("Expecting '{'");
4754
0
      return;
4755
0
  }
4756
0
  NEXT;
4757
0
  xmlFAParseCharProp(ctxt);
4758
0
  if (CUR != '}') {
4759
0
      ERROR("Expecting '}'");
4760
0
      return;
4761
0
  }
4762
0
  NEXT;
4763
0
    } else if (cur == 'P') {
4764
0
  NEXT;
4765
0
  if (CUR != '{') {
4766
0
      ERROR("Expecting '{'");
4767
0
      return;
4768
0
  }
4769
0
  NEXT;
4770
0
  xmlFAParseCharProp(ctxt);
4771
0
        if (ctxt->atom != NULL)
4772
0
      ctxt->atom->neg = 1;
4773
0
  if (CUR != '}') {
4774
0
      ERROR("Expecting '}'");
4775
0
      return;
4776
0
  }
4777
0
  NEXT;
4778
0
    } else if ((cur == 'n') || (cur == 'r') || (cur == 't') || (cur == '\\') ||
4779
0
  (cur == '|') || (cur == '.') || (cur == '?') || (cur == '*') ||
4780
0
  (cur == '+') || (cur == '(') || (cur == ')') || (cur == '{') ||
4781
0
  (cur == '}') || (cur == 0x2D) || (cur == 0x5B) || (cur == 0x5D) ||
4782
0
  (cur == 0x5E) ||
4783
4784
  /* Non-standard escape sequences:
4785
   *                  Java 1.8|.NET Core 3.1|MSXML 6 */
4786
0
  (cur == '!') ||     /*   +  |     +       |    +   */
4787
0
  (cur == '"') ||     /*   +  |     +       |    +   */
4788
0
  (cur == '#') ||     /*   +  |     +       |    +   */
4789
0
  (cur == '$') ||     /*   +  |     +       |    +   */
4790
0
  (cur == '%') ||     /*   +  |     +       |    +   */
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 == 'u')) {     /*      |     +       |    +   */
4801
0
  if (ctxt->atom == NULL) {
4802
0
      ctxt->atom = xmlRegNewAtom(ctxt, XML_REGEXP_CHARVAL);
4803
0
      if (ctxt->atom != NULL) {
4804
0
          switch (cur) {
4805
0
        case 'n':
4806
0
            ctxt->atom->codepoint = '\n';
4807
0
      break;
4808
0
        case 'r':
4809
0
            ctxt->atom->codepoint = '\r';
4810
0
      break;
4811
0
        case 't':
4812
0
            ctxt->atom->codepoint = '\t';
4813
0
      break;
4814
0
        case 'u':
4815
0
      cur = parse_escaped_codepoint(ctxt);
4816
0
      if (cur < 0) {
4817
0
          return;
4818
0
      }
4819
0
      ctxt->atom->codepoint = cur;
4820
0
      break;
4821
0
        default:
4822
0
      ctxt->atom->codepoint = cur;
4823
0
    }
4824
0
      }
4825
0
  } else if (ctxt->atom->type == XML_REGEXP_RANGES) {
4826
0
            switch (cur) {
4827
0
                case 'n':
4828
0
                    cur = '\n';
4829
0
                    break;
4830
0
                case 'r':
4831
0
                    cur = '\r';
4832
0
                    break;
4833
0
                case 't':
4834
0
                    cur = '\t';
4835
0
                    break;
4836
0
            }
4837
0
      xmlRegAtomAddRange(ctxt, ctxt->atom, ctxt->neg,
4838
0
             XML_REGEXP_CHARVAL, cur, cur, NULL);
4839
0
  }
4840
0
  NEXT;
4841
0
    } else if ((cur == 's') || (cur == 'S') || (cur == 'i') || (cur == 'I') ||
4842
0
  (cur == 'c') || (cur == 'C') || (cur == 'd') || (cur == 'D') ||
4843
0
  (cur == 'w') || (cur == 'W')) {
4844
0
  xmlRegAtomType type = XML_REGEXP_ANYSPACE;
4845
4846
0
  switch (cur) {
4847
0
      case 's':
4848
0
    type = XML_REGEXP_ANYSPACE;
4849
0
    break;
4850
0
      case 'S':
4851
0
    type = XML_REGEXP_NOTSPACE;
4852
0
    break;
4853
0
      case 'i':
4854
0
    type = XML_REGEXP_INITNAME;
4855
0
    break;
4856
0
      case 'I':
4857
0
    type = XML_REGEXP_NOTINITNAME;
4858
0
    break;
4859
0
      case 'c':
4860
0
    type = XML_REGEXP_NAMECHAR;
4861
0
    break;
4862
0
      case 'C':
4863
0
    type = XML_REGEXP_NOTNAMECHAR;
4864
0
    break;
4865
0
      case 'd':
4866
0
    type = XML_REGEXP_DECIMAL;
4867
0
    break;
4868
0
      case 'D':
4869
0
    type = XML_REGEXP_NOTDECIMAL;
4870
0
    break;
4871
0
      case 'w':
4872
0
    type = XML_REGEXP_REALCHAR;
4873
0
    break;
4874
0
      case 'W':
4875
0
    type = XML_REGEXP_NOTREALCHAR;
4876
0
    break;
4877
0
  }
4878
0
  NEXT;
4879
0
  if (ctxt->atom == NULL) {
4880
0
      ctxt->atom = xmlRegNewAtom(ctxt, type);
4881
0
  } else if (ctxt->atom->type == XML_REGEXP_RANGES) {
4882
0
      xmlRegAtomAddRange(ctxt, ctxt->atom, ctxt->neg,
4883
0
             type, 0, 0, NULL);
4884
0
  }
4885
0
    } else {
4886
0
  ERROR("Wrong escape sequence, misuse of character '\\'");
4887
0
    }
4888
0
}
4889
4890
/**
4891
 * xmlFAParseCharRange:
4892
 * @ctxt:  a regexp parser context
4893
 *
4894
 * [17]   charRange   ::=     seRange | XmlCharRef | XmlCharIncDash
4895
 * [18]   seRange   ::=   charOrEsc '-' charOrEsc
4896
 * [20]   charOrEsc   ::=   XmlChar | SingleCharEsc
4897
 * [21]   XmlChar   ::=   [^\#x2D#x5B#x5D]
4898
 * [22]   XmlCharIncDash   ::=   [^\#x5B#x5D]
4899
 */
4900
static void
4901
0
xmlFAParseCharRange(xmlRegParserCtxtPtr ctxt) {
4902
0
    int cur, len;
4903
0
    int start = -1;
4904
0
    int end = -1;
4905
4906
0
    if (CUR == '\0') {
4907
0
        ERROR("Expecting ']'");
4908
0
  return;
4909
0
    }
4910
4911
0
    cur = CUR;
4912
0
    if (cur == '\\') {
4913
0
  NEXT;
4914
0
  cur = CUR;
4915
0
  switch (cur) {
4916
0
      case 'n': start = 0xA; break;
4917
0
      case 'r': start = 0xD; break;
4918
0
      case 't': start = 0x9; break;
4919
0
      case '\\': case '|': case '.': case '-': case '^': case '?':
4920
0
      case '*': case '+': case '{': case '}': case '(': case ')':
4921
0
      case '[': case ']':
4922
0
    start = cur; break;
4923
0
      default:
4924
0
    ERROR("Invalid escape value");
4925
0
    return;
4926
0
  }
4927
0
  end = start;
4928
0
        len = 1;
4929
0
    } else if ((cur != 0x5B) && (cur != 0x5D)) {
4930
0
        len = 4;
4931
0
        end = start = xmlGetUTF8Char(ctxt->cur, &len);
4932
0
        if (start < 0) {
4933
0
            ERROR("Invalid UTF-8");
4934
0
            return;
4935
0
        }
4936
0
    } else {
4937
0
  ERROR("Expecting a char range");
4938
0
  return;
4939
0
    }
4940
    /*
4941
     * Since we are "inside" a range, we can assume ctxt->cur is past
4942
     * the start of ctxt->string, and PREV should be safe
4943
     */
4944
0
    if ((start == '-') && (NXT(1) != ']') && (PREV != '[') && (PREV != '^')) {
4945
0
  NEXTL(len);
4946
0
  return;
4947
0
    }
4948
0
    NEXTL(len);
4949
0
    cur = CUR;
4950
0
    if ((cur != '-') || (NXT(1) == '[') || (NXT(1) == ']')) {
4951
0
        xmlRegAtomAddRange(ctxt, ctxt->atom, ctxt->neg,
4952
0
                  XML_REGEXP_CHARVAL, start, end, NULL);
4953
0
  return;
4954
0
    }
4955
0
    NEXT;
4956
0
    cur = CUR;
4957
0
    if (cur == '\\') {
4958
0
  NEXT;
4959
0
  cur = CUR;
4960
0
  switch (cur) {
4961
0
      case 'n': end = 0xA; break;
4962
0
      case 'r': end = 0xD; break;
4963
0
      case 't': end = 0x9; break;
4964
0
      case '\\': case '|': case '.': case '-': case '^': case '?':
4965
0
      case '*': case '+': case '{': case '}': case '(': case ')':
4966
0
      case '[': case ']':
4967
0
    end = cur; break;
4968
0
      default:
4969
0
    ERROR("Invalid escape value");
4970
0
    return;
4971
0
  }
4972
0
        len = 1;
4973
0
    } else if ((cur != '\0') && (cur != 0x5B) && (cur != 0x5D)) {
4974
0
        len = 4;
4975
0
        end = xmlGetUTF8Char(ctxt->cur, &len);
4976
0
        if (end < 0) {
4977
0
            ERROR("Invalid UTF-8");
4978
0
            return;
4979
0
        }
4980
0
    } else {
4981
0
  ERROR("Expecting the end of a char range");
4982
0
  return;
4983
0
    }
4984
4985
    /* TODO check that the values are acceptable character ranges for XML */
4986
0
    if (end < start) {
4987
0
  ERROR("End of range is before start of range");
4988
0
    } else {
4989
0
        NEXTL(len);
4990
0
        xmlRegAtomAddRange(ctxt, ctxt->atom, ctxt->neg,
4991
0
               XML_REGEXP_CHARVAL, start, end, NULL);
4992
0
    }
4993
0
    return;
4994
0
}
4995
4996
/**
4997
 * xmlFAParsePosCharGroup:
4998
 * @ctxt:  a regexp parser context
4999
 *
5000
 * [14]   posCharGroup ::= ( charRange | charClassEsc  )+
5001
 */
5002
static void
5003
0
xmlFAParsePosCharGroup(xmlRegParserCtxtPtr ctxt) {
5004
0
    do {
5005
0
  if (CUR == '\\') {
5006
0
      xmlFAParseCharClassEsc(ctxt);
5007
0
  } else {
5008
0
      xmlFAParseCharRange(ctxt);
5009
0
  }
5010
0
    } while ((CUR != ']') && (CUR != '-') &&
5011
0
             (CUR != 0) && (ctxt->error == 0));
5012
0
}
5013
5014
/**
5015
 * xmlFAParseCharGroup:
5016
 * @ctxt:  a regexp parser context
5017
 *
5018
 * [13]   charGroup    ::= posCharGroup | negCharGroup | charClassSub
5019
 * [15]   negCharGroup ::= '^' posCharGroup
5020
 * [16]   charClassSub ::= ( posCharGroup | negCharGroup ) '-' charClassExpr
5021
 * [12]   charClassExpr ::= '[' charGroup ']'
5022
 */
5023
static void
5024
0
xmlFAParseCharGroup(xmlRegParserCtxtPtr ctxt) {
5025
0
    int neg = ctxt->neg;
5026
5027
0
    if (CUR == '^') {
5028
0
  NEXT;
5029
0
  ctxt->neg = !ctxt->neg;
5030
0
  xmlFAParsePosCharGroup(ctxt);
5031
0
  ctxt->neg = neg;
5032
0
    }
5033
0
    while ((CUR != ']') && (ctxt->error == 0)) {
5034
0
  if ((CUR == '-') && (NXT(1) == '[')) {
5035
0
      NEXT; /* eat the '-' */
5036
0
      NEXT; /* eat the '[' */
5037
0
      ctxt->neg = 2;
5038
0
      xmlFAParseCharGroup(ctxt);
5039
0
      ctxt->neg = neg;
5040
0
      if (CUR == ']') {
5041
0
    NEXT;
5042
0
      } else {
5043
0
    ERROR("charClassExpr: ']' expected");
5044
0
      }
5045
0
      break;
5046
0
  } else {
5047
0
      xmlFAParsePosCharGroup(ctxt);
5048
0
  }
5049
0
    }
5050
0
}
5051
5052
/**
5053
 * xmlFAParseCharClass:
5054
 * @ctxt:  a regexp parser context
5055
 *
5056
 * [11]   charClass   ::=     charClassEsc | charClassExpr
5057
 * [12]   charClassExpr   ::=   '[' charGroup ']'
5058
 */
5059
static void
5060
0
xmlFAParseCharClass(xmlRegParserCtxtPtr ctxt) {
5061
0
    if (CUR == '[') {
5062
0
  NEXT;
5063
0
  ctxt->atom = xmlRegNewAtom(ctxt, XML_REGEXP_RANGES);
5064
0
  if (ctxt->atom == NULL)
5065
0
      return;
5066
0
  xmlFAParseCharGroup(ctxt);
5067
0
  if (CUR == ']') {
5068
0
      NEXT;
5069
0
  } else {
5070
0
      ERROR("xmlFAParseCharClass: ']' expected");
5071
0
  }
5072
0
    } else {
5073
0
  xmlFAParseCharClassEsc(ctxt);
5074
0
    }
5075
0
}
5076
5077
/**
5078
 * xmlFAParseQuantExact:
5079
 * @ctxt:  a regexp parser context
5080
 *
5081
 * [8]   QuantExact   ::=   [0-9]+
5082
 *
5083
 * Returns 0 if success or -1 in case of error
5084
 */
5085
static int
5086
0
xmlFAParseQuantExact(xmlRegParserCtxtPtr ctxt) {
5087
0
    int ret = 0;
5088
0
    int ok = 0;
5089
0
    int overflow = 0;
5090
5091
0
    while ((CUR >= '0') && (CUR <= '9')) {
5092
0
        if (ret > INT_MAX / 10) {
5093
0
            overflow = 1;
5094
0
        } else {
5095
0
            int digit = CUR - '0';
5096
5097
0
            ret *= 10;
5098
0
            if (ret > INT_MAX - digit)
5099
0
                overflow = 1;
5100
0
            else
5101
0
                ret += digit;
5102
0
        }
5103
0
  ok = 1;
5104
0
  NEXT;
5105
0
    }
5106
0
    if ((ok != 1) || (overflow == 1)) {
5107
0
  return(-1);
5108
0
    }
5109
0
    return(ret);
5110
0
}
5111
5112
/**
5113
 * xmlFAParseQuantifier:
5114
 * @ctxt:  a regexp parser context
5115
 *
5116
 * [4]   quantifier   ::=   [?*+] | ( '{' quantity '}' )
5117
 * [5]   quantity   ::=   quantRange | quantMin | QuantExact
5118
 * [6]   quantRange   ::=   QuantExact ',' QuantExact
5119
 * [7]   quantMin   ::=   QuantExact ','
5120
 * [8]   QuantExact   ::=   [0-9]+
5121
 */
5122
static int
5123
0
xmlFAParseQuantifier(xmlRegParserCtxtPtr ctxt) {
5124
0
    int cur;
5125
5126
0
    cur = CUR;
5127
0
    if ((cur == '?') || (cur == '*') || (cur == '+')) {
5128
0
  if (ctxt->atom != NULL) {
5129
0
      if (cur == '?')
5130
0
    ctxt->atom->quant = XML_REGEXP_QUANT_OPT;
5131
0
      else if (cur == '*')
5132
0
    ctxt->atom->quant = XML_REGEXP_QUANT_MULT;
5133
0
      else if (cur == '+')
5134
0
    ctxt->atom->quant = XML_REGEXP_QUANT_PLUS;
5135
0
  }
5136
0
  NEXT;
5137
0
  return(1);
5138
0
    }
5139
0
    if (cur == '{') {
5140
0
  int min = 0, max = 0;
5141
5142
0
  NEXT;
5143
0
  cur = xmlFAParseQuantExact(ctxt);
5144
0
  if (cur >= 0)
5145
0
      min = cur;
5146
0
        else {
5147
0
            ERROR("Improper quantifier");
5148
0
        }
5149
0
  if (CUR == ',') {
5150
0
      NEXT;
5151
0
      if (CUR == '}')
5152
0
          max = INT_MAX;
5153
0
      else {
5154
0
          cur = xmlFAParseQuantExact(ctxt);
5155
0
          if (cur >= 0)
5156
0
        max = cur;
5157
0
    else {
5158
0
        ERROR("Improper quantifier");
5159
0
    }
5160
0
      }
5161
0
  }
5162
0
  if (CUR == '}') {
5163
0
      NEXT;
5164
0
  } else {
5165
0
      ERROR("Unterminated quantifier");
5166
0
  }
5167
0
  if (max == 0)
5168
0
      max = min;
5169
0
  if (ctxt->atom != NULL) {
5170
0
      ctxt->atom->quant = XML_REGEXP_QUANT_RANGE;
5171
0
      ctxt->atom->min = min;
5172
0
      ctxt->atom->max = max;
5173
0
  }
5174
0
  return(1);
5175
0
    }
5176
0
    return(0);
5177
0
}
5178
5179
/**
5180
 * xmlFAParseAtom:
5181
 * @ctxt:  a regexp parser context
5182
 *
5183
 * [9]   atom   ::=   Char | charClass | ( '(' regExp ')' )
5184
 */
5185
static int
5186
0
xmlFAParseAtom(xmlRegParserCtxtPtr ctxt) {
5187
0
    int codepoint, len;
5188
5189
0
    codepoint = xmlFAIsChar(ctxt);
5190
0
    if (codepoint > 0) {
5191
0
  ctxt->atom = xmlRegNewAtom(ctxt, XML_REGEXP_CHARVAL);
5192
0
  if (ctxt->atom == NULL)
5193
0
      return(-1);
5194
0
        len = 4;
5195
0
        codepoint = xmlGetUTF8Char(ctxt->cur, &len);
5196
0
        if (codepoint < 0) {
5197
0
            ERROR("Invalid UTF-8");
5198
0
            return(-1);
5199
0
        }
5200
0
  ctxt->atom->codepoint = codepoint;
5201
0
  NEXTL(len);
5202
0
  return(1);
5203
0
    } else if (CUR == '|') {
5204
0
  return(0);
5205
0
    } else if (CUR == 0) {
5206
0
  return(0);
5207
0
    } else if (CUR == ')') {
5208
0
  return(0);
5209
0
    } else if (CUR == '(') {
5210
0
  xmlRegStatePtr start, oldend, start0;
5211
5212
0
  NEXT;
5213
0
        if (ctxt->depth >= 50) {
5214
0
      ERROR("xmlFAParseAtom: maximum nesting depth exceeded");
5215
0
            return(-1);
5216
0
        }
5217
  /*
5218
   * this extra Epsilon transition is needed if we count with 0 allowed
5219
   * unfortunately this can't be known at that point
5220
   */
5221
0
  xmlFAGenerateEpsilonTransition(ctxt, ctxt->state, NULL);
5222
0
  start0 = ctxt->state;
5223
0
  xmlFAGenerateEpsilonTransition(ctxt, ctxt->state, NULL);
5224
0
  start = ctxt->state;
5225
0
  oldend = ctxt->end;
5226
0
  ctxt->end = NULL;
5227
0
  ctxt->atom = NULL;
5228
0
        ctxt->depth++;
5229
0
  xmlFAParseRegExp(ctxt, 0);
5230
0
        ctxt->depth--;
5231
0
  if (CUR == ')') {
5232
0
      NEXT;
5233
0
  } else {
5234
0
      ERROR("xmlFAParseAtom: expecting ')'");
5235
0
  }
5236
0
  ctxt->atom = xmlRegNewAtom(ctxt, XML_REGEXP_SUBREG);
5237
0
  if (ctxt->atom == NULL)
5238
0
      return(-1);
5239
0
  ctxt->atom->start = start;
5240
0
  ctxt->atom->start0 = start0;
5241
0
  ctxt->atom->stop = ctxt->state;
5242
0
  ctxt->end = oldend;
5243
0
  return(1);
5244
0
    } else if ((CUR == '[') || (CUR == '\\') || (CUR == '.')) {
5245
0
  xmlFAParseCharClass(ctxt);
5246
0
  return(1);
5247
0
    }
5248
0
    return(0);
5249
0
}
5250
5251
/**
5252
 * xmlFAParsePiece:
5253
 * @ctxt:  a regexp parser context
5254
 *
5255
 * [3]   piece   ::=   atom quantifier?
5256
 */
5257
static int
5258
0
xmlFAParsePiece(xmlRegParserCtxtPtr ctxt) {
5259
0
    int ret;
5260
5261
0
    ctxt->atom = NULL;
5262
0
    ret = xmlFAParseAtom(ctxt);
5263
0
    if (ret == 0)
5264
0
  return(0);
5265
0
    if (ctxt->atom == NULL) {
5266
0
  ERROR("internal: no atom generated");
5267
0
    }
5268
0
    xmlFAParseQuantifier(ctxt);
5269
0
    return(1);
5270
0
}
5271
5272
/**
5273
 * xmlFAParseBranch:
5274
 * @ctxt:  a regexp parser context
5275
 * @to: optional target to the end of the branch
5276
 *
5277
 * @to is used to optimize by removing duplicate path in automata
5278
 * in expressions like (a|b)(c|d)
5279
 *
5280
 * [2]   branch   ::=   piece*
5281
 */
5282
static int
5283
0
xmlFAParseBranch(xmlRegParserCtxtPtr ctxt, xmlRegStatePtr to) {
5284
0
    xmlRegStatePtr previous;
5285
0
    int ret;
5286
5287
0
    previous = ctxt->state;
5288
0
    ret = xmlFAParsePiece(ctxt);
5289
0
    if (ret == 0) {
5290
        /* Empty branch */
5291
0
  xmlFAGenerateEpsilonTransition(ctxt, previous, to);
5292
0
    } else {
5293
0
  if (xmlFAGenerateTransitions(ctxt, previous,
5294
0
          (CUR=='|' || CUR==')' || CUR==0) ? to : NULL,
5295
0
                ctxt->atom) < 0) {
5296
0
            xmlRegFreeAtom(ctxt->atom);
5297
0
            ctxt->atom = NULL;
5298
0
      return(-1);
5299
0
        }
5300
0
  previous = ctxt->state;
5301
0
  ctxt->atom = NULL;
5302
0
    }
5303
0
    while ((ret != 0) && (ctxt->error == 0)) {
5304
0
  ret = xmlFAParsePiece(ctxt);
5305
0
  if (ret != 0) {
5306
0
      if (xmlFAGenerateTransitions(ctxt, previous,
5307
0
              (CUR=='|' || CUR==')' || CUR==0) ? to : NULL,
5308
0
                    ctxt->atom) < 0) {
5309
0
                xmlRegFreeAtom(ctxt->atom);
5310
0
                ctxt->atom = NULL;
5311
0
                return(-1);
5312
0
            }
5313
0
      previous = ctxt->state;
5314
0
      ctxt->atom = NULL;
5315
0
  }
5316
0
    }
5317
0
    return(0);
5318
0
}
5319
5320
/**
5321
 * xmlFAParseRegExp:
5322
 * @ctxt:  a regexp parser context
5323
 * @top:  is this the top-level expression ?
5324
 *
5325
 * [1]   regExp   ::=     branch  ( '|' branch )*
5326
 */
5327
static void
5328
0
xmlFAParseRegExp(xmlRegParserCtxtPtr ctxt, int top) {
5329
0
    xmlRegStatePtr start, end;
5330
5331
    /* if not top start should have been generated by an epsilon trans */
5332
0
    start = ctxt->state;
5333
0
    ctxt->end = NULL;
5334
0
    xmlFAParseBranch(ctxt, NULL);
5335
0
    if (top) {
5336
0
  ctxt->state->type = XML_REGEXP_FINAL_STATE;
5337
0
    }
5338
0
    if (CUR != '|') {
5339
0
  ctxt->end = ctxt->state;
5340
0
  return;
5341
0
    }
5342
0
    end = ctxt->state;
5343
0
    while ((CUR == '|') && (ctxt->error == 0)) {
5344
0
  NEXT;
5345
0
  ctxt->state = start;
5346
0
  ctxt->end = NULL;
5347
0
  xmlFAParseBranch(ctxt, end);
5348
0
    }
5349
0
    if (!top) {
5350
0
  ctxt->state = end;
5351
0
  ctxt->end = end;
5352
0
    }
5353
0
}
5354
5355
/************************************************************************
5356
 *                  *
5357
 *      The basic API         *
5358
 *                  *
5359
 ************************************************************************/
5360
5361
/**
5362
 * xmlRegexpPrint:
5363
 * @output: the file for the output debug
5364
 * @regexp: the compiled regexp
5365
 *
5366
 * Print the content of the compiled regular expression
5367
 */
5368
void
5369
0
xmlRegexpPrint(FILE *output, xmlRegexpPtr regexp) {
5370
0
    int i;
5371
5372
0
    if (output == NULL)
5373
0
        return;
5374
0
    fprintf(output, " regexp: ");
5375
0
    if (regexp == NULL) {
5376
0
  fprintf(output, "NULL\n");
5377
0
  return;
5378
0
    }
5379
0
    fprintf(output, "'%s' ", regexp->string);
5380
0
    fprintf(output, "\n");
5381
0
    fprintf(output, "%d atoms:\n", regexp->nbAtoms);
5382
0
    for (i = 0;i < regexp->nbAtoms; i++) {
5383
0
  fprintf(output, " %02d ", i);
5384
0
  xmlRegPrintAtom(output, regexp->atoms[i]);
5385
0
    }
5386
0
    fprintf(output, "%d states:", regexp->nbStates);
5387
0
    fprintf(output, "\n");
5388
0
    for (i = 0;i < regexp->nbStates; i++) {
5389
0
  xmlRegPrintState(output, regexp->states[i]);
5390
0
    }
5391
0
    fprintf(output, "%d counters:\n", regexp->nbCounters);
5392
0
    for (i = 0;i < regexp->nbCounters; i++) {
5393
0
  fprintf(output, " %d: min %d max %d\n", i, regexp->counters[i].min,
5394
0
                                    regexp->counters[i].max);
5395
0
    }
5396
0
}
5397
5398
/**
5399
 * xmlRegexpCompile:
5400
 * @regexp:  a regular expression string
5401
 *
5402
 * Parses a regular expression conforming to XML Schemas Part 2 Datatype
5403
 * Appendix F and builds an automata suitable for testing strings against
5404
 * that regular expression
5405
 *
5406
 * Returns the compiled expression or NULL in case of error
5407
 */
5408
xmlRegexpPtr
5409
0
xmlRegexpCompile(const xmlChar *regexp) {
5410
0
    xmlRegexpPtr ret = NULL;
5411
0
    xmlRegParserCtxtPtr ctxt;
5412
5413
0
    if (regexp == NULL)
5414
0
        return(NULL);
5415
5416
0
    ctxt = xmlRegNewParserCtxt(regexp);
5417
0
    if (ctxt == NULL)
5418
0
  return(NULL);
5419
5420
    /* initialize the parser */
5421
0
    ctxt->state = xmlRegStatePush(ctxt);
5422
0
    if (ctxt->state == NULL)
5423
0
        goto error;
5424
0
    ctxt->start = ctxt->state;
5425
0
    ctxt->end = NULL;
5426
5427
    /* parse the expression building an automata */
5428
0
    xmlFAParseRegExp(ctxt, 1);
5429
0
    if (CUR != 0) {
5430
0
  ERROR("xmlFAParseRegExp: extra characters");
5431
0
    }
5432
0
    if (ctxt->error != 0)
5433
0
        goto error;
5434
0
    ctxt->end = ctxt->state;
5435
0
    ctxt->start->type = XML_REGEXP_START_STATE;
5436
0
    ctxt->end->type = XML_REGEXP_FINAL_STATE;
5437
5438
    /* remove the Epsilon except for counted transitions */
5439
0
    xmlFAEliminateEpsilonTransitions(ctxt);
5440
5441
5442
0
    if (ctxt->error != 0)
5443
0
        goto error;
5444
0
    ret = xmlRegEpxFromParse(ctxt);
5445
5446
0
error:
5447
0
    xmlRegFreeParserCtxt(ctxt);
5448
0
    return(ret);
5449
0
}
5450
5451
/**
5452
 * xmlRegexpExec:
5453
 * @comp:  the compiled regular expression
5454
 * @content:  the value to check against the regular expression
5455
 *
5456
 * Check if the regular expression generates the value
5457
 *
5458
 * Returns 1 if it matches, 0 if not and a negative value in case of error
5459
 */
5460
int
5461
0
xmlRegexpExec(xmlRegexpPtr comp, const xmlChar *content) {
5462
0
    if ((comp == NULL) || (content == NULL))
5463
0
  return(-1);
5464
0
    return(xmlFARegExec(comp, content));
5465
0
}
5466
5467
/**
5468
 * xmlRegexpIsDeterminist:
5469
 * @comp:  the compiled regular expression
5470
 *
5471
 * Check if the regular expression is determinist
5472
 *
5473
 * Returns 1 if it yes, 0 if not and a negative value in case of error
5474
 */
5475
int
5476
0
xmlRegexpIsDeterminist(xmlRegexpPtr comp) {
5477
0
    xmlAutomataPtr am;
5478
0
    int ret;
5479
5480
0
    if (comp == NULL)
5481
0
  return(-1);
5482
0
    if (comp->determinist != -1)
5483
0
  return(comp->determinist);
5484
5485
0
    am = xmlNewAutomata();
5486
0
    if (am == NULL)
5487
0
        return(-1);
5488
0
    if (am->states != NULL) {
5489
0
  int i;
5490
5491
0
  for (i = 0;i < am->nbStates;i++)
5492
0
      xmlRegFreeState(am->states[i]);
5493
0
  xmlFree(am->states);
5494
0
    }
5495
0
    am->nbAtoms = comp->nbAtoms;
5496
0
    am->atoms = comp->atoms;
5497
0
    am->nbStates = comp->nbStates;
5498
0
    am->states = comp->states;
5499
0
    am->determinist = -1;
5500
0
    am->flags = comp->flags;
5501
0
    ret = xmlFAComputesDeterminism(am);
5502
0
    am->atoms = NULL;
5503
0
    am->states = NULL;
5504
0
    xmlFreeAutomata(am);
5505
0
    comp->determinist = ret;
5506
0
    return(ret);
5507
0
}
5508
5509
/**
5510
 * xmlRegFreeRegexp:
5511
 * @regexp:  the regexp
5512
 *
5513
 * Free a regexp
5514
 */
5515
void
5516
0
xmlRegFreeRegexp(xmlRegexpPtr regexp) {
5517
0
    int i;
5518
0
    if (regexp == NULL)
5519
0
  return;
5520
5521
0
    if (regexp->string != NULL)
5522
0
  xmlFree(regexp->string);
5523
0
    if (regexp->states != NULL) {
5524
0
  for (i = 0;i < regexp->nbStates;i++)
5525
0
      xmlRegFreeState(regexp->states[i]);
5526
0
  xmlFree(regexp->states);
5527
0
    }
5528
0
    if (regexp->atoms != NULL) {
5529
0
  for (i = 0;i < regexp->nbAtoms;i++)
5530
0
      xmlRegFreeAtom(regexp->atoms[i]);
5531
0
  xmlFree(regexp->atoms);
5532
0
    }
5533
0
    if (regexp->counters != NULL)
5534
0
  xmlFree(regexp->counters);
5535
0
    if (regexp->compact != NULL)
5536
0
  xmlFree(regexp->compact);
5537
0
    if (regexp->transdata != NULL)
5538
0
  xmlFree(regexp->transdata);
5539
0
    if (regexp->stringMap != NULL) {
5540
0
  for (i = 0; i < regexp->nbstrings;i++)
5541
0
      xmlFree(regexp->stringMap[i]);
5542
0
  xmlFree(regexp->stringMap);
5543
0
    }
5544
5545
0
    xmlFree(regexp);
5546
0
}
5547
5548
#ifdef LIBXML_AUTOMATA_ENABLED
5549
/************************************************************************
5550
 *                  *
5551
 *      The Automata interface        *
5552
 *                  *
5553
 ************************************************************************/
5554
5555
/**
5556
 * xmlNewAutomata:
5557
 *
5558
 * Create a new automata
5559
 *
5560
 * Returns the new object or NULL in case of failure
5561
 */
5562
xmlAutomataPtr
5563
0
xmlNewAutomata(void) {
5564
0
    xmlAutomataPtr ctxt;
5565
5566
0
    ctxt = xmlRegNewParserCtxt(NULL);
5567
0
    if (ctxt == NULL)
5568
0
  return(NULL);
5569
5570
    /* initialize the parser */
5571
0
    ctxt->state = xmlRegStatePush(ctxt);
5572
0
    if (ctxt->state == NULL) {
5573
0
  xmlFreeAutomata(ctxt);
5574
0
  return(NULL);
5575
0
    }
5576
0
    ctxt->start = ctxt->state;
5577
0
    ctxt->end = NULL;
5578
5579
0
    ctxt->start->type = XML_REGEXP_START_STATE;
5580
0
    ctxt->flags = 0;
5581
5582
0
    return(ctxt);
5583
0
}
5584
5585
/**
5586
 * xmlFreeAutomata:
5587
 * @am: an automata
5588
 *
5589
 * Free an automata
5590
 */
5591
void
5592
0
xmlFreeAutomata(xmlAutomataPtr am) {
5593
0
    if (am == NULL)
5594
0
  return;
5595
0
    xmlRegFreeParserCtxt(am);
5596
0
}
5597
5598
/**
5599
 * xmlAutomataSetFlags:
5600
 * @am: an automata
5601
 * @flags:  a set of internal flags
5602
 *
5603
 * Set some flags on the automata
5604
 */
5605
void
5606
0
xmlAutomataSetFlags(xmlAutomataPtr am, int flags) {
5607
0
    if (am == NULL)
5608
0
  return;
5609
0
    am->flags |= flags;
5610
0
}
5611
5612
/**
5613
 * xmlAutomataGetInitState:
5614
 * @am: an automata
5615
 *
5616
 * Initial state lookup
5617
 *
5618
 * Returns the initial state of the automata
5619
 */
5620
xmlAutomataStatePtr
5621
0
xmlAutomataGetInitState(xmlAutomataPtr am) {
5622
0
    if (am == NULL)
5623
0
  return(NULL);
5624
0
    return(am->start);
5625
0
}
5626
5627
/**
5628
 * xmlAutomataSetFinalState:
5629
 * @am: an automata
5630
 * @state: a state in this automata
5631
 *
5632
 * Makes that state a final state
5633
 *
5634
 * Returns 0 or -1 in case of error
5635
 */
5636
int
5637
0
xmlAutomataSetFinalState(xmlAutomataPtr am, xmlAutomataStatePtr state) {
5638
0
    if ((am == NULL) || (state == NULL))
5639
0
  return(-1);
5640
0
    state->type = XML_REGEXP_FINAL_STATE;
5641
0
    return(0);
5642
0
}
5643
5644
/**
5645
 * xmlAutomataNewTransition:
5646
 * @am: an automata
5647
 * @from: the starting point of the transition
5648
 * @to: the target point of the transition or NULL
5649
 * @token: the input string associated to that transition
5650
 * @data: data passed to the callback function if the transition is activated
5651
 *
5652
 * If @to is NULL, this creates first a new target state in the automata
5653
 * and then adds a transition from the @from state to the target state
5654
 * activated by the value of @token
5655
 *
5656
 * Returns the target state or NULL in case of error
5657
 */
5658
xmlAutomataStatePtr
5659
xmlAutomataNewTransition(xmlAutomataPtr am, xmlAutomataStatePtr from,
5660
       xmlAutomataStatePtr to, const xmlChar *token,
5661
0
       void *data) {
5662
0
    xmlRegAtomPtr atom;
5663
5664
0
    if ((am == NULL) || (from == NULL) || (token == NULL))
5665
0
  return(NULL);
5666
0
    atom = xmlRegNewAtom(am, XML_REGEXP_STRING);
5667
0
    if (atom == NULL)
5668
0
        return(NULL);
5669
0
    atom->data = data;
5670
0
    atom->valuep = xmlStrdup(token);
5671
0
    if (atom->valuep == NULL) {
5672
0
        xmlRegFreeAtom(atom);
5673
0
        xmlRegexpErrMemory(am);
5674
0
        return(NULL);
5675
0
    }
5676
5677
0
    if (xmlFAGenerateTransitions(am, from, to, atom) < 0) {
5678
0
        xmlRegFreeAtom(atom);
5679
0
  return(NULL);
5680
0
    }
5681
0
    if (to == NULL)
5682
0
  return(am->state);
5683
0
    return(to);
5684
0
}
5685
5686
/**
5687
 * xmlAutomataNewTransition2:
5688
 * @am: an automata
5689
 * @from: the starting point of the transition
5690
 * @to: the target point of the transition or NULL
5691
 * @token: the first input string associated to that transition
5692
 * @token2: the second input string associated to that transition
5693
 * @data: data passed to the callback function if the transition is activated
5694
 *
5695
 * If @to is NULL, this creates first a new target state in the automata
5696
 * and then adds a transition from the @from state to the target state
5697
 * activated by the value of @token
5698
 *
5699
 * Returns the target state or NULL in case of error
5700
 */
5701
xmlAutomataStatePtr
5702
xmlAutomataNewTransition2(xmlAutomataPtr am, xmlAutomataStatePtr from,
5703
        xmlAutomataStatePtr to, const xmlChar *token,
5704
0
        const xmlChar *token2, void *data) {
5705
0
    xmlRegAtomPtr atom;
5706
5707
0
    if ((am == NULL) || (from == NULL) || (token == NULL))
5708
0
  return(NULL);
5709
0
    atom = xmlRegNewAtom(am, XML_REGEXP_STRING);
5710
0
    if (atom == NULL)
5711
0
  return(NULL);
5712
0
    atom->data = data;
5713
0
    if ((token2 == NULL) || (*token2 == 0)) {
5714
0
  atom->valuep = xmlStrdup(token);
5715
0
    } else {
5716
0
  int lenn, lenp;
5717
0
  xmlChar *str;
5718
5719
0
  lenn = strlen((char *) token2);
5720
0
  lenp = strlen((char *) token);
5721
5722
0
  str = (xmlChar *) xmlMallocAtomic(lenn + lenp + 2);
5723
0
  if (str == NULL) {
5724
0
      xmlRegFreeAtom(atom);
5725
0
      return(NULL);
5726
0
  }
5727
0
  memcpy(&str[0], token, lenp);
5728
0
  str[lenp] = '|';
5729
0
  memcpy(&str[lenp + 1], token2, lenn);
5730
0
  str[lenn + lenp + 1] = 0;
5731
5732
0
  atom->valuep = str;
5733
0
    }
5734
5735
0
    if (xmlFAGenerateTransitions(am, from, to, atom) < 0) {
5736
0
        xmlRegFreeAtom(atom);
5737
0
  return(NULL);
5738
0
    }
5739
0
    if (to == NULL)
5740
0
  return(am->state);
5741
0
    return(to);
5742
0
}
5743
5744
/**
5745
 * xmlAutomataNewNegTrans:
5746
 * @am: an automata
5747
 * @from: the starting point of the transition
5748
 * @to: the target point of the transition or NULL
5749
 * @token: the first input string associated to that transition
5750
 * @token2: the second input string associated to that transition
5751
 * @data: data passed to the callback function if the transition is activated
5752
 *
5753
 * If @to is NULL, this creates first a new target state in the automata
5754
 * and then adds a transition from the @from state to the target state
5755
 * activated by any value except (@token,@token2)
5756
 * Note that if @token2 is not NULL, then (X, NULL) won't match to follow
5757
 # the semantic of XSD ##other
5758
 *
5759
 * Returns the target state or NULL in case of error
5760
 */
5761
xmlAutomataStatePtr
5762
xmlAutomataNewNegTrans(xmlAutomataPtr am, xmlAutomataStatePtr from,
5763
           xmlAutomataStatePtr to, const xmlChar *token,
5764
0
           const xmlChar *token2, void *data) {
5765
0
    xmlRegAtomPtr atom;
5766
0
    xmlChar err_msg[200];
5767
5768
0
    if ((am == NULL) || (from == NULL) || (token == NULL))
5769
0
  return(NULL);
5770
0
    atom = xmlRegNewAtom(am, XML_REGEXP_STRING);
5771
0
    if (atom == NULL)
5772
0
  return(NULL);
5773
0
    atom->data = data;
5774
0
    atom->neg = 1;
5775
0
    if ((token2 == NULL) || (*token2 == 0)) {
5776
0
  atom->valuep = xmlStrdup(token);
5777
0
    } else {
5778
0
  int lenn, lenp;
5779
0
  xmlChar *str;
5780
5781
0
  lenn = strlen((char *) token2);
5782
0
  lenp = strlen((char *) token);
5783
5784
0
  str = (xmlChar *) xmlMallocAtomic(lenn + lenp + 2);
5785
0
  if (str == NULL) {
5786
0
      xmlRegFreeAtom(atom);
5787
0
      return(NULL);
5788
0
  }
5789
0
  memcpy(&str[0], token, lenp);
5790
0
  str[lenp] = '|';
5791
0
  memcpy(&str[lenp + 1], token2, lenn);
5792
0
  str[lenn + lenp + 1] = 0;
5793
5794
0
  atom->valuep = str;
5795
0
    }
5796
0
    snprintf((char *) err_msg, 199, "not %s", (const char *) atom->valuep);
5797
0
    err_msg[199] = 0;
5798
0
    atom->valuep2 = xmlStrdup(err_msg);
5799
5800
0
    if (xmlFAGenerateTransitions(am, from, to, atom) < 0) {
5801
0
        xmlRegFreeAtom(atom);
5802
0
  return(NULL);
5803
0
    }
5804
0
    am->negs++;
5805
0
    if (to == NULL)
5806
0
  return(am->state);
5807
0
    return(to);
5808
0
}
5809
5810
/**
5811
 * xmlAutomataNewCountTrans2:
5812
 * @am: an automata
5813
 * @from: the starting point of the transition
5814
 * @to: the target point of the transition or NULL
5815
 * @token: the input string associated to that transition
5816
 * @token2: the second input string associated to that transition
5817
 * @min:  the minimum successive occurrences of token
5818
 * @max:  the maximum successive occurrences of token
5819
 * @data:  data associated to the transition
5820
 *
5821
 * If @to is NULL, this creates first a new target state in the automata
5822
 * and then adds a transition from the @from state to the target state
5823
 * activated by a succession of input of value @token and @token2 and
5824
 * whose number is between @min and @max
5825
 *
5826
 * Returns the target state or NULL in case of error
5827
 */
5828
xmlAutomataStatePtr
5829
xmlAutomataNewCountTrans2(xmlAutomataPtr am, xmlAutomataStatePtr from,
5830
       xmlAutomataStatePtr to, const xmlChar *token,
5831
       const xmlChar *token2,
5832
0
       int min, int max, void *data) {
5833
0
    xmlRegAtomPtr atom;
5834
0
    int counter;
5835
5836
0
    if ((am == NULL) || (from == NULL) || (token == NULL))
5837
0
  return(NULL);
5838
0
    if (min < 0)
5839
0
  return(NULL);
5840
0
    if ((max < min) || (max < 1))
5841
0
  return(NULL);
5842
0
    atom = xmlRegNewAtom(am, XML_REGEXP_STRING);
5843
0
    if (atom == NULL)
5844
0
  return(NULL);
5845
0
    if ((token2 == NULL) || (*token2 == 0)) {
5846
0
  atom->valuep = xmlStrdup(token);
5847
0
        if (atom->valuep == NULL)
5848
0
            goto error;
5849
0
    } else {
5850
0
  int lenn, lenp;
5851
0
  xmlChar *str;
5852
5853
0
  lenn = strlen((char *) token2);
5854
0
  lenp = strlen((char *) token);
5855
5856
0
  str = (xmlChar *) xmlMallocAtomic(lenn + lenp + 2);
5857
0
  if (str == NULL)
5858
0
      goto error;
5859
0
  memcpy(&str[0], token, lenp);
5860
0
  str[lenp] = '|';
5861
0
  memcpy(&str[lenp + 1], token2, lenn);
5862
0
  str[lenn + lenp + 1] = 0;
5863
5864
0
  atom->valuep = str;
5865
0
    }
5866
0
    atom->data = data;
5867
0
    if (min == 0)
5868
0
  atom->min = 1;
5869
0
    else
5870
0
  atom->min = min;
5871
0
    atom->max = max;
5872
5873
    /*
5874
     * associate a counter to the transition.
5875
     */
5876
0
    counter = xmlRegGetCounter(am);
5877
0
    if (counter < 0)
5878
0
        goto error;
5879
0
    am->counters[counter].min = min;
5880
0
    am->counters[counter].max = max;
5881
5882
    /* xmlFAGenerateTransitions(am, from, to, atom); */
5883
0
    if (to == NULL) {
5884
0
  to = xmlRegStatePush(am);
5885
0
        if (to == NULL)
5886
0
            goto error;
5887
0
    }
5888
0
    xmlRegStateAddTrans(am, from, atom, to, counter, -1);
5889
0
    if (xmlRegAtomPush(am, atom) < 0)
5890
0
        goto error;
5891
0
    am->state = to;
5892
5893
0
    if (to == NULL)
5894
0
  to = am->state;
5895
0
    if (to == NULL)
5896
0
  return(NULL);
5897
0
    if (min == 0)
5898
0
  xmlFAGenerateEpsilonTransition(am, from, to);
5899
0
    return(to);
5900
5901
0
error:
5902
0
    xmlRegFreeAtom(atom);
5903
0
    return(NULL);
5904
0
}
5905
5906
/**
5907
 * xmlAutomataNewCountTrans:
5908
 * @am: an automata
5909
 * @from: the starting point of the transition
5910
 * @to: the target point of the transition or NULL
5911
 * @token: the input string associated to that transition
5912
 * @min:  the minimum successive occurrences of token
5913
 * @max:  the maximum successive occurrences of token
5914
 * @data:  data associated to the transition
5915
 *
5916
 * If @to is NULL, this creates first a new target state in the automata
5917
 * and then adds a transition from the @from state to the target state
5918
 * activated by a succession of input of value @token and whose number
5919
 * is between @min and @max
5920
 *
5921
 * Returns the target state or NULL in case of error
5922
 */
5923
xmlAutomataStatePtr
5924
xmlAutomataNewCountTrans(xmlAutomataPtr am, xmlAutomataStatePtr from,
5925
       xmlAutomataStatePtr to, const xmlChar *token,
5926
0
       int min, int max, void *data) {
5927
0
    xmlRegAtomPtr atom;
5928
0
    int counter;
5929
5930
0
    if ((am == NULL) || (from == NULL) || (token == NULL))
5931
0
  return(NULL);
5932
0
    if (min < 0)
5933
0
  return(NULL);
5934
0
    if ((max < min) || (max < 1))
5935
0
  return(NULL);
5936
0
    atom = xmlRegNewAtom(am, XML_REGEXP_STRING);
5937
0
    if (atom == NULL)
5938
0
  return(NULL);
5939
0
    atom->valuep = xmlStrdup(token);
5940
0
    if (atom->valuep == NULL)
5941
0
        goto error;
5942
0
    atom->data = data;
5943
0
    if (min == 0)
5944
0
  atom->min = 1;
5945
0
    else
5946
0
  atom->min = min;
5947
0
    atom->max = max;
5948
5949
    /*
5950
     * associate a counter to the transition.
5951
     */
5952
0
    counter = xmlRegGetCounter(am);
5953
0
    if (counter < 0)
5954
0
        goto error;
5955
0
    am->counters[counter].min = min;
5956
0
    am->counters[counter].max = max;
5957
5958
    /* xmlFAGenerateTransitions(am, from, to, atom); */
5959
0
    if (to == NULL) {
5960
0
  to = xmlRegStatePush(am);
5961
0
        if (to == NULL)
5962
0
            goto error;
5963
0
    }
5964
0
    xmlRegStateAddTrans(am, from, atom, to, counter, -1);
5965
0
    if (xmlRegAtomPush(am, atom) < 0)
5966
0
        goto error;
5967
0
    am->state = to;
5968
5969
0
    if (to == NULL)
5970
0
  to = am->state;
5971
0
    if (to == NULL)
5972
0
  return(NULL);
5973
0
    if (min == 0)
5974
0
  xmlFAGenerateEpsilonTransition(am, from, to);
5975
0
    return(to);
5976
5977
0
error:
5978
0
    xmlRegFreeAtom(atom);
5979
0
    return(NULL);
5980
0
}
5981
5982
/**
5983
 * xmlAutomataNewOnceTrans2:
5984
 * @am: an automata
5985
 * @from: the starting point of the transition
5986
 * @to: the target point of the transition or NULL
5987
 * @token: the input string associated to that transition
5988
 * @token2: the second input string associated to that transition
5989
 * @min:  the minimum successive occurrences of token
5990
 * @max:  the maximum successive occurrences of token
5991
 * @data:  data associated to the transition
5992
 *
5993
 * If @to is NULL, this creates first a new target state in the automata
5994
 * and then adds a transition from the @from state to the target state
5995
 * activated by a succession of input of value @token and @token2 and whose
5996
 * number is between @min and @max, moreover that transition can only be
5997
 * crossed once.
5998
 *
5999
 * Returns the target state or NULL in case of error
6000
 */
6001
xmlAutomataStatePtr
6002
xmlAutomataNewOnceTrans2(xmlAutomataPtr am, xmlAutomataStatePtr from,
6003
       xmlAutomataStatePtr to, const xmlChar *token,
6004
       const xmlChar *token2,
6005
0
       int min, int max, void *data) {
6006
0
    xmlRegAtomPtr atom;
6007
0
    int counter;
6008
6009
0
    if ((am == NULL) || (from == NULL) || (token == NULL))
6010
0
  return(NULL);
6011
0
    if (min < 1)
6012
0
  return(NULL);
6013
0
    if (max < min)
6014
0
  return(NULL);
6015
0
    atom = xmlRegNewAtom(am, XML_REGEXP_STRING);
6016
0
    if (atom == NULL)
6017
0
  return(NULL);
6018
0
    if ((token2 == NULL) || (*token2 == 0)) {
6019
0
  atom->valuep = xmlStrdup(token);
6020
0
        if (atom->valuep == NULL)
6021
0
            goto error;
6022
0
    } else {
6023
0
  int lenn, lenp;
6024
0
  xmlChar *str;
6025
6026
0
  lenn = strlen((char *) token2);
6027
0
  lenp = strlen((char *) token);
6028
6029
0
  str = (xmlChar *) xmlMallocAtomic(lenn + lenp + 2);
6030
0
  if (str == NULL)
6031
0
      goto error;
6032
0
  memcpy(&str[0], token, lenp);
6033
0
  str[lenp] = '|';
6034
0
  memcpy(&str[lenp + 1], token2, lenn);
6035
0
  str[lenn + lenp + 1] = 0;
6036
6037
0
  atom->valuep = str;
6038
0
    }
6039
0
    atom->data = data;
6040
0
    atom->quant = XML_REGEXP_QUANT_ONCEONLY;
6041
0
    atom->min = min;
6042
0
    atom->max = max;
6043
    /*
6044
     * associate a counter to the transition.
6045
     */
6046
0
    counter = xmlRegGetCounter(am);
6047
0
    if (counter < 0)
6048
0
        goto error;
6049
0
    am->counters[counter].min = 1;
6050
0
    am->counters[counter].max = 1;
6051
6052
    /* xmlFAGenerateTransitions(am, from, to, atom); */
6053
0
    if (to == NULL) {
6054
0
  to = xmlRegStatePush(am);
6055
0
        if (to == NULL)
6056
0
            goto error;
6057
0
    }
6058
0
    xmlRegStateAddTrans(am, from, atom, to, counter, -1);
6059
0
    if (xmlRegAtomPush(am, atom) < 0)
6060
0
        goto error;
6061
0
    am->state = to;
6062
0
    return(to);
6063
6064
0
error:
6065
0
    xmlRegFreeAtom(atom);
6066
0
    return(NULL);
6067
0
}
6068
6069
6070
6071
/**
6072
 * xmlAutomataNewOnceTrans:
6073
 * @am: an automata
6074
 * @from: the starting point of the transition
6075
 * @to: the target point of the transition or NULL
6076
 * @token: the input string associated to that transition
6077
 * @min:  the minimum successive occurrences of token
6078
 * @max:  the maximum successive occurrences of token
6079
 * @data:  data associated to the transition
6080
 *
6081
 * If @to is NULL, this creates first a new target state in the automata
6082
 * and then adds a transition from the @from state to the target state
6083
 * activated by a succession of input of value @token and whose number
6084
 * is between @min and @max, moreover that transition can only be crossed
6085
 * once.
6086
 *
6087
 * Returns the target state or NULL in case of error
6088
 */
6089
xmlAutomataStatePtr
6090
xmlAutomataNewOnceTrans(xmlAutomataPtr am, xmlAutomataStatePtr from,
6091
       xmlAutomataStatePtr to, const xmlChar *token,
6092
0
       int min, int max, void *data) {
6093
0
    xmlRegAtomPtr atom;
6094
0
    int counter;
6095
6096
0
    if ((am == NULL) || (from == NULL) || (token == NULL))
6097
0
  return(NULL);
6098
0
    if (min < 1)
6099
0
  return(NULL);
6100
0
    if (max < min)
6101
0
  return(NULL);
6102
0
    atom = xmlRegNewAtom(am, XML_REGEXP_STRING);
6103
0
    if (atom == NULL)
6104
0
  return(NULL);
6105
0
    atom->valuep = xmlStrdup(token);
6106
0
    atom->data = data;
6107
0
    atom->quant = XML_REGEXP_QUANT_ONCEONLY;
6108
0
    atom->min = min;
6109
0
    atom->max = max;
6110
    /*
6111
     * associate a counter to the transition.
6112
     */
6113
0
    counter = xmlRegGetCounter(am);
6114
0
    if (counter < 0)
6115
0
        goto error;
6116
0
    am->counters[counter].min = 1;
6117
0
    am->counters[counter].max = 1;
6118
6119
    /* xmlFAGenerateTransitions(am, from, to, atom); */
6120
0
    if (to == NULL) {
6121
0
  to = xmlRegStatePush(am);
6122
0
        if (to == NULL)
6123
0
            goto error;
6124
0
    }
6125
0
    xmlRegStateAddTrans(am, from, atom, to, counter, -1);
6126
0
    if (xmlRegAtomPush(am, atom) < 0)
6127
0
        goto error;
6128
0
    am->state = to;
6129
0
    return(to);
6130
6131
0
error:
6132
0
    xmlRegFreeAtom(atom);
6133
0
    return(NULL);
6134
0
}
6135
6136
/**
6137
 * xmlAutomataNewState:
6138
 * @am: an automata
6139
 *
6140
 * Create a new disconnected state in the automata
6141
 *
6142
 * Returns the new state or NULL in case of error
6143
 */
6144
xmlAutomataStatePtr
6145
0
xmlAutomataNewState(xmlAutomataPtr am) {
6146
0
    if (am == NULL)
6147
0
  return(NULL);
6148
0
    return(xmlRegStatePush(am));
6149
0
}
6150
6151
/**
6152
 * xmlAutomataNewEpsilon:
6153
 * @am: an automata
6154
 * @from: the starting point of the transition
6155
 * @to: the target point of the transition or NULL
6156
 *
6157
 * If @to is NULL, this creates first a new target state in the automata
6158
 * and then adds an epsilon transition from the @from state to the
6159
 * target state
6160
 *
6161
 * Returns the target state or NULL in case of error
6162
 */
6163
xmlAutomataStatePtr
6164
xmlAutomataNewEpsilon(xmlAutomataPtr am, xmlAutomataStatePtr from,
6165
0
          xmlAutomataStatePtr to) {
6166
0
    if ((am == NULL) || (from == NULL))
6167
0
  return(NULL);
6168
0
    xmlFAGenerateEpsilonTransition(am, from, to);
6169
0
    if (to == NULL)
6170
0
  return(am->state);
6171
0
    return(to);
6172
0
}
6173
6174
/**
6175
 * xmlAutomataNewAllTrans:
6176
 * @am: an automata
6177
 * @from: the starting point of the transition
6178
 * @to: the target point of the transition or NULL
6179
 * @lax: allow to transition if not all all transitions have been activated
6180
 *
6181
 * If @to is NULL, this creates first a new target state in the automata
6182
 * and then adds a an ALL transition from the @from state to the
6183
 * target state. That transition is an epsilon transition allowed only when
6184
 * all transitions from the @from node have been activated.
6185
 *
6186
 * Returns the target state or NULL in case of error
6187
 */
6188
xmlAutomataStatePtr
6189
xmlAutomataNewAllTrans(xmlAutomataPtr am, xmlAutomataStatePtr from,
6190
0
           xmlAutomataStatePtr to, int lax) {
6191
0
    if ((am == NULL) || (from == NULL))
6192
0
  return(NULL);
6193
0
    xmlFAGenerateAllTransition(am, from, to, lax);
6194
0
    if (to == NULL)
6195
0
  return(am->state);
6196
0
    return(to);
6197
0
}
6198
6199
/**
6200
 * xmlAutomataNewCounter:
6201
 * @am: an automata
6202
 * @min:  the minimal value on the counter
6203
 * @max:  the maximal value on the counter
6204
 *
6205
 * Create a new counter
6206
 *
6207
 * Returns the counter number or -1 in case of error
6208
 */
6209
int
6210
0
xmlAutomataNewCounter(xmlAutomataPtr am, int min, int max) {
6211
0
    int ret;
6212
6213
0
    if (am == NULL)
6214
0
  return(-1);
6215
6216
0
    ret = xmlRegGetCounter(am);
6217
0
    if (ret < 0)
6218
0
  return(-1);
6219
0
    am->counters[ret].min = min;
6220
0
    am->counters[ret].max = max;
6221
0
    return(ret);
6222
0
}
6223
6224
/**
6225
 * xmlAutomataNewCountedTrans:
6226
 * @am: an automata
6227
 * @from: the starting point of the transition
6228
 * @to: the target point of the transition or NULL
6229
 * @counter: the counter associated to that transition
6230
 *
6231
 * If @to is NULL, this creates first a new target state in the automata
6232
 * and then adds an epsilon transition from the @from state to the target state
6233
 * which will increment the counter provided
6234
 *
6235
 * Returns the target state or NULL in case of error
6236
 */
6237
xmlAutomataStatePtr
6238
xmlAutomataNewCountedTrans(xmlAutomataPtr am, xmlAutomataStatePtr from,
6239
0
    xmlAutomataStatePtr to, int counter) {
6240
0
    if ((am == NULL) || (from == NULL) || (counter < 0))
6241
0
  return(NULL);
6242
0
    xmlFAGenerateCountedEpsilonTransition(am, from, to, counter);
6243
0
    if (to == NULL)
6244
0
  return(am->state);
6245
0
    return(to);
6246
0
}
6247
6248
/**
6249
 * xmlAutomataNewCounterTrans:
6250
 * @am: an automata
6251
 * @from: the starting point of the transition
6252
 * @to: the target point of the transition or NULL
6253
 * @counter: the counter associated to that transition
6254
 *
6255
 * If @to is NULL, this creates first a new target state in the automata
6256
 * and then adds an epsilon transition from the @from state to the target state
6257
 * which will be allowed only if the counter is within the right range.
6258
 *
6259
 * Returns the target state or NULL in case of error
6260
 */
6261
xmlAutomataStatePtr
6262
xmlAutomataNewCounterTrans(xmlAutomataPtr am, xmlAutomataStatePtr from,
6263
0
    xmlAutomataStatePtr to, int counter) {
6264
0
    if ((am == NULL) || (from == NULL) || (counter < 0))
6265
0
  return(NULL);
6266
0
    xmlFAGenerateCountedTransition(am, from, to, counter);
6267
0
    if (to == NULL)
6268
0
  return(am->state);
6269
0
    return(to);
6270
0
}
6271
6272
/**
6273
 * xmlAutomataCompile:
6274
 * @am: an automata
6275
 *
6276
 * Compile the automata into a Reg Exp ready for being executed.
6277
 * The automata should be free after this point.
6278
 *
6279
 * Returns the compiled regexp or NULL in case of error
6280
 */
6281
xmlRegexpPtr
6282
0
xmlAutomataCompile(xmlAutomataPtr am) {
6283
0
    xmlRegexpPtr ret;
6284
6285
0
    if ((am == NULL) || (am->error != 0)) return(NULL);
6286
0
    xmlFAEliminateEpsilonTransitions(am);
6287
0
    if (am->error != 0)
6288
0
        return(NULL);
6289
    /* xmlFAComputesDeterminism(am); */
6290
0
    ret = xmlRegEpxFromParse(am);
6291
6292
0
    return(ret);
6293
0
}
6294
6295
/**
6296
 * xmlAutomataIsDeterminist:
6297
 * @am: an automata
6298
 *
6299
 * Checks if an automata is determinist.
6300
 *
6301
 * Returns 1 if true, 0 if not, and -1 in case of error
6302
 */
6303
int
6304
0
xmlAutomataIsDeterminist(xmlAutomataPtr am) {
6305
0
    int ret;
6306
6307
0
    if (am == NULL)
6308
0
  return(-1);
6309
6310
0
    ret = xmlFAComputesDeterminism(am);
6311
0
    return(ret);
6312
0
}
6313
#endif /* LIBXML_AUTOMATA_ENABLED */
6314
6315
#ifdef LIBXML_EXPR_ENABLED
6316
/************************************************************************
6317
 *                  *
6318
 *    Formal Expression handling code       *
6319
 *                  *
6320
 ************************************************************************/
6321
/************************************************************************
6322
 *                  *
6323
 *    Expression handling context       *
6324
 *                  *
6325
 ************************************************************************/
6326
6327
struct _xmlExpCtxt {
6328
    xmlDictPtr dict;
6329
    xmlExpNodePtr *table;
6330
    int size;
6331
    int nbElems;
6332
    int nb_nodes;
6333
    int maxNodes;
6334
    const char *expr;
6335
    const char *cur;
6336
    int nb_cons;
6337
    int tabSize;
6338
};
6339
6340
/**
6341
 * xmlExpNewCtxt:
6342
 * @maxNodes:  the maximum number of nodes
6343
 * @dict:  optional dictionary to use internally
6344
 *
6345
 * Creates a new context for manipulating expressions
6346
 *
6347
 * Returns the context or NULL in case of error
6348
 */
6349
xmlExpCtxtPtr
6350
xmlExpNewCtxt(int maxNodes, xmlDictPtr dict) {
6351
    xmlExpCtxtPtr ret;
6352
    int size = 256;
6353
6354
    if (maxNodes <= 4096)
6355
        maxNodes = 4096;
6356
6357
    ret = (xmlExpCtxtPtr) xmlMalloc(sizeof(xmlExpCtxt));
6358
    if (ret == NULL)
6359
        return(NULL);
6360
    memset(ret, 0, sizeof(xmlExpCtxt));
6361
    ret->size = size;
6362
    ret->nbElems = 0;
6363
    ret->maxNodes = maxNodes;
6364
    ret->table = xmlMalloc(size * sizeof(xmlExpNodePtr));
6365
    if (ret->table == NULL) {
6366
        xmlFree(ret);
6367
  return(NULL);
6368
    }
6369
    memset(ret->table, 0, size * sizeof(xmlExpNodePtr));
6370
    if (dict == NULL) {
6371
        ret->dict = xmlDictCreate();
6372
  if (ret->dict == NULL) {
6373
      xmlFree(ret->table);
6374
      xmlFree(ret);
6375
      return(NULL);
6376
  }
6377
    } else {
6378
        ret->dict = dict;
6379
  xmlDictReference(ret->dict);
6380
    }
6381
    return(ret);
6382
}
6383
6384
/**
6385
 * xmlExpFreeCtxt:
6386
 * @ctxt:  an expression context
6387
 *
6388
 * Free an expression context
6389
 */
6390
void
6391
xmlExpFreeCtxt(xmlExpCtxtPtr ctxt) {
6392
    if (ctxt == NULL)
6393
        return;
6394
    xmlDictFree(ctxt->dict);
6395
    if (ctxt->table != NULL)
6396
  xmlFree(ctxt->table);
6397
    xmlFree(ctxt);
6398
}
6399
6400
/************************************************************************
6401
 *                  *
6402
 *    Structure associated to an expression node    *
6403
 *                  *
6404
 ************************************************************************/
6405
#define MAX_NODES 10000
6406
6407
/*
6408
 * TODO:
6409
 * - Wildcards
6410
 * - public API for creation
6411
 *
6412
 * Started
6413
 * - regression testing
6414
 *
6415
 * Done
6416
 * - split into module and test tool
6417
 * - memleaks
6418
 */
6419
6420
typedef enum {
6421
    XML_EXP_NILABLE = (1 << 0)
6422
} xmlExpNodeInfo;
6423
6424
#define IS_NILLABLE(node) ((node)->info & XML_EXP_NILABLE)
6425
6426
struct _xmlExpNode {
6427
    unsigned char type;/* xmlExpNodeType */
6428
    unsigned char info;/* OR of xmlExpNodeInfo */
6429
    unsigned short key; /* the hash key */
6430
    unsigned int ref; /* The number of references */
6431
    int c_max;    /* the maximum length it can consume */
6432
    xmlExpNodePtr exp_left;
6433
    xmlExpNodePtr next;/* the next node in the hash table or free list */
6434
    union {
6435
  struct {
6436
      int f_min;
6437
      int f_max;
6438
  } count;
6439
  struct {
6440
      xmlExpNodePtr f_right;
6441
  } children;
6442
        const xmlChar *f_str;
6443
    } field;
6444
};
6445
6446
#define exp_min field.count.f_min
6447
#define exp_max field.count.f_max
6448
/* #define exp_left field.children.f_left */
6449
#define exp_right field.children.f_right
6450
#define exp_str field.f_str
6451
6452
static xmlExpNodePtr xmlExpNewNode(xmlExpCtxtPtr ctxt, xmlExpNodeType type);
6453
static xmlExpNode forbiddenExpNode = {
6454
    XML_EXP_FORBID, 0, 0, 0, 0, NULL, NULL, {{ 0, 0}}
6455
};
6456
xmlExpNodePtr forbiddenExp = &forbiddenExpNode;
6457
static xmlExpNode emptyExpNode = {
6458
    XML_EXP_EMPTY, 1, 0, 0, 0, NULL, NULL, {{ 0, 0}}
6459
};
6460
xmlExpNodePtr emptyExp = &emptyExpNode;
6461
6462
/************************************************************************
6463
 *                  *
6464
 *  The custom hash table for unicity and canonicalization    *
6465
 *  of sub-expressions pointers           *
6466
 *                  *
6467
 ************************************************************************/
6468
/*
6469
 * xmlExpHashNameComputeKey:
6470
 * Calculate the hash key for a token
6471
 */
6472
static unsigned short
6473
xmlExpHashNameComputeKey(const xmlChar *name) {
6474
    unsigned short value = 0L;
6475
    char ch;
6476
6477
    if (name != NULL) {
6478
  value += 30 * (*name);
6479
  while ((ch = *name++) != 0) {
6480
      value = value ^ ((value << 5) + (value >> 3) + (unsigned long)ch);
6481
  }
6482
    }
6483
    return (value);
6484
}
6485
6486
/*
6487
 * xmlExpHashComputeKey:
6488
 * Calculate the hash key for a compound expression
6489
 */
6490
static unsigned short
6491
xmlExpHashComputeKey(xmlExpNodeType type, xmlExpNodePtr left,
6492
                     xmlExpNodePtr right) {
6493
    unsigned long value;
6494
    unsigned short ret;
6495
6496
    switch (type) {
6497
        case XML_EXP_SEQ:
6498
      value = left->key;
6499
      value += right->key;
6500
      value *= 3;
6501
      ret = (unsigned short) value;
6502
      break;
6503
        case XML_EXP_OR:
6504
      value = left->key;
6505
      value += right->key;
6506
      value *= 7;
6507
      ret = (unsigned short) value;
6508
      break;
6509
        case XML_EXP_COUNT:
6510
      value = left->key;
6511
      value += right->key;
6512
      ret = (unsigned short) value;
6513
      break;
6514
  default:
6515
      ret = 0;
6516
    }
6517
    return(ret);
6518
}
6519
6520
6521
static xmlExpNodePtr
6522
xmlExpNewNode(xmlExpCtxtPtr ctxt, xmlExpNodeType type) {
6523
    xmlExpNodePtr ret;
6524
6525
    if (ctxt->nb_nodes >= MAX_NODES)
6526
        return(NULL);
6527
    ret = (xmlExpNodePtr) xmlMalloc(sizeof(xmlExpNode));
6528
    if (ret == NULL)
6529
        return(NULL);
6530
    memset(ret, 0, sizeof(xmlExpNode));
6531
    ret->type = type;
6532
    ret->next = NULL;
6533
    ctxt->nb_nodes++;
6534
    ctxt->nb_cons++;
6535
    return(ret);
6536
}
6537
6538
/**
6539
 * xmlExpHashGetEntry:
6540
 * @table: the hash table
6541
 *
6542
 * Get the unique entry from the hash table. The entry is created if
6543
 * needed. @left and @right are consumed, i.e. their ref count will
6544
 * be decremented by the operation.
6545
 *
6546
 * Returns the pointer or NULL in case of error
6547
 */
6548
static xmlExpNodePtr
6549
xmlExpHashGetEntry(xmlExpCtxtPtr ctxt, xmlExpNodeType type,
6550
                   xmlExpNodePtr left, xmlExpNodePtr right,
6551
       const xmlChar *name, int min, int max) {
6552
    unsigned short kbase, key;
6553
    xmlExpNodePtr entry;
6554
    xmlExpNodePtr insert;
6555
6556
    if (ctxt == NULL)
6557
  return(NULL);
6558
6559
    /*
6560
     * Check for duplicate and insertion location.
6561
     */
6562
    if (type == XML_EXP_ATOM) {
6563
  kbase = xmlExpHashNameComputeKey(name);
6564
    } else if (type == XML_EXP_COUNT) {
6565
        /* COUNT reduction rule 1 */
6566
  /* a{1} -> a */
6567
  if (min == max) {
6568
      if (min == 1) {
6569
    return(left);
6570
      }
6571
      if (min == 0) {
6572
    xmlExpFree(ctxt, left);
6573
          return(emptyExp);
6574
      }
6575
  }
6576
  if (min < 0) {
6577
      xmlExpFree(ctxt, left);
6578
      return(forbiddenExp);
6579
  }
6580
        if (max == -1)
6581
      kbase = min + 79;
6582
  else
6583
      kbase = max - min;
6584
  kbase += left->key;
6585
    } else if (type == XML_EXP_OR) {
6586
        /* Forbid reduction rules */
6587
        if (left->type == XML_EXP_FORBID) {
6588
      xmlExpFree(ctxt, left);
6589
      return(right);
6590
  }
6591
        if (right->type == XML_EXP_FORBID) {
6592
      xmlExpFree(ctxt, right);
6593
      return(left);
6594
  }
6595
6596
        /* OR reduction rule 1 */
6597
  /* a | a reduced to a */
6598
        if (left == right) {
6599
      xmlExpFree(ctxt, right);
6600
      return(left);
6601
  }
6602
        /* OR canonicalization rule 1 */
6603
  /* linearize (a | b) | c into a | (b | c) */
6604
        if ((left->type == XML_EXP_OR) && (right->type != XML_EXP_OR)) {
6605
      xmlExpNodePtr tmp = left;
6606
            left = right;
6607
      right = tmp;
6608
  }
6609
        /* OR reduction rule 2 */
6610
  /* a | (a | b) and b | (a | b) are reduced to a | b */
6611
        if (right->type == XML_EXP_OR) {
6612
      if ((left == right->exp_left) ||
6613
          (left == right->exp_right)) {
6614
    xmlExpFree(ctxt, left);
6615
    return(right);
6616
      }
6617
  }
6618
        /* OR canonicalization rule 2 */
6619
  /* linearize (a | b) | c into a | (b | c) */
6620
        if (left->type == XML_EXP_OR) {
6621
      xmlExpNodePtr tmp;
6622
6623
      /* OR canonicalization rule 2 */
6624
      if ((left->exp_right->type != XML_EXP_OR) &&
6625
          (left->exp_right->key < left->exp_left->key)) {
6626
          tmp = left->exp_right;
6627
    left->exp_right = left->exp_left;
6628
    left->exp_left = tmp;
6629
      }
6630
      left->exp_right->ref++;
6631
      tmp = xmlExpHashGetEntry(ctxt, XML_EXP_OR, left->exp_right, right,
6632
                               NULL, 0, 0);
6633
      left->exp_left->ref++;
6634
      tmp = xmlExpHashGetEntry(ctxt, XML_EXP_OR, left->exp_left, tmp,
6635
                               NULL, 0, 0);
6636
6637
      xmlExpFree(ctxt, left);
6638
      return(tmp);
6639
  }
6640
  if (right->type == XML_EXP_OR) {
6641
      /* Ordering in the tree */
6642
      /* C | (A | B) -> A | (B | C) */
6643
      if (left->key > right->exp_right->key) {
6644
    xmlExpNodePtr tmp;
6645
    right->exp_right->ref++;
6646
    tmp = xmlExpHashGetEntry(ctxt, XML_EXP_OR, right->exp_right,
6647
                             left, NULL, 0, 0);
6648
    right->exp_left->ref++;
6649
    tmp = xmlExpHashGetEntry(ctxt, XML_EXP_OR, right->exp_left,
6650
                             tmp, NULL, 0, 0);
6651
    xmlExpFree(ctxt, right);
6652
    return(tmp);
6653
      }
6654
      /* Ordering in the tree */
6655
      /* B | (A | C) -> A | (B | C) */
6656
      if (left->key > right->exp_left->key) {
6657
    xmlExpNodePtr tmp;
6658
    right->exp_right->ref++;
6659
    tmp = xmlExpHashGetEntry(ctxt, XML_EXP_OR, left,
6660
                             right->exp_right, NULL, 0, 0);
6661
    right->exp_left->ref++;
6662
    tmp = xmlExpHashGetEntry(ctxt, XML_EXP_OR, right->exp_left,
6663
                             tmp, NULL, 0, 0);
6664
    xmlExpFree(ctxt, right);
6665
    return(tmp);
6666
      }
6667
  }
6668
  /* we know both types are != XML_EXP_OR here */
6669
        else if (left->key > right->key) {
6670
      xmlExpNodePtr tmp = left;
6671
            left = right;
6672
      right = tmp;
6673
  }
6674
  kbase = xmlExpHashComputeKey(type, left, right);
6675
    } else if (type == XML_EXP_SEQ) {
6676
        /* Forbid reduction rules */
6677
        if (left->type == XML_EXP_FORBID) {
6678
      xmlExpFree(ctxt, right);
6679
      return(left);
6680
  }
6681
        if (right->type == XML_EXP_FORBID) {
6682
      xmlExpFree(ctxt, left);
6683
      return(right);
6684
  }
6685
        /* Empty reduction rules */
6686
        if (right->type == XML_EXP_EMPTY) {
6687
      return(left);
6688
  }
6689
        if (left->type == XML_EXP_EMPTY) {
6690
      return(right);
6691
  }
6692
  kbase = xmlExpHashComputeKey(type, left, right);
6693
    } else
6694
        return(NULL);
6695
6696
    key = kbase % ctxt->size;
6697
    if (ctxt->table[key] != NULL) {
6698
  for (insert = ctxt->table[key]; insert != NULL;
6699
       insert = insert->next) {
6700
      if ((insert->key == kbase) &&
6701
          (insert->type == type)) {
6702
    if (type == XML_EXP_ATOM) {
6703
        if (name == insert->exp_str) {
6704
      insert->ref++;
6705
      return(insert);
6706
        }
6707
    } else if (type == XML_EXP_COUNT) {
6708
        if ((insert->exp_min == min) && (insert->exp_max == max) &&
6709
            (insert->exp_left == left)) {
6710
      insert->ref++;
6711
      left->ref--;
6712
      return(insert);
6713
        }
6714
    } else if ((insert->exp_left == left) &&
6715
         (insert->exp_right == right)) {
6716
        insert->ref++;
6717
        left->ref--;
6718
        right->ref--;
6719
        return(insert);
6720
    }
6721
      }
6722
  }
6723
    }
6724
6725
    entry = xmlExpNewNode(ctxt, type);
6726
    if (entry == NULL)
6727
        return(NULL);
6728
    entry->key = kbase;
6729
    if (type == XML_EXP_ATOM) {
6730
  entry->exp_str = name;
6731
  entry->c_max = 1;
6732
    } else if (type == XML_EXP_COUNT) {
6733
        entry->exp_min = min;
6734
        entry->exp_max = max;
6735
  entry->exp_left = left;
6736
  if ((min == 0) || (IS_NILLABLE(left)))
6737
      entry->info |= XML_EXP_NILABLE;
6738
  if (max < 0)
6739
      entry->c_max = -1;
6740
  else
6741
      entry->c_max = max * entry->exp_left->c_max;
6742
    } else {
6743
  entry->exp_left = left;
6744
  entry->exp_right = right;
6745
  if (type == XML_EXP_OR) {
6746
      if ((IS_NILLABLE(left)) || (IS_NILLABLE(right)))
6747
    entry->info |= XML_EXP_NILABLE;
6748
      if ((entry->exp_left->c_max == -1) ||
6749
          (entry->exp_right->c_max == -1))
6750
    entry->c_max = -1;
6751
      else if (entry->exp_left->c_max > entry->exp_right->c_max)
6752
          entry->c_max = entry->exp_left->c_max;
6753
      else
6754
          entry->c_max = entry->exp_right->c_max;
6755
  } else {
6756
      if ((IS_NILLABLE(left)) && (IS_NILLABLE(right)))
6757
    entry->info |= XML_EXP_NILABLE;
6758
      if ((entry->exp_left->c_max == -1) ||
6759
          (entry->exp_right->c_max == -1))
6760
    entry->c_max = -1;
6761
      else
6762
          entry->c_max = entry->exp_left->c_max + entry->exp_right->c_max;
6763
  }
6764
    }
6765
    entry->ref = 1;
6766
    if (ctxt->table[key] != NULL)
6767
        entry->next = ctxt->table[key];
6768
6769
    ctxt->table[key] = entry;
6770
    ctxt->nbElems++;
6771
6772
    return(entry);
6773
}
6774
6775
/**
6776
 * xmlExpFree:
6777
 * @ctxt: the expression context
6778
 * @exp: the expression
6779
 *
6780
 * Dereference the expression
6781
 */
6782
void
6783
xmlExpFree(xmlExpCtxtPtr ctxt, xmlExpNodePtr exp) {
6784
    if ((exp == NULL) || (exp == forbiddenExp) || (exp == emptyExp))
6785
        return;
6786
    exp->ref--;
6787
    if (exp->ref == 0) {
6788
        unsigned short key;
6789
6790
        /* Unlink it first from the hash table */
6791
  key = exp->key % ctxt->size;
6792
  if (ctxt->table[key] == exp) {
6793
      ctxt->table[key] = exp->next;
6794
  } else {
6795
      xmlExpNodePtr tmp;
6796
6797
      tmp = ctxt->table[key];
6798
      while (tmp != NULL) {
6799
          if (tmp->next == exp) {
6800
        tmp->next = exp->next;
6801
        break;
6802
    }
6803
          tmp = tmp->next;
6804
      }
6805
  }
6806
6807
        if ((exp->type == XML_EXP_SEQ) || (exp->type == XML_EXP_OR)) {
6808
      xmlExpFree(ctxt, exp->exp_left);
6809
      xmlExpFree(ctxt, exp->exp_right);
6810
  } else if (exp->type == XML_EXP_COUNT) {
6811
      xmlExpFree(ctxt, exp->exp_left);
6812
  }
6813
        xmlFree(exp);
6814
  ctxt->nb_nodes--;
6815
    }
6816
}
6817
6818
/**
6819
 * xmlExpRef:
6820
 * @exp: the expression
6821
 *
6822
 * Increase the reference count of the expression
6823
 */
6824
void
6825
xmlExpRef(xmlExpNodePtr exp) {
6826
    if (exp != NULL)
6827
        exp->ref++;
6828
}
6829
6830
/**
6831
 * xmlExpNewAtom:
6832
 * @ctxt: the expression context
6833
 * @name: the atom name
6834
 * @len: the atom name length in byte (or -1);
6835
 *
6836
 * Get the atom associated to this name from that context
6837
 *
6838
 * Returns the node or NULL in case of error
6839
 */
6840
xmlExpNodePtr
6841
xmlExpNewAtom(xmlExpCtxtPtr ctxt, const xmlChar *name, int len) {
6842
    if ((ctxt == NULL) || (name == NULL))
6843
        return(NULL);
6844
    name = xmlDictLookup(ctxt->dict, name, len);
6845
    if (name == NULL)
6846
        return(NULL);
6847
    return(xmlExpHashGetEntry(ctxt, XML_EXP_ATOM, NULL, NULL, name, 0, 0));
6848
}
6849
6850
/**
6851
 * xmlExpNewOr:
6852
 * @ctxt: the expression context
6853
 * @left: left expression
6854
 * @right: right expression
6855
 *
6856
 * Get the atom associated to the choice @left | @right
6857
 * Note that @left and @right are consumed in the operation, to keep
6858
 * an handle on them use xmlExpRef() and use xmlExpFree() to release them,
6859
 * this is true even in case of failure (unless ctxt == NULL).
6860
 *
6861
 * Returns the node or NULL in case of error
6862
 */
6863
xmlExpNodePtr
6864
xmlExpNewOr(xmlExpCtxtPtr ctxt, xmlExpNodePtr left, xmlExpNodePtr right) {
6865
    if (ctxt == NULL)
6866
        return(NULL);
6867
    if ((left == NULL) || (right == NULL)) {
6868
        xmlExpFree(ctxt, left);
6869
        xmlExpFree(ctxt, right);
6870
        return(NULL);
6871
    }
6872
    return(xmlExpHashGetEntry(ctxt, XML_EXP_OR, left, right, NULL, 0, 0));
6873
}
6874
6875
/**
6876
 * xmlExpNewSeq:
6877
 * @ctxt: the expression context
6878
 * @left: left expression
6879
 * @right: right expression
6880
 *
6881
 * Get the atom associated to the sequence @left , @right
6882
 * Note that @left and @right are consumed in the operation, to keep
6883
 * an handle on them use xmlExpRef() and use xmlExpFree() to release them,
6884
 * this is true even in case of failure (unless ctxt == NULL).
6885
 *
6886
 * Returns the node or NULL in case of error
6887
 */
6888
xmlExpNodePtr
6889
xmlExpNewSeq(xmlExpCtxtPtr ctxt, xmlExpNodePtr left, xmlExpNodePtr right) {
6890
    if (ctxt == NULL)
6891
        return(NULL);
6892
    if ((left == NULL) || (right == NULL)) {
6893
        xmlExpFree(ctxt, left);
6894
        xmlExpFree(ctxt, right);
6895
        return(NULL);
6896
    }
6897
    return(xmlExpHashGetEntry(ctxt, XML_EXP_SEQ, left, right, NULL, 0, 0));
6898
}
6899
6900
/**
6901
 * xmlExpNewRange:
6902
 * @ctxt: the expression context
6903
 * @subset: the expression to be repeated
6904
 * @min: the lower bound for the repetition
6905
 * @max: the upper bound for the repetition, -1 means infinite
6906
 *
6907
 * Get the atom associated to the range (@subset){@min, @max}
6908
 * Note that @subset is consumed in the operation, to keep
6909
 * an handle on it use xmlExpRef() and use xmlExpFree() to release it,
6910
 * this is true even in case of failure (unless ctxt == NULL).
6911
 *
6912
 * Returns the node or NULL in case of error
6913
 */
6914
xmlExpNodePtr
6915
xmlExpNewRange(xmlExpCtxtPtr ctxt, xmlExpNodePtr subset, int min, int max) {
6916
    if (ctxt == NULL)
6917
        return(NULL);
6918
    if ((subset == NULL) || (min < 0) || (max < -1) ||
6919
        ((max >= 0) && (min > max))) {
6920
  xmlExpFree(ctxt, subset);
6921
        return(NULL);
6922
    }
6923
    return(xmlExpHashGetEntry(ctxt, XML_EXP_COUNT, subset,
6924
                              NULL, NULL, min, max));
6925
}
6926
6927
/************************************************************************
6928
 *                  *
6929
 *    Public API for operations on expressions    *
6930
 *                  *
6931
 ************************************************************************/
6932
6933
static int
6934
xmlExpGetLanguageInt(xmlExpCtxtPtr ctxt, xmlExpNodePtr exp,
6935
                     const xmlChar**list, int len, int nb) {
6936
    int tmp, tmp2;
6937
tail:
6938
    switch (exp->type) {
6939
        case XML_EXP_EMPTY:
6940
      return(0);
6941
        case XML_EXP_ATOM:
6942
      for (tmp = 0;tmp < nb;tmp++)
6943
          if (list[tmp] == exp->exp_str)
6944
        return(0);
6945
            if (nb >= len)
6946
          return(-2);
6947
      list[nb] = exp->exp_str;
6948
      return(1);
6949
        case XML_EXP_COUNT:
6950
      exp = exp->exp_left;
6951
      goto tail;
6952
        case XML_EXP_SEQ:
6953
        case XML_EXP_OR:
6954
      tmp = xmlExpGetLanguageInt(ctxt, exp->exp_left, list, len, nb);
6955
      if (tmp < 0)
6956
          return(tmp);
6957
      tmp2 = xmlExpGetLanguageInt(ctxt, exp->exp_right, list, len,
6958
                                  nb + tmp);
6959
      if (tmp2 < 0)
6960
          return(tmp2);
6961
            return(tmp + tmp2);
6962
    }
6963
    return(-1);
6964
}
6965
6966
/**
6967
 * xmlExpGetLanguage:
6968
 * @ctxt: the expression context
6969
 * @exp: the expression
6970
 * @langList: where to store the tokens
6971
 * @len: the allocated length of @list
6972
 *
6973
 * Find all the strings used in @exp and store them in @list
6974
 *
6975
 * Returns the number of unique strings found, -1 in case of errors and
6976
 *         -2 if there is more than @len strings
6977
 */
6978
int
6979
xmlExpGetLanguage(xmlExpCtxtPtr ctxt, xmlExpNodePtr exp,
6980
                  const xmlChar**langList, int len) {
6981
    if ((ctxt == NULL) || (exp == NULL) || (langList == NULL) || (len <= 0))
6982
        return(-1);
6983
    return(xmlExpGetLanguageInt(ctxt, exp, langList, len, 0));
6984
}
6985
6986
static int
6987
xmlExpGetStartInt(xmlExpCtxtPtr ctxt, xmlExpNodePtr exp,
6988
                  const xmlChar**list, int len, int nb) {
6989
    int tmp, tmp2;
6990
tail:
6991
    switch (exp->type) {
6992
        case XML_EXP_FORBID:
6993
      return(0);
6994
        case XML_EXP_EMPTY:
6995
      return(0);
6996
        case XML_EXP_ATOM:
6997
      for (tmp = 0;tmp < nb;tmp++)
6998
          if (list[tmp] == exp->exp_str)
6999
        return(0);
7000
            if (nb >= len)
7001
          return(-2);
7002
      list[nb] = exp->exp_str;
7003
      return(1);
7004
        case XML_EXP_COUNT:
7005
      exp = exp->exp_left;
7006
      goto tail;
7007
        case XML_EXP_SEQ:
7008
      tmp = xmlExpGetStartInt(ctxt, exp->exp_left, list, len, nb);
7009
      if (tmp < 0)
7010
          return(tmp);
7011
      if (IS_NILLABLE(exp->exp_left)) {
7012
    tmp2 = xmlExpGetStartInt(ctxt, exp->exp_right, list, len,
7013
              nb + tmp);
7014
    if (tmp2 < 0)
7015
        return(tmp2);
7016
    tmp += tmp2;
7017
      }
7018
            return(tmp);
7019
        case XML_EXP_OR:
7020
      tmp = xmlExpGetStartInt(ctxt, exp->exp_left, list, len, nb);
7021
      if (tmp < 0)
7022
          return(tmp);
7023
      tmp2 = xmlExpGetStartInt(ctxt, exp->exp_right, list, len,
7024
                                  nb + tmp);
7025
      if (tmp2 < 0)
7026
          return(tmp2);
7027
            return(tmp + tmp2);
7028
    }
7029
    return(-1);
7030
}
7031
7032
/**
7033
 * xmlExpGetStart:
7034
 * @ctxt: the expression context
7035
 * @exp: the expression
7036
 * @tokList: where to store the tokens
7037
 * @len: the allocated length of @list
7038
 *
7039
 * Find all the strings that appears at the start of the languages
7040
 * accepted by @exp and store them in @list. E.g. for (a, b) | c
7041
 * it will return the list [a, c]
7042
 *
7043
 * Returns the number of unique strings found, -1 in case of errors and
7044
 *         -2 if there is more than @len strings
7045
 */
7046
int
7047
xmlExpGetStart(xmlExpCtxtPtr ctxt, xmlExpNodePtr exp,
7048
               const xmlChar**tokList, int len) {
7049
    if ((ctxt == NULL) || (exp == NULL) || (tokList == NULL) || (len <= 0))
7050
        return(-1);
7051
    return(xmlExpGetStartInt(ctxt, exp, tokList, len, 0));
7052
}
7053
7054
/**
7055
 * xmlExpIsNillable:
7056
 * @exp: the expression
7057
 *
7058
 * Finds if the expression is nillable, i.e. if it accepts the empty sequence
7059
 *
7060
 * Returns 1 if nillable, 0 if not and -1 in case of error
7061
 */
7062
int
7063
xmlExpIsNillable(xmlExpNodePtr exp) {
7064
    if (exp == NULL)
7065
        return(-1);
7066
    return(IS_NILLABLE(exp) != 0);
7067
}
7068
7069
static xmlExpNodePtr
7070
xmlExpStringDeriveInt(xmlExpCtxtPtr ctxt, xmlExpNodePtr exp, const xmlChar *str)
7071
{
7072
    xmlExpNodePtr ret;
7073
7074
    switch (exp->type) {
7075
  case XML_EXP_EMPTY:
7076
      return(forbiddenExp);
7077
  case XML_EXP_FORBID:
7078
      return(forbiddenExp);
7079
  case XML_EXP_ATOM:
7080
      if (exp->exp_str == str) {
7081
          ret = emptyExp;
7082
      } else {
7083
          /* TODO wildcards here */
7084
    ret = forbiddenExp;
7085
      }
7086
      return(ret);
7087
  case XML_EXP_OR: {
7088
      xmlExpNodePtr tmp;
7089
7090
      tmp = xmlExpStringDeriveInt(ctxt, exp->exp_left, str);
7091
      if (tmp == NULL) {
7092
    return(NULL);
7093
      }
7094
      ret = xmlExpStringDeriveInt(ctxt, exp->exp_right, str);
7095
      if (ret == NULL) {
7096
          xmlExpFree(ctxt, tmp);
7097
    return(NULL);
7098
      }
7099
            ret = xmlExpHashGetEntry(ctxt, XML_EXP_OR, tmp, ret,
7100
           NULL, 0, 0);
7101
      return(ret);
7102
  }
7103
  case XML_EXP_SEQ:
7104
      ret = xmlExpStringDeriveInt(ctxt, exp->exp_left, str);
7105
      if (ret == NULL) {
7106
          return(NULL);
7107
      } else if (ret == forbiddenExp) {
7108
          if (IS_NILLABLE(exp->exp_left)) {
7109
        ret = xmlExpStringDeriveInt(ctxt, exp->exp_right, str);
7110
    }
7111
      } else {
7112
          exp->exp_right->ref++;
7113
          ret = xmlExpHashGetEntry(ctxt, XML_EXP_SEQ, ret, exp->exp_right,
7114
                             NULL, 0, 0);
7115
      }
7116
      return(ret);
7117
  case XML_EXP_COUNT: {
7118
      int min, max;
7119
      xmlExpNodePtr tmp;
7120
7121
      if (exp->exp_max == 0)
7122
    return(forbiddenExp);
7123
      ret = xmlExpStringDeriveInt(ctxt, exp->exp_left, str);
7124
      if (ret == NULL)
7125
          return(NULL);
7126
      if (ret == forbiddenExp) {
7127
          return(ret);
7128
      }
7129
      if (exp->exp_max == 1)
7130
    return(ret);
7131
      if (exp->exp_max < 0) /* unbounded */
7132
    max = -1;
7133
      else
7134
    max = exp->exp_max - 1;
7135
      if (exp->exp_min > 0)
7136
    min = exp->exp_min - 1;
7137
      else
7138
    min = 0;
7139
      exp->exp_left->ref++;
7140
      tmp = xmlExpHashGetEntry(ctxt, XML_EXP_COUNT, exp->exp_left, NULL,
7141
             NULL, min, max);
7142
      if (ret == emptyExp) {
7143
          return(tmp);
7144
      }
7145
      return(xmlExpHashGetEntry(ctxt, XML_EXP_SEQ, ret, tmp,
7146
                                NULL, 0, 0));
7147
  }
7148
    }
7149
    return(NULL);
7150
}
7151
7152
/**
7153
 * xmlExpStringDerive:
7154
 * @ctxt: the expression context
7155
 * @exp: the expression
7156
 * @str: the string
7157
 * @len: the string len in bytes if available
7158
 *
7159
 * Do one step of Brzozowski derivation of the expression @exp with
7160
 * respect to the input string
7161
 *
7162
 * Returns the resulting expression or NULL in case of internal error
7163
 */
7164
xmlExpNodePtr
7165
xmlExpStringDerive(xmlExpCtxtPtr ctxt, xmlExpNodePtr exp,
7166
                   const xmlChar *str, int len) {
7167
    const xmlChar *input;
7168
7169
    if ((exp == NULL) || (ctxt == NULL) || (str == NULL)) {
7170
        return(NULL);
7171
    }
7172
    /*
7173
     * check the string is in the dictionary, if yes use an interned
7174
     * copy, otherwise we know it's not an acceptable input
7175
     */
7176
    input = xmlDictExists(ctxt->dict, str, len);
7177
    if (input == NULL) {
7178
        return(forbiddenExp);
7179
    }
7180
    return(xmlExpStringDeriveInt(ctxt, exp, input));
7181
}
7182
7183
static int
7184
xmlExpCheckCard(xmlExpNodePtr exp, xmlExpNodePtr sub) {
7185
    int ret = 1;
7186
7187
    if (sub->c_max == -1) {
7188
        if (exp->c_max != -1)
7189
      ret = 0;
7190
    } else if ((exp->c_max >= 0) && (exp->c_max < sub->c_max)) {
7191
        ret = 0;
7192
    }
7193
#if 0
7194
    if ((IS_NILLABLE(sub)) && (!IS_NILLABLE(exp)))
7195
        ret = 0;
7196
#endif
7197
    return(ret);
7198
}
7199
7200
static xmlExpNodePtr xmlExpExpDeriveInt(xmlExpCtxtPtr ctxt, xmlExpNodePtr exp,
7201
                                        xmlExpNodePtr sub);
7202
/**
7203
 * xmlExpDivide:
7204
 * @ctxt: the expressions context
7205
 * @exp: the englobing expression
7206
 * @sub: the subexpression
7207
 * @mult: the multiple expression
7208
 * @remain: the remain from the derivation of the multiple
7209
 *
7210
 * Check if exp is a multiple of sub, i.e. if there is a finite number n
7211
 * so that sub{n} subsume exp
7212
 *
7213
 * Returns the multiple value if successful, 0 if it is not a multiple
7214
 *         and -1 in case of internal error.
7215
 */
7216
7217
static int
7218
xmlExpDivide(xmlExpCtxtPtr ctxt, xmlExpNodePtr exp, xmlExpNodePtr sub,
7219
             xmlExpNodePtr *mult, xmlExpNodePtr *remain) {
7220
    int i;
7221
    xmlExpNodePtr tmp, tmp2;
7222
7223
    if (mult != NULL) *mult = NULL;
7224
    if (remain != NULL) *remain = NULL;
7225
    if (exp->c_max == -1) return(0);
7226
    if (IS_NILLABLE(exp) && (!IS_NILLABLE(sub))) return(0);
7227
7228
    for (i = 1;i <= exp->c_max;i++) {
7229
        sub->ref++;
7230
        tmp = xmlExpHashGetEntry(ctxt, XML_EXP_COUNT,
7231
         sub, NULL, NULL, i, i);
7232
  if (tmp == NULL) {
7233
      return(-1);
7234
  }
7235
  if (!xmlExpCheckCard(tmp, exp)) {
7236
      xmlExpFree(ctxt, tmp);
7237
      continue;
7238
  }
7239
  tmp2 = xmlExpExpDeriveInt(ctxt, tmp, exp);
7240
  if (tmp2 == NULL) {
7241
      xmlExpFree(ctxt, tmp);
7242
      return(-1);
7243
  }
7244
  if ((tmp2 != forbiddenExp) && (IS_NILLABLE(tmp2))) {
7245
      if (remain != NULL)
7246
          *remain = tmp2;
7247
      else
7248
          xmlExpFree(ctxt, tmp2);
7249
      if (mult != NULL)
7250
          *mult = tmp;
7251
      else
7252
          xmlExpFree(ctxt, tmp);
7253
      return(i);
7254
  }
7255
  xmlExpFree(ctxt, tmp);
7256
  xmlExpFree(ctxt, tmp2);
7257
    }
7258
    return(0);
7259
}
7260
7261
/**
7262
 * xmlExpExpDeriveInt:
7263
 * @ctxt: the expressions context
7264
 * @exp: the englobing expression
7265
 * @sub: the subexpression
7266
 *
7267
 * Try to do a step of Brzozowski derivation but at a higher level
7268
 * the input being a subexpression.
7269
 *
7270
 * Returns the resulting expression or NULL in case of internal error
7271
 */
7272
static xmlExpNodePtr
7273
xmlExpExpDeriveInt(xmlExpCtxtPtr ctxt, xmlExpNodePtr exp, xmlExpNodePtr sub) {
7274
    xmlExpNodePtr ret, tmp, tmp2, tmp3;
7275
    const xmlChar **tab;
7276
    int len, i;
7277
7278
    /*
7279
     * In case of equality and if the expression can only consume a finite
7280
     * amount, then the derivation is empty
7281
     */
7282
    if ((exp == sub) && (exp->c_max >= 0)) {
7283
        return(emptyExp);
7284
    }
7285
    /*
7286
     * decompose sub sequence first
7287
     */
7288
    if (sub->type == XML_EXP_EMPTY) {
7289
  exp->ref++;
7290
        return(exp);
7291
    }
7292
    if (sub->type == XML_EXP_SEQ) {
7293
        tmp = xmlExpExpDeriveInt(ctxt, exp, sub->exp_left);
7294
  if (tmp == NULL)
7295
      return(NULL);
7296
  if (tmp == forbiddenExp)
7297
      return(tmp);
7298
  ret = xmlExpExpDeriveInt(ctxt, tmp, sub->exp_right);
7299
  xmlExpFree(ctxt, tmp);
7300
  return(ret);
7301
    }
7302
    if (sub->type == XML_EXP_OR) {
7303
        tmp = xmlExpExpDeriveInt(ctxt, exp, sub->exp_left);
7304
  if (tmp == forbiddenExp)
7305
      return(tmp);
7306
  if (tmp == NULL)
7307
      return(NULL);
7308
  ret = xmlExpExpDeriveInt(ctxt, exp, sub->exp_right);
7309
  if ((ret == NULL) || (ret == forbiddenExp)) {
7310
      xmlExpFree(ctxt, tmp);
7311
      return(ret);
7312
  }
7313
  return(xmlExpHashGetEntry(ctxt, XML_EXP_OR, tmp, ret, NULL, 0, 0));
7314
    }
7315
    if (!xmlExpCheckCard(exp, sub)) {
7316
        return(forbiddenExp);
7317
    }
7318
    switch (exp->type) {
7319
        case XML_EXP_EMPTY:
7320
      if (sub == emptyExp)
7321
          return(emptyExp);
7322
      return(forbiddenExp);
7323
        case XML_EXP_FORBID:
7324
      return(forbiddenExp);
7325
        case XML_EXP_ATOM:
7326
      if (sub->type == XML_EXP_ATOM) {
7327
          /* TODO: handle wildcards */
7328
          if (exp->exp_str == sub->exp_str) {
7329
        return(emptyExp);
7330
                }
7331
          return(forbiddenExp);
7332
      }
7333
      if ((sub->type == XML_EXP_COUNT) &&
7334
          (sub->exp_max == 1) &&
7335
          (sub->exp_left->type == XML_EXP_ATOM)) {
7336
          /* TODO: handle wildcards */
7337
          if (exp->exp_str == sub->exp_left->exp_str) {
7338
        return(emptyExp);
7339
    }
7340
          return(forbiddenExp);
7341
      }
7342
      return(forbiddenExp);
7343
        case XML_EXP_SEQ:
7344
      /* try to get the sequence consumed only if possible */
7345
      if (xmlExpCheckCard(exp->exp_left, sub)) {
7346
    /* See if the sequence can be consumed directly */
7347
    ret = xmlExpExpDeriveInt(ctxt, exp->exp_left, sub);
7348
    if ((ret != forbiddenExp) && (ret != NULL)) {
7349
        /*
7350
         * TODO: assumption here that we are determinist
7351
         *       i.e. we won't get to a nillable exp left
7352
         *       subset which could be matched by the right
7353
         *       part too.
7354
         * e.g.: (a | b)+,(a | c) and 'a+,a'
7355
         */
7356
        exp->exp_right->ref++;
7357
        return(xmlExpHashGetEntry(ctxt, XML_EXP_SEQ, ret,
7358
                exp->exp_right, NULL, 0, 0));
7359
    }
7360
      }
7361
      /* Try instead to decompose */
7362
      if (sub->type == XML_EXP_COUNT) {
7363
    int min, max;
7364
7365
          ret = xmlExpExpDeriveInt(ctxt, exp->exp_left, sub->exp_left);
7366
    if (ret == NULL)
7367
        return(NULL);
7368
    if (ret != forbiddenExp) {
7369
        if (sub->exp_max < 0)
7370
            max = -1;
7371
              else
7372
            max = sub->exp_max -1;
7373
        if (sub->exp_min > 0)
7374
            min = sub->exp_min -1;
7375
        else
7376
            min = 0;
7377
        exp->exp_right->ref++;
7378
        tmp = xmlExpHashGetEntry(ctxt, XML_EXP_SEQ, ret,
7379
                                 exp->exp_right, NULL, 0, 0);
7380
        if (tmp == NULL)
7381
            return(NULL);
7382
7383
        sub->exp_left->ref++;
7384
        tmp2 = xmlExpHashGetEntry(ctxt, XML_EXP_COUNT,
7385
              sub->exp_left, NULL, NULL, min, max);
7386
        if (tmp2 == NULL) {
7387
            xmlExpFree(ctxt, tmp);
7388
      return(NULL);
7389
        }
7390
        ret = xmlExpExpDeriveInt(ctxt, tmp, tmp2);
7391
        xmlExpFree(ctxt, tmp);
7392
        xmlExpFree(ctxt, tmp2);
7393
        return(ret);
7394
    }
7395
      }
7396
      /* we made no progress on structured operations */
7397
      break;
7398
        case XML_EXP_OR:
7399
      ret = xmlExpExpDeriveInt(ctxt, exp->exp_left, sub);
7400
      if (ret == NULL)
7401
          return(NULL);
7402
      tmp = xmlExpExpDeriveInt(ctxt, exp->exp_right, sub);
7403
      if (tmp == NULL) {
7404
    xmlExpFree(ctxt, ret);
7405
          return(NULL);
7406
      }
7407
      return(xmlExpHashGetEntry(ctxt, XML_EXP_OR, ret, tmp, NULL, 0, 0));
7408
        case XML_EXP_COUNT: {
7409
      int min, max;
7410
7411
      if (sub->type == XML_EXP_COUNT) {
7412
          /*
7413
     * Try to see if the loop is completely subsumed
7414
     */
7415
          tmp = xmlExpExpDeriveInt(ctxt, exp->exp_left, sub->exp_left);
7416
    if (tmp == NULL)
7417
        return(NULL);
7418
    if (tmp == forbiddenExp) {
7419
        int mult;
7420
7421
        mult = xmlExpDivide(ctxt, sub->exp_left, exp->exp_left,
7422
                            NULL, &tmp);
7423
        if (mult <= 0) {
7424
                        return(forbiddenExp);
7425
        }
7426
        if (sub->exp_max == -1) {
7427
            max = -1;
7428
      if (exp->exp_max == -1) {
7429
          if (exp->exp_min <= sub->exp_min * mult)
7430
              min = 0;
7431
          else
7432
              min = exp->exp_min - sub->exp_min * mult;
7433
      } else {
7434
                            xmlExpFree(ctxt, tmp);
7435
          return(forbiddenExp);
7436
      }
7437
        } else {
7438
      if (exp->exp_max == -1) {
7439
          if (exp->exp_min > sub->exp_min * mult) {
7440
        max = -1;
7441
        min = exp->exp_min - sub->exp_min * mult;
7442
          } else {
7443
        max = -1;
7444
        min = 0;
7445
          }
7446
      } else {
7447
          if (exp->exp_max < sub->exp_max * mult) {
7448
        xmlExpFree(ctxt, tmp);
7449
        return(forbiddenExp);
7450
          }
7451
          if (sub->exp_max * mult > exp->exp_min)
7452
        min = 0;
7453
          else
7454
        min = exp->exp_min - sub->exp_max * mult;
7455
          max = exp->exp_max - sub->exp_max * mult;
7456
      }
7457
        }
7458
    } else if (!IS_NILLABLE(tmp)) {
7459
        /*
7460
         * TODO: loop here to try to grow if working on finite
7461
         *       blocks.
7462
         */
7463
        xmlExpFree(ctxt, tmp);
7464
        return(forbiddenExp);
7465
    } else if (sub->exp_max == -1) {
7466
        if (exp->exp_max == -1) {
7467
            if (exp->exp_min <= sub->exp_min) {
7468
                            max = -1;
7469
          min = 0;
7470
      } else {
7471
                            max = -1;
7472
          min = exp->exp_min - sub->exp_min;
7473
      }
7474
        } else if (exp->exp_min > sub->exp_min) {
7475
            xmlExpFree(ctxt, tmp);
7476
            return(forbiddenExp);
7477
        } else {
7478
      max = -1;
7479
      min = 0;
7480
        }
7481
    } else {
7482
        if (exp->exp_max == -1) {
7483
            if (exp->exp_min > sub->exp_min) {
7484
          max = -1;
7485
          min = exp->exp_min - sub->exp_min;
7486
      } else {
7487
          max = -1;
7488
          min = 0;
7489
      }
7490
        } else {
7491
            if (exp->exp_max < sub->exp_max) {
7492
          xmlExpFree(ctxt, tmp);
7493
          return(forbiddenExp);
7494
      }
7495
      if (sub->exp_max > exp->exp_min)
7496
          min = 0;
7497
      else
7498
          min = exp->exp_min - sub->exp_max;
7499
      max = exp->exp_max - sub->exp_max;
7500
        }
7501
    }
7502
    exp->exp_left->ref++;
7503
    tmp2 = xmlExpHashGetEntry(ctxt, XML_EXP_COUNT, exp->exp_left,
7504
                              NULL, NULL, min, max);
7505
    if (tmp2 == NULL) {
7506
        return(NULL);
7507
    }
7508
                ret = xmlExpHashGetEntry(ctxt, XML_EXP_SEQ, tmp, tmp2,
7509
                             NULL, 0, 0);
7510
    return(ret);
7511
      }
7512
      tmp = xmlExpExpDeriveInt(ctxt, exp->exp_left, sub);
7513
      if (tmp == NULL)
7514
    return(NULL);
7515
      if (tmp == forbiddenExp) {
7516
    return(forbiddenExp);
7517
      }
7518
      if (exp->exp_min > 0)
7519
    min = exp->exp_min - 1;
7520
      else
7521
    min = 0;
7522
      if (exp->exp_max < 0)
7523
    max = -1;
7524
      else
7525
    max = exp->exp_max - 1;
7526
7527
      exp->exp_left->ref++;
7528
      tmp2 = xmlExpHashGetEntry(ctxt, XML_EXP_COUNT, exp->exp_left,
7529
              NULL, NULL, min, max);
7530
      if (tmp2 == NULL)
7531
    return(NULL);
7532
      ret = xmlExpHashGetEntry(ctxt, XML_EXP_SEQ, tmp, tmp2,
7533
             NULL, 0, 0);
7534
      return(ret);
7535
  }
7536
    }
7537
7538
    if (IS_NILLABLE(sub)) {
7539
        if (!(IS_NILLABLE(exp)))
7540
      return(forbiddenExp);
7541
  else
7542
      ret = emptyExp;
7543
    } else
7544
  ret = NULL;
7545
    /*
7546
     * here the structured derivation made no progress so
7547
     * we use the default token based derivation to force one more step
7548
     */
7549
    if (ctxt->tabSize == 0)
7550
        ctxt->tabSize = 40;
7551
7552
    tab = (const xmlChar **) xmlMalloc(ctxt->tabSize *
7553
                                 sizeof(const xmlChar *));
7554
    if (tab == NULL) {
7555
  return(NULL);
7556
    }
7557
7558
    /*
7559
     * collect all the strings accepted by the subexpression on input
7560
     */
7561
    len = xmlExpGetStartInt(ctxt, sub, tab, ctxt->tabSize, 0);
7562
    while (len < 0) {
7563
        const xmlChar **temp;
7564
  temp = (const xmlChar **) xmlRealloc((xmlChar **) tab, ctxt->tabSize * 2 *
7565
                                       sizeof(const xmlChar *));
7566
  if (temp == NULL) {
7567
      xmlFree((xmlChar **) tab);
7568
      return(NULL);
7569
  }
7570
  tab = temp;
7571
  ctxt->tabSize *= 2;
7572
  len = xmlExpGetStartInt(ctxt, sub, tab, ctxt->tabSize, 0);
7573
    }
7574
    for (i = 0;i < len;i++) {
7575
        tmp = xmlExpStringDeriveInt(ctxt, exp, tab[i]);
7576
  if ((tmp == NULL) || (tmp == forbiddenExp)) {
7577
      xmlExpFree(ctxt, ret);
7578
      xmlFree((xmlChar **) tab);
7579
      return(tmp);
7580
  }
7581
  tmp2 = xmlExpStringDeriveInt(ctxt, sub, tab[i]);
7582
  if ((tmp2 == NULL) || (tmp2 == forbiddenExp)) {
7583
      xmlExpFree(ctxt, tmp);
7584
      xmlExpFree(ctxt, ret);
7585
      xmlFree((xmlChar **) tab);
7586
      return(tmp);
7587
  }
7588
  tmp3 = xmlExpExpDeriveInt(ctxt, tmp, tmp2);
7589
  xmlExpFree(ctxt, tmp);
7590
  xmlExpFree(ctxt, tmp2);
7591
7592
  if ((tmp3 == NULL) || (tmp3 == forbiddenExp)) {
7593
      xmlExpFree(ctxt, ret);
7594
      xmlFree((xmlChar **) tab);
7595
      return(tmp3);
7596
  }
7597
7598
  if (ret == NULL)
7599
      ret = tmp3;
7600
  else {
7601
      ret = xmlExpHashGetEntry(ctxt, XML_EXP_OR, ret, tmp3, NULL, 0, 0);
7602
      if (ret == NULL) {
7603
    xmlFree((xmlChar **) tab);
7604
          return(NULL);
7605
      }
7606
  }
7607
    }
7608
    xmlFree((xmlChar **) tab);
7609
    return(ret);
7610
}
7611
7612
/**
7613
 * xmlExpExpDerive:
7614
 * @ctxt: the expressions context
7615
 * @exp: the englobing expression
7616
 * @sub: the subexpression
7617
 *
7618
 * Evaluates the expression resulting from @exp consuming a sub expression @sub
7619
 * Based on algebraic derivation and sometimes direct Brzozowski derivation
7620
 * it usually takes less than linear time and can handle expressions generating
7621
 * infinite languages.
7622
 *
7623
 * Returns the resulting expression or NULL in case of internal error, the
7624
 *         result must be freed
7625
 */
7626
xmlExpNodePtr
7627
xmlExpExpDerive(xmlExpCtxtPtr ctxt, xmlExpNodePtr exp, xmlExpNodePtr sub) {
7628
    if ((exp == NULL) || (ctxt == NULL) || (sub == NULL))
7629
        return(NULL);
7630
7631
    /*
7632
     * O(1) speedups
7633
     */
7634
    if (IS_NILLABLE(sub) && (!IS_NILLABLE(exp))) {
7635
        return(forbiddenExp);
7636
    }
7637
    if (xmlExpCheckCard(exp, sub) == 0) {
7638
        return(forbiddenExp);
7639
    }
7640
    return(xmlExpExpDeriveInt(ctxt, exp, sub));
7641
}
7642
7643
/**
7644
 * xmlExpSubsume:
7645
 * @ctxt: the expressions context
7646
 * @exp: the englobing expression
7647
 * @sub: the subexpression
7648
 *
7649
 * Check whether @exp accepts all the languages accepted by @sub
7650
 * the input being a subexpression.
7651
 *
7652
 * Returns 1 if true 0 if false and -1 in case of failure.
7653
 */
7654
int
7655
xmlExpSubsume(xmlExpCtxtPtr ctxt, xmlExpNodePtr exp, xmlExpNodePtr sub) {
7656
    xmlExpNodePtr tmp;
7657
7658
    if ((exp == NULL) || (ctxt == NULL) || (sub == NULL))
7659
        return(-1);
7660
7661
    /*
7662
     * TODO: speedup by checking the language of sub is a subset of the
7663
     *       language of exp
7664
     */
7665
    /*
7666
     * O(1) speedups
7667
     */
7668
    if (IS_NILLABLE(sub) && (!IS_NILLABLE(exp))) {
7669
        return(0);
7670
    }
7671
    if (xmlExpCheckCard(exp, sub) == 0) {
7672
        return(0);
7673
    }
7674
    tmp = xmlExpExpDeriveInt(ctxt, exp, sub);
7675
    if (tmp == NULL)
7676
        return(-1);
7677
    if (tmp == forbiddenExp)
7678
  return(0);
7679
    if (tmp == emptyExp)
7680
  return(1);
7681
    if ((tmp != NULL) && (IS_NILLABLE(tmp))) {
7682
        xmlExpFree(ctxt, tmp);
7683
        return(1);
7684
    }
7685
    xmlExpFree(ctxt, tmp);
7686
    return(0);
7687
}
7688
7689
/************************************************************************
7690
 *                  *
7691
 *      Parsing expression        *
7692
 *                  *
7693
 ************************************************************************/
7694
7695
static xmlExpNodePtr xmlExpParseExpr(xmlExpCtxtPtr ctxt);
7696
7697
#undef CUR
7698
#define CUR (*ctxt->cur)
7699
#undef NEXT
7700
#define NEXT ctxt->cur++;
7701
#undef IS_BLANK
7702
#define IS_BLANK(c) ((c == ' ') || (c == '\n') || (c == '\r') || (c == '\t'))
7703
#define SKIP_BLANKS while (IS_BLANK(*ctxt->cur)) ctxt->cur++;
7704
7705
static int
7706
xmlExpParseNumber(xmlExpCtxtPtr ctxt) {
7707
    int ret = 0;
7708
7709
    SKIP_BLANKS
7710
    if (CUR == '*') {
7711
  NEXT
7712
  return(-1);
7713
    }
7714
    if ((CUR < '0') || (CUR > '9'))
7715
        return(-1);
7716
    while ((CUR >= '0') && (CUR <= '9')) {
7717
        ret = ret * 10 + (CUR - '0');
7718
  NEXT
7719
    }
7720
    return(ret);
7721
}
7722
7723
static xmlExpNodePtr
7724
xmlExpParseOr(xmlExpCtxtPtr ctxt) {
7725
    const char *base;
7726
    xmlExpNodePtr ret;
7727
    const xmlChar *val;
7728
7729
    SKIP_BLANKS
7730
    base = ctxt->cur;
7731
    if (*ctxt->cur == '(') {
7732
        NEXT
7733
  ret = xmlExpParseExpr(ctxt);
7734
  SKIP_BLANKS
7735
  if (*ctxt->cur != ')') {
7736
      fprintf(stderr, "unbalanced '(' : %s\n", base);
7737
      xmlExpFree(ctxt, ret);
7738
      return(NULL);
7739
  }
7740
  NEXT;
7741
  SKIP_BLANKS
7742
  goto parse_quantifier;
7743
    }
7744
    while ((CUR != 0) && (!(IS_BLANK(CUR))) && (CUR != '(') &&
7745
           (CUR != ')') && (CUR != '|') && (CUR != ',') && (CUR != '{') &&
7746
     (CUR != '*') && (CUR != '+') && (CUR != '?') && (CUR != '}'))
7747
  NEXT;
7748
    val = xmlDictLookup(ctxt->dict, BAD_CAST base, ctxt->cur - base);
7749
    if (val == NULL)
7750
        return(NULL);
7751
    ret = xmlExpHashGetEntry(ctxt, XML_EXP_ATOM, NULL, NULL, val, 0, 0);
7752
    if (ret == NULL)
7753
        return(NULL);
7754
    SKIP_BLANKS
7755
parse_quantifier:
7756
    if (CUR == '{') {
7757
        int min, max;
7758
7759
        NEXT
7760
  min = xmlExpParseNumber(ctxt);
7761
  if (min < 0) {
7762
      xmlExpFree(ctxt, ret);
7763
      return(NULL);
7764
  }
7765
  SKIP_BLANKS
7766
  if (CUR == ',') {
7767
      NEXT
7768
      max = xmlExpParseNumber(ctxt);
7769
      SKIP_BLANKS
7770
  } else
7771
      max = min;
7772
  if (CUR != '}') {
7773
      xmlExpFree(ctxt, ret);
7774
      return(NULL);
7775
  }
7776
        NEXT
7777
  ret = xmlExpHashGetEntry(ctxt, XML_EXP_COUNT, ret, NULL, NULL,
7778
                           min, max);
7779
  SKIP_BLANKS
7780
    } else if (CUR == '?') {
7781
        NEXT
7782
  ret = xmlExpHashGetEntry(ctxt, XML_EXP_COUNT, ret, NULL, NULL,
7783
                           0, 1);
7784
  SKIP_BLANKS
7785
    } else if (CUR == '+') {
7786
        NEXT
7787
  ret = xmlExpHashGetEntry(ctxt, XML_EXP_COUNT, ret, NULL, NULL,
7788
                           1, -1);
7789
  SKIP_BLANKS
7790
    } else if (CUR == '*') {
7791
        NEXT
7792
  ret = xmlExpHashGetEntry(ctxt, XML_EXP_COUNT, ret, NULL, NULL,
7793
                           0, -1);
7794
  SKIP_BLANKS
7795
    }
7796
    return(ret);
7797
}
7798
7799
7800
static xmlExpNodePtr
7801
xmlExpParseSeq(xmlExpCtxtPtr ctxt) {
7802
    xmlExpNodePtr ret, right;
7803
7804
    ret = xmlExpParseOr(ctxt);
7805
    SKIP_BLANKS
7806
    while (CUR == '|') {
7807
        NEXT
7808
  right = xmlExpParseOr(ctxt);
7809
  if (right == NULL) {
7810
      xmlExpFree(ctxt, ret);
7811
      return(NULL);
7812
  }
7813
  ret = xmlExpHashGetEntry(ctxt, XML_EXP_OR, ret, right, NULL, 0, 0);
7814
  if (ret == NULL)
7815
      return(NULL);
7816
    }
7817
    return(ret);
7818
}
7819
7820
static xmlExpNodePtr
7821
xmlExpParseExpr(xmlExpCtxtPtr ctxt) {
7822
    xmlExpNodePtr ret, right;
7823
7824
    ret = xmlExpParseSeq(ctxt);
7825
    SKIP_BLANKS
7826
    while (CUR == ',') {
7827
        NEXT
7828
  right = xmlExpParseSeq(ctxt);
7829
  if (right == NULL) {
7830
      xmlExpFree(ctxt, ret);
7831
      return(NULL);
7832
  }
7833
  ret = xmlExpHashGetEntry(ctxt, XML_EXP_SEQ, ret, right, NULL, 0, 0);
7834
  if (ret == NULL)
7835
      return(NULL);
7836
    }
7837
    return(ret);
7838
}
7839
7840
/**
7841
 * xmlExpParse:
7842
 * @ctxt: the expressions context
7843
 * @expr: the 0 terminated string
7844
 *
7845
 * Minimal parser for regexps, it understand the following constructs
7846
 *  - string terminals
7847
 *  - choice operator |
7848
 *  - sequence operator ,
7849
 *  - subexpressions (...)
7850
 *  - usual cardinality operators + * and ?
7851
 *  - finite sequences  { min, max }
7852
 *  - infinite sequences { min, * }
7853
 * There is minimal checkings made especially no checking on strings values
7854
 *
7855
 * Returns a new expression or NULL in case of failure
7856
 */
7857
xmlExpNodePtr
7858
xmlExpParse(xmlExpCtxtPtr ctxt, const char *expr) {
7859
    xmlExpNodePtr ret;
7860
7861
    ctxt->expr = expr;
7862
    ctxt->cur = expr;
7863
7864
    ret = xmlExpParseExpr(ctxt);
7865
    SKIP_BLANKS
7866
    if (*ctxt->cur != 0) {
7867
        xmlExpFree(ctxt, ret);
7868
        return(NULL);
7869
    }
7870
    return(ret);
7871
}
7872
7873
static void
7874
xmlExpDumpInt(xmlBufferPtr buf, xmlExpNodePtr expr, int glob) {
7875
    xmlExpNodePtr c;
7876
7877
    if (expr == NULL) return;
7878
    if (glob) xmlBufferWriteChar(buf, "(");
7879
    switch (expr->type) {
7880
        case XML_EXP_EMPTY:
7881
      xmlBufferWriteChar(buf, "empty");
7882
      break;
7883
        case XML_EXP_FORBID:
7884
      xmlBufferWriteChar(buf, "forbidden");
7885
      break;
7886
        case XML_EXP_ATOM:
7887
      xmlBufferWriteCHAR(buf, expr->exp_str);
7888
      break;
7889
        case XML_EXP_SEQ:
7890
      c = expr->exp_left;
7891
      if ((c->type == XML_EXP_SEQ) || (c->type == XML_EXP_OR))
7892
          xmlExpDumpInt(buf, c, 1);
7893
      else
7894
          xmlExpDumpInt(buf, c, 0);
7895
      xmlBufferWriteChar(buf, " , ");
7896
      c = expr->exp_right;
7897
      if ((c->type == XML_EXP_SEQ) || (c->type == XML_EXP_OR))
7898
          xmlExpDumpInt(buf, c, 1);
7899
      else
7900
          xmlExpDumpInt(buf, c, 0);
7901
            break;
7902
        case XML_EXP_OR:
7903
      c = expr->exp_left;
7904
      if ((c->type == XML_EXP_SEQ) || (c->type == XML_EXP_OR))
7905
          xmlExpDumpInt(buf, c, 1);
7906
      else
7907
          xmlExpDumpInt(buf, c, 0);
7908
      xmlBufferWriteChar(buf, " | ");
7909
      c = expr->exp_right;
7910
      if ((c->type == XML_EXP_SEQ) || (c->type == XML_EXP_OR))
7911
          xmlExpDumpInt(buf, c, 1);
7912
      else
7913
          xmlExpDumpInt(buf, c, 0);
7914
            break;
7915
        case XML_EXP_COUNT: {
7916
      char rep[40];
7917
7918
      c = expr->exp_left;
7919
      if ((c->type == XML_EXP_SEQ) || (c->type == XML_EXP_OR))
7920
          xmlExpDumpInt(buf, c, 1);
7921
      else
7922
          xmlExpDumpInt(buf, c, 0);
7923
      if ((expr->exp_min == 0) && (expr->exp_max == 1)) {
7924
    rep[0] = '?';
7925
    rep[1] = 0;
7926
      } else if ((expr->exp_min == 0) && (expr->exp_max == -1)) {
7927
    rep[0] = '*';
7928
    rep[1] = 0;
7929
      } else if ((expr->exp_min == 1) && (expr->exp_max == -1)) {
7930
    rep[0] = '+';
7931
    rep[1] = 0;
7932
      } else if (expr->exp_max == expr->exp_min) {
7933
          snprintf(rep, 39, "{%d}", expr->exp_min);
7934
      } else if (expr->exp_max < 0) {
7935
          snprintf(rep, 39, "{%d,inf}", expr->exp_min);
7936
      } else {
7937
          snprintf(rep, 39, "{%d,%d}", expr->exp_min, expr->exp_max);
7938
      }
7939
      rep[39] = 0;
7940
      xmlBufferWriteChar(buf, rep);
7941
      break;
7942
  }
7943
  default:
7944
      fprintf(stderr, "Error in tree\n");
7945
    }
7946
    if (glob)
7947
        xmlBufferWriteChar(buf, ")");
7948
}
7949
/**
7950
 * xmlExpDump:
7951
 * @buf:  a buffer to receive the output
7952
 * @expr:  the compiled expression
7953
 *
7954
 * Serialize the expression as compiled to the buffer
7955
 */
7956
void
7957
xmlExpDump(xmlBufferPtr buf, xmlExpNodePtr expr) {
7958
    if ((buf == NULL) || (expr == NULL))
7959
        return;
7960
    xmlExpDumpInt(buf, expr, 0);
7961
}
7962
7963
/**
7964
 * xmlExpMaxToken:
7965
 * @expr: a compiled expression
7966
 *
7967
 * Indicate the maximum number of input a expression can accept
7968
 *
7969
 * Returns the maximum length or -1 in case of error
7970
 */
7971
int
7972
xmlExpMaxToken(xmlExpNodePtr expr) {
7973
    if (expr == NULL)
7974
        return(-1);
7975
    return(expr->c_max);
7976
}
7977
7978
/**
7979
 * xmlExpCtxtNbNodes:
7980
 * @ctxt: an expression context
7981
 *
7982
 * Debugging facility provides the number of allocated nodes at a that point
7983
 *
7984
 * Returns the number of nodes in use or -1 in case of error
7985
 */
7986
int
7987
xmlExpCtxtNbNodes(xmlExpCtxtPtr ctxt) {
7988
    if (ctxt == NULL)
7989
        return(-1);
7990
    return(ctxt->nb_nodes);
7991
}
7992
7993
/**
7994
 * xmlExpCtxtNbCons:
7995
 * @ctxt: an expression context
7996
 *
7997
 * Debugging facility provides the number of allocated nodes over lifetime
7998
 *
7999
 * Returns the number of nodes ever allocated or -1 in case of error
8000
 */
8001
int
8002
xmlExpCtxtNbCons(xmlExpCtxtPtr ctxt) {
8003
    if (ctxt == NULL)
8004
        return(-1);
8005
    return(ctxt->nb_cons);
8006
}
8007
8008
#endif /* LIBXML_EXPR_ENABLED */
8009
8010
#endif /* LIBXML_REGEXP_ENABLED */