-
Notifications
You must be signed in to change notification settings - Fork 366
/
xmlregexp.c
7963 lines (7463 loc) · 210 KB
/
xmlregexp.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* regexp.c: generic and extensible Regular Expression engine
*
* Basically designed with the purpose of compiling regexps for
* the variety of validation/schemas mechanisms now available in
* XML related specifications these include:
* - XML-1.0 DTD validation
* - XML Schemas structure part 1
* - XML Schemas Datatypes part 2 especially Appendix F
* - RELAX-NG/TREX i.e. the counter proposal
*
* See Copyright for the status of this software.
*
* Daniel Veillard <[email protected]>
*/
#define IN_LIBXML
#include "libxml.h"
#ifdef LIBXML_REGEXP_ENABLED
#include <stdio.h>
#include <string.h>
#include <limits.h>
#include <libxml/tree.h>
#include <libxml/parserInternals.h>
#include <libxml/xmlregexp.h>
#include <libxml/xmlautomata.h>
#include <libxml/xmlunicode.h>
#include "private/error.h"
#include "private/regexp.h"
#ifndef SIZE_MAX
#define SIZE_MAX ((size_t) -1)
#endif
#define MAX_PUSH 10000000
#ifdef ERROR
#undef ERROR
#endif
#define ERROR(str) \
ctxt->error = XML_REGEXP_COMPILE_ERROR; \
xmlRegexpErrCompile(ctxt, str);
#define NEXT ctxt->cur++
#define CUR (*(ctxt->cur))
#define NXT(index) (ctxt->cur[index])
#define NEXTL(l) ctxt->cur += l;
#define XML_REG_STRING_SEPARATOR '|'
/*
* Need PREV to check on a '-' within a Character Group. May only be used
* when it's guaranteed that cur is not at the beginning of ctxt->string!
*/
#define PREV (ctxt->cur[-1])
/************************************************************************
* *
* Datatypes and structures *
* *
************************************************************************/
/*
* Note: the order of the enums below is significant, do not shuffle
*/
typedef enum {
XML_REGEXP_EPSILON = 1,
XML_REGEXP_CHARVAL,
XML_REGEXP_RANGES,
XML_REGEXP_SUBREG, /* used for () sub regexps */
XML_REGEXP_STRING,
XML_REGEXP_ANYCHAR, /* . */
XML_REGEXP_ANYSPACE, /* \s */
XML_REGEXP_NOTSPACE, /* \S */
XML_REGEXP_INITNAME, /* \l */
XML_REGEXP_NOTINITNAME, /* \L */
XML_REGEXP_NAMECHAR, /* \c */
XML_REGEXP_NOTNAMECHAR, /* \C */
XML_REGEXP_DECIMAL, /* \d */
XML_REGEXP_NOTDECIMAL, /* \D */
XML_REGEXP_REALCHAR, /* \w */
XML_REGEXP_NOTREALCHAR, /* \W */
XML_REGEXP_LETTER = 100,
XML_REGEXP_LETTER_UPPERCASE,
XML_REGEXP_LETTER_LOWERCASE,
XML_REGEXP_LETTER_TITLECASE,
XML_REGEXP_LETTER_MODIFIER,
XML_REGEXP_LETTER_OTHERS,
XML_REGEXP_MARK,
XML_REGEXP_MARK_NONSPACING,
XML_REGEXP_MARK_SPACECOMBINING,
XML_REGEXP_MARK_ENCLOSING,
XML_REGEXP_NUMBER,
XML_REGEXP_NUMBER_DECIMAL,
XML_REGEXP_NUMBER_LETTER,
XML_REGEXP_NUMBER_OTHERS,
XML_REGEXP_PUNCT,
XML_REGEXP_PUNCT_CONNECTOR,
XML_REGEXP_PUNCT_DASH,
XML_REGEXP_PUNCT_OPEN,
XML_REGEXP_PUNCT_CLOSE,
XML_REGEXP_PUNCT_INITQUOTE,
XML_REGEXP_PUNCT_FINQUOTE,
XML_REGEXP_PUNCT_OTHERS,
XML_REGEXP_SEPAR,
XML_REGEXP_SEPAR_SPACE,
XML_REGEXP_SEPAR_LINE,
XML_REGEXP_SEPAR_PARA,
XML_REGEXP_SYMBOL,
XML_REGEXP_SYMBOL_MATH,
XML_REGEXP_SYMBOL_CURRENCY,
XML_REGEXP_SYMBOL_MODIFIER,
XML_REGEXP_SYMBOL_OTHERS,
XML_REGEXP_OTHER,
XML_REGEXP_OTHER_CONTROL,
XML_REGEXP_OTHER_FORMAT,
XML_REGEXP_OTHER_PRIVATE,
XML_REGEXP_OTHER_NA,
XML_REGEXP_BLOCK_NAME
} xmlRegAtomType;
typedef enum {
XML_REGEXP_QUANT_EPSILON = 1,
XML_REGEXP_QUANT_ONCE,
XML_REGEXP_QUANT_OPT,
XML_REGEXP_QUANT_MULT,
XML_REGEXP_QUANT_PLUS,
XML_REGEXP_QUANT_ONCEONLY,
XML_REGEXP_QUANT_ALL,
XML_REGEXP_QUANT_RANGE
} xmlRegQuantType;
typedef enum {
XML_REGEXP_START_STATE = 1,
XML_REGEXP_FINAL_STATE,
XML_REGEXP_TRANS_STATE,
XML_REGEXP_SINK_STATE,
XML_REGEXP_UNREACH_STATE
} xmlRegStateType;
typedef enum {
XML_REGEXP_MARK_NORMAL = 0,
XML_REGEXP_MARK_START,
XML_REGEXP_MARK_VISITED
} xmlRegMarkedType;
typedef struct _xmlRegRange xmlRegRange;
typedef xmlRegRange *xmlRegRangePtr;
struct _xmlRegRange {
int neg; /* 0 normal, 1 not, 2 exclude */
xmlRegAtomType type;
int start;
int end;
xmlChar *blockName;
};
typedef struct _xmlRegAtom xmlRegAtom;
typedef xmlRegAtom *xmlRegAtomPtr;
typedef struct _xmlAutomataState xmlRegState;
typedef xmlRegState *xmlRegStatePtr;
struct _xmlRegAtom {
int no;
xmlRegAtomType type;
xmlRegQuantType quant;
int min;
int max;
void *valuep;
void *valuep2;
int neg;
int codepoint;
xmlRegStatePtr start;
xmlRegStatePtr start0;
xmlRegStatePtr stop;
int maxRanges;
int nbRanges;
xmlRegRangePtr *ranges;
void *data;
};
typedef struct _xmlRegCounter xmlRegCounter;
typedef xmlRegCounter *xmlRegCounterPtr;
struct _xmlRegCounter {
int min;
int max;
};
typedef struct _xmlRegTrans xmlRegTrans;
typedef xmlRegTrans *xmlRegTransPtr;
struct _xmlRegTrans {
xmlRegAtomPtr atom;
int to;
int counter;
int count;
int nd;
};
struct _xmlAutomataState {
xmlRegStateType type;
xmlRegMarkedType mark;
xmlRegMarkedType markd;
xmlRegMarkedType reached;
int no;
int maxTrans;
int nbTrans;
xmlRegTrans *trans;
/* knowing states pointing to us can speed things up */
int maxTransTo;
int nbTransTo;
int *transTo;
};
typedef struct _xmlAutomata xmlRegParserCtxt;
typedef xmlRegParserCtxt *xmlRegParserCtxtPtr;
#define AM_AUTOMATA_RNG 1
struct _xmlAutomata {
xmlChar *string;
xmlChar *cur;
int error;
int neg;
xmlRegStatePtr start;
xmlRegStatePtr end;
xmlRegStatePtr state;
xmlRegAtomPtr atom;
int maxAtoms;
int nbAtoms;
xmlRegAtomPtr *atoms;
int maxStates;
int nbStates;
xmlRegStatePtr *states;
int maxCounters;
int nbCounters;
xmlRegCounter *counters;
int determinist;
int negs;
int flags;
int depth;
};
struct _xmlRegexp {
xmlChar *string;
int nbStates;
xmlRegStatePtr *states;
int nbAtoms;
xmlRegAtomPtr *atoms;
int nbCounters;
xmlRegCounter *counters;
int determinist;
int flags;
/*
* That's the compact form for determinists automatas
*/
int nbstates;
int *compact;
void **transdata;
int nbstrings;
xmlChar **stringMap;
};
typedef struct _xmlRegExecRollback xmlRegExecRollback;
typedef xmlRegExecRollback *xmlRegExecRollbackPtr;
struct _xmlRegExecRollback {
xmlRegStatePtr state;/* the current state */
int index; /* the index in the input stack */
int nextbranch; /* the next transition to explore in that state */
int *counts; /* save the automata state if it has some */
};
typedef struct _xmlRegInputToken xmlRegInputToken;
typedef xmlRegInputToken *xmlRegInputTokenPtr;
struct _xmlRegInputToken {
xmlChar *value;
void *data;
};
struct _xmlRegExecCtxt {
int status; /* execution status != 0 indicate an error */
int determinist; /* did we find an indeterministic behaviour */
xmlRegexpPtr comp; /* the compiled regexp */
xmlRegExecCallbacks callback;
void *data;
xmlRegStatePtr state;/* the current state */
int transno; /* the current transition on that state */
int transcount; /* the number of chars in char counted transitions */
/*
* A stack of rollback states
*/
int maxRollbacks;
int nbRollbacks;
xmlRegExecRollback *rollbacks;
/*
* The state of the automata if any
*/
int *counts;
/*
* The input stack
*/
int inputStackMax;
int inputStackNr;
int index;
int *charStack;
const xmlChar *inputString; /* when operating on characters */
xmlRegInputTokenPtr inputStack;/* when operating on strings */
/*
* error handling
*/
int errStateNo; /* the error state number */
xmlRegStatePtr errState; /* the error state */
xmlChar *errString; /* the string raising the error */
int *errCounts; /* counters at the error state */
int nbPush;
};
#define REGEXP_ALL_COUNTER 0x123456
#define REGEXP_ALL_LAX_COUNTER 0x123457
static void xmlFAParseRegExp(xmlRegParserCtxtPtr ctxt, int top);
static void xmlRegFreeState(xmlRegStatePtr state);
static void xmlRegFreeAtom(xmlRegAtomPtr atom);
static int xmlRegStrEqualWildcard(const xmlChar *expStr, const xmlChar *valStr);
static int xmlRegCheckCharacter(xmlRegAtomPtr atom, int codepoint);
static int xmlRegCheckCharacterRange(xmlRegAtomType type, int codepoint,
int neg, int start, int end, const xmlChar *blockName);
/************************************************************************
* *
* Regexp memory error handler *
* *
************************************************************************/
/**
* xmlRegexpErrMemory:
* @extra: extra information
*
* Handle an out of memory condition
*/
static void
xmlRegexpErrMemory(xmlRegParserCtxtPtr ctxt)
{
if (ctxt != NULL)
ctxt->error = XML_ERR_NO_MEMORY;
xmlRaiseMemoryError(NULL, NULL, NULL, XML_FROM_REGEXP, NULL);
}
/**
* xmlRegexpErrCompile:
* @extra: extra information
*
* Handle a compilation failure
*/
static void
xmlRegexpErrCompile(xmlRegParserCtxtPtr ctxt, const char *extra)
{
const char *regexp = NULL;
int idx = 0;
int res;
if (ctxt != NULL) {
regexp = (const char *) ctxt->string;
idx = ctxt->cur - ctxt->string;
ctxt->error = XML_REGEXP_COMPILE_ERROR;
}
res = xmlRaiseError(NULL, NULL, NULL, NULL, NULL, XML_FROM_REGEXP,
XML_REGEXP_COMPILE_ERROR, XML_ERR_FATAL,
NULL, 0, extra, regexp, NULL, idx, 0,
"failed to compile: %s\n", extra);
if (res < 0)
xmlRegexpErrMemory(ctxt);
}
/************************************************************************
* *
* Allocation/Deallocation *
* *
************************************************************************/
static int xmlFAComputesDeterminism(xmlRegParserCtxtPtr ctxt);
/**
* xmlRegCalloc2:
* @dim1: size of first dimension
* @dim2: size of second dimension
* @elemSize: size of element
*
* Allocate a two-dimensional array and set all elements to zero.
*
* Returns the new array or NULL in case of error.
*/
static void*
xmlRegCalloc2(size_t dim1, size_t dim2, size_t elemSize) {
size_t totalSize;
void *ret;
/* Check for overflow */
if ((dim2 == 0) || (elemSize == 0) ||
(dim1 > SIZE_MAX / dim2 / elemSize))
return (NULL);
totalSize = dim1 * dim2 * elemSize;
ret = xmlMalloc(totalSize);
if (ret != NULL)
memset(ret, 0, totalSize);
return (ret);
}
/**
* xmlRegEpxFromParse:
* @ctxt: the parser context used to build it
*
* Allocate a new regexp and fill it with the result from the parser
*
* Returns the new regexp or NULL in case of error
*/
static xmlRegexpPtr
xmlRegEpxFromParse(xmlRegParserCtxtPtr ctxt) {
xmlRegexpPtr ret;
ret = (xmlRegexpPtr) xmlMalloc(sizeof(xmlRegexp));
if (ret == NULL) {
xmlRegexpErrMemory(ctxt);
return(NULL);
}
memset(ret, 0, sizeof(xmlRegexp));
ret->string = ctxt->string;
ret->nbStates = ctxt->nbStates;
ret->states = ctxt->states;
ret->nbAtoms = ctxt->nbAtoms;
ret->atoms = ctxt->atoms;
ret->nbCounters = ctxt->nbCounters;
ret->counters = ctxt->counters;
ret->determinist = ctxt->determinist;
ret->flags = ctxt->flags;
if (ret->determinist == -1) {
if (xmlRegexpIsDeterminist(ret) < 0) {
xmlRegexpErrMemory(ctxt);
xmlFree(ret);
return(NULL);
}
}
if ((ret->determinist != 0) &&
(ret->nbCounters == 0) &&
(ctxt->negs == 0) &&
(ret->atoms != NULL) &&
(ret->atoms[0] != NULL) &&
(ret->atoms[0]->type == XML_REGEXP_STRING)) {
int i, j, nbstates = 0, nbatoms = 0;
int *stateRemap;
int *stringRemap;
int *transitions;
void **transdata;
xmlChar **stringMap;
xmlChar *value;
/*
* Switch to a compact representation
* 1/ counting the effective number of states left
* 2/ counting the unique number of atoms, and check that
* they are all of the string type
* 3/ build a table state x atom for the transitions
*/
stateRemap = xmlMalloc(ret->nbStates * sizeof(int));
if (stateRemap == NULL) {
xmlRegexpErrMemory(ctxt);
xmlFree(ret);
return(NULL);
}
for (i = 0;i < ret->nbStates;i++) {
if (ret->states[i] != NULL) {
stateRemap[i] = nbstates;
nbstates++;
} else {
stateRemap[i] = -1;
}
}
stringMap = xmlMalloc(ret->nbAtoms * sizeof(char *));
if (stringMap == NULL) {
xmlRegexpErrMemory(ctxt);
xmlFree(stateRemap);
xmlFree(ret);
return(NULL);
}
stringRemap = xmlMalloc(ret->nbAtoms * sizeof(int));
if (stringRemap == NULL) {
xmlRegexpErrMemory(ctxt);
xmlFree(stringMap);
xmlFree(stateRemap);
xmlFree(ret);
return(NULL);
}
for (i = 0;i < ret->nbAtoms;i++) {
if ((ret->atoms[i]->type == XML_REGEXP_STRING) &&
(ret->atoms[i]->quant == XML_REGEXP_QUANT_ONCE)) {
value = ret->atoms[i]->valuep;
for (j = 0;j < nbatoms;j++) {
if (xmlStrEqual(stringMap[j], value)) {
stringRemap[i] = j;
break;
}
}
if (j >= nbatoms) {
stringRemap[i] = nbatoms;
stringMap[nbatoms] = xmlStrdup(value);
if (stringMap[nbatoms] == NULL) {
for (i = 0;i < nbatoms;i++)
xmlFree(stringMap[i]);
xmlFree(stringRemap);
xmlFree(stringMap);
xmlFree(stateRemap);
xmlFree(ret);
return(NULL);
}
nbatoms++;
}
} else {
xmlFree(stateRemap);
xmlFree(stringRemap);
for (i = 0;i < nbatoms;i++)
xmlFree(stringMap[i]);
xmlFree(stringMap);
xmlFree(ret);
return(NULL);
}
}
transitions = (int *) xmlRegCalloc2(nbstates + 1, nbatoms + 1,
sizeof(int));
if (transitions == NULL) {
xmlFree(stateRemap);
xmlFree(stringRemap);
for (i = 0;i < nbatoms;i++)
xmlFree(stringMap[i]);
xmlFree(stringMap);
xmlFree(ret);
return(NULL);
}
/*
* Allocate the transition table. The first entry for each
* state corresponds to the state type.
*/
transdata = NULL;
for (i = 0;i < ret->nbStates;i++) {
int stateno, atomno, targetno, prev;
xmlRegStatePtr state;
xmlRegTransPtr trans;
stateno = stateRemap[i];
if (stateno == -1)
continue;
state = ret->states[i];
transitions[stateno * (nbatoms + 1)] = state->type;
for (j = 0;j < state->nbTrans;j++) {
trans = &(state->trans[j]);
if ((trans->to < 0) || (trans->atom == NULL))
continue;
atomno = stringRemap[trans->atom->no];
if ((trans->atom->data != NULL) && (transdata == NULL)) {
transdata = (void **) xmlRegCalloc2(nbstates, nbatoms,
sizeof(void *));
if (transdata == NULL) {
xmlRegexpErrMemory(ctxt);
break;
}
}
targetno = stateRemap[trans->to];
/*
* if the same atom can generate transitions to 2 different
* states then it means the automata is not deterministic and
* the compact form can't be used !
*/
prev = transitions[stateno * (nbatoms + 1) + atomno + 1];
if (prev != 0) {
if (prev != targetno + 1) {
ret->determinist = 0;
if (transdata != NULL)
xmlFree(transdata);
xmlFree(transitions);
xmlFree(stateRemap);
xmlFree(stringRemap);
for (i = 0;i < nbatoms;i++)
xmlFree(stringMap[i]);
xmlFree(stringMap);
goto not_determ;
}
} else {
transitions[stateno * (nbatoms + 1) + atomno + 1] =
targetno + 1; /* to avoid 0 */
if (transdata != NULL)
transdata[stateno * nbatoms + atomno] =
trans->atom->data;
}
}
}
ret->determinist = 1;
/*
* Cleanup of the old data
*/
if (ret->states != NULL) {
for (i = 0;i < ret->nbStates;i++)
xmlRegFreeState(ret->states[i]);
xmlFree(ret->states);
}
ret->states = NULL;
ret->nbStates = 0;
if (ret->atoms != NULL) {
for (i = 0;i < ret->nbAtoms;i++)
xmlRegFreeAtom(ret->atoms[i]);
xmlFree(ret->atoms);
}
ret->atoms = NULL;
ret->nbAtoms = 0;
ret->compact = transitions;
ret->transdata = transdata;
ret->stringMap = stringMap;
ret->nbstrings = nbatoms;
ret->nbstates = nbstates;
xmlFree(stateRemap);
xmlFree(stringRemap);
}
not_determ:
ctxt->string = NULL;
ctxt->nbStates = 0;
ctxt->states = NULL;
ctxt->nbAtoms = 0;
ctxt->atoms = NULL;
ctxt->nbCounters = 0;
ctxt->counters = NULL;
return(ret);
}
/**
* xmlRegNewParserCtxt:
* @string: the string to parse
*
* Allocate a new regexp parser context
*
* Returns the new context or NULL in case of error
*/
static xmlRegParserCtxtPtr
xmlRegNewParserCtxt(const xmlChar *string) {
xmlRegParserCtxtPtr ret;
ret = (xmlRegParserCtxtPtr) xmlMalloc(sizeof(xmlRegParserCtxt));
if (ret == NULL)
return(NULL);
memset(ret, 0, sizeof(xmlRegParserCtxt));
if (string != NULL) {
ret->string = xmlStrdup(string);
if (ret->string == NULL) {
xmlFree(ret);
return(NULL);
}
}
ret->cur = ret->string;
ret->neg = 0;
ret->negs = 0;
ret->error = 0;
ret->determinist = -1;
return(ret);
}
/**
* xmlRegNewRange:
* @ctxt: the regexp parser context
* @neg: is that negative
* @type: the type of range
* @start: the start codepoint
* @end: the end codepoint
*
* Allocate a new regexp range
*
* Returns the new range or NULL in case of error
*/
static xmlRegRangePtr
xmlRegNewRange(xmlRegParserCtxtPtr ctxt,
int neg, xmlRegAtomType type, int start, int end) {
xmlRegRangePtr ret;
ret = (xmlRegRangePtr) xmlMalloc(sizeof(xmlRegRange));
if (ret == NULL) {
xmlRegexpErrMemory(ctxt);
return(NULL);
}
ret->neg = neg;
ret->type = type;
ret->start = start;
ret->end = end;
return(ret);
}
/**
* xmlRegFreeRange:
* @range: the regexp range
*
* Free a regexp range
*/
static void
xmlRegFreeRange(xmlRegRangePtr range) {
if (range == NULL)
return;
if (range->blockName != NULL)
xmlFree(range->blockName);
xmlFree(range);
}
/**
* xmlRegCopyRange:
* @range: the regexp range
*
* Copy a regexp range
*
* Returns the new copy or NULL in case of error.
*/
static xmlRegRangePtr
xmlRegCopyRange(xmlRegParserCtxtPtr ctxt, xmlRegRangePtr range) {
xmlRegRangePtr ret;
if (range == NULL)
return(NULL);
ret = xmlRegNewRange(ctxt, range->neg, range->type, range->start,
range->end);
if (ret == NULL)
return(NULL);
if (range->blockName != NULL) {
ret->blockName = xmlStrdup(range->blockName);
if (ret->blockName == NULL) {
xmlRegexpErrMemory(ctxt);
xmlRegFreeRange(ret);
return(NULL);
}
}
return(ret);
}
/**
* xmlRegNewAtom:
* @ctxt: the regexp parser context
* @type: the type of atom
*
* Allocate a new atom
*
* Returns the new atom or NULL in case of error
*/
static xmlRegAtomPtr
xmlRegNewAtom(xmlRegParserCtxtPtr ctxt, xmlRegAtomType type) {
xmlRegAtomPtr ret;
ret = (xmlRegAtomPtr) xmlMalloc(sizeof(xmlRegAtom));
if (ret == NULL) {
xmlRegexpErrMemory(ctxt);
return(NULL);
}
memset(ret, 0, sizeof(xmlRegAtom));
ret->type = type;
ret->quant = XML_REGEXP_QUANT_ONCE;
ret->min = 0;
ret->max = 0;
return(ret);
}
/**
* xmlRegFreeAtom:
* @atom: the regexp atom
*
* Free a regexp atom
*/
static void
xmlRegFreeAtom(xmlRegAtomPtr atom) {
int i;
if (atom == NULL)
return;
for (i = 0;i < atom->nbRanges;i++)
xmlRegFreeRange(atom->ranges[i]);
if (atom->ranges != NULL)
xmlFree(atom->ranges);
if ((atom->type == XML_REGEXP_STRING) && (atom->valuep != NULL))
xmlFree(atom->valuep);
if ((atom->type == XML_REGEXP_STRING) && (atom->valuep2 != NULL))
xmlFree(atom->valuep2);
if ((atom->type == XML_REGEXP_BLOCK_NAME) && (atom->valuep != NULL))
xmlFree(atom->valuep);
xmlFree(atom);
}
/**
* xmlRegCopyAtom:
* @ctxt: the regexp parser context
* @atom: the original atom
*
* Allocate a new regexp range
*
* Returns the new atom or NULL in case of error
*/
static xmlRegAtomPtr
xmlRegCopyAtom(xmlRegParserCtxtPtr ctxt, xmlRegAtomPtr atom) {
xmlRegAtomPtr ret;
ret = (xmlRegAtomPtr) xmlMalloc(sizeof(xmlRegAtom));
if (ret == NULL) {
xmlRegexpErrMemory(ctxt);
return(NULL);
}
memset(ret, 0, sizeof(xmlRegAtom));
ret->type = atom->type;
ret->quant = atom->quant;
ret->min = atom->min;
ret->max = atom->max;
if (atom->nbRanges > 0) {
int i;
ret->ranges = (xmlRegRangePtr *) xmlMalloc(sizeof(xmlRegRangePtr) *
atom->nbRanges);
if (ret->ranges == NULL) {
xmlRegexpErrMemory(ctxt);
goto error;
}
for (i = 0;i < atom->nbRanges;i++) {
ret->ranges[i] = xmlRegCopyRange(ctxt, atom->ranges[i]);
if (ret->ranges[i] == NULL)
goto error;
ret->nbRanges = i + 1;
}
}
return(ret);
error:
xmlRegFreeAtom(ret);
return(NULL);
}
static xmlRegStatePtr
xmlRegNewState(xmlRegParserCtxtPtr ctxt) {
xmlRegStatePtr ret;
ret = (xmlRegStatePtr) xmlMalloc(sizeof(xmlRegState));
if (ret == NULL) {
xmlRegexpErrMemory(ctxt);
return(NULL);
}
memset(ret, 0, sizeof(xmlRegState));
ret->type = XML_REGEXP_TRANS_STATE;
ret->mark = XML_REGEXP_MARK_NORMAL;
return(ret);
}
/**
* xmlRegFreeState:
* @state: the regexp state
*
* Free a regexp state
*/
static void
xmlRegFreeState(xmlRegStatePtr state) {
if (state == NULL)
return;
if (state->trans != NULL)
xmlFree(state->trans);
if (state->transTo != NULL)
xmlFree(state->transTo);
xmlFree(state);
}
/**
* xmlRegFreeParserCtxt:
* @ctxt: the regexp parser context
*
* Free a regexp parser context
*/
static void
xmlRegFreeParserCtxt(xmlRegParserCtxtPtr ctxt) {
int i;
if (ctxt == NULL)
return;
if (ctxt->string != NULL)
xmlFree(ctxt->string);
if (ctxt->states != NULL) {
for (i = 0;i < ctxt->nbStates;i++)
xmlRegFreeState(ctxt->states[i]);
xmlFree(ctxt->states);
}
if (ctxt->atoms != NULL) {
for (i = 0;i < ctxt->nbAtoms;i++)
xmlRegFreeAtom(ctxt->atoms[i]);
xmlFree(ctxt->atoms);
}
if (ctxt->counters != NULL)
xmlFree(ctxt->counters);
xmlFree(ctxt);
}
/************************************************************************
* *
* Display of Data structures *
* *
************************************************************************/
static void
xmlRegPrintAtomType(FILE *output, xmlRegAtomType type) {
switch (type) {
case XML_REGEXP_EPSILON:
fprintf(output, "epsilon "); break;
case XML_REGEXP_CHARVAL:
fprintf(output, "charval "); break;
case XML_REGEXP_RANGES:
fprintf(output, "ranges "); break;
case XML_REGEXP_SUBREG:
fprintf(output, "subexpr "); break;
case XML_REGEXP_STRING:
fprintf(output, "string "); break;
case XML_REGEXP_ANYCHAR:
fprintf(output, "anychar "); break;
case XML_REGEXP_ANYSPACE:
fprintf(output, "anyspace "); break;
case XML_REGEXP_NOTSPACE:
fprintf(output, "notspace "); break;
case XML_REGEXP_INITNAME:
fprintf(output, "initname "); break;
case XML_REGEXP_NOTINITNAME:
fprintf(output, "notinitname "); break;
case XML_REGEXP_NAMECHAR:
fprintf(output, "namechar "); break;
case XML_REGEXP_NOTNAMECHAR:
fprintf(output, "notnamechar "); break;
case XML_REGEXP_DECIMAL:
fprintf(output, "decimal "); break;
case XML_REGEXP_NOTDECIMAL:
fprintf(output, "notdecimal "); break;
case XML_REGEXP_REALCHAR:
fprintf(output, "realchar "); break;
case XML_REGEXP_NOTREALCHAR:
fprintf(output, "notrealchar "); break;
case XML_REGEXP_LETTER:
fprintf(output, "LETTER "); break;
case XML_REGEXP_LETTER_UPPERCASE:
fprintf(output, "LETTER_UPPERCASE "); break;
case XML_REGEXP_LETTER_LOWERCASE:
fprintf(output, "LETTER_LOWERCASE "); break;
case XML_REGEXP_LETTER_TITLECASE:
fprintf(output, "LETTER_TITLECASE "); break;
case XML_REGEXP_LETTER_MODIFIER:
fprintf(output, "LETTER_MODIFIER "); break;
case XML_REGEXP_LETTER_OTHERS:
fprintf(output, "LETTER_OTHERS "); break;
case XML_REGEXP_MARK:
fprintf(output, "MARK "); break;
case XML_REGEXP_MARK_NONSPACING:
fprintf(output, "MARK_NONSPACING "); break;
case XML_REGEXP_MARK_SPACECOMBINING:
fprintf(output, "MARK_SPACECOMBINING "); break;
case XML_REGEXP_MARK_ENCLOSING:
fprintf(output, "MARK_ENCLOSING "); break;
case XML_REGEXP_NUMBER:
fprintf(output, "NUMBER "); break;
case XML_REGEXP_NUMBER_DECIMAL:
fprintf(output, "NUMBER_DECIMAL "); break;
case XML_REGEXP_NUMBER_LETTER:
fprintf(output, "NUMBER_LETTER "); break;
case XML_REGEXP_NUMBER_OTHERS:
fprintf(output, "NUMBER_OTHERS "); break;
case XML_REGEXP_PUNCT:
fprintf(output, "PUNCT "); break;
case XML_REGEXP_PUNCT_CONNECTOR:
fprintf(output, "PUNCT_CONNECTOR "); break;
case XML_REGEXP_PUNCT_DASH:
fprintf(output, "PUNCT_DASH "); break;