Coverage Report

Created: 2023-11-19 07:09

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