This repository has been archived by the owner on Sep 24, 2022. It is now read-only.
forked from GNOME/libxml2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
relaxng.c
11123 lines (10489 loc) · 360 KB
/
relaxng.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
/*
* relaxng.c : implementation of the Relax-NG handling and validity checking
*
* See Copyright for the status of this software.
*
* Daniel Veillard <[email protected]>
*/
/**
* TODO:
* - add support for DTD compatibility spec
* http://www.oasis-open.org/committees/relax-ng/compatibility-20011203.html
* - report better mem allocations pbms at runtime and abort immediately.
*/
#define IN_LIBXML
#include "libxml.h"
#ifdef LIBXML_SCHEMAS_ENABLED
#include <string.h>
#include <stdio.h>
#include <stddef.h>
#include <libxml/xmlmemory.h>
#include <libxml/parser.h>
#include <libxml/parserInternals.h>
#include <libxml/hash.h>
#include <libxml/uri.h>
#include <libxml/relaxng.h>
#include <libxml/xmlschemastypes.h>
#include <libxml/xmlautomata.h>
#include <libxml/xmlregexp.h>
#include <libxml/xmlschemastypes.h>
/*
* The Relax-NG namespace
*/
static const xmlChar *xmlRelaxNGNs = (const xmlChar *)
"http://relaxng.org/ns/structure/1.0";
#define IS_RELAXNG(node, typ) \
((node != NULL) && (node->ns != NULL) && \
(node->type == XML_ELEMENT_NODE) && \
(xmlStrEqual(node->name, (const xmlChar *) typ)) && \
(xmlStrEqual(node->ns->href, xmlRelaxNGNs)))
#if 0
#define DEBUG 1
#define DEBUG_GRAMMAR 1
#define DEBUG_CONTENT 1
#define DEBUG_TYPE 1
#define DEBUG_VALID 1
#define DEBUG_INTERLEAVE 1
#define DEBUG_LIST 1
#define DEBUG_INCLUDE 1
#define DEBUG_ERROR 1
#define DEBUG_COMPILE 1
#define DEBUG_PROGRESSIVE 1
#endif
#define MAX_ERROR 5
#define TODO \
xmlGenericError(xmlGenericErrorContext, \
"Unimplemented block at %s:%d\n", \
__FILE__, __LINE__);
typedef struct _xmlRelaxNGSchema xmlRelaxNGSchema;
typedef xmlRelaxNGSchema *xmlRelaxNGSchemaPtr;
typedef struct _xmlRelaxNGDefine xmlRelaxNGDefine;
typedef xmlRelaxNGDefine *xmlRelaxNGDefinePtr;
typedef struct _xmlRelaxNGDocument xmlRelaxNGDocument;
typedef xmlRelaxNGDocument *xmlRelaxNGDocumentPtr;
typedef struct _xmlRelaxNGInclude xmlRelaxNGInclude;
typedef xmlRelaxNGInclude *xmlRelaxNGIncludePtr;
typedef enum {
XML_RELAXNG_COMBINE_UNDEFINED = 0, /* undefined */
XML_RELAXNG_COMBINE_CHOICE, /* choice */
XML_RELAXNG_COMBINE_INTERLEAVE /* interleave */
} xmlRelaxNGCombine;
typedef enum {
XML_RELAXNG_CONTENT_ERROR = -1,
XML_RELAXNG_CONTENT_EMPTY = 0,
XML_RELAXNG_CONTENT_SIMPLE,
XML_RELAXNG_CONTENT_COMPLEX
} xmlRelaxNGContentType;
typedef struct _xmlRelaxNGGrammar xmlRelaxNGGrammar;
typedef xmlRelaxNGGrammar *xmlRelaxNGGrammarPtr;
struct _xmlRelaxNGGrammar {
xmlRelaxNGGrammarPtr parent; /* the parent grammar if any */
xmlRelaxNGGrammarPtr children; /* the children grammar if any */
xmlRelaxNGGrammarPtr next; /* the next grammar if any */
xmlRelaxNGDefinePtr start; /* <start> content */
xmlRelaxNGCombine combine; /* the default combine value */
xmlRelaxNGDefinePtr startList; /* list of <start> definitions */
xmlHashTablePtr defs; /* define* */
xmlHashTablePtr refs; /* references */
};
typedef enum {
XML_RELAXNG_NOOP = -1, /* a no operation from simplification */
XML_RELAXNG_EMPTY = 0, /* an empty pattern */
XML_RELAXNG_NOT_ALLOWED, /* not allowed top */
XML_RELAXNG_EXCEPT, /* except present in nameclass defs */
XML_RELAXNG_TEXT, /* textual content */
XML_RELAXNG_ELEMENT, /* an element */
XML_RELAXNG_DATATYPE, /* external data type definition */
XML_RELAXNG_PARAM, /* external data type parameter */
XML_RELAXNG_VALUE, /* value from an external data type definition */
XML_RELAXNG_LIST, /* a list of patterns */
XML_RELAXNG_ATTRIBUTE, /* an attribute following a pattern */
XML_RELAXNG_DEF, /* a definition */
XML_RELAXNG_REF, /* reference to a definition */
XML_RELAXNG_EXTERNALREF, /* reference to an external def */
XML_RELAXNG_PARENTREF, /* reference to a def in the parent grammar */
XML_RELAXNG_OPTIONAL, /* optional patterns */
XML_RELAXNG_ZEROORMORE, /* zero or more non empty patterns */
XML_RELAXNG_ONEORMORE, /* one or more non empty patterns */
XML_RELAXNG_CHOICE, /* a choice between non empty patterns */
XML_RELAXNG_GROUP, /* a pair/group of non empty patterns */
XML_RELAXNG_INTERLEAVE, /* interleaving choice of non-empty patterns */
XML_RELAXNG_START /* Used to keep track of starts on grammars */
} xmlRelaxNGType;
#define IS_NULLABLE (1 << 0)
#define IS_NOT_NULLABLE (1 << 1)
#define IS_INDETERMINIST (1 << 2)
#define IS_MIXED (1 << 3)
#define IS_TRIABLE (1 << 4)
#define IS_PROCESSED (1 << 5)
#define IS_COMPILABLE (1 << 6)
#define IS_NOT_COMPILABLE (1 << 7)
#define IS_EXTERNAL_REF (1 << 8)
struct _xmlRelaxNGDefine {
xmlRelaxNGType type; /* the type of definition */
xmlNodePtr node; /* the node in the source */
xmlChar *name; /* the element local name if present */
xmlChar *ns; /* the namespace local name if present */
xmlChar *value; /* value when available */
void *data; /* data lib or specific pointer */
xmlRelaxNGDefinePtr content; /* the expected content */
xmlRelaxNGDefinePtr parent; /* the parent definition, if any */
xmlRelaxNGDefinePtr next; /* list within grouping sequences */
xmlRelaxNGDefinePtr attrs; /* list of attributes for elements */
xmlRelaxNGDefinePtr nameClass; /* the nameClass definition if any */
xmlRelaxNGDefinePtr nextHash; /* next define in defs/refs hash tables */
short depth; /* used for the cycle detection */
short dflags; /* define related flags */
xmlRegexpPtr contModel; /* a compiled content model if available */
};
/**
* _xmlRelaxNG:
*
* A RelaxNGs definition
*/
struct _xmlRelaxNG {
void *_private; /* unused by the library for users or bindings */
xmlRelaxNGGrammarPtr topgrammar;
xmlDocPtr doc;
int idref; /* requires idref checking */
xmlHashTablePtr defs; /* define */
xmlHashTablePtr refs; /* references */
xmlRelaxNGDocumentPtr documents; /* all the documents loaded */
xmlRelaxNGIncludePtr includes; /* all the includes loaded */
int defNr; /* number of defines used */
xmlRelaxNGDefinePtr *defTab; /* pointer to the allocated definitions */
};
#define XML_RELAXNG_IN_ATTRIBUTE (1 << 0)
#define XML_RELAXNG_IN_ONEORMORE (1 << 1)
#define XML_RELAXNG_IN_LIST (1 << 2)
#define XML_RELAXNG_IN_DATAEXCEPT (1 << 3)
#define XML_RELAXNG_IN_START (1 << 4)
#define XML_RELAXNG_IN_OOMGROUP (1 << 5)
#define XML_RELAXNG_IN_OOMINTERLEAVE (1 << 6)
#define XML_RELAXNG_IN_EXTERNALREF (1 << 7)
#define XML_RELAXNG_IN_ANYEXCEPT (1 << 8)
#define XML_RELAXNG_IN_NSEXCEPT (1 << 9)
struct _xmlRelaxNGParserCtxt {
void *userData; /* user specific data block */
xmlRelaxNGValidityErrorFunc error; /* the callback in case of errors */
xmlRelaxNGValidityWarningFunc warning; /* the callback in case of warning */
xmlStructuredErrorFunc serror;
xmlRelaxNGValidErr err;
xmlRelaxNGPtr schema; /* The schema in use */
xmlRelaxNGGrammarPtr grammar; /* the current grammar */
xmlRelaxNGGrammarPtr parentgrammar; /* the parent grammar */
int flags; /* parser flags */
int nbErrors; /* number of errors at parse time */
int nbWarnings; /* number of warnings at parse time */
const xmlChar *define; /* the current define scope */
xmlRelaxNGDefinePtr def; /* the current define */
int nbInterleaves;
xmlHashTablePtr interleaves; /* keep track of all the interleaves */
xmlRelaxNGDocumentPtr documents; /* all the documents loaded */
xmlRelaxNGIncludePtr includes; /* all the includes loaded */
xmlChar *URL;
xmlDocPtr document;
int defNr; /* number of defines used */
int defMax; /* number of defines allocated */
xmlRelaxNGDefinePtr *defTab; /* pointer to the allocated definitions */
const char *buffer;
int size;
/* the document stack */
xmlRelaxNGDocumentPtr doc; /* Current parsed external ref */
int docNr; /* Depth of the parsing stack */
int docMax; /* Max depth of the parsing stack */
xmlRelaxNGDocumentPtr *docTab; /* array of docs */
/* the include stack */
xmlRelaxNGIncludePtr inc; /* Current parsed include */
int incNr; /* Depth of the include parsing stack */
int incMax; /* Max depth of the parsing stack */
xmlRelaxNGIncludePtr *incTab; /* array of incs */
int idref; /* requires idref checking */
/* used to compile content models */
xmlAutomataPtr am; /* the automata */
xmlAutomataStatePtr state; /* used to build the automata */
int crng; /* compact syntax and other flags */
int freedoc; /* need to free the document */
};
#define FLAGS_IGNORABLE 1
#define FLAGS_NEGATIVE 2
#define FLAGS_MIXED_CONTENT 4
#define FLAGS_NOERROR 8
/**
* xmlRelaxNGInterleaveGroup:
*
* A RelaxNGs partition set associated to lists of definitions
*/
typedef struct _xmlRelaxNGInterleaveGroup xmlRelaxNGInterleaveGroup;
typedef xmlRelaxNGInterleaveGroup *xmlRelaxNGInterleaveGroupPtr;
struct _xmlRelaxNGInterleaveGroup {
xmlRelaxNGDefinePtr rule; /* the rule to satisfy */
xmlRelaxNGDefinePtr *defs; /* the array of element definitions */
xmlRelaxNGDefinePtr *attrs; /* the array of attributes definitions */
};
#define IS_DETERMINIST 1
#define IS_NEEDCHECK 2
/**
* xmlRelaxNGPartitions:
*
* A RelaxNGs partition associated to an interleave group
*/
typedef struct _xmlRelaxNGPartition xmlRelaxNGPartition;
typedef xmlRelaxNGPartition *xmlRelaxNGPartitionPtr;
struct _xmlRelaxNGPartition {
int nbgroups; /* number of groups in the partitions */
xmlHashTablePtr triage; /* hash table used to direct nodes to the
* right group when possible */
int flags; /* determinist ? */
xmlRelaxNGInterleaveGroupPtr *groups;
};
/**
* xmlRelaxNGValidState:
*
* A RelaxNGs validation state
*/
#define MAX_ATTR 20
typedef struct _xmlRelaxNGValidState xmlRelaxNGValidState;
typedef xmlRelaxNGValidState *xmlRelaxNGValidStatePtr;
struct _xmlRelaxNGValidState {
xmlNodePtr node; /* the current node */
xmlNodePtr seq; /* the sequence of children left to validate */
int nbAttrs; /* the number of attributes */
int maxAttrs; /* the size of attrs */
int nbAttrLeft; /* the number of attributes left to validate */
xmlChar *value; /* the value when operating on string */
xmlChar *endvalue; /* the end value when operating on string */
xmlAttrPtr *attrs; /* the array of attributes */
};
/**
* xmlRelaxNGStates:
*
* A RelaxNGs container for validation state
*/
typedef struct _xmlRelaxNGStates xmlRelaxNGStates;
typedef xmlRelaxNGStates *xmlRelaxNGStatesPtr;
struct _xmlRelaxNGStates {
int nbState; /* the number of states */
int maxState; /* the size of the array */
xmlRelaxNGValidStatePtr *tabState;
};
#define ERROR_IS_DUP 1
/**
* xmlRelaxNGValidError:
*
* A RelaxNGs validation error
*/
typedef struct _xmlRelaxNGValidError xmlRelaxNGValidError;
typedef xmlRelaxNGValidError *xmlRelaxNGValidErrorPtr;
struct _xmlRelaxNGValidError {
xmlRelaxNGValidErr err; /* the error number */
int flags; /* flags */
xmlNodePtr node; /* the current node */
xmlNodePtr seq; /* the current child */
const xmlChar *arg1; /* first arg */
const xmlChar *arg2; /* second arg */
};
/**
* xmlRelaxNGValidCtxt:
*
* A RelaxNGs validation context
*/
struct _xmlRelaxNGValidCtxt {
void *userData; /* user specific data block */
xmlRelaxNGValidityErrorFunc error; /* the callback in case of errors */
xmlRelaxNGValidityWarningFunc warning; /* the callback in case of warning */
xmlStructuredErrorFunc serror;
int nbErrors; /* number of errors in validation */
xmlRelaxNGPtr schema; /* The schema in use */
xmlDocPtr doc; /* the document being validated */
int flags; /* validation flags */
int depth; /* validation depth */
int idref; /* requires idref checking */
int errNo; /* the first error found */
/*
* Errors accumulated in branches may have to be stacked to be
* provided back when it's sure they affect validation.
*/
xmlRelaxNGValidErrorPtr err; /* Last error */
int errNr; /* Depth of the error stack */
int errMax; /* Max depth of the error stack */
xmlRelaxNGValidErrorPtr errTab; /* stack of errors */
xmlRelaxNGValidStatePtr state; /* the current validation state */
xmlRelaxNGStatesPtr states; /* the accumulated state list */
xmlRelaxNGStatesPtr freeState; /* the pool of free valid states */
int freeStatesNr;
int freeStatesMax;
xmlRelaxNGStatesPtr *freeStates; /* the pool of free state groups */
/*
* This is used for "progressive" validation
*/
xmlRegExecCtxtPtr elem; /* the current element regexp */
int elemNr; /* the number of element validated */
int elemMax; /* the max depth of elements */
xmlRegExecCtxtPtr *elemTab; /* the stack of regexp runtime */
int pstate; /* progressive state */
xmlNodePtr pnode; /* the current node */
xmlRelaxNGDefinePtr pdef; /* the non-streamable definition */
int perr; /* signal error in content model
* outside the regexp */
};
/**
* xmlRelaxNGInclude:
*
* Structure associated to a RelaxNGs document element
*/
struct _xmlRelaxNGInclude {
xmlRelaxNGIncludePtr next; /* keep a chain of includes */
xmlChar *href; /* the normalized href value */
xmlDocPtr doc; /* the associated XML document */
xmlRelaxNGDefinePtr content; /* the definitions */
xmlRelaxNGPtr schema; /* the schema */
};
/**
* xmlRelaxNGDocument:
*
* Structure associated to a RelaxNGs document element
*/
struct _xmlRelaxNGDocument {
xmlRelaxNGDocumentPtr next; /* keep a chain of documents */
xmlChar *href; /* the normalized href value */
xmlDocPtr doc; /* the associated XML document */
xmlRelaxNGDefinePtr content; /* the definitions */
xmlRelaxNGPtr schema; /* the schema */
int externalRef; /* 1 if an external ref */
};
/************************************************************************
* *
* Some factorized error routines *
* *
************************************************************************/
/**
* xmlRngPErrMemory:
* @ctxt: an Relax-NG parser context
* @extra: extra information
*
* Handle a redefinition of attribute error
*/
static void
xmlRngPErrMemory(xmlRelaxNGParserCtxtPtr ctxt, const char *extra)
{
xmlStructuredErrorFunc schannel = NULL;
xmlGenericErrorFunc channel = NULL;
void *data = NULL;
if (ctxt != NULL) {
if (ctxt->serror != NULL)
schannel = ctxt->serror;
else
channel = ctxt->error;
data = ctxt->userData;
ctxt->nbErrors++;
}
if (extra)
__xmlRaiseError(schannel, channel, data,
NULL, NULL, XML_FROM_RELAXNGP,
XML_ERR_NO_MEMORY, XML_ERR_FATAL, NULL, 0, extra,
NULL, NULL, 0, 0,
"Memory allocation failed : %s\n", extra);
else
__xmlRaiseError(schannel, channel, data,
NULL, NULL, XML_FROM_RELAXNGP,
XML_ERR_NO_MEMORY, XML_ERR_FATAL, NULL, 0, NULL,
NULL, NULL, 0, 0, "Memory allocation failed\n");
}
/**
* xmlRngVErrMemory:
* @ctxt: a Relax-NG validation context
* @extra: extra information
*
* Handle a redefinition of attribute error
*/
static void
xmlRngVErrMemory(xmlRelaxNGValidCtxtPtr ctxt, const char *extra)
{
xmlStructuredErrorFunc schannel = NULL;
xmlGenericErrorFunc channel = NULL;
void *data = NULL;
if (ctxt != NULL) {
if (ctxt->serror != NULL)
schannel = ctxt->serror;
else
channel = ctxt->error;
data = ctxt->userData;
ctxt->nbErrors++;
}
if (extra)
__xmlRaiseError(schannel, channel, data,
NULL, NULL, XML_FROM_RELAXNGV,
XML_ERR_NO_MEMORY, XML_ERR_FATAL, NULL, 0, extra,
NULL, NULL, 0, 0,
"Memory allocation failed : %s\n", extra);
else
__xmlRaiseError(schannel, channel, data,
NULL, NULL, XML_FROM_RELAXNGV,
XML_ERR_NO_MEMORY, XML_ERR_FATAL, NULL, 0, NULL,
NULL, NULL, 0, 0, "Memory allocation failed\n");
}
/**
* xmlRngPErr:
* @ctxt: a Relax-NG parser context
* @node: the node raising the error
* @error: the error code
* @msg: message
* @str1: extra info
* @str2: extra info
*
* Handle a Relax NG Parsing error
*/
static void LIBXML_ATTR_FORMAT(4,0)
xmlRngPErr(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node, int error,
const char *msg, const xmlChar * str1, const xmlChar * str2)
{
xmlStructuredErrorFunc schannel = NULL;
xmlGenericErrorFunc channel = NULL;
void *data = NULL;
if (ctxt != NULL) {
if (ctxt->serror != NULL)
schannel = ctxt->serror;
else
channel = ctxt->error;
data = ctxt->userData;
ctxt->nbErrors++;
}
__xmlRaiseError(schannel, channel, data,
NULL, node, XML_FROM_RELAXNGP,
error, XML_ERR_ERROR, NULL, 0,
(const char *) str1, (const char *) str2, NULL, 0, 0,
msg, str1, str2);
}
/**
* xmlRngVErr:
* @ctxt: a Relax-NG validation context
* @node: the node raising the error
* @error: the error code
* @msg: message
* @str1: extra info
* @str2: extra info
*
* Handle a Relax NG Validation error
*/
static void LIBXML_ATTR_FORMAT(4,0)
xmlRngVErr(xmlRelaxNGValidCtxtPtr ctxt, xmlNodePtr node, int error,
const char *msg, const xmlChar * str1, const xmlChar * str2)
{
xmlStructuredErrorFunc schannel = NULL;
xmlGenericErrorFunc channel = NULL;
void *data = NULL;
if (ctxt != NULL) {
if (ctxt->serror != NULL)
schannel = ctxt->serror;
else
channel = ctxt->error;
data = ctxt->userData;
ctxt->nbErrors++;
}
__xmlRaiseError(schannel, channel, data,
NULL, node, XML_FROM_RELAXNGV,
error, XML_ERR_ERROR, NULL, 0,
(const char *) str1, (const char *) str2, NULL, 0, 0,
msg, str1, str2);
}
/************************************************************************
* *
* Preliminary type checking interfaces *
* *
************************************************************************/
/**
* xmlRelaxNGTypeHave:
* @data: data needed for the library
* @type: the type name
* @value: the value to check
*
* Function provided by a type library to check if a type is exported
*
* Returns 1 if yes, 0 if no and -1 in case of error.
*/
typedef int (*xmlRelaxNGTypeHave) (void *data, const xmlChar * type);
/**
* xmlRelaxNGTypeCheck:
* @data: data needed for the library
* @type: the type name
* @value: the value to check
* @result: place to store the result if needed
*
* Function provided by a type library to check if a value match a type
*
* Returns 1 if yes, 0 if no and -1 in case of error.
*/
typedef int (*xmlRelaxNGTypeCheck) (void *data, const xmlChar * type,
const xmlChar * value, void **result,
xmlNodePtr node);
/**
* xmlRelaxNGFacetCheck:
* @data: data needed for the library
* @type: the type name
* @facet: the facet name
* @val: the facet value
* @strval: the string value
* @value: the value to check
*
* Function provided by a type library to check a value facet
*
* Returns 1 if yes, 0 if no and -1 in case of error.
*/
typedef int (*xmlRelaxNGFacetCheck) (void *data, const xmlChar * type,
const xmlChar * facet,
const xmlChar * val,
const xmlChar * strval, void *value);
/**
* xmlRelaxNGTypeFree:
* @data: data needed for the library
* @result: the value to free
*
* Function provided by a type library to free a returned result
*/
typedef void (*xmlRelaxNGTypeFree) (void *data, void *result);
/**
* xmlRelaxNGTypeCompare:
* @data: data needed for the library
* @type: the type name
* @value1: the first value
* @value2: the second value
*
* Function provided by a type library to compare two values accordingly
* to a type.
*
* Returns 1 if yes, 0 if no and -1 in case of error.
*/
typedef int (*xmlRelaxNGTypeCompare) (void *data, const xmlChar * type,
const xmlChar * value1,
xmlNodePtr ctxt1,
void *comp1,
const xmlChar * value2,
xmlNodePtr ctxt2);
typedef struct _xmlRelaxNGTypeLibrary xmlRelaxNGTypeLibrary;
typedef xmlRelaxNGTypeLibrary *xmlRelaxNGTypeLibraryPtr;
struct _xmlRelaxNGTypeLibrary {
const xmlChar *namespace; /* the datatypeLibrary value */
void *data; /* data needed for the library */
xmlRelaxNGTypeHave have; /* the export function */
xmlRelaxNGTypeCheck check; /* the checking function */
xmlRelaxNGTypeCompare comp; /* the compare function */
xmlRelaxNGFacetCheck facet; /* the facet check function */
xmlRelaxNGTypeFree freef; /* the freeing function */
};
/************************************************************************
* *
* Allocation functions *
* *
************************************************************************/
static void xmlRelaxNGFreeGrammar(xmlRelaxNGGrammarPtr grammar);
static void xmlRelaxNGFreeDefine(xmlRelaxNGDefinePtr define);
static void xmlRelaxNGNormExtSpace(xmlChar * value);
static void xmlRelaxNGFreeInnerSchema(xmlRelaxNGPtr schema);
static int xmlRelaxNGEqualValidState(xmlRelaxNGValidCtxtPtr ctxt
ATTRIBUTE_UNUSED,
xmlRelaxNGValidStatePtr state1,
xmlRelaxNGValidStatePtr state2);
static void xmlRelaxNGFreeValidState(xmlRelaxNGValidCtxtPtr ctxt,
xmlRelaxNGValidStatePtr state);
/**
* xmlRelaxNGFreeDocument:
* @docu: a document structure
*
* Deallocate a RelaxNG document structure.
*/
static void
xmlRelaxNGFreeDocument(xmlRelaxNGDocumentPtr docu)
{
if (docu == NULL)
return;
if (docu->href != NULL)
xmlFree(docu->href);
if (docu->doc != NULL)
xmlFreeDoc(docu->doc);
if (docu->schema != NULL)
xmlRelaxNGFreeInnerSchema(docu->schema);
xmlFree(docu);
}
/**
* xmlRelaxNGFreeDocumentList:
* @docu: a list of document structure
*
* Deallocate a RelaxNG document structures.
*/
static void
xmlRelaxNGFreeDocumentList(xmlRelaxNGDocumentPtr docu)
{
xmlRelaxNGDocumentPtr next;
while (docu != NULL) {
next = docu->next;
xmlRelaxNGFreeDocument(docu);
docu = next;
}
}
/**
* xmlRelaxNGFreeInclude:
* @incl: a include structure
*
* Deallocate a RelaxNG include structure.
*/
static void
xmlRelaxNGFreeInclude(xmlRelaxNGIncludePtr incl)
{
if (incl == NULL)
return;
if (incl->href != NULL)
xmlFree(incl->href);
if (incl->doc != NULL)
xmlFreeDoc(incl->doc);
if (incl->schema != NULL)
xmlRelaxNGFree(incl->schema);
xmlFree(incl);
}
/**
* xmlRelaxNGFreeIncludeList:
* @incl: a include structure list
*
* Deallocate a RelaxNG include structure.
*/
static void
xmlRelaxNGFreeIncludeList(xmlRelaxNGIncludePtr incl)
{
xmlRelaxNGIncludePtr next;
while (incl != NULL) {
next = incl->next;
xmlRelaxNGFreeInclude(incl);
incl = next;
}
}
/**
* xmlRelaxNGNewRelaxNG:
* @ctxt: a Relax-NG validation context (optional)
*
* Allocate a new RelaxNG structure.
*
* Returns the newly allocated structure or NULL in case or error
*/
static xmlRelaxNGPtr
xmlRelaxNGNewRelaxNG(xmlRelaxNGParserCtxtPtr ctxt)
{
xmlRelaxNGPtr ret;
ret = (xmlRelaxNGPtr) xmlMalloc(sizeof(xmlRelaxNG));
if (ret == NULL) {
xmlRngPErrMemory(ctxt, NULL);
return (NULL);
}
memset(ret, 0, sizeof(xmlRelaxNG));
return (ret);
}
/**
* xmlRelaxNGFreeInnerSchema:
* @schema: a schema structure
*
* Deallocate a RelaxNG schema structure.
*/
static void
xmlRelaxNGFreeInnerSchema(xmlRelaxNGPtr schema)
{
if (schema == NULL)
return;
if (schema->doc != NULL)
xmlFreeDoc(schema->doc);
if (schema->defTab != NULL) {
int i;
for (i = 0; i < schema->defNr; i++)
xmlRelaxNGFreeDefine(schema->defTab[i]);
xmlFree(schema->defTab);
}
xmlFree(schema);
}
/**
* xmlRelaxNGFree:
* @schema: a schema structure
*
* Deallocate a RelaxNG structure.
*/
void
xmlRelaxNGFree(xmlRelaxNGPtr schema)
{
if (schema == NULL)
return;
if (schema->topgrammar != NULL)
xmlRelaxNGFreeGrammar(schema->topgrammar);
if (schema->doc != NULL)
xmlFreeDoc(schema->doc);
if (schema->documents != NULL)
xmlRelaxNGFreeDocumentList(schema->documents);
if (schema->includes != NULL)
xmlRelaxNGFreeIncludeList(schema->includes);
if (schema->defTab != NULL) {
int i;
for (i = 0; i < schema->defNr; i++)
xmlRelaxNGFreeDefine(schema->defTab[i]);
xmlFree(schema->defTab);
}
xmlFree(schema);
}
/**
* xmlRelaxNGNewGrammar:
* @ctxt: a Relax-NG validation context (optional)
*
* Allocate a new RelaxNG grammar.
*
* Returns the newly allocated structure or NULL in case or error
*/
static xmlRelaxNGGrammarPtr
xmlRelaxNGNewGrammar(xmlRelaxNGParserCtxtPtr ctxt)
{
xmlRelaxNGGrammarPtr ret;
ret = (xmlRelaxNGGrammarPtr) xmlMalloc(sizeof(xmlRelaxNGGrammar));
if (ret == NULL) {
xmlRngPErrMemory(ctxt, NULL);
return (NULL);
}
memset(ret, 0, sizeof(xmlRelaxNGGrammar));
return (ret);
}
/**
* xmlRelaxNGFreeGrammar:
* @grammar: a grammar structure
*
* Deallocate a RelaxNG grammar structure.
*/
static void
xmlRelaxNGFreeGrammar(xmlRelaxNGGrammarPtr grammar)
{
if (grammar == NULL)
return;
if (grammar->children != NULL) {
xmlRelaxNGFreeGrammar(grammar->children);
}
if (grammar->next != NULL) {
xmlRelaxNGFreeGrammar(grammar->next);
}
if (grammar->refs != NULL) {
xmlHashFree(grammar->refs, NULL);
}
if (grammar->defs != NULL) {
xmlHashFree(grammar->defs, NULL);
}
xmlFree(grammar);
}
/**
* xmlRelaxNGNewDefine:
* @ctxt: a Relax-NG validation context
* @node: the node in the input document.
*
* Allocate a new RelaxNG define.
*
* Returns the newly allocated structure or NULL in case or error
*/
static xmlRelaxNGDefinePtr
xmlRelaxNGNewDefine(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node)
{
xmlRelaxNGDefinePtr ret;
if (ctxt->defMax == 0) {
ctxt->defMax = 16;
ctxt->defNr = 0;
ctxt->defTab = (xmlRelaxNGDefinePtr *)
xmlMalloc(ctxt->defMax * sizeof(xmlRelaxNGDefinePtr));
if (ctxt->defTab == NULL) {
xmlRngPErrMemory(ctxt, "allocating define\n");
return (NULL);
}
} else if (ctxt->defMax <= ctxt->defNr) {
xmlRelaxNGDefinePtr *tmp;
ctxt->defMax *= 2;
tmp = (xmlRelaxNGDefinePtr *) xmlRealloc(ctxt->defTab,
ctxt->defMax *
sizeof
(xmlRelaxNGDefinePtr));
if (tmp == NULL) {
xmlRngPErrMemory(ctxt, "allocating define\n");
return (NULL);
}
ctxt->defTab = tmp;
}
ret = (xmlRelaxNGDefinePtr) xmlMalloc(sizeof(xmlRelaxNGDefine));
if (ret == NULL) {
xmlRngPErrMemory(ctxt, "allocating define\n");
return (NULL);
}
memset(ret, 0, sizeof(xmlRelaxNGDefine));
ctxt->defTab[ctxt->defNr++] = ret;
ret->node = node;
ret->depth = -1;
return (ret);
}
/**
* xmlRelaxNGFreePartition:
* @partitions: a partition set structure
*
* Deallocate RelaxNG partition set structures.
*/
static void
xmlRelaxNGFreePartition(xmlRelaxNGPartitionPtr partitions)
{
xmlRelaxNGInterleaveGroupPtr group;
int j;
if (partitions != NULL) {
if (partitions->groups != NULL) {
for (j = 0; j < partitions->nbgroups; j++) {
group = partitions->groups[j];
if (group != NULL) {
if (group->defs != NULL)
xmlFree(group->defs);
if (group->attrs != NULL)
xmlFree(group->attrs);
xmlFree(group);
}
}
xmlFree(partitions->groups);
}
if (partitions->triage != NULL) {
xmlHashFree(partitions->triage, NULL);
}
xmlFree(partitions);
}
}
/**
* xmlRelaxNGFreeDefine:
* @define: a define structure
*
* Deallocate a RelaxNG define structure.
*/
static void
xmlRelaxNGFreeDefine(xmlRelaxNGDefinePtr define)
{
if (define == NULL)
return;
if ((define->type == XML_RELAXNG_VALUE) && (define->attrs != NULL)) {
xmlRelaxNGTypeLibraryPtr lib;
lib = (xmlRelaxNGTypeLibraryPtr) define->data;
if ((lib != NULL) && (lib->freef != NULL))
lib->freef(lib->data, (void *) define->attrs);
}
if ((define->data != NULL) && (define->type == XML_RELAXNG_INTERLEAVE))
xmlRelaxNGFreePartition((xmlRelaxNGPartitionPtr) define->data);
if ((define->data != NULL) && (define->type == XML_RELAXNG_CHOICE))
xmlHashFree((xmlHashTablePtr) define->data, NULL);
if (define->name != NULL)
xmlFree(define->name);
if (define->ns != NULL)
xmlFree(define->ns);
if (define->value != NULL)
xmlFree(define->value);
if (define->contModel != NULL)
xmlRegFreeRegexp(define->contModel);
xmlFree(define);
}
/**
* xmlRelaxNGNewStates:
* @ctxt: a Relax-NG validation context